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