xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvme.c (revision e0d77d0f38aa60ca61b3ce6e60d64fad2aa0853d)
101649561SJames Smart /*******************************************************************
201649561SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
301649561SJames Smart  * Fibre Channel Host Bus Adapters.                                *
422871fe3SJustin Tee  * Copyright (C) 2017-2023 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
lpfc_nvme_create_queue(struct nvme_fc_local_port * pnvme_lport,unsigned int qidx,u16 qsize,void ** handle)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;
96a4691038SJames Smart 
97a4691038SJames Smart 	if (!vport || vport->load_flag & FC_UNLOADING ||
98a4691038SJames Smart 	    vport->phba->hba_flag & HBA_IOQ_FLUSH)
99a4691038SJames Smart 		return -ENODEV;
100a4691038SJames Smart 
10101649561SJames Smart 	qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
10201649561SJames Smart 	if (qhandle == NULL)
10301649561SJames Smart 		return -ENOMEM;
10401649561SJames Smart 
105d6d189ceSBart Van Assche 	qhandle->cpu_id = raw_smp_processor_id();
10601649561SJames Smart 	qhandle->qidx = qidx;
10701649561SJames Smart 	/*
10801649561SJames Smart 	 * NVME qidx == 0 is the admin queue, so both admin queue
10901649561SJames Smart 	 * and first IO queue will use MSI-X vector and associated
11001649561SJames Smart 	 * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
11101649561SJames Smart 	 */
11201649561SJames Smart 	if (qidx) {
11301649561SJames Smart 		str = "IO ";  /* IO queue */
11401649561SJames Smart 		qhandle->index = ((qidx - 1) %
1156a828b0fSJames Smart 			lpfc_nvme_template.max_hw_queues);
11601649561SJames Smart 	} else {
11701649561SJames Smart 		str = "ADM";  /* Admin queue */
11801649561SJames Smart 		qhandle->index = qidx;
11901649561SJames Smart 	}
12001649561SJames Smart 
121d58734f0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
12201649561SJames Smart 			 "6073 Binding %s HdwQueue %d  (cpu %d) to "
12332350664SJames Smart 			 "hdw_queue %d qhandle x%px\n", str,
12401649561SJames Smart 			 qidx, qhandle->cpu_id, qhandle->index, qhandle);
12501649561SJames Smart 	*handle = (void *)qhandle;
12601649561SJames Smart 	return 0;
12701649561SJames Smart }
12801649561SJames Smart 
12901649561SJames Smart /**
13001649561SJames Smart  * lpfc_nvme_delete_queue -
13109d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from
13201649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
13301649561SJames Smart  * @handle: An opaque driver handle from lpfc_nvme_create_queue
13401649561SJames Smart  *
13501649561SJames Smart  * Driver registers this routine to free
13601649561SJames Smart  * any internal data structures to bind the @qidx to its internal
13701649561SJames Smart  * IO queues.
13801649561SJames Smart  *
13901649561SJames Smart  * Return value :
14001649561SJames Smart  *   0 - Success
14101649561SJames Smart  *   TODO:  What are the failure codes.
14201649561SJames Smart  **/
14301649561SJames Smart static void
lpfc_nvme_delete_queue(struct nvme_fc_local_port * pnvme_lport,unsigned int qidx,void * handle)14401649561SJames Smart lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
14501649561SJames Smart 		       unsigned int qidx,
14601649561SJames Smart 		       void *handle)
14701649561SJames Smart {
14801649561SJames Smart 	struct lpfc_nvme_lport *lport;
14901649561SJames Smart 	struct lpfc_vport *vport;
15001649561SJames Smart 
151c3725bdcSJames Smart 	if (!pnvme_lport->private)
152c3725bdcSJames Smart 		return;
153c3725bdcSJames Smart 
15401649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
15501649561SJames Smart 	vport = lport->vport;
15601649561SJames Smart 
15701649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
15832350664SJames Smart 			"6001 ENTER.  lpfc_pnvme x%px, qidx x%x qhandle x%px\n",
15901649561SJames Smart 			lport, qidx, handle);
16001649561SJames Smart 	kfree(handle);
16101649561SJames Smart }
16201649561SJames Smart 
16301649561SJames Smart static void
lpfc_nvme_localport_delete(struct nvme_fc_local_port * localport)16401649561SJames Smart lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
16501649561SJames Smart {
16601649561SJames Smart 	struct lpfc_nvme_lport *lport = localport->private;
16701649561SJames Smart 
168add9d6beSJames Smart 	lpfc_printf_vlog(lport->vport, KERN_INFO, LOG_NVME,
16932350664SJames Smart 			 "6173 localport x%px delete complete\n",
170add9d6beSJames Smart 			 lport);
171add9d6beSJames Smart 
17201649561SJames Smart 	/* release any threads waiting for the unreg to complete */
1737961cba6SEwan D. Milne 	if (lport->vport->localport)
1747961cba6SEwan D. Milne 		complete(lport->lport_unreg_cmp);
17501649561SJames Smart }
17601649561SJames Smart 
17701649561SJames Smart /* lpfc_nvme_remoteport_delete
17801649561SJames Smart  *
17901649561SJames Smart  * @remoteport: Pointer to an nvme transport remoteport instance.
18001649561SJames Smart  *
18101649561SJames Smart  * This is a template downcall.  NVME transport calls this function
18201649561SJames Smart  * when it has completed the unregistration of a previously
18301649561SJames Smart  * registered remoteport.
18401649561SJames Smart  *
18501649561SJames Smart  * Return value :
18601649561SJames Smart  * None
18701649561SJames Smart  */
1883999df75SBart Van Assche static void
lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port * remoteport)18901649561SJames Smart lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
19001649561SJames Smart {
19101649561SJames Smart 	struct lpfc_nvme_rport *rport = remoteport->private;
19201649561SJames Smart 	struct lpfc_vport *vport;
19301649561SJames Smart 	struct lpfc_nodelist *ndlp;
194e9b11083SJames Smart 	u32 fc4_xpt_flags;
19501649561SJames Smart 
19601649561SJames Smart 	ndlp = rport->ndlp;
197e9b11083SJames Smart 	if (!ndlp) {
198f1156125SJames Smart 		pr_err("**** %s: NULL ndlp on rport x%px remoteport x%px\n",
199e9b11083SJames Smart 		       __func__, rport, remoteport);
20001649561SJames Smart 		goto rport_err;
201e9b11083SJames Smart 	}
20201649561SJames Smart 
20301649561SJames Smart 	vport = ndlp->vport;
204e9b11083SJames Smart 	if (!vport) {
205f1156125SJames Smart 		pr_err("**** %s: Null vport on ndlp x%px, ste x%x rport x%px\n",
206e9b11083SJames Smart 		       __func__, ndlp, ndlp->nlp_state, rport);
20701649561SJames Smart 		goto rport_err;
208e9b11083SJames Smart 	}
209e9b11083SJames Smart 
210e9b11083SJames Smart 	fc4_xpt_flags = NVME_XPT_REGD | SCSI_XPT_REGD;
21101649561SJames Smart 
21201649561SJames Smart 	/* Remove this rport from the lport's list - memory is owned by the
21301649561SJames Smart 	 * transport. Remove the ndlp reference for the NVME transport before
2147a06dcd3SJames Smart 	 * calling state machine to remove the node.
21501649561SJames Smart 	 */
21601649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
217982fc396SJames Smart 			 "6146 remoteport delete of remoteport x%px, ndlp x%px "
218982fc396SJames Smart 			 "DID x%x xflags x%x\n",
219982fc396SJames Smart 			 remoteport, ndlp, ndlp->nlp_DID, ndlp->fc4_xpt_flags);
220c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
221b15bd3e6SJames Smart 
222b15bd3e6SJames Smart 	/* The register rebind might have occurred before the delete
223b15bd3e6SJames Smart 	 * downcall.  Guard against this race.
224b15bd3e6SJames Smart 	 */
22506145683SJames Smart 	if (ndlp->fc4_xpt_flags & NVME_XPT_UNREG_WAIT)
22606145683SJames Smart 		ndlp->fc4_xpt_flags &= ~(NVME_XPT_UNREG_WAIT | NVME_XPT_REGD);
227e9b11083SJames Smart 
228c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
2293fd78355SJames Smart 
230e9b11083SJames Smart 	/* On a devloss timeout event, one more put is executed provided the
231dae40be7SJustin Tee 	 * NVME and SCSI rport unregister requests are complete.
2323fd78355SJames Smart 	 */
233e9b11083SJames Smart 	if (!(ndlp->fc4_xpt_flags & fc4_xpt_flags))
234e9b11083SJames Smart 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
23501649561SJames Smart 
23601649561SJames Smart  rport_err:
2373fd78355SJames Smart 	return;
23801649561SJames Smart }
23901649561SJames Smart 
2403a8070c5SJames Smart /**
2413a8070c5SJames Smart  * lpfc_nvme_handle_lsreq - Process an unsolicited NVME LS request
2423a8070c5SJames Smart  * @phba: pointer to lpfc hba data structure.
2433a8070c5SJames Smart  * @axchg: pointer to exchange context for the NVME LS request
2443a8070c5SJames Smart  *
2453a8070c5SJames Smart  * This routine is used for processing an asychronously received NVME LS
2463a8070c5SJames Smart  * request. Any remaining validation is done and the LS is then forwarded
2473a8070c5SJames Smart  * to the nvme-fc transport via nvme_fc_rcv_ls_req().
2483a8070c5SJames Smart  *
2499aa09e98SJames Smart  * The calling sequence should be: nvme_fc_rcv_ls_req() -> (processing)
2509aa09e98SJames Smart  * -> lpfc_nvme_xmt_ls_rsp/cmp -> req->done.
2519aa09e98SJames Smart  * __lpfc_nvme_xmt_ls_rsp_cmp should free the allocated axchg.
2529aa09e98SJames Smart  *
2533a8070c5SJames Smart  * Returns 0 if LS was handled and delivered to the transport
2543a8070c5SJames Smart  * Returns 1 if LS failed to be handled and should be dropped
2553a8070c5SJames Smart  */
2563a8070c5SJames Smart int
lpfc_nvme_handle_lsreq(struct lpfc_hba * phba,struct lpfc_async_xchg_ctx * axchg)2573a8070c5SJames Smart lpfc_nvme_handle_lsreq(struct lpfc_hba *phba,
2583a8070c5SJames Smart 			struct lpfc_async_xchg_ctx *axchg)
2593a8070c5SJames Smart {
2609aa09e98SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2619aa09e98SJames Smart 	struct lpfc_vport *vport;
2629aa09e98SJames Smart 	struct lpfc_nvme_rport *lpfc_rport;
2639aa09e98SJames Smart 	struct nvme_fc_remote_port *remoteport;
2649aa09e98SJames Smart 	struct lpfc_nvme_lport *lport;
2659aa09e98SJames Smart 	uint32_t *payload = axchg->payload;
2669aa09e98SJames Smart 	int rc;
2679aa09e98SJames Smart 
2689aa09e98SJames Smart 	vport = axchg->ndlp->vport;
2699aa09e98SJames Smart 	lpfc_rport = axchg->ndlp->nrport;
2709aa09e98SJames Smart 	if (!lpfc_rport)
2719aa09e98SJames Smart 		return -EINVAL;
2729aa09e98SJames Smart 
2739aa09e98SJames Smart 	remoteport = lpfc_rport->remoteport;
274a4691038SJames Smart 	if (!vport->localport ||
275a4691038SJames Smart 	    vport->phba->hba_flag & HBA_IOQ_FLUSH)
2769aa09e98SJames Smart 		return -EINVAL;
2779aa09e98SJames Smart 
2789aa09e98SJames Smart 	lport = vport->localport->private;
2799aa09e98SJames Smart 	if (!lport)
2809aa09e98SJames Smart 		return -EINVAL;
2819aa09e98SJames Smart 
2829aa09e98SJames Smart 	rc = nvme_fc_rcv_ls_req(remoteport, &axchg->ls_rsp, axchg->payload,
2839aa09e98SJames Smart 				axchg->size);
2849aa09e98SJames Smart 
2859aa09e98SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2869aa09e98SJames Smart 			"6205 NVME Unsol rcv: sz %d rc %d: %08x %08x %08x "
2879aa09e98SJames Smart 			"%08x %08x %08x\n",
2889aa09e98SJames Smart 			axchg->size, rc,
2899aa09e98SJames Smart 			*payload, *(payload+1), *(payload+2),
2909aa09e98SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
2919aa09e98SJames Smart 
2929aa09e98SJames Smart 	if (!rc)
2939aa09e98SJames Smart 		return 0;
2949aa09e98SJames Smart #endif
2953a8070c5SJames Smart 	return 1;
2963a8070c5SJames Smart }
2973a8070c5SJames Smart 
2986514b25dSJames Smart /**
2996514b25dSJames Smart  * __lpfc_nvme_ls_req_cmp - Generic completion handler for a NVME
3006514b25dSJames Smart  *        LS request.
3016514b25dSJames Smart  * @phba: Pointer to HBA context object
3026514b25dSJames Smart  * @vport: The local port that issued the LS
3036514b25dSJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3046514b25dSJames Smart  * @wcqe: Pointer to driver response CQE object.
3056514b25dSJames Smart  *
3066514b25dSJames Smart  * This function is the generic completion handler for NVME LS requests.
3076514b25dSJames Smart  * The function updates any states and statistics, calls the transport
3086514b25dSJames Smart  * ls_req done() routine, then tears down the command and buffers used
3096514b25dSJames Smart  * for the LS request.
3106514b25dSJames Smart  **/
3116514b25dSJames Smart void
__lpfc_nvme_ls_req_cmp(struct lpfc_hba * phba,struct lpfc_vport * vport,struct lpfc_iocbq * cmdwqe,struct lpfc_wcqe_complete * wcqe)3126514b25dSJames Smart __lpfc_nvme_ls_req_cmp(struct lpfc_hba *phba, struct lpfc_vport *vport,
3136514b25dSJames Smart 			struct lpfc_iocbq *cmdwqe,
3146514b25dSJames Smart 			struct lpfc_wcqe_complete *wcqe)
3156514b25dSJames Smart {
3166514b25dSJames Smart 	struct nvmefc_ls_req *pnvme_lsreq;
3176514b25dSJames Smart 	struct lpfc_dmabuf *buf_ptr;
3186514b25dSJames Smart 	struct lpfc_nodelist *ndlp;
3196a84d015SJustin Tee 	int status;
3206514b25dSJames Smart 
321d51cf5bdSJames Smart 	pnvme_lsreq = cmdwqe->context_un.nvme_lsreq;
322d51cf5bdSJames Smart 	ndlp = cmdwqe->ndlp;
323d51cf5bdSJames Smart 	buf_ptr = cmdwqe->bpl_dmabuf;
324d51cf5bdSJames Smart 
3256a84d015SJustin Tee 	status = bf_get(lpfc_wcqe_c_status, wcqe);
3266514b25dSJames Smart 
3276514b25dSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
328f1156125SJames Smart 			 "6047 NVMEx LS REQ x%px cmpl DID %x Xri: %x "
3296514b25dSJames Smart 			 "status %x reason x%x cmd:x%px lsreg:x%px bmp:x%px "
3306514b25dSJames Smart 			 "ndlp:x%px\n",
3316514b25dSJames Smart 			 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
3326514b25dSJames Smart 			 cmdwqe->sli4_xritag, status,
3336514b25dSJames Smart 			 (wcqe->parameter & 0xffff),
334d51cf5bdSJames Smart 			 cmdwqe, pnvme_lsreq, cmdwqe->bpl_dmabuf,
335d51cf5bdSJames Smart 			 ndlp);
3366514b25dSJames Smart 
3376514b25dSJames Smart 	lpfc_nvmeio_data(phba, "NVMEx LS CMPL: xri x%x stat x%x parm x%x\n",
3386514b25dSJames Smart 			 cmdwqe->sli4_xritag, status, wcqe->parameter);
3396514b25dSJames Smart 
340d51cf5bdSJames Smart 	if (buf_ptr) {
3416514b25dSJames Smart 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3426514b25dSJames Smart 		kfree(buf_ptr);
343d51cf5bdSJames Smart 		cmdwqe->bpl_dmabuf = NULL;
3446514b25dSJames Smart 	}
3456a84d015SJustin Tee 	if (pnvme_lsreq->done) {
3466a84d015SJustin Tee 		if (status != CQE_STATUS_SUCCESS)
3476a84d015SJustin Tee 			status = -ENXIO;
3486514b25dSJames Smart 		pnvme_lsreq->done(pnvme_lsreq, status);
3496a84d015SJustin Tee 	} else {
350372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3516514b25dSJames Smart 				 "6046 NVMEx cmpl without done call back? "
352f1156125SJames Smart 				 "Data x%px DID %x Xri: %x status %x\n",
3536514b25dSJames Smart 				pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
3546514b25dSJames Smart 				cmdwqe->sli4_xritag, status);
3556a84d015SJustin Tee 	}
3566514b25dSJames Smart 	if (ndlp) {
3576514b25dSJames Smart 		lpfc_nlp_put(ndlp);
358d51cf5bdSJames Smart 		cmdwqe->ndlp = NULL;
3596514b25dSJames Smart 	}
3606514b25dSJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
3616514b25dSJames Smart }
3626514b25dSJames Smart 
36301649561SJames Smart static void
lpfc_nvme_ls_req_cmp(struct lpfc_hba * phba,struct lpfc_iocbq * cmdwqe,struct lpfc_iocbq * rspwqe)3646514b25dSJames Smart lpfc_nvme_ls_req_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
365a680a929SJames Smart 		     struct lpfc_iocbq *rspwqe)
36601649561SJames Smart {
36701649561SJames Smart 	struct lpfc_vport *vport = cmdwqe->vport;
3684b056682SJames Smart 	struct lpfc_nvme_lport *lport;
36901649561SJames Smart 	uint32_t status;
370a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
37101649561SJames Smart 
3726a84d015SJustin Tee 	status = bf_get(lpfc_wcqe_c_status, wcqe);
37366a210ffSJames Smart 
37466a85155SJames Smart 	if (vport->localport) {
37566a85155SJames Smart 		lport = (struct lpfc_nvme_lport *)vport->localport->private;
37666a210ffSJames Smart 		if (lport) {
37766a210ffSJames Smart 			atomic_inc(&lport->fc4NvmeLsCmpls);
3784b056682SJames Smart 			if (status) {
3794b056682SJames Smart 				if (bf_get(lpfc_wcqe_c_xb, wcqe))
3804b056682SJames Smart 					atomic_inc(&lport->cmpl_ls_xb);
3814b056682SJames Smart 				atomic_inc(&lport->cmpl_ls_err);
3824b056682SJames Smart 			}
38366a210ffSJames Smart 		}
38466a85155SJames Smart 	}
3854b056682SJames Smart 
3866514b25dSJames Smart 	__lpfc_nvme_ls_req_cmp(phba, vport, cmdwqe, wcqe);
38701649561SJames Smart }
38801649561SJames Smart 
38901649561SJames Smart static int
lpfc_nvme_gen_req(struct lpfc_vport * vport,struct lpfc_dmabuf * bmp,struct lpfc_dmabuf * inp,struct nvmefc_ls_req * pnvme_lsreq,void (* cmpl)(struct lpfc_hba *,struct lpfc_iocbq *,struct lpfc_iocbq *),struct lpfc_nodelist * ndlp,uint32_t num_entry,uint32_t tmo,uint8_t retry)39001649561SJames Smart lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
39101649561SJames Smart 		  struct lpfc_dmabuf *inp,
39201649561SJames Smart 		  struct nvmefc_ls_req *pnvme_lsreq,
39301649561SJames Smart 		  void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
394a680a929SJames Smart 			       struct lpfc_iocbq *),
39501649561SJames Smart 		  struct lpfc_nodelist *ndlp, uint32_t num_entry,
39601649561SJames Smart 		  uint32_t tmo, uint8_t retry)
39701649561SJames Smart {
39801649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
399205e8240SJames Smart 	union lpfc_wqe128 *wqe;
40001649561SJames Smart 	struct lpfc_iocbq *genwqe;
40101649561SJames Smart 	struct ulp_bde64 *bpl;
40201649561SJames Smart 	struct ulp_bde64 bde;
40301649561SJames Smart 	int i, rc, xmit_len, first_len;
40401649561SJames Smart 
40501649561SJames Smart 	/* Allocate buffer for  command WQE */
40601649561SJames Smart 	genwqe = lpfc_sli_get_iocbq(phba);
40701649561SJames Smart 	if (genwqe == NULL)
40801649561SJames Smart 		return 1;
40901649561SJames Smart 
41001649561SJames Smart 	wqe = &genwqe->wqe;
411d9f492a1SJames Smart 	/* Initialize only 64 bytes */
41201649561SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
41301649561SJames Smart 
414d51cf5bdSJames Smart 	genwqe->bpl_dmabuf = bmp;
415a680a929SJames Smart 	genwqe->cmd_flag |= LPFC_IO_NVME_LS;
41601649561SJames Smart 
41701649561SJames Smart 	/* Save for completion so we can release these resources */
418d51cf5bdSJames Smart 	genwqe->ndlp = lpfc_nlp_get(ndlp);
419d51cf5bdSJames Smart 	if (!genwqe->ndlp) {
4204430f7fdSJames Smart 		dev_warn(&phba->pcidev->dev,
4214430f7fdSJames Smart 			 "Warning: Failed node ref, not sending LS_REQ\n");
4224430f7fdSJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
4234430f7fdSJames Smart 		return 1;
4244430f7fdSJames Smart 	}
4254430f7fdSJames Smart 
426d51cf5bdSJames Smart 	genwqe->context_un.nvme_lsreq = pnvme_lsreq;
42701649561SJames Smart 	/* Fill in payload, bp points to frame payload */
42801649561SJames Smart 
42901649561SJames Smart 	if (!tmo)
43001649561SJames Smart 		/* FC spec states we need 3 * ratov for CT requests */
43101649561SJames Smart 		tmo = (3 * phba->fc_ratov);
43201649561SJames Smart 
43301649561SJames Smart 	/* For this command calculate the xmit length of the request bde. */
43401649561SJames Smart 	xmit_len = 0;
43501649561SJames Smart 	first_len = 0;
43601649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
43701649561SJames Smart 	for (i = 0; i < num_entry; i++) {
43801649561SJames Smart 		bde.tus.w = bpl[i].tus.w;
43901649561SJames Smart 		if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
44001649561SJames Smart 			break;
44101649561SJames Smart 		xmit_len += bde.tus.f.bdeSize;
44201649561SJames Smart 		if (i == 0)
44301649561SJames Smart 			first_len = xmit_len;
44401649561SJames Smart 	}
44501649561SJames Smart 
446a680a929SJames Smart 	genwqe->num_bdes = num_entry;
44701649561SJames Smart 	genwqe->hba_wqidx = 0;
44801649561SJames Smart 
44901649561SJames Smart 	/* Words 0 - 2 */
45001649561SJames Smart 	wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
45101649561SJames Smart 	wqe->generic.bde.tus.f.bdeSize = first_len;
45201649561SJames Smart 	wqe->generic.bde.addrLow = bpl[0].addrLow;
45301649561SJames Smart 	wqe->generic.bde.addrHigh = bpl[0].addrHigh;
45401649561SJames Smart 
45501649561SJames Smart 	/* Word 3 */
45601649561SJames Smart 	wqe->gen_req.request_payload_len = first_len;
45701649561SJames Smart 
45801649561SJames Smart 	/* Word 4 */
45901649561SJames Smart 
46001649561SJames Smart 	/* Word 5 */
46101649561SJames Smart 	bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
46201649561SJames Smart 	bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
46301649561SJames Smart 	bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
4648b361639SJames Smart 	bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
46501649561SJames Smart 	bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
46601649561SJames Smart 
46701649561SJames Smart 	/* Word 6 */
46801649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
46901649561SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
47001649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
47101649561SJames Smart 
47201649561SJames Smart 	/* Word 7 */
473c33b1609SJames Smart 	bf_set(wqe_tmo, &wqe->gen_req.wqe_com, tmo);
47401649561SJames Smart 	bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
47501649561SJames Smart 	bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
47601649561SJames Smart 	bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
47701649561SJames Smart 
47801649561SJames Smart 	/* Word 8 */
47901649561SJames Smart 	wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
48001649561SJames Smart 
48101649561SJames Smart 	/* Word 9 */
48201649561SJames Smart 	bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
48301649561SJames Smart 
48401649561SJames Smart 	/* Word 10 */
48501649561SJames Smart 	bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
48601649561SJames Smart 	bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
48701649561SJames Smart 	bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
48801649561SJames Smart 	bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
48901649561SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
49001649561SJames Smart 
49101649561SJames Smart 	/* Word 11 */
49201649561SJames Smart 	bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
49301649561SJames Smart 	bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
49401649561SJames Smart 
49501649561SJames Smart 
49601649561SJames Smart 	/* Issue GEN REQ WQE for NPORT <did> */
497a680a929SJames Smart 	genwqe->cmd_cmpl = cmpl;
49801649561SJames Smart 	genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
49901649561SJames Smart 	genwqe->vport = vport;
50001649561SJames Smart 	genwqe->retry = retry;
50101649561SJames Smart 
502bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  XMIT: xri x%x iotag x%x to x%06x\n",
503bd2cdd5eSJames Smart 			 genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
504bd2cdd5eSJames Smart 
5051fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], genwqe);
506cd22d605SDick Kennedy 	if (rc) {
507372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
50801649561SJames Smart 				 "6045 Issue GEN REQ WQE to NPORT x%x "
5096514b25dSJames Smart 				 "Data: x%x x%x  rc x%x\n",
51001649561SJames Smart 				 ndlp->nlp_DID, genwqe->iotag,
5116514b25dSJames Smart 				 vport->port_state, rc);
512e9b11083SJames Smart 		lpfc_nlp_put(ndlp);
51301649561SJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
51401649561SJames Smart 		return 1;
51501649561SJames Smart 	}
5166514b25dSJames Smart 
5176514b25dSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_ELS,
5186514b25dSJames Smart 			 "6050 Issue GEN REQ WQE to NPORT x%x "
5196514b25dSJames Smart 			 "Data: oxid: x%x state: x%x wq:x%px lsreq:x%px "
5206514b25dSJames Smart 			 "bmp:x%px xmit:%d 1st:%d\n",
5216514b25dSJames Smart 			 ndlp->nlp_DID, genwqe->sli4_xritag,
5226514b25dSJames Smart 			 vport->port_state,
5236514b25dSJames Smart 			 genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
52401649561SJames Smart 	return 0;
52501649561SJames Smart }
52601649561SJames Smart 
5276514b25dSJames Smart 
52801649561SJames Smart /**
5296514b25dSJames Smart  * __lpfc_nvme_ls_req - Generic service routine to issue an NVME LS request
5306514b25dSJames Smart  * @vport: The local port issuing the LS
5316514b25dSJames Smart  * @ndlp: The remote port to send the LS to
5326514b25dSJames Smart  * @pnvme_lsreq: Pointer to LS request structure from the transport
53309d99705SLee Jones  * @gen_req_cmp: Completion call-back
53401649561SJames Smart  *
5356514b25dSJames Smart  * Routine validates the ndlp, builds buffers and sends a GEN_REQUEST
5366514b25dSJames Smart  * WQE to perform the LS operation.
53701649561SJames Smart  *
53801649561SJames Smart  * Return value :
53901649561SJames Smart  *   0 - Success
5406514b25dSJames Smart  *   non-zero: various error codes, in form of -Exxx
54101649561SJames Smart  **/
5426514b25dSJames Smart int
__lpfc_nvme_ls_req(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct nvmefc_ls_req * pnvme_lsreq,void (* gen_req_cmp)(struct lpfc_hba * phba,struct lpfc_iocbq * cmdwqe,struct lpfc_iocbq * rspwqe))5436514b25dSJames Smart __lpfc_nvme_ls_req(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5446514b25dSJames Smart 		      struct nvmefc_ls_req *pnvme_lsreq,
5456514b25dSJames Smart 		      void (*gen_req_cmp)(struct lpfc_hba *phba,
5466514b25dSJames Smart 				struct lpfc_iocbq *cmdwqe,
547a680a929SJames Smart 				struct lpfc_iocbq *rspwqe))
54801649561SJames Smart {
54901649561SJames Smart 	struct lpfc_dmabuf *bmp;
5506514b25dSJames Smart 	struct ulp_bde64 *bpl;
5516514b25dSJames Smart 	int ret;
552ba43c4d0SJames Smart 	uint16_t ntype, nstate;
55301649561SJames Smart 
554307e3380SJames Smart 	if (!ndlp) {
555372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5566514b25dSJames Smart 				 "6051 NVMEx LS REQ: Bad NDLP x%px, Failing "
5576514b25dSJames Smart 				 "LS Req\n",
5586514b25dSJames Smart 				 ndlp);
559ba43c4d0SJames Smart 		return -ENODEV;
560ba43c4d0SJames Smart 	}
561ba43c4d0SJames Smart 
562ba43c4d0SJames Smart 	ntype = ndlp->nlp_type;
563ba43c4d0SJames Smart 	nstate = ndlp->nlp_state;
564ba43c4d0SJames Smart 	if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
565ba43c4d0SJames Smart 	    (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
566372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5676514b25dSJames Smart 				 "6088 NVMEx LS REQ: Fail DID x%06x not "
5686514b25dSJames Smart 				 "ready for IO. Type x%x, State x%x\n",
5696514b25dSJames Smart 				 ndlp->nlp_DID, ntype, nstate);
570ba43c4d0SJames Smart 		return -ENODEV;
57101649561SJames Smart 	}
572a4691038SJames Smart 	if (vport->phba->hba_flag & HBA_IOQ_FLUSH)
573a4691038SJames Smart 		return -ENODEV;
5746514b25dSJames Smart 
5758c65830aSJames Smart 	if (!vport->phba->sli4_hba.nvmels_wq)
5768c65830aSJames Smart 		return -ENOMEM;
5778c65830aSJames Smart 
5786514b25dSJames Smart 	/*
5796514b25dSJames Smart 	 * there are two dma buf in the request, actually there is one and
5806514b25dSJames Smart 	 * the second one is just the start address + cmd size.
5816514b25dSJames Smart 	 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
5826514b25dSJames Smart 	 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
5836514b25dSJames Smart 	 * because the nvem layer owns the data bufs.
5846514b25dSJames Smart 	 * We do not have to break these packets open, we don't care what is
5856514b25dSJames Smart 	 * in them. And we do not have to look at the resonse data, we only
5866514b25dSJames Smart 	 * care that we got a response. All of the caring is going to happen
5876514b25dSJames Smart 	 * in the nvme-fc layer.
5886514b25dSJames Smart 	 */
5896514b25dSJames Smart 
5906514b25dSJames Smart 	bmp = kmalloc(sizeof(*bmp), GFP_KERNEL);
59101649561SJames Smart 	if (!bmp) {
592372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5936514b25dSJames Smart 				 "6044 NVMEx LS REQ: Could not alloc LS buf "
5946514b25dSJames Smart 				 "for DID %x\n",
5956514b25dSJames Smart 				 ndlp->nlp_DID);
5966514b25dSJames Smart 		return -ENOMEM;
59701649561SJames Smart 	}
5986514b25dSJames Smart 
59901649561SJames Smart 	bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
60001649561SJames Smart 	if (!bmp->virt) {
601372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6026514b25dSJames Smart 				 "6042 NVMEx LS REQ: Could not alloc mbuf "
6036514b25dSJames Smart 				 "for DID %x\n",
6046514b25dSJames Smart 				 ndlp->nlp_DID);
60501649561SJames Smart 		kfree(bmp);
6066514b25dSJames Smart 		return -ENOMEM;
60701649561SJames Smart 	}
6086514b25dSJames Smart 
6096514b25dSJames Smart 	INIT_LIST_HEAD(&bmp->list);
6106514b25dSJames Smart 
61101649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
61201649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
61301649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
61401649561SJames Smart 	bpl->tus.f.bdeFlags = 0;
61501649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
61601649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
61701649561SJames Smart 	bpl++;
61801649561SJames Smart 
61901649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
62001649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
62101649561SJames Smart 	bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
62201649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
62301649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
62401649561SJames Smart 
62501649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
6266514b25dSJames Smart 			"6149 NVMEx LS REQ: Issue to DID 0x%06x lsreq x%px, "
6276514b25dSJames Smart 			"rqstlen:%d rsplen:%d %pad %pad\n",
6286514b25dSJames Smart 			ndlp->nlp_DID, pnvme_lsreq, pnvme_lsreq->rqstlen,
629825c6abbSArnd Bergmann 			pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
630825c6abbSArnd Bergmann 			&pnvme_lsreq->rspdma);
63101649561SJames Smart 
63201649561SJames Smart 	ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
6336514b25dSJames Smart 				pnvme_lsreq, gen_req_cmp, ndlp, 2,
634c33b1609SJames Smart 				pnvme_lsreq->timeout, 0);
63501649561SJames Smart 	if (ret != WQE_SUCCESS) {
636372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6376514b25dSJames Smart 				 "6052 NVMEx REQ: EXIT. issue ls wqe failed "
6386514b25dSJames Smart 				 "lsreq x%px Status %x DID %x\n",
6396514b25dSJames Smart 				 pnvme_lsreq, ret, ndlp->nlp_DID);
64001649561SJames Smart 		lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
64101649561SJames Smart 		kfree(bmp);
6426514b25dSJames Smart 		return -EIO;
64301649561SJames Smart 	}
64401649561SJames Smart 
6456514b25dSJames Smart 	return 0;
6466514b25dSJames Smart }
6476514b25dSJames Smart 
6486514b25dSJames Smart /**
6496514b25dSJames Smart  * lpfc_nvme_ls_req - Issue an NVME Link Service request
65009d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from.
6513e749170SLee Jones  * @pnvme_rport: Transport remoteport that LS is to be sent to.
65209d99705SLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
6536514b25dSJames Smart  *
6546514b25dSJames Smart  * Driver registers this routine to handle any link service request
6556514b25dSJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
6566514b25dSJames Smart  *
6576514b25dSJames Smart  * Return value :
6586514b25dSJames Smart  *   0 - Success
6596514b25dSJames Smart  *   non-zero: various error codes, in form of -Exxx
6606514b25dSJames Smart  **/
6616514b25dSJames Smart static int
lpfc_nvme_ls_req(struct nvme_fc_local_port * pnvme_lport,struct nvme_fc_remote_port * pnvme_rport,struct nvmefc_ls_req * pnvme_lsreq)6626514b25dSJames Smart lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
6636514b25dSJames Smart 		 struct nvme_fc_remote_port *pnvme_rport,
6646514b25dSJames Smart 		 struct nvmefc_ls_req *pnvme_lsreq)
6656514b25dSJames Smart {
6666514b25dSJames Smart 	struct lpfc_nvme_lport *lport;
6676514b25dSJames Smart 	struct lpfc_nvme_rport *rport;
6686514b25dSJames Smart 	struct lpfc_vport *vport;
6696514b25dSJames Smart 	int ret;
6706514b25dSJames Smart 
6716514b25dSJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
6726514b25dSJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
6736514b25dSJames Smart 	if (unlikely(!lport) || unlikely(!rport))
6746514b25dSJames Smart 		return -EINVAL;
6756514b25dSJames Smart 
6766514b25dSJames Smart 	vport = lport->vport;
677a4691038SJames Smart 	if (vport->load_flag & FC_UNLOADING ||
678a4691038SJames Smart 	    vport->phba->hba_flag & HBA_IOQ_FLUSH)
6796514b25dSJames Smart 		return -ENODEV;
6806514b25dSJames Smart 
6816514b25dSJames Smart 	atomic_inc(&lport->fc4NvmeLsRequests);
6826514b25dSJames Smart 
6836514b25dSJames Smart 	ret = __lpfc_nvme_ls_req(vport, rport->ndlp, pnvme_lsreq,
6846514b25dSJames Smart 				 lpfc_nvme_ls_req_cmp);
6856514b25dSJames Smart 	if (ret)
6866514b25dSJames Smart 		atomic_inc(&lport->xmt_ls_err);
6876514b25dSJames Smart 
68801649561SJames Smart 	return ret;
68901649561SJames Smart }
69001649561SJames Smart 
69101649561SJames Smart /**
692e96a22b0SJames Smart  * __lpfc_nvme_ls_abort - Generic service routine to abort a prior
693e96a22b0SJames Smart  *         NVME LS request
694e96a22b0SJames Smart  * @vport: The local port that issued the LS
695e96a22b0SJames Smart  * @ndlp: The remote port the LS was sent to
696e96a22b0SJames Smart  * @pnvme_lsreq: Pointer to LS request structure from the transport
69701649561SJames Smart  *
698e96a22b0SJames Smart  * The driver validates the ndlp, looks for the LS, and aborts the
699e96a22b0SJames Smart  * LS if found.
70001649561SJames Smart  *
701e96a22b0SJames Smart  * Returns:
702e96a22b0SJames Smart  * 0 : if LS found and aborted
703e96a22b0SJames Smart  * non-zero: various error conditions in form -Exxx
704e96a22b0SJames Smart  **/
705e96a22b0SJames Smart int
__lpfc_nvme_ls_abort(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct nvmefc_ls_req * pnvme_lsreq)706e96a22b0SJames Smart __lpfc_nvme_ls_abort(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
707e96a22b0SJames Smart 			struct nvmefc_ls_req *pnvme_lsreq)
708e96a22b0SJames Smart {
709e96a22b0SJames Smart 	struct lpfc_hba *phba = vport->phba;
710e96a22b0SJames Smart 	struct lpfc_sli_ring *pring;
711e96a22b0SJames Smart 	struct lpfc_iocbq *wqe, *next_wqe;
712e96a22b0SJames Smart 	bool foundit = false;
713e96a22b0SJames Smart 
714e96a22b0SJames Smart 	if (!ndlp) {
715372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
716e96a22b0SJames Smart 				"6049 NVMEx LS REQ Abort: Bad NDLP x%px DID "
717e96a22b0SJames Smart 				"x%06x, Failing LS Req\n",
718e96a22b0SJames Smart 				ndlp, ndlp ? ndlp->nlp_DID : 0);
719e96a22b0SJames Smart 		return -EINVAL;
720e96a22b0SJames Smart 	}
721e96a22b0SJames Smart 
722e96a22b0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_NVME_ABTS,
723e96a22b0SJames Smart 			 "6040 NVMEx LS REQ Abort: Issue LS_ABORT for lsreq "
724f1156125SJames Smart 			 "x%px rqstlen:%d rsplen:%d %pad %pad\n",
725e96a22b0SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
726e96a22b0SJames Smart 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
727e96a22b0SJames Smart 			 &pnvme_lsreq->rspdma);
728e96a22b0SJames Smart 
729e96a22b0SJames Smart 	/*
730e96a22b0SJames Smart 	 * Lock the ELS ring txcmplq and look for the wqe that matches
731e96a22b0SJames Smart 	 * this ELS. If found, issue an abort on the wqe.
732e96a22b0SJames Smart 	 */
733e96a22b0SJames Smart 	pring = phba->sli4_hba.nvmels_wq->pring;
734e96a22b0SJames Smart 	spin_lock_irq(&phba->hbalock);
735e96a22b0SJames Smart 	spin_lock(&pring->ring_lock);
736e96a22b0SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
737d51cf5bdSJames Smart 		if (wqe->context_un.nvme_lsreq == pnvme_lsreq) {
738a680a929SJames Smart 			wqe->cmd_flag |= LPFC_DRIVER_ABORTED;
739e96a22b0SJames Smart 			foundit = true;
740e96a22b0SJames Smart 			break;
741e96a22b0SJames Smart 		}
742e96a22b0SJames Smart 	}
743e96a22b0SJames Smart 	spin_unlock(&pring->ring_lock);
744e96a22b0SJames Smart 
745e96a22b0SJames Smart 	if (foundit)
746db7531d2SJames Smart 		lpfc_sli_issue_abort_iotag(phba, pring, wqe, NULL);
747e96a22b0SJames Smart 	spin_unlock_irq(&phba->hbalock);
748e96a22b0SJames Smart 
749e96a22b0SJames Smart 	if (foundit)
750e96a22b0SJames Smart 		return 0;
751e96a22b0SJames Smart 
752e96a22b0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_NVME_ABTS,
753f1156125SJames Smart 			 "6213 NVMEx LS REQ Abort: Unable to locate req x%px\n",
754e96a22b0SJames Smart 			 pnvme_lsreq);
7556b6e8963SJames Smart 	return -EINVAL;
756e96a22b0SJames Smart }
757e96a22b0SJames Smart 
7589aa09e98SJames Smart static int
lpfc_nvme_xmt_ls_rsp(struct nvme_fc_local_port * localport,struct nvme_fc_remote_port * remoteport,struct nvmefc_ls_rsp * ls_rsp)7599aa09e98SJames Smart lpfc_nvme_xmt_ls_rsp(struct nvme_fc_local_port *localport,
7609aa09e98SJames Smart 		     struct nvme_fc_remote_port *remoteport,
7619aa09e98SJames Smart 		     struct nvmefc_ls_rsp *ls_rsp)
7629aa09e98SJames Smart {
7639aa09e98SJames Smart 	struct lpfc_async_xchg_ctx *axchg =
7649aa09e98SJames Smart 		container_of(ls_rsp, struct lpfc_async_xchg_ctx, ls_rsp);
7659aa09e98SJames Smart 	struct lpfc_nvme_lport *lport;
7669aa09e98SJames Smart 	int rc;
7679aa09e98SJames Smart 
7689aa09e98SJames Smart 	if (axchg->phba->pport->load_flag & FC_UNLOADING)
7699aa09e98SJames Smart 		return -ENODEV;
7709aa09e98SJames Smart 
7719aa09e98SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
7729aa09e98SJames Smart 
7739aa09e98SJames Smart 	rc = __lpfc_nvme_xmt_ls_rsp(axchg, ls_rsp, __lpfc_nvme_xmt_ls_rsp_cmp);
7749aa09e98SJames Smart 
7759aa09e98SJames Smart 	if (rc) {
7769aa09e98SJames Smart 		/*
7779aa09e98SJames Smart 		 * unless the failure is due to having already sent
7789aa09e98SJames Smart 		 * the response, an abort will be generated for the
7799aa09e98SJames Smart 		 * exchange if the rsp can't be sent.
7809aa09e98SJames Smart 		 */
7819aa09e98SJames Smart 		if (rc != -EALREADY)
7829aa09e98SJames Smart 			atomic_inc(&lport->xmt_ls_abort);
7839aa09e98SJames Smart 		return rc;
7849aa09e98SJames Smart 	}
7859aa09e98SJames Smart 
7869aa09e98SJames Smart 	return 0;
7879aa09e98SJames Smart }
7889aa09e98SJames Smart 
789e96a22b0SJames Smart /**
790e96a22b0SJames Smart  * lpfc_nvme_ls_abort - Abort a prior NVME LS request
79109d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from.
79209d99705SLee Jones  * @pnvme_rport: Transport remoteport that LS is to be sent to.
79309d99705SLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
794e96a22b0SJames Smart  *
795e96a22b0SJames Smart  * Driver registers this routine to abort a NVME LS request that is
796e96a22b0SJames Smart  * in progress (from the transports perspective).
79701649561SJames Smart  **/
79801649561SJames Smart static void
lpfc_nvme_ls_abort(struct nvme_fc_local_port * pnvme_lport,struct nvme_fc_remote_port * pnvme_rport,struct nvmefc_ls_req * pnvme_lsreq)79901649561SJames Smart lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
80001649561SJames Smart 		   struct nvme_fc_remote_port *pnvme_rport,
80101649561SJames Smart 		   struct nvmefc_ls_req *pnvme_lsreq)
80201649561SJames Smart {
80301649561SJames Smart 	struct lpfc_nvme_lport *lport;
80401649561SJames Smart 	struct lpfc_vport *vport;
80501649561SJames Smart 	struct lpfc_nodelist *ndlp;
806e96a22b0SJames Smart 	int ret;
80701649561SJames Smart 
80801649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
80966a210ffSJames Smart 	if (unlikely(!lport))
81066a210ffSJames Smart 		return;
81101649561SJames Smart 	vport = lport->vport;
81201649561SJames Smart 
8133386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
8143386f4bdSJames Smart 		return;
8153386f4bdSJames Smart 
81601649561SJames Smart 	ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
81701649561SJames Smart 
818e96a22b0SJames Smart 	ret = __lpfc_nvme_ls_abort(vport, ndlp, pnvme_lsreq);
819e96a22b0SJames Smart 	if (!ret)
8204b056682SJames Smart 		atomic_inc(&lport->xmt_ls_abort);
82101649561SJames Smart }
82201649561SJames Smart 
82301649561SJames Smart /* Fix up the existing sgls for NVME IO. */
8245fd11085SJames Smart static inline void
lpfc_nvme_adj_fcp_sgls(struct lpfc_vport * vport,struct lpfc_io_buf * lpfc_ncmd,struct nvmefc_fcp_req * nCmd)82501649561SJames Smart lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
826c490850aSJames Smart 		       struct lpfc_io_buf *lpfc_ncmd,
82701649561SJames Smart 		       struct nvmefc_fcp_req *nCmd)
82801649561SJames Smart {
8294e565cf0SJames Smart 	struct lpfc_hba  *phba = vport->phba;
83001649561SJames Smart 	struct sli4_sge *sgl;
83101649561SJames Smart 	union lpfc_wqe128 *wqe;
83201649561SJames Smart 	uint32_t *wptr, *dptr;
83301649561SJames Smart 
83401649561SJames Smart 	/*
8354e565cf0SJames Smart 	 * Get a local pointer to the built-in wqe and correct
8364e565cf0SJames Smart 	 * the cmd size to match NVME's 96 bytes and fix
8374e565cf0SJames Smart 	 * the dma address.
8384e565cf0SJames Smart 	 */
8394e565cf0SJames Smart 
840205e8240SJames Smart 	wqe = &lpfc_ncmd->cur_iocbq.wqe;
8414e565cf0SJames Smart 
8424e565cf0SJames Smart 	/*
84301649561SJames Smart 	 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
84401649561SJames Smart 	 * match NVME.  NVME sends 96 bytes. Also, use the
84501649561SJames Smart 	 * nvme commands command and response dma addresses
84601649561SJames Smart 	 * rather than the virtual memory to ease the restore
84701649561SJames Smart 	 * operation.
84801649561SJames Smart 	 */
8490794d601SJames Smart 	sgl = lpfc_ncmd->dma_sgl;
85001649561SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
8514e565cf0SJames Smart 	if (phba->cfg_nvme_embed_cmd) {
8524e565cf0SJames Smart 		sgl->addr_hi = 0;
8534e565cf0SJames Smart 		sgl->addr_lo = 0;
8544e565cf0SJames Smart 
8554e565cf0SJames Smart 		/* Word 0-2 - NVME CMND IU (embedded payload) */
8564e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
8574e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeSize = 56;
8584e565cf0SJames Smart 		wqe->generic.bde.addrHigh = 0;
8594e565cf0SJames Smart 		wqe->generic.bde.addrLow =  64;  /* Word 16 */
8604e565cf0SJames Smart 
8615fd11085SJames Smart 		/* Word 10  - dbde is 0, wqes is 1 in template */
86201649561SJames Smart 
86301649561SJames Smart 		/*
86401649561SJames Smart 		 * Embed the payload in the last half of the WQE
86501649561SJames Smart 		 * WQE words 16-30 get the NVME CMD IU payload
86601649561SJames Smart 		 *
867b06a622fSJames Smart 		 * WQE words 16-19 get payload Words 1-4
86801649561SJames Smart 		 * WQE words 20-21 get payload Words 6-7
86901649561SJames Smart 		 * WQE words 22-29 get payload Words 16-23
87001649561SJames Smart 		 */
871b06a622fSJames Smart 		wptr = &wqe->words[16];  /* WQE ptr */
87201649561SJames Smart 		dptr = (uint32_t *)nCmd->cmdaddr;  /* payload ptr */
873b06a622fSJames Smart 		dptr++;			/* Skip Word 0 in payload */
87401649561SJames Smart 
875b06a622fSJames Smart 		*wptr++ = *dptr++;	/* Word 1 */
87601649561SJames Smart 		*wptr++ = *dptr++;	/* Word 2 */
87701649561SJames Smart 		*wptr++ = *dptr++;	/* Word 3 */
87801649561SJames Smart 		*wptr++ = *dptr++;	/* Word 4 */
87901649561SJames Smart 		dptr++;			/* Skip Word 5 in payload */
88001649561SJames Smart 		*wptr++ = *dptr++;	/* Word 6 */
88101649561SJames Smart 		*wptr++ = *dptr++;	/* Word 7 */
88201649561SJames Smart 		dptr += 8;		/* Skip Words 8-15 in payload */
88301649561SJames Smart 		*wptr++ = *dptr++;	/* Word 16 */
88401649561SJames Smart 		*wptr++ = *dptr++;	/* Word 17 */
88501649561SJames Smart 		*wptr++ = *dptr++;	/* Word 18 */
88601649561SJames Smart 		*wptr++ = *dptr++;	/* Word 19 */
88701649561SJames Smart 		*wptr++ = *dptr++;	/* Word 20 */
88801649561SJames Smart 		*wptr++ = *dptr++;	/* Word 21 */
88901649561SJames Smart 		*wptr++ = *dptr++;	/* Word 22 */
89001649561SJames Smart 		*wptr   = *dptr;	/* Word 23 */
8915fd11085SJames Smart 	} else {
8925fd11085SJames Smart 		sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->cmddma));
8935fd11085SJames Smart 		sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->cmddma));
8945fd11085SJames Smart 
8955fd11085SJames Smart 		/* Word 0-2 - NVME CMND IU Inline BDE */
8965fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
8975fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeSize = nCmd->cmdlen;
8985fd11085SJames Smart 		wqe->generic.bde.addrHigh = sgl->addr_hi;
8995fd11085SJames Smart 		wqe->generic.bde.addrLow =  sgl->addr_lo;
9005fd11085SJames Smart 
9015fd11085SJames Smart 		/* Word 10 */
9025fd11085SJames Smart 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
9035fd11085SJames Smart 		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
9045fd11085SJames Smart 	}
9055fd11085SJames Smart 
9065fd11085SJames Smart 	sgl++;
9075fd11085SJames Smart 
9085fd11085SJames Smart 	/* Setup the physical region for the FCP RSP */
9095fd11085SJames Smart 	sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
9105fd11085SJames Smart 	sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
9115fd11085SJames Smart 	sgl->word2 = le32_to_cpu(sgl->word2);
9125fd11085SJames Smart 	if (nCmd->sg_cnt)
9135fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
9145fd11085SJames Smart 	else
9155fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 1);
9165fd11085SJames Smart 	sgl->word2 = cpu_to_le32(sgl->word2);
9175fd11085SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->rsplen);
91801649561SJames Smart }
91901649561SJames Smart 
920bd2cdd5eSJames Smart 
92109d99705SLee Jones /*
922a680a929SJames Smart  * lpfc_nvme_io_cmd_cmpl - Complete an NVME-over-FCP IO
92301649561SJames Smart  *
92401649561SJames Smart  * Driver registers this routine as it io request handler.  This
92501649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
92601649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
92701649561SJames Smart  *
92801649561SJames Smart  * Return value :
92901649561SJames Smart  *   0 - Success
93001649561SJames Smart  *   TODO: What are the failure codes.
93101649561SJames Smart  **/
93201649561SJames Smart static void
lpfc_nvme_io_cmd_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * pwqeIn,struct lpfc_iocbq * pwqeOut)933a680a929SJames Smart lpfc_nvme_io_cmd_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
934a680a929SJames Smart 		      struct lpfc_iocbq *pwqeOut)
93501649561SJames Smart {
936d51cf5bdSJames Smart 	struct lpfc_io_buf *lpfc_ncmd = pwqeIn->io_buf;
937a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &pwqeOut->wcqe_cmpl;
93801649561SJames Smart 	struct lpfc_vport *vport = pwqeIn->vport;
93901649561SJames Smart 	struct nvmefc_fcp_req *nCmd;
94001649561SJames Smart 	struct nvme_fc_ersp_iu *ep;
94101649561SJames Smart 	struct nvme_fc_cmd_iu *cp;
94201649561SJames Smart 	struct lpfc_nodelist *ndlp;
943bbe3012bSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
9444b056682SJames Smart 	struct lpfc_nvme_lport *lport;
945352b205aSArnd Bergmann 	uint32_t code, status, idx;
94601649561SJames Smart 	uint16_t cid, sqhd, data;
94701649561SJames Smart 	uint32_t *ptr;
94802243836SJames Smart 	uint32_t lat;
94902243836SJames Smart 	bool call_done = false;
950840eda96SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
951840eda96SJames Smart 	int cpu;
952840eda96SJames Smart #endif
95325ac2c97SJames Smart 	int offline = 0;
95401649561SJames Smart 
95501649561SJames Smart 	/* Sanity check on return of outstanding command */
9564d5e789aSJames Smart 	if (!lpfc_ncmd) {
957372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9584d5e789aSJames Smart 				 "6071 Null lpfc_ncmd pointer. No "
9594d5e789aSJames Smart 				 "release, skip completion\n");
9604d5e789aSJames Smart 		return;
9614d5e789aSJames Smart 	}
9624d5e789aSJames Smart 
963c2017260SJames Smart 	/* Guard against abort handler being called at same time */
964c2017260SJames Smart 	spin_lock(&lpfc_ncmd->buf_lock);
965c2017260SJames Smart 
966c2017260SJames Smart 	if (!lpfc_ncmd->nvmeCmd) {
967c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
968372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
96932350664SJames Smart 				 "6066 Missing cmpl ptrs: lpfc_ncmd x%px, "
97032350664SJames Smart 				 "nvmeCmd x%px\n",
9710b05e9feSJames Smart 				 lpfc_ncmd, lpfc_ncmd->nvmeCmd);
9724d5e789aSJames Smart 
9734d5e789aSJames Smart 		/* Release the lpfc_ncmd regardless of the missing elements. */
9744d5e789aSJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
97501649561SJames Smart 		return;
97601649561SJames Smart 	}
97701649561SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
9784b056682SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
97966a210ffSJames Smart 
9804c47efc1SJames Smart 	idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
9814c47efc1SJames Smart 	phba->sli4_hba.hdwq[idx].nvme_cstat.io_cmpls++;
9824c47efc1SJames Smart 
9831df09449SJames Smart 	if (unlikely(status && vport->localport)) {
9844b056682SJames Smart 		lport = (struct lpfc_nvme_lport *)vport->localport->private;
9851df09449SJames Smart 		if (lport) {
9864b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
9874b056682SJames Smart 				atomic_inc(&lport->cmpl_fcp_xb);
9884b056682SJames Smart 			atomic_inc(&lport->cmpl_fcp_err);
9894b056682SJames Smart 		}
99066a210ffSJames Smart 	}
99101649561SJames Smart 
992bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
993bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
9944b056682SJames Smart 			 status, wcqe->parameter);
99501649561SJames Smart 	/*
99601649561SJames Smart 	 * Catch race where our node has transitioned, but the
99701649561SJames Smart 	 * transport is still transitioning.
99801649561SJames Smart 	 */
9990b05e9feSJames Smart 	ndlp = lpfc_ncmd->ndlp;
1000307e3380SJames Smart 	if (!ndlp) {
1001372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
100201649561SJames Smart 				 "6062 Ignoring NVME cmpl.  No ndlp\n");
100301649561SJames Smart 		goto out_err;
100401649561SJames Smart 	}
100501649561SJames Smart 
100601649561SJames Smart 	code = bf_get(lpfc_wcqe_c_code, wcqe);
100701649561SJames Smart 	if (code == CQE_CODE_NVME_ERSP) {
100801649561SJames Smart 		/* For this type of CQE, we need to rebuild the rsp */
100901649561SJames Smart 		ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
101001649561SJames Smart 
101101649561SJames Smart 		/*
101201649561SJames Smart 		 * Get Command Id from cmd to plug into response. This
101301649561SJames Smart 		 * code is not needed in the next NVME Transport drop.
101401649561SJames Smart 		 */
101501649561SJames Smart 		cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
101601649561SJames Smart 		cid = cp->sqe.common.command_id;
101701649561SJames Smart 
101801649561SJames Smart 		/*
101901649561SJames Smart 		 * RSN is in CQE word 2
102001649561SJames Smart 		 * SQHD is in CQE Word 3 bits 15:0
102101649561SJames Smart 		 * Cmd Specific info is in CQE Word 1
102201649561SJames Smart 		 * and in CQE Word 0 bits 15:0
102301649561SJames Smart 		 */
102401649561SJames Smart 		sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
102501649561SJames Smart 
102601649561SJames Smart 		/* Now lets build the NVME ERSP IU */
102701649561SJames Smart 		ep->iu_len = cpu_to_be16(8);
102801649561SJames Smart 		ep->rsn = wcqe->parameter;
102901649561SJames Smart 		ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
103001649561SJames Smart 		ep->rsvd12 = 0;
103101649561SJames Smart 		ptr = (uint32_t *)&ep->cqe.result.u64;
103201649561SJames Smart 		*ptr++ = wcqe->total_data_placed;
103301649561SJames Smart 		data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
103401649561SJames Smart 		*ptr = (uint32_t)data;
103501649561SJames Smart 		ep->cqe.sq_head = sqhd;
103601649561SJames Smart 		ep->cqe.sq_id =  nCmd->sqid;
103701649561SJames Smart 		ep->cqe.command_id = cid;
103801649561SJames Smart 		ep->cqe.status = 0;
103901649561SJames Smart 
104001649561SJames Smart 		lpfc_ncmd->status = IOSTAT_SUCCESS;
104101649561SJames Smart 		lpfc_ncmd->result = 0;
104201649561SJames Smart 		nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
104301649561SJames Smart 		nCmd->transferred_length = nCmd->payload_length;
104401649561SJames Smart 	} else {
10456a84d015SJustin Tee 		lpfc_ncmd->status = status;
1046952c303bSDick Kennedy 		lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
104701649561SJames Smart 
104801649561SJames Smart 		/* For NVME, the only failure path that results in an
104901649561SJames Smart 		 * IO error is when the adapter rejects it.  All other
105001649561SJames Smart 		 * conditions are a success case and resolved by the
105101649561SJames Smart 		 * transport.
105201649561SJames Smart 		 * IOSTAT_FCP_RSP_ERROR means:
105301649561SJames Smart 		 * 1. Length of data received doesn't match total
105401649561SJames Smart 		 *    transfer length in WQE
105501649561SJames Smart 		 * 2. If the RSP payload does NOT match these cases:
105601649561SJames Smart 		 *    a. RSP length 12/24 bytes and all zeros
105701649561SJames Smart 		 *    b. NVME ERSP
105801649561SJames Smart 		 */
105901649561SJames Smart 		switch (lpfc_ncmd->status) {
106001649561SJames Smart 		case IOSTAT_SUCCESS:
106101649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
106201649561SJames Smart 			nCmd->rcv_rsplen = 0;
106301649561SJames Smart 			nCmd->status = 0;
106401649561SJames Smart 			break;
106501649561SJames Smart 		case IOSTAT_FCP_RSP_ERROR:
106601649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
106701649561SJames Smart 			nCmd->rcv_rsplen = wcqe->parameter;
106801649561SJames Smart 			nCmd->status = 0;
1069fa21189dSJames Smart 
1070ea7bd1f3SJames Smart 			/* Get the NVME cmd details for this unique error. */
1071ea7bd1f3SJames Smart 			cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
1072ea7bd1f3SJames Smart 			ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
1073ea7bd1f3SJames Smart 
1074fa21189dSJames Smart 			/* Check if this is really an ERSP */
1075fa21189dSJames Smart 			if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN) {
1076fa21189dSJames Smart 				lpfc_ncmd->status = IOSTAT_SUCCESS;
1077fa21189dSJames Smart 				lpfc_ncmd->result = 0;
1078fa21189dSJames Smart 
1079fa21189dSJames Smart 				lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
1080ea7bd1f3SJames Smart 					"6084 NVME FCP_ERR ERSP: "
1081ea7bd1f3SJames Smart 					"xri %x placed x%x opcode x%x cmd_id "
1082ea7bd1f3SJames Smart 					"x%x cqe_status x%x\n",
1083fa21189dSJames Smart 					lpfc_ncmd->cur_iocbq.sli4_xritag,
1084ea7bd1f3SJames Smart 					wcqe->total_data_placed,
1085ea7bd1f3SJames Smart 					cp->sqe.common.opcode,
1086ea7bd1f3SJames Smart 					cp->sqe.common.command_id,
1087ea7bd1f3SJames Smart 					ep->cqe.status);
108801649561SJames Smart 				break;
1089fa21189dSJames Smart 			}
1090372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
109101649561SJames Smart 					 "6081 NVME Completion Protocol Error: "
109286c67379SJames Smart 					 "xri %x status x%x result x%x "
1093ea7bd1f3SJames Smart 					 "placed x%x opcode x%x cmd_id x%x, "
1094ea7bd1f3SJames Smart 					 "cqe_status x%x\n",
109586c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
109601649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
1097ea7bd1f3SJames Smart 					 wcqe->total_data_placed,
1098ea7bd1f3SJames Smart 					 cp->sqe.common.opcode,
1099ea7bd1f3SJames Smart 					 cp->sqe.common.command_id,
1100ea7bd1f3SJames Smart 					 ep->cqe.status);
110101649561SJames Smart 			break;
1102952c303bSDick Kennedy 		case IOSTAT_LOCAL_REJECT:
1103952c303bSDick Kennedy 			/* Let fall through to set command final state. */
1104952c303bSDick Kennedy 			if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
1105952c303bSDick Kennedy 				lpfc_printf_vlog(vport, KERN_INFO,
1106952c303bSDick Kennedy 					 LOG_NVME_IOERR,
110732350664SJames Smart 					 "6032 Delay Aborted cmd x%px "
110832350664SJames Smart 					 "nvme cmd x%px, xri x%x, "
1109952c303bSDick Kennedy 					 "xb %d\n",
1110952c303bSDick Kennedy 					 lpfc_ncmd, nCmd,
1111952c303bSDick Kennedy 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
1112952c303bSDick Kennedy 					 bf_get(lpfc_wcqe_c_xb, wcqe));
1113df561f66SGustavo A. R. Silva 			fallthrough;
111401649561SJames Smart 		default:
111501649561SJames Smart out_err:
1116e3246a12SJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
111786c67379SJames Smart 					 "6072 NVME Completion Error: xri %x "
11182c013a3aSJames Smart 					 "status x%x result x%x [x%x] "
11192c013a3aSJames Smart 					 "placed x%x\n",
112086c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
112101649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
11222c013a3aSJames Smart 					 wcqe->parameter,
112301649561SJames Smart 					 wcqe->total_data_placed);
112401649561SJames Smart 			nCmd->transferred_length = 0;
112501649561SJames Smart 			nCmd->rcv_rsplen = 0;
11268e009ce8SJames Smart 			nCmd->status = NVME_SC_INTERNAL;
112725ac2c97SJames Smart 			offline = pci_channel_offline(vport->phba->pcidev);
112801649561SJames Smart 		}
112901649561SJames Smart 	}
113001649561SJames Smart 
113101649561SJames Smart 	/* pick up SLI4 exhange busy condition */
113225ac2c97SJames Smart 	if (bf_get(lpfc_wcqe_c_xb, wcqe) && !offline)
113301649561SJames Smart 		lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
113401649561SJames Smart 	else
113501649561SJames Smart 		lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
113601649561SJames Smart 
113701649561SJames Smart 	/* Update stats and complete the IO.  There is
113801649561SJames Smart 	 * no need for dma unprep because the nvme_transport
113901649561SJames Smart 	 * owns the dma address.
114001649561SJames Smart 	 */
1141bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1142c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start) {
1143bd2cdd5eSJames Smart 		lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
11442fcbc569SJames Smart 		lpfc_ncmd->ts_data_io = ktime_get_ns();
11452fcbc569SJames Smart 		phba->ktime_last_cmd = lpfc_ncmd->ts_data_io;
11462fcbc569SJames Smart 		lpfc_io_ktime(phba, lpfc_ncmd);
1147bd2cdd5eSJames Smart 	}
1148840eda96SJames Smart 	if (unlikely(phba->hdwqstat_on & LPFC_CHECK_NVME_IO)) {
1149d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
1150840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
115163df6d63SJames Smart 		if (lpfc_ncmd->cpu != cpu)
115263df6d63SJames Smart 			lpfc_printf_vlog(vport,
115363df6d63SJames Smart 					 KERN_INFO, LOG_NVME_IOERR,
1154bd2cdd5eSJames Smart 					 "6701 CPU Check cmpl: "
1155bd2cdd5eSJames Smart 					 "cpu %d expect %d\n",
115663df6d63SJames Smart 					 cpu, lpfc_ncmd->cpu);
1157bd2cdd5eSJames Smart 	}
1158bd2cdd5eSJames Smart #endif
1159952c303bSDick Kennedy 
1160952c303bSDick Kennedy 	/* NVME targets need completion held off until the abort exchange
1161add9d6beSJames Smart 	 * completes unless the NVME Rport is getting unregistered.
1162952c303bSDick Kennedy 	 */
1163add9d6beSJames Smart 
11643fd78355SJames Smart 	if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
116591455b85SJames Smart 		freqpriv = nCmd->private;
116691455b85SJames Smart 		freqpriv->nvme_buf = NULL;
11673fd78355SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
116802243836SJames Smart 		call_done = true;
116902243836SJames Smart 	}
1170c2017260SJames Smart 	spin_unlock(&lpfc_ncmd->buf_lock);
117102243836SJames Smart 
117202243836SJames Smart 	/* Check if IO qualified for CMF */
117302243836SJames Smart 	if (phba->cmf_active_mode != LPFC_CFG_OFF &&
117402243836SJames Smart 	    nCmd->io_dir == NVMEFC_FCP_READ &&
117502243836SJames Smart 	    nCmd->payload_length) {
117602243836SJames Smart 		/* Used when calculating average latency */
117702243836SJames Smart 		lat = ktime_get_ns() - lpfc_ncmd->rx_cmd_start;
117802243836SJames Smart 		lpfc_update_cmf_cmpl(phba, lat, nCmd->payload_length, NULL);
117902243836SJames Smart 	}
118002243836SJames Smart 
118102243836SJames Smart 	if (call_done)
1182c2017260SJames Smart 		nCmd->done(nCmd);
118301649561SJames Smart 
1184952c303bSDick Kennedy 	/* Call release with XB=1 to queue the IO into the abort list. */
118501649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
118601649561SJames Smart }
118701649561SJames Smart 
118801649561SJames Smart 
118901649561SJames Smart /**
119001649561SJames Smart  * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
119109d99705SLee Jones  * @vport: pointer to a host virtual N_Port data structure
11923e749170SLee Jones  * @lpfc_ncmd: Pointer to lpfc scsi command
119309d99705SLee Jones  * @pnode: pointer to a node-list data structure
119409d99705SLee Jones  * @cstat: pointer to the control status structure
119501649561SJames Smart  *
119601649561SJames Smart  * Driver registers this routine as it io request handler.  This
119701649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
119801649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
119901649561SJames Smart  *
120001649561SJames Smart  * Return value :
120101649561SJames Smart  *   0 - Success
120201649561SJames Smart  *   TODO: What are the failure codes.
120301649561SJames Smart  **/
120401649561SJames Smart static int
lpfc_nvme_prep_io_cmd(struct lpfc_vport * vport,struct lpfc_io_buf * lpfc_ncmd,struct lpfc_nodelist * pnode,struct lpfc_fc4_ctrl_stat * cstat)120501649561SJames Smart lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
1206c490850aSJames Smart 		      struct lpfc_io_buf *lpfc_ncmd,
120766a210ffSJames Smart 		      struct lpfc_nodelist *pnode,
12084c47efc1SJames Smart 		      struct lpfc_fc4_ctrl_stat *cstat)
120901649561SJames Smart {
121001649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
121101649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
12122e7e9c0cSJames Smart 	struct nvme_common_command *sqe;
12132e7e9c0cSJames Smart 	struct lpfc_iocbq *pwqeq = &lpfc_ncmd->cur_iocbq;
1214205e8240SJames Smart 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
121501649561SJames Smart 	uint32_t req_len;
121601649561SJames Smart 
121701649561SJames Smart 	/*
121801649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
121901649561SJames Smart 	 * the single mapping, or neither.
122001649561SJames Smart 	 */
122101649561SJames Smart 	if (nCmd->sg_cnt) {
122201649561SJames Smart 		if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
12235fd11085SJames Smart 			/* From the iwrite template, initialize words 7 - 11 */
12245fd11085SJames Smart 			memcpy(&wqe->words[7],
12255fd11085SJames Smart 			       &lpfc_iwrite_cmd_template.words[7],
12265fd11085SJames Smart 			       sizeof(uint32_t) * 5);
12275fd11085SJames Smart 
12285fd11085SJames Smart 			/* Word 4 */
12295fd11085SJames Smart 			wqe->fcp_iwrite.total_xfer_len = nCmd->payload_length;
12305fd11085SJames Smart 
123101649561SJames Smart 			/* Word 5 */
123201649561SJames Smart 			if ((phba->cfg_nvme_enable_fb) &&
123301649561SJames Smart 			    (pnode->nlp_flag & NLP_FIRSTBURST)) {
123401649561SJames Smart 				req_len = lpfc_ncmd->nvmeCmd->payload_length;
123501649561SJames Smart 				if (req_len < pnode->nvme_fb_size)
123601649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
123701649561SJames Smart 						req_len;
123801649561SJames Smart 				else
123901649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
124001649561SJames Smart 						pnode->nvme_fb_size;
12415fd11085SJames Smart 			} else {
12425fd11085SJames Smart 				wqe->fcp_iwrite.initial_xfer_len = 0;
124301649561SJames Smart 			}
12444c47efc1SJames Smart 			cstat->output_requests++;
124501649561SJames Smart 		} else {
12465fd11085SJames Smart 			/* From the iread template, initialize words 7 - 11 */
12475fd11085SJames Smart 			memcpy(&wqe->words[7],
12485fd11085SJames Smart 			       &lpfc_iread_cmd_template.words[7],
12495fd11085SJames Smart 			       sizeof(uint32_t) * 5);
125001649561SJames Smart 
12515fd11085SJames Smart 			/* Word 4 */
12525fd11085SJames Smart 			wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
125301649561SJames Smart 
12545fd11085SJames Smart 			/* Word 5 */
12555fd11085SJames Smart 			wqe->fcp_iread.rsrvd5 = 0;
125601649561SJames Smart 
125702243836SJames Smart 			/* For a CMF Managed port, iod must be zero'ed */
125802243836SJames Smart 			if (phba->cmf_active_mode == LPFC_CFG_MANAGED)
125902243836SJames Smart 				bf_set(wqe_iod, &wqe->fcp_iread.wqe_com,
126002243836SJames Smart 				       LPFC_WQE_IOD_NONE);
12614c47efc1SJames Smart 			cstat->input_requests++;
126201649561SJames Smart 		}
126301649561SJames Smart 	} else {
12645fd11085SJames Smart 		/* From the icmnd template, initialize words 4 - 11 */
12655fd11085SJames Smart 		memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
12665fd11085SJames Smart 		       sizeof(uint32_t) * 8);
12674c47efc1SJames Smart 		cstat->control_requests++;
126801649561SJames Smart 	}
12690d8af096SJames Smart 
12702e7e9c0cSJames Smart 	if (pnode->nlp_nvme_info & NLP_NVME_NSLER) {
12710d8af096SJames Smart 		bf_set(wqe_erp, &wqe->generic.wqe_com, 1);
12722e7e9c0cSJames Smart 		sqe = &((struct nvme_fc_cmd_iu *)
12732e7e9c0cSJames Smart 			nCmd->cmdaddr)->sqe.common;
12742e7e9c0cSJames Smart 		if (sqe->opcode == nvme_admin_async_event)
12752e7e9c0cSJames Smart 			bf_set(wqe_ffrq, &wqe->generic.wqe_com, 1);
12762e7e9c0cSJames Smart 	}
12772e7e9c0cSJames Smart 
127801649561SJames Smart 	/*
127901649561SJames Smart 	 * Finish initializing those WQE fields that are independent
128001649561SJames Smart 	 * of the nvme_cmnd request_buffer
128101649561SJames Smart 	 */
128201649561SJames Smart 
12835fd11085SJames Smart 	/* Word 3 */
12845fd11085SJames Smart 	bf_set(payload_offset_len, &wqe->fcp_icmd,
12855fd11085SJames Smart 	       (nCmd->rsplen + nCmd->cmdlen));
12865fd11085SJames Smart 
128701649561SJames Smart 	/* Word 6 */
128801649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
128901649561SJames Smart 	       phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
129001649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
129101649561SJames Smart 
129201649561SJames Smart 	/* Word 8 */
129301649561SJames Smart 	wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
129401649561SJames Smart 
129501649561SJames Smart 	/* Word 9 */
129601649561SJames Smart 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
129701649561SJames Smart 
1298840a4701SJames Smart 	/* Word 10 */
1299840a4701SJames Smart 	bf_set(wqe_xchg, &wqe->fcp_iwrite.wqe_com, LPFC_NVME_XCHG);
1300840a4701SJames Smart 
1301414abe0aSJames Smart 	/* Words 13 14 15 are for PBDE support */
1302414abe0aSJames Smart 
1303896325a8SJames Smart 	/* add the VMID tags as per switch response */
1304896325a8SJames Smart 	if (unlikely(lpfc_ncmd->cur_iocbq.cmd_flag & LPFC_IO_VMID)) {
1305896325a8SJames Smart 		if (phba->pport->vmid_priority_tagging) {
1306896325a8SJames Smart 			bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
1307896325a8SJames Smart 			bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
1308896325a8SJames Smart 			       lpfc_ncmd->cur_iocbq.vmid_tag.cs_ctl_vmid);
1309896325a8SJames Smart 		} else {
1310896325a8SJames Smart 			bf_set(wqe_appid, &wqe->fcp_iwrite.wqe_com, 1);
1311896325a8SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
1312896325a8SJames Smart 			wqe->words[31] = lpfc_ncmd->cur_iocbq.vmid_tag.app_id;
1313896325a8SJames Smart 		}
1314896325a8SJames Smart 	}
1315896325a8SJames Smart 
131601649561SJames Smart 	pwqeq->vport = vport;
131701649561SJames Smart 	return 0;
131801649561SJames Smart }
131901649561SJames Smart 
132001649561SJames Smart 
132101649561SJames Smart /**
132201649561SJames Smart  * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
132309d99705SLee Jones  * @vport: pointer to a host virtual N_Port data structure
13243e749170SLee Jones  * @lpfc_ncmd: Pointer to lpfc scsi command
132501649561SJames Smart  *
132601649561SJames Smart  * Driver registers this routine as it io request handler.  This
132701649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
132801649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
132901649561SJames Smart  *
133001649561SJames Smart  * Return value :
133101649561SJames Smart  *   0 - Success
133201649561SJames Smart  *   TODO: What are the failure codes.
133301649561SJames Smart  **/
133401649561SJames Smart static int
lpfc_nvme_prep_io_dma(struct lpfc_vport * vport,struct lpfc_io_buf * lpfc_ncmd)133501649561SJames Smart lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
1336c490850aSJames Smart 		      struct lpfc_io_buf *lpfc_ncmd)
133701649561SJames Smart {
133801649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
133901649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1340205e8240SJames Smart 	union lpfc_wqe128 *wqe = &lpfc_ncmd->cur_iocbq.wqe;
13410794d601SJames Smart 	struct sli4_sge *sgl = lpfc_ncmd->dma_sgl;
1342d79c9e9dSJames Smart 	struct sli4_hybrid_sgl *sgl_xtra = NULL;
134301649561SJames Smart 	struct scatterlist *data_sg;
134401649561SJames Smart 	struct sli4_sge *first_data_sgl;
13450bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
1346d79c9e9dSJames Smart 	dma_addr_t physaddr = 0;
1347d79c9e9dSJames Smart 	uint32_t dma_len = 0;
134801649561SJames Smart 	uint32_t dma_offset = 0;
1349d79c9e9dSJames Smart 	int nseg, i, j;
1350d79c9e9dSJames Smart 	bool lsp_just_set = false;
135101649561SJames Smart 
135201649561SJames Smart 	/* Fix up the command and response DMA stuff. */
135301649561SJames Smart 	lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
135401649561SJames Smart 
135501649561SJames Smart 	/*
135601649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
135701649561SJames Smart 	 * the single mapping, or neither.
135801649561SJames Smart 	 */
135901649561SJames Smart 	if (nCmd->sg_cnt) {
136001649561SJames Smart 		/*
136101649561SJames Smart 		 * Jump over the cmd and rsp SGEs.  The fix routine
136201649561SJames Smart 		 * has already adjusted for this.
136301649561SJames Smart 		 */
136401649561SJames Smart 		sgl += 2;
136501649561SJames Smart 
136601649561SJames Smart 		first_data_sgl = sgl;
136701649561SJames Smart 		lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
136881e6a637SJames Smart 		if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
1369372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
137001649561SJames Smart 					"6058 Too many sg segments from "
137101649561SJames Smart 					"NVME Transport.  Max %d, "
137201649561SJames Smart 					"nvmeIO sg_cnt %d\n",
137329bfd55aSJames Smart 					phba->cfg_nvme_seg_cnt + 1,
137401649561SJames Smart 					lpfc_ncmd->seg_cnt);
137501649561SJames Smart 			lpfc_ncmd->seg_cnt = 0;
137601649561SJames Smart 			return 1;
137701649561SJames Smart 		}
137801649561SJames Smart 
137901649561SJames Smart 		/*
138001649561SJames Smart 		 * The driver established a maximum scatter-gather segment count
138101649561SJames Smart 		 * during probe that limits the number of sg elements in any
138201649561SJames Smart 		 * single nvme command.  Just run through the seg_cnt and format
138301649561SJames Smart 		 * the sge's.
138401649561SJames Smart 		 */
138501649561SJames Smart 		nseg = nCmd->sg_cnt;
138601649561SJames Smart 		data_sg = nCmd->first_sgl;
1387d79c9e9dSJames Smart 
1388d79c9e9dSJames Smart 		/* for tracking the segment boundaries */
1389d79c9e9dSJames Smart 		j = 2;
139001649561SJames Smart 		for (i = 0; i < nseg; i++) {
139101649561SJames Smart 			if (data_sg == NULL) {
1392372c187bSDick Kennedy 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
139301649561SJames Smart 						"6059 dptr err %d, nseg %d\n",
139401649561SJames Smart 						i, nseg);
139501649561SJames Smart 				lpfc_ncmd->seg_cnt = 0;
139601649561SJames Smart 				return 1;
139701649561SJames Smart 			}
1398d79c9e9dSJames Smart 
1399d79c9e9dSJames Smart 			sgl->word2 = 0;
1400315b3fd1SJames Smart 			if (nseg == 1) {
1401d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 1);
1402d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_type, sgl,
1403d79c9e9dSJames Smart 				       LPFC_SGE_TYPE_DATA);
1404d79c9e9dSJames Smart 			} else {
1405d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 0);
1406d79c9e9dSJames Smart 
1407d79c9e9dSJames Smart 				/* expand the segment */
1408d79c9e9dSJames Smart 				if (!lsp_just_set &&
1409d79c9e9dSJames Smart 				    !((j + 1) % phba->border_sge_num) &&
1410d79c9e9dSJames Smart 				    ((nseg - 1) != i)) {
1411d79c9e9dSJames Smart 					/* set LSP type */
1412d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_type, sgl,
1413d79c9e9dSJames Smart 					       LPFC_SGE_TYPE_LSP);
1414d79c9e9dSJames Smart 
1415d79c9e9dSJames Smart 					sgl_xtra = lpfc_get_sgl_per_hdwq(
1416d79c9e9dSJames Smart 							phba, lpfc_ncmd);
1417d79c9e9dSJames Smart 
1418d79c9e9dSJames Smart 					if (unlikely(!sgl_xtra)) {
1419d79c9e9dSJames Smart 						lpfc_ncmd->seg_cnt = 0;
1420d79c9e9dSJames Smart 						return 1;
1421d79c9e9dSJames Smart 					}
1422d79c9e9dSJames Smart 					sgl->addr_lo = cpu_to_le32(putPaddrLow(
1423d79c9e9dSJames Smart 						       sgl_xtra->dma_phys_sgl));
1424d79c9e9dSJames Smart 					sgl->addr_hi = cpu_to_le32(putPaddrHigh(
1425d79c9e9dSJames Smart 						       sgl_xtra->dma_phys_sgl));
1426d79c9e9dSJames Smart 
1427d79c9e9dSJames Smart 				} else {
1428d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_type, sgl,
1429d79c9e9dSJames Smart 					       LPFC_SGE_TYPE_DATA);
1430d79c9e9dSJames Smart 				}
1431d79c9e9dSJames Smart 			}
1432d79c9e9dSJames Smart 
1433d79c9e9dSJames Smart 			if (!(bf_get(lpfc_sli4_sge_type, sgl) &
1434d79c9e9dSJames Smart 				     LPFC_SGE_TYPE_LSP)) {
1435d79c9e9dSJames Smart 				if ((nseg - 1) == i)
1436d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_last, sgl, 1);
1437d79c9e9dSJames Smart 
1438a14396b6SJames Smart 				physaddr = sg_dma_address(data_sg);
1439a14396b6SJames Smart 				dma_len = sg_dma_len(data_sg);
1440d79c9e9dSJames Smart 				sgl->addr_lo = cpu_to_le32(
1441d79c9e9dSJames Smart 							 putPaddrLow(physaddr));
1442d79c9e9dSJames Smart 				sgl->addr_hi = cpu_to_le32(
1443d79c9e9dSJames Smart 							putPaddrHigh(physaddr));
1444d79c9e9dSJames Smart 
144501649561SJames Smart 				bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
144601649561SJames Smart 				sgl->word2 = cpu_to_le32(sgl->word2);
144701649561SJames Smart 				sgl->sge_len = cpu_to_le32(dma_len);
144801649561SJames Smart 
144901649561SJames Smart 				dma_offset += dma_len;
145001649561SJames Smart 				data_sg = sg_next(data_sg);
1451d79c9e9dSJames Smart 
145201649561SJames Smart 				sgl++;
1453d79c9e9dSJames Smart 
1454d79c9e9dSJames Smart 				lsp_just_set = false;
1455d79c9e9dSJames Smart 			} else {
1456d79c9e9dSJames Smart 				sgl->word2 = cpu_to_le32(sgl->word2);
1457d79c9e9dSJames Smart 
1458d79c9e9dSJames Smart 				sgl->sge_len = cpu_to_le32(
1459d79c9e9dSJames Smart 						     phba->cfg_sg_dma_buf_size);
1460d79c9e9dSJames Smart 
1461d79c9e9dSJames Smart 				sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
1462d79c9e9dSJames Smart 				i = i - 1;
1463d79c9e9dSJames Smart 
1464d79c9e9dSJames Smart 				lsp_just_set = true;
1465d79c9e9dSJames Smart 			}
1466d79c9e9dSJames Smart 
1467d79c9e9dSJames Smart 			j++;
146801649561SJames Smart 		}
1469315b3fd1SJames Smart 
1470315b3fd1SJames Smart 		/* PBDE support for first data SGE only */
1471315b3fd1SJames Smart 		if (nseg == 1 && phba->cfg_enable_pbde) {
14720bc2b7c5SJames Smart 			/* Words 13-15 */
14730bc2b7c5SJames Smart 			bde = (struct ulp_bde64 *)
14740bc2b7c5SJames Smart 				&wqe->words[13];
14750bc2b7c5SJames Smart 			bde->addrLow = first_data_sgl->addr_lo;
14760bc2b7c5SJames Smart 			bde->addrHigh = first_data_sgl->addr_hi;
14770bc2b7c5SJames Smart 			bde->tus.f.bdeSize =
14780bc2b7c5SJames Smart 				le32_to_cpu(first_data_sgl->sge_len);
14790bc2b7c5SJames Smart 			bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
14800bc2b7c5SJames Smart 			bde->tus.w = cpu_to_le32(bde->tus.w);
1481b101eb27SJames Smart 
1482315b3fd1SJames Smart 			/* Word 11 - set PBDE bit */
1483b101eb27SJames Smart 			bf_set(wqe_pbde, &wqe->generic.wqe_com, 1);
14845fd11085SJames Smart 		} else {
14855fd11085SJames Smart 			memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
1486315b3fd1SJames Smart 			/* Word 11 - PBDE bit disabled by default template */
14875fd11085SJames Smart 		}
14880bc2b7c5SJames Smart 
1489414abe0aSJames Smart 	} else {
14900794d601SJames Smart 		lpfc_ncmd->seg_cnt = 0;
14910794d601SJames Smart 
149201649561SJames Smart 		/* For this clause to be valid, the payload_length
149301649561SJames Smart 		 * and sg_cnt must zero.
149401649561SJames Smart 		 */
149501649561SJames Smart 		if (nCmd->payload_length != 0) {
1496372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
149701649561SJames Smart 					"6063 NVME DMA Prep Err: sg_cnt %d "
149801649561SJames Smart 					"payload_length x%x\n",
149901649561SJames Smart 					nCmd->sg_cnt, nCmd->payload_length);
150001649561SJames Smart 			return 1;
150101649561SJames Smart 		}
150201649561SJames Smart 	}
150301649561SJames Smart 	return 0;
150401649561SJames Smart }
150501649561SJames Smart 
150601649561SJames Smart /**
150701649561SJames Smart  * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
15083e749170SLee Jones  * @pnvme_lport: Pointer to the driver's local port data
15093e749170SLee Jones  * @pnvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
151001649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
15113e749170SLee Jones  * @pnvme_fcreq: IO request from nvme fc to driver.
151201649561SJames Smart  *
151301649561SJames Smart  * Driver registers this routine as it io request handler.  This
151401649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
15153e749170SLee Jones  * data structure to the rport indicated in @lpfc_nvme_rport.
151601649561SJames Smart  *
151701649561SJames Smart  * Return value :
151801649561SJames Smart  *   0 - Success
151901649561SJames Smart  *   TODO: What are the failure codes.
152001649561SJames Smart  **/
152101649561SJames Smart static int
lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port * pnvme_lport,struct nvme_fc_remote_port * pnvme_rport,void * hw_queue_handle,struct nvmefc_fcp_req * pnvme_fcreq)152201649561SJames Smart lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
152301649561SJames Smart 			struct nvme_fc_remote_port *pnvme_rport,
152401649561SJames Smart 			void *hw_queue_handle,
152501649561SJames Smart 			struct nvmefc_fcp_req *pnvme_fcreq)
152601649561SJames Smart {
152701649561SJames Smart 	int ret = 0;
1528cf1a1d3eSJames Smart 	int expedite = 0;
152963df6d63SJames Smart 	int idx, cpu;
153001649561SJames Smart 	struct lpfc_nvme_lport *lport;
15314c47efc1SJames Smart 	struct lpfc_fc4_ctrl_stat *cstat;
153201649561SJames Smart 	struct lpfc_vport *vport;
153301649561SJames Smart 	struct lpfc_hba *phba;
153401649561SJames Smart 	struct lpfc_nodelist *ndlp;
1535c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
153601649561SJames Smart 	struct lpfc_nvme_rport *rport;
153701649561SJames Smart 	struct lpfc_nvme_qhandle *lpfc_queue_info;
1538c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
1539cf1a1d3eSJames Smart 	struct nvme_common_command *sqe;
1540bd2cdd5eSJames Smart 	uint64_t start = 0;
1541896325a8SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
1542896325a8SJames Smart 	u8 *uuid = NULL;
1543896325a8SJames Smart 	int err;
1544896325a8SJames Smart 	enum dma_data_direction iodir;
1545896325a8SJames Smart #endif
154601649561SJames Smart 
1547c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1548c3725bdcSJames Smart 	 * have timing races.
1549c3725bdcSJames Smart 	 */
155001649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1551c3725bdcSJames Smart 	if (unlikely(!lport)) {
1552c3725bdcSJames Smart 		ret = -EINVAL;
1553c3725bdcSJames Smart 		goto out_fail;
1554c3725bdcSJames Smart 	}
1555c3725bdcSJames Smart 
155601649561SJames Smart 	vport = lport->vport;
1557c3725bdcSJames Smart 
1558c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
155944c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
156044c2757bSJames Smart 				 "6117 Fail IO, NULL hw_queue_handle\n");
156144c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1562cd240071SJames Smart 		ret = -EBUSY;
1563c3725bdcSJames Smart 		goto out_fail;
1564c3725bdcSJames Smart 	}
1565c3725bdcSJames Smart 
156601649561SJames Smart 	phba = vport->phba;
156701649561SJames Smart 
1568a4691038SJames Smart 	if ((unlikely(vport->load_flag & FC_UNLOADING)) ||
1569a4691038SJames Smart 	    phba->hba_flag & HBA_IOQ_FLUSH) {
157044c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
157144c2757bSJames Smart 				 "6124 Fail IO, Driver unload\n");
157244c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
15733386f4bdSJames Smart 		ret = -ENODEV;
15743386f4bdSJames Smart 		goto out_fail;
15753386f4bdSJames Smart 	}
15763386f4bdSJames Smart 
1577c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1578c3725bdcSJames Smart 	if (unlikely(!freqpriv)) {
157944c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
158044c2757bSJames Smart 				 "6158 Fail IO, NULL request data\n");
158144c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1582c3725bdcSJames Smart 		ret = -EINVAL;
1583b7672ae6SDick Kennedy 		goto out_fail;
1584b7672ae6SDick Kennedy 	}
1585b7672ae6SDick Kennedy 
1586bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1587bd2cdd5eSJames Smart 	if (phba->ktime_on)
1588bd2cdd5eSJames Smart 		start = ktime_get_ns();
1589bd2cdd5eSJames Smart #endif
159001649561SJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
159101649561SJames Smart 	lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
159201649561SJames Smart 
159301649561SJames Smart 	/*
159401649561SJames Smart 	 * Catch race where our node has transitioned, but the
159501649561SJames Smart 	 * transport is still transitioning.
159601649561SJames Smart 	 */
159701649561SJames Smart 	ndlp = rport->ndlp;
1598307e3380SJames Smart 	if (!ndlp) {
159944c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
160032350664SJames Smart 				 "6053 Busy IO, ndlp not ready: rport x%px "
160132350664SJames Smart 				  "ndlp x%px, DID x%06x\n",
160201649561SJames Smart 				 rport, ndlp, pnvme_rport->port_id);
160344c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1604cd240071SJames Smart 		ret = -EBUSY;
160501649561SJames Smart 		goto out_fail;
160601649561SJames Smart 	}
160701649561SJames Smart 
160801649561SJames Smart 	/* The remote node has to be a mapped target or it's an error. */
160901649561SJames Smart 	if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
161001649561SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
161144c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
161244c2757bSJames Smart 				 "6036 Fail IO, DID x%06x not ready for "
1613cd240071SJames Smart 				 "IO. State x%x, Type x%x Flg x%x\n",
161444c2757bSJames Smart 				 pnvme_rport->port_id,
1615cd240071SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type,
1616e9b11083SJames Smart 				 ndlp->fc4_xpt_flags);
16174b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_bad_ndlp);
1618cd240071SJames Smart 		ret = -EBUSY;
161901649561SJames Smart 		goto out_fail;
162001649561SJames Smart 
162101649561SJames Smart 	}
162201649561SJames Smart 
1623cf1a1d3eSJames Smart 	/* Currently only NVME Keep alive commands should be expedited
1624cf1a1d3eSJames Smart 	 * if the driver runs out of a resource. These should only be
1625cf1a1d3eSJames Smart 	 * issued on the admin queue, qidx 0
1626cf1a1d3eSJames Smart 	 */
1627cf1a1d3eSJames Smart 	if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
1628cf1a1d3eSJames Smart 		sqe = &((struct nvme_fc_cmd_iu *)
1629cf1a1d3eSJames Smart 			pnvme_fcreq->cmdaddr)->sqe.common;
1630cf1a1d3eSJames Smart 		if (sqe->opcode == nvme_admin_keep_alive)
1631cf1a1d3eSJames Smart 			expedite = 1;
1632cf1a1d3eSJames Smart 	}
1633cf1a1d3eSJames Smart 
163402243836SJames Smart 	/* Check if IO qualifies for CMF */
163502243836SJames Smart 	if (phba->cmf_active_mode != LPFC_CFG_OFF &&
163602243836SJames Smart 	    pnvme_fcreq->io_dir == NVMEFC_FCP_READ &&
163702243836SJames Smart 	    pnvme_fcreq->payload_length) {
163802243836SJames Smart 		ret = lpfc_update_cmf_cmd(phba, pnvme_fcreq->payload_length);
163902243836SJames Smart 		if (ret) {
164002243836SJames Smart 			ret = -EBUSY;
164102243836SJames Smart 			goto out_fail;
164202243836SJames Smart 		}
164302243836SJames Smart 		/* Get start time for IO latency */
164402243836SJames Smart 		start = ktime_get_ns();
164502243836SJames Smart 	}
164602243836SJames Smart 
164701649561SJames Smart 	/* The node is shared with FCP IO, make sure the IO pending count does
164801649561SJames Smart 	 * not exceed the programmed depth.
164901649561SJames Smart 	 */
16502a5b7d62SJames Smart 	if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1651cf1a1d3eSJames Smart 		if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
1652cf1a1d3eSJames Smart 		    !expedite) {
165344c2757bSJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
165444c2757bSJames Smart 					 "6174 Fail IO, ndlp qdepth exceeded: "
16554d5e789aSJames Smart 					 "idx %d DID %x pend %d qdepth %d\n",
16564d5e789aSJames Smart 					 lpfc_queue_info->index, ndlp->nlp_DID,
16574d5e789aSJames Smart 					 atomic_read(&ndlp->cmd_pending),
16584d5e789aSJames Smart 					 ndlp->cmd_qdepth);
16594b056682SJames Smart 			atomic_inc(&lport->xmt_fcp_qdepth);
1660cd22d605SDick Kennedy 			ret = -EBUSY;
166102243836SJames Smart 			goto out_fail1;
166201649561SJames Smart 		}
16632a5b7d62SJames Smart 	}
166401649561SJames Smart 
16656a828b0fSJames Smart 	/* Lookup Hardware Queue index based on fcp_io_sched module parameter */
166645aa312eSJames Smart 	if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
166745aa312eSJames Smart 		idx = lpfc_queue_info->index;
166845aa312eSJames Smart 	} else {
1669d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
16706a828b0fSJames Smart 		idx = phba->sli4_hba.cpu_map[cpu].hdwq;
167145aa312eSJames Smart 	}
167245aa312eSJames Smart 
167345aa312eSJames Smart 	lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, idx, expedite);
167401649561SJames Smart 	if (lpfc_ncmd == NULL) {
16754b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_noxri);
167601649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
167744c2757bSJames Smart 				 "6065 Fail IO, driver buffer pool is empty: "
167844c2757bSJames Smart 				 "idx %d DID %x\n",
167944c2757bSJames Smart 				 lpfc_queue_info->index, ndlp->nlp_DID);
1680cd22d605SDick Kennedy 		ret = -EBUSY;
168102243836SJames Smart 		goto out_fail1;
168201649561SJames Smart 	}
1683bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1684c8a4ce0bSDick Kennedy 	if (start) {
1685bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_start = start;
1686bd2cdd5eSJames Smart 		lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
1687c8a4ce0bSDick Kennedy 	} else {
1688c8a4ce0bSDick Kennedy 		lpfc_ncmd->ts_cmd_start = 0;
1689bd2cdd5eSJames Smart 	}
1690bd2cdd5eSJames Smart #endif
169102243836SJames Smart 	lpfc_ncmd->rx_cmd_start = start;
169201649561SJames Smart 
169301649561SJames Smart 	/*
169401649561SJames Smart 	 * Store the data needed by the driver to issue, abort, and complete
169501649561SJames Smart 	 * an IO.
169601649561SJames Smart 	 * Do not let the IO hang out forever.  There is no midlayer issuing
169701649561SJames Smart 	 * an abort so inform the FW of the maximum IO pending time.
169801649561SJames Smart 	 */
1699bbe3012bSJames Smart 	freqpriv->nvme_buf = lpfc_ncmd;
170001649561SJames Smart 	lpfc_ncmd->nvmeCmd = pnvme_fcreq;
1701318083adSJames Smart 	lpfc_ncmd->ndlp = ndlp;
17020794d601SJames Smart 	lpfc_ncmd->qidx = lpfc_queue_info->qidx;
170301649561SJames Smart 
1704896325a8SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
1705896325a8SJames Smart 	/* check the necessary and sufficient condition to support VMID */
1706896325a8SJames Smart 	if (lpfc_is_vmid_enabled(phba) &&
1707896325a8SJames Smart 	    (ndlp->vmid_support ||
1708896325a8SJames Smart 	     phba->pport->vmid_priority_tagging ==
1709896325a8SJames Smart 	     LPFC_VMID_PRIO_TAG_ALL_TARGETS)) {
1710896325a8SJames Smart 		/* is the I/O generated by a VM, get the associated virtual */
1711896325a8SJames Smart 		/* entity id */
1712896325a8SJames Smart 		uuid = nvme_fc_io_getuuid(pnvme_fcreq);
1713896325a8SJames Smart 
1714896325a8SJames Smart 		if (uuid) {
1715896325a8SJames Smart 			if (pnvme_fcreq->io_dir == NVMEFC_FCP_WRITE)
1716896325a8SJames Smart 				iodir = DMA_TO_DEVICE;
1717896325a8SJames Smart 			else if (pnvme_fcreq->io_dir == NVMEFC_FCP_READ)
1718896325a8SJames Smart 				iodir = DMA_FROM_DEVICE;
1719896325a8SJames Smart 			else
1720896325a8SJames Smart 				iodir = DMA_NONE;
1721896325a8SJames Smart 
1722896325a8SJames Smart 			err = lpfc_vmid_get_appid(vport, uuid, iodir,
1723896325a8SJames Smart 					(union lpfc_vmid_io_tag *)
1724896325a8SJames Smart 						&lpfc_ncmd->cur_iocbq.vmid_tag);
1725896325a8SJames Smart 			if (!err)
1726896325a8SJames Smart 				lpfc_ncmd->cur_iocbq.cmd_flag |= LPFC_IO_VMID;
1727896325a8SJames Smart 		}
1728896325a8SJames Smart 	}
1729896325a8SJames Smart #endif
1730896325a8SJames Smart 
173101649561SJames Smart 	/*
173201649561SJames Smart 	 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
173301649561SJames Smart 	 * This identfier was create in our hardware queue create callback
173401649561SJames Smart 	 * routine. The driver now is dependent on the IO queue steering from
173501649561SJames Smart 	 * the transport.  We are trusting the upper NVME layers know which
173601649561SJames Smart 	 * index to use and that they have affinitized a CPU to this hardware
173701649561SJames Smart 	 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
173801649561SJames Smart 	 */
173966a210ffSJames Smart 	lpfc_ncmd->cur_iocbq.hba_wqidx = idx;
17404c47efc1SJames Smart 	cstat = &phba->sli4_hba.hdwq[idx].nvme_cstat;
174166a210ffSJames Smart 
174266a210ffSJames Smart 	lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp, cstat);
174366a210ffSJames Smart 	ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
174466a210ffSJames Smart 	if (ret) {
174544c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
174644c2757bSJames Smart 				 "6175 Fail IO, Prep DMA: "
174744c2757bSJames Smart 				 "idx %d DID %x\n",
174844c2757bSJames Smart 				 lpfc_queue_info->index, ndlp->nlp_DID);
174944c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
175066a210ffSJames Smart 		ret = -ENOMEM;
175166a210ffSJames Smart 		goto out_free_nvme_buf;
175266a210ffSJames Smart 	}
175366a210ffSJames Smart 
1754bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1755bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
1756bd2cdd5eSJames Smart 			 lpfc_queue_info->index, ndlp->nlp_DID);
1757bd2cdd5eSJames Smart 
17581fbf9742SJames Smart 	ret = lpfc_sli4_issue_wqe(phba, lpfc_ncmd->hdwq, &lpfc_ncmd->cur_iocbq);
175901649561SJames Smart 	if (ret) {
17604b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_wqerr);
1761e3246a12SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
176244c2757bSJames Smart 				 "6113 Fail IO, Could not issue WQE err %x "
176301649561SJames Smart 				 "sid: x%x did: x%x oxid: x%x\n",
176401649561SJames Smart 				 ret, vport->fc_myDID, ndlp->nlp_DID,
176501649561SJames Smart 				 lpfc_ncmd->cur_iocbq.sli4_xritag);
176601649561SJames Smart 		goto out_free_nvme_buf;
176701649561SJames Smart 	}
176801649561SJames Smart 
1769c490850aSJames Smart 	if (phba->cfg_xri_rebalancing)
1770c490850aSJames Smart 		lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_ncmd->hdwq_no);
1771c490850aSJames Smart 
1772bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1773c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start)
1774bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1775bd2cdd5eSJames Smart 
1776840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVME_IO) {
1777d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
1778840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
177963df6d63SJames Smart 		lpfc_ncmd->cpu = cpu;
178063df6d63SJames Smart 		if (idx != cpu)
1781bd2cdd5eSJames Smart 			lpfc_printf_vlog(vport,
178263df6d63SJames Smart 					 KERN_INFO, LOG_NVME_IOERR,
1783bd2cdd5eSJames Smart 					"6702 CPU Check cmd: "
1784bd2cdd5eSJames Smart 					"cpu %d wq %d\n",
1785bd2cdd5eSJames Smart 					lpfc_ncmd->cpu,
1786bd2cdd5eSJames Smart 					lpfc_queue_info->index);
1787bd2cdd5eSJames Smart 	}
1788bd2cdd5eSJames Smart #endif
178901649561SJames Smart 	return 0;
179001649561SJames Smart 
179101649561SJames Smart  out_free_nvme_buf:
17922cee7808SJames Smart 	if (lpfc_ncmd->nvmeCmd->sg_cnt) {
17932cee7808SJames Smart 		if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
17944c47efc1SJames Smart 			cstat->output_requests--;
17952cee7808SJames Smart 		else
17964c47efc1SJames Smart 			cstat->input_requests--;
17972cee7808SJames Smart 	} else
17984c47efc1SJames Smart 		cstat->control_requests--;
179901649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
180002243836SJames Smart  out_fail1:
180102243836SJames Smart 	lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT,
180202243836SJames Smart 			     pnvme_fcreq->payload_length, NULL);
180301649561SJames Smart  out_fail:
180401649561SJames Smart 	return ret;
180501649561SJames Smart }
180601649561SJames Smart 
180701649561SJames Smart /**
180801649561SJames Smart  * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
180901649561SJames Smart  * @phba: Pointer to HBA context object
181001649561SJames Smart  * @cmdiocb: Pointer to command iocb object.
181124e1f056SJames Smart  * @rspiocb: Pointer to response iocb object.
181201649561SJames Smart  *
181301649561SJames Smart  * This is the callback function for any NVME FCP IO that was aborted.
181401649561SJames Smart  *
181501649561SJames Smart  * Return value:
181601649561SJames Smart  *   None
181701649561SJames Smart  **/
181801649561SJames Smart void
lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)181901649561SJames Smart lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
182024e1f056SJames Smart 			   struct lpfc_iocbq *rspiocb)
182101649561SJames Smart {
182224e1f056SJames Smart 	struct lpfc_wcqe_complete *abts_cmpl = &rspiocb->wcqe_cmpl;
182324e1f056SJames Smart 
1824e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
182501649561SJames Smart 			"6145 ABORT_XRI_CN completing on rpi x%x "
182601649561SJames Smart 			"original iotag x%x, abort cmd iotag x%x "
182701649561SJames Smart 			"req_tag x%x, status x%x, hwstatus x%x\n",
182831a59f75SJames Smart 			bf_get(wqe_ctxt_tag, &cmdiocb->wqe.generic.wqe_com),
182931a59f75SJames Smart 			get_job_abtsiotag(phba, cmdiocb), cmdiocb->iotag,
183001649561SJames Smart 			bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
183101649561SJames Smart 			bf_get(lpfc_wcqe_c_status, abts_cmpl),
183201649561SJames Smart 			bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
183301649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocb);
183401649561SJames Smart }
183501649561SJames Smart 
183601649561SJames Smart /**
183701649561SJames Smart  * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
18383e749170SLee Jones  * @pnvme_lport: Pointer to the driver's local port data
18393e749170SLee Jones  * @pnvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
184001649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
18413e749170SLee Jones  * @pnvme_fcreq: IO request from nvme fc to driver.
184201649561SJames Smart  *
184301649561SJames Smart  * Driver registers this routine as its nvme request io abort handler.  This
184401649561SJames Smart  * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
184501649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.  This routine
184601649561SJames Smart  * is executed asynchronously - one the target is validated as "MAPPED" and
184701649561SJames Smart  * ready for IO, the driver issues the abort request and returns.
184801649561SJames Smart  *
184901649561SJames Smart  * Return value:
185001649561SJames Smart  *   None
185101649561SJames Smart  **/
185201649561SJames Smart static void
lpfc_nvme_fcp_abort(struct nvme_fc_local_port * pnvme_lport,struct nvme_fc_remote_port * pnvme_rport,void * hw_queue_handle,struct nvmefc_fcp_req * pnvme_fcreq)185301649561SJames Smart lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
185401649561SJames Smart 		    struct nvme_fc_remote_port *pnvme_rport,
185501649561SJames Smart 		    void *hw_queue_handle,
185601649561SJames Smart 		    struct nvmefc_fcp_req *pnvme_fcreq)
185701649561SJames Smart {
185801649561SJames Smart 	struct lpfc_nvme_lport *lport;
185901649561SJames Smart 	struct lpfc_vport *vport;
186001649561SJames Smart 	struct lpfc_hba *phba;
1861c490850aSJames Smart 	struct lpfc_io_buf *lpfc_nbuf;
186201649561SJames Smart 	struct lpfc_iocbq *nvmereq_wqe;
1863c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
186401649561SJames Smart 	unsigned long flags;
186501649561SJames Smart 	int ret_val;
186601649561SJames Smart 
1867c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1868c3725bdcSJames Smart 	 * have timing races.
1869c3725bdcSJames Smart 	 */
187001649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1871c3725bdcSJames Smart 	if (unlikely(!lport))
1872c3725bdcSJames Smart 		return;
187301649561SJames Smart 
1874c3725bdcSJames Smart 	vport = lport->vport;
1875c3725bdcSJames Smart 
1876c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
1877c3725bdcSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1878c3725bdcSJames Smart 				 "6129 Fail Abort, HW Queue Handle NULL.\n");
1879c3725bdcSJames Smart 		return;
1880c3725bdcSJames Smart 	}
1881c3725bdcSJames Smart 
1882c3725bdcSJames Smart 	phba = vport->phba;
1883c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1884c3725bdcSJames Smart 
1885c3725bdcSJames Smart 	if (unlikely(!freqpriv))
1886c3725bdcSJames Smart 		return;
18873386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
18883386f4bdSJames Smart 		return;
18893386f4bdSJames Smart 
189001649561SJames Smart 	/* Announce entry to new IO submit field. */
189186c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
189201649561SJames Smart 			 "6002 Abort Request to rport DID x%06x "
189332350664SJames Smart 			 "for nvme_fc_req x%px\n",
189401649561SJames Smart 			 pnvme_rport->port_id,
189501649561SJames Smart 			 pnvme_fcreq);
189601649561SJames Smart 
189778e9e350SJustin Tee 	lpfc_nbuf = freqpriv->nvme_buf;
189878e9e350SJustin Tee 	if (!lpfc_nbuf) {
189978e9e350SJustin Tee 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
190078e9e350SJustin Tee 				 "6140 NVME IO req has no matching lpfc nvme "
190178e9e350SJustin Tee 				 "io buffer.  Skipping abort req.\n");
190278e9e350SJustin Tee 		return;
190378e9e350SJustin Tee 	} else if (!lpfc_nbuf->nvmeCmd) {
190478e9e350SJustin Tee 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
190578e9e350SJustin Tee 				 "6141 lpfc NVME IO req has no nvme_fcreq "
190678e9e350SJustin Tee 				 "io buffer.  Skipping abort req.\n");
190778e9e350SJustin Tee 		return;
190878e9e350SJustin Tee 	}
190978e9e350SJustin Tee 
191078e9e350SJustin Tee 	/* Guard against IO completion being called at same time */
191178e9e350SJustin Tee 	spin_lock_irqsave(&lpfc_nbuf->buf_lock, flags);
191278e9e350SJustin Tee 
191301649561SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
191401649561SJames Smart 	 * cleared when the reset is complete and rings reestablished.
191501649561SJames Smart 	 */
191678e9e350SJustin Tee 	spin_lock(&phba->hbalock);
191701649561SJames Smart 	/* driver queued commands are in process of being flushed */
1918c00f62e6SJames Smart 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
191978e9e350SJustin Tee 		spin_unlock(&phba->hbalock);
192078e9e350SJustin Tee 		spin_unlock_irqrestore(&lpfc_nbuf->buf_lock, flags);
1921372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
192201649561SJames Smart 				 "6139 Driver in reset cleanup - flushing "
192301649561SJames Smart 				 "NVME Req now.  hba_flag x%x\n",
192401649561SJames Smart 				 phba->hba_flag);
192501649561SJames Smart 		return;
192601649561SJames Smart 	}
192701649561SJames Smart 
19282b7824d0SJames Smart 	nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
192901649561SJames Smart 
193001649561SJames Smart 	/*
193101649561SJames Smart 	 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
193201649561SJames Smart 	 * state must match the nvme_fcreq passed by the nvme
193301649561SJames Smart 	 * transport.  If they don't match, it is likely the driver
193401649561SJames Smart 	 * has already completed the NVME IO and the nvme transport
193501649561SJames Smart 	 * has not seen it yet.
193601649561SJames Smart 	 */
193701649561SJames Smart 	if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
1938372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
193901649561SJames Smart 				 "6143 NVME req mismatch: "
194032350664SJames Smart 				 "lpfc_nbuf x%px nvmeCmd x%px, "
194132350664SJames Smart 				 "pnvme_fcreq x%px.  Skipping Abort xri x%x\n",
194201649561SJames Smart 				 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
19432b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1944c2017260SJames Smart 		goto out_unlock;
194501649561SJames Smart 	}
194601649561SJames Smart 
194701649561SJames Smart 	/* Don't abort IOs no longer on the pending queue. */
1948a680a929SJames Smart 	if (!(nvmereq_wqe->cmd_flag & LPFC_IO_ON_TXCMPLQ)) {
1949372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
195032350664SJames Smart 				 "6142 NVME IO req x%px not queued - skipping "
19512b7824d0SJames Smart 				 "abort req xri x%x\n",
19522b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1953c2017260SJames Smart 		goto out_unlock;
195401649561SJames Smart 	}
195501649561SJames Smart 
19564b056682SJames Smart 	atomic_inc(&lport->xmt_fcp_abort);
1957bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1958bd2cdd5eSJames Smart 			 nvmereq_wqe->sli4_xritag,
195900cefeb9SJames Smart 			 nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
1960bd2cdd5eSJames Smart 
196101649561SJames Smart 	/* Outstanding abort is in progress */
1962a680a929SJames Smart 	if (nvmereq_wqe->cmd_flag & LPFC_DRIVER_ABORTED) {
1963372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
196401649561SJames Smart 				 "6144 Outstanding NVME I/O Abort Request "
196532350664SJames Smart 				 "still pending on nvme_fcreq x%px, "
1966f1156125SJames Smart 				 "lpfc_ncmd x%px xri x%x\n",
19672b7824d0SJames Smart 				 pnvme_fcreq, lpfc_nbuf,
19682b7824d0SJames Smart 				 nvmereq_wqe->sli4_xritag);
1969c2017260SJames Smart 		goto out_unlock;
197001649561SJames Smart 	}
197101649561SJames Smart 
1972db7531d2SJames Smart 	ret_val = lpfc_sli4_issue_abort_iotag(phba, nvmereq_wqe,
1973db7531d2SJames Smart 					      lpfc_nvme_abort_fcreq_cmpl);
197401649561SJames Smart 
197578e9e350SJustin Tee 	spin_unlock(&phba->hbalock);
197678e9e350SJustin Tee 	spin_unlock_irqrestore(&lpfc_nbuf->buf_lock, flags);
1977a22d73b6SJames Smart 
1978a22d73b6SJames Smart 	/* Make sure HBA is alive */
1979a22d73b6SJames Smart 	lpfc_issue_hb_tmo(phba);
1980a22d73b6SJames Smart 
1981db7531d2SJames Smart 	if (ret_val != WQE_SUCCESS) {
1982372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
198301649561SJames Smart 				 "6137 Failed abts issue_wqe with status x%x "
198432350664SJames Smart 				 "for nvme_fcreq x%px.\n",
198501649561SJames Smart 				 ret_val, pnvme_fcreq);
198601649561SJames Smart 		return;
198701649561SJames Smart 	}
198801649561SJames Smart 
198986c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
19902b7824d0SJames Smart 			 "6138 Transport Abort NVME Request Issued for "
19911a5cd3d0SJustin Tee 			 "ox_id x%x\n",
19921a5cd3d0SJustin Tee 			 nvmereq_wqe->sli4_xritag);
1993c2017260SJames Smart 	return;
1994c2017260SJames Smart 
1995c2017260SJames Smart out_unlock:
199678e9e350SJustin Tee 	spin_unlock(&phba->hbalock);
199778e9e350SJustin Tee 	spin_unlock_irqrestore(&lpfc_nbuf->buf_lock, flags);
1998c2017260SJames Smart 	return;
199901649561SJames Smart }
200001649561SJames Smart 
200101649561SJames Smart /* Declare and initialization an instance of the FC NVME template. */
200201649561SJames Smart static struct nvme_fc_port_template lpfc_nvme_template = {
200301649561SJames Smart 	/* initiator-based functions */
200401649561SJames Smart 	.localport_delete  = lpfc_nvme_localport_delete,
200501649561SJames Smart 	.remoteport_delete = lpfc_nvme_remoteport_delete,
200601649561SJames Smart 	.create_queue = lpfc_nvme_create_queue,
200701649561SJames Smart 	.delete_queue = lpfc_nvme_delete_queue,
200801649561SJames Smart 	.ls_req       = lpfc_nvme_ls_req,
200901649561SJames Smart 	.fcp_io       = lpfc_nvme_fcp_io_submit,
201001649561SJames Smart 	.ls_abort     = lpfc_nvme_ls_abort,
201101649561SJames Smart 	.fcp_abort    = lpfc_nvme_fcp_abort,
20129aa09e98SJames Smart 	.xmt_ls_rsp   = lpfc_nvme_xmt_ls_rsp,
201301649561SJames Smart 
201401649561SJames Smart 	.max_hw_queues = 1,
201501649561SJames Smart 	.max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
201601649561SJames Smart 	.max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
201701649561SJames Smart 	.dma_boundary = 0xFFFFFFFF,
201801649561SJames Smart 
201901649561SJames Smart 	/* Sizes of additional private data for data structures.
202001649561SJames Smart 	 * No use for the last two sizes at this time.
202101649561SJames Smart 	 */
202201649561SJames Smart 	.local_priv_sz = sizeof(struct lpfc_nvme_lport),
202301649561SJames Smart 	.remote_priv_sz = sizeof(struct lpfc_nvme_rport),
202401649561SJames Smart 	.lsrqst_priv_sz = 0,
2025bbe3012bSJames Smart 	.fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
202601649561SJames Smart };
202701649561SJames Smart 
20283e749170SLee Jones /*
20295e5b511dSJames Smart  * lpfc_get_nvme_buf - Get a nvme buffer from io_buf_list of the HBA
203001649561SJames Smart  *
20315e5b511dSJames Smart  * This routine removes a nvme buffer from head of @hdwq io_buf_list
203201649561SJames Smart  * and returns to caller.
203301649561SJames Smart  *
203401649561SJames Smart  * Return codes:
203501649561SJames Smart  *   NULL - Error
203601649561SJames Smart  *   Pointer to lpfc_nvme_buf - Success
203701649561SJames Smart  **/
2038c490850aSJames Smart static struct lpfc_io_buf *
lpfc_get_nvme_buf(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,int idx,int expedite)2039cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
20405e5b511dSJames Smart 		  int idx, int expedite)
204101649561SJames Smart {
2042c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
20435e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
20440794d601SJames Smart 	struct sli4_sge *sgl;
20450794d601SJames Smart 	struct lpfc_iocbq *pwqeq;
20460794d601SJames Smart 	union lpfc_wqe128 *wqe;
204701649561SJames Smart 
2048c490850aSJames Smart 	lpfc_ncmd = lpfc_get_io_buf(phba, NULL, idx, expedite);
20492a5b7d62SJames Smart 
20500794d601SJames Smart 	if (lpfc_ncmd) {
20510794d601SJames Smart 		pwqeq = &(lpfc_ncmd->cur_iocbq);
20520794d601SJames Smart 		wqe = &pwqeq->wqe;
20530794d601SJames Smart 
20540794d601SJames Smart 		/* Setup key fields in buffer that may have been changed
20550794d601SJames Smart 		 * if other protocols used this buffer.
20560794d601SJames Smart 		 */
2057a680a929SJames Smart 		pwqeq->cmd_flag = LPFC_IO_NVME;
2058a680a929SJames Smart 		pwqeq->cmd_cmpl = lpfc_nvme_io_cmd_cmpl;
20590794d601SJames Smart 		lpfc_ncmd->start_time = jiffies;
20600794d601SJames Smart 		lpfc_ncmd->flags = 0;
20610794d601SJames Smart 
20620794d601SJames Smart 		/* Rsp SGE will be filled in when we rcv an IO
20630794d601SJames Smart 		 * from the NVME Layer to be sent.
20640794d601SJames Smart 		 * The cmd is going to be embedded so we need a SKIP SGE.
20650794d601SJames Smart 		 */
20660794d601SJames Smart 		sgl = lpfc_ncmd->dma_sgl;
20670794d601SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
20680794d601SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
20690794d601SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
20700794d601SJames Smart 		/* Fill in word 3 / sgl_len during cmd submission */
20710794d601SJames Smart 
2072d9f492a1SJames Smart 		/* Initialize 64 bytes only */
20730794d601SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
20740794d601SJames Smart 
20750794d601SJames Smart 		if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
20762a5b7d62SJames Smart 			atomic_inc(&ndlp->cmd_pending);
2077c490850aSJames Smart 			lpfc_ncmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
20782a5b7d62SJames Smart 		}
20795e5b511dSJames Smart 
2080c490850aSJames Smart 	} else {
2081c490850aSJames Smart 		qp = &phba->sli4_hba.hdwq[idx];
20825e5b511dSJames Smart 		qp->empty_io_bufs++;
2083c490850aSJames Smart 	}
20845e5b511dSJames Smart 
208501649561SJames Smart 	return  lpfc_ncmd;
208601649561SJames Smart }
208701649561SJames Smart 
208801649561SJames Smart /**
208901649561SJames Smart  * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
209001649561SJames Smart  * @phba: The Hba for which this call is being executed.
209101649561SJames Smart  * @lpfc_ncmd: The nvme buffer which is being released.
209201649561SJames Smart  *
209301649561SJames Smart  * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
20945e5b511dSJames Smart  * lpfc_io_buf_list list. For SLI4 XRI's are tied to the nvme buffer
209501649561SJames Smart  * and cannot be reused for at least RA_TOV amount of time if it was
209601649561SJames Smart  * aborted.
209701649561SJames Smart  **/
209801649561SJames Smart static void
lpfc_release_nvme_buf(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_ncmd)2099c490850aSJames Smart lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd)
210001649561SJames Smart {
21015e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
210201649561SJames Smart 	unsigned long iflag = 0;
210301649561SJames Smart 
2104c490850aSJames Smart 	if ((lpfc_ncmd->flags & LPFC_SBUF_BUMP_QDEPTH) && lpfc_ncmd->ndlp)
21052a5b7d62SJames Smart 		atomic_dec(&lpfc_ncmd->ndlp->cmd_pending);
21062a5b7d62SJames Smart 
21072a5b7d62SJames Smart 	lpfc_ncmd->ndlp = NULL;
2108c490850aSJames Smart 	lpfc_ncmd->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
21092a5b7d62SJames Smart 
21101fbf9742SJames Smart 	qp = lpfc_ncmd->hdwq;
211122770cbaSJames Smart 	if (unlikely(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
211286c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
211386c67379SJames Smart 				"6310 XB release deferred for "
211486c67379SJames Smart 				"ox_id x%x on reqtag x%x\n",
211586c67379SJames Smart 				lpfc_ncmd->cur_iocbq.sli4_xritag,
211686c67379SJames Smart 				lpfc_ncmd->cur_iocbq.iotag);
211786c67379SJames Smart 
2118c00f62e6SJames Smart 		spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
211901649561SJames Smart 		list_add_tail(&lpfc_ncmd->list,
2120c00f62e6SJames Smart 			&qp->lpfc_abts_io_buf_list);
21215e5b511dSJames Smart 		qp->abts_nvme_io_bufs++;
2122c00f62e6SJames Smart 		spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
2123c490850aSJames Smart 	} else
2124c490850aSJames Smart 		lpfc_release_io_buf(phba, (struct lpfc_io_buf *)lpfc_ncmd, qp);
212501649561SJames Smart }
212601649561SJames Smart 
212701649561SJames Smart /**
212801649561SJames Smart  * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
2129f6b35a75SLee Jones  * @vport: the lpfc_vport instance requesting a localport.
213001649561SJames Smart  *
213101649561SJames Smart  * This routine is invoked to create an nvme localport instance to bind
213201649561SJames Smart  * to the nvme_fc_transport.  It is called once during driver load
213301649561SJames Smart  * like lpfc_create_shost after all other services are initialized.
213401649561SJames Smart  * It requires a vport, vpi, and wwns at call time.  Other localport
213501649561SJames Smart  * parameters are modified as the driver's FCID and the Fabric WWN
213601649561SJames Smart  * are established.
213701649561SJames Smart  *
213801649561SJames Smart  * Return codes
213901649561SJames Smart  *      0 - successful
214001649561SJames Smart  *      -ENOMEM - no heap memory available
214101649561SJames Smart  *      other values - from nvme registration upcall
214201649561SJames Smart  **/
214301649561SJames Smart int
lpfc_nvme_create_localport(struct lpfc_vport * vport)214401649561SJames Smart lpfc_nvme_create_localport(struct lpfc_vport *vport)
214501649561SJames Smart {
2146166d7211SJames Smart 	int ret = 0;
214701649561SJames Smart 	struct lpfc_hba  *phba = vport->phba;
214801649561SJames Smart 	struct nvme_fc_port_info nfcp_info;
214901649561SJames Smart 	struct nvme_fc_local_port *localport;
215001649561SJames Smart 	struct lpfc_nvme_lport *lport;
215101649561SJames Smart 
215201649561SJames Smart 	/* Initialize this localport instance.  The vport wwn usage ensures
215301649561SJames Smart 	 * that NPIV is accounted for.
215401649561SJames Smart 	 */
215501649561SJames Smart 	memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
215601649561SJames Smart 	nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
215701649561SJames Smart 	nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
215801649561SJames Smart 	nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
215901649561SJames Smart 
21605b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
21615b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
21625b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
21634d4c4a4aSJames Smart 	 */
21644d4c4a4aSJames Smart 	lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
216501649561SJames Smart 
2166542ddc9bSJames Smart 	/* Advertise how many hw queues we support based on cfg_hdw_queue,
2167542ddc9bSJames Smart 	 * which will not exceed cpu count.
2168542ddc9bSJames Smart 	 */
2169cdb42becSJames Smart 	lpfc_nvme_template.max_hw_queues = phba->cfg_hdw_queue;
217066a210ffSJames Smart 
2171faf5a744SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_FC))
2172faf5a744SArnd Bergmann 		return ret;
2173faf5a744SArnd Bergmann 
217401649561SJames Smart 	/* localport is allocated from the stack, but the registration
217501649561SJames Smart 	 * call allocates heap memory as well as the private area.
217601649561SJames Smart 	 */
2177faf5a744SArnd Bergmann 
217801649561SJames Smart 	ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
217901649561SJames Smart 					 &vport->phba->pcidev->dev, &localport);
218001649561SJames Smart 	if (!ret) {
218101649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
218201649561SJames Smart 				 "6005 Successfully registered local "
218332350664SJames Smart 				 "NVME port num %d, localP x%px, private "
218432350664SJames Smart 				 "x%px, sg_seg %d\n",
218501649561SJames Smart 				 localport->port_num, localport,
218601649561SJames Smart 				 localport->private,
218701649561SJames Smart 				 lpfc_nvme_template.max_sgl_segments);
218801649561SJames Smart 
218901649561SJames Smart 		/* Private is our lport size declared in the template. */
219001649561SJames Smart 		lport = (struct lpfc_nvme_lport *)localport->private;
219101649561SJames Smart 		vport->localport = localport;
219201649561SJames Smart 		lport->vport = vport;
219301649561SJames Smart 		vport->nvmei_support = 1;
21946b486ce9SDick Kennedy 
21954b056682SJames Smart 		atomic_set(&lport->xmt_fcp_noxri, 0);
21964b056682SJames Smart 		atomic_set(&lport->xmt_fcp_bad_ndlp, 0);
21974b056682SJames Smart 		atomic_set(&lport->xmt_fcp_qdepth, 0);
219844c2757bSJames Smart 		atomic_set(&lport->xmt_fcp_err, 0);
21994b056682SJames Smart 		atomic_set(&lport->xmt_fcp_wqerr, 0);
22004b056682SJames Smart 		atomic_set(&lport->xmt_fcp_abort, 0);
22014b056682SJames Smart 		atomic_set(&lport->xmt_ls_abort, 0);
22024b056682SJames Smart 		atomic_set(&lport->xmt_ls_err, 0);
22034b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_xb, 0);
22044b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_err, 0);
22054b056682SJames Smart 		atomic_set(&lport->cmpl_ls_xb, 0);
22064b056682SJames Smart 		atomic_set(&lport->cmpl_ls_err, 0);
22079aa09e98SJames Smart 
220866a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsRequests, 0);
220966a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsCmpls, 0);
22106b486ce9SDick Kennedy 	}
22113ebd9b47SJames Smart 
221201649561SJames Smart 	return ret;
221301649561SJames Smart }
221401649561SJames Smart 
2215d964b3e5SBart Van Assche #if (IS_ENABLED(CONFIG_NVME_FC))
2216add9d6beSJames Smart /* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2217add9d6beSJames Smart  *
2218add9d6beSJames Smart  * The driver has to wait for the host nvme transport to callback
2219add9d6beSJames Smart  * indicating the localport has successfully unregistered all
2220add9d6beSJames Smart  * resources.  Since this is an uninterruptible wait, loop every ten
2221add9d6beSJames Smart  * seconds and print a message indicating no progress.
2222add9d6beSJames Smart  *
2223add9d6beSJames Smart  * An uninterruptible wait is used because of the risk of transport-to-
2224add9d6beSJames Smart  * driver state mismatch.
2225add9d6beSJames Smart  */
22263999df75SBart Van Assche static void
lpfc_nvme_lport_unreg_wait(struct lpfc_vport * vport,struct lpfc_nvme_lport * lport,struct completion * lport_unreg_cmp)2227add9d6beSJames Smart lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
22287961cba6SEwan D. Milne 			   struct lpfc_nvme_lport *lport,
22297961cba6SEwan D. Milne 			   struct completion *lport_unreg_cmp)
2230add9d6beSJames Smart {
2231add9d6beSJames Smart 	u32 wait_tmo;
22322ab70c21SJames Smart 	int ret, i, pending = 0;
22332ab70c21SJames Smart 	struct lpfc_sli_ring  *pring;
22342ab70c21SJames Smart 	struct lpfc_hba  *phba = vport->phba;
2235e7dab164SJames Smart 	struct lpfc_sli4_hdw_queue *qp;
2236e7dab164SJames Smart 	int abts_scsi, abts_nvme;
2237add9d6beSJames Smart 
2238add9d6beSJames Smart 	/* Host transport has to clean up and confirm requiring an indefinite
2239add9d6beSJames Smart 	 * wait. Print a message if a 10 second wait expires and renew the
2240add9d6beSJames Smart 	 * wait. This is unexpected.
2241add9d6beSJames Smart 	 */
2242add9d6beSJames Smart 	wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2243add9d6beSJames Smart 	while (true) {
22447961cba6SEwan D. Milne 		ret = wait_for_completion_timeout(lport_unreg_cmp, wait_tmo);
2245add9d6beSJames Smart 		if (unlikely(!ret)) {
22462ab70c21SJames Smart 			pending = 0;
2247e7dab164SJames Smart 			abts_scsi = 0;
2248e7dab164SJames Smart 			abts_nvme = 0;
22492ab70c21SJames Smart 			for (i = 0; i < phba->cfg_hdw_queue; i++) {
2250e7dab164SJames Smart 				qp = &phba->sli4_hba.hdwq[i];
225135ed9613SJames Smart 				if (!vport->localport || !qp || !qp->io_wq)
225225ac2c97SJames Smart 					return;
225325ac2c97SJames Smart 
2254e7dab164SJames Smart 				pring = qp->io_wq->pring;
22552ab70c21SJames Smart 				if (!pring)
22562ab70c21SJames Smart 					continue;
22572ab70c21SJames Smart 				pending += pring->txcmplq_cnt;
2258e7dab164SJames Smart 				abts_scsi += qp->abts_scsi_io_bufs;
2259e7dab164SJames Smart 				abts_nvme += qp->abts_nvme_io_bufs;
22602ab70c21SJames Smart 			}
226135ed9613SJames Smart 			if (!vport->localport ||
226235ed9613SJames Smart 			    test_bit(HBA_PCI_ERR, &vport->phba->bit_flags) ||
226327c2bcf0SJustin Tee 			    phba->link_state == LPFC_HBA_ERROR ||
226435ed9613SJames Smart 			    vport->load_flag & FC_UNLOADING)
226525ac2c97SJames Smart 				return;
226625ac2c97SJames Smart 
2267372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
226832350664SJames Smart 					 "6176 Lport x%px Localport x%px wait "
2269e7dab164SJames Smart 					 "timed out. Pending %d [%d:%d]. "
2270e7dab164SJames Smart 					 "Renewing.\n",
2271e7dab164SJames Smart 					 lport, vport->localport, pending,
2272e7dab164SJames Smart 					 abts_scsi, abts_nvme);
2273add9d6beSJames Smart 			continue;
2274add9d6beSJames Smart 		}
2275add9d6beSJames Smart 		break;
2276add9d6beSJames Smart 	}
2277add9d6beSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
227832350664SJames Smart 			 "6177 Lport x%px Localport x%px Complete Success\n",
2279add9d6beSJames Smart 			 lport, vport->localport);
2280add9d6beSJames Smart }
2281d964b3e5SBart Van Assche #endif
2282add9d6beSJames Smart 
228301649561SJames Smart /**
228401649561SJames Smart  * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
22853e749170SLee Jones  * @vport: pointer to a host virtual N_Port data structure
228601649561SJames Smart  *
228701649561SJames Smart  * This routine is invoked to destroy all lports bound to the phba.
228801649561SJames Smart  * The lport memory was allocated by the nvme fc transport and is
228901649561SJames Smart  * released there.  This routine ensures all rports bound to the
229001649561SJames Smart  * lport have been disconnected.
229101649561SJames Smart  *
229201649561SJames Smart  **/
229301649561SJames Smart void
lpfc_nvme_destroy_localport(struct lpfc_vport * vport)229401649561SJames Smart lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
229501649561SJames Smart {
22967d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
229701649561SJames Smart 	struct nvme_fc_local_port *localport;
229801649561SJames Smart 	struct lpfc_nvme_lport *lport;
229901649561SJames Smart 	int ret;
23007961cba6SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(lport_unreg_cmp);
230101649561SJames Smart 
230201649561SJames Smart 	if (vport->nvmei_support == 0)
230301649561SJames Smart 		return;
230401649561SJames Smart 
230501649561SJames Smart 	localport = vport->localport;
230625ac2c97SJames Smart 	if (!localport)
230725ac2c97SJames Smart 		return;
230801649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
230901649561SJames Smart 
231001649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
231132350664SJames Smart 			 "6011 Destroying NVME localport x%px\n",
231201649561SJames Smart 			 localport);
2313166d7211SJames Smart 
231401649561SJames Smart 	/* lport's rport list is clear.  Unregister
231501649561SJames Smart 	 * lport and release resources.
231601649561SJames Smart 	 */
23177961cba6SEwan D. Milne 	lport->lport_unreg_cmp = &lport_unreg_cmp;
231801649561SJames Smart 	ret = nvme_fc_unregister_localport(localport);
2319add9d6beSJames Smart 
2320add9d6beSJames Smart 	/* Wait for completion.  This either blocks
2321add9d6beSJames Smart 	 * indefinitely or succeeds
2322add9d6beSJames Smart 	 */
23237961cba6SEwan D. Milne 	lpfc_nvme_lport_unreg_wait(vport, lport, &lport_unreg_cmp);
23247961cba6SEwan D. Milne 	vport->localport = NULL;
232501649561SJames Smart 
232601649561SJames Smart 	/* Regardless of the unregister upcall response, clear
232701649561SJames Smart 	 * nvmei_support.  All rports are unregistered and the
232801649561SJames Smart 	 * driver will clean up.
232901649561SJames Smart 	 */
233001649561SJames Smart 	vport->nvmei_support = 0;
233101649561SJames Smart 	if (ret == 0) {
233201649561SJames Smart 		lpfc_printf_vlog(vport,
233301649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
233401649561SJames Smart 				 "6009 Unregistered lport Success\n");
233501649561SJames Smart 	} else {
233601649561SJames Smart 		lpfc_printf_vlog(vport,
233701649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
233801649561SJames Smart 				 "6010 Unregistered lport "
233901649561SJames Smart 				 "Failed, status x%x\n",
234001649561SJames Smart 				 ret);
234101649561SJames Smart 	}
2342166d7211SJames Smart #endif
234301649561SJames Smart }
234401649561SJames Smart 
234501649561SJames Smart void
lpfc_nvme_update_localport(struct lpfc_vport * vport)234601649561SJames Smart lpfc_nvme_update_localport(struct lpfc_vport *vport)
234701649561SJames Smart {
23484410a67aSJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
234901649561SJames Smart 	struct nvme_fc_local_port *localport;
235001649561SJames Smart 	struct lpfc_nvme_lport *lport;
235101649561SJames Smart 
235201649561SJames Smart 	localport = vport->localport;
23534410a67aSJames Smart 	if (!localport) {
23544410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
23554410a67aSJames Smart 				 "6710 Update NVME fail. No localport\n");
23564410a67aSJames Smart 		return;
23574410a67aSJames Smart 	}
235801649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
23594410a67aSJames Smart 	if (!lport) {
23604410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
236132350664SJames Smart 				 "6171 Update NVME fail. localP x%px, No lport\n",
23624410a67aSJames Smart 				 localport);
23634410a67aSJames Smart 		return;
23644410a67aSJames Smart 	}
236501649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
236632350664SJames Smart 			 "6012 Update NVME lport x%px did x%x\n",
236701649561SJames Smart 			 localport, vport->fc_myDID);
236801649561SJames Smart 
236901649561SJames Smart 	localport->port_id = vport->fc_myDID;
237001649561SJames Smart 	if (localport->port_id == 0)
237101649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
237201649561SJames Smart 	else
237301649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
237401649561SJames Smart 
237501649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
237632350664SJames Smart 			 "6030 bound lport x%px to DID x%06x\n",
237701649561SJames Smart 			 lport, localport->port_id);
23784410a67aSJames Smart #endif
237901649561SJames Smart }
238001649561SJames Smart 
238101649561SJames Smart int
lpfc_nvme_register_port(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)238201649561SJames Smart lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
238301649561SJames Smart {
23847d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
238501649561SJames Smart 	int ret = 0;
238601649561SJames Smart 	struct nvme_fc_local_port *localport;
238701649561SJames Smart 	struct lpfc_nvme_lport *lport;
238801649561SJames Smart 	struct lpfc_nvme_rport *rport;
238901466024SJames Smart 	struct lpfc_nvme_rport *oldrport;
239001649561SJames Smart 	struct nvme_fc_remote_port *remote_port;
239101649561SJames Smart 	struct nvme_fc_port_info rpinfo;
239293a3922dSJames Smart 	struct lpfc_nodelist *prev_ndlp = NULL;
23931f546823SJames Smart 	struct fc_rport *srport = ndlp->rport;
239401649561SJames Smart 
239501649561SJames Smart 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
239601649561SJames Smart 			 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
239701649561SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type);
239801649561SJames Smart 
239901649561SJames Smart 	localport = vport->localport;
2400bb6a8a2cSDick Kennedy 	if (!localport)
2401bb6a8a2cSDick Kennedy 		return 0;
2402bb6a8a2cSDick Kennedy 
240301649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
240401649561SJames Smart 
24057a06dcd3SJames Smart 	/* NVME rports are not preserved across devloss.
24067a06dcd3SJames Smart 	 * Just register this instance.  Note, rpinfo->dev_loss_tmo
24077a06dcd3SJames Smart 	 * is left 0 to indicate accept transport defaults.  The
24087a06dcd3SJames Smart 	 * driver communicates port role capabilities consistent
24097a06dcd3SJames Smart 	 * with the PRLI response data.
241001649561SJames Smart 	 */
24117a06dcd3SJames Smart 	memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
24127a06dcd3SJames Smart 	rpinfo.port_id = ndlp->nlp_DID;
241301649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET)
24147a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
241501649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
24167a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
241701649561SJames Smart 
24187a06dcd3SJames Smart 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
24197a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
24207a06dcd3SJames Smart 
24217a06dcd3SJames Smart 	rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
24227a06dcd3SJames Smart 	rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
24231f546823SJames Smart 	if (srport)
24241f546823SJames Smart 		rpinfo.dev_loss_tmo = srport->dev_loss_tmo;
24251f546823SJames Smart 	else
24261f546823SJames Smart 		rpinfo.dev_loss_tmo = vport->cfg_devloss_tmo;
242701466024SJames Smart 
2428c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
2429ba3d58a1SJames Smart 
2430ba3d58a1SJames Smart 	/* If an oldrport exists, so does the ndlp reference.  If not
2431ba3d58a1SJames Smart 	 * a new reference is needed because either the node has never
2432ba3d58a1SJames Smart 	 * been registered or it's been unregistered and getting deleted.
2433ba3d58a1SJames Smart 	 */
243401466024SJames Smart 	oldrport = lpfc_ndlp_get_nrport(ndlp);
243561184f17SJames Smart 	if (oldrport) {
243661184f17SJames Smart 		prev_ndlp = oldrport->ndlp;
2437c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
243861184f17SJames Smart 	} else {
2439c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
24404430f7fdSJames Smart 		if (!lpfc_nlp_get(ndlp)) {
24414430f7fdSJames Smart 			dev_warn(&vport->phba->pcidev->dev,
24424430f7fdSJames Smart 				 "Warning - No node ref - exit register\n");
24434430f7fdSJames Smart 			return 0;
24444430f7fdSJames Smart 		}
244561184f17SJames Smart 	}
24463fd78355SJames Smart 
24477a06dcd3SJames Smart 	ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
24487a06dcd3SJames Smart 	if (!ret) {
24497a06dcd3SJames Smart 		/* If the ndlp already has an nrport, this is just
24507a06dcd3SJames Smart 		 * a resume of the existing rport.  Else this is a
24517a06dcd3SJames Smart 		 * new rport.
24527a06dcd3SJames Smart 		 */
2453b15bd3e6SJames Smart 		/* Guard against an unregister/reregister
2454b15bd3e6SJames Smart 		 * race that leaves the WAIT flag set.
2455b15bd3e6SJames Smart 		 */
2456c6adba15SJames Smart 		spin_lock_irq(&ndlp->lock);
245706145683SJames Smart 		ndlp->fc4_xpt_flags &= ~NVME_XPT_UNREG_WAIT;
2458e9b11083SJames Smart 		ndlp->fc4_xpt_flags |= NVME_XPT_REGD;
2459c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
24607a06dcd3SJames Smart 		rport = remote_port->private;
246101466024SJames Smart 		if (oldrport) {
24622b75d0f9SDick Kennedy 
24633fd78355SJames Smart 			/* Sever the ndlp<->rport association
24643fd78355SJames Smart 			 * before dropping the ndlp ref from
24653fd78355SJames Smart 			 * register.
24662b75d0f9SDick Kennedy 			 */
2467c6adba15SJames Smart 			spin_lock_irq(&ndlp->lock);
24682b75d0f9SDick Kennedy 			ndlp->nrport = NULL;
246906145683SJames Smart 			ndlp->fc4_xpt_flags &= ~NVME_XPT_UNREG_WAIT;
2470c6adba15SJames Smart 			spin_unlock_irq(&ndlp->lock);
24712b75d0f9SDick Kennedy 			rport->ndlp = NULL;
24723fd78355SJames Smart 			rport->remoteport = NULL;
2473b04744ceSJames Smart 
2474b04744ceSJames Smart 			/* Reference only removed if previous NDLP is no longer
2475b04744ceSJames Smart 			 * active. It might be just a swap and removing the
2476b04744ceSJames Smart 			 * reference would cause a premature cleanup.
2477b04744ceSJames Smart 			 */
2478b04744ceSJames Smart 			if (prev_ndlp && prev_ndlp != ndlp) {
2479307e3380SJames Smart 				if (!prev_ndlp->nrport)
2480b04744ceSJames Smart 					lpfc_nlp_put(prev_ndlp);
2481b04744ceSJames Smart 			}
24822b75d0f9SDick Kennedy 		}
24832b75d0f9SDick Kennedy 
24842b75d0f9SDick Kennedy 		/* Clean bind the rport to the ndlp. */
248501649561SJames Smart 		rport->remoteport = remote_port;
248601649561SJames Smart 		rport->lport = lport;
24873fd78355SJames Smart 		rport->ndlp = ndlp;
2488c6adba15SJames Smart 		spin_lock_irq(&ndlp->lock);
248901649561SJames Smart 		ndlp->nrport = rport;
2490c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
249101649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO,
249201649561SJames Smart 				 LOG_NVME_DISC | LOG_NODE,
24936825b7bdSJames Smart 				 "6022 Bind lport x%px to remoteport x%px "
24946825b7bdSJames Smart 				 "rport x%px WWNN 0x%llx, "
24957a06dcd3SJames Smart 				 "Rport WWPN 0x%llx DID "
249632350664SJames Smart 				 "x%06x Role x%x, ndlp %p prev_ndlp x%px\n",
249793a3922dSJames Smart 				 lport, remote_port, rport,
249801649561SJames Smart 				 rpinfo.node_name, rpinfo.port_name,
24993fd78355SJames Smart 				 rpinfo.port_id, rpinfo.port_role,
250093a3922dSJames Smart 				 ndlp, prev_ndlp);
250101649561SJames Smart 	} else {
250201649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR,
2503372c187bSDick Kennedy 				 LOG_TRACE_EVENT,
250401649561SJames Smart 				 "6031 RemotePort Registration failed "
250590cec07fSJustin Tee 				 "err: %d, DID x%06x ref %u\n",
250690cec07fSJustin Tee 				 ret, ndlp->nlp_DID, kref_read(&ndlp->kref));
250790cec07fSJustin Tee 		lpfc_nlp_put(ndlp);
250801649561SJames Smart 	}
25097a06dcd3SJames Smart 
251001649561SJames Smart 	return ret;
2511166d7211SJames Smart #else
2512166d7211SJames Smart 	return 0;
2513166d7211SJames Smart #endif
251401649561SJames Smart }
251501649561SJames Smart 
25163e749170SLee Jones /*
25176f2589f4SJames Smart  * lpfc_nvme_rescan_port - Check to see if we should rescan this remoteport
25186f2589f4SJames Smart  *
25196f2589f4SJames Smart  * If the ndlp represents an NVME Target, that we are logged into,
25206f2589f4SJames Smart  * ping the NVME FC Transport layer to initiate a device rescan
25216f2589f4SJames Smart  * on this remote NPort.
25226f2589f4SJames Smart  */
25236f2589f4SJames Smart void
lpfc_nvme_rescan_port(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)25246f2589f4SJames Smart lpfc_nvme_rescan_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
25256f2589f4SJames Smart {
25266f2589f4SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2527a6d10f24SJames Smart 	struct lpfc_nvme_rport *nrport;
2528a6d10f24SJames Smart 	struct nvme_fc_remote_port *remoteport = NULL;
25296f2589f4SJames Smart 
2530c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
2531a6d10f24SJames Smart 	nrport = lpfc_ndlp_get_nrport(ndlp);
2532a6d10f24SJames Smart 	if (nrport)
2533a6d10f24SJames Smart 		remoteport = nrport->remoteport;
2534c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
25356f2589f4SJames Smart 
25366f2589f4SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
25376f2589f4SJames Smart 			 "6170 Rescan NPort DID x%06x type x%x "
2538a6d10f24SJames Smart 			 "state x%x nrport x%px remoteport x%px\n",
2539a6d10f24SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type, ndlp->nlp_state,
2540a6d10f24SJames Smart 			 nrport, remoteport);
2541a6d10f24SJames Smart 
2542a6d10f24SJames Smart 	if (!nrport || !remoteport)
2543a6d10f24SJames Smart 		goto rescan_exit;
25446f2589f4SJames Smart 
2545b83a8c21SJames Smart 	/* Rescan an NVME target in MAPPED state with DISCOVERY role set */
25466f2589f4SJames Smart 	if (remoteport->port_role & FC_PORT_ROLE_NVME_DISCOVERY &&
25476f2589f4SJames Smart 	    ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
25486f2589f4SJames Smart 		nvme_fc_rescan_remoteport(remoteport);
25496f2589f4SJames Smart 
2550b83a8c21SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
25516f2589f4SJames Smart 				 "6172 NVME rescanned DID x%06x "
25526f2589f4SJames Smart 				 "port_state x%x\n",
25536f2589f4SJames Smart 				 ndlp->nlp_DID, remoteport->port_state);
25546f2589f4SJames Smart 	}
25556f2589f4SJames Smart 	return;
2556a6d10f24SJames Smart  rescan_exit:
2557a6d10f24SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2558a6d10f24SJames Smart 			 "6169 Skip NVME Rport Rescan, NVME remoteport "
2559a6d10f24SJames Smart 			 "unregistered\n");
25606f2589f4SJames Smart #endif
25616f2589f4SJames Smart }
25626f2589f4SJames Smart 
256301649561SJames Smart /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
256401649561SJames Smart  *
256501649561SJames Smart  * There is no notion of Devloss or rport recovery from the current
256601649561SJames Smart  * nvme_transport perspective.  Loss of an rport just means IO cannot
256701649561SJames Smart  * be sent and recovery is completely up to the initator.
256801649561SJames Smart  * For now, the driver just unbinds the DID and port_role so that
2569dae40be7SJustin Tee  * no further IO can be issued.
257001649561SJames Smart  */
257101649561SJames Smart void
lpfc_nvme_unregister_port(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)257201649561SJames Smart lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
257301649561SJames Smart {
25747d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
257501649561SJames Smart 	int ret;
257601649561SJames Smart 	struct nvme_fc_local_port *localport;
257701649561SJames Smart 	struct lpfc_nvme_lport *lport;
257801649561SJames Smart 	struct lpfc_nvme_rport *rport;
25799e210178SJames Smart 	struct nvme_fc_remote_port *remoteport = NULL;
258001649561SJames Smart 
258101649561SJames Smart 	localport = vport->localport;
258201649561SJames Smart 
258301649561SJames Smart 	/* This is fundamental error.  The localport is always
258401649561SJames Smart 	 * available until driver unload.  Just exit.
258501649561SJames Smart 	 */
258601649561SJames Smart 	if (!localport)
258701649561SJames Smart 		return;
258801649561SJames Smart 
258901649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
259001649561SJames Smart 	if (!lport)
259101649561SJames Smart 		goto input_err;
259201649561SJames Smart 
2593c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
259401466024SJames Smart 	rport = lpfc_ndlp_get_nrport(ndlp);
25959e210178SJames Smart 	if (rport)
25969e210178SJames Smart 		remoteport = rport->remoteport;
2597c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
25989e210178SJames Smart 	if (!remoteport)
259901649561SJames Smart 		goto input_err;
260001649561SJames Smart 
260101649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
260232350664SJames Smart 			 "6033 Unreg nvme remoteport x%px, portname x%llx, "
2603e9b11083SJames Smart 			 "port_id x%06x, portstate x%x port type x%x "
2604e9b11083SJames Smart 			 "refcnt %d\n",
260501649561SJames Smart 			 remoteport, remoteport->port_name,
260601649561SJames Smart 			 remoteport->port_id, remoteport->port_state,
2607e9b11083SJames Smart 			 ndlp->nlp_type, kref_read(&ndlp->kref));
260801649561SJames Smart 
260901649561SJames Smart 	/* Sanity check ndlp type.  Only call for NVME ports. Don't
261001649561SJames Smart 	 * clear any rport state until the transport calls back.
261101649561SJames Smart 	 */
26123b5bde69SJames Smart 
26133b5bde69SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET) {
26147a06dcd3SJames Smart 		/* No concern about the role change on the nvme remoteport.
26157a06dcd3SJames Smart 		 * The transport will update it.
26167a06dcd3SJames Smart 		 */
2617*8a1d8deaSJustin Tee 		spin_lock_irq(&ndlp->lock);
261806145683SJames Smart 		ndlp->fc4_xpt_flags |= NVME_XPT_UNREG_WAIT;
2619*8a1d8deaSJustin Tee 		spin_unlock_irq(&ndlp->lock);
26207438273fSJames Smart 
26217438273fSJames Smart 		/* Don't let the host nvme transport keep sending keep-alives
26227438273fSJames Smart 		 * on this remoteport. Vport is unloading, no recovery. The
26237438273fSJames Smart 		 * return values is ignored.  The upcall is a courtesy to the
26247438273fSJames Smart 		 * transport.
26257438273fSJames Smart 		 */
262627c2bcf0SJustin Tee 		if (vport->load_flag & FC_UNLOADING ||
262727c2bcf0SJustin Tee 		    unlikely(vport->phba->link_state == LPFC_HBA_ERROR))
26287438273fSJames Smart 			(void)nvme_fc_set_remoteport_devloss(remoteport, 0);
26297438273fSJames Smart 
263001649561SJames Smart 		ret = nvme_fc_unregister_remoteport(remoteport);
2631e9b11083SJames Smart 
2632e9b11083SJames Smart 		/* The driver no longer knows if the nrport memory is valid.
2633e9b11083SJames Smart 		 * because the controller teardown process has begun and
2634e9b11083SJames Smart 		 * is asynchronous.  Break the binding in the ndlp. Also
2635e9b11083SJames Smart 		 * remove the register ndlp reference to setup node release.
2636e9b11083SJames Smart 		 */
2637e9b11083SJames Smart 		ndlp->nrport = NULL;
26383fd78355SJames Smart 		lpfc_nlp_put(ndlp);
2639e9b11083SJames Smart 		if (ret != 0) {
2640372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
264101649561SJames Smart 					 "6167 NVME unregister failed %d "
264201649561SJames Smart 					 "port_state x%x\n",
264301649561SJames Smart 					 ret, remoteport->port_state);
2644dae40be7SJustin Tee 
2645dae40be7SJustin Tee 			if (vport->load_flag & FC_UNLOADING) {
2646dae40be7SJustin Tee 				/* Only 1 thread can drop the initial node
2647dae40be7SJustin Tee 				 * reference. Check if another thread has set
2648dae40be7SJustin Tee 				 * NLP_DROPPED.
2649dae40be7SJustin Tee 				 */
2650dae40be7SJustin Tee 				spin_lock_irq(&ndlp->lock);
2651dae40be7SJustin Tee 				if (!(ndlp->nlp_flag & NLP_DROPPED)) {
2652dae40be7SJustin Tee 					ndlp->nlp_flag |= NLP_DROPPED;
2653dae40be7SJustin Tee 					spin_unlock_irq(&ndlp->lock);
2654dae40be7SJustin Tee 					lpfc_nlp_put(ndlp);
2655dae40be7SJustin Tee 					return;
2656dae40be7SJustin Tee 				}
2657dae40be7SJustin Tee 				spin_unlock_irq(&ndlp->lock);
2658dae40be7SJustin Tee 			}
26593fd78355SJames Smart 		}
266001649561SJames Smart 	}
266101649561SJames Smart 	return;
266201649561SJames Smart 
266301649561SJames Smart  input_err:
2664166d7211SJames Smart #endif
2665372c187bSDick Kennedy 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
266632350664SJames Smart 			 "6168 State error: lport x%px, rport x%px FCID x%06x\n",
266701649561SJames Smart 			 vport->localport, ndlp->rport, ndlp->nlp_DID);
266801649561SJames Smart }
2669318083adSJames Smart 
2670318083adSJames Smart /**
267125ac2c97SJames Smart  * lpfc_sli4_nvme_pci_offline_aborted - Fast-path process of NVME xri abort
267225ac2c97SJames Smart  * @phba: pointer to lpfc hba data structure.
267325ac2c97SJames Smart  * @lpfc_ncmd: The nvme job structure for the request being aborted.
267425ac2c97SJames Smart  *
267525ac2c97SJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
267625ac2c97SJames Smart  * NVME aborted xri.  Aborted NVME IO commands are completed to the transport
267725ac2c97SJames Smart  * here.
267825ac2c97SJames Smart  **/
267925ac2c97SJames Smart void
lpfc_sli4_nvme_pci_offline_aborted(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_ncmd)268025ac2c97SJames Smart lpfc_sli4_nvme_pci_offline_aborted(struct lpfc_hba *phba,
268125ac2c97SJames Smart 				   struct lpfc_io_buf *lpfc_ncmd)
268225ac2c97SJames Smart {
268325ac2c97SJames Smart 	struct nvmefc_fcp_req *nvme_cmd = NULL;
268425ac2c97SJames Smart 
268525ac2c97SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
268625ac2c97SJames Smart 			"6533 %s nvme_cmd %p tag x%x abort complete and "
268725ac2c97SJames Smart 			"xri released\n", __func__,
268825ac2c97SJames Smart 			lpfc_ncmd->nvmeCmd,
268925ac2c97SJames Smart 			lpfc_ncmd->cur_iocbq.iotag);
269025ac2c97SJames Smart 
269125ac2c97SJames Smart 	/* Aborted NVME commands are required to not complete
269225ac2c97SJames Smart 	 * before the abort exchange command fully completes.
269325ac2c97SJames Smart 	 * Once completed, it is available via the put list.
269425ac2c97SJames Smart 	 */
269525ac2c97SJames Smart 	if (lpfc_ncmd->nvmeCmd) {
269625ac2c97SJames Smart 		nvme_cmd = lpfc_ncmd->nvmeCmd;
269725ac2c97SJames Smart 		nvme_cmd->transferred_length = 0;
269825ac2c97SJames Smart 		nvme_cmd->rcv_rsplen = 0;
269925ac2c97SJames Smart 		nvme_cmd->status = NVME_SC_INTERNAL;
270025ac2c97SJames Smart 		nvme_cmd->done(nvme_cmd);
270125ac2c97SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
270225ac2c97SJames Smart 	}
270325ac2c97SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
270425ac2c97SJames Smart }
270525ac2c97SJames Smart 
270625ac2c97SJames Smart /**
2707318083adSJames Smart  * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2708318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
2709318083adSJames Smart  * @axri: pointer to the fcp xri abort wcqe structure.
2710c00f62e6SJames Smart  * @lpfc_ncmd: The nvme job structure for the request being aborted.
2711318083adSJames Smart  *
2712318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
2713952c303bSDick Kennedy  * NVME aborted xri.  Aborted NVME IO commands are completed to the transport
2714952c303bSDick Kennedy  * here.
2715318083adSJames Smart  **/
2716318083adSJames Smart void
lpfc_sli4_nvme_xri_aborted(struct lpfc_hba * phba,struct sli4_wcqe_xri_aborted * axri,struct lpfc_io_buf * lpfc_ncmd)2717318083adSJames Smart lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2718c00f62e6SJames Smart 			   struct sli4_wcqe_xri_aborted *axri,
2719c00f62e6SJames Smart 			   struct lpfc_io_buf *lpfc_ncmd)
2720318083adSJames Smart {
2721318083adSJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
2722952c303bSDick Kennedy 	struct nvmefc_fcp_req *nvme_cmd = NULL;
2723c00f62e6SJames Smart 	struct lpfc_nodelist *ndlp = lpfc_ncmd->ndlp;
2724318083adSJames Smart 
2725318083adSJames Smart 
2726952c303bSDick Kennedy 	if (ndlp)
2727318083adSJames Smart 		lpfc_sli4_abts_err_handler(phba, ndlp, axri);
272886c67379SJames Smart 
272986c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2730c00f62e6SJames Smart 			"6311 nvme_cmd %p xri x%x tag x%x abort complete and "
2731c00f62e6SJames Smart 			"xri released\n",
2732952c303bSDick Kennedy 			lpfc_ncmd->nvmeCmd, xri,
2733952c303bSDick Kennedy 			lpfc_ncmd->cur_iocbq.iotag);
273486c67379SJames Smart 
2735952c303bSDick Kennedy 	/* Aborted NVME commands are required to not complete
2736952c303bSDick Kennedy 	 * before the abort exchange command fully completes.
2737952c303bSDick Kennedy 	 * Once completed, it is available via the put list.
2738952c303bSDick Kennedy 	 */
27393fd78355SJames Smart 	if (lpfc_ncmd->nvmeCmd) {
2740952c303bSDick Kennedy 		nvme_cmd = lpfc_ncmd->nvmeCmd;
2741952c303bSDick Kennedy 		nvme_cmd->done(nvme_cmd);
27423fd78355SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
27433fd78355SJames Smart 	}
2744318083adSJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
2745318083adSJames Smart }
2746c3725bdcSJames Smart 
2747c3725bdcSJames Smart /**
2748c3725bdcSJames Smart  * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2749c3725bdcSJames Smart  * @phba: Pointer to HBA context object.
2750c3725bdcSJames Smart  *
2751c3725bdcSJames Smart  * This function flushes all wqes in the nvme rings and frees all resources
2752c3725bdcSJames Smart  * in the txcmplq. This function does not issue abort wqes for the IO
2753c3725bdcSJames Smart  * commands in txcmplq, they will just be returned with
2754c3725bdcSJames Smart  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2755c3725bdcSJames Smart  * slot has been permanently disabled.
2756c3725bdcSJames Smart  **/
2757c3725bdcSJames Smart void
lpfc_nvme_wait_for_io_drain(struct lpfc_hba * phba)2758c3725bdcSJames Smart lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2759c3725bdcSJames Smart {
2760c3725bdcSJames Smart 	struct lpfc_sli_ring  *pring;
2761c3725bdcSJames Smart 	u32 i, wait_cnt = 0;
2762c3725bdcSJames Smart 
2763cdb42becSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4 || !phba->sli4_hba.hdwq)
2764c3725bdcSJames Smart 		return;
2765c3725bdcSJames Smart 
2766c00f62e6SJames Smart 	/* Cycle through all IO rings and make sure all outstanding
2767c3725bdcSJames Smart 	 * WQEs have been removed from the txcmplqs.
2768c3725bdcSJames Smart 	 */
2769cdb42becSJames Smart 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
2770c00f62e6SJames Smart 		if (!phba->sli4_hba.hdwq[i].io_wq)
27716a828b0fSJames Smart 			continue;
2772c00f62e6SJames Smart 		pring = phba->sli4_hba.hdwq[i].io_wq->pring;
2773c3725bdcSJames Smart 
2774d580c613SJames Smart 		if (!pring)
2775d580c613SJames Smart 			continue;
2776d580c613SJames Smart 
2777c3725bdcSJames Smart 		/* Retrieve everything on the txcmplq */
2778c3725bdcSJames Smart 		while (!list_empty(&pring->txcmplq)) {
2779c3725bdcSJames Smart 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
2780c3725bdcSJames Smart 			wait_cnt++;
2781c3725bdcSJames Smart 
2782c3725bdcSJames Smart 			/* The sleep is 10mS.  Every ten seconds,
2783c3725bdcSJames Smart 			 * dump a message.  Something is wrong.
2784c3725bdcSJames Smart 			 */
2785c3725bdcSJames Smart 			if ((wait_cnt % 1000) == 0) {
2786372c187bSDick Kennedy 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2787c3725bdcSJames Smart 						"6178 NVME IO not empty, "
2788c3725bdcSJames Smart 						"cnt %d\n", wait_cnt);
2789c3725bdcSJames Smart 			}
2790c3725bdcSJames Smart 		}
2791c3725bdcSJames Smart 	}
2792a22d73b6SJames Smart 
2793a22d73b6SJames Smart 	/* Make sure HBA is alive */
2794a22d73b6SJames Smart 	lpfc_issue_hb_tmo(phba);
2795a22d73b6SJames Smart 
2796c3725bdcSJames Smart }
279784f2ddf8SJames Smart 
279884f2ddf8SJames Smart void
lpfc_nvme_cancel_iocb(struct lpfc_hba * phba,struct lpfc_iocbq * pwqeIn,uint32_t stat,uint32_t param)27999ec58ec7SJames Smart lpfc_nvme_cancel_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
28009ec58ec7SJames Smart 		      uint32_t stat, uint32_t param)
280184f2ddf8SJames Smart {
280284f2ddf8SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
280384f2ddf8SJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
280484f2ddf8SJames Smart 	struct nvmefc_fcp_req *nCmd;
28059ec58ec7SJames Smart 	struct lpfc_wcqe_complete wcqe;
28069ec58ec7SJames Smart 	struct lpfc_wcqe_complete *wcqep = &wcqe;
280784f2ddf8SJames Smart 
2808d51cf5bdSJames Smart 	lpfc_ncmd = pwqeIn->io_buf;
28099ec58ec7SJames Smart 	if (!lpfc_ncmd) {
281084f2ddf8SJames Smart 		lpfc_sli_release_iocbq(phba, pwqeIn);
281184f2ddf8SJames Smart 		return;
281284f2ddf8SJames Smart 	}
281384f2ddf8SJames Smart 	/* For abort iocb just return, IO iocb will do a done call */
281484f2ddf8SJames Smart 	if (bf_get(wqe_cmnd, &pwqeIn->wqe.gen_req.wqe_com) ==
281584f2ddf8SJames Smart 	    CMD_ABORT_XRI_CX) {
281684f2ddf8SJames Smart 		lpfc_sli_release_iocbq(phba, pwqeIn);
281784f2ddf8SJames Smart 		return;
281884f2ddf8SJames Smart 	}
281984f2ddf8SJames Smart 
282084f2ddf8SJames Smart 	spin_lock(&lpfc_ncmd->buf_lock);
28219ec58ec7SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
28229ec58ec7SJames Smart 	if (!nCmd) {
282384f2ddf8SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
282484f2ddf8SJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
282584f2ddf8SJames Smart 		return;
282684f2ddf8SJames Smart 	}
28279ec58ec7SJames Smart 	spin_unlock(&lpfc_ncmd->buf_lock);
282884f2ddf8SJames Smart 
282984f2ddf8SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
283084f2ddf8SJames Smart 			"6194 NVME Cancel xri %x\n",
283184f2ddf8SJames Smart 			lpfc_ncmd->cur_iocbq.sli4_xritag);
283284f2ddf8SJames Smart 
28339ec58ec7SJames Smart 	wcqep->word0 = 0;
28349ec58ec7SJames Smart 	bf_set(lpfc_wcqe_c_status, wcqep, stat);
28359ec58ec7SJames Smart 	wcqep->parameter = param;
28364ecc9b02SJames Smart 	wcqep->total_data_placed = 0;
28379ec58ec7SJames Smart 	wcqep->word3 = 0; /* xb is 0 */
283884f2ddf8SJames Smart 
283984f2ddf8SJames Smart 	/* Call release with XB=1 to queue the IO into the abort list. */
28409ec58ec7SJames Smart 	if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
28419ec58ec7SJames Smart 		bf_set(lpfc_wcqe_c_xb, wcqep, 1);
28429ec58ec7SJames Smart 
2843a680a929SJames Smart 	memcpy(&pwqeIn->wcqe_cmpl, wcqep, sizeof(*wcqep));
2844a680a929SJames Smart 	(pwqeIn->cmd_cmpl)(phba, pwqeIn, pwqeIn);
284584f2ddf8SJames Smart #endif
284684f2ddf8SJames Smart }
2847