xref: /linux/drivers/scsi/lpfc/lpfc_nvme.c (revision db7531d2b3775c662466e4eeda774986472c6d18)
101649561SJames Smart /*******************************************************************
201649561SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
301649561SJames Smart  * Fibre Channel Host Bus Adapters.                                *
40d041215SJames Smart  * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
53e21d1cbSJames Smart  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
601649561SJames Smart  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
701649561SJames Smart  * EMULEX and SLI are trademarks of Emulex.                        *
8d080abe0SJames Smart  * www.broadcom.com                                                *
901649561SJames Smart  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
1001649561SJames Smart  *                                                                 *
1101649561SJames Smart  * This program is free software; you can redistribute it and/or   *
1201649561SJames Smart  * modify it under the terms of version 2 of the GNU General       *
1301649561SJames Smart  * Public License as published by the Free Software Foundation.    *
1401649561SJames Smart  * This program is distributed in the hope that it will be useful. *
1501649561SJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
1601649561SJames Smart  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
1701649561SJames Smart  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
1801649561SJames Smart  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
1901649561SJames Smart  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
2001649561SJames Smart  * more details, a copy of which can be found in the file COPYING  *
2101649561SJames Smart  * included with this package.                                     *
2201649561SJames Smart  ********************************************************************/
2301649561SJames Smart #include <linux/pci.h>
2401649561SJames Smart #include <linux/slab.h>
2501649561SJames Smart #include <linux/interrupt.h>
2601649561SJames Smart #include <linux/delay.h>
2701649561SJames Smart #include <asm/unaligned.h>
2801649561SJames Smart #include <linux/crc-t10dif.h>
2901649561SJames Smart #include <net/checksum.h>
3001649561SJames Smart 
3101649561SJames Smart #include <scsi/scsi.h>
3201649561SJames Smart #include <scsi/scsi_device.h>
3301649561SJames Smart #include <scsi/scsi_eh.h>
3401649561SJames Smart #include <scsi/scsi_host.h>
3501649561SJames Smart #include <scsi/scsi_tcq.h>
3601649561SJames Smart #include <scsi/scsi_transport_fc.h>
3701649561SJames Smart #include <scsi/fc/fc_fs.h>
3801649561SJames Smart 
3901649561SJames Smart #include "lpfc_version.h"
4001649561SJames Smart #include "lpfc_hw4.h"
4101649561SJames Smart #include "lpfc_hw.h"
4201649561SJames Smart #include "lpfc_sli.h"
4301649561SJames Smart #include "lpfc_sli4.h"
4401649561SJames Smart #include "lpfc_nl.h"
4501649561SJames Smart #include "lpfc_disc.h"
4601649561SJames Smart #include "lpfc.h"
4701649561SJames Smart #include "lpfc_nvme.h"
4801649561SJames Smart #include "lpfc_scsi.h"
4901649561SJames Smart #include "lpfc_logmsg.h"
5001649561SJames Smart #include "lpfc_crtn.h"
5101649561SJames Smart #include "lpfc_vport.h"
52bd2cdd5eSJames Smart #include "lpfc_debugfs.h"
5301649561SJames Smart 
5401649561SJames Smart /* NVME initiator-based functions */
5501649561SJames Smart 
56c490850aSJames Smart static struct lpfc_io_buf *
57cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
585e5b511dSJames Smart 		  int idx, int expedite);
5901649561SJames Smart 
6001649561SJames Smart static void
61c490850aSJames Smart lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_io_buf *);
6201649561SJames Smart 
6381e6a637SJames Smart static struct nvme_fc_port_template lpfc_nvme_template;
6401649561SJames Smart 
6501649561SJames Smart /**
6601649561SJames Smart  * lpfc_nvme_create_queue -
6709d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from
6801649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
6909d99705SLee Jones  * @qsize: Size of the queue in bytes
7001649561SJames Smart  * @handle: An opaque driver handle used in follow-up calls.
7101649561SJames Smart  *
7201649561SJames Smart  * Driver registers this routine to preallocate and initialize any
7301649561SJames Smart  * internal data structures to bind the @qidx to its internal IO queues.
7401649561SJames Smart  * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
7501649561SJames Smart  *
7601649561SJames Smart  * Return value :
7701649561SJames Smart  *   0 - Success
7801649561SJames Smart  *   -EINVAL - Unsupported input value.
7901649561SJames Smart  *   -ENOMEM - Could not alloc necessary memory
8001649561SJames Smart  **/
8101649561SJames Smart static int
8201649561SJames Smart lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
8301649561SJames Smart 		       unsigned int qidx, u16 qsize,
8401649561SJames Smart 		       void **handle)
8501649561SJames Smart {
8601649561SJames Smart 	struct lpfc_nvme_lport *lport;
8701649561SJames Smart 	struct lpfc_vport *vport;
8801649561SJames Smart 	struct lpfc_nvme_qhandle *qhandle;
8901649561SJames Smart 	char *str;
9001649561SJames Smart 
91c3725bdcSJames Smart 	if (!pnvme_lport->private)
92c3725bdcSJames Smart 		return -ENOMEM;
93c3725bdcSJames Smart 
9401649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
9501649561SJames Smart 	vport = lport->vport;
9601649561SJames Smart 	qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
9701649561SJames Smart 	if (qhandle == NULL)
9801649561SJames Smart 		return -ENOMEM;
9901649561SJames Smart 
100d6d189ceSBart Van Assche 	qhandle->cpu_id = raw_smp_processor_id();
10101649561SJames Smart 	qhandle->qidx = qidx;
10201649561SJames Smart 	/*
10301649561SJames Smart 	 * NVME qidx == 0 is the admin queue, so both admin queue
10401649561SJames Smart 	 * and first IO queue will use MSI-X vector and associated
10501649561SJames Smart 	 * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
10601649561SJames Smart 	 */
10701649561SJames Smart 	if (qidx) {
10801649561SJames Smart 		str = "IO ";  /* IO queue */
10901649561SJames Smart 		qhandle->index = ((qidx - 1) %
1106a828b0fSJames Smart 			lpfc_nvme_template.max_hw_queues);
11101649561SJames Smart 	} else {
11201649561SJames Smart 		str = "ADM";  /* Admin queue */
11301649561SJames Smart 		qhandle->index = qidx;
11401649561SJames Smart 	}
11501649561SJames Smart 
116d58734f0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
11701649561SJames Smart 			 "6073 Binding %s HdwQueue %d  (cpu %d) to "
11832350664SJames Smart 			 "hdw_queue %d qhandle x%px\n", str,
11901649561SJames Smart 			 qidx, qhandle->cpu_id, qhandle->index, qhandle);
12001649561SJames Smart 	*handle = (void *)qhandle;
12101649561SJames Smart 	return 0;
12201649561SJames Smart }
12301649561SJames Smart 
12401649561SJames Smart /**
12501649561SJames Smart  * lpfc_nvme_delete_queue -
12609d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from
12701649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
12801649561SJames Smart  * @handle: An opaque driver handle from lpfc_nvme_create_queue
12901649561SJames Smart  *
13001649561SJames Smart  * Driver registers this routine to free
13101649561SJames Smart  * any internal data structures to bind the @qidx to its internal
13201649561SJames Smart  * IO queues.
13301649561SJames Smart  *
13401649561SJames Smart  * Return value :
13501649561SJames Smart  *   0 - Success
13601649561SJames Smart  *   TODO:  What are the failure codes.
13701649561SJames Smart  **/
13801649561SJames Smart static void
13901649561SJames Smart lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
14001649561SJames Smart 		       unsigned int qidx,
14101649561SJames Smart 		       void *handle)
14201649561SJames Smart {
14301649561SJames Smart 	struct lpfc_nvme_lport *lport;
14401649561SJames Smart 	struct lpfc_vport *vport;
14501649561SJames Smart 
146c3725bdcSJames Smart 	if (!pnvme_lport->private)
147c3725bdcSJames Smart 		return;
148c3725bdcSJames Smart 
14901649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
15001649561SJames Smart 	vport = lport->vport;
15101649561SJames Smart 
15201649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
15332350664SJames Smart 			"6001 ENTER.  lpfc_pnvme x%px, qidx x%x qhandle x%px\n",
15401649561SJames Smart 			lport, qidx, handle);
15501649561SJames Smart 	kfree(handle);
15601649561SJames Smart }
15701649561SJames Smart 
15801649561SJames Smart static void
15901649561SJames Smart lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
16001649561SJames Smart {
16101649561SJames Smart 	struct lpfc_nvme_lport *lport = localport->private;
16201649561SJames Smart 
163add9d6beSJames Smart 	lpfc_printf_vlog(lport->vport, KERN_INFO, LOG_NVME,
16432350664SJames Smart 			 "6173 localport x%px delete complete\n",
165add9d6beSJames Smart 			 lport);
166add9d6beSJames Smart 
16701649561SJames Smart 	/* release any threads waiting for the unreg to complete */
1687961cba6SEwan D. Milne 	if (lport->vport->localport)
1697961cba6SEwan D. Milne 		complete(lport->lport_unreg_cmp);
17001649561SJames Smart }
17101649561SJames Smart 
17201649561SJames Smart /* lpfc_nvme_remoteport_delete
17301649561SJames Smart  *
17401649561SJames Smart  * @remoteport: Pointer to an nvme transport remoteport instance.
17501649561SJames Smart  *
17601649561SJames Smart  * This is a template downcall.  NVME transport calls this function
17701649561SJames Smart  * when it has completed the unregistration of a previously
17801649561SJames Smart  * registered remoteport.
17901649561SJames Smart  *
18001649561SJames Smart  * Return value :
18101649561SJames Smart  * None
18201649561SJames Smart  */
1833999df75SBart Van Assche static void
18401649561SJames Smart lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
18501649561SJames Smart {
18601649561SJames Smart 	struct lpfc_nvme_rport *rport = remoteport->private;
18701649561SJames Smart 	struct lpfc_vport *vport;
18801649561SJames Smart 	struct lpfc_nodelist *ndlp;
189e9b11083SJames Smart 	u32 fc4_xpt_flags;
19001649561SJames Smart 
19101649561SJames Smart 	ndlp = rport->ndlp;
192e9b11083SJames Smart 	if (!ndlp) {
193e9b11083SJames Smart 		pr_err("**** %s: NULL ndlp on rport %p remoteport %p\n",
194e9b11083SJames Smart 		       __func__, rport, remoteport);
19501649561SJames Smart 		goto rport_err;
196e9b11083SJames Smart 	}
19701649561SJames Smart 
19801649561SJames Smart 	vport = ndlp->vport;
199e9b11083SJames Smart 	if (!vport) {
200e9b11083SJames Smart 		pr_err("**** %s: Null vport on ndlp %p, ste x%x rport %p\n",
201e9b11083SJames Smart 		       __func__, ndlp, ndlp->nlp_state, rport);
20201649561SJames Smart 		goto rport_err;
203e9b11083SJames Smart 	}
204e9b11083SJames Smart 
205e9b11083SJames Smart 	fc4_xpt_flags = NVME_XPT_REGD | SCSI_XPT_REGD;
20601649561SJames Smart 
20701649561SJames Smart 	/* Remove this rport from the lport's list - memory is owned by the
20801649561SJames Smart 	 * transport. Remove the ndlp reference for the NVME transport before
2097a06dcd3SJames Smart 	 * calling state machine to remove the node.
21001649561SJames Smart 	 */
21101649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
212e9b11083SJames Smart 			"6146 remoteport delete of remoteport %p\n",
21301649561SJames Smart 			remoteport);
214c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
215b15bd3e6SJames Smart 
216b15bd3e6SJames Smart 	/* The register rebind might have occurred before the delete
217b15bd3e6SJames Smart 	 * downcall.  Guard against this race.
218b15bd3e6SJames Smart 	 */
219e9b11083SJames Smart 	if (ndlp->fc4_xpt_flags & NLP_WAIT_FOR_UNREG)
220e9b11083SJames Smart 		ndlp->fc4_xpt_flags &= ~(NLP_WAIT_FOR_UNREG | NVME_XPT_REGD);
221e9b11083SJames Smart 
222c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
2233fd78355SJames Smart 
224e9b11083SJames Smart 	/* On a devloss timeout event, one more put is executed provided the
225e9b11083SJames Smart 	 * NVME and SCSI rport unregister requests are complete.  If the vport
226e9b11083SJames Smart 	 * is unloading, this extra put is executed by lpfc_drop_node.
2273fd78355SJames Smart 	 */
228e9b11083SJames Smart 	if (!(ndlp->fc4_xpt_flags & fc4_xpt_flags))
229e9b11083SJames Smart 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
23001649561SJames Smart 
23101649561SJames Smart  rport_err:
2323fd78355SJames Smart 	return;
23301649561SJames Smart }
23401649561SJames Smart 
2353a8070c5SJames Smart /**
2363a8070c5SJames Smart  * lpfc_nvme_handle_lsreq - Process an unsolicited NVME LS request
2373a8070c5SJames Smart  * @phba: pointer to lpfc hba data structure.
2383a8070c5SJames Smart  * @axchg: pointer to exchange context for the NVME LS request
2393a8070c5SJames Smart  *
2403a8070c5SJames Smart  * This routine is used for processing an asychronously received NVME LS
2413a8070c5SJames Smart  * request. Any remaining validation is done and the LS is then forwarded
2423a8070c5SJames Smart  * to the nvme-fc transport via nvme_fc_rcv_ls_req().
2433a8070c5SJames Smart  *
2449aa09e98SJames Smart  * The calling sequence should be: nvme_fc_rcv_ls_req() -> (processing)
2459aa09e98SJames Smart  * -> lpfc_nvme_xmt_ls_rsp/cmp -> req->done.
2469aa09e98SJames Smart  * __lpfc_nvme_xmt_ls_rsp_cmp should free the allocated axchg.
2479aa09e98SJames Smart  *
2483a8070c5SJames Smart  * Returns 0 if LS was handled and delivered to the transport
2493a8070c5SJames Smart  * Returns 1 if LS failed to be handled and should be dropped
2503a8070c5SJames Smart  */
2513a8070c5SJames Smart int
2523a8070c5SJames Smart lpfc_nvme_handle_lsreq(struct lpfc_hba *phba,
2533a8070c5SJames Smart 			struct lpfc_async_xchg_ctx *axchg)
2543a8070c5SJames Smart {
2559aa09e98SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2569aa09e98SJames Smart 	struct lpfc_vport *vport;
2579aa09e98SJames Smart 	struct lpfc_nvme_rport *lpfc_rport;
2589aa09e98SJames Smart 	struct nvme_fc_remote_port *remoteport;
2599aa09e98SJames Smart 	struct lpfc_nvme_lport *lport;
2609aa09e98SJames Smart 	uint32_t *payload = axchg->payload;
2619aa09e98SJames Smart 	int rc;
2629aa09e98SJames Smart 
2639aa09e98SJames Smart 	vport = axchg->ndlp->vport;
2649aa09e98SJames Smart 	lpfc_rport = axchg->ndlp->nrport;
2659aa09e98SJames Smart 	if (!lpfc_rport)
2669aa09e98SJames Smart 		return -EINVAL;
2679aa09e98SJames Smart 
2689aa09e98SJames Smart 	remoteport = lpfc_rport->remoteport;
2699aa09e98SJames Smart 	if (!vport->localport)
2709aa09e98SJames Smart 		return -EINVAL;
2719aa09e98SJames Smart 
2729aa09e98SJames Smart 	lport = vport->localport->private;
2739aa09e98SJames Smart 	if (!lport)
2749aa09e98SJames Smart 		return -EINVAL;
2759aa09e98SJames Smart 
2769aa09e98SJames Smart 	rc = nvme_fc_rcv_ls_req(remoteport, &axchg->ls_rsp, axchg->payload,
2779aa09e98SJames Smart 				axchg->size);
2789aa09e98SJames Smart 
2799aa09e98SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2809aa09e98SJames Smart 			"6205 NVME Unsol rcv: sz %d rc %d: %08x %08x %08x "
2819aa09e98SJames Smart 			"%08x %08x %08x\n",
2829aa09e98SJames Smart 			axchg->size, rc,
2839aa09e98SJames Smart 			*payload, *(payload+1), *(payload+2),
2849aa09e98SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
2859aa09e98SJames Smart 
2869aa09e98SJames Smart 	if (!rc)
2879aa09e98SJames Smart 		return 0;
2889aa09e98SJames Smart #endif
2893a8070c5SJames Smart 	return 1;
2903a8070c5SJames Smart }
2913a8070c5SJames Smart 
2926514b25dSJames Smart /**
2936514b25dSJames Smart  * __lpfc_nvme_ls_req_cmp - Generic completion handler for a NVME
2946514b25dSJames Smart  *        LS request.
2956514b25dSJames Smart  * @phba: Pointer to HBA context object
2966514b25dSJames Smart  * @vport: The local port that issued the LS
2976514b25dSJames Smart  * @cmdwqe: Pointer to driver command WQE object.
2986514b25dSJames Smart  * @wcqe: Pointer to driver response CQE object.
2996514b25dSJames Smart  *
3006514b25dSJames Smart  * This function is the generic completion handler for NVME LS requests.
3016514b25dSJames Smart  * The function updates any states and statistics, calls the transport
3026514b25dSJames Smart  * ls_req done() routine, then tears down the command and buffers used
3036514b25dSJames Smart  * for the LS request.
3046514b25dSJames Smart  **/
3056514b25dSJames Smart void
3066514b25dSJames Smart __lpfc_nvme_ls_req_cmp(struct lpfc_hba *phba,  struct lpfc_vport *vport,
3076514b25dSJames Smart 			struct lpfc_iocbq *cmdwqe,
3086514b25dSJames Smart 			struct lpfc_wcqe_complete *wcqe)
3096514b25dSJames Smart {
3106514b25dSJames Smart 	struct nvmefc_ls_req *pnvme_lsreq;
3116514b25dSJames Smart 	struct lpfc_dmabuf *buf_ptr;
3126514b25dSJames Smart 	struct lpfc_nodelist *ndlp;
3136514b25dSJames Smart 	uint32_t status;
3146514b25dSJames Smart 
3156514b25dSJames Smart 	pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
3166514b25dSJames Smart 	ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
3176514b25dSJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
3186514b25dSJames Smart 
3196514b25dSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
3206514b25dSJames Smart 			 "6047 NVMEx LS REQ %px cmpl DID %x Xri: %x "
3216514b25dSJames Smart 			 "status %x reason x%x cmd:x%px lsreg:x%px bmp:x%px "
3226514b25dSJames Smart 			 "ndlp:x%px\n",
3236514b25dSJames Smart 			 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
3246514b25dSJames Smart 			 cmdwqe->sli4_xritag, status,
3256514b25dSJames Smart 			 (wcqe->parameter & 0xffff),
3266514b25dSJames Smart 			 cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
3276514b25dSJames Smart 
3286514b25dSJames Smart 	lpfc_nvmeio_data(phba, "NVMEx LS CMPL: xri x%x stat x%x parm x%x\n",
3296514b25dSJames Smart 			 cmdwqe->sli4_xritag, status, wcqe->parameter);
3306514b25dSJames Smart 
3316514b25dSJames Smart 	if (cmdwqe->context3) {
3326514b25dSJames Smart 		buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
3336514b25dSJames Smart 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3346514b25dSJames Smart 		kfree(buf_ptr);
3356514b25dSJames Smart 		cmdwqe->context3 = NULL;
3366514b25dSJames Smart 	}
3376514b25dSJames Smart 	if (pnvme_lsreq->done)
3386514b25dSJames Smart 		pnvme_lsreq->done(pnvme_lsreq, status);
3396514b25dSJames Smart 	else
340372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3416514b25dSJames Smart 				 "6046 NVMEx cmpl without done call back? "
3426514b25dSJames Smart 				 "Data %px DID %x Xri: %x status %x\n",
3436514b25dSJames Smart 				pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
3446514b25dSJames Smart 				cmdwqe->sli4_xritag, status);
3456514b25dSJames Smart 	if (ndlp) {
3466514b25dSJames Smart 		lpfc_nlp_put(ndlp);
3476514b25dSJames Smart 		cmdwqe->context1 = NULL;
3486514b25dSJames Smart 	}
3496514b25dSJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
3506514b25dSJames Smart }
3516514b25dSJames Smart 
35201649561SJames Smart static void
3536514b25dSJames Smart lpfc_nvme_ls_req_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
35401649561SJames Smart 		       struct lpfc_wcqe_complete *wcqe)
35501649561SJames Smart {
35601649561SJames Smart 	struct lpfc_vport *vport = cmdwqe->vport;
3574b056682SJames Smart 	struct lpfc_nvme_lport *lport;
35801649561SJames Smart 	uint32_t status;
35901649561SJames Smart 
36001649561SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
36166a210ffSJames Smart 
36266a85155SJames Smart 	if (vport->localport) {
36366a85155SJames Smart 		lport = (struct lpfc_nvme_lport *)vport->localport->private;
36466a210ffSJames Smart 		if (lport) {
36566a210ffSJames Smart 			atomic_inc(&lport->fc4NvmeLsCmpls);
3664b056682SJames Smart 			if (status) {
3674b056682SJames Smart 				if (bf_get(lpfc_wcqe_c_xb, wcqe))
3684b056682SJames Smart 					atomic_inc(&lport->cmpl_ls_xb);
3694b056682SJames Smart 				atomic_inc(&lport->cmpl_ls_err);
3704b056682SJames Smart 			}
37166a210ffSJames Smart 		}
37266a85155SJames Smart 	}
3734b056682SJames Smart 
3746514b25dSJames Smart 	__lpfc_nvme_ls_req_cmp(phba, vport, cmdwqe, wcqe);
37501649561SJames Smart }
37601649561SJames Smart 
37701649561SJames Smart static int
37801649561SJames Smart lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
37901649561SJames Smart 		  struct lpfc_dmabuf *inp,
38001649561SJames Smart 		  struct nvmefc_ls_req *pnvme_lsreq,
38101649561SJames Smart 		  void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
38201649561SJames Smart 			       struct lpfc_wcqe_complete *),
38301649561SJames Smart 		  struct lpfc_nodelist *ndlp, uint32_t num_entry,
38401649561SJames Smart 		  uint32_t tmo, uint8_t retry)
38501649561SJames Smart {
38601649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
387205e8240SJames Smart 	union lpfc_wqe128 *wqe;
38801649561SJames Smart 	struct lpfc_iocbq *genwqe;
38901649561SJames Smart 	struct ulp_bde64 *bpl;
39001649561SJames Smart 	struct ulp_bde64 bde;
39101649561SJames Smart 	int i, rc, xmit_len, first_len;
39201649561SJames Smart 
39301649561SJames Smart 	/* Allocate buffer for  command WQE */
39401649561SJames Smart 	genwqe = lpfc_sli_get_iocbq(phba);
39501649561SJames Smart 	if (genwqe == NULL)
39601649561SJames Smart 		return 1;
39701649561SJames Smart 
39801649561SJames Smart 	wqe = &genwqe->wqe;
399d9f492a1SJames Smart 	/* Initialize only 64 bytes */
40001649561SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
40101649561SJames Smart 
40201649561SJames Smart 	genwqe->context3 = (uint8_t *)bmp;
40301649561SJames Smart 	genwqe->iocb_flag |= LPFC_IO_NVME_LS;
40401649561SJames Smart 
40501649561SJames Smart 	/* Save for completion so we can release these resources */
40601649561SJames Smart 	genwqe->context1 = lpfc_nlp_get(ndlp);
4074430f7fdSJames Smart 	if (!genwqe->context1) {
4084430f7fdSJames Smart 		dev_warn(&phba->pcidev->dev,
4094430f7fdSJames Smart 			 "Warning: Failed node ref, not sending LS_REQ\n");
4104430f7fdSJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
4114430f7fdSJames Smart 		return 1;
4124430f7fdSJames Smart 	}
4134430f7fdSJames Smart 
41401649561SJames Smart 	genwqe->context2 = (uint8_t *)pnvme_lsreq;
41501649561SJames Smart 	/* Fill in payload, bp points to frame payload */
41601649561SJames Smart 
41701649561SJames Smart 	if (!tmo)
41801649561SJames Smart 		/* FC spec states we need 3 * ratov for CT requests */
41901649561SJames Smart 		tmo = (3 * phba->fc_ratov);
42001649561SJames Smart 
42101649561SJames Smart 	/* For this command calculate the xmit length of the request bde. */
42201649561SJames Smart 	xmit_len = 0;
42301649561SJames Smart 	first_len = 0;
42401649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
42501649561SJames Smart 	for (i = 0; i < num_entry; i++) {
42601649561SJames Smart 		bde.tus.w = bpl[i].tus.w;
42701649561SJames Smart 		if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
42801649561SJames Smart 			break;
42901649561SJames Smart 		xmit_len += bde.tus.f.bdeSize;
43001649561SJames Smart 		if (i == 0)
43101649561SJames Smart 			first_len = xmit_len;
43201649561SJames Smart 	}
43301649561SJames Smart 
43401649561SJames Smart 	genwqe->rsvd2 = num_entry;
43501649561SJames Smart 	genwqe->hba_wqidx = 0;
43601649561SJames Smart 
43701649561SJames Smart 	/* Words 0 - 2 */
43801649561SJames Smart 	wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
43901649561SJames Smart 	wqe->generic.bde.tus.f.bdeSize = first_len;
44001649561SJames Smart 	wqe->generic.bde.addrLow = bpl[0].addrLow;
44101649561SJames Smart 	wqe->generic.bde.addrHigh = bpl[0].addrHigh;
44201649561SJames Smart 
44301649561SJames Smart 	/* Word 3 */
44401649561SJames Smart 	wqe->gen_req.request_payload_len = first_len;
44501649561SJames Smart 
44601649561SJames Smart 	/* Word 4 */
44701649561SJames Smart 
44801649561SJames Smart 	/* Word 5 */
44901649561SJames Smart 	bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
45001649561SJames Smart 	bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
45101649561SJames Smart 	bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
4528b361639SJames Smart 	bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
45301649561SJames Smart 	bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
45401649561SJames Smart 
45501649561SJames Smart 	/* Word 6 */
45601649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
45701649561SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
45801649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
45901649561SJames Smart 
46001649561SJames Smart 	/* Word 7 */
46101649561SJames Smart 	bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
46201649561SJames Smart 	bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
46301649561SJames Smart 	bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
46401649561SJames Smart 	bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
46501649561SJames Smart 
46601649561SJames Smart 	/* Word 8 */
46701649561SJames Smart 	wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
46801649561SJames Smart 
46901649561SJames Smart 	/* Word 9 */
47001649561SJames Smart 	bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
47101649561SJames Smart 
47201649561SJames Smart 	/* Word 10 */
47301649561SJames Smart 	bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
47401649561SJames Smart 	bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
47501649561SJames Smart 	bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
47601649561SJames Smart 	bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
47701649561SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
47801649561SJames Smart 
47901649561SJames Smart 	/* Word 11 */
48001649561SJames Smart 	bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
48101649561SJames Smart 	bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
48201649561SJames Smart 
48301649561SJames Smart 
48401649561SJames Smart 	/* Issue GEN REQ WQE for NPORT <did> */
48501649561SJames Smart 	genwqe->wqe_cmpl = cmpl;
48601649561SJames Smart 	genwqe->iocb_cmpl = NULL;
48701649561SJames Smart 	genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
48801649561SJames Smart 	genwqe->vport = vport;
48901649561SJames Smart 	genwqe->retry = retry;
49001649561SJames Smart 
491bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  XMIT: xri x%x iotag x%x to x%06x\n",
492bd2cdd5eSJames Smart 			 genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
493bd2cdd5eSJames Smart 
4941fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], genwqe);
495cd22d605SDick Kennedy 	if (rc) {
496372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
49701649561SJames Smart 				 "6045 Issue GEN REQ WQE to NPORT x%x "
4986514b25dSJames Smart 				 "Data: x%x x%x  rc x%x\n",
49901649561SJames Smart 				 ndlp->nlp_DID, genwqe->iotag,
5006514b25dSJames Smart 				 vport->port_state, rc);
501e9b11083SJames Smart 		lpfc_nlp_put(ndlp);
50201649561SJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
50301649561SJames Smart 		return 1;
50401649561SJames Smart 	}
5056514b25dSJames Smart 
5066514b25dSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_ELS,
5076514b25dSJames Smart 			 "6050 Issue GEN REQ WQE to NPORT x%x "
5086514b25dSJames Smart 			 "Data: oxid: x%x state: x%x wq:x%px lsreq:x%px "
5096514b25dSJames Smart 			 "bmp:x%px xmit:%d 1st:%d\n",
5106514b25dSJames Smart 			 ndlp->nlp_DID, genwqe->sli4_xritag,
5116514b25dSJames Smart 			 vport->port_state,
5126514b25dSJames Smart 			 genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
51301649561SJames Smart 	return 0;
51401649561SJames Smart }
51501649561SJames Smart 
5166514b25dSJames Smart 
51701649561SJames Smart /**
5186514b25dSJames Smart  * __lpfc_nvme_ls_req - Generic service routine to issue an NVME LS request
5196514b25dSJames Smart  * @vport: The local port issuing the LS
5206514b25dSJames Smart  * @ndlp: The remote port to send the LS to
5216514b25dSJames Smart  * @pnvme_lsreq: Pointer to LS request structure from the transport
52209d99705SLee Jones  * @gen_req_cmp: Completion call-back
52301649561SJames Smart  *
5246514b25dSJames Smart  * Routine validates the ndlp, builds buffers and sends a GEN_REQUEST
5256514b25dSJames Smart  * WQE to perform the LS operation.
52601649561SJames Smart  *
52701649561SJames Smart  * Return value :
52801649561SJames Smart  *   0 - Success
5296514b25dSJames Smart  *   non-zero: various error codes, in form of -Exxx
53001649561SJames Smart  **/
5316514b25dSJames Smart int
5326514b25dSJames Smart __lpfc_nvme_ls_req(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5336514b25dSJames Smart 		      struct nvmefc_ls_req *pnvme_lsreq,
5346514b25dSJames Smart 		      void (*gen_req_cmp)(struct lpfc_hba *phba,
5356514b25dSJames Smart 				struct lpfc_iocbq *cmdwqe,
5366514b25dSJames Smart 				struct lpfc_wcqe_complete *wcqe))
53701649561SJames Smart {
53801649561SJames Smart 	struct lpfc_dmabuf *bmp;
5396514b25dSJames Smart 	struct ulp_bde64 *bpl;
5406514b25dSJames Smart 	int ret;
541ba43c4d0SJames Smart 	uint16_t ntype, nstate;
54201649561SJames Smart 
543307e3380SJames Smart 	if (!ndlp) {
544372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5456514b25dSJames Smart 				 "6051 NVMEx LS REQ: Bad NDLP x%px, Failing "
5466514b25dSJames Smart 				 "LS Req\n",
5476514b25dSJames Smart 				 ndlp);
548ba43c4d0SJames Smart 		return -ENODEV;
549ba43c4d0SJames Smart 	}
550ba43c4d0SJames Smart 
551ba43c4d0SJames Smart 	ntype = ndlp->nlp_type;
552ba43c4d0SJames Smart 	nstate = ndlp->nlp_state;
553ba43c4d0SJames Smart 	if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
554ba43c4d0SJames Smart 	    (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
555372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5566514b25dSJames Smart 				 "6088 NVMEx LS REQ: Fail DID x%06x not "
5576514b25dSJames Smart 				 "ready for IO. Type x%x, State x%x\n",
5586514b25dSJames Smart 				 ndlp->nlp_DID, ntype, nstate);
559ba43c4d0SJames Smart 		return -ENODEV;
56001649561SJames Smart 	}
5616514b25dSJames Smart 
5626514b25dSJames Smart 	/*
5636514b25dSJames Smart 	 * there are two dma buf in the request, actually there is one and
5646514b25dSJames Smart 	 * the second one is just the start address + cmd size.
5656514b25dSJames Smart 	 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
5666514b25dSJames Smart 	 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
5676514b25dSJames Smart 	 * because the nvem layer owns the data bufs.
5686514b25dSJames Smart 	 * We do not have to break these packets open, we don't care what is
5696514b25dSJames Smart 	 * in them. And we do not have to look at the resonse data, we only
5706514b25dSJames Smart 	 * care that we got a response. All of the caring is going to happen
5716514b25dSJames Smart 	 * in the nvme-fc layer.
5726514b25dSJames Smart 	 */
5736514b25dSJames Smart 
5746514b25dSJames Smart 	bmp = kmalloc(sizeof(*bmp), GFP_KERNEL);
57501649561SJames Smart 	if (!bmp) {
576372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5776514b25dSJames Smart 				 "6044 NVMEx LS REQ: Could not alloc LS buf "
5786514b25dSJames Smart 				 "for DID %x\n",
5796514b25dSJames Smart 				 ndlp->nlp_DID);
5806514b25dSJames Smart 		return -ENOMEM;
58101649561SJames Smart 	}
5826514b25dSJames Smart 
58301649561SJames Smart 	bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
58401649561SJames Smart 	if (!bmp->virt) {
585372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5866514b25dSJames Smart 				 "6042 NVMEx LS REQ: Could not alloc mbuf "
5876514b25dSJames Smart 				 "for DID %x\n",
5886514b25dSJames Smart 				 ndlp->nlp_DID);
58901649561SJames Smart 		kfree(bmp);
5906514b25dSJames Smart 		return -ENOMEM;
59101649561SJames Smart 	}
5926514b25dSJames Smart 
5936514b25dSJames Smart 	INIT_LIST_HEAD(&bmp->list);
5946514b25dSJames Smart 
59501649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
59601649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
59701649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
59801649561SJames Smart 	bpl->tus.f.bdeFlags = 0;
59901649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
60001649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
60101649561SJames Smart 	bpl++;
60201649561SJames Smart 
60301649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
60401649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
60501649561SJames Smart 	bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
60601649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
60701649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
60801649561SJames Smart 
60901649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
6106514b25dSJames Smart 			"6149 NVMEx LS REQ: Issue to DID 0x%06x lsreq x%px, "
6116514b25dSJames Smart 			"rqstlen:%d rsplen:%d %pad %pad\n",
6126514b25dSJames Smart 			ndlp->nlp_DID, pnvme_lsreq, pnvme_lsreq->rqstlen,
613825c6abbSArnd Bergmann 			pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
614825c6abbSArnd Bergmann 			&pnvme_lsreq->rspdma);
61501649561SJames Smart 
61601649561SJames Smart 	ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
6176514b25dSJames Smart 				pnvme_lsreq, gen_req_cmp, ndlp, 2,
6186514b25dSJames Smart 				LPFC_NVME_LS_TIMEOUT, 0);
61901649561SJames Smart 	if (ret != WQE_SUCCESS) {
620372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6216514b25dSJames Smart 				 "6052 NVMEx REQ: EXIT. issue ls wqe failed "
6226514b25dSJames Smart 				 "lsreq x%px Status %x DID %x\n",
6236514b25dSJames Smart 				 pnvme_lsreq, ret, ndlp->nlp_DID);
62401649561SJames Smart 		lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
62501649561SJames Smart 		kfree(bmp);
6266514b25dSJames Smart 		return -EIO;
62701649561SJames Smart 	}
62801649561SJames Smart 
6296514b25dSJames Smart 	return 0;
6306514b25dSJames Smart }
6316514b25dSJames Smart 
6326514b25dSJames Smart /**
6336514b25dSJames Smart  * lpfc_nvme_ls_req - Issue an NVME Link Service request
63409d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from.
6353e749170SLee Jones  * @pnvme_rport: Transport remoteport that LS is to be sent to.
63609d99705SLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
6376514b25dSJames Smart  *
6386514b25dSJames Smart  * Driver registers this routine to handle any link service request
6396514b25dSJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
6406514b25dSJames Smart  *
6416514b25dSJames Smart  * Return value :
6426514b25dSJames Smart  *   0 - Success
6436514b25dSJames Smart  *   non-zero: various error codes, in form of -Exxx
6446514b25dSJames Smart  **/
6456514b25dSJames Smart static int
6466514b25dSJames Smart lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
6476514b25dSJames Smart 		 struct nvme_fc_remote_port *pnvme_rport,
6486514b25dSJames Smart 		 struct nvmefc_ls_req *pnvme_lsreq)
6496514b25dSJames Smart {
6506514b25dSJames Smart 	struct lpfc_nvme_lport *lport;
6516514b25dSJames Smart 	struct lpfc_nvme_rport *rport;
6526514b25dSJames Smart 	struct lpfc_vport *vport;
6536514b25dSJames Smart 	int ret;
6546514b25dSJames Smart 
6556514b25dSJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
6566514b25dSJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
6576514b25dSJames Smart 	if (unlikely(!lport) || unlikely(!rport))
6586514b25dSJames Smart 		return -EINVAL;
6596514b25dSJames Smart 
6606514b25dSJames Smart 	vport = lport->vport;
6616514b25dSJames Smart 	if (vport->load_flag & FC_UNLOADING)
6626514b25dSJames Smart 		return -ENODEV;
6636514b25dSJames Smart 
6646514b25dSJames Smart 	atomic_inc(&lport->fc4NvmeLsRequests);
6656514b25dSJames Smart 
6666514b25dSJames Smart 	ret = __lpfc_nvme_ls_req(vport, rport->ndlp, pnvme_lsreq,
6676514b25dSJames Smart 				 lpfc_nvme_ls_req_cmp);
6686514b25dSJames Smart 	if (ret)
6696514b25dSJames Smart 		atomic_inc(&lport->xmt_ls_err);
6706514b25dSJames Smart 
67101649561SJames Smart 	return ret;
67201649561SJames Smart }
67301649561SJames Smart 
67401649561SJames Smart /**
675e96a22b0SJames Smart  * __lpfc_nvme_ls_abort - Generic service routine to abort a prior
676e96a22b0SJames Smart  *         NVME LS request
677e96a22b0SJames Smart  * @vport: The local port that issued the LS
678e96a22b0SJames Smart  * @ndlp: The remote port the LS was sent to
679e96a22b0SJames Smart  * @pnvme_lsreq: Pointer to LS request structure from the transport
68001649561SJames Smart  *
681e96a22b0SJames Smart  * The driver validates the ndlp, looks for the LS, and aborts the
682e96a22b0SJames Smart  * LS if found.
68301649561SJames Smart  *
684e96a22b0SJames Smart  * Returns:
685e96a22b0SJames Smart  * 0 : if LS found and aborted
686e96a22b0SJames Smart  * non-zero: various error conditions in form -Exxx
687e96a22b0SJames Smart  **/
688e96a22b0SJames Smart int
689e96a22b0SJames Smart __lpfc_nvme_ls_abort(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
690e96a22b0SJames Smart 			struct nvmefc_ls_req *pnvme_lsreq)
691e96a22b0SJames Smart {
692e96a22b0SJames Smart 	struct lpfc_hba *phba = vport->phba;
693e96a22b0SJames Smart 	struct lpfc_sli_ring *pring;
694e96a22b0SJames Smart 	struct lpfc_iocbq *wqe, *next_wqe;
695e96a22b0SJames Smart 	bool foundit = false;
696e96a22b0SJames Smart 
697e96a22b0SJames Smart 	if (!ndlp) {
698372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
699e96a22b0SJames Smart 				"6049 NVMEx LS REQ Abort: Bad NDLP x%px DID "
700e96a22b0SJames Smart 				"x%06x, Failing LS Req\n",
701e96a22b0SJames Smart 				ndlp, ndlp ? ndlp->nlp_DID : 0);
702e96a22b0SJames Smart 		return -EINVAL;
703e96a22b0SJames Smart 	}
704e96a22b0SJames Smart 
705e96a22b0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_NVME_ABTS,
706e96a22b0SJames Smart 			 "6040 NVMEx LS REQ Abort: Issue LS_ABORT for lsreq "
707e96a22b0SJames Smart 			 "x%p rqstlen:%d rsplen:%d %pad %pad\n",
708e96a22b0SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
709e96a22b0SJames Smart 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
710e96a22b0SJames Smart 			 &pnvme_lsreq->rspdma);
711e96a22b0SJames Smart 
712e96a22b0SJames Smart 	/*
713e96a22b0SJames Smart 	 * Lock the ELS ring txcmplq and look for the wqe that matches
714e96a22b0SJames Smart 	 * this ELS. If found, issue an abort on the wqe.
715e96a22b0SJames Smart 	 */
716e96a22b0SJames Smart 	pring = phba->sli4_hba.nvmels_wq->pring;
717e96a22b0SJames Smart 	spin_lock_irq(&phba->hbalock);
718e96a22b0SJames Smart 	spin_lock(&pring->ring_lock);
719e96a22b0SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
720e96a22b0SJames Smart 		if (wqe->context2 == pnvme_lsreq) {
721e96a22b0SJames Smart 			wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
722e96a22b0SJames Smart 			foundit = true;
723e96a22b0SJames Smart 			break;
724e96a22b0SJames Smart 		}
725e96a22b0SJames Smart 	}
726e96a22b0SJames Smart 	spin_unlock(&pring->ring_lock);
727e96a22b0SJames Smart 
728e96a22b0SJames Smart 	if (foundit)
729*db7531d2SJames Smart 		lpfc_sli_issue_abort_iotag(phba, pring, wqe, NULL);
730e96a22b0SJames Smart 	spin_unlock_irq(&phba->hbalock);
731e96a22b0SJames Smart 
732e96a22b0SJames Smart 	if (foundit)
733e96a22b0SJames Smart 		return 0;
734e96a22b0SJames Smart 
735e96a22b0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_NVME_ABTS,
736e96a22b0SJames Smart 			 "6213 NVMEx LS REQ Abort: Unable to locate req x%p\n",
737e96a22b0SJames Smart 			 pnvme_lsreq);
7386b6e8963SJames Smart 	return -EINVAL;
739e96a22b0SJames Smart }
740e96a22b0SJames Smart 
7419aa09e98SJames Smart static int
7429aa09e98SJames Smart lpfc_nvme_xmt_ls_rsp(struct nvme_fc_local_port *localport,
7439aa09e98SJames Smart 		     struct nvme_fc_remote_port *remoteport,
7449aa09e98SJames Smart 		     struct nvmefc_ls_rsp *ls_rsp)
7459aa09e98SJames Smart {
7469aa09e98SJames Smart 	struct lpfc_async_xchg_ctx *axchg =
7479aa09e98SJames Smart 		container_of(ls_rsp, struct lpfc_async_xchg_ctx, ls_rsp);
7489aa09e98SJames Smart 	struct lpfc_nvme_lport *lport;
7499aa09e98SJames Smart 	int rc;
7509aa09e98SJames Smart 
7519aa09e98SJames Smart 	if (axchg->phba->pport->load_flag & FC_UNLOADING)
7529aa09e98SJames Smart 		return -ENODEV;
7539aa09e98SJames Smart 
7549aa09e98SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
7559aa09e98SJames Smart 
7569aa09e98SJames Smart 	rc = __lpfc_nvme_xmt_ls_rsp(axchg, ls_rsp, __lpfc_nvme_xmt_ls_rsp_cmp);
7579aa09e98SJames Smart 
7589aa09e98SJames Smart 	if (rc) {
7599aa09e98SJames Smart 		/*
7609aa09e98SJames Smart 		 * unless the failure is due to having already sent
7619aa09e98SJames Smart 		 * the response, an abort will be generated for the
7629aa09e98SJames Smart 		 * exchange if the rsp can't be sent.
7639aa09e98SJames Smart 		 */
7649aa09e98SJames Smart 		if (rc != -EALREADY)
7659aa09e98SJames Smart 			atomic_inc(&lport->xmt_ls_abort);
7669aa09e98SJames Smart 		return rc;
7679aa09e98SJames Smart 	}
7689aa09e98SJames Smart 
7699aa09e98SJames Smart 	return 0;
7709aa09e98SJames Smart }
7719aa09e98SJames Smart 
772e96a22b0SJames Smart /**
773e96a22b0SJames Smart  * lpfc_nvme_ls_abort - Abort a prior NVME LS request
77409d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from.
77509d99705SLee Jones  * @pnvme_rport: Transport remoteport that LS is to be sent to.
77609d99705SLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
777e96a22b0SJames Smart  *
778e96a22b0SJames Smart  * Driver registers this routine to abort a NVME LS request that is
779e96a22b0SJames Smart  * in progress (from the transports perspective).
78001649561SJames Smart  **/
78101649561SJames Smart static void
78201649561SJames Smart lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
78301649561SJames Smart 		   struct nvme_fc_remote_port *pnvme_rport,
78401649561SJames Smart 		   struct nvmefc_ls_req *pnvme_lsreq)
78501649561SJames Smart {
78601649561SJames Smart 	struct lpfc_nvme_lport *lport;
78701649561SJames Smart 	struct lpfc_vport *vport;
78801649561SJames Smart 	struct lpfc_nodelist *ndlp;
789e96a22b0SJames Smart 	int ret;
79001649561SJames Smart 
79101649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
79266a210ffSJames Smart 	if (unlikely(!lport))
79366a210ffSJames Smart 		return;
79401649561SJames Smart 	vport = lport->vport;
79501649561SJames Smart 
7963386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
7973386f4bdSJames Smart 		return;
7983386f4bdSJames Smart 
79901649561SJames Smart 	ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
80001649561SJames Smart 
801e96a22b0SJames Smart 	ret = __lpfc_nvme_ls_abort(vport, ndlp, pnvme_lsreq);
802e96a22b0SJames Smart 	if (!ret)
8034b056682SJames Smart 		atomic_inc(&lport->xmt_ls_abort);
80401649561SJames Smart }
80501649561SJames Smart 
80601649561SJames Smart /* Fix up the existing sgls for NVME IO. */
8075fd11085SJames Smart static inline void
80801649561SJames Smart lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
809c490850aSJames Smart 		       struct lpfc_io_buf *lpfc_ncmd,
81001649561SJames Smart 		       struct nvmefc_fcp_req *nCmd)
81101649561SJames Smart {
8124e565cf0SJames Smart 	struct lpfc_hba  *phba = vport->phba;
81301649561SJames Smart 	struct sli4_sge *sgl;
81401649561SJames Smart 	union lpfc_wqe128 *wqe;
81501649561SJames Smart 	uint32_t *wptr, *dptr;
81601649561SJames Smart 
81701649561SJames Smart 	/*
8184e565cf0SJames Smart 	 * Get a local pointer to the built-in wqe and correct
8194e565cf0SJames Smart 	 * the cmd size to match NVME's 96 bytes and fix
8204e565cf0SJames Smart 	 * the dma address.
8214e565cf0SJames Smart 	 */
8224e565cf0SJames Smart 
823205e8240SJames Smart 	wqe = &lpfc_ncmd->cur_iocbq.wqe;
8244e565cf0SJames Smart 
8254e565cf0SJames Smart 	/*
82601649561SJames Smart 	 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
82701649561SJames Smart 	 * match NVME.  NVME sends 96 bytes. Also, use the
82801649561SJames Smart 	 * nvme commands command and response dma addresses
82901649561SJames Smart 	 * rather than the virtual memory to ease the restore
83001649561SJames Smart 	 * operation.
83101649561SJames Smart 	 */
8320794d601SJames Smart 	sgl = lpfc_ncmd->dma_sgl;
83301649561SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
8344e565cf0SJames Smart 	if (phba->cfg_nvme_embed_cmd) {
8354e565cf0SJames Smart 		sgl->addr_hi = 0;
8364e565cf0SJames Smart 		sgl->addr_lo = 0;
8374e565cf0SJames Smart 
8384e565cf0SJames Smart 		/* Word 0-2 - NVME CMND IU (embedded payload) */
8394e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
8404e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeSize = 56;
8414e565cf0SJames Smart 		wqe->generic.bde.addrHigh = 0;
8424e565cf0SJames Smart 		wqe->generic.bde.addrLow =  64;  /* Word 16 */
8434e565cf0SJames Smart 
8445fd11085SJames Smart 		/* Word 10  - dbde is 0, wqes is 1 in template */
84501649561SJames Smart 
84601649561SJames Smart 		/*
84701649561SJames Smart 		 * Embed the payload in the last half of the WQE
84801649561SJames Smart 		 * WQE words 16-30 get the NVME CMD IU payload
84901649561SJames Smart 		 *
850b06a622fSJames Smart 		 * WQE words 16-19 get payload Words 1-4
85101649561SJames Smart 		 * WQE words 20-21 get payload Words 6-7
85201649561SJames Smart 		 * WQE words 22-29 get payload Words 16-23
85301649561SJames Smart 		 */
854b06a622fSJames Smart 		wptr = &wqe->words[16];  /* WQE ptr */
85501649561SJames Smart 		dptr = (uint32_t *)nCmd->cmdaddr;  /* payload ptr */
856b06a622fSJames Smart 		dptr++;			/* Skip Word 0 in payload */
85701649561SJames Smart 
858b06a622fSJames Smart 		*wptr++ = *dptr++;	/* Word 1 */
85901649561SJames Smart 		*wptr++ = *dptr++;	/* Word 2 */
86001649561SJames Smart 		*wptr++ = *dptr++;	/* Word 3 */
86101649561SJames Smart 		*wptr++ = *dptr++;	/* Word 4 */
86201649561SJames Smart 		dptr++;			/* Skip Word 5 in payload */
86301649561SJames Smart 		*wptr++ = *dptr++;	/* Word 6 */
86401649561SJames Smart 		*wptr++ = *dptr++;	/* Word 7 */
86501649561SJames Smart 		dptr += 8;		/* Skip Words 8-15 in payload */
86601649561SJames Smart 		*wptr++ = *dptr++;	/* Word 16 */
86701649561SJames Smart 		*wptr++ = *dptr++;	/* Word 17 */
86801649561SJames Smart 		*wptr++ = *dptr++;	/* Word 18 */
86901649561SJames Smart 		*wptr++ = *dptr++;	/* Word 19 */
87001649561SJames Smart 		*wptr++ = *dptr++;	/* Word 20 */
87101649561SJames Smart 		*wptr++ = *dptr++;	/* Word 21 */
87201649561SJames Smart 		*wptr++ = *dptr++;	/* Word 22 */
87301649561SJames Smart 		*wptr   = *dptr;	/* Word 23 */
8745fd11085SJames Smart 	} else {
8755fd11085SJames Smart 		sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->cmddma));
8765fd11085SJames Smart 		sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->cmddma));
8775fd11085SJames Smart 
8785fd11085SJames Smart 		/* Word 0-2 - NVME CMND IU Inline BDE */
8795fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
8805fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeSize = nCmd->cmdlen;
8815fd11085SJames Smart 		wqe->generic.bde.addrHigh = sgl->addr_hi;
8825fd11085SJames Smart 		wqe->generic.bde.addrLow =  sgl->addr_lo;
8835fd11085SJames Smart 
8845fd11085SJames Smart 		/* Word 10 */
8855fd11085SJames Smart 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
8865fd11085SJames Smart 		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
8875fd11085SJames Smart 	}
8885fd11085SJames Smart 
8895fd11085SJames Smart 	sgl++;
8905fd11085SJames Smart 
8915fd11085SJames Smart 	/* Setup the physical region for the FCP RSP */
8925fd11085SJames Smart 	sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
8935fd11085SJames Smart 	sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
8945fd11085SJames Smart 	sgl->word2 = le32_to_cpu(sgl->word2);
8955fd11085SJames Smart 	if (nCmd->sg_cnt)
8965fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
8975fd11085SJames Smart 	else
8985fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 1);
8995fd11085SJames Smart 	sgl->word2 = cpu_to_le32(sgl->word2);
9005fd11085SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->rsplen);
90101649561SJames Smart }
90201649561SJames Smart 
903bd2cdd5eSJames Smart 
90409d99705SLee Jones /*
90501649561SJames Smart  * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
90601649561SJames Smart  *
90701649561SJames Smart  * Driver registers this routine as it io request handler.  This
90801649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
90901649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
91001649561SJames Smart  *
91101649561SJames Smart  * Return value :
91201649561SJames Smart  *   0 - Success
91301649561SJames Smart  *   TODO: What are the failure codes.
91401649561SJames Smart  **/
91501649561SJames Smart static void
91601649561SJames Smart lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
91701649561SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
91801649561SJames Smart {
919c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd =
920c490850aSJames Smart 		(struct lpfc_io_buf *)pwqeIn->context1;
92101649561SJames Smart 	struct lpfc_vport *vport = pwqeIn->vport;
92201649561SJames Smart 	struct nvmefc_fcp_req *nCmd;
92301649561SJames Smart 	struct nvme_fc_ersp_iu *ep;
92401649561SJames Smart 	struct nvme_fc_cmd_iu *cp;
92501649561SJames Smart 	struct lpfc_nodelist *ndlp;
926bbe3012bSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
9274b056682SJames Smart 	struct lpfc_nvme_lport *lport;
928352b205aSArnd Bergmann 	uint32_t code, status, idx;
92901649561SJames Smart 	uint16_t cid, sqhd, data;
93001649561SJames Smart 	uint32_t *ptr;
931840eda96SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
932840eda96SJames Smart 	int cpu;
933840eda96SJames Smart #endif
93401649561SJames Smart 
93501649561SJames Smart 	/* Sanity check on return of outstanding command */
9364d5e789aSJames Smart 	if (!lpfc_ncmd) {
937372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9384d5e789aSJames Smart 				 "6071 Null lpfc_ncmd pointer. No "
9394d5e789aSJames Smart 				 "release, skip completion\n");
9404d5e789aSJames Smart 		return;
9414d5e789aSJames Smart 	}
9424d5e789aSJames Smart 
943c2017260SJames Smart 	/* Guard against abort handler being called at same time */
944c2017260SJames Smart 	spin_lock(&lpfc_ncmd->buf_lock);
945c2017260SJames Smart 
946c2017260SJames Smart 	if (!lpfc_ncmd->nvmeCmd) {
947c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
948372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
94932350664SJames Smart 				 "6066 Missing cmpl ptrs: lpfc_ncmd x%px, "
95032350664SJames Smart 				 "nvmeCmd x%px\n",
9510b05e9feSJames Smart 				 lpfc_ncmd, lpfc_ncmd->nvmeCmd);
9524d5e789aSJames Smart 
9534d5e789aSJames Smart 		/* Release the lpfc_ncmd regardless of the missing elements. */
9544d5e789aSJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
95501649561SJames Smart 		return;
95601649561SJames Smart 	}
95701649561SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
9584b056682SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
95966a210ffSJames Smart 
9604c47efc1SJames Smart 	idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
9614c47efc1SJames Smart 	phba->sli4_hba.hdwq[idx].nvme_cstat.io_cmpls++;
9624c47efc1SJames Smart 
9631df09449SJames Smart 	if (unlikely(status && vport->localport)) {
9644b056682SJames Smart 		lport = (struct lpfc_nvme_lport *)vport->localport->private;
9651df09449SJames Smart 		if (lport) {
9664b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
9674b056682SJames Smart 				atomic_inc(&lport->cmpl_fcp_xb);
9684b056682SJames Smart 			atomic_inc(&lport->cmpl_fcp_err);
9694b056682SJames Smart 		}
97066a210ffSJames Smart 	}
97101649561SJames Smart 
972bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
973bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
9744b056682SJames Smart 			 status, wcqe->parameter);
97501649561SJames Smart 	/*
97601649561SJames Smart 	 * Catch race where our node has transitioned, but the
97701649561SJames Smart 	 * transport is still transitioning.
97801649561SJames Smart 	 */
9790b05e9feSJames Smart 	ndlp = lpfc_ncmd->ndlp;
980307e3380SJames Smart 	if (!ndlp) {
981372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
98201649561SJames Smart 				 "6062 Ignoring NVME cmpl.  No ndlp\n");
98301649561SJames Smart 		goto out_err;
98401649561SJames Smart 	}
98501649561SJames Smart 
98601649561SJames Smart 	code = bf_get(lpfc_wcqe_c_code, wcqe);
98701649561SJames Smart 	if (code == CQE_CODE_NVME_ERSP) {
98801649561SJames Smart 		/* For this type of CQE, we need to rebuild the rsp */
98901649561SJames Smart 		ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
99001649561SJames Smart 
99101649561SJames Smart 		/*
99201649561SJames Smart 		 * Get Command Id from cmd to plug into response. This
99301649561SJames Smart 		 * code is not needed in the next NVME Transport drop.
99401649561SJames Smart 		 */
99501649561SJames Smart 		cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
99601649561SJames Smart 		cid = cp->sqe.common.command_id;
99701649561SJames Smart 
99801649561SJames Smart 		/*
99901649561SJames Smart 		 * RSN is in CQE word 2
100001649561SJames Smart 		 * SQHD is in CQE Word 3 bits 15:0
100101649561SJames Smart 		 * Cmd Specific info is in CQE Word 1
100201649561SJames Smart 		 * and in CQE Word 0 bits 15:0
100301649561SJames Smart 		 */
100401649561SJames Smart 		sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
100501649561SJames Smart 
100601649561SJames Smart 		/* Now lets build the NVME ERSP IU */
100701649561SJames Smart 		ep->iu_len = cpu_to_be16(8);
100801649561SJames Smart 		ep->rsn = wcqe->parameter;
100901649561SJames Smart 		ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
101001649561SJames Smart 		ep->rsvd12 = 0;
101101649561SJames Smart 		ptr = (uint32_t *)&ep->cqe.result.u64;
101201649561SJames Smart 		*ptr++ = wcqe->total_data_placed;
101301649561SJames Smart 		data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
101401649561SJames Smart 		*ptr = (uint32_t)data;
101501649561SJames Smart 		ep->cqe.sq_head = sqhd;
101601649561SJames Smart 		ep->cqe.sq_id =  nCmd->sqid;
101701649561SJames Smart 		ep->cqe.command_id = cid;
101801649561SJames Smart 		ep->cqe.status = 0;
101901649561SJames Smart 
102001649561SJames Smart 		lpfc_ncmd->status = IOSTAT_SUCCESS;
102101649561SJames Smart 		lpfc_ncmd->result = 0;
102201649561SJames Smart 		nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
102301649561SJames Smart 		nCmd->transferred_length = nCmd->payload_length;
102401649561SJames Smart 	} else {
10254b056682SJames Smart 		lpfc_ncmd->status = (status & LPFC_IOCB_STATUS_MASK);
1026952c303bSDick Kennedy 		lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
102701649561SJames Smart 
102801649561SJames Smart 		/* For NVME, the only failure path that results in an
102901649561SJames Smart 		 * IO error is when the adapter rejects it.  All other
103001649561SJames Smart 		 * conditions are a success case and resolved by the
103101649561SJames Smart 		 * transport.
103201649561SJames Smart 		 * IOSTAT_FCP_RSP_ERROR means:
103301649561SJames Smart 		 * 1. Length of data received doesn't match total
103401649561SJames Smart 		 *    transfer length in WQE
103501649561SJames Smart 		 * 2. If the RSP payload does NOT match these cases:
103601649561SJames Smart 		 *    a. RSP length 12/24 bytes and all zeros
103701649561SJames Smart 		 *    b. NVME ERSP
103801649561SJames Smart 		 */
103901649561SJames Smart 		switch (lpfc_ncmd->status) {
104001649561SJames Smart 		case IOSTAT_SUCCESS:
104101649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
104201649561SJames Smart 			nCmd->rcv_rsplen = 0;
104301649561SJames Smart 			nCmd->status = 0;
104401649561SJames Smart 			break;
104501649561SJames Smart 		case IOSTAT_FCP_RSP_ERROR:
104601649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
104701649561SJames Smart 			nCmd->rcv_rsplen = wcqe->parameter;
104801649561SJames Smart 			nCmd->status = 0;
104901649561SJames Smart 			/* Sanity check */
105001649561SJames Smart 			if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
105101649561SJames Smart 				break;
1052372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
105301649561SJames Smart 					 "6081 NVME Completion Protocol Error: "
105486c67379SJames Smart 					 "xri %x status x%x result x%x "
105586c67379SJames Smart 					 "placed x%x\n",
105686c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
105701649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
105801649561SJames Smart 					 wcqe->total_data_placed);
105901649561SJames Smart 			break;
1060952c303bSDick Kennedy 		case IOSTAT_LOCAL_REJECT:
1061952c303bSDick Kennedy 			/* Let fall through to set command final state. */
1062952c303bSDick Kennedy 			if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
1063952c303bSDick Kennedy 				lpfc_printf_vlog(vport, KERN_INFO,
1064952c303bSDick Kennedy 					 LOG_NVME_IOERR,
106532350664SJames Smart 					 "6032 Delay Aborted cmd x%px "
106632350664SJames Smart 					 "nvme cmd x%px, xri x%x, "
1067952c303bSDick Kennedy 					 "xb %d\n",
1068952c303bSDick Kennedy 					 lpfc_ncmd, nCmd,
1069952c303bSDick Kennedy 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
1070952c303bSDick Kennedy 					 bf_get(lpfc_wcqe_c_xb, wcqe));
1071df561f66SGustavo A. R. Silva 			fallthrough;
107201649561SJames Smart 		default:
107301649561SJames Smart out_err:
1074e3246a12SJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
107586c67379SJames Smart 					 "6072 NVME Completion Error: xri %x "
10762c013a3aSJames Smart 					 "status x%x result x%x [x%x] "
10772c013a3aSJames Smart 					 "placed x%x\n",
107886c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
107901649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
10802c013a3aSJames Smart 					 wcqe->parameter,
108101649561SJames Smart 					 wcqe->total_data_placed);
108201649561SJames Smart 			nCmd->transferred_length = 0;
108301649561SJames Smart 			nCmd->rcv_rsplen = 0;
10848e009ce8SJames Smart 			nCmd->status = NVME_SC_INTERNAL;
108501649561SJames Smart 		}
108601649561SJames Smart 	}
108701649561SJames Smart 
108801649561SJames Smart 	/* pick up SLI4 exhange busy condition */
108901649561SJames Smart 	if (bf_get(lpfc_wcqe_c_xb, wcqe))
109001649561SJames Smart 		lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
109101649561SJames Smart 	else
109201649561SJames Smart 		lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
109301649561SJames Smart 
109401649561SJames Smart 	/* Update stats and complete the IO.  There is
109501649561SJames Smart 	 * no need for dma unprep because the nvme_transport
109601649561SJames Smart 	 * owns the dma address.
109701649561SJames Smart 	 */
1098bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1099c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start) {
1100bd2cdd5eSJames Smart 		lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
11012fcbc569SJames Smart 		lpfc_ncmd->ts_data_io = ktime_get_ns();
11022fcbc569SJames Smart 		phba->ktime_last_cmd = lpfc_ncmd->ts_data_io;
11032fcbc569SJames Smart 		lpfc_io_ktime(phba, lpfc_ncmd);
1104bd2cdd5eSJames Smart 	}
1105840eda96SJames Smart 	if (unlikely(phba->hdwqstat_on & LPFC_CHECK_NVME_IO)) {
1106d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
1107840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
110863df6d63SJames Smart 		if (lpfc_ncmd->cpu != cpu)
110963df6d63SJames Smart 			lpfc_printf_vlog(vport,
111063df6d63SJames Smart 					 KERN_INFO, LOG_NVME_IOERR,
1111bd2cdd5eSJames Smart 					 "6701 CPU Check cmpl: "
1112bd2cdd5eSJames Smart 					 "cpu %d expect %d\n",
111363df6d63SJames Smart 					 cpu, lpfc_ncmd->cpu);
1114bd2cdd5eSJames Smart 	}
1115bd2cdd5eSJames Smart #endif
1116952c303bSDick Kennedy 
1117952c303bSDick Kennedy 	/* NVME targets need completion held off until the abort exchange
1118add9d6beSJames Smart 	 * completes unless the NVME Rport is getting unregistered.
1119952c303bSDick Kennedy 	 */
1120add9d6beSJames Smart 
11213fd78355SJames Smart 	if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
112291455b85SJames Smart 		freqpriv = nCmd->private;
112391455b85SJames Smart 		freqpriv->nvme_buf = NULL;
11243fd78355SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
1125c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
1126c2017260SJames Smart 		nCmd->done(nCmd);
1127c2017260SJames Smart 	} else
1128c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
112901649561SJames Smart 
1130952c303bSDick Kennedy 	/* Call release with XB=1 to queue the IO into the abort list. */
113101649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
113201649561SJames Smart }
113301649561SJames Smart 
113401649561SJames Smart 
113501649561SJames Smart /**
113601649561SJames Smart  * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
113709d99705SLee Jones  * @vport: pointer to a host virtual N_Port data structure
11383e749170SLee Jones  * @lpfc_ncmd: Pointer to lpfc scsi command
113909d99705SLee Jones  * @pnode: pointer to a node-list data structure
114009d99705SLee Jones  * @cstat: pointer to the control status structure
114101649561SJames Smart  *
114201649561SJames Smart  * Driver registers this routine as it io request handler.  This
114301649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
114401649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
114501649561SJames Smart  *
114601649561SJames Smart  * Return value :
114701649561SJames Smart  *   0 - Success
114801649561SJames Smart  *   TODO: What are the failure codes.
114901649561SJames Smart  **/
115001649561SJames Smart static int
115101649561SJames Smart lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
1152c490850aSJames Smart 		      struct lpfc_io_buf *lpfc_ncmd,
115366a210ffSJames Smart 		      struct lpfc_nodelist *pnode,
11544c47efc1SJames Smart 		      struct lpfc_fc4_ctrl_stat *cstat)
115501649561SJames Smart {
115601649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
115701649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
115801649561SJames Smart 	struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
1159205e8240SJames Smart 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
116001649561SJames Smart 	uint32_t req_len;
116101649561SJames Smart 
116201649561SJames Smart 	/*
116301649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
116401649561SJames Smart 	 * the single mapping, or neither.
116501649561SJames Smart 	 */
116601649561SJames Smart 	if (nCmd->sg_cnt) {
116701649561SJames Smart 		if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
11685fd11085SJames Smart 			/* From the iwrite template, initialize words 7 - 11 */
11695fd11085SJames Smart 			memcpy(&wqe->words[7],
11705fd11085SJames Smart 			       &lpfc_iwrite_cmd_template.words[7],
11715fd11085SJames Smart 			       sizeof(uint32_t) * 5);
11725fd11085SJames Smart 
11735fd11085SJames Smart 			/* Word 4 */
11745fd11085SJames Smart 			wqe->fcp_iwrite.total_xfer_len = nCmd->payload_length;
11755fd11085SJames Smart 
117601649561SJames Smart 			/* Word 5 */
117701649561SJames Smart 			if ((phba->cfg_nvme_enable_fb) &&
117801649561SJames Smart 			    (pnode->nlp_flag & NLP_FIRSTBURST)) {
117901649561SJames Smart 				req_len = lpfc_ncmd->nvmeCmd->payload_length;
118001649561SJames Smart 				if (req_len < pnode->nvme_fb_size)
118101649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
118201649561SJames Smart 						req_len;
118301649561SJames Smart 				else
118401649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
118501649561SJames Smart 						pnode->nvme_fb_size;
11865fd11085SJames Smart 			} else {
11875fd11085SJames Smart 				wqe->fcp_iwrite.initial_xfer_len = 0;
118801649561SJames Smart 			}
11894c47efc1SJames Smart 			cstat->output_requests++;
119001649561SJames Smart 		} else {
11915fd11085SJames Smart 			/* From the iread template, initialize words 7 - 11 */
11925fd11085SJames Smart 			memcpy(&wqe->words[7],
11935fd11085SJames Smart 			       &lpfc_iread_cmd_template.words[7],
11945fd11085SJames Smart 			       sizeof(uint32_t) * 5);
119501649561SJames Smart 
11965fd11085SJames Smart 			/* Word 4 */
11975fd11085SJames Smart 			wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
119801649561SJames Smart 
11995fd11085SJames Smart 			/* Word 5 */
12005fd11085SJames Smart 			wqe->fcp_iread.rsrvd5 = 0;
120101649561SJames Smart 
12024c47efc1SJames Smart 			cstat->input_requests++;
120301649561SJames Smart 		}
120401649561SJames Smart 	} else {
12055fd11085SJames Smart 		/* From the icmnd template, initialize words 4 - 11 */
12065fd11085SJames Smart 		memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
12075fd11085SJames Smart 		       sizeof(uint32_t) * 8);
12084c47efc1SJames Smart 		cstat->control_requests++;
120901649561SJames Smart 	}
12100d8af096SJames Smart 
12110d8af096SJames Smart 	if (pnode->nlp_nvme_info & NLP_NVME_NSLER)
12120d8af096SJames Smart 		bf_set(wqe_erp, &wqe->generic.wqe_com, 1);
121301649561SJames Smart 	/*
121401649561SJames Smart 	 * Finish initializing those WQE fields that are independent
121501649561SJames Smart 	 * of the nvme_cmnd request_buffer
121601649561SJames Smart 	 */
121701649561SJames Smart 
12185fd11085SJames Smart 	/* Word 3 */
12195fd11085SJames Smart 	bf_set(payload_offset_len, &wqe->fcp_icmd,
12205fd11085SJames Smart 	       (nCmd->rsplen + nCmd->cmdlen));
12215fd11085SJames Smart 
122201649561SJames Smart 	/* Word 6 */
122301649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
122401649561SJames Smart 	       phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
122501649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
122601649561SJames Smart 
122701649561SJames Smart 	/* Word 8 */
122801649561SJames Smart 	wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
122901649561SJames Smart 
123001649561SJames Smart 	/* Word 9 */
123101649561SJames Smart 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
123201649561SJames Smart 
1233840a4701SJames Smart 	/* Word 10 */
1234840a4701SJames Smart 	bf_set(wqe_xchg, &wqe->fcp_iwrite.wqe_com, LPFC_NVME_XCHG);
1235840a4701SJames Smart 
1236414abe0aSJames Smart 	/* Words 13 14 15 are for PBDE support */
1237414abe0aSJames Smart 
123801649561SJames Smart 	pwqeq->vport = vport;
123901649561SJames Smart 	return 0;
124001649561SJames Smart }
124101649561SJames Smart 
124201649561SJames Smart 
124301649561SJames Smart /**
124401649561SJames Smart  * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
124509d99705SLee Jones  * @vport: pointer to a host virtual N_Port data structure
12463e749170SLee Jones  * @lpfc_ncmd: Pointer to lpfc scsi command
124701649561SJames Smart  *
124801649561SJames Smart  * Driver registers this routine as it io request handler.  This
124901649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
125001649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
125101649561SJames Smart  *
125201649561SJames Smart  * Return value :
125301649561SJames Smart  *   0 - Success
125401649561SJames Smart  *   TODO: What are the failure codes.
125501649561SJames Smart  **/
125601649561SJames Smart static int
125701649561SJames Smart lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
1258c490850aSJames Smart 		      struct lpfc_io_buf *lpfc_ncmd)
125901649561SJames Smart {
126001649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
126101649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1262205e8240SJames Smart 	union lpfc_wqe128 *wqe = &lpfc_ncmd->cur_iocbq.wqe;
12630794d601SJames Smart 	struct sli4_sge *sgl = lpfc_ncmd->dma_sgl;
1264d79c9e9dSJames Smart 	struct sli4_hybrid_sgl *sgl_xtra = NULL;
126501649561SJames Smart 	struct scatterlist *data_sg;
126601649561SJames Smart 	struct sli4_sge *first_data_sgl;
12670bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
1268d79c9e9dSJames Smart 	dma_addr_t physaddr = 0;
126901649561SJames Smart 	uint32_t num_bde = 0;
1270d79c9e9dSJames Smart 	uint32_t dma_len = 0;
127101649561SJames Smart 	uint32_t dma_offset = 0;
1272d79c9e9dSJames Smart 	int nseg, i, j;
1273d79c9e9dSJames Smart 	bool lsp_just_set = false;
127401649561SJames Smart 
127501649561SJames Smart 	/* Fix up the command and response DMA stuff. */
127601649561SJames Smart 	lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
127701649561SJames Smart 
127801649561SJames Smart 	/*
127901649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
128001649561SJames Smart 	 * the single mapping, or neither.
128101649561SJames Smart 	 */
128201649561SJames Smart 	if (nCmd->sg_cnt) {
128301649561SJames Smart 		/*
128401649561SJames Smart 		 * Jump over the cmd and rsp SGEs.  The fix routine
128501649561SJames Smart 		 * has already adjusted for this.
128601649561SJames Smart 		 */
128701649561SJames Smart 		sgl += 2;
128801649561SJames Smart 
128901649561SJames Smart 		first_data_sgl = sgl;
129001649561SJames Smart 		lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
129181e6a637SJames Smart 		if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
1292372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
129301649561SJames Smart 					"6058 Too many sg segments from "
129401649561SJames Smart 					"NVME Transport.  Max %d, "
129501649561SJames Smart 					"nvmeIO sg_cnt %d\n",
129629bfd55aSJames Smart 					phba->cfg_nvme_seg_cnt + 1,
129701649561SJames Smart 					lpfc_ncmd->seg_cnt);
129801649561SJames Smart 			lpfc_ncmd->seg_cnt = 0;
129901649561SJames Smart 			return 1;
130001649561SJames Smart 		}
130101649561SJames Smart 
130201649561SJames Smart 		/*
130301649561SJames Smart 		 * The driver established a maximum scatter-gather segment count
130401649561SJames Smart 		 * during probe that limits the number of sg elements in any
130501649561SJames Smart 		 * single nvme command.  Just run through the seg_cnt and format
130601649561SJames Smart 		 * the sge's.
130701649561SJames Smart 		 */
130801649561SJames Smart 		nseg = nCmd->sg_cnt;
130901649561SJames Smart 		data_sg = nCmd->first_sgl;
1310d79c9e9dSJames Smart 
1311d79c9e9dSJames Smart 		/* for tracking the segment boundaries */
1312d79c9e9dSJames Smart 		j = 2;
131301649561SJames Smart 		for (i = 0; i < nseg; i++) {
131401649561SJames Smart 			if (data_sg == NULL) {
1315372c187bSDick Kennedy 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
131601649561SJames Smart 						"6059 dptr err %d, nseg %d\n",
131701649561SJames Smart 						i, nseg);
131801649561SJames Smart 				lpfc_ncmd->seg_cnt = 0;
131901649561SJames Smart 				return 1;
132001649561SJames Smart 			}
1321d79c9e9dSJames Smart 
1322d79c9e9dSJames Smart 			sgl->word2 = 0;
1323d79c9e9dSJames Smart 			if ((num_bde + 1) == nseg) {
1324d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 1);
1325d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_type, sgl,
1326d79c9e9dSJames Smart 				       LPFC_SGE_TYPE_DATA);
1327d79c9e9dSJames Smart 			} else {
1328d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 0);
1329d79c9e9dSJames Smart 
1330d79c9e9dSJames Smart 				/* expand the segment */
1331d79c9e9dSJames Smart 				if (!lsp_just_set &&
1332d79c9e9dSJames Smart 				    !((j + 1) % phba->border_sge_num) &&
1333d79c9e9dSJames Smart 				    ((nseg - 1) != i)) {
1334d79c9e9dSJames Smart 					/* set LSP type */
1335d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_type, sgl,
1336d79c9e9dSJames Smart 					       LPFC_SGE_TYPE_LSP);
1337d79c9e9dSJames Smart 
1338d79c9e9dSJames Smart 					sgl_xtra = lpfc_get_sgl_per_hdwq(
1339d79c9e9dSJames Smart 							phba, lpfc_ncmd);
1340d79c9e9dSJames Smart 
1341d79c9e9dSJames Smart 					if (unlikely(!sgl_xtra)) {
1342d79c9e9dSJames Smart 						lpfc_ncmd->seg_cnt = 0;
1343d79c9e9dSJames Smart 						return 1;
1344d79c9e9dSJames Smart 					}
1345d79c9e9dSJames Smart 					sgl->addr_lo = cpu_to_le32(putPaddrLow(
1346d79c9e9dSJames Smart 						       sgl_xtra->dma_phys_sgl));
1347d79c9e9dSJames Smart 					sgl->addr_hi = cpu_to_le32(putPaddrHigh(
1348d79c9e9dSJames Smart 						       sgl_xtra->dma_phys_sgl));
1349d79c9e9dSJames Smart 
1350d79c9e9dSJames Smart 				} else {
1351d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_type, sgl,
1352d79c9e9dSJames Smart 					       LPFC_SGE_TYPE_DATA);
1353d79c9e9dSJames Smart 				}
1354d79c9e9dSJames Smart 			}
1355d79c9e9dSJames Smart 
1356d79c9e9dSJames Smart 			if (!(bf_get(lpfc_sli4_sge_type, sgl) &
1357d79c9e9dSJames Smart 				     LPFC_SGE_TYPE_LSP)) {
1358d79c9e9dSJames Smart 				if ((nseg - 1) == i)
1359d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_last, sgl, 1);
1360d79c9e9dSJames Smart 
136101649561SJames Smart 				physaddr = data_sg->dma_address;
136201649561SJames Smart 				dma_len = data_sg->length;
1363d79c9e9dSJames Smart 				sgl->addr_lo = cpu_to_le32(
1364d79c9e9dSJames Smart 							 putPaddrLow(physaddr));
1365d79c9e9dSJames Smart 				sgl->addr_hi = cpu_to_le32(
1366d79c9e9dSJames Smart 							putPaddrHigh(physaddr));
1367d79c9e9dSJames Smart 
136801649561SJames Smart 				bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
136901649561SJames Smart 				sgl->word2 = cpu_to_le32(sgl->word2);
137001649561SJames Smart 				sgl->sge_len = cpu_to_le32(dma_len);
137101649561SJames Smart 
137201649561SJames Smart 				dma_offset += dma_len;
137301649561SJames Smart 				data_sg = sg_next(data_sg);
1374d79c9e9dSJames Smart 
137501649561SJames Smart 				sgl++;
1376d79c9e9dSJames Smart 
1377d79c9e9dSJames Smart 				lsp_just_set = false;
1378d79c9e9dSJames Smart 			} else {
1379d79c9e9dSJames Smart 				sgl->word2 = cpu_to_le32(sgl->word2);
1380d79c9e9dSJames Smart 
1381d79c9e9dSJames Smart 				sgl->sge_len = cpu_to_le32(
1382d79c9e9dSJames Smart 						     phba->cfg_sg_dma_buf_size);
1383d79c9e9dSJames Smart 
1384d79c9e9dSJames Smart 				sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
1385d79c9e9dSJames Smart 				i = i - 1;
1386d79c9e9dSJames Smart 
1387d79c9e9dSJames Smart 				lsp_just_set = true;
1388d79c9e9dSJames Smart 			}
1389d79c9e9dSJames Smart 
1390d79c9e9dSJames Smart 			j++;
139101649561SJames Smart 		}
1392414abe0aSJames Smart 		if (phba->cfg_enable_pbde) {
13930bc2b7c5SJames Smart 			/* Use PBDE support for first SGL only, offset == 0 */
13940bc2b7c5SJames Smart 			/* Words 13-15 */
13950bc2b7c5SJames Smart 			bde = (struct ulp_bde64 *)
13960bc2b7c5SJames Smart 				&wqe->words[13];
13970bc2b7c5SJames Smart 			bde->addrLow = first_data_sgl->addr_lo;
13980bc2b7c5SJames Smart 			bde->addrHigh = first_data_sgl->addr_hi;
13990bc2b7c5SJames Smart 			bde->tus.f.bdeSize =
14000bc2b7c5SJames Smart 				le32_to_cpu(first_data_sgl->sge_len);
14010bc2b7c5SJames Smart 			bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
14020bc2b7c5SJames Smart 			bde->tus.w = cpu_to_le32(bde->tus.w);
1403b101eb27SJames Smart 
1404b101eb27SJames Smart 			/* Word 11 */
1405b101eb27SJames Smart 			bf_set(wqe_pbde, &wqe->generic.wqe_com, 1);
14065fd11085SJames Smart 		} else {
14075fd11085SJames Smart 			memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
14080bc2b7c5SJames Smart 			bf_set(wqe_pbde, &wqe->generic.wqe_com, 0);
14095fd11085SJames Smart 		}
14100bc2b7c5SJames Smart 
1411414abe0aSJames Smart 	} else {
14120794d601SJames Smart 		lpfc_ncmd->seg_cnt = 0;
14130794d601SJames Smart 
141401649561SJames Smart 		/* For this clause to be valid, the payload_length
141501649561SJames Smart 		 * and sg_cnt must zero.
141601649561SJames Smart 		 */
141701649561SJames Smart 		if (nCmd->payload_length != 0) {
1418372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
141901649561SJames Smart 					"6063 NVME DMA Prep Err: sg_cnt %d "
142001649561SJames Smart 					"payload_length x%x\n",
142101649561SJames Smart 					nCmd->sg_cnt, nCmd->payload_length);
142201649561SJames Smart 			return 1;
142301649561SJames Smart 		}
142401649561SJames Smart 	}
142501649561SJames Smart 	return 0;
142601649561SJames Smart }
142701649561SJames Smart 
142801649561SJames Smart /**
142901649561SJames Smart  * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
14303e749170SLee Jones  * @pnvme_lport: Pointer to the driver's local port data
14313e749170SLee Jones  * @pnvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
143201649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
14333e749170SLee Jones  * @pnvme_fcreq: IO request from nvme fc to driver.
143401649561SJames Smart  *
143501649561SJames Smart  * Driver registers this routine as it io request handler.  This
143601649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
14373e749170SLee Jones  * data structure to the rport indicated in @lpfc_nvme_rport.
143801649561SJames Smart  *
143901649561SJames Smart  * Return value :
144001649561SJames Smart  *   0 - Success
144101649561SJames Smart  *   TODO: What are the failure codes.
144201649561SJames Smart  **/
144301649561SJames Smart static int
144401649561SJames Smart lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
144501649561SJames Smart 			struct nvme_fc_remote_port *pnvme_rport,
144601649561SJames Smart 			void *hw_queue_handle,
144701649561SJames Smart 			struct nvmefc_fcp_req *pnvme_fcreq)
144801649561SJames Smart {
144901649561SJames Smart 	int ret = 0;
1450cf1a1d3eSJames Smart 	int expedite = 0;
145163df6d63SJames Smart 	int idx, cpu;
145201649561SJames Smart 	struct lpfc_nvme_lport *lport;
14534c47efc1SJames Smart 	struct lpfc_fc4_ctrl_stat *cstat;
145401649561SJames Smart 	struct lpfc_vport *vport;
145501649561SJames Smart 	struct lpfc_hba *phba;
145601649561SJames Smart 	struct lpfc_nodelist *ndlp;
1457c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
145801649561SJames Smart 	struct lpfc_nvme_rport *rport;
145901649561SJames Smart 	struct lpfc_nvme_qhandle *lpfc_queue_info;
1460c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
1461cf1a1d3eSJames Smart 	struct nvme_common_command *sqe;
1462bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1463bd2cdd5eSJames Smart 	uint64_t start = 0;
1464bd2cdd5eSJames Smart #endif
146501649561SJames Smart 
1466c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1467c3725bdcSJames Smart 	 * have timing races.
1468c3725bdcSJames Smart 	 */
146901649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1470c3725bdcSJames Smart 	if (unlikely(!lport)) {
1471c3725bdcSJames Smart 		ret = -EINVAL;
1472c3725bdcSJames Smart 		goto out_fail;
1473c3725bdcSJames Smart 	}
1474c3725bdcSJames Smart 
147501649561SJames Smart 	vport = lport->vport;
1476c3725bdcSJames Smart 
1477c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
147844c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
147944c2757bSJames Smart 				 "6117 Fail IO, NULL hw_queue_handle\n");
148044c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1481cd240071SJames Smart 		ret = -EBUSY;
1482c3725bdcSJames Smart 		goto out_fail;
1483c3725bdcSJames Smart 	}
1484c3725bdcSJames Smart 
148501649561SJames Smart 	phba = vport->phba;
148601649561SJames Smart 
14871df09449SJames Smart 	if (unlikely(vport->load_flag & FC_UNLOADING)) {
148844c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
148944c2757bSJames Smart 				 "6124 Fail IO, Driver unload\n");
149044c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
14913386f4bdSJames Smart 		ret = -ENODEV;
14923386f4bdSJames Smart 		goto out_fail;
14933386f4bdSJames Smart 	}
14943386f4bdSJames Smart 
1495c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1496c3725bdcSJames Smart 	if (unlikely(!freqpriv)) {
149744c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
149844c2757bSJames Smart 				 "6158 Fail IO, NULL request data\n");
149944c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1500c3725bdcSJames Smart 		ret = -EINVAL;
1501b7672ae6SDick Kennedy 		goto out_fail;
1502b7672ae6SDick Kennedy 	}
1503b7672ae6SDick Kennedy 
1504bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1505bd2cdd5eSJames Smart 	if (phba->ktime_on)
1506bd2cdd5eSJames Smart 		start = ktime_get_ns();
1507bd2cdd5eSJames Smart #endif
150801649561SJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
150901649561SJames Smart 	lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
151001649561SJames Smart 
151101649561SJames Smart 	/*
151201649561SJames Smart 	 * Catch race where our node has transitioned, but the
151301649561SJames Smart 	 * transport is still transitioning.
151401649561SJames Smart 	 */
151501649561SJames Smart 	ndlp = rport->ndlp;
1516307e3380SJames Smart 	if (!ndlp) {
151744c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
151832350664SJames Smart 				 "6053 Busy IO, ndlp not ready: rport x%px "
151932350664SJames Smart 				  "ndlp x%px, DID x%06x\n",
152001649561SJames Smart 				 rport, ndlp, pnvme_rport->port_id);
152144c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1522cd240071SJames Smart 		ret = -EBUSY;
152301649561SJames Smart 		goto out_fail;
152401649561SJames Smart 	}
152501649561SJames Smart 
152601649561SJames Smart 	/* The remote node has to be a mapped target or it's an error. */
152701649561SJames Smart 	if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
152801649561SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
152944c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
153044c2757bSJames Smart 				 "6036 Fail IO, DID x%06x not ready for "
1531cd240071SJames Smart 				 "IO. State x%x, Type x%x Flg x%x\n",
153244c2757bSJames Smart 				 pnvme_rport->port_id,
1533cd240071SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type,
1534e9b11083SJames Smart 				 ndlp->fc4_xpt_flags);
15354b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_bad_ndlp);
1536cd240071SJames Smart 		ret = -EBUSY;
153701649561SJames Smart 		goto out_fail;
153801649561SJames Smart 
153901649561SJames Smart 	}
154001649561SJames Smart 
1541cf1a1d3eSJames Smart 	/* Currently only NVME Keep alive commands should be expedited
1542cf1a1d3eSJames Smart 	 * if the driver runs out of a resource. These should only be
1543cf1a1d3eSJames Smart 	 * issued on the admin queue, qidx 0
1544cf1a1d3eSJames Smart 	 */
1545cf1a1d3eSJames Smart 	if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
1546cf1a1d3eSJames Smart 		sqe = &((struct nvme_fc_cmd_iu *)
1547cf1a1d3eSJames Smart 			pnvme_fcreq->cmdaddr)->sqe.common;
1548cf1a1d3eSJames Smart 		if (sqe->opcode == nvme_admin_keep_alive)
1549cf1a1d3eSJames Smart 			expedite = 1;
1550cf1a1d3eSJames Smart 	}
1551cf1a1d3eSJames Smart 
155201649561SJames Smart 	/* The node is shared with FCP IO, make sure the IO pending count does
155301649561SJames Smart 	 * not exceed the programmed depth.
155401649561SJames Smart 	 */
15552a5b7d62SJames Smart 	if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1556cf1a1d3eSJames Smart 		if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
1557cf1a1d3eSJames Smart 		    !expedite) {
155844c2757bSJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
155944c2757bSJames Smart 					 "6174 Fail IO, ndlp qdepth exceeded: "
15604d5e789aSJames Smart 					 "idx %d DID %x pend %d qdepth %d\n",
15614d5e789aSJames Smart 					 lpfc_queue_info->index, ndlp->nlp_DID,
15624d5e789aSJames Smart 					 atomic_read(&ndlp->cmd_pending),
15634d5e789aSJames Smart 					 ndlp->cmd_qdepth);
15644b056682SJames Smart 			atomic_inc(&lport->xmt_fcp_qdepth);
1565cd22d605SDick Kennedy 			ret = -EBUSY;
156601649561SJames Smart 			goto out_fail;
156701649561SJames Smart 		}
15682a5b7d62SJames Smart 	}
156901649561SJames Smart 
15706a828b0fSJames Smart 	/* Lookup Hardware Queue index based on fcp_io_sched module parameter */
157145aa312eSJames Smart 	if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
157245aa312eSJames Smart 		idx = lpfc_queue_info->index;
157345aa312eSJames Smart 	} else {
1574d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
15756a828b0fSJames Smart 		idx = phba->sli4_hba.cpu_map[cpu].hdwq;
157645aa312eSJames Smart 	}
157745aa312eSJames Smart 
157845aa312eSJames Smart 	lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, idx, expedite);
157901649561SJames Smart 	if (lpfc_ncmd == NULL) {
15804b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_noxri);
158101649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
158244c2757bSJames Smart 				 "6065 Fail IO, driver buffer pool is empty: "
158344c2757bSJames Smart 				 "idx %d DID %x\n",
158444c2757bSJames Smart 				 lpfc_queue_info->index, ndlp->nlp_DID);
1585cd22d605SDick Kennedy 		ret = -EBUSY;
158601649561SJames Smart 		goto out_fail;
158701649561SJames Smart 	}
1588bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1589c8a4ce0bSDick Kennedy 	if (start) {
1590bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_start = start;
1591bd2cdd5eSJames Smart 		lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
1592c8a4ce0bSDick Kennedy 	} else {
1593c8a4ce0bSDick Kennedy 		lpfc_ncmd->ts_cmd_start = 0;
1594bd2cdd5eSJames Smart 	}
1595bd2cdd5eSJames Smart #endif
159601649561SJames Smart 
159701649561SJames Smart 	/*
159801649561SJames Smart 	 * Store the data needed by the driver to issue, abort, and complete
159901649561SJames Smart 	 * an IO.
160001649561SJames Smart 	 * Do not let the IO hang out forever.  There is no midlayer issuing
160101649561SJames Smart 	 * an abort so inform the FW of the maximum IO pending time.
160201649561SJames Smart 	 */
1603bbe3012bSJames Smart 	freqpriv->nvme_buf = lpfc_ncmd;
160401649561SJames Smart 	lpfc_ncmd->nvmeCmd = pnvme_fcreq;
1605318083adSJames Smart 	lpfc_ncmd->ndlp = ndlp;
16060794d601SJames Smart 	lpfc_ncmd->qidx = lpfc_queue_info->qidx;
160701649561SJames Smart 
160801649561SJames Smart 	/*
160901649561SJames Smart 	 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
161001649561SJames Smart 	 * This identfier was create in our hardware queue create callback
161101649561SJames Smart 	 * routine. The driver now is dependent on the IO queue steering from
161201649561SJames Smart 	 * the transport.  We are trusting the upper NVME layers know which
161301649561SJames Smart 	 * index to use and that they have affinitized a CPU to this hardware
161401649561SJames Smart 	 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
161501649561SJames Smart 	 */
161666a210ffSJames Smart 	lpfc_ncmd->cur_iocbq.hba_wqidx = idx;
16174c47efc1SJames Smart 	cstat = &phba->sli4_hba.hdwq[idx].nvme_cstat;
161866a210ffSJames Smart 
161966a210ffSJames Smart 	lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp, cstat);
162066a210ffSJames Smart 	ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
162166a210ffSJames Smart 	if (ret) {
162244c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
162344c2757bSJames Smart 				 "6175 Fail IO, Prep DMA: "
162444c2757bSJames Smart 				 "idx %d DID %x\n",
162544c2757bSJames Smart 				 lpfc_queue_info->index, ndlp->nlp_DID);
162644c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
162766a210ffSJames Smart 		ret = -ENOMEM;
162866a210ffSJames Smart 		goto out_free_nvme_buf;
162966a210ffSJames Smart 	}
163066a210ffSJames Smart 
1631bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1632bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
1633bd2cdd5eSJames Smart 			 lpfc_queue_info->index, ndlp->nlp_DID);
1634bd2cdd5eSJames Smart 
16351fbf9742SJames Smart 	ret = lpfc_sli4_issue_wqe(phba, lpfc_ncmd->hdwq, &lpfc_ncmd->cur_iocbq);
163601649561SJames Smart 	if (ret) {
16374b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_wqerr);
1638e3246a12SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
163944c2757bSJames Smart 				 "6113 Fail IO, Could not issue WQE err %x "
164001649561SJames Smart 				 "sid: x%x did: x%x oxid: x%x\n",
164101649561SJames Smart 				 ret, vport->fc_myDID, ndlp->nlp_DID,
164201649561SJames Smart 				 lpfc_ncmd->cur_iocbq.sli4_xritag);
164301649561SJames Smart 		goto out_free_nvme_buf;
164401649561SJames Smart 	}
164501649561SJames Smart 
1646c490850aSJames Smart 	if (phba->cfg_xri_rebalancing)
1647c490850aSJames Smart 		lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_ncmd->hdwq_no);
1648c490850aSJames Smart 
1649bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1650c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start)
1651bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1652bd2cdd5eSJames Smart 
1653840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVME_IO) {
1654d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
1655840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
165663df6d63SJames Smart 		lpfc_ncmd->cpu = cpu;
165763df6d63SJames Smart 		if (idx != cpu)
1658bd2cdd5eSJames Smart 			lpfc_printf_vlog(vport,
165963df6d63SJames Smart 					 KERN_INFO, LOG_NVME_IOERR,
1660bd2cdd5eSJames Smart 					"6702 CPU Check cmd: "
1661bd2cdd5eSJames Smart 					"cpu %d wq %d\n",
1662bd2cdd5eSJames Smart 					lpfc_ncmd->cpu,
1663bd2cdd5eSJames Smart 					lpfc_queue_info->index);
1664bd2cdd5eSJames Smart 	}
1665bd2cdd5eSJames Smart #endif
166601649561SJames Smart 	return 0;
166701649561SJames Smart 
166801649561SJames Smart  out_free_nvme_buf:
16692cee7808SJames Smart 	if (lpfc_ncmd->nvmeCmd->sg_cnt) {
16702cee7808SJames Smart 		if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
16714c47efc1SJames Smart 			cstat->output_requests--;
16722cee7808SJames Smart 		else
16734c47efc1SJames Smart 			cstat->input_requests--;
16742cee7808SJames Smart 	} else
16754c47efc1SJames Smart 		cstat->control_requests--;
167601649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
167701649561SJames Smart  out_fail:
167801649561SJames Smart 	return ret;
167901649561SJames Smart }
168001649561SJames Smart 
168101649561SJames Smart /**
168201649561SJames Smart  * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
168301649561SJames Smart  * @phba: Pointer to HBA context object
168401649561SJames Smart  * @cmdiocb: Pointer to command iocb object.
16853e749170SLee Jones  * @abts_cmpl: Pointer to wcqe complete object.
168601649561SJames Smart  *
168701649561SJames Smart  * This is the callback function for any NVME FCP IO that was aborted.
168801649561SJames Smart  *
168901649561SJames Smart  * Return value:
169001649561SJames Smart  *   None
169101649561SJames Smart  **/
169201649561SJames Smart void
169301649561SJames Smart lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
169401649561SJames Smart 			   struct lpfc_wcqe_complete *abts_cmpl)
169501649561SJames Smart {
1696e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
169701649561SJames Smart 			"6145 ABORT_XRI_CN completing on rpi x%x "
169801649561SJames Smart 			"original iotag x%x, abort cmd iotag x%x "
169901649561SJames Smart 			"req_tag x%x, status x%x, hwstatus x%x\n",
170001649561SJames Smart 			cmdiocb->iocb.un.acxri.abortContextTag,
170101649561SJames Smart 			cmdiocb->iocb.un.acxri.abortIoTag,
170201649561SJames Smart 			cmdiocb->iotag,
170301649561SJames Smart 			bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
170401649561SJames Smart 			bf_get(lpfc_wcqe_c_status, abts_cmpl),
170501649561SJames Smart 			bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
170601649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocb);
170701649561SJames Smart }
170801649561SJames Smart 
170901649561SJames Smart /**
171001649561SJames Smart  * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
17113e749170SLee Jones  * @pnvme_lport: Pointer to the driver's local port data
17123e749170SLee Jones  * @pnvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
171301649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
17143e749170SLee Jones  * @pnvme_fcreq: IO request from nvme fc to driver.
171501649561SJames Smart  *
171601649561SJames Smart  * Driver registers this routine as its nvme request io abort handler.  This
171701649561SJames Smart  * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
171801649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.  This routine
171901649561SJames Smart  * is executed asynchronously - one the target is validated as "MAPPED" and
172001649561SJames Smart  * ready for IO, the driver issues the abort request and returns.
172101649561SJames Smart  *
172201649561SJames Smart  * Return value:
172301649561SJames Smart  *   None
172401649561SJames Smart  **/
172501649561SJames Smart static void
172601649561SJames Smart lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
172701649561SJames Smart 		    struct nvme_fc_remote_port *pnvme_rport,
172801649561SJames Smart 		    void *hw_queue_handle,
172901649561SJames Smart 		    struct nvmefc_fcp_req *pnvme_fcreq)
173001649561SJames Smart {
173101649561SJames Smart 	struct lpfc_nvme_lport *lport;
173201649561SJames Smart 	struct lpfc_vport *vport;
173301649561SJames Smart 	struct lpfc_hba *phba;
1734c490850aSJames Smart 	struct lpfc_io_buf *lpfc_nbuf;
173501649561SJames Smart 	struct lpfc_iocbq *nvmereq_wqe;
1736c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
173701649561SJames Smart 	unsigned long flags;
173801649561SJames Smart 	int ret_val;
173901649561SJames Smart 
1740c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1741c3725bdcSJames Smart 	 * have timing races.
1742c3725bdcSJames Smart 	 */
174301649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1744c3725bdcSJames Smart 	if (unlikely(!lport))
1745c3725bdcSJames Smart 		return;
174601649561SJames Smart 
1747c3725bdcSJames Smart 	vport = lport->vport;
1748c3725bdcSJames Smart 
1749c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
1750c3725bdcSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1751c3725bdcSJames Smart 				 "6129 Fail Abort, HW Queue Handle NULL.\n");
1752c3725bdcSJames Smart 		return;
1753c3725bdcSJames Smart 	}
1754c3725bdcSJames Smart 
1755c3725bdcSJames Smart 	phba = vport->phba;
1756c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1757c3725bdcSJames Smart 
1758c3725bdcSJames Smart 	if (unlikely(!freqpriv))
1759c3725bdcSJames Smart 		return;
17603386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
17613386f4bdSJames Smart 		return;
17623386f4bdSJames Smart 
176301649561SJames Smart 	/* Announce entry to new IO submit field. */
176486c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
176501649561SJames Smart 			 "6002 Abort Request to rport DID x%06x "
176632350664SJames Smart 			 "for nvme_fc_req x%px\n",
176701649561SJames Smart 			 pnvme_rport->port_id,
176801649561SJames Smart 			 pnvme_fcreq);
176901649561SJames Smart 
177001649561SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
177101649561SJames Smart 	 * cleared when the reset is complete and rings reestablished.
177201649561SJames Smart 	 */
177301649561SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
177401649561SJames Smart 	/* driver queued commands are in process of being flushed */
1775c00f62e6SJames Smart 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
177601649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1777372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
177801649561SJames Smart 				 "6139 Driver in reset cleanup - flushing "
177901649561SJames Smart 				 "NVME Req now.  hba_flag x%x\n",
178001649561SJames Smart 				 phba->hba_flag);
178101649561SJames Smart 		return;
178201649561SJames Smart 	}
178301649561SJames Smart 
1784bbe3012bSJames Smart 	lpfc_nbuf = freqpriv->nvme_buf;
178501649561SJames Smart 	if (!lpfc_nbuf) {
178601649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1787372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
178801649561SJames Smart 				 "6140 NVME IO req has no matching lpfc nvme "
178901649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
179001649561SJames Smart 		return;
179101649561SJames Smart 	} else if (!lpfc_nbuf->nvmeCmd) {
179201649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1793372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
179401649561SJames Smart 				 "6141 lpfc NVME IO req has no nvme_fcreq "
179501649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
179601649561SJames Smart 		return;
179701649561SJames Smart 	}
17982b7824d0SJames Smart 	nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
179901649561SJames Smart 
1800c2017260SJames Smart 	/* Guard against IO completion being called at same time */
1801c2017260SJames Smart 	spin_lock(&lpfc_nbuf->buf_lock);
1802c2017260SJames Smart 
180301649561SJames Smart 	/*
180401649561SJames Smart 	 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
180501649561SJames Smart 	 * state must match the nvme_fcreq passed by the nvme
180601649561SJames Smart 	 * transport.  If they don't match, it is likely the driver
180701649561SJames Smart 	 * has already completed the NVME IO and the nvme transport
180801649561SJames Smart 	 * has not seen it yet.
180901649561SJames Smart 	 */
181001649561SJames Smart 	if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
1811372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
181201649561SJames Smart 				 "6143 NVME req mismatch: "
181332350664SJames Smart 				 "lpfc_nbuf x%px nvmeCmd x%px, "
181432350664SJames Smart 				 "pnvme_fcreq x%px.  Skipping Abort xri x%x\n",
181501649561SJames Smart 				 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
18162b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1817c2017260SJames Smart 		goto out_unlock;
181801649561SJames Smart 	}
181901649561SJames Smart 
182001649561SJames Smart 	/* Don't abort IOs no longer on the pending queue. */
182101649561SJames Smart 	if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
1822372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
182332350664SJames Smart 				 "6142 NVME IO req x%px not queued - skipping "
18242b7824d0SJames Smart 				 "abort req xri x%x\n",
18252b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1826c2017260SJames Smart 		goto out_unlock;
182701649561SJames Smart 	}
182801649561SJames Smart 
18294b056682SJames Smart 	atomic_inc(&lport->xmt_fcp_abort);
1830bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1831bd2cdd5eSJames Smart 			 nvmereq_wqe->sli4_xritag,
183200cefeb9SJames Smart 			 nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
1833bd2cdd5eSJames Smart 
183401649561SJames Smart 	/* Outstanding abort is in progress */
183501649561SJames Smart 	if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
1836372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
183701649561SJames Smart 				 "6144 Outstanding NVME I/O Abort Request "
183832350664SJames Smart 				 "still pending on nvme_fcreq x%px, "
183932350664SJames Smart 				 "lpfc_ncmd %px xri x%x\n",
18402b7824d0SJames Smart 				 pnvme_fcreq, lpfc_nbuf,
18412b7824d0SJames Smart 				 nvmereq_wqe->sli4_xritag);
1842c2017260SJames Smart 		goto out_unlock;
184301649561SJames Smart 	}
184401649561SJames Smart 
1845*db7531d2SJames Smart 	ret_val = lpfc_sli4_issue_abort_iotag(phba, nvmereq_wqe,
1846*db7531d2SJames Smart 					      lpfc_nvme_abort_fcreq_cmpl);
184701649561SJames Smart 
1848c2017260SJames Smart 	spin_unlock(&lpfc_nbuf->buf_lock);
184901649561SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1850*db7531d2SJames Smart 	if (ret_val != WQE_SUCCESS) {
1851372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
185201649561SJames Smart 				 "6137 Failed abts issue_wqe with status x%x "
185332350664SJames Smart 				 "for nvme_fcreq x%px.\n",
185401649561SJames Smart 				 ret_val, pnvme_fcreq);
185501649561SJames Smart 		return;
185601649561SJames Smart 	}
185701649561SJames Smart 
185886c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
18592b7824d0SJames Smart 			 "6138 Transport Abort NVME Request Issued for "
1860*db7531d2SJames Smart 			 "ox_id x%x\n",
1861*db7531d2SJames Smart 			 nvmereq_wqe->sli4_xritag);
1862c2017260SJames Smart 	return;
1863c2017260SJames Smart 
1864c2017260SJames Smart out_unlock:
1865c2017260SJames Smart 	spin_unlock(&lpfc_nbuf->buf_lock);
1866c2017260SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1867c2017260SJames Smart 	return;
186801649561SJames Smart }
186901649561SJames Smart 
187001649561SJames Smart /* Declare and initialization an instance of the FC NVME template. */
187101649561SJames Smart static struct nvme_fc_port_template lpfc_nvme_template = {
187201649561SJames Smart 	/* initiator-based functions */
187301649561SJames Smart 	.localport_delete  = lpfc_nvme_localport_delete,
187401649561SJames Smart 	.remoteport_delete = lpfc_nvme_remoteport_delete,
187501649561SJames Smart 	.create_queue = lpfc_nvme_create_queue,
187601649561SJames Smart 	.delete_queue = lpfc_nvme_delete_queue,
187701649561SJames Smart 	.ls_req       = lpfc_nvme_ls_req,
187801649561SJames Smart 	.fcp_io       = lpfc_nvme_fcp_io_submit,
187901649561SJames Smart 	.ls_abort     = lpfc_nvme_ls_abort,
188001649561SJames Smart 	.fcp_abort    = lpfc_nvme_fcp_abort,
18819aa09e98SJames Smart 	.xmt_ls_rsp   = lpfc_nvme_xmt_ls_rsp,
188201649561SJames Smart 
188301649561SJames Smart 	.max_hw_queues = 1,
188401649561SJames Smart 	.max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
188501649561SJames Smart 	.max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
188601649561SJames Smart 	.dma_boundary = 0xFFFFFFFF,
188701649561SJames Smart 
188801649561SJames Smart 	/* Sizes of additional private data for data structures.
188901649561SJames Smart 	 * No use for the last two sizes at this time.
189001649561SJames Smart 	 */
189101649561SJames Smart 	.local_priv_sz = sizeof(struct lpfc_nvme_lport),
189201649561SJames Smart 	.remote_priv_sz = sizeof(struct lpfc_nvme_rport),
189301649561SJames Smart 	.lsrqst_priv_sz = 0,
1894bbe3012bSJames Smart 	.fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
189501649561SJames Smart };
189601649561SJames Smart 
18973e749170SLee Jones /*
18985e5b511dSJames Smart  * lpfc_get_nvme_buf - Get a nvme buffer from io_buf_list of the HBA
189901649561SJames Smart  *
19005e5b511dSJames Smart  * This routine removes a nvme buffer from head of @hdwq io_buf_list
190101649561SJames Smart  * and returns to caller.
190201649561SJames Smart  *
190301649561SJames Smart  * Return codes:
190401649561SJames Smart  *   NULL - Error
190501649561SJames Smart  *   Pointer to lpfc_nvme_buf - Success
190601649561SJames Smart  **/
1907c490850aSJames Smart static struct lpfc_io_buf *
1908cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19095e5b511dSJames Smart 		  int idx, int expedite)
191001649561SJames Smart {
1911c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
19125e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
19130794d601SJames Smart 	struct sli4_sge *sgl;
19140794d601SJames Smart 	struct lpfc_iocbq *pwqeq;
19150794d601SJames Smart 	union lpfc_wqe128 *wqe;
191601649561SJames Smart 
1917c490850aSJames Smart 	lpfc_ncmd = lpfc_get_io_buf(phba, NULL, idx, expedite);
19182a5b7d62SJames Smart 
19190794d601SJames Smart 	if (lpfc_ncmd) {
19200794d601SJames Smart 		pwqeq = &(lpfc_ncmd->cur_iocbq);
19210794d601SJames Smart 		wqe = &pwqeq->wqe;
19220794d601SJames Smart 
19230794d601SJames Smart 		/* Setup key fields in buffer that may have been changed
19240794d601SJames Smart 		 * if other protocols used this buffer.
19250794d601SJames Smart 		 */
19260794d601SJames Smart 		pwqeq->iocb_flag = LPFC_IO_NVME;
19270794d601SJames Smart 		pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
19280794d601SJames Smart 		lpfc_ncmd->start_time = jiffies;
19290794d601SJames Smart 		lpfc_ncmd->flags = 0;
19300794d601SJames Smart 
19310794d601SJames Smart 		/* Rsp SGE will be filled in when we rcv an IO
19320794d601SJames Smart 		 * from the NVME Layer to be sent.
19330794d601SJames Smart 		 * The cmd is going to be embedded so we need a SKIP SGE.
19340794d601SJames Smart 		 */
19350794d601SJames Smart 		sgl = lpfc_ncmd->dma_sgl;
19360794d601SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
19370794d601SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
19380794d601SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
19390794d601SJames Smart 		/* Fill in word 3 / sgl_len during cmd submission */
19400794d601SJames Smart 
1941d9f492a1SJames Smart 		/* Initialize 64 bytes only */
19420794d601SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
19430794d601SJames Smart 
19440794d601SJames Smart 		if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
19452a5b7d62SJames Smart 			atomic_inc(&ndlp->cmd_pending);
1946c490850aSJames Smart 			lpfc_ncmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
19472a5b7d62SJames Smart 		}
19485e5b511dSJames Smart 
1949c490850aSJames Smart 	} else {
1950c490850aSJames Smart 		qp = &phba->sli4_hba.hdwq[idx];
19515e5b511dSJames Smart 		qp->empty_io_bufs++;
1952c490850aSJames Smart 	}
19535e5b511dSJames Smart 
195401649561SJames Smart 	return  lpfc_ncmd;
195501649561SJames Smart }
195601649561SJames Smart 
195701649561SJames Smart /**
195801649561SJames Smart  * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
195901649561SJames Smart  * @phba: The Hba for which this call is being executed.
196001649561SJames Smart  * @lpfc_ncmd: The nvme buffer which is being released.
196101649561SJames Smart  *
196201649561SJames Smart  * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
19635e5b511dSJames Smart  * lpfc_io_buf_list list. For SLI4 XRI's are tied to the nvme buffer
196401649561SJames Smart  * and cannot be reused for at least RA_TOV amount of time if it was
196501649561SJames Smart  * aborted.
196601649561SJames Smart  **/
196701649561SJames Smart static void
1968c490850aSJames Smart lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd)
196901649561SJames Smart {
19705e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
197101649561SJames Smart 	unsigned long iflag = 0;
197201649561SJames Smart 
1973c490850aSJames Smart 	if ((lpfc_ncmd->flags & LPFC_SBUF_BUMP_QDEPTH) && lpfc_ncmd->ndlp)
19742a5b7d62SJames Smart 		atomic_dec(&lpfc_ncmd->ndlp->cmd_pending);
19752a5b7d62SJames Smart 
19762a5b7d62SJames Smart 	lpfc_ncmd->ndlp = NULL;
1977c490850aSJames Smart 	lpfc_ncmd->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
19782a5b7d62SJames Smart 
19791fbf9742SJames Smart 	qp = lpfc_ncmd->hdwq;
198022770cbaSJames Smart 	if (unlikely(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
198186c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
198286c67379SJames Smart 				"6310 XB release deferred for "
198386c67379SJames Smart 				"ox_id x%x on reqtag x%x\n",
198486c67379SJames Smart 				lpfc_ncmd->cur_iocbq.sli4_xritag,
198586c67379SJames Smart 				lpfc_ncmd->cur_iocbq.iotag);
198686c67379SJames Smart 
1987c00f62e6SJames Smart 		spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
198801649561SJames Smart 		list_add_tail(&lpfc_ncmd->list,
1989c00f62e6SJames Smart 			&qp->lpfc_abts_io_buf_list);
19905e5b511dSJames Smart 		qp->abts_nvme_io_bufs++;
1991c00f62e6SJames Smart 		spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
1992c490850aSJames Smart 	} else
1993c490850aSJames Smart 		lpfc_release_io_buf(phba, (struct lpfc_io_buf *)lpfc_ncmd, qp);
199401649561SJames Smart }
199501649561SJames Smart 
199601649561SJames Smart /**
199701649561SJames Smart  * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
19983e749170SLee Jones  * @vport - the lpfc_vport instance requesting a localport.
199901649561SJames Smart  *
200001649561SJames Smart  * This routine is invoked to create an nvme localport instance to bind
200101649561SJames Smart  * to the nvme_fc_transport.  It is called once during driver load
200201649561SJames Smart  * like lpfc_create_shost after all other services are initialized.
200301649561SJames Smart  * It requires a vport, vpi, and wwns at call time.  Other localport
200401649561SJames Smart  * parameters are modified as the driver's FCID and the Fabric WWN
200501649561SJames Smart  * are established.
200601649561SJames Smart  *
200701649561SJames Smart  * Return codes
200801649561SJames Smart  *      0 - successful
200901649561SJames Smart  *      -ENOMEM - no heap memory available
201001649561SJames Smart  *      other values - from nvme registration upcall
201101649561SJames Smart  **/
201201649561SJames Smart int
201301649561SJames Smart lpfc_nvme_create_localport(struct lpfc_vport *vport)
201401649561SJames Smart {
2015166d7211SJames Smart 	int ret = 0;
201601649561SJames Smart 	struct lpfc_hba  *phba = vport->phba;
201701649561SJames Smart 	struct nvme_fc_port_info nfcp_info;
201801649561SJames Smart 	struct nvme_fc_local_port *localport;
201901649561SJames Smart 	struct lpfc_nvme_lport *lport;
202001649561SJames Smart 
202101649561SJames Smart 	/* Initialize this localport instance.  The vport wwn usage ensures
202201649561SJames Smart 	 * that NPIV is accounted for.
202301649561SJames Smart 	 */
202401649561SJames Smart 	memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
202501649561SJames Smart 	nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
202601649561SJames Smart 	nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
202701649561SJames Smart 	nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
202801649561SJames Smart 
20295b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
20305b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
20315b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
20324d4c4a4aSJames Smart 	 */
20334d4c4a4aSJames Smart 	lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
203401649561SJames Smart 
2035542ddc9bSJames Smart 	/* Advertise how many hw queues we support based on cfg_hdw_queue,
2036542ddc9bSJames Smart 	 * which will not exceed cpu count.
2037542ddc9bSJames Smart 	 */
2038cdb42becSJames Smart 	lpfc_nvme_template.max_hw_queues = phba->cfg_hdw_queue;
203966a210ffSJames Smart 
2040faf5a744SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_FC))
2041faf5a744SArnd Bergmann 		return ret;
2042faf5a744SArnd Bergmann 
204301649561SJames Smart 	/* localport is allocated from the stack, but the registration
204401649561SJames Smart 	 * call allocates heap memory as well as the private area.
204501649561SJames Smart 	 */
2046faf5a744SArnd Bergmann 
204701649561SJames Smart 	ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
204801649561SJames Smart 					 &vport->phba->pcidev->dev, &localport);
204901649561SJames Smart 	if (!ret) {
205001649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
205101649561SJames Smart 				 "6005 Successfully registered local "
205232350664SJames Smart 				 "NVME port num %d, localP x%px, private "
205332350664SJames Smart 				 "x%px, sg_seg %d\n",
205401649561SJames Smart 				 localport->port_num, localport,
205501649561SJames Smart 				 localport->private,
205601649561SJames Smart 				 lpfc_nvme_template.max_sgl_segments);
205701649561SJames Smart 
205801649561SJames Smart 		/* Private is our lport size declared in the template. */
205901649561SJames Smart 		lport = (struct lpfc_nvme_lport *)localport->private;
206001649561SJames Smart 		vport->localport = localport;
206101649561SJames Smart 		lport->vport = vport;
206201649561SJames Smart 		vport->nvmei_support = 1;
20636b486ce9SDick Kennedy 
20644b056682SJames Smart 		atomic_set(&lport->xmt_fcp_noxri, 0);
20654b056682SJames Smart 		atomic_set(&lport->xmt_fcp_bad_ndlp, 0);
20664b056682SJames Smart 		atomic_set(&lport->xmt_fcp_qdepth, 0);
206744c2757bSJames Smart 		atomic_set(&lport->xmt_fcp_err, 0);
20684b056682SJames Smart 		atomic_set(&lport->xmt_fcp_wqerr, 0);
20694b056682SJames Smart 		atomic_set(&lport->xmt_fcp_abort, 0);
20704b056682SJames Smart 		atomic_set(&lport->xmt_ls_abort, 0);
20714b056682SJames Smart 		atomic_set(&lport->xmt_ls_err, 0);
20724b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_xb, 0);
20734b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_err, 0);
20744b056682SJames Smart 		atomic_set(&lport->cmpl_ls_xb, 0);
20754b056682SJames Smart 		atomic_set(&lport->cmpl_ls_err, 0);
20769aa09e98SJames Smart 
207766a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsRequests, 0);
207866a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsCmpls, 0);
20796b486ce9SDick Kennedy 	}
20803ebd9b47SJames Smart 
208101649561SJames Smart 	return ret;
208201649561SJames Smart }
208301649561SJames Smart 
2084d964b3e5SBart Van Assche #if (IS_ENABLED(CONFIG_NVME_FC))
2085add9d6beSJames Smart /* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2086add9d6beSJames Smart  *
2087add9d6beSJames Smart  * The driver has to wait for the host nvme transport to callback
2088add9d6beSJames Smart  * indicating the localport has successfully unregistered all
2089add9d6beSJames Smart  * resources.  Since this is an uninterruptible wait, loop every ten
2090add9d6beSJames Smart  * seconds and print a message indicating no progress.
2091add9d6beSJames Smart  *
2092add9d6beSJames Smart  * An uninterruptible wait is used because of the risk of transport-to-
2093add9d6beSJames Smart  * driver state mismatch.
2094add9d6beSJames Smart  */
20953999df75SBart Van Assche static void
2096add9d6beSJames Smart lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
20977961cba6SEwan D. Milne 			   struct lpfc_nvme_lport *lport,
20987961cba6SEwan D. Milne 			   struct completion *lport_unreg_cmp)
2099add9d6beSJames Smart {
2100add9d6beSJames Smart 	u32 wait_tmo;
21012ab70c21SJames Smart 	int ret, i, pending = 0;
21022ab70c21SJames Smart 	struct lpfc_sli_ring  *pring;
21032ab70c21SJames Smart 	struct lpfc_hba  *phba = vport->phba;
2104e7dab164SJames Smart 	struct lpfc_sli4_hdw_queue *qp;
2105e7dab164SJames Smart 	int abts_scsi, abts_nvme;
2106add9d6beSJames Smart 
2107add9d6beSJames Smart 	/* Host transport has to clean up and confirm requiring an indefinite
2108add9d6beSJames Smart 	 * wait. Print a message if a 10 second wait expires and renew the
2109add9d6beSJames Smart 	 * wait. This is unexpected.
2110add9d6beSJames Smart 	 */
2111add9d6beSJames Smart 	wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2112add9d6beSJames Smart 	while (true) {
21137961cba6SEwan D. Milne 		ret = wait_for_completion_timeout(lport_unreg_cmp, wait_tmo);
2114add9d6beSJames Smart 		if (unlikely(!ret)) {
21152ab70c21SJames Smart 			pending = 0;
2116e7dab164SJames Smart 			abts_scsi = 0;
2117e7dab164SJames Smart 			abts_nvme = 0;
21182ab70c21SJames Smart 			for (i = 0; i < phba->cfg_hdw_queue; i++) {
2119e7dab164SJames Smart 				qp = &phba->sli4_hba.hdwq[i];
2120e7dab164SJames Smart 				pring = qp->io_wq->pring;
21212ab70c21SJames Smart 				if (!pring)
21222ab70c21SJames Smart 					continue;
21232ab70c21SJames Smart 				pending += pring->txcmplq_cnt;
2124e7dab164SJames Smart 				abts_scsi += qp->abts_scsi_io_bufs;
2125e7dab164SJames Smart 				abts_nvme += qp->abts_nvme_io_bufs;
21262ab70c21SJames Smart 			}
2127372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
212832350664SJames Smart 					 "6176 Lport x%px Localport x%px wait "
2129e7dab164SJames Smart 					 "timed out. Pending %d [%d:%d]. "
2130e7dab164SJames Smart 					 "Renewing.\n",
2131e7dab164SJames Smart 					 lport, vport->localport, pending,
2132e7dab164SJames Smart 					 abts_scsi, abts_nvme);
2133add9d6beSJames Smart 			continue;
2134add9d6beSJames Smart 		}
2135add9d6beSJames Smart 		break;
2136add9d6beSJames Smart 	}
2137add9d6beSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
213832350664SJames Smart 			 "6177 Lport x%px Localport x%px Complete Success\n",
2139add9d6beSJames Smart 			 lport, vport->localport);
2140add9d6beSJames Smart }
2141d964b3e5SBart Van Assche #endif
2142add9d6beSJames Smart 
214301649561SJames Smart /**
214401649561SJames Smart  * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
21453e749170SLee Jones  * @vport: pointer to a host virtual N_Port data structure
214601649561SJames Smart  *
214701649561SJames Smart  * This routine is invoked to destroy all lports bound to the phba.
214801649561SJames Smart  * The lport memory was allocated by the nvme fc transport and is
214901649561SJames Smart  * released there.  This routine ensures all rports bound to the
215001649561SJames Smart  * lport have been disconnected.
215101649561SJames Smart  *
215201649561SJames Smart  **/
215301649561SJames Smart void
215401649561SJames Smart lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
215501649561SJames Smart {
21567d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
215701649561SJames Smart 	struct nvme_fc_local_port *localport;
215801649561SJames Smart 	struct lpfc_nvme_lport *lport;
215901649561SJames Smart 	int ret;
21607961cba6SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(lport_unreg_cmp);
216101649561SJames Smart 
216201649561SJames Smart 	if (vport->nvmei_support == 0)
216301649561SJames Smart 		return;
216401649561SJames Smart 
216501649561SJames Smart 	localport = vport->localport;
216601649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
216701649561SJames Smart 
216801649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
216932350664SJames Smart 			 "6011 Destroying NVME localport x%px\n",
217001649561SJames Smart 			 localport);
2171166d7211SJames Smart 
217201649561SJames Smart 	/* lport's rport list is clear.  Unregister
217301649561SJames Smart 	 * lport and release resources.
217401649561SJames Smart 	 */
21757961cba6SEwan D. Milne 	lport->lport_unreg_cmp = &lport_unreg_cmp;
217601649561SJames Smart 	ret = nvme_fc_unregister_localport(localport);
2177add9d6beSJames Smart 
2178add9d6beSJames Smart 	/* Wait for completion.  This either blocks
2179add9d6beSJames Smart 	 * indefinitely or succeeds
2180add9d6beSJames Smart 	 */
21817961cba6SEwan D. Milne 	lpfc_nvme_lport_unreg_wait(vport, lport, &lport_unreg_cmp);
21827961cba6SEwan D. Milne 	vport->localport = NULL;
218301649561SJames Smart 
218401649561SJames Smart 	/* Regardless of the unregister upcall response, clear
218501649561SJames Smart 	 * nvmei_support.  All rports are unregistered and the
218601649561SJames Smart 	 * driver will clean up.
218701649561SJames Smart 	 */
218801649561SJames Smart 	vport->nvmei_support = 0;
218901649561SJames Smart 	if (ret == 0) {
219001649561SJames Smart 		lpfc_printf_vlog(vport,
219101649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
219201649561SJames Smart 				 "6009 Unregistered lport Success\n");
219301649561SJames Smart 	} else {
219401649561SJames Smart 		lpfc_printf_vlog(vport,
219501649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
219601649561SJames Smart 				 "6010 Unregistered lport "
219701649561SJames Smart 				 "Failed, status x%x\n",
219801649561SJames Smart 				 ret);
219901649561SJames Smart 	}
2200166d7211SJames Smart #endif
220101649561SJames Smart }
220201649561SJames Smart 
220301649561SJames Smart void
220401649561SJames Smart lpfc_nvme_update_localport(struct lpfc_vport *vport)
220501649561SJames Smart {
22064410a67aSJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
220701649561SJames Smart 	struct nvme_fc_local_port *localport;
220801649561SJames Smart 	struct lpfc_nvme_lport *lport;
220901649561SJames Smart 
221001649561SJames Smart 	localport = vport->localport;
22114410a67aSJames Smart 	if (!localport) {
22124410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
22134410a67aSJames Smart 				 "6710 Update NVME fail. No localport\n");
22144410a67aSJames Smart 		return;
22154410a67aSJames Smart 	}
221601649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
22174410a67aSJames Smart 	if (!lport) {
22184410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
221932350664SJames Smart 				 "6171 Update NVME fail. localP x%px, No lport\n",
22204410a67aSJames Smart 				 localport);
22214410a67aSJames Smart 		return;
22224410a67aSJames Smart 	}
222301649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
222432350664SJames Smart 			 "6012 Update NVME lport x%px did x%x\n",
222501649561SJames Smart 			 localport, vport->fc_myDID);
222601649561SJames Smart 
222701649561SJames Smart 	localport->port_id = vport->fc_myDID;
222801649561SJames Smart 	if (localport->port_id == 0)
222901649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
223001649561SJames Smart 	else
223101649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
223201649561SJames Smart 
223301649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
223432350664SJames Smart 			 "6030 bound lport x%px to DID x%06x\n",
223501649561SJames Smart 			 lport, localport->port_id);
22364410a67aSJames Smart #endif
223701649561SJames Smart }
223801649561SJames Smart 
223901649561SJames Smart int
224001649561SJames Smart lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
224101649561SJames Smart {
22427d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
224301649561SJames Smart 	int ret = 0;
224401649561SJames Smart 	struct nvme_fc_local_port *localport;
224501649561SJames Smart 	struct lpfc_nvme_lport *lport;
224601649561SJames Smart 	struct lpfc_nvme_rport *rport;
224701466024SJames Smart 	struct lpfc_nvme_rport *oldrport;
224801649561SJames Smart 	struct nvme_fc_remote_port *remote_port;
224901649561SJames Smart 	struct nvme_fc_port_info rpinfo;
225093a3922dSJames Smart 	struct lpfc_nodelist *prev_ndlp = NULL;
22511f546823SJames Smart 	struct fc_rport *srport = ndlp->rport;
225201649561SJames Smart 
225301649561SJames Smart 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
225401649561SJames Smart 			 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
225501649561SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type);
225601649561SJames Smart 
225701649561SJames Smart 	localport = vport->localport;
2258bb6a8a2cSDick Kennedy 	if (!localport)
2259bb6a8a2cSDick Kennedy 		return 0;
2260bb6a8a2cSDick Kennedy 
226101649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
226201649561SJames Smart 
22637a06dcd3SJames Smart 	/* NVME rports are not preserved across devloss.
22647a06dcd3SJames Smart 	 * Just register this instance.  Note, rpinfo->dev_loss_tmo
22657a06dcd3SJames Smart 	 * is left 0 to indicate accept transport defaults.  The
22667a06dcd3SJames Smart 	 * driver communicates port role capabilities consistent
22677a06dcd3SJames Smart 	 * with the PRLI response data.
226801649561SJames Smart 	 */
22697a06dcd3SJames Smart 	memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
22707a06dcd3SJames Smart 	rpinfo.port_id = ndlp->nlp_DID;
227101649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET)
22727a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
227301649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
22747a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
227501649561SJames Smart 
22767a06dcd3SJames Smart 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
22777a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
22787a06dcd3SJames Smart 
22797a06dcd3SJames Smart 	rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
22807a06dcd3SJames Smart 	rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
22811f546823SJames Smart 	if (srport)
22821f546823SJames Smart 		rpinfo.dev_loss_tmo = srport->dev_loss_tmo;
22831f546823SJames Smart 	else
22841f546823SJames Smart 		rpinfo.dev_loss_tmo = vport->cfg_devloss_tmo;
228501466024SJames Smart 
2286c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
228701466024SJames Smart 	oldrport = lpfc_ndlp_get_nrport(ndlp);
228861184f17SJames Smart 	if (oldrport) {
228961184f17SJames Smart 		prev_ndlp = oldrport->ndlp;
2290c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
229161184f17SJames Smart 	} else {
2292c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
22934430f7fdSJames Smart 		if (!lpfc_nlp_get(ndlp)) {
22944430f7fdSJames Smart 			dev_warn(&vport->phba->pcidev->dev,
22954430f7fdSJames Smart 				 "Warning - No node ref - exit register\n");
22964430f7fdSJames Smart 			return 0;
22974430f7fdSJames Smart 		}
229861184f17SJames Smart 	}
22993fd78355SJames Smart 
23007a06dcd3SJames Smart 	ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
23017a06dcd3SJames Smart 	if (!ret) {
23027a06dcd3SJames Smart 		/* If the ndlp already has an nrport, this is just
23037a06dcd3SJames Smart 		 * a resume of the existing rport.  Else this is a
23047a06dcd3SJames Smart 		 * new rport.
23057a06dcd3SJames Smart 		 */
2306b15bd3e6SJames Smart 		/* Guard against an unregister/reregister
2307b15bd3e6SJames Smart 		 * race that leaves the WAIT flag set.
2308b15bd3e6SJames Smart 		 */
2309c6adba15SJames Smart 		spin_lock_irq(&ndlp->lock);
2310e9b11083SJames Smart 		ndlp->fc4_xpt_flags &= ~NLP_WAIT_FOR_UNREG;
2311e9b11083SJames Smart 		ndlp->fc4_xpt_flags |= NVME_XPT_REGD;
2312c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
23137a06dcd3SJames Smart 		rport = remote_port->private;
231401466024SJames Smart 		if (oldrport) {
23152b75d0f9SDick Kennedy 
23163fd78355SJames Smart 			/* Sever the ndlp<->rport association
23173fd78355SJames Smart 			 * before dropping the ndlp ref from
23183fd78355SJames Smart 			 * register.
23192b75d0f9SDick Kennedy 			 */
2320c6adba15SJames Smart 			spin_lock_irq(&ndlp->lock);
23212b75d0f9SDick Kennedy 			ndlp->nrport = NULL;
2322e9b11083SJames Smart 			ndlp->fc4_xpt_flags &= ~NLP_WAIT_FOR_UNREG;
2323c6adba15SJames Smart 			spin_unlock_irq(&ndlp->lock);
23242b75d0f9SDick Kennedy 			rport->ndlp = NULL;
23253fd78355SJames Smart 			rport->remoteport = NULL;
2326b04744ceSJames Smart 
2327b04744ceSJames Smart 			/* Reference only removed if previous NDLP is no longer
2328b04744ceSJames Smart 			 * active. It might be just a swap and removing the
2329b04744ceSJames Smart 			 * reference would cause a premature cleanup.
2330b04744ceSJames Smart 			 */
2331b04744ceSJames Smart 			if (prev_ndlp && prev_ndlp != ndlp) {
2332307e3380SJames Smart 				if (!prev_ndlp->nrport)
2333b04744ceSJames Smart 					lpfc_nlp_put(prev_ndlp);
2334b04744ceSJames Smart 			}
23352b75d0f9SDick Kennedy 		}
23362b75d0f9SDick Kennedy 
23372b75d0f9SDick Kennedy 		/* Clean bind the rport to the ndlp. */
233801649561SJames Smart 		rport->remoteport = remote_port;
233901649561SJames Smart 		rport->lport = lport;
23403fd78355SJames Smart 		rport->ndlp = ndlp;
2341c6adba15SJames Smart 		spin_lock_irq(&ndlp->lock);
234201649561SJames Smart 		ndlp->nrport = rport;
2343c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
234401649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO,
234501649561SJames Smart 				 LOG_NVME_DISC | LOG_NODE,
23466825b7bdSJames Smart 				 "6022 Bind lport x%px to remoteport x%px "
23476825b7bdSJames Smart 				 "rport x%px WWNN 0x%llx, "
23487a06dcd3SJames Smart 				 "Rport WWPN 0x%llx DID "
234932350664SJames Smart 				 "x%06x Role x%x, ndlp %p prev_ndlp x%px\n",
235093a3922dSJames Smart 				 lport, remote_port, rport,
235101649561SJames Smart 				 rpinfo.node_name, rpinfo.port_name,
23523fd78355SJames Smart 				 rpinfo.port_id, rpinfo.port_role,
235393a3922dSJames Smart 				 ndlp, prev_ndlp);
235401649561SJames Smart 	} else {
235501649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR,
2356372c187bSDick Kennedy 				 LOG_TRACE_EVENT,
235701649561SJames Smart 				 "6031 RemotePort Registration failed "
235801649561SJames Smart 				 "err: %d, DID x%06x\n",
235901649561SJames Smart 				 ret, ndlp->nlp_DID);
236001649561SJames Smart 	}
23617a06dcd3SJames Smart 
236201649561SJames Smart 	return ret;
2363166d7211SJames Smart #else
2364166d7211SJames Smart 	return 0;
2365166d7211SJames Smart #endif
236601649561SJames Smart }
236701649561SJames Smart 
23683e749170SLee Jones /*
23696f2589f4SJames Smart  * lpfc_nvme_rescan_port - Check to see if we should rescan this remoteport
23706f2589f4SJames Smart  *
23716f2589f4SJames Smart  * If the ndlp represents an NVME Target, that we are logged into,
23726f2589f4SJames Smart  * ping the NVME FC Transport layer to initiate a device rescan
23736f2589f4SJames Smart  * on this remote NPort.
23746f2589f4SJames Smart  */
23756f2589f4SJames Smart void
23766f2589f4SJames Smart lpfc_nvme_rescan_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
23776f2589f4SJames Smart {
23786f2589f4SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2379a6d10f24SJames Smart 	struct lpfc_nvme_rport *nrport;
2380a6d10f24SJames Smart 	struct nvme_fc_remote_port *remoteport = NULL;
23816f2589f4SJames Smart 
2382c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
2383a6d10f24SJames Smart 	nrport = lpfc_ndlp_get_nrport(ndlp);
2384a6d10f24SJames Smart 	if (nrport)
2385a6d10f24SJames Smart 		remoteport = nrport->remoteport;
2386c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
23876f2589f4SJames Smart 
23886f2589f4SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
23896f2589f4SJames Smart 			 "6170 Rescan NPort DID x%06x type x%x "
2390a6d10f24SJames Smart 			 "state x%x nrport x%px remoteport x%px\n",
2391a6d10f24SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type, ndlp->nlp_state,
2392a6d10f24SJames Smart 			 nrport, remoteport);
2393a6d10f24SJames Smart 
2394a6d10f24SJames Smart 	if (!nrport || !remoteport)
2395a6d10f24SJames Smart 		goto rescan_exit;
23966f2589f4SJames Smart 
23976f2589f4SJames Smart 	/* Only rescan if we are an NVME target in the MAPPED state */
23986f2589f4SJames Smart 	if (remoteport->port_role & FC_PORT_ROLE_NVME_DISCOVERY &&
23996f2589f4SJames Smart 	    ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
24006f2589f4SJames Smart 		nvme_fc_rescan_remoteport(remoteport);
24016f2589f4SJames Smart 
2402372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
24036f2589f4SJames Smart 				 "6172 NVME rescanned DID x%06x "
24046f2589f4SJames Smart 				 "port_state x%x\n",
24056f2589f4SJames Smart 				 ndlp->nlp_DID, remoteport->port_state);
24066f2589f4SJames Smart 	}
24076f2589f4SJames Smart 	return;
2408a6d10f24SJames Smart  rescan_exit:
2409a6d10f24SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2410a6d10f24SJames Smart 			 "6169 Skip NVME Rport Rescan, NVME remoteport "
2411a6d10f24SJames Smart 			 "unregistered\n");
24126f2589f4SJames Smart #endif
24136f2589f4SJames Smart }
24146f2589f4SJames Smart 
241501649561SJames Smart /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
241601649561SJames Smart  *
241701649561SJames Smart  * There is no notion of Devloss or rport recovery from the current
241801649561SJames Smart  * nvme_transport perspective.  Loss of an rport just means IO cannot
241901649561SJames Smart  * be sent and recovery is completely up to the initator.
242001649561SJames Smart  * For now, the driver just unbinds the DID and port_role so that
242101649561SJames Smart  * no further IO can be issued.  Changes are planned for later.
242201649561SJames Smart  *
242301649561SJames Smart  * Notes - the ndlp reference count is not decremented here since
242401649561SJames Smart  * since there is no nvme_transport api for devloss.  Node ref count
242501649561SJames Smart  * is only adjusted in driver unload.
242601649561SJames Smart  */
242701649561SJames Smart void
242801649561SJames Smart lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
242901649561SJames Smart {
24307d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
243101649561SJames Smart 	int ret;
243201649561SJames Smart 	struct nvme_fc_local_port *localport;
243301649561SJames Smart 	struct lpfc_nvme_lport *lport;
243401649561SJames Smart 	struct lpfc_nvme_rport *rport;
24359e210178SJames Smart 	struct nvme_fc_remote_port *remoteport = NULL;
243601649561SJames Smart 
243701649561SJames Smart 	localport = vport->localport;
243801649561SJames Smart 
243901649561SJames Smart 	/* This is fundamental error.  The localport is always
244001649561SJames Smart 	 * available until driver unload.  Just exit.
244101649561SJames Smart 	 */
244201649561SJames Smart 	if (!localport)
244301649561SJames Smart 		return;
244401649561SJames Smart 
244501649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
244601649561SJames Smart 	if (!lport)
244701649561SJames Smart 		goto input_err;
244801649561SJames Smart 
2449c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
245001466024SJames Smart 	rport = lpfc_ndlp_get_nrport(ndlp);
24519e210178SJames Smart 	if (rport)
24529e210178SJames Smart 		remoteport = rport->remoteport;
2453c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
24549e210178SJames Smart 	if (!remoteport)
245501649561SJames Smart 		goto input_err;
245601649561SJames Smart 
245701649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
245832350664SJames Smart 			 "6033 Unreg nvme remoteport x%px, portname x%llx, "
2459e9b11083SJames Smart 			 "port_id x%06x, portstate x%x port type x%x "
2460e9b11083SJames Smart 			 "refcnt %d\n",
246101649561SJames Smart 			 remoteport, remoteport->port_name,
246201649561SJames Smart 			 remoteport->port_id, remoteport->port_state,
2463e9b11083SJames Smart 			 ndlp->nlp_type, kref_read(&ndlp->kref));
246401649561SJames Smart 
246501649561SJames Smart 	/* Sanity check ndlp type.  Only call for NVME ports. Don't
246601649561SJames Smart 	 * clear any rport state until the transport calls back.
246701649561SJames Smart 	 */
24683b5bde69SJames Smart 
24693b5bde69SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET) {
24707a06dcd3SJames Smart 		/* No concern about the role change on the nvme remoteport.
24717a06dcd3SJames Smart 		 * The transport will update it.
24727a06dcd3SJames Smart 		 */
2473e9b11083SJames Smart 		spin_lock_irq(&vport->phba->hbalock);
2474e9b11083SJames Smart 		ndlp->fc4_xpt_flags |= NLP_WAIT_FOR_UNREG;
2475e9b11083SJames Smart 		spin_unlock_irq(&vport->phba->hbalock);
24767438273fSJames Smart 
24777438273fSJames Smart 		/* Don't let the host nvme transport keep sending keep-alives
24787438273fSJames Smart 		 * on this remoteport. Vport is unloading, no recovery. The
24797438273fSJames Smart 		 * return values is ignored.  The upcall is a courtesy to the
24807438273fSJames Smart 		 * transport.
24817438273fSJames Smart 		 */
24827438273fSJames Smart 		if (vport->load_flag & FC_UNLOADING)
24837438273fSJames Smart 			(void)nvme_fc_set_remoteport_devloss(remoteport, 0);
24847438273fSJames Smart 
248501649561SJames Smart 		ret = nvme_fc_unregister_remoteport(remoteport);
2486e9b11083SJames Smart 
2487e9b11083SJames Smart 		/* The driver no longer knows if the nrport memory is valid.
2488e9b11083SJames Smart 		 * because the controller teardown process has begun and
2489e9b11083SJames Smart 		 * is asynchronous.  Break the binding in the ndlp. Also
2490e9b11083SJames Smart 		 * remove the register ndlp reference to setup node release.
2491e9b11083SJames Smart 		 */
2492e9b11083SJames Smart 		ndlp->nrport = NULL;
24933fd78355SJames Smart 		lpfc_nlp_put(ndlp);
2494e9b11083SJames Smart 		if (ret != 0) {
2495372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
249601649561SJames Smart 					 "6167 NVME unregister failed %d "
249701649561SJames Smart 					 "port_state x%x\n",
249801649561SJames Smart 					 ret, remoteport->port_state);
24993fd78355SJames Smart 		}
250001649561SJames Smart 	}
250101649561SJames Smart 	return;
250201649561SJames Smart 
250301649561SJames Smart  input_err:
2504166d7211SJames Smart #endif
2505372c187bSDick Kennedy 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
250632350664SJames Smart 			 "6168 State error: lport x%px, rport x%px FCID x%06x\n",
250701649561SJames Smart 			 vport->localport, ndlp->rport, ndlp->nlp_DID);
250801649561SJames Smart }
2509318083adSJames Smart 
2510318083adSJames Smart /**
2511318083adSJames Smart  * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2512318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
2513318083adSJames Smart  * @axri: pointer to the fcp xri abort wcqe structure.
2514c00f62e6SJames Smart  * @lpfc_ncmd: The nvme job structure for the request being aborted.
2515318083adSJames Smart  *
2516318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
2517952c303bSDick Kennedy  * NVME aborted xri.  Aborted NVME IO commands are completed to the transport
2518952c303bSDick Kennedy  * here.
2519318083adSJames Smart  **/
2520318083adSJames Smart void
2521318083adSJames Smart lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2522c00f62e6SJames Smart 			   struct sli4_wcqe_xri_aborted *axri,
2523c00f62e6SJames Smart 			   struct lpfc_io_buf *lpfc_ncmd)
2524318083adSJames Smart {
2525318083adSJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
2526952c303bSDick Kennedy 	struct nvmefc_fcp_req *nvme_cmd = NULL;
2527c00f62e6SJames Smart 	struct lpfc_nodelist *ndlp = lpfc_ncmd->ndlp;
2528318083adSJames Smart 
2529318083adSJames Smart 
2530952c303bSDick Kennedy 	if (ndlp)
2531318083adSJames Smart 		lpfc_sli4_abts_err_handler(phba, ndlp, axri);
253286c67379SJames Smart 
253386c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2534c00f62e6SJames Smart 			"6311 nvme_cmd %p xri x%x tag x%x abort complete and "
2535c00f62e6SJames Smart 			"xri released\n",
2536952c303bSDick Kennedy 			lpfc_ncmd->nvmeCmd, xri,
2537952c303bSDick Kennedy 			lpfc_ncmd->cur_iocbq.iotag);
253886c67379SJames Smart 
2539952c303bSDick Kennedy 	/* Aborted NVME commands are required to not complete
2540952c303bSDick Kennedy 	 * before the abort exchange command fully completes.
2541952c303bSDick Kennedy 	 * Once completed, it is available via the put list.
2542952c303bSDick Kennedy 	 */
25433fd78355SJames Smart 	if (lpfc_ncmd->nvmeCmd) {
2544952c303bSDick Kennedy 		nvme_cmd = lpfc_ncmd->nvmeCmd;
2545952c303bSDick Kennedy 		nvme_cmd->done(nvme_cmd);
25463fd78355SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
25473fd78355SJames Smart 	}
2548318083adSJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
2549318083adSJames Smart }
2550c3725bdcSJames Smart 
2551c3725bdcSJames Smart /**
2552c3725bdcSJames Smart  * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2553c3725bdcSJames Smart  * @phba: Pointer to HBA context object.
2554c3725bdcSJames Smart  *
2555c3725bdcSJames Smart  * This function flushes all wqes in the nvme rings and frees all resources
2556c3725bdcSJames Smart  * in the txcmplq. This function does not issue abort wqes for the IO
2557c3725bdcSJames Smart  * commands in txcmplq, they will just be returned with
2558c3725bdcSJames Smart  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2559c3725bdcSJames Smart  * slot has been permanently disabled.
2560c3725bdcSJames Smart  **/
2561c3725bdcSJames Smart void
2562c3725bdcSJames Smart lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2563c3725bdcSJames Smart {
2564c3725bdcSJames Smart 	struct lpfc_sli_ring  *pring;
2565c3725bdcSJames Smart 	u32 i, wait_cnt = 0;
2566c3725bdcSJames Smart 
2567cdb42becSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4 || !phba->sli4_hba.hdwq)
2568c3725bdcSJames Smart 		return;
2569c3725bdcSJames Smart 
2570c00f62e6SJames Smart 	/* Cycle through all IO rings and make sure all outstanding
2571c3725bdcSJames Smart 	 * WQEs have been removed from the txcmplqs.
2572c3725bdcSJames Smart 	 */
2573cdb42becSJames Smart 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
2574c00f62e6SJames Smart 		if (!phba->sli4_hba.hdwq[i].io_wq)
25756a828b0fSJames Smart 			continue;
2576c00f62e6SJames Smart 		pring = phba->sli4_hba.hdwq[i].io_wq->pring;
2577c3725bdcSJames Smart 
2578d580c613SJames Smart 		if (!pring)
2579d580c613SJames Smart 			continue;
2580d580c613SJames Smart 
2581c3725bdcSJames Smart 		/* Retrieve everything on the txcmplq */
2582c3725bdcSJames Smart 		while (!list_empty(&pring->txcmplq)) {
2583c3725bdcSJames Smart 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
2584c3725bdcSJames Smart 			wait_cnt++;
2585c3725bdcSJames Smart 
2586c3725bdcSJames Smart 			/* The sleep is 10mS.  Every ten seconds,
2587c3725bdcSJames Smart 			 * dump a message.  Something is wrong.
2588c3725bdcSJames Smart 			 */
2589c3725bdcSJames Smart 			if ((wait_cnt % 1000) == 0) {
2590372c187bSDick Kennedy 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2591c3725bdcSJames Smart 						"6178 NVME IO not empty, "
2592c3725bdcSJames Smart 						"cnt %d\n", wait_cnt);
2593c3725bdcSJames Smart 			}
2594c3725bdcSJames Smart 		}
2595c3725bdcSJames Smart 	}
2596c3725bdcSJames Smart }
259784f2ddf8SJames Smart 
259884f2ddf8SJames Smart void
259984f2ddf8SJames Smart lpfc_nvme_cancel_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn)
260084f2ddf8SJames Smart {
260184f2ddf8SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
260284f2ddf8SJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
260384f2ddf8SJames Smart 	struct nvmefc_fcp_req *nCmd;
260484f2ddf8SJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
260584f2ddf8SJames Smart 
260684f2ddf8SJames Smart 	if (!pwqeIn->context1) {
260784f2ddf8SJames Smart 		lpfc_sli_release_iocbq(phba, pwqeIn);
260884f2ddf8SJames Smart 		return;
260984f2ddf8SJames Smart 	}
261084f2ddf8SJames Smart 	/* For abort iocb just return, IO iocb will do a done call */
261184f2ddf8SJames Smart 	if (bf_get(wqe_cmnd, &pwqeIn->wqe.gen_req.wqe_com) ==
261284f2ddf8SJames Smart 	    CMD_ABORT_XRI_CX) {
261384f2ddf8SJames Smart 		lpfc_sli_release_iocbq(phba, pwqeIn);
261484f2ddf8SJames Smart 		return;
261584f2ddf8SJames Smart 	}
261684f2ddf8SJames Smart 	lpfc_ncmd = (struct lpfc_io_buf *)pwqeIn->context1;
261784f2ddf8SJames Smart 
261884f2ddf8SJames Smart 	spin_lock(&lpfc_ncmd->buf_lock);
261984f2ddf8SJames Smart 	if (!lpfc_ncmd->nvmeCmd) {
262084f2ddf8SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
262184f2ddf8SJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
262284f2ddf8SJames Smart 		return;
262384f2ddf8SJames Smart 	}
262484f2ddf8SJames Smart 
262584f2ddf8SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
262684f2ddf8SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
262784f2ddf8SJames Smart 			"6194 NVME Cancel xri %x\n",
262884f2ddf8SJames Smart 			lpfc_ncmd->cur_iocbq.sli4_xritag);
262984f2ddf8SJames Smart 
263084f2ddf8SJames Smart 	nCmd->transferred_length = 0;
263184f2ddf8SJames Smart 	nCmd->rcv_rsplen = 0;
263284f2ddf8SJames Smart 	nCmd->status = NVME_SC_INTERNAL;
263384f2ddf8SJames Smart 	freqpriv = nCmd->private;
263484f2ddf8SJames Smart 	freqpriv->nvme_buf = NULL;
263584f2ddf8SJames Smart 	lpfc_ncmd->nvmeCmd = NULL;
263684f2ddf8SJames Smart 
263784f2ddf8SJames Smart 	spin_unlock(&lpfc_ncmd->buf_lock);
263884f2ddf8SJames Smart 	nCmd->done(nCmd);
263984f2ddf8SJames Smart 
264084f2ddf8SJames Smart 	/* Call release with XB=1 to queue the IO into the abort list. */
264184f2ddf8SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
264284f2ddf8SJames Smart #endif
264384f2ddf8SJames Smart }
2644