blob: 9f2821d69e3863b1c1b55748a7b0f34e84cf7167 [file] [log] [blame]
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001/*
2 * CXL Flash Device Driver
3 *
4 * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
5 * Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
6 *
7 * Copyright (C) 2015 IBM Corporation
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15#include <linux/delay.h>
16#include <linux/list.h>
17#include <linux/module.h>
18#include <linux/pci.h>
19
20#include <asm/unaligned.h>
21
22#include <misc/cxl.h>
23
24#include <scsi/scsi_cmnd.h>
25#include <scsi/scsi_host.h>
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050026#include <uapi/scsi/cxlflash_ioctl.h>
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050027
28#include "main.h"
29#include "sislite.h"
30#include "common.h"
31
32MODULE_DESCRIPTION(CXLFLASH_ADAPTER_NAME);
33MODULE_AUTHOR("Manoj N. Kumar <manoj@linux.vnet.ibm.com>");
34MODULE_AUTHOR("Matthew R. Ochs <mrochs@linux.vnet.ibm.com>");
35MODULE_LICENSE("GPL");
36
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050037/**
Matthew R. Ochs15305512015-10-21 15:12:10 -050038 * cmd_checkout() - checks out an AFU command
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050039 * @afu: AFU to checkout from.
40 *
41 * Commands are checked out in a round-robin fashion. Note that since
42 * the command pool is larger than the hardware queue, the majority of
43 * times we will only loop once or twice before getting a command. The
Matthew R. Ochse7ab2d402016-11-28 18:42:01 -060044 * CDB within the command is initialized (zeroed) prior to returning.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050045 *
46 * Return: The checked out command or NULL when command pool is empty.
47 */
Matthew R. Ochs15305512015-10-21 15:12:10 -050048static struct afu_cmd *cmd_checkout(struct afu *afu)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050049{
50 int k, dec = CXLFLASH_NUM_CMDS;
51 struct afu_cmd *cmd;
52
53 while (dec--) {
54 k = (afu->cmd_couts++ & (CXLFLASH_NUM_CMDS - 1));
55
56 cmd = &afu->cmd[k];
57
58 if (!atomic_dec_if_positive(&cmd->free)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -050059 pr_devel("%s: returning found index=%d cmd=%p\n",
60 __func__, cmd->slot, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050061 memset(cmd->rcb.cdb, 0, sizeof(cmd->rcb.cdb));
62 return cmd;
63 }
64 }
65
66 return NULL;
67}
68
69/**
Matthew R. Ochs15305512015-10-21 15:12:10 -050070 * cmd_checkin() - checks in an AFU command
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050071 * @cmd: AFU command to checkin.
72 *
73 * Safe to pass commands that have already been checked in. Several
74 * internal tracking fields are reset as part of the checkin. Note
75 * that these are intentionally reset prior to toggling the free bit
76 * to avoid clobbering values in the event that the command is checked
77 * out right away.
78 */
Matthew R. Ochs15305512015-10-21 15:12:10 -050079static void cmd_checkin(struct afu_cmd *cmd)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050080{
81 cmd->rcb.scp = NULL;
82 cmd->rcb.timeout = 0;
83 cmd->sa.ioasc = 0;
84 cmd->cmd_tmf = false;
85 cmd->sa.host_use[0] = 0; /* clears both completion and retry bytes */
86
87 if (unlikely(atomic_inc_return(&cmd->free) != 1)) {
88 pr_err("%s: Freeing cmd (%d) that is not in use!\n",
89 __func__, cmd->slot);
90 return;
91 }
92
Matthew R. Ochs4392ba42015-10-21 15:13:11 -050093 pr_devel("%s: released cmd %p index=%d\n", __func__, cmd, cmd->slot);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050094}
95
96/**
97 * process_cmd_err() - command error handler
98 * @cmd: AFU command that experienced the error.
99 * @scp: SCSI command associated with the AFU command in error.
100 *
101 * Translates error bits from AFU command to SCSI command results.
102 */
103static void process_cmd_err(struct afu_cmd *cmd, struct scsi_cmnd *scp)
104{
105 struct sisl_ioarcb *ioarcb;
106 struct sisl_ioasa *ioasa;
Matthew R. Ochs83960122015-10-21 15:13:29 -0500107 u32 resid;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500108
109 if (unlikely(!cmd))
110 return;
111
112 ioarcb = &(cmd->rcb);
113 ioasa = &(cmd->sa);
114
115 if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) {
Matthew R. Ochs83960122015-10-21 15:13:29 -0500116 resid = ioasa->resid;
117 scsi_set_resid(scp, resid);
118 pr_debug("%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
119 __func__, cmd, scp, resid);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500120 }
121
122 if (ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN) {
123 pr_debug("%s: cmd underrun cmd = %p scp = %p\n",
124 __func__, cmd, scp);
125 scp->result = (DID_ERROR << 16);
126 }
127
128 pr_debug("%s: cmd failed afu_rc=%d scsi_rc=%d fc_rc=%d "
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500129 "afu_extra=0x%X, scsi_extra=0x%X, fc_extra=0x%X\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500130 __func__, ioasa->rc.afu_rc, ioasa->rc.scsi_rc,
131 ioasa->rc.fc_rc, ioasa->afu_extra, ioasa->scsi_extra,
132 ioasa->fc_extra);
133
134 if (ioasa->rc.scsi_rc) {
135 /* We have a SCSI status */
136 if (ioasa->rc.flags & SISL_RC_FLAGS_SENSE_VALID) {
137 memcpy(scp->sense_buffer, ioasa->sense_data,
138 SISL_SENSE_DATA_LEN);
139 scp->result = ioasa->rc.scsi_rc;
140 } else
141 scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16);
142 }
143
144 /*
145 * We encountered an error. Set scp->result based on nature
146 * of error.
147 */
148 if (ioasa->rc.fc_rc) {
149 /* We have an FC status */
150 switch (ioasa->rc.fc_rc) {
151 case SISL_FC_RC_LINKDOWN:
152 scp->result = (DID_REQUEUE << 16);
153 break;
154 case SISL_FC_RC_RESID:
155 /* This indicates an FCP resid underrun */
156 if (!(ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN)) {
157 /* If the SISL_RC_FLAGS_OVERRUN flag was set,
158 * then we will handle this error else where.
159 * If not then we must handle it here.
Matthew R. Ochs83960122015-10-21 15:13:29 -0500160 * This is probably an AFU bug.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500161 */
162 scp->result = (DID_ERROR << 16);
163 }
164 break;
165 case SISL_FC_RC_RESIDERR:
166 /* Resid mismatch between adapter and device */
167 case SISL_FC_RC_TGTABORT:
168 case SISL_FC_RC_ABORTOK:
169 case SISL_FC_RC_ABORTFAIL:
170 case SISL_FC_RC_NOLOGI:
171 case SISL_FC_RC_ABORTPEND:
172 case SISL_FC_RC_WRABORTPEND:
173 case SISL_FC_RC_NOEXP:
174 case SISL_FC_RC_INUSE:
175 scp->result = (DID_ERROR << 16);
176 break;
177 }
178 }
179
180 if (ioasa->rc.afu_rc) {
181 /* We have an AFU error */
182 switch (ioasa->rc.afu_rc) {
183 case SISL_AFU_RC_NO_CHANNELS:
Matthew R. Ochs83960122015-10-21 15:13:29 -0500184 scp->result = (DID_NO_CONNECT << 16);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500185 break;
186 case SISL_AFU_RC_DATA_DMA_ERR:
187 switch (ioasa->afu_extra) {
188 case SISL_AFU_DMA_ERR_PAGE_IN:
189 /* Retry */
190 scp->result = (DID_IMM_RETRY << 16);
191 break;
192 case SISL_AFU_DMA_ERR_INVALID_EA:
193 default:
194 scp->result = (DID_ERROR << 16);
195 }
196 break;
197 case SISL_AFU_RC_OUT_OF_DATA_BUFS:
198 /* Retry */
199 scp->result = (DID_ALLOC_FAILURE << 16);
200 break;
201 default:
202 scp->result = (DID_ERROR << 16);
203 }
204 }
205}
206
207/**
208 * cmd_complete() - command completion handler
209 * @cmd: AFU command that has completed.
210 *
211 * Prepares and submits command that has either completed or timed out to
212 * the SCSI stack. Checks AFU command back into command pool for non-internal
213 * (rcb.scp populated) commands.
214 */
215static void cmd_complete(struct afu_cmd *cmd)
216{
217 struct scsi_cmnd *scp;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500218 ulong lock_flags;
219 struct afu *afu = cmd->parent;
220 struct cxlflash_cfg *cfg = afu->parent;
221 bool cmd_is_tmf;
222
223 spin_lock_irqsave(&cmd->slock, lock_flags);
224 cmd->sa.host_use_b[0] |= B_DONE;
225 spin_unlock_irqrestore(&cmd->slock, lock_flags);
226
227 if (cmd->rcb.scp) {
228 scp = cmd->rcb.scp;
Matthew R. Ochs83960122015-10-21 15:13:29 -0500229 if (unlikely(cmd->sa.ioasc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500230 process_cmd_err(cmd, scp);
231 else
232 scp->result = (DID_OK << 16);
233
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500234 cmd_is_tmf = cmd->cmd_tmf;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500235 cmd_checkin(cmd); /* Don't use cmd after here */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500236
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500237 pr_debug_ratelimited("%s: calling scsi_done scp=%p result=%X "
238 "ioasc=%d\n", __func__, scp, scp->result,
239 cmd->sa.ioasc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500240
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500241 scsi_dma_unmap(scp);
242 scp->scsi_done(scp);
243
244 if (cmd_is_tmf) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500245 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500246 cfg->tmf_active = false;
247 wake_up_all_locked(&cfg->tmf_waitq);
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500248 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500249 }
250 } else
251 complete(&cmd->cevent);
252}
253
254/**
Matthew R. Ochs15305512015-10-21 15:12:10 -0500255 * context_reset() - timeout handler for AFU commands
256 * @cmd: AFU command that timed out.
257 *
258 * Sends a reset to the AFU.
259 */
260static void context_reset(struct afu_cmd *cmd)
261{
262 int nretry = 0;
263 u64 rrin = 0x1;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500264 struct afu *afu = cmd->parent;
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600265 struct cxlflash_cfg *cfg = afu->parent;
266 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500267 ulong lock_flags;
268
269 pr_debug("%s: cmd=%p\n", __func__, cmd);
270
271 spin_lock_irqsave(&cmd->slock, lock_flags);
272
273 /* Already completed? */
274 if (cmd->sa.host_use_b[0] & B_DONE) {
275 spin_unlock_irqrestore(&cmd->slock, lock_flags);
276 return;
277 }
278
279 cmd->sa.host_use_b[0] |= (B_DONE | B_ERROR | B_TIMEOUT);
280 spin_unlock_irqrestore(&cmd->slock, lock_flags);
281
Matthew R. Ochs15305512015-10-21 15:12:10 -0500282 writeq_be(rrin, &afu->host_map->ioarrin);
283 do {
284 rrin = readq_be(&afu->host_map->ioarrin);
285 if (rrin != 0x1)
286 break;
287 /* Double delay each time */
Manoj N. Kumarea765432016-03-25 14:26:49 -0500288 udelay(1 << nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500289 } while (nretry++ < MC_ROOM_RETRY_CNT);
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600290
291 dev_dbg(dev, "%s: returning rrin=0x%016llX nretry=%d\n",
292 __func__, rrin, nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500293}
294
295/**
296 * send_cmd() - sends an AFU command
297 * @afu: AFU associated with the host.
298 * @cmd: AFU command to send.
299 *
300 * Return:
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500301 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochs15305512015-10-21 15:12:10 -0500302 */
303static int send_cmd(struct afu *afu, struct afu_cmd *cmd)
304{
305 struct cxlflash_cfg *cfg = afu->parent;
306 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500307 int rc = 0;
Uma Krishnan11f7b182016-11-28 18:41:45 -0600308 s64 room;
309 ulong lock_flags;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500310
311 /*
Uma Krishnan11f7b182016-11-28 18:41:45 -0600312 * To avoid the performance penalty of MMIO, spread the update of
313 * 'room' over multiple commands.
Matthew R. Ochs15305512015-10-21 15:12:10 -0500314 */
Uma Krishnan11f7b182016-11-28 18:41:45 -0600315 spin_lock_irqsave(&afu->rrin_slock, lock_flags);
316 if (--afu->room < 0) {
317 room = readq_be(&afu->host_map->cmd_room);
318 if (room <= 0) {
319 dev_dbg_ratelimited(dev, "%s: no cmd_room to send "
320 "0x%02X, room=0x%016llX\n",
321 __func__, cmd->rcb.cdb[0], room);
322 afu->room = 0;
323 rc = SCSI_MLQUEUE_HOST_BUSY;
324 goto out;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500325 }
Uma Krishnan11f7b182016-11-28 18:41:45 -0600326 afu->room = room - 1;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500327 }
328
Matthew R. Ochs15305512015-10-21 15:12:10 -0500329 writeq_be((u64)&cmd->rcb, &afu->host_map->ioarrin);
330out:
Uma Krishnan11f7b182016-11-28 18:41:45 -0600331 spin_unlock_irqrestore(&afu->rrin_slock, lock_flags);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500332 pr_devel("%s: cmd=%p len=%d ea=%p rc=%d\n", __func__, cmd,
333 cmd->rcb.data_len, (void *)cmd->rcb.data_ea, rc);
334 return rc;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500335}
336
337/**
338 * wait_resp() - polls for a response or timeout to a sent AFU command
339 * @afu: AFU associated with the host.
340 * @cmd: AFU command that was sent.
341 */
342static void wait_resp(struct afu *afu, struct afu_cmd *cmd)
343{
344 ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
345
346 timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
347 if (!timeout)
348 context_reset(cmd);
349
350 if (unlikely(cmd->sa.ioasc != 0))
351 pr_err("%s: CMD 0x%X failed, IOASC: flags 0x%X, afu_rc 0x%X, "
352 "scsi_rc 0x%X, fc_rc 0x%X\n", __func__, cmd->rcb.cdb[0],
353 cmd->sa.rc.flags, cmd->sa.rc.afu_rc, cmd->sa.rc.scsi_rc,
354 cmd->sa.rc.fc_rc);
355}
356
357/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500358 * send_tmf() - sends a Task Management Function (TMF)
359 * @afu: AFU to checkout from.
360 * @scp: SCSI command from stack.
361 * @tmfcmd: TMF command to send.
362 *
363 * Return:
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500364 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500365 */
366static int send_tmf(struct afu *afu, struct scsi_cmnd *scp, u64 tmfcmd)
367{
368 struct afu_cmd *cmd;
369
370 u32 port_sel = scp->device->channel + 1;
371 short lflag = 0;
372 struct Scsi_Host *host = scp->device->host;
373 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500374 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500375 ulong lock_flags;
376 int rc = 0;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500377 ulong to;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500378
Matthew R. Ochs15305512015-10-21 15:12:10 -0500379 cmd = cmd_checkout(afu);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500380 if (unlikely(!cmd)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500381 dev_err(dev, "%s: could not get a free command\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500382 rc = SCSI_MLQUEUE_HOST_BUSY;
383 goto out;
384 }
385
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500386 /* When Task Management Function is active do not send another */
387 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500388 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500389 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
390 !cfg->tmf_active,
391 cfg->tmf_slock);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500392 cfg->tmf_active = true;
393 cmd->cmd_tmf = true;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500394 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500395
396 cmd->rcb.ctx_id = afu->ctx_hndl;
397 cmd->rcb.port_sel = port_sel;
398 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
399
400 lflag = SISL_REQ_FLAGS_TMF_CMD;
401
402 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
403 SISL_REQ_FLAGS_SUP_UNDERRUN | lflag);
404
405 /* Stash the scp in the reserved field, for reuse during interrupt */
406 cmd->rcb.scp = scp;
407
408 /* Copy the CDB from the cmd passed in */
409 memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
410
411 /* Send the command */
Matthew R. Ochs15305512015-10-21 15:12:10 -0500412 rc = send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500413 if (unlikely(rc)) {
Matthew R. Ochs15305512015-10-21 15:12:10 -0500414 cmd_checkin(cmd);
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500415 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500416 cfg->tmf_active = false;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500417 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500418 goto out;
419 }
420
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500421 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
422 to = msecs_to_jiffies(5000);
423 to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
424 !cfg->tmf_active,
425 cfg->tmf_slock,
426 to);
427 if (!to) {
428 cfg->tmf_active = false;
429 dev_err(dev, "%s: TMF timed out!\n", __func__);
430 rc = -1;
431 }
432 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500433out:
434 return rc;
435}
436
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600437static void afu_unmap(struct kref *ref)
438{
439 struct afu *afu = container_of(ref, struct afu, mapcount);
440
441 if (likely(afu->afu_map)) {
442 cxl_psa_unmap((void __iomem *)afu->afu_map);
443 afu->afu_map = NULL;
444 }
445}
446
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500447/**
448 * cxlflash_driver_info() - information handler for this host driver
449 * @host: SCSI host associated with device.
450 *
451 * Return: A string describing the device.
452 */
453static const char *cxlflash_driver_info(struct Scsi_Host *host)
454{
455 return CXLFLASH_ADAPTER_NAME;
456}
457
458/**
459 * cxlflash_queuecommand() - sends a mid-layer request
460 * @host: SCSI host associated with device.
461 * @scp: SCSI command to send.
462 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500463 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500464 */
465static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
466{
467 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
468 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500469 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500470 struct afu_cmd *cmd;
471 u32 port_sel = scp->device->channel + 1;
472 int nseg, i, ncount;
473 struct scatterlist *sg;
474 ulong lock_flags;
475 short lflag = 0;
476 int rc = 0;
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600477 int kref_got = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500478
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500479 dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
480 "cdb=(%08X-%08X-%08X-%08X)\n",
481 __func__, scp, host->host_no, scp->device->channel,
482 scp->device->id, scp->device->lun,
483 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
484 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
485 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
486 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500487
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500488 /*
489 * If a Task Management Function is active, wait for it to complete
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500490 * before continuing with regular commands.
491 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500492 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500493 if (cfg->tmf_active) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500494 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500495 rc = SCSI_MLQUEUE_HOST_BUSY;
496 goto out;
497 }
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500498 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500499
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500500 switch (cfg->state) {
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500501 case STATE_RESET:
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500502 dev_dbg_ratelimited(dev, "%s: device is in reset!\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500503 rc = SCSI_MLQUEUE_HOST_BUSY;
504 goto out;
505 case STATE_FAILTERM:
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500506 dev_dbg_ratelimited(dev, "%s: device has failed!\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500507 scp->result = (DID_NO_CONNECT << 16);
508 scp->scsi_done(scp);
509 rc = 0;
510 goto out;
511 default:
512 break;
513 }
514
Matthew R. Ochs15305512015-10-21 15:12:10 -0500515 cmd = cmd_checkout(afu);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500516 if (unlikely(!cmd)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500517 dev_err(dev, "%s: could not get a free command\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500518 rc = SCSI_MLQUEUE_HOST_BUSY;
519 goto out;
520 }
521
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600522 kref_get(&cfg->afu->mapcount);
523 kref_got = 1;
524
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500525 cmd->rcb.ctx_id = afu->ctx_hndl;
526 cmd->rcb.port_sel = port_sel;
527 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
528
529 if (scp->sc_data_direction == DMA_TO_DEVICE)
530 lflag = SISL_REQ_FLAGS_HOST_WRITE;
531 else
532 lflag = SISL_REQ_FLAGS_HOST_READ;
533
534 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
535 SISL_REQ_FLAGS_SUP_UNDERRUN | lflag);
536
537 /* Stash the scp in the reserved field, for reuse during interrupt */
538 cmd->rcb.scp = scp;
539
540 nseg = scsi_dma_map(scp);
541 if (unlikely(nseg < 0)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500542 dev_err(dev, "%s: Fail DMA map! nseg=%d\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500543 __func__, nseg);
544 rc = SCSI_MLQUEUE_HOST_BUSY;
545 goto out;
546 }
547
548 ncount = scsi_sg_count(scp);
549 scsi_for_each_sg(scp, sg, ncount, i) {
550 cmd->rcb.data_len = sg_dma_len(sg);
551 cmd->rcb.data_ea = sg_dma_address(sg);
552 }
553
554 /* Copy the CDB from the scsi_cmnd passed in */
555 memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
556
557 /* Send the command */
Matthew R. Ochs15305512015-10-21 15:12:10 -0500558 rc = send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500559 if (unlikely(rc)) {
Matthew R. Ochs15305512015-10-21 15:12:10 -0500560 cmd_checkin(cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500561 scsi_dma_unmap(scp);
562 }
563
564out:
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600565 if (kref_got)
566 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500567 pr_devel("%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500568 return rc;
569}
570
571/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500572 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500573 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500574 */
575static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
576{
577 struct pci_dev *pdev = cfg->dev;
578
579 if (pci_channel_offline(pdev))
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500580 wait_event_timeout(cfg->reset_waitq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500581 !pci_channel_offline(pdev),
582 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
583}
584
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500585/**
586 * free_mem() - free memory associated with the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500587 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500588 */
589static void free_mem(struct cxlflash_cfg *cfg)
590{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500591 struct afu *afu = cfg->afu;
592
593 if (cfg->afu) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500594 free_pages((ulong)afu, get_order(sizeof(struct afu)));
595 cfg->afu = NULL;
596 }
597}
598
599/**
600 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500601 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500602 *
603 * Safe to call with AFU in a partially allocated/initialized state.
Manoj Kumaree91e332015-12-14 15:07:02 -0600604 *
605 * Cleans up all state associated with the command queue, and unmaps
606 * the MMIO space.
607 *
608 * - complete() will take care of commands we initiated (they'll be checked
609 * in as part of the cleanup that occurs after the completion)
610 *
611 * - cmd_checkin() will take care of entries that we did not initiate and that
612 * have not (and will not) complete because they are sitting on a [now stale]
613 * hardware queue
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500614 */
615static void stop_afu(struct cxlflash_cfg *cfg)
616{
617 int i;
618 struct afu *afu = cfg->afu;
Manoj Kumaree91e332015-12-14 15:07:02 -0600619 struct afu_cmd *cmd;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500620
621 if (likely(afu)) {
Manoj Kumaree91e332015-12-14 15:07:02 -0600622 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
623 cmd = &afu->cmd[i];
624 complete(&cmd->cevent);
625 if (!atomic_read(&cmd->free))
626 cmd_checkin(cmd);
627 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500628
629 if (likely(afu->afu_map)) {
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500630 cxl_psa_unmap((void __iomem *)afu->afu_map);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500631 afu->afu_map = NULL;
632 }
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600633 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500634 }
635}
636
637/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500638 * term_intr() - disables all AFU interrupts
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500639 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500640 * @level: Depth of allocation, where to begin waterfall tear down.
641 *
642 * Safe to call with AFU/MC in partially allocated/initialized state.
643 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500644static void term_intr(struct cxlflash_cfg *cfg, enum undo_level level)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500645{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500646 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500647 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500648
649 if (!afu || !cfg->mcctx) {
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500650 dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500651 return;
652 }
653
654 switch (level) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500655 case UNMAP_THREE:
656 cxl_unmap_afu_irq(cfg->mcctx, 3, afu);
657 case UNMAP_TWO:
658 cxl_unmap_afu_irq(cfg->mcctx, 2, afu);
659 case UNMAP_ONE:
660 cxl_unmap_afu_irq(cfg->mcctx, 1, afu);
661 case FREE_IRQ:
662 cxl_free_afu_irqs(cfg->mcctx);
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500663 /* fall through */
664 case UNDO_NOOP:
665 /* No action required */
666 break;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500667 }
668}
669
670/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500671 * term_mc() - terminates the master context
672 * @cfg: Internal structure associated with the host.
673 * @level: Depth of allocation, where to begin waterfall tear down.
674 *
675 * Safe to call with AFU/MC in partially allocated/initialized state.
676 */
677static void term_mc(struct cxlflash_cfg *cfg)
678{
679 int rc = 0;
680 struct afu *afu = cfg->afu;
681 struct device *dev = &cfg->dev->dev;
682
683 if (!afu || !cfg->mcctx) {
684 dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
685 return;
686 }
687
688 rc = cxl_stop_context(cfg->mcctx);
689 WARN_ON(rc);
690 cfg->mcctx = NULL;
691}
692
693/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500694 * term_afu() - terminates the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500695 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500696 *
697 * Safe to call with AFU/MC in partially allocated/initialized state.
698 */
699static void term_afu(struct cxlflash_cfg *cfg)
700{
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500701 /*
702 * Tear down is carefully orchestrated to ensure
703 * no interrupts can come in when the problem state
704 * area is unmapped.
705 *
706 * 1) Disable all AFU interrupts
707 * 2) Unmap the problem state area
708 * 3) Stop the master context
709 */
710 term_intr(cfg, UNMAP_THREE);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500711 if (cfg->afu)
712 stop_afu(cfg);
713
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500714 term_mc(cfg);
Uma Krishnan6ded8b32016-03-04 15:55:15 -0600715
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500716 pr_debug("%s: returning\n", __func__);
717}
718
719/**
Uma Krishnan704c4b02016-06-15 18:49:57 -0500720 * notify_shutdown() - notifies device of pending shutdown
721 * @cfg: Internal structure associated with the host.
722 * @wait: Whether to wait for shutdown processing to complete.
723 *
724 * This function will notify the AFU that the adapter is being shutdown
725 * and will wait for shutdown processing to complete if wait is true.
726 * This notification should flush pending I/Os to the device and halt
727 * further I/Os until the next AFU reset is issued and device restarted.
728 */
729static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
730{
731 struct afu *afu = cfg->afu;
732 struct device *dev = &cfg->dev->dev;
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500733 struct sisl_global_map __iomem *global;
Uma Krishnan704c4b02016-06-15 18:49:57 -0500734 struct dev_dependent_vals *ddv;
735 u64 reg, status;
736 int i, retry_cnt = 0;
737
738 ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
739 if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
740 return;
741
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500742 if (!afu || !afu->afu_map) {
743 dev_dbg(dev, "%s: The problem state area is not mapped\n",
744 __func__);
745 return;
746 }
747
748 global = &afu->afu_map->global;
749
Uma Krishnan704c4b02016-06-15 18:49:57 -0500750 /* Notify AFU */
751 for (i = 0; i < NUM_FC_PORTS; i++) {
752 reg = readq_be(&global->fc_regs[i][FC_CONFIG2 / 8]);
753 reg |= SISL_FC_SHUTDOWN_NORMAL;
754 writeq_be(reg, &global->fc_regs[i][FC_CONFIG2 / 8]);
755 }
756
757 if (!wait)
758 return;
759
760 /* Wait up to 1.5 seconds for shutdown processing to complete */
761 for (i = 0; i < NUM_FC_PORTS; i++) {
762 retry_cnt = 0;
763 while (true) {
764 status = readq_be(&global->fc_regs[i][FC_STATUS / 8]);
765 if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
766 break;
767 if (++retry_cnt >= MC_RETRY_CNT) {
768 dev_dbg(dev, "%s: port %d shutdown processing "
769 "not yet completed\n", __func__, i);
770 break;
771 }
772 msleep(100 * retry_cnt);
773 }
774 }
775}
776
777/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500778 * cxlflash_remove() - PCI entry point to tear down host
779 * @pdev: PCI device associated with the host.
780 *
781 * Safe to use as a cleanup in partially allocated/initialized state.
782 */
783static void cxlflash_remove(struct pci_dev *pdev)
784{
785 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
786 ulong lock_flags;
787
Uma Krishnanbabf9852016-09-02 15:39:16 -0500788 if (!pci_is_enabled(pdev)) {
789 pr_debug("%s: Device is disabled\n", __func__);
790 return;
791 }
792
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500793 /* If a Task Management Function is active, wait for it to complete
794 * before continuing with remove.
795 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500796 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500797 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500798 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
799 !cfg->tmf_active,
800 cfg->tmf_slock);
801 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500802
Uma Krishnan704c4b02016-06-15 18:49:57 -0500803 /* Notify AFU and wait for shutdown processing to complete */
804 notify_shutdown(cfg, true);
805
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500806 cfg->state = STATE_FAILTERM;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500807 cxlflash_stop_term_user_contexts(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500808
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500809 switch (cfg->init_state) {
810 case INIT_STATE_SCSI:
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500811 cxlflash_term_local_luns(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500812 scsi_remove_host(cfg->host);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -0500813 /* fall through */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500814 case INIT_STATE_AFU:
Matthew R. Ochsd8046212015-10-21 15:14:17 -0500815 cancel_work_sync(&cfg->work_q);
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600816 term_afu(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500817 case INIT_STATE_PCI:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500818 pci_disable_device(pdev);
819 case INIT_STATE_NONE:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500820 free_mem(cfg);
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -0500821 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500822 break;
823 }
824
825 pr_debug("%s: returning\n", __func__);
826}
827
828/**
829 * alloc_mem() - allocates the AFU and its command pool
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500830 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500831 *
832 * A partially allocated state remains on failure.
833 *
834 * Return:
835 * 0 on success
836 * -ENOMEM on failure to allocate memory
837 */
838static int alloc_mem(struct cxlflash_cfg *cfg)
839{
840 int rc = 0;
841 int i;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500842 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500843
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -0500844 /* AFU is ~12k, i.e. only one 64k page or up to four 4k pages */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500845 cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
846 get_order(sizeof(struct afu)));
847 if (unlikely(!cfg->afu)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500848 dev_err(dev, "%s: cannot get %d free pages\n",
849 __func__, get_order(sizeof(struct afu)));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500850 rc = -ENOMEM;
851 goto out;
852 }
853 cfg->afu->parent = cfg;
854 cfg->afu->afu_map = NULL;
855
Matthew R. Ochse7ab2d402016-11-28 18:42:01 -0600856 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500857 atomic_set(&cfg->afu->cmd[i].free, 1);
858 cfg->afu->cmd[i].slot = i;
859 }
860
861out:
862 return rc;
863}
864
865/**
866 * init_pci() - initializes the host as a PCI device
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500867 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500868 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500869 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500870 */
871static int init_pci(struct cxlflash_cfg *cfg)
872{
873 struct pci_dev *pdev = cfg->dev;
874 int rc = 0;
875
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500876 rc = pci_enable_device(pdev);
877 if (rc || pci_channel_offline(pdev)) {
878 if (pci_channel_offline(pdev)) {
879 cxlflash_wait_for_pci_err_recovery(cfg);
880 rc = pci_enable_device(pdev);
881 }
882
883 if (rc) {
884 dev_err(&pdev->dev, "%s: Cannot enable adapter\n",
885 __func__);
886 cxlflash_wait_for_pci_err_recovery(cfg);
Manoj N. Kumar961487e2016-03-04 15:55:14 -0600887 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500888 }
889 }
890
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500891out:
892 pr_debug("%s: returning rc=%d\n", __func__, rc);
893 return rc;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500894}
895
896/**
897 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500898 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500899 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500900 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500901 */
902static int init_scsi(struct cxlflash_cfg *cfg)
903{
904 struct pci_dev *pdev = cfg->dev;
905 int rc = 0;
906
907 rc = scsi_add_host(cfg->host, &pdev->dev);
908 if (rc) {
909 dev_err(&pdev->dev, "%s: scsi_add_host failed (rc=%d)\n",
910 __func__, rc);
911 goto out;
912 }
913
914 scsi_scan_host(cfg->host);
915
916out:
917 pr_debug("%s: returning rc=%d\n", __func__, rc);
918 return rc;
919}
920
921/**
922 * set_port_online() - transitions the specified host FC port to online state
923 * @fc_regs: Top of MMIO region defined for specified port.
924 *
925 * The provided MMIO region must be mapped prior to call. Online state means
926 * that the FC link layer has synced, completed the handshaking process, and
927 * is ready for login to start.
928 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500929static void set_port_online(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500930{
931 u64 cmdcfg;
932
933 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
934 cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
935 cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
936 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
937}
938
939/**
940 * set_port_offline() - transitions the specified host FC port to offline state
941 * @fc_regs: Top of MMIO region defined for specified port.
942 *
943 * The provided MMIO region must be mapped prior to call.
944 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500945static void set_port_offline(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500946{
947 u64 cmdcfg;
948
949 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
950 cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
951 cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
952 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
953}
954
955/**
956 * wait_port_online() - waits for the specified host FC port come online
957 * @fc_regs: Top of MMIO region defined for specified port.
958 * @delay_us: Number of microseconds to delay between reading port status.
959 * @nretry: Number of cycles to retry reading port status.
960 *
961 * The provided MMIO region must be mapped prior to call. This will timeout
962 * when the cable is not plugged in.
963 *
964 * Return:
965 * TRUE (1) when the specified port is online
966 * FALSE (0) when the specified port fails to come online after timeout
967 * -EINVAL when @delay_us is less than 1000
968 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500969static int wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500970{
971 u64 status;
972
973 if (delay_us < 1000) {
974 pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
975 return -EINVAL;
976 }
977
978 do {
979 msleep(delay_us / 1000);
980 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -0500981 if (status == U64_MAX)
982 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500983 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
984 nretry--);
985
986 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
987}
988
989/**
990 * wait_port_offline() - waits for the specified host FC port go offline
991 * @fc_regs: Top of MMIO region defined for specified port.
992 * @delay_us: Number of microseconds to delay between reading port status.
993 * @nretry: Number of cycles to retry reading port status.
994 *
995 * The provided MMIO region must be mapped prior to call.
996 *
997 * Return:
998 * TRUE (1) when the specified port is offline
999 * FALSE (0) when the specified port fails to go offline after timeout
1000 * -EINVAL when @delay_us is less than 1000
1001 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001002static int wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001003{
1004 u64 status;
1005
1006 if (delay_us < 1000) {
1007 pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
1008 return -EINVAL;
1009 }
1010
1011 do {
1012 msleep(delay_us / 1000);
1013 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -05001014 if (status == U64_MAX)
1015 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001016 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
1017 nretry--);
1018
1019 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
1020}
1021
1022/**
1023 * afu_set_wwpn() - configures the WWPN for the specified host FC port
1024 * @afu: AFU associated with the host that owns the specified FC port.
1025 * @port: Port number being configured.
1026 * @fc_regs: Top of MMIO region defined for specified port.
1027 * @wwpn: The world-wide-port-number previously discovered for port.
1028 *
1029 * The provided MMIO region must be mapped prior to call. As part of the
1030 * sequence to configure the WWPN, the port is toggled offline and then back
1031 * online. This toggling action can cause this routine to delay up to a few
1032 * seconds. When configured to use the internal LUN feature of the AFU, a
1033 * failure to come online is overridden.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001034 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001035static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
1036 u64 wwpn)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001037{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001038 set_port_offline(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001039 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1040 FC_PORT_STATUS_RETRY_CNT)) {
1041 pr_debug("%s: wait on port %d to go offline timed out\n",
1042 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001043 }
1044
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001045 writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
Matthew R. Ochs964497b2015-10-21 15:13:54 -05001046
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001047 set_port_online(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001048 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1049 FC_PORT_STATUS_RETRY_CNT)) {
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001050 pr_debug("%s: wait on port %d to go online timed out\n",
1051 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001052 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001053}
1054
1055/**
1056 * afu_link_reset() - resets the specified host FC port
1057 * @afu: AFU associated with the host that owns the specified FC port.
1058 * @port: Port number being configured.
1059 * @fc_regs: Top of MMIO region defined for specified port.
1060 *
1061 * The provided MMIO region must be mapped prior to call. The sequence to
1062 * reset the port involves toggling it offline and then back online. This
1063 * action can cause this routine to delay up to a few seconds. An effort
1064 * is made to maintain link with the device by switching to host to use
1065 * the alternate port exclusively while the reset takes place.
1066 * failure to come online is overridden.
1067 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001068static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001069{
1070 u64 port_sel;
1071
1072 /* first switch the AFU to the other links, if any */
1073 port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
Dan Carpenter4da74db2015-08-18 11:57:43 +03001074 port_sel &= ~(1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001075 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1076 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1077
1078 set_port_offline(fc_regs);
1079 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1080 FC_PORT_STATUS_RETRY_CNT))
1081 pr_err("%s: wait on port %d to go offline timed out\n",
1082 __func__, port);
1083
1084 set_port_online(fc_regs);
1085 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1086 FC_PORT_STATUS_RETRY_CNT))
1087 pr_err("%s: wait on port %d to go online timed out\n",
1088 __func__, port);
1089
1090 /* switch back to include this port */
Dan Carpenter4da74db2015-08-18 11:57:43 +03001091 port_sel |= (1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001092 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1093 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1094
1095 pr_debug("%s: returning port_sel=%lld\n", __func__, port_sel);
1096}
1097
1098/*
1099 * Asynchronous interrupt information table
1100 */
1101static const struct asyc_intr_info ainfo[] = {
1102 {SISL_ASTATUS_FC0_OTHER, "other error", 0, CLR_FC_ERROR | LINK_RESET},
1103 {SISL_ASTATUS_FC0_LOGO, "target initiated LOGO", 0, 0},
1104 {SISL_ASTATUS_FC0_CRC_T, "CRC threshold exceeded", 0, LINK_RESET},
Manoj Kumare6e6df32015-10-21 15:16:07 -05001105 {SISL_ASTATUS_FC0_LOGI_R, "login timed out, retrying", 0, LINK_RESET},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001106 {SISL_ASTATUS_FC0_LOGI_F, "login failed", 0, CLR_FC_ERROR},
Matthew R. Ochsef510742015-10-21 15:13:37 -05001107 {SISL_ASTATUS_FC0_LOGI_S, "login succeeded", 0, SCAN_HOST},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001108 {SISL_ASTATUS_FC0_LINK_DN, "link down", 0, 0},
Uma Krishnanbbbfae92016-09-02 15:38:48 -05001109 {SISL_ASTATUS_FC0_LINK_UP, "link up", 0, 0},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001110 {SISL_ASTATUS_FC1_OTHER, "other error", 1, CLR_FC_ERROR | LINK_RESET},
1111 {SISL_ASTATUS_FC1_LOGO, "target initiated LOGO", 1, 0},
1112 {SISL_ASTATUS_FC1_CRC_T, "CRC threshold exceeded", 1, LINK_RESET},
Manoj Kumara9be2942015-12-14 14:55:09 -06001113 {SISL_ASTATUS_FC1_LOGI_R, "login timed out, retrying", 1, LINK_RESET},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001114 {SISL_ASTATUS_FC1_LOGI_F, "login failed", 1, CLR_FC_ERROR},
Matthew R. Ochsef510742015-10-21 15:13:37 -05001115 {SISL_ASTATUS_FC1_LOGI_S, "login succeeded", 1, SCAN_HOST},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001116 {SISL_ASTATUS_FC1_LINK_DN, "link down", 1, 0},
Uma Krishnanbbbfae92016-09-02 15:38:48 -05001117 {SISL_ASTATUS_FC1_LINK_UP, "link up", 1, 0},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001118 {0x0, "", 0, 0} /* terminator */
1119};
1120
1121/**
1122 * find_ainfo() - locates and returns asynchronous interrupt information
1123 * @status: Status code set by AFU on error.
1124 *
1125 * Return: The located information or NULL when the status code is invalid.
1126 */
1127static const struct asyc_intr_info *find_ainfo(u64 status)
1128{
1129 const struct asyc_intr_info *info;
1130
1131 for (info = &ainfo[0]; info->status; info++)
1132 if (info->status == status)
1133 return info;
1134
1135 return NULL;
1136}
1137
1138/**
1139 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1140 * @afu: AFU associated with the host.
1141 */
1142static void afu_err_intr_init(struct afu *afu)
1143{
1144 int i;
1145 u64 reg;
1146
1147 /* global async interrupts: AFU clears afu_ctrl on context exit
1148 * if async interrupts were sent to that context. This prevents
1149 * the AFU form sending further async interrupts when
1150 * there is
1151 * nobody to receive them.
1152 */
1153
1154 /* mask all */
1155 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
1156 /* set LISN# to send and point to master context */
1157 reg = ((u64) (((afu->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
1158
1159 if (afu->internal_lun)
1160 reg |= 1; /* Bit 63 indicates local lun */
1161 writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1162 /* clear all */
1163 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1164 /* unmask bits that are of interest */
1165 /* note: afu can send an interrupt after this step */
1166 writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1167 /* clear again in case a bit came on after previous clear but before */
1168 /* unmask */
1169 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1170
1171 /* Clear/Set internal lun bits */
1172 reg = readq_be(&afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1173 reg &= SISL_FC_INTERNAL_MASK;
1174 if (afu->internal_lun)
1175 reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
1176 writeq_be(reg, &afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1177
1178 /* now clear FC errors */
1179 for (i = 0; i < NUM_FC_PORTS; i++) {
1180 writeq_be(0xFFFFFFFFU,
1181 &afu->afu_map->global.fc_regs[i][FC_ERROR / 8]);
1182 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRCAP / 8]);
1183 }
1184
1185 /* sync interrupts for master's IOARRIN write */
1186 /* note that unlike asyncs, there can be no pending sync interrupts */
1187 /* at this time (this is a fresh context and master has not written */
1188 /* IOARRIN yet), so there is nothing to clear. */
1189
1190 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1191 writeq_be(SISL_MSI_SYNC_ERROR, &afu->host_map->ctx_ctrl);
1192 writeq_be(SISL_ISTATUS_MASK, &afu->host_map->intr_mask);
1193}
1194
1195/**
1196 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1197 * @irq: Interrupt number.
1198 * @data: Private data provided at interrupt registration, the AFU.
1199 *
1200 * Return: Always return IRQ_HANDLED.
1201 */
1202static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1203{
1204 struct afu *afu = (struct afu *)data;
1205 u64 reg;
1206 u64 reg_unmasked;
1207
1208 reg = readq_be(&afu->host_map->intr_status);
1209 reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1210
1211 if (reg_unmasked == 0UL) {
1212 pr_err("%s: %llX: spurious interrupt, intr_status %016llX\n",
1213 __func__, (u64)afu, reg);
1214 goto cxlflash_sync_err_irq_exit;
1215 }
1216
1217 pr_err("%s: %llX: unexpected interrupt, intr_status %016llX\n",
1218 __func__, (u64)afu, reg);
1219
1220 writeq_be(reg_unmasked, &afu->host_map->intr_clear);
1221
1222cxlflash_sync_err_irq_exit:
1223 pr_debug("%s: returning rc=%d\n", __func__, IRQ_HANDLED);
1224 return IRQ_HANDLED;
1225}
1226
1227/**
1228 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1229 * @irq: Interrupt number.
1230 * @data: Private data provided at interrupt registration, the AFU.
1231 *
1232 * Return: Always return IRQ_HANDLED.
1233 */
1234static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
1235{
1236 struct afu *afu = (struct afu *)data;
1237 struct afu_cmd *cmd;
1238 bool toggle = afu->toggle;
1239 u64 entry,
1240 *hrrq_start = afu->hrrq_start,
1241 *hrrq_end = afu->hrrq_end,
1242 *hrrq_curr = afu->hrrq_curr;
1243
1244 /* Process however many RRQ entries that are ready */
1245 while (true) {
1246 entry = *hrrq_curr;
1247
1248 if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1249 break;
1250
1251 cmd = (struct afu_cmd *)(entry & ~SISL_RESP_HANDLE_T_BIT);
1252 cmd_complete(cmd);
1253
1254 /* Advance to next entry or wrap and flip the toggle bit */
1255 if (hrrq_curr < hrrq_end)
1256 hrrq_curr++;
1257 else {
1258 hrrq_curr = hrrq_start;
1259 toggle ^= SISL_RESP_HANDLE_T_BIT;
1260 }
1261 }
1262
1263 afu->hrrq_curr = hrrq_curr;
1264 afu->toggle = toggle;
1265
1266 return IRQ_HANDLED;
1267}
1268
1269/**
1270 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1271 * @irq: Interrupt number.
1272 * @data: Private data provided at interrupt registration, the AFU.
1273 *
1274 * Return: Always return IRQ_HANDLED.
1275 */
1276static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1277{
1278 struct afu *afu = (struct afu *)data;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001279 struct cxlflash_cfg *cfg = afu->parent;
1280 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001281 u64 reg_unmasked;
1282 const struct asyc_intr_info *info;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001283 struct sisl_global_map __iomem *global = &afu->afu_map->global;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001284 u64 reg;
1285 u8 port;
1286 int i;
1287
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001288 reg = readq_be(&global->regs.aintr_status);
1289 reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1290
1291 if (reg_unmasked == 0) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001292 dev_err(dev, "%s: spurious interrupt, aintr_status 0x%016llX\n",
1293 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001294 goto out;
1295 }
1296
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001297 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001298 writeq_be(reg_unmasked, &global->regs.aintr_clear);
1299
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001300 /* Check each bit that is on */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001301 for (i = 0; reg_unmasked; i++, reg_unmasked = (reg_unmasked >> 1)) {
1302 info = find_ainfo(1ULL << i);
Matthew R. Ochs16798d32015-10-21 15:13:45 -05001303 if (((reg_unmasked & 0x1) == 0) || !info)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001304 continue;
1305
1306 port = info->port;
1307
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001308 dev_err(dev, "%s: FC Port %d -> %s, fc_status 0x%08llX\n",
1309 __func__, port, info->desc,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001310 readq_be(&global->fc_regs[port][FC_STATUS / 8]));
1311
1312 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001313 * Do link reset first, some OTHER errors will set FC_ERROR
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001314 * again if cleared before or w/o a reset
1315 */
1316 if (info->action & LINK_RESET) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001317 dev_err(dev, "%s: FC Port %d: resetting link\n",
1318 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001319 cfg->lr_state = LINK_RESET_REQUIRED;
1320 cfg->lr_port = port;
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06001321 kref_get(&cfg->afu->mapcount);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001322 schedule_work(&cfg->work_q);
1323 }
1324
1325 if (info->action & CLR_FC_ERROR) {
1326 reg = readq_be(&global->fc_regs[port][FC_ERROR / 8]);
1327
1328 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001329 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001330 * should be the same and tracing one is sufficient.
1331 */
1332
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001333 dev_err(dev, "%s: fc %d: clearing fc_error 0x%08llX\n",
1334 __func__, port, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001335
1336 writeq_be(reg, &global->fc_regs[port][FC_ERROR / 8]);
1337 writeq_be(0, &global->fc_regs[port][FC_ERRCAP / 8]);
1338 }
Matthew R. Ochsef510742015-10-21 15:13:37 -05001339
1340 if (info->action & SCAN_HOST) {
1341 atomic_inc(&cfg->scan_host_needed);
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06001342 kref_get(&cfg->afu->mapcount);
Matthew R. Ochsef510742015-10-21 15:13:37 -05001343 schedule_work(&cfg->work_q);
1344 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001345 }
1346
1347out:
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001348 dev_dbg(dev, "%s: returning IRQ_HANDLED, afu=%p\n", __func__, afu);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001349 return IRQ_HANDLED;
1350}
1351
1352/**
1353 * start_context() - starts the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001354 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001355 *
1356 * Return: A success or failure value from CXL services.
1357 */
1358static int start_context(struct cxlflash_cfg *cfg)
1359{
1360 int rc = 0;
1361
1362 rc = cxl_start_context(cfg->mcctx,
1363 cfg->afu->work.work_element_descriptor,
1364 NULL);
1365
1366 pr_debug("%s: returning rc=%d\n", __func__, rc);
1367 return rc;
1368}
1369
1370/**
1371 * read_vpd() - obtains the WWPNs from VPD
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001372 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001373 * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
1374 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001375 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001376 */
1377static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1378{
Frederic Barratca946d4e2016-03-04 12:26:43 +01001379 struct pci_dev *dev = cfg->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001380 int rc = 0;
1381 int ro_start, ro_size, i, j, k;
1382 ssize_t vpd_size;
1383 char vpd_data[CXLFLASH_VPD_LEN];
1384 char tmp_buf[WWPN_BUF_LEN] = { 0 };
1385 char *wwpn_vpd_tags[NUM_FC_PORTS] = { "V5", "V6" };
1386
1387 /* Get the VPD data from the device */
Frederic Barratca946d4e2016-03-04 12:26:43 +01001388 vpd_size = cxl_read_adapter_vpd(dev, vpd_data, sizeof(vpd_data));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001389 if (unlikely(vpd_size <= 0)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001390 dev_err(&dev->dev, "%s: Unable to read VPD (size = %ld)\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001391 __func__, vpd_size);
1392 rc = -ENODEV;
1393 goto out;
1394 }
1395
1396 /* Get the read only section offset */
1397 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
1398 PCI_VPD_LRDT_RO_DATA);
1399 if (unlikely(ro_start < 0)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001400 dev_err(&dev->dev, "%s: VPD Read-only data not found\n",
1401 __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001402 rc = -ENODEV;
1403 goto out;
1404 }
1405
1406 /* Get the read only section size, cap when extends beyond read VPD */
1407 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1408 j = ro_size;
1409 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1410 if (unlikely((i + j) > vpd_size)) {
1411 pr_debug("%s: Might need to read more VPD (%d > %ld)\n",
1412 __func__, (i + j), vpd_size);
1413 ro_size = vpd_size - i;
1414 }
1415
1416 /*
1417 * Find the offset of the WWPN tag within the read only
1418 * VPD data and validate the found field (partials are
1419 * no good to us). Convert the ASCII data to an integer
1420 * value. Note that we must copy to a temporary buffer
1421 * because the conversion service requires that the ASCII
1422 * string be terminated.
1423 */
1424 for (k = 0; k < NUM_FC_PORTS; k++) {
1425 j = ro_size;
1426 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1427
1428 i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
1429 if (unlikely(i < 0)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001430 dev_err(&dev->dev, "%s: Port %d WWPN not found "
1431 "in VPD\n", __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001432 rc = -ENODEV;
1433 goto out;
1434 }
1435
1436 j = pci_vpd_info_field_size(&vpd_data[i]);
1437 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1438 if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001439 dev_err(&dev->dev, "%s: Port %d WWPN incomplete or "
1440 "VPD corrupt\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001441 __func__, k);
1442 rc = -ENODEV;
1443 goto out;
1444 }
1445
1446 memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1447 rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1448 if (unlikely(rc)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001449 dev_err(&dev->dev, "%s: Fail to convert port %d WWPN "
1450 "to integer\n", __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001451 rc = -ENODEV;
1452 goto out;
1453 }
1454 }
1455
1456out:
1457 pr_debug("%s: returning rc=%d\n", __func__, rc);
1458 return rc;
1459}
1460
1461/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001462 * init_pcr() - initialize the provisioning and control registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001463 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001464 *
1465 * Also sets up fast access to the mapped registers and initializes AFU
1466 * command fields that never change.
1467 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001468static void init_pcr(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001469{
1470 struct afu *afu = cfg->afu;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001471 struct sisl_ctrl_map __iomem *ctrl_map;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001472 int i;
1473
1474 for (i = 0; i < MAX_CONTEXT; i++) {
1475 ctrl_map = &afu->afu_map->ctrls[i].ctrl;
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001476 /* Disrupt any clients that could be running */
1477 /* e.g. clients that survived a master restart */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001478 writeq_be(0, &ctrl_map->rht_start);
1479 writeq_be(0, &ctrl_map->rht_cnt_id);
1480 writeq_be(0, &ctrl_map->ctx_cap);
1481 }
1482
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001483 /* Copy frequently used fields into afu */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001484 afu->ctx_hndl = (u16) cxl_process_element(cfg->mcctx);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001485 afu->host_map = &afu->afu_map->hosts[afu->ctx_hndl].host;
1486 afu->ctrl_map = &afu->afu_map->ctrls[afu->ctx_hndl].ctrl;
1487
1488 /* Program the Endian Control for the master context */
1489 writeq_be(SISL_ENDIAN_CTRL, &afu->host_map->endian_ctrl);
1490
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001491 /* Initialize cmd fields that never change */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001492 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
1493 afu->cmd[i].rcb.ctx_id = afu->ctx_hndl;
1494 afu->cmd[i].rcb.msi = SISL_MSI_RRQ_UPDATED;
1495 afu->cmd[i].rcb.rrq = 0x0;
1496 }
1497}
1498
1499/**
1500 * init_global() - initialize AFU global registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001501 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001502 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001503static int init_global(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001504{
1505 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001506 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001507 u64 wwpn[NUM_FC_PORTS]; /* wwpn of AFU ports */
1508 int i = 0, num_ports = 0;
1509 int rc = 0;
1510 u64 reg;
1511
1512 rc = read_vpd(cfg, &wwpn[0]);
1513 if (rc) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001514 dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001515 goto out;
1516 }
1517
1518 pr_debug("%s: wwpn0=0x%llX wwpn1=0x%llX\n", __func__, wwpn[0], wwpn[1]);
1519
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001520 /* Set up RRQ in AFU for master issued cmds */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001521 writeq_be((u64) afu->hrrq_start, &afu->host_map->rrq_start);
1522 writeq_be((u64) afu->hrrq_end, &afu->host_map->rrq_end);
1523
1524 /* AFU configuration */
1525 reg = readq_be(&afu->afu_map->global.regs.afu_config);
1526 reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1527 /* enable all auto retry options and control endianness */
1528 /* leave others at default: */
1529 /* CTX_CAP write protected, mbox_r does not clear on read and */
1530 /* checker on if dual afu */
1531 writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1532
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001533 /* Global port select: select either port */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001534 if (afu->internal_lun) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001535 /* Only use port 0 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001536 writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
1537 num_ports = NUM_FC_PORTS - 1;
1538 } else {
1539 writeq_be(BOTH_PORTS, &afu->afu_map->global.regs.afu_port_sel);
1540 num_ports = NUM_FC_PORTS;
1541 }
1542
1543 for (i = 0; i < num_ports; i++) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001544 /* Unmask all errors (but they are still masked at AFU) */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001545 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRMSK / 8]);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001546 /* Clear CRC error cnt & set a threshold */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001547 (void)readq_be(&afu->afu_map->global.
1548 fc_regs[i][FC_CNT_CRCERR / 8]);
1549 writeq_be(MC_CRC_THRESH, &afu->afu_map->global.fc_regs[i]
1550 [FC_CRC_THRESH / 8]);
1551
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001552 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001553 if (wwpn[i] != 0)
1554 afu_set_wwpn(afu, i,
1555 &afu->afu_map->global.fc_regs[i][0],
1556 wwpn[i]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001557 /* Programming WWPN back to back causes additional
1558 * offline/online transitions and a PLOGI
1559 */
1560 msleep(100);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001561 }
1562
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001563 /* Set up master's own CTX_CAP to allow real mode, host translation */
1564 /* tables, afu cmds and read/write GSCSI cmds. */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001565 /* First, unlock ctx_cap write by reading mbox */
1566 (void)readq_be(&afu->ctrl_map->mbox_r); /* unlock ctx_cap */
1567 writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1568 SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1569 SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1570 &afu->ctrl_map->ctx_cap);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001571 /* Initialize heartbeat */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001572 afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
1573
1574out:
1575 return rc;
1576}
1577
1578/**
1579 * start_afu() - initializes and starts the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001580 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001581 */
1582static int start_afu(struct cxlflash_cfg *cfg)
1583{
1584 struct afu *afu = cfg->afu;
1585 struct afu_cmd *cmd;
1586
1587 int i = 0;
1588 int rc = 0;
1589
1590 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
1591 cmd = &afu->cmd[i];
1592
1593 init_completion(&cmd->cevent);
1594 spin_lock_init(&cmd->slock);
1595 cmd->parent = afu;
1596 }
1597
1598 init_pcr(cfg);
1599
Matthew R. Ochsaf104832015-10-21 15:15:14 -05001600 /* After an AFU reset, RRQ entries are stale, clear them */
1601 memset(&afu->rrq_entry, 0, sizeof(afu->rrq_entry));
1602
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001603 /* Initialize RRQ pointers */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001604 afu->hrrq_start = &afu->rrq_entry[0];
1605 afu->hrrq_end = &afu->rrq_entry[NUM_RRQ_ENTRY - 1];
1606 afu->hrrq_curr = afu->hrrq_start;
1607 afu->toggle = 1;
1608
1609 rc = init_global(cfg);
1610
1611 pr_debug("%s: returning rc=%d\n", __func__, rc);
1612 return rc;
1613}
1614
1615/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001616 * init_intr() - setup interrupt handlers for the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001617 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001618 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001619 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001620 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001621static enum undo_level init_intr(struct cxlflash_cfg *cfg,
1622 struct cxl_context *ctx)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001623{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001624 struct afu *afu = cfg->afu;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001625 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001626 int rc = 0;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001627 enum undo_level level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001628
1629 rc = cxl_allocate_afu_irqs(ctx, 3);
1630 if (unlikely(rc)) {
1631 dev_err(dev, "%s: call to allocate_afu_irqs failed rc=%d!\n",
1632 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001633 level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001634 goto out;
1635 }
1636
1637 rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, afu,
1638 "SISL_MSI_SYNC_ERROR");
1639 if (unlikely(rc <= 0)) {
1640 dev_err(dev, "%s: IRQ 1 (SISL_MSI_SYNC_ERROR) map failed!\n",
1641 __func__);
1642 level = FREE_IRQ;
1643 goto out;
1644 }
1645
1646 rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, afu,
1647 "SISL_MSI_RRQ_UPDATED");
1648 if (unlikely(rc <= 0)) {
1649 dev_err(dev, "%s: IRQ 2 (SISL_MSI_RRQ_UPDATED) map failed!\n",
1650 __func__);
1651 level = UNMAP_ONE;
1652 goto out;
1653 }
1654
1655 rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, afu,
1656 "SISL_MSI_ASYNC_ERROR");
1657 if (unlikely(rc <= 0)) {
1658 dev_err(dev, "%s: IRQ 3 (SISL_MSI_ASYNC_ERROR) map failed!\n",
1659 __func__);
1660 level = UNMAP_TWO;
1661 goto out;
1662 }
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001663out:
1664 return level;
1665}
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001666
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001667/**
1668 * init_mc() - create and register as the master context
1669 * @cfg: Internal structure associated with the host.
1670 *
1671 * Return: 0 on success, -errno on failure
1672 */
1673static int init_mc(struct cxlflash_cfg *cfg)
1674{
1675 struct cxl_context *ctx;
1676 struct device *dev = &cfg->dev->dev;
1677 int rc = 0;
1678 enum undo_level level;
1679
1680 ctx = cxl_get_context(cfg->dev);
1681 if (unlikely(!ctx)) {
1682 rc = -ENOMEM;
1683 goto ret;
1684 }
1685 cfg->mcctx = ctx;
1686
1687 /* Set it up as a master with the CXL */
1688 cxl_set_master(ctx);
1689
1690 /* During initialization reset the AFU to start from a clean slate */
1691 rc = cxl_afu_reset(cfg->mcctx);
1692 if (unlikely(rc)) {
1693 dev_err(dev, "%s: initial AFU reset failed rc=%d\n",
1694 __func__, rc);
1695 goto ret;
1696 }
1697
1698 level = init_intr(cfg, ctx);
1699 if (unlikely(level)) {
1700 dev_err(dev, "%s: setting up interrupts failed rc=%d\n",
1701 __func__, rc);
1702 goto out;
1703 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001704
1705 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1706 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1707 * element (pe) that is embedded in the context (ctx)
1708 */
1709 rc = start_context(cfg);
1710 if (unlikely(rc)) {
1711 dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
1712 level = UNMAP_THREE;
1713 goto out;
1714 }
1715ret:
1716 pr_debug("%s: returning rc=%d\n", __func__, rc);
1717 return rc;
1718out:
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001719 term_intr(cfg, level);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001720 goto ret;
1721}
1722
1723/**
1724 * init_afu() - setup as master context and start AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001725 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001726 *
1727 * This routine is a higher level of control for configuring the
1728 * AFU on probe and reset paths.
1729 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001730 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001731 */
1732static int init_afu(struct cxlflash_cfg *cfg)
1733{
1734 u64 reg;
1735 int rc = 0;
1736 struct afu *afu = cfg->afu;
1737 struct device *dev = &cfg->dev->dev;
1738
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001739 cxl_perst_reloads_same_image(cfg->cxl_afu, true);
1740
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001741 rc = init_mc(cfg);
1742 if (rc) {
1743 dev_err(dev, "%s: call to init_mc failed, rc=%d!\n",
1744 __func__, rc);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001745 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001746 }
1747
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001748 /* Map the entire MMIO space of the AFU */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001749 afu->afu_map = cxl_psa_map(cfg->mcctx);
1750 if (!afu->afu_map) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001751 dev_err(dev, "%s: call to cxl_psa_map failed!\n", __func__);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001752 rc = -ENOMEM;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001753 goto err1;
1754 }
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06001755 kref_init(&afu->mapcount);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001756
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001757 /* No byte reverse on reading afu_version or string will be backwards */
1758 reg = readq(&afu->afu_map->global.regs.afu_version);
1759 memcpy(afu->version, &reg, sizeof(reg));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001760 afu->interface_version =
1761 readq_be(&afu->afu_map->global.regs.interface_version);
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001762 if ((afu->interface_version + 1) == 0) {
1763 pr_err("Back level AFU, please upgrade. AFU version %s "
1764 "interface version 0x%llx\n", afu->version,
1765 afu->interface_version);
1766 rc = -EINVAL;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001767 goto err2;
1768 }
1769
1770 pr_debug("%s: afu version %s, interface version 0x%llX\n", __func__,
1771 afu->version, afu->interface_version);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001772
1773 rc = start_afu(cfg);
1774 if (rc) {
1775 dev_err(dev, "%s: call to start_afu failed, rc=%d!\n",
1776 __func__, rc);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001777 goto err2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001778 }
1779
1780 afu_err_intr_init(cfg->afu);
Uma Krishnan11f7b182016-11-28 18:41:45 -06001781 spin_lock_init(&afu->rrin_slock);
1782 afu->room = readq_be(&afu->host_map->cmd_room);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001783
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05001784 /* Restore the LUN mappings */
1785 cxlflash_restore_luntable(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001786out:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001787 pr_debug("%s: returning rc=%d\n", __func__, rc);
1788 return rc;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001789
1790err2:
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06001791 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001792err1:
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001793 term_intr(cfg, UNMAP_THREE);
1794 term_mc(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001795 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001796}
1797
1798/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001799 * cxlflash_afu_sync() - builds and sends an AFU sync command
1800 * @afu: AFU associated with the host.
1801 * @ctx_hndl_u: Identifies context requesting sync.
1802 * @res_hndl_u: Identifies resource requesting sync.
1803 * @mode: Type of sync to issue (lightweight, heavyweight, global).
1804 *
1805 * The AFU can only take 1 sync command at a time. This routine enforces this
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001806 * limitation by using a mutex to provide exclusive access to the AFU during
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001807 * the sync. This design point requires calling threads to not be on interrupt
1808 * context due to the possibility of sleeping during concurrent sync operations.
1809 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001810 * AFU sync operations are only necessary and allowed when the device is
1811 * operating normally. When not operating normally, sync requests can occur as
1812 * part of cleaning up resources associated with an adapter prior to removal.
1813 * In this scenario, these requests are simply ignored (safe due to the AFU
1814 * going away).
1815 *
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001816 * Return:
1817 * 0 on success
1818 * -1 on failure
1819 */
1820int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
1821 res_hndl_t res_hndl_u, u8 mode)
1822{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001823 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001824 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001825 struct afu_cmd *cmd = NULL;
1826 int rc = 0;
1827 int retry_cnt = 0;
1828 static DEFINE_MUTEX(sync_active);
1829
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001830 if (cfg->state != STATE_NORMAL) {
1831 pr_debug("%s: Sync not required! (%u)\n", __func__, cfg->state);
1832 return 0;
1833 }
1834
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001835 mutex_lock(&sync_active);
1836retry:
Matthew R. Ochs15305512015-10-21 15:12:10 -05001837 cmd = cmd_checkout(afu);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001838 if (unlikely(!cmd)) {
1839 retry_cnt++;
1840 udelay(1000 * retry_cnt);
1841 if (retry_cnt < MC_RETRY_CNT)
1842 goto retry;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001843 dev_err(dev, "%s: could not get a free command\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001844 rc = -1;
1845 goto out;
1846 }
1847
1848 pr_debug("%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
1849
1850 memset(cmd->rcb.cdb, 0, sizeof(cmd->rcb.cdb));
1851
1852 cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
1853 cmd->rcb.port_sel = 0x0; /* NA */
1854 cmd->rcb.lun_id = 0x0; /* NA */
1855 cmd->rcb.data_len = 0x0;
1856 cmd->rcb.data_ea = 0x0;
1857 cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
1858
1859 cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
1860 cmd->rcb.cdb[1] = mode;
1861
1862 /* The cdb is aligned, no unaligned accessors required */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001863 *((__be16 *)&cmd->rcb.cdb[2]) = cpu_to_be16(ctx_hndl_u);
1864 *((__be32 *)&cmd->rcb.cdb[4]) = cpu_to_be32(res_hndl_u);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001865
Matthew R. Ochs15305512015-10-21 15:12:10 -05001866 rc = send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001867 if (unlikely(rc))
1868 goto out;
1869
Matthew R. Ochs15305512015-10-21 15:12:10 -05001870 wait_resp(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001871
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001872 /* Set on timeout */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001873 if (unlikely((cmd->sa.ioasc != 0) ||
1874 (cmd->sa.host_use_b[0] & B_ERROR)))
1875 rc = -1;
1876out:
1877 mutex_unlock(&sync_active);
1878 if (cmd)
Matthew R. Ochs15305512015-10-21 15:12:10 -05001879 cmd_checkin(cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001880 pr_debug("%s: returning rc=%d\n", __func__, rc);
1881 return rc;
1882}
1883
1884/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001885 * afu_reset() - resets the AFU
1886 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001887 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001888 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001889 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001890static int afu_reset(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001891{
1892 int rc = 0;
1893 /* Stop the context before the reset. Since the context is
1894 * no longer available restart it after the reset is complete
1895 */
1896
1897 term_afu(cfg);
1898
1899 rc = init_afu(cfg);
1900
1901 pr_debug("%s: returning rc=%d\n", __func__, rc);
1902 return rc;
1903}
1904
1905/**
Manoj N. Kumarf4113962016-06-15 18:49:20 -05001906 * drain_ioctls() - wait until all currently executing ioctls have completed
1907 * @cfg: Internal structure associated with the host.
1908 *
1909 * Obtain write access to read/write semaphore that wraps ioctl
1910 * handling to 'drain' ioctls currently executing.
1911 */
1912static void drain_ioctls(struct cxlflash_cfg *cfg)
1913{
1914 down_write(&cfg->ioctl_rwsem);
1915 up_write(&cfg->ioctl_rwsem);
1916}
1917
1918/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001919 * cxlflash_eh_device_reset_handler() - reset a single LUN
1920 * @scp: SCSI command to send.
1921 *
1922 * Return:
1923 * SUCCESS as defined in scsi/scsi.h
1924 * FAILED as defined in scsi/scsi.h
1925 */
1926static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
1927{
1928 int rc = SUCCESS;
1929 struct Scsi_Host *host = scp->device->host;
1930 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
1931 struct afu *afu = cfg->afu;
1932 int rcr = 0;
1933
1934 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
1935 "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
1936 host->host_no, scp->device->channel,
1937 scp->device->id, scp->device->lun,
1938 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1939 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1940 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1941 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
1942
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001943retry:
Matthew R. Ochs15305512015-10-21 15:12:10 -05001944 switch (cfg->state) {
1945 case STATE_NORMAL:
1946 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
1947 if (unlikely(rcr))
1948 rc = FAILED;
1949 break;
1950 case STATE_RESET:
1951 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001952 goto retry;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001953 default:
1954 rc = FAILED;
1955 break;
1956 }
1957
1958 pr_debug("%s: returning rc=%d\n", __func__, rc);
1959 return rc;
1960}
1961
1962/**
1963 * cxlflash_eh_host_reset_handler() - reset the host adapter
1964 * @scp: SCSI command from stack identifying host.
1965 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05001966 * Following a reset, the state is evaluated again in case an EEH occurred
1967 * during the reset. In such a scenario, the host reset will either yield
1968 * until the EEH recovery is complete or return success or failure based
1969 * upon the current device state.
1970 *
Matthew R. Ochs15305512015-10-21 15:12:10 -05001971 * Return:
1972 * SUCCESS as defined in scsi/scsi.h
1973 * FAILED as defined in scsi/scsi.h
1974 */
1975static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
1976{
1977 int rc = SUCCESS;
1978 int rcr = 0;
1979 struct Scsi_Host *host = scp->device->host;
1980 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
1981
1982 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
1983 "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
1984 host->host_no, scp->device->channel,
1985 scp->device->id, scp->device->lun,
1986 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1987 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1988 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1989 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
1990
1991 switch (cfg->state) {
1992 case STATE_NORMAL:
1993 cfg->state = STATE_RESET;
Manoj N. Kumarf4113962016-06-15 18:49:20 -05001994 drain_ioctls(cfg);
Matthew R. Ochs15305512015-10-21 15:12:10 -05001995 cxlflash_mark_contexts_error(cfg);
1996 rcr = afu_reset(cfg);
1997 if (rcr) {
1998 rc = FAILED;
1999 cfg->state = STATE_FAILTERM;
2000 } else
2001 cfg->state = STATE_NORMAL;
2002 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002003 ssleep(1);
2004 /* fall through */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002005 case STATE_RESET:
2006 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2007 if (cfg->state == STATE_NORMAL)
2008 break;
2009 /* fall through */
2010 default:
2011 rc = FAILED;
2012 break;
2013 }
2014
2015 pr_debug("%s: returning rc=%d\n", __func__, rc);
2016 return rc;
2017}
2018
2019/**
2020 * cxlflash_change_queue_depth() - change the queue depth for the device
2021 * @sdev: SCSI device destined for queue depth change.
2022 * @qdepth: Requested queue depth value to set.
2023 *
2024 * The requested queue depth is capped to the maximum supported value.
2025 *
2026 * Return: The actual queue depth set.
2027 */
2028static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
2029{
2030
2031 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
2032 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
2033
2034 scsi_change_queue_depth(sdev, qdepth);
2035 return sdev->queue_depth;
2036}
2037
2038/**
2039 * cxlflash_show_port_status() - queries and presents the current port status
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002040 * @port: Desired port for status reporting.
2041 * @afu: AFU owning the specified port.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002042 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2043 *
2044 * Return: The size of the ASCII string returned in @buf.
2045 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002046static ssize_t cxlflash_show_port_status(u32 port, struct afu *afu, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002047{
Matthew R. Ochs15305512015-10-21 15:12:10 -05002048 char *disp_status;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002049 u64 status;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002050 __be64 __iomem *fc_regs;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002051
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002052 if (port >= NUM_FC_PORTS)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002053 return 0;
2054
2055 fc_regs = &afu->afu_map->global.fc_regs[port][0];
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002056 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
2057 status &= FC_MTIP_STATUS_MASK;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002058
2059 if (status == FC_MTIP_STATUS_ONLINE)
2060 disp_status = "online";
2061 else if (status == FC_MTIP_STATUS_OFFLINE)
2062 disp_status = "offline";
2063 else
2064 disp_status = "unknown";
2065
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002066 return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002067}
2068
2069/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002070 * port0_show() - queries and presents the current status of port 0
2071 * @dev: Generic device associated with the host owning the port.
2072 * @attr: Device attribute representing the port.
2073 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002074 *
2075 * Return: The size of the ASCII string returned in @buf.
2076 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002077static ssize_t port0_show(struct device *dev,
2078 struct device_attribute *attr,
2079 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002080{
2081 struct Scsi_Host *shost = class_to_shost(dev);
2082 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2083 struct afu *afu = cfg->afu;
2084
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002085 return cxlflash_show_port_status(0, afu, buf);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002086}
2087
2088/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002089 * port1_show() - queries and presents the current status of port 1
2090 * @dev: Generic device associated with the host owning the port.
2091 * @attr: Device attribute representing the port.
2092 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2093 *
2094 * Return: The size of the ASCII string returned in @buf.
2095 */
2096static ssize_t port1_show(struct device *dev,
2097 struct device_attribute *attr,
2098 char *buf)
2099{
2100 struct Scsi_Host *shost = class_to_shost(dev);
2101 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2102 struct afu *afu = cfg->afu;
2103
2104 return cxlflash_show_port_status(1, afu, buf);
2105}
2106
2107/**
2108 * lun_mode_show() - presents the current LUN mode of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002109 * @dev: Generic device associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002110 * @attr: Device attribute representing the LUN mode.
2111 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2112 *
2113 * Return: The size of the ASCII string returned in @buf.
2114 */
2115static ssize_t lun_mode_show(struct device *dev,
2116 struct device_attribute *attr, char *buf)
2117{
2118 struct Scsi_Host *shost = class_to_shost(dev);
2119 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2120 struct afu *afu = cfg->afu;
2121
2122 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
2123}
2124
2125/**
2126 * lun_mode_store() - sets the LUN mode of the host
2127 * @dev: Generic device associated with the host.
2128 * @attr: Device attribute representing the LUN mode.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002129 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2130 * @count: Length of data resizing in @buf.
2131 *
2132 * The CXL Flash AFU supports a dummy LUN mode where the external
2133 * links and storage are not required. Space on the FPGA is used
2134 * to create 1 or 2 small LUNs which are presented to the system
2135 * as if they were a normal storage device. This feature is useful
2136 * during development and also provides manufacturing with a way
2137 * to test the AFU without an actual device.
2138 *
2139 * 0 = external LUN[s] (default)
2140 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2141 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2142 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2143 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2144 *
2145 * Return: The size of the ASCII string returned in @buf.
2146 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002147static ssize_t lun_mode_store(struct device *dev,
2148 struct device_attribute *attr,
2149 const char *buf, size_t count)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002150{
2151 struct Scsi_Host *shost = class_to_shost(dev);
2152 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2153 struct afu *afu = cfg->afu;
2154 int rc;
2155 u32 lun_mode;
2156
2157 rc = kstrtouint(buf, 10, &lun_mode);
2158 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
2159 afu->internal_lun = lun_mode;
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002160
2161 /*
2162 * When configured for internal LUN, there is only one channel,
2163 * channel number 0, else there will be 2 (default).
2164 */
2165 if (afu->internal_lun)
2166 shost->max_channel = 0;
2167 else
2168 shost->max_channel = NUM_FC_PORTS - 1;
2169
Matthew R. Ochs15305512015-10-21 15:12:10 -05002170 afu_reset(cfg);
2171 scsi_scan_host(cfg->host);
2172 }
2173
2174 return count;
2175}
2176
2177/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002178 * ioctl_version_show() - presents the current ioctl version of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002179 * @dev: Generic device associated with the host.
2180 * @attr: Device attribute representing the ioctl version.
2181 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2182 *
2183 * Return: The size of the ASCII string returned in @buf.
2184 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002185static ssize_t ioctl_version_show(struct device *dev,
2186 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002187{
2188 return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
2189}
2190
2191/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002192 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2193 * @port: Desired port for status reporting.
2194 * @afu: AFU owning the specified port.
2195 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2196 *
2197 * Return: The size of the ASCII string returned in @buf.
2198 */
2199static ssize_t cxlflash_show_port_lun_table(u32 port,
2200 struct afu *afu,
2201 char *buf)
2202{
2203 int i;
2204 ssize_t bytes = 0;
2205 __be64 __iomem *fc_port;
2206
2207 if (port >= NUM_FC_PORTS)
2208 return 0;
2209
2210 fc_port = &afu->afu_map->global.fc_port[port][0];
2211
2212 for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2213 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
2214 "%03d: %016llX\n", i, readq_be(&fc_port[i]));
2215 return bytes;
2216}
2217
2218/**
2219 * port0_lun_table_show() - presents the current LUN table of port 0
2220 * @dev: Generic device associated with the host owning the port.
2221 * @attr: Device attribute representing the port.
2222 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2223 *
2224 * Return: The size of the ASCII string returned in @buf.
2225 */
2226static ssize_t port0_lun_table_show(struct device *dev,
2227 struct device_attribute *attr,
2228 char *buf)
2229{
2230 struct Scsi_Host *shost = class_to_shost(dev);
2231 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2232 struct afu *afu = cfg->afu;
2233
2234 return cxlflash_show_port_lun_table(0, afu, buf);
2235}
2236
2237/**
2238 * port1_lun_table_show() - presents the current LUN table of port 1
2239 * @dev: Generic device associated with the host owning the port.
2240 * @attr: Device attribute representing the port.
2241 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2242 *
2243 * Return: The size of the ASCII string returned in @buf.
2244 */
2245static ssize_t port1_lun_table_show(struct device *dev,
2246 struct device_attribute *attr,
2247 char *buf)
2248{
2249 struct Scsi_Host *shost = class_to_shost(dev);
2250 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2251 struct afu *afu = cfg->afu;
2252
2253 return cxlflash_show_port_lun_table(1, afu, buf);
2254}
2255
2256/**
2257 * mode_show() - presents the current mode of the device
Matthew R. Ochs15305512015-10-21 15:12:10 -05002258 * @dev: Generic device associated with the device.
2259 * @attr: Device attribute representing the device mode.
2260 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2261 *
2262 * Return: The size of the ASCII string returned in @buf.
2263 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002264static ssize_t mode_show(struct device *dev,
2265 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002266{
2267 struct scsi_device *sdev = to_scsi_device(dev);
2268
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002269 return scnprintf(buf, PAGE_SIZE, "%s\n",
2270 sdev->hostdata ? "superpipe" : "legacy");
Matthew R. Ochs15305512015-10-21 15:12:10 -05002271}
2272
2273/*
2274 * Host attributes
2275 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002276static DEVICE_ATTR_RO(port0);
2277static DEVICE_ATTR_RO(port1);
2278static DEVICE_ATTR_RW(lun_mode);
2279static DEVICE_ATTR_RO(ioctl_version);
2280static DEVICE_ATTR_RO(port0_lun_table);
2281static DEVICE_ATTR_RO(port1_lun_table);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002282
2283static struct device_attribute *cxlflash_host_attrs[] = {
2284 &dev_attr_port0,
2285 &dev_attr_port1,
2286 &dev_attr_lun_mode,
2287 &dev_attr_ioctl_version,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002288 &dev_attr_port0_lun_table,
2289 &dev_attr_port1_lun_table,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002290 NULL
2291};
2292
2293/*
2294 * Device attributes
2295 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002296static DEVICE_ATTR_RO(mode);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002297
2298static struct device_attribute *cxlflash_dev_attrs[] = {
2299 &dev_attr_mode,
2300 NULL
2301};
2302
2303/*
2304 * Host template
2305 */
2306static struct scsi_host_template driver_template = {
2307 .module = THIS_MODULE,
2308 .name = CXLFLASH_ADAPTER_NAME,
2309 .info = cxlflash_driver_info,
2310 .ioctl = cxlflash_ioctl,
2311 .proc_name = CXLFLASH_NAME,
2312 .queuecommand = cxlflash_queuecommand,
2313 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
2314 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
2315 .change_queue_depth = cxlflash_change_queue_depth,
Manoj N. Kumar83430832016-03-04 15:55:20 -06002316 .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002317 .can_queue = CXLFLASH_MAX_CMDS,
2318 .this_id = -1,
Uma Krishnan68ab2d72016-11-28 18:41:06 -06002319 .sg_tablesize = 1, /* No scatter gather support */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002320 .max_sectors = CXLFLASH_MAX_SECTORS,
2321 .use_clustering = ENABLE_CLUSTERING,
2322 .shost_attrs = cxlflash_host_attrs,
2323 .sdev_attrs = cxlflash_dev_attrs,
2324};
2325
2326/*
2327 * Device dependent values
2328 */
Uma Krishnan96e1b662016-06-15 18:49:38 -05002329static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
2330 0ULL };
2331static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
Uma Krishnan704c4b02016-06-15 18:49:57 -05002332 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs15305512015-10-21 15:12:10 -05002333
2334/*
2335 * PCI device binding table
2336 */
2337static struct pci_device_id cxlflash_pci_table[] = {
2338 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
2339 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
Manoj Kumara2746fb2015-12-14 15:07:43 -06002340 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
2341 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
Matthew R. Ochs15305512015-10-21 15:12:10 -05002342 {}
2343};
2344
2345MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
2346
2347/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002348 * cxlflash_worker_thread() - work thread handler for the AFU
2349 * @work: Work structure contained within cxlflash associated with host.
2350 *
2351 * Handles the following events:
2352 * - Link reset which cannot be performed on interrupt context due to
2353 * blocking up to a few seconds
Matthew R. Ochsef510742015-10-21 15:13:37 -05002354 * - Rescan the host
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002355 */
2356static void cxlflash_worker_thread(struct work_struct *work)
2357{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002358 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
2359 work_q);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002360 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002361 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002362 int port;
2363 ulong lock_flags;
2364
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002365 /* Avoid MMIO if the device has failed */
2366
2367 if (cfg->state != STATE_NORMAL)
2368 return;
2369
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002370 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2371
2372 if (cfg->lr_state == LINK_RESET_REQUIRED) {
2373 port = cfg->lr_port;
2374 if (port < 0)
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002375 dev_err(dev, "%s: invalid port index %d\n",
2376 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002377 else {
2378 spin_unlock_irqrestore(cfg->host->host_lock,
2379 lock_flags);
2380
2381 /* The reset can block... */
2382 afu_link_reset(afu, port,
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05002383 &afu->afu_map->global.fc_regs[port][0]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002384 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2385 }
2386
2387 cfg->lr_state = LINK_RESET_COMPLETE;
2388 }
2389
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002390 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
Matthew R. Ochsef510742015-10-21 15:13:37 -05002391
2392 if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
2393 scsi_scan_host(cfg->host);
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06002394 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002395}
2396
2397/**
2398 * cxlflash_probe() - PCI entry point to add host
2399 * @pdev: PCI device associated with the host.
2400 * @dev_id: PCI device id associated with device.
2401 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002402 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002403 */
2404static int cxlflash_probe(struct pci_dev *pdev,
2405 const struct pci_device_id *dev_id)
2406{
2407 struct Scsi_Host *host;
2408 struct cxlflash_cfg *cfg = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002409 struct dev_dependent_vals *ddv;
2410 int rc = 0;
2411
2412 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
2413 __func__, pdev->irq);
2414
2415 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
2416 driver_template.max_sectors = ddv->max_sectors;
2417
2418 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
2419 if (!host) {
2420 dev_err(&pdev->dev, "%s: call to scsi_host_alloc failed!\n",
2421 __func__);
2422 rc = -ENOMEM;
2423 goto out;
2424 }
2425
2426 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
2427 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
2428 host->max_channel = NUM_FC_PORTS - 1;
2429 host->unique_id = host->host_no;
2430 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
2431
2432 cfg = (struct cxlflash_cfg *)host->hostdata;
2433 cfg->host = host;
2434 rc = alloc_mem(cfg);
2435 if (rc) {
Matthew R. Ochsfa3f2c62015-10-21 15:15:45 -05002436 dev_err(&pdev->dev, "%s: call to alloc_mem failed!\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002437 __func__);
2438 rc = -ENOMEM;
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -05002439 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002440 goto out;
2441 }
2442
2443 cfg->init_state = INIT_STATE_NONE;
2444 cfg->dev = pdev;
Matthew R. Ochs17ead262015-10-21 15:15:37 -05002445 cfg->cxl_fops = cxlflash_cxl_fops;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002446
2447 /*
2448 * The promoted LUNs move to the top of the LUN table. The rest stay
2449 * on the bottom half. The bottom half grows from the end
2450 * (index = 255), whereas the top half grows from the beginning
2451 * (index = 0).
2452 */
2453 cfg->promote_lun_index = 0;
2454 cfg->last_lun_index[0] = CXLFLASH_NUM_VLUNS/2 - 1;
2455 cfg->last_lun_index[1] = CXLFLASH_NUM_VLUNS/2 - 1;
2456
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002457 cfg->dev_id = (struct pci_device_id *)dev_id;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002458
2459 init_waitqueue_head(&cfg->tmf_waitq);
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002460 init_waitqueue_head(&cfg->reset_waitq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002461
2462 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
2463 cfg->lr_state = LINK_RESET_INVALID;
2464 cfg->lr_port = -1;
Matthew R. Ochs0d731222015-10-21 15:16:24 -05002465 spin_lock_init(&cfg->tmf_slock);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002466 mutex_init(&cfg->ctx_tbl_list_mutex);
2467 mutex_init(&cfg->ctx_recovery_mutex);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002468 init_rwsem(&cfg->ioctl_rwsem);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002469 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
2470 INIT_LIST_HEAD(&cfg->lluns);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002471
2472 pci_set_drvdata(pdev, cfg);
2473
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002474 cfg->cxl_afu = cxl_pci_to_afu(pdev);
2475
2476 rc = init_pci(cfg);
2477 if (rc) {
2478 dev_err(&pdev->dev, "%s: call to init_pci "
2479 "failed rc=%d!\n", __func__, rc);
2480 goto out_remove;
2481 }
2482 cfg->init_state = INIT_STATE_PCI;
2483
2484 rc = init_afu(cfg);
2485 if (rc) {
2486 dev_err(&pdev->dev, "%s: call to init_afu "
2487 "failed rc=%d!\n", __func__, rc);
2488 goto out_remove;
2489 }
2490 cfg->init_state = INIT_STATE_AFU;
2491
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002492 rc = init_scsi(cfg);
2493 if (rc) {
2494 dev_err(&pdev->dev, "%s: call to init_scsi "
2495 "failed rc=%d!\n", __func__, rc);
2496 goto out_remove;
2497 }
2498 cfg->init_state = INIT_STATE_SCSI;
2499
2500out:
2501 pr_debug("%s: returning rc=%d\n", __func__, rc);
2502 return rc;
2503
2504out_remove:
2505 cxlflash_remove(pdev);
2506 goto out;
2507}
2508
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002509/**
2510 * cxlflash_pci_error_detected() - called when a PCI error is detected
2511 * @pdev: PCI device struct.
2512 * @state: PCI channel state.
2513 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002514 * When an EEH occurs during an active reset, wait until the reset is
2515 * complete and then take action based upon the device state.
2516 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002517 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2518 */
2519static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
2520 pci_channel_state_t state)
2521{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002522 int rc = 0;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002523 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2524 struct device *dev = &cfg->dev->dev;
2525
2526 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
2527
2528 switch (state) {
2529 case pci_channel_io_frozen:
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002530 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2531 if (cfg->state == STATE_FAILTERM)
2532 return PCI_ERS_RESULT_DISCONNECT;
2533
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002534 cfg->state = STATE_RESET;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002535 scsi_block_requests(cfg->host);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002536 drain_ioctls(cfg);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002537 rc = cxlflash_mark_contexts_error(cfg);
2538 if (unlikely(rc))
2539 dev_err(dev, "%s: Failed to mark user contexts!(%d)\n",
2540 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05002541 term_afu(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002542 return PCI_ERS_RESULT_NEED_RESET;
2543 case pci_channel_io_perm_failure:
2544 cfg->state = STATE_FAILTERM;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002545 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002546 scsi_unblock_requests(cfg->host);
2547 return PCI_ERS_RESULT_DISCONNECT;
2548 default:
2549 break;
2550 }
2551 return PCI_ERS_RESULT_NEED_RESET;
2552}
2553
2554/**
2555 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2556 * @pdev: PCI device struct.
2557 *
2558 * This routine is called by the pci error recovery code after the PCI
2559 * slot has been reset, just before we should resume normal operations.
2560 *
2561 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2562 */
2563static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
2564{
2565 int rc = 0;
2566 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2567 struct device *dev = &cfg->dev->dev;
2568
2569 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2570
2571 rc = init_afu(cfg);
2572 if (unlikely(rc)) {
2573 dev_err(dev, "%s: EEH recovery failed! (%d)\n", __func__, rc);
2574 return PCI_ERS_RESULT_DISCONNECT;
2575 }
2576
2577 return PCI_ERS_RESULT_RECOVERED;
2578}
2579
2580/**
2581 * cxlflash_pci_resume() - called when normal operation can resume
2582 * @pdev: PCI device struct
2583 */
2584static void cxlflash_pci_resume(struct pci_dev *pdev)
2585{
2586 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2587 struct device *dev = &cfg->dev->dev;
2588
2589 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2590
2591 cfg->state = STATE_NORMAL;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002592 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002593 scsi_unblock_requests(cfg->host);
2594}
2595
2596static const struct pci_error_handlers cxlflash_err_handler = {
2597 .error_detected = cxlflash_pci_error_detected,
2598 .slot_reset = cxlflash_pci_slot_reset,
2599 .resume = cxlflash_pci_resume,
2600};
2601
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002602/*
2603 * PCI device structure
2604 */
2605static struct pci_driver cxlflash_driver = {
2606 .name = CXLFLASH_NAME,
2607 .id_table = cxlflash_pci_table,
2608 .probe = cxlflash_probe,
2609 .remove = cxlflash_remove,
Uma Krishnanbabf9852016-09-02 15:39:16 -05002610 .shutdown = cxlflash_remove,
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002611 .err_handler = &cxlflash_err_handler,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002612};
2613
2614/**
2615 * init_cxlflash() - module entry point
2616 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002617 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002618 */
2619static int __init init_cxlflash(void)
2620{
Uma Krishnan85599212015-12-14 15:06:33 -06002621 pr_info("%s: %s\n", __func__, CXLFLASH_ADAPTER_NAME);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002622
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002623 cxlflash_list_init();
2624
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002625 return pci_register_driver(&cxlflash_driver);
2626}
2627
2628/**
2629 * exit_cxlflash() - module exit point
2630 */
2631static void __exit exit_cxlflash(void)
2632{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002633 cxlflash_term_global_luns();
2634 cxlflash_free_errpage();
2635
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002636 pci_unregister_driver(&cxlflash_driver);
2637}
2638
2639module_init(init_cxlflash);
2640module_exit(exit_cxlflash);