scsi: hisi_sas: add v3 hw suspend and resume
[linux-2.6-block.git] / drivers / scsi / lpfc / lpfc_nvme.c
CommitLineData
01649561
JS
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
d080abe0
JS
4 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
01649561
JS
6 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
7 * EMULEX and SLI are trademarks of Emulex. *
d080abe0 8 * www.broadcom.com *
01649561
JS
9 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
10 * *
11 * This program is free software; you can redistribute it and/or *
12 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
22 ********************************************************************/
23#include <linux/pci.h>
24#include <linux/slab.h>
25#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <asm/unaligned.h>
28#include <linux/crc-t10dif.h>
29#include <net/checksum.h>
30
31#include <scsi/scsi.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_eh.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_tcq.h>
36#include <scsi/scsi_transport_fc.h>
37#include <scsi/fc/fc_fs.h>
38
39#include <linux/nvme.h>
40#include <linux/nvme-fc-driver.h>
41#include <linux/nvme-fc.h>
42#include "lpfc_version.h"
43#include "lpfc_hw4.h"
44#include "lpfc_hw.h"
45#include "lpfc_sli.h"
46#include "lpfc_sli4.h"
47#include "lpfc_nl.h"
48#include "lpfc_disc.h"
49#include "lpfc.h"
50#include "lpfc_nvme.h"
51#include "lpfc_scsi.h"
52#include "lpfc_logmsg.h"
53#include "lpfc_crtn.h"
54#include "lpfc_vport.h"
bd2cdd5e 55#include "lpfc_debugfs.h"
01649561
JS
56
57/* NVME initiator-based functions */
58
59static struct lpfc_nvme_buf *
60lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp);
61
62static void
63lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_nvme_buf *);
64
81e6a637 65static struct nvme_fc_port_template lpfc_nvme_template;
01649561
JS
66
67/**
68 * lpfc_nvme_create_queue -
69 * @lpfc_pnvme: Pointer to the driver's nvme instance data
70 * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
71 * @handle: An opaque driver handle used in follow-up calls.
72 *
73 * Driver registers this routine to preallocate and initialize any
74 * internal data structures to bind the @qidx to its internal IO queues.
75 * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
76 *
77 * Return value :
78 * 0 - Success
79 * -EINVAL - Unsupported input value.
80 * -ENOMEM - Could not alloc necessary memory
81 **/
82static int
83lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
84 unsigned int qidx, u16 qsize,
85 void **handle)
86{
87 struct lpfc_nvme_lport *lport;
88 struct lpfc_vport *vport;
89 struct lpfc_nvme_qhandle *qhandle;
90 char *str;
91
c3725bdc
JS
92 if (!pnvme_lport->private)
93 return -ENOMEM;
94
01649561
JS
95 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
96 vport = lport->vport;
97 qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
98 if (qhandle == NULL)
99 return -ENOMEM;
100
101 qhandle->cpu_id = smp_processor_id();
102 qhandle->qidx = qidx;
103 /*
104 * NVME qidx == 0 is the admin queue, so both admin queue
105 * and first IO queue will use MSI-X vector and associated
106 * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
107 */
108 if (qidx) {
109 str = "IO "; /* IO queue */
110 qhandle->index = ((qidx - 1) %
111 vport->phba->cfg_nvme_io_channel);
112 } else {
113 str = "ADM"; /* Admin queue */
114 qhandle->index = qidx;
115 }
116
d58734f0 117 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
01649561
JS
118 "6073 Binding %s HdwQueue %d (cpu %d) to "
119 "io_channel %d qhandle %p\n", str,
120 qidx, qhandle->cpu_id, qhandle->index, qhandle);
121 *handle = (void *)qhandle;
122 return 0;
123}
124
125/**
126 * lpfc_nvme_delete_queue -
127 * @lpfc_pnvme: Pointer to the driver's nvme instance data
128 * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
129 * @handle: An opaque driver handle from lpfc_nvme_create_queue
130 *
131 * Driver registers this routine to free
132 * any internal data structures to bind the @qidx to its internal
133 * IO queues.
134 *
135 * Return value :
136 * 0 - Success
137 * TODO: What are the failure codes.
138 **/
139static void
140lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
141 unsigned int qidx,
142 void *handle)
143{
144 struct lpfc_nvme_lport *lport;
145 struct lpfc_vport *vport;
146
c3725bdc
JS
147 if (!pnvme_lport->private)
148 return;
149
01649561
JS
150 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
151 vport = lport->vport;
152
153 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
154 "6001 ENTER. lpfc_pnvme %p, qidx x%xi qhandle %p\n",
155 lport, qidx, handle);
156 kfree(handle);
157}
158
159static void
160lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
161{
162 struct lpfc_nvme_lport *lport = localport->private;
163
add9d6be
JS
164 lpfc_printf_vlog(lport->vport, KERN_INFO, LOG_NVME,
165 "6173 localport %p delete complete\n",
166 lport);
167
01649561
JS
168 /* release any threads waiting for the unreg to complete */
169 complete(&lport->lport_unreg_done);
170}
171
172/* lpfc_nvme_remoteport_delete
173 *
174 * @remoteport: Pointer to an nvme transport remoteport instance.
175 *
176 * This is a template downcall. NVME transport calls this function
177 * when it has completed the unregistration of a previously
178 * registered remoteport.
179 *
180 * Return value :
181 * None
182 */
183void
184lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
185{
186 struct lpfc_nvme_rport *rport = remoteport->private;
187 struct lpfc_vport *vport;
188 struct lpfc_nodelist *ndlp;
189
190 ndlp = rport->ndlp;
191 if (!ndlp)
192 goto rport_err;
193
194 vport = ndlp->vport;
195 if (!vport)
196 goto rport_err;
197
198 /* Remove this rport from the lport's list - memory is owned by the
199 * transport. Remove the ndlp reference for the NVME transport before
7a06dcd3 200 * calling state machine to remove the node.
01649561
JS
201 */
202 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
203 "6146 remoteport delete complete %p\n",
204 remoteport);
7a06dcd3 205 ndlp->nrport = NULL;
01649561
JS
206 lpfc_nlp_put(ndlp);
207
208 rport_err:
209 /* This call has to execute as long as the rport is valid.
210 * Release any threads waiting for the unreg to complete.
211 */
212 complete(&rport->rport_unreg_done);
213}
214
215static void
216lpfc_nvme_cmpl_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
217 struct lpfc_wcqe_complete *wcqe)
218{
219 struct lpfc_vport *vport = cmdwqe->vport;
220 uint32_t status;
221 struct nvmefc_ls_req *pnvme_lsreq;
222 struct lpfc_dmabuf *buf_ptr;
223 struct lpfc_nodelist *ndlp;
224
2cee7808 225 atomic_inc(&vport->phba->fc4NvmeLsCmpls);
01649561
JS
226
227 pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
228 status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
229 ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
230 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
231 "6047 nvme cmpl Enter "
232 "Data %p DID %x Xri: %x status %x cmd:%p lsreg:%p "
233 "bmp:%p ndlp:%p\n",
234 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
235 cmdwqe->sli4_xritag, status,
236 cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
237
bd2cdd5e
JS
238 lpfc_nvmeio_data(phba, "NVME LS CMPL: xri x%x stat x%x parm x%x\n",
239 cmdwqe->sli4_xritag, status, wcqe->parameter);
240
01649561
JS
241 if (cmdwqe->context3) {
242 buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
243 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
244 kfree(buf_ptr);
245 cmdwqe->context3 = NULL;
246 }
247 if (pnvme_lsreq->done)
248 pnvme_lsreq->done(pnvme_lsreq, status);
249 else
250 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
251 "6046 nvme cmpl without done call back? "
252 "Data %p DID %x Xri: %x status %x\n",
253 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
254 cmdwqe->sli4_xritag, status);
255 if (ndlp) {
256 lpfc_nlp_put(ndlp);
257 cmdwqe->context1 = NULL;
258 }
259 lpfc_sli_release_iocbq(phba, cmdwqe);
260}
261
262static int
263lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
264 struct lpfc_dmabuf *inp,
265 struct nvmefc_ls_req *pnvme_lsreq,
266 void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
267 struct lpfc_wcqe_complete *),
268 struct lpfc_nodelist *ndlp, uint32_t num_entry,
269 uint32_t tmo, uint8_t retry)
270{
271 struct lpfc_hba *phba = vport->phba;
272 union lpfc_wqe *wqe;
273 struct lpfc_iocbq *genwqe;
274 struct ulp_bde64 *bpl;
275 struct ulp_bde64 bde;
276 int i, rc, xmit_len, first_len;
277
278 /* Allocate buffer for command WQE */
279 genwqe = lpfc_sli_get_iocbq(phba);
280 if (genwqe == NULL)
281 return 1;
282
283 wqe = &genwqe->wqe;
284 memset(wqe, 0, sizeof(union lpfc_wqe));
285
286 genwqe->context3 = (uint8_t *)bmp;
287 genwqe->iocb_flag |= LPFC_IO_NVME_LS;
288
289 /* Save for completion so we can release these resources */
290 genwqe->context1 = lpfc_nlp_get(ndlp);
291 genwqe->context2 = (uint8_t *)pnvme_lsreq;
292 /* Fill in payload, bp points to frame payload */
293
294 if (!tmo)
295 /* FC spec states we need 3 * ratov for CT requests */
296 tmo = (3 * phba->fc_ratov);
297
298 /* For this command calculate the xmit length of the request bde. */
299 xmit_len = 0;
300 first_len = 0;
301 bpl = (struct ulp_bde64 *)bmp->virt;
302 for (i = 0; i < num_entry; i++) {
303 bde.tus.w = bpl[i].tus.w;
304 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
305 break;
306 xmit_len += bde.tus.f.bdeSize;
307 if (i == 0)
308 first_len = xmit_len;
309 }
310
311 genwqe->rsvd2 = num_entry;
312 genwqe->hba_wqidx = 0;
313
314 /* Words 0 - 2 */
315 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
316 wqe->generic.bde.tus.f.bdeSize = first_len;
317 wqe->generic.bde.addrLow = bpl[0].addrLow;
318 wqe->generic.bde.addrHigh = bpl[0].addrHigh;
319
320 /* Word 3 */
321 wqe->gen_req.request_payload_len = first_len;
322
323 /* Word 4 */
324
325 /* Word 5 */
326 bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
327 bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
328 bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
8b361639 329 bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
01649561
JS
330 bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
331
332 /* Word 6 */
333 bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
334 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
335 bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
336
337 /* Word 7 */
338 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
339 bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
340 bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
341 bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
342
343 /* Word 8 */
344 wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
345
346 /* Word 9 */
347 bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
348
349 /* Word 10 */
350 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
351 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
352 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
353 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
354 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
355
356 /* Word 11 */
357 bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
358 bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
359
360
361 /* Issue GEN REQ WQE for NPORT <did> */
362 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
363 "6050 Issue GEN REQ WQE to NPORT x%x "
364 "Data: x%x x%x wq:%p lsreq:%p bmp:%p xmit:%d 1st:%d\n",
365 ndlp->nlp_DID, genwqe->iotag,
366 vport->port_state,
367 genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
368 genwqe->wqe_cmpl = cmpl;
369 genwqe->iocb_cmpl = NULL;
370 genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
371 genwqe->vport = vport;
372 genwqe->retry = retry;
373
bd2cdd5e
JS
374 lpfc_nvmeio_data(phba, "NVME LS XMIT: xri x%x iotag x%x to x%06x\n",
375 genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
376
01649561 377 rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, genwqe);
cd22d605 378 if (rc) {
01649561
JS
379 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
380 "6045 Issue GEN REQ WQE to NPORT x%x "
381 "Data: x%x x%x\n",
382 ndlp->nlp_DID, genwqe->iotag,
383 vport->port_state);
384 lpfc_sli_release_iocbq(phba, genwqe);
385 return 1;
386 }
387 return 0;
388}
389
390/**
391 * lpfc_nvme_ls_req - Issue an Link Service request
392 * @lpfc_pnvme: Pointer to the driver's nvme instance data
393 * @lpfc_nvme_lport: Pointer to the driver's local port data
394 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
395 *
396 * Driver registers this routine to handle any link service request
397 * from the nvme_fc transport to a remote nvme-aware port.
398 *
399 * Return value :
400 * 0 - Success
401 * TODO: What are the failure codes.
402 **/
403static int
404lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
405 struct nvme_fc_remote_port *pnvme_rport,
406 struct nvmefc_ls_req *pnvme_lsreq)
407{
408 int ret = 0;
409 struct lpfc_nvme_lport *lport;
410 struct lpfc_vport *vport;
411 struct lpfc_nodelist *ndlp;
412 struct ulp_bde64 *bpl;
413 struct lpfc_dmabuf *bmp;
ba43c4d0 414 uint16_t ntype, nstate;
01649561
JS
415
416 /* there are two dma buf in the request, actually there is one and
417 * the second one is just the start address + cmd size.
418 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
419 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
420 * because the nvem layer owns the data bufs.
421 * We do not have to break these packets open, we don't care what is in
422 * them. And we do not have to look at the resonse data, we only care
423 * that we got a response. All of the caring is going to happen in the
424 * nvme-fc layer.
425 */
426
427 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
428 vport = lport->vport;
429
3386f4bd
JS
430 if (vport->load_flag & FC_UNLOADING)
431 return -ENODEV;
432
1abcb371
DK
433 if (vport->load_flag & FC_UNLOADING)
434 return -ENODEV;
435
01649561 436 ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
ba43c4d0
JS
437 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
438 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
439 "6051 DID x%06x not an active rport.\n",
01649561 440 pnvme_rport->port_id);
ba43c4d0
JS
441 return -ENODEV;
442 }
443
444 /* The remote node has to be a mapped nvme target or an
445 * unmapped nvme initiator or it's an error.
446 */
447 ntype = ndlp->nlp_type;
448 nstate = ndlp->nlp_state;
449 if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
450 (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
451 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
452 "6088 DID x%06x not ready for "
453 "IO. State x%x, Type x%x\n",
454 pnvme_rport->port_id,
455 ndlp->nlp_state, ndlp->nlp_type);
456 return -ENODEV;
01649561
JS
457 }
458 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
459 if (!bmp) {
460
461 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
462 "6044 Could not find node for DID %x\n",
463 pnvme_rport->port_id);
464 return 2;
465 }
466 INIT_LIST_HEAD(&bmp->list);
467 bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
468 if (!bmp->virt) {
469 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
470 "6042 Could not find node for DID %x\n",
471 pnvme_rport->port_id);
472 kfree(bmp);
473 return 3;
474 }
475 bpl = (struct ulp_bde64 *)bmp->virt;
476 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
477 bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
478 bpl->tus.f.bdeFlags = 0;
479 bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
480 bpl->tus.w = le32_to_cpu(bpl->tus.w);
481 bpl++;
482
483 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
484 bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
485 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
486 bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
487 bpl->tus.w = le32_to_cpu(bpl->tus.w);
488
489 /* Expand print to include key fields. */
490 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
ba43c4d0 491 "6149 ENTER. lport %p, rport %p lsreq%p rqstlen:%d "
825c6abb 492 "rsplen:%d %pad %pad\n",
01649561
JS
493 pnvme_lport, pnvme_rport,
494 pnvme_lsreq, pnvme_lsreq->rqstlen,
825c6abb
AB
495 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
496 &pnvme_lsreq->rspdma);
01649561 497
2cee7808 498 atomic_inc(&vport->phba->fc4NvmeLsRequests);
01649561
JS
499
500 /* Hardcode the wait to 30 seconds. Connections are failing otherwise.
501 * This code allows it all to work.
502 */
503 ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
504 pnvme_lsreq, lpfc_nvme_cmpl_gen_req,
505 ndlp, 2, 30, 0);
506 if (ret != WQE_SUCCESS) {
507 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
508 "6052 EXIT. issue ls wqe failed lport %p, "
509 "rport %p lsreq%p Status %x DID %x\n",
510 pnvme_lport, pnvme_rport, pnvme_lsreq,
511 ret, ndlp->nlp_DID);
512 lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
513 kfree(bmp);
514 return ret;
515 }
516
517 /* Stub in routine and return 0 for now. */
518 return ret;
519}
520
521/**
522 * lpfc_nvme_ls_abort - Issue an Link Service request
523 * @lpfc_pnvme: Pointer to the driver's nvme instance data
524 * @lpfc_nvme_lport: Pointer to the driver's local port data
525 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
526 *
527 * Driver registers this routine to handle any link service request
528 * from the nvme_fc transport to a remote nvme-aware port.
529 *
530 * Return value :
531 * 0 - Success
532 * TODO: What are the failure codes.
533 **/
534static void
535lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
536 struct nvme_fc_remote_port *pnvme_rport,
537 struct nvmefc_ls_req *pnvme_lsreq)
538{
539 struct lpfc_nvme_lport *lport;
540 struct lpfc_vport *vport;
541 struct lpfc_hba *phba;
542 struct lpfc_nodelist *ndlp;
543 LIST_HEAD(abort_list);
544 struct lpfc_sli_ring *pring;
545 struct lpfc_iocbq *wqe, *next_wqe;
546
547 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
548 vport = lport->vport;
549 phba = vport->phba;
550
3386f4bd
JS
551 if (vport->load_flag & FC_UNLOADING)
552 return;
553
01649561
JS
554 ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
555 if (!ndlp) {
556 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
557 "6049 Could not find node for DID %x\n",
558 pnvme_rport->port_id);
559 return;
560 }
561
562 /* Expand print to include key fields. */
563 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
564 "6040 ENTER. lport %p, rport %p lsreq %p rqstlen:%d "
825c6abb 565 "rsplen:%d %pad %pad\n",
01649561
JS
566 pnvme_lport, pnvme_rport,
567 pnvme_lsreq, pnvme_lsreq->rqstlen,
825c6abb
AB
568 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
569 &pnvme_lsreq->rspdma);
01649561
JS
570
571 /*
572 * Lock the ELS ring txcmplq and build a local list of all ELS IOs
573 * that need an ABTS. The IOs need to stay on the txcmplq so that
574 * the abort operation completes them successfully.
575 */
576 pring = phba->sli4_hba.nvmels_wq->pring;
577 spin_lock_irq(&phba->hbalock);
578 spin_lock(&pring->ring_lock);
579 list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
580 /* Add to abort_list on on NDLP match. */
581 if (lpfc_check_sli_ndlp(phba, pring, wqe, ndlp)) {
582 wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
583 list_add_tail(&wqe->dlist, &abort_list);
584 }
585 }
586 spin_unlock(&pring->ring_lock);
587 spin_unlock_irq(&phba->hbalock);
588
589 /* Abort the targeted IOs and remove them from the abort list. */
590 list_for_each_entry_safe(wqe, next_wqe, &abort_list, dlist) {
591 spin_lock_irq(&phba->hbalock);
592 list_del_init(&wqe->dlist);
593 lpfc_sli_issue_abort_iotag(phba, pring, wqe);
594 spin_unlock_irq(&phba->hbalock);
595 }
596}
597
598/* Fix up the existing sgls for NVME IO. */
599static void
600lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
601 struct lpfc_nvme_buf *lpfc_ncmd,
602 struct nvmefc_fcp_req *nCmd)
603{
604 struct sli4_sge *sgl;
605 union lpfc_wqe128 *wqe;
606 uint32_t *wptr, *dptr;
607
608 /*
609 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
610 * match NVME. NVME sends 96 bytes. Also, use the
611 * nvme commands command and response dma addresses
612 * rather than the virtual memory to ease the restore
613 * operation.
614 */
615 sgl = lpfc_ncmd->nvme_sgl;
616 sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
617
618 sgl++;
619
620 /* Setup the physical region for the FCP RSP */
621 sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
622 sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
623 sgl->word2 = le32_to_cpu(sgl->word2);
624 if (nCmd->sg_cnt)
625 bf_set(lpfc_sli4_sge_last, sgl, 0);
626 else
627 bf_set(lpfc_sli4_sge_last, sgl, 1);
628 sgl->word2 = cpu_to_le32(sgl->word2);
629 sgl->sge_len = cpu_to_le32(nCmd->rsplen);
630
631 /*
632 * Get a local pointer to the built-in wqe and correct
633 * the cmd size to match NVME's 96 bytes and fix
634 * the dma address.
635 */
636
637 /* 128 byte wqe support here */
638 wqe = (union lpfc_wqe128 *)&lpfc_ncmd->cur_iocbq.wqe;
639
640 /* Word 0-2 - NVME CMND IU (embedded payload) */
641 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
642 wqe->generic.bde.tus.f.bdeSize = 60;
643 wqe->generic.bde.addrHigh = 0;
644 wqe->generic.bde.addrLow = 64; /* Word 16 */
645
646 /* Word 3 */
647 bf_set(payload_offset_len, &wqe->fcp_icmd,
648 (nCmd->rsplen + nCmd->cmdlen));
649
650 /* Word 10 */
651 bf_set(wqe_nvme, &wqe->fcp_icmd.wqe_com, 1);
652 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
653
654 /*
655 * Embed the payload in the last half of the WQE
656 * WQE words 16-30 get the NVME CMD IU payload
657 *
b06a622f 658 * WQE words 16-19 get payload Words 1-4
01649561
JS
659 * WQE words 20-21 get payload Words 6-7
660 * WQE words 22-29 get payload Words 16-23
661 */
b06a622f 662 wptr = &wqe->words[16]; /* WQE ptr */
01649561 663 dptr = (uint32_t *)nCmd->cmdaddr; /* payload ptr */
b06a622f 664 dptr++; /* Skip Word 0 in payload */
01649561 665
b06a622f 666 *wptr++ = *dptr++; /* Word 1 */
01649561
JS
667 *wptr++ = *dptr++; /* Word 2 */
668 *wptr++ = *dptr++; /* Word 3 */
669 *wptr++ = *dptr++; /* Word 4 */
670 dptr++; /* Skip Word 5 in payload */
671 *wptr++ = *dptr++; /* Word 6 */
672 *wptr++ = *dptr++; /* Word 7 */
673 dptr += 8; /* Skip Words 8-15 in payload */
674 *wptr++ = *dptr++; /* Word 16 */
675 *wptr++ = *dptr++; /* Word 17 */
676 *wptr++ = *dptr++; /* Word 18 */
677 *wptr++ = *dptr++; /* Word 19 */
678 *wptr++ = *dptr++; /* Word 20 */
679 *wptr++ = *dptr++; /* Word 21 */
680 *wptr++ = *dptr++; /* Word 22 */
681 *wptr = *dptr; /* Word 23 */
682}
683
bd2cdd5e
JS
684#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
685static void
686lpfc_nvme_ktime(struct lpfc_hba *phba,
687 struct lpfc_nvme_buf *lpfc_ncmd)
688{
689 uint64_t seg1, seg2, seg3, seg4;
c8a4ce0b 690 uint64_t segsum;
bd2cdd5e 691
bd2cdd5e
JS
692 if (!lpfc_ncmd->ts_last_cmd ||
693 !lpfc_ncmd->ts_cmd_start ||
694 !lpfc_ncmd->ts_cmd_wqput ||
695 !lpfc_ncmd->ts_isr_cmpl ||
696 !lpfc_ncmd->ts_data_nvme)
697 return;
c8a4ce0b
DK
698
699 if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_cmd_start)
700 return;
bd2cdd5e
JS
701 if (lpfc_ncmd->ts_cmd_start < lpfc_ncmd->ts_last_cmd)
702 return;
703 if (lpfc_ncmd->ts_cmd_wqput < lpfc_ncmd->ts_cmd_start)
704 return;
705 if (lpfc_ncmd->ts_isr_cmpl < lpfc_ncmd->ts_cmd_wqput)
706 return;
707 if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_isr_cmpl)
708 return;
709 /*
710 * Segment 1 - Time from Last FCP command cmpl is handed
711 * off to NVME Layer to start of next command.
712 * Segment 2 - Time from Driver receives a IO cmd start
713 * from NVME Layer to WQ put is done on IO cmd.
714 * Segment 3 - Time from Driver WQ put is done on IO cmd
715 * to MSI-X ISR for IO cmpl.
716 * Segment 4 - Time from MSI-X ISR for IO cmpl to when
717 * cmpl is handled off to the NVME Layer.
718 */
719 seg1 = lpfc_ncmd->ts_cmd_start - lpfc_ncmd->ts_last_cmd;
c8a4ce0b
DK
720 if (seg1 > 5000000) /* 5 ms - for sequential IOs only */
721 seg1 = 0;
bd2cdd5e
JS
722
723 /* Calculate times relative to start of IO */
724 seg2 = (lpfc_ncmd->ts_cmd_wqput - lpfc_ncmd->ts_cmd_start);
c8a4ce0b
DK
725 segsum = seg2;
726 seg3 = lpfc_ncmd->ts_isr_cmpl - lpfc_ncmd->ts_cmd_start;
727 if (segsum > seg3)
728 return;
729 seg3 -= segsum;
730 segsum += seg3;
731
732 seg4 = lpfc_ncmd->ts_data_nvme - lpfc_ncmd->ts_cmd_start;
733 if (segsum > seg4)
734 return;
735 seg4 -= segsum;
736
bd2cdd5e
JS
737 phba->ktime_data_samples++;
738 phba->ktime_seg1_total += seg1;
739 if (seg1 < phba->ktime_seg1_min)
740 phba->ktime_seg1_min = seg1;
741 else if (seg1 > phba->ktime_seg1_max)
742 phba->ktime_seg1_max = seg1;
743 phba->ktime_seg2_total += seg2;
744 if (seg2 < phba->ktime_seg2_min)
745 phba->ktime_seg2_min = seg2;
746 else if (seg2 > phba->ktime_seg2_max)
747 phba->ktime_seg2_max = seg2;
748 phba->ktime_seg3_total += seg3;
749 if (seg3 < phba->ktime_seg3_min)
750 phba->ktime_seg3_min = seg3;
751 else if (seg3 > phba->ktime_seg3_max)
752 phba->ktime_seg3_max = seg3;
753 phba->ktime_seg4_total += seg4;
754 if (seg4 < phba->ktime_seg4_min)
755 phba->ktime_seg4_min = seg4;
756 else if (seg4 > phba->ktime_seg4_max)
757 phba->ktime_seg4_max = seg4;
758
759 lpfc_ncmd->ts_last_cmd = 0;
760 lpfc_ncmd->ts_cmd_start = 0;
761 lpfc_ncmd->ts_cmd_wqput = 0;
762 lpfc_ncmd->ts_isr_cmpl = 0;
763 lpfc_ncmd->ts_data_nvme = 0;
764}
765#endif
766
01649561
JS
767/**
768 * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
769 * @lpfc_pnvme: Pointer to the driver's nvme instance data
770 * @lpfc_nvme_lport: Pointer to the driver's local port data
771 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
772 *
773 * Driver registers this routine as it io request handler. This
774 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
775 * data structure to the rport indicated in @lpfc_nvme_rport.
776 *
777 * Return value :
778 * 0 - Success
779 * TODO: What are the failure codes.
780 **/
781static void
782lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
783 struct lpfc_wcqe_complete *wcqe)
784{
785 struct lpfc_nvme_buf *lpfc_ncmd =
786 (struct lpfc_nvme_buf *)pwqeIn->context1;
787 struct lpfc_vport *vport = pwqeIn->vport;
788 struct nvmefc_fcp_req *nCmd;
789 struct nvme_fc_ersp_iu *ep;
790 struct nvme_fc_cmd_iu *cp;
791 struct lpfc_nvme_rport *rport;
792 struct lpfc_nodelist *ndlp;
bbe3012b 793 struct lpfc_nvme_fcpreq_priv *freqpriv;
01649561
JS
794 unsigned long flags;
795 uint32_t code;
796 uint16_t cid, sqhd, data;
797 uint32_t *ptr;
798
799 /* Sanity check on return of outstanding command */
800 if (!lpfc_ncmd || !lpfc_ncmd->nvmeCmd || !lpfc_ncmd->nrport) {
801 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
802 "6071 Completion pointers bad on wqe %p.\n",
803 wcqe);
804 return;
805 }
2cee7808 806 atomic_inc(&phba->fc4NvmeIoCmpls);
01649561
JS
807
808 nCmd = lpfc_ncmd->nvmeCmd;
809 rport = lpfc_ncmd->nrport;
810
bd2cdd5e
JS
811 lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
812 lpfc_ncmd->cur_iocbq.sli4_xritag,
813 bf_get(lpfc_wcqe_c_status, wcqe), wcqe->parameter);
01649561
JS
814 /*
815 * Catch race where our node has transitioned, but the
816 * transport is still transitioning.
817 */
818 ndlp = rport->ndlp;
819 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
820 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
c154e750
JS
821 "6061 rport %p, DID x%06x node not ready.\n",
822 rport, rport->remoteport->port_id);
01649561
JS
823
824 ndlp = lpfc_findnode_did(vport, rport->remoteport->port_id);
825 if (!ndlp) {
826 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
827 "6062 Ignoring NVME cmpl. No ndlp\n");
828 goto out_err;
829 }
830 }
831
832 code = bf_get(lpfc_wcqe_c_code, wcqe);
833 if (code == CQE_CODE_NVME_ERSP) {
834 /* For this type of CQE, we need to rebuild the rsp */
835 ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
836
837 /*
838 * Get Command Id from cmd to plug into response. This
839 * code is not needed in the next NVME Transport drop.
840 */
841 cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
842 cid = cp->sqe.common.command_id;
843
844 /*
845 * RSN is in CQE word 2
846 * SQHD is in CQE Word 3 bits 15:0
847 * Cmd Specific info is in CQE Word 1
848 * and in CQE Word 0 bits 15:0
849 */
850 sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
851
852 /* Now lets build the NVME ERSP IU */
853 ep->iu_len = cpu_to_be16(8);
854 ep->rsn = wcqe->parameter;
855 ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
856 ep->rsvd12 = 0;
857 ptr = (uint32_t *)&ep->cqe.result.u64;
858 *ptr++ = wcqe->total_data_placed;
859 data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
860 *ptr = (uint32_t)data;
861 ep->cqe.sq_head = sqhd;
862 ep->cqe.sq_id = nCmd->sqid;
863 ep->cqe.command_id = cid;
864 ep->cqe.status = 0;
865
866 lpfc_ncmd->status = IOSTAT_SUCCESS;
867 lpfc_ncmd->result = 0;
868 nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
869 nCmd->transferred_length = nCmd->payload_length;
870 } else {
871 lpfc_ncmd->status = (bf_get(lpfc_wcqe_c_status, wcqe) &
872 LPFC_IOCB_STATUS_MASK);
952c303b 873 lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
01649561
JS
874
875 /* For NVME, the only failure path that results in an
876 * IO error is when the adapter rejects it. All other
877 * conditions are a success case and resolved by the
878 * transport.
879 * IOSTAT_FCP_RSP_ERROR means:
880 * 1. Length of data received doesn't match total
881 * transfer length in WQE
882 * 2. If the RSP payload does NOT match these cases:
883 * a. RSP length 12/24 bytes and all zeros
884 * b. NVME ERSP
885 */
886 switch (lpfc_ncmd->status) {
887 case IOSTAT_SUCCESS:
888 nCmd->transferred_length = wcqe->total_data_placed;
889 nCmd->rcv_rsplen = 0;
890 nCmd->status = 0;
891 break;
892 case IOSTAT_FCP_RSP_ERROR:
893 nCmd->transferred_length = wcqe->total_data_placed;
894 nCmd->rcv_rsplen = wcqe->parameter;
895 nCmd->status = 0;
896 /* Sanity check */
897 if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
898 break;
899 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
900 "6081 NVME Completion Protocol Error: "
86c67379
JS
901 "xri %x status x%x result x%x "
902 "placed x%x\n",
903 lpfc_ncmd->cur_iocbq.sli4_xritag,
01649561
JS
904 lpfc_ncmd->status, lpfc_ncmd->result,
905 wcqe->total_data_placed);
906 break;
952c303b
DK
907 case IOSTAT_LOCAL_REJECT:
908 /* Let fall through to set command final state. */
909 if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
910 lpfc_printf_vlog(vport, KERN_INFO,
911 LOG_NVME_IOERR,
912 "6032 Delay Aborted cmd %p "
913 "nvme cmd %p, xri x%x, "
914 "xb %d\n",
915 lpfc_ncmd, nCmd,
916 lpfc_ncmd->cur_iocbq.sli4_xritag,
917 bf_get(lpfc_wcqe_c_xb, wcqe));
01649561
JS
918 default:
919out_err:
e3246a12 920 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
86c67379 921 "6072 NVME Completion Error: xri %x "
01649561 922 "status x%x result x%x placed x%x\n",
86c67379 923 lpfc_ncmd->cur_iocbq.sli4_xritag,
01649561
JS
924 lpfc_ncmd->status, lpfc_ncmd->result,
925 wcqe->total_data_placed);
926 nCmd->transferred_length = 0;
927 nCmd->rcv_rsplen = 0;
8e009ce8 928 nCmd->status = NVME_SC_INTERNAL;
01649561
JS
929 }
930 }
931
932 /* pick up SLI4 exhange busy condition */
933 if (bf_get(lpfc_wcqe_c_xb, wcqe))
934 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
935 else
936 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
937
938 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
939 atomic_dec(&ndlp->cmd_pending);
940
941 /* Update stats and complete the IO. There is
942 * no need for dma unprep because the nvme_transport
943 * owns the dma address.
944 */
bd2cdd5e 945#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
c8a4ce0b 946 if (lpfc_ncmd->ts_cmd_start) {
bd2cdd5e
JS
947 lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
948 lpfc_ncmd->ts_data_nvme = ktime_get_ns();
949 phba->ktime_last_cmd = lpfc_ncmd->ts_data_nvme;
950 lpfc_nvme_ktime(phba, lpfc_ncmd);
951 }
952 if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
953 if (lpfc_ncmd->cpu != smp_processor_id())
954 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
955 "6701 CPU Check cmpl: "
956 "cpu %d expect %d\n",
957 smp_processor_id(), lpfc_ncmd->cpu);
958 if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
959 phba->cpucheck_cmpl_io[lpfc_ncmd->cpu]++;
960 }
961#endif
bbe3012b
JS
962 freqpriv = nCmd->private;
963 freqpriv->nvme_buf = NULL;
952c303b
DK
964
965 /* NVME targets need completion held off until the abort exchange
add9d6be 966 * completes unless the NVME Rport is getting unregistered.
952c303b 967 */
add9d6be
JS
968 if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY) ||
969 ndlp->upcall_flags & NLP_WAIT_FOR_UNREG) {
970 /* Clear the XBUSY flag to prevent double completions.
971 * The nvme rport is getting unregistered and there is
972 * no need to defer the IO.
973 */
974 if (lpfc_ncmd->flags & LPFC_SBUF_XBUSY)
975 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
976
952c303b 977 nCmd->done(nCmd);
add9d6be 978 }
01649561
JS
979
980 spin_lock_irqsave(&phba->hbalock, flags);
981 lpfc_ncmd->nrport = NULL;
982 spin_unlock_irqrestore(&phba->hbalock, flags);
983
952c303b 984 /* Call release with XB=1 to queue the IO into the abort list. */
01649561
JS
985 lpfc_release_nvme_buf(phba, lpfc_ncmd);
986}
987
988
989/**
990 * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
991 * @lpfc_pnvme: Pointer to the driver's nvme instance data
992 * @lpfc_nvme_lport: Pointer to the driver's local port data
993 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
994 * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
995 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
996 *
997 * Driver registers this routine as it io request handler. This
998 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
999 * data structure to the rport indicated in @lpfc_nvme_rport.
1000 *
1001 * Return value :
1002 * 0 - Success
1003 * TODO: What are the failure codes.
1004 **/
1005static int
1006lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
1007 struct lpfc_nvme_buf *lpfc_ncmd,
1008 struct lpfc_nodelist *pnode)
1009{
1010 struct lpfc_hba *phba = vport->phba;
1011 struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1012 struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
1013 union lpfc_wqe128 *wqe = (union lpfc_wqe128 *)&pwqeq->wqe;
1014 uint32_t req_len;
1015
1016 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
1017 return -EINVAL;
1018
1019 /*
1020 * There are three possibilities here - use scatter-gather segment, use
1021 * the single mapping, or neither.
1022 */
1023 wqe->fcp_iwrite.initial_xfer_len = 0;
1024 if (nCmd->sg_cnt) {
1025 if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
1026 /* Word 5 */
1027 if ((phba->cfg_nvme_enable_fb) &&
1028 (pnode->nlp_flag & NLP_FIRSTBURST)) {
1029 req_len = lpfc_ncmd->nvmeCmd->payload_length;
1030 if (req_len < pnode->nvme_fb_size)
1031 wqe->fcp_iwrite.initial_xfer_len =
1032 req_len;
1033 else
1034 wqe->fcp_iwrite.initial_xfer_len =
1035 pnode->nvme_fb_size;
1036 }
1037
1038 /* Word 7 */
1039 bf_set(wqe_cmnd, &wqe->generic.wqe_com,
1040 CMD_FCP_IWRITE64_WQE);
1041 bf_set(wqe_pu, &wqe->generic.wqe_com,
1042 PARM_READ_CHECK);
1043
1044 /* Word 10 */
1045 bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
1046 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com,
1047 LPFC_WQE_IOD_WRITE);
1048 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
1049 LPFC_WQE_LENLOC_WORD4);
1050 if (phba->cfg_nvme_oas)
1051 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
1052
1053 /* Word 11 */
1054 bf_set(wqe_cmd_type, &wqe->generic.wqe_com,
1055 NVME_WRITE_CMD);
1056
2cee7808 1057 atomic_inc(&phba->fc4NvmeOutputRequests);
01649561
JS
1058 } else {
1059 /* Word 7 */
1060 bf_set(wqe_cmnd, &wqe->generic.wqe_com,
1061 CMD_FCP_IREAD64_WQE);
1062 bf_set(wqe_pu, &wqe->generic.wqe_com,
1063 PARM_READ_CHECK);
1064
1065 /* Word 10 */
1066 bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
1067 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com,
1068 LPFC_WQE_IOD_READ);
1069 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
1070 LPFC_WQE_LENLOC_WORD4);
1071 if (phba->cfg_nvme_oas)
1072 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
1073
1074 /* Word 11 */
1075 bf_set(wqe_cmd_type, &wqe->generic.wqe_com,
1076 NVME_READ_CMD);
1077
2cee7808 1078 atomic_inc(&phba->fc4NvmeInputRequests);
01649561
JS
1079 }
1080 } else {
1081 /* Word 4 */
1082 wqe->fcp_icmd.rsrvd4 = 0;
1083
1084 /* Word 7 */
1085 bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_FCP_ICMND64_WQE);
1086 bf_set(wqe_pu, &wqe->generic.wqe_com, 0);
1087
1088 /* Word 10 */
1089 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
1090 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
1091 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
1092 LPFC_WQE_LENLOC_NONE);
1093 if (phba->cfg_nvme_oas)
1094 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
1095
1096 /* Word 11 */
1097 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, NVME_READ_CMD);
1098
2cee7808 1099 atomic_inc(&phba->fc4NvmeControlRequests);
01649561
JS
1100 }
1101 /*
1102 * Finish initializing those WQE fields that are independent
1103 * of the nvme_cmnd request_buffer
1104 */
1105
1106 /* Word 6 */
1107 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
1108 phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
1109 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
1110
1111 /* Word 7 */
1112 /* Preserve Class data in the ndlp. */
1113 bf_set(wqe_class, &wqe->generic.wqe_com,
1114 (pnode->nlp_fcp_info & 0x0f));
1115
1116 /* Word 8 */
1117 wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
1118
1119 /* Word 9 */
1120 bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
1121
1122 /* Word 11 */
1123 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1124
1125 pwqeq->vport = vport;
1126 return 0;
1127}
1128
1129
1130/**
1131 * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
1132 * @lpfc_pnvme: Pointer to the driver's nvme instance data
1133 * @lpfc_nvme_lport: Pointer to the driver's local port data
1134 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1135 * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1136 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1137 *
1138 * Driver registers this routine as it io request handler. This
1139 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1140 * data structure to the rport indicated in @lpfc_nvme_rport.
1141 *
1142 * Return value :
1143 * 0 - Success
1144 * TODO: What are the failure codes.
1145 **/
1146static int
1147lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
1148 struct lpfc_nvme_buf *lpfc_ncmd)
1149{
1150 struct lpfc_hba *phba = vport->phba;
1151 struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1152 union lpfc_wqe128 *wqe = (union lpfc_wqe128 *)&lpfc_ncmd->cur_iocbq.wqe;
1153 struct sli4_sge *sgl = lpfc_ncmd->nvme_sgl;
1154 struct scatterlist *data_sg;
1155 struct sli4_sge *first_data_sgl;
1156 dma_addr_t physaddr;
1157 uint32_t num_bde = 0;
1158 uint32_t dma_len;
1159 uint32_t dma_offset = 0;
1160 int nseg, i;
1161
1162 /* Fix up the command and response DMA stuff. */
1163 lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
1164
1165 /*
1166 * There are three possibilities here - use scatter-gather segment, use
1167 * the single mapping, or neither.
1168 */
1169 if (nCmd->sg_cnt) {
1170 /*
1171 * Jump over the cmd and rsp SGEs. The fix routine
1172 * has already adjusted for this.
1173 */
1174 sgl += 2;
1175
1176 first_data_sgl = sgl;
1177 lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
81e6a637 1178 if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
01649561
JS
1179 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1180 "6058 Too many sg segments from "
1181 "NVME Transport. Max %d, "
1182 "nvmeIO sg_cnt %d\n",
29bfd55a 1183 phba->cfg_nvme_seg_cnt + 1,
01649561
JS
1184 lpfc_ncmd->seg_cnt);
1185 lpfc_ncmd->seg_cnt = 0;
1186 return 1;
1187 }
1188
1189 /*
1190 * The driver established a maximum scatter-gather segment count
1191 * during probe that limits the number of sg elements in any
1192 * single nvme command. Just run through the seg_cnt and format
1193 * the sge's.
1194 */
1195 nseg = nCmd->sg_cnt;
1196 data_sg = nCmd->first_sgl;
1197 for (i = 0; i < nseg; i++) {
1198 if (data_sg == NULL) {
1199 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1200 "6059 dptr err %d, nseg %d\n",
1201 i, nseg);
1202 lpfc_ncmd->seg_cnt = 0;
1203 return 1;
1204 }
1205 physaddr = data_sg->dma_address;
1206 dma_len = data_sg->length;
1207 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1208 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1209 sgl->word2 = le32_to_cpu(sgl->word2);
1210 if ((num_bde + 1) == nseg)
1211 bf_set(lpfc_sli4_sge_last, sgl, 1);
1212 else
1213 bf_set(lpfc_sli4_sge_last, sgl, 0);
1214 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1215 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
1216 sgl->word2 = cpu_to_le32(sgl->word2);
1217 sgl->sge_len = cpu_to_le32(dma_len);
1218
1219 dma_offset += dma_len;
1220 data_sg = sg_next(data_sg);
1221 sgl++;
1222 }
1223 } else {
1224 /* For this clause to be valid, the payload_length
1225 * and sg_cnt must zero.
1226 */
1227 if (nCmd->payload_length != 0) {
1228 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1229 "6063 NVME DMA Prep Err: sg_cnt %d "
1230 "payload_length x%x\n",
1231 nCmd->sg_cnt, nCmd->payload_length);
1232 return 1;
1233 }
1234 }
1235
1236 /*
1237 * Due to difference in data length between DIF/non-DIF paths,
1238 * we need to set word 4 of WQE here
1239 */
1240 wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
1241 return 0;
1242}
1243
1244/**
1245 * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
1246 * @lpfc_pnvme: Pointer to the driver's nvme instance data
1247 * @lpfc_nvme_lport: Pointer to the driver's local port data
1248 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1249 * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1250 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1251 *
1252 * Driver registers this routine as it io request handler. This
1253 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1254 * data structure to the rport
1255 indicated in @lpfc_nvme_rport.
1256 *
1257 * Return value :
1258 * 0 - Success
1259 * TODO: What are the failure codes.
1260 **/
1261static int
1262lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
1263 struct nvme_fc_remote_port *pnvme_rport,
1264 void *hw_queue_handle,
1265 struct nvmefc_fcp_req *pnvme_fcreq)
1266{
1267 int ret = 0;
1268 struct lpfc_nvme_lport *lport;
1269 struct lpfc_vport *vport;
1270 struct lpfc_hba *phba;
1271 struct lpfc_nodelist *ndlp;
1272 struct lpfc_nvme_buf *lpfc_ncmd;
1273 struct lpfc_nvme_rport *rport;
1274 struct lpfc_nvme_qhandle *lpfc_queue_info;
c3725bdc 1275 struct lpfc_nvme_fcpreq_priv *freqpriv;
bd2cdd5e
JS
1276#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1277 uint64_t start = 0;
1278#endif
01649561 1279
c3725bdc
JS
1280 /* Validate pointers. LLDD fault handling with transport does
1281 * have timing races.
1282 */
01649561 1283 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
c3725bdc
JS
1284 if (unlikely(!lport)) {
1285 ret = -EINVAL;
1286 goto out_fail;
1287 }
1288
01649561 1289 vport = lport->vport;
c3725bdc
JS
1290
1291 if (unlikely(!hw_queue_handle)) {
1292 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1293 "6129 Fail Abort, NULL hw_queue_handle\n");
1294 ret = -EINVAL;
1295 goto out_fail;
1296 }
1297
01649561
JS
1298 phba = vport->phba;
1299
1abcb371
DK
1300 if (vport->load_flag & FC_UNLOADING) {
1301 ret = -ENODEV;
1302 goto out_fail;
1303 }
1304
3386f4bd
JS
1305 if (vport->load_flag & FC_UNLOADING) {
1306 ret = -ENODEV;
1307 goto out_fail;
1308 }
1309
c3725bdc
JS
1310 freqpriv = pnvme_fcreq->private;
1311 if (unlikely(!freqpriv)) {
1312 ret = -EINVAL;
b7672ae6
DK
1313 goto out_fail;
1314 }
1315
bd2cdd5e
JS
1316#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1317 if (phba->ktime_on)
1318 start = ktime_get_ns();
1319#endif
01649561
JS
1320 rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
1321 lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
1322
1323 /*
1324 * Catch race where our node has transitioned, but the
1325 * transport is still transitioning.
1326 */
1327 ndlp = rport->ndlp;
1328 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1329 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
1330 "6053 rport %p, ndlp %p, DID x%06x "
1331 "ndlp not ready.\n",
1332 rport, ndlp, pnvme_rport->port_id);
1333
1334 ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
1335 if (!ndlp) {
1336 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1337 "6066 Missing node for DID %x\n",
1338 pnvme_rport->port_id);
1339 ret = -ENODEV;
1340 goto out_fail;
1341 }
1342 }
1343
1344 /* The remote node has to be a mapped target or it's an error. */
1345 if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
1346 (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
1347 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
1348 "6036 rport %p, DID x%06x not ready for "
1349 "IO. State x%x, Type x%x\n",
1350 rport, pnvme_rport->port_id,
1351 ndlp->nlp_state, ndlp->nlp_type);
1352 ret = -ENODEV;
1353 goto out_fail;
1354
1355 }
1356
1357 /* The node is shared with FCP IO, make sure the IO pending count does
1358 * not exceed the programmed depth.
1359 */
1360 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
cd22d605 1361 ret = -EBUSY;
01649561
JS
1362 goto out_fail;
1363 }
1364
1365 lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp);
1366 if (lpfc_ncmd == NULL) {
1367 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1368 "6065 driver's buffer pool is empty, "
1369 "IO failed\n");
cd22d605 1370 ret = -EBUSY;
01649561
JS
1371 goto out_fail;
1372 }
bd2cdd5e 1373#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
c8a4ce0b 1374 if (start) {
bd2cdd5e
JS
1375 lpfc_ncmd->ts_cmd_start = start;
1376 lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
c8a4ce0b
DK
1377 } else {
1378 lpfc_ncmd->ts_cmd_start = 0;
bd2cdd5e
JS
1379 }
1380#endif
01649561
JS
1381
1382 /*
1383 * Store the data needed by the driver to issue, abort, and complete
1384 * an IO.
1385 * Do not let the IO hang out forever. There is no midlayer issuing
1386 * an abort so inform the FW of the maximum IO pending time.
1387 */
bbe3012b 1388 freqpriv->nvme_buf = lpfc_ncmd;
01649561
JS
1389 lpfc_ncmd->nvmeCmd = pnvme_fcreq;
1390 lpfc_ncmd->nrport = rport;
318083ad 1391 lpfc_ncmd->ndlp = ndlp;
01649561
JS
1392 lpfc_ncmd->start_time = jiffies;
1393
1394 lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp);
1395 ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
1396 if (ret) {
1397 ret = -ENOMEM;
1398 goto out_free_nvme_buf;
1399 }
1400
1401 atomic_inc(&ndlp->cmd_pending);
1402
1403 /*
1404 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
1405 * This identfier was create in our hardware queue create callback
1406 * routine. The driver now is dependent on the IO queue steering from
1407 * the transport. We are trusting the upper NVME layers know which
1408 * index to use and that they have affinitized a CPU to this hardware
1409 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
1410 */
1411 lpfc_ncmd->cur_iocbq.hba_wqidx = lpfc_queue_info->index;
1412
bd2cdd5e
JS
1413 lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1414 lpfc_ncmd->cur_iocbq.sli4_xritag,
1415 lpfc_queue_info->index, ndlp->nlp_DID);
1416
01649561
JS
1417 ret = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, &lpfc_ncmd->cur_iocbq);
1418 if (ret) {
1419 atomic_dec(&ndlp->cmd_pending);
e3246a12 1420 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
01649561
JS
1421 "6113 FCP could not issue WQE err %x "
1422 "sid: x%x did: x%x oxid: x%x\n",
1423 ret, vport->fc_myDID, ndlp->nlp_DID,
1424 lpfc_ncmd->cur_iocbq.sli4_xritag);
01649561
JS
1425 goto out_free_nvme_buf;
1426 }
1427
bd2cdd5e 1428#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
c8a4ce0b 1429 if (lpfc_ncmd->ts_cmd_start)
bd2cdd5e
JS
1430 lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1431
1432 if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1433 lpfc_ncmd->cpu = smp_processor_id();
1434 if (lpfc_ncmd->cpu != lpfc_queue_info->index) {
1435 /* Check for admin queue */
1436 if (lpfc_queue_info->qidx) {
1437 lpfc_printf_vlog(vport,
1438 KERN_ERR, LOG_NVME_IOERR,
1439 "6702 CPU Check cmd: "
1440 "cpu %d wq %d\n",
1441 lpfc_ncmd->cpu,
1442 lpfc_queue_info->index);
1443 }
1444 lpfc_ncmd->cpu = lpfc_queue_info->index;
1445 }
1446 if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
1447 phba->cpucheck_xmt_io[lpfc_ncmd->cpu]++;
1448 }
1449#endif
01649561
JS
1450 return 0;
1451
1452 out_free_nvme_buf:
2cee7808
JS
1453 if (lpfc_ncmd->nvmeCmd->sg_cnt) {
1454 if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
1455 atomic_dec(&phba->fc4NvmeOutputRequests);
1456 else
1457 atomic_dec(&phba->fc4NvmeInputRequests);
1458 } else
1459 atomic_dec(&phba->fc4NvmeControlRequests);
01649561
JS
1460 lpfc_release_nvme_buf(phba, lpfc_ncmd);
1461 out_fail:
1462 return ret;
1463}
1464
1465/**
1466 * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
1467 * @phba: Pointer to HBA context object
1468 * @cmdiocb: Pointer to command iocb object.
1469 * @rspiocb: Pointer to response iocb object.
1470 *
1471 * This is the callback function for any NVME FCP IO that was aborted.
1472 *
1473 * Return value:
1474 * None
1475 **/
1476void
1477lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1478 struct lpfc_wcqe_complete *abts_cmpl)
1479{
e3246a12 1480 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
01649561
JS
1481 "6145 ABORT_XRI_CN completing on rpi x%x "
1482 "original iotag x%x, abort cmd iotag x%x "
1483 "req_tag x%x, status x%x, hwstatus x%x\n",
1484 cmdiocb->iocb.un.acxri.abortContextTag,
1485 cmdiocb->iocb.un.acxri.abortIoTag,
1486 cmdiocb->iotag,
1487 bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
1488 bf_get(lpfc_wcqe_c_status, abts_cmpl),
1489 bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
1490 lpfc_sli_release_iocbq(phba, cmdiocb);
1491}
1492
1493/**
1494 * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
1495 * @lpfc_pnvme: Pointer to the driver's nvme instance data
1496 * @lpfc_nvme_lport: Pointer to the driver's local port data
1497 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1498 * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1499 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1500 *
1501 * Driver registers this routine as its nvme request io abort handler. This
1502 * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
1503 * data structure to the rport indicated in @lpfc_nvme_rport. This routine
1504 * is executed asynchronously - one the target is validated as "MAPPED" and
1505 * ready for IO, the driver issues the abort request and returns.
1506 *
1507 * Return value:
1508 * None
1509 **/
1510static void
1511lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
1512 struct nvme_fc_remote_port *pnvme_rport,
1513 void *hw_queue_handle,
1514 struct nvmefc_fcp_req *pnvme_fcreq)
1515{
1516 struct lpfc_nvme_lport *lport;
1517 struct lpfc_vport *vport;
1518 struct lpfc_hba *phba;
01649561
JS
1519 struct lpfc_nvme_buf *lpfc_nbuf;
1520 struct lpfc_iocbq *abts_buf;
1521 struct lpfc_iocbq *nvmereq_wqe;
c3725bdc 1522 struct lpfc_nvme_fcpreq_priv *freqpriv;
01649561
JS
1523 union lpfc_wqe *abts_wqe;
1524 unsigned long flags;
1525 int ret_val;
1526
c3725bdc
JS
1527 /* Validate pointers. LLDD fault handling with transport does
1528 * have timing races.
1529 */
01649561 1530 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
c3725bdc
JS
1531 if (unlikely(!lport))
1532 return;
1533
01649561 1534 vport = lport->vport;
c3725bdc
JS
1535
1536 if (unlikely(!hw_queue_handle)) {
1537 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1538 "6129 Fail Abort, HW Queue Handle NULL.\n");
1539 return;
1540 }
1541
01649561 1542 phba = vport->phba;
c3725bdc 1543 freqpriv = pnvme_fcreq->private;
01649561 1544
c3725bdc
JS
1545 if (unlikely(!freqpriv))
1546 return;
3386f4bd
JS
1547 if (vport->load_flag & FC_UNLOADING)
1548 return;
1549
01649561 1550 /* Announce entry to new IO submit field. */
86c67379 1551 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
01649561
JS
1552 "6002 Abort Request to rport DID x%06x "
1553 "for nvme_fc_req %p\n",
1554 pnvme_rport->port_id,
1555 pnvme_fcreq);
1556
01649561
JS
1557 /* If the hba is getting reset, this flag is set. It is
1558 * cleared when the reset is complete and rings reestablished.
1559 */
1560 spin_lock_irqsave(&phba->hbalock, flags);
1561 /* driver queued commands are in process of being flushed */
1562 if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
1563 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1564 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561
JS
1565 "6139 Driver in reset cleanup - flushing "
1566 "NVME Req now. hba_flag x%x\n",
1567 phba->hba_flag);
1568 return;
1569 }
1570
bbe3012b 1571 lpfc_nbuf = freqpriv->nvme_buf;
01649561
JS
1572 if (!lpfc_nbuf) {
1573 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1574 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561
JS
1575 "6140 NVME IO req has no matching lpfc nvme "
1576 "io buffer. Skipping abort req.\n");
1577 return;
1578 } else if (!lpfc_nbuf->nvmeCmd) {
1579 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1580 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561
JS
1581 "6141 lpfc NVME IO req has no nvme_fcreq "
1582 "io buffer. Skipping abort req.\n");
1583 return;
1584 }
2b7824d0 1585 nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
01649561
JS
1586
1587 /*
1588 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
1589 * state must match the nvme_fcreq passed by the nvme
1590 * transport. If they don't match, it is likely the driver
1591 * has already completed the NVME IO and the nvme transport
1592 * has not seen it yet.
1593 */
1594 if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
1595 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1596 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561
JS
1597 "6143 NVME req mismatch: "
1598 "lpfc_nbuf %p nvmeCmd %p, "
2b7824d0 1599 "pnvme_fcreq %p. Skipping Abort xri x%x\n",
01649561 1600 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
2b7824d0 1601 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
01649561
JS
1602 return;
1603 }
1604
1605 /* Don't abort IOs no longer on the pending queue. */
01649561
JS
1606 if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
1607 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1608 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561 1609 "6142 NVME IO req %p not queued - skipping "
2b7824d0
JS
1610 "abort req xri x%x\n",
1611 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
01649561
JS
1612 return;
1613 }
1614
bd2cdd5e
JS
1615 lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1616 nvmereq_wqe->sli4_xritag,
00cefeb9 1617 nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
bd2cdd5e 1618
01649561
JS
1619 /* Outstanding abort is in progress */
1620 if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
1621 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1622 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561
JS
1623 "6144 Outstanding NVME I/O Abort Request "
1624 "still pending on nvme_fcreq %p, "
2b7824d0
JS
1625 "lpfc_ncmd %p xri x%x\n",
1626 pnvme_fcreq, lpfc_nbuf,
1627 nvmereq_wqe->sli4_xritag);
01649561
JS
1628 return;
1629 }
1630
1631 abts_buf = __lpfc_sli_get_iocbq(phba);
1632 if (!abts_buf) {
1633 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1634 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561 1635 "6136 No available abort wqes. Skipping "
2b7824d0
JS
1636 "Abts req for nvme_fcreq %p xri x%x\n",
1637 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
01649561
JS
1638 return;
1639 }
1640
1641 /* Ready - mark outstanding as aborted by driver. */
1642 nvmereq_wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
1643
1644 /* Complete prepping the abort wqe and issue to the FW. */
1645 abts_wqe = &abts_buf->wqe;
1646
1647 /* WQEs are reused. Clear stale data and set key fields to
1648 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
1649 */
1650 memset(abts_wqe, 0, sizeof(union lpfc_wqe));
1651 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
1652
1653 /* word 7 */
1654 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
1655 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
1656 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
1657 nvmereq_wqe->iocb.ulpClass);
1658
1659 /* word 8 - tell the FW to abort the IO associated with this
1660 * outstanding exchange ID.
1661 */
1662 abts_wqe->abort_cmd.wqe_com.abort_tag = nvmereq_wqe->sli4_xritag;
1663
1664 /* word 9 - this is the iotag for the abts_wqe completion. */
1665 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
1666 abts_buf->iotag);
1667
1668 /* word 10 */
1669 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, nvmereq_wqe->hba_wqidx);
1670 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
1671 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
1672
1673 /* word 11 */
1674 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
1675 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
1676 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1677
1678 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
1679 abts_buf->iocb_flag |= LPFC_IO_NVME;
1680 abts_buf->hba_wqidx = nvmereq_wqe->hba_wqidx;
1681 abts_buf->vport = vport;
1682 abts_buf->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
1683 ret_val = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_buf);
1684 spin_unlock_irqrestore(&phba->hbalock, flags);
cd22d605 1685 if (ret_val) {
86c67379 1686 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561
JS
1687 "6137 Failed abts issue_wqe with status x%x "
1688 "for nvme_fcreq %p.\n",
1689 ret_val, pnvme_fcreq);
1690 lpfc_sli_release_iocbq(phba, abts_buf);
1691 return;
1692 }
1693
86c67379 1694 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
2b7824d0 1695 "6138 Transport Abort NVME Request Issued for "
01649561
JS
1696 "ox_id x%x on reqtag x%x\n",
1697 nvmereq_wqe->sli4_xritag,
1698 abts_buf->iotag);
1699}
1700
1701/* Declare and initialization an instance of the FC NVME template. */
1702static struct nvme_fc_port_template lpfc_nvme_template = {
1703 /* initiator-based functions */
1704 .localport_delete = lpfc_nvme_localport_delete,
1705 .remoteport_delete = lpfc_nvme_remoteport_delete,
1706 .create_queue = lpfc_nvme_create_queue,
1707 .delete_queue = lpfc_nvme_delete_queue,
1708 .ls_req = lpfc_nvme_ls_req,
1709 .fcp_io = lpfc_nvme_fcp_io_submit,
1710 .ls_abort = lpfc_nvme_ls_abort,
1711 .fcp_abort = lpfc_nvme_fcp_abort,
1712
1713 .max_hw_queues = 1,
1714 .max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
1715 .max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
1716 .dma_boundary = 0xFFFFFFFF,
1717
1718 /* Sizes of additional private data for data structures.
1719 * No use for the last two sizes at this time.
1720 */
1721 .local_priv_sz = sizeof(struct lpfc_nvme_lport),
1722 .remote_priv_sz = sizeof(struct lpfc_nvme_rport),
1723 .lsrqst_priv_sz = 0,
bbe3012b 1724 .fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
01649561
JS
1725};
1726
1727/**
1728 * lpfc_sli4_post_nvme_sgl_block - post a block of nvme sgl list to firmware
1729 * @phba: pointer to lpfc hba data structure.
1730 * @nblist: pointer to nvme buffer list.
1731 * @count: number of scsi buffers on the list.
1732 *
1733 * This routine is invoked to post a block of @count scsi sgl pages from a
1734 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
1735 * No Lock is held.
1736 *
1737 **/
1738static int
1739lpfc_sli4_post_nvme_sgl_block(struct lpfc_hba *phba,
1740 struct list_head *nblist,
1741 int count)
1742{
1743 struct lpfc_nvme_buf *lpfc_ncmd;
1744 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
1745 struct sgl_page_pairs *sgl_pg_pairs;
1746 void *viraddr;
1747 LPFC_MBOXQ_t *mbox;
1748 uint32_t reqlen, alloclen, pg_pairs;
1749 uint32_t mbox_tmo;
1750 uint16_t xritag_start = 0;
1751 int rc = 0;
1752 uint32_t shdr_status, shdr_add_status;
1753 dma_addr_t pdma_phys_bpl1;
1754 union lpfc_sli4_cfg_shdr *shdr;
1755
1756 /* Calculate the requested length of the dma memory */
1757 reqlen = count * sizeof(struct sgl_page_pairs) +
1758 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
1759 if (reqlen > SLI4_PAGE_SIZE) {
1760 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1761 "6118 Block sgl registration required DMA "
1762 "size (%d) great than a page\n", reqlen);
1763 return -ENOMEM;
1764 }
1765 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1766 if (!mbox) {
1767 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1768 "6119 Failed to allocate mbox cmd memory\n");
1769 return -ENOMEM;
1770 }
1771
1772 /* Allocate DMA memory and set up the non-embedded mailbox command */
1773 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
1774 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
1775 LPFC_SLI4_MBX_NEMBED);
1776
1777 if (alloclen < reqlen) {
1778 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1779 "6120 Allocated DMA memory size (%d) is "
1780 "less than the requested DMA memory "
1781 "size (%d)\n", alloclen, reqlen);
1782 lpfc_sli4_mbox_cmd_free(phba, mbox);
1783 return -ENOMEM;
1784 }
1785
1786 /* Get the first SGE entry from the non-embedded DMA memory */
1787 viraddr = mbox->sge_array->addr[0];
1788
1789 /* Set up the SGL pages in the non-embedded DMA pages */
1790 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
1791 sgl_pg_pairs = &sgl->sgl_pg_pairs;
1792
1793 pg_pairs = 0;
1794 list_for_each_entry(lpfc_ncmd, nblist, list) {
1795 /* Set up the sge entry */
1796 sgl_pg_pairs->sgl_pg0_addr_lo =
1797 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
1798 sgl_pg_pairs->sgl_pg0_addr_hi =
1799 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
1800 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
1801 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
1802 SGL_PAGE_SIZE;
1803 else
1804 pdma_phys_bpl1 = 0;
1805 sgl_pg_pairs->sgl_pg1_addr_lo =
1806 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
1807 sgl_pg_pairs->sgl_pg1_addr_hi =
1808 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
1809 /* Keep the first xritag on the list */
1810 if (pg_pairs == 0)
1811 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
1812 sgl_pg_pairs++;
1813 pg_pairs++;
1814 }
1815 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
1816 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
1817 /* Perform endian conversion if necessary */
1818 sgl->word0 = cpu_to_le32(sgl->word0);
1819
1820 if (!phba->sli4_hba.intr_enable)
1821 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
1822 else {
1823 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
1824 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
1825 }
1826 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
1827 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1828 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
1829 if (rc != MBX_TIMEOUT)
1830 lpfc_sli4_mbox_cmd_free(phba, mbox);
1831 if (shdr_status || shdr_add_status || rc) {
1832 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1833 "6125 POST_SGL_BLOCK mailbox command failed "
1834 "status x%x add_status x%x mbx status x%x\n",
1835 shdr_status, shdr_add_status, rc);
1836 rc = -ENXIO;
1837 }
1838 return rc;
1839}
1840
1841/**
1842 * lpfc_post_nvme_sgl_list - Post blocks of nvme buffer sgls from a list
1843 * @phba: pointer to lpfc hba data structure.
1844 * @post_nblist: pointer to the nvme buffer list.
1845 *
1846 * This routine walks a list of nvme buffers that was passed in. It attempts
1847 * to construct blocks of nvme buffer sgls which contains contiguous xris and
1848 * uses the non-embedded SGL block post mailbox commands to post to the port.
1849 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
1850 * embedded SGL post mailbox command for posting. The @post_nblist passed in
1851 * must be local list, thus no lock is needed when manipulate the list.
1852 *
1853 * Returns: 0 = failure, non-zero number of successfully posted buffers.
1854 **/
1855static int
1856lpfc_post_nvme_sgl_list(struct lpfc_hba *phba,
1857 struct list_head *post_nblist, int sb_count)
1858{
1859 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
1860 int status, sgl_size;
1861 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
1862 dma_addr_t pdma_phys_sgl1;
1863 int last_xritag = NO_XRI;
1864 int cur_xritag;
1865 LIST_HEAD(prep_nblist);
1866 LIST_HEAD(blck_nblist);
1867 LIST_HEAD(nvme_nblist);
1868
1869 /* sanity check */
1870 if (sb_count <= 0)
1871 return -EINVAL;
1872
1873 sgl_size = phba->cfg_sg_dma_buf_size;
1874
1875 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
1876 list_del_init(&lpfc_ncmd->list);
1877 block_cnt++;
1878 if ((last_xritag != NO_XRI) &&
1879 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
1880 /* a hole in xri block, form a sgl posting block */
1881 list_splice_init(&prep_nblist, &blck_nblist);
1882 post_cnt = block_cnt - 1;
1883 /* prepare list for next posting block */
1884 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
1885 block_cnt = 1;
1886 } else {
1887 /* prepare list for next posting block */
1888 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
1889 /* enough sgls for non-embed sgl mbox command */
1890 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
1891 list_splice_init(&prep_nblist, &blck_nblist);
1892 post_cnt = block_cnt;
1893 block_cnt = 0;
1894 }
1895 }
1896 num_posting++;
1897 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
1898
1899 /* end of repost sgl list condition for NVME buffers */
1900 if (num_posting == sb_count) {
1901 if (post_cnt == 0) {
1902 /* last sgl posting block */
1903 list_splice_init(&prep_nblist, &blck_nblist);
1904 post_cnt = block_cnt;
1905 } else if (block_cnt == 1) {
1906 /* last single sgl with non-contiguous xri */
1907 if (sgl_size > SGL_PAGE_SIZE)
1908 pdma_phys_sgl1 =
1909 lpfc_ncmd->dma_phys_sgl +
1910 SGL_PAGE_SIZE;
1911 else
1912 pdma_phys_sgl1 = 0;
1913 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
1914 status = lpfc_sli4_post_sgl(phba,
1915 lpfc_ncmd->dma_phys_sgl,
1916 pdma_phys_sgl1, cur_xritag);
1917 if (status) {
1918 /* failure, put on abort nvme list */
318083ad 1919 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
01649561
JS
1920 } else {
1921 /* success, put on NVME buffer list */
318083ad 1922 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
01649561
JS
1923 lpfc_ncmd->status = IOSTAT_SUCCESS;
1924 num_posted++;
1925 }
1926 /* success, put on NVME buffer sgl list */
1927 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
1928 }
1929 }
1930
1931 /* continue until a nembed page worth of sgls */
1932 if (post_cnt == 0)
1933 continue;
1934
1935 /* post block of NVME buffer list sgls */
1936 status = lpfc_sli4_post_nvme_sgl_block(phba, &blck_nblist,
1937 post_cnt);
1938
1939 /* don't reset xirtag due to hole in xri block */
1940 if (block_cnt == 0)
1941 last_xritag = NO_XRI;
1942
1943 /* reset NVME buffer post count for next round of posting */
1944 post_cnt = 0;
1945
1946 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
1947 while (!list_empty(&blck_nblist)) {
1948 list_remove_head(&blck_nblist, lpfc_ncmd,
1949 struct lpfc_nvme_buf, list);
1950 if (status) {
1951 /* failure, put on abort nvme list */
318083ad 1952 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
01649561
JS
1953 } else {
1954 /* success, put on NVME buffer list */
318083ad 1955 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
01649561
JS
1956 lpfc_ncmd->status = IOSTAT_SUCCESS;
1957 num_posted++;
1958 }
1959 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
1960 }
1961 }
1962 /* Push NVME buffers with sgl posted to the available list */
1963 while (!list_empty(&nvme_nblist)) {
1964 list_remove_head(&nvme_nblist, lpfc_ncmd,
1965 struct lpfc_nvme_buf, list);
1966 lpfc_release_nvme_buf(phba, lpfc_ncmd);
1967 }
1968 return num_posted;
1969}
1970
1971/**
1972 * lpfc_repost_nvme_sgl_list - Repost all the allocated nvme buffer sgls
1973 * @phba: pointer to lpfc hba data structure.
1974 *
1975 * This routine walks the list of nvme buffers that have been allocated and
1976 * repost them to the port by using SGL block post. This is needed after a
1977 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
1978 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
1979 * to the lpfc_nvme_buf_list. If the repost fails, reject all nvme buffers.
1980 *
1981 * Returns: 0 = success, non-zero failure.
1982 **/
1983int
1984lpfc_repost_nvme_sgl_list(struct lpfc_hba *phba)
1985{
1986 LIST_HEAD(post_nblist);
1987 int num_posted, rc = 0;
1988
1989 /* get all NVME buffers need to repost to a local list */
1990 spin_lock_irq(&phba->nvme_buf_list_get_lock);
1991 spin_lock(&phba->nvme_buf_list_put_lock);
1992 list_splice_init(&phba->lpfc_nvme_buf_list_get, &post_nblist);
1993 list_splice(&phba->lpfc_nvme_buf_list_put, &post_nblist);
1994 spin_unlock(&phba->nvme_buf_list_put_lock);
1995 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
1996
1997 /* post the list of nvme buffer sgls to port if available */
1998 if (!list_empty(&post_nblist)) {
1999 num_posted = lpfc_post_nvme_sgl_list(phba, &post_nblist,
2000 phba->sli4_hba.nvme_xri_cnt);
2001 /* failed to post any nvme buffer, return error */
2002 if (num_posted == 0)
2003 rc = -EIO;
2004 }
2005 return rc;
2006}
2007
2008/**
2009 * lpfc_new_nvme_buf - Scsi buffer allocator for HBA with SLI4 IF spec
2010 * @vport: The virtual port for which this call being executed.
2011 * @num_to_allocate: The requested number of buffers to allocate.
2012 *
2013 * This routine allocates nvme buffers for device with SLI-4 interface spec,
2014 * the nvme buffer contains all the necessary information needed to initiate
2015 * a NVME I/O. After allocating up to @num_to_allocate NVME buffers and put
2016 * them on a list, it post them to the port by using SGL block post.
2017 *
2018 * Return codes:
2019 * int - number of nvme buffers that were allocated and posted.
2020 * 0 = failure, less than num_to_alloc is a partial failure.
2021 **/
2022static int
2023lpfc_new_nvme_buf(struct lpfc_vport *vport, int num_to_alloc)
2024{
2025 struct lpfc_hba *phba = vport->phba;
2026 struct lpfc_nvme_buf *lpfc_ncmd;
2027 struct lpfc_iocbq *pwqeq;
2028 union lpfc_wqe128 *wqe;
2029 struct sli4_sge *sgl;
2030 dma_addr_t pdma_phys_sgl;
2031 uint16_t iotag, lxri = 0;
2032 int bcnt, num_posted, sgl_size;
2033 LIST_HEAD(prep_nblist);
2034 LIST_HEAD(post_nblist);
2035 LIST_HEAD(nvme_nblist);
2036
2037 sgl_size = phba->cfg_sg_dma_buf_size;
2038
2039 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
2040 lpfc_ncmd = kzalloc(sizeof(struct lpfc_nvme_buf), GFP_KERNEL);
2041 if (!lpfc_ncmd)
2042 break;
2043 /*
2044 * Get memory from the pci pool to map the virt space to
2045 * pci bus space for an I/O. The DMA buffer includes the
2046 * number of SGE's necessary to support the sg_tablesize.
2047 */
df2f7729
TM
2048 lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
2049 GFP_KERNEL,
2050 &lpfc_ncmd->dma_handle);
01649561
JS
2051 if (!lpfc_ncmd->data) {
2052 kfree(lpfc_ncmd);
2053 break;
2054 }
01649561
JS
2055
2056 lxri = lpfc_sli4_next_xritag(phba);
2057 if (lxri == NO_XRI) {
771db5c0 2058 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
01649561
JS
2059 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
2060 kfree(lpfc_ncmd);
2061 break;
2062 }
2063 pwqeq = &(lpfc_ncmd->cur_iocbq);
2064 wqe = (union lpfc_wqe128 *)&pwqeq->wqe;
2065
2066 /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
2067 iotag = lpfc_sli_next_iotag(phba, pwqeq);
2068 if (iotag == 0) {
771db5c0 2069 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
01649561
JS
2070 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
2071 kfree(lpfc_ncmd);
2072 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2073 "6121 Failed to allocated IOTAG for"
2074 " XRI:0x%x\n", lxri);
2075 lpfc_sli4_free_xri(phba, lxri);
2076 break;
2077 }
2078 pwqeq->sli4_lxritag = lxri;
2079 pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
2080 pwqeq->iocb_flag |= LPFC_IO_NVME;
2081 pwqeq->context1 = lpfc_ncmd;
2082 pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
2083
2084 /* Initialize local short-hand pointers. */
2085 lpfc_ncmd->nvme_sgl = lpfc_ncmd->data;
2086 sgl = lpfc_ncmd->nvme_sgl;
2087 pdma_phys_sgl = lpfc_ncmd->dma_handle;
2088 lpfc_ncmd->dma_phys_sgl = pdma_phys_sgl;
2089
2090 /* Rsp SGE will be filled in when we rcv an IO
2091 * from the NVME Layer to be sent.
2092 * The cmd is going to be embedded so we need a SKIP SGE.
2093 */
2094 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2095 bf_set(lpfc_sli4_sge_last, sgl, 0);
2096 sgl->word2 = cpu_to_le32(sgl->word2);
2097 /* Fill in word 3 / sgl_len during cmd submission */
2098
2099 lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
2100
2101 /* Word 7 */
2102 bf_set(wqe_erp, &wqe->generic.wqe_com, 0);
2103 /* NVME upper layers will time things out, if needed */
2104 bf_set(wqe_tmo, &wqe->generic.wqe_com, 0);
2105
2106 /* Word 10 */
2107 bf_set(wqe_ebde_cnt, &wqe->generic.wqe_com, 0);
2108 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
2109
2110 /* add the nvme buffer to a post list */
2111 list_add_tail(&lpfc_ncmd->list, &post_nblist);
2112 spin_lock_irq(&phba->nvme_buf_list_get_lock);
2113 phba->sli4_hba.nvme_xri_cnt++;
2114 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
2115 }
2116 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
2117 "6114 Allocate %d out of %d requested new NVME "
2118 "buffers\n", bcnt, num_to_alloc);
2119
2120 /* post the list of nvme buffer sgls to port if available */
2121 if (!list_empty(&post_nblist))
2122 num_posted = lpfc_post_nvme_sgl_list(phba,
2123 &post_nblist, bcnt);
2124 else
2125 num_posted = 0;
2126
2127 return num_posted;
2128}
2129
2130/**
2131 * lpfc_get_nvme_buf - Get a nvme buffer from lpfc_nvme_buf_list of the HBA
2132 * @phba: The HBA for which this call is being executed.
2133 *
2134 * This routine removes a nvme buffer from head of @phba lpfc_nvme_buf_list list
2135 * and returns to caller.
2136 *
2137 * Return codes:
2138 * NULL - Error
2139 * Pointer to lpfc_nvme_buf - Success
2140 **/
2141static struct lpfc_nvme_buf *
2142lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
2143{
2144 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
2145 unsigned long iflag = 0;
2146 int found = 0;
2147
2148 spin_lock_irqsave(&phba->nvme_buf_list_get_lock, iflag);
2149 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
2150 &phba->lpfc_nvme_buf_list_get, list) {
bbe3012b 2151 list_del_init(&lpfc_ncmd->list);
01649561
JS
2152 found = 1;
2153 break;
2154 }
2155 if (!found) {
2156 spin_lock(&phba->nvme_buf_list_put_lock);
2157 list_splice(&phba->lpfc_nvme_buf_list_put,
2158 &phba->lpfc_nvme_buf_list_get);
2159 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
2160 spin_unlock(&phba->nvme_buf_list_put_lock);
2161 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
2162 &phba->lpfc_nvme_buf_list_get, list) {
bbe3012b 2163 list_del_init(&lpfc_ncmd->list);
01649561
JS
2164 found = 1;
2165 break;
2166 }
2167 }
2168 spin_unlock_irqrestore(&phba->nvme_buf_list_get_lock, iflag);
2169 if (!found)
2170 return NULL;
2171 return lpfc_ncmd;
2172}
2173
2174/**
2175 * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
2176 * @phba: The Hba for which this call is being executed.
2177 * @lpfc_ncmd: The nvme buffer which is being released.
2178 *
2179 * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
2180 * lpfc_nvme_buf_list list. For SLI4 XRI's are tied to the nvme buffer
2181 * and cannot be reused for at least RA_TOV amount of time if it was
2182 * aborted.
2183 **/
2184static void
2185lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_nvme_buf *lpfc_ncmd)
2186{
2187 unsigned long iflag = 0;
2188
2189 lpfc_ncmd->nonsg_phys = 0;
318083ad 2190 if (lpfc_ncmd->flags & LPFC_SBUF_XBUSY) {
86c67379
JS
2191 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2192 "6310 XB release deferred for "
2193 "ox_id x%x on reqtag x%x\n",
2194 lpfc_ncmd->cur_iocbq.sli4_xritag,
2195 lpfc_ncmd->cur_iocbq.iotag);
2196
01649561
JS
2197 spin_lock_irqsave(&phba->sli4_hba.abts_nvme_buf_list_lock,
2198 iflag);
01649561
JS
2199 list_add_tail(&lpfc_ncmd->list,
2200 &phba->sli4_hba.lpfc_abts_nvme_buf_list);
2201 spin_unlock_irqrestore(&phba->sli4_hba.abts_nvme_buf_list_lock,
2202 iflag);
2203 } else {
2204 lpfc_ncmd->nvmeCmd = NULL;
2205 lpfc_ncmd->cur_iocbq.iocb_flag = LPFC_IO_NVME;
2206 spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
2207 list_add_tail(&lpfc_ncmd->list, &phba->lpfc_nvme_buf_list_put);
2208 spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
2209 }
2210}
2211
2212/**
2213 * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
2214 * @pvport - the lpfc_vport instance requesting a localport.
2215 *
2216 * This routine is invoked to create an nvme localport instance to bind
2217 * to the nvme_fc_transport. It is called once during driver load
2218 * like lpfc_create_shost after all other services are initialized.
2219 * It requires a vport, vpi, and wwns at call time. Other localport
2220 * parameters are modified as the driver's FCID and the Fabric WWN
2221 * are established.
2222 *
2223 * Return codes
2224 * 0 - successful
2225 * -ENOMEM - no heap memory available
2226 * other values - from nvme registration upcall
2227 **/
2228int
2229lpfc_nvme_create_localport(struct lpfc_vport *vport)
2230{
166d7211 2231 int ret = 0;
01649561
JS
2232 struct lpfc_hba *phba = vport->phba;
2233 struct nvme_fc_port_info nfcp_info;
2234 struct nvme_fc_local_port *localport;
2235 struct lpfc_nvme_lport *lport;
166d7211 2236 int len;
01649561
JS
2237
2238 /* Initialize this localport instance. The vport wwn usage ensures
2239 * that NPIV is accounted for.
2240 */
2241 memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
2242 nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
2243 nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
2244 nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
2245
4d4c4a4a
JS
2246 /* Limit to LPFC_MAX_NVME_SEG_CNT.
2247 * For now need + 1 to get around NVME transport logic.
2248 */
2249 if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
2250 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_INIT,
2251 "6300 Reducing sg segment cnt to %d\n",
2252 LPFC_MAX_NVME_SEG_CNT);
2253 phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
2254 } else {
2255 phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
2256 }
2257 lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
01649561
JS
2258 lpfc_nvme_template.max_hw_queues = phba->cfg_nvme_io_channel;
2259
2260 /* localport is allocated from the stack, but the registration
2261 * call allocates heap memory as well as the private area.
2262 */
7d708033 2263#if (IS_ENABLED(CONFIG_NVME_FC))
01649561
JS
2264 ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
2265 &vport->phba->pcidev->dev, &localport);
166d7211
JS
2266#else
2267 ret = -ENOMEM;
2268#endif
01649561
JS
2269 if (!ret) {
2270 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
2271 "6005 Successfully registered local "
2272 "NVME port num %d, localP %p, private %p, "
2273 "sg_seg %d\n",
2274 localport->port_num, localport,
2275 localport->private,
2276 lpfc_nvme_template.max_sgl_segments);
2277
2278 /* Private is our lport size declared in the template. */
2279 lport = (struct lpfc_nvme_lport *)localport->private;
2280 vport->localport = localport;
2281 lport->vport = vport;
01649561 2282 vport->nvmei_support = 1;
6b486ce9
DK
2283
2284 /* Don't post more new bufs if repost already recovered
2285 * the nvme sgls.
2286 */
2287 if (phba->sli4_hba.nvme_xri_cnt == 0) {
2288 len = lpfc_new_nvme_buf(vport,
2289 phba->sli4_hba.nvme_xri_max);
2290 vport->phba->total_nvme_bufs += len;
2291 }
01649561
JS
2292 }
2293
01649561
JS
2294 return ret;
2295}
2296
add9d6be
JS
2297/* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2298 *
2299 * The driver has to wait for the host nvme transport to callback
2300 * indicating the localport has successfully unregistered all
2301 * resources. Since this is an uninterruptible wait, loop every ten
2302 * seconds and print a message indicating no progress.
2303 *
2304 * An uninterruptible wait is used because of the risk of transport-to-
2305 * driver state mismatch.
2306 */
2307void
2308lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
2309 struct lpfc_nvme_lport *lport)
2310{
2311#if (IS_ENABLED(CONFIG_NVME_FC))
2312 u32 wait_tmo;
2313 int ret;
2314
2315 /* Host transport has to clean up and confirm requiring an indefinite
2316 * wait. Print a message if a 10 second wait expires and renew the
2317 * wait. This is unexpected.
2318 */
2319 wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2320 while (true) {
2321 ret = wait_for_completion_timeout(&lport->lport_unreg_done,
2322 wait_tmo);
2323 if (unlikely(!ret)) {
2324 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
2325 "6176 Lport %p Localport %p wait "
2326 "timed out. Renewing.\n",
2327 lport, vport->localport);
2328 continue;
2329 }
2330 break;
2331 }
2332 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
2333 "6177 Lport %p Localport %p Complete Success\n",
2334 lport, vport->localport);
2335#endif
2336}
2337
01649561
JS
2338/**
2339 * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
2340 * @pnvme: pointer to lpfc nvme data structure.
2341 *
2342 * This routine is invoked to destroy all lports bound to the phba.
2343 * The lport memory was allocated by the nvme fc transport and is
2344 * released there. This routine ensures all rports bound to the
2345 * lport have been disconnected.
2346 *
2347 **/
2348void
2349lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
2350{
7d708033 2351#if (IS_ENABLED(CONFIG_NVME_FC))
01649561
JS
2352 struct nvme_fc_local_port *localport;
2353 struct lpfc_nvme_lport *lport;
01649561
JS
2354 int ret;
2355
2356 if (vport->nvmei_support == 0)
2357 return;
2358
2359 localport = vport->localport;
2360 vport->localport = NULL;
2361 lport = (struct lpfc_nvme_lport *)localport->private;
2362
2363 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2364 "6011 Destroying NVME localport %p\n",
2365 localport);
166d7211 2366
01649561
JS
2367 /* lport's rport list is clear. Unregister
2368 * lport and release resources.
2369 */
2370 init_completion(&lport->lport_unreg_done);
2371 ret = nvme_fc_unregister_localport(localport);
add9d6be
JS
2372
2373 /* Wait for completion. This either blocks
2374 * indefinitely or succeeds
2375 */
2376 lpfc_nvme_lport_unreg_wait(vport, lport);
01649561
JS
2377
2378 /* Regardless of the unregister upcall response, clear
2379 * nvmei_support. All rports are unregistered and the
2380 * driver will clean up.
2381 */
2382 vport->nvmei_support = 0;
2383 if (ret == 0) {
2384 lpfc_printf_vlog(vport,
2385 KERN_INFO, LOG_NVME_DISC,
2386 "6009 Unregistered lport Success\n");
2387 } else {
2388 lpfc_printf_vlog(vport,
2389 KERN_INFO, LOG_NVME_DISC,
2390 "6010 Unregistered lport "
2391 "Failed, status x%x\n",
2392 ret);
2393 }
166d7211 2394#endif
01649561
JS
2395}
2396
2397void
2398lpfc_nvme_update_localport(struct lpfc_vport *vport)
2399{
4410a67a 2400#if (IS_ENABLED(CONFIG_NVME_FC))
01649561
JS
2401 struct nvme_fc_local_port *localport;
2402 struct lpfc_nvme_lport *lport;
2403
2404 localport = vport->localport;
4410a67a
JS
2405 if (!localport) {
2406 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
2407 "6710 Update NVME fail. No localport\n");
2408 return;
2409 }
01649561 2410 lport = (struct lpfc_nvme_lport *)localport->private;
4410a67a
JS
2411 if (!lport) {
2412 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
2413 "6171 Update NVME fail. localP %p, No lport\n",
2414 localport);
2415 return;
2416 }
01649561
JS
2417 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2418 "6012 Update NVME lport %p did x%x\n",
2419 localport, vport->fc_myDID);
2420
2421 localport->port_id = vport->fc_myDID;
2422 if (localport->port_id == 0)
2423 localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
2424 else
2425 localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
2426
2427 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2428 "6030 bound lport %p to DID x%06x\n",
2429 lport, localport->port_id);
4410a67a 2430#endif
01649561
JS
2431}
2432
2433int
2434lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2435{
7d708033 2436#if (IS_ENABLED(CONFIG_NVME_FC))
01649561
JS
2437 int ret = 0;
2438 struct nvme_fc_local_port *localport;
2439 struct lpfc_nvme_lport *lport;
2440 struct lpfc_nvme_rport *rport;
2441 struct nvme_fc_remote_port *remote_port;
2442 struct nvme_fc_port_info rpinfo;
2b75d0f9 2443 struct lpfc_nodelist *prev_ndlp;
01649561
JS
2444
2445 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
2446 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
2447 ndlp->nlp_DID, ndlp->nlp_type);
2448
2449 localport = vport->localport;
bb6a8a2c
DK
2450 if (!localport)
2451 return 0;
2452
01649561
JS
2453 lport = (struct lpfc_nvme_lport *)localport->private;
2454
7a06dcd3
JS
2455 /* NVME rports are not preserved across devloss.
2456 * Just register this instance. Note, rpinfo->dev_loss_tmo
2457 * is left 0 to indicate accept transport defaults. The
2458 * driver communicates port role capabilities consistent
2459 * with the PRLI response data.
2460 */
2461 memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
2462 rpinfo.port_id = ndlp->nlp_DID;
2463 if (ndlp->nlp_type & NLP_NVME_TARGET)
2464 rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
2465 if (ndlp->nlp_type & NLP_NVME_INITIATOR)
2466 rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
2467
2468 if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
2469 rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
2470
2471 rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
2472 rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2473 ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
2474 if (!ret) {
2475 /* If the ndlp already has an nrport, this is just
2476 * a resume of the existing rport. Else this is a
2477 * new rport.
01649561 2478 */
7a06dcd3 2479 rport = remote_port->private;
2b75d0f9 2480 if (ndlp->nrport) {
7a06dcd3
JS
2481 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
2482 LOG_NVME_DISC,
2483 "6014 Rebinding lport to "
2484 "rport wwpn 0x%llx, "
2485 "Data: x%x x%x x%x x%06x\n",
2486 remote_port->port_name,
2487 remote_port->port_id,
2488 remote_port->port_role,
01649561
JS
2489 ndlp->nlp_type,
2490 ndlp->nlp_DID);
2b75d0f9
DK
2491 prev_ndlp = rport->ndlp;
2492
2493 /* Sever the ndlp<->rport connection before dropping
2494 * the ndlp ref from register.
2495 */
2496 ndlp->nrport = NULL;
2497 rport->ndlp = NULL;
2498 if (prev_ndlp)
2499 lpfc_nlp_put(ndlp);
01649561 2500 }
2b75d0f9
DK
2501
2502 /* Clean bind the rport to the ndlp. */
2503 rport->remoteport = remote_port;
2504 rport->lport = lport;
2505 rport->ndlp = lpfc_nlp_get(ndlp);
2506 if (!rport->ndlp)
2507 return -1;
2508 ndlp->nrport = rport;
2509 lpfc_printf_vlog(vport, KERN_INFO,
2510 LOG_NVME_DISC | LOG_NODE,
2511 "6022 Binding new rport to "
2512 "lport %p Rport WWNN 0x%llx, "
2513 "Rport WWPN 0x%llx DID "
2514 "x%06x Role x%x\n",
2515 lport,
2516 rpinfo.node_name, rpinfo.port_name,
2517 rpinfo.port_id, rpinfo.port_role);
01649561 2518 } else {
7a06dcd3
JS
2519 lpfc_printf_vlog(vport, KERN_ERR,
2520 LOG_NVME_DISC | LOG_NODE,
2521 "6031 RemotePort Registration failed "
2522 "err: %d, DID x%06x\n",
2523 ret, ndlp->nlp_DID);
01649561 2524 }
7a06dcd3 2525
01649561 2526 return ret;
166d7211
JS
2527#else
2528 return 0;
2529#endif
01649561
JS
2530}
2531
add9d6be
JS
2532/* lpfc_nvme_rport_unreg_wait - Wait for the host to complete an rport unreg.
2533 *
2534 * The driver has to wait for the host nvme transport to callback
2535 * indicating the remoteport has successfully unregistered all
2536 * resources. Since this is an uninterruptible wait, loop every ten
2537 * seconds and print a message indicating no progress.
2538 *
2539 * An uninterruptible wait is used because of the risk of transport-to-
2540 * driver state mismatch.
2541 */
2542void
2543lpfc_nvme_rport_unreg_wait(struct lpfc_vport *vport,
2544 struct lpfc_nvme_rport *rport)
2545{
2546#if (IS_ENABLED(CONFIG_NVME_FC))
2547 u32 wait_tmo;
2548 int ret;
2549
2550 /* Host transport has to clean up and confirm requiring an indefinite
2551 * wait. Print a message if a 10 second wait expires and renew the
2552 * wait. This is unexpected.
2553 */
2554 wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2555 while (true) {
2556 ret = wait_for_completion_timeout(&rport->rport_unreg_done,
2557 wait_tmo);
2558 if (unlikely(!ret)) {
2559 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
2560 "6174 Rport %p Remoteport %p wait "
2561 "timed out. Renewing.\n",
2562 rport, rport->remoteport);
2563 continue;
2564 }
2565 break;
2566 }
2567 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
2568 "6175 Rport %p Remoteport %p Complete Success\n",
2569 rport, rport->remoteport);
2570#endif
2571}
2572
01649561
JS
2573/* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
2574 *
2575 * There is no notion of Devloss or rport recovery from the current
2576 * nvme_transport perspective. Loss of an rport just means IO cannot
2577 * be sent and recovery is completely up to the initator.
2578 * For now, the driver just unbinds the DID and port_role so that
2579 * no further IO can be issued. Changes are planned for later.
2580 *
2581 * Notes - the ndlp reference count is not decremented here since
2582 * since there is no nvme_transport api for devloss. Node ref count
2583 * is only adjusted in driver unload.
2584 */
2585void
2586lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2587{
7d708033 2588#if (IS_ENABLED(CONFIG_NVME_FC))
01649561
JS
2589 int ret;
2590 struct nvme_fc_local_port *localport;
2591 struct lpfc_nvme_lport *lport;
2592 struct lpfc_nvme_rport *rport;
2593 struct nvme_fc_remote_port *remoteport;
2594
2595 localport = vport->localport;
2596
2597 /* This is fundamental error. The localport is always
2598 * available until driver unload. Just exit.
2599 */
2600 if (!localport)
2601 return;
2602
2603 lport = (struct lpfc_nvme_lport *)localport->private;
2604 if (!lport)
2605 goto input_err;
2606
2607 rport = ndlp->nrport;
2608 if (!rport)
2609 goto input_err;
2610
2611 remoteport = rport->remoteport;
2612 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2613 "6033 Unreg nvme remoteport %p, portname x%llx, "
2614 "port_id x%06x, portstate x%x port type x%x\n",
2615 remoteport, remoteport->port_name,
2616 remoteport->port_id, remoteport->port_state,
2617 ndlp->nlp_type);
2618
2619 /* Sanity check ndlp type. Only call for NVME ports. Don't
2620 * clear any rport state until the transport calls back.
2621 */
3b5bde69
JS
2622
2623 if (ndlp->nlp_type & NLP_NVME_TARGET) {
01649561 2624 init_completion(&rport->rport_unreg_done);
7a06dcd3
JS
2625
2626 /* No concern about the role change on the nvme remoteport.
2627 * The transport will update it.
2628 */
add9d6be 2629 ndlp->upcall_flags |= NLP_WAIT_FOR_UNREG;
01649561 2630 ret = nvme_fc_unregister_remoteport(remoteport);
add9d6be 2631 if (ret != 0)
01649561
JS
2632 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
2633 "6167 NVME unregister failed %d "
2634 "port_state x%x\n",
2635 ret, remoteport->port_state);
add9d6be
JS
2636 else
2637 /* Wait for completion. This either blocks
2638 * indefinitely or succeeds
2639 */
2640 lpfc_nvme_rport_unreg_wait(vport, rport);
2641 ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
01649561
JS
2642 }
2643 return;
2644
2645 input_err:
166d7211 2646#endif
01649561 2647 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
2b7824d0 2648 "6168 State error: lport %p, rport%p FCID x%06x\n",
01649561
JS
2649 vport->localport, ndlp->rport, ndlp->nlp_DID);
2650}
318083ad
JS
2651
2652/**
2653 * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2654 * @phba: pointer to lpfc hba data structure.
2655 * @axri: pointer to the fcp xri abort wcqe structure.
2656 *
2657 * This routine is invoked by the worker thread to process a SLI4 fast-path
952c303b
DK
2658 * NVME aborted xri. Aborted NVME IO commands are completed to the transport
2659 * here.
318083ad
JS
2660 **/
2661void
2662lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2663 struct sli4_wcqe_xri_aborted *axri)
2664{
2665 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
318083ad 2666 struct lpfc_nvme_buf *lpfc_ncmd, *next_lpfc_ncmd;
952c303b 2667 struct nvmefc_fcp_req *nvme_cmd = NULL;
318083ad
JS
2668 struct lpfc_nodelist *ndlp;
2669 unsigned long iflag = 0;
318083ad
JS
2670
2671 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
2672 return;
2673 spin_lock_irqsave(&phba->hbalock, iflag);
2674 spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2675 list_for_each_entry_safe(lpfc_ncmd, next_lpfc_ncmd,
2676 &phba->sli4_hba.lpfc_abts_nvme_buf_list,
2677 list) {
2678 if (lpfc_ncmd->cur_iocbq.sli4_xritag == xri) {
bbe3012b 2679 list_del_init(&lpfc_ncmd->list);
318083ad
JS
2680 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
2681 lpfc_ncmd->status = IOSTAT_SUCCESS;
2682 spin_unlock(
2683 &phba->sli4_hba.abts_nvme_buf_list_lock);
2684
318083ad
JS
2685 spin_unlock_irqrestore(&phba->hbalock, iflag);
2686 ndlp = lpfc_ncmd->ndlp;
952c303b 2687 if (ndlp)
318083ad 2688 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
86c67379
JS
2689
2690 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
952c303b
DK
2691 "6311 nvme_cmd %p xri x%x tag x%x "
2692 "abort complete and xri released\n",
2693 lpfc_ncmd->nvmeCmd, xri,
2694 lpfc_ncmd->cur_iocbq.iotag);
2695
2696 /* Aborted NVME commands are required to not complete
2697 * before the abort exchange command fully completes.
2698 * Once completed, it is available via the put list.
2699 */
2700 nvme_cmd = lpfc_ncmd->nvmeCmd;
2701 nvme_cmd->done(nvme_cmd);
318083ad 2702 lpfc_release_nvme_buf(phba, lpfc_ncmd);
318083ad
JS
2703 return;
2704 }
2705 }
2706 spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2707 spin_unlock_irqrestore(&phba->hbalock, iflag);
86c67379
JS
2708
2709 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2710 "6312 XRI Aborted xri x%x not found\n", xri);
2711
318083ad 2712}
c3725bdc
JS
2713
2714/**
2715 * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2716 * @phba: Pointer to HBA context object.
2717 *
2718 * This function flushes all wqes in the nvme rings and frees all resources
2719 * in the txcmplq. This function does not issue abort wqes for the IO
2720 * commands in txcmplq, they will just be returned with
2721 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2722 * slot has been permanently disabled.
2723 **/
2724void
2725lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2726{
2727 struct lpfc_sli_ring *pring;
2728 u32 i, wait_cnt = 0;
2729
2730 if (phba->sli_rev < LPFC_SLI_REV4)
2731 return;
2732
2733 /* Cycle through all NVME rings and make sure all outstanding
2734 * WQEs have been removed from the txcmplqs.
2735 */
2736 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
2737 pring = phba->sli4_hba.nvme_wq[i]->pring;
2738
2739 /* Retrieve everything on the txcmplq */
2740 while (!list_empty(&pring->txcmplq)) {
2741 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
2742 wait_cnt++;
2743
2744 /* The sleep is 10mS. Every ten seconds,
2745 * dump a message. Something is wrong.
2746 */
2747 if ((wait_cnt % 1000) == 0) {
2748 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2749 "6178 NVME IO not empty, "
2750 "cnt %d\n", wait_cnt);
2751 }
2752 }
2753 }
2754}