blob: 6d33d8c3a9fc241863931b9011a1f37175b1eba7 [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
44 * buffer and CDB within the command are initialized (zeroed) prior to
45 * returning.
46 *
47 * Return: The checked out command or NULL when command pool is empty.
48 */
Matthew R. Ochs15305512015-10-21 15:12:10 -050049static struct afu_cmd *cmd_checkout(struct afu *afu)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050050{
51 int k, dec = CXLFLASH_NUM_CMDS;
52 struct afu_cmd *cmd;
53
54 while (dec--) {
55 k = (afu->cmd_couts++ & (CXLFLASH_NUM_CMDS - 1));
56
57 cmd = &afu->cmd[k];
58
59 if (!atomic_dec_if_positive(&cmd->free)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -050060 pr_devel("%s: returning found index=%d cmd=%p\n",
61 __func__, cmd->slot, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050062 memset(cmd->buf, 0, CMD_BUFSIZE);
63 memset(cmd->rcb.cdb, 0, sizeof(cmd->rcb.cdb));
64 return cmd;
65 }
66 }
67
68 return NULL;
69}
70
71/**
Matthew R. Ochs15305512015-10-21 15:12:10 -050072 * cmd_checkin() - checks in an AFU command
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050073 * @cmd: AFU command to checkin.
74 *
75 * Safe to pass commands that have already been checked in. Several
76 * internal tracking fields are reset as part of the checkin. Note
77 * that these are intentionally reset prior to toggling the free bit
78 * to avoid clobbering values in the event that the command is checked
79 * out right away.
80 */
Matthew R. Ochs15305512015-10-21 15:12:10 -050081static void cmd_checkin(struct afu_cmd *cmd)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050082{
83 cmd->rcb.scp = NULL;
84 cmd->rcb.timeout = 0;
85 cmd->sa.ioasc = 0;
86 cmd->cmd_tmf = false;
87 cmd->sa.host_use[0] = 0; /* clears both completion and retry bytes */
88
89 if (unlikely(atomic_inc_return(&cmd->free) != 1)) {
90 pr_err("%s: Freeing cmd (%d) that is not in use!\n",
91 __func__, cmd->slot);
92 return;
93 }
94
Matthew R. Ochs4392ba42015-10-21 15:13:11 -050095 pr_devel("%s: released cmd %p index=%d\n", __func__, cmd, cmd->slot);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -050096}
97
98/**
99 * process_cmd_err() - command error handler
100 * @cmd: AFU command that experienced the error.
101 * @scp: SCSI command associated with the AFU command in error.
102 *
103 * Translates error bits from AFU command to SCSI command results.
104 */
105static void process_cmd_err(struct afu_cmd *cmd, struct scsi_cmnd *scp)
106{
107 struct sisl_ioarcb *ioarcb;
108 struct sisl_ioasa *ioasa;
Matthew R. Ochs83960122015-10-21 15:13:29 -0500109 u32 resid;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500110
111 if (unlikely(!cmd))
112 return;
113
114 ioarcb = &(cmd->rcb);
115 ioasa = &(cmd->sa);
116
117 if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) {
Matthew R. Ochs83960122015-10-21 15:13:29 -0500118 resid = ioasa->resid;
119 scsi_set_resid(scp, resid);
120 pr_debug("%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
121 __func__, cmd, scp, resid);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500122 }
123
124 if (ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN) {
125 pr_debug("%s: cmd underrun cmd = %p scp = %p\n",
126 __func__, cmd, scp);
127 scp->result = (DID_ERROR << 16);
128 }
129
130 pr_debug("%s: cmd failed afu_rc=%d scsi_rc=%d fc_rc=%d "
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500131 "afu_extra=0x%X, scsi_extra=0x%X, fc_extra=0x%X\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500132 __func__, ioasa->rc.afu_rc, ioasa->rc.scsi_rc,
133 ioasa->rc.fc_rc, ioasa->afu_extra, ioasa->scsi_extra,
134 ioasa->fc_extra);
135
136 if (ioasa->rc.scsi_rc) {
137 /* We have a SCSI status */
138 if (ioasa->rc.flags & SISL_RC_FLAGS_SENSE_VALID) {
139 memcpy(scp->sense_buffer, ioasa->sense_data,
140 SISL_SENSE_DATA_LEN);
141 scp->result = ioasa->rc.scsi_rc;
142 } else
143 scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16);
144 }
145
146 /*
147 * We encountered an error. Set scp->result based on nature
148 * of error.
149 */
150 if (ioasa->rc.fc_rc) {
151 /* We have an FC status */
152 switch (ioasa->rc.fc_rc) {
153 case SISL_FC_RC_LINKDOWN:
154 scp->result = (DID_REQUEUE << 16);
155 break;
156 case SISL_FC_RC_RESID:
157 /* This indicates an FCP resid underrun */
158 if (!(ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN)) {
159 /* If the SISL_RC_FLAGS_OVERRUN flag was set,
160 * then we will handle this error else where.
161 * If not then we must handle it here.
Matthew R. Ochs83960122015-10-21 15:13:29 -0500162 * This is probably an AFU bug.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500163 */
164 scp->result = (DID_ERROR << 16);
165 }
166 break;
167 case SISL_FC_RC_RESIDERR:
168 /* Resid mismatch between adapter and device */
169 case SISL_FC_RC_TGTABORT:
170 case SISL_FC_RC_ABORTOK:
171 case SISL_FC_RC_ABORTFAIL:
172 case SISL_FC_RC_NOLOGI:
173 case SISL_FC_RC_ABORTPEND:
174 case SISL_FC_RC_WRABORTPEND:
175 case SISL_FC_RC_NOEXP:
176 case SISL_FC_RC_INUSE:
177 scp->result = (DID_ERROR << 16);
178 break;
179 }
180 }
181
182 if (ioasa->rc.afu_rc) {
183 /* We have an AFU error */
184 switch (ioasa->rc.afu_rc) {
185 case SISL_AFU_RC_NO_CHANNELS:
Matthew R. Ochs83960122015-10-21 15:13:29 -0500186 scp->result = (DID_NO_CONNECT << 16);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500187 break;
188 case SISL_AFU_RC_DATA_DMA_ERR:
189 switch (ioasa->afu_extra) {
190 case SISL_AFU_DMA_ERR_PAGE_IN:
191 /* Retry */
192 scp->result = (DID_IMM_RETRY << 16);
193 break;
194 case SISL_AFU_DMA_ERR_INVALID_EA:
195 default:
196 scp->result = (DID_ERROR << 16);
197 }
198 break;
199 case SISL_AFU_RC_OUT_OF_DATA_BUFS:
200 /* Retry */
201 scp->result = (DID_ALLOC_FAILURE << 16);
202 break;
203 default:
204 scp->result = (DID_ERROR << 16);
205 }
206 }
207}
208
209/**
210 * cmd_complete() - command completion handler
211 * @cmd: AFU command that has completed.
212 *
213 * Prepares and submits command that has either completed or timed out to
214 * the SCSI stack. Checks AFU command back into command pool for non-internal
215 * (rcb.scp populated) commands.
216 */
217static void cmd_complete(struct afu_cmd *cmd)
218{
219 struct scsi_cmnd *scp;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500220 ulong lock_flags;
221 struct afu *afu = cmd->parent;
222 struct cxlflash_cfg *cfg = afu->parent;
223 bool cmd_is_tmf;
224
225 spin_lock_irqsave(&cmd->slock, lock_flags);
226 cmd->sa.host_use_b[0] |= B_DONE;
227 spin_unlock_irqrestore(&cmd->slock, lock_flags);
228
229 if (cmd->rcb.scp) {
230 scp = cmd->rcb.scp;
Matthew R. Ochs83960122015-10-21 15:13:29 -0500231 if (unlikely(cmd->sa.ioasc))
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500232 process_cmd_err(cmd, scp);
233 else
234 scp->result = (DID_OK << 16);
235
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500236 cmd_is_tmf = cmd->cmd_tmf;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500237 cmd_checkin(cmd); /* Don't use cmd after here */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500238
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500239 pr_debug_ratelimited("%s: calling scsi_done scp=%p result=%X "
240 "ioasc=%d\n", __func__, scp, scp->result,
241 cmd->sa.ioasc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500242
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500243 scsi_dma_unmap(scp);
244 scp->scsi_done(scp);
245
246 if (cmd_is_tmf) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500247 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500248 cfg->tmf_active = false;
249 wake_up_all_locked(&cfg->tmf_waitq);
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500250 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500251 }
252 } else
253 complete(&cmd->cevent);
254}
255
256/**
Matthew R. Ochs15305512015-10-21 15:12:10 -0500257 * context_reset() - timeout handler for AFU commands
258 * @cmd: AFU command that timed out.
259 *
260 * Sends a reset to the AFU.
261 */
262static void context_reset(struct afu_cmd *cmd)
263{
264 int nretry = 0;
265 u64 rrin = 0x1;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500266 struct afu *afu = cmd->parent;
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600267 struct cxlflash_cfg *cfg = afu->parent;
268 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs15305512015-10-21 15:12:10 -0500269 ulong lock_flags;
270
271 pr_debug("%s: cmd=%p\n", __func__, cmd);
272
273 spin_lock_irqsave(&cmd->slock, lock_flags);
274
275 /* Already completed? */
276 if (cmd->sa.host_use_b[0] & B_DONE) {
277 spin_unlock_irqrestore(&cmd->slock, lock_flags);
278 return;
279 }
280
281 cmd->sa.host_use_b[0] |= (B_DONE | B_ERROR | B_TIMEOUT);
282 spin_unlock_irqrestore(&cmd->slock, lock_flags);
283
Matthew R. Ochs15305512015-10-21 15:12:10 -0500284 writeq_be(rrin, &afu->host_map->ioarrin);
285 do {
286 rrin = readq_be(&afu->host_map->ioarrin);
287 if (rrin != 0x1)
288 break;
289 /* Double delay each time */
Manoj N. Kumarea765432016-03-25 14:26:49 -0500290 udelay(1 << nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500291 } while (nretry++ < MC_ROOM_RETRY_CNT);
Uma Krishnan3d2f6172016-11-28 18:41:36 -0600292
293 dev_dbg(dev, "%s: returning rrin=0x%016llX nretry=%d\n",
294 __func__, rrin, nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500295}
296
297/**
298 * send_cmd() - sends an AFU command
299 * @afu: AFU associated with the host.
300 * @cmd: AFU command to send.
301 *
302 * Return:
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500303 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochs15305512015-10-21 15:12:10 -0500304 */
305static int send_cmd(struct afu *afu, struct afu_cmd *cmd)
306{
307 struct cxlflash_cfg *cfg = afu->parent;
308 struct device *dev = &cfg->dev->dev;
309 int nretry = 0;
310 int rc = 0;
311 u64 room;
312 long newval;
313
314 /*
315 * This routine is used by critical users such an AFU sync and to
316 * send a task management function (TMF). Thus we want to retry a
317 * bit before returning an error. To avoid the performance penalty
318 * of MMIO, we spread the update of 'room' over multiple commands.
319 */
320retry:
321 newval = atomic64_dec_if_positive(&afu->room);
322 if (!newval) {
323 do {
324 room = readq_be(&afu->host_map->cmd_room);
325 atomic64_set(&afu->room, room);
326 if (room)
327 goto write_ioarrin;
Manoj N. Kumarea765432016-03-25 14:26:49 -0500328 udelay(1 << nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500329 } while (nretry++ < MC_ROOM_RETRY_CNT);
330
331 dev_err(dev, "%s: no cmd_room to send 0x%X\n",
332 __func__, cmd->rcb.cdb[0]);
333
334 goto no_room;
335 } else if (unlikely(newval < 0)) {
336 /* This should be rare. i.e. Only if two threads race and
337 * decrement before the MMIO read is done. In this case
338 * just benefit from the other thread having updated
339 * afu->room.
340 */
341 if (nretry++ < MC_ROOM_RETRY_CNT) {
Manoj N. Kumarea765432016-03-25 14:26:49 -0500342 udelay(1 << nretry);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500343 goto retry;
344 }
345
346 goto no_room;
347 }
348
349write_ioarrin:
350 writeq_be((u64)&cmd->rcb, &afu->host_map->ioarrin);
351out:
352 pr_devel("%s: cmd=%p len=%d ea=%p rc=%d\n", __func__, cmd,
353 cmd->rcb.data_len, (void *)cmd->rcb.data_ea, rc);
354 return rc;
355
356no_room:
357 afu->read_room = true;
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600358 kref_get(&cfg->afu->mapcount);
Matthew R. Ochs15305512015-10-21 15:12:10 -0500359 schedule_work(&cfg->work_q);
360 rc = SCSI_MLQUEUE_HOST_BUSY;
361 goto out;
362}
363
364/**
365 * wait_resp() - polls for a response or timeout to a sent AFU command
366 * @afu: AFU associated with the host.
367 * @cmd: AFU command that was sent.
368 */
369static void wait_resp(struct afu *afu, struct afu_cmd *cmd)
370{
371 ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
372
373 timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
374 if (!timeout)
375 context_reset(cmd);
376
377 if (unlikely(cmd->sa.ioasc != 0))
378 pr_err("%s: CMD 0x%X failed, IOASC: flags 0x%X, afu_rc 0x%X, "
379 "scsi_rc 0x%X, fc_rc 0x%X\n", __func__, cmd->rcb.cdb[0],
380 cmd->sa.rc.flags, cmd->sa.rc.afu_rc, cmd->sa.rc.scsi_rc,
381 cmd->sa.rc.fc_rc);
382}
383
384/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500385 * send_tmf() - sends a Task Management Function (TMF)
386 * @afu: AFU to checkout from.
387 * @scp: SCSI command from stack.
388 * @tmfcmd: TMF command to send.
389 *
390 * Return:
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500391 * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500392 */
393static int send_tmf(struct afu *afu, struct scsi_cmnd *scp, u64 tmfcmd)
394{
395 struct afu_cmd *cmd;
396
397 u32 port_sel = scp->device->channel + 1;
398 short lflag = 0;
399 struct Scsi_Host *host = scp->device->host;
400 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500401 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500402 ulong lock_flags;
403 int rc = 0;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500404 ulong to;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500405
Matthew R. Ochs15305512015-10-21 15:12:10 -0500406 cmd = cmd_checkout(afu);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500407 if (unlikely(!cmd)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500408 dev_err(dev, "%s: could not get a free command\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500409 rc = SCSI_MLQUEUE_HOST_BUSY;
410 goto out;
411 }
412
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500413 /* When Task Management Function is active do not send another */
414 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500415 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500416 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
417 !cfg->tmf_active,
418 cfg->tmf_slock);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500419 cfg->tmf_active = true;
420 cmd->cmd_tmf = true;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500421 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500422
423 cmd->rcb.ctx_id = afu->ctx_hndl;
424 cmd->rcb.port_sel = port_sel;
425 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
426
427 lflag = SISL_REQ_FLAGS_TMF_CMD;
428
429 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
430 SISL_REQ_FLAGS_SUP_UNDERRUN | lflag);
431
432 /* Stash the scp in the reserved field, for reuse during interrupt */
433 cmd->rcb.scp = scp;
434
435 /* Copy the CDB from the cmd passed in */
436 memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
437
438 /* Send the command */
Matthew R. Ochs15305512015-10-21 15:12:10 -0500439 rc = send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500440 if (unlikely(rc)) {
Matthew R. Ochs15305512015-10-21 15:12:10 -0500441 cmd_checkin(cmd);
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500442 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500443 cfg->tmf_active = false;
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500444 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500445 goto out;
446 }
447
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500448 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
449 to = msecs_to_jiffies(5000);
450 to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
451 !cfg->tmf_active,
452 cfg->tmf_slock,
453 to);
454 if (!to) {
455 cfg->tmf_active = false;
456 dev_err(dev, "%s: TMF timed out!\n", __func__);
457 rc = -1;
458 }
459 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500460out:
461 return rc;
462}
463
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600464static void afu_unmap(struct kref *ref)
465{
466 struct afu *afu = container_of(ref, struct afu, mapcount);
467
468 if (likely(afu->afu_map)) {
469 cxl_psa_unmap((void __iomem *)afu->afu_map);
470 afu->afu_map = NULL;
471 }
472}
473
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500474/**
475 * cxlflash_driver_info() - information handler for this host driver
476 * @host: SCSI host associated with device.
477 *
478 * Return: A string describing the device.
479 */
480static const char *cxlflash_driver_info(struct Scsi_Host *host)
481{
482 return CXLFLASH_ADAPTER_NAME;
483}
484
485/**
486 * cxlflash_queuecommand() - sends a mid-layer request
487 * @host: SCSI host associated with device.
488 * @scp: SCSI command to send.
489 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500490 * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500491 */
492static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
493{
494 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
495 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500496 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500497 struct afu_cmd *cmd;
498 u32 port_sel = scp->device->channel + 1;
499 int nseg, i, ncount;
500 struct scatterlist *sg;
501 ulong lock_flags;
502 short lflag = 0;
503 int rc = 0;
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600504 int kref_got = 0;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500505
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500506 dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
507 "cdb=(%08X-%08X-%08X-%08X)\n",
508 __func__, scp, host->host_no, scp->device->channel,
509 scp->device->id, scp->device->lun,
510 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
511 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
512 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
513 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500514
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500515 /*
516 * If a Task Management Function is active, wait for it to complete
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500517 * before continuing with regular commands.
518 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500519 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500520 if (cfg->tmf_active) {
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500521 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500522 rc = SCSI_MLQUEUE_HOST_BUSY;
523 goto out;
524 }
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500525 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500526
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500527 switch (cfg->state) {
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500528 case STATE_RESET:
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500529 dev_dbg_ratelimited(dev, "%s: device is in reset!\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500530 rc = SCSI_MLQUEUE_HOST_BUSY;
531 goto out;
532 case STATE_FAILTERM:
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500533 dev_dbg_ratelimited(dev, "%s: device has failed!\n", __func__);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500534 scp->result = (DID_NO_CONNECT << 16);
535 scp->scsi_done(scp);
536 rc = 0;
537 goto out;
538 default:
539 break;
540 }
541
Matthew R. Ochs15305512015-10-21 15:12:10 -0500542 cmd = cmd_checkout(afu);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500543 if (unlikely(!cmd)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500544 dev_err(dev, "%s: could not get a free command\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500545 rc = SCSI_MLQUEUE_HOST_BUSY;
546 goto out;
547 }
548
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600549 kref_get(&cfg->afu->mapcount);
550 kref_got = 1;
551
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500552 cmd->rcb.ctx_id = afu->ctx_hndl;
553 cmd->rcb.port_sel = port_sel;
554 cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
555
556 if (scp->sc_data_direction == DMA_TO_DEVICE)
557 lflag = SISL_REQ_FLAGS_HOST_WRITE;
558 else
559 lflag = SISL_REQ_FLAGS_HOST_READ;
560
561 cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
562 SISL_REQ_FLAGS_SUP_UNDERRUN | lflag);
563
564 /* Stash the scp in the reserved field, for reuse during interrupt */
565 cmd->rcb.scp = scp;
566
567 nseg = scsi_dma_map(scp);
568 if (unlikely(nseg < 0)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500569 dev_err(dev, "%s: Fail DMA map! nseg=%d\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500570 __func__, nseg);
571 rc = SCSI_MLQUEUE_HOST_BUSY;
572 goto out;
573 }
574
575 ncount = scsi_sg_count(scp);
576 scsi_for_each_sg(scp, sg, ncount, i) {
577 cmd->rcb.data_len = sg_dma_len(sg);
578 cmd->rcb.data_ea = sg_dma_address(sg);
579 }
580
581 /* Copy the CDB from the scsi_cmnd passed in */
582 memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
583
584 /* Send the command */
Matthew R. Ochs15305512015-10-21 15:12:10 -0500585 rc = send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500586 if (unlikely(rc)) {
Matthew R. Ochs15305512015-10-21 15:12:10 -0500587 cmd_checkin(cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500588 scsi_dma_unmap(scp);
589 }
590
591out:
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600592 if (kref_got)
593 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500594 pr_devel("%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500595 return rc;
596}
597
598/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500599 * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500600 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500601 */
602static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
603{
604 struct pci_dev *pdev = cfg->dev;
605
606 if (pci_channel_offline(pdev))
Matthew R. Ochs439e85c2015-10-21 15:12:00 -0500607 wait_event_timeout(cfg->reset_waitq,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500608 !pci_channel_offline(pdev),
609 CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
610}
611
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500612/**
613 * free_mem() - free memory associated with the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500614 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500615 */
616static void free_mem(struct cxlflash_cfg *cfg)
617{
618 int i;
619 char *buf = NULL;
620 struct afu *afu = cfg->afu;
621
622 if (cfg->afu) {
623 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
624 buf = afu->cmd[i].buf;
625 if (!((u64)buf & (PAGE_SIZE - 1)))
626 free_page((ulong)buf);
627 }
628
629 free_pages((ulong)afu, get_order(sizeof(struct afu)));
630 cfg->afu = NULL;
631 }
632}
633
634/**
635 * stop_afu() - stops the AFU command timers and unmaps the MMIO space
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500636 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500637 *
638 * Safe to call with AFU in a partially allocated/initialized state.
Manoj Kumaree91e332015-12-14 15:07:02 -0600639 *
640 * Cleans up all state associated with the command queue, and unmaps
641 * the MMIO space.
642 *
643 * - complete() will take care of commands we initiated (they'll be checked
644 * in as part of the cleanup that occurs after the completion)
645 *
646 * - cmd_checkin() will take care of entries that we did not initiate and that
647 * have not (and will not) complete because they are sitting on a [now stale]
648 * hardware queue
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500649 */
650static void stop_afu(struct cxlflash_cfg *cfg)
651{
652 int i;
653 struct afu *afu = cfg->afu;
Manoj Kumaree91e332015-12-14 15:07:02 -0600654 struct afu_cmd *cmd;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500655
656 if (likely(afu)) {
Manoj Kumaree91e332015-12-14 15:07:02 -0600657 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
658 cmd = &afu->cmd[i];
659 complete(&cmd->cevent);
660 if (!atomic_read(&cmd->free))
661 cmd_checkin(cmd);
662 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500663
664 if (likely(afu->afu_map)) {
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500665 cxl_psa_unmap((void __iomem *)afu->afu_map);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500666 afu->afu_map = NULL;
667 }
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600668 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500669 }
670}
671
672/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500673 * term_intr() - disables all AFU interrupts
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500674 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500675 * @level: Depth of allocation, where to begin waterfall tear down.
676 *
677 * Safe to call with AFU/MC in partially allocated/initialized state.
678 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500679static void term_intr(struct cxlflash_cfg *cfg, enum undo_level level)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500680{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500681 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500682 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500683
684 if (!afu || !cfg->mcctx) {
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500685 dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500686 return;
687 }
688
689 switch (level) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500690 case UNMAP_THREE:
691 cxl_unmap_afu_irq(cfg->mcctx, 3, afu);
692 case UNMAP_TWO:
693 cxl_unmap_afu_irq(cfg->mcctx, 2, afu);
694 case UNMAP_ONE:
695 cxl_unmap_afu_irq(cfg->mcctx, 1, afu);
696 case FREE_IRQ:
697 cxl_free_afu_irqs(cfg->mcctx);
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500698 /* fall through */
699 case UNDO_NOOP:
700 /* No action required */
701 break;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500702 }
703}
704
705/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500706 * term_mc() - terminates the master context
707 * @cfg: Internal structure associated with the host.
708 * @level: Depth of allocation, where to begin waterfall tear down.
709 *
710 * Safe to call with AFU/MC in partially allocated/initialized state.
711 */
712static void term_mc(struct cxlflash_cfg *cfg)
713{
714 int rc = 0;
715 struct afu *afu = cfg->afu;
716 struct device *dev = &cfg->dev->dev;
717
718 if (!afu || !cfg->mcctx) {
719 dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
720 return;
721 }
722
723 rc = cxl_stop_context(cfg->mcctx);
724 WARN_ON(rc);
725 cfg->mcctx = NULL;
726}
727
728/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500729 * term_afu() - terminates the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500730 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500731 *
732 * Safe to call with AFU/MC in partially allocated/initialized state.
733 */
734static void term_afu(struct cxlflash_cfg *cfg)
735{
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500736 /*
737 * Tear down is carefully orchestrated to ensure
738 * no interrupts can come in when the problem state
739 * area is unmapped.
740 *
741 * 1) Disable all AFU interrupts
742 * 2) Unmap the problem state area
743 * 3) Stop the master context
744 */
745 term_intr(cfg, UNMAP_THREE);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500746 if (cfg->afu)
747 stop_afu(cfg);
748
Manoj N. Kumar9526f362016-03-25 14:26:34 -0500749 term_mc(cfg);
Uma Krishnan6ded8b32016-03-04 15:55:15 -0600750
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500751 pr_debug("%s: returning\n", __func__);
752}
753
754/**
Uma Krishnan704c4b02016-06-15 18:49:57 -0500755 * notify_shutdown() - notifies device of pending shutdown
756 * @cfg: Internal structure associated with the host.
757 * @wait: Whether to wait for shutdown processing to complete.
758 *
759 * This function will notify the AFU that the adapter is being shutdown
760 * and will wait for shutdown processing to complete if wait is true.
761 * This notification should flush pending I/Os to the device and halt
762 * further I/Os until the next AFU reset is issued and device restarted.
763 */
764static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
765{
766 struct afu *afu = cfg->afu;
767 struct device *dev = &cfg->dev->dev;
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500768 struct sisl_global_map __iomem *global;
Uma Krishnan704c4b02016-06-15 18:49:57 -0500769 struct dev_dependent_vals *ddv;
770 u64 reg, status;
771 int i, retry_cnt = 0;
772
773 ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
774 if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
775 return;
776
Uma Krishnan1bd2b282016-07-21 15:44:04 -0500777 if (!afu || !afu->afu_map) {
778 dev_dbg(dev, "%s: The problem state area is not mapped\n",
779 __func__);
780 return;
781 }
782
783 global = &afu->afu_map->global;
784
Uma Krishnan704c4b02016-06-15 18:49:57 -0500785 /* Notify AFU */
786 for (i = 0; i < NUM_FC_PORTS; i++) {
787 reg = readq_be(&global->fc_regs[i][FC_CONFIG2 / 8]);
788 reg |= SISL_FC_SHUTDOWN_NORMAL;
789 writeq_be(reg, &global->fc_regs[i][FC_CONFIG2 / 8]);
790 }
791
792 if (!wait)
793 return;
794
795 /* Wait up to 1.5 seconds for shutdown processing to complete */
796 for (i = 0; i < NUM_FC_PORTS; i++) {
797 retry_cnt = 0;
798 while (true) {
799 status = readq_be(&global->fc_regs[i][FC_STATUS / 8]);
800 if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
801 break;
802 if (++retry_cnt >= MC_RETRY_CNT) {
803 dev_dbg(dev, "%s: port %d shutdown processing "
804 "not yet completed\n", __func__, i);
805 break;
806 }
807 msleep(100 * retry_cnt);
808 }
809 }
810}
811
812/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500813 * cxlflash_remove() - PCI entry point to tear down host
814 * @pdev: PCI device associated with the host.
815 *
816 * Safe to use as a cleanup in partially allocated/initialized state.
817 */
818static void cxlflash_remove(struct pci_dev *pdev)
819{
820 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
821 ulong lock_flags;
822
Uma Krishnanbabf9852016-09-02 15:39:16 -0500823 if (!pci_is_enabled(pdev)) {
824 pr_debug("%s: Device is disabled\n", __func__);
825 return;
826 }
827
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500828 /* If a Task Management Function is active, wait for it to complete
829 * before continuing with remove.
830 */
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500831 spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500832 if (cfg->tmf_active)
Matthew R. Ochs018d1dc952015-10-21 15:13:21 -0500833 wait_event_interruptible_lock_irq(cfg->tmf_waitq,
834 !cfg->tmf_active,
835 cfg->tmf_slock);
836 spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500837
Uma Krishnan704c4b02016-06-15 18:49:57 -0500838 /* Notify AFU and wait for shutdown processing to complete */
839 notify_shutdown(cfg, true);
840
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500841 cfg->state = STATE_FAILTERM;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500842 cxlflash_stop_term_user_contexts(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -0500843
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500844 switch (cfg->init_state) {
845 case INIT_STATE_SCSI:
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500846 cxlflash_term_local_luns(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500847 scsi_remove_host(cfg->host);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -0500848 /* fall through */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500849 case INIT_STATE_AFU:
Matthew R. Ochsd8046212015-10-21 15:14:17 -0500850 cancel_work_sync(&cfg->work_q);
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -0600851 term_afu(cfg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500852 case INIT_STATE_PCI:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500853 pci_disable_device(pdev);
854 case INIT_STATE_NONE:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500855 free_mem(cfg);
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -0500856 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500857 break;
858 }
859
860 pr_debug("%s: returning\n", __func__);
861}
862
863/**
864 * alloc_mem() - allocates the AFU and its command pool
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500865 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500866 *
867 * A partially allocated state remains on failure.
868 *
869 * Return:
870 * 0 on success
871 * -ENOMEM on failure to allocate memory
872 */
873static int alloc_mem(struct cxlflash_cfg *cfg)
874{
875 int rc = 0;
876 int i;
877 char *buf = NULL;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500878 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500879
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -0500880 /* AFU is ~12k, i.e. only one 64k page or up to four 4k pages */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500881 cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
882 get_order(sizeof(struct afu)));
883 if (unlikely(!cfg->afu)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500884 dev_err(dev, "%s: cannot get %d free pages\n",
885 __func__, get_order(sizeof(struct afu)));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500886 rc = -ENOMEM;
887 goto out;
888 }
889 cfg->afu->parent = cfg;
890 cfg->afu->afu_map = NULL;
891
892 for (i = 0; i < CXLFLASH_NUM_CMDS; buf += CMD_BUFSIZE, i++) {
893 if (!((u64)buf & (PAGE_SIZE - 1))) {
894 buf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
895 if (unlikely(!buf)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -0500896 dev_err(dev,
897 "%s: Allocate command buffers fail!\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500898 __func__);
899 rc = -ENOMEM;
900 free_mem(cfg);
901 goto out;
902 }
903 }
904
905 cfg->afu->cmd[i].buf = buf;
906 atomic_set(&cfg->afu->cmd[i].free, 1);
907 cfg->afu->cmd[i].slot = i;
908 }
909
910out:
911 return rc;
912}
913
914/**
915 * init_pci() - initializes the host as a PCI device
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500916 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500917 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500918 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500919 */
920static int init_pci(struct cxlflash_cfg *cfg)
921{
922 struct pci_dev *pdev = cfg->dev;
923 int rc = 0;
924
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500925 rc = pci_enable_device(pdev);
926 if (rc || pci_channel_offline(pdev)) {
927 if (pci_channel_offline(pdev)) {
928 cxlflash_wait_for_pci_err_recovery(cfg);
929 rc = pci_enable_device(pdev);
930 }
931
932 if (rc) {
933 dev_err(&pdev->dev, "%s: Cannot enable adapter\n",
934 __func__);
935 cxlflash_wait_for_pci_err_recovery(cfg);
Manoj N. Kumar961487e2016-03-04 15:55:14 -0600936 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500937 }
938 }
939
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500940out:
941 pr_debug("%s: returning rc=%d\n", __func__, rc);
942 return rc;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500943}
944
945/**
946 * init_scsi() - adds the host to the SCSI stack and kicks off host scan
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500947 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500948 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -0500949 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500950 */
951static int init_scsi(struct cxlflash_cfg *cfg)
952{
953 struct pci_dev *pdev = cfg->dev;
954 int rc = 0;
955
956 rc = scsi_add_host(cfg->host, &pdev->dev);
957 if (rc) {
958 dev_err(&pdev->dev, "%s: scsi_add_host failed (rc=%d)\n",
959 __func__, rc);
960 goto out;
961 }
962
963 scsi_scan_host(cfg->host);
964
965out:
966 pr_debug("%s: returning rc=%d\n", __func__, rc);
967 return rc;
968}
969
970/**
971 * set_port_online() - transitions the specified host FC port to online state
972 * @fc_regs: Top of MMIO region defined for specified port.
973 *
974 * The provided MMIO region must be mapped prior to call. Online state means
975 * that the FC link layer has synced, completed the handshaking process, and
976 * is ready for login to start.
977 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500978static void set_port_online(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500979{
980 u64 cmdcfg;
981
982 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
983 cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
984 cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
985 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
986}
987
988/**
989 * set_port_offline() - transitions the specified host FC port to offline state
990 * @fc_regs: Top of MMIO region defined for specified port.
991 *
992 * The provided MMIO region must be mapped prior to call.
993 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -0500994static void set_port_offline(__be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -0500995{
996 u64 cmdcfg;
997
998 cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
999 cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
1000 cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
1001 writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
1002}
1003
1004/**
1005 * wait_port_online() - waits for the specified host FC port come online
1006 * @fc_regs: Top of MMIO region defined for specified port.
1007 * @delay_us: Number of microseconds to delay between reading port status.
1008 * @nretry: Number of cycles to retry reading port status.
1009 *
1010 * The provided MMIO region must be mapped prior to call. This will timeout
1011 * when the cable is not plugged in.
1012 *
1013 * Return:
1014 * TRUE (1) when the specified port is online
1015 * FALSE (0) when the specified port fails to come online after timeout
1016 * -EINVAL when @delay_us is less than 1000
1017 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001018static int wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001019{
1020 u64 status;
1021
1022 if (delay_us < 1000) {
1023 pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
1024 return -EINVAL;
1025 }
1026
1027 do {
1028 msleep(delay_us / 1000);
1029 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -05001030 if (status == U64_MAX)
1031 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001032 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
1033 nretry--);
1034
1035 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
1036}
1037
1038/**
1039 * wait_port_offline() - waits for the specified host FC port go offline
1040 * @fc_regs: Top of MMIO region defined for specified port.
1041 * @delay_us: Number of microseconds to delay between reading port status.
1042 * @nretry: Number of cycles to retry reading port status.
1043 *
1044 * The provided MMIO region must be mapped prior to call.
1045 *
1046 * Return:
1047 * TRUE (1) when the specified port is offline
1048 * FALSE (0) when the specified port fails to go offline after timeout
1049 * -EINVAL when @delay_us is less than 1000
1050 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001051static int wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001052{
1053 u64 status;
1054
1055 if (delay_us < 1000) {
1056 pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
1057 return -EINVAL;
1058 }
1059
1060 do {
1061 msleep(delay_us / 1000);
1062 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
Matthew R. Ochs05dab432016-09-02 15:40:03 -05001063 if (status == U64_MAX)
1064 nretry /= 2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001065 } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
1066 nretry--);
1067
1068 return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
1069}
1070
1071/**
1072 * afu_set_wwpn() - configures the WWPN for the specified host FC port
1073 * @afu: AFU associated with the host that owns the specified FC port.
1074 * @port: Port number being configured.
1075 * @fc_regs: Top of MMIO region defined for specified port.
1076 * @wwpn: The world-wide-port-number previously discovered for port.
1077 *
1078 * The provided MMIO region must be mapped prior to call. As part of the
1079 * sequence to configure the WWPN, the port is toggled offline and then back
1080 * online. This toggling action can cause this routine to delay up to a few
1081 * seconds. When configured to use the internal LUN feature of the AFU, a
1082 * failure to come online is overridden.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001083 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001084static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
1085 u64 wwpn)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001086{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001087 set_port_offline(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001088 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1089 FC_PORT_STATUS_RETRY_CNT)) {
1090 pr_debug("%s: wait on port %d to go offline timed out\n",
1091 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001092 }
1093
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001094 writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
Matthew R. Ochs964497b2015-10-21 15:13:54 -05001095
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001096 set_port_online(fc_regs);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001097 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1098 FC_PORT_STATUS_RETRY_CNT)) {
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001099 pr_debug("%s: wait on port %d to go online timed out\n",
1100 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001101 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001102}
1103
1104/**
1105 * afu_link_reset() - resets the specified host FC port
1106 * @afu: AFU associated with the host that owns the specified FC port.
1107 * @port: Port number being configured.
1108 * @fc_regs: Top of MMIO region defined for specified port.
1109 *
1110 * The provided MMIO region must be mapped prior to call. The sequence to
1111 * reset the port involves toggling it offline and then back online. This
1112 * action can cause this routine to delay up to a few seconds. An effort
1113 * is made to maintain link with the device by switching to host to use
1114 * the alternate port exclusively while the reset takes place.
1115 * failure to come online is overridden.
1116 */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001117static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001118{
1119 u64 port_sel;
1120
1121 /* first switch the AFU to the other links, if any */
1122 port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
Dan Carpenter4da74db2015-08-18 11:57:43 +03001123 port_sel &= ~(1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001124 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1125 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1126
1127 set_port_offline(fc_regs);
1128 if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1129 FC_PORT_STATUS_RETRY_CNT))
1130 pr_err("%s: wait on port %d to go offline timed out\n",
1131 __func__, port);
1132
1133 set_port_online(fc_regs);
1134 if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1135 FC_PORT_STATUS_RETRY_CNT))
1136 pr_err("%s: wait on port %d to go online timed out\n",
1137 __func__, port);
1138
1139 /* switch back to include this port */
Dan Carpenter4da74db2015-08-18 11:57:43 +03001140 port_sel |= (1ULL << port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001141 writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1142 cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1143
1144 pr_debug("%s: returning port_sel=%lld\n", __func__, port_sel);
1145}
1146
1147/*
1148 * Asynchronous interrupt information table
1149 */
1150static const struct asyc_intr_info ainfo[] = {
1151 {SISL_ASTATUS_FC0_OTHER, "other error", 0, CLR_FC_ERROR | LINK_RESET},
1152 {SISL_ASTATUS_FC0_LOGO, "target initiated LOGO", 0, 0},
1153 {SISL_ASTATUS_FC0_CRC_T, "CRC threshold exceeded", 0, LINK_RESET},
Manoj Kumare6e6df32015-10-21 15:16:07 -05001154 {SISL_ASTATUS_FC0_LOGI_R, "login timed out, retrying", 0, LINK_RESET},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001155 {SISL_ASTATUS_FC0_LOGI_F, "login failed", 0, CLR_FC_ERROR},
Matthew R. Ochsef510742015-10-21 15:13:37 -05001156 {SISL_ASTATUS_FC0_LOGI_S, "login succeeded", 0, SCAN_HOST},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001157 {SISL_ASTATUS_FC0_LINK_DN, "link down", 0, 0},
Uma Krishnanbbbfae92016-09-02 15:38:48 -05001158 {SISL_ASTATUS_FC0_LINK_UP, "link up", 0, 0},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001159 {SISL_ASTATUS_FC1_OTHER, "other error", 1, CLR_FC_ERROR | LINK_RESET},
1160 {SISL_ASTATUS_FC1_LOGO, "target initiated LOGO", 1, 0},
1161 {SISL_ASTATUS_FC1_CRC_T, "CRC threshold exceeded", 1, LINK_RESET},
Manoj Kumara9be2942015-12-14 14:55:09 -06001162 {SISL_ASTATUS_FC1_LOGI_R, "login timed out, retrying", 1, LINK_RESET},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001163 {SISL_ASTATUS_FC1_LOGI_F, "login failed", 1, CLR_FC_ERROR},
Matthew R. Ochsef510742015-10-21 15:13:37 -05001164 {SISL_ASTATUS_FC1_LOGI_S, "login succeeded", 1, SCAN_HOST},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001165 {SISL_ASTATUS_FC1_LINK_DN, "link down", 1, 0},
Uma Krishnanbbbfae92016-09-02 15:38:48 -05001166 {SISL_ASTATUS_FC1_LINK_UP, "link up", 1, 0},
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001167 {0x0, "", 0, 0} /* terminator */
1168};
1169
1170/**
1171 * find_ainfo() - locates and returns asynchronous interrupt information
1172 * @status: Status code set by AFU on error.
1173 *
1174 * Return: The located information or NULL when the status code is invalid.
1175 */
1176static const struct asyc_intr_info *find_ainfo(u64 status)
1177{
1178 const struct asyc_intr_info *info;
1179
1180 for (info = &ainfo[0]; info->status; info++)
1181 if (info->status == status)
1182 return info;
1183
1184 return NULL;
1185}
1186
1187/**
1188 * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1189 * @afu: AFU associated with the host.
1190 */
1191static void afu_err_intr_init(struct afu *afu)
1192{
1193 int i;
1194 u64 reg;
1195
1196 /* global async interrupts: AFU clears afu_ctrl on context exit
1197 * if async interrupts were sent to that context. This prevents
1198 * the AFU form sending further async interrupts when
1199 * there is
1200 * nobody to receive them.
1201 */
1202
1203 /* mask all */
1204 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
1205 /* set LISN# to send and point to master context */
1206 reg = ((u64) (((afu->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
1207
1208 if (afu->internal_lun)
1209 reg |= 1; /* Bit 63 indicates local lun */
1210 writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1211 /* clear all */
1212 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1213 /* unmask bits that are of interest */
1214 /* note: afu can send an interrupt after this step */
1215 writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1216 /* clear again in case a bit came on after previous clear but before */
1217 /* unmask */
1218 writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1219
1220 /* Clear/Set internal lun bits */
1221 reg = readq_be(&afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1222 reg &= SISL_FC_INTERNAL_MASK;
1223 if (afu->internal_lun)
1224 reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
1225 writeq_be(reg, &afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
1226
1227 /* now clear FC errors */
1228 for (i = 0; i < NUM_FC_PORTS; i++) {
1229 writeq_be(0xFFFFFFFFU,
1230 &afu->afu_map->global.fc_regs[i][FC_ERROR / 8]);
1231 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRCAP / 8]);
1232 }
1233
1234 /* sync interrupts for master's IOARRIN write */
1235 /* note that unlike asyncs, there can be no pending sync interrupts */
1236 /* at this time (this is a fresh context and master has not written */
1237 /* IOARRIN yet), so there is nothing to clear. */
1238
1239 /* set LISN#, it is always sent to the context that wrote IOARRIN */
1240 writeq_be(SISL_MSI_SYNC_ERROR, &afu->host_map->ctx_ctrl);
1241 writeq_be(SISL_ISTATUS_MASK, &afu->host_map->intr_mask);
1242}
1243
1244/**
1245 * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1246 * @irq: Interrupt number.
1247 * @data: Private data provided at interrupt registration, the AFU.
1248 *
1249 * Return: Always return IRQ_HANDLED.
1250 */
1251static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1252{
1253 struct afu *afu = (struct afu *)data;
1254 u64 reg;
1255 u64 reg_unmasked;
1256
1257 reg = readq_be(&afu->host_map->intr_status);
1258 reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1259
1260 if (reg_unmasked == 0UL) {
1261 pr_err("%s: %llX: spurious interrupt, intr_status %016llX\n",
1262 __func__, (u64)afu, reg);
1263 goto cxlflash_sync_err_irq_exit;
1264 }
1265
1266 pr_err("%s: %llX: unexpected interrupt, intr_status %016llX\n",
1267 __func__, (u64)afu, reg);
1268
1269 writeq_be(reg_unmasked, &afu->host_map->intr_clear);
1270
1271cxlflash_sync_err_irq_exit:
1272 pr_debug("%s: returning rc=%d\n", __func__, IRQ_HANDLED);
1273 return IRQ_HANDLED;
1274}
1275
1276/**
1277 * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
1278 * @irq: Interrupt number.
1279 * @data: Private data provided at interrupt registration, the AFU.
1280 *
1281 * Return: Always return IRQ_HANDLED.
1282 */
1283static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
1284{
1285 struct afu *afu = (struct afu *)data;
1286 struct afu_cmd *cmd;
1287 bool toggle = afu->toggle;
1288 u64 entry,
1289 *hrrq_start = afu->hrrq_start,
1290 *hrrq_end = afu->hrrq_end,
1291 *hrrq_curr = afu->hrrq_curr;
1292
1293 /* Process however many RRQ entries that are ready */
1294 while (true) {
1295 entry = *hrrq_curr;
1296
1297 if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1298 break;
1299
1300 cmd = (struct afu_cmd *)(entry & ~SISL_RESP_HANDLE_T_BIT);
1301 cmd_complete(cmd);
1302
1303 /* Advance to next entry or wrap and flip the toggle bit */
1304 if (hrrq_curr < hrrq_end)
1305 hrrq_curr++;
1306 else {
1307 hrrq_curr = hrrq_start;
1308 toggle ^= SISL_RESP_HANDLE_T_BIT;
1309 }
1310 }
1311
1312 afu->hrrq_curr = hrrq_curr;
1313 afu->toggle = toggle;
1314
1315 return IRQ_HANDLED;
1316}
1317
1318/**
1319 * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1320 * @irq: Interrupt number.
1321 * @data: Private data provided at interrupt registration, the AFU.
1322 *
1323 * Return: Always return IRQ_HANDLED.
1324 */
1325static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1326{
1327 struct afu *afu = (struct afu *)data;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001328 struct cxlflash_cfg *cfg = afu->parent;
1329 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001330 u64 reg_unmasked;
1331 const struct asyc_intr_info *info;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001332 struct sisl_global_map __iomem *global = &afu->afu_map->global;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001333 u64 reg;
1334 u8 port;
1335 int i;
1336
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001337 reg = readq_be(&global->regs.aintr_status);
1338 reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1339
1340 if (reg_unmasked == 0) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001341 dev_err(dev, "%s: spurious interrupt, aintr_status 0x%016llX\n",
1342 __func__, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001343 goto out;
1344 }
1345
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001346 /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001347 writeq_be(reg_unmasked, &global->regs.aintr_clear);
1348
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001349 /* Check each bit that is on */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001350 for (i = 0; reg_unmasked; i++, reg_unmasked = (reg_unmasked >> 1)) {
1351 info = find_ainfo(1ULL << i);
Matthew R. Ochs16798d32015-10-21 15:13:45 -05001352 if (((reg_unmasked & 0x1) == 0) || !info)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001353 continue;
1354
1355 port = info->port;
1356
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001357 dev_err(dev, "%s: FC Port %d -> %s, fc_status 0x%08llX\n",
1358 __func__, port, info->desc,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001359 readq_be(&global->fc_regs[port][FC_STATUS / 8]));
1360
1361 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001362 * Do link reset first, some OTHER errors will set FC_ERROR
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001363 * again if cleared before or w/o a reset
1364 */
1365 if (info->action & LINK_RESET) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001366 dev_err(dev, "%s: FC Port %d: resetting link\n",
1367 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001368 cfg->lr_state = LINK_RESET_REQUIRED;
1369 cfg->lr_port = port;
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06001370 kref_get(&cfg->afu->mapcount);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001371 schedule_work(&cfg->work_q);
1372 }
1373
1374 if (info->action & CLR_FC_ERROR) {
1375 reg = readq_be(&global->fc_regs[port][FC_ERROR / 8]);
1376
1377 /*
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001378 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001379 * should be the same and tracing one is sufficient.
1380 */
1381
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001382 dev_err(dev, "%s: fc %d: clearing fc_error 0x%08llX\n",
1383 __func__, port, reg);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001384
1385 writeq_be(reg, &global->fc_regs[port][FC_ERROR / 8]);
1386 writeq_be(0, &global->fc_regs[port][FC_ERRCAP / 8]);
1387 }
Matthew R. Ochsef510742015-10-21 15:13:37 -05001388
1389 if (info->action & SCAN_HOST) {
1390 atomic_inc(&cfg->scan_host_needed);
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06001391 kref_get(&cfg->afu->mapcount);
Matthew R. Ochsef510742015-10-21 15:13:37 -05001392 schedule_work(&cfg->work_q);
1393 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001394 }
1395
1396out:
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001397 dev_dbg(dev, "%s: returning IRQ_HANDLED, afu=%p\n", __func__, afu);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001398 return IRQ_HANDLED;
1399}
1400
1401/**
1402 * start_context() - starts the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001403 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001404 *
1405 * Return: A success or failure value from CXL services.
1406 */
1407static int start_context(struct cxlflash_cfg *cfg)
1408{
1409 int rc = 0;
1410
1411 rc = cxl_start_context(cfg->mcctx,
1412 cfg->afu->work.work_element_descriptor,
1413 NULL);
1414
1415 pr_debug("%s: returning rc=%d\n", __func__, rc);
1416 return rc;
1417}
1418
1419/**
1420 * read_vpd() - obtains the WWPNs from VPD
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001421 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001422 * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
1423 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001424 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001425 */
1426static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1427{
Frederic Barratca946d4e2016-03-04 12:26:43 +01001428 struct pci_dev *dev = cfg->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001429 int rc = 0;
1430 int ro_start, ro_size, i, j, k;
1431 ssize_t vpd_size;
1432 char vpd_data[CXLFLASH_VPD_LEN];
1433 char tmp_buf[WWPN_BUF_LEN] = { 0 };
1434 char *wwpn_vpd_tags[NUM_FC_PORTS] = { "V5", "V6" };
1435
1436 /* Get the VPD data from the device */
Frederic Barratca946d4e2016-03-04 12:26:43 +01001437 vpd_size = cxl_read_adapter_vpd(dev, vpd_data, sizeof(vpd_data));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001438 if (unlikely(vpd_size <= 0)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001439 dev_err(&dev->dev, "%s: Unable to read VPD (size = %ld)\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001440 __func__, vpd_size);
1441 rc = -ENODEV;
1442 goto out;
1443 }
1444
1445 /* Get the read only section offset */
1446 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
1447 PCI_VPD_LRDT_RO_DATA);
1448 if (unlikely(ro_start < 0)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001449 dev_err(&dev->dev, "%s: VPD Read-only data not found\n",
1450 __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001451 rc = -ENODEV;
1452 goto out;
1453 }
1454
1455 /* Get the read only section size, cap when extends beyond read VPD */
1456 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1457 j = ro_size;
1458 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1459 if (unlikely((i + j) > vpd_size)) {
1460 pr_debug("%s: Might need to read more VPD (%d > %ld)\n",
1461 __func__, (i + j), vpd_size);
1462 ro_size = vpd_size - i;
1463 }
1464
1465 /*
1466 * Find the offset of the WWPN tag within the read only
1467 * VPD data and validate the found field (partials are
1468 * no good to us). Convert the ASCII data to an integer
1469 * value. Note that we must copy to a temporary buffer
1470 * because the conversion service requires that the ASCII
1471 * string be terminated.
1472 */
1473 for (k = 0; k < NUM_FC_PORTS; k++) {
1474 j = ro_size;
1475 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1476
1477 i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
1478 if (unlikely(i < 0)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001479 dev_err(&dev->dev, "%s: Port %d WWPN not found "
1480 "in VPD\n", __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001481 rc = -ENODEV;
1482 goto out;
1483 }
1484
1485 j = pci_vpd_info_field_size(&vpd_data[i]);
1486 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1487 if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001488 dev_err(&dev->dev, "%s: Port %d WWPN incomplete or "
1489 "VPD corrupt\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001490 __func__, k);
1491 rc = -ENODEV;
1492 goto out;
1493 }
1494
1495 memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1496 rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1497 if (unlikely(rc)) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001498 dev_err(&dev->dev, "%s: Fail to convert port %d WWPN "
1499 "to integer\n", __func__, k);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001500 rc = -ENODEV;
1501 goto out;
1502 }
1503 }
1504
1505out:
1506 pr_debug("%s: returning rc=%d\n", __func__, rc);
1507 return rc;
1508}
1509
1510/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001511 * init_pcr() - initialize the provisioning and control registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001512 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001513 *
1514 * Also sets up fast access to the mapped registers and initializes AFU
1515 * command fields that never change.
1516 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001517static void init_pcr(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001518{
1519 struct afu *afu = cfg->afu;
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001520 struct sisl_ctrl_map __iomem *ctrl_map;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001521 int i;
1522
1523 for (i = 0; i < MAX_CONTEXT; i++) {
1524 ctrl_map = &afu->afu_map->ctrls[i].ctrl;
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001525 /* Disrupt any clients that could be running */
1526 /* e.g. clients that survived a master restart */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001527 writeq_be(0, &ctrl_map->rht_start);
1528 writeq_be(0, &ctrl_map->rht_cnt_id);
1529 writeq_be(0, &ctrl_map->ctx_cap);
1530 }
1531
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001532 /* Copy frequently used fields into afu */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001533 afu->ctx_hndl = (u16) cxl_process_element(cfg->mcctx);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001534 afu->host_map = &afu->afu_map->hosts[afu->ctx_hndl].host;
1535 afu->ctrl_map = &afu->afu_map->ctrls[afu->ctx_hndl].ctrl;
1536
1537 /* Program the Endian Control for the master context */
1538 writeq_be(SISL_ENDIAN_CTRL, &afu->host_map->endian_ctrl);
1539
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001540 /* Initialize cmd fields that never change */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001541 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
1542 afu->cmd[i].rcb.ctx_id = afu->ctx_hndl;
1543 afu->cmd[i].rcb.msi = SISL_MSI_RRQ_UPDATED;
1544 afu->cmd[i].rcb.rrq = 0x0;
1545 }
1546}
1547
1548/**
1549 * init_global() - initialize AFU global registers
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001550 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001551 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001552static int init_global(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001553{
1554 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001555 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001556 u64 wwpn[NUM_FC_PORTS]; /* wwpn of AFU ports */
1557 int i = 0, num_ports = 0;
1558 int rc = 0;
1559 u64 reg;
1560
1561 rc = read_vpd(cfg, &wwpn[0]);
1562 if (rc) {
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001563 dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001564 goto out;
1565 }
1566
1567 pr_debug("%s: wwpn0=0x%llX wwpn1=0x%llX\n", __func__, wwpn[0], wwpn[1]);
1568
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001569 /* Set up RRQ in AFU for master issued cmds */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001570 writeq_be((u64) afu->hrrq_start, &afu->host_map->rrq_start);
1571 writeq_be((u64) afu->hrrq_end, &afu->host_map->rrq_end);
1572
1573 /* AFU configuration */
1574 reg = readq_be(&afu->afu_map->global.regs.afu_config);
1575 reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1576 /* enable all auto retry options and control endianness */
1577 /* leave others at default: */
1578 /* CTX_CAP write protected, mbox_r does not clear on read and */
1579 /* checker on if dual afu */
1580 writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1581
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001582 /* Global port select: select either port */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001583 if (afu->internal_lun) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001584 /* Only use port 0 */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001585 writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
1586 num_ports = NUM_FC_PORTS - 1;
1587 } else {
1588 writeq_be(BOTH_PORTS, &afu->afu_map->global.regs.afu_port_sel);
1589 num_ports = NUM_FC_PORTS;
1590 }
1591
1592 for (i = 0; i < num_ports; i++) {
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001593 /* Unmask all errors (but they are still masked at AFU) */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001594 writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRMSK / 8]);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001595 /* Clear CRC error cnt & set a threshold */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001596 (void)readq_be(&afu->afu_map->global.
1597 fc_regs[i][FC_CNT_CRCERR / 8]);
1598 writeq_be(MC_CRC_THRESH, &afu->afu_map->global.fc_regs[i]
1599 [FC_CRC_THRESH / 8]);
1600
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001601 /* Set WWPNs. If already programmed, wwpn[i] is 0 */
Matthew R. Ochsf8013262016-09-02 15:40:20 -05001602 if (wwpn[i] != 0)
1603 afu_set_wwpn(afu, i,
1604 &afu->afu_map->global.fc_regs[i][0],
1605 wwpn[i]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001606 /* Programming WWPN back to back causes additional
1607 * offline/online transitions and a PLOGI
1608 */
1609 msleep(100);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001610 }
1611
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001612 /* Set up master's own CTX_CAP to allow real mode, host translation */
1613 /* tables, afu cmds and read/write GSCSI cmds. */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001614 /* First, unlock ctx_cap write by reading mbox */
1615 (void)readq_be(&afu->ctrl_map->mbox_r); /* unlock ctx_cap */
1616 writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1617 SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1618 SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1619 &afu->ctrl_map->ctx_cap);
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001620 /* Initialize heartbeat */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001621 afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
1622
1623out:
1624 return rc;
1625}
1626
1627/**
1628 * start_afu() - initializes and starts the AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001629 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001630 */
1631static int start_afu(struct cxlflash_cfg *cfg)
1632{
1633 struct afu *afu = cfg->afu;
1634 struct afu_cmd *cmd;
1635
1636 int i = 0;
1637 int rc = 0;
1638
1639 for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
1640 cmd = &afu->cmd[i];
1641
1642 init_completion(&cmd->cevent);
1643 spin_lock_init(&cmd->slock);
1644 cmd->parent = afu;
1645 }
1646
1647 init_pcr(cfg);
1648
Matthew R. Ochsaf104832015-10-21 15:15:14 -05001649 /* After an AFU reset, RRQ entries are stale, clear them */
1650 memset(&afu->rrq_entry, 0, sizeof(afu->rrq_entry));
1651
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001652 /* Initialize RRQ pointers */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001653 afu->hrrq_start = &afu->rrq_entry[0];
1654 afu->hrrq_end = &afu->rrq_entry[NUM_RRQ_ENTRY - 1];
1655 afu->hrrq_curr = afu->hrrq_start;
1656 afu->toggle = 1;
1657
1658 rc = init_global(cfg);
1659
1660 pr_debug("%s: returning rc=%d\n", __func__, rc);
1661 return rc;
1662}
1663
1664/**
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001665 * init_intr() - setup interrupt handlers for the master context
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001666 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001667 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001668 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001669 */
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001670static enum undo_level init_intr(struct cxlflash_cfg *cfg,
1671 struct cxl_context *ctx)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001672{
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001673 struct afu *afu = cfg->afu;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001674 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001675 int rc = 0;
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001676 enum undo_level level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001677
1678 rc = cxl_allocate_afu_irqs(ctx, 3);
1679 if (unlikely(rc)) {
1680 dev_err(dev, "%s: call to allocate_afu_irqs failed rc=%d!\n",
1681 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001682 level = UNDO_NOOP;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001683 goto out;
1684 }
1685
1686 rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, afu,
1687 "SISL_MSI_SYNC_ERROR");
1688 if (unlikely(rc <= 0)) {
1689 dev_err(dev, "%s: IRQ 1 (SISL_MSI_SYNC_ERROR) map failed!\n",
1690 __func__);
1691 level = FREE_IRQ;
1692 goto out;
1693 }
1694
1695 rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, afu,
1696 "SISL_MSI_RRQ_UPDATED");
1697 if (unlikely(rc <= 0)) {
1698 dev_err(dev, "%s: IRQ 2 (SISL_MSI_RRQ_UPDATED) map failed!\n",
1699 __func__);
1700 level = UNMAP_ONE;
1701 goto out;
1702 }
1703
1704 rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, afu,
1705 "SISL_MSI_ASYNC_ERROR");
1706 if (unlikely(rc <= 0)) {
1707 dev_err(dev, "%s: IRQ 3 (SISL_MSI_ASYNC_ERROR) map failed!\n",
1708 __func__);
1709 level = UNMAP_TWO;
1710 goto out;
1711 }
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001712out:
1713 return level;
1714}
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001715
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001716/**
1717 * init_mc() - create and register as the master context
1718 * @cfg: Internal structure associated with the host.
1719 *
1720 * Return: 0 on success, -errno on failure
1721 */
1722static int init_mc(struct cxlflash_cfg *cfg)
1723{
1724 struct cxl_context *ctx;
1725 struct device *dev = &cfg->dev->dev;
1726 int rc = 0;
1727 enum undo_level level;
1728
1729 ctx = cxl_get_context(cfg->dev);
1730 if (unlikely(!ctx)) {
1731 rc = -ENOMEM;
1732 goto ret;
1733 }
1734 cfg->mcctx = ctx;
1735
1736 /* Set it up as a master with the CXL */
1737 cxl_set_master(ctx);
1738
1739 /* During initialization reset the AFU to start from a clean slate */
1740 rc = cxl_afu_reset(cfg->mcctx);
1741 if (unlikely(rc)) {
1742 dev_err(dev, "%s: initial AFU reset failed rc=%d\n",
1743 __func__, rc);
1744 goto ret;
1745 }
1746
1747 level = init_intr(cfg, ctx);
1748 if (unlikely(level)) {
1749 dev_err(dev, "%s: setting up interrupts failed rc=%d\n",
1750 __func__, rc);
1751 goto out;
1752 }
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001753
1754 /* This performs the equivalent of the CXL_IOCTL_START_WORK.
1755 * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
1756 * element (pe) that is embedded in the context (ctx)
1757 */
1758 rc = start_context(cfg);
1759 if (unlikely(rc)) {
1760 dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
1761 level = UNMAP_THREE;
1762 goto out;
1763 }
1764ret:
1765 pr_debug("%s: returning rc=%d\n", __func__, rc);
1766 return rc;
1767out:
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001768 term_intr(cfg, level);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001769 goto ret;
1770}
1771
1772/**
1773 * init_afu() - setup as master context and start AFU
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001774 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001775 *
1776 * This routine is a higher level of control for configuring the
1777 * AFU on probe and reset paths.
1778 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001779 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001780 */
1781static int init_afu(struct cxlflash_cfg *cfg)
1782{
1783 u64 reg;
1784 int rc = 0;
1785 struct afu *afu = cfg->afu;
1786 struct device *dev = &cfg->dev->dev;
1787
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001788 cxl_perst_reloads_same_image(cfg->cxl_afu, true);
1789
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001790 rc = init_mc(cfg);
1791 if (rc) {
1792 dev_err(dev, "%s: call to init_mc failed, rc=%d!\n",
1793 __func__, rc);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001794 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001795 }
1796
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001797 /* Map the entire MMIO space of the AFU */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001798 afu->afu_map = cxl_psa_map(cfg->mcctx);
1799 if (!afu->afu_map) {
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001800 dev_err(dev, "%s: call to cxl_psa_map failed!\n", __func__);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001801 rc = -ENOMEM;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001802 goto err1;
1803 }
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06001804 kref_init(&afu->mapcount);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001805
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001806 /* No byte reverse on reading afu_version or string will be backwards */
1807 reg = readq(&afu->afu_map->global.regs.afu_version);
1808 memcpy(afu->version, &reg, sizeof(reg));
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001809 afu->interface_version =
1810 readq_be(&afu->afu_map->global.regs.interface_version);
Matthew R. Ochse5ce0672015-10-21 15:14:01 -05001811 if ((afu->interface_version + 1) == 0) {
1812 pr_err("Back level AFU, please upgrade. AFU version %s "
1813 "interface version 0x%llx\n", afu->version,
1814 afu->interface_version);
1815 rc = -EINVAL;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001816 goto err2;
1817 }
1818
1819 pr_debug("%s: afu version %s, interface version 0x%llX\n", __func__,
1820 afu->version, afu->interface_version);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001821
1822 rc = start_afu(cfg);
1823 if (rc) {
1824 dev_err(dev, "%s: call to start_afu failed, rc=%d!\n",
1825 __func__, rc);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001826 goto err2;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001827 }
1828
1829 afu_err_intr_init(cfg->afu);
1830 atomic64_set(&afu->room, readq_be(&afu->host_map->cmd_room));
1831
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05001832 /* Restore the LUN mappings */
1833 cxlflash_restore_luntable(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001834out:
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001835 pr_debug("%s: returning rc=%d\n", __func__, rc);
1836 return rc;
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001837
1838err2:
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06001839 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001840err1:
Manoj N. Kumar9526f362016-03-25 14:26:34 -05001841 term_intr(cfg, UNMAP_THREE);
1842 term_mc(cfg);
Matthew R. Ochsee3491b2015-10-21 15:16:00 -05001843 goto out;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001844}
1845
1846/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001847 * cxlflash_afu_sync() - builds and sends an AFU sync command
1848 * @afu: AFU associated with the host.
1849 * @ctx_hndl_u: Identifies context requesting sync.
1850 * @res_hndl_u: Identifies resource requesting sync.
1851 * @mode: Type of sync to issue (lightweight, heavyweight, global).
1852 *
1853 * The AFU can only take 1 sync command at a time. This routine enforces this
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001854 * limitation by using a mutex to provide exclusive access to the AFU during
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001855 * the sync. This design point requires calling threads to not be on interrupt
1856 * context due to the possibility of sleeping during concurrent sync operations.
1857 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001858 * AFU sync operations are only necessary and allowed when the device is
1859 * operating normally. When not operating normally, sync requests can occur as
1860 * part of cleaning up resources associated with an adapter prior to removal.
1861 * In this scenario, these requests are simply ignored (safe due to the AFU
1862 * going away).
1863 *
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001864 * Return:
1865 * 0 on success
1866 * -1 on failure
1867 */
1868int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
1869 res_hndl_t res_hndl_u, u8 mode)
1870{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001871 struct cxlflash_cfg *cfg = afu->parent;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001872 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001873 struct afu_cmd *cmd = NULL;
1874 int rc = 0;
1875 int retry_cnt = 0;
1876 static DEFINE_MUTEX(sync_active);
1877
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05001878 if (cfg->state != STATE_NORMAL) {
1879 pr_debug("%s: Sync not required! (%u)\n", __func__, cfg->state);
1880 return 0;
1881 }
1882
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001883 mutex_lock(&sync_active);
1884retry:
Matthew R. Ochs15305512015-10-21 15:12:10 -05001885 cmd = cmd_checkout(afu);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001886 if (unlikely(!cmd)) {
1887 retry_cnt++;
1888 udelay(1000 * retry_cnt);
1889 if (retry_cnt < MC_RETRY_CNT)
1890 goto retry;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05001891 dev_err(dev, "%s: could not get a free command\n", __func__);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001892 rc = -1;
1893 goto out;
1894 }
1895
1896 pr_debug("%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
1897
1898 memset(cmd->rcb.cdb, 0, sizeof(cmd->rcb.cdb));
1899
1900 cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
1901 cmd->rcb.port_sel = 0x0; /* NA */
1902 cmd->rcb.lun_id = 0x0; /* NA */
1903 cmd->rcb.data_len = 0x0;
1904 cmd->rcb.data_ea = 0x0;
1905 cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
1906
1907 cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
1908 cmd->rcb.cdb[1] = mode;
1909
1910 /* The cdb is aligned, no unaligned accessors required */
Matthew R. Ochs1786f4a2015-10-21 15:14:48 -05001911 *((__be16 *)&cmd->rcb.cdb[2]) = cpu_to_be16(ctx_hndl_u);
1912 *((__be32 *)&cmd->rcb.cdb[4]) = cpu_to_be32(res_hndl_u);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001913
Matthew R. Ochs15305512015-10-21 15:12:10 -05001914 rc = send_cmd(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001915 if (unlikely(rc))
1916 goto out;
1917
Matthew R. Ochs15305512015-10-21 15:12:10 -05001918 wait_resp(afu, cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001919
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05001920 /* Set on timeout */
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001921 if (unlikely((cmd->sa.ioasc != 0) ||
1922 (cmd->sa.host_use_b[0] & B_ERROR)))
1923 rc = -1;
1924out:
1925 mutex_unlock(&sync_active);
1926 if (cmd)
Matthew R. Ochs15305512015-10-21 15:12:10 -05001927 cmd_checkin(cmd);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001928 pr_debug("%s: returning rc=%d\n", __func__, rc);
1929 return rc;
1930}
1931
1932/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001933 * afu_reset() - resets the AFU
1934 * @cfg: Internal structure associated with the host.
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001935 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05001936 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001937 */
Matthew R. Ochs15305512015-10-21 15:12:10 -05001938static int afu_reset(struct cxlflash_cfg *cfg)
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05001939{
1940 int rc = 0;
1941 /* Stop the context before the reset. Since the context is
1942 * no longer available restart it after the reset is complete
1943 */
1944
1945 term_afu(cfg);
1946
1947 rc = init_afu(cfg);
1948
1949 pr_debug("%s: returning rc=%d\n", __func__, rc);
1950 return rc;
1951}
1952
1953/**
Manoj N. Kumarf4113962016-06-15 18:49:20 -05001954 * drain_ioctls() - wait until all currently executing ioctls have completed
1955 * @cfg: Internal structure associated with the host.
1956 *
1957 * Obtain write access to read/write semaphore that wraps ioctl
1958 * handling to 'drain' ioctls currently executing.
1959 */
1960static void drain_ioctls(struct cxlflash_cfg *cfg)
1961{
1962 down_write(&cfg->ioctl_rwsem);
1963 up_write(&cfg->ioctl_rwsem);
1964}
1965
1966/**
Matthew R. Ochs15305512015-10-21 15:12:10 -05001967 * cxlflash_eh_device_reset_handler() - reset a single LUN
1968 * @scp: SCSI command to send.
1969 *
1970 * Return:
1971 * SUCCESS as defined in scsi/scsi.h
1972 * FAILED as defined in scsi/scsi.h
1973 */
1974static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
1975{
1976 int rc = SUCCESS;
1977 struct Scsi_Host *host = scp->device->host;
1978 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
1979 struct afu *afu = cfg->afu;
1980 int rcr = 0;
1981
1982 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
1983 "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
1984 host->host_no, scp->device->channel,
1985 scp->device->id, scp->device->lun,
1986 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
1987 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
1988 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
1989 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
1990
Matthew R. Ochsed486da2015-10-21 15:14:24 -05001991retry:
Matthew R. Ochs15305512015-10-21 15:12:10 -05001992 switch (cfg->state) {
1993 case STATE_NORMAL:
1994 rcr = send_tmf(afu, scp, TMF_LUN_RESET);
1995 if (unlikely(rcr))
1996 rc = FAILED;
1997 break;
1998 case STATE_RESET:
1999 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
Matthew R. Ochsed486da2015-10-21 15:14:24 -05002000 goto retry;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002001 default:
2002 rc = FAILED;
2003 break;
2004 }
2005
2006 pr_debug("%s: returning rc=%d\n", __func__, rc);
2007 return rc;
2008}
2009
2010/**
2011 * cxlflash_eh_host_reset_handler() - reset the host adapter
2012 * @scp: SCSI command from stack identifying host.
2013 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002014 * Following a reset, the state is evaluated again in case an EEH occurred
2015 * during the reset. In such a scenario, the host reset will either yield
2016 * until the EEH recovery is complete or return success or failure based
2017 * upon the current device state.
2018 *
Matthew R. Ochs15305512015-10-21 15:12:10 -05002019 * Return:
2020 * SUCCESS as defined in scsi/scsi.h
2021 * FAILED as defined in scsi/scsi.h
2022 */
2023static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
2024{
2025 int rc = SUCCESS;
2026 int rcr = 0;
2027 struct Scsi_Host *host = scp->device->host;
2028 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
2029
2030 pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
2031 "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
2032 host->host_no, scp->device->channel,
2033 scp->device->id, scp->device->lun,
2034 get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
2035 get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
2036 get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
2037 get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
2038
2039 switch (cfg->state) {
2040 case STATE_NORMAL:
2041 cfg->state = STATE_RESET;
Manoj N. Kumarf4113962016-06-15 18:49:20 -05002042 drain_ioctls(cfg);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002043 cxlflash_mark_contexts_error(cfg);
2044 rcr = afu_reset(cfg);
2045 if (rcr) {
2046 rc = FAILED;
2047 cfg->state = STATE_FAILTERM;
2048 } else
2049 cfg->state = STATE_NORMAL;
2050 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002051 ssleep(1);
2052 /* fall through */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002053 case STATE_RESET:
2054 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2055 if (cfg->state == STATE_NORMAL)
2056 break;
2057 /* fall through */
2058 default:
2059 rc = FAILED;
2060 break;
2061 }
2062
2063 pr_debug("%s: returning rc=%d\n", __func__, rc);
2064 return rc;
2065}
2066
2067/**
2068 * cxlflash_change_queue_depth() - change the queue depth for the device
2069 * @sdev: SCSI device destined for queue depth change.
2070 * @qdepth: Requested queue depth value to set.
2071 *
2072 * The requested queue depth is capped to the maximum supported value.
2073 *
2074 * Return: The actual queue depth set.
2075 */
2076static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
2077{
2078
2079 if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
2080 qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
2081
2082 scsi_change_queue_depth(sdev, qdepth);
2083 return sdev->queue_depth;
2084}
2085
2086/**
2087 * cxlflash_show_port_status() - queries and presents the current port status
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002088 * @port: Desired port for status reporting.
2089 * @afu: AFU owning the specified port.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002090 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2091 *
2092 * Return: The size of the ASCII string returned in @buf.
2093 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002094static ssize_t cxlflash_show_port_status(u32 port, struct afu *afu, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002095{
Matthew R. Ochs15305512015-10-21 15:12:10 -05002096 char *disp_status;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002097 u64 status;
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002098 __be64 __iomem *fc_regs;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002099
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002100 if (port >= NUM_FC_PORTS)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002101 return 0;
2102
2103 fc_regs = &afu->afu_map->global.fc_regs[port][0];
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002104 status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
2105 status &= FC_MTIP_STATUS_MASK;
Matthew R. Ochs15305512015-10-21 15:12:10 -05002106
2107 if (status == FC_MTIP_STATUS_ONLINE)
2108 disp_status = "online";
2109 else if (status == FC_MTIP_STATUS_OFFLINE)
2110 disp_status = "offline";
2111 else
2112 disp_status = "unknown";
2113
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002114 return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002115}
2116
2117/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002118 * port0_show() - queries and presents the current status of port 0
2119 * @dev: Generic device associated with the host owning the port.
2120 * @attr: Device attribute representing the port.
2121 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002122 *
2123 * Return: The size of the ASCII string returned in @buf.
2124 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002125static ssize_t port0_show(struct device *dev,
2126 struct device_attribute *attr,
2127 char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002128{
2129 struct Scsi_Host *shost = class_to_shost(dev);
2130 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2131 struct afu *afu = cfg->afu;
2132
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002133 return cxlflash_show_port_status(0, afu, buf);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002134}
2135
2136/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002137 * port1_show() - queries and presents the current status of port 1
2138 * @dev: Generic device associated with the host owning the port.
2139 * @attr: Device attribute representing the port.
2140 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2141 *
2142 * Return: The size of the ASCII string returned in @buf.
2143 */
2144static ssize_t port1_show(struct device *dev,
2145 struct device_attribute *attr,
2146 char *buf)
2147{
2148 struct Scsi_Host *shost = class_to_shost(dev);
2149 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2150 struct afu *afu = cfg->afu;
2151
2152 return cxlflash_show_port_status(1, afu, buf);
2153}
2154
2155/**
2156 * lun_mode_show() - presents the current LUN mode of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002157 * @dev: Generic device associated with the host.
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002158 * @attr: Device attribute representing the LUN mode.
2159 * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
2160 *
2161 * Return: The size of the ASCII string returned in @buf.
2162 */
2163static ssize_t lun_mode_show(struct device *dev,
2164 struct device_attribute *attr, char *buf)
2165{
2166 struct Scsi_Host *shost = class_to_shost(dev);
2167 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2168 struct afu *afu = cfg->afu;
2169
2170 return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
2171}
2172
2173/**
2174 * lun_mode_store() - sets the LUN mode of the host
2175 * @dev: Generic device associated with the host.
2176 * @attr: Device attribute representing the LUN mode.
Matthew R. Ochs15305512015-10-21 15:12:10 -05002177 * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
2178 * @count: Length of data resizing in @buf.
2179 *
2180 * The CXL Flash AFU supports a dummy LUN mode where the external
2181 * links and storage are not required. Space on the FPGA is used
2182 * to create 1 or 2 small LUNs which are presented to the system
2183 * as if they were a normal storage device. This feature is useful
2184 * during development and also provides manufacturing with a way
2185 * to test the AFU without an actual device.
2186 *
2187 * 0 = external LUN[s] (default)
2188 * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
2189 * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
2190 * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
2191 * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
2192 *
2193 * Return: The size of the ASCII string returned in @buf.
2194 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002195static ssize_t lun_mode_store(struct device *dev,
2196 struct device_attribute *attr,
2197 const char *buf, size_t count)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002198{
2199 struct Scsi_Host *shost = class_to_shost(dev);
2200 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2201 struct afu *afu = cfg->afu;
2202 int rc;
2203 u32 lun_mode;
2204
2205 rc = kstrtouint(buf, 10, &lun_mode);
2206 if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
2207 afu->internal_lun = lun_mode;
Manoj N. Kumar603ecce2016-03-04 15:55:19 -06002208
2209 /*
2210 * When configured for internal LUN, there is only one channel,
2211 * channel number 0, else there will be 2 (default).
2212 */
2213 if (afu->internal_lun)
2214 shost->max_channel = 0;
2215 else
2216 shost->max_channel = NUM_FC_PORTS - 1;
2217
Matthew R. Ochs15305512015-10-21 15:12:10 -05002218 afu_reset(cfg);
2219 scsi_scan_host(cfg->host);
2220 }
2221
2222 return count;
2223}
2224
2225/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002226 * ioctl_version_show() - presents the current ioctl version of the host
Matthew R. Ochs15305512015-10-21 15:12:10 -05002227 * @dev: Generic device associated with the host.
2228 * @attr: Device attribute representing the ioctl version.
2229 * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
2230 *
2231 * Return: The size of the ASCII string returned in @buf.
2232 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002233static ssize_t ioctl_version_show(struct device *dev,
2234 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002235{
2236 return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
2237}
2238
2239/**
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002240 * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2241 * @port: Desired port for status reporting.
2242 * @afu: AFU owning the specified port.
2243 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2244 *
2245 * Return: The size of the ASCII string returned in @buf.
2246 */
2247static ssize_t cxlflash_show_port_lun_table(u32 port,
2248 struct afu *afu,
2249 char *buf)
2250{
2251 int i;
2252 ssize_t bytes = 0;
2253 __be64 __iomem *fc_port;
2254
2255 if (port >= NUM_FC_PORTS)
2256 return 0;
2257
2258 fc_port = &afu->afu_map->global.fc_port[port][0];
2259
2260 for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2261 bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
2262 "%03d: %016llX\n", i, readq_be(&fc_port[i]));
2263 return bytes;
2264}
2265
2266/**
2267 * port0_lun_table_show() - presents the current LUN table of port 0
2268 * @dev: Generic device associated with the host owning the port.
2269 * @attr: Device attribute representing the port.
2270 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2271 *
2272 * Return: The size of the ASCII string returned in @buf.
2273 */
2274static ssize_t port0_lun_table_show(struct device *dev,
2275 struct device_attribute *attr,
2276 char *buf)
2277{
2278 struct Scsi_Host *shost = class_to_shost(dev);
2279 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2280 struct afu *afu = cfg->afu;
2281
2282 return cxlflash_show_port_lun_table(0, afu, buf);
2283}
2284
2285/**
2286 * port1_lun_table_show() - presents the current LUN table of port 1
2287 * @dev: Generic device associated with the host owning the port.
2288 * @attr: Device attribute representing the port.
2289 * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
2290 *
2291 * Return: The size of the ASCII string returned in @buf.
2292 */
2293static ssize_t port1_lun_table_show(struct device *dev,
2294 struct device_attribute *attr,
2295 char *buf)
2296{
2297 struct Scsi_Host *shost = class_to_shost(dev);
2298 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
2299 struct afu *afu = cfg->afu;
2300
2301 return cxlflash_show_port_lun_table(1, afu, buf);
2302}
2303
2304/**
2305 * mode_show() - presents the current mode of the device
Matthew R. Ochs15305512015-10-21 15:12:10 -05002306 * @dev: Generic device associated with the device.
2307 * @attr: Device attribute representing the device mode.
2308 * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
2309 *
2310 * Return: The size of the ASCII string returned in @buf.
2311 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002312static ssize_t mode_show(struct device *dev,
2313 struct device_attribute *attr, char *buf)
Matthew R. Ochs15305512015-10-21 15:12:10 -05002314{
2315 struct scsi_device *sdev = to_scsi_device(dev);
2316
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002317 return scnprintf(buf, PAGE_SIZE, "%s\n",
2318 sdev->hostdata ? "superpipe" : "legacy");
Matthew R. Ochs15305512015-10-21 15:12:10 -05002319}
2320
2321/*
2322 * Host attributes
2323 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002324static DEVICE_ATTR_RO(port0);
2325static DEVICE_ATTR_RO(port1);
2326static DEVICE_ATTR_RW(lun_mode);
2327static DEVICE_ATTR_RO(ioctl_version);
2328static DEVICE_ATTR_RO(port0_lun_table);
2329static DEVICE_ATTR_RO(port1_lun_table);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002330
2331static struct device_attribute *cxlflash_host_attrs[] = {
2332 &dev_attr_port0,
2333 &dev_attr_port1,
2334 &dev_attr_lun_mode,
2335 &dev_attr_ioctl_version,
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002336 &dev_attr_port0_lun_table,
2337 &dev_attr_port1_lun_table,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002338 NULL
2339};
2340
2341/*
2342 * Device attributes
2343 */
Matthew R. Ochse0f01a22015-10-21 15:12:39 -05002344static DEVICE_ATTR_RO(mode);
Matthew R. Ochs15305512015-10-21 15:12:10 -05002345
2346static struct device_attribute *cxlflash_dev_attrs[] = {
2347 &dev_attr_mode,
2348 NULL
2349};
2350
2351/*
2352 * Host template
2353 */
2354static struct scsi_host_template driver_template = {
2355 .module = THIS_MODULE,
2356 .name = CXLFLASH_ADAPTER_NAME,
2357 .info = cxlflash_driver_info,
2358 .ioctl = cxlflash_ioctl,
2359 .proc_name = CXLFLASH_NAME,
2360 .queuecommand = cxlflash_queuecommand,
2361 .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
2362 .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
2363 .change_queue_depth = cxlflash_change_queue_depth,
Manoj N. Kumar83430832016-03-04 15:55:20 -06002364 .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
Matthew R. Ochs15305512015-10-21 15:12:10 -05002365 .can_queue = CXLFLASH_MAX_CMDS,
2366 .this_id = -1,
Uma Krishnan68ab2d72016-11-28 18:41:06 -06002367 .sg_tablesize = 1, /* No scatter gather support */
Matthew R. Ochs15305512015-10-21 15:12:10 -05002368 .max_sectors = CXLFLASH_MAX_SECTORS,
2369 .use_clustering = ENABLE_CLUSTERING,
2370 .shost_attrs = cxlflash_host_attrs,
2371 .sdev_attrs = cxlflash_dev_attrs,
2372};
2373
2374/*
2375 * Device dependent values
2376 */
Uma Krishnan96e1b662016-06-15 18:49:38 -05002377static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
2378 0ULL };
2379static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
Uma Krishnan704c4b02016-06-15 18:49:57 -05002380 CXLFLASH_NOTIFY_SHUTDOWN };
Matthew R. Ochs15305512015-10-21 15:12:10 -05002381
2382/*
2383 * PCI device binding table
2384 */
2385static struct pci_device_id cxlflash_pci_table[] = {
2386 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
2387 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
Manoj Kumara2746fb2015-12-14 15:07:43 -06002388 {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
2389 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
Matthew R. Ochs15305512015-10-21 15:12:10 -05002390 {}
2391};
2392
2393MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
2394
2395/**
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002396 * cxlflash_worker_thread() - work thread handler for the AFU
2397 * @work: Work structure contained within cxlflash associated with host.
2398 *
2399 * Handles the following events:
2400 * - Link reset which cannot be performed on interrupt context due to
2401 * blocking up to a few seconds
2402 * - Read AFU command room
Matthew R. Ochsef510742015-10-21 15:13:37 -05002403 * - Rescan the host
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002404 */
2405static void cxlflash_worker_thread(struct work_struct *work)
2406{
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002407 struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
2408 work_q);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002409 struct afu *afu = cfg->afu;
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002410 struct device *dev = &cfg->dev->dev;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002411 int port;
2412 ulong lock_flags;
2413
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002414 /* Avoid MMIO if the device has failed */
2415
2416 if (cfg->state != STATE_NORMAL)
2417 return;
2418
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002419 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2420
2421 if (cfg->lr_state == LINK_RESET_REQUIRED) {
2422 port = cfg->lr_port;
2423 if (port < 0)
Matthew R. Ochs4392ba42015-10-21 15:13:11 -05002424 dev_err(dev, "%s: invalid port index %d\n",
2425 __func__, port);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002426 else {
2427 spin_unlock_irqrestore(cfg->host->host_lock,
2428 lock_flags);
2429
2430 /* The reset can block... */
2431 afu_link_reset(afu, port,
Matthew R. Ochsf15fbf82015-10-21 15:15:06 -05002432 &afu->afu_map->global.fc_regs[port][0]);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002433 spin_lock_irqsave(cfg->host->host_lock, lock_flags);
2434 }
2435
2436 cfg->lr_state = LINK_RESET_COMPLETE;
2437 }
2438
2439 if (afu->read_room) {
2440 atomic64_set(&afu->room, readq_be(&afu->host_map->cmd_room));
2441 afu->read_room = false;
2442 }
2443
2444 spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
Matthew R. Ochsef510742015-10-21 15:13:37 -05002445
2446 if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
2447 scsi_scan_host(cfg->host);
Manoj Kumarb45cdbaf2015-12-14 15:07:23 -06002448 kref_put(&afu->mapcount, afu_unmap);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002449}
2450
2451/**
2452 * cxlflash_probe() - PCI entry point to add host
2453 * @pdev: PCI device associated with the host.
2454 * @dev_id: PCI device id associated with device.
2455 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002456 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002457 */
2458static int cxlflash_probe(struct pci_dev *pdev,
2459 const struct pci_device_id *dev_id)
2460{
2461 struct Scsi_Host *host;
2462 struct cxlflash_cfg *cfg = NULL;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002463 struct dev_dependent_vals *ddv;
2464 int rc = 0;
2465
2466 dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
2467 __func__, pdev->irq);
2468
2469 ddv = (struct dev_dependent_vals *)dev_id->driver_data;
2470 driver_template.max_sectors = ddv->max_sectors;
2471
2472 host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
2473 if (!host) {
2474 dev_err(&pdev->dev, "%s: call to scsi_host_alloc failed!\n",
2475 __func__);
2476 rc = -ENOMEM;
2477 goto out;
2478 }
2479
2480 host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
2481 host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
2482 host->max_channel = NUM_FC_PORTS - 1;
2483 host->unique_id = host->host_no;
2484 host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
2485
2486 cfg = (struct cxlflash_cfg *)host->hostdata;
2487 cfg->host = host;
2488 rc = alloc_mem(cfg);
2489 if (rc) {
Matthew R. Ochsfa3f2c62015-10-21 15:15:45 -05002490 dev_err(&pdev->dev, "%s: call to alloc_mem failed!\n",
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002491 __func__);
2492 rc = -ENOMEM;
Matthew R. Ochs8b5b1e82015-10-21 15:14:09 -05002493 scsi_host_put(cfg->host);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002494 goto out;
2495 }
2496
2497 cfg->init_state = INIT_STATE_NONE;
2498 cfg->dev = pdev;
Matthew R. Ochs17ead262015-10-21 15:15:37 -05002499 cfg->cxl_fops = cxlflash_cxl_fops;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -05002500
2501 /*
2502 * The promoted LUNs move to the top of the LUN table. The rest stay
2503 * on the bottom half. The bottom half grows from the end
2504 * (index = 255), whereas the top half grows from the beginning
2505 * (index = 0).
2506 */
2507 cfg->promote_lun_index = 0;
2508 cfg->last_lun_index[0] = CXLFLASH_NUM_VLUNS/2 - 1;
2509 cfg->last_lun_index[1] = CXLFLASH_NUM_VLUNS/2 - 1;
2510
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002511 cfg->dev_id = (struct pci_device_id *)dev_id;
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002512
2513 init_waitqueue_head(&cfg->tmf_waitq);
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002514 init_waitqueue_head(&cfg->reset_waitq);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002515
2516 INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
2517 cfg->lr_state = LINK_RESET_INVALID;
2518 cfg->lr_port = -1;
Matthew R. Ochs0d731222015-10-21 15:16:24 -05002519 spin_lock_init(&cfg->tmf_slock);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002520 mutex_init(&cfg->ctx_tbl_list_mutex);
2521 mutex_init(&cfg->ctx_recovery_mutex);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002522 init_rwsem(&cfg->ioctl_rwsem);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002523 INIT_LIST_HEAD(&cfg->ctx_err_recovery);
2524 INIT_LIST_HEAD(&cfg->lluns);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002525
2526 pci_set_drvdata(pdev, cfg);
2527
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002528 cfg->cxl_afu = cxl_pci_to_afu(pdev);
2529
2530 rc = init_pci(cfg);
2531 if (rc) {
2532 dev_err(&pdev->dev, "%s: call to init_pci "
2533 "failed rc=%d!\n", __func__, rc);
2534 goto out_remove;
2535 }
2536 cfg->init_state = INIT_STATE_PCI;
2537
2538 rc = init_afu(cfg);
2539 if (rc) {
2540 dev_err(&pdev->dev, "%s: call to init_afu "
2541 "failed rc=%d!\n", __func__, rc);
2542 goto out_remove;
2543 }
2544 cfg->init_state = INIT_STATE_AFU;
2545
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002546 rc = init_scsi(cfg);
2547 if (rc) {
2548 dev_err(&pdev->dev, "%s: call to init_scsi "
2549 "failed rc=%d!\n", __func__, rc);
2550 goto out_remove;
2551 }
2552 cfg->init_state = INIT_STATE_SCSI;
2553
2554out:
2555 pr_debug("%s: returning rc=%d\n", __func__, rc);
2556 return rc;
2557
2558out_remove:
2559 cxlflash_remove(pdev);
2560 goto out;
2561}
2562
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002563/**
2564 * cxlflash_pci_error_detected() - called when a PCI error is detected
2565 * @pdev: PCI device struct.
2566 * @state: PCI channel state.
2567 *
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002568 * When an EEH occurs during an active reset, wait until the reset is
2569 * complete and then take action based upon the device state.
2570 *
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002571 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
2572 */
2573static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
2574 pci_channel_state_t state)
2575{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002576 int rc = 0;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002577 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2578 struct device *dev = &cfg->dev->dev;
2579
2580 dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
2581
2582 switch (state) {
2583 case pci_channel_io_frozen:
Matthew R. Ochs1d3324c2016-09-02 15:39:30 -05002584 wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2585 if (cfg->state == STATE_FAILTERM)
2586 return PCI_ERS_RESULT_DISCONNECT;
2587
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002588 cfg->state = STATE_RESET;
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002589 scsi_block_requests(cfg->host);
Matthew R. Ochs0a27ae52015-10-21 15:11:52 -05002590 drain_ioctls(cfg);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002591 rc = cxlflash_mark_contexts_error(cfg);
2592 if (unlikely(rc))
2593 dev_err(dev, "%s: Failed to mark user contexts!(%d)\n",
2594 __func__, rc);
Manoj N. Kumar9526f362016-03-25 14:26:34 -05002595 term_afu(cfg);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002596 return PCI_ERS_RESULT_NEED_RESET;
2597 case pci_channel_io_perm_failure:
2598 cfg->state = STATE_FAILTERM;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002599 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002600 scsi_unblock_requests(cfg->host);
2601 return PCI_ERS_RESULT_DISCONNECT;
2602 default:
2603 break;
2604 }
2605 return PCI_ERS_RESULT_NEED_RESET;
2606}
2607
2608/**
2609 * cxlflash_pci_slot_reset() - called when PCI slot has been reset
2610 * @pdev: PCI device struct.
2611 *
2612 * This routine is called by the pci error recovery code after the PCI
2613 * slot has been reset, just before we should resume normal operations.
2614 *
2615 * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
2616 */
2617static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
2618{
2619 int rc = 0;
2620 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2621 struct device *dev = &cfg->dev->dev;
2622
2623 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2624
2625 rc = init_afu(cfg);
2626 if (unlikely(rc)) {
2627 dev_err(dev, "%s: EEH recovery failed! (%d)\n", __func__, rc);
2628 return PCI_ERS_RESULT_DISCONNECT;
2629 }
2630
2631 return PCI_ERS_RESULT_RECOVERED;
2632}
2633
2634/**
2635 * cxlflash_pci_resume() - called when normal operation can resume
2636 * @pdev: PCI device struct
2637 */
2638static void cxlflash_pci_resume(struct pci_dev *pdev)
2639{
2640 struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
2641 struct device *dev = &cfg->dev->dev;
2642
2643 dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
2644
2645 cfg->state = STATE_NORMAL;
Matthew R. Ochs439e85c2015-10-21 15:12:00 -05002646 wake_up_all(&cfg->reset_waitq);
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002647 scsi_unblock_requests(cfg->host);
2648}
2649
2650static const struct pci_error_handlers cxlflash_err_handler = {
2651 .error_detected = cxlflash_pci_error_detected,
2652 .slot_reset = cxlflash_pci_slot_reset,
2653 .resume = cxlflash_pci_resume,
2654};
2655
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002656/*
2657 * PCI device structure
2658 */
2659static struct pci_driver cxlflash_driver = {
2660 .name = CXLFLASH_NAME,
2661 .id_table = cxlflash_pci_table,
2662 .probe = cxlflash_probe,
2663 .remove = cxlflash_remove,
Uma Krishnanbabf9852016-09-02 15:39:16 -05002664 .shutdown = cxlflash_remove,
Matthew R. Ochs5cdac812015-08-13 21:47:34 -05002665 .err_handler = &cxlflash_err_handler,
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002666};
2667
2668/**
2669 * init_cxlflash() - module entry point
2670 *
Matthew R. Ochs1284fb02015-10-21 15:14:40 -05002671 * Return: 0 on success, -errno on failure
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002672 */
2673static int __init init_cxlflash(void)
2674{
Uma Krishnan85599212015-12-14 15:06:33 -06002675 pr_info("%s: %s\n", __func__, CXLFLASH_ADAPTER_NAME);
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002676
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002677 cxlflash_list_init();
2678
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002679 return pci_register_driver(&cxlflash_driver);
2680}
2681
2682/**
2683 * exit_cxlflash() - module exit point
2684 */
2685static void __exit exit_cxlflash(void)
2686{
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05002687 cxlflash_term_global_luns();
2688 cxlflash_free_errpage();
2689
Matthew R. Ochsc21e0bb2015-06-09 17:15:52 -05002690 pci_unregister_driver(&cxlflash_driver);
2691}
2692
2693module_init(init_cxlflash);
2694module_exit(exit_cxlflash);