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