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