scsi: cxlflash: Schedule asynchronous reset of the host
[linux-2.6/btrfs-unstable.git] / drivers / scsi / cxlflash / main.c
blob20c2c5e111b44364f98a15c53293bf0b9fdb6bb1
1 /*
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>
22 #include <misc/cxl.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_host.h>
26 #include <uapi/scsi/cxlflash_ioctl.h>
28 #include "main.h"
29 #include "sislite.h"
30 #include "common.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");
37 /**
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;
51 u32 resid;
53 if (unlikely(!cmd))
54 return;
56 ioarcb = &(cmd->rcb);
57 ioasa = &(cmd->sa);
59 if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) {
60 resid = ioasa->resid;
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",
68 __func__, cmd, scp);
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,
81 SISL_SENSE_DATA_LEN);
82 scp->result = ioasa->rc.scsi_rc;
83 } else
84 scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16);
88 * We encountered an error. Set scp->result based on nature
89 * of error.
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);
96 break;
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);
107 break;
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);
119 break;
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);
128 break;
129 case SISL_AFU_RC_DATA_DMA_ERR:
130 switch (ioasa->afu_extra) {
131 case SISL_AFU_DMA_ERR_PAGE_IN:
132 /* Retry */
133 scp->result = (DID_IMM_RETRY << 16);
134 break;
135 case SISL_AFU_DMA_ERR_INVALID_EA:
136 default:
137 scp->result = (DID_ERROR << 16);
139 break;
140 case SISL_AFU_RC_OUT_OF_DATA_BUFS:
141 /* Retry */
142 scp->result = (DID_ALLOC_FAILURE << 16);
143 break;
144 default:
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;
161 ulong lock_flags;
162 struct afu *afu = cmd->parent;
163 struct cxlflash_cfg *cfg = afu->parent;
164 struct device *dev = &cfg->dev->dev;
165 bool cmd_is_tmf;
167 if (cmd->scp) {
168 scp = cmd->scp;
169 if (unlikely(cmd->sa.ioasc))
170 process_cmd_err(cmd, scp);
171 else
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);
179 scp->scsi_done(scp);
181 if (cmd_is_tmf) {
182 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
183 cfg->tmf_active = false;
184 wake_up_all_locked(&cfg->tmf_waitq);
185 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
187 } else
188 complete(&cmd->cevent);
192 * context_reset() - reset context via specified register
193 * @hwq: Hardware queue owning the context to be reset.
194 * @reset_reg: MMIO register to perform reset.
196 * Return: 0 on success, -errno on failure
198 static int context_reset(struct hwq *hwq, __be64 __iomem *reset_reg)
200 struct cxlflash_cfg *cfg = hwq->afu->parent;
201 struct device *dev = &cfg->dev->dev;
202 int rc = -ETIMEDOUT;
203 int nretry = 0;
204 u64 val = 0x1;
206 dev_dbg(dev, "%s: hwq=%p\n", __func__, hwq);
208 writeq_be(val, reset_reg);
209 do {
210 val = readq_be(reset_reg);
211 if ((val & 0x1) == 0x0) {
212 rc = 0;
213 break;
216 /* Double delay each time */
217 udelay(1 << nretry);
218 } while (nretry++ < MC_ROOM_RETRY_CNT);
220 dev_dbg(dev, "%s: returning rc=%d, val=%016llx nretry=%d\n",
221 __func__, rc, val, nretry);
222 return rc;
226 * context_reset_ioarrin() - reset context via IOARRIN register
227 * @hwq: Hardware queue owning the context to be reset.
229 * Return: 0 on success, -errno on failure
231 static int context_reset_ioarrin(struct hwq *hwq)
233 return context_reset(hwq, &hwq->host_map->ioarrin);
237 * context_reset_sq() - reset context via SQ_CONTEXT_RESET register
238 * @hwq: Hardware queue owning the context to be reset.
240 * Return: 0 on success, -errno on failure
242 static int context_reset_sq(struct hwq *hwq)
244 return context_reset(hwq, &hwq->host_map->sq_ctx_reset);
248 * send_cmd_ioarrin() - sends an AFU command via IOARRIN register
249 * @afu: AFU associated with the host.
250 * @cmd: AFU command to send.
252 * Return:
253 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
255 static int send_cmd_ioarrin(struct afu *afu, struct afu_cmd *cmd)
257 struct cxlflash_cfg *cfg = afu->parent;
258 struct device *dev = &cfg->dev->dev;
259 struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
260 int rc = 0;
261 s64 room;
262 ulong lock_flags;
265 * To avoid the performance penalty of MMIO, spread the update of
266 * 'room' over multiple commands.
268 spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
269 if (--hwq->room < 0) {
270 room = readq_be(&hwq->host_map->cmd_room);
271 if (room <= 0) {
272 dev_dbg_ratelimited(dev, "%s: no cmd_room to send "
273 "0x%02X, room=0x%016llX\n",
274 __func__, cmd->rcb.cdb[0], room);
275 hwq->room = 0;
276 rc = SCSI_MLQUEUE_HOST_BUSY;
277 goto out;
279 hwq->room = room - 1;
282 writeq_be((u64)&cmd->rcb, &hwq->host_map->ioarrin);
283 out:
284 spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
285 dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx rc=%d\n", __func__,
286 cmd, cmd->rcb.data_len, cmd->rcb.data_ea, rc);
287 return rc;
291 * send_cmd_sq() - sends an AFU command via SQ ring
292 * @afu: AFU associated with the host.
293 * @cmd: AFU command to send.
295 * Return:
296 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
298 static int send_cmd_sq(struct afu *afu, struct afu_cmd *cmd)
300 struct cxlflash_cfg *cfg = afu->parent;
301 struct device *dev = &cfg->dev->dev;
302 struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
303 int rc = 0;
304 int newval;
305 ulong lock_flags;
307 newval = atomic_dec_if_positive(&hwq->hsq_credits);
308 if (newval <= 0) {
309 rc = SCSI_MLQUEUE_HOST_BUSY;
310 goto out;
313 cmd->rcb.ioasa = &cmd->sa;
315 spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
317 *hwq->hsq_curr = cmd->rcb;
318 if (hwq->hsq_curr < hwq->hsq_end)
319 hwq->hsq_curr++;
320 else
321 hwq->hsq_curr = hwq->hsq_start;
322 writeq_be((u64)hwq->hsq_curr, &hwq->host_map->sq_tail);
324 spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
325 out:
326 dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx ioasa=%p rc=%d curr=%p "
327 "head=%016llx tail=%016llx\n", __func__, cmd, cmd->rcb.data_len,
328 cmd->rcb.data_ea, cmd->rcb.ioasa, rc, hwq->hsq_curr,
329 readq_be(&hwq->host_map->sq_head),
330 readq_be(&hwq->host_map->sq_tail));
331 return rc;
335 * wait_resp() - polls for a response or timeout to a sent AFU command
336 * @afu: AFU associated with the host.
337 * @cmd: AFU command that was sent.
339 * Return: 0 on success, -errno on failure
341 static int wait_resp(struct afu *afu, struct afu_cmd *cmd)
343 struct cxlflash_cfg *cfg = afu->parent;
344 struct device *dev = &cfg->dev->dev;
345 int rc = 0;
346 ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
348 timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
349 if (!timeout)
350 rc = -ETIMEDOUT;
352 if (unlikely(cmd->sa.ioasc != 0)) {
353 dev_err(dev, "%s: cmd %02x failed, ioasc=%08x\n",
354 __func__, cmd->rcb.cdb[0], cmd->sa.ioasc);
355 rc = -EIO;
358 return rc;
362 * cmd_to_target_hwq() - selects a target hardware queue for a SCSI command
363 * @host: SCSI host associated with device.
364 * @scp: SCSI command to send.
365 * @afu: SCSI command to send.
367 * Hashes a command based upon the hardware queue mode.
369 * Return: Trusted index of target hardware queue
371 static u32 cmd_to_target_hwq(struct Scsi_Host *host, struct scsi_cmnd *scp,
372 struct afu *afu)
374 u32 tag;
375 u32 hwq = 0;
377 if (afu->num_hwqs == 1)
378 return 0;
380 switch (afu->hwq_mode) {
381 case HWQ_MODE_RR:
382 hwq = afu->hwq_rr_count++ % afu->num_hwqs;
383 break;
384 case HWQ_MODE_TAG:
385 tag = blk_mq_unique_tag(scp->request);
386 hwq = blk_mq_unique_tag_to_hwq(tag);
387 break;
388 case HWQ_MODE_CPU:
389 hwq = smp_processor_id() % afu->num_hwqs;
390 break;
391 default:
392 WARN_ON_ONCE(1);
395 return hwq;
399 * send_tmf() - sends a Task Management Function (TMF)
400 * @afu: AFU to checkout from.
401 * @scp: SCSI command from stack.
402 * @tmfcmd: TMF command to send.
404 * Return:
405 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
407 static int send_tmf(struct afu *afu, struct scsi_cmnd *scp, u64 tmfcmd)
409 struct Scsi_Host *host = scp->device->host;
410 struct cxlflash_cfg *cfg = shost_priv(host);
411 struct afu_cmd *cmd = sc_to_afucz(scp);
412 struct device *dev = &cfg->dev->dev;
413 int hwq_index = cmd_to_target_hwq(host, scp, afu);
414 struct hwq *hwq = get_hwq(afu, hwq_index);
415 ulong lock_flags;
416 int rc = 0;
417 ulong to;
419 /* When Task Management Function is active do not send another */
420 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
421 if (cfg->tmf_active)
422 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
423 !cfg->tmf_active,
424 cfg->tmf_slock);
425 cfg->tmf_active = true;
426 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
428 cmd->scp = scp;
429 cmd->parent = afu;
430 cmd->cmd_tmf = true;
431 cmd->hwq_index = hwq_index;
433 cmd->rcb.ctx_id = hwq->ctx_hndl;
434 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
435 cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
436 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
437 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
438 SISL_REQ_FLAGS_SUP_UNDERRUN |
439 SISL_REQ_FLAGS_TMF_CMD);
440 memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
442 rc = afu->send_cmd(afu, cmd);
443 if (unlikely(rc)) {
444 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
445 cfg->tmf_active = false;
446 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
447 goto out;
450 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
451 to = msecs_to_jiffies(5000);
452 to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
453 !cfg->tmf_active,
454 cfg->tmf_slock,
455 to);
456 if (!to) {
457 cfg->tmf_active = false;
458 dev_err(dev, "%s: TMF timed out\n", __func__);
459 rc = -1;
461 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
462 out:
463 return rc;
467 * cxlflash_driver_info() - information handler for this host driver
468 * @host: SCSI host associated with device.
470 * Return: A string describing the device.
472 static const char *cxlflash_driver_info(struct Scsi_Host *host)
474 return CXLFLASH_ADAPTER_NAME;
478 * cxlflash_queuecommand() - sends a mid-layer request
479 * @host: SCSI host associated with device.
480 * @scp: SCSI command to send.
482 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
484 static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
486 struct cxlflash_cfg *cfg = shost_priv(host);
487 struct afu *afu = cfg->afu;
488 struct device *dev = &cfg->dev->dev;
489 struct afu_cmd *cmd = sc_to_afucz(scp);
490 struct scatterlist *sg = scsi_sglist(scp);
491 int hwq_index = cmd_to_target_hwq(host, scp, afu);
492 struct hwq *hwq = get_hwq(afu, hwq_index);
493 u16 req_flags = SISL_REQ_FLAGS_SUP_UNDERRUN;
494 ulong lock_flags;
495 int rc = 0;
497 dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
498 "cdb=(%08x-%08x-%08x-%08x)\n",
499 __func__, scp, host->host_no, scp->device->channel,
500 scp->device->id, scp->device->lun,
501 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
502 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
503 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
504 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
507 * If a Task Management Function is active, wait for it to complete
508 * before continuing with regular commands.
510 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
511 if (cfg->tmf_active) {
512 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
513 rc = SCSI_MLQUEUE_HOST_BUSY;
514 goto out;
516 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
518 switch (cfg->state) {
519 case STATE_PROBING:
520 case STATE_PROBED:
521 case STATE_RESET:
522 dev_dbg_ratelimited(dev, "%s: device is in reset\n", __func__);
523 rc = SCSI_MLQUEUE_HOST_BUSY;
524 goto out;
525 case STATE_FAILTERM:
526 dev_dbg_ratelimited(dev, "%s: device has failed\n", __func__);
527 scp->result = (DID_NO_CONNECT << 16);
528 scp->scsi_done(scp);
529 rc = 0;
530 goto out;
531 default:
532 break;
535 if (likely(sg)) {
536 cmd->rcb.data_len = sg->length;
537 cmd->rcb.data_ea = (uintptr_t)sg_virt(sg);
540 cmd->scp = scp;
541 cmd->parent = afu;
542 cmd->hwq_index = hwq_index;
544 cmd->rcb.ctx_id = hwq->ctx_hndl;
545 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
546 cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
547 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
549 if (scp->sc_data_direction == DMA_TO_DEVICE)
550 req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
552 cmd->rcb.req_flags = req_flags;
553 memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
555 rc = afu->send_cmd(afu, cmd);
556 out:
557 return rc;
561 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
562 * @cfg: Internal structure associated with the host.
564 static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
566 struct pci_dev *pdev = cfg->dev;
568 if (pci_channel_offline(pdev))
569 wait_event_timeout(cfg->reset_waitq,
570 !pci_channel_offline(pdev),
571 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
575 * free_mem() - free memory associated with the AFU
576 * @cfg: Internal structure associated with the host.
578 static void free_mem(struct cxlflash_cfg *cfg)
580 struct afu *afu = cfg->afu;
582 if (cfg->afu) {
583 free_pages((ulong)afu, get_order(sizeof(struct afu)));
584 cfg->afu = NULL;
589 * cxlflash_reset_sync() - synchronizing point for asynchronous resets
590 * @cfg: Internal structure associated with the host.
592 static void cxlflash_reset_sync(struct cxlflash_cfg *cfg)
594 if (cfg->async_reset_cookie == 0)
595 return;
597 /* Wait until all async calls prior to this cookie have completed */
598 async_synchronize_cookie(cfg->async_reset_cookie + 1);
599 cfg->async_reset_cookie = 0;
603 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
604 * @cfg: Internal structure associated with the host.
606 * Safe to call with AFU in a partially allocated/initialized state.
608 * Cancels scheduled worker threads, waits for any active internal AFU
609 * commands to timeout, disables IRQ polling and then unmaps the MMIO space.
611 static void stop_afu(struct cxlflash_cfg *cfg)
613 struct afu *afu = cfg->afu;
614 struct hwq *hwq;
615 int i;
617 cancel_work_sync(&cfg->work_q);
618 if (!current_is_async())
619 cxlflash_reset_sync(cfg);
621 if (likely(afu)) {
622 while (atomic_read(&afu->cmds_active))
623 ssleep(1);
625 if (afu_is_irqpoll_enabled(afu)) {
626 for (i = 0; i < afu->num_hwqs; i++) {
627 hwq = get_hwq(afu, i);
629 irq_poll_disable(&hwq->irqpoll);
633 if (likely(afu->afu_map)) {
634 cxl_psa_unmap((void __iomem *)afu->afu_map);
635 afu->afu_map = NULL;
641 * term_intr() - disables all AFU interrupts
642 * @cfg: Internal structure associated with the host.
643 * @level: Depth of allocation, where to begin waterfall tear down.
644 * @index: Index of the hardware queue.
646 * Safe to call with AFU/MC in partially allocated/initialized state.
648 static void term_intr(struct cxlflash_cfg *cfg, enum undo_level level,
649 u32 index)
651 struct afu *afu = cfg->afu;
652 struct device *dev = &cfg->dev->dev;
653 struct hwq *hwq;
655 if (!afu) {
656 dev_err(dev, "%s: returning with NULL afu\n", __func__);
657 return;
660 hwq = get_hwq(afu, index);
662 if (!hwq->ctx) {
663 dev_err(dev, "%s: returning with NULL MC\n", __func__);
664 return;
667 switch (level) {
668 case UNMAP_THREE:
669 /* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
670 if (index == PRIMARY_HWQ)
671 cxl_unmap_afu_irq(hwq->ctx, 3, hwq);
672 case UNMAP_TWO:
673 cxl_unmap_afu_irq(hwq->ctx, 2, hwq);
674 case UNMAP_ONE:
675 cxl_unmap_afu_irq(hwq->ctx, 1, hwq);
676 case FREE_IRQ:
677 cxl_free_afu_irqs(hwq->ctx);
678 /* fall through */
679 case UNDO_NOOP:
680 /* No action required */
681 break;
686 * term_mc() - terminates the master context
687 * @cfg: Internal structure associated with the host.
688 * @index: Index of the hardware queue.
690 * Safe to call with AFU/MC in partially allocated/initialized state.
692 static void term_mc(struct cxlflash_cfg *cfg, u32 index)
694 struct afu *afu = cfg->afu;
695 struct device *dev = &cfg->dev->dev;
696 struct hwq *hwq;
698 if (!afu) {
699 dev_err(dev, "%s: returning with NULL afu\n", __func__);
700 return;
703 hwq = get_hwq(afu, index);
705 if (!hwq->ctx) {
706 dev_err(dev, "%s: returning with NULL MC\n", __func__);
707 return;
710 WARN_ON(cxl_stop_context(hwq->ctx));
711 if (index != PRIMARY_HWQ)
712 WARN_ON(cxl_release_context(hwq->ctx));
713 hwq->ctx = NULL;
717 * term_afu() - terminates the AFU
718 * @cfg: Internal structure associated with the host.
720 * Safe to call with AFU/MC in partially allocated/initialized state.
722 static void term_afu(struct cxlflash_cfg *cfg)
724 struct device *dev = &cfg->dev->dev;
725 int k;
728 * Tear down is carefully orchestrated to ensure
729 * no interrupts can come in when the problem state
730 * area is unmapped.
732 * 1) Disable all AFU interrupts for each master
733 * 2) Unmap the problem state area
734 * 3) Stop each master context
736 for (k = cfg->afu->num_hwqs - 1; k >= 0; k--)
737 term_intr(cfg, UNMAP_THREE, k);
739 if (cfg->afu)
740 stop_afu(cfg);
742 for (k = cfg->afu->num_hwqs - 1; k >= 0; k--)
743 term_mc(cfg, k);
745 dev_dbg(dev, "%s: returning\n", __func__);
749 * notify_shutdown() - notifies device of pending shutdown
750 * @cfg: Internal structure associated with the host.
751 * @wait: Whether to wait for shutdown processing to complete.
753 * This function will notify the AFU that the adapter is being shutdown
754 * and will wait for shutdown processing to complete if wait is true.
755 * This notification should flush pending I/Os to the device and halt
756 * further I/Os until the next AFU reset is issued and device restarted.
758 static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
760 struct afu *afu = cfg->afu;
761 struct device *dev = &cfg->dev->dev;
762 struct dev_dependent_vals *ddv;
763 __be64 __iomem *fc_port_regs;
764 u64 reg, status;
765 int i, retry_cnt = 0;
767 ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
768 if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
769 return;
771 if (!afu || !afu->afu_map) {
772 dev_dbg(dev, "%s: Problem state area not mapped\n", __func__);
773 return;
776 /* Notify AFU */
777 for (i = 0; i < cfg->num_fc_ports; i++) {
778 fc_port_regs = get_fc_port_regs(cfg, i);
780 reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
781 reg |= SISL_FC_SHUTDOWN_NORMAL;
782 writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
785 if (!wait)
786 return;
788 /* Wait up to 1.5 seconds for shutdown processing to complete */
789 for (i = 0; i < cfg->num_fc_ports; i++) {
790 fc_port_regs = get_fc_port_regs(cfg, i);
791 retry_cnt = 0;
793 while (true) {
794 status = readq_be(&fc_port_regs[FC_STATUS / 8]);
795 if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
796 break;
797 if (++retry_cnt >= MC_RETRY_CNT) {
798 dev_dbg(dev, "%s: port %d shutdown processing "
799 "not yet completed\n", __func__, i);
800 break;
802 msleep(100 * retry_cnt);
808 * cxlflash_remove() - PCI entry point to tear down host
809 * @pdev: PCI device associated with the host.
811 * Safe to use as a cleanup in partially allocated/initialized state. Note that
812 * the reset_waitq is flushed as part of the stop/termination of user contexts.
814 static void cxlflash_remove(struct pci_dev *pdev)
816 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
817 struct device *dev = &pdev->dev;
818 ulong lock_flags;
820 if (!pci_is_enabled(pdev)) {
821 dev_dbg(dev, "%s: Device is disabled\n", __func__);
822 return;
825 /* If a Task Management Function is active, wait for it to complete
826 * before continuing with remove.
828 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
829 if (cfg->tmf_active)
830 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
831 !cfg->tmf_active,
832 cfg->tmf_slock);
833 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
835 /* Notify AFU and wait for shutdown processing to complete */
836 notify_shutdown(cfg, true);
838 cfg->state = STATE_FAILTERM;
839 cxlflash_stop_term_user_contexts(cfg);
841 switch (cfg->init_state) {
842 case INIT_STATE_SCSI:
843 cxlflash_term_local_luns(cfg);
844 scsi_remove_host(cfg->host);
845 case INIT_STATE_AFU:
846 term_afu(cfg);
847 case INIT_STATE_PCI:
848 pci_disable_device(pdev);
849 case INIT_STATE_NONE:
850 free_mem(cfg);
851 scsi_host_put(cfg->host);
852 break;
855 dev_dbg(dev, "%s: returning\n", __func__);
859 * alloc_mem() - allocates the AFU and its command pool
860 * @cfg: Internal structure associated with the host.
862 * A partially allocated state remains on failure.
864 * Return:
865 * 0 on success
866 * -ENOMEM on failure to allocate memory
868 static int alloc_mem(struct cxlflash_cfg *cfg)
870 int rc = 0;
871 struct device *dev = &cfg->dev->dev;
873 /* AFU is ~28k, i.e. only one 64k page or up to seven 4k pages */
874 cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
875 get_order(sizeof(struct afu)));
876 if (unlikely(!cfg->afu)) {
877 dev_err(dev, "%s: cannot get %d free pages\n",
878 __func__, get_order(sizeof(struct afu)));
879 rc = -ENOMEM;
880 goto out;
882 cfg->afu->parent = cfg;
883 cfg->afu->desired_hwqs = CXLFLASH_DEF_HWQS;
884 cfg->afu->afu_map = NULL;
885 out:
886 return rc;
890 * init_pci() - initializes the host as a PCI device
891 * @cfg: Internal structure associated with the host.
893 * Return: 0 on success, -errno on failure
895 static int init_pci(struct cxlflash_cfg *cfg)
897 struct pci_dev *pdev = cfg->dev;
898 struct device *dev = &cfg->dev->dev;
899 int rc = 0;
901 rc = pci_enable_device(pdev);
902 if (rc || pci_channel_offline(pdev)) {
903 if (pci_channel_offline(pdev)) {
904 cxlflash_wait_for_pci_err_recovery(cfg);
905 rc = pci_enable_device(pdev);
908 if (rc) {
909 dev_err(dev, "%s: Cannot enable adapter\n", __func__);
910 cxlflash_wait_for_pci_err_recovery(cfg);
911 goto out;
915 out:
916 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
917 return rc;
921 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
922 * @cfg: Internal structure associated with the host.
924 * Return: 0 on success, -errno on failure
926 static int init_scsi(struct cxlflash_cfg *cfg)
928 struct pci_dev *pdev = cfg->dev;
929 struct device *dev = &cfg->dev->dev;
930 int rc = 0;
932 rc = scsi_add_host(cfg->host, &pdev->dev);
933 if (rc) {
934 dev_err(dev, "%s: scsi_add_host failed rc=%d\n", __func__, rc);
935 goto out;
938 scsi_scan_host(cfg->host);
940 out:
941 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
942 return rc;
946 * set_port_online() - transitions the specified host FC port to online state
947 * @fc_regs: Top of MMIO region defined for specified port.
949 * The provided MMIO region must be mapped prior to call. Online state means
950 * that the FC link layer has synced, completed the handshaking process, and
951 * is ready for login to start.
953 static void set_port_online(__be64 __iomem *fc_regs)
955 u64 cmdcfg;
957 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
958 cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
959 cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
960 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
964 * set_port_offline() - transitions the specified host FC port to offline state
965 * @fc_regs: Top of MMIO region defined for specified port.
967 * The provided MMIO region must be mapped prior to call.
969 static void set_port_offline(__be64 __iomem *fc_regs)
971 u64 cmdcfg;
973 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
974 cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
975 cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
976 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
980 * wait_port_online() - waits for the specified host FC port come online
981 * @fc_regs: Top of MMIO region defined for specified port.
982 * @delay_us: Number of microseconds to delay between reading port status.
983 * @nretry: Number of cycles to retry reading port status.
985 * The provided MMIO region must be mapped prior to call. This will timeout
986 * when the cable is not plugged in.
988 * Return:
989 * TRUE (1) when the specified port is online
990 * FALSE (0) when the specified port fails to come online after timeout
992 static bool wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
994 u64 status;
996 WARN_ON(delay_us < 1000);
998 do {
999 msleep(delay_us / 1000);
1000 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
1001 if (status == U64_MAX)
1002 nretry /= 2;
1003 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
1004 nretry--);
1006 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
1010 * wait_port_offline() - waits for the specified host FC port go offline
1011 * @fc_regs: Top of MMIO region defined for specified port.
1012 * @delay_us: Number of microseconds to delay between reading port status.
1013 * @nretry: Number of cycles to retry reading port status.
1015 * The provided MMIO region must be mapped prior to call.
1017 * Return:
1018 * TRUE (1) when the specified port is offline
1019 * FALSE (0) when the specified port fails to go offline after timeout
1021 static bool wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
1023 u64 status;
1025 WARN_ON(delay_us < 1000);
1027 do {
1028 msleep(delay_us / 1000);
1029 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
1030 if (status == U64_MAX)
1031 nretry /= 2;
1032 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
1033 nretry--);
1035 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
1039 * afu_set_wwpn() - configures the WWPN for the specified host FC port
1040 * @afu: AFU associated with the host that owns the specified FC port.
1041 * @port: Port number being configured.
1042 * @fc_regs: Top of MMIO region defined for specified port.
1043 * @wwpn: The world-wide-port-number previously discovered for port.
1045 * The provided MMIO region must be mapped prior to call. As part of the
1046 * sequence to configure the WWPN, the port is toggled offline and then back
1047 * online. This toggling action can cause this routine to delay up to a few
1048 * seconds. When configured to use the internal LUN feature of the AFU, a
1049 * failure to come online is overridden.
1051 static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
1052 u64 wwpn)
1054 struct cxlflash_cfg *cfg = afu->parent;
1055 struct device *dev = &cfg->dev->dev;
1057 set_port_offline(fc_regs);
1058 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1059 FC_PORT_STATUS_RETRY_CNT)) {
1060 dev_dbg(dev, "%s: wait on port %d to go offline timed out\n",
1061 __func__, port);
1064 writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
1066 set_port_online(fc_regs);
1067 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1068 FC_PORT_STATUS_RETRY_CNT)) {
1069 dev_dbg(dev, "%s: wait on port %d to go online timed out\n",
1070 __func__, port);
1075 * afu_link_reset() - resets the specified host FC port
1076 * @afu: AFU associated with the host that owns the specified FC port.
1077 * @port: Port number being configured.
1078 * @fc_regs: Top of MMIO region defined for specified port.
1080 * The provided MMIO region must be mapped prior to call. The sequence to
1081 * reset the port involves toggling it offline and then back online. This
1082 * action can cause this routine to delay up to a few seconds. An effort
1083 * is made to maintain link with the device by switching to host to use
1084 * the alternate port exclusively while the reset takes place.
1085 * failure to come online is overridden.
1087 static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
1089 struct cxlflash_cfg *cfg = afu->parent;
1090 struct device *dev = &cfg->dev->dev;
1091 u64 port_sel;
1093 /* first switch the AFU to the other links, if any */
1094 port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
1095 port_sel &= ~(1ULL << port);
1096 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1097 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1099 set_port_offline(fc_regs);
1100 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1101 FC_PORT_STATUS_RETRY_CNT))
1102 dev_err(dev, "%s: wait on port %d to go offline timed out\n",
1103 __func__, port);
1105 set_port_online(fc_regs);
1106 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1107 FC_PORT_STATUS_RETRY_CNT))
1108 dev_err(dev, "%s: wait on port %d to go online timed out\n",
1109 __func__, port);
1111 /* switch back to include this port */
1112 port_sel |= (1ULL << port);
1113 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1114 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1116 dev_dbg(dev, "%s: returning port_sel=%016llx\n", __func__, port_sel);
1120 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1121 * @afu: AFU associated with the host.
1123 static void afu_err_intr_init(struct afu *afu)
1125 struct cxlflash_cfg *cfg = afu->parent;
1126 __be64 __iomem *fc_port_regs;
1127 int i;
1128 struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
1129 u64 reg;
1131 /* global async interrupts: AFU clears afu_ctrl on context exit
1132 * if async interrupts were sent to that context. This prevents
1133 * the AFU form sending further async interrupts when
1134 * there is
1135 * nobody to receive them.
1138 /* mask all */
1139 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
1140 /* set LISN# to send and point to primary master context */
1141 reg = ((u64) (((hwq->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
1143 if (afu->internal_lun)
1144 reg |= 1; /* Bit 63 indicates local lun */
1145 writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1146 /* clear all */
1147 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1148 /* unmask bits that are of interest */
1149 /* note: afu can send an interrupt after this step */
1150 writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1151 /* clear again in case a bit came on after previous clear but before */
1152 /* unmask */
1153 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1155 /* Clear/Set internal lun bits */
1156 fc_port_regs = get_fc_port_regs(cfg, 0);
1157 reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
1158 reg &= SISL_FC_INTERNAL_MASK;
1159 if (afu->internal_lun)
1160 reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
1161 writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
1163 /* now clear FC errors */
1164 for (i = 0; i < cfg->num_fc_ports; i++) {
1165 fc_port_regs = get_fc_port_regs(cfg, i);
1167 writeq_be(0xFFFFFFFFU, &fc_port_regs[FC_ERROR / 8]);
1168 writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
1171 /* sync interrupts for master's IOARRIN write */
1172 /* note that unlike asyncs, there can be no pending sync interrupts */
1173 /* at this time (this is a fresh context and master has not written */
1174 /* IOARRIN yet), so there is nothing to clear. */
1176 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1177 for (i = 0; i < afu->num_hwqs; i++) {
1178 hwq = get_hwq(afu, i);
1180 writeq_be(SISL_MSI_SYNC_ERROR, &hwq->host_map->ctx_ctrl);
1181 writeq_be(SISL_ISTATUS_MASK, &hwq->host_map->intr_mask);
1186 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1187 * @irq: Interrupt number.
1188 * @data: Private data provided at interrupt registration, the AFU.
1190 * Return: Always return IRQ_HANDLED.
1192 static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1194 struct hwq *hwq = (struct hwq *)data;
1195 struct cxlflash_cfg *cfg = hwq->afu->parent;
1196 struct device *dev = &cfg->dev->dev;
1197 u64 reg;
1198 u64 reg_unmasked;
1200 reg = readq_be(&hwq->host_map->intr_status);
1201 reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1203 if (reg_unmasked == 0UL) {
1204 dev_err(dev, "%s: spurious interrupt, intr_status=%016llx\n",
1205 __func__, reg);
1206 goto cxlflash_sync_err_irq_exit;
1209 dev_err(dev, "%s: unexpected interrupt, intr_status=%016llx\n",
1210 __func__, reg);
1212 writeq_be(reg_unmasked, &hwq->host_map->intr_clear);
1214 cxlflash_sync_err_irq_exit:
1215 return IRQ_HANDLED;
1219 * process_hrrq() - process the read-response queue
1220 * @afu: AFU associated with the host.
1221 * @doneq: Queue of commands harvested from the RRQ.
1222 * @budget: Threshold of RRQ entries to process.
1224 * This routine must be called holding the disabled RRQ spin lock.
1226 * Return: The number of entries processed.
1228 static int process_hrrq(struct hwq *hwq, struct list_head *doneq, int budget)
1230 struct afu *afu = hwq->afu;
1231 struct afu_cmd *cmd;
1232 struct sisl_ioasa *ioasa;
1233 struct sisl_ioarcb *ioarcb;
1234 bool toggle = hwq->toggle;
1235 int num_hrrq = 0;
1236 u64 entry,
1237 *hrrq_start = hwq->hrrq_start,
1238 *hrrq_end = hwq->hrrq_end,
1239 *hrrq_curr = hwq->hrrq_curr;
1241 /* Process ready RRQ entries up to the specified budget (if any) */
1242 while (true) {
1243 entry = *hrrq_curr;
1245 if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1246 break;
1248 entry &= ~SISL_RESP_HANDLE_T_BIT;
1250 if (afu_is_sq_cmd_mode(afu)) {
1251 ioasa = (struct sisl_ioasa *)entry;
1252 cmd = container_of(ioasa, struct afu_cmd, sa);
1253 } else {
1254 ioarcb = (struct sisl_ioarcb *)entry;
1255 cmd = container_of(ioarcb, struct afu_cmd, rcb);
1258 list_add_tail(&cmd->queue, doneq);
1260 /* Advance to next entry or wrap and flip the toggle bit */
1261 if (hrrq_curr < hrrq_end)
1262 hrrq_curr++;
1263 else {
1264 hrrq_curr = hrrq_start;
1265 toggle ^= SISL_RESP_HANDLE_T_BIT;
1268 atomic_inc(&hwq->hsq_credits);
1269 num_hrrq++;
1271 if (budget > 0 && num_hrrq >= budget)
1272 break;
1275 hwq->hrrq_curr = hrrq_curr;
1276 hwq->toggle = toggle;
1278 return num_hrrq;
1282 * process_cmd_doneq() - process a queue of harvested RRQ commands
1283 * @doneq: Queue of completed commands.
1285 * Note that upon return the queue can no longer be trusted.
1287 static void process_cmd_doneq(struct list_head *doneq)
1289 struct afu_cmd *cmd, *tmp;
1291 WARN_ON(list_empty(doneq));
1293 list_for_each_entry_safe(cmd, tmp, doneq, queue)
1294 cmd_complete(cmd);
1298 * cxlflash_irqpoll() - process a queue of harvested RRQ commands
1299 * @irqpoll: IRQ poll structure associated with queue to poll.
1300 * @budget: Threshold of RRQ entries to process per poll.
1302 * Return: The number of entries processed.
1304 static int cxlflash_irqpoll(struct irq_poll *irqpoll, int budget)
1306 struct hwq *hwq = container_of(irqpoll, struct hwq, irqpoll);
1307 unsigned long hrrq_flags;
1308 LIST_HEAD(doneq);
1309 int num_entries = 0;
1311 spin_lock_irqsave(&hwq->hrrq_slock, hrrq_flags);
1313 num_entries = process_hrrq(hwq, &doneq, budget);
1314 if (num_entries < budget)
1315 irq_poll_complete(irqpoll);
1317 spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
1319 process_cmd_doneq(&doneq);
1320 return num_entries;
1324 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1325 * @irq: Interrupt number.
1326 * @data: Private data provided at interrupt registration, the AFU.
1328 * Return: IRQ_HANDLED or IRQ_NONE when no ready entries found.
1330 static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
1332 struct hwq *hwq = (struct hwq *)data;
1333 struct afu *afu = hwq->afu;
1334 unsigned long hrrq_flags;
1335 LIST_HEAD(doneq);
1336 int num_entries = 0;
1338 spin_lock_irqsave(&hwq->hrrq_slock, hrrq_flags);
1340 if (afu_is_irqpoll_enabled(afu)) {
1341 irq_poll_sched(&hwq->irqpoll);
1342 spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
1343 return IRQ_HANDLED;
1346 num_entries = process_hrrq(hwq, &doneq, -1);
1347 spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
1349 if (num_entries == 0)
1350 return IRQ_NONE;
1352 process_cmd_doneq(&doneq);
1353 return IRQ_HANDLED;
1357 * Asynchronous interrupt information table
1359 * NOTE:
1360 * - Order matters here as this array is indexed by bit position.
1362 * - The checkpatch script considers the BUILD_SISL_ASTATUS_FC_PORT macro
1363 * as complex and complains due to a lack of parentheses/braces.
1365 #define ASTATUS_FC(_a, _b, _c, _d) \
1366 { SISL_ASTATUS_FC##_a##_##_b, _c, _a, (_d) }
1368 #define BUILD_SISL_ASTATUS_FC_PORT(_a) \
1369 ASTATUS_FC(_a, LINK_UP, "link up", 0), \
1370 ASTATUS_FC(_a, LINK_DN, "link down", 0), \
1371 ASTATUS_FC(_a, LOGI_S, "login succeeded", SCAN_HOST), \
1372 ASTATUS_FC(_a, LOGI_F, "login failed", CLR_FC_ERROR), \
1373 ASTATUS_FC(_a, LOGI_R, "login timed out, retrying", LINK_RESET), \
1374 ASTATUS_FC(_a, CRC_T, "CRC threshold exceeded", LINK_RESET), \
1375 ASTATUS_FC(_a, LOGO, "target initiated LOGO", 0), \
1376 ASTATUS_FC(_a, OTHER, "other error", CLR_FC_ERROR | LINK_RESET)
1378 static const struct asyc_intr_info ainfo[] = {
1379 BUILD_SISL_ASTATUS_FC_PORT(1),
1380 BUILD_SISL_ASTATUS_FC_PORT(0),
1381 BUILD_SISL_ASTATUS_FC_PORT(3),
1382 BUILD_SISL_ASTATUS_FC_PORT(2)
1386 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1387 * @irq: Interrupt number.
1388 * @data: Private data provided at interrupt registration, the AFU.
1390 * Return: Always return IRQ_HANDLED.
1392 static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1394 struct hwq *hwq = (struct hwq *)data;
1395 struct afu *afu = hwq->afu;
1396 struct cxlflash_cfg *cfg = afu->parent;
1397 struct device *dev = &cfg->dev->dev;
1398 const struct asyc_intr_info *info;
1399 struct sisl_global_map __iomem *global = &afu->afu_map->global;
1400 __be64 __iomem *fc_port_regs;
1401 u64 reg_unmasked;
1402 u64 reg;
1403 u64 bit;
1404 u8 port;
1406 reg = readq_be(&global->regs.aintr_status);
1407 reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1409 if (unlikely(reg_unmasked == 0)) {
1410 dev_err(dev, "%s: spurious interrupt, aintr_status=%016llx\n",
1411 __func__, reg);
1412 goto out;
1415 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
1416 writeq_be(reg_unmasked, &global->regs.aintr_clear);
1418 /* Check each bit that is on */
1419 for_each_set_bit(bit, (ulong *)&reg_unmasked, BITS_PER_LONG) {
1420 if (unlikely(bit >= ARRAY_SIZE(ainfo))) {
1421 WARN_ON_ONCE(1);
1422 continue;
1425 info = &ainfo[bit];
1426 if (unlikely(info->status != 1ULL << bit)) {
1427 WARN_ON_ONCE(1);
1428 continue;
1431 port = info->port;
1432 fc_port_regs = get_fc_port_regs(cfg, port);
1434 dev_err(dev, "%s: FC Port %d -> %s, fc_status=%016llx\n",
1435 __func__, port, info->desc,
1436 readq_be(&fc_port_regs[FC_STATUS / 8]));
1439 * Do link reset first, some OTHER errors will set FC_ERROR
1440 * again if cleared before or w/o a reset
1442 if (info->action & LINK_RESET) {
1443 dev_err(dev, "%s: FC Port %d: resetting link\n",
1444 __func__, port);
1445 cfg->lr_state = LINK_RESET_REQUIRED;
1446 cfg->lr_port = port;
1447 schedule_work(&cfg->work_q);
1450 if (info->action & CLR_FC_ERROR) {
1451 reg = readq_be(&fc_port_regs[FC_ERROR / 8]);
1454 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
1455 * should be the same and tracing one is sufficient.
1458 dev_err(dev, "%s: fc %d: clearing fc_error=%016llx\n",
1459 __func__, port, reg);
1461 writeq_be(reg, &fc_port_regs[FC_ERROR / 8]);
1462 writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
1465 if (info->action & SCAN_HOST) {
1466 atomic_inc(&cfg->scan_host_needed);
1467 schedule_work(&cfg->work_q);
1471 out:
1472 return IRQ_HANDLED;
1476 * start_context() - starts the master context
1477 * @cfg: Internal structure associated with the host.
1478 * @index: Index of the hardware queue.
1480 * Return: A success or failure value from CXL services.
1482 static int start_context(struct cxlflash_cfg *cfg, u32 index)
1484 struct device *dev = &cfg->dev->dev;
1485 struct hwq *hwq = get_hwq(cfg->afu, index);
1486 int rc = 0;
1488 rc = cxl_start_context(hwq->ctx,
1489 hwq->work.work_element_descriptor,
1490 NULL);
1492 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
1493 return rc;
1497 * read_vpd() - obtains the WWPNs from VPD
1498 * @cfg: Internal structure associated with the host.
1499 * @wwpn: Array of size MAX_FC_PORTS to pass back WWPNs
1501 * Return: 0 on success, -errno on failure
1503 static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1505 struct device *dev = &cfg->dev->dev;
1506 struct pci_dev *pdev = cfg->dev;
1507 int rc = 0;
1508 int ro_start, ro_size, i, j, k;
1509 ssize_t vpd_size;
1510 char vpd_data[CXLFLASH_VPD_LEN];
1511 char tmp_buf[WWPN_BUF_LEN] = { 0 };
1512 char *wwpn_vpd_tags[MAX_FC_PORTS] = { "V5", "V6", "V7", "V8" };
1514 /* Get the VPD data from the device */
1515 vpd_size = cxl_read_adapter_vpd(pdev, vpd_data, sizeof(vpd_data));
1516 if (unlikely(vpd_size <= 0)) {
1517 dev_err(dev, "%s: Unable to read VPD (size = %ld)\n",
1518 __func__, vpd_size);
1519 rc = -ENODEV;
1520 goto out;
1523 /* Get the read only section offset */
1524 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
1525 PCI_VPD_LRDT_RO_DATA);
1526 if (unlikely(ro_start < 0)) {
1527 dev_err(dev, "%s: VPD Read-only data not found\n", __func__);
1528 rc = -ENODEV;
1529 goto out;
1532 /* Get the read only section size, cap when extends beyond read VPD */
1533 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1534 j = ro_size;
1535 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1536 if (unlikely((i + j) > vpd_size)) {
1537 dev_dbg(dev, "%s: Might need to read more VPD (%d > %ld)\n",
1538 __func__, (i + j), vpd_size);
1539 ro_size = vpd_size - i;
1543 * Find the offset of the WWPN tag within the read only
1544 * VPD data and validate the found field (partials are
1545 * no good to us). Convert the ASCII data to an integer
1546 * value. Note that we must copy to a temporary buffer
1547 * because the conversion service requires that the ASCII
1548 * string be terminated.
1550 for (k = 0; k < cfg->num_fc_ports; k++) {
1551 j = ro_size;
1552 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1554 i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
1555 if (unlikely(i < 0)) {
1556 dev_err(dev, "%s: Port %d WWPN not found in VPD\n",
1557 __func__, k);
1558 rc = -ENODEV;
1559 goto out;
1562 j = pci_vpd_info_field_size(&vpd_data[i]);
1563 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1564 if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
1565 dev_err(dev, "%s: Port %d WWPN incomplete or bad VPD\n",
1566 __func__, k);
1567 rc = -ENODEV;
1568 goto out;
1571 memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1572 rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1573 if (unlikely(rc)) {
1574 dev_err(dev, "%s: WWPN conversion failed for port %d\n",
1575 __func__, k);
1576 rc = -ENODEV;
1577 goto out;
1580 dev_dbg(dev, "%s: wwpn%d=%016llx\n", __func__, k, wwpn[k]);
1583 out:
1584 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
1585 return rc;
1589 * init_pcr() - initialize the provisioning and control registers
1590 * @cfg: Internal structure associated with the host.
1592 * Also sets up fast access to the mapped registers and initializes AFU
1593 * command fields that never change.
1595 static void init_pcr(struct cxlflash_cfg *cfg)
1597 struct afu *afu = cfg->afu;
1598 struct sisl_ctrl_map __iomem *ctrl_map;
1599 struct hwq *hwq;
1600 int i;
1602 for (i = 0; i < MAX_CONTEXT; i++) {
1603 ctrl_map = &afu->afu_map->ctrls[i].ctrl;
1604 /* Disrupt any clients that could be running */
1605 /* e.g. clients that survived a master restart */
1606 writeq_be(0, &ctrl_map->rht_start);
1607 writeq_be(0, &ctrl_map->rht_cnt_id);
1608 writeq_be(0, &ctrl_map->ctx_cap);
1611 /* Copy frequently used fields into hwq */
1612 for (i = 0; i < afu->num_hwqs; i++) {
1613 hwq = get_hwq(afu, i);
1615 hwq->ctx_hndl = (u16) cxl_process_element(hwq->ctx);
1616 hwq->host_map = &afu->afu_map->hosts[hwq->ctx_hndl].host;
1617 hwq->ctrl_map = &afu->afu_map->ctrls[hwq->ctx_hndl].ctrl;
1619 /* Program the Endian Control for the master context */
1620 writeq_be(SISL_ENDIAN_CTRL, &hwq->host_map->endian_ctrl);
1625 * init_global() - initialize AFU global registers
1626 * @cfg: Internal structure associated with the host.
1628 static int init_global(struct cxlflash_cfg *cfg)
1630 struct afu *afu = cfg->afu;
1631 struct device *dev = &cfg->dev->dev;
1632 struct hwq *hwq;
1633 struct sisl_host_map __iomem *hmap;
1634 __be64 __iomem *fc_port_regs;
1635 u64 wwpn[MAX_FC_PORTS]; /* wwpn of AFU ports */
1636 int i = 0, num_ports = 0;
1637 int rc = 0;
1638 u64 reg;
1640 rc = read_vpd(cfg, &wwpn[0]);
1641 if (rc) {
1642 dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
1643 goto out;
1646 /* Set up RRQ and SQ in HWQ for master issued cmds */
1647 for (i = 0; i < afu->num_hwqs; i++) {
1648 hwq = get_hwq(afu, i);
1649 hmap = hwq->host_map;
1651 writeq_be((u64) hwq->hrrq_start, &hmap->rrq_start);
1652 writeq_be((u64) hwq->hrrq_end, &hmap->rrq_end);
1654 if (afu_is_sq_cmd_mode(afu)) {
1655 writeq_be((u64)hwq->hsq_start, &hmap->sq_start);
1656 writeq_be((u64)hwq->hsq_end, &hmap->sq_end);
1660 /* AFU configuration */
1661 reg = readq_be(&afu->afu_map->global.regs.afu_config);
1662 reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1663 /* enable all auto retry options and control endianness */
1664 /* leave others at default: */
1665 /* CTX_CAP write protected, mbox_r does not clear on read and */
1666 /* checker on if dual afu */
1667 writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1669 /* Global port select: select either port */
1670 if (afu->internal_lun) {
1671 /* Only use port 0 */
1672 writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
1673 num_ports = 0;
1674 } else {
1675 writeq_be(PORT_MASK(cfg->num_fc_ports),
1676 &afu->afu_map->global.regs.afu_port_sel);
1677 num_ports = cfg->num_fc_ports;
1680 for (i = 0; i < num_ports; i++) {
1681 fc_port_regs = get_fc_port_regs(cfg, i);
1683 /* Unmask all errors (but they are still masked at AFU) */
1684 writeq_be(0, &fc_port_regs[FC_ERRMSK / 8]);
1685 /* Clear CRC error cnt & set a threshold */
1686 (void)readq_be(&fc_port_regs[FC_CNT_CRCERR / 8]);
1687 writeq_be(MC_CRC_THRESH, &fc_port_regs[FC_CRC_THRESH / 8]);
1689 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
1690 if (wwpn[i] != 0)
1691 afu_set_wwpn(afu, i, &fc_port_regs[0], wwpn[i]);
1692 /* Programming WWPN back to back causes additional
1693 * offline/online transitions and a PLOGI
1695 msleep(100);
1698 /* Set up master's own CTX_CAP to allow real mode, host translation */
1699 /* tables, afu cmds and read/write GSCSI cmds. */
1700 /* First, unlock ctx_cap write by reading mbox */
1701 for (i = 0; i < afu->num_hwqs; i++) {
1702 hwq = get_hwq(afu, i);
1704 (void)readq_be(&hwq->ctrl_map->mbox_r); /* unlock ctx_cap */
1705 writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1706 SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1707 SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1708 &hwq->ctrl_map->ctx_cap);
1710 /* Initialize heartbeat */
1711 afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
1712 out:
1713 return rc;
1717 * start_afu() - initializes and starts the AFU
1718 * @cfg: Internal structure associated with the host.
1720 static int start_afu(struct cxlflash_cfg *cfg)
1722 struct afu *afu = cfg->afu;
1723 struct device *dev = &cfg->dev->dev;
1724 struct hwq *hwq;
1725 int rc = 0;
1726 int i;
1728 init_pcr(cfg);
1730 /* Initialize each HWQ */
1731 for (i = 0; i < afu->num_hwqs; i++) {
1732 hwq = get_hwq(afu, i);
1734 /* After an AFU reset, RRQ entries are stale, clear them */
1735 memset(&hwq->rrq_entry, 0, sizeof(hwq->rrq_entry));
1737 /* Initialize RRQ pointers */
1738 hwq->hrrq_start = &hwq->rrq_entry[0];
1739 hwq->hrrq_end = &hwq->rrq_entry[NUM_RRQ_ENTRY - 1];
1740 hwq->hrrq_curr = hwq->hrrq_start;
1741 hwq->toggle = 1;
1743 /* Initialize spin locks */
1744 spin_lock_init(&hwq->hrrq_slock);
1745 spin_lock_init(&hwq->hsq_slock);
1747 /* Initialize SQ */
1748 if (afu_is_sq_cmd_mode(afu)) {
1749 memset(&hwq->sq, 0, sizeof(hwq->sq));
1750 hwq->hsq_start = &hwq->sq[0];
1751 hwq->hsq_end = &hwq->sq[NUM_SQ_ENTRY - 1];
1752 hwq->hsq_curr = hwq->hsq_start;
1754 atomic_set(&hwq->hsq_credits, NUM_SQ_ENTRY - 1);
1757 /* Initialize IRQ poll */
1758 if (afu_is_irqpoll_enabled(afu))
1759 irq_poll_init(&hwq->irqpoll, afu->irqpoll_weight,
1760 cxlflash_irqpoll);
1764 rc = init_global(cfg);
1766 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
1767 return rc;
1771 * init_intr() - setup interrupt handlers for the master context
1772 * @cfg: Internal structure associated with the host.
1773 * @hwq: Hardware queue to initialize.
1775 * Return: 0 on success, -errno on failure
1777 static enum undo_level init_intr(struct cxlflash_cfg *cfg,
1778 struct hwq *hwq)
1780 struct device *dev = &cfg->dev->dev;
1781 struct cxl_context *ctx = hwq->ctx;
1782 int rc = 0;
1783 enum undo_level level = UNDO_NOOP;
1784 bool is_primary_hwq = (hwq->index == PRIMARY_HWQ);
1785 int num_irqs = is_primary_hwq ? 3 : 2;
1787 rc = cxl_allocate_afu_irqs(ctx, num_irqs);
1788 if (unlikely(rc)) {
1789 dev_err(dev, "%s: allocate_afu_irqs failed rc=%d\n",
1790 __func__, rc);
1791 level = UNDO_NOOP;
1792 goto out;
1795 rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, hwq,
1796 "SISL_MSI_SYNC_ERROR");
1797 if (unlikely(rc <= 0)) {
1798 dev_err(dev, "%s: SISL_MSI_SYNC_ERROR map failed\n", __func__);
1799 level = FREE_IRQ;
1800 goto out;
1803 rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, hwq,
1804 "SISL_MSI_RRQ_UPDATED");
1805 if (unlikely(rc <= 0)) {
1806 dev_err(dev, "%s: SISL_MSI_RRQ_UPDATED map failed\n", __func__);
1807 level = UNMAP_ONE;
1808 goto out;
1811 /* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
1812 if (!is_primary_hwq)
1813 goto out;
1815 rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, hwq,
1816 "SISL_MSI_ASYNC_ERROR");
1817 if (unlikely(rc <= 0)) {
1818 dev_err(dev, "%s: SISL_MSI_ASYNC_ERROR map failed\n", __func__);
1819 level = UNMAP_TWO;
1820 goto out;
1822 out:
1823 return level;
1827 * init_mc() - create and register as the master context
1828 * @cfg: Internal structure associated with the host.
1829 * index: HWQ Index of the master context.
1831 * Return: 0 on success, -errno on failure
1833 static int init_mc(struct cxlflash_cfg *cfg, u32 index)
1835 struct cxl_context *ctx;
1836 struct device *dev = &cfg->dev->dev;
1837 struct hwq *hwq = get_hwq(cfg->afu, index);
1838 int rc = 0;
1839 enum undo_level level;
1841 hwq->afu = cfg->afu;
1842 hwq->index = index;
1844 if (index == PRIMARY_HWQ)
1845 ctx = cxl_get_context(cfg->dev);
1846 else
1847 ctx = cxl_dev_context_init(cfg->dev);
1848 if (unlikely(!ctx)) {
1849 rc = -ENOMEM;
1850 goto err1;
1853 WARN_ON(hwq->ctx);
1854 hwq->ctx = ctx;
1856 /* Set it up as a master with the CXL */
1857 cxl_set_master(ctx);
1859 /* Reset AFU when initializing primary context */
1860 if (index == PRIMARY_HWQ) {
1861 rc = cxl_afu_reset(ctx);
1862 if (unlikely(rc)) {
1863 dev_err(dev, "%s: AFU reset failed rc=%d\n",
1864 __func__, rc);
1865 goto err1;
1869 level = init_intr(cfg, hwq);
1870 if (unlikely(level)) {
1871 dev_err(dev, "%s: interrupt init failed rc=%d\n", __func__, rc);
1872 goto err2;
1875 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1876 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1877 * element (pe) that is embedded in the context (ctx)
1879 rc = start_context(cfg, index);
1880 if (unlikely(rc)) {
1881 dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
1882 level = UNMAP_THREE;
1883 goto err2;
1886 out:
1887 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
1888 return rc;
1889 err2:
1890 term_intr(cfg, level, index);
1891 if (index != PRIMARY_HWQ)
1892 cxl_release_context(ctx);
1893 err1:
1894 hwq->ctx = NULL;
1895 goto out;
1899 * get_num_afu_ports() - determines and configures the number of AFU ports
1900 * @cfg: Internal structure associated with the host.
1902 * This routine determines the number of AFU ports by converting the global
1903 * port selection mask. The converted value is only valid following an AFU
1904 * reset (explicit or power-on). This routine must be invoked shortly after
1905 * mapping as other routines are dependent on the number of ports during the
1906 * initialization sequence.
1908 * To support legacy AFUs that might not have reflected an initial global
1909 * port mask (value read is 0), default to the number of ports originally
1910 * supported by the cxlflash driver (2) before hardware with other port
1911 * offerings was introduced.
1913 static void get_num_afu_ports(struct cxlflash_cfg *cfg)
1915 struct afu *afu = cfg->afu;
1916 struct device *dev = &cfg->dev->dev;
1917 u64 port_mask;
1918 int num_fc_ports = LEGACY_FC_PORTS;
1920 port_mask = readq_be(&afu->afu_map->global.regs.afu_port_sel);
1921 if (port_mask != 0ULL)
1922 num_fc_ports = min(ilog2(port_mask) + 1, MAX_FC_PORTS);
1924 dev_dbg(dev, "%s: port_mask=%016llx num_fc_ports=%d\n",
1925 __func__, port_mask, num_fc_ports);
1927 cfg->num_fc_ports = num_fc_ports;
1928 cfg->host->max_channel = PORTNUM2CHAN(num_fc_ports);
1932 * init_afu() - setup as master context and start AFU
1933 * @cfg: Internal structure associated with the host.
1935 * This routine is a higher level of control for configuring the
1936 * AFU on probe and reset paths.
1938 * Return: 0 on success, -errno on failure
1940 static int init_afu(struct cxlflash_cfg *cfg)
1942 u64 reg;
1943 int rc = 0;
1944 struct afu *afu = cfg->afu;
1945 struct device *dev = &cfg->dev->dev;
1946 struct hwq *hwq;
1947 int i;
1949 cxl_perst_reloads_same_image(cfg->cxl_afu, true);
1951 afu->num_hwqs = afu->desired_hwqs;
1952 for (i = 0; i < afu->num_hwqs; i++) {
1953 rc = init_mc(cfg, i);
1954 if (rc) {
1955 dev_err(dev, "%s: init_mc failed rc=%d index=%d\n",
1956 __func__, rc, i);
1957 goto err1;
1961 /* Map the entire MMIO space of the AFU using the first context */
1962 hwq = get_hwq(afu, PRIMARY_HWQ);
1963 afu->afu_map = cxl_psa_map(hwq->ctx);
1964 if (!afu->afu_map) {
1965 dev_err(dev, "%s: cxl_psa_map failed\n", __func__);
1966 rc = -ENOMEM;
1967 goto err1;
1970 /* No byte reverse on reading afu_version or string will be backwards */
1971 reg = readq(&afu->afu_map->global.regs.afu_version);
1972 memcpy(afu->version, &reg, sizeof(reg));
1973 afu->interface_version =
1974 readq_be(&afu->afu_map->global.regs.interface_version);
1975 if ((afu->interface_version + 1) == 0) {
1976 dev_err(dev, "Back level AFU, please upgrade. AFU version %s "
1977 "interface version %016llx\n", afu->version,
1978 afu->interface_version);
1979 rc = -EINVAL;
1980 goto err1;
1983 if (afu_is_sq_cmd_mode(afu)) {
1984 afu->send_cmd = send_cmd_sq;
1985 afu->context_reset = context_reset_sq;
1986 } else {
1987 afu->send_cmd = send_cmd_ioarrin;
1988 afu->context_reset = context_reset_ioarrin;
1991 dev_dbg(dev, "%s: afu_ver=%s interface_ver=%016llx\n", __func__,
1992 afu->version, afu->interface_version);
1994 get_num_afu_ports(cfg);
1996 rc = start_afu(cfg);
1997 if (rc) {
1998 dev_err(dev, "%s: start_afu failed, rc=%d\n", __func__, rc);
1999 goto err1;
2002 afu_err_intr_init(cfg->afu);
2003 for (i = 0; i < afu->num_hwqs; i++) {
2004 hwq = get_hwq(afu, i);
2006 hwq->room = readq_be(&hwq->host_map->cmd_room);
2009 /* Restore the LUN mappings */
2010 cxlflash_restore_luntable(cfg);
2011 out:
2012 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
2013 return rc;
2015 err1:
2016 for (i = afu->num_hwqs - 1; i >= 0; i--) {
2017 term_intr(cfg, UNMAP_THREE, i);
2018 term_mc(cfg, i);
2020 goto out;
2024 * afu_reset() - resets the AFU
2025 * @cfg: Internal structure associated with the host.
2027 * Return: 0 on success, -errno on failure
2029 static int afu_reset(struct cxlflash_cfg *cfg)
2031 struct device *dev = &cfg->dev->dev;
2032 int rc = 0;
2034 /* Stop the context before the reset. Since the context is
2035 * no longer available restart it after the reset is complete
2037 term_afu(cfg);
2039 rc = init_afu(cfg);
2041 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
2042 return rc;
2046 * drain_ioctls() - wait until all currently executing ioctls have completed
2047 * @cfg: Internal structure associated with the host.
2049 * Obtain write access to read/write semaphore that wraps ioctl
2050 * handling to 'drain' ioctls currently executing.
2052 static void drain_ioctls(struct cxlflash_cfg *cfg)
2054 down_write(&cfg->ioctl_rwsem);
2055 up_write(&cfg->ioctl_rwsem);
2059 * cxlflash_async_reset_host() - asynchronous host reset handler
2060 * @data: Private data provided while scheduling reset.
2061 * @cookie: Cookie that can be used for checkpointing.
2063 static void cxlflash_async_reset_host(void *data, async_cookie_t cookie)
2065 struct cxlflash_cfg *cfg = data;
2066 struct device *dev = &cfg->dev->dev;
2067 int rc = 0;
2069 if (cfg->state != STATE_RESET) {
2070 dev_dbg(dev, "%s: Not performing a reset, state=%d\n",
2071 __func__, cfg->state);
2072 goto out;
2075 drain_ioctls(cfg);
2076 cxlflash_mark_contexts_error(cfg);
2077 rc = afu_reset(cfg);
2078 if (rc)
2079 cfg->state = STATE_FAILTERM;
2080 else
2081 cfg->state = STATE_NORMAL;
2082 wake_up_all(&cfg->reset_waitq);
2084 out:
2085 scsi_unblock_requests(cfg->host);
2089 * cxlflash_schedule_async_reset() - schedule an asynchronous host reset
2090 * @cfg: Internal structure associated with the host.
2092 static void cxlflash_schedule_async_reset(struct cxlflash_cfg *cfg)
2094 struct device *dev = &cfg->dev->dev;
2096 if (cfg->state != STATE_NORMAL) {
2097 dev_dbg(dev, "%s: Not performing reset state=%d\n",
2098 __func__, cfg->state);
2099 return;
2102 cfg->state = STATE_RESET;
2103 scsi_block_requests(cfg->host);
2104 cfg->async_reset_cookie = async_schedule(cxlflash_async_reset_host,
2105 cfg);
2109 * cxlflash_afu_sync() - builds and sends an AFU sync command
2110 * @afu: AFU associated with the host.
2111 * @ctx_hndl_u: Identifies context requesting sync.
2112 * @res_hndl_u: Identifies resource requesting sync.
2113 * @mode: Type of sync to issue (lightweight, heavyweight, global).
2115 * The AFU can only take 1 sync command at a time. This routine enforces this
2116 * limitation by using a mutex to provide exclusive access to the AFU during
2117 * the sync. This design point requires calling threads to not be on interrupt
2118 * context due to the possibility of sleeping during concurrent sync operations.
2120 * AFU sync operations are only necessary and allowed when the device is
2121 * operating normally. When not operating normally, sync requests can occur as
2122 * part of cleaning up resources associated with an adapter prior to removal.
2123 * In this scenario, these requests are simply ignored (safe due to the AFU
2124 * going away).
2126 * Return:
2127 * 0 on success, -errno on failure
2129 int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
2130 res_hndl_t res_hndl_u, u8 mode)
2132 struct cxlflash_cfg *cfg = afu->parent;
2133 struct device *dev = &cfg->dev->dev;
2134 struct afu_cmd *cmd = NULL;
2135 struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
2136 char *buf = NULL;
2137 int rc = 0;
2138 int nretry = 0;
2139 static DEFINE_MUTEX(sync_active);
2141 if (cfg->state != STATE_NORMAL) {
2142 dev_dbg(dev, "%s: Sync not required state=%u\n",
2143 __func__, cfg->state);
2144 return 0;
2147 mutex_lock(&sync_active);
2148 atomic_inc(&afu->cmds_active);
2149 buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
2150 if (unlikely(!buf)) {
2151 dev_err(dev, "%s: no memory for command\n", __func__);
2152 rc = -ENOMEM;
2153 goto out;
2156 cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
2158 retry:
2159 init_completion(&cmd->cevent);
2160 cmd->parent = afu;
2161 cmd->hwq_index = hwq->index;
2163 dev_dbg(dev, "%s: afu=%p cmd=%p ctx=%d nretry=%d\n",
2164 __func__, afu, cmd, ctx_hndl_u, nretry);
2166 cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
2167 cmd->rcb.ctx_id = hwq->ctx_hndl;
2168 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
2169 cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
2171 cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
2172 cmd->rcb.cdb[1] = mode;
2174 /* The cdb is aligned, no unaligned accessors required */
2175 *((__be16 *)&cmd->rcb.cdb[2]) = cpu_to_be16(ctx_hndl_u);
2176 *((__be32 *)&cmd->rcb.cdb[4]) = cpu_to_be32(res_hndl_u);
2178 rc = afu->send_cmd(afu, cmd);
2179 if (unlikely(rc)) {
2180 rc = -ENOBUFS;
2181 goto out;
2184 rc = wait_resp(afu, cmd);
2185 if (rc == -ETIMEDOUT) {
2186 rc = afu->context_reset(hwq);
2187 if (!rc && ++nretry < 2)
2188 goto retry;
2189 cxlflash_schedule_async_reset(cfg);
2192 out:
2193 atomic_dec(&afu->cmds_active);
2194 mutex_unlock(&sync_active);
2195 kfree(buf);
2196 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
2197 return rc;
2201 * cxlflash_eh_device_reset_handler() - reset a single LUN
2202 * @scp: SCSI command to send.
2204 * Return:
2205 * SUCCESS as defined in scsi/scsi.h
2206 * FAILED as defined in scsi/scsi.h
2208 static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
2210 int rc = SUCCESS;
2211 struct Scsi_Host *host = scp->device->host;
2212 struct cxlflash_cfg *cfg = shost_priv(host);
2213 struct device *dev = &cfg->dev->dev;
2214 struct afu *afu = cfg->afu;
2215 int rcr = 0;
2217 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
2218 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
2219 scp->device->channel, scp->device->id, scp->device->lun,
2220 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
2221 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
2222 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
2223 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
2225 retry:
2226 switch (cfg->state) {
2227 case STATE_NORMAL:
2228 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
2229 if (unlikely(rcr))
2230 rc = FAILED;
2231 break;
2232 case STATE_RESET:
2233 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2234 goto retry;
2235 default:
2236 rc = FAILED;
2237 break;
2240 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
2241 return rc;
2245 * cxlflash_eh_host_reset_handler() - reset the host adapter
2246 * @scp: SCSI command from stack identifying host.
2248 * Following a reset, the state is evaluated again in case an EEH occurred
2249 * during the reset. In such a scenario, the host reset will either yield
2250 * until the EEH recovery is complete or return success or failure based
2251 * upon the current device state.
2253 * Return:
2254 * SUCCESS as defined in scsi/scsi.h
2255 * FAILED as defined in scsi/scsi.h
2257 static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
2259 int rc = SUCCESS;
2260 int rcr = 0;
2261 struct Scsi_Host *host = scp->device->host;
2262 struct cxlflash_cfg *cfg = shost_priv(host);
2263 struct device *dev = &cfg->dev->dev;
2265 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
2266 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
2267 scp->device->channel, scp->device->id, scp->device->lun,
2268 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
2269 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
2270 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
2271 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
2273 switch (cfg->state) {
2274 case STATE_NORMAL:
2275 cfg->state = STATE_RESET;
2276 drain_ioctls(cfg);
2277 cxlflash_mark_contexts_error(cfg);
2278 rcr = afu_reset(cfg);
2279 if (rcr) {
2280 rc = FAILED;
2281 cfg->state = STATE_FAILTERM;
2282 } else
2283 cfg->state = STATE_NORMAL;
2284 wake_up_all(&cfg->reset_waitq);
2285 ssleep(1);
2286 /* fall through */
2287 case STATE_RESET:
2288 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2289 if (cfg->state == STATE_NORMAL)
2290 break;
2291 /* fall through */
2292 default:
2293 rc = FAILED;
2294 break;
2297 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
2298 return rc;
2302 * cxlflash_change_queue_depth() - change the queue depth for the device
2303 * @sdev: SCSI device destined for queue depth change.
2304 * @qdepth: Requested queue depth value to set.
2306 * The requested queue depth is capped to the maximum supported value.
2308 * Return: The actual queue depth set.
2310 static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
2313 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
2314 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
2316 scsi_change_queue_depth(sdev, qdepth);
2317 return sdev->queue_depth;
2321 * cxlflash_show_port_status() - queries and presents the current port status
2322 * @port: Desired port for status reporting.
2323 * @cfg: Internal structure associated with the host.
2324 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2326 * Return: The size of the ASCII string returned in @buf or -EINVAL.
2328 static ssize_t cxlflash_show_port_status(u32 port,
2329 struct cxlflash_cfg *cfg,
2330 char *buf)
2332 struct device *dev = &cfg->dev->dev;
2333 char *disp_status;
2334 u64 status;
2335 __be64 __iomem *fc_port_regs;
2337 WARN_ON(port >= MAX_FC_PORTS);
2339 if (port >= cfg->num_fc_ports) {
2340 dev_info(dev, "%s: Port %d not supported on this card.\n",
2341 __func__, port);
2342 return -EINVAL;
2345 fc_port_regs = get_fc_port_regs(cfg, port);
2346 status = readq_be(&fc_port_regs[FC_MTIP_STATUS / 8]);
2347 status &= FC_MTIP_STATUS_MASK;
2349 if (status == FC_MTIP_STATUS_ONLINE)
2350 disp_status = "online";
2351 else if (status == FC_MTIP_STATUS_OFFLINE)
2352 disp_status = "offline";
2353 else
2354 disp_status = "unknown";
2356 return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
2360 * port0_show() - queries and presents the current status of port 0
2361 * @dev: Generic device associated with the host owning the port.
2362 * @attr: Device attribute representing the port.
2363 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2365 * Return: The size of the ASCII string returned in @buf.
2367 static ssize_t port0_show(struct device *dev,
2368 struct device_attribute *attr,
2369 char *buf)
2371 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2373 return cxlflash_show_port_status(0, cfg, buf);
2377 * port1_show() - queries and presents the current status of port 1
2378 * @dev: Generic device associated with the host owning the port.
2379 * @attr: Device attribute representing the port.
2380 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2382 * Return: The size of the ASCII string returned in @buf.
2384 static ssize_t port1_show(struct device *dev,
2385 struct device_attribute *attr,
2386 char *buf)
2388 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2390 return cxlflash_show_port_status(1, cfg, buf);
2394 * port2_show() - queries and presents the current status of port 2
2395 * @dev: Generic device associated with the host owning the port.
2396 * @attr: Device attribute representing the port.
2397 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2399 * Return: The size of the ASCII string returned in @buf.
2401 static ssize_t port2_show(struct device *dev,
2402 struct device_attribute *attr,
2403 char *buf)
2405 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2407 return cxlflash_show_port_status(2, cfg, buf);
2411 * port3_show() - queries and presents the current status of port 3
2412 * @dev: Generic device associated with the host owning the port.
2413 * @attr: Device attribute representing the port.
2414 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2416 * Return: The size of the ASCII string returned in @buf.
2418 static ssize_t port3_show(struct device *dev,
2419 struct device_attribute *attr,
2420 char *buf)
2422 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2424 return cxlflash_show_port_status(3, cfg, buf);
2428 * lun_mode_show() - presents the current LUN mode of the host
2429 * @dev: Generic device associated with the host.
2430 * @attr: Device attribute representing the LUN mode.
2431 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2433 * Return: The size of the ASCII string returned in @buf.
2435 static ssize_t lun_mode_show(struct device *dev,
2436 struct device_attribute *attr, char *buf)
2438 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2439 struct afu *afu = cfg->afu;
2441 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
2445 * lun_mode_store() - sets the LUN mode of the host
2446 * @dev: Generic device associated with the host.
2447 * @attr: Device attribute representing the LUN mode.
2448 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2449 * @count: Length of data resizing in @buf.
2451 * The CXL Flash AFU supports a dummy LUN mode where the external
2452 * links and storage are not required. Space on the FPGA is used
2453 * to create 1 or 2 small LUNs which are presented to the system
2454 * as if they were a normal storage device. This feature is useful
2455 * during development and also provides manufacturing with a way
2456 * to test the AFU without an actual device.
2458 * 0 = external LUN[s] (default)
2459 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2460 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2461 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2462 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2464 * Return: The size of the ASCII string returned in @buf.
2466 static ssize_t lun_mode_store(struct device *dev,
2467 struct device_attribute *attr,
2468 const char *buf, size_t count)
2470 struct Scsi_Host *shost = class_to_shost(dev);
2471 struct cxlflash_cfg *cfg = shost_priv(shost);
2472 struct afu *afu = cfg->afu;
2473 int rc;
2474 u32 lun_mode;
2476 rc = kstrtouint(buf, 10, &lun_mode);
2477 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
2478 afu->internal_lun = lun_mode;
2481 * When configured for internal LUN, there is only one channel,
2482 * channel number 0, else there will be one less than the number
2483 * of fc ports for this card.
2485 if (afu->internal_lun)
2486 shost->max_channel = 0;
2487 else
2488 shost->max_channel = PORTNUM2CHAN(cfg->num_fc_ports);
2490 afu_reset(cfg);
2491 scsi_scan_host(cfg->host);
2494 return count;
2498 * ioctl_version_show() - presents the current ioctl version of the host
2499 * @dev: Generic device associated with the host.
2500 * @attr: Device attribute representing the ioctl version.
2501 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2503 * Return: The size of the ASCII string returned in @buf.
2505 static ssize_t ioctl_version_show(struct device *dev,
2506 struct device_attribute *attr, char *buf)
2508 return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
2512 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2513 * @port: Desired port for status reporting.
2514 * @cfg: Internal structure associated with the host.
2515 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2517 * Return: The size of the ASCII string returned in @buf or -EINVAL.
2519 static ssize_t cxlflash_show_port_lun_table(u32 port,
2520 struct cxlflash_cfg *cfg,
2521 char *buf)
2523 struct device *dev = &cfg->dev->dev;
2524 __be64 __iomem *fc_port_luns;
2525 int i;
2526 ssize_t bytes = 0;
2528 WARN_ON(port >= MAX_FC_PORTS);
2530 if (port >= cfg->num_fc_ports) {
2531 dev_info(dev, "%s: Port %d not supported on this card.\n",
2532 __func__, port);
2533 return -EINVAL;
2536 fc_port_luns = get_fc_port_luns(cfg, port);
2538 for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2539 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
2540 "%03d: %016llx\n",
2541 i, readq_be(&fc_port_luns[i]));
2542 return bytes;
2546 * port0_lun_table_show() - presents the current LUN table of port 0
2547 * @dev: Generic device associated with the host owning the port.
2548 * @attr: Device attribute representing the port.
2549 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2551 * Return: The size of the ASCII string returned in @buf.
2553 static ssize_t port0_lun_table_show(struct device *dev,
2554 struct device_attribute *attr,
2555 char *buf)
2557 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2559 return cxlflash_show_port_lun_table(0, cfg, buf);
2563 * port1_lun_table_show() - presents the current LUN table of port 1
2564 * @dev: Generic device associated with the host owning the port.
2565 * @attr: Device attribute representing the port.
2566 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2568 * Return: The size of the ASCII string returned in @buf.
2570 static ssize_t port1_lun_table_show(struct device *dev,
2571 struct device_attribute *attr,
2572 char *buf)
2574 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2576 return cxlflash_show_port_lun_table(1, cfg, buf);
2580 * port2_lun_table_show() - presents the current LUN table of port 2
2581 * @dev: Generic device associated with the host owning the port.
2582 * @attr: Device attribute representing the port.
2583 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2585 * Return: The size of the ASCII string returned in @buf.
2587 static ssize_t port2_lun_table_show(struct device *dev,
2588 struct device_attribute *attr,
2589 char *buf)
2591 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2593 return cxlflash_show_port_lun_table(2, cfg, buf);
2597 * port3_lun_table_show() - presents the current LUN table of port 3
2598 * @dev: Generic device associated with the host owning the port.
2599 * @attr: Device attribute representing the port.
2600 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2602 * Return: The size of the ASCII string returned in @buf.
2604 static ssize_t port3_lun_table_show(struct device *dev,
2605 struct device_attribute *attr,
2606 char *buf)
2608 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2610 return cxlflash_show_port_lun_table(3, cfg, buf);
2614 * irqpoll_weight_show() - presents the current IRQ poll weight for the host
2615 * @dev: Generic device associated with the host.
2616 * @attr: Device attribute representing the IRQ poll weight.
2617 * @buf: Buffer of length PAGE_SIZE to report back the current IRQ poll
2618 * weight in ASCII.
2620 * An IRQ poll weight of 0 indicates polling is disabled.
2622 * Return: The size of the ASCII string returned in @buf.
2624 static ssize_t irqpoll_weight_show(struct device *dev,
2625 struct device_attribute *attr, char *buf)
2627 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2628 struct afu *afu = cfg->afu;
2630 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->irqpoll_weight);
2634 * irqpoll_weight_store() - sets the current IRQ poll weight for the host
2635 * @dev: Generic device associated with the host.
2636 * @attr: Device attribute representing the IRQ poll weight.
2637 * @buf: Buffer of length PAGE_SIZE containing the desired IRQ poll
2638 * weight in ASCII.
2639 * @count: Length of data resizing in @buf.
2641 * An IRQ poll weight of 0 indicates polling is disabled.
2643 * Return: The size of the ASCII string returned in @buf.
2645 static ssize_t irqpoll_weight_store(struct device *dev,
2646 struct device_attribute *attr,
2647 const char *buf, size_t count)
2649 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2650 struct device *cfgdev = &cfg->dev->dev;
2651 struct afu *afu = cfg->afu;
2652 struct hwq *hwq;
2653 u32 weight;
2654 int rc, i;
2656 rc = kstrtouint(buf, 10, &weight);
2657 if (rc)
2658 return -EINVAL;
2660 if (weight > 256) {
2661 dev_info(cfgdev,
2662 "Invalid IRQ poll weight. It must be 256 or less.\n");
2663 return -EINVAL;
2666 if (weight == afu->irqpoll_weight) {
2667 dev_info(cfgdev,
2668 "Current IRQ poll weight has the same weight.\n");
2669 return -EINVAL;
2672 if (afu_is_irqpoll_enabled(afu)) {
2673 for (i = 0; i < afu->num_hwqs; i++) {
2674 hwq = get_hwq(afu, i);
2676 irq_poll_disable(&hwq->irqpoll);
2680 afu->irqpoll_weight = weight;
2682 if (weight > 0) {
2683 for (i = 0; i < afu->num_hwqs; i++) {
2684 hwq = get_hwq(afu, i);
2686 irq_poll_init(&hwq->irqpoll, weight, cxlflash_irqpoll);
2690 return count;
2694 * num_hwqs_show() - presents the number of hardware queues for the host
2695 * @dev: Generic device associated with the host.
2696 * @attr: Device attribute representing the number of hardware queues.
2697 * @buf: Buffer of length PAGE_SIZE to report back the number of hardware
2698 * queues in ASCII.
2700 * Return: The size of the ASCII string returned in @buf.
2702 static ssize_t num_hwqs_show(struct device *dev,
2703 struct device_attribute *attr, char *buf)
2705 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2706 struct afu *afu = cfg->afu;
2708 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->num_hwqs);
2712 * num_hwqs_store() - sets the number of hardware queues for the host
2713 * @dev: Generic device associated with the host.
2714 * @attr: Device attribute representing the number of hardware queues.
2715 * @buf: Buffer of length PAGE_SIZE containing the number of hardware
2716 * queues in ASCII.
2717 * @count: Length of data resizing in @buf.
2719 * n > 0: num_hwqs = n
2720 * n = 0: num_hwqs = num_online_cpus()
2721 * n < 0: num_online_cpus() / abs(n)
2723 * Return: The size of the ASCII string returned in @buf.
2725 static ssize_t num_hwqs_store(struct device *dev,
2726 struct device_attribute *attr,
2727 const char *buf, size_t count)
2729 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2730 struct afu *afu = cfg->afu;
2731 int rc;
2732 int nhwqs, num_hwqs;
2734 rc = kstrtoint(buf, 10, &nhwqs);
2735 if (rc)
2736 return -EINVAL;
2738 if (nhwqs >= 1)
2739 num_hwqs = nhwqs;
2740 else if (nhwqs == 0)
2741 num_hwqs = num_online_cpus();
2742 else
2743 num_hwqs = num_online_cpus() / abs(nhwqs);
2745 afu->desired_hwqs = min(num_hwqs, CXLFLASH_MAX_HWQS);
2746 WARN_ON_ONCE(afu->desired_hwqs == 0);
2748 retry:
2749 switch (cfg->state) {
2750 case STATE_NORMAL:
2751 cfg->state = STATE_RESET;
2752 drain_ioctls(cfg);
2753 cxlflash_mark_contexts_error(cfg);
2754 rc = afu_reset(cfg);
2755 if (rc)
2756 cfg->state = STATE_FAILTERM;
2757 else
2758 cfg->state = STATE_NORMAL;
2759 wake_up_all(&cfg->reset_waitq);
2760 break;
2761 case STATE_RESET:
2762 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2763 if (cfg->state == STATE_NORMAL)
2764 goto retry;
2765 default:
2766 /* Ideally should not happen */
2767 dev_err(dev, "%s: Device is not ready, state=%d\n",
2768 __func__, cfg->state);
2769 break;
2772 return count;
2775 static const char *hwq_mode_name[MAX_HWQ_MODE] = { "rr", "tag", "cpu" };
2778 * hwq_mode_show() - presents the HWQ steering mode for the host
2779 * @dev: Generic device associated with the host.
2780 * @attr: Device attribute representing the HWQ steering mode.
2781 * @buf: Buffer of length PAGE_SIZE to report back the HWQ steering mode
2782 * as a character string.
2784 * Return: The size of the ASCII string returned in @buf.
2786 static ssize_t hwq_mode_show(struct device *dev,
2787 struct device_attribute *attr, char *buf)
2789 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2790 struct afu *afu = cfg->afu;
2792 return scnprintf(buf, PAGE_SIZE, "%s\n", hwq_mode_name[afu->hwq_mode]);
2796 * hwq_mode_store() - sets the HWQ steering mode for the host
2797 * @dev: Generic device associated with the host.
2798 * @attr: Device attribute representing the HWQ steering mode.
2799 * @buf: Buffer of length PAGE_SIZE containing the HWQ steering mode
2800 * as a character string.
2801 * @count: Length of data resizing in @buf.
2803 * rr = Round-Robin
2804 * tag = Block MQ Tagging
2805 * cpu = CPU Affinity
2807 * Return: The size of the ASCII string returned in @buf.
2809 static ssize_t hwq_mode_store(struct device *dev,
2810 struct device_attribute *attr,
2811 const char *buf, size_t count)
2813 struct Scsi_Host *shost = class_to_shost(dev);
2814 struct cxlflash_cfg *cfg = shost_priv(shost);
2815 struct device *cfgdev = &cfg->dev->dev;
2816 struct afu *afu = cfg->afu;
2817 int i;
2818 u32 mode = MAX_HWQ_MODE;
2820 for (i = 0; i < MAX_HWQ_MODE; i++) {
2821 if (!strncmp(hwq_mode_name[i], buf, strlen(hwq_mode_name[i]))) {
2822 mode = i;
2823 break;
2827 if (mode >= MAX_HWQ_MODE) {
2828 dev_info(cfgdev, "Invalid HWQ steering mode.\n");
2829 return -EINVAL;
2832 if ((mode == HWQ_MODE_TAG) && !shost_use_blk_mq(shost)) {
2833 dev_info(cfgdev, "SCSI-MQ is not enabled, use a different "
2834 "HWQ steering mode.\n");
2835 return -EINVAL;
2838 afu->hwq_mode = mode;
2840 return count;
2844 * mode_show() - presents the current mode of the device
2845 * @dev: Generic device associated with the device.
2846 * @attr: Device attribute representing the device mode.
2847 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2849 * Return: The size of the ASCII string returned in @buf.
2851 static ssize_t mode_show(struct device *dev,
2852 struct device_attribute *attr, char *buf)
2854 struct scsi_device *sdev = to_scsi_device(dev);
2856 return scnprintf(buf, PAGE_SIZE, "%s\n",
2857 sdev->hostdata ? "superpipe" : "legacy");
2861 * Host attributes
2863 static DEVICE_ATTR_RO(port0);
2864 static DEVICE_ATTR_RO(port1);
2865 static DEVICE_ATTR_RO(port2);
2866 static DEVICE_ATTR_RO(port3);
2867 static DEVICE_ATTR_RW(lun_mode);
2868 static DEVICE_ATTR_RO(ioctl_version);
2869 static DEVICE_ATTR_RO(port0_lun_table);
2870 static DEVICE_ATTR_RO(port1_lun_table);
2871 static DEVICE_ATTR_RO(port2_lun_table);
2872 static DEVICE_ATTR_RO(port3_lun_table);
2873 static DEVICE_ATTR_RW(irqpoll_weight);
2874 static DEVICE_ATTR_RW(num_hwqs);
2875 static DEVICE_ATTR_RW(hwq_mode);
2877 static struct device_attribute *cxlflash_host_attrs[] = {
2878 &dev_attr_port0,
2879 &dev_attr_port1,
2880 &dev_attr_port2,
2881 &dev_attr_port3,
2882 &dev_attr_lun_mode,
2883 &dev_attr_ioctl_version,
2884 &dev_attr_port0_lun_table,
2885 &dev_attr_port1_lun_table,
2886 &dev_attr_port2_lun_table,
2887 &dev_attr_port3_lun_table,
2888 &dev_attr_irqpoll_weight,
2889 &dev_attr_num_hwqs,
2890 &dev_attr_hwq_mode,
2891 NULL
2895 * Device attributes
2897 static DEVICE_ATTR_RO(mode);
2899 static struct device_attribute *cxlflash_dev_attrs[] = {
2900 &dev_attr_mode,
2901 NULL
2905 * Host template
2907 static struct scsi_host_template driver_template = {
2908 .module = THIS_MODULE,
2909 .name = CXLFLASH_ADAPTER_NAME,
2910 .info = cxlflash_driver_info,
2911 .ioctl = cxlflash_ioctl,
2912 .proc_name = CXLFLASH_NAME,
2913 .queuecommand = cxlflash_queuecommand,
2914 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
2915 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
2916 .change_queue_depth = cxlflash_change_queue_depth,
2917 .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
2918 .can_queue = CXLFLASH_MAX_CMDS,
2919 .cmd_size = sizeof(struct afu_cmd) + __alignof__(struct afu_cmd) - 1,
2920 .this_id = -1,
2921 .sg_tablesize = 1, /* No scatter gather support */
2922 .max_sectors = CXLFLASH_MAX_SECTORS,
2923 .use_clustering = ENABLE_CLUSTERING,
2924 .shost_attrs = cxlflash_host_attrs,
2925 .sdev_attrs = cxlflash_dev_attrs,
2929 * Device dependent values
2931 static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
2932 0ULL };
2933 static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
2934 CXLFLASH_NOTIFY_SHUTDOWN };
2935 static struct dev_dependent_vals dev_briard_vals = { CXLFLASH_MAX_SECTORS,
2936 CXLFLASH_NOTIFY_SHUTDOWN };
2939 * PCI device binding table
2941 static struct pci_device_id cxlflash_pci_table[] = {
2942 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
2943 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
2944 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
2945 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
2946 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_BRIARD,
2947 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_briard_vals},
2951 MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
2954 * cxlflash_worker_thread() - work thread handler for the AFU
2955 * @work: Work structure contained within cxlflash associated with host.
2957 * Handles the following events:
2958 * - Link reset which cannot be performed on interrupt context due to
2959 * blocking up to a few seconds
2960 * - Rescan the host
2962 static void cxlflash_worker_thread(struct work_struct *work)
2964 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
2965 work_q);
2966 struct afu *afu = cfg->afu;
2967 struct device *dev = &cfg->dev->dev;
2968 __be64 __iomem *fc_port_regs;
2969 int port;
2970 ulong lock_flags;
2972 /* Avoid MMIO if the device has failed */
2974 if (cfg->state != STATE_NORMAL)
2975 return;
2977 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2979 if (cfg->lr_state == LINK_RESET_REQUIRED) {
2980 port = cfg->lr_port;
2981 if (port < 0)
2982 dev_err(dev, "%s: invalid port index %d\n",
2983 __func__, port);
2984 else {
2985 spin_unlock_irqrestore(cfg->host->host_lock,
2986 lock_flags);
2988 /* The reset can block... */
2989 fc_port_regs = get_fc_port_regs(cfg, port);
2990 afu_link_reset(afu, port, fc_port_regs);
2991 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2994 cfg->lr_state = LINK_RESET_COMPLETE;
2997 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
2999 if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
3000 scsi_scan_host(cfg->host);
3004 * cxlflash_probe() - PCI entry point to add host
3005 * @pdev: PCI device associated with the host.
3006 * @dev_id: PCI device id associated with device.
3008 * The device will initially start out in a 'probing' state and
3009 * transition to the 'normal' state at the end of a successful
3010 * probe. Should an EEH event occur during probe, the notification
3011 * thread (error_detected()) will wait until the probe handler
3012 * is nearly complete. At that time, the device will be moved to
3013 * a 'probed' state and the EEH thread woken up to drive the slot
3014 * reset and recovery (device moves to 'normal' state). Meanwhile,
3015 * the probe will be allowed to exit successfully.
3017 * Return: 0 on success, -errno on failure
3019 static int cxlflash_probe(struct pci_dev *pdev,
3020 const struct pci_device_id *dev_id)
3022 struct Scsi_Host *host;
3023 struct cxlflash_cfg *cfg = NULL;
3024 struct device *dev = &pdev->dev;
3025 struct dev_dependent_vals *ddv;
3026 int rc = 0;
3027 int k;
3029 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
3030 __func__, pdev->irq);
3032 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
3033 driver_template.max_sectors = ddv->max_sectors;
3035 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
3036 if (!host) {
3037 dev_err(dev, "%s: scsi_host_alloc failed\n", __func__);
3038 rc = -ENOMEM;
3039 goto out;
3042 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
3043 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
3044 host->unique_id = host->host_no;
3045 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
3047 cfg = shost_priv(host);
3048 cfg->host = host;
3049 rc = alloc_mem(cfg);
3050 if (rc) {
3051 dev_err(dev, "%s: alloc_mem failed\n", __func__);
3052 rc = -ENOMEM;
3053 scsi_host_put(cfg->host);
3054 goto out;
3057 cfg->init_state = INIT_STATE_NONE;
3058 cfg->dev = pdev;
3059 cfg->cxl_fops = cxlflash_cxl_fops;
3062 * Promoted LUNs move to the top of the LUN table. The rest stay on
3063 * the bottom half. The bottom half grows from the end (index = 255),
3064 * whereas the top half grows from the beginning (index = 0).
3066 * Initialize the last LUN index for all possible ports.
3068 cfg->promote_lun_index = 0;
3070 for (k = 0; k < MAX_FC_PORTS; k++)
3071 cfg->last_lun_index[k] = CXLFLASH_NUM_VLUNS/2 - 1;
3073 cfg->dev_id = (struct pci_device_id *)dev_id;
3075 init_waitqueue_head(&cfg->tmf_waitq);
3076 init_waitqueue_head(&cfg->reset_waitq);
3078 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
3079 cfg->lr_state = LINK_RESET_INVALID;
3080 cfg->lr_port = -1;
3081 spin_lock_init(&cfg->tmf_slock);
3082 mutex_init(&cfg->ctx_tbl_list_mutex);
3083 mutex_init(&cfg->ctx_recovery_mutex);
3084 init_rwsem(&cfg->ioctl_rwsem);
3085 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
3086 INIT_LIST_HEAD(&cfg->lluns);
3088 pci_set_drvdata(pdev, cfg);
3090 cfg->cxl_afu = cxl_pci_to_afu(pdev);
3092 rc = init_pci(cfg);
3093 if (rc) {
3094 dev_err(dev, "%s: init_pci failed rc=%d\n", __func__, rc);
3095 goto out_remove;
3097 cfg->init_state = INIT_STATE_PCI;
3099 rc = init_afu(cfg);
3100 if (rc && !wq_has_sleeper(&cfg->reset_waitq)) {
3101 dev_err(dev, "%s: init_afu failed rc=%d\n", __func__, rc);
3102 goto out_remove;
3104 cfg->init_state = INIT_STATE_AFU;
3106 rc = init_scsi(cfg);
3107 if (rc) {
3108 dev_err(dev, "%s: init_scsi failed rc=%d\n", __func__, rc);
3109 goto out_remove;
3111 cfg->init_state = INIT_STATE_SCSI;
3113 if (wq_has_sleeper(&cfg->reset_waitq)) {
3114 cfg->state = STATE_PROBED;
3115 wake_up_all(&cfg->reset_waitq);
3116 } else
3117 cfg->state = STATE_NORMAL;
3118 out:
3119 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
3120 return rc;
3122 out_remove:
3123 cxlflash_remove(pdev);
3124 goto out;
3128 * cxlflash_pci_error_detected() - called when a PCI error is detected
3129 * @pdev: PCI device struct.
3130 * @state: PCI channel state.
3132 * When an EEH occurs during an active reset, wait until the reset is
3133 * complete and then take action based upon the device state.
3135 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
3137 static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
3138 pci_channel_state_t state)
3140 int rc = 0;
3141 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
3142 struct device *dev = &cfg->dev->dev;
3144 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
3146 switch (state) {
3147 case pci_channel_io_frozen:
3148 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET &&
3149 cfg->state != STATE_PROBING);
3150 if (cfg->state == STATE_FAILTERM)
3151 return PCI_ERS_RESULT_DISCONNECT;
3153 cfg->state = STATE_RESET;
3154 scsi_block_requests(cfg->host);
3155 drain_ioctls(cfg);
3156 rc = cxlflash_mark_contexts_error(cfg);
3157 if (unlikely(rc))
3158 dev_err(dev, "%s: Failed to mark user contexts rc=%d\n",
3159 __func__, rc);
3160 term_afu(cfg);
3161 return PCI_ERS_RESULT_NEED_RESET;
3162 case pci_channel_io_perm_failure:
3163 cfg->state = STATE_FAILTERM;
3164 wake_up_all(&cfg->reset_waitq);
3165 scsi_unblock_requests(cfg->host);
3166 return PCI_ERS_RESULT_DISCONNECT;
3167 default:
3168 break;
3170 return PCI_ERS_RESULT_NEED_RESET;
3174 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
3175 * @pdev: PCI device struct.
3177 * This routine is called by the pci error recovery code after the PCI
3178 * slot has been reset, just before we should resume normal operations.
3180 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
3182 static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
3184 int rc = 0;
3185 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
3186 struct device *dev = &cfg->dev->dev;
3188 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
3190 rc = init_afu(cfg);
3191 if (unlikely(rc)) {
3192 dev_err(dev, "%s: EEH recovery failed rc=%d\n", __func__, rc);
3193 return PCI_ERS_RESULT_DISCONNECT;
3196 return PCI_ERS_RESULT_RECOVERED;
3200 * cxlflash_pci_resume() - called when normal operation can resume
3201 * @pdev: PCI device struct
3203 static void cxlflash_pci_resume(struct pci_dev *pdev)
3205 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
3206 struct device *dev = &cfg->dev->dev;
3208 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
3210 cfg->state = STATE_NORMAL;
3211 wake_up_all(&cfg->reset_waitq);
3212 scsi_unblock_requests(cfg->host);
3215 static const struct pci_error_handlers cxlflash_err_handler = {
3216 .error_detected = cxlflash_pci_error_detected,
3217 .slot_reset = cxlflash_pci_slot_reset,
3218 .resume = cxlflash_pci_resume,
3222 * PCI device structure
3224 static struct pci_driver cxlflash_driver = {
3225 .name = CXLFLASH_NAME,
3226 .id_table = cxlflash_pci_table,
3227 .probe = cxlflash_probe,
3228 .remove = cxlflash_remove,
3229 .shutdown = cxlflash_remove,
3230 .err_handler = &cxlflash_err_handler,
3234 * init_cxlflash() - module entry point
3236 * Return: 0 on success, -errno on failure
3238 static int __init init_cxlflash(void)
3240 check_sizes();
3241 cxlflash_list_init();
3243 return pci_register_driver(&cxlflash_driver);
3247 * exit_cxlflash() - module exit point
3249 static void __exit exit_cxlflash(void)
3251 cxlflash_term_global_luns();
3252 cxlflash_free_errpage();
3254 pci_unregister_driver(&cxlflash_driver);
3257 module_init(init_cxlflash);
3258 module_exit(exit_cxlflash);