xref: /linux/drivers/scsi/lpfc/lpfc_nvme.c (revision f6b35a75042b212c8eb81846ece1da6b471e5b56)
101649561SJames Smart /*******************************************************************
201649561SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
301649561SJames Smart  * Fibre Channel Host Bus Adapters.                                *
467073c69SJames Smart  * Copyright (C) 2017-2021 Broadcom. All Rights Reserved. The term *
53e21d1cbSJames Smart  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
601649561SJames Smart  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
701649561SJames Smart  * EMULEX and SLI are trademarks of Emulex.                        *
8d080abe0SJames Smart  * www.broadcom.com                                                *
901649561SJames Smart  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
1001649561SJames Smart  *                                                                 *
1101649561SJames Smart  * This program is free software; you can redistribute it and/or   *
1201649561SJames Smart  * modify it under the terms of version 2 of the GNU General       *
1301649561SJames Smart  * Public License as published by the Free Software Foundation.    *
1401649561SJames Smart  * This program is distributed in the hope that it will be useful. *
1501649561SJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
1601649561SJames Smart  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
1701649561SJames Smart  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
1801649561SJames Smart  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
1901649561SJames Smart  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
2001649561SJames Smart  * more details, a copy of which can be found in the file COPYING  *
2101649561SJames Smart  * included with this package.                                     *
2201649561SJames Smart  ********************************************************************/
2301649561SJames Smart #include <linux/pci.h>
2401649561SJames Smart #include <linux/slab.h>
2501649561SJames Smart #include <linux/interrupt.h>
2601649561SJames Smart #include <linux/delay.h>
2701649561SJames Smart #include <asm/unaligned.h>
2801649561SJames Smart #include <linux/crc-t10dif.h>
2901649561SJames Smart #include <net/checksum.h>
3001649561SJames Smart 
3101649561SJames Smart #include <scsi/scsi.h>
3201649561SJames Smart #include <scsi/scsi_device.h>
3301649561SJames Smart #include <scsi/scsi_eh.h>
3401649561SJames Smart #include <scsi/scsi_host.h>
3501649561SJames Smart #include <scsi/scsi_tcq.h>
3601649561SJames Smart #include <scsi/scsi_transport_fc.h>
3701649561SJames Smart #include <scsi/fc/fc_fs.h>
3801649561SJames Smart 
3901649561SJames Smart #include "lpfc_version.h"
4001649561SJames Smart #include "lpfc_hw4.h"
4101649561SJames Smart #include "lpfc_hw.h"
4201649561SJames Smart #include "lpfc_sli.h"
4301649561SJames Smart #include "lpfc_sli4.h"
4401649561SJames Smart #include "lpfc_nl.h"
4501649561SJames Smart #include "lpfc_disc.h"
4601649561SJames Smart #include "lpfc.h"
4701649561SJames Smart #include "lpfc_nvme.h"
4801649561SJames Smart #include "lpfc_scsi.h"
4901649561SJames Smart #include "lpfc_logmsg.h"
5001649561SJames Smart #include "lpfc_crtn.h"
5101649561SJames Smart #include "lpfc_vport.h"
52bd2cdd5eSJames Smart #include "lpfc_debugfs.h"
5301649561SJames Smart 
5401649561SJames Smart /* NVME initiator-based functions */
5501649561SJames Smart 
56c490850aSJames Smart static struct lpfc_io_buf *
57cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
585e5b511dSJames Smart 		  int idx, int expedite);
5901649561SJames Smart 
6001649561SJames Smart static void
61c490850aSJames Smart lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_io_buf *);
6201649561SJames Smart 
6381e6a637SJames Smart static struct nvme_fc_port_template lpfc_nvme_template;
6401649561SJames Smart 
6501649561SJames Smart /**
6601649561SJames Smart  * lpfc_nvme_create_queue -
6709d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from
6801649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
6909d99705SLee Jones  * @qsize: Size of the queue in bytes
7001649561SJames Smart  * @handle: An opaque driver handle used in follow-up calls.
7101649561SJames Smart  *
7201649561SJames Smart  * Driver registers this routine to preallocate and initialize any
7301649561SJames Smart  * internal data structures to bind the @qidx to its internal IO queues.
7401649561SJames Smart  * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
7501649561SJames Smart  *
7601649561SJames Smart  * Return value :
7701649561SJames Smart  *   0 - Success
7801649561SJames Smart  *   -EINVAL - Unsupported input value.
7901649561SJames Smart  *   -ENOMEM - Could not alloc necessary memory
8001649561SJames Smart  **/
8101649561SJames Smart static int
8201649561SJames Smart lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
8301649561SJames Smart 		       unsigned int qidx, u16 qsize,
8401649561SJames Smart 		       void **handle)
8501649561SJames Smart {
8601649561SJames Smart 	struct lpfc_nvme_lport *lport;
8701649561SJames Smart 	struct lpfc_vport *vport;
8801649561SJames Smart 	struct lpfc_nvme_qhandle *qhandle;
8901649561SJames Smart 	char *str;
9001649561SJames Smart 
91c3725bdcSJames Smart 	if (!pnvme_lport->private)
92c3725bdcSJames Smart 		return -ENOMEM;
93c3725bdcSJames Smart 
9401649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
9501649561SJames Smart 	vport = lport->vport;
9601649561SJames Smart 	qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
9701649561SJames Smart 	if (qhandle == NULL)
9801649561SJames Smart 		return -ENOMEM;
9901649561SJames Smart 
100d6d189ceSBart Van Assche 	qhandle->cpu_id = raw_smp_processor_id();
10101649561SJames Smart 	qhandle->qidx = qidx;
10201649561SJames Smart 	/*
10301649561SJames Smart 	 * NVME qidx == 0 is the admin queue, so both admin queue
10401649561SJames Smart 	 * and first IO queue will use MSI-X vector and associated
10501649561SJames Smart 	 * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
10601649561SJames Smart 	 */
10701649561SJames Smart 	if (qidx) {
10801649561SJames Smart 		str = "IO ";  /* IO queue */
10901649561SJames Smart 		qhandle->index = ((qidx - 1) %
1106a828b0fSJames Smart 			lpfc_nvme_template.max_hw_queues);
11101649561SJames Smart 	} else {
11201649561SJames Smart 		str = "ADM";  /* Admin queue */
11301649561SJames Smart 		qhandle->index = qidx;
11401649561SJames Smart 	}
11501649561SJames Smart 
116d58734f0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
11701649561SJames Smart 			 "6073 Binding %s HdwQueue %d  (cpu %d) to "
11832350664SJames Smart 			 "hdw_queue %d qhandle x%px\n", str,
11901649561SJames Smart 			 qidx, qhandle->cpu_id, qhandle->index, qhandle);
12001649561SJames Smart 	*handle = (void *)qhandle;
12101649561SJames Smart 	return 0;
12201649561SJames Smart }
12301649561SJames Smart 
12401649561SJames Smart /**
12501649561SJames Smart  * lpfc_nvme_delete_queue -
12609d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from
12701649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
12801649561SJames Smart  * @handle: An opaque driver handle from lpfc_nvme_create_queue
12901649561SJames Smart  *
13001649561SJames Smart  * Driver registers this routine to free
13101649561SJames Smart  * any internal data structures to bind the @qidx to its internal
13201649561SJames Smart  * IO queues.
13301649561SJames Smart  *
13401649561SJames Smart  * Return value :
13501649561SJames Smart  *   0 - Success
13601649561SJames Smart  *   TODO:  What are the failure codes.
13701649561SJames Smart  **/
13801649561SJames Smart static void
13901649561SJames Smart lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
14001649561SJames Smart 		       unsigned int qidx,
14101649561SJames Smart 		       void *handle)
14201649561SJames Smart {
14301649561SJames Smart 	struct lpfc_nvme_lport *lport;
14401649561SJames Smart 	struct lpfc_vport *vport;
14501649561SJames Smart 
146c3725bdcSJames Smart 	if (!pnvme_lport->private)
147c3725bdcSJames Smart 		return;
148c3725bdcSJames Smart 
14901649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
15001649561SJames Smart 	vport = lport->vport;
15101649561SJames Smart 
15201649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
15332350664SJames Smart 			"6001 ENTER.  lpfc_pnvme x%px, qidx x%x qhandle x%px\n",
15401649561SJames Smart 			lport, qidx, handle);
15501649561SJames Smart 	kfree(handle);
15601649561SJames Smart }
15701649561SJames Smart 
15801649561SJames Smart static void
15901649561SJames Smart lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
16001649561SJames Smart {
16101649561SJames Smart 	struct lpfc_nvme_lport *lport = localport->private;
16201649561SJames Smart 
163add9d6beSJames Smart 	lpfc_printf_vlog(lport->vport, KERN_INFO, LOG_NVME,
16432350664SJames Smart 			 "6173 localport x%px delete complete\n",
165add9d6beSJames Smart 			 lport);
166add9d6beSJames Smart 
16701649561SJames Smart 	/* release any threads waiting for the unreg to complete */
1687961cba6SEwan D. Milne 	if (lport->vport->localport)
1697961cba6SEwan D. Milne 		complete(lport->lport_unreg_cmp);
17001649561SJames Smart }
17101649561SJames Smart 
17201649561SJames Smart /* lpfc_nvme_remoteport_delete
17301649561SJames Smart  *
17401649561SJames Smart  * @remoteport: Pointer to an nvme transport remoteport instance.
17501649561SJames Smart  *
17601649561SJames Smart  * This is a template downcall.  NVME transport calls this function
17701649561SJames Smart  * when it has completed the unregistration of a previously
17801649561SJames Smart  * registered remoteport.
17901649561SJames Smart  *
18001649561SJames Smart  * Return value :
18101649561SJames Smart  * None
18201649561SJames Smart  */
1833999df75SBart Van Assche static void
18401649561SJames Smart lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
18501649561SJames Smart {
18601649561SJames Smart 	struct lpfc_nvme_rport *rport = remoteport->private;
18701649561SJames Smart 	struct lpfc_vport *vport;
18801649561SJames Smart 	struct lpfc_nodelist *ndlp;
189e9b11083SJames Smart 	u32 fc4_xpt_flags;
19001649561SJames Smart 
19101649561SJames Smart 	ndlp = rport->ndlp;
192e9b11083SJames Smart 	if (!ndlp) {
193e9b11083SJames Smart 		pr_err("**** %s: NULL ndlp on rport %p remoteport %p\n",
194e9b11083SJames Smart 		       __func__, rport, remoteport);
19501649561SJames Smart 		goto rport_err;
196e9b11083SJames Smart 	}
19701649561SJames Smart 
19801649561SJames Smart 	vport = ndlp->vport;
199e9b11083SJames Smart 	if (!vport) {
200e9b11083SJames Smart 		pr_err("**** %s: Null vport on ndlp %p, ste x%x rport %p\n",
201e9b11083SJames Smart 		       __func__, ndlp, ndlp->nlp_state, rport);
20201649561SJames Smart 		goto rport_err;
203e9b11083SJames Smart 	}
204e9b11083SJames Smart 
205e9b11083SJames Smart 	fc4_xpt_flags = NVME_XPT_REGD | SCSI_XPT_REGD;
20601649561SJames Smart 
20701649561SJames Smart 	/* Remove this rport from the lport's list - memory is owned by the
20801649561SJames Smart 	 * transport. Remove the ndlp reference for the NVME transport before
2097a06dcd3SJames Smart 	 * calling state machine to remove the node.
21001649561SJames Smart 	 */
21101649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
212e9b11083SJames Smart 			"6146 remoteport delete of remoteport %p\n",
21301649561SJames Smart 			remoteport);
214c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
215b15bd3e6SJames Smart 
216b15bd3e6SJames Smart 	/* The register rebind might have occurred before the delete
217b15bd3e6SJames Smart 	 * downcall.  Guard against this race.
218b15bd3e6SJames Smart 	 */
219e9b11083SJames Smart 	if (ndlp->fc4_xpt_flags & NLP_WAIT_FOR_UNREG)
220e9b11083SJames Smart 		ndlp->fc4_xpt_flags &= ~(NLP_WAIT_FOR_UNREG | NVME_XPT_REGD);
221e9b11083SJames Smart 
222c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
2233fd78355SJames Smart 
224e9b11083SJames Smart 	/* On a devloss timeout event, one more put is executed provided the
225e9b11083SJames Smart 	 * NVME and SCSI rport unregister requests are complete.  If the vport
226e9b11083SJames Smart 	 * is unloading, this extra put is executed by lpfc_drop_node.
2273fd78355SJames Smart 	 */
228e9b11083SJames Smart 	if (!(ndlp->fc4_xpt_flags & fc4_xpt_flags))
229e9b11083SJames Smart 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
23001649561SJames Smart 
23101649561SJames Smart  rport_err:
2323fd78355SJames Smart 	return;
23301649561SJames Smart }
23401649561SJames Smart 
2353a8070c5SJames Smart /**
2363a8070c5SJames Smart  * lpfc_nvme_handle_lsreq - Process an unsolicited NVME LS request
2373a8070c5SJames Smart  * @phba: pointer to lpfc hba data structure.
2383a8070c5SJames Smart  * @axchg: pointer to exchange context for the NVME LS request
2393a8070c5SJames Smart  *
2403a8070c5SJames Smart  * This routine is used for processing an asychronously received NVME LS
2413a8070c5SJames Smart  * request. Any remaining validation is done and the LS is then forwarded
2423a8070c5SJames Smart  * to the nvme-fc transport via nvme_fc_rcv_ls_req().
2433a8070c5SJames Smart  *
2449aa09e98SJames Smart  * The calling sequence should be: nvme_fc_rcv_ls_req() -> (processing)
2459aa09e98SJames Smart  * -> lpfc_nvme_xmt_ls_rsp/cmp -> req->done.
2469aa09e98SJames Smart  * __lpfc_nvme_xmt_ls_rsp_cmp should free the allocated axchg.
2479aa09e98SJames Smart  *
2483a8070c5SJames Smart  * Returns 0 if LS was handled and delivered to the transport
2493a8070c5SJames Smart  * Returns 1 if LS failed to be handled and should be dropped
2503a8070c5SJames Smart  */
2513a8070c5SJames Smart int
2523a8070c5SJames Smart lpfc_nvme_handle_lsreq(struct lpfc_hba *phba,
2533a8070c5SJames Smart 			struct lpfc_async_xchg_ctx *axchg)
2543a8070c5SJames Smart {
2559aa09e98SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2569aa09e98SJames Smart 	struct lpfc_vport *vport;
2579aa09e98SJames Smart 	struct lpfc_nvme_rport *lpfc_rport;
2589aa09e98SJames Smart 	struct nvme_fc_remote_port *remoteport;
2599aa09e98SJames Smart 	struct lpfc_nvme_lport *lport;
2609aa09e98SJames Smart 	uint32_t *payload = axchg->payload;
2619aa09e98SJames Smart 	int rc;
2629aa09e98SJames Smart 
2639aa09e98SJames Smart 	vport = axchg->ndlp->vport;
2649aa09e98SJames Smart 	lpfc_rport = axchg->ndlp->nrport;
2659aa09e98SJames Smart 	if (!lpfc_rport)
2669aa09e98SJames Smart 		return -EINVAL;
2679aa09e98SJames Smart 
2689aa09e98SJames Smart 	remoteport = lpfc_rport->remoteport;
2699aa09e98SJames Smart 	if (!vport->localport)
2709aa09e98SJames Smart 		return -EINVAL;
2719aa09e98SJames Smart 
2729aa09e98SJames Smart 	lport = vport->localport->private;
2739aa09e98SJames Smart 	if (!lport)
2749aa09e98SJames Smart 		return -EINVAL;
2759aa09e98SJames Smart 
2769aa09e98SJames Smart 	rc = nvme_fc_rcv_ls_req(remoteport, &axchg->ls_rsp, axchg->payload,
2779aa09e98SJames Smart 				axchg->size);
2789aa09e98SJames Smart 
2799aa09e98SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2809aa09e98SJames Smart 			"6205 NVME Unsol rcv: sz %d rc %d: %08x %08x %08x "
2819aa09e98SJames Smart 			"%08x %08x %08x\n",
2829aa09e98SJames Smart 			axchg->size, rc,
2839aa09e98SJames Smart 			*payload, *(payload+1), *(payload+2),
2849aa09e98SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
2859aa09e98SJames Smart 
2869aa09e98SJames Smart 	if (!rc)
2879aa09e98SJames Smart 		return 0;
2889aa09e98SJames Smart #endif
2893a8070c5SJames Smart 	return 1;
2903a8070c5SJames Smart }
2913a8070c5SJames Smart 
2926514b25dSJames Smart /**
2936514b25dSJames Smart  * __lpfc_nvme_ls_req_cmp - Generic completion handler for a NVME
2946514b25dSJames Smart  *        LS request.
2956514b25dSJames Smart  * @phba: Pointer to HBA context object
2966514b25dSJames Smart  * @vport: The local port that issued the LS
2976514b25dSJames Smart  * @cmdwqe: Pointer to driver command WQE object.
2986514b25dSJames Smart  * @wcqe: Pointer to driver response CQE object.
2996514b25dSJames Smart  *
3006514b25dSJames Smart  * This function is the generic completion handler for NVME LS requests.
3016514b25dSJames Smart  * The function updates any states and statistics, calls the transport
3026514b25dSJames Smart  * ls_req done() routine, then tears down the command and buffers used
3036514b25dSJames Smart  * for the LS request.
3046514b25dSJames Smart  **/
3056514b25dSJames Smart void
3066514b25dSJames Smart __lpfc_nvme_ls_req_cmp(struct lpfc_hba *phba,  struct lpfc_vport *vport,
3076514b25dSJames Smart 			struct lpfc_iocbq *cmdwqe,
3086514b25dSJames Smart 			struct lpfc_wcqe_complete *wcqe)
3096514b25dSJames Smart {
3106514b25dSJames Smart 	struct nvmefc_ls_req *pnvme_lsreq;
3116514b25dSJames Smart 	struct lpfc_dmabuf *buf_ptr;
3126514b25dSJames Smart 	struct lpfc_nodelist *ndlp;
3136514b25dSJames Smart 	uint32_t status;
3146514b25dSJames Smart 
3156514b25dSJames Smart 	pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
3166514b25dSJames Smart 	ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
3176514b25dSJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
3186514b25dSJames Smart 
3196514b25dSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
3206514b25dSJames Smart 			 "6047 NVMEx LS REQ %px cmpl DID %x Xri: %x "
3216514b25dSJames Smart 			 "status %x reason x%x cmd:x%px lsreg:x%px bmp:x%px "
3226514b25dSJames Smart 			 "ndlp:x%px\n",
3236514b25dSJames Smart 			 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
3246514b25dSJames Smart 			 cmdwqe->sli4_xritag, status,
3256514b25dSJames Smart 			 (wcqe->parameter & 0xffff),
3266514b25dSJames Smart 			 cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
3276514b25dSJames Smart 
3286514b25dSJames Smart 	lpfc_nvmeio_data(phba, "NVMEx LS CMPL: xri x%x stat x%x parm x%x\n",
3296514b25dSJames Smart 			 cmdwqe->sli4_xritag, status, wcqe->parameter);
3306514b25dSJames Smart 
3316514b25dSJames Smart 	if (cmdwqe->context3) {
3326514b25dSJames Smart 		buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
3336514b25dSJames Smart 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3346514b25dSJames Smart 		kfree(buf_ptr);
3356514b25dSJames Smart 		cmdwqe->context3 = NULL;
3366514b25dSJames Smart 	}
3376514b25dSJames Smart 	if (pnvme_lsreq->done)
3386514b25dSJames Smart 		pnvme_lsreq->done(pnvme_lsreq, status);
3396514b25dSJames Smart 	else
340372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3416514b25dSJames Smart 				 "6046 NVMEx cmpl without done call back? "
3426514b25dSJames Smart 				 "Data %px DID %x Xri: %x status %x\n",
3436514b25dSJames Smart 				pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
3446514b25dSJames Smart 				cmdwqe->sli4_xritag, status);
3456514b25dSJames Smart 	if (ndlp) {
3466514b25dSJames Smart 		lpfc_nlp_put(ndlp);
3476514b25dSJames Smart 		cmdwqe->context1 = NULL;
3486514b25dSJames Smart 	}
3496514b25dSJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
3506514b25dSJames Smart }
3516514b25dSJames Smart 
35201649561SJames Smart static void
3536514b25dSJames Smart lpfc_nvme_ls_req_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
35401649561SJames Smart 		       struct lpfc_wcqe_complete *wcqe)
35501649561SJames Smart {
35601649561SJames Smart 	struct lpfc_vport *vport = cmdwqe->vport;
3574b056682SJames Smart 	struct lpfc_nvme_lport *lport;
35801649561SJames Smart 	uint32_t status;
35901649561SJames Smart 
36001649561SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
36166a210ffSJames Smart 
36266a85155SJames Smart 	if (vport->localport) {
36366a85155SJames Smart 		lport = (struct lpfc_nvme_lport *)vport->localport->private;
36466a210ffSJames Smart 		if (lport) {
36566a210ffSJames Smart 			atomic_inc(&lport->fc4NvmeLsCmpls);
3664b056682SJames Smart 			if (status) {
3674b056682SJames Smart 				if (bf_get(lpfc_wcqe_c_xb, wcqe))
3684b056682SJames Smart 					atomic_inc(&lport->cmpl_ls_xb);
3694b056682SJames Smart 				atomic_inc(&lport->cmpl_ls_err);
3704b056682SJames Smart 			}
37166a210ffSJames Smart 		}
37266a85155SJames Smart 	}
3734b056682SJames Smart 
3746514b25dSJames Smart 	__lpfc_nvme_ls_req_cmp(phba, vport, cmdwqe, wcqe);
37501649561SJames Smart }
37601649561SJames Smart 
37701649561SJames Smart static int
37801649561SJames Smart lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
37901649561SJames Smart 		  struct lpfc_dmabuf *inp,
38001649561SJames Smart 		  struct nvmefc_ls_req *pnvme_lsreq,
38101649561SJames Smart 		  void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
38201649561SJames Smart 			       struct lpfc_wcqe_complete *),
38301649561SJames Smart 		  struct lpfc_nodelist *ndlp, uint32_t num_entry,
38401649561SJames Smart 		  uint32_t tmo, uint8_t retry)
38501649561SJames Smart {
38601649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
387205e8240SJames Smart 	union lpfc_wqe128 *wqe;
38801649561SJames Smart 	struct lpfc_iocbq *genwqe;
38901649561SJames Smart 	struct ulp_bde64 *bpl;
39001649561SJames Smart 	struct ulp_bde64 bde;
39101649561SJames Smart 	int i, rc, xmit_len, first_len;
39201649561SJames Smart 
39301649561SJames Smart 	/* Allocate buffer for  command WQE */
39401649561SJames Smart 	genwqe = lpfc_sli_get_iocbq(phba);
39501649561SJames Smart 	if (genwqe == NULL)
39601649561SJames Smart 		return 1;
39701649561SJames Smart 
39801649561SJames Smart 	wqe = &genwqe->wqe;
399d9f492a1SJames Smart 	/* Initialize only 64 bytes */
40001649561SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
40101649561SJames Smart 
40201649561SJames Smart 	genwqe->context3 = (uint8_t *)bmp;
40301649561SJames Smart 	genwqe->iocb_flag |= LPFC_IO_NVME_LS;
40401649561SJames Smart 
40501649561SJames Smart 	/* Save for completion so we can release these resources */
40601649561SJames Smart 	genwqe->context1 = lpfc_nlp_get(ndlp);
4074430f7fdSJames Smart 	if (!genwqe->context1) {
4084430f7fdSJames Smart 		dev_warn(&phba->pcidev->dev,
4094430f7fdSJames Smart 			 "Warning: Failed node ref, not sending LS_REQ\n");
4104430f7fdSJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
4114430f7fdSJames Smart 		return 1;
4124430f7fdSJames Smart 	}
4134430f7fdSJames Smart 
41401649561SJames Smart 	genwqe->context2 = (uint8_t *)pnvme_lsreq;
41501649561SJames Smart 	/* Fill in payload, bp points to frame payload */
41601649561SJames Smart 
41701649561SJames Smart 	if (!tmo)
41801649561SJames Smart 		/* FC spec states we need 3 * ratov for CT requests */
41901649561SJames Smart 		tmo = (3 * phba->fc_ratov);
42001649561SJames Smart 
42101649561SJames Smart 	/* For this command calculate the xmit length of the request bde. */
42201649561SJames Smart 	xmit_len = 0;
42301649561SJames Smart 	first_len = 0;
42401649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
42501649561SJames Smart 	for (i = 0; i < num_entry; i++) {
42601649561SJames Smart 		bde.tus.w = bpl[i].tus.w;
42701649561SJames Smart 		if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
42801649561SJames Smart 			break;
42901649561SJames Smart 		xmit_len += bde.tus.f.bdeSize;
43001649561SJames Smart 		if (i == 0)
43101649561SJames Smart 			first_len = xmit_len;
43201649561SJames Smart 	}
43301649561SJames Smart 
43401649561SJames Smart 	genwqe->rsvd2 = num_entry;
43501649561SJames Smart 	genwqe->hba_wqidx = 0;
43601649561SJames Smart 
43701649561SJames Smart 	/* Words 0 - 2 */
43801649561SJames Smart 	wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
43901649561SJames Smart 	wqe->generic.bde.tus.f.bdeSize = first_len;
44001649561SJames Smart 	wqe->generic.bde.addrLow = bpl[0].addrLow;
44101649561SJames Smart 	wqe->generic.bde.addrHigh = bpl[0].addrHigh;
44201649561SJames Smart 
44301649561SJames Smart 	/* Word 3 */
44401649561SJames Smart 	wqe->gen_req.request_payload_len = first_len;
44501649561SJames Smart 
44601649561SJames Smart 	/* Word 4 */
44701649561SJames Smart 
44801649561SJames Smart 	/* Word 5 */
44901649561SJames Smart 	bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
45001649561SJames Smart 	bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
45101649561SJames Smart 	bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
4528b361639SJames Smart 	bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
45301649561SJames Smart 	bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
45401649561SJames Smart 
45501649561SJames Smart 	/* Word 6 */
45601649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
45701649561SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
45801649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
45901649561SJames Smart 
46001649561SJames Smart 	/* Word 7 */
461c33b1609SJames Smart 	bf_set(wqe_tmo, &wqe->gen_req.wqe_com, tmo);
46201649561SJames Smart 	bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
46301649561SJames Smart 	bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
46401649561SJames Smart 	bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
46501649561SJames Smart 
46601649561SJames Smart 	/* Word 8 */
46701649561SJames Smart 	wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
46801649561SJames Smart 
46901649561SJames Smart 	/* Word 9 */
47001649561SJames Smart 	bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
47101649561SJames Smart 
47201649561SJames Smart 	/* Word 10 */
47301649561SJames Smart 	bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
47401649561SJames Smart 	bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
47501649561SJames Smart 	bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
47601649561SJames Smart 	bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
47701649561SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
47801649561SJames Smart 
47901649561SJames Smart 	/* Word 11 */
48001649561SJames Smart 	bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
48101649561SJames Smart 	bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
48201649561SJames Smart 
48301649561SJames Smart 
48401649561SJames Smart 	/* Issue GEN REQ WQE for NPORT <did> */
48501649561SJames Smart 	genwqe->wqe_cmpl = cmpl;
48601649561SJames Smart 	genwqe->iocb_cmpl = NULL;
48701649561SJames Smart 	genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
48801649561SJames Smart 	genwqe->vport = vport;
48901649561SJames Smart 	genwqe->retry = retry;
49001649561SJames Smart 
491bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  XMIT: xri x%x iotag x%x to x%06x\n",
492bd2cdd5eSJames Smart 			 genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
493bd2cdd5eSJames Smart 
4941fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], genwqe);
495cd22d605SDick Kennedy 	if (rc) {
496372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
49701649561SJames Smart 				 "6045 Issue GEN REQ WQE to NPORT x%x "
4986514b25dSJames Smart 				 "Data: x%x x%x  rc x%x\n",
49901649561SJames Smart 				 ndlp->nlp_DID, genwqe->iotag,
5006514b25dSJames Smart 				 vport->port_state, rc);
501e9b11083SJames Smart 		lpfc_nlp_put(ndlp);
50201649561SJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
50301649561SJames Smart 		return 1;
50401649561SJames Smart 	}
5056514b25dSJames Smart 
5066514b25dSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_ELS,
5076514b25dSJames Smart 			 "6050 Issue GEN REQ WQE to NPORT x%x "
5086514b25dSJames Smart 			 "Data: oxid: x%x state: x%x wq:x%px lsreq:x%px "
5096514b25dSJames Smart 			 "bmp:x%px xmit:%d 1st:%d\n",
5106514b25dSJames Smart 			 ndlp->nlp_DID, genwqe->sli4_xritag,
5116514b25dSJames Smart 			 vport->port_state,
5126514b25dSJames Smart 			 genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
51301649561SJames Smart 	return 0;
51401649561SJames Smart }
51501649561SJames Smart 
5166514b25dSJames Smart 
51701649561SJames Smart /**
5186514b25dSJames Smart  * __lpfc_nvme_ls_req - Generic service routine to issue an NVME LS request
5196514b25dSJames Smart  * @vport: The local port issuing the LS
5206514b25dSJames Smart  * @ndlp: The remote port to send the LS to
5216514b25dSJames Smart  * @pnvme_lsreq: Pointer to LS request structure from the transport
52209d99705SLee Jones  * @gen_req_cmp: Completion call-back
52301649561SJames Smart  *
5246514b25dSJames Smart  * Routine validates the ndlp, builds buffers and sends a GEN_REQUEST
5256514b25dSJames Smart  * WQE to perform the LS operation.
52601649561SJames Smart  *
52701649561SJames Smart  * Return value :
52801649561SJames Smart  *   0 - Success
5296514b25dSJames Smart  *   non-zero: various error codes, in form of -Exxx
53001649561SJames Smart  **/
5316514b25dSJames Smart int
5326514b25dSJames Smart __lpfc_nvme_ls_req(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5336514b25dSJames Smart 		      struct nvmefc_ls_req *pnvme_lsreq,
5346514b25dSJames Smart 		      void (*gen_req_cmp)(struct lpfc_hba *phba,
5356514b25dSJames Smart 				struct lpfc_iocbq *cmdwqe,
5366514b25dSJames Smart 				struct lpfc_wcqe_complete *wcqe))
53701649561SJames Smart {
53801649561SJames Smart 	struct lpfc_dmabuf *bmp;
5396514b25dSJames Smart 	struct ulp_bde64 *bpl;
5406514b25dSJames Smart 	int ret;
541ba43c4d0SJames Smart 	uint16_t ntype, nstate;
54201649561SJames Smart 
543307e3380SJames Smart 	if (!ndlp) {
544372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5456514b25dSJames Smart 				 "6051 NVMEx LS REQ: Bad NDLP x%px, Failing "
5466514b25dSJames Smart 				 "LS Req\n",
5476514b25dSJames Smart 				 ndlp);
548ba43c4d0SJames Smart 		return -ENODEV;
549ba43c4d0SJames Smart 	}
550ba43c4d0SJames Smart 
551ba43c4d0SJames Smart 	ntype = ndlp->nlp_type;
552ba43c4d0SJames Smart 	nstate = ndlp->nlp_state;
553ba43c4d0SJames Smart 	if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
554ba43c4d0SJames Smart 	    (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
555372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5566514b25dSJames Smart 				 "6088 NVMEx LS REQ: Fail DID x%06x not "
5576514b25dSJames Smart 				 "ready for IO. Type x%x, State x%x\n",
5586514b25dSJames Smart 				 ndlp->nlp_DID, ntype, nstate);
559ba43c4d0SJames Smart 		return -ENODEV;
56001649561SJames Smart 	}
5616514b25dSJames Smart 
5628c65830aSJames Smart 	if (!vport->phba->sli4_hba.nvmels_wq)
5638c65830aSJames Smart 		return -ENOMEM;
5648c65830aSJames Smart 
5656514b25dSJames Smart 	/*
5666514b25dSJames Smart 	 * there are two dma buf in the request, actually there is one and
5676514b25dSJames Smart 	 * the second one is just the start address + cmd size.
5686514b25dSJames Smart 	 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
5696514b25dSJames Smart 	 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
5706514b25dSJames Smart 	 * because the nvem layer owns the data bufs.
5716514b25dSJames Smart 	 * We do not have to break these packets open, we don't care what is
5726514b25dSJames Smart 	 * in them. And we do not have to look at the resonse data, we only
5736514b25dSJames Smart 	 * care that we got a response. All of the caring is going to happen
5746514b25dSJames Smart 	 * in the nvme-fc layer.
5756514b25dSJames Smart 	 */
5766514b25dSJames Smart 
5776514b25dSJames Smart 	bmp = kmalloc(sizeof(*bmp), GFP_KERNEL);
57801649561SJames Smart 	if (!bmp) {
579372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5806514b25dSJames Smart 				 "6044 NVMEx LS REQ: Could not alloc LS buf "
5816514b25dSJames Smart 				 "for DID %x\n",
5826514b25dSJames Smart 				 ndlp->nlp_DID);
5836514b25dSJames Smart 		return -ENOMEM;
58401649561SJames Smart 	}
5856514b25dSJames Smart 
58601649561SJames Smart 	bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
58701649561SJames Smart 	if (!bmp->virt) {
588372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5896514b25dSJames Smart 				 "6042 NVMEx LS REQ: Could not alloc mbuf "
5906514b25dSJames Smart 				 "for DID %x\n",
5916514b25dSJames Smart 				 ndlp->nlp_DID);
59201649561SJames Smart 		kfree(bmp);
5936514b25dSJames Smart 		return -ENOMEM;
59401649561SJames Smart 	}
5956514b25dSJames Smart 
5966514b25dSJames Smart 	INIT_LIST_HEAD(&bmp->list);
5976514b25dSJames Smart 
59801649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
59901649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
60001649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
60101649561SJames Smart 	bpl->tus.f.bdeFlags = 0;
60201649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
60301649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
60401649561SJames Smart 	bpl++;
60501649561SJames Smart 
60601649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
60701649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
60801649561SJames Smart 	bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
60901649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
61001649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
61101649561SJames Smart 
61201649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
6136514b25dSJames Smart 			"6149 NVMEx LS REQ: Issue to DID 0x%06x lsreq x%px, "
6146514b25dSJames Smart 			"rqstlen:%d rsplen:%d %pad %pad\n",
6156514b25dSJames Smart 			ndlp->nlp_DID, pnvme_lsreq, pnvme_lsreq->rqstlen,
616825c6abbSArnd Bergmann 			pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
617825c6abbSArnd Bergmann 			&pnvme_lsreq->rspdma);
61801649561SJames Smart 
61901649561SJames Smart 	ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
6206514b25dSJames Smart 				pnvme_lsreq, gen_req_cmp, ndlp, 2,
621c33b1609SJames Smart 				pnvme_lsreq->timeout, 0);
62201649561SJames Smart 	if (ret != WQE_SUCCESS) {
623372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6246514b25dSJames Smart 				 "6052 NVMEx REQ: EXIT. issue ls wqe failed "
6256514b25dSJames Smart 				 "lsreq x%px Status %x DID %x\n",
6266514b25dSJames Smart 				 pnvme_lsreq, ret, ndlp->nlp_DID);
62701649561SJames Smart 		lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
62801649561SJames Smart 		kfree(bmp);
6296514b25dSJames Smart 		return -EIO;
63001649561SJames Smart 	}
63101649561SJames Smart 
6326514b25dSJames Smart 	return 0;
6336514b25dSJames Smart }
6346514b25dSJames Smart 
6356514b25dSJames Smart /**
6366514b25dSJames Smart  * lpfc_nvme_ls_req - Issue an NVME Link Service request
63709d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from.
6383e749170SLee Jones  * @pnvme_rport: Transport remoteport that LS is to be sent to.
63909d99705SLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
6406514b25dSJames Smart  *
6416514b25dSJames Smart  * Driver registers this routine to handle any link service request
6426514b25dSJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
6436514b25dSJames Smart  *
6446514b25dSJames Smart  * Return value :
6456514b25dSJames Smart  *   0 - Success
6466514b25dSJames Smart  *   non-zero: various error codes, in form of -Exxx
6476514b25dSJames Smart  **/
6486514b25dSJames Smart static int
6496514b25dSJames Smart lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
6506514b25dSJames Smart 		 struct nvme_fc_remote_port *pnvme_rport,
6516514b25dSJames Smart 		 struct nvmefc_ls_req *pnvme_lsreq)
6526514b25dSJames Smart {
6536514b25dSJames Smart 	struct lpfc_nvme_lport *lport;
6546514b25dSJames Smart 	struct lpfc_nvme_rport *rport;
6556514b25dSJames Smart 	struct lpfc_vport *vport;
6566514b25dSJames Smart 	int ret;
6576514b25dSJames Smart 
6586514b25dSJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
6596514b25dSJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
6606514b25dSJames Smart 	if (unlikely(!lport) || unlikely(!rport))
6616514b25dSJames Smart 		return -EINVAL;
6626514b25dSJames Smart 
6636514b25dSJames Smart 	vport = lport->vport;
6646514b25dSJames Smart 	if (vport->load_flag & FC_UNLOADING)
6656514b25dSJames Smart 		return -ENODEV;
6666514b25dSJames Smart 
6676514b25dSJames Smart 	atomic_inc(&lport->fc4NvmeLsRequests);
6686514b25dSJames Smart 
6696514b25dSJames Smart 	ret = __lpfc_nvme_ls_req(vport, rport->ndlp, pnvme_lsreq,
6706514b25dSJames Smart 				 lpfc_nvme_ls_req_cmp);
6716514b25dSJames Smart 	if (ret)
6726514b25dSJames Smart 		atomic_inc(&lport->xmt_ls_err);
6736514b25dSJames Smart 
67401649561SJames Smart 	return ret;
67501649561SJames Smart }
67601649561SJames Smart 
67701649561SJames Smart /**
678e96a22b0SJames Smart  * __lpfc_nvme_ls_abort - Generic service routine to abort a prior
679e96a22b0SJames Smart  *         NVME LS request
680e96a22b0SJames Smart  * @vport: The local port that issued the LS
681e96a22b0SJames Smart  * @ndlp: The remote port the LS was sent to
682e96a22b0SJames Smart  * @pnvme_lsreq: Pointer to LS request structure from the transport
68301649561SJames Smart  *
684e96a22b0SJames Smart  * The driver validates the ndlp, looks for the LS, and aborts the
685e96a22b0SJames Smart  * LS if found.
68601649561SJames Smart  *
687e96a22b0SJames Smart  * Returns:
688e96a22b0SJames Smart  * 0 : if LS found and aborted
689e96a22b0SJames Smart  * non-zero: various error conditions in form -Exxx
690e96a22b0SJames Smart  **/
691e96a22b0SJames Smart int
692e96a22b0SJames Smart __lpfc_nvme_ls_abort(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
693e96a22b0SJames Smart 			struct nvmefc_ls_req *pnvme_lsreq)
694e96a22b0SJames Smart {
695e96a22b0SJames Smart 	struct lpfc_hba *phba = vport->phba;
696e96a22b0SJames Smart 	struct lpfc_sli_ring *pring;
697e96a22b0SJames Smart 	struct lpfc_iocbq *wqe, *next_wqe;
698e96a22b0SJames Smart 	bool foundit = false;
699e96a22b0SJames Smart 
700e96a22b0SJames Smart 	if (!ndlp) {
701372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
702e96a22b0SJames Smart 				"6049 NVMEx LS REQ Abort: Bad NDLP x%px DID "
703e96a22b0SJames Smart 				"x%06x, Failing LS Req\n",
704e96a22b0SJames Smart 				ndlp, ndlp ? ndlp->nlp_DID : 0);
705e96a22b0SJames Smart 		return -EINVAL;
706e96a22b0SJames Smart 	}
707e96a22b0SJames Smart 
708e96a22b0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_NVME_ABTS,
709e96a22b0SJames Smart 			 "6040 NVMEx LS REQ Abort: Issue LS_ABORT for lsreq "
710e96a22b0SJames Smart 			 "x%p rqstlen:%d rsplen:%d %pad %pad\n",
711e96a22b0SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
712e96a22b0SJames Smart 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
713e96a22b0SJames Smart 			 &pnvme_lsreq->rspdma);
714e96a22b0SJames Smart 
715e96a22b0SJames Smart 	/*
716e96a22b0SJames Smart 	 * Lock the ELS ring txcmplq and look for the wqe that matches
717e96a22b0SJames Smart 	 * this ELS. If found, issue an abort on the wqe.
718e96a22b0SJames Smart 	 */
719e96a22b0SJames Smart 	pring = phba->sli4_hba.nvmels_wq->pring;
720e96a22b0SJames Smart 	spin_lock_irq(&phba->hbalock);
721e96a22b0SJames Smart 	spin_lock(&pring->ring_lock);
722e96a22b0SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
723e96a22b0SJames Smart 		if (wqe->context2 == pnvme_lsreq) {
724e96a22b0SJames Smart 			wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
725e96a22b0SJames Smart 			foundit = true;
726e96a22b0SJames Smart 			break;
727e96a22b0SJames Smart 		}
728e96a22b0SJames Smart 	}
729e96a22b0SJames Smart 	spin_unlock(&pring->ring_lock);
730e96a22b0SJames Smart 
731e96a22b0SJames Smart 	if (foundit)
732db7531d2SJames Smart 		lpfc_sli_issue_abort_iotag(phba, pring, wqe, NULL);
733e96a22b0SJames Smart 	spin_unlock_irq(&phba->hbalock);
734e96a22b0SJames Smart 
735e96a22b0SJames Smart 	if (foundit)
736e96a22b0SJames Smart 		return 0;
737e96a22b0SJames Smart 
738e96a22b0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_NVME_ABTS,
739e96a22b0SJames Smart 			 "6213 NVMEx LS REQ Abort: Unable to locate req x%p\n",
740e96a22b0SJames Smart 			 pnvme_lsreq);
7416b6e8963SJames Smart 	return -EINVAL;
742e96a22b0SJames Smart }
743e96a22b0SJames Smart 
7449aa09e98SJames Smart static int
7459aa09e98SJames Smart lpfc_nvme_xmt_ls_rsp(struct nvme_fc_local_port *localport,
7469aa09e98SJames Smart 		     struct nvme_fc_remote_port *remoteport,
7479aa09e98SJames Smart 		     struct nvmefc_ls_rsp *ls_rsp)
7489aa09e98SJames Smart {
7499aa09e98SJames Smart 	struct lpfc_async_xchg_ctx *axchg =
7509aa09e98SJames Smart 		container_of(ls_rsp, struct lpfc_async_xchg_ctx, ls_rsp);
7519aa09e98SJames Smart 	struct lpfc_nvme_lport *lport;
7529aa09e98SJames Smart 	int rc;
7539aa09e98SJames Smart 
7549aa09e98SJames Smart 	if (axchg->phba->pport->load_flag & FC_UNLOADING)
7559aa09e98SJames Smart 		return -ENODEV;
7569aa09e98SJames Smart 
7579aa09e98SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
7589aa09e98SJames Smart 
7599aa09e98SJames Smart 	rc = __lpfc_nvme_xmt_ls_rsp(axchg, ls_rsp, __lpfc_nvme_xmt_ls_rsp_cmp);
7609aa09e98SJames Smart 
7619aa09e98SJames Smart 	if (rc) {
7629aa09e98SJames Smart 		/*
7639aa09e98SJames Smart 		 * unless the failure is due to having already sent
7649aa09e98SJames Smart 		 * the response, an abort will be generated for the
7659aa09e98SJames Smart 		 * exchange if the rsp can't be sent.
7669aa09e98SJames Smart 		 */
7679aa09e98SJames Smart 		if (rc != -EALREADY)
7689aa09e98SJames Smart 			atomic_inc(&lport->xmt_ls_abort);
7699aa09e98SJames Smart 		return rc;
7709aa09e98SJames Smart 	}
7719aa09e98SJames Smart 
7729aa09e98SJames Smart 	return 0;
7739aa09e98SJames Smart }
7749aa09e98SJames Smart 
775e96a22b0SJames Smart /**
776e96a22b0SJames Smart  * lpfc_nvme_ls_abort - Abort a prior NVME LS request
77709d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from.
77809d99705SLee Jones  * @pnvme_rport: Transport remoteport that LS is to be sent to.
77909d99705SLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
780e96a22b0SJames Smart  *
781e96a22b0SJames Smart  * Driver registers this routine to abort a NVME LS request that is
782e96a22b0SJames Smart  * in progress (from the transports perspective).
78301649561SJames Smart  **/
78401649561SJames Smart static void
78501649561SJames Smart lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
78601649561SJames Smart 		   struct nvme_fc_remote_port *pnvme_rport,
78701649561SJames Smart 		   struct nvmefc_ls_req *pnvme_lsreq)
78801649561SJames Smart {
78901649561SJames Smart 	struct lpfc_nvme_lport *lport;
79001649561SJames Smart 	struct lpfc_vport *vport;
79101649561SJames Smart 	struct lpfc_nodelist *ndlp;
792e96a22b0SJames Smart 	int ret;
79301649561SJames Smart 
79401649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
79566a210ffSJames Smart 	if (unlikely(!lport))
79666a210ffSJames Smart 		return;
79701649561SJames Smart 	vport = lport->vport;
79801649561SJames Smart 
7993386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
8003386f4bdSJames Smart 		return;
8013386f4bdSJames Smart 
80201649561SJames Smart 	ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
80301649561SJames Smart 
804e96a22b0SJames Smart 	ret = __lpfc_nvme_ls_abort(vport, ndlp, pnvme_lsreq);
805e96a22b0SJames Smart 	if (!ret)
8064b056682SJames Smart 		atomic_inc(&lport->xmt_ls_abort);
80701649561SJames Smart }
80801649561SJames Smart 
80901649561SJames Smart /* Fix up the existing sgls for NVME IO. */
8105fd11085SJames Smart static inline void
81101649561SJames Smart lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
812c490850aSJames Smart 		       struct lpfc_io_buf *lpfc_ncmd,
81301649561SJames Smart 		       struct nvmefc_fcp_req *nCmd)
81401649561SJames Smart {
8154e565cf0SJames Smart 	struct lpfc_hba  *phba = vport->phba;
81601649561SJames Smart 	struct sli4_sge *sgl;
81701649561SJames Smart 	union lpfc_wqe128 *wqe;
81801649561SJames Smart 	uint32_t *wptr, *dptr;
81901649561SJames Smart 
82001649561SJames Smart 	/*
8214e565cf0SJames Smart 	 * Get a local pointer to the built-in wqe and correct
8224e565cf0SJames Smart 	 * the cmd size to match NVME's 96 bytes and fix
8234e565cf0SJames Smart 	 * the dma address.
8244e565cf0SJames Smart 	 */
8254e565cf0SJames Smart 
826205e8240SJames Smart 	wqe = &lpfc_ncmd->cur_iocbq.wqe;
8274e565cf0SJames Smart 
8284e565cf0SJames Smart 	/*
82901649561SJames Smart 	 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
83001649561SJames Smart 	 * match NVME.  NVME sends 96 bytes. Also, use the
83101649561SJames Smart 	 * nvme commands command and response dma addresses
83201649561SJames Smart 	 * rather than the virtual memory to ease the restore
83301649561SJames Smart 	 * operation.
83401649561SJames Smart 	 */
8350794d601SJames Smart 	sgl = lpfc_ncmd->dma_sgl;
83601649561SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
8374e565cf0SJames Smart 	if (phba->cfg_nvme_embed_cmd) {
8384e565cf0SJames Smart 		sgl->addr_hi = 0;
8394e565cf0SJames Smart 		sgl->addr_lo = 0;
8404e565cf0SJames Smart 
8414e565cf0SJames Smart 		/* Word 0-2 - NVME CMND IU (embedded payload) */
8424e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
8434e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeSize = 56;
8444e565cf0SJames Smart 		wqe->generic.bde.addrHigh = 0;
8454e565cf0SJames Smart 		wqe->generic.bde.addrLow =  64;  /* Word 16 */
8464e565cf0SJames Smart 
8475fd11085SJames Smart 		/* Word 10  - dbde is 0, wqes is 1 in template */
84801649561SJames Smart 
84901649561SJames Smart 		/*
85001649561SJames Smart 		 * Embed the payload in the last half of the WQE
85101649561SJames Smart 		 * WQE words 16-30 get the NVME CMD IU payload
85201649561SJames Smart 		 *
853b06a622fSJames Smart 		 * WQE words 16-19 get payload Words 1-4
85401649561SJames Smart 		 * WQE words 20-21 get payload Words 6-7
85501649561SJames Smart 		 * WQE words 22-29 get payload Words 16-23
85601649561SJames Smart 		 */
857b06a622fSJames Smart 		wptr = &wqe->words[16];  /* WQE ptr */
85801649561SJames Smart 		dptr = (uint32_t *)nCmd->cmdaddr;  /* payload ptr */
859b06a622fSJames Smart 		dptr++;			/* Skip Word 0 in payload */
86001649561SJames Smart 
861b06a622fSJames Smart 		*wptr++ = *dptr++;	/* Word 1 */
86201649561SJames Smart 		*wptr++ = *dptr++;	/* Word 2 */
86301649561SJames Smart 		*wptr++ = *dptr++;	/* Word 3 */
86401649561SJames Smart 		*wptr++ = *dptr++;	/* Word 4 */
86501649561SJames Smart 		dptr++;			/* Skip Word 5 in payload */
86601649561SJames Smart 		*wptr++ = *dptr++;	/* Word 6 */
86701649561SJames Smart 		*wptr++ = *dptr++;	/* Word 7 */
86801649561SJames Smart 		dptr += 8;		/* Skip Words 8-15 in payload */
86901649561SJames Smart 		*wptr++ = *dptr++;	/* Word 16 */
87001649561SJames Smart 		*wptr++ = *dptr++;	/* Word 17 */
87101649561SJames Smart 		*wptr++ = *dptr++;	/* Word 18 */
87201649561SJames Smart 		*wptr++ = *dptr++;	/* Word 19 */
87301649561SJames Smart 		*wptr++ = *dptr++;	/* Word 20 */
87401649561SJames Smart 		*wptr++ = *dptr++;	/* Word 21 */
87501649561SJames Smart 		*wptr++ = *dptr++;	/* Word 22 */
87601649561SJames Smart 		*wptr   = *dptr;	/* Word 23 */
8775fd11085SJames Smart 	} else {
8785fd11085SJames Smart 		sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->cmddma));
8795fd11085SJames Smart 		sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->cmddma));
8805fd11085SJames Smart 
8815fd11085SJames Smart 		/* Word 0-2 - NVME CMND IU Inline BDE */
8825fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
8835fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeSize = nCmd->cmdlen;
8845fd11085SJames Smart 		wqe->generic.bde.addrHigh = sgl->addr_hi;
8855fd11085SJames Smart 		wqe->generic.bde.addrLow =  sgl->addr_lo;
8865fd11085SJames Smart 
8875fd11085SJames Smart 		/* Word 10 */
8885fd11085SJames Smart 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
8895fd11085SJames Smart 		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
8905fd11085SJames Smart 	}
8915fd11085SJames Smart 
8925fd11085SJames Smart 	sgl++;
8935fd11085SJames Smart 
8945fd11085SJames Smart 	/* Setup the physical region for the FCP RSP */
8955fd11085SJames Smart 	sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
8965fd11085SJames Smart 	sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
8975fd11085SJames Smart 	sgl->word2 = le32_to_cpu(sgl->word2);
8985fd11085SJames Smart 	if (nCmd->sg_cnt)
8995fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
9005fd11085SJames Smart 	else
9015fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 1);
9025fd11085SJames Smart 	sgl->word2 = cpu_to_le32(sgl->word2);
9035fd11085SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->rsplen);
90401649561SJames Smart }
90501649561SJames Smart 
906bd2cdd5eSJames Smart 
90709d99705SLee Jones /*
90801649561SJames Smart  * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
90901649561SJames Smart  *
91001649561SJames Smart  * Driver registers this routine as it io request handler.  This
91101649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
91201649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
91301649561SJames Smart  *
91401649561SJames Smart  * Return value :
91501649561SJames Smart  *   0 - Success
91601649561SJames Smart  *   TODO: What are the failure codes.
91701649561SJames Smart  **/
91801649561SJames Smart static void
91901649561SJames Smart lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
92001649561SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
92101649561SJames Smart {
922c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd =
923c490850aSJames Smart 		(struct lpfc_io_buf *)pwqeIn->context1;
92401649561SJames Smart 	struct lpfc_vport *vport = pwqeIn->vport;
92501649561SJames Smart 	struct nvmefc_fcp_req *nCmd;
92601649561SJames Smart 	struct nvme_fc_ersp_iu *ep;
92701649561SJames Smart 	struct nvme_fc_cmd_iu *cp;
92801649561SJames Smart 	struct lpfc_nodelist *ndlp;
929bbe3012bSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
9304b056682SJames Smart 	struct lpfc_nvme_lport *lport;
931352b205aSArnd Bergmann 	uint32_t code, status, idx;
93201649561SJames Smart 	uint16_t cid, sqhd, data;
93301649561SJames Smart 	uint32_t *ptr;
934840eda96SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
935840eda96SJames Smart 	int cpu;
936840eda96SJames Smart #endif
93701649561SJames Smart 
93801649561SJames Smart 	/* Sanity check on return of outstanding command */
9394d5e789aSJames Smart 	if (!lpfc_ncmd) {
940372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9414d5e789aSJames Smart 				 "6071 Null lpfc_ncmd pointer. No "
9424d5e789aSJames Smart 				 "release, skip completion\n");
9434d5e789aSJames Smart 		return;
9444d5e789aSJames Smart 	}
9454d5e789aSJames Smart 
946c2017260SJames Smart 	/* Guard against abort handler being called at same time */
947c2017260SJames Smart 	spin_lock(&lpfc_ncmd->buf_lock);
948c2017260SJames Smart 
949c2017260SJames Smart 	if (!lpfc_ncmd->nvmeCmd) {
950c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
951372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
95232350664SJames Smart 				 "6066 Missing cmpl ptrs: lpfc_ncmd x%px, "
95332350664SJames Smart 				 "nvmeCmd x%px\n",
9540b05e9feSJames Smart 				 lpfc_ncmd, lpfc_ncmd->nvmeCmd);
9554d5e789aSJames Smart 
9564d5e789aSJames Smart 		/* Release the lpfc_ncmd regardless of the missing elements. */
9574d5e789aSJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
95801649561SJames Smart 		return;
95901649561SJames Smart 	}
96001649561SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
9614b056682SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
96266a210ffSJames Smart 
9634c47efc1SJames Smart 	idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
9644c47efc1SJames Smart 	phba->sli4_hba.hdwq[idx].nvme_cstat.io_cmpls++;
9654c47efc1SJames Smart 
9661df09449SJames Smart 	if (unlikely(status && vport->localport)) {
9674b056682SJames Smart 		lport = (struct lpfc_nvme_lport *)vport->localport->private;
9681df09449SJames Smart 		if (lport) {
9694b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
9704b056682SJames Smart 				atomic_inc(&lport->cmpl_fcp_xb);
9714b056682SJames Smart 			atomic_inc(&lport->cmpl_fcp_err);
9724b056682SJames Smart 		}
97366a210ffSJames Smart 	}
97401649561SJames Smart 
975bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
976bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
9774b056682SJames Smart 			 status, wcqe->parameter);
97801649561SJames Smart 	/*
97901649561SJames Smart 	 * Catch race where our node has transitioned, but the
98001649561SJames Smart 	 * transport is still transitioning.
98101649561SJames Smart 	 */
9820b05e9feSJames Smart 	ndlp = lpfc_ncmd->ndlp;
983307e3380SJames Smart 	if (!ndlp) {
984372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
98501649561SJames Smart 				 "6062 Ignoring NVME cmpl.  No ndlp\n");
98601649561SJames Smart 		goto out_err;
98701649561SJames Smart 	}
98801649561SJames Smart 
98901649561SJames Smart 	code = bf_get(lpfc_wcqe_c_code, wcqe);
99001649561SJames Smart 	if (code == CQE_CODE_NVME_ERSP) {
99101649561SJames Smart 		/* For this type of CQE, we need to rebuild the rsp */
99201649561SJames Smart 		ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
99301649561SJames Smart 
99401649561SJames Smart 		/*
99501649561SJames Smart 		 * Get Command Id from cmd to plug into response. This
99601649561SJames Smart 		 * code is not needed in the next NVME Transport drop.
99701649561SJames Smart 		 */
99801649561SJames Smart 		cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
99901649561SJames Smart 		cid = cp->sqe.common.command_id;
100001649561SJames Smart 
100101649561SJames Smart 		/*
100201649561SJames Smart 		 * RSN is in CQE word 2
100301649561SJames Smart 		 * SQHD is in CQE Word 3 bits 15:0
100401649561SJames Smart 		 * Cmd Specific info is in CQE Word 1
100501649561SJames Smart 		 * and in CQE Word 0 bits 15:0
100601649561SJames Smart 		 */
100701649561SJames Smart 		sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
100801649561SJames Smart 
100901649561SJames Smart 		/* Now lets build the NVME ERSP IU */
101001649561SJames Smart 		ep->iu_len = cpu_to_be16(8);
101101649561SJames Smart 		ep->rsn = wcqe->parameter;
101201649561SJames Smart 		ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
101301649561SJames Smart 		ep->rsvd12 = 0;
101401649561SJames Smart 		ptr = (uint32_t *)&ep->cqe.result.u64;
101501649561SJames Smart 		*ptr++ = wcqe->total_data_placed;
101601649561SJames Smart 		data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
101701649561SJames Smart 		*ptr = (uint32_t)data;
101801649561SJames Smart 		ep->cqe.sq_head = sqhd;
101901649561SJames Smart 		ep->cqe.sq_id =  nCmd->sqid;
102001649561SJames Smart 		ep->cqe.command_id = cid;
102101649561SJames Smart 		ep->cqe.status = 0;
102201649561SJames Smart 
102301649561SJames Smart 		lpfc_ncmd->status = IOSTAT_SUCCESS;
102401649561SJames Smart 		lpfc_ncmd->result = 0;
102501649561SJames Smart 		nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
102601649561SJames Smart 		nCmd->transferred_length = nCmd->payload_length;
102701649561SJames Smart 	} else {
10284b056682SJames Smart 		lpfc_ncmd->status = (status & LPFC_IOCB_STATUS_MASK);
1029952c303bSDick Kennedy 		lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
103001649561SJames Smart 
103101649561SJames Smart 		/* For NVME, the only failure path that results in an
103201649561SJames Smart 		 * IO error is when the adapter rejects it.  All other
103301649561SJames Smart 		 * conditions are a success case and resolved by the
103401649561SJames Smart 		 * transport.
103501649561SJames Smart 		 * IOSTAT_FCP_RSP_ERROR means:
103601649561SJames Smart 		 * 1. Length of data received doesn't match total
103701649561SJames Smart 		 *    transfer length in WQE
103801649561SJames Smart 		 * 2. If the RSP payload does NOT match these cases:
103901649561SJames Smart 		 *    a. RSP length 12/24 bytes and all zeros
104001649561SJames Smart 		 *    b. NVME ERSP
104101649561SJames Smart 		 */
104201649561SJames Smart 		switch (lpfc_ncmd->status) {
104301649561SJames Smart 		case IOSTAT_SUCCESS:
104401649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
104501649561SJames Smart 			nCmd->rcv_rsplen = 0;
104601649561SJames Smart 			nCmd->status = 0;
104701649561SJames Smart 			break;
104801649561SJames Smart 		case IOSTAT_FCP_RSP_ERROR:
104901649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
105001649561SJames Smart 			nCmd->rcv_rsplen = wcqe->parameter;
105101649561SJames Smart 			nCmd->status = 0;
105201649561SJames Smart 			/* Sanity check */
105301649561SJames Smart 			if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
105401649561SJames Smart 				break;
1055372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
105601649561SJames Smart 					 "6081 NVME Completion Protocol Error: "
105786c67379SJames Smart 					 "xri %x status x%x result x%x "
105886c67379SJames Smart 					 "placed x%x\n",
105986c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
106001649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
106101649561SJames Smart 					 wcqe->total_data_placed);
106201649561SJames Smart 			break;
1063952c303bSDick Kennedy 		case IOSTAT_LOCAL_REJECT:
1064952c303bSDick Kennedy 			/* Let fall through to set command final state. */
1065952c303bSDick Kennedy 			if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
1066952c303bSDick Kennedy 				lpfc_printf_vlog(vport, KERN_INFO,
1067952c303bSDick Kennedy 					 LOG_NVME_IOERR,
106832350664SJames Smart 					 "6032 Delay Aborted cmd x%px "
106932350664SJames Smart 					 "nvme cmd x%px, xri x%x, "
1070952c303bSDick Kennedy 					 "xb %d\n",
1071952c303bSDick Kennedy 					 lpfc_ncmd, nCmd,
1072952c303bSDick Kennedy 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
1073952c303bSDick Kennedy 					 bf_get(lpfc_wcqe_c_xb, wcqe));
1074df561f66SGustavo A. R. Silva 			fallthrough;
107501649561SJames Smart 		default:
107601649561SJames Smart out_err:
1077e3246a12SJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
107886c67379SJames Smart 					 "6072 NVME Completion Error: xri %x "
10792c013a3aSJames Smart 					 "status x%x result x%x [x%x] "
10802c013a3aSJames Smart 					 "placed x%x\n",
108186c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
108201649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
10832c013a3aSJames Smart 					 wcqe->parameter,
108401649561SJames Smart 					 wcqe->total_data_placed);
108501649561SJames Smart 			nCmd->transferred_length = 0;
108601649561SJames Smart 			nCmd->rcv_rsplen = 0;
10878e009ce8SJames Smart 			nCmd->status = NVME_SC_INTERNAL;
108801649561SJames Smart 		}
108901649561SJames Smart 	}
109001649561SJames Smart 
109101649561SJames Smart 	/* pick up SLI4 exhange busy condition */
109201649561SJames Smart 	if (bf_get(lpfc_wcqe_c_xb, wcqe))
109301649561SJames Smart 		lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
109401649561SJames Smart 	else
109501649561SJames Smart 		lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
109601649561SJames Smart 
109701649561SJames Smart 	/* Update stats and complete the IO.  There is
109801649561SJames Smart 	 * no need for dma unprep because the nvme_transport
109901649561SJames Smart 	 * owns the dma address.
110001649561SJames Smart 	 */
1101bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1102c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start) {
1103bd2cdd5eSJames Smart 		lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
11042fcbc569SJames Smart 		lpfc_ncmd->ts_data_io = ktime_get_ns();
11052fcbc569SJames Smart 		phba->ktime_last_cmd = lpfc_ncmd->ts_data_io;
11062fcbc569SJames Smart 		lpfc_io_ktime(phba, lpfc_ncmd);
1107bd2cdd5eSJames Smart 	}
1108840eda96SJames Smart 	if (unlikely(phba->hdwqstat_on & LPFC_CHECK_NVME_IO)) {
1109d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
1110840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
111163df6d63SJames Smart 		if (lpfc_ncmd->cpu != cpu)
111263df6d63SJames Smart 			lpfc_printf_vlog(vport,
111363df6d63SJames Smart 					 KERN_INFO, LOG_NVME_IOERR,
1114bd2cdd5eSJames Smart 					 "6701 CPU Check cmpl: "
1115bd2cdd5eSJames Smart 					 "cpu %d expect %d\n",
111663df6d63SJames Smart 					 cpu, lpfc_ncmd->cpu);
1117bd2cdd5eSJames Smart 	}
1118bd2cdd5eSJames Smart #endif
1119952c303bSDick Kennedy 
1120952c303bSDick Kennedy 	/* NVME targets need completion held off until the abort exchange
1121add9d6beSJames Smart 	 * completes unless the NVME Rport is getting unregistered.
1122952c303bSDick Kennedy 	 */
1123add9d6beSJames Smart 
11243fd78355SJames Smart 	if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
112591455b85SJames Smart 		freqpriv = nCmd->private;
112691455b85SJames Smart 		freqpriv->nvme_buf = NULL;
11273fd78355SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
1128c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
1129c2017260SJames Smart 		nCmd->done(nCmd);
1130c2017260SJames Smart 	} else
1131c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
113201649561SJames Smart 
1133952c303bSDick Kennedy 	/* Call release with XB=1 to queue the IO into the abort list. */
113401649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
113501649561SJames Smart }
113601649561SJames Smart 
113701649561SJames Smart 
113801649561SJames Smart /**
113901649561SJames Smart  * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
114009d99705SLee Jones  * @vport: pointer to a host virtual N_Port data structure
11413e749170SLee Jones  * @lpfc_ncmd: Pointer to lpfc scsi command
114209d99705SLee Jones  * @pnode: pointer to a node-list data structure
114309d99705SLee Jones  * @cstat: pointer to the control status structure
114401649561SJames Smart  *
114501649561SJames Smart  * Driver registers this routine as it io request handler.  This
114601649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
114701649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
114801649561SJames Smart  *
114901649561SJames Smart  * Return value :
115001649561SJames Smart  *   0 - Success
115101649561SJames Smart  *   TODO: What are the failure codes.
115201649561SJames Smart  **/
115301649561SJames Smart static int
115401649561SJames Smart lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
1155c490850aSJames Smart 		      struct lpfc_io_buf *lpfc_ncmd,
115666a210ffSJames Smart 		      struct lpfc_nodelist *pnode,
11574c47efc1SJames Smart 		      struct lpfc_fc4_ctrl_stat *cstat)
115801649561SJames Smart {
115901649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
116001649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
116101649561SJames Smart 	struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
1162205e8240SJames Smart 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
116301649561SJames Smart 	uint32_t req_len;
116401649561SJames Smart 
116501649561SJames Smart 	/*
116601649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
116701649561SJames Smart 	 * the single mapping, or neither.
116801649561SJames Smart 	 */
116901649561SJames Smart 	if (nCmd->sg_cnt) {
117001649561SJames Smart 		if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
11715fd11085SJames Smart 			/* From the iwrite template, initialize words 7 - 11 */
11725fd11085SJames Smart 			memcpy(&wqe->words[7],
11735fd11085SJames Smart 			       &lpfc_iwrite_cmd_template.words[7],
11745fd11085SJames Smart 			       sizeof(uint32_t) * 5);
11755fd11085SJames Smart 
11765fd11085SJames Smart 			/* Word 4 */
11775fd11085SJames Smart 			wqe->fcp_iwrite.total_xfer_len = nCmd->payload_length;
11785fd11085SJames Smart 
117901649561SJames Smart 			/* Word 5 */
118001649561SJames Smart 			if ((phba->cfg_nvme_enable_fb) &&
118101649561SJames Smart 			    (pnode->nlp_flag & NLP_FIRSTBURST)) {
118201649561SJames Smart 				req_len = lpfc_ncmd->nvmeCmd->payload_length;
118301649561SJames Smart 				if (req_len < pnode->nvme_fb_size)
118401649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
118501649561SJames Smart 						req_len;
118601649561SJames Smart 				else
118701649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
118801649561SJames Smart 						pnode->nvme_fb_size;
11895fd11085SJames Smart 			} else {
11905fd11085SJames Smart 				wqe->fcp_iwrite.initial_xfer_len = 0;
119101649561SJames Smart 			}
11924c47efc1SJames Smart 			cstat->output_requests++;
119301649561SJames Smart 		} else {
11945fd11085SJames Smart 			/* From the iread template, initialize words 7 - 11 */
11955fd11085SJames Smart 			memcpy(&wqe->words[7],
11965fd11085SJames Smart 			       &lpfc_iread_cmd_template.words[7],
11975fd11085SJames Smart 			       sizeof(uint32_t) * 5);
119801649561SJames Smart 
11995fd11085SJames Smart 			/* Word 4 */
12005fd11085SJames Smart 			wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
120101649561SJames Smart 
12025fd11085SJames Smart 			/* Word 5 */
12035fd11085SJames Smart 			wqe->fcp_iread.rsrvd5 = 0;
120401649561SJames Smart 
12054c47efc1SJames Smart 			cstat->input_requests++;
120601649561SJames Smart 		}
120701649561SJames Smart 	} else {
12085fd11085SJames Smart 		/* From the icmnd template, initialize words 4 - 11 */
12095fd11085SJames Smart 		memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
12105fd11085SJames Smart 		       sizeof(uint32_t) * 8);
12114c47efc1SJames Smart 		cstat->control_requests++;
121201649561SJames Smart 	}
12130d8af096SJames Smart 
12140d8af096SJames Smart 	if (pnode->nlp_nvme_info & NLP_NVME_NSLER)
12150d8af096SJames Smart 		bf_set(wqe_erp, &wqe->generic.wqe_com, 1);
121601649561SJames Smart 	/*
121701649561SJames Smart 	 * Finish initializing those WQE fields that are independent
121801649561SJames Smart 	 * of the nvme_cmnd request_buffer
121901649561SJames Smart 	 */
122001649561SJames Smart 
12215fd11085SJames Smart 	/* Word 3 */
12225fd11085SJames Smart 	bf_set(payload_offset_len, &wqe->fcp_icmd,
12235fd11085SJames Smart 	       (nCmd->rsplen + nCmd->cmdlen));
12245fd11085SJames Smart 
122501649561SJames Smart 	/* Word 6 */
122601649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
122701649561SJames Smart 	       phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
122801649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
122901649561SJames Smart 
123001649561SJames Smart 	/* Word 8 */
123101649561SJames Smart 	wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
123201649561SJames Smart 
123301649561SJames Smart 	/* Word 9 */
123401649561SJames Smart 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
123501649561SJames Smart 
1236840a4701SJames Smart 	/* Word 10 */
1237840a4701SJames Smart 	bf_set(wqe_xchg, &wqe->fcp_iwrite.wqe_com, LPFC_NVME_XCHG);
1238840a4701SJames Smart 
1239414abe0aSJames Smart 	/* Words 13 14 15 are for PBDE support */
1240414abe0aSJames Smart 
124101649561SJames Smart 	pwqeq->vport = vport;
124201649561SJames Smart 	return 0;
124301649561SJames Smart }
124401649561SJames Smart 
124501649561SJames Smart 
124601649561SJames Smart /**
124701649561SJames Smart  * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
124809d99705SLee Jones  * @vport: pointer to a host virtual N_Port data structure
12493e749170SLee Jones  * @lpfc_ncmd: Pointer to lpfc scsi command
125001649561SJames Smart  *
125101649561SJames Smart  * Driver registers this routine as it io request handler.  This
125201649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
125301649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
125401649561SJames Smart  *
125501649561SJames Smart  * Return value :
125601649561SJames Smart  *   0 - Success
125701649561SJames Smart  *   TODO: What are the failure codes.
125801649561SJames Smart  **/
125901649561SJames Smart static int
126001649561SJames Smart lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
1261c490850aSJames Smart 		      struct lpfc_io_buf *lpfc_ncmd)
126201649561SJames Smart {
126301649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
126401649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1265205e8240SJames Smart 	union lpfc_wqe128 *wqe = &lpfc_ncmd->cur_iocbq.wqe;
12660794d601SJames Smart 	struct sli4_sge *sgl = lpfc_ncmd->dma_sgl;
1267d79c9e9dSJames Smart 	struct sli4_hybrid_sgl *sgl_xtra = NULL;
126801649561SJames Smart 	struct scatterlist *data_sg;
126901649561SJames Smart 	struct sli4_sge *first_data_sgl;
12700bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
1271d79c9e9dSJames Smart 	dma_addr_t physaddr = 0;
127201649561SJames Smart 	uint32_t num_bde = 0;
1273d79c9e9dSJames Smart 	uint32_t dma_len = 0;
127401649561SJames Smart 	uint32_t dma_offset = 0;
1275d79c9e9dSJames Smart 	int nseg, i, j;
1276d79c9e9dSJames Smart 	bool lsp_just_set = false;
127701649561SJames Smart 
127801649561SJames Smart 	/* Fix up the command and response DMA stuff. */
127901649561SJames Smart 	lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
128001649561SJames Smart 
128101649561SJames Smart 	/*
128201649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
128301649561SJames Smart 	 * the single mapping, or neither.
128401649561SJames Smart 	 */
128501649561SJames Smart 	if (nCmd->sg_cnt) {
128601649561SJames Smart 		/*
128701649561SJames Smart 		 * Jump over the cmd and rsp SGEs.  The fix routine
128801649561SJames Smart 		 * has already adjusted for this.
128901649561SJames Smart 		 */
129001649561SJames Smart 		sgl += 2;
129101649561SJames Smart 
129201649561SJames Smart 		first_data_sgl = sgl;
129301649561SJames Smart 		lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
129481e6a637SJames Smart 		if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
1295372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
129601649561SJames Smart 					"6058 Too many sg segments from "
129701649561SJames Smart 					"NVME Transport.  Max %d, "
129801649561SJames Smart 					"nvmeIO sg_cnt %d\n",
129929bfd55aSJames Smart 					phba->cfg_nvme_seg_cnt + 1,
130001649561SJames Smart 					lpfc_ncmd->seg_cnt);
130101649561SJames Smart 			lpfc_ncmd->seg_cnt = 0;
130201649561SJames Smart 			return 1;
130301649561SJames Smart 		}
130401649561SJames Smart 
130501649561SJames Smart 		/*
130601649561SJames Smart 		 * The driver established a maximum scatter-gather segment count
130701649561SJames Smart 		 * during probe that limits the number of sg elements in any
130801649561SJames Smart 		 * single nvme command.  Just run through the seg_cnt and format
130901649561SJames Smart 		 * the sge's.
131001649561SJames Smart 		 */
131101649561SJames Smart 		nseg = nCmd->sg_cnt;
131201649561SJames Smart 		data_sg = nCmd->first_sgl;
1313d79c9e9dSJames Smart 
1314d79c9e9dSJames Smart 		/* for tracking the segment boundaries */
1315d79c9e9dSJames Smart 		j = 2;
131601649561SJames Smart 		for (i = 0; i < nseg; i++) {
131701649561SJames Smart 			if (data_sg == NULL) {
1318372c187bSDick Kennedy 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
131901649561SJames Smart 						"6059 dptr err %d, nseg %d\n",
132001649561SJames Smart 						i, nseg);
132101649561SJames Smart 				lpfc_ncmd->seg_cnt = 0;
132201649561SJames Smart 				return 1;
132301649561SJames Smart 			}
1324d79c9e9dSJames Smart 
1325d79c9e9dSJames Smart 			sgl->word2 = 0;
1326d79c9e9dSJames Smart 			if ((num_bde + 1) == nseg) {
1327d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 1);
1328d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_type, sgl,
1329d79c9e9dSJames Smart 				       LPFC_SGE_TYPE_DATA);
1330d79c9e9dSJames Smart 			} else {
1331d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 0);
1332d79c9e9dSJames Smart 
1333d79c9e9dSJames Smart 				/* expand the segment */
1334d79c9e9dSJames Smart 				if (!lsp_just_set &&
1335d79c9e9dSJames Smart 				    !((j + 1) % phba->border_sge_num) &&
1336d79c9e9dSJames Smart 				    ((nseg - 1) != i)) {
1337d79c9e9dSJames Smart 					/* set LSP type */
1338d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_type, sgl,
1339d79c9e9dSJames Smart 					       LPFC_SGE_TYPE_LSP);
1340d79c9e9dSJames Smart 
1341d79c9e9dSJames Smart 					sgl_xtra = lpfc_get_sgl_per_hdwq(
1342d79c9e9dSJames Smart 							phba, lpfc_ncmd);
1343d79c9e9dSJames Smart 
1344d79c9e9dSJames Smart 					if (unlikely(!sgl_xtra)) {
1345d79c9e9dSJames Smart 						lpfc_ncmd->seg_cnt = 0;
1346d79c9e9dSJames Smart 						return 1;
1347d79c9e9dSJames Smart 					}
1348d79c9e9dSJames Smart 					sgl->addr_lo = cpu_to_le32(putPaddrLow(
1349d79c9e9dSJames Smart 						       sgl_xtra->dma_phys_sgl));
1350d79c9e9dSJames Smart 					sgl->addr_hi = cpu_to_le32(putPaddrHigh(
1351d79c9e9dSJames Smart 						       sgl_xtra->dma_phys_sgl));
1352d79c9e9dSJames Smart 
1353d79c9e9dSJames Smart 				} else {
1354d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_type, sgl,
1355d79c9e9dSJames Smart 					       LPFC_SGE_TYPE_DATA);
1356d79c9e9dSJames Smart 				}
1357d79c9e9dSJames Smart 			}
1358d79c9e9dSJames Smart 
1359d79c9e9dSJames Smart 			if (!(bf_get(lpfc_sli4_sge_type, sgl) &
1360d79c9e9dSJames Smart 				     LPFC_SGE_TYPE_LSP)) {
1361d79c9e9dSJames Smart 				if ((nseg - 1) == i)
1362d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_last, sgl, 1);
1363d79c9e9dSJames Smart 
136401649561SJames Smart 				physaddr = data_sg->dma_address;
136501649561SJames Smart 				dma_len = data_sg->length;
1366d79c9e9dSJames Smart 				sgl->addr_lo = cpu_to_le32(
1367d79c9e9dSJames Smart 							 putPaddrLow(physaddr));
1368d79c9e9dSJames Smart 				sgl->addr_hi = cpu_to_le32(
1369d79c9e9dSJames Smart 							putPaddrHigh(physaddr));
1370d79c9e9dSJames Smart 
137101649561SJames Smart 				bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
137201649561SJames Smart 				sgl->word2 = cpu_to_le32(sgl->word2);
137301649561SJames Smart 				sgl->sge_len = cpu_to_le32(dma_len);
137401649561SJames Smart 
137501649561SJames Smart 				dma_offset += dma_len;
137601649561SJames Smart 				data_sg = sg_next(data_sg);
1377d79c9e9dSJames Smart 
137801649561SJames Smart 				sgl++;
1379d79c9e9dSJames Smart 
1380d79c9e9dSJames Smart 				lsp_just_set = false;
1381d79c9e9dSJames Smart 			} else {
1382d79c9e9dSJames Smart 				sgl->word2 = cpu_to_le32(sgl->word2);
1383d79c9e9dSJames Smart 
1384d79c9e9dSJames Smart 				sgl->sge_len = cpu_to_le32(
1385d79c9e9dSJames Smart 						     phba->cfg_sg_dma_buf_size);
1386d79c9e9dSJames Smart 
1387d79c9e9dSJames Smart 				sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
1388d79c9e9dSJames Smart 				i = i - 1;
1389d79c9e9dSJames Smart 
1390d79c9e9dSJames Smart 				lsp_just_set = true;
1391d79c9e9dSJames Smart 			}
1392d79c9e9dSJames Smart 
1393d79c9e9dSJames Smart 			j++;
139401649561SJames Smart 		}
1395414abe0aSJames Smart 		if (phba->cfg_enable_pbde) {
13960bc2b7c5SJames Smart 			/* Use PBDE support for first SGL only, offset == 0 */
13970bc2b7c5SJames Smart 			/* Words 13-15 */
13980bc2b7c5SJames Smart 			bde = (struct ulp_bde64 *)
13990bc2b7c5SJames Smart 				&wqe->words[13];
14000bc2b7c5SJames Smart 			bde->addrLow = first_data_sgl->addr_lo;
14010bc2b7c5SJames Smart 			bde->addrHigh = first_data_sgl->addr_hi;
14020bc2b7c5SJames Smart 			bde->tus.f.bdeSize =
14030bc2b7c5SJames Smart 				le32_to_cpu(first_data_sgl->sge_len);
14040bc2b7c5SJames Smart 			bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
14050bc2b7c5SJames Smart 			bde->tus.w = cpu_to_le32(bde->tus.w);
1406b101eb27SJames Smart 
1407b101eb27SJames Smart 			/* Word 11 */
1408b101eb27SJames Smart 			bf_set(wqe_pbde, &wqe->generic.wqe_com, 1);
14095fd11085SJames Smart 		} else {
14105fd11085SJames Smart 			memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
14110bc2b7c5SJames Smart 			bf_set(wqe_pbde, &wqe->generic.wqe_com, 0);
14125fd11085SJames Smart 		}
14130bc2b7c5SJames Smart 
1414414abe0aSJames Smart 	} else {
14150794d601SJames Smart 		lpfc_ncmd->seg_cnt = 0;
14160794d601SJames Smart 
141701649561SJames Smart 		/* For this clause to be valid, the payload_length
141801649561SJames Smart 		 * and sg_cnt must zero.
141901649561SJames Smart 		 */
142001649561SJames Smart 		if (nCmd->payload_length != 0) {
1421372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
142201649561SJames Smart 					"6063 NVME DMA Prep Err: sg_cnt %d "
142301649561SJames Smart 					"payload_length x%x\n",
142401649561SJames Smart 					nCmd->sg_cnt, nCmd->payload_length);
142501649561SJames Smart 			return 1;
142601649561SJames Smart 		}
142701649561SJames Smart 	}
142801649561SJames Smart 	return 0;
142901649561SJames Smart }
143001649561SJames Smart 
143101649561SJames Smart /**
143201649561SJames Smart  * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
14333e749170SLee Jones  * @pnvme_lport: Pointer to the driver's local port data
14343e749170SLee Jones  * @pnvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
143501649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
14363e749170SLee Jones  * @pnvme_fcreq: IO request from nvme fc to driver.
143701649561SJames Smart  *
143801649561SJames Smart  * Driver registers this routine as it io request handler.  This
143901649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
14403e749170SLee Jones  * data structure to the rport indicated in @lpfc_nvme_rport.
144101649561SJames Smart  *
144201649561SJames Smart  * Return value :
144301649561SJames Smart  *   0 - Success
144401649561SJames Smart  *   TODO: What are the failure codes.
144501649561SJames Smart  **/
144601649561SJames Smart static int
144701649561SJames Smart lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
144801649561SJames Smart 			struct nvme_fc_remote_port *pnvme_rport,
144901649561SJames Smart 			void *hw_queue_handle,
145001649561SJames Smart 			struct nvmefc_fcp_req *pnvme_fcreq)
145101649561SJames Smart {
145201649561SJames Smart 	int ret = 0;
1453cf1a1d3eSJames Smart 	int expedite = 0;
145463df6d63SJames Smart 	int idx, cpu;
145501649561SJames Smart 	struct lpfc_nvme_lport *lport;
14564c47efc1SJames Smart 	struct lpfc_fc4_ctrl_stat *cstat;
145701649561SJames Smart 	struct lpfc_vport *vport;
145801649561SJames Smart 	struct lpfc_hba *phba;
145901649561SJames Smart 	struct lpfc_nodelist *ndlp;
1460c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
146101649561SJames Smart 	struct lpfc_nvme_rport *rport;
146201649561SJames Smart 	struct lpfc_nvme_qhandle *lpfc_queue_info;
1463c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
1464cf1a1d3eSJames Smart 	struct nvme_common_command *sqe;
1465bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1466bd2cdd5eSJames Smart 	uint64_t start = 0;
1467bd2cdd5eSJames Smart #endif
146801649561SJames Smart 
1469c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1470c3725bdcSJames Smart 	 * have timing races.
1471c3725bdcSJames Smart 	 */
147201649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1473c3725bdcSJames Smart 	if (unlikely(!lport)) {
1474c3725bdcSJames Smart 		ret = -EINVAL;
1475c3725bdcSJames Smart 		goto out_fail;
1476c3725bdcSJames Smart 	}
1477c3725bdcSJames Smart 
147801649561SJames Smart 	vport = lport->vport;
1479c3725bdcSJames Smart 
1480c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
148144c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
148244c2757bSJames Smart 				 "6117 Fail IO, NULL hw_queue_handle\n");
148344c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1484cd240071SJames Smart 		ret = -EBUSY;
1485c3725bdcSJames Smart 		goto out_fail;
1486c3725bdcSJames Smart 	}
1487c3725bdcSJames Smart 
148801649561SJames Smart 	phba = vport->phba;
148901649561SJames Smart 
14901df09449SJames Smart 	if (unlikely(vport->load_flag & FC_UNLOADING)) {
149144c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
149244c2757bSJames Smart 				 "6124 Fail IO, Driver unload\n");
149344c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
14943386f4bdSJames Smart 		ret = -ENODEV;
14953386f4bdSJames Smart 		goto out_fail;
14963386f4bdSJames Smart 	}
14973386f4bdSJames Smart 
1498c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1499c3725bdcSJames Smart 	if (unlikely(!freqpriv)) {
150044c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
150144c2757bSJames Smart 				 "6158 Fail IO, NULL request data\n");
150244c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1503c3725bdcSJames Smart 		ret = -EINVAL;
1504b7672ae6SDick Kennedy 		goto out_fail;
1505b7672ae6SDick Kennedy 	}
1506b7672ae6SDick Kennedy 
1507bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1508bd2cdd5eSJames Smart 	if (phba->ktime_on)
1509bd2cdd5eSJames Smart 		start = ktime_get_ns();
1510bd2cdd5eSJames Smart #endif
151101649561SJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
151201649561SJames Smart 	lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
151301649561SJames Smart 
151401649561SJames Smart 	/*
151501649561SJames Smart 	 * Catch race where our node has transitioned, but the
151601649561SJames Smart 	 * transport is still transitioning.
151701649561SJames Smart 	 */
151801649561SJames Smart 	ndlp = rport->ndlp;
1519307e3380SJames Smart 	if (!ndlp) {
152044c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
152132350664SJames Smart 				 "6053 Busy IO, ndlp not ready: rport x%px "
152232350664SJames Smart 				  "ndlp x%px, DID x%06x\n",
152301649561SJames Smart 				 rport, ndlp, pnvme_rport->port_id);
152444c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1525cd240071SJames Smart 		ret = -EBUSY;
152601649561SJames Smart 		goto out_fail;
152701649561SJames Smart 	}
152801649561SJames Smart 
152901649561SJames Smart 	/* The remote node has to be a mapped target or it's an error. */
153001649561SJames Smart 	if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
153101649561SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
153244c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
153344c2757bSJames Smart 				 "6036 Fail IO, DID x%06x not ready for "
1534cd240071SJames Smart 				 "IO. State x%x, Type x%x Flg x%x\n",
153544c2757bSJames Smart 				 pnvme_rport->port_id,
1536cd240071SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type,
1537e9b11083SJames Smart 				 ndlp->fc4_xpt_flags);
15384b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_bad_ndlp);
1539cd240071SJames Smart 		ret = -EBUSY;
154001649561SJames Smart 		goto out_fail;
154101649561SJames Smart 
154201649561SJames Smart 	}
154301649561SJames Smart 
1544cf1a1d3eSJames Smart 	/* Currently only NVME Keep alive commands should be expedited
1545cf1a1d3eSJames Smart 	 * if the driver runs out of a resource. These should only be
1546cf1a1d3eSJames Smart 	 * issued on the admin queue, qidx 0
1547cf1a1d3eSJames Smart 	 */
1548cf1a1d3eSJames Smart 	if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
1549cf1a1d3eSJames Smart 		sqe = &((struct nvme_fc_cmd_iu *)
1550cf1a1d3eSJames Smart 			pnvme_fcreq->cmdaddr)->sqe.common;
1551cf1a1d3eSJames Smart 		if (sqe->opcode == nvme_admin_keep_alive)
1552cf1a1d3eSJames Smart 			expedite = 1;
1553cf1a1d3eSJames Smart 	}
1554cf1a1d3eSJames Smart 
155501649561SJames Smart 	/* The node is shared with FCP IO, make sure the IO pending count does
155601649561SJames Smart 	 * not exceed the programmed depth.
155701649561SJames Smart 	 */
15582a5b7d62SJames Smart 	if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1559cf1a1d3eSJames Smart 		if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
1560cf1a1d3eSJames Smart 		    !expedite) {
156144c2757bSJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
156244c2757bSJames Smart 					 "6174 Fail IO, ndlp qdepth exceeded: "
15634d5e789aSJames Smart 					 "idx %d DID %x pend %d qdepth %d\n",
15644d5e789aSJames Smart 					 lpfc_queue_info->index, ndlp->nlp_DID,
15654d5e789aSJames Smart 					 atomic_read(&ndlp->cmd_pending),
15664d5e789aSJames Smart 					 ndlp->cmd_qdepth);
15674b056682SJames Smart 			atomic_inc(&lport->xmt_fcp_qdepth);
1568cd22d605SDick Kennedy 			ret = -EBUSY;
156901649561SJames Smart 			goto out_fail;
157001649561SJames Smart 		}
15712a5b7d62SJames Smart 	}
157201649561SJames Smart 
15736a828b0fSJames Smart 	/* Lookup Hardware Queue index based on fcp_io_sched module parameter */
157445aa312eSJames Smart 	if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
157545aa312eSJames Smart 		idx = lpfc_queue_info->index;
157645aa312eSJames Smart 	} else {
1577d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
15786a828b0fSJames Smart 		idx = phba->sli4_hba.cpu_map[cpu].hdwq;
157945aa312eSJames Smart 	}
158045aa312eSJames Smart 
158145aa312eSJames Smart 	lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, idx, expedite);
158201649561SJames Smart 	if (lpfc_ncmd == NULL) {
15834b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_noxri);
158401649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
158544c2757bSJames Smart 				 "6065 Fail IO, driver buffer pool is empty: "
158644c2757bSJames Smart 				 "idx %d DID %x\n",
158744c2757bSJames Smart 				 lpfc_queue_info->index, ndlp->nlp_DID);
1588cd22d605SDick Kennedy 		ret = -EBUSY;
158901649561SJames Smart 		goto out_fail;
159001649561SJames Smart 	}
1591bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1592c8a4ce0bSDick Kennedy 	if (start) {
1593bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_start = start;
1594bd2cdd5eSJames Smart 		lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
1595c8a4ce0bSDick Kennedy 	} else {
1596c8a4ce0bSDick Kennedy 		lpfc_ncmd->ts_cmd_start = 0;
1597bd2cdd5eSJames Smart 	}
1598bd2cdd5eSJames Smart #endif
159901649561SJames Smart 
160001649561SJames Smart 	/*
160101649561SJames Smart 	 * Store the data needed by the driver to issue, abort, and complete
160201649561SJames Smart 	 * an IO.
160301649561SJames Smart 	 * Do not let the IO hang out forever.  There is no midlayer issuing
160401649561SJames Smart 	 * an abort so inform the FW of the maximum IO pending time.
160501649561SJames Smart 	 */
1606bbe3012bSJames Smart 	freqpriv->nvme_buf = lpfc_ncmd;
160701649561SJames Smart 	lpfc_ncmd->nvmeCmd = pnvme_fcreq;
1608318083adSJames Smart 	lpfc_ncmd->ndlp = ndlp;
16090794d601SJames Smart 	lpfc_ncmd->qidx = lpfc_queue_info->qidx;
161001649561SJames Smart 
161101649561SJames Smart 	/*
161201649561SJames Smart 	 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
161301649561SJames Smart 	 * This identfier was create in our hardware queue create callback
161401649561SJames Smart 	 * routine. The driver now is dependent on the IO queue steering from
161501649561SJames Smart 	 * the transport.  We are trusting the upper NVME layers know which
161601649561SJames Smart 	 * index to use and that they have affinitized a CPU to this hardware
161701649561SJames Smart 	 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
161801649561SJames Smart 	 */
161966a210ffSJames Smart 	lpfc_ncmd->cur_iocbq.hba_wqidx = idx;
16204c47efc1SJames Smart 	cstat = &phba->sli4_hba.hdwq[idx].nvme_cstat;
162166a210ffSJames Smart 
162266a210ffSJames Smart 	lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp, cstat);
162366a210ffSJames Smart 	ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
162466a210ffSJames Smart 	if (ret) {
162544c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
162644c2757bSJames Smart 				 "6175 Fail IO, Prep DMA: "
162744c2757bSJames Smart 				 "idx %d DID %x\n",
162844c2757bSJames Smart 				 lpfc_queue_info->index, ndlp->nlp_DID);
162944c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
163066a210ffSJames Smart 		ret = -ENOMEM;
163166a210ffSJames Smart 		goto out_free_nvme_buf;
163266a210ffSJames Smart 	}
163366a210ffSJames Smart 
1634bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1635bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
1636bd2cdd5eSJames Smart 			 lpfc_queue_info->index, ndlp->nlp_DID);
1637bd2cdd5eSJames Smart 
16381fbf9742SJames Smart 	ret = lpfc_sli4_issue_wqe(phba, lpfc_ncmd->hdwq, &lpfc_ncmd->cur_iocbq);
163901649561SJames Smart 	if (ret) {
16404b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_wqerr);
1641e3246a12SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
164244c2757bSJames Smart 				 "6113 Fail IO, Could not issue WQE err %x "
164301649561SJames Smart 				 "sid: x%x did: x%x oxid: x%x\n",
164401649561SJames Smart 				 ret, vport->fc_myDID, ndlp->nlp_DID,
164501649561SJames Smart 				 lpfc_ncmd->cur_iocbq.sli4_xritag);
164601649561SJames Smart 		goto out_free_nvme_buf;
164701649561SJames Smart 	}
164801649561SJames Smart 
1649c490850aSJames Smart 	if (phba->cfg_xri_rebalancing)
1650c490850aSJames Smart 		lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_ncmd->hdwq_no);
1651c490850aSJames Smart 
1652bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1653c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start)
1654bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1655bd2cdd5eSJames Smart 
1656840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVME_IO) {
1657d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
1658840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
165963df6d63SJames Smart 		lpfc_ncmd->cpu = cpu;
166063df6d63SJames Smart 		if (idx != cpu)
1661bd2cdd5eSJames Smart 			lpfc_printf_vlog(vport,
166263df6d63SJames Smart 					 KERN_INFO, LOG_NVME_IOERR,
1663bd2cdd5eSJames Smart 					"6702 CPU Check cmd: "
1664bd2cdd5eSJames Smart 					"cpu %d wq %d\n",
1665bd2cdd5eSJames Smart 					lpfc_ncmd->cpu,
1666bd2cdd5eSJames Smart 					lpfc_queue_info->index);
1667bd2cdd5eSJames Smart 	}
1668bd2cdd5eSJames Smart #endif
166901649561SJames Smart 	return 0;
167001649561SJames Smart 
167101649561SJames Smart  out_free_nvme_buf:
16722cee7808SJames Smart 	if (lpfc_ncmd->nvmeCmd->sg_cnt) {
16732cee7808SJames Smart 		if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
16744c47efc1SJames Smart 			cstat->output_requests--;
16752cee7808SJames Smart 		else
16764c47efc1SJames Smart 			cstat->input_requests--;
16772cee7808SJames Smart 	} else
16784c47efc1SJames Smart 		cstat->control_requests--;
167901649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
168001649561SJames Smart  out_fail:
168101649561SJames Smart 	return ret;
168201649561SJames Smart }
168301649561SJames Smart 
168401649561SJames Smart /**
168501649561SJames Smart  * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
168601649561SJames Smart  * @phba: Pointer to HBA context object
168701649561SJames Smart  * @cmdiocb: Pointer to command iocb object.
16883e749170SLee Jones  * @abts_cmpl: Pointer to wcqe complete object.
168901649561SJames Smart  *
169001649561SJames Smart  * This is the callback function for any NVME FCP IO that was aborted.
169101649561SJames Smart  *
169201649561SJames Smart  * Return value:
169301649561SJames Smart  *   None
169401649561SJames Smart  **/
169501649561SJames Smart void
169601649561SJames Smart lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
169701649561SJames Smart 			   struct lpfc_wcqe_complete *abts_cmpl)
169801649561SJames Smart {
1699e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
170001649561SJames Smart 			"6145 ABORT_XRI_CN completing on rpi x%x "
170101649561SJames Smart 			"original iotag x%x, abort cmd iotag x%x "
170201649561SJames Smart 			"req_tag x%x, status x%x, hwstatus x%x\n",
170301649561SJames Smart 			cmdiocb->iocb.un.acxri.abortContextTag,
170401649561SJames Smart 			cmdiocb->iocb.un.acxri.abortIoTag,
170501649561SJames Smart 			cmdiocb->iotag,
170601649561SJames Smart 			bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
170701649561SJames Smart 			bf_get(lpfc_wcqe_c_status, abts_cmpl),
170801649561SJames Smart 			bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
170901649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocb);
171001649561SJames Smart }
171101649561SJames Smart 
171201649561SJames Smart /**
171301649561SJames Smart  * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
17143e749170SLee Jones  * @pnvme_lport: Pointer to the driver's local port data
17153e749170SLee Jones  * @pnvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
171601649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
17173e749170SLee Jones  * @pnvme_fcreq: IO request from nvme fc to driver.
171801649561SJames Smart  *
171901649561SJames Smart  * Driver registers this routine as its nvme request io abort handler.  This
172001649561SJames Smart  * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
172101649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.  This routine
172201649561SJames Smart  * is executed asynchronously - one the target is validated as "MAPPED" and
172301649561SJames Smart  * ready for IO, the driver issues the abort request and returns.
172401649561SJames Smart  *
172501649561SJames Smart  * Return value:
172601649561SJames Smart  *   None
172701649561SJames Smart  **/
172801649561SJames Smart static void
172901649561SJames Smart lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
173001649561SJames Smart 		    struct nvme_fc_remote_port *pnvme_rport,
173101649561SJames Smart 		    void *hw_queue_handle,
173201649561SJames Smart 		    struct nvmefc_fcp_req *pnvme_fcreq)
173301649561SJames Smart {
173401649561SJames Smart 	struct lpfc_nvme_lport *lport;
173501649561SJames Smart 	struct lpfc_vport *vport;
173601649561SJames Smart 	struct lpfc_hba *phba;
1737c490850aSJames Smart 	struct lpfc_io_buf *lpfc_nbuf;
173801649561SJames Smart 	struct lpfc_iocbq *nvmereq_wqe;
1739c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
174001649561SJames Smart 	unsigned long flags;
174101649561SJames Smart 	int ret_val;
174201649561SJames Smart 
1743c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1744c3725bdcSJames Smart 	 * have timing races.
1745c3725bdcSJames Smart 	 */
174601649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1747c3725bdcSJames Smart 	if (unlikely(!lport))
1748c3725bdcSJames Smart 		return;
174901649561SJames Smart 
1750c3725bdcSJames Smart 	vport = lport->vport;
1751c3725bdcSJames Smart 
1752c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
1753c3725bdcSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1754c3725bdcSJames Smart 				 "6129 Fail Abort, HW Queue Handle NULL.\n");
1755c3725bdcSJames Smart 		return;
1756c3725bdcSJames Smart 	}
1757c3725bdcSJames Smart 
1758c3725bdcSJames Smart 	phba = vport->phba;
1759c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1760c3725bdcSJames Smart 
1761c3725bdcSJames Smart 	if (unlikely(!freqpriv))
1762c3725bdcSJames Smart 		return;
17633386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
17643386f4bdSJames Smart 		return;
17653386f4bdSJames Smart 
176601649561SJames Smart 	/* Announce entry to new IO submit field. */
176786c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
176801649561SJames Smart 			 "6002 Abort Request to rport DID x%06x "
176932350664SJames Smart 			 "for nvme_fc_req x%px\n",
177001649561SJames Smart 			 pnvme_rport->port_id,
177101649561SJames Smart 			 pnvme_fcreq);
177201649561SJames Smart 
177301649561SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
177401649561SJames Smart 	 * cleared when the reset is complete and rings reestablished.
177501649561SJames Smart 	 */
177601649561SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
177701649561SJames Smart 	/* driver queued commands are in process of being flushed */
1778c00f62e6SJames Smart 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
177901649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1780372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
178101649561SJames Smart 				 "6139 Driver in reset cleanup - flushing "
178201649561SJames Smart 				 "NVME Req now.  hba_flag x%x\n",
178301649561SJames Smart 				 phba->hba_flag);
178401649561SJames Smart 		return;
178501649561SJames Smart 	}
178601649561SJames Smart 
1787bbe3012bSJames Smart 	lpfc_nbuf = freqpriv->nvme_buf;
178801649561SJames Smart 	if (!lpfc_nbuf) {
178901649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1790372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
179101649561SJames Smart 				 "6140 NVME IO req has no matching lpfc nvme "
179201649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
179301649561SJames Smart 		return;
179401649561SJames Smart 	} else if (!lpfc_nbuf->nvmeCmd) {
179501649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1796372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
179701649561SJames Smart 				 "6141 lpfc NVME IO req has no nvme_fcreq "
179801649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
179901649561SJames Smart 		return;
180001649561SJames Smart 	}
18012b7824d0SJames Smart 	nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
180201649561SJames Smart 
1803c2017260SJames Smart 	/* Guard against IO completion being called at same time */
1804c2017260SJames Smart 	spin_lock(&lpfc_nbuf->buf_lock);
1805c2017260SJames Smart 
180601649561SJames Smart 	/*
180701649561SJames Smart 	 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
180801649561SJames Smart 	 * state must match the nvme_fcreq passed by the nvme
180901649561SJames Smart 	 * transport.  If they don't match, it is likely the driver
181001649561SJames Smart 	 * has already completed the NVME IO and the nvme transport
181101649561SJames Smart 	 * has not seen it yet.
181201649561SJames Smart 	 */
181301649561SJames Smart 	if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
1814372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
181501649561SJames Smart 				 "6143 NVME req mismatch: "
181632350664SJames Smart 				 "lpfc_nbuf x%px nvmeCmd x%px, "
181732350664SJames Smart 				 "pnvme_fcreq x%px.  Skipping Abort xri x%x\n",
181801649561SJames Smart 				 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
18192b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1820c2017260SJames Smart 		goto out_unlock;
182101649561SJames Smart 	}
182201649561SJames Smart 
182301649561SJames Smart 	/* Don't abort IOs no longer on the pending queue. */
182401649561SJames Smart 	if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
1825372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
182632350664SJames Smart 				 "6142 NVME IO req x%px not queued - skipping "
18272b7824d0SJames Smart 				 "abort req xri x%x\n",
18282b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1829c2017260SJames Smart 		goto out_unlock;
183001649561SJames Smart 	}
183101649561SJames Smart 
18324b056682SJames Smart 	atomic_inc(&lport->xmt_fcp_abort);
1833bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1834bd2cdd5eSJames Smart 			 nvmereq_wqe->sli4_xritag,
183500cefeb9SJames Smart 			 nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
1836bd2cdd5eSJames Smart 
183701649561SJames Smart 	/* Outstanding abort is in progress */
183801649561SJames Smart 	if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
1839372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
184001649561SJames Smart 				 "6144 Outstanding NVME I/O Abort Request "
184132350664SJames Smart 				 "still pending on nvme_fcreq x%px, "
184232350664SJames Smart 				 "lpfc_ncmd %px xri x%x\n",
18432b7824d0SJames Smart 				 pnvme_fcreq, lpfc_nbuf,
18442b7824d0SJames Smart 				 nvmereq_wqe->sli4_xritag);
1845c2017260SJames Smart 		goto out_unlock;
184601649561SJames Smart 	}
184701649561SJames Smart 
1848db7531d2SJames Smart 	ret_val = lpfc_sli4_issue_abort_iotag(phba, nvmereq_wqe,
1849db7531d2SJames Smart 					      lpfc_nvme_abort_fcreq_cmpl);
185001649561SJames Smart 
1851c2017260SJames Smart 	spin_unlock(&lpfc_nbuf->buf_lock);
185201649561SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1853a22d73b6SJames Smart 
1854a22d73b6SJames Smart 	/* Make sure HBA is alive */
1855a22d73b6SJames Smart 	lpfc_issue_hb_tmo(phba);
1856a22d73b6SJames Smart 
1857db7531d2SJames Smart 	if (ret_val != WQE_SUCCESS) {
1858372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
185901649561SJames Smart 				 "6137 Failed abts issue_wqe with status x%x "
186032350664SJames Smart 				 "for nvme_fcreq x%px.\n",
186101649561SJames Smart 				 ret_val, pnvme_fcreq);
186201649561SJames Smart 		return;
186301649561SJames Smart 	}
186401649561SJames Smart 
186586c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
18662b7824d0SJames Smart 			 "6138 Transport Abort NVME Request Issued for "
1867db7531d2SJames Smart 			 "ox_id x%x\n",
1868db7531d2SJames Smart 			 nvmereq_wqe->sli4_xritag);
1869c2017260SJames Smart 	return;
1870c2017260SJames Smart 
1871c2017260SJames Smart out_unlock:
1872c2017260SJames Smart 	spin_unlock(&lpfc_nbuf->buf_lock);
1873c2017260SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1874c2017260SJames Smart 	return;
187501649561SJames Smart }
187601649561SJames Smart 
187701649561SJames Smart /* Declare and initialization an instance of the FC NVME template. */
187801649561SJames Smart static struct nvme_fc_port_template lpfc_nvme_template = {
187901649561SJames Smart 	/* initiator-based functions */
188001649561SJames Smart 	.localport_delete  = lpfc_nvme_localport_delete,
188101649561SJames Smart 	.remoteport_delete = lpfc_nvme_remoteport_delete,
188201649561SJames Smart 	.create_queue = lpfc_nvme_create_queue,
188301649561SJames Smart 	.delete_queue = lpfc_nvme_delete_queue,
188401649561SJames Smart 	.ls_req       = lpfc_nvme_ls_req,
188501649561SJames Smart 	.fcp_io       = lpfc_nvme_fcp_io_submit,
188601649561SJames Smart 	.ls_abort     = lpfc_nvme_ls_abort,
188701649561SJames Smart 	.fcp_abort    = lpfc_nvme_fcp_abort,
18889aa09e98SJames Smart 	.xmt_ls_rsp   = lpfc_nvme_xmt_ls_rsp,
188901649561SJames Smart 
189001649561SJames Smart 	.max_hw_queues = 1,
189101649561SJames Smart 	.max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
189201649561SJames Smart 	.max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
189301649561SJames Smart 	.dma_boundary = 0xFFFFFFFF,
189401649561SJames Smart 
189501649561SJames Smart 	/* Sizes of additional private data for data structures.
189601649561SJames Smart 	 * No use for the last two sizes at this time.
189701649561SJames Smart 	 */
189801649561SJames Smart 	.local_priv_sz = sizeof(struct lpfc_nvme_lport),
189901649561SJames Smart 	.remote_priv_sz = sizeof(struct lpfc_nvme_rport),
190001649561SJames Smart 	.lsrqst_priv_sz = 0,
1901bbe3012bSJames Smart 	.fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
190201649561SJames Smart };
190301649561SJames Smart 
19043e749170SLee Jones /*
19055e5b511dSJames Smart  * lpfc_get_nvme_buf - Get a nvme buffer from io_buf_list of the HBA
190601649561SJames Smart  *
19075e5b511dSJames Smart  * This routine removes a nvme buffer from head of @hdwq io_buf_list
190801649561SJames Smart  * and returns to caller.
190901649561SJames Smart  *
191001649561SJames Smart  * Return codes:
191101649561SJames Smart  *   NULL - Error
191201649561SJames Smart  *   Pointer to lpfc_nvme_buf - Success
191301649561SJames Smart  **/
1914c490850aSJames Smart static struct lpfc_io_buf *
1915cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19165e5b511dSJames Smart 		  int idx, int expedite)
191701649561SJames Smart {
1918c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
19195e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
19200794d601SJames Smart 	struct sli4_sge *sgl;
19210794d601SJames Smart 	struct lpfc_iocbq *pwqeq;
19220794d601SJames Smart 	union lpfc_wqe128 *wqe;
192301649561SJames Smart 
1924c490850aSJames Smart 	lpfc_ncmd = lpfc_get_io_buf(phba, NULL, idx, expedite);
19252a5b7d62SJames Smart 
19260794d601SJames Smart 	if (lpfc_ncmd) {
19270794d601SJames Smart 		pwqeq = &(lpfc_ncmd->cur_iocbq);
19280794d601SJames Smart 		wqe = &pwqeq->wqe;
19290794d601SJames Smart 
19300794d601SJames Smart 		/* Setup key fields in buffer that may have been changed
19310794d601SJames Smart 		 * if other protocols used this buffer.
19320794d601SJames Smart 		 */
19330794d601SJames Smart 		pwqeq->iocb_flag = LPFC_IO_NVME;
19340794d601SJames Smart 		pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
19350794d601SJames Smart 		lpfc_ncmd->start_time = jiffies;
19360794d601SJames Smart 		lpfc_ncmd->flags = 0;
19370794d601SJames Smart 
19380794d601SJames Smart 		/* Rsp SGE will be filled in when we rcv an IO
19390794d601SJames Smart 		 * from the NVME Layer to be sent.
19400794d601SJames Smart 		 * The cmd is going to be embedded so we need a SKIP SGE.
19410794d601SJames Smart 		 */
19420794d601SJames Smart 		sgl = lpfc_ncmd->dma_sgl;
19430794d601SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
19440794d601SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
19450794d601SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
19460794d601SJames Smart 		/* Fill in word 3 / sgl_len during cmd submission */
19470794d601SJames Smart 
1948d9f492a1SJames Smart 		/* Initialize 64 bytes only */
19490794d601SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
19500794d601SJames Smart 
19510794d601SJames Smart 		if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
19522a5b7d62SJames Smart 			atomic_inc(&ndlp->cmd_pending);
1953c490850aSJames Smart 			lpfc_ncmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
19542a5b7d62SJames Smart 		}
19555e5b511dSJames Smart 
1956c490850aSJames Smart 	} else {
1957c490850aSJames Smart 		qp = &phba->sli4_hba.hdwq[idx];
19585e5b511dSJames Smart 		qp->empty_io_bufs++;
1959c490850aSJames Smart 	}
19605e5b511dSJames Smart 
196101649561SJames Smart 	return  lpfc_ncmd;
196201649561SJames Smart }
196301649561SJames Smart 
196401649561SJames Smart /**
196501649561SJames Smart  * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
196601649561SJames Smart  * @phba: The Hba for which this call is being executed.
196701649561SJames Smart  * @lpfc_ncmd: The nvme buffer which is being released.
196801649561SJames Smart  *
196901649561SJames Smart  * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
19705e5b511dSJames Smart  * lpfc_io_buf_list list. For SLI4 XRI's are tied to the nvme buffer
197101649561SJames Smart  * and cannot be reused for at least RA_TOV amount of time if it was
197201649561SJames Smart  * aborted.
197301649561SJames Smart  **/
197401649561SJames Smart static void
1975c490850aSJames Smart lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd)
197601649561SJames Smart {
19775e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
197801649561SJames Smart 	unsigned long iflag = 0;
197901649561SJames Smart 
1980c490850aSJames Smart 	if ((lpfc_ncmd->flags & LPFC_SBUF_BUMP_QDEPTH) && lpfc_ncmd->ndlp)
19812a5b7d62SJames Smart 		atomic_dec(&lpfc_ncmd->ndlp->cmd_pending);
19822a5b7d62SJames Smart 
19832a5b7d62SJames Smart 	lpfc_ncmd->ndlp = NULL;
1984c490850aSJames Smart 	lpfc_ncmd->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
19852a5b7d62SJames Smart 
19861fbf9742SJames Smart 	qp = lpfc_ncmd->hdwq;
198722770cbaSJames Smart 	if (unlikely(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
198886c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
198986c67379SJames Smart 				"6310 XB release deferred for "
199086c67379SJames Smart 				"ox_id x%x on reqtag x%x\n",
199186c67379SJames Smart 				lpfc_ncmd->cur_iocbq.sli4_xritag,
199286c67379SJames Smart 				lpfc_ncmd->cur_iocbq.iotag);
199386c67379SJames Smart 
1994c00f62e6SJames Smart 		spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
199501649561SJames Smart 		list_add_tail(&lpfc_ncmd->list,
1996c00f62e6SJames Smart 			&qp->lpfc_abts_io_buf_list);
19975e5b511dSJames Smart 		qp->abts_nvme_io_bufs++;
1998c00f62e6SJames Smart 		spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
1999c490850aSJames Smart 	} else
2000c490850aSJames Smart 		lpfc_release_io_buf(phba, (struct lpfc_io_buf *)lpfc_ncmd, qp);
200101649561SJames Smart }
200201649561SJames Smart 
200301649561SJames Smart /**
200401649561SJames Smart  * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
2005*f6b35a75SLee Jones  * @vport: the lpfc_vport instance requesting a localport.
200601649561SJames Smart  *
200701649561SJames Smart  * This routine is invoked to create an nvme localport instance to bind
200801649561SJames Smart  * to the nvme_fc_transport.  It is called once during driver load
200901649561SJames Smart  * like lpfc_create_shost after all other services are initialized.
201001649561SJames Smart  * It requires a vport, vpi, and wwns at call time.  Other localport
201101649561SJames Smart  * parameters are modified as the driver's FCID and the Fabric WWN
201201649561SJames Smart  * are established.
201301649561SJames Smart  *
201401649561SJames Smart  * Return codes
201501649561SJames Smart  *      0 - successful
201601649561SJames Smart  *      -ENOMEM - no heap memory available
201701649561SJames Smart  *      other values - from nvme registration upcall
201801649561SJames Smart  **/
201901649561SJames Smart int
202001649561SJames Smart lpfc_nvme_create_localport(struct lpfc_vport *vport)
202101649561SJames Smart {
2022166d7211SJames Smart 	int ret = 0;
202301649561SJames Smart 	struct lpfc_hba  *phba = vport->phba;
202401649561SJames Smart 	struct nvme_fc_port_info nfcp_info;
202501649561SJames Smart 	struct nvme_fc_local_port *localport;
202601649561SJames Smart 	struct lpfc_nvme_lport *lport;
202701649561SJames Smart 
202801649561SJames Smart 	/* Initialize this localport instance.  The vport wwn usage ensures
202901649561SJames Smart 	 * that NPIV is accounted for.
203001649561SJames Smart 	 */
203101649561SJames Smart 	memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
203201649561SJames Smart 	nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
203301649561SJames Smart 	nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
203401649561SJames Smart 	nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
203501649561SJames Smart 
20365b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
20375b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
20385b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
20394d4c4a4aSJames Smart 	 */
20404d4c4a4aSJames Smart 	lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
204101649561SJames Smart 
2042542ddc9bSJames Smart 	/* Advertise how many hw queues we support based on cfg_hdw_queue,
2043542ddc9bSJames Smart 	 * which will not exceed cpu count.
2044542ddc9bSJames Smart 	 */
2045cdb42becSJames Smart 	lpfc_nvme_template.max_hw_queues = phba->cfg_hdw_queue;
204666a210ffSJames Smart 
2047faf5a744SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_FC))
2048faf5a744SArnd Bergmann 		return ret;
2049faf5a744SArnd Bergmann 
205001649561SJames Smart 	/* localport is allocated from the stack, but the registration
205101649561SJames Smart 	 * call allocates heap memory as well as the private area.
205201649561SJames Smart 	 */
2053faf5a744SArnd Bergmann 
205401649561SJames Smart 	ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
205501649561SJames Smart 					 &vport->phba->pcidev->dev, &localport);
205601649561SJames Smart 	if (!ret) {
205701649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
205801649561SJames Smart 				 "6005 Successfully registered local "
205932350664SJames Smart 				 "NVME port num %d, localP x%px, private "
206032350664SJames Smart 				 "x%px, sg_seg %d\n",
206101649561SJames Smart 				 localport->port_num, localport,
206201649561SJames Smart 				 localport->private,
206301649561SJames Smart 				 lpfc_nvme_template.max_sgl_segments);
206401649561SJames Smart 
206501649561SJames Smart 		/* Private is our lport size declared in the template. */
206601649561SJames Smart 		lport = (struct lpfc_nvme_lport *)localport->private;
206701649561SJames Smart 		vport->localport = localport;
206801649561SJames Smart 		lport->vport = vport;
206901649561SJames Smart 		vport->nvmei_support = 1;
20706b486ce9SDick Kennedy 
20714b056682SJames Smart 		atomic_set(&lport->xmt_fcp_noxri, 0);
20724b056682SJames Smart 		atomic_set(&lport->xmt_fcp_bad_ndlp, 0);
20734b056682SJames Smart 		atomic_set(&lport->xmt_fcp_qdepth, 0);
207444c2757bSJames Smart 		atomic_set(&lport->xmt_fcp_err, 0);
20754b056682SJames Smart 		atomic_set(&lport->xmt_fcp_wqerr, 0);
20764b056682SJames Smart 		atomic_set(&lport->xmt_fcp_abort, 0);
20774b056682SJames Smart 		atomic_set(&lport->xmt_ls_abort, 0);
20784b056682SJames Smart 		atomic_set(&lport->xmt_ls_err, 0);
20794b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_xb, 0);
20804b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_err, 0);
20814b056682SJames Smart 		atomic_set(&lport->cmpl_ls_xb, 0);
20824b056682SJames Smart 		atomic_set(&lport->cmpl_ls_err, 0);
20839aa09e98SJames Smart 
208466a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsRequests, 0);
208566a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsCmpls, 0);
20866b486ce9SDick Kennedy 	}
20873ebd9b47SJames Smart 
208801649561SJames Smart 	return ret;
208901649561SJames Smart }
209001649561SJames Smart 
2091d964b3e5SBart Van Assche #if (IS_ENABLED(CONFIG_NVME_FC))
2092add9d6beSJames Smart /* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2093add9d6beSJames Smart  *
2094add9d6beSJames Smart  * The driver has to wait for the host nvme transport to callback
2095add9d6beSJames Smart  * indicating the localport has successfully unregistered all
2096add9d6beSJames Smart  * resources.  Since this is an uninterruptible wait, loop every ten
2097add9d6beSJames Smart  * seconds and print a message indicating no progress.
2098add9d6beSJames Smart  *
2099add9d6beSJames Smart  * An uninterruptible wait is used because of the risk of transport-to-
2100add9d6beSJames Smart  * driver state mismatch.
2101add9d6beSJames Smart  */
21023999df75SBart Van Assche static void
2103add9d6beSJames Smart lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
21047961cba6SEwan D. Milne 			   struct lpfc_nvme_lport *lport,
21057961cba6SEwan D. Milne 			   struct completion *lport_unreg_cmp)
2106add9d6beSJames Smart {
2107add9d6beSJames Smart 	u32 wait_tmo;
21082ab70c21SJames Smart 	int ret, i, pending = 0;
21092ab70c21SJames Smart 	struct lpfc_sli_ring  *pring;
21102ab70c21SJames Smart 	struct lpfc_hba  *phba = vport->phba;
2111e7dab164SJames Smart 	struct lpfc_sli4_hdw_queue *qp;
2112e7dab164SJames Smart 	int abts_scsi, abts_nvme;
2113add9d6beSJames Smart 
2114add9d6beSJames Smart 	/* Host transport has to clean up and confirm requiring an indefinite
2115add9d6beSJames Smart 	 * wait. Print a message if a 10 second wait expires and renew the
2116add9d6beSJames Smart 	 * wait. This is unexpected.
2117add9d6beSJames Smart 	 */
2118add9d6beSJames Smart 	wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2119add9d6beSJames Smart 	while (true) {
21207961cba6SEwan D. Milne 		ret = wait_for_completion_timeout(lport_unreg_cmp, wait_tmo);
2121add9d6beSJames Smart 		if (unlikely(!ret)) {
21222ab70c21SJames Smart 			pending = 0;
2123e7dab164SJames Smart 			abts_scsi = 0;
2124e7dab164SJames Smart 			abts_nvme = 0;
21252ab70c21SJames Smart 			for (i = 0; i < phba->cfg_hdw_queue; i++) {
2126e7dab164SJames Smart 				qp = &phba->sli4_hba.hdwq[i];
2127e7dab164SJames Smart 				pring = qp->io_wq->pring;
21282ab70c21SJames Smart 				if (!pring)
21292ab70c21SJames Smart 					continue;
21302ab70c21SJames Smart 				pending += pring->txcmplq_cnt;
2131e7dab164SJames Smart 				abts_scsi += qp->abts_scsi_io_bufs;
2132e7dab164SJames Smart 				abts_nvme += qp->abts_nvme_io_bufs;
21332ab70c21SJames Smart 			}
2134372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
213532350664SJames Smart 					 "6176 Lport x%px Localport x%px wait "
2136e7dab164SJames Smart 					 "timed out. Pending %d [%d:%d]. "
2137e7dab164SJames Smart 					 "Renewing.\n",
2138e7dab164SJames Smart 					 lport, vport->localport, pending,
2139e7dab164SJames Smart 					 abts_scsi, abts_nvme);
2140add9d6beSJames Smart 			continue;
2141add9d6beSJames Smart 		}
2142add9d6beSJames Smart 		break;
2143add9d6beSJames Smart 	}
2144add9d6beSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
214532350664SJames Smart 			 "6177 Lport x%px Localport x%px Complete Success\n",
2146add9d6beSJames Smart 			 lport, vport->localport);
2147add9d6beSJames Smart }
2148d964b3e5SBart Van Assche #endif
2149add9d6beSJames Smart 
215001649561SJames Smart /**
215101649561SJames Smart  * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
21523e749170SLee Jones  * @vport: pointer to a host virtual N_Port data structure
215301649561SJames Smart  *
215401649561SJames Smart  * This routine is invoked to destroy all lports bound to the phba.
215501649561SJames Smart  * The lport memory was allocated by the nvme fc transport and is
215601649561SJames Smart  * released there.  This routine ensures all rports bound to the
215701649561SJames Smart  * lport have been disconnected.
215801649561SJames Smart  *
215901649561SJames Smart  **/
216001649561SJames Smart void
216101649561SJames Smart lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
216201649561SJames Smart {
21637d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
216401649561SJames Smart 	struct nvme_fc_local_port *localport;
216501649561SJames Smart 	struct lpfc_nvme_lport *lport;
216601649561SJames Smart 	int ret;
21677961cba6SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(lport_unreg_cmp);
216801649561SJames Smart 
216901649561SJames Smart 	if (vport->nvmei_support == 0)
217001649561SJames Smart 		return;
217101649561SJames Smart 
217201649561SJames Smart 	localport = vport->localport;
217301649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
217401649561SJames Smart 
217501649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
217632350664SJames Smart 			 "6011 Destroying NVME localport x%px\n",
217701649561SJames Smart 			 localport);
2178166d7211SJames Smart 
217901649561SJames Smart 	/* lport's rport list is clear.  Unregister
218001649561SJames Smart 	 * lport and release resources.
218101649561SJames Smart 	 */
21827961cba6SEwan D. Milne 	lport->lport_unreg_cmp = &lport_unreg_cmp;
218301649561SJames Smart 	ret = nvme_fc_unregister_localport(localport);
2184add9d6beSJames Smart 
2185add9d6beSJames Smart 	/* Wait for completion.  This either blocks
2186add9d6beSJames Smart 	 * indefinitely or succeeds
2187add9d6beSJames Smart 	 */
21887961cba6SEwan D. Milne 	lpfc_nvme_lport_unreg_wait(vport, lport, &lport_unreg_cmp);
21897961cba6SEwan D. Milne 	vport->localport = NULL;
219001649561SJames Smart 
219101649561SJames Smart 	/* Regardless of the unregister upcall response, clear
219201649561SJames Smart 	 * nvmei_support.  All rports are unregistered and the
219301649561SJames Smart 	 * driver will clean up.
219401649561SJames Smart 	 */
219501649561SJames Smart 	vport->nvmei_support = 0;
219601649561SJames Smart 	if (ret == 0) {
219701649561SJames Smart 		lpfc_printf_vlog(vport,
219801649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
219901649561SJames Smart 				 "6009 Unregistered lport Success\n");
220001649561SJames Smart 	} else {
220101649561SJames Smart 		lpfc_printf_vlog(vport,
220201649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
220301649561SJames Smart 				 "6010 Unregistered lport "
220401649561SJames Smart 				 "Failed, status x%x\n",
220501649561SJames Smart 				 ret);
220601649561SJames Smart 	}
2207166d7211SJames Smart #endif
220801649561SJames Smart }
220901649561SJames Smart 
221001649561SJames Smart void
221101649561SJames Smart lpfc_nvme_update_localport(struct lpfc_vport *vport)
221201649561SJames Smart {
22134410a67aSJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
221401649561SJames Smart 	struct nvme_fc_local_port *localport;
221501649561SJames Smart 	struct lpfc_nvme_lport *lport;
221601649561SJames Smart 
221701649561SJames Smart 	localport = vport->localport;
22184410a67aSJames Smart 	if (!localport) {
22194410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
22204410a67aSJames Smart 				 "6710 Update NVME fail. No localport\n");
22214410a67aSJames Smart 		return;
22224410a67aSJames Smart 	}
222301649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
22244410a67aSJames Smart 	if (!lport) {
22254410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
222632350664SJames Smart 				 "6171 Update NVME fail. localP x%px, No lport\n",
22274410a67aSJames Smart 				 localport);
22284410a67aSJames Smart 		return;
22294410a67aSJames Smart 	}
223001649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
223132350664SJames Smart 			 "6012 Update NVME lport x%px did x%x\n",
223201649561SJames Smart 			 localport, vport->fc_myDID);
223301649561SJames Smart 
223401649561SJames Smart 	localport->port_id = vport->fc_myDID;
223501649561SJames Smart 	if (localport->port_id == 0)
223601649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
223701649561SJames Smart 	else
223801649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
223901649561SJames Smart 
224001649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
224132350664SJames Smart 			 "6030 bound lport x%px to DID x%06x\n",
224201649561SJames Smart 			 lport, localport->port_id);
22434410a67aSJames Smart #endif
224401649561SJames Smart }
224501649561SJames Smart 
224601649561SJames Smart int
224701649561SJames Smart lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
224801649561SJames Smart {
22497d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
225001649561SJames Smart 	int ret = 0;
225101649561SJames Smart 	struct nvme_fc_local_port *localport;
225201649561SJames Smart 	struct lpfc_nvme_lport *lport;
225301649561SJames Smart 	struct lpfc_nvme_rport *rport;
225401466024SJames Smart 	struct lpfc_nvme_rport *oldrport;
225501649561SJames Smart 	struct nvme_fc_remote_port *remote_port;
225601649561SJames Smart 	struct nvme_fc_port_info rpinfo;
225793a3922dSJames Smart 	struct lpfc_nodelist *prev_ndlp = NULL;
22581f546823SJames Smart 	struct fc_rport *srport = ndlp->rport;
225901649561SJames Smart 
226001649561SJames Smart 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
226101649561SJames Smart 			 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
226201649561SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type);
226301649561SJames Smart 
226401649561SJames Smart 	localport = vport->localport;
2265bb6a8a2cSDick Kennedy 	if (!localport)
2266bb6a8a2cSDick Kennedy 		return 0;
2267bb6a8a2cSDick Kennedy 
226801649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
226901649561SJames Smart 
22707a06dcd3SJames Smart 	/* NVME rports are not preserved across devloss.
22717a06dcd3SJames Smart 	 * Just register this instance.  Note, rpinfo->dev_loss_tmo
22727a06dcd3SJames Smart 	 * is left 0 to indicate accept transport defaults.  The
22737a06dcd3SJames Smart 	 * driver communicates port role capabilities consistent
22747a06dcd3SJames Smart 	 * with the PRLI response data.
227501649561SJames Smart 	 */
22767a06dcd3SJames Smart 	memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
22777a06dcd3SJames Smart 	rpinfo.port_id = ndlp->nlp_DID;
227801649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET)
22797a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
228001649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
22817a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
228201649561SJames Smart 
22837a06dcd3SJames Smart 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
22847a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
22857a06dcd3SJames Smart 
22867a06dcd3SJames Smart 	rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
22877a06dcd3SJames Smart 	rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
22881f546823SJames Smart 	if (srport)
22891f546823SJames Smart 		rpinfo.dev_loss_tmo = srport->dev_loss_tmo;
22901f546823SJames Smart 	else
22911f546823SJames Smart 		rpinfo.dev_loss_tmo = vport->cfg_devloss_tmo;
229201466024SJames Smart 
2293c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
229401466024SJames Smart 	oldrport = lpfc_ndlp_get_nrport(ndlp);
229561184f17SJames Smart 	if (oldrport) {
229661184f17SJames Smart 		prev_ndlp = oldrport->ndlp;
2297c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
229861184f17SJames Smart 	} else {
2299c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
23004430f7fdSJames Smart 		if (!lpfc_nlp_get(ndlp)) {
23014430f7fdSJames Smart 			dev_warn(&vport->phba->pcidev->dev,
23024430f7fdSJames Smart 				 "Warning - No node ref - exit register\n");
23034430f7fdSJames Smart 			return 0;
23044430f7fdSJames Smart 		}
230561184f17SJames Smart 	}
23063fd78355SJames Smart 
23077a06dcd3SJames Smart 	ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
23087a06dcd3SJames Smart 	if (!ret) {
23097a06dcd3SJames Smart 		/* If the ndlp already has an nrport, this is just
23107a06dcd3SJames Smart 		 * a resume of the existing rport.  Else this is a
23117a06dcd3SJames Smart 		 * new rport.
23127a06dcd3SJames Smart 		 */
2313b15bd3e6SJames Smart 		/* Guard against an unregister/reregister
2314b15bd3e6SJames Smart 		 * race that leaves the WAIT flag set.
2315b15bd3e6SJames Smart 		 */
2316c6adba15SJames Smart 		spin_lock_irq(&ndlp->lock);
2317e9b11083SJames Smart 		ndlp->fc4_xpt_flags &= ~NLP_WAIT_FOR_UNREG;
2318e9b11083SJames Smart 		ndlp->fc4_xpt_flags |= NVME_XPT_REGD;
2319c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
23207a06dcd3SJames Smart 		rport = remote_port->private;
232101466024SJames Smart 		if (oldrport) {
23222b75d0f9SDick Kennedy 
23233fd78355SJames Smart 			/* Sever the ndlp<->rport association
23243fd78355SJames Smart 			 * before dropping the ndlp ref from
23253fd78355SJames Smart 			 * register.
23262b75d0f9SDick Kennedy 			 */
2327c6adba15SJames Smart 			spin_lock_irq(&ndlp->lock);
23282b75d0f9SDick Kennedy 			ndlp->nrport = NULL;
2329e9b11083SJames Smart 			ndlp->fc4_xpt_flags &= ~NLP_WAIT_FOR_UNREG;
2330c6adba15SJames Smart 			spin_unlock_irq(&ndlp->lock);
23312b75d0f9SDick Kennedy 			rport->ndlp = NULL;
23323fd78355SJames Smart 			rport->remoteport = NULL;
2333b04744ceSJames Smart 
2334b04744ceSJames Smart 			/* Reference only removed if previous NDLP is no longer
2335b04744ceSJames Smart 			 * active. It might be just a swap and removing the
2336b04744ceSJames Smart 			 * reference would cause a premature cleanup.
2337b04744ceSJames Smart 			 */
2338b04744ceSJames Smart 			if (prev_ndlp && prev_ndlp != ndlp) {
2339307e3380SJames Smart 				if (!prev_ndlp->nrport)
2340b04744ceSJames Smart 					lpfc_nlp_put(prev_ndlp);
2341b04744ceSJames Smart 			}
23422b75d0f9SDick Kennedy 		}
23432b75d0f9SDick Kennedy 
23442b75d0f9SDick Kennedy 		/* Clean bind the rport to the ndlp. */
234501649561SJames Smart 		rport->remoteport = remote_port;
234601649561SJames Smart 		rport->lport = lport;
23473fd78355SJames Smart 		rport->ndlp = ndlp;
2348c6adba15SJames Smart 		spin_lock_irq(&ndlp->lock);
234901649561SJames Smart 		ndlp->nrport = rport;
2350c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
235101649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO,
235201649561SJames Smart 				 LOG_NVME_DISC | LOG_NODE,
23536825b7bdSJames Smart 				 "6022 Bind lport x%px to remoteport x%px "
23546825b7bdSJames Smart 				 "rport x%px WWNN 0x%llx, "
23557a06dcd3SJames Smart 				 "Rport WWPN 0x%llx DID "
235632350664SJames Smart 				 "x%06x Role x%x, ndlp %p prev_ndlp x%px\n",
235793a3922dSJames Smart 				 lport, remote_port, rport,
235801649561SJames Smart 				 rpinfo.node_name, rpinfo.port_name,
23593fd78355SJames Smart 				 rpinfo.port_id, rpinfo.port_role,
236093a3922dSJames Smart 				 ndlp, prev_ndlp);
236101649561SJames Smart 	} else {
236201649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR,
2363372c187bSDick Kennedy 				 LOG_TRACE_EVENT,
236401649561SJames Smart 				 "6031 RemotePort Registration failed "
236501649561SJames Smart 				 "err: %d, DID x%06x\n",
236601649561SJames Smart 				 ret, ndlp->nlp_DID);
236701649561SJames Smart 	}
23687a06dcd3SJames Smart 
236901649561SJames Smart 	return ret;
2370166d7211SJames Smart #else
2371166d7211SJames Smart 	return 0;
2372166d7211SJames Smart #endif
237301649561SJames Smart }
237401649561SJames Smart 
23753e749170SLee Jones /*
23766f2589f4SJames Smart  * lpfc_nvme_rescan_port - Check to see if we should rescan this remoteport
23776f2589f4SJames Smart  *
23786f2589f4SJames Smart  * If the ndlp represents an NVME Target, that we are logged into,
23796f2589f4SJames Smart  * ping the NVME FC Transport layer to initiate a device rescan
23806f2589f4SJames Smart  * on this remote NPort.
23816f2589f4SJames Smart  */
23826f2589f4SJames Smart void
23836f2589f4SJames Smart lpfc_nvme_rescan_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
23846f2589f4SJames Smart {
23856f2589f4SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2386a6d10f24SJames Smart 	struct lpfc_nvme_rport *nrport;
2387a6d10f24SJames Smart 	struct nvme_fc_remote_port *remoteport = NULL;
23886f2589f4SJames Smart 
2389c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
2390a6d10f24SJames Smart 	nrport = lpfc_ndlp_get_nrport(ndlp);
2391a6d10f24SJames Smart 	if (nrport)
2392a6d10f24SJames Smart 		remoteport = nrport->remoteport;
2393c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
23946f2589f4SJames Smart 
23956f2589f4SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
23966f2589f4SJames Smart 			 "6170 Rescan NPort DID x%06x type x%x "
2397a6d10f24SJames Smart 			 "state x%x nrport x%px remoteport x%px\n",
2398a6d10f24SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type, ndlp->nlp_state,
2399a6d10f24SJames Smart 			 nrport, remoteport);
2400a6d10f24SJames Smart 
2401a6d10f24SJames Smart 	if (!nrport || !remoteport)
2402a6d10f24SJames Smart 		goto rescan_exit;
24036f2589f4SJames Smart 
24046f2589f4SJames Smart 	/* Only rescan if we are an NVME target in the MAPPED state */
24056f2589f4SJames Smart 	if (remoteport->port_role & FC_PORT_ROLE_NVME_DISCOVERY &&
24066f2589f4SJames Smart 	    ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
24076f2589f4SJames Smart 		nvme_fc_rescan_remoteport(remoteport);
24086f2589f4SJames Smart 
2409372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
24106f2589f4SJames Smart 				 "6172 NVME rescanned DID x%06x "
24116f2589f4SJames Smart 				 "port_state x%x\n",
24126f2589f4SJames Smart 				 ndlp->nlp_DID, remoteport->port_state);
24136f2589f4SJames Smart 	}
24146f2589f4SJames Smart 	return;
2415a6d10f24SJames Smart  rescan_exit:
2416a6d10f24SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2417a6d10f24SJames Smart 			 "6169 Skip NVME Rport Rescan, NVME remoteport "
2418a6d10f24SJames Smart 			 "unregistered\n");
24196f2589f4SJames Smart #endif
24206f2589f4SJames Smart }
24216f2589f4SJames Smart 
242201649561SJames Smart /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
242301649561SJames Smart  *
242401649561SJames Smart  * There is no notion of Devloss or rport recovery from the current
242501649561SJames Smart  * nvme_transport perspective.  Loss of an rport just means IO cannot
242601649561SJames Smart  * be sent and recovery is completely up to the initator.
242701649561SJames Smart  * For now, the driver just unbinds the DID and port_role so that
242801649561SJames Smart  * no further IO can be issued.  Changes are planned for later.
242901649561SJames Smart  *
243001649561SJames Smart  * Notes - the ndlp reference count is not decremented here since
243101649561SJames Smart  * since there is no nvme_transport api for devloss.  Node ref count
243201649561SJames Smart  * is only adjusted in driver unload.
243301649561SJames Smart  */
243401649561SJames Smart void
243501649561SJames Smart lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
243601649561SJames Smart {
24377d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
243801649561SJames Smart 	int ret;
243901649561SJames Smart 	struct nvme_fc_local_port *localport;
244001649561SJames Smart 	struct lpfc_nvme_lport *lport;
244101649561SJames Smart 	struct lpfc_nvme_rport *rport;
24429e210178SJames Smart 	struct nvme_fc_remote_port *remoteport = NULL;
244301649561SJames Smart 
244401649561SJames Smart 	localport = vport->localport;
244501649561SJames Smart 
244601649561SJames Smart 	/* This is fundamental error.  The localport is always
244701649561SJames Smart 	 * available until driver unload.  Just exit.
244801649561SJames Smart 	 */
244901649561SJames Smart 	if (!localport)
245001649561SJames Smart 		return;
245101649561SJames Smart 
245201649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
245301649561SJames Smart 	if (!lport)
245401649561SJames Smart 		goto input_err;
245501649561SJames Smart 
2456c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
245701466024SJames Smart 	rport = lpfc_ndlp_get_nrport(ndlp);
24589e210178SJames Smart 	if (rport)
24599e210178SJames Smart 		remoteport = rport->remoteport;
2460c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
24619e210178SJames Smart 	if (!remoteport)
246201649561SJames Smart 		goto input_err;
246301649561SJames Smart 
246401649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
246532350664SJames Smart 			 "6033 Unreg nvme remoteport x%px, portname x%llx, "
2466e9b11083SJames Smart 			 "port_id x%06x, portstate x%x port type x%x "
2467e9b11083SJames Smart 			 "refcnt %d\n",
246801649561SJames Smart 			 remoteport, remoteport->port_name,
246901649561SJames Smart 			 remoteport->port_id, remoteport->port_state,
2470e9b11083SJames Smart 			 ndlp->nlp_type, kref_read(&ndlp->kref));
247101649561SJames Smart 
247201649561SJames Smart 	/* Sanity check ndlp type.  Only call for NVME ports. Don't
247301649561SJames Smart 	 * clear any rport state until the transport calls back.
247401649561SJames Smart 	 */
24753b5bde69SJames Smart 
24763b5bde69SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET) {
24777a06dcd3SJames Smart 		/* No concern about the role change on the nvme remoteport.
24787a06dcd3SJames Smart 		 * The transport will update it.
24797a06dcd3SJames Smart 		 */
2480e9b11083SJames Smart 		spin_lock_irq(&vport->phba->hbalock);
2481e9b11083SJames Smart 		ndlp->fc4_xpt_flags |= NLP_WAIT_FOR_UNREG;
2482e9b11083SJames Smart 		spin_unlock_irq(&vport->phba->hbalock);
24837438273fSJames Smart 
24847438273fSJames Smart 		/* Don't let the host nvme transport keep sending keep-alives
24857438273fSJames Smart 		 * on this remoteport. Vport is unloading, no recovery. The
24867438273fSJames Smart 		 * return values is ignored.  The upcall is a courtesy to the
24877438273fSJames Smart 		 * transport.
24887438273fSJames Smart 		 */
24897438273fSJames Smart 		if (vport->load_flag & FC_UNLOADING)
24907438273fSJames Smart 			(void)nvme_fc_set_remoteport_devloss(remoteport, 0);
24917438273fSJames Smart 
249201649561SJames Smart 		ret = nvme_fc_unregister_remoteport(remoteport);
2493e9b11083SJames Smart 
2494e9b11083SJames Smart 		/* The driver no longer knows if the nrport memory is valid.
2495e9b11083SJames Smart 		 * because the controller teardown process has begun and
2496e9b11083SJames Smart 		 * is asynchronous.  Break the binding in the ndlp. Also
2497e9b11083SJames Smart 		 * remove the register ndlp reference to setup node release.
2498e9b11083SJames Smart 		 */
2499e9b11083SJames Smart 		ndlp->nrport = NULL;
25003fd78355SJames Smart 		lpfc_nlp_put(ndlp);
2501e9b11083SJames Smart 		if (ret != 0) {
2502372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
250301649561SJames Smart 					 "6167 NVME unregister failed %d "
250401649561SJames Smart 					 "port_state x%x\n",
250501649561SJames Smart 					 ret, remoteport->port_state);
25063fd78355SJames Smart 		}
250701649561SJames Smart 	}
250801649561SJames Smart 	return;
250901649561SJames Smart 
251001649561SJames Smart  input_err:
2511166d7211SJames Smart #endif
2512372c187bSDick Kennedy 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
251332350664SJames Smart 			 "6168 State error: lport x%px, rport x%px FCID x%06x\n",
251401649561SJames Smart 			 vport->localport, ndlp->rport, ndlp->nlp_DID);
251501649561SJames Smart }
2516318083adSJames Smart 
2517318083adSJames Smart /**
2518318083adSJames Smart  * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2519318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
2520318083adSJames Smart  * @axri: pointer to the fcp xri abort wcqe structure.
2521c00f62e6SJames Smart  * @lpfc_ncmd: The nvme job structure for the request being aborted.
2522318083adSJames Smart  *
2523318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
2524952c303bSDick Kennedy  * NVME aborted xri.  Aborted NVME IO commands are completed to the transport
2525952c303bSDick Kennedy  * here.
2526318083adSJames Smart  **/
2527318083adSJames Smart void
2528318083adSJames Smart lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2529c00f62e6SJames Smart 			   struct sli4_wcqe_xri_aborted *axri,
2530c00f62e6SJames Smart 			   struct lpfc_io_buf *lpfc_ncmd)
2531318083adSJames Smart {
2532318083adSJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
2533952c303bSDick Kennedy 	struct nvmefc_fcp_req *nvme_cmd = NULL;
2534c00f62e6SJames Smart 	struct lpfc_nodelist *ndlp = lpfc_ncmd->ndlp;
2535318083adSJames Smart 
2536318083adSJames Smart 
2537952c303bSDick Kennedy 	if (ndlp)
2538318083adSJames Smart 		lpfc_sli4_abts_err_handler(phba, ndlp, axri);
253986c67379SJames Smart 
254086c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2541c00f62e6SJames Smart 			"6311 nvme_cmd %p xri x%x tag x%x abort complete and "
2542c00f62e6SJames Smart 			"xri released\n",
2543952c303bSDick Kennedy 			lpfc_ncmd->nvmeCmd, xri,
2544952c303bSDick Kennedy 			lpfc_ncmd->cur_iocbq.iotag);
254586c67379SJames Smart 
2546952c303bSDick Kennedy 	/* Aborted NVME commands are required to not complete
2547952c303bSDick Kennedy 	 * before the abort exchange command fully completes.
2548952c303bSDick Kennedy 	 * Once completed, it is available via the put list.
2549952c303bSDick Kennedy 	 */
25503fd78355SJames Smart 	if (lpfc_ncmd->nvmeCmd) {
2551952c303bSDick Kennedy 		nvme_cmd = lpfc_ncmd->nvmeCmd;
2552952c303bSDick Kennedy 		nvme_cmd->done(nvme_cmd);
25533fd78355SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
25543fd78355SJames Smart 	}
2555318083adSJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
2556318083adSJames Smart }
2557c3725bdcSJames Smart 
2558c3725bdcSJames Smart /**
2559c3725bdcSJames Smart  * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2560c3725bdcSJames Smart  * @phba: Pointer to HBA context object.
2561c3725bdcSJames Smart  *
2562c3725bdcSJames Smart  * This function flushes all wqes in the nvme rings and frees all resources
2563c3725bdcSJames Smart  * in the txcmplq. This function does not issue abort wqes for the IO
2564c3725bdcSJames Smart  * commands in txcmplq, they will just be returned with
2565c3725bdcSJames Smart  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2566c3725bdcSJames Smart  * slot has been permanently disabled.
2567c3725bdcSJames Smart  **/
2568c3725bdcSJames Smart void
2569c3725bdcSJames Smart lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2570c3725bdcSJames Smart {
2571c3725bdcSJames Smart 	struct lpfc_sli_ring  *pring;
2572c3725bdcSJames Smart 	u32 i, wait_cnt = 0;
2573c3725bdcSJames Smart 
2574cdb42becSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4 || !phba->sli4_hba.hdwq)
2575c3725bdcSJames Smart 		return;
2576c3725bdcSJames Smart 
2577c00f62e6SJames Smart 	/* Cycle through all IO rings and make sure all outstanding
2578c3725bdcSJames Smart 	 * WQEs have been removed from the txcmplqs.
2579c3725bdcSJames Smart 	 */
2580cdb42becSJames Smart 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
2581c00f62e6SJames Smart 		if (!phba->sli4_hba.hdwq[i].io_wq)
25826a828b0fSJames Smart 			continue;
2583c00f62e6SJames Smart 		pring = phba->sli4_hba.hdwq[i].io_wq->pring;
2584c3725bdcSJames Smart 
2585d580c613SJames Smart 		if (!pring)
2586d580c613SJames Smart 			continue;
2587d580c613SJames Smart 
2588c3725bdcSJames Smart 		/* Retrieve everything on the txcmplq */
2589c3725bdcSJames Smart 		while (!list_empty(&pring->txcmplq)) {
2590c3725bdcSJames Smart 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
2591c3725bdcSJames Smart 			wait_cnt++;
2592c3725bdcSJames Smart 
2593c3725bdcSJames Smart 			/* The sleep is 10mS.  Every ten seconds,
2594c3725bdcSJames Smart 			 * dump a message.  Something is wrong.
2595c3725bdcSJames Smart 			 */
2596c3725bdcSJames Smart 			if ((wait_cnt % 1000) == 0) {
2597372c187bSDick Kennedy 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2598c3725bdcSJames Smart 						"6178 NVME IO not empty, "
2599c3725bdcSJames Smart 						"cnt %d\n", wait_cnt);
2600c3725bdcSJames Smart 			}
2601c3725bdcSJames Smart 		}
2602c3725bdcSJames Smart 	}
2603a22d73b6SJames Smart 
2604a22d73b6SJames Smart 	/* Make sure HBA is alive */
2605a22d73b6SJames Smart 	lpfc_issue_hb_tmo(phba);
2606a22d73b6SJames Smart 
2607c3725bdcSJames Smart }
260884f2ddf8SJames Smart 
260984f2ddf8SJames Smart void
26109ec58ec7SJames Smart lpfc_nvme_cancel_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
26119ec58ec7SJames Smart 		      uint32_t stat, uint32_t param)
261284f2ddf8SJames Smart {
261384f2ddf8SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
261484f2ddf8SJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
261584f2ddf8SJames Smart 	struct nvmefc_fcp_req *nCmd;
26169ec58ec7SJames Smart 	struct lpfc_wcqe_complete wcqe;
26179ec58ec7SJames Smart 	struct lpfc_wcqe_complete *wcqep = &wcqe;
261884f2ddf8SJames Smart 
26199ec58ec7SJames Smart 	lpfc_ncmd = (struct lpfc_io_buf *)pwqeIn->context1;
26209ec58ec7SJames Smart 	if (!lpfc_ncmd) {
262184f2ddf8SJames Smart 		lpfc_sli_release_iocbq(phba, pwqeIn);
262284f2ddf8SJames Smart 		return;
262384f2ddf8SJames Smart 	}
262484f2ddf8SJames Smart 	/* For abort iocb just return, IO iocb will do a done call */
262584f2ddf8SJames Smart 	if (bf_get(wqe_cmnd, &pwqeIn->wqe.gen_req.wqe_com) ==
262684f2ddf8SJames Smart 	    CMD_ABORT_XRI_CX) {
262784f2ddf8SJames Smart 		lpfc_sli_release_iocbq(phba, pwqeIn);
262884f2ddf8SJames Smart 		return;
262984f2ddf8SJames Smart 	}
263084f2ddf8SJames Smart 
263184f2ddf8SJames Smart 	spin_lock(&lpfc_ncmd->buf_lock);
26329ec58ec7SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
26339ec58ec7SJames Smart 	if (!nCmd) {
263484f2ddf8SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
263584f2ddf8SJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
263684f2ddf8SJames Smart 		return;
263784f2ddf8SJames Smart 	}
26389ec58ec7SJames Smart 	spin_unlock(&lpfc_ncmd->buf_lock);
263984f2ddf8SJames Smart 
264084f2ddf8SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
264184f2ddf8SJames Smart 			"6194 NVME Cancel xri %x\n",
264284f2ddf8SJames Smart 			lpfc_ncmd->cur_iocbq.sli4_xritag);
264384f2ddf8SJames Smart 
26449ec58ec7SJames Smart 	wcqep->word0 = 0;
26459ec58ec7SJames Smart 	bf_set(lpfc_wcqe_c_status, wcqep, stat);
26469ec58ec7SJames Smart 	wcqep->parameter = param;
26479ec58ec7SJames Smart 	wcqep->word3 = 0; /* xb is 0 */
264884f2ddf8SJames Smart 
264984f2ddf8SJames Smart 	/* Call release with XB=1 to queue the IO into the abort list. */
26509ec58ec7SJames Smart 	if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
26519ec58ec7SJames Smart 		bf_set(lpfc_wcqe_c_xb, wcqep, 1);
26529ec58ec7SJames Smart 
26539ec58ec7SJames Smart 	(pwqeIn->wqe_cmpl)(phba, pwqeIn, wcqep);
265484f2ddf8SJames Smart #endif
265584f2ddf8SJames Smart }
2656