blob: 7a787b6e21c4aaf4897db6fd205f9dce6faa119c [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
Uma Krishnana834a362017-06-21 21:15:18 -050037static struct class *cxlflash_class;
38static u32 cxlflash_major;
39static DECLARE_BITMAP(cxlflash_minor, CXLFLASH_MAX_ADAPTERS);
40
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050041/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050042 * process_cmd_err() - command error handler
43 * @cmd: AFU command that experienced the error.
44 * @scp: SCSI command associated with the AFU command in error.
45 *
46 * Translates error bits from AFU command to SCSI command results.
47 */
48static void process_cmd_err(struct afu_cmd *cmd, struct scsi_cmnd *scp)
49{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060050 struct afu *afu = cmd->parent;
51 struct cxlflash_cfg *cfg = afu->parent;
52 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050053 struct sisl_ioarcb *ioarcb;
54 struct sisl_ioasa *ioasa;
Matthew R. Ochs83960122015-10-21 15:13:29 -050055 u32 resid;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050056
57 if (unlikely(!cmd))
58 return;
59
60 ioarcb = &(cmd->rcb);
61 ioasa = &(cmd->sa);
62
63 if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) {
Matthew R. Ochs83960122015-10-21 15:13:29 -050064 resid = ioasa->resid;
65 scsi_set_resid(scp, resid);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060066 dev_dbg(dev, "%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
67 __func__, cmd, scp, resid);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050068 }
69
70 if (ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060071 dev_dbg(dev, "%s: cmd underrun cmd = %p scp = %p\n",
72 __func__, cmd, scp);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050073 scp->result = (DID_ERROR << 16);
74 }
75
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060076 dev_dbg(dev, "%s: cmd failed afu_rc=%02x scsi_rc=%02x fc_rc=%02x "
77 "afu_extra=%02x scsi_extra=%02x fc_extra=%02x\n", __func__,
78 ioasa->rc.afu_rc, ioasa->rc.scsi_rc, ioasa->rc.fc_rc,
79 ioasa->afu_extra, ioasa->scsi_extra, ioasa->fc_extra);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050080
81 if (ioasa->rc.scsi_rc) {
82 /* We have a SCSI status */
83 if (ioasa->rc.flags & SISL_RC_FLAGS_SENSE_VALID) {
84 memcpy(scp->sense_buffer, ioasa->sense_data,
85 SISL_SENSE_DATA_LEN);
86 scp->result = ioasa->rc.scsi_rc;
87 } else
88 scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16);
89 }
90
91 /*
92 * We encountered an error. Set scp->result based on nature
93 * of error.
94 */
95 if (ioasa->rc.fc_rc) {
96 /* We have an FC status */
97 switch (ioasa->rc.fc_rc) {
98 case SISL_FC_RC_LINKDOWN:
99 scp->result = (DID_REQUEUE << 16);
100 break;
101 case SISL_FC_RC_RESID:
102 /* This indicates an FCP resid underrun */
103 if (!(ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN)) {
104 /* If the SISL_RC_FLAGS_OVERRUN flag was set,
105 * then we will handle this error else where.
106 * If not then we must handle it here.
Matthew R. Ochs83960122015-10-21 15:13:29 -0500107 * This is probably an AFU bug.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500108 */
109 scp->result = (DID_ERROR << 16);
110 }
111 break;
112 case SISL_FC_RC_RESIDERR:
113 /* Resid mismatch between adapter and device */
114 case SISL_FC_RC_TGTABORT:
115 case SISL_FC_RC_ABORTOK:
116 case SISL_FC_RC_ABORTFAIL:
117 case SISL_FC_RC_NOLOGI:
118 case SISL_FC_RC_ABORTPEND:
119 case SISL_FC_RC_WRABORTPEND:
120 case SISL_FC_RC_NOEXP:
121 case SISL_FC_RC_INUSE:
122 scp->result = (DID_ERROR << 16);
123 break;
124 }
125 }
126
127 if (ioasa->rc.afu_rc) {
128 /* We have an AFU error */
129 switch (ioasa->rc.afu_rc) {
130 case SISL_AFU_RC_NO_CHANNELS:
Matthew R. Ochs83960122015-10-21 15:13:29 -0500131 scp->result = (DID_NO_CONNECT << 16);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500132 break;
133 case SISL_AFU_RC_DATA_DMA_ERR:
134 switch (ioasa->afu_extra) {
135 case SISL_AFU_DMA_ERR_PAGE_IN:
136 /* Retry */
137 scp->result = (DID_IMM_RETRY << 16);
138 break;
139 case SISL_AFU_DMA_ERR_INVALID_EA:
140 default:
141 scp->result = (DID_ERROR << 16);
142 }
143 break;
144 case SISL_AFU_RC_OUT_OF_DATA_BUFS:
145 /* Retry */
146 scp->result = (DID_ALLOC_FAILURE << 16);
147 break;
148 default:
149 scp->result = (DID_ERROR << 16);
150 }
151 }
152}
153
154/**
155 * cmd_complete() - command completion handler
156 * @cmd: AFU command that has completed.
157 *
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500158 * For SCSI commands this routine prepares and submits commands that have
159 * either completed or timed out to the SCSI stack. For internal commands
160 * (TMF or AFU), this routine simply notifies the originator that the
161 * command has completed.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500162 */
163static void cmd_complete(struct afu_cmd *cmd)
164{
165 struct scsi_cmnd *scp;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500166 ulong lock_flags;
167 struct afu *afu = cmd->parent;
168 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600169 struct device *dev = &cfg->dev->dev;
Uma Krishnana002bf82017-06-21 21:14:43 -0500170 struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500171
Uma Krishnana002bf82017-06-21 21:14:43 -0500172 spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
173 list_del(&cmd->list);
174 spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
175
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600176 if (cmd->scp) {
177 scp = cmd->scp;
Matthew R. Ochs83960122015-10-21 15:13:29 -0500178 if (unlikely(cmd->sa.ioasc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500179 process_cmd_err(cmd, scp);
180 else
181 scp->result = (DID_OK << 16);
182
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600183 dev_dbg_ratelimited(dev, "%s:scp=%p result=%08x ioasc=%08x\n",
184 __func__, scp, scp->result, cmd->sa.ioasc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500185 scp->scsi_done(scp);
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500186 } else if (cmd->cmd_tmf) {
187 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
188 cfg->tmf_active = false;
189 wake_up_all_locked(&cfg->tmf_waitq);
190 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500191 } else
192 complete(&cmd->cevent);
193}
194
195/**
Uma Krishnana1ea04b2017-06-21 21:14:56 -0500196 * flush_pending_cmds() - flush all pending commands on this hardware queue
197 * @hwq: Hardware queue to flush.
198 *
199 * The hardware send queue lock associated with this hardware queue must be
200 * held when calling this routine.
201 */
202static void flush_pending_cmds(struct hwq *hwq)
203{
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500204 struct cxlflash_cfg *cfg = hwq->afu->parent;
Uma Krishnana1ea04b2017-06-21 21:14:56 -0500205 struct afu_cmd *cmd, *tmp;
206 struct scsi_cmnd *scp;
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500207 ulong lock_flags;
Uma Krishnana1ea04b2017-06-21 21:14:56 -0500208
209 list_for_each_entry_safe(cmd, tmp, &hwq->pending_cmds, list) {
210 /* Bypass command when on a doneq, cmd_complete() will handle */
211 if (!list_empty(&cmd->queue))
212 continue;
213
214 list_del(&cmd->list);
215
216 if (cmd->scp) {
217 scp = cmd->scp;
218 scp->result = (DID_IMM_RETRY << 16);
219 scp->scsi_done(scp);
220 } else {
221 cmd->cmd_aborted = true;
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500222
223 if (cmd->cmd_tmf) {
224 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
225 cfg->tmf_active = false;
226 wake_up_all_locked(&cfg->tmf_waitq);
227 spin_unlock_irqrestore(&cfg->tmf_slock,
228 lock_flags);
229 } else
230 complete(&cmd->cevent);
Uma Krishnana1ea04b2017-06-21 21:14:56 -0500231 }
232 }
233}
234
235/**
Uma Krishnana96851d2017-06-21 21:14:02 -0500236 * context_reset() - reset context via specified register
237 * @hwq: Hardware queue owning the context to be reset.
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600238 * @reset_reg: MMIO register to perform reset.
Uma Krishnana96851d2017-06-21 21:14:02 -0500239 *
Uma Krishnan7c4c41f2017-06-21 21:15:06 -0500240 * When the reset is successful, the SISLite specification guarantees that
241 * the AFU has aborted all currently pending I/O. Accordingly, these commands
242 * must be flushed.
243 *
Uma Krishnana96851d2017-06-21 21:14:02 -0500244 * Return: 0 on success, -errno on failure
Matthew R. Ochs15305512015-10-21 15:12:10 -0500245 */
Uma Krishnana96851d2017-06-21 21:14:02 -0500246static int context_reset(struct hwq *hwq, __be64 __iomem *reset_reg)
Matthew R. Ochs15305512015-10-21 15:12:10 -0500247{
Uma Krishnana96851d2017-06-21 21:14:02 -0500248 struct cxlflash_cfg *cfg = hwq->afu->parent;
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600249 struct device *dev = &cfg->dev->dev;
Uma Krishnana96851d2017-06-21 21:14:02 -0500250 int rc = -ETIMEDOUT;
251 int nretry = 0;
252 u64 val = 0x1;
Uma Krishnan7c4c41f2017-06-21 21:15:06 -0500253 ulong lock_flags;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500254
Uma Krishnana96851d2017-06-21 21:14:02 -0500255 dev_dbg(dev, "%s: hwq=%p\n", __func__, hwq);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500256
Uma Krishnan7c4c41f2017-06-21 21:15:06 -0500257 spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
258
Uma Krishnana96851d2017-06-21 21:14:02 -0500259 writeq_be(val, reset_reg);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500260 do {
Uma Krishnana96851d2017-06-21 21:14:02 -0500261 val = readq_be(reset_reg);
262 if ((val & 0x1) == 0x0) {
263 rc = 0;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500264 break;
Uma Krishnana96851d2017-06-21 21:14:02 -0500265 }
266
Matthew R. Ochs15305512015-10-21 15:12:10 -0500267 /* Double delay each time */
Manoj N. Kumarea765432016-03-25 14:26:49 -0500268 udelay(1 << nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500269 } while (nretry++ < MC_ROOM_RETRY_CNT);
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600270
Uma Krishnan7c4c41f2017-06-21 21:15:06 -0500271 if (!rc)
272 flush_pending_cmds(hwq);
273
274 spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
275
Uma Krishnana96851d2017-06-21 21:14:02 -0500276 dev_dbg(dev, "%s: returning rc=%d, val=%016llx nretry=%d\n",
277 __func__, rc, val, nretry);
278 return rc;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500279}
280
281/**
Uma Krishnana96851d2017-06-21 21:14:02 -0500282 * context_reset_ioarrin() - reset context via IOARRIN register
283 * @hwq: Hardware queue owning the context to be reset.
284 *
285 * Return: 0 on success, -errno on failure
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600286 */
Uma Krishnana96851d2017-06-21 21:14:02 -0500287static int context_reset_ioarrin(struct hwq *hwq)
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600288{
Uma Krishnana96851d2017-06-21 21:14:02 -0500289 return context_reset(hwq, &hwq->host_map->ioarrin);
Matthew R. Ochs9c7d1ee2017-01-11 19:19:08 -0600290}
291
292/**
Uma Krishnana96851d2017-06-21 21:14:02 -0500293 * context_reset_sq() - reset context via SQ_CONTEXT_RESET register
294 * @hwq: Hardware queue owning the context to be reset.
295 *
296 * Return: 0 on success, -errno on failure
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600297 */
Uma Krishnana96851d2017-06-21 21:14:02 -0500298static int context_reset_sq(struct hwq *hwq)
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600299{
Uma Krishnana96851d2017-06-21 21:14:02 -0500300 return context_reset(hwq, &hwq->host_map->sq_ctx_reset);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600301}
302
303/**
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600304 * send_cmd_ioarrin() - sends an AFU command via IOARRIN register
Matthew R. Ochs15305512015-10-21 15:12:10 -0500305 * @afu: AFU associated with the host.
306 * @cmd: AFU command to send.
307 *
308 * Return:
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500309 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochs15305512015-10-21 15:12:10 -0500310 */
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600311static int send_cmd_ioarrin(struct afu *afu, struct afu_cmd *cmd)
Matthew R. Ochs15305512015-10-21 15:12:10 -0500312{
313 struct cxlflash_cfg *cfg = afu->parent;
314 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500315 struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500316 int rc = 0;
Uma Krishnan11f7b182016-11-28 18:41:45 -0600317 s64 room;
318 ulong lock_flags;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500319
320 /*
Uma Krishnan11f7b182016-11-28 18:41:45 -0600321 * To avoid the performance penalty of MMIO, spread the update of
322 * 'room' over multiple commands.
Matthew R. Ochs15305512015-10-21 15:12:10 -0500323 */
Uma Krishnan66ea9bc2017-06-21 21:13:32 -0500324 spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500325 if (--hwq->room < 0) {
326 room = readq_be(&hwq->host_map->cmd_room);
Uma Krishnan11f7b182016-11-28 18:41:45 -0600327 if (room <= 0) {
328 dev_dbg_ratelimited(dev, "%s: no cmd_room to send "
329 "0x%02X, room=0x%016llX\n",
330 __func__, cmd->rcb.cdb[0], room);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500331 hwq->room = 0;
Uma Krishnan11f7b182016-11-28 18:41:45 -0600332 rc = SCSI_MLQUEUE_HOST_BUSY;
333 goto out;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500334 }
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500335 hwq->room = room - 1;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500336 }
337
Uma Krishnana002bf82017-06-21 21:14:43 -0500338 list_add(&cmd->list, &hwq->pending_cmds);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500339 writeq_be((u64)&cmd->rcb, &hwq->host_map->ioarrin);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500340out:
Uma Krishnan66ea9bc2017-06-21 21:13:32 -0500341 spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600342 dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx rc=%d\n", __func__,
343 cmd, cmd->rcb.data_len, cmd->rcb.data_ea, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500344 return rc;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500345}
346
347/**
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600348 * send_cmd_sq() - sends an AFU command via SQ ring
349 * @afu: AFU associated with the host.
350 * @cmd: AFU command to send.
351 *
352 * Return:
353 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
354 */
355static int send_cmd_sq(struct afu *afu, struct afu_cmd *cmd)
356{
357 struct cxlflash_cfg *cfg = afu->parent;
358 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500359 struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600360 int rc = 0;
361 int newval;
362 ulong lock_flags;
363
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500364 newval = atomic_dec_if_positive(&hwq->hsq_credits);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600365 if (newval <= 0) {
366 rc = SCSI_MLQUEUE_HOST_BUSY;
367 goto out;
368 }
369
370 cmd->rcb.ioasa = &cmd->sa;
371
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500372 spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600373
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500374 *hwq->hsq_curr = cmd->rcb;
375 if (hwq->hsq_curr < hwq->hsq_end)
376 hwq->hsq_curr++;
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600377 else
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500378 hwq->hsq_curr = hwq->hsq_start;
Uma Krishnana002bf82017-06-21 21:14:43 -0500379
380 list_add(&cmd->list, &hwq->pending_cmds);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500381 writeq_be((u64)hwq->hsq_curr, &hwq->host_map->sq_tail);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600382
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500383 spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600384out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600385 dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx ioasa=%p rc=%d curr=%p "
386 "head=%016llx tail=%016llx\n", __func__, cmd, cmd->rcb.data_len,
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500387 cmd->rcb.data_ea, cmd->rcb.ioasa, rc, hwq->hsq_curr,
388 readq_be(&hwq->host_map->sq_head),
389 readq_be(&hwq->host_map->sq_tail));
Matthew R. Ochs696d0b02017-01-11 19:19:33 -0600390 return rc;
391}
392
393/**
Matthew R. Ochs15305512015-10-21 15:12:10 -0500394 * wait_resp() - polls for a response or timeout to a sent AFU command
395 * @afu: AFU associated with the host.
396 * @cmd: AFU command that was sent.
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600397 *
Uma Krishnana96851d2017-06-21 21:14:02 -0500398 * Return: 0 on success, -errno on failure
Matthew R. Ochs15305512015-10-21 15:12:10 -0500399 */
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600400static int wait_resp(struct afu *afu, struct afu_cmd *cmd)
Matthew R. Ochs15305512015-10-21 15:12:10 -0500401{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600402 struct cxlflash_cfg *cfg = afu->parent;
403 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600404 int rc = 0;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500405 ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
406
407 timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
Uma Krishnana96851d2017-06-21 21:14:02 -0500408 if (!timeout)
409 rc = -ETIMEDOUT;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500410
Uma Krishnana1ea04b2017-06-21 21:14:56 -0500411 if (cmd->cmd_aborted)
412 rc = -EAGAIN;
413
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600414 if (unlikely(cmd->sa.ioasc != 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600415 dev_err(dev, "%s: cmd %02x failed, ioasc=%08x\n",
416 __func__, cmd->rcb.cdb[0], cmd->sa.ioasc);
Uma Krishnana96851d2017-06-21 21:14:02 -0500417 rc = -EIO;
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -0600418 }
419
420 return rc;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500421}
422
423/**
Matthew R. Ochs1dd0c0e2017-04-12 14:16:02 -0500424 * cmd_to_target_hwq() - selects a target hardware queue for a SCSI command
425 * @host: SCSI host associated with device.
426 * @scp: SCSI command to send.
427 * @afu: SCSI command to send.
428 *
429 * Hashes a command based upon the hardware queue mode.
430 *
431 * Return: Trusted index of target hardware queue
432 */
433static u32 cmd_to_target_hwq(struct Scsi_Host *host, struct scsi_cmnd *scp,
434 struct afu *afu)
435{
436 u32 tag;
437 u32 hwq = 0;
438
439 if (afu->num_hwqs == 1)
440 return 0;
441
442 switch (afu->hwq_mode) {
443 case HWQ_MODE_RR:
444 hwq = afu->hwq_rr_count++ % afu->num_hwqs;
445 break;
446 case HWQ_MODE_TAG:
447 tag = blk_mq_unique_tag(scp->request);
448 hwq = blk_mq_unique_tag_to_hwq(tag);
449 break;
450 case HWQ_MODE_CPU:
451 hwq = smp_processor_id() % afu->num_hwqs;
452 break;
453 default:
454 WARN_ON_ONCE(1);
455 }
456
457 return hwq;
458}
459
460/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500461 * send_tmf() - sends a Task Management Function (TMF)
462 * @afu: AFU to checkout from.
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500463 * @scp: SCSI command from stack describing target.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500464 * @tmfcmd: TMF command to send.
465 *
466 * Return:
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500467 * 0 on success, SCSI_MLQUEUE_HOST_BUSY or -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500468 */
469static int send_tmf(struct afu *afu, struct scsi_cmnd *scp, u64 tmfcmd)
470{
Matthew R. Ochs1dd0c0e2017-04-12 14:16:02 -0500471 struct Scsi_Host *host = scp->device->host;
472 struct cxlflash_cfg *cfg = shost_priv(host);
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500473 struct afu_cmd *cmd = NULL;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500474 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs1dd0c0e2017-04-12 14:16:02 -0500475 int hwq_index = cmd_to_target_hwq(host, scp, afu);
476 struct hwq *hwq = get_hwq(afu, hwq_index);
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500477 char *buf = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500478 ulong lock_flags;
479 int rc = 0;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500480 ulong to;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500481
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500482 buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
483 if (unlikely(!buf)) {
484 dev_err(dev, "%s: no memory for command\n", __func__);
485 rc = -ENOMEM;
486 goto out;
487 }
488
489 cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
490 INIT_LIST_HEAD(&cmd->queue);
491
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500492 /* When Task Management Function is active do not send another */
493 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500494 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500495 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
496 !cfg->tmf_active,
497 cfg->tmf_slock);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500498 cfg->tmf_active = true;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500499 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500500
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600501 cmd->parent = afu;
502 cmd->cmd_tmf = true;
Matthew R. Ochs1dd0c0e2017-04-12 14:16:02 -0500503 cmd->hwq_index = hwq_index;
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600504
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500505 cmd->rcb.ctx_id = hwq->ctx_hndl;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600506 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -0500507 cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500508 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500509 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
Matthew R. Ochsd4ace352016-11-28 18:42:50 -0600510 SISL_REQ_FLAGS_SUP_UNDERRUN |
511 SISL_REQ_FLAGS_TMF_CMD);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500512 memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
513
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600514 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500515 if (unlikely(rc)) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500516 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500517 cfg->tmf_active = false;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500518 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500519 goto out;
520 }
521
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500522 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
523 to = msecs_to_jiffies(5000);
524 to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
525 !cfg->tmf_active,
526 cfg->tmf_slock,
527 to);
528 if (!to) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600529 dev_err(dev, "%s: TMF timed out\n", __func__);
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500530 rc = -ETIMEDOUT;
531 } else if (cmd->cmd_aborted) {
532 dev_err(dev, "%s: TMF aborted\n", __func__);
533 rc = -EAGAIN;
534 } else if (cmd->sa.ioasc) {
535 dev_err(dev, "%s: TMF failed ioasc=%08x\n",
536 __func__, cmd->sa.ioasc);
537 rc = -EIO;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500538 }
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500539 cfg->tmf_active = false;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500540 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500541out:
Matthew R. Ochs8ba1ddb2017-06-21 21:16:54 -0500542 kfree(buf);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500543 return rc;
544}
545
546/**
547 * cxlflash_driver_info() - information handler for this host driver
548 * @host: SCSI host associated with device.
549 *
550 * Return: A string describing the device.
551 */
552static const char *cxlflash_driver_info(struct Scsi_Host *host)
553{
554 return CXLFLASH_ADAPTER_NAME;
555}
556
557/**
558 * cxlflash_queuecommand() - sends a mid-layer request
559 * @host: SCSI host associated with device.
560 * @scp: SCSI command to send.
561 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500562 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500563 */
564static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
565{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600566 struct cxlflash_cfg *cfg = shost_priv(host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500567 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500568 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs479ad8e2017-06-21 21:16:44 -0500569 struct afu_cmd *cmd = sc_to_afuci(scp);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600570 struct scatterlist *sg = scsi_sglist(scp);
Matthew R. Ochs1dd0c0e2017-04-12 14:16:02 -0500571 int hwq_index = cmd_to_target_hwq(host, scp, afu);
572 struct hwq *hwq = get_hwq(afu, hwq_index);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600573 u16 req_flags = SISL_REQ_FLAGS_SUP_UNDERRUN;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500574 ulong lock_flags;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500575 int rc = 0;
576
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500577 dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600578 "cdb=(%08x-%08x-%08x-%08x)\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500579 __func__, scp, host->host_no, scp->device->channel,
580 scp->device->id, scp->device->lun,
581 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
582 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
583 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
584 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500585
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500586 /*
587 * If a Task Management Function is active, wait for it to complete
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500588 * before continuing with regular commands.
589 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500590 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500591 if (cfg->tmf_active) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500592 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500593 rc = SCSI_MLQUEUE_HOST_BUSY;
594 goto out;
595 }
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500596 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500597
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500598 switch (cfg->state) {
Matthew R. Ochs323e3342017-04-12 14:14:51 -0500599 case STATE_PROBING:
600 case STATE_PROBED:
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500601 case STATE_RESET:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600602 dev_dbg_ratelimited(dev, "%s: device is in reset\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500603 rc = SCSI_MLQUEUE_HOST_BUSY;
604 goto out;
605 case STATE_FAILTERM:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600606 dev_dbg_ratelimited(dev, "%s: device has failed\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500607 scp->result = (DID_NO_CONNECT << 16);
608 scp->scsi_done(scp);
609 rc = 0;
610 goto out;
611 default:
612 break;
613 }
614
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600615 if (likely(sg)) {
Matthew R. Ochs50b787f2017-04-12 14:15:02 -0500616 cmd->rcb.data_len = sg->length;
617 cmd->rcb.data_ea = (uintptr_t)sg_virt(sg);
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600618 }
619
Matthew R. Ochsfe7f9692016-11-28 18:43:18 -0600620 cmd->scp = scp;
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600621 cmd->parent = afu;
Matthew R. Ochs1dd0c0e2017-04-12 14:16:02 -0500622 cmd->hwq_index = hwq_index;
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600623
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500624 cmd->rcb.ctx_id = hwq->ctx_hndl;
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -0600625 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -0500626 cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500627 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
628
629 if (scp->sc_data_direction == DMA_TO_DEVICE)
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600630 req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500631
Matthew R. Ochs9d893262016-11-28 18:43:01 -0600632 cmd->rcb.req_flags = req_flags;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500633 memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
634
Matthew R. Ochs48b4be32016-11-28 18:43:09 -0600635 rc = afu->send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500636out:
637 return rc;
638}
639
640/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500641 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500642 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500643 */
644static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
645{
646 struct pci_dev *pdev = cfg->dev;
647
648 if (pci_channel_offline(pdev))
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500649 wait_event_timeout(cfg->reset_waitq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500650 !pci_channel_offline(pdev),
651 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
652}
653
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500654/**
655 * free_mem() - free memory associated with the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500656 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500657 */
658static void free_mem(struct cxlflash_cfg *cfg)
659{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500660 struct afu *afu = cfg->afu;
661
662 if (cfg->afu) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500663 free_pages((ulong)afu, get_order(sizeof(struct afu)));
664 cfg->afu = NULL;
665 }
666}
667
668/**
Uma Krishnan0b09e712017-06-21 21:14:17 -0500669 * cxlflash_reset_sync() - synchronizing point for asynchronous resets
670 * @cfg: Internal structure associated with the host.
671 */
672static void cxlflash_reset_sync(struct cxlflash_cfg *cfg)
673{
674 if (cfg->async_reset_cookie == 0)
675 return;
676
677 /* Wait until all async calls prior to this cookie have completed */
678 async_synchronize_cookie(cfg->async_reset_cookie + 1);
679 cfg->async_reset_cookie = 0;
680}
681
682/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500683 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500684 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500685 *
686 * Safe to call with AFU in a partially allocated/initialized state.
Manoj Kumaree91e332015-12-14 15:07:02 -0600687 *
Uma Krishnan0df5bef2017-01-11 19:20:03 -0600688 * Cancels scheduled worker threads, waits for any active internal AFU
Matthew R. Ochscba06e62017-04-12 14:13:20 -0500689 * commands to timeout, disables IRQ polling and then unmaps the MMIO space.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500690 */
691static void stop_afu(struct cxlflash_cfg *cfg)
692{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500693 struct afu *afu = cfg->afu;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500694 struct hwq *hwq;
695 int i;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500696
Uma Krishnan0df5bef2017-01-11 19:20:03 -0600697 cancel_work_sync(&cfg->work_q);
Uma Krishnan0b09e712017-06-21 21:14:17 -0500698 if (!current_is_async())
699 cxlflash_reset_sync(cfg);
Uma Krishnan0df5bef2017-01-11 19:20:03 -0600700
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500701 if (likely(afu)) {
Matthew R. Ochsde012832016-11-28 18:42:33 -0600702 while (atomic_read(&afu->cmds_active))
703 ssleep(1);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500704
705 if (afu_is_irqpoll_enabled(afu)) {
Matthew R. Ochs30652672017-04-12 14:15:53 -0500706 for (i = 0; i < afu->num_hwqs; i++) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500707 hwq = get_hwq(afu, i);
708
709 irq_poll_disable(&hwq->irqpoll);
710 }
711 }
712
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500713 if (likely(afu->afu_map)) {
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500714 cxl_psa_unmap((void __iomem *)afu->afu_map);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500715 afu->afu_map = NULL;
716 }
717 }
718}
719
720/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500721 * term_intr() - disables all AFU interrupts
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500722 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500723 * @level: Depth of allocation, where to begin waterfall tear down.
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500724 * @index: Index of the hardware queue.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500725 *
726 * Safe to call with AFU/MC in partially allocated/initialized state.
727 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500728static void term_intr(struct cxlflash_cfg *cfg, enum undo_level level,
729 u32 index)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500730{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500731 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500732 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500733 struct hwq *hwq;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500734
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500735 if (!afu) {
736 dev_err(dev, "%s: returning with NULL afu\n", __func__);
737 return;
738 }
739
740 hwq = get_hwq(afu, index);
741
742 if (!hwq->ctx) {
743 dev_err(dev, "%s: returning with NULL MC\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500744 return;
745 }
746
747 switch (level) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500748 case UNMAP_THREE:
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500749 /* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
750 if (index == PRIMARY_HWQ)
751 cxl_unmap_afu_irq(hwq->ctx, 3, hwq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500752 case UNMAP_TWO:
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500753 cxl_unmap_afu_irq(hwq->ctx, 2, hwq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500754 case UNMAP_ONE:
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500755 cxl_unmap_afu_irq(hwq->ctx, 1, hwq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500756 case FREE_IRQ:
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500757 cxl_free_afu_irqs(hwq->ctx);
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500758 /* fall through */
759 case UNDO_NOOP:
760 /* No action required */
761 break;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500762 }
763}
764
765/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500766 * term_mc() - terminates the master context
767 * @cfg: Internal structure associated with the host.
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500768 * @index: Index of the hardware queue.
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500769 *
770 * Safe to call with AFU/MC in partially allocated/initialized state.
771 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500772static void term_mc(struct cxlflash_cfg *cfg, u32 index)
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500773{
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500774 struct afu *afu = cfg->afu;
775 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500776 struct hwq *hwq;
Uma Krishnana1ea04b2017-06-21 21:14:56 -0500777 ulong lock_flags;
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500778
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500779 if (!afu) {
780 dev_err(dev, "%s: returning with NULL afu\n", __func__);
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500781 return;
782 }
783
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500784 hwq = get_hwq(afu, index);
785
786 if (!hwq->ctx) {
787 dev_err(dev, "%s: returning with NULL MC\n", __func__);
788 return;
789 }
790
791 WARN_ON(cxl_stop_context(hwq->ctx));
792 if (index != PRIMARY_HWQ)
793 WARN_ON(cxl_release_context(hwq->ctx));
794 hwq->ctx = NULL;
Uma Krishnana1ea04b2017-06-21 21:14:56 -0500795
796 spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
797 flush_pending_cmds(hwq);
798 spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500799}
800
801/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500802 * term_afu() - terminates the AFU
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 * Safe to call with AFU/MC in partially allocated/initialized state.
806 */
807static void term_afu(struct cxlflash_cfg *cfg)
808{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600809 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500810 int k;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600811
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500812 /*
813 * Tear down is carefully orchestrated to ensure
814 * no interrupts can come in when the problem state
815 * area is unmapped.
816 *
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500817 * 1) Disable all AFU interrupts for each master
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500818 * 2) Unmap the problem state area
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500819 * 3) Stop each master context
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500820 */
Matthew R. Ochs30652672017-04-12 14:15:53 -0500821 for (k = cfg->afu->num_hwqs - 1; k >= 0; k--)
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500822 term_intr(cfg, UNMAP_THREE, k);
823
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500824 if (cfg->afu)
825 stop_afu(cfg);
826
Matthew R. Ochs30652672017-04-12 14:15:53 -0500827 for (k = cfg->afu->num_hwqs - 1; k >= 0; k--)
Uma Krishnanbfc0bab2017-04-12 14:15:42 -0500828 term_mc(cfg, k);
Uma Krishnan6ded8b32016-03-04 15:55:15 -0600829
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600830 dev_dbg(dev, "%s: returning\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500831}
832
833/**
Uma Krishnan704c4b02016-06-15 18:49:57 -0500834 * notify_shutdown() - notifies device of pending shutdown
835 * @cfg: Internal structure associated with the host.
836 * @wait: Whether to wait for shutdown processing to complete.
837 *
838 * This function will notify the AFU that the adapter is being shutdown
839 * and will wait for shutdown processing to complete if wait is true.
840 * This notification should flush pending I/Os to the device and halt
841 * further I/Os until the next AFU reset is issued and device restarted.
842 */
843static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
844{
845 struct afu *afu = cfg->afu;
846 struct device *dev = &cfg->dev->dev;
Uma Krishnan704c4b02016-06-15 18:49:57 -0500847 struct dev_dependent_vals *ddv;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500848 __be64 __iomem *fc_port_regs;
Uma Krishnan704c4b02016-06-15 18:49:57 -0500849 u64 reg, status;
850 int i, retry_cnt = 0;
851
852 ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
853 if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
854 return;
855
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500856 if (!afu || !afu->afu_map) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600857 dev_dbg(dev, "%s: Problem state area not mapped\n", __func__);
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500858 return;
859 }
860
Uma Krishnan704c4b02016-06-15 18:49:57 -0500861 /* Notify AFU */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -0500862 for (i = 0; i < cfg->num_fc_ports; i++) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500863 fc_port_regs = get_fc_port_regs(cfg, i);
864
865 reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
Uma Krishnan704c4b02016-06-15 18:49:57 -0500866 reg |= SISL_FC_SHUTDOWN_NORMAL;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500867 writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
Uma Krishnan704c4b02016-06-15 18:49:57 -0500868 }
869
870 if (!wait)
871 return;
872
873 /* Wait up to 1.5 seconds for shutdown processing to complete */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -0500874 for (i = 0; i < cfg->num_fc_ports; i++) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500875 fc_port_regs = get_fc_port_regs(cfg, i);
Uma Krishnan704c4b02016-06-15 18:49:57 -0500876 retry_cnt = 0;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500877
Uma Krishnan704c4b02016-06-15 18:49:57 -0500878 while (true) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -0500879 status = readq_be(&fc_port_regs[FC_STATUS / 8]);
Uma Krishnan704c4b02016-06-15 18:49:57 -0500880 if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
881 break;
882 if (++retry_cnt >= MC_RETRY_CNT) {
883 dev_dbg(dev, "%s: port %d shutdown processing "
884 "not yet completed\n", __func__, i);
885 break;
886 }
887 msleep(100 * retry_cnt);
888 }
889 }
890}
891
892/**
Uma Krishnana834a362017-06-21 21:15:18 -0500893 * cxlflash_get_minor() - gets the first available minor number
894 *
895 * Return: Unique minor number that can be used to create the character device.
896 */
897static int cxlflash_get_minor(void)
898{
899 int minor;
900 long bit;
901
902 bit = find_first_zero_bit(cxlflash_minor, CXLFLASH_MAX_ADAPTERS);
903 if (bit >= CXLFLASH_MAX_ADAPTERS)
904 return -1;
905
906 minor = bit & MINORMASK;
907 set_bit(minor, cxlflash_minor);
908 return minor;
909}
910
911/**
912 * cxlflash_put_minor() - releases the minor number
913 * @minor: Minor number that is no longer needed.
914 */
915static void cxlflash_put_minor(int minor)
916{
917 clear_bit(minor, cxlflash_minor);
918}
919
920/**
921 * cxlflash_release_chrdev() - release the character device for the host
922 * @cfg: Internal structure associated with the host.
923 */
924static void cxlflash_release_chrdev(struct cxlflash_cfg *cfg)
925{
926 put_device(cfg->chardev);
927 device_unregister(cfg->chardev);
928 cfg->chardev = NULL;
929 cdev_del(&cfg->cdev);
930 cxlflash_put_minor(MINOR(cfg->cdev.dev));
931}
932
933/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500934 * cxlflash_remove() - PCI entry point to tear down host
935 * @pdev: PCI device associated with the host.
936 *
Matthew R. Ochs323e3342017-04-12 14:14:51 -0500937 * Safe to use as a cleanup in partially allocated/initialized state. Note that
938 * the reset_waitq is flushed as part of the stop/termination of user contexts.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500939 */
940static void cxlflash_remove(struct pci_dev *pdev)
941{
942 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600943 struct device *dev = &pdev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500944 ulong lock_flags;
945
Uma Krishnanbabf9852016-09-02 15:39:16 -0500946 if (!pci_is_enabled(pdev)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600947 dev_dbg(dev, "%s: Device is disabled\n", __func__);
Uma Krishnanbabf9852016-09-02 15:39:16 -0500948 return;
949 }
950
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500951 /* If a Task Management Function is active, wait for it to complete
952 * before continuing with remove.
953 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500954 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500955 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500956 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
957 !cfg->tmf_active,
958 cfg->tmf_slock);
959 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500960
Uma Krishnan704c4b02016-06-15 18:49:57 -0500961 /* Notify AFU and wait for shutdown processing to complete */
962 notify_shutdown(cfg, true);
963
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500964 cfg->state = STATE_FAILTERM;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500965 cxlflash_stop_term_user_contexts(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500966
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500967 switch (cfg->init_state) {
Uma Krishnana834a362017-06-21 21:15:18 -0500968 case INIT_STATE_CDEV:
969 cxlflash_release_chrdev(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500970 case INIT_STATE_SCSI:
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500971 cxlflash_term_local_luns(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500972 scsi_remove_host(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500973 case INIT_STATE_AFU:
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600974 term_afu(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500975 case INIT_STATE_PCI:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500976 pci_disable_device(pdev);
977 case INIT_STATE_NONE:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500978 free_mem(cfg);
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -0500979 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500980 break;
981 }
982
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600983 dev_dbg(dev, "%s: returning\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500984}
985
986/**
987 * alloc_mem() - allocates the AFU and its command pool
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500988 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500989 *
990 * A partially allocated state remains on failure.
991 *
992 * Return:
993 * 0 on success
994 * -ENOMEM on failure to allocate memory
995 */
996static int alloc_mem(struct cxlflash_cfg *cfg)
997{
998 int rc = 0;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500999 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001000
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001001 /* AFU is ~28k, i.e. only one 64k page or up to seven 4k pages */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001002 cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1003 get_order(sizeof(struct afu)));
1004 if (unlikely(!cfg->afu)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001005 dev_err(dev, "%s: cannot get %d free pages\n",
1006 __func__, get_order(sizeof(struct afu)));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001007 rc = -ENOMEM;
1008 goto out;
1009 }
1010 cfg->afu->parent = cfg;
Matthew R. Ochs30652672017-04-12 14:15:53 -05001011 cfg->afu->desired_hwqs = CXLFLASH_DEF_HWQS;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001012 cfg->afu->afu_map = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001013out:
1014 return rc;
1015}
1016
1017/**
1018 * init_pci() - initializes the host as a PCI device
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001019 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001020 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001021 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001022 */
1023static int init_pci(struct cxlflash_cfg *cfg)
1024{
1025 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001026 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001027 int rc = 0;
1028
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001029 rc = pci_enable_device(pdev);
1030 if (rc || pci_channel_offline(pdev)) {
1031 if (pci_channel_offline(pdev)) {
1032 cxlflash_wait_for_pci_err_recovery(cfg);
1033 rc = pci_enable_device(pdev);
1034 }
1035
1036 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001037 dev_err(dev, "%s: Cannot enable adapter\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001038 cxlflash_wait_for_pci_err_recovery(cfg);
Manoj N. Kumar961487e2016-03-04 15:55:14 -06001039 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001040 }
1041 }
1042
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001043out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001044 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001045 return rc;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001046}
1047
1048/**
1049 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001050 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001051 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001052 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001053 */
1054static int init_scsi(struct cxlflash_cfg *cfg)
1055{
1056 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001057 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001058 int rc = 0;
1059
1060 rc = scsi_add_host(cfg->host, &pdev->dev);
1061 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001062 dev_err(dev, "%s: scsi_add_host failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001063 goto out;
1064 }
1065
1066 scsi_scan_host(cfg->host);
1067
1068out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001069 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001070 return rc;
1071}
1072
1073/**
1074 * set_port_online() - transitions the specified host FC port to online state
1075 * @fc_regs: Top of MMIO region defined for specified port.
1076 *
1077 * The provided MMIO region must be mapped prior to call. Online state means
1078 * that the FC link layer has synced, completed the handshaking process, and
1079 * is ready for login to start.
1080 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001081static void set_port_online(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001082{
1083 u64 cmdcfg;
1084
1085 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
1086 cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
1087 cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
1088 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
1089}
1090
1091/**
1092 * set_port_offline() - transitions the specified host FC port to offline state
1093 * @fc_regs: Top of MMIO region defined for specified port.
1094 *
1095 * The provided MMIO region must be mapped prior to call.
1096 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001097static void set_port_offline(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001098{
1099 u64 cmdcfg;
1100
1101 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
1102 cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
1103 cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
1104 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
1105}
1106
1107/**
1108 * wait_port_online() - waits for the specified host FC port come online
1109 * @fc_regs: Top of MMIO region defined for specified port.
1110 * @delay_us: Number of microseconds to delay between reading port status.
1111 * @nretry: Number of cycles to retry reading port status.
1112 *
1113 * The provided MMIO region must be mapped prior to call. This will timeout
1114 * when the cable is not plugged in.
1115 *
1116 * Return:
1117 * TRUE (1) when the specified port is online
1118 * FALSE (0) when the specified port fails to come online after timeout
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001119 */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001120static bool wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001121{
1122 u64 status;
1123
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001124 WARN_ON(delay_us < 1000);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001125
1126 do {
1127 msleep(delay_us / 1000);
1128 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -05001129 if (status == U64_MAX)
1130 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001131 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
1132 nretry--);
1133
1134 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
1135}
1136
1137/**
1138 * wait_port_offline() - waits for the specified host FC port go offline
1139 * @fc_regs: Top of MMIO region defined for specified port.
1140 * @delay_us: Number of microseconds to delay between reading port status.
1141 * @nretry: Number of cycles to retry reading port status.
1142 *
1143 * The provided MMIO region must be mapped prior to call.
1144 *
1145 * Return:
1146 * TRUE (1) when the specified port is offline
1147 * FALSE (0) when the specified port fails to go offline after timeout
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001148 */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001149static bool wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001150{
1151 u64 status;
1152
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001153 WARN_ON(delay_us < 1000);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001154
1155 do {
1156 msleep(delay_us / 1000);
1157 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -05001158 if (status == U64_MAX)
1159 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001160 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
1161 nretry--);
1162
1163 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
1164}
1165
1166/**
1167 * afu_set_wwpn() - configures the WWPN for the specified host FC port
1168 * @afu: AFU associated with the host that owns the specified FC port.
1169 * @port: Port number being configured.
1170 * @fc_regs: Top of MMIO region defined for specified port.
1171 * @wwpn: The world-wide-port-number previously discovered for port.
1172 *
1173 * The provided MMIO region must be mapped prior to call. As part of the
1174 * sequence to configure the WWPN, the port is toggled offline and then back
1175 * online. This toggling action can cause this routine to delay up to a few
1176 * seconds. When configured to use the internal LUN feature of the AFU, a
1177 * failure to come online is overridden.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001178 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001179static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
1180 u64 wwpn)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001181{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001182 struct cxlflash_cfg *cfg = afu->parent;
1183 struct device *dev = &cfg->dev->dev;
1184
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001185 set_port_offline(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001186 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1187 FC_PORT_STATUS_RETRY_CNT)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001188 dev_dbg(dev, "%s: wait on port %d to go offline timed out\n",
1189 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001190 }
1191
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001192 writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
Matthew R. Ochs964497b2015-10-21 15:13:54 -05001193
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001194 set_port_online(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001195 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1196 FC_PORT_STATUS_RETRY_CNT)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001197 dev_dbg(dev, "%s: wait on port %d to go online timed out\n",
1198 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001199 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001200}
1201
1202/**
1203 * afu_link_reset() - resets the specified host FC port
1204 * @afu: AFU associated with the host that owns the specified FC port.
1205 * @port: Port number being configured.
1206 * @fc_regs: Top of MMIO region defined for specified port.
1207 *
1208 * The provided MMIO region must be mapped prior to call. The sequence to
1209 * reset the port involves toggling it offline and then back online. This
1210 * action can cause this routine to delay up to a few seconds. An effort
1211 * is made to maintain link with the device by switching to host to use
1212 * the alternate port exclusively while the reset takes place.
1213 * failure to come online is overridden.
1214 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001215static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001216{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001217 struct cxlflash_cfg *cfg = afu->parent;
1218 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001219 u64 port_sel;
1220
1221 /* first switch the AFU to the other links, if any */
1222 port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
Dan Carpenter4da74db2015-08-18 11:57:43 +03001223 port_sel &= ~(1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001224 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1225 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1226
1227 set_port_offline(fc_regs);
1228 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1229 FC_PORT_STATUS_RETRY_CNT))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001230 dev_err(dev, "%s: wait on port %d to go offline timed out\n",
1231 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001232
1233 set_port_online(fc_regs);
1234 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1235 FC_PORT_STATUS_RETRY_CNT))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001236 dev_err(dev, "%s: wait on port %d to go online timed out\n",
1237 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001238
1239 /* switch back to include this port */
Dan Carpenter4da74db2015-08-18 11:57:43 +03001240 port_sel |= (1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001241 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1242 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1243
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001244 dev_dbg(dev, "%s: returning port_sel=%016llx\n", __func__, port_sel);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001245}
1246
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001247/**
1248 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1249 * @afu: AFU associated with the host.
1250 */
1251static void afu_err_intr_init(struct afu *afu)
1252{
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001253 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001254 __be64 __iomem *fc_port_regs;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001255 int i;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001256 struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001257 u64 reg;
1258
1259 /* global async interrupts: AFU clears afu_ctrl on context exit
1260 * if async interrupts were sent to that context. This prevents
1261 * the AFU form sending further async interrupts when
1262 * there is
1263 * nobody to receive them.
1264 */
1265
1266 /* mask all */
1267 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001268 /* set LISN# to send and point to primary master context */
1269 reg = ((u64) (((hwq->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001270
1271 if (afu->internal_lun)
1272 reg |= 1; /* Bit 63 indicates local lun */
1273 writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1274 /* clear all */
1275 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1276 /* unmask bits that are of interest */
1277 /* note: afu can send an interrupt after this step */
1278 writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1279 /* clear again in case a bit came on after previous clear but before */
1280 /* unmask */
1281 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1282
1283 /* Clear/Set internal lun bits */
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001284 fc_port_regs = get_fc_port_regs(cfg, 0);
1285 reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001286 reg &= SISL_FC_INTERNAL_MASK;
1287 if (afu->internal_lun)
1288 reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001289 writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001290
1291 /* now clear FC errors */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001292 for (i = 0; i < cfg->num_fc_ports; i++) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001293 fc_port_regs = get_fc_port_regs(cfg, i);
1294
1295 writeq_be(0xFFFFFFFFU, &fc_port_regs[FC_ERROR / 8]);
1296 writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001297 }
1298
1299 /* sync interrupts for master's IOARRIN write */
1300 /* note that unlike asyncs, there can be no pending sync interrupts */
1301 /* at this time (this is a fresh context and master has not written */
1302 /* IOARRIN yet), so there is nothing to clear. */
1303
1304 /* set LISN#, it is always sent to the context that wrote IOARRIN */
Matthew R. Ochs30652672017-04-12 14:15:53 -05001305 for (i = 0; i < afu->num_hwqs; i++) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001306 hwq = get_hwq(afu, i);
1307
1308 writeq_be(SISL_MSI_SYNC_ERROR, &hwq->host_map->ctx_ctrl);
1309 writeq_be(SISL_ISTATUS_MASK, &hwq->host_map->intr_mask);
1310 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001311}
1312
1313/**
1314 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1315 * @irq: Interrupt number.
1316 * @data: Private data provided at interrupt registration, the AFU.
1317 *
1318 * Return: Always return IRQ_HANDLED.
1319 */
1320static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1321{
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001322 struct hwq *hwq = (struct hwq *)data;
1323 struct cxlflash_cfg *cfg = hwq->afu->parent;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001324 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001325 u64 reg;
1326 u64 reg_unmasked;
1327
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001328 reg = readq_be(&hwq->host_map->intr_status);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001329 reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1330
1331 if (reg_unmasked == 0UL) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001332 dev_err(dev, "%s: spurious interrupt, intr_status=%016llx\n",
1333 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001334 goto cxlflash_sync_err_irq_exit;
1335 }
1336
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001337 dev_err(dev, "%s: unexpected interrupt, intr_status=%016llx\n",
1338 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001339
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001340 writeq_be(reg_unmasked, &hwq->host_map->intr_clear);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001341
1342cxlflash_sync_err_irq_exit:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001343 return IRQ_HANDLED;
1344}
1345
1346/**
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001347 * process_hrrq() - process the read-response queue
1348 * @afu: AFU associated with the host.
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001349 * @doneq: Queue of commands harvested from the RRQ.
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001350 * @budget: Threshold of RRQ entries to process.
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001351 *
1352 * This routine must be called holding the disabled RRQ spin lock.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001353 *
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001354 * Return: The number of entries processed.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001355 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001356static int process_hrrq(struct hwq *hwq, struct list_head *doneq, int budget)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001357{
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001358 struct afu *afu = hwq->afu;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001359 struct afu_cmd *cmd;
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001360 struct sisl_ioasa *ioasa;
1361 struct sisl_ioarcb *ioarcb;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001362 bool toggle = hwq->toggle;
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001363 int num_hrrq = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001364 u64 entry,
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001365 *hrrq_start = hwq->hrrq_start,
1366 *hrrq_end = hwq->hrrq_end,
1367 *hrrq_curr = hwq->hrrq_curr;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001368
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001369 /* Process ready RRQ entries up to the specified budget (if any) */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001370 while (true) {
1371 entry = *hrrq_curr;
1372
1373 if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1374 break;
1375
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001376 entry &= ~SISL_RESP_HANDLE_T_BIT;
1377
1378 if (afu_is_sq_cmd_mode(afu)) {
1379 ioasa = (struct sisl_ioasa *)entry;
1380 cmd = container_of(ioasa, struct afu_cmd, sa);
1381 } else {
1382 ioarcb = (struct sisl_ioarcb *)entry;
1383 cmd = container_of(ioarcb, struct afu_cmd, rcb);
1384 }
1385
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001386 list_add_tail(&cmd->queue, doneq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001387
1388 /* Advance to next entry or wrap and flip the toggle bit */
1389 if (hrrq_curr < hrrq_end)
1390 hrrq_curr++;
1391 else {
1392 hrrq_curr = hrrq_start;
1393 toggle ^= SISL_RESP_HANDLE_T_BIT;
1394 }
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001395
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001396 atomic_inc(&hwq->hsq_credits);
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001397 num_hrrq++;
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001398
1399 if (budget > 0 && num_hrrq >= budget)
1400 break;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001401 }
1402
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001403 hwq->hrrq_curr = hrrq_curr;
1404 hwq->toggle = toggle;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001405
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001406 return num_hrrq;
1407}
1408
1409/**
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001410 * process_cmd_doneq() - process a queue of harvested RRQ commands
1411 * @doneq: Queue of completed commands.
1412 *
1413 * Note that upon return the queue can no longer be trusted.
1414 */
1415static void process_cmd_doneq(struct list_head *doneq)
1416{
1417 struct afu_cmd *cmd, *tmp;
1418
1419 WARN_ON(list_empty(doneq));
1420
1421 list_for_each_entry_safe(cmd, tmp, doneq, queue)
1422 cmd_complete(cmd);
1423}
1424
1425/**
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001426 * cxlflash_irqpoll() - process a queue of harvested RRQ commands
1427 * @irqpoll: IRQ poll structure associated with queue to poll.
1428 * @budget: Threshold of RRQ entries to process per poll.
1429 *
1430 * Return: The number of entries processed.
1431 */
1432static int cxlflash_irqpoll(struct irq_poll *irqpoll, int budget)
1433{
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001434 struct hwq *hwq = container_of(irqpoll, struct hwq, irqpoll);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001435 unsigned long hrrq_flags;
1436 LIST_HEAD(doneq);
1437 int num_entries = 0;
1438
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001439 spin_lock_irqsave(&hwq->hrrq_slock, hrrq_flags);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001440
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001441 num_entries = process_hrrq(hwq, &doneq, budget);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001442 if (num_entries < budget)
1443 irq_poll_complete(irqpoll);
1444
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001445 spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001446
1447 process_cmd_doneq(&doneq);
1448 return num_entries;
1449}
1450
1451/**
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001452 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1453 * @irq: Interrupt number.
1454 * @data: Private data provided at interrupt registration, the AFU.
1455 *
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001456 * Return: IRQ_HANDLED or IRQ_NONE when no ready entries found.
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001457 */
1458static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
1459{
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001460 struct hwq *hwq = (struct hwq *)data;
1461 struct afu *afu = hwq->afu;
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001462 unsigned long hrrq_flags;
1463 LIST_HEAD(doneq);
1464 int num_entries = 0;
Matthew R. Ochs76a6ebb2017-04-12 14:11:44 -05001465
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001466 spin_lock_irqsave(&hwq->hrrq_slock, hrrq_flags);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001467
1468 if (afu_is_irqpoll_enabled(afu)) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001469 irq_poll_sched(&hwq->irqpoll);
1470 spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05001471 return IRQ_HANDLED;
1472 }
1473
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001474 num_entries = process_hrrq(hwq, &doneq, -1);
1475 spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
Matthew R. Ochsf918b4a2017-04-12 14:12:55 -05001476
1477 if (num_entries == 0)
1478 return IRQ_NONE;
1479
1480 process_cmd_doneq(&doneq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001481 return IRQ_HANDLED;
1482}
1483
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001484/*
1485 * Asynchronous interrupt information table
1486 *
1487 * NOTE:
1488 * - Order matters here as this array is indexed by bit position.
1489 *
1490 * - The checkpatch script considers the BUILD_SISL_ASTATUS_FC_PORT macro
1491 * as complex and complains due to a lack of parentheses/braces.
1492 */
1493#define ASTATUS_FC(_a, _b, _c, _d) \
1494 { SISL_ASTATUS_FC##_a##_##_b, _c, _a, (_d) }
1495
1496#define BUILD_SISL_ASTATUS_FC_PORT(_a) \
1497 ASTATUS_FC(_a, LINK_UP, "link up", 0), \
1498 ASTATUS_FC(_a, LINK_DN, "link down", 0), \
1499 ASTATUS_FC(_a, LOGI_S, "login succeeded", SCAN_HOST), \
1500 ASTATUS_FC(_a, LOGI_F, "login failed", CLR_FC_ERROR), \
1501 ASTATUS_FC(_a, LOGI_R, "login timed out, retrying", LINK_RESET), \
1502 ASTATUS_FC(_a, CRC_T, "CRC threshold exceeded", LINK_RESET), \
1503 ASTATUS_FC(_a, LOGO, "target initiated LOGO", 0), \
1504 ASTATUS_FC(_a, OTHER, "other error", CLR_FC_ERROR | LINK_RESET)
1505
1506static const struct asyc_intr_info ainfo[] = {
1507 BUILD_SISL_ASTATUS_FC_PORT(1),
1508 BUILD_SISL_ASTATUS_FC_PORT(0),
1509 BUILD_SISL_ASTATUS_FC_PORT(3),
1510 BUILD_SISL_ASTATUS_FC_PORT(2)
1511};
1512
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001513/**
1514 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1515 * @irq: Interrupt number.
1516 * @data: Private data provided at interrupt registration, the AFU.
1517 *
1518 * Return: Always return IRQ_HANDLED.
1519 */
1520static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1521{
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001522 struct hwq *hwq = (struct hwq *)data;
1523 struct afu *afu = hwq->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001524 struct cxlflash_cfg *cfg = afu->parent;
1525 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001526 const struct asyc_intr_info *info;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001527 struct sisl_global_map __iomem *global = &afu->afu_map->global;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001528 __be64 __iomem *fc_port_regs;
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001529 u64 reg_unmasked;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001530 u64 reg;
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001531 u64 bit;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001532 u8 port;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001533
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001534 reg = readq_be(&global->regs.aintr_status);
1535 reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1536
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001537 if (unlikely(reg_unmasked == 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001538 dev_err(dev, "%s: spurious interrupt, aintr_status=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001539 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001540 goto out;
1541 }
1542
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001543 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001544 writeq_be(reg_unmasked, &global->regs.aintr_clear);
1545
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001546 /* Check each bit that is on */
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001547 for_each_set_bit(bit, (ulong *)&reg_unmasked, BITS_PER_LONG) {
1548 if (unlikely(bit >= ARRAY_SIZE(ainfo))) {
1549 WARN_ON_ONCE(1);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001550 continue;
Matthew R. Ochse2ef33f2017-04-12 14:15:29 -05001551 }
1552
1553 info = &ainfo[bit];
1554 if (unlikely(info->status != 1ULL << bit)) {
1555 WARN_ON_ONCE(1);
1556 continue;
1557 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001558
1559 port = info->port;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001560 fc_port_regs = get_fc_port_regs(cfg, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001561
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001562 dev_err(dev, "%s: FC Port %d -> %s, fc_status=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001563 __func__, port, info->desc,
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001564 readq_be(&fc_port_regs[FC_STATUS / 8]));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001565
1566 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001567 * Do link reset first, some OTHER errors will set FC_ERROR
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001568 * again if cleared before or w/o a reset
1569 */
1570 if (info->action & LINK_RESET) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001571 dev_err(dev, "%s: FC Port %d: resetting link\n",
1572 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001573 cfg->lr_state = LINK_RESET_REQUIRED;
1574 cfg->lr_port = port;
1575 schedule_work(&cfg->work_q);
1576 }
1577
1578 if (info->action & CLR_FC_ERROR) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001579 reg = readq_be(&fc_port_regs[FC_ERROR / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001580
1581 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001582 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001583 * should be the same and tracing one is sufficient.
1584 */
1585
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001586 dev_err(dev, "%s: fc %d: clearing fc_error=%016llx\n",
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001587 __func__, port, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001588
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001589 writeq_be(reg, &fc_port_regs[FC_ERROR / 8]);
1590 writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001591 }
Matthew R. Ochsef510742015-10-21 15:13:37 -05001592
1593 if (info->action & SCAN_HOST) {
1594 atomic_inc(&cfg->scan_host_needed);
1595 schedule_work(&cfg->work_q);
1596 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001597 }
1598
1599out:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001600 return IRQ_HANDLED;
1601}
1602
1603/**
1604 * start_context() - starts the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001605 * @cfg: Internal structure associated with the host.
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001606 * @index: Index of the hardware queue.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001607 *
1608 * Return: A success or failure value from CXL services.
1609 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001610static int start_context(struct cxlflash_cfg *cfg, u32 index)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001611{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001612 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001613 struct hwq *hwq = get_hwq(cfg->afu, index);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001614 int rc = 0;
1615
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001616 rc = cxl_start_context(hwq->ctx,
1617 hwq->work.work_element_descriptor,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001618 NULL);
1619
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001620 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001621 return rc;
1622}
1623
1624/**
1625 * read_vpd() - obtains the WWPNs from VPD
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001626 * @cfg: Internal structure associated with the host.
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001627 * @wwpn: Array of size MAX_FC_PORTS to pass back WWPNs
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001628 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001629 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001630 */
1631static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1632{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001633 struct device *dev = &cfg->dev->dev;
1634 struct pci_dev *pdev = cfg->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001635 int rc = 0;
1636 int ro_start, ro_size, i, j, k;
1637 ssize_t vpd_size;
1638 char vpd_data[CXLFLASH_VPD_LEN];
1639 char tmp_buf[WWPN_BUF_LEN] = { 0 };
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05001640 char *wwpn_vpd_tags[MAX_FC_PORTS] = { "V5", "V6", "V7", "V8" };
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001641
1642 /* Get the VPD data from the device */
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001643 vpd_size = cxl_read_adapter_vpd(pdev, vpd_data, sizeof(vpd_data));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001644 if (unlikely(vpd_size <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001645 dev_err(dev, "%s: Unable to read VPD (size = %ld)\n",
1646 __func__, vpd_size);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001647 rc = -ENODEV;
1648 goto out;
1649 }
1650
1651 /* Get the read only section offset */
1652 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
1653 PCI_VPD_LRDT_RO_DATA);
1654 if (unlikely(ro_start < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001655 dev_err(dev, "%s: VPD Read-only data not found\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001656 rc = -ENODEV;
1657 goto out;
1658 }
1659
1660 /* Get the read only section size, cap when extends beyond read VPD */
1661 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1662 j = ro_size;
1663 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1664 if (unlikely((i + j) > vpd_size)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001665 dev_dbg(dev, "%s: Might need to read more VPD (%d > %ld)\n",
1666 __func__, (i + j), vpd_size);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001667 ro_size = vpd_size - i;
1668 }
1669
1670 /*
1671 * Find the offset of the WWPN tag within the read only
1672 * VPD data and validate the found field (partials are
1673 * no good to us). Convert the ASCII data to an integer
1674 * value. Note that we must copy to a temporary buffer
1675 * because the conversion service requires that the ASCII
1676 * string be terminated.
1677 */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001678 for (k = 0; k < cfg->num_fc_ports; k++) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001679 j = ro_size;
1680 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1681
1682 i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
1683 if (unlikely(i < 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001684 dev_err(dev, "%s: Port %d WWPN not found in VPD\n",
1685 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001686 rc = -ENODEV;
1687 goto out;
1688 }
1689
1690 j = pci_vpd_info_field_size(&vpd_data[i]);
1691 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1692 if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001693 dev_err(dev, "%s: Port %d WWPN incomplete or bad VPD\n",
1694 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001695 rc = -ENODEV;
1696 goto out;
1697 }
1698
1699 memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1700 rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1701 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001702 dev_err(dev, "%s: WWPN conversion failed for port %d\n",
1703 __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001704 rc = -ENODEV;
1705 goto out;
1706 }
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001707
1708 dev_dbg(dev, "%s: wwpn%d=%016llx\n", __func__, k, wwpn[k]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001709 }
1710
1711out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001712 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001713 return rc;
1714}
1715
1716/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001717 * init_pcr() - initialize the provisioning and control registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001718 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001719 *
1720 * Also sets up fast access to the mapped registers and initializes AFU
1721 * command fields that never change.
1722 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001723static void init_pcr(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001724{
1725 struct afu *afu = cfg->afu;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001726 struct sisl_ctrl_map __iomem *ctrl_map;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001727 struct hwq *hwq;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001728 int i;
1729
1730 for (i = 0; i < MAX_CONTEXT; i++) {
1731 ctrl_map = &afu->afu_map->ctrls[i].ctrl;
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001732 /* Disrupt any clients that could be running */
1733 /* e.g. clients that survived a master restart */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001734 writeq_be(0, &ctrl_map->rht_start);
1735 writeq_be(0, &ctrl_map->rht_cnt_id);
1736 writeq_be(0, &ctrl_map->ctx_cap);
1737 }
1738
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001739 /* Copy frequently used fields into hwq */
Matthew R. Ochs30652672017-04-12 14:15:53 -05001740 for (i = 0; i < afu->num_hwqs; i++) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001741 hwq = get_hwq(afu, i);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001742
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001743 hwq->ctx_hndl = (u16) cxl_process_element(hwq->ctx);
1744 hwq->host_map = &afu->afu_map->hosts[hwq->ctx_hndl].host;
1745 hwq->ctrl_map = &afu->afu_map->ctrls[hwq->ctx_hndl].ctrl;
1746
1747 /* Program the Endian Control for the master context */
1748 writeq_be(SISL_ENDIAN_CTRL, &hwq->host_map->endian_ctrl);
1749 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001750}
1751
1752/**
1753 * init_global() - initialize AFU global registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001754 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001755 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001756static int init_global(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001757{
1758 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001759 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001760 struct hwq *hwq;
1761 struct sisl_host_map __iomem *hmap;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001762 __be64 __iomem *fc_port_regs;
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001763 u64 wwpn[MAX_FC_PORTS]; /* wwpn of AFU ports */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001764 int i = 0, num_ports = 0;
1765 int rc = 0;
1766 u64 reg;
1767
1768 rc = read_vpd(cfg, &wwpn[0]);
1769 if (rc) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001770 dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001771 goto out;
1772 }
1773
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001774 /* Set up RRQ and SQ in HWQ for master issued cmds */
Matthew R. Ochs30652672017-04-12 14:15:53 -05001775 for (i = 0; i < afu->num_hwqs; i++) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001776 hwq = get_hwq(afu, i);
1777 hmap = hwq->host_map;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001778
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001779 writeq_be((u64) hwq->hrrq_start, &hmap->rrq_start);
1780 writeq_be((u64) hwq->hrrq_end, &hmap->rrq_end);
1781
1782 if (afu_is_sq_cmd_mode(afu)) {
1783 writeq_be((u64)hwq->hsq_start, &hmap->sq_start);
1784 writeq_be((u64)hwq->hsq_end, &hmap->sq_end);
1785 }
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001786 }
1787
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001788 /* AFU configuration */
1789 reg = readq_be(&afu->afu_map->global.regs.afu_config);
1790 reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1791 /* enable all auto retry options and control endianness */
1792 /* leave others at default: */
1793 /* CTX_CAP write protected, mbox_r does not clear on read and */
1794 /* checker on if dual afu */
1795 writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1796
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001797 /* Global port select: select either port */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001798 if (afu->internal_lun) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001799 /* Only use port 0 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001800 writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001801 num_ports = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001802 } else {
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -05001803 writeq_be(PORT_MASK(cfg->num_fc_ports),
1804 &afu->afu_map->global.regs.afu_port_sel);
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05001805 num_ports = cfg->num_fc_ports;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001806 }
1807
1808 for (i = 0; i < num_ports; i++) {
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001809 fc_port_regs = get_fc_port_regs(cfg, i);
1810
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001811 /* Unmask all errors (but they are still masked at AFU) */
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001812 writeq_be(0, &fc_port_regs[FC_ERRMSK / 8]);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001813 /* Clear CRC error cnt & set a threshold */
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001814 (void)readq_be(&fc_port_regs[FC_CNT_CRCERR / 8]);
1815 writeq_be(MC_CRC_THRESH, &fc_port_regs[FC_CRC_THRESH / 8]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001816
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001817 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001818 if (wwpn[i] != 0)
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05001819 afu_set_wwpn(afu, i, &fc_port_regs[0], wwpn[i]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001820 /* Programming WWPN back to back causes additional
1821 * offline/online transitions and a PLOGI
1822 */
1823 msleep(100);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001824 }
1825
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001826 /* Set up master's own CTX_CAP to allow real mode, host translation */
1827 /* tables, afu cmds and read/write GSCSI cmds. */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001828 /* First, unlock ctx_cap write by reading mbox */
Matthew R. Ochs30652672017-04-12 14:15:53 -05001829 for (i = 0; i < afu->num_hwqs; i++) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001830 hwq = get_hwq(afu, i);
1831
1832 (void)readq_be(&hwq->ctrl_map->mbox_r); /* unlock ctx_cap */
1833 writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1834 SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1835 SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1836 &hwq->ctrl_map->ctx_cap);
1837 }
Matthew R. Ochs3223c012017-06-21 21:16:33 -05001838
1839 /*
1840 * Determine write-same unmap support for host by evaluating the unmap
1841 * sector support bit of the context control register associated with
1842 * the primary hardware queue. Note that while this status is reflected
1843 * in a context register, the outcome can be assumed to be host-wide.
1844 */
1845 hwq = get_hwq(afu, PRIMARY_HWQ);
1846 reg = readq_be(&hwq->host_map->ctx_ctrl);
1847 if (reg & SISL_CTX_CTRL_UNMAP_SECTOR)
1848 cfg->ws_unmap = true;
1849
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001850 /* Initialize heartbeat */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001851 afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001852out:
1853 return rc;
1854}
1855
1856/**
1857 * start_afu() - initializes and starts the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001858 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001859 */
1860static int start_afu(struct cxlflash_cfg *cfg)
1861{
1862 struct afu *afu = cfg->afu;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001863 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001864 struct hwq *hwq;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001865 int rc = 0;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001866 int i;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001867
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001868 init_pcr(cfg);
1869
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001870 /* Initialize each HWQ */
Matthew R. Ochs30652672017-04-12 14:15:53 -05001871 for (i = 0; i < afu->num_hwqs; i++) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001872 hwq = get_hwq(afu, i);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001873
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001874 /* After an AFU reset, RRQ entries are stale, clear them */
1875 memset(&hwq->rrq_entry, 0, sizeof(hwq->rrq_entry));
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001876
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001877 /* Initialize RRQ pointers */
1878 hwq->hrrq_start = &hwq->rrq_entry[0];
1879 hwq->hrrq_end = &hwq->rrq_entry[NUM_RRQ_ENTRY - 1];
1880 hwq->hrrq_curr = hwq->hrrq_start;
1881 hwq->toggle = 1;
Uma Krishnan66ea9bc2017-06-21 21:13:32 -05001882
1883 /* Initialize spin locks */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001884 spin_lock_init(&hwq->hrrq_slock);
Uma Krishnan66ea9bc2017-06-21 21:13:32 -05001885 spin_lock_init(&hwq->hsq_slock);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001886
1887 /* Initialize SQ */
1888 if (afu_is_sq_cmd_mode(afu)) {
1889 memset(&hwq->sq, 0, sizeof(hwq->sq));
1890 hwq->hsq_start = &hwq->sq[0];
1891 hwq->hsq_end = &hwq->sq[NUM_SQ_ENTRY - 1];
1892 hwq->hsq_curr = hwq->hsq_start;
1893
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001894 atomic_set(&hwq->hsq_credits, NUM_SQ_ENTRY - 1);
1895 }
1896
1897 /* Initialize IRQ poll */
1898 if (afu_is_irqpoll_enabled(afu))
1899 irq_poll_init(&hwq->irqpoll, afu->irqpoll_weight,
1900 cxlflash_irqpoll);
1901
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06001902 }
1903
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001904 rc = init_global(cfg);
1905
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001906 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001907 return rc;
1908}
1909
1910/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001911 * init_intr() - setup interrupt handlers for the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001912 * @cfg: Internal structure associated with the host.
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001913 * @hwq: Hardware queue to initialize.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001914 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001915 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001916 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001917static enum undo_level init_intr(struct cxlflash_cfg *cfg,
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001918 struct hwq *hwq)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001919{
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001920 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001921 struct cxl_context *ctx = hwq->ctx;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001922 int rc = 0;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001923 enum undo_level level = UNDO_NOOP;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001924 bool is_primary_hwq = (hwq->index == PRIMARY_HWQ);
1925 int num_irqs = is_primary_hwq ? 3 : 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001926
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001927 rc = cxl_allocate_afu_irqs(ctx, num_irqs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001928 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001929 dev_err(dev, "%s: allocate_afu_irqs failed rc=%d\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001930 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001931 level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001932 goto out;
1933 }
1934
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001935 rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, hwq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001936 "SISL_MSI_SYNC_ERROR");
1937 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001938 dev_err(dev, "%s: SISL_MSI_SYNC_ERROR map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001939 level = FREE_IRQ;
1940 goto out;
1941 }
1942
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001943 rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, hwq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001944 "SISL_MSI_RRQ_UPDATED");
1945 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001946 dev_err(dev, "%s: SISL_MSI_RRQ_UPDATED map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001947 level = UNMAP_ONE;
1948 goto out;
1949 }
1950
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001951 /* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
1952 if (!is_primary_hwq)
1953 goto out;
1954
1955 rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, hwq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001956 "SISL_MSI_ASYNC_ERROR");
1957 if (unlikely(rc <= 0)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06001958 dev_err(dev, "%s: SISL_MSI_ASYNC_ERROR map failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001959 level = UNMAP_TWO;
1960 goto out;
1961 }
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001962out:
1963 return level;
1964}
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001965
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001966/**
1967 * init_mc() - create and register as the master context
1968 * @cfg: Internal structure associated with the host.
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001969 * index: HWQ Index of the master context.
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001970 *
1971 * Return: 0 on success, -errno on failure
1972 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001973static int init_mc(struct cxlflash_cfg *cfg, u32 index)
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001974{
1975 struct cxl_context *ctx;
1976 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001977 struct hwq *hwq = get_hwq(cfg->afu, index);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001978 int rc = 0;
1979 enum undo_level level;
1980
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001981 hwq->afu = cfg->afu;
1982 hwq->index = index;
Uma Krishnana002bf82017-06-21 21:14:43 -05001983 INIT_LIST_HEAD(&hwq->pending_cmds);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001984
1985 if (index == PRIMARY_HWQ)
1986 ctx = cxl_get_context(cfg->dev);
1987 else
1988 ctx = cxl_dev_context_init(cfg->dev);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001989 if (unlikely(!ctx)) {
1990 rc = -ENOMEM;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001991 goto err1;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001992 }
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05001993
1994 WARN_ON(hwq->ctx);
1995 hwq->ctx = ctx;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001996
1997 /* Set it up as a master with the CXL */
1998 cxl_set_master(ctx);
1999
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002000 /* Reset AFU when initializing primary context */
2001 if (index == PRIMARY_HWQ) {
2002 rc = cxl_afu_reset(ctx);
2003 if (unlikely(rc)) {
2004 dev_err(dev, "%s: AFU reset failed rc=%d\n",
2005 __func__, rc);
2006 goto err1;
2007 }
Manoj N. Kumar9526f362016-03-25 14:26:34 -05002008 }
2009
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002010 level = init_intr(cfg, hwq);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05002011 if (unlikely(level)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002012 dev_err(dev, "%s: interrupt init failed rc=%d\n", __func__, rc);
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002013 goto err2;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05002014 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002015
2016 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
2017 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
2018 * element (pe) that is embedded in the context (ctx)
2019 */
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002020 rc = start_context(cfg, index);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002021 if (unlikely(rc)) {
2022 dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
2023 level = UNMAP_THREE;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002024 goto err2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002025 }
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002026
2027out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002028 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002029 return rc;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002030err2:
2031 term_intr(cfg, level, index);
2032 if (index != PRIMARY_HWQ)
2033 cxl_release_context(ctx);
2034err1:
2035 hwq->ctx = NULL;
2036 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002037}
2038
2039/**
Matthew R. Ochs565180722017-04-12 14:14:28 -05002040 * get_num_afu_ports() - determines and configures the number of AFU ports
2041 * @cfg: Internal structure associated with the host.
2042 *
2043 * This routine determines the number of AFU ports by converting the global
2044 * port selection mask. The converted value is only valid following an AFU
2045 * reset (explicit or power-on). This routine must be invoked shortly after
2046 * mapping as other routines are dependent on the number of ports during the
2047 * initialization sequence.
2048 *
2049 * To support legacy AFUs that might not have reflected an initial global
2050 * port mask (value read is 0), default to the number of ports originally
2051 * supported by the cxlflash driver (2) before hardware with other port
2052 * offerings was introduced.
2053 */
2054static void get_num_afu_ports(struct cxlflash_cfg *cfg)
2055{
2056 struct afu *afu = cfg->afu;
2057 struct device *dev = &cfg->dev->dev;
2058 u64 port_mask;
2059 int num_fc_ports = LEGACY_FC_PORTS;
2060
2061 port_mask = readq_be(&afu->afu_map->global.regs.afu_port_sel);
2062 if (port_mask != 0ULL)
2063 num_fc_ports = min(ilog2(port_mask) + 1, MAX_FC_PORTS);
2064
2065 dev_dbg(dev, "%s: port_mask=%016llx num_fc_ports=%d\n",
2066 __func__, port_mask, num_fc_ports);
2067
2068 cfg->num_fc_ports = num_fc_ports;
2069 cfg->host->max_channel = PORTNUM2CHAN(num_fc_ports);
2070}
2071
2072/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002073 * init_afu() - setup as master context and start AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002074 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002075 *
2076 * This routine is a higher level of control for configuring the
2077 * AFU on probe and reset paths.
2078 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002079 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002080 */
2081static int init_afu(struct cxlflash_cfg *cfg)
2082{
2083 u64 reg;
2084 int rc = 0;
2085 struct afu *afu = cfg->afu;
2086 struct device *dev = &cfg->dev->dev;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002087 struct hwq *hwq;
2088 int i;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002089
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002090 cxl_perst_reloads_same_image(cfg->cxl_afu, true);
2091
Matthew R. Ochs30652672017-04-12 14:15:53 -05002092 afu->num_hwqs = afu->desired_hwqs;
2093 for (i = 0; i < afu->num_hwqs; i++) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002094 rc = init_mc(cfg, i);
2095 if (rc) {
2096 dev_err(dev, "%s: init_mc failed rc=%d index=%d\n",
2097 __func__, rc, i);
2098 goto err1;
2099 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002100 }
2101
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002102 /* Map the entire MMIO space of the AFU using the first context */
2103 hwq = get_hwq(afu, PRIMARY_HWQ);
2104 afu->afu_map = cxl_psa_map(hwq->ctx);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002105 if (!afu->afu_map) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002106 dev_err(dev, "%s: cxl_psa_map failed\n", __func__);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05002107 rc = -ENOMEM;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002108 goto err1;
2109 }
2110
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05002111 /* No byte reverse on reading afu_version or string will be backwards */
2112 reg = readq(&afu->afu_map->global.regs.afu_version);
2113 memcpy(afu->version, &reg, sizeof(reg));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002114 afu->interface_version =
2115 readq_be(&afu->afu_map->global.regs.interface_version);
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05002116 if ((afu->interface_version + 1) == 0) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002117 dev_err(dev, "Back level AFU, please upgrade. AFU version %s "
2118 "interface version %016llx\n", afu->version,
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05002119 afu->interface_version);
2120 rc = -EINVAL;
Uma Krishnan0df5bef2017-01-11 19:20:03 -06002121 goto err1;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05002122 }
2123
Matthew R. Ochs696d0b02017-01-11 19:19:33 -06002124 if (afu_is_sq_cmd_mode(afu)) {
2125 afu->send_cmd = send_cmd_sq;
2126 afu->context_reset = context_reset_sq;
2127 } else {
2128 afu->send_cmd = send_cmd_ioarrin;
2129 afu->context_reset = context_reset_ioarrin;
2130 }
Matthew R. Ochs48b4be32016-11-28 18:43:09 -06002131
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002132 dev_dbg(dev, "%s: afu_ver=%s interface_ver=%016llx\n", __func__,
2133 afu->version, afu->interface_version);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002134
Matthew R. Ochs565180722017-04-12 14:14:28 -05002135 get_num_afu_ports(cfg);
2136
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002137 rc = start_afu(cfg);
2138 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002139 dev_err(dev, "%s: start_afu failed, rc=%d\n", __func__, rc);
Uma Krishnan0df5bef2017-01-11 19:20:03 -06002140 goto err1;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002141 }
2142
2143 afu_err_intr_init(cfg->afu);
Matthew R. Ochs30652672017-04-12 14:15:53 -05002144 for (i = 0; i < afu->num_hwqs; i++) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002145 hwq = get_hwq(afu, i);
2146
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002147 hwq->room = readq_be(&hwq->host_map->cmd_room);
2148 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002149
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002150 /* Restore the LUN mappings */
2151 cxlflash_restore_luntable(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05002152out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002153 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002154 return rc;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05002155
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05002156err1:
Matthew R. Ochs30652672017-04-12 14:15:53 -05002157 for (i = afu->num_hwqs - 1; i >= 0; i--) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002158 term_intr(cfg, UNMAP_THREE, i);
2159 term_mc(cfg, i);
2160 }
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05002161 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002162}
2163
2164/**
Uma Krishnan0b09e712017-06-21 21:14:17 -05002165 * afu_reset() - resets the AFU
2166 * @cfg: Internal structure associated with the host.
2167 *
2168 * Return: 0 on success, -errno on failure
2169 */
2170static int afu_reset(struct cxlflash_cfg *cfg)
2171{
2172 struct device *dev = &cfg->dev->dev;
2173 int rc = 0;
2174
2175 /* Stop the context before the reset. Since the context is
2176 * no longer available restart it after the reset is complete
2177 */
2178 term_afu(cfg);
2179
2180 rc = init_afu(cfg);
2181
2182 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
2183 return rc;
2184}
2185
2186/**
2187 * drain_ioctls() - wait until all currently executing ioctls have completed
2188 * @cfg: Internal structure associated with the host.
2189 *
2190 * Obtain write access to read/write semaphore that wraps ioctl
2191 * handling to 'drain' ioctls currently executing.
2192 */
2193static void drain_ioctls(struct cxlflash_cfg *cfg)
2194{
2195 down_write(&cfg->ioctl_rwsem);
2196 up_write(&cfg->ioctl_rwsem);
2197}
2198
2199/**
2200 * cxlflash_async_reset_host() - asynchronous host reset handler
2201 * @data: Private data provided while scheduling reset.
2202 * @cookie: Cookie that can be used for checkpointing.
2203 */
2204static void cxlflash_async_reset_host(void *data, async_cookie_t cookie)
2205{
2206 struct cxlflash_cfg *cfg = data;
2207 struct device *dev = &cfg->dev->dev;
2208 int rc = 0;
2209
2210 if (cfg->state != STATE_RESET) {
2211 dev_dbg(dev, "%s: Not performing a reset, state=%d\n",
2212 __func__, cfg->state);
2213 goto out;
2214 }
2215
2216 drain_ioctls(cfg);
2217 cxlflash_mark_contexts_error(cfg);
2218 rc = afu_reset(cfg);
2219 if (rc)
2220 cfg->state = STATE_FAILTERM;
2221 else
2222 cfg->state = STATE_NORMAL;
2223 wake_up_all(&cfg->reset_waitq);
2224
2225out:
2226 scsi_unblock_requests(cfg->host);
2227}
2228
2229/**
2230 * cxlflash_schedule_async_reset() - schedule an asynchronous host reset
2231 * @cfg: Internal structure associated with the host.
2232 */
2233static void cxlflash_schedule_async_reset(struct cxlflash_cfg *cfg)
2234{
2235 struct device *dev = &cfg->dev->dev;
2236
2237 if (cfg->state != STATE_NORMAL) {
2238 dev_dbg(dev, "%s: Not performing reset state=%d\n",
2239 __func__, cfg->state);
2240 return;
2241 }
2242
2243 cfg->state = STATE_RESET;
2244 scsi_block_requests(cfg->host);
2245 cfg->async_reset_cookie = async_schedule(cxlflash_async_reset_host,
2246 cfg);
2247}
2248
2249/**
Matthew R. Ochscf243022017-06-21 21:15:31 -05002250 * send_afu_cmd() - builds and sends an internal AFU command
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002251 * @afu: AFU associated with the host.
Matthew R. Ochscf243022017-06-21 21:15:31 -05002252 * @rcb: Pre-populated IOARCB describing command to send.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002253 *
Matthew R. Ochscf243022017-06-21 21:15:31 -05002254 * The AFU can only take one internal AFU command at a time. This limitation is
2255 * enforced by using a mutex to provide exclusive access to the AFU during the
2256 * operation. This design point requires calling threads to not be on interrupt
2257 * context due to the possibility of sleeping during concurrent AFU operations.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002258 *
Matthew R. Ochscf243022017-06-21 21:15:31 -05002259 * The command status is optionally passed back to the caller when the caller
2260 * populates the IOASA field of the IOARCB with a pointer to an IOASA structure.
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002261 *
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002262 * Return:
Uma Krishnan539d8902017-06-21 21:13:48 -05002263 * 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002264 */
Matthew R. Ochscf243022017-06-21 21:15:31 -05002265static int send_afu_cmd(struct afu *afu, struct sisl_ioarcb *rcb)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002266{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002267 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002268 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002269 struct afu_cmd *cmd = NULL;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002270 struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06002271 char *buf = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002272 int rc = 0;
Uma Krishnana96851d2017-06-21 21:14:02 -05002273 int nretry = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002274 static DEFINE_MUTEX(sync_active);
2275
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002276 if (cfg->state != STATE_NORMAL) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002277 dev_dbg(dev, "%s: Sync not required state=%u\n",
2278 __func__, cfg->state);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002279 return 0;
2280 }
2281
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002282 mutex_lock(&sync_active);
Matthew R. Ochsde012832016-11-28 18:42:33 -06002283 atomic_inc(&afu->cmds_active);
Uma Krishnana1ea04b2017-06-21 21:14:56 -05002284 buf = kmalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06002285 if (unlikely(!buf)) {
2286 dev_err(dev, "%s: no memory for command\n", __func__);
Uma Krishnan539d8902017-06-21 21:13:48 -05002287 rc = -ENOMEM;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002288 goto out;
2289 }
2290
Matthew R. Ochs350bb472016-11-28 18:42:11 -06002291 cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
Uma Krishnana96851d2017-06-21 21:14:02 -05002292
2293retry:
Uma Krishnana1ea04b2017-06-21 21:14:56 -05002294 memset(cmd, 0, sizeof(*cmd));
Matthew R. Ochscf243022017-06-21 21:15:31 -05002295 memcpy(&cmd->rcb, rcb, sizeof(*rcb));
Uma Krishnana1ea04b2017-06-21 21:14:56 -05002296 INIT_LIST_HEAD(&cmd->queue);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06002297 init_completion(&cmd->cevent);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06002298 cmd->parent = afu;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002299 cmd->hwq_index = hwq->index;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002300 cmd->rcb.ctx_id = hwq->ctx_hndl;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002301
Matthew R. Ochscf243022017-06-21 21:15:31 -05002302 dev_dbg(dev, "%s: afu=%p cmd=%p type=%02x nretry=%d\n",
2303 __func__, afu, cmd, cmd->rcb.cdb[0], nretry);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002304
Matthew R. Ochs48b4be32016-11-28 18:43:09 -06002305 rc = afu->send_cmd(afu, cmd);
Uma Krishnan539d8902017-06-21 21:13:48 -05002306 if (unlikely(rc)) {
2307 rc = -ENOBUFS;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002308 goto out;
Uma Krishnan539d8902017-06-21 21:13:48 -05002309 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002310
Matthew R. Ochs9ba848a2016-11-28 18:42:42 -06002311 rc = wait_resp(afu, cmd);
Uma Krishnana1ea04b2017-06-21 21:14:56 -05002312 switch (rc) {
2313 case -ETIMEDOUT:
Uma Krishnana96851d2017-06-21 21:14:02 -05002314 rc = afu->context_reset(hwq);
Uma Krishnana1ea04b2017-06-21 21:14:56 -05002315 if (rc) {
2316 cxlflash_schedule_async_reset(cfg);
2317 break;
2318 }
2319 /* fall through to retry */
2320 case -EAGAIN:
2321 if (++nretry < 2)
Uma Krishnana96851d2017-06-21 21:14:02 -05002322 goto retry;
Uma Krishnana1ea04b2017-06-21 21:14:56 -05002323 /* fall through to exit */
2324 default:
2325 break;
Uma Krishnana96851d2017-06-21 21:14:02 -05002326 }
2327
Matthew R. Ochscf243022017-06-21 21:15:31 -05002328 if (rcb->ioasa)
2329 *rcb->ioasa = cmd->sa;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002330out:
Matthew R. Ochsde012832016-11-28 18:42:33 -06002331 atomic_dec(&afu->cmds_active);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002332 mutex_unlock(&sync_active);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06002333 kfree(buf);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002334 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002335 return rc;
2336}
2337
2338/**
Matthew R. Ochscf243022017-06-21 21:15:31 -05002339 * cxlflash_afu_sync() - builds and sends an AFU sync command
2340 * @afu: AFU associated with the host.
2341 * @ctx: Identifies context requesting sync.
2342 * @res: Identifies resource requesting sync.
2343 * @mode: Type of sync to issue (lightweight, heavyweight, global).
2344 *
2345 * AFU sync operations are only necessary and allowed when the device is
2346 * operating normally. When not operating normally, sync requests can occur as
2347 * part of cleaning up resources associated with an adapter prior to removal.
2348 * In this scenario, these requests are simply ignored (safe due to the AFU
2349 * going away).
2350 *
2351 * Return:
2352 * 0 on success, -errno on failure
2353 */
2354int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx, res_hndl_t res, u8 mode)
2355{
2356 struct cxlflash_cfg *cfg = afu->parent;
2357 struct device *dev = &cfg->dev->dev;
2358 struct sisl_ioarcb rcb = { 0 };
2359
2360 dev_dbg(dev, "%s: afu=%p ctx=%u res=%u mode=%u\n",
2361 __func__, afu, ctx, res, mode);
2362
2363 rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
2364 rcb.msi = SISL_MSI_RRQ_UPDATED;
2365 rcb.timeout = MC_AFU_SYNC_TIMEOUT;
2366
2367 rcb.cdb[0] = SISL_AFU_CMD_SYNC;
2368 rcb.cdb[1] = mode;
2369 put_unaligned_be16(ctx, &rcb.cdb[2]);
2370 put_unaligned_be32(res, &rcb.cdb[4]);
2371
2372 return send_afu_cmd(afu, &rcb);
2373}
2374
2375/**
Uma Krishnan7c4c41f2017-06-21 21:15:06 -05002376 * cxlflash_eh_abort_handler() - abort a SCSI command
2377 * @scp: SCSI command to abort.
2378 *
2379 * CXL Flash devices do not support a single command abort. Reset the context
2380 * as per SISLite specification. Flush any pending commands in the hardware
2381 * queue before the reset.
2382 *
2383 * Return: SUCCESS/FAILED as defined in scsi/scsi.h
2384 */
2385static int cxlflash_eh_abort_handler(struct scsi_cmnd *scp)
2386{
2387 int rc = FAILED;
2388 struct Scsi_Host *host = scp->device->host;
2389 struct cxlflash_cfg *cfg = shost_priv(host);
2390 struct afu_cmd *cmd = sc_to_afuc(scp);
2391 struct device *dev = &cfg->dev->dev;
2392 struct afu *afu = cfg->afu;
2393 struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
2394
2395 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
2396 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
2397 scp->device->channel, scp->device->id, scp->device->lun,
2398 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
2399 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
2400 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
2401 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
2402
2403 /* When the state is not normal, another reset/reload is in progress.
2404 * Return failed and the mid-layer will invoke host reset handler.
2405 */
2406 if (cfg->state != STATE_NORMAL) {
2407 dev_dbg(dev, "%s: Invalid state for abort, state=%d\n",
2408 __func__, cfg->state);
2409 goto out;
2410 }
2411
2412 rc = afu->context_reset(hwq);
2413 if (unlikely(rc))
2414 goto out;
2415
2416 rc = SUCCESS;
2417
2418out:
2419 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
2420 return rc;
2421}
2422
2423/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05002424 * cxlflash_eh_device_reset_handler() - reset a single LUN
2425 * @scp: SCSI command to send.
2426 *
2427 * Return:
2428 * SUCCESS as defined in scsi/scsi.h
2429 * FAILED as defined in scsi/scsi.h
2430 */
2431static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
2432{
2433 int rc = SUCCESS;
2434 struct Scsi_Host *host = scp->device->host;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002435 struct cxlflash_cfg *cfg = shost_priv(host);
2436 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002437 struct afu *afu = cfg->afu;
2438 int rcr = 0;
2439
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002440 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
2441 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
2442 scp->device->channel, scp->device->id, scp->device->lun,
2443 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
2444 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
2445 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
2446 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochs15305512015-10-21 15:12:10 -05002447
Matthew R. Ochsed486da2015-10-21 15:14:24 -05002448retry:
Matthew R. Ochs15305512015-10-21 15:12:10 -05002449 switch (cfg->state) {
2450 case STATE_NORMAL:
2451 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
2452 if (unlikely(rcr))
2453 rc = FAILED;
2454 break;
2455 case STATE_RESET:
2456 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
Matthew R. Ochsed486da2015-10-21 15:14:24 -05002457 goto retry;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002458 default:
2459 rc = FAILED;
2460 break;
2461 }
2462
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002463 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002464 return rc;
2465}
2466
2467/**
2468 * cxlflash_eh_host_reset_handler() - reset the host adapter
2469 * @scp: SCSI command from stack identifying host.
2470 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002471 * Following a reset, the state is evaluated again in case an EEH occurred
2472 * during the reset. In such a scenario, the host reset will either yield
2473 * until the EEH recovery is complete or return success or failure based
2474 * upon the current device state.
2475 *
Matthew R. Ochs15305512015-10-21 15:12:10 -05002476 * Return:
2477 * SUCCESS as defined in scsi/scsi.h
2478 * FAILED as defined in scsi/scsi.h
2479 */
2480static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
2481{
2482 int rc = SUCCESS;
2483 int rcr = 0;
2484 struct Scsi_Host *host = scp->device->host;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002485 struct cxlflash_cfg *cfg = shost_priv(host);
2486 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002487
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002488 dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
2489 "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
2490 scp->device->channel, scp->device->id, scp->device->lun,
2491 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
2492 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
2493 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
2494 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochs15305512015-10-21 15:12:10 -05002495
2496 switch (cfg->state) {
2497 case STATE_NORMAL:
2498 cfg->state = STATE_RESET;
Manoj N. Kumarf4113962016-06-15 18:49:20 -05002499 drain_ioctls(cfg);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002500 cxlflash_mark_contexts_error(cfg);
2501 rcr = afu_reset(cfg);
2502 if (rcr) {
2503 rc = FAILED;
2504 cfg->state = STATE_FAILTERM;
2505 } else
2506 cfg->state = STATE_NORMAL;
2507 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002508 ssleep(1);
2509 /* fall through */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002510 case STATE_RESET:
2511 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2512 if (cfg->state == STATE_NORMAL)
2513 break;
2514 /* fall through */
2515 default:
2516 rc = FAILED;
2517 break;
2518 }
2519
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002520 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002521 return rc;
2522}
2523
2524/**
2525 * cxlflash_change_queue_depth() - change the queue depth for the device
2526 * @sdev: SCSI device destined for queue depth change.
2527 * @qdepth: Requested queue depth value to set.
2528 *
2529 * The requested queue depth is capped to the maximum supported value.
2530 *
2531 * Return: The actual queue depth set.
2532 */
2533static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
2534{
2535
2536 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
2537 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
2538
2539 scsi_change_queue_depth(sdev, qdepth);
2540 return sdev->queue_depth;
2541}
2542
2543/**
2544 * cxlflash_show_port_status() - queries and presents the current port status
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002545 * @port: Desired port for status reporting.
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002546 * @cfg: Internal structure associated with the host.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002547 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2548 *
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002549 * Return: The size of the ASCII string returned in @buf or -EINVAL.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002550 */
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002551static ssize_t cxlflash_show_port_status(u32 port,
2552 struct cxlflash_cfg *cfg,
2553 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002554{
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002555 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002556 char *disp_status;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002557 u64 status;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002558 __be64 __iomem *fc_port_regs;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002559
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002560 WARN_ON(port >= MAX_FC_PORTS);
2561
2562 if (port >= cfg->num_fc_ports) {
2563 dev_info(dev, "%s: Port %d not supported on this card.\n",
2564 __func__, port);
2565 return -EINVAL;
2566 }
Matthew R. Ochs15305512015-10-21 15:12:10 -05002567
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002568 fc_port_regs = get_fc_port_regs(cfg, port);
2569 status = readq_be(&fc_port_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002570 status &= FC_MTIP_STATUS_MASK;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002571
2572 if (status == FC_MTIP_STATUS_ONLINE)
2573 disp_status = "online";
2574 else if (status == FC_MTIP_STATUS_OFFLINE)
2575 disp_status = "offline";
2576 else
2577 disp_status = "unknown";
2578
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002579 return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002580}
2581
2582/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002583 * port0_show() - queries and presents the current status of port 0
2584 * @dev: Generic device associated with the host owning the port.
2585 * @attr: Device attribute representing the port.
2586 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002587 *
2588 * Return: The size of the ASCII string returned in @buf.
2589 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002590static ssize_t port0_show(struct device *dev,
2591 struct device_attribute *attr,
2592 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002593{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002594 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochs15305512015-10-21 15:12:10 -05002595
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002596 return cxlflash_show_port_status(0, cfg, buf);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002597}
2598
2599/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002600 * port1_show() - queries and presents the current status of port 1
2601 * @dev: Generic device associated with the host owning the port.
2602 * @attr: Device attribute representing the port.
2603 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2604 *
2605 * Return: The size of the ASCII string returned in @buf.
2606 */
2607static ssize_t port1_show(struct device *dev,
2608 struct device_attribute *attr,
2609 char *buf)
2610{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002611 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002612
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002613 return cxlflash_show_port_status(1, cfg, buf);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002614}
2615
2616/**
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002617 * port2_show() - queries and presents the current status of port 2
2618 * @dev: Generic device associated with the host owning the port.
2619 * @attr: Device attribute representing the port.
2620 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2621 *
2622 * Return: The size of the ASCII string returned in @buf.
2623 */
2624static ssize_t port2_show(struct device *dev,
2625 struct device_attribute *attr,
2626 char *buf)
2627{
2628 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2629
2630 return cxlflash_show_port_status(2, cfg, buf);
2631}
2632
2633/**
2634 * port3_show() - queries and presents the current status of port 3
2635 * @dev: Generic device associated with the host owning the port.
2636 * @attr: Device attribute representing the port.
2637 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2638 *
2639 * Return: The size of the ASCII string returned in @buf.
2640 */
2641static ssize_t port3_show(struct device *dev,
2642 struct device_attribute *attr,
2643 char *buf)
2644{
2645 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2646
2647 return cxlflash_show_port_status(3, cfg, buf);
2648}
2649
2650/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002651 * lun_mode_show() - presents the current LUN mode of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002652 * @dev: Generic device associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002653 * @attr: Device attribute representing the LUN mode.
2654 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2655 *
2656 * Return: The size of the ASCII string returned in @buf.
2657 */
2658static ssize_t lun_mode_show(struct device *dev,
2659 struct device_attribute *attr, char *buf)
2660{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002661 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002662 struct afu *afu = cfg->afu;
2663
2664 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
2665}
2666
2667/**
2668 * lun_mode_store() - sets the LUN mode of the host
2669 * @dev: Generic device associated with the host.
2670 * @attr: Device attribute representing the LUN mode.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002671 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2672 * @count: Length of data resizing in @buf.
2673 *
2674 * The CXL Flash AFU supports a dummy LUN mode where the external
2675 * links and storage are not required. Space on the FPGA is used
2676 * to create 1 or 2 small LUNs which are presented to the system
2677 * as if they were a normal storage device. This feature is useful
2678 * during development and also provides manufacturing with a way
2679 * to test the AFU without an actual device.
2680 *
2681 * 0 = external LUN[s] (default)
2682 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2683 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2684 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2685 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2686 *
2687 * Return: The size of the ASCII string returned in @buf.
2688 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002689static ssize_t lun_mode_store(struct device *dev,
2690 struct device_attribute *attr,
2691 const char *buf, size_t count)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002692{
2693 struct Scsi_Host *shost = class_to_shost(dev);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002694 struct cxlflash_cfg *cfg = shost_priv(shost);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002695 struct afu *afu = cfg->afu;
2696 int rc;
2697 u32 lun_mode;
2698
2699 rc = kstrtouint(buf, 10, &lun_mode);
2700 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
2701 afu->internal_lun = lun_mode;
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002702
2703 /*
2704 * When configured for internal LUN, there is only one channel,
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002705 * channel number 0, else there will be one less than the number
2706 * of fc ports for this card.
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002707 */
2708 if (afu->internal_lun)
2709 shost->max_channel = 0;
2710 else
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -05002711 shost->max_channel = PORTNUM2CHAN(cfg->num_fc_ports);
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002712
Matthew R. Ochs15305512015-10-21 15:12:10 -05002713 afu_reset(cfg);
2714 scsi_scan_host(cfg->host);
2715 }
2716
2717 return count;
2718}
2719
2720/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002721 * ioctl_version_show() - presents the current ioctl version of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002722 * @dev: Generic device associated with the host.
2723 * @attr: Device attribute representing the ioctl version.
2724 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2725 *
2726 * Return: The size of the ASCII string returned in @buf.
2727 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002728static ssize_t ioctl_version_show(struct device *dev,
2729 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002730{
Matthew R. Ochsd6e32f52017-06-21 21:15:42 -05002731 ssize_t bytes = 0;
2732
2733 bytes = scnprintf(buf, PAGE_SIZE,
2734 "disk: %u\n", DK_CXLFLASH_VERSION_0);
2735 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
2736 "host: %u\n", HT_CXLFLASH_VERSION_0);
2737
2738 return bytes;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002739}
2740
2741/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002742 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2743 * @port: Desired port for status reporting.
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002744 * @cfg: Internal structure associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002745 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2746 *
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002747 * Return: The size of the ASCII string returned in @buf or -EINVAL.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002748 */
2749static ssize_t cxlflash_show_port_lun_table(u32 port,
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002750 struct cxlflash_cfg *cfg,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002751 char *buf)
2752{
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002753 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002754 __be64 __iomem *fc_port_luns;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002755 int i;
2756 ssize_t bytes = 0;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002757
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05002758 WARN_ON(port >= MAX_FC_PORTS);
2759
2760 if (port >= cfg->num_fc_ports) {
2761 dev_info(dev, "%s: Port %d not supported on this card.\n",
2762 __func__, port);
2763 return -EINVAL;
2764 }
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002765
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002766 fc_port_luns = get_fc_port_luns(cfg, port);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002767
2768 for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2769 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05002770 "%03d: %016llx\n",
2771 i, readq_be(&fc_port_luns[i]));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002772 return bytes;
2773}
2774
2775/**
2776 * port0_lun_table_show() - presents the current LUN table of port 0
2777 * @dev: Generic device associated with the host owning the port.
2778 * @attr: Device attribute representing the port.
2779 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2780 *
2781 * Return: The size of the ASCII string returned in @buf.
2782 */
2783static ssize_t port0_lun_table_show(struct device *dev,
2784 struct device_attribute *attr,
2785 char *buf)
2786{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002787 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002788
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002789 return cxlflash_show_port_lun_table(0, cfg, buf);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002790}
2791
2792/**
2793 * port1_lun_table_show() - presents the current LUN table of port 1
2794 * @dev: Generic device associated with the host owning the port.
2795 * @attr: Device attribute representing the port.
2796 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2797 *
2798 * Return: The size of the ASCII string returned in @buf.
2799 */
2800static ssize_t port1_lun_table_show(struct device *dev,
2801 struct device_attribute *attr,
2802 char *buf)
2803{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06002804 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002805
Matthew R. Ochs3b225cd2017-04-12 14:13:34 -05002806 return cxlflash_show_port_lun_table(1, cfg, buf);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002807}
2808
2809/**
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05002810 * port2_lun_table_show() - presents the current LUN table of port 2
2811 * @dev: Generic device associated with the host owning the port.
2812 * @attr: Device attribute representing the port.
2813 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2814 *
2815 * Return: The size of the ASCII string returned in @buf.
2816 */
2817static ssize_t port2_lun_table_show(struct device *dev,
2818 struct device_attribute *attr,
2819 char *buf)
2820{
2821 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2822
2823 return cxlflash_show_port_lun_table(2, cfg, buf);
2824}
2825
2826/**
2827 * port3_lun_table_show() - presents the current LUN table of port 3
2828 * @dev: Generic device associated with the host owning the port.
2829 * @attr: Device attribute representing the port.
2830 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2831 *
2832 * Return: The size of the ASCII string returned in @buf.
2833 */
2834static ssize_t port3_lun_table_show(struct device *dev,
2835 struct device_attribute *attr,
2836 char *buf)
2837{
2838 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2839
2840 return cxlflash_show_port_lun_table(3, cfg, buf);
2841}
2842
2843/**
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002844 * irqpoll_weight_show() - presents the current IRQ poll weight for the host
2845 * @dev: Generic device associated with the host.
2846 * @attr: Device attribute representing the IRQ poll weight.
2847 * @buf: Buffer of length PAGE_SIZE to report back the current IRQ poll
2848 * weight in ASCII.
2849 *
2850 * An IRQ poll weight of 0 indicates polling is disabled.
2851 *
2852 * Return: The size of the ASCII string returned in @buf.
2853 */
2854static ssize_t irqpoll_weight_show(struct device *dev,
2855 struct device_attribute *attr, char *buf)
2856{
2857 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2858 struct afu *afu = cfg->afu;
2859
2860 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->irqpoll_weight);
2861}
2862
2863/**
2864 * irqpoll_weight_store() - sets the current IRQ poll weight for the host
2865 * @dev: Generic device associated with the host.
2866 * @attr: Device attribute representing the IRQ poll weight.
2867 * @buf: Buffer of length PAGE_SIZE containing the desired IRQ poll
2868 * weight in ASCII.
2869 * @count: Length of data resizing in @buf.
2870 *
2871 * An IRQ poll weight of 0 indicates polling is disabled.
2872 *
2873 * Return: The size of the ASCII string returned in @buf.
2874 */
2875static ssize_t irqpoll_weight_store(struct device *dev,
2876 struct device_attribute *attr,
2877 const char *buf, size_t count)
2878{
2879 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2880 struct device *cfgdev = &cfg->dev->dev;
2881 struct afu *afu = cfg->afu;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002882 struct hwq *hwq;
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002883 u32 weight;
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002884 int rc, i;
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002885
2886 rc = kstrtouint(buf, 10, &weight);
2887 if (rc)
2888 return -EINVAL;
2889
2890 if (weight > 256) {
2891 dev_info(cfgdev,
2892 "Invalid IRQ poll weight. It must be 256 or less.\n");
2893 return -EINVAL;
2894 }
2895
2896 if (weight == afu->irqpoll_weight) {
2897 dev_info(cfgdev,
2898 "Current IRQ poll weight has the same weight.\n");
2899 return -EINVAL;
2900 }
2901
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002902 if (afu_is_irqpoll_enabled(afu)) {
Matthew R. Ochs30652672017-04-12 14:15:53 -05002903 for (i = 0; i < afu->num_hwqs; i++) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002904 hwq = get_hwq(afu, i);
2905
2906 irq_poll_disable(&hwq->irqpoll);
2907 }
2908 }
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002909
2910 afu->irqpoll_weight = weight;
2911
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002912 if (weight > 0) {
Matthew R. Ochs30652672017-04-12 14:15:53 -05002913 for (i = 0; i < afu->num_hwqs; i++) {
Uma Krishnanbfc0bab2017-04-12 14:15:42 -05002914 hwq = get_hwq(afu, i);
2915
2916 irq_poll_init(&hwq->irqpoll, weight, cxlflash_irqpoll);
2917 }
2918 }
Matthew R. Ochscba06e62017-04-12 14:13:20 -05002919
2920 return count;
2921}
2922
2923/**
Matthew R. Ochs30652672017-04-12 14:15:53 -05002924 * num_hwqs_show() - presents the number of hardware queues for the host
2925 * @dev: Generic device associated with the host.
2926 * @attr: Device attribute representing the number of hardware queues.
2927 * @buf: Buffer of length PAGE_SIZE to report back the number of hardware
2928 * queues in ASCII.
2929 *
2930 * Return: The size of the ASCII string returned in @buf.
2931 */
2932static ssize_t num_hwqs_show(struct device *dev,
2933 struct device_attribute *attr, char *buf)
2934{
2935 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2936 struct afu *afu = cfg->afu;
2937
2938 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->num_hwqs);
2939}
2940
2941/**
2942 * num_hwqs_store() - sets the number of hardware queues for the host
2943 * @dev: Generic device associated with the host.
2944 * @attr: Device attribute representing the number of hardware queues.
2945 * @buf: Buffer of length PAGE_SIZE containing the number of hardware
2946 * queues in ASCII.
2947 * @count: Length of data resizing in @buf.
2948 *
2949 * n > 0: num_hwqs = n
2950 * n = 0: num_hwqs = num_online_cpus()
2951 * n < 0: num_online_cpus() / abs(n)
2952 *
2953 * Return: The size of the ASCII string returned in @buf.
2954 */
2955static ssize_t num_hwqs_store(struct device *dev,
2956 struct device_attribute *attr,
2957 const char *buf, size_t count)
2958{
2959 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2960 struct afu *afu = cfg->afu;
2961 int rc;
2962 int nhwqs, num_hwqs;
2963
2964 rc = kstrtoint(buf, 10, &nhwqs);
2965 if (rc)
2966 return -EINVAL;
2967
2968 if (nhwqs >= 1)
2969 num_hwqs = nhwqs;
2970 else if (nhwqs == 0)
2971 num_hwqs = num_online_cpus();
2972 else
2973 num_hwqs = num_online_cpus() / abs(nhwqs);
2974
2975 afu->desired_hwqs = min(num_hwqs, CXLFLASH_MAX_HWQS);
2976 WARN_ON_ONCE(afu->desired_hwqs == 0);
2977
2978retry:
2979 switch (cfg->state) {
2980 case STATE_NORMAL:
2981 cfg->state = STATE_RESET;
2982 drain_ioctls(cfg);
2983 cxlflash_mark_contexts_error(cfg);
2984 rc = afu_reset(cfg);
2985 if (rc)
2986 cfg->state = STATE_FAILTERM;
2987 else
2988 cfg->state = STATE_NORMAL;
2989 wake_up_all(&cfg->reset_waitq);
2990 break;
2991 case STATE_RESET:
2992 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2993 if (cfg->state == STATE_NORMAL)
2994 goto retry;
2995 default:
2996 /* Ideally should not happen */
2997 dev_err(dev, "%s: Device is not ready, state=%d\n",
2998 __func__, cfg->state);
2999 break;
3000 }
3001
3002 return count;
3003}
3004
Matthew R. Ochs1dd0c0e2017-04-12 14:16:02 -05003005static const char *hwq_mode_name[MAX_HWQ_MODE] = { "rr", "tag", "cpu" };
3006
3007/**
3008 * hwq_mode_show() - presents the HWQ steering mode for the host
3009 * @dev: Generic device associated with the host.
3010 * @attr: Device attribute representing the HWQ steering mode.
3011 * @buf: Buffer of length PAGE_SIZE to report back the HWQ steering mode
3012 * as a character string.
3013 *
3014 * Return: The size of the ASCII string returned in @buf.
3015 */
3016static ssize_t hwq_mode_show(struct device *dev,
3017 struct device_attribute *attr, char *buf)
3018{
3019 struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
3020 struct afu *afu = cfg->afu;
3021
3022 return scnprintf(buf, PAGE_SIZE, "%s\n", hwq_mode_name[afu->hwq_mode]);
3023}
3024
3025/**
3026 * hwq_mode_store() - sets the HWQ steering mode for the host
3027 * @dev: Generic device associated with the host.
3028 * @attr: Device attribute representing the HWQ steering mode.
3029 * @buf: Buffer of length PAGE_SIZE containing the HWQ steering mode
3030 * as a character string.
3031 * @count: Length of data resizing in @buf.
3032 *
3033 * rr = Round-Robin
3034 * tag = Block MQ Tagging
3035 * cpu = CPU Affinity
3036 *
3037 * Return: The size of the ASCII string returned in @buf.
3038 */
3039static ssize_t hwq_mode_store(struct device *dev,
3040 struct device_attribute *attr,
3041 const char *buf, size_t count)
3042{
3043 struct Scsi_Host *shost = class_to_shost(dev);
3044 struct cxlflash_cfg *cfg = shost_priv(shost);
3045 struct device *cfgdev = &cfg->dev->dev;
3046 struct afu *afu = cfg->afu;
3047 int i;
3048 u32 mode = MAX_HWQ_MODE;
3049
3050 for (i = 0; i < MAX_HWQ_MODE; i++) {
3051 if (!strncmp(hwq_mode_name[i], buf, strlen(hwq_mode_name[i]))) {
3052 mode = i;
3053 break;
3054 }
3055 }
3056
3057 if (mode >= MAX_HWQ_MODE) {
3058 dev_info(cfgdev, "Invalid HWQ steering mode.\n");
3059 return -EINVAL;
3060 }
3061
3062 if ((mode == HWQ_MODE_TAG) && !shost_use_blk_mq(shost)) {
3063 dev_info(cfgdev, "SCSI-MQ is not enabled, use a different "
3064 "HWQ steering mode.\n");
3065 return -EINVAL;
3066 }
3067
3068 afu->hwq_mode = mode;
3069
3070 return count;
3071}
3072
Matthew R. Ochs30652672017-04-12 14:15:53 -05003073/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05003074 * mode_show() - presents the current mode of the device
Matthew R. Ochs15305512015-10-21 15:12:10 -05003075 * @dev: Generic device associated with the device.
3076 * @attr: Device attribute representing the device mode.
3077 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
3078 *
3079 * Return: The size of the ASCII string returned in @buf.
3080 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05003081static ssize_t mode_show(struct device *dev,
3082 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05003083{
3084 struct scsi_device *sdev = to_scsi_device(dev);
3085
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05003086 return scnprintf(buf, PAGE_SIZE, "%s\n",
3087 sdev->hostdata ? "superpipe" : "legacy");
Matthew R. Ochs15305512015-10-21 15:12:10 -05003088}
3089
3090/*
3091 * Host attributes
3092 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05003093static DEVICE_ATTR_RO(port0);
3094static DEVICE_ATTR_RO(port1);
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05003095static DEVICE_ATTR_RO(port2);
3096static DEVICE_ATTR_RO(port3);
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05003097static DEVICE_ATTR_RW(lun_mode);
3098static DEVICE_ATTR_RO(ioctl_version);
3099static DEVICE_ATTR_RO(port0_lun_table);
3100static DEVICE_ATTR_RO(port1_lun_table);
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05003101static DEVICE_ATTR_RO(port2_lun_table);
3102static DEVICE_ATTR_RO(port3_lun_table);
Matthew R. Ochscba06e62017-04-12 14:13:20 -05003103static DEVICE_ATTR_RW(irqpoll_weight);
Matthew R. Ochs30652672017-04-12 14:15:53 -05003104static DEVICE_ATTR_RW(num_hwqs);
Matthew R. Ochs1dd0c0e2017-04-12 14:16:02 -05003105static DEVICE_ATTR_RW(hwq_mode);
Matthew R. Ochs15305512015-10-21 15:12:10 -05003106
3107static struct device_attribute *cxlflash_host_attrs[] = {
3108 &dev_attr_port0,
3109 &dev_attr_port1,
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05003110 &dev_attr_port2,
3111 &dev_attr_port3,
Matthew R. Ochs15305512015-10-21 15:12:10 -05003112 &dev_attr_lun_mode,
3113 &dev_attr_ioctl_version,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05003114 &dev_attr_port0_lun_table,
3115 &dev_attr_port1_lun_table,
Matthew R. Ochs1cd7fab2017-04-12 14:14:41 -05003116 &dev_attr_port2_lun_table,
3117 &dev_attr_port3_lun_table,
Matthew R. Ochscba06e62017-04-12 14:13:20 -05003118 &dev_attr_irqpoll_weight,
Matthew R. Ochs30652672017-04-12 14:15:53 -05003119 &dev_attr_num_hwqs,
Matthew R. Ochs1dd0c0e2017-04-12 14:16:02 -05003120 &dev_attr_hwq_mode,
Matthew R. Ochs15305512015-10-21 15:12:10 -05003121 NULL
3122};
3123
3124/*
3125 * Device attributes
3126 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05003127static DEVICE_ATTR_RO(mode);
Matthew R. Ochs15305512015-10-21 15:12:10 -05003128
3129static struct device_attribute *cxlflash_dev_attrs[] = {
3130 &dev_attr_mode,
3131 NULL
3132};
3133
3134/*
3135 * Host template
3136 */
3137static struct scsi_host_template driver_template = {
3138 .module = THIS_MODULE,
3139 .name = CXLFLASH_ADAPTER_NAME,
3140 .info = cxlflash_driver_info,
3141 .ioctl = cxlflash_ioctl,
3142 .proc_name = CXLFLASH_NAME,
3143 .queuecommand = cxlflash_queuecommand,
Uma Krishnan7c4c41f2017-06-21 21:15:06 -05003144 .eh_abort_handler = cxlflash_eh_abort_handler,
Matthew R. Ochs15305512015-10-21 15:12:10 -05003145 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
3146 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
3147 .change_queue_depth = cxlflash_change_queue_depth,
Manoj N. Kumar83430832016-03-04 15:55:20 -06003148 .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
Matthew R. Ochs15305512015-10-21 15:12:10 -05003149 .can_queue = CXLFLASH_MAX_CMDS,
Matthew R. Ochs5fbb96c2016-11-28 18:42:19 -06003150 .cmd_size = sizeof(struct afu_cmd) + __alignof__(struct afu_cmd) - 1,
Matthew R. Ochs15305512015-10-21 15:12:10 -05003151 .this_id = -1,
Uma Krishnan68ab2d72016-11-28 18:41:06 -06003152 .sg_tablesize = 1, /* No scatter gather support */
Matthew R. Ochs15305512015-10-21 15:12:10 -05003153 .max_sectors = CXLFLASH_MAX_SECTORS,
3154 .use_clustering = ENABLE_CLUSTERING,
3155 .shost_attrs = cxlflash_host_attrs,
3156 .sdev_attrs = cxlflash_dev_attrs,
3157};
3158
3159/*
3160 * Device dependent values
3161 */
Uma Krishnan96e1b662016-06-15 18:49:38 -05003162static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
3163 0ULL };
3164static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
Uma Krishnan704c4b02016-06-15 18:49:57 -05003165 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs94344522017-02-16 21:39:32 -06003166static struct dev_dependent_vals dev_briard_vals = { CXLFLASH_MAX_SECTORS,
3167 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs15305512015-10-21 15:12:10 -05003168
3169/*
3170 * PCI device binding table
3171 */
3172static struct pci_device_id cxlflash_pci_table[] = {
3173 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
3174 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
Manoj Kumara2746fb2015-12-14 15:07:43 -06003175 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
3176 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
Matthew R. Ochs94344522017-02-16 21:39:32 -06003177 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_BRIARD,
3178 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_briard_vals},
Matthew R. Ochs15305512015-10-21 15:12:10 -05003179 {}
3180};
3181
3182MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
3183
3184/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003185 * cxlflash_worker_thread() - work thread handler for the AFU
3186 * @work: Work structure contained within cxlflash associated with host.
3187 *
3188 * Handles the following events:
3189 * - Link reset which cannot be performed on interrupt context due to
3190 * blocking up to a few seconds
Matthew R. Ochsef510742015-10-21 15:13:37 -05003191 * - Rescan the host
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003192 */
3193static void cxlflash_worker_thread(struct work_struct *work)
3194{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05003195 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
3196 work_q);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003197 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05003198 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05003199 __be64 __iomem *fc_port_regs;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003200 int port;
3201 ulong lock_flags;
3202
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05003203 /* Avoid MMIO if the device has failed */
3204
3205 if (cfg->state != STATE_NORMAL)
3206 return;
3207
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003208 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
3209
3210 if (cfg->lr_state == LINK_RESET_REQUIRED) {
3211 port = cfg->lr_port;
3212 if (port < 0)
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05003213 dev_err(dev, "%s: invalid port index %d\n",
3214 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003215 else {
3216 spin_unlock_irqrestore(cfg->host->host_lock,
3217 lock_flags);
3218
3219 /* The reset can block... */
Matthew R. Ochs0aa14882017-04-12 14:14:17 -05003220 fc_port_regs = get_fc_port_regs(cfg, port);
3221 afu_link_reset(afu, port, fc_port_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003222 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
3223 }
3224
3225 cfg->lr_state = LINK_RESET_COMPLETE;
3226 }
3227
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003228 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
Matthew R. Ochsef510742015-10-21 15:13:37 -05003229
3230 if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
3231 scsi_scan_host(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003232}
3233
3234/**
Uma Krishnana834a362017-06-21 21:15:18 -05003235 * cxlflash_chr_open() - character device open handler
3236 * @inode: Device inode associated with this character device.
3237 * @file: File pointer for this device.
3238 *
3239 * Only users with admin privileges are allowed to open the character device.
3240 *
3241 * Return: 0 on success, -errno on failure
3242 */
3243static int cxlflash_chr_open(struct inode *inode, struct file *file)
3244{
3245 struct cxlflash_cfg *cfg;
3246
3247 if (!capable(CAP_SYS_ADMIN))
3248 return -EACCES;
3249
3250 cfg = container_of(inode->i_cdev, struct cxlflash_cfg, cdev);
3251 file->private_data = cfg;
3252
3253 return 0;
3254}
3255
Matthew R. Ochsd6e32f52017-06-21 21:15:42 -05003256/**
3257 * decode_hioctl() - translates encoded host ioctl to easily identifiable string
3258 * @cmd: The host ioctl command to decode.
3259 *
3260 * Return: A string identifying the decoded host ioctl.
3261 */
3262static char *decode_hioctl(int cmd)
3263{
3264 switch (cmd) {
Matthew R. Ochs9cf43a32017-06-21 21:16:13 -05003265 case HT_CXLFLASH_LUN_PROVISION:
3266 return __stringify_1(HT_CXLFLASH_LUN_PROVISION);
Matthew R. Ochsd6e32f52017-06-21 21:15:42 -05003267 }
3268
3269 return "UNKNOWN";
3270}
3271
3272/**
Matthew R. Ochs9cf43a32017-06-21 21:16:13 -05003273 * cxlflash_lun_provision() - host LUN provisioning handler
3274 * @cfg: Internal structure associated with the host.
3275 * @arg: Kernel copy of userspace ioctl data structure.
3276 *
3277 * Return: 0 on success, -errno on failure
3278 */
3279static int cxlflash_lun_provision(struct cxlflash_cfg *cfg,
3280 struct ht_cxlflash_lun_provision *lunprov)
3281{
3282 struct afu *afu = cfg->afu;
3283 struct device *dev = &cfg->dev->dev;
3284 struct sisl_ioarcb rcb;
3285 struct sisl_ioasa asa;
3286 __be64 __iomem *fc_port_regs;
3287 u16 port = lunprov->port;
3288 u16 scmd = lunprov->hdr.subcmd;
3289 u16 type;
3290 u64 reg;
3291 u64 size;
3292 u64 lun_id;
3293 int rc = 0;
3294
3295 if (!afu_is_lun_provision(afu)) {
3296 rc = -ENOTSUPP;
3297 goto out;
3298 }
3299
3300 if (port >= cfg->num_fc_ports) {
3301 rc = -EINVAL;
3302 goto out;
3303 }
3304
3305 switch (scmd) {
3306 case HT_CXLFLASH_LUN_PROVISION_SUBCMD_CREATE_LUN:
3307 type = SISL_AFU_LUN_PROVISION_CREATE;
3308 size = lunprov->size;
3309 lun_id = 0;
3310 break;
3311 case HT_CXLFLASH_LUN_PROVISION_SUBCMD_DELETE_LUN:
3312 type = SISL_AFU_LUN_PROVISION_DELETE;
3313 size = 0;
3314 lun_id = lunprov->lun_id;
3315 break;
3316 case HT_CXLFLASH_LUN_PROVISION_SUBCMD_QUERY_PORT:
3317 fc_port_regs = get_fc_port_regs(cfg, port);
3318
3319 reg = readq_be(&fc_port_regs[FC_MAX_NUM_LUNS / 8]);
3320 lunprov->max_num_luns = reg;
3321 reg = readq_be(&fc_port_regs[FC_CUR_NUM_LUNS / 8]);
3322 lunprov->cur_num_luns = reg;
3323 reg = readq_be(&fc_port_regs[FC_MAX_CAP_PORT / 8]);
3324 lunprov->max_cap_port = reg;
3325 reg = readq_be(&fc_port_regs[FC_CUR_CAP_PORT / 8]);
3326 lunprov->cur_cap_port = reg;
3327
3328 goto out;
3329 default:
3330 rc = -EINVAL;
3331 goto out;
3332 }
3333
3334 memset(&rcb, 0, sizeof(rcb));
3335 memset(&asa, 0, sizeof(asa));
3336 rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
3337 rcb.lun_id = lun_id;
3338 rcb.msi = SISL_MSI_RRQ_UPDATED;
3339 rcb.timeout = MC_LUN_PROV_TIMEOUT;
3340 rcb.ioasa = &asa;
3341
3342 rcb.cdb[0] = SISL_AFU_CMD_LUN_PROVISION;
3343 rcb.cdb[1] = type;
3344 rcb.cdb[2] = port;
3345 put_unaligned_be64(size, &rcb.cdb[8]);
3346
3347 rc = send_afu_cmd(afu, &rcb);
3348 if (rc) {
3349 dev_err(dev, "%s: send_afu_cmd failed rc=%d asc=%08x afux=%x\n",
3350 __func__, rc, asa.ioasc, asa.afu_extra);
3351 goto out;
3352 }
3353
3354 if (scmd == HT_CXLFLASH_LUN_PROVISION_SUBCMD_CREATE_LUN) {
3355 lunprov->lun_id = (u64)asa.lunid_hi << 32 | asa.lunid_lo;
3356 memcpy(lunprov->wwid, asa.wwid, sizeof(lunprov->wwid));
3357 }
3358out:
3359 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
3360 return rc;
3361}
3362
3363/**
Matthew R. Ochsbc88ac42017-06-21 21:16:22 -05003364 * cxlflash_afu_debug() - host AFU debug handler
3365 * @cfg: Internal structure associated with the host.
3366 * @arg: Kernel copy of userspace ioctl data structure.
3367 *
3368 * For debug requests requiring a data buffer, always provide an aligned
3369 * (cache line) buffer to the AFU to appease any alignment requirements.
3370 *
3371 * Return: 0 on success, -errno on failure
3372 */
3373static int cxlflash_afu_debug(struct cxlflash_cfg *cfg,
3374 struct ht_cxlflash_afu_debug *afu_dbg)
3375{
3376 struct afu *afu = cfg->afu;
3377 struct device *dev = &cfg->dev->dev;
3378 struct sisl_ioarcb rcb;
3379 struct sisl_ioasa asa;
3380 char *buf = NULL;
3381 char *kbuf = NULL;
3382 void __user *ubuf = (__force void __user *)afu_dbg->data_ea;
3383 u16 req_flags = SISL_REQ_FLAGS_AFU_CMD;
3384 u32 ulen = afu_dbg->data_len;
3385 bool is_write = afu_dbg->hdr.flags & HT_CXLFLASH_HOST_WRITE;
3386 int rc = 0;
3387
3388 if (!afu_is_afu_debug(afu)) {
3389 rc = -ENOTSUPP;
3390 goto out;
3391 }
3392
3393 if (ulen) {
3394 req_flags |= SISL_REQ_FLAGS_SUP_UNDERRUN;
3395
3396 if (ulen > HT_CXLFLASH_AFU_DEBUG_MAX_DATA_LEN) {
3397 rc = -EINVAL;
3398 goto out;
3399 }
3400
3401 if (unlikely(!access_ok(is_write ? VERIFY_READ : VERIFY_WRITE,
3402 ubuf, ulen))) {
3403 rc = -EFAULT;
3404 goto out;
3405 }
3406
3407 buf = kmalloc(ulen + cache_line_size() - 1, GFP_KERNEL);
3408 if (unlikely(!buf)) {
3409 rc = -ENOMEM;
3410 goto out;
3411 }
3412
3413 kbuf = PTR_ALIGN(buf, cache_line_size());
3414
3415 if (is_write) {
3416 req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
3417
3418 rc = copy_from_user(kbuf, ubuf, ulen);
3419 if (unlikely(rc))
3420 goto out;
3421 }
3422 }
3423
3424 memset(&rcb, 0, sizeof(rcb));
3425 memset(&asa, 0, sizeof(asa));
3426
3427 rcb.req_flags = req_flags;
3428 rcb.msi = SISL_MSI_RRQ_UPDATED;
3429 rcb.timeout = MC_AFU_DEBUG_TIMEOUT;
3430 rcb.ioasa = &asa;
3431
3432 if (ulen) {
3433 rcb.data_len = ulen;
3434 rcb.data_ea = (uintptr_t)kbuf;
3435 }
3436
3437 rcb.cdb[0] = SISL_AFU_CMD_DEBUG;
3438 memcpy(&rcb.cdb[4], afu_dbg->afu_subcmd,
3439 HT_CXLFLASH_AFU_DEBUG_SUBCMD_LEN);
3440
3441 rc = send_afu_cmd(afu, &rcb);
3442 if (rc) {
3443 dev_err(dev, "%s: send_afu_cmd failed rc=%d asc=%08x afux=%x\n",
3444 __func__, rc, asa.ioasc, asa.afu_extra);
3445 goto out;
3446 }
3447
3448 if (ulen && !is_write)
3449 rc = copy_to_user(ubuf, kbuf, ulen);
3450out:
3451 kfree(buf);
3452 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
3453 return rc;
3454}
3455
3456/**
Matthew R. Ochsd6e32f52017-06-21 21:15:42 -05003457 * cxlflash_chr_ioctl() - character device IOCTL handler
3458 * @file: File pointer for this device.
3459 * @cmd: IOCTL command.
3460 * @arg: Userspace ioctl data structure.
3461 *
3462 * A read/write semaphore is used to implement a 'drain' of currently
3463 * running ioctls. The read semaphore is taken at the beginning of each
3464 * ioctl thread and released upon concluding execution. Additionally the
3465 * semaphore should be released and then reacquired in any ioctl execution
3466 * path which will wait for an event to occur that is outside the scope of
3467 * the ioctl (i.e. an adapter reset). To drain the ioctls currently running,
3468 * a thread simply needs to acquire the write semaphore.
3469 *
3470 * Return: 0 on success, -errno on failure
3471 */
3472static long cxlflash_chr_ioctl(struct file *file, unsigned int cmd,
3473 unsigned long arg)
3474{
3475 typedef int (*hioctl) (struct cxlflash_cfg *, void *);
3476
3477 struct cxlflash_cfg *cfg = file->private_data;
3478 struct device *dev = &cfg->dev->dev;
3479 char buf[sizeof(union cxlflash_ht_ioctls)];
3480 void __user *uarg = (void __user *)arg;
3481 struct ht_cxlflash_hdr *hdr;
3482 size_t size = 0;
3483 bool known_ioctl = false;
3484 int idx = 0;
3485 int rc = 0;
3486 hioctl do_ioctl = NULL;
3487
3488 static const struct {
3489 size_t size;
3490 hioctl ioctl;
3491 } ioctl_tbl[] = { /* NOTE: order matters here */
Matthew R. Ochs9cf43a32017-06-21 21:16:13 -05003492 { sizeof(struct ht_cxlflash_lun_provision),
3493 (hioctl)cxlflash_lun_provision },
Matthew R. Ochsbc88ac42017-06-21 21:16:22 -05003494 { sizeof(struct ht_cxlflash_afu_debug),
3495 (hioctl)cxlflash_afu_debug },
Matthew R. Ochsd6e32f52017-06-21 21:15:42 -05003496 };
3497
3498 /* Hold read semaphore so we can drain if needed */
3499 down_read(&cfg->ioctl_rwsem);
3500
3501 dev_dbg(dev, "%s: cmd=%u idx=%d tbl_size=%lu\n",
3502 __func__, cmd, idx, sizeof(ioctl_tbl));
3503
3504 switch (cmd) {
Matthew R. Ochs9cf43a32017-06-21 21:16:13 -05003505 case HT_CXLFLASH_LUN_PROVISION:
Matthew R. Ochsbc88ac42017-06-21 21:16:22 -05003506 case HT_CXLFLASH_AFU_DEBUG:
Matthew R. Ochs9cf43a32017-06-21 21:16:13 -05003507 known_ioctl = true;
3508 idx = _IOC_NR(HT_CXLFLASH_LUN_PROVISION) - _IOC_NR(cmd);
3509 size = ioctl_tbl[idx].size;
3510 do_ioctl = ioctl_tbl[idx].ioctl;
3511
3512 if (likely(do_ioctl))
3513 break;
3514
3515 /* fall through */
Matthew R. Ochsd6e32f52017-06-21 21:15:42 -05003516 default:
3517 rc = -EINVAL;
3518 goto out;
3519 }
3520
3521 if (unlikely(copy_from_user(&buf, uarg, size))) {
3522 dev_err(dev, "%s: copy_from_user() fail "
3523 "size=%lu cmd=%d (%s) uarg=%p\n",
3524 __func__, size, cmd, decode_hioctl(cmd), uarg);
3525 rc = -EFAULT;
3526 goto out;
3527 }
3528
3529 hdr = (struct ht_cxlflash_hdr *)&buf;
3530 if (hdr->version != HT_CXLFLASH_VERSION_0) {
3531 dev_dbg(dev, "%s: Version %u not supported for %s\n",
3532 __func__, hdr->version, decode_hioctl(cmd));
3533 rc = -EINVAL;
3534 goto out;
3535 }
3536
3537 if (hdr->rsvd[0] || hdr->rsvd[1] || hdr->return_flags) {
3538 dev_dbg(dev, "%s: Reserved/rflags populated\n", __func__);
3539 rc = -EINVAL;
3540 goto out;
3541 }
3542
3543 rc = do_ioctl(cfg, (void *)&buf);
3544 if (likely(!rc))
3545 if (unlikely(copy_to_user(uarg, &buf, size))) {
3546 dev_err(dev, "%s: copy_to_user() fail "
3547 "size=%lu cmd=%d (%s) uarg=%p\n",
3548 __func__, size, cmd, decode_hioctl(cmd), uarg);
3549 rc = -EFAULT;
3550 }
3551
3552 /* fall through to exit */
3553
3554out:
3555 up_read(&cfg->ioctl_rwsem);
3556 if (unlikely(rc && known_ioctl))
3557 dev_err(dev, "%s: ioctl %s (%08X) returned rc=%d\n",
3558 __func__, decode_hioctl(cmd), cmd, rc);
3559 else
3560 dev_dbg(dev, "%s: ioctl %s (%08X) returned rc=%d\n",
3561 __func__, decode_hioctl(cmd), cmd, rc);
3562 return rc;
3563}
3564
Uma Krishnana834a362017-06-21 21:15:18 -05003565/*
3566 * Character device file operations
3567 */
3568static const struct file_operations cxlflash_chr_fops = {
3569 .owner = THIS_MODULE,
3570 .open = cxlflash_chr_open,
Matthew R. Ochsd6e32f52017-06-21 21:15:42 -05003571 .unlocked_ioctl = cxlflash_chr_ioctl,
3572 .compat_ioctl = cxlflash_chr_ioctl,
Uma Krishnana834a362017-06-21 21:15:18 -05003573};
3574
3575/**
3576 * init_chrdev() - initialize the character device for the host
3577 * @cfg: Internal structure associated with the host.
3578 *
3579 * Return: 0 on success, -errno on failure
3580 */
3581static int init_chrdev(struct cxlflash_cfg *cfg)
3582{
3583 struct device *dev = &cfg->dev->dev;
3584 struct device *char_dev;
3585 dev_t devno;
3586 int minor;
3587 int rc = 0;
3588
3589 minor = cxlflash_get_minor();
3590 if (unlikely(minor < 0)) {
3591 dev_err(dev, "%s: Exhausted allowed adapters\n", __func__);
3592 rc = -ENOSPC;
3593 goto out;
3594 }
3595
3596 devno = MKDEV(cxlflash_major, minor);
3597 cdev_init(&cfg->cdev, &cxlflash_chr_fops);
3598
3599 rc = cdev_add(&cfg->cdev, devno, 1);
3600 if (rc) {
3601 dev_err(dev, "%s: cdev_add failed rc=%d\n", __func__, rc);
3602 goto err1;
3603 }
3604
3605 char_dev = device_create(cxlflash_class, NULL, devno,
3606 NULL, "cxlflash%d", minor);
3607 if (IS_ERR(char_dev)) {
3608 rc = PTR_ERR(char_dev);
3609 dev_err(dev, "%s: device_create failed rc=%d\n",
3610 __func__, rc);
3611 goto err2;
3612 }
3613
3614 cfg->chardev = char_dev;
3615out:
3616 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
3617 return rc;
3618err2:
3619 cdev_del(&cfg->cdev);
3620err1:
3621 cxlflash_put_minor(minor);
3622 goto out;
3623}
3624
3625/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003626 * cxlflash_probe() - PCI entry point to add host
3627 * @pdev: PCI device associated with the host.
3628 * @dev_id: PCI device id associated with device.
3629 *
Matthew R. Ochs323e3342017-04-12 14:14:51 -05003630 * The device will initially start out in a 'probing' state and
3631 * transition to the 'normal' state at the end of a successful
3632 * probe. Should an EEH event occur during probe, the notification
3633 * thread (error_detected()) will wait until the probe handler
3634 * is nearly complete. At that time, the device will be moved to
3635 * a 'probed' state and the EEH thread woken up to drive the slot
3636 * reset and recovery (device moves to 'normal' state). Meanwhile,
3637 * the probe will be allowed to exit successfully.
3638 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05003639 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003640 */
3641static int cxlflash_probe(struct pci_dev *pdev,
3642 const struct pci_device_id *dev_id)
3643{
3644 struct Scsi_Host *host;
3645 struct cxlflash_cfg *cfg = NULL;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06003646 struct device *dev = &pdev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003647 struct dev_dependent_vals *ddv;
3648 int rc = 0;
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05003649 int k;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003650
3651 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
3652 __func__, pdev->irq);
3653
3654 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
3655 driver_template.max_sectors = ddv->max_sectors;
3656
3657 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
3658 if (!host) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06003659 dev_err(dev, "%s: scsi_host_alloc failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003660 rc = -ENOMEM;
3661 goto out;
3662 }
3663
3664 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
3665 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003666 host->unique_id = host->host_no;
3667 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
3668
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06003669 cfg = shost_priv(host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003670 cfg->host = host;
3671 rc = alloc_mem(cfg);
3672 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06003673 dev_err(dev, "%s: alloc_mem failed\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003674 rc = -ENOMEM;
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -05003675 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003676 goto out;
3677 }
3678
3679 cfg->init_state = INIT_STATE_NONE;
3680 cfg->dev = pdev;
Matthew R. Ochs17ead262015-10-21 15:15:37 -05003681 cfg->cxl_fops = cxlflash_cxl_fops;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05003682
3683 /*
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05003684 * Promoted LUNs move to the top of the LUN table. The rest stay on
3685 * the bottom half. The bottom half grows from the end (index = 255),
3686 * whereas the top half grows from the beginning (index = 0).
3687 *
3688 * Initialize the last LUN index for all possible ports.
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05003689 */
Matthew R. Ochs78ae0282017-04-12 14:13:50 -05003690 cfg->promote_lun_index = 0;
3691
3692 for (k = 0; k < MAX_FC_PORTS; k++)
3693 cfg->last_lun_index[k] = CXLFLASH_NUM_VLUNS/2 - 1;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05003694
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003695 cfg->dev_id = (struct pci_device_id *)dev_id;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003696
3697 init_waitqueue_head(&cfg->tmf_waitq);
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05003698 init_waitqueue_head(&cfg->reset_waitq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003699
3700 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
3701 cfg->lr_state = LINK_RESET_INVALID;
3702 cfg->lr_port = -1;
Matthew R. Ochs0d731222015-10-21 15:16:24 -05003703 spin_lock_init(&cfg->tmf_slock);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05003704 mutex_init(&cfg->ctx_tbl_list_mutex);
3705 mutex_init(&cfg->ctx_recovery_mutex);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05003706 init_rwsem(&cfg->ioctl_rwsem);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05003707 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
3708 INIT_LIST_HEAD(&cfg->lluns);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003709
3710 pci_set_drvdata(pdev, cfg);
3711
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003712 cfg->cxl_afu = cxl_pci_to_afu(pdev);
3713
3714 rc = init_pci(cfg);
3715 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06003716 dev_err(dev, "%s: init_pci failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003717 goto out_remove;
3718 }
3719 cfg->init_state = INIT_STATE_PCI;
3720
3721 rc = init_afu(cfg);
Matthew R. Ochs323e3342017-04-12 14:14:51 -05003722 if (rc && !wq_has_sleeper(&cfg->reset_waitq)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06003723 dev_err(dev, "%s: init_afu failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003724 goto out_remove;
3725 }
3726 cfg->init_state = INIT_STATE_AFU;
3727
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003728 rc = init_scsi(cfg);
3729 if (rc) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06003730 dev_err(dev, "%s: init_scsi failed rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003731 goto out_remove;
3732 }
3733 cfg->init_state = INIT_STATE_SCSI;
3734
Uma Krishnana834a362017-06-21 21:15:18 -05003735 rc = init_chrdev(cfg);
3736 if (rc) {
3737 dev_err(dev, "%s: init_chrdev failed rc=%d\n", __func__, rc);
3738 goto out_remove;
3739 }
3740 cfg->init_state = INIT_STATE_CDEV;
3741
Matthew R. Ochs323e3342017-04-12 14:14:51 -05003742 if (wq_has_sleeper(&cfg->reset_waitq)) {
3743 cfg->state = STATE_PROBED;
3744 wake_up_all(&cfg->reset_waitq);
3745 } else
3746 cfg->state = STATE_NORMAL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003747out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06003748 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003749 return rc;
3750
3751out_remove:
3752 cxlflash_remove(pdev);
3753 goto out;
3754}
3755
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05003756/**
3757 * cxlflash_pci_error_detected() - called when a PCI error is detected
3758 * @pdev: PCI device struct.
3759 * @state: PCI channel state.
3760 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05003761 * When an EEH occurs during an active reset, wait until the reset is
3762 * complete and then take action based upon the device state.
3763 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05003764 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
3765 */
3766static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
3767 pci_channel_state_t state)
3768{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05003769 int rc = 0;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05003770 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
3771 struct device *dev = &cfg->dev->dev;
3772
3773 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
3774
3775 switch (state) {
3776 case pci_channel_io_frozen:
Matthew R. Ochs323e3342017-04-12 14:14:51 -05003777 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET &&
3778 cfg->state != STATE_PROBING);
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05003779 if (cfg->state == STATE_FAILTERM)
3780 return PCI_ERS_RESULT_DISCONNECT;
3781
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05003782 cfg->state = STATE_RESET;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05003783 scsi_block_requests(cfg->host);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05003784 drain_ioctls(cfg);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05003785 rc = cxlflash_mark_contexts_error(cfg);
3786 if (unlikely(rc))
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06003787 dev_err(dev, "%s: Failed to mark user contexts rc=%d\n",
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05003788 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05003789 term_afu(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05003790 return PCI_ERS_RESULT_NEED_RESET;
3791 case pci_channel_io_perm_failure:
3792 cfg->state = STATE_FAILTERM;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05003793 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05003794 scsi_unblock_requests(cfg->host);
3795 return PCI_ERS_RESULT_DISCONNECT;
3796 default:
3797 break;
3798 }
3799 return PCI_ERS_RESULT_NEED_RESET;
3800}
3801
3802/**
3803 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
3804 * @pdev: PCI device struct.
3805 *
3806 * This routine is called by the pci error recovery code after the PCI
3807 * slot has been reset, just before we should resume normal operations.
3808 *
3809 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
3810 */
3811static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
3812{
3813 int rc = 0;
3814 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
3815 struct device *dev = &cfg->dev->dev;
3816
3817 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
3818
3819 rc = init_afu(cfg);
3820 if (unlikely(rc)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -06003821 dev_err(dev, "%s: EEH recovery failed rc=%d\n", __func__, rc);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05003822 return PCI_ERS_RESULT_DISCONNECT;
3823 }
3824
3825 return PCI_ERS_RESULT_RECOVERED;
3826}
3827
3828/**
3829 * cxlflash_pci_resume() - called when normal operation can resume
3830 * @pdev: PCI device struct
3831 */
3832static void cxlflash_pci_resume(struct pci_dev *pdev)
3833{
3834 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
3835 struct device *dev = &cfg->dev->dev;
3836
3837 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
3838
3839 cfg->state = STATE_NORMAL;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05003840 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05003841 scsi_unblock_requests(cfg->host);
3842}
3843
Uma Krishnana834a362017-06-21 21:15:18 -05003844/**
3845 * cxlflash_devnode() - provides devtmpfs for devices in the cxlflash class
3846 * @dev: Character device.
3847 * @mode: Mode that can be used to verify access.
3848 *
3849 * Return: Allocated string describing the devtmpfs structure.
3850 */
3851static char *cxlflash_devnode(struct device *dev, umode_t *mode)
3852{
3853 return kasprintf(GFP_KERNEL, "cxlflash/%s", dev_name(dev));
3854}
3855
3856/**
3857 * cxlflash_class_init() - create character device class
3858 *
3859 * Return: 0 on success, -errno on failure
3860 */
3861static int cxlflash_class_init(void)
3862{
3863 dev_t devno;
3864 int rc = 0;
3865
3866 rc = alloc_chrdev_region(&devno, 0, CXLFLASH_MAX_ADAPTERS, "cxlflash");
3867 if (unlikely(rc)) {
3868 pr_err("%s: alloc_chrdev_region failed rc=%d\n", __func__, rc);
3869 goto out;
3870 }
3871
3872 cxlflash_major = MAJOR(devno);
3873
3874 cxlflash_class = class_create(THIS_MODULE, "cxlflash");
3875 if (IS_ERR(cxlflash_class)) {
3876 rc = PTR_ERR(cxlflash_class);
3877 pr_err("%s: class_create failed rc=%d\n", __func__, rc);
3878 goto err;
3879 }
3880
3881 cxlflash_class->devnode = cxlflash_devnode;
3882out:
3883 pr_debug("%s: returning rc=%d\n", __func__, rc);
3884 return rc;
3885err:
3886 unregister_chrdev_region(devno, CXLFLASH_MAX_ADAPTERS);
3887 goto out;
3888}
3889
3890/**
3891 * cxlflash_class_exit() - destroy character device class
3892 */
3893static void cxlflash_class_exit(void)
3894{
3895 dev_t devno = MKDEV(cxlflash_major, 0);
3896
3897 class_destroy(cxlflash_class);
3898 unregister_chrdev_region(devno, CXLFLASH_MAX_ADAPTERS);
3899}
3900
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05003901static const struct pci_error_handlers cxlflash_err_handler = {
3902 .error_detected = cxlflash_pci_error_detected,
3903 .slot_reset = cxlflash_pci_slot_reset,
3904 .resume = cxlflash_pci_resume,
3905};
3906
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003907/*
3908 * PCI device structure
3909 */
3910static struct pci_driver cxlflash_driver = {
3911 .name = CXLFLASH_NAME,
3912 .id_table = cxlflash_pci_table,
3913 .probe = cxlflash_probe,
3914 .remove = cxlflash_remove,
Uma Krishnanbabf9852016-09-02 15:39:16 -05003915 .shutdown = cxlflash_remove,
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05003916 .err_handler = &cxlflash_err_handler,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003917};
3918
3919/**
3920 * init_cxlflash() - module entry point
3921 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05003922 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003923 */
3924static int __init init_cxlflash(void)
3925{
Uma Krishnana834a362017-06-21 21:15:18 -05003926 int rc;
3927
Matthew R. Ochscd41e182017-04-12 14:15:11 -05003928 check_sizes();
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05003929 cxlflash_list_init();
Uma Krishnana834a362017-06-21 21:15:18 -05003930 rc = cxlflash_class_init();
3931 if (unlikely(rc))
3932 goto out;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05003933
Uma Krishnana834a362017-06-21 21:15:18 -05003934 rc = pci_register_driver(&cxlflash_driver);
3935 if (unlikely(rc))
3936 goto err;
3937out:
3938 pr_debug("%s: returning rc=%d\n", __func__, rc);
3939 return rc;
3940err:
3941 cxlflash_class_exit();
3942 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003943}
3944
3945/**
3946 * exit_cxlflash() - module exit point
3947 */
3948static void __exit exit_cxlflash(void)
3949{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05003950 cxlflash_term_global_luns();
3951 cxlflash_free_errpage();
3952
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003953 pci_unregister_driver(&cxlflash_driver);
Uma Krishnana834a362017-06-21 21:15:18 -05003954 cxlflash_class_exit();
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05003955}
3956
3957module_init(init_cxlflash);
3958module_exit(exit_cxlflash);