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>
31 MODULE_DESCRIPTION(CXLFLASH_ADAPTER_NAME
);
32 MODULE_AUTHOR("Manoj N. Kumar <manoj@linux.vnet.ibm.com>");
33 MODULE_AUTHOR("Matthew R. Ochs <mrochs@linux.vnet.ibm.com>");
34 MODULE_LICENSE("GPL");
38 * cxlflash_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 struct afu_cmd
*cxlflash_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_debug("%s: returning found index=%d\n",
62 memset(cmd
->buf
, 0, CMD_BUFSIZE
);
63 memset(cmd
->rcb
.cdb
, 0, sizeof(cmd
->rcb
.cdb
));
72 * cxlflash_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 void cxlflash_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_debug("%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
;
113 ioarcb
= &(cmd
->rcb
);
116 if (ioasa
->rc
.flags
& SISL_RC_FLAGS_UNDERRUN
) {
117 pr_debug("%s: cmd underrun cmd = %p scp = %p\n",
119 scp
->result
= (DID_ERROR
<< 16);
122 if (ioasa
->rc
.flags
& SISL_RC_FLAGS_OVERRUN
) {
123 pr_debug("%s: cmd underrun cmd = %p scp = %p\n",
125 scp
->result
= (DID_ERROR
<< 16);
128 pr_debug("%s: cmd failed afu_rc=%d scsi_rc=%d fc_rc=%d "
129 "afu_extra=0x%X, scsi_entra=0x%X, fc_extra=0x%X\n",
130 __func__
, ioasa
->rc
.afu_rc
, ioasa
->rc
.scsi_rc
,
131 ioasa
->rc
.fc_rc
, ioasa
->afu_extra
, ioasa
->scsi_extra
,
134 if (ioasa
->rc
.scsi_rc
) {
135 /* We have a SCSI status */
136 if (ioasa
->rc
.flags
& SISL_RC_FLAGS_SENSE_VALID
) {
137 memcpy(scp
->sense_buffer
, ioasa
->sense_data
,
138 SISL_SENSE_DATA_LEN
);
139 scp
->result
= ioasa
->rc
.scsi_rc
;
141 scp
->result
= ioasa
->rc
.scsi_rc
| (DID_ERROR
<< 16);
145 * We encountered an error. Set scp->result based on nature
148 if (ioasa
->rc
.fc_rc
) {
149 /* We have an FC status */
150 switch (ioasa
->rc
.fc_rc
) {
151 case SISL_FC_RC_LINKDOWN
:
152 scp
->result
= (DID_REQUEUE
<< 16);
154 case SISL_FC_RC_RESID
:
155 /* This indicates an FCP resid underrun */
156 if (!(ioasa
->rc
.flags
& SISL_RC_FLAGS_OVERRUN
)) {
157 /* If the SISL_RC_FLAGS_OVERRUN flag was set,
158 * then we will handle this error else where.
159 * If not then we must handle it here.
160 * This is probably an AFU bug. We will
161 * attempt a retry to see if that resolves it.
163 scp
->result
= (DID_ERROR
<< 16);
166 case SISL_FC_RC_RESIDERR
:
167 /* Resid mismatch between adapter and device */
168 case SISL_FC_RC_TGTABORT
:
169 case SISL_FC_RC_ABORTOK
:
170 case SISL_FC_RC_ABORTFAIL
:
171 case SISL_FC_RC_NOLOGI
:
172 case SISL_FC_RC_ABORTPEND
:
173 case SISL_FC_RC_WRABORTPEND
:
174 case SISL_FC_RC_NOEXP
:
175 case SISL_FC_RC_INUSE
:
176 scp
->result
= (DID_ERROR
<< 16);
181 if (ioasa
->rc
.afu_rc
) {
182 /* We have an AFU error */
183 switch (ioasa
->rc
.afu_rc
) {
184 case SISL_AFU_RC_NO_CHANNELS
:
185 scp
->result
= (DID_MEDIUM_ERROR
<< 16);
187 case SISL_AFU_RC_DATA_DMA_ERR
:
188 switch (ioasa
->afu_extra
) {
189 case SISL_AFU_DMA_ERR_PAGE_IN
:
191 scp
->result
= (DID_IMM_RETRY
<< 16);
193 case SISL_AFU_DMA_ERR_INVALID_EA
:
195 scp
->result
= (DID_ERROR
<< 16);
198 case SISL_AFU_RC_OUT_OF_DATA_BUFS
:
200 scp
->result
= (DID_ALLOC_FAILURE
<< 16);
203 scp
->result
= (DID_ERROR
<< 16);
209 * cmd_complete() - command completion handler
210 * @cmd: AFU command that has completed.
212 * Prepares and submits command that has either completed or timed out to
213 * the SCSI stack. Checks AFU command back into command pool for non-internal
214 * (rcb.scp populated) commands.
216 static void cmd_complete(struct afu_cmd
*cmd
)
218 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
.rc
.afu_rc
||
232 cmd
->sa
.rc
.scsi_rc
||
234 process_cmd_err(cmd
, scp
);
236 scp
->result
= (DID_OK
<< 16);
238 resid
= cmd
->sa
.resid
;
239 cmd_is_tmf
= cmd
->cmd_tmf
;
240 cxlflash_cmd_checkin(cmd
); /* Don't use cmd after here */
242 pr_debug("%s: calling scsi_set_resid, scp=%p "
243 "result=%X resid=%d\n", __func__
,
244 scp
, scp
->result
, resid
);
246 scsi_set_resid(scp
, resid
);
251 spin_lock_irqsave(&cfg
->tmf_waitq
.lock
, lock_flags
);
252 cfg
->tmf_active
= false;
253 wake_up_all_locked(&cfg
->tmf_waitq
);
254 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
,
258 complete(&cmd
->cevent
);
262 * send_tmf() - sends a Task Management Function (TMF)
263 * @afu: AFU to checkout from.
264 * @scp: SCSI command from stack.
265 * @tmfcmd: TMF command to send.
269 * SCSI_MLQUEUE_HOST_BUSY when host is busy
271 static int send_tmf(struct afu
*afu
, struct scsi_cmnd
*scp
, u64 tmfcmd
)
275 u32 port_sel
= scp
->device
->channel
+ 1;
277 struct Scsi_Host
*host
= scp
->device
->host
;
278 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
282 cmd
= cxlflash_cmd_checkout(afu
);
283 if (unlikely(!cmd
)) {
284 pr_err("%s: could not get a free command\n", __func__
);
285 rc
= SCSI_MLQUEUE_HOST_BUSY
;
289 /* If a Task Management Function is active, do not send one more.
291 spin_lock_irqsave(&cfg
->tmf_waitq
.lock
, lock_flags
);
293 wait_event_interruptible_locked_irq(cfg
->tmf_waitq
,
295 cfg
->tmf_active
= true;
297 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
, lock_flags
);
299 cmd
->rcb
.ctx_id
= afu
->ctx_hndl
;
300 cmd
->rcb
.port_sel
= port_sel
;
301 cmd
->rcb
.lun_id
= lun_to_lunid(scp
->device
->lun
);
303 lflag
= SISL_REQ_FLAGS_TMF_CMD
;
305 cmd
->rcb
.req_flags
= (SISL_REQ_FLAGS_PORT_LUN_ID
|
306 SISL_REQ_FLAGS_SUP_UNDERRUN
| lflag
);
308 /* Stash the scp in the reserved field, for reuse during interrupt */
311 /* Copy the CDB from the cmd passed in */
312 memcpy(cmd
->rcb
.cdb
, &tmfcmd
, sizeof(tmfcmd
));
314 /* Send the command */
315 rc
= cxlflash_send_cmd(afu
, cmd
);
317 cxlflash_cmd_checkin(cmd
);
318 spin_lock_irqsave(&cfg
->tmf_waitq
.lock
, lock_flags
);
319 cfg
->tmf_active
= false;
320 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
, lock_flags
);
324 spin_lock_irqsave(&cfg
->tmf_waitq
.lock
, lock_flags
);
325 wait_event_interruptible_locked_irq(cfg
->tmf_waitq
, !cfg
->tmf_active
);
326 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
, lock_flags
);
332 * cxlflash_driver_info() - information handler for this host driver
333 * @host: SCSI host associated with device.
335 * Return: A string describing the device.
337 static const char *cxlflash_driver_info(struct Scsi_Host
*host
)
339 return CXLFLASH_ADAPTER_NAME
;
343 * cxlflash_queuecommand() - sends a mid-layer request
344 * @host: SCSI host associated with device.
345 * @scp: SCSI command to send.
349 * SCSI_MLQUEUE_HOST_BUSY when host is busy
351 static int cxlflash_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*scp
)
353 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
354 struct afu
*afu
= cfg
->afu
;
355 struct pci_dev
*pdev
= cfg
->dev
;
356 struct device
*dev
= &cfg
->dev
->dev
;
358 u32 port_sel
= scp
->device
->channel
+ 1;
360 struct scatterlist
*sg
;
365 pr_debug("%s: (scp=%p) %d/%d/%d/%llu cdb=(%08X-%08X-%08X-%08X)\n",
366 __func__
, scp
, host
->host_no
, scp
->device
->channel
,
367 scp
->device
->id
, scp
->device
->lun
,
368 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
369 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
370 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
371 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
373 /* If a Task Management Function is active, wait for it to complete
374 * before continuing with regular commands.
376 spin_lock_irqsave(&cfg
->tmf_waitq
.lock
, lock_flags
);
377 if (cfg
->tmf_active
) {
378 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
, lock_flags
);
379 rc
= SCSI_MLQUEUE_HOST_BUSY
;
382 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
, lock_flags
);
384 switch (cfg
->state
) {
386 dev_dbg_ratelimited(dev
, "%s: device in limbo!\n", __func__
);
387 rc
= SCSI_MLQUEUE_HOST_BUSY
;
390 dev_dbg_ratelimited(dev
, "%s: device has failed!\n", __func__
);
391 scp
->result
= (DID_NO_CONNECT
<< 16);
399 cmd
= cxlflash_cmd_checkout(afu
);
400 if (unlikely(!cmd
)) {
401 pr_err("%s: could not get a free command\n", __func__
);
402 rc
= SCSI_MLQUEUE_HOST_BUSY
;
406 cmd
->rcb
.ctx_id
= afu
->ctx_hndl
;
407 cmd
->rcb
.port_sel
= port_sel
;
408 cmd
->rcb
.lun_id
= lun_to_lunid(scp
->device
->lun
);
410 if (scp
->sc_data_direction
== DMA_TO_DEVICE
)
411 lflag
= SISL_REQ_FLAGS_HOST_WRITE
;
413 lflag
= SISL_REQ_FLAGS_HOST_READ
;
415 cmd
->rcb
.req_flags
= (SISL_REQ_FLAGS_PORT_LUN_ID
|
416 SISL_REQ_FLAGS_SUP_UNDERRUN
| lflag
);
418 /* Stash the scp in the reserved field, for reuse during interrupt */
421 nseg
= scsi_dma_map(scp
);
422 if (unlikely(nseg
< 0)) {
423 dev_err(&pdev
->dev
, "%s: Fail DMA map! nseg=%d\n",
425 rc
= SCSI_MLQUEUE_HOST_BUSY
;
429 ncount
= scsi_sg_count(scp
);
430 scsi_for_each_sg(scp
, sg
, ncount
, i
) {
431 cmd
->rcb
.data_len
= sg_dma_len(sg
);
432 cmd
->rcb
.data_ea
= sg_dma_address(sg
);
435 /* Copy the CDB from the scsi_cmnd passed in */
436 memcpy(cmd
->rcb
.cdb
, scp
->cmnd
, sizeof(cmd
->rcb
.cdb
));
438 /* Send the command */
439 rc
= cxlflash_send_cmd(afu
, cmd
);
441 cxlflash_cmd_checkin(cmd
);
450 * cxlflash_eh_device_reset_handler() - reset a single LUN
451 * @scp: SCSI command to send.
454 * SUCCESS as defined in scsi/scsi.h
455 * FAILED as defined in scsi/scsi.h
457 static int cxlflash_eh_device_reset_handler(struct scsi_cmnd
*scp
)
460 struct Scsi_Host
*host
= scp
->device
->host
;
461 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
462 struct afu
*afu
= cfg
->afu
;
465 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
466 "cdb=(%08X-%08X-%08X-%08X)\n", __func__
, scp
,
467 host
->host_no
, scp
->device
->channel
,
468 scp
->device
->id
, scp
->device
->lun
,
469 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
470 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
471 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
472 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
474 switch (cfg
->state
) {
476 rcr
= send_tmf(afu
, scp
, TMF_LUN_RESET
);
481 wait_event(cfg
->limbo_waitq
, cfg
->state
!= STATE_LIMBO
);
482 if (cfg
->state
== STATE_NORMAL
)
490 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
495 * cxlflash_eh_host_reset_handler() - reset the host adapter
496 * @scp: SCSI command from stack identifying host.
499 * SUCCESS as defined in scsi/scsi.h
500 * FAILED as defined in scsi/scsi.h
502 static int cxlflash_eh_host_reset_handler(struct scsi_cmnd
*scp
)
506 struct Scsi_Host
*host
= scp
->device
->host
;
507 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
509 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
510 "cdb=(%08X-%08X-%08X-%08X)\n", __func__
, scp
,
511 host
->host_no
, scp
->device
->channel
,
512 scp
->device
->id
, scp
->device
->lun
,
513 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
514 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
515 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
516 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
518 switch (cfg
->state
) {
520 cfg
->state
= STATE_LIMBO
;
521 scsi_block_requests(cfg
->host
);
523 rcr
= cxlflash_afu_reset(cfg
);
526 cfg
->state
= STATE_FAILTERM
;
528 cfg
->state
= STATE_NORMAL
;
529 wake_up_all(&cfg
->limbo_waitq
);
530 scsi_unblock_requests(cfg
->host
);
533 wait_event(cfg
->limbo_waitq
, cfg
->state
!= STATE_LIMBO
);
534 if (cfg
->state
== STATE_NORMAL
)
542 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
547 * cxlflash_change_queue_depth() - change the queue depth for the device
548 * @sdev: SCSI device destined for queue depth change.
549 * @qdepth: Requested queue depth value to set.
551 * The requested queue depth is capped to the maximum supported value.
553 * Return: The actual queue depth set.
555 static int cxlflash_change_queue_depth(struct scsi_device
*sdev
, int qdepth
)
558 if (qdepth
> CXLFLASH_MAX_CMDS_PER_LUN
)
559 qdepth
= CXLFLASH_MAX_CMDS_PER_LUN
;
561 scsi_change_queue_depth(sdev
, qdepth
);
562 return sdev
->queue_depth
;
566 * cxlflash_show_port_status() - queries and presents the current port status
567 * @dev: Generic device associated with the host owning the port.
568 * @attr: Device attribute representing the port.
569 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
571 * Return: The size of the ASCII string returned in @buf.
573 static ssize_t
cxlflash_show_port_status(struct device
*dev
,
574 struct device_attribute
*attr
,
577 struct Scsi_Host
*shost
= class_to_shost(dev
);
578 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)shost
->hostdata
;
579 struct afu
*afu
= cfg
->afu
;
587 rc
= kstrtouint((attr
->attr
.name
+ 4), 10, &port
);
588 if (rc
|| (port
> NUM_FC_PORTS
))
591 fc_regs
= &afu
->afu_map
->global
.fc_regs
[port
][0];
593 (readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]) & FC_MTIP_STATUS_MASK
);
595 if (status
== FC_MTIP_STATUS_ONLINE
)
596 disp_status
= "online";
597 else if (status
== FC_MTIP_STATUS_OFFLINE
)
598 disp_status
= "offline";
600 disp_status
= "unknown";
602 return snprintf(buf
, PAGE_SIZE
, "%s\n", disp_status
);
606 * cxlflash_show_lun_mode() - presents the current LUN mode of the host
607 * @dev: Generic device associated with the host.
608 * @attr: Device attribute representing the lun mode.
609 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
611 * Return: The size of the ASCII string returned in @buf.
613 static ssize_t
cxlflash_show_lun_mode(struct device
*dev
,
614 struct device_attribute
*attr
, char *buf
)
616 struct Scsi_Host
*shost
= class_to_shost(dev
);
617 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)shost
->hostdata
;
618 struct afu
*afu
= cfg
->afu
;
620 return snprintf(buf
, PAGE_SIZE
, "%u\n", afu
->internal_lun
);
624 * cxlflash_store_lun_mode() - sets the LUN mode of the host
625 * @dev: Generic device associated with the host.
626 * @attr: Device attribute representing the lun mode.
627 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
628 * @count: Length of data resizing in @buf.
630 * The CXL Flash AFU supports a dummy LUN mode where the external
631 * links and storage are not required. Space on the FPGA is used
632 * to create 1 or 2 small LUNs which are presented to the system
633 * as if they were a normal storage device. This feature is useful
634 * during development and also provides manufacturing with a way
635 * to test the AFU without an actual device.
637 * 0 = external LUN[s] (default)
638 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
639 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
640 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
641 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
643 * Return: The size of the ASCII string returned in @buf.
645 static ssize_t
cxlflash_store_lun_mode(struct device
*dev
,
646 struct device_attribute
*attr
,
647 const char *buf
, size_t count
)
649 struct Scsi_Host
*shost
= class_to_shost(dev
);
650 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)shost
->hostdata
;
651 struct afu
*afu
= cfg
->afu
;
655 rc
= kstrtouint(buf
, 10, &lun_mode
);
656 if (!rc
&& (lun_mode
< 5) && (lun_mode
!= afu
->internal_lun
)) {
657 afu
->internal_lun
= lun_mode
;
658 cxlflash_afu_reset(cfg
);
659 scsi_scan_host(cfg
->host
);
666 * cxlflash_show_dev_mode() - presents the current mode of the device
667 * @dev: Generic device associated with the device.
668 * @attr: Device attribute representing the device mode.
669 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
671 * Return: The size of the ASCII string returned in @buf.
673 static ssize_t
cxlflash_show_dev_mode(struct device
*dev
,
674 struct device_attribute
*attr
, char *buf
)
676 struct scsi_device
*sdev
= to_scsi_device(dev
);
678 return snprintf(buf
, PAGE_SIZE
, "%s\n",
679 sdev
->hostdata
? "superpipe" : "legacy");
683 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
684 * @cxlflash: Internal structure associated with the host.
686 static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg
*cfg
)
688 struct pci_dev
*pdev
= cfg
->dev
;
690 if (pci_channel_offline(pdev
))
691 wait_event_timeout(cfg
->limbo_waitq
,
692 !pci_channel_offline(pdev
),
693 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT
);
699 static DEVICE_ATTR(port0
, S_IRUGO
, cxlflash_show_port_status
, NULL
);
700 static DEVICE_ATTR(port1
, S_IRUGO
, cxlflash_show_port_status
, NULL
);
701 static DEVICE_ATTR(lun_mode
, S_IRUGO
| S_IWUSR
, cxlflash_show_lun_mode
,
702 cxlflash_store_lun_mode
);
704 static struct device_attribute
*cxlflash_host_attrs
[] = {
714 static DEVICE_ATTR(mode
, S_IRUGO
, cxlflash_show_dev_mode
, NULL
);
716 static struct device_attribute
*cxlflash_dev_attrs
[] = {
724 static struct scsi_host_template driver_template
= {
725 .module
= THIS_MODULE
,
726 .name
= CXLFLASH_ADAPTER_NAME
,
727 .info
= cxlflash_driver_info
,
728 .proc_name
= CXLFLASH_NAME
,
729 .queuecommand
= cxlflash_queuecommand
,
730 .eh_device_reset_handler
= cxlflash_eh_device_reset_handler
,
731 .eh_host_reset_handler
= cxlflash_eh_host_reset_handler
,
732 .change_queue_depth
= cxlflash_change_queue_depth
,
734 .can_queue
= CXLFLASH_MAX_CMDS
,
736 .sg_tablesize
= SG_NONE
, /* No scatter gather support. */
737 .max_sectors
= CXLFLASH_MAX_SECTORS
,
738 .use_clustering
= ENABLE_CLUSTERING
,
739 .shost_attrs
= cxlflash_host_attrs
,
740 .sdev_attrs
= cxlflash_dev_attrs
,
744 * Device dependent values
746 static struct dev_dependent_vals dev_corsa_vals
= { CXLFLASH_MAX_SECTORS
};
749 * PCI device binding table
751 static struct pci_device_id cxlflash_pci_table
[] = {
752 {PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_IBM_CORSA
,
753 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (kernel_ulong_t
)&dev_corsa_vals
},
757 MODULE_DEVICE_TABLE(pci
, cxlflash_pci_table
);
760 * free_mem() - free memory associated with the AFU
761 * @cxlflash: Internal structure associated with the host.
763 static void free_mem(struct cxlflash_cfg
*cfg
)
767 struct afu
*afu
= cfg
->afu
;
770 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; i
++) {
771 buf
= afu
->cmd
[i
].buf
;
772 if (!((u64
)buf
& (PAGE_SIZE
- 1)))
773 free_page((ulong
)buf
);
776 free_pages((ulong
)afu
, get_order(sizeof(struct afu
)));
782 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
783 * @cxlflash: Internal structure associated with the host.
785 * Safe to call with AFU in a partially allocated/initialized state.
787 static void stop_afu(struct cxlflash_cfg
*cfg
)
790 struct afu
*afu
= cfg
->afu
;
793 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; i
++)
794 complete(&afu
->cmd
[i
].cevent
);
796 if (likely(afu
->afu_map
)) {
797 cxl_psa_unmap((void *)afu
->afu_map
);
804 * term_mc() - terminates the master context
805 * @cxlflash: Internal structure associated with the host.
806 * @level: Depth of allocation, where to begin waterfall tear down.
808 * Safe to call with AFU/MC in partially allocated/initialized state.
810 static void term_mc(struct cxlflash_cfg
*cfg
, enum undo_level level
)
813 struct afu
*afu
= cfg
->afu
;
815 if (!afu
|| !cfg
->mcctx
) {
816 pr_err("%s: returning from term_mc with NULL afu or MC\n",
823 rc
= cxl_stop_context(cfg
->mcctx
);
826 cxl_unmap_afu_irq(cfg
->mcctx
, 3, afu
);
828 cxl_unmap_afu_irq(cfg
->mcctx
, 2, afu
);
830 cxl_unmap_afu_irq(cfg
->mcctx
, 1, afu
);
832 cxl_free_afu_irqs(cfg
->mcctx
);
833 case RELEASE_CONTEXT
:
839 * term_afu() - terminates the AFU
840 * @cxlflash: Internal structure associated with the host.
842 * Safe to call with AFU/MC in partially allocated/initialized state.
844 static void term_afu(struct cxlflash_cfg
*cfg
)
846 term_mc(cfg
, UNDO_START
);
851 pr_debug("%s: returning\n", __func__
);
855 * cxlflash_remove() - PCI entry point to tear down host
856 * @pdev: PCI device associated with the host.
858 * Safe to use as a cleanup in partially allocated/initialized state.
860 static void cxlflash_remove(struct pci_dev
*pdev
)
862 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
865 /* If a Task Management Function is active, wait for it to complete
866 * before continuing with remove.
868 spin_lock_irqsave(&cfg
->tmf_waitq
.lock
, lock_flags
);
870 wait_event_interruptible_locked_irq(cfg
->tmf_waitq
,
872 spin_unlock_irqrestore(&cfg
->tmf_waitq
.lock
, lock_flags
);
874 cfg
->state
= STATE_FAILTERM
;
876 switch (cfg
->init_state
) {
877 case INIT_STATE_SCSI
:
878 scsi_remove_host(cfg
->host
);
879 scsi_host_put(cfg
->host
);
884 pci_release_regions(cfg
->dev
);
885 pci_disable_device(pdev
);
886 case INIT_STATE_NONE
:
887 flush_work(&cfg
->work_q
);
892 pr_debug("%s: returning\n", __func__
);
896 * alloc_mem() - allocates the AFU and its command pool
897 * @cxlflash: Internal structure associated with the host.
899 * A partially allocated state remains on failure.
903 * -ENOMEM on failure to allocate memory
905 static int alloc_mem(struct cxlflash_cfg
*cfg
)
911 /* This allocation is about 12K, i.e. only 1 64k page
912 * and upto 4 4k pages
914 cfg
->afu
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
915 get_order(sizeof(struct afu
)));
916 if (unlikely(!cfg
->afu
)) {
917 pr_err("%s: cannot get %d free pages\n",
918 __func__
, get_order(sizeof(struct afu
)));
922 cfg
->afu
->parent
= cfg
;
923 cfg
->afu
->afu_map
= NULL
;
925 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; buf
+= CMD_BUFSIZE
, i
++) {
926 if (!((u64
)buf
& (PAGE_SIZE
- 1))) {
927 buf
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
928 if (unlikely(!buf
)) {
929 pr_err("%s: Allocate command buffers fail!\n",
937 cfg
->afu
->cmd
[i
].buf
= buf
;
938 atomic_set(&cfg
->afu
->cmd
[i
].free
, 1);
939 cfg
->afu
->cmd
[i
].slot
= i
;
947 * init_pci() - initializes the host as a PCI device
948 * @cxlflash: Internal structure associated with the host.
952 * -EIO on unable to communicate with device
953 * A return code from the PCI sub-routines
955 static int init_pci(struct cxlflash_cfg
*cfg
)
957 struct pci_dev
*pdev
= cfg
->dev
;
960 cfg
->cxlflash_regs_pci
= pci_resource_start(pdev
, 0);
961 rc
= pci_request_regions(pdev
, CXLFLASH_NAME
);
964 "%s: Couldn't register memory range of registers\n",
969 rc
= pci_enable_device(pdev
);
970 if (rc
|| pci_channel_offline(pdev
)) {
971 if (pci_channel_offline(pdev
)) {
972 cxlflash_wait_for_pci_err_recovery(cfg
);
973 rc
= pci_enable_device(pdev
);
977 dev_err(&pdev
->dev
, "%s: Cannot enable adapter\n",
979 cxlflash_wait_for_pci_err_recovery(cfg
);
980 goto out_release_regions
;
984 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
986 dev_dbg(&pdev
->dev
, "%s: Failed to set 64 bit PCI DMA mask\n",
988 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
992 dev_err(&pdev
->dev
, "%s: Failed to set PCI DMA mask\n",
997 pci_set_master(pdev
);
999 if (pci_channel_offline(pdev
)) {
1000 cxlflash_wait_for_pci_err_recovery(cfg
);
1001 if (pci_channel_offline(pdev
)) {
1003 goto out_msi_disable
;
1007 rc
= pci_save_state(pdev
);
1009 if (rc
!= PCIBIOS_SUCCESSFUL
) {
1010 dev_err(&pdev
->dev
, "%s: Failed to save PCI config space\n",
1017 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1022 cxlflash_wait_for_pci_err_recovery(cfg
);
1024 pci_disable_device(pdev
);
1025 out_release_regions
:
1026 pci_release_regions(pdev
);
1032 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
1033 * @cxlflash: Internal structure associated with the host.
1037 * A return code from adding the host
1039 static int init_scsi(struct cxlflash_cfg
*cfg
)
1041 struct pci_dev
*pdev
= cfg
->dev
;
1044 rc
= scsi_add_host(cfg
->host
, &pdev
->dev
);
1046 dev_err(&pdev
->dev
, "%s: scsi_add_host failed (rc=%d)\n",
1051 scsi_scan_host(cfg
->host
);
1054 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1059 * set_port_online() - transitions the specified host FC port to online state
1060 * @fc_regs: Top of MMIO region defined for specified port.
1062 * The provided MMIO region must be mapped prior to call. Online state means
1063 * that the FC link layer has synced, completed the handshaking process, and
1064 * is ready for login to start.
1066 static void set_port_online(u64
*fc_regs
)
1070 cmdcfg
= readq_be(&fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1071 cmdcfg
&= (~FC_MTIP_CMDCONFIG_OFFLINE
); /* clear OFF_LINE */
1072 cmdcfg
|= (FC_MTIP_CMDCONFIG_ONLINE
); /* set ON_LINE */
1073 writeq_be(cmdcfg
, &fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1077 * set_port_offline() - transitions the specified host FC port to offline state
1078 * @fc_regs: Top of MMIO region defined for specified port.
1080 * The provided MMIO region must be mapped prior to call.
1082 static void set_port_offline(u64
*fc_regs
)
1086 cmdcfg
= readq_be(&fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1087 cmdcfg
&= (~FC_MTIP_CMDCONFIG_ONLINE
); /* clear ON_LINE */
1088 cmdcfg
|= (FC_MTIP_CMDCONFIG_OFFLINE
); /* set OFF_LINE */
1089 writeq_be(cmdcfg
, &fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1093 * wait_port_online() - waits for the specified host FC port come online
1094 * @fc_regs: Top of MMIO region defined for specified port.
1095 * @delay_us: Number of microseconds to delay between reading port status.
1096 * @nretry: Number of cycles to retry reading port status.
1098 * The provided MMIO region must be mapped prior to call. This will timeout
1099 * when the cable is not plugged in.
1102 * TRUE (1) when the specified port is online
1103 * FALSE (0) when the specified port fails to come online after timeout
1104 * -EINVAL when @delay_us is less than 1000
1106 static int wait_port_online(u64
*fc_regs
, u32 delay_us
, u32 nretry
)
1110 if (delay_us
< 1000) {
1111 pr_err("%s: invalid delay specified %d\n", __func__
, delay_us
);
1116 msleep(delay_us
/ 1000);
1117 status
= readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]);
1118 } while ((status
& FC_MTIP_STATUS_MASK
) != FC_MTIP_STATUS_ONLINE
&&
1121 return ((status
& FC_MTIP_STATUS_MASK
) == FC_MTIP_STATUS_ONLINE
);
1125 * wait_port_offline() - waits for the specified host FC port go offline
1126 * @fc_regs: Top of MMIO region defined for specified port.
1127 * @delay_us: Number of microseconds to delay between reading port status.
1128 * @nretry: Number of cycles to retry reading port status.
1130 * The provided MMIO region must be mapped prior to call.
1133 * TRUE (1) when the specified port is offline
1134 * FALSE (0) when the specified port fails to go offline after timeout
1135 * -EINVAL when @delay_us is less than 1000
1137 static int wait_port_offline(u64
*fc_regs
, u32 delay_us
, u32 nretry
)
1141 if (delay_us
< 1000) {
1142 pr_err("%s: invalid delay specified %d\n", __func__
, delay_us
);
1147 msleep(delay_us
/ 1000);
1148 status
= readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]);
1149 } while ((status
& FC_MTIP_STATUS_MASK
) != FC_MTIP_STATUS_OFFLINE
&&
1152 return ((status
& FC_MTIP_STATUS_MASK
) == FC_MTIP_STATUS_OFFLINE
);
1156 * afu_set_wwpn() - configures the WWPN for the specified host FC port
1157 * @afu: AFU associated with the host that owns the specified FC port.
1158 * @port: Port number being configured.
1159 * @fc_regs: Top of MMIO region defined for specified port.
1160 * @wwpn: The world-wide-port-number previously discovered for port.
1162 * The provided MMIO region must be mapped prior to call. As part of the
1163 * sequence to configure the WWPN, the port is toggled offline and then back
1164 * online. This toggling action can cause this routine to delay up to a few
1165 * seconds. When configured to use the internal LUN feature of the AFU, a
1166 * failure to come online is overridden.
1169 * 0 when the WWPN is successfully written and the port comes back online
1170 * -1 when the port fails to go offline or come back up online
1172 static int afu_set_wwpn(struct afu
*afu
, int port
, u64
*fc_regs
, u64 wwpn
)
1176 set_port_offline(fc_regs
);
1178 if (!wait_port_offline(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1179 FC_PORT_STATUS_RETRY_CNT
)) {
1180 pr_debug("%s: wait on port %d to go offline timed out\n",
1182 ret
= -1; /* but continue on to leave the port back online */
1186 writeq_be(wwpn
, &fc_regs
[FC_PNAME
/ 8]);
1188 set_port_online(fc_regs
);
1190 if (!wait_port_online(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1191 FC_PORT_STATUS_RETRY_CNT
)) {
1192 pr_debug("%s: wait on port %d to go online timed out\n",
1197 * Override for internal lun!!!
1199 if (afu
->internal_lun
) {
1200 pr_debug("%s: Overriding port %d online timeout!!!\n",
1206 pr_debug("%s: returning rc=%d\n", __func__
, ret
);
1212 * afu_link_reset() - resets the specified host FC port
1213 * @afu: AFU associated with the host that owns the specified FC port.
1214 * @port: Port number being configured.
1215 * @fc_regs: Top of MMIO region defined for specified port.
1217 * The provided MMIO region must be mapped prior to call. The sequence to
1218 * reset the port involves toggling it offline and then back online. This
1219 * action can cause this routine to delay up to a few seconds. An effort
1220 * is made to maintain link with the device by switching to host to use
1221 * the alternate port exclusively while the reset takes place.
1222 * failure to come online is overridden.
1224 static void afu_link_reset(struct afu
*afu
, int port
, u64
*fc_regs
)
1228 /* first switch the AFU to the other links, if any */
1229 port_sel
= readq_be(&afu
->afu_map
->global
.regs
.afu_port_sel
);
1230 port_sel
&= ~(1 << port
);
1231 writeq_be(port_sel
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1232 cxlflash_afu_sync(afu
, 0, 0, AFU_GSYNC
);
1234 set_port_offline(fc_regs
);
1235 if (!wait_port_offline(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1236 FC_PORT_STATUS_RETRY_CNT
))
1237 pr_err("%s: wait on port %d to go offline timed out\n",
1240 set_port_online(fc_regs
);
1241 if (!wait_port_online(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1242 FC_PORT_STATUS_RETRY_CNT
))
1243 pr_err("%s: wait on port %d to go online timed out\n",
1246 /* switch back to include this port */
1247 port_sel
|= (1 << port
);
1248 writeq_be(port_sel
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1249 cxlflash_afu_sync(afu
, 0, 0, AFU_GSYNC
);
1251 pr_debug("%s: returning port_sel=%lld\n", __func__
, port_sel
);
1255 * Asynchronous interrupt information table
1257 static const struct asyc_intr_info ainfo
[] = {
1258 {SISL_ASTATUS_FC0_OTHER
, "other error", 0, CLR_FC_ERROR
| LINK_RESET
},
1259 {SISL_ASTATUS_FC0_LOGO
, "target initiated LOGO", 0, 0},
1260 {SISL_ASTATUS_FC0_CRC_T
, "CRC threshold exceeded", 0, LINK_RESET
},
1261 {SISL_ASTATUS_FC0_LOGI_R
, "login timed out, retrying", 0, 0},
1262 {SISL_ASTATUS_FC0_LOGI_F
, "login failed", 0, CLR_FC_ERROR
},
1263 {SISL_ASTATUS_FC0_LOGI_S
, "login succeeded", 0, 0},
1264 {SISL_ASTATUS_FC0_LINK_DN
, "link down", 0, 0},
1265 {SISL_ASTATUS_FC0_LINK_UP
, "link up", 0, 0},
1266 {SISL_ASTATUS_FC1_OTHER
, "other error", 1, CLR_FC_ERROR
| LINK_RESET
},
1267 {SISL_ASTATUS_FC1_LOGO
, "target initiated LOGO", 1, 0},
1268 {SISL_ASTATUS_FC1_CRC_T
, "CRC threshold exceeded", 1, LINK_RESET
},
1269 {SISL_ASTATUS_FC1_LOGI_R
, "login timed out, retrying", 1, 0},
1270 {SISL_ASTATUS_FC1_LOGI_F
, "login failed", 1, CLR_FC_ERROR
},
1271 {SISL_ASTATUS_FC1_LOGI_S
, "login succeeded", 1, 0},
1272 {SISL_ASTATUS_FC1_LINK_DN
, "link down", 1, 0},
1273 {SISL_ASTATUS_FC1_LINK_UP
, "link up", 1, 0},
1274 {0x0, "", 0, 0} /* terminator */
1278 * find_ainfo() - locates and returns asynchronous interrupt information
1279 * @status: Status code set by AFU on error.
1281 * Return: The located information or NULL when the status code is invalid.
1283 static const struct asyc_intr_info
*find_ainfo(u64 status
)
1285 const struct asyc_intr_info
*info
;
1287 for (info
= &ainfo
[0]; info
->status
; info
++)
1288 if (info
->status
== status
)
1295 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1296 * @afu: AFU associated with the host.
1298 static void afu_err_intr_init(struct afu
*afu
)
1303 /* global async interrupts: AFU clears afu_ctrl on context exit
1304 * if async interrupts were sent to that context. This prevents
1305 * the AFU form sending further async interrupts when
1307 * nobody to receive them.
1311 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_mask
);
1312 /* set LISN# to send and point to master context */
1313 reg
= ((u64
) (((afu
->ctx_hndl
<< 8) | SISL_MSI_ASYNC_ERROR
)) << 40);
1315 if (afu
->internal_lun
)
1316 reg
|= 1; /* Bit 63 indicates local lun */
1317 writeq_be(reg
, &afu
->afu_map
->global
.regs
.afu_ctrl
);
1319 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_clear
);
1320 /* unmask bits that are of interest */
1321 /* note: afu can send an interrupt after this step */
1322 writeq_be(SISL_ASTATUS_MASK
, &afu
->afu_map
->global
.regs
.aintr_mask
);
1323 /* clear again in case a bit came on after previous clear but before */
1325 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_clear
);
1327 /* Clear/Set internal lun bits */
1328 reg
= readq_be(&afu
->afu_map
->global
.fc_regs
[0][FC_CONFIG2
/ 8]);
1329 reg
&= SISL_FC_INTERNAL_MASK
;
1330 if (afu
->internal_lun
)
1331 reg
|= ((u64
)(afu
->internal_lun
- 1) << SISL_FC_INTERNAL_SHIFT
);
1332 writeq_be(reg
, &afu
->afu_map
->global
.fc_regs
[0][FC_CONFIG2
/ 8]);
1334 /* now clear FC errors */
1335 for (i
= 0; i
< NUM_FC_PORTS
; i
++) {
1336 writeq_be(0xFFFFFFFFU
,
1337 &afu
->afu_map
->global
.fc_regs
[i
][FC_ERROR
/ 8]);
1338 writeq_be(0, &afu
->afu_map
->global
.fc_regs
[i
][FC_ERRCAP
/ 8]);
1341 /* sync interrupts for master's IOARRIN write */
1342 /* note that unlike asyncs, there can be no pending sync interrupts */
1343 /* at this time (this is a fresh context and master has not written */
1344 /* IOARRIN yet), so there is nothing to clear. */
1346 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1347 writeq_be(SISL_MSI_SYNC_ERROR
, &afu
->host_map
->ctx_ctrl
);
1348 writeq_be(SISL_ISTATUS_MASK
, &afu
->host_map
->intr_mask
);
1352 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1353 * @irq: Interrupt number.
1354 * @data: Private data provided at interrupt registration, the AFU.
1356 * Return: Always return IRQ_HANDLED.
1358 static irqreturn_t
cxlflash_sync_err_irq(int irq
, void *data
)
1360 struct afu
*afu
= (struct afu
*)data
;
1364 reg
= readq_be(&afu
->host_map
->intr_status
);
1365 reg_unmasked
= (reg
& SISL_ISTATUS_UNMASK
);
1367 if (reg_unmasked
== 0UL) {
1368 pr_err("%s: %llX: spurious interrupt, intr_status %016llX\n",
1369 __func__
, (u64
)afu
, reg
);
1370 goto cxlflash_sync_err_irq_exit
;
1373 pr_err("%s: %llX: unexpected interrupt, intr_status %016llX\n",
1374 __func__
, (u64
)afu
, reg
);
1376 writeq_be(reg_unmasked
, &afu
->host_map
->intr_clear
);
1378 cxlflash_sync_err_irq_exit
:
1379 pr_debug("%s: returning rc=%d\n", __func__
, IRQ_HANDLED
);
1384 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1385 * @irq: Interrupt number.
1386 * @data: Private data provided at interrupt registration, the AFU.
1388 * Return: Always return IRQ_HANDLED.
1390 static irqreturn_t
cxlflash_rrq_irq(int irq
, void *data
)
1392 struct afu
*afu
= (struct afu
*)data
;
1393 struct afu_cmd
*cmd
;
1394 bool toggle
= afu
->toggle
;
1396 *hrrq_start
= afu
->hrrq_start
,
1397 *hrrq_end
= afu
->hrrq_end
,
1398 *hrrq_curr
= afu
->hrrq_curr
;
1400 /* Process however many RRQ entries that are ready */
1404 if ((entry
& SISL_RESP_HANDLE_T_BIT
) != toggle
)
1407 cmd
= (struct afu_cmd
*)(entry
& ~SISL_RESP_HANDLE_T_BIT
);
1410 /* Advance to next entry or wrap and flip the toggle bit */
1411 if (hrrq_curr
< hrrq_end
)
1414 hrrq_curr
= hrrq_start
;
1415 toggle
^= SISL_RESP_HANDLE_T_BIT
;
1419 afu
->hrrq_curr
= hrrq_curr
;
1420 afu
->toggle
= toggle
;
1426 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1427 * @irq: Interrupt number.
1428 * @data: Private data provided at interrupt registration, the AFU.
1430 * Return: Always return IRQ_HANDLED.
1432 static irqreturn_t
cxlflash_async_err_irq(int irq
, void *data
)
1434 struct afu
*afu
= (struct afu
*)data
;
1435 struct cxlflash_cfg
*cfg
;
1437 const struct asyc_intr_info
*info
;
1438 struct sisl_global_map
*global
= &afu
->afu_map
->global
;
1445 reg
= readq_be(&global
->regs
.aintr_status
);
1446 reg_unmasked
= (reg
& SISL_ASTATUS_UNMASK
);
1448 if (reg_unmasked
== 0) {
1449 pr_err("%s: spurious interrupt, aintr_status 0x%016llX\n",
1454 /* it is OK to clear AFU status before FC_ERROR */
1455 writeq_be(reg_unmasked
, &global
->regs
.aintr_clear
);
1457 /* check each bit that is on */
1458 for (i
= 0; reg_unmasked
; i
++, reg_unmasked
= (reg_unmasked
>> 1)) {
1459 info
= find_ainfo(1ULL << i
);
1460 if ((reg_unmasked
& 0x1) || !info
)
1465 pr_err("%s: FC Port %d -> %s, fc_status 0x%08llX\n",
1466 __func__
, port
, info
->desc
,
1467 readq_be(&global
->fc_regs
[port
][FC_STATUS
/ 8]));
1470 * do link reset first, some OTHER errors will set FC_ERROR
1471 * again if cleared before or w/o a reset
1473 if (info
->action
& LINK_RESET
) {
1474 pr_err("%s: FC Port %d: resetting link\n",
1476 cfg
->lr_state
= LINK_RESET_REQUIRED
;
1477 cfg
->lr_port
= port
;
1478 schedule_work(&cfg
->work_q
);
1481 if (info
->action
& CLR_FC_ERROR
) {
1482 reg
= readq_be(&global
->fc_regs
[port
][FC_ERROR
/ 8]);
1485 * since all errors are unmasked, FC_ERROR and FC_ERRCAP
1486 * should be the same and tracing one is sufficient.
1489 pr_err("%s: fc %d: clearing fc_error 0x%08llX\n",
1490 __func__
, port
, reg
);
1492 writeq_be(reg
, &global
->fc_regs
[port
][FC_ERROR
/ 8]);
1493 writeq_be(0, &global
->fc_regs
[port
][FC_ERRCAP
/ 8]);
1498 pr_debug("%s: returning rc=%d, afu=%p\n", __func__
, IRQ_HANDLED
, afu
);
1503 * start_context() - starts the master context
1504 * @cxlflash: Internal structure associated with the host.
1506 * Return: A success or failure value from CXL services.
1508 static int start_context(struct cxlflash_cfg
*cfg
)
1512 rc
= cxl_start_context(cfg
->mcctx
,
1513 cfg
->afu
->work
.work_element_descriptor
,
1516 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1521 * read_vpd() - obtains the WWPNs from VPD
1522 * @cxlflash: Internal structure associated with the host.
1523 * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
1527 * -ENODEV when VPD or WWPN keywords not found
1529 static int read_vpd(struct cxlflash_cfg
*cfg
, u64 wwpn
[])
1531 struct pci_dev
*dev
= cfg
->parent_dev
;
1533 int ro_start
, ro_size
, i
, j
, k
;
1535 char vpd_data
[CXLFLASH_VPD_LEN
];
1536 char tmp_buf
[WWPN_BUF_LEN
] = { 0 };
1537 char *wwpn_vpd_tags
[NUM_FC_PORTS
] = { "V5", "V6" };
1539 /* Get the VPD data from the device */
1540 vpd_size
= pci_read_vpd(dev
, 0, sizeof(vpd_data
), vpd_data
);
1541 if (unlikely(vpd_size
<= 0)) {
1542 pr_err("%s: Unable to read VPD (size = %ld)\n",
1543 __func__
, vpd_size
);
1548 /* Get the read only section offset */
1549 ro_start
= pci_vpd_find_tag(vpd_data
, 0, vpd_size
,
1550 PCI_VPD_LRDT_RO_DATA
);
1551 if (unlikely(ro_start
< 0)) {
1552 pr_err("%s: VPD Read-only data not found\n", __func__
);
1557 /* Get the read only section size, cap when extends beyond read VPD */
1558 ro_size
= pci_vpd_lrdt_size(&vpd_data
[ro_start
]);
1560 i
= ro_start
+ PCI_VPD_LRDT_TAG_SIZE
;
1561 if (unlikely((i
+ j
) > vpd_size
)) {
1562 pr_debug("%s: Might need to read more VPD (%d > %ld)\n",
1563 __func__
, (i
+ j
), vpd_size
);
1564 ro_size
= vpd_size
- i
;
1568 * Find the offset of the WWPN tag within the read only
1569 * VPD data and validate the found field (partials are
1570 * no good to us). Convert the ASCII data to an integer
1571 * value. Note that we must copy to a temporary buffer
1572 * because the conversion service requires that the ASCII
1573 * string be terminated.
1575 for (k
= 0; k
< NUM_FC_PORTS
; k
++) {
1577 i
= ro_start
+ PCI_VPD_LRDT_TAG_SIZE
;
1579 i
= pci_vpd_find_info_keyword(vpd_data
, i
, j
, wwpn_vpd_tags
[k
]);
1580 if (unlikely(i
< 0)) {
1581 pr_err("%s: Port %d WWPN not found in VPD\n",
1587 j
= pci_vpd_info_field_size(&vpd_data
[i
]);
1588 i
+= PCI_VPD_INFO_FLD_HDR_SIZE
;
1589 if (unlikely((i
+ j
> vpd_size
) || (j
!= WWPN_LEN
))) {
1590 pr_err("%s: Port %d WWPN incomplete or VPD corrupt\n",
1596 memcpy(tmp_buf
, &vpd_data
[i
], WWPN_LEN
);
1597 rc
= kstrtoul(tmp_buf
, WWPN_LEN
, (ulong
*)&wwpn
[k
]);
1599 pr_err("%s: Fail to convert port %d WWPN to integer\n",
1607 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1612 * cxlflash_context_reset() - timeout handler for AFU commands
1613 * @cmd: AFU command that timed out.
1615 * Sends a reset to the AFU.
1617 void cxlflash_context_reset(struct afu_cmd
*cmd
)
1622 struct afu
*afu
= cmd
->parent
;
1625 pr_debug("%s: cmd=%p\n", __func__
, cmd
);
1627 spin_lock_irqsave(&cmd
->slock
, lock_flags
);
1629 /* Already completed? */
1630 if (cmd
->sa
.host_use_b
[0] & B_DONE
) {
1631 spin_unlock_irqrestore(&cmd
->slock
, lock_flags
);
1635 cmd
->sa
.host_use_b
[0] |= (B_DONE
| B_ERROR
| B_TIMEOUT
);
1636 spin_unlock_irqrestore(&cmd
->slock
, lock_flags
);
1639 * We really want to send this reset at all costs, so spread
1640 * out wait time on successive retries for available room.
1643 room
= readq_be(&afu
->host_map
->cmd_room
);
1644 atomic64_set(&afu
->room
, room
);
1648 } while (nretry
++ < MC_ROOM_RETRY_CNT
);
1650 pr_err("%s: no cmd_room to send reset\n", __func__
);
1655 writeq_be(rrin
, &afu
->host_map
->ioarrin
);
1657 rrin
= readq_be(&afu
->host_map
->ioarrin
);
1660 /* Double delay each time */
1662 } while (nretry
++ < MC_ROOM_RETRY_CNT
);
1666 * init_pcr() - initialize the provisioning and control registers
1667 * @cxlflash: Internal structure associated with the host.
1669 * Also sets up fast access to the mapped registers and initializes AFU
1670 * command fields that never change.
1672 void init_pcr(struct cxlflash_cfg
*cfg
)
1674 struct afu
*afu
= cfg
->afu
;
1675 struct sisl_ctrl_map
*ctrl_map
;
1678 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
1679 ctrl_map
= &afu
->afu_map
->ctrls
[i
].ctrl
;
1680 /* disrupt any clients that could be running */
1681 /* e. g. clients that survived a master restart */
1682 writeq_be(0, &ctrl_map
->rht_start
);
1683 writeq_be(0, &ctrl_map
->rht_cnt_id
);
1684 writeq_be(0, &ctrl_map
->ctx_cap
);
1687 /* copy frequently used fields into afu */
1688 afu
->ctx_hndl
= (u16
) cxl_process_element(cfg
->mcctx
);
1689 /* ctx_hndl is 16 bits in CAIA */
1690 afu
->host_map
= &afu
->afu_map
->hosts
[afu
->ctx_hndl
].host
;
1691 afu
->ctrl_map
= &afu
->afu_map
->ctrls
[afu
->ctx_hndl
].ctrl
;
1693 /* Program the Endian Control for the master context */
1694 writeq_be(SISL_ENDIAN_CTRL
, &afu
->host_map
->endian_ctrl
);
1696 /* initialize cmd fields that never change */
1697 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; i
++) {
1698 afu
->cmd
[i
].rcb
.ctx_id
= afu
->ctx_hndl
;
1699 afu
->cmd
[i
].rcb
.msi
= SISL_MSI_RRQ_UPDATED
;
1700 afu
->cmd
[i
].rcb
.rrq
= 0x0;
1705 * init_global() - initialize AFU global registers
1706 * @cxlflash: Internal structure associated with the host.
1708 int init_global(struct cxlflash_cfg
*cfg
)
1710 struct afu
*afu
= cfg
->afu
;
1711 u64 wwpn
[NUM_FC_PORTS
]; /* wwpn of AFU ports */
1712 int i
= 0, num_ports
= 0;
1716 rc
= read_vpd(cfg
, &wwpn
[0]);
1718 pr_err("%s: could not read vpd rc=%d\n", __func__
, rc
);
1722 pr_debug("%s: wwpn0=0x%llX wwpn1=0x%llX\n", __func__
, wwpn
[0], wwpn
[1]);
1724 /* set up RRQ in AFU for master issued cmds */
1725 writeq_be((u64
) afu
->hrrq_start
, &afu
->host_map
->rrq_start
);
1726 writeq_be((u64
) afu
->hrrq_end
, &afu
->host_map
->rrq_end
);
1728 /* AFU configuration */
1729 reg
= readq_be(&afu
->afu_map
->global
.regs
.afu_config
);
1730 reg
|= SISL_AFUCONF_AR_ALL
|SISL_AFUCONF_ENDIAN
;
1731 /* enable all auto retry options and control endianness */
1732 /* leave others at default: */
1733 /* CTX_CAP write protected, mbox_r does not clear on read and */
1734 /* checker on if dual afu */
1735 writeq_be(reg
, &afu
->afu_map
->global
.regs
.afu_config
);
1737 /* global port select: select either port */
1738 if (afu
->internal_lun
) {
1739 /* only use port 0 */
1740 writeq_be(PORT0
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1741 num_ports
= NUM_FC_PORTS
- 1;
1743 writeq_be(BOTH_PORTS
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1744 num_ports
= NUM_FC_PORTS
;
1747 for (i
= 0; i
< num_ports
; i
++) {
1748 /* unmask all errors (but they are still masked at AFU) */
1749 writeq_be(0, &afu
->afu_map
->global
.fc_regs
[i
][FC_ERRMSK
/ 8]);
1750 /* clear CRC error cnt & set a threshold */
1751 (void)readq_be(&afu
->afu_map
->global
.
1752 fc_regs
[i
][FC_CNT_CRCERR
/ 8]);
1753 writeq_be(MC_CRC_THRESH
, &afu
->afu_map
->global
.fc_regs
[i
]
1754 [FC_CRC_THRESH
/ 8]);
1756 /* set WWPNs. If already programmed, wwpn[i] is 0 */
1758 afu_set_wwpn(afu
, i
,
1759 &afu
->afu_map
->global
.fc_regs
[i
][0],
1761 pr_err("%s: failed to set WWPN on port %d\n",
1766 /* Programming WWPN back to back causes additional
1767 * offline/online transitions and a PLOGI
1773 /* set up master's own CTX_CAP to allow real mode, host translation */
1774 /* tbls, afu cmds and read/write GSCSI cmds. */
1775 /* First, unlock ctx_cap write by reading mbox */
1776 (void)readq_be(&afu
->ctrl_map
->mbox_r
); /* unlock ctx_cap */
1777 writeq_be((SISL_CTX_CAP_REAL_MODE
| SISL_CTX_CAP_HOST_XLATE
|
1778 SISL_CTX_CAP_READ_CMD
| SISL_CTX_CAP_WRITE_CMD
|
1779 SISL_CTX_CAP_AFU_CMD
| SISL_CTX_CAP_GSCSI_CMD
),
1780 &afu
->ctrl_map
->ctx_cap
);
1781 /* init heartbeat */
1782 afu
->hb
= readq_be(&afu
->afu_map
->global
.regs
.afu_hb
);
1789 * start_afu() - initializes and starts the AFU
1790 * @cxlflash: Internal structure associated with the host.
1792 static int start_afu(struct cxlflash_cfg
*cfg
)
1794 struct afu
*afu
= cfg
->afu
;
1795 struct afu_cmd
*cmd
;
1800 for (i
= 0; i
< CXLFLASH_NUM_CMDS
; i
++) {
1803 init_completion(&cmd
->cevent
);
1804 spin_lock_init(&cmd
->slock
);
1810 /* initialize RRQ pointers */
1811 afu
->hrrq_start
= &afu
->rrq_entry
[0];
1812 afu
->hrrq_end
= &afu
->rrq_entry
[NUM_RRQ_ENTRY
- 1];
1813 afu
->hrrq_curr
= afu
->hrrq_start
;
1816 rc
= init_global(cfg
);
1818 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1823 * init_mc() - create and register as the master context
1824 * @cxlflash: Internal structure associated with the host.
1828 * -ENOMEM when unable to obtain a context from CXL services
1829 * A failure value from CXL services.
1831 static int init_mc(struct cxlflash_cfg
*cfg
)
1833 struct cxl_context
*ctx
;
1834 struct device
*dev
= &cfg
->dev
->dev
;
1835 struct afu
*afu
= cfg
->afu
;
1837 enum undo_level level
;
1839 ctx
= cxl_get_context(cfg
->dev
);
1844 /* Set it up as a master with the CXL */
1845 cxl_set_master(ctx
);
1847 /* During initialization reset the AFU to start from a clean slate */
1848 rc
= cxl_afu_reset(cfg
->mcctx
);
1850 dev_err(dev
, "%s: initial AFU reset failed rc=%d\n",
1852 level
= RELEASE_CONTEXT
;
1856 rc
= cxl_allocate_afu_irqs(ctx
, 3);
1858 dev_err(dev
, "%s: call to allocate_afu_irqs failed rc=%d!\n",
1860 level
= RELEASE_CONTEXT
;
1864 rc
= cxl_map_afu_irq(ctx
, 1, cxlflash_sync_err_irq
, afu
,
1865 "SISL_MSI_SYNC_ERROR");
1866 if (unlikely(rc
<= 0)) {
1867 dev_err(dev
, "%s: IRQ 1 (SISL_MSI_SYNC_ERROR) map failed!\n",
1873 rc
= cxl_map_afu_irq(ctx
, 2, cxlflash_rrq_irq
, afu
,
1874 "SISL_MSI_RRQ_UPDATED");
1875 if (unlikely(rc
<= 0)) {
1876 dev_err(dev
, "%s: IRQ 2 (SISL_MSI_RRQ_UPDATED) map failed!\n",
1882 rc
= cxl_map_afu_irq(ctx
, 3, cxlflash_async_err_irq
, afu
,
1883 "SISL_MSI_ASYNC_ERROR");
1884 if (unlikely(rc
<= 0)) {
1885 dev_err(dev
, "%s: IRQ 3 (SISL_MSI_ASYNC_ERROR) map failed!\n",
1893 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1894 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1895 * element (pe) that is embedded in the context (ctx)
1897 rc
= start_context(cfg
);
1899 dev_err(dev
, "%s: start context failed rc=%d\n", __func__
, rc
);
1900 level
= UNMAP_THREE
;
1904 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1907 term_mc(cfg
, level
);
1912 * init_afu() - setup as master context and start AFU
1913 * @cxlflash: Internal structure associated with the host.
1915 * This routine is a higher level of control for configuring the
1916 * AFU on probe and reset paths.
1920 * -ENOMEM when unable to map the AFU MMIO space
1921 * A failure value from internal services.
1923 static int init_afu(struct cxlflash_cfg
*cfg
)
1927 struct afu
*afu
= cfg
->afu
;
1928 struct device
*dev
= &cfg
->dev
->dev
;
1930 cxl_perst_reloads_same_image(cfg
->cxl_afu
, true);
1934 dev_err(dev
, "%s: call to init_mc failed, rc=%d!\n",
1939 /* Map the entire MMIO space of the AFU.
1941 afu
->afu_map
= cxl_psa_map(cfg
->mcctx
);
1942 if (!afu
->afu_map
) {
1944 term_mc(cfg
, UNDO_START
);
1945 dev_err(dev
, "%s: call to cxl_psa_map failed!\n", __func__
);
1949 /* don't byte reverse on reading afu_version, else the string form */
1950 /* will be backwards */
1951 reg
= afu
->afu_map
->global
.regs
.afu_version
;
1952 memcpy(afu
->version
, ®
, 8);
1953 afu
->interface_version
=
1954 readq_be(&afu
->afu_map
->global
.regs
.interface_version
);
1955 pr_debug("%s: afu version %s, interface version 0x%llX\n",
1956 __func__
, afu
->version
, afu
->interface_version
);
1958 rc
= start_afu(cfg
);
1960 dev_err(dev
, "%s: call to start_afu failed, rc=%d!\n",
1962 term_mc(cfg
, UNDO_START
);
1963 cxl_psa_unmap((void *)afu
->afu_map
);
1964 afu
->afu_map
= NULL
;
1968 afu_err_intr_init(cfg
->afu
);
1969 atomic64_set(&afu
->room
, readq_be(&afu
->host_map
->cmd_room
));
1972 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
1977 * cxlflash_send_cmd() - sends an AFU command
1978 * @afu: AFU associated with the host.
1979 * @cmd: AFU command to send.
1985 int cxlflash_send_cmd(struct afu
*afu
, struct afu_cmd
*cmd
)
1987 struct cxlflash_cfg
*cfg
= afu
->parent
;
1994 * This routine is used by critical users such an AFU sync and to
1995 * send a task management function (TMF). Thus we want to retry a
1996 * bit before returning an error. To avoid the performance penalty
1997 * of MMIO, we spread the update of 'room' over multiple commands.
2000 newval
= atomic64_dec_if_positive(&afu
->room
);
2003 room
= readq_be(&afu
->host_map
->cmd_room
);
2004 atomic64_set(&afu
->room
, room
);
2008 } while (nretry
++ < MC_ROOM_RETRY_CNT
);
2010 pr_err("%s: no cmd_room to send 0x%X\n",
2011 __func__
, cmd
->rcb
.cdb
[0]);
2014 } else if (unlikely(newval
< 0)) {
2015 /* This should be rare. i.e. Only if two threads race and
2016 * decrement before the MMIO read is done. In this case
2017 * just benefit from the other thread having updated
2020 if (nretry
++ < MC_ROOM_RETRY_CNT
) {
2029 writeq_be((u64
)&cmd
->rcb
, &afu
->host_map
->ioarrin
);
2031 pr_debug("%s: cmd=%p len=%d ea=%p rc=%d\n", __func__
, cmd
,
2032 cmd
->rcb
.data_len
, (void *)cmd
->rcb
.data_ea
, rc
);
2036 afu
->read_room
= true;
2037 schedule_work(&cfg
->work_q
);
2038 rc
= SCSI_MLQUEUE_HOST_BUSY
;
2043 * cxlflash_wait_resp() - polls for a response or timeout to a sent AFU command
2044 * @afu: AFU associated with the host.
2045 * @cmd: AFU command that was sent.
2047 void cxlflash_wait_resp(struct afu
*afu
, struct afu_cmd
*cmd
)
2049 ulong timeout
= jiffies
+ (cmd
->rcb
.timeout
* 2 * HZ
);
2051 timeout
= wait_for_completion_timeout(&cmd
->cevent
, timeout
);
2053 cxlflash_context_reset(cmd
);
2055 if (unlikely(cmd
->sa
.ioasc
!= 0))
2056 pr_err("%s: CMD 0x%X failed, IOASC: flags 0x%X, afu_rc 0x%X, "
2057 "scsi_rc 0x%X, fc_rc 0x%X\n", __func__
, cmd
->rcb
.cdb
[0],
2058 cmd
->sa
.rc
.flags
, cmd
->sa
.rc
.afu_rc
, cmd
->sa
.rc
.scsi_rc
,
2063 * cxlflash_afu_sync() - builds and sends an AFU sync command
2064 * @afu: AFU associated with the host.
2065 * @ctx_hndl_u: Identifies context requesting sync.
2066 * @res_hndl_u: Identifies resource requesting sync.
2067 * @mode: Type of sync to issue (lightweight, heavyweight, global).
2069 * The AFU can only take 1 sync command at a time. This routine enforces this
2070 * limitation by using a mutex to provide exlusive access to the AFU during
2071 * the sync. This design point requires calling threads to not be on interrupt
2072 * context due to the possibility of sleeping during concurrent sync operations.
2074 * AFU sync operations are only necessary and allowed when the device is
2075 * operating normally. When not operating normally, sync requests can occur as
2076 * part of cleaning up resources associated with an adapter prior to removal.
2077 * In this scenario, these requests are simply ignored (safe due to the AFU
2084 int cxlflash_afu_sync(struct afu
*afu
, ctx_hndl_t ctx_hndl_u
,
2085 res_hndl_t res_hndl_u
, u8 mode
)
2087 struct cxlflash_cfg
*cfg
= afu
->parent
;
2088 struct afu_cmd
*cmd
= NULL
;
2091 static DEFINE_MUTEX(sync_active
);
2093 if (cfg
->state
!= STATE_NORMAL
) {
2094 pr_debug("%s: Sync not required! (%u)\n", __func__
, cfg
->state
);
2098 mutex_lock(&sync_active
);
2100 cmd
= cxlflash_cmd_checkout(afu
);
2101 if (unlikely(!cmd
)) {
2103 udelay(1000 * retry_cnt
);
2104 if (retry_cnt
< MC_RETRY_CNT
)
2106 pr_err("%s: could not get a free command\n", __func__
);
2111 pr_debug("%s: afu=%p cmd=%p %d\n", __func__
, afu
, cmd
, ctx_hndl_u
);
2113 memset(cmd
->rcb
.cdb
, 0, sizeof(cmd
->rcb
.cdb
));
2115 cmd
->rcb
.req_flags
= SISL_REQ_FLAGS_AFU_CMD
;
2116 cmd
->rcb
.port_sel
= 0x0; /* NA */
2117 cmd
->rcb
.lun_id
= 0x0; /* NA */
2118 cmd
->rcb
.data_len
= 0x0;
2119 cmd
->rcb
.data_ea
= 0x0;
2120 cmd
->rcb
.timeout
= MC_AFU_SYNC_TIMEOUT
;
2122 cmd
->rcb
.cdb
[0] = 0xC0; /* AFU Sync */
2123 cmd
->rcb
.cdb
[1] = mode
;
2125 /* The cdb is aligned, no unaligned accessors required */
2126 *((u16
*)&cmd
->rcb
.cdb
[2]) = swab16(ctx_hndl_u
);
2127 *((u32
*)&cmd
->rcb
.cdb
[4]) = swab32(res_hndl_u
);
2129 rc
= cxlflash_send_cmd(afu
, cmd
);
2133 cxlflash_wait_resp(afu
, cmd
);
2135 /* set on timeout */
2136 if (unlikely((cmd
->sa
.ioasc
!= 0) ||
2137 (cmd
->sa
.host_use_b
[0] & B_ERROR
)))
2140 mutex_unlock(&sync_active
);
2142 cxlflash_cmd_checkin(cmd
);
2143 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
2148 * cxlflash_afu_reset() - resets the AFU
2149 * @cxlflash: Internal structure associated with the host.
2153 * A failure value from internal services.
2155 int cxlflash_afu_reset(struct cxlflash_cfg
*cfg
)
2158 /* Stop the context before the reset. Since the context is
2159 * no longer available restart it after the reset is complete
2166 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
2171 * cxlflash_worker_thread() - work thread handler for the AFU
2172 * @work: Work structure contained within cxlflash associated with host.
2174 * Handles the following events:
2175 * - Link reset which cannot be performed on interrupt context due to
2176 * blocking up to a few seconds
2177 * - Read AFU command room
2179 static void cxlflash_worker_thread(struct work_struct
*work
)
2181 struct cxlflash_cfg
*cfg
= container_of(work
, struct cxlflash_cfg
,
2183 struct afu
*afu
= cfg
->afu
;
2187 /* Avoid MMIO if the device has failed */
2189 if (cfg
->state
!= STATE_NORMAL
)
2192 spin_lock_irqsave(cfg
->host
->host_lock
, lock_flags
);
2194 if (cfg
->lr_state
== LINK_RESET_REQUIRED
) {
2195 port
= cfg
->lr_port
;
2197 pr_err("%s: invalid port index %d\n", __func__
, port
);
2199 spin_unlock_irqrestore(cfg
->host
->host_lock
,
2202 /* The reset can block... */
2203 afu_link_reset(afu
, port
,
2205 global
.fc_regs
[port
][0]);
2206 spin_lock_irqsave(cfg
->host
->host_lock
, lock_flags
);
2209 cfg
->lr_state
= LINK_RESET_COMPLETE
;
2212 if (afu
->read_room
) {
2213 atomic64_set(&afu
->room
, readq_be(&afu
->host_map
->cmd_room
));
2214 afu
->read_room
= false;
2217 spin_unlock_irqrestore(cfg
->host
->host_lock
, lock_flags
);
2221 * cxlflash_probe() - PCI entry point to add host
2222 * @pdev: PCI device associated with the host.
2223 * @dev_id: PCI device id associated with device.
2225 * Return: 0 on success / non-zero on failure
2227 static int cxlflash_probe(struct pci_dev
*pdev
,
2228 const struct pci_device_id
*dev_id
)
2230 struct Scsi_Host
*host
;
2231 struct cxlflash_cfg
*cfg
= NULL
;
2232 struct device
*phys_dev
;
2233 struct dev_dependent_vals
*ddv
;
2236 dev_dbg(&pdev
->dev
, "%s: Found CXLFLASH with IRQ: %d\n",
2237 __func__
, pdev
->irq
);
2239 ddv
= (struct dev_dependent_vals
*)dev_id
->driver_data
;
2240 driver_template
.max_sectors
= ddv
->max_sectors
;
2242 host
= scsi_host_alloc(&driver_template
, sizeof(struct cxlflash_cfg
));
2244 dev_err(&pdev
->dev
, "%s: call to scsi_host_alloc failed!\n",
2250 host
->max_id
= CXLFLASH_MAX_NUM_TARGETS_PER_BUS
;
2251 host
->max_lun
= CXLFLASH_MAX_NUM_LUNS_PER_TARGET
;
2252 host
->max_channel
= NUM_FC_PORTS
- 1;
2253 host
->unique_id
= host
->host_no
;
2254 host
->max_cmd_len
= CXLFLASH_MAX_CDB_LEN
;
2256 cfg
= (struct cxlflash_cfg
*)host
->hostdata
;
2258 rc
= alloc_mem(cfg
);
2260 dev_err(&pdev
->dev
, "%s: call to scsi_host_alloc failed!\n",
2266 cfg
->init_state
= INIT_STATE_NONE
;
2268 cfg
->dev_id
= (struct pci_device_id
*)dev_id
;
2271 init_waitqueue_head(&cfg
->tmf_waitq
);
2272 init_waitqueue_head(&cfg
->limbo_waitq
);
2274 INIT_WORK(&cfg
->work_q
, cxlflash_worker_thread
);
2275 cfg
->lr_state
= LINK_RESET_INVALID
;
2278 pci_set_drvdata(pdev
, cfg
);
2280 /* Use the special service provided to look up the physical
2281 * PCI device, since we are called on the probe of the virtual
2282 * PCI host bus (vphb)
2284 phys_dev
= cxl_get_phys_dev(pdev
);
2285 if (!dev_is_pci(phys_dev
)) {
2286 pr_err("%s: not a pci dev\n", __func__
);
2290 cfg
->parent_dev
= to_pci_dev(phys_dev
);
2292 cfg
->cxl_afu
= cxl_pci_to_afu(pdev
);
2296 dev_err(&pdev
->dev
, "%s: call to init_pci "
2297 "failed rc=%d!\n", __func__
, rc
);
2300 cfg
->init_state
= INIT_STATE_PCI
;
2304 dev_err(&pdev
->dev
, "%s: call to init_afu "
2305 "failed rc=%d!\n", __func__
, rc
);
2308 cfg
->init_state
= INIT_STATE_AFU
;
2311 rc
= init_scsi(cfg
);
2313 dev_err(&pdev
->dev
, "%s: call to init_scsi "
2314 "failed rc=%d!\n", __func__
, rc
);
2317 cfg
->init_state
= INIT_STATE_SCSI
;
2320 pr_debug("%s: returning rc=%d\n", __func__
, rc
);
2324 cxlflash_remove(pdev
);
2329 * cxlflash_pci_error_detected() - called when a PCI error is detected
2330 * @pdev: PCI device struct.
2331 * @state: PCI channel state.
2333 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2335 static pci_ers_result_t
cxlflash_pci_error_detected(struct pci_dev
*pdev
,
2336 pci_channel_state_t state
)
2338 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
2339 struct device
*dev
= &cfg
->dev
->dev
;
2341 dev_dbg(dev
, "%s: pdev=%p state=%u\n", __func__
, pdev
, state
);
2344 case pci_channel_io_frozen
:
2345 cfg
->state
= STATE_LIMBO
;
2347 /* Turn off legacy I/O */
2348 scsi_block_requests(cfg
->host
);
2350 term_mc(cfg
, UNDO_START
);
2353 return PCI_ERS_RESULT_NEED_RESET
;
2354 case pci_channel_io_perm_failure
:
2355 cfg
->state
= STATE_FAILTERM
;
2356 wake_up_all(&cfg
->limbo_waitq
);
2357 scsi_unblock_requests(cfg
->host
);
2358 return PCI_ERS_RESULT_DISCONNECT
;
2362 return PCI_ERS_RESULT_NEED_RESET
;
2366 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2367 * @pdev: PCI device struct.
2369 * This routine is called by the pci error recovery code after the PCI
2370 * slot has been reset, just before we should resume normal operations.
2372 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2374 static pci_ers_result_t
cxlflash_pci_slot_reset(struct pci_dev
*pdev
)
2377 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
2378 struct device
*dev
= &cfg
->dev
->dev
;
2380 dev_dbg(dev
, "%s: pdev=%p\n", __func__
, pdev
);
2384 dev_err(dev
, "%s: EEH recovery failed! (%d)\n", __func__
, rc
);
2385 return PCI_ERS_RESULT_DISCONNECT
;
2388 return PCI_ERS_RESULT_RECOVERED
;
2392 * cxlflash_pci_resume() - called when normal operation can resume
2393 * @pdev: PCI device struct
2395 static void cxlflash_pci_resume(struct pci_dev
*pdev
)
2397 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
2398 struct device
*dev
= &cfg
->dev
->dev
;
2400 dev_dbg(dev
, "%s: pdev=%p\n", __func__
, pdev
);
2402 cfg
->state
= STATE_NORMAL
;
2403 wake_up_all(&cfg
->limbo_waitq
);
2404 scsi_unblock_requests(cfg
->host
);
2407 static const struct pci_error_handlers cxlflash_err_handler
= {
2408 .error_detected
= cxlflash_pci_error_detected
,
2409 .slot_reset
= cxlflash_pci_slot_reset
,
2410 .resume
= cxlflash_pci_resume
,
2414 * PCI device structure
2416 static struct pci_driver cxlflash_driver
= {
2417 .name
= CXLFLASH_NAME
,
2418 .id_table
= cxlflash_pci_table
,
2419 .probe
= cxlflash_probe
,
2420 .remove
= cxlflash_remove
,
2421 .err_handler
= &cxlflash_err_handler
,
2425 * init_cxlflash() - module entry point
2427 * Return: 0 on success / non-zero on failure
2429 static int __init
init_cxlflash(void)
2431 pr_info("%s: IBM Power CXL Flash Adapter: %s\n",
2432 __func__
, CXLFLASH_DRIVER_DATE
);
2434 return pci_register_driver(&cxlflash_driver
);
2438 * exit_cxlflash() - module exit point
2440 static void __exit
exit_cxlflash(void)
2442 pci_unregister_driver(&cxlflash_driver
);
2445 module_init(init_cxlflash
);
2446 module_exit(exit_cxlflash
);