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