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