2 * CXL Flash Device Driver
4 * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
5 * Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
7 * Copyright (C) 2015 IBM Corporation
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
15 #include <linux/delay.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/pci.h>
20 #include <asm/unaligned.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_host.h>
26 #include <uapi/scsi/cxlflash_ioctl.h>
32 MODULE_DESCRIPTION(CXLFLASH_ADAPTER_NAME
);
33 MODULE_AUTHOR("Manoj N. Kumar <manoj@linux.vnet.ibm.com>");
34 MODULE_AUTHOR("Matthew R. Ochs <mrochs@linux.vnet.ibm.com>");
35 MODULE_LICENSE("GPL");
38 * process_cmd_err() - command error handler
39 * @cmd: AFU command that experienced the error.
40 * @scp: SCSI command associated with the AFU command in error.
42 * Translates error bits from AFU command to SCSI command results.
44 static void process_cmd_err(struct afu_cmd
*cmd
, struct scsi_cmnd
*scp
)
46 struct afu
*afu
= cmd
->parent
;
47 struct cxlflash_cfg
*cfg
= afu
->parent
;
48 struct device
*dev
= &cfg
->dev
->dev
;
49 struct sisl_ioarcb
*ioarcb
;
50 struct sisl_ioasa
*ioasa
;
59 if (ioasa
->rc
.flags
& SISL_RC_FLAGS_UNDERRUN
) {
61 scsi_set_resid(scp
, resid
);
62 dev_dbg(dev
, "%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
63 __func__
, cmd
, scp
, resid
);
66 if (ioasa
->rc
.flags
& SISL_RC_FLAGS_OVERRUN
) {
67 dev_dbg(dev
, "%s: cmd underrun cmd = %p scp = %p\n",
69 scp
->result
= (DID_ERROR
<< 16);
72 dev_dbg(dev
, "%s: cmd failed afu_rc=%02x scsi_rc=%02x fc_rc=%02x "
73 "afu_extra=%02x scsi_extra=%02x fc_extra=%02x\n", __func__
,
74 ioasa
->rc
.afu_rc
, ioasa
->rc
.scsi_rc
, ioasa
->rc
.fc_rc
,
75 ioasa
->afu_extra
, ioasa
->scsi_extra
, ioasa
->fc_extra
);
77 if (ioasa
->rc
.scsi_rc
) {
78 /* We have a SCSI status */
79 if (ioasa
->rc
.flags
& SISL_RC_FLAGS_SENSE_VALID
) {
80 memcpy(scp
->sense_buffer
, ioasa
->sense_data
,
82 scp
->result
= ioasa
->rc
.scsi_rc
;
84 scp
->result
= ioasa
->rc
.scsi_rc
| (DID_ERROR
<< 16);
88 * We encountered an error. Set scp->result based on nature
91 if (ioasa
->rc
.fc_rc
) {
92 /* We have an FC status */
93 switch (ioasa
->rc
.fc_rc
) {
94 case SISL_FC_RC_LINKDOWN
:
95 scp
->result
= (DID_REQUEUE
<< 16);
97 case SISL_FC_RC_RESID
:
98 /* This indicates an FCP resid underrun */
99 if (!(ioasa
->rc
.flags
& SISL_RC_FLAGS_OVERRUN
)) {
100 /* If the SISL_RC_FLAGS_OVERRUN flag was set,
101 * then we will handle this error else where.
102 * If not then we must handle it here.
103 * This is probably an AFU bug.
105 scp
->result
= (DID_ERROR
<< 16);
108 case SISL_FC_RC_RESIDERR
:
109 /* Resid mismatch between adapter and device */
110 case SISL_FC_RC_TGTABORT
:
111 case SISL_FC_RC_ABORTOK
:
112 case SISL_FC_RC_ABORTFAIL
:
113 case SISL_FC_RC_NOLOGI
:
114 case SISL_FC_RC_ABORTPEND
:
115 case SISL_FC_RC_WRABORTPEND
:
116 case SISL_FC_RC_NOEXP
:
117 case SISL_FC_RC_INUSE
:
118 scp
->result
= (DID_ERROR
<< 16);
123 if (ioasa
->rc
.afu_rc
) {
124 /* We have an AFU error */
125 switch (ioasa
->rc
.afu_rc
) {
126 case SISL_AFU_RC_NO_CHANNELS
:
127 scp
->result
= (DID_NO_CONNECT
<< 16);
129 case SISL_AFU_RC_DATA_DMA_ERR
:
130 switch (ioasa
->afu_extra
) {
131 case SISL_AFU_DMA_ERR_PAGE_IN
:
133 scp
->result
= (DID_IMM_RETRY
<< 16);
135 case SISL_AFU_DMA_ERR_INVALID_EA
:
137 scp
->result
= (DID_ERROR
<< 16);
140 case SISL_AFU_RC_OUT_OF_DATA_BUFS
:
142 scp
->result
= (DID_ALLOC_FAILURE
<< 16);
145 scp
->result
= (DID_ERROR
<< 16);
151 * cmd_complete() - command completion handler
152 * @cmd: AFU command that has completed.
154 * Prepares and submits command that has either completed or timed out to
155 * the SCSI stack. Checks AFU command back into command pool for non-internal
156 * (cmd->scp populated) commands.
158 static void cmd_complete(struct afu_cmd
*cmd
)
160 struct scsi_cmnd
*scp
;
162 struct afu
*afu
= cmd
->parent
;
163 struct cxlflash_cfg
*cfg
= afu
->parent
;
164 struct device
*dev
= &cfg
->dev
->dev
;
169 if (unlikely(cmd
->sa
.ioasc
))
170 process_cmd_err(cmd
, scp
);
172 scp
->result
= (DID_OK
<< 16);
174 cmd_is_tmf
= cmd
->cmd_tmf
;
176 dev_dbg_ratelimited(dev
, "%s:scp=%p result=%08x ioasc=%08x\n",
177 __func__
, scp
, scp
->result
, cmd
->sa
.ioasc
);
183 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
184 cfg
->tmf_active
= false;
185 wake_up_all_locked(&cfg
->tmf_waitq
);
186 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
189 complete(&cmd
->cevent
);
193 * context_reset() - reset command owner context via specified register
194 * @cmd: AFU command that timed out.
195 * @reset_reg: MMIO register to perform reset.
197 static void context_reset(struct afu_cmd
*cmd
, __be64 __iomem
*reset_reg
)
201 struct afu
*afu
= cmd
->parent
;
202 struct cxlflash_cfg
*cfg
= afu
->parent
;
203 struct device
*dev
= &cfg
->dev
->dev
;
205 dev_dbg(dev
, "%s: cmd=%p\n", __func__
, cmd
);
207 writeq_be(rrin
, reset_reg
);
209 rrin
= readq_be(reset_reg
);
212 /* Double delay each time */
214 } while (nretry
++ < MC_ROOM_RETRY_CNT
);
216 dev_dbg(dev
, "%s: returning rrin=%016llx nretry=%d\n",
217 __func__
, rrin
, nretry
);
221 * context_reset_ioarrin() - reset command owner context via IOARRIN register
222 * @cmd: AFU command that timed out.
224 static void context_reset_ioarrin(struct afu_cmd
*cmd
)
226 struct afu
*afu
= cmd
->parent
;
228 context_reset(cmd
, &afu
->host_map
->ioarrin
);
232 * context_reset_sq() - reset command owner context w/ SQ Context Reset register
233 * @cmd: AFU command that timed out.
235 static void context_reset_sq(struct afu_cmd
*cmd
)
237 struct afu
*afu
= cmd
->parent
;
239 context_reset(cmd
, &afu
->host_map
->sq_ctx_reset
);
243 * send_cmd_ioarrin() - sends an AFU command via IOARRIN register
244 * @afu: AFU associated with the host.
245 * @cmd: AFU command to send.
248 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
250 static int send_cmd_ioarrin(struct afu
*afu
, struct afu_cmd
*cmd
)
252 struct cxlflash_cfg
*cfg
= afu
->parent
;
253 struct device
*dev
= &cfg
->dev
->dev
;
259 * To avoid the performance penalty of MMIO, spread the update of
260 * 'room' over multiple commands.
262 spin_lock_irqsave(&afu
->rrin_slock
, lock_flags
);
263 if (--afu
->room
< 0) {
264 room
= readq_be(&afu
->host_map
->cmd_room
);
266 dev_dbg_ratelimited(dev
, "%s: no cmd_room to send "
267 "0x%02X, room=0x%016llX\n",
268 __func__
, cmd
->rcb
.cdb
[0], room
);
270 rc
= SCSI_MLQUEUE_HOST_BUSY
;
273 afu
->room
= room
- 1;
276 writeq_be((u64
)&cmd
->rcb
, &afu
->host_map
->ioarrin
);
278 spin_unlock_irqrestore(&afu
->rrin_slock
, lock_flags
);
279 dev_dbg(dev
, "%s: cmd=%p len=%u ea=%016llx rc=%d\n", __func__
,
280 cmd
, cmd
->rcb
.data_len
, cmd
->rcb
.data_ea
, rc
);
285 * send_cmd_sq() - sends an AFU command via SQ ring
286 * @afu: AFU associated with the host.
287 * @cmd: AFU command to send.
290 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
292 static int send_cmd_sq(struct afu
*afu
, struct afu_cmd
*cmd
)
294 struct cxlflash_cfg
*cfg
= afu
->parent
;
295 struct device
*dev
= &cfg
->dev
->dev
;
300 newval
= atomic_dec_if_positive(&afu
->hsq_credits
);
302 rc
= SCSI_MLQUEUE_HOST_BUSY
;
306 cmd
->rcb
.ioasa
= &cmd
->sa
;
308 spin_lock_irqsave(&afu
->hsq_slock
, lock_flags
);
310 *afu
->hsq_curr
= cmd
->rcb
;
311 if (afu
->hsq_curr
< afu
->hsq_end
)
314 afu
->hsq_curr
= afu
->hsq_start
;
315 writeq_be((u64
)afu
->hsq_curr
, &afu
->host_map
->sq_tail
);
317 spin_unlock_irqrestore(&afu
->hsq_slock
, lock_flags
);
319 dev_dbg(dev
, "%s: cmd=%p len=%u ea=%016llx ioasa=%p rc=%d curr=%p "
320 "head=%016llx tail=%016llx\n", __func__
, cmd
, cmd
->rcb
.data_len
,
321 cmd
->rcb
.data_ea
, cmd
->rcb
.ioasa
, rc
, afu
->hsq_curr
,
322 readq_be(&afu
->host_map
->sq_head
),
323 readq_be(&afu
->host_map
->sq_tail
));
328 * wait_resp() - polls for a response or timeout to a sent AFU command
329 * @afu: AFU associated with the host.
330 * @cmd: AFU command that was sent.
333 * 0 on success, -1 on timeout/error
335 static int wait_resp(struct afu
*afu
, struct afu_cmd
*cmd
)
337 struct cxlflash_cfg
*cfg
= afu
->parent
;
338 struct device
*dev
= &cfg
->dev
->dev
;
340 ulong timeout
= msecs_to_jiffies(cmd
->rcb
.timeout
* 2 * 1000);
342 timeout
= wait_for_completion_timeout(&cmd
->cevent
, timeout
);
344 afu
->context_reset(cmd
);
348 if (unlikely(cmd
->sa
.ioasc
!= 0)) {
349 dev_err(dev
, "%s: cmd %02x failed, ioasc=%08x\n",
350 __func__
, cmd
->rcb
.cdb
[0], cmd
->sa
.ioasc
);
358 * send_tmf() - sends a Task Management Function (TMF)
359 * @afu: AFU to checkout from.
360 * @scp: SCSI command from stack.
361 * @tmfcmd: TMF command to send.
364 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
366 static int send_tmf(struct afu
*afu
, struct scsi_cmnd
*scp
, u64 tmfcmd
)
368 u32 port_sel
= scp
->device
->channel
+ 1;
369 struct cxlflash_cfg
*cfg
= shost_priv(scp
->device
->host
);
370 struct afu_cmd
*cmd
= sc_to_afucz(scp
);
371 struct device
*dev
= &cfg
->dev
->dev
;
376 /* When Task Management Function is active do not send another */
377 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
379 wait_event_interruptible_lock_irq(cfg
->tmf_waitq
,
382 cfg
->tmf_active
= true;
383 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
389 cmd
->rcb
.ctx_id
= afu
->ctx_hndl
;
390 cmd
->rcb
.msi
= SISL_MSI_RRQ_UPDATED
;
391 cmd
->rcb
.port_sel
= port_sel
;
392 cmd
->rcb
.lun_id
= lun_to_lunid(scp
->device
->lun
);
393 cmd
->rcb
.req_flags
= (SISL_REQ_FLAGS_PORT_LUN_ID
|
394 SISL_REQ_FLAGS_SUP_UNDERRUN
|
395 SISL_REQ_FLAGS_TMF_CMD
);
396 memcpy(cmd
->rcb
.cdb
, &tmfcmd
, sizeof(tmfcmd
));
398 rc
= afu
->send_cmd(afu
, cmd
);
400 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
401 cfg
->tmf_active
= false;
402 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
406 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
407 to
= msecs_to_jiffies(5000);
408 to
= wait_event_interruptible_lock_irq_timeout(cfg
->tmf_waitq
,
413 cfg
->tmf_active
= false;
414 dev_err(dev
, "%s: TMF timed out\n", __func__
);
417 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
423 * cxlflash_driver_info() - information handler for this host driver
424 * @host: SCSI host associated with device.
426 * Return: A string describing the device.
428 static const char *cxlflash_driver_info(struct Scsi_Host
*host
)
430 return CXLFLASH_ADAPTER_NAME
;
434 * cxlflash_queuecommand() - sends a mid-layer request
435 * @host: SCSI host associated with device.
436 * @scp: SCSI command to send.
438 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
440 static int cxlflash_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*scp
)
442 struct cxlflash_cfg
*cfg
= shost_priv(host
);
443 struct afu
*afu
= cfg
->afu
;
444 struct device
*dev
= &cfg
->dev
->dev
;
445 struct afu_cmd
*cmd
= sc_to_afucz(scp
);
446 struct scatterlist
*sg
= scsi_sglist(scp
);
447 u32 port_sel
= scp
->device
->channel
+ 1;
448 u16 req_flags
= SISL_REQ_FLAGS_SUP_UNDERRUN
;
453 dev_dbg_ratelimited(dev
, "%s: (scp=%p) %d/%d/%d/%llu "
454 "cdb=(%08x-%08x-%08x-%08x)\n",
455 __func__
, scp
, host
->host_no
, scp
->device
->channel
,
456 scp
->device
->id
, scp
->device
->lun
,
457 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
458 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
459 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
460 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
463 * If a Task Management Function is active, wait for it to complete
464 * before continuing with regular commands.
466 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
467 if (cfg
->tmf_active
) {
468 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
469 rc
= SCSI_MLQUEUE_HOST_BUSY
;
472 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
474 switch (cfg
->state
) {
476 dev_dbg_ratelimited(dev
, "%s: device is in reset\n", __func__
);
477 rc
= SCSI_MLQUEUE_HOST_BUSY
;
480 dev_dbg_ratelimited(dev
, "%s: device has failed\n", __func__
);
481 scp
->result
= (DID_NO_CONNECT
<< 16);
490 nseg
= scsi_dma_map(scp
);
491 if (unlikely(nseg
< 0)) {
492 dev_err(dev
, "%s: Fail DMA map\n", __func__
);
493 rc
= SCSI_MLQUEUE_HOST_BUSY
;
497 cmd
->rcb
.data_len
= sg_dma_len(sg
);
498 cmd
->rcb
.data_ea
= sg_dma_address(sg
);
504 cmd
->rcb
.ctx_id
= afu
->ctx_hndl
;
505 cmd
->rcb
.msi
= SISL_MSI_RRQ_UPDATED
;
506 cmd
->rcb
.port_sel
= port_sel
;
507 cmd
->rcb
.lun_id
= lun_to_lunid(scp
->device
->lun
);
509 if (scp
->sc_data_direction
== DMA_TO_DEVICE
)
510 req_flags
|= SISL_REQ_FLAGS_HOST_WRITE
;
512 cmd
->rcb
.req_flags
= req_flags
;
513 memcpy(cmd
->rcb
.cdb
, scp
->cmnd
, sizeof(cmd
->rcb
.cdb
));
515 rc
= afu
->send_cmd(afu
, cmd
);
523 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
524 * @cfg: Internal structure associated with the host.
526 static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg
*cfg
)
528 struct pci_dev
*pdev
= cfg
->dev
;
530 if (pci_channel_offline(pdev
))
531 wait_event_timeout(cfg
->reset_waitq
,
532 !pci_channel_offline(pdev
),
533 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT
);
537 * free_mem() - free memory associated with the AFU
538 * @cfg: Internal structure associated with the host.
540 static void free_mem(struct cxlflash_cfg
*cfg
)
542 struct afu
*afu
= cfg
->afu
;
545 free_pages((ulong
)afu
, get_order(sizeof(struct afu
)));
551 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
552 * @cfg: Internal structure associated with the host.
554 * Safe to call with AFU in a partially allocated/initialized state.
556 * Cancels scheduled worker threads, waits for any active internal AFU
557 * commands to timeout and then unmaps the MMIO space.
559 static void stop_afu(struct cxlflash_cfg
*cfg
)
561 struct afu
*afu
= cfg
->afu
;
563 cancel_work_sync(&cfg
->work_q
);
566 while (atomic_read(&afu
->cmds_active
))
568 if (likely(afu
->afu_map
)) {
569 cxl_psa_unmap((void __iomem
*)afu
->afu_map
);
576 * term_intr() - disables all AFU interrupts
577 * @cfg: Internal structure associated with the host.
578 * @level: Depth of allocation, where to begin waterfall tear down.
580 * Safe to call with AFU/MC in partially allocated/initialized state.
582 static void term_intr(struct cxlflash_cfg
*cfg
, enum undo_level level
)
584 struct afu
*afu
= cfg
->afu
;
585 struct device
*dev
= &cfg
->dev
->dev
;
587 if (!afu
|| !cfg
->mcctx
) {
588 dev_err(dev
, "%s: returning with NULL afu or MC\n", __func__
);
594 cxl_unmap_afu_irq(cfg
->mcctx
, 3, afu
);
596 cxl_unmap_afu_irq(cfg
->mcctx
, 2, afu
);
598 cxl_unmap_afu_irq(cfg
->mcctx
, 1, afu
);
600 cxl_free_afu_irqs(cfg
->mcctx
);
603 /* No action required */
609 * term_mc() - terminates the master context
610 * @cfg: Internal structure associated with the host.
611 * @level: Depth of allocation, where to begin waterfall tear down.
613 * Safe to call with AFU/MC in partially allocated/initialized state.
615 static void term_mc(struct cxlflash_cfg
*cfg
)
618 struct afu
*afu
= cfg
->afu
;
619 struct device
*dev
= &cfg
->dev
->dev
;
621 if (!afu
|| !cfg
->mcctx
) {
622 dev_err(dev
, "%s: returning with NULL afu or MC\n", __func__
);
626 rc
= cxl_stop_context(cfg
->mcctx
);
632 * term_afu() - terminates the AFU
633 * @cfg: Internal structure associated with the host.
635 * Safe to call with AFU/MC in partially allocated/initialized state.
637 static void term_afu(struct cxlflash_cfg
*cfg
)
639 struct device
*dev
= &cfg
->dev
->dev
;
642 * Tear down is carefully orchestrated to ensure
643 * no interrupts can come in when the problem state
646 * 1) Disable all AFU interrupts
647 * 2) Unmap the problem state area
648 * 3) Stop the master context
650 term_intr(cfg
, UNMAP_THREE
);
656 dev_dbg(dev
, "%s: returning\n", __func__
);
660 * notify_shutdown() - notifies device of pending shutdown
661 * @cfg: Internal structure associated with the host.
662 * @wait: Whether to wait for shutdown processing to complete.
664 * This function will notify the AFU that the adapter is being shutdown
665 * and will wait for shutdown processing to complete if wait is true.
666 * This notification should flush pending I/Os to the device and halt
667 * further I/Os until the next AFU reset is issued and device restarted.
669 static void notify_shutdown(struct cxlflash_cfg
*cfg
, bool wait
)
671 struct afu
*afu
= cfg
->afu
;
672 struct device
*dev
= &cfg
->dev
->dev
;
673 struct sisl_global_map __iomem
*global
;
674 struct dev_dependent_vals
*ddv
;
676 int i
, retry_cnt
= 0;
678 ddv
= (struct dev_dependent_vals
*)cfg
->dev_id
->driver_data
;
679 if (!(ddv
->flags
& CXLFLASH_NOTIFY_SHUTDOWN
))
682 if (!afu
|| !afu
->afu_map
) {
683 dev_dbg(dev
, "%s: Problem state area not mapped\n", __func__
);
687 global
= &afu
->afu_map
->global
;
690 for (i
= 0; i
< NUM_FC_PORTS
; i
++) {
691 reg
= readq_be(&global
->fc_regs
[i
][FC_CONFIG2
/ 8]);
692 reg
|= SISL_FC_SHUTDOWN_NORMAL
;
693 writeq_be(reg
, &global
->fc_regs
[i
][FC_CONFIG2
/ 8]);
699 /* Wait up to 1.5 seconds for shutdown processing to complete */
700 for (i
= 0; i
< NUM_FC_PORTS
; i
++) {
703 status
= readq_be(&global
->fc_regs
[i
][FC_STATUS
/ 8]);
704 if (status
& SISL_STATUS_SHUTDOWN_COMPLETE
)
706 if (++retry_cnt
>= MC_RETRY_CNT
) {
707 dev_dbg(dev
, "%s: port %d shutdown processing "
708 "not yet completed\n", __func__
, i
);
711 msleep(100 * retry_cnt
);
717 * cxlflash_remove() - PCI entry point to tear down host
718 * @pdev: PCI device associated with the host.
720 * Safe to use as a cleanup in partially allocated/initialized state.
722 static void cxlflash_remove(struct pci_dev
*pdev
)
724 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
725 struct device
*dev
= &pdev
->dev
;
728 if (!pci_is_enabled(pdev
)) {
729 dev_dbg(dev
, "%s: Device is disabled\n", __func__
);
733 /* If a Task Management Function is active, wait for it to complete
734 * before continuing with remove.
736 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
738 wait_event_interruptible_lock_irq(cfg
->tmf_waitq
,
741 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
743 /* Notify AFU and wait for shutdown processing to complete */
744 notify_shutdown(cfg
, true);
746 cfg
->state
= STATE_FAILTERM
;
747 cxlflash_stop_term_user_contexts(cfg
);
749 switch (cfg
->init_state
) {
750 case INIT_STATE_SCSI
:
751 cxlflash_term_local_luns(cfg
);
752 scsi_remove_host(cfg
->host
);
757 pci_disable_device(pdev
);
758 case INIT_STATE_NONE
:
760 scsi_host_put(cfg
->host
);
764 dev_dbg(dev
, "%s: returning\n", __func__
);
768 * alloc_mem() - allocates the AFU and its command pool
769 * @cfg: Internal structure associated with the host.
771 * A partially allocated state remains on failure.
775 * -ENOMEM on failure to allocate memory
777 static int alloc_mem(struct cxlflash_cfg
*cfg
)
780 struct device
*dev
= &cfg
->dev
->dev
;
782 /* AFU is ~28k, i.e. only one 64k page or up to seven 4k pages */
783 cfg
->afu
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
784 get_order(sizeof(struct afu
)));
785 if (unlikely(!cfg
->afu
)) {
786 dev_err(dev
, "%s: cannot get %d free pages\n",
787 __func__
, get_order(sizeof(struct afu
)));
791 cfg
->afu
->parent
= cfg
;
792 cfg
->afu
->afu_map
= NULL
;
798 * init_pci() - initializes the host as a PCI device
799 * @cfg: Internal structure associated with the host.
801 * Return: 0 on success, -errno on failure
803 static int init_pci(struct cxlflash_cfg
*cfg
)
805 struct pci_dev
*pdev
= cfg
->dev
;
806 struct device
*dev
= &cfg
->dev
->dev
;
809 rc
= pci_enable_device(pdev
);
810 if (rc
|| pci_channel_offline(pdev
)) {
811 if (pci_channel_offline(pdev
)) {
812 cxlflash_wait_for_pci_err_recovery(cfg
);
813 rc
= pci_enable_device(pdev
);
817 dev_err(dev
, "%s: Cannot enable adapter\n", __func__
);
818 cxlflash_wait_for_pci_err_recovery(cfg
);
824 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
829 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
830 * @cfg: Internal structure associated with the host.
832 * Return: 0 on success, -errno on failure
834 static int init_scsi(struct cxlflash_cfg
*cfg
)
836 struct pci_dev
*pdev
= cfg
->dev
;
837 struct device
*dev
= &cfg
->dev
->dev
;
840 rc
= scsi_add_host(cfg
->host
, &pdev
->dev
);
842 dev_err(dev
, "%s: scsi_add_host failed rc=%d\n", __func__
, rc
);
846 scsi_scan_host(cfg
->host
);
849 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
854 * set_port_online() - transitions the specified host FC port to online state
855 * @fc_regs: Top of MMIO region defined for specified port.
857 * The provided MMIO region must be mapped prior to call. Online state means
858 * that the FC link layer has synced, completed the handshaking process, and
859 * is ready for login to start.
861 static void set_port_online(__be64 __iomem
*fc_regs
)
865 cmdcfg
= readq_be(&fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
866 cmdcfg
&= (~FC_MTIP_CMDCONFIG_OFFLINE
); /* clear OFF_LINE */
867 cmdcfg
|= (FC_MTIP_CMDCONFIG_ONLINE
); /* set ON_LINE */
868 writeq_be(cmdcfg
, &fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
872 * set_port_offline() - transitions the specified host FC port to offline state
873 * @fc_regs: Top of MMIO region defined for specified port.
875 * The provided MMIO region must be mapped prior to call.
877 static void set_port_offline(__be64 __iomem
*fc_regs
)
881 cmdcfg
= readq_be(&fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
882 cmdcfg
&= (~FC_MTIP_CMDCONFIG_ONLINE
); /* clear ON_LINE */
883 cmdcfg
|= (FC_MTIP_CMDCONFIG_OFFLINE
); /* set OFF_LINE */
884 writeq_be(cmdcfg
, &fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
888 * wait_port_online() - waits for the specified host FC port come online
889 * @fc_regs: Top of MMIO region defined for specified port.
890 * @delay_us: Number of microseconds to delay between reading port status.
891 * @nretry: Number of cycles to retry reading port status.
893 * The provided MMIO region must be mapped prior to call. This will timeout
894 * when the cable is not plugged in.
897 * TRUE (1) when the specified port is online
898 * FALSE (0) when the specified port fails to come online after timeout
900 static bool wait_port_online(__be64 __iomem
*fc_regs
, u32 delay_us
, u32 nretry
)
904 WARN_ON(delay_us
< 1000);
907 msleep(delay_us
/ 1000);
908 status
= readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]);
909 if (status
== U64_MAX
)
911 } while ((status
& FC_MTIP_STATUS_MASK
) != FC_MTIP_STATUS_ONLINE
&&
914 return ((status
& FC_MTIP_STATUS_MASK
) == FC_MTIP_STATUS_ONLINE
);
918 * wait_port_offline() - waits for the specified host FC port go offline
919 * @fc_regs: Top of MMIO region defined for specified port.
920 * @delay_us: Number of microseconds to delay between reading port status.
921 * @nretry: Number of cycles to retry reading port status.
923 * The provided MMIO region must be mapped prior to call.
926 * TRUE (1) when the specified port is offline
927 * FALSE (0) when the specified port fails to go offline after timeout
929 static bool wait_port_offline(__be64 __iomem
*fc_regs
, u32 delay_us
, u32 nretry
)
933 WARN_ON(delay_us
< 1000);
936 msleep(delay_us
/ 1000);
937 status
= readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]);
938 if (status
== U64_MAX
)
940 } while ((status
& FC_MTIP_STATUS_MASK
) != FC_MTIP_STATUS_OFFLINE
&&
943 return ((status
& FC_MTIP_STATUS_MASK
) == FC_MTIP_STATUS_OFFLINE
);
947 * afu_set_wwpn() - configures the WWPN for the specified host FC port
948 * @afu: AFU associated with the host that owns the specified FC port.
949 * @port: Port number being configured.
950 * @fc_regs: Top of MMIO region defined for specified port.
951 * @wwpn: The world-wide-port-number previously discovered for port.
953 * The provided MMIO region must be mapped prior to call. As part of the
954 * sequence to configure the WWPN, the port is toggled offline and then back
955 * online. This toggling action can cause this routine to delay up to a few
956 * seconds. When configured to use the internal LUN feature of the AFU, a
957 * failure to come online is overridden.
959 static void afu_set_wwpn(struct afu
*afu
, int port
, __be64 __iomem
*fc_regs
,
962 struct cxlflash_cfg
*cfg
= afu
->parent
;
963 struct device
*dev
= &cfg
->dev
->dev
;
965 set_port_offline(fc_regs
);
966 if (!wait_port_offline(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
967 FC_PORT_STATUS_RETRY_CNT
)) {
968 dev_dbg(dev
, "%s: wait on port %d to go offline timed out\n",
972 writeq_be(wwpn
, &fc_regs
[FC_PNAME
/ 8]);
974 set_port_online(fc_regs
);
975 if (!wait_port_online(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
976 FC_PORT_STATUS_RETRY_CNT
)) {
977 dev_dbg(dev
, "%s: wait on port %d to go online timed out\n",
983 * afu_link_reset() - resets the specified host FC port
984 * @afu: AFU associated with the host that owns the specified FC port.
985 * @port: Port number being configured.
986 * @fc_regs: Top of MMIO region defined for specified port.
988 * The provided MMIO region must be mapped prior to call. The sequence to
989 * reset the port involves toggling it offline and then back online. This
990 * action can cause this routine to delay up to a few seconds. An effort
991 * is made to maintain link with the device by switching to host to use
992 * the alternate port exclusively while the reset takes place.
993 * failure to come online is overridden.
995 static void afu_link_reset(struct afu
*afu
, int port
, __be64 __iomem
*fc_regs
)
997 struct cxlflash_cfg
*cfg
= afu
->parent
;
998 struct device
*dev
= &cfg
->dev
->dev
;
1001 /* first switch the AFU to the other links, if any */
1002 port_sel
= readq_be(&afu
->afu_map
->global
.regs
.afu_port_sel
);
1003 port_sel
&= ~(1ULL << port
);
1004 writeq_be(port_sel
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1005 cxlflash_afu_sync(afu
, 0, 0, AFU_GSYNC
);
1007 set_port_offline(fc_regs
);
1008 if (!wait_port_offline(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1009 FC_PORT_STATUS_RETRY_CNT
))
1010 dev_err(dev
, "%s: wait on port %d to go offline timed out\n",
1013 set_port_online(fc_regs
);
1014 if (!wait_port_online(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1015 FC_PORT_STATUS_RETRY_CNT
))
1016 dev_err(dev
, "%s: wait on port %d to go online timed out\n",
1019 /* switch back to include this port */
1020 port_sel
|= (1ULL << port
);
1021 writeq_be(port_sel
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1022 cxlflash_afu_sync(afu
, 0, 0, AFU_GSYNC
);
1024 dev_dbg(dev
, "%s: returning port_sel=%016llx\n", __func__
, port_sel
);
1028 * Asynchronous interrupt information table
1030 static const struct asyc_intr_info ainfo
[] = {
1031 {SISL_ASTATUS_FC0_OTHER
, "other error", 0, CLR_FC_ERROR
| LINK_RESET
},
1032 {SISL_ASTATUS_FC0_LOGO
, "target initiated LOGO", 0, 0},
1033 {SISL_ASTATUS_FC0_CRC_T
, "CRC threshold exceeded", 0, LINK_RESET
},
1034 {SISL_ASTATUS_FC0_LOGI_R
, "login timed out, retrying", 0, LINK_RESET
},
1035 {SISL_ASTATUS_FC0_LOGI_F
, "login failed", 0, CLR_FC_ERROR
},
1036 {SISL_ASTATUS_FC0_LOGI_S
, "login succeeded", 0, SCAN_HOST
},
1037 {SISL_ASTATUS_FC0_LINK_DN
, "link down", 0, 0},
1038 {SISL_ASTATUS_FC0_LINK_UP
, "link up", 0, 0},
1039 {SISL_ASTATUS_FC1_OTHER
, "other error", 1, CLR_FC_ERROR
| LINK_RESET
},
1040 {SISL_ASTATUS_FC1_LOGO
, "target initiated LOGO", 1, 0},
1041 {SISL_ASTATUS_FC1_CRC_T
, "CRC threshold exceeded", 1, LINK_RESET
},
1042 {SISL_ASTATUS_FC1_LOGI_R
, "login timed out, retrying", 1, LINK_RESET
},
1043 {SISL_ASTATUS_FC1_LOGI_F
, "login failed", 1, CLR_FC_ERROR
},
1044 {SISL_ASTATUS_FC1_LOGI_S
, "login succeeded", 1, SCAN_HOST
},
1045 {SISL_ASTATUS_FC1_LINK_DN
, "link down", 1, 0},
1046 {SISL_ASTATUS_FC1_LINK_UP
, "link up", 1, 0},
1047 {0x0, "", 0, 0} /* terminator */
1051 * find_ainfo() - locates and returns asynchronous interrupt information
1052 * @status: Status code set by AFU on error.
1054 * Return: The located information or NULL when the status code is invalid.
1056 static const struct asyc_intr_info
*find_ainfo(u64 status
)
1058 const struct asyc_intr_info
*info
;
1060 for (info
= &ainfo
[0]; info
->status
; info
++)
1061 if (info
->status
== status
)
1068 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1069 * @afu: AFU associated with the host.
1071 static void afu_err_intr_init(struct afu
*afu
)
1076 /* global async interrupts: AFU clears afu_ctrl on context exit
1077 * if async interrupts were sent to that context. This prevents
1078 * the AFU form sending further async interrupts when
1080 * nobody to receive them.
1084 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_mask
);
1085 /* set LISN# to send and point to master context */
1086 reg
= ((u64
) (((afu
->ctx_hndl
<< 8) | SISL_MSI_ASYNC_ERROR
)) << 40);
1088 if (afu
->internal_lun
)
1089 reg
|= 1; /* Bit 63 indicates local lun */
1090 writeq_be(reg
, &afu
->afu_map
->global
.regs
.afu_ctrl
);
1092 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_clear
);
1093 /* unmask bits that are of interest */
1094 /* note: afu can send an interrupt after this step */
1095 writeq_be(SISL_ASTATUS_MASK
, &afu
->afu_map
->global
.regs
.aintr_mask
);
1096 /* clear again in case a bit came on after previous clear but before */
1098 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_clear
);
1100 /* Clear/Set internal lun bits */
1101 reg
= readq_be(&afu
->afu_map
->global
.fc_regs
[0][FC_CONFIG2
/ 8]);
1102 reg
&= SISL_FC_INTERNAL_MASK
;
1103 if (afu
->internal_lun
)
1104 reg
|= ((u64
)(afu
->internal_lun
- 1) << SISL_FC_INTERNAL_SHIFT
);
1105 writeq_be(reg
, &afu
->afu_map
->global
.fc_regs
[0][FC_CONFIG2
/ 8]);
1107 /* now clear FC errors */
1108 for (i
= 0; i
< NUM_FC_PORTS
; i
++) {
1109 writeq_be(0xFFFFFFFFU
,
1110 &afu
->afu_map
->global
.fc_regs
[i
][FC_ERROR
/ 8]);
1111 writeq_be(0, &afu
->afu_map
->global
.fc_regs
[i
][FC_ERRCAP
/ 8]);
1114 /* sync interrupts for master's IOARRIN write */
1115 /* note that unlike asyncs, there can be no pending sync interrupts */
1116 /* at this time (this is a fresh context and master has not written */
1117 /* IOARRIN yet), so there is nothing to clear. */
1119 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1120 writeq_be(SISL_MSI_SYNC_ERROR
, &afu
->host_map
->ctx_ctrl
);
1121 writeq_be(SISL_ISTATUS_MASK
, &afu
->host_map
->intr_mask
);
1125 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1126 * @irq: Interrupt number.
1127 * @data: Private data provided at interrupt registration, the AFU.
1129 * Return: Always return IRQ_HANDLED.
1131 static irqreturn_t
cxlflash_sync_err_irq(int irq
, void *data
)
1133 struct afu
*afu
= (struct afu
*)data
;
1134 struct cxlflash_cfg
*cfg
= afu
->parent
;
1135 struct device
*dev
= &cfg
->dev
->dev
;
1139 reg
= readq_be(&afu
->host_map
->intr_status
);
1140 reg_unmasked
= (reg
& SISL_ISTATUS_UNMASK
);
1142 if (reg_unmasked
== 0UL) {
1143 dev_err(dev
, "%s: spurious interrupt, intr_status=%016llx\n",
1145 goto cxlflash_sync_err_irq_exit
;
1148 dev_err(dev
, "%s: unexpected interrupt, intr_status=%016llx\n",
1151 writeq_be(reg_unmasked
, &afu
->host_map
->intr_clear
);
1153 cxlflash_sync_err_irq_exit
:
1158 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1159 * @irq: Interrupt number.
1160 * @data: Private data provided at interrupt registration, the AFU.
1162 * Return: Always return IRQ_HANDLED.
1164 static irqreturn_t
cxlflash_rrq_irq(int irq
, void *data
)
1166 struct afu
*afu
= (struct afu
*)data
;
1167 struct afu_cmd
*cmd
;
1168 struct sisl_ioasa
*ioasa
;
1169 struct sisl_ioarcb
*ioarcb
;
1170 bool toggle
= afu
->toggle
;
1172 *hrrq_start
= afu
->hrrq_start
,
1173 *hrrq_end
= afu
->hrrq_end
,
1174 *hrrq_curr
= afu
->hrrq_curr
;
1176 /* Process however many RRQ entries that are ready */
1180 if ((entry
& SISL_RESP_HANDLE_T_BIT
) != toggle
)
1183 entry
&= ~SISL_RESP_HANDLE_T_BIT
;
1185 if (afu_is_sq_cmd_mode(afu
)) {
1186 ioasa
= (struct sisl_ioasa
*)entry
;
1187 cmd
= container_of(ioasa
, struct afu_cmd
, sa
);
1189 ioarcb
= (struct sisl_ioarcb
*)entry
;
1190 cmd
= container_of(ioarcb
, struct afu_cmd
, rcb
);
1195 /* Advance to next entry or wrap and flip the toggle bit */
1196 if (hrrq_curr
< hrrq_end
)
1199 hrrq_curr
= hrrq_start
;
1200 toggle
^= SISL_RESP_HANDLE_T_BIT
;
1203 atomic_inc(&afu
->hsq_credits
);
1206 afu
->hrrq_curr
= hrrq_curr
;
1207 afu
->toggle
= toggle
;
1213 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1214 * @irq: Interrupt number.
1215 * @data: Private data provided at interrupt registration, the AFU.
1217 * Return: Always return IRQ_HANDLED.
1219 static irqreturn_t
cxlflash_async_err_irq(int irq
, void *data
)
1221 struct afu
*afu
= (struct afu
*)data
;
1222 struct cxlflash_cfg
*cfg
= afu
->parent
;
1223 struct device
*dev
= &cfg
->dev
->dev
;
1225 const struct asyc_intr_info
*info
;
1226 struct sisl_global_map __iomem
*global
= &afu
->afu_map
->global
;
1231 reg
= readq_be(&global
->regs
.aintr_status
);
1232 reg_unmasked
= (reg
& SISL_ASTATUS_UNMASK
);
1234 if (reg_unmasked
== 0) {
1235 dev_err(dev
, "%s: spurious interrupt, aintr_status=%016llx\n",
1240 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
1241 writeq_be(reg_unmasked
, &global
->regs
.aintr_clear
);
1243 /* Check each bit that is on */
1244 for (i
= 0; reg_unmasked
; i
++, reg_unmasked
= (reg_unmasked
>> 1)) {
1245 info
= find_ainfo(1ULL << i
);
1246 if (((reg_unmasked
& 0x1) == 0) || !info
)
1251 dev_err(dev
, "%s: FC Port %d -> %s, fc_status=%016llx\n",
1252 __func__
, port
, info
->desc
,
1253 readq_be(&global
->fc_regs
[port
][FC_STATUS
/ 8]));
1256 * Do link reset first, some OTHER errors will set FC_ERROR
1257 * again if cleared before or w/o a reset
1259 if (info
->action
& LINK_RESET
) {
1260 dev_err(dev
, "%s: FC Port %d: resetting link\n",
1262 cfg
->lr_state
= LINK_RESET_REQUIRED
;
1263 cfg
->lr_port
= port
;
1264 schedule_work(&cfg
->work_q
);
1267 if (info
->action
& CLR_FC_ERROR
) {
1268 reg
= readq_be(&global
->fc_regs
[port
][FC_ERROR
/ 8]);
1271 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
1272 * should be the same and tracing one is sufficient.
1275 dev_err(dev
, "%s: fc %d: clearing fc_error=%016llx\n",
1276 __func__
, port
, reg
);
1278 writeq_be(reg
, &global
->fc_regs
[port
][FC_ERROR
/ 8]);
1279 writeq_be(0, &global
->fc_regs
[port
][FC_ERRCAP
/ 8]);
1282 if (info
->action
& SCAN_HOST
) {
1283 atomic_inc(&cfg
->scan_host_needed
);
1284 schedule_work(&cfg
->work_q
);
1293 * start_context() - starts the master context
1294 * @cfg: Internal structure associated with the host.
1296 * Return: A success or failure value from CXL services.
1298 static int start_context(struct cxlflash_cfg
*cfg
)
1300 struct device
*dev
= &cfg
->dev
->dev
;
1303 rc
= cxl_start_context(cfg
->mcctx
,
1304 cfg
->afu
->work
.work_element_descriptor
,
1307 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1312 * read_vpd() - obtains the WWPNs from VPD
1313 * @cfg: Internal structure associated with the host.
1314 * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
1316 * Return: 0 on success, -errno on failure
1318 static int read_vpd(struct cxlflash_cfg
*cfg
, u64 wwpn
[])
1320 struct device
*dev
= &cfg
->dev
->dev
;
1321 struct pci_dev
*pdev
= cfg
->dev
;
1323 int ro_start
, ro_size
, i
, j
, k
;
1325 char vpd_data
[CXLFLASH_VPD_LEN
];
1326 char tmp_buf
[WWPN_BUF_LEN
] = { 0 };
1327 char *wwpn_vpd_tags
[NUM_FC_PORTS
] = { "V5", "V6" };
1329 /* Get the VPD data from the device */
1330 vpd_size
= cxl_read_adapter_vpd(pdev
, vpd_data
, sizeof(vpd_data
));
1331 if (unlikely(vpd_size
<= 0)) {
1332 dev_err(dev
, "%s: Unable to read VPD (size = %ld)\n",
1333 __func__
, vpd_size
);
1338 /* Get the read only section offset */
1339 ro_start
= pci_vpd_find_tag(vpd_data
, 0, vpd_size
,
1340 PCI_VPD_LRDT_RO_DATA
);
1341 if (unlikely(ro_start
< 0)) {
1342 dev_err(dev
, "%s: VPD Read-only data not found\n", __func__
);
1347 /* Get the read only section size, cap when extends beyond read VPD */
1348 ro_size
= pci_vpd_lrdt_size(&vpd_data
[ro_start
]);
1350 i
= ro_start
+ PCI_VPD_LRDT_TAG_SIZE
;
1351 if (unlikely((i
+ j
) > vpd_size
)) {
1352 dev_dbg(dev
, "%s: Might need to read more VPD (%d > %ld)\n",
1353 __func__
, (i
+ j
), vpd_size
);
1354 ro_size
= vpd_size
- i
;
1358 * Find the offset of the WWPN tag within the read only
1359 * VPD data and validate the found field (partials are
1360 * no good to us). Convert the ASCII data to an integer
1361 * value. Note that we must copy to a temporary buffer
1362 * because the conversion service requires that the ASCII
1363 * string be terminated.
1365 for (k
= 0; k
< NUM_FC_PORTS
; k
++) {
1367 i
= ro_start
+ PCI_VPD_LRDT_TAG_SIZE
;
1369 i
= pci_vpd_find_info_keyword(vpd_data
, i
, j
, wwpn_vpd_tags
[k
]);
1370 if (unlikely(i
< 0)) {
1371 dev_err(dev
, "%s: Port %d WWPN not found in VPD\n",
1377 j
= pci_vpd_info_field_size(&vpd_data
[i
]);
1378 i
+= PCI_VPD_INFO_FLD_HDR_SIZE
;
1379 if (unlikely((i
+ j
> vpd_size
) || (j
!= WWPN_LEN
))) {
1380 dev_err(dev
, "%s: Port %d WWPN incomplete or bad VPD\n",
1386 memcpy(tmp_buf
, &vpd_data
[i
], WWPN_LEN
);
1387 rc
= kstrtoul(tmp_buf
, WWPN_LEN
, (ulong
*)&wwpn
[k
]);
1389 dev_err(dev
, "%s: WWPN conversion failed for port %d\n",
1397 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1402 * init_pcr() - initialize the provisioning and control registers
1403 * @cfg: Internal structure associated with the host.
1405 * Also sets up fast access to the mapped registers and initializes AFU
1406 * command fields that never change.
1408 static void init_pcr(struct cxlflash_cfg
*cfg
)
1410 struct afu
*afu
= cfg
->afu
;
1411 struct sisl_ctrl_map __iomem
*ctrl_map
;
1414 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
1415 ctrl_map
= &afu
->afu_map
->ctrls
[i
].ctrl
;
1416 /* Disrupt any clients that could be running */
1417 /* e.g. clients that survived a master restart */
1418 writeq_be(0, &ctrl_map
->rht_start
);
1419 writeq_be(0, &ctrl_map
->rht_cnt_id
);
1420 writeq_be(0, &ctrl_map
->ctx_cap
);
1423 /* Copy frequently used fields into afu */
1424 afu
->ctx_hndl
= (u16
) cxl_process_element(cfg
->mcctx
);
1425 afu
->host_map
= &afu
->afu_map
->hosts
[afu
->ctx_hndl
].host
;
1426 afu
->ctrl_map
= &afu
->afu_map
->ctrls
[afu
->ctx_hndl
].ctrl
;
1428 /* Program the Endian Control for the master context */
1429 writeq_be(SISL_ENDIAN_CTRL
, &afu
->host_map
->endian_ctrl
);
1433 * init_global() - initialize AFU global registers
1434 * @cfg: Internal structure associated with the host.
1436 static int init_global(struct cxlflash_cfg
*cfg
)
1438 struct afu
*afu
= cfg
->afu
;
1439 struct device
*dev
= &cfg
->dev
->dev
;
1440 u64 wwpn
[NUM_FC_PORTS
]; /* wwpn of AFU ports */
1441 int i
= 0, num_ports
= 0;
1445 rc
= read_vpd(cfg
, &wwpn
[0]);
1447 dev_err(dev
, "%s: could not read vpd rc=%d\n", __func__
, rc
);
1451 dev_dbg(dev
, "%s: wwpn0=%016llx wwpn1=%016llx\n",
1452 __func__
, wwpn
[0], wwpn
[1]);
1454 /* Set up RRQ and SQ in AFU for master issued cmds */
1455 writeq_be((u64
) afu
->hrrq_start
, &afu
->host_map
->rrq_start
);
1456 writeq_be((u64
) afu
->hrrq_end
, &afu
->host_map
->rrq_end
);
1458 if (afu_is_sq_cmd_mode(afu
)) {
1459 writeq_be((u64
)afu
->hsq_start
, &afu
->host_map
->sq_start
);
1460 writeq_be((u64
)afu
->hsq_end
, &afu
->host_map
->sq_end
);
1463 /* AFU configuration */
1464 reg
= readq_be(&afu
->afu_map
->global
.regs
.afu_config
);
1465 reg
|= SISL_AFUCONF_AR_ALL
|SISL_AFUCONF_ENDIAN
;
1466 /* enable all auto retry options and control endianness */
1467 /* leave others at default: */
1468 /* CTX_CAP write protected, mbox_r does not clear on read and */
1469 /* checker on if dual afu */
1470 writeq_be(reg
, &afu
->afu_map
->global
.regs
.afu_config
);
1472 /* Global port select: select either port */
1473 if (afu
->internal_lun
) {
1474 /* Only use port 0 */
1475 writeq_be(PORT0
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1476 num_ports
= NUM_FC_PORTS
- 1;
1478 writeq_be(BOTH_PORTS
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1479 num_ports
= NUM_FC_PORTS
;
1482 for (i
= 0; i
< num_ports
; i
++) {
1483 /* Unmask all errors (but they are still masked at AFU) */
1484 writeq_be(0, &afu
->afu_map
->global
.fc_regs
[i
][FC_ERRMSK
/ 8]);
1485 /* Clear CRC error cnt & set a threshold */
1486 (void)readq_be(&afu
->afu_map
->global
.
1487 fc_regs
[i
][FC_CNT_CRCERR
/ 8]);
1488 writeq_be(MC_CRC_THRESH
, &afu
->afu_map
->global
.fc_regs
[i
]
1489 [FC_CRC_THRESH
/ 8]);
1491 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
1493 afu_set_wwpn(afu
, i
,
1494 &afu
->afu_map
->global
.fc_regs
[i
][0],
1496 /* Programming WWPN back to back causes additional
1497 * offline/online transitions and a PLOGI
1502 /* Set up master's own CTX_CAP to allow real mode, host translation */
1503 /* tables, afu cmds and read/write GSCSI cmds. */
1504 /* First, unlock ctx_cap write by reading mbox */
1505 (void)readq_be(&afu
->ctrl_map
->mbox_r
); /* unlock ctx_cap */
1506 writeq_be((SISL_CTX_CAP_REAL_MODE
| SISL_CTX_CAP_HOST_XLATE
|
1507 SISL_CTX_CAP_READ_CMD
| SISL_CTX_CAP_WRITE_CMD
|
1508 SISL_CTX_CAP_AFU_CMD
| SISL_CTX_CAP_GSCSI_CMD
),
1509 &afu
->ctrl_map
->ctx_cap
);
1510 /* Initialize heartbeat */
1511 afu
->hb
= readq_be(&afu
->afu_map
->global
.regs
.afu_hb
);
1517 * start_afu() - initializes and starts the AFU
1518 * @cfg: Internal structure associated with the host.
1520 static int start_afu(struct cxlflash_cfg
*cfg
)
1522 struct afu
*afu
= cfg
->afu
;
1523 struct device
*dev
= &cfg
->dev
->dev
;
1528 /* After an AFU reset, RRQ entries are stale, clear them */
1529 memset(&afu
->rrq_entry
, 0, sizeof(afu
->rrq_entry
));
1531 /* Initialize RRQ pointers */
1532 afu
->hrrq_start
= &afu
->rrq_entry
[0];
1533 afu
->hrrq_end
= &afu
->rrq_entry
[NUM_RRQ_ENTRY
- 1];
1534 afu
->hrrq_curr
= afu
->hrrq_start
;
1538 if (afu_is_sq_cmd_mode(afu
)) {
1539 memset(&afu
->sq
, 0, sizeof(afu
->sq
));
1540 afu
->hsq_start
= &afu
->sq
[0];
1541 afu
->hsq_end
= &afu
->sq
[NUM_SQ_ENTRY
- 1];
1542 afu
->hsq_curr
= afu
->hsq_start
;
1544 spin_lock_init(&afu
->hsq_slock
);
1545 atomic_set(&afu
->hsq_credits
, NUM_SQ_ENTRY
- 1);
1548 rc
= init_global(cfg
);
1550 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1555 * init_intr() - setup interrupt handlers for the master context
1556 * @cfg: Internal structure associated with the host.
1558 * Return: 0 on success, -errno on failure
1560 static enum undo_level
init_intr(struct cxlflash_cfg
*cfg
,
1561 struct cxl_context
*ctx
)
1563 struct afu
*afu
= cfg
->afu
;
1564 struct device
*dev
= &cfg
->dev
->dev
;
1566 enum undo_level level
= UNDO_NOOP
;
1568 rc
= cxl_allocate_afu_irqs(ctx
, 3);
1570 dev_err(dev
, "%s: allocate_afu_irqs failed rc=%d\n",
1576 rc
= cxl_map_afu_irq(ctx
, 1, cxlflash_sync_err_irq
, afu
,
1577 "SISL_MSI_SYNC_ERROR");
1578 if (unlikely(rc
<= 0)) {
1579 dev_err(dev
, "%s: SISL_MSI_SYNC_ERROR map failed\n", __func__
);
1584 rc
= cxl_map_afu_irq(ctx
, 2, cxlflash_rrq_irq
, afu
,
1585 "SISL_MSI_RRQ_UPDATED");
1586 if (unlikely(rc
<= 0)) {
1587 dev_err(dev
, "%s: SISL_MSI_RRQ_UPDATED map failed\n", __func__
);
1592 rc
= cxl_map_afu_irq(ctx
, 3, cxlflash_async_err_irq
, afu
,
1593 "SISL_MSI_ASYNC_ERROR");
1594 if (unlikely(rc
<= 0)) {
1595 dev_err(dev
, "%s: SISL_MSI_ASYNC_ERROR map failed\n", __func__
);
1604 * init_mc() - create and register as the master context
1605 * @cfg: Internal structure associated with the host.
1607 * Return: 0 on success, -errno on failure
1609 static int init_mc(struct cxlflash_cfg
*cfg
)
1611 struct cxl_context
*ctx
;
1612 struct device
*dev
= &cfg
->dev
->dev
;
1614 enum undo_level level
;
1616 ctx
= cxl_get_context(cfg
->dev
);
1617 if (unlikely(!ctx
)) {
1623 /* Set it up as a master with the CXL */
1624 cxl_set_master(ctx
);
1626 /* During initialization reset the AFU to start from a clean slate */
1627 rc
= cxl_afu_reset(cfg
->mcctx
);
1629 dev_err(dev
, "%s: AFU reset failed rc=%d\n", __func__
, rc
);
1633 level
= init_intr(cfg
, ctx
);
1634 if (unlikely(level
)) {
1635 dev_err(dev
, "%s: interrupt init failed rc=%d\n", __func__
, rc
);
1639 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1640 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1641 * element (pe) that is embedded in the context (ctx)
1643 rc
= start_context(cfg
);
1645 dev_err(dev
, "%s: start context failed rc=%d\n", __func__
, rc
);
1646 level
= UNMAP_THREE
;
1650 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1653 term_intr(cfg
, level
);
1658 * init_afu() - setup as master context and start AFU
1659 * @cfg: Internal structure associated with the host.
1661 * This routine is a higher level of control for configuring the
1662 * AFU on probe and reset paths.
1664 * Return: 0 on success, -errno on failure
1666 static int init_afu(struct cxlflash_cfg
*cfg
)
1670 struct afu
*afu
= cfg
->afu
;
1671 struct device
*dev
= &cfg
->dev
->dev
;
1673 cxl_perst_reloads_same_image(cfg
->cxl_afu
, true);
1677 dev_err(dev
, "%s: init_mc failed rc=%d\n",
1682 /* Map the entire MMIO space of the AFU */
1683 afu
->afu_map
= cxl_psa_map(cfg
->mcctx
);
1684 if (!afu
->afu_map
) {
1685 dev_err(dev
, "%s: cxl_psa_map failed\n", __func__
);
1690 /* No byte reverse on reading afu_version or string will be backwards */
1691 reg
= readq(&afu
->afu_map
->global
.regs
.afu_version
);
1692 memcpy(afu
->version
, ®
, sizeof(reg
));
1693 afu
->interface_version
=
1694 readq_be(&afu
->afu_map
->global
.regs
.interface_version
);
1695 if ((afu
->interface_version
+ 1) == 0) {
1696 dev_err(dev
, "Back level AFU, please upgrade. AFU version %s "
1697 "interface version %016llx\n", afu
->version
,
1698 afu
->interface_version
);
1703 if (afu_is_sq_cmd_mode(afu
)) {
1704 afu
->send_cmd
= send_cmd_sq
;
1705 afu
->context_reset
= context_reset_sq
;
1707 afu
->send_cmd
= send_cmd_ioarrin
;
1708 afu
->context_reset
= context_reset_ioarrin
;
1711 dev_dbg(dev
, "%s: afu_ver=%s interface_ver=%016llx\n", __func__
,
1712 afu
->version
, afu
->interface_version
);
1714 rc
= start_afu(cfg
);
1716 dev_err(dev
, "%s: start_afu failed, rc=%d\n", __func__
, rc
);
1720 afu_err_intr_init(cfg
->afu
);
1721 spin_lock_init(&afu
->rrin_slock
);
1722 afu
->room
= readq_be(&afu
->host_map
->cmd_room
);
1724 /* Restore the LUN mappings */
1725 cxlflash_restore_luntable(cfg
);
1727 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1731 term_intr(cfg
, UNMAP_THREE
);
1737 * cxlflash_afu_sync() - builds and sends an AFU sync command
1738 * @afu: AFU associated with the host.
1739 * @ctx_hndl_u: Identifies context requesting sync.
1740 * @res_hndl_u: Identifies resource requesting sync.
1741 * @mode: Type of sync to issue (lightweight, heavyweight, global).
1743 * The AFU can only take 1 sync command at a time. This routine enforces this
1744 * limitation by using a mutex to provide exclusive access to the AFU during
1745 * the sync. This design point requires calling threads to not be on interrupt
1746 * context due to the possibility of sleeping during concurrent sync operations.
1748 * AFU sync operations are only necessary and allowed when the device is
1749 * operating normally. When not operating normally, sync requests can occur as
1750 * part of cleaning up resources associated with an adapter prior to removal.
1751 * In this scenario, these requests are simply ignored (safe due to the AFU
1758 int cxlflash_afu_sync(struct afu
*afu
, ctx_hndl_t ctx_hndl_u
,
1759 res_hndl_t res_hndl_u
, u8 mode
)
1761 struct cxlflash_cfg
*cfg
= afu
->parent
;
1762 struct device
*dev
= &cfg
->dev
->dev
;
1763 struct afu_cmd
*cmd
= NULL
;
1766 static DEFINE_MUTEX(sync_active
);
1768 if (cfg
->state
!= STATE_NORMAL
) {
1769 dev_dbg(dev
, "%s: Sync not required state=%u\n",
1770 __func__
, cfg
->state
);
1774 mutex_lock(&sync_active
);
1775 atomic_inc(&afu
->cmds_active
);
1776 buf
= kzalloc(sizeof(*cmd
) + __alignof__(*cmd
) - 1, GFP_KERNEL
);
1777 if (unlikely(!buf
)) {
1778 dev_err(dev
, "%s: no memory for command\n", __func__
);
1783 cmd
= (struct afu_cmd
*)PTR_ALIGN(buf
, __alignof__(*cmd
));
1784 init_completion(&cmd
->cevent
);
1787 dev_dbg(dev
, "%s: afu=%p cmd=%p %d\n", __func__
, afu
, cmd
, ctx_hndl_u
);
1789 cmd
->rcb
.req_flags
= SISL_REQ_FLAGS_AFU_CMD
;
1790 cmd
->rcb
.ctx_id
= afu
->ctx_hndl
;
1791 cmd
->rcb
.msi
= SISL_MSI_RRQ_UPDATED
;
1792 cmd
->rcb
.timeout
= MC_AFU_SYNC_TIMEOUT
;
1794 cmd
->rcb
.cdb
[0] = 0xC0; /* AFU Sync */
1795 cmd
->rcb
.cdb
[1] = mode
;
1797 /* The cdb is aligned, no unaligned accessors required */
1798 *((__be16
*)&cmd
->rcb
.cdb
[2]) = cpu_to_be16(ctx_hndl_u
);
1799 *((__be32
*)&cmd
->rcb
.cdb
[4]) = cpu_to_be32(res_hndl_u
);
1801 rc
= afu
->send_cmd(afu
, cmd
);
1805 rc
= wait_resp(afu
, cmd
);
1809 atomic_dec(&afu
->cmds_active
);
1810 mutex_unlock(&sync_active
);
1812 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1817 * afu_reset() - resets the AFU
1818 * @cfg: Internal structure associated with the host.
1820 * Return: 0 on success, -errno on failure
1822 static int afu_reset(struct cxlflash_cfg
*cfg
)
1824 struct device
*dev
= &cfg
->dev
->dev
;
1827 /* Stop the context before the reset. Since the context is
1828 * no longer available restart it after the reset is complete
1834 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1839 * drain_ioctls() - wait until all currently executing ioctls have completed
1840 * @cfg: Internal structure associated with the host.
1842 * Obtain write access to read/write semaphore that wraps ioctl
1843 * handling to 'drain' ioctls currently executing.
1845 static void drain_ioctls(struct cxlflash_cfg
*cfg
)
1847 down_write(&cfg
->ioctl_rwsem
);
1848 up_write(&cfg
->ioctl_rwsem
);
1852 * cxlflash_eh_device_reset_handler() - reset a single LUN
1853 * @scp: SCSI command to send.
1856 * SUCCESS as defined in scsi/scsi.h
1857 * FAILED as defined in scsi/scsi.h
1859 static int cxlflash_eh_device_reset_handler(struct scsi_cmnd
*scp
)
1862 struct Scsi_Host
*host
= scp
->device
->host
;
1863 struct cxlflash_cfg
*cfg
= shost_priv(host
);
1864 struct device
*dev
= &cfg
->dev
->dev
;
1865 struct afu
*afu
= cfg
->afu
;
1868 dev_dbg(dev
, "%s: (scp=%p) %d/%d/%d/%llu "
1869 "cdb=(%08x-%08x-%08x-%08x)\n", __func__
, scp
, host
->host_no
,
1870 scp
->device
->channel
, scp
->device
->id
, scp
->device
->lun
,
1871 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
1872 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
1873 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
1874 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
1877 switch (cfg
->state
) {
1879 rcr
= send_tmf(afu
, scp
, TMF_LUN_RESET
);
1884 wait_event(cfg
->reset_waitq
, cfg
->state
!= STATE_RESET
);
1891 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1896 * cxlflash_eh_host_reset_handler() - reset the host adapter
1897 * @scp: SCSI command from stack identifying host.
1899 * Following a reset, the state is evaluated again in case an EEH occurred
1900 * during the reset. In such a scenario, the host reset will either yield
1901 * until the EEH recovery is complete or return success or failure based
1902 * upon the current device state.
1905 * SUCCESS as defined in scsi/scsi.h
1906 * FAILED as defined in scsi/scsi.h
1908 static int cxlflash_eh_host_reset_handler(struct scsi_cmnd
*scp
)
1912 struct Scsi_Host
*host
= scp
->device
->host
;
1913 struct cxlflash_cfg
*cfg
= shost_priv(host
);
1914 struct device
*dev
= &cfg
->dev
->dev
;
1916 dev_dbg(dev
, "%s: (scp=%p) %d/%d/%d/%llu "
1917 "cdb=(%08x-%08x-%08x-%08x)\n", __func__
, scp
, host
->host_no
,
1918 scp
->device
->channel
, 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 cfg
->state
= STATE_RESET
;
1928 cxlflash_mark_contexts_error(cfg
);
1929 rcr
= afu_reset(cfg
);
1932 cfg
->state
= STATE_FAILTERM
;
1934 cfg
->state
= STATE_NORMAL
;
1935 wake_up_all(&cfg
->reset_waitq
);
1939 wait_event(cfg
->reset_waitq
, cfg
->state
!= STATE_RESET
);
1940 if (cfg
->state
== STATE_NORMAL
)
1948 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1953 * cxlflash_change_queue_depth() - change the queue depth for the device
1954 * @sdev: SCSI device destined for queue depth change.
1955 * @qdepth: Requested queue depth value to set.
1957 * The requested queue depth is capped to the maximum supported value.
1959 * Return: The actual queue depth set.
1961 static int cxlflash_change_queue_depth(struct scsi_device
*sdev
, int qdepth
)
1964 if (qdepth
> CXLFLASH_MAX_CMDS_PER_LUN
)
1965 qdepth
= CXLFLASH_MAX_CMDS_PER_LUN
;
1967 scsi_change_queue_depth(sdev
, qdepth
);
1968 return sdev
->queue_depth
;
1972 * cxlflash_show_port_status() - queries and presents the current port status
1973 * @port: Desired port for status reporting.
1974 * @afu: AFU owning the specified port.
1975 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
1977 * Return: The size of the ASCII string returned in @buf.
1979 static ssize_t
cxlflash_show_port_status(u32 port
, struct afu
*afu
, char *buf
)
1983 __be64 __iomem
*fc_regs
;
1985 if (port
>= NUM_FC_PORTS
)
1988 fc_regs
= &afu
->afu_map
->global
.fc_regs
[port
][0];
1989 status
= readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]);
1990 status
&= FC_MTIP_STATUS_MASK
;
1992 if (status
== FC_MTIP_STATUS_ONLINE
)
1993 disp_status
= "online";
1994 else if (status
== FC_MTIP_STATUS_OFFLINE
)
1995 disp_status
= "offline";
1997 disp_status
= "unknown";
1999 return scnprintf(buf
, PAGE_SIZE
, "%s\n", disp_status
);
2003 * port0_show() - queries and presents the current status of port 0
2004 * @dev: Generic device associated with the host owning the port.
2005 * @attr: Device attribute representing the port.
2006 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2008 * Return: The size of the ASCII string returned in @buf.
2010 static ssize_t
port0_show(struct device
*dev
,
2011 struct device_attribute
*attr
,
2014 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2015 struct afu
*afu
= cfg
->afu
;
2017 return cxlflash_show_port_status(0, afu
, buf
);
2021 * port1_show() - queries and presents the current status of port 1
2022 * @dev: Generic device associated with the host owning the port.
2023 * @attr: Device attribute representing the port.
2024 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2026 * Return: The size of the ASCII string returned in @buf.
2028 static ssize_t
port1_show(struct device
*dev
,
2029 struct device_attribute
*attr
,
2032 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2033 struct afu
*afu
= cfg
->afu
;
2035 return cxlflash_show_port_status(1, afu
, buf
);
2039 * lun_mode_show() - presents the current LUN mode of the host
2040 * @dev: Generic device associated with the host.
2041 * @attr: Device attribute representing the LUN mode.
2042 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2044 * Return: The size of the ASCII string returned in @buf.
2046 static ssize_t
lun_mode_show(struct device
*dev
,
2047 struct device_attribute
*attr
, char *buf
)
2049 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2050 struct afu
*afu
= cfg
->afu
;
2052 return scnprintf(buf
, PAGE_SIZE
, "%u\n", afu
->internal_lun
);
2056 * lun_mode_store() - sets the LUN mode of the host
2057 * @dev: Generic device associated with the host.
2058 * @attr: Device attribute representing the LUN mode.
2059 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2060 * @count: Length of data resizing in @buf.
2062 * The CXL Flash AFU supports a dummy LUN mode where the external
2063 * links and storage are not required. Space on the FPGA is used
2064 * to create 1 or 2 small LUNs which are presented to the system
2065 * as if they were a normal storage device. This feature is useful
2066 * during development and also provides manufacturing with a way
2067 * to test the AFU without an actual device.
2069 * 0 = external LUN[s] (default)
2070 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2071 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2072 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2073 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2075 * Return: The size of the ASCII string returned in @buf.
2077 static ssize_t
lun_mode_store(struct device
*dev
,
2078 struct device_attribute
*attr
,
2079 const char *buf
, size_t count
)
2081 struct Scsi_Host
*shost
= class_to_shost(dev
);
2082 struct cxlflash_cfg
*cfg
= shost_priv(shost
);
2083 struct afu
*afu
= cfg
->afu
;
2087 rc
= kstrtouint(buf
, 10, &lun_mode
);
2088 if (!rc
&& (lun_mode
< 5) && (lun_mode
!= afu
->internal_lun
)) {
2089 afu
->internal_lun
= lun_mode
;
2092 * When configured for internal LUN, there is only one channel,
2093 * channel number 0, else there will be 2 (default).
2095 if (afu
->internal_lun
)
2096 shost
->max_channel
= 0;
2098 shost
->max_channel
= NUM_FC_PORTS
- 1;
2101 scsi_scan_host(cfg
->host
);
2108 * ioctl_version_show() - presents the current ioctl version of the host
2109 * @dev: Generic device associated with the host.
2110 * @attr: Device attribute representing the ioctl version.
2111 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2113 * Return: The size of the ASCII string returned in @buf.
2115 static ssize_t
ioctl_version_show(struct device
*dev
,
2116 struct device_attribute
*attr
, char *buf
)
2118 return scnprintf(buf
, PAGE_SIZE
, "%u\n", DK_CXLFLASH_VERSION_0
);
2122 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2123 * @port: Desired port for status reporting.
2124 * @afu: AFU owning the specified port.
2125 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2127 * Return: The size of the ASCII string returned in @buf.
2129 static ssize_t
cxlflash_show_port_lun_table(u32 port
,
2135 __be64 __iomem
*fc_port
;
2137 if (port
>= NUM_FC_PORTS
)
2140 fc_port
= &afu
->afu_map
->global
.fc_port
[port
][0];
2142 for (i
= 0; i
< CXLFLASH_NUM_VLUNS
; i
++)
2143 bytes
+= scnprintf(buf
+ bytes
, PAGE_SIZE
- bytes
,
2144 "%03d: %016llx\n", i
, readq_be(&fc_port
[i
]));
2149 * port0_lun_table_show() - presents the current LUN table of port 0
2150 * @dev: Generic device associated with the host owning the port.
2151 * @attr: Device attribute representing the port.
2152 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2154 * Return: The size of the ASCII string returned in @buf.
2156 static ssize_t
port0_lun_table_show(struct device
*dev
,
2157 struct device_attribute
*attr
,
2160 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2161 struct afu
*afu
= cfg
->afu
;
2163 return cxlflash_show_port_lun_table(0, afu
, buf
);
2167 * port1_lun_table_show() - presents the current LUN table of port 1
2168 * @dev: Generic device associated with the host owning the port.
2169 * @attr: Device attribute representing the port.
2170 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2172 * Return: The size of the ASCII string returned in @buf.
2174 static ssize_t
port1_lun_table_show(struct device
*dev
,
2175 struct device_attribute
*attr
,
2178 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2179 struct afu
*afu
= cfg
->afu
;
2181 return cxlflash_show_port_lun_table(1, afu
, buf
);
2185 * mode_show() - presents the current mode of the device
2186 * @dev: Generic device associated with the device.
2187 * @attr: Device attribute representing the device mode.
2188 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2190 * Return: The size of the ASCII string returned in @buf.
2192 static ssize_t
mode_show(struct device
*dev
,
2193 struct device_attribute
*attr
, char *buf
)
2195 struct scsi_device
*sdev
= to_scsi_device(dev
);
2197 return scnprintf(buf
, PAGE_SIZE
, "%s\n",
2198 sdev
->hostdata
? "superpipe" : "legacy");
2204 static DEVICE_ATTR_RO(port0
);
2205 static DEVICE_ATTR_RO(port1
);
2206 static DEVICE_ATTR_RW(lun_mode
);
2207 static DEVICE_ATTR_RO(ioctl_version
);
2208 static DEVICE_ATTR_RO(port0_lun_table
);
2209 static DEVICE_ATTR_RO(port1_lun_table
);
2211 static struct device_attribute
*cxlflash_host_attrs
[] = {
2215 &dev_attr_ioctl_version
,
2216 &dev_attr_port0_lun_table
,
2217 &dev_attr_port1_lun_table
,
2224 static DEVICE_ATTR_RO(mode
);
2226 static struct device_attribute
*cxlflash_dev_attrs
[] = {
2234 static struct scsi_host_template driver_template
= {
2235 .module
= THIS_MODULE
,
2236 .name
= CXLFLASH_ADAPTER_NAME
,
2237 .info
= cxlflash_driver_info
,
2238 .ioctl
= cxlflash_ioctl
,
2239 .proc_name
= CXLFLASH_NAME
,
2240 .queuecommand
= cxlflash_queuecommand
,
2241 .eh_device_reset_handler
= cxlflash_eh_device_reset_handler
,
2242 .eh_host_reset_handler
= cxlflash_eh_host_reset_handler
,
2243 .change_queue_depth
= cxlflash_change_queue_depth
,
2244 .cmd_per_lun
= CXLFLASH_MAX_CMDS_PER_LUN
,
2245 .can_queue
= CXLFLASH_MAX_CMDS
,
2246 .cmd_size
= sizeof(struct afu_cmd
) + __alignof__(struct afu_cmd
) - 1,
2248 .sg_tablesize
= 1, /* No scatter gather support */
2249 .max_sectors
= CXLFLASH_MAX_SECTORS
,
2250 .use_clustering
= ENABLE_CLUSTERING
,
2251 .shost_attrs
= cxlflash_host_attrs
,
2252 .sdev_attrs
= cxlflash_dev_attrs
,
2256 * Device dependent values
2258 static struct dev_dependent_vals dev_corsa_vals
= { CXLFLASH_MAX_SECTORS
,
2260 static struct dev_dependent_vals dev_flash_gt_vals
= { CXLFLASH_MAX_SECTORS
,
2261 CXLFLASH_NOTIFY_SHUTDOWN
};
2264 * PCI device binding table
2266 static struct pci_device_id cxlflash_pci_table
[] = {
2267 {PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_IBM_CORSA
,
2268 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (kernel_ulong_t
)&dev_corsa_vals
},
2269 {PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_IBM_FLASH_GT
,
2270 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (kernel_ulong_t
)&dev_flash_gt_vals
},
2274 MODULE_DEVICE_TABLE(pci
, cxlflash_pci_table
);
2277 * cxlflash_worker_thread() - work thread handler for the AFU
2278 * @work: Work structure contained within cxlflash associated with host.
2280 * Handles the following events:
2281 * - Link reset which cannot be performed on interrupt context due to
2282 * blocking up to a few seconds
2285 static void cxlflash_worker_thread(struct work_struct
*work
)
2287 struct cxlflash_cfg
*cfg
= container_of(work
, struct cxlflash_cfg
,
2289 struct afu
*afu
= cfg
->afu
;
2290 struct device
*dev
= &cfg
->dev
->dev
;
2294 /* Avoid MMIO if the device has failed */
2296 if (cfg
->state
!= STATE_NORMAL
)
2299 spin_lock_irqsave(cfg
->host
->host_lock
, lock_flags
);
2301 if (cfg
->lr_state
== LINK_RESET_REQUIRED
) {
2302 port
= cfg
->lr_port
;
2304 dev_err(dev
, "%s: invalid port index %d\n",
2307 spin_unlock_irqrestore(cfg
->host
->host_lock
,
2310 /* The reset can block... */
2311 afu_link_reset(afu
, port
,
2312 &afu
->afu_map
->global
.fc_regs
[port
][0]);
2313 spin_lock_irqsave(cfg
->host
->host_lock
, lock_flags
);
2316 cfg
->lr_state
= LINK_RESET_COMPLETE
;
2319 spin_unlock_irqrestore(cfg
->host
->host_lock
, lock_flags
);
2321 if (atomic_dec_if_positive(&cfg
->scan_host_needed
) >= 0)
2322 scsi_scan_host(cfg
->host
);
2326 * cxlflash_probe() - PCI entry point to add host
2327 * @pdev: PCI device associated with the host.
2328 * @dev_id: PCI device id associated with device.
2330 * Return: 0 on success, -errno on failure
2332 static int cxlflash_probe(struct pci_dev
*pdev
,
2333 const struct pci_device_id
*dev_id
)
2335 struct Scsi_Host
*host
;
2336 struct cxlflash_cfg
*cfg
= NULL
;
2337 struct device
*dev
= &pdev
->dev
;
2338 struct dev_dependent_vals
*ddv
;
2341 dev_dbg(&pdev
->dev
, "%s: Found CXLFLASH with IRQ: %d\n",
2342 __func__
, pdev
->irq
);
2344 ddv
= (struct dev_dependent_vals
*)dev_id
->driver_data
;
2345 driver_template
.max_sectors
= ddv
->max_sectors
;
2347 host
= scsi_host_alloc(&driver_template
, sizeof(struct cxlflash_cfg
));
2349 dev_err(dev
, "%s: scsi_host_alloc failed\n", __func__
);
2354 host
->max_id
= CXLFLASH_MAX_NUM_TARGETS_PER_BUS
;
2355 host
->max_lun
= CXLFLASH_MAX_NUM_LUNS_PER_TARGET
;
2356 host
->max_channel
= NUM_FC_PORTS
- 1;
2357 host
->unique_id
= host
->host_no
;
2358 host
->max_cmd_len
= CXLFLASH_MAX_CDB_LEN
;
2360 cfg
= shost_priv(host
);
2362 rc
= alloc_mem(cfg
);
2364 dev_err(dev
, "%s: alloc_mem failed\n", __func__
);
2366 scsi_host_put(cfg
->host
);
2370 cfg
->init_state
= INIT_STATE_NONE
;
2372 cfg
->cxl_fops
= cxlflash_cxl_fops
;
2375 * The promoted LUNs move to the top of the LUN table. The rest stay
2376 * on the bottom half. The bottom half grows from the end
2377 * (index = 255), whereas the top half grows from the beginning
2380 cfg
->promote_lun_index
= 0;
2381 cfg
->last_lun_index
[0] = CXLFLASH_NUM_VLUNS
/2 - 1;
2382 cfg
->last_lun_index
[1] = CXLFLASH_NUM_VLUNS
/2 - 1;
2384 cfg
->dev_id
= (struct pci_device_id
*)dev_id
;
2386 init_waitqueue_head(&cfg
->tmf_waitq
);
2387 init_waitqueue_head(&cfg
->reset_waitq
);
2389 INIT_WORK(&cfg
->work_q
, cxlflash_worker_thread
);
2390 cfg
->lr_state
= LINK_RESET_INVALID
;
2392 spin_lock_init(&cfg
->tmf_slock
);
2393 mutex_init(&cfg
->ctx_tbl_list_mutex
);
2394 mutex_init(&cfg
->ctx_recovery_mutex
);
2395 init_rwsem(&cfg
->ioctl_rwsem
);
2396 INIT_LIST_HEAD(&cfg
->ctx_err_recovery
);
2397 INIT_LIST_HEAD(&cfg
->lluns
);
2399 pci_set_drvdata(pdev
, cfg
);
2401 cfg
->cxl_afu
= cxl_pci_to_afu(pdev
);
2405 dev_err(dev
, "%s: init_pci failed rc=%d\n", __func__
, rc
);
2408 cfg
->init_state
= INIT_STATE_PCI
;
2412 dev_err(dev
, "%s: init_afu failed rc=%d\n", __func__
, rc
);
2415 cfg
->init_state
= INIT_STATE_AFU
;
2417 rc
= init_scsi(cfg
);
2419 dev_err(dev
, "%s: init_scsi failed rc=%d\n", __func__
, rc
);
2422 cfg
->init_state
= INIT_STATE_SCSI
;
2425 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
2429 cxlflash_remove(pdev
);
2434 * cxlflash_pci_error_detected() - called when a PCI error is detected
2435 * @pdev: PCI device struct.
2436 * @state: PCI channel state.
2438 * When an EEH occurs during an active reset, wait until the reset is
2439 * complete and then take action based upon the device state.
2441 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2443 static pci_ers_result_t
cxlflash_pci_error_detected(struct pci_dev
*pdev
,
2444 pci_channel_state_t state
)
2447 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
2448 struct device
*dev
= &cfg
->dev
->dev
;
2450 dev_dbg(dev
, "%s: pdev=%p state=%u\n", __func__
, pdev
, state
);
2453 case pci_channel_io_frozen
:
2454 wait_event(cfg
->reset_waitq
, cfg
->state
!= STATE_RESET
);
2455 if (cfg
->state
== STATE_FAILTERM
)
2456 return PCI_ERS_RESULT_DISCONNECT
;
2458 cfg
->state
= STATE_RESET
;
2459 scsi_block_requests(cfg
->host
);
2461 rc
= cxlflash_mark_contexts_error(cfg
);
2463 dev_err(dev
, "%s: Failed to mark user contexts rc=%d\n",
2466 return PCI_ERS_RESULT_NEED_RESET
;
2467 case pci_channel_io_perm_failure
:
2468 cfg
->state
= STATE_FAILTERM
;
2469 wake_up_all(&cfg
->reset_waitq
);
2470 scsi_unblock_requests(cfg
->host
);
2471 return PCI_ERS_RESULT_DISCONNECT
;
2475 return PCI_ERS_RESULT_NEED_RESET
;
2479 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2480 * @pdev: PCI device struct.
2482 * This routine is called by the pci error recovery code after the PCI
2483 * slot has been reset, just before we should resume normal operations.
2485 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2487 static pci_ers_result_t
cxlflash_pci_slot_reset(struct pci_dev
*pdev
)
2490 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
2491 struct device
*dev
= &cfg
->dev
->dev
;
2493 dev_dbg(dev
, "%s: pdev=%p\n", __func__
, pdev
);
2497 dev_err(dev
, "%s: EEH recovery failed rc=%d\n", __func__
, rc
);
2498 return PCI_ERS_RESULT_DISCONNECT
;
2501 return PCI_ERS_RESULT_RECOVERED
;
2505 * cxlflash_pci_resume() - called when normal operation can resume
2506 * @pdev: PCI device struct
2508 static void cxlflash_pci_resume(struct pci_dev
*pdev
)
2510 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
2511 struct device
*dev
= &cfg
->dev
->dev
;
2513 dev_dbg(dev
, "%s: pdev=%p\n", __func__
, pdev
);
2515 cfg
->state
= STATE_NORMAL
;
2516 wake_up_all(&cfg
->reset_waitq
);
2517 scsi_unblock_requests(cfg
->host
);
2520 static const struct pci_error_handlers cxlflash_err_handler
= {
2521 .error_detected
= cxlflash_pci_error_detected
,
2522 .slot_reset
= cxlflash_pci_slot_reset
,
2523 .resume
= cxlflash_pci_resume
,
2527 * PCI device structure
2529 static struct pci_driver cxlflash_driver
= {
2530 .name
= CXLFLASH_NAME
,
2531 .id_table
= cxlflash_pci_table
,
2532 .probe
= cxlflash_probe
,
2533 .remove
= cxlflash_remove
,
2534 .shutdown
= cxlflash_remove
,
2535 .err_handler
= &cxlflash_err_handler
,
2539 * init_cxlflash() - module entry point
2541 * Return: 0 on success, -errno on failure
2543 static int __init
init_cxlflash(void)
2545 cxlflash_list_init();
2547 return pci_register_driver(&cxlflash_driver
);
2551 * exit_cxlflash() - module exit point
2553 static void __exit
exit_cxlflash(void)
2555 cxlflash_term_global_luns();
2556 cxlflash_free_errpage();
2558 pci_unregister_driver(&cxlflash_driver
);
2561 module_init(init_cxlflash
);
2562 module_exit(exit_cxlflash
);