blob: 0f13b4de448a96d7288cfa05a6ded8881b3974ef [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;
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001826 char *buf = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001827 int rc = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001828 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);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001836 buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
1837 if (unlikely(!buf)) {
1838 dev_err(dev, "%s: no memory for command\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001839 rc = -1;
1840 goto out;
1841 }
1842
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001843 cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
1844 init_completion(&cmd->cevent);
1845 spin_lock_init(&cmd->slock);
1846 cmd->parent = afu;
1847
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001848 pr_debug("%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
1849
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001850 cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001851 cmd->rcb.ctx_id = afu->ctx_hndl;
1852 cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001853 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);
Matthew R. Ochs350bb472016-11-28 18:42:11 -06001878 kfree(buf);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001879 pr_debug("%s: returning rc=%d\n", __func__, rc);
1880 return rc;
1881}
1882
1883/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001884 * afu_reset() - resets the AFU
1885 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001886 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001887 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001888 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001889static int afu_reset(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001890{
1891 int rc = 0;
1892 /* Stop the context before the reset. Since the context is
1893 * no longer available restart it after the reset is complete
1894 */
1895
1896 term_afu(cfg);
1897
1898 rc = init_afu(cfg);
1899
1900 pr_debug("%s: returning rc=%d\n", __func__, rc);
1901 return rc;
1902}
1903
1904/**
Manoj N. Kumarf4113962016-06-15 18:49:20 -05001905 * drain_ioctls() - wait until all currently executing ioctls have completed
1906 * @cfg: Internal structure associated with the host.
1907 *
1908 * Obtain write access to read/write semaphore that wraps ioctl
1909 * handling to 'drain' ioctls currently executing.
1910 */
1911static void drain_ioctls(struct cxlflash_cfg *cfg)
1912{
1913 down_write(&cfg->ioctl_rwsem);
1914 up_write(&cfg->ioctl_rwsem);
1915}
1916
1917/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001918 * cxlflash_eh_device_reset_handler() - reset a single LUN
1919 * @scp: SCSI command to send.
1920 *
1921 * Return:
1922 * SUCCESS as defined in scsi/scsi.h
1923 * FAILED as defined in scsi/scsi.h
1924 */
1925static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
1926{
1927 int rc = SUCCESS;
1928 struct Scsi_Host *host = scp->device->host;
1929 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
1930 struct afu *afu = cfg->afu;
1931 int rcr = 0;
1932
1933 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
1934 "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
1935 host->host_no, scp->device->channel,
1936 scp->device->id, scp->device->lun,
1937 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1938 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1939 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1940 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
1941
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001942retry:
Matthew R. Ochs15305512015-10-21 15:12:10 -05001943 switch (cfg->state) {
1944 case STATE_NORMAL:
1945 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
1946 if (unlikely(rcr))
1947 rc = FAILED;
1948 break;
1949 case STATE_RESET:
1950 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001951 goto retry;
Matthew R. Ochs15305512015-10-21 15:12:10 -05001952 default:
1953 rc = FAILED;
1954 break;
1955 }
1956
1957 pr_debug("%s: returning rc=%d\n", __func__, rc);
1958 return rc;
1959}
1960
1961/**
1962 * cxlflash_eh_host_reset_handler() - reset the host adapter
1963 * @scp: SCSI command from stack identifying host.
1964 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05001965 * Following a reset, the state is evaluated again in case an EEH occurred
1966 * during the reset. In such a scenario, the host reset will either yield
1967 * until the EEH recovery is complete or return success or failure based
1968 * upon the current device state.
1969 *
Matthew R. Ochs15305512015-10-21 15:12:10 -05001970 * Return:
1971 * SUCCESS as defined in scsi/scsi.h
1972 * FAILED as defined in scsi/scsi.h
1973 */
1974static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
1975{
1976 int rc = SUCCESS;
1977 int rcr = 0;
1978 struct Scsi_Host *host = scp->device->host;
1979 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
1980
1981 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
1982 "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
1983 host->host_no, scp->device->channel,
1984 scp->device->id, scp->device->lun,
1985 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1986 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1987 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1988 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
1989
1990 switch (cfg->state) {
1991 case STATE_NORMAL:
1992 cfg->state = STATE_RESET;
Manoj N. Kumarf4113962016-06-15 18:49:20 -05001993 drain_ioctls(cfg);
Matthew R. Ochs15305512015-10-21 15:12:10 -05001994 cxlflash_mark_contexts_error(cfg);
1995 rcr = afu_reset(cfg);
1996 if (rcr) {
1997 rc = FAILED;
1998 cfg->state = STATE_FAILTERM;
1999 } else
2000 cfg->state = STATE_NORMAL;
2001 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002002 ssleep(1);
2003 /* fall through */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002004 case STATE_RESET:
2005 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2006 if (cfg->state == STATE_NORMAL)
2007 break;
2008 /* fall through */
2009 default:
2010 rc = FAILED;
2011 break;
2012 }
2013
2014 pr_debug("%s: returning rc=%d\n", __func__, rc);
2015 return rc;
2016}
2017
2018/**
2019 * cxlflash_change_queue_depth() - change the queue depth for the device
2020 * @sdev: SCSI device destined for queue depth change.
2021 * @qdepth: Requested queue depth value to set.
2022 *
2023 * The requested queue depth is capped to the maximum supported value.
2024 *
2025 * Return: The actual queue depth set.
2026 */
2027static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
2028{
2029
2030 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
2031 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
2032
2033 scsi_change_queue_depth(sdev, qdepth);
2034 return sdev->queue_depth;
2035}
2036
2037/**
2038 * cxlflash_show_port_status() - queries and presents the current port status
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002039 * @port: Desired port for status reporting.
2040 * @afu: AFU owning the specified port.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002041 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2042 *
2043 * Return: The size of the ASCII string returned in @buf.
2044 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002045static ssize_t cxlflash_show_port_status(u32 port, struct afu *afu, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002046{
Matthew R. Ochs15305512015-10-21 15:12:10 -05002047 char *disp_status;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002048 u64 status;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002049 __be64 __iomem *fc_regs;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002050
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002051 if (port >= NUM_FC_PORTS)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002052 return 0;
2053
2054 fc_regs = &afu->afu_map->global.fc_regs[port][0];
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002055 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
2056 status &= FC_MTIP_STATUS_MASK;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002057
2058 if (status == FC_MTIP_STATUS_ONLINE)
2059 disp_status = "online";
2060 else if (status == FC_MTIP_STATUS_OFFLINE)
2061 disp_status = "offline";
2062 else
2063 disp_status = "unknown";
2064
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002065 return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002066}
2067
2068/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002069 * port0_show() - queries and presents the current status of port 0
2070 * @dev: Generic device associated with the host owning the port.
2071 * @attr: Device attribute representing the port.
2072 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002073 *
2074 * Return: The size of the ASCII string returned in @buf.
2075 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002076static ssize_t port0_show(struct device *dev,
2077 struct device_attribute *attr,
2078 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002079{
2080 struct Scsi_Host *shost = class_to_shost(dev);
2081 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2082 struct afu *afu = cfg->afu;
2083
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002084 return cxlflash_show_port_status(0, afu, buf);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002085}
2086
2087/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002088 * port1_show() - queries and presents the current status of port 1
2089 * @dev: Generic device associated with the host owning the port.
2090 * @attr: Device attribute representing the port.
2091 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2092 *
2093 * Return: The size of the ASCII string returned in @buf.
2094 */
2095static ssize_t port1_show(struct device *dev,
2096 struct device_attribute *attr,
2097 char *buf)
2098{
2099 struct Scsi_Host *shost = class_to_shost(dev);
2100 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2101 struct afu *afu = cfg->afu;
2102
2103 return cxlflash_show_port_status(1, afu, buf);
2104}
2105
2106/**
2107 * lun_mode_show() - presents the current LUN mode of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002108 * @dev: Generic device associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002109 * @attr: Device attribute representing the LUN mode.
2110 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2111 *
2112 * Return: The size of the ASCII string returned in @buf.
2113 */
2114static ssize_t lun_mode_show(struct device *dev,
2115 struct device_attribute *attr, char *buf)
2116{
2117 struct Scsi_Host *shost = class_to_shost(dev);
2118 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2119 struct afu *afu = cfg->afu;
2120
2121 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
2122}
2123
2124/**
2125 * lun_mode_store() - sets the LUN mode of the host
2126 * @dev: Generic device associated with the host.
2127 * @attr: Device attribute representing the LUN mode.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002128 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2129 * @count: Length of data resizing in @buf.
2130 *
2131 * The CXL Flash AFU supports a dummy LUN mode where the external
2132 * links and storage are not required. Space on the FPGA is used
2133 * to create 1 or 2 small LUNs which are presented to the system
2134 * as if they were a normal storage device. This feature is useful
2135 * during development and also provides manufacturing with a way
2136 * to test the AFU without an actual device.
2137 *
2138 * 0 = external LUN[s] (default)
2139 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2140 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2141 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2142 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2143 *
2144 * Return: The size of the ASCII string returned in @buf.
2145 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002146static ssize_t lun_mode_store(struct device *dev,
2147 struct device_attribute *attr,
2148 const char *buf, size_t count)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002149{
2150 struct Scsi_Host *shost = class_to_shost(dev);
2151 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2152 struct afu *afu = cfg->afu;
2153 int rc;
2154 u32 lun_mode;
2155
2156 rc = kstrtouint(buf, 10, &lun_mode);
2157 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
2158 afu->internal_lun = lun_mode;
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002159
2160 /*
2161 * When configured for internal LUN, there is only one channel,
2162 * channel number 0, else there will be 2 (default).
2163 */
2164 if (afu->internal_lun)
2165 shost->max_channel = 0;
2166 else
2167 shost->max_channel = NUM_FC_PORTS - 1;
2168
Matthew R. Ochs15305512015-10-21 15:12:10 -05002169 afu_reset(cfg);
2170 scsi_scan_host(cfg->host);
2171 }
2172
2173 return count;
2174}
2175
2176/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002177 * ioctl_version_show() - presents the current ioctl version of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002178 * @dev: Generic device associated with the host.
2179 * @attr: Device attribute representing the ioctl version.
2180 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2181 *
2182 * Return: The size of the ASCII string returned in @buf.
2183 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002184static ssize_t ioctl_version_show(struct device *dev,
2185 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002186{
2187 return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
2188}
2189
2190/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002191 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2192 * @port: Desired port for status reporting.
2193 * @afu: AFU owning the specified port.
2194 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2195 *
2196 * Return: The size of the ASCII string returned in @buf.
2197 */
2198static ssize_t cxlflash_show_port_lun_table(u32 port,
2199 struct afu *afu,
2200 char *buf)
2201{
2202 int i;
2203 ssize_t bytes = 0;
2204 __be64 __iomem *fc_port;
2205
2206 if (port >= NUM_FC_PORTS)
2207 return 0;
2208
2209 fc_port = &afu->afu_map->global.fc_port[port][0];
2210
2211 for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2212 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
2213 "%03d: %016llX\n", i, readq_be(&fc_port[i]));
2214 return bytes;
2215}
2216
2217/**
2218 * port0_lun_table_show() - presents the current LUN table of port 0
2219 * @dev: Generic device associated with the host owning the port.
2220 * @attr: Device attribute representing the port.
2221 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2222 *
2223 * Return: The size of the ASCII string returned in @buf.
2224 */
2225static ssize_t port0_lun_table_show(struct device *dev,
2226 struct device_attribute *attr,
2227 char *buf)
2228{
2229 struct Scsi_Host *shost = class_to_shost(dev);
2230 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2231 struct afu *afu = cfg->afu;
2232
2233 return cxlflash_show_port_lun_table(0, afu, buf);
2234}
2235
2236/**
2237 * port1_lun_table_show() - presents the current LUN table of port 1
2238 * @dev: Generic device associated with the host owning the port.
2239 * @attr: Device attribute representing the port.
2240 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2241 *
2242 * Return: The size of the ASCII string returned in @buf.
2243 */
2244static ssize_t port1_lun_table_show(struct device *dev,
2245 struct device_attribute *attr,
2246 char *buf)
2247{
2248 struct Scsi_Host *shost = class_to_shost(dev);
2249 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2250 struct afu *afu = cfg->afu;
2251
2252 return cxlflash_show_port_lun_table(1, afu, buf);
2253}
2254
2255/**
2256 * mode_show() - presents the current mode of the device
Matthew R. Ochs15305512015-10-21 15:12:10 -05002257 * @dev: Generic device associated with the device.
2258 * @attr: Device attribute representing the device mode.
2259 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2260 *
2261 * Return: The size of the ASCII string returned in @buf.
2262 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002263static ssize_t mode_show(struct device *dev,
2264 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002265{
2266 struct scsi_device *sdev = to_scsi_device(dev);
2267
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002268 return scnprintf(buf, PAGE_SIZE, "%s\n",
2269 sdev->hostdata ? "superpipe" : "legacy");
Matthew R. Ochs15305512015-10-21 15:12:10 -05002270}
2271
2272/*
2273 * Host attributes
2274 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002275static DEVICE_ATTR_RO(port0);
2276static DEVICE_ATTR_RO(port1);
2277static DEVICE_ATTR_RW(lun_mode);
2278static DEVICE_ATTR_RO(ioctl_version);
2279static DEVICE_ATTR_RO(port0_lun_table);
2280static DEVICE_ATTR_RO(port1_lun_table);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002281
2282static struct device_attribute *cxlflash_host_attrs[] = {
2283 &dev_attr_port0,
2284 &dev_attr_port1,
2285 &dev_attr_lun_mode,
2286 &dev_attr_ioctl_version,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002287 &dev_attr_port0_lun_table,
2288 &dev_attr_port1_lun_table,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002289 NULL
2290};
2291
2292/*
2293 * Device attributes
2294 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002295static DEVICE_ATTR_RO(mode);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002296
2297static struct device_attribute *cxlflash_dev_attrs[] = {
2298 &dev_attr_mode,
2299 NULL
2300};
2301
2302/*
2303 * Host template
2304 */
2305static struct scsi_host_template driver_template = {
2306 .module = THIS_MODULE,
2307 .name = CXLFLASH_ADAPTER_NAME,
2308 .info = cxlflash_driver_info,
2309 .ioctl = cxlflash_ioctl,
2310 .proc_name = CXLFLASH_NAME,
2311 .queuecommand = cxlflash_queuecommand,
2312 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
2313 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
2314 .change_queue_depth = cxlflash_change_queue_depth,
Manoj N. Kumar83430832016-03-04 15:55:20 -06002315 .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002316 .can_queue = CXLFLASH_MAX_CMDS,
2317 .this_id = -1,
Uma Krishnan68ab2d72016-11-28 18:41:06 -06002318 .sg_tablesize = 1, /* No scatter gather support */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002319 .max_sectors = CXLFLASH_MAX_SECTORS,
2320 .use_clustering = ENABLE_CLUSTERING,
2321 .shost_attrs = cxlflash_host_attrs,
2322 .sdev_attrs = cxlflash_dev_attrs,
2323};
2324
2325/*
2326 * Device dependent values
2327 */
Uma Krishnan96e1b662016-06-15 18:49:38 -05002328static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
2329 0ULL };
2330static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
Uma Krishnan704c4b02016-06-15 18:49:57 -05002331 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs15305512015-10-21 15:12:10 -05002332
2333/*
2334 * PCI device binding table
2335 */
2336static struct pci_device_id cxlflash_pci_table[] = {
2337 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
2338 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
Manoj Kumara2746fb2015-12-14 15:07:43 -06002339 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
2340 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
Matthew R. Ochs15305512015-10-21 15:12:10 -05002341 {}
2342};
2343
2344MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
2345
2346/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002347 * cxlflash_worker_thread() - work thread handler for the AFU
2348 * @work: Work structure contained within cxlflash associated with host.
2349 *
2350 * Handles the following events:
2351 * - Link reset which cannot be performed on interrupt context due to
2352 * blocking up to a few seconds
Matthew R. Ochsef510742015-10-21 15:13:37 -05002353 * - Rescan the host
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002354 */
2355static void cxlflash_worker_thread(struct work_struct *work)
2356{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002357 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
2358 work_q);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002359 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002360 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002361 int port;
2362 ulong lock_flags;
2363
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002364 /* Avoid MMIO if the device has failed */
2365
2366 if (cfg->state != STATE_NORMAL)
2367 return;
2368
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002369 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2370
2371 if (cfg->lr_state == LINK_RESET_REQUIRED) {
2372 port = cfg->lr_port;
2373 if (port < 0)
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002374 dev_err(dev, "%s: invalid port index %d\n",
2375 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002376 else {
2377 spin_unlock_irqrestore(cfg->host->host_lock,
2378 lock_flags);
2379
2380 /* The reset can block... */
2381 afu_link_reset(afu, port,
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05002382 &afu->afu_map->global.fc_regs[port][0]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002383 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2384 }
2385
2386 cfg->lr_state = LINK_RESET_COMPLETE;
2387 }
2388
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002389 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
Matthew R. Ochsef510742015-10-21 15:13:37 -05002390
2391 if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
2392 scsi_scan_host(cfg->host);
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06002393 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002394}
2395
2396/**
2397 * cxlflash_probe() - PCI entry point to add host
2398 * @pdev: PCI device associated with the host.
2399 * @dev_id: PCI device id associated with device.
2400 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002401 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002402 */
2403static int cxlflash_probe(struct pci_dev *pdev,
2404 const struct pci_device_id *dev_id)
2405{
2406 struct Scsi_Host *host;
2407 struct cxlflash_cfg *cfg = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002408 struct dev_dependent_vals *ddv;
2409 int rc = 0;
2410
2411 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
2412 __func__, pdev->irq);
2413
2414 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
2415 driver_template.max_sectors = ddv->max_sectors;
2416
2417 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
2418 if (!host) {
2419 dev_err(&pdev->dev, "%s: call to scsi_host_alloc failed!\n",
2420 __func__);
2421 rc = -ENOMEM;
2422 goto out;
2423 }
2424
2425 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
2426 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
2427 host->max_channel = NUM_FC_PORTS - 1;
2428 host->unique_id = host->host_no;
2429 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
2430
2431 cfg = (struct cxlflash_cfg *)host->hostdata;
2432 cfg->host = host;
2433 rc = alloc_mem(cfg);
2434 if (rc) {
Matthew R. Ochsfa3f2c62015-10-21 15:15:45 -05002435 dev_err(&pdev->dev, "%s: call to alloc_mem failed!\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002436 __func__);
2437 rc = -ENOMEM;
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -05002438 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002439 goto out;
2440 }
2441
2442 cfg->init_state = INIT_STATE_NONE;
2443 cfg->dev = pdev;
Matthew R. Ochs17ead262015-10-21 15:15:37 -05002444 cfg->cxl_fops = cxlflash_cxl_fops;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002445
2446 /*
2447 * The promoted LUNs move to the top of the LUN table. The rest stay
2448 * on the bottom half. The bottom half grows from the end
2449 * (index = 255), whereas the top half grows from the beginning
2450 * (index = 0).
2451 */
2452 cfg->promote_lun_index = 0;
2453 cfg->last_lun_index[0] = CXLFLASH_NUM_VLUNS/2 - 1;
2454 cfg->last_lun_index[1] = CXLFLASH_NUM_VLUNS/2 - 1;
2455
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002456 cfg->dev_id = (struct pci_device_id *)dev_id;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002457
2458 init_waitqueue_head(&cfg->tmf_waitq);
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002459 init_waitqueue_head(&cfg->reset_waitq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002460
2461 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
2462 cfg->lr_state = LINK_RESET_INVALID;
2463 cfg->lr_port = -1;
Matthew R. Ochs0d731222015-10-21 15:16:24 -05002464 spin_lock_init(&cfg->tmf_slock);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002465 mutex_init(&cfg->ctx_tbl_list_mutex);
2466 mutex_init(&cfg->ctx_recovery_mutex);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002467 init_rwsem(&cfg->ioctl_rwsem);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002468 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
2469 INIT_LIST_HEAD(&cfg->lluns);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002470
2471 pci_set_drvdata(pdev, cfg);
2472
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002473 cfg->cxl_afu = cxl_pci_to_afu(pdev);
2474
2475 rc = init_pci(cfg);
2476 if (rc) {
2477 dev_err(&pdev->dev, "%s: call to init_pci "
2478 "failed rc=%d!\n", __func__, rc);
2479 goto out_remove;
2480 }
2481 cfg->init_state = INIT_STATE_PCI;
2482
2483 rc = init_afu(cfg);
2484 if (rc) {
2485 dev_err(&pdev->dev, "%s: call to init_afu "
2486 "failed rc=%d!\n", __func__, rc);
2487 goto out_remove;
2488 }
2489 cfg->init_state = INIT_STATE_AFU;
2490
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002491 rc = init_scsi(cfg);
2492 if (rc) {
2493 dev_err(&pdev->dev, "%s: call to init_scsi "
2494 "failed rc=%d!\n", __func__, rc);
2495 goto out_remove;
2496 }
2497 cfg->init_state = INIT_STATE_SCSI;
2498
2499out:
2500 pr_debug("%s: returning rc=%d\n", __func__, rc);
2501 return rc;
2502
2503out_remove:
2504 cxlflash_remove(pdev);
2505 goto out;
2506}
2507
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002508/**
2509 * cxlflash_pci_error_detected() - called when a PCI error is detected
2510 * @pdev: PCI device struct.
2511 * @state: PCI channel state.
2512 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002513 * When an EEH occurs during an active reset, wait until the reset is
2514 * complete and then take action based upon the device state.
2515 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002516 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2517 */
2518static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
2519 pci_channel_state_t state)
2520{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002521 int rc = 0;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002522 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2523 struct device *dev = &cfg->dev->dev;
2524
2525 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
2526
2527 switch (state) {
2528 case pci_channel_io_frozen:
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002529 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2530 if (cfg->state == STATE_FAILTERM)
2531 return PCI_ERS_RESULT_DISCONNECT;
2532
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002533 cfg->state = STATE_RESET;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002534 scsi_block_requests(cfg->host);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002535 drain_ioctls(cfg);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002536 rc = cxlflash_mark_contexts_error(cfg);
2537 if (unlikely(rc))
2538 dev_err(dev, "%s: Failed to mark user contexts!(%d)\n",
2539 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05002540 term_afu(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002541 return PCI_ERS_RESULT_NEED_RESET;
2542 case pci_channel_io_perm_failure:
2543 cfg->state = STATE_FAILTERM;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002544 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002545 scsi_unblock_requests(cfg->host);
2546 return PCI_ERS_RESULT_DISCONNECT;
2547 default:
2548 break;
2549 }
2550 return PCI_ERS_RESULT_NEED_RESET;
2551}
2552
2553/**
2554 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2555 * @pdev: PCI device struct.
2556 *
2557 * This routine is called by the pci error recovery code after the PCI
2558 * slot has been reset, just before we should resume normal operations.
2559 *
2560 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2561 */
2562static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
2563{
2564 int rc = 0;
2565 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2566 struct device *dev = &cfg->dev->dev;
2567
2568 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2569
2570 rc = init_afu(cfg);
2571 if (unlikely(rc)) {
2572 dev_err(dev, "%s: EEH recovery failed! (%d)\n", __func__, rc);
2573 return PCI_ERS_RESULT_DISCONNECT;
2574 }
2575
2576 return PCI_ERS_RESULT_RECOVERED;
2577}
2578
2579/**
2580 * cxlflash_pci_resume() - called when normal operation can resume
2581 * @pdev: PCI device struct
2582 */
2583static void cxlflash_pci_resume(struct pci_dev *pdev)
2584{
2585 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2586 struct device *dev = &cfg->dev->dev;
2587
2588 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2589
2590 cfg->state = STATE_NORMAL;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002591 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002592 scsi_unblock_requests(cfg->host);
2593}
2594
2595static const struct pci_error_handlers cxlflash_err_handler = {
2596 .error_detected = cxlflash_pci_error_detected,
2597 .slot_reset = cxlflash_pci_slot_reset,
2598 .resume = cxlflash_pci_resume,
2599};
2600
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002601/*
2602 * PCI device structure
2603 */
2604static struct pci_driver cxlflash_driver = {
2605 .name = CXLFLASH_NAME,
2606 .id_table = cxlflash_pci_table,
2607 .probe = cxlflash_probe,
2608 .remove = cxlflash_remove,
Uma Krishnanbabf9852016-09-02 15:39:16 -05002609 .shutdown = cxlflash_remove,
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002610 .err_handler = &cxlflash_err_handler,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002611};
2612
2613/**
2614 * init_cxlflash() - module entry point
2615 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002616 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002617 */
2618static int __init init_cxlflash(void)
2619{
Uma Krishnan85599212015-12-14 15:06:33 -06002620 pr_info("%s: %s\n", __func__, CXLFLASH_ADAPTER_NAME);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002621
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002622 cxlflash_list_init();
2623
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002624 return pci_register_driver(&cxlflash_driver);
2625}
2626
2627/**
2628 * exit_cxlflash() - module exit point
2629 */
2630static void __exit exit_cxlflash(void)
2631{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002632 cxlflash_term_global_luns();
2633 cxlflash_free_errpage();
2634
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002635 pci_unregister_driver(&cxlflash_driver);
2636}
2637
2638module_init(init_cxlflash);
2639module_exit(exit_cxlflash);