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;
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(&cfg->dev->dev, "%s: device in limbo!\n",
388 rc = SCSI_MLQUEUE_HOST_BUSY;
391 dev_dbg_ratelimited(&cfg->dev->dev, "%s: device has failed!\n",
393 scp->result = (DID_NO_CONNECT << 16);
401 cmd = cxlflash_cmd_checkout(afu);
402 if (unlikely(!cmd)) {
403 pr_err("%s: could not get a free command\n", __func__);
404 rc = SCSI_MLQUEUE_HOST_BUSY;
408 cmd->rcb.ctx_id = afu->ctx_hndl;
409 cmd->rcb.port_sel = port_sel;
410 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
412 if (scp->sc_data_direction == DMA_TO_DEVICE)
413 lflag = SISL_REQ_FLAGS_HOST_WRITE;
415 lflag = SISL_REQ_FLAGS_HOST_READ;
417 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
418 SISL_REQ_FLAGS_SUP_UNDERRUN | lflag);
420 /* Stash the scp in the reserved field, for reuse during interrupt */
423 nseg = scsi_dma_map(scp);
424 if (unlikely(nseg < 0)) {
425 dev_err(&pdev->dev, "%s: Fail DMA map! nseg=%d\n",
427 rc = SCSI_MLQUEUE_HOST_BUSY;
431 ncount = scsi_sg_count(scp);
432 scsi_for_each_sg(scp, sg, ncount, i) {
433 cmd->rcb.data_len = sg_dma_len(sg);
434 cmd->rcb.data_ea = sg_dma_address(sg);
437 /* Copy the CDB from the scsi_cmnd passed in */
438 memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
440 /* Send the command */
441 rc = cxlflash_send_cmd(afu, cmd);
443 cxlflash_cmd_checkin(cmd);
452 * cxlflash_eh_device_reset_handler() - reset a single LUN
453 * @scp: SCSI command to send.
456 * SUCCESS as defined in scsi/scsi.h
457 * FAILED as defined in scsi/scsi.h
459 static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
462 struct Scsi_Host *host = scp->device->host;
463 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
464 struct afu *afu = cfg->afu;
467 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
468 "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
469 host->host_no, scp->device->channel,
470 scp->device->id, scp->device->lun,
471 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
472 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
473 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
474 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
476 switch (cfg->state) {
478 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
483 wait_event(cfg->limbo_waitq, cfg->state != STATE_LIMBO);
484 if (cfg->state == STATE_NORMAL)
492 pr_debug("%s: returning rc=%d\n", __func__, rc);
497 * cxlflash_eh_host_reset_handler() - reset the host adapter
498 * @scp: SCSI command from stack identifying host.
501 * SUCCESS as defined in scsi/scsi.h
502 * FAILED as defined in scsi/scsi.h
504 static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
508 struct Scsi_Host *host = scp->device->host;
509 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
511 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
512 "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
513 host->host_no, scp->device->channel,
514 scp->device->id, scp->device->lun,
515 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
516 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
517 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
518 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
520 switch (cfg->state) {
522 cfg->state = STATE_LIMBO;
523 scsi_block_requests(cfg->host);
524 cxlflash_mark_contexts_error(cfg);
525 rcr = cxlflash_afu_reset(cfg);
528 cfg->state = STATE_FAILTERM;
530 cfg->state = STATE_NORMAL;
531 wake_up_all(&cfg->limbo_waitq);
532 scsi_unblock_requests(cfg->host);
535 wait_event(cfg->limbo_waitq, cfg->state != STATE_LIMBO);
536 if (cfg->state == STATE_NORMAL)
544 pr_debug("%s: returning rc=%d\n", __func__, rc);
549 * cxlflash_change_queue_depth() - change the queue depth for the device
550 * @sdev: SCSI device destined for queue depth change.
551 * @qdepth: Requested queue depth value to set.
553 * The requested queue depth is capped to the maximum supported value.
555 * Return: The actual queue depth set.
557 static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
560 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
561 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
563 scsi_change_queue_depth(sdev, qdepth);
564 return sdev->queue_depth;
568 * cxlflash_show_port_status() - queries and presents the current port status
569 * @dev: Generic device associated with the host owning the port.
570 * @attr: Device attribute representing the port.
571 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
573 * Return: The size of the ASCII string returned in @buf.
575 static ssize_t cxlflash_show_port_status(struct device *dev,
576 struct device_attribute *attr,
579 struct Scsi_Host *shost = class_to_shost(dev);
580 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
581 struct afu *afu = cfg->afu;
589 rc = kstrtouint((attr->attr.name + 4), 10, &port);
590 if (rc || (port >= NUM_FC_PORTS))
593 fc_regs = &afu->afu_map->global.fc_regs[port][0];
595 (readq_be(&fc_regs[FC_MTIP_STATUS / 8]) & FC_MTIP_STATUS_MASK);
597 if (status == FC_MTIP_STATUS_ONLINE)
598 disp_status = "online";
599 else if (status == FC_MTIP_STATUS_OFFLINE)
600 disp_status = "offline";
602 disp_status = "unknown";
604 return snprintf(buf, PAGE_SIZE, "%s\n", disp_status);
608 * cxlflash_show_lun_mode() - presents the current LUN mode of the host
609 * @dev: Generic device associated with the host.
610 * @attr: Device attribute representing the lun mode.
611 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
613 * Return: The size of the ASCII string returned in @buf.
615 static ssize_t cxlflash_show_lun_mode(struct device *dev,
616 struct device_attribute *attr, char *buf)
618 struct Scsi_Host *shost = class_to_shost(dev);
619 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
620 struct afu *afu = cfg->afu;
622 return snprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
626 * cxlflash_store_lun_mode() - sets the LUN mode of the host
627 * @dev: Generic device associated with the host.
628 * @attr: Device attribute representing the lun mode.
629 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
630 * @count: Length of data resizing in @buf.
632 * The CXL Flash AFU supports a dummy LUN mode where the external
633 * links and storage are not required. Space on the FPGA is used
634 * to create 1 or 2 small LUNs which are presented to the system
635 * as if they were a normal storage device. This feature is useful
636 * during development and also provides manufacturing with a way
637 * to test the AFU without an actual device.
639 * 0 = external LUN[s] (default)
640 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
641 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
642 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
643 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
645 * Return: The size of the ASCII string returned in @buf.
647 static ssize_t cxlflash_store_lun_mode(struct device *dev,
648 struct device_attribute *attr,
649 const char *buf, size_t count)
651 struct Scsi_Host *shost = class_to_shost(dev);
652 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
653 struct afu *afu = cfg->afu;
657 rc = kstrtouint(buf, 10, &lun_mode);
658 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
659 afu->internal_lun = lun_mode;
660 cxlflash_afu_reset(cfg);
661 scsi_scan_host(cfg->host);
668 * cxlflash_show_ioctl_version() - presents the current ioctl version of the host
669 * @dev: Generic device associated with the host.
670 * @attr: Device attribute representing the ioctl version.
671 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
673 * Return: The size of the ASCII string returned in @buf.
675 static ssize_t cxlflash_show_ioctl_version(struct device *dev,
676 struct device_attribute *attr,
679 return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
683 * cxlflash_show_dev_mode() - presents the current mode of the device
684 * @dev: Generic device associated with the device.
685 * @attr: Device attribute representing the device mode.
686 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
688 * Return: The size of the ASCII string returned in @buf.
690 static ssize_t cxlflash_show_dev_mode(struct device *dev,
691 struct device_attribute *attr, char *buf)
693 struct scsi_device *sdev = to_scsi_device(dev);
695 return snprintf(buf, PAGE_SIZE, "%s\n",
696 sdev->hostdata ? "superpipe" : "legacy");
700 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
701 * @cxlflash: Internal structure associated with the host.
703 static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
705 struct pci_dev *pdev = cfg->dev;
707 if (pci_channel_offline(pdev))
708 wait_event_timeout(cfg->limbo_waitq,
709 !pci_channel_offline(pdev),
710 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
716 static DEVICE_ATTR(port0, S_IRUGO, cxlflash_show_port_status, NULL);
717 static DEVICE_ATTR(port1, S_IRUGO, cxlflash_show_port_status, NULL);
718 static DEVICE_ATTR(lun_mode, S_IRUGO | S_IWUSR, cxlflash_show_lun_mode,
719 cxlflash_store_lun_mode);
720 static DEVICE_ATTR(ioctl_version, S_IRUGO, cxlflash_show_ioctl_version, NULL);
722 static struct device_attribute *cxlflash_host_attrs[] = {
726 &dev_attr_ioctl_version,
733 static DEVICE_ATTR(mode, S_IRUGO, cxlflash_show_dev_mode, NULL);
735 static struct device_attribute *cxlflash_dev_attrs[] = {
743 static struct scsi_host_template driver_template = {
744 .module = THIS_MODULE,
745 .name = CXLFLASH_ADAPTER_NAME,
746 .info = cxlflash_driver_info,
747 .ioctl = cxlflash_ioctl,
748 .proc_name = CXLFLASH_NAME,
749 .queuecommand = cxlflash_queuecommand,
750 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
751 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
752 .change_queue_depth = cxlflash_change_queue_depth,
754 .can_queue = CXLFLASH_MAX_CMDS,
756 .sg_tablesize = SG_NONE, /* No scatter gather support. */
757 .max_sectors = CXLFLASH_MAX_SECTORS,
758 .use_clustering = ENABLE_CLUSTERING,
759 .shost_attrs = cxlflash_host_attrs,
760 .sdev_attrs = cxlflash_dev_attrs,
764 * Device dependent values
766 static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS };
769 * PCI device binding table
771 static struct pci_device_id cxlflash_pci_table[] = {
772 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
773 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
777 MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
780 * free_mem() - free memory associated with the AFU
781 * @cxlflash: Internal structure associated with the host.
783 static void free_mem(struct cxlflash_cfg *cfg)
787 struct afu *afu = cfg->afu;
790 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
791 buf = afu->cmd[i].buf;
792 if (!((u64)buf & (PAGE_SIZE - 1)))
793 free_page((ulong)buf);
796 free_pages((ulong)afu, get_order(sizeof(struct afu)));
802 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
803 * @cxlflash: Internal structure associated with the host.
805 * Safe to call with AFU in a partially allocated/initialized state.
807 static void stop_afu(struct cxlflash_cfg *cfg)
810 struct afu *afu = cfg->afu;
813 for (i = 0; i < CXLFLASH_NUM_CMDS; i++)
814 complete(&afu->cmd[i].cevent);
816 if (likely(afu->afu_map)) {
817 cxl_psa_unmap((void *)afu->afu_map);
824 * term_mc() - terminates the master context
825 * @cxlflash: Internal structure associated with the host.
826 * @level: Depth of allocation, where to begin waterfall tear down.
828 * Safe to call with AFU/MC in partially allocated/initialized state.
830 static void term_mc(struct cxlflash_cfg *cfg, enum undo_level level)
833 struct afu *afu = cfg->afu;
835 if (!afu || !cfg->mcctx) {
836 pr_err("%s: returning from term_mc with NULL afu or MC\n",
843 rc = cxl_stop_context(cfg->mcctx);
846 cxl_unmap_afu_irq(cfg->mcctx, 3, afu);
848 cxl_unmap_afu_irq(cfg->mcctx, 2, afu);
850 cxl_unmap_afu_irq(cfg->mcctx, 1, afu);
852 cxl_free_afu_irqs(cfg->mcctx);
853 case RELEASE_CONTEXT:
859 * term_afu() - terminates the AFU
860 * @cxlflash: Internal structure associated with the host.
862 * Safe to call with AFU/MC in partially allocated/initialized state.
864 static void term_afu(struct cxlflash_cfg *cfg)
866 term_mc(cfg, UNDO_START);
871 pr_debug("%s: returning\n", __func__);
875 * cxlflash_remove() - PCI entry point to tear down host
876 * @pdev: PCI device associated with the host.
878 * Safe to use as a cleanup in partially allocated/initialized state.
880 static void cxlflash_remove(struct pci_dev *pdev)
882 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
885 /* If a Task Management Function is active, wait for it to complete
886 * before continuing with remove.
888 spin_lock_irqsave(&cfg->tmf_waitq.lock, lock_flags);
890 wait_event_interruptible_locked_irq(cfg->tmf_waitq,
892 spin_unlock_irqrestore(&cfg->tmf_waitq.lock, lock_flags);
894 cfg->state = STATE_FAILTERM;
895 cxlflash_stop_term_user_contexts(cfg);
897 switch (cfg->init_state) {
898 case INIT_STATE_SCSI:
899 cxlflash_term_local_luns(cfg);
900 scsi_remove_host(cfg->host);
901 scsi_host_put(cfg->host);
906 pci_release_regions(cfg->dev);
907 pci_disable_device(pdev);
908 case INIT_STATE_NONE:
909 flush_work(&cfg->work_q);
914 pr_debug("%s: returning\n", __func__);
918 * alloc_mem() - allocates the AFU and its command pool
919 * @cxlflash: Internal structure associated with the host.
921 * A partially allocated state remains on failure.
925 * -ENOMEM on failure to allocate memory
927 static int alloc_mem(struct cxlflash_cfg *cfg)
933 /* This allocation is about 12K, i.e. only 1 64k page
934 * and upto 4 4k pages
936 cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
937 get_order(sizeof(struct afu)));
938 if (unlikely(!cfg->afu)) {
939 pr_err("%s: cannot get %d free pages\n",
940 __func__, get_order(sizeof(struct afu)));
944 cfg->afu->parent = cfg;
945 cfg->afu->afu_map = NULL;
947 for (i = 0; i < CXLFLASH_NUM_CMDS; buf += CMD_BUFSIZE, i++) {
948 if (!((u64)buf & (PAGE_SIZE - 1))) {
949 buf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
950 if (unlikely(!buf)) {
951 pr_err("%s: Allocate command buffers fail!\n",
959 cfg->afu->cmd[i].buf = buf;
960 atomic_set(&cfg->afu->cmd[i].free, 1);
961 cfg->afu->cmd[i].slot = i;
969 * init_pci() - initializes the host as a PCI device
970 * @cxlflash: Internal structure associated with the host.
974 * -EIO on unable to communicate with device
975 * A return code from the PCI sub-routines
977 static int init_pci(struct cxlflash_cfg *cfg)
979 struct pci_dev *pdev = cfg->dev;
982 cfg->cxlflash_regs_pci = pci_resource_start(pdev, 0);
983 rc = pci_request_regions(pdev, CXLFLASH_NAME);
986 "%s: Couldn't register memory range of registers\n",
991 rc = pci_enable_device(pdev);
992 if (rc || pci_channel_offline(pdev)) {
993 if (pci_channel_offline(pdev)) {
994 cxlflash_wait_for_pci_err_recovery(cfg);
995 rc = pci_enable_device(pdev);
999 dev_err(&pdev->dev, "%s: Cannot enable adapter\n",
1001 cxlflash_wait_for_pci_err_recovery(cfg);
1002 goto out_release_regions;
1006 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1008 dev_dbg(&pdev->dev, "%s: Failed to set 64 bit PCI DMA mask\n",
1010 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1014 dev_err(&pdev->dev, "%s: Failed to set PCI DMA mask\n",
1019 pci_set_master(pdev);
1021 if (pci_channel_offline(pdev)) {
1022 cxlflash_wait_for_pci_err_recovery(cfg);
1023 if (pci_channel_offline(pdev)) {
1025 goto out_msi_disable;
1029 rc = pci_save_state(pdev);
1031 if (rc != PCIBIOS_SUCCESSFUL) {
1032 dev_err(&pdev->dev, "%s: Failed to save PCI config space\n",
1039 pr_debug("%s: returning rc=%d\n", __func__, rc);
1044 cxlflash_wait_for_pci_err_recovery(cfg);
1046 pci_disable_device(pdev);
1047 out_release_regions:
1048 pci_release_regions(pdev);
1054 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
1055 * @cxlflash: Internal structure associated with the host.
1059 * A return code from adding the host
1061 static int init_scsi(struct cxlflash_cfg *cfg)
1063 struct pci_dev *pdev = cfg->dev;
1066 rc = scsi_add_host(cfg->host, &pdev->dev);
1068 dev_err(&pdev->dev, "%s: scsi_add_host failed (rc=%d)\n",
1073 scsi_scan_host(cfg->host);
1076 pr_debug("%s: returning rc=%d\n", __func__, rc);
1081 * set_port_online() - transitions the specified host FC port to online state
1082 * @fc_regs: Top of MMIO region defined for specified port.
1084 * The provided MMIO region must be mapped prior to call. Online state means
1085 * that the FC link layer has synced, completed the handshaking process, and
1086 * is ready for login to start.
1088 static void set_port_online(u64 *fc_regs)
1092 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
1093 cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
1094 cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
1095 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
1099 * set_port_offline() - transitions the specified host FC port to offline state
1100 * @fc_regs: Top of MMIO region defined for specified port.
1102 * The provided MMIO region must be mapped prior to call.
1104 static void set_port_offline(u64 *fc_regs)
1108 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
1109 cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
1110 cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
1111 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
1115 * wait_port_online() - waits for the specified host FC port come online
1116 * @fc_regs: Top of MMIO region defined for specified port.
1117 * @delay_us: Number of microseconds to delay between reading port status.
1118 * @nretry: Number of cycles to retry reading port status.
1120 * The provided MMIO region must be mapped prior to call. This will timeout
1121 * when the cable is not plugged in.
1124 * TRUE (1) when the specified port is online
1125 * FALSE (0) when the specified port fails to come online after timeout
1126 * -EINVAL when @delay_us is less than 1000
1128 static int wait_port_online(u64 *fc_regs, u32 delay_us, u32 nretry)
1132 if (delay_us < 1000) {
1133 pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
1138 msleep(delay_us / 1000);
1139 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
1140 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
1143 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
1147 * wait_port_offline() - waits for the specified host FC port go offline
1148 * @fc_regs: Top of MMIO region defined for specified port.
1149 * @delay_us: Number of microseconds to delay between reading port status.
1150 * @nretry: Number of cycles to retry reading port status.
1152 * The provided MMIO region must be mapped prior to call.
1155 * TRUE (1) when the specified port is offline
1156 * FALSE (0) when the specified port fails to go offline after timeout
1157 * -EINVAL when @delay_us is less than 1000
1159 static int wait_port_offline(u64 *fc_regs, u32 delay_us, u32 nretry)
1163 if (delay_us < 1000) {
1164 pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
1169 msleep(delay_us / 1000);
1170 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
1171 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
1174 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
1178 * afu_set_wwpn() - configures the WWPN for the specified host FC port
1179 * @afu: AFU associated with the host that owns the specified FC port.
1180 * @port: Port number being configured.
1181 * @fc_regs: Top of MMIO region defined for specified port.
1182 * @wwpn: The world-wide-port-number previously discovered for port.
1184 * The provided MMIO region must be mapped prior to call. As part of the
1185 * sequence to configure the WWPN, the port is toggled offline and then back
1186 * online. This toggling action can cause this routine to delay up to a few
1187 * seconds. When configured to use the internal LUN feature of the AFU, a
1188 * failure to come online is overridden.
1191 * 0 when the WWPN is successfully written and the port comes back online
1192 * -1 when the port fails to go offline or come back up online
1194 static int afu_set_wwpn(struct afu *afu, int port, u64 *fc_regs, u64 wwpn)
1198 set_port_offline(fc_regs);
1200 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1201 FC_PORT_STATUS_RETRY_CNT)) {
1202 pr_debug("%s: wait on port %d to go offline timed out\n",
1204 ret = -1; /* but continue on to leave the port back online */
1208 writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
1210 set_port_online(fc_regs);
1212 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1213 FC_PORT_STATUS_RETRY_CNT)) {
1214 pr_debug("%s: wait on port %d to go online timed out\n",
1219 * Override for internal lun!!!
1221 if (afu->internal_lun) {
1222 pr_debug("%s: Overriding port %d online timeout!!!\n",
1228 pr_debug("%s: returning rc=%d\n", __func__, ret);
1234 * afu_link_reset() - resets the specified host FC port
1235 * @afu: AFU associated with the host that owns the specified FC port.
1236 * @port: Port number being configured.
1237 * @fc_regs: Top of MMIO region defined for specified port.
1239 * The provided MMIO region must be mapped prior to call. The sequence to
1240 * reset the port involves toggling it offline and then back online. This
1241 * action can cause this routine to delay up to a few seconds. An effort
1242 * is made to maintain link with the device by switching to host to use
1243 * the alternate port exclusively while the reset takes place.
1244 * failure to come online is overridden.
1246 static void afu_link_reset(struct afu *afu, int port, u64 *fc_regs)
1250 /* first switch the AFU to the other links, if any */
1251 port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
1252 port_sel &= ~(1ULL << port);
1253 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1254 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1256 set_port_offline(fc_regs);
1257 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1258 FC_PORT_STATUS_RETRY_CNT))
1259 pr_err("%s: wait on port %d to go offline timed out\n",
1262 set_port_online(fc_regs);
1263 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1264 FC_PORT_STATUS_RETRY_CNT))
1265 pr_err("%s: wait on port %d to go online timed out\n",
1268 /* switch back to include this port */
1269 port_sel |= (1ULL << port);
1270 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1271 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1273 pr_debug("%s: returning port_sel=%lld\n", __func__, port_sel);
1277 * Asynchronous interrupt information table
1279 static const struct asyc_intr_info ainfo[] = {
1280 {SISL_ASTATUS_FC0_OTHER, "other error", 0, CLR_FC_ERROR | LINK_RESET},
1281 {SISL_ASTATUS_FC0_LOGO, "target initiated LOGO", 0, 0},
1282 {SISL_ASTATUS_FC0_CRC_T, "CRC threshold exceeded", 0, LINK_RESET},
1283 {SISL_ASTATUS_FC0_LOGI_R, "login timed out, retrying", 0, 0},
1284 {SISL_ASTATUS_FC0_LOGI_F, "login failed", 0, CLR_FC_ERROR},
1285 {SISL_ASTATUS_FC0_LOGI_S, "login succeeded", 0, 0},
1286 {SISL_ASTATUS_FC0_LINK_DN, "link down", 0, 0},
1287 {SISL_ASTATUS_FC0_LINK_UP, "link up", 0, 0},
1288 {SISL_ASTATUS_FC1_OTHER, "other error", 1, CLR_FC_ERROR | LINK_RESET},
1289 {SISL_ASTATUS_FC1_LOGO, "target initiated LOGO", 1, 0},
1290 {SISL_ASTATUS_FC1_CRC_T, "CRC threshold exceeded", 1, LINK_RESET},
1291 {SISL_ASTATUS_FC1_LOGI_R, "login timed out, retrying", 1, 0},
1292 {SISL_ASTATUS_FC1_LOGI_F, "login failed", 1, CLR_FC_ERROR},
1293 {SISL_ASTATUS_FC1_LOGI_S, "login succeeded", 1, 0},
1294 {SISL_ASTATUS_FC1_LINK_DN, "link down", 1, 0},
1295 {SISL_ASTATUS_FC1_LINK_UP, "link up", 1, 0},
1296 {0x0, "", 0, 0} /* terminator */
1300 * find_ainfo() - locates and returns asynchronous interrupt information
1301 * @status: Status code set by AFU on error.
1303 * Return: The located information or NULL when the status code is invalid.
1305 static const struct asyc_intr_info *find_ainfo(u64 status)
1307 const struct asyc_intr_info *info;
1309 for (info = &ainfo[0]; info->status; info++)
1310 if (info->status == status)
1317 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1318 * @afu: AFU associated with the host.
1320 static void afu_err_intr_init(struct afu *afu)
1325 /* global async interrupts: AFU clears afu_ctrl on context exit
1326 * if async interrupts were sent to that context. This prevents
1327 * the AFU form sending further async interrupts when
1329 * nobody to receive them.
1333 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
1334 /* set LISN# to send and point to master context */
1335 reg = ((u64) (((afu->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
1337 if (afu->internal_lun)
1338 reg |= 1; /* Bit 63 indicates local lun */
1339 writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1341 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1342 /* unmask bits that are of interest */
1343 /* note: afu can send an interrupt after this step */
1344 writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1345 /* clear again in case a bit came on after previous clear but before */
1347 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1349 /* Clear/Set internal lun bits */
1350 reg = readq_be(&afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1351 reg &= SISL_FC_INTERNAL_MASK;
1352 if (afu->internal_lun)
1353 reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
1354 writeq_be(reg, &afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1356 /* now clear FC errors */
1357 for (i = 0; i < NUM_FC_PORTS; i++) {
1358 writeq_be(0xFFFFFFFFU,
1359 &afu->afu_map->global.fc_regs[i][FC_ERROR / 8]);
1360 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRCAP / 8]);
1363 /* sync interrupts for master's IOARRIN write */
1364 /* note that unlike asyncs, there can be no pending sync interrupts */
1365 /* at this time (this is a fresh context and master has not written */
1366 /* IOARRIN yet), so there is nothing to clear. */
1368 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1369 writeq_be(SISL_MSI_SYNC_ERROR, &afu->host_map->ctx_ctrl);
1370 writeq_be(SISL_ISTATUS_MASK, &afu->host_map->intr_mask);
1374 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1375 * @irq: Interrupt number.
1376 * @data: Private data provided at interrupt registration, the AFU.
1378 * Return: Always return IRQ_HANDLED.
1380 static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1382 struct afu *afu = (struct afu *)data;
1386 reg = readq_be(&afu->host_map->intr_status);
1387 reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1389 if (reg_unmasked == 0UL) {
1390 pr_err("%s: %llX: spurious interrupt, intr_status %016llX\n",
1391 __func__, (u64)afu, reg);
1392 goto cxlflash_sync_err_irq_exit;
1395 pr_err("%s: %llX: unexpected interrupt, intr_status %016llX\n",
1396 __func__, (u64)afu, reg);
1398 writeq_be(reg_unmasked, &afu->host_map->intr_clear);
1400 cxlflash_sync_err_irq_exit:
1401 pr_debug("%s: returning rc=%d\n", __func__, IRQ_HANDLED);
1406 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1407 * @irq: Interrupt number.
1408 * @data: Private data provided at interrupt registration, the AFU.
1410 * Return: Always return IRQ_HANDLED.
1412 static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
1414 struct afu *afu = (struct afu *)data;
1415 struct afu_cmd *cmd;
1416 bool toggle = afu->toggle;
1418 *hrrq_start = afu->hrrq_start,
1419 *hrrq_end = afu->hrrq_end,
1420 *hrrq_curr = afu->hrrq_curr;
1422 /* Process however many RRQ entries that are ready */
1426 if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1429 cmd = (struct afu_cmd *)(entry & ~SISL_RESP_HANDLE_T_BIT);
1432 /* Advance to next entry or wrap and flip the toggle bit */
1433 if (hrrq_curr < hrrq_end)
1436 hrrq_curr = hrrq_start;
1437 toggle ^= SISL_RESP_HANDLE_T_BIT;
1441 afu->hrrq_curr = hrrq_curr;
1442 afu->toggle = toggle;
1448 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1449 * @irq: Interrupt number.
1450 * @data: Private data provided at interrupt registration, the AFU.
1452 * Return: Always return IRQ_HANDLED.
1454 static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1456 struct afu *afu = (struct afu *)data;
1457 struct cxlflash_cfg *cfg;
1459 const struct asyc_intr_info *info;
1460 struct sisl_global_map *global = &afu->afu_map->global;
1467 reg = readq_be(&global->regs.aintr_status);
1468 reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1470 if (reg_unmasked == 0) {
1471 pr_err("%s: spurious interrupt, aintr_status 0x%016llX\n",
1476 /* it is OK to clear AFU status before FC_ERROR */
1477 writeq_be(reg_unmasked, &global->regs.aintr_clear);
1479 /* check each bit that is on */
1480 for (i = 0; reg_unmasked; i++, reg_unmasked = (reg_unmasked >> 1)) {
1481 info = find_ainfo(1ULL << i);
1482 if ((reg_unmasked & 0x1) || !info)
1487 pr_err("%s: FC Port %d -> %s, fc_status 0x%08llX\n",
1488 __func__, port, info->desc,
1489 readq_be(&global->fc_regs[port][FC_STATUS / 8]));
1492 * do link reset first, some OTHER errors will set FC_ERROR
1493 * again if cleared before or w/o a reset
1495 if (info->action & LINK_RESET) {
1496 pr_err("%s: FC Port %d: resetting link\n",
1498 cfg->lr_state = LINK_RESET_REQUIRED;
1499 cfg->lr_port = port;
1500 schedule_work(&cfg->work_q);
1503 if (info->action & CLR_FC_ERROR) {
1504 reg = readq_be(&global->fc_regs[port][FC_ERROR / 8]);
1507 * since all errors are unmasked, FC_ERROR and FC_ERRCAP
1508 * should be the same and tracing one is sufficient.
1511 pr_err("%s: fc %d: clearing fc_error 0x%08llX\n",
1512 __func__, port, reg);
1514 writeq_be(reg, &global->fc_regs[port][FC_ERROR / 8]);
1515 writeq_be(0, &global->fc_regs[port][FC_ERRCAP / 8]);
1520 pr_debug("%s: returning rc=%d, afu=%p\n", __func__, IRQ_HANDLED, afu);
1525 * start_context() - starts the master context
1526 * @cxlflash: Internal structure associated with the host.
1528 * Return: A success or failure value from CXL services.
1530 static int start_context(struct cxlflash_cfg *cfg)
1534 rc = cxl_start_context(cfg->mcctx,
1535 cfg->afu->work.work_element_descriptor,
1538 pr_debug("%s: returning rc=%d\n", __func__, rc);
1543 * read_vpd() - obtains the WWPNs from VPD
1544 * @cxlflash: Internal structure associated with the host.
1545 * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
1549 * -ENODEV when VPD or WWPN keywords not found
1551 static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1553 struct pci_dev *dev = cfg->parent_dev;
1555 int ro_start, ro_size, i, j, k;
1557 char vpd_data[CXLFLASH_VPD_LEN];
1558 char tmp_buf[WWPN_BUF_LEN] = { 0 };
1559 char *wwpn_vpd_tags[NUM_FC_PORTS] = { "V5", "V6" };
1561 /* Get the VPD data from the device */
1562 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
1563 if (unlikely(vpd_size <= 0)) {
1564 pr_err("%s: Unable to read VPD (size = %ld)\n",
1565 __func__, vpd_size);
1570 /* Get the read only section offset */
1571 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
1572 PCI_VPD_LRDT_RO_DATA);
1573 if (unlikely(ro_start < 0)) {
1574 pr_err("%s: VPD Read-only data not found\n", __func__);
1579 /* Get the read only section size, cap when extends beyond read VPD */
1580 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1582 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1583 if (unlikely((i + j) > vpd_size)) {
1584 pr_debug("%s: Might need to read more VPD (%d > %ld)\n",
1585 __func__, (i + j), vpd_size);
1586 ro_size = vpd_size - i;
1590 * Find the offset of the WWPN tag within the read only
1591 * VPD data and validate the found field (partials are
1592 * no good to us). Convert the ASCII data to an integer
1593 * value. Note that we must copy to a temporary buffer
1594 * because the conversion service requires that the ASCII
1595 * string be terminated.
1597 for (k = 0; k < NUM_FC_PORTS; k++) {
1599 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1601 i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
1602 if (unlikely(i < 0)) {
1603 pr_err("%s: Port %d WWPN not found in VPD\n",
1609 j = pci_vpd_info_field_size(&vpd_data[i]);
1610 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1611 if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
1612 pr_err("%s: Port %d WWPN incomplete or VPD corrupt\n",
1618 memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1619 rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1621 pr_err("%s: Fail to convert port %d WWPN to integer\n",
1629 pr_debug("%s: returning rc=%d\n", __func__, rc);
1634 * cxlflash_context_reset() - timeout handler for AFU commands
1635 * @cmd: AFU command that timed out.
1637 * Sends a reset to the AFU.
1639 void cxlflash_context_reset(struct afu_cmd *cmd)
1644 struct afu *afu = cmd->parent;
1647 pr_debug("%s: cmd=%p\n", __func__, cmd);
1649 spin_lock_irqsave(&cmd->slock, lock_flags);
1651 /* Already completed? */
1652 if (cmd->sa.host_use_b[0] & B_DONE) {
1653 spin_unlock_irqrestore(&cmd->slock, lock_flags);
1657 cmd->sa.host_use_b[0] |= (B_DONE | B_ERROR | B_TIMEOUT);
1658 spin_unlock_irqrestore(&cmd->slock, lock_flags);
1661 * We really want to send this reset at all costs, so spread
1662 * out wait time on successive retries for available room.
1665 room = readq_be(&afu->host_map->cmd_room);
1666 atomic64_set(&afu->room, room);
1670 } while (nretry++ < MC_ROOM_RETRY_CNT);
1672 pr_err("%s: no cmd_room to send reset\n", __func__);
1677 writeq_be(rrin, &afu->host_map->ioarrin);
1679 rrin = readq_be(&afu->host_map->ioarrin);
1682 /* Double delay each time */
1684 } while (nretry++ < MC_ROOM_RETRY_CNT);
1688 * init_pcr() - initialize the provisioning and control registers
1689 * @cxlflash: Internal structure associated with the host.
1691 * Also sets up fast access to the mapped registers and initializes AFU
1692 * command fields that never change.
1694 void init_pcr(struct cxlflash_cfg *cfg)
1696 struct afu *afu = cfg->afu;
1697 struct sisl_ctrl_map *ctrl_map;
1700 for (i = 0; i < MAX_CONTEXT; i++) {
1701 ctrl_map = &afu->afu_map->ctrls[i].ctrl;
1702 /* disrupt any clients that could be running */
1703 /* e. g. clients that survived a master restart */
1704 writeq_be(0, &ctrl_map->rht_start);
1705 writeq_be(0, &ctrl_map->rht_cnt_id);
1706 writeq_be(0, &ctrl_map->ctx_cap);
1709 /* copy frequently used fields into afu */
1710 afu->ctx_hndl = (u16) cxl_process_element(cfg->mcctx);
1711 /* ctx_hndl is 16 bits in CAIA */
1712 afu->host_map = &afu->afu_map->hosts[afu->ctx_hndl].host;
1713 afu->ctrl_map = &afu->afu_map->ctrls[afu->ctx_hndl].ctrl;
1715 /* Program the Endian Control for the master context */
1716 writeq_be(SISL_ENDIAN_CTRL, &afu->host_map->endian_ctrl);
1718 /* initialize cmd fields that never change */
1719 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
1720 afu->cmd[i].rcb.ctx_id = afu->ctx_hndl;
1721 afu->cmd[i].rcb.msi = SISL_MSI_RRQ_UPDATED;
1722 afu->cmd[i].rcb.rrq = 0x0;
1727 * init_global() - initialize AFU global registers
1728 * @cxlflash: Internal structure associated with the host.
1730 int init_global(struct cxlflash_cfg *cfg)
1732 struct afu *afu = cfg->afu;
1733 u64 wwpn[NUM_FC_PORTS]; /* wwpn of AFU ports */
1734 int i = 0, num_ports = 0;
1738 rc = read_vpd(cfg, &wwpn[0]);
1740 pr_err("%s: could not read vpd rc=%d\n", __func__, rc);
1744 pr_debug("%s: wwpn0=0x%llX wwpn1=0x%llX\n", __func__, wwpn[0], wwpn[1]);
1746 /* set up RRQ in AFU for master issued cmds */
1747 writeq_be((u64) afu->hrrq_start, &afu->host_map->rrq_start);
1748 writeq_be((u64) afu->hrrq_end, &afu->host_map->rrq_end);
1750 /* AFU configuration */
1751 reg = readq_be(&afu->afu_map->global.regs.afu_config);
1752 reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1753 /* enable all auto retry options and control endianness */
1754 /* leave others at default: */
1755 /* CTX_CAP write protected, mbox_r does not clear on read and */
1756 /* checker on if dual afu */
1757 writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1759 /* global port select: select either port */
1760 if (afu->internal_lun) {
1761 /* only use port 0 */
1762 writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
1763 num_ports = NUM_FC_PORTS - 1;
1765 writeq_be(BOTH_PORTS, &afu->afu_map->global.regs.afu_port_sel);
1766 num_ports = NUM_FC_PORTS;
1769 for (i = 0; i < num_ports; i++) {
1770 /* unmask all errors (but they are still masked at AFU) */
1771 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRMSK / 8]);
1772 /* clear CRC error cnt & set a threshold */
1773 (void)readq_be(&afu->afu_map->global.
1774 fc_regs[i][FC_CNT_CRCERR / 8]);
1775 writeq_be(MC_CRC_THRESH, &afu->afu_map->global.fc_regs[i]
1776 [FC_CRC_THRESH / 8]);
1778 /* set WWPNs. If already programmed, wwpn[i] is 0 */
1780 afu_set_wwpn(afu, i,
1781 &afu->afu_map->global.fc_regs[i][0],
1783 pr_err("%s: failed to set WWPN on port %d\n",
1788 /* Programming WWPN back to back causes additional
1789 * offline/online transitions and a PLOGI
1795 /* set up master's own CTX_CAP to allow real mode, host translation */
1796 /* tbls, afu cmds and read/write GSCSI cmds. */
1797 /* First, unlock ctx_cap write by reading mbox */
1798 (void)readq_be(&afu->ctrl_map->mbox_r); /* unlock ctx_cap */
1799 writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1800 SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1801 SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1802 &afu->ctrl_map->ctx_cap);
1803 /* init heartbeat */
1804 afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
1811 * start_afu() - initializes and starts the AFU
1812 * @cxlflash: Internal structure associated with the host.
1814 static int start_afu(struct cxlflash_cfg *cfg)
1816 struct afu *afu = cfg->afu;
1817 struct afu_cmd *cmd;
1822 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
1825 init_completion(&cmd->cevent);
1826 spin_lock_init(&cmd->slock);
1832 /* initialize RRQ pointers */
1833 afu->hrrq_start = &afu->rrq_entry[0];
1834 afu->hrrq_end = &afu->rrq_entry[NUM_RRQ_ENTRY - 1];
1835 afu->hrrq_curr = afu->hrrq_start;
1838 rc = init_global(cfg);
1840 pr_debug("%s: returning rc=%d\n", __func__, rc);
1845 * init_mc() - create and register as the master context
1846 * @cxlflash: Internal structure associated with the host.
1850 * -ENOMEM when unable to obtain a context from CXL services
1851 * A failure value from CXL services.
1853 static int init_mc(struct cxlflash_cfg *cfg)
1855 struct cxl_context *ctx;
1856 struct device *dev = &cfg->dev->dev;
1857 struct afu *afu = cfg->afu;
1859 enum undo_level level;
1861 ctx = cxl_get_context(cfg->dev);
1866 /* Set it up as a master with the CXL */
1867 cxl_set_master(ctx);
1869 /* During initialization reset the AFU to start from a clean slate */
1870 rc = cxl_afu_reset(cfg->mcctx);
1872 dev_err(dev, "%s: initial AFU reset failed rc=%d\n",
1874 level = RELEASE_CONTEXT;
1878 rc = cxl_allocate_afu_irqs(ctx, 3);
1880 dev_err(dev, "%s: call to allocate_afu_irqs failed rc=%d!\n",
1882 level = RELEASE_CONTEXT;
1886 rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, afu,
1887 "SISL_MSI_SYNC_ERROR");
1888 if (unlikely(rc <= 0)) {
1889 dev_err(dev, "%s: IRQ 1 (SISL_MSI_SYNC_ERROR) map failed!\n",
1895 rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, afu,
1896 "SISL_MSI_RRQ_UPDATED");
1897 if (unlikely(rc <= 0)) {
1898 dev_err(dev, "%s: IRQ 2 (SISL_MSI_RRQ_UPDATED) map failed!\n",
1904 rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, afu,
1905 "SISL_MSI_ASYNC_ERROR");
1906 if (unlikely(rc <= 0)) {
1907 dev_err(dev, "%s: IRQ 3 (SISL_MSI_ASYNC_ERROR) map failed!\n",
1915 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1916 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1917 * element (pe) that is embedded in the context (ctx)
1919 rc = start_context(cfg);
1921 dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
1922 level = UNMAP_THREE;
1926 pr_debug("%s: returning rc=%d\n", __func__, rc);
1929 term_mc(cfg, level);
1934 * init_afu() - setup as master context and start AFU
1935 * @cxlflash: Internal structure associated with the host.
1937 * This routine is a higher level of control for configuring the
1938 * AFU on probe and reset paths.
1942 * -ENOMEM when unable to map the AFU MMIO space
1943 * A failure value from internal services.
1945 static int init_afu(struct cxlflash_cfg *cfg)
1949 struct afu *afu = cfg->afu;
1950 struct device *dev = &cfg->dev->dev;
1952 cxl_perst_reloads_same_image(cfg->cxl_afu, true);
1956 dev_err(dev, "%s: call to init_mc failed, rc=%d!\n",
1961 /* Map the entire MMIO space of the AFU.
1963 afu->afu_map = cxl_psa_map(cfg->mcctx);
1964 if (!afu->afu_map) {
1966 term_mc(cfg, UNDO_START);
1967 dev_err(dev, "%s: call to cxl_psa_map failed!\n", __func__);
1971 /* don't byte reverse on reading afu_version, else the string form */
1972 /* will be backwards */
1973 reg = afu->afu_map->global.regs.afu_version;
1974 memcpy(afu->version, ®, 8);
1975 afu->interface_version =
1976 readq_be(&afu->afu_map->global.regs.interface_version);
1977 pr_debug("%s: afu version %s, interface version 0x%llX\n",
1978 __func__, afu->version, afu->interface_version);
1980 rc = start_afu(cfg);
1982 dev_err(dev, "%s: call to start_afu failed, rc=%d!\n",
1984 term_mc(cfg, UNDO_START);
1985 cxl_psa_unmap((void *)afu->afu_map);
1986 afu->afu_map = NULL;
1990 afu_err_intr_init(cfg->afu);
1991 atomic64_set(&afu->room, readq_be(&afu->host_map->cmd_room));
1993 /* Restore the LUN mappings */
1994 cxlflash_restore_luntable(cfg);
1996 pr_debug("%s: returning rc=%d\n", __func__, rc);
2001 * cxlflash_send_cmd() - sends an AFU command
2002 * @afu: AFU associated with the host.
2003 * @cmd: AFU command to send.
2009 int cxlflash_send_cmd(struct afu *afu, struct afu_cmd *cmd)
2011 struct cxlflash_cfg *cfg = afu->parent;
2018 * This routine is used by critical users such an AFU sync and to
2019 * send a task management function (TMF). Thus we want to retry a
2020 * bit before returning an error. To avoid the performance penalty
2021 * of MMIO, we spread the update of 'room' over multiple commands.
2024 newval = atomic64_dec_if_positive(&afu->room);
2027 room = readq_be(&afu->host_map->cmd_room);
2028 atomic64_set(&afu->room, room);
2032 } while (nretry++ < MC_ROOM_RETRY_CNT);
2034 pr_err("%s: no cmd_room to send 0x%X\n",
2035 __func__, cmd->rcb.cdb[0]);
2038 } else if (unlikely(newval < 0)) {
2039 /* This should be rare. i.e. Only if two threads race and
2040 * decrement before the MMIO read is done. In this case
2041 * just benefit from the other thread having updated
2044 if (nretry++ < MC_ROOM_RETRY_CNT) {
2053 writeq_be((u64)&cmd->rcb, &afu->host_map->ioarrin);
2055 pr_debug("%s: cmd=%p len=%d ea=%p rc=%d\n", __func__, cmd,
2056 cmd->rcb.data_len, (void *)cmd->rcb.data_ea, rc);
2060 afu->read_room = true;
2061 schedule_work(&cfg->work_q);
2062 rc = SCSI_MLQUEUE_HOST_BUSY;
2067 * cxlflash_wait_resp() - polls for a response or timeout to a sent AFU command
2068 * @afu: AFU associated with the host.
2069 * @cmd: AFU command that was sent.
2071 void cxlflash_wait_resp(struct afu *afu, struct afu_cmd *cmd)
2073 ulong timeout = jiffies + (cmd->rcb.timeout * 2 * HZ);
2075 timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
2077 cxlflash_context_reset(cmd);
2079 if (unlikely(cmd->sa.ioasc != 0))
2080 pr_err("%s: CMD 0x%X failed, IOASC: flags 0x%X, afu_rc 0x%X, "
2081 "scsi_rc 0x%X, fc_rc 0x%X\n", __func__, cmd->rcb.cdb[0],
2082 cmd->sa.rc.flags, cmd->sa.rc.afu_rc, cmd->sa.rc.scsi_rc,
2087 * cxlflash_afu_sync() - builds and sends an AFU sync command
2088 * @afu: AFU associated with the host.
2089 * @ctx_hndl_u: Identifies context requesting sync.
2090 * @res_hndl_u: Identifies resource requesting sync.
2091 * @mode: Type of sync to issue (lightweight, heavyweight, global).
2093 * The AFU can only take 1 sync command at a time. This routine enforces this
2094 * limitation by using a mutex to provide exlusive access to the AFU during
2095 * the sync. This design point requires calling threads to not be on interrupt
2096 * context due to the possibility of sleeping during concurrent sync operations.
2098 * AFU sync operations are only necessary and allowed when the device is
2099 * operating normally. When not operating normally, sync requests can occur as
2100 * part of cleaning up resources associated with an adapter prior to removal.
2101 * In this scenario, these requests are simply ignored (safe due to the AFU
2108 int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
2109 res_hndl_t res_hndl_u, u8 mode)
2111 struct cxlflash_cfg *cfg = afu->parent;
2112 struct afu_cmd *cmd = NULL;
2115 static DEFINE_MUTEX(sync_active);
2117 if (cfg->state != STATE_NORMAL) {
2118 pr_debug("%s: Sync not required! (%u)\n", __func__, cfg->state);
2122 mutex_lock(&sync_active);
2124 cmd = cxlflash_cmd_checkout(afu);
2125 if (unlikely(!cmd)) {
2127 udelay(1000 * retry_cnt);
2128 if (retry_cnt < MC_RETRY_CNT)
2130 pr_err("%s: could not get a free command\n", __func__);
2135 pr_debug("%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
2137 memset(cmd->rcb.cdb, 0, sizeof(cmd->rcb.cdb));
2139 cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
2140 cmd->rcb.port_sel = 0x0; /* NA */
2141 cmd->rcb.lun_id = 0x0; /* NA */
2142 cmd->rcb.data_len = 0x0;
2143 cmd->rcb.data_ea = 0x0;
2144 cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
2146 cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
2147 cmd->rcb.cdb[1] = mode;
2149 /* The cdb is aligned, no unaligned accessors required */
2150 *((u16 *)&cmd->rcb.cdb[2]) = swab16(ctx_hndl_u);
2151 *((u32 *)&cmd->rcb.cdb[4]) = swab32(res_hndl_u);
2153 rc = cxlflash_send_cmd(afu, cmd);
2157 cxlflash_wait_resp(afu, cmd);
2159 /* set on timeout */
2160 if (unlikely((cmd->sa.ioasc != 0) ||
2161 (cmd->sa.host_use_b[0] & B_ERROR)))
2164 mutex_unlock(&sync_active);
2166 cxlflash_cmd_checkin(cmd);
2167 pr_debug("%s: returning rc=%d\n", __func__, rc);
2172 * cxlflash_afu_reset() - resets the AFU
2173 * @cxlflash: Internal structure associated with the host.
2177 * A failure value from internal services.
2179 int cxlflash_afu_reset(struct cxlflash_cfg *cfg)
2182 /* Stop the context before the reset. Since the context is
2183 * no longer available restart it after the reset is complete
2190 pr_debug("%s: returning rc=%d\n", __func__, rc);
2195 * cxlflash_worker_thread() - work thread handler for the AFU
2196 * @work: Work structure contained within cxlflash associated with host.
2198 * Handles the following events:
2199 * - Link reset which cannot be performed on interrupt context due to
2200 * blocking up to a few seconds
2201 * - Read AFU command room
2203 static void cxlflash_worker_thread(struct work_struct *work)
2205 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
2207 struct afu *afu = cfg->afu;
2211 /* Avoid MMIO if the device has failed */
2213 if (cfg->state != STATE_NORMAL)
2216 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2218 if (cfg->lr_state == LINK_RESET_REQUIRED) {
2219 port = cfg->lr_port;
2221 pr_err("%s: invalid port index %d\n", __func__, port);
2223 spin_unlock_irqrestore(cfg->host->host_lock,
2226 /* The reset can block... */
2227 afu_link_reset(afu, port,
2229 global.fc_regs[port][0]);
2230 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2233 cfg->lr_state = LINK_RESET_COMPLETE;
2236 if (afu->read_room) {
2237 atomic64_set(&afu->room, readq_be(&afu->host_map->cmd_room));
2238 afu->read_room = false;
2241 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
2245 * cxlflash_probe() - PCI entry point to add host
2246 * @pdev: PCI device associated with the host.
2247 * @dev_id: PCI device id associated with device.
2249 * Return: 0 on success / non-zero on failure
2251 static int cxlflash_probe(struct pci_dev *pdev,
2252 const struct pci_device_id *dev_id)
2254 struct Scsi_Host *host;
2255 struct cxlflash_cfg *cfg = NULL;
2256 struct device *phys_dev;
2257 struct dev_dependent_vals *ddv;
2260 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
2261 __func__, pdev->irq);
2263 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
2264 driver_template.max_sectors = ddv->max_sectors;
2266 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
2268 dev_err(&pdev->dev, "%s: call to scsi_host_alloc failed!\n",
2274 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
2275 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
2276 host->max_channel = NUM_FC_PORTS - 1;
2277 host->unique_id = host->host_no;
2278 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
2280 cfg = (struct cxlflash_cfg *)host->hostdata;
2282 rc = alloc_mem(cfg);
2284 dev_err(&pdev->dev, "%s: call to scsi_host_alloc failed!\n",
2290 cfg->init_state = INIT_STATE_NONE;
2294 * The promoted LUNs move to the top of the LUN table. The rest stay
2295 * on the bottom half. The bottom half grows from the end
2296 * (index = 255), whereas the top half grows from the beginning
2299 cfg->promote_lun_index = 0;
2300 cfg->last_lun_index[0] = CXLFLASH_NUM_VLUNS/2 - 1;
2301 cfg->last_lun_index[1] = CXLFLASH_NUM_VLUNS/2 - 1;
2303 cfg->dev_id = (struct pci_device_id *)dev_id;
2306 init_waitqueue_head(&cfg->tmf_waitq);
2307 init_waitqueue_head(&cfg->limbo_waitq);
2309 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
2310 cfg->lr_state = LINK_RESET_INVALID;
2312 mutex_init(&cfg->ctx_tbl_list_mutex);
2313 mutex_init(&cfg->ctx_recovery_mutex);
2314 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
2315 INIT_LIST_HEAD(&cfg->lluns);
2317 pci_set_drvdata(pdev, cfg);
2319 /* Use the special service provided to look up the physical
2320 * PCI device, since we are called on the probe of the virtual
2321 * PCI host bus (vphb)
2323 phys_dev = cxl_get_phys_dev(pdev);
2324 if (!dev_is_pci(phys_dev)) {
2325 pr_err("%s: not a pci dev\n", __func__);
2329 cfg->parent_dev = to_pci_dev(phys_dev);
2331 cfg->cxl_afu = cxl_pci_to_afu(pdev);
2335 dev_err(&pdev->dev, "%s: call to init_pci "
2336 "failed rc=%d!\n", __func__, rc);
2339 cfg->init_state = INIT_STATE_PCI;
2343 dev_err(&pdev->dev, "%s: call to init_afu "
2344 "failed rc=%d!\n", __func__, rc);
2347 cfg->init_state = INIT_STATE_AFU;
2350 rc = init_scsi(cfg);
2352 dev_err(&pdev->dev, "%s: call to init_scsi "
2353 "failed rc=%d!\n", __func__, rc);
2356 cfg->init_state = INIT_STATE_SCSI;
2359 pr_debug("%s: returning rc=%d\n", __func__, rc);
2363 cxlflash_remove(pdev);
2368 * cxlflash_pci_error_detected() - called when a PCI error is detected
2369 * @pdev: PCI device struct.
2370 * @state: PCI channel state.
2372 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2374 static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
2375 pci_channel_state_t state)
2378 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2379 struct device *dev = &cfg->dev->dev;
2381 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
2384 case pci_channel_io_frozen:
2385 cfg->state = STATE_LIMBO;
2387 /* Turn off legacy I/O */
2388 scsi_block_requests(cfg->host);
2389 rc = cxlflash_mark_contexts_error(cfg);
2391 dev_err(dev, "%s: Failed to mark user contexts!(%d)\n",
2393 term_mc(cfg, UNDO_START);
2396 return PCI_ERS_RESULT_NEED_RESET;
2397 case pci_channel_io_perm_failure:
2398 cfg->state = STATE_FAILTERM;
2399 wake_up_all(&cfg->limbo_waitq);
2400 scsi_unblock_requests(cfg->host);
2401 return PCI_ERS_RESULT_DISCONNECT;
2405 return PCI_ERS_RESULT_NEED_RESET;
2409 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2410 * @pdev: PCI device struct.
2412 * This routine is called by the pci error recovery code after the PCI
2413 * slot has been reset, just before we should resume normal operations.
2415 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2417 static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
2420 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2421 struct device *dev = &cfg->dev->dev;
2423 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2427 dev_err(dev, "%s: EEH recovery failed! (%d)\n", __func__, rc);
2428 return PCI_ERS_RESULT_DISCONNECT;
2431 return PCI_ERS_RESULT_RECOVERED;
2435 * cxlflash_pci_resume() - called when normal operation can resume
2436 * @pdev: PCI device struct
2438 static void cxlflash_pci_resume(struct pci_dev *pdev)
2440 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2441 struct device *dev = &cfg->dev->dev;
2443 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2445 cfg->state = STATE_NORMAL;
2446 wake_up_all(&cfg->limbo_waitq);
2447 scsi_unblock_requests(cfg->host);
2450 static const struct pci_error_handlers cxlflash_err_handler = {
2451 .error_detected = cxlflash_pci_error_detected,
2452 .slot_reset = cxlflash_pci_slot_reset,
2453 .resume = cxlflash_pci_resume,
2457 * PCI device structure
2459 static struct pci_driver cxlflash_driver = {
2460 .name = CXLFLASH_NAME,
2461 .id_table = cxlflash_pci_table,
2462 .probe = cxlflash_probe,
2463 .remove = cxlflash_remove,
2464 .err_handler = &cxlflash_err_handler,
2468 * init_cxlflash() - module entry point
2470 * Return: 0 on success / non-zero on failure
2472 static int __init init_cxlflash(void)
2474 pr_info("%s: IBM Power CXL Flash Adapter: %s\n",
2475 __func__, CXLFLASH_DRIVER_DATE);
2477 cxlflash_list_init();
2479 return pci_register_driver(&cxlflash_driver);
2483 * exit_cxlflash() - module exit point
2485 static void __exit exit_cxlflash(void)
2487 cxlflash_term_global_luns();
2488 cxlflash_free_errpage();
2490 pci_unregister_driver(&cxlflash_driver);
2493 module_init(init_cxlflash);
2494 module_exit(exit_cxlflash);