2 * CXL Flash Device Driver
4 * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
5 * Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
7 * Copyright (C) 2015 IBM Corporation
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
15 #include <linux/delay.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/pci.h>
20 #include <asm/unaligned.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_host.h>
26 #include <uapi/scsi/cxlflash_ioctl.h>
32 MODULE_DESCRIPTION(CXLFLASH_ADAPTER_NAME
);
33 MODULE_AUTHOR("Manoj N. Kumar <manoj@linux.vnet.ibm.com>");
34 MODULE_AUTHOR("Matthew R. Ochs <mrochs@linux.vnet.ibm.com>");
35 MODULE_LICENSE("GPL");
38 * cmd_checkout() - checks out an AFU command
39 * @afu: AFU to checkout from.
41 * Commands are checked out in a round-robin fashion. Note that since
42 * the command pool is larger than the hardware queue, the majority of
43 * times we will only loop once or twice before getting a command. The
44 * buffer and CDB within the command are initialized (zeroed) prior to
47 * Return: The checked out command or NULL when command pool is empty.
49 static struct afu_cmd
*cmd_checkout(struct afu
*afu
)
51 int k
, dec
= CXLFLASH_NUM_CMDS
;
55 k
= (afu
->cmd_couts
++ & (CXLFLASH_NUM_CMDS
- 1));
59 if (!atomic_dec_if_positive(&cmd
->free
)) {
60 pr_devel("%s: returning found index=%d cmd=%p\n",
61 __func__
, cmd
->slot
, cmd
);
62 memset(cmd
->buf
, 0, CMD_BUFSIZE
);
63 memset(cmd
->rcb
.cdb
, 0, sizeof(cmd
->rcb
.cdb
));
72 * cmd_checkin() - checks in an AFU command
73 * @cmd: AFU command to checkin.
75 * Safe to pass commands that have already been checked in. Several
76 * internal tracking fields are reset as part of the checkin. Note
77 * that these are intentionally reset prior to toggling the free bit
78 * to avoid clobbering values in the event that the command is checked
81 static void cmd_checkin(struct afu_cmd
*cmd
)
87 cmd
->sa
.host_use
[0] = 0; /* clears both completion and retry bytes */
89 if (unlikely(atomic_inc_return(&cmd
->free
) != 1)) {
90 pr_err("%s: Freeing cmd (%d) that is not in use!\n",
95 pr_devel("%s: released cmd %p index=%d\n", __func__
, cmd
, cmd
->slot
);
99 * process_cmd_err() - command error handler
100 * @cmd: AFU command that experienced the error.
101 * @scp: SCSI command associated with the AFU command in error.
103 * Translates error bits from AFU command to SCSI command results.
105 static void process_cmd_err(struct afu_cmd
*cmd
, struct scsi_cmnd
*scp
)
107 struct sisl_ioarcb
*ioarcb
;
108 struct sisl_ioasa
*ioasa
;
114 ioarcb
= &(cmd
->rcb
);
117 if (ioasa
->rc
.flags
& SISL_RC_FLAGS_UNDERRUN
) {
118 resid
= ioasa
->resid
;
119 scsi_set_resid(scp
, resid
);
120 pr_debug("%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
121 __func__
, cmd
, scp
, resid
);
124 if (ioasa
->rc
.flags
& SISL_RC_FLAGS_OVERRUN
) {
125 pr_debug("%s: cmd underrun cmd = %p scp = %p\n",
127 scp
->result
= (DID_ERROR
<< 16);
130 pr_debug("%s: cmd failed afu_rc=%d scsi_rc=%d fc_rc=%d "
131 "afu_extra=0x%X, scsi_extra=0x%X, fc_extra=0x%X\n",
132 __func__
, ioasa
->rc
.afu_rc
, ioasa
->rc
.scsi_rc
,
133 ioasa
->rc
.fc_rc
, ioasa
->afu_extra
, ioasa
->scsi_extra
,
136 if (ioasa
->rc
.scsi_rc
) {
137 /* We have a SCSI status */
138 if (ioasa
->rc
.flags
& SISL_RC_FLAGS_SENSE_VALID
) {
139 memcpy(scp
->sense_buffer
, ioasa
->sense_data
,
140 SISL_SENSE_DATA_LEN
);
141 scp
->result
= ioasa
->rc
.scsi_rc
;
143 scp
->result
= ioasa
->rc
.scsi_rc
| (DID_ERROR
<< 16);
147 * We encountered an error. Set scp->result based on nature
150 if (ioasa
->rc
.fc_rc
) {
151 /* We have an FC status */
152 switch (ioasa
->rc
.fc_rc
) {
153 case SISL_FC_RC_LINKDOWN
:
154 scp
->result
= (DID_REQUEUE
<< 16);
156 case SISL_FC_RC_RESID
:
157 /* This indicates an FCP resid underrun */
158 if (!(ioasa
->rc
.flags
& SISL_RC_FLAGS_OVERRUN
)) {
159 /* If the SISL_RC_FLAGS_OVERRUN flag was set,
160 * then we will handle this error else where.
161 * If not then we must handle it here.
162 * This is probably an AFU bug.
164 scp
->result
= (DID_ERROR
<< 16);
167 case SISL_FC_RC_RESIDERR
:
168 /* Resid mismatch between adapter and device */
169 case SISL_FC_RC_TGTABORT
:
170 case SISL_FC_RC_ABORTOK
:
171 case SISL_FC_RC_ABORTFAIL
:
172 case SISL_FC_RC_NOLOGI
:
173 case SISL_FC_RC_ABORTPEND
:
174 case SISL_FC_RC_WRABORTPEND
:
175 case SISL_FC_RC_NOEXP
:
176 case SISL_FC_RC_INUSE
:
177 scp
->result
= (DID_ERROR
<< 16);
182 if (ioasa
->rc
.afu_rc
) {
183 /* We have an AFU error */
184 switch (ioasa
->rc
.afu_rc
) {
185 case SISL_AFU_RC_NO_CHANNELS
:
186 scp
->result
= (DID_NO_CONNECT
<< 16);
188 case SISL_AFU_RC_DATA_DMA_ERR
:
189 switch (ioasa
->afu_extra
) {
190 case SISL_AFU_DMA_ERR_PAGE_IN
:
192 scp
->result
= (DID_IMM_RETRY
<< 16);
194 case SISL_AFU_DMA_ERR_INVALID_EA
:
196 scp
->result
= (DID_ERROR
<< 16);
199 case SISL_AFU_RC_OUT_OF_DATA_BUFS
:
201 scp
->result
= (DID_ALLOC_FAILURE
<< 16);
204 scp
->result
= (DID_ERROR
<< 16);
210 * cmd_complete() - command completion handler
211 * @cmd: AFU command that has completed.
213 * Prepares and submits command that has either completed or timed out to
214 * the SCSI stack. Checks AFU command back into command pool for non-internal
215 * (rcb.scp populated) commands.
217 static void cmd_complete(struct afu_cmd
*cmd
)
219 struct scsi_cmnd
*scp
;
221 struct afu
*afu
= cmd
->parent
;
222 struct cxlflash_cfg
*cfg
= afu
->parent
;
225 spin_lock_irqsave(&cmd
->slock
, lock_flags
);
226 cmd
->sa
.host_use_b
[0] |= B_DONE
;
227 spin_unlock_irqrestore(&cmd
->slock
, lock_flags
);
231 if (unlikely(cmd
->sa
.ioasc
))
232 process_cmd_err(cmd
, scp
);
234 scp
->result
= (DID_OK
<< 16);
236 cmd_is_tmf
= cmd
->cmd_tmf
;
237 cmd_checkin(cmd
); /* Don't use cmd after here */
239 pr_debug_ratelimited("%s: calling scsi_done scp=%p result=%X "
240 "ioasc=%d\n", __func__
, scp
, scp
->result
,
247 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
248 cfg
->tmf_active
= false;
249 wake_up_all_locked(&cfg
->tmf_waitq
);
250 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
253 complete(&cmd
->cevent
);
257 * context_reset() - timeout handler for AFU commands
258 * @cmd: AFU command that timed out.
260 * Sends a reset to the AFU.
262 static void context_reset(struct afu_cmd
*cmd
)
266 struct afu
*afu
= cmd
->parent
;
267 struct cxlflash_cfg
*cfg
= afu
->parent
;
268 struct device
*dev
= &cfg
->dev
->dev
;
271 pr_debug("%s: cmd=%p\n", __func__
, cmd
);
273 spin_lock_irqsave(&cmd
->slock
, lock_flags
);
275 /* Already completed? */
276 if (cmd
->sa
.host_use_b
[0] & B_DONE
) {
277 spin_unlock_irqrestore(&cmd
->slock
, lock_flags
);
281 cmd
->sa
.host_use_b
[0] |= (B_DONE
| B_ERROR
| B_TIMEOUT
);
282 spin_unlock_irqrestore(&cmd
->slock
, lock_flags
);
284 writeq_be(rrin
, &afu
->host_map
->ioarrin
);
286 rrin
= readq_be(&afu
->host_map
->ioarrin
);
289 /* Double delay each time */
291 } while (nretry
++ < MC_ROOM_RETRY_CNT
);
293 dev_dbg(dev
, "%s: returning rrin=0x%016llX nretry=%d\n",
294 __func__
, rrin
, nretry
);
298 * send_cmd() - sends an AFU command
299 * @afu: AFU associated with the host.
300 * @cmd: AFU command to send.
303 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
305 static int send_cmd(struct afu
*afu
, struct afu_cmd
*cmd
)
307 struct cxlflash_cfg
*cfg
= afu
->parent
;
308 struct device
*dev
= &cfg
->dev
->dev
;
315 * This routine is used by critical users such an AFU sync and to
316 * send a task management function (TMF). Thus we want to retry a
317 * bit before returning an error. To avoid the performance penalty
318 * of MMIO, we spread the update of 'room' over multiple commands.
321 newval
= atomic64_dec_if_positive(&afu
->room
);
324 room
= readq_be(&afu
->host_map
->cmd_room
);
325 atomic64_set(&afu
->room
, room
);
329 } while (nretry
++ < MC_ROOM_RETRY_CNT
);
331 dev_err(dev
, "%s: no cmd_room to send 0x%X\n",
332 __func__
, cmd
->rcb
.cdb
[0]);
335 } else if (unlikely(newval
< 0)) {
336 /* This should be rare. i.e. Only if two threads race and
337 * decrement before the MMIO read is done. In this case
338 * just benefit from the other thread having updated
341 if (nretry
++ < MC_ROOM_RETRY_CNT
) {
350 writeq_be((u64
)&cmd
->rcb
, &afu
->host_map
->ioarrin
);
352 pr_devel("%s: cmd=%p len=%d ea=%p rc=%d\n", __func__
, cmd
,
353 cmd
->rcb
.data_len
, (void *)cmd
->rcb
.data_ea
, rc
);
357 afu
->read_room
= true;
358 kref_get(&cfg
->afu
->mapcount
);
359 schedule_work(&cfg
->work_q
);
360 rc
= SCSI_MLQUEUE_HOST_BUSY
;
365 * wait_resp() - polls for a response or timeout to a sent AFU command
366 * @afu: AFU associated with the host.
367 * @cmd: AFU command that was sent.
369 static void wait_resp(struct afu
*afu
, struct afu_cmd
*cmd
)
371 ulong timeout
= msecs_to_jiffies(cmd
->rcb
.timeout
* 2 * 1000);
373 timeout
= wait_for_completion_timeout(&cmd
->cevent
, timeout
);
377 if (unlikely(cmd
->sa
.ioasc
!= 0))
378 pr_err("%s: CMD 0x%X failed, IOASC: flags 0x%X, afu_rc 0x%X, "
379 "scsi_rc 0x%X, fc_rc 0x%X\n", __func__
, cmd
->rcb
.cdb
[0],
380 cmd
->sa
.rc
.flags
, cmd
->sa
.rc
.afu_rc
, cmd
->sa
.rc
.scsi_rc
,
385 * send_tmf() - sends a Task Management Function (TMF)
386 * @afu: AFU to checkout from.
387 * @scp: SCSI command from stack.
388 * @tmfcmd: TMF command to send.
391 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
393 static int send_tmf(struct afu
*afu
, struct scsi_cmnd
*scp
, u64 tmfcmd
)
397 u32 port_sel
= scp
->device
->channel
+ 1;
399 struct Scsi_Host
*host
= scp
->device
->host
;
400 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
401 struct device
*dev
= &cfg
->dev
->dev
;
406 cmd
= cmd_checkout(afu
);
407 if (unlikely(!cmd
)) {
408 dev_err(dev
, "%s: could not get a free command\n", __func__
);
409 rc
= SCSI_MLQUEUE_HOST_BUSY
;
413 /* When Task Management Function is active do not send another */
414 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
416 wait_event_interruptible_lock_irq(cfg
->tmf_waitq
,
419 cfg
->tmf_active
= true;
421 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
423 cmd
->rcb
.ctx_id
= afu
->ctx_hndl
;
424 cmd
->rcb
.port_sel
= port_sel
;
425 cmd
->rcb
.lun_id
= lun_to_lunid(scp
->device
->lun
);
427 lflag
= SISL_REQ_FLAGS_TMF_CMD
;
429 cmd
->rcb
.req_flags
= (SISL_REQ_FLAGS_PORT_LUN_ID
|
430 SISL_REQ_FLAGS_SUP_UNDERRUN
| lflag
);
432 /* Stash the scp in the reserved field, for reuse during interrupt */
435 /* Copy the CDB from the cmd passed in */
436 memcpy(cmd
->rcb
.cdb
, &tmfcmd
, sizeof(tmfcmd
));
438 /* Send the command */
439 rc
= send_cmd(afu
, cmd
);
442 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
443 cfg
->tmf_active
= false;
444 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
448 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
449 to
= msecs_to_jiffies(5000);
450 to
= wait_event_interruptible_lock_irq_timeout(cfg
->tmf_waitq
,
455 cfg
->tmf_active
= false;
456 dev_err(dev
, "%s: TMF timed out!\n", __func__
);
459 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
464 static void afu_unmap(struct kref
*ref
)
466 struct afu
*afu
= container_of(ref
, struct afu
, mapcount
);
468 if (likely(afu
->afu_map
)) {
469 cxl_psa_unmap((void __iomem
*)afu
->afu_map
);
475 * cxlflash_driver_info() - information handler for this host driver
476 * @host: SCSI host associated with device.
478 * Return: A string describing the device.
480 static const char *cxlflash_driver_info(struct Scsi_Host
*host
)
482 return CXLFLASH_ADAPTER_NAME
;
486 * cxlflash_queuecommand() - sends a mid-layer request
487 * @host: SCSI host associated with device.
488 * @scp: SCSI command to send.
490 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
492 static int cxlflash_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*scp
)
494 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
495 struct afu
*afu
= cfg
->afu
;
496 struct device
*dev
= &cfg
->dev
->dev
;
498 u32 port_sel
= scp
->device
->channel
+ 1;
500 struct scatterlist
*sg
;
506 dev_dbg_ratelimited(dev
, "%s: (scp=%p) %d/%d/%d/%llu "
507 "cdb=(%08X-%08X-%08X-%08X)\n",
508 __func__
, scp
, host
->host_no
, scp
->device
->channel
,
509 scp
->device
->id
, scp
->device
->lun
,
510 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
511 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
512 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
513 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
516 * If a Task Management Function is active, wait for it to complete
517 * before continuing with regular commands.
519 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
520 if (cfg
->tmf_active
) {
521 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
522 rc
= SCSI_MLQUEUE_HOST_BUSY
;
525 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
527 switch (cfg
->state
) {
529 dev_dbg_ratelimited(dev
, "%s: device is in reset!\n", __func__
);
530 rc
= SCSI_MLQUEUE_HOST_BUSY
;
533 dev_dbg_ratelimited(dev
, "%s: device has failed!\n", __func__
);
534 scp
->result
= (DID_NO_CONNECT
<< 16);
542 cmd
= cmd_checkout(afu
);
543 if (unlikely(!cmd
)) {
544 dev_err(dev
, "%s: could not get a free command\n", __func__
);
545 rc
= SCSI_MLQUEUE_HOST_BUSY
;
549 kref_get(&cfg
->afu
->mapcount
);
552 cmd
->rcb
.ctx_id
= afu
->ctx_hndl
;
553 cmd
->rcb
.port_sel
= port_sel
;
554 cmd
->rcb
.lun_id
= lun_to_lunid(scp
->device
->lun
);
556 if (scp
->sc_data_direction
== DMA_TO_DEVICE
)
557 lflag
= SISL_REQ_FLAGS_HOST_WRITE
;
559 lflag
= SISL_REQ_FLAGS_HOST_READ
;
561 cmd
->rcb
.req_flags
= (SISL_REQ_FLAGS_PORT_LUN_ID
|
562 SISL_REQ_FLAGS_SUP_UNDERRUN
| lflag
);
564 /* Stash the scp in the reserved field, for reuse during interrupt */
567 nseg
= scsi_dma_map(scp
);
568 if (unlikely(nseg
< 0)) {
569 dev_err(dev
, "%s: Fail DMA map! nseg=%d\n",
571 rc
= SCSI_MLQUEUE_HOST_BUSY
;
575 ncount
= scsi_sg_count(scp
);
576 scsi_for_each_sg(scp
, sg
, ncount
, i
) {
577 cmd
->rcb
.data_len
= sg_dma_len(sg
);
578 cmd
->rcb
.data_ea
= sg_dma_address(sg
);
581 /* Copy the CDB from the scsi_cmnd passed in */
582 memcpy(cmd
->rcb
.cdb
, scp
->cmnd
, sizeof(cmd
->rcb
.cdb
));
584 /* Send the command */
585 rc
= send_cmd(afu
, cmd
);
593 kref_put(&afu
->mapcount
, afu_unmap
);
594 pr_devel("%s: returning rc=%d\n", __func__
, rc
);
599 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
600 * @cfg: Internal structure associated with the host.
602 static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg
*cfg
)
604 struct pci_dev
*pdev
= cfg
->dev
;
606 if (pci_channel_offline(pdev
))
607 wait_event_timeout(cfg
->reset_waitq
,
608 !pci_channel_offline(pdev
),
609 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT
);
613 * free_mem() - free memory associated with the AFU
614 * @cfg: Internal structure associated with the host.
616 static void free_mem(struct cxlflash_cfg
*cfg
)
620 struct afu
*afu
= cfg
->afu
;
623 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; i
++) {
624 buf
= afu
->cmd
[i
].buf
;
625 if (!((u64
)buf
& (PAGE_SIZE
- 1)))
626 free_page((ulong
)buf
);
629 free_pages((ulong
)afu
, get_order(sizeof(struct afu
)));
635 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
636 * @cfg: Internal structure associated with the host.
638 * Safe to call with AFU in a partially allocated/initialized state.
640 * Cleans up all state associated with the command queue, and unmaps
643 * - complete() will take care of commands we initiated (they'll be checked
644 * in as part of the cleanup that occurs after the completion)
646 * - cmd_checkin() will take care of entries that we did not initiate and that
647 * have not (and will not) complete because they are sitting on a [now stale]
650 static void stop_afu(struct cxlflash_cfg
*cfg
)
653 struct afu
*afu
= cfg
->afu
;
657 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; i
++) {
659 complete(&cmd
->cevent
);
660 if (!atomic_read(&cmd
->free
))
664 if (likely(afu
->afu_map
)) {
665 cxl_psa_unmap((void __iomem
*)afu
->afu_map
);
668 kref_put(&afu
->mapcount
, afu_unmap
);
673 * term_intr() - disables all AFU interrupts
674 * @cfg: Internal structure associated with the host.
675 * @level: Depth of allocation, where to begin waterfall tear down.
677 * Safe to call with AFU/MC in partially allocated/initialized state.
679 static void term_intr(struct cxlflash_cfg
*cfg
, enum undo_level level
)
681 struct afu
*afu
= cfg
->afu
;
682 struct device
*dev
= &cfg
->dev
->dev
;
684 if (!afu
|| !cfg
->mcctx
) {
685 dev_err(dev
, "%s: returning with NULL afu or MC\n", __func__
);
691 cxl_unmap_afu_irq(cfg
->mcctx
, 3, afu
);
693 cxl_unmap_afu_irq(cfg
->mcctx
, 2, afu
);
695 cxl_unmap_afu_irq(cfg
->mcctx
, 1, afu
);
697 cxl_free_afu_irqs(cfg
->mcctx
);
700 /* No action required */
706 * term_mc() - terminates the master context
707 * @cfg: Internal structure associated with the host.
708 * @level: Depth of allocation, where to begin waterfall tear down.
710 * Safe to call with AFU/MC in partially allocated/initialized state.
712 static void term_mc(struct cxlflash_cfg
*cfg
)
715 struct afu
*afu
= cfg
->afu
;
716 struct device
*dev
= &cfg
->dev
->dev
;
718 if (!afu
|| !cfg
->mcctx
) {
719 dev_err(dev
, "%s: returning with NULL afu or MC\n", __func__
);
723 rc
= cxl_stop_context(cfg
->mcctx
);
729 * term_afu() - terminates the AFU
730 * @cfg: Internal structure associated with the host.
732 * Safe to call with AFU/MC in partially allocated/initialized state.
734 static void term_afu(struct cxlflash_cfg
*cfg
)
737 * Tear down is carefully orchestrated to ensure
738 * no interrupts can come in when the problem state
741 * 1) Disable all AFU interrupts
742 * 2) Unmap the problem state area
743 * 3) Stop the master context
745 term_intr(cfg
, UNMAP_THREE
);
751 pr_debug("%s: returning\n", __func__
);
755 * notify_shutdown() - notifies device of pending shutdown
756 * @cfg: Internal structure associated with the host.
757 * @wait: Whether to wait for shutdown processing to complete.
759 * This function will notify the AFU that the adapter is being shutdown
760 * and will wait for shutdown processing to complete if wait is true.
761 * This notification should flush pending I/Os to the device and halt
762 * further I/Os until the next AFU reset is issued and device restarted.
764 static void notify_shutdown(struct cxlflash_cfg
*cfg
, bool wait
)
766 struct afu
*afu
= cfg
->afu
;
767 struct device
*dev
= &cfg
->dev
->dev
;
768 struct sisl_global_map __iomem
*global
;
769 struct dev_dependent_vals
*ddv
;
771 int i
, retry_cnt
= 0;
773 ddv
= (struct dev_dependent_vals
*)cfg
->dev_id
->driver_data
;
774 if (!(ddv
->flags
& CXLFLASH_NOTIFY_SHUTDOWN
))
777 if (!afu
|| !afu
->afu_map
) {
778 dev_dbg(dev
, "%s: The problem state area is not mapped\n",
783 global
= &afu
->afu_map
->global
;
786 for (i
= 0; i
< NUM_FC_PORTS
; i
++) {
787 reg
= readq_be(&global
->fc_regs
[i
][FC_CONFIG2
/ 8]);
788 reg
|= SISL_FC_SHUTDOWN_NORMAL
;
789 writeq_be(reg
, &global
->fc_regs
[i
][FC_CONFIG2
/ 8]);
795 /* Wait up to 1.5 seconds for shutdown processing to complete */
796 for (i
= 0; i
< NUM_FC_PORTS
; i
++) {
799 status
= readq_be(&global
->fc_regs
[i
][FC_STATUS
/ 8]);
800 if (status
& SISL_STATUS_SHUTDOWN_COMPLETE
)
802 if (++retry_cnt
>= MC_RETRY_CNT
) {
803 dev_dbg(dev
, "%s: port %d shutdown processing "
804 "not yet completed\n", __func__
, i
);
807 msleep(100 * retry_cnt
);
813 * cxlflash_remove() - PCI entry point to tear down host
814 * @pdev: PCI device associated with the host.
816 * Safe to use as a cleanup in partially allocated/initialized state.
818 static void cxlflash_remove(struct pci_dev
*pdev
)
820 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
823 if (!pci_is_enabled(pdev
)) {
824 pr_debug("%s: Device is disabled\n", __func__
);
828 /* If a Task Management Function is active, wait for it to complete
829 * before continuing with remove.
831 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
833 wait_event_interruptible_lock_irq(cfg
->tmf_waitq
,
836 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
838 /* Notify AFU and wait for shutdown processing to complete */
839 notify_shutdown(cfg
, true);
841 cfg
->state
= STATE_FAILTERM
;
842 cxlflash_stop_term_user_contexts(cfg
);
844 switch (cfg
->init_state
) {
845 case INIT_STATE_SCSI
:
846 cxlflash_term_local_luns(cfg
);
847 scsi_remove_host(cfg
->host
);
850 cancel_work_sync(&cfg
->work_q
);
853 pci_disable_device(pdev
);
854 case INIT_STATE_NONE
:
856 scsi_host_put(cfg
->host
);
860 pr_debug("%s: returning\n", __func__
);
864 * alloc_mem() - allocates the AFU and its command pool
865 * @cfg: Internal structure associated with the host.
867 * A partially allocated state remains on failure.
871 * -ENOMEM on failure to allocate memory
873 static int alloc_mem(struct cxlflash_cfg
*cfg
)
878 struct device
*dev
= &cfg
->dev
->dev
;
880 /* AFU is ~12k, i.e. only one 64k page or up to four 4k pages */
881 cfg
->afu
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
882 get_order(sizeof(struct afu
)));
883 if (unlikely(!cfg
->afu
)) {
884 dev_err(dev
, "%s: cannot get %d free pages\n",
885 __func__
, get_order(sizeof(struct afu
)));
889 cfg
->afu
->parent
= cfg
;
890 cfg
->afu
->afu_map
= NULL
;
892 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; buf
+= CMD_BUFSIZE
, i
++) {
893 if (!((u64
)buf
& (PAGE_SIZE
- 1))) {
894 buf
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
895 if (unlikely(!buf
)) {
897 "%s: Allocate command buffers fail!\n",
905 cfg
->afu
->cmd
[i
].buf
= buf
;
906 atomic_set(&cfg
->afu
->cmd
[i
].free
, 1);
907 cfg
->afu
->cmd
[i
].slot
= i
;
915 * init_pci() - initializes the host as a PCI device
916 * @cfg: Internal structure associated with the host.
918 * Return: 0 on success, -errno on failure
920 static int init_pci(struct cxlflash_cfg
*cfg
)
922 struct pci_dev
*pdev
= cfg
->dev
;
925 rc
= pci_enable_device(pdev
);
926 if (rc
|| pci_channel_offline(pdev
)) {
927 if (pci_channel_offline(pdev
)) {
928 cxlflash_wait_for_pci_err_recovery(cfg
);
929 rc
= pci_enable_device(pdev
);
933 dev_err(&pdev
->dev
, "%s: Cannot enable adapter\n",
935 cxlflash_wait_for_pci_err_recovery(cfg
);
941 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
946 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
947 * @cfg: Internal structure associated with the host.
949 * Return: 0 on success, -errno on failure
951 static int init_scsi(struct cxlflash_cfg
*cfg
)
953 struct pci_dev
*pdev
= cfg
->dev
;
956 rc
= scsi_add_host(cfg
->host
, &pdev
->dev
);
958 dev_err(&pdev
->dev
, "%s: scsi_add_host failed (rc=%d)\n",
963 scsi_scan_host(cfg
->host
);
966 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
971 * set_port_online() - transitions the specified host FC port to online state
972 * @fc_regs: Top of MMIO region defined for specified port.
974 * The provided MMIO region must be mapped prior to call. Online state means
975 * that the FC link layer has synced, completed the handshaking process, and
976 * is ready for login to start.
978 static void set_port_online(__be64 __iomem
*fc_regs
)
982 cmdcfg
= readq_be(&fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
983 cmdcfg
&= (~FC_MTIP_CMDCONFIG_OFFLINE
); /* clear OFF_LINE */
984 cmdcfg
|= (FC_MTIP_CMDCONFIG_ONLINE
); /* set ON_LINE */
985 writeq_be(cmdcfg
, &fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
989 * set_port_offline() - transitions the specified host FC port to offline state
990 * @fc_regs: Top of MMIO region defined for specified port.
992 * The provided MMIO region must be mapped prior to call.
994 static void set_port_offline(__be64 __iomem
*fc_regs
)
998 cmdcfg
= readq_be(&fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
999 cmdcfg
&= (~FC_MTIP_CMDCONFIG_ONLINE
); /* clear ON_LINE */
1000 cmdcfg
|= (FC_MTIP_CMDCONFIG_OFFLINE
); /* set OFF_LINE */
1001 writeq_be(cmdcfg
, &fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1005 * wait_port_online() - waits for the specified host FC port come online
1006 * @fc_regs: Top of MMIO region defined for specified port.
1007 * @delay_us: Number of microseconds to delay between reading port status.
1008 * @nretry: Number of cycles to retry reading port status.
1010 * The provided MMIO region must be mapped prior to call. This will timeout
1011 * when the cable is not plugged in.
1014 * TRUE (1) when the specified port is online
1015 * FALSE (0) when the specified port fails to come online after timeout
1016 * -EINVAL when @delay_us is less than 1000
1018 static int wait_port_online(__be64 __iomem
*fc_regs
, u32 delay_us
, u32 nretry
)
1022 if (delay_us
< 1000) {
1023 pr_err("%s: invalid delay specified %d\n", __func__
, delay_us
);
1028 msleep(delay_us
/ 1000);
1029 status
= readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]);
1030 if (status
== U64_MAX
)
1032 } while ((status
& FC_MTIP_STATUS_MASK
) != FC_MTIP_STATUS_ONLINE
&&
1035 return ((status
& FC_MTIP_STATUS_MASK
) == FC_MTIP_STATUS_ONLINE
);
1039 * wait_port_offline() - waits for the specified host FC port go offline
1040 * @fc_regs: Top of MMIO region defined for specified port.
1041 * @delay_us: Number of microseconds to delay between reading port status.
1042 * @nretry: Number of cycles to retry reading port status.
1044 * The provided MMIO region must be mapped prior to call.
1047 * TRUE (1) when the specified port is offline
1048 * FALSE (0) when the specified port fails to go offline after timeout
1049 * -EINVAL when @delay_us is less than 1000
1051 static int wait_port_offline(__be64 __iomem
*fc_regs
, u32 delay_us
, u32 nretry
)
1055 if (delay_us
< 1000) {
1056 pr_err("%s: invalid delay specified %d\n", __func__
, delay_us
);
1061 msleep(delay_us
/ 1000);
1062 status
= readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]);
1063 if (status
== U64_MAX
)
1065 } while ((status
& FC_MTIP_STATUS_MASK
) != FC_MTIP_STATUS_OFFLINE
&&
1068 return ((status
& FC_MTIP_STATUS_MASK
) == FC_MTIP_STATUS_OFFLINE
);
1072 * afu_set_wwpn() - configures the WWPN for the specified host FC port
1073 * @afu: AFU associated with the host that owns the specified FC port.
1074 * @port: Port number being configured.
1075 * @fc_regs: Top of MMIO region defined for specified port.
1076 * @wwpn: The world-wide-port-number previously discovered for port.
1078 * The provided MMIO region must be mapped prior to call. As part of the
1079 * sequence to configure the WWPN, the port is toggled offline and then back
1080 * online. This toggling action can cause this routine to delay up to a few
1081 * seconds. When configured to use the internal LUN feature of the AFU, a
1082 * failure to come online is overridden.
1084 static void afu_set_wwpn(struct afu
*afu
, int port
, __be64 __iomem
*fc_regs
,
1087 set_port_offline(fc_regs
);
1088 if (!wait_port_offline(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1089 FC_PORT_STATUS_RETRY_CNT
)) {
1090 pr_debug("%s: wait on port %d to go offline timed out\n",
1094 writeq_be(wwpn
, &fc_regs
[FC_PNAME
/ 8]);
1096 set_port_online(fc_regs
);
1097 if (!wait_port_online(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1098 FC_PORT_STATUS_RETRY_CNT
)) {
1099 pr_debug("%s: wait on port %d to go online timed out\n",
1105 * afu_link_reset() - resets the specified host FC port
1106 * @afu: AFU associated with the host that owns the specified FC port.
1107 * @port: Port number being configured.
1108 * @fc_regs: Top of MMIO region defined for specified port.
1110 * The provided MMIO region must be mapped prior to call. The sequence to
1111 * reset the port involves toggling it offline and then back online. This
1112 * action can cause this routine to delay up to a few seconds. An effort
1113 * is made to maintain link with the device by switching to host to use
1114 * the alternate port exclusively while the reset takes place.
1115 * failure to come online is overridden.
1117 static void afu_link_reset(struct afu
*afu
, int port
, __be64 __iomem
*fc_regs
)
1121 /* first switch the AFU to the other links, if any */
1122 port_sel
= readq_be(&afu
->afu_map
->global
.regs
.afu_port_sel
);
1123 port_sel
&= ~(1ULL << port
);
1124 writeq_be(port_sel
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1125 cxlflash_afu_sync(afu
, 0, 0, AFU_GSYNC
);
1127 set_port_offline(fc_regs
);
1128 if (!wait_port_offline(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1129 FC_PORT_STATUS_RETRY_CNT
))
1130 pr_err("%s: wait on port %d to go offline timed out\n",
1133 set_port_online(fc_regs
);
1134 if (!wait_port_online(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1135 FC_PORT_STATUS_RETRY_CNT
))
1136 pr_err("%s: wait on port %d to go online timed out\n",
1139 /* switch back to include this port */
1140 port_sel
|= (1ULL << port
);
1141 writeq_be(port_sel
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1142 cxlflash_afu_sync(afu
, 0, 0, AFU_GSYNC
);
1144 pr_debug("%s: returning port_sel=%lld\n", __func__
, port_sel
);
1148 * Asynchronous interrupt information table
1150 static const struct asyc_intr_info ainfo
[] = {
1151 {SISL_ASTATUS_FC0_OTHER
, "other error", 0, CLR_FC_ERROR
| LINK_RESET
},
1152 {SISL_ASTATUS_FC0_LOGO
, "target initiated LOGO", 0, 0},
1153 {SISL_ASTATUS_FC0_CRC_T
, "CRC threshold exceeded", 0, LINK_RESET
},
1154 {SISL_ASTATUS_FC0_LOGI_R
, "login timed out, retrying", 0, LINK_RESET
},
1155 {SISL_ASTATUS_FC0_LOGI_F
, "login failed", 0, CLR_FC_ERROR
},
1156 {SISL_ASTATUS_FC0_LOGI_S
, "login succeeded", 0, SCAN_HOST
},
1157 {SISL_ASTATUS_FC0_LINK_DN
, "link down", 0, 0},
1158 {SISL_ASTATUS_FC0_LINK_UP
, "link up", 0, 0},
1159 {SISL_ASTATUS_FC1_OTHER
, "other error", 1, CLR_FC_ERROR
| LINK_RESET
},
1160 {SISL_ASTATUS_FC1_LOGO
, "target initiated LOGO", 1, 0},
1161 {SISL_ASTATUS_FC1_CRC_T
, "CRC threshold exceeded", 1, LINK_RESET
},
1162 {SISL_ASTATUS_FC1_LOGI_R
, "login timed out, retrying", 1, LINK_RESET
},
1163 {SISL_ASTATUS_FC1_LOGI_F
, "login failed", 1, CLR_FC_ERROR
},
1164 {SISL_ASTATUS_FC1_LOGI_S
, "login succeeded", 1, SCAN_HOST
},
1165 {SISL_ASTATUS_FC1_LINK_DN
, "link down", 1, 0},
1166 {SISL_ASTATUS_FC1_LINK_UP
, "link up", 1, 0},
1167 {0x0, "", 0, 0} /* terminator */
1171 * find_ainfo() - locates and returns asynchronous interrupt information
1172 * @status: Status code set by AFU on error.
1174 * Return: The located information or NULL when the status code is invalid.
1176 static const struct asyc_intr_info
*find_ainfo(u64 status
)
1178 const struct asyc_intr_info
*info
;
1180 for (info
= &ainfo
[0]; info
->status
; info
++)
1181 if (info
->status
== status
)
1188 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1189 * @afu: AFU associated with the host.
1191 static void afu_err_intr_init(struct afu
*afu
)
1196 /* global async interrupts: AFU clears afu_ctrl on context exit
1197 * if async interrupts were sent to that context. This prevents
1198 * the AFU form sending further async interrupts when
1200 * nobody to receive them.
1204 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_mask
);
1205 /* set LISN# to send and point to master context */
1206 reg
= ((u64
) (((afu
->ctx_hndl
<< 8) | SISL_MSI_ASYNC_ERROR
)) << 40);
1208 if (afu
->internal_lun
)
1209 reg
|= 1; /* Bit 63 indicates local lun */
1210 writeq_be(reg
, &afu
->afu_map
->global
.regs
.afu_ctrl
);
1212 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_clear
);
1213 /* unmask bits that are of interest */
1214 /* note: afu can send an interrupt after this step */
1215 writeq_be(SISL_ASTATUS_MASK
, &afu
->afu_map
->global
.regs
.aintr_mask
);
1216 /* clear again in case a bit came on after previous clear but before */
1218 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_clear
);
1220 /* Clear/Set internal lun bits */
1221 reg
= readq_be(&afu
->afu_map
->global
.fc_regs
[0][FC_CONFIG2
/ 8]);
1222 reg
&= SISL_FC_INTERNAL_MASK
;
1223 if (afu
->internal_lun
)
1224 reg
|= ((u64
)(afu
->internal_lun
- 1) << SISL_FC_INTERNAL_SHIFT
);
1225 writeq_be(reg
, &afu
->afu_map
->global
.fc_regs
[0][FC_CONFIG2
/ 8]);
1227 /* now clear FC errors */
1228 for (i
= 0; i
< NUM_FC_PORTS
; i
++) {
1229 writeq_be(0xFFFFFFFFU
,
1230 &afu
->afu_map
->global
.fc_regs
[i
][FC_ERROR
/ 8]);
1231 writeq_be(0, &afu
->afu_map
->global
.fc_regs
[i
][FC_ERRCAP
/ 8]);
1234 /* sync interrupts for master's IOARRIN write */
1235 /* note that unlike asyncs, there can be no pending sync interrupts */
1236 /* at this time (this is a fresh context and master has not written */
1237 /* IOARRIN yet), so there is nothing to clear. */
1239 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1240 writeq_be(SISL_MSI_SYNC_ERROR
, &afu
->host_map
->ctx_ctrl
);
1241 writeq_be(SISL_ISTATUS_MASK
, &afu
->host_map
->intr_mask
);
1245 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1246 * @irq: Interrupt number.
1247 * @data: Private data provided at interrupt registration, the AFU.
1249 * Return: Always return IRQ_HANDLED.
1251 static irqreturn_t
cxlflash_sync_err_irq(int irq
, void *data
)
1253 struct afu
*afu
= (struct afu
*)data
;
1257 reg
= readq_be(&afu
->host_map
->intr_status
);
1258 reg_unmasked
= (reg
& SISL_ISTATUS_UNMASK
);
1260 if (reg_unmasked
== 0UL) {
1261 pr_err("%s: %llX: spurious interrupt, intr_status %016llX\n",
1262 __func__
, (u64
)afu
, reg
);
1263 goto cxlflash_sync_err_irq_exit
;
1266 pr_err("%s: %llX: unexpected interrupt, intr_status %016llX\n",
1267 __func__
, (u64
)afu
, reg
);
1269 writeq_be(reg_unmasked
, &afu
->host_map
->intr_clear
);
1271 cxlflash_sync_err_irq_exit
:
1272 pr_debug("%s: returning rc=%d\n", __func__
, IRQ_HANDLED
);
1277 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1278 * @irq: Interrupt number.
1279 * @data: Private data provided at interrupt registration, the AFU.
1281 * Return: Always return IRQ_HANDLED.
1283 static irqreturn_t
cxlflash_rrq_irq(int irq
, void *data
)
1285 struct afu
*afu
= (struct afu
*)data
;
1286 struct afu_cmd
*cmd
;
1287 bool toggle
= afu
->toggle
;
1289 *hrrq_start
= afu
->hrrq_start
,
1290 *hrrq_end
= afu
->hrrq_end
,
1291 *hrrq_curr
= afu
->hrrq_curr
;
1293 /* Process however many RRQ entries that are ready */
1297 if ((entry
& SISL_RESP_HANDLE_T_BIT
) != toggle
)
1300 cmd
= (struct afu_cmd
*)(entry
& ~SISL_RESP_HANDLE_T_BIT
);
1303 /* Advance to next entry or wrap and flip the toggle bit */
1304 if (hrrq_curr
< hrrq_end
)
1307 hrrq_curr
= hrrq_start
;
1308 toggle
^= SISL_RESP_HANDLE_T_BIT
;
1312 afu
->hrrq_curr
= hrrq_curr
;
1313 afu
->toggle
= toggle
;
1319 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1320 * @irq: Interrupt number.
1321 * @data: Private data provided at interrupt registration, the AFU.
1323 * Return: Always return IRQ_HANDLED.
1325 static irqreturn_t
cxlflash_async_err_irq(int irq
, void *data
)
1327 struct afu
*afu
= (struct afu
*)data
;
1328 struct cxlflash_cfg
*cfg
= afu
->parent
;
1329 struct device
*dev
= &cfg
->dev
->dev
;
1331 const struct asyc_intr_info
*info
;
1332 struct sisl_global_map __iomem
*global
= &afu
->afu_map
->global
;
1337 reg
= readq_be(&global
->regs
.aintr_status
);
1338 reg_unmasked
= (reg
& SISL_ASTATUS_UNMASK
);
1340 if (reg_unmasked
== 0) {
1341 dev_err(dev
, "%s: spurious interrupt, aintr_status 0x%016llX\n",
1346 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
1347 writeq_be(reg_unmasked
, &global
->regs
.aintr_clear
);
1349 /* Check each bit that is on */
1350 for (i
= 0; reg_unmasked
; i
++, reg_unmasked
= (reg_unmasked
>> 1)) {
1351 info
= find_ainfo(1ULL << i
);
1352 if (((reg_unmasked
& 0x1) == 0) || !info
)
1357 dev_err(dev
, "%s: FC Port %d -> %s, fc_status 0x%08llX\n",
1358 __func__
, port
, info
->desc
,
1359 readq_be(&global
->fc_regs
[port
][FC_STATUS
/ 8]));
1362 * Do link reset first, some OTHER errors will set FC_ERROR
1363 * again if cleared before or w/o a reset
1365 if (info
->action
& LINK_RESET
) {
1366 dev_err(dev
, "%s: FC Port %d: resetting link\n",
1368 cfg
->lr_state
= LINK_RESET_REQUIRED
;
1369 cfg
->lr_port
= port
;
1370 kref_get(&cfg
->afu
->mapcount
);
1371 schedule_work(&cfg
->work_q
);
1374 if (info
->action
& CLR_FC_ERROR
) {
1375 reg
= readq_be(&global
->fc_regs
[port
][FC_ERROR
/ 8]);
1378 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
1379 * should be the same and tracing one is sufficient.
1382 dev_err(dev
, "%s: fc %d: clearing fc_error 0x%08llX\n",
1383 __func__
, port
, reg
);
1385 writeq_be(reg
, &global
->fc_regs
[port
][FC_ERROR
/ 8]);
1386 writeq_be(0, &global
->fc_regs
[port
][FC_ERRCAP
/ 8]);
1389 if (info
->action
& SCAN_HOST
) {
1390 atomic_inc(&cfg
->scan_host_needed
);
1391 kref_get(&cfg
->afu
->mapcount
);
1392 schedule_work(&cfg
->work_q
);
1397 dev_dbg(dev
, "%s: returning IRQ_HANDLED, afu=%p\n", __func__
, afu
);
1402 * start_context() - starts the master context
1403 * @cfg: Internal structure associated with the host.
1405 * Return: A success or failure value from CXL services.
1407 static int start_context(struct cxlflash_cfg
*cfg
)
1411 rc
= cxl_start_context(cfg
->mcctx
,
1412 cfg
->afu
->work
.work_element_descriptor
,
1415 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1420 * read_vpd() - obtains the WWPNs from VPD
1421 * @cfg: Internal structure associated with the host.
1422 * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
1424 * Return: 0 on success, -errno on failure
1426 static int read_vpd(struct cxlflash_cfg
*cfg
, u64 wwpn
[])
1428 struct pci_dev
*dev
= cfg
->dev
;
1430 int ro_start
, ro_size
, i
, j
, k
;
1432 char vpd_data
[CXLFLASH_VPD_LEN
];
1433 char tmp_buf
[WWPN_BUF_LEN
] = { 0 };
1434 char *wwpn_vpd_tags
[NUM_FC_PORTS
] = { "V5", "V6" };
1436 /* Get the VPD data from the device */
1437 vpd_size
= cxl_read_adapter_vpd(dev
, vpd_data
, sizeof(vpd_data
));
1438 if (unlikely(vpd_size
<= 0)) {
1439 dev_err(&dev
->dev
, "%s: Unable to read VPD (size = %ld)\n",
1440 __func__
, vpd_size
);
1445 /* Get the read only section offset */
1446 ro_start
= pci_vpd_find_tag(vpd_data
, 0, vpd_size
,
1447 PCI_VPD_LRDT_RO_DATA
);
1448 if (unlikely(ro_start
< 0)) {
1449 dev_err(&dev
->dev
, "%s: VPD Read-only data not found\n",
1455 /* Get the read only section size, cap when extends beyond read VPD */
1456 ro_size
= pci_vpd_lrdt_size(&vpd_data
[ro_start
]);
1458 i
= ro_start
+ PCI_VPD_LRDT_TAG_SIZE
;
1459 if (unlikely((i
+ j
) > vpd_size
)) {
1460 pr_debug("%s: Might need to read more VPD (%d > %ld)\n",
1461 __func__
, (i
+ j
), vpd_size
);
1462 ro_size
= vpd_size
- i
;
1466 * Find the offset of the WWPN tag within the read only
1467 * VPD data and validate the found field (partials are
1468 * no good to us). Convert the ASCII data to an integer
1469 * value. Note that we must copy to a temporary buffer
1470 * because the conversion service requires that the ASCII
1471 * string be terminated.
1473 for (k
= 0; k
< NUM_FC_PORTS
; k
++) {
1475 i
= ro_start
+ PCI_VPD_LRDT_TAG_SIZE
;
1477 i
= pci_vpd_find_info_keyword(vpd_data
, i
, j
, wwpn_vpd_tags
[k
]);
1478 if (unlikely(i
< 0)) {
1479 dev_err(&dev
->dev
, "%s: Port %d WWPN not found "
1480 "in VPD\n", __func__
, k
);
1485 j
= pci_vpd_info_field_size(&vpd_data
[i
]);
1486 i
+= PCI_VPD_INFO_FLD_HDR_SIZE
;
1487 if (unlikely((i
+ j
> vpd_size
) || (j
!= WWPN_LEN
))) {
1488 dev_err(&dev
->dev
, "%s: Port %d WWPN incomplete or "
1495 memcpy(tmp_buf
, &vpd_data
[i
], WWPN_LEN
);
1496 rc
= kstrtoul(tmp_buf
, WWPN_LEN
, (ulong
*)&wwpn
[k
]);
1498 dev_err(&dev
->dev
, "%s: Fail to convert port %d WWPN "
1499 "to integer\n", __func__
, k
);
1506 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1511 * init_pcr() - initialize the provisioning and control registers
1512 * @cfg: Internal structure associated with the host.
1514 * Also sets up fast access to the mapped registers and initializes AFU
1515 * command fields that never change.
1517 static void init_pcr(struct cxlflash_cfg
*cfg
)
1519 struct afu
*afu
= cfg
->afu
;
1520 struct sisl_ctrl_map __iomem
*ctrl_map
;
1523 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
1524 ctrl_map
= &afu
->afu_map
->ctrls
[i
].ctrl
;
1525 /* Disrupt any clients that could be running */
1526 /* e.g. clients that survived a master restart */
1527 writeq_be(0, &ctrl_map
->rht_start
);
1528 writeq_be(0, &ctrl_map
->rht_cnt_id
);
1529 writeq_be(0, &ctrl_map
->ctx_cap
);
1532 /* Copy frequently used fields into afu */
1533 afu
->ctx_hndl
= (u16
) cxl_process_element(cfg
->mcctx
);
1534 afu
->host_map
= &afu
->afu_map
->hosts
[afu
->ctx_hndl
].host
;
1535 afu
->ctrl_map
= &afu
->afu_map
->ctrls
[afu
->ctx_hndl
].ctrl
;
1537 /* Program the Endian Control for the master context */
1538 writeq_be(SISL_ENDIAN_CTRL
, &afu
->host_map
->endian_ctrl
);
1540 /* Initialize cmd fields that never change */
1541 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; i
++) {
1542 afu
->cmd
[i
].rcb
.ctx_id
= afu
->ctx_hndl
;
1543 afu
->cmd
[i
].rcb
.msi
= SISL_MSI_RRQ_UPDATED
;
1544 afu
->cmd
[i
].rcb
.rrq
= 0x0;
1549 * init_global() - initialize AFU global registers
1550 * @cfg: Internal structure associated with the host.
1552 static int init_global(struct cxlflash_cfg
*cfg
)
1554 struct afu
*afu
= cfg
->afu
;
1555 struct device
*dev
= &cfg
->dev
->dev
;
1556 u64 wwpn
[NUM_FC_PORTS
]; /* wwpn of AFU ports */
1557 int i
= 0, num_ports
= 0;
1561 rc
= read_vpd(cfg
, &wwpn
[0]);
1563 dev_err(dev
, "%s: could not read vpd rc=%d\n", __func__
, rc
);
1567 pr_debug("%s: wwpn0=0x%llX wwpn1=0x%llX\n", __func__
, wwpn
[0], wwpn
[1]);
1569 /* Set up RRQ in AFU for master issued cmds */
1570 writeq_be((u64
) afu
->hrrq_start
, &afu
->host_map
->rrq_start
);
1571 writeq_be((u64
) afu
->hrrq_end
, &afu
->host_map
->rrq_end
);
1573 /* AFU configuration */
1574 reg
= readq_be(&afu
->afu_map
->global
.regs
.afu_config
);
1575 reg
|= SISL_AFUCONF_AR_ALL
|SISL_AFUCONF_ENDIAN
;
1576 /* enable all auto retry options and control endianness */
1577 /* leave others at default: */
1578 /* CTX_CAP write protected, mbox_r does not clear on read and */
1579 /* checker on if dual afu */
1580 writeq_be(reg
, &afu
->afu_map
->global
.regs
.afu_config
);
1582 /* Global port select: select either port */
1583 if (afu
->internal_lun
) {
1584 /* Only use port 0 */
1585 writeq_be(PORT0
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1586 num_ports
= NUM_FC_PORTS
- 1;
1588 writeq_be(BOTH_PORTS
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1589 num_ports
= NUM_FC_PORTS
;
1592 for (i
= 0; i
< num_ports
; i
++) {
1593 /* Unmask all errors (but they are still masked at AFU) */
1594 writeq_be(0, &afu
->afu_map
->global
.fc_regs
[i
][FC_ERRMSK
/ 8]);
1595 /* Clear CRC error cnt & set a threshold */
1596 (void)readq_be(&afu
->afu_map
->global
.
1597 fc_regs
[i
][FC_CNT_CRCERR
/ 8]);
1598 writeq_be(MC_CRC_THRESH
, &afu
->afu_map
->global
.fc_regs
[i
]
1599 [FC_CRC_THRESH
/ 8]);
1601 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
1603 afu_set_wwpn(afu
, i
,
1604 &afu
->afu_map
->global
.fc_regs
[i
][0],
1606 /* Programming WWPN back to back causes additional
1607 * offline/online transitions and a PLOGI
1612 /* Set up master's own CTX_CAP to allow real mode, host translation */
1613 /* tables, afu cmds and read/write GSCSI cmds. */
1614 /* First, unlock ctx_cap write by reading mbox */
1615 (void)readq_be(&afu
->ctrl_map
->mbox_r
); /* unlock ctx_cap */
1616 writeq_be((SISL_CTX_CAP_REAL_MODE
| SISL_CTX_CAP_HOST_XLATE
|
1617 SISL_CTX_CAP_READ_CMD
| SISL_CTX_CAP_WRITE_CMD
|
1618 SISL_CTX_CAP_AFU_CMD
| SISL_CTX_CAP_GSCSI_CMD
),
1619 &afu
->ctrl_map
->ctx_cap
);
1620 /* Initialize heartbeat */
1621 afu
->hb
= readq_be(&afu
->afu_map
->global
.regs
.afu_hb
);
1628 * start_afu() - initializes and starts the AFU
1629 * @cfg: Internal structure associated with the host.
1631 static int start_afu(struct cxlflash_cfg
*cfg
)
1633 struct afu
*afu
= cfg
->afu
;
1634 struct afu_cmd
*cmd
;
1639 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; i
++) {
1642 init_completion(&cmd
->cevent
);
1643 spin_lock_init(&cmd
->slock
);
1649 /* After an AFU reset, RRQ entries are stale, clear them */
1650 memset(&afu
->rrq_entry
, 0, sizeof(afu
->rrq_entry
));
1652 /* Initialize RRQ pointers */
1653 afu
->hrrq_start
= &afu
->rrq_entry
[0];
1654 afu
->hrrq_end
= &afu
->rrq_entry
[NUM_RRQ_ENTRY
- 1];
1655 afu
->hrrq_curr
= afu
->hrrq_start
;
1658 rc
= init_global(cfg
);
1660 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1665 * init_intr() - setup interrupt handlers for the master context
1666 * @cfg: Internal structure associated with the host.
1668 * Return: 0 on success, -errno on failure
1670 static enum undo_level
init_intr(struct cxlflash_cfg
*cfg
,
1671 struct cxl_context
*ctx
)
1673 struct afu
*afu
= cfg
->afu
;
1674 struct device
*dev
= &cfg
->dev
->dev
;
1676 enum undo_level level
= UNDO_NOOP
;
1678 rc
= cxl_allocate_afu_irqs(ctx
, 3);
1680 dev_err(dev
, "%s: call to allocate_afu_irqs failed rc=%d!\n",
1686 rc
= cxl_map_afu_irq(ctx
, 1, cxlflash_sync_err_irq
, afu
,
1687 "SISL_MSI_SYNC_ERROR");
1688 if (unlikely(rc
<= 0)) {
1689 dev_err(dev
, "%s: IRQ 1 (SISL_MSI_SYNC_ERROR) map failed!\n",
1695 rc
= cxl_map_afu_irq(ctx
, 2, cxlflash_rrq_irq
, afu
,
1696 "SISL_MSI_RRQ_UPDATED");
1697 if (unlikely(rc
<= 0)) {
1698 dev_err(dev
, "%s: IRQ 2 (SISL_MSI_RRQ_UPDATED) map failed!\n",
1704 rc
= cxl_map_afu_irq(ctx
, 3, cxlflash_async_err_irq
, afu
,
1705 "SISL_MSI_ASYNC_ERROR");
1706 if (unlikely(rc
<= 0)) {
1707 dev_err(dev
, "%s: IRQ 3 (SISL_MSI_ASYNC_ERROR) map failed!\n",
1717 * init_mc() - create and register as the master context
1718 * @cfg: Internal structure associated with the host.
1720 * Return: 0 on success, -errno on failure
1722 static int init_mc(struct cxlflash_cfg
*cfg
)
1724 struct cxl_context
*ctx
;
1725 struct device
*dev
= &cfg
->dev
->dev
;
1727 enum undo_level level
;
1729 ctx
= cxl_get_context(cfg
->dev
);
1730 if (unlikely(!ctx
)) {
1736 /* Set it up as a master with the CXL */
1737 cxl_set_master(ctx
);
1739 /* During initialization reset the AFU to start from a clean slate */
1740 rc
= cxl_afu_reset(cfg
->mcctx
);
1742 dev_err(dev
, "%s: initial AFU reset failed rc=%d\n",
1747 level
= init_intr(cfg
, ctx
);
1748 if (unlikely(level
)) {
1749 dev_err(dev
, "%s: setting up interrupts failed rc=%d\n",
1754 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1755 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1756 * element (pe) that is embedded in the context (ctx)
1758 rc
= start_context(cfg
);
1760 dev_err(dev
, "%s: start context failed rc=%d\n", __func__
, rc
);
1761 level
= UNMAP_THREE
;
1765 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1768 term_intr(cfg
, level
);
1773 * init_afu() - setup as master context and start AFU
1774 * @cfg: Internal structure associated with the host.
1776 * This routine is a higher level of control for configuring the
1777 * AFU on probe and reset paths.
1779 * Return: 0 on success, -errno on failure
1781 static int init_afu(struct cxlflash_cfg
*cfg
)
1785 struct afu
*afu
= cfg
->afu
;
1786 struct device
*dev
= &cfg
->dev
->dev
;
1788 cxl_perst_reloads_same_image(cfg
->cxl_afu
, true);
1792 dev_err(dev
, "%s: call to init_mc failed, rc=%d!\n",
1797 /* Map the entire MMIO space of the AFU */
1798 afu
->afu_map
= cxl_psa_map(cfg
->mcctx
);
1799 if (!afu
->afu_map
) {
1800 dev_err(dev
, "%s: call to cxl_psa_map failed!\n", __func__
);
1804 kref_init(&afu
->mapcount
);
1806 /* No byte reverse on reading afu_version or string will be backwards */
1807 reg
= readq(&afu
->afu_map
->global
.regs
.afu_version
);
1808 memcpy(afu
->version
, ®
, sizeof(reg
));
1809 afu
->interface_version
=
1810 readq_be(&afu
->afu_map
->global
.regs
.interface_version
);
1811 if ((afu
->interface_version
+ 1) == 0) {
1812 pr_err("Back level AFU, please upgrade. AFU version %s "
1813 "interface version 0x%llx\n", afu
->version
,
1814 afu
->interface_version
);
1819 pr_debug("%s: afu version %s, interface version 0x%llX\n", __func__
,
1820 afu
->version
, afu
->interface_version
);
1822 rc
= start_afu(cfg
);
1824 dev_err(dev
, "%s: call to start_afu failed, rc=%d!\n",
1829 afu_err_intr_init(cfg
->afu
);
1830 atomic64_set(&afu
->room
, readq_be(&afu
->host_map
->cmd_room
));
1832 /* Restore the LUN mappings */
1833 cxlflash_restore_luntable(cfg
);
1835 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1839 kref_put(&afu
->mapcount
, afu_unmap
);
1841 term_intr(cfg
, UNMAP_THREE
);
1847 * cxlflash_afu_sync() - builds and sends an AFU sync command
1848 * @afu: AFU associated with the host.
1849 * @ctx_hndl_u: Identifies context requesting sync.
1850 * @res_hndl_u: Identifies resource requesting sync.
1851 * @mode: Type of sync to issue (lightweight, heavyweight, global).
1853 * The AFU can only take 1 sync command at a time. This routine enforces this
1854 * limitation by using a mutex to provide exclusive access to the AFU during
1855 * the sync. This design point requires calling threads to not be on interrupt
1856 * context due to the possibility of sleeping during concurrent sync operations.
1858 * AFU sync operations are only necessary and allowed when the device is
1859 * operating normally. When not operating normally, sync requests can occur as
1860 * part of cleaning up resources associated with an adapter prior to removal.
1861 * In this scenario, these requests are simply ignored (safe due to the AFU
1868 int cxlflash_afu_sync(struct afu
*afu
, ctx_hndl_t ctx_hndl_u
,
1869 res_hndl_t res_hndl_u
, u8 mode
)
1871 struct cxlflash_cfg
*cfg
= afu
->parent
;
1872 struct device
*dev
= &cfg
->dev
->dev
;
1873 struct afu_cmd
*cmd
= NULL
;
1876 static DEFINE_MUTEX(sync_active
);
1878 if (cfg
->state
!= STATE_NORMAL
) {
1879 pr_debug("%s: Sync not required! (%u)\n", __func__
, cfg
->state
);
1883 mutex_lock(&sync_active
);
1885 cmd
= cmd_checkout(afu
);
1886 if (unlikely(!cmd
)) {
1888 udelay(1000 * retry_cnt
);
1889 if (retry_cnt
< MC_RETRY_CNT
)
1891 dev_err(dev
, "%s: could not get a free command\n", __func__
);
1896 pr_debug("%s: afu=%p cmd=%p %d\n", __func__
, afu
, cmd
, ctx_hndl_u
);
1898 memset(cmd
->rcb
.cdb
, 0, sizeof(cmd
->rcb
.cdb
));
1900 cmd
->rcb
.req_flags
= SISL_REQ_FLAGS_AFU_CMD
;
1901 cmd
->rcb
.port_sel
= 0x0; /* NA */
1902 cmd
->rcb
.lun_id
= 0x0; /* NA */
1903 cmd
->rcb
.data_len
= 0x0;
1904 cmd
->rcb
.data_ea
= 0x0;
1905 cmd
->rcb
.timeout
= MC_AFU_SYNC_TIMEOUT
;
1907 cmd
->rcb
.cdb
[0] = 0xC0; /* AFU Sync */
1908 cmd
->rcb
.cdb
[1] = mode
;
1910 /* The cdb is aligned, no unaligned accessors required */
1911 *((__be16
*)&cmd
->rcb
.cdb
[2]) = cpu_to_be16(ctx_hndl_u
);
1912 *((__be32
*)&cmd
->rcb
.cdb
[4]) = cpu_to_be32(res_hndl_u
);
1914 rc
= send_cmd(afu
, cmd
);
1918 wait_resp(afu
, cmd
);
1920 /* Set on timeout */
1921 if (unlikely((cmd
->sa
.ioasc
!= 0) ||
1922 (cmd
->sa
.host_use_b
[0] & B_ERROR
)))
1925 mutex_unlock(&sync_active
);
1928 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1933 * afu_reset() - resets the AFU
1934 * @cfg: Internal structure associated with the host.
1936 * Return: 0 on success, -errno on failure
1938 static int afu_reset(struct cxlflash_cfg
*cfg
)
1941 /* Stop the context before the reset. Since the context is
1942 * no longer available restart it after the reset is complete
1949 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1954 * drain_ioctls() - wait until all currently executing ioctls have completed
1955 * @cfg: Internal structure associated with the host.
1957 * Obtain write access to read/write semaphore that wraps ioctl
1958 * handling to 'drain' ioctls currently executing.
1960 static void drain_ioctls(struct cxlflash_cfg
*cfg
)
1962 down_write(&cfg
->ioctl_rwsem
);
1963 up_write(&cfg
->ioctl_rwsem
);
1967 * cxlflash_eh_device_reset_handler() - reset a single LUN
1968 * @scp: SCSI command to send.
1971 * SUCCESS as defined in scsi/scsi.h
1972 * FAILED as defined in scsi/scsi.h
1974 static int cxlflash_eh_device_reset_handler(struct scsi_cmnd
*scp
)
1977 struct Scsi_Host
*host
= scp
->device
->host
;
1978 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
1979 struct afu
*afu
= cfg
->afu
;
1982 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
1983 "cdb=(%08X-%08X-%08X-%08X)\n", __func__
, scp
,
1984 host
->host_no
, scp
->device
->channel
,
1985 scp
->device
->id
, scp
->device
->lun
,
1986 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
1987 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
1988 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
1989 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
1992 switch (cfg
->state
) {
1994 rcr
= send_tmf(afu
, scp
, TMF_LUN_RESET
);
1999 wait_event(cfg
->reset_waitq
, cfg
->state
!= STATE_RESET
);
2006 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
2011 * cxlflash_eh_host_reset_handler() - reset the host adapter
2012 * @scp: SCSI command from stack identifying host.
2014 * Following a reset, the state is evaluated again in case an EEH occurred
2015 * during the reset. In such a scenario, the host reset will either yield
2016 * until the EEH recovery is complete or return success or failure based
2017 * upon the current device state.
2020 * SUCCESS as defined in scsi/scsi.h
2021 * FAILED as defined in scsi/scsi.h
2023 static int cxlflash_eh_host_reset_handler(struct scsi_cmnd
*scp
)
2027 struct Scsi_Host
*host
= scp
->device
->host
;
2028 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
2030 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
2031 "cdb=(%08X-%08X-%08X-%08X)\n", __func__
, scp
,
2032 host
->host_no
, scp
->device
->channel
,
2033 scp
->device
->id
, scp
->device
->lun
,
2034 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
2035 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
2036 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
2037 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
2039 switch (cfg
->state
) {
2041 cfg
->state
= STATE_RESET
;
2043 cxlflash_mark_contexts_error(cfg
);
2044 rcr
= afu_reset(cfg
);
2047 cfg
->state
= STATE_FAILTERM
;
2049 cfg
->state
= STATE_NORMAL
;
2050 wake_up_all(&cfg
->reset_waitq
);
2054 wait_event(cfg
->reset_waitq
, cfg
->state
!= STATE_RESET
);
2055 if (cfg
->state
== STATE_NORMAL
)
2063 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
2068 * cxlflash_change_queue_depth() - change the queue depth for the device
2069 * @sdev: SCSI device destined for queue depth change.
2070 * @qdepth: Requested queue depth value to set.
2072 * The requested queue depth is capped to the maximum supported value.
2074 * Return: The actual queue depth set.
2076 static int cxlflash_change_queue_depth(struct scsi_device
*sdev
, int qdepth
)
2079 if (qdepth
> CXLFLASH_MAX_CMDS_PER_LUN
)
2080 qdepth
= CXLFLASH_MAX_CMDS_PER_LUN
;
2082 scsi_change_queue_depth(sdev
, qdepth
);
2083 return sdev
->queue_depth
;
2087 * cxlflash_show_port_status() - queries and presents the current port status
2088 * @port: Desired port for status reporting.
2089 * @afu: AFU owning the specified port.
2090 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2092 * Return: The size of the ASCII string returned in @buf.
2094 static ssize_t
cxlflash_show_port_status(u32 port
, struct afu
*afu
, char *buf
)
2098 __be64 __iomem
*fc_regs
;
2100 if (port
>= NUM_FC_PORTS
)
2103 fc_regs
= &afu
->afu_map
->global
.fc_regs
[port
][0];
2104 status
= readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]);
2105 status
&= FC_MTIP_STATUS_MASK
;
2107 if (status
== FC_MTIP_STATUS_ONLINE
)
2108 disp_status
= "online";
2109 else if (status
== FC_MTIP_STATUS_OFFLINE
)
2110 disp_status
= "offline";
2112 disp_status
= "unknown";
2114 return scnprintf(buf
, PAGE_SIZE
, "%s\n", disp_status
);
2118 * port0_show() - queries and presents the current status of port 0
2119 * @dev: Generic device associated with the host owning the port.
2120 * @attr: Device attribute representing the port.
2121 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2123 * Return: The size of the ASCII string returned in @buf.
2125 static ssize_t
port0_show(struct device
*dev
,
2126 struct device_attribute
*attr
,
2129 struct Scsi_Host
*shost
= class_to_shost(dev
);
2130 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)shost
->hostdata
;
2131 struct afu
*afu
= cfg
->afu
;
2133 return cxlflash_show_port_status(0, afu
, buf
);
2137 * port1_show() - queries and presents the current status of port 1
2138 * @dev: Generic device associated with the host owning the port.
2139 * @attr: Device attribute representing the port.
2140 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2142 * Return: The size of the ASCII string returned in @buf.
2144 static ssize_t
port1_show(struct device
*dev
,
2145 struct device_attribute
*attr
,
2148 struct Scsi_Host
*shost
= class_to_shost(dev
);
2149 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)shost
->hostdata
;
2150 struct afu
*afu
= cfg
->afu
;
2152 return cxlflash_show_port_status(1, afu
, buf
);
2156 * lun_mode_show() - presents the current LUN mode of the host
2157 * @dev: Generic device associated with the host.
2158 * @attr: Device attribute representing the LUN mode.
2159 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2161 * Return: The size of the ASCII string returned in @buf.
2163 static ssize_t
lun_mode_show(struct device
*dev
,
2164 struct device_attribute
*attr
, char *buf
)
2166 struct Scsi_Host
*shost
= class_to_shost(dev
);
2167 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)shost
->hostdata
;
2168 struct afu
*afu
= cfg
->afu
;
2170 return scnprintf(buf
, PAGE_SIZE
, "%u\n", afu
->internal_lun
);
2174 * lun_mode_store() - sets the LUN mode of the host
2175 * @dev: Generic device associated with the host.
2176 * @attr: Device attribute representing the LUN mode.
2177 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2178 * @count: Length of data resizing in @buf.
2180 * The CXL Flash AFU supports a dummy LUN mode where the external
2181 * links and storage are not required. Space on the FPGA is used
2182 * to create 1 or 2 small LUNs which are presented to the system
2183 * as if they were a normal storage device. This feature is useful
2184 * during development and also provides manufacturing with a way
2185 * to test the AFU without an actual device.
2187 * 0 = external LUN[s] (default)
2188 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2189 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2190 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2191 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2193 * Return: The size of the ASCII string returned in @buf.
2195 static ssize_t
lun_mode_store(struct device
*dev
,
2196 struct device_attribute
*attr
,
2197 const char *buf
, size_t count
)
2199 struct Scsi_Host
*shost
= class_to_shost(dev
);
2200 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)shost
->hostdata
;
2201 struct afu
*afu
= cfg
->afu
;
2205 rc
= kstrtouint(buf
, 10, &lun_mode
);
2206 if (!rc
&& (lun_mode
< 5) && (lun_mode
!= afu
->internal_lun
)) {
2207 afu
->internal_lun
= lun_mode
;
2210 * When configured for internal LUN, there is only one channel,
2211 * channel number 0, else there will be 2 (default).
2213 if (afu
->internal_lun
)
2214 shost
->max_channel
= 0;
2216 shost
->max_channel
= NUM_FC_PORTS
- 1;
2219 scsi_scan_host(cfg
->host
);
2226 * ioctl_version_show() - presents the current ioctl version of the host
2227 * @dev: Generic device associated with the host.
2228 * @attr: Device attribute representing the ioctl version.
2229 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2231 * Return: The size of the ASCII string returned in @buf.
2233 static ssize_t
ioctl_version_show(struct device
*dev
,
2234 struct device_attribute
*attr
, char *buf
)
2236 return scnprintf(buf
, PAGE_SIZE
, "%u\n", DK_CXLFLASH_VERSION_0
);
2240 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2241 * @port: Desired port for status reporting.
2242 * @afu: AFU owning the specified port.
2243 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2245 * Return: The size of the ASCII string returned in @buf.
2247 static ssize_t
cxlflash_show_port_lun_table(u32 port
,
2253 __be64 __iomem
*fc_port
;
2255 if (port
>= NUM_FC_PORTS
)
2258 fc_port
= &afu
->afu_map
->global
.fc_port
[port
][0];
2260 for (i
= 0; i
< CXLFLASH_NUM_VLUNS
; i
++)
2261 bytes
+= scnprintf(buf
+ bytes
, PAGE_SIZE
- bytes
,
2262 "%03d: %016llX\n", i
, readq_be(&fc_port
[i
]));
2267 * port0_lun_table_show() - presents the current LUN table of port 0
2268 * @dev: Generic device associated with the host owning the port.
2269 * @attr: Device attribute representing the port.
2270 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2272 * Return: The size of the ASCII string returned in @buf.
2274 static ssize_t
port0_lun_table_show(struct device
*dev
,
2275 struct device_attribute
*attr
,
2278 struct Scsi_Host
*shost
= class_to_shost(dev
);
2279 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)shost
->hostdata
;
2280 struct afu
*afu
= cfg
->afu
;
2282 return cxlflash_show_port_lun_table(0, afu
, buf
);
2286 * port1_lun_table_show() - presents the current LUN table of port 1
2287 * @dev: Generic device associated with the host owning the port.
2288 * @attr: Device attribute representing the port.
2289 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2291 * Return: The size of the ASCII string returned in @buf.
2293 static ssize_t
port1_lun_table_show(struct device
*dev
,
2294 struct device_attribute
*attr
,
2297 struct Scsi_Host
*shost
= class_to_shost(dev
);
2298 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)shost
->hostdata
;
2299 struct afu
*afu
= cfg
->afu
;
2301 return cxlflash_show_port_lun_table(1, afu
, buf
);
2305 * mode_show() - presents the current mode of the device
2306 * @dev: Generic device associated with the device.
2307 * @attr: Device attribute representing the device mode.
2308 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2310 * Return: The size of the ASCII string returned in @buf.
2312 static ssize_t
mode_show(struct device
*dev
,
2313 struct device_attribute
*attr
, char *buf
)
2315 struct scsi_device
*sdev
= to_scsi_device(dev
);
2317 return scnprintf(buf
, PAGE_SIZE
, "%s\n",
2318 sdev
->hostdata
? "superpipe" : "legacy");
2324 static DEVICE_ATTR_RO(port0
);
2325 static DEVICE_ATTR_RO(port1
);
2326 static DEVICE_ATTR_RW(lun_mode
);
2327 static DEVICE_ATTR_RO(ioctl_version
);
2328 static DEVICE_ATTR_RO(port0_lun_table
);
2329 static DEVICE_ATTR_RO(port1_lun_table
);
2331 static struct device_attribute
*cxlflash_host_attrs
[] = {
2335 &dev_attr_ioctl_version
,
2336 &dev_attr_port0_lun_table
,
2337 &dev_attr_port1_lun_table
,
2344 static DEVICE_ATTR_RO(mode
);
2346 static struct device_attribute
*cxlflash_dev_attrs
[] = {
2354 static struct scsi_host_template driver_template
= {
2355 .module
= THIS_MODULE
,
2356 .name
= CXLFLASH_ADAPTER_NAME
,
2357 .info
= cxlflash_driver_info
,
2358 .ioctl
= cxlflash_ioctl
,
2359 .proc_name
= CXLFLASH_NAME
,
2360 .queuecommand
= cxlflash_queuecommand
,
2361 .eh_device_reset_handler
= cxlflash_eh_device_reset_handler
,
2362 .eh_host_reset_handler
= cxlflash_eh_host_reset_handler
,
2363 .change_queue_depth
= cxlflash_change_queue_depth
,
2364 .cmd_per_lun
= CXLFLASH_MAX_CMDS_PER_LUN
,
2365 .can_queue
= CXLFLASH_MAX_CMDS
,
2367 .sg_tablesize
= 1, /* No scatter gather support */
2368 .max_sectors
= CXLFLASH_MAX_SECTORS
,
2369 .use_clustering
= ENABLE_CLUSTERING
,
2370 .shost_attrs
= cxlflash_host_attrs
,
2371 .sdev_attrs
= cxlflash_dev_attrs
,
2375 * Device dependent values
2377 static struct dev_dependent_vals dev_corsa_vals
= { CXLFLASH_MAX_SECTORS
,
2379 static struct dev_dependent_vals dev_flash_gt_vals
= { CXLFLASH_MAX_SECTORS
,
2380 CXLFLASH_NOTIFY_SHUTDOWN
};
2383 * PCI device binding table
2385 static struct pci_device_id cxlflash_pci_table
[] = {
2386 {PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_IBM_CORSA
,
2387 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (kernel_ulong_t
)&dev_corsa_vals
},
2388 {PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_IBM_FLASH_GT
,
2389 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (kernel_ulong_t
)&dev_flash_gt_vals
},
2393 MODULE_DEVICE_TABLE(pci
, cxlflash_pci_table
);
2396 * cxlflash_worker_thread() - work thread handler for the AFU
2397 * @work: Work structure contained within cxlflash associated with host.
2399 * Handles the following events:
2400 * - Link reset which cannot be performed on interrupt context due to
2401 * blocking up to a few seconds
2402 * - Read AFU command room
2405 static void cxlflash_worker_thread(struct work_struct
*work
)
2407 struct cxlflash_cfg
*cfg
= container_of(work
, struct cxlflash_cfg
,
2409 struct afu
*afu
= cfg
->afu
;
2410 struct device
*dev
= &cfg
->dev
->dev
;
2414 /* Avoid MMIO if the device has failed */
2416 if (cfg
->state
!= STATE_NORMAL
)
2419 spin_lock_irqsave(cfg
->host
->host_lock
, lock_flags
);
2421 if (cfg
->lr_state
== LINK_RESET_REQUIRED
) {
2422 port
= cfg
->lr_port
;
2424 dev_err(dev
, "%s: invalid port index %d\n",
2427 spin_unlock_irqrestore(cfg
->host
->host_lock
,
2430 /* The reset can block... */
2431 afu_link_reset(afu
, port
,
2432 &afu
->afu_map
->global
.fc_regs
[port
][0]);
2433 spin_lock_irqsave(cfg
->host
->host_lock
, lock_flags
);
2436 cfg
->lr_state
= LINK_RESET_COMPLETE
;
2439 if (afu
->read_room
) {
2440 atomic64_set(&afu
->room
, readq_be(&afu
->host_map
->cmd_room
));
2441 afu
->read_room
= false;
2444 spin_unlock_irqrestore(cfg
->host
->host_lock
, lock_flags
);
2446 if (atomic_dec_if_positive(&cfg
->scan_host_needed
) >= 0)
2447 scsi_scan_host(cfg
->host
);
2448 kref_put(&afu
->mapcount
, afu_unmap
);
2452 * cxlflash_probe() - PCI entry point to add host
2453 * @pdev: PCI device associated with the host.
2454 * @dev_id: PCI device id associated with device.
2456 * Return: 0 on success, -errno on failure
2458 static int cxlflash_probe(struct pci_dev
*pdev
,
2459 const struct pci_device_id
*dev_id
)
2461 struct Scsi_Host
*host
;
2462 struct cxlflash_cfg
*cfg
= NULL
;
2463 struct dev_dependent_vals
*ddv
;
2466 dev_dbg(&pdev
->dev
, "%s: Found CXLFLASH with IRQ: %d\n",
2467 __func__
, pdev
->irq
);
2469 ddv
= (struct dev_dependent_vals
*)dev_id
->driver_data
;
2470 driver_template
.max_sectors
= ddv
->max_sectors
;
2472 host
= scsi_host_alloc(&driver_template
, sizeof(struct cxlflash_cfg
));
2474 dev_err(&pdev
->dev
, "%s: call to scsi_host_alloc failed!\n",
2480 host
->max_id
= CXLFLASH_MAX_NUM_TARGETS_PER_BUS
;
2481 host
->max_lun
= CXLFLASH_MAX_NUM_LUNS_PER_TARGET
;
2482 host
->max_channel
= NUM_FC_PORTS
- 1;
2483 host
->unique_id
= host
->host_no
;
2484 host
->max_cmd_len
= CXLFLASH_MAX_CDB_LEN
;
2486 cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
2488 rc
= alloc_mem(cfg
);
2490 dev_err(&pdev
->dev
, "%s: call to alloc_mem failed!\n",
2493 scsi_host_put(cfg
->host
);
2497 cfg
->init_state
= INIT_STATE_NONE
;
2499 cfg
->cxl_fops
= cxlflash_cxl_fops
;
2502 * The promoted LUNs move to the top of the LUN table. The rest stay
2503 * on the bottom half. The bottom half grows from the end
2504 * (index = 255), whereas the top half grows from the beginning
2507 cfg
->promote_lun_index
= 0;
2508 cfg
->last_lun_index
[0] = CXLFLASH_NUM_VLUNS
/2 - 1;
2509 cfg
->last_lun_index
[1] = CXLFLASH_NUM_VLUNS
/2 - 1;
2511 cfg
->dev_id
= (struct pci_device_id
*)dev_id
;
2513 init_waitqueue_head(&cfg
->tmf_waitq
);
2514 init_waitqueue_head(&cfg
->reset_waitq
);
2516 INIT_WORK(&cfg
->work_q
, cxlflash_worker_thread
);
2517 cfg
->lr_state
= LINK_RESET_INVALID
;
2519 spin_lock_init(&cfg
->tmf_slock
);
2520 mutex_init(&cfg
->ctx_tbl_list_mutex
);
2521 mutex_init(&cfg
->ctx_recovery_mutex
);
2522 init_rwsem(&cfg
->ioctl_rwsem
);
2523 INIT_LIST_HEAD(&cfg
->ctx_err_recovery
);
2524 INIT_LIST_HEAD(&cfg
->lluns
);
2526 pci_set_drvdata(pdev
, cfg
);
2528 cfg
->cxl_afu
= cxl_pci_to_afu(pdev
);
2532 dev_err(&pdev
->dev
, "%s: call to init_pci "
2533 "failed rc=%d!\n", __func__
, rc
);
2536 cfg
->init_state
= INIT_STATE_PCI
;
2540 dev_err(&pdev
->dev
, "%s: call to init_afu "
2541 "failed rc=%d!\n", __func__
, rc
);
2544 cfg
->init_state
= INIT_STATE_AFU
;
2546 rc
= init_scsi(cfg
);
2548 dev_err(&pdev
->dev
, "%s: call to init_scsi "
2549 "failed rc=%d!\n", __func__
, rc
);
2552 cfg
->init_state
= INIT_STATE_SCSI
;
2555 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
2559 cxlflash_remove(pdev
);
2564 * cxlflash_pci_error_detected() - called when a PCI error is detected
2565 * @pdev: PCI device struct.
2566 * @state: PCI channel state.
2568 * When an EEH occurs during an active reset, wait until the reset is
2569 * complete and then take action based upon the device state.
2571 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2573 static pci_ers_result_t
cxlflash_pci_error_detected(struct pci_dev
*pdev
,
2574 pci_channel_state_t state
)
2577 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
2578 struct device
*dev
= &cfg
->dev
->dev
;
2580 dev_dbg(dev
, "%s: pdev=%p state=%u\n", __func__
, pdev
, state
);
2583 case pci_channel_io_frozen
:
2584 wait_event(cfg
->reset_waitq
, cfg
->state
!= STATE_RESET
);
2585 if (cfg
->state
== STATE_FAILTERM
)
2586 return PCI_ERS_RESULT_DISCONNECT
;
2588 cfg
->state
= STATE_RESET
;
2589 scsi_block_requests(cfg
->host
);
2591 rc
= cxlflash_mark_contexts_error(cfg
);
2593 dev_err(dev
, "%s: Failed to mark user contexts!(%d)\n",
2596 return PCI_ERS_RESULT_NEED_RESET
;
2597 case pci_channel_io_perm_failure
:
2598 cfg
->state
= STATE_FAILTERM
;
2599 wake_up_all(&cfg
->reset_waitq
);
2600 scsi_unblock_requests(cfg
->host
);
2601 return PCI_ERS_RESULT_DISCONNECT
;
2605 return PCI_ERS_RESULT_NEED_RESET
;
2609 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2610 * @pdev: PCI device struct.
2612 * This routine is called by the pci error recovery code after the PCI
2613 * slot has been reset, just before we should resume normal operations.
2615 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2617 static pci_ers_result_t
cxlflash_pci_slot_reset(struct pci_dev
*pdev
)
2620 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
2621 struct device
*dev
= &cfg
->dev
->dev
;
2623 dev_dbg(dev
, "%s: pdev=%p\n", __func__
, pdev
);
2627 dev_err(dev
, "%s: EEH recovery failed! (%d)\n", __func__
, rc
);
2628 return PCI_ERS_RESULT_DISCONNECT
;
2631 return PCI_ERS_RESULT_RECOVERED
;
2635 * cxlflash_pci_resume() - called when normal operation can resume
2636 * @pdev: PCI device struct
2638 static void cxlflash_pci_resume(struct pci_dev
*pdev
)
2640 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
2641 struct device
*dev
= &cfg
->dev
->dev
;
2643 dev_dbg(dev
, "%s: pdev=%p\n", __func__
, pdev
);
2645 cfg
->state
= STATE_NORMAL
;
2646 wake_up_all(&cfg
->reset_waitq
);
2647 scsi_unblock_requests(cfg
->host
);
2650 static const struct pci_error_handlers cxlflash_err_handler
= {
2651 .error_detected
= cxlflash_pci_error_detected
,
2652 .slot_reset
= cxlflash_pci_slot_reset
,
2653 .resume
= cxlflash_pci_resume
,
2657 * PCI device structure
2659 static struct pci_driver cxlflash_driver
= {
2660 .name
= CXLFLASH_NAME
,
2661 .id_table
= cxlflash_pci_table
,
2662 .probe
= cxlflash_probe
,
2663 .remove
= cxlflash_remove
,
2664 .shutdown
= cxlflash_remove
,
2665 .err_handler
= &cxlflash_err_handler
,
2669 * init_cxlflash() - module entry point
2671 * Return: 0 on success, -errno on failure
2673 static int __init
init_cxlflash(void)
2675 pr_info("%s: %s\n", __func__
, CXLFLASH_ADAPTER_NAME
);
2677 cxlflash_list_init();
2679 return pci_register_driver(&cxlflash_driver
);
2683 * exit_cxlflash() - module exit point
2685 static void __exit
exit_cxlflash(void)
2687 cxlflash_term_global_luns();
2688 cxlflash_free_errpage();
2690 pci_unregister_driver(&cxlflash_driver
);
2693 module_init(init_cxlflash
);
2694 module_exit(exit_cxlflash
);