xref: /linux/drivers/scsi/lpfc/lpfc_nvme.c (revision 840a470181c72f82f8bc9453b47ac0d2e79f6b4a)
101649561SJames Smart /*******************************************************************
201649561SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
301649561SJames Smart  * Fibre Channel Host Bus Adapters.                                *
40d041215SJames Smart  * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
53e21d1cbSJames Smart  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
601649561SJames Smart  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
701649561SJames Smart  * EMULEX and SLI are trademarks of Emulex.                        *
8d080abe0SJames Smart  * www.broadcom.com                                                *
901649561SJames Smart  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
1001649561SJames Smart  *                                                                 *
1101649561SJames Smart  * This program is free software; you can redistribute it and/or   *
1201649561SJames Smart  * modify it under the terms of version 2 of the GNU General       *
1301649561SJames Smart  * Public License as published by the Free Software Foundation.    *
1401649561SJames Smart  * This program is distributed in the hope that it will be useful. *
1501649561SJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
1601649561SJames Smart  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
1701649561SJames Smart  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
1801649561SJames Smart  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
1901649561SJames Smart  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
2001649561SJames Smart  * more details, a copy of which can be found in the file COPYING  *
2101649561SJames Smart  * included with this package.                                     *
2201649561SJames Smart  ********************************************************************/
2301649561SJames Smart #include <linux/pci.h>
2401649561SJames Smart #include <linux/slab.h>
2501649561SJames Smart #include <linux/interrupt.h>
2601649561SJames Smart #include <linux/delay.h>
2701649561SJames Smart #include <asm/unaligned.h>
2801649561SJames Smart #include <linux/crc-t10dif.h>
2901649561SJames Smart #include <net/checksum.h>
3001649561SJames Smart 
3101649561SJames Smart #include <scsi/scsi.h>
3201649561SJames Smart #include <scsi/scsi_device.h>
3301649561SJames Smart #include <scsi/scsi_eh.h>
3401649561SJames Smart #include <scsi/scsi_host.h>
3501649561SJames Smart #include <scsi/scsi_tcq.h>
3601649561SJames Smart #include <scsi/scsi_transport_fc.h>
3701649561SJames Smart #include <scsi/fc/fc_fs.h>
3801649561SJames Smart 
3901649561SJames Smart #include "lpfc_version.h"
4001649561SJames Smart #include "lpfc_hw4.h"
4101649561SJames Smart #include "lpfc_hw.h"
4201649561SJames Smart #include "lpfc_sli.h"
4301649561SJames Smart #include "lpfc_sli4.h"
4401649561SJames Smart #include "lpfc_nl.h"
4501649561SJames Smart #include "lpfc_disc.h"
4601649561SJames Smart #include "lpfc.h"
4701649561SJames Smart #include "lpfc_nvme.h"
4801649561SJames Smart #include "lpfc_scsi.h"
4901649561SJames Smart #include "lpfc_logmsg.h"
5001649561SJames Smart #include "lpfc_crtn.h"
5101649561SJames Smart #include "lpfc_vport.h"
52bd2cdd5eSJames Smart #include "lpfc_debugfs.h"
5301649561SJames Smart 
5401649561SJames Smart /* NVME initiator-based functions */
5501649561SJames Smart 
56c490850aSJames Smart static struct lpfc_io_buf *
57cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
585e5b511dSJames Smart 		  int idx, int expedite);
5901649561SJames Smart 
6001649561SJames Smart static void
61c490850aSJames Smart lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_io_buf *);
6201649561SJames Smart 
6381e6a637SJames Smart static struct nvme_fc_port_template lpfc_nvme_template;
6401649561SJames Smart 
6501649561SJames Smart /**
6651f8e43eSJames Smart  * lpfc_nvme_prep_abort_wqe - set up 'abort' work queue entry.
6751f8e43eSJames Smart  * @pwqeq: Pointer to command iocb.
6851f8e43eSJames Smart  * @xritag: Tag that  uniqely identifies the local exchange resource.
6951f8e43eSJames Smart  * @opt: Option bits -
7051f8e43eSJames Smart  *		bit 0 = inhibit sending abts on the link
7151f8e43eSJames Smart  *
7251f8e43eSJames Smart  * This function is called with hbalock held.
7351f8e43eSJames Smart  **/
7451f8e43eSJames Smart void
7551f8e43eSJames Smart lpfc_nvme_prep_abort_wqe(struct lpfc_iocbq *pwqeq, u16 xritag, u8 opt)
7651f8e43eSJames Smart {
7751f8e43eSJames Smart 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
7851f8e43eSJames Smart 
7951f8e43eSJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
8051f8e43eSJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
8151f8e43eSJames Smart 	 */
8251f8e43eSJames Smart 	memset(wqe, 0, sizeof(*wqe));
8351f8e43eSJames Smart 
8451f8e43eSJames Smart 	if (opt & INHIBIT_ABORT)
8551f8e43eSJames Smart 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8651f8e43eSJames Smart 	/* Abort specified xri tag, with the mask deliberately zeroed */
8751f8e43eSJames Smart 	bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
8851f8e43eSJames Smart 
8951f8e43eSJames Smart 	bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9051f8e43eSJames Smart 
9151f8e43eSJames Smart 	/* Abort the IO associated with this outstanding exchange ID. */
9251f8e43eSJames Smart 	wqe->abort_cmd.wqe_com.abort_tag = xritag;
9351f8e43eSJames Smart 
9451f8e43eSJames Smart 	/* iotag for the wqe completion. */
9551f8e43eSJames Smart 	bf_set(wqe_reqtag, &wqe->abort_cmd.wqe_com, pwqeq->iotag);
9651f8e43eSJames Smart 
9751f8e43eSJames Smart 	bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9851f8e43eSJames Smart 	bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
9951f8e43eSJames Smart 
10051f8e43eSJames Smart 	bf_set(wqe_cmd_type, &wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10151f8e43eSJames Smart 	bf_set(wqe_wqec, &wqe->abort_cmd.wqe_com, 1);
10251f8e43eSJames Smart 	bf_set(wqe_cqid, &wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10351f8e43eSJames Smart }
10451f8e43eSJames Smart 
10551f8e43eSJames Smart /**
10601649561SJames Smart  * lpfc_nvme_create_queue -
10709d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from
10801649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
10909d99705SLee Jones  * @qsize: Size of the queue in bytes
11001649561SJames Smart  * @handle: An opaque driver handle used in follow-up calls.
11101649561SJames Smart  *
11201649561SJames Smart  * Driver registers this routine to preallocate and initialize any
11301649561SJames Smart  * internal data structures to bind the @qidx to its internal IO queues.
11401649561SJames Smart  * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
11501649561SJames Smart  *
11601649561SJames Smart  * Return value :
11701649561SJames Smart  *   0 - Success
11801649561SJames Smart  *   -EINVAL - Unsupported input value.
11901649561SJames Smart  *   -ENOMEM - Could not alloc necessary memory
12001649561SJames Smart  **/
12101649561SJames Smart static int
12201649561SJames Smart lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
12301649561SJames Smart 		       unsigned int qidx, u16 qsize,
12401649561SJames Smart 		       void **handle)
12501649561SJames Smart {
12601649561SJames Smart 	struct lpfc_nvme_lport *lport;
12701649561SJames Smart 	struct lpfc_vport *vport;
12801649561SJames Smart 	struct lpfc_nvme_qhandle *qhandle;
12901649561SJames Smart 	char *str;
13001649561SJames Smart 
131c3725bdcSJames Smart 	if (!pnvme_lport->private)
132c3725bdcSJames Smart 		return -ENOMEM;
133c3725bdcSJames Smart 
13401649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
13501649561SJames Smart 	vport = lport->vport;
13601649561SJames Smart 	qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
13701649561SJames Smart 	if (qhandle == NULL)
13801649561SJames Smart 		return -ENOMEM;
13901649561SJames Smart 
140d6d189ceSBart Van Assche 	qhandle->cpu_id = raw_smp_processor_id();
14101649561SJames Smart 	qhandle->qidx = qidx;
14201649561SJames Smart 	/*
14301649561SJames Smart 	 * NVME qidx == 0 is the admin queue, so both admin queue
14401649561SJames Smart 	 * and first IO queue will use MSI-X vector and associated
14501649561SJames Smart 	 * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
14601649561SJames Smart 	 */
14701649561SJames Smart 	if (qidx) {
14801649561SJames Smart 		str = "IO ";  /* IO queue */
14901649561SJames Smart 		qhandle->index = ((qidx - 1) %
1506a828b0fSJames Smart 			lpfc_nvme_template.max_hw_queues);
15101649561SJames Smart 	} else {
15201649561SJames Smart 		str = "ADM";  /* Admin queue */
15301649561SJames Smart 		qhandle->index = qidx;
15401649561SJames Smart 	}
15501649561SJames Smart 
156d58734f0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
15701649561SJames Smart 			 "6073 Binding %s HdwQueue %d  (cpu %d) to "
15832350664SJames Smart 			 "hdw_queue %d qhandle x%px\n", str,
15901649561SJames Smart 			 qidx, qhandle->cpu_id, qhandle->index, qhandle);
16001649561SJames Smart 	*handle = (void *)qhandle;
16101649561SJames Smart 	return 0;
16201649561SJames Smart }
16301649561SJames Smart 
16401649561SJames Smart /**
16501649561SJames Smart  * lpfc_nvme_delete_queue -
16609d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from
16701649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
16801649561SJames Smart  * @handle: An opaque driver handle from lpfc_nvme_create_queue
16901649561SJames Smart  *
17001649561SJames Smart  * Driver registers this routine to free
17101649561SJames Smart  * any internal data structures to bind the @qidx to its internal
17201649561SJames Smart  * IO queues.
17301649561SJames Smart  *
17401649561SJames Smart  * Return value :
17501649561SJames Smart  *   0 - Success
17601649561SJames Smart  *   TODO:  What are the failure codes.
17701649561SJames Smart  **/
17801649561SJames Smart static void
17901649561SJames Smart lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
18001649561SJames Smart 		       unsigned int qidx,
18101649561SJames Smart 		       void *handle)
18201649561SJames Smart {
18301649561SJames Smart 	struct lpfc_nvme_lport *lport;
18401649561SJames Smart 	struct lpfc_vport *vport;
18501649561SJames Smart 
186c3725bdcSJames Smart 	if (!pnvme_lport->private)
187c3725bdcSJames Smart 		return;
188c3725bdcSJames Smart 
18901649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
19001649561SJames Smart 	vport = lport->vport;
19101649561SJames Smart 
19201649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
19332350664SJames Smart 			"6001 ENTER.  lpfc_pnvme x%px, qidx x%x qhandle x%px\n",
19401649561SJames Smart 			lport, qidx, handle);
19501649561SJames Smart 	kfree(handle);
19601649561SJames Smart }
19701649561SJames Smart 
19801649561SJames Smart static void
19901649561SJames Smart lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
20001649561SJames Smart {
20101649561SJames Smart 	struct lpfc_nvme_lport *lport = localport->private;
20201649561SJames Smart 
203add9d6beSJames Smart 	lpfc_printf_vlog(lport->vport, KERN_INFO, LOG_NVME,
20432350664SJames Smart 			 "6173 localport x%px delete complete\n",
205add9d6beSJames Smart 			 lport);
206add9d6beSJames Smart 
20701649561SJames Smart 	/* release any threads waiting for the unreg to complete */
2087961cba6SEwan D. Milne 	if (lport->vport->localport)
2097961cba6SEwan D. Milne 		complete(lport->lport_unreg_cmp);
21001649561SJames Smart }
21101649561SJames Smart 
21201649561SJames Smart /* lpfc_nvme_remoteport_delete
21301649561SJames Smart  *
21401649561SJames Smart  * @remoteport: Pointer to an nvme transport remoteport instance.
21501649561SJames Smart  *
21601649561SJames Smart  * This is a template downcall.  NVME transport calls this function
21701649561SJames Smart  * when it has completed the unregistration of a previously
21801649561SJames Smart  * registered remoteport.
21901649561SJames Smart  *
22001649561SJames Smart  * Return value :
22101649561SJames Smart  * None
22201649561SJames Smart  */
2233999df75SBart Van Assche static void
22401649561SJames Smart lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
22501649561SJames Smart {
22601649561SJames Smart 	struct lpfc_nvme_rport *rport = remoteport->private;
22701649561SJames Smart 	struct lpfc_vport *vport;
22801649561SJames Smart 	struct lpfc_nodelist *ndlp;
229e9b11083SJames Smart 	u32 fc4_xpt_flags;
23001649561SJames Smart 
23101649561SJames Smart 	ndlp = rport->ndlp;
232e9b11083SJames Smart 	if (!ndlp) {
233e9b11083SJames Smart 		pr_err("**** %s: NULL ndlp on rport %p remoteport %p\n",
234e9b11083SJames Smart 		       __func__, rport, remoteport);
23501649561SJames Smart 		goto rport_err;
236e9b11083SJames Smart 	}
23701649561SJames Smart 
23801649561SJames Smart 	vport = ndlp->vport;
239e9b11083SJames Smart 	if (!vport) {
240e9b11083SJames Smart 		pr_err("**** %s: Null vport on ndlp %p, ste x%x rport %p\n",
241e9b11083SJames Smart 		       __func__, ndlp, ndlp->nlp_state, rport);
24201649561SJames Smart 		goto rport_err;
243e9b11083SJames Smart 	}
244e9b11083SJames Smart 
245e9b11083SJames Smart 	fc4_xpt_flags = NVME_XPT_REGD | SCSI_XPT_REGD;
24601649561SJames Smart 
24701649561SJames Smart 	/* Remove this rport from the lport's list - memory is owned by the
24801649561SJames Smart 	 * transport. Remove the ndlp reference for the NVME transport before
2497a06dcd3SJames Smart 	 * calling state machine to remove the node.
25001649561SJames Smart 	 */
25101649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
252e9b11083SJames Smart 			"6146 remoteport delete of remoteport %p\n",
25301649561SJames Smart 			remoteport);
254c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
255b15bd3e6SJames Smart 
256b15bd3e6SJames Smart 	/* The register rebind might have occurred before the delete
257b15bd3e6SJames Smart 	 * downcall.  Guard against this race.
258b15bd3e6SJames Smart 	 */
259e9b11083SJames Smart 	if (ndlp->fc4_xpt_flags & NLP_WAIT_FOR_UNREG)
260e9b11083SJames Smart 		ndlp->fc4_xpt_flags &= ~(NLP_WAIT_FOR_UNREG | NVME_XPT_REGD);
261e9b11083SJames Smart 
262c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
2633fd78355SJames Smart 
264e9b11083SJames Smart 	/* On a devloss timeout event, one more put is executed provided the
265e9b11083SJames Smart 	 * NVME and SCSI rport unregister requests are complete.  If the vport
266e9b11083SJames Smart 	 * is unloading, this extra put is executed by lpfc_drop_node.
2673fd78355SJames Smart 	 */
268e9b11083SJames Smart 	if (!(ndlp->fc4_xpt_flags & fc4_xpt_flags))
269e9b11083SJames Smart 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
27001649561SJames Smart 
27101649561SJames Smart  rport_err:
2723fd78355SJames Smart 	return;
27301649561SJames Smart }
27401649561SJames Smart 
2753a8070c5SJames Smart /**
2763a8070c5SJames Smart  * lpfc_nvme_handle_lsreq - Process an unsolicited NVME LS request
2773a8070c5SJames Smart  * @phba: pointer to lpfc hba data structure.
2783a8070c5SJames Smart  * @axchg: pointer to exchange context for the NVME LS request
2793a8070c5SJames Smart  *
2803a8070c5SJames Smart  * This routine is used for processing an asychronously received NVME LS
2813a8070c5SJames Smart  * request. Any remaining validation is done and the LS is then forwarded
2823a8070c5SJames Smart  * to the nvme-fc transport via nvme_fc_rcv_ls_req().
2833a8070c5SJames Smart  *
2849aa09e98SJames Smart  * The calling sequence should be: nvme_fc_rcv_ls_req() -> (processing)
2859aa09e98SJames Smart  * -> lpfc_nvme_xmt_ls_rsp/cmp -> req->done.
2869aa09e98SJames Smart  * __lpfc_nvme_xmt_ls_rsp_cmp should free the allocated axchg.
2879aa09e98SJames Smart  *
2883a8070c5SJames Smart  * Returns 0 if LS was handled and delivered to the transport
2893a8070c5SJames Smart  * Returns 1 if LS failed to be handled and should be dropped
2903a8070c5SJames Smart  */
2913a8070c5SJames Smart int
2923a8070c5SJames Smart lpfc_nvme_handle_lsreq(struct lpfc_hba *phba,
2933a8070c5SJames Smart 			struct lpfc_async_xchg_ctx *axchg)
2943a8070c5SJames Smart {
2959aa09e98SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2969aa09e98SJames Smart 	struct lpfc_vport *vport;
2979aa09e98SJames Smart 	struct lpfc_nvme_rport *lpfc_rport;
2989aa09e98SJames Smart 	struct nvme_fc_remote_port *remoteport;
2999aa09e98SJames Smart 	struct lpfc_nvme_lport *lport;
3009aa09e98SJames Smart 	uint32_t *payload = axchg->payload;
3019aa09e98SJames Smart 	int rc;
3029aa09e98SJames Smart 
3039aa09e98SJames Smart 	vport = axchg->ndlp->vport;
3049aa09e98SJames Smart 	lpfc_rport = axchg->ndlp->nrport;
3059aa09e98SJames Smart 	if (!lpfc_rport)
3069aa09e98SJames Smart 		return -EINVAL;
3079aa09e98SJames Smart 
3089aa09e98SJames Smart 	remoteport = lpfc_rport->remoteport;
3099aa09e98SJames Smart 	if (!vport->localport)
3109aa09e98SJames Smart 		return -EINVAL;
3119aa09e98SJames Smart 
3129aa09e98SJames Smart 	lport = vport->localport->private;
3139aa09e98SJames Smart 	if (!lport)
3149aa09e98SJames Smart 		return -EINVAL;
3159aa09e98SJames Smart 
3169aa09e98SJames Smart 	rc = nvme_fc_rcv_ls_req(remoteport, &axchg->ls_rsp, axchg->payload,
3179aa09e98SJames Smart 				axchg->size);
3189aa09e98SJames Smart 
3199aa09e98SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
3209aa09e98SJames Smart 			"6205 NVME Unsol rcv: sz %d rc %d: %08x %08x %08x "
3219aa09e98SJames Smart 			"%08x %08x %08x\n",
3229aa09e98SJames Smart 			axchg->size, rc,
3239aa09e98SJames Smart 			*payload, *(payload+1), *(payload+2),
3249aa09e98SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
3259aa09e98SJames Smart 
3269aa09e98SJames Smart 	if (!rc)
3279aa09e98SJames Smart 		return 0;
3289aa09e98SJames Smart #endif
3293a8070c5SJames Smart 	return 1;
3303a8070c5SJames Smart }
3313a8070c5SJames Smart 
3326514b25dSJames Smart /**
3336514b25dSJames Smart  * __lpfc_nvme_ls_req_cmp - Generic completion handler for a NVME
3346514b25dSJames Smart  *        LS request.
3356514b25dSJames Smart  * @phba: Pointer to HBA context object
3366514b25dSJames Smart  * @vport: The local port that issued the LS
3376514b25dSJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3386514b25dSJames Smart  * @wcqe: Pointer to driver response CQE object.
3396514b25dSJames Smart  *
3406514b25dSJames Smart  * This function is the generic completion handler for NVME LS requests.
3416514b25dSJames Smart  * The function updates any states and statistics, calls the transport
3426514b25dSJames Smart  * ls_req done() routine, then tears down the command and buffers used
3436514b25dSJames Smart  * for the LS request.
3446514b25dSJames Smart  **/
3456514b25dSJames Smart void
3466514b25dSJames Smart __lpfc_nvme_ls_req_cmp(struct lpfc_hba *phba,  struct lpfc_vport *vport,
3476514b25dSJames Smart 			struct lpfc_iocbq *cmdwqe,
3486514b25dSJames Smart 			struct lpfc_wcqe_complete *wcqe)
3496514b25dSJames Smart {
3506514b25dSJames Smart 	struct nvmefc_ls_req *pnvme_lsreq;
3516514b25dSJames Smart 	struct lpfc_dmabuf *buf_ptr;
3526514b25dSJames Smart 	struct lpfc_nodelist *ndlp;
3536514b25dSJames Smart 	uint32_t status;
3546514b25dSJames Smart 
3556514b25dSJames Smart 	pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
3566514b25dSJames Smart 	ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
3576514b25dSJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
3586514b25dSJames Smart 
3596514b25dSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
3606514b25dSJames Smart 			 "6047 NVMEx LS REQ %px cmpl DID %x Xri: %x "
3616514b25dSJames Smart 			 "status %x reason x%x cmd:x%px lsreg:x%px bmp:x%px "
3626514b25dSJames Smart 			 "ndlp:x%px\n",
3636514b25dSJames Smart 			 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
3646514b25dSJames Smart 			 cmdwqe->sli4_xritag, status,
3656514b25dSJames Smart 			 (wcqe->parameter & 0xffff),
3666514b25dSJames Smart 			 cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
3676514b25dSJames Smart 
3686514b25dSJames Smart 	lpfc_nvmeio_data(phba, "NVMEx LS CMPL: xri x%x stat x%x parm x%x\n",
3696514b25dSJames Smart 			 cmdwqe->sli4_xritag, status, wcqe->parameter);
3706514b25dSJames Smart 
3716514b25dSJames Smart 	if (cmdwqe->context3) {
3726514b25dSJames Smart 		buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
3736514b25dSJames Smart 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3746514b25dSJames Smart 		kfree(buf_ptr);
3756514b25dSJames Smart 		cmdwqe->context3 = NULL;
3766514b25dSJames Smart 	}
3776514b25dSJames Smart 	if (pnvme_lsreq->done)
3786514b25dSJames Smart 		pnvme_lsreq->done(pnvme_lsreq, status);
3796514b25dSJames Smart 	else
380372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3816514b25dSJames Smart 				 "6046 NVMEx cmpl without done call back? "
3826514b25dSJames Smart 				 "Data %px DID %x Xri: %x status %x\n",
3836514b25dSJames Smart 				pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
3846514b25dSJames Smart 				cmdwqe->sli4_xritag, status);
3856514b25dSJames Smart 	if (ndlp) {
3866514b25dSJames Smart 		lpfc_nlp_put(ndlp);
3876514b25dSJames Smart 		cmdwqe->context1 = NULL;
3886514b25dSJames Smart 	}
3896514b25dSJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
3906514b25dSJames Smart }
3916514b25dSJames Smart 
39201649561SJames Smart static void
3936514b25dSJames Smart lpfc_nvme_ls_req_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
39401649561SJames Smart 		       struct lpfc_wcqe_complete *wcqe)
39501649561SJames Smart {
39601649561SJames Smart 	struct lpfc_vport *vport = cmdwqe->vport;
3974b056682SJames Smart 	struct lpfc_nvme_lport *lport;
39801649561SJames Smart 	uint32_t status;
39901649561SJames Smart 
40001649561SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
40166a210ffSJames Smart 
40266a85155SJames Smart 	if (vport->localport) {
40366a85155SJames Smart 		lport = (struct lpfc_nvme_lport *)vport->localport->private;
40466a210ffSJames Smart 		if (lport) {
40566a210ffSJames Smart 			atomic_inc(&lport->fc4NvmeLsCmpls);
4064b056682SJames Smart 			if (status) {
4074b056682SJames Smart 				if (bf_get(lpfc_wcqe_c_xb, wcqe))
4084b056682SJames Smart 					atomic_inc(&lport->cmpl_ls_xb);
4094b056682SJames Smart 				atomic_inc(&lport->cmpl_ls_err);
4104b056682SJames Smart 			}
41166a210ffSJames Smart 		}
41266a85155SJames Smart 	}
4134b056682SJames Smart 
4146514b25dSJames Smart 	__lpfc_nvme_ls_req_cmp(phba, vport, cmdwqe, wcqe);
41501649561SJames Smart }
41601649561SJames Smart 
41701649561SJames Smart static int
41801649561SJames Smart lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
41901649561SJames Smart 		  struct lpfc_dmabuf *inp,
42001649561SJames Smart 		  struct nvmefc_ls_req *pnvme_lsreq,
42101649561SJames Smart 		  void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
42201649561SJames Smart 			       struct lpfc_wcqe_complete *),
42301649561SJames Smart 		  struct lpfc_nodelist *ndlp, uint32_t num_entry,
42401649561SJames Smart 		  uint32_t tmo, uint8_t retry)
42501649561SJames Smart {
42601649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
427205e8240SJames Smart 	union lpfc_wqe128 *wqe;
42801649561SJames Smart 	struct lpfc_iocbq *genwqe;
42901649561SJames Smart 	struct ulp_bde64 *bpl;
43001649561SJames Smart 	struct ulp_bde64 bde;
43101649561SJames Smart 	int i, rc, xmit_len, first_len;
43201649561SJames Smart 
43301649561SJames Smart 	/* Allocate buffer for  command WQE */
43401649561SJames Smart 	genwqe = lpfc_sli_get_iocbq(phba);
43501649561SJames Smart 	if (genwqe == NULL)
43601649561SJames Smart 		return 1;
43701649561SJames Smart 
43801649561SJames Smart 	wqe = &genwqe->wqe;
439d9f492a1SJames Smart 	/* Initialize only 64 bytes */
44001649561SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
44101649561SJames Smart 
44201649561SJames Smart 	genwqe->context3 = (uint8_t *)bmp;
44301649561SJames Smart 	genwqe->iocb_flag |= LPFC_IO_NVME_LS;
44401649561SJames Smart 
44501649561SJames Smart 	/* Save for completion so we can release these resources */
44601649561SJames Smart 	genwqe->context1 = lpfc_nlp_get(ndlp);
4474430f7fdSJames Smart 	if (!genwqe->context1) {
4484430f7fdSJames Smart 		dev_warn(&phba->pcidev->dev,
4494430f7fdSJames Smart 			 "Warning: Failed node ref, not sending LS_REQ\n");
4504430f7fdSJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
4514430f7fdSJames Smart 		return 1;
4524430f7fdSJames Smart 	}
4534430f7fdSJames Smart 
45401649561SJames Smart 	genwqe->context2 = (uint8_t *)pnvme_lsreq;
45501649561SJames Smart 	/* Fill in payload, bp points to frame payload */
45601649561SJames Smart 
45701649561SJames Smart 	if (!tmo)
45801649561SJames Smart 		/* FC spec states we need 3 * ratov for CT requests */
45901649561SJames Smart 		tmo = (3 * phba->fc_ratov);
46001649561SJames Smart 
46101649561SJames Smart 	/* For this command calculate the xmit length of the request bde. */
46201649561SJames Smart 	xmit_len = 0;
46301649561SJames Smart 	first_len = 0;
46401649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
46501649561SJames Smart 	for (i = 0; i < num_entry; i++) {
46601649561SJames Smart 		bde.tus.w = bpl[i].tus.w;
46701649561SJames Smart 		if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
46801649561SJames Smart 			break;
46901649561SJames Smart 		xmit_len += bde.tus.f.bdeSize;
47001649561SJames Smart 		if (i == 0)
47101649561SJames Smart 			first_len = xmit_len;
47201649561SJames Smart 	}
47301649561SJames Smart 
47401649561SJames Smart 	genwqe->rsvd2 = num_entry;
47501649561SJames Smart 	genwqe->hba_wqidx = 0;
47601649561SJames Smart 
47701649561SJames Smart 	/* Words 0 - 2 */
47801649561SJames Smart 	wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
47901649561SJames Smart 	wqe->generic.bde.tus.f.bdeSize = first_len;
48001649561SJames Smart 	wqe->generic.bde.addrLow = bpl[0].addrLow;
48101649561SJames Smart 	wqe->generic.bde.addrHigh = bpl[0].addrHigh;
48201649561SJames Smart 
48301649561SJames Smart 	/* Word 3 */
48401649561SJames Smart 	wqe->gen_req.request_payload_len = first_len;
48501649561SJames Smart 
48601649561SJames Smart 	/* Word 4 */
48701649561SJames Smart 
48801649561SJames Smart 	/* Word 5 */
48901649561SJames Smart 	bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
49001649561SJames Smart 	bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
49101649561SJames Smart 	bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
4928b361639SJames Smart 	bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
49301649561SJames Smart 	bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
49401649561SJames Smart 
49501649561SJames Smart 	/* Word 6 */
49601649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
49701649561SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
49801649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
49901649561SJames Smart 
50001649561SJames Smart 	/* Word 7 */
50101649561SJames Smart 	bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
50201649561SJames Smart 	bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
50301649561SJames Smart 	bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
50401649561SJames Smart 	bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
50501649561SJames Smart 
50601649561SJames Smart 	/* Word 8 */
50701649561SJames Smart 	wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
50801649561SJames Smart 
50901649561SJames Smart 	/* Word 9 */
51001649561SJames Smart 	bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
51101649561SJames Smart 
51201649561SJames Smart 	/* Word 10 */
51301649561SJames Smart 	bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
51401649561SJames Smart 	bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
51501649561SJames Smart 	bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
51601649561SJames Smart 	bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
51701649561SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
51801649561SJames Smart 
51901649561SJames Smart 	/* Word 11 */
52001649561SJames Smart 	bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
52101649561SJames Smart 	bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
52201649561SJames Smart 
52301649561SJames Smart 
52401649561SJames Smart 	/* Issue GEN REQ WQE for NPORT <did> */
52501649561SJames Smart 	genwqe->wqe_cmpl = cmpl;
52601649561SJames Smart 	genwqe->iocb_cmpl = NULL;
52701649561SJames Smart 	genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
52801649561SJames Smart 	genwqe->vport = vport;
52901649561SJames Smart 	genwqe->retry = retry;
53001649561SJames Smart 
531bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  XMIT: xri x%x iotag x%x to x%06x\n",
532bd2cdd5eSJames Smart 			 genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
533bd2cdd5eSJames Smart 
5341fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], genwqe);
535cd22d605SDick Kennedy 	if (rc) {
536372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
53701649561SJames Smart 				 "6045 Issue GEN REQ WQE to NPORT x%x "
5386514b25dSJames Smart 				 "Data: x%x x%x  rc x%x\n",
53901649561SJames Smart 				 ndlp->nlp_DID, genwqe->iotag,
5406514b25dSJames Smart 				 vport->port_state, rc);
541e9b11083SJames Smart 		lpfc_nlp_put(ndlp);
54201649561SJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
54301649561SJames Smart 		return 1;
54401649561SJames Smart 	}
5456514b25dSJames Smart 
5466514b25dSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_ELS,
5476514b25dSJames Smart 			 "6050 Issue GEN REQ WQE to NPORT x%x "
5486514b25dSJames Smart 			 "Data: oxid: x%x state: x%x wq:x%px lsreq:x%px "
5496514b25dSJames Smart 			 "bmp:x%px xmit:%d 1st:%d\n",
5506514b25dSJames Smart 			 ndlp->nlp_DID, genwqe->sli4_xritag,
5516514b25dSJames Smart 			 vport->port_state,
5526514b25dSJames Smart 			 genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
55301649561SJames Smart 	return 0;
55401649561SJames Smart }
55501649561SJames Smart 
5566514b25dSJames Smart 
55701649561SJames Smart /**
5586514b25dSJames Smart  * __lpfc_nvme_ls_req - Generic service routine to issue an NVME LS request
5596514b25dSJames Smart  * @vport: The local port issuing the LS
5606514b25dSJames Smart  * @ndlp: The remote port to send the LS to
5616514b25dSJames Smart  * @pnvme_lsreq: Pointer to LS request structure from the transport
56209d99705SLee Jones  * @gen_req_cmp: Completion call-back
56301649561SJames Smart  *
5646514b25dSJames Smart  * Routine validates the ndlp, builds buffers and sends a GEN_REQUEST
5656514b25dSJames Smart  * WQE to perform the LS operation.
56601649561SJames Smart  *
56701649561SJames Smart  * Return value :
56801649561SJames Smart  *   0 - Success
5696514b25dSJames Smart  *   non-zero: various error codes, in form of -Exxx
57001649561SJames Smart  **/
5716514b25dSJames Smart int
5726514b25dSJames Smart __lpfc_nvme_ls_req(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5736514b25dSJames Smart 		      struct nvmefc_ls_req *pnvme_lsreq,
5746514b25dSJames Smart 		      void (*gen_req_cmp)(struct lpfc_hba *phba,
5756514b25dSJames Smart 				struct lpfc_iocbq *cmdwqe,
5766514b25dSJames Smart 				struct lpfc_wcqe_complete *wcqe))
57701649561SJames Smart {
57801649561SJames Smart 	struct lpfc_dmabuf *bmp;
5796514b25dSJames Smart 	struct ulp_bde64 *bpl;
5806514b25dSJames Smart 	int ret;
581ba43c4d0SJames Smart 	uint16_t ntype, nstate;
58201649561SJames Smart 
583307e3380SJames Smart 	if (!ndlp) {
584372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5856514b25dSJames Smart 				 "6051 NVMEx LS REQ: Bad NDLP x%px, Failing "
5866514b25dSJames Smart 				 "LS Req\n",
5876514b25dSJames Smart 				 ndlp);
588ba43c4d0SJames Smart 		return -ENODEV;
589ba43c4d0SJames Smart 	}
590ba43c4d0SJames Smart 
591ba43c4d0SJames Smart 	ntype = ndlp->nlp_type;
592ba43c4d0SJames Smart 	nstate = ndlp->nlp_state;
593ba43c4d0SJames Smart 	if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
594ba43c4d0SJames Smart 	    (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
595372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5966514b25dSJames Smart 				 "6088 NVMEx LS REQ: Fail DID x%06x not "
5976514b25dSJames Smart 				 "ready for IO. Type x%x, State x%x\n",
5986514b25dSJames Smart 				 ndlp->nlp_DID, ntype, nstate);
599ba43c4d0SJames Smart 		return -ENODEV;
60001649561SJames Smart 	}
6016514b25dSJames Smart 
6026514b25dSJames Smart 	/*
6036514b25dSJames Smart 	 * there are two dma buf in the request, actually there is one and
6046514b25dSJames Smart 	 * the second one is just the start address + cmd size.
6056514b25dSJames Smart 	 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
6066514b25dSJames Smart 	 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
6076514b25dSJames Smart 	 * because the nvem layer owns the data bufs.
6086514b25dSJames Smart 	 * We do not have to break these packets open, we don't care what is
6096514b25dSJames Smart 	 * in them. And we do not have to look at the resonse data, we only
6106514b25dSJames Smart 	 * care that we got a response. All of the caring is going to happen
6116514b25dSJames Smart 	 * in the nvme-fc layer.
6126514b25dSJames Smart 	 */
6136514b25dSJames Smart 
6146514b25dSJames Smart 	bmp = kmalloc(sizeof(*bmp), GFP_KERNEL);
61501649561SJames Smart 	if (!bmp) {
616372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6176514b25dSJames Smart 				 "6044 NVMEx LS REQ: Could not alloc LS buf "
6186514b25dSJames Smart 				 "for DID %x\n",
6196514b25dSJames Smart 				 ndlp->nlp_DID);
6206514b25dSJames Smart 		return -ENOMEM;
62101649561SJames Smart 	}
6226514b25dSJames Smart 
62301649561SJames Smart 	bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
62401649561SJames Smart 	if (!bmp->virt) {
625372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6266514b25dSJames Smart 				 "6042 NVMEx LS REQ: Could not alloc mbuf "
6276514b25dSJames Smart 				 "for DID %x\n",
6286514b25dSJames Smart 				 ndlp->nlp_DID);
62901649561SJames Smart 		kfree(bmp);
6306514b25dSJames Smart 		return -ENOMEM;
63101649561SJames Smart 	}
6326514b25dSJames Smart 
6336514b25dSJames Smart 	INIT_LIST_HEAD(&bmp->list);
6346514b25dSJames Smart 
63501649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
63601649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
63701649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
63801649561SJames Smart 	bpl->tus.f.bdeFlags = 0;
63901649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
64001649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
64101649561SJames Smart 	bpl++;
64201649561SJames Smart 
64301649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
64401649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
64501649561SJames Smart 	bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
64601649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
64701649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
64801649561SJames Smart 
64901649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
6506514b25dSJames Smart 			"6149 NVMEx LS REQ: Issue to DID 0x%06x lsreq x%px, "
6516514b25dSJames Smart 			"rqstlen:%d rsplen:%d %pad %pad\n",
6526514b25dSJames Smart 			ndlp->nlp_DID, pnvme_lsreq, pnvme_lsreq->rqstlen,
653825c6abbSArnd Bergmann 			pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
654825c6abbSArnd Bergmann 			&pnvme_lsreq->rspdma);
65501649561SJames Smart 
65601649561SJames Smart 	ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
6576514b25dSJames Smart 				pnvme_lsreq, gen_req_cmp, ndlp, 2,
6586514b25dSJames Smart 				LPFC_NVME_LS_TIMEOUT, 0);
65901649561SJames Smart 	if (ret != WQE_SUCCESS) {
660372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6616514b25dSJames Smart 				 "6052 NVMEx REQ: EXIT. issue ls wqe failed "
6626514b25dSJames Smart 				 "lsreq x%px Status %x DID %x\n",
6636514b25dSJames Smart 				 pnvme_lsreq, ret, ndlp->nlp_DID);
66401649561SJames Smart 		lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
66501649561SJames Smart 		kfree(bmp);
6666514b25dSJames Smart 		return -EIO;
66701649561SJames Smart 	}
66801649561SJames Smart 
6696514b25dSJames Smart 	return 0;
6706514b25dSJames Smart }
6716514b25dSJames Smart 
6726514b25dSJames Smart /**
6736514b25dSJames Smart  * lpfc_nvme_ls_req - Issue an NVME Link Service request
67409d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from.
6753e749170SLee Jones  * @pnvme_rport: Transport remoteport that LS is to be sent to.
67609d99705SLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
6776514b25dSJames Smart  *
6786514b25dSJames Smart  * Driver registers this routine to handle any link service request
6796514b25dSJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
6806514b25dSJames Smart  *
6816514b25dSJames Smart  * Return value :
6826514b25dSJames Smart  *   0 - Success
6836514b25dSJames Smart  *   non-zero: various error codes, in form of -Exxx
6846514b25dSJames Smart  **/
6856514b25dSJames Smart static int
6866514b25dSJames Smart lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
6876514b25dSJames Smart 		 struct nvme_fc_remote_port *pnvme_rport,
6886514b25dSJames Smart 		 struct nvmefc_ls_req *pnvme_lsreq)
6896514b25dSJames Smart {
6906514b25dSJames Smart 	struct lpfc_nvme_lport *lport;
6916514b25dSJames Smart 	struct lpfc_nvme_rport *rport;
6926514b25dSJames Smart 	struct lpfc_vport *vport;
6936514b25dSJames Smart 	int ret;
6946514b25dSJames Smart 
6956514b25dSJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
6966514b25dSJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
6976514b25dSJames Smart 	if (unlikely(!lport) || unlikely(!rport))
6986514b25dSJames Smart 		return -EINVAL;
6996514b25dSJames Smart 
7006514b25dSJames Smart 	vport = lport->vport;
7016514b25dSJames Smart 	if (vport->load_flag & FC_UNLOADING)
7026514b25dSJames Smart 		return -ENODEV;
7036514b25dSJames Smart 
7046514b25dSJames Smart 	atomic_inc(&lport->fc4NvmeLsRequests);
7056514b25dSJames Smart 
7066514b25dSJames Smart 	ret = __lpfc_nvme_ls_req(vport, rport->ndlp, pnvme_lsreq,
7076514b25dSJames Smart 				 lpfc_nvme_ls_req_cmp);
7086514b25dSJames Smart 	if (ret)
7096514b25dSJames Smart 		atomic_inc(&lport->xmt_ls_err);
7106514b25dSJames Smart 
71101649561SJames Smart 	return ret;
71201649561SJames Smart }
71301649561SJames Smart 
71401649561SJames Smart /**
715e96a22b0SJames Smart  * __lpfc_nvme_ls_abort - Generic service routine to abort a prior
716e96a22b0SJames Smart  *         NVME LS request
717e96a22b0SJames Smart  * @vport: The local port that issued the LS
718e96a22b0SJames Smart  * @ndlp: The remote port the LS was sent to
719e96a22b0SJames Smart  * @pnvme_lsreq: Pointer to LS request structure from the transport
72001649561SJames Smart  *
721e96a22b0SJames Smart  * The driver validates the ndlp, looks for the LS, and aborts the
722e96a22b0SJames Smart  * LS if found.
72301649561SJames Smart  *
724e96a22b0SJames Smart  * Returns:
725e96a22b0SJames Smart  * 0 : if LS found and aborted
726e96a22b0SJames Smart  * non-zero: various error conditions in form -Exxx
727e96a22b0SJames Smart  **/
728e96a22b0SJames Smart int
729e96a22b0SJames Smart __lpfc_nvme_ls_abort(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
730e96a22b0SJames Smart 			struct nvmefc_ls_req *pnvme_lsreq)
731e96a22b0SJames Smart {
732e96a22b0SJames Smart 	struct lpfc_hba *phba = vport->phba;
733e96a22b0SJames Smart 	struct lpfc_sli_ring *pring;
734e96a22b0SJames Smart 	struct lpfc_iocbq *wqe, *next_wqe;
735e96a22b0SJames Smart 	bool foundit = false;
736e96a22b0SJames Smart 
737e96a22b0SJames Smart 	if (!ndlp) {
738372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
739e96a22b0SJames Smart 				"6049 NVMEx LS REQ Abort: Bad NDLP x%px DID "
740e96a22b0SJames Smart 				"x%06x, Failing LS Req\n",
741e96a22b0SJames Smart 				ndlp, ndlp ? ndlp->nlp_DID : 0);
742e96a22b0SJames Smart 		return -EINVAL;
743e96a22b0SJames Smart 	}
744e96a22b0SJames Smart 
745e96a22b0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_NVME_ABTS,
746e96a22b0SJames Smart 			 "6040 NVMEx LS REQ Abort: Issue LS_ABORT for lsreq "
747e96a22b0SJames Smart 			 "x%p rqstlen:%d rsplen:%d %pad %pad\n",
748e96a22b0SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
749e96a22b0SJames Smart 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
750e96a22b0SJames Smart 			 &pnvme_lsreq->rspdma);
751e96a22b0SJames Smart 
752e96a22b0SJames Smart 	/*
753e96a22b0SJames Smart 	 * Lock the ELS ring txcmplq and look for the wqe that matches
754e96a22b0SJames Smart 	 * this ELS. If found, issue an abort on the wqe.
755e96a22b0SJames Smart 	 */
756e96a22b0SJames Smart 	pring = phba->sli4_hba.nvmels_wq->pring;
757e96a22b0SJames Smart 	spin_lock_irq(&phba->hbalock);
758e96a22b0SJames Smart 	spin_lock(&pring->ring_lock);
759e96a22b0SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
760e96a22b0SJames Smart 		if (wqe->context2 == pnvme_lsreq) {
761e96a22b0SJames Smart 			wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
762e96a22b0SJames Smart 			foundit = true;
763e96a22b0SJames Smart 			break;
764e96a22b0SJames Smart 		}
765e96a22b0SJames Smart 	}
766e96a22b0SJames Smart 	spin_unlock(&pring->ring_lock);
767e96a22b0SJames Smart 
768e96a22b0SJames Smart 	if (foundit)
769e96a22b0SJames Smart 		lpfc_sli_issue_abort_iotag(phba, pring, wqe);
770e96a22b0SJames Smart 	spin_unlock_irq(&phba->hbalock);
771e96a22b0SJames Smart 
772e96a22b0SJames Smart 	if (foundit)
773e96a22b0SJames Smart 		return 0;
774e96a22b0SJames Smart 
775e96a22b0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_NVME_ABTS,
776e96a22b0SJames Smart 			 "6213 NVMEx LS REQ Abort: Unable to locate req x%p\n",
777e96a22b0SJames Smart 			 pnvme_lsreq);
7786b6e8963SJames Smart 	return -EINVAL;
779e96a22b0SJames Smart }
780e96a22b0SJames Smart 
7819aa09e98SJames Smart static int
7829aa09e98SJames Smart lpfc_nvme_xmt_ls_rsp(struct nvme_fc_local_port *localport,
7839aa09e98SJames Smart 		     struct nvme_fc_remote_port *remoteport,
7849aa09e98SJames Smart 		     struct nvmefc_ls_rsp *ls_rsp)
7859aa09e98SJames Smart {
7869aa09e98SJames Smart 	struct lpfc_async_xchg_ctx *axchg =
7879aa09e98SJames Smart 		container_of(ls_rsp, struct lpfc_async_xchg_ctx, ls_rsp);
7889aa09e98SJames Smart 	struct lpfc_nvme_lport *lport;
7899aa09e98SJames Smart 	int rc;
7909aa09e98SJames Smart 
7919aa09e98SJames Smart 	if (axchg->phba->pport->load_flag & FC_UNLOADING)
7929aa09e98SJames Smart 		return -ENODEV;
7939aa09e98SJames Smart 
7949aa09e98SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
7959aa09e98SJames Smart 
7969aa09e98SJames Smart 	rc = __lpfc_nvme_xmt_ls_rsp(axchg, ls_rsp, __lpfc_nvme_xmt_ls_rsp_cmp);
7979aa09e98SJames Smart 
7989aa09e98SJames Smart 	if (rc) {
7999aa09e98SJames Smart 		/*
8009aa09e98SJames Smart 		 * unless the failure is due to having already sent
8019aa09e98SJames Smart 		 * the response, an abort will be generated for the
8029aa09e98SJames Smart 		 * exchange if the rsp can't be sent.
8039aa09e98SJames Smart 		 */
8049aa09e98SJames Smart 		if (rc != -EALREADY)
8059aa09e98SJames Smart 			atomic_inc(&lport->xmt_ls_abort);
8069aa09e98SJames Smart 		return rc;
8079aa09e98SJames Smart 	}
8089aa09e98SJames Smart 
8099aa09e98SJames Smart 	return 0;
8109aa09e98SJames Smart }
8119aa09e98SJames Smart 
812e96a22b0SJames Smart /**
813e96a22b0SJames Smart  * lpfc_nvme_ls_abort - Abort a prior NVME LS request
81409d99705SLee Jones  * @pnvme_lport: Transport localport that LS is to be issued from.
81509d99705SLee Jones  * @pnvme_rport: Transport remoteport that LS is to be sent to.
81609d99705SLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
817e96a22b0SJames Smart  *
818e96a22b0SJames Smart  * Driver registers this routine to abort a NVME LS request that is
819e96a22b0SJames Smart  * in progress (from the transports perspective).
82001649561SJames Smart  **/
82101649561SJames Smart static void
82201649561SJames Smart lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
82301649561SJames Smart 		   struct nvme_fc_remote_port *pnvme_rport,
82401649561SJames Smart 		   struct nvmefc_ls_req *pnvme_lsreq)
82501649561SJames Smart {
82601649561SJames Smart 	struct lpfc_nvme_lport *lport;
82701649561SJames Smart 	struct lpfc_vport *vport;
82801649561SJames Smart 	struct lpfc_nodelist *ndlp;
829e96a22b0SJames Smart 	int ret;
83001649561SJames Smart 
83101649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
83266a210ffSJames Smart 	if (unlikely(!lport))
83366a210ffSJames Smart 		return;
83401649561SJames Smart 	vport = lport->vport;
83501649561SJames Smart 
8363386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
8373386f4bdSJames Smart 		return;
8383386f4bdSJames Smart 
83901649561SJames Smart 	ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
84001649561SJames Smart 
841e96a22b0SJames Smart 	ret = __lpfc_nvme_ls_abort(vport, ndlp, pnvme_lsreq);
842e96a22b0SJames Smart 	if (!ret)
8434b056682SJames Smart 		atomic_inc(&lport->xmt_ls_abort);
84401649561SJames Smart }
84501649561SJames Smart 
84601649561SJames Smart /* Fix up the existing sgls for NVME IO. */
8475fd11085SJames Smart static inline void
84801649561SJames Smart lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
849c490850aSJames Smart 		       struct lpfc_io_buf *lpfc_ncmd,
85001649561SJames Smart 		       struct nvmefc_fcp_req *nCmd)
85101649561SJames Smart {
8524e565cf0SJames Smart 	struct lpfc_hba  *phba = vport->phba;
85301649561SJames Smart 	struct sli4_sge *sgl;
85401649561SJames Smart 	union lpfc_wqe128 *wqe;
85501649561SJames Smart 	uint32_t *wptr, *dptr;
85601649561SJames Smart 
85701649561SJames Smart 	/*
8584e565cf0SJames Smart 	 * Get a local pointer to the built-in wqe and correct
8594e565cf0SJames Smart 	 * the cmd size to match NVME's 96 bytes and fix
8604e565cf0SJames Smart 	 * the dma address.
8614e565cf0SJames Smart 	 */
8624e565cf0SJames Smart 
863205e8240SJames Smart 	wqe = &lpfc_ncmd->cur_iocbq.wqe;
8644e565cf0SJames Smart 
8654e565cf0SJames Smart 	/*
86601649561SJames Smart 	 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
86701649561SJames Smart 	 * match NVME.  NVME sends 96 bytes. Also, use the
86801649561SJames Smart 	 * nvme commands command and response dma addresses
86901649561SJames Smart 	 * rather than the virtual memory to ease the restore
87001649561SJames Smart 	 * operation.
87101649561SJames Smart 	 */
8720794d601SJames Smart 	sgl = lpfc_ncmd->dma_sgl;
87301649561SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
8744e565cf0SJames Smart 	if (phba->cfg_nvme_embed_cmd) {
8754e565cf0SJames Smart 		sgl->addr_hi = 0;
8764e565cf0SJames Smart 		sgl->addr_lo = 0;
8774e565cf0SJames Smart 
8784e565cf0SJames Smart 		/* Word 0-2 - NVME CMND IU (embedded payload) */
8794e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
8804e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeSize = 56;
8814e565cf0SJames Smart 		wqe->generic.bde.addrHigh = 0;
8824e565cf0SJames Smart 		wqe->generic.bde.addrLow =  64;  /* Word 16 */
8834e565cf0SJames Smart 
8845fd11085SJames Smart 		/* Word 10  - dbde is 0, wqes is 1 in template */
88501649561SJames Smart 
88601649561SJames Smart 		/*
88701649561SJames Smart 		 * Embed the payload in the last half of the WQE
88801649561SJames Smart 		 * WQE words 16-30 get the NVME CMD IU payload
88901649561SJames Smart 		 *
890b06a622fSJames Smart 		 * WQE words 16-19 get payload Words 1-4
89101649561SJames Smart 		 * WQE words 20-21 get payload Words 6-7
89201649561SJames Smart 		 * WQE words 22-29 get payload Words 16-23
89301649561SJames Smart 		 */
894b06a622fSJames Smart 		wptr = &wqe->words[16];  /* WQE ptr */
89501649561SJames Smart 		dptr = (uint32_t *)nCmd->cmdaddr;  /* payload ptr */
896b06a622fSJames Smart 		dptr++;			/* Skip Word 0 in payload */
89701649561SJames Smart 
898b06a622fSJames Smart 		*wptr++ = *dptr++;	/* Word 1 */
89901649561SJames Smart 		*wptr++ = *dptr++;	/* Word 2 */
90001649561SJames Smart 		*wptr++ = *dptr++;	/* Word 3 */
90101649561SJames Smart 		*wptr++ = *dptr++;	/* Word 4 */
90201649561SJames Smart 		dptr++;			/* Skip Word 5 in payload */
90301649561SJames Smart 		*wptr++ = *dptr++;	/* Word 6 */
90401649561SJames Smart 		*wptr++ = *dptr++;	/* Word 7 */
90501649561SJames Smart 		dptr += 8;		/* Skip Words 8-15 in payload */
90601649561SJames Smart 		*wptr++ = *dptr++;	/* Word 16 */
90701649561SJames Smart 		*wptr++ = *dptr++;	/* Word 17 */
90801649561SJames Smart 		*wptr++ = *dptr++;	/* Word 18 */
90901649561SJames Smart 		*wptr++ = *dptr++;	/* Word 19 */
91001649561SJames Smart 		*wptr++ = *dptr++;	/* Word 20 */
91101649561SJames Smart 		*wptr++ = *dptr++;	/* Word 21 */
91201649561SJames Smart 		*wptr++ = *dptr++;	/* Word 22 */
91301649561SJames Smart 		*wptr   = *dptr;	/* Word 23 */
9145fd11085SJames Smart 	} else {
9155fd11085SJames Smart 		sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->cmddma));
9165fd11085SJames Smart 		sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->cmddma));
9175fd11085SJames Smart 
9185fd11085SJames Smart 		/* Word 0-2 - NVME CMND IU Inline BDE */
9195fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
9205fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeSize = nCmd->cmdlen;
9215fd11085SJames Smart 		wqe->generic.bde.addrHigh = sgl->addr_hi;
9225fd11085SJames Smart 		wqe->generic.bde.addrLow =  sgl->addr_lo;
9235fd11085SJames Smart 
9245fd11085SJames Smart 		/* Word 10 */
9255fd11085SJames Smart 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
9265fd11085SJames Smart 		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
9275fd11085SJames Smart 	}
9285fd11085SJames Smart 
9295fd11085SJames Smart 	sgl++;
9305fd11085SJames Smart 
9315fd11085SJames Smart 	/* Setup the physical region for the FCP RSP */
9325fd11085SJames Smart 	sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
9335fd11085SJames Smart 	sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
9345fd11085SJames Smart 	sgl->word2 = le32_to_cpu(sgl->word2);
9355fd11085SJames Smart 	if (nCmd->sg_cnt)
9365fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
9375fd11085SJames Smart 	else
9385fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 1);
9395fd11085SJames Smart 	sgl->word2 = cpu_to_le32(sgl->word2);
9405fd11085SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->rsplen);
94101649561SJames Smart }
94201649561SJames Smart 
943bd2cdd5eSJames Smart 
94409d99705SLee Jones /*
94501649561SJames Smart  * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
94601649561SJames Smart  *
94701649561SJames Smart  * Driver registers this routine as it io request handler.  This
94801649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
94901649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
95001649561SJames Smart  *
95101649561SJames Smart  * Return value :
95201649561SJames Smart  *   0 - Success
95301649561SJames Smart  *   TODO: What are the failure codes.
95401649561SJames Smart  **/
95501649561SJames Smart static void
95601649561SJames Smart lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
95701649561SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
95801649561SJames Smart {
959c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd =
960c490850aSJames Smart 		(struct lpfc_io_buf *)pwqeIn->context1;
96101649561SJames Smart 	struct lpfc_vport *vport = pwqeIn->vport;
96201649561SJames Smart 	struct nvmefc_fcp_req *nCmd;
96301649561SJames Smart 	struct nvme_fc_ersp_iu *ep;
96401649561SJames Smart 	struct nvme_fc_cmd_iu *cp;
96501649561SJames Smart 	struct lpfc_nodelist *ndlp;
966bbe3012bSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
9674b056682SJames Smart 	struct lpfc_nvme_lport *lport;
968352b205aSArnd Bergmann 	uint32_t code, status, idx;
96901649561SJames Smart 	uint16_t cid, sqhd, data;
97001649561SJames Smart 	uint32_t *ptr;
971840eda96SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
972840eda96SJames Smart 	int cpu;
973840eda96SJames Smart #endif
97401649561SJames Smart 
97501649561SJames Smart 	/* Sanity check on return of outstanding command */
9764d5e789aSJames Smart 	if (!lpfc_ncmd) {
977372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9784d5e789aSJames Smart 				 "6071 Null lpfc_ncmd pointer. No "
9794d5e789aSJames Smart 				 "release, skip completion\n");
9804d5e789aSJames Smart 		return;
9814d5e789aSJames Smart 	}
9824d5e789aSJames Smart 
983c2017260SJames Smart 	/* Guard against abort handler being called at same time */
984c2017260SJames Smart 	spin_lock(&lpfc_ncmd->buf_lock);
985c2017260SJames Smart 
986c2017260SJames Smart 	if (!lpfc_ncmd->nvmeCmd) {
987c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
988372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
98932350664SJames Smart 				 "6066 Missing cmpl ptrs: lpfc_ncmd x%px, "
99032350664SJames Smart 				 "nvmeCmd x%px\n",
9910b05e9feSJames Smart 				 lpfc_ncmd, lpfc_ncmd->nvmeCmd);
9924d5e789aSJames Smart 
9934d5e789aSJames Smart 		/* Release the lpfc_ncmd regardless of the missing elements. */
9944d5e789aSJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
99501649561SJames Smart 		return;
99601649561SJames Smart 	}
99701649561SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
9984b056682SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
99966a210ffSJames Smart 
10004c47efc1SJames Smart 	idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
10014c47efc1SJames Smart 	phba->sli4_hba.hdwq[idx].nvme_cstat.io_cmpls++;
10024c47efc1SJames Smart 
10031df09449SJames Smart 	if (unlikely(status && vport->localport)) {
10044b056682SJames Smart 		lport = (struct lpfc_nvme_lport *)vport->localport->private;
10051df09449SJames Smart 		if (lport) {
10064b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
10074b056682SJames Smart 				atomic_inc(&lport->cmpl_fcp_xb);
10084b056682SJames Smart 			atomic_inc(&lport->cmpl_fcp_err);
10094b056682SJames Smart 		}
101066a210ffSJames Smart 	}
101101649561SJames Smart 
1012bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
1013bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
10144b056682SJames Smart 			 status, wcqe->parameter);
101501649561SJames Smart 	/*
101601649561SJames Smart 	 * Catch race where our node has transitioned, but the
101701649561SJames Smart 	 * transport is still transitioning.
101801649561SJames Smart 	 */
10190b05e9feSJames Smart 	ndlp = lpfc_ncmd->ndlp;
1020307e3380SJames Smart 	if (!ndlp) {
1021372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
102201649561SJames Smart 				 "6062 Ignoring NVME cmpl.  No ndlp\n");
102301649561SJames Smart 		goto out_err;
102401649561SJames Smart 	}
102501649561SJames Smart 
102601649561SJames Smart 	code = bf_get(lpfc_wcqe_c_code, wcqe);
102701649561SJames Smart 	if (code == CQE_CODE_NVME_ERSP) {
102801649561SJames Smart 		/* For this type of CQE, we need to rebuild the rsp */
102901649561SJames Smart 		ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
103001649561SJames Smart 
103101649561SJames Smart 		/*
103201649561SJames Smart 		 * Get Command Id from cmd to plug into response. This
103301649561SJames Smart 		 * code is not needed in the next NVME Transport drop.
103401649561SJames Smart 		 */
103501649561SJames Smart 		cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
103601649561SJames Smart 		cid = cp->sqe.common.command_id;
103701649561SJames Smart 
103801649561SJames Smart 		/*
103901649561SJames Smart 		 * RSN is in CQE word 2
104001649561SJames Smart 		 * SQHD is in CQE Word 3 bits 15:0
104101649561SJames Smart 		 * Cmd Specific info is in CQE Word 1
104201649561SJames Smart 		 * and in CQE Word 0 bits 15:0
104301649561SJames Smart 		 */
104401649561SJames Smart 		sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
104501649561SJames Smart 
104601649561SJames Smart 		/* Now lets build the NVME ERSP IU */
104701649561SJames Smart 		ep->iu_len = cpu_to_be16(8);
104801649561SJames Smart 		ep->rsn = wcqe->parameter;
104901649561SJames Smart 		ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
105001649561SJames Smart 		ep->rsvd12 = 0;
105101649561SJames Smart 		ptr = (uint32_t *)&ep->cqe.result.u64;
105201649561SJames Smart 		*ptr++ = wcqe->total_data_placed;
105301649561SJames Smart 		data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
105401649561SJames Smart 		*ptr = (uint32_t)data;
105501649561SJames Smart 		ep->cqe.sq_head = sqhd;
105601649561SJames Smart 		ep->cqe.sq_id =  nCmd->sqid;
105701649561SJames Smart 		ep->cqe.command_id = cid;
105801649561SJames Smart 		ep->cqe.status = 0;
105901649561SJames Smart 
106001649561SJames Smart 		lpfc_ncmd->status = IOSTAT_SUCCESS;
106101649561SJames Smart 		lpfc_ncmd->result = 0;
106201649561SJames Smart 		nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
106301649561SJames Smart 		nCmd->transferred_length = nCmd->payload_length;
106401649561SJames Smart 	} else {
10654b056682SJames Smart 		lpfc_ncmd->status = (status & LPFC_IOCB_STATUS_MASK);
1066952c303bSDick Kennedy 		lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
106701649561SJames Smart 
106801649561SJames Smart 		/* For NVME, the only failure path that results in an
106901649561SJames Smart 		 * IO error is when the adapter rejects it.  All other
107001649561SJames Smart 		 * conditions are a success case and resolved by the
107101649561SJames Smart 		 * transport.
107201649561SJames Smart 		 * IOSTAT_FCP_RSP_ERROR means:
107301649561SJames Smart 		 * 1. Length of data received doesn't match total
107401649561SJames Smart 		 *    transfer length in WQE
107501649561SJames Smart 		 * 2. If the RSP payload does NOT match these cases:
107601649561SJames Smart 		 *    a. RSP length 12/24 bytes and all zeros
107701649561SJames Smart 		 *    b. NVME ERSP
107801649561SJames Smart 		 */
107901649561SJames Smart 		switch (lpfc_ncmd->status) {
108001649561SJames Smart 		case IOSTAT_SUCCESS:
108101649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
108201649561SJames Smart 			nCmd->rcv_rsplen = 0;
108301649561SJames Smart 			nCmd->status = 0;
108401649561SJames Smart 			break;
108501649561SJames Smart 		case IOSTAT_FCP_RSP_ERROR:
108601649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
108701649561SJames Smart 			nCmd->rcv_rsplen = wcqe->parameter;
108801649561SJames Smart 			nCmd->status = 0;
108901649561SJames Smart 			/* Sanity check */
109001649561SJames Smart 			if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
109101649561SJames Smart 				break;
1092372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
109301649561SJames Smart 					 "6081 NVME Completion Protocol Error: "
109486c67379SJames Smart 					 "xri %x status x%x result x%x "
109586c67379SJames Smart 					 "placed x%x\n",
109686c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
109701649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
109801649561SJames Smart 					 wcqe->total_data_placed);
109901649561SJames Smart 			break;
1100952c303bSDick Kennedy 		case IOSTAT_LOCAL_REJECT:
1101952c303bSDick Kennedy 			/* Let fall through to set command final state. */
1102952c303bSDick Kennedy 			if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
1103952c303bSDick Kennedy 				lpfc_printf_vlog(vport, KERN_INFO,
1104952c303bSDick Kennedy 					 LOG_NVME_IOERR,
110532350664SJames Smart 					 "6032 Delay Aborted cmd x%px "
110632350664SJames Smart 					 "nvme cmd x%px, xri x%x, "
1107952c303bSDick Kennedy 					 "xb %d\n",
1108952c303bSDick Kennedy 					 lpfc_ncmd, nCmd,
1109952c303bSDick Kennedy 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
1110952c303bSDick Kennedy 					 bf_get(lpfc_wcqe_c_xb, wcqe));
1111df561f66SGustavo A. R. Silva 			fallthrough;
111201649561SJames Smart 		default:
111301649561SJames Smart out_err:
1114e3246a12SJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
111586c67379SJames Smart 					 "6072 NVME Completion Error: xri %x "
11162c013a3aSJames Smart 					 "status x%x result x%x [x%x] "
11172c013a3aSJames Smart 					 "placed x%x\n",
111886c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
111901649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
11202c013a3aSJames Smart 					 wcqe->parameter,
112101649561SJames Smart 					 wcqe->total_data_placed);
112201649561SJames Smart 			nCmd->transferred_length = 0;
112301649561SJames Smart 			nCmd->rcv_rsplen = 0;
11248e009ce8SJames Smart 			nCmd->status = NVME_SC_INTERNAL;
112501649561SJames Smart 		}
112601649561SJames Smart 	}
112701649561SJames Smart 
112801649561SJames Smart 	/* pick up SLI4 exhange busy condition */
112901649561SJames Smart 	if (bf_get(lpfc_wcqe_c_xb, wcqe))
113001649561SJames Smart 		lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
113101649561SJames Smart 	else
113201649561SJames Smart 		lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
113301649561SJames Smart 
113401649561SJames Smart 	/* Update stats and complete the IO.  There is
113501649561SJames Smart 	 * no need for dma unprep because the nvme_transport
113601649561SJames Smart 	 * owns the dma address.
113701649561SJames Smart 	 */
1138bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1139c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start) {
1140bd2cdd5eSJames Smart 		lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
11412fcbc569SJames Smart 		lpfc_ncmd->ts_data_io = ktime_get_ns();
11422fcbc569SJames Smart 		phba->ktime_last_cmd = lpfc_ncmd->ts_data_io;
11432fcbc569SJames Smart 		lpfc_io_ktime(phba, lpfc_ncmd);
1144bd2cdd5eSJames Smart 	}
1145840eda96SJames Smart 	if (unlikely(phba->hdwqstat_on & LPFC_CHECK_NVME_IO)) {
1146d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
1147840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
114863df6d63SJames Smart 		if (lpfc_ncmd->cpu != cpu)
114963df6d63SJames Smart 			lpfc_printf_vlog(vport,
115063df6d63SJames Smart 					 KERN_INFO, LOG_NVME_IOERR,
1151bd2cdd5eSJames Smart 					 "6701 CPU Check cmpl: "
1152bd2cdd5eSJames Smart 					 "cpu %d expect %d\n",
115363df6d63SJames Smart 					 cpu, lpfc_ncmd->cpu);
1154bd2cdd5eSJames Smart 	}
1155bd2cdd5eSJames Smart #endif
1156952c303bSDick Kennedy 
1157952c303bSDick Kennedy 	/* NVME targets need completion held off until the abort exchange
1158add9d6beSJames Smart 	 * completes unless the NVME Rport is getting unregistered.
1159952c303bSDick Kennedy 	 */
1160add9d6beSJames Smart 
11613fd78355SJames Smart 	if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
116291455b85SJames Smart 		freqpriv = nCmd->private;
116391455b85SJames Smart 		freqpriv->nvme_buf = NULL;
11643fd78355SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
1165c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
1166c2017260SJames Smart 		nCmd->done(nCmd);
1167c2017260SJames Smart 	} else
1168c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
116901649561SJames Smart 
1170952c303bSDick Kennedy 	/* Call release with XB=1 to queue the IO into the abort list. */
117101649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
117201649561SJames Smart }
117301649561SJames Smart 
117401649561SJames Smart 
117501649561SJames Smart /**
117601649561SJames Smart  * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
117709d99705SLee Jones  * @vport: pointer to a host virtual N_Port data structure
11783e749170SLee Jones  * @lpfc_ncmd: Pointer to lpfc scsi command
117909d99705SLee Jones  * @pnode: pointer to a node-list data structure
118009d99705SLee Jones  * @cstat: pointer to the control status structure
118101649561SJames Smart  *
118201649561SJames Smart  * Driver registers this routine as it io request handler.  This
118301649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
118401649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
118501649561SJames Smart  *
118601649561SJames Smart  * Return value :
118701649561SJames Smart  *   0 - Success
118801649561SJames Smart  *   TODO: What are the failure codes.
118901649561SJames Smart  **/
119001649561SJames Smart static int
119101649561SJames Smart lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
1192c490850aSJames Smart 		      struct lpfc_io_buf *lpfc_ncmd,
119366a210ffSJames Smart 		      struct lpfc_nodelist *pnode,
11944c47efc1SJames Smart 		      struct lpfc_fc4_ctrl_stat *cstat)
119501649561SJames Smart {
119601649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
119701649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
119801649561SJames Smart 	struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
1199205e8240SJames Smart 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
120001649561SJames Smart 	uint32_t req_len;
120101649561SJames Smart 
120201649561SJames Smart 	/*
120301649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
120401649561SJames Smart 	 * the single mapping, or neither.
120501649561SJames Smart 	 */
120601649561SJames Smart 	if (nCmd->sg_cnt) {
120701649561SJames Smart 		if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
12085fd11085SJames Smart 			/* From the iwrite template, initialize words 7 - 11 */
12095fd11085SJames Smart 			memcpy(&wqe->words[7],
12105fd11085SJames Smart 			       &lpfc_iwrite_cmd_template.words[7],
12115fd11085SJames Smart 			       sizeof(uint32_t) * 5);
12125fd11085SJames Smart 
12135fd11085SJames Smart 			/* Word 4 */
12145fd11085SJames Smart 			wqe->fcp_iwrite.total_xfer_len = nCmd->payload_length;
12155fd11085SJames Smart 
121601649561SJames Smart 			/* Word 5 */
121701649561SJames Smart 			if ((phba->cfg_nvme_enable_fb) &&
121801649561SJames Smart 			    (pnode->nlp_flag & NLP_FIRSTBURST)) {
121901649561SJames Smart 				req_len = lpfc_ncmd->nvmeCmd->payload_length;
122001649561SJames Smart 				if (req_len < pnode->nvme_fb_size)
122101649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
122201649561SJames Smart 						req_len;
122301649561SJames Smart 				else
122401649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
122501649561SJames Smart 						pnode->nvme_fb_size;
12265fd11085SJames Smart 			} else {
12275fd11085SJames Smart 				wqe->fcp_iwrite.initial_xfer_len = 0;
122801649561SJames Smart 			}
12294c47efc1SJames Smart 			cstat->output_requests++;
123001649561SJames Smart 		} else {
12315fd11085SJames Smart 			/* From the iread template, initialize words 7 - 11 */
12325fd11085SJames Smart 			memcpy(&wqe->words[7],
12335fd11085SJames Smart 			       &lpfc_iread_cmd_template.words[7],
12345fd11085SJames Smart 			       sizeof(uint32_t) * 5);
123501649561SJames Smart 
12365fd11085SJames Smart 			/* Word 4 */
12375fd11085SJames Smart 			wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
123801649561SJames Smart 
12395fd11085SJames Smart 			/* Word 5 */
12405fd11085SJames Smart 			wqe->fcp_iread.rsrvd5 = 0;
124101649561SJames Smart 
12424c47efc1SJames Smart 			cstat->input_requests++;
124301649561SJames Smart 		}
124401649561SJames Smart 	} else {
12455fd11085SJames Smart 		/* From the icmnd template, initialize words 4 - 11 */
12465fd11085SJames Smart 		memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
12475fd11085SJames Smart 		       sizeof(uint32_t) * 8);
12484c47efc1SJames Smart 		cstat->control_requests++;
124901649561SJames Smart 	}
12500d8af096SJames Smart 
12510d8af096SJames Smart 	if (pnode->nlp_nvme_info & NLP_NVME_NSLER)
12520d8af096SJames Smart 		bf_set(wqe_erp, &wqe->generic.wqe_com, 1);
125301649561SJames Smart 	/*
125401649561SJames Smart 	 * Finish initializing those WQE fields that are independent
125501649561SJames Smart 	 * of the nvme_cmnd request_buffer
125601649561SJames Smart 	 */
125701649561SJames Smart 
12585fd11085SJames Smart 	/* Word 3 */
12595fd11085SJames Smart 	bf_set(payload_offset_len, &wqe->fcp_icmd,
12605fd11085SJames Smart 	       (nCmd->rsplen + nCmd->cmdlen));
12615fd11085SJames Smart 
126201649561SJames Smart 	/* Word 6 */
126301649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
126401649561SJames Smart 	       phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
126501649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
126601649561SJames Smart 
126701649561SJames Smart 	/* Word 8 */
126801649561SJames Smart 	wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
126901649561SJames Smart 
127001649561SJames Smart 	/* Word 9 */
127101649561SJames Smart 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
127201649561SJames Smart 
1273*840a4701SJames Smart 	/* Word 10 */
1274*840a4701SJames Smart 	bf_set(wqe_xchg, &wqe->fcp_iwrite.wqe_com, LPFC_NVME_XCHG);
1275*840a4701SJames Smart 
1276414abe0aSJames Smart 	/* Words 13 14 15 are for PBDE support */
1277414abe0aSJames Smart 
127801649561SJames Smart 	pwqeq->vport = vport;
127901649561SJames Smart 	return 0;
128001649561SJames Smart }
128101649561SJames Smart 
128201649561SJames Smart 
128301649561SJames Smart /**
128401649561SJames Smart  * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
128509d99705SLee Jones  * @vport: pointer to a host virtual N_Port data structure
12863e749170SLee Jones  * @lpfc_ncmd: Pointer to lpfc scsi command
128701649561SJames Smart  *
128801649561SJames Smart  * Driver registers this routine as it io request handler.  This
128901649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
129001649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
129101649561SJames Smart  *
129201649561SJames Smart  * Return value :
129301649561SJames Smart  *   0 - Success
129401649561SJames Smart  *   TODO: What are the failure codes.
129501649561SJames Smart  **/
129601649561SJames Smart static int
129701649561SJames Smart lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
1298c490850aSJames Smart 		      struct lpfc_io_buf *lpfc_ncmd)
129901649561SJames Smart {
130001649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
130101649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1302205e8240SJames Smart 	union lpfc_wqe128 *wqe = &lpfc_ncmd->cur_iocbq.wqe;
13030794d601SJames Smart 	struct sli4_sge *sgl = lpfc_ncmd->dma_sgl;
1304d79c9e9dSJames Smart 	struct sli4_hybrid_sgl *sgl_xtra = NULL;
130501649561SJames Smart 	struct scatterlist *data_sg;
130601649561SJames Smart 	struct sli4_sge *first_data_sgl;
13070bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
1308d79c9e9dSJames Smart 	dma_addr_t physaddr = 0;
130901649561SJames Smart 	uint32_t num_bde = 0;
1310d79c9e9dSJames Smart 	uint32_t dma_len = 0;
131101649561SJames Smart 	uint32_t dma_offset = 0;
1312d79c9e9dSJames Smart 	int nseg, i, j;
1313d79c9e9dSJames Smart 	bool lsp_just_set = false;
131401649561SJames Smart 
131501649561SJames Smart 	/* Fix up the command and response DMA stuff. */
131601649561SJames Smart 	lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
131701649561SJames Smart 
131801649561SJames Smart 	/*
131901649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
132001649561SJames Smart 	 * the single mapping, or neither.
132101649561SJames Smart 	 */
132201649561SJames Smart 	if (nCmd->sg_cnt) {
132301649561SJames Smart 		/*
132401649561SJames Smart 		 * Jump over the cmd and rsp SGEs.  The fix routine
132501649561SJames Smart 		 * has already adjusted for this.
132601649561SJames Smart 		 */
132701649561SJames Smart 		sgl += 2;
132801649561SJames Smart 
132901649561SJames Smart 		first_data_sgl = sgl;
133001649561SJames Smart 		lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
133181e6a637SJames Smart 		if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
1332372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
133301649561SJames Smart 					"6058 Too many sg segments from "
133401649561SJames Smart 					"NVME Transport.  Max %d, "
133501649561SJames Smart 					"nvmeIO sg_cnt %d\n",
133629bfd55aSJames Smart 					phba->cfg_nvme_seg_cnt + 1,
133701649561SJames Smart 					lpfc_ncmd->seg_cnt);
133801649561SJames Smart 			lpfc_ncmd->seg_cnt = 0;
133901649561SJames Smart 			return 1;
134001649561SJames Smart 		}
134101649561SJames Smart 
134201649561SJames Smart 		/*
134301649561SJames Smart 		 * The driver established a maximum scatter-gather segment count
134401649561SJames Smart 		 * during probe that limits the number of sg elements in any
134501649561SJames Smart 		 * single nvme command.  Just run through the seg_cnt and format
134601649561SJames Smart 		 * the sge's.
134701649561SJames Smart 		 */
134801649561SJames Smart 		nseg = nCmd->sg_cnt;
134901649561SJames Smart 		data_sg = nCmd->first_sgl;
1350d79c9e9dSJames Smart 
1351d79c9e9dSJames Smart 		/* for tracking the segment boundaries */
1352d79c9e9dSJames Smart 		j = 2;
135301649561SJames Smart 		for (i = 0; i < nseg; i++) {
135401649561SJames Smart 			if (data_sg == NULL) {
1355372c187bSDick Kennedy 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
135601649561SJames Smart 						"6059 dptr err %d, nseg %d\n",
135701649561SJames Smart 						i, nseg);
135801649561SJames Smart 				lpfc_ncmd->seg_cnt = 0;
135901649561SJames Smart 				return 1;
136001649561SJames Smart 			}
1361d79c9e9dSJames Smart 
1362d79c9e9dSJames Smart 			sgl->word2 = 0;
1363d79c9e9dSJames Smart 			if ((num_bde + 1) == nseg) {
1364d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 1);
1365d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_type, sgl,
1366d79c9e9dSJames Smart 				       LPFC_SGE_TYPE_DATA);
1367d79c9e9dSJames Smart 			} else {
1368d79c9e9dSJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 0);
1369d79c9e9dSJames Smart 
1370d79c9e9dSJames Smart 				/* expand the segment */
1371d79c9e9dSJames Smart 				if (!lsp_just_set &&
1372d79c9e9dSJames Smart 				    !((j + 1) % phba->border_sge_num) &&
1373d79c9e9dSJames Smart 				    ((nseg - 1) != i)) {
1374d79c9e9dSJames Smart 					/* set LSP type */
1375d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_type, sgl,
1376d79c9e9dSJames Smart 					       LPFC_SGE_TYPE_LSP);
1377d79c9e9dSJames Smart 
1378d79c9e9dSJames Smart 					sgl_xtra = lpfc_get_sgl_per_hdwq(
1379d79c9e9dSJames Smart 							phba, lpfc_ncmd);
1380d79c9e9dSJames Smart 
1381d79c9e9dSJames Smart 					if (unlikely(!sgl_xtra)) {
1382d79c9e9dSJames Smart 						lpfc_ncmd->seg_cnt = 0;
1383d79c9e9dSJames Smart 						return 1;
1384d79c9e9dSJames Smart 					}
1385d79c9e9dSJames Smart 					sgl->addr_lo = cpu_to_le32(putPaddrLow(
1386d79c9e9dSJames Smart 						       sgl_xtra->dma_phys_sgl));
1387d79c9e9dSJames Smart 					sgl->addr_hi = cpu_to_le32(putPaddrHigh(
1388d79c9e9dSJames Smart 						       sgl_xtra->dma_phys_sgl));
1389d79c9e9dSJames Smart 
1390d79c9e9dSJames Smart 				} else {
1391d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_type, sgl,
1392d79c9e9dSJames Smart 					       LPFC_SGE_TYPE_DATA);
1393d79c9e9dSJames Smart 				}
1394d79c9e9dSJames Smart 			}
1395d79c9e9dSJames Smart 
1396d79c9e9dSJames Smart 			if (!(bf_get(lpfc_sli4_sge_type, sgl) &
1397d79c9e9dSJames Smart 				     LPFC_SGE_TYPE_LSP)) {
1398d79c9e9dSJames Smart 				if ((nseg - 1) == i)
1399d79c9e9dSJames Smart 					bf_set(lpfc_sli4_sge_last, sgl, 1);
1400d79c9e9dSJames Smart 
140101649561SJames Smart 				physaddr = data_sg->dma_address;
140201649561SJames Smart 				dma_len = data_sg->length;
1403d79c9e9dSJames Smart 				sgl->addr_lo = cpu_to_le32(
1404d79c9e9dSJames Smart 							 putPaddrLow(physaddr));
1405d79c9e9dSJames Smart 				sgl->addr_hi = cpu_to_le32(
1406d79c9e9dSJames Smart 							putPaddrHigh(physaddr));
1407d79c9e9dSJames Smart 
140801649561SJames Smart 				bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
140901649561SJames Smart 				sgl->word2 = cpu_to_le32(sgl->word2);
141001649561SJames Smart 				sgl->sge_len = cpu_to_le32(dma_len);
141101649561SJames Smart 
141201649561SJames Smart 				dma_offset += dma_len;
141301649561SJames Smart 				data_sg = sg_next(data_sg);
1414d79c9e9dSJames Smart 
141501649561SJames Smart 				sgl++;
1416d79c9e9dSJames Smart 
1417d79c9e9dSJames Smart 				lsp_just_set = false;
1418d79c9e9dSJames Smart 			} else {
1419d79c9e9dSJames Smart 				sgl->word2 = cpu_to_le32(sgl->word2);
1420d79c9e9dSJames Smart 
1421d79c9e9dSJames Smart 				sgl->sge_len = cpu_to_le32(
1422d79c9e9dSJames Smart 						     phba->cfg_sg_dma_buf_size);
1423d79c9e9dSJames Smart 
1424d79c9e9dSJames Smart 				sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
1425d79c9e9dSJames Smart 				i = i - 1;
1426d79c9e9dSJames Smart 
1427d79c9e9dSJames Smart 				lsp_just_set = true;
1428d79c9e9dSJames Smart 			}
1429d79c9e9dSJames Smart 
1430d79c9e9dSJames Smart 			j++;
143101649561SJames Smart 		}
1432414abe0aSJames Smart 		if (phba->cfg_enable_pbde) {
14330bc2b7c5SJames Smart 			/* Use PBDE support for first SGL only, offset == 0 */
14340bc2b7c5SJames Smart 			/* Words 13-15 */
14350bc2b7c5SJames Smart 			bde = (struct ulp_bde64 *)
14360bc2b7c5SJames Smart 				&wqe->words[13];
14370bc2b7c5SJames Smart 			bde->addrLow = first_data_sgl->addr_lo;
14380bc2b7c5SJames Smart 			bde->addrHigh = first_data_sgl->addr_hi;
14390bc2b7c5SJames Smart 			bde->tus.f.bdeSize =
14400bc2b7c5SJames Smart 				le32_to_cpu(first_data_sgl->sge_len);
14410bc2b7c5SJames Smart 			bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
14420bc2b7c5SJames Smart 			bde->tus.w = cpu_to_le32(bde->tus.w);
1443b101eb27SJames Smart 
1444b101eb27SJames Smart 			/* Word 11 */
1445b101eb27SJames Smart 			bf_set(wqe_pbde, &wqe->generic.wqe_com, 1);
14465fd11085SJames Smart 		} else {
14475fd11085SJames Smart 			memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
14480bc2b7c5SJames Smart 			bf_set(wqe_pbde, &wqe->generic.wqe_com, 0);
14495fd11085SJames Smart 		}
14500bc2b7c5SJames Smart 
1451414abe0aSJames Smart 	} else {
14520794d601SJames Smart 		lpfc_ncmd->seg_cnt = 0;
14530794d601SJames Smart 
145401649561SJames Smart 		/* For this clause to be valid, the payload_length
145501649561SJames Smart 		 * and sg_cnt must zero.
145601649561SJames Smart 		 */
145701649561SJames Smart 		if (nCmd->payload_length != 0) {
1458372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
145901649561SJames Smart 					"6063 NVME DMA Prep Err: sg_cnt %d "
146001649561SJames Smart 					"payload_length x%x\n",
146101649561SJames Smart 					nCmd->sg_cnt, nCmd->payload_length);
146201649561SJames Smart 			return 1;
146301649561SJames Smart 		}
146401649561SJames Smart 	}
146501649561SJames Smart 	return 0;
146601649561SJames Smart }
146701649561SJames Smart 
146801649561SJames Smart /**
146901649561SJames Smart  * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
14703e749170SLee Jones  * @pnvme_lport: Pointer to the driver's local port data
14713e749170SLee Jones  * @pnvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
147201649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
14733e749170SLee Jones  * @pnvme_fcreq: IO request from nvme fc to driver.
147401649561SJames Smart  *
147501649561SJames Smart  * Driver registers this routine as it io request handler.  This
147601649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
14773e749170SLee Jones  * data structure to the rport indicated in @lpfc_nvme_rport.
147801649561SJames Smart  *
147901649561SJames Smart  * Return value :
148001649561SJames Smart  *   0 - Success
148101649561SJames Smart  *   TODO: What are the failure codes.
148201649561SJames Smart  **/
148301649561SJames Smart static int
148401649561SJames Smart lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
148501649561SJames Smart 			struct nvme_fc_remote_port *pnvme_rport,
148601649561SJames Smart 			void *hw_queue_handle,
148701649561SJames Smart 			struct nvmefc_fcp_req *pnvme_fcreq)
148801649561SJames Smart {
148901649561SJames Smart 	int ret = 0;
1490cf1a1d3eSJames Smart 	int expedite = 0;
149163df6d63SJames Smart 	int idx, cpu;
149201649561SJames Smart 	struct lpfc_nvme_lport *lport;
14934c47efc1SJames Smart 	struct lpfc_fc4_ctrl_stat *cstat;
149401649561SJames Smart 	struct lpfc_vport *vport;
149501649561SJames Smart 	struct lpfc_hba *phba;
149601649561SJames Smart 	struct lpfc_nodelist *ndlp;
1497c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
149801649561SJames Smart 	struct lpfc_nvme_rport *rport;
149901649561SJames Smart 	struct lpfc_nvme_qhandle *lpfc_queue_info;
1500c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
1501cf1a1d3eSJames Smart 	struct nvme_common_command *sqe;
1502bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1503bd2cdd5eSJames Smart 	uint64_t start = 0;
1504bd2cdd5eSJames Smart #endif
150501649561SJames Smart 
1506c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1507c3725bdcSJames Smart 	 * have timing races.
1508c3725bdcSJames Smart 	 */
150901649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1510c3725bdcSJames Smart 	if (unlikely(!lport)) {
1511c3725bdcSJames Smart 		ret = -EINVAL;
1512c3725bdcSJames Smart 		goto out_fail;
1513c3725bdcSJames Smart 	}
1514c3725bdcSJames Smart 
151501649561SJames Smart 	vport = lport->vport;
1516c3725bdcSJames Smart 
1517c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
151844c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
151944c2757bSJames Smart 				 "6117 Fail IO, NULL hw_queue_handle\n");
152044c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1521cd240071SJames Smart 		ret = -EBUSY;
1522c3725bdcSJames Smart 		goto out_fail;
1523c3725bdcSJames Smart 	}
1524c3725bdcSJames Smart 
152501649561SJames Smart 	phba = vport->phba;
152601649561SJames Smart 
15271df09449SJames Smart 	if (unlikely(vport->load_flag & FC_UNLOADING)) {
152844c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
152944c2757bSJames Smart 				 "6124 Fail IO, Driver unload\n");
153044c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
15313386f4bdSJames Smart 		ret = -ENODEV;
15323386f4bdSJames Smart 		goto out_fail;
15333386f4bdSJames Smart 	}
15343386f4bdSJames Smart 
1535c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1536c3725bdcSJames Smart 	if (unlikely(!freqpriv)) {
153744c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
153844c2757bSJames Smart 				 "6158 Fail IO, NULL request data\n");
153944c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1540c3725bdcSJames Smart 		ret = -EINVAL;
1541b7672ae6SDick Kennedy 		goto out_fail;
1542b7672ae6SDick Kennedy 	}
1543b7672ae6SDick Kennedy 
1544bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1545bd2cdd5eSJames Smart 	if (phba->ktime_on)
1546bd2cdd5eSJames Smart 		start = ktime_get_ns();
1547bd2cdd5eSJames Smart #endif
154801649561SJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
154901649561SJames Smart 	lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
155001649561SJames Smart 
155101649561SJames Smart 	/*
155201649561SJames Smart 	 * Catch race where our node has transitioned, but the
155301649561SJames Smart 	 * transport is still transitioning.
155401649561SJames Smart 	 */
155501649561SJames Smart 	ndlp = rport->ndlp;
1556307e3380SJames Smart 	if (!ndlp) {
155744c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
155832350664SJames Smart 				 "6053 Busy IO, ndlp not ready: rport x%px "
155932350664SJames Smart 				  "ndlp x%px, DID x%06x\n",
156001649561SJames Smart 				 rport, ndlp, pnvme_rport->port_id);
156144c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1562cd240071SJames Smart 		ret = -EBUSY;
156301649561SJames Smart 		goto out_fail;
156401649561SJames Smart 	}
156501649561SJames Smart 
156601649561SJames Smart 	/* The remote node has to be a mapped target or it's an error. */
156701649561SJames Smart 	if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
156801649561SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
156944c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
157044c2757bSJames Smart 				 "6036 Fail IO, DID x%06x not ready for "
1571cd240071SJames Smart 				 "IO. State x%x, Type x%x Flg x%x\n",
157244c2757bSJames Smart 				 pnvme_rport->port_id,
1573cd240071SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type,
1574e9b11083SJames Smart 				 ndlp->fc4_xpt_flags);
15754b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_bad_ndlp);
1576cd240071SJames Smart 		ret = -EBUSY;
157701649561SJames Smart 		goto out_fail;
157801649561SJames Smart 
157901649561SJames Smart 	}
158001649561SJames Smart 
1581cf1a1d3eSJames Smart 	/* Currently only NVME Keep alive commands should be expedited
1582cf1a1d3eSJames Smart 	 * if the driver runs out of a resource. These should only be
1583cf1a1d3eSJames Smart 	 * issued on the admin queue, qidx 0
1584cf1a1d3eSJames Smart 	 */
1585cf1a1d3eSJames Smart 	if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
1586cf1a1d3eSJames Smart 		sqe = &((struct nvme_fc_cmd_iu *)
1587cf1a1d3eSJames Smart 			pnvme_fcreq->cmdaddr)->sqe.common;
1588cf1a1d3eSJames Smart 		if (sqe->opcode == nvme_admin_keep_alive)
1589cf1a1d3eSJames Smart 			expedite = 1;
1590cf1a1d3eSJames Smart 	}
1591cf1a1d3eSJames Smart 
159201649561SJames Smart 	/* The node is shared with FCP IO, make sure the IO pending count does
159301649561SJames Smart 	 * not exceed the programmed depth.
159401649561SJames Smart 	 */
15952a5b7d62SJames Smart 	if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1596cf1a1d3eSJames Smart 		if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
1597cf1a1d3eSJames Smart 		    !expedite) {
159844c2757bSJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
159944c2757bSJames Smart 					 "6174 Fail IO, ndlp qdepth exceeded: "
16004d5e789aSJames Smart 					 "idx %d DID %x pend %d qdepth %d\n",
16014d5e789aSJames Smart 					 lpfc_queue_info->index, ndlp->nlp_DID,
16024d5e789aSJames Smart 					 atomic_read(&ndlp->cmd_pending),
16034d5e789aSJames Smart 					 ndlp->cmd_qdepth);
16044b056682SJames Smart 			atomic_inc(&lport->xmt_fcp_qdepth);
1605cd22d605SDick Kennedy 			ret = -EBUSY;
160601649561SJames Smart 			goto out_fail;
160701649561SJames Smart 		}
16082a5b7d62SJames Smart 	}
160901649561SJames Smart 
16106a828b0fSJames Smart 	/* Lookup Hardware Queue index based on fcp_io_sched module parameter */
161145aa312eSJames Smart 	if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
161245aa312eSJames Smart 		idx = lpfc_queue_info->index;
161345aa312eSJames Smart 	} else {
1614d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
16156a828b0fSJames Smart 		idx = phba->sli4_hba.cpu_map[cpu].hdwq;
161645aa312eSJames Smart 	}
161745aa312eSJames Smart 
161845aa312eSJames Smart 	lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, idx, expedite);
161901649561SJames Smart 	if (lpfc_ncmd == NULL) {
16204b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_noxri);
162101649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
162244c2757bSJames Smart 				 "6065 Fail IO, driver buffer pool is empty: "
162344c2757bSJames Smart 				 "idx %d DID %x\n",
162444c2757bSJames Smart 				 lpfc_queue_info->index, ndlp->nlp_DID);
1625cd22d605SDick Kennedy 		ret = -EBUSY;
162601649561SJames Smart 		goto out_fail;
162701649561SJames Smart 	}
1628bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1629c8a4ce0bSDick Kennedy 	if (start) {
1630bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_start = start;
1631bd2cdd5eSJames Smart 		lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
1632c8a4ce0bSDick Kennedy 	} else {
1633c8a4ce0bSDick Kennedy 		lpfc_ncmd->ts_cmd_start = 0;
1634bd2cdd5eSJames Smart 	}
1635bd2cdd5eSJames Smart #endif
163601649561SJames Smart 
163701649561SJames Smart 	/*
163801649561SJames Smart 	 * Store the data needed by the driver to issue, abort, and complete
163901649561SJames Smart 	 * an IO.
164001649561SJames Smart 	 * Do not let the IO hang out forever.  There is no midlayer issuing
164101649561SJames Smart 	 * an abort so inform the FW of the maximum IO pending time.
164201649561SJames Smart 	 */
1643bbe3012bSJames Smart 	freqpriv->nvme_buf = lpfc_ncmd;
164401649561SJames Smart 	lpfc_ncmd->nvmeCmd = pnvme_fcreq;
1645318083adSJames Smart 	lpfc_ncmd->ndlp = ndlp;
16460794d601SJames Smart 	lpfc_ncmd->qidx = lpfc_queue_info->qidx;
164701649561SJames Smart 
164801649561SJames Smart 	/*
164901649561SJames Smart 	 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
165001649561SJames Smart 	 * This identfier was create in our hardware queue create callback
165101649561SJames Smart 	 * routine. The driver now is dependent on the IO queue steering from
165201649561SJames Smart 	 * the transport.  We are trusting the upper NVME layers know which
165301649561SJames Smart 	 * index to use and that they have affinitized a CPU to this hardware
165401649561SJames Smart 	 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
165501649561SJames Smart 	 */
165666a210ffSJames Smart 	lpfc_ncmd->cur_iocbq.hba_wqidx = idx;
16574c47efc1SJames Smart 	cstat = &phba->sli4_hba.hdwq[idx].nvme_cstat;
165866a210ffSJames Smart 
165966a210ffSJames Smart 	lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp, cstat);
166066a210ffSJames Smart 	ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
166166a210ffSJames Smart 	if (ret) {
166244c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
166344c2757bSJames Smart 				 "6175 Fail IO, Prep DMA: "
166444c2757bSJames Smart 				 "idx %d DID %x\n",
166544c2757bSJames Smart 				 lpfc_queue_info->index, ndlp->nlp_DID);
166644c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
166766a210ffSJames Smart 		ret = -ENOMEM;
166866a210ffSJames Smart 		goto out_free_nvme_buf;
166966a210ffSJames Smart 	}
167066a210ffSJames Smart 
1671bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1672bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
1673bd2cdd5eSJames Smart 			 lpfc_queue_info->index, ndlp->nlp_DID);
1674bd2cdd5eSJames Smart 
16751fbf9742SJames Smart 	ret = lpfc_sli4_issue_wqe(phba, lpfc_ncmd->hdwq, &lpfc_ncmd->cur_iocbq);
167601649561SJames Smart 	if (ret) {
16774b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_wqerr);
1678e3246a12SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
167944c2757bSJames Smart 				 "6113 Fail IO, Could not issue WQE err %x "
168001649561SJames Smart 				 "sid: x%x did: x%x oxid: x%x\n",
168101649561SJames Smart 				 ret, vport->fc_myDID, ndlp->nlp_DID,
168201649561SJames Smart 				 lpfc_ncmd->cur_iocbq.sli4_xritag);
168301649561SJames Smart 		goto out_free_nvme_buf;
168401649561SJames Smart 	}
168501649561SJames Smart 
1686c490850aSJames Smart 	if (phba->cfg_xri_rebalancing)
1687c490850aSJames Smart 		lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_ncmd->hdwq_no);
1688c490850aSJames Smart 
1689bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1690c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start)
1691bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1692bd2cdd5eSJames Smart 
1693840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVME_IO) {
1694d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
1695840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
169663df6d63SJames Smart 		lpfc_ncmd->cpu = cpu;
169763df6d63SJames Smart 		if (idx != cpu)
1698bd2cdd5eSJames Smart 			lpfc_printf_vlog(vport,
169963df6d63SJames Smart 					 KERN_INFO, LOG_NVME_IOERR,
1700bd2cdd5eSJames Smart 					"6702 CPU Check cmd: "
1701bd2cdd5eSJames Smart 					"cpu %d wq %d\n",
1702bd2cdd5eSJames Smart 					lpfc_ncmd->cpu,
1703bd2cdd5eSJames Smart 					lpfc_queue_info->index);
1704bd2cdd5eSJames Smart 	}
1705bd2cdd5eSJames Smart #endif
170601649561SJames Smart 	return 0;
170701649561SJames Smart 
170801649561SJames Smart  out_free_nvme_buf:
17092cee7808SJames Smart 	if (lpfc_ncmd->nvmeCmd->sg_cnt) {
17102cee7808SJames Smart 		if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
17114c47efc1SJames Smart 			cstat->output_requests--;
17122cee7808SJames Smart 		else
17134c47efc1SJames Smart 			cstat->input_requests--;
17142cee7808SJames Smart 	} else
17154c47efc1SJames Smart 		cstat->control_requests--;
171601649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
171701649561SJames Smart  out_fail:
171801649561SJames Smart 	return ret;
171901649561SJames Smart }
172001649561SJames Smart 
172101649561SJames Smart /**
172201649561SJames Smart  * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
172301649561SJames Smart  * @phba: Pointer to HBA context object
172401649561SJames Smart  * @cmdiocb: Pointer to command iocb object.
17253e749170SLee Jones  * @abts_cmpl: Pointer to wcqe complete object.
172601649561SJames Smart  *
172701649561SJames Smart  * This is the callback function for any NVME FCP IO that was aborted.
172801649561SJames Smart  *
172901649561SJames Smart  * Return value:
173001649561SJames Smart  *   None
173101649561SJames Smart  **/
173201649561SJames Smart void
173301649561SJames Smart lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
173401649561SJames Smart 			   struct lpfc_wcqe_complete *abts_cmpl)
173501649561SJames Smart {
1736e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
173701649561SJames Smart 			"6145 ABORT_XRI_CN completing on rpi x%x "
173801649561SJames Smart 			"original iotag x%x, abort cmd iotag x%x "
173901649561SJames Smart 			"req_tag x%x, status x%x, hwstatus x%x\n",
174001649561SJames Smart 			cmdiocb->iocb.un.acxri.abortContextTag,
174101649561SJames Smart 			cmdiocb->iocb.un.acxri.abortIoTag,
174201649561SJames Smart 			cmdiocb->iotag,
174301649561SJames Smart 			bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
174401649561SJames Smart 			bf_get(lpfc_wcqe_c_status, abts_cmpl),
174501649561SJames Smart 			bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
174601649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocb);
174701649561SJames Smart }
174801649561SJames Smart 
174901649561SJames Smart /**
175001649561SJames Smart  * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
17513e749170SLee Jones  * @pnvme_lport: Pointer to the driver's local port data
17523e749170SLee Jones  * @pnvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
175301649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
17543e749170SLee Jones  * @pnvme_fcreq: IO request from nvme fc to driver.
175501649561SJames Smart  *
175601649561SJames Smart  * Driver registers this routine as its nvme request io abort handler.  This
175701649561SJames Smart  * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
175801649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.  This routine
175901649561SJames Smart  * is executed asynchronously - one the target is validated as "MAPPED" and
176001649561SJames Smart  * ready for IO, the driver issues the abort request and returns.
176101649561SJames Smart  *
176201649561SJames Smart  * Return value:
176301649561SJames Smart  *   None
176401649561SJames Smart  **/
176501649561SJames Smart static void
176601649561SJames Smart lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
176701649561SJames Smart 		    struct nvme_fc_remote_port *pnvme_rport,
176801649561SJames Smart 		    void *hw_queue_handle,
176901649561SJames Smart 		    struct nvmefc_fcp_req *pnvme_fcreq)
177001649561SJames Smart {
177101649561SJames Smart 	struct lpfc_nvme_lport *lport;
177201649561SJames Smart 	struct lpfc_vport *vport;
177301649561SJames Smart 	struct lpfc_hba *phba;
1774c490850aSJames Smart 	struct lpfc_io_buf *lpfc_nbuf;
177501649561SJames Smart 	struct lpfc_iocbq *abts_buf;
177601649561SJames Smart 	struct lpfc_iocbq *nvmereq_wqe;
1777c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
177801649561SJames Smart 	unsigned long flags;
177901649561SJames Smart 	int ret_val;
178001649561SJames Smart 
1781c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1782c3725bdcSJames Smart 	 * have timing races.
1783c3725bdcSJames Smart 	 */
178401649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1785c3725bdcSJames Smart 	if (unlikely(!lport))
1786c3725bdcSJames Smart 		return;
178701649561SJames Smart 
1788c3725bdcSJames Smart 	vport = lport->vport;
1789c3725bdcSJames Smart 
1790c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
1791c3725bdcSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1792c3725bdcSJames Smart 				 "6129 Fail Abort, HW Queue Handle NULL.\n");
1793c3725bdcSJames Smart 		return;
1794c3725bdcSJames Smart 	}
1795c3725bdcSJames Smart 
1796c3725bdcSJames Smart 	phba = vport->phba;
1797c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1798c3725bdcSJames Smart 
1799c3725bdcSJames Smart 	if (unlikely(!freqpriv))
1800c3725bdcSJames Smart 		return;
18013386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
18023386f4bdSJames Smart 		return;
18033386f4bdSJames Smart 
180401649561SJames Smart 	/* Announce entry to new IO submit field. */
180586c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
180601649561SJames Smart 			 "6002 Abort Request to rport DID x%06x "
180732350664SJames Smart 			 "for nvme_fc_req x%px\n",
180801649561SJames Smart 			 pnvme_rport->port_id,
180901649561SJames Smart 			 pnvme_fcreq);
181001649561SJames Smart 
181101649561SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
181201649561SJames Smart 	 * cleared when the reset is complete and rings reestablished.
181301649561SJames Smart 	 */
181401649561SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
181501649561SJames Smart 	/* driver queued commands are in process of being flushed */
1816c00f62e6SJames Smart 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
181701649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1818372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
181901649561SJames Smart 				 "6139 Driver in reset cleanup - flushing "
182001649561SJames Smart 				 "NVME Req now.  hba_flag x%x\n",
182101649561SJames Smart 				 phba->hba_flag);
182201649561SJames Smart 		return;
182301649561SJames Smart 	}
182401649561SJames Smart 
1825bbe3012bSJames Smart 	lpfc_nbuf = freqpriv->nvme_buf;
182601649561SJames Smart 	if (!lpfc_nbuf) {
182701649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1828372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
182901649561SJames Smart 				 "6140 NVME IO req has no matching lpfc nvme "
183001649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
183101649561SJames Smart 		return;
183201649561SJames Smart 	} else if (!lpfc_nbuf->nvmeCmd) {
183301649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1834372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
183501649561SJames Smart 				 "6141 lpfc NVME IO req has no nvme_fcreq "
183601649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
183701649561SJames Smart 		return;
183801649561SJames Smart 	}
18392b7824d0SJames Smart 	nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
184001649561SJames Smart 
1841c2017260SJames Smart 	/* Guard against IO completion being called at same time */
1842c2017260SJames Smart 	spin_lock(&lpfc_nbuf->buf_lock);
1843c2017260SJames Smart 
184401649561SJames Smart 	/*
184501649561SJames Smart 	 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
184601649561SJames Smart 	 * state must match the nvme_fcreq passed by the nvme
184701649561SJames Smart 	 * transport.  If they don't match, it is likely the driver
184801649561SJames Smart 	 * has already completed the NVME IO and the nvme transport
184901649561SJames Smart 	 * has not seen it yet.
185001649561SJames Smart 	 */
185101649561SJames Smart 	if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
1852372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
185301649561SJames Smart 				 "6143 NVME req mismatch: "
185432350664SJames Smart 				 "lpfc_nbuf x%px nvmeCmd x%px, "
185532350664SJames Smart 				 "pnvme_fcreq x%px.  Skipping Abort xri x%x\n",
185601649561SJames Smart 				 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
18572b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1858c2017260SJames Smart 		goto out_unlock;
185901649561SJames Smart 	}
186001649561SJames Smart 
186101649561SJames Smart 	/* Don't abort IOs no longer on the pending queue. */
186201649561SJames Smart 	if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
1863372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
186432350664SJames Smart 				 "6142 NVME IO req x%px not queued - skipping "
18652b7824d0SJames Smart 				 "abort req xri x%x\n",
18662b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1867c2017260SJames Smart 		goto out_unlock;
186801649561SJames Smart 	}
186901649561SJames Smart 
18704b056682SJames Smart 	atomic_inc(&lport->xmt_fcp_abort);
1871bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1872bd2cdd5eSJames Smart 			 nvmereq_wqe->sli4_xritag,
187300cefeb9SJames Smart 			 nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
1874bd2cdd5eSJames Smart 
187501649561SJames Smart 	/* Outstanding abort is in progress */
187601649561SJames Smart 	if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
1877372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
187801649561SJames Smart 				 "6144 Outstanding NVME I/O Abort Request "
187932350664SJames Smart 				 "still pending on nvme_fcreq x%px, "
188032350664SJames Smart 				 "lpfc_ncmd %px xri x%x\n",
18812b7824d0SJames Smart 				 pnvme_fcreq, lpfc_nbuf,
18822b7824d0SJames Smart 				 nvmereq_wqe->sli4_xritag);
1883c2017260SJames Smart 		goto out_unlock;
188401649561SJames Smart 	}
188501649561SJames Smart 
188601649561SJames Smart 	abts_buf = __lpfc_sli_get_iocbq(phba);
188701649561SJames Smart 	if (!abts_buf) {
1888372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
188901649561SJames Smart 				 "6136 No available abort wqes. Skipping "
189032350664SJames Smart 				 "Abts req for nvme_fcreq x%px xri x%x\n",
18912b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1892c2017260SJames Smart 		goto out_unlock;
189301649561SJames Smart 	}
189401649561SJames Smart 
189501649561SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
189601649561SJames Smart 	nvmereq_wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
189701649561SJames Smart 
189851f8e43eSJames Smart 	lpfc_nvme_prep_abort_wqe(abts_buf, nvmereq_wqe->sli4_xritag, 0);
189901649561SJames Smart 
190001649561SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
190101649561SJames Smart 	abts_buf->iocb_flag |= LPFC_IO_NVME;
190201649561SJames Smart 	abts_buf->hba_wqidx = nvmereq_wqe->hba_wqidx;
190301649561SJames Smart 	abts_buf->vport = vport;
190401649561SJames Smart 	abts_buf->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
19051fbf9742SJames Smart 	ret_val = lpfc_sli4_issue_wqe(phba, lpfc_nbuf->hdwq, abts_buf);
1906c2017260SJames Smart 	spin_unlock(&lpfc_nbuf->buf_lock);
190701649561SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1908cd22d605SDick Kennedy 	if (ret_val) {
1909372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
191001649561SJames Smart 				 "6137 Failed abts issue_wqe with status x%x "
191132350664SJames Smart 				 "for nvme_fcreq x%px.\n",
191201649561SJames Smart 				 ret_val, pnvme_fcreq);
191301649561SJames Smart 		lpfc_sli_release_iocbq(phba, abts_buf);
191401649561SJames Smart 		return;
191501649561SJames Smart 	}
191601649561SJames Smart 
191786c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
19182b7824d0SJames Smart 			 "6138 Transport Abort NVME Request Issued for "
191901649561SJames Smart 			 "ox_id x%x on reqtag x%x\n",
192001649561SJames Smart 			 nvmereq_wqe->sli4_xritag,
192101649561SJames Smart 			 abts_buf->iotag);
1922c2017260SJames Smart 	return;
1923c2017260SJames Smart 
1924c2017260SJames Smart out_unlock:
1925c2017260SJames Smart 	spin_unlock(&lpfc_nbuf->buf_lock);
1926c2017260SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1927c2017260SJames Smart 	return;
192801649561SJames Smart }
192901649561SJames Smart 
193001649561SJames Smart /* Declare and initialization an instance of the FC NVME template. */
193101649561SJames Smart static struct nvme_fc_port_template lpfc_nvme_template = {
193201649561SJames Smart 	/* initiator-based functions */
193301649561SJames Smart 	.localport_delete  = lpfc_nvme_localport_delete,
193401649561SJames Smart 	.remoteport_delete = lpfc_nvme_remoteport_delete,
193501649561SJames Smart 	.create_queue = lpfc_nvme_create_queue,
193601649561SJames Smart 	.delete_queue = lpfc_nvme_delete_queue,
193701649561SJames Smart 	.ls_req       = lpfc_nvme_ls_req,
193801649561SJames Smart 	.fcp_io       = lpfc_nvme_fcp_io_submit,
193901649561SJames Smart 	.ls_abort     = lpfc_nvme_ls_abort,
194001649561SJames Smart 	.fcp_abort    = lpfc_nvme_fcp_abort,
19419aa09e98SJames Smart 	.xmt_ls_rsp   = lpfc_nvme_xmt_ls_rsp,
194201649561SJames Smart 
194301649561SJames Smart 	.max_hw_queues = 1,
194401649561SJames Smart 	.max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
194501649561SJames Smart 	.max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
194601649561SJames Smart 	.dma_boundary = 0xFFFFFFFF,
194701649561SJames Smart 
194801649561SJames Smart 	/* Sizes of additional private data for data structures.
194901649561SJames Smart 	 * No use for the last two sizes at this time.
195001649561SJames Smart 	 */
195101649561SJames Smart 	.local_priv_sz = sizeof(struct lpfc_nvme_lport),
195201649561SJames Smart 	.remote_priv_sz = sizeof(struct lpfc_nvme_rport),
195301649561SJames Smart 	.lsrqst_priv_sz = 0,
1954bbe3012bSJames Smart 	.fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
195501649561SJames Smart };
195601649561SJames Smart 
19573e749170SLee Jones /*
19585e5b511dSJames Smart  * lpfc_get_nvme_buf - Get a nvme buffer from io_buf_list of the HBA
195901649561SJames Smart  *
19605e5b511dSJames Smart  * This routine removes a nvme buffer from head of @hdwq io_buf_list
196101649561SJames Smart  * and returns to caller.
196201649561SJames Smart  *
196301649561SJames Smart  * Return codes:
196401649561SJames Smart  *   NULL - Error
196501649561SJames Smart  *   Pointer to lpfc_nvme_buf - Success
196601649561SJames Smart  **/
1967c490850aSJames Smart static struct lpfc_io_buf *
1968cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19695e5b511dSJames Smart 		  int idx, int expedite)
197001649561SJames Smart {
1971c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
19725e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
19730794d601SJames Smart 	struct sli4_sge *sgl;
19740794d601SJames Smart 	struct lpfc_iocbq *pwqeq;
19750794d601SJames Smart 	union lpfc_wqe128 *wqe;
197601649561SJames Smart 
1977c490850aSJames Smart 	lpfc_ncmd = lpfc_get_io_buf(phba, NULL, idx, expedite);
19782a5b7d62SJames Smart 
19790794d601SJames Smart 	if (lpfc_ncmd) {
19800794d601SJames Smart 		pwqeq = &(lpfc_ncmd->cur_iocbq);
19810794d601SJames Smart 		wqe = &pwqeq->wqe;
19820794d601SJames Smart 
19830794d601SJames Smart 		/* Setup key fields in buffer that may have been changed
19840794d601SJames Smart 		 * if other protocols used this buffer.
19850794d601SJames Smart 		 */
19860794d601SJames Smart 		pwqeq->iocb_flag = LPFC_IO_NVME;
19870794d601SJames Smart 		pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
19880794d601SJames Smart 		lpfc_ncmd->start_time = jiffies;
19890794d601SJames Smart 		lpfc_ncmd->flags = 0;
19900794d601SJames Smart 
19910794d601SJames Smart 		/* Rsp SGE will be filled in when we rcv an IO
19920794d601SJames Smart 		 * from the NVME Layer to be sent.
19930794d601SJames Smart 		 * The cmd is going to be embedded so we need a SKIP SGE.
19940794d601SJames Smart 		 */
19950794d601SJames Smart 		sgl = lpfc_ncmd->dma_sgl;
19960794d601SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
19970794d601SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
19980794d601SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
19990794d601SJames Smart 		/* Fill in word 3 / sgl_len during cmd submission */
20000794d601SJames Smart 
2001d9f492a1SJames Smart 		/* Initialize 64 bytes only */
20020794d601SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
20030794d601SJames Smart 
20040794d601SJames Smart 		if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
20052a5b7d62SJames Smart 			atomic_inc(&ndlp->cmd_pending);
2006c490850aSJames Smart 			lpfc_ncmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
20072a5b7d62SJames Smart 		}
20085e5b511dSJames Smart 
2009c490850aSJames Smart 	} else {
2010c490850aSJames Smart 		qp = &phba->sli4_hba.hdwq[idx];
20115e5b511dSJames Smart 		qp->empty_io_bufs++;
2012c490850aSJames Smart 	}
20135e5b511dSJames Smart 
201401649561SJames Smart 	return  lpfc_ncmd;
201501649561SJames Smart }
201601649561SJames Smart 
201701649561SJames Smart /**
201801649561SJames Smart  * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
201901649561SJames Smart  * @phba: The Hba for which this call is being executed.
202001649561SJames Smart  * @lpfc_ncmd: The nvme buffer which is being released.
202101649561SJames Smart  *
202201649561SJames Smart  * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
20235e5b511dSJames Smart  * lpfc_io_buf_list list. For SLI4 XRI's are tied to the nvme buffer
202401649561SJames Smart  * and cannot be reused for at least RA_TOV amount of time if it was
202501649561SJames Smart  * aborted.
202601649561SJames Smart  **/
202701649561SJames Smart static void
2028c490850aSJames Smart lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd)
202901649561SJames Smart {
20305e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
203101649561SJames Smart 	unsigned long iflag = 0;
203201649561SJames Smart 
2033c490850aSJames Smart 	if ((lpfc_ncmd->flags & LPFC_SBUF_BUMP_QDEPTH) && lpfc_ncmd->ndlp)
20342a5b7d62SJames Smart 		atomic_dec(&lpfc_ncmd->ndlp->cmd_pending);
20352a5b7d62SJames Smart 
20362a5b7d62SJames Smart 	lpfc_ncmd->ndlp = NULL;
2037c490850aSJames Smart 	lpfc_ncmd->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
20382a5b7d62SJames Smart 
20391fbf9742SJames Smart 	qp = lpfc_ncmd->hdwq;
204022770cbaSJames Smart 	if (unlikely(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
204186c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
204286c67379SJames Smart 				"6310 XB release deferred for "
204386c67379SJames Smart 				"ox_id x%x on reqtag x%x\n",
204486c67379SJames Smart 				lpfc_ncmd->cur_iocbq.sli4_xritag,
204586c67379SJames Smart 				lpfc_ncmd->cur_iocbq.iotag);
204686c67379SJames Smart 
2047c00f62e6SJames Smart 		spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
204801649561SJames Smart 		list_add_tail(&lpfc_ncmd->list,
2049c00f62e6SJames Smart 			&qp->lpfc_abts_io_buf_list);
20505e5b511dSJames Smart 		qp->abts_nvme_io_bufs++;
2051c00f62e6SJames Smart 		spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
2052c490850aSJames Smart 	} else
2053c490850aSJames Smart 		lpfc_release_io_buf(phba, (struct lpfc_io_buf *)lpfc_ncmd, qp);
205401649561SJames Smart }
205501649561SJames Smart 
205601649561SJames Smart /**
205701649561SJames Smart  * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
20583e749170SLee Jones  * @vport - the lpfc_vport instance requesting a localport.
205901649561SJames Smart  *
206001649561SJames Smart  * This routine is invoked to create an nvme localport instance to bind
206101649561SJames Smart  * to the nvme_fc_transport.  It is called once during driver load
206201649561SJames Smart  * like lpfc_create_shost after all other services are initialized.
206301649561SJames Smart  * It requires a vport, vpi, and wwns at call time.  Other localport
206401649561SJames Smart  * parameters are modified as the driver's FCID and the Fabric WWN
206501649561SJames Smart  * are established.
206601649561SJames Smart  *
206701649561SJames Smart  * Return codes
206801649561SJames Smart  *      0 - successful
206901649561SJames Smart  *      -ENOMEM - no heap memory available
207001649561SJames Smart  *      other values - from nvme registration upcall
207101649561SJames Smart  **/
207201649561SJames Smart int
207301649561SJames Smart lpfc_nvme_create_localport(struct lpfc_vport *vport)
207401649561SJames Smart {
2075166d7211SJames Smart 	int ret = 0;
207601649561SJames Smart 	struct lpfc_hba  *phba = vport->phba;
207701649561SJames Smart 	struct nvme_fc_port_info nfcp_info;
207801649561SJames Smart 	struct nvme_fc_local_port *localport;
207901649561SJames Smart 	struct lpfc_nvme_lport *lport;
208001649561SJames Smart 
208101649561SJames Smart 	/* Initialize this localport instance.  The vport wwn usage ensures
208201649561SJames Smart 	 * that NPIV is accounted for.
208301649561SJames Smart 	 */
208401649561SJames Smart 	memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
208501649561SJames Smart 	nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
208601649561SJames Smart 	nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
208701649561SJames Smart 	nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
208801649561SJames Smart 
20895b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
20905b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
20915b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
20924d4c4a4aSJames Smart 	 */
20934d4c4a4aSJames Smart 	lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
209401649561SJames Smart 
2095542ddc9bSJames Smart 	/* Advertise how many hw queues we support based on cfg_hdw_queue,
2096542ddc9bSJames Smart 	 * which will not exceed cpu count.
2097542ddc9bSJames Smart 	 */
2098cdb42becSJames Smart 	lpfc_nvme_template.max_hw_queues = phba->cfg_hdw_queue;
209966a210ffSJames Smart 
2100faf5a744SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_FC))
2101faf5a744SArnd Bergmann 		return ret;
2102faf5a744SArnd Bergmann 
210301649561SJames Smart 	/* localport is allocated from the stack, but the registration
210401649561SJames Smart 	 * call allocates heap memory as well as the private area.
210501649561SJames Smart 	 */
2106faf5a744SArnd Bergmann 
210701649561SJames Smart 	ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
210801649561SJames Smart 					 &vport->phba->pcidev->dev, &localport);
210901649561SJames Smart 	if (!ret) {
211001649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
211101649561SJames Smart 				 "6005 Successfully registered local "
211232350664SJames Smart 				 "NVME port num %d, localP x%px, private "
211332350664SJames Smart 				 "x%px, sg_seg %d\n",
211401649561SJames Smart 				 localport->port_num, localport,
211501649561SJames Smart 				 localport->private,
211601649561SJames Smart 				 lpfc_nvme_template.max_sgl_segments);
211701649561SJames Smart 
211801649561SJames Smart 		/* Private is our lport size declared in the template. */
211901649561SJames Smart 		lport = (struct lpfc_nvme_lport *)localport->private;
212001649561SJames Smart 		vport->localport = localport;
212101649561SJames Smart 		lport->vport = vport;
212201649561SJames Smart 		vport->nvmei_support = 1;
21236b486ce9SDick Kennedy 
21244b056682SJames Smart 		atomic_set(&lport->xmt_fcp_noxri, 0);
21254b056682SJames Smart 		atomic_set(&lport->xmt_fcp_bad_ndlp, 0);
21264b056682SJames Smart 		atomic_set(&lport->xmt_fcp_qdepth, 0);
212744c2757bSJames Smart 		atomic_set(&lport->xmt_fcp_err, 0);
21284b056682SJames Smart 		atomic_set(&lport->xmt_fcp_wqerr, 0);
21294b056682SJames Smart 		atomic_set(&lport->xmt_fcp_abort, 0);
21304b056682SJames Smart 		atomic_set(&lport->xmt_ls_abort, 0);
21314b056682SJames Smart 		atomic_set(&lport->xmt_ls_err, 0);
21324b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_xb, 0);
21334b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_err, 0);
21344b056682SJames Smart 		atomic_set(&lport->cmpl_ls_xb, 0);
21354b056682SJames Smart 		atomic_set(&lport->cmpl_ls_err, 0);
21369aa09e98SJames Smart 
213766a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsRequests, 0);
213866a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsCmpls, 0);
21396b486ce9SDick Kennedy 	}
21403ebd9b47SJames Smart 
214101649561SJames Smart 	return ret;
214201649561SJames Smart }
214301649561SJames Smart 
2144d964b3e5SBart Van Assche #if (IS_ENABLED(CONFIG_NVME_FC))
2145add9d6beSJames Smart /* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2146add9d6beSJames Smart  *
2147add9d6beSJames Smart  * The driver has to wait for the host nvme transport to callback
2148add9d6beSJames Smart  * indicating the localport has successfully unregistered all
2149add9d6beSJames Smart  * resources.  Since this is an uninterruptible wait, loop every ten
2150add9d6beSJames Smart  * seconds and print a message indicating no progress.
2151add9d6beSJames Smart  *
2152add9d6beSJames Smart  * An uninterruptible wait is used because of the risk of transport-to-
2153add9d6beSJames Smart  * driver state mismatch.
2154add9d6beSJames Smart  */
21553999df75SBart Van Assche static void
2156add9d6beSJames Smart lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
21577961cba6SEwan D. Milne 			   struct lpfc_nvme_lport *lport,
21587961cba6SEwan D. Milne 			   struct completion *lport_unreg_cmp)
2159add9d6beSJames Smart {
2160add9d6beSJames Smart 	u32 wait_tmo;
21612ab70c21SJames Smart 	int ret, i, pending = 0;
21622ab70c21SJames Smart 	struct lpfc_sli_ring  *pring;
21632ab70c21SJames Smart 	struct lpfc_hba  *phba = vport->phba;
2164e7dab164SJames Smart 	struct lpfc_sli4_hdw_queue *qp;
2165e7dab164SJames Smart 	int abts_scsi, abts_nvme;
2166add9d6beSJames Smart 
2167add9d6beSJames Smart 	/* Host transport has to clean up and confirm requiring an indefinite
2168add9d6beSJames Smart 	 * wait. Print a message if a 10 second wait expires and renew the
2169add9d6beSJames Smart 	 * wait. This is unexpected.
2170add9d6beSJames Smart 	 */
2171add9d6beSJames Smart 	wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2172add9d6beSJames Smart 	while (true) {
21737961cba6SEwan D. Milne 		ret = wait_for_completion_timeout(lport_unreg_cmp, wait_tmo);
2174add9d6beSJames Smart 		if (unlikely(!ret)) {
21752ab70c21SJames Smart 			pending = 0;
2176e7dab164SJames Smart 			abts_scsi = 0;
2177e7dab164SJames Smart 			abts_nvme = 0;
21782ab70c21SJames Smart 			for (i = 0; i < phba->cfg_hdw_queue; i++) {
2179e7dab164SJames Smart 				qp = &phba->sli4_hba.hdwq[i];
2180e7dab164SJames Smart 				pring = qp->io_wq->pring;
21812ab70c21SJames Smart 				if (!pring)
21822ab70c21SJames Smart 					continue;
21832ab70c21SJames Smart 				pending += pring->txcmplq_cnt;
2184e7dab164SJames Smart 				abts_scsi += qp->abts_scsi_io_bufs;
2185e7dab164SJames Smart 				abts_nvme += qp->abts_nvme_io_bufs;
21862ab70c21SJames Smart 			}
2187372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
218832350664SJames Smart 					 "6176 Lport x%px Localport x%px wait "
2189e7dab164SJames Smart 					 "timed out. Pending %d [%d:%d]. "
2190e7dab164SJames Smart 					 "Renewing.\n",
2191e7dab164SJames Smart 					 lport, vport->localport, pending,
2192e7dab164SJames Smart 					 abts_scsi, abts_nvme);
2193add9d6beSJames Smart 			continue;
2194add9d6beSJames Smart 		}
2195add9d6beSJames Smart 		break;
2196add9d6beSJames Smart 	}
2197add9d6beSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
219832350664SJames Smart 			 "6177 Lport x%px Localport x%px Complete Success\n",
2199add9d6beSJames Smart 			 lport, vport->localport);
2200add9d6beSJames Smart }
2201d964b3e5SBart Van Assche #endif
2202add9d6beSJames Smart 
220301649561SJames Smart /**
220401649561SJames Smart  * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
22053e749170SLee Jones  * @vport: pointer to a host virtual N_Port data structure
220601649561SJames Smart  *
220701649561SJames Smart  * This routine is invoked to destroy all lports bound to the phba.
220801649561SJames Smart  * The lport memory was allocated by the nvme fc transport and is
220901649561SJames Smart  * released there.  This routine ensures all rports bound to the
221001649561SJames Smart  * lport have been disconnected.
221101649561SJames Smart  *
221201649561SJames Smart  **/
221301649561SJames Smart void
221401649561SJames Smart lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
221501649561SJames Smart {
22167d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
221701649561SJames Smart 	struct nvme_fc_local_port *localport;
221801649561SJames Smart 	struct lpfc_nvme_lport *lport;
221901649561SJames Smart 	int ret;
22207961cba6SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(lport_unreg_cmp);
222101649561SJames Smart 
222201649561SJames Smart 	if (vport->nvmei_support == 0)
222301649561SJames Smart 		return;
222401649561SJames Smart 
222501649561SJames Smart 	localport = vport->localport;
222601649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
222701649561SJames Smart 
222801649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
222932350664SJames Smart 			 "6011 Destroying NVME localport x%px\n",
223001649561SJames Smart 			 localport);
2231166d7211SJames Smart 
223201649561SJames Smart 	/* lport's rport list is clear.  Unregister
223301649561SJames Smart 	 * lport and release resources.
223401649561SJames Smart 	 */
22357961cba6SEwan D. Milne 	lport->lport_unreg_cmp = &lport_unreg_cmp;
223601649561SJames Smart 	ret = nvme_fc_unregister_localport(localport);
2237add9d6beSJames Smart 
2238add9d6beSJames Smart 	/* Wait for completion.  This either blocks
2239add9d6beSJames Smart 	 * indefinitely or succeeds
2240add9d6beSJames Smart 	 */
22417961cba6SEwan D. Milne 	lpfc_nvme_lport_unreg_wait(vport, lport, &lport_unreg_cmp);
22427961cba6SEwan D. Milne 	vport->localport = NULL;
224301649561SJames Smart 
224401649561SJames Smart 	/* Regardless of the unregister upcall response, clear
224501649561SJames Smart 	 * nvmei_support.  All rports are unregistered and the
224601649561SJames Smart 	 * driver will clean up.
224701649561SJames Smart 	 */
224801649561SJames Smart 	vport->nvmei_support = 0;
224901649561SJames Smart 	if (ret == 0) {
225001649561SJames Smart 		lpfc_printf_vlog(vport,
225101649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
225201649561SJames Smart 				 "6009 Unregistered lport Success\n");
225301649561SJames Smart 	} else {
225401649561SJames Smart 		lpfc_printf_vlog(vport,
225501649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
225601649561SJames Smart 				 "6010 Unregistered lport "
225701649561SJames Smart 				 "Failed, status x%x\n",
225801649561SJames Smart 				 ret);
225901649561SJames Smart 	}
2260166d7211SJames Smart #endif
226101649561SJames Smart }
226201649561SJames Smart 
226301649561SJames Smart void
226401649561SJames Smart lpfc_nvme_update_localport(struct lpfc_vport *vport)
226501649561SJames Smart {
22664410a67aSJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
226701649561SJames Smart 	struct nvme_fc_local_port *localport;
226801649561SJames Smart 	struct lpfc_nvme_lport *lport;
226901649561SJames Smart 
227001649561SJames Smart 	localport = vport->localport;
22714410a67aSJames Smart 	if (!localport) {
22724410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
22734410a67aSJames Smart 				 "6710 Update NVME fail. No localport\n");
22744410a67aSJames Smart 		return;
22754410a67aSJames Smart 	}
227601649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
22774410a67aSJames Smart 	if (!lport) {
22784410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
227932350664SJames Smart 				 "6171 Update NVME fail. localP x%px, No lport\n",
22804410a67aSJames Smart 				 localport);
22814410a67aSJames Smart 		return;
22824410a67aSJames Smart 	}
228301649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
228432350664SJames Smart 			 "6012 Update NVME lport x%px did x%x\n",
228501649561SJames Smart 			 localport, vport->fc_myDID);
228601649561SJames Smart 
228701649561SJames Smart 	localport->port_id = vport->fc_myDID;
228801649561SJames Smart 	if (localport->port_id == 0)
228901649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
229001649561SJames Smart 	else
229101649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
229201649561SJames Smart 
229301649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
229432350664SJames Smart 			 "6030 bound lport x%px to DID x%06x\n",
229501649561SJames Smart 			 lport, localport->port_id);
22964410a67aSJames Smart #endif
229701649561SJames Smart }
229801649561SJames Smart 
229901649561SJames Smart int
230001649561SJames Smart lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
230101649561SJames Smart {
23027d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
230301649561SJames Smart 	int ret = 0;
230401649561SJames Smart 	struct nvme_fc_local_port *localport;
230501649561SJames Smart 	struct lpfc_nvme_lport *lport;
230601649561SJames Smart 	struct lpfc_nvme_rport *rport;
230701466024SJames Smart 	struct lpfc_nvme_rport *oldrport;
230801649561SJames Smart 	struct nvme_fc_remote_port *remote_port;
230901649561SJames Smart 	struct nvme_fc_port_info rpinfo;
231093a3922dSJames Smart 	struct lpfc_nodelist *prev_ndlp = NULL;
23111f546823SJames Smart 	struct fc_rport *srport = ndlp->rport;
231201649561SJames Smart 
231301649561SJames Smart 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
231401649561SJames Smart 			 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
231501649561SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type);
231601649561SJames Smart 
231701649561SJames Smart 	localport = vport->localport;
2318bb6a8a2cSDick Kennedy 	if (!localport)
2319bb6a8a2cSDick Kennedy 		return 0;
2320bb6a8a2cSDick Kennedy 
232101649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
232201649561SJames Smart 
23237a06dcd3SJames Smart 	/* NVME rports are not preserved across devloss.
23247a06dcd3SJames Smart 	 * Just register this instance.  Note, rpinfo->dev_loss_tmo
23257a06dcd3SJames Smart 	 * is left 0 to indicate accept transport defaults.  The
23267a06dcd3SJames Smart 	 * driver communicates port role capabilities consistent
23277a06dcd3SJames Smart 	 * with the PRLI response data.
232801649561SJames Smart 	 */
23297a06dcd3SJames Smart 	memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
23307a06dcd3SJames Smart 	rpinfo.port_id = ndlp->nlp_DID;
233101649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET)
23327a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
233301649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
23347a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
233501649561SJames Smart 
23367a06dcd3SJames Smart 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
23377a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
23387a06dcd3SJames Smart 
23397a06dcd3SJames Smart 	rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
23407a06dcd3SJames Smart 	rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
23411f546823SJames Smart 	if (srport)
23421f546823SJames Smart 		rpinfo.dev_loss_tmo = srport->dev_loss_tmo;
23431f546823SJames Smart 	else
23441f546823SJames Smart 		rpinfo.dev_loss_tmo = vport->cfg_devloss_tmo;
234501466024SJames Smart 
2346c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
234701466024SJames Smart 	oldrport = lpfc_ndlp_get_nrport(ndlp);
234861184f17SJames Smart 	if (oldrport) {
234961184f17SJames Smart 		prev_ndlp = oldrport->ndlp;
2350c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
235161184f17SJames Smart 	} else {
2352c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
23534430f7fdSJames Smart 		if (!lpfc_nlp_get(ndlp)) {
23544430f7fdSJames Smart 			dev_warn(&vport->phba->pcidev->dev,
23554430f7fdSJames Smart 				 "Warning - No node ref - exit register\n");
23564430f7fdSJames Smart 			return 0;
23574430f7fdSJames Smart 		}
235861184f17SJames Smart 	}
23593fd78355SJames Smart 
23607a06dcd3SJames Smart 	ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
23617a06dcd3SJames Smart 	if (!ret) {
23627a06dcd3SJames Smart 		/* If the ndlp already has an nrport, this is just
23637a06dcd3SJames Smart 		 * a resume of the existing rport.  Else this is a
23647a06dcd3SJames Smart 		 * new rport.
23657a06dcd3SJames Smart 		 */
2366b15bd3e6SJames Smart 		/* Guard against an unregister/reregister
2367b15bd3e6SJames Smart 		 * race that leaves the WAIT flag set.
2368b15bd3e6SJames Smart 		 */
2369c6adba15SJames Smart 		spin_lock_irq(&ndlp->lock);
2370e9b11083SJames Smart 		ndlp->fc4_xpt_flags &= ~NLP_WAIT_FOR_UNREG;
2371e9b11083SJames Smart 		ndlp->fc4_xpt_flags |= NVME_XPT_REGD;
2372c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
23737a06dcd3SJames Smart 		rport = remote_port->private;
237401466024SJames Smart 		if (oldrport) {
23752b75d0f9SDick Kennedy 
23763fd78355SJames Smart 			/* Sever the ndlp<->rport association
23773fd78355SJames Smart 			 * before dropping the ndlp ref from
23783fd78355SJames Smart 			 * register.
23792b75d0f9SDick Kennedy 			 */
2380c6adba15SJames Smart 			spin_lock_irq(&ndlp->lock);
23812b75d0f9SDick Kennedy 			ndlp->nrport = NULL;
2382e9b11083SJames Smart 			ndlp->fc4_xpt_flags &= ~NLP_WAIT_FOR_UNREG;
2383c6adba15SJames Smart 			spin_unlock_irq(&ndlp->lock);
23842b75d0f9SDick Kennedy 			rport->ndlp = NULL;
23853fd78355SJames Smart 			rport->remoteport = NULL;
2386b04744ceSJames Smart 
2387b04744ceSJames Smart 			/* Reference only removed if previous NDLP is no longer
2388b04744ceSJames Smart 			 * active. It might be just a swap and removing the
2389b04744ceSJames Smart 			 * reference would cause a premature cleanup.
2390b04744ceSJames Smart 			 */
2391b04744ceSJames Smart 			if (prev_ndlp && prev_ndlp != ndlp) {
2392307e3380SJames Smart 				if (!prev_ndlp->nrport)
2393b04744ceSJames Smart 					lpfc_nlp_put(prev_ndlp);
2394b04744ceSJames Smart 			}
23952b75d0f9SDick Kennedy 		}
23962b75d0f9SDick Kennedy 
23972b75d0f9SDick Kennedy 		/* Clean bind the rport to the ndlp. */
239801649561SJames Smart 		rport->remoteport = remote_port;
239901649561SJames Smart 		rport->lport = lport;
24003fd78355SJames Smart 		rport->ndlp = ndlp;
2401c6adba15SJames Smart 		spin_lock_irq(&ndlp->lock);
240201649561SJames Smart 		ndlp->nrport = rport;
2403c6adba15SJames Smart 		spin_unlock_irq(&ndlp->lock);
240401649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO,
240501649561SJames Smart 				 LOG_NVME_DISC | LOG_NODE,
24066825b7bdSJames Smart 				 "6022 Bind lport x%px to remoteport x%px "
24076825b7bdSJames Smart 				 "rport x%px WWNN 0x%llx, "
24087a06dcd3SJames Smart 				 "Rport WWPN 0x%llx DID "
240932350664SJames Smart 				 "x%06x Role x%x, ndlp %p prev_ndlp x%px\n",
241093a3922dSJames Smart 				 lport, remote_port, rport,
241101649561SJames Smart 				 rpinfo.node_name, rpinfo.port_name,
24123fd78355SJames Smart 				 rpinfo.port_id, rpinfo.port_role,
241393a3922dSJames Smart 				 ndlp, prev_ndlp);
241401649561SJames Smart 	} else {
241501649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR,
2416372c187bSDick Kennedy 				 LOG_TRACE_EVENT,
241701649561SJames Smart 				 "6031 RemotePort Registration failed "
241801649561SJames Smart 				 "err: %d, DID x%06x\n",
241901649561SJames Smart 				 ret, ndlp->nlp_DID);
242001649561SJames Smart 	}
24217a06dcd3SJames Smart 
242201649561SJames Smart 	return ret;
2423166d7211SJames Smart #else
2424166d7211SJames Smart 	return 0;
2425166d7211SJames Smart #endif
242601649561SJames Smart }
242701649561SJames Smart 
24283e749170SLee Jones /*
24296f2589f4SJames Smart  * lpfc_nvme_rescan_port - Check to see if we should rescan this remoteport
24306f2589f4SJames Smart  *
24316f2589f4SJames Smart  * If the ndlp represents an NVME Target, that we are logged into,
24326f2589f4SJames Smart  * ping the NVME FC Transport layer to initiate a device rescan
24336f2589f4SJames Smart  * on this remote NPort.
24346f2589f4SJames Smart  */
24356f2589f4SJames Smart void
24366f2589f4SJames Smart lpfc_nvme_rescan_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
24376f2589f4SJames Smart {
24386f2589f4SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2439a6d10f24SJames Smart 	struct lpfc_nvme_rport *nrport;
2440a6d10f24SJames Smart 	struct nvme_fc_remote_port *remoteport = NULL;
24416f2589f4SJames Smart 
2442c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
2443a6d10f24SJames Smart 	nrport = lpfc_ndlp_get_nrport(ndlp);
2444a6d10f24SJames Smart 	if (nrport)
2445a6d10f24SJames Smart 		remoteport = nrport->remoteport;
2446c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
24476f2589f4SJames Smart 
24486f2589f4SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
24496f2589f4SJames Smart 			 "6170 Rescan NPort DID x%06x type x%x "
2450a6d10f24SJames Smart 			 "state x%x nrport x%px remoteport x%px\n",
2451a6d10f24SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type, ndlp->nlp_state,
2452a6d10f24SJames Smart 			 nrport, remoteport);
2453a6d10f24SJames Smart 
2454a6d10f24SJames Smart 	if (!nrport || !remoteport)
2455a6d10f24SJames Smart 		goto rescan_exit;
24566f2589f4SJames Smart 
24576f2589f4SJames Smart 	/* Only rescan if we are an NVME target in the MAPPED state */
24586f2589f4SJames Smart 	if (remoteport->port_role & FC_PORT_ROLE_NVME_DISCOVERY &&
24596f2589f4SJames Smart 	    ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
24606f2589f4SJames Smart 		nvme_fc_rescan_remoteport(remoteport);
24616f2589f4SJames Smart 
2462372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
24636f2589f4SJames Smart 				 "6172 NVME rescanned DID x%06x "
24646f2589f4SJames Smart 				 "port_state x%x\n",
24656f2589f4SJames Smart 				 ndlp->nlp_DID, remoteport->port_state);
24666f2589f4SJames Smart 	}
24676f2589f4SJames Smart 	return;
2468a6d10f24SJames Smart  rescan_exit:
2469a6d10f24SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2470a6d10f24SJames Smart 			 "6169 Skip NVME Rport Rescan, NVME remoteport "
2471a6d10f24SJames Smart 			 "unregistered\n");
24726f2589f4SJames Smart #endif
24736f2589f4SJames Smart }
24746f2589f4SJames Smart 
247501649561SJames Smart /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
247601649561SJames Smart  *
247701649561SJames Smart  * There is no notion of Devloss or rport recovery from the current
247801649561SJames Smart  * nvme_transport perspective.  Loss of an rport just means IO cannot
247901649561SJames Smart  * be sent and recovery is completely up to the initator.
248001649561SJames Smart  * For now, the driver just unbinds the DID and port_role so that
248101649561SJames Smart  * no further IO can be issued.  Changes are planned for later.
248201649561SJames Smart  *
248301649561SJames Smart  * Notes - the ndlp reference count is not decremented here since
248401649561SJames Smart  * since there is no nvme_transport api for devloss.  Node ref count
248501649561SJames Smart  * is only adjusted in driver unload.
248601649561SJames Smart  */
248701649561SJames Smart void
248801649561SJames Smart lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
248901649561SJames Smart {
24907d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
249101649561SJames Smart 	int ret;
249201649561SJames Smart 	struct nvme_fc_local_port *localport;
249301649561SJames Smart 	struct lpfc_nvme_lport *lport;
249401649561SJames Smart 	struct lpfc_nvme_rport *rport;
24959e210178SJames Smart 	struct nvme_fc_remote_port *remoteport = NULL;
249601649561SJames Smart 
249701649561SJames Smart 	localport = vport->localport;
249801649561SJames Smart 
249901649561SJames Smart 	/* This is fundamental error.  The localport is always
250001649561SJames Smart 	 * available until driver unload.  Just exit.
250101649561SJames Smart 	 */
250201649561SJames Smart 	if (!localport)
250301649561SJames Smart 		return;
250401649561SJames Smart 
250501649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
250601649561SJames Smart 	if (!lport)
250701649561SJames Smart 		goto input_err;
250801649561SJames Smart 
2509c6adba15SJames Smart 	spin_lock_irq(&ndlp->lock);
251001466024SJames Smart 	rport = lpfc_ndlp_get_nrport(ndlp);
25119e210178SJames Smart 	if (rport)
25129e210178SJames Smart 		remoteport = rport->remoteport;
2513c6adba15SJames Smart 	spin_unlock_irq(&ndlp->lock);
25149e210178SJames Smart 	if (!remoteport)
251501649561SJames Smart 		goto input_err;
251601649561SJames Smart 
251701649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
251832350664SJames Smart 			 "6033 Unreg nvme remoteport x%px, portname x%llx, "
2519e9b11083SJames Smart 			 "port_id x%06x, portstate x%x port type x%x "
2520e9b11083SJames Smart 			 "refcnt %d\n",
252101649561SJames Smart 			 remoteport, remoteport->port_name,
252201649561SJames Smart 			 remoteport->port_id, remoteport->port_state,
2523e9b11083SJames Smart 			 ndlp->nlp_type, kref_read(&ndlp->kref));
252401649561SJames Smart 
252501649561SJames Smart 	/* Sanity check ndlp type.  Only call for NVME ports. Don't
252601649561SJames Smart 	 * clear any rport state until the transport calls back.
252701649561SJames Smart 	 */
25283b5bde69SJames Smart 
25293b5bde69SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET) {
25307a06dcd3SJames Smart 		/* No concern about the role change on the nvme remoteport.
25317a06dcd3SJames Smart 		 * The transport will update it.
25327a06dcd3SJames Smart 		 */
2533e9b11083SJames Smart 		spin_lock_irq(&vport->phba->hbalock);
2534e9b11083SJames Smart 		ndlp->fc4_xpt_flags |= NLP_WAIT_FOR_UNREG;
2535e9b11083SJames Smart 		spin_unlock_irq(&vport->phba->hbalock);
25367438273fSJames Smart 
25377438273fSJames Smart 		/* Don't let the host nvme transport keep sending keep-alives
25387438273fSJames Smart 		 * on this remoteport. Vport is unloading, no recovery. The
25397438273fSJames Smart 		 * return values is ignored.  The upcall is a courtesy to the
25407438273fSJames Smart 		 * transport.
25417438273fSJames Smart 		 */
25427438273fSJames Smart 		if (vport->load_flag & FC_UNLOADING)
25437438273fSJames Smart 			(void)nvme_fc_set_remoteport_devloss(remoteport, 0);
25447438273fSJames Smart 
254501649561SJames Smart 		ret = nvme_fc_unregister_remoteport(remoteport);
2546e9b11083SJames Smart 
2547e9b11083SJames Smart 		/* The driver no longer knows if the nrport memory is valid.
2548e9b11083SJames Smart 		 * because the controller teardown process has begun and
2549e9b11083SJames Smart 		 * is asynchronous.  Break the binding in the ndlp. Also
2550e9b11083SJames Smart 		 * remove the register ndlp reference to setup node release.
2551e9b11083SJames Smart 		 */
2552e9b11083SJames Smart 		ndlp->nrport = NULL;
25533fd78355SJames Smart 		lpfc_nlp_put(ndlp);
2554e9b11083SJames Smart 		if (ret != 0) {
2555372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
255601649561SJames Smart 					 "6167 NVME unregister failed %d "
255701649561SJames Smart 					 "port_state x%x\n",
255801649561SJames Smart 					 ret, remoteport->port_state);
25593fd78355SJames Smart 		}
256001649561SJames Smart 	}
256101649561SJames Smart 	return;
256201649561SJames Smart 
256301649561SJames Smart  input_err:
2564166d7211SJames Smart #endif
2565372c187bSDick Kennedy 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
256632350664SJames Smart 			 "6168 State error: lport x%px, rport x%px FCID x%06x\n",
256701649561SJames Smart 			 vport->localport, ndlp->rport, ndlp->nlp_DID);
256801649561SJames Smart }
2569318083adSJames Smart 
2570318083adSJames Smart /**
2571318083adSJames Smart  * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2572318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
2573318083adSJames Smart  * @axri: pointer to the fcp xri abort wcqe structure.
2574c00f62e6SJames Smart  * @lpfc_ncmd: The nvme job structure for the request being aborted.
2575318083adSJames Smart  *
2576318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
2577952c303bSDick Kennedy  * NVME aborted xri.  Aborted NVME IO commands are completed to the transport
2578952c303bSDick Kennedy  * here.
2579318083adSJames Smart  **/
2580318083adSJames Smart void
2581318083adSJames Smart lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2582c00f62e6SJames Smart 			   struct sli4_wcqe_xri_aborted *axri,
2583c00f62e6SJames Smart 			   struct lpfc_io_buf *lpfc_ncmd)
2584318083adSJames Smart {
2585318083adSJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
2586952c303bSDick Kennedy 	struct nvmefc_fcp_req *nvme_cmd = NULL;
2587c00f62e6SJames Smart 	struct lpfc_nodelist *ndlp = lpfc_ncmd->ndlp;
2588318083adSJames Smart 
2589318083adSJames Smart 
2590952c303bSDick Kennedy 	if (ndlp)
2591318083adSJames Smart 		lpfc_sli4_abts_err_handler(phba, ndlp, axri);
259286c67379SJames Smart 
259386c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2594c00f62e6SJames Smart 			"6311 nvme_cmd %p xri x%x tag x%x abort complete and "
2595c00f62e6SJames Smart 			"xri released\n",
2596952c303bSDick Kennedy 			lpfc_ncmd->nvmeCmd, xri,
2597952c303bSDick Kennedy 			lpfc_ncmd->cur_iocbq.iotag);
259886c67379SJames Smart 
2599952c303bSDick Kennedy 	/* Aborted NVME commands are required to not complete
2600952c303bSDick Kennedy 	 * before the abort exchange command fully completes.
2601952c303bSDick Kennedy 	 * Once completed, it is available via the put list.
2602952c303bSDick Kennedy 	 */
26033fd78355SJames Smart 	if (lpfc_ncmd->nvmeCmd) {
2604952c303bSDick Kennedy 		nvme_cmd = lpfc_ncmd->nvmeCmd;
2605952c303bSDick Kennedy 		nvme_cmd->done(nvme_cmd);
26063fd78355SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
26073fd78355SJames Smart 	}
2608318083adSJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
2609318083adSJames Smart }
2610c3725bdcSJames Smart 
2611c3725bdcSJames Smart /**
2612c3725bdcSJames Smart  * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2613c3725bdcSJames Smart  * @phba: Pointer to HBA context object.
2614c3725bdcSJames Smart  *
2615c3725bdcSJames Smart  * This function flushes all wqes in the nvme rings and frees all resources
2616c3725bdcSJames Smart  * in the txcmplq. This function does not issue abort wqes for the IO
2617c3725bdcSJames Smart  * commands in txcmplq, they will just be returned with
2618c3725bdcSJames Smart  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2619c3725bdcSJames Smart  * slot has been permanently disabled.
2620c3725bdcSJames Smart  **/
2621c3725bdcSJames Smart void
2622c3725bdcSJames Smart lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2623c3725bdcSJames Smart {
2624c3725bdcSJames Smart 	struct lpfc_sli_ring  *pring;
2625c3725bdcSJames Smart 	u32 i, wait_cnt = 0;
2626c3725bdcSJames Smart 
2627cdb42becSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4 || !phba->sli4_hba.hdwq)
2628c3725bdcSJames Smart 		return;
2629c3725bdcSJames Smart 
2630c00f62e6SJames Smart 	/* Cycle through all IO rings and make sure all outstanding
2631c3725bdcSJames Smart 	 * WQEs have been removed from the txcmplqs.
2632c3725bdcSJames Smart 	 */
2633cdb42becSJames Smart 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
2634c00f62e6SJames Smart 		if (!phba->sli4_hba.hdwq[i].io_wq)
26356a828b0fSJames Smart 			continue;
2636c00f62e6SJames Smart 		pring = phba->sli4_hba.hdwq[i].io_wq->pring;
2637c3725bdcSJames Smart 
2638d580c613SJames Smart 		if (!pring)
2639d580c613SJames Smart 			continue;
2640d580c613SJames Smart 
2641c3725bdcSJames Smart 		/* Retrieve everything on the txcmplq */
2642c3725bdcSJames Smart 		while (!list_empty(&pring->txcmplq)) {
2643c3725bdcSJames Smart 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
2644c3725bdcSJames Smart 			wait_cnt++;
2645c3725bdcSJames Smart 
2646c3725bdcSJames Smart 			/* The sleep is 10mS.  Every ten seconds,
2647c3725bdcSJames Smart 			 * dump a message.  Something is wrong.
2648c3725bdcSJames Smart 			 */
2649c3725bdcSJames Smart 			if ((wait_cnt % 1000) == 0) {
2650372c187bSDick Kennedy 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2651c3725bdcSJames Smart 						"6178 NVME IO not empty, "
2652c3725bdcSJames Smart 						"cnt %d\n", wait_cnt);
2653c3725bdcSJames Smart 			}
2654c3725bdcSJames Smart 		}
2655c3725bdcSJames Smart 	}
2656c3725bdcSJames Smart }
265784f2ddf8SJames Smart 
265884f2ddf8SJames Smart void
265984f2ddf8SJames Smart lpfc_nvme_cancel_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn)
266084f2ddf8SJames Smart {
266184f2ddf8SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
266284f2ddf8SJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
266384f2ddf8SJames Smart 	struct nvmefc_fcp_req *nCmd;
266484f2ddf8SJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
266584f2ddf8SJames Smart 
266684f2ddf8SJames Smart 	if (!pwqeIn->context1) {
266784f2ddf8SJames Smart 		lpfc_sli_release_iocbq(phba, pwqeIn);
266884f2ddf8SJames Smart 		return;
266984f2ddf8SJames Smart 	}
267084f2ddf8SJames Smart 	/* For abort iocb just return, IO iocb will do a done call */
267184f2ddf8SJames Smart 	if (bf_get(wqe_cmnd, &pwqeIn->wqe.gen_req.wqe_com) ==
267284f2ddf8SJames Smart 	    CMD_ABORT_XRI_CX) {
267384f2ddf8SJames Smart 		lpfc_sli_release_iocbq(phba, pwqeIn);
267484f2ddf8SJames Smart 		return;
267584f2ddf8SJames Smart 	}
267684f2ddf8SJames Smart 	lpfc_ncmd = (struct lpfc_io_buf *)pwqeIn->context1;
267784f2ddf8SJames Smart 
267884f2ddf8SJames Smart 	spin_lock(&lpfc_ncmd->buf_lock);
267984f2ddf8SJames Smart 	if (!lpfc_ncmd->nvmeCmd) {
268084f2ddf8SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
268184f2ddf8SJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
268284f2ddf8SJames Smart 		return;
268384f2ddf8SJames Smart 	}
268484f2ddf8SJames Smart 
268584f2ddf8SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
268684f2ddf8SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
268784f2ddf8SJames Smart 			"6194 NVME Cancel xri %x\n",
268884f2ddf8SJames Smart 			lpfc_ncmd->cur_iocbq.sli4_xritag);
268984f2ddf8SJames Smart 
269084f2ddf8SJames Smart 	nCmd->transferred_length = 0;
269184f2ddf8SJames Smart 	nCmd->rcv_rsplen = 0;
269284f2ddf8SJames Smart 	nCmd->status = NVME_SC_INTERNAL;
269384f2ddf8SJames Smart 	freqpriv = nCmd->private;
269484f2ddf8SJames Smart 	freqpriv->nvme_buf = NULL;
269584f2ddf8SJames Smart 	lpfc_ncmd->nvmeCmd = NULL;
269684f2ddf8SJames Smart 
269784f2ddf8SJames Smart 	spin_unlock(&lpfc_ncmd->buf_lock);
269884f2ddf8SJames Smart 	nCmd->done(nCmd);
269984f2ddf8SJames Smart 
270084f2ddf8SJames Smart 	/* Call release with XB=1 to queue the IO into the abort list. */
270184f2ddf8SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
270284f2ddf8SJames Smart #endif
270384f2ddf8SJames Smart }
2704