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