blob: ab38bca5df2b442eb385cc4b42364cfddee1a178 [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 scsi_dma_unmap(scp);
180 scp->scsi_done(scp);
181
182 if (cmd_is_tmf) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500183 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500184 cfg->tmf_active = false;
185 wake_up_all_locked(&cfg->tmf_waitq);
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500186 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500187 }
188 } else
189 complete(&cmd->cevent);
190}
191
192/**
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600193 * context_reset() - reset command owner context via specified register
Matthew R. Ochs15305512015-10-21 15:12:10 -0500194 * @cmd: AFU command that timed out.
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600195 * @reset_reg: MMIO register to perform reset.
Matthew R. Ochs15305512015-10-21 15:12:10 -0500196 */
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600197static void context_reset(struct afu_cmd *cmd, __be64 __iomem *reset_reg)
Matthew R. Ochs15305512015-10-21 15:12:10 -0500198{
199 int nretry = 0;
200 u64 rrin = 0x1;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500201 struct afu *afu = cmd->parent;
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600202 struct cxlflash_cfg *cfg = afu->parent;
203 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500204
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600205 dev_dbg(dev, "%s: cmd=%p\n", __func__, cmd);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500206
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600207 writeq_be(rrin, reset_reg);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500208 do {
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600209 rrin = readq_be(reset_reg);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500210 if (rrin != 0x1)
211 break;
212 /* Double delay each time */
Manoj N. Kumarea765432016-03-25 14:26:49 -0500213 udelay(1 << nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500214 } while (nretry++ < MC_ROOM_RETRY_CNT);
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600215
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600216 dev_dbg(dev, "%s: returning rrin=%016llx nretry=%d\n",
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600217 __func__, rrin, nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500218}
219
220/**
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600221 * context_reset_ioarrin() - reset command owner context via IOARRIN register
222 * @cmd: AFU command that timed out.
223 */
224static void context_reset_ioarrin(struct afu_cmd *cmd)
225{
226 struct afu *afu = cmd->parent;
227
228 context_reset(cmd, &afu->host_map->ioarrin);
229}
230
231/**
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600232 * context_reset_sq() - reset command owner context w/ SQ Context Reset register
233 * @cmd: AFU command that timed out.
234 */
235static void context_reset_sq(struct afu_cmd *cmd)
236{
237 struct afu *afu = cmd->parent;
238
239 context_reset(cmd, &afu->host_map->sq_ctx_reset);
240}
241
242/**
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600243 * send_cmd_ioarrin() - sends an AFU command via IOARRIN register
Matthew R. Ochs15305512015-10-21 15:12:10 -0500244 * @afu: AFU associated with the host.
245 * @cmd: AFU command to send.
246 *
247 * Return:
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500248 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochs15305512015-10-21 15:12:10 -0500249 */
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600250static int send_cmd_ioarrin(struct afu *afu, struct afu_cmd *cmd)
Matthew R. Ochs15305512015-10-21 15:12:10 -0500251{
252 struct cxlflash_cfg *cfg = afu->parent;
253 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500254 int rc = 0;
Uma Krishnan11f7b182016-11-28 18:41:45 -0600255 s64 room;
256 ulong lock_flags;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500257
258 /*
Uma Krishnan11f7b182016-11-28 18:41:45 -0600259 * To avoid the performance penalty of MMIO, spread the update of
260 * 'room' over multiple commands.
Matthew R. Ochs15305512015-10-21 15:12:10 -0500261 */
Uma Krishnan11f7b182016-11-28 18:41:45 -0600262 spin_lock_irqsave(&afu->rrin_slock, lock_flags);
263 if (--afu->room < 0) {
264 room = readq_be(&afu->host_map->cmd_room);
265 if (room <= 0) {
266 dev_dbg_ratelimited(dev, "%s: no cmd_room to send "
267 "0x%02X, room=0x%016llX\n",
268 __func__, cmd->rcb.cdb[0], room);
269 afu->room = 0;
270 rc = SCSI_MLQUEUE_HOST_BUSY;
271 goto out;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500272 }
Uma Krishnan11f7b182016-11-28 18:41:45 -0600273 afu->room = room - 1;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500274 }
275
Matthew R. Ochs15305512015-10-21 15:12:10 -0500276 writeq_be((u64)&cmd->rcb, &afu->host_map->ioarrin);
277out:
Uma Krishnan11f7b182016-11-28 18:41:45 -0600278 spin_unlock_irqrestore(&afu->rrin_slock, lock_flags);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600279 dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx rc=%d\n", __func__,
280 cmd, cmd->rcb.data_len, cmd->rcb.data_ea, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500281 return rc;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500282}
283
284/**
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600285 * send_cmd_sq() - sends an AFU command via SQ ring
286 * @afu: AFU associated with the host.
287 * @cmd: AFU command to send.
288 *
289 * Return:
290 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
291 */
292static int send_cmd_sq(struct afu *afu, struct afu_cmd *cmd)
293{
294 struct cxlflash_cfg *cfg = afu->parent;
295 struct device *dev = &cfg->dev->dev;
296 int rc = 0;
297 int newval;
298 ulong lock_flags;
299
300 newval = atomic_dec_if_positive(&afu->hsq_credits);
301 if (newval <= 0) {
302 rc = SCSI_MLQUEUE_HOST_BUSY;
303 goto out;
304 }
305
306 cmd->rcb.ioasa = &cmd->sa;
307
308 spin_lock_irqsave(&afu->hsq_slock, lock_flags);
309
310 *afu->hsq_curr = cmd->rcb;
311 if (afu->hsq_curr < afu->hsq_end)
312 afu->hsq_curr++;
313 else
314 afu->hsq_curr = afu->hsq_start;
315 writeq_be((u64)afu->hsq_curr, &afu->host_map->sq_tail);
316
317 spin_unlock_irqrestore(&afu->hsq_slock, lock_flags);
318out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600319 dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx ioasa=%p rc=%d curr=%p "
320 "head=%016llx tail=%016llx\n", __func__, cmd, cmd->rcb.data_len,
321 cmd->rcb.data_ea, cmd->rcb.ioasa, rc, afu->hsq_curr,
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600322 readq_be(&afu->host_map->sq_head),
323 readq_be(&afu->host_map->sq_tail));
324 return rc;
325}
326
327/**
Matthew R. Ochs15305512015-10-21 15:12:10 -0500328 * wait_resp() - polls for a response or timeout to a sent AFU command
329 * @afu: AFU associated with the host.
330 * @cmd: AFU command that was sent.
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600331 *
332 * Return:
333 * 0 on success, -1 on timeout/error
Matthew R. Ochs15305512015-10-21 15:12:10 -0500334 */
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600335static int wait_resp(struct afu *afu, struct afu_cmd *cmd)
Matthew R. Ochs15305512015-10-21 15:12:10 -0500336{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600337 struct cxlflash_cfg *cfg = afu->parent;
338 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600339 int rc = 0;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500340 ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
341
342 timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600343 if (!timeout) {
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600344 afu->context_reset(cmd);
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600345 rc = -1;
346 }
Matthew R. Ochs15305512015-10-21 15:12:10 -0500347
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600348 if (unlikely(cmd->sa.ioasc != 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600349 dev_err(dev, "%s: cmd %02x failed, ioasc=%08x\n",
350 __func__, cmd->rcb.cdb[0], cmd->sa.ioasc);
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600351 rc = -1;
352 }
353
354 return rc;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500355}
356
357/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500358 * send_tmf() - sends a Task Management Function (TMF)
359 * @afu: AFU to checkout from.
360 * @scp: SCSI command from stack.
361 * @tmfcmd: TMF command to send.
362 *
363 * Return:
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500364 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500365 */
366static int send_tmf(struct afu *afu, struct scsi_cmnd *scp, u64 tmfcmd)
367{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500368 u32 port_sel = scp->device->channel + 1;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600369 struct cxlflash_cfg *cfg = shost_priv(scp->device->host);
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600370 struct afu_cmd *cmd = sc_to_afucz(scp);
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500371 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500372 ulong lock_flags;
373 int rc = 0;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500374 ulong to;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500375
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500376 /* When Task Management Function is active do not send another */
377 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500378 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500379 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
380 !cfg->tmf_active,
381 cfg->tmf_slock);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500382 cfg->tmf_active = true;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500383 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500384
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600385 cmd->scp = scp;
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600386 cmd->parent = afu;
387 cmd->cmd_tmf = true;
388
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500389 cmd->rcb.ctx_id = afu->ctx_hndl;
Matthew R. Ochs5fbb96c82016-11-28 18:42:19 -0600390 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500391 cmd->rcb.port_sel = port_sel;
392 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500393 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600394 SISL_REQ_FLAGS_SUP_UNDERRUN |
395 SISL_REQ_FLAGS_TMF_CMD);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500396 memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
397
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600398 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500399 if (unlikely(rc)) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500400 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500401 cfg->tmf_active = false;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500402 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500403 goto out;
404 }
405
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500406 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
407 to = msecs_to_jiffies(5000);
408 to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
409 !cfg->tmf_active,
410 cfg->tmf_slock,
411 to);
412 if (!to) {
413 cfg->tmf_active = false;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600414 dev_err(dev, "%s: TMF timed out\n", __func__);
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500415 rc = -1;
416 }
417 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500418out:
419 return rc;
420}
421
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600422static void afu_unmap(struct kref *ref)
423{
424 struct afu *afu = container_of(ref, struct afu, mapcount);
425
426 if (likely(afu->afu_map)) {
427 cxl_psa_unmap((void __iomem *)afu->afu_map);
428 afu->afu_map = NULL;
429 }
430}
431
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500432/**
433 * cxlflash_driver_info() - information handler for this host driver
434 * @host: SCSI host associated with device.
435 *
436 * Return: A string describing the device.
437 */
438static const char *cxlflash_driver_info(struct Scsi_Host *host)
439{
440 return CXLFLASH_ADAPTER_NAME;
441}
442
443/**
444 * cxlflash_queuecommand() - sends a mid-layer request
445 * @host: SCSI host associated with device.
446 * @scp: SCSI command to send.
447 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500448 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500449 */
450static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
451{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600452 struct cxlflash_cfg *cfg = shost_priv(host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500453 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500454 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs5fbb96c82016-11-28 18:42:19 -0600455 struct afu_cmd *cmd = sc_to_afucz(scp);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600456 struct scatterlist *sg = scsi_sglist(scp);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500457 u32 port_sel = scp->device->channel + 1;
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600458 u16 req_flags = SISL_REQ_FLAGS_SUP_UNDERRUN;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500459 ulong lock_flags;
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600460 int nseg = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500461 int rc = 0;
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600462 int kref_got = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500463
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500464 dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600465 "cdb=(%08x-%08x-%08x-%08x)\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500466 __func__, scp, host->host_no, scp->device->channel,
467 scp->device->id, scp->device->lun,
468 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
469 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
470 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
471 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500472
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500473 /*
474 * If a Task Management Function is active, wait for it to complete
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500475 * before continuing with regular commands.
476 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500477 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500478 if (cfg->tmf_active) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500479 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500480 rc = SCSI_MLQUEUE_HOST_BUSY;
481 goto out;
482 }
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500483 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500484
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500485 switch (cfg->state) {
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500486 case STATE_RESET:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600487 dev_dbg_ratelimited(dev, "%s: device is in reset\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500488 rc = SCSI_MLQUEUE_HOST_BUSY;
489 goto out;
490 case STATE_FAILTERM:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600491 dev_dbg_ratelimited(dev, "%s: device has failed\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500492 scp->result = (DID_NO_CONNECT << 16);
493 scp->scsi_done(scp);
494 rc = 0;
495 goto out;
496 default:
497 break;
498 }
499
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600500 kref_get(&cfg->afu->mapcount);
501 kref_got = 1;
502
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600503 if (likely(sg)) {
504 nseg = scsi_dma_map(scp);
505 if (unlikely(nseg < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600506 dev_err(dev, "%s: Fail DMA map\n", __func__);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600507 rc = SCSI_MLQUEUE_HOST_BUSY;
508 goto out;
509 }
510
511 cmd->rcb.data_len = sg_dma_len(sg);
512 cmd->rcb.data_ea = sg_dma_address(sg);
513 }
514
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600515 cmd->scp = scp;
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600516 cmd->parent = afu;
517
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500518 cmd->rcb.ctx_id = afu->ctx_hndl;
Matthew R. Ochs5fbb96c82016-11-28 18:42:19 -0600519 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500520 cmd->rcb.port_sel = port_sel;
521 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
522
523 if (scp->sc_data_direction == DMA_TO_DEVICE)
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600524 req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500525
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600526 cmd->rcb.req_flags = req_flags;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500527 memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
528
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600529 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochs5fbb96c82016-11-28 18:42:19 -0600530 if (unlikely(rc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500531 scsi_dma_unmap(scp);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500532out:
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600533 if (kref_got)
534 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500535 return rc;
536}
537
538/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500539 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500540 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500541 */
542static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
543{
544 struct pci_dev *pdev = cfg->dev;
545
546 if (pci_channel_offline(pdev))
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500547 wait_event_timeout(cfg->reset_waitq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500548 !pci_channel_offline(pdev),
549 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
550}
551
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500552/**
553 * free_mem() - free memory associated with the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500554 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500555 */
556static void free_mem(struct cxlflash_cfg *cfg)
557{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500558 struct afu *afu = cfg->afu;
559
560 if (cfg->afu) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500561 free_pages((ulong)afu, get_order(sizeof(struct afu)));
562 cfg->afu = NULL;
563 }
564}
565
566/**
567 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
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 *
570 * Safe to call with AFU in a partially allocated/initialized state.
Manoj Kumaree91e332015-12-14 15:07:02 -0600571 *
Matthew R. Ochsde012832016-11-28 18:42:33 -0600572 * Waits for any active internal AFU commands to timeout and then unmaps
Manoj Kumaree91e332015-12-14 15:07:02 -0600573 * the MMIO space.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500574 */
575static void stop_afu(struct cxlflash_cfg *cfg)
576{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500577 struct afu *afu = cfg->afu;
578
579 if (likely(afu)) {
Matthew R. Ochsde012832016-11-28 18:42:33 -0600580 while (atomic_read(&afu->cmds_active))
581 ssleep(1);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500582 if (likely(afu->afu_map)) {
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500583 cxl_psa_unmap((void __iomem *)afu->afu_map);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500584 afu->afu_map = NULL;
585 }
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600586 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500587 }
588}
589
590/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500591 * term_intr() - disables all AFU interrupts
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500592 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500593 * @level: Depth of allocation, where to begin waterfall tear down.
594 *
595 * Safe to call with AFU/MC in partially allocated/initialized state.
596 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500597static void term_intr(struct cxlflash_cfg *cfg, enum undo_level level)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500598{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500599 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500600 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500601
602 if (!afu || !cfg->mcctx) {
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500603 dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500604 return;
605 }
606
607 switch (level) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500608 case UNMAP_THREE:
609 cxl_unmap_afu_irq(cfg->mcctx, 3, afu);
610 case UNMAP_TWO:
611 cxl_unmap_afu_irq(cfg->mcctx, 2, afu);
612 case UNMAP_ONE:
613 cxl_unmap_afu_irq(cfg->mcctx, 1, afu);
614 case FREE_IRQ:
615 cxl_free_afu_irqs(cfg->mcctx);
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500616 /* fall through */
617 case UNDO_NOOP:
618 /* No action required */
619 break;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500620 }
621}
622
623/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500624 * term_mc() - terminates the master context
625 * @cfg: Internal structure associated with the host.
626 * @level: Depth of allocation, where to begin waterfall tear down.
627 *
628 * Safe to call with AFU/MC in partially allocated/initialized state.
629 */
630static void term_mc(struct cxlflash_cfg *cfg)
631{
632 int rc = 0;
633 struct afu *afu = cfg->afu;
634 struct device *dev = &cfg->dev->dev;
635
636 if (!afu || !cfg->mcctx) {
637 dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
638 return;
639 }
640
641 rc = cxl_stop_context(cfg->mcctx);
642 WARN_ON(rc);
643 cfg->mcctx = NULL;
644}
645
646/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500647 * term_afu() - terminates the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500648 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500649 *
650 * Safe to call with AFU/MC in partially allocated/initialized state.
651 */
652static void term_afu(struct cxlflash_cfg *cfg)
653{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600654 struct device *dev = &cfg->dev->dev;
655
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500656 /*
657 * Tear down is carefully orchestrated to ensure
658 * no interrupts can come in when the problem state
659 * area is unmapped.
660 *
661 * 1) Disable all AFU interrupts
662 * 2) Unmap the problem state area
663 * 3) Stop the master context
664 */
665 term_intr(cfg, UNMAP_THREE);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500666 if (cfg->afu)
667 stop_afu(cfg);
668
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500669 term_mc(cfg);
Uma Krishnan6ded8b32016-03-04 15:55:15 -0600670
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600671 dev_dbg(dev, "%s: returning\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500672}
673
674/**
Uma Krishnan704c4b02016-06-15 18:49:57 -0500675 * notify_shutdown() - notifies device of pending shutdown
676 * @cfg: Internal structure associated with the host.
677 * @wait: Whether to wait for shutdown processing to complete.
678 *
679 * This function will notify the AFU that the adapter is being shutdown
680 * and will wait for shutdown processing to complete if wait is true.
681 * This notification should flush pending I/Os to the device and halt
682 * further I/Os until the next AFU reset is issued and device restarted.
683 */
684static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
685{
686 struct afu *afu = cfg->afu;
687 struct device *dev = &cfg->dev->dev;
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500688 struct sisl_global_map __iomem *global;
Uma Krishnan704c4b02016-06-15 18:49:57 -0500689 struct dev_dependent_vals *ddv;
690 u64 reg, status;
691 int i, retry_cnt = 0;
692
693 ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
694 if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
695 return;
696
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500697 if (!afu || !afu->afu_map) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600698 dev_dbg(dev, "%s: Problem state area not mapped\n", __func__);
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500699 return;
700 }
701
702 global = &afu->afu_map->global;
703
Uma Krishnan704c4b02016-06-15 18:49:57 -0500704 /* Notify AFU */
705 for (i = 0; i < NUM_FC_PORTS; i++) {
706 reg = readq_be(&global->fc_regs[i][FC_CONFIG2 / 8]);
707 reg |= SISL_FC_SHUTDOWN_NORMAL;
708 writeq_be(reg, &global->fc_regs[i][FC_CONFIG2 / 8]);
709 }
710
711 if (!wait)
712 return;
713
714 /* Wait up to 1.5 seconds for shutdown processing to complete */
715 for (i = 0; i < NUM_FC_PORTS; i++) {
716 retry_cnt = 0;
717 while (true) {
718 status = readq_be(&global->fc_regs[i][FC_STATUS / 8]);
719 if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
720 break;
721 if (++retry_cnt >= MC_RETRY_CNT) {
722 dev_dbg(dev, "%s: port %d shutdown processing "
723 "not yet completed\n", __func__, i);
724 break;
725 }
726 msleep(100 * retry_cnt);
727 }
728 }
729}
730
731/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500732 * cxlflash_remove() - PCI entry point to tear down host
733 * @pdev: PCI device associated with the host.
734 *
735 * Safe to use as a cleanup in partially allocated/initialized state.
736 */
737static void cxlflash_remove(struct pci_dev *pdev)
738{
739 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600740 struct device *dev = &pdev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500741 ulong lock_flags;
742
Uma Krishnanbabf9852016-09-02 15:39:16 -0500743 if (!pci_is_enabled(pdev)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600744 dev_dbg(dev, "%s: Device is disabled\n", __func__);
Uma Krishnanbabf9852016-09-02 15:39:16 -0500745 return;
746 }
747
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500748 /* If a Task Management Function is active, wait for it to complete
749 * before continuing with remove.
750 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500751 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500752 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500753 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
754 !cfg->tmf_active,
755 cfg->tmf_slock);
756 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500757
Uma Krishnan704c4b02016-06-15 18:49:57 -0500758 /* Notify AFU and wait for shutdown processing to complete */
759 notify_shutdown(cfg, true);
760
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500761 cfg->state = STATE_FAILTERM;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500762 cxlflash_stop_term_user_contexts(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500763
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500764 switch (cfg->init_state) {
765 case INIT_STATE_SCSI:
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500766 cxlflash_term_local_luns(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500767 scsi_remove_host(cfg->host);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -0500768 /* fall through */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500769 case INIT_STATE_AFU:
Matthew R. Ochsd8046212015-10-21 15:14:17 -0500770 cancel_work_sync(&cfg->work_q);
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600771 term_afu(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500772 case INIT_STATE_PCI:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500773 pci_disable_device(pdev);
774 case INIT_STATE_NONE:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500775 free_mem(cfg);
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -0500776 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500777 break;
778 }
779
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600780 dev_dbg(dev, "%s: returning\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500781}
782
783/**
784 * alloc_mem() - allocates the AFU and its command pool
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500785 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500786 *
787 * A partially allocated state remains on failure.
788 *
789 * Return:
790 * 0 on success
791 * -ENOMEM on failure to allocate memory
792 */
793static int alloc_mem(struct cxlflash_cfg *cfg)
794{
795 int rc = 0;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500796 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500797
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600798 /* AFU is ~28k, i.e. only one 64k page or up to seven 4k pages */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500799 cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
800 get_order(sizeof(struct afu)));
801 if (unlikely(!cfg->afu)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500802 dev_err(dev, "%s: cannot get %d free pages\n",
803 __func__, get_order(sizeof(struct afu)));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500804 rc = -ENOMEM;
805 goto out;
806 }
807 cfg->afu->parent = cfg;
808 cfg->afu->afu_map = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500809out:
810 return rc;
811}
812
813/**
814 * init_pci() - initializes the host as a PCI device
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500815 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500816 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500817 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500818 */
819static int init_pci(struct cxlflash_cfg *cfg)
820{
821 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600822 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500823 int rc = 0;
824
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500825 rc = pci_enable_device(pdev);
826 if (rc || pci_channel_offline(pdev)) {
827 if (pci_channel_offline(pdev)) {
828 cxlflash_wait_for_pci_err_recovery(cfg);
829 rc = pci_enable_device(pdev);
830 }
831
832 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600833 dev_err(dev, "%s: Cannot enable adapter\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500834 cxlflash_wait_for_pci_err_recovery(cfg);
Manoj N. Kumar961487e2016-03-04 15:55:14 -0600835 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500836 }
837 }
838
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500839out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600840 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500841 return rc;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500842}
843
844/**
845 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500846 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500847 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500848 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500849 */
850static int init_scsi(struct cxlflash_cfg *cfg)
851{
852 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600853 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500854 int rc = 0;
855
856 rc = scsi_add_host(cfg->host, &pdev->dev);
857 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600858 dev_err(dev, "%s: scsi_add_host failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500859 goto out;
860 }
861
862 scsi_scan_host(cfg->host);
863
864out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600865 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500866 return rc;
867}
868
869/**
870 * set_port_online() - transitions the specified host FC port to online state
871 * @fc_regs: Top of MMIO region defined for specified port.
872 *
873 * The provided MMIO region must be mapped prior to call. Online state means
874 * that the FC link layer has synced, completed the handshaking process, and
875 * is ready for login to start.
876 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500877static void set_port_online(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500878{
879 u64 cmdcfg;
880
881 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
882 cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
883 cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
884 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
885}
886
887/**
888 * set_port_offline() - transitions the specified host FC port to offline state
889 * @fc_regs: Top of MMIO region defined for specified port.
890 *
891 * The provided MMIO region must be mapped prior to call.
892 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500893static void set_port_offline(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500894{
895 u64 cmdcfg;
896
897 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
898 cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
899 cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
900 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
901}
902
903/**
904 * wait_port_online() - waits for the specified host FC port come online
905 * @fc_regs: Top of MMIO region defined for specified port.
906 * @delay_us: Number of microseconds to delay between reading port status.
907 * @nretry: Number of cycles to retry reading port status.
908 *
909 * The provided MMIO region must be mapped prior to call. This will timeout
910 * when the cable is not plugged in.
911 *
912 * Return:
913 * TRUE (1) when the specified port is online
914 * FALSE (0) when the specified port fails to come online after timeout
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500915 */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600916static bool wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500917{
918 u64 status;
919
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600920 WARN_ON(delay_us < 1000);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500921
922 do {
923 msleep(delay_us / 1000);
924 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -0500925 if (status == U64_MAX)
926 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500927 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
928 nretry--);
929
930 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
931}
932
933/**
934 * wait_port_offline() - waits for the specified host FC port go offline
935 * @fc_regs: Top of MMIO region defined for specified port.
936 * @delay_us: Number of microseconds to delay between reading port status.
937 * @nretry: Number of cycles to retry reading port status.
938 *
939 * The provided MMIO region must be mapped prior to call.
940 *
941 * Return:
942 * TRUE (1) when the specified port is offline
943 * FALSE (0) when the specified port fails to go offline after timeout
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500944 */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600945static bool wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500946{
947 u64 status;
948
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600949 WARN_ON(delay_us < 1000);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500950
951 do {
952 msleep(delay_us / 1000);
953 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -0500954 if (status == U64_MAX)
955 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500956 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
957 nretry--);
958
959 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
960}
961
962/**
963 * afu_set_wwpn() - configures the WWPN for the specified host FC port
964 * @afu: AFU associated with the host that owns the specified FC port.
965 * @port: Port number being configured.
966 * @fc_regs: Top of MMIO region defined for specified port.
967 * @wwpn: The world-wide-port-number previously discovered for port.
968 *
969 * The provided MMIO region must be mapped prior to call. As part of the
970 * sequence to configure the WWPN, the port is toggled offline and then back
971 * online. This toggling action can cause this routine to delay up to a few
972 * seconds. When configured to use the internal LUN feature of the AFU, a
973 * failure to come online is overridden.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500974 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -0500975static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
976 u64 wwpn)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500977{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600978 struct cxlflash_cfg *cfg = afu->parent;
979 struct device *dev = &cfg->dev->dev;
980
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500981 set_port_offline(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500982 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
983 FC_PORT_STATUS_RETRY_CNT)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600984 dev_dbg(dev, "%s: wait on port %d to go offline timed out\n",
985 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500986 }
987
Matthew R. Ochsf8013262016-09-02 15:40:20 -0500988 writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
Matthew R. Ochs964497b2015-10-21 15:13:54 -0500989
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500990 set_port_online(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500991 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
992 FC_PORT_STATUS_RETRY_CNT)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600993 dev_dbg(dev, "%s: wait on port %d to go online timed out\n",
994 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500995 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500996}
997
998/**
999 * afu_link_reset() - resets the specified host FC port
1000 * @afu: AFU associated with the host that owns the specified FC port.
1001 * @port: Port number being configured.
1002 * @fc_regs: Top of MMIO region defined for specified port.
1003 *
1004 * The provided MMIO region must be mapped prior to call. The sequence to
1005 * reset the port involves toggling it offline and then back online. This
1006 * action can cause this routine to delay up to a few seconds. An effort
1007 * is made to maintain link with the device by switching to host to use
1008 * the alternate port exclusively while the reset takes place.
1009 * failure to come online is overridden.
1010 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001011static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001012{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001013 struct cxlflash_cfg *cfg = afu->parent;
1014 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001015 u64 port_sel;
1016
1017 /* first switch the AFU to the other links, if any */
1018 port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
Dan Carpenter4da74db2015-08-18 11:57:43 +03001019 port_sel &= ~(1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001020 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1021 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1022
1023 set_port_offline(fc_regs);
1024 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1025 FC_PORT_STATUS_RETRY_CNT))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001026 dev_err(dev, "%s: wait on port %d to go offline timed out\n",
1027 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001028
1029 set_port_online(fc_regs);
1030 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1031 FC_PORT_STATUS_RETRY_CNT))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001032 dev_err(dev, "%s: wait on port %d to go online timed out\n",
1033 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001034
1035 /* switch back to include this port */
Dan Carpenter4da74db2015-08-18 11:57:43 +03001036 port_sel |= (1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001037 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1038 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1039
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001040 dev_dbg(dev, "%s: returning port_sel=%016llx\n", __func__, port_sel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001041}
1042
1043/*
1044 * Asynchronous interrupt information table
1045 */
1046static const struct asyc_intr_info ainfo[] = {
1047 {SISL_ASTATUS_FC0_OTHER, "other error", 0, CLR_FC_ERROR | LINK_RESET},
1048 {SISL_ASTATUS_FC0_LOGO, "target initiated LOGO", 0, 0},
1049 {SISL_ASTATUS_FC0_CRC_T, "CRC threshold exceeded", 0, LINK_RESET},
Manoj Kumare6e6df32015-10-21 15:16:07 -05001050 {SISL_ASTATUS_FC0_LOGI_R, "login timed out, retrying", 0, LINK_RESET},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001051 {SISL_ASTATUS_FC0_LOGI_F, "login failed", 0, CLR_FC_ERROR},
Matthew R. Ochsef510742015-10-21 15:13:37 -05001052 {SISL_ASTATUS_FC0_LOGI_S, "login succeeded", 0, SCAN_HOST},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001053 {SISL_ASTATUS_FC0_LINK_DN, "link down", 0, 0},
Uma Krishnanbbbfae92016-09-02 15:38:48 -05001054 {SISL_ASTATUS_FC0_LINK_UP, "link up", 0, 0},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001055 {SISL_ASTATUS_FC1_OTHER, "other error", 1, CLR_FC_ERROR | LINK_RESET},
1056 {SISL_ASTATUS_FC1_LOGO, "target initiated LOGO", 1, 0},
1057 {SISL_ASTATUS_FC1_CRC_T, "CRC threshold exceeded", 1, LINK_RESET},
Manoj Kumara9be2942015-12-14 14:55:09 -06001058 {SISL_ASTATUS_FC1_LOGI_R, "login timed out, retrying", 1, LINK_RESET},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001059 {SISL_ASTATUS_FC1_LOGI_F, "login failed", 1, CLR_FC_ERROR},
Matthew R. Ochsef510742015-10-21 15:13:37 -05001060 {SISL_ASTATUS_FC1_LOGI_S, "login succeeded", 1, SCAN_HOST},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001061 {SISL_ASTATUS_FC1_LINK_DN, "link down", 1, 0},
Uma Krishnanbbbfae92016-09-02 15:38:48 -05001062 {SISL_ASTATUS_FC1_LINK_UP, "link up", 1, 0},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001063 {0x0, "", 0, 0} /* terminator */
1064};
1065
1066/**
1067 * find_ainfo() - locates and returns asynchronous interrupt information
1068 * @status: Status code set by AFU on error.
1069 *
1070 * Return: The located information or NULL when the status code is invalid.
1071 */
1072static const struct asyc_intr_info *find_ainfo(u64 status)
1073{
1074 const struct asyc_intr_info *info;
1075
1076 for (info = &ainfo[0]; info->status; info++)
1077 if (info->status == status)
1078 return info;
1079
1080 return NULL;
1081}
1082
1083/**
1084 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1085 * @afu: AFU associated with the host.
1086 */
1087static void afu_err_intr_init(struct afu *afu)
1088{
1089 int i;
1090 u64 reg;
1091
1092 /* global async interrupts: AFU clears afu_ctrl on context exit
1093 * if async interrupts were sent to that context. This prevents
1094 * the AFU form sending further async interrupts when
1095 * there is
1096 * nobody to receive them.
1097 */
1098
1099 /* mask all */
1100 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
1101 /* set LISN# to send and point to master context */
1102 reg = ((u64) (((afu->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
1103
1104 if (afu->internal_lun)
1105 reg |= 1; /* Bit 63 indicates local lun */
1106 writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1107 /* clear all */
1108 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1109 /* unmask bits that are of interest */
1110 /* note: afu can send an interrupt after this step */
1111 writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1112 /* clear again in case a bit came on after previous clear but before */
1113 /* unmask */
1114 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1115
1116 /* Clear/Set internal lun bits */
1117 reg = readq_be(&afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1118 reg &= SISL_FC_INTERNAL_MASK;
1119 if (afu->internal_lun)
1120 reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
1121 writeq_be(reg, &afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1122
1123 /* now clear FC errors */
1124 for (i = 0; i < NUM_FC_PORTS; i++) {
1125 writeq_be(0xFFFFFFFFU,
1126 &afu->afu_map->global.fc_regs[i][FC_ERROR / 8]);
1127 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRCAP / 8]);
1128 }
1129
1130 /* sync interrupts for master's IOARRIN write */
1131 /* note that unlike asyncs, there can be no pending sync interrupts */
1132 /* at this time (this is a fresh context and master has not written */
1133 /* IOARRIN yet), so there is nothing to clear. */
1134
1135 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1136 writeq_be(SISL_MSI_SYNC_ERROR, &afu->host_map->ctx_ctrl);
1137 writeq_be(SISL_ISTATUS_MASK, &afu->host_map->intr_mask);
1138}
1139
1140/**
1141 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1142 * @irq: Interrupt number.
1143 * @data: Private data provided at interrupt registration, the AFU.
1144 *
1145 * Return: Always return IRQ_HANDLED.
1146 */
1147static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1148{
1149 struct afu *afu = (struct afu *)data;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001150 struct cxlflash_cfg *cfg = afu->parent;
1151 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001152 u64 reg;
1153 u64 reg_unmasked;
1154
1155 reg = readq_be(&afu->host_map->intr_status);
1156 reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1157
1158 if (reg_unmasked == 0UL) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001159 dev_err(dev, "%s: spurious interrupt, intr_status=%016llx\n",
1160 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001161 goto cxlflash_sync_err_irq_exit;
1162 }
1163
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001164 dev_err(dev, "%s: unexpected interrupt, intr_status=%016llx\n",
1165 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001166
1167 writeq_be(reg_unmasked, &afu->host_map->intr_clear);
1168
1169cxlflash_sync_err_irq_exit:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001170 return IRQ_HANDLED;
1171}
1172
1173/**
1174 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1175 * @irq: Interrupt number.
1176 * @data: Private data provided at interrupt registration, the AFU.
1177 *
1178 * Return: Always return IRQ_HANDLED.
1179 */
1180static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
1181{
1182 struct afu *afu = (struct afu *)data;
1183 struct afu_cmd *cmd;
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001184 struct sisl_ioasa *ioasa;
1185 struct sisl_ioarcb *ioarcb;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001186 bool toggle = afu->toggle;
1187 u64 entry,
1188 *hrrq_start = afu->hrrq_start,
1189 *hrrq_end = afu->hrrq_end,
1190 *hrrq_curr = afu->hrrq_curr;
1191
1192 /* Process however many RRQ entries that are ready */
1193 while (true) {
1194 entry = *hrrq_curr;
1195
1196 if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1197 break;
1198
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001199 entry &= ~SISL_RESP_HANDLE_T_BIT;
1200
1201 if (afu_is_sq_cmd_mode(afu)) {
1202 ioasa = (struct sisl_ioasa *)entry;
1203 cmd = container_of(ioasa, struct afu_cmd, sa);
1204 } else {
1205 ioarcb = (struct sisl_ioarcb *)entry;
1206 cmd = container_of(ioarcb, struct afu_cmd, rcb);
1207 }
1208
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001209 cmd_complete(cmd);
1210
1211 /* Advance to next entry or wrap and flip the toggle bit */
1212 if (hrrq_curr < hrrq_end)
1213 hrrq_curr++;
1214 else {
1215 hrrq_curr = hrrq_start;
1216 toggle ^= SISL_RESP_HANDLE_T_BIT;
1217 }
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001218
1219 atomic_inc(&afu->hsq_credits);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001220 }
1221
1222 afu->hrrq_curr = hrrq_curr;
1223 afu->toggle = toggle;
1224
1225 return IRQ_HANDLED;
1226}
1227
1228/**
1229 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1230 * @irq: Interrupt number.
1231 * @data: Private data provided at interrupt registration, the AFU.
1232 *
1233 * Return: Always return IRQ_HANDLED.
1234 */
1235static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1236{
1237 struct afu *afu = (struct afu *)data;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001238 struct cxlflash_cfg *cfg = afu->parent;
1239 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001240 u64 reg_unmasked;
1241 const struct asyc_intr_info *info;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001242 struct sisl_global_map __iomem *global = &afu->afu_map->global;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001243 u64 reg;
1244 u8 port;
1245 int i;
1246
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001247 reg = readq_be(&global->regs.aintr_status);
1248 reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1249
1250 if (reg_unmasked == 0) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001251 dev_err(dev, "%s: spurious interrupt, aintr_status=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001252 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001253 goto out;
1254 }
1255
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001256 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001257 writeq_be(reg_unmasked, &global->regs.aintr_clear);
1258
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001259 /* Check each bit that is on */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001260 for (i = 0; reg_unmasked; i++, reg_unmasked = (reg_unmasked >> 1)) {
1261 info = find_ainfo(1ULL << i);
Matthew R. Ochs16798d32015-10-21 15:13:45 -05001262 if (((reg_unmasked & 0x1) == 0) || !info)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001263 continue;
1264
1265 port = info->port;
1266
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001267 dev_err(dev, "%s: FC Port %d -> %s, fc_status=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001268 __func__, port, info->desc,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001269 readq_be(&global->fc_regs[port][FC_STATUS / 8]));
1270
1271 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001272 * Do link reset first, some OTHER errors will set FC_ERROR
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001273 * again if cleared before or w/o a reset
1274 */
1275 if (info->action & LINK_RESET) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001276 dev_err(dev, "%s: FC Port %d: resetting link\n",
1277 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001278 cfg->lr_state = LINK_RESET_REQUIRED;
1279 cfg->lr_port = port;
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06001280 kref_get(&cfg->afu->mapcount);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001281 schedule_work(&cfg->work_q);
1282 }
1283
1284 if (info->action & CLR_FC_ERROR) {
1285 reg = readq_be(&global->fc_regs[port][FC_ERROR / 8]);
1286
1287 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001288 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001289 * should be the same and tracing one is sufficient.
1290 */
1291
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001292 dev_err(dev, "%s: fc %d: clearing fc_error=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001293 __func__, port, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001294
1295 writeq_be(reg, &global->fc_regs[port][FC_ERROR / 8]);
1296 writeq_be(0, &global->fc_regs[port][FC_ERRCAP / 8]);
1297 }
Matthew R. Ochsef510742015-10-21 15:13:37 -05001298
1299 if (info->action & SCAN_HOST) {
1300 atomic_inc(&cfg->scan_host_needed);
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06001301 kref_get(&cfg->afu->mapcount);
Matthew R. Ochsef510742015-10-21 15:13:37 -05001302 schedule_work(&cfg->work_q);
1303 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001304 }
1305
1306out:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001307 return IRQ_HANDLED;
1308}
1309
1310/**
1311 * start_context() - starts the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001312 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001313 *
1314 * Return: A success or failure value from CXL services.
1315 */
1316static int start_context(struct cxlflash_cfg *cfg)
1317{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001318 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001319 int rc = 0;
1320
1321 rc = cxl_start_context(cfg->mcctx,
1322 cfg->afu->work.work_element_descriptor,
1323 NULL);
1324
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001325 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001326 return rc;
1327}
1328
1329/**
1330 * read_vpd() - obtains the WWPNs from VPD
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001331 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001332 * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
1333 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001334 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001335 */
1336static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1337{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001338 struct device *dev = &cfg->dev->dev;
1339 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001340 int rc = 0;
1341 int ro_start, ro_size, i, j, k;
1342 ssize_t vpd_size;
1343 char vpd_data[CXLFLASH_VPD_LEN];
1344 char tmp_buf[WWPN_BUF_LEN] = { 0 };
1345 char *wwpn_vpd_tags[NUM_FC_PORTS] = { "V5", "V6" };
1346
1347 /* Get the VPD data from the device */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001348 vpd_size = cxl_read_adapter_vpd(pdev, vpd_data, sizeof(vpd_data));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001349 if (unlikely(vpd_size <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001350 dev_err(dev, "%s: Unable to read VPD (size = %ld)\n",
1351 __func__, vpd_size);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001352 rc = -ENODEV;
1353 goto out;
1354 }
1355
1356 /* Get the read only section offset */
1357 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
1358 PCI_VPD_LRDT_RO_DATA);
1359 if (unlikely(ro_start < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001360 dev_err(dev, "%s: VPD Read-only data not found\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001361 rc = -ENODEV;
1362 goto out;
1363 }
1364
1365 /* Get the read only section size, cap when extends beyond read VPD */
1366 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1367 j = ro_size;
1368 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1369 if (unlikely((i + j) > vpd_size)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001370 dev_dbg(dev, "%s: Might need to read more VPD (%d > %ld)\n",
1371 __func__, (i + j), vpd_size);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001372 ro_size = vpd_size - i;
1373 }
1374
1375 /*
1376 * Find the offset of the WWPN tag within the read only
1377 * VPD data and validate the found field (partials are
1378 * no good to us). Convert the ASCII data to an integer
1379 * value. Note that we must copy to a temporary buffer
1380 * because the conversion service requires that the ASCII
1381 * string be terminated.
1382 */
1383 for (k = 0; k < NUM_FC_PORTS; k++) {
1384 j = ro_size;
1385 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1386
1387 i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
1388 if (unlikely(i < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001389 dev_err(dev, "%s: Port %d WWPN not found in VPD\n",
1390 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001391 rc = -ENODEV;
1392 goto out;
1393 }
1394
1395 j = pci_vpd_info_field_size(&vpd_data[i]);
1396 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1397 if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001398 dev_err(dev, "%s: Port %d WWPN incomplete or bad VPD\n",
1399 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001400 rc = -ENODEV;
1401 goto out;
1402 }
1403
1404 memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1405 rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1406 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001407 dev_err(dev, "%s: WWPN conversion failed for port %d\n",
1408 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001409 rc = -ENODEV;
1410 goto out;
1411 }
1412 }
1413
1414out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001415 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001416 return rc;
1417}
1418
1419/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001420 * init_pcr() - initialize the provisioning and control registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001421 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001422 *
1423 * Also sets up fast access to the mapped registers and initializes AFU
1424 * command fields that never change.
1425 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001426static void init_pcr(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001427{
1428 struct afu *afu = cfg->afu;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001429 struct sisl_ctrl_map __iomem *ctrl_map;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001430 int i;
1431
1432 for (i = 0; i < MAX_CONTEXT; i++) {
1433 ctrl_map = &afu->afu_map->ctrls[i].ctrl;
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001434 /* Disrupt any clients that could be running */
1435 /* e.g. clients that survived a master restart */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001436 writeq_be(0, &ctrl_map->rht_start);
1437 writeq_be(0, &ctrl_map->rht_cnt_id);
1438 writeq_be(0, &ctrl_map->ctx_cap);
1439 }
1440
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001441 /* Copy frequently used fields into afu */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001442 afu->ctx_hndl = (u16) cxl_process_element(cfg->mcctx);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001443 afu->host_map = &afu->afu_map->hosts[afu->ctx_hndl].host;
1444 afu->ctrl_map = &afu->afu_map->ctrls[afu->ctx_hndl].ctrl;
1445
1446 /* Program the Endian Control for the master context */
1447 writeq_be(SISL_ENDIAN_CTRL, &afu->host_map->endian_ctrl);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001448}
1449
1450/**
1451 * init_global() - initialize AFU global registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001452 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001453 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001454static int init_global(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001455{
1456 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001457 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001458 u64 wwpn[NUM_FC_PORTS]; /* wwpn of AFU ports */
1459 int i = 0, num_ports = 0;
1460 int rc = 0;
1461 u64 reg;
1462
1463 rc = read_vpd(cfg, &wwpn[0]);
1464 if (rc) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001465 dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001466 goto out;
1467 }
1468
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001469 dev_dbg(dev, "%s: wwpn0=%016llx wwpn1=%016llx\n",
1470 __func__, wwpn[0], wwpn[1]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001471
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001472 /* Set up RRQ and SQ in AFU for master issued cmds */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001473 writeq_be((u64) afu->hrrq_start, &afu->host_map->rrq_start);
1474 writeq_be((u64) afu->hrrq_end, &afu->host_map->rrq_end);
1475
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001476 if (afu_is_sq_cmd_mode(afu)) {
1477 writeq_be((u64)afu->hsq_start, &afu->host_map->sq_start);
1478 writeq_be((u64)afu->hsq_end, &afu->host_map->sq_end);
1479 }
1480
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001481 /* AFU configuration */
1482 reg = readq_be(&afu->afu_map->global.regs.afu_config);
1483 reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1484 /* enable all auto retry options and control endianness */
1485 /* leave others at default: */
1486 /* CTX_CAP write protected, mbox_r does not clear on read and */
1487 /* checker on if dual afu */
1488 writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1489
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001490 /* Global port select: select either port */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001491 if (afu->internal_lun) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001492 /* Only use port 0 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001493 writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
1494 num_ports = NUM_FC_PORTS - 1;
1495 } else {
1496 writeq_be(BOTH_PORTS, &afu->afu_map->global.regs.afu_port_sel);
1497 num_ports = NUM_FC_PORTS;
1498 }
1499
1500 for (i = 0; i < num_ports; i++) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001501 /* Unmask all errors (but they are still masked at AFU) */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001502 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRMSK / 8]);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001503 /* Clear CRC error cnt & set a threshold */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001504 (void)readq_be(&afu->afu_map->global.
1505 fc_regs[i][FC_CNT_CRCERR / 8]);
1506 writeq_be(MC_CRC_THRESH, &afu->afu_map->global.fc_regs[i]
1507 [FC_CRC_THRESH / 8]);
1508
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001509 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001510 if (wwpn[i] != 0)
1511 afu_set_wwpn(afu, i,
1512 &afu->afu_map->global.fc_regs[i][0],
1513 wwpn[i]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001514 /* Programming WWPN back to back causes additional
1515 * offline/online transitions and a PLOGI
1516 */
1517 msleep(100);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001518 }
1519
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001520 /* Set up master's own CTX_CAP to allow real mode, host translation */
1521 /* tables, afu cmds and read/write GSCSI cmds. */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001522 /* First, unlock ctx_cap write by reading mbox */
1523 (void)readq_be(&afu->ctrl_map->mbox_r); /* unlock ctx_cap */
1524 writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1525 SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1526 SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1527 &afu->ctrl_map->ctx_cap);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001528 /* Initialize heartbeat */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001529 afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001530out:
1531 return rc;
1532}
1533
1534/**
1535 * start_afu() - initializes and starts the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001536 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001537 */
1538static int start_afu(struct cxlflash_cfg *cfg)
1539{
1540 struct afu *afu = cfg->afu;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001541 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001542 int rc = 0;
1543
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001544 init_pcr(cfg);
1545
Matthew R. Ochsaf104832015-10-21 15:15:14 -05001546 /* After an AFU reset, RRQ entries are stale, clear them */
1547 memset(&afu->rrq_entry, 0, sizeof(afu->rrq_entry));
1548
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001549 /* Initialize RRQ pointers */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001550 afu->hrrq_start = &afu->rrq_entry[0];
1551 afu->hrrq_end = &afu->rrq_entry[NUM_RRQ_ENTRY - 1];
1552 afu->hrrq_curr = afu->hrrq_start;
1553 afu->toggle = 1;
1554
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001555 /* Initialize SQ */
1556 if (afu_is_sq_cmd_mode(afu)) {
1557 memset(&afu->sq, 0, sizeof(afu->sq));
1558 afu->hsq_start = &afu->sq[0];
1559 afu->hsq_end = &afu->sq[NUM_SQ_ENTRY - 1];
1560 afu->hsq_curr = afu->hsq_start;
1561
1562 spin_lock_init(&afu->hsq_slock);
1563 atomic_set(&afu->hsq_credits, NUM_SQ_ENTRY - 1);
1564 }
1565
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001566 rc = init_global(cfg);
1567
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001568 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001569 return rc;
1570}
1571
1572/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001573 * init_intr() - setup interrupt handlers for the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001574 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001575 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001576 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001577 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001578static enum undo_level init_intr(struct cxlflash_cfg *cfg,
1579 struct cxl_context *ctx)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001580{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001581 struct afu *afu = cfg->afu;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001582 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001583 int rc = 0;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001584 enum undo_level level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001585
1586 rc = cxl_allocate_afu_irqs(ctx, 3);
1587 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001588 dev_err(dev, "%s: allocate_afu_irqs failed rc=%d\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001589 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001590 level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001591 goto out;
1592 }
1593
1594 rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, afu,
1595 "SISL_MSI_SYNC_ERROR");
1596 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001597 dev_err(dev, "%s: SISL_MSI_SYNC_ERROR map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001598 level = FREE_IRQ;
1599 goto out;
1600 }
1601
1602 rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, afu,
1603 "SISL_MSI_RRQ_UPDATED");
1604 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001605 dev_err(dev, "%s: SISL_MSI_RRQ_UPDATED map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001606 level = UNMAP_ONE;
1607 goto out;
1608 }
1609
1610 rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, afu,
1611 "SISL_MSI_ASYNC_ERROR");
1612 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001613 dev_err(dev, "%s: SISL_MSI_ASYNC_ERROR map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001614 level = UNMAP_TWO;
1615 goto out;
1616 }
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001617out:
1618 return level;
1619}
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001620
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001621/**
1622 * init_mc() - create and register as the master context
1623 * @cfg: Internal structure associated with the host.
1624 *
1625 * Return: 0 on success, -errno on failure
1626 */
1627static int init_mc(struct cxlflash_cfg *cfg)
1628{
1629 struct cxl_context *ctx;
1630 struct device *dev = &cfg->dev->dev;
1631 int rc = 0;
1632 enum undo_level level;
1633
1634 ctx = cxl_get_context(cfg->dev);
1635 if (unlikely(!ctx)) {
1636 rc = -ENOMEM;
1637 goto ret;
1638 }
1639 cfg->mcctx = ctx;
1640
1641 /* Set it up as a master with the CXL */
1642 cxl_set_master(ctx);
1643
1644 /* During initialization reset the AFU to start from a clean slate */
1645 rc = cxl_afu_reset(cfg->mcctx);
1646 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001647 dev_err(dev, "%s: AFU reset failed rc=%d\n", __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001648 goto ret;
1649 }
1650
1651 level = init_intr(cfg, ctx);
1652 if (unlikely(level)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001653 dev_err(dev, "%s: interrupt init failed rc=%d\n", __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001654 goto out;
1655 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001656
1657 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1658 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1659 * element (pe) that is embedded in the context (ctx)
1660 */
1661 rc = start_context(cfg);
1662 if (unlikely(rc)) {
1663 dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
1664 level = UNMAP_THREE;
1665 goto out;
1666 }
1667ret:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001668 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001669 return rc;
1670out:
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001671 term_intr(cfg, level);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001672 goto ret;
1673}
1674
1675/**
1676 * init_afu() - setup as master context and start AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001677 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001678 *
1679 * This routine is a higher level of control for configuring the
1680 * AFU on probe and reset paths.
1681 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001682 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001683 */
1684static int init_afu(struct cxlflash_cfg *cfg)
1685{
1686 u64 reg;
1687 int rc = 0;
1688 struct afu *afu = cfg->afu;
1689 struct device *dev = &cfg->dev->dev;
1690
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001691 cxl_perst_reloads_same_image(cfg->cxl_afu, true);
1692
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001693 rc = init_mc(cfg);
1694 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001695 dev_err(dev, "%s: init_mc failed rc=%d\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001696 __func__, rc);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001697 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001698 }
1699
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001700 /* Map the entire MMIO space of the AFU */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001701 afu->afu_map = cxl_psa_map(cfg->mcctx);
1702 if (!afu->afu_map) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001703 dev_err(dev, "%s: cxl_psa_map failed\n", __func__);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001704 rc = -ENOMEM;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001705 goto err1;
1706 }
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06001707 kref_init(&afu->mapcount);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001708
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001709 /* No byte reverse on reading afu_version or string will be backwards */
1710 reg = readq(&afu->afu_map->global.regs.afu_version);
1711 memcpy(afu->version, &reg, sizeof(reg));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001712 afu->interface_version =
1713 readq_be(&afu->afu_map->global.regs.interface_version);
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001714 if ((afu->interface_version + 1) == 0) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001715 dev_err(dev, "Back level AFU, please upgrade. AFU version %s "
1716 "interface version %016llx\n", afu->version,
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001717 afu->interface_version);
1718 rc = -EINVAL;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001719 goto err2;
1720 }
1721
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001722 if (afu_is_sq_cmd_mode(afu)) {
1723 afu->send_cmd = send_cmd_sq;
1724 afu->context_reset = context_reset_sq;
1725 } else {
1726 afu->send_cmd = send_cmd_ioarrin;
1727 afu->context_reset = context_reset_ioarrin;
1728 }
Matthew R. Ochs48b4be32016-11-28 18:43:09 -06001729
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001730 dev_dbg(dev, "%s: afu_ver=%s interface_ver=%016llx\n", __func__,
1731 afu->version, afu->interface_version);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001732
1733 rc = start_afu(cfg);
1734 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001735 dev_err(dev, "%s: start_afu failed, rc=%d\n", __func__, rc);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001736 goto err2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001737 }
1738
1739 afu_err_intr_init(cfg->afu);
Uma Krishnan11f7b182016-11-28 18:41:45 -06001740 spin_lock_init(&afu->rrin_slock);
1741 afu->room = readq_be(&afu->host_map->cmd_room);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001742
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05001743 /* Restore the LUN mappings */
1744 cxlflash_restore_luntable(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001745out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001746 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001747 return rc;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001748
1749err2:
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06001750 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001751err1:
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001752 term_intr(cfg, UNMAP_THREE);
1753 term_mc(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001754 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001755}
1756
1757/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001758 * cxlflash_afu_sync() - builds and sends an AFU sync command
1759 * @afu: AFU associated with the host.
1760 * @ctx_hndl_u: Identifies context requesting sync.
1761 * @res_hndl_u: Identifies resource requesting sync.
1762 * @mode: Type of sync to issue (lightweight, heavyweight, global).
1763 *
1764 * The AFU can only take 1 sync command at a time. This routine enforces this
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001765 * limitation by using a mutex to provide exclusive access to the AFU during
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001766 * the sync. This design point requires calling threads to not be on interrupt
1767 * context due to the possibility of sleeping during concurrent sync operations.
1768 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001769 * AFU sync operations are only necessary and allowed when the device is
1770 * operating normally. When not operating normally, sync requests can occur as
1771 * part of cleaning up resources associated with an adapter prior to removal.
1772 * In this scenario, these requests are simply ignored (safe due to the AFU
1773 * going away).
1774 *
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001775 * Return:
1776 * 0 on success
1777 * -1 on failure
1778 */
1779int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
1780 res_hndl_t res_hndl_u, u8 mode)
1781{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001782 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001783 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001784 struct afu_cmd *cmd = NULL;
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001785 char *buf = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001786 int rc = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001787 static DEFINE_MUTEX(sync_active);
1788
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001789 if (cfg->state != STATE_NORMAL) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001790 dev_dbg(dev, "%s: Sync not required state=%u\n",
1791 __func__, cfg->state);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001792 return 0;
1793 }
1794
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001795 mutex_lock(&sync_active);
Matthew R. Ochsde012832016-11-28 18:42:33 -06001796 atomic_inc(&afu->cmds_active);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001797 buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
1798 if (unlikely(!buf)) {
1799 dev_err(dev, "%s: no memory for command\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001800 rc = -1;
1801 goto out;
1802 }
1803
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001804 cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
1805 init_completion(&cmd->cevent);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001806 cmd->parent = afu;
1807
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001808 dev_dbg(dev, "%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001809
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001810 cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001811 cmd->rcb.ctx_id = afu->ctx_hndl;
1812 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001813 cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
1814
1815 cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
1816 cmd->rcb.cdb[1] = mode;
1817
1818 /* The cdb is aligned, no unaligned accessors required */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001819 *((__be16 *)&cmd->rcb.cdb[2]) = cpu_to_be16(ctx_hndl_u);
1820 *((__be32 *)&cmd->rcb.cdb[4]) = cpu_to_be32(res_hndl_u);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001821
Matthew R. Ochs48b4be32016-11-28 18:43:09 -06001822 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001823 if (unlikely(rc))
1824 goto out;
1825
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -06001826 rc = wait_resp(afu, cmd);
1827 if (unlikely(rc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001828 rc = -1;
1829out:
Matthew R. Ochsde012832016-11-28 18:42:33 -06001830 atomic_dec(&afu->cmds_active);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001831 mutex_unlock(&sync_active);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001832 kfree(buf);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001833 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001834 return rc;
1835}
1836
1837/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001838 * afu_reset() - resets the AFU
1839 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001840 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001841 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001842 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001843static int afu_reset(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001844{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001845 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001846 int rc = 0;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001847
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001848 /* Stop the context before the reset. Since the context is
1849 * no longer available restart it after the reset is complete
1850 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001851 term_afu(cfg);
1852
1853 rc = init_afu(cfg);
1854
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001855 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001856 return rc;
1857}
1858
1859/**
Manoj N. Kumarf4113962016-06-15 18:49:20 -05001860 * drain_ioctls() - wait until all currently executing ioctls have completed
1861 * @cfg: Internal structure associated with the host.
1862 *
1863 * Obtain write access to read/write semaphore that wraps ioctl
1864 * handling to 'drain' ioctls currently executing.
1865 */
1866static void drain_ioctls(struct cxlflash_cfg *cfg)
1867{
1868 down_write(&cfg->ioctl_rwsem);
1869 up_write(&cfg->ioctl_rwsem);
1870}
1871
1872/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001873 * cxlflash_eh_device_reset_handler() - reset a single LUN
1874 * @scp: SCSI command to send.
1875 *
1876 * Return:
1877 * SUCCESS as defined in scsi/scsi.h
1878 * FAILED as defined in scsi/scsi.h
1879 */
1880static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
1881{
1882 int rc = SUCCESS;
1883 struct Scsi_Host *host = scp->device->host;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001884 struct cxlflash_cfg *cfg = shost_priv(host);
1885 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001886 struct afu *afu = cfg->afu;
1887 int rcr = 0;
1888
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001889 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
1890 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
1891 scp->device->channel, scp->device->id, scp->device->lun,
1892 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1893 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1894 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1895 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochs15305512015-10-21 15:12:10 -05001896
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001897retry:
Matthew R. Ochs15305512015-10-21 15:12:10 -05001898 switch (cfg->state) {
1899 case STATE_NORMAL:
1900 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
1901 if (unlikely(rcr))
1902 rc = FAILED;
1903 break;
1904 case STATE_RESET:
1905 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001906 goto retry;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001907 default:
1908 rc = FAILED;
1909 break;
1910 }
1911
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001912 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -05001913 return rc;
1914}
1915
1916/**
1917 * cxlflash_eh_host_reset_handler() - reset the host adapter
1918 * @scp: SCSI command from stack identifying host.
1919 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05001920 * Following a reset, the state is evaluated again in case an EEH occurred
1921 * during the reset. In such a scenario, the host reset will either yield
1922 * until the EEH recovery is complete or return success or failure based
1923 * upon the current device state.
1924 *
Matthew R. Ochs15305512015-10-21 15:12:10 -05001925 * Return:
1926 * SUCCESS as defined in scsi/scsi.h
1927 * FAILED as defined in scsi/scsi.h
1928 */
1929static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
1930{
1931 int rc = SUCCESS;
1932 int rcr = 0;
1933 struct Scsi_Host *host = scp->device->host;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001934 struct cxlflash_cfg *cfg = shost_priv(host);
1935 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001936
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001937 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
1938 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
1939 scp->device->channel, scp->device->id, scp->device->lun,
1940 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1941 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1942 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1943 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochs15305512015-10-21 15:12:10 -05001944
1945 switch (cfg->state) {
1946 case STATE_NORMAL:
1947 cfg->state = STATE_RESET;
Manoj N. Kumarf4113962016-06-15 18:49:20 -05001948 drain_ioctls(cfg);
Matthew R. Ochs15305512015-10-21 15:12:10 -05001949 cxlflash_mark_contexts_error(cfg);
1950 rcr = afu_reset(cfg);
1951 if (rcr) {
1952 rc = FAILED;
1953 cfg->state = STATE_FAILTERM;
1954 } else
1955 cfg->state = STATE_NORMAL;
1956 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05001957 ssleep(1);
1958 /* fall through */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001959 case STATE_RESET:
1960 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
1961 if (cfg->state == STATE_NORMAL)
1962 break;
1963 /* fall through */
1964 default:
1965 rc = FAILED;
1966 break;
1967 }
1968
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001969 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -05001970 return rc;
1971}
1972
1973/**
1974 * cxlflash_change_queue_depth() - change the queue depth for the device
1975 * @sdev: SCSI device destined for queue depth change.
1976 * @qdepth: Requested queue depth value to set.
1977 *
1978 * The requested queue depth is capped to the maximum supported value.
1979 *
1980 * Return: The actual queue depth set.
1981 */
1982static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
1983{
1984
1985 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
1986 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
1987
1988 scsi_change_queue_depth(sdev, qdepth);
1989 return sdev->queue_depth;
1990}
1991
1992/**
1993 * cxlflash_show_port_status() - queries and presents the current port status
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05001994 * @port: Desired port for status reporting.
1995 * @afu: AFU owning the specified port.
Matthew R. Ochs15305512015-10-21 15:12:10 -05001996 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
1997 *
1998 * Return: The size of the ASCII string returned in @buf.
1999 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002000static ssize_t cxlflash_show_port_status(u32 port, struct afu *afu, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002001{
Matthew R. Ochs15305512015-10-21 15:12:10 -05002002 char *disp_status;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002003 u64 status;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002004 __be64 __iomem *fc_regs;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002005
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002006 if (port >= NUM_FC_PORTS)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002007 return 0;
2008
2009 fc_regs = &afu->afu_map->global.fc_regs[port][0];
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002010 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
2011 status &= FC_MTIP_STATUS_MASK;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002012
2013 if (status == FC_MTIP_STATUS_ONLINE)
2014 disp_status = "online";
2015 else if (status == FC_MTIP_STATUS_OFFLINE)
2016 disp_status = "offline";
2017 else
2018 disp_status = "unknown";
2019
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002020 return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002021}
2022
2023/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002024 * port0_show() - queries and presents the current status of port 0
2025 * @dev: Generic device associated with the host owning the port.
2026 * @attr: Device attribute representing the port.
2027 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002028 *
2029 * Return: The size of the ASCII string returned in @buf.
2030 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002031static ssize_t port0_show(struct device *dev,
2032 struct device_attribute *attr,
2033 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002034{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002035 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochs15305512015-10-21 15:12:10 -05002036 struct afu *afu = cfg->afu;
2037
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002038 return cxlflash_show_port_status(0, afu, buf);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002039}
2040
2041/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002042 * port1_show() - queries and presents the current status of port 1
2043 * @dev: Generic device associated with the host owning the port.
2044 * @attr: Device attribute representing the port.
2045 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2046 *
2047 * Return: The size of the ASCII string returned in @buf.
2048 */
2049static ssize_t port1_show(struct device *dev,
2050 struct device_attribute *attr,
2051 char *buf)
2052{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002053 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002054 struct afu *afu = cfg->afu;
2055
2056 return cxlflash_show_port_status(1, afu, buf);
2057}
2058
2059/**
2060 * lun_mode_show() - presents the current LUN mode of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002061 * @dev: Generic device associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002062 * @attr: Device attribute representing the LUN mode.
2063 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2064 *
2065 * Return: The size of the ASCII string returned in @buf.
2066 */
2067static ssize_t lun_mode_show(struct device *dev,
2068 struct device_attribute *attr, char *buf)
2069{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002070 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002071 struct afu *afu = cfg->afu;
2072
2073 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
2074}
2075
2076/**
2077 * lun_mode_store() - sets the LUN mode of the host
2078 * @dev: Generic device associated with the host.
2079 * @attr: Device attribute representing the LUN mode.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002080 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2081 * @count: Length of data resizing in @buf.
2082 *
2083 * The CXL Flash AFU supports a dummy LUN mode where the external
2084 * links and storage are not required. Space on the FPGA is used
2085 * to create 1 or 2 small LUNs which are presented to the system
2086 * as if they were a normal storage device. This feature is useful
2087 * during development and also provides manufacturing with a way
2088 * to test the AFU without an actual device.
2089 *
2090 * 0 = external LUN[s] (default)
2091 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2092 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2093 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2094 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2095 *
2096 * Return: The size of the ASCII string returned in @buf.
2097 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002098static ssize_t lun_mode_store(struct device *dev,
2099 struct device_attribute *attr,
2100 const char *buf, size_t count)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002101{
2102 struct Scsi_Host *shost = class_to_shost(dev);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002103 struct cxlflash_cfg *cfg = shost_priv(shost);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002104 struct afu *afu = cfg->afu;
2105 int rc;
2106 u32 lun_mode;
2107
2108 rc = kstrtouint(buf, 10, &lun_mode);
2109 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
2110 afu->internal_lun = lun_mode;
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002111
2112 /*
2113 * When configured for internal LUN, there is only one channel,
2114 * channel number 0, else there will be 2 (default).
2115 */
2116 if (afu->internal_lun)
2117 shost->max_channel = 0;
2118 else
2119 shost->max_channel = NUM_FC_PORTS - 1;
2120
Matthew R. Ochs15305512015-10-21 15:12:10 -05002121 afu_reset(cfg);
2122 scsi_scan_host(cfg->host);
2123 }
2124
2125 return count;
2126}
2127
2128/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002129 * ioctl_version_show() - presents the current ioctl version of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002130 * @dev: Generic device associated with the host.
2131 * @attr: Device attribute representing the ioctl version.
2132 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2133 *
2134 * Return: The size of the ASCII string returned in @buf.
2135 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002136static ssize_t ioctl_version_show(struct device *dev,
2137 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002138{
2139 return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
2140}
2141
2142/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002143 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2144 * @port: Desired port for status reporting.
2145 * @afu: AFU owning the specified port.
2146 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2147 *
2148 * Return: The size of the ASCII string returned in @buf.
2149 */
2150static ssize_t cxlflash_show_port_lun_table(u32 port,
2151 struct afu *afu,
2152 char *buf)
2153{
2154 int i;
2155 ssize_t bytes = 0;
2156 __be64 __iomem *fc_port;
2157
2158 if (port >= NUM_FC_PORTS)
2159 return 0;
2160
2161 fc_port = &afu->afu_map->global.fc_port[port][0];
2162
2163 for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2164 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002165 "%03d: %016llx\n", i, readq_be(&fc_port[i]));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002166 return bytes;
2167}
2168
2169/**
2170 * port0_lun_table_show() - presents the current LUN table of port 0
2171 * @dev: Generic device associated with the host owning the port.
2172 * @attr: Device attribute representing the port.
2173 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2174 *
2175 * Return: The size of the ASCII string returned in @buf.
2176 */
2177static ssize_t port0_lun_table_show(struct device *dev,
2178 struct device_attribute *attr,
2179 char *buf)
2180{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002181 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002182 struct afu *afu = cfg->afu;
2183
2184 return cxlflash_show_port_lun_table(0, afu, buf);
2185}
2186
2187/**
2188 * port1_lun_table_show() - presents the current LUN table of port 1
2189 * @dev: Generic device associated with the host owning the port.
2190 * @attr: Device attribute representing the port.
2191 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2192 *
2193 * Return: The size of the ASCII string returned in @buf.
2194 */
2195static ssize_t port1_lun_table_show(struct device *dev,
2196 struct device_attribute *attr,
2197 char *buf)
2198{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002199 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002200 struct afu *afu = cfg->afu;
2201
2202 return cxlflash_show_port_lun_table(1, afu, buf);
2203}
2204
2205/**
2206 * mode_show() - presents the current mode of the device
Matthew R. Ochs15305512015-10-21 15:12:10 -05002207 * @dev: Generic device associated with the device.
2208 * @attr: Device attribute representing the device mode.
2209 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2210 *
2211 * Return: The size of the ASCII string returned in @buf.
2212 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002213static ssize_t mode_show(struct device *dev,
2214 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002215{
2216 struct scsi_device *sdev = to_scsi_device(dev);
2217
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002218 return scnprintf(buf, PAGE_SIZE, "%s\n",
2219 sdev->hostdata ? "superpipe" : "legacy");
Matthew R. Ochs15305512015-10-21 15:12:10 -05002220}
2221
2222/*
2223 * Host attributes
2224 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002225static DEVICE_ATTR_RO(port0);
2226static DEVICE_ATTR_RO(port1);
2227static DEVICE_ATTR_RW(lun_mode);
2228static DEVICE_ATTR_RO(ioctl_version);
2229static DEVICE_ATTR_RO(port0_lun_table);
2230static DEVICE_ATTR_RO(port1_lun_table);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002231
2232static struct device_attribute *cxlflash_host_attrs[] = {
2233 &dev_attr_port0,
2234 &dev_attr_port1,
2235 &dev_attr_lun_mode,
2236 &dev_attr_ioctl_version,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002237 &dev_attr_port0_lun_table,
2238 &dev_attr_port1_lun_table,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002239 NULL
2240};
2241
2242/*
2243 * Device attributes
2244 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002245static DEVICE_ATTR_RO(mode);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002246
2247static struct device_attribute *cxlflash_dev_attrs[] = {
2248 &dev_attr_mode,
2249 NULL
2250};
2251
2252/*
2253 * Host template
2254 */
2255static struct scsi_host_template driver_template = {
2256 .module = THIS_MODULE,
2257 .name = CXLFLASH_ADAPTER_NAME,
2258 .info = cxlflash_driver_info,
2259 .ioctl = cxlflash_ioctl,
2260 .proc_name = CXLFLASH_NAME,
2261 .queuecommand = cxlflash_queuecommand,
2262 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
2263 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
2264 .change_queue_depth = cxlflash_change_queue_depth,
Manoj N. Kumar83430832016-03-04 15:55:20 -06002265 .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002266 .can_queue = CXLFLASH_MAX_CMDS,
Matthew R. Ochs5fbb96c82016-11-28 18:42:19 -06002267 .cmd_size = sizeof(struct afu_cmd) + __alignof__(struct afu_cmd) - 1,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002268 .this_id = -1,
Uma Krishnan68ab2d72016-11-28 18:41:06 -06002269 .sg_tablesize = 1, /* No scatter gather support */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002270 .max_sectors = CXLFLASH_MAX_SECTORS,
2271 .use_clustering = ENABLE_CLUSTERING,
2272 .shost_attrs = cxlflash_host_attrs,
2273 .sdev_attrs = cxlflash_dev_attrs,
2274};
2275
2276/*
2277 * Device dependent values
2278 */
Uma Krishnan96e1b662016-06-15 18:49:38 -05002279static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
2280 0ULL };
2281static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
Uma Krishnan704c4b02016-06-15 18:49:57 -05002282 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs15305512015-10-21 15:12:10 -05002283
2284/*
2285 * PCI device binding table
2286 */
2287static struct pci_device_id cxlflash_pci_table[] = {
2288 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
2289 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
Manoj Kumara2746fb2015-12-14 15:07:43 -06002290 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
2291 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
Matthew R. Ochs15305512015-10-21 15:12:10 -05002292 {}
2293};
2294
2295MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
2296
2297/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002298 * cxlflash_worker_thread() - work thread handler for the AFU
2299 * @work: Work structure contained within cxlflash associated with host.
2300 *
2301 * Handles the following events:
2302 * - Link reset which cannot be performed on interrupt context due to
2303 * blocking up to a few seconds
Matthew R. Ochsef510742015-10-21 15:13:37 -05002304 * - Rescan the host
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002305 */
2306static void cxlflash_worker_thread(struct work_struct *work)
2307{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002308 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
2309 work_q);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002310 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002311 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002312 int port;
2313 ulong lock_flags;
2314
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002315 /* Avoid MMIO if the device has failed */
2316
2317 if (cfg->state != STATE_NORMAL)
2318 return;
2319
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002320 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2321
2322 if (cfg->lr_state == LINK_RESET_REQUIRED) {
2323 port = cfg->lr_port;
2324 if (port < 0)
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002325 dev_err(dev, "%s: invalid port index %d\n",
2326 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002327 else {
2328 spin_unlock_irqrestore(cfg->host->host_lock,
2329 lock_flags);
2330
2331 /* The reset can block... */
2332 afu_link_reset(afu, port,
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05002333 &afu->afu_map->global.fc_regs[port][0]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002334 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2335 }
2336
2337 cfg->lr_state = LINK_RESET_COMPLETE;
2338 }
2339
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002340 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
Matthew R. Ochsef510742015-10-21 15:13:37 -05002341
2342 if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
2343 scsi_scan_host(cfg->host);
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06002344 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002345}
2346
2347/**
2348 * cxlflash_probe() - PCI entry point to add host
2349 * @pdev: PCI device associated with the host.
2350 * @dev_id: PCI device id associated with device.
2351 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002352 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002353 */
2354static int cxlflash_probe(struct pci_dev *pdev,
2355 const struct pci_device_id *dev_id)
2356{
2357 struct Scsi_Host *host;
2358 struct cxlflash_cfg *cfg = NULL;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002359 struct device *dev = &pdev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002360 struct dev_dependent_vals *ddv;
2361 int rc = 0;
2362
2363 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
2364 __func__, pdev->irq);
2365
2366 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
2367 driver_template.max_sectors = ddv->max_sectors;
2368
2369 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
2370 if (!host) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002371 dev_err(dev, "%s: scsi_host_alloc failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002372 rc = -ENOMEM;
2373 goto out;
2374 }
2375
2376 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
2377 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
2378 host->max_channel = NUM_FC_PORTS - 1;
2379 host->unique_id = host->host_no;
2380 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
2381
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002382 cfg = shost_priv(host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002383 cfg->host = host;
2384 rc = alloc_mem(cfg);
2385 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002386 dev_err(dev, "%s: alloc_mem failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002387 rc = -ENOMEM;
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -05002388 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002389 goto out;
2390 }
2391
2392 cfg->init_state = INIT_STATE_NONE;
2393 cfg->dev = pdev;
Matthew R. Ochs17ead262015-10-21 15:15:37 -05002394 cfg->cxl_fops = cxlflash_cxl_fops;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002395
2396 /*
2397 * The promoted LUNs move to the top of the LUN table. The rest stay
2398 * on the bottom half. The bottom half grows from the end
2399 * (index = 255), whereas the top half grows from the beginning
2400 * (index = 0).
2401 */
2402 cfg->promote_lun_index = 0;
2403 cfg->last_lun_index[0] = CXLFLASH_NUM_VLUNS/2 - 1;
2404 cfg->last_lun_index[1] = CXLFLASH_NUM_VLUNS/2 - 1;
2405
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002406 cfg->dev_id = (struct pci_device_id *)dev_id;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002407
2408 init_waitqueue_head(&cfg->tmf_waitq);
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002409 init_waitqueue_head(&cfg->reset_waitq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002410
2411 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
2412 cfg->lr_state = LINK_RESET_INVALID;
2413 cfg->lr_port = -1;
Matthew R. Ochs0d731222015-10-21 15:16:24 -05002414 spin_lock_init(&cfg->tmf_slock);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002415 mutex_init(&cfg->ctx_tbl_list_mutex);
2416 mutex_init(&cfg->ctx_recovery_mutex);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002417 init_rwsem(&cfg->ioctl_rwsem);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002418 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
2419 INIT_LIST_HEAD(&cfg->lluns);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002420
2421 pci_set_drvdata(pdev, cfg);
2422
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002423 cfg->cxl_afu = cxl_pci_to_afu(pdev);
2424
2425 rc = init_pci(cfg);
2426 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002427 dev_err(dev, "%s: init_pci failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002428 goto out_remove;
2429 }
2430 cfg->init_state = INIT_STATE_PCI;
2431
2432 rc = init_afu(cfg);
2433 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002434 dev_err(dev, "%s: init_afu failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002435 goto out_remove;
2436 }
2437 cfg->init_state = INIT_STATE_AFU;
2438
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002439 rc = init_scsi(cfg);
2440 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002441 dev_err(dev, "%s: init_scsi failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002442 goto out_remove;
2443 }
2444 cfg->init_state = INIT_STATE_SCSI;
2445
2446out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002447 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002448 return rc;
2449
2450out_remove:
2451 cxlflash_remove(pdev);
2452 goto out;
2453}
2454
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002455/**
2456 * cxlflash_pci_error_detected() - called when a PCI error is detected
2457 * @pdev: PCI device struct.
2458 * @state: PCI channel state.
2459 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002460 * When an EEH occurs during an active reset, wait until the reset is
2461 * complete and then take action based upon the device state.
2462 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002463 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2464 */
2465static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
2466 pci_channel_state_t state)
2467{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002468 int rc = 0;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002469 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2470 struct device *dev = &cfg->dev->dev;
2471
2472 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
2473
2474 switch (state) {
2475 case pci_channel_io_frozen:
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002476 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2477 if (cfg->state == STATE_FAILTERM)
2478 return PCI_ERS_RESULT_DISCONNECT;
2479
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002480 cfg->state = STATE_RESET;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002481 scsi_block_requests(cfg->host);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002482 drain_ioctls(cfg);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002483 rc = cxlflash_mark_contexts_error(cfg);
2484 if (unlikely(rc))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002485 dev_err(dev, "%s: Failed to mark user contexts rc=%d\n",
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002486 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05002487 term_afu(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002488 return PCI_ERS_RESULT_NEED_RESET;
2489 case pci_channel_io_perm_failure:
2490 cfg->state = STATE_FAILTERM;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002491 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002492 scsi_unblock_requests(cfg->host);
2493 return PCI_ERS_RESULT_DISCONNECT;
2494 default:
2495 break;
2496 }
2497 return PCI_ERS_RESULT_NEED_RESET;
2498}
2499
2500/**
2501 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2502 * @pdev: PCI device struct.
2503 *
2504 * This routine is called by the pci error recovery code after the PCI
2505 * slot has been reset, just before we should resume normal operations.
2506 *
2507 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2508 */
2509static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
2510{
2511 int rc = 0;
2512 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2513 struct device *dev = &cfg->dev->dev;
2514
2515 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2516
2517 rc = init_afu(cfg);
2518 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002519 dev_err(dev, "%s: EEH recovery failed rc=%d\n", __func__, rc);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002520 return PCI_ERS_RESULT_DISCONNECT;
2521 }
2522
2523 return PCI_ERS_RESULT_RECOVERED;
2524}
2525
2526/**
2527 * cxlflash_pci_resume() - called when normal operation can resume
2528 * @pdev: PCI device struct
2529 */
2530static void cxlflash_pci_resume(struct pci_dev *pdev)
2531{
2532 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2533 struct device *dev = &cfg->dev->dev;
2534
2535 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2536
2537 cfg->state = STATE_NORMAL;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002538 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002539 scsi_unblock_requests(cfg->host);
2540}
2541
2542static const struct pci_error_handlers cxlflash_err_handler = {
2543 .error_detected = cxlflash_pci_error_detected,
2544 .slot_reset = cxlflash_pci_slot_reset,
2545 .resume = cxlflash_pci_resume,
2546};
2547
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002548/*
2549 * PCI device structure
2550 */
2551static struct pci_driver cxlflash_driver = {
2552 .name = CXLFLASH_NAME,
2553 .id_table = cxlflash_pci_table,
2554 .probe = cxlflash_probe,
2555 .remove = cxlflash_remove,
Uma Krishnanbabf9852016-09-02 15:39:16 -05002556 .shutdown = cxlflash_remove,
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002557 .err_handler = &cxlflash_err_handler,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002558};
2559
2560/**
2561 * init_cxlflash() - module entry point
2562 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002563 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002564 */
2565static int __init init_cxlflash(void)
2566{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002567 cxlflash_list_init();
2568
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002569 return pci_register_driver(&cxlflash_driver);
2570}
2571
2572/**
2573 * exit_cxlflash() - module exit point
2574 */
2575static void __exit exit_cxlflash(void)
2576{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002577 cxlflash_term_global_luns();
2578 cxlflash_free_errpage();
2579
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002580 pci_unregister_driver(&cxlflash_driver);
2581}
2582
2583module_init(init_cxlflash);
2584module_exit(exit_cxlflash);