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
;
165 struct hwq
*hwq
= get_hwq(afu
, cmd
->hwq_index
);
168 spin_lock_irqsave(&hwq
->hsq_slock
, lock_flags
);
169 list_del(&cmd
->list
);
170 spin_unlock_irqrestore(&hwq
->hsq_slock
, lock_flags
);
174 if (unlikely(cmd
->sa
.ioasc
))
175 process_cmd_err(cmd
, scp
);
177 scp
->result
= (DID_OK
<< 16);
179 cmd_is_tmf
= cmd
->cmd_tmf
;
181 dev_dbg_ratelimited(dev
, "%s:scp=%p result=%08x ioasc=%08x\n",
182 __func__
, scp
, scp
->result
, cmd
->sa
.ioasc
);
187 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
188 cfg
->tmf_active
= false;
189 wake_up_all_locked(&cfg
->tmf_waitq
);
190 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
193 complete(&cmd
->cevent
);
197 * flush_pending_cmds() - flush all pending commands on this hardware queue
198 * @hwq: Hardware queue to flush.
200 * The hardware send queue lock associated with this hardware queue must be
201 * held when calling this routine.
203 static void flush_pending_cmds(struct hwq
*hwq
)
205 struct afu_cmd
*cmd
, *tmp
;
206 struct scsi_cmnd
*scp
;
208 list_for_each_entry_safe(cmd
, tmp
, &hwq
->pending_cmds
, list
) {
209 /* Bypass command when on a doneq, cmd_complete() will handle */
210 if (!list_empty(&cmd
->queue
))
213 list_del(&cmd
->list
);
217 scp
->result
= (DID_IMM_RETRY
<< 16);
220 cmd
->cmd_aborted
= true;
221 complete(&cmd
->cevent
);
227 * context_reset() - reset context via specified register
228 * @hwq: Hardware queue owning the context to be reset.
229 * @reset_reg: MMIO register to perform reset.
231 * Return: 0 on success, -errno on failure
233 static int context_reset(struct hwq
*hwq
, __be64 __iomem
*reset_reg
)
235 struct cxlflash_cfg
*cfg
= hwq
->afu
->parent
;
236 struct device
*dev
= &cfg
->dev
->dev
;
241 dev_dbg(dev
, "%s: hwq=%p\n", __func__
, hwq
);
243 writeq_be(val
, reset_reg
);
245 val
= readq_be(reset_reg
);
246 if ((val
& 0x1) == 0x0) {
251 /* Double delay each time */
253 } while (nretry
++ < MC_ROOM_RETRY_CNT
);
255 dev_dbg(dev
, "%s: returning rc=%d, val=%016llx nretry=%d\n",
256 __func__
, rc
, val
, nretry
);
261 * context_reset_ioarrin() - reset context via IOARRIN register
262 * @hwq: Hardware queue owning the context to be reset.
264 * Return: 0 on success, -errno on failure
266 static int context_reset_ioarrin(struct hwq
*hwq
)
268 return context_reset(hwq
, &hwq
->host_map
->ioarrin
);
272 * context_reset_sq() - reset context via SQ_CONTEXT_RESET register
273 * @hwq: Hardware queue owning the context to be reset.
275 * Return: 0 on success, -errno on failure
277 static int context_reset_sq(struct hwq
*hwq
)
279 return context_reset(hwq
, &hwq
->host_map
->sq_ctx_reset
);
283 * send_cmd_ioarrin() - sends an AFU command via IOARRIN register
284 * @afu: AFU associated with the host.
285 * @cmd: AFU command to send.
288 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
290 static int send_cmd_ioarrin(struct afu
*afu
, struct afu_cmd
*cmd
)
292 struct cxlflash_cfg
*cfg
= afu
->parent
;
293 struct device
*dev
= &cfg
->dev
->dev
;
294 struct hwq
*hwq
= get_hwq(afu
, cmd
->hwq_index
);
300 * To avoid the performance penalty of MMIO, spread the update of
301 * 'room' over multiple commands.
303 spin_lock_irqsave(&hwq
->hsq_slock
, lock_flags
);
304 if (--hwq
->room
< 0) {
305 room
= readq_be(&hwq
->host_map
->cmd_room
);
307 dev_dbg_ratelimited(dev
, "%s: no cmd_room to send "
308 "0x%02X, room=0x%016llX\n",
309 __func__
, cmd
->rcb
.cdb
[0], room
);
311 rc
= SCSI_MLQUEUE_HOST_BUSY
;
314 hwq
->room
= room
- 1;
317 list_add(&cmd
->list
, &hwq
->pending_cmds
);
318 writeq_be((u64
)&cmd
->rcb
, &hwq
->host_map
->ioarrin
);
320 spin_unlock_irqrestore(&hwq
->hsq_slock
, lock_flags
);
321 dev_dbg(dev
, "%s: cmd=%p len=%u ea=%016llx rc=%d\n", __func__
,
322 cmd
, cmd
->rcb
.data_len
, cmd
->rcb
.data_ea
, rc
);
327 * send_cmd_sq() - sends an AFU command via SQ ring
328 * @afu: AFU associated with the host.
329 * @cmd: AFU command to send.
332 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
334 static int send_cmd_sq(struct afu
*afu
, struct afu_cmd
*cmd
)
336 struct cxlflash_cfg
*cfg
= afu
->parent
;
337 struct device
*dev
= &cfg
->dev
->dev
;
338 struct hwq
*hwq
= get_hwq(afu
, cmd
->hwq_index
);
343 newval
= atomic_dec_if_positive(&hwq
->hsq_credits
);
345 rc
= SCSI_MLQUEUE_HOST_BUSY
;
349 cmd
->rcb
.ioasa
= &cmd
->sa
;
351 spin_lock_irqsave(&hwq
->hsq_slock
, lock_flags
);
353 *hwq
->hsq_curr
= cmd
->rcb
;
354 if (hwq
->hsq_curr
< hwq
->hsq_end
)
357 hwq
->hsq_curr
= hwq
->hsq_start
;
359 list_add(&cmd
->list
, &hwq
->pending_cmds
);
360 writeq_be((u64
)hwq
->hsq_curr
, &hwq
->host_map
->sq_tail
);
362 spin_unlock_irqrestore(&hwq
->hsq_slock
, lock_flags
);
364 dev_dbg(dev
, "%s: cmd=%p len=%u ea=%016llx ioasa=%p rc=%d curr=%p "
365 "head=%016llx tail=%016llx\n", __func__
, cmd
, cmd
->rcb
.data_len
,
366 cmd
->rcb
.data_ea
, cmd
->rcb
.ioasa
, rc
, hwq
->hsq_curr
,
367 readq_be(&hwq
->host_map
->sq_head
),
368 readq_be(&hwq
->host_map
->sq_tail
));
373 * wait_resp() - polls for a response or timeout to a sent AFU command
374 * @afu: AFU associated with the host.
375 * @cmd: AFU command that was sent.
377 * Return: 0 on success, -errno on failure
379 static int wait_resp(struct afu
*afu
, struct afu_cmd
*cmd
)
381 struct cxlflash_cfg
*cfg
= afu
->parent
;
382 struct device
*dev
= &cfg
->dev
->dev
;
384 ulong timeout
= msecs_to_jiffies(cmd
->rcb
.timeout
* 2 * 1000);
386 timeout
= wait_for_completion_timeout(&cmd
->cevent
, timeout
);
390 if (cmd
->cmd_aborted
)
393 if (unlikely(cmd
->sa
.ioasc
!= 0)) {
394 dev_err(dev
, "%s: cmd %02x failed, ioasc=%08x\n",
395 __func__
, cmd
->rcb
.cdb
[0], cmd
->sa
.ioasc
);
403 * cmd_to_target_hwq() - selects a target hardware queue for a SCSI command
404 * @host: SCSI host associated with device.
405 * @scp: SCSI command to send.
406 * @afu: SCSI command to send.
408 * Hashes a command based upon the hardware queue mode.
410 * Return: Trusted index of target hardware queue
412 static u32
cmd_to_target_hwq(struct Scsi_Host
*host
, struct scsi_cmnd
*scp
,
418 if (afu
->num_hwqs
== 1)
421 switch (afu
->hwq_mode
) {
423 hwq
= afu
->hwq_rr_count
++ % afu
->num_hwqs
;
426 tag
= blk_mq_unique_tag(scp
->request
);
427 hwq
= blk_mq_unique_tag_to_hwq(tag
);
430 hwq
= smp_processor_id() % afu
->num_hwqs
;
440 * send_tmf() - sends a Task Management Function (TMF)
441 * @afu: AFU to checkout from.
442 * @scp: SCSI command from stack.
443 * @tmfcmd: TMF command to send.
446 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
448 static int send_tmf(struct afu
*afu
, struct scsi_cmnd
*scp
, u64 tmfcmd
)
450 struct Scsi_Host
*host
= scp
->device
->host
;
451 struct cxlflash_cfg
*cfg
= shost_priv(host
);
452 struct afu_cmd
*cmd
= sc_to_afucz(scp
);
453 struct device
*dev
= &cfg
->dev
->dev
;
454 int hwq_index
= cmd_to_target_hwq(host
, scp
, afu
);
455 struct hwq
*hwq
= get_hwq(afu
, hwq_index
);
460 /* When Task Management Function is active do not send another */
461 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
463 wait_event_interruptible_lock_irq(cfg
->tmf_waitq
,
466 cfg
->tmf_active
= true;
467 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
472 cmd
->hwq_index
= hwq_index
;
474 cmd
->rcb
.ctx_id
= hwq
->ctx_hndl
;
475 cmd
->rcb
.msi
= SISL_MSI_RRQ_UPDATED
;
476 cmd
->rcb
.port_sel
= CHAN2PORTMASK(scp
->device
->channel
);
477 cmd
->rcb
.lun_id
= lun_to_lunid(scp
->device
->lun
);
478 cmd
->rcb
.req_flags
= (SISL_REQ_FLAGS_PORT_LUN_ID
|
479 SISL_REQ_FLAGS_SUP_UNDERRUN
|
480 SISL_REQ_FLAGS_TMF_CMD
);
481 memcpy(cmd
->rcb
.cdb
, &tmfcmd
, sizeof(tmfcmd
));
483 rc
= afu
->send_cmd(afu
, cmd
);
485 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
486 cfg
->tmf_active
= false;
487 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
491 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
492 to
= msecs_to_jiffies(5000);
493 to
= wait_event_interruptible_lock_irq_timeout(cfg
->tmf_waitq
,
498 cfg
->tmf_active
= false;
499 dev_err(dev
, "%s: TMF timed out\n", __func__
);
502 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
508 * cxlflash_driver_info() - information handler for this host driver
509 * @host: SCSI host associated with device.
511 * Return: A string describing the device.
513 static const char *cxlflash_driver_info(struct Scsi_Host
*host
)
515 return CXLFLASH_ADAPTER_NAME
;
519 * cxlflash_queuecommand() - sends a mid-layer request
520 * @host: SCSI host associated with device.
521 * @scp: SCSI command to send.
523 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
525 static int cxlflash_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*scp
)
527 struct cxlflash_cfg
*cfg
= shost_priv(host
);
528 struct afu
*afu
= cfg
->afu
;
529 struct device
*dev
= &cfg
->dev
->dev
;
530 struct afu_cmd
*cmd
= sc_to_afucz(scp
);
531 struct scatterlist
*sg
= scsi_sglist(scp
);
532 int hwq_index
= cmd_to_target_hwq(host
, scp
, afu
);
533 struct hwq
*hwq
= get_hwq(afu
, hwq_index
);
534 u16 req_flags
= SISL_REQ_FLAGS_SUP_UNDERRUN
;
538 dev_dbg_ratelimited(dev
, "%s: (scp=%p) %d/%d/%d/%llu "
539 "cdb=(%08x-%08x-%08x-%08x)\n",
540 __func__
, scp
, host
->host_no
, scp
->device
->channel
,
541 scp
->device
->id
, scp
->device
->lun
,
542 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
543 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
544 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
545 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
548 * If a Task Management Function is active, wait for it to complete
549 * before continuing with regular commands.
551 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
552 if (cfg
->tmf_active
) {
553 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
554 rc
= SCSI_MLQUEUE_HOST_BUSY
;
557 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
559 switch (cfg
->state
) {
563 dev_dbg_ratelimited(dev
, "%s: device is in reset\n", __func__
);
564 rc
= SCSI_MLQUEUE_HOST_BUSY
;
567 dev_dbg_ratelimited(dev
, "%s: device has failed\n", __func__
);
568 scp
->result
= (DID_NO_CONNECT
<< 16);
577 cmd
->rcb
.data_len
= sg
->length
;
578 cmd
->rcb
.data_ea
= (uintptr_t)sg_virt(sg
);
583 cmd
->hwq_index
= hwq_index
;
585 cmd
->rcb
.ctx_id
= hwq
->ctx_hndl
;
586 cmd
->rcb
.msi
= SISL_MSI_RRQ_UPDATED
;
587 cmd
->rcb
.port_sel
= CHAN2PORTMASK(scp
->device
->channel
);
588 cmd
->rcb
.lun_id
= lun_to_lunid(scp
->device
->lun
);
590 if (scp
->sc_data_direction
== DMA_TO_DEVICE
)
591 req_flags
|= SISL_REQ_FLAGS_HOST_WRITE
;
593 cmd
->rcb
.req_flags
= req_flags
;
594 memcpy(cmd
->rcb
.cdb
, scp
->cmnd
, sizeof(cmd
->rcb
.cdb
));
596 rc
= afu
->send_cmd(afu
, cmd
);
602 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
603 * @cfg: Internal structure associated with the host.
605 static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg
*cfg
)
607 struct pci_dev
*pdev
= cfg
->dev
;
609 if (pci_channel_offline(pdev
))
610 wait_event_timeout(cfg
->reset_waitq
,
611 !pci_channel_offline(pdev
),
612 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT
);
616 * free_mem() - free memory associated with the AFU
617 * @cfg: Internal structure associated with the host.
619 static void free_mem(struct cxlflash_cfg
*cfg
)
621 struct afu
*afu
= cfg
->afu
;
624 free_pages((ulong
)afu
, get_order(sizeof(struct afu
)));
630 * cxlflash_reset_sync() - synchronizing point for asynchronous resets
631 * @cfg: Internal structure associated with the host.
633 static void cxlflash_reset_sync(struct cxlflash_cfg
*cfg
)
635 if (cfg
->async_reset_cookie
== 0)
638 /* Wait until all async calls prior to this cookie have completed */
639 async_synchronize_cookie(cfg
->async_reset_cookie
+ 1);
640 cfg
->async_reset_cookie
= 0;
644 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
645 * @cfg: Internal structure associated with the host.
647 * Safe to call with AFU in a partially allocated/initialized state.
649 * Cancels scheduled worker threads, waits for any active internal AFU
650 * commands to timeout, disables IRQ polling and then unmaps the MMIO space.
652 static void stop_afu(struct cxlflash_cfg
*cfg
)
654 struct afu
*afu
= cfg
->afu
;
658 cancel_work_sync(&cfg
->work_q
);
659 if (!current_is_async())
660 cxlflash_reset_sync(cfg
);
663 while (atomic_read(&afu
->cmds_active
))
666 if (afu_is_irqpoll_enabled(afu
)) {
667 for (i
= 0; i
< afu
->num_hwqs
; i
++) {
668 hwq
= get_hwq(afu
, i
);
670 irq_poll_disable(&hwq
->irqpoll
);
674 if (likely(afu
->afu_map
)) {
675 cxl_psa_unmap((void __iomem
*)afu
->afu_map
);
682 * term_intr() - disables all AFU interrupts
683 * @cfg: Internal structure associated with the host.
684 * @level: Depth of allocation, where to begin waterfall tear down.
685 * @index: Index of the hardware queue.
687 * Safe to call with AFU/MC in partially allocated/initialized state.
689 static void term_intr(struct cxlflash_cfg
*cfg
, enum undo_level level
,
692 struct afu
*afu
= cfg
->afu
;
693 struct device
*dev
= &cfg
->dev
->dev
;
697 dev_err(dev
, "%s: returning with NULL afu\n", __func__
);
701 hwq
= get_hwq(afu
, index
);
704 dev_err(dev
, "%s: returning with NULL MC\n", __func__
);
710 /* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
711 if (index
== PRIMARY_HWQ
)
712 cxl_unmap_afu_irq(hwq
->ctx
, 3, hwq
);
714 cxl_unmap_afu_irq(hwq
->ctx
, 2, hwq
);
716 cxl_unmap_afu_irq(hwq
->ctx
, 1, hwq
);
718 cxl_free_afu_irqs(hwq
->ctx
);
721 /* No action required */
727 * term_mc() - terminates the master context
728 * @cfg: Internal structure associated with the host.
729 * @index: Index of the hardware queue.
731 * Safe to call with AFU/MC in partially allocated/initialized state.
733 static void term_mc(struct cxlflash_cfg
*cfg
, u32 index
)
735 struct afu
*afu
= cfg
->afu
;
736 struct device
*dev
= &cfg
->dev
->dev
;
741 dev_err(dev
, "%s: returning with NULL afu\n", __func__
);
745 hwq
= get_hwq(afu
, index
);
748 dev_err(dev
, "%s: returning with NULL MC\n", __func__
);
752 WARN_ON(cxl_stop_context(hwq
->ctx
));
753 if (index
!= PRIMARY_HWQ
)
754 WARN_ON(cxl_release_context(hwq
->ctx
));
757 spin_lock_irqsave(&hwq
->hsq_slock
, lock_flags
);
758 flush_pending_cmds(hwq
);
759 spin_unlock_irqrestore(&hwq
->hsq_slock
, lock_flags
);
763 * term_afu() - terminates the AFU
764 * @cfg: Internal structure associated with the host.
766 * Safe to call with AFU/MC in partially allocated/initialized state.
768 static void term_afu(struct cxlflash_cfg
*cfg
)
770 struct device
*dev
= &cfg
->dev
->dev
;
774 * Tear down is carefully orchestrated to ensure
775 * no interrupts can come in when the problem state
778 * 1) Disable all AFU interrupts for each master
779 * 2) Unmap the problem state area
780 * 3) Stop each master context
782 for (k
= cfg
->afu
->num_hwqs
- 1; k
>= 0; k
--)
783 term_intr(cfg
, UNMAP_THREE
, k
);
788 for (k
= cfg
->afu
->num_hwqs
- 1; k
>= 0; k
--)
791 dev_dbg(dev
, "%s: returning\n", __func__
);
795 * notify_shutdown() - notifies device of pending shutdown
796 * @cfg: Internal structure associated with the host.
797 * @wait: Whether to wait for shutdown processing to complete.
799 * This function will notify the AFU that the adapter is being shutdown
800 * and will wait for shutdown processing to complete if wait is true.
801 * This notification should flush pending I/Os to the device and halt
802 * further I/Os until the next AFU reset is issued and device restarted.
804 static void notify_shutdown(struct cxlflash_cfg
*cfg
, bool wait
)
806 struct afu
*afu
= cfg
->afu
;
807 struct device
*dev
= &cfg
->dev
->dev
;
808 struct dev_dependent_vals
*ddv
;
809 __be64 __iomem
*fc_port_regs
;
811 int i
, retry_cnt
= 0;
813 ddv
= (struct dev_dependent_vals
*)cfg
->dev_id
->driver_data
;
814 if (!(ddv
->flags
& CXLFLASH_NOTIFY_SHUTDOWN
))
817 if (!afu
|| !afu
->afu_map
) {
818 dev_dbg(dev
, "%s: Problem state area not mapped\n", __func__
);
823 for (i
= 0; i
< cfg
->num_fc_ports
; i
++) {
824 fc_port_regs
= get_fc_port_regs(cfg
, i
);
826 reg
= readq_be(&fc_port_regs
[FC_CONFIG2
/ 8]);
827 reg
|= SISL_FC_SHUTDOWN_NORMAL
;
828 writeq_be(reg
, &fc_port_regs
[FC_CONFIG2
/ 8]);
834 /* Wait up to 1.5 seconds for shutdown processing to complete */
835 for (i
= 0; i
< cfg
->num_fc_ports
; i
++) {
836 fc_port_regs
= get_fc_port_regs(cfg
, i
);
840 status
= readq_be(&fc_port_regs
[FC_STATUS
/ 8]);
841 if (status
& SISL_STATUS_SHUTDOWN_COMPLETE
)
843 if (++retry_cnt
>= MC_RETRY_CNT
) {
844 dev_dbg(dev
, "%s: port %d shutdown processing "
845 "not yet completed\n", __func__
, i
);
848 msleep(100 * retry_cnt
);
854 * cxlflash_remove() - PCI entry point to tear down host
855 * @pdev: PCI device associated with the host.
857 * Safe to use as a cleanup in partially allocated/initialized state. Note that
858 * the reset_waitq is flushed as part of the stop/termination of user contexts.
860 static void cxlflash_remove(struct pci_dev
*pdev
)
862 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
863 struct device
*dev
= &pdev
->dev
;
866 if (!pci_is_enabled(pdev
)) {
867 dev_dbg(dev
, "%s: Device is disabled\n", __func__
);
871 /* If a Task Management Function is active, wait for it to complete
872 * before continuing with remove.
874 spin_lock_irqsave(&cfg
->tmf_slock
, lock_flags
);
876 wait_event_interruptible_lock_irq(cfg
->tmf_waitq
,
879 spin_unlock_irqrestore(&cfg
->tmf_slock
, lock_flags
);
881 /* Notify AFU and wait for shutdown processing to complete */
882 notify_shutdown(cfg
, true);
884 cfg
->state
= STATE_FAILTERM
;
885 cxlflash_stop_term_user_contexts(cfg
);
887 switch (cfg
->init_state
) {
888 case INIT_STATE_SCSI
:
889 cxlflash_term_local_luns(cfg
);
890 scsi_remove_host(cfg
->host
);
894 pci_disable_device(pdev
);
895 case INIT_STATE_NONE
:
897 scsi_host_put(cfg
->host
);
901 dev_dbg(dev
, "%s: returning\n", __func__
);
905 * alloc_mem() - allocates the AFU and its command pool
906 * @cfg: Internal structure associated with the host.
908 * A partially allocated state remains on failure.
912 * -ENOMEM on failure to allocate memory
914 static int alloc_mem(struct cxlflash_cfg
*cfg
)
917 struct device
*dev
= &cfg
->dev
->dev
;
919 /* AFU is ~28k, i.e. only one 64k page or up to seven 4k pages */
920 cfg
->afu
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
921 get_order(sizeof(struct afu
)));
922 if (unlikely(!cfg
->afu
)) {
923 dev_err(dev
, "%s: cannot get %d free pages\n",
924 __func__
, get_order(sizeof(struct afu
)));
928 cfg
->afu
->parent
= cfg
;
929 cfg
->afu
->desired_hwqs
= CXLFLASH_DEF_HWQS
;
930 cfg
->afu
->afu_map
= NULL
;
936 * init_pci() - initializes the host as a PCI device
937 * @cfg: Internal structure associated with the host.
939 * Return: 0 on success, -errno on failure
941 static int init_pci(struct cxlflash_cfg
*cfg
)
943 struct pci_dev
*pdev
= cfg
->dev
;
944 struct device
*dev
= &cfg
->dev
->dev
;
947 rc
= pci_enable_device(pdev
);
948 if (rc
|| pci_channel_offline(pdev
)) {
949 if (pci_channel_offline(pdev
)) {
950 cxlflash_wait_for_pci_err_recovery(cfg
);
951 rc
= pci_enable_device(pdev
);
955 dev_err(dev
, "%s: Cannot enable adapter\n", __func__
);
956 cxlflash_wait_for_pci_err_recovery(cfg
);
962 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
967 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
968 * @cfg: Internal structure associated with the host.
970 * Return: 0 on success, -errno on failure
972 static int init_scsi(struct cxlflash_cfg
*cfg
)
974 struct pci_dev
*pdev
= cfg
->dev
;
975 struct device
*dev
= &cfg
->dev
->dev
;
978 rc
= scsi_add_host(cfg
->host
, &pdev
->dev
);
980 dev_err(dev
, "%s: scsi_add_host failed rc=%d\n", __func__
, rc
);
984 scsi_scan_host(cfg
->host
);
987 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
992 * set_port_online() - transitions the specified host FC port to online state
993 * @fc_regs: Top of MMIO region defined for specified port.
995 * The provided MMIO region must be mapped prior to call. Online state means
996 * that the FC link layer has synced, completed the handshaking process, and
997 * is ready for login to start.
999 static void set_port_online(__be64 __iomem
*fc_regs
)
1003 cmdcfg
= readq_be(&fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1004 cmdcfg
&= (~FC_MTIP_CMDCONFIG_OFFLINE
); /* clear OFF_LINE */
1005 cmdcfg
|= (FC_MTIP_CMDCONFIG_ONLINE
); /* set ON_LINE */
1006 writeq_be(cmdcfg
, &fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1010 * set_port_offline() - transitions the specified host FC port to offline state
1011 * @fc_regs: Top of MMIO region defined for specified port.
1013 * The provided MMIO region must be mapped prior to call.
1015 static void set_port_offline(__be64 __iomem
*fc_regs
)
1019 cmdcfg
= readq_be(&fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1020 cmdcfg
&= (~FC_MTIP_CMDCONFIG_ONLINE
); /* clear ON_LINE */
1021 cmdcfg
|= (FC_MTIP_CMDCONFIG_OFFLINE
); /* set OFF_LINE */
1022 writeq_be(cmdcfg
, &fc_regs
[FC_MTIP_CMDCONFIG
/ 8]);
1026 * wait_port_online() - waits for the specified host FC port come online
1027 * @fc_regs: Top of MMIO region defined for specified port.
1028 * @delay_us: Number of microseconds to delay between reading port status.
1029 * @nretry: Number of cycles to retry reading port status.
1031 * The provided MMIO region must be mapped prior to call. This will timeout
1032 * when the cable is not plugged in.
1035 * TRUE (1) when the specified port is online
1036 * FALSE (0) when the specified port fails to come online after timeout
1038 static bool wait_port_online(__be64 __iomem
*fc_regs
, u32 delay_us
, u32 nretry
)
1042 WARN_ON(delay_us
< 1000);
1045 msleep(delay_us
/ 1000);
1046 status
= readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]);
1047 if (status
== U64_MAX
)
1049 } while ((status
& FC_MTIP_STATUS_MASK
) != FC_MTIP_STATUS_ONLINE
&&
1052 return ((status
& FC_MTIP_STATUS_MASK
) == FC_MTIP_STATUS_ONLINE
);
1056 * wait_port_offline() - waits for the specified host FC port go offline
1057 * @fc_regs: Top of MMIO region defined for specified port.
1058 * @delay_us: Number of microseconds to delay between reading port status.
1059 * @nretry: Number of cycles to retry reading port status.
1061 * The provided MMIO region must be mapped prior to call.
1064 * TRUE (1) when the specified port is offline
1065 * FALSE (0) when the specified port fails to go offline after timeout
1067 static bool wait_port_offline(__be64 __iomem
*fc_regs
, u32 delay_us
, u32 nretry
)
1071 WARN_ON(delay_us
< 1000);
1074 msleep(delay_us
/ 1000);
1075 status
= readq_be(&fc_regs
[FC_MTIP_STATUS
/ 8]);
1076 if (status
== U64_MAX
)
1078 } while ((status
& FC_MTIP_STATUS_MASK
) != FC_MTIP_STATUS_OFFLINE
&&
1081 return ((status
& FC_MTIP_STATUS_MASK
) == FC_MTIP_STATUS_OFFLINE
);
1085 * afu_set_wwpn() - configures the WWPN for the specified host FC port
1086 * @afu: AFU associated with the host that owns the specified FC port.
1087 * @port: Port number being configured.
1088 * @fc_regs: Top of MMIO region defined for specified port.
1089 * @wwpn: The world-wide-port-number previously discovered for port.
1091 * The provided MMIO region must be mapped prior to call. As part of the
1092 * sequence to configure the WWPN, the port is toggled offline and then back
1093 * online. This toggling action can cause this routine to delay up to a few
1094 * seconds. When configured to use the internal LUN feature of the AFU, a
1095 * failure to come online is overridden.
1097 static void afu_set_wwpn(struct afu
*afu
, int port
, __be64 __iomem
*fc_regs
,
1100 struct cxlflash_cfg
*cfg
= afu
->parent
;
1101 struct device
*dev
= &cfg
->dev
->dev
;
1103 set_port_offline(fc_regs
);
1104 if (!wait_port_offline(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1105 FC_PORT_STATUS_RETRY_CNT
)) {
1106 dev_dbg(dev
, "%s: wait on port %d to go offline timed out\n",
1110 writeq_be(wwpn
, &fc_regs
[FC_PNAME
/ 8]);
1112 set_port_online(fc_regs
);
1113 if (!wait_port_online(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1114 FC_PORT_STATUS_RETRY_CNT
)) {
1115 dev_dbg(dev
, "%s: wait on port %d to go online timed out\n",
1121 * afu_link_reset() - resets the specified host FC port
1122 * @afu: AFU associated with the host that owns the specified FC port.
1123 * @port: Port number being configured.
1124 * @fc_regs: Top of MMIO region defined for specified port.
1126 * The provided MMIO region must be mapped prior to call. The sequence to
1127 * reset the port involves toggling it offline and then back online. This
1128 * action can cause this routine to delay up to a few seconds. An effort
1129 * is made to maintain link with the device by switching to host to use
1130 * the alternate port exclusively while the reset takes place.
1131 * failure to come online is overridden.
1133 static void afu_link_reset(struct afu
*afu
, int port
, __be64 __iomem
*fc_regs
)
1135 struct cxlflash_cfg
*cfg
= afu
->parent
;
1136 struct device
*dev
= &cfg
->dev
->dev
;
1139 /* first switch the AFU to the other links, if any */
1140 port_sel
= readq_be(&afu
->afu_map
->global
.regs
.afu_port_sel
);
1141 port_sel
&= ~(1ULL << port
);
1142 writeq_be(port_sel
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1143 cxlflash_afu_sync(afu
, 0, 0, AFU_GSYNC
);
1145 set_port_offline(fc_regs
);
1146 if (!wait_port_offline(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1147 FC_PORT_STATUS_RETRY_CNT
))
1148 dev_err(dev
, "%s: wait on port %d to go offline timed out\n",
1151 set_port_online(fc_regs
);
1152 if (!wait_port_online(fc_regs
, FC_PORT_STATUS_RETRY_INTERVAL_US
,
1153 FC_PORT_STATUS_RETRY_CNT
))
1154 dev_err(dev
, "%s: wait on port %d to go online timed out\n",
1157 /* switch back to include this port */
1158 port_sel
|= (1ULL << port
);
1159 writeq_be(port_sel
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1160 cxlflash_afu_sync(afu
, 0, 0, AFU_GSYNC
);
1162 dev_dbg(dev
, "%s: returning port_sel=%016llx\n", __func__
, port_sel
);
1166 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1167 * @afu: AFU associated with the host.
1169 static void afu_err_intr_init(struct afu
*afu
)
1171 struct cxlflash_cfg
*cfg
= afu
->parent
;
1172 __be64 __iomem
*fc_port_regs
;
1174 struct hwq
*hwq
= get_hwq(afu
, PRIMARY_HWQ
);
1177 /* global async interrupts: AFU clears afu_ctrl on context exit
1178 * if async interrupts were sent to that context. This prevents
1179 * the AFU form sending further async interrupts when
1181 * nobody to receive them.
1185 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_mask
);
1186 /* set LISN# to send and point to primary master context */
1187 reg
= ((u64
) (((hwq
->ctx_hndl
<< 8) | SISL_MSI_ASYNC_ERROR
)) << 40);
1189 if (afu
->internal_lun
)
1190 reg
|= 1; /* Bit 63 indicates local lun */
1191 writeq_be(reg
, &afu
->afu_map
->global
.regs
.afu_ctrl
);
1193 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_clear
);
1194 /* unmask bits that are of interest */
1195 /* note: afu can send an interrupt after this step */
1196 writeq_be(SISL_ASTATUS_MASK
, &afu
->afu_map
->global
.regs
.aintr_mask
);
1197 /* clear again in case a bit came on after previous clear but before */
1199 writeq_be(-1ULL, &afu
->afu_map
->global
.regs
.aintr_clear
);
1201 /* Clear/Set internal lun bits */
1202 fc_port_regs
= get_fc_port_regs(cfg
, 0);
1203 reg
= readq_be(&fc_port_regs
[FC_CONFIG2
/ 8]);
1204 reg
&= SISL_FC_INTERNAL_MASK
;
1205 if (afu
->internal_lun
)
1206 reg
|= ((u64
)(afu
->internal_lun
- 1) << SISL_FC_INTERNAL_SHIFT
);
1207 writeq_be(reg
, &fc_port_regs
[FC_CONFIG2
/ 8]);
1209 /* now clear FC errors */
1210 for (i
= 0; i
< cfg
->num_fc_ports
; i
++) {
1211 fc_port_regs
= get_fc_port_regs(cfg
, i
);
1213 writeq_be(0xFFFFFFFFU
, &fc_port_regs
[FC_ERROR
/ 8]);
1214 writeq_be(0, &fc_port_regs
[FC_ERRCAP
/ 8]);
1217 /* sync interrupts for master's IOARRIN write */
1218 /* note that unlike asyncs, there can be no pending sync interrupts */
1219 /* at this time (this is a fresh context and master has not written */
1220 /* IOARRIN yet), so there is nothing to clear. */
1222 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1223 for (i
= 0; i
< afu
->num_hwqs
; i
++) {
1224 hwq
= get_hwq(afu
, i
);
1226 writeq_be(SISL_MSI_SYNC_ERROR
, &hwq
->host_map
->ctx_ctrl
);
1227 writeq_be(SISL_ISTATUS_MASK
, &hwq
->host_map
->intr_mask
);
1232 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1233 * @irq: Interrupt number.
1234 * @data: Private data provided at interrupt registration, the AFU.
1236 * Return: Always return IRQ_HANDLED.
1238 static irqreturn_t
cxlflash_sync_err_irq(int irq
, void *data
)
1240 struct hwq
*hwq
= (struct hwq
*)data
;
1241 struct cxlflash_cfg
*cfg
= hwq
->afu
->parent
;
1242 struct device
*dev
= &cfg
->dev
->dev
;
1246 reg
= readq_be(&hwq
->host_map
->intr_status
);
1247 reg_unmasked
= (reg
& SISL_ISTATUS_UNMASK
);
1249 if (reg_unmasked
== 0UL) {
1250 dev_err(dev
, "%s: spurious interrupt, intr_status=%016llx\n",
1252 goto cxlflash_sync_err_irq_exit
;
1255 dev_err(dev
, "%s: unexpected interrupt, intr_status=%016llx\n",
1258 writeq_be(reg_unmasked
, &hwq
->host_map
->intr_clear
);
1260 cxlflash_sync_err_irq_exit
:
1265 * process_hrrq() - process the read-response queue
1266 * @afu: AFU associated with the host.
1267 * @doneq: Queue of commands harvested from the RRQ.
1268 * @budget: Threshold of RRQ entries to process.
1270 * This routine must be called holding the disabled RRQ spin lock.
1272 * Return: The number of entries processed.
1274 static int process_hrrq(struct hwq
*hwq
, struct list_head
*doneq
, int budget
)
1276 struct afu
*afu
= hwq
->afu
;
1277 struct afu_cmd
*cmd
;
1278 struct sisl_ioasa
*ioasa
;
1279 struct sisl_ioarcb
*ioarcb
;
1280 bool toggle
= hwq
->toggle
;
1283 *hrrq_start
= hwq
->hrrq_start
,
1284 *hrrq_end
= hwq
->hrrq_end
,
1285 *hrrq_curr
= hwq
->hrrq_curr
;
1287 /* Process ready RRQ entries up to the specified budget (if any) */
1291 if ((entry
& SISL_RESP_HANDLE_T_BIT
) != toggle
)
1294 entry
&= ~SISL_RESP_HANDLE_T_BIT
;
1296 if (afu_is_sq_cmd_mode(afu
)) {
1297 ioasa
= (struct sisl_ioasa
*)entry
;
1298 cmd
= container_of(ioasa
, struct afu_cmd
, sa
);
1300 ioarcb
= (struct sisl_ioarcb
*)entry
;
1301 cmd
= container_of(ioarcb
, struct afu_cmd
, rcb
);
1304 list_add_tail(&cmd
->queue
, doneq
);
1306 /* Advance to next entry or wrap and flip the toggle bit */
1307 if (hrrq_curr
< hrrq_end
)
1310 hrrq_curr
= hrrq_start
;
1311 toggle
^= SISL_RESP_HANDLE_T_BIT
;
1314 atomic_inc(&hwq
->hsq_credits
);
1317 if (budget
> 0 && num_hrrq
>= budget
)
1321 hwq
->hrrq_curr
= hrrq_curr
;
1322 hwq
->toggle
= toggle
;
1328 * process_cmd_doneq() - process a queue of harvested RRQ commands
1329 * @doneq: Queue of completed commands.
1331 * Note that upon return the queue can no longer be trusted.
1333 static void process_cmd_doneq(struct list_head
*doneq
)
1335 struct afu_cmd
*cmd
, *tmp
;
1337 WARN_ON(list_empty(doneq
));
1339 list_for_each_entry_safe(cmd
, tmp
, doneq
, queue
)
1344 * cxlflash_irqpoll() - process a queue of harvested RRQ commands
1345 * @irqpoll: IRQ poll structure associated with queue to poll.
1346 * @budget: Threshold of RRQ entries to process per poll.
1348 * Return: The number of entries processed.
1350 static int cxlflash_irqpoll(struct irq_poll
*irqpoll
, int budget
)
1352 struct hwq
*hwq
= container_of(irqpoll
, struct hwq
, irqpoll
);
1353 unsigned long hrrq_flags
;
1355 int num_entries
= 0;
1357 spin_lock_irqsave(&hwq
->hrrq_slock
, hrrq_flags
);
1359 num_entries
= process_hrrq(hwq
, &doneq
, budget
);
1360 if (num_entries
< budget
)
1361 irq_poll_complete(irqpoll
);
1363 spin_unlock_irqrestore(&hwq
->hrrq_slock
, hrrq_flags
);
1365 process_cmd_doneq(&doneq
);
1370 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1371 * @irq: Interrupt number.
1372 * @data: Private data provided at interrupt registration, the AFU.
1374 * Return: IRQ_HANDLED or IRQ_NONE when no ready entries found.
1376 static irqreturn_t
cxlflash_rrq_irq(int irq
, void *data
)
1378 struct hwq
*hwq
= (struct hwq
*)data
;
1379 struct afu
*afu
= hwq
->afu
;
1380 unsigned long hrrq_flags
;
1382 int num_entries
= 0;
1384 spin_lock_irqsave(&hwq
->hrrq_slock
, hrrq_flags
);
1386 if (afu_is_irqpoll_enabled(afu
)) {
1387 irq_poll_sched(&hwq
->irqpoll
);
1388 spin_unlock_irqrestore(&hwq
->hrrq_slock
, hrrq_flags
);
1392 num_entries
= process_hrrq(hwq
, &doneq
, -1);
1393 spin_unlock_irqrestore(&hwq
->hrrq_slock
, hrrq_flags
);
1395 if (num_entries
== 0)
1398 process_cmd_doneq(&doneq
);
1403 * Asynchronous interrupt information table
1406 * - Order matters here as this array is indexed by bit position.
1408 * - The checkpatch script considers the BUILD_SISL_ASTATUS_FC_PORT macro
1409 * as complex and complains due to a lack of parentheses/braces.
1411 #define ASTATUS_FC(_a, _b, _c, _d) \
1412 { SISL_ASTATUS_FC##_a##_##_b, _c, _a, (_d) }
1414 #define BUILD_SISL_ASTATUS_FC_PORT(_a) \
1415 ASTATUS_FC(_a, LINK_UP, "link up", 0), \
1416 ASTATUS_FC(_a, LINK_DN, "link down", 0), \
1417 ASTATUS_FC(_a, LOGI_S, "login succeeded", SCAN_HOST), \
1418 ASTATUS_FC(_a, LOGI_F, "login failed", CLR_FC_ERROR), \
1419 ASTATUS_FC(_a, LOGI_R, "login timed out, retrying", LINK_RESET), \
1420 ASTATUS_FC(_a, CRC_T, "CRC threshold exceeded", LINK_RESET), \
1421 ASTATUS_FC(_a, LOGO, "target initiated LOGO", 0), \
1422 ASTATUS_FC(_a, OTHER, "other error", CLR_FC_ERROR | LINK_RESET)
1424 static const struct asyc_intr_info ainfo
[] = {
1425 BUILD_SISL_ASTATUS_FC_PORT(1),
1426 BUILD_SISL_ASTATUS_FC_PORT(0),
1427 BUILD_SISL_ASTATUS_FC_PORT(3),
1428 BUILD_SISL_ASTATUS_FC_PORT(2)
1432 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1433 * @irq: Interrupt number.
1434 * @data: Private data provided at interrupt registration, the AFU.
1436 * Return: Always return IRQ_HANDLED.
1438 static irqreturn_t
cxlflash_async_err_irq(int irq
, void *data
)
1440 struct hwq
*hwq
= (struct hwq
*)data
;
1441 struct afu
*afu
= hwq
->afu
;
1442 struct cxlflash_cfg
*cfg
= afu
->parent
;
1443 struct device
*dev
= &cfg
->dev
->dev
;
1444 const struct asyc_intr_info
*info
;
1445 struct sisl_global_map __iomem
*global
= &afu
->afu_map
->global
;
1446 __be64 __iomem
*fc_port_regs
;
1452 reg
= readq_be(&global
->regs
.aintr_status
);
1453 reg_unmasked
= (reg
& SISL_ASTATUS_UNMASK
);
1455 if (unlikely(reg_unmasked
== 0)) {
1456 dev_err(dev
, "%s: spurious interrupt, aintr_status=%016llx\n",
1461 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
1462 writeq_be(reg_unmasked
, &global
->regs
.aintr_clear
);
1464 /* Check each bit that is on */
1465 for_each_set_bit(bit
, (ulong
*)®_unmasked
, BITS_PER_LONG
) {
1466 if (unlikely(bit
>= ARRAY_SIZE(ainfo
))) {
1472 if (unlikely(info
->status
!= 1ULL << bit
)) {
1478 fc_port_regs
= get_fc_port_regs(cfg
, port
);
1480 dev_err(dev
, "%s: FC Port %d -> %s, fc_status=%016llx\n",
1481 __func__
, port
, info
->desc
,
1482 readq_be(&fc_port_regs
[FC_STATUS
/ 8]));
1485 * Do link reset first, some OTHER errors will set FC_ERROR
1486 * again if cleared before or w/o a reset
1488 if (info
->action
& LINK_RESET
) {
1489 dev_err(dev
, "%s: FC Port %d: resetting link\n",
1491 cfg
->lr_state
= LINK_RESET_REQUIRED
;
1492 cfg
->lr_port
= port
;
1493 schedule_work(&cfg
->work_q
);
1496 if (info
->action
& CLR_FC_ERROR
) {
1497 reg
= readq_be(&fc_port_regs
[FC_ERROR
/ 8]);
1500 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
1501 * should be the same and tracing one is sufficient.
1504 dev_err(dev
, "%s: fc %d: clearing fc_error=%016llx\n",
1505 __func__
, port
, reg
);
1507 writeq_be(reg
, &fc_port_regs
[FC_ERROR
/ 8]);
1508 writeq_be(0, &fc_port_regs
[FC_ERRCAP
/ 8]);
1511 if (info
->action
& SCAN_HOST
) {
1512 atomic_inc(&cfg
->scan_host_needed
);
1513 schedule_work(&cfg
->work_q
);
1522 * start_context() - starts the master context
1523 * @cfg: Internal structure associated with the host.
1524 * @index: Index of the hardware queue.
1526 * Return: A success or failure value from CXL services.
1528 static int start_context(struct cxlflash_cfg
*cfg
, u32 index
)
1530 struct device
*dev
= &cfg
->dev
->dev
;
1531 struct hwq
*hwq
= get_hwq(cfg
->afu
, index
);
1534 rc
= cxl_start_context(hwq
->ctx
,
1535 hwq
->work
.work_element_descriptor
,
1538 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1543 * read_vpd() - obtains the WWPNs from VPD
1544 * @cfg: Internal structure associated with the host.
1545 * @wwpn: Array of size MAX_FC_PORTS to pass back WWPNs
1547 * Return: 0 on success, -errno on failure
1549 static int read_vpd(struct cxlflash_cfg
*cfg
, u64 wwpn
[])
1551 struct device
*dev
= &cfg
->dev
->dev
;
1552 struct pci_dev
*pdev
= cfg
->dev
;
1554 int ro_start
, ro_size
, i
, j
, k
;
1556 char vpd_data
[CXLFLASH_VPD_LEN
];
1557 char tmp_buf
[WWPN_BUF_LEN
] = { 0 };
1558 char *wwpn_vpd_tags
[MAX_FC_PORTS
] = { "V5", "V6", "V7", "V8" };
1560 /* Get the VPD data from the device */
1561 vpd_size
= cxl_read_adapter_vpd(pdev
, vpd_data
, sizeof(vpd_data
));
1562 if (unlikely(vpd_size
<= 0)) {
1563 dev_err(dev
, "%s: Unable to read VPD (size = %ld)\n",
1564 __func__
, vpd_size
);
1569 /* Get the read only section offset */
1570 ro_start
= pci_vpd_find_tag(vpd_data
, 0, vpd_size
,
1571 PCI_VPD_LRDT_RO_DATA
);
1572 if (unlikely(ro_start
< 0)) {
1573 dev_err(dev
, "%s: VPD Read-only data not found\n", __func__
);
1578 /* Get the read only section size, cap when extends beyond read VPD */
1579 ro_size
= pci_vpd_lrdt_size(&vpd_data
[ro_start
]);
1581 i
= ro_start
+ PCI_VPD_LRDT_TAG_SIZE
;
1582 if (unlikely((i
+ j
) > vpd_size
)) {
1583 dev_dbg(dev
, "%s: Might need to read more VPD (%d > %ld)\n",
1584 __func__
, (i
+ j
), vpd_size
);
1585 ro_size
= vpd_size
- i
;
1589 * Find the offset of the WWPN tag within the read only
1590 * VPD data and validate the found field (partials are
1591 * no good to us). Convert the ASCII data to an integer
1592 * value. Note that we must copy to a temporary buffer
1593 * because the conversion service requires that the ASCII
1594 * string be terminated.
1596 for (k
= 0; k
< cfg
->num_fc_ports
; k
++) {
1598 i
= ro_start
+ PCI_VPD_LRDT_TAG_SIZE
;
1600 i
= pci_vpd_find_info_keyword(vpd_data
, i
, j
, wwpn_vpd_tags
[k
]);
1601 if (unlikely(i
< 0)) {
1602 dev_err(dev
, "%s: Port %d WWPN not found in VPD\n",
1608 j
= pci_vpd_info_field_size(&vpd_data
[i
]);
1609 i
+= PCI_VPD_INFO_FLD_HDR_SIZE
;
1610 if (unlikely((i
+ j
> vpd_size
) || (j
!= WWPN_LEN
))) {
1611 dev_err(dev
, "%s: Port %d WWPN incomplete or bad VPD\n",
1617 memcpy(tmp_buf
, &vpd_data
[i
], WWPN_LEN
);
1618 rc
= kstrtoul(tmp_buf
, WWPN_LEN
, (ulong
*)&wwpn
[k
]);
1620 dev_err(dev
, "%s: WWPN conversion failed for port %d\n",
1626 dev_dbg(dev
, "%s: wwpn%d=%016llx\n", __func__
, k
, wwpn
[k
]);
1630 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1635 * init_pcr() - initialize the provisioning and control registers
1636 * @cfg: Internal structure associated with the host.
1638 * Also sets up fast access to the mapped registers and initializes AFU
1639 * command fields that never change.
1641 static void init_pcr(struct cxlflash_cfg
*cfg
)
1643 struct afu
*afu
= cfg
->afu
;
1644 struct sisl_ctrl_map __iomem
*ctrl_map
;
1648 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
1649 ctrl_map
= &afu
->afu_map
->ctrls
[i
].ctrl
;
1650 /* Disrupt any clients that could be running */
1651 /* e.g. clients that survived a master restart */
1652 writeq_be(0, &ctrl_map
->rht_start
);
1653 writeq_be(0, &ctrl_map
->rht_cnt_id
);
1654 writeq_be(0, &ctrl_map
->ctx_cap
);
1657 /* Copy frequently used fields into hwq */
1658 for (i
= 0; i
< afu
->num_hwqs
; i
++) {
1659 hwq
= get_hwq(afu
, i
);
1661 hwq
->ctx_hndl
= (u16
) cxl_process_element(hwq
->ctx
);
1662 hwq
->host_map
= &afu
->afu_map
->hosts
[hwq
->ctx_hndl
].host
;
1663 hwq
->ctrl_map
= &afu
->afu_map
->ctrls
[hwq
->ctx_hndl
].ctrl
;
1665 /* Program the Endian Control for the master context */
1666 writeq_be(SISL_ENDIAN_CTRL
, &hwq
->host_map
->endian_ctrl
);
1671 * init_global() - initialize AFU global registers
1672 * @cfg: Internal structure associated with the host.
1674 static int init_global(struct cxlflash_cfg
*cfg
)
1676 struct afu
*afu
= cfg
->afu
;
1677 struct device
*dev
= &cfg
->dev
->dev
;
1679 struct sisl_host_map __iomem
*hmap
;
1680 __be64 __iomem
*fc_port_regs
;
1681 u64 wwpn
[MAX_FC_PORTS
]; /* wwpn of AFU ports */
1682 int i
= 0, num_ports
= 0;
1686 rc
= read_vpd(cfg
, &wwpn
[0]);
1688 dev_err(dev
, "%s: could not read vpd rc=%d\n", __func__
, rc
);
1692 /* Set up RRQ and SQ in HWQ for master issued cmds */
1693 for (i
= 0; i
< afu
->num_hwqs
; i
++) {
1694 hwq
= get_hwq(afu
, i
);
1695 hmap
= hwq
->host_map
;
1697 writeq_be((u64
) hwq
->hrrq_start
, &hmap
->rrq_start
);
1698 writeq_be((u64
) hwq
->hrrq_end
, &hmap
->rrq_end
);
1700 if (afu_is_sq_cmd_mode(afu
)) {
1701 writeq_be((u64
)hwq
->hsq_start
, &hmap
->sq_start
);
1702 writeq_be((u64
)hwq
->hsq_end
, &hmap
->sq_end
);
1706 /* AFU configuration */
1707 reg
= readq_be(&afu
->afu_map
->global
.regs
.afu_config
);
1708 reg
|= SISL_AFUCONF_AR_ALL
|SISL_AFUCONF_ENDIAN
;
1709 /* enable all auto retry options and control endianness */
1710 /* leave others at default: */
1711 /* CTX_CAP write protected, mbox_r does not clear on read and */
1712 /* checker on if dual afu */
1713 writeq_be(reg
, &afu
->afu_map
->global
.regs
.afu_config
);
1715 /* Global port select: select either port */
1716 if (afu
->internal_lun
) {
1717 /* Only use port 0 */
1718 writeq_be(PORT0
, &afu
->afu_map
->global
.regs
.afu_port_sel
);
1721 writeq_be(PORT_MASK(cfg
->num_fc_ports
),
1722 &afu
->afu_map
->global
.regs
.afu_port_sel
);
1723 num_ports
= cfg
->num_fc_ports
;
1726 for (i
= 0; i
< num_ports
; i
++) {
1727 fc_port_regs
= get_fc_port_regs(cfg
, i
);
1729 /* Unmask all errors (but they are still masked at AFU) */
1730 writeq_be(0, &fc_port_regs
[FC_ERRMSK
/ 8]);
1731 /* Clear CRC error cnt & set a threshold */
1732 (void)readq_be(&fc_port_regs
[FC_CNT_CRCERR
/ 8]);
1733 writeq_be(MC_CRC_THRESH
, &fc_port_regs
[FC_CRC_THRESH
/ 8]);
1735 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
1737 afu_set_wwpn(afu
, i
, &fc_port_regs
[0], wwpn
[i
]);
1738 /* Programming WWPN back to back causes additional
1739 * offline/online transitions and a PLOGI
1744 /* Set up master's own CTX_CAP to allow real mode, host translation */
1745 /* tables, afu cmds and read/write GSCSI cmds. */
1746 /* First, unlock ctx_cap write by reading mbox */
1747 for (i
= 0; i
< afu
->num_hwqs
; i
++) {
1748 hwq
= get_hwq(afu
, i
);
1750 (void)readq_be(&hwq
->ctrl_map
->mbox_r
); /* unlock ctx_cap */
1751 writeq_be((SISL_CTX_CAP_REAL_MODE
| SISL_CTX_CAP_HOST_XLATE
|
1752 SISL_CTX_CAP_READ_CMD
| SISL_CTX_CAP_WRITE_CMD
|
1753 SISL_CTX_CAP_AFU_CMD
| SISL_CTX_CAP_GSCSI_CMD
),
1754 &hwq
->ctrl_map
->ctx_cap
);
1756 /* Initialize heartbeat */
1757 afu
->hb
= readq_be(&afu
->afu_map
->global
.regs
.afu_hb
);
1763 * start_afu() - initializes and starts the AFU
1764 * @cfg: Internal structure associated with the host.
1766 static int start_afu(struct cxlflash_cfg
*cfg
)
1768 struct afu
*afu
= cfg
->afu
;
1769 struct device
*dev
= &cfg
->dev
->dev
;
1776 /* Initialize each HWQ */
1777 for (i
= 0; i
< afu
->num_hwqs
; i
++) {
1778 hwq
= get_hwq(afu
, i
);
1780 /* After an AFU reset, RRQ entries are stale, clear them */
1781 memset(&hwq
->rrq_entry
, 0, sizeof(hwq
->rrq_entry
));
1783 /* Initialize RRQ pointers */
1784 hwq
->hrrq_start
= &hwq
->rrq_entry
[0];
1785 hwq
->hrrq_end
= &hwq
->rrq_entry
[NUM_RRQ_ENTRY
- 1];
1786 hwq
->hrrq_curr
= hwq
->hrrq_start
;
1789 /* Initialize spin locks */
1790 spin_lock_init(&hwq
->hrrq_slock
);
1791 spin_lock_init(&hwq
->hsq_slock
);
1794 if (afu_is_sq_cmd_mode(afu
)) {
1795 memset(&hwq
->sq
, 0, sizeof(hwq
->sq
));
1796 hwq
->hsq_start
= &hwq
->sq
[0];
1797 hwq
->hsq_end
= &hwq
->sq
[NUM_SQ_ENTRY
- 1];
1798 hwq
->hsq_curr
= hwq
->hsq_start
;
1800 atomic_set(&hwq
->hsq_credits
, NUM_SQ_ENTRY
- 1);
1803 /* Initialize IRQ poll */
1804 if (afu_is_irqpoll_enabled(afu
))
1805 irq_poll_init(&hwq
->irqpoll
, afu
->irqpoll_weight
,
1810 rc
= init_global(cfg
);
1812 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1817 * init_intr() - setup interrupt handlers for the master context
1818 * @cfg: Internal structure associated with the host.
1819 * @hwq: Hardware queue to initialize.
1821 * Return: 0 on success, -errno on failure
1823 static enum undo_level
init_intr(struct cxlflash_cfg
*cfg
,
1826 struct device
*dev
= &cfg
->dev
->dev
;
1827 struct cxl_context
*ctx
= hwq
->ctx
;
1829 enum undo_level level
= UNDO_NOOP
;
1830 bool is_primary_hwq
= (hwq
->index
== PRIMARY_HWQ
);
1831 int num_irqs
= is_primary_hwq
? 3 : 2;
1833 rc
= cxl_allocate_afu_irqs(ctx
, num_irqs
);
1835 dev_err(dev
, "%s: allocate_afu_irqs failed rc=%d\n",
1841 rc
= cxl_map_afu_irq(ctx
, 1, cxlflash_sync_err_irq
, hwq
,
1842 "SISL_MSI_SYNC_ERROR");
1843 if (unlikely(rc
<= 0)) {
1844 dev_err(dev
, "%s: SISL_MSI_SYNC_ERROR map failed\n", __func__
);
1849 rc
= cxl_map_afu_irq(ctx
, 2, cxlflash_rrq_irq
, hwq
,
1850 "SISL_MSI_RRQ_UPDATED");
1851 if (unlikely(rc
<= 0)) {
1852 dev_err(dev
, "%s: SISL_MSI_RRQ_UPDATED map failed\n", __func__
);
1857 /* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
1858 if (!is_primary_hwq
)
1861 rc
= cxl_map_afu_irq(ctx
, 3, cxlflash_async_err_irq
, hwq
,
1862 "SISL_MSI_ASYNC_ERROR");
1863 if (unlikely(rc
<= 0)) {
1864 dev_err(dev
, "%s: SISL_MSI_ASYNC_ERROR map failed\n", __func__
);
1873 * init_mc() - create and register as the master context
1874 * @cfg: Internal structure associated with the host.
1875 * index: HWQ Index of the master context.
1877 * Return: 0 on success, -errno on failure
1879 static int init_mc(struct cxlflash_cfg
*cfg
, u32 index
)
1881 struct cxl_context
*ctx
;
1882 struct device
*dev
= &cfg
->dev
->dev
;
1883 struct hwq
*hwq
= get_hwq(cfg
->afu
, index
);
1885 enum undo_level level
;
1887 hwq
->afu
= cfg
->afu
;
1889 INIT_LIST_HEAD(&hwq
->pending_cmds
);
1891 if (index
== PRIMARY_HWQ
)
1892 ctx
= cxl_get_context(cfg
->dev
);
1894 ctx
= cxl_dev_context_init(cfg
->dev
);
1895 if (unlikely(!ctx
)) {
1903 /* Set it up as a master with the CXL */
1904 cxl_set_master(ctx
);
1906 /* Reset AFU when initializing primary context */
1907 if (index
== PRIMARY_HWQ
) {
1908 rc
= cxl_afu_reset(ctx
);
1910 dev_err(dev
, "%s: AFU reset failed rc=%d\n",
1916 level
= init_intr(cfg
, hwq
);
1917 if (unlikely(level
)) {
1918 dev_err(dev
, "%s: interrupt init failed rc=%d\n", __func__
, rc
);
1922 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1923 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1924 * element (pe) that is embedded in the context (ctx)
1926 rc
= start_context(cfg
, index
);
1928 dev_err(dev
, "%s: start context failed rc=%d\n", __func__
, rc
);
1929 level
= UNMAP_THREE
;
1934 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1937 term_intr(cfg
, level
, index
);
1938 if (index
!= PRIMARY_HWQ
)
1939 cxl_release_context(ctx
);
1946 * get_num_afu_ports() - determines and configures the number of AFU ports
1947 * @cfg: Internal structure associated with the host.
1949 * This routine determines the number of AFU ports by converting the global
1950 * port selection mask. The converted value is only valid following an AFU
1951 * reset (explicit or power-on). This routine must be invoked shortly after
1952 * mapping as other routines are dependent on the number of ports during the
1953 * initialization sequence.
1955 * To support legacy AFUs that might not have reflected an initial global
1956 * port mask (value read is 0), default to the number of ports originally
1957 * supported by the cxlflash driver (2) before hardware with other port
1958 * offerings was introduced.
1960 static void get_num_afu_ports(struct cxlflash_cfg
*cfg
)
1962 struct afu
*afu
= cfg
->afu
;
1963 struct device
*dev
= &cfg
->dev
->dev
;
1965 int num_fc_ports
= LEGACY_FC_PORTS
;
1967 port_mask
= readq_be(&afu
->afu_map
->global
.regs
.afu_port_sel
);
1968 if (port_mask
!= 0ULL)
1969 num_fc_ports
= min(ilog2(port_mask
) + 1, MAX_FC_PORTS
);
1971 dev_dbg(dev
, "%s: port_mask=%016llx num_fc_ports=%d\n",
1972 __func__
, port_mask
, num_fc_ports
);
1974 cfg
->num_fc_ports
= num_fc_ports
;
1975 cfg
->host
->max_channel
= PORTNUM2CHAN(num_fc_ports
);
1979 * init_afu() - setup as master context and start AFU
1980 * @cfg: Internal structure associated with the host.
1982 * This routine is a higher level of control for configuring the
1983 * AFU on probe and reset paths.
1985 * Return: 0 on success, -errno on failure
1987 static int init_afu(struct cxlflash_cfg
*cfg
)
1991 struct afu
*afu
= cfg
->afu
;
1992 struct device
*dev
= &cfg
->dev
->dev
;
1996 cxl_perst_reloads_same_image(cfg
->cxl_afu
, true);
1998 afu
->num_hwqs
= afu
->desired_hwqs
;
1999 for (i
= 0; i
< afu
->num_hwqs
; i
++) {
2000 rc
= init_mc(cfg
, i
);
2002 dev_err(dev
, "%s: init_mc failed rc=%d index=%d\n",
2008 /* Map the entire MMIO space of the AFU using the first context */
2009 hwq
= get_hwq(afu
, PRIMARY_HWQ
);
2010 afu
->afu_map
= cxl_psa_map(hwq
->ctx
);
2011 if (!afu
->afu_map
) {
2012 dev_err(dev
, "%s: cxl_psa_map failed\n", __func__
);
2017 /* No byte reverse on reading afu_version or string will be backwards */
2018 reg
= readq(&afu
->afu_map
->global
.regs
.afu_version
);
2019 memcpy(afu
->version
, ®
, sizeof(reg
));
2020 afu
->interface_version
=
2021 readq_be(&afu
->afu_map
->global
.regs
.interface_version
);
2022 if ((afu
->interface_version
+ 1) == 0) {
2023 dev_err(dev
, "Back level AFU, please upgrade. AFU version %s "
2024 "interface version %016llx\n", afu
->version
,
2025 afu
->interface_version
);
2030 if (afu_is_sq_cmd_mode(afu
)) {
2031 afu
->send_cmd
= send_cmd_sq
;
2032 afu
->context_reset
= context_reset_sq
;
2034 afu
->send_cmd
= send_cmd_ioarrin
;
2035 afu
->context_reset
= context_reset_ioarrin
;
2038 dev_dbg(dev
, "%s: afu_ver=%s interface_ver=%016llx\n", __func__
,
2039 afu
->version
, afu
->interface_version
);
2041 get_num_afu_ports(cfg
);
2043 rc
= start_afu(cfg
);
2045 dev_err(dev
, "%s: start_afu failed, rc=%d\n", __func__
, rc
);
2049 afu_err_intr_init(cfg
->afu
);
2050 for (i
= 0; i
< afu
->num_hwqs
; i
++) {
2051 hwq
= get_hwq(afu
, i
);
2053 hwq
->room
= readq_be(&hwq
->host_map
->cmd_room
);
2056 /* Restore the LUN mappings */
2057 cxlflash_restore_luntable(cfg
);
2059 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
2063 for (i
= afu
->num_hwqs
- 1; i
>= 0; i
--) {
2064 term_intr(cfg
, UNMAP_THREE
, i
);
2071 * afu_reset() - resets the AFU
2072 * @cfg: Internal structure associated with the host.
2074 * Return: 0 on success, -errno on failure
2076 static int afu_reset(struct cxlflash_cfg
*cfg
)
2078 struct device
*dev
= &cfg
->dev
->dev
;
2081 /* Stop the context before the reset. Since the context is
2082 * no longer available restart it after the reset is complete
2088 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
2093 * drain_ioctls() - wait until all currently executing ioctls have completed
2094 * @cfg: Internal structure associated with the host.
2096 * Obtain write access to read/write semaphore that wraps ioctl
2097 * handling to 'drain' ioctls currently executing.
2099 static void drain_ioctls(struct cxlflash_cfg
*cfg
)
2101 down_write(&cfg
->ioctl_rwsem
);
2102 up_write(&cfg
->ioctl_rwsem
);
2106 * cxlflash_async_reset_host() - asynchronous host reset handler
2107 * @data: Private data provided while scheduling reset.
2108 * @cookie: Cookie that can be used for checkpointing.
2110 static void cxlflash_async_reset_host(void *data
, async_cookie_t cookie
)
2112 struct cxlflash_cfg
*cfg
= data
;
2113 struct device
*dev
= &cfg
->dev
->dev
;
2116 if (cfg
->state
!= STATE_RESET
) {
2117 dev_dbg(dev
, "%s: Not performing a reset, state=%d\n",
2118 __func__
, cfg
->state
);
2123 cxlflash_mark_contexts_error(cfg
);
2124 rc
= afu_reset(cfg
);
2126 cfg
->state
= STATE_FAILTERM
;
2128 cfg
->state
= STATE_NORMAL
;
2129 wake_up_all(&cfg
->reset_waitq
);
2132 scsi_unblock_requests(cfg
->host
);
2136 * cxlflash_schedule_async_reset() - schedule an asynchronous host reset
2137 * @cfg: Internal structure associated with the host.
2139 static void cxlflash_schedule_async_reset(struct cxlflash_cfg
*cfg
)
2141 struct device
*dev
= &cfg
->dev
->dev
;
2143 if (cfg
->state
!= STATE_NORMAL
) {
2144 dev_dbg(dev
, "%s: Not performing reset state=%d\n",
2145 __func__
, cfg
->state
);
2149 cfg
->state
= STATE_RESET
;
2150 scsi_block_requests(cfg
->host
);
2151 cfg
->async_reset_cookie
= async_schedule(cxlflash_async_reset_host
,
2156 * cxlflash_afu_sync() - builds and sends an AFU sync command
2157 * @afu: AFU associated with the host.
2158 * @ctx_hndl_u: Identifies context requesting sync.
2159 * @res_hndl_u: Identifies resource requesting sync.
2160 * @mode: Type of sync to issue (lightweight, heavyweight, global).
2162 * The AFU can only take 1 sync command at a time. This routine enforces this
2163 * limitation by using a mutex to provide exclusive access to the AFU during
2164 * the sync. This design point requires calling threads to not be on interrupt
2165 * context due to the possibility of sleeping during concurrent sync operations.
2167 * AFU sync operations are only necessary and allowed when the device is
2168 * operating normally. When not operating normally, sync requests can occur as
2169 * part of cleaning up resources associated with an adapter prior to removal.
2170 * In this scenario, these requests are simply ignored (safe due to the AFU
2174 * 0 on success, -errno on failure
2176 int cxlflash_afu_sync(struct afu
*afu
, ctx_hndl_t ctx_hndl_u
,
2177 res_hndl_t res_hndl_u
, u8 mode
)
2179 struct cxlflash_cfg
*cfg
= afu
->parent
;
2180 struct device
*dev
= &cfg
->dev
->dev
;
2181 struct afu_cmd
*cmd
= NULL
;
2182 struct hwq
*hwq
= get_hwq(afu
, PRIMARY_HWQ
);
2186 static DEFINE_MUTEX(sync_active
);
2188 if (cfg
->state
!= STATE_NORMAL
) {
2189 dev_dbg(dev
, "%s: Sync not required state=%u\n",
2190 __func__
, cfg
->state
);
2194 mutex_lock(&sync_active
);
2195 atomic_inc(&afu
->cmds_active
);
2196 buf
= kmalloc(sizeof(*cmd
) + __alignof__(*cmd
) - 1, GFP_KERNEL
);
2197 if (unlikely(!buf
)) {
2198 dev_err(dev
, "%s: no memory for command\n", __func__
);
2203 cmd
= (struct afu_cmd
*)PTR_ALIGN(buf
, __alignof__(*cmd
));
2206 memset(cmd
, 0, sizeof(*cmd
));
2207 INIT_LIST_HEAD(&cmd
->queue
);
2208 init_completion(&cmd
->cevent
);
2210 cmd
->hwq_index
= hwq
->index
;
2212 dev_dbg(dev
, "%s: afu=%p cmd=%p ctx=%d nretry=%d\n",
2213 __func__
, afu
, cmd
, ctx_hndl_u
, nretry
);
2215 cmd
->rcb
.req_flags
= SISL_REQ_FLAGS_AFU_CMD
;
2216 cmd
->rcb
.ctx_id
= hwq
->ctx_hndl
;
2217 cmd
->rcb
.msi
= SISL_MSI_RRQ_UPDATED
;
2218 cmd
->rcb
.timeout
= MC_AFU_SYNC_TIMEOUT
;
2220 cmd
->rcb
.cdb
[0] = 0xC0; /* AFU Sync */
2221 cmd
->rcb
.cdb
[1] = mode
;
2223 /* The cdb is aligned, no unaligned accessors required */
2224 *((__be16
*)&cmd
->rcb
.cdb
[2]) = cpu_to_be16(ctx_hndl_u
);
2225 *((__be32
*)&cmd
->rcb
.cdb
[4]) = cpu_to_be32(res_hndl_u
);
2227 rc
= afu
->send_cmd(afu
, cmd
);
2233 rc
= wait_resp(afu
, cmd
);
2236 rc
= afu
->context_reset(hwq
);
2238 cxlflash_schedule_async_reset(cfg
);
2241 /* fall through to retry */
2245 /* fall through to exit */
2251 atomic_dec(&afu
->cmds_active
);
2252 mutex_unlock(&sync_active
);
2254 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
2259 * cxlflash_eh_device_reset_handler() - reset a single LUN
2260 * @scp: SCSI command to send.
2263 * SUCCESS as defined in scsi/scsi.h
2264 * FAILED as defined in scsi/scsi.h
2266 static int cxlflash_eh_device_reset_handler(struct scsi_cmnd
*scp
)
2269 struct Scsi_Host
*host
= scp
->device
->host
;
2270 struct cxlflash_cfg
*cfg
= shost_priv(host
);
2271 struct device
*dev
= &cfg
->dev
->dev
;
2272 struct afu
*afu
= cfg
->afu
;
2275 dev_dbg(dev
, "%s: (scp=%p) %d/%d/%d/%llu "
2276 "cdb=(%08x-%08x-%08x-%08x)\n", __func__
, scp
, host
->host_no
,
2277 scp
->device
->channel
, scp
->device
->id
, scp
->device
->lun
,
2278 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
2279 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
2280 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
2281 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
2284 switch (cfg
->state
) {
2286 rcr
= send_tmf(afu
, scp
, TMF_LUN_RESET
);
2291 wait_event(cfg
->reset_waitq
, cfg
->state
!= STATE_RESET
);
2298 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
2303 * cxlflash_eh_host_reset_handler() - reset the host adapter
2304 * @scp: SCSI command from stack identifying host.
2306 * Following a reset, the state is evaluated again in case an EEH occurred
2307 * during the reset. In such a scenario, the host reset will either yield
2308 * until the EEH recovery is complete or return success or failure based
2309 * upon the current device state.
2312 * SUCCESS as defined in scsi/scsi.h
2313 * FAILED as defined in scsi/scsi.h
2315 static int cxlflash_eh_host_reset_handler(struct scsi_cmnd
*scp
)
2319 struct Scsi_Host
*host
= scp
->device
->host
;
2320 struct cxlflash_cfg
*cfg
= shost_priv(host
);
2321 struct device
*dev
= &cfg
->dev
->dev
;
2323 dev_dbg(dev
, "%s: (scp=%p) %d/%d/%d/%llu "
2324 "cdb=(%08x-%08x-%08x-%08x)\n", __func__
, scp
, host
->host_no
,
2325 scp
->device
->channel
, scp
->device
->id
, scp
->device
->lun
,
2326 get_unaligned_be32(&((u32
*)scp
->cmnd
)[0]),
2327 get_unaligned_be32(&((u32
*)scp
->cmnd
)[1]),
2328 get_unaligned_be32(&((u32
*)scp
->cmnd
)[2]),
2329 get_unaligned_be32(&((u32
*)scp
->cmnd
)[3]));
2331 switch (cfg
->state
) {
2333 cfg
->state
= STATE_RESET
;
2335 cxlflash_mark_contexts_error(cfg
);
2336 rcr
= afu_reset(cfg
);
2339 cfg
->state
= STATE_FAILTERM
;
2341 cfg
->state
= STATE_NORMAL
;
2342 wake_up_all(&cfg
->reset_waitq
);
2346 wait_event(cfg
->reset_waitq
, cfg
->state
!= STATE_RESET
);
2347 if (cfg
->state
== STATE_NORMAL
)
2355 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
2360 * cxlflash_change_queue_depth() - change the queue depth for the device
2361 * @sdev: SCSI device destined for queue depth change.
2362 * @qdepth: Requested queue depth value to set.
2364 * The requested queue depth is capped to the maximum supported value.
2366 * Return: The actual queue depth set.
2368 static int cxlflash_change_queue_depth(struct scsi_device
*sdev
, int qdepth
)
2371 if (qdepth
> CXLFLASH_MAX_CMDS_PER_LUN
)
2372 qdepth
= CXLFLASH_MAX_CMDS_PER_LUN
;
2374 scsi_change_queue_depth(sdev
, qdepth
);
2375 return sdev
->queue_depth
;
2379 * cxlflash_show_port_status() - queries and presents the current port status
2380 * @port: Desired port for status reporting.
2381 * @cfg: Internal structure associated with the host.
2382 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2384 * Return: The size of the ASCII string returned in @buf or -EINVAL.
2386 static ssize_t
cxlflash_show_port_status(u32 port
,
2387 struct cxlflash_cfg
*cfg
,
2390 struct device
*dev
= &cfg
->dev
->dev
;
2393 __be64 __iomem
*fc_port_regs
;
2395 WARN_ON(port
>= MAX_FC_PORTS
);
2397 if (port
>= cfg
->num_fc_ports
) {
2398 dev_info(dev
, "%s: Port %d not supported on this card.\n",
2403 fc_port_regs
= get_fc_port_regs(cfg
, port
);
2404 status
= readq_be(&fc_port_regs
[FC_MTIP_STATUS
/ 8]);
2405 status
&= FC_MTIP_STATUS_MASK
;
2407 if (status
== FC_MTIP_STATUS_ONLINE
)
2408 disp_status
= "online";
2409 else if (status
== FC_MTIP_STATUS_OFFLINE
)
2410 disp_status
= "offline";
2412 disp_status
= "unknown";
2414 return scnprintf(buf
, PAGE_SIZE
, "%s\n", disp_status
);
2418 * port0_show() - queries and presents the current status of port 0
2419 * @dev: Generic device associated with the host owning the port.
2420 * @attr: Device attribute representing the port.
2421 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2423 * Return: The size of the ASCII string returned in @buf.
2425 static ssize_t
port0_show(struct device
*dev
,
2426 struct device_attribute
*attr
,
2429 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2431 return cxlflash_show_port_status(0, cfg
, buf
);
2435 * port1_show() - queries and presents the current status of port 1
2436 * @dev: Generic device associated with the host owning the port.
2437 * @attr: Device attribute representing the port.
2438 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2440 * Return: The size of the ASCII string returned in @buf.
2442 static ssize_t
port1_show(struct device
*dev
,
2443 struct device_attribute
*attr
,
2446 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2448 return cxlflash_show_port_status(1, cfg
, buf
);
2452 * port2_show() - queries and presents the current status of port 2
2453 * @dev: Generic device associated with the host owning the port.
2454 * @attr: Device attribute representing the port.
2455 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2457 * Return: The size of the ASCII string returned in @buf.
2459 static ssize_t
port2_show(struct device
*dev
,
2460 struct device_attribute
*attr
,
2463 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2465 return cxlflash_show_port_status(2, cfg
, buf
);
2469 * port3_show() - queries and presents the current status of port 3
2470 * @dev: Generic device associated with the host owning the port.
2471 * @attr: Device attribute representing the port.
2472 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2474 * Return: The size of the ASCII string returned in @buf.
2476 static ssize_t
port3_show(struct device
*dev
,
2477 struct device_attribute
*attr
,
2480 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2482 return cxlflash_show_port_status(3, cfg
, buf
);
2486 * lun_mode_show() - presents the current LUN mode of the host
2487 * @dev: Generic device associated with the host.
2488 * @attr: Device attribute representing the LUN mode.
2489 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2491 * Return: The size of the ASCII string returned in @buf.
2493 static ssize_t
lun_mode_show(struct device
*dev
,
2494 struct device_attribute
*attr
, char *buf
)
2496 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2497 struct afu
*afu
= cfg
->afu
;
2499 return scnprintf(buf
, PAGE_SIZE
, "%u\n", afu
->internal_lun
);
2503 * lun_mode_store() - sets the LUN mode of the host
2504 * @dev: Generic device associated with the host.
2505 * @attr: Device attribute representing the LUN mode.
2506 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2507 * @count: Length of data resizing in @buf.
2509 * The CXL Flash AFU supports a dummy LUN mode where the external
2510 * links and storage are not required. Space on the FPGA is used
2511 * to create 1 or 2 small LUNs which are presented to the system
2512 * as if they were a normal storage device. This feature is useful
2513 * during development and also provides manufacturing with a way
2514 * to test the AFU without an actual device.
2516 * 0 = external LUN[s] (default)
2517 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2518 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2519 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2520 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2522 * Return: The size of the ASCII string returned in @buf.
2524 static ssize_t
lun_mode_store(struct device
*dev
,
2525 struct device_attribute
*attr
,
2526 const char *buf
, size_t count
)
2528 struct Scsi_Host
*shost
= class_to_shost(dev
);
2529 struct cxlflash_cfg
*cfg
= shost_priv(shost
);
2530 struct afu
*afu
= cfg
->afu
;
2534 rc
= kstrtouint(buf
, 10, &lun_mode
);
2535 if (!rc
&& (lun_mode
< 5) && (lun_mode
!= afu
->internal_lun
)) {
2536 afu
->internal_lun
= lun_mode
;
2539 * When configured for internal LUN, there is only one channel,
2540 * channel number 0, else there will be one less than the number
2541 * of fc ports for this card.
2543 if (afu
->internal_lun
)
2544 shost
->max_channel
= 0;
2546 shost
->max_channel
= PORTNUM2CHAN(cfg
->num_fc_ports
);
2549 scsi_scan_host(cfg
->host
);
2556 * ioctl_version_show() - presents the current ioctl version of the host
2557 * @dev: Generic device associated with the host.
2558 * @attr: Device attribute representing the ioctl version.
2559 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2561 * Return: The size of the ASCII string returned in @buf.
2563 static ssize_t
ioctl_version_show(struct device
*dev
,
2564 struct device_attribute
*attr
, char *buf
)
2566 return scnprintf(buf
, PAGE_SIZE
, "%u\n", DK_CXLFLASH_VERSION_0
);
2570 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2571 * @port: Desired port for status reporting.
2572 * @cfg: Internal structure associated with the host.
2573 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2575 * Return: The size of the ASCII string returned in @buf or -EINVAL.
2577 static ssize_t
cxlflash_show_port_lun_table(u32 port
,
2578 struct cxlflash_cfg
*cfg
,
2581 struct device
*dev
= &cfg
->dev
->dev
;
2582 __be64 __iomem
*fc_port_luns
;
2586 WARN_ON(port
>= MAX_FC_PORTS
);
2588 if (port
>= cfg
->num_fc_ports
) {
2589 dev_info(dev
, "%s: Port %d not supported on this card.\n",
2594 fc_port_luns
= get_fc_port_luns(cfg
, port
);
2596 for (i
= 0; i
< CXLFLASH_NUM_VLUNS
; i
++)
2597 bytes
+= scnprintf(buf
+ bytes
, PAGE_SIZE
- bytes
,
2599 i
, readq_be(&fc_port_luns
[i
]));
2604 * port0_lun_table_show() - presents the current LUN table of port 0
2605 * @dev: Generic device associated with the host owning the port.
2606 * @attr: Device attribute representing the port.
2607 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2609 * Return: The size of the ASCII string returned in @buf.
2611 static ssize_t
port0_lun_table_show(struct device
*dev
,
2612 struct device_attribute
*attr
,
2615 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2617 return cxlflash_show_port_lun_table(0, cfg
, buf
);
2621 * port1_lun_table_show() - presents the current LUN table of port 1
2622 * @dev: Generic device associated with the host owning the port.
2623 * @attr: Device attribute representing the port.
2624 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2626 * Return: The size of the ASCII string returned in @buf.
2628 static ssize_t
port1_lun_table_show(struct device
*dev
,
2629 struct device_attribute
*attr
,
2632 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2634 return cxlflash_show_port_lun_table(1, cfg
, buf
);
2638 * port2_lun_table_show() - presents the current LUN table of port 2
2639 * @dev: Generic device associated with the host owning the port.
2640 * @attr: Device attribute representing the port.
2641 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2643 * Return: The size of the ASCII string returned in @buf.
2645 static ssize_t
port2_lun_table_show(struct device
*dev
,
2646 struct device_attribute
*attr
,
2649 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2651 return cxlflash_show_port_lun_table(2, cfg
, buf
);
2655 * port3_lun_table_show() - presents the current LUN table of port 3
2656 * @dev: Generic device associated with the host owning the port.
2657 * @attr: Device attribute representing the port.
2658 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2660 * Return: The size of the ASCII string returned in @buf.
2662 static ssize_t
port3_lun_table_show(struct device
*dev
,
2663 struct device_attribute
*attr
,
2666 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2668 return cxlflash_show_port_lun_table(3, cfg
, buf
);
2672 * irqpoll_weight_show() - presents the current IRQ poll weight for the host
2673 * @dev: Generic device associated with the host.
2674 * @attr: Device attribute representing the IRQ poll weight.
2675 * @buf: Buffer of length PAGE_SIZE to report back the current IRQ poll
2678 * An IRQ poll weight of 0 indicates polling is disabled.
2680 * Return: The size of the ASCII string returned in @buf.
2682 static ssize_t
irqpoll_weight_show(struct device
*dev
,
2683 struct device_attribute
*attr
, char *buf
)
2685 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2686 struct afu
*afu
= cfg
->afu
;
2688 return scnprintf(buf
, PAGE_SIZE
, "%u\n", afu
->irqpoll_weight
);
2692 * irqpoll_weight_store() - sets the current IRQ poll weight for the host
2693 * @dev: Generic device associated with the host.
2694 * @attr: Device attribute representing the IRQ poll weight.
2695 * @buf: Buffer of length PAGE_SIZE containing the desired IRQ poll
2697 * @count: Length of data resizing in @buf.
2699 * An IRQ poll weight of 0 indicates polling is disabled.
2701 * Return: The size of the ASCII string returned in @buf.
2703 static ssize_t
irqpoll_weight_store(struct device
*dev
,
2704 struct device_attribute
*attr
,
2705 const char *buf
, size_t count
)
2707 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2708 struct device
*cfgdev
= &cfg
->dev
->dev
;
2709 struct afu
*afu
= cfg
->afu
;
2714 rc
= kstrtouint(buf
, 10, &weight
);
2720 "Invalid IRQ poll weight. It must be 256 or less.\n");
2724 if (weight
== afu
->irqpoll_weight
) {
2726 "Current IRQ poll weight has the same weight.\n");
2730 if (afu_is_irqpoll_enabled(afu
)) {
2731 for (i
= 0; i
< afu
->num_hwqs
; i
++) {
2732 hwq
= get_hwq(afu
, i
);
2734 irq_poll_disable(&hwq
->irqpoll
);
2738 afu
->irqpoll_weight
= weight
;
2741 for (i
= 0; i
< afu
->num_hwqs
; i
++) {
2742 hwq
= get_hwq(afu
, i
);
2744 irq_poll_init(&hwq
->irqpoll
, weight
, cxlflash_irqpoll
);
2752 * num_hwqs_show() - presents the number of hardware queues for the host
2753 * @dev: Generic device associated with the host.
2754 * @attr: Device attribute representing the number of hardware queues.
2755 * @buf: Buffer of length PAGE_SIZE to report back the number of hardware
2758 * Return: The size of the ASCII string returned in @buf.
2760 static ssize_t
num_hwqs_show(struct device
*dev
,
2761 struct device_attribute
*attr
, char *buf
)
2763 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2764 struct afu
*afu
= cfg
->afu
;
2766 return scnprintf(buf
, PAGE_SIZE
, "%u\n", afu
->num_hwqs
);
2770 * num_hwqs_store() - sets the number of hardware queues for the host
2771 * @dev: Generic device associated with the host.
2772 * @attr: Device attribute representing the number of hardware queues.
2773 * @buf: Buffer of length PAGE_SIZE containing the number of hardware
2775 * @count: Length of data resizing in @buf.
2777 * n > 0: num_hwqs = n
2778 * n = 0: num_hwqs = num_online_cpus()
2779 * n < 0: num_online_cpus() / abs(n)
2781 * Return: The size of the ASCII string returned in @buf.
2783 static ssize_t
num_hwqs_store(struct device
*dev
,
2784 struct device_attribute
*attr
,
2785 const char *buf
, size_t count
)
2787 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2788 struct afu
*afu
= cfg
->afu
;
2790 int nhwqs
, num_hwqs
;
2792 rc
= kstrtoint(buf
, 10, &nhwqs
);
2798 else if (nhwqs
== 0)
2799 num_hwqs
= num_online_cpus();
2801 num_hwqs
= num_online_cpus() / abs(nhwqs
);
2803 afu
->desired_hwqs
= min(num_hwqs
, CXLFLASH_MAX_HWQS
);
2804 WARN_ON_ONCE(afu
->desired_hwqs
== 0);
2807 switch (cfg
->state
) {
2809 cfg
->state
= STATE_RESET
;
2811 cxlflash_mark_contexts_error(cfg
);
2812 rc
= afu_reset(cfg
);
2814 cfg
->state
= STATE_FAILTERM
;
2816 cfg
->state
= STATE_NORMAL
;
2817 wake_up_all(&cfg
->reset_waitq
);
2820 wait_event(cfg
->reset_waitq
, cfg
->state
!= STATE_RESET
);
2821 if (cfg
->state
== STATE_NORMAL
)
2824 /* Ideally should not happen */
2825 dev_err(dev
, "%s: Device is not ready, state=%d\n",
2826 __func__
, cfg
->state
);
2833 static const char *hwq_mode_name
[MAX_HWQ_MODE
] = { "rr", "tag", "cpu" };
2836 * hwq_mode_show() - presents the HWQ steering mode for the host
2837 * @dev: Generic device associated with the host.
2838 * @attr: Device attribute representing the HWQ steering mode.
2839 * @buf: Buffer of length PAGE_SIZE to report back the HWQ steering mode
2840 * as a character string.
2842 * Return: The size of the ASCII string returned in @buf.
2844 static ssize_t
hwq_mode_show(struct device
*dev
,
2845 struct device_attribute
*attr
, char *buf
)
2847 struct cxlflash_cfg
*cfg
= shost_priv(class_to_shost(dev
));
2848 struct afu
*afu
= cfg
->afu
;
2850 return scnprintf(buf
, PAGE_SIZE
, "%s\n", hwq_mode_name
[afu
->hwq_mode
]);
2854 * hwq_mode_store() - sets the HWQ steering mode for the host
2855 * @dev: Generic device associated with the host.
2856 * @attr: Device attribute representing the HWQ steering mode.
2857 * @buf: Buffer of length PAGE_SIZE containing the HWQ steering mode
2858 * as a character string.
2859 * @count: Length of data resizing in @buf.
2862 * tag = Block MQ Tagging
2863 * cpu = CPU Affinity
2865 * Return: The size of the ASCII string returned in @buf.
2867 static ssize_t
hwq_mode_store(struct device
*dev
,
2868 struct device_attribute
*attr
,
2869 const char *buf
, size_t count
)
2871 struct Scsi_Host
*shost
= class_to_shost(dev
);
2872 struct cxlflash_cfg
*cfg
= shost_priv(shost
);
2873 struct device
*cfgdev
= &cfg
->dev
->dev
;
2874 struct afu
*afu
= cfg
->afu
;
2876 u32 mode
= MAX_HWQ_MODE
;
2878 for (i
= 0; i
< MAX_HWQ_MODE
; i
++) {
2879 if (!strncmp(hwq_mode_name
[i
], buf
, strlen(hwq_mode_name
[i
]))) {
2885 if (mode
>= MAX_HWQ_MODE
) {
2886 dev_info(cfgdev
, "Invalid HWQ steering mode.\n");
2890 if ((mode
== HWQ_MODE_TAG
) && !shost_use_blk_mq(shost
)) {
2891 dev_info(cfgdev
, "SCSI-MQ is not enabled, use a different "
2892 "HWQ steering mode.\n");
2896 afu
->hwq_mode
= mode
;
2902 * mode_show() - presents the current mode of the device
2903 * @dev: Generic device associated with the device.
2904 * @attr: Device attribute representing the device mode.
2905 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2907 * Return: The size of the ASCII string returned in @buf.
2909 static ssize_t
mode_show(struct device
*dev
,
2910 struct device_attribute
*attr
, char *buf
)
2912 struct scsi_device
*sdev
= to_scsi_device(dev
);
2914 return scnprintf(buf
, PAGE_SIZE
, "%s\n",
2915 sdev
->hostdata
? "superpipe" : "legacy");
2921 static DEVICE_ATTR_RO(port0
);
2922 static DEVICE_ATTR_RO(port1
);
2923 static DEVICE_ATTR_RO(port2
);
2924 static DEVICE_ATTR_RO(port3
);
2925 static DEVICE_ATTR_RW(lun_mode
);
2926 static DEVICE_ATTR_RO(ioctl_version
);
2927 static DEVICE_ATTR_RO(port0_lun_table
);
2928 static DEVICE_ATTR_RO(port1_lun_table
);
2929 static DEVICE_ATTR_RO(port2_lun_table
);
2930 static DEVICE_ATTR_RO(port3_lun_table
);
2931 static DEVICE_ATTR_RW(irqpoll_weight
);
2932 static DEVICE_ATTR_RW(num_hwqs
);
2933 static DEVICE_ATTR_RW(hwq_mode
);
2935 static struct device_attribute
*cxlflash_host_attrs
[] = {
2941 &dev_attr_ioctl_version
,
2942 &dev_attr_port0_lun_table
,
2943 &dev_attr_port1_lun_table
,
2944 &dev_attr_port2_lun_table
,
2945 &dev_attr_port3_lun_table
,
2946 &dev_attr_irqpoll_weight
,
2955 static DEVICE_ATTR_RO(mode
);
2957 static struct device_attribute
*cxlflash_dev_attrs
[] = {
2965 static struct scsi_host_template driver_template
= {
2966 .module
= THIS_MODULE
,
2967 .name
= CXLFLASH_ADAPTER_NAME
,
2968 .info
= cxlflash_driver_info
,
2969 .ioctl
= cxlflash_ioctl
,
2970 .proc_name
= CXLFLASH_NAME
,
2971 .queuecommand
= cxlflash_queuecommand
,
2972 .eh_device_reset_handler
= cxlflash_eh_device_reset_handler
,
2973 .eh_host_reset_handler
= cxlflash_eh_host_reset_handler
,
2974 .change_queue_depth
= cxlflash_change_queue_depth
,
2975 .cmd_per_lun
= CXLFLASH_MAX_CMDS_PER_LUN
,
2976 .can_queue
= CXLFLASH_MAX_CMDS
,
2977 .cmd_size
= sizeof(struct afu_cmd
) + __alignof__(struct afu_cmd
) - 1,
2979 .sg_tablesize
= 1, /* No scatter gather support */
2980 .max_sectors
= CXLFLASH_MAX_SECTORS
,
2981 .use_clustering
= ENABLE_CLUSTERING
,
2982 .shost_attrs
= cxlflash_host_attrs
,
2983 .sdev_attrs
= cxlflash_dev_attrs
,
2987 * Device dependent values
2989 static struct dev_dependent_vals dev_corsa_vals
= { CXLFLASH_MAX_SECTORS
,
2991 static struct dev_dependent_vals dev_flash_gt_vals
= { CXLFLASH_MAX_SECTORS
,
2992 CXLFLASH_NOTIFY_SHUTDOWN
};
2993 static struct dev_dependent_vals dev_briard_vals
= { CXLFLASH_MAX_SECTORS
,
2994 CXLFLASH_NOTIFY_SHUTDOWN
};
2997 * PCI device binding table
2999 static struct pci_device_id cxlflash_pci_table
[] = {
3000 {PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_IBM_CORSA
,
3001 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (kernel_ulong_t
)&dev_corsa_vals
},
3002 {PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_IBM_FLASH_GT
,
3003 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (kernel_ulong_t
)&dev_flash_gt_vals
},
3004 {PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_IBM_BRIARD
,
3005 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, (kernel_ulong_t
)&dev_briard_vals
},
3009 MODULE_DEVICE_TABLE(pci
, cxlflash_pci_table
);
3012 * cxlflash_worker_thread() - work thread handler for the AFU
3013 * @work: Work structure contained within cxlflash associated with host.
3015 * Handles the following events:
3016 * - Link reset which cannot be performed on interrupt context due to
3017 * blocking up to a few seconds
3020 static void cxlflash_worker_thread(struct work_struct
*work
)
3022 struct cxlflash_cfg
*cfg
= container_of(work
, struct cxlflash_cfg
,
3024 struct afu
*afu
= cfg
->afu
;
3025 struct device
*dev
= &cfg
->dev
->dev
;
3026 __be64 __iomem
*fc_port_regs
;
3030 /* Avoid MMIO if the device has failed */
3032 if (cfg
->state
!= STATE_NORMAL
)
3035 spin_lock_irqsave(cfg
->host
->host_lock
, lock_flags
);
3037 if (cfg
->lr_state
== LINK_RESET_REQUIRED
) {
3038 port
= cfg
->lr_port
;
3040 dev_err(dev
, "%s: invalid port index %d\n",
3043 spin_unlock_irqrestore(cfg
->host
->host_lock
,
3046 /* The reset can block... */
3047 fc_port_regs
= get_fc_port_regs(cfg
, port
);
3048 afu_link_reset(afu
, port
, fc_port_regs
);
3049 spin_lock_irqsave(cfg
->host
->host_lock
, lock_flags
);
3052 cfg
->lr_state
= LINK_RESET_COMPLETE
;
3055 spin_unlock_irqrestore(cfg
->host
->host_lock
, lock_flags
);
3057 if (atomic_dec_if_positive(&cfg
->scan_host_needed
) >= 0)
3058 scsi_scan_host(cfg
->host
);
3062 * cxlflash_probe() - PCI entry point to add host
3063 * @pdev: PCI device associated with the host.
3064 * @dev_id: PCI device id associated with device.
3066 * The device will initially start out in a 'probing' state and
3067 * transition to the 'normal' state at the end of a successful
3068 * probe. Should an EEH event occur during probe, the notification
3069 * thread (error_detected()) will wait until the probe handler
3070 * is nearly complete. At that time, the device will be moved to
3071 * a 'probed' state and the EEH thread woken up to drive the slot
3072 * reset and recovery (device moves to 'normal' state). Meanwhile,
3073 * the probe will be allowed to exit successfully.
3075 * Return: 0 on success, -errno on failure
3077 static int cxlflash_probe(struct pci_dev
*pdev
,
3078 const struct pci_device_id
*dev_id
)
3080 struct Scsi_Host
*host
;
3081 struct cxlflash_cfg
*cfg
= NULL
;
3082 struct device
*dev
= &pdev
->dev
;
3083 struct dev_dependent_vals
*ddv
;
3087 dev_dbg(&pdev
->dev
, "%s: Found CXLFLASH with IRQ: %d\n",
3088 __func__
, pdev
->irq
);
3090 ddv
= (struct dev_dependent_vals
*)dev_id
->driver_data
;
3091 driver_template
.max_sectors
= ddv
->max_sectors
;
3093 host
= scsi_host_alloc(&driver_template
, sizeof(struct cxlflash_cfg
));
3095 dev_err(dev
, "%s: scsi_host_alloc failed\n", __func__
);
3100 host
->max_id
= CXLFLASH_MAX_NUM_TARGETS_PER_BUS
;
3101 host
->max_lun
= CXLFLASH_MAX_NUM_LUNS_PER_TARGET
;
3102 host
->unique_id
= host
->host_no
;
3103 host
->max_cmd_len
= CXLFLASH_MAX_CDB_LEN
;
3105 cfg
= shost_priv(host
);
3107 rc
= alloc_mem(cfg
);
3109 dev_err(dev
, "%s: alloc_mem failed\n", __func__
);
3111 scsi_host_put(cfg
->host
);
3115 cfg
->init_state
= INIT_STATE_NONE
;
3117 cfg
->cxl_fops
= cxlflash_cxl_fops
;
3120 * Promoted LUNs move to the top of the LUN table. The rest stay on
3121 * the bottom half. The bottom half grows from the end (index = 255),
3122 * whereas the top half grows from the beginning (index = 0).
3124 * Initialize the last LUN index for all possible ports.
3126 cfg
->promote_lun_index
= 0;
3128 for (k
= 0; k
< MAX_FC_PORTS
; k
++)
3129 cfg
->last_lun_index
[k
] = CXLFLASH_NUM_VLUNS
/2 - 1;
3131 cfg
->dev_id
= (struct pci_device_id
*)dev_id
;
3133 init_waitqueue_head(&cfg
->tmf_waitq
);
3134 init_waitqueue_head(&cfg
->reset_waitq
);
3136 INIT_WORK(&cfg
->work_q
, cxlflash_worker_thread
);
3137 cfg
->lr_state
= LINK_RESET_INVALID
;
3139 spin_lock_init(&cfg
->tmf_slock
);
3140 mutex_init(&cfg
->ctx_tbl_list_mutex
);
3141 mutex_init(&cfg
->ctx_recovery_mutex
);
3142 init_rwsem(&cfg
->ioctl_rwsem
);
3143 INIT_LIST_HEAD(&cfg
->ctx_err_recovery
);
3144 INIT_LIST_HEAD(&cfg
->lluns
);
3146 pci_set_drvdata(pdev
, cfg
);
3148 cfg
->cxl_afu
= cxl_pci_to_afu(pdev
);
3152 dev_err(dev
, "%s: init_pci failed rc=%d\n", __func__
, rc
);
3155 cfg
->init_state
= INIT_STATE_PCI
;
3158 if (rc
&& !wq_has_sleeper(&cfg
->reset_waitq
)) {
3159 dev_err(dev
, "%s: init_afu failed rc=%d\n", __func__
, rc
);
3162 cfg
->init_state
= INIT_STATE_AFU
;
3164 rc
= init_scsi(cfg
);
3166 dev_err(dev
, "%s: init_scsi failed rc=%d\n", __func__
, rc
);
3169 cfg
->init_state
= INIT_STATE_SCSI
;
3171 if (wq_has_sleeper(&cfg
->reset_waitq
)) {
3172 cfg
->state
= STATE_PROBED
;
3173 wake_up_all(&cfg
->reset_waitq
);
3175 cfg
->state
= STATE_NORMAL
;
3177 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
3181 cxlflash_remove(pdev
);
3186 * cxlflash_pci_error_detected() - called when a PCI error is detected
3187 * @pdev: PCI device struct.
3188 * @state: PCI channel state.
3190 * When an EEH occurs during an active reset, wait until the reset is
3191 * complete and then take action based upon the device state.
3193 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
3195 static pci_ers_result_t
cxlflash_pci_error_detected(struct pci_dev
*pdev
,
3196 pci_channel_state_t state
)
3199 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
3200 struct device
*dev
= &cfg
->dev
->dev
;
3202 dev_dbg(dev
, "%s: pdev=%p state=%u\n", __func__
, pdev
, state
);
3205 case pci_channel_io_frozen
:
3206 wait_event(cfg
->reset_waitq
, cfg
->state
!= STATE_RESET
&&
3207 cfg
->state
!= STATE_PROBING
);
3208 if (cfg
->state
== STATE_FAILTERM
)
3209 return PCI_ERS_RESULT_DISCONNECT
;
3211 cfg
->state
= STATE_RESET
;
3212 scsi_block_requests(cfg
->host
);
3214 rc
= cxlflash_mark_contexts_error(cfg
);
3216 dev_err(dev
, "%s: Failed to mark user contexts rc=%d\n",
3219 return PCI_ERS_RESULT_NEED_RESET
;
3220 case pci_channel_io_perm_failure
:
3221 cfg
->state
= STATE_FAILTERM
;
3222 wake_up_all(&cfg
->reset_waitq
);
3223 scsi_unblock_requests(cfg
->host
);
3224 return PCI_ERS_RESULT_DISCONNECT
;
3228 return PCI_ERS_RESULT_NEED_RESET
;
3232 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
3233 * @pdev: PCI device struct.
3235 * This routine is called by the pci error recovery code after the PCI
3236 * slot has been reset, just before we should resume normal operations.
3238 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
3240 static pci_ers_result_t
cxlflash_pci_slot_reset(struct pci_dev
*pdev
)
3243 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
3244 struct device
*dev
= &cfg
->dev
->dev
;
3246 dev_dbg(dev
, "%s: pdev=%p\n", __func__
, pdev
);
3250 dev_err(dev
, "%s: EEH recovery failed rc=%d\n", __func__
, rc
);
3251 return PCI_ERS_RESULT_DISCONNECT
;
3254 return PCI_ERS_RESULT_RECOVERED
;
3258 * cxlflash_pci_resume() - called when normal operation can resume
3259 * @pdev: PCI device struct
3261 static void cxlflash_pci_resume(struct pci_dev
*pdev
)
3263 struct cxlflash_cfg
*cfg
= pci_get_drvdata(pdev
);
3264 struct device
*dev
= &cfg
->dev
->dev
;
3266 dev_dbg(dev
, "%s: pdev=%p\n", __func__
, pdev
);
3268 cfg
->state
= STATE_NORMAL
;
3269 wake_up_all(&cfg
->reset_waitq
);
3270 scsi_unblock_requests(cfg
->host
);
3273 static const struct pci_error_handlers cxlflash_err_handler
= {
3274 .error_detected
= cxlflash_pci_error_detected
,
3275 .slot_reset
= cxlflash_pci_slot_reset
,
3276 .resume
= cxlflash_pci_resume
,
3280 * PCI device structure
3282 static struct pci_driver cxlflash_driver
= {
3283 .name
= CXLFLASH_NAME
,
3284 .id_table
= cxlflash_pci_table
,
3285 .probe
= cxlflash_probe
,
3286 .remove
= cxlflash_remove
,
3287 .shutdown
= cxlflash_remove
,
3288 .err_handler
= &cxlflash_err_handler
,
3292 * init_cxlflash() - module entry point
3294 * Return: 0 on success, -errno on failure
3296 static int __init
init_cxlflash(void)
3299 cxlflash_list_init();
3301 return pci_register_driver(&cxlflash_driver
);
3305 * exit_cxlflash() - module exit point
3307 static void __exit
exit_cxlflash(void)
3309 cxlflash_term_global_luns();
3310 cxlflash_free_errpage();
3312 pci_unregister_driver(&cxlflash_driver
);
3315 module_init(init_cxlflash
);
3316 module_exit(exit_cxlflash
);