blob: c60936fb70bb9ac7b4fee0471933a932d3f43f77 [file] [log] [blame]
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001/*
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>
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050026#include <uapi/scsi/cxlflash_ioctl.h>
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050027
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
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050037/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050038 * 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{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060046 struct afu *afu = cmd->parent;
47 struct cxlflash_cfg *cfg = afu->parent;
48 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050049 struct sisl_ioarcb *ioarcb;
50 struct sisl_ioasa *ioasa;
Matthew R. Ochs83960122015-10-21 15:13:29 -050051 u32 resid;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050052
53 if (unlikely(!cmd))
54 return;
55
56 ioarcb = &(cmd->rcb);
57 ioasa = &(cmd->sa);
58
59 if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) {
Matthew R. Ochs83960122015-10-21 15:13:29 -050060 resid = ioasa->resid;
61 scsi_set_resid(scp, resid);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060062 dev_dbg(dev, "%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
63 __func__, cmd, scp, resid);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050064 }
65
66 if (ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060067 dev_dbg(dev, "%s: cmd underrun cmd = %p scp = %p\n",
68 __func__, cmd, scp);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050069 scp->result = (DID_ERROR << 16);
70 }
71
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060072 dev_dbg(dev, "%s: cmd failed afu_rc=%02x scsi_rc=%02x fc_rc=%02x "
73 "afu_extra=%02x scsi_extra=%02x fc_extra=%02x\n", __func__,
74 ioasa->rc.afu_rc, ioasa->rc.scsi_rc, ioasa->rc.fc_rc,
75 ioasa->afu_extra, ioasa->scsi_extra, ioasa->fc_extra);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050076
77 if (ioasa->rc.scsi_rc) {
78 /* We have a SCSI status */
79 if (ioasa->rc.flags & SISL_RC_FLAGS_SENSE_VALID) {
80 memcpy(scp->sense_buffer, ioasa->sense_data,
81 SISL_SENSE_DATA_LEN);
82 scp->result = ioasa->rc.scsi_rc;
83 } else
84 scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16);
85 }
86
87 /*
88 * We encountered an error. Set scp->result based on nature
89 * of error.
90 */
91 if (ioasa->rc.fc_rc) {
92 /* We have an FC status */
93 switch (ioasa->rc.fc_rc) {
94 case SISL_FC_RC_LINKDOWN:
95 scp->result = (DID_REQUEUE << 16);
96 break;
97 case SISL_FC_RC_RESID:
98 /* This indicates an FCP resid underrun */
99 if (!(ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN)) {
100 /* If the SISL_RC_FLAGS_OVERRUN flag was set,
101 * then we will handle this error else where.
102 * If not then we must handle it here.
Matthew R. Ochs83960122015-10-21 15:13:29 -0500103 * This is probably an AFU bug.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500104 */
105 scp->result = (DID_ERROR << 16);
106 }
107 break;
108 case SISL_FC_RC_RESIDERR:
109 /* Resid mismatch between adapter and device */
110 case SISL_FC_RC_TGTABORT:
111 case SISL_FC_RC_ABORTOK:
112 case SISL_FC_RC_ABORTFAIL:
113 case SISL_FC_RC_NOLOGI:
114 case SISL_FC_RC_ABORTPEND:
115 case SISL_FC_RC_WRABORTPEND:
116 case SISL_FC_RC_NOEXP:
117 case SISL_FC_RC_INUSE:
118 scp->result = (DID_ERROR << 16);
119 break;
120 }
121 }
122
123 if (ioasa->rc.afu_rc) {
124 /* We have an AFU error */
125 switch (ioasa->rc.afu_rc) {
126 case SISL_AFU_RC_NO_CHANNELS:
Matthew R. Ochs83960122015-10-21 15:13:29 -0500127 scp->result = (DID_NO_CONNECT << 16);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500128 break;
129 case SISL_AFU_RC_DATA_DMA_ERR:
130 switch (ioasa->afu_extra) {
131 case SISL_AFU_DMA_ERR_PAGE_IN:
132 /* Retry */
133 scp->result = (DID_IMM_RETRY << 16);
134 break;
135 case SISL_AFU_DMA_ERR_INVALID_EA:
136 default:
137 scp->result = (DID_ERROR << 16);
138 }
139 break;
140 case SISL_AFU_RC_OUT_OF_DATA_BUFS:
141 /* Retry */
142 scp->result = (DID_ALLOC_FAILURE << 16);
143 break;
144 default:
145 scp->result = (DID_ERROR << 16);
146 }
147 }
148}
149
150/**
151 * cmd_complete() - command completion handler
152 * @cmd: AFU command that has completed.
153 *
154 * Prepares and submits command that has either completed or timed out to
155 * the SCSI stack. Checks AFU command back into command pool for non-internal
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600156 * (cmd->scp populated) commands.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500157 */
158static void cmd_complete(struct afu_cmd *cmd)
159{
160 struct scsi_cmnd *scp;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500161 ulong lock_flags;
162 struct afu *afu = cmd->parent;
163 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600164 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500165 bool cmd_is_tmf;
166
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600167 if (cmd->scp) {
168 scp = cmd->scp;
Matthew R. Ochs83960122015-10-21 15:13:29 -0500169 if (unlikely(cmd->sa.ioasc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500170 process_cmd_err(cmd, scp);
171 else
172 scp->result = (DID_OK << 16);
173
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500174 cmd_is_tmf = cmd->cmd_tmf;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500175
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600176 dev_dbg_ratelimited(dev, "%s:scp=%p result=%08x ioasc=%08x\n",
177 __func__, scp, scp->result, cmd->sa.ioasc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500178
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500179 scp->scsi_done(scp);
180
181 if (cmd_is_tmf) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500182 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500183 cfg->tmf_active = false;
184 wake_up_all_locked(&cfg->tmf_waitq);
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500185 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500186 }
187 } else
188 complete(&cmd->cevent);
189}
190
191/**
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600192 * context_reset() - reset command owner context via specified register
Matthew R. Ochs15305512015-10-21 15:12:10 -0500193 * @cmd: AFU command that timed out.
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600194 * @reset_reg: MMIO register to perform reset.
Matthew R. Ochs15305512015-10-21 15:12:10 -0500195 */
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600196static void context_reset(struct afu_cmd *cmd, __be64 __iomem *reset_reg)
Matthew R. Ochs15305512015-10-21 15:12:10 -0500197{
198 int nretry = 0;
199 u64 rrin = 0x1;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500200 struct afu *afu = cmd->parent;
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600201 struct cxlflash_cfg *cfg = afu->parent;
202 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500203
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600204 dev_dbg(dev, "%s: cmd=%p\n", __func__, cmd);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500205
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600206 writeq_be(rrin, reset_reg);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500207 do {
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600208 rrin = readq_be(reset_reg);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500209 if (rrin != 0x1)
210 break;
211 /* Double delay each time */
Manoj N. Kumarea765432016-03-25 14:26:49 -0500212 udelay(1 << nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500213 } while (nretry++ < MC_ROOM_RETRY_CNT);
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600214
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600215 dev_dbg(dev, "%s: returning rrin=%016llx nretry=%d\n",
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600216 __func__, rrin, nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500217}
218
219/**
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600220 * context_reset_ioarrin() - reset command owner context via IOARRIN register
221 * @cmd: AFU command that timed out.
222 */
223static void context_reset_ioarrin(struct afu_cmd *cmd)
224{
225 struct afu *afu = cmd->parent;
226
227 context_reset(cmd, &afu->host_map->ioarrin);
228}
229
230/**
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600231 * context_reset_sq() - reset command owner context w/ SQ Context Reset register
232 * @cmd: AFU command that timed out.
233 */
234static void context_reset_sq(struct afu_cmd *cmd)
235{
236 struct afu *afu = cmd->parent;
237
238 context_reset(cmd, &afu->host_map->sq_ctx_reset);
239}
240
241/**
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600242 * send_cmd_ioarrin() - sends an AFU command via IOARRIN register
Matthew R. Ochs15305512015-10-21 15:12:10 -0500243 * @afu: AFU associated with the host.
244 * @cmd: AFU command to send.
245 *
246 * Return:
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500247 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochs15305512015-10-21 15:12:10 -0500248 */
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600249static int send_cmd_ioarrin(struct afu *afu, struct afu_cmd *cmd)
Matthew R. Ochs15305512015-10-21 15:12:10 -0500250{
251 struct cxlflash_cfg *cfg = afu->parent;
252 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500253 int rc = 0;
Uma Krishnan11f7b182016-11-28 18:41:45 -0600254 s64 room;
255 ulong lock_flags;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500256
257 /*
Uma Krishnan11f7b182016-11-28 18:41:45 -0600258 * To avoid the performance penalty of MMIO, spread the update of
259 * 'room' over multiple commands.
Matthew R. Ochs15305512015-10-21 15:12:10 -0500260 */
Uma Krishnan11f7b182016-11-28 18:41:45 -0600261 spin_lock_irqsave(&afu->rrin_slock, lock_flags);
262 if (--afu->room < 0) {
263 room = readq_be(&afu->host_map->cmd_room);
264 if (room <= 0) {
265 dev_dbg_ratelimited(dev, "%s: no cmd_room to send "
266 "0x%02X, room=0x%016llX\n",
267 __func__, cmd->rcb.cdb[0], room);
268 afu->room = 0;
269 rc = SCSI_MLQUEUE_HOST_BUSY;
270 goto out;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500271 }
Uma Krishnan11f7b182016-11-28 18:41:45 -0600272 afu->room = room - 1;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500273 }
274
Matthew R. Ochs15305512015-10-21 15:12:10 -0500275 writeq_be((u64)&cmd->rcb, &afu->host_map->ioarrin);
276out:
Uma Krishnan11f7b182016-11-28 18:41:45 -0600277 spin_unlock_irqrestore(&afu->rrin_slock, lock_flags);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600278 dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx rc=%d\n", __func__,
279 cmd, cmd->rcb.data_len, cmd->rcb.data_ea, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500280 return rc;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500281}
282
283/**
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600284 * send_cmd_sq() - sends an AFU command via SQ ring
285 * @afu: AFU associated with the host.
286 * @cmd: AFU command to send.
287 *
288 * Return:
289 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
290 */
291static int send_cmd_sq(struct afu *afu, struct afu_cmd *cmd)
292{
293 struct cxlflash_cfg *cfg = afu->parent;
294 struct device *dev = &cfg->dev->dev;
295 int rc = 0;
296 int newval;
297 ulong lock_flags;
298
299 newval = atomic_dec_if_positive(&afu->hsq_credits);
300 if (newval <= 0) {
301 rc = SCSI_MLQUEUE_HOST_BUSY;
302 goto out;
303 }
304
305 cmd->rcb.ioasa = &cmd->sa;
306
307 spin_lock_irqsave(&afu->hsq_slock, lock_flags);
308
309 *afu->hsq_curr = cmd->rcb;
310 if (afu->hsq_curr < afu->hsq_end)
311 afu->hsq_curr++;
312 else
313 afu->hsq_curr = afu->hsq_start;
314 writeq_be((u64)afu->hsq_curr, &afu->host_map->sq_tail);
315
316 spin_unlock_irqrestore(&afu->hsq_slock, lock_flags);
317out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600318 dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx ioasa=%p rc=%d curr=%p "
319 "head=%016llx tail=%016llx\n", __func__, cmd, cmd->rcb.data_len,
320 cmd->rcb.data_ea, cmd->rcb.ioasa, rc, afu->hsq_curr,
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600321 readq_be(&afu->host_map->sq_head),
322 readq_be(&afu->host_map->sq_tail));
323 return rc;
324}
325
326/**
Matthew R. Ochs15305512015-10-21 15:12:10 -0500327 * wait_resp() - polls for a response or timeout to a sent AFU command
328 * @afu: AFU associated with the host.
329 * @cmd: AFU command that was sent.
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600330 *
331 * Return:
332 * 0 on success, -1 on timeout/error
Matthew R. Ochs15305512015-10-21 15:12:10 -0500333 */
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600334static int wait_resp(struct afu *afu, struct afu_cmd *cmd)
Matthew R. Ochs15305512015-10-21 15:12:10 -0500335{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600336 struct cxlflash_cfg *cfg = afu->parent;
337 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600338 int rc = 0;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500339 ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
340
341 timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600342 if (!timeout) {
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600343 afu->context_reset(cmd);
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600344 rc = -1;
345 }
Matthew R. Ochs15305512015-10-21 15:12:10 -0500346
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600347 if (unlikely(cmd->sa.ioasc != 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600348 dev_err(dev, "%s: cmd %02x failed, ioasc=%08x\n",
349 __func__, cmd->rcb.cdb[0], cmd->sa.ioasc);
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600350 rc = -1;
351 }
352
353 return rc;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500354}
355
356/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500357 * send_tmf() - sends a Task Management Function (TMF)
358 * @afu: AFU to checkout from.
359 * @scp: SCSI command from stack.
360 * @tmfcmd: TMF command to send.
361 *
362 * Return:
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500363 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500364 */
365static int send_tmf(struct afu *afu, struct scsi_cmnd *scp, u64 tmfcmd)
366{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600367 struct cxlflash_cfg *cfg = shost_priv(scp->device->host);
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600368 struct afu_cmd *cmd = sc_to_afucz(scp);
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500369 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500370 ulong lock_flags;
371 int rc = 0;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500372 ulong to;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500373
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500374 /* When Task Management Function is active do not send another */
375 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500376 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500377 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
378 !cfg->tmf_active,
379 cfg->tmf_slock);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500380 cfg->tmf_active = true;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500381 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500382
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600383 cmd->scp = scp;
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600384 cmd->parent = afu;
385 cmd->cmd_tmf = true;
386
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500387 cmd->rcb.ctx_id = afu->ctx_hndl;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600388 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -0500389 cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500390 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500391 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600392 SISL_REQ_FLAGS_SUP_UNDERRUN |
393 SISL_REQ_FLAGS_TMF_CMD);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500394 memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
395
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600396 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500397 if (unlikely(rc)) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500398 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500399 cfg->tmf_active = false;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500400 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500401 goto out;
402 }
403
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500404 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
405 to = msecs_to_jiffies(5000);
406 to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
407 !cfg->tmf_active,
408 cfg->tmf_slock,
409 to);
410 if (!to) {
411 cfg->tmf_active = false;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600412 dev_err(dev, "%s: TMF timed out\n", __func__);
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500413 rc = -1;
414 }
415 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500416out:
417 return rc;
418}
419
420/**
421 * cxlflash_driver_info() - information handler for this host driver
422 * @host: SCSI host associated with device.
423 *
424 * Return: A string describing the device.
425 */
426static const char *cxlflash_driver_info(struct Scsi_Host *host)
427{
428 return CXLFLASH_ADAPTER_NAME;
429}
430
431/**
432 * cxlflash_queuecommand() - sends a mid-layer request
433 * @host: SCSI host associated with device.
434 * @scp: SCSI command to send.
435 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500436 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500437 */
438static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
439{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600440 struct cxlflash_cfg *cfg = shost_priv(host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500441 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500442 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600443 struct afu_cmd *cmd = sc_to_afucz(scp);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600444 struct scatterlist *sg = scsi_sglist(scp);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600445 u16 req_flags = SISL_REQ_FLAGS_SUP_UNDERRUN;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500446 ulong lock_flags;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500447 int rc = 0;
448
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500449 dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600450 "cdb=(%08x-%08x-%08x-%08x)\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500451 __func__, scp, host->host_no, scp->device->channel,
452 scp->device->id, scp->device->lun,
453 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
454 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
455 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
456 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500457
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500458 /*
459 * If a Task Management Function is active, wait for it to complete
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500460 * before continuing with regular commands.
461 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500462 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500463 if (cfg->tmf_active) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500464 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500465 rc = SCSI_MLQUEUE_HOST_BUSY;
466 goto out;
467 }
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500468 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500469
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500470 switch (cfg->state) {
Matthew R. Ochs323e3342017-04-12 14:14:51 -0500471 case STATE_PROBING:
472 case STATE_PROBED:
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500473 case STATE_RESET:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600474 dev_dbg_ratelimited(dev, "%s: device is in reset\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500475 rc = SCSI_MLQUEUE_HOST_BUSY;
476 goto out;
477 case STATE_FAILTERM:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600478 dev_dbg_ratelimited(dev, "%s: device has failed\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500479 scp->result = (DID_NO_CONNECT << 16);
480 scp->scsi_done(scp);
481 rc = 0;
482 goto out;
483 default:
484 break;
485 }
486
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600487 if (likely(sg)) {
Matthew R. Ochs50b787f2017-04-12 14:15:02 -0500488 cmd->rcb.data_len = sg->length;
489 cmd->rcb.data_ea = (uintptr_t)sg_virt(sg);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600490 }
491
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600492 cmd->scp = scp;
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600493 cmd->parent = afu;
494
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500495 cmd->rcb.ctx_id = afu->ctx_hndl;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600496 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -0500497 cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500498 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
499
500 if (scp->sc_data_direction == DMA_TO_DEVICE)
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600501 req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500502
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600503 cmd->rcb.req_flags = req_flags;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500504 memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
505
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600506 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500507out:
508 return rc;
509}
510
511/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500512 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500513 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500514 */
515static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
516{
517 struct pci_dev *pdev = cfg->dev;
518
519 if (pci_channel_offline(pdev))
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500520 wait_event_timeout(cfg->reset_waitq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500521 !pci_channel_offline(pdev),
522 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
523}
524
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500525/**
526 * free_mem() - free memory associated with the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500527 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500528 */
529static void free_mem(struct cxlflash_cfg *cfg)
530{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500531 struct afu *afu = cfg->afu;
532
533 if (cfg->afu) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500534 free_pages((ulong)afu, get_order(sizeof(struct afu)));
535 cfg->afu = NULL;
536 }
537}
538
539/**
540 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500541 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500542 *
543 * Safe to call with AFU in a partially allocated/initialized state.
Manoj Kumaree91e332015-12-14 15:07:02 -0600544 *
Uma Krishnan0df5bef2017-01-11 19:20:03 -0600545 * Cancels scheduled worker threads, waits for any active internal AFU
Matthew R. Ochscba06e62017-04-12 14:13:20 -0500546 * commands to timeout, disables IRQ polling and then unmaps the MMIO space.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500547 */
548static void stop_afu(struct cxlflash_cfg *cfg)
549{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500550 struct afu *afu = cfg->afu;
551
Uma Krishnan0df5bef2017-01-11 19:20:03 -0600552 cancel_work_sync(&cfg->work_q);
553
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500554 if (likely(afu)) {
Matthew R. Ochsde012832016-11-28 18:42:33 -0600555 while (atomic_read(&afu->cmds_active))
556 ssleep(1);
Matthew R. Ochscba06e62017-04-12 14:13:20 -0500557 if (afu_is_irqpoll_enabled(afu))
558 irq_poll_disable(&afu->irqpoll);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500559 if (likely(afu->afu_map)) {
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500560 cxl_psa_unmap((void __iomem *)afu->afu_map);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500561 afu->afu_map = NULL;
562 }
563 }
564}
565
566/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500567 * term_intr() - disables all AFU interrupts
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500568 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500569 * @level: Depth of allocation, where to begin waterfall tear down.
570 *
571 * Safe to call with AFU/MC in partially allocated/initialized state.
572 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500573static void term_intr(struct cxlflash_cfg *cfg, enum undo_level level)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500574{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500575 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500576 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500577
578 if (!afu || !cfg->mcctx) {
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500579 dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500580 return;
581 }
582
583 switch (level) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500584 case UNMAP_THREE:
585 cxl_unmap_afu_irq(cfg->mcctx, 3, afu);
586 case UNMAP_TWO:
587 cxl_unmap_afu_irq(cfg->mcctx, 2, afu);
588 case UNMAP_ONE:
589 cxl_unmap_afu_irq(cfg->mcctx, 1, afu);
590 case FREE_IRQ:
591 cxl_free_afu_irqs(cfg->mcctx);
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500592 /* fall through */
593 case UNDO_NOOP:
594 /* No action required */
595 break;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500596 }
597}
598
599/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500600 * term_mc() - terminates the master context
601 * @cfg: Internal structure associated with the host.
602 * @level: Depth of allocation, where to begin waterfall tear down.
603 *
604 * Safe to call with AFU/MC in partially allocated/initialized state.
605 */
606static void term_mc(struct cxlflash_cfg *cfg)
607{
608 int rc = 0;
609 struct afu *afu = cfg->afu;
610 struct device *dev = &cfg->dev->dev;
611
612 if (!afu || !cfg->mcctx) {
613 dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
614 return;
615 }
616
617 rc = cxl_stop_context(cfg->mcctx);
618 WARN_ON(rc);
619 cfg->mcctx = NULL;
620}
621
622/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500623 * term_afu() - terminates the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500624 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500625 *
626 * Safe to call with AFU/MC in partially allocated/initialized state.
627 */
628static void term_afu(struct cxlflash_cfg *cfg)
629{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600630 struct device *dev = &cfg->dev->dev;
631
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500632 /*
633 * Tear down is carefully orchestrated to ensure
634 * no interrupts can come in when the problem state
635 * area is unmapped.
636 *
637 * 1) Disable all AFU interrupts
638 * 2) Unmap the problem state area
639 * 3) Stop the master context
640 */
641 term_intr(cfg, UNMAP_THREE);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500642 if (cfg->afu)
643 stop_afu(cfg);
644
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500645 term_mc(cfg);
Uma Krishnan6ded8b32016-03-04 15:55:15 -0600646
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600647 dev_dbg(dev, "%s: returning\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500648}
649
650/**
Uma Krishnan704c4b02016-06-15 18:49:57 -0500651 * notify_shutdown() - notifies device of pending shutdown
652 * @cfg: Internal structure associated with the host.
653 * @wait: Whether to wait for shutdown processing to complete.
654 *
655 * This function will notify the AFU that the adapter is being shutdown
656 * and will wait for shutdown processing to complete if wait is true.
657 * This notification should flush pending I/Os to the device and halt
658 * further I/Os until the next AFU reset is issued and device restarted.
659 */
660static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
661{
662 struct afu *afu = cfg->afu;
663 struct device *dev = &cfg->dev->dev;
Uma Krishnan704c4b02016-06-15 18:49:57 -0500664 struct dev_dependent_vals *ddv;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500665 __be64 __iomem *fc_port_regs;
Uma Krishnan704c4b02016-06-15 18:49:57 -0500666 u64 reg, status;
667 int i, retry_cnt = 0;
668
669 ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
670 if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
671 return;
672
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500673 if (!afu || !afu->afu_map) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600674 dev_dbg(dev, "%s: Problem state area not mapped\n", __func__);
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500675 return;
676 }
677
Uma Krishnan704c4b02016-06-15 18:49:57 -0500678 /* Notify AFU */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -0500679 for (i = 0; i < cfg->num_fc_ports; i++) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500680 fc_port_regs = get_fc_port_regs(cfg, i);
681
682 reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
Uma Krishnan704c4b02016-06-15 18:49:57 -0500683 reg |= SISL_FC_SHUTDOWN_NORMAL;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500684 writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
Uma Krishnan704c4b02016-06-15 18:49:57 -0500685 }
686
687 if (!wait)
688 return;
689
690 /* Wait up to 1.5 seconds for shutdown processing to complete */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -0500691 for (i = 0; i < cfg->num_fc_ports; i++) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500692 fc_port_regs = get_fc_port_regs(cfg, i);
Uma Krishnan704c4b02016-06-15 18:49:57 -0500693 retry_cnt = 0;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500694
Uma Krishnan704c4b02016-06-15 18:49:57 -0500695 while (true) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500696 status = readq_be(&fc_port_regs[FC_STATUS / 8]);
Uma Krishnan704c4b02016-06-15 18:49:57 -0500697 if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
698 break;
699 if (++retry_cnt >= MC_RETRY_CNT) {
700 dev_dbg(dev, "%s: port %d shutdown processing "
701 "not yet completed\n", __func__, i);
702 break;
703 }
704 msleep(100 * retry_cnt);
705 }
706 }
707}
708
709/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500710 * cxlflash_remove() - PCI entry point to tear down host
711 * @pdev: PCI device associated with the host.
712 *
Matthew R. Ochs323e3342017-04-12 14:14:51 -0500713 * Safe to use as a cleanup in partially allocated/initialized state. Note that
714 * the reset_waitq is flushed as part of the stop/termination of user contexts.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500715 */
716static void cxlflash_remove(struct pci_dev *pdev)
717{
718 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600719 struct device *dev = &pdev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500720 ulong lock_flags;
721
Uma Krishnanbabf9852016-09-02 15:39:16 -0500722 if (!pci_is_enabled(pdev)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600723 dev_dbg(dev, "%s: Device is disabled\n", __func__);
Uma Krishnanbabf9852016-09-02 15:39:16 -0500724 return;
725 }
726
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500727 /* If a Task Management Function is active, wait for it to complete
728 * before continuing with remove.
729 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500730 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500731 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500732 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
733 !cfg->tmf_active,
734 cfg->tmf_slock);
735 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500736
Uma Krishnan704c4b02016-06-15 18:49:57 -0500737 /* Notify AFU and wait for shutdown processing to complete */
738 notify_shutdown(cfg, true);
739
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500740 cfg->state = STATE_FAILTERM;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500741 cxlflash_stop_term_user_contexts(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500742
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500743 switch (cfg->init_state) {
744 case INIT_STATE_SCSI:
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500745 cxlflash_term_local_luns(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500746 scsi_remove_host(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500747 case INIT_STATE_AFU:
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600748 term_afu(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500749 case INIT_STATE_PCI:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500750 pci_disable_device(pdev);
751 case INIT_STATE_NONE:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500752 free_mem(cfg);
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -0500753 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500754 break;
755 }
756
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600757 dev_dbg(dev, "%s: returning\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500758}
759
760/**
761 * alloc_mem() - allocates the AFU and its command pool
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500762 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500763 *
764 * A partially allocated state remains on failure.
765 *
766 * Return:
767 * 0 on success
768 * -ENOMEM on failure to allocate memory
769 */
770static int alloc_mem(struct cxlflash_cfg *cfg)
771{
772 int rc = 0;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500773 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500774
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600775 /* AFU is ~28k, i.e. only one 64k page or up to seven 4k pages */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500776 cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
777 get_order(sizeof(struct afu)));
778 if (unlikely(!cfg->afu)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500779 dev_err(dev, "%s: cannot get %d free pages\n",
780 __func__, get_order(sizeof(struct afu)));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500781 rc = -ENOMEM;
782 goto out;
783 }
784 cfg->afu->parent = cfg;
785 cfg->afu->afu_map = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500786out:
787 return rc;
788}
789
790/**
791 * init_pci() - initializes the host as a PCI device
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500792 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500793 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500794 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500795 */
796static int init_pci(struct cxlflash_cfg *cfg)
797{
798 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600799 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500800 int rc = 0;
801
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500802 rc = pci_enable_device(pdev);
803 if (rc || pci_channel_offline(pdev)) {
804 if (pci_channel_offline(pdev)) {
805 cxlflash_wait_for_pci_err_recovery(cfg);
806 rc = pci_enable_device(pdev);
807 }
808
809 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600810 dev_err(dev, "%s: Cannot enable adapter\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500811 cxlflash_wait_for_pci_err_recovery(cfg);
Manoj N. Kumar961487e2016-03-04 15:55:14 -0600812 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500813 }
814 }
815
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500816out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600817 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500818 return rc;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500819}
820
821/**
822 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500823 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500824 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500825 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500826 */
827static int init_scsi(struct cxlflash_cfg *cfg)
828{
829 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600830 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500831 int rc = 0;
832
833 rc = scsi_add_host(cfg->host, &pdev->dev);
834 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600835 dev_err(dev, "%s: scsi_add_host failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500836 goto out;
837 }
838
839 scsi_scan_host(cfg->host);
840
841out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600842 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500843 return rc;
844}
845
846/**
847 * set_port_online() - transitions the specified host FC port to online state
848 * @fc_regs: Top of MMIO region defined for specified port.
849 *
850 * The provided MMIO region must be mapped prior to call. Online state means
851 * that the FC link layer has synced, completed the handshaking process, and
852 * is ready for login to start.
853 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500854static void set_port_online(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500855{
856 u64 cmdcfg;
857
858 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
859 cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
860 cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
861 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
862}
863
864/**
865 * set_port_offline() - transitions the specified host FC port to offline state
866 * @fc_regs: Top of MMIO region defined for specified port.
867 *
868 * The provided MMIO region must be mapped prior to call.
869 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500870static void set_port_offline(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500871{
872 u64 cmdcfg;
873
874 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
875 cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
876 cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
877 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
878}
879
880/**
881 * wait_port_online() - waits for the specified host FC port come online
882 * @fc_regs: Top of MMIO region defined for specified port.
883 * @delay_us: Number of microseconds to delay between reading port status.
884 * @nretry: Number of cycles to retry reading port status.
885 *
886 * The provided MMIO region must be mapped prior to call. This will timeout
887 * when the cable is not plugged in.
888 *
889 * Return:
890 * TRUE (1) when the specified port is online
891 * FALSE (0) when the specified port fails to come online after timeout
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500892 */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600893static bool wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500894{
895 u64 status;
896
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600897 WARN_ON(delay_us < 1000);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500898
899 do {
900 msleep(delay_us / 1000);
901 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -0500902 if (status == U64_MAX)
903 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500904 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
905 nretry--);
906
907 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
908}
909
910/**
911 * wait_port_offline() - waits for the specified host FC port go offline
912 * @fc_regs: Top of MMIO region defined for specified port.
913 * @delay_us: Number of microseconds to delay between reading port status.
914 * @nretry: Number of cycles to retry reading port status.
915 *
916 * The provided MMIO region must be mapped prior to call.
917 *
918 * Return:
919 * TRUE (1) when the specified port is offline
920 * FALSE (0) when the specified port fails to go offline after timeout
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500921 */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600922static bool wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500923{
924 u64 status;
925
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600926 WARN_ON(delay_us < 1000);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500927
928 do {
929 msleep(delay_us / 1000);
930 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -0500931 if (status == U64_MAX)
932 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500933 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
934 nretry--);
935
936 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
937}
938
939/**
940 * afu_set_wwpn() - configures the WWPN for the specified host FC port
941 * @afu: AFU associated with the host that owns the specified FC port.
942 * @port: Port number being configured.
943 * @fc_regs: Top of MMIO region defined for specified port.
944 * @wwpn: The world-wide-port-number previously discovered for port.
945 *
946 * The provided MMIO region must be mapped prior to call. As part of the
947 * sequence to configure the WWPN, the port is toggled offline and then back
948 * online. This toggling action can cause this routine to delay up to a few
949 * seconds. When configured to use the internal LUN feature of the AFU, a
950 * failure to come online is overridden.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500951 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -0500952static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
953 u64 wwpn)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500954{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600955 struct cxlflash_cfg *cfg = afu->parent;
956 struct device *dev = &cfg->dev->dev;
957
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500958 set_port_offline(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500959 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
960 FC_PORT_STATUS_RETRY_CNT)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600961 dev_dbg(dev, "%s: wait on port %d to go offline timed out\n",
962 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500963 }
964
Matthew R. Ochsf8013262016-09-02 15:40:20 -0500965 writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
Matthew R. Ochs964497b2015-10-21 15:13:54 -0500966
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500967 set_port_online(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500968 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
969 FC_PORT_STATUS_RETRY_CNT)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600970 dev_dbg(dev, "%s: wait on port %d to go online timed out\n",
971 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500972 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500973}
974
975/**
976 * afu_link_reset() - resets the specified host FC port
977 * @afu: AFU associated with the host that owns the specified FC port.
978 * @port: Port number being configured.
979 * @fc_regs: Top of MMIO region defined for specified port.
980 *
981 * The provided MMIO region must be mapped prior to call. The sequence to
982 * reset the port involves toggling it offline and then back online. This
983 * action can cause this routine to delay up to a few seconds. An effort
984 * is made to maintain link with the device by switching to host to use
985 * the alternate port exclusively while the reset takes place.
986 * failure to come online is overridden.
987 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500988static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500989{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600990 struct cxlflash_cfg *cfg = afu->parent;
991 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500992 u64 port_sel;
993
994 /* first switch the AFU to the other links, if any */
995 port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
Dan Carpenter4da74db2015-08-18 11:57:43 +0300996 port_sel &= ~(1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500997 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
998 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
999
1000 set_port_offline(fc_regs);
1001 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1002 FC_PORT_STATUS_RETRY_CNT))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001003 dev_err(dev, "%s: wait on port %d to go offline timed out\n",
1004 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001005
1006 set_port_online(fc_regs);
1007 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1008 FC_PORT_STATUS_RETRY_CNT))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001009 dev_err(dev, "%s: wait on port %d to go online timed out\n",
1010 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001011
1012 /* switch back to include this port */
Dan Carpenter4da74db2015-08-18 11:57:43 +03001013 port_sel |= (1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001014 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1015 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1016
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001017 dev_dbg(dev, "%s: returning port_sel=%016llx\n", __func__, port_sel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001018}
1019
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001020/**
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{
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001026 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001027 __be64 __iomem *fc_port_regs;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001028 int i;
1029 u64 reg;
1030
1031 /* global async interrupts: AFU clears afu_ctrl on context exit
1032 * if async interrupts were sent to that context. This prevents
1033 * the AFU form sending further async interrupts when
1034 * there is
1035 * nobody to receive them.
1036 */
1037
1038 /* mask all */
1039 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
1040 /* set LISN# to send and point to master context */
1041 reg = ((u64) (((afu->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
1042
1043 if (afu->internal_lun)
1044 reg |= 1; /* Bit 63 indicates local lun */
1045 writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1046 /* clear all */
1047 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1048 /* unmask bits that are of interest */
1049 /* note: afu can send an interrupt after this step */
1050 writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1051 /* clear again in case a bit came on after previous clear but before */
1052 /* unmask */
1053 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1054
1055 /* Clear/Set internal lun bits */
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001056 fc_port_regs = get_fc_port_regs(cfg, 0);
1057 reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001058 reg &= SISL_FC_INTERNAL_MASK;
1059 if (afu->internal_lun)
1060 reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001061 writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001062
1063 /* now clear FC errors */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001064 for (i = 0; i < cfg->num_fc_ports; i++) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001065 fc_port_regs = get_fc_port_regs(cfg, i);
1066
1067 writeq_be(0xFFFFFFFFU, &fc_port_regs[FC_ERROR / 8]);
1068 writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001069 }
1070
1071 /* sync interrupts for master's IOARRIN write */
1072 /* note that unlike asyncs, there can be no pending sync interrupts */
1073 /* at this time (this is a fresh context and master has not written */
1074 /* IOARRIN yet), so there is nothing to clear. */
1075
1076 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1077 writeq_be(SISL_MSI_SYNC_ERROR, &afu->host_map->ctx_ctrl);
1078 writeq_be(SISL_ISTATUS_MASK, &afu->host_map->intr_mask);
1079}
1080
1081/**
1082 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1083 * @irq: Interrupt number.
1084 * @data: Private data provided at interrupt registration, the AFU.
1085 *
1086 * Return: Always return IRQ_HANDLED.
1087 */
1088static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1089{
1090 struct afu *afu = (struct afu *)data;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001091 struct cxlflash_cfg *cfg = afu->parent;
1092 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001093 u64 reg;
1094 u64 reg_unmasked;
1095
1096 reg = readq_be(&afu->host_map->intr_status);
1097 reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1098
1099 if (reg_unmasked == 0UL) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001100 dev_err(dev, "%s: spurious interrupt, intr_status=%016llx\n",
1101 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001102 goto cxlflash_sync_err_irq_exit;
1103 }
1104
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001105 dev_err(dev, "%s: unexpected interrupt, intr_status=%016llx\n",
1106 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001107
1108 writeq_be(reg_unmasked, &afu->host_map->intr_clear);
1109
1110cxlflash_sync_err_irq_exit:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001111 return IRQ_HANDLED;
1112}
1113
1114/**
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001115 * process_hrrq() - process the read-response queue
1116 * @afu: AFU associated with the host.
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001117 * @doneq: Queue of commands harvested from the RRQ.
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001118 * @budget: Threshold of RRQ entries to process.
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001119 *
1120 * This routine must be called holding the disabled RRQ spin lock.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001121 *
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001122 * Return: The number of entries processed.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001123 */
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001124static int process_hrrq(struct afu *afu, struct list_head *doneq, int budget)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001125{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001126 struct afu_cmd *cmd;
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001127 struct sisl_ioasa *ioasa;
1128 struct sisl_ioarcb *ioarcb;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001129 bool toggle = afu->toggle;
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001130 int num_hrrq = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001131 u64 entry,
1132 *hrrq_start = afu->hrrq_start,
1133 *hrrq_end = afu->hrrq_end,
1134 *hrrq_curr = afu->hrrq_curr;
1135
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001136 /* Process ready RRQ entries up to the specified budget (if any) */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001137 while (true) {
1138 entry = *hrrq_curr;
1139
1140 if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1141 break;
1142
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001143 entry &= ~SISL_RESP_HANDLE_T_BIT;
1144
1145 if (afu_is_sq_cmd_mode(afu)) {
1146 ioasa = (struct sisl_ioasa *)entry;
1147 cmd = container_of(ioasa, struct afu_cmd, sa);
1148 } else {
1149 ioarcb = (struct sisl_ioarcb *)entry;
1150 cmd = container_of(ioarcb, struct afu_cmd, rcb);
1151 }
1152
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001153 list_add_tail(&cmd->queue, doneq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001154
1155 /* Advance to next entry or wrap and flip the toggle bit */
1156 if (hrrq_curr < hrrq_end)
1157 hrrq_curr++;
1158 else {
1159 hrrq_curr = hrrq_start;
1160 toggle ^= SISL_RESP_HANDLE_T_BIT;
1161 }
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001162
1163 atomic_inc(&afu->hsq_credits);
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001164 num_hrrq++;
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001165
1166 if (budget > 0 && num_hrrq >= budget)
1167 break;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001168 }
1169
1170 afu->hrrq_curr = hrrq_curr;
1171 afu->toggle = toggle;
1172
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001173 return num_hrrq;
1174}
1175
1176/**
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001177 * process_cmd_doneq() - process a queue of harvested RRQ commands
1178 * @doneq: Queue of completed commands.
1179 *
1180 * Note that upon return the queue can no longer be trusted.
1181 */
1182static void process_cmd_doneq(struct list_head *doneq)
1183{
1184 struct afu_cmd *cmd, *tmp;
1185
1186 WARN_ON(list_empty(doneq));
1187
1188 list_for_each_entry_safe(cmd, tmp, doneq, queue)
1189 cmd_complete(cmd);
1190}
1191
1192/**
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001193 * cxlflash_irqpoll() - process a queue of harvested RRQ commands
1194 * @irqpoll: IRQ poll structure associated with queue to poll.
1195 * @budget: Threshold of RRQ entries to process per poll.
1196 *
1197 * Return: The number of entries processed.
1198 */
1199static int cxlflash_irqpoll(struct irq_poll *irqpoll, int budget)
1200{
1201 struct afu *afu = container_of(irqpoll, struct afu, irqpoll);
1202 unsigned long hrrq_flags;
1203 LIST_HEAD(doneq);
1204 int num_entries = 0;
1205
1206 spin_lock_irqsave(&afu->hrrq_slock, hrrq_flags);
1207
1208 num_entries = process_hrrq(afu, &doneq, budget);
1209 if (num_entries < budget)
1210 irq_poll_complete(irqpoll);
1211
1212 spin_unlock_irqrestore(&afu->hrrq_slock, hrrq_flags);
1213
1214 process_cmd_doneq(&doneq);
1215 return num_entries;
1216}
1217
1218/**
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001219 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1220 * @irq: Interrupt number.
1221 * @data: Private data provided at interrupt registration, the AFU.
1222 *
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001223 * Return: IRQ_HANDLED or IRQ_NONE when no ready entries found.
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001224 */
1225static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
1226{
1227 struct afu *afu = (struct afu *)data;
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001228 unsigned long hrrq_flags;
1229 LIST_HEAD(doneq);
1230 int num_entries = 0;
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001231
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001232 spin_lock_irqsave(&afu->hrrq_slock, hrrq_flags);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001233
1234 if (afu_is_irqpoll_enabled(afu)) {
1235 irq_poll_sched(&afu->irqpoll);
1236 spin_unlock_irqrestore(&afu->hrrq_slock, hrrq_flags);
1237 return IRQ_HANDLED;
1238 }
1239
1240 num_entries = process_hrrq(afu, &doneq, -1);
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001241 spin_unlock_irqrestore(&afu->hrrq_slock, hrrq_flags);
1242
1243 if (num_entries == 0)
1244 return IRQ_NONE;
1245
1246 process_cmd_doneq(&doneq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001247 return IRQ_HANDLED;
1248}
1249
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001250/*
1251 * Asynchronous interrupt information table
1252 *
1253 * NOTE:
1254 * - Order matters here as this array is indexed by bit position.
1255 *
1256 * - The checkpatch script considers the BUILD_SISL_ASTATUS_FC_PORT macro
1257 * as complex and complains due to a lack of parentheses/braces.
1258 */
1259#define ASTATUS_FC(_a, _b, _c, _d) \
1260 { SISL_ASTATUS_FC##_a##_##_b, _c, _a, (_d) }
1261
1262#define BUILD_SISL_ASTATUS_FC_PORT(_a) \
1263 ASTATUS_FC(_a, LINK_UP, "link up", 0), \
1264 ASTATUS_FC(_a, LINK_DN, "link down", 0), \
1265 ASTATUS_FC(_a, LOGI_S, "login succeeded", SCAN_HOST), \
1266 ASTATUS_FC(_a, LOGI_F, "login failed", CLR_FC_ERROR), \
1267 ASTATUS_FC(_a, LOGI_R, "login timed out, retrying", LINK_RESET), \
1268 ASTATUS_FC(_a, CRC_T, "CRC threshold exceeded", LINK_RESET), \
1269 ASTATUS_FC(_a, LOGO, "target initiated LOGO", 0), \
1270 ASTATUS_FC(_a, OTHER, "other error", CLR_FC_ERROR | LINK_RESET)
1271
1272static const struct asyc_intr_info ainfo[] = {
1273 BUILD_SISL_ASTATUS_FC_PORT(1),
1274 BUILD_SISL_ASTATUS_FC_PORT(0),
1275 BUILD_SISL_ASTATUS_FC_PORT(3),
1276 BUILD_SISL_ASTATUS_FC_PORT(2)
1277};
1278
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001279/**
1280 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1281 * @irq: Interrupt number.
1282 * @data: Private data provided at interrupt registration, the AFU.
1283 *
1284 * Return: Always return IRQ_HANDLED.
1285 */
1286static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1287{
1288 struct afu *afu = (struct afu *)data;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001289 struct cxlflash_cfg *cfg = afu->parent;
1290 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001291 const struct asyc_intr_info *info;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001292 struct sisl_global_map __iomem *global = &afu->afu_map->global;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001293 __be64 __iomem *fc_port_regs;
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001294 u64 reg_unmasked;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001295 u64 reg;
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001296 u64 bit;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001297 u8 port;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001298
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001299 reg = readq_be(&global->regs.aintr_status);
1300 reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1301
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001302 if (unlikely(reg_unmasked == 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001303 dev_err(dev, "%s: spurious interrupt, aintr_status=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001304 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001305 goto out;
1306 }
1307
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001308 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001309 writeq_be(reg_unmasked, &global->regs.aintr_clear);
1310
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001311 /* Check each bit that is on */
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001312 for_each_set_bit(bit, (ulong *)&reg_unmasked, BITS_PER_LONG) {
1313 if (unlikely(bit >= ARRAY_SIZE(ainfo))) {
1314 WARN_ON_ONCE(1);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001315 continue;
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001316 }
1317
1318 info = &ainfo[bit];
1319 if (unlikely(info->status != 1ULL << bit)) {
1320 WARN_ON_ONCE(1);
1321 continue;
1322 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001323
1324 port = info->port;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001325 fc_port_regs = get_fc_port_regs(cfg, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001326
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001327 dev_err(dev, "%s: FC Port %d -> %s, fc_status=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001328 __func__, port, info->desc,
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001329 readq_be(&fc_port_regs[FC_STATUS / 8]));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001330
1331 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001332 * Do link reset first, some OTHER errors will set FC_ERROR
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001333 * again if cleared before or w/o a reset
1334 */
1335 if (info->action & LINK_RESET) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001336 dev_err(dev, "%s: FC Port %d: resetting link\n",
1337 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001338 cfg->lr_state = LINK_RESET_REQUIRED;
1339 cfg->lr_port = port;
1340 schedule_work(&cfg->work_q);
1341 }
1342
1343 if (info->action & CLR_FC_ERROR) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001344 reg = readq_be(&fc_port_regs[FC_ERROR / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001345
1346 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001347 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001348 * should be the same and tracing one is sufficient.
1349 */
1350
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001351 dev_err(dev, "%s: fc %d: clearing fc_error=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001352 __func__, port, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001353
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001354 writeq_be(reg, &fc_port_regs[FC_ERROR / 8]);
1355 writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001356 }
Matthew R. Ochsef510742015-10-21 15:13:37 -05001357
1358 if (info->action & SCAN_HOST) {
1359 atomic_inc(&cfg->scan_host_needed);
1360 schedule_work(&cfg->work_q);
1361 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001362 }
1363
1364out:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001365 return IRQ_HANDLED;
1366}
1367
1368/**
1369 * start_context() - starts the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001370 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001371 *
1372 * Return: A success or failure value from CXL services.
1373 */
1374static int start_context(struct cxlflash_cfg *cfg)
1375{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001376 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001377 int rc = 0;
1378
1379 rc = cxl_start_context(cfg->mcctx,
1380 cfg->afu->work.work_element_descriptor,
1381 NULL);
1382
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001383 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001384 return rc;
1385}
1386
1387/**
1388 * read_vpd() - obtains the WWPNs from VPD
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001389 * @cfg: Internal structure associated with the host.
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001390 * @wwpn: Array of size MAX_FC_PORTS to pass back WWPNs
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001391 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001392 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001393 */
1394static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1395{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001396 struct device *dev = &cfg->dev->dev;
1397 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001398 int rc = 0;
1399 int ro_start, ro_size, i, j, k;
1400 ssize_t vpd_size;
1401 char vpd_data[CXLFLASH_VPD_LEN];
1402 char tmp_buf[WWPN_BUF_LEN] = { 0 };
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05001403 char *wwpn_vpd_tags[MAX_FC_PORTS] = { "V5", "V6", "V7", "V8" };
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001404
1405 /* Get the VPD data from the device */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001406 vpd_size = cxl_read_adapter_vpd(pdev, vpd_data, sizeof(vpd_data));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001407 if (unlikely(vpd_size <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001408 dev_err(dev, "%s: Unable to read VPD (size = %ld)\n",
1409 __func__, vpd_size);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001410 rc = -ENODEV;
1411 goto out;
1412 }
1413
1414 /* Get the read only section offset */
1415 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
1416 PCI_VPD_LRDT_RO_DATA);
1417 if (unlikely(ro_start < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001418 dev_err(dev, "%s: VPD Read-only data not found\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001419 rc = -ENODEV;
1420 goto out;
1421 }
1422
1423 /* Get the read only section size, cap when extends beyond read VPD */
1424 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1425 j = ro_size;
1426 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1427 if (unlikely((i + j) > vpd_size)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001428 dev_dbg(dev, "%s: Might need to read more VPD (%d > %ld)\n",
1429 __func__, (i + j), vpd_size);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001430 ro_size = vpd_size - i;
1431 }
1432
1433 /*
1434 * Find the offset of the WWPN tag within the read only
1435 * VPD data and validate the found field (partials are
1436 * no good to us). Convert the ASCII data to an integer
1437 * value. Note that we must copy to a temporary buffer
1438 * because the conversion service requires that the ASCII
1439 * string be terminated.
1440 */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001441 for (k = 0; k < cfg->num_fc_ports; k++) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001442 j = ro_size;
1443 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1444
1445 i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
1446 if (unlikely(i < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001447 dev_err(dev, "%s: Port %d WWPN not found in VPD\n",
1448 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001449 rc = -ENODEV;
1450 goto out;
1451 }
1452
1453 j = pci_vpd_info_field_size(&vpd_data[i]);
1454 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1455 if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001456 dev_err(dev, "%s: Port %d WWPN incomplete or bad VPD\n",
1457 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001458 rc = -ENODEV;
1459 goto out;
1460 }
1461
1462 memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1463 rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1464 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001465 dev_err(dev, "%s: WWPN conversion failed for port %d\n",
1466 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001467 rc = -ENODEV;
1468 goto out;
1469 }
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001470
1471 dev_dbg(dev, "%s: wwpn%d=%016llx\n", __func__, k, wwpn[k]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001472 }
1473
1474out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001475 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001476 return rc;
1477}
1478
1479/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001480 * init_pcr() - initialize the provisioning and control registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001481 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001482 *
1483 * Also sets up fast access to the mapped registers and initializes AFU
1484 * command fields that never change.
1485 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001486static void init_pcr(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001487{
1488 struct afu *afu = cfg->afu;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001489 struct sisl_ctrl_map __iomem *ctrl_map;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001490 int i;
1491
1492 for (i = 0; i < MAX_CONTEXT; i++) {
1493 ctrl_map = &afu->afu_map->ctrls[i].ctrl;
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001494 /* Disrupt any clients that could be running */
1495 /* e.g. clients that survived a master restart */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001496 writeq_be(0, &ctrl_map->rht_start);
1497 writeq_be(0, &ctrl_map->rht_cnt_id);
1498 writeq_be(0, &ctrl_map->ctx_cap);
1499 }
1500
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001501 /* Copy frequently used fields into afu */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001502 afu->ctx_hndl = (u16) cxl_process_element(cfg->mcctx);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001503 afu->host_map = &afu->afu_map->hosts[afu->ctx_hndl].host;
1504 afu->ctrl_map = &afu->afu_map->ctrls[afu->ctx_hndl].ctrl;
1505
1506 /* Program the Endian Control for the master context */
1507 writeq_be(SISL_ENDIAN_CTRL, &afu->host_map->endian_ctrl);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001508}
1509
1510/**
1511 * init_global() - initialize AFU global registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001512 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001513 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001514static int init_global(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001515{
1516 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001517 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001518 __be64 __iomem *fc_port_regs;
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001519 u64 wwpn[MAX_FC_PORTS]; /* wwpn of AFU ports */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001520 int i = 0, num_ports = 0;
1521 int rc = 0;
1522 u64 reg;
1523
1524 rc = read_vpd(cfg, &wwpn[0]);
1525 if (rc) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001526 dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001527 goto out;
1528 }
1529
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001530 /* Set up RRQ and SQ in AFU for master issued cmds */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001531 writeq_be((u64) afu->hrrq_start, &afu->host_map->rrq_start);
1532 writeq_be((u64) afu->hrrq_end, &afu->host_map->rrq_end);
1533
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001534 if (afu_is_sq_cmd_mode(afu)) {
1535 writeq_be((u64)afu->hsq_start, &afu->host_map->sq_start);
1536 writeq_be((u64)afu->hsq_end, &afu->host_map->sq_end);
1537 }
1538
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001539 /* AFU configuration */
1540 reg = readq_be(&afu->afu_map->global.regs.afu_config);
1541 reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1542 /* enable all auto retry options and control endianness */
1543 /* leave others at default: */
1544 /* CTX_CAP write protected, mbox_r does not clear on read and */
1545 /* checker on if dual afu */
1546 writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1547
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001548 /* Global port select: select either port */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001549 if (afu->internal_lun) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001550 /* Only use port 0 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001551 writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001552 num_ports = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001553 } else {
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -05001554 writeq_be(PORT_MASK(cfg->num_fc_ports),
1555 &afu->afu_map->global.regs.afu_port_sel);
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001556 num_ports = cfg->num_fc_ports;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001557 }
1558
1559 for (i = 0; i < num_ports; i++) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001560 fc_port_regs = get_fc_port_regs(cfg, i);
1561
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001562 /* Unmask all errors (but they are still masked at AFU) */
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001563 writeq_be(0, &fc_port_regs[FC_ERRMSK / 8]);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001564 /* Clear CRC error cnt & set a threshold */
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001565 (void)readq_be(&fc_port_regs[FC_CNT_CRCERR / 8]);
1566 writeq_be(MC_CRC_THRESH, &fc_port_regs[FC_CRC_THRESH / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001567
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001568 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001569 if (wwpn[i] != 0)
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001570 afu_set_wwpn(afu, i, &fc_port_regs[0], wwpn[i]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001571 /* Programming WWPN back to back causes additional
1572 * offline/online transitions and a PLOGI
1573 */
1574 msleep(100);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001575 }
1576
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001577 /* Set up master's own CTX_CAP to allow real mode, host translation */
1578 /* tables, afu cmds and read/write GSCSI cmds. */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001579 /* First, unlock ctx_cap write by reading mbox */
1580 (void)readq_be(&afu->ctrl_map->mbox_r); /* unlock ctx_cap */
1581 writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1582 SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1583 SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1584 &afu->ctrl_map->ctx_cap);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001585 /* Initialize heartbeat */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001586 afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001587out:
1588 return rc;
1589}
1590
1591/**
1592 * start_afu() - initializes and starts the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001593 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001594 */
1595static int start_afu(struct cxlflash_cfg *cfg)
1596{
1597 struct afu *afu = cfg->afu;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001598 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001599 int rc = 0;
1600
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001601 init_pcr(cfg);
1602
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001603 /* Initialize RRQ */
Matthew R. Ochsaf104832015-10-21 15:15:14 -05001604 memset(&afu->rrq_entry, 0, sizeof(afu->rrq_entry));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001605 afu->hrrq_start = &afu->rrq_entry[0];
1606 afu->hrrq_end = &afu->rrq_entry[NUM_RRQ_ENTRY - 1];
1607 afu->hrrq_curr = afu->hrrq_start;
1608 afu->toggle = 1;
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001609 spin_lock_init(&afu->hrrq_slock);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001610
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001611 /* Initialize SQ */
1612 if (afu_is_sq_cmd_mode(afu)) {
1613 memset(&afu->sq, 0, sizeof(afu->sq));
1614 afu->hsq_start = &afu->sq[0];
1615 afu->hsq_end = &afu->sq[NUM_SQ_ENTRY - 1];
1616 afu->hsq_curr = afu->hsq_start;
1617
1618 spin_lock_init(&afu->hsq_slock);
1619 atomic_set(&afu->hsq_credits, NUM_SQ_ENTRY - 1);
1620 }
1621
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001622 /* Initialize IRQ poll */
1623 if (afu_is_irqpoll_enabled(afu))
1624 irq_poll_init(&afu->irqpoll, afu->irqpoll_weight,
1625 cxlflash_irqpoll);
1626
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001627 rc = init_global(cfg);
1628
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001629 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001630 return rc;
1631}
1632
1633/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001634 * init_intr() - setup interrupt handlers for the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001635 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001636 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001637 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001638 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001639static enum undo_level init_intr(struct cxlflash_cfg *cfg,
1640 struct cxl_context *ctx)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001641{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001642 struct afu *afu = cfg->afu;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001643 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001644 int rc = 0;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001645 enum undo_level level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001646
1647 rc = cxl_allocate_afu_irqs(ctx, 3);
1648 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001649 dev_err(dev, "%s: allocate_afu_irqs failed rc=%d\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001650 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001651 level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001652 goto out;
1653 }
1654
1655 rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, afu,
1656 "SISL_MSI_SYNC_ERROR");
1657 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001658 dev_err(dev, "%s: SISL_MSI_SYNC_ERROR map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001659 level = FREE_IRQ;
1660 goto out;
1661 }
1662
1663 rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, afu,
1664 "SISL_MSI_RRQ_UPDATED");
1665 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001666 dev_err(dev, "%s: SISL_MSI_RRQ_UPDATED map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001667 level = UNMAP_ONE;
1668 goto out;
1669 }
1670
1671 rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, afu,
1672 "SISL_MSI_ASYNC_ERROR");
1673 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001674 dev_err(dev, "%s: SISL_MSI_ASYNC_ERROR map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001675 level = UNMAP_TWO;
1676 goto out;
1677 }
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001678out:
1679 return level;
1680}
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001681
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001682/**
1683 * init_mc() - create and register as the master context
1684 * @cfg: Internal structure associated with the host.
1685 *
1686 * Return: 0 on success, -errno on failure
1687 */
1688static int init_mc(struct cxlflash_cfg *cfg)
1689{
1690 struct cxl_context *ctx;
1691 struct device *dev = &cfg->dev->dev;
1692 int rc = 0;
1693 enum undo_level level;
1694
1695 ctx = cxl_get_context(cfg->dev);
1696 if (unlikely(!ctx)) {
1697 rc = -ENOMEM;
1698 goto ret;
1699 }
1700 cfg->mcctx = ctx;
1701
1702 /* Set it up as a master with the CXL */
1703 cxl_set_master(ctx);
1704
1705 /* During initialization reset the AFU to start from a clean slate */
1706 rc = cxl_afu_reset(cfg->mcctx);
1707 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001708 dev_err(dev, "%s: AFU reset failed rc=%d\n", __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001709 goto ret;
1710 }
1711
1712 level = init_intr(cfg, ctx);
1713 if (unlikely(level)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001714 dev_err(dev, "%s: interrupt init failed rc=%d\n", __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001715 goto out;
1716 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001717
1718 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1719 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1720 * element (pe) that is embedded in the context (ctx)
1721 */
1722 rc = start_context(cfg);
1723 if (unlikely(rc)) {
1724 dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
1725 level = UNMAP_THREE;
1726 goto out;
1727 }
1728ret:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001729 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001730 return rc;
1731out:
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001732 term_intr(cfg, level);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001733 goto ret;
1734}
1735
1736/**
Matthew R. Ochs565180722017-04-12 14:14:28 -05001737 * get_num_afu_ports() - determines and configures the number of AFU ports
1738 * @cfg: Internal structure associated with the host.
1739 *
1740 * This routine determines the number of AFU ports by converting the global
1741 * port selection mask. The converted value is only valid following an AFU
1742 * reset (explicit or power-on). This routine must be invoked shortly after
1743 * mapping as other routines are dependent on the number of ports during the
1744 * initialization sequence.
1745 *
1746 * To support legacy AFUs that might not have reflected an initial global
1747 * port mask (value read is 0), default to the number of ports originally
1748 * supported by the cxlflash driver (2) before hardware with other port
1749 * offerings was introduced.
1750 */
1751static void get_num_afu_ports(struct cxlflash_cfg *cfg)
1752{
1753 struct afu *afu = cfg->afu;
1754 struct device *dev = &cfg->dev->dev;
1755 u64 port_mask;
1756 int num_fc_ports = LEGACY_FC_PORTS;
1757
1758 port_mask = readq_be(&afu->afu_map->global.regs.afu_port_sel);
1759 if (port_mask != 0ULL)
1760 num_fc_ports = min(ilog2(port_mask) + 1, MAX_FC_PORTS);
1761
1762 dev_dbg(dev, "%s: port_mask=%016llx num_fc_ports=%d\n",
1763 __func__, port_mask, num_fc_ports);
1764
1765 cfg->num_fc_ports = num_fc_ports;
1766 cfg->host->max_channel = PORTNUM2CHAN(num_fc_ports);
1767}
1768
1769/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001770 * init_afu() - setup as master context and start AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001771 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001772 *
1773 * This routine is a higher level of control for configuring the
1774 * AFU on probe and reset paths.
1775 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001776 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001777 */
1778static int init_afu(struct cxlflash_cfg *cfg)
1779{
1780 u64 reg;
1781 int rc = 0;
1782 struct afu *afu = cfg->afu;
1783 struct device *dev = &cfg->dev->dev;
1784
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001785 cxl_perst_reloads_same_image(cfg->cxl_afu, true);
1786
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001787 rc = init_mc(cfg);
1788 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001789 dev_err(dev, "%s: init_mc failed rc=%d\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001790 __func__, rc);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001791 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001792 }
1793
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001794 /* Map the entire MMIO space of the AFU */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001795 afu->afu_map = cxl_psa_map(cfg->mcctx);
1796 if (!afu->afu_map) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001797 dev_err(dev, "%s: cxl_psa_map failed\n", __func__);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001798 rc = -ENOMEM;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001799 goto err1;
1800 }
1801
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001802 /* No byte reverse on reading afu_version or string will be backwards */
1803 reg = readq(&afu->afu_map->global.regs.afu_version);
1804 memcpy(afu->version, &reg, sizeof(reg));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001805 afu->interface_version =
1806 readq_be(&afu->afu_map->global.regs.interface_version);
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001807 if ((afu->interface_version + 1) == 0) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001808 dev_err(dev, "Back level AFU, please upgrade. AFU version %s "
1809 "interface version %016llx\n", afu->version,
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001810 afu->interface_version);
1811 rc = -EINVAL;
Uma Krishnan0df5bef2017-01-11 19:20:03 -06001812 goto err1;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001813 }
1814
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001815 if (afu_is_sq_cmd_mode(afu)) {
1816 afu->send_cmd = send_cmd_sq;
1817 afu->context_reset = context_reset_sq;
1818 } else {
1819 afu->send_cmd = send_cmd_ioarrin;
1820 afu->context_reset = context_reset_ioarrin;
1821 }
Matthew R. Ochs48b4be32016-11-28 18:43:09 -06001822
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001823 dev_dbg(dev, "%s: afu_ver=%s interface_ver=%016llx\n", __func__,
1824 afu->version, afu->interface_version);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001825
Matthew R. Ochs565180722017-04-12 14:14:28 -05001826 get_num_afu_ports(cfg);
1827
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001828 rc = start_afu(cfg);
1829 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001830 dev_err(dev, "%s: start_afu failed, rc=%d\n", __func__, rc);
Uma Krishnan0df5bef2017-01-11 19:20:03 -06001831 goto err1;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001832 }
1833
1834 afu_err_intr_init(cfg->afu);
Uma Krishnan11f7b182016-11-28 18:41:45 -06001835 spin_lock_init(&afu->rrin_slock);
1836 afu->room = readq_be(&afu->host_map->cmd_room);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001837
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05001838 /* Restore the LUN mappings */
1839 cxlflash_restore_luntable(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001840out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001841 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001842 return rc;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001843
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001844err1:
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001845 term_intr(cfg, UNMAP_THREE);
1846 term_mc(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001847 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001848}
1849
1850/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001851 * cxlflash_afu_sync() - builds and sends an AFU sync command
1852 * @afu: AFU associated with the host.
1853 * @ctx_hndl_u: Identifies context requesting sync.
1854 * @res_hndl_u: Identifies resource requesting sync.
1855 * @mode: Type of sync to issue (lightweight, heavyweight, global).
1856 *
1857 * The AFU can only take 1 sync command at a time. This routine enforces this
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001858 * limitation by using a mutex to provide exclusive access to the AFU during
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001859 * the sync. This design point requires calling threads to not be on interrupt
1860 * context due to the possibility of sleeping during concurrent sync operations.
1861 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001862 * AFU sync operations are only necessary and allowed when the device is
1863 * operating normally. When not operating normally, sync requests can occur as
1864 * part of cleaning up resources associated with an adapter prior to removal.
1865 * In this scenario, these requests are simply ignored (safe due to the AFU
1866 * going away).
1867 *
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001868 * Return:
1869 * 0 on success
1870 * -1 on failure
1871 */
1872int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
1873 res_hndl_t res_hndl_u, u8 mode)
1874{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001875 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001876 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001877 struct afu_cmd *cmd = NULL;
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001878 char *buf = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001879 int rc = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001880 static DEFINE_MUTEX(sync_active);
1881
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001882 if (cfg->state != STATE_NORMAL) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001883 dev_dbg(dev, "%s: Sync not required state=%u\n",
1884 __func__, cfg->state);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001885 return 0;
1886 }
1887
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001888 mutex_lock(&sync_active);
Matthew R. Ochsde012832016-11-28 18:42:33 -06001889 atomic_inc(&afu->cmds_active);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001890 buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
1891 if (unlikely(!buf)) {
1892 dev_err(dev, "%s: no memory for command\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001893 rc = -1;
1894 goto out;
1895 }
1896
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001897 cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
1898 init_completion(&cmd->cevent);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001899 cmd->parent = afu;
1900
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001901 dev_dbg(dev, "%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001902
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001903 cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001904 cmd->rcb.ctx_id = afu->ctx_hndl;
1905 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001906 cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
1907
1908 cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
1909 cmd->rcb.cdb[1] = mode;
1910
1911 /* The cdb is aligned, no unaligned accessors required */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001912 *((__be16 *)&cmd->rcb.cdb[2]) = cpu_to_be16(ctx_hndl_u);
1913 *((__be32 *)&cmd->rcb.cdb[4]) = cpu_to_be32(res_hndl_u);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001914
Matthew R. Ochs48b4be32016-11-28 18:43:09 -06001915 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001916 if (unlikely(rc))
1917 goto out;
1918
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -06001919 rc = wait_resp(afu, cmd);
1920 if (unlikely(rc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001921 rc = -1;
1922out:
Matthew R. Ochsde012832016-11-28 18:42:33 -06001923 atomic_dec(&afu->cmds_active);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001924 mutex_unlock(&sync_active);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001925 kfree(buf);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001926 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001927 return rc;
1928}
1929
1930/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001931 * afu_reset() - resets the AFU
1932 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001933 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001934 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001935 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001936static int afu_reset(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001937{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001938 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001939 int rc = 0;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001940
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001941 /* Stop the context before the reset. Since the context is
1942 * no longer available restart it after the reset is complete
1943 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001944 term_afu(cfg);
1945
1946 rc = init_afu(cfg);
1947
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001948 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001949 return rc;
1950}
1951
1952/**
Manoj N. Kumarf4113962016-06-15 18:49:20 -05001953 * drain_ioctls() - wait until all currently executing ioctls have completed
1954 * @cfg: Internal structure associated with the host.
1955 *
1956 * Obtain write access to read/write semaphore that wraps ioctl
1957 * handling to 'drain' ioctls currently executing.
1958 */
1959static void drain_ioctls(struct cxlflash_cfg *cfg)
1960{
1961 down_write(&cfg->ioctl_rwsem);
1962 up_write(&cfg->ioctl_rwsem);
1963}
1964
1965/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001966 * cxlflash_eh_device_reset_handler() - reset a single LUN
1967 * @scp: SCSI command to send.
1968 *
1969 * Return:
1970 * SUCCESS as defined in scsi/scsi.h
1971 * FAILED as defined in scsi/scsi.h
1972 */
1973static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
1974{
1975 int rc = SUCCESS;
1976 struct Scsi_Host *host = scp->device->host;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001977 struct cxlflash_cfg *cfg = shost_priv(host);
1978 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001979 struct afu *afu = cfg->afu;
1980 int rcr = 0;
1981
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001982 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
1983 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
1984 scp->device->channel, scp->device->id, scp->device->lun,
1985 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1986 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1987 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1988 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochs15305512015-10-21 15:12:10 -05001989
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001990retry:
Matthew R. Ochs15305512015-10-21 15:12:10 -05001991 switch (cfg->state) {
1992 case STATE_NORMAL:
1993 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
1994 if (unlikely(rcr))
1995 rc = FAILED;
1996 break;
1997 case STATE_RESET:
1998 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001999 goto retry;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002000 default:
2001 rc = FAILED;
2002 break;
2003 }
2004
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002005 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002006 return rc;
2007}
2008
2009/**
2010 * cxlflash_eh_host_reset_handler() - reset the host adapter
2011 * @scp: SCSI command from stack identifying host.
2012 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002013 * Following a reset, the state is evaluated again in case an EEH occurred
2014 * during the reset. In such a scenario, the host reset will either yield
2015 * until the EEH recovery is complete or return success or failure based
2016 * upon the current device state.
2017 *
Matthew R. Ochs15305512015-10-21 15:12:10 -05002018 * Return:
2019 * SUCCESS as defined in scsi/scsi.h
2020 * FAILED as defined in scsi/scsi.h
2021 */
2022static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
2023{
2024 int rc = SUCCESS;
2025 int rcr = 0;
2026 struct Scsi_Host *host = scp->device->host;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002027 struct cxlflash_cfg *cfg = shost_priv(host);
2028 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002029
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002030 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
2031 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
2032 scp->device->channel, scp->device->id, scp->device->lun,
2033 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
2034 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
2035 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
2036 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochs15305512015-10-21 15:12:10 -05002037
2038 switch (cfg->state) {
2039 case STATE_NORMAL:
2040 cfg->state = STATE_RESET;
Manoj N. Kumarf4113962016-06-15 18:49:20 -05002041 drain_ioctls(cfg);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002042 cxlflash_mark_contexts_error(cfg);
2043 rcr = afu_reset(cfg);
2044 if (rcr) {
2045 rc = FAILED;
2046 cfg->state = STATE_FAILTERM;
2047 } else
2048 cfg->state = STATE_NORMAL;
2049 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002050 ssleep(1);
2051 /* fall through */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002052 case STATE_RESET:
2053 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2054 if (cfg->state == STATE_NORMAL)
2055 break;
2056 /* fall through */
2057 default:
2058 rc = FAILED;
2059 break;
2060 }
2061
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002062 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002063 return rc;
2064}
2065
2066/**
2067 * cxlflash_change_queue_depth() - change the queue depth for the device
2068 * @sdev: SCSI device destined for queue depth change.
2069 * @qdepth: Requested queue depth value to set.
2070 *
2071 * The requested queue depth is capped to the maximum supported value.
2072 *
2073 * Return: The actual queue depth set.
2074 */
2075static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
2076{
2077
2078 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
2079 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
2080
2081 scsi_change_queue_depth(sdev, qdepth);
2082 return sdev->queue_depth;
2083}
2084
2085/**
2086 * cxlflash_show_port_status() - queries and presents the current port status
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002087 * @port: Desired port for status reporting.
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002088 * @cfg: Internal structure associated with the host.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002089 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2090 *
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002091 * Return: The size of the ASCII string returned in @buf or -EINVAL.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002092 */
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002093static ssize_t cxlflash_show_port_status(u32 port,
2094 struct cxlflash_cfg *cfg,
2095 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002096{
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002097 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002098 char *disp_status;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002099 u64 status;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002100 __be64 __iomem *fc_port_regs;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002101
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002102 WARN_ON(port >= MAX_FC_PORTS);
2103
2104 if (port >= cfg->num_fc_ports) {
2105 dev_info(dev, "%s: Port %d not supported on this card.\n",
2106 __func__, port);
2107 return -EINVAL;
2108 }
Matthew R. Ochs15305512015-10-21 15:12:10 -05002109
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002110 fc_port_regs = get_fc_port_regs(cfg, port);
2111 status = readq_be(&fc_port_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002112 status &= FC_MTIP_STATUS_MASK;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002113
2114 if (status == FC_MTIP_STATUS_ONLINE)
2115 disp_status = "online";
2116 else if (status == FC_MTIP_STATUS_OFFLINE)
2117 disp_status = "offline";
2118 else
2119 disp_status = "unknown";
2120
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002121 return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002122}
2123
2124/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002125 * port0_show() - queries and presents the current status of port 0
2126 * @dev: Generic device associated with the host owning the port.
2127 * @attr: Device attribute representing the port.
2128 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002129 *
2130 * Return: The size of the ASCII string returned in @buf.
2131 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002132static ssize_t port0_show(struct device *dev,
2133 struct device_attribute *attr,
2134 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002135{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002136 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochs15305512015-10-21 15:12:10 -05002137
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002138 return cxlflash_show_port_status(0, cfg, buf);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002139}
2140
2141/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002142 * port1_show() - queries and presents the current status of port 1
2143 * @dev: Generic device associated with the host owning the port.
2144 * @attr: Device attribute representing the port.
2145 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2146 *
2147 * Return: The size of the ASCII string returned in @buf.
2148 */
2149static ssize_t port1_show(struct device *dev,
2150 struct device_attribute *attr,
2151 char *buf)
2152{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002153 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002154
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002155 return cxlflash_show_port_status(1, cfg, buf);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002156}
2157
2158/**
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002159 * port2_show() - queries and presents the current status of port 2
2160 * @dev: Generic device associated with the host owning the port.
2161 * @attr: Device attribute representing the port.
2162 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2163 *
2164 * Return: The size of the ASCII string returned in @buf.
2165 */
2166static ssize_t port2_show(struct device *dev,
2167 struct device_attribute *attr,
2168 char *buf)
2169{
2170 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2171
2172 return cxlflash_show_port_status(2, cfg, buf);
2173}
2174
2175/**
2176 * port3_show() - queries and presents the current status of port 3
2177 * @dev: Generic device associated with the host owning the port.
2178 * @attr: Device attribute representing the port.
2179 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2180 *
2181 * Return: The size of the ASCII string returned in @buf.
2182 */
2183static ssize_t port3_show(struct device *dev,
2184 struct device_attribute *attr,
2185 char *buf)
2186{
2187 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2188
2189 return cxlflash_show_port_status(3, cfg, buf);
2190}
2191
2192/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002193 * lun_mode_show() - presents the current LUN mode of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002194 * @dev: Generic device associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002195 * @attr: Device attribute representing the LUN mode.
2196 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2197 *
2198 * Return: The size of the ASCII string returned in @buf.
2199 */
2200static ssize_t lun_mode_show(struct device *dev,
2201 struct device_attribute *attr, char *buf)
2202{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002203 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002204 struct afu *afu = cfg->afu;
2205
2206 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
2207}
2208
2209/**
2210 * lun_mode_store() - sets the LUN mode of the host
2211 * @dev: Generic device associated with the host.
2212 * @attr: Device attribute representing the LUN mode.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002213 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2214 * @count: Length of data resizing in @buf.
2215 *
2216 * The CXL Flash AFU supports a dummy LUN mode where the external
2217 * links and storage are not required. Space on the FPGA is used
2218 * to create 1 or 2 small LUNs which are presented to the system
2219 * as if they were a normal storage device. This feature is useful
2220 * during development and also provides manufacturing with a way
2221 * to test the AFU without an actual device.
2222 *
2223 * 0 = external LUN[s] (default)
2224 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2225 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2226 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2227 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2228 *
2229 * Return: The size of the ASCII string returned in @buf.
2230 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002231static ssize_t lun_mode_store(struct device *dev,
2232 struct device_attribute *attr,
2233 const char *buf, size_t count)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002234{
2235 struct Scsi_Host *shost = class_to_shost(dev);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002236 struct cxlflash_cfg *cfg = shost_priv(shost);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002237 struct afu *afu = cfg->afu;
2238 int rc;
2239 u32 lun_mode;
2240
2241 rc = kstrtouint(buf, 10, &lun_mode);
2242 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
2243 afu->internal_lun = lun_mode;
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002244
2245 /*
2246 * When configured for internal LUN, there is only one channel,
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002247 * channel number 0, else there will be one less than the number
2248 * of fc ports for this card.
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002249 */
2250 if (afu->internal_lun)
2251 shost->max_channel = 0;
2252 else
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -05002253 shost->max_channel = PORTNUM2CHAN(cfg->num_fc_ports);
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002254
Matthew R. Ochs15305512015-10-21 15:12:10 -05002255 afu_reset(cfg);
2256 scsi_scan_host(cfg->host);
2257 }
2258
2259 return count;
2260}
2261
2262/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002263 * ioctl_version_show() - presents the current ioctl version of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002264 * @dev: Generic device associated with the host.
2265 * @attr: Device attribute representing the ioctl version.
2266 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2267 *
2268 * Return: The size of the ASCII string returned in @buf.
2269 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002270static ssize_t ioctl_version_show(struct device *dev,
2271 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002272{
2273 return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
2274}
2275
2276/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002277 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2278 * @port: Desired port for status reporting.
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002279 * @cfg: Internal structure associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002280 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2281 *
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002282 * Return: The size of the ASCII string returned in @buf or -EINVAL.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002283 */
2284static ssize_t cxlflash_show_port_lun_table(u32 port,
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002285 struct cxlflash_cfg *cfg,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002286 char *buf)
2287{
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002288 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002289 __be64 __iomem *fc_port_luns;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002290 int i;
2291 ssize_t bytes = 0;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002292
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002293 WARN_ON(port >= MAX_FC_PORTS);
2294
2295 if (port >= cfg->num_fc_ports) {
2296 dev_info(dev, "%s: Port %d not supported on this card.\n",
2297 __func__, port);
2298 return -EINVAL;
2299 }
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002300
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002301 fc_port_luns = get_fc_port_luns(cfg, port);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002302
2303 for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2304 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002305 "%03d: %016llx\n",
2306 i, readq_be(&fc_port_luns[i]));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002307 return bytes;
2308}
2309
2310/**
2311 * port0_lun_table_show() - presents the current LUN table of port 0
2312 * @dev: Generic device associated with the host owning the port.
2313 * @attr: Device attribute representing the port.
2314 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2315 *
2316 * Return: The size of the ASCII string returned in @buf.
2317 */
2318static ssize_t port0_lun_table_show(struct device *dev,
2319 struct device_attribute *attr,
2320 char *buf)
2321{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002322 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002323
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002324 return cxlflash_show_port_lun_table(0, cfg, buf);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002325}
2326
2327/**
2328 * port1_lun_table_show() - presents the current LUN table of port 1
2329 * @dev: Generic device associated with the host owning the port.
2330 * @attr: Device attribute representing the port.
2331 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2332 *
2333 * Return: The size of the ASCII string returned in @buf.
2334 */
2335static ssize_t port1_lun_table_show(struct device *dev,
2336 struct device_attribute *attr,
2337 char *buf)
2338{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002339 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002340
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002341 return cxlflash_show_port_lun_table(1, cfg, buf);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002342}
2343
2344/**
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002345 * port2_lun_table_show() - presents the current LUN table of port 2
2346 * @dev: Generic device associated with the host owning the port.
2347 * @attr: Device attribute representing the port.
2348 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2349 *
2350 * Return: The size of the ASCII string returned in @buf.
2351 */
2352static ssize_t port2_lun_table_show(struct device *dev,
2353 struct device_attribute *attr,
2354 char *buf)
2355{
2356 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2357
2358 return cxlflash_show_port_lun_table(2, cfg, buf);
2359}
2360
2361/**
2362 * port3_lun_table_show() - presents the current LUN table of port 3
2363 * @dev: Generic device associated with the host owning the port.
2364 * @attr: Device attribute representing the port.
2365 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2366 *
2367 * Return: The size of the ASCII string returned in @buf.
2368 */
2369static ssize_t port3_lun_table_show(struct device *dev,
2370 struct device_attribute *attr,
2371 char *buf)
2372{
2373 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2374
2375 return cxlflash_show_port_lun_table(3, cfg, buf);
2376}
2377
2378/**
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002379 * irqpoll_weight_show() - presents the current IRQ poll weight for the host
2380 * @dev: Generic device associated with the host.
2381 * @attr: Device attribute representing the IRQ poll weight.
2382 * @buf: Buffer of length PAGE_SIZE to report back the current IRQ poll
2383 * weight in ASCII.
2384 *
2385 * An IRQ poll weight of 0 indicates polling is disabled.
2386 *
2387 * Return: The size of the ASCII string returned in @buf.
2388 */
2389static ssize_t irqpoll_weight_show(struct device *dev,
2390 struct device_attribute *attr, char *buf)
2391{
2392 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2393 struct afu *afu = cfg->afu;
2394
2395 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->irqpoll_weight);
2396}
2397
2398/**
2399 * irqpoll_weight_store() - sets the current IRQ poll weight for the host
2400 * @dev: Generic device associated with the host.
2401 * @attr: Device attribute representing the IRQ poll weight.
2402 * @buf: Buffer of length PAGE_SIZE containing the desired IRQ poll
2403 * weight in ASCII.
2404 * @count: Length of data resizing in @buf.
2405 *
2406 * An IRQ poll weight of 0 indicates polling is disabled.
2407 *
2408 * Return: The size of the ASCII string returned in @buf.
2409 */
2410static ssize_t irqpoll_weight_store(struct device *dev,
2411 struct device_attribute *attr,
2412 const char *buf, size_t count)
2413{
2414 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2415 struct device *cfgdev = &cfg->dev->dev;
2416 struct afu *afu = cfg->afu;
2417 u32 weight;
2418 int rc;
2419
2420 rc = kstrtouint(buf, 10, &weight);
2421 if (rc)
2422 return -EINVAL;
2423
2424 if (weight > 256) {
2425 dev_info(cfgdev,
2426 "Invalid IRQ poll weight. It must be 256 or less.\n");
2427 return -EINVAL;
2428 }
2429
2430 if (weight == afu->irqpoll_weight) {
2431 dev_info(cfgdev,
2432 "Current IRQ poll weight has the same weight.\n");
2433 return -EINVAL;
2434 }
2435
2436 if (afu_is_irqpoll_enabled(afu))
2437 irq_poll_disable(&afu->irqpoll);
2438
2439 afu->irqpoll_weight = weight;
2440
2441 if (weight > 0)
2442 irq_poll_init(&afu->irqpoll, weight, cxlflash_irqpoll);
2443
2444 return count;
2445}
2446
2447/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002448 * mode_show() - presents the current mode of the device
Matthew R. Ochs15305512015-10-21 15:12:10 -05002449 * @dev: Generic device associated with the device.
2450 * @attr: Device attribute representing the device mode.
2451 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2452 *
2453 * Return: The size of the ASCII string returned in @buf.
2454 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002455static ssize_t mode_show(struct device *dev,
2456 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002457{
2458 struct scsi_device *sdev = to_scsi_device(dev);
2459
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002460 return scnprintf(buf, PAGE_SIZE, "%s\n",
2461 sdev->hostdata ? "superpipe" : "legacy");
Matthew R. Ochs15305512015-10-21 15:12:10 -05002462}
2463
2464/*
2465 * Host attributes
2466 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002467static DEVICE_ATTR_RO(port0);
2468static DEVICE_ATTR_RO(port1);
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002469static DEVICE_ATTR_RO(port2);
2470static DEVICE_ATTR_RO(port3);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002471static DEVICE_ATTR_RW(lun_mode);
2472static DEVICE_ATTR_RO(ioctl_version);
2473static DEVICE_ATTR_RO(port0_lun_table);
2474static DEVICE_ATTR_RO(port1_lun_table);
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002475static DEVICE_ATTR_RO(port2_lun_table);
2476static DEVICE_ATTR_RO(port3_lun_table);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002477static DEVICE_ATTR_RW(irqpoll_weight);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002478
2479static struct device_attribute *cxlflash_host_attrs[] = {
2480 &dev_attr_port0,
2481 &dev_attr_port1,
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002482 &dev_attr_port2,
2483 &dev_attr_port3,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002484 &dev_attr_lun_mode,
2485 &dev_attr_ioctl_version,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002486 &dev_attr_port0_lun_table,
2487 &dev_attr_port1_lun_table,
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002488 &dev_attr_port2_lun_table,
2489 &dev_attr_port3_lun_table,
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002490 &dev_attr_irqpoll_weight,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002491 NULL
2492};
2493
2494/*
2495 * Device attributes
2496 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002497static DEVICE_ATTR_RO(mode);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002498
2499static struct device_attribute *cxlflash_dev_attrs[] = {
2500 &dev_attr_mode,
2501 NULL
2502};
2503
2504/*
2505 * Host template
2506 */
2507static struct scsi_host_template driver_template = {
2508 .module = THIS_MODULE,
2509 .name = CXLFLASH_ADAPTER_NAME,
2510 .info = cxlflash_driver_info,
2511 .ioctl = cxlflash_ioctl,
2512 .proc_name = CXLFLASH_NAME,
2513 .queuecommand = cxlflash_queuecommand,
2514 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
2515 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
2516 .change_queue_depth = cxlflash_change_queue_depth,
Manoj N. Kumar83430832016-03-04 15:55:20 -06002517 .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002518 .can_queue = CXLFLASH_MAX_CMDS,
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -06002519 .cmd_size = sizeof(struct afu_cmd) + __alignof__(struct afu_cmd) - 1,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002520 .this_id = -1,
Uma Krishnan68ab2d72016-11-28 18:41:06 -06002521 .sg_tablesize = 1, /* No scatter gather support */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002522 .max_sectors = CXLFLASH_MAX_SECTORS,
2523 .use_clustering = ENABLE_CLUSTERING,
2524 .shost_attrs = cxlflash_host_attrs,
2525 .sdev_attrs = cxlflash_dev_attrs,
2526};
2527
2528/*
2529 * Device dependent values
2530 */
Uma Krishnan96e1b662016-06-15 18:49:38 -05002531static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
2532 0ULL };
2533static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
Uma Krishnan704c4b02016-06-15 18:49:57 -05002534 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs94344522017-02-16 21:39:32 -06002535static struct dev_dependent_vals dev_briard_vals = { CXLFLASH_MAX_SECTORS,
2536 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs15305512015-10-21 15:12:10 -05002537
2538/*
2539 * PCI device binding table
2540 */
2541static struct pci_device_id cxlflash_pci_table[] = {
2542 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
2543 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
Manoj Kumara2746fb2015-12-14 15:07:43 -06002544 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
2545 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
Matthew R. Ochs94344522017-02-16 21:39:32 -06002546 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_BRIARD,
2547 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_briard_vals},
Matthew R. Ochs15305512015-10-21 15:12:10 -05002548 {}
2549};
2550
2551MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
2552
2553/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002554 * cxlflash_worker_thread() - work thread handler for the AFU
2555 * @work: Work structure contained within cxlflash associated with host.
2556 *
2557 * Handles the following events:
2558 * - Link reset which cannot be performed on interrupt context due to
2559 * blocking up to a few seconds
Matthew R. Ochsef510742015-10-21 15:13:37 -05002560 * - Rescan the host
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002561 */
2562static void cxlflash_worker_thread(struct work_struct *work)
2563{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002564 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
2565 work_q);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002566 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002567 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002568 __be64 __iomem *fc_port_regs;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002569 int port;
2570 ulong lock_flags;
2571
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002572 /* Avoid MMIO if the device has failed */
2573
2574 if (cfg->state != STATE_NORMAL)
2575 return;
2576
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002577 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2578
2579 if (cfg->lr_state == LINK_RESET_REQUIRED) {
2580 port = cfg->lr_port;
2581 if (port < 0)
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002582 dev_err(dev, "%s: invalid port index %d\n",
2583 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002584 else {
2585 spin_unlock_irqrestore(cfg->host->host_lock,
2586 lock_flags);
2587
2588 /* The reset can block... */
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002589 fc_port_regs = get_fc_port_regs(cfg, port);
2590 afu_link_reset(afu, port, fc_port_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002591 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2592 }
2593
2594 cfg->lr_state = LINK_RESET_COMPLETE;
2595 }
2596
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002597 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
Matthew R. Ochsef510742015-10-21 15:13:37 -05002598
2599 if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
2600 scsi_scan_host(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002601}
2602
2603/**
2604 * cxlflash_probe() - PCI entry point to add host
2605 * @pdev: PCI device associated with the host.
2606 * @dev_id: PCI device id associated with device.
2607 *
Matthew R. Ochs323e3342017-04-12 14:14:51 -05002608 * The device will initially start out in a 'probing' state and
2609 * transition to the 'normal' state at the end of a successful
2610 * probe. Should an EEH event occur during probe, the notification
2611 * thread (error_detected()) will wait until the probe handler
2612 * is nearly complete. At that time, the device will be moved to
2613 * a 'probed' state and the EEH thread woken up to drive the slot
2614 * reset and recovery (device moves to 'normal' state). Meanwhile,
2615 * the probe will be allowed to exit successfully.
2616 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002617 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002618 */
2619static int cxlflash_probe(struct pci_dev *pdev,
2620 const struct pci_device_id *dev_id)
2621{
2622 struct Scsi_Host *host;
2623 struct cxlflash_cfg *cfg = NULL;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002624 struct device *dev = &pdev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002625 struct dev_dependent_vals *ddv;
2626 int rc = 0;
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002627 int k;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002628
2629 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
2630 __func__, pdev->irq);
2631
2632 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
2633 driver_template.max_sectors = ddv->max_sectors;
2634
2635 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
2636 if (!host) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002637 dev_err(dev, "%s: scsi_host_alloc failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002638 rc = -ENOMEM;
2639 goto out;
2640 }
2641
2642 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
2643 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002644 host->unique_id = host->host_no;
2645 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
2646
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002647 cfg = shost_priv(host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002648 cfg->host = host;
2649 rc = alloc_mem(cfg);
2650 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002651 dev_err(dev, "%s: alloc_mem failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002652 rc = -ENOMEM;
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -05002653 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002654 goto out;
2655 }
2656
2657 cfg->init_state = INIT_STATE_NONE;
2658 cfg->dev = pdev;
Matthew R. Ochs17ead262015-10-21 15:15:37 -05002659 cfg->cxl_fops = cxlflash_cxl_fops;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002660
2661 /*
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002662 * Promoted LUNs move to the top of the LUN table. The rest stay on
2663 * the bottom half. The bottom half grows from the end (index = 255),
2664 * whereas the top half grows from the beginning (index = 0).
2665 *
2666 * Initialize the last LUN index for all possible ports.
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002667 */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002668 cfg->promote_lun_index = 0;
2669
2670 for (k = 0; k < MAX_FC_PORTS; k++)
2671 cfg->last_lun_index[k] = CXLFLASH_NUM_VLUNS/2 - 1;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002672
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002673 cfg->dev_id = (struct pci_device_id *)dev_id;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002674
2675 init_waitqueue_head(&cfg->tmf_waitq);
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002676 init_waitqueue_head(&cfg->reset_waitq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002677
2678 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
2679 cfg->lr_state = LINK_RESET_INVALID;
2680 cfg->lr_port = -1;
Matthew R. Ochs0d731222015-10-21 15:16:24 -05002681 spin_lock_init(&cfg->tmf_slock);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002682 mutex_init(&cfg->ctx_tbl_list_mutex);
2683 mutex_init(&cfg->ctx_recovery_mutex);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002684 init_rwsem(&cfg->ioctl_rwsem);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002685 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
2686 INIT_LIST_HEAD(&cfg->lluns);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002687
2688 pci_set_drvdata(pdev, cfg);
2689
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002690 cfg->cxl_afu = cxl_pci_to_afu(pdev);
2691
2692 rc = init_pci(cfg);
2693 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002694 dev_err(dev, "%s: init_pci failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002695 goto out_remove;
2696 }
2697 cfg->init_state = INIT_STATE_PCI;
2698
2699 rc = init_afu(cfg);
Matthew R. Ochs323e3342017-04-12 14:14:51 -05002700 if (rc && !wq_has_sleeper(&cfg->reset_waitq)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002701 dev_err(dev, "%s: init_afu failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002702 goto out_remove;
2703 }
2704 cfg->init_state = INIT_STATE_AFU;
2705
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002706 rc = init_scsi(cfg);
2707 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002708 dev_err(dev, "%s: init_scsi failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002709 goto out_remove;
2710 }
2711 cfg->init_state = INIT_STATE_SCSI;
2712
Matthew R. Ochs323e3342017-04-12 14:14:51 -05002713 if (wq_has_sleeper(&cfg->reset_waitq)) {
2714 cfg->state = STATE_PROBED;
2715 wake_up_all(&cfg->reset_waitq);
2716 } else
2717 cfg->state = STATE_NORMAL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002718out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002719 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002720 return rc;
2721
2722out_remove:
2723 cxlflash_remove(pdev);
2724 goto out;
2725}
2726
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002727/**
2728 * cxlflash_pci_error_detected() - called when a PCI error is detected
2729 * @pdev: PCI device struct.
2730 * @state: PCI channel state.
2731 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002732 * When an EEH occurs during an active reset, wait until the reset is
2733 * complete and then take action based upon the device state.
2734 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002735 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2736 */
2737static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
2738 pci_channel_state_t state)
2739{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002740 int rc = 0;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002741 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2742 struct device *dev = &cfg->dev->dev;
2743
2744 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
2745
2746 switch (state) {
2747 case pci_channel_io_frozen:
Matthew R. Ochs323e3342017-04-12 14:14:51 -05002748 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET &&
2749 cfg->state != STATE_PROBING);
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002750 if (cfg->state == STATE_FAILTERM)
2751 return PCI_ERS_RESULT_DISCONNECT;
2752
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002753 cfg->state = STATE_RESET;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002754 scsi_block_requests(cfg->host);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002755 drain_ioctls(cfg);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002756 rc = cxlflash_mark_contexts_error(cfg);
2757 if (unlikely(rc))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002758 dev_err(dev, "%s: Failed to mark user contexts rc=%d\n",
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002759 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05002760 term_afu(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002761 return PCI_ERS_RESULT_NEED_RESET;
2762 case pci_channel_io_perm_failure:
2763 cfg->state = STATE_FAILTERM;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002764 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002765 scsi_unblock_requests(cfg->host);
2766 return PCI_ERS_RESULT_DISCONNECT;
2767 default:
2768 break;
2769 }
2770 return PCI_ERS_RESULT_NEED_RESET;
2771}
2772
2773/**
2774 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2775 * @pdev: PCI device struct.
2776 *
2777 * This routine is called by the pci error recovery code after the PCI
2778 * slot has been reset, just before we should resume normal operations.
2779 *
2780 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2781 */
2782static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
2783{
2784 int rc = 0;
2785 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2786 struct device *dev = &cfg->dev->dev;
2787
2788 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2789
2790 rc = init_afu(cfg);
2791 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002792 dev_err(dev, "%s: EEH recovery failed rc=%d\n", __func__, rc);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002793 return PCI_ERS_RESULT_DISCONNECT;
2794 }
2795
2796 return PCI_ERS_RESULT_RECOVERED;
2797}
2798
2799/**
2800 * cxlflash_pci_resume() - called when normal operation can resume
2801 * @pdev: PCI device struct
2802 */
2803static void cxlflash_pci_resume(struct pci_dev *pdev)
2804{
2805 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2806 struct device *dev = &cfg->dev->dev;
2807
2808 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2809
2810 cfg->state = STATE_NORMAL;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002811 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002812 scsi_unblock_requests(cfg->host);
2813}
2814
2815static const struct pci_error_handlers cxlflash_err_handler = {
2816 .error_detected = cxlflash_pci_error_detected,
2817 .slot_reset = cxlflash_pci_slot_reset,
2818 .resume = cxlflash_pci_resume,
2819};
2820
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002821/*
2822 * PCI device structure
2823 */
2824static struct pci_driver cxlflash_driver = {
2825 .name = CXLFLASH_NAME,
2826 .id_table = cxlflash_pci_table,
2827 .probe = cxlflash_probe,
2828 .remove = cxlflash_remove,
Uma Krishnanbabf9852016-09-02 15:39:16 -05002829 .shutdown = cxlflash_remove,
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002830 .err_handler = &cxlflash_err_handler,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002831};
2832
2833/**
2834 * init_cxlflash() - module entry point
2835 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002836 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002837 */
2838static int __init init_cxlflash(void)
2839{
Matthew R. Ochscd41e182017-04-12 14:15:11 -05002840 check_sizes();
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002841 cxlflash_list_init();
2842
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002843 return pci_register_driver(&cxlflash_driver);
2844}
2845
2846/**
2847 * exit_cxlflash() - module exit point
2848 */
2849static void __exit exit_cxlflash(void)
2850{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002851 cxlflash_term_global_luns();
2852 cxlflash_free_errpage();
2853
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002854 pci_unregister_driver(&cxlflash_driver);
2855}
2856
2857module_init(init_cxlflash);
2858module_exit(exit_cxlflash);