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