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