blob: 3061d8045382e437b445d025b2c2910fb15c7a96 [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. Ochs5fbb96c2016-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
422/**
423 * cxlflash_driver_info() - information handler for this host driver
424 * @host: SCSI host associated with device.
425 *
426 * Return: A string describing the device.
427 */
428static const char *cxlflash_driver_info(struct Scsi_Host *host)
429{
430 return CXLFLASH_ADAPTER_NAME;
431}
432
433/**
434 * cxlflash_queuecommand() - sends a mid-layer request
435 * @host: SCSI host associated with device.
436 * @scp: SCSI command to send.
437 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500438 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500439 */
440static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
441{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600442 struct cxlflash_cfg *cfg = shost_priv(host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500443 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500444 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600445 struct afu_cmd *cmd = sc_to_afucz(scp);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600446 struct scatterlist *sg = scsi_sglist(scp);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500447 u32 port_sel = scp->device->channel + 1;
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600448 u16 req_flags = SISL_REQ_FLAGS_SUP_UNDERRUN;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500449 ulong lock_flags;
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600450 int nseg = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500451 int rc = 0;
452
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500453 dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600454 "cdb=(%08x-%08x-%08x-%08x)\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500455 __func__, scp, host->host_no, scp->device->channel,
456 scp->device->id, scp->device->lun,
457 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
458 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
459 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
460 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500461
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500462 /*
463 * If a Task Management Function is active, wait for it to complete
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500464 * before continuing with regular commands.
465 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500466 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500467 if (cfg->tmf_active) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500468 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500469 rc = SCSI_MLQUEUE_HOST_BUSY;
470 goto out;
471 }
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500472 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500473
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500474 switch (cfg->state) {
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500475 case STATE_RESET:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600476 dev_dbg_ratelimited(dev, "%s: device is in reset\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500477 rc = SCSI_MLQUEUE_HOST_BUSY;
478 goto out;
479 case STATE_FAILTERM:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600480 dev_dbg_ratelimited(dev, "%s: device has failed\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500481 scp->result = (DID_NO_CONNECT << 16);
482 scp->scsi_done(scp);
483 rc = 0;
484 goto out;
485 default:
486 break;
487 }
488
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600489 if (likely(sg)) {
490 nseg = scsi_dma_map(scp);
491 if (unlikely(nseg < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600492 dev_err(dev, "%s: Fail DMA map\n", __func__);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600493 rc = SCSI_MLQUEUE_HOST_BUSY;
494 goto out;
495 }
496
497 cmd->rcb.data_len = sg_dma_len(sg);
498 cmd->rcb.data_ea = sg_dma_address(sg);
499 }
500
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600501 cmd->scp = scp;
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600502 cmd->parent = afu;
503
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500504 cmd->rcb.ctx_id = afu->ctx_hndl;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600505 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500506 cmd->rcb.port_sel = port_sel;
507 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
508
509 if (scp->sc_data_direction == DMA_TO_DEVICE)
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600510 req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500511
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600512 cmd->rcb.req_flags = req_flags;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500513 memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
514
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600515 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600516 if (unlikely(rc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500517 scsi_dma_unmap(scp);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500518out:
519 return rc;
520}
521
522/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500523 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500524 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500525 */
526static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
527{
528 struct pci_dev *pdev = cfg->dev;
529
530 if (pci_channel_offline(pdev))
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500531 wait_event_timeout(cfg->reset_waitq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500532 !pci_channel_offline(pdev),
533 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
534}
535
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500536/**
537 * free_mem() - free memory associated with the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500538 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500539 */
540static void free_mem(struct cxlflash_cfg *cfg)
541{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500542 struct afu *afu = cfg->afu;
543
544 if (cfg->afu) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500545 free_pages((ulong)afu, get_order(sizeof(struct afu)));
546 cfg->afu = NULL;
547 }
548}
549
550/**
551 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500552 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500553 *
554 * Safe to call with AFU in a partially allocated/initialized state.
Manoj Kumaree91e332015-12-14 15:07:02 -0600555 *
Uma Krishnan0df5bef2017-01-11 19:20:03 -0600556 * Cancels scheduled worker threads, waits for any active internal AFU
557 * commands to timeout and then unmaps the MMIO space.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500558 */
559static void stop_afu(struct cxlflash_cfg *cfg)
560{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500561 struct afu *afu = cfg->afu;
562
Uma Krishnan0df5bef2017-01-11 19:20:03 -0600563 cancel_work_sync(&cfg->work_q);
564
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500565 if (likely(afu)) {
Matthew R. Ochsde012832016-11-28 18:42:33 -0600566 while (atomic_read(&afu->cmds_active))
567 ssleep(1);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500568 if (likely(afu->afu_map)) {
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500569 cxl_psa_unmap((void __iomem *)afu->afu_map);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500570 afu->afu_map = NULL;
571 }
572 }
573}
574
575/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500576 * term_intr() - disables all AFU interrupts
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500577 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500578 * @level: Depth of allocation, where to begin waterfall tear down.
579 *
580 * Safe to call with AFU/MC in partially allocated/initialized state.
581 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500582static void term_intr(struct cxlflash_cfg *cfg, enum undo_level level)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500583{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500584 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500585 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500586
587 if (!afu || !cfg->mcctx) {
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500588 dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500589 return;
590 }
591
592 switch (level) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500593 case UNMAP_THREE:
594 cxl_unmap_afu_irq(cfg->mcctx, 3, afu);
595 case UNMAP_TWO:
596 cxl_unmap_afu_irq(cfg->mcctx, 2, afu);
597 case UNMAP_ONE:
598 cxl_unmap_afu_irq(cfg->mcctx, 1, afu);
599 case FREE_IRQ:
600 cxl_free_afu_irqs(cfg->mcctx);
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500601 /* fall through */
602 case UNDO_NOOP:
603 /* No action required */
604 break;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500605 }
606}
607
608/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500609 * term_mc() - terminates the master context
610 * @cfg: Internal structure associated with the host.
611 * @level: Depth of allocation, where to begin waterfall tear down.
612 *
613 * Safe to call with AFU/MC in partially allocated/initialized state.
614 */
615static void term_mc(struct cxlflash_cfg *cfg)
616{
617 int rc = 0;
618 struct afu *afu = cfg->afu;
619 struct device *dev = &cfg->dev->dev;
620
621 if (!afu || !cfg->mcctx) {
622 dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
623 return;
624 }
625
626 rc = cxl_stop_context(cfg->mcctx);
627 WARN_ON(rc);
628 cfg->mcctx = NULL;
629}
630
631/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500632 * term_afu() - terminates the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500633 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500634 *
635 * Safe to call with AFU/MC in partially allocated/initialized state.
636 */
637static void term_afu(struct cxlflash_cfg *cfg)
638{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600639 struct device *dev = &cfg->dev->dev;
640
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500641 /*
642 * Tear down is carefully orchestrated to ensure
643 * no interrupts can come in when the problem state
644 * area is unmapped.
645 *
646 * 1) Disable all AFU interrupts
647 * 2) Unmap the problem state area
648 * 3) Stop the master context
649 */
650 term_intr(cfg, UNMAP_THREE);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500651 if (cfg->afu)
652 stop_afu(cfg);
653
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500654 term_mc(cfg);
Uma Krishnan6ded8b32016-03-04 15:55:15 -0600655
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600656 dev_dbg(dev, "%s: returning\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500657}
658
659/**
Uma Krishnan704c4b02016-06-15 18:49:57 -0500660 * notify_shutdown() - notifies device of pending shutdown
661 * @cfg: Internal structure associated with the host.
662 * @wait: Whether to wait for shutdown processing to complete.
663 *
664 * This function will notify the AFU that the adapter is being shutdown
665 * and will wait for shutdown processing to complete if wait is true.
666 * This notification should flush pending I/Os to the device and halt
667 * further I/Os until the next AFU reset is issued and device restarted.
668 */
669static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
670{
671 struct afu *afu = cfg->afu;
672 struct device *dev = &cfg->dev->dev;
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500673 struct sisl_global_map __iomem *global;
Uma Krishnan704c4b02016-06-15 18:49:57 -0500674 struct dev_dependent_vals *ddv;
675 u64 reg, status;
676 int i, retry_cnt = 0;
677
678 ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
679 if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
680 return;
681
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500682 if (!afu || !afu->afu_map) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600683 dev_dbg(dev, "%s: Problem state area not mapped\n", __func__);
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500684 return;
685 }
686
687 global = &afu->afu_map->global;
688
Uma Krishnan704c4b02016-06-15 18:49:57 -0500689 /* Notify AFU */
690 for (i = 0; i < NUM_FC_PORTS; i++) {
691 reg = readq_be(&global->fc_regs[i][FC_CONFIG2 / 8]);
692 reg |= SISL_FC_SHUTDOWN_NORMAL;
693 writeq_be(reg, &global->fc_regs[i][FC_CONFIG2 / 8]);
694 }
695
696 if (!wait)
697 return;
698
699 /* Wait up to 1.5 seconds for shutdown processing to complete */
700 for (i = 0; i < NUM_FC_PORTS; i++) {
701 retry_cnt = 0;
702 while (true) {
703 status = readq_be(&global->fc_regs[i][FC_STATUS / 8]);
704 if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
705 break;
706 if (++retry_cnt >= MC_RETRY_CNT) {
707 dev_dbg(dev, "%s: port %d shutdown processing "
708 "not yet completed\n", __func__, i);
709 break;
710 }
711 msleep(100 * retry_cnt);
712 }
713 }
714}
715
716/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500717 * cxlflash_remove() - PCI entry point to tear down host
718 * @pdev: PCI device associated with the host.
719 *
720 * Safe to use as a cleanup in partially allocated/initialized state.
721 */
722static void cxlflash_remove(struct pci_dev *pdev)
723{
724 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600725 struct device *dev = &pdev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500726 ulong lock_flags;
727
Uma Krishnanbabf9852016-09-02 15:39:16 -0500728 if (!pci_is_enabled(pdev)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600729 dev_dbg(dev, "%s: Device is disabled\n", __func__);
Uma Krishnanbabf9852016-09-02 15:39:16 -0500730 return;
731 }
732
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500733 /* If a Task Management Function is active, wait for it to complete
734 * before continuing with remove.
735 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500736 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500737 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500738 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
739 !cfg->tmf_active,
740 cfg->tmf_slock);
741 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500742
Uma Krishnan704c4b02016-06-15 18:49:57 -0500743 /* Notify AFU and wait for shutdown processing to complete */
744 notify_shutdown(cfg, true);
745
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500746 cfg->state = STATE_FAILTERM;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500747 cxlflash_stop_term_user_contexts(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500748
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500749 switch (cfg->init_state) {
750 case INIT_STATE_SCSI:
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500751 cxlflash_term_local_luns(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500752 scsi_remove_host(cfg->host);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -0500753 /* fall through */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500754 case INIT_STATE_AFU:
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600755 term_afu(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500756 case INIT_STATE_PCI:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500757 pci_disable_device(pdev);
758 case INIT_STATE_NONE:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500759 free_mem(cfg);
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -0500760 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500761 break;
762 }
763
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600764 dev_dbg(dev, "%s: returning\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500765}
766
767/**
768 * alloc_mem() - allocates the AFU and its command pool
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500769 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500770 *
771 * A partially allocated state remains on failure.
772 *
773 * Return:
774 * 0 on success
775 * -ENOMEM on failure to allocate memory
776 */
777static int alloc_mem(struct cxlflash_cfg *cfg)
778{
779 int rc = 0;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500780 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500781
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600782 /* AFU is ~28k, i.e. only one 64k page or up to seven 4k pages */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500783 cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
784 get_order(sizeof(struct afu)));
785 if (unlikely(!cfg->afu)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500786 dev_err(dev, "%s: cannot get %d free pages\n",
787 __func__, get_order(sizeof(struct afu)));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500788 rc = -ENOMEM;
789 goto out;
790 }
791 cfg->afu->parent = cfg;
792 cfg->afu->afu_map = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500793out:
794 return rc;
795}
796
797/**
798 * init_pci() - initializes the host as a PCI device
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500799 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500800 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500801 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500802 */
803static int init_pci(struct cxlflash_cfg *cfg)
804{
805 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600806 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500807 int rc = 0;
808
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500809 rc = pci_enable_device(pdev);
810 if (rc || pci_channel_offline(pdev)) {
811 if (pci_channel_offline(pdev)) {
812 cxlflash_wait_for_pci_err_recovery(cfg);
813 rc = pci_enable_device(pdev);
814 }
815
816 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600817 dev_err(dev, "%s: Cannot enable adapter\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500818 cxlflash_wait_for_pci_err_recovery(cfg);
Manoj N. Kumar961487e2016-03-04 15:55:14 -0600819 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500820 }
821 }
822
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500823out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600824 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500825 return rc;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500826}
827
828/**
829 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500830 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500831 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500832 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500833 */
834static int init_scsi(struct cxlflash_cfg *cfg)
835{
836 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600837 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500838 int rc = 0;
839
840 rc = scsi_add_host(cfg->host, &pdev->dev);
841 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600842 dev_err(dev, "%s: scsi_add_host failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500843 goto out;
844 }
845
846 scsi_scan_host(cfg->host);
847
848out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600849 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500850 return rc;
851}
852
853/**
854 * set_port_online() - transitions the specified host FC port to online state
855 * @fc_regs: Top of MMIO region defined for specified port.
856 *
857 * The provided MMIO region must be mapped prior to call. Online state means
858 * that the FC link layer has synced, completed the handshaking process, and
859 * is ready for login to start.
860 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500861static void set_port_online(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500862{
863 u64 cmdcfg;
864
865 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
866 cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
867 cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
868 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
869}
870
871/**
872 * set_port_offline() - transitions the specified host FC port to offline state
873 * @fc_regs: Top of MMIO region defined for specified port.
874 *
875 * The provided MMIO region must be mapped prior to call.
876 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500877static void set_port_offline(__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_ONLINE); /* clear ON_LINE */
883 cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
884 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
885}
886
887/**
888 * wait_port_online() - waits for the specified host FC port come online
889 * @fc_regs: Top of MMIO region defined for specified port.
890 * @delay_us: Number of microseconds to delay between reading port status.
891 * @nretry: Number of cycles to retry reading port status.
892 *
893 * The provided MMIO region must be mapped prior to call. This will timeout
894 * when the cable is not plugged in.
895 *
896 * Return:
897 * TRUE (1) when the specified port is online
898 * FALSE (0) when the specified port fails to come online after timeout
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500899 */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600900static bool wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500901{
902 u64 status;
903
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600904 WARN_ON(delay_us < 1000);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500905
906 do {
907 msleep(delay_us / 1000);
908 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -0500909 if (status == U64_MAX)
910 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500911 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
912 nretry--);
913
914 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
915}
916
917/**
918 * wait_port_offline() - waits for the specified host FC port go offline
919 * @fc_regs: Top of MMIO region defined for specified port.
920 * @delay_us: Number of microseconds to delay between reading port status.
921 * @nretry: Number of cycles to retry reading port status.
922 *
923 * The provided MMIO region must be mapped prior to call.
924 *
925 * Return:
926 * TRUE (1) when the specified port is offline
927 * FALSE (0) when the specified port fails to go offline after timeout
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500928 */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600929static bool wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500930{
931 u64 status;
932
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600933 WARN_ON(delay_us < 1000);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500934
935 do {
936 msleep(delay_us / 1000);
937 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -0500938 if (status == U64_MAX)
939 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500940 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
941 nretry--);
942
943 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
944}
945
946/**
947 * afu_set_wwpn() - configures the WWPN for the specified host FC port
948 * @afu: AFU associated with the host that owns the specified FC port.
949 * @port: Port number being configured.
950 * @fc_regs: Top of MMIO region defined for specified port.
951 * @wwpn: The world-wide-port-number previously discovered for port.
952 *
953 * The provided MMIO region must be mapped prior to call. As part of the
954 * sequence to configure the WWPN, the port is toggled offline and then back
955 * online. This toggling action can cause this routine to delay up to a few
956 * seconds. When configured to use the internal LUN feature of the AFU, a
957 * failure to come online is overridden.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500958 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -0500959static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
960 u64 wwpn)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500961{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600962 struct cxlflash_cfg *cfg = afu->parent;
963 struct device *dev = &cfg->dev->dev;
964
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500965 set_port_offline(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500966 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
967 FC_PORT_STATUS_RETRY_CNT)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600968 dev_dbg(dev, "%s: wait on port %d to go offline timed out\n",
969 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500970 }
971
Matthew R. Ochsf8013262016-09-02 15:40:20 -0500972 writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
Matthew R. Ochs964497b2015-10-21 15:13:54 -0500973
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500974 set_port_online(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500975 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
976 FC_PORT_STATUS_RETRY_CNT)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600977 dev_dbg(dev, "%s: wait on port %d to go online timed out\n",
978 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500979 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500980}
981
982/**
983 * afu_link_reset() - resets the specified host FC port
984 * @afu: AFU associated with the host that owns the specified FC port.
985 * @port: Port number being configured.
986 * @fc_regs: Top of MMIO region defined for specified port.
987 *
988 * The provided MMIO region must be mapped prior to call. The sequence to
989 * reset the port involves toggling it offline and then back online. This
990 * action can cause this routine to delay up to a few seconds. An effort
991 * is made to maintain link with the device by switching to host to use
992 * the alternate port exclusively while the reset takes place.
993 * failure to come online is overridden.
994 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500995static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500996{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600997 struct cxlflash_cfg *cfg = afu->parent;
998 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500999 u64 port_sel;
1000
1001 /* first switch the AFU to the other links, if any */
1002 port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
Dan Carpenter4da74db2015-08-18 11:57:43 +03001003 port_sel &= ~(1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001004 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1005 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1006
1007 set_port_offline(fc_regs);
1008 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1009 FC_PORT_STATUS_RETRY_CNT))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001010 dev_err(dev, "%s: wait on port %d to go offline timed out\n",
1011 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001012
1013 set_port_online(fc_regs);
1014 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1015 FC_PORT_STATUS_RETRY_CNT))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001016 dev_err(dev, "%s: wait on port %d to go online timed out\n",
1017 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001018
1019 /* switch back to include this port */
Dan Carpenter4da74db2015-08-18 11:57:43 +03001020 port_sel |= (1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001021 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1022 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1023
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001024 dev_dbg(dev, "%s: returning port_sel=%016llx\n", __func__, port_sel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001025}
1026
1027/*
1028 * Asynchronous interrupt information table
1029 */
1030static const struct asyc_intr_info ainfo[] = {
1031 {SISL_ASTATUS_FC0_OTHER, "other error", 0, CLR_FC_ERROR | LINK_RESET},
1032 {SISL_ASTATUS_FC0_LOGO, "target initiated LOGO", 0, 0},
1033 {SISL_ASTATUS_FC0_CRC_T, "CRC threshold exceeded", 0, LINK_RESET},
Manoj Kumare6e6df32015-10-21 15:16:07 -05001034 {SISL_ASTATUS_FC0_LOGI_R, "login timed out, retrying", 0, LINK_RESET},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001035 {SISL_ASTATUS_FC0_LOGI_F, "login failed", 0, CLR_FC_ERROR},
Matthew R. Ochsef510742015-10-21 15:13:37 -05001036 {SISL_ASTATUS_FC0_LOGI_S, "login succeeded", 0, SCAN_HOST},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001037 {SISL_ASTATUS_FC0_LINK_DN, "link down", 0, 0},
Uma Krishnanbbbfae92016-09-02 15:38:48 -05001038 {SISL_ASTATUS_FC0_LINK_UP, "link up", 0, 0},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001039 {SISL_ASTATUS_FC1_OTHER, "other error", 1, CLR_FC_ERROR | LINK_RESET},
1040 {SISL_ASTATUS_FC1_LOGO, "target initiated LOGO", 1, 0},
1041 {SISL_ASTATUS_FC1_CRC_T, "CRC threshold exceeded", 1, LINK_RESET},
Manoj Kumara9be2942015-12-14 14:55:09 -06001042 {SISL_ASTATUS_FC1_LOGI_R, "login timed out, retrying", 1, LINK_RESET},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001043 {SISL_ASTATUS_FC1_LOGI_F, "login failed", 1, CLR_FC_ERROR},
Matthew R. Ochsef510742015-10-21 15:13:37 -05001044 {SISL_ASTATUS_FC1_LOGI_S, "login succeeded", 1, SCAN_HOST},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001045 {SISL_ASTATUS_FC1_LINK_DN, "link down", 1, 0},
Uma Krishnanbbbfae92016-09-02 15:38:48 -05001046 {SISL_ASTATUS_FC1_LINK_UP, "link up", 1, 0},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001047 {0x0, "", 0, 0} /* terminator */
1048};
1049
1050/**
1051 * find_ainfo() - locates and returns asynchronous interrupt information
1052 * @status: Status code set by AFU on error.
1053 *
1054 * Return: The located information or NULL when the status code is invalid.
1055 */
1056static const struct asyc_intr_info *find_ainfo(u64 status)
1057{
1058 const struct asyc_intr_info *info;
1059
1060 for (info = &ainfo[0]; info->status; info++)
1061 if (info->status == status)
1062 return info;
1063
1064 return NULL;
1065}
1066
1067/**
1068 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1069 * @afu: AFU associated with the host.
1070 */
1071static void afu_err_intr_init(struct afu *afu)
1072{
1073 int i;
1074 u64 reg;
1075
1076 /* global async interrupts: AFU clears afu_ctrl on context exit
1077 * if async interrupts were sent to that context. This prevents
1078 * the AFU form sending further async interrupts when
1079 * there is
1080 * nobody to receive them.
1081 */
1082
1083 /* mask all */
1084 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
1085 /* set LISN# to send and point to master context */
1086 reg = ((u64) (((afu->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
1087
1088 if (afu->internal_lun)
1089 reg |= 1; /* Bit 63 indicates local lun */
1090 writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1091 /* clear all */
1092 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1093 /* unmask bits that are of interest */
1094 /* note: afu can send an interrupt after this step */
1095 writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1096 /* clear again in case a bit came on after previous clear but before */
1097 /* unmask */
1098 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1099
1100 /* Clear/Set internal lun bits */
1101 reg = readq_be(&afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1102 reg &= SISL_FC_INTERNAL_MASK;
1103 if (afu->internal_lun)
1104 reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
1105 writeq_be(reg, &afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1106
1107 /* now clear FC errors */
1108 for (i = 0; i < NUM_FC_PORTS; i++) {
1109 writeq_be(0xFFFFFFFFU,
1110 &afu->afu_map->global.fc_regs[i][FC_ERROR / 8]);
1111 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRCAP / 8]);
1112 }
1113
1114 /* sync interrupts for master's IOARRIN write */
1115 /* note that unlike asyncs, there can be no pending sync interrupts */
1116 /* at this time (this is a fresh context and master has not written */
1117 /* IOARRIN yet), so there is nothing to clear. */
1118
1119 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1120 writeq_be(SISL_MSI_SYNC_ERROR, &afu->host_map->ctx_ctrl);
1121 writeq_be(SISL_ISTATUS_MASK, &afu->host_map->intr_mask);
1122}
1123
1124/**
1125 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1126 * @irq: Interrupt number.
1127 * @data: Private data provided at interrupt registration, the AFU.
1128 *
1129 * Return: Always return IRQ_HANDLED.
1130 */
1131static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1132{
1133 struct afu *afu = (struct afu *)data;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001134 struct cxlflash_cfg *cfg = afu->parent;
1135 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001136 u64 reg;
1137 u64 reg_unmasked;
1138
1139 reg = readq_be(&afu->host_map->intr_status);
1140 reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1141
1142 if (reg_unmasked == 0UL) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001143 dev_err(dev, "%s: spurious interrupt, intr_status=%016llx\n",
1144 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001145 goto cxlflash_sync_err_irq_exit;
1146 }
1147
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001148 dev_err(dev, "%s: unexpected interrupt, intr_status=%016llx\n",
1149 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001150
1151 writeq_be(reg_unmasked, &afu->host_map->intr_clear);
1152
1153cxlflash_sync_err_irq_exit:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001154 return IRQ_HANDLED;
1155}
1156
1157/**
1158 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1159 * @irq: Interrupt number.
1160 * @data: Private data provided at interrupt registration, the AFU.
1161 *
1162 * Return: Always return IRQ_HANDLED.
1163 */
1164static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
1165{
1166 struct afu *afu = (struct afu *)data;
1167 struct afu_cmd *cmd;
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001168 struct sisl_ioasa *ioasa;
1169 struct sisl_ioarcb *ioarcb;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001170 bool toggle = afu->toggle;
1171 u64 entry,
1172 *hrrq_start = afu->hrrq_start,
1173 *hrrq_end = afu->hrrq_end,
1174 *hrrq_curr = afu->hrrq_curr;
1175
1176 /* Process however many RRQ entries that are ready */
1177 while (true) {
1178 entry = *hrrq_curr;
1179
1180 if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1181 break;
1182
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001183 entry &= ~SISL_RESP_HANDLE_T_BIT;
1184
1185 if (afu_is_sq_cmd_mode(afu)) {
1186 ioasa = (struct sisl_ioasa *)entry;
1187 cmd = container_of(ioasa, struct afu_cmd, sa);
1188 } else {
1189 ioarcb = (struct sisl_ioarcb *)entry;
1190 cmd = container_of(ioarcb, struct afu_cmd, rcb);
1191 }
1192
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001193 cmd_complete(cmd);
1194
1195 /* Advance to next entry or wrap and flip the toggle bit */
1196 if (hrrq_curr < hrrq_end)
1197 hrrq_curr++;
1198 else {
1199 hrrq_curr = hrrq_start;
1200 toggle ^= SISL_RESP_HANDLE_T_BIT;
1201 }
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001202
1203 atomic_inc(&afu->hsq_credits);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001204 }
1205
1206 afu->hrrq_curr = hrrq_curr;
1207 afu->toggle = toggle;
1208
1209 return IRQ_HANDLED;
1210}
1211
1212/**
1213 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1214 * @irq: Interrupt number.
1215 * @data: Private data provided at interrupt registration, the AFU.
1216 *
1217 * Return: Always return IRQ_HANDLED.
1218 */
1219static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1220{
1221 struct afu *afu = (struct afu *)data;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001222 struct cxlflash_cfg *cfg = afu->parent;
1223 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001224 u64 reg_unmasked;
1225 const struct asyc_intr_info *info;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001226 struct sisl_global_map __iomem *global = &afu->afu_map->global;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001227 u64 reg;
1228 u8 port;
1229 int i;
1230
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001231 reg = readq_be(&global->regs.aintr_status);
1232 reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1233
1234 if (reg_unmasked == 0) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001235 dev_err(dev, "%s: spurious interrupt, aintr_status=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001236 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001237 goto out;
1238 }
1239
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001240 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001241 writeq_be(reg_unmasked, &global->regs.aintr_clear);
1242
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001243 /* Check each bit that is on */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001244 for (i = 0; reg_unmasked; i++, reg_unmasked = (reg_unmasked >> 1)) {
1245 info = find_ainfo(1ULL << i);
Matthew R. Ochs16798d32015-10-21 15:13:45 -05001246 if (((reg_unmasked & 0x1) == 0) || !info)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001247 continue;
1248
1249 port = info->port;
1250
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001251 dev_err(dev, "%s: FC Port %d -> %s, fc_status=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001252 __func__, port, info->desc,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001253 readq_be(&global->fc_regs[port][FC_STATUS / 8]));
1254
1255 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001256 * Do link reset first, some OTHER errors will set FC_ERROR
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001257 * again if cleared before or w/o a reset
1258 */
1259 if (info->action & LINK_RESET) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001260 dev_err(dev, "%s: FC Port %d: resetting link\n",
1261 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001262 cfg->lr_state = LINK_RESET_REQUIRED;
1263 cfg->lr_port = port;
1264 schedule_work(&cfg->work_q);
1265 }
1266
1267 if (info->action & CLR_FC_ERROR) {
1268 reg = readq_be(&global->fc_regs[port][FC_ERROR / 8]);
1269
1270 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001271 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001272 * should be the same and tracing one is sufficient.
1273 */
1274
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001275 dev_err(dev, "%s: fc %d: clearing fc_error=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001276 __func__, port, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001277
1278 writeq_be(reg, &global->fc_regs[port][FC_ERROR / 8]);
1279 writeq_be(0, &global->fc_regs[port][FC_ERRCAP / 8]);
1280 }
Matthew R. Ochsef510742015-10-21 15:13:37 -05001281
1282 if (info->action & SCAN_HOST) {
1283 atomic_inc(&cfg->scan_host_needed);
1284 schedule_work(&cfg->work_q);
1285 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001286 }
1287
1288out:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001289 return IRQ_HANDLED;
1290}
1291
1292/**
1293 * start_context() - starts the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001294 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001295 *
1296 * Return: A success or failure value from CXL services.
1297 */
1298static int start_context(struct cxlflash_cfg *cfg)
1299{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001300 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001301 int rc = 0;
1302
1303 rc = cxl_start_context(cfg->mcctx,
1304 cfg->afu->work.work_element_descriptor,
1305 NULL);
1306
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001307 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001308 return rc;
1309}
1310
1311/**
1312 * read_vpd() - obtains the WWPNs from VPD
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001313 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001314 * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
1315 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001316 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001317 */
1318static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1319{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001320 struct device *dev = &cfg->dev->dev;
1321 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001322 int rc = 0;
1323 int ro_start, ro_size, i, j, k;
1324 ssize_t vpd_size;
1325 char vpd_data[CXLFLASH_VPD_LEN];
1326 char tmp_buf[WWPN_BUF_LEN] = { 0 };
1327 char *wwpn_vpd_tags[NUM_FC_PORTS] = { "V5", "V6" };
1328
1329 /* Get the VPD data from the device */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001330 vpd_size = cxl_read_adapter_vpd(pdev, vpd_data, sizeof(vpd_data));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001331 if (unlikely(vpd_size <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001332 dev_err(dev, "%s: Unable to read VPD (size = %ld)\n",
1333 __func__, vpd_size);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001334 rc = -ENODEV;
1335 goto out;
1336 }
1337
1338 /* Get the read only section offset */
1339 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
1340 PCI_VPD_LRDT_RO_DATA);
1341 if (unlikely(ro_start < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001342 dev_err(dev, "%s: VPD Read-only data not found\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001343 rc = -ENODEV;
1344 goto out;
1345 }
1346
1347 /* Get the read only section size, cap when extends beyond read VPD */
1348 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1349 j = ro_size;
1350 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1351 if (unlikely((i + j) > vpd_size)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001352 dev_dbg(dev, "%s: Might need to read more VPD (%d > %ld)\n",
1353 __func__, (i + j), vpd_size);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001354 ro_size = vpd_size - i;
1355 }
1356
1357 /*
1358 * Find the offset of the WWPN tag within the read only
1359 * VPD data and validate the found field (partials are
1360 * no good to us). Convert the ASCII data to an integer
1361 * value. Note that we must copy to a temporary buffer
1362 * because the conversion service requires that the ASCII
1363 * string be terminated.
1364 */
1365 for (k = 0; k < NUM_FC_PORTS; k++) {
1366 j = ro_size;
1367 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1368
1369 i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
1370 if (unlikely(i < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001371 dev_err(dev, "%s: Port %d WWPN not found in VPD\n",
1372 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001373 rc = -ENODEV;
1374 goto out;
1375 }
1376
1377 j = pci_vpd_info_field_size(&vpd_data[i]);
1378 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1379 if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001380 dev_err(dev, "%s: Port %d WWPN incomplete or bad VPD\n",
1381 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001382 rc = -ENODEV;
1383 goto out;
1384 }
1385
1386 memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1387 rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1388 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001389 dev_err(dev, "%s: WWPN conversion failed for port %d\n",
1390 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001391 rc = -ENODEV;
1392 goto out;
1393 }
1394 }
1395
1396out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001397 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001398 return rc;
1399}
1400
1401/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001402 * init_pcr() - initialize the provisioning and control registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001403 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001404 *
1405 * Also sets up fast access to the mapped registers and initializes AFU
1406 * command fields that never change.
1407 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001408static void init_pcr(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001409{
1410 struct afu *afu = cfg->afu;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001411 struct sisl_ctrl_map __iomem *ctrl_map;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001412 int i;
1413
1414 for (i = 0; i < MAX_CONTEXT; i++) {
1415 ctrl_map = &afu->afu_map->ctrls[i].ctrl;
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001416 /* Disrupt any clients that could be running */
1417 /* e.g. clients that survived a master restart */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001418 writeq_be(0, &ctrl_map->rht_start);
1419 writeq_be(0, &ctrl_map->rht_cnt_id);
1420 writeq_be(0, &ctrl_map->ctx_cap);
1421 }
1422
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001423 /* Copy frequently used fields into afu */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001424 afu->ctx_hndl = (u16) cxl_process_element(cfg->mcctx);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001425 afu->host_map = &afu->afu_map->hosts[afu->ctx_hndl].host;
1426 afu->ctrl_map = &afu->afu_map->ctrls[afu->ctx_hndl].ctrl;
1427
1428 /* Program the Endian Control for the master context */
1429 writeq_be(SISL_ENDIAN_CTRL, &afu->host_map->endian_ctrl);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001430}
1431
1432/**
1433 * init_global() - initialize AFU global registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001434 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001435 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001436static int init_global(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001437{
1438 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001439 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001440 u64 wwpn[NUM_FC_PORTS]; /* wwpn of AFU ports */
1441 int i = 0, num_ports = 0;
1442 int rc = 0;
1443 u64 reg;
1444
1445 rc = read_vpd(cfg, &wwpn[0]);
1446 if (rc) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001447 dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001448 goto out;
1449 }
1450
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001451 dev_dbg(dev, "%s: wwpn0=%016llx wwpn1=%016llx\n",
1452 __func__, wwpn[0], wwpn[1]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001453
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001454 /* Set up RRQ and SQ in AFU for master issued cmds */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001455 writeq_be((u64) afu->hrrq_start, &afu->host_map->rrq_start);
1456 writeq_be((u64) afu->hrrq_end, &afu->host_map->rrq_end);
1457
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001458 if (afu_is_sq_cmd_mode(afu)) {
1459 writeq_be((u64)afu->hsq_start, &afu->host_map->sq_start);
1460 writeq_be((u64)afu->hsq_end, &afu->host_map->sq_end);
1461 }
1462
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001463 /* AFU configuration */
1464 reg = readq_be(&afu->afu_map->global.regs.afu_config);
1465 reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1466 /* enable all auto retry options and control endianness */
1467 /* leave others at default: */
1468 /* CTX_CAP write protected, mbox_r does not clear on read and */
1469 /* checker on if dual afu */
1470 writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1471
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001472 /* Global port select: select either port */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001473 if (afu->internal_lun) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001474 /* Only use port 0 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001475 writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
1476 num_ports = NUM_FC_PORTS - 1;
1477 } else {
1478 writeq_be(BOTH_PORTS, &afu->afu_map->global.regs.afu_port_sel);
1479 num_ports = NUM_FC_PORTS;
1480 }
1481
1482 for (i = 0; i < num_ports; i++) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001483 /* Unmask all errors (but they are still masked at AFU) */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001484 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRMSK / 8]);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001485 /* Clear CRC error cnt & set a threshold */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001486 (void)readq_be(&afu->afu_map->global.
1487 fc_regs[i][FC_CNT_CRCERR / 8]);
1488 writeq_be(MC_CRC_THRESH, &afu->afu_map->global.fc_regs[i]
1489 [FC_CRC_THRESH / 8]);
1490
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001491 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001492 if (wwpn[i] != 0)
1493 afu_set_wwpn(afu, i,
1494 &afu->afu_map->global.fc_regs[i][0],
1495 wwpn[i]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001496 /* Programming WWPN back to back causes additional
1497 * offline/online transitions and a PLOGI
1498 */
1499 msleep(100);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001500 }
1501
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001502 /* Set up master's own CTX_CAP to allow real mode, host translation */
1503 /* tables, afu cmds and read/write GSCSI cmds. */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001504 /* First, unlock ctx_cap write by reading mbox */
1505 (void)readq_be(&afu->ctrl_map->mbox_r); /* unlock ctx_cap */
1506 writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1507 SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1508 SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1509 &afu->ctrl_map->ctx_cap);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001510 /* Initialize heartbeat */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001511 afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001512out:
1513 return rc;
1514}
1515
1516/**
1517 * start_afu() - initializes and starts the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001518 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001519 */
1520static int start_afu(struct cxlflash_cfg *cfg)
1521{
1522 struct afu *afu = cfg->afu;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001523 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001524 int rc = 0;
1525
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001526 init_pcr(cfg);
1527
Matthew R. Ochsaf104832015-10-21 15:15:14 -05001528 /* After an AFU reset, RRQ entries are stale, clear them */
1529 memset(&afu->rrq_entry, 0, sizeof(afu->rrq_entry));
1530
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001531 /* Initialize RRQ pointers */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001532 afu->hrrq_start = &afu->rrq_entry[0];
1533 afu->hrrq_end = &afu->rrq_entry[NUM_RRQ_ENTRY - 1];
1534 afu->hrrq_curr = afu->hrrq_start;
1535 afu->toggle = 1;
1536
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001537 /* Initialize SQ */
1538 if (afu_is_sq_cmd_mode(afu)) {
1539 memset(&afu->sq, 0, sizeof(afu->sq));
1540 afu->hsq_start = &afu->sq[0];
1541 afu->hsq_end = &afu->sq[NUM_SQ_ENTRY - 1];
1542 afu->hsq_curr = afu->hsq_start;
1543
1544 spin_lock_init(&afu->hsq_slock);
1545 atomic_set(&afu->hsq_credits, NUM_SQ_ENTRY - 1);
1546 }
1547
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001548 rc = init_global(cfg);
1549
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001550 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001551 return rc;
1552}
1553
1554/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001555 * init_intr() - setup interrupt handlers for the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001556 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001557 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001558 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001559 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001560static enum undo_level init_intr(struct cxlflash_cfg *cfg,
1561 struct cxl_context *ctx)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001562{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001563 struct afu *afu = cfg->afu;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001564 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001565 int rc = 0;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001566 enum undo_level level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001567
1568 rc = cxl_allocate_afu_irqs(ctx, 3);
1569 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001570 dev_err(dev, "%s: allocate_afu_irqs failed rc=%d\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001571 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001572 level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001573 goto out;
1574 }
1575
1576 rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, afu,
1577 "SISL_MSI_SYNC_ERROR");
1578 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001579 dev_err(dev, "%s: SISL_MSI_SYNC_ERROR map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001580 level = FREE_IRQ;
1581 goto out;
1582 }
1583
1584 rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, afu,
1585 "SISL_MSI_RRQ_UPDATED");
1586 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001587 dev_err(dev, "%s: SISL_MSI_RRQ_UPDATED map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001588 level = UNMAP_ONE;
1589 goto out;
1590 }
1591
1592 rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, afu,
1593 "SISL_MSI_ASYNC_ERROR");
1594 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001595 dev_err(dev, "%s: SISL_MSI_ASYNC_ERROR map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001596 level = UNMAP_TWO;
1597 goto out;
1598 }
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001599out:
1600 return level;
1601}
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001602
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001603/**
1604 * init_mc() - create and register as the master context
1605 * @cfg: Internal structure associated with the host.
1606 *
1607 * Return: 0 on success, -errno on failure
1608 */
1609static int init_mc(struct cxlflash_cfg *cfg)
1610{
1611 struct cxl_context *ctx;
1612 struct device *dev = &cfg->dev->dev;
1613 int rc = 0;
1614 enum undo_level level;
1615
1616 ctx = cxl_get_context(cfg->dev);
1617 if (unlikely(!ctx)) {
1618 rc = -ENOMEM;
1619 goto ret;
1620 }
1621 cfg->mcctx = ctx;
1622
1623 /* Set it up as a master with the CXL */
1624 cxl_set_master(ctx);
1625
1626 /* During initialization reset the AFU to start from a clean slate */
1627 rc = cxl_afu_reset(cfg->mcctx);
1628 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001629 dev_err(dev, "%s: AFU reset failed rc=%d\n", __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001630 goto ret;
1631 }
1632
1633 level = init_intr(cfg, ctx);
1634 if (unlikely(level)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001635 dev_err(dev, "%s: interrupt init failed rc=%d\n", __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001636 goto out;
1637 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001638
1639 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1640 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1641 * element (pe) that is embedded in the context (ctx)
1642 */
1643 rc = start_context(cfg);
1644 if (unlikely(rc)) {
1645 dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
1646 level = UNMAP_THREE;
1647 goto out;
1648 }
1649ret:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001650 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001651 return rc;
1652out:
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001653 term_intr(cfg, level);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001654 goto ret;
1655}
1656
1657/**
1658 * init_afu() - setup as master context and start AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001659 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001660 *
1661 * This routine is a higher level of control for configuring the
1662 * AFU on probe and reset paths.
1663 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001664 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001665 */
1666static int init_afu(struct cxlflash_cfg *cfg)
1667{
1668 u64 reg;
1669 int rc = 0;
1670 struct afu *afu = cfg->afu;
1671 struct device *dev = &cfg->dev->dev;
1672
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001673 cxl_perst_reloads_same_image(cfg->cxl_afu, true);
1674
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001675 rc = init_mc(cfg);
1676 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001677 dev_err(dev, "%s: init_mc failed rc=%d\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001678 __func__, rc);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001679 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001680 }
1681
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001682 /* Map the entire MMIO space of the AFU */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001683 afu->afu_map = cxl_psa_map(cfg->mcctx);
1684 if (!afu->afu_map) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001685 dev_err(dev, "%s: cxl_psa_map failed\n", __func__);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001686 rc = -ENOMEM;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001687 goto err1;
1688 }
1689
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001690 /* No byte reverse on reading afu_version or string will be backwards */
1691 reg = readq(&afu->afu_map->global.regs.afu_version);
1692 memcpy(afu->version, &reg, sizeof(reg));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001693 afu->interface_version =
1694 readq_be(&afu->afu_map->global.regs.interface_version);
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001695 if ((afu->interface_version + 1) == 0) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001696 dev_err(dev, "Back level AFU, please upgrade. AFU version %s "
1697 "interface version %016llx\n", afu->version,
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001698 afu->interface_version);
1699 rc = -EINVAL;
Uma Krishnan0df5bef2017-01-11 19:20:03 -06001700 goto err1;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001701 }
1702
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001703 if (afu_is_sq_cmd_mode(afu)) {
1704 afu->send_cmd = send_cmd_sq;
1705 afu->context_reset = context_reset_sq;
1706 } else {
1707 afu->send_cmd = send_cmd_ioarrin;
1708 afu->context_reset = context_reset_ioarrin;
1709 }
Matthew R. Ochs48b4be32016-11-28 18:43:09 -06001710
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001711 dev_dbg(dev, "%s: afu_ver=%s interface_ver=%016llx\n", __func__,
1712 afu->version, afu->interface_version);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001713
1714 rc = start_afu(cfg);
1715 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001716 dev_err(dev, "%s: start_afu failed, rc=%d\n", __func__, rc);
Uma Krishnan0df5bef2017-01-11 19:20:03 -06001717 goto err1;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001718 }
1719
1720 afu_err_intr_init(cfg->afu);
Uma Krishnan11f7b182016-11-28 18:41:45 -06001721 spin_lock_init(&afu->rrin_slock);
1722 afu->room = readq_be(&afu->host_map->cmd_room);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001723
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05001724 /* Restore the LUN mappings */
1725 cxlflash_restore_luntable(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001726out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001727 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001728 return rc;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001729
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001730err1:
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001731 term_intr(cfg, UNMAP_THREE);
1732 term_mc(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001733 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001734}
1735
1736/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001737 * cxlflash_afu_sync() - builds and sends an AFU sync command
1738 * @afu: AFU associated with the host.
1739 * @ctx_hndl_u: Identifies context requesting sync.
1740 * @res_hndl_u: Identifies resource requesting sync.
1741 * @mode: Type of sync to issue (lightweight, heavyweight, global).
1742 *
1743 * The AFU can only take 1 sync command at a time. This routine enforces this
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001744 * limitation by using a mutex to provide exclusive access to the AFU during
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001745 * the sync. This design point requires calling threads to not be on interrupt
1746 * context due to the possibility of sleeping during concurrent sync operations.
1747 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001748 * AFU sync operations are only necessary and allowed when the device is
1749 * operating normally. When not operating normally, sync requests can occur as
1750 * part of cleaning up resources associated with an adapter prior to removal.
1751 * In this scenario, these requests are simply ignored (safe due to the AFU
1752 * going away).
1753 *
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001754 * Return:
1755 * 0 on success
1756 * -1 on failure
1757 */
1758int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
1759 res_hndl_t res_hndl_u, u8 mode)
1760{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001761 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001762 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001763 struct afu_cmd *cmd = NULL;
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001764 char *buf = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001765 int rc = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001766 static DEFINE_MUTEX(sync_active);
1767
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001768 if (cfg->state != STATE_NORMAL) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001769 dev_dbg(dev, "%s: Sync not required state=%u\n",
1770 __func__, cfg->state);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001771 return 0;
1772 }
1773
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001774 mutex_lock(&sync_active);
Matthew R. Ochsde012832016-11-28 18:42:33 -06001775 atomic_inc(&afu->cmds_active);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001776 buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
1777 if (unlikely(!buf)) {
1778 dev_err(dev, "%s: no memory for command\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001779 rc = -1;
1780 goto out;
1781 }
1782
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001783 cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
1784 init_completion(&cmd->cevent);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001785 cmd->parent = afu;
1786
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001787 dev_dbg(dev, "%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001788
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001789 cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001790 cmd->rcb.ctx_id = afu->ctx_hndl;
1791 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001792 cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
1793
1794 cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
1795 cmd->rcb.cdb[1] = mode;
1796
1797 /* The cdb is aligned, no unaligned accessors required */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001798 *((__be16 *)&cmd->rcb.cdb[2]) = cpu_to_be16(ctx_hndl_u);
1799 *((__be32 *)&cmd->rcb.cdb[4]) = cpu_to_be32(res_hndl_u);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001800
Matthew R. Ochs48b4be32016-11-28 18:43:09 -06001801 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001802 if (unlikely(rc))
1803 goto out;
1804
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -06001805 rc = wait_resp(afu, cmd);
1806 if (unlikely(rc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001807 rc = -1;
1808out:
Matthew R. Ochsde012832016-11-28 18:42:33 -06001809 atomic_dec(&afu->cmds_active);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001810 mutex_unlock(&sync_active);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001811 kfree(buf);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001812 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001813 return rc;
1814}
1815
1816/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001817 * afu_reset() - resets the AFU
1818 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001819 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001820 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001821 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001822static int afu_reset(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001823{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001824 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001825 int rc = 0;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001826
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001827 /* Stop the context before the reset. Since the context is
1828 * no longer available restart it after the reset is complete
1829 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001830 term_afu(cfg);
1831
1832 rc = init_afu(cfg);
1833
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001834 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001835 return rc;
1836}
1837
1838/**
Manoj N. Kumarf4113962016-06-15 18:49:20 -05001839 * drain_ioctls() - wait until all currently executing ioctls have completed
1840 * @cfg: Internal structure associated with the host.
1841 *
1842 * Obtain write access to read/write semaphore that wraps ioctl
1843 * handling to 'drain' ioctls currently executing.
1844 */
1845static void drain_ioctls(struct cxlflash_cfg *cfg)
1846{
1847 down_write(&cfg->ioctl_rwsem);
1848 up_write(&cfg->ioctl_rwsem);
1849}
1850
1851/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001852 * cxlflash_eh_device_reset_handler() - reset a single LUN
1853 * @scp: SCSI command to send.
1854 *
1855 * Return:
1856 * SUCCESS as defined in scsi/scsi.h
1857 * FAILED as defined in scsi/scsi.h
1858 */
1859static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
1860{
1861 int rc = SUCCESS;
1862 struct Scsi_Host *host = scp->device->host;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001863 struct cxlflash_cfg *cfg = shost_priv(host);
1864 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001865 struct afu *afu = cfg->afu;
1866 int rcr = 0;
1867
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001868 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
1869 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
1870 scp->device->channel, scp->device->id, scp->device->lun,
1871 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1872 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1873 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1874 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochs15305512015-10-21 15:12:10 -05001875
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001876retry:
Matthew R. Ochs15305512015-10-21 15:12:10 -05001877 switch (cfg->state) {
1878 case STATE_NORMAL:
1879 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
1880 if (unlikely(rcr))
1881 rc = FAILED;
1882 break;
1883 case STATE_RESET:
1884 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001885 goto retry;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001886 default:
1887 rc = FAILED;
1888 break;
1889 }
1890
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001891 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -05001892 return rc;
1893}
1894
1895/**
1896 * cxlflash_eh_host_reset_handler() - reset the host adapter
1897 * @scp: SCSI command from stack identifying host.
1898 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05001899 * Following a reset, the state is evaluated again in case an EEH occurred
1900 * during the reset. In such a scenario, the host reset will either yield
1901 * until the EEH recovery is complete or return success or failure based
1902 * upon the current device state.
1903 *
Matthew R. Ochs15305512015-10-21 15:12:10 -05001904 * Return:
1905 * SUCCESS as defined in scsi/scsi.h
1906 * FAILED as defined in scsi/scsi.h
1907 */
1908static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
1909{
1910 int rc = SUCCESS;
1911 int rcr = 0;
1912 struct Scsi_Host *host = scp->device->host;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001913 struct cxlflash_cfg *cfg = shost_priv(host);
1914 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001915
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001916 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
1917 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
1918 scp->device->channel, scp->device->id, scp->device->lun,
1919 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1920 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1921 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1922 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochs15305512015-10-21 15:12:10 -05001923
1924 switch (cfg->state) {
1925 case STATE_NORMAL:
1926 cfg->state = STATE_RESET;
Manoj N. Kumarf4113962016-06-15 18:49:20 -05001927 drain_ioctls(cfg);
Matthew R. Ochs15305512015-10-21 15:12:10 -05001928 cxlflash_mark_contexts_error(cfg);
1929 rcr = afu_reset(cfg);
1930 if (rcr) {
1931 rc = FAILED;
1932 cfg->state = STATE_FAILTERM;
1933 } else
1934 cfg->state = STATE_NORMAL;
1935 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05001936 ssleep(1);
1937 /* fall through */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001938 case STATE_RESET:
1939 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
1940 if (cfg->state == STATE_NORMAL)
1941 break;
1942 /* fall through */
1943 default:
1944 rc = FAILED;
1945 break;
1946 }
1947
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001948 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -05001949 return rc;
1950}
1951
1952/**
1953 * cxlflash_change_queue_depth() - change the queue depth for the device
1954 * @sdev: SCSI device destined for queue depth change.
1955 * @qdepth: Requested queue depth value to set.
1956 *
1957 * The requested queue depth is capped to the maximum supported value.
1958 *
1959 * Return: The actual queue depth set.
1960 */
1961static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
1962{
1963
1964 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
1965 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
1966
1967 scsi_change_queue_depth(sdev, qdepth);
1968 return sdev->queue_depth;
1969}
1970
1971/**
1972 * cxlflash_show_port_status() - queries and presents the current port status
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05001973 * @port: Desired port for status reporting.
1974 * @afu: AFU owning the specified port.
Matthew R. Ochs15305512015-10-21 15:12:10 -05001975 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
1976 *
1977 * Return: The size of the ASCII string returned in @buf.
1978 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05001979static ssize_t cxlflash_show_port_status(u32 port, struct afu *afu, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05001980{
Matthew R. Ochs15305512015-10-21 15:12:10 -05001981 char *disp_status;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001982 u64 status;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05001983 __be64 __iomem *fc_regs;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001984
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05001985 if (port >= NUM_FC_PORTS)
Matthew R. Ochs15305512015-10-21 15:12:10 -05001986 return 0;
1987
1988 fc_regs = &afu->afu_map->global.fc_regs[port][0];
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05001989 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
1990 status &= FC_MTIP_STATUS_MASK;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001991
1992 if (status == FC_MTIP_STATUS_ONLINE)
1993 disp_status = "online";
1994 else if (status == FC_MTIP_STATUS_OFFLINE)
1995 disp_status = "offline";
1996 else
1997 disp_status = "unknown";
1998
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05001999 return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002000}
2001
2002/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002003 * port0_show() - queries and presents the current status of port 0
2004 * @dev: Generic device associated with the host owning the port.
2005 * @attr: Device attribute representing the port.
2006 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002007 *
2008 * Return: The size of the ASCII string returned in @buf.
2009 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002010static ssize_t port0_show(struct device *dev,
2011 struct device_attribute *attr,
2012 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002013{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002014 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochs15305512015-10-21 15:12:10 -05002015 struct afu *afu = cfg->afu;
2016
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002017 return cxlflash_show_port_status(0, afu, buf);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002018}
2019
2020/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002021 * port1_show() - queries and presents the current status of port 1
2022 * @dev: Generic device associated with the host owning the port.
2023 * @attr: Device attribute representing the port.
2024 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2025 *
2026 * Return: The size of the ASCII string returned in @buf.
2027 */
2028static ssize_t port1_show(struct device *dev,
2029 struct device_attribute *attr,
2030 char *buf)
2031{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002032 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002033 struct afu *afu = cfg->afu;
2034
2035 return cxlflash_show_port_status(1, afu, buf);
2036}
2037
2038/**
2039 * lun_mode_show() - presents the current LUN mode of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002040 * @dev: Generic device associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002041 * @attr: Device attribute representing the LUN mode.
2042 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2043 *
2044 * Return: The size of the ASCII string returned in @buf.
2045 */
2046static ssize_t lun_mode_show(struct device *dev,
2047 struct device_attribute *attr, char *buf)
2048{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002049 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002050 struct afu *afu = cfg->afu;
2051
2052 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
2053}
2054
2055/**
2056 * lun_mode_store() - sets the LUN mode of the host
2057 * @dev: Generic device associated with the host.
2058 * @attr: Device attribute representing the LUN mode.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002059 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2060 * @count: Length of data resizing in @buf.
2061 *
2062 * The CXL Flash AFU supports a dummy LUN mode where the external
2063 * links and storage are not required. Space on the FPGA is used
2064 * to create 1 or 2 small LUNs which are presented to the system
2065 * as if they were a normal storage device. This feature is useful
2066 * during development and also provides manufacturing with a way
2067 * to test the AFU without an actual device.
2068 *
2069 * 0 = external LUN[s] (default)
2070 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2071 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2072 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2073 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2074 *
2075 * Return: The size of the ASCII string returned in @buf.
2076 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002077static ssize_t lun_mode_store(struct device *dev,
2078 struct device_attribute *attr,
2079 const char *buf, size_t count)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002080{
2081 struct Scsi_Host *shost = class_to_shost(dev);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002082 struct cxlflash_cfg *cfg = shost_priv(shost);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002083 struct afu *afu = cfg->afu;
2084 int rc;
2085 u32 lun_mode;
2086
2087 rc = kstrtouint(buf, 10, &lun_mode);
2088 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
2089 afu->internal_lun = lun_mode;
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002090
2091 /*
2092 * When configured for internal LUN, there is only one channel,
2093 * channel number 0, else there will be 2 (default).
2094 */
2095 if (afu->internal_lun)
2096 shost->max_channel = 0;
2097 else
2098 shost->max_channel = NUM_FC_PORTS - 1;
2099
Matthew R. Ochs15305512015-10-21 15:12:10 -05002100 afu_reset(cfg);
2101 scsi_scan_host(cfg->host);
2102 }
2103
2104 return count;
2105}
2106
2107/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002108 * ioctl_version_show() - presents the current ioctl version of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002109 * @dev: Generic device associated with the host.
2110 * @attr: Device attribute representing the ioctl version.
2111 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2112 *
2113 * Return: The size of the ASCII string returned in @buf.
2114 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002115static ssize_t ioctl_version_show(struct device *dev,
2116 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002117{
2118 return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
2119}
2120
2121/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002122 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2123 * @port: Desired port for status reporting.
2124 * @afu: AFU owning the specified port.
2125 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2126 *
2127 * Return: The size of the ASCII string returned in @buf.
2128 */
2129static ssize_t cxlflash_show_port_lun_table(u32 port,
2130 struct afu *afu,
2131 char *buf)
2132{
2133 int i;
2134 ssize_t bytes = 0;
2135 __be64 __iomem *fc_port;
2136
2137 if (port >= NUM_FC_PORTS)
2138 return 0;
2139
2140 fc_port = &afu->afu_map->global.fc_port[port][0];
2141
2142 for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2143 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002144 "%03d: %016llx\n", i, readq_be(&fc_port[i]));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002145 return bytes;
2146}
2147
2148/**
2149 * port0_lun_table_show() - presents the current LUN table of port 0
2150 * @dev: Generic device associated with the host owning the port.
2151 * @attr: Device attribute representing the port.
2152 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2153 *
2154 * Return: The size of the ASCII string returned in @buf.
2155 */
2156static ssize_t port0_lun_table_show(struct device *dev,
2157 struct device_attribute *attr,
2158 char *buf)
2159{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002160 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002161 struct afu *afu = cfg->afu;
2162
2163 return cxlflash_show_port_lun_table(0, afu, buf);
2164}
2165
2166/**
2167 * port1_lun_table_show() - presents the current LUN table of port 1
2168 * @dev: Generic device associated with the host owning the port.
2169 * @attr: Device attribute representing the port.
2170 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2171 *
2172 * Return: The size of the ASCII string returned in @buf.
2173 */
2174static ssize_t port1_lun_table_show(struct device *dev,
2175 struct device_attribute *attr,
2176 char *buf)
2177{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002178 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002179 struct afu *afu = cfg->afu;
2180
2181 return cxlflash_show_port_lun_table(1, afu, buf);
2182}
2183
2184/**
2185 * mode_show() - presents the current mode of the device
Matthew R. Ochs15305512015-10-21 15:12:10 -05002186 * @dev: Generic device associated with the device.
2187 * @attr: Device attribute representing the device mode.
2188 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2189 *
2190 * Return: The size of the ASCII string returned in @buf.
2191 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002192static ssize_t mode_show(struct device *dev,
2193 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002194{
2195 struct scsi_device *sdev = to_scsi_device(dev);
2196
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002197 return scnprintf(buf, PAGE_SIZE, "%s\n",
2198 sdev->hostdata ? "superpipe" : "legacy");
Matthew R. Ochs15305512015-10-21 15:12:10 -05002199}
2200
2201/*
2202 * Host attributes
2203 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002204static DEVICE_ATTR_RO(port0);
2205static DEVICE_ATTR_RO(port1);
2206static DEVICE_ATTR_RW(lun_mode);
2207static DEVICE_ATTR_RO(ioctl_version);
2208static DEVICE_ATTR_RO(port0_lun_table);
2209static DEVICE_ATTR_RO(port1_lun_table);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002210
2211static struct device_attribute *cxlflash_host_attrs[] = {
2212 &dev_attr_port0,
2213 &dev_attr_port1,
2214 &dev_attr_lun_mode,
2215 &dev_attr_ioctl_version,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002216 &dev_attr_port0_lun_table,
2217 &dev_attr_port1_lun_table,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002218 NULL
2219};
2220
2221/*
2222 * Device attributes
2223 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002224static DEVICE_ATTR_RO(mode);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002225
2226static struct device_attribute *cxlflash_dev_attrs[] = {
2227 &dev_attr_mode,
2228 NULL
2229};
2230
2231/*
2232 * Host template
2233 */
2234static struct scsi_host_template driver_template = {
2235 .module = THIS_MODULE,
2236 .name = CXLFLASH_ADAPTER_NAME,
2237 .info = cxlflash_driver_info,
2238 .ioctl = cxlflash_ioctl,
2239 .proc_name = CXLFLASH_NAME,
2240 .queuecommand = cxlflash_queuecommand,
2241 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
2242 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
2243 .change_queue_depth = cxlflash_change_queue_depth,
Manoj N. Kumar83430832016-03-04 15:55:20 -06002244 .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002245 .can_queue = CXLFLASH_MAX_CMDS,
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -06002246 .cmd_size = sizeof(struct afu_cmd) + __alignof__(struct afu_cmd) - 1,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002247 .this_id = -1,
Uma Krishnan68ab2d72016-11-28 18:41:06 -06002248 .sg_tablesize = 1, /* No scatter gather support */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002249 .max_sectors = CXLFLASH_MAX_SECTORS,
2250 .use_clustering = ENABLE_CLUSTERING,
2251 .shost_attrs = cxlflash_host_attrs,
2252 .sdev_attrs = cxlflash_dev_attrs,
2253};
2254
2255/*
2256 * Device dependent values
2257 */
Uma Krishnan96e1b662016-06-15 18:49:38 -05002258static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
2259 0ULL };
2260static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
Uma Krishnan704c4b02016-06-15 18:49:57 -05002261 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs94344522017-02-16 21:39:32 -06002262static struct dev_dependent_vals dev_briard_vals = { CXLFLASH_MAX_SECTORS,
2263 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs15305512015-10-21 15:12:10 -05002264
2265/*
2266 * PCI device binding table
2267 */
2268static struct pci_device_id cxlflash_pci_table[] = {
2269 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
2270 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
Manoj Kumara2746fb2015-12-14 15:07:43 -06002271 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
2272 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
Matthew R. Ochs94344522017-02-16 21:39:32 -06002273 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_BRIARD,
2274 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_briard_vals},
Matthew R. Ochs15305512015-10-21 15:12:10 -05002275 {}
2276};
2277
2278MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
2279
2280/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002281 * cxlflash_worker_thread() - work thread handler for the AFU
2282 * @work: Work structure contained within cxlflash associated with host.
2283 *
2284 * Handles the following events:
2285 * - Link reset which cannot be performed on interrupt context due to
2286 * blocking up to a few seconds
Matthew R. Ochsef510742015-10-21 15:13:37 -05002287 * - Rescan the host
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002288 */
2289static void cxlflash_worker_thread(struct work_struct *work)
2290{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002291 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
2292 work_q);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002293 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002294 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002295 int port;
2296 ulong lock_flags;
2297
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002298 /* Avoid MMIO if the device has failed */
2299
2300 if (cfg->state != STATE_NORMAL)
2301 return;
2302
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002303 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2304
2305 if (cfg->lr_state == LINK_RESET_REQUIRED) {
2306 port = cfg->lr_port;
2307 if (port < 0)
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002308 dev_err(dev, "%s: invalid port index %d\n",
2309 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002310 else {
2311 spin_unlock_irqrestore(cfg->host->host_lock,
2312 lock_flags);
2313
2314 /* The reset can block... */
2315 afu_link_reset(afu, port,
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05002316 &afu->afu_map->global.fc_regs[port][0]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002317 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2318 }
2319
2320 cfg->lr_state = LINK_RESET_COMPLETE;
2321 }
2322
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002323 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
Matthew R. Ochsef510742015-10-21 15:13:37 -05002324
2325 if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
2326 scsi_scan_host(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002327}
2328
2329/**
2330 * cxlflash_probe() - PCI entry point to add host
2331 * @pdev: PCI device associated with the host.
2332 * @dev_id: PCI device id associated with device.
2333 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002334 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002335 */
2336static int cxlflash_probe(struct pci_dev *pdev,
2337 const struct pci_device_id *dev_id)
2338{
2339 struct Scsi_Host *host;
2340 struct cxlflash_cfg *cfg = NULL;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002341 struct device *dev = &pdev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002342 struct dev_dependent_vals *ddv;
2343 int rc = 0;
2344
2345 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
2346 __func__, pdev->irq);
2347
2348 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
2349 driver_template.max_sectors = ddv->max_sectors;
2350
2351 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
2352 if (!host) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002353 dev_err(dev, "%s: scsi_host_alloc failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002354 rc = -ENOMEM;
2355 goto out;
2356 }
2357
2358 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
2359 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
2360 host->max_channel = NUM_FC_PORTS - 1;
2361 host->unique_id = host->host_no;
2362 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
2363
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002364 cfg = shost_priv(host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002365 cfg->host = host;
2366 rc = alloc_mem(cfg);
2367 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002368 dev_err(dev, "%s: alloc_mem failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002369 rc = -ENOMEM;
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -05002370 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002371 goto out;
2372 }
2373
2374 cfg->init_state = INIT_STATE_NONE;
2375 cfg->dev = pdev;
Matthew R. Ochs17ead262015-10-21 15:15:37 -05002376 cfg->cxl_fops = cxlflash_cxl_fops;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002377
2378 /*
2379 * The promoted LUNs move to the top of the LUN table. The rest stay
2380 * on the bottom half. The bottom half grows from the end
2381 * (index = 255), whereas the top half grows from the beginning
2382 * (index = 0).
2383 */
2384 cfg->promote_lun_index = 0;
2385 cfg->last_lun_index[0] = CXLFLASH_NUM_VLUNS/2 - 1;
2386 cfg->last_lun_index[1] = CXLFLASH_NUM_VLUNS/2 - 1;
2387
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002388 cfg->dev_id = (struct pci_device_id *)dev_id;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002389
2390 init_waitqueue_head(&cfg->tmf_waitq);
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002391 init_waitqueue_head(&cfg->reset_waitq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002392
2393 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
2394 cfg->lr_state = LINK_RESET_INVALID;
2395 cfg->lr_port = -1;
Matthew R. Ochs0d731222015-10-21 15:16:24 -05002396 spin_lock_init(&cfg->tmf_slock);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002397 mutex_init(&cfg->ctx_tbl_list_mutex);
2398 mutex_init(&cfg->ctx_recovery_mutex);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002399 init_rwsem(&cfg->ioctl_rwsem);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002400 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
2401 INIT_LIST_HEAD(&cfg->lluns);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002402
2403 pci_set_drvdata(pdev, cfg);
2404
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002405 cfg->cxl_afu = cxl_pci_to_afu(pdev);
2406
2407 rc = init_pci(cfg);
2408 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002409 dev_err(dev, "%s: init_pci failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002410 goto out_remove;
2411 }
2412 cfg->init_state = INIT_STATE_PCI;
2413
2414 rc = init_afu(cfg);
2415 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002416 dev_err(dev, "%s: init_afu failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002417 goto out_remove;
2418 }
2419 cfg->init_state = INIT_STATE_AFU;
2420
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002421 rc = init_scsi(cfg);
2422 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002423 dev_err(dev, "%s: init_scsi failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002424 goto out_remove;
2425 }
2426 cfg->init_state = INIT_STATE_SCSI;
2427
2428out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002429 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002430 return rc;
2431
2432out_remove:
2433 cxlflash_remove(pdev);
2434 goto out;
2435}
2436
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002437/**
2438 * cxlflash_pci_error_detected() - called when a PCI error is detected
2439 * @pdev: PCI device struct.
2440 * @state: PCI channel state.
2441 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002442 * When an EEH occurs during an active reset, wait until the reset is
2443 * complete and then take action based upon the device state.
2444 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002445 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2446 */
2447static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
2448 pci_channel_state_t state)
2449{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002450 int rc = 0;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002451 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2452 struct device *dev = &cfg->dev->dev;
2453
2454 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
2455
2456 switch (state) {
2457 case pci_channel_io_frozen:
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002458 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2459 if (cfg->state == STATE_FAILTERM)
2460 return PCI_ERS_RESULT_DISCONNECT;
2461
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002462 cfg->state = STATE_RESET;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002463 scsi_block_requests(cfg->host);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002464 drain_ioctls(cfg);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002465 rc = cxlflash_mark_contexts_error(cfg);
2466 if (unlikely(rc))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002467 dev_err(dev, "%s: Failed to mark user contexts rc=%d\n",
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002468 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05002469 term_afu(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002470 return PCI_ERS_RESULT_NEED_RESET;
2471 case pci_channel_io_perm_failure:
2472 cfg->state = STATE_FAILTERM;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002473 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002474 scsi_unblock_requests(cfg->host);
2475 return PCI_ERS_RESULT_DISCONNECT;
2476 default:
2477 break;
2478 }
2479 return PCI_ERS_RESULT_NEED_RESET;
2480}
2481
2482/**
2483 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2484 * @pdev: PCI device struct.
2485 *
2486 * This routine is called by the pci error recovery code after the PCI
2487 * slot has been reset, just before we should resume normal operations.
2488 *
2489 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2490 */
2491static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
2492{
2493 int rc = 0;
2494 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2495 struct device *dev = &cfg->dev->dev;
2496
2497 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2498
2499 rc = init_afu(cfg);
2500 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002501 dev_err(dev, "%s: EEH recovery failed rc=%d\n", __func__, rc);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002502 return PCI_ERS_RESULT_DISCONNECT;
2503 }
2504
2505 return PCI_ERS_RESULT_RECOVERED;
2506}
2507
2508/**
2509 * cxlflash_pci_resume() - called when normal operation can resume
2510 * @pdev: PCI device struct
2511 */
2512static void cxlflash_pci_resume(struct pci_dev *pdev)
2513{
2514 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2515 struct device *dev = &cfg->dev->dev;
2516
2517 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2518
2519 cfg->state = STATE_NORMAL;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002520 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002521 scsi_unblock_requests(cfg->host);
2522}
2523
2524static const struct pci_error_handlers cxlflash_err_handler = {
2525 .error_detected = cxlflash_pci_error_detected,
2526 .slot_reset = cxlflash_pci_slot_reset,
2527 .resume = cxlflash_pci_resume,
2528};
2529
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002530/*
2531 * PCI device structure
2532 */
2533static struct pci_driver cxlflash_driver = {
2534 .name = CXLFLASH_NAME,
2535 .id_table = cxlflash_pci_table,
2536 .probe = cxlflash_probe,
2537 .remove = cxlflash_remove,
Uma Krishnanbabf9852016-09-02 15:39:16 -05002538 .shutdown = cxlflash_remove,
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002539 .err_handler = &cxlflash_err_handler,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002540};
2541
2542/**
2543 * init_cxlflash() - module entry point
2544 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002545 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002546 */
2547static int __init init_cxlflash(void)
2548{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002549 cxlflash_list_init();
2550
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002551 return pci_register_driver(&cxlflash_driver);
2552}
2553
2554/**
2555 * exit_cxlflash() - module exit point
2556 */
2557static void __exit exit_cxlflash(void)
2558{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002559 cxlflash_term_global_luns();
2560 cxlflash_free_errpage();
2561
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002562 pci_unregister_driver(&cxlflash_driver);
2563}
2564
2565module_init(init_cxlflash);
2566module_exit(exit_cxlflash);