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