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 * 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 static struct afu_cmd *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_devel("%s: returning found index=%d cmd=%p\n",
62 __func__, cmd->slot, cmd);
63 memset(cmd->buf, 0, CMD_BUFSIZE);
64 memset(cmd->rcb.cdb, 0, sizeof(cmd->rcb.cdb));
73 * 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 static void 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_devel("%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;
115 ioarcb = &(cmd->rcb);
118 if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) {
119 resid = ioasa->resid;
120 scsi_set_resid(scp, resid);
121 pr_debug("%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
122 __func__, cmd, scp, resid);
125 if (ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN) {
126 pr_debug("%s: cmd underrun cmd = %p scp = %p\n",
128 scp->result = (DID_ERROR << 16);
131 pr_debug("%s: cmd failed afu_rc=%d scsi_rc=%d fc_rc=%d "
132 "afu_extra=0x%X, scsi_extra=0x%X, fc_extra=0x%X\n",
133 __func__, ioasa->rc.afu_rc, ioasa->rc.scsi_rc,
134 ioasa->rc.fc_rc, ioasa->afu_extra, ioasa->scsi_extra,
137 if (ioasa->rc.scsi_rc) {
138 /* We have a SCSI status */
139 if (ioasa->rc.flags & SISL_RC_FLAGS_SENSE_VALID) {
140 memcpy(scp->sense_buffer, ioasa->sense_data,
141 SISL_SENSE_DATA_LEN);
142 scp->result = ioasa->rc.scsi_rc;
144 scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16);
148 * We encountered an error. Set scp->result based on nature
151 if (ioasa->rc.fc_rc) {
152 /* We have an FC status */
153 switch (ioasa->rc.fc_rc) {
154 case SISL_FC_RC_LINKDOWN:
155 scp->result = (DID_REQUEUE << 16);
157 case SISL_FC_RC_RESID:
158 /* This indicates an FCP resid underrun */
159 if (!(ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN)) {
160 /* If the SISL_RC_FLAGS_OVERRUN flag was set,
161 * then we will handle this error else where.
162 * If not then we must handle it here.
163 * This is probably an AFU bug.
165 scp->result = (DID_ERROR << 16);
168 case SISL_FC_RC_RESIDERR:
169 /* Resid mismatch between adapter and device */
170 case SISL_FC_RC_TGTABORT:
171 case SISL_FC_RC_ABORTOK:
172 case SISL_FC_RC_ABORTFAIL:
173 case SISL_FC_RC_NOLOGI:
174 case SISL_FC_RC_ABORTPEND:
175 case SISL_FC_RC_WRABORTPEND:
176 case SISL_FC_RC_NOEXP:
177 case SISL_FC_RC_INUSE:
178 scp->result = (DID_ERROR << 16);
183 if (ioasa->rc.afu_rc) {
184 /* We have an AFU error */
185 switch (ioasa->rc.afu_rc) {
186 case SISL_AFU_RC_NO_CHANNELS:
187 scp->result = (DID_NO_CONNECT << 16);
189 case SISL_AFU_RC_DATA_DMA_ERR:
190 switch (ioasa->afu_extra) {
191 case SISL_AFU_DMA_ERR_PAGE_IN:
193 scp->result = (DID_IMM_RETRY << 16);
195 case SISL_AFU_DMA_ERR_INVALID_EA:
197 scp->result = (DID_ERROR << 16);
200 case SISL_AFU_RC_OUT_OF_DATA_BUFS:
202 scp->result = (DID_ALLOC_FAILURE << 16);
205 scp->result = (DID_ERROR << 16);
211 * cmd_complete() - command completion handler
212 * @cmd: AFU command that has completed.
214 * Prepares and submits command that has either completed or timed out to
215 * the SCSI stack. Checks AFU command back into command pool for non-internal
216 * (rcb.scp populated) commands.
218 static void cmd_complete(struct afu_cmd *cmd)
220 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.ioasc))
233 process_cmd_err(cmd, scp);
235 scp->result = (DID_OK << 16);
237 cmd_is_tmf = cmd->cmd_tmf;
238 cmd_checkin(cmd); /* Don't use cmd after here */
240 pr_debug_ratelimited("%s: calling scsi_done scp=%p result=%X "
241 "ioasc=%d\n", __func__, scp, scp->result,
248 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
249 cfg->tmf_active = false;
250 wake_up_all_locked(&cfg->tmf_waitq);
251 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
254 complete(&cmd->cevent);
258 * context_reset() - timeout handler for AFU commands
259 * @cmd: AFU command that timed out.
261 * Sends a reset to the AFU.
263 static void context_reset(struct afu_cmd *cmd)
268 struct afu *afu = cmd->parent;
271 pr_debug("%s: cmd=%p\n", __func__, cmd);
273 spin_lock_irqsave(&cmd->slock, lock_flags);
275 /* Already completed? */
276 if (cmd->sa.host_use_b[0] & B_DONE) {
277 spin_unlock_irqrestore(&cmd->slock, lock_flags);
281 cmd->sa.host_use_b[0] |= (B_DONE | B_ERROR | B_TIMEOUT);
282 spin_unlock_irqrestore(&cmd->slock, lock_flags);
285 * We really want to send this reset at all costs, so spread
286 * out wait time on successive retries for available room.
289 room = readq_be(&afu->host_map->cmd_room);
290 atomic64_set(&afu->room, room);
294 } while (nretry++ < MC_ROOM_RETRY_CNT);
296 pr_err("%s: no cmd_room to send reset\n", __func__);
301 writeq_be(rrin, &afu->host_map->ioarrin);
303 rrin = readq_be(&afu->host_map->ioarrin);
306 /* Double delay each time */
308 } while (nretry++ < MC_ROOM_RETRY_CNT);
312 * send_cmd() - sends an AFU command
313 * @afu: AFU associated with the host.
314 * @cmd: AFU command to send.
317 * 0 on success or SCSI_MLQUEUE_HOST_BUSY
319 static int send_cmd(struct afu *afu, struct afu_cmd *cmd)
321 struct cxlflash_cfg *cfg = afu->parent;
322 struct device *dev = &cfg->dev->dev;
329 * This routine is used by critical users such an AFU sync and to
330 * send a task management function (TMF). Thus we want to retry a
331 * bit before returning an error. To avoid the performance penalty
332 * of MMIO, we spread the update of 'room' over multiple commands.
335 newval = atomic64_dec_if_positive(&afu->room);
338 room = readq_be(&afu->host_map->cmd_room);
339 atomic64_set(&afu->room, room);
343 } while (nretry++ < MC_ROOM_RETRY_CNT);
345 dev_err(dev, "%s: no cmd_room to send 0x%X\n",
346 __func__, cmd->rcb.cdb[0]);
349 } else if (unlikely(newval < 0)) {
350 /* This should be rare. i.e. Only if two threads race and
351 * decrement before the MMIO read is done. In this case
352 * just benefit from the other thread having updated
355 if (nretry++ < MC_ROOM_RETRY_CNT) {
364 writeq_be((u64)&cmd->rcb, &afu->host_map->ioarrin);
366 pr_devel("%s: cmd=%p len=%d ea=%p rc=%d\n", __func__, cmd,
367 cmd->rcb.data_len, (void *)cmd->rcb.data_ea, rc);
371 afu->read_room = true;
372 schedule_work(&cfg->work_q);
373 rc = SCSI_MLQUEUE_HOST_BUSY;
378 * wait_resp() - polls for a response or timeout to a sent AFU command
379 * @afu: AFU associated with the host.
380 * @cmd: AFU command that was sent.
382 static void wait_resp(struct afu *afu, struct afu_cmd *cmd)
384 ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
386 timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
390 if (unlikely(cmd->sa.ioasc != 0))
391 pr_err("%s: CMD 0x%X failed, IOASC: flags 0x%X, afu_rc 0x%X, "
392 "scsi_rc 0x%X, fc_rc 0x%X\n", __func__, cmd->rcb.cdb[0],
393 cmd->sa.rc.flags, cmd->sa.rc.afu_rc, cmd->sa.rc.scsi_rc,
398 * send_tmf() - sends a Task Management Function (TMF)
399 * @afu: AFU to checkout from.
400 * @scp: SCSI command from stack.
401 * @tmfcmd: TMF command to send.
405 * SCSI_MLQUEUE_HOST_BUSY when host is busy
407 static int send_tmf(struct afu *afu, struct scsi_cmnd *scp, u64 tmfcmd)
411 u32 port_sel = scp->device->channel + 1;
413 struct Scsi_Host *host = scp->device->host;
414 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
415 struct device *dev = &cfg->dev->dev;
420 cmd = cmd_checkout(afu);
421 if (unlikely(!cmd)) {
422 dev_err(dev, "%s: could not get a free command\n", __func__);
423 rc = SCSI_MLQUEUE_HOST_BUSY;
427 /* When Task Management Function is active do not send another */
428 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
430 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
433 cfg->tmf_active = true;
435 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
437 cmd->rcb.ctx_id = afu->ctx_hndl;
438 cmd->rcb.port_sel = port_sel;
439 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
441 lflag = SISL_REQ_FLAGS_TMF_CMD;
443 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
444 SISL_REQ_FLAGS_SUP_UNDERRUN | lflag);
446 /* Stash the scp in the reserved field, for reuse during interrupt */
449 /* Copy the CDB from the cmd passed in */
450 memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
452 /* Send the command */
453 rc = send_cmd(afu, cmd);
456 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
457 cfg->tmf_active = false;
458 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
462 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
463 to = msecs_to_jiffies(5000);
464 to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
469 cfg->tmf_active = false;
470 dev_err(dev, "%s: TMF timed out!\n", __func__);
473 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
479 * cxlflash_driver_info() - information handler for this host driver
480 * @host: SCSI host associated with device.
482 * Return: A string describing the device.
484 static const char *cxlflash_driver_info(struct Scsi_Host *host)
486 return CXLFLASH_ADAPTER_NAME;
490 * cxlflash_queuecommand() - sends a mid-layer request
491 * @host: SCSI host associated with device.
492 * @scp: SCSI command to send.
496 * SCSI_MLQUEUE_HOST_BUSY when host is busy
498 static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
500 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
501 struct afu *afu = cfg->afu;
502 struct device *dev = &cfg->dev->dev;
504 u32 port_sel = scp->device->channel + 1;
506 struct scatterlist *sg;
511 dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
512 "cdb=(%08X-%08X-%08X-%08X)\n",
513 __func__, scp, 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]));
521 * If a Task Management Function is active, wait for it to complete
522 * before continuing with regular commands.
524 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
525 if (cfg->tmf_active) {
526 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
527 rc = SCSI_MLQUEUE_HOST_BUSY;
530 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
532 switch (cfg->state) {
534 dev_dbg_ratelimited(dev, "%s: device is in reset!\n", __func__);
535 rc = SCSI_MLQUEUE_HOST_BUSY;
538 dev_dbg_ratelimited(dev, "%s: device has failed!\n", __func__);
539 scp->result = (DID_NO_CONNECT << 16);
547 cmd = cmd_checkout(afu);
548 if (unlikely(!cmd)) {
549 dev_err(dev, "%s: could not get a free command\n", __func__);
550 rc = SCSI_MLQUEUE_HOST_BUSY;
554 cmd->rcb.ctx_id = afu->ctx_hndl;
555 cmd->rcb.port_sel = port_sel;
556 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
558 if (scp->sc_data_direction == DMA_TO_DEVICE)
559 lflag = SISL_REQ_FLAGS_HOST_WRITE;
561 lflag = SISL_REQ_FLAGS_HOST_READ;
563 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
564 SISL_REQ_FLAGS_SUP_UNDERRUN | lflag);
566 /* Stash the scp in the reserved field, for reuse during interrupt */
569 nseg = scsi_dma_map(scp);
570 if (unlikely(nseg < 0)) {
571 dev_err(dev, "%s: Fail DMA map! nseg=%d\n",
573 rc = SCSI_MLQUEUE_HOST_BUSY;
577 ncount = scsi_sg_count(scp);
578 scsi_for_each_sg(scp, sg, ncount, i) {
579 cmd->rcb.data_len = sg_dma_len(sg);
580 cmd->rcb.data_ea = sg_dma_address(sg);
583 /* Copy the CDB from the scsi_cmnd passed in */
584 memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
586 /* Send the command */
587 rc = send_cmd(afu, cmd);
594 pr_devel("%s: returning rc=%d\n", __func__, rc);
599 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
600 * @cxlflash: Internal structure associated with the host.
602 static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
604 struct pci_dev *pdev = cfg->dev;
606 if (pci_channel_offline(pdev))
607 wait_event_timeout(cfg->reset_waitq,
608 !pci_channel_offline(pdev),
609 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
613 * free_mem() - free memory associated with the AFU
614 * @cxlflash: Internal structure associated with the host.
616 static void free_mem(struct cxlflash_cfg *cfg)
620 struct afu *afu = cfg->afu;
623 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
624 buf = afu->cmd[i].buf;
625 if (!((u64)buf & (PAGE_SIZE - 1)))
626 free_page((ulong)buf);
629 free_pages((ulong)afu, get_order(sizeof(struct afu)));
635 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
636 * @cxlflash: Internal structure associated with the host.
638 * Safe to call with AFU in a partially allocated/initialized state.
640 static void stop_afu(struct cxlflash_cfg *cfg)
643 struct afu *afu = cfg->afu;
646 for (i = 0; i < CXLFLASH_NUM_CMDS; i++)
647 complete(&afu->cmd[i].cevent);
649 if (likely(afu->afu_map)) {
650 cxl_psa_unmap((void *)afu->afu_map);
657 * term_mc() - terminates the master context
658 * @cxlflash: Internal structure associated with the host.
659 * @level: Depth of allocation, where to begin waterfall tear down.
661 * Safe to call with AFU/MC in partially allocated/initialized state.
663 static void term_mc(struct cxlflash_cfg *cfg, enum undo_level level)
666 struct afu *afu = cfg->afu;
667 struct device *dev = &cfg->dev->dev;
669 if (!afu || !cfg->mcctx) {
670 dev_err(dev, "%s: returning from term_mc with NULL afu or MC\n",
677 rc = cxl_stop_context(cfg->mcctx);
680 cxl_unmap_afu_irq(cfg->mcctx, 3, afu);
682 cxl_unmap_afu_irq(cfg->mcctx, 2, afu);
684 cxl_unmap_afu_irq(cfg->mcctx, 1, afu);
686 cxl_free_afu_irqs(cfg->mcctx);
687 case RELEASE_CONTEXT:
693 * term_afu() - terminates the AFU
694 * @cxlflash: Internal structure associated with the host.
696 * Safe to call with AFU/MC in partially allocated/initialized state.
698 static void term_afu(struct cxlflash_cfg *cfg)
700 term_mc(cfg, UNDO_START);
705 pr_debug("%s: returning\n", __func__);
709 * cxlflash_remove() - PCI entry point to tear down host
710 * @pdev: PCI device associated with the host.
712 * Safe to use as a cleanup in partially allocated/initialized state.
714 static void cxlflash_remove(struct pci_dev *pdev)
716 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
719 /* If a Task Management Function is active, wait for it to complete
720 * before continuing with remove.
722 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
724 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
727 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
729 cfg->state = STATE_FAILTERM;
730 cxlflash_stop_term_user_contexts(cfg);
732 switch (cfg->init_state) {
733 case INIT_STATE_SCSI:
734 cxlflash_term_local_luns(cfg);
735 scsi_remove_host(cfg->host);
736 scsi_host_put(cfg->host);
741 pci_release_regions(cfg->dev);
742 pci_disable_device(pdev);
743 case INIT_STATE_NONE:
744 flush_work(&cfg->work_q);
749 pr_debug("%s: returning\n", __func__);
753 * alloc_mem() - allocates the AFU and its command pool
754 * @cxlflash: Internal structure associated with the host.
756 * A partially allocated state remains on failure.
760 * -ENOMEM on failure to allocate memory
762 static int alloc_mem(struct cxlflash_cfg *cfg)
767 struct device *dev = &cfg->dev->dev;
769 /* This allocation is about 12K, i.e. only 1 64k page
770 * and upto 4 4k pages
772 cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
773 get_order(sizeof(struct afu)));
774 if (unlikely(!cfg->afu)) {
775 dev_err(dev, "%s: cannot get %d free pages\n",
776 __func__, get_order(sizeof(struct afu)));
780 cfg->afu->parent = cfg;
781 cfg->afu->afu_map = NULL;
783 for (i = 0; i < CXLFLASH_NUM_CMDS; buf += CMD_BUFSIZE, i++) {
784 if (!((u64)buf & (PAGE_SIZE - 1))) {
785 buf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
786 if (unlikely(!buf)) {
788 "%s: Allocate command buffers fail!\n",
796 cfg->afu->cmd[i].buf = buf;
797 atomic_set(&cfg->afu->cmd[i].free, 1);
798 cfg->afu->cmd[i].slot = i;
806 * init_pci() - initializes the host as a PCI device
807 * @cxlflash: Internal structure associated with the host.
811 * -EIO on unable to communicate with device
812 * A return code from the PCI sub-routines
814 static int init_pci(struct cxlflash_cfg *cfg)
816 struct pci_dev *pdev = cfg->dev;
819 cfg->cxlflash_regs_pci = pci_resource_start(pdev, 0);
820 rc = pci_request_regions(pdev, CXLFLASH_NAME);
823 "%s: Couldn't register memory range of registers\n",
828 rc = pci_enable_device(pdev);
829 if (rc || pci_channel_offline(pdev)) {
830 if (pci_channel_offline(pdev)) {
831 cxlflash_wait_for_pci_err_recovery(cfg);
832 rc = pci_enable_device(pdev);
836 dev_err(&pdev->dev, "%s: Cannot enable adapter\n",
838 cxlflash_wait_for_pci_err_recovery(cfg);
839 goto out_release_regions;
843 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
845 dev_dbg(&pdev->dev, "%s: Failed to set 64 bit PCI DMA mask\n",
847 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
851 dev_err(&pdev->dev, "%s: Failed to set PCI DMA mask\n",
856 pci_set_master(pdev);
858 if (pci_channel_offline(pdev)) {
859 cxlflash_wait_for_pci_err_recovery(cfg);
860 if (pci_channel_offline(pdev)) {
862 goto out_msi_disable;
866 rc = pci_save_state(pdev);
868 if (rc != PCIBIOS_SUCCESSFUL) {
869 dev_err(&pdev->dev, "%s: Failed to save PCI config space\n",
876 pr_debug("%s: returning rc=%d\n", __func__, rc);
881 cxlflash_wait_for_pci_err_recovery(cfg);
883 pci_disable_device(pdev);
885 pci_release_regions(pdev);
891 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
892 * @cxlflash: Internal structure associated with the host.
896 * A return code from adding the host
898 static int init_scsi(struct cxlflash_cfg *cfg)
900 struct pci_dev *pdev = cfg->dev;
903 rc = scsi_add_host(cfg->host, &pdev->dev);
905 dev_err(&pdev->dev, "%s: scsi_add_host failed (rc=%d)\n",
910 scsi_scan_host(cfg->host);
913 pr_debug("%s: returning rc=%d\n", __func__, rc);
918 * set_port_online() - transitions the specified host FC port to online state
919 * @fc_regs: Top of MMIO region defined for specified port.
921 * The provided MMIO region must be mapped prior to call. Online state means
922 * that the FC link layer has synced, completed the handshaking process, and
923 * is ready for login to start.
925 static void set_port_online(u64 *fc_regs)
929 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
930 cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
931 cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
932 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
936 * set_port_offline() - transitions the specified host FC port to offline state
937 * @fc_regs: Top of MMIO region defined for specified port.
939 * The provided MMIO region must be mapped prior to call.
941 static void set_port_offline(u64 *fc_regs)
945 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
946 cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
947 cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
948 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
952 * wait_port_online() - waits for the specified host FC port come online
953 * @fc_regs: Top of MMIO region defined for specified port.
954 * @delay_us: Number of microseconds to delay between reading port status.
955 * @nretry: Number of cycles to retry reading port status.
957 * The provided MMIO region must be mapped prior to call. This will timeout
958 * when the cable is not plugged in.
961 * TRUE (1) when the specified port is online
962 * FALSE (0) when the specified port fails to come online after timeout
963 * -EINVAL when @delay_us is less than 1000
965 static int wait_port_online(u64 *fc_regs, u32 delay_us, u32 nretry)
969 if (delay_us < 1000) {
970 pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
975 msleep(delay_us / 1000);
976 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
977 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
980 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
984 * wait_port_offline() - waits for the specified host FC port go offline
985 * @fc_regs: Top of MMIO region defined for specified port.
986 * @delay_us: Number of microseconds to delay between reading port status.
987 * @nretry: Number of cycles to retry reading port status.
989 * The provided MMIO region must be mapped prior to call.
992 * TRUE (1) when the specified port is offline
993 * FALSE (0) when the specified port fails to go offline after timeout
994 * -EINVAL when @delay_us is less than 1000
996 static int wait_port_offline(u64 *fc_regs, u32 delay_us, u32 nretry)
1000 if (delay_us < 1000) {
1001 pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
1006 msleep(delay_us / 1000);
1007 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
1008 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
1011 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
1015 * afu_set_wwpn() - configures the WWPN for the specified host FC port
1016 * @afu: AFU associated with the host that owns the specified FC port.
1017 * @port: Port number being configured.
1018 * @fc_regs: Top of MMIO region defined for specified port.
1019 * @wwpn: The world-wide-port-number previously discovered for port.
1021 * The provided MMIO region must be mapped prior to call. As part of the
1022 * sequence to configure the WWPN, the port is toggled offline and then back
1023 * online. This toggling action can cause this routine to delay up to a few
1024 * seconds. When configured to use the internal LUN feature of the AFU, a
1025 * failure to come online is overridden.
1028 * 0 when the WWPN is successfully written and the port comes back online
1029 * -1 when the port fails to go offline or come back up online
1031 static int afu_set_wwpn(struct afu *afu, int port, u64 *fc_regs, u64 wwpn)
1035 set_port_offline(fc_regs);
1037 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1038 FC_PORT_STATUS_RETRY_CNT)) {
1039 pr_debug("%s: wait on port %d to go offline timed out\n",
1041 ret = -1; /* but continue on to leave the port back online */
1045 writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
1047 set_port_online(fc_regs);
1049 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1050 FC_PORT_STATUS_RETRY_CNT)) {
1051 pr_debug("%s: wait on port %d to go online timed out\n",
1056 * Override for internal lun!!!
1058 if (afu->internal_lun) {
1059 pr_debug("%s: Overriding port %d online timeout!!!\n",
1065 pr_debug("%s: returning rc=%d\n", __func__, ret);
1071 * afu_link_reset() - resets the specified host FC port
1072 * @afu: AFU associated with the host that owns the specified FC port.
1073 * @port: Port number being configured.
1074 * @fc_regs: Top of MMIO region defined for specified port.
1076 * The provided MMIO region must be mapped prior to call. The sequence to
1077 * reset the port involves toggling it offline and then back online. This
1078 * action can cause this routine to delay up to a few seconds. An effort
1079 * is made to maintain link with the device by switching to host to use
1080 * the alternate port exclusively while the reset takes place.
1081 * failure to come online is overridden.
1083 static void afu_link_reset(struct afu *afu, int port, u64 *fc_regs)
1087 /* first switch the AFU to the other links, if any */
1088 port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
1089 port_sel &= ~(1ULL << port);
1090 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1091 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1093 set_port_offline(fc_regs);
1094 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1095 FC_PORT_STATUS_RETRY_CNT))
1096 pr_err("%s: wait on port %d to go offline timed out\n",
1099 set_port_online(fc_regs);
1100 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1101 FC_PORT_STATUS_RETRY_CNT))
1102 pr_err("%s: wait on port %d to go online timed out\n",
1105 /* switch back to include this port */
1106 port_sel |= (1ULL << port);
1107 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1108 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1110 pr_debug("%s: returning port_sel=%lld\n", __func__, port_sel);
1114 * Asynchronous interrupt information table
1116 static const struct asyc_intr_info ainfo[] = {
1117 {SISL_ASTATUS_FC0_OTHER, "other error", 0, CLR_FC_ERROR | LINK_RESET},
1118 {SISL_ASTATUS_FC0_LOGO, "target initiated LOGO", 0, 0},
1119 {SISL_ASTATUS_FC0_CRC_T, "CRC threshold exceeded", 0, LINK_RESET},
1120 {SISL_ASTATUS_FC0_LOGI_R, "login timed out, retrying", 0, 0},
1121 {SISL_ASTATUS_FC0_LOGI_F, "login failed", 0, CLR_FC_ERROR},
1122 {SISL_ASTATUS_FC0_LOGI_S, "login succeeded", 0, SCAN_HOST},
1123 {SISL_ASTATUS_FC0_LINK_DN, "link down", 0, 0},
1124 {SISL_ASTATUS_FC0_LINK_UP, "link up", 0, SCAN_HOST},
1125 {SISL_ASTATUS_FC1_OTHER, "other error", 1, CLR_FC_ERROR | LINK_RESET},
1126 {SISL_ASTATUS_FC1_LOGO, "target initiated LOGO", 1, 0},
1127 {SISL_ASTATUS_FC1_CRC_T, "CRC threshold exceeded", 1, LINK_RESET},
1128 {SISL_ASTATUS_FC1_LOGI_R, "login timed out, retrying", 1, 0},
1129 {SISL_ASTATUS_FC1_LOGI_F, "login failed", 1, CLR_FC_ERROR},
1130 {SISL_ASTATUS_FC1_LOGI_S, "login succeeded", 1, SCAN_HOST},
1131 {SISL_ASTATUS_FC1_LINK_DN, "link down", 1, 0},
1132 {SISL_ASTATUS_FC1_LINK_UP, "link up", 1, SCAN_HOST},
1133 {0x0, "", 0, 0} /* terminator */
1137 * find_ainfo() - locates and returns asynchronous interrupt information
1138 * @status: Status code set by AFU on error.
1140 * Return: The located information or NULL when the status code is invalid.
1142 static const struct asyc_intr_info *find_ainfo(u64 status)
1144 const struct asyc_intr_info *info;
1146 for (info = &ainfo[0]; info->status; info++)
1147 if (info->status == status)
1154 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1155 * @afu: AFU associated with the host.
1157 static void afu_err_intr_init(struct afu *afu)
1162 /* global async interrupts: AFU clears afu_ctrl on context exit
1163 * if async interrupts were sent to that context. This prevents
1164 * the AFU form sending further async interrupts when
1166 * nobody to receive them.
1170 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
1171 /* set LISN# to send and point to master context */
1172 reg = ((u64) (((afu->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
1174 if (afu->internal_lun)
1175 reg |= 1; /* Bit 63 indicates local lun */
1176 writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1178 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1179 /* unmask bits that are of interest */
1180 /* note: afu can send an interrupt after this step */
1181 writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1182 /* clear again in case a bit came on after previous clear but before */
1184 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1186 /* Clear/Set internal lun bits */
1187 reg = readq_be(&afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1188 reg &= SISL_FC_INTERNAL_MASK;
1189 if (afu->internal_lun)
1190 reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
1191 writeq_be(reg, &afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1193 /* now clear FC errors */
1194 for (i = 0; i < NUM_FC_PORTS; i++) {
1195 writeq_be(0xFFFFFFFFU,
1196 &afu->afu_map->global.fc_regs[i][FC_ERROR / 8]);
1197 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRCAP / 8]);
1200 /* sync interrupts for master's IOARRIN write */
1201 /* note that unlike asyncs, there can be no pending sync interrupts */
1202 /* at this time (this is a fresh context and master has not written */
1203 /* IOARRIN yet), so there is nothing to clear. */
1205 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1206 writeq_be(SISL_MSI_SYNC_ERROR, &afu->host_map->ctx_ctrl);
1207 writeq_be(SISL_ISTATUS_MASK, &afu->host_map->intr_mask);
1211 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1212 * @irq: Interrupt number.
1213 * @data: Private data provided at interrupt registration, the AFU.
1215 * Return: Always return IRQ_HANDLED.
1217 static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1219 struct afu *afu = (struct afu *)data;
1223 reg = readq_be(&afu->host_map->intr_status);
1224 reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1226 if (reg_unmasked == 0UL) {
1227 pr_err("%s: %llX: spurious interrupt, intr_status %016llX\n",
1228 __func__, (u64)afu, reg);
1229 goto cxlflash_sync_err_irq_exit;
1232 pr_err("%s: %llX: unexpected interrupt, intr_status %016llX\n",
1233 __func__, (u64)afu, reg);
1235 writeq_be(reg_unmasked, &afu->host_map->intr_clear);
1237 cxlflash_sync_err_irq_exit:
1238 pr_debug("%s: returning rc=%d\n", __func__, IRQ_HANDLED);
1243 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1244 * @irq: Interrupt number.
1245 * @data: Private data provided at interrupt registration, the AFU.
1247 * Return: Always return IRQ_HANDLED.
1249 static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
1251 struct afu *afu = (struct afu *)data;
1252 struct afu_cmd *cmd;
1253 bool toggle = afu->toggle;
1255 *hrrq_start = afu->hrrq_start,
1256 *hrrq_end = afu->hrrq_end,
1257 *hrrq_curr = afu->hrrq_curr;
1259 /* Process however many RRQ entries that are ready */
1263 if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1266 cmd = (struct afu_cmd *)(entry & ~SISL_RESP_HANDLE_T_BIT);
1269 /* Advance to next entry or wrap and flip the toggle bit */
1270 if (hrrq_curr < hrrq_end)
1273 hrrq_curr = hrrq_start;
1274 toggle ^= SISL_RESP_HANDLE_T_BIT;
1278 afu->hrrq_curr = hrrq_curr;
1279 afu->toggle = toggle;
1285 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1286 * @irq: Interrupt number.
1287 * @data: Private data provided at interrupt registration, the AFU.
1289 * Return: Always return IRQ_HANDLED.
1291 static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1293 struct afu *afu = (struct afu *)data;
1294 struct cxlflash_cfg *cfg = afu->parent;
1295 struct device *dev = &cfg->dev->dev;
1297 const struct asyc_intr_info *info;
1298 struct sisl_global_map *global = &afu->afu_map->global;
1303 reg = readq_be(&global->regs.aintr_status);
1304 reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1306 if (reg_unmasked == 0) {
1307 dev_err(dev, "%s: spurious interrupt, aintr_status 0x%016llX\n",
1312 /* it is OK to clear AFU status before FC_ERROR */
1313 writeq_be(reg_unmasked, &global->regs.aintr_clear);
1315 /* check each bit that is on */
1316 for (i = 0; reg_unmasked; i++, reg_unmasked = (reg_unmasked >> 1)) {
1317 info = find_ainfo(1ULL << i);
1318 if (((reg_unmasked & 0x1) == 0) || !info)
1323 dev_err(dev, "%s: FC Port %d -> %s, fc_status 0x%08llX\n",
1324 __func__, port, info->desc,
1325 readq_be(&global->fc_regs[port][FC_STATUS / 8]));
1328 * do link reset first, some OTHER errors will set FC_ERROR
1329 * again if cleared before or w/o a reset
1331 if (info->action & LINK_RESET) {
1332 dev_err(dev, "%s: FC Port %d: resetting link\n",
1334 cfg->lr_state = LINK_RESET_REQUIRED;
1335 cfg->lr_port = port;
1336 schedule_work(&cfg->work_q);
1339 if (info->action & CLR_FC_ERROR) {
1340 reg = readq_be(&global->fc_regs[port][FC_ERROR / 8]);
1343 * since all errors are unmasked, FC_ERROR and FC_ERRCAP
1344 * should be the same and tracing one is sufficient.
1347 dev_err(dev, "%s: fc %d: clearing fc_error 0x%08llX\n",
1348 __func__, port, reg);
1350 writeq_be(reg, &global->fc_regs[port][FC_ERROR / 8]);
1351 writeq_be(0, &global->fc_regs[port][FC_ERRCAP / 8]);
1354 if (info->action & SCAN_HOST) {
1355 atomic_inc(&cfg->scan_host_needed);
1356 schedule_work(&cfg->work_q);
1361 dev_dbg(dev, "%s: returning IRQ_HANDLED, afu=%p\n", __func__, afu);
1366 * start_context() - starts the master context
1367 * @cxlflash: Internal structure associated with the host.
1369 * Return: A success or failure value from CXL services.
1371 static int start_context(struct cxlflash_cfg *cfg)
1375 rc = cxl_start_context(cfg->mcctx,
1376 cfg->afu->work.work_element_descriptor,
1379 pr_debug("%s: returning rc=%d\n", __func__, rc);
1384 * read_vpd() - obtains the WWPNs from VPD
1385 * @cxlflash: Internal structure associated with the host.
1386 * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
1390 * -ENODEV when VPD or WWPN keywords not found
1392 static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1394 struct pci_dev *dev = cfg->parent_dev;
1396 int ro_start, ro_size, i, j, k;
1398 char vpd_data[CXLFLASH_VPD_LEN];
1399 char tmp_buf[WWPN_BUF_LEN] = { 0 };
1400 char *wwpn_vpd_tags[NUM_FC_PORTS] = { "V5", "V6" };
1402 /* Get the VPD data from the device */
1403 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
1404 if (unlikely(vpd_size <= 0)) {
1405 dev_err(&dev->dev, "%s: Unable to read VPD (size = %ld)\n",
1406 __func__, vpd_size);
1411 /* Get the read only section offset */
1412 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
1413 PCI_VPD_LRDT_RO_DATA);
1414 if (unlikely(ro_start < 0)) {
1415 dev_err(&dev->dev, "%s: VPD Read-only data not found\n",
1421 /* Get the read only section size, cap when extends beyond read VPD */
1422 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1424 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1425 if (unlikely((i + j) > vpd_size)) {
1426 pr_debug("%s: Might need to read more VPD (%d > %ld)\n",
1427 __func__, (i + j), vpd_size);
1428 ro_size = vpd_size - i;
1432 * Find the offset of the WWPN tag within the read only
1433 * VPD data and validate the found field (partials are
1434 * no good to us). Convert the ASCII data to an integer
1435 * value. Note that we must copy to a temporary buffer
1436 * because the conversion service requires that the ASCII
1437 * string be terminated.
1439 for (k = 0; k < NUM_FC_PORTS; k++) {
1441 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1443 i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
1444 if (unlikely(i < 0)) {
1445 dev_err(&dev->dev, "%s: Port %d WWPN not found "
1446 "in VPD\n", __func__, k);
1451 j = pci_vpd_info_field_size(&vpd_data[i]);
1452 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1453 if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
1454 dev_err(&dev->dev, "%s: Port %d WWPN incomplete or "
1461 memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1462 rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1464 dev_err(&dev->dev, "%s: Fail to convert port %d WWPN "
1465 "to integer\n", __func__, k);
1472 pr_debug("%s: returning rc=%d\n", __func__, rc);
1477 * init_pcr() - initialize the provisioning and control registers
1478 * @cxlflash: Internal structure associated with the host.
1480 * Also sets up fast access to the mapped registers and initializes AFU
1481 * command fields that never change.
1483 static void init_pcr(struct cxlflash_cfg *cfg)
1485 struct afu *afu = cfg->afu;
1486 struct sisl_ctrl_map *ctrl_map;
1489 for (i = 0; i < MAX_CONTEXT; i++) {
1490 ctrl_map = &afu->afu_map->ctrls[i].ctrl;
1491 /* disrupt any clients that could be running */
1492 /* e. g. clients that survived a master restart */
1493 writeq_be(0, &ctrl_map->rht_start);
1494 writeq_be(0, &ctrl_map->rht_cnt_id);
1495 writeq_be(0, &ctrl_map->ctx_cap);
1498 /* copy frequently used fields into afu */
1499 afu->ctx_hndl = (u16) cxl_process_element(cfg->mcctx);
1500 /* ctx_hndl is 16 bits in CAIA */
1501 afu->host_map = &afu->afu_map->hosts[afu->ctx_hndl].host;
1502 afu->ctrl_map = &afu->afu_map->ctrls[afu->ctx_hndl].ctrl;
1504 /* Program the Endian Control for the master context */
1505 writeq_be(SISL_ENDIAN_CTRL, &afu->host_map->endian_ctrl);
1507 /* initialize cmd fields that never change */
1508 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
1509 afu->cmd[i].rcb.ctx_id = afu->ctx_hndl;
1510 afu->cmd[i].rcb.msi = SISL_MSI_RRQ_UPDATED;
1511 afu->cmd[i].rcb.rrq = 0x0;
1516 * init_global() - initialize AFU global registers
1517 * @cxlflash: Internal structure associated with the host.
1519 static int init_global(struct cxlflash_cfg *cfg)
1521 struct afu *afu = cfg->afu;
1522 struct device *dev = &cfg->dev->dev;
1523 u64 wwpn[NUM_FC_PORTS]; /* wwpn of AFU ports */
1524 int i = 0, num_ports = 0;
1528 rc = read_vpd(cfg, &wwpn[0]);
1530 dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
1534 pr_debug("%s: wwpn0=0x%llX wwpn1=0x%llX\n", __func__, wwpn[0], wwpn[1]);
1536 /* set up RRQ in AFU for master issued cmds */
1537 writeq_be((u64) afu->hrrq_start, &afu->host_map->rrq_start);
1538 writeq_be((u64) afu->hrrq_end, &afu->host_map->rrq_end);
1540 /* AFU configuration */
1541 reg = readq_be(&afu->afu_map->global.regs.afu_config);
1542 reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1543 /* enable all auto retry options and control endianness */
1544 /* leave others at default: */
1545 /* CTX_CAP write protected, mbox_r does not clear on read and */
1546 /* checker on if dual afu */
1547 writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1549 /* global port select: select either port */
1550 if (afu->internal_lun) {
1551 /* only use port 0 */
1552 writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
1553 num_ports = NUM_FC_PORTS - 1;
1555 writeq_be(BOTH_PORTS, &afu->afu_map->global.regs.afu_port_sel);
1556 num_ports = NUM_FC_PORTS;
1559 for (i = 0; i < num_ports; i++) {
1560 /* unmask all errors (but they are still masked at AFU) */
1561 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRMSK / 8]);
1562 /* clear CRC error cnt & set a threshold */
1563 (void)readq_be(&afu->afu_map->global.
1564 fc_regs[i][FC_CNT_CRCERR / 8]);
1565 writeq_be(MC_CRC_THRESH, &afu->afu_map->global.fc_regs[i]
1566 [FC_CRC_THRESH / 8]);
1568 /* set WWPNs. If already programmed, wwpn[i] is 0 */
1570 afu_set_wwpn(afu, i,
1571 &afu->afu_map->global.fc_regs[i][0],
1573 dev_err(dev, "%s: failed to set WWPN on port %d\n",
1578 /* Programming WWPN back to back causes additional
1579 * offline/online transitions and a PLOGI
1585 /* set up master's own CTX_CAP to allow real mode, host translation */
1586 /* tbls, afu cmds and read/write GSCSI cmds. */
1587 /* First, unlock ctx_cap write by reading mbox */
1588 (void)readq_be(&afu->ctrl_map->mbox_r); /* unlock ctx_cap */
1589 writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1590 SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1591 SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1592 &afu->ctrl_map->ctx_cap);
1593 /* init heartbeat */
1594 afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
1601 * start_afu() - initializes and starts the AFU
1602 * @cxlflash: Internal structure associated with the host.
1604 static int start_afu(struct cxlflash_cfg *cfg)
1606 struct afu *afu = cfg->afu;
1607 struct afu_cmd *cmd;
1612 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
1615 init_completion(&cmd->cevent);
1616 spin_lock_init(&cmd->slock);
1622 /* initialize RRQ pointers */
1623 afu->hrrq_start = &afu->rrq_entry[0];
1624 afu->hrrq_end = &afu->rrq_entry[NUM_RRQ_ENTRY - 1];
1625 afu->hrrq_curr = afu->hrrq_start;
1628 rc = init_global(cfg);
1630 pr_debug("%s: returning rc=%d\n", __func__, rc);
1635 * init_mc() - create and register as the master context
1636 * @cxlflash: Internal structure associated with the host.
1640 * -ENOMEM when unable to obtain a context from CXL services
1641 * A failure value from CXL services.
1643 static int init_mc(struct cxlflash_cfg *cfg)
1645 struct cxl_context *ctx;
1646 struct device *dev = &cfg->dev->dev;
1647 struct afu *afu = cfg->afu;
1649 enum undo_level level;
1651 ctx = cxl_get_context(cfg->dev);
1656 /* Set it up as a master with the CXL */
1657 cxl_set_master(ctx);
1659 /* During initialization reset the AFU to start from a clean slate */
1660 rc = cxl_afu_reset(cfg->mcctx);
1662 dev_err(dev, "%s: initial AFU reset failed rc=%d\n",
1664 level = RELEASE_CONTEXT;
1668 rc = cxl_allocate_afu_irqs(ctx, 3);
1670 dev_err(dev, "%s: call to allocate_afu_irqs failed rc=%d!\n",
1672 level = RELEASE_CONTEXT;
1676 rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, afu,
1677 "SISL_MSI_SYNC_ERROR");
1678 if (unlikely(rc <= 0)) {
1679 dev_err(dev, "%s: IRQ 1 (SISL_MSI_SYNC_ERROR) map failed!\n",
1685 rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, afu,
1686 "SISL_MSI_RRQ_UPDATED");
1687 if (unlikely(rc <= 0)) {
1688 dev_err(dev, "%s: IRQ 2 (SISL_MSI_RRQ_UPDATED) map failed!\n",
1694 rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, afu,
1695 "SISL_MSI_ASYNC_ERROR");
1696 if (unlikely(rc <= 0)) {
1697 dev_err(dev, "%s: IRQ 3 (SISL_MSI_ASYNC_ERROR) map failed!\n",
1705 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1706 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1707 * element (pe) that is embedded in the context (ctx)
1709 rc = start_context(cfg);
1711 dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
1712 level = UNMAP_THREE;
1716 pr_debug("%s: returning rc=%d\n", __func__, rc);
1719 term_mc(cfg, level);
1724 * init_afu() - setup as master context and start AFU
1725 * @cxlflash: Internal structure associated with the host.
1727 * This routine is a higher level of control for configuring the
1728 * AFU on probe and reset paths.
1732 * -ENOMEM when unable to map the AFU MMIO space
1733 * A failure value from internal services.
1735 static int init_afu(struct cxlflash_cfg *cfg)
1739 struct afu *afu = cfg->afu;
1740 struct device *dev = &cfg->dev->dev;
1742 cxl_perst_reloads_same_image(cfg->cxl_afu, true);
1746 dev_err(dev, "%s: call to init_mc failed, rc=%d!\n",
1751 /* Map the entire MMIO space of the AFU.
1753 afu->afu_map = cxl_psa_map(cfg->mcctx);
1754 if (!afu->afu_map) {
1756 term_mc(cfg, UNDO_START);
1757 dev_err(dev, "%s: call to cxl_psa_map failed!\n", __func__);
1761 /* don't byte reverse on reading afu_version, else the string form */
1762 /* will be backwards */
1763 reg = afu->afu_map->global.regs.afu_version;
1764 memcpy(afu->version, ®, 8);
1765 afu->interface_version =
1766 readq_be(&afu->afu_map->global.regs.interface_version);
1767 pr_debug("%s: afu version %s, interface version 0x%llX\n",
1768 __func__, afu->version, afu->interface_version);
1770 rc = start_afu(cfg);
1772 dev_err(dev, "%s: call to start_afu failed, rc=%d!\n",
1774 term_mc(cfg, UNDO_START);
1775 cxl_psa_unmap((void *)afu->afu_map);
1776 afu->afu_map = NULL;
1780 afu_err_intr_init(cfg->afu);
1781 atomic64_set(&afu->room, readq_be(&afu->host_map->cmd_room));
1783 /* Restore the LUN mappings */
1784 cxlflash_restore_luntable(cfg);
1786 pr_debug("%s: returning rc=%d\n", __func__, rc);
1791 * cxlflash_afu_sync() - builds and sends an AFU sync command
1792 * @afu: AFU associated with the host.
1793 * @ctx_hndl_u: Identifies context requesting sync.
1794 * @res_hndl_u: Identifies resource requesting sync.
1795 * @mode: Type of sync to issue (lightweight, heavyweight, global).
1797 * The AFU can only take 1 sync command at a time. This routine enforces this
1798 * limitation by using a mutex to provide exlusive access to the AFU during
1799 * the sync. This design point requires calling threads to not be on interrupt
1800 * context due to the possibility of sleeping during concurrent sync operations.
1802 * AFU sync operations are only necessary and allowed when the device is
1803 * operating normally. When not operating normally, sync requests can occur as
1804 * part of cleaning up resources associated with an adapter prior to removal.
1805 * In this scenario, these requests are simply ignored (safe due to the AFU
1812 int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
1813 res_hndl_t res_hndl_u, u8 mode)
1815 struct cxlflash_cfg *cfg = afu->parent;
1816 struct device *dev = &cfg->dev->dev;
1817 struct afu_cmd *cmd = NULL;
1820 static DEFINE_MUTEX(sync_active);
1822 if (cfg->state != STATE_NORMAL) {
1823 pr_debug("%s: Sync not required! (%u)\n", __func__, cfg->state);
1827 mutex_lock(&sync_active);
1829 cmd = cmd_checkout(afu);
1830 if (unlikely(!cmd)) {
1832 udelay(1000 * retry_cnt);
1833 if (retry_cnt < MC_RETRY_CNT)
1835 dev_err(dev, "%s: could not get a free command\n", __func__);
1840 pr_debug("%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
1842 memset(cmd->rcb.cdb, 0, sizeof(cmd->rcb.cdb));
1844 cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
1845 cmd->rcb.port_sel = 0x0; /* NA */
1846 cmd->rcb.lun_id = 0x0; /* NA */
1847 cmd->rcb.data_len = 0x0;
1848 cmd->rcb.data_ea = 0x0;
1849 cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
1851 cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
1852 cmd->rcb.cdb[1] = mode;
1854 /* The cdb is aligned, no unaligned accessors required */
1855 *((u16 *)&cmd->rcb.cdb[2]) = swab16(ctx_hndl_u);
1856 *((u32 *)&cmd->rcb.cdb[4]) = swab32(res_hndl_u);
1858 rc = send_cmd(afu, cmd);
1862 wait_resp(afu, cmd);
1864 /* set on timeout */
1865 if (unlikely((cmd->sa.ioasc != 0) ||
1866 (cmd->sa.host_use_b[0] & B_ERROR)))
1869 mutex_unlock(&sync_active);
1872 pr_debug("%s: returning rc=%d\n", __func__, rc);
1877 * afu_reset() - resets the AFU
1878 * @cfg: Internal structure associated with the host.
1882 * A failure value from internal services.
1884 static int afu_reset(struct cxlflash_cfg *cfg)
1887 /* Stop the context before the reset. Since the context is
1888 * no longer available restart it after the reset is complete
1895 pr_debug("%s: returning rc=%d\n", __func__, rc);
1900 * cxlflash_eh_device_reset_handler() - reset a single LUN
1901 * @scp: SCSI command to send.
1904 * SUCCESS as defined in scsi/scsi.h
1905 * FAILED as defined in scsi/scsi.h
1907 static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
1910 struct Scsi_Host *host = scp->device->host;
1911 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
1912 struct afu *afu = cfg->afu;
1915 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
1916 "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
1917 host->host_no, scp->device->channel,
1918 scp->device->id, scp->device->lun,
1919 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1920 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1921 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1922 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
1924 switch (cfg->state) {
1926 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
1931 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
1932 if (cfg->state == STATE_NORMAL)
1940 pr_debug("%s: returning rc=%d\n", __func__, rc);
1945 * cxlflash_eh_host_reset_handler() - reset the host adapter
1946 * @scp: SCSI command from stack identifying host.
1949 * SUCCESS as defined in scsi/scsi.h
1950 * FAILED as defined in scsi/scsi.h
1952 static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
1956 struct Scsi_Host *host = scp->device->host;
1957 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
1959 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
1960 "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
1961 host->host_no, scp->device->channel,
1962 scp->device->id, scp->device->lun,
1963 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1964 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1965 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1966 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
1968 switch (cfg->state) {
1970 cfg->state = STATE_RESET;
1971 scsi_block_requests(cfg->host);
1972 cxlflash_mark_contexts_error(cfg);
1973 rcr = afu_reset(cfg);
1976 cfg->state = STATE_FAILTERM;
1978 cfg->state = STATE_NORMAL;
1979 wake_up_all(&cfg->reset_waitq);
1980 scsi_unblock_requests(cfg->host);
1983 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
1984 if (cfg->state == STATE_NORMAL)
1992 pr_debug("%s: returning rc=%d\n", __func__, rc);
1997 * cxlflash_change_queue_depth() - change the queue depth for the device
1998 * @sdev: SCSI device destined for queue depth change.
1999 * @qdepth: Requested queue depth value to set.
2001 * The requested queue depth is capped to the maximum supported value.
2003 * Return: The actual queue depth set.
2005 static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
2008 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
2009 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
2011 scsi_change_queue_depth(sdev, qdepth);
2012 return sdev->queue_depth;
2016 * cxlflash_show_port_status() - queries and presents the current port status
2017 * @port: Desired port for status reporting.
2018 * @afu: AFU owning the specified port.
2019 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2021 * Return: The size of the ASCII string returned in @buf.
2023 static ssize_t cxlflash_show_port_status(u32 port, struct afu *afu, char *buf)
2027 __be64 __iomem *fc_regs;
2029 if (port >= NUM_FC_PORTS)
2032 fc_regs = &afu->afu_map->global.fc_regs[port][0];
2033 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
2034 status &= FC_MTIP_STATUS_MASK;
2036 if (status == FC_MTIP_STATUS_ONLINE)
2037 disp_status = "online";
2038 else if (status == FC_MTIP_STATUS_OFFLINE)
2039 disp_status = "offline";
2041 disp_status = "unknown";
2043 return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
2047 * port0_show() - queries and presents the current status of port 0
2048 * @dev: Generic device associated with the host owning the port.
2049 * @attr: Device attribute representing the port.
2050 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2052 * Return: The size of the ASCII string returned in @buf.
2054 static ssize_t port0_show(struct device *dev,
2055 struct device_attribute *attr,
2058 struct Scsi_Host *shost = class_to_shost(dev);
2059 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2060 struct afu *afu = cfg->afu;
2062 return cxlflash_show_port_status(0, afu, buf);
2066 * port1_show() - queries and presents the current status of port 1
2067 * @dev: Generic device associated with the host owning the port.
2068 * @attr: Device attribute representing the port.
2069 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2071 * Return: The size of the ASCII string returned in @buf.
2073 static ssize_t port1_show(struct device *dev,
2074 struct device_attribute *attr,
2077 struct Scsi_Host *shost = class_to_shost(dev);
2078 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2079 struct afu *afu = cfg->afu;
2081 return cxlflash_show_port_status(1, afu, buf);
2085 * lun_mode_show() - presents the current LUN mode of the host
2086 * @dev: Generic device associated with the host.
2087 * @attr: Device attribute representing the LUN mode.
2088 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2090 * Return: The size of the ASCII string returned in @buf.
2092 static ssize_t lun_mode_show(struct device *dev,
2093 struct device_attribute *attr, char *buf)
2095 struct Scsi_Host *shost = class_to_shost(dev);
2096 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2097 struct afu *afu = cfg->afu;
2099 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
2103 * lun_mode_store() - sets the LUN mode of the host
2104 * @dev: Generic device associated with the host.
2105 * @attr: Device attribute representing the LUN mode.
2106 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2107 * @count: Length of data resizing in @buf.
2109 * The CXL Flash AFU supports a dummy LUN mode where the external
2110 * links and storage are not required. Space on the FPGA is used
2111 * to create 1 or 2 small LUNs which are presented to the system
2112 * as if they were a normal storage device. This feature is useful
2113 * during development and also provides manufacturing with a way
2114 * to test the AFU without an actual device.
2116 * 0 = external LUN[s] (default)
2117 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2118 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2119 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2120 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2122 * Return: The size of the ASCII string returned in @buf.
2124 static ssize_t lun_mode_store(struct device *dev,
2125 struct device_attribute *attr,
2126 const char *buf, size_t count)
2128 struct Scsi_Host *shost = class_to_shost(dev);
2129 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2130 struct afu *afu = cfg->afu;
2134 rc = kstrtouint(buf, 10, &lun_mode);
2135 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
2136 afu->internal_lun = lun_mode;
2138 scsi_scan_host(cfg->host);
2145 * ioctl_version_show() - presents the current ioctl version of the host
2146 * @dev: Generic device associated with the host.
2147 * @attr: Device attribute representing the ioctl version.
2148 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2150 * Return: The size of the ASCII string returned in @buf.
2152 static ssize_t ioctl_version_show(struct device *dev,
2153 struct device_attribute *attr, char *buf)
2155 return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
2159 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2160 * @port: Desired port for status reporting.
2161 * @afu: AFU owning the specified port.
2162 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2164 * Return: The size of the ASCII string returned in @buf.
2166 static ssize_t cxlflash_show_port_lun_table(u32 port,
2172 __be64 __iomem *fc_port;
2174 if (port >= NUM_FC_PORTS)
2177 fc_port = &afu->afu_map->global.fc_port[port][0];
2179 for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2180 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
2181 "%03d: %016llX\n", i, readq_be(&fc_port[i]));
2186 * port0_lun_table_show() - presents the current LUN table of port 0
2187 * @dev: Generic device associated with the host owning the port.
2188 * @attr: Device attribute representing the port.
2189 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2191 * Return: The size of the ASCII string returned in @buf.
2193 static ssize_t port0_lun_table_show(struct device *dev,
2194 struct device_attribute *attr,
2197 struct Scsi_Host *shost = class_to_shost(dev);
2198 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2199 struct afu *afu = cfg->afu;
2201 return cxlflash_show_port_lun_table(0, afu, buf);
2205 * port1_lun_table_show() - presents the current LUN table of port 1
2206 * @dev: Generic device associated with the host owning the port.
2207 * @attr: Device attribute representing the port.
2208 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2210 * Return: The size of the ASCII string returned in @buf.
2212 static ssize_t port1_lun_table_show(struct device *dev,
2213 struct device_attribute *attr,
2216 struct Scsi_Host *shost = class_to_shost(dev);
2217 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2218 struct afu *afu = cfg->afu;
2220 return cxlflash_show_port_lun_table(1, afu, buf);
2224 * mode_show() - presents the current mode of the device
2225 * @dev: Generic device associated with the device.
2226 * @attr: Device attribute representing the device mode.
2227 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2229 * Return: The size of the ASCII string returned in @buf.
2231 static ssize_t mode_show(struct device *dev,
2232 struct device_attribute *attr, char *buf)
2234 struct scsi_device *sdev = to_scsi_device(dev);
2236 return scnprintf(buf, PAGE_SIZE, "%s\n",
2237 sdev->hostdata ? "superpipe" : "legacy");
2243 static DEVICE_ATTR_RO(port0);
2244 static DEVICE_ATTR_RO(port1);
2245 static DEVICE_ATTR_RW(lun_mode);
2246 static DEVICE_ATTR_RO(ioctl_version);
2247 static DEVICE_ATTR_RO(port0_lun_table);
2248 static DEVICE_ATTR_RO(port1_lun_table);
2250 static struct device_attribute *cxlflash_host_attrs[] = {
2254 &dev_attr_ioctl_version,
2255 &dev_attr_port0_lun_table,
2256 &dev_attr_port1_lun_table,
2263 static DEVICE_ATTR_RO(mode);
2265 static struct device_attribute *cxlflash_dev_attrs[] = {
2273 static struct scsi_host_template driver_template = {
2274 .module = THIS_MODULE,
2275 .name = CXLFLASH_ADAPTER_NAME,
2276 .info = cxlflash_driver_info,
2277 .ioctl = cxlflash_ioctl,
2278 .proc_name = CXLFLASH_NAME,
2279 .queuecommand = cxlflash_queuecommand,
2280 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
2281 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
2282 .change_queue_depth = cxlflash_change_queue_depth,
2284 .can_queue = CXLFLASH_MAX_CMDS,
2286 .sg_tablesize = SG_NONE, /* No scatter gather support. */
2287 .max_sectors = CXLFLASH_MAX_SECTORS,
2288 .use_clustering = ENABLE_CLUSTERING,
2289 .shost_attrs = cxlflash_host_attrs,
2290 .sdev_attrs = cxlflash_dev_attrs,
2294 * Device dependent values
2296 static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS };
2299 * PCI device binding table
2301 static struct pci_device_id cxlflash_pci_table[] = {
2302 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
2303 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
2307 MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
2310 * cxlflash_worker_thread() - work thread handler for the AFU
2311 * @work: Work structure contained within cxlflash associated with host.
2313 * Handles the following events:
2314 * - Link reset which cannot be performed on interrupt context due to
2315 * blocking up to a few seconds
2316 * - Read AFU command room
2319 static void cxlflash_worker_thread(struct work_struct *work)
2321 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
2323 struct afu *afu = cfg->afu;
2324 struct device *dev = &cfg->dev->dev;
2328 /* Avoid MMIO if the device has failed */
2330 if (cfg->state != STATE_NORMAL)
2333 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2335 if (cfg->lr_state == LINK_RESET_REQUIRED) {
2336 port = cfg->lr_port;
2338 dev_err(dev, "%s: invalid port index %d\n",
2341 spin_unlock_irqrestore(cfg->host->host_lock,
2344 /* The reset can block... */
2345 afu_link_reset(afu, port,
2347 global.fc_regs[port][0]);
2348 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2351 cfg->lr_state = LINK_RESET_COMPLETE;
2354 if (afu->read_room) {
2355 atomic64_set(&afu->room, readq_be(&afu->host_map->cmd_room));
2356 afu->read_room = false;
2359 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
2361 if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
2362 scsi_scan_host(cfg->host);
2366 * cxlflash_probe() - PCI entry point to add host
2367 * @pdev: PCI device associated with the host.
2368 * @dev_id: PCI device id associated with device.
2370 * Return: 0 on success / non-zero on failure
2372 static int cxlflash_probe(struct pci_dev *pdev,
2373 const struct pci_device_id *dev_id)
2375 struct Scsi_Host *host;
2376 struct cxlflash_cfg *cfg = NULL;
2377 struct device *phys_dev;
2378 struct dev_dependent_vals *ddv;
2381 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
2382 __func__, pdev->irq);
2384 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
2385 driver_template.max_sectors = ddv->max_sectors;
2387 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
2389 dev_err(&pdev->dev, "%s: call to scsi_host_alloc failed!\n",
2395 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
2396 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
2397 host->max_channel = NUM_FC_PORTS - 1;
2398 host->unique_id = host->host_no;
2399 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
2401 cfg = (struct cxlflash_cfg *)host->hostdata;
2403 rc = alloc_mem(cfg);
2405 dev_err(&pdev->dev, "%s: call to scsi_host_alloc failed!\n",
2411 cfg->init_state = INIT_STATE_NONE;
2415 * The promoted LUNs move to the top of the LUN table. The rest stay
2416 * on the bottom half. The bottom half grows from the end
2417 * (index = 255), whereas the top half grows from the beginning
2420 cfg->promote_lun_index = 0;
2421 cfg->last_lun_index[0] = CXLFLASH_NUM_VLUNS/2 - 1;
2422 cfg->last_lun_index[1] = CXLFLASH_NUM_VLUNS/2 - 1;
2424 cfg->dev_id = (struct pci_device_id *)dev_id;
2427 init_waitqueue_head(&cfg->tmf_waitq);
2428 init_waitqueue_head(&cfg->reset_waitq);
2430 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
2431 cfg->lr_state = LINK_RESET_INVALID;
2433 mutex_init(&cfg->ctx_tbl_list_mutex);
2434 mutex_init(&cfg->ctx_recovery_mutex);
2435 init_rwsem(&cfg->ioctl_rwsem);
2436 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
2437 INIT_LIST_HEAD(&cfg->lluns);
2439 pci_set_drvdata(pdev, cfg);
2441 /* Use the special service provided to look up the physical
2442 * PCI device, since we are called on the probe of the virtual
2443 * PCI host bus (vphb)
2445 phys_dev = cxl_get_phys_dev(pdev);
2446 if (!dev_is_pci(phys_dev)) {
2447 dev_err(&pdev->dev, "%s: not a pci dev\n", __func__);
2451 cfg->parent_dev = to_pci_dev(phys_dev);
2453 cfg->cxl_afu = cxl_pci_to_afu(pdev);
2457 dev_err(&pdev->dev, "%s: call to init_pci "
2458 "failed rc=%d!\n", __func__, rc);
2461 cfg->init_state = INIT_STATE_PCI;
2465 dev_err(&pdev->dev, "%s: call to init_afu "
2466 "failed rc=%d!\n", __func__, rc);
2469 cfg->init_state = INIT_STATE_AFU;
2472 rc = init_scsi(cfg);
2474 dev_err(&pdev->dev, "%s: call to init_scsi "
2475 "failed rc=%d!\n", __func__, rc);
2478 cfg->init_state = INIT_STATE_SCSI;
2481 pr_debug("%s: returning rc=%d\n", __func__, rc);
2485 cxlflash_remove(pdev);
2490 * drain_ioctls() - wait until all currently executing ioctls have completed
2491 * @cfg: Internal structure associated with the host.
2493 * Obtain write access to read/write semaphore that wraps ioctl
2494 * handling to 'drain' ioctls currently executing.
2496 static void drain_ioctls(struct cxlflash_cfg *cfg)
2498 down_write(&cfg->ioctl_rwsem);
2499 up_write(&cfg->ioctl_rwsem);
2503 * cxlflash_pci_error_detected() - called when a PCI error is detected
2504 * @pdev: PCI device struct.
2505 * @state: PCI channel state.
2507 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2509 static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
2510 pci_channel_state_t state)
2513 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2514 struct device *dev = &cfg->dev->dev;
2516 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
2519 case pci_channel_io_frozen:
2520 cfg->state = STATE_RESET;
2521 scsi_block_requests(cfg->host);
2523 rc = cxlflash_mark_contexts_error(cfg);
2525 dev_err(dev, "%s: Failed to mark user contexts!(%d)\n",
2527 term_mc(cfg, UNDO_START);
2529 return PCI_ERS_RESULT_NEED_RESET;
2530 case pci_channel_io_perm_failure:
2531 cfg->state = STATE_FAILTERM;
2532 wake_up_all(&cfg->reset_waitq);
2533 scsi_unblock_requests(cfg->host);
2534 return PCI_ERS_RESULT_DISCONNECT;
2538 return PCI_ERS_RESULT_NEED_RESET;
2542 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2543 * @pdev: PCI device struct.
2545 * This routine is called by the pci error recovery code after the PCI
2546 * slot has been reset, just before we should resume normal operations.
2548 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2550 static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
2553 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2554 struct device *dev = &cfg->dev->dev;
2556 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2560 dev_err(dev, "%s: EEH recovery failed! (%d)\n", __func__, rc);
2561 return PCI_ERS_RESULT_DISCONNECT;
2564 return PCI_ERS_RESULT_RECOVERED;
2568 * cxlflash_pci_resume() - called when normal operation can resume
2569 * @pdev: PCI device struct
2571 static void cxlflash_pci_resume(struct pci_dev *pdev)
2573 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2574 struct device *dev = &cfg->dev->dev;
2576 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2578 cfg->state = STATE_NORMAL;
2579 wake_up_all(&cfg->reset_waitq);
2580 scsi_unblock_requests(cfg->host);
2583 static const struct pci_error_handlers cxlflash_err_handler = {
2584 .error_detected = cxlflash_pci_error_detected,
2585 .slot_reset = cxlflash_pci_slot_reset,
2586 .resume = cxlflash_pci_resume,
2590 * PCI device structure
2592 static struct pci_driver cxlflash_driver = {
2593 .name = CXLFLASH_NAME,
2594 .id_table = cxlflash_pci_table,
2595 .probe = cxlflash_probe,
2596 .remove = cxlflash_remove,
2597 .err_handler = &cxlflash_err_handler,
2601 * init_cxlflash() - module entry point
2603 * Return: 0 on success / non-zero on failure
2605 static int __init init_cxlflash(void)
2607 pr_info("%s: IBM Power CXL Flash Adapter: %s\n",
2608 __func__, CXLFLASH_DRIVER_DATE);
2610 cxlflash_list_init();
2612 return pci_register_driver(&cxlflash_driver);
2616 * exit_cxlflash() - module exit point
2618 static void __exit exit_cxlflash(void)
2620 cxlflash_term_global_luns();
2621 cxlflash_free_errpage();
2623 pci_unregister_driver(&cxlflash_driver);
2626 module_init(init_cxlflash);
2627 module_exit(exit_cxlflash);