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