blob: 04e1a8effa76d1765d79340b04f58655b5e138d5 [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. Ochsfb67d442017-01-11 19:19:47 -0600368 struct cxlflash_cfg *cfg = shost_priv(scp->device->host);
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600369 struct afu_cmd *cmd = sc_to_afucz(scp);
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500370 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500371 ulong lock_flags;
372 int rc = 0;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500373 ulong to;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500374
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500375 /* When Task Management Function is active do not send another */
376 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500377 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500378 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
379 !cfg->tmf_active,
380 cfg->tmf_slock);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500381 cfg->tmf_active = true;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500382 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500383
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600384 cmd->scp = scp;
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600385 cmd->parent = afu;
386 cmd->cmd_tmf = true;
387
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500388 cmd->rcb.ctx_id = afu->ctx_hndl;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600389 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -0500390 cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500391 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500392 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600393 SISL_REQ_FLAGS_SUP_UNDERRUN |
394 SISL_REQ_FLAGS_TMF_CMD);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500395 memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
396
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600397 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500398 if (unlikely(rc)) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500399 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500400 cfg->tmf_active = false;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500401 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500402 goto out;
403 }
404
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500405 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
406 to = msecs_to_jiffies(5000);
407 to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
408 !cfg->tmf_active,
409 cfg->tmf_slock,
410 to);
411 if (!to) {
412 cfg->tmf_active = false;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600413 dev_err(dev, "%s: TMF timed out\n", __func__);
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500414 rc = -1;
415 }
416 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500417out:
418 return rc;
419}
420
421/**
422 * cxlflash_driver_info() - information handler for this host driver
423 * @host: SCSI host associated with device.
424 *
425 * Return: A string describing the device.
426 */
427static const char *cxlflash_driver_info(struct Scsi_Host *host)
428{
429 return CXLFLASH_ADAPTER_NAME;
430}
431
432/**
433 * cxlflash_queuecommand() - sends a mid-layer request
434 * @host: SCSI host associated with device.
435 * @scp: SCSI command to send.
436 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500437 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500438 */
439static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
440{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600441 struct cxlflash_cfg *cfg = shost_priv(host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500442 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500443 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600444 struct afu_cmd *cmd = sc_to_afucz(scp);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600445 struct scatterlist *sg = scsi_sglist(scp);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600446 u16 req_flags = SISL_REQ_FLAGS_SUP_UNDERRUN;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500447 ulong lock_flags;
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600448 int nseg = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500449 int rc = 0;
450
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500451 dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600452 "cdb=(%08x-%08x-%08x-%08x)\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500453 __func__, scp, host->host_no, scp->device->channel,
454 scp->device->id, scp->device->lun,
455 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
456 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
457 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
458 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500459
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500460 /*
461 * If a Task Management Function is active, wait for it to complete
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500462 * before continuing with regular commands.
463 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500464 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500465 if (cfg->tmf_active) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500466 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500467 rc = SCSI_MLQUEUE_HOST_BUSY;
468 goto out;
469 }
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500470 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500471
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500472 switch (cfg->state) {
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500473 case STATE_RESET:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600474 dev_dbg_ratelimited(dev, "%s: device is in reset\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500475 rc = SCSI_MLQUEUE_HOST_BUSY;
476 goto out;
477 case STATE_FAILTERM:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600478 dev_dbg_ratelimited(dev, "%s: device has failed\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500479 scp->result = (DID_NO_CONNECT << 16);
480 scp->scsi_done(scp);
481 rc = 0;
482 goto out;
483 default:
484 break;
485 }
486
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600487 if (likely(sg)) {
488 nseg = scsi_dma_map(scp);
489 if (unlikely(nseg < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600490 dev_err(dev, "%s: Fail DMA map\n", __func__);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600491 rc = SCSI_MLQUEUE_HOST_BUSY;
492 goto out;
493 }
494
495 cmd->rcb.data_len = sg_dma_len(sg);
496 cmd->rcb.data_ea = sg_dma_address(sg);
497 }
498
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600499 cmd->scp = scp;
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600500 cmd->parent = afu;
501
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500502 cmd->rcb.ctx_id = afu->ctx_hndl;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600503 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -0500504 cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500505 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
506
507 if (scp->sc_data_direction == DMA_TO_DEVICE)
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600508 req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500509
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600510 cmd->rcb.req_flags = req_flags;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500511 memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
512
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600513 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600514 if (unlikely(rc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500515 scsi_dma_unmap(scp);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500516out:
517 return rc;
518}
519
520/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500521 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500522 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500523 */
524static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
525{
526 struct pci_dev *pdev = cfg->dev;
527
528 if (pci_channel_offline(pdev))
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500529 wait_event_timeout(cfg->reset_waitq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500530 !pci_channel_offline(pdev),
531 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
532}
533
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500534/**
535 * free_mem() - free memory associated with the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500536 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500537 */
538static void free_mem(struct cxlflash_cfg *cfg)
539{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500540 struct afu *afu = cfg->afu;
541
542 if (cfg->afu) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500543 free_pages((ulong)afu, get_order(sizeof(struct afu)));
544 cfg->afu = NULL;
545 }
546}
547
548/**
549 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500550 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500551 *
552 * Safe to call with AFU in a partially allocated/initialized state.
Manoj Kumaree91e332015-12-14 15:07:02 -0600553 *
Uma Krishnan0df5bef2017-01-11 19:20:03 -0600554 * Cancels scheduled worker threads, waits for any active internal AFU
Matthew R. Ochscba06e62017-04-12 14:13:20 -0500555 * commands to timeout, disables IRQ polling and then unmaps the MMIO space.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500556 */
557static void stop_afu(struct cxlflash_cfg *cfg)
558{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500559 struct afu *afu = cfg->afu;
560
Uma Krishnan0df5bef2017-01-11 19:20:03 -0600561 cancel_work_sync(&cfg->work_q);
562
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500563 if (likely(afu)) {
Matthew R. Ochsde012832016-11-28 18:42:33 -0600564 while (atomic_read(&afu->cmds_active))
565 ssleep(1);
Matthew R. Ochscba06e62017-04-12 14:13:20 -0500566 if (afu_is_irqpoll_enabled(afu))
567 irq_poll_disable(&afu->irqpoll);
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 */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -0500690 for (i = 0; i < cfg->num_fc_ports; i++) {
Uma Krishnan704c4b02016-06-15 18:49:57 -0500691 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 */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -0500700 for (i = 0; i < cfg->num_fc_ports; i++) {
Uma Krishnan704c4b02016-06-15 18:49:57 -0500701 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{
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001073 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001074 int i;
1075 u64 reg;
1076
1077 /* global async interrupts: AFU clears afu_ctrl on context exit
1078 * if async interrupts were sent to that context. This prevents
1079 * the AFU form sending further async interrupts when
1080 * there is
1081 * nobody to receive them.
1082 */
1083
1084 /* mask all */
1085 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
1086 /* set LISN# to send and point to master context */
1087 reg = ((u64) (((afu->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
1088
1089 if (afu->internal_lun)
1090 reg |= 1; /* Bit 63 indicates local lun */
1091 writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1092 /* clear all */
1093 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1094 /* unmask bits that are of interest */
1095 /* note: afu can send an interrupt after this step */
1096 writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1097 /* clear again in case a bit came on after previous clear but before */
1098 /* unmask */
1099 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1100
1101 /* Clear/Set internal lun bits */
1102 reg = readq_be(&afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1103 reg &= SISL_FC_INTERNAL_MASK;
1104 if (afu->internal_lun)
1105 reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
1106 writeq_be(reg, &afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1107
1108 /* now clear FC errors */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001109 for (i = 0; i < cfg->num_fc_ports; i++) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001110 writeq_be(0xFFFFFFFFU,
1111 &afu->afu_map->global.fc_regs[i][FC_ERROR / 8]);
1112 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRCAP / 8]);
1113 }
1114
1115 /* sync interrupts for master's IOARRIN write */
1116 /* note that unlike asyncs, there can be no pending sync interrupts */
1117 /* at this time (this is a fresh context and master has not written */
1118 /* IOARRIN yet), so there is nothing to clear. */
1119
1120 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1121 writeq_be(SISL_MSI_SYNC_ERROR, &afu->host_map->ctx_ctrl);
1122 writeq_be(SISL_ISTATUS_MASK, &afu->host_map->intr_mask);
1123}
1124
1125/**
1126 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1127 * @irq: Interrupt number.
1128 * @data: Private data provided at interrupt registration, the AFU.
1129 *
1130 * Return: Always return IRQ_HANDLED.
1131 */
1132static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1133{
1134 struct afu *afu = (struct afu *)data;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001135 struct cxlflash_cfg *cfg = afu->parent;
1136 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001137 u64 reg;
1138 u64 reg_unmasked;
1139
1140 reg = readq_be(&afu->host_map->intr_status);
1141 reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1142
1143 if (reg_unmasked == 0UL) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001144 dev_err(dev, "%s: spurious interrupt, intr_status=%016llx\n",
1145 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001146 goto cxlflash_sync_err_irq_exit;
1147 }
1148
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001149 dev_err(dev, "%s: unexpected interrupt, intr_status=%016llx\n",
1150 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001151
1152 writeq_be(reg_unmasked, &afu->host_map->intr_clear);
1153
1154cxlflash_sync_err_irq_exit:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001155 return IRQ_HANDLED;
1156}
1157
1158/**
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001159 * process_hrrq() - process the read-response queue
1160 * @afu: AFU associated with the host.
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001161 * @doneq: Queue of commands harvested from the RRQ.
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001162 * @budget: Threshold of RRQ entries to process.
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001163 *
1164 * This routine must be called holding the disabled RRQ spin lock.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001165 *
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001166 * Return: The number of entries processed.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001167 */
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001168static int process_hrrq(struct afu *afu, struct list_head *doneq, int budget)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001169{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001170 struct afu_cmd *cmd;
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001171 struct sisl_ioasa *ioasa;
1172 struct sisl_ioarcb *ioarcb;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001173 bool toggle = afu->toggle;
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001174 int num_hrrq = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001175 u64 entry,
1176 *hrrq_start = afu->hrrq_start,
1177 *hrrq_end = afu->hrrq_end,
1178 *hrrq_curr = afu->hrrq_curr;
1179
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001180 /* Process ready RRQ entries up to the specified budget (if any) */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001181 while (true) {
1182 entry = *hrrq_curr;
1183
1184 if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1185 break;
1186
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001187 entry &= ~SISL_RESP_HANDLE_T_BIT;
1188
1189 if (afu_is_sq_cmd_mode(afu)) {
1190 ioasa = (struct sisl_ioasa *)entry;
1191 cmd = container_of(ioasa, struct afu_cmd, sa);
1192 } else {
1193 ioarcb = (struct sisl_ioarcb *)entry;
1194 cmd = container_of(ioarcb, struct afu_cmd, rcb);
1195 }
1196
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001197 list_add_tail(&cmd->queue, doneq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001198
1199 /* Advance to next entry or wrap and flip the toggle bit */
1200 if (hrrq_curr < hrrq_end)
1201 hrrq_curr++;
1202 else {
1203 hrrq_curr = hrrq_start;
1204 toggle ^= SISL_RESP_HANDLE_T_BIT;
1205 }
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001206
1207 atomic_inc(&afu->hsq_credits);
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001208 num_hrrq++;
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001209
1210 if (budget > 0 && num_hrrq >= budget)
1211 break;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001212 }
1213
1214 afu->hrrq_curr = hrrq_curr;
1215 afu->toggle = toggle;
1216
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001217 return num_hrrq;
1218}
1219
1220/**
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001221 * process_cmd_doneq() - process a queue of harvested RRQ commands
1222 * @doneq: Queue of completed commands.
1223 *
1224 * Note that upon return the queue can no longer be trusted.
1225 */
1226static void process_cmd_doneq(struct list_head *doneq)
1227{
1228 struct afu_cmd *cmd, *tmp;
1229
1230 WARN_ON(list_empty(doneq));
1231
1232 list_for_each_entry_safe(cmd, tmp, doneq, queue)
1233 cmd_complete(cmd);
1234}
1235
1236/**
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001237 * cxlflash_irqpoll() - process a queue of harvested RRQ commands
1238 * @irqpoll: IRQ poll structure associated with queue to poll.
1239 * @budget: Threshold of RRQ entries to process per poll.
1240 *
1241 * Return: The number of entries processed.
1242 */
1243static int cxlflash_irqpoll(struct irq_poll *irqpoll, int budget)
1244{
1245 struct afu *afu = container_of(irqpoll, struct afu, irqpoll);
1246 unsigned long hrrq_flags;
1247 LIST_HEAD(doneq);
1248 int num_entries = 0;
1249
1250 spin_lock_irqsave(&afu->hrrq_slock, hrrq_flags);
1251
1252 num_entries = process_hrrq(afu, &doneq, budget);
1253 if (num_entries < budget)
1254 irq_poll_complete(irqpoll);
1255
1256 spin_unlock_irqrestore(&afu->hrrq_slock, hrrq_flags);
1257
1258 process_cmd_doneq(&doneq);
1259 return num_entries;
1260}
1261
1262/**
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001263 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1264 * @irq: Interrupt number.
1265 * @data: Private data provided at interrupt registration, the AFU.
1266 *
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001267 * Return: IRQ_HANDLED or IRQ_NONE when no ready entries found.
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001268 */
1269static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
1270{
1271 struct afu *afu = (struct afu *)data;
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001272 unsigned long hrrq_flags;
1273 LIST_HEAD(doneq);
1274 int num_entries = 0;
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001275
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001276 spin_lock_irqsave(&afu->hrrq_slock, hrrq_flags);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001277
1278 if (afu_is_irqpoll_enabled(afu)) {
1279 irq_poll_sched(&afu->irqpoll);
1280 spin_unlock_irqrestore(&afu->hrrq_slock, hrrq_flags);
1281 return IRQ_HANDLED;
1282 }
1283
1284 num_entries = process_hrrq(afu, &doneq, -1);
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001285 spin_unlock_irqrestore(&afu->hrrq_slock, hrrq_flags);
1286
1287 if (num_entries == 0)
1288 return IRQ_NONE;
1289
1290 process_cmd_doneq(&doneq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001291 return IRQ_HANDLED;
1292}
1293
1294/**
1295 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1296 * @irq: Interrupt number.
1297 * @data: Private data provided at interrupt registration, the AFU.
1298 *
1299 * Return: Always return IRQ_HANDLED.
1300 */
1301static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1302{
1303 struct afu *afu = (struct afu *)data;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001304 struct cxlflash_cfg *cfg = afu->parent;
1305 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001306 u64 reg_unmasked;
1307 const struct asyc_intr_info *info;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001308 struct sisl_global_map __iomem *global = &afu->afu_map->global;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001309 u64 reg;
1310 u8 port;
1311 int i;
1312
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001313 reg = readq_be(&global->regs.aintr_status);
1314 reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1315
1316 if (reg_unmasked == 0) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001317 dev_err(dev, "%s: spurious interrupt, aintr_status=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001318 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001319 goto out;
1320 }
1321
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001322 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001323 writeq_be(reg_unmasked, &global->regs.aintr_clear);
1324
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001325 /* Check each bit that is on */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001326 for (i = 0; reg_unmasked; i++, reg_unmasked = (reg_unmasked >> 1)) {
1327 info = find_ainfo(1ULL << i);
Matthew R. Ochs16798d32015-10-21 15:13:45 -05001328 if (((reg_unmasked & 0x1) == 0) || !info)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001329 continue;
1330
1331 port = info->port;
1332
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001333 dev_err(dev, "%s: FC Port %d -> %s, fc_status=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001334 __func__, port, info->desc,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001335 readq_be(&global->fc_regs[port][FC_STATUS / 8]));
1336
1337 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001338 * Do link reset first, some OTHER errors will set FC_ERROR
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001339 * again if cleared before or w/o a reset
1340 */
1341 if (info->action & LINK_RESET) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001342 dev_err(dev, "%s: FC Port %d: resetting link\n",
1343 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001344 cfg->lr_state = LINK_RESET_REQUIRED;
1345 cfg->lr_port = port;
1346 schedule_work(&cfg->work_q);
1347 }
1348
1349 if (info->action & CLR_FC_ERROR) {
1350 reg = readq_be(&global->fc_regs[port][FC_ERROR / 8]);
1351
1352 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001353 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001354 * should be the same and tracing one is sufficient.
1355 */
1356
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001357 dev_err(dev, "%s: fc %d: clearing fc_error=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001358 __func__, port, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001359
1360 writeq_be(reg, &global->fc_regs[port][FC_ERROR / 8]);
1361 writeq_be(0, &global->fc_regs[port][FC_ERRCAP / 8]);
1362 }
Matthew R. Ochsef510742015-10-21 15:13:37 -05001363
1364 if (info->action & SCAN_HOST) {
1365 atomic_inc(&cfg->scan_host_needed);
1366 schedule_work(&cfg->work_q);
1367 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001368 }
1369
1370out:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001371 return IRQ_HANDLED;
1372}
1373
1374/**
1375 * start_context() - starts the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001376 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001377 *
1378 * Return: A success or failure value from CXL services.
1379 */
1380static int start_context(struct cxlflash_cfg *cfg)
1381{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001382 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001383 int rc = 0;
1384
1385 rc = cxl_start_context(cfg->mcctx,
1386 cfg->afu->work.work_element_descriptor,
1387 NULL);
1388
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001389 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001390 return rc;
1391}
1392
1393/**
1394 * read_vpd() - obtains the WWPNs from VPD
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001395 * @cfg: Internal structure associated with the host.
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001396 * @wwpn: Array of size MAX_FC_PORTS to pass back WWPNs
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001397 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001398 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001399 */
1400static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1401{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001402 struct device *dev = &cfg->dev->dev;
1403 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001404 int rc = 0;
1405 int ro_start, ro_size, i, j, k;
1406 ssize_t vpd_size;
1407 char vpd_data[CXLFLASH_VPD_LEN];
1408 char tmp_buf[WWPN_BUF_LEN] = { 0 };
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001409 char *wwpn_vpd_tags[MAX_FC_PORTS] = { "V5", "V6" };
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001410
1411 /* Get the VPD data from the device */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001412 vpd_size = cxl_read_adapter_vpd(pdev, vpd_data, sizeof(vpd_data));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001413 if (unlikely(vpd_size <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001414 dev_err(dev, "%s: Unable to read VPD (size = %ld)\n",
1415 __func__, vpd_size);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001416 rc = -ENODEV;
1417 goto out;
1418 }
1419
1420 /* Get the read only section offset */
1421 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
1422 PCI_VPD_LRDT_RO_DATA);
1423 if (unlikely(ro_start < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001424 dev_err(dev, "%s: VPD Read-only data not found\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001425 rc = -ENODEV;
1426 goto out;
1427 }
1428
1429 /* Get the read only section size, cap when extends beyond read VPD */
1430 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1431 j = ro_size;
1432 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1433 if (unlikely((i + j) > vpd_size)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001434 dev_dbg(dev, "%s: Might need to read more VPD (%d > %ld)\n",
1435 __func__, (i + j), vpd_size);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001436 ro_size = vpd_size - i;
1437 }
1438
1439 /*
1440 * Find the offset of the WWPN tag within the read only
1441 * VPD data and validate the found field (partials are
1442 * no good to us). Convert the ASCII data to an integer
1443 * value. Note that we must copy to a temporary buffer
1444 * because the conversion service requires that the ASCII
1445 * string be terminated.
1446 */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001447 for (k = 0; k < cfg->num_fc_ports; k++) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001448 j = ro_size;
1449 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1450
1451 i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
1452 if (unlikely(i < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001453 dev_err(dev, "%s: Port %d WWPN not found in VPD\n",
1454 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001455 rc = -ENODEV;
1456 goto out;
1457 }
1458
1459 j = pci_vpd_info_field_size(&vpd_data[i]);
1460 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1461 if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001462 dev_err(dev, "%s: Port %d WWPN incomplete or bad VPD\n",
1463 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001464 rc = -ENODEV;
1465 goto out;
1466 }
1467
1468 memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1469 rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1470 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001471 dev_err(dev, "%s: WWPN conversion failed for port %d\n",
1472 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001473 rc = -ENODEV;
1474 goto out;
1475 }
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001476
1477 dev_dbg(dev, "%s: wwpn%d=%016llx\n", __func__, k, wwpn[k]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001478 }
1479
1480out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001481 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001482 return rc;
1483}
1484
1485/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001486 * init_pcr() - initialize the provisioning and control registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001487 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001488 *
1489 * Also sets up fast access to the mapped registers and initializes AFU
1490 * command fields that never change.
1491 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001492static void init_pcr(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001493{
1494 struct afu *afu = cfg->afu;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001495 struct sisl_ctrl_map __iomem *ctrl_map;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001496 int i;
1497
1498 for (i = 0; i < MAX_CONTEXT; i++) {
1499 ctrl_map = &afu->afu_map->ctrls[i].ctrl;
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001500 /* Disrupt any clients that could be running */
1501 /* e.g. clients that survived a master restart */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001502 writeq_be(0, &ctrl_map->rht_start);
1503 writeq_be(0, &ctrl_map->rht_cnt_id);
1504 writeq_be(0, &ctrl_map->ctx_cap);
1505 }
1506
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001507 /* Copy frequently used fields into afu */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001508 afu->ctx_hndl = (u16) cxl_process_element(cfg->mcctx);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001509 afu->host_map = &afu->afu_map->hosts[afu->ctx_hndl].host;
1510 afu->ctrl_map = &afu->afu_map->ctrls[afu->ctx_hndl].ctrl;
1511
1512 /* Program the Endian Control for the master context */
1513 writeq_be(SISL_ENDIAN_CTRL, &afu->host_map->endian_ctrl);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001514}
1515
1516/**
1517 * init_global() - initialize AFU global registers
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 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001520static int init_global(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001521{
1522 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001523 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001524 u64 wwpn[MAX_FC_PORTS]; /* wwpn of AFU ports */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001525 int i = 0, num_ports = 0;
1526 int rc = 0;
1527 u64 reg;
1528
1529 rc = read_vpd(cfg, &wwpn[0]);
1530 if (rc) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001531 dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001532 goto out;
1533 }
1534
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001535 /* Set up RRQ and SQ in AFU for master issued cmds */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001536 writeq_be((u64) afu->hrrq_start, &afu->host_map->rrq_start);
1537 writeq_be((u64) afu->hrrq_end, &afu->host_map->rrq_end);
1538
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001539 if (afu_is_sq_cmd_mode(afu)) {
1540 writeq_be((u64)afu->hsq_start, &afu->host_map->sq_start);
1541 writeq_be((u64)afu->hsq_end, &afu->host_map->sq_end);
1542 }
1543
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001544 /* AFU configuration */
1545 reg = readq_be(&afu->afu_map->global.regs.afu_config);
1546 reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1547 /* enable all auto retry options and control endianness */
1548 /* leave others at default: */
1549 /* CTX_CAP write protected, mbox_r does not clear on read and */
1550 /* checker on if dual afu */
1551 writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1552
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001553 /* Global port select: select either port */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001554 if (afu->internal_lun) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001555 /* Only use port 0 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001556 writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001557 num_ports = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001558 } else {
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -05001559 writeq_be(PORT_MASK(cfg->num_fc_ports),
1560 &afu->afu_map->global.regs.afu_port_sel);
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001561 num_ports = cfg->num_fc_ports;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001562 }
1563
1564 for (i = 0; i < num_ports; i++) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001565 /* Unmask all errors (but they are still masked at AFU) */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001566 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRMSK / 8]);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001567 /* Clear CRC error cnt & set a threshold */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001568 (void)readq_be(&afu->afu_map->global.
1569 fc_regs[i][FC_CNT_CRCERR / 8]);
1570 writeq_be(MC_CRC_THRESH, &afu->afu_map->global.fc_regs[i]
1571 [FC_CRC_THRESH / 8]);
1572
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001573 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001574 if (wwpn[i] != 0)
1575 afu_set_wwpn(afu, i,
1576 &afu->afu_map->global.fc_regs[i][0],
1577 wwpn[i]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001578 /* Programming WWPN back to back causes additional
1579 * offline/online transitions and a PLOGI
1580 */
1581 msleep(100);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001582 }
1583
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001584 /* Set up master's own CTX_CAP to allow real mode, host translation */
1585 /* tables, afu cmds and read/write GSCSI cmds. */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001586 /* First, unlock ctx_cap write by reading mbox */
1587 (void)readq_be(&afu->ctrl_map->mbox_r); /* unlock ctx_cap */
1588 writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1589 SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1590 SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1591 &afu->ctrl_map->ctx_cap);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001592 /* Initialize heartbeat */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001593 afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001594out:
1595 return rc;
1596}
1597
1598/**
1599 * start_afu() - initializes and starts the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001600 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001601 */
1602static int start_afu(struct cxlflash_cfg *cfg)
1603{
1604 struct afu *afu = cfg->afu;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001605 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001606 int rc = 0;
1607
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001608 init_pcr(cfg);
1609
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001610 /* Initialize RRQ */
Matthew R. Ochsaf104832015-10-21 15:15:14 -05001611 memset(&afu->rrq_entry, 0, sizeof(afu->rrq_entry));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001612 afu->hrrq_start = &afu->rrq_entry[0];
1613 afu->hrrq_end = &afu->rrq_entry[NUM_RRQ_ENTRY - 1];
1614 afu->hrrq_curr = afu->hrrq_start;
1615 afu->toggle = 1;
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001616 spin_lock_init(&afu->hrrq_slock);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001617
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001618 /* Initialize SQ */
1619 if (afu_is_sq_cmd_mode(afu)) {
1620 memset(&afu->sq, 0, sizeof(afu->sq));
1621 afu->hsq_start = &afu->sq[0];
1622 afu->hsq_end = &afu->sq[NUM_SQ_ENTRY - 1];
1623 afu->hsq_curr = afu->hsq_start;
1624
1625 spin_lock_init(&afu->hsq_slock);
1626 atomic_set(&afu->hsq_credits, NUM_SQ_ENTRY - 1);
1627 }
1628
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001629 /* Initialize IRQ poll */
1630 if (afu_is_irqpoll_enabled(afu))
1631 irq_poll_init(&afu->irqpoll, afu->irqpoll_weight,
1632 cxlflash_irqpoll);
1633
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001634 rc = init_global(cfg);
1635
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001636 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001637 return rc;
1638}
1639
1640/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001641 * init_intr() - setup interrupt handlers for the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001642 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001643 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001644 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001645 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001646static enum undo_level init_intr(struct cxlflash_cfg *cfg,
1647 struct cxl_context *ctx)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001648{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001649 struct afu *afu = cfg->afu;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001650 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001651 int rc = 0;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001652 enum undo_level level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001653
1654 rc = cxl_allocate_afu_irqs(ctx, 3);
1655 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001656 dev_err(dev, "%s: allocate_afu_irqs failed rc=%d\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001657 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001658 level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001659 goto out;
1660 }
1661
1662 rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, afu,
1663 "SISL_MSI_SYNC_ERROR");
1664 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001665 dev_err(dev, "%s: SISL_MSI_SYNC_ERROR map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001666 level = FREE_IRQ;
1667 goto out;
1668 }
1669
1670 rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, afu,
1671 "SISL_MSI_RRQ_UPDATED");
1672 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001673 dev_err(dev, "%s: SISL_MSI_RRQ_UPDATED map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001674 level = UNMAP_ONE;
1675 goto out;
1676 }
1677
1678 rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, afu,
1679 "SISL_MSI_ASYNC_ERROR");
1680 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001681 dev_err(dev, "%s: SISL_MSI_ASYNC_ERROR map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001682 level = UNMAP_TWO;
1683 goto out;
1684 }
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001685out:
1686 return level;
1687}
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001688
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001689/**
1690 * init_mc() - create and register as the master context
1691 * @cfg: Internal structure associated with the host.
1692 *
1693 * Return: 0 on success, -errno on failure
1694 */
1695static int init_mc(struct cxlflash_cfg *cfg)
1696{
1697 struct cxl_context *ctx;
1698 struct device *dev = &cfg->dev->dev;
1699 int rc = 0;
1700 enum undo_level level;
1701
1702 ctx = cxl_get_context(cfg->dev);
1703 if (unlikely(!ctx)) {
1704 rc = -ENOMEM;
1705 goto ret;
1706 }
1707 cfg->mcctx = ctx;
1708
1709 /* Set it up as a master with the CXL */
1710 cxl_set_master(ctx);
1711
1712 /* During initialization reset the AFU to start from a clean slate */
1713 rc = cxl_afu_reset(cfg->mcctx);
1714 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001715 dev_err(dev, "%s: AFU reset failed rc=%d\n", __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001716 goto ret;
1717 }
1718
1719 level = init_intr(cfg, ctx);
1720 if (unlikely(level)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001721 dev_err(dev, "%s: interrupt init failed rc=%d\n", __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001722 goto out;
1723 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001724
1725 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1726 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1727 * element (pe) that is embedded in the context (ctx)
1728 */
1729 rc = start_context(cfg);
1730 if (unlikely(rc)) {
1731 dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
1732 level = UNMAP_THREE;
1733 goto out;
1734 }
1735ret:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001736 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001737 return rc;
1738out:
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001739 term_intr(cfg, level);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001740 goto ret;
1741}
1742
1743/**
1744 * init_afu() - setup as master context and start AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001745 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001746 *
1747 * This routine is a higher level of control for configuring the
1748 * AFU on probe and reset paths.
1749 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001750 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001751 */
1752static int init_afu(struct cxlflash_cfg *cfg)
1753{
1754 u64 reg;
1755 int rc = 0;
1756 struct afu *afu = cfg->afu;
1757 struct device *dev = &cfg->dev->dev;
1758
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001759 cxl_perst_reloads_same_image(cfg->cxl_afu, true);
1760
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001761 rc = init_mc(cfg);
1762 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001763 dev_err(dev, "%s: init_mc failed rc=%d\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001764 __func__, rc);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001765 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001766 }
1767
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001768 /* Map the entire MMIO space of the AFU */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001769 afu->afu_map = cxl_psa_map(cfg->mcctx);
1770 if (!afu->afu_map) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001771 dev_err(dev, "%s: cxl_psa_map failed\n", __func__);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001772 rc = -ENOMEM;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001773 goto err1;
1774 }
1775
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001776 /* No byte reverse on reading afu_version or string will be backwards */
1777 reg = readq(&afu->afu_map->global.regs.afu_version);
1778 memcpy(afu->version, &reg, sizeof(reg));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001779 afu->interface_version =
1780 readq_be(&afu->afu_map->global.regs.interface_version);
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001781 if ((afu->interface_version + 1) == 0) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001782 dev_err(dev, "Back level AFU, please upgrade. AFU version %s "
1783 "interface version %016llx\n", afu->version,
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001784 afu->interface_version);
1785 rc = -EINVAL;
Uma Krishnan0df5bef2017-01-11 19:20:03 -06001786 goto err1;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001787 }
1788
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001789 if (afu_is_sq_cmd_mode(afu)) {
1790 afu->send_cmd = send_cmd_sq;
1791 afu->context_reset = context_reset_sq;
1792 } else {
1793 afu->send_cmd = send_cmd_ioarrin;
1794 afu->context_reset = context_reset_ioarrin;
1795 }
Matthew R. Ochs48b4be32016-11-28 18:43:09 -06001796
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001797 dev_dbg(dev, "%s: afu_ver=%s interface_ver=%016llx\n", __func__,
1798 afu->version, afu->interface_version);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001799
1800 rc = start_afu(cfg);
1801 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001802 dev_err(dev, "%s: start_afu failed, rc=%d\n", __func__, rc);
Uma Krishnan0df5bef2017-01-11 19:20:03 -06001803 goto err1;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001804 }
1805
1806 afu_err_intr_init(cfg->afu);
Uma Krishnan11f7b182016-11-28 18:41:45 -06001807 spin_lock_init(&afu->rrin_slock);
1808 afu->room = readq_be(&afu->host_map->cmd_room);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001809
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05001810 /* Restore the LUN mappings */
1811 cxlflash_restore_luntable(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001812out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001813 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001814 return rc;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001815
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001816err1:
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001817 term_intr(cfg, UNMAP_THREE);
1818 term_mc(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001819 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001820}
1821
1822/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001823 * cxlflash_afu_sync() - builds and sends an AFU sync command
1824 * @afu: AFU associated with the host.
1825 * @ctx_hndl_u: Identifies context requesting sync.
1826 * @res_hndl_u: Identifies resource requesting sync.
1827 * @mode: Type of sync to issue (lightweight, heavyweight, global).
1828 *
1829 * The AFU can only take 1 sync command at a time. This routine enforces this
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001830 * limitation by using a mutex to provide exclusive access to the AFU during
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001831 * the sync. This design point requires calling threads to not be on interrupt
1832 * context due to the possibility of sleeping during concurrent sync operations.
1833 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001834 * AFU sync operations are only necessary and allowed when the device is
1835 * operating normally. When not operating normally, sync requests can occur as
1836 * part of cleaning up resources associated with an adapter prior to removal.
1837 * In this scenario, these requests are simply ignored (safe due to the AFU
1838 * going away).
1839 *
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001840 * Return:
1841 * 0 on success
1842 * -1 on failure
1843 */
1844int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
1845 res_hndl_t res_hndl_u, u8 mode)
1846{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001847 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001848 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001849 struct afu_cmd *cmd = NULL;
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001850 char *buf = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001851 int rc = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001852 static DEFINE_MUTEX(sync_active);
1853
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001854 if (cfg->state != STATE_NORMAL) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001855 dev_dbg(dev, "%s: Sync not required state=%u\n",
1856 __func__, cfg->state);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001857 return 0;
1858 }
1859
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001860 mutex_lock(&sync_active);
Matthew R. Ochsde012832016-11-28 18:42:33 -06001861 atomic_inc(&afu->cmds_active);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001862 buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
1863 if (unlikely(!buf)) {
1864 dev_err(dev, "%s: no memory for command\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001865 rc = -1;
1866 goto out;
1867 }
1868
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001869 cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
1870 init_completion(&cmd->cevent);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001871 cmd->parent = afu;
1872
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001873 dev_dbg(dev, "%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001874
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001875 cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001876 cmd->rcb.ctx_id = afu->ctx_hndl;
1877 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001878 cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
1879
1880 cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
1881 cmd->rcb.cdb[1] = mode;
1882
1883 /* The cdb is aligned, no unaligned accessors required */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001884 *((__be16 *)&cmd->rcb.cdb[2]) = cpu_to_be16(ctx_hndl_u);
1885 *((__be32 *)&cmd->rcb.cdb[4]) = cpu_to_be32(res_hndl_u);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001886
Matthew R. Ochs48b4be32016-11-28 18:43:09 -06001887 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001888 if (unlikely(rc))
1889 goto out;
1890
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -06001891 rc = wait_resp(afu, cmd);
1892 if (unlikely(rc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001893 rc = -1;
1894out:
Matthew R. Ochsde012832016-11-28 18:42:33 -06001895 atomic_dec(&afu->cmds_active);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001896 mutex_unlock(&sync_active);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001897 kfree(buf);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001898 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001899 return rc;
1900}
1901
1902/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001903 * afu_reset() - resets the AFU
1904 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001905 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001906 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001907 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001908static int afu_reset(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001909{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001910 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001911 int rc = 0;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001912
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001913 /* Stop the context before the reset. Since the context is
1914 * no longer available restart it after the reset is complete
1915 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001916 term_afu(cfg);
1917
1918 rc = init_afu(cfg);
1919
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001920 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001921 return rc;
1922}
1923
1924/**
Manoj N. Kumarf4113962016-06-15 18:49:20 -05001925 * drain_ioctls() - wait until all currently executing ioctls have completed
1926 * @cfg: Internal structure associated with the host.
1927 *
1928 * Obtain write access to read/write semaphore that wraps ioctl
1929 * handling to 'drain' ioctls currently executing.
1930 */
1931static void drain_ioctls(struct cxlflash_cfg *cfg)
1932{
1933 down_write(&cfg->ioctl_rwsem);
1934 up_write(&cfg->ioctl_rwsem);
1935}
1936
1937/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001938 * cxlflash_eh_device_reset_handler() - reset a single LUN
1939 * @scp: SCSI command to send.
1940 *
1941 * Return:
1942 * SUCCESS as defined in scsi/scsi.h
1943 * FAILED as defined in scsi/scsi.h
1944 */
1945static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
1946{
1947 int rc = SUCCESS;
1948 struct Scsi_Host *host = scp->device->host;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001949 struct cxlflash_cfg *cfg = shost_priv(host);
1950 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001951 struct afu *afu = cfg->afu;
1952 int rcr = 0;
1953
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001954 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
1955 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
1956 scp->device->channel, scp->device->id, scp->device->lun,
1957 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1958 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1959 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1960 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochs15305512015-10-21 15:12:10 -05001961
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001962retry:
Matthew R. Ochs15305512015-10-21 15:12:10 -05001963 switch (cfg->state) {
1964 case STATE_NORMAL:
1965 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
1966 if (unlikely(rcr))
1967 rc = FAILED;
1968 break;
1969 case STATE_RESET:
1970 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001971 goto retry;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001972 default:
1973 rc = FAILED;
1974 break;
1975 }
1976
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001977 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -05001978 return rc;
1979}
1980
1981/**
1982 * cxlflash_eh_host_reset_handler() - reset the host adapter
1983 * @scp: SCSI command from stack identifying host.
1984 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05001985 * Following a reset, the state is evaluated again in case an EEH occurred
1986 * during the reset. In such a scenario, the host reset will either yield
1987 * until the EEH recovery is complete or return success or failure based
1988 * upon the current device state.
1989 *
Matthew R. Ochs15305512015-10-21 15:12:10 -05001990 * Return:
1991 * SUCCESS as defined in scsi/scsi.h
1992 * FAILED as defined in scsi/scsi.h
1993 */
1994static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
1995{
1996 int rc = SUCCESS;
1997 int rcr = 0;
1998 struct Scsi_Host *host = scp->device->host;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001999 struct cxlflash_cfg *cfg = shost_priv(host);
2000 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002001
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002002 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
2003 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
2004 scp->device->channel, scp->device->id, scp->device->lun,
2005 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
2006 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
2007 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
2008 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochs15305512015-10-21 15:12:10 -05002009
2010 switch (cfg->state) {
2011 case STATE_NORMAL:
2012 cfg->state = STATE_RESET;
Manoj N. Kumarf4113962016-06-15 18:49:20 -05002013 drain_ioctls(cfg);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002014 cxlflash_mark_contexts_error(cfg);
2015 rcr = afu_reset(cfg);
2016 if (rcr) {
2017 rc = FAILED;
2018 cfg->state = STATE_FAILTERM;
2019 } else
2020 cfg->state = STATE_NORMAL;
2021 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002022 ssleep(1);
2023 /* fall through */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002024 case STATE_RESET:
2025 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2026 if (cfg->state == STATE_NORMAL)
2027 break;
2028 /* fall through */
2029 default:
2030 rc = FAILED;
2031 break;
2032 }
2033
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002034 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002035 return rc;
2036}
2037
2038/**
2039 * cxlflash_change_queue_depth() - change the queue depth for the device
2040 * @sdev: SCSI device destined for queue depth change.
2041 * @qdepth: Requested queue depth value to set.
2042 *
2043 * The requested queue depth is capped to the maximum supported value.
2044 *
2045 * Return: The actual queue depth set.
2046 */
2047static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
2048{
2049
2050 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
2051 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
2052
2053 scsi_change_queue_depth(sdev, qdepth);
2054 return sdev->queue_depth;
2055}
2056
2057/**
2058 * cxlflash_show_port_status() - queries and presents the current port status
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002059 * @port: Desired port for status reporting.
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002060 * @cfg: Internal structure associated with the host.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002061 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2062 *
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002063 * Return: The size of the ASCII string returned in @buf or -EINVAL.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002064 */
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002065static ssize_t cxlflash_show_port_status(u32 port,
2066 struct cxlflash_cfg *cfg,
2067 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002068{
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002069 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002070 struct afu *afu = cfg->afu;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002071 char *disp_status;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002072 u64 status;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002073 __be64 __iomem *fc_regs;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002074
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002075 WARN_ON(port >= MAX_FC_PORTS);
2076
2077 if (port >= cfg->num_fc_ports) {
2078 dev_info(dev, "%s: Port %d not supported on this card.\n",
2079 __func__, port);
2080 return -EINVAL;
2081 }
Matthew R. Ochs15305512015-10-21 15:12:10 -05002082
2083 fc_regs = &afu->afu_map->global.fc_regs[port][0];
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002084 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
2085 status &= FC_MTIP_STATUS_MASK;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002086
2087 if (status == FC_MTIP_STATUS_ONLINE)
2088 disp_status = "online";
2089 else if (status == FC_MTIP_STATUS_OFFLINE)
2090 disp_status = "offline";
2091 else
2092 disp_status = "unknown";
2093
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002094 return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002095}
2096
2097/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002098 * port0_show() - queries and presents the current status of port 0
2099 * @dev: Generic device associated with the host owning the port.
2100 * @attr: Device attribute representing the port.
2101 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002102 *
2103 * Return: The size of the ASCII string returned in @buf.
2104 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002105static ssize_t port0_show(struct device *dev,
2106 struct device_attribute *attr,
2107 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002108{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002109 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochs15305512015-10-21 15:12:10 -05002110
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002111 return cxlflash_show_port_status(0, cfg, buf);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002112}
2113
2114/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002115 * port1_show() - queries and presents the current status of port 1
2116 * @dev: Generic device associated with the host owning the port.
2117 * @attr: Device attribute representing the port.
2118 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2119 *
2120 * Return: The size of the ASCII string returned in @buf.
2121 */
2122static ssize_t port1_show(struct device *dev,
2123 struct device_attribute *attr,
2124 char *buf)
2125{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002126 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002127
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002128 return cxlflash_show_port_status(1, cfg, buf);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002129}
2130
2131/**
2132 * lun_mode_show() - presents the current LUN mode of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002133 * @dev: Generic device associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002134 * @attr: Device attribute representing the LUN mode.
2135 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2136 *
2137 * Return: The size of the ASCII string returned in @buf.
2138 */
2139static ssize_t lun_mode_show(struct device *dev,
2140 struct device_attribute *attr, char *buf)
2141{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002142 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002143 struct afu *afu = cfg->afu;
2144
2145 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
2146}
2147
2148/**
2149 * lun_mode_store() - sets the LUN mode of the host
2150 * @dev: Generic device associated with the host.
2151 * @attr: Device attribute representing the LUN mode.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002152 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2153 * @count: Length of data resizing in @buf.
2154 *
2155 * The CXL Flash AFU supports a dummy LUN mode where the external
2156 * links and storage are not required. Space on the FPGA is used
2157 * to create 1 or 2 small LUNs which are presented to the system
2158 * as if they were a normal storage device. This feature is useful
2159 * during development and also provides manufacturing with a way
2160 * to test the AFU without an actual device.
2161 *
2162 * 0 = external LUN[s] (default)
2163 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2164 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2165 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2166 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2167 *
2168 * Return: The size of the ASCII string returned in @buf.
2169 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002170static ssize_t lun_mode_store(struct device *dev,
2171 struct device_attribute *attr,
2172 const char *buf, size_t count)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002173{
2174 struct Scsi_Host *shost = class_to_shost(dev);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002175 struct cxlflash_cfg *cfg = shost_priv(shost);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002176 struct afu *afu = cfg->afu;
2177 int rc;
2178 u32 lun_mode;
2179
2180 rc = kstrtouint(buf, 10, &lun_mode);
2181 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
2182 afu->internal_lun = lun_mode;
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002183
2184 /*
2185 * When configured for internal LUN, there is only one channel,
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002186 * channel number 0, else there will be one less than the number
2187 * of fc ports for this card.
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002188 */
2189 if (afu->internal_lun)
2190 shost->max_channel = 0;
2191 else
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -05002192 shost->max_channel = PORTNUM2CHAN(cfg->num_fc_ports);
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002193
Matthew R. Ochs15305512015-10-21 15:12:10 -05002194 afu_reset(cfg);
2195 scsi_scan_host(cfg->host);
2196 }
2197
2198 return count;
2199}
2200
2201/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002202 * ioctl_version_show() - presents the current ioctl version of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002203 * @dev: Generic device associated with the host.
2204 * @attr: Device attribute representing the ioctl version.
2205 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2206 *
2207 * Return: The size of the ASCII string returned in @buf.
2208 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002209static ssize_t ioctl_version_show(struct device *dev,
2210 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002211{
2212 return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
2213}
2214
2215/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002216 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2217 * @port: Desired port for status reporting.
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002218 * @cfg: Internal structure associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002219 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2220 *
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002221 * Return: The size of the ASCII string returned in @buf or -EINVAL.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002222 */
2223static ssize_t cxlflash_show_port_lun_table(u32 port,
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002224 struct cxlflash_cfg *cfg,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002225 char *buf)
2226{
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002227 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002228 struct afu *afu = cfg->afu;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002229 int i;
2230 ssize_t bytes = 0;
2231 __be64 __iomem *fc_port;
2232
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002233 WARN_ON(port >= MAX_FC_PORTS);
2234
2235 if (port >= cfg->num_fc_ports) {
2236 dev_info(dev, "%s: Port %d not supported on this card.\n",
2237 __func__, port);
2238 return -EINVAL;
2239 }
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002240
2241 fc_port = &afu->afu_map->global.fc_port[port][0];
2242
2243 for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2244 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002245 "%03d: %016llx\n", i, readq_be(&fc_port[i]));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002246 return bytes;
2247}
2248
2249/**
2250 * port0_lun_table_show() - presents the current LUN table of port 0
2251 * @dev: Generic device associated with the host owning the port.
2252 * @attr: Device attribute representing the port.
2253 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2254 *
2255 * Return: The size of the ASCII string returned in @buf.
2256 */
2257static ssize_t port0_lun_table_show(struct device *dev,
2258 struct device_attribute *attr,
2259 char *buf)
2260{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002261 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002262
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002263 return cxlflash_show_port_lun_table(0, cfg, buf);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002264}
2265
2266/**
2267 * port1_lun_table_show() - presents the current LUN table of port 1
2268 * @dev: Generic device associated with the host owning the port.
2269 * @attr: Device attribute representing the port.
2270 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2271 *
2272 * Return: The size of the ASCII string returned in @buf.
2273 */
2274static ssize_t port1_lun_table_show(struct device *dev,
2275 struct device_attribute *attr,
2276 char *buf)
2277{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002278 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002279
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002280 return cxlflash_show_port_lun_table(1, cfg, buf);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002281}
2282
2283/**
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002284 * irqpoll_weight_show() - presents the current IRQ poll weight for the host
2285 * @dev: Generic device associated with the host.
2286 * @attr: Device attribute representing the IRQ poll weight.
2287 * @buf: Buffer of length PAGE_SIZE to report back the current IRQ poll
2288 * weight in ASCII.
2289 *
2290 * An IRQ poll weight of 0 indicates polling is disabled.
2291 *
2292 * Return: The size of the ASCII string returned in @buf.
2293 */
2294static ssize_t irqpoll_weight_show(struct device *dev,
2295 struct device_attribute *attr, char *buf)
2296{
2297 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2298 struct afu *afu = cfg->afu;
2299
2300 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->irqpoll_weight);
2301}
2302
2303/**
2304 * irqpoll_weight_store() - sets the current IRQ poll weight for the host
2305 * @dev: Generic device associated with the host.
2306 * @attr: Device attribute representing the IRQ poll weight.
2307 * @buf: Buffer of length PAGE_SIZE containing the desired IRQ poll
2308 * weight in ASCII.
2309 * @count: Length of data resizing in @buf.
2310 *
2311 * An IRQ poll weight of 0 indicates polling is disabled.
2312 *
2313 * Return: The size of the ASCII string returned in @buf.
2314 */
2315static ssize_t irqpoll_weight_store(struct device *dev,
2316 struct device_attribute *attr,
2317 const char *buf, size_t count)
2318{
2319 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2320 struct device *cfgdev = &cfg->dev->dev;
2321 struct afu *afu = cfg->afu;
2322 u32 weight;
2323 int rc;
2324
2325 rc = kstrtouint(buf, 10, &weight);
2326 if (rc)
2327 return -EINVAL;
2328
2329 if (weight > 256) {
2330 dev_info(cfgdev,
2331 "Invalid IRQ poll weight. It must be 256 or less.\n");
2332 return -EINVAL;
2333 }
2334
2335 if (weight == afu->irqpoll_weight) {
2336 dev_info(cfgdev,
2337 "Current IRQ poll weight has the same weight.\n");
2338 return -EINVAL;
2339 }
2340
2341 if (afu_is_irqpoll_enabled(afu))
2342 irq_poll_disable(&afu->irqpoll);
2343
2344 afu->irqpoll_weight = weight;
2345
2346 if (weight > 0)
2347 irq_poll_init(&afu->irqpoll, weight, cxlflash_irqpoll);
2348
2349 return count;
2350}
2351
2352/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002353 * mode_show() - presents the current mode of the device
Matthew R. Ochs15305512015-10-21 15:12:10 -05002354 * @dev: Generic device associated with the device.
2355 * @attr: Device attribute representing the device mode.
2356 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2357 *
2358 * Return: The size of the ASCII string returned in @buf.
2359 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002360static ssize_t mode_show(struct device *dev,
2361 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002362{
2363 struct scsi_device *sdev = to_scsi_device(dev);
2364
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002365 return scnprintf(buf, PAGE_SIZE, "%s\n",
2366 sdev->hostdata ? "superpipe" : "legacy");
Matthew R. Ochs15305512015-10-21 15:12:10 -05002367}
2368
2369/*
2370 * Host attributes
2371 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002372static DEVICE_ATTR_RO(port0);
2373static DEVICE_ATTR_RO(port1);
2374static DEVICE_ATTR_RW(lun_mode);
2375static DEVICE_ATTR_RO(ioctl_version);
2376static DEVICE_ATTR_RO(port0_lun_table);
2377static DEVICE_ATTR_RO(port1_lun_table);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002378static DEVICE_ATTR_RW(irqpoll_weight);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002379
2380static struct device_attribute *cxlflash_host_attrs[] = {
2381 &dev_attr_port0,
2382 &dev_attr_port1,
2383 &dev_attr_lun_mode,
2384 &dev_attr_ioctl_version,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002385 &dev_attr_port0_lun_table,
2386 &dev_attr_port1_lun_table,
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002387 &dev_attr_irqpoll_weight,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002388 NULL
2389};
2390
2391/*
2392 * Device attributes
2393 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002394static DEVICE_ATTR_RO(mode);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002395
2396static struct device_attribute *cxlflash_dev_attrs[] = {
2397 &dev_attr_mode,
2398 NULL
2399};
2400
2401/*
2402 * Host template
2403 */
2404static struct scsi_host_template driver_template = {
2405 .module = THIS_MODULE,
2406 .name = CXLFLASH_ADAPTER_NAME,
2407 .info = cxlflash_driver_info,
2408 .ioctl = cxlflash_ioctl,
2409 .proc_name = CXLFLASH_NAME,
2410 .queuecommand = cxlflash_queuecommand,
2411 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
2412 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
2413 .change_queue_depth = cxlflash_change_queue_depth,
Manoj N. Kumar83430832016-03-04 15:55:20 -06002414 .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002415 .can_queue = CXLFLASH_MAX_CMDS,
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -06002416 .cmd_size = sizeof(struct afu_cmd) + __alignof__(struct afu_cmd) - 1,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002417 .this_id = -1,
Uma Krishnan68ab2d72016-11-28 18:41:06 -06002418 .sg_tablesize = 1, /* No scatter gather support */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002419 .max_sectors = CXLFLASH_MAX_SECTORS,
2420 .use_clustering = ENABLE_CLUSTERING,
2421 .shost_attrs = cxlflash_host_attrs,
2422 .sdev_attrs = cxlflash_dev_attrs,
2423};
2424
2425/*
2426 * Device dependent values
2427 */
Uma Krishnan96e1b662016-06-15 18:49:38 -05002428static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
2429 0ULL };
2430static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
Uma Krishnan704c4b02016-06-15 18:49:57 -05002431 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs94344522017-02-16 21:39:32 -06002432static struct dev_dependent_vals dev_briard_vals = { CXLFLASH_MAX_SECTORS,
2433 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs15305512015-10-21 15:12:10 -05002434
2435/*
2436 * PCI device binding table
2437 */
2438static struct pci_device_id cxlflash_pci_table[] = {
2439 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
2440 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
Manoj Kumara2746fb2015-12-14 15:07:43 -06002441 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
2442 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
Matthew R. Ochs94344522017-02-16 21:39:32 -06002443 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_BRIARD,
2444 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_briard_vals},
Matthew R. Ochs15305512015-10-21 15:12:10 -05002445 {}
2446};
2447
2448MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
2449
2450/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002451 * cxlflash_worker_thread() - work thread handler for the AFU
2452 * @work: Work structure contained within cxlflash associated with host.
2453 *
2454 * Handles the following events:
2455 * - Link reset which cannot be performed on interrupt context due to
2456 * blocking up to a few seconds
Matthew R. Ochsef510742015-10-21 15:13:37 -05002457 * - Rescan the host
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002458 */
2459static void cxlflash_worker_thread(struct work_struct *work)
2460{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002461 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
2462 work_q);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002463 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002464 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002465 int port;
2466 ulong lock_flags;
2467
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002468 /* Avoid MMIO if the device has failed */
2469
2470 if (cfg->state != STATE_NORMAL)
2471 return;
2472
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002473 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2474
2475 if (cfg->lr_state == LINK_RESET_REQUIRED) {
2476 port = cfg->lr_port;
2477 if (port < 0)
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002478 dev_err(dev, "%s: invalid port index %d\n",
2479 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002480 else {
2481 spin_unlock_irqrestore(cfg->host->host_lock,
2482 lock_flags);
2483
2484 /* The reset can block... */
2485 afu_link_reset(afu, port,
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05002486 &afu->afu_map->global.fc_regs[port][0]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002487 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2488 }
2489
2490 cfg->lr_state = LINK_RESET_COMPLETE;
2491 }
2492
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002493 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
Matthew R. Ochsef510742015-10-21 15:13:37 -05002494
2495 if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
2496 scsi_scan_host(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002497}
2498
2499/**
2500 * cxlflash_probe() - PCI entry point to add host
2501 * @pdev: PCI device associated with the host.
2502 * @dev_id: PCI device id associated with device.
2503 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002504 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002505 */
2506static int cxlflash_probe(struct pci_dev *pdev,
2507 const struct pci_device_id *dev_id)
2508{
2509 struct Scsi_Host *host;
2510 struct cxlflash_cfg *cfg = NULL;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002511 struct device *dev = &pdev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002512 struct dev_dependent_vals *ddv;
2513 int rc = 0;
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002514 int k;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002515
2516 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
2517 __func__, pdev->irq);
2518
2519 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
2520 driver_template.max_sectors = ddv->max_sectors;
2521
2522 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
2523 if (!host) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002524 dev_err(dev, "%s: scsi_host_alloc failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002525 rc = -ENOMEM;
2526 goto out;
2527 }
2528
2529 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
2530 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -05002531 host->max_channel = PORTNUM2CHAN(NUM_FC_PORTS);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002532 host->unique_id = host->host_no;
2533 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
2534
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002535 cfg = shost_priv(host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002536 cfg->host = host;
2537 rc = alloc_mem(cfg);
2538 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002539 dev_err(dev, "%s: alloc_mem failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002540 rc = -ENOMEM;
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -05002541 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002542 goto out;
2543 }
2544
2545 cfg->init_state = INIT_STATE_NONE;
2546 cfg->dev = pdev;
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002547 cfg->num_fc_ports = NUM_FC_PORTS;
Matthew R. Ochs17ead262015-10-21 15:15:37 -05002548 cfg->cxl_fops = cxlflash_cxl_fops;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002549
2550 /*
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002551 * Promoted LUNs move to the top of the LUN table. The rest stay on
2552 * the bottom half. The bottom half grows from the end (index = 255),
2553 * whereas the top half grows from the beginning (index = 0).
2554 *
2555 * Initialize the last LUN index for all possible ports.
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002556 */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002557 cfg->promote_lun_index = 0;
2558
2559 for (k = 0; k < MAX_FC_PORTS; k++)
2560 cfg->last_lun_index[k] = CXLFLASH_NUM_VLUNS/2 - 1;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002561
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002562 cfg->dev_id = (struct pci_device_id *)dev_id;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002563
2564 init_waitqueue_head(&cfg->tmf_waitq);
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002565 init_waitqueue_head(&cfg->reset_waitq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002566
2567 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
2568 cfg->lr_state = LINK_RESET_INVALID;
2569 cfg->lr_port = -1;
Matthew R. Ochs0d731222015-10-21 15:16:24 -05002570 spin_lock_init(&cfg->tmf_slock);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002571 mutex_init(&cfg->ctx_tbl_list_mutex);
2572 mutex_init(&cfg->ctx_recovery_mutex);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002573 init_rwsem(&cfg->ioctl_rwsem);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002574 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
2575 INIT_LIST_HEAD(&cfg->lluns);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002576
2577 pci_set_drvdata(pdev, cfg);
2578
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002579 cfg->cxl_afu = cxl_pci_to_afu(pdev);
2580
2581 rc = init_pci(cfg);
2582 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002583 dev_err(dev, "%s: init_pci failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002584 goto out_remove;
2585 }
2586 cfg->init_state = INIT_STATE_PCI;
2587
2588 rc = init_afu(cfg);
2589 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002590 dev_err(dev, "%s: init_afu failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002591 goto out_remove;
2592 }
2593 cfg->init_state = INIT_STATE_AFU;
2594
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002595 rc = init_scsi(cfg);
2596 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002597 dev_err(dev, "%s: init_scsi failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002598 goto out_remove;
2599 }
2600 cfg->init_state = INIT_STATE_SCSI;
2601
2602out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002603 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002604 return rc;
2605
2606out_remove:
2607 cxlflash_remove(pdev);
2608 goto out;
2609}
2610
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002611/**
2612 * cxlflash_pci_error_detected() - called when a PCI error is detected
2613 * @pdev: PCI device struct.
2614 * @state: PCI channel state.
2615 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002616 * When an EEH occurs during an active reset, wait until the reset is
2617 * complete and then take action based upon the device state.
2618 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002619 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2620 */
2621static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
2622 pci_channel_state_t state)
2623{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002624 int rc = 0;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002625 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2626 struct device *dev = &cfg->dev->dev;
2627
2628 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
2629
2630 switch (state) {
2631 case pci_channel_io_frozen:
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002632 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2633 if (cfg->state == STATE_FAILTERM)
2634 return PCI_ERS_RESULT_DISCONNECT;
2635
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002636 cfg->state = STATE_RESET;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002637 scsi_block_requests(cfg->host);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002638 drain_ioctls(cfg);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002639 rc = cxlflash_mark_contexts_error(cfg);
2640 if (unlikely(rc))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002641 dev_err(dev, "%s: Failed to mark user contexts rc=%d\n",
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002642 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05002643 term_afu(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002644 return PCI_ERS_RESULT_NEED_RESET;
2645 case pci_channel_io_perm_failure:
2646 cfg->state = STATE_FAILTERM;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002647 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002648 scsi_unblock_requests(cfg->host);
2649 return PCI_ERS_RESULT_DISCONNECT;
2650 default:
2651 break;
2652 }
2653 return PCI_ERS_RESULT_NEED_RESET;
2654}
2655
2656/**
2657 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2658 * @pdev: PCI device struct.
2659 *
2660 * This routine is called by the pci error recovery code after the PCI
2661 * slot has been reset, just before we should resume normal operations.
2662 *
2663 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2664 */
2665static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
2666{
2667 int rc = 0;
2668 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2669 struct device *dev = &cfg->dev->dev;
2670
2671 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2672
2673 rc = init_afu(cfg);
2674 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002675 dev_err(dev, "%s: EEH recovery failed rc=%d\n", __func__, rc);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002676 return PCI_ERS_RESULT_DISCONNECT;
2677 }
2678
2679 return PCI_ERS_RESULT_RECOVERED;
2680}
2681
2682/**
2683 * cxlflash_pci_resume() - called when normal operation can resume
2684 * @pdev: PCI device struct
2685 */
2686static void cxlflash_pci_resume(struct pci_dev *pdev)
2687{
2688 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2689 struct device *dev = &cfg->dev->dev;
2690
2691 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2692
2693 cfg->state = STATE_NORMAL;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002694 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002695 scsi_unblock_requests(cfg->host);
2696}
2697
2698static const struct pci_error_handlers cxlflash_err_handler = {
2699 .error_detected = cxlflash_pci_error_detected,
2700 .slot_reset = cxlflash_pci_slot_reset,
2701 .resume = cxlflash_pci_resume,
2702};
2703
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002704/*
2705 * PCI device structure
2706 */
2707static struct pci_driver cxlflash_driver = {
2708 .name = CXLFLASH_NAME,
2709 .id_table = cxlflash_pci_table,
2710 .probe = cxlflash_probe,
2711 .remove = cxlflash_remove,
Uma Krishnanbabf9852016-09-02 15:39:16 -05002712 .shutdown = cxlflash_remove,
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002713 .err_handler = &cxlflash_err_handler,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002714};
2715
2716/**
2717 * init_cxlflash() - module entry point
2718 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002719 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002720 */
2721static int __init init_cxlflash(void)
2722{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002723 cxlflash_list_init();
2724
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002725 return pci_register_driver(&cxlflash_driver);
2726}
2727
2728/**
2729 * exit_cxlflash() - module exit point
2730 */
2731static void __exit exit_cxlflash(void)
2732{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002733 cxlflash_term_global_luns();
2734 cxlflash_free_errpage();
2735
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002736 pci_unregister_driver(&cxlflash_driver);
2737}
2738
2739module_init(init_cxlflash);
2740module_exit(exit_cxlflash);