blob: 5d068696eee45ce594444a783e1ce123223957f1 [file] [log] [blame]
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001/*
2 * CXL Flash Device Driver
3 *
4 * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
5 * Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
6 *
7 * Copyright (C) 2015 IBM Corporation
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15#include <linux/delay.h>
16#include <linux/list.h>
17#include <linux/module.h>
18#include <linux/pci.h>
19
20#include <asm/unaligned.h>
21
22#include <misc/cxl.h>
23
24#include <scsi/scsi_cmnd.h>
25#include <scsi/scsi_host.h>
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050026#include <uapi/scsi/cxlflash_ioctl.h>
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050027
28#include "main.h"
29#include "sislite.h"
30#include "common.h"
31
32MODULE_DESCRIPTION(CXLFLASH_ADAPTER_NAME);
33MODULE_AUTHOR("Manoj N. Kumar <manoj@linux.vnet.ibm.com>");
34MODULE_AUTHOR("Matthew R. Ochs <mrochs@linux.vnet.ibm.com>");
35MODULE_LICENSE("GPL");
36
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050037/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050038 * process_cmd_err() - command error handler
39 * @cmd: AFU command that experienced the error.
40 * @scp: SCSI command associated with the AFU command in error.
41 *
42 * Translates error bits from AFU command to SCSI command results.
43 */
44static void process_cmd_err(struct afu_cmd *cmd, struct scsi_cmnd *scp)
45{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060046 struct afu *afu = cmd->parent;
47 struct cxlflash_cfg *cfg = afu->parent;
48 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050049 struct sisl_ioarcb *ioarcb;
50 struct sisl_ioasa *ioasa;
Matthew R. Ochs83960122015-10-21 15:13:29 -050051 u32 resid;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050052
53 if (unlikely(!cmd))
54 return;
55
56 ioarcb = &(cmd->rcb);
57 ioasa = &(cmd->sa);
58
59 if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) {
Matthew R. Ochs83960122015-10-21 15:13:29 -050060 resid = ioasa->resid;
61 scsi_set_resid(scp, resid);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060062 dev_dbg(dev, "%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
63 __func__, cmd, scp, resid);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050064 }
65
66 if (ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060067 dev_dbg(dev, "%s: cmd underrun cmd = %p scp = %p\n",
68 __func__, cmd, scp);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050069 scp->result = (DID_ERROR << 16);
70 }
71
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060072 dev_dbg(dev, "%s: cmd failed afu_rc=%02x scsi_rc=%02x fc_rc=%02x "
73 "afu_extra=%02x scsi_extra=%02x fc_extra=%02x\n", __func__,
74 ioasa->rc.afu_rc, ioasa->rc.scsi_rc, ioasa->rc.fc_rc,
75 ioasa->afu_extra, ioasa->scsi_extra, ioasa->fc_extra);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050076
77 if (ioasa->rc.scsi_rc) {
78 /* We have a SCSI status */
79 if (ioasa->rc.flags & SISL_RC_FLAGS_SENSE_VALID) {
80 memcpy(scp->sense_buffer, ioasa->sense_data,
81 SISL_SENSE_DATA_LEN);
82 scp->result = ioasa->rc.scsi_rc;
83 } else
84 scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16);
85 }
86
87 /*
88 * We encountered an error. Set scp->result based on nature
89 * of error.
90 */
91 if (ioasa->rc.fc_rc) {
92 /* We have an FC status */
93 switch (ioasa->rc.fc_rc) {
94 case SISL_FC_RC_LINKDOWN:
95 scp->result = (DID_REQUEUE << 16);
96 break;
97 case SISL_FC_RC_RESID:
98 /* This indicates an FCP resid underrun */
99 if (!(ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN)) {
100 /* If the SISL_RC_FLAGS_OVERRUN flag was set,
101 * then we will handle this error else where.
102 * If not then we must handle it here.
Matthew R. Ochs83960122015-10-21 15:13:29 -0500103 * This is probably an AFU bug.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500104 */
105 scp->result = (DID_ERROR << 16);
106 }
107 break;
108 case SISL_FC_RC_RESIDERR:
109 /* Resid mismatch between adapter and device */
110 case SISL_FC_RC_TGTABORT:
111 case SISL_FC_RC_ABORTOK:
112 case SISL_FC_RC_ABORTFAIL:
113 case SISL_FC_RC_NOLOGI:
114 case SISL_FC_RC_ABORTPEND:
115 case SISL_FC_RC_WRABORTPEND:
116 case SISL_FC_RC_NOEXP:
117 case SISL_FC_RC_INUSE:
118 scp->result = (DID_ERROR << 16);
119 break;
120 }
121 }
122
123 if (ioasa->rc.afu_rc) {
124 /* We have an AFU error */
125 switch (ioasa->rc.afu_rc) {
126 case SISL_AFU_RC_NO_CHANNELS:
Matthew R. Ochs83960122015-10-21 15:13:29 -0500127 scp->result = (DID_NO_CONNECT << 16);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500128 break;
129 case SISL_AFU_RC_DATA_DMA_ERR:
130 switch (ioasa->afu_extra) {
131 case SISL_AFU_DMA_ERR_PAGE_IN:
132 /* Retry */
133 scp->result = (DID_IMM_RETRY << 16);
134 break;
135 case SISL_AFU_DMA_ERR_INVALID_EA:
136 default:
137 scp->result = (DID_ERROR << 16);
138 }
139 break;
140 case SISL_AFU_RC_OUT_OF_DATA_BUFS:
141 /* Retry */
142 scp->result = (DID_ALLOC_FAILURE << 16);
143 break;
144 default:
145 scp->result = (DID_ERROR << 16);
146 }
147 }
148}
149
150/**
151 * cmd_complete() - command completion handler
152 * @cmd: AFU command that has completed.
153 *
154 * Prepares and submits command that has either completed or timed out to
155 * the SCSI stack. Checks AFU command back into command pool for non-internal
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600156 * (cmd->scp populated) commands.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500157 */
158static void cmd_complete(struct afu_cmd *cmd)
159{
160 struct scsi_cmnd *scp;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500161 ulong lock_flags;
162 struct afu *afu = cmd->parent;
163 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600164 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500165 bool cmd_is_tmf;
166
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600167 if (cmd->scp) {
168 scp = cmd->scp;
Matthew R. Ochs83960122015-10-21 15:13:29 -0500169 if (unlikely(cmd->sa.ioasc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500170 process_cmd_err(cmd, scp);
171 else
172 scp->result = (DID_OK << 16);
173
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500174 cmd_is_tmf = cmd->cmd_tmf;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500175
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600176 dev_dbg_ratelimited(dev, "%s:scp=%p result=%08x ioasc=%08x\n",
177 __func__, scp, scp->result, cmd->sa.ioasc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500178
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500179 scp->scsi_done(scp);
180
181 if (cmd_is_tmf) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500182 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500183 cfg->tmf_active = false;
184 wake_up_all_locked(&cfg->tmf_waitq);
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500185 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500186 }
187 } else
188 complete(&cmd->cevent);
189}
190
191/**
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600192 * context_reset() - reset command owner context via specified register
Matthew R. Ochs15305512015-10-21 15:12:10 -0500193 * @cmd: AFU command that timed out.
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600194 * @reset_reg: MMIO register to perform reset.
Matthew R. Ochs15305512015-10-21 15:12:10 -0500195 */
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600196static void context_reset(struct afu_cmd *cmd, __be64 __iomem *reset_reg)
Matthew R. Ochs15305512015-10-21 15:12:10 -0500197{
198 int nretry = 0;
199 u64 rrin = 0x1;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500200 struct afu *afu = cmd->parent;
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600201 struct cxlflash_cfg *cfg = afu->parent;
202 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500203
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600204 dev_dbg(dev, "%s: cmd=%p\n", __func__, cmd);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500205
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600206 writeq_be(rrin, reset_reg);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500207 do {
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600208 rrin = readq_be(reset_reg);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500209 if (rrin != 0x1)
210 break;
211 /* Double delay each time */
Manoj N. Kumarea765432016-03-25 14:26:49 -0500212 udelay(1 << nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500213 } while (nretry++ < MC_ROOM_RETRY_CNT);
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600214
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600215 dev_dbg(dev, "%s: returning rrin=%016llx nretry=%d\n",
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600216 __func__, rrin, nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500217}
218
219/**
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600220 * context_reset_ioarrin() - reset command owner context via IOARRIN register
221 * @cmd: AFU command that timed out.
222 */
223static void context_reset_ioarrin(struct afu_cmd *cmd)
224{
225 struct afu *afu = cmd->parent;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500226 struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600227
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500228 context_reset(cmd, &hwq->host_map->ioarrin);
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600229}
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;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500238 struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600239
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500240 context_reset(cmd, &hwq->host_map->sq_ctx_reset);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600241}
242
243/**
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600244 * send_cmd_ioarrin() - sends an AFU command via IOARRIN register
Matthew R. Ochs15305512015-10-21 15:12:10 -0500245 * @afu: AFU associated with the host.
246 * @cmd: AFU command to send.
247 *
248 * Return:
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500249 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochs15305512015-10-21 15:12:10 -0500250 */
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600251static int send_cmd_ioarrin(struct afu *afu, struct afu_cmd *cmd)
Matthew R. Ochs15305512015-10-21 15:12:10 -0500252{
253 struct cxlflash_cfg *cfg = afu->parent;
254 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500255 struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500256 int rc = 0;
Uma Krishnan11f7b182016-11-28 18:41:45 -0600257 s64 room;
258 ulong lock_flags;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500259
260 /*
Uma Krishnan11f7b182016-11-28 18:41:45 -0600261 * To avoid the performance penalty of MMIO, spread the update of
262 * 'room' over multiple commands.
Matthew R. Ochs15305512015-10-21 15:12:10 -0500263 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500264 spin_lock_irqsave(&hwq->rrin_slock, lock_flags);
265 if (--hwq->room < 0) {
266 room = readq_be(&hwq->host_map->cmd_room);
Uma Krishnan11f7b182016-11-28 18:41:45 -0600267 if (room <= 0) {
268 dev_dbg_ratelimited(dev, "%s: no cmd_room to send "
269 "0x%02X, room=0x%016llX\n",
270 __func__, cmd->rcb.cdb[0], room);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500271 hwq->room = 0;
Uma Krishnan11f7b182016-11-28 18:41:45 -0600272 rc = SCSI_MLQUEUE_HOST_BUSY;
273 goto out;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500274 }
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500275 hwq->room = room - 1;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500276 }
277
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500278 writeq_be((u64)&cmd->rcb, &hwq->host_map->ioarrin);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500279out:
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500280 spin_unlock_irqrestore(&hwq->rrin_slock, lock_flags);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600281 dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx rc=%d\n", __func__,
282 cmd, cmd->rcb.data_len, cmd->rcb.data_ea, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500283 return rc;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500284}
285
286/**
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600287 * send_cmd_sq() - sends an AFU command via SQ ring
288 * @afu: AFU associated with the host.
289 * @cmd: AFU command to send.
290 *
291 * Return:
292 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
293 */
294static int send_cmd_sq(struct afu *afu, struct afu_cmd *cmd)
295{
296 struct cxlflash_cfg *cfg = afu->parent;
297 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500298 struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600299 int rc = 0;
300 int newval;
301 ulong lock_flags;
302
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500303 newval = atomic_dec_if_positive(&hwq->hsq_credits);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600304 if (newval <= 0) {
305 rc = SCSI_MLQUEUE_HOST_BUSY;
306 goto out;
307 }
308
309 cmd->rcb.ioasa = &cmd->sa;
310
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500311 spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600312
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500313 *hwq->hsq_curr = cmd->rcb;
314 if (hwq->hsq_curr < hwq->hsq_end)
315 hwq->hsq_curr++;
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600316 else
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500317 hwq->hsq_curr = hwq->hsq_start;
318 writeq_be((u64)hwq->hsq_curr, &hwq->host_map->sq_tail);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600319
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500320 spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600321out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600322 dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx ioasa=%p rc=%d curr=%p "
323 "head=%016llx tail=%016llx\n", __func__, cmd, cmd->rcb.data_len,
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500324 cmd->rcb.data_ea, cmd->rcb.ioasa, rc, hwq->hsq_curr,
325 readq_be(&hwq->host_map->sq_head),
326 readq_be(&hwq->host_map->sq_tail));
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600327 return rc;
328}
329
330/**
Matthew R. Ochs15305512015-10-21 15:12:10 -0500331 * wait_resp() - polls for a response or timeout to a sent AFU command
332 * @afu: AFU associated with the host.
333 * @cmd: AFU command that was sent.
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600334 *
335 * Return:
336 * 0 on success, -1 on timeout/error
Matthew R. Ochs15305512015-10-21 15:12:10 -0500337 */
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600338static int wait_resp(struct afu *afu, struct afu_cmd *cmd)
Matthew R. Ochs15305512015-10-21 15:12:10 -0500339{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600340 struct cxlflash_cfg *cfg = afu->parent;
341 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600342 int rc = 0;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500343 ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
344
345 timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600346 if (!timeout) {
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600347 afu->context_reset(cmd);
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600348 rc = -1;
349 }
Matthew R. Ochs15305512015-10-21 15:12:10 -0500350
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600351 if (unlikely(cmd->sa.ioasc != 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600352 dev_err(dev, "%s: cmd %02x failed, ioasc=%08x\n",
353 __func__, cmd->rcb.cdb[0], cmd->sa.ioasc);
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600354 rc = -1;
355 }
356
357 return rc;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500358}
359
360/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500361 * send_tmf() - sends a Task Management Function (TMF)
362 * @afu: AFU to checkout from.
363 * @scp: SCSI command from stack.
364 * @tmfcmd: TMF command to send.
365 *
366 * Return:
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500367 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500368 */
369static int send_tmf(struct afu *afu, struct scsi_cmnd *scp, u64 tmfcmd)
370{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600371 struct cxlflash_cfg *cfg = shost_priv(scp->device->host);
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600372 struct afu_cmd *cmd = sc_to_afucz(scp);
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500373 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500374 struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500375 ulong lock_flags;
376 int rc = 0;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500377 ulong to;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500378
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500379 /* When Task Management Function is active do not send another */
380 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500381 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500382 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
383 !cfg->tmf_active,
384 cfg->tmf_slock);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500385 cfg->tmf_active = true;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500386 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500387
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600388 cmd->scp = scp;
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600389 cmd->parent = afu;
390 cmd->cmd_tmf = true;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500391 cmd->hwq_index = hwq->index;
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600392
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500393 cmd->rcb.ctx_id = hwq->ctx_hndl;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600394 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -0500395 cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500396 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500397 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600398 SISL_REQ_FLAGS_SUP_UNDERRUN |
399 SISL_REQ_FLAGS_TMF_CMD);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500400 memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
401
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600402 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500403 if (unlikely(rc)) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500404 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500405 cfg->tmf_active = false;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500406 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500407 goto out;
408 }
409
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500410 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
411 to = msecs_to_jiffies(5000);
412 to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
413 !cfg->tmf_active,
414 cfg->tmf_slock,
415 to);
416 if (!to) {
417 cfg->tmf_active = false;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600418 dev_err(dev, "%s: TMF timed out\n", __func__);
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500419 rc = -1;
420 }
421 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500422out:
423 return rc;
424}
425
426/**
427 * cxlflash_driver_info() - information handler for this host driver
428 * @host: SCSI host associated with device.
429 *
430 * Return: A string describing the device.
431 */
432static const char *cxlflash_driver_info(struct Scsi_Host *host)
433{
434 return CXLFLASH_ADAPTER_NAME;
435}
436
437/**
438 * cxlflash_queuecommand() - sends a mid-layer request
439 * @host: SCSI host associated with device.
440 * @scp: SCSI command to send.
441 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500442 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500443 */
444static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
445{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600446 struct cxlflash_cfg *cfg = shost_priv(host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500447 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500448 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600449 struct afu_cmd *cmd = sc_to_afucz(scp);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600450 struct scatterlist *sg = scsi_sglist(scp);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500451 struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600452 u16 req_flags = SISL_REQ_FLAGS_SUP_UNDERRUN;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500453 ulong lock_flags;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500454 int rc = 0;
455
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500456 dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600457 "cdb=(%08x-%08x-%08x-%08x)\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500458 __func__, scp, host->host_no, scp->device->channel,
459 scp->device->id, scp->device->lun,
460 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
461 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
462 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
463 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500464
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500465 /*
466 * If a Task Management Function is active, wait for it to complete
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500467 * before continuing with regular commands.
468 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500469 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500470 if (cfg->tmf_active) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500471 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500472 rc = SCSI_MLQUEUE_HOST_BUSY;
473 goto out;
474 }
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500475 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500476
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500477 switch (cfg->state) {
Matthew R. Ochs323e3342017-04-12 14:14:51 -0500478 case STATE_PROBING:
479 case STATE_PROBED:
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500480 case STATE_RESET:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600481 dev_dbg_ratelimited(dev, "%s: device is in reset\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500482 rc = SCSI_MLQUEUE_HOST_BUSY;
483 goto out;
484 case STATE_FAILTERM:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600485 dev_dbg_ratelimited(dev, "%s: device has failed\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500486 scp->result = (DID_NO_CONNECT << 16);
487 scp->scsi_done(scp);
488 rc = 0;
489 goto out;
490 default:
491 break;
492 }
493
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600494 if (likely(sg)) {
Matthew R. Ochs50b787f2017-04-12 14:15:02 -0500495 cmd->rcb.data_len = sg->length;
496 cmd->rcb.data_ea = (uintptr_t)sg_virt(sg);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600497 }
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;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500501 cmd->hwq_index = hwq->index;
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600502
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500503 cmd->rcb.ctx_id = hwq->ctx_hndl;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600504 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -0500505 cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500506 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
507
508 if (scp->sc_data_direction == DMA_TO_DEVICE)
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600509 req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500510
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600511 cmd->rcb.req_flags = req_flags;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500512 memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
513
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600514 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500515out:
516 return rc;
517}
518
519/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500520 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500521 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500522 */
523static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
524{
525 struct pci_dev *pdev = cfg->dev;
526
527 if (pci_channel_offline(pdev))
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500528 wait_event_timeout(cfg->reset_waitq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500529 !pci_channel_offline(pdev),
530 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
531}
532
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500533/**
534 * free_mem() - free memory associated with the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500535 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500536 */
537static void free_mem(struct cxlflash_cfg *cfg)
538{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500539 struct afu *afu = cfg->afu;
540
541 if (cfg->afu) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500542 free_pages((ulong)afu, get_order(sizeof(struct afu)));
543 cfg->afu = NULL;
544 }
545}
546
547/**
548 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500549 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500550 *
551 * Safe to call with AFU in a partially allocated/initialized state.
Manoj Kumaree91e332015-12-14 15:07:02 -0600552 *
Uma Krishnan0df5bef2017-01-11 19:20:03 -0600553 * Cancels scheduled worker threads, waits for any active internal AFU
Matthew R. Ochscba06e62017-04-12 14:13:20 -0500554 * commands to timeout, disables IRQ polling and then unmaps the MMIO space.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500555 */
556static void stop_afu(struct cxlflash_cfg *cfg)
557{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500558 struct afu *afu = cfg->afu;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500559 struct hwq *hwq;
560 int i;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500561
Uma Krishnan0df5bef2017-01-11 19:20:03 -0600562 cancel_work_sync(&cfg->work_q);
563
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500564 if (likely(afu)) {
Matthew R. Ochsde012832016-11-28 18:42:33 -0600565 while (atomic_read(&afu->cmds_active))
566 ssleep(1);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500567
568 if (afu_is_irqpoll_enabled(afu)) {
569 for (i = 0; i < CXLFLASH_NUM_HWQS; i++) {
570 hwq = get_hwq(afu, i);
571
572 irq_poll_disable(&hwq->irqpoll);
573 }
574 }
575
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500576 if (likely(afu->afu_map)) {
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500577 cxl_psa_unmap((void __iomem *)afu->afu_map);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500578 afu->afu_map = NULL;
579 }
580 }
581}
582
583/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500584 * term_intr() - disables all AFU interrupts
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500585 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500586 * @level: Depth of allocation, where to begin waterfall tear down.
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500587 * @index: Index of the hardware queue.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500588 *
589 * Safe to call with AFU/MC in partially allocated/initialized state.
590 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500591static void term_intr(struct cxlflash_cfg *cfg, enum undo_level level,
592 u32 index)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500593{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500594 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500595 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500596 struct hwq *hwq;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500597
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500598 if (!afu) {
599 dev_err(dev, "%s: returning with NULL afu\n", __func__);
600 return;
601 }
602
603 hwq = get_hwq(afu, index);
604
605 if (!hwq->ctx) {
606 dev_err(dev, "%s: returning with NULL MC\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500607 return;
608 }
609
610 switch (level) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500611 case UNMAP_THREE:
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500612 /* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
613 if (index == PRIMARY_HWQ)
614 cxl_unmap_afu_irq(hwq->ctx, 3, hwq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500615 case UNMAP_TWO:
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500616 cxl_unmap_afu_irq(hwq->ctx, 2, hwq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500617 case UNMAP_ONE:
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500618 cxl_unmap_afu_irq(hwq->ctx, 1, hwq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500619 case FREE_IRQ:
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500620 cxl_free_afu_irqs(hwq->ctx);
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500621 /* fall through */
622 case UNDO_NOOP:
623 /* No action required */
624 break;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500625 }
626}
627
628/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500629 * term_mc() - terminates the master context
630 * @cfg: Internal structure associated with the host.
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500631 * @index: Index of the hardware queue.
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500632 *
633 * Safe to call with AFU/MC in partially allocated/initialized state.
634 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500635static void term_mc(struct cxlflash_cfg *cfg, u32 index)
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500636{
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500637 struct afu *afu = cfg->afu;
638 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500639 struct hwq *hwq;
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500640
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500641 if (!afu) {
642 dev_err(dev, "%s: returning with NULL afu\n", __func__);
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500643 return;
644 }
645
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500646 hwq = get_hwq(afu, index);
647
648 if (!hwq->ctx) {
649 dev_err(dev, "%s: returning with NULL MC\n", __func__);
650 return;
651 }
652
653 WARN_ON(cxl_stop_context(hwq->ctx));
654 if (index != PRIMARY_HWQ)
655 WARN_ON(cxl_release_context(hwq->ctx));
656 hwq->ctx = NULL;
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500657}
658
659/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500660 * term_afu() - terminates the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500661 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500662 *
663 * Safe to call with AFU/MC in partially allocated/initialized state.
664 */
665static void term_afu(struct cxlflash_cfg *cfg)
666{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600667 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500668 int k;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600669
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500670 /*
671 * Tear down is carefully orchestrated to ensure
672 * no interrupts can come in when the problem state
673 * area is unmapped.
674 *
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500675 * 1) Disable all AFU interrupts for each master
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500676 * 2) Unmap the problem state area
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500677 * 3) Stop each master context
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500678 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500679 for (k = CXLFLASH_NUM_HWQS - 1; k >= 0; k--)
680 term_intr(cfg, UNMAP_THREE, k);
681
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500682 if (cfg->afu)
683 stop_afu(cfg);
684
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500685 for (k = CXLFLASH_NUM_HWQS - 1; k >= 0; k--)
686 term_mc(cfg, k);
Uma Krishnan6ded8b32016-03-04 15:55:15 -0600687
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600688 dev_dbg(dev, "%s: returning\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500689}
690
691/**
Uma Krishnan704c4b02016-06-15 18:49:57 -0500692 * notify_shutdown() - notifies device of pending shutdown
693 * @cfg: Internal structure associated with the host.
694 * @wait: Whether to wait for shutdown processing to complete.
695 *
696 * This function will notify the AFU that the adapter is being shutdown
697 * and will wait for shutdown processing to complete if wait is true.
698 * This notification should flush pending I/Os to the device and halt
699 * further I/Os until the next AFU reset is issued and device restarted.
700 */
701static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
702{
703 struct afu *afu = cfg->afu;
704 struct device *dev = &cfg->dev->dev;
Uma Krishnan704c4b02016-06-15 18:49:57 -0500705 struct dev_dependent_vals *ddv;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500706 __be64 __iomem *fc_port_regs;
Uma Krishnan704c4b02016-06-15 18:49:57 -0500707 u64 reg, status;
708 int i, retry_cnt = 0;
709
710 ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
711 if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
712 return;
713
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500714 if (!afu || !afu->afu_map) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600715 dev_dbg(dev, "%s: Problem state area not mapped\n", __func__);
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500716 return;
717 }
718
Uma Krishnan704c4b02016-06-15 18:49:57 -0500719 /* Notify AFU */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -0500720 for (i = 0; i < cfg->num_fc_ports; i++) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500721 fc_port_regs = get_fc_port_regs(cfg, i);
722
723 reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
Uma Krishnan704c4b02016-06-15 18:49:57 -0500724 reg |= SISL_FC_SHUTDOWN_NORMAL;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500725 writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
Uma Krishnan704c4b02016-06-15 18:49:57 -0500726 }
727
728 if (!wait)
729 return;
730
731 /* Wait up to 1.5 seconds for shutdown processing to complete */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -0500732 for (i = 0; i < cfg->num_fc_ports; i++) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500733 fc_port_regs = get_fc_port_regs(cfg, i);
Uma Krishnan704c4b02016-06-15 18:49:57 -0500734 retry_cnt = 0;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500735
Uma Krishnan704c4b02016-06-15 18:49:57 -0500736 while (true) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500737 status = readq_be(&fc_port_regs[FC_STATUS / 8]);
Uma Krishnan704c4b02016-06-15 18:49:57 -0500738 if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
739 break;
740 if (++retry_cnt >= MC_RETRY_CNT) {
741 dev_dbg(dev, "%s: port %d shutdown processing "
742 "not yet completed\n", __func__, i);
743 break;
744 }
745 msleep(100 * retry_cnt);
746 }
747 }
748}
749
750/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500751 * cxlflash_remove() - PCI entry point to tear down host
752 * @pdev: PCI device associated with the host.
753 *
Matthew R. Ochs323e3342017-04-12 14:14:51 -0500754 * Safe to use as a cleanup in partially allocated/initialized state. Note that
755 * the reset_waitq is flushed as part of the stop/termination of user contexts.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500756 */
757static void cxlflash_remove(struct pci_dev *pdev)
758{
759 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600760 struct device *dev = &pdev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500761 ulong lock_flags;
762
Uma Krishnanbabf9852016-09-02 15:39:16 -0500763 if (!pci_is_enabled(pdev)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600764 dev_dbg(dev, "%s: Device is disabled\n", __func__);
Uma Krishnanbabf9852016-09-02 15:39:16 -0500765 return;
766 }
767
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500768 /* If a Task Management Function is active, wait for it to complete
769 * before continuing with remove.
770 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500771 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500772 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500773 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
774 !cfg->tmf_active,
775 cfg->tmf_slock);
776 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500777
Uma Krishnan704c4b02016-06-15 18:49:57 -0500778 /* Notify AFU and wait for shutdown processing to complete */
779 notify_shutdown(cfg, true);
780
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500781 cfg->state = STATE_FAILTERM;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500782 cxlflash_stop_term_user_contexts(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500783
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500784 switch (cfg->init_state) {
785 case INIT_STATE_SCSI:
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500786 cxlflash_term_local_luns(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500787 scsi_remove_host(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500788 case INIT_STATE_AFU:
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600789 term_afu(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500790 case INIT_STATE_PCI:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500791 pci_disable_device(pdev);
792 case INIT_STATE_NONE:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500793 free_mem(cfg);
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -0500794 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500795 break;
796 }
797
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600798 dev_dbg(dev, "%s: returning\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500799}
800
801/**
802 * alloc_mem() - allocates the AFU and its command pool
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500803 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500804 *
805 * A partially allocated state remains on failure.
806 *
807 * Return:
808 * 0 on success
809 * -ENOMEM on failure to allocate memory
810 */
811static int alloc_mem(struct cxlflash_cfg *cfg)
812{
813 int rc = 0;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500814 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500815
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600816 /* AFU is ~28k, i.e. only one 64k page or up to seven 4k pages */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500817 cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
818 get_order(sizeof(struct afu)));
819 if (unlikely(!cfg->afu)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500820 dev_err(dev, "%s: cannot get %d free pages\n",
821 __func__, get_order(sizeof(struct afu)));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500822 rc = -ENOMEM;
823 goto out;
824 }
825 cfg->afu->parent = cfg;
826 cfg->afu->afu_map = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500827out:
828 return rc;
829}
830
831/**
832 * init_pci() - initializes the host as a PCI device
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500833 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500834 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500835 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500836 */
837static int init_pci(struct cxlflash_cfg *cfg)
838{
839 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600840 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500841 int rc = 0;
842
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500843 rc = pci_enable_device(pdev);
844 if (rc || pci_channel_offline(pdev)) {
845 if (pci_channel_offline(pdev)) {
846 cxlflash_wait_for_pci_err_recovery(cfg);
847 rc = pci_enable_device(pdev);
848 }
849
850 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600851 dev_err(dev, "%s: Cannot enable adapter\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500852 cxlflash_wait_for_pci_err_recovery(cfg);
Manoj N. Kumar961487e2016-03-04 15:55:14 -0600853 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500854 }
855 }
856
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500857out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600858 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500859 return rc;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500860}
861
862/**
863 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500864 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500865 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500866 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500867 */
868static int init_scsi(struct cxlflash_cfg *cfg)
869{
870 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600871 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500872 int rc = 0;
873
874 rc = scsi_add_host(cfg->host, &pdev->dev);
875 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600876 dev_err(dev, "%s: scsi_add_host failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500877 goto out;
878 }
879
880 scsi_scan_host(cfg->host);
881
882out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600883 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500884 return rc;
885}
886
887/**
888 * set_port_online() - transitions the specified host FC port to online state
889 * @fc_regs: Top of MMIO region defined for specified port.
890 *
891 * The provided MMIO region must be mapped prior to call. Online state means
892 * that the FC link layer has synced, completed the handshaking process, and
893 * is ready for login to start.
894 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500895static void set_port_online(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500896{
897 u64 cmdcfg;
898
899 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
900 cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
901 cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
902 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
903}
904
905/**
906 * set_port_offline() - transitions the specified host FC port to offline state
907 * @fc_regs: Top of MMIO region defined for specified port.
908 *
909 * The provided MMIO region must be mapped prior to call.
910 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500911static void set_port_offline(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500912{
913 u64 cmdcfg;
914
915 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
916 cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
917 cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
918 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
919}
920
921/**
922 * wait_port_online() - waits for the specified host FC port come online
923 * @fc_regs: Top of MMIO region defined for specified port.
924 * @delay_us: Number of microseconds to delay between reading port status.
925 * @nretry: Number of cycles to retry reading port status.
926 *
927 * The provided MMIO region must be mapped prior to call. This will timeout
928 * when the cable is not plugged in.
929 *
930 * Return:
931 * TRUE (1) when the specified port is online
932 * FALSE (0) when the specified port fails to come online after timeout
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500933 */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600934static bool wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500935{
936 u64 status;
937
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600938 WARN_ON(delay_us < 1000);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500939
940 do {
941 msleep(delay_us / 1000);
942 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -0500943 if (status == U64_MAX)
944 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500945 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
946 nretry--);
947
948 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
949}
950
951/**
952 * wait_port_offline() - waits for the specified host FC port go offline
953 * @fc_regs: Top of MMIO region defined for specified port.
954 * @delay_us: Number of microseconds to delay between reading port status.
955 * @nretry: Number of cycles to retry reading port status.
956 *
957 * The provided MMIO region must be mapped prior to call.
958 *
959 * Return:
960 * TRUE (1) when the specified port is offline
961 * FALSE (0) when the specified port fails to go offline after timeout
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500962 */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600963static bool wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500964{
965 u64 status;
966
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600967 WARN_ON(delay_us < 1000);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500968
969 do {
970 msleep(delay_us / 1000);
971 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -0500972 if (status == U64_MAX)
973 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500974 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
975 nretry--);
976
977 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
978}
979
980/**
981 * afu_set_wwpn() - configures the WWPN for the specified host FC port
982 * @afu: AFU associated with the host that owns the specified FC port.
983 * @port: Port number being configured.
984 * @fc_regs: Top of MMIO region defined for specified port.
985 * @wwpn: The world-wide-port-number previously discovered for port.
986 *
987 * The provided MMIO region must be mapped prior to call. As part of the
988 * sequence to configure the WWPN, the port is toggled offline and then back
989 * online. This toggling action can cause this routine to delay up to a few
990 * seconds. When configured to use the internal LUN feature of the AFU, a
991 * failure to come online is overridden.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500992 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -0500993static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
994 u64 wwpn)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500995{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600996 struct cxlflash_cfg *cfg = afu->parent;
997 struct device *dev = &cfg->dev->dev;
998
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500999 set_port_offline(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001000 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1001 FC_PORT_STATUS_RETRY_CNT)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001002 dev_dbg(dev, "%s: wait on port %d to go offline timed out\n",
1003 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001004 }
1005
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001006 writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
Matthew R. Ochs964497b2015-10-21 15:13:54 -05001007
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001008 set_port_online(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001009 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1010 FC_PORT_STATUS_RETRY_CNT)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001011 dev_dbg(dev, "%s: wait on port %d to go online timed out\n",
1012 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001013 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001014}
1015
1016/**
1017 * afu_link_reset() - resets the specified host FC port
1018 * @afu: AFU associated with the host that owns the specified FC port.
1019 * @port: Port number being configured.
1020 * @fc_regs: Top of MMIO region defined for specified port.
1021 *
1022 * The provided MMIO region must be mapped prior to call. The sequence to
1023 * reset the port involves toggling it offline and then back online. This
1024 * action can cause this routine to delay up to a few seconds. An effort
1025 * is made to maintain link with the device by switching to host to use
1026 * the alternate port exclusively while the reset takes place.
1027 * failure to come online is overridden.
1028 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001029static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001030{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001031 struct cxlflash_cfg *cfg = afu->parent;
1032 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001033 u64 port_sel;
1034
1035 /* first switch the AFU to the other links, if any */
1036 port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
Dan Carpenter4da74db2015-08-18 11:57:43 +03001037 port_sel &= ~(1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001038 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1039 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1040
1041 set_port_offline(fc_regs);
1042 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1043 FC_PORT_STATUS_RETRY_CNT))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001044 dev_err(dev, "%s: wait on port %d to go offline timed out\n",
1045 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001046
1047 set_port_online(fc_regs);
1048 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1049 FC_PORT_STATUS_RETRY_CNT))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001050 dev_err(dev, "%s: wait on port %d to go online timed out\n",
1051 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001052
1053 /* switch back to include this port */
Dan Carpenter4da74db2015-08-18 11:57:43 +03001054 port_sel |= (1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001055 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1056 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1057
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001058 dev_dbg(dev, "%s: returning port_sel=%016llx\n", __func__, port_sel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001059}
1060
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001061/**
1062 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1063 * @afu: AFU associated with the host.
1064 */
1065static void afu_err_intr_init(struct afu *afu)
1066{
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001067 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001068 __be64 __iomem *fc_port_regs;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001069 int i;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001070 struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001071 u64 reg;
1072
1073 /* global async interrupts: AFU clears afu_ctrl on context exit
1074 * if async interrupts were sent to that context. This prevents
1075 * the AFU form sending further async interrupts when
1076 * there is
1077 * nobody to receive them.
1078 */
1079
1080 /* mask all */
1081 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001082 /* set LISN# to send and point to primary master context */
1083 reg = ((u64) (((hwq->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001084
1085 if (afu->internal_lun)
1086 reg |= 1; /* Bit 63 indicates local lun */
1087 writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1088 /* clear all */
1089 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1090 /* unmask bits that are of interest */
1091 /* note: afu can send an interrupt after this step */
1092 writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1093 /* clear again in case a bit came on after previous clear but before */
1094 /* unmask */
1095 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1096
1097 /* Clear/Set internal lun bits */
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001098 fc_port_regs = get_fc_port_regs(cfg, 0);
1099 reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001100 reg &= SISL_FC_INTERNAL_MASK;
1101 if (afu->internal_lun)
1102 reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001103 writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001104
1105 /* now clear FC errors */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001106 for (i = 0; i < cfg->num_fc_ports; i++) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001107 fc_port_regs = get_fc_port_regs(cfg, i);
1108
1109 writeq_be(0xFFFFFFFFU, &fc_port_regs[FC_ERROR / 8]);
1110 writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001111 }
1112
1113 /* sync interrupts for master's IOARRIN write */
1114 /* note that unlike asyncs, there can be no pending sync interrupts */
1115 /* at this time (this is a fresh context and master has not written */
1116 /* IOARRIN yet), so there is nothing to clear. */
1117
1118 /* set LISN#, it is always sent to the context that wrote IOARRIN */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001119 for (i = 0; i < CXLFLASH_NUM_HWQS; i++) {
1120 hwq = get_hwq(afu, i);
1121
1122 writeq_be(SISL_MSI_SYNC_ERROR, &hwq->host_map->ctx_ctrl);
1123 writeq_be(SISL_ISTATUS_MASK, &hwq->host_map->intr_mask);
1124 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001125}
1126
1127/**
1128 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1129 * @irq: Interrupt number.
1130 * @data: Private data provided at interrupt registration, the AFU.
1131 *
1132 * Return: Always return IRQ_HANDLED.
1133 */
1134static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1135{
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001136 struct hwq *hwq = (struct hwq *)data;
1137 struct cxlflash_cfg *cfg = hwq->afu->parent;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001138 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001139 u64 reg;
1140 u64 reg_unmasked;
1141
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001142 reg = readq_be(&hwq->host_map->intr_status);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001143 reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1144
1145 if (reg_unmasked == 0UL) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001146 dev_err(dev, "%s: spurious interrupt, intr_status=%016llx\n",
1147 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001148 goto cxlflash_sync_err_irq_exit;
1149 }
1150
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001151 dev_err(dev, "%s: unexpected interrupt, intr_status=%016llx\n",
1152 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001153
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001154 writeq_be(reg_unmasked, &hwq->host_map->intr_clear);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001155
1156cxlflash_sync_err_irq_exit:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001157 return IRQ_HANDLED;
1158}
1159
1160/**
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001161 * process_hrrq() - process the read-response queue
1162 * @afu: AFU associated with the host.
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001163 * @doneq: Queue of commands harvested from the RRQ.
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001164 * @budget: Threshold of RRQ entries to process.
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001165 *
1166 * This routine must be called holding the disabled RRQ spin lock.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001167 *
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001168 * Return: The number of entries processed.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001169 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001170static int process_hrrq(struct hwq *hwq, struct list_head *doneq, int budget)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001171{
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001172 struct afu *afu = hwq->afu;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001173 struct afu_cmd *cmd;
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001174 struct sisl_ioasa *ioasa;
1175 struct sisl_ioarcb *ioarcb;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001176 bool toggle = hwq->toggle;
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001177 int num_hrrq = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001178 u64 entry,
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001179 *hrrq_start = hwq->hrrq_start,
1180 *hrrq_end = hwq->hrrq_end,
1181 *hrrq_curr = hwq->hrrq_curr;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001182
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001183 /* Process ready RRQ entries up to the specified budget (if any) */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001184 while (true) {
1185 entry = *hrrq_curr;
1186
1187 if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1188 break;
1189
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001190 entry &= ~SISL_RESP_HANDLE_T_BIT;
1191
1192 if (afu_is_sq_cmd_mode(afu)) {
1193 ioasa = (struct sisl_ioasa *)entry;
1194 cmd = container_of(ioasa, struct afu_cmd, sa);
1195 } else {
1196 ioarcb = (struct sisl_ioarcb *)entry;
1197 cmd = container_of(ioarcb, struct afu_cmd, rcb);
1198 }
1199
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001200 list_add_tail(&cmd->queue, doneq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001201
1202 /* Advance to next entry or wrap and flip the toggle bit */
1203 if (hrrq_curr < hrrq_end)
1204 hrrq_curr++;
1205 else {
1206 hrrq_curr = hrrq_start;
1207 toggle ^= SISL_RESP_HANDLE_T_BIT;
1208 }
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001209
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001210 atomic_inc(&hwq->hsq_credits);
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001211 num_hrrq++;
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001212
1213 if (budget > 0 && num_hrrq >= budget)
1214 break;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001215 }
1216
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001217 hwq->hrrq_curr = hrrq_curr;
1218 hwq->toggle = toggle;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001219
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001220 return num_hrrq;
1221}
1222
1223/**
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001224 * process_cmd_doneq() - process a queue of harvested RRQ commands
1225 * @doneq: Queue of completed commands.
1226 *
1227 * Note that upon return the queue can no longer be trusted.
1228 */
1229static void process_cmd_doneq(struct list_head *doneq)
1230{
1231 struct afu_cmd *cmd, *tmp;
1232
1233 WARN_ON(list_empty(doneq));
1234
1235 list_for_each_entry_safe(cmd, tmp, doneq, queue)
1236 cmd_complete(cmd);
1237}
1238
1239/**
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001240 * cxlflash_irqpoll() - process a queue of harvested RRQ commands
1241 * @irqpoll: IRQ poll structure associated with queue to poll.
1242 * @budget: Threshold of RRQ entries to process per poll.
1243 *
1244 * Return: The number of entries processed.
1245 */
1246static int cxlflash_irqpoll(struct irq_poll *irqpoll, int budget)
1247{
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001248 struct hwq *hwq = container_of(irqpoll, struct hwq, irqpoll);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001249 unsigned long hrrq_flags;
1250 LIST_HEAD(doneq);
1251 int num_entries = 0;
1252
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001253 spin_lock_irqsave(&hwq->hrrq_slock, hrrq_flags);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001254
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001255 num_entries = process_hrrq(hwq, &doneq, budget);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001256 if (num_entries < budget)
1257 irq_poll_complete(irqpoll);
1258
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001259 spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001260
1261 process_cmd_doneq(&doneq);
1262 return num_entries;
1263}
1264
1265/**
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001266 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1267 * @irq: Interrupt number.
1268 * @data: Private data provided at interrupt registration, the AFU.
1269 *
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001270 * Return: IRQ_HANDLED or IRQ_NONE when no ready entries found.
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001271 */
1272static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
1273{
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001274 struct hwq *hwq = (struct hwq *)data;
1275 struct afu *afu = hwq->afu;
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001276 unsigned long hrrq_flags;
1277 LIST_HEAD(doneq);
1278 int num_entries = 0;
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001279
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001280 spin_lock_irqsave(&hwq->hrrq_slock, hrrq_flags);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001281
1282 if (afu_is_irqpoll_enabled(afu)) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001283 irq_poll_sched(&hwq->irqpoll);
1284 spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001285 return IRQ_HANDLED;
1286 }
1287
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001288 num_entries = process_hrrq(hwq, &doneq, -1);
1289 spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001290
1291 if (num_entries == 0)
1292 return IRQ_NONE;
1293
1294 process_cmd_doneq(&doneq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001295 return IRQ_HANDLED;
1296}
1297
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001298/*
1299 * Asynchronous interrupt information table
1300 *
1301 * NOTE:
1302 * - Order matters here as this array is indexed by bit position.
1303 *
1304 * - The checkpatch script considers the BUILD_SISL_ASTATUS_FC_PORT macro
1305 * as complex and complains due to a lack of parentheses/braces.
1306 */
1307#define ASTATUS_FC(_a, _b, _c, _d) \
1308 { SISL_ASTATUS_FC##_a##_##_b, _c, _a, (_d) }
1309
1310#define BUILD_SISL_ASTATUS_FC_PORT(_a) \
1311 ASTATUS_FC(_a, LINK_UP, "link up", 0), \
1312 ASTATUS_FC(_a, LINK_DN, "link down", 0), \
1313 ASTATUS_FC(_a, LOGI_S, "login succeeded", SCAN_HOST), \
1314 ASTATUS_FC(_a, LOGI_F, "login failed", CLR_FC_ERROR), \
1315 ASTATUS_FC(_a, LOGI_R, "login timed out, retrying", LINK_RESET), \
1316 ASTATUS_FC(_a, CRC_T, "CRC threshold exceeded", LINK_RESET), \
1317 ASTATUS_FC(_a, LOGO, "target initiated LOGO", 0), \
1318 ASTATUS_FC(_a, OTHER, "other error", CLR_FC_ERROR | LINK_RESET)
1319
1320static const struct asyc_intr_info ainfo[] = {
1321 BUILD_SISL_ASTATUS_FC_PORT(1),
1322 BUILD_SISL_ASTATUS_FC_PORT(0),
1323 BUILD_SISL_ASTATUS_FC_PORT(3),
1324 BUILD_SISL_ASTATUS_FC_PORT(2)
1325};
1326
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001327/**
1328 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1329 * @irq: Interrupt number.
1330 * @data: Private data provided at interrupt registration, the AFU.
1331 *
1332 * Return: Always return IRQ_HANDLED.
1333 */
1334static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1335{
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001336 struct hwq *hwq = (struct hwq *)data;
1337 struct afu *afu = hwq->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001338 struct cxlflash_cfg *cfg = afu->parent;
1339 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001340 const struct asyc_intr_info *info;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001341 struct sisl_global_map __iomem *global = &afu->afu_map->global;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001342 __be64 __iomem *fc_port_regs;
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001343 u64 reg_unmasked;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001344 u64 reg;
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001345 u64 bit;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001346 u8 port;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001347
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001348 reg = readq_be(&global->regs.aintr_status);
1349 reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1350
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001351 if (unlikely(reg_unmasked == 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001352 dev_err(dev, "%s: spurious interrupt, aintr_status=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001353 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001354 goto out;
1355 }
1356
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001357 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001358 writeq_be(reg_unmasked, &global->regs.aintr_clear);
1359
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001360 /* Check each bit that is on */
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001361 for_each_set_bit(bit, (ulong *)&reg_unmasked, BITS_PER_LONG) {
1362 if (unlikely(bit >= ARRAY_SIZE(ainfo))) {
1363 WARN_ON_ONCE(1);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001364 continue;
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001365 }
1366
1367 info = &ainfo[bit];
1368 if (unlikely(info->status != 1ULL << bit)) {
1369 WARN_ON_ONCE(1);
1370 continue;
1371 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001372
1373 port = info->port;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001374 fc_port_regs = get_fc_port_regs(cfg, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001375
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001376 dev_err(dev, "%s: FC Port %d -> %s, fc_status=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001377 __func__, port, info->desc,
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001378 readq_be(&fc_port_regs[FC_STATUS / 8]));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001379
1380 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001381 * Do link reset first, some OTHER errors will set FC_ERROR
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001382 * again if cleared before or w/o a reset
1383 */
1384 if (info->action & LINK_RESET) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001385 dev_err(dev, "%s: FC Port %d: resetting link\n",
1386 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001387 cfg->lr_state = LINK_RESET_REQUIRED;
1388 cfg->lr_port = port;
1389 schedule_work(&cfg->work_q);
1390 }
1391
1392 if (info->action & CLR_FC_ERROR) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001393 reg = readq_be(&fc_port_regs[FC_ERROR / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001394
1395 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001396 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001397 * should be the same and tracing one is sufficient.
1398 */
1399
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001400 dev_err(dev, "%s: fc %d: clearing fc_error=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001401 __func__, port, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001402
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001403 writeq_be(reg, &fc_port_regs[FC_ERROR / 8]);
1404 writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001405 }
Matthew R. Ochsef510742015-10-21 15:13:37 -05001406
1407 if (info->action & SCAN_HOST) {
1408 atomic_inc(&cfg->scan_host_needed);
1409 schedule_work(&cfg->work_q);
1410 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001411 }
1412
1413out:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001414 return IRQ_HANDLED;
1415}
1416
1417/**
1418 * start_context() - starts the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001419 * @cfg: Internal structure associated with the host.
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001420 * @index: Index of the hardware queue.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001421 *
1422 * Return: A success or failure value from CXL services.
1423 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001424static int start_context(struct cxlflash_cfg *cfg, u32 index)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001425{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001426 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001427 struct hwq *hwq = get_hwq(cfg->afu, index);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001428 int rc = 0;
1429
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001430 rc = cxl_start_context(hwq->ctx,
1431 hwq->work.work_element_descriptor,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001432 NULL);
1433
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001434 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001435 return rc;
1436}
1437
1438/**
1439 * read_vpd() - obtains the WWPNs from VPD
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001440 * @cfg: Internal structure associated with the host.
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001441 * @wwpn: Array of size MAX_FC_PORTS to pass back WWPNs
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001442 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001443 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001444 */
1445static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1446{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001447 struct device *dev = &cfg->dev->dev;
1448 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001449 int rc = 0;
1450 int ro_start, ro_size, i, j, k;
1451 ssize_t vpd_size;
1452 char vpd_data[CXLFLASH_VPD_LEN];
1453 char tmp_buf[WWPN_BUF_LEN] = { 0 };
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05001454 char *wwpn_vpd_tags[MAX_FC_PORTS] = { "V5", "V6", "V7", "V8" };
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001455
1456 /* Get the VPD data from the device */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001457 vpd_size = cxl_read_adapter_vpd(pdev, vpd_data, sizeof(vpd_data));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001458 if (unlikely(vpd_size <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001459 dev_err(dev, "%s: Unable to read VPD (size = %ld)\n",
1460 __func__, vpd_size);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001461 rc = -ENODEV;
1462 goto out;
1463 }
1464
1465 /* Get the read only section offset */
1466 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
1467 PCI_VPD_LRDT_RO_DATA);
1468 if (unlikely(ro_start < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001469 dev_err(dev, "%s: VPD Read-only data not found\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001470 rc = -ENODEV;
1471 goto out;
1472 }
1473
1474 /* Get the read only section size, cap when extends beyond read VPD */
1475 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1476 j = ro_size;
1477 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1478 if (unlikely((i + j) > vpd_size)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001479 dev_dbg(dev, "%s: Might need to read more VPD (%d > %ld)\n",
1480 __func__, (i + j), vpd_size);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001481 ro_size = vpd_size - i;
1482 }
1483
1484 /*
1485 * Find the offset of the WWPN tag within the read only
1486 * VPD data and validate the found field (partials are
1487 * no good to us). Convert the ASCII data to an integer
1488 * value. Note that we must copy to a temporary buffer
1489 * because the conversion service requires that the ASCII
1490 * string be terminated.
1491 */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001492 for (k = 0; k < cfg->num_fc_ports; k++) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001493 j = ro_size;
1494 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1495
1496 i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
1497 if (unlikely(i < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001498 dev_err(dev, "%s: Port %d WWPN not found in VPD\n",
1499 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001500 rc = -ENODEV;
1501 goto out;
1502 }
1503
1504 j = pci_vpd_info_field_size(&vpd_data[i]);
1505 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1506 if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001507 dev_err(dev, "%s: Port %d WWPN incomplete or bad VPD\n",
1508 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001509 rc = -ENODEV;
1510 goto out;
1511 }
1512
1513 memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1514 rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1515 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001516 dev_err(dev, "%s: WWPN conversion failed for port %d\n",
1517 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001518 rc = -ENODEV;
1519 goto out;
1520 }
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001521
1522 dev_dbg(dev, "%s: wwpn%d=%016llx\n", __func__, k, wwpn[k]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001523 }
1524
1525out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001526 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001527 return rc;
1528}
1529
1530/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001531 * init_pcr() - initialize the provisioning and control registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001532 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001533 *
1534 * Also sets up fast access to the mapped registers and initializes AFU
1535 * command fields that never change.
1536 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001537static void init_pcr(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001538{
1539 struct afu *afu = cfg->afu;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001540 struct sisl_ctrl_map __iomem *ctrl_map;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001541 struct hwq *hwq;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001542 int i;
1543
1544 for (i = 0; i < MAX_CONTEXT; i++) {
1545 ctrl_map = &afu->afu_map->ctrls[i].ctrl;
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001546 /* Disrupt any clients that could be running */
1547 /* e.g. clients that survived a master restart */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001548 writeq_be(0, &ctrl_map->rht_start);
1549 writeq_be(0, &ctrl_map->rht_cnt_id);
1550 writeq_be(0, &ctrl_map->ctx_cap);
1551 }
1552
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001553 /* Copy frequently used fields into hwq */
1554 for (i = 0; i < CXLFLASH_NUM_HWQS; i++) {
1555 hwq = get_hwq(afu, i);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001556
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001557 hwq->ctx_hndl = (u16) cxl_process_element(hwq->ctx);
1558 hwq->host_map = &afu->afu_map->hosts[hwq->ctx_hndl].host;
1559 hwq->ctrl_map = &afu->afu_map->ctrls[hwq->ctx_hndl].ctrl;
1560
1561 /* Program the Endian Control for the master context */
1562 writeq_be(SISL_ENDIAN_CTRL, &hwq->host_map->endian_ctrl);
1563 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001564}
1565
1566/**
1567 * init_global() - initialize AFU global registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001568 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001569 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001570static int init_global(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001571{
1572 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001573 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001574 struct hwq *hwq;
1575 struct sisl_host_map __iomem *hmap;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001576 __be64 __iomem *fc_port_regs;
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001577 u64 wwpn[MAX_FC_PORTS]; /* wwpn of AFU ports */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001578 int i = 0, num_ports = 0;
1579 int rc = 0;
1580 u64 reg;
1581
1582 rc = read_vpd(cfg, &wwpn[0]);
1583 if (rc) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001584 dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001585 goto out;
1586 }
1587
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001588 /* Set up RRQ and SQ in HWQ for master issued cmds */
1589 for (i = 0; i < CXLFLASH_NUM_HWQS; i++) {
1590 hwq = get_hwq(afu, i);
1591 hmap = hwq->host_map;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001592
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001593 writeq_be((u64) hwq->hrrq_start, &hmap->rrq_start);
1594 writeq_be((u64) hwq->hrrq_end, &hmap->rrq_end);
1595
1596 if (afu_is_sq_cmd_mode(afu)) {
1597 writeq_be((u64)hwq->hsq_start, &hmap->sq_start);
1598 writeq_be((u64)hwq->hsq_end, &hmap->sq_end);
1599 }
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001600 }
1601
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001602 /* AFU configuration */
1603 reg = readq_be(&afu->afu_map->global.regs.afu_config);
1604 reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1605 /* enable all auto retry options and control endianness */
1606 /* leave others at default: */
1607 /* CTX_CAP write protected, mbox_r does not clear on read and */
1608 /* checker on if dual afu */
1609 writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1610
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001611 /* Global port select: select either port */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001612 if (afu->internal_lun) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001613 /* Only use port 0 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001614 writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001615 num_ports = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001616 } else {
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -05001617 writeq_be(PORT_MASK(cfg->num_fc_ports),
1618 &afu->afu_map->global.regs.afu_port_sel);
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001619 num_ports = cfg->num_fc_ports;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001620 }
1621
1622 for (i = 0; i < num_ports; i++) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001623 fc_port_regs = get_fc_port_regs(cfg, i);
1624
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001625 /* Unmask all errors (but they are still masked at AFU) */
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001626 writeq_be(0, &fc_port_regs[FC_ERRMSK / 8]);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001627 /* Clear CRC error cnt & set a threshold */
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001628 (void)readq_be(&fc_port_regs[FC_CNT_CRCERR / 8]);
1629 writeq_be(MC_CRC_THRESH, &fc_port_regs[FC_CRC_THRESH / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001630
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001631 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001632 if (wwpn[i] != 0)
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001633 afu_set_wwpn(afu, i, &fc_port_regs[0], wwpn[i]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001634 /* Programming WWPN back to back causes additional
1635 * offline/online transitions and a PLOGI
1636 */
1637 msleep(100);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001638 }
1639
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001640 /* Set up master's own CTX_CAP to allow real mode, host translation */
1641 /* tables, afu cmds and read/write GSCSI cmds. */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001642 /* First, unlock ctx_cap write by reading mbox */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001643 for (i = 0; i < CXLFLASH_NUM_HWQS; i++) {
1644 hwq = get_hwq(afu, i);
1645
1646 (void)readq_be(&hwq->ctrl_map->mbox_r); /* unlock ctx_cap */
1647 writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1648 SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1649 SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1650 &hwq->ctrl_map->ctx_cap);
1651 }
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001652 /* Initialize heartbeat */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001653 afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001654out:
1655 return rc;
1656}
1657
1658/**
1659 * start_afu() - initializes and starts the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001660 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001661 */
1662static int start_afu(struct cxlflash_cfg *cfg)
1663{
1664 struct afu *afu = cfg->afu;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001665 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001666 struct hwq *hwq;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001667 int rc = 0;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001668 int i;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001669
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001670 init_pcr(cfg);
1671
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001672 /* Initialize each HWQ */
1673 for (i = 0; i < CXLFLASH_NUM_HWQS; i++) {
1674 hwq = get_hwq(afu, i);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001675
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001676 /* After an AFU reset, RRQ entries are stale, clear them */
1677 memset(&hwq->rrq_entry, 0, sizeof(hwq->rrq_entry));
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001678
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001679 /* Initialize RRQ pointers */
1680 hwq->hrrq_start = &hwq->rrq_entry[0];
1681 hwq->hrrq_end = &hwq->rrq_entry[NUM_RRQ_ENTRY - 1];
1682 hwq->hrrq_curr = hwq->hrrq_start;
1683 hwq->toggle = 1;
1684 spin_lock_init(&hwq->hrrq_slock);
1685
1686 /* Initialize SQ */
1687 if (afu_is_sq_cmd_mode(afu)) {
1688 memset(&hwq->sq, 0, sizeof(hwq->sq));
1689 hwq->hsq_start = &hwq->sq[0];
1690 hwq->hsq_end = &hwq->sq[NUM_SQ_ENTRY - 1];
1691 hwq->hsq_curr = hwq->hsq_start;
1692
1693 spin_lock_init(&hwq->hsq_slock);
1694 atomic_set(&hwq->hsq_credits, NUM_SQ_ENTRY - 1);
1695 }
1696
1697 /* Initialize IRQ poll */
1698 if (afu_is_irqpoll_enabled(afu))
1699 irq_poll_init(&hwq->irqpoll, afu->irqpoll_weight,
1700 cxlflash_irqpoll);
1701
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001702 }
1703
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001704 rc = init_global(cfg);
1705
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001706 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001707 return rc;
1708}
1709
1710/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001711 * init_intr() - setup interrupt handlers for the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001712 * @cfg: Internal structure associated with the host.
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001713 * @hwq: Hardware queue to initialize.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001714 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001715 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001716 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001717static enum undo_level init_intr(struct cxlflash_cfg *cfg,
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001718 struct hwq *hwq)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001719{
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001720 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001721 struct cxl_context *ctx = hwq->ctx;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001722 int rc = 0;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001723 enum undo_level level = UNDO_NOOP;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001724 bool is_primary_hwq = (hwq->index == PRIMARY_HWQ);
1725 int num_irqs = is_primary_hwq ? 3 : 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001726
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001727 rc = cxl_allocate_afu_irqs(ctx, num_irqs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001728 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001729 dev_err(dev, "%s: allocate_afu_irqs failed rc=%d\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001730 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001731 level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001732 goto out;
1733 }
1734
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001735 rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, hwq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001736 "SISL_MSI_SYNC_ERROR");
1737 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001738 dev_err(dev, "%s: SISL_MSI_SYNC_ERROR map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001739 level = FREE_IRQ;
1740 goto out;
1741 }
1742
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001743 rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, hwq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001744 "SISL_MSI_RRQ_UPDATED");
1745 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001746 dev_err(dev, "%s: SISL_MSI_RRQ_UPDATED map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001747 level = UNMAP_ONE;
1748 goto out;
1749 }
1750
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001751 /* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
1752 if (!is_primary_hwq)
1753 goto out;
1754
1755 rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, hwq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001756 "SISL_MSI_ASYNC_ERROR");
1757 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001758 dev_err(dev, "%s: SISL_MSI_ASYNC_ERROR map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001759 level = UNMAP_TWO;
1760 goto out;
1761 }
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001762out:
1763 return level;
1764}
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001765
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001766/**
1767 * init_mc() - create and register as the master context
1768 * @cfg: Internal structure associated with the host.
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001769 * index: HWQ Index of the master context.
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001770 *
1771 * Return: 0 on success, -errno on failure
1772 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001773static int init_mc(struct cxlflash_cfg *cfg, u32 index)
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001774{
1775 struct cxl_context *ctx;
1776 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001777 struct hwq *hwq = get_hwq(cfg->afu, index);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001778 int rc = 0;
1779 enum undo_level level;
1780
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001781 hwq->afu = cfg->afu;
1782 hwq->index = index;
1783
1784 if (index == PRIMARY_HWQ)
1785 ctx = cxl_get_context(cfg->dev);
1786 else
1787 ctx = cxl_dev_context_init(cfg->dev);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001788 if (unlikely(!ctx)) {
1789 rc = -ENOMEM;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001790 goto err1;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001791 }
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001792
1793 WARN_ON(hwq->ctx);
1794 hwq->ctx = ctx;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001795
1796 /* Set it up as a master with the CXL */
1797 cxl_set_master(ctx);
1798
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001799 /* Reset AFU when initializing primary context */
1800 if (index == PRIMARY_HWQ) {
1801 rc = cxl_afu_reset(ctx);
1802 if (unlikely(rc)) {
1803 dev_err(dev, "%s: AFU reset failed rc=%d\n",
1804 __func__, rc);
1805 goto err1;
1806 }
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001807 }
1808
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001809 level = init_intr(cfg, hwq);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001810 if (unlikely(level)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001811 dev_err(dev, "%s: interrupt init failed rc=%d\n", __func__, rc);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001812 goto err2;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001813 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001814
1815 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1816 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1817 * element (pe) that is embedded in the context (ctx)
1818 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001819 rc = start_context(cfg, index);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001820 if (unlikely(rc)) {
1821 dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
1822 level = UNMAP_THREE;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001823 goto err2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001824 }
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001825
1826out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001827 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001828 return rc;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001829err2:
1830 term_intr(cfg, level, index);
1831 if (index != PRIMARY_HWQ)
1832 cxl_release_context(ctx);
1833err1:
1834 hwq->ctx = NULL;
1835 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001836}
1837
1838/**
Matthew R. Ochs565180722017-04-12 14:14:28 -05001839 * get_num_afu_ports() - determines and configures the number of AFU ports
1840 * @cfg: Internal structure associated with the host.
1841 *
1842 * This routine determines the number of AFU ports by converting the global
1843 * port selection mask. The converted value is only valid following an AFU
1844 * reset (explicit or power-on). This routine must be invoked shortly after
1845 * mapping as other routines are dependent on the number of ports during the
1846 * initialization sequence.
1847 *
1848 * To support legacy AFUs that might not have reflected an initial global
1849 * port mask (value read is 0), default to the number of ports originally
1850 * supported by the cxlflash driver (2) before hardware with other port
1851 * offerings was introduced.
1852 */
1853static void get_num_afu_ports(struct cxlflash_cfg *cfg)
1854{
1855 struct afu *afu = cfg->afu;
1856 struct device *dev = &cfg->dev->dev;
1857 u64 port_mask;
1858 int num_fc_ports = LEGACY_FC_PORTS;
1859
1860 port_mask = readq_be(&afu->afu_map->global.regs.afu_port_sel);
1861 if (port_mask != 0ULL)
1862 num_fc_ports = min(ilog2(port_mask) + 1, MAX_FC_PORTS);
1863
1864 dev_dbg(dev, "%s: port_mask=%016llx num_fc_ports=%d\n",
1865 __func__, port_mask, num_fc_ports);
1866
1867 cfg->num_fc_ports = num_fc_ports;
1868 cfg->host->max_channel = PORTNUM2CHAN(num_fc_ports);
1869}
1870
1871/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001872 * init_afu() - setup as master context and start AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001873 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001874 *
1875 * This routine is a higher level of control for configuring the
1876 * AFU on probe and reset paths.
1877 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001878 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001879 */
1880static int init_afu(struct cxlflash_cfg *cfg)
1881{
1882 u64 reg;
1883 int rc = 0;
1884 struct afu *afu = cfg->afu;
1885 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001886 struct hwq *hwq;
1887 int i;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001888
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001889 cxl_perst_reloads_same_image(cfg->cxl_afu, true);
1890
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001891 for (i = 0; i < CXLFLASH_NUM_HWQS; i++) {
1892 rc = init_mc(cfg, i);
1893 if (rc) {
1894 dev_err(dev, "%s: init_mc failed rc=%d index=%d\n",
1895 __func__, rc, i);
1896 goto err1;
1897 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001898 }
1899
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001900 /* Map the entire MMIO space of the AFU using the first context */
1901 hwq = get_hwq(afu, PRIMARY_HWQ);
1902 afu->afu_map = cxl_psa_map(hwq->ctx);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001903 if (!afu->afu_map) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001904 dev_err(dev, "%s: cxl_psa_map failed\n", __func__);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001905 rc = -ENOMEM;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001906 goto err1;
1907 }
1908
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001909 /* No byte reverse on reading afu_version or string will be backwards */
1910 reg = readq(&afu->afu_map->global.regs.afu_version);
1911 memcpy(afu->version, &reg, sizeof(reg));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001912 afu->interface_version =
1913 readq_be(&afu->afu_map->global.regs.interface_version);
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001914 if ((afu->interface_version + 1) == 0) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001915 dev_err(dev, "Back level AFU, please upgrade. AFU version %s "
1916 "interface version %016llx\n", afu->version,
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001917 afu->interface_version);
1918 rc = -EINVAL;
Uma Krishnan0df5bef2017-01-11 19:20:03 -06001919 goto err1;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001920 }
1921
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001922 if (afu_is_sq_cmd_mode(afu)) {
1923 afu->send_cmd = send_cmd_sq;
1924 afu->context_reset = context_reset_sq;
1925 } else {
1926 afu->send_cmd = send_cmd_ioarrin;
1927 afu->context_reset = context_reset_ioarrin;
1928 }
Matthew R. Ochs48b4be32016-11-28 18:43:09 -06001929
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001930 dev_dbg(dev, "%s: afu_ver=%s interface_ver=%016llx\n", __func__,
1931 afu->version, afu->interface_version);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001932
Matthew R. Ochs565180722017-04-12 14:14:28 -05001933 get_num_afu_ports(cfg);
1934
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001935 rc = start_afu(cfg);
1936 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001937 dev_err(dev, "%s: start_afu failed, rc=%d\n", __func__, rc);
Uma Krishnan0df5bef2017-01-11 19:20:03 -06001938 goto err1;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001939 }
1940
1941 afu_err_intr_init(cfg->afu);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001942 for (i = 0; i < CXLFLASH_NUM_HWQS; i++) {
1943 hwq = get_hwq(afu, i);
1944
1945 spin_lock_init(&hwq->rrin_slock);
1946 hwq->room = readq_be(&hwq->host_map->cmd_room);
1947 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001948
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05001949 /* Restore the LUN mappings */
1950 cxlflash_restore_luntable(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001951out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001952 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001953 return rc;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001954
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001955err1:
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001956 for (i = CXLFLASH_NUM_HWQS - 1; i >= 0; i--) {
1957 term_intr(cfg, UNMAP_THREE, i);
1958 term_mc(cfg, i);
1959 }
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001960 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001961}
1962
1963/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001964 * cxlflash_afu_sync() - builds and sends an AFU sync command
1965 * @afu: AFU associated with the host.
1966 * @ctx_hndl_u: Identifies context requesting sync.
1967 * @res_hndl_u: Identifies resource requesting sync.
1968 * @mode: Type of sync to issue (lightweight, heavyweight, global).
1969 *
1970 * The AFU can only take 1 sync command at a time. This routine enforces this
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001971 * limitation by using a mutex to provide exclusive access to the AFU during
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001972 * the sync. This design point requires calling threads to not be on interrupt
1973 * context due to the possibility of sleeping during concurrent sync operations.
1974 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001975 * AFU sync operations are only necessary and allowed when the device is
1976 * operating normally. When not operating normally, sync requests can occur as
1977 * part of cleaning up resources associated with an adapter prior to removal.
1978 * In this scenario, these requests are simply ignored (safe due to the AFU
1979 * going away).
1980 *
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001981 * Return:
1982 * 0 on success
1983 * -1 on failure
1984 */
1985int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
1986 res_hndl_t res_hndl_u, u8 mode)
1987{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001988 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001989 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001990 struct afu_cmd *cmd = NULL;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001991 struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001992 char *buf = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001993 int rc = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001994 static DEFINE_MUTEX(sync_active);
1995
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001996 if (cfg->state != STATE_NORMAL) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001997 dev_dbg(dev, "%s: Sync not required state=%u\n",
1998 __func__, cfg->state);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001999 return 0;
2000 }
2001
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002002 mutex_lock(&sync_active);
Matthew R. Ochsde012832016-11-28 18:42:33 -06002003 atomic_inc(&afu->cmds_active);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06002004 buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
2005 if (unlikely(!buf)) {
2006 dev_err(dev, "%s: no memory for command\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002007 rc = -1;
2008 goto out;
2009 }
2010
Matthew R. Ochs350bb472016-11-28 18:42:11 -06002011 cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
2012 init_completion(&cmd->cevent);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06002013 cmd->parent = afu;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002014 cmd->hwq_index = hwq->index;
Matthew R. Ochs350bb472016-11-28 18:42:11 -06002015
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002016 dev_dbg(dev, "%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002017
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002018 cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002019 cmd->rcb.ctx_id = hwq->ctx_hndl;
Matthew R. Ochs350bb472016-11-28 18:42:11 -06002020 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002021 cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
2022
2023 cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
2024 cmd->rcb.cdb[1] = mode;
2025
2026 /* The cdb is aligned, no unaligned accessors required */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05002027 *((__be16 *)&cmd->rcb.cdb[2]) = cpu_to_be16(ctx_hndl_u);
2028 *((__be32 *)&cmd->rcb.cdb[4]) = cpu_to_be32(res_hndl_u);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002029
Matthew R. Ochs48b4be32016-11-28 18:43:09 -06002030 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002031 if (unlikely(rc))
2032 goto out;
2033
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -06002034 rc = wait_resp(afu, cmd);
2035 if (unlikely(rc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002036 rc = -1;
2037out:
Matthew R. Ochsde012832016-11-28 18:42:33 -06002038 atomic_dec(&afu->cmds_active);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002039 mutex_unlock(&sync_active);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06002040 kfree(buf);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002041 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002042 return rc;
2043}
2044
2045/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05002046 * afu_reset() - resets the AFU
2047 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002048 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002049 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002050 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002051static int afu_reset(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002052{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002053 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002054 int rc = 0;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002055
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002056 /* Stop the context before the reset. Since the context is
2057 * no longer available restart it after the reset is complete
2058 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002059 term_afu(cfg);
2060
2061 rc = init_afu(cfg);
2062
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002063 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002064 return rc;
2065}
2066
2067/**
Manoj N. Kumarf4113962016-06-15 18:49:20 -05002068 * drain_ioctls() - wait until all currently executing ioctls have completed
2069 * @cfg: Internal structure associated with the host.
2070 *
2071 * Obtain write access to read/write semaphore that wraps ioctl
2072 * handling to 'drain' ioctls currently executing.
2073 */
2074static void drain_ioctls(struct cxlflash_cfg *cfg)
2075{
2076 down_write(&cfg->ioctl_rwsem);
2077 up_write(&cfg->ioctl_rwsem);
2078}
2079
2080/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05002081 * cxlflash_eh_device_reset_handler() - reset a single LUN
2082 * @scp: SCSI command to send.
2083 *
2084 * Return:
2085 * SUCCESS as defined in scsi/scsi.h
2086 * FAILED as defined in scsi/scsi.h
2087 */
2088static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
2089{
2090 int rc = SUCCESS;
2091 struct Scsi_Host *host = scp->device->host;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002092 struct cxlflash_cfg *cfg = shost_priv(host);
2093 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002094 struct afu *afu = cfg->afu;
2095 int rcr = 0;
2096
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002097 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
2098 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
2099 scp->device->channel, scp->device->id, scp->device->lun,
2100 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
2101 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
2102 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
2103 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochs15305512015-10-21 15:12:10 -05002104
Matthew R. Ochsed486da2015-10-21 15:14:24 -05002105retry:
Matthew R. Ochs15305512015-10-21 15:12:10 -05002106 switch (cfg->state) {
2107 case STATE_NORMAL:
2108 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
2109 if (unlikely(rcr))
2110 rc = FAILED;
2111 break;
2112 case STATE_RESET:
2113 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
Matthew R. Ochsed486da2015-10-21 15:14:24 -05002114 goto retry;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002115 default:
2116 rc = FAILED;
2117 break;
2118 }
2119
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002120 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002121 return rc;
2122}
2123
2124/**
2125 * cxlflash_eh_host_reset_handler() - reset the host adapter
2126 * @scp: SCSI command from stack identifying host.
2127 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002128 * Following a reset, the state is evaluated again in case an EEH occurred
2129 * during the reset. In such a scenario, the host reset will either yield
2130 * until the EEH recovery is complete or return success or failure based
2131 * upon the current device state.
2132 *
Matthew R. Ochs15305512015-10-21 15:12:10 -05002133 * Return:
2134 * SUCCESS as defined in scsi/scsi.h
2135 * FAILED as defined in scsi/scsi.h
2136 */
2137static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
2138{
2139 int rc = SUCCESS;
2140 int rcr = 0;
2141 struct Scsi_Host *host = scp->device->host;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002142 struct cxlflash_cfg *cfg = shost_priv(host);
2143 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002144
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002145 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
2146 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
2147 scp->device->channel, scp->device->id, scp->device->lun,
2148 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
2149 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
2150 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
2151 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochs15305512015-10-21 15:12:10 -05002152
2153 switch (cfg->state) {
2154 case STATE_NORMAL:
2155 cfg->state = STATE_RESET;
Manoj N. Kumarf4113962016-06-15 18:49:20 -05002156 drain_ioctls(cfg);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002157 cxlflash_mark_contexts_error(cfg);
2158 rcr = afu_reset(cfg);
2159 if (rcr) {
2160 rc = FAILED;
2161 cfg->state = STATE_FAILTERM;
2162 } else
2163 cfg->state = STATE_NORMAL;
2164 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002165 ssleep(1);
2166 /* fall through */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002167 case STATE_RESET:
2168 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2169 if (cfg->state == STATE_NORMAL)
2170 break;
2171 /* fall through */
2172 default:
2173 rc = FAILED;
2174 break;
2175 }
2176
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002177 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002178 return rc;
2179}
2180
2181/**
2182 * cxlflash_change_queue_depth() - change the queue depth for the device
2183 * @sdev: SCSI device destined for queue depth change.
2184 * @qdepth: Requested queue depth value to set.
2185 *
2186 * The requested queue depth is capped to the maximum supported value.
2187 *
2188 * Return: The actual queue depth set.
2189 */
2190static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
2191{
2192
2193 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
2194 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
2195
2196 scsi_change_queue_depth(sdev, qdepth);
2197 return sdev->queue_depth;
2198}
2199
2200/**
2201 * cxlflash_show_port_status() - queries and presents the current port status
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002202 * @port: Desired port for status reporting.
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002203 * @cfg: Internal structure associated with the host.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002204 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2205 *
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002206 * Return: The size of the ASCII string returned in @buf or -EINVAL.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002207 */
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002208static ssize_t cxlflash_show_port_status(u32 port,
2209 struct cxlflash_cfg *cfg,
2210 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002211{
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002212 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002213 char *disp_status;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002214 u64 status;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002215 __be64 __iomem *fc_port_regs;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002216
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002217 WARN_ON(port >= MAX_FC_PORTS);
2218
2219 if (port >= cfg->num_fc_ports) {
2220 dev_info(dev, "%s: Port %d not supported on this card.\n",
2221 __func__, port);
2222 return -EINVAL;
2223 }
Matthew R. Ochs15305512015-10-21 15:12:10 -05002224
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002225 fc_port_regs = get_fc_port_regs(cfg, port);
2226 status = readq_be(&fc_port_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002227 status &= FC_MTIP_STATUS_MASK;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002228
2229 if (status == FC_MTIP_STATUS_ONLINE)
2230 disp_status = "online";
2231 else if (status == FC_MTIP_STATUS_OFFLINE)
2232 disp_status = "offline";
2233 else
2234 disp_status = "unknown";
2235
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002236 return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002237}
2238
2239/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002240 * port0_show() - queries and presents the current status of port 0
2241 * @dev: Generic device associated with the host owning the port.
2242 * @attr: Device attribute representing the port.
2243 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002244 *
2245 * Return: The size of the ASCII string returned in @buf.
2246 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002247static ssize_t port0_show(struct device *dev,
2248 struct device_attribute *attr,
2249 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002250{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002251 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochs15305512015-10-21 15:12:10 -05002252
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002253 return cxlflash_show_port_status(0, cfg, buf);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002254}
2255
2256/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002257 * port1_show() - queries and presents the current status of port 1
2258 * @dev: Generic device associated with the host owning the port.
2259 * @attr: Device attribute representing the port.
2260 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2261 *
2262 * Return: The size of the ASCII string returned in @buf.
2263 */
2264static ssize_t port1_show(struct device *dev,
2265 struct device_attribute *attr,
2266 char *buf)
2267{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002268 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002269
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002270 return cxlflash_show_port_status(1, cfg, buf);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002271}
2272
2273/**
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002274 * port2_show() - queries and presents the current status of port 2
2275 * @dev: Generic device associated with the host owning the port.
2276 * @attr: Device attribute representing the port.
2277 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2278 *
2279 * Return: The size of the ASCII string returned in @buf.
2280 */
2281static ssize_t port2_show(struct device *dev,
2282 struct device_attribute *attr,
2283 char *buf)
2284{
2285 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2286
2287 return cxlflash_show_port_status(2, cfg, buf);
2288}
2289
2290/**
2291 * port3_show() - queries and presents the current status of port 3
2292 * @dev: Generic device associated with the host owning the port.
2293 * @attr: Device attribute representing the port.
2294 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2295 *
2296 * Return: The size of the ASCII string returned in @buf.
2297 */
2298static ssize_t port3_show(struct device *dev,
2299 struct device_attribute *attr,
2300 char *buf)
2301{
2302 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2303
2304 return cxlflash_show_port_status(3, cfg, buf);
2305}
2306
2307/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002308 * lun_mode_show() - presents the current LUN mode of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002309 * @dev: Generic device associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002310 * @attr: Device attribute representing the LUN mode.
2311 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2312 *
2313 * Return: The size of the ASCII string returned in @buf.
2314 */
2315static ssize_t lun_mode_show(struct device *dev,
2316 struct device_attribute *attr, char *buf)
2317{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002318 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002319 struct afu *afu = cfg->afu;
2320
2321 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
2322}
2323
2324/**
2325 * lun_mode_store() - sets the LUN mode of the host
2326 * @dev: Generic device associated with the host.
2327 * @attr: Device attribute representing the LUN mode.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002328 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2329 * @count: Length of data resizing in @buf.
2330 *
2331 * The CXL Flash AFU supports a dummy LUN mode where the external
2332 * links and storage are not required. Space on the FPGA is used
2333 * to create 1 or 2 small LUNs which are presented to the system
2334 * as if they were a normal storage device. This feature is useful
2335 * during development and also provides manufacturing with a way
2336 * to test the AFU without an actual device.
2337 *
2338 * 0 = external LUN[s] (default)
2339 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2340 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2341 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2342 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2343 *
2344 * Return: The size of the ASCII string returned in @buf.
2345 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002346static ssize_t lun_mode_store(struct device *dev,
2347 struct device_attribute *attr,
2348 const char *buf, size_t count)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002349{
2350 struct Scsi_Host *shost = class_to_shost(dev);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002351 struct cxlflash_cfg *cfg = shost_priv(shost);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002352 struct afu *afu = cfg->afu;
2353 int rc;
2354 u32 lun_mode;
2355
2356 rc = kstrtouint(buf, 10, &lun_mode);
2357 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
2358 afu->internal_lun = lun_mode;
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002359
2360 /*
2361 * When configured for internal LUN, there is only one channel,
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002362 * channel number 0, else there will be one less than the number
2363 * of fc ports for this card.
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002364 */
2365 if (afu->internal_lun)
2366 shost->max_channel = 0;
2367 else
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -05002368 shost->max_channel = PORTNUM2CHAN(cfg->num_fc_ports);
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002369
Matthew R. Ochs15305512015-10-21 15:12:10 -05002370 afu_reset(cfg);
2371 scsi_scan_host(cfg->host);
2372 }
2373
2374 return count;
2375}
2376
2377/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002378 * ioctl_version_show() - presents the current ioctl version of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002379 * @dev: Generic device associated with the host.
2380 * @attr: Device attribute representing the ioctl version.
2381 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2382 *
2383 * Return: The size of the ASCII string returned in @buf.
2384 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002385static ssize_t ioctl_version_show(struct device *dev,
2386 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002387{
2388 return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
2389}
2390
2391/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002392 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2393 * @port: Desired port for status reporting.
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002394 * @cfg: Internal structure associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002395 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2396 *
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002397 * Return: The size of the ASCII string returned in @buf or -EINVAL.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002398 */
2399static ssize_t cxlflash_show_port_lun_table(u32 port,
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002400 struct cxlflash_cfg *cfg,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002401 char *buf)
2402{
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002403 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002404 __be64 __iomem *fc_port_luns;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002405 int i;
2406 ssize_t bytes = 0;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002407
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002408 WARN_ON(port >= MAX_FC_PORTS);
2409
2410 if (port >= cfg->num_fc_ports) {
2411 dev_info(dev, "%s: Port %d not supported on this card.\n",
2412 __func__, port);
2413 return -EINVAL;
2414 }
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002415
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002416 fc_port_luns = get_fc_port_luns(cfg, port);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002417
2418 for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2419 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002420 "%03d: %016llx\n",
2421 i, readq_be(&fc_port_luns[i]));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002422 return bytes;
2423}
2424
2425/**
2426 * port0_lun_table_show() - presents the current LUN table of port 0
2427 * @dev: Generic device associated with the host owning the port.
2428 * @attr: Device attribute representing the port.
2429 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2430 *
2431 * Return: The size of the ASCII string returned in @buf.
2432 */
2433static ssize_t port0_lun_table_show(struct device *dev,
2434 struct device_attribute *attr,
2435 char *buf)
2436{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002437 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002438
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002439 return cxlflash_show_port_lun_table(0, cfg, buf);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002440}
2441
2442/**
2443 * port1_lun_table_show() - presents the current LUN table of port 1
2444 * @dev: Generic device associated with the host owning the port.
2445 * @attr: Device attribute representing the port.
2446 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2447 *
2448 * Return: The size of the ASCII string returned in @buf.
2449 */
2450static ssize_t port1_lun_table_show(struct device *dev,
2451 struct device_attribute *attr,
2452 char *buf)
2453{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002454 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002455
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002456 return cxlflash_show_port_lun_table(1, cfg, buf);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002457}
2458
2459/**
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002460 * port2_lun_table_show() - presents the current LUN table of port 2
2461 * @dev: Generic device associated with the host owning the port.
2462 * @attr: Device attribute representing the port.
2463 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2464 *
2465 * Return: The size of the ASCII string returned in @buf.
2466 */
2467static ssize_t port2_lun_table_show(struct device *dev,
2468 struct device_attribute *attr,
2469 char *buf)
2470{
2471 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2472
2473 return cxlflash_show_port_lun_table(2, cfg, buf);
2474}
2475
2476/**
2477 * port3_lun_table_show() - presents the current LUN table of port 3
2478 * @dev: Generic device associated with the host owning the port.
2479 * @attr: Device attribute representing the port.
2480 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2481 *
2482 * Return: The size of the ASCII string returned in @buf.
2483 */
2484static ssize_t port3_lun_table_show(struct device *dev,
2485 struct device_attribute *attr,
2486 char *buf)
2487{
2488 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2489
2490 return cxlflash_show_port_lun_table(3, cfg, buf);
2491}
2492
2493/**
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002494 * irqpoll_weight_show() - presents the current IRQ poll weight for the host
2495 * @dev: Generic device associated with the host.
2496 * @attr: Device attribute representing the IRQ poll weight.
2497 * @buf: Buffer of length PAGE_SIZE to report back the current IRQ poll
2498 * weight in ASCII.
2499 *
2500 * An IRQ poll weight of 0 indicates polling is disabled.
2501 *
2502 * Return: The size of the ASCII string returned in @buf.
2503 */
2504static ssize_t irqpoll_weight_show(struct device *dev,
2505 struct device_attribute *attr, char *buf)
2506{
2507 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2508 struct afu *afu = cfg->afu;
2509
2510 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->irqpoll_weight);
2511}
2512
2513/**
2514 * irqpoll_weight_store() - sets the current IRQ poll weight for the host
2515 * @dev: Generic device associated with the host.
2516 * @attr: Device attribute representing the IRQ poll weight.
2517 * @buf: Buffer of length PAGE_SIZE containing the desired IRQ poll
2518 * weight in ASCII.
2519 * @count: Length of data resizing in @buf.
2520 *
2521 * An IRQ poll weight of 0 indicates polling is disabled.
2522 *
2523 * Return: The size of the ASCII string returned in @buf.
2524 */
2525static ssize_t irqpoll_weight_store(struct device *dev,
2526 struct device_attribute *attr,
2527 const char *buf, size_t count)
2528{
2529 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2530 struct device *cfgdev = &cfg->dev->dev;
2531 struct afu *afu = cfg->afu;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002532 struct hwq *hwq;
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002533 u32 weight;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002534 int rc, i;
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002535
2536 rc = kstrtouint(buf, 10, &weight);
2537 if (rc)
2538 return -EINVAL;
2539
2540 if (weight > 256) {
2541 dev_info(cfgdev,
2542 "Invalid IRQ poll weight. It must be 256 or less.\n");
2543 return -EINVAL;
2544 }
2545
2546 if (weight == afu->irqpoll_weight) {
2547 dev_info(cfgdev,
2548 "Current IRQ poll weight has the same weight.\n");
2549 return -EINVAL;
2550 }
2551
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002552 if (afu_is_irqpoll_enabled(afu)) {
2553 for (i = 0; i < CXLFLASH_NUM_HWQS; i++) {
2554 hwq = get_hwq(afu, i);
2555
2556 irq_poll_disable(&hwq->irqpoll);
2557 }
2558 }
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002559
2560 afu->irqpoll_weight = weight;
2561
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002562 if (weight > 0) {
2563 for (i = 0; i < CXLFLASH_NUM_HWQS; i++) {
2564 hwq = get_hwq(afu, i);
2565
2566 irq_poll_init(&hwq->irqpoll, weight, cxlflash_irqpoll);
2567 }
2568 }
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002569
2570 return count;
2571}
2572
2573/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002574 * mode_show() - presents the current mode of the device
Matthew R. Ochs15305512015-10-21 15:12:10 -05002575 * @dev: Generic device associated with the device.
2576 * @attr: Device attribute representing the device mode.
2577 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2578 *
2579 * Return: The size of the ASCII string returned in @buf.
2580 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002581static ssize_t mode_show(struct device *dev,
2582 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002583{
2584 struct scsi_device *sdev = to_scsi_device(dev);
2585
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002586 return scnprintf(buf, PAGE_SIZE, "%s\n",
2587 sdev->hostdata ? "superpipe" : "legacy");
Matthew R. Ochs15305512015-10-21 15:12:10 -05002588}
2589
2590/*
2591 * Host attributes
2592 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002593static DEVICE_ATTR_RO(port0);
2594static DEVICE_ATTR_RO(port1);
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002595static DEVICE_ATTR_RO(port2);
2596static DEVICE_ATTR_RO(port3);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002597static DEVICE_ATTR_RW(lun_mode);
2598static DEVICE_ATTR_RO(ioctl_version);
2599static DEVICE_ATTR_RO(port0_lun_table);
2600static DEVICE_ATTR_RO(port1_lun_table);
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002601static DEVICE_ATTR_RO(port2_lun_table);
2602static DEVICE_ATTR_RO(port3_lun_table);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002603static DEVICE_ATTR_RW(irqpoll_weight);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002604
2605static struct device_attribute *cxlflash_host_attrs[] = {
2606 &dev_attr_port0,
2607 &dev_attr_port1,
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002608 &dev_attr_port2,
2609 &dev_attr_port3,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002610 &dev_attr_lun_mode,
2611 &dev_attr_ioctl_version,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002612 &dev_attr_port0_lun_table,
2613 &dev_attr_port1_lun_table,
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002614 &dev_attr_port2_lun_table,
2615 &dev_attr_port3_lun_table,
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002616 &dev_attr_irqpoll_weight,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002617 NULL
2618};
2619
2620/*
2621 * Device attributes
2622 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002623static DEVICE_ATTR_RO(mode);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002624
2625static struct device_attribute *cxlflash_dev_attrs[] = {
2626 &dev_attr_mode,
2627 NULL
2628};
2629
2630/*
2631 * Host template
2632 */
2633static struct scsi_host_template driver_template = {
2634 .module = THIS_MODULE,
2635 .name = CXLFLASH_ADAPTER_NAME,
2636 .info = cxlflash_driver_info,
2637 .ioctl = cxlflash_ioctl,
2638 .proc_name = CXLFLASH_NAME,
2639 .queuecommand = cxlflash_queuecommand,
2640 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
2641 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
2642 .change_queue_depth = cxlflash_change_queue_depth,
Manoj N. Kumar83430832016-03-04 15:55:20 -06002643 .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002644 .can_queue = CXLFLASH_MAX_CMDS,
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -06002645 .cmd_size = sizeof(struct afu_cmd) + __alignof__(struct afu_cmd) - 1,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002646 .this_id = -1,
Uma Krishnan68ab2d72016-11-28 18:41:06 -06002647 .sg_tablesize = 1, /* No scatter gather support */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002648 .max_sectors = CXLFLASH_MAX_SECTORS,
2649 .use_clustering = ENABLE_CLUSTERING,
2650 .shost_attrs = cxlflash_host_attrs,
2651 .sdev_attrs = cxlflash_dev_attrs,
2652};
2653
2654/*
2655 * Device dependent values
2656 */
Uma Krishnan96e1b662016-06-15 18:49:38 -05002657static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
2658 0ULL };
2659static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
Uma Krishnan704c4b02016-06-15 18:49:57 -05002660 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs94344522017-02-16 21:39:32 -06002661static struct dev_dependent_vals dev_briard_vals = { CXLFLASH_MAX_SECTORS,
2662 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs15305512015-10-21 15:12:10 -05002663
2664/*
2665 * PCI device binding table
2666 */
2667static struct pci_device_id cxlflash_pci_table[] = {
2668 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
2669 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
Manoj Kumara2746fb2015-12-14 15:07:43 -06002670 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
2671 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
Matthew R. Ochs94344522017-02-16 21:39:32 -06002672 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_BRIARD,
2673 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_briard_vals},
Matthew R. Ochs15305512015-10-21 15:12:10 -05002674 {}
2675};
2676
2677MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
2678
2679/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002680 * cxlflash_worker_thread() - work thread handler for the AFU
2681 * @work: Work structure contained within cxlflash associated with host.
2682 *
2683 * Handles the following events:
2684 * - Link reset which cannot be performed on interrupt context due to
2685 * blocking up to a few seconds
Matthew R. Ochsef510742015-10-21 15:13:37 -05002686 * - Rescan the host
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002687 */
2688static void cxlflash_worker_thread(struct work_struct *work)
2689{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002690 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
2691 work_q);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002692 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002693 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002694 __be64 __iomem *fc_port_regs;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002695 int port;
2696 ulong lock_flags;
2697
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002698 /* Avoid MMIO if the device has failed */
2699
2700 if (cfg->state != STATE_NORMAL)
2701 return;
2702
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002703 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2704
2705 if (cfg->lr_state == LINK_RESET_REQUIRED) {
2706 port = cfg->lr_port;
2707 if (port < 0)
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002708 dev_err(dev, "%s: invalid port index %d\n",
2709 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002710 else {
2711 spin_unlock_irqrestore(cfg->host->host_lock,
2712 lock_flags);
2713
2714 /* The reset can block... */
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002715 fc_port_regs = get_fc_port_regs(cfg, port);
2716 afu_link_reset(afu, port, fc_port_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002717 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2718 }
2719
2720 cfg->lr_state = LINK_RESET_COMPLETE;
2721 }
2722
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002723 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
Matthew R. Ochsef510742015-10-21 15:13:37 -05002724
2725 if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
2726 scsi_scan_host(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002727}
2728
2729/**
2730 * cxlflash_probe() - PCI entry point to add host
2731 * @pdev: PCI device associated with the host.
2732 * @dev_id: PCI device id associated with device.
2733 *
Matthew R. Ochs323e3342017-04-12 14:14:51 -05002734 * The device will initially start out in a 'probing' state and
2735 * transition to the 'normal' state at the end of a successful
2736 * probe. Should an EEH event occur during probe, the notification
2737 * thread (error_detected()) will wait until the probe handler
2738 * is nearly complete. At that time, the device will be moved to
2739 * a 'probed' state and the EEH thread woken up to drive the slot
2740 * reset and recovery (device moves to 'normal' state). Meanwhile,
2741 * the probe will be allowed to exit successfully.
2742 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002743 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002744 */
2745static int cxlflash_probe(struct pci_dev *pdev,
2746 const struct pci_device_id *dev_id)
2747{
2748 struct Scsi_Host *host;
2749 struct cxlflash_cfg *cfg = NULL;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002750 struct device *dev = &pdev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002751 struct dev_dependent_vals *ddv;
2752 int rc = 0;
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002753 int k;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002754
2755 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
2756 __func__, pdev->irq);
2757
2758 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
2759 driver_template.max_sectors = ddv->max_sectors;
2760
2761 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
2762 if (!host) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002763 dev_err(dev, "%s: scsi_host_alloc failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002764 rc = -ENOMEM;
2765 goto out;
2766 }
2767
2768 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
2769 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002770 host->unique_id = host->host_no;
2771 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
2772
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002773 cfg = shost_priv(host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002774 cfg->host = host;
2775 rc = alloc_mem(cfg);
2776 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002777 dev_err(dev, "%s: alloc_mem failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002778 rc = -ENOMEM;
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -05002779 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002780 goto out;
2781 }
2782
2783 cfg->init_state = INIT_STATE_NONE;
2784 cfg->dev = pdev;
Matthew R. Ochs17ead262015-10-21 15:15:37 -05002785 cfg->cxl_fops = cxlflash_cxl_fops;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002786
2787 /*
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002788 * Promoted LUNs move to the top of the LUN table. The rest stay on
2789 * the bottom half. The bottom half grows from the end (index = 255),
2790 * whereas the top half grows from the beginning (index = 0).
2791 *
2792 * Initialize the last LUN index for all possible ports.
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002793 */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002794 cfg->promote_lun_index = 0;
2795
2796 for (k = 0; k < MAX_FC_PORTS; k++)
2797 cfg->last_lun_index[k] = CXLFLASH_NUM_VLUNS/2 - 1;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002798
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002799 cfg->dev_id = (struct pci_device_id *)dev_id;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002800
2801 init_waitqueue_head(&cfg->tmf_waitq);
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002802 init_waitqueue_head(&cfg->reset_waitq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002803
2804 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
2805 cfg->lr_state = LINK_RESET_INVALID;
2806 cfg->lr_port = -1;
Matthew R. Ochs0d731222015-10-21 15:16:24 -05002807 spin_lock_init(&cfg->tmf_slock);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002808 mutex_init(&cfg->ctx_tbl_list_mutex);
2809 mutex_init(&cfg->ctx_recovery_mutex);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002810 init_rwsem(&cfg->ioctl_rwsem);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002811 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
2812 INIT_LIST_HEAD(&cfg->lluns);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002813
2814 pci_set_drvdata(pdev, cfg);
2815
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002816 cfg->cxl_afu = cxl_pci_to_afu(pdev);
2817
2818 rc = init_pci(cfg);
2819 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002820 dev_err(dev, "%s: init_pci failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002821 goto out_remove;
2822 }
2823 cfg->init_state = INIT_STATE_PCI;
2824
2825 rc = init_afu(cfg);
Matthew R. Ochs323e3342017-04-12 14:14:51 -05002826 if (rc && !wq_has_sleeper(&cfg->reset_waitq)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002827 dev_err(dev, "%s: init_afu failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002828 goto out_remove;
2829 }
2830 cfg->init_state = INIT_STATE_AFU;
2831
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002832 rc = init_scsi(cfg);
2833 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002834 dev_err(dev, "%s: init_scsi failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002835 goto out_remove;
2836 }
2837 cfg->init_state = INIT_STATE_SCSI;
2838
Matthew R. Ochs323e3342017-04-12 14:14:51 -05002839 if (wq_has_sleeper(&cfg->reset_waitq)) {
2840 cfg->state = STATE_PROBED;
2841 wake_up_all(&cfg->reset_waitq);
2842 } else
2843 cfg->state = STATE_NORMAL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002844out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002845 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002846 return rc;
2847
2848out_remove:
2849 cxlflash_remove(pdev);
2850 goto out;
2851}
2852
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002853/**
2854 * cxlflash_pci_error_detected() - called when a PCI error is detected
2855 * @pdev: PCI device struct.
2856 * @state: PCI channel state.
2857 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002858 * When an EEH occurs during an active reset, wait until the reset is
2859 * complete and then take action based upon the device state.
2860 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002861 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2862 */
2863static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
2864 pci_channel_state_t state)
2865{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002866 int rc = 0;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002867 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2868 struct device *dev = &cfg->dev->dev;
2869
2870 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
2871
2872 switch (state) {
2873 case pci_channel_io_frozen:
Matthew R. Ochs323e3342017-04-12 14:14:51 -05002874 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET &&
2875 cfg->state != STATE_PROBING);
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002876 if (cfg->state == STATE_FAILTERM)
2877 return PCI_ERS_RESULT_DISCONNECT;
2878
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002879 cfg->state = STATE_RESET;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002880 scsi_block_requests(cfg->host);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002881 drain_ioctls(cfg);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002882 rc = cxlflash_mark_contexts_error(cfg);
2883 if (unlikely(rc))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002884 dev_err(dev, "%s: Failed to mark user contexts rc=%d\n",
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002885 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05002886 term_afu(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002887 return PCI_ERS_RESULT_NEED_RESET;
2888 case pci_channel_io_perm_failure:
2889 cfg->state = STATE_FAILTERM;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002890 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002891 scsi_unblock_requests(cfg->host);
2892 return PCI_ERS_RESULT_DISCONNECT;
2893 default:
2894 break;
2895 }
2896 return PCI_ERS_RESULT_NEED_RESET;
2897}
2898
2899/**
2900 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2901 * @pdev: PCI device struct.
2902 *
2903 * This routine is called by the pci error recovery code after the PCI
2904 * slot has been reset, just before we should resume normal operations.
2905 *
2906 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2907 */
2908static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
2909{
2910 int rc = 0;
2911 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2912 struct device *dev = &cfg->dev->dev;
2913
2914 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2915
2916 rc = init_afu(cfg);
2917 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002918 dev_err(dev, "%s: EEH recovery failed rc=%d\n", __func__, rc);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002919 return PCI_ERS_RESULT_DISCONNECT;
2920 }
2921
2922 return PCI_ERS_RESULT_RECOVERED;
2923}
2924
2925/**
2926 * cxlflash_pci_resume() - called when normal operation can resume
2927 * @pdev: PCI device struct
2928 */
2929static void cxlflash_pci_resume(struct pci_dev *pdev)
2930{
2931 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2932 struct device *dev = &cfg->dev->dev;
2933
2934 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2935
2936 cfg->state = STATE_NORMAL;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002937 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002938 scsi_unblock_requests(cfg->host);
2939}
2940
2941static const struct pci_error_handlers cxlflash_err_handler = {
2942 .error_detected = cxlflash_pci_error_detected,
2943 .slot_reset = cxlflash_pci_slot_reset,
2944 .resume = cxlflash_pci_resume,
2945};
2946
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002947/*
2948 * PCI device structure
2949 */
2950static struct pci_driver cxlflash_driver = {
2951 .name = CXLFLASH_NAME,
2952 .id_table = cxlflash_pci_table,
2953 .probe = cxlflash_probe,
2954 .remove = cxlflash_remove,
Uma Krishnanbabf9852016-09-02 15:39:16 -05002955 .shutdown = cxlflash_remove,
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002956 .err_handler = &cxlflash_err_handler,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002957};
2958
2959/**
2960 * init_cxlflash() - module entry point
2961 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002962 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002963 */
2964static int __init init_cxlflash(void)
2965{
Matthew R. Ochscd41e182017-04-12 14:15:11 -05002966 check_sizes();
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002967 cxlflash_list_init();
2968
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002969 return pci_register_driver(&cxlflash_driver);
2970}
2971
2972/**
2973 * exit_cxlflash() - module exit point
2974 */
2975static void __exit exit_cxlflash(void)
2976{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002977 cxlflash_term_global_luns();
2978 cxlflash_free_errpage();
2979
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002980 pci_unregister_driver(&cxlflash_driver);
2981}
2982
2983module_init(init_cxlflash);
2984module_exit(exit_cxlflash);