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