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