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