xref: /linux/drivers/scsi/lpfc/lpfc_nvme.c (revision 982fc3965d1350d3332e04046b0e101006184ba9)
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) {
193f1156125SJames Smart 		pr_err("**** %s: NULL ndlp on rport x%px remoteport x%px\n",
194e9b11083SJames Smart 		       __func__, rport, remoteport);
19501649561SJames Smart 		goto rport_err;
196e9b11083SJames Smart 	}
19701649561SJames Smart 
19801649561SJames Smart 	vport = ndlp->vport;
199e9b11083SJames Smart 	if (!vport) {
200f1156125SJames Smart 		pr_err("**** %s: Null vport on ndlp x%px, ste x%x rport x%px\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,
212*982fc396SJames Smart 			 "6146 remoteport delete of remoteport x%px, ndlp x%px "
213*982fc396SJames Smart 			 "DID x%x xflags x%x\n",
214*982fc396SJames Smart 			 remoteport, ndlp, ndlp->nlp_DID, ndlp->fc4_xpt_flags);
215c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
216b15bd3e6SJames Smart 
217b15bd3e6SJames Smart 	/* The register rebind might have occurred before the delete
218b15bd3e6SJames Smart 	 * downcall.  Guard against this race.
219b15bd3e6SJames Smart 	 */
22006145683SJames Smart 	if (ndlp->fc4_xpt_flags & NVME_XPT_UNREG_WAIT)
22106145683SJames Smart 		ndlp->fc4_xpt_flags &= ~(NVME_XPT_UNREG_WAIT | NVME_XPT_REGD);
222e9b11083SJames Smart 
223c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
2243fd78355SJames Smart 
225e9b11083SJames Smart 	/* On a devloss timeout event, one more put is executed provided the
226e9b11083SJames Smart 	 * NVME and SCSI rport unregister requests are complete.  If the vport
227e9b11083SJames Smart 	 * is unloading, this extra put is executed by lpfc_drop_node.
2283fd78355SJames Smart 	 */
229e9b11083SJames Smart 	if (!(ndlp->fc4_xpt_flags & fc4_xpt_flags))
230e9b11083SJames Smart 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
23101649561SJames Smart 
23201649561SJames Smart  rport_err:
2333fd78355SJames Smart 	return;
23401649561SJames Smart }
23501649561SJames Smart 
2363a8070c5SJames Smart /**
2373a8070c5SJames Smart  * lpfc_nvme_handle_lsreq - Process an unsolicited NVME LS request
2383a8070c5SJames Smart  * @phba: pointer to lpfc hba data structure.
2393a8070c5SJames Smart  * @axchg: pointer to exchange context for the NVME LS request
2403a8070c5SJames Smart  *
2413a8070c5SJames Smart  * This routine is used for processing an asychronously received NVME LS
2423a8070c5SJames Smart  * request. Any remaining validation is done and the LS is then forwarded
2433a8070c5SJames Smart  * to the nvme-fc transport via nvme_fc_rcv_ls_req().
2443a8070c5SJames Smart  *
2459aa09e98SJames Smart  * The calling sequence should be: nvme_fc_rcv_ls_req() -> (processing)
2469aa09e98SJames Smart  * -> lpfc_nvme_xmt_ls_rsp/cmp -> req->done.
2479aa09e98SJames Smart  * __lpfc_nvme_xmt_ls_rsp_cmp should free the allocated axchg.
2489aa09e98SJames Smart  *
2493a8070c5SJames Smart  * Returns 0 if LS was handled and delivered to the transport
2503a8070c5SJames Smart  * Returns 1 if LS failed to be handled and should be dropped
2513a8070c5SJames Smart  */
2523a8070c5SJames Smart int
2533a8070c5SJames Smart lpfc_nvme_handle_lsreq(struct lpfc_hba *phba,
2543a8070c5SJames Smart 			struct lpfc_async_xchg_ctx *axchg)
2553a8070c5SJames Smart {
2569aa09e98SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2579aa09e98SJames Smart 	struct lpfc_vport *vport;
2589aa09e98SJames Smart 	struct lpfc_nvme_rport *lpfc_rport;
2599aa09e98SJames Smart 	struct nvme_fc_remote_port *remoteport;
2609aa09e98SJames Smart 	struct lpfc_nvme_lport *lport;
2619aa09e98SJames Smart 	uint32_t *payload = axchg->payload;
2629aa09e98SJames Smart 	int rc;
2639aa09e98SJames Smart 
2649aa09e98SJames Smart 	vport = axchg->ndlp->vport;
2659aa09e98SJames Smart 	lpfc_rport = axchg->ndlp->nrport;
2669aa09e98SJames Smart 	if (!lpfc_rport)
2679aa09e98SJames Smart 		return -EINVAL;
2689aa09e98SJames Smart 
2699aa09e98SJames Smart 	remoteport = lpfc_rport->remoteport;
2709aa09e98SJames Smart 	if (!vport->localport)
2719aa09e98SJames Smart 		return -EINVAL;
2729aa09e98SJames Smart 
2739aa09e98SJames Smart 	lport = vport->localport->private;
2749aa09e98SJames Smart 	if (!lport)
2759aa09e98SJames Smart 		return -EINVAL;
2769aa09e98SJames Smart 
2779aa09e98SJames Smart 	rc = nvme_fc_rcv_ls_req(remoteport, &axchg->ls_rsp, axchg->payload,
2789aa09e98SJames Smart 				axchg->size);
2799aa09e98SJames Smart 
2809aa09e98SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2819aa09e98SJames Smart 			"6205 NVME Unsol rcv: sz %d rc %d: %08x %08x %08x "
2829aa09e98SJames Smart 			"%08x %08x %08x\n",
2839aa09e98SJames Smart 			axchg->size, rc,
2849aa09e98SJames Smart 			*payload, *(payload+1), *(payload+2),
2859aa09e98SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
2869aa09e98SJames Smart 
2879aa09e98SJames Smart 	if (!rc)
2889aa09e98SJames Smart 		return 0;
2899aa09e98SJames Smart #endif
2903a8070c5SJames Smart 	return 1;
2913a8070c5SJames Smart }
2923a8070c5SJames Smart 
2936514b25dSJames Smart /**
2946514b25dSJames Smart  * __lpfc_nvme_ls_req_cmp - Generic completion handler for a NVME
2956514b25dSJames Smart  *        LS request.
2966514b25dSJames Smart  * @phba: Pointer to HBA context object
2976514b25dSJames Smart  * @vport: The local port that issued the LS
2986514b25dSJames Smart  * @cmdwqe: Pointer to driver command WQE object.
2996514b25dSJames Smart  * @wcqe: Pointer to driver response CQE object.
3006514b25dSJames Smart  *
3016514b25dSJames Smart  * This function is the generic completion handler for NVME LS requests.
3026514b25dSJames Smart  * The function updates any states and statistics, calls the transport
3036514b25dSJames Smart  * ls_req done() routine, then tears down the command and buffers used
3046514b25dSJames Smart  * for the LS request.
3056514b25dSJames Smart  **/
3066514b25dSJames Smart void
3076514b25dSJames Smart __lpfc_nvme_ls_req_cmp(struct lpfc_hba *phba,  struct lpfc_vport *vport,
3086514b25dSJames Smart 			struct lpfc_iocbq *cmdwqe,
3096514b25dSJames Smart 			struct lpfc_wcqe_complete *wcqe)
3106514b25dSJames Smart {
3116514b25dSJames Smart 	struct nvmefc_ls_req *pnvme_lsreq;
3126514b25dSJames Smart 	struct lpfc_dmabuf *buf_ptr;
3136514b25dSJames Smart 	struct lpfc_nodelist *ndlp;
3146514b25dSJames Smart 	uint32_t status;
3156514b25dSJames Smart 
3166514b25dSJames Smart 	pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
3176514b25dSJames Smart 	ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
3186514b25dSJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
3196514b25dSJames Smart 
3206514b25dSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
321f1156125SJames Smart 			 "6047 NVMEx LS REQ x%px cmpl DID %x Xri: %x "
3226514b25dSJames Smart 			 "status %x reason x%x cmd:x%px lsreg:x%px bmp:x%px "
3236514b25dSJames Smart 			 "ndlp:x%px\n",
3246514b25dSJames Smart 			 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
3256514b25dSJames Smart 			 cmdwqe->sli4_xritag, status,
3266514b25dSJames Smart 			 (wcqe->parameter & 0xffff),
3276514b25dSJames Smart 			 cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
3286514b25dSJames Smart 
3296514b25dSJames Smart 	lpfc_nvmeio_data(phba, "NVMEx LS CMPL: xri x%x stat x%x parm x%x\n",
3306514b25dSJames Smart 			 cmdwqe->sli4_xritag, status, wcqe->parameter);
3316514b25dSJames Smart 
3326514b25dSJames Smart 	if (cmdwqe->context3) {
3336514b25dSJames Smart 		buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
3346514b25dSJames Smart 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3356514b25dSJames Smart 		kfree(buf_ptr);
3366514b25dSJames Smart 		cmdwqe->context3 = NULL;
3376514b25dSJames Smart 	}
3386514b25dSJames Smart 	if (pnvme_lsreq->done)
3396514b25dSJames Smart 		pnvme_lsreq->done(pnvme_lsreq, status);
3406514b25dSJames Smart 	else
341372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3426514b25dSJames Smart 				 "6046 NVMEx cmpl without done call back? "
343f1156125SJames Smart 				 "Data x%px DID %x Xri: %x status %x\n",
3446514b25dSJames Smart 				pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
3456514b25dSJames Smart 				cmdwqe->sli4_xritag, status);
3466514b25dSJames Smart 	if (ndlp) {
3476514b25dSJames Smart 		lpfc_nlp_put(ndlp);
3486514b25dSJames Smart 		cmdwqe->context1 = NULL;
3496514b25dSJames Smart 	}
3506514b25dSJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
3516514b25dSJames Smart }
3526514b25dSJames Smart 
35301649561SJames Smart static void
3546514b25dSJames Smart lpfc_nvme_ls_req_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
35501649561SJames Smart 		       struct lpfc_wcqe_complete *wcqe)
35601649561SJames Smart {
35701649561SJames Smart 	struct lpfc_vport *vport = cmdwqe->vport;
3584b056682SJames Smart 	struct lpfc_nvme_lport *lport;
35901649561SJames Smart 	uint32_t status;
36001649561SJames Smart 
36101649561SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
36266a210ffSJames Smart 
36366a85155SJames Smart 	if (vport->localport) {
36466a85155SJames Smart 		lport = (struct lpfc_nvme_lport *)vport->localport->private;
36566a210ffSJames Smart 		if (lport) {
36666a210ffSJames Smart 			atomic_inc(&lport->fc4NvmeLsCmpls);
3674b056682SJames Smart 			if (status) {
3684b056682SJames Smart 				if (bf_get(lpfc_wcqe_c_xb, wcqe))
3694b056682SJames Smart 					atomic_inc(&lport->cmpl_ls_xb);
3704b056682SJames Smart 				atomic_inc(&lport->cmpl_ls_err);
3714b056682SJames Smart 			}
37266a210ffSJames Smart 		}
37366a85155SJames Smart 	}
3744b056682SJames Smart 
3756514b25dSJames Smart 	__lpfc_nvme_ls_req_cmp(phba, vport, cmdwqe, wcqe);
37601649561SJames Smart }
37701649561SJames Smart 
37801649561SJames Smart static int
37901649561SJames Smart lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
38001649561SJames Smart 		  struct lpfc_dmabuf *inp,
38101649561SJames Smart 		  struct nvmefc_ls_req *pnvme_lsreq,
38201649561SJames Smart 		  void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
38301649561SJames Smart 			       struct lpfc_wcqe_complete *),
38401649561SJames Smart 		  struct lpfc_nodelist *ndlp, uint32_t num_entry,
38501649561SJames Smart 		  uint32_t tmo, uint8_t retry)
38601649561SJames Smart {
38701649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
388205e8240SJames Smart 	union lpfc_wqe128 *wqe;
38901649561SJames Smart 	struct lpfc_iocbq *genwqe;
39001649561SJames Smart 	struct ulp_bde64 *bpl;
39101649561SJames Smart 	struct ulp_bde64 bde;
39201649561SJames Smart 	int i, rc, xmit_len, first_len;
39301649561SJames Smart 
39401649561SJames Smart 	/* Allocate buffer for  command WQE */
39501649561SJames Smart 	genwqe = lpfc_sli_get_iocbq(phba);
39601649561SJames Smart 	if (genwqe == NULL)
39701649561SJames Smart 		return 1;
39801649561SJames Smart 
39901649561SJames Smart 	wqe = &genwqe->wqe;
400d9f492a1SJames Smart 	/* Initialize only 64 bytes */
40101649561SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
40201649561SJames Smart 
40301649561SJames Smart 	genwqe->context3 = (uint8_t *)bmp;
40401649561SJames Smart 	genwqe->iocb_flag |= LPFC_IO_NVME_LS;
40501649561SJames Smart 
40601649561SJames Smart 	/* Save for completion so we can release these resources */
40701649561SJames Smart 	genwqe->context1 = lpfc_nlp_get(ndlp);
4084430f7fdSJames Smart 	if (!genwqe->context1) {
4094430f7fdSJames Smart 		dev_warn(&phba->pcidev->dev,
4104430f7fdSJames Smart 			 "Warning: Failed node ref, not sending LS_REQ\n");
4114430f7fdSJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
4124430f7fdSJames Smart 		return 1;
4134430f7fdSJames Smart 	}
4144430f7fdSJames Smart 
41501649561SJames Smart 	genwqe->context2 = (uint8_t *)pnvme_lsreq;
41601649561SJames Smart 	/* Fill in payload, bp points to frame payload */
41701649561SJames Smart 
41801649561SJames Smart 	if (!tmo)
41901649561SJames Smart 		/* FC spec states we need 3 * ratov for CT requests */
42001649561SJames Smart 		tmo = (3 * phba->fc_ratov);
42101649561SJames Smart 
42201649561SJames Smart 	/* For this command calculate the xmit length of the request bde. */
42301649561SJames Smart 	xmit_len = 0;
42401649561SJames Smart 	first_len = 0;
42501649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
42601649561SJames Smart 	for (i = 0; i < num_entry; i++) {
42701649561SJames Smart 		bde.tus.w = bpl[i].tus.w;
42801649561SJames Smart 		if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
42901649561SJames Smart 			break;
43001649561SJames Smart 		xmit_len += bde.tus.f.bdeSize;
43101649561SJames Smart 		if (i == 0)
43201649561SJames Smart 			first_len = xmit_len;
43301649561SJames Smart 	}
43401649561SJames Smart 
43501649561SJames Smart 	genwqe->rsvd2 = num_entry;
43601649561SJames Smart 	genwqe->hba_wqidx = 0;
43701649561SJames Smart 
43801649561SJames Smart 	/* Words 0 - 2 */
43901649561SJames Smart 	wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
44001649561SJames Smart 	wqe->generic.bde.tus.f.bdeSize = first_len;
44101649561SJames Smart 	wqe->generic.bde.addrLow = bpl[0].addrLow;
44201649561SJames Smart 	wqe->generic.bde.addrHigh = bpl[0].addrHigh;
44301649561SJames Smart 
44401649561SJames Smart 	/* Word 3 */
44501649561SJames Smart 	wqe->gen_req.request_payload_len = first_len;
44601649561SJames Smart 
44701649561SJames Smart 	/* Word 4 */
44801649561SJames Smart 
44901649561SJames Smart 	/* Word 5 */
45001649561SJames Smart 	bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
45101649561SJames Smart 	bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
45201649561SJames Smart 	bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
4538b361639SJames Smart 	bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
45401649561SJames Smart 	bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
45501649561SJames Smart 
45601649561SJames Smart 	/* Word 6 */
45701649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
45801649561SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
45901649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
46001649561SJames Smart 
46101649561SJames Smart 	/* Word 7 */
462c33b1609SJames Smart 	bf_set(wqe_tmo, &wqe->gen_req.wqe_com, tmo);
46301649561SJames Smart 	bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
46401649561SJames Smart 	bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
46501649561SJames Smart 	bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
46601649561SJames Smart 
46701649561SJames Smart 	/* Word 8 */
46801649561SJames Smart 	wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
46901649561SJames Smart 
47001649561SJames Smart 	/* Word 9 */
47101649561SJames Smart 	bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
47201649561SJames Smart 
47301649561SJames Smart 	/* Word 10 */
47401649561SJames Smart 	bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
47501649561SJames Smart 	bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
47601649561SJames Smart 	bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
47701649561SJames Smart 	bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
47801649561SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
47901649561SJames Smart 
48001649561SJames Smart 	/* Word 11 */
48101649561SJames Smart 	bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
48201649561SJames Smart 	bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
48301649561SJames Smart 
48401649561SJames Smart 
48501649561SJames Smart 	/* Issue GEN REQ WQE for NPORT <did> */
48601649561SJames Smart 	genwqe->wqe_cmpl = cmpl;
48701649561SJames Smart 	genwqe->iocb_cmpl = NULL;
48801649561SJames Smart 	genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
48901649561SJames Smart 	genwqe->vport = vport;
49001649561SJames Smart 	genwqe->retry = retry;
49101649561SJames Smart 
492bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  XMIT: xri x%x iotag x%x to x%06x\n",
493bd2cdd5eSJames Smart 			 genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
494bd2cdd5eSJames Smart 
4951fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], genwqe);
496cd22d605SDick Kennedy 	if (rc) {
497372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
49801649561SJames Smart 				 "6045 Issue GEN REQ WQE to NPORT x%x "
4996514b25dSJames Smart 				 "Data: x%x x%x  rc x%x\n",
50001649561SJames Smart 				 ndlp->nlp_DID, genwqe->iotag,
5016514b25dSJames Smart 				 vport->port_state, rc);
502e9b11083SJames Smart 		lpfc_nlp_put(ndlp);
50301649561SJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
50401649561SJames Smart 		return 1;
50501649561SJames Smart 	}
5066514b25dSJames Smart 
5076514b25dSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_ELS,
5086514b25dSJames Smart 			 "6050 Issue GEN REQ WQE to NPORT x%x "
5096514b25dSJames Smart 			 "Data: oxid: x%x state: x%x wq:x%px lsreq:x%px "
5106514b25dSJames Smart 			 "bmp:x%px xmit:%d 1st:%d\n",
5116514b25dSJames Smart 			 ndlp->nlp_DID, genwqe->sli4_xritag,
5126514b25dSJames Smart 			 vport->port_state,
5136514b25dSJames Smart 			 genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
51401649561SJames Smart 	return 0;
51501649561SJames Smart }
51601649561SJames Smart 
5176514b25dSJames Smart 
51801649561SJames Smart /**
5196514b25dSJames Smart  * __lpfc_nvme_ls_req - Generic service routine to issue an NVME LS request
5206514b25dSJames Smart  * @vport: The local port issuing the LS
5216514b25dSJames Smart  * @ndlp: The remote port to send the LS to
5226514b25dSJames Smart  * @pnvme_lsreq: Pointer to LS request structure from the transport
52309d99705SLee Jones  * @gen_req_cmp: Completion call-back
52401649561SJames Smart  *
5256514b25dSJames Smart  * Routine validates the ndlp, builds buffers and sends a GEN_REQUEST
5266514b25dSJames Smart  * WQE to perform the LS operation.
52701649561SJames Smart  *
52801649561SJames Smart  * Return value :
52901649561SJames Smart  *   0 - Success
5306514b25dSJames Smart  *   non-zero: various error codes, in form of -Exxx
53101649561SJames Smart  **/
5326514b25dSJames Smart int
5336514b25dSJames Smart __lpfc_nvme_ls_req(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5346514b25dSJames Smart 		      struct nvmefc_ls_req *pnvme_lsreq,
5356514b25dSJames Smart 		      void (*gen_req_cmp)(struct lpfc_hba *phba,
5366514b25dSJames Smart 				struct lpfc_iocbq *cmdwqe,
5376514b25dSJames Smart 				struct lpfc_wcqe_complete *wcqe))
53801649561SJames Smart {
53901649561SJames Smart 	struct lpfc_dmabuf *bmp;
5406514b25dSJames Smart 	struct ulp_bde64 *bpl;
5416514b25dSJames Smart 	int ret;
542ba43c4d0SJames Smart 	uint16_t ntype, nstate;
54301649561SJames Smart 
544307e3380SJames Smart 	if (!ndlp) {
545372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5466514b25dSJames Smart 				 "6051 NVMEx LS REQ: Bad NDLP x%px, Failing "
5476514b25dSJames Smart 				 "LS Req\n",
5486514b25dSJames Smart 				 ndlp);
549ba43c4d0SJames Smart 		return -ENODEV;
550ba43c4d0SJames Smart 	}
551ba43c4d0SJames Smart 
552ba43c4d0SJames Smart 	ntype = ndlp->nlp_type;
553ba43c4d0SJames Smart 	nstate = ndlp->nlp_state;
554ba43c4d0SJames Smart 	if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
555ba43c4d0SJames Smart 	    (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
556372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5576514b25dSJames Smart 				 "6088 NVMEx LS REQ: Fail DID x%06x not "
5586514b25dSJames Smart 				 "ready for IO. Type x%x, State x%x\n",
5596514b25dSJames Smart 				 ndlp->nlp_DID, ntype, nstate);
560ba43c4d0SJames Smart 		return -ENODEV;
56101649561SJames Smart 	}
5626514b25dSJames Smart 
5638c65830aSJames Smart 	if (!vport->phba->sli4_hba.nvmels_wq)
5648c65830aSJames Smart 		return -ENOMEM;
5658c65830aSJames Smart 
5666514b25dSJames Smart 	/*
5676514b25dSJames Smart 	 * there are two dma buf in the request, actually there is one and
5686514b25dSJames Smart 	 * the second one is just the start address + cmd size.
5696514b25dSJames Smart 	 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
5706514b25dSJames Smart 	 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
5716514b25dSJames Smart 	 * because the nvem layer owns the data bufs.
5726514b25dSJames Smart 	 * We do not have to break these packets open, we don't care what is
5736514b25dSJames Smart 	 * in them. And we do not have to look at the resonse data, we only
5746514b25dSJames Smart 	 * care that we got a response. All of the caring is going to happen
5756514b25dSJames Smart 	 * in the nvme-fc layer.
5766514b25dSJames Smart 	 */
5776514b25dSJames Smart 
5786514b25dSJames Smart 	bmp = kmalloc(sizeof(*bmp), GFP_KERNEL);
57901649561SJames Smart 	if (!bmp) {
580372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5816514b25dSJames Smart 				 "6044 NVMEx LS REQ: Could not alloc LS buf "
5826514b25dSJames Smart 				 "for DID %x\n",
5836514b25dSJames Smart 				 ndlp->nlp_DID);
5846514b25dSJames Smart 		return -ENOMEM;
58501649561SJames Smart 	}
5866514b25dSJames Smart 
58701649561SJames Smart 	bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
58801649561SJames Smart 	if (!bmp->virt) {
589372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5906514b25dSJames Smart 				 "6042 NVMEx LS REQ: Could not alloc mbuf "
5916514b25dSJames Smart 				 "for DID %x\n",
5926514b25dSJames Smart 				 ndlp->nlp_DID);
59301649561SJames Smart 		kfree(bmp);
5946514b25dSJames Smart 		return -ENOMEM;
59501649561SJames Smart 	}
5966514b25dSJames Smart 
5976514b25dSJames Smart 	INIT_LIST_HEAD(&bmp->list);
5986514b25dSJames Smart 
59901649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
60001649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
60101649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
60201649561SJames Smart 	bpl->tus.f.bdeFlags = 0;
60301649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
60401649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
60501649561SJames Smart 	bpl++;
60601649561SJames Smart 
60701649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
60801649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
60901649561SJames Smart 	bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
61001649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
61101649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
61201649561SJames Smart 
61301649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
6146514b25dSJames Smart 			"6149 NVMEx LS REQ: Issue to DID 0x%06x lsreq x%px, "
6156514b25dSJames Smart 			"rqstlen:%d rsplen:%d %pad %pad\n",
6166514b25dSJames Smart 			ndlp->nlp_DID, pnvme_lsreq, pnvme_lsreq->rqstlen,
617825c6abbSArnd Bergmann 			pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
618825c6abbSArnd Bergmann 			&pnvme_lsreq->rspdma);
61901649561SJames Smart 
62001649561SJames Smart 	ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
6216514b25dSJames Smart 				pnvme_lsreq, gen_req_cmp, ndlp, 2,
622c33b1609SJames Smart 				pnvme_lsreq->timeout, 0);
62301649561SJames Smart 	if (ret != WQE_SUCCESS) {
624372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6256514b25dSJames Smart 				 "6052 NVMEx REQ: EXIT. issue ls wqe failed "
6266514b25dSJames Smart 				 "lsreq x%px Status %x DID %x\n",
6276514b25dSJames Smart 				 pnvme_lsreq, ret, ndlp->nlp_DID);
62801649561SJames Smart 		lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
62901649561SJames Smart 		kfree(bmp);
6306514b25dSJames Smart 		return -EIO;
63101649561SJames Smart 	}
63201649561SJames Smart 
6336514b25dSJames Smart 	return 0;
6346514b25dSJames Smart }
6356514b25dSJames Smart 
6366514b25dSJames Smart /**
6376514b25dSJames Smart  * lpfc_nvme_ls_req - Issue an NVME Link Service request
63809d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from.
6393e749170SLee Jones  * @pnvme_rport: Transport remoteport that LS is to be sent to.
64009d99705SLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
6416514b25dSJames Smart  *
6426514b25dSJames Smart  * Driver registers this routine to handle any link service request
6436514b25dSJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
6446514b25dSJames Smart  *
6456514b25dSJames Smart  * Return value :
6466514b25dSJames Smart  *   0 - Success
6476514b25dSJames Smart  *   non-zero: various error codes, in form of -Exxx
6486514b25dSJames Smart  **/
6496514b25dSJames Smart static int
6506514b25dSJames Smart lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
6516514b25dSJames Smart 		 struct nvme_fc_remote_port *pnvme_rport,
6526514b25dSJames Smart 		 struct nvmefc_ls_req *pnvme_lsreq)
6536514b25dSJames Smart {
6546514b25dSJames Smart 	struct lpfc_nvme_lport *lport;
6556514b25dSJames Smart 	struct lpfc_nvme_rport *rport;
6566514b25dSJames Smart 	struct lpfc_vport *vport;
6576514b25dSJames Smart 	int ret;
6586514b25dSJames Smart 
6596514b25dSJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
6606514b25dSJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
6616514b25dSJames Smart 	if (unlikely(!lport) || unlikely(!rport))
6626514b25dSJames Smart 		return -EINVAL;
6636514b25dSJames Smart 
6646514b25dSJames Smart 	vport = lport->vport;
6656514b25dSJames Smart 	if (vport->load_flag & FC_UNLOADING)
6666514b25dSJames Smart 		return -ENODEV;
6676514b25dSJames Smart 
6686514b25dSJames Smart 	atomic_inc(&lport->fc4NvmeLsRequests);
6696514b25dSJames Smart 
6706514b25dSJames Smart 	ret = __lpfc_nvme_ls_req(vport, rport->ndlp, pnvme_lsreq,
6716514b25dSJames Smart 				 lpfc_nvme_ls_req_cmp);
6726514b25dSJames Smart 	if (ret)
6736514b25dSJames Smart 		atomic_inc(&lport->xmt_ls_err);
6746514b25dSJames Smart 
67501649561SJames Smart 	return ret;
67601649561SJames Smart }
67701649561SJames Smart 
67801649561SJames Smart /**
679e96a22b0SJames Smart  * __lpfc_nvme_ls_abort - Generic service routine to abort a prior
680e96a22b0SJames Smart  *         NVME LS request
681e96a22b0SJames Smart  * @vport: The local port that issued the LS
682e96a22b0SJames Smart  * @ndlp: The remote port the LS was sent to
683e96a22b0SJames Smart  * @pnvme_lsreq: Pointer to LS request structure from the transport
68401649561SJames Smart  *
685e96a22b0SJames Smart  * The driver validates the ndlp, looks for the LS, and aborts the
686e96a22b0SJames Smart  * LS if found.
68701649561SJames Smart  *
688e96a22b0SJames Smart  * Returns:
689e96a22b0SJames Smart  * 0 : if LS found and aborted
690e96a22b0SJames Smart  * non-zero: various error conditions in form -Exxx
691e96a22b0SJames Smart  **/
692e96a22b0SJames Smart int
693e96a22b0SJames Smart __lpfc_nvme_ls_abort(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
694e96a22b0SJames Smart 			struct nvmefc_ls_req *pnvme_lsreq)
695e96a22b0SJames Smart {
696e96a22b0SJames Smart 	struct lpfc_hba *phba = vport->phba;
697e96a22b0SJames Smart 	struct lpfc_sli_ring *pring;
698e96a22b0SJames Smart 	struct lpfc_iocbq *wqe, *next_wqe;
699e96a22b0SJames Smart 	bool foundit = false;
700e96a22b0SJames Smart 
701e96a22b0SJames Smart 	if (!ndlp) {
702372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
703e96a22b0SJames Smart 				"6049 NVMEx LS REQ Abort: Bad NDLP x%px DID "
704e96a22b0SJames Smart 				"x%06x, Failing LS Req\n",
705e96a22b0SJames Smart 				ndlp, ndlp ? ndlp->nlp_DID : 0);
706e96a22b0SJames Smart 		return -EINVAL;
707e96a22b0SJames Smart 	}
708e96a22b0SJames Smart 
709e96a22b0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_NVME_ABTS,
710e96a22b0SJames Smart 			 "6040 NVMEx LS REQ Abort: Issue LS_ABORT for lsreq "
711f1156125SJames Smart 			 "x%px rqstlen:%d rsplen:%d %pad %pad\n",
712e96a22b0SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
713e96a22b0SJames Smart 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
714e96a22b0SJames Smart 			 &pnvme_lsreq->rspdma);
715e96a22b0SJames Smart 
716e96a22b0SJames Smart 	/*
717e96a22b0SJames Smart 	 * Lock the ELS ring txcmplq and look for the wqe that matches
718e96a22b0SJames Smart 	 * this ELS. If found, issue an abort on the wqe.
719e96a22b0SJames Smart 	 */
720e96a22b0SJames Smart 	pring = phba->sli4_hba.nvmels_wq->pring;
721e96a22b0SJames Smart 	spin_lock_irq(&phba->hbalock);
722e96a22b0SJames Smart 	spin_lock(&pring->ring_lock);
723e96a22b0SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
724e96a22b0SJames Smart 		if (wqe->context2 == pnvme_lsreq) {
725e96a22b0SJames Smart 			wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
726e96a22b0SJames Smart 			foundit = true;
727e96a22b0SJames Smart 			break;
728e96a22b0SJames Smart 		}
729e96a22b0SJames Smart 	}
730e96a22b0SJames Smart 	spin_unlock(&pring->ring_lock);
731e96a22b0SJames Smart 
732e96a22b0SJames Smart 	if (foundit)
733db7531d2SJames Smart 		lpfc_sli_issue_abort_iotag(phba, pring, wqe, NULL);
734e96a22b0SJames Smart 	spin_unlock_irq(&phba->hbalock);
735e96a22b0SJames Smart 
736e96a22b0SJames Smart 	if (foundit)
737e96a22b0SJames Smart 		return 0;
738e96a22b0SJames Smart 
739e96a22b0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_NVME_ABTS,
740f1156125SJames Smart 			 "6213 NVMEx LS REQ Abort: Unable to locate req x%px\n",
741e96a22b0SJames Smart 			 pnvme_lsreq);
7426b6e8963SJames Smart 	return -EINVAL;
743e96a22b0SJames Smart }
744e96a22b0SJames Smart 
7459aa09e98SJames Smart static int
7469aa09e98SJames Smart lpfc_nvme_xmt_ls_rsp(struct nvme_fc_local_port *localport,
7479aa09e98SJames Smart 		     struct nvme_fc_remote_port *remoteport,
7489aa09e98SJames Smart 		     struct nvmefc_ls_rsp *ls_rsp)
7499aa09e98SJames Smart {
7509aa09e98SJames Smart 	struct lpfc_async_xchg_ctx *axchg =
7519aa09e98SJames Smart 		container_of(ls_rsp, struct lpfc_async_xchg_ctx, ls_rsp);
7529aa09e98SJames Smart 	struct lpfc_nvme_lport *lport;
7539aa09e98SJames Smart 	int rc;
7549aa09e98SJames Smart 
7559aa09e98SJames Smart 	if (axchg->phba->pport->load_flag & FC_UNLOADING)
7569aa09e98SJames Smart 		return -ENODEV;
7579aa09e98SJames Smart 
7589aa09e98SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
7599aa09e98SJames Smart 
7609aa09e98SJames Smart 	rc = __lpfc_nvme_xmt_ls_rsp(axchg, ls_rsp, __lpfc_nvme_xmt_ls_rsp_cmp);
7619aa09e98SJames Smart 
7629aa09e98SJames Smart 	if (rc) {
7639aa09e98SJames Smart 		/*
7649aa09e98SJames Smart 		 * unless the failure is due to having already sent
7659aa09e98SJames Smart 		 * the response, an abort will be generated for the
7669aa09e98SJames Smart 		 * exchange if the rsp can't be sent.
7679aa09e98SJames Smart 		 */
7689aa09e98SJames Smart 		if (rc != -EALREADY)
7699aa09e98SJames Smart 			atomic_inc(&lport->xmt_ls_abort);
7709aa09e98SJames Smart 		return rc;
7719aa09e98SJames Smart 	}
7729aa09e98SJames Smart 
7739aa09e98SJames Smart 	return 0;
7749aa09e98SJames Smart }
7759aa09e98SJames Smart 
776e96a22b0SJames Smart /**
777e96a22b0SJames Smart  * lpfc_nvme_ls_abort - Abort a prior NVME LS request
77809d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from.
77909d99705SLee Jones  * @pnvme_rport: Transport remoteport that LS is to be sent to.
78009d99705SLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
781e96a22b0SJames Smart  *
782e96a22b0SJames Smart  * Driver registers this routine to abort a NVME LS request that is
783e96a22b0SJames Smart  * in progress (from the transports perspective).
78401649561SJames Smart  **/
78501649561SJames Smart static void
78601649561SJames Smart lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
78701649561SJames Smart 		   struct nvme_fc_remote_port *pnvme_rport,
78801649561SJames Smart 		   struct nvmefc_ls_req *pnvme_lsreq)
78901649561SJames Smart {
79001649561SJames Smart 	struct lpfc_nvme_lport *lport;
79101649561SJames Smart 	struct lpfc_vport *vport;
79201649561SJames Smart 	struct lpfc_nodelist *ndlp;
793e96a22b0SJames Smart 	int ret;
79401649561SJames Smart 
79501649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
79666a210ffSJames Smart 	if (unlikely(!lport))
79766a210ffSJames Smart 		return;
79801649561SJames Smart 	vport = lport->vport;
79901649561SJames Smart 
8003386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
8013386f4bdSJames Smart 		return;
8023386f4bdSJames Smart 
80301649561SJames Smart 	ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
80401649561SJames Smart 
805e96a22b0SJames Smart 	ret = __lpfc_nvme_ls_abort(vport, ndlp, pnvme_lsreq);
806e96a22b0SJames Smart 	if (!ret)
8074b056682SJames Smart 		atomic_inc(&lport->xmt_ls_abort);
80801649561SJames Smart }
80901649561SJames Smart 
81001649561SJames Smart /* Fix up the existing sgls for NVME IO. */
8115fd11085SJames Smart static inline void
81201649561SJames Smart lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
813c490850aSJames Smart 		       struct lpfc_io_buf *lpfc_ncmd,
81401649561SJames Smart 		       struct nvmefc_fcp_req *nCmd)
81501649561SJames Smart {
8164e565cf0SJames Smart 	struct lpfc_hba  *phba = vport->phba;
81701649561SJames Smart 	struct sli4_sge *sgl;
81801649561SJames Smart 	union lpfc_wqe128 *wqe;
81901649561SJames Smart 	uint32_t *wptr, *dptr;
82001649561SJames Smart 
82101649561SJames Smart 	/*
8224e565cf0SJames Smart 	 * Get a local pointer to the built-in wqe and correct
8234e565cf0SJames Smart 	 * the cmd size to match NVME's 96 bytes and fix
8244e565cf0SJames Smart 	 * the dma address.
8254e565cf0SJames Smart 	 */
8264e565cf0SJames Smart 
827205e8240SJames Smart 	wqe = &lpfc_ncmd->cur_iocbq.wqe;
8284e565cf0SJames Smart 
8294e565cf0SJames Smart 	/*
83001649561SJames Smart 	 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
83101649561SJames Smart 	 * match NVME.  NVME sends 96 bytes. Also, use the
83201649561SJames Smart 	 * nvme commands command and response dma addresses
83301649561SJames Smart 	 * rather than the virtual memory to ease the restore
83401649561SJames Smart 	 * operation.
83501649561SJames Smart 	 */
8360794d601SJames Smart 	sgl = lpfc_ncmd->dma_sgl;
83701649561SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
8384e565cf0SJames Smart 	if (phba->cfg_nvme_embed_cmd) {
8394e565cf0SJames Smart 		sgl->addr_hi = 0;
8404e565cf0SJames Smart 		sgl->addr_lo = 0;
8414e565cf0SJames Smart 
8424e565cf0SJames Smart 		/* Word 0-2 - NVME CMND IU (embedded payload) */
8434e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
8444e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeSize = 56;
8454e565cf0SJames Smart 		wqe->generic.bde.addrHigh = 0;
8464e565cf0SJames Smart 		wqe->generic.bde.addrLow =  64;  /* Word 16 */
8474e565cf0SJames Smart 
8485fd11085SJames Smart 		/* Word 10  - dbde is 0, wqes is 1 in template */
84901649561SJames Smart 
85001649561SJames Smart 		/*
85101649561SJames Smart 		 * Embed the payload in the last half of the WQE
85201649561SJames Smart 		 * WQE words 16-30 get the NVME CMD IU payload
85301649561SJames Smart 		 *
854b06a622fSJames Smart 		 * WQE words 16-19 get payload Words 1-4
85501649561SJames Smart 		 * WQE words 20-21 get payload Words 6-7
85601649561SJames Smart 		 * WQE words 22-29 get payload Words 16-23
85701649561SJames Smart 		 */
858b06a622fSJames Smart 		wptr = &wqe->words[16];  /* WQE ptr */
85901649561SJames Smart 		dptr = (uint32_t *)nCmd->cmdaddr;  /* payload ptr */
860b06a622fSJames Smart 		dptr++;			/* Skip Word 0 in payload */
86101649561SJames Smart 
862b06a622fSJames Smart 		*wptr++ = *dptr++;	/* Word 1 */
86301649561SJames Smart 		*wptr++ = *dptr++;	/* Word 2 */
86401649561SJames Smart 		*wptr++ = *dptr++;	/* Word 3 */
86501649561SJames Smart 		*wptr++ = *dptr++;	/* Word 4 */
86601649561SJames Smart 		dptr++;			/* Skip Word 5 in payload */
86701649561SJames Smart 		*wptr++ = *dptr++;	/* Word 6 */
86801649561SJames Smart 		*wptr++ = *dptr++;	/* Word 7 */
86901649561SJames Smart 		dptr += 8;		/* Skip Words 8-15 in payload */
87001649561SJames Smart 		*wptr++ = *dptr++;	/* Word 16 */
87101649561SJames Smart 		*wptr++ = *dptr++;	/* Word 17 */
87201649561SJames Smart 		*wptr++ = *dptr++;	/* Word 18 */
87301649561SJames Smart 		*wptr++ = *dptr++;	/* Word 19 */
87401649561SJames Smart 		*wptr++ = *dptr++;	/* Word 20 */
87501649561SJames Smart 		*wptr++ = *dptr++;	/* Word 21 */
87601649561SJames Smart 		*wptr++ = *dptr++;	/* Word 22 */
87701649561SJames Smart 		*wptr   = *dptr;	/* Word 23 */
8785fd11085SJames Smart 	} else {
8795fd11085SJames Smart 		sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->cmddma));
8805fd11085SJames Smart 		sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->cmddma));
8815fd11085SJames Smart 
8825fd11085SJames Smart 		/* Word 0-2 - NVME CMND IU Inline BDE */
8835fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
8845fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeSize = nCmd->cmdlen;
8855fd11085SJames Smart 		wqe->generic.bde.addrHigh = sgl->addr_hi;
8865fd11085SJames Smart 		wqe->generic.bde.addrLow =  sgl->addr_lo;
8875fd11085SJames Smart 
8885fd11085SJames Smart 		/* Word 10 */
8895fd11085SJames Smart 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
8905fd11085SJames Smart 		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
8915fd11085SJames Smart 	}
8925fd11085SJames Smart 
8935fd11085SJames Smart 	sgl++;
8945fd11085SJames Smart 
8955fd11085SJames Smart 	/* Setup the physical region for the FCP RSP */
8965fd11085SJames Smart 	sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
8975fd11085SJames Smart 	sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
8985fd11085SJames Smart 	sgl->word2 = le32_to_cpu(sgl->word2);
8995fd11085SJames Smart 	if (nCmd->sg_cnt)
9005fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
9015fd11085SJames Smart 	else
9025fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 1);
9035fd11085SJames Smart 	sgl->word2 = cpu_to_le32(sgl->word2);
9045fd11085SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->rsplen);
90501649561SJames Smart }
90601649561SJames Smart 
907bd2cdd5eSJames Smart 
90809d99705SLee Jones /*
90901649561SJames Smart  * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
91001649561SJames Smart  *
91101649561SJames Smart  * Driver registers this routine as it io request handler.  This
91201649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
91301649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
91401649561SJames Smart  *
91501649561SJames Smart  * Return value :
91601649561SJames Smart  *   0 - Success
91701649561SJames Smart  *   TODO: What are the failure codes.
91801649561SJames Smart  **/
91901649561SJames Smart static void
92001649561SJames Smart lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
92101649561SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
92201649561SJames Smart {
923c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd =
924c490850aSJames Smart 		(struct lpfc_io_buf *)pwqeIn->context1;
92501649561SJames Smart 	struct lpfc_vport *vport = pwqeIn->vport;
92601649561SJames Smart 	struct nvmefc_fcp_req *nCmd;
92701649561SJames Smart 	struct nvme_fc_ersp_iu *ep;
92801649561SJames Smart 	struct nvme_fc_cmd_iu *cp;
92901649561SJames Smart 	struct lpfc_nodelist *ndlp;
930bbe3012bSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
9314b056682SJames Smart 	struct lpfc_nvme_lport *lport;
932352b205aSArnd Bergmann 	uint32_t code, status, idx;
93301649561SJames Smart 	uint16_t cid, sqhd, data;
93401649561SJames Smart 	uint32_t *ptr;
93502243836SJames Smart 	uint32_t lat;
93602243836SJames Smart 	bool call_done = false;
937840eda96SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
938840eda96SJames Smart 	int cpu;
939840eda96SJames Smart #endif
94001649561SJames Smart 
94101649561SJames Smart 	/* Sanity check on return of outstanding command */
9424d5e789aSJames Smart 	if (!lpfc_ncmd) {
943372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9444d5e789aSJames Smart 				 "6071 Null lpfc_ncmd pointer. No "
9454d5e789aSJames Smart 				 "release, skip completion\n");
9464d5e789aSJames Smart 		return;
9474d5e789aSJames Smart 	}
9484d5e789aSJames Smart 
949c2017260SJames Smart 	/* Guard against abort handler being called at same time */
950c2017260SJames Smart 	spin_lock(&lpfc_ncmd->buf_lock);
951c2017260SJames Smart 
952c2017260SJames Smart 	if (!lpfc_ncmd->nvmeCmd) {
953c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
954372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
95532350664SJames Smart 				 "6066 Missing cmpl ptrs: lpfc_ncmd x%px, "
95632350664SJames Smart 				 "nvmeCmd x%px\n",
9570b05e9feSJames Smart 				 lpfc_ncmd, lpfc_ncmd->nvmeCmd);
9584d5e789aSJames Smart 
9594d5e789aSJames Smart 		/* Release the lpfc_ncmd regardless of the missing elements. */
9604d5e789aSJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
96101649561SJames Smart 		return;
96201649561SJames Smart 	}
96301649561SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
9644b056682SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
96566a210ffSJames Smart 
9664c47efc1SJames Smart 	idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
9674c47efc1SJames Smart 	phba->sli4_hba.hdwq[idx].nvme_cstat.io_cmpls++;
9684c47efc1SJames Smart 
9691df09449SJames Smart 	if (unlikely(status && vport->localport)) {
9704b056682SJames Smart 		lport = (struct lpfc_nvme_lport *)vport->localport->private;
9711df09449SJames Smart 		if (lport) {
9724b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
9734b056682SJames Smart 				atomic_inc(&lport->cmpl_fcp_xb);
9744b056682SJames Smart 			atomic_inc(&lport->cmpl_fcp_err);
9754b056682SJames Smart 		}
97666a210ffSJames Smart 	}
97701649561SJames Smart 
978bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
979bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
9804b056682SJames Smart 			 status, wcqe->parameter);
98101649561SJames Smart 	/*
98201649561SJames Smart 	 * Catch race where our node has transitioned, but the
98301649561SJames Smart 	 * transport is still transitioning.
98401649561SJames Smart 	 */
9850b05e9feSJames Smart 	ndlp = lpfc_ncmd->ndlp;
986307e3380SJames Smart 	if (!ndlp) {
987372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
98801649561SJames Smart 				 "6062 Ignoring NVME cmpl.  No ndlp\n");
98901649561SJames Smart 		goto out_err;
99001649561SJames Smart 	}
99101649561SJames Smart 
99201649561SJames Smart 	code = bf_get(lpfc_wcqe_c_code, wcqe);
99301649561SJames Smart 	if (code == CQE_CODE_NVME_ERSP) {
99401649561SJames Smart 		/* For this type of CQE, we need to rebuild the rsp */
99501649561SJames Smart 		ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
99601649561SJames Smart 
99701649561SJames Smart 		/*
99801649561SJames Smart 		 * Get Command Id from cmd to plug into response. This
99901649561SJames Smart 		 * code is not needed in the next NVME Transport drop.
100001649561SJames Smart 		 */
100101649561SJames Smart 		cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
100201649561SJames Smart 		cid = cp->sqe.common.command_id;
100301649561SJames Smart 
100401649561SJames Smart 		/*
100501649561SJames Smart 		 * RSN is in CQE word 2
100601649561SJames Smart 		 * SQHD is in CQE Word 3 bits 15:0
100701649561SJames Smart 		 * Cmd Specific info is in CQE Word 1
100801649561SJames Smart 		 * and in CQE Word 0 bits 15:0
100901649561SJames Smart 		 */
101001649561SJames Smart 		sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
101101649561SJames Smart 
101201649561SJames Smart 		/* Now lets build the NVME ERSP IU */
101301649561SJames Smart 		ep->iu_len = cpu_to_be16(8);
101401649561SJames Smart 		ep->rsn = wcqe->parameter;
101501649561SJames Smart 		ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
101601649561SJames Smart 		ep->rsvd12 = 0;
101701649561SJames Smart 		ptr = (uint32_t *)&ep->cqe.result.u64;
101801649561SJames Smart 		*ptr++ = wcqe->total_data_placed;
101901649561SJames Smart 		data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
102001649561SJames Smart 		*ptr = (uint32_t)data;
102101649561SJames Smart 		ep->cqe.sq_head = sqhd;
102201649561SJames Smart 		ep->cqe.sq_id =  nCmd->sqid;
102301649561SJames Smart 		ep->cqe.command_id = cid;
102401649561SJames Smart 		ep->cqe.status = 0;
102501649561SJames Smart 
102601649561SJames Smart 		lpfc_ncmd->status = IOSTAT_SUCCESS;
102701649561SJames Smart 		lpfc_ncmd->result = 0;
102801649561SJames Smart 		nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
102901649561SJames Smart 		nCmd->transferred_length = nCmd->payload_length;
103001649561SJames Smart 	} else {
10314b056682SJames Smart 		lpfc_ncmd->status = (status & LPFC_IOCB_STATUS_MASK);
1032952c303bSDick Kennedy 		lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
103301649561SJames Smart 
103401649561SJames Smart 		/* For NVME, the only failure path that results in an
103501649561SJames Smart 		 * IO error is when the adapter rejects it.  All other
103601649561SJames Smart 		 * conditions are a success case and resolved by the
103701649561SJames Smart 		 * transport.
103801649561SJames Smart 		 * IOSTAT_FCP_RSP_ERROR means:
103901649561SJames Smart 		 * 1. Length of data received doesn't match total
104001649561SJames Smart 		 *    transfer length in WQE
104101649561SJames Smart 		 * 2. If the RSP payload does NOT match these cases:
104201649561SJames Smart 		 *    a. RSP length 12/24 bytes and all zeros
104301649561SJames Smart 		 *    b. NVME ERSP
104401649561SJames Smart 		 */
104501649561SJames Smart 		switch (lpfc_ncmd->status) {
104601649561SJames Smart 		case IOSTAT_SUCCESS:
104701649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
104801649561SJames Smart 			nCmd->rcv_rsplen = 0;
104901649561SJames Smart 			nCmd->status = 0;
105001649561SJames Smart 			break;
105101649561SJames Smart 		case IOSTAT_FCP_RSP_ERROR:
105201649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
105301649561SJames Smart 			nCmd->rcv_rsplen = wcqe->parameter;
105401649561SJames Smart 			nCmd->status = 0;
1055fa21189dSJames Smart 
1056fa21189dSJames Smart 			/* Check if this is really an ERSP */
1057fa21189dSJames Smart 			if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN) {
1058fa21189dSJames Smart 				lpfc_ncmd->status = IOSTAT_SUCCESS;
1059fa21189dSJames Smart 				lpfc_ncmd->result = 0;
1060fa21189dSJames Smart 
1061fa21189dSJames Smart 				lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
1062fa21189dSJames Smart 					 "6084 NVME Completion ERSP: "
1063fa21189dSJames Smart 					 "xri %x placed x%x\n",
1064fa21189dSJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
1065fa21189dSJames Smart 					 wcqe->total_data_placed);
106601649561SJames Smart 				break;
1067fa21189dSJames Smart 			}
1068372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
106901649561SJames Smart 					 "6081 NVME Completion Protocol Error: "
107086c67379SJames Smart 					 "xri %x status x%x result x%x "
107186c67379SJames Smart 					 "placed x%x\n",
107286c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
107301649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
107401649561SJames Smart 					 wcqe->total_data_placed);
107501649561SJames Smart 			break;
1076952c303bSDick Kennedy 		case IOSTAT_LOCAL_REJECT:
1077952c303bSDick Kennedy 			/* Let fall through to set command final state. */
1078952c303bSDick Kennedy 			if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
1079952c303bSDick Kennedy 				lpfc_printf_vlog(vport, KERN_INFO,
1080952c303bSDick Kennedy 					 LOG_NVME_IOERR,
108132350664SJames Smart 					 "6032 Delay Aborted cmd x%px "
108232350664SJames Smart 					 "nvme cmd x%px, xri x%x, "
1083952c303bSDick Kennedy 					 "xb %d\n",
1084952c303bSDick Kennedy 					 lpfc_ncmd, nCmd,
1085952c303bSDick Kennedy 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
1086952c303bSDick Kennedy 					 bf_get(lpfc_wcqe_c_xb, wcqe));
1087df561f66SGustavo A. R. Silva 			fallthrough;
108801649561SJames Smart 		default:
108901649561SJames Smart out_err:
1090e3246a12SJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
109186c67379SJames Smart 					 "6072 NVME Completion Error: xri %x "
10922c013a3aSJames Smart 					 "status x%x result x%x [x%x] "
10932c013a3aSJames Smart 					 "placed x%x\n",
109486c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
109501649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
10962c013a3aSJames Smart 					 wcqe->parameter,
109701649561SJames Smart 					 wcqe->total_data_placed);
109801649561SJames Smart 			nCmd->transferred_length = 0;
109901649561SJames Smart 			nCmd->rcv_rsplen = 0;
11008e009ce8SJames Smart 			nCmd->status = NVME_SC_INTERNAL;
110101649561SJames Smart 		}
110201649561SJames Smart 	}
110301649561SJames Smart 
110401649561SJames Smart 	/* pick up SLI4 exhange busy condition */
110501649561SJames Smart 	if (bf_get(lpfc_wcqe_c_xb, wcqe))
110601649561SJames Smart 		lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
110701649561SJames Smart 	else
110801649561SJames Smart 		lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
110901649561SJames Smart 
111001649561SJames Smart 	/* Update stats and complete the IO.  There is
111101649561SJames Smart 	 * no need for dma unprep because the nvme_transport
111201649561SJames Smart 	 * owns the dma address.
111301649561SJames Smart 	 */
1114bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1115c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start) {
1116bd2cdd5eSJames Smart 		lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
11172fcbc569SJames Smart 		lpfc_ncmd->ts_data_io = ktime_get_ns();
11182fcbc569SJames Smart 		phba->ktime_last_cmd = lpfc_ncmd->ts_data_io;
11192fcbc569SJames Smart 		lpfc_io_ktime(phba, lpfc_ncmd);
1120bd2cdd5eSJames Smart 	}
1121840eda96SJames Smart 	if (unlikely(phba->hdwqstat_on & LPFC_CHECK_NVME_IO)) {
1122d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
1123840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
112463df6d63SJames Smart 		if (lpfc_ncmd->cpu != cpu)
112563df6d63SJames Smart 			lpfc_printf_vlog(vport,
112663df6d63SJames Smart 					 KERN_INFO, LOG_NVME_IOERR,
1127bd2cdd5eSJames Smart 					 "6701 CPU Check cmpl: "
1128bd2cdd5eSJames Smart 					 "cpu %d expect %d\n",
112963df6d63SJames Smart 					 cpu, lpfc_ncmd->cpu);
1130bd2cdd5eSJames Smart 	}
1131bd2cdd5eSJames Smart #endif
1132952c303bSDick Kennedy 
1133952c303bSDick Kennedy 	/* NVME targets need completion held off until the abort exchange
1134add9d6beSJames Smart 	 * completes unless the NVME Rport is getting unregistered.
1135952c303bSDick Kennedy 	 */
1136add9d6beSJames Smart 
11373fd78355SJames Smart 	if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
113891455b85SJames Smart 		freqpriv = nCmd->private;
113991455b85SJames Smart 		freqpriv->nvme_buf = NULL;
11403fd78355SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
114102243836SJames Smart 		call_done = true;
114202243836SJames Smart 	}
1143c2017260SJames Smart 	spin_unlock(&lpfc_ncmd->buf_lock);
114402243836SJames Smart 
114502243836SJames Smart 	/* Check if IO qualified for CMF */
114602243836SJames Smart 	if (phba->cmf_active_mode != LPFC_CFG_OFF &&
114702243836SJames Smart 	    nCmd->io_dir == NVMEFC_FCP_READ &&
114802243836SJames Smart 	    nCmd->payload_length) {
114902243836SJames Smart 		/* Used when calculating average latency */
115002243836SJames Smart 		lat = ktime_get_ns() - lpfc_ncmd->rx_cmd_start;
115102243836SJames Smart 		lpfc_update_cmf_cmpl(phba, lat, nCmd->payload_length, NULL);
115202243836SJames Smart 	}
115302243836SJames Smart 
115402243836SJames Smart 	if (call_done)
1155c2017260SJames Smart 		nCmd->done(nCmd);
115601649561SJames Smart 
1157952c303bSDick Kennedy 	/* Call release with XB=1 to queue the IO into the abort list. */
115801649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
115901649561SJames Smart }
116001649561SJames Smart 
116101649561SJames Smart 
116201649561SJames Smart /**
116301649561SJames Smart  * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
116409d99705SLee Jones  * @vport: pointer to a host virtual N_Port data structure
11653e749170SLee Jones  * @lpfc_ncmd: Pointer to lpfc scsi command
116609d99705SLee Jones  * @pnode: pointer to a node-list data structure
116709d99705SLee Jones  * @cstat: pointer to the control status structure
116801649561SJames Smart  *
116901649561SJames Smart  * Driver registers this routine as it io request handler.  This
117001649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
117101649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
117201649561SJames Smart  *
117301649561SJames Smart  * Return value :
117401649561SJames Smart  *   0 - Success
117501649561SJames Smart  *   TODO: What are the failure codes.
117601649561SJames Smart  **/
117701649561SJames Smart static int
117801649561SJames Smart lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
1179c490850aSJames Smart 		      struct lpfc_io_buf *lpfc_ncmd,
118066a210ffSJames Smart 		      struct lpfc_nodelist *pnode,
11814c47efc1SJames Smart 		      struct lpfc_fc4_ctrl_stat *cstat)
118201649561SJames Smart {
118301649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
118401649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
118501649561SJames Smart 	struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
1186205e8240SJames Smart 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
118701649561SJames Smart 	uint32_t req_len;
118801649561SJames Smart 
118901649561SJames Smart 	/*
119001649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
119101649561SJames Smart 	 * the single mapping, or neither.
119201649561SJames Smart 	 */
119301649561SJames Smart 	if (nCmd->sg_cnt) {
119401649561SJames Smart 		if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
11955fd11085SJames Smart 			/* From the iwrite template, initialize words 7 - 11 */
11965fd11085SJames Smart 			memcpy(&wqe->words[7],
11975fd11085SJames Smart 			       &lpfc_iwrite_cmd_template.words[7],
11985fd11085SJames Smart 			       sizeof(uint32_t) * 5);
11995fd11085SJames Smart 
12005fd11085SJames Smart 			/* Word 4 */
12015fd11085SJames Smart 			wqe->fcp_iwrite.total_xfer_len = nCmd->payload_length;
12025fd11085SJames Smart 
120301649561SJames Smart 			/* Word 5 */
120401649561SJames Smart 			if ((phba->cfg_nvme_enable_fb) &&
120501649561SJames Smart 			    (pnode->nlp_flag & NLP_FIRSTBURST)) {
120601649561SJames Smart 				req_len = lpfc_ncmd->nvmeCmd->payload_length;
120701649561SJames Smart 				if (req_len < pnode->nvme_fb_size)
120801649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
120901649561SJames Smart 						req_len;
121001649561SJames Smart 				else
121101649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
121201649561SJames Smart 						pnode->nvme_fb_size;
12135fd11085SJames Smart 			} else {
12145fd11085SJames Smart 				wqe->fcp_iwrite.initial_xfer_len = 0;
121501649561SJames Smart 			}
12164c47efc1SJames Smart 			cstat->output_requests++;
121701649561SJames Smart 		} else {
12185fd11085SJames Smart 			/* From the iread template, initialize words 7 - 11 */
12195fd11085SJames Smart 			memcpy(&wqe->words[7],
12205fd11085SJames Smart 			       &lpfc_iread_cmd_template.words[7],
12215fd11085SJames Smart 			       sizeof(uint32_t) * 5);
122201649561SJames Smart 
12235fd11085SJames Smart 			/* Word 4 */
12245fd11085SJames Smart 			wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
122501649561SJames Smart 
12265fd11085SJames Smart 			/* Word 5 */
12275fd11085SJames Smart 			wqe->fcp_iread.rsrvd5 = 0;
122801649561SJames Smart 
122902243836SJames Smart 			/* For a CMF Managed port, iod must be zero'ed */
123002243836SJames Smart 			if (phba->cmf_active_mode == LPFC_CFG_MANAGED)
123102243836SJames Smart 				bf_set(wqe_iod, &wqe->fcp_iread.wqe_com,
123202243836SJames Smart 				       LPFC_WQE_IOD_NONE);
12334c47efc1SJames Smart 			cstat->input_requests++;
123401649561SJames Smart 		}
123501649561SJames Smart 	} else {
12365fd11085SJames Smart 		/* From the icmnd template, initialize words 4 - 11 */
12375fd11085SJames Smart 		memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
12385fd11085SJames Smart 		       sizeof(uint32_t) * 8);
12394c47efc1SJames Smart 		cstat->control_requests++;
124001649561SJames Smart 	}
12410d8af096SJames Smart 
12420d8af096SJames Smart 	if (pnode->nlp_nvme_info & NLP_NVME_NSLER)
12430d8af096SJames Smart 		bf_set(wqe_erp, &wqe->generic.wqe_com, 1);
124401649561SJames Smart 	/*
124501649561SJames Smart 	 * Finish initializing those WQE fields that are independent
124601649561SJames Smart 	 * of the nvme_cmnd request_buffer
124701649561SJames Smart 	 */
124801649561SJames Smart 
12495fd11085SJames Smart 	/* Word 3 */
12505fd11085SJames Smart 	bf_set(payload_offset_len, &wqe->fcp_icmd,
12515fd11085SJames Smart 	       (nCmd->rsplen + nCmd->cmdlen));
12525fd11085SJames Smart 
125301649561SJames Smart 	/* Word 6 */
125401649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
125501649561SJames Smart 	       phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
125601649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
125701649561SJames Smart 
125801649561SJames Smart 	/* Word 8 */
125901649561SJames Smart 	wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
126001649561SJames Smart 
126101649561SJames Smart 	/* Word 9 */
126201649561SJames Smart 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
126301649561SJames Smart 
1264840a4701SJames Smart 	/* Word 10 */
1265840a4701SJames Smart 	bf_set(wqe_xchg, &wqe->fcp_iwrite.wqe_com, LPFC_NVME_XCHG);
1266840a4701SJames Smart 
1267414abe0aSJames Smart 	/* Words 13 14 15 are for PBDE support */
1268414abe0aSJames Smart 
126901649561SJames Smart 	pwqeq->vport = vport;
127001649561SJames Smart 	return 0;
127101649561SJames Smart }
127201649561SJames Smart 
127301649561SJames Smart 
127401649561SJames Smart /**
127501649561SJames Smart  * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
127609d99705SLee Jones  * @vport: pointer to a host virtual N_Port data structure
12773e749170SLee Jones  * @lpfc_ncmd: Pointer to lpfc scsi command
127801649561SJames Smart  *
127901649561SJames Smart  * Driver registers this routine as it io request handler.  This
128001649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
128101649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
128201649561SJames Smart  *
128301649561SJames Smart  * Return value :
128401649561SJames Smart  *   0 - Success
128501649561SJames Smart  *   TODO: What are the failure codes.
128601649561SJames Smart  **/
128701649561SJames Smart static int
128801649561SJames Smart lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
1289c490850aSJames Smart 		      struct lpfc_io_buf *lpfc_ncmd)
129001649561SJames Smart {
129101649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
129201649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1293205e8240SJames Smart 	union lpfc_wqe128 *wqe = &lpfc_ncmd->cur_iocbq.wqe;
12940794d601SJames Smart 	struct sli4_sge *sgl = lpfc_ncmd->dma_sgl;
1295d79c9e9dSJames Smart 	struct sli4_hybrid_sgl *sgl_xtra = NULL;
129601649561SJames Smart 	struct scatterlist *data_sg;
129701649561SJames Smart 	struct sli4_sge *first_data_sgl;
12980bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
1299d79c9e9dSJames Smart 	dma_addr_t physaddr = 0;
130001649561SJames Smart 	uint32_t num_bde = 0;
1301d79c9e9dSJames Smart 	uint32_t dma_len = 0;
130201649561SJames Smart 	uint32_t dma_offset = 0;
1303d79c9e9dSJames Smart 	int nseg, i, j;
1304d79c9e9dSJames Smart 	bool lsp_just_set = false;
130501649561SJames Smart 
130601649561SJames Smart 	/* Fix up the command and response DMA stuff. */
130701649561SJames Smart 	lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
130801649561SJames Smart 
130901649561SJames Smart 	/*
131001649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
131101649561SJames Smart 	 * the single mapping, or neither.
131201649561SJames Smart 	 */
131301649561SJames Smart 	if (nCmd->sg_cnt) {
131401649561SJames Smart 		/*
131501649561SJames Smart 		 * Jump over the cmd and rsp SGEs.  The fix routine
131601649561SJames Smart 		 * has already adjusted for this.
131701649561SJames Smart 		 */
131801649561SJames Smart 		sgl += 2;
131901649561SJames Smart 
132001649561SJames Smart 		first_data_sgl = sgl;
132101649561SJames Smart 		lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
132281e6a637SJames Smart 		if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
1323372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
132401649561SJames Smart 					"6058 Too many sg segments from "
132501649561SJames Smart 					"NVME Transport.  Max %d, "
132601649561SJames Smart 					"nvmeIO sg_cnt %d\n",
132729bfd55aSJames Smart 					phba->cfg_nvme_seg_cnt + 1,
132801649561SJames Smart 					lpfc_ncmd->seg_cnt);
132901649561SJames Smart 			lpfc_ncmd->seg_cnt = 0;
133001649561SJames Smart 			return 1;
133101649561SJames Smart 		}
133201649561SJames Smart 
133301649561SJames Smart 		/*
133401649561SJames Smart 		 * The driver established a maximum scatter-gather segment count
133501649561SJames Smart 		 * during probe that limits the number of sg elements in any
133601649561SJames Smart 		 * single nvme command.  Just run through the seg_cnt and format
133701649561SJames Smart 		 * the sge's.
133801649561SJames Smart 		 */
133901649561SJames Smart 		nseg = nCmd->sg_cnt;
134001649561SJames Smart 		data_sg = nCmd->first_sgl;
1341d79c9e9dSJames Smart 
1342d79c9e9dSJames Smart 		/* for tracking the segment boundaries */
1343d79c9e9dSJames Smart 		j = 2;
134401649561SJames Smart 		for (i = 0; i < nseg; i++) {
134501649561SJames Smart 			if (data_sg == NULL) {
1346372c187bSDick Kennedy 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
134701649561SJames Smart 						"6059 dptr err %d, nseg %d\n",
134801649561SJames Smart 						i, nseg);
134901649561SJames Smart 				lpfc_ncmd->seg_cnt = 0;
135001649561SJames Smart 				return 1;
135101649561SJames Smart 			}
1352d79c9e9dSJames Smart 
1353d79c9e9dSJames Smart 			sgl->word2 = 0;
1354d79c9e9dSJames Smart 			if ((num_bde + 1) == nseg) {
1355d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 1);
1356d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_type, sgl,
1357d79c9e9dSJames Smart 				       LPFC_SGE_TYPE_DATA);
1358d79c9e9dSJames Smart 			} else {
1359d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 0);
1360d79c9e9dSJames Smart 
1361d79c9e9dSJames Smart 				/* expand the segment */
1362d79c9e9dSJames Smart 				if (!lsp_just_set &&
1363d79c9e9dSJames Smart 				    !((j + 1) % phba->border_sge_num) &&
1364d79c9e9dSJames Smart 				    ((nseg - 1) != i)) {
1365d79c9e9dSJames Smart 					/* set LSP type */
1366d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_type, sgl,
1367d79c9e9dSJames Smart 					       LPFC_SGE_TYPE_LSP);
1368d79c9e9dSJames Smart 
1369d79c9e9dSJames Smart 					sgl_xtra = lpfc_get_sgl_per_hdwq(
1370d79c9e9dSJames Smart 							phba, lpfc_ncmd);
1371d79c9e9dSJames Smart 
1372d79c9e9dSJames Smart 					if (unlikely(!sgl_xtra)) {
1373d79c9e9dSJames Smart 						lpfc_ncmd->seg_cnt = 0;
1374d79c9e9dSJames Smart 						return 1;
1375d79c9e9dSJames Smart 					}
1376d79c9e9dSJames Smart 					sgl->addr_lo = cpu_to_le32(putPaddrLow(
1377d79c9e9dSJames Smart 						       sgl_xtra->dma_phys_sgl));
1378d79c9e9dSJames Smart 					sgl->addr_hi = cpu_to_le32(putPaddrHigh(
1379d79c9e9dSJames Smart 						       sgl_xtra->dma_phys_sgl));
1380d79c9e9dSJames Smart 
1381d79c9e9dSJames Smart 				} else {
1382d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_type, sgl,
1383d79c9e9dSJames Smart 					       LPFC_SGE_TYPE_DATA);
1384d79c9e9dSJames Smart 				}
1385d79c9e9dSJames Smart 			}
1386d79c9e9dSJames Smart 
1387d79c9e9dSJames Smart 			if (!(bf_get(lpfc_sli4_sge_type, sgl) &
1388d79c9e9dSJames Smart 				     LPFC_SGE_TYPE_LSP)) {
1389d79c9e9dSJames Smart 				if ((nseg - 1) == i)
1390d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_last, sgl, 1);
1391d79c9e9dSJames Smart 
139201649561SJames Smart 				physaddr = data_sg->dma_address;
139301649561SJames Smart 				dma_len = data_sg->length;
1394d79c9e9dSJames Smart 				sgl->addr_lo = cpu_to_le32(
1395d79c9e9dSJames Smart 							 putPaddrLow(physaddr));
1396d79c9e9dSJames Smart 				sgl->addr_hi = cpu_to_le32(
1397d79c9e9dSJames Smart 							putPaddrHigh(physaddr));
1398d79c9e9dSJames Smart 
139901649561SJames Smart 				bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
140001649561SJames Smart 				sgl->word2 = cpu_to_le32(sgl->word2);
140101649561SJames Smart 				sgl->sge_len = cpu_to_le32(dma_len);
140201649561SJames Smart 
140301649561SJames Smart 				dma_offset += dma_len;
140401649561SJames Smart 				data_sg = sg_next(data_sg);
1405d79c9e9dSJames Smart 
140601649561SJames Smart 				sgl++;
1407d79c9e9dSJames Smart 
1408d79c9e9dSJames Smart 				lsp_just_set = false;
1409d79c9e9dSJames Smart 			} else {
1410d79c9e9dSJames Smart 				sgl->word2 = cpu_to_le32(sgl->word2);
1411d79c9e9dSJames Smart 
1412d79c9e9dSJames Smart 				sgl->sge_len = cpu_to_le32(
1413d79c9e9dSJames Smart 						     phba->cfg_sg_dma_buf_size);
1414d79c9e9dSJames Smart 
1415d79c9e9dSJames Smart 				sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
1416d79c9e9dSJames Smart 				i = i - 1;
1417d79c9e9dSJames Smart 
1418d79c9e9dSJames Smart 				lsp_just_set = true;
1419d79c9e9dSJames Smart 			}
1420d79c9e9dSJames Smart 
1421d79c9e9dSJames Smart 			j++;
142201649561SJames Smart 		}
1423414abe0aSJames Smart 		if (phba->cfg_enable_pbde) {
14240bc2b7c5SJames Smart 			/* Use PBDE support for first SGL only, offset == 0 */
14250bc2b7c5SJames Smart 			/* Words 13-15 */
14260bc2b7c5SJames Smart 			bde = (struct ulp_bde64 *)
14270bc2b7c5SJames Smart 				&wqe->words[13];
14280bc2b7c5SJames Smart 			bde->addrLow = first_data_sgl->addr_lo;
14290bc2b7c5SJames Smart 			bde->addrHigh = first_data_sgl->addr_hi;
14300bc2b7c5SJames Smart 			bde->tus.f.bdeSize =
14310bc2b7c5SJames Smart 				le32_to_cpu(first_data_sgl->sge_len);
14320bc2b7c5SJames Smart 			bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
14330bc2b7c5SJames Smart 			bde->tus.w = cpu_to_le32(bde->tus.w);
1434b101eb27SJames Smart 
1435b101eb27SJames Smart 			/* Word 11 */
1436b101eb27SJames Smart 			bf_set(wqe_pbde, &wqe->generic.wqe_com, 1);
14375fd11085SJames Smart 		} else {
14385fd11085SJames Smart 			memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
14390bc2b7c5SJames Smart 			bf_set(wqe_pbde, &wqe->generic.wqe_com, 0);
14405fd11085SJames Smart 		}
14410bc2b7c5SJames Smart 
1442414abe0aSJames Smart 	} else {
14430794d601SJames Smart 		lpfc_ncmd->seg_cnt = 0;
14440794d601SJames Smart 
144501649561SJames Smart 		/* For this clause to be valid, the payload_length
144601649561SJames Smart 		 * and sg_cnt must zero.
144701649561SJames Smart 		 */
144801649561SJames Smart 		if (nCmd->payload_length != 0) {
1449372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
145001649561SJames Smart 					"6063 NVME DMA Prep Err: sg_cnt %d "
145101649561SJames Smart 					"payload_length x%x\n",
145201649561SJames Smart 					nCmd->sg_cnt, nCmd->payload_length);
145301649561SJames Smart 			return 1;
145401649561SJames Smart 		}
145501649561SJames Smart 	}
145601649561SJames Smart 	return 0;
145701649561SJames Smart }
145801649561SJames Smart 
145901649561SJames Smart /**
146001649561SJames Smart  * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
14613e749170SLee Jones  * @pnvme_lport: Pointer to the driver's local port data
14623e749170SLee Jones  * @pnvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
146301649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
14643e749170SLee Jones  * @pnvme_fcreq: IO request from nvme fc to driver.
146501649561SJames Smart  *
146601649561SJames Smart  * Driver registers this routine as it io request handler.  This
146701649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
14683e749170SLee Jones  * data structure to the rport indicated in @lpfc_nvme_rport.
146901649561SJames Smart  *
147001649561SJames Smart  * Return value :
147101649561SJames Smart  *   0 - Success
147201649561SJames Smart  *   TODO: What are the failure codes.
147301649561SJames Smart  **/
147401649561SJames Smart static int
147501649561SJames Smart lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
147601649561SJames Smart 			struct nvme_fc_remote_port *pnvme_rport,
147701649561SJames Smart 			void *hw_queue_handle,
147801649561SJames Smart 			struct nvmefc_fcp_req *pnvme_fcreq)
147901649561SJames Smart {
148001649561SJames Smart 	int ret = 0;
1481cf1a1d3eSJames Smart 	int expedite = 0;
148263df6d63SJames Smart 	int idx, cpu;
148301649561SJames Smart 	struct lpfc_nvme_lport *lport;
14844c47efc1SJames Smart 	struct lpfc_fc4_ctrl_stat *cstat;
148501649561SJames Smart 	struct lpfc_vport *vport;
148601649561SJames Smart 	struct lpfc_hba *phba;
148701649561SJames Smart 	struct lpfc_nodelist *ndlp;
1488c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
148901649561SJames Smart 	struct lpfc_nvme_rport *rport;
149001649561SJames Smart 	struct lpfc_nvme_qhandle *lpfc_queue_info;
1491c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
1492cf1a1d3eSJames Smart 	struct nvme_common_command *sqe;
1493bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1494bd2cdd5eSJames Smart 	uint64_t start = 0;
1495bd2cdd5eSJames Smart #endif
149601649561SJames Smart 
1497c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1498c3725bdcSJames Smart 	 * have timing races.
1499c3725bdcSJames Smart 	 */
150001649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1501c3725bdcSJames Smart 	if (unlikely(!lport)) {
1502c3725bdcSJames Smart 		ret = -EINVAL;
1503c3725bdcSJames Smart 		goto out_fail;
1504c3725bdcSJames Smart 	}
1505c3725bdcSJames Smart 
150601649561SJames Smart 	vport = lport->vport;
1507c3725bdcSJames Smart 
1508c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
150944c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
151044c2757bSJames Smart 				 "6117 Fail IO, NULL hw_queue_handle\n");
151144c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1512cd240071SJames Smart 		ret = -EBUSY;
1513c3725bdcSJames Smart 		goto out_fail;
1514c3725bdcSJames Smart 	}
1515c3725bdcSJames Smart 
151601649561SJames Smart 	phba = vport->phba;
151701649561SJames Smart 
15181df09449SJames Smart 	if (unlikely(vport->load_flag & FC_UNLOADING)) {
151944c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
152044c2757bSJames Smart 				 "6124 Fail IO, Driver unload\n");
152144c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
15223386f4bdSJames Smart 		ret = -ENODEV;
15233386f4bdSJames Smart 		goto out_fail;
15243386f4bdSJames Smart 	}
15253386f4bdSJames Smart 
1526c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1527c3725bdcSJames Smart 	if (unlikely(!freqpriv)) {
152844c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
152944c2757bSJames Smart 				 "6158 Fail IO, NULL request data\n");
153044c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1531c3725bdcSJames Smart 		ret = -EINVAL;
1532b7672ae6SDick Kennedy 		goto out_fail;
1533b7672ae6SDick Kennedy 	}
1534b7672ae6SDick Kennedy 
1535bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1536bd2cdd5eSJames Smart 	if (phba->ktime_on)
1537bd2cdd5eSJames Smart 		start = ktime_get_ns();
1538bd2cdd5eSJames Smart #endif
153901649561SJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
154001649561SJames Smart 	lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
154101649561SJames Smart 
154201649561SJames Smart 	/*
154301649561SJames Smart 	 * Catch race where our node has transitioned, but the
154401649561SJames Smart 	 * transport is still transitioning.
154501649561SJames Smart 	 */
154601649561SJames Smart 	ndlp = rport->ndlp;
1547307e3380SJames Smart 	if (!ndlp) {
154844c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
154932350664SJames Smart 				 "6053 Busy IO, ndlp not ready: rport x%px "
155032350664SJames Smart 				  "ndlp x%px, DID x%06x\n",
155101649561SJames Smart 				 rport, ndlp, pnvme_rport->port_id);
155244c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1553cd240071SJames Smart 		ret = -EBUSY;
155401649561SJames Smart 		goto out_fail;
155501649561SJames Smart 	}
155601649561SJames Smart 
155701649561SJames Smart 	/* The remote node has to be a mapped target or it's an error. */
155801649561SJames Smart 	if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
155901649561SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
156044c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
156144c2757bSJames Smart 				 "6036 Fail IO, DID x%06x not ready for "
1562cd240071SJames Smart 				 "IO. State x%x, Type x%x Flg x%x\n",
156344c2757bSJames Smart 				 pnvme_rport->port_id,
1564cd240071SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type,
1565e9b11083SJames Smart 				 ndlp->fc4_xpt_flags);
15664b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_bad_ndlp);
1567cd240071SJames Smart 		ret = -EBUSY;
156801649561SJames Smart 		goto out_fail;
156901649561SJames Smart 
157001649561SJames Smart 	}
157101649561SJames Smart 
1572cf1a1d3eSJames Smart 	/* Currently only NVME Keep alive commands should be expedited
1573cf1a1d3eSJames Smart 	 * if the driver runs out of a resource. These should only be
1574cf1a1d3eSJames Smart 	 * issued on the admin queue, qidx 0
1575cf1a1d3eSJames Smart 	 */
1576cf1a1d3eSJames Smart 	if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
1577cf1a1d3eSJames Smart 		sqe = &((struct nvme_fc_cmd_iu *)
1578cf1a1d3eSJames Smart 			pnvme_fcreq->cmdaddr)->sqe.common;
1579cf1a1d3eSJames Smart 		if (sqe->opcode == nvme_admin_keep_alive)
1580cf1a1d3eSJames Smart 			expedite = 1;
1581cf1a1d3eSJames Smart 	}
1582cf1a1d3eSJames Smart 
158302243836SJames Smart 	/* Check if IO qualifies for CMF */
158402243836SJames Smart 	if (phba->cmf_active_mode != LPFC_CFG_OFF &&
158502243836SJames Smart 	    pnvme_fcreq->io_dir == NVMEFC_FCP_READ &&
158602243836SJames Smart 	    pnvme_fcreq->payload_length) {
158702243836SJames Smart 		ret = lpfc_update_cmf_cmd(phba, pnvme_fcreq->payload_length);
158802243836SJames Smart 		if (ret) {
158902243836SJames Smart 			ret = -EBUSY;
159002243836SJames Smart 			goto out_fail;
159102243836SJames Smart 		}
159202243836SJames Smart 		/* Get start time for IO latency */
159302243836SJames Smart 		start = ktime_get_ns();
159402243836SJames Smart 	}
159502243836SJames Smart 
159601649561SJames Smart 	/* The node is shared with FCP IO, make sure the IO pending count does
159701649561SJames Smart 	 * not exceed the programmed depth.
159801649561SJames Smart 	 */
15992a5b7d62SJames Smart 	if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1600cf1a1d3eSJames Smart 		if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
1601cf1a1d3eSJames Smart 		    !expedite) {
160244c2757bSJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
160344c2757bSJames Smart 					 "6174 Fail IO, ndlp qdepth exceeded: "
16044d5e789aSJames Smart 					 "idx %d DID %x pend %d qdepth %d\n",
16054d5e789aSJames Smart 					 lpfc_queue_info->index, ndlp->nlp_DID,
16064d5e789aSJames Smart 					 atomic_read(&ndlp->cmd_pending),
16074d5e789aSJames Smart 					 ndlp->cmd_qdepth);
16084b056682SJames Smart 			atomic_inc(&lport->xmt_fcp_qdepth);
1609cd22d605SDick Kennedy 			ret = -EBUSY;
161002243836SJames Smart 			goto out_fail1;
161101649561SJames Smart 		}
16122a5b7d62SJames Smart 	}
161301649561SJames Smart 
16146a828b0fSJames Smart 	/* Lookup Hardware Queue index based on fcp_io_sched module parameter */
161545aa312eSJames Smart 	if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
161645aa312eSJames Smart 		idx = lpfc_queue_info->index;
161745aa312eSJames Smart 	} else {
1618d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
16196a828b0fSJames Smart 		idx = phba->sli4_hba.cpu_map[cpu].hdwq;
162045aa312eSJames Smart 	}
162145aa312eSJames Smart 
162245aa312eSJames Smart 	lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, idx, expedite);
162301649561SJames Smart 	if (lpfc_ncmd == NULL) {
16244b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_noxri);
162501649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
162644c2757bSJames Smart 				 "6065 Fail IO, driver buffer pool is empty: "
162744c2757bSJames Smart 				 "idx %d DID %x\n",
162844c2757bSJames Smart 				 lpfc_queue_info->index, ndlp->nlp_DID);
1629cd22d605SDick Kennedy 		ret = -EBUSY;
163002243836SJames Smart 		goto out_fail1;
163101649561SJames Smart 	}
1632bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1633c8a4ce0bSDick Kennedy 	if (start) {
1634bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_start = start;
1635bd2cdd5eSJames Smart 		lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
1636c8a4ce0bSDick Kennedy 	} else {
1637c8a4ce0bSDick Kennedy 		lpfc_ncmd->ts_cmd_start = 0;
1638bd2cdd5eSJames Smart 	}
1639bd2cdd5eSJames Smart #endif
164002243836SJames Smart 	lpfc_ncmd->rx_cmd_start = start;
164101649561SJames Smart 
164201649561SJames Smart 	/*
164301649561SJames Smart 	 * Store the data needed by the driver to issue, abort, and complete
164401649561SJames Smart 	 * an IO.
164501649561SJames Smart 	 * Do not let the IO hang out forever.  There is no midlayer issuing
164601649561SJames Smart 	 * an abort so inform the FW of the maximum IO pending time.
164701649561SJames Smart 	 */
1648bbe3012bSJames Smart 	freqpriv->nvme_buf = lpfc_ncmd;
164901649561SJames Smart 	lpfc_ncmd->nvmeCmd = pnvme_fcreq;
1650318083adSJames Smart 	lpfc_ncmd->ndlp = ndlp;
16510794d601SJames Smart 	lpfc_ncmd->qidx = lpfc_queue_info->qidx;
165201649561SJames Smart 
165301649561SJames Smart 	/*
165401649561SJames Smart 	 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
165501649561SJames Smart 	 * This identfier was create in our hardware queue create callback
165601649561SJames Smart 	 * routine. The driver now is dependent on the IO queue steering from
165701649561SJames Smart 	 * the transport.  We are trusting the upper NVME layers know which
165801649561SJames Smart 	 * index to use and that they have affinitized a CPU to this hardware
165901649561SJames Smart 	 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
166001649561SJames Smart 	 */
166166a210ffSJames Smart 	lpfc_ncmd->cur_iocbq.hba_wqidx = idx;
16624c47efc1SJames Smart 	cstat = &phba->sli4_hba.hdwq[idx].nvme_cstat;
166366a210ffSJames Smart 
166466a210ffSJames Smart 	lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp, cstat);
166566a210ffSJames Smart 	ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
166666a210ffSJames Smart 	if (ret) {
166744c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
166844c2757bSJames Smart 				 "6175 Fail IO, Prep DMA: "
166944c2757bSJames Smart 				 "idx %d DID %x\n",
167044c2757bSJames Smart 				 lpfc_queue_info->index, ndlp->nlp_DID);
167144c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
167266a210ffSJames Smart 		ret = -ENOMEM;
167366a210ffSJames Smart 		goto out_free_nvme_buf;
167466a210ffSJames Smart 	}
167566a210ffSJames Smart 
1676bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1677bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
1678bd2cdd5eSJames Smart 			 lpfc_queue_info->index, ndlp->nlp_DID);
1679bd2cdd5eSJames Smart 
16801fbf9742SJames Smart 	ret = lpfc_sli4_issue_wqe(phba, lpfc_ncmd->hdwq, &lpfc_ncmd->cur_iocbq);
168101649561SJames Smart 	if (ret) {
16824b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_wqerr);
1683e3246a12SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
168444c2757bSJames Smart 				 "6113 Fail IO, Could not issue WQE err %x "
168501649561SJames Smart 				 "sid: x%x did: x%x oxid: x%x\n",
168601649561SJames Smart 				 ret, vport->fc_myDID, ndlp->nlp_DID,
168701649561SJames Smart 				 lpfc_ncmd->cur_iocbq.sli4_xritag);
168801649561SJames Smart 		goto out_free_nvme_buf;
168901649561SJames Smart 	}
169001649561SJames Smart 
1691c490850aSJames Smart 	if (phba->cfg_xri_rebalancing)
1692c490850aSJames Smart 		lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_ncmd->hdwq_no);
1693c490850aSJames Smart 
1694bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1695c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start)
1696bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1697bd2cdd5eSJames Smart 
1698840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVME_IO) {
1699d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
1700840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
170163df6d63SJames Smart 		lpfc_ncmd->cpu = cpu;
170263df6d63SJames Smart 		if (idx != cpu)
1703bd2cdd5eSJames Smart 			lpfc_printf_vlog(vport,
170463df6d63SJames Smart 					 KERN_INFO, LOG_NVME_IOERR,
1705bd2cdd5eSJames Smart 					"6702 CPU Check cmd: "
1706bd2cdd5eSJames Smart 					"cpu %d wq %d\n",
1707bd2cdd5eSJames Smart 					lpfc_ncmd->cpu,
1708bd2cdd5eSJames Smart 					lpfc_queue_info->index);
1709bd2cdd5eSJames Smart 	}
1710bd2cdd5eSJames Smart #endif
171101649561SJames Smart 	return 0;
171201649561SJames Smart 
171301649561SJames Smart  out_free_nvme_buf:
17142cee7808SJames Smart 	if (lpfc_ncmd->nvmeCmd->sg_cnt) {
17152cee7808SJames Smart 		if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
17164c47efc1SJames Smart 			cstat->output_requests--;
17172cee7808SJames Smart 		else
17184c47efc1SJames Smart 			cstat->input_requests--;
17192cee7808SJames Smart 	} else
17204c47efc1SJames Smart 		cstat->control_requests--;
172101649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
172202243836SJames Smart  out_fail1:
172302243836SJames Smart 	lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT,
172402243836SJames Smart 			     pnvme_fcreq->payload_length, NULL);
172501649561SJames Smart  out_fail:
172601649561SJames Smart 	return ret;
172701649561SJames Smart }
172801649561SJames Smart 
172901649561SJames Smart /**
173001649561SJames Smart  * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
173101649561SJames Smart  * @phba: Pointer to HBA context object
173201649561SJames Smart  * @cmdiocb: Pointer to command iocb object.
17333e749170SLee Jones  * @abts_cmpl: Pointer to wcqe complete object.
173401649561SJames Smart  *
173501649561SJames Smart  * This is the callback function for any NVME FCP IO that was aborted.
173601649561SJames Smart  *
173701649561SJames Smart  * Return value:
173801649561SJames Smart  *   None
173901649561SJames Smart  **/
174001649561SJames Smart void
174101649561SJames Smart lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
174201649561SJames Smart 			   struct lpfc_wcqe_complete *abts_cmpl)
174301649561SJames Smart {
1744e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
174501649561SJames Smart 			"6145 ABORT_XRI_CN completing on rpi x%x "
174601649561SJames Smart 			"original iotag x%x, abort cmd iotag x%x "
174701649561SJames Smart 			"req_tag x%x, status x%x, hwstatus x%x\n",
174801649561SJames Smart 			cmdiocb->iocb.un.acxri.abortContextTag,
174901649561SJames Smart 			cmdiocb->iocb.un.acxri.abortIoTag,
175001649561SJames Smart 			cmdiocb->iotag,
175101649561SJames Smart 			bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
175201649561SJames Smart 			bf_get(lpfc_wcqe_c_status, abts_cmpl),
175301649561SJames Smart 			bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
175401649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocb);
175501649561SJames Smart }
175601649561SJames Smart 
175701649561SJames Smart /**
175801649561SJames Smart  * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
17593e749170SLee Jones  * @pnvme_lport: Pointer to the driver's local port data
17603e749170SLee Jones  * @pnvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
176101649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
17623e749170SLee Jones  * @pnvme_fcreq: IO request from nvme fc to driver.
176301649561SJames Smart  *
176401649561SJames Smart  * Driver registers this routine as its nvme request io abort handler.  This
176501649561SJames Smart  * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
176601649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.  This routine
176701649561SJames Smart  * is executed asynchronously - one the target is validated as "MAPPED" and
176801649561SJames Smart  * ready for IO, the driver issues the abort request and returns.
176901649561SJames Smart  *
177001649561SJames Smart  * Return value:
177101649561SJames Smart  *   None
177201649561SJames Smart  **/
177301649561SJames Smart static void
177401649561SJames Smart lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
177501649561SJames Smart 		    struct nvme_fc_remote_port *pnvme_rport,
177601649561SJames Smart 		    void *hw_queue_handle,
177701649561SJames Smart 		    struct nvmefc_fcp_req *pnvme_fcreq)
177801649561SJames Smart {
177901649561SJames Smart 	struct lpfc_nvme_lport *lport;
178001649561SJames Smart 	struct lpfc_vport *vport;
178101649561SJames Smart 	struct lpfc_hba *phba;
1782c490850aSJames Smart 	struct lpfc_io_buf *lpfc_nbuf;
178301649561SJames Smart 	struct lpfc_iocbq *nvmereq_wqe;
1784c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
178501649561SJames Smart 	unsigned long flags;
178601649561SJames Smart 	int ret_val;
178701649561SJames Smart 
1788c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1789c3725bdcSJames Smart 	 * have timing races.
1790c3725bdcSJames Smart 	 */
179101649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1792c3725bdcSJames Smart 	if (unlikely(!lport))
1793c3725bdcSJames Smart 		return;
179401649561SJames Smart 
1795c3725bdcSJames Smart 	vport = lport->vport;
1796c3725bdcSJames Smart 
1797c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
1798c3725bdcSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1799c3725bdcSJames Smart 				 "6129 Fail Abort, HW Queue Handle NULL.\n");
1800c3725bdcSJames Smart 		return;
1801c3725bdcSJames Smart 	}
1802c3725bdcSJames Smart 
1803c3725bdcSJames Smart 	phba = vport->phba;
1804c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1805c3725bdcSJames Smart 
1806c3725bdcSJames Smart 	if (unlikely(!freqpriv))
1807c3725bdcSJames Smart 		return;
18083386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
18093386f4bdSJames Smart 		return;
18103386f4bdSJames Smart 
181101649561SJames Smart 	/* Announce entry to new IO submit field. */
181286c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
181301649561SJames Smart 			 "6002 Abort Request to rport DID x%06x "
181432350664SJames Smart 			 "for nvme_fc_req x%px\n",
181501649561SJames Smart 			 pnvme_rport->port_id,
181601649561SJames Smart 			 pnvme_fcreq);
181701649561SJames Smart 
181801649561SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
181901649561SJames Smart 	 * cleared when the reset is complete and rings reestablished.
182001649561SJames Smart 	 */
182101649561SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
182201649561SJames Smart 	/* driver queued commands are in process of being flushed */
1823c00f62e6SJames Smart 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
182401649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1825372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
182601649561SJames Smart 				 "6139 Driver in reset cleanup - flushing "
182701649561SJames Smart 				 "NVME Req now.  hba_flag x%x\n",
182801649561SJames Smart 				 phba->hba_flag);
182901649561SJames Smart 		return;
183001649561SJames Smart 	}
183101649561SJames Smart 
1832bbe3012bSJames Smart 	lpfc_nbuf = freqpriv->nvme_buf;
183301649561SJames Smart 	if (!lpfc_nbuf) {
183401649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1835372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
183601649561SJames Smart 				 "6140 NVME IO req has no matching lpfc nvme "
183701649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
183801649561SJames Smart 		return;
183901649561SJames Smart 	} else if (!lpfc_nbuf->nvmeCmd) {
184001649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1841372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
184201649561SJames Smart 				 "6141 lpfc NVME IO req has no nvme_fcreq "
184301649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
184401649561SJames Smart 		return;
184501649561SJames Smart 	}
18462b7824d0SJames Smart 	nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
184701649561SJames Smart 
1848c2017260SJames Smart 	/* Guard against IO completion being called at same time */
1849c2017260SJames Smart 	spin_lock(&lpfc_nbuf->buf_lock);
1850c2017260SJames Smart 
185101649561SJames Smart 	/*
185201649561SJames Smart 	 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
185301649561SJames Smart 	 * state must match the nvme_fcreq passed by the nvme
185401649561SJames Smart 	 * transport.  If they don't match, it is likely the driver
185501649561SJames Smart 	 * has already completed the NVME IO and the nvme transport
185601649561SJames Smart 	 * has not seen it yet.
185701649561SJames Smart 	 */
185801649561SJames Smart 	if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
1859372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
186001649561SJames Smart 				 "6143 NVME req mismatch: "
186132350664SJames Smart 				 "lpfc_nbuf x%px nvmeCmd x%px, "
186232350664SJames Smart 				 "pnvme_fcreq x%px.  Skipping Abort xri x%x\n",
186301649561SJames Smart 				 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
18642b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1865c2017260SJames Smart 		goto out_unlock;
186601649561SJames Smart 	}
186701649561SJames Smart 
186801649561SJames Smart 	/* Don't abort IOs no longer on the pending queue. */
186901649561SJames Smart 	if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
1870372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
187132350664SJames Smart 				 "6142 NVME IO req x%px not queued - skipping "
18722b7824d0SJames Smart 				 "abort req xri x%x\n",
18732b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1874c2017260SJames Smart 		goto out_unlock;
187501649561SJames Smart 	}
187601649561SJames Smart 
18774b056682SJames Smart 	atomic_inc(&lport->xmt_fcp_abort);
1878bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1879bd2cdd5eSJames Smart 			 nvmereq_wqe->sli4_xritag,
188000cefeb9SJames Smart 			 nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
1881bd2cdd5eSJames Smart 
188201649561SJames Smart 	/* Outstanding abort is in progress */
188301649561SJames Smart 	if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
1884372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
188501649561SJames Smart 				 "6144 Outstanding NVME I/O Abort Request "
188632350664SJames Smart 				 "still pending on nvme_fcreq x%px, "
1887f1156125SJames Smart 				 "lpfc_ncmd x%px xri x%x\n",
18882b7824d0SJames Smart 				 pnvme_fcreq, lpfc_nbuf,
18892b7824d0SJames Smart 				 nvmereq_wqe->sli4_xritag);
1890c2017260SJames Smart 		goto out_unlock;
189101649561SJames Smart 	}
189201649561SJames Smart 
1893db7531d2SJames Smart 	ret_val = lpfc_sli4_issue_abort_iotag(phba, nvmereq_wqe,
1894db7531d2SJames Smart 					      lpfc_nvme_abort_fcreq_cmpl);
189501649561SJames Smart 
1896c2017260SJames Smart 	spin_unlock(&lpfc_nbuf->buf_lock);
189701649561SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1898a22d73b6SJames Smart 
1899a22d73b6SJames Smart 	/* Make sure HBA is alive */
1900a22d73b6SJames Smart 	lpfc_issue_hb_tmo(phba);
1901a22d73b6SJames Smart 
1902db7531d2SJames Smart 	if (ret_val != WQE_SUCCESS) {
1903372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
190401649561SJames Smart 				 "6137 Failed abts issue_wqe with status x%x "
190532350664SJames Smart 				 "for nvme_fcreq x%px.\n",
190601649561SJames Smart 				 ret_val, pnvme_fcreq);
190701649561SJames Smart 		return;
190801649561SJames Smart 	}
190901649561SJames Smart 
191086c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
19112b7824d0SJames Smart 			 "6138 Transport Abort NVME Request Issued for "
1912db7531d2SJames Smart 			 "ox_id x%x\n",
1913db7531d2SJames Smart 			 nvmereq_wqe->sli4_xritag);
1914c2017260SJames Smart 	return;
1915c2017260SJames Smart 
1916c2017260SJames Smart out_unlock:
1917c2017260SJames Smart 	spin_unlock(&lpfc_nbuf->buf_lock);
1918c2017260SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1919c2017260SJames Smart 	return;
192001649561SJames Smart }
192101649561SJames Smart 
192201649561SJames Smart /* Declare and initialization an instance of the FC NVME template. */
192301649561SJames Smart static struct nvme_fc_port_template lpfc_nvme_template = {
192401649561SJames Smart 	/* initiator-based functions */
192501649561SJames Smart 	.localport_delete  = lpfc_nvme_localport_delete,
192601649561SJames Smart 	.remoteport_delete = lpfc_nvme_remoteport_delete,
192701649561SJames Smart 	.create_queue = lpfc_nvme_create_queue,
192801649561SJames Smart 	.delete_queue = lpfc_nvme_delete_queue,
192901649561SJames Smart 	.ls_req       = lpfc_nvme_ls_req,
193001649561SJames Smart 	.fcp_io       = lpfc_nvme_fcp_io_submit,
193101649561SJames Smart 	.ls_abort     = lpfc_nvme_ls_abort,
193201649561SJames Smart 	.fcp_abort    = lpfc_nvme_fcp_abort,
19339aa09e98SJames Smart 	.xmt_ls_rsp   = lpfc_nvme_xmt_ls_rsp,
193401649561SJames Smart 
193501649561SJames Smart 	.max_hw_queues = 1,
193601649561SJames Smart 	.max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
193701649561SJames Smart 	.max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
193801649561SJames Smart 	.dma_boundary = 0xFFFFFFFF,
193901649561SJames Smart 
194001649561SJames Smart 	/* Sizes of additional private data for data structures.
194101649561SJames Smart 	 * No use for the last two sizes at this time.
194201649561SJames Smart 	 */
194301649561SJames Smart 	.local_priv_sz = sizeof(struct lpfc_nvme_lport),
194401649561SJames Smart 	.remote_priv_sz = sizeof(struct lpfc_nvme_rport),
194501649561SJames Smart 	.lsrqst_priv_sz = 0,
1946bbe3012bSJames Smart 	.fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
194701649561SJames Smart };
194801649561SJames Smart 
19493e749170SLee Jones /*
19505e5b511dSJames Smart  * lpfc_get_nvme_buf - Get a nvme buffer from io_buf_list of the HBA
195101649561SJames Smart  *
19525e5b511dSJames Smart  * This routine removes a nvme buffer from head of @hdwq io_buf_list
195301649561SJames Smart  * and returns to caller.
195401649561SJames Smart  *
195501649561SJames Smart  * Return codes:
195601649561SJames Smart  *   NULL - Error
195701649561SJames Smart  *   Pointer to lpfc_nvme_buf - Success
195801649561SJames Smart  **/
1959c490850aSJames Smart static struct lpfc_io_buf *
1960cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19615e5b511dSJames Smart 		  int idx, int expedite)
196201649561SJames Smart {
1963c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
19645e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
19650794d601SJames Smart 	struct sli4_sge *sgl;
19660794d601SJames Smart 	struct lpfc_iocbq *pwqeq;
19670794d601SJames Smart 	union lpfc_wqe128 *wqe;
196801649561SJames Smart 
1969c490850aSJames Smart 	lpfc_ncmd = lpfc_get_io_buf(phba, NULL, idx, expedite);
19702a5b7d62SJames Smart 
19710794d601SJames Smart 	if (lpfc_ncmd) {
19720794d601SJames Smart 		pwqeq = &(lpfc_ncmd->cur_iocbq);
19730794d601SJames Smart 		wqe = &pwqeq->wqe;
19740794d601SJames Smart 
19750794d601SJames Smart 		/* Setup key fields in buffer that may have been changed
19760794d601SJames Smart 		 * if other protocols used this buffer.
19770794d601SJames Smart 		 */
19780794d601SJames Smart 		pwqeq->iocb_flag = LPFC_IO_NVME;
19790794d601SJames Smart 		pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
19800794d601SJames Smart 		lpfc_ncmd->start_time = jiffies;
19810794d601SJames Smart 		lpfc_ncmd->flags = 0;
19820794d601SJames Smart 
19830794d601SJames Smart 		/* Rsp SGE will be filled in when we rcv an IO
19840794d601SJames Smart 		 * from the NVME Layer to be sent.
19850794d601SJames Smart 		 * The cmd is going to be embedded so we need a SKIP SGE.
19860794d601SJames Smart 		 */
19870794d601SJames Smart 		sgl = lpfc_ncmd->dma_sgl;
19880794d601SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
19890794d601SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
19900794d601SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
19910794d601SJames Smart 		/* Fill in word 3 / sgl_len during cmd submission */
19920794d601SJames Smart 
1993d9f492a1SJames Smart 		/* Initialize 64 bytes only */
19940794d601SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
19950794d601SJames Smart 
19960794d601SJames Smart 		if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
19972a5b7d62SJames Smart 			atomic_inc(&ndlp->cmd_pending);
1998c490850aSJames Smart 			lpfc_ncmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
19992a5b7d62SJames Smart 		}
20005e5b511dSJames Smart 
2001c490850aSJames Smart 	} else {
2002c490850aSJames Smart 		qp = &phba->sli4_hba.hdwq[idx];
20035e5b511dSJames Smart 		qp->empty_io_bufs++;
2004c490850aSJames Smart 	}
20055e5b511dSJames Smart 
200601649561SJames Smart 	return  lpfc_ncmd;
200701649561SJames Smart }
200801649561SJames Smart 
200901649561SJames Smart /**
201001649561SJames Smart  * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
201101649561SJames Smart  * @phba: The Hba for which this call is being executed.
201201649561SJames Smart  * @lpfc_ncmd: The nvme buffer which is being released.
201301649561SJames Smart  *
201401649561SJames Smart  * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
20155e5b511dSJames Smart  * lpfc_io_buf_list list. For SLI4 XRI's are tied to the nvme buffer
201601649561SJames Smart  * and cannot be reused for at least RA_TOV amount of time if it was
201701649561SJames Smart  * aborted.
201801649561SJames Smart  **/
201901649561SJames Smart static void
2020c490850aSJames Smart lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd)
202101649561SJames Smart {
20225e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
202301649561SJames Smart 	unsigned long iflag = 0;
202401649561SJames Smart 
2025c490850aSJames Smart 	if ((lpfc_ncmd->flags & LPFC_SBUF_BUMP_QDEPTH) && lpfc_ncmd->ndlp)
20262a5b7d62SJames Smart 		atomic_dec(&lpfc_ncmd->ndlp->cmd_pending);
20272a5b7d62SJames Smart 
20282a5b7d62SJames Smart 	lpfc_ncmd->ndlp = NULL;
2029c490850aSJames Smart 	lpfc_ncmd->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
20302a5b7d62SJames Smart 
20311fbf9742SJames Smart 	qp = lpfc_ncmd->hdwq;
203222770cbaSJames Smart 	if (unlikely(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
203386c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
203486c67379SJames Smart 				"6310 XB release deferred for "
203586c67379SJames Smart 				"ox_id x%x on reqtag x%x\n",
203686c67379SJames Smart 				lpfc_ncmd->cur_iocbq.sli4_xritag,
203786c67379SJames Smart 				lpfc_ncmd->cur_iocbq.iotag);
203886c67379SJames Smart 
2039c00f62e6SJames Smart 		spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
204001649561SJames Smart 		list_add_tail(&lpfc_ncmd->list,
2041c00f62e6SJames Smart 			&qp->lpfc_abts_io_buf_list);
20425e5b511dSJames Smart 		qp->abts_nvme_io_bufs++;
2043c00f62e6SJames Smart 		spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
2044c490850aSJames Smart 	} else
2045c490850aSJames Smart 		lpfc_release_io_buf(phba, (struct lpfc_io_buf *)lpfc_ncmd, qp);
204601649561SJames Smart }
204701649561SJames Smart 
204801649561SJames Smart /**
204901649561SJames Smart  * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
2050f6b35a75SLee Jones  * @vport: the lpfc_vport instance requesting a localport.
205101649561SJames Smart  *
205201649561SJames Smart  * This routine is invoked to create an nvme localport instance to bind
205301649561SJames Smart  * to the nvme_fc_transport.  It is called once during driver load
205401649561SJames Smart  * like lpfc_create_shost after all other services are initialized.
205501649561SJames Smart  * It requires a vport, vpi, and wwns at call time.  Other localport
205601649561SJames Smart  * parameters are modified as the driver's FCID and the Fabric WWN
205701649561SJames Smart  * are established.
205801649561SJames Smart  *
205901649561SJames Smart  * Return codes
206001649561SJames Smart  *      0 - successful
206101649561SJames Smart  *      -ENOMEM - no heap memory available
206201649561SJames Smart  *      other values - from nvme registration upcall
206301649561SJames Smart  **/
206401649561SJames Smart int
206501649561SJames Smart lpfc_nvme_create_localport(struct lpfc_vport *vport)
206601649561SJames Smart {
2067166d7211SJames Smart 	int ret = 0;
206801649561SJames Smart 	struct lpfc_hba  *phba = vport->phba;
206901649561SJames Smart 	struct nvme_fc_port_info nfcp_info;
207001649561SJames Smart 	struct nvme_fc_local_port *localport;
207101649561SJames Smart 	struct lpfc_nvme_lport *lport;
207201649561SJames Smart 
207301649561SJames Smart 	/* Initialize this localport instance.  The vport wwn usage ensures
207401649561SJames Smart 	 * that NPIV is accounted for.
207501649561SJames Smart 	 */
207601649561SJames Smart 	memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
207701649561SJames Smart 	nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
207801649561SJames Smart 	nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
207901649561SJames Smart 	nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
208001649561SJames Smart 
20815b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
20825b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
20835b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
20844d4c4a4aSJames Smart 	 */
20854d4c4a4aSJames Smart 	lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
208601649561SJames Smart 
2087542ddc9bSJames Smart 	/* Advertise how many hw queues we support based on cfg_hdw_queue,
2088542ddc9bSJames Smart 	 * which will not exceed cpu count.
2089542ddc9bSJames Smart 	 */
2090cdb42becSJames Smart 	lpfc_nvme_template.max_hw_queues = phba->cfg_hdw_queue;
209166a210ffSJames Smart 
2092faf5a744SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_FC))
2093faf5a744SArnd Bergmann 		return ret;
2094faf5a744SArnd Bergmann 
209501649561SJames Smart 	/* localport is allocated from the stack, but the registration
209601649561SJames Smart 	 * call allocates heap memory as well as the private area.
209701649561SJames Smart 	 */
2098faf5a744SArnd Bergmann 
209901649561SJames Smart 	ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
210001649561SJames Smart 					 &vport->phba->pcidev->dev, &localport);
210101649561SJames Smart 	if (!ret) {
210201649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
210301649561SJames Smart 				 "6005 Successfully registered local "
210432350664SJames Smart 				 "NVME port num %d, localP x%px, private "
210532350664SJames Smart 				 "x%px, sg_seg %d\n",
210601649561SJames Smart 				 localport->port_num, localport,
210701649561SJames Smart 				 localport->private,
210801649561SJames Smart 				 lpfc_nvme_template.max_sgl_segments);
210901649561SJames Smart 
211001649561SJames Smart 		/* Private is our lport size declared in the template. */
211101649561SJames Smart 		lport = (struct lpfc_nvme_lport *)localport->private;
211201649561SJames Smart 		vport->localport = localport;
211301649561SJames Smart 		lport->vport = vport;
211401649561SJames Smart 		vport->nvmei_support = 1;
21156b486ce9SDick Kennedy 
21164b056682SJames Smart 		atomic_set(&lport->xmt_fcp_noxri, 0);
21174b056682SJames Smart 		atomic_set(&lport->xmt_fcp_bad_ndlp, 0);
21184b056682SJames Smart 		atomic_set(&lport->xmt_fcp_qdepth, 0);
211944c2757bSJames Smart 		atomic_set(&lport->xmt_fcp_err, 0);
21204b056682SJames Smart 		atomic_set(&lport->xmt_fcp_wqerr, 0);
21214b056682SJames Smart 		atomic_set(&lport->xmt_fcp_abort, 0);
21224b056682SJames Smart 		atomic_set(&lport->xmt_ls_abort, 0);
21234b056682SJames Smart 		atomic_set(&lport->xmt_ls_err, 0);
21244b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_xb, 0);
21254b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_err, 0);
21264b056682SJames Smart 		atomic_set(&lport->cmpl_ls_xb, 0);
21274b056682SJames Smart 		atomic_set(&lport->cmpl_ls_err, 0);
21289aa09e98SJames Smart 
212966a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsRequests, 0);
213066a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsCmpls, 0);
21316b486ce9SDick Kennedy 	}
21323ebd9b47SJames Smart 
213301649561SJames Smart 	return ret;
213401649561SJames Smart }
213501649561SJames Smart 
2136d964b3e5SBart Van Assche #if (IS_ENABLED(CONFIG_NVME_FC))
2137add9d6beSJames Smart /* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2138add9d6beSJames Smart  *
2139add9d6beSJames Smart  * The driver has to wait for the host nvme transport to callback
2140add9d6beSJames Smart  * indicating the localport has successfully unregistered all
2141add9d6beSJames Smart  * resources.  Since this is an uninterruptible wait, loop every ten
2142add9d6beSJames Smart  * seconds and print a message indicating no progress.
2143add9d6beSJames Smart  *
2144add9d6beSJames Smart  * An uninterruptible wait is used because of the risk of transport-to-
2145add9d6beSJames Smart  * driver state mismatch.
2146add9d6beSJames Smart  */
21473999df75SBart Van Assche static void
2148add9d6beSJames Smart lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
21497961cba6SEwan D. Milne 			   struct lpfc_nvme_lport *lport,
21507961cba6SEwan D. Milne 			   struct completion *lport_unreg_cmp)
2151add9d6beSJames Smart {
2152add9d6beSJames Smart 	u32 wait_tmo;
21532ab70c21SJames Smart 	int ret, i, pending = 0;
21542ab70c21SJames Smart 	struct lpfc_sli_ring  *pring;
21552ab70c21SJames Smart 	struct lpfc_hba  *phba = vport->phba;
2156e7dab164SJames Smart 	struct lpfc_sli4_hdw_queue *qp;
2157e7dab164SJames Smart 	int abts_scsi, abts_nvme;
2158add9d6beSJames Smart 
2159add9d6beSJames Smart 	/* Host transport has to clean up and confirm requiring an indefinite
2160add9d6beSJames Smart 	 * wait. Print a message if a 10 second wait expires and renew the
2161add9d6beSJames Smart 	 * wait. This is unexpected.
2162add9d6beSJames Smart 	 */
2163add9d6beSJames Smart 	wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2164add9d6beSJames Smart 	while (true) {
21657961cba6SEwan D. Milne 		ret = wait_for_completion_timeout(lport_unreg_cmp, wait_tmo);
2166add9d6beSJames Smart 		if (unlikely(!ret)) {
21672ab70c21SJames Smart 			pending = 0;
2168e7dab164SJames Smart 			abts_scsi = 0;
2169e7dab164SJames Smart 			abts_nvme = 0;
21702ab70c21SJames Smart 			for (i = 0; i < phba->cfg_hdw_queue; i++) {
2171e7dab164SJames Smart 				qp = &phba->sli4_hba.hdwq[i];
2172e7dab164SJames Smart 				pring = qp->io_wq->pring;
21732ab70c21SJames Smart 				if (!pring)
21742ab70c21SJames Smart 					continue;
21752ab70c21SJames Smart 				pending += pring->txcmplq_cnt;
2176e7dab164SJames Smart 				abts_scsi += qp->abts_scsi_io_bufs;
2177e7dab164SJames Smart 				abts_nvme += qp->abts_nvme_io_bufs;
21782ab70c21SJames Smart 			}
2179372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
218032350664SJames Smart 					 "6176 Lport x%px Localport x%px wait "
2181e7dab164SJames Smart 					 "timed out. Pending %d [%d:%d]. "
2182e7dab164SJames Smart 					 "Renewing.\n",
2183e7dab164SJames Smart 					 lport, vport->localport, pending,
2184e7dab164SJames Smart 					 abts_scsi, abts_nvme);
2185add9d6beSJames Smart 			continue;
2186add9d6beSJames Smart 		}
2187add9d6beSJames Smart 		break;
2188add9d6beSJames Smart 	}
2189add9d6beSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
219032350664SJames Smart 			 "6177 Lport x%px Localport x%px Complete Success\n",
2191add9d6beSJames Smart 			 lport, vport->localport);
2192add9d6beSJames Smart }
2193d964b3e5SBart Van Assche #endif
2194add9d6beSJames Smart 
219501649561SJames Smart /**
219601649561SJames Smart  * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
21973e749170SLee Jones  * @vport: pointer to a host virtual N_Port data structure
219801649561SJames Smart  *
219901649561SJames Smart  * This routine is invoked to destroy all lports bound to the phba.
220001649561SJames Smart  * The lport memory was allocated by the nvme fc transport and is
220101649561SJames Smart  * released there.  This routine ensures all rports bound to the
220201649561SJames Smart  * lport have been disconnected.
220301649561SJames Smart  *
220401649561SJames Smart  **/
220501649561SJames Smart void
220601649561SJames Smart lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
220701649561SJames Smart {
22087d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
220901649561SJames Smart 	struct nvme_fc_local_port *localport;
221001649561SJames Smart 	struct lpfc_nvme_lport *lport;
221101649561SJames Smart 	int ret;
22127961cba6SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(lport_unreg_cmp);
221301649561SJames Smart 
221401649561SJames Smart 	if (vport->nvmei_support == 0)
221501649561SJames Smart 		return;
221601649561SJames Smart 
221701649561SJames Smart 	localport = vport->localport;
221801649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
221901649561SJames Smart 
222001649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
222132350664SJames Smart 			 "6011 Destroying NVME localport x%px\n",
222201649561SJames Smart 			 localport);
2223166d7211SJames Smart 
222401649561SJames Smart 	/* lport's rport list is clear.  Unregister
222501649561SJames Smart 	 * lport and release resources.
222601649561SJames Smart 	 */
22277961cba6SEwan D. Milne 	lport->lport_unreg_cmp = &lport_unreg_cmp;
222801649561SJames Smart 	ret = nvme_fc_unregister_localport(localport);
2229add9d6beSJames Smart 
2230add9d6beSJames Smart 	/* Wait for completion.  This either blocks
2231add9d6beSJames Smart 	 * indefinitely or succeeds
2232add9d6beSJames Smart 	 */
22337961cba6SEwan D. Milne 	lpfc_nvme_lport_unreg_wait(vport, lport, &lport_unreg_cmp);
22347961cba6SEwan D. Milne 	vport->localport = NULL;
223501649561SJames Smart 
223601649561SJames Smart 	/* Regardless of the unregister upcall response, clear
223701649561SJames Smart 	 * nvmei_support.  All rports are unregistered and the
223801649561SJames Smart 	 * driver will clean up.
223901649561SJames Smart 	 */
224001649561SJames Smart 	vport->nvmei_support = 0;
224101649561SJames Smart 	if (ret == 0) {
224201649561SJames Smart 		lpfc_printf_vlog(vport,
224301649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
224401649561SJames Smart 				 "6009 Unregistered lport Success\n");
224501649561SJames Smart 	} else {
224601649561SJames Smart 		lpfc_printf_vlog(vport,
224701649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
224801649561SJames Smart 				 "6010 Unregistered lport "
224901649561SJames Smart 				 "Failed, status x%x\n",
225001649561SJames Smart 				 ret);
225101649561SJames Smart 	}
2252166d7211SJames Smart #endif
225301649561SJames Smart }
225401649561SJames Smart 
225501649561SJames Smart void
225601649561SJames Smart lpfc_nvme_update_localport(struct lpfc_vport *vport)
225701649561SJames Smart {
22584410a67aSJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
225901649561SJames Smart 	struct nvme_fc_local_port *localport;
226001649561SJames Smart 	struct lpfc_nvme_lport *lport;
226101649561SJames Smart 
226201649561SJames Smart 	localport = vport->localport;
22634410a67aSJames Smart 	if (!localport) {
22644410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
22654410a67aSJames Smart 				 "6710 Update NVME fail. No localport\n");
22664410a67aSJames Smart 		return;
22674410a67aSJames Smart 	}
226801649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
22694410a67aSJames Smart 	if (!lport) {
22704410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
227132350664SJames Smart 				 "6171 Update NVME fail. localP x%px, No lport\n",
22724410a67aSJames Smart 				 localport);
22734410a67aSJames Smart 		return;
22744410a67aSJames Smart 	}
227501649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
227632350664SJames Smart 			 "6012 Update NVME lport x%px did x%x\n",
227701649561SJames Smart 			 localport, vport->fc_myDID);
227801649561SJames Smart 
227901649561SJames Smart 	localport->port_id = vport->fc_myDID;
228001649561SJames Smart 	if (localport->port_id == 0)
228101649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
228201649561SJames Smart 	else
228301649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
228401649561SJames Smart 
228501649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
228632350664SJames Smart 			 "6030 bound lport x%px to DID x%06x\n",
228701649561SJames Smart 			 lport, localport->port_id);
22884410a67aSJames Smart #endif
228901649561SJames Smart }
229001649561SJames Smart 
229101649561SJames Smart int
229201649561SJames Smart lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
229301649561SJames Smart {
22947d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
229501649561SJames Smart 	int ret = 0;
229601649561SJames Smart 	struct nvme_fc_local_port *localport;
229701649561SJames Smart 	struct lpfc_nvme_lport *lport;
229801649561SJames Smart 	struct lpfc_nvme_rport *rport;
229901466024SJames Smart 	struct lpfc_nvme_rport *oldrport;
230001649561SJames Smart 	struct nvme_fc_remote_port *remote_port;
230101649561SJames Smart 	struct nvme_fc_port_info rpinfo;
230293a3922dSJames Smart 	struct lpfc_nodelist *prev_ndlp = NULL;
23031f546823SJames Smart 	struct fc_rport *srport = ndlp->rport;
230401649561SJames Smart 
230501649561SJames Smart 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
230601649561SJames Smart 			 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
230701649561SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type);
230801649561SJames Smart 
230901649561SJames Smart 	localport = vport->localport;
2310bb6a8a2cSDick Kennedy 	if (!localport)
2311bb6a8a2cSDick Kennedy 		return 0;
2312bb6a8a2cSDick Kennedy 
231301649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
231401649561SJames Smart 
23157a06dcd3SJames Smart 	/* NVME rports are not preserved across devloss.
23167a06dcd3SJames Smart 	 * Just register this instance.  Note, rpinfo->dev_loss_tmo
23177a06dcd3SJames Smart 	 * is left 0 to indicate accept transport defaults.  The
23187a06dcd3SJames Smart 	 * driver communicates port role capabilities consistent
23197a06dcd3SJames Smart 	 * with the PRLI response data.
232001649561SJames Smart 	 */
23217a06dcd3SJames Smart 	memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
23227a06dcd3SJames Smart 	rpinfo.port_id = ndlp->nlp_DID;
232301649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET)
23247a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
232501649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
23267a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
232701649561SJames Smart 
23287a06dcd3SJames Smart 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
23297a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
23307a06dcd3SJames Smart 
23317a06dcd3SJames Smart 	rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
23327a06dcd3SJames Smart 	rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
23331f546823SJames Smart 	if (srport)
23341f546823SJames Smart 		rpinfo.dev_loss_tmo = srport->dev_loss_tmo;
23351f546823SJames Smart 	else
23361f546823SJames Smart 		rpinfo.dev_loss_tmo = vport->cfg_devloss_tmo;
233701466024SJames Smart 
2338c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
233901466024SJames Smart 	oldrport = lpfc_ndlp_get_nrport(ndlp);
234061184f17SJames Smart 	if (oldrport) {
234161184f17SJames Smart 		prev_ndlp = oldrport->ndlp;
2342c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
234361184f17SJames Smart 	} else {
2344c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
23454430f7fdSJames Smart 		if (!lpfc_nlp_get(ndlp)) {
23464430f7fdSJames Smart 			dev_warn(&vport->phba->pcidev->dev,
23474430f7fdSJames Smart 				 "Warning - No node ref - exit register\n");
23484430f7fdSJames Smart 			return 0;
23494430f7fdSJames Smart 		}
235061184f17SJames Smart 	}
23513fd78355SJames Smart 
23527a06dcd3SJames Smart 	ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
23537a06dcd3SJames Smart 	if (!ret) {
23547a06dcd3SJames Smart 		/* If the ndlp already has an nrport, this is just
23557a06dcd3SJames Smart 		 * a resume of the existing rport.  Else this is a
23567a06dcd3SJames Smart 		 * new rport.
23577a06dcd3SJames Smart 		 */
2358b15bd3e6SJames Smart 		/* Guard against an unregister/reregister
2359b15bd3e6SJames Smart 		 * race that leaves the WAIT flag set.
2360b15bd3e6SJames Smart 		 */
2361c6adba15SJames Smart 		spin_lock_irq(&ndlp->lock);
236206145683SJames Smart 		ndlp->fc4_xpt_flags &= ~NVME_XPT_UNREG_WAIT;
2363e9b11083SJames Smart 		ndlp->fc4_xpt_flags |= NVME_XPT_REGD;
2364c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
23657a06dcd3SJames Smart 		rport = remote_port->private;
236601466024SJames Smart 		if (oldrport) {
23672b75d0f9SDick Kennedy 
23683fd78355SJames Smart 			/* Sever the ndlp<->rport association
23693fd78355SJames Smart 			 * before dropping the ndlp ref from
23703fd78355SJames Smart 			 * register.
23712b75d0f9SDick Kennedy 			 */
2372c6adba15SJames Smart 			spin_lock_irq(&ndlp->lock);
23732b75d0f9SDick Kennedy 			ndlp->nrport = NULL;
237406145683SJames Smart 			ndlp->fc4_xpt_flags &= ~NVME_XPT_UNREG_WAIT;
2375c6adba15SJames Smart 			spin_unlock_irq(&ndlp->lock);
23762b75d0f9SDick Kennedy 			rport->ndlp = NULL;
23773fd78355SJames Smart 			rport->remoteport = NULL;
2378b04744ceSJames Smart 
2379b04744ceSJames Smart 			/* Reference only removed if previous NDLP is no longer
2380b04744ceSJames Smart 			 * active. It might be just a swap and removing the
2381b04744ceSJames Smart 			 * reference would cause a premature cleanup.
2382b04744ceSJames Smart 			 */
2383b04744ceSJames Smart 			if (prev_ndlp && prev_ndlp != ndlp) {
2384307e3380SJames Smart 				if (!prev_ndlp->nrport)
2385b04744ceSJames Smart 					lpfc_nlp_put(prev_ndlp);
2386b04744ceSJames Smart 			}
23872b75d0f9SDick Kennedy 		}
23882b75d0f9SDick Kennedy 
23892b75d0f9SDick Kennedy 		/* Clean bind the rport to the ndlp. */
239001649561SJames Smart 		rport->remoteport = remote_port;
239101649561SJames Smart 		rport->lport = lport;
23923fd78355SJames Smart 		rport->ndlp = ndlp;
2393c6adba15SJames Smart 		spin_lock_irq(&ndlp->lock);
239401649561SJames Smart 		ndlp->nrport = rport;
2395c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
239601649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO,
239701649561SJames Smart 				 LOG_NVME_DISC | LOG_NODE,
23986825b7bdSJames Smart 				 "6022 Bind lport x%px to remoteport x%px "
23996825b7bdSJames Smart 				 "rport x%px WWNN 0x%llx, "
24007a06dcd3SJames Smart 				 "Rport WWPN 0x%llx DID "
240132350664SJames Smart 				 "x%06x Role x%x, ndlp %p prev_ndlp x%px\n",
240293a3922dSJames Smart 				 lport, remote_port, rport,
240301649561SJames Smart 				 rpinfo.node_name, rpinfo.port_name,
24043fd78355SJames Smart 				 rpinfo.port_id, rpinfo.port_role,
240593a3922dSJames Smart 				 ndlp, prev_ndlp);
240601649561SJames Smart 	} else {
240701649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR,
2408372c187bSDick Kennedy 				 LOG_TRACE_EVENT,
240901649561SJames Smart 				 "6031 RemotePort Registration failed "
241001649561SJames Smart 				 "err: %d, DID x%06x\n",
241101649561SJames Smart 				 ret, ndlp->nlp_DID);
241201649561SJames Smart 	}
24137a06dcd3SJames Smart 
241401649561SJames Smart 	return ret;
2415166d7211SJames Smart #else
2416166d7211SJames Smart 	return 0;
2417166d7211SJames Smart #endif
241801649561SJames Smart }
241901649561SJames Smart 
24203e749170SLee Jones /*
24216f2589f4SJames Smart  * lpfc_nvme_rescan_port - Check to see if we should rescan this remoteport
24226f2589f4SJames Smart  *
24236f2589f4SJames Smart  * If the ndlp represents an NVME Target, that we are logged into,
24246f2589f4SJames Smart  * ping the NVME FC Transport layer to initiate a device rescan
24256f2589f4SJames Smart  * on this remote NPort.
24266f2589f4SJames Smart  */
24276f2589f4SJames Smart void
24286f2589f4SJames Smart lpfc_nvme_rescan_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
24296f2589f4SJames Smart {
24306f2589f4SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2431a6d10f24SJames Smart 	struct lpfc_nvme_rport *nrport;
2432a6d10f24SJames Smart 	struct nvme_fc_remote_port *remoteport = NULL;
24336f2589f4SJames Smart 
2434c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
2435a6d10f24SJames Smart 	nrport = lpfc_ndlp_get_nrport(ndlp);
2436a6d10f24SJames Smart 	if (nrport)
2437a6d10f24SJames Smart 		remoteport = nrport->remoteport;
2438c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
24396f2589f4SJames Smart 
24406f2589f4SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
24416f2589f4SJames Smart 			 "6170 Rescan NPort DID x%06x type x%x "
2442a6d10f24SJames Smart 			 "state x%x nrport x%px remoteport x%px\n",
2443a6d10f24SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type, ndlp->nlp_state,
2444a6d10f24SJames Smart 			 nrport, remoteport);
2445a6d10f24SJames Smart 
2446a6d10f24SJames Smart 	if (!nrport || !remoteport)
2447a6d10f24SJames Smart 		goto rescan_exit;
24486f2589f4SJames Smart 
24496f2589f4SJames Smart 	/* Only rescan if we are an NVME target in the MAPPED state */
24506f2589f4SJames Smart 	if (remoteport->port_role & FC_PORT_ROLE_NVME_DISCOVERY &&
24516f2589f4SJames Smart 	    ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
24526f2589f4SJames Smart 		nvme_fc_rescan_remoteport(remoteport);
24536f2589f4SJames Smart 
2454372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
24556f2589f4SJames Smart 				 "6172 NVME rescanned DID x%06x "
24566f2589f4SJames Smart 				 "port_state x%x\n",
24576f2589f4SJames Smart 				 ndlp->nlp_DID, remoteport->port_state);
24586f2589f4SJames Smart 	}
24596f2589f4SJames Smart 	return;
2460a6d10f24SJames Smart  rescan_exit:
2461a6d10f24SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2462a6d10f24SJames Smart 			 "6169 Skip NVME Rport Rescan, NVME remoteport "
2463a6d10f24SJames Smart 			 "unregistered\n");
24646f2589f4SJames Smart #endif
24656f2589f4SJames Smart }
24666f2589f4SJames Smart 
246701649561SJames Smart /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
246801649561SJames Smart  *
246901649561SJames Smart  * There is no notion of Devloss or rport recovery from the current
247001649561SJames Smart  * nvme_transport perspective.  Loss of an rport just means IO cannot
247101649561SJames Smart  * be sent and recovery is completely up to the initator.
247201649561SJames Smart  * For now, the driver just unbinds the DID and port_role so that
247301649561SJames Smart  * no further IO can be issued.  Changes are planned for later.
247401649561SJames Smart  *
247501649561SJames Smart  * Notes - the ndlp reference count is not decremented here since
247601649561SJames Smart  * since there is no nvme_transport api for devloss.  Node ref count
247701649561SJames Smart  * is only adjusted in driver unload.
247801649561SJames Smart  */
247901649561SJames Smart void
248001649561SJames Smart lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
248101649561SJames Smart {
24827d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
248301649561SJames Smart 	int ret;
248401649561SJames Smart 	struct nvme_fc_local_port *localport;
248501649561SJames Smart 	struct lpfc_nvme_lport *lport;
248601649561SJames Smart 	struct lpfc_nvme_rport *rport;
24879e210178SJames Smart 	struct nvme_fc_remote_port *remoteport = NULL;
248801649561SJames Smart 
248901649561SJames Smart 	localport = vport->localport;
249001649561SJames Smart 
249101649561SJames Smart 	/* This is fundamental error.  The localport is always
249201649561SJames Smart 	 * available until driver unload.  Just exit.
249301649561SJames Smart 	 */
249401649561SJames Smart 	if (!localport)
249501649561SJames Smart 		return;
249601649561SJames Smart 
249701649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
249801649561SJames Smart 	if (!lport)
249901649561SJames Smart 		goto input_err;
250001649561SJames Smart 
2501c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
250201466024SJames Smart 	rport = lpfc_ndlp_get_nrport(ndlp);
25039e210178SJames Smart 	if (rport)
25049e210178SJames Smart 		remoteport = rport->remoteport;
2505c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
25069e210178SJames Smart 	if (!remoteport)
250701649561SJames Smart 		goto input_err;
250801649561SJames Smart 
250901649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
251032350664SJames Smart 			 "6033 Unreg nvme remoteport x%px, portname x%llx, "
2511e9b11083SJames Smart 			 "port_id x%06x, portstate x%x port type x%x "
2512e9b11083SJames Smart 			 "refcnt %d\n",
251301649561SJames Smart 			 remoteport, remoteport->port_name,
251401649561SJames Smart 			 remoteport->port_id, remoteport->port_state,
2515e9b11083SJames Smart 			 ndlp->nlp_type, kref_read(&ndlp->kref));
251601649561SJames Smart 
251701649561SJames Smart 	/* Sanity check ndlp type.  Only call for NVME ports. Don't
251801649561SJames Smart 	 * clear any rport state until the transport calls back.
251901649561SJames Smart 	 */
25203b5bde69SJames Smart 
25213b5bde69SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET) {
25227a06dcd3SJames Smart 		/* No concern about the role change on the nvme remoteport.
25237a06dcd3SJames Smart 		 * The transport will update it.
25247a06dcd3SJames Smart 		 */
2525e9b11083SJames Smart 		spin_lock_irq(&vport->phba->hbalock);
252606145683SJames Smart 		ndlp->fc4_xpt_flags |= NVME_XPT_UNREG_WAIT;
2527e9b11083SJames Smart 		spin_unlock_irq(&vport->phba->hbalock);
25287438273fSJames Smart 
25297438273fSJames Smart 		/* Don't let the host nvme transport keep sending keep-alives
25307438273fSJames Smart 		 * on this remoteport. Vport is unloading, no recovery. The
25317438273fSJames Smart 		 * return values is ignored.  The upcall is a courtesy to the
25327438273fSJames Smart 		 * transport.
25337438273fSJames Smart 		 */
25347438273fSJames Smart 		if (vport->load_flag & FC_UNLOADING)
25357438273fSJames Smart 			(void)nvme_fc_set_remoteport_devloss(remoteport, 0);
25367438273fSJames Smart 
253701649561SJames Smart 		ret = nvme_fc_unregister_remoteport(remoteport);
2538e9b11083SJames Smart 
2539e9b11083SJames Smart 		/* The driver no longer knows if the nrport memory is valid.
2540e9b11083SJames Smart 		 * because the controller teardown process has begun and
2541e9b11083SJames Smart 		 * is asynchronous.  Break the binding in the ndlp. Also
2542e9b11083SJames Smart 		 * remove the register ndlp reference to setup node release.
2543e9b11083SJames Smart 		 */
2544e9b11083SJames Smart 		ndlp->nrport = NULL;
25453fd78355SJames Smart 		lpfc_nlp_put(ndlp);
2546e9b11083SJames Smart 		if (ret != 0) {
2547372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
254801649561SJames Smart 					 "6167 NVME unregister failed %d "
254901649561SJames Smart 					 "port_state x%x\n",
255001649561SJames Smart 					 ret, remoteport->port_state);
25513fd78355SJames Smart 		}
255201649561SJames Smart 	}
255301649561SJames Smart 	return;
255401649561SJames Smart 
255501649561SJames Smart  input_err:
2556166d7211SJames Smart #endif
2557372c187bSDick Kennedy 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
255832350664SJames Smart 			 "6168 State error: lport x%px, rport x%px FCID x%06x\n",
255901649561SJames Smart 			 vport->localport, ndlp->rport, ndlp->nlp_DID);
256001649561SJames Smart }
2561318083adSJames Smart 
2562318083adSJames Smart /**
2563318083adSJames Smart  * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2564318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
2565318083adSJames Smart  * @axri: pointer to the fcp xri abort wcqe structure.
2566c00f62e6SJames Smart  * @lpfc_ncmd: The nvme job structure for the request being aborted.
2567318083adSJames Smart  *
2568318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
2569952c303bSDick Kennedy  * NVME aborted xri.  Aborted NVME IO commands are completed to the transport
2570952c303bSDick Kennedy  * here.
2571318083adSJames Smart  **/
2572318083adSJames Smart void
2573318083adSJames Smart lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2574c00f62e6SJames Smart 			   struct sli4_wcqe_xri_aborted *axri,
2575c00f62e6SJames Smart 			   struct lpfc_io_buf *lpfc_ncmd)
2576318083adSJames Smart {
2577318083adSJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
2578952c303bSDick Kennedy 	struct nvmefc_fcp_req *nvme_cmd = NULL;
2579c00f62e6SJames Smart 	struct lpfc_nodelist *ndlp = lpfc_ncmd->ndlp;
2580318083adSJames Smart 
2581318083adSJames Smart 
2582952c303bSDick Kennedy 	if (ndlp)
2583318083adSJames Smart 		lpfc_sli4_abts_err_handler(phba, ndlp, axri);
258486c67379SJames Smart 
258586c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2586c00f62e6SJames Smart 			"6311 nvme_cmd %p xri x%x tag x%x abort complete and "
2587c00f62e6SJames Smart 			"xri released\n",
2588952c303bSDick Kennedy 			lpfc_ncmd->nvmeCmd, xri,
2589952c303bSDick Kennedy 			lpfc_ncmd->cur_iocbq.iotag);
259086c67379SJames Smart 
2591952c303bSDick Kennedy 	/* Aborted NVME commands are required to not complete
2592952c303bSDick Kennedy 	 * before the abort exchange command fully completes.
2593952c303bSDick Kennedy 	 * Once completed, it is available via the put list.
2594952c303bSDick Kennedy 	 */
25953fd78355SJames Smart 	if (lpfc_ncmd->nvmeCmd) {
2596952c303bSDick Kennedy 		nvme_cmd = lpfc_ncmd->nvmeCmd;
2597952c303bSDick Kennedy 		nvme_cmd->done(nvme_cmd);
25983fd78355SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
25993fd78355SJames Smart 	}
2600318083adSJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
2601318083adSJames Smart }
2602c3725bdcSJames Smart 
2603c3725bdcSJames Smart /**
2604c3725bdcSJames Smart  * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2605c3725bdcSJames Smart  * @phba: Pointer to HBA context object.
2606c3725bdcSJames Smart  *
2607c3725bdcSJames Smart  * This function flushes all wqes in the nvme rings and frees all resources
2608c3725bdcSJames Smart  * in the txcmplq. This function does not issue abort wqes for the IO
2609c3725bdcSJames Smart  * commands in txcmplq, they will just be returned with
2610c3725bdcSJames Smart  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2611c3725bdcSJames Smart  * slot has been permanently disabled.
2612c3725bdcSJames Smart  **/
2613c3725bdcSJames Smart void
2614c3725bdcSJames Smart lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2615c3725bdcSJames Smart {
2616c3725bdcSJames Smart 	struct lpfc_sli_ring  *pring;
2617c3725bdcSJames Smart 	u32 i, wait_cnt = 0;
2618c3725bdcSJames Smart 
2619cdb42becSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4 || !phba->sli4_hba.hdwq)
2620c3725bdcSJames Smart 		return;
2621c3725bdcSJames Smart 
2622c00f62e6SJames Smart 	/* Cycle through all IO rings and make sure all outstanding
2623c3725bdcSJames Smart 	 * WQEs have been removed from the txcmplqs.
2624c3725bdcSJames Smart 	 */
2625cdb42becSJames Smart 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
2626c00f62e6SJames Smart 		if (!phba->sli4_hba.hdwq[i].io_wq)
26276a828b0fSJames Smart 			continue;
2628c00f62e6SJames Smart 		pring = phba->sli4_hba.hdwq[i].io_wq->pring;
2629c3725bdcSJames Smart 
2630d580c613SJames Smart 		if (!pring)
2631d580c613SJames Smart 			continue;
2632d580c613SJames Smart 
2633c3725bdcSJames Smart 		/* Retrieve everything on the txcmplq */
2634c3725bdcSJames Smart 		while (!list_empty(&pring->txcmplq)) {
2635c3725bdcSJames Smart 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
2636c3725bdcSJames Smart 			wait_cnt++;
2637c3725bdcSJames Smart 
2638c3725bdcSJames Smart 			/* The sleep is 10mS.  Every ten seconds,
2639c3725bdcSJames Smart 			 * dump a message.  Something is wrong.
2640c3725bdcSJames Smart 			 */
2641c3725bdcSJames Smart 			if ((wait_cnt % 1000) == 0) {
2642372c187bSDick Kennedy 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2643c3725bdcSJames Smart 						"6178 NVME IO not empty, "
2644c3725bdcSJames Smart 						"cnt %d\n", wait_cnt);
2645c3725bdcSJames Smart 			}
2646c3725bdcSJames Smart 		}
2647c3725bdcSJames Smart 	}
2648a22d73b6SJames Smart 
2649a22d73b6SJames Smart 	/* Make sure HBA is alive */
2650a22d73b6SJames Smart 	lpfc_issue_hb_tmo(phba);
2651a22d73b6SJames Smart 
2652c3725bdcSJames Smart }
265384f2ddf8SJames Smart 
265484f2ddf8SJames Smart void
26559ec58ec7SJames Smart lpfc_nvme_cancel_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
26569ec58ec7SJames Smart 		      uint32_t stat, uint32_t param)
265784f2ddf8SJames Smart {
265884f2ddf8SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
265984f2ddf8SJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
266084f2ddf8SJames Smart 	struct nvmefc_fcp_req *nCmd;
26619ec58ec7SJames Smart 	struct lpfc_wcqe_complete wcqe;
26629ec58ec7SJames Smart 	struct lpfc_wcqe_complete *wcqep = &wcqe;
266384f2ddf8SJames Smart 
26649ec58ec7SJames Smart 	lpfc_ncmd = (struct lpfc_io_buf *)pwqeIn->context1;
26659ec58ec7SJames Smart 	if (!lpfc_ncmd) {
266684f2ddf8SJames Smart 		lpfc_sli_release_iocbq(phba, pwqeIn);
266784f2ddf8SJames Smart 		return;
266884f2ddf8SJames Smart 	}
266984f2ddf8SJames Smart 	/* For abort iocb just return, IO iocb will do a done call */
267084f2ddf8SJames Smart 	if (bf_get(wqe_cmnd, &pwqeIn->wqe.gen_req.wqe_com) ==
267184f2ddf8SJames Smart 	    CMD_ABORT_XRI_CX) {
267284f2ddf8SJames Smart 		lpfc_sli_release_iocbq(phba, pwqeIn);
267384f2ddf8SJames Smart 		return;
267484f2ddf8SJames Smart 	}
267584f2ddf8SJames Smart 
267684f2ddf8SJames Smart 	spin_lock(&lpfc_ncmd->buf_lock);
26779ec58ec7SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
26789ec58ec7SJames Smart 	if (!nCmd) {
267984f2ddf8SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
268084f2ddf8SJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
268184f2ddf8SJames Smart 		return;
268284f2ddf8SJames Smart 	}
26839ec58ec7SJames Smart 	spin_unlock(&lpfc_ncmd->buf_lock);
268484f2ddf8SJames Smart 
268584f2ddf8SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
268684f2ddf8SJames Smart 			"6194 NVME Cancel xri %x\n",
268784f2ddf8SJames Smart 			lpfc_ncmd->cur_iocbq.sli4_xritag);
268884f2ddf8SJames Smart 
26899ec58ec7SJames Smart 	wcqep->word0 = 0;
26909ec58ec7SJames Smart 	bf_set(lpfc_wcqe_c_status, wcqep, stat);
26919ec58ec7SJames Smart 	wcqep->parameter = param;
26929ec58ec7SJames Smart 	wcqep->word3 = 0; /* xb is 0 */
269384f2ddf8SJames Smart 
269484f2ddf8SJames Smart 	/* Call release with XB=1 to queue the IO into the abort list. */
26959ec58ec7SJames Smart 	if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
26969ec58ec7SJames Smart 		bf_set(lpfc_wcqe_c_xb, wcqep, 1);
26979ec58ec7SJames Smart 
26989ec58ec7SJames Smart 	(pwqeIn->wqe_cmpl)(phba, pwqeIn, wcqep);
269984f2ddf8SJames Smart #endif
270084f2ddf8SJames Smart }
2701