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");
39 * cxlflash_cmd_checkout() - checks out an AFU command
40 * @afu: AFU to checkout from.
42 * Commands are checked out in a round-robin fashion. Note that since
43 * the command pool is larger than the hardware queue, the majority of
44 * times we will only loop once or twice before getting a command. The
45 * buffer and CDB within the command are initialized (zeroed) prior to
48 * Return: The checked out command or NULL when command pool is empty.
50 struct afu_cmd
*cxlflash_cmd_checkout(struct afu
*afu
)
52 int k
, dec
= CXLFLASH_NUM_CMDS
;
56 k
= (afu
->cmd_couts
++ & (CXLFLASH_NUM_CMDS
- 1));
60 if (!atomic_dec_if_positive(&cmd
->free
)) {
61 pr_debug("%s: returning found index=%d\n",
63 memset(cmd
->buf
, 0, CMD_BUFSIZE
);
64 memset(cmd
->rcb
.cdb
, 0, sizeof(cmd
->rcb
.cdb
));
73 * cxlflash_cmd_checkin() - checks in an AFU command
74 * @cmd: AFU command to checkin.
76 * Safe to pass commands that have already been checked in. Several
77 * internal tracking fields are reset as part of the checkin. Note
78 * that these are intentionally reset prior to toggling the free bit
79 * to avoid clobbering values in the event that the command is checked
82 void cxlflash_cmd_checkin(struct afu_cmd
*cmd
)
88 cmd
->sa
.host_use
[0] = 0; /* clears both completion and retry bytes */
90 if (unlikely(atomic_inc_return(&cmd
->free
) != 1)) {
91 pr_err("%s: Freeing cmd (%d) that is not in use!\n",
96 pr_debug("%s: released cmd %p index=%d\n", __func__
, cmd
, cmd
->slot
);
100 * process_cmd_err() - command error handler
101 * @cmd: AFU command that experienced the error.
102 * @scp: SCSI command associated with the AFU command in error.
104 * Translates error bits from AFU command to SCSI command results.
106 static void process_cmd_err(struct afu_cmd
*cmd
, struct scsi_cmnd
*scp
)
108 struct sisl_ioarcb
*ioarcb
;
109 struct sisl_ioasa
*ioasa
;
114 ioarcb
= &(cmd
->rcb
);
117 if (ioasa
->rc
.flags
& SISL_RC_FLAGS_UNDERRUN
) {
118 pr_debug("%s: cmd underrun cmd = %p scp = %p\n",
120 scp
->result
= (DID_ERROR
<< 16);
123 if (ioasa
->rc
.flags
& SISL_RC_FLAGS_OVERRUN
) {
124 pr_debug("%s: cmd underrun cmd = %p scp = %p\n",
126 scp
->result
= (DID_ERROR
<< 16);
129 pr_debug("%s: cmd failed afu_rc=%d scsi_rc=%d fc_rc=%d "
130 "afu_extra=0x%X, scsi_entra=0x%X, fc_extra=0x%X\n",
131 __func__
, ioasa
->rc
.afu_rc
, ioasa
->rc
.scsi_rc
,
132 ioasa
->rc
.fc_rc
, ioasa
->afu_extra
, ioasa
->scsi_extra
,
135 if (ioasa
->rc
.scsi_rc
) {
136 /* We have a SCSI status */
137 if (ioasa
->rc
.flags
& SISL_RC_FLAGS_SENSE_VALID
) {
138 memcpy(scp
->sense_buffer
, ioasa
->sense_data
,
139 SISL_SENSE_DATA_LEN
);
140 scp
->result
= ioasa
->rc
.scsi_rc
;
142 scp
->result
= ioasa
->rc
.scsi_rc
| (DID_ERROR
<< 16);
146 * We encountered an error. Set scp->result based on nature
149 if (ioasa
->rc
.fc_rc
) {
150 /* We have an FC status */
151 switch (ioasa
->rc
.fc_rc
) {
152 case SISL_FC_RC_LINKDOWN
:
153 scp
->result
= (DID_REQUEUE
<< 16);
155 case SISL_FC_RC_RESID
:
156 /* This indicates an FCP resid underrun */
157 if (!(ioasa
->rc
.flags
& SISL_RC_FLAGS_OVERRUN
)) {
158 /* If the SISL_RC_FLAGS_OVERRUN flag was set,
159 * then we will handle this error else where.
160 * If not then we must handle it here.
161 * This is probably an AFU bug. We will
162 * attempt a retry to see if that resolves it.
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_MEDIUM_ERROR
<< 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
;
222 struct afu
*afu
= cmd
->parent
;
223 struct cxlflash_cfg
*cfg
= afu
->parent
;
226 spin_lock_irqsave(&cmd
->slock
, lock_flags
);
227 cmd
->sa
.host_use_b
[0] |= B_DONE
;
228 spin_unlock_irqrestore(&cmd
->slock
, lock_flags
);
232 if (unlikely(cmd
->sa
.rc
.afu_rc
||
233 cmd
->sa
.rc
.scsi_rc
||
235 process_cmd_err(cmd
, scp
);
237 scp
->result
= (DID_OK
<< 16);
239 resid
= cmd
->sa
.resid
;
240 cmd_is_tmf
= cmd
->cmd_tmf
;
241 cxlflash_cmd_checkin(cmd
); /* Don't use cmd after here */
243 pr_debug("%s: calling scsi_set_resid, scp=%p "
244 "result=%X resid=%d\n", __func__
,
245 scp
, scp
->result
, resid
);
247 scsi_set_resid(scp
, resid
);
252 spin_lock_irqsave(&cfg
->tmf_waitq
.lock
, lock_flags
);
253 cfg
->tmf_active
= false;
254 wake_up_all_locked(&cfg
->tmf_waitq
);
255 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
,
259 complete(&cmd
->cevent
);
263 * send_tmf() - sends a Task Management Function (TMF)
264 * @afu: AFU to checkout from.
265 * @scp: SCSI command from stack.
266 * @tmfcmd: TMF command to send.
270 * SCSI_MLQUEUE_HOST_BUSY when host is busy
272 static int send_tmf(struct afu
*afu
, struct scsi_cmnd
*scp
, u64 tmfcmd
)
276 u32 port_sel
= scp
->device
->channel
+ 1;
278 struct Scsi_Host
*host
= scp
->device
->host
;
279 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
283 cmd
= cxlflash_cmd_checkout(afu
);
284 if (unlikely(!cmd
)) {
285 pr_err("%s: could not get a free command\n", __func__
);
286 rc
= SCSI_MLQUEUE_HOST_BUSY
;
290 /* If a Task Management Function is active, do not send one more.
292 spin_lock_irqsave(&cfg
->tmf_waitq
.lock
, lock_flags
);
294 wait_event_interruptible_locked_irq(cfg
->tmf_waitq
,
296 cfg
->tmf_active
= true;
298 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
, lock_flags
);
300 cmd
->rcb
.ctx_id
= afu
->ctx_hndl
;
301 cmd
->rcb
.port_sel
= port_sel
;
302 cmd
->rcb
.lun_id
= lun_to_lunid(scp
->device
->lun
);
304 lflag
= SISL_REQ_FLAGS_TMF_CMD
;
306 cmd
->rcb
.req_flags
= (SISL_REQ_FLAGS_PORT_LUN_ID
|
307 SISL_REQ_FLAGS_SUP_UNDERRUN
| lflag
);
309 /* Stash the scp in the reserved field, for reuse during interrupt */
312 /* Copy the CDB from the cmd passed in */
313 memcpy(cmd
->rcb
.cdb
, &tmfcmd
, sizeof(tmfcmd
));
315 /* Send the command */
316 rc
= cxlflash_send_cmd(afu
, cmd
);
318 cxlflash_cmd_checkin(cmd
);
319 spin_lock_irqsave(&cfg
->tmf_waitq
.lock
, lock_flags
);
320 cfg
->tmf_active
= false;
321 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
, lock_flags
);
325 spin_lock_irqsave(&cfg
->tmf_waitq
.lock
, lock_flags
);
326 wait_event_interruptible_locked_irq(cfg
->tmf_waitq
, !cfg
->tmf_active
);
327 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
, lock_flags
);
333 * cxlflash_driver_info() - information handler for this host driver
334 * @host: SCSI host associated with device.
336 * Return: A string describing the device.
338 static const char *cxlflash_driver_info(struct Scsi_Host
*host
)
340 return CXLFLASH_ADAPTER_NAME
;
344 * cxlflash_queuecommand() - sends a mid-layer request
345 * @host: SCSI host associated with device.
346 * @scp: SCSI command to send.
350 * SCSI_MLQUEUE_HOST_BUSY when host is busy
352 static int cxlflash_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*scp
)
354 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
355 struct afu
*afu
= cfg
->afu
;
356 struct pci_dev
*pdev
= cfg
->dev
;
357 struct device
*dev
= &cfg
->dev
->dev
;
359 u32 port_sel
= scp
->device
->channel
+ 1;
361 struct scatterlist
*sg
;
366 pr_debug("%s: (scp=%p) %d/%d/%d/%llu cdb=(%08X-%08X-%08X-%08X)\n",
367 __func__
, scp
, host
->host_no
, scp
->device
->channel
,
368 scp
->device
->id
, scp
->device
->lun
,
369 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
370 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
371 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
372 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
374 /* If a Task Management Function is active, wait for it to complete
375 * before continuing with regular commands.
377 spin_lock_irqsave(&cfg
->tmf_waitq
.lock
, lock_flags
);
378 if (cfg
->tmf_active
) {
379 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
, lock_flags
);
380 rc
= SCSI_MLQUEUE_HOST_BUSY
;
383 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
, lock_flags
);
385 switch (cfg
->state
) {
387 dev_dbg_ratelimited(dev
, "%s: device in limbo!\n", __func__
);
388 rc
= SCSI_MLQUEUE_HOST_BUSY
;
391 dev_dbg_ratelimited(dev
, "%s: device has failed!\n", __func__
);
392 scp
->result
= (DID_NO_CONNECT
<< 16);
400 cmd
= cxlflash_cmd_checkout(afu
);
401 if (unlikely(!cmd
)) {
402 pr_err("%s: could not get a free command\n", __func__
);
403 rc
= SCSI_MLQUEUE_HOST_BUSY
;
407 cmd
->rcb
.ctx_id
= afu
->ctx_hndl
;
408 cmd
->rcb
.port_sel
= port_sel
;
409 cmd
->rcb
.lun_id
= lun_to_lunid(scp
->device
->lun
);
411 if (scp
->sc_data_direction
== DMA_TO_DEVICE
)
412 lflag
= SISL_REQ_FLAGS_HOST_WRITE
;
414 lflag
= SISL_REQ_FLAGS_HOST_READ
;
416 cmd
->rcb
.req_flags
= (SISL_REQ_FLAGS_PORT_LUN_ID
|
417 SISL_REQ_FLAGS_SUP_UNDERRUN
| lflag
);
419 /* Stash the scp in the reserved field, for reuse during interrupt */
422 nseg
= scsi_dma_map(scp
);
423 if (unlikely(nseg
< 0)) {
424 dev_err(&pdev
->dev
, "%s: Fail DMA map! nseg=%d\n",
426 rc
= SCSI_MLQUEUE_HOST_BUSY
;
430 ncount
= scsi_sg_count(scp
);
431 scsi_for_each_sg(scp
, sg
, ncount
, i
) {
432 cmd
->rcb
.data_len
= sg_dma_len(sg
);
433 cmd
->rcb
.data_ea
= sg_dma_address(sg
);
436 /* Copy the CDB from the scsi_cmnd passed in */
437 memcpy(cmd
->rcb
.cdb
, scp
->cmnd
, sizeof(cmd
->rcb
.cdb
));
439 /* Send the command */
440 rc
= cxlflash_send_cmd(afu
, cmd
);
442 cxlflash_cmd_checkin(cmd
);
451 * cxlflash_eh_device_reset_handler() - reset a single LUN
452 * @scp: SCSI command to send.
455 * SUCCESS as defined in scsi/scsi.h
456 * FAILED as defined in scsi/scsi.h
458 static int cxlflash_eh_device_reset_handler(struct scsi_cmnd
*scp
)
461 struct Scsi_Host
*host
= scp
->device
->host
;
462 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
463 struct afu
*afu
= cfg
->afu
;
466 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
467 "cdb=(%08X-%08X-%08X-%08X)\n", __func__
, scp
,
468 host
->host_no
, scp
->device
->channel
,
469 scp
->device
->id
, scp
->device
->lun
,
470 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
471 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
472 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
473 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
475 switch (cfg
->state
) {
477 rcr
= send_tmf(afu
, scp
, TMF_LUN_RESET
);
482 wait_event(cfg
->limbo_waitq
, cfg
->state
!= STATE_LIMBO
);
483 if (cfg
->state
== STATE_NORMAL
)
491 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
496 * cxlflash_eh_host_reset_handler() - reset the host adapter
497 * @scp: SCSI command from stack identifying host.
500 * SUCCESS as defined in scsi/scsi.h
501 * FAILED as defined in scsi/scsi.h
503 static int cxlflash_eh_host_reset_handler(struct scsi_cmnd
*scp
)
507 struct Scsi_Host
*host
= scp
->device
->host
;
508 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
510 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
511 "cdb=(%08X-%08X-%08X-%08X)\n", __func__
, scp
,
512 host
->host_no
, scp
->device
->channel
,
513 scp
->device
->id
, scp
->device
->lun
,
514 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
515 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
516 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
517 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
519 switch (cfg
->state
) {
521 cfg
->state
= STATE_LIMBO
;
522 scsi_block_requests(cfg
->host
);
523 cxlflash_mark_contexts_error(cfg
);
524 rcr
= cxlflash_afu_reset(cfg
);
527 cfg
->state
= STATE_FAILTERM
;
529 cfg
->state
= STATE_NORMAL
;
530 wake_up_all(&cfg
->limbo_waitq
);
531 scsi_unblock_requests(cfg
->host
);
534 wait_event(cfg
->limbo_waitq
, cfg
->state
!= STATE_LIMBO
);
535 if (cfg
->state
== STATE_NORMAL
)
543 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
548 * cxlflash_change_queue_depth() - change the queue depth for the device
549 * @sdev: SCSI device destined for queue depth change.
550 * @qdepth: Requested queue depth value to set.
552 * The requested queue depth is capped to the maximum supported value.
554 * Return: The actual queue depth set.
556 static int cxlflash_change_queue_depth(struct scsi_device
*sdev
, int qdepth
)
559 if (qdepth
> CXLFLASH_MAX_CMDS_PER_LUN
)
560 qdepth
= CXLFLASH_MAX_CMDS_PER_LUN
;
562 scsi_change_queue_depth(sdev
, qdepth
);
563 return sdev
->queue_depth
;
567 * cxlflash_show_port_status() - queries and presents the current port status
568 * @dev: Generic device associated with the host owning the port.
569 * @attr: Device attribute representing the port.
570 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
572 * Return: The size of the ASCII string returned in @buf.
574 static ssize_t
cxlflash_show_port_status(struct device
*dev
,
575 struct device_attribute
*attr
,
578 struct Scsi_Host
*shost
= class_to_shost(dev
);
579 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)shost
->hostdata
;
580 struct afu
*afu
= cfg
->afu
;
588 rc
= kstrtouint((attr
->attr
.name
+ 4), 10, &port
);
589 if (rc
|| (port
> NUM_FC_PORTS
))
592 fc_regs
= &afu
->afu_map
->global
.fc_regs
[port
][0];
594 (readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]) & FC_MTIP_STATUS_MASK
);
596 if (status
== FC_MTIP_STATUS_ONLINE
)
597 disp_status
= "online";
598 else if (status
== FC_MTIP_STATUS_OFFLINE
)
599 disp_status
= "offline";
601 disp_status
= "unknown";
603 return snprintf(buf
, PAGE_SIZE
, "%s\n", disp_status
);
607 * cxlflash_show_lun_mode() - presents the current LUN mode of the host
608 * @dev: Generic device associated with the host.
609 * @attr: Device attribute representing the lun mode.
610 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
612 * Return: The size of the ASCII string returned in @buf.
614 static ssize_t
cxlflash_show_lun_mode(struct device
*dev
,
615 struct device_attribute
*attr
, char *buf
)
617 struct Scsi_Host
*shost
= class_to_shost(dev
);
618 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)shost
->hostdata
;
619 struct afu
*afu
= cfg
->afu
;
621 return snprintf(buf
, PAGE_SIZE
, "%u\n", afu
->internal_lun
);
625 * cxlflash_store_lun_mode() - sets the LUN mode of the host
626 * @dev: Generic device associated with the host.
627 * @attr: Device attribute representing the lun mode.
628 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
629 * @count: Length of data resizing in @buf.
631 * The CXL Flash AFU supports a dummy LUN mode where the external
632 * links and storage are not required. Space on the FPGA is used
633 * to create 1 or 2 small LUNs which are presented to the system
634 * as if they were a normal storage device. This feature is useful
635 * during development and also provides manufacturing with a way
636 * to test the AFU without an actual device.
638 * 0 = external LUN[s] (default)
639 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
640 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
641 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
642 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
644 * Return: The size of the ASCII string returned in @buf.
646 static ssize_t
cxlflash_store_lun_mode(struct device
*dev
,
647 struct device_attribute
*attr
,
648 const char *buf
, size_t count
)
650 struct Scsi_Host
*shost
= class_to_shost(dev
);
651 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)shost
->hostdata
;
652 struct afu
*afu
= cfg
->afu
;
656 rc
= kstrtouint(buf
, 10, &lun_mode
);
657 if (!rc
&& (lun_mode
< 5) && (lun_mode
!= afu
->internal_lun
)) {
658 afu
->internal_lun
= lun_mode
;
659 cxlflash_afu_reset(cfg
);
660 scsi_scan_host(cfg
->host
);
667 * cxlflash_show_ioctl_version() - presents the current ioctl version of the host
668 * @dev: Generic device associated with the host.
669 * @attr: Device attribute representing the ioctl version.
670 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
672 * Return: The size of the ASCII string returned in @buf.
674 static ssize_t
cxlflash_show_ioctl_version(struct device
*dev
,
675 struct device_attribute
*attr
,
678 return scnprintf(buf
, PAGE_SIZE
, "%u\n", DK_CXLFLASH_VERSION_0
);
682 * cxlflash_show_dev_mode() - presents the current mode of the device
683 * @dev: Generic device associated with the device.
684 * @attr: Device attribute representing the device mode.
685 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
687 * Return: The size of the ASCII string returned in @buf.
689 static ssize_t
cxlflash_show_dev_mode(struct device
*dev
,
690 struct device_attribute
*attr
, char *buf
)
692 struct scsi_device
*sdev
= to_scsi_device(dev
);
694 return snprintf(buf
, PAGE_SIZE
, "%s\n",
695 sdev
->hostdata
? "superpipe" : "legacy");
699 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
700 * @cxlflash: Internal structure associated with the host.
702 static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg
*cfg
)
704 struct pci_dev
*pdev
= cfg
->dev
;
706 if (pci_channel_offline(pdev
))
707 wait_event_timeout(cfg
->limbo_waitq
,
708 !pci_channel_offline(pdev
),
709 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT
);
715 static DEVICE_ATTR(port0
, S_IRUGO
, cxlflash_show_port_status
, NULL
);
716 static DEVICE_ATTR(port1
, S_IRUGO
, cxlflash_show_port_status
, NULL
);
717 static DEVICE_ATTR(lun_mode
, S_IRUGO
| S_IWUSR
, cxlflash_show_lun_mode
,
718 cxlflash_store_lun_mode
);
719 static DEVICE_ATTR(ioctl_version
, S_IRUGO
, cxlflash_show_ioctl_version
, NULL
);
721 static struct device_attribute
*cxlflash_host_attrs
[] = {
725 &dev_attr_ioctl_version
,
732 static DEVICE_ATTR(mode
, S_IRUGO
, cxlflash_show_dev_mode
, NULL
);
734 static struct device_attribute
*cxlflash_dev_attrs
[] = {
742 static struct scsi_host_template driver_template
= {
743 .module
= THIS_MODULE
,
744 .name
= CXLFLASH_ADAPTER_NAME
,
745 .info
= cxlflash_driver_info
,
746 .ioctl
= cxlflash_ioctl
,
747 .proc_name
= CXLFLASH_NAME
,
748 .queuecommand
= cxlflash_queuecommand
,
749 .eh_device_reset_handler
= cxlflash_eh_device_reset_handler
,
750 .eh_host_reset_handler
= cxlflash_eh_host_reset_handler
,
751 .change_queue_depth
= cxlflash_change_queue_depth
,
753 .can_queue
= CXLFLASH_MAX_CMDS
,
755 .sg_tablesize
= SG_NONE
, /* No scatter gather support. */
756 .max_sectors
= CXLFLASH_MAX_SECTORS
,
757 .use_clustering
= ENABLE_CLUSTERING
,
758 .shost_attrs
= cxlflash_host_attrs
,
759 .sdev_attrs
= cxlflash_dev_attrs
,
763 * Device dependent values
765 static struct dev_dependent_vals dev_corsa_vals
= { CXLFLASH_MAX_SECTORS
};
768 * PCI device binding table
770 static struct pci_device_id cxlflash_pci_table
[] = {
771 {PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_IBM_CORSA
,
772 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (kernel_ulong_t
)&dev_corsa_vals
},
776 MODULE_DEVICE_TABLE(pci
, cxlflash_pci_table
);
779 * free_mem() - free memory associated with the AFU
780 * @cxlflash: Internal structure associated with the host.
782 static void free_mem(struct cxlflash_cfg
*cfg
)
786 struct afu
*afu
= cfg
->afu
;
789 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; i
++) {
790 buf
= afu
->cmd
[i
].buf
;
791 if (!((u64
)buf
& (PAGE_SIZE
- 1)))
792 free_page((ulong
)buf
);
795 free_pages((ulong
)afu
, get_order(sizeof(struct afu
)));
801 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
802 * @cxlflash: Internal structure associated with the host.
804 * Safe to call with AFU in a partially allocated/initialized state.
806 static void stop_afu(struct cxlflash_cfg
*cfg
)
809 struct afu
*afu
= cfg
->afu
;
812 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; i
++)
813 complete(&afu
->cmd
[i
].cevent
);
815 if (likely(afu
->afu_map
)) {
816 cxl_psa_unmap((void *)afu
->afu_map
);
823 * term_mc() - terminates the master context
824 * @cxlflash: Internal structure associated with the host.
825 * @level: Depth of allocation, where to begin waterfall tear down.
827 * Safe to call with AFU/MC in partially allocated/initialized state.
829 static void term_mc(struct cxlflash_cfg
*cfg
, enum undo_level level
)
832 struct afu
*afu
= cfg
->afu
;
834 if (!afu
|| !cfg
->mcctx
) {
835 pr_err("%s: returning from term_mc with NULL afu or MC\n",
842 rc
= cxl_stop_context(cfg
->mcctx
);
845 cxl_unmap_afu_irq(cfg
->mcctx
, 3, afu
);
847 cxl_unmap_afu_irq(cfg
->mcctx
, 2, afu
);
849 cxl_unmap_afu_irq(cfg
->mcctx
, 1, afu
);
851 cxl_free_afu_irqs(cfg
->mcctx
);
852 case RELEASE_CONTEXT
:
858 * term_afu() - terminates the AFU
859 * @cxlflash: Internal structure associated with the host.
861 * Safe to call with AFU/MC in partially allocated/initialized state.
863 static void term_afu(struct cxlflash_cfg
*cfg
)
865 term_mc(cfg
, UNDO_START
);
870 pr_debug("%s: returning\n", __func__
);
874 * cxlflash_remove() - PCI entry point to tear down host
875 * @pdev: PCI device associated with the host.
877 * Safe to use as a cleanup in partially allocated/initialized state.
879 static void cxlflash_remove(struct pci_dev
*pdev
)
881 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
884 /* If a Task Management Function is active, wait for it to complete
885 * before continuing with remove.
887 spin_lock_irqsave(&cfg
->tmf_waitq
.lock
, lock_flags
);
889 wait_event_interruptible_locked_irq(cfg
->tmf_waitq
,
891 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
, lock_flags
);
893 cfg
->state
= STATE_FAILTERM
;
894 cxlflash_stop_term_user_contexts(cfg
);
896 switch (cfg
->init_state
) {
897 case INIT_STATE_SCSI
:
898 cxlflash_term_local_luns(cfg
);
899 scsi_remove_host(cfg
->host
);
900 scsi_host_put(cfg
->host
);
905 pci_release_regions(cfg
->dev
);
906 pci_disable_device(pdev
);
907 case INIT_STATE_NONE
:
908 flush_work(&cfg
->work_q
);
913 pr_debug("%s: returning\n", __func__
);
917 * alloc_mem() - allocates the AFU and its command pool
918 * @cxlflash: Internal structure associated with the host.
920 * A partially allocated state remains on failure.
924 * -ENOMEM on failure to allocate memory
926 static int alloc_mem(struct cxlflash_cfg
*cfg
)
932 /* This allocation is about 12K, i.e. only 1 64k page
933 * and upto 4 4k pages
935 cfg
->afu
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
936 get_order(sizeof(struct afu
)));
937 if (unlikely(!cfg
->afu
)) {
938 pr_err("%s: cannot get %d free pages\n",
939 __func__
, get_order(sizeof(struct afu
)));
943 cfg
->afu
->parent
= cfg
;
944 cfg
->afu
->afu_map
= NULL
;
946 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; buf
+= CMD_BUFSIZE
, i
++) {
947 if (!((u64
)buf
& (PAGE_SIZE
- 1))) {
948 buf
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
949 if (unlikely(!buf
)) {
950 pr_err("%s: Allocate command buffers fail!\n",
958 cfg
->afu
->cmd
[i
].buf
= buf
;
959 atomic_set(&cfg
->afu
->cmd
[i
].free
, 1);
960 cfg
->afu
->cmd
[i
].slot
= i
;
968 * init_pci() - initializes the host as a PCI device
969 * @cxlflash: Internal structure associated with the host.
973 * -EIO on unable to communicate with device
974 * A return code from the PCI sub-routines
976 static int init_pci(struct cxlflash_cfg
*cfg
)
978 struct pci_dev
*pdev
= cfg
->dev
;
981 cfg
->cxlflash_regs_pci
= pci_resource_start(pdev
, 0);
982 rc
= pci_request_regions(pdev
, CXLFLASH_NAME
);
985 "%s: Couldn't register memory range of registers\n",
990 rc
= pci_enable_device(pdev
);
991 if (rc
|| pci_channel_offline(pdev
)) {
992 if (pci_channel_offline(pdev
)) {
993 cxlflash_wait_for_pci_err_recovery(cfg
);
994 rc
= pci_enable_device(pdev
);
998 dev_err(&pdev
->dev
, "%s: Cannot enable adapter\n",
1000 cxlflash_wait_for_pci_err_recovery(cfg
);
1001 goto out_release_regions
;
1005 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
1007 dev_dbg(&pdev
->dev
, "%s: Failed to set 64 bit PCI DMA mask\n",
1009 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
1013 dev_err(&pdev
->dev
, "%s: Failed to set PCI DMA mask\n",
1018 pci_set_master(pdev
);
1020 if (pci_channel_offline(pdev
)) {
1021 cxlflash_wait_for_pci_err_recovery(cfg
);
1022 if (pci_channel_offline(pdev
)) {
1024 goto out_msi_disable
;
1028 rc
= pci_save_state(pdev
);
1030 if (rc
!= PCIBIOS_SUCCESSFUL
) {
1031 dev_err(&pdev
->dev
, "%s: Failed to save PCI config space\n",
1038 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1043 cxlflash_wait_for_pci_err_recovery(cfg
);
1045 pci_disable_device(pdev
);
1046 out_release_regions
:
1047 pci_release_regions(pdev
);
1053 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
1054 * @cxlflash: Internal structure associated with the host.
1058 * A return code from adding the host
1060 static int init_scsi(struct cxlflash_cfg
*cfg
)
1062 struct pci_dev
*pdev
= cfg
->dev
;
1065 rc
= scsi_add_host(cfg
->host
, &pdev
->dev
);
1067 dev_err(&pdev
->dev
, "%s: scsi_add_host failed (rc=%d)\n",
1072 scsi_scan_host(cfg
->host
);
1075 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1080 * set_port_online() - transitions the specified host FC port to online state
1081 * @fc_regs: Top of MMIO region defined for specified port.
1083 * The provided MMIO region must be mapped prior to call. Online state means
1084 * that the FC link layer has synced, completed the handshaking process, and
1085 * is ready for login to start.
1087 static void set_port_online(u64
*fc_regs
)
1091 cmdcfg
= readq_be(&fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1092 cmdcfg
&= (~FC_MTIP_CMDCONFIG_OFFLINE
); /* clear OFF_LINE */
1093 cmdcfg
|= (FC_MTIP_CMDCONFIG_ONLINE
); /* set ON_LINE */
1094 writeq_be(cmdcfg
, &fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1098 * set_port_offline() - transitions the specified host FC port to offline state
1099 * @fc_regs: Top of MMIO region defined for specified port.
1101 * The provided MMIO region must be mapped prior to call.
1103 static void set_port_offline(u64
*fc_regs
)
1107 cmdcfg
= readq_be(&fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1108 cmdcfg
&= (~FC_MTIP_CMDCONFIG_ONLINE
); /* clear ON_LINE */
1109 cmdcfg
|= (FC_MTIP_CMDCONFIG_OFFLINE
); /* set OFF_LINE */
1110 writeq_be(cmdcfg
, &fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1114 * wait_port_online() - waits for the specified host FC port come online
1115 * @fc_regs: Top of MMIO region defined for specified port.
1116 * @delay_us: Number of microseconds to delay between reading port status.
1117 * @nretry: Number of cycles to retry reading port status.
1119 * The provided MMIO region must be mapped prior to call. This will timeout
1120 * when the cable is not plugged in.
1123 * TRUE (1) when the specified port is online
1124 * FALSE (0) when the specified port fails to come online after timeout
1125 * -EINVAL when @delay_us is less than 1000
1127 static int wait_port_online(u64
*fc_regs
, u32 delay_us
, u32 nretry
)
1131 if (delay_us
< 1000) {
1132 pr_err("%s: invalid delay specified %d\n", __func__
, delay_us
);
1137 msleep(delay_us
/ 1000);
1138 status
= readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]);
1139 } while ((status
& FC_MTIP_STATUS_MASK
) != FC_MTIP_STATUS_ONLINE
&&
1142 return ((status
& FC_MTIP_STATUS_MASK
) == FC_MTIP_STATUS_ONLINE
);
1146 * wait_port_offline() - waits for the specified host FC port go offline
1147 * @fc_regs: Top of MMIO region defined for specified port.
1148 * @delay_us: Number of microseconds to delay between reading port status.
1149 * @nretry: Number of cycles to retry reading port status.
1151 * The provided MMIO region must be mapped prior to call.
1154 * TRUE (1) when the specified port is offline
1155 * FALSE (0) when the specified port fails to go offline after timeout
1156 * -EINVAL when @delay_us is less than 1000
1158 static int wait_port_offline(u64
*fc_regs
, u32 delay_us
, u32 nretry
)
1162 if (delay_us
< 1000) {
1163 pr_err("%s: invalid delay specified %d\n", __func__
, delay_us
);
1168 msleep(delay_us
/ 1000);
1169 status
= readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]);
1170 } while ((status
& FC_MTIP_STATUS_MASK
) != FC_MTIP_STATUS_OFFLINE
&&
1173 return ((status
& FC_MTIP_STATUS_MASK
) == FC_MTIP_STATUS_OFFLINE
);
1177 * afu_set_wwpn() - configures the WWPN for the specified host FC port
1178 * @afu: AFU associated with the host that owns the specified FC port.
1179 * @port: Port number being configured.
1180 * @fc_regs: Top of MMIO region defined for specified port.
1181 * @wwpn: The world-wide-port-number previously discovered for port.
1183 * The provided MMIO region must be mapped prior to call. As part of the
1184 * sequence to configure the WWPN, the port is toggled offline and then back
1185 * online. This toggling action can cause this routine to delay up to a few
1186 * seconds. When configured to use the internal LUN feature of the AFU, a
1187 * failure to come online is overridden.
1190 * 0 when the WWPN is successfully written and the port comes back online
1191 * -1 when the port fails to go offline or come back up online
1193 static int afu_set_wwpn(struct afu
*afu
, int port
, u64
*fc_regs
, u64 wwpn
)
1197 set_port_offline(fc_regs
);
1199 if (!wait_port_offline(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1200 FC_PORT_STATUS_RETRY_CNT
)) {
1201 pr_debug("%s: wait on port %d to go offline timed out\n",
1203 ret
= -1; /* but continue on to leave the port back online */
1207 writeq_be(wwpn
, &fc_regs
[FC_PNAME
/ 8]);
1209 set_port_online(fc_regs
);
1211 if (!wait_port_online(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1212 FC_PORT_STATUS_RETRY_CNT
)) {
1213 pr_debug("%s: wait on port %d to go online timed out\n",
1218 * Override for internal lun!!!
1220 if (afu
->internal_lun
) {
1221 pr_debug("%s: Overriding port %d online timeout!!!\n",
1227 pr_debug("%s: returning rc=%d\n", __func__
, ret
);
1233 * afu_link_reset() - resets the specified host FC port
1234 * @afu: AFU associated with the host that owns the specified FC port.
1235 * @port: Port number being configured.
1236 * @fc_regs: Top of MMIO region defined for specified port.
1238 * The provided MMIO region must be mapped prior to call. The sequence to
1239 * reset the port involves toggling it offline and then back online. This
1240 * action can cause this routine to delay up to a few seconds. An effort
1241 * is made to maintain link with the device by switching to host to use
1242 * the alternate port exclusively while the reset takes place.
1243 * failure to come online is overridden.
1245 static void afu_link_reset(struct afu
*afu
, int port
, u64
*fc_regs
)
1249 /* first switch the AFU to the other links, if any */
1250 port_sel
= readq_be(&afu
->afu_map
->global
.regs
.afu_port_sel
);
1251 port_sel
&= ~(1 << port
);
1252 writeq_be(port_sel
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1253 cxlflash_afu_sync(afu
, 0, 0, AFU_GSYNC
);
1255 set_port_offline(fc_regs
);
1256 if (!wait_port_offline(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1257 FC_PORT_STATUS_RETRY_CNT
))
1258 pr_err("%s: wait on port %d to go offline timed out\n",
1261 set_port_online(fc_regs
);
1262 if (!wait_port_online(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1263 FC_PORT_STATUS_RETRY_CNT
))
1264 pr_err("%s: wait on port %d to go online timed out\n",
1267 /* switch back to include this port */
1268 port_sel
|= (1 << port
);
1269 writeq_be(port_sel
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1270 cxlflash_afu_sync(afu
, 0, 0, AFU_GSYNC
);
1272 pr_debug("%s: returning port_sel=%lld\n", __func__
, port_sel
);
1276 * Asynchronous interrupt information table
1278 static const struct asyc_intr_info ainfo
[] = {
1279 {SISL_ASTATUS_FC0_OTHER
, "other error", 0, CLR_FC_ERROR
| LINK_RESET
},
1280 {SISL_ASTATUS_FC0_LOGO
, "target initiated LOGO", 0, 0},
1281 {SISL_ASTATUS_FC0_CRC_T
, "CRC threshold exceeded", 0, LINK_RESET
},
1282 {SISL_ASTATUS_FC0_LOGI_R
, "login timed out, retrying", 0, 0},
1283 {SISL_ASTATUS_FC0_LOGI_F
, "login failed", 0, CLR_FC_ERROR
},
1284 {SISL_ASTATUS_FC0_LOGI_S
, "login succeeded", 0, 0},
1285 {SISL_ASTATUS_FC0_LINK_DN
, "link down", 0, 0},
1286 {SISL_ASTATUS_FC0_LINK_UP
, "link up", 0, 0},
1287 {SISL_ASTATUS_FC1_OTHER
, "other error", 1, CLR_FC_ERROR
| LINK_RESET
},
1288 {SISL_ASTATUS_FC1_LOGO
, "target initiated LOGO", 1, 0},
1289 {SISL_ASTATUS_FC1_CRC_T
, "CRC threshold exceeded", 1, LINK_RESET
},
1290 {SISL_ASTATUS_FC1_LOGI_R
, "login timed out, retrying", 1, 0},
1291 {SISL_ASTATUS_FC1_LOGI_F
, "login failed", 1, CLR_FC_ERROR
},
1292 {SISL_ASTATUS_FC1_LOGI_S
, "login succeeded", 1, 0},
1293 {SISL_ASTATUS_FC1_LINK_DN
, "link down", 1, 0},
1294 {SISL_ASTATUS_FC1_LINK_UP
, "link up", 1, 0},
1295 {0x0, "", 0, 0} /* terminator */
1299 * find_ainfo() - locates and returns asynchronous interrupt information
1300 * @status: Status code set by AFU on error.
1302 * Return: The located information or NULL when the status code is invalid.
1304 static const struct asyc_intr_info
*find_ainfo(u64 status
)
1306 const struct asyc_intr_info
*info
;
1308 for (info
= &ainfo
[0]; info
->status
; info
++)
1309 if (info
->status
== status
)
1316 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1317 * @afu: AFU associated with the host.
1319 static void afu_err_intr_init(struct afu
*afu
)
1324 /* global async interrupts: AFU clears afu_ctrl on context exit
1325 * if async interrupts were sent to that context. This prevents
1326 * the AFU form sending further async interrupts when
1328 * nobody to receive them.
1332 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_mask
);
1333 /* set LISN# to send and point to master context */
1334 reg
= ((u64
) (((afu
->ctx_hndl
<< 8) | SISL_MSI_ASYNC_ERROR
)) << 40);
1336 if (afu
->internal_lun
)
1337 reg
|= 1; /* Bit 63 indicates local lun */
1338 writeq_be(reg
, &afu
->afu_map
->global
.regs
.afu_ctrl
);
1340 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_clear
);
1341 /* unmask bits that are of interest */
1342 /* note: afu can send an interrupt after this step */
1343 writeq_be(SISL_ASTATUS_MASK
, &afu
->afu_map
->global
.regs
.aintr_mask
);
1344 /* clear again in case a bit came on after previous clear but before */
1346 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_clear
);
1348 /* Clear/Set internal lun bits */
1349 reg
= readq_be(&afu
->afu_map
->global
.fc_regs
[0][FC_CONFIG2
/ 8]);
1350 reg
&= SISL_FC_INTERNAL_MASK
;
1351 if (afu
->internal_lun
)
1352 reg
|= ((u64
)(afu
->internal_lun
- 1) << SISL_FC_INTERNAL_SHIFT
);
1353 writeq_be(reg
, &afu
->afu_map
->global
.fc_regs
[0][FC_CONFIG2
/ 8]);
1355 /* now clear FC errors */
1356 for (i
= 0; i
< NUM_FC_PORTS
; i
++) {
1357 writeq_be(0xFFFFFFFFU
,
1358 &afu
->afu_map
->global
.fc_regs
[i
][FC_ERROR
/ 8]);
1359 writeq_be(0, &afu
->afu_map
->global
.fc_regs
[i
][FC_ERRCAP
/ 8]);
1362 /* sync interrupts for master's IOARRIN write */
1363 /* note that unlike asyncs, there can be no pending sync interrupts */
1364 /* at this time (this is a fresh context and master has not written */
1365 /* IOARRIN yet), so there is nothing to clear. */
1367 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1368 writeq_be(SISL_MSI_SYNC_ERROR
, &afu
->host_map
->ctx_ctrl
);
1369 writeq_be(SISL_ISTATUS_MASK
, &afu
->host_map
->intr_mask
);
1373 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1374 * @irq: Interrupt number.
1375 * @data: Private data provided at interrupt registration, the AFU.
1377 * Return: Always return IRQ_HANDLED.
1379 static irqreturn_t
cxlflash_sync_err_irq(int irq
, void *data
)
1381 struct afu
*afu
= (struct afu
*)data
;
1385 reg
= readq_be(&afu
->host_map
->intr_status
);
1386 reg_unmasked
= (reg
& SISL_ISTATUS_UNMASK
);
1388 if (reg_unmasked
== 0UL) {
1389 pr_err("%s: %llX: spurious interrupt, intr_status %016llX\n",
1390 __func__
, (u64
)afu
, reg
);
1391 goto cxlflash_sync_err_irq_exit
;
1394 pr_err("%s: %llX: unexpected interrupt, intr_status %016llX\n",
1395 __func__
, (u64
)afu
, reg
);
1397 writeq_be(reg_unmasked
, &afu
->host_map
->intr_clear
);
1399 cxlflash_sync_err_irq_exit
:
1400 pr_debug("%s: returning rc=%d\n", __func__
, IRQ_HANDLED
);
1405 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1406 * @irq: Interrupt number.
1407 * @data: Private data provided at interrupt registration, the AFU.
1409 * Return: Always return IRQ_HANDLED.
1411 static irqreturn_t
cxlflash_rrq_irq(int irq
, void *data
)
1413 struct afu
*afu
= (struct afu
*)data
;
1414 struct afu_cmd
*cmd
;
1415 bool toggle
= afu
->toggle
;
1417 *hrrq_start
= afu
->hrrq_start
,
1418 *hrrq_end
= afu
->hrrq_end
,
1419 *hrrq_curr
= afu
->hrrq_curr
;
1421 /* Process however many RRQ entries that are ready */
1425 if ((entry
& SISL_RESP_HANDLE_T_BIT
) != toggle
)
1428 cmd
= (struct afu_cmd
*)(entry
& ~SISL_RESP_HANDLE_T_BIT
);
1431 /* Advance to next entry or wrap and flip the toggle bit */
1432 if (hrrq_curr
< hrrq_end
)
1435 hrrq_curr
= hrrq_start
;
1436 toggle
^= SISL_RESP_HANDLE_T_BIT
;
1440 afu
->hrrq_curr
= hrrq_curr
;
1441 afu
->toggle
= toggle
;
1447 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1448 * @irq: Interrupt number.
1449 * @data: Private data provided at interrupt registration, the AFU.
1451 * Return: Always return IRQ_HANDLED.
1453 static irqreturn_t
cxlflash_async_err_irq(int irq
, void *data
)
1455 struct afu
*afu
= (struct afu
*)data
;
1456 struct cxlflash_cfg
*cfg
;
1458 const struct asyc_intr_info
*info
;
1459 struct sisl_global_map
*global
= &afu
->afu_map
->global
;
1466 reg
= readq_be(&global
->regs
.aintr_status
);
1467 reg_unmasked
= (reg
& SISL_ASTATUS_UNMASK
);
1469 if (reg_unmasked
== 0) {
1470 pr_err("%s: spurious interrupt, aintr_status 0x%016llX\n",
1475 /* it is OK to clear AFU status before FC_ERROR */
1476 writeq_be(reg_unmasked
, &global
->regs
.aintr_clear
);
1478 /* check each bit that is on */
1479 for (i
= 0; reg_unmasked
; i
++, reg_unmasked
= (reg_unmasked
>> 1)) {
1480 info
= find_ainfo(1ULL << i
);
1481 if ((reg_unmasked
& 0x1) || !info
)
1486 pr_err("%s: FC Port %d -> %s, fc_status 0x%08llX\n",
1487 __func__
, port
, info
->desc
,
1488 readq_be(&global
->fc_regs
[port
][FC_STATUS
/ 8]));
1491 * do link reset first, some OTHER errors will set FC_ERROR
1492 * again if cleared before or w/o a reset
1494 if (info
->action
& LINK_RESET
) {
1495 pr_err("%s: FC Port %d: resetting link\n",
1497 cfg
->lr_state
= LINK_RESET_REQUIRED
;
1498 cfg
->lr_port
= port
;
1499 schedule_work(&cfg
->work_q
);
1502 if (info
->action
& CLR_FC_ERROR
) {
1503 reg
= readq_be(&global
->fc_regs
[port
][FC_ERROR
/ 8]);
1506 * since all errors are unmasked, FC_ERROR and FC_ERRCAP
1507 * should be the same and tracing one is sufficient.
1510 pr_err("%s: fc %d: clearing fc_error 0x%08llX\n",
1511 __func__
, port
, reg
);
1513 writeq_be(reg
, &global
->fc_regs
[port
][FC_ERROR
/ 8]);
1514 writeq_be(0, &global
->fc_regs
[port
][FC_ERRCAP
/ 8]);
1519 pr_debug("%s: returning rc=%d, afu=%p\n", __func__
, IRQ_HANDLED
, afu
);
1524 * start_context() - starts the master context
1525 * @cxlflash: Internal structure associated with the host.
1527 * Return: A success or failure value from CXL services.
1529 static int start_context(struct cxlflash_cfg
*cfg
)
1533 rc
= cxl_start_context(cfg
->mcctx
,
1534 cfg
->afu
->work
.work_element_descriptor
,
1537 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1542 * read_vpd() - obtains the WWPNs from VPD
1543 * @cxlflash: Internal structure associated with the host.
1544 * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
1548 * -ENODEV when VPD or WWPN keywords not found
1550 static int read_vpd(struct cxlflash_cfg
*cfg
, u64 wwpn
[])
1552 struct pci_dev
*dev
= cfg
->parent_dev
;
1554 int ro_start
, ro_size
, i
, j
, k
;
1556 char vpd_data
[CXLFLASH_VPD_LEN
];
1557 char tmp_buf
[WWPN_BUF_LEN
] = { 0 };
1558 char *wwpn_vpd_tags
[NUM_FC_PORTS
] = { "V5", "V6" };
1560 /* Get the VPD data from the device */
1561 vpd_size
= pci_read_vpd(dev
, 0, sizeof(vpd_data
), vpd_data
);
1562 if (unlikely(vpd_size
<= 0)) {
1563 pr_err("%s: Unable to read VPD (size = %ld)\n",
1564 __func__
, vpd_size
);
1569 /* Get the read only section offset */
1570 ro_start
= pci_vpd_find_tag(vpd_data
, 0, vpd_size
,
1571 PCI_VPD_LRDT_RO_DATA
);
1572 if (unlikely(ro_start
< 0)) {
1573 pr_err("%s: VPD Read-only data not found\n", __func__
);
1578 /* Get the read only section size, cap when extends beyond read VPD */
1579 ro_size
= pci_vpd_lrdt_size(&vpd_data
[ro_start
]);
1581 i
= ro_start
+ PCI_VPD_LRDT_TAG_SIZE
;
1582 if (unlikely((i
+ j
) > vpd_size
)) {
1583 pr_debug("%s: Might need to read more VPD (%d > %ld)\n",
1584 __func__
, (i
+ j
), vpd_size
);
1585 ro_size
= vpd_size
- i
;
1589 * Find the offset of the WWPN tag within the read only
1590 * VPD data and validate the found field (partials are
1591 * no good to us). Convert the ASCII data to an integer
1592 * value. Note that we must copy to a temporary buffer
1593 * because the conversion service requires that the ASCII
1594 * string be terminated.
1596 for (k
= 0; k
< NUM_FC_PORTS
; k
++) {
1598 i
= ro_start
+ PCI_VPD_LRDT_TAG_SIZE
;
1600 i
= pci_vpd_find_info_keyword(vpd_data
, i
, j
, wwpn_vpd_tags
[k
]);
1601 if (unlikely(i
< 0)) {
1602 pr_err("%s: Port %d WWPN not found in VPD\n",
1608 j
= pci_vpd_info_field_size(&vpd_data
[i
]);
1609 i
+= PCI_VPD_INFO_FLD_HDR_SIZE
;
1610 if (unlikely((i
+ j
> vpd_size
) || (j
!= WWPN_LEN
))) {
1611 pr_err("%s: Port %d WWPN incomplete or VPD corrupt\n",
1617 memcpy(tmp_buf
, &vpd_data
[i
], WWPN_LEN
);
1618 rc
= kstrtoul(tmp_buf
, WWPN_LEN
, (ulong
*)&wwpn
[k
]);
1620 pr_err("%s: Fail to convert port %d WWPN to integer\n",
1628 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1633 * cxlflash_context_reset() - timeout handler for AFU commands
1634 * @cmd: AFU command that timed out.
1636 * Sends a reset to the AFU.
1638 void cxlflash_context_reset(struct afu_cmd
*cmd
)
1643 struct afu
*afu
= cmd
->parent
;
1646 pr_debug("%s: cmd=%p\n", __func__
, cmd
);
1648 spin_lock_irqsave(&cmd
->slock
, lock_flags
);
1650 /* Already completed? */
1651 if (cmd
->sa
.host_use_b
[0] & B_DONE
) {
1652 spin_unlock_irqrestore(&cmd
->slock
, lock_flags
);
1656 cmd
->sa
.host_use_b
[0] |= (B_DONE
| B_ERROR
| B_TIMEOUT
);
1657 spin_unlock_irqrestore(&cmd
->slock
, lock_flags
);
1660 * We really want to send this reset at all costs, so spread
1661 * out wait time on successive retries for available room.
1664 room
= readq_be(&afu
->host_map
->cmd_room
);
1665 atomic64_set(&afu
->room
, room
);
1669 } while (nretry
++ < MC_ROOM_RETRY_CNT
);
1671 pr_err("%s: no cmd_room to send reset\n", __func__
);
1676 writeq_be(rrin
, &afu
->host_map
->ioarrin
);
1678 rrin
= readq_be(&afu
->host_map
->ioarrin
);
1681 /* Double delay each time */
1683 } while (nretry
++ < MC_ROOM_RETRY_CNT
);
1687 * init_pcr() - initialize the provisioning and control registers
1688 * @cxlflash: Internal structure associated with the host.
1690 * Also sets up fast access to the mapped registers and initializes AFU
1691 * command fields that never change.
1693 void init_pcr(struct cxlflash_cfg
*cfg
)
1695 struct afu
*afu
= cfg
->afu
;
1696 struct sisl_ctrl_map
*ctrl_map
;
1699 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
1700 ctrl_map
= &afu
->afu_map
->ctrls
[i
].ctrl
;
1701 /* disrupt any clients that could be running */
1702 /* e. g. clients that survived a master restart */
1703 writeq_be(0, &ctrl_map
->rht_start
);
1704 writeq_be(0, &ctrl_map
->rht_cnt_id
);
1705 writeq_be(0, &ctrl_map
->ctx_cap
);
1708 /* copy frequently used fields into afu */
1709 afu
->ctx_hndl
= (u16
) cxl_process_element(cfg
->mcctx
);
1710 /* ctx_hndl is 16 bits in CAIA */
1711 afu
->host_map
= &afu
->afu_map
->hosts
[afu
->ctx_hndl
].host
;
1712 afu
->ctrl_map
= &afu
->afu_map
->ctrls
[afu
->ctx_hndl
].ctrl
;
1714 /* Program the Endian Control for the master context */
1715 writeq_be(SISL_ENDIAN_CTRL
, &afu
->host_map
->endian_ctrl
);
1717 /* initialize cmd fields that never change */
1718 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; i
++) {
1719 afu
->cmd
[i
].rcb
.ctx_id
= afu
->ctx_hndl
;
1720 afu
->cmd
[i
].rcb
.msi
= SISL_MSI_RRQ_UPDATED
;
1721 afu
->cmd
[i
].rcb
.rrq
= 0x0;
1726 * init_global() - initialize AFU global registers
1727 * @cxlflash: Internal structure associated with the host.
1729 int init_global(struct cxlflash_cfg
*cfg
)
1731 struct afu
*afu
= cfg
->afu
;
1732 u64 wwpn
[NUM_FC_PORTS
]; /* wwpn of AFU ports */
1733 int i
= 0, num_ports
= 0;
1737 rc
= read_vpd(cfg
, &wwpn
[0]);
1739 pr_err("%s: could not read vpd rc=%d\n", __func__
, rc
);
1743 pr_debug("%s: wwpn0=0x%llX wwpn1=0x%llX\n", __func__
, wwpn
[0], wwpn
[1]);
1745 /* set up RRQ in AFU for master issued cmds */
1746 writeq_be((u64
) afu
->hrrq_start
, &afu
->host_map
->rrq_start
);
1747 writeq_be((u64
) afu
->hrrq_end
, &afu
->host_map
->rrq_end
);
1749 /* AFU configuration */
1750 reg
= readq_be(&afu
->afu_map
->global
.regs
.afu_config
);
1751 reg
|= SISL_AFUCONF_AR_ALL
|SISL_AFUCONF_ENDIAN
;
1752 /* enable all auto retry options and control endianness */
1753 /* leave others at default: */
1754 /* CTX_CAP write protected, mbox_r does not clear on read and */
1755 /* checker on if dual afu */
1756 writeq_be(reg
, &afu
->afu_map
->global
.regs
.afu_config
);
1758 /* global port select: select either port */
1759 if (afu
->internal_lun
) {
1760 /* only use port 0 */
1761 writeq_be(PORT0
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1762 num_ports
= NUM_FC_PORTS
- 1;
1764 writeq_be(BOTH_PORTS
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1765 num_ports
= NUM_FC_PORTS
;
1768 for (i
= 0; i
< num_ports
; i
++) {
1769 /* unmask all errors (but they are still masked at AFU) */
1770 writeq_be(0, &afu
->afu_map
->global
.fc_regs
[i
][FC_ERRMSK
/ 8]);
1771 /* clear CRC error cnt & set a threshold */
1772 (void)readq_be(&afu
->afu_map
->global
.
1773 fc_regs
[i
][FC_CNT_CRCERR
/ 8]);
1774 writeq_be(MC_CRC_THRESH
, &afu
->afu_map
->global
.fc_regs
[i
]
1775 [FC_CRC_THRESH
/ 8]);
1777 /* set WWPNs. If already programmed, wwpn[i] is 0 */
1779 afu_set_wwpn(afu
, i
,
1780 &afu
->afu_map
->global
.fc_regs
[i
][0],
1782 pr_err("%s: failed to set WWPN on port %d\n",
1787 /* Programming WWPN back to back causes additional
1788 * offline/online transitions and a PLOGI
1794 /* set up master's own CTX_CAP to allow real mode, host translation */
1795 /* tbls, afu cmds and read/write GSCSI cmds. */
1796 /* First, unlock ctx_cap write by reading mbox */
1797 (void)readq_be(&afu
->ctrl_map
->mbox_r
); /* unlock ctx_cap */
1798 writeq_be((SISL_CTX_CAP_REAL_MODE
| SISL_CTX_CAP_HOST_XLATE
|
1799 SISL_CTX_CAP_READ_CMD
| SISL_CTX_CAP_WRITE_CMD
|
1800 SISL_CTX_CAP_AFU_CMD
| SISL_CTX_CAP_GSCSI_CMD
),
1801 &afu
->ctrl_map
->ctx_cap
);
1802 /* init heartbeat */
1803 afu
->hb
= readq_be(&afu
->afu_map
->global
.regs
.afu_hb
);
1810 * start_afu() - initializes and starts the AFU
1811 * @cxlflash: Internal structure associated with the host.
1813 static int start_afu(struct cxlflash_cfg
*cfg
)
1815 struct afu
*afu
= cfg
->afu
;
1816 struct afu_cmd
*cmd
;
1821 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; i
++) {
1824 init_completion(&cmd
->cevent
);
1825 spin_lock_init(&cmd
->slock
);
1831 /* initialize RRQ pointers */
1832 afu
->hrrq_start
= &afu
->rrq_entry
[0];
1833 afu
->hrrq_end
= &afu
->rrq_entry
[NUM_RRQ_ENTRY
- 1];
1834 afu
->hrrq_curr
= afu
->hrrq_start
;
1837 rc
= init_global(cfg
);
1839 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1844 * init_mc() - create and register as the master context
1845 * @cxlflash: Internal structure associated with the host.
1849 * -ENOMEM when unable to obtain a context from CXL services
1850 * A failure value from CXL services.
1852 static int init_mc(struct cxlflash_cfg
*cfg
)
1854 struct cxl_context
*ctx
;
1855 struct device
*dev
= &cfg
->dev
->dev
;
1856 struct afu
*afu
= cfg
->afu
;
1858 enum undo_level level
;
1860 ctx
= cxl_get_context(cfg
->dev
);
1865 /* Set it up as a master with the CXL */
1866 cxl_set_master(ctx
);
1868 /* During initialization reset the AFU to start from a clean slate */
1869 rc
= cxl_afu_reset(cfg
->mcctx
);
1871 dev_err(dev
, "%s: initial AFU reset failed rc=%d\n",
1873 level
= RELEASE_CONTEXT
;
1877 rc
= cxl_allocate_afu_irqs(ctx
, 3);
1879 dev_err(dev
, "%s: call to allocate_afu_irqs failed rc=%d!\n",
1881 level
= RELEASE_CONTEXT
;
1885 rc
= cxl_map_afu_irq(ctx
, 1, cxlflash_sync_err_irq
, afu
,
1886 "SISL_MSI_SYNC_ERROR");
1887 if (unlikely(rc
<= 0)) {
1888 dev_err(dev
, "%s: IRQ 1 (SISL_MSI_SYNC_ERROR) map failed!\n",
1894 rc
= cxl_map_afu_irq(ctx
, 2, cxlflash_rrq_irq
, afu
,
1895 "SISL_MSI_RRQ_UPDATED");
1896 if (unlikely(rc
<= 0)) {
1897 dev_err(dev
, "%s: IRQ 2 (SISL_MSI_RRQ_UPDATED) map failed!\n",
1903 rc
= cxl_map_afu_irq(ctx
, 3, cxlflash_async_err_irq
, afu
,
1904 "SISL_MSI_ASYNC_ERROR");
1905 if (unlikely(rc
<= 0)) {
1906 dev_err(dev
, "%s: IRQ 3 (SISL_MSI_ASYNC_ERROR) map failed!\n",
1914 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1915 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1916 * element (pe) that is embedded in the context (ctx)
1918 rc
= start_context(cfg
);
1920 dev_err(dev
, "%s: start context failed rc=%d\n", __func__
, rc
);
1921 level
= UNMAP_THREE
;
1925 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1928 term_mc(cfg
, level
);
1933 * init_afu() - setup as master context and start AFU
1934 * @cxlflash: Internal structure associated with the host.
1936 * This routine is a higher level of control for configuring the
1937 * AFU on probe and reset paths.
1941 * -ENOMEM when unable to map the AFU MMIO space
1942 * A failure value from internal services.
1944 static int init_afu(struct cxlflash_cfg
*cfg
)
1948 struct afu
*afu
= cfg
->afu
;
1949 struct device
*dev
= &cfg
->dev
->dev
;
1951 cxl_perst_reloads_same_image(cfg
->cxl_afu
, true);
1955 dev_err(dev
, "%s: call to init_mc failed, rc=%d!\n",
1960 /* Map the entire MMIO space of the AFU.
1962 afu
->afu_map
= cxl_psa_map(cfg
->mcctx
);
1963 if (!afu
->afu_map
) {
1965 term_mc(cfg
, UNDO_START
);
1966 dev_err(dev
, "%s: call to cxl_psa_map failed!\n", __func__
);
1970 /* don't byte reverse on reading afu_version, else the string form */
1971 /* will be backwards */
1972 reg
= afu
->afu_map
->global
.regs
.afu_version
;
1973 memcpy(afu
->version
, ®
, 8);
1974 afu
->interface_version
=
1975 readq_be(&afu
->afu_map
->global
.regs
.interface_version
);
1976 pr_debug("%s: afu version %s, interface version 0x%llX\n",
1977 __func__
, afu
->version
, afu
->interface_version
);
1979 rc
= start_afu(cfg
);
1981 dev_err(dev
, "%s: call to start_afu failed, rc=%d!\n",
1983 term_mc(cfg
, UNDO_START
);
1984 cxl_psa_unmap((void *)afu
->afu_map
);
1985 afu
->afu_map
= NULL
;
1989 afu_err_intr_init(cfg
->afu
);
1990 atomic64_set(&afu
->room
, readq_be(&afu
->host_map
->cmd_room
));
1993 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1998 * cxlflash_send_cmd() - sends an AFU command
1999 * @afu: AFU associated with the host.
2000 * @cmd: AFU command to send.
2006 int cxlflash_send_cmd(struct afu
*afu
, struct afu_cmd
*cmd
)
2008 struct cxlflash_cfg
*cfg
= afu
->parent
;
2015 * This routine is used by critical users such an AFU sync and to
2016 * send a task management function (TMF). Thus we want to retry a
2017 * bit before returning an error. To avoid the performance penalty
2018 * of MMIO, we spread the update of 'room' over multiple commands.
2021 newval
= atomic64_dec_if_positive(&afu
->room
);
2024 room
= readq_be(&afu
->host_map
->cmd_room
);
2025 atomic64_set(&afu
->room
, room
);
2029 } while (nretry
++ < MC_ROOM_RETRY_CNT
);
2031 pr_err("%s: no cmd_room to send 0x%X\n",
2032 __func__
, cmd
->rcb
.cdb
[0]);
2035 } else if (unlikely(newval
< 0)) {
2036 /* This should be rare. i.e. Only if two threads race and
2037 * decrement before the MMIO read is done. In this case
2038 * just benefit from the other thread having updated
2041 if (nretry
++ < MC_ROOM_RETRY_CNT
) {
2050 writeq_be((u64
)&cmd
->rcb
, &afu
->host_map
->ioarrin
);
2052 pr_debug("%s: cmd=%p len=%d ea=%p rc=%d\n", __func__
, cmd
,
2053 cmd
->rcb
.data_len
, (void *)cmd
->rcb
.data_ea
, rc
);
2057 afu
->read_room
= true;
2058 schedule_work(&cfg
->work_q
);
2059 rc
= SCSI_MLQUEUE_HOST_BUSY
;
2064 * cxlflash_wait_resp() - polls for a response or timeout to a sent AFU command
2065 * @afu: AFU associated with the host.
2066 * @cmd: AFU command that was sent.
2068 void cxlflash_wait_resp(struct afu
*afu
, struct afu_cmd
*cmd
)
2070 ulong timeout
= jiffies
+ (cmd
->rcb
.timeout
* 2 * HZ
);
2072 timeout
= wait_for_completion_timeout(&cmd
->cevent
, timeout
);
2074 cxlflash_context_reset(cmd
);
2076 if (unlikely(cmd
->sa
.ioasc
!= 0))
2077 pr_err("%s: CMD 0x%X failed, IOASC: flags 0x%X, afu_rc 0x%X, "
2078 "scsi_rc 0x%X, fc_rc 0x%X\n", __func__
, cmd
->rcb
.cdb
[0],
2079 cmd
->sa
.rc
.flags
, cmd
->sa
.rc
.afu_rc
, cmd
->sa
.rc
.scsi_rc
,
2084 * cxlflash_afu_sync() - builds and sends an AFU sync command
2085 * @afu: AFU associated with the host.
2086 * @ctx_hndl_u: Identifies context requesting sync.
2087 * @res_hndl_u: Identifies resource requesting sync.
2088 * @mode: Type of sync to issue (lightweight, heavyweight, global).
2090 * The AFU can only take 1 sync command at a time. This routine enforces this
2091 * limitation by using a mutex to provide exlusive access to the AFU during
2092 * the sync. This design point requires calling threads to not be on interrupt
2093 * context due to the possibility of sleeping during concurrent sync operations.
2095 * AFU sync operations are only necessary and allowed when the device is
2096 * operating normally. When not operating normally, sync requests can occur as
2097 * part of cleaning up resources associated with an adapter prior to removal.
2098 * In this scenario, these requests are simply ignored (safe due to the AFU
2105 int cxlflash_afu_sync(struct afu
*afu
, ctx_hndl_t ctx_hndl_u
,
2106 res_hndl_t res_hndl_u
, u8 mode
)
2108 struct cxlflash_cfg
*cfg
= afu
->parent
;
2109 struct afu_cmd
*cmd
= NULL
;
2112 static DEFINE_MUTEX(sync_active
);
2114 if (cfg
->state
!= STATE_NORMAL
) {
2115 pr_debug("%s: Sync not required! (%u)\n", __func__
, cfg
->state
);
2119 mutex_lock(&sync_active
);
2121 cmd
= cxlflash_cmd_checkout(afu
);
2122 if (unlikely(!cmd
)) {
2124 udelay(1000 * retry_cnt
);
2125 if (retry_cnt
< MC_RETRY_CNT
)
2127 pr_err("%s: could not get a free command\n", __func__
);
2132 pr_debug("%s: afu=%p cmd=%p %d\n", __func__
, afu
, cmd
, ctx_hndl_u
);
2134 memset(cmd
->rcb
.cdb
, 0, sizeof(cmd
->rcb
.cdb
));
2136 cmd
->rcb
.req_flags
= SISL_REQ_FLAGS_AFU_CMD
;
2137 cmd
->rcb
.port_sel
= 0x0; /* NA */
2138 cmd
->rcb
.lun_id
= 0x0; /* NA */
2139 cmd
->rcb
.data_len
= 0x0;
2140 cmd
->rcb
.data_ea
= 0x0;
2141 cmd
->rcb
.timeout
= MC_AFU_SYNC_TIMEOUT
;
2143 cmd
->rcb
.cdb
[0] = 0xC0; /* AFU Sync */
2144 cmd
->rcb
.cdb
[1] = mode
;
2146 /* The cdb is aligned, no unaligned accessors required */
2147 *((u16
*)&cmd
->rcb
.cdb
[2]) = swab16(ctx_hndl_u
);
2148 *((u32
*)&cmd
->rcb
.cdb
[4]) = swab32(res_hndl_u
);
2150 rc
= cxlflash_send_cmd(afu
, cmd
);
2154 cxlflash_wait_resp(afu
, cmd
);
2156 /* set on timeout */
2157 if (unlikely((cmd
->sa
.ioasc
!= 0) ||
2158 (cmd
->sa
.host_use_b
[0] & B_ERROR
)))
2161 mutex_unlock(&sync_active
);
2163 cxlflash_cmd_checkin(cmd
);
2164 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
2169 * cxlflash_afu_reset() - resets the AFU
2170 * @cxlflash: Internal structure associated with the host.
2174 * A failure value from internal services.
2176 int cxlflash_afu_reset(struct cxlflash_cfg
*cfg
)
2179 /* Stop the context before the reset. Since the context is
2180 * no longer available restart it after the reset is complete
2187 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
2192 * cxlflash_worker_thread() - work thread handler for the AFU
2193 * @work: Work structure contained within cxlflash associated with host.
2195 * Handles the following events:
2196 * - Link reset which cannot be performed on interrupt context due to
2197 * blocking up to a few seconds
2198 * - Read AFU command room
2200 static void cxlflash_worker_thread(struct work_struct
*work
)
2202 struct cxlflash_cfg
*cfg
= container_of(work
, struct cxlflash_cfg
,
2204 struct afu
*afu
= cfg
->afu
;
2208 /* Avoid MMIO if the device has failed */
2210 if (cfg
->state
!= STATE_NORMAL
)
2213 spin_lock_irqsave(cfg
->host
->host_lock
, lock_flags
);
2215 if (cfg
->lr_state
== LINK_RESET_REQUIRED
) {
2216 port
= cfg
->lr_port
;
2218 pr_err("%s: invalid port index %d\n", __func__
, port
);
2220 spin_unlock_irqrestore(cfg
->host
->host_lock
,
2223 /* The reset can block... */
2224 afu_link_reset(afu
, port
,
2226 global
.fc_regs
[port
][0]);
2227 spin_lock_irqsave(cfg
->host
->host_lock
, lock_flags
);
2230 cfg
->lr_state
= LINK_RESET_COMPLETE
;
2233 if (afu
->read_room
) {
2234 atomic64_set(&afu
->room
, readq_be(&afu
->host_map
->cmd_room
));
2235 afu
->read_room
= false;
2238 spin_unlock_irqrestore(cfg
->host
->host_lock
, lock_flags
);
2242 * cxlflash_probe() - PCI entry point to add host
2243 * @pdev: PCI device associated with the host.
2244 * @dev_id: PCI device id associated with device.
2246 * Return: 0 on success / non-zero on failure
2248 static int cxlflash_probe(struct pci_dev
*pdev
,
2249 const struct pci_device_id
*dev_id
)
2251 struct Scsi_Host
*host
;
2252 struct cxlflash_cfg
*cfg
= NULL
;
2253 struct device
*phys_dev
;
2254 struct dev_dependent_vals
*ddv
;
2257 dev_dbg(&pdev
->dev
, "%s: Found CXLFLASH with IRQ: %d\n",
2258 __func__
, pdev
->irq
);
2260 ddv
= (struct dev_dependent_vals
*)dev_id
->driver_data
;
2261 driver_template
.max_sectors
= ddv
->max_sectors
;
2263 host
= scsi_host_alloc(&driver_template
, sizeof(struct cxlflash_cfg
));
2265 dev_err(&pdev
->dev
, "%s: call to scsi_host_alloc failed!\n",
2271 host
->max_id
= CXLFLASH_MAX_NUM_TARGETS_PER_BUS
;
2272 host
->max_lun
= CXLFLASH_MAX_NUM_LUNS_PER_TARGET
;
2273 host
->max_channel
= NUM_FC_PORTS
- 1;
2274 host
->unique_id
= host
->host_no
;
2275 host
->max_cmd_len
= CXLFLASH_MAX_CDB_LEN
;
2277 cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
2279 rc
= alloc_mem(cfg
);
2281 dev_err(&pdev
->dev
, "%s: call to scsi_host_alloc failed!\n",
2287 cfg
->init_state
= INIT_STATE_NONE
;
2289 cfg
->dev_id
= (struct pci_device_id
*)dev_id
;
2292 init_waitqueue_head(&cfg
->tmf_waitq
);
2293 init_waitqueue_head(&cfg
->limbo_waitq
);
2295 INIT_WORK(&cfg
->work_q
, cxlflash_worker_thread
);
2296 cfg
->lr_state
= LINK_RESET_INVALID
;
2298 mutex_init(&cfg
->ctx_tbl_list_mutex
);
2299 mutex_init(&cfg
->ctx_recovery_mutex
);
2300 INIT_LIST_HEAD(&cfg
->ctx_err_recovery
);
2301 INIT_LIST_HEAD(&cfg
->lluns
);
2303 pci_set_drvdata(pdev
, cfg
);
2305 /* Use the special service provided to look up the physical
2306 * PCI device, since we are called on the probe of the virtual
2307 * PCI host bus (vphb)
2309 phys_dev
= cxl_get_phys_dev(pdev
);
2310 if (!dev_is_pci(phys_dev
)) {
2311 pr_err("%s: not a pci dev\n", __func__
);
2315 cfg
->parent_dev
= to_pci_dev(phys_dev
);
2317 cfg
->cxl_afu
= cxl_pci_to_afu(pdev
);
2321 dev_err(&pdev
->dev
, "%s: call to init_pci "
2322 "failed rc=%d!\n", __func__
, rc
);
2325 cfg
->init_state
= INIT_STATE_PCI
;
2329 dev_err(&pdev
->dev
, "%s: call to init_afu "
2330 "failed rc=%d!\n", __func__
, rc
);
2333 cfg
->init_state
= INIT_STATE_AFU
;
2336 rc
= init_scsi(cfg
);
2338 dev_err(&pdev
->dev
, "%s: call to init_scsi "
2339 "failed rc=%d!\n", __func__
, rc
);
2342 cfg
->init_state
= INIT_STATE_SCSI
;
2345 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
2349 cxlflash_remove(pdev
);
2354 * cxlflash_pci_error_detected() - called when a PCI error is detected
2355 * @pdev: PCI device struct.
2356 * @state: PCI channel state.
2358 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2360 static pci_ers_result_t
cxlflash_pci_error_detected(struct pci_dev
*pdev
,
2361 pci_channel_state_t state
)
2364 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
2365 struct device
*dev
= &cfg
->dev
->dev
;
2367 dev_dbg(dev
, "%s: pdev=%p state=%u\n", __func__
, pdev
, state
);
2370 case pci_channel_io_frozen
:
2371 cfg
->state
= STATE_LIMBO
;
2373 /* Turn off legacy I/O */
2374 scsi_block_requests(cfg
->host
);
2375 rc
= cxlflash_mark_contexts_error(cfg
);
2377 dev_err(dev
, "%s: Failed to mark user contexts!(%d)\n",
2379 term_mc(cfg
, UNDO_START
);
2382 return PCI_ERS_RESULT_NEED_RESET
;
2383 case pci_channel_io_perm_failure
:
2384 cfg
->state
= STATE_FAILTERM
;
2385 wake_up_all(&cfg
->limbo_waitq
);
2386 scsi_unblock_requests(cfg
->host
);
2387 return PCI_ERS_RESULT_DISCONNECT
;
2391 return PCI_ERS_RESULT_NEED_RESET
;
2395 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2396 * @pdev: PCI device struct.
2398 * This routine is called by the pci error recovery code after the PCI
2399 * slot has been reset, just before we should resume normal operations.
2401 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2403 static pci_ers_result_t
cxlflash_pci_slot_reset(struct pci_dev
*pdev
)
2406 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
2407 struct device
*dev
= &cfg
->dev
->dev
;
2409 dev_dbg(dev
, "%s: pdev=%p\n", __func__
, pdev
);
2413 dev_err(dev
, "%s: EEH recovery failed! (%d)\n", __func__
, rc
);
2414 return PCI_ERS_RESULT_DISCONNECT
;
2417 return PCI_ERS_RESULT_RECOVERED
;
2421 * cxlflash_pci_resume() - called when normal operation can resume
2422 * @pdev: PCI device struct
2424 static void cxlflash_pci_resume(struct pci_dev
*pdev
)
2426 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
2427 struct device
*dev
= &cfg
->dev
->dev
;
2429 dev_dbg(dev
, "%s: pdev=%p\n", __func__
, pdev
);
2431 cfg
->state
= STATE_NORMAL
;
2432 wake_up_all(&cfg
->limbo_waitq
);
2433 scsi_unblock_requests(cfg
->host
);
2436 static const struct pci_error_handlers cxlflash_err_handler
= {
2437 .error_detected
= cxlflash_pci_error_detected
,
2438 .slot_reset
= cxlflash_pci_slot_reset
,
2439 .resume
= cxlflash_pci_resume
,
2443 * PCI device structure
2445 static struct pci_driver cxlflash_driver
= {
2446 .name
= CXLFLASH_NAME
,
2447 .id_table
= cxlflash_pci_table
,
2448 .probe
= cxlflash_probe
,
2449 .remove
= cxlflash_remove
,
2450 .err_handler
= &cxlflash_err_handler
,
2454 * init_cxlflash() - module entry point
2456 * Return: 0 on success / non-zero on failure
2458 static int __init
init_cxlflash(void)
2460 pr_info("%s: IBM Power CXL Flash Adapter: %s\n",
2461 __func__
, CXLFLASH_DRIVER_DATE
);
2463 cxlflash_list_init();
2465 return pci_register_driver(&cxlflash_driver
);
2469 * exit_cxlflash() - module exit point
2471 static void __exit
exit_cxlflash(void)
2473 cxlflash_term_global_luns();
2474 cxlflash_free_errpage();
2476 pci_unregister_driver(&cxlflash_driver
);
2479 module_init(init_cxlflash
);
2480 module_exit(exit_cxlflash
);