]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/scsi/cxlflash/main.c
scsi: cxlflash: Remove AFU command lock
[mirror_ubuntu-zesty-kernel.git] / drivers / scsi / cxlflash / main.c
1 /*
2 * CXL Flash Device Driver
3 *
4 * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
5 * Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
6 *
7 * Copyright (C) 2015 IBM Corporation
8 *
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.
13 */
14
15 #include <linux/delay.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/pci.h>
19
20 #include <asm/unaligned.h>
21
22 #include <misc/cxl.h>
23
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_host.h>
26 #include <uapi/scsi/cxlflash_ioctl.h>
27
28 #include "main.h"
29 #include "sislite.h"
30 #include "common.h"
31
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");
36
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.
41 *
42 * Translates error bits from AFU command to SCSI command results.
43 */
44 static void process_cmd_err(struct afu_cmd *cmd, struct scsi_cmnd *scp)
45 {
46 struct sisl_ioarcb *ioarcb;
47 struct sisl_ioasa *ioasa;
48 u32 resid;
49
50 if (unlikely(!cmd))
51 return;
52
53 ioarcb = &(cmd->rcb);
54 ioasa = &(cmd->sa);
55
56 if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) {
57 resid = ioasa->resid;
58 scsi_set_resid(scp, resid);
59 pr_debug("%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
60 __func__, cmd, scp, resid);
61 }
62
63 if (ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN) {
64 pr_debug("%s: cmd underrun cmd = %p scp = %p\n",
65 __func__, cmd, scp);
66 scp->result = (DID_ERROR << 16);
67 }
68
69 pr_debug("%s: cmd failed afu_rc=%d scsi_rc=%d fc_rc=%d "
70 "afu_extra=0x%X, scsi_extra=0x%X, fc_extra=0x%X\n",
71 __func__, ioasa->rc.afu_rc, ioasa->rc.scsi_rc,
72 ioasa->rc.fc_rc, ioasa->afu_extra, ioasa->scsi_extra,
73 ioasa->fc_extra);
74
75 if (ioasa->rc.scsi_rc) {
76 /* We have a SCSI status */
77 if (ioasa->rc.flags & SISL_RC_FLAGS_SENSE_VALID) {
78 memcpy(scp->sense_buffer, ioasa->sense_data,
79 SISL_SENSE_DATA_LEN);
80 scp->result = ioasa->rc.scsi_rc;
81 } else
82 scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16);
83 }
84
85 /*
86 * We encountered an error. Set scp->result based on nature
87 * of error.
88 */
89 if (ioasa->rc.fc_rc) {
90 /* We have an FC status */
91 switch (ioasa->rc.fc_rc) {
92 case SISL_FC_RC_LINKDOWN:
93 scp->result = (DID_REQUEUE << 16);
94 break;
95 case SISL_FC_RC_RESID:
96 /* This indicates an FCP resid underrun */
97 if (!(ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN)) {
98 /* If the SISL_RC_FLAGS_OVERRUN flag was set,
99 * then we will handle this error else where.
100 * If not then we must handle it here.
101 * This is probably an AFU bug.
102 */
103 scp->result = (DID_ERROR << 16);
104 }
105 break;
106 case SISL_FC_RC_RESIDERR:
107 /* Resid mismatch between adapter and device */
108 case SISL_FC_RC_TGTABORT:
109 case SISL_FC_RC_ABORTOK:
110 case SISL_FC_RC_ABORTFAIL:
111 case SISL_FC_RC_NOLOGI:
112 case SISL_FC_RC_ABORTPEND:
113 case SISL_FC_RC_WRABORTPEND:
114 case SISL_FC_RC_NOEXP:
115 case SISL_FC_RC_INUSE:
116 scp->result = (DID_ERROR << 16);
117 break;
118 }
119 }
120
121 if (ioasa->rc.afu_rc) {
122 /* We have an AFU error */
123 switch (ioasa->rc.afu_rc) {
124 case SISL_AFU_RC_NO_CHANNELS:
125 scp->result = (DID_NO_CONNECT << 16);
126 break;
127 case SISL_AFU_RC_DATA_DMA_ERR:
128 switch (ioasa->afu_extra) {
129 case SISL_AFU_DMA_ERR_PAGE_IN:
130 /* Retry */
131 scp->result = (DID_IMM_RETRY << 16);
132 break;
133 case SISL_AFU_DMA_ERR_INVALID_EA:
134 default:
135 scp->result = (DID_ERROR << 16);
136 }
137 break;
138 case SISL_AFU_RC_OUT_OF_DATA_BUFS:
139 /* Retry */
140 scp->result = (DID_ALLOC_FAILURE << 16);
141 break;
142 default:
143 scp->result = (DID_ERROR << 16);
144 }
145 }
146 }
147
148 /**
149 * cmd_complete() - command completion handler
150 * @cmd: AFU command that has completed.
151 *
152 * Prepares and submits command that has either completed or timed out to
153 * the SCSI stack. Checks AFU command back into command pool for non-internal
154 * (rcb.scp populated) commands.
155 */
156 static void cmd_complete(struct afu_cmd *cmd)
157 {
158 struct scsi_cmnd *scp;
159 ulong lock_flags;
160 struct afu *afu = cmd->parent;
161 struct cxlflash_cfg *cfg = afu->parent;
162 bool cmd_is_tmf;
163
164 if (cmd->rcb.scp) {
165 scp = cmd->rcb.scp;
166 if (unlikely(cmd->sa.ioasc))
167 process_cmd_err(cmd, scp);
168 else
169 scp->result = (DID_OK << 16);
170
171 cmd_is_tmf = cmd->cmd_tmf;
172
173 pr_debug_ratelimited("%s: calling scsi_done scp=%p result=%X "
174 "ioasc=%d\n", __func__, scp, scp->result,
175 cmd->sa.ioasc);
176
177 scsi_dma_unmap(scp);
178 scp->scsi_done(scp);
179
180 if (cmd_is_tmf) {
181 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
182 cfg->tmf_active = false;
183 wake_up_all_locked(&cfg->tmf_waitq);
184 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
185 }
186 } else
187 complete(&cmd->cevent);
188 }
189
190 /**
191 * context_reset() - timeout handler for AFU commands
192 * @cmd: AFU command that timed out.
193 *
194 * Sends a reset to the AFU.
195 */
196 static void context_reset(struct afu_cmd *cmd)
197 {
198 int nretry = 0;
199 u64 rrin = 0x1;
200 struct afu *afu = cmd->parent;
201 struct cxlflash_cfg *cfg = afu->parent;
202 struct device *dev = &cfg->dev->dev;
203
204 pr_debug("%s: cmd=%p\n", __func__, cmd);
205
206 writeq_be(rrin, &afu->host_map->ioarrin);
207 do {
208 rrin = readq_be(&afu->host_map->ioarrin);
209 if (rrin != 0x1)
210 break;
211 /* Double delay each time */
212 udelay(1 << nretry);
213 } while (nretry++ < MC_ROOM_RETRY_CNT);
214
215 dev_dbg(dev, "%s: returning rrin=0x%016llX nretry=%d\n",
216 __func__, rrin, nretry);
217 }
218
219 /**
220 * send_cmd() - sends an AFU command
221 * @afu: AFU associated with the host.
222 * @cmd: AFU command to send.
223 *
224 * Return:
225 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
226 */
227 static int send_cmd(struct afu *afu, struct afu_cmd *cmd)
228 {
229 struct cxlflash_cfg *cfg = afu->parent;
230 struct device *dev = &cfg->dev->dev;
231 int rc = 0;
232 s64 room;
233 ulong lock_flags;
234
235 /*
236 * To avoid the performance penalty of MMIO, spread the update of
237 * 'room' over multiple commands.
238 */
239 spin_lock_irqsave(&afu->rrin_slock, lock_flags);
240 if (--afu->room < 0) {
241 room = readq_be(&afu->host_map->cmd_room);
242 if (room <= 0) {
243 dev_dbg_ratelimited(dev, "%s: no cmd_room to send "
244 "0x%02X, room=0x%016llX\n",
245 __func__, cmd->rcb.cdb[0], room);
246 afu->room = 0;
247 rc = SCSI_MLQUEUE_HOST_BUSY;
248 goto out;
249 }
250 afu->room = room - 1;
251 }
252
253 writeq_be((u64)&cmd->rcb, &afu->host_map->ioarrin);
254 out:
255 spin_unlock_irqrestore(&afu->rrin_slock, lock_flags);
256 pr_devel("%s: cmd=%p len=%d ea=%p rc=%d\n", __func__, cmd,
257 cmd->rcb.data_len, (void *)cmd->rcb.data_ea, rc);
258 return rc;
259 }
260
261 /**
262 * wait_resp() - polls for a response or timeout to a sent AFU command
263 * @afu: AFU associated with the host.
264 * @cmd: AFU command that was sent.
265 *
266 * Return:
267 * 0 on success, -1 on timeout/error
268 */
269 static int wait_resp(struct afu *afu, struct afu_cmd *cmd)
270 {
271 int rc = 0;
272 ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
273
274 timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
275 if (!timeout) {
276 context_reset(cmd);
277 rc = -1;
278 }
279
280 if (unlikely(cmd->sa.ioasc != 0)) {
281 pr_err("%s: CMD 0x%X failed, IOASC: flags 0x%X, afu_rc 0x%X, "
282 "scsi_rc 0x%X, fc_rc 0x%X\n", __func__, cmd->rcb.cdb[0],
283 cmd->sa.rc.flags, cmd->sa.rc.afu_rc, cmd->sa.rc.scsi_rc,
284 cmd->sa.rc.fc_rc);
285 rc = -1;
286 }
287
288 return rc;
289 }
290
291 /**
292 * send_tmf() - sends a Task Management Function (TMF)
293 * @afu: AFU to checkout from.
294 * @scp: SCSI command from stack.
295 * @tmfcmd: TMF command to send.
296 *
297 * Return:
298 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
299 */
300 static int send_tmf(struct afu *afu, struct scsi_cmnd *scp, u64 tmfcmd)
301 {
302 struct afu_cmd *cmd = sc_to_afucz(scp);
303
304 u32 port_sel = scp->device->channel + 1;
305 short lflag = 0;
306 struct Scsi_Host *host = scp->device->host;
307 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
308 struct device *dev = &cfg->dev->dev;
309 ulong lock_flags;
310 int rc = 0;
311 ulong to;
312
313 /* When Task Management Function is active do not send another */
314 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
315 if (cfg->tmf_active)
316 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
317 !cfg->tmf_active,
318 cfg->tmf_slock);
319 cfg->tmf_active = true;
320 cmd->cmd_tmf = true;
321 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
322
323 cmd->rcb.ctx_id = afu->ctx_hndl;
324 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
325 cmd->rcb.port_sel = port_sel;
326 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
327
328 lflag = SISL_REQ_FLAGS_TMF_CMD;
329
330 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
331 SISL_REQ_FLAGS_SUP_UNDERRUN | lflag);
332
333 /* Stash the scp in the command, for reuse during interrupt */
334 cmd->rcb.scp = scp;
335 cmd->parent = afu;
336
337 /* Copy the CDB from the cmd passed in */
338 memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
339
340 /* Send the command */
341 rc = send_cmd(afu, cmd);
342 if (unlikely(rc)) {
343 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
344 cfg->tmf_active = false;
345 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
346 goto out;
347 }
348
349 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
350 to = msecs_to_jiffies(5000);
351 to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
352 !cfg->tmf_active,
353 cfg->tmf_slock,
354 to);
355 if (!to) {
356 cfg->tmf_active = false;
357 dev_err(dev, "%s: TMF timed out!\n", __func__);
358 rc = -1;
359 }
360 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
361 out:
362 return rc;
363 }
364
365 static void afu_unmap(struct kref *ref)
366 {
367 struct afu *afu = container_of(ref, struct afu, mapcount);
368
369 if (likely(afu->afu_map)) {
370 cxl_psa_unmap((void __iomem *)afu->afu_map);
371 afu->afu_map = NULL;
372 }
373 }
374
375 /**
376 * cxlflash_driver_info() - information handler for this host driver
377 * @host: SCSI host associated with device.
378 *
379 * Return: A string describing the device.
380 */
381 static const char *cxlflash_driver_info(struct Scsi_Host *host)
382 {
383 return CXLFLASH_ADAPTER_NAME;
384 }
385
386 /**
387 * cxlflash_queuecommand() - sends a mid-layer request
388 * @host: SCSI host associated with device.
389 * @scp: SCSI command to send.
390 *
391 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
392 */
393 static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
394 {
395 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
396 struct afu *afu = cfg->afu;
397 struct device *dev = &cfg->dev->dev;
398 struct afu_cmd *cmd = sc_to_afucz(scp);
399 u32 port_sel = scp->device->channel + 1;
400 int nseg, i, ncount;
401 struct scatterlist *sg;
402 ulong lock_flags;
403 short lflag = 0;
404 int rc = 0;
405 int kref_got = 0;
406
407 dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
408 "cdb=(%08X-%08X-%08X-%08X)\n",
409 __func__, scp, host->host_no, scp->device->channel,
410 scp->device->id, scp->device->lun,
411 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
412 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
413 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
414 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
415
416 /*
417 * If a Task Management Function is active, wait for it to complete
418 * before continuing with regular commands.
419 */
420 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
421 if (cfg->tmf_active) {
422 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
423 rc = SCSI_MLQUEUE_HOST_BUSY;
424 goto out;
425 }
426 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
427
428 switch (cfg->state) {
429 case STATE_RESET:
430 dev_dbg_ratelimited(dev, "%s: device is in reset!\n", __func__);
431 rc = SCSI_MLQUEUE_HOST_BUSY;
432 goto out;
433 case STATE_FAILTERM:
434 dev_dbg_ratelimited(dev, "%s: device has failed!\n", __func__);
435 scp->result = (DID_NO_CONNECT << 16);
436 scp->scsi_done(scp);
437 rc = 0;
438 goto out;
439 default:
440 break;
441 }
442
443 kref_get(&cfg->afu->mapcount);
444 kref_got = 1;
445
446 cmd->rcb.ctx_id = afu->ctx_hndl;
447 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
448 cmd->rcb.port_sel = port_sel;
449 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
450
451 if (scp->sc_data_direction == DMA_TO_DEVICE)
452 lflag = SISL_REQ_FLAGS_HOST_WRITE;
453 else
454 lflag = SISL_REQ_FLAGS_HOST_READ;
455
456 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
457 SISL_REQ_FLAGS_SUP_UNDERRUN | lflag);
458
459 /* Stash the scp in the reserved field, for reuse during interrupt */
460 cmd->rcb.scp = scp;
461 cmd->parent = afu;
462
463 nseg = scsi_dma_map(scp);
464 if (unlikely(nseg < 0)) {
465 dev_err(dev, "%s: Fail DMA map! nseg=%d\n",
466 __func__, nseg);
467 rc = SCSI_MLQUEUE_HOST_BUSY;
468 goto out;
469 }
470
471 ncount = scsi_sg_count(scp);
472 scsi_for_each_sg(scp, sg, ncount, i) {
473 cmd->rcb.data_len = sg_dma_len(sg);
474 cmd->rcb.data_ea = sg_dma_address(sg);
475 }
476
477 /* Copy the CDB from the scsi_cmnd passed in */
478 memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
479
480 /* Send the command */
481 rc = send_cmd(afu, cmd);
482 if (unlikely(rc))
483 scsi_dma_unmap(scp);
484
485 out:
486 if (kref_got)
487 kref_put(&afu->mapcount, afu_unmap);
488 pr_devel("%s: returning rc=%d\n", __func__, rc);
489 return rc;
490 }
491
492 /**
493 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
494 * @cfg: Internal structure associated with the host.
495 */
496 static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
497 {
498 struct pci_dev *pdev = cfg->dev;
499
500 if (pci_channel_offline(pdev))
501 wait_event_timeout(cfg->reset_waitq,
502 !pci_channel_offline(pdev),
503 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
504 }
505
506 /**
507 * free_mem() - free memory associated with the AFU
508 * @cfg: Internal structure associated with the host.
509 */
510 static void free_mem(struct cxlflash_cfg *cfg)
511 {
512 struct afu *afu = cfg->afu;
513
514 if (cfg->afu) {
515 free_pages((ulong)afu, get_order(sizeof(struct afu)));
516 cfg->afu = NULL;
517 }
518 }
519
520 /**
521 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
522 * @cfg: Internal structure associated with the host.
523 *
524 * Safe to call with AFU in a partially allocated/initialized state.
525 *
526 * Waits for any active internal AFU commands to timeout and then unmaps
527 * the MMIO space.
528 */
529 static void stop_afu(struct cxlflash_cfg *cfg)
530 {
531 struct afu *afu = cfg->afu;
532
533 if (likely(afu)) {
534 while (atomic_read(&afu->cmds_active))
535 ssleep(1);
536 if (likely(afu->afu_map)) {
537 cxl_psa_unmap((void __iomem *)afu->afu_map);
538 afu->afu_map = NULL;
539 }
540 kref_put(&afu->mapcount, afu_unmap);
541 }
542 }
543
544 /**
545 * term_intr() - disables all AFU interrupts
546 * @cfg: Internal structure associated with the host.
547 * @level: Depth of allocation, where to begin waterfall tear down.
548 *
549 * Safe to call with AFU/MC in partially allocated/initialized state.
550 */
551 static void term_intr(struct cxlflash_cfg *cfg, enum undo_level level)
552 {
553 struct afu *afu = cfg->afu;
554 struct device *dev = &cfg->dev->dev;
555
556 if (!afu || !cfg->mcctx) {
557 dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
558 return;
559 }
560
561 switch (level) {
562 case UNMAP_THREE:
563 cxl_unmap_afu_irq(cfg->mcctx, 3, afu);
564 case UNMAP_TWO:
565 cxl_unmap_afu_irq(cfg->mcctx, 2, afu);
566 case UNMAP_ONE:
567 cxl_unmap_afu_irq(cfg->mcctx, 1, afu);
568 case FREE_IRQ:
569 cxl_free_afu_irqs(cfg->mcctx);
570 /* fall through */
571 case UNDO_NOOP:
572 /* No action required */
573 break;
574 }
575 }
576
577 /**
578 * term_mc() - terminates the master context
579 * @cfg: Internal structure associated with the host.
580 * @level: Depth of allocation, where to begin waterfall tear down.
581 *
582 * Safe to call with AFU/MC in partially allocated/initialized state.
583 */
584 static void term_mc(struct cxlflash_cfg *cfg)
585 {
586 int rc = 0;
587 struct afu *afu = cfg->afu;
588 struct device *dev = &cfg->dev->dev;
589
590 if (!afu || !cfg->mcctx) {
591 dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
592 return;
593 }
594
595 rc = cxl_stop_context(cfg->mcctx);
596 WARN_ON(rc);
597 cfg->mcctx = NULL;
598 }
599
600 /**
601 * term_afu() - terminates the AFU
602 * @cfg: Internal structure associated with the host.
603 *
604 * Safe to call with AFU/MC in partially allocated/initialized state.
605 */
606 static void term_afu(struct cxlflash_cfg *cfg)
607 {
608 /*
609 * Tear down is carefully orchestrated to ensure
610 * no interrupts can come in when the problem state
611 * area is unmapped.
612 *
613 * 1) Disable all AFU interrupts
614 * 2) Unmap the problem state area
615 * 3) Stop the master context
616 */
617 term_intr(cfg, UNMAP_THREE);
618 if (cfg->afu)
619 stop_afu(cfg);
620
621 term_mc(cfg);
622
623 pr_debug("%s: returning\n", __func__);
624 }
625
626 /**
627 * notify_shutdown() - notifies device of pending shutdown
628 * @cfg: Internal structure associated with the host.
629 * @wait: Whether to wait for shutdown processing to complete.
630 *
631 * This function will notify the AFU that the adapter is being shutdown
632 * and will wait for shutdown processing to complete if wait is true.
633 * This notification should flush pending I/Os to the device and halt
634 * further I/Os until the next AFU reset is issued and device restarted.
635 */
636 static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
637 {
638 struct afu *afu = cfg->afu;
639 struct device *dev = &cfg->dev->dev;
640 struct sisl_global_map __iomem *global;
641 struct dev_dependent_vals *ddv;
642 u64 reg, status;
643 int i, retry_cnt = 0;
644
645 ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
646 if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
647 return;
648
649 if (!afu || !afu->afu_map) {
650 dev_dbg(dev, "%s: The problem state area is not mapped\n",
651 __func__);
652 return;
653 }
654
655 global = &afu->afu_map->global;
656
657 /* Notify AFU */
658 for (i = 0; i < NUM_FC_PORTS; i++) {
659 reg = readq_be(&global->fc_regs[i][FC_CONFIG2 / 8]);
660 reg |= SISL_FC_SHUTDOWN_NORMAL;
661 writeq_be(reg, &global->fc_regs[i][FC_CONFIG2 / 8]);
662 }
663
664 if (!wait)
665 return;
666
667 /* Wait up to 1.5 seconds for shutdown processing to complete */
668 for (i = 0; i < NUM_FC_PORTS; i++) {
669 retry_cnt = 0;
670 while (true) {
671 status = readq_be(&global->fc_regs[i][FC_STATUS / 8]);
672 if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
673 break;
674 if (++retry_cnt >= MC_RETRY_CNT) {
675 dev_dbg(dev, "%s: port %d shutdown processing "
676 "not yet completed\n", __func__, i);
677 break;
678 }
679 msleep(100 * retry_cnt);
680 }
681 }
682 }
683
684 /**
685 * cxlflash_remove() - PCI entry point to tear down host
686 * @pdev: PCI device associated with the host.
687 *
688 * Safe to use as a cleanup in partially allocated/initialized state.
689 */
690 static void cxlflash_remove(struct pci_dev *pdev)
691 {
692 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
693 ulong lock_flags;
694
695 if (!pci_is_enabled(pdev)) {
696 pr_debug("%s: Device is disabled\n", __func__);
697 return;
698 }
699
700 /* If a Task Management Function is active, wait for it to complete
701 * before continuing with remove.
702 */
703 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
704 if (cfg->tmf_active)
705 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
706 !cfg->tmf_active,
707 cfg->tmf_slock);
708 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
709
710 /* Notify AFU and wait for shutdown processing to complete */
711 notify_shutdown(cfg, true);
712
713 cfg->state = STATE_FAILTERM;
714 cxlflash_stop_term_user_contexts(cfg);
715
716 switch (cfg->init_state) {
717 case INIT_STATE_SCSI:
718 cxlflash_term_local_luns(cfg);
719 scsi_remove_host(cfg->host);
720 /* fall through */
721 case INIT_STATE_AFU:
722 cancel_work_sync(&cfg->work_q);
723 term_afu(cfg);
724 case INIT_STATE_PCI:
725 pci_disable_device(pdev);
726 case INIT_STATE_NONE:
727 free_mem(cfg);
728 scsi_host_put(cfg->host);
729 break;
730 }
731
732 pr_debug("%s: returning\n", __func__);
733 }
734
735 /**
736 * alloc_mem() - allocates the AFU and its command pool
737 * @cfg: Internal structure associated with the host.
738 *
739 * A partially allocated state remains on failure.
740 *
741 * Return:
742 * 0 on success
743 * -ENOMEM on failure to allocate memory
744 */
745 static int alloc_mem(struct cxlflash_cfg *cfg)
746 {
747 int rc = 0;
748 struct device *dev = &cfg->dev->dev;
749
750 /* AFU is ~12k, i.e. only one 64k page or up to four 4k pages */
751 cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
752 get_order(sizeof(struct afu)));
753 if (unlikely(!cfg->afu)) {
754 dev_err(dev, "%s: cannot get %d free pages\n",
755 __func__, get_order(sizeof(struct afu)));
756 rc = -ENOMEM;
757 goto out;
758 }
759 cfg->afu->parent = cfg;
760 cfg->afu->afu_map = NULL;
761 out:
762 return rc;
763 }
764
765 /**
766 * init_pci() - initializes the host as a PCI device
767 * @cfg: Internal structure associated with the host.
768 *
769 * Return: 0 on success, -errno on failure
770 */
771 static int init_pci(struct cxlflash_cfg *cfg)
772 {
773 struct pci_dev *pdev = cfg->dev;
774 int rc = 0;
775
776 rc = pci_enable_device(pdev);
777 if (rc || pci_channel_offline(pdev)) {
778 if (pci_channel_offline(pdev)) {
779 cxlflash_wait_for_pci_err_recovery(cfg);
780 rc = pci_enable_device(pdev);
781 }
782
783 if (rc) {
784 dev_err(&pdev->dev, "%s: Cannot enable adapter\n",
785 __func__);
786 cxlflash_wait_for_pci_err_recovery(cfg);
787 goto out;
788 }
789 }
790
791 out:
792 pr_debug("%s: returning rc=%d\n", __func__, rc);
793 return rc;
794 }
795
796 /**
797 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
798 * @cfg: Internal structure associated with the host.
799 *
800 * Return: 0 on success, -errno on failure
801 */
802 static int init_scsi(struct cxlflash_cfg *cfg)
803 {
804 struct pci_dev *pdev = cfg->dev;
805 int rc = 0;
806
807 rc = scsi_add_host(cfg->host, &pdev->dev);
808 if (rc) {
809 dev_err(&pdev->dev, "%s: scsi_add_host failed (rc=%d)\n",
810 __func__, rc);
811 goto out;
812 }
813
814 scsi_scan_host(cfg->host);
815
816 out:
817 pr_debug("%s: returning rc=%d\n", __func__, rc);
818 return rc;
819 }
820
821 /**
822 * set_port_online() - transitions the specified host FC port to online state
823 * @fc_regs: Top of MMIO region defined for specified port.
824 *
825 * The provided MMIO region must be mapped prior to call. Online state means
826 * that the FC link layer has synced, completed the handshaking process, and
827 * is ready for login to start.
828 */
829 static void set_port_online(__be64 __iomem *fc_regs)
830 {
831 u64 cmdcfg;
832
833 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
834 cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
835 cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
836 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
837 }
838
839 /**
840 * set_port_offline() - transitions the specified host FC port to offline state
841 * @fc_regs: Top of MMIO region defined for specified port.
842 *
843 * The provided MMIO region must be mapped prior to call.
844 */
845 static void set_port_offline(__be64 __iomem *fc_regs)
846 {
847 u64 cmdcfg;
848
849 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
850 cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
851 cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
852 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
853 }
854
855 /**
856 * wait_port_online() - waits for the specified host FC port come online
857 * @fc_regs: Top of MMIO region defined for specified port.
858 * @delay_us: Number of microseconds to delay between reading port status.
859 * @nretry: Number of cycles to retry reading port status.
860 *
861 * The provided MMIO region must be mapped prior to call. This will timeout
862 * when the cable is not plugged in.
863 *
864 * Return:
865 * TRUE (1) when the specified port is online
866 * FALSE (0) when the specified port fails to come online after timeout
867 * -EINVAL when @delay_us is less than 1000
868 */
869 static int wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
870 {
871 u64 status;
872
873 if (delay_us < 1000) {
874 pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
875 return -EINVAL;
876 }
877
878 do {
879 msleep(delay_us / 1000);
880 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
881 if (status == U64_MAX)
882 nretry /= 2;
883 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
884 nretry--);
885
886 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
887 }
888
889 /**
890 * wait_port_offline() - waits for the specified host FC port go offline
891 * @fc_regs: Top of MMIO region defined for specified port.
892 * @delay_us: Number of microseconds to delay between reading port status.
893 * @nretry: Number of cycles to retry reading port status.
894 *
895 * The provided MMIO region must be mapped prior to call.
896 *
897 * Return:
898 * TRUE (1) when the specified port is offline
899 * FALSE (0) when the specified port fails to go offline after timeout
900 * -EINVAL when @delay_us is less than 1000
901 */
902 static int wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
903 {
904 u64 status;
905
906 if (delay_us < 1000) {
907 pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
908 return -EINVAL;
909 }
910
911 do {
912 msleep(delay_us / 1000);
913 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
914 if (status == U64_MAX)
915 nretry /= 2;
916 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
917 nretry--);
918
919 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
920 }
921
922 /**
923 * afu_set_wwpn() - configures the WWPN for the specified host FC port
924 * @afu: AFU associated with the host that owns the specified FC port.
925 * @port: Port number being configured.
926 * @fc_regs: Top of MMIO region defined for specified port.
927 * @wwpn: The world-wide-port-number previously discovered for port.
928 *
929 * The provided MMIO region must be mapped prior to call. As part of the
930 * sequence to configure the WWPN, the port is toggled offline and then back
931 * online. This toggling action can cause this routine to delay up to a few
932 * seconds. When configured to use the internal LUN feature of the AFU, a
933 * failure to come online is overridden.
934 */
935 static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
936 u64 wwpn)
937 {
938 set_port_offline(fc_regs);
939 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
940 FC_PORT_STATUS_RETRY_CNT)) {
941 pr_debug("%s: wait on port %d to go offline timed out\n",
942 __func__, port);
943 }
944
945 writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
946
947 set_port_online(fc_regs);
948 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
949 FC_PORT_STATUS_RETRY_CNT)) {
950 pr_debug("%s: wait on port %d to go online timed out\n",
951 __func__, port);
952 }
953 }
954
955 /**
956 * afu_link_reset() - resets the specified host FC port
957 * @afu: AFU associated with the host that owns the specified FC port.
958 * @port: Port number being configured.
959 * @fc_regs: Top of MMIO region defined for specified port.
960 *
961 * The provided MMIO region must be mapped prior to call. The sequence to
962 * reset the port involves toggling it offline and then back online. This
963 * action can cause this routine to delay up to a few seconds. An effort
964 * is made to maintain link with the device by switching to host to use
965 * the alternate port exclusively while the reset takes place.
966 * failure to come online is overridden.
967 */
968 static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
969 {
970 u64 port_sel;
971
972 /* first switch the AFU to the other links, if any */
973 port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
974 port_sel &= ~(1ULL << port);
975 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
976 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
977
978 set_port_offline(fc_regs);
979 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
980 FC_PORT_STATUS_RETRY_CNT))
981 pr_err("%s: wait on port %d to go offline timed out\n",
982 __func__, port);
983
984 set_port_online(fc_regs);
985 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
986 FC_PORT_STATUS_RETRY_CNT))
987 pr_err("%s: wait on port %d to go online timed out\n",
988 __func__, port);
989
990 /* switch back to include this port */
991 port_sel |= (1ULL << port);
992 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
993 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
994
995 pr_debug("%s: returning port_sel=%lld\n", __func__, port_sel);
996 }
997
998 /*
999 * Asynchronous interrupt information table
1000 */
1001 static const struct asyc_intr_info ainfo[] = {
1002 {SISL_ASTATUS_FC0_OTHER, "other error", 0, CLR_FC_ERROR | LINK_RESET},
1003 {SISL_ASTATUS_FC0_LOGO, "target initiated LOGO", 0, 0},
1004 {SISL_ASTATUS_FC0_CRC_T, "CRC threshold exceeded", 0, LINK_RESET},
1005 {SISL_ASTATUS_FC0_LOGI_R, "login timed out, retrying", 0, LINK_RESET},
1006 {SISL_ASTATUS_FC0_LOGI_F, "login failed", 0, CLR_FC_ERROR},
1007 {SISL_ASTATUS_FC0_LOGI_S, "login succeeded", 0, SCAN_HOST},
1008 {SISL_ASTATUS_FC0_LINK_DN, "link down", 0, 0},
1009 {SISL_ASTATUS_FC0_LINK_UP, "link up", 0, 0},
1010 {SISL_ASTATUS_FC1_OTHER, "other error", 1, CLR_FC_ERROR | LINK_RESET},
1011 {SISL_ASTATUS_FC1_LOGO, "target initiated LOGO", 1, 0},
1012 {SISL_ASTATUS_FC1_CRC_T, "CRC threshold exceeded", 1, LINK_RESET},
1013 {SISL_ASTATUS_FC1_LOGI_R, "login timed out, retrying", 1, LINK_RESET},
1014 {SISL_ASTATUS_FC1_LOGI_F, "login failed", 1, CLR_FC_ERROR},
1015 {SISL_ASTATUS_FC1_LOGI_S, "login succeeded", 1, SCAN_HOST},
1016 {SISL_ASTATUS_FC1_LINK_DN, "link down", 1, 0},
1017 {SISL_ASTATUS_FC1_LINK_UP, "link up", 1, 0},
1018 {0x0, "", 0, 0} /* terminator */
1019 };
1020
1021 /**
1022 * find_ainfo() - locates and returns asynchronous interrupt information
1023 * @status: Status code set by AFU on error.
1024 *
1025 * Return: The located information or NULL when the status code is invalid.
1026 */
1027 static const struct asyc_intr_info *find_ainfo(u64 status)
1028 {
1029 const struct asyc_intr_info *info;
1030
1031 for (info = &ainfo[0]; info->status; info++)
1032 if (info->status == status)
1033 return info;
1034
1035 return NULL;
1036 }
1037
1038 /**
1039 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1040 * @afu: AFU associated with the host.
1041 */
1042 static void afu_err_intr_init(struct afu *afu)
1043 {
1044 int i;
1045 u64 reg;
1046
1047 /* global async interrupts: AFU clears afu_ctrl on context exit
1048 * if async interrupts were sent to that context. This prevents
1049 * the AFU form sending further async interrupts when
1050 * there is
1051 * nobody to receive them.
1052 */
1053
1054 /* mask all */
1055 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
1056 /* set LISN# to send and point to master context */
1057 reg = ((u64) (((afu->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
1058
1059 if (afu->internal_lun)
1060 reg |= 1; /* Bit 63 indicates local lun */
1061 writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1062 /* clear all */
1063 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1064 /* unmask bits that are of interest */
1065 /* note: afu can send an interrupt after this step */
1066 writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1067 /* clear again in case a bit came on after previous clear but before */
1068 /* unmask */
1069 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1070
1071 /* Clear/Set internal lun bits */
1072 reg = readq_be(&afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1073 reg &= SISL_FC_INTERNAL_MASK;
1074 if (afu->internal_lun)
1075 reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
1076 writeq_be(reg, &afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1077
1078 /* now clear FC errors */
1079 for (i = 0; i < NUM_FC_PORTS; i++) {
1080 writeq_be(0xFFFFFFFFU,
1081 &afu->afu_map->global.fc_regs[i][FC_ERROR / 8]);
1082 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRCAP / 8]);
1083 }
1084
1085 /* sync interrupts for master's IOARRIN write */
1086 /* note that unlike asyncs, there can be no pending sync interrupts */
1087 /* at this time (this is a fresh context and master has not written */
1088 /* IOARRIN yet), so there is nothing to clear. */
1089
1090 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1091 writeq_be(SISL_MSI_SYNC_ERROR, &afu->host_map->ctx_ctrl);
1092 writeq_be(SISL_ISTATUS_MASK, &afu->host_map->intr_mask);
1093 }
1094
1095 /**
1096 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1097 * @irq: Interrupt number.
1098 * @data: Private data provided at interrupt registration, the AFU.
1099 *
1100 * Return: Always return IRQ_HANDLED.
1101 */
1102 static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1103 {
1104 struct afu *afu = (struct afu *)data;
1105 u64 reg;
1106 u64 reg_unmasked;
1107
1108 reg = readq_be(&afu->host_map->intr_status);
1109 reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1110
1111 if (reg_unmasked == 0UL) {
1112 pr_err("%s: %llX: spurious interrupt, intr_status %016llX\n",
1113 __func__, (u64)afu, reg);
1114 goto cxlflash_sync_err_irq_exit;
1115 }
1116
1117 pr_err("%s: %llX: unexpected interrupt, intr_status %016llX\n",
1118 __func__, (u64)afu, reg);
1119
1120 writeq_be(reg_unmasked, &afu->host_map->intr_clear);
1121
1122 cxlflash_sync_err_irq_exit:
1123 pr_debug("%s: returning rc=%d\n", __func__, IRQ_HANDLED);
1124 return IRQ_HANDLED;
1125 }
1126
1127 /**
1128 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1129 * @irq: Interrupt number.
1130 * @data: Private data provided at interrupt registration, the AFU.
1131 *
1132 * Return: Always return IRQ_HANDLED.
1133 */
1134 static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
1135 {
1136 struct afu *afu = (struct afu *)data;
1137 struct afu_cmd *cmd;
1138 bool toggle = afu->toggle;
1139 u64 entry,
1140 *hrrq_start = afu->hrrq_start,
1141 *hrrq_end = afu->hrrq_end,
1142 *hrrq_curr = afu->hrrq_curr;
1143
1144 /* Process however many RRQ entries that are ready */
1145 while (true) {
1146 entry = *hrrq_curr;
1147
1148 if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1149 break;
1150
1151 cmd = (struct afu_cmd *)(entry & ~SISL_RESP_HANDLE_T_BIT);
1152 cmd_complete(cmd);
1153
1154 /* Advance to next entry or wrap and flip the toggle bit */
1155 if (hrrq_curr < hrrq_end)
1156 hrrq_curr++;
1157 else {
1158 hrrq_curr = hrrq_start;
1159 toggle ^= SISL_RESP_HANDLE_T_BIT;
1160 }
1161 }
1162
1163 afu->hrrq_curr = hrrq_curr;
1164 afu->toggle = toggle;
1165
1166 return IRQ_HANDLED;
1167 }
1168
1169 /**
1170 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1171 * @irq: Interrupt number.
1172 * @data: Private data provided at interrupt registration, the AFU.
1173 *
1174 * Return: Always return IRQ_HANDLED.
1175 */
1176 static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1177 {
1178 struct afu *afu = (struct afu *)data;
1179 struct cxlflash_cfg *cfg = afu->parent;
1180 struct device *dev = &cfg->dev->dev;
1181 u64 reg_unmasked;
1182 const struct asyc_intr_info *info;
1183 struct sisl_global_map __iomem *global = &afu->afu_map->global;
1184 u64 reg;
1185 u8 port;
1186 int i;
1187
1188 reg = readq_be(&global->regs.aintr_status);
1189 reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1190
1191 if (reg_unmasked == 0) {
1192 dev_err(dev, "%s: spurious interrupt, aintr_status 0x%016llX\n",
1193 __func__, reg);
1194 goto out;
1195 }
1196
1197 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
1198 writeq_be(reg_unmasked, &global->regs.aintr_clear);
1199
1200 /* Check each bit that is on */
1201 for (i = 0; reg_unmasked; i++, reg_unmasked = (reg_unmasked >> 1)) {
1202 info = find_ainfo(1ULL << i);
1203 if (((reg_unmasked & 0x1) == 0) || !info)
1204 continue;
1205
1206 port = info->port;
1207
1208 dev_err(dev, "%s: FC Port %d -> %s, fc_status 0x%08llX\n",
1209 __func__, port, info->desc,
1210 readq_be(&global->fc_regs[port][FC_STATUS / 8]));
1211
1212 /*
1213 * Do link reset first, some OTHER errors will set FC_ERROR
1214 * again if cleared before or w/o a reset
1215 */
1216 if (info->action & LINK_RESET) {
1217 dev_err(dev, "%s: FC Port %d: resetting link\n",
1218 __func__, port);
1219 cfg->lr_state = LINK_RESET_REQUIRED;
1220 cfg->lr_port = port;
1221 kref_get(&cfg->afu->mapcount);
1222 schedule_work(&cfg->work_q);
1223 }
1224
1225 if (info->action & CLR_FC_ERROR) {
1226 reg = readq_be(&global->fc_regs[port][FC_ERROR / 8]);
1227
1228 /*
1229 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
1230 * should be the same and tracing one is sufficient.
1231 */
1232
1233 dev_err(dev, "%s: fc %d: clearing fc_error 0x%08llX\n",
1234 __func__, port, reg);
1235
1236 writeq_be(reg, &global->fc_regs[port][FC_ERROR / 8]);
1237 writeq_be(0, &global->fc_regs[port][FC_ERRCAP / 8]);
1238 }
1239
1240 if (info->action & SCAN_HOST) {
1241 atomic_inc(&cfg->scan_host_needed);
1242 kref_get(&cfg->afu->mapcount);
1243 schedule_work(&cfg->work_q);
1244 }
1245 }
1246
1247 out:
1248 dev_dbg(dev, "%s: returning IRQ_HANDLED, afu=%p\n", __func__, afu);
1249 return IRQ_HANDLED;
1250 }
1251
1252 /**
1253 * start_context() - starts the master context
1254 * @cfg: Internal structure associated with the host.
1255 *
1256 * Return: A success or failure value from CXL services.
1257 */
1258 static int start_context(struct cxlflash_cfg *cfg)
1259 {
1260 int rc = 0;
1261
1262 rc = cxl_start_context(cfg->mcctx,
1263 cfg->afu->work.work_element_descriptor,
1264 NULL);
1265
1266 pr_debug("%s: returning rc=%d\n", __func__, rc);
1267 return rc;
1268 }
1269
1270 /**
1271 * read_vpd() - obtains the WWPNs from VPD
1272 * @cfg: Internal structure associated with the host.
1273 * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
1274 *
1275 * Return: 0 on success, -errno on failure
1276 */
1277 static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1278 {
1279 struct pci_dev *dev = cfg->dev;
1280 int rc = 0;
1281 int ro_start, ro_size, i, j, k;
1282 ssize_t vpd_size;
1283 char vpd_data[CXLFLASH_VPD_LEN];
1284 char tmp_buf[WWPN_BUF_LEN] = { 0 };
1285 char *wwpn_vpd_tags[NUM_FC_PORTS] = { "V5", "V6" };
1286
1287 /* Get the VPD data from the device */
1288 vpd_size = cxl_read_adapter_vpd(dev, vpd_data, sizeof(vpd_data));
1289 if (unlikely(vpd_size <= 0)) {
1290 dev_err(&dev->dev, "%s: Unable to read VPD (size = %ld)\n",
1291 __func__, vpd_size);
1292 rc = -ENODEV;
1293 goto out;
1294 }
1295
1296 /* Get the read only section offset */
1297 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
1298 PCI_VPD_LRDT_RO_DATA);
1299 if (unlikely(ro_start < 0)) {
1300 dev_err(&dev->dev, "%s: VPD Read-only data not found\n",
1301 __func__);
1302 rc = -ENODEV;
1303 goto out;
1304 }
1305
1306 /* Get the read only section size, cap when extends beyond read VPD */
1307 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1308 j = ro_size;
1309 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1310 if (unlikely((i + j) > vpd_size)) {
1311 pr_debug("%s: Might need to read more VPD (%d > %ld)\n",
1312 __func__, (i + j), vpd_size);
1313 ro_size = vpd_size - i;
1314 }
1315
1316 /*
1317 * Find the offset of the WWPN tag within the read only
1318 * VPD data and validate the found field (partials are
1319 * no good to us). Convert the ASCII data to an integer
1320 * value. Note that we must copy to a temporary buffer
1321 * because the conversion service requires that the ASCII
1322 * string be terminated.
1323 */
1324 for (k = 0; k < NUM_FC_PORTS; k++) {
1325 j = ro_size;
1326 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1327
1328 i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
1329 if (unlikely(i < 0)) {
1330 dev_err(&dev->dev, "%s: Port %d WWPN not found "
1331 "in VPD\n", __func__, k);
1332 rc = -ENODEV;
1333 goto out;
1334 }
1335
1336 j = pci_vpd_info_field_size(&vpd_data[i]);
1337 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1338 if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
1339 dev_err(&dev->dev, "%s: Port %d WWPN incomplete or "
1340 "VPD corrupt\n",
1341 __func__, k);
1342 rc = -ENODEV;
1343 goto out;
1344 }
1345
1346 memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1347 rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1348 if (unlikely(rc)) {
1349 dev_err(&dev->dev, "%s: Fail to convert port %d WWPN "
1350 "to integer\n", __func__, k);
1351 rc = -ENODEV;
1352 goto out;
1353 }
1354 }
1355
1356 out:
1357 pr_debug("%s: returning rc=%d\n", __func__, rc);
1358 return rc;
1359 }
1360
1361 /**
1362 * init_pcr() - initialize the provisioning and control registers
1363 * @cfg: Internal structure associated with the host.
1364 *
1365 * Also sets up fast access to the mapped registers and initializes AFU
1366 * command fields that never change.
1367 */
1368 static void init_pcr(struct cxlflash_cfg *cfg)
1369 {
1370 struct afu *afu = cfg->afu;
1371 struct sisl_ctrl_map __iomem *ctrl_map;
1372 int i;
1373
1374 for (i = 0; i < MAX_CONTEXT; i++) {
1375 ctrl_map = &afu->afu_map->ctrls[i].ctrl;
1376 /* Disrupt any clients that could be running */
1377 /* e.g. clients that survived a master restart */
1378 writeq_be(0, &ctrl_map->rht_start);
1379 writeq_be(0, &ctrl_map->rht_cnt_id);
1380 writeq_be(0, &ctrl_map->ctx_cap);
1381 }
1382
1383 /* Copy frequently used fields into afu */
1384 afu->ctx_hndl = (u16) cxl_process_element(cfg->mcctx);
1385 afu->host_map = &afu->afu_map->hosts[afu->ctx_hndl].host;
1386 afu->ctrl_map = &afu->afu_map->ctrls[afu->ctx_hndl].ctrl;
1387
1388 /* Program the Endian Control for the master context */
1389 writeq_be(SISL_ENDIAN_CTRL, &afu->host_map->endian_ctrl);
1390 }
1391
1392 /**
1393 * init_global() - initialize AFU global registers
1394 * @cfg: Internal structure associated with the host.
1395 */
1396 static int init_global(struct cxlflash_cfg *cfg)
1397 {
1398 struct afu *afu = cfg->afu;
1399 struct device *dev = &cfg->dev->dev;
1400 u64 wwpn[NUM_FC_PORTS]; /* wwpn of AFU ports */
1401 int i = 0, num_ports = 0;
1402 int rc = 0;
1403 u64 reg;
1404
1405 rc = read_vpd(cfg, &wwpn[0]);
1406 if (rc) {
1407 dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
1408 goto out;
1409 }
1410
1411 pr_debug("%s: wwpn0=0x%llX wwpn1=0x%llX\n", __func__, wwpn[0], wwpn[1]);
1412
1413 /* Set up RRQ in AFU for master issued cmds */
1414 writeq_be((u64) afu->hrrq_start, &afu->host_map->rrq_start);
1415 writeq_be((u64) afu->hrrq_end, &afu->host_map->rrq_end);
1416
1417 /* AFU configuration */
1418 reg = readq_be(&afu->afu_map->global.regs.afu_config);
1419 reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1420 /* enable all auto retry options and control endianness */
1421 /* leave others at default: */
1422 /* CTX_CAP write protected, mbox_r does not clear on read and */
1423 /* checker on if dual afu */
1424 writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1425
1426 /* Global port select: select either port */
1427 if (afu->internal_lun) {
1428 /* Only use port 0 */
1429 writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
1430 num_ports = NUM_FC_PORTS - 1;
1431 } else {
1432 writeq_be(BOTH_PORTS, &afu->afu_map->global.regs.afu_port_sel);
1433 num_ports = NUM_FC_PORTS;
1434 }
1435
1436 for (i = 0; i < num_ports; i++) {
1437 /* Unmask all errors (but they are still masked at AFU) */
1438 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRMSK / 8]);
1439 /* Clear CRC error cnt & set a threshold */
1440 (void)readq_be(&afu->afu_map->global.
1441 fc_regs[i][FC_CNT_CRCERR / 8]);
1442 writeq_be(MC_CRC_THRESH, &afu->afu_map->global.fc_regs[i]
1443 [FC_CRC_THRESH / 8]);
1444
1445 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
1446 if (wwpn[i] != 0)
1447 afu_set_wwpn(afu, i,
1448 &afu->afu_map->global.fc_regs[i][0],
1449 wwpn[i]);
1450 /* Programming WWPN back to back causes additional
1451 * offline/online transitions and a PLOGI
1452 */
1453 msleep(100);
1454 }
1455
1456 /* Set up master's own CTX_CAP to allow real mode, host translation */
1457 /* tables, afu cmds and read/write GSCSI cmds. */
1458 /* First, unlock ctx_cap write by reading mbox */
1459 (void)readq_be(&afu->ctrl_map->mbox_r); /* unlock ctx_cap */
1460 writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1461 SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1462 SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1463 &afu->ctrl_map->ctx_cap);
1464 /* Initialize heartbeat */
1465 afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
1466
1467 out:
1468 return rc;
1469 }
1470
1471 /**
1472 * start_afu() - initializes and starts the AFU
1473 * @cfg: Internal structure associated with the host.
1474 */
1475 static int start_afu(struct cxlflash_cfg *cfg)
1476 {
1477 struct afu *afu = cfg->afu;
1478 int rc = 0;
1479
1480 init_pcr(cfg);
1481
1482 /* After an AFU reset, RRQ entries are stale, clear them */
1483 memset(&afu->rrq_entry, 0, sizeof(afu->rrq_entry));
1484
1485 /* Initialize RRQ pointers */
1486 afu->hrrq_start = &afu->rrq_entry[0];
1487 afu->hrrq_end = &afu->rrq_entry[NUM_RRQ_ENTRY - 1];
1488 afu->hrrq_curr = afu->hrrq_start;
1489 afu->toggle = 1;
1490
1491 rc = init_global(cfg);
1492
1493 pr_debug("%s: returning rc=%d\n", __func__, rc);
1494 return rc;
1495 }
1496
1497 /**
1498 * init_intr() - setup interrupt handlers for the master context
1499 * @cfg: Internal structure associated with the host.
1500 *
1501 * Return: 0 on success, -errno on failure
1502 */
1503 static enum undo_level init_intr(struct cxlflash_cfg *cfg,
1504 struct cxl_context *ctx)
1505 {
1506 struct afu *afu = cfg->afu;
1507 struct device *dev = &cfg->dev->dev;
1508 int rc = 0;
1509 enum undo_level level = UNDO_NOOP;
1510
1511 rc = cxl_allocate_afu_irqs(ctx, 3);
1512 if (unlikely(rc)) {
1513 dev_err(dev, "%s: call to allocate_afu_irqs failed rc=%d!\n",
1514 __func__, rc);
1515 level = UNDO_NOOP;
1516 goto out;
1517 }
1518
1519 rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, afu,
1520 "SISL_MSI_SYNC_ERROR");
1521 if (unlikely(rc <= 0)) {
1522 dev_err(dev, "%s: IRQ 1 (SISL_MSI_SYNC_ERROR) map failed!\n",
1523 __func__);
1524 level = FREE_IRQ;
1525 goto out;
1526 }
1527
1528 rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, afu,
1529 "SISL_MSI_RRQ_UPDATED");
1530 if (unlikely(rc <= 0)) {
1531 dev_err(dev, "%s: IRQ 2 (SISL_MSI_RRQ_UPDATED) map failed!\n",
1532 __func__);
1533 level = UNMAP_ONE;
1534 goto out;
1535 }
1536
1537 rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, afu,
1538 "SISL_MSI_ASYNC_ERROR");
1539 if (unlikely(rc <= 0)) {
1540 dev_err(dev, "%s: IRQ 3 (SISL_MSI_ASYNC_ERROR) map failed!\n",
1541 __func__);
1542 level = UNMAP_TWO;
1543 goto out;
1544 }
1545 out:
1546 return level;
1547 }
1548
1549 /**
1550 * init_mc() - create and register as the master context
1551 * @cfg: Internal structure associated with the host.
1552 *
1553 * Return: 0 on success, -errno on failure
1554 */
1555 static int init_mc(struct cxlflash_cfg *cfg)
1556 {
1557 struct cxl_context *ctx;
1558 struct device *dev = &cfg->dev->dev;
1559 int rc = 0;
1560 enum undo_level level;
1561
1562 ctx = cxl_get_context(cfg->dev);
1563 if (unlikely(!ctx)) {
1564 rc = -ENOMEM;
1565 goto ret;
1566 }
1567 cfg->mcctx = ctx;
1568
1569 /* Set it up as a master with the CXL */
1570 cxl_set_master(ctx);
1571
1572 /* During initialization reset the AFU to start from a clean slate */
1573 rc = cxl_afu_reset(cfg->mcctx);
1574 if (unlikely(rc)) {
1575 dev_err(dev, "%s: initial AFU reset failed rc=%d\n",
1576 __func__, rc);
1577 goto ret;
1578 }
1579
1580 level = init_intr(cfg, ctx);
1581 if (unlikely(level)) {
1582 dev_err(dev, "%s: setting up interrupts failed rc=%d\n",
1583 __func__, rc);
1584 goto out;
1585 }
1586
1587 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1588 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1589 * element (pe) that is embedded in the context (ctx)
1590 */
1591 rc = start_context(cfg);
1592 if (unlikely(rc)) {
1593 dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
1594 level = UNMAP_THREE;
1595 goto out;
1596 }
1597 ret:
1598 pr_debug("%s: returning rc=%d\n", __func__, rc);
1599 return rc;
1600 out:
1601 term_intr(cfg, level);
1602 goto ret;
1603 }
1604
1605 /**
1606 * init_afu() - setup as master context and start AFU
1607 * @cfg: Internal structure associated with the host.
1608 *
1609 * This routine is a higher level of control for configuring the
1610 * AFU on probe and reset paths.
1611 *
1612 * Return: 0 on success, -errno on failure
1613 */
1614 static int init_afu(struct cxlflash_cfg *cfg)
1615 {
1616 u64 reg;
1617 int rc = 0;
1618 struct afu *afu = cfg->afu;
1619 struct device *dev = &cfg->dev->dev;
1620
1621 cxl_perst_reloads_same_image(cfg->cxl_afu, true);
1622
1623 rc = init_mc(cfg);
1624 if (rc) {
1625 dev_err(dev, "%s: call to init_mc failed, rc=%d!\n",
1626 __func__, rc);
1627 goto out;
1628 }
1629
1630 /* Map the entire MMIO space of the AFU */
1631 afu->afu_map = cxl_psa_map(cfg->mcctx);
1632 if (!afu->afu_map) {
1633 dev_err(dev, "%s: call to cxl_psa_map failed!\n", __func__);
1634 rc = -ENOMEM;
1635 goto err1;
1636 }
1637 kref_init(&afu->mapcount);
1638
1639 /* No byte reverse on reading afu_version or string will be backwards */
1640 reg = readq(&afu->afu_map->global.regs.afu_version);
1641 memcpy(afu->version, &reg, sizeof(reg));
1642 afu->interface_version =
1643 readq_be(&afu->afu_map->global.regs.interface_version);
1644 if ((afu->interface_version + 1) == 0) {
1645 pr_err("Back level AFU, please upgrade. AFU version %s "
1646 "interface version 0x%llx\n", afu->version,
1647 afu->interface_version);
1648 rc = -EINVAL;
1649 goto err2;
1650 }
1651
1652 pr_debug("%s: afu version %s, interface version 0x%llX\n", __func__,
1653 afu->version, afu->interface_version);
1654
1655 rc = start_afu(cfg);
1656 if (rc) {
1657 dev_err(dev, "%s: call to start_afu failed, rc=%d!\n",
1658 __func__, rc);
1659 goto err2;
1660 }
1661
1662 afu_err_intr_init(cfg->afu);
1663 spin_lock_init(&afu->rrin_slock);
1664 afu->room = readq_be(&afu->host_map->cmd_room);
1665
1666 /* Restore the LUN mappings */
1667 cxlflash_restore_luntable(cfg);
1668 out:
1669 pr_debug("%s: returning rc=%d\n", __func__, rc);
1670 return rc;
1671
1672 err2:
1673 kref_put(&afu->mapcount, afu_unmap);
1674 err1:
1675 term_intr(cfg, UNMAP_THREE);
1676 term_mc(cfg);
1677 goto out;
1678 }
1679
1680 /**
1681 * cxlflash_afu_sync() - builds and sends an AFU sync command
1682 * @afu: AFU associated with the host.
1683 * @ctx_hndl_u: Identifies context requesting sync.
1684 * @res_hndl_u: Identifies resource requesting sync.
1685 * @mode: Type of sync to issue (lightweight, heavyweight, global).
1686 *
1687 * The AFU can only take 1 sync command at a time. This routine enforces this
1688 * limitation by using a mutex to provide exclusive access to the AFU during
1689 * the sync. This design point requires calling threads to not be on interrupt
1690 * context due to the possibility of sleeping during concurrent sync operations.
1691 *
1692 * AFU sync operations are only necessary and allowed when the device is
1693 * operating normally. When not operating normally, sync requests can occur as
1694 * part of cleaning up resources associated with an adapter prior to removal.
1695 * In this scenario, these requests are simply ignored (safe due to the AFU
1696 * going away).
1697 *
1698 * Return:
1699 * 0 on success
1700 * -1 on failure
1701 */
1702 int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
1703 res_hndl_t res_hndl_u, u8 mode)
1704 {
1705 struct cxlflash_cfg *cfg = afu->parent;
1706 struct device *dev = &cfg->dev->dev;
1707 struct afu_cmd *cmd = NULL;
1708 char *buf = NULL;
1709 int rc = 0;
1710 static DEFINE_MUTEX(sync_active);
1711
1712 if (cfg->state != STATE_NORMAL) {
1713 pr_debug("%s: Sync not required! (%u)\n", __func__, cfg->state);
1714 return 0;
1715 }
1716
1717 mutex_lock(&sync_active);
1718 atomic_inc(&afu->cmds_active);
1719 buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
1720 if (unlikely(!buf)) {
1721 dev_err(dev, "%s: no memory for command\n", __func__);
1722 rc = -1;
1723 goto out;
1724 }
1725
1726 cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
1727 init_completion(&cmd->cevent);
1728 cmd->parent = afu;
1729
1730 pr_debug("%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
1731
1732 cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
1733 cmd->rcb.ctx_id = afu->ctx_hndl;
1734 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
1735 cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
1736
1737 cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
1738 cmd->rcb.cdb[1] = mode;
1739
1740 /* The cdb is aligned, no unaligned accessors required */
1741 *((__be16 *)&cmd->rcb.cdb[2]) = cpu_to_be16(ctx_hndl_u);
1742 *((__be32 *)&cmd->rcb.cdb[4]) = cpu_to_be32(res_hndl_u);
1743
1744 rc = send_cmd(afu, cmd);
1745 if (unlikely(rc))
1746 goto out;
1747
1748 rc = wait_resp(afu, cmd);
1749 if (unlikely(rc))
1750 rc = -1;
1751 out:
1752 atomic_dec(&afu->cmds_active);
1753 mutex_unlock(&sync_active);
1754 kfree(buf);
1755 pr_debug("%s: returning rc=%d\n", __func__, rc);
1756 return rc;
1757 }
1758
1759 /**
1760 * afu_reset() - resets the AFU
1761 * @cfg: Internal structure associated with the host.
1762 *
1763 * Return: 0 on success, -errno on failure
1764 */
1765 static int afu_reset(struct cxlflash_cfg *cfg)
1766 {
1767 int rc = 0;
1768 /* Stop the context before the reset. Since the context is
1769 * no longer available restart it after the reset is complete
1770 */
1771
1772 term_afu(cfg);
1773
1774 rc = init_afu(cfg);
1775
1776 pr_debug("%s: returning rc=%d\n", __func__, rc);
1777 return rc;
1778 }
1779
1780 /**
1781 * drain_ioctls() - wait until all currently executing ioctls have completed
1782 * @cfg: Internal structure associated with the host.
1783 *
1784 * Obtain write access to read/write semaphore that wraps ioctl
1785 * handling to 'drain' ioctls currently executing.
1786 */
1787 static void drain_ioctls(struct cxlflash_cfg *cfg)
1788 {
1789 down_write(&cfg->ioctl_rwsem);
1790 up_write(&cfg->ioctl_rwsem);
1791 }
1792
1793 /**
1794 * cxlflash_eh_device_reset_handler() - reset a single LUN
1795 * @scp: SCSI command to send.
1796 *
1797 * Return:
1798 * SUCCESS as defined in scsi/scsi.h
1799 * FAILED as defined in scsi/scsi.h
1800 */
1801 static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
1802 {
1803 int rc = SUCCESS;
1804 struct Scsi_Host *host = scp->device->host;
1805 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
1806 struct afu *afu = cfg->afu;
1807 int rcr = 0;
1808
1809 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
1810 "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
1811 host->host_no, scp->device->channel,
1812 scp->device->id, scp->device->lun,
1813 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1814 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1815 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1816 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
1817
1818 retry:
1819 switch (cfg->state) {
1820 case STATE_NORMAL:
1821 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
1822 if (unlikely(rcr))
1823 rc = FAILED;
1824 break;
1825 case STATE_RESET:
1826 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
1827 goto retry;
1828 default:
1829 rc = FAILED;
1830 break;
1831 }
1832
1833 pr_debug("%s: returning rc=%d\n", __func__, rc);
1834 return rc;
1835 }
1836
1837 /**
1838 * cxlflash_eh_host_reset_handler() - reset the host adapter
1839 * @scp: SCSI command from stack identifying host.
1840 *
1841 * Following a reset, the state is evaluated again in case an EEH occurred
1842 * during the reset. In such a scenario, the host reset will either yield
1843 * until the EEH recovery is complete or return success or failure based
1844 * upon the current device state.
1845 *
1846 * Return:
1847 * SUCCESS as defined in scsi/scsi.h
1848 * FAILED as defined in scsi/scsi.h
1849 */
1850 static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
1851 {
1852 int rc = SUCCESS;
1853 int rcr = 0;
1854 struct Scsi_Host *host = scp->device->host;
1855 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
1856
1857 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
1858 "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
1859 host->host_no, scp->device->channel,
1860 scp->device->id, scp->device->lun,
1861 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1862 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1863 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1864 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
1865
1866 switch (cfg->state) {
1867 case STATE_NORMAL:
1868 cfg->state = STATE_RESET;
1869 drain_ioctls(cfg);
1870 cxlflash_mark_contexts_error(cfg);
1871 rcr = afu_reset(cfg);
1872 if (rcr) {
1873 rc = FAILED;
1874 cfg->state = STATE_FAILTERM;
1875 } else
1876 cfg->state = STATE_NORMAL;
1877 wake_up_all(&cfg->reset_waitq);
1878 ssleep(1);
1879 /* fall through */
1880 case STATE_RESET:
1881 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
1882 if (cfg->state == STATE_NORMAL)
1883 break;
1884 /* fall through */
1885 default:
1886 rc = FAILED;
1887 break;
1888 }
1889
1890 pr_debug("%s: returning rc=%d\n", __func__, rc);
1891 return rc;
1892 }
1893
1894 /**
1895 * cxlflash_change_queue_depth() - change the queue depth for the device
1896 * @sdev: SCSI device destined for queue depth change.
1897 * @qdepth: Requested queue depth value to set.
1898 *
1899 * The requested queue depth is capped to the maximum supported value.
1900 *
1901 * Return: The actual queue depth set.
1902 */
1903 static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
1904 {
1905
1906 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
1907 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
1908
1909 scsi_change_queue_depth(sdev, qdepth);
1910 return sdev->queue_depth;
1911 }
1912
1913 /**
1914 * cxlflash_show_port_status() - queries and presents the current port status
1915 * @port: Desired port for status reporting.
1916 * @afu: AFU owning the specified port.
1917 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
1918 *
1919 * Return: The size of the ASCII string returned in @buf.
1920 */
1921 static ssize_t cxlflash_show_port_status(u32 port, struct afu *afu, char *buf)
1922 {
1923 char *disp_status;
1924 u64 status;
1925 __be64 __iomem *fc_regs;
1926
1927 if (port >= NUM_FC_PORTS)
1928 return 0;
1929
1930 fc_regs = &afu->afu_map->global.fc_regs[port][0];
1931 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
1932 status &= FC_MTIP_STATUS_MASK;
1933
1934 if (status == FC_MTIP_STATUS_ONLINE)
1935 disp_status = "online";
1936 else if (status == FC_MTIP_STATUS_OFFLINE)
1937 disp_status = "offline";
1938 else
1939 disp_status = "unknown";
1940
1941 return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
1942 }
1943
1944 /**
1945 * port0_show() - queries and presents the current status of port 0
1946 * @dev: Generic device associated with the host owning the port.
1947 * @attr: Device attribute representing the port.
1948 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
1949 *
1950 * Return: The size of the ASCII string returned in @buf.
1951 */
1952 static ssize_t port0_show(struct device *dev,
1953 struct device_attribute *attr,
1954 char *buf)
1955 {
1956 struct Scsi_Host *shost = class_to_shost(dev);
1957 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
1958 struct afu *afu = cfg->afu;
1959
1960 return cxlflash_show_port_status(0, afu, buf);
1961 }
1962
1963 /**
1964 * port1_show() - queries and presents the current status of port 1
1965 * @dev: Generic device associated with the host owning the port.
1966 * @attr: Device attribute representing the port.
1967 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
1968 *
1969 * Return: The size of the ASCII string returned in @buf.
1970 */
1971 static ssize_t port1_show(struct device *dev,
1972 struct device_attribute *attr,
1973 char *buf)
1974 {
1975 struct Scsi_Host *shost = class_to_shost(dev);
1976 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
1977 struct afu *afu = cfg->afu;
1978
1979 return cxlflash_show_port_status(1, afu, buf);
1980 }
1981
1982 /**
1983 * lun_mode_show() - presents the current LUN mode of the host
1984 * @dev: Generic device associated with the host.
1985 * @attr: Device attribute representing the LUN mode.
1986 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
1987 *
1988 * Return: The size of the ASCII string returned in @buf.
1989 */
1990 static ssize_t lun_mode_show(struct device *dev,
1991 struct device_attribute *attr, char *buf)
1992 {
1993 struct Scsi_Host *shost = class_to_shost(dev);
1994 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
1995 struct afu *afu = cfg->afu;
1996
1997 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
1998 }
1999
2000 /**
2001 * lun_mode_store() - sets the LUN mode of the host
2002 * @dev: Generic device associated with the host.
2003 * @attr: Device attribute representing the LUN mode.
2004 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2005 * @count: Length of data resizing in @buf.
2006 *
2007 * The CXL Flash AFU supports a dummy LUN mode where the external
2008 * links and storage are not required. Space on the FPGA is used
2009 * to create 1 or 2 small LUNs which are presented to the system
2010 * as if they were a normal storage device. This feature is useful
2011 * during development and also provides manufacturing with a way
2012 * to test the AFU without an actual device.
2013 *
2014 * 0 = external LUN[s] (default)
2015 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2016 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2017 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2018 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2019 *
2020 * Return: The size of the ASCII string returned in @buf.
2021 */
2022 static ssize_t lun_mode_store(struct device *dev,
2023 struct device_attribute *attr,
2024 const char *buf, size_t count)
2025 {
2026 struct Scsi_Host *shost = class_to_shost(dev);
2027 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2028 struct afu *afu = cfg->afu;
2029 int rc;
2030 u32 lun_mode;
2031
2032 rc = kstrtouint(buf, 10, &lun_mode);
2033 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
2034 afu->internal_lun = lun_mode;
2035
2036 /*
2037 * When configured for internal LUN, there is only one channel,
2038 * channel number 0, else there will be 2 (default).
2039 */
2040 if (afu->internal_lun)
2041 shost->max_channel = 0;
2042 else
2043 shost->max_channel = NUM_FC_PORTS - 1;
2044
2045 afu_reset(cfg);
2046 scsi_scan_host(cfg->host);
2047 }
2048
2049 return count;
2050 }
2051
2052 /**
2053 * ioctl_version_show() - presents the current ioctl version of the host
2054 * @dev: Generic device associated with the host.
2055 * @attr: Device attribute representing the ioctl version.
2056 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2057 *
2058 * Return: The size of the ASCII string returned in @buf.
2059 */
2060 static ssize_t ioctl_version_show(struct device *dev,
2061 struct device_attribute *attr, char *buf)
2062 {
2063 return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
2064 }
2065
2066 /**
2067 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2068 * @port: Desired port for status reporting.
2069 * @afu: AFU owning the specified port.
2070 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2071 *
2072 * Return: The size of the ASCII string returned in @buf.
2073 */
2074 static ssize_t cxlflash_show_port_lun_table(u32 port,
2075 struct afu *afu,
2076 char *buf)
2077 {
2078 int i;
2079 ssize_t bytes = 0;
2080 __be64 __iomem *fc_port;
2081
2082 if (port >= NUM_FC_PORTS)
2083 return 0;
2084
2085 fc_port = &afu->afu_map->global.fc_port[port][0];
2086
2087 for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2088 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
2089 "%03d: %016llX\n", i, readq_be(&fc_port[i]));
2090 return bytes;
2091 }
2092
2093 /**
2094 * port0_lun_table_show() - presents the current LUN table of port 0
2095 * @dev: Generic device associated with the host owning the port.
2096 * @attr: Device attribute representing the port.
2097 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2098 *
2099 * Return: The size of the ASCII string returned in @buf.
2100 */
2101 static ssize_t port0_lun_table_show(struct device *dev,
2102 struct device_attribute *attr,
2103 char *buf)
2104 {
2105 struct Scsi_Host *shost = class_to_shost(dev);
2106 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2107 struct afu *afu = cfg->afu;
2108
2109 return cxlflash_show_port_lun_table(0, afu, buf);
2110 }
2111
2112 /**
2113 * port1_lun_table_show() - presents the current LUN table of port 1
2114 * @dev: Generic device associated with the host owning the port.
2115 * @attr: Device attribute representing the port.
2116 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2117 *
2118 * Return: The size of the ASCII string returned in @buf.
2119 */
2120 static ssize_t port1_lun_table_show(struct device *dev,
2121 struct device_attribute *attr,
2122 char *buf)
2123 {
2124 struct Scsi_Host *shost = class_to_shost(dev);
2125 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2126 struct afu *afu = cfg->afu;
2127
2128 return cxlflash_show_port_lun_table(1, afu, buf);
2129 }
2130
2131 /**
2132 * mode_show() - presents the current mode of the device
2133 * @dev: Generic device associated with the device.
2134 * @attr: Device attribute representing the device mode.
2135 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2136 *
2137 * Return: The size of the ASCII string returned in @buf.
2138 */
2139 static ssize_t mode_show(struct device *dev,
2140 struct device_attribute *attr, char *buf)
2141 {
2142 struct scsi_device *sdev = to_scsi_device(dev);
2143
2144 return scnprintf(buf, PAGE_SIZE, "%s\n",
2145 sdev->hostdata ? "superpipe" : "legacy");
2146 }
2147
2148 /*
2149 * Host attributes
2150 */
2151 static DEVICE_ATTR_RO(port0);
2152 static DEVICE_ATTR_RO(port1);
2153 static DEVICE_ATTR_RW(lun_mode);
2154 static DEVICE_ATTR_RO(ioctl_version);
2155 static DEVICE_ATTR_RO(port0_lun_table);
2156 static DEVICE_ATTR_RO(port1_lun_table);
2157
2158 static struct device_attribute *cxlflash_host_attrs[] = {
2159 &dev_attr_port0,
2160 &dev_attr_port1,
2161 &dev_attr_lun_mode,
2162 &dev_attr_ioctl_version,
2163 &dev_attr_port0_lun_table,
2164 &dev_attr_port1_lun_table,
2165 NULL
2166 };
2167
2168 /*
2169 * Device attributes
2170 */
2171 static DEVICE_ATTR_RO(mode);
2172
2173 static struct device_attribute *cxlflash_dev_attrs[] = {
2174 &dev_attr_mode,
2175 NULL
2176 };
2177
2178 /*
2179 * Host template
2180 */
2181 static struct scsi_host_template driver_template = {
2182 .module = THIS_MODULE,
2183 .name = CXLFLASH_ADAPTER_NAME,
2184 .info = cxlflash_driver_info,
2185 .ioctl = cxlflash_ioctl,
2186 .proc_name = CXLFLASH_NAME,
2187 .queuecommand = cxlflash_queuecommand,
2188 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
2189 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
2190 .change_queue_depth = cxlflash_change_queue_depth,
2191 .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
2192 .can_queue = CXLFLASH_MAX_CMDS,
2193 .cmd_size = sizeof(struct afu_cmd) + __alignof__(struct afu_cmd) - 1,
2194 .this_id = -1,
2195 .sg_tablesize = 1, /* No scatter gather support */
2196 .max_sectors = CXLFLASH_MAX_SECTORS,
2197 .use_clustering = ENABLE_CLUSTERING,
2198 .shost_attrs = cxlflash_host_attrs,
2199 .sdev_attrs = cxlflash_dev_attrs,
2200 };
2201
2202 /*
2203 * Device dependent values
2204 */
2205 static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
2206 0ULL };
2207 static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
2208 CXLFLASH_NOTIFY_SHUTDOWN };
2209
2210 /*
2211 * PCI device binding table
2212 */
2213 static struct pci_device_id cxlflash_pci_table[] = {
2214 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
2215 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
2216 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
2217 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
2218 {}
2219 };
2220
2221 MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
2222
2223 /**
2224 * cxlflash_worker_thread() - work thread handler for the AFU
2225 * @work: Work structure contained within cxlflash associated with host.
2226 *
2227 * Handles the following events:
2228 * - Link reset which cannot be performed on interrupt context due to
2229 * blocking up to a few seconds
2230 * - Rescan the host
2231 */
2232 static void cxlflash_worker_thread(struct work_struct *work)
2233 {
2234 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
2235 work_q);
2236 struct afu *afu = cfg->afu;
2237 struct device *dev = &cfg->dev->dev;
2238 int port;
2239 ulong lock_flags;
2240
2241 /* Avoid MMIO if the device has failed */
2242
2243 if (cfg->state != STATE_NORMAL)
2244 return;
2245
2246 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2247
2248 if (cfg->lr_state == LINK_RESET_REQUIRED) {
2249 port = cfg->lr_port;
2250 if (port < 0)
2251 dev_err(dev, "%s: invalid port index %d\n",
2252 __func__, port);
2253 else {
2254 spin_unlock_irqrestore(cfg->host->host_lock,
2255 lock_flags);
2256
2257 /* The reset can block... */
2258 afu_link_reset(afu, port,
2259 &afu->afu_map->global.fc_regs[port][0]);
2260 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2261 }
2262
2263 cfg->lr_state = LINK_RESET_COMPLETE;
2264 }
2265
2266 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
2267
2268 if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
2269 scsi_scan_host(cfg->host);
2270 kref_put(&afu->mapcount, afu_unmap);
2271 }
2272
2273 /**
2274 * cxlflash_probe() - PCI entry point to add host
2275 * @pdev: PCI device associated with the host.
2276 * @dev_id: PCI device id associated with device.
2277 *
2278 * Return: 0 on success, -errno on failure
2279 */
2280 static int cxlflash_probe(struct pci_dev *pdev,
2281 const struct pci_device_id *dev_id)
2282 {
2283 struct Scsi_Host *host;
2284 struct cxlflash_cfg *cfg = NULL;
2285 struct dev_dependent_vals *ddv;
2286 int rc = 0;
2287
2288 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
2289 __func__, pdev->irq);
2290
2291 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
2292 driver_template.max_sectors = ddv->max_sectors;
2293
2294 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
2295 if (!host) {
2296 dev_err(&pdev->dev, "%s: call to scsi_host_alloc failed!\n",
2297 __func__);
2298 rc = -ENOMEM;
2299 goto out;
2300 }
2301
2302 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
2303 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
2304 host->max_channel = NUM_FC_PORTS - 1;
2305 host->unique_id = host->host_no;
2306 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
2307
2308 cfg = (struct cxlflash_cfg *)host->hostdata;
2309 cfg->host = host;
2310 rc = alloc_mem(cfg);
2311 if (rc) {
2312 dev_err(&pdev->dev, "%s: call to alloc_mem failed!\n",
2313 __func__);
2314 rc = -ENOMEM;
2315 scsi_host_put(cfg->host);
2316 goto out;
2317 }
2318
2319 cfg->init_state = INIT_STATE_NONE;
2320 cfg->dev = pdev;
2321 cfg->cxl_fops = cxlflash_cxl_fops;
2322
2323 /*
2324 * The promoted LUNs move to the top of the LUN table. The rest stay
2325 * on the bottom half. The bottom half grows from the end
2326 * (index = 255), whereas the top half grows from the beginning
2327 * (index = 0).
2328 */
2329 cfg->promote_lun_index = 0;
2330 cfg->last_lun_index[0] = CXLFLASH_NUM_VLUNS/2 - 1;
2331 cfg->last_lun_index[1] = CXLFLASH_NUM_VLUNS/2 - 1;
2332
2333 cfg->dev_id = (struct pci_device_id *)dev_id;
2334
2335 init_waitqueue_head(&cfg->tmf_waitq);
2336 init_waitqueue_head(&cfg->reset_waitq);
2337
2338 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
2339 cfg->lr_state = LINK_RESET_INVALID;
2340 cfg->lr_port = -1;
2341 spin_lock_init(&cfg->tmf_slock);
2342 mutex_init(&cfg->ctx_tbl_list_mutex);
2343 mutex_init(&cfg->ctx_recovery_mutex);
2344 init_rwsem(&cfg->ioctl_rwsem);
2345 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
2346 INIT_LIST_HEAD(&cfg->lluns);
2347
2348 pci_set_drvdata(pdev, cfg);
2349
2350 cfg->cxl_afu = cxl_pci_to_afu(pdev);
2351
2352 rc = init_pci(cfg);
2353 if (rc) {
2354 dev_err(&pdev->dev, "%s: call to init_pci "
2355 "failed rc=%d!\n", __func__, rc);
2356 goto out_remove;
2357 }
2358 cfg->init_state = INIT_STATE_PCI;
2359
2360 rc = init_afu(cfg);
2361 if (rc) {
2362 dev_err(&pdev->dev, "%s: call to init_afu "
2363 "failed rc=%d!\n", __func__, rc);
2364 goto out_remove;
2365 }
2366 cfg->init_state = INIT_STATE_AFU;
2367
2368 rc = init_scsi(cfg);
2369 if (rc) {
2370 dev_err(&pdev->dev, "%s: call to init_scsi "
2371 "failed rc=%d!\n", __func__, rc);
2372 goto out_remove;
2373 }
2374 cfg->init_state = INIT_STATE_SCSI;
2375
2376 out:
2377 pr_debug("%s: returning rc=%d\n", __func__, rc);
2378 return rc;
2379
2380 out_remove:
2381 cxlflash_remove(pdev);
2382 goto out;
2383 }
2384
2385 /**
2386 * cxlflash_pci_error_detected() - called when a PCI error is detected
2387 * @pdev: PCI device struct.
2388 * @state: PCI channel state.
2389 *
2390 * When an EEH occurs during an active reset, wait until the reset is
2391 * complete and then take action based upon the device state.
2392 *
2393 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2394 */
2395 static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
2396 pci_channel_state_t state)
2397 {
2398 int rc = 0;
2399 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2400 struct device *dev = &cfg->dev->dev;
2401
2402 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
2403
2404 switch (state) {
2405 case pci_channel_io_frozen:
2406 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2407 if (cfg->state == STATE_FAILTERM)
2408 return PCI_ERS_RESULT_DISCONNECT;
2409
2410 cfg->state = STATE_RESET;
2411 scsi_block_requests(cfg->host);
2412 drain_ioctls(cfg);
2413 rc = cxlflash_mark_contexts_error(cfg);
2414 if (unlikely(rc))
2415 dev_err(dev, "%s: Failed to mark user contexts!(%d)\n",
2416 __func__, rc);
2417 term_afu(cfg);
2418 return PCI_ERS_RESULT_NEED_RESET;
2419 case pci_channel_io_perm_failure:
2420 cfg->state = STATE_FAILTERM;
2421 wake_up_all(&cfg->reset_waitq);
2422 scsi_unblock_requests(cfg->host);
2423 return PCI_ERS_RESULT_DISCONNECT;
2424 default:
2425 break;
2426 }
2427 return PCI_ERS_RESULT_NEED_RESET;
2428 }
2429
2430 /**
2431 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2432 * @pdev: PCI device struct.
2433 *
2434 * This routine is called by the pci error recovery code after the PCI
2435 * slot has been reset, just before we should resume normal operations.
2436 *
2437 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2438 */
2439 static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
2440 {
2441 int rc = 0;
2442 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2443 struct device *dev = &cfg->dev->dev;
2444
2445 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2446
2447 rc = init_afu(cfg);
2448 if (unlikely(rc)) {
2449 dev_err(dev, "%s: EEH recovery failed! (%d)\n", __func__, rc);
2450 return PCI_ERS_RESULT_DISCONNECT;
2451 }
2452
2453 return PCI_ERS_RESULT_RECOVERED;
2454 }
2455
2456 /**
2457 * cxlflash_pci_resume() - called when normal operation can resume
2458 * @pdev: PCI device struct
2459 */
2460 static void cxlflash_pci_resume(struct pci_dev *pdev)
2461 {
2462 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2463 struct device *dev = &cfg->dev->dev;
2464
2465 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2466
2467 cfg->state = STATE_NORMAL;
2468 wake_up_all(&cfg->reset_waitq);
2469 scsi_unblock_requests(cfg->host);
2470 }
2471
2472 static const struct pci_error_handlers cxlflash_err_handler = {
2473 .error_detected = cxlflash_pci_error_detected,
2474 .slot_reset = cxlflash_pci_slot_reset,
2475 .resume = cxlflash_pci_resume,
2476 };
2477
2478 /*
2479 * PCI device structure
2480 */
2481 static struct pci_driver cxlflash_driver = {
2482 .name = CXLFLASH_NAME,
2483 .id_table = cxlflash_pci_table,
2484 .probe = cxlflash_probe,
2485 .remove = cxlflash_remove,
2486 .shutdown = cxlflash_remove,
2487 .err_handler = &cxlflash_err_handler,
2488 };
2489
2490 /**
2491 * init_cxlflash() - module entry point
2492 *
2493 * Return: 0 on success, -errno on failure
2494 */
2495 static int __init init_cxlflash(void)
2496 {
2497 pr_info("%s: %s\n", __func__, CXLFLASH_ADAPTER_NAME);
2498
2499 cxlflash_list_init();
2500
2501 return pci_register_driver(&cxlflash_driver);
2502 }
2503
2504 /**
2505 * exit_cxlflash() - module exit point
2506 */
2507 static void __exit exit_cxlflash(void)
2508 {
2509 cxlflash_term_global_luns();
2510 cxlflash_free_errpage();
2511
2512 pci_unregister_driver(&cxlflash_driver);
2513 }
2514
2515 module_init(init_cxlflash);
2516 module_exit(exit_cxlflash);