xref: /linux/drivers/scsi/lpfc/lpfc_nvme.c (revision 84f2ddf8cf317afcfd92e06ed5a25aea48dc200e)
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 <linux/nvme.h>
4001649561SJames Smart #include <linux/nvme-fc-driver.h>
4101649561SJames Smart #include <linux/nvme-fc.h>
4201649561SJames Smart #include "lpfc_version.h"
4301649561SJames Smart #include "lpfc_hw4.h"
4401649561SJames Smart #include "lpfc_hw.h"
4501649561SJames Smart #include "lpfc_sli.h"
4601649561SJames Smart #include "lpfc_sli4.h"
4701649561SJames Smart #include "lpfc_nl.h"
4801649561SJames Smart #include "lpfc_disc.h"
4901649561SJames Smart #include "lpfc.h"
5001649561SJames Smart #include "lpfc_nvme.h"
5101649561SJames Smart #include "lpfc_scsi.h"
5201649561SJames Smart #include "lpfc_logmsg.h"
5301649561SJames Smart #include "lpfc_crtn.h"
5401649561SJames Smart #include "lpfc_vport.h"
55bd2cdd5eSJames Smart #include "lpfc_debugfs.h"
5601649561SJames Smart 
5701649561SJames Smart /* NVME initiator-based functions */
5801649561SJames Smart 
59c490850aSJames Smart static struct lpfc_io_buf *
60cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
615e5b511dSJames Smart 		  int idx, int expedite);
6201649561SJames Smart 
6301649561SJames Smart static void
64c490850aSJames Smart lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_io_buf *);
6501649561SJames Smart 
6681e6a637SJames Smart static struct nvme_fc_port_template lpfc_nvme_template;
6701649561SJames Smart 
68fab2e466SColin Ian King static union lpfc_wqe128 lpfc_iread_cmd_template;
69fab2e466SColin Ian King static union lpfc_wqe128 lpfc_iwrite_cmd_template;
70fab2e466SColin Ian King static union lpfc_wqe128 lpfc_icmnd_cmd_template;
715fd11085SJames Smart 
725fd11085SJames Smart /* Setup WQE templates for NVME IOs */
735fd11085SJames Smart void
74fab2e466SColin Ian King lpfc_nvme_cmd_template(void)
755fd11085SJames Smart {
765fd11085SJames Smart 	union lpfc_wqe128 *wqe;
775fd11085SJames Smart 
785fd11085SJames Smart 	/* IREAD template */
795fd11085SJames Smart 	wqe = &lpfc_iread_cmd_template;
805fd11085SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
815fd11085SJames Smart 
825fd11085SJames Smart 	/* Word 0, 1, 2 - BDE is variable */
835fd11085SJames Smart 
845fd11085SJames Smart 	/* Word 3 - cmd_buff_len, payload_offset_len is zero */
855fd11085SJames Smart 
865fd11085SJames Smart 	/* Word 4 - total_xfer_len is variable */
875fd11085SJames Smart 
885fd11085SJames Smart 	/* Word 5 - is zero */
895fd11085SJames Smart 
905fd11085SJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
915fd11085SJames Smart 
925fd11085SJames Smart 	/* Word 7 */
935fd11085SJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_iread.wqe_com, CMD_FCP_IREAD64_WQE);
945fd11085SJames Smart 	bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, PARM_READ_CHECK);
955fd11085SJames Smart 	bf_set(wqe_class, &wqe->fcp_iread.wqe_com, CLASS3);
965fd11085SJames Smart 	bf_set(wqe_ct, &wqe->fcp_iread.wqe_com, SLI4_CT_RPI);
975fd11085SJames Smart 
985fd11085SJames Smart 	/* Word 8 - abort_tag is variable */
995fd11085SJames Smart 
1005fd11085SJames Smart 	/* Word 9  - reqtag is variable */
1015fd11085SJames Smart 
1025fd11085SJames Smart 	/* Word 10 - dbde, wqes is variable */
1035fd11085SJames Smart 	bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
1045fd11085SJames Smart 	bf_set(wqe_nvme, &wqe->fcp_iread.wqe_com, 1);
1055fd11085SJames Smart 	bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
1065fd11085SJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com, LPFC_WQE_LENLOC_WORD4);
1075fd11085SJames Smart 	bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
1085fd11085SJames Smart 	bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
1095fd11085SJames Smart 
1105fd11085SJames Smart 	/* Word 11 - pbde is variable */
1115fd11085SJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_iread.wqe_com, NVME_READ_CMD);
1125fd11085SJames Smart 	bf_set(wqe_cqid, &wqe->fcp_iread.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1135fd11085SJames Smart 	bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
1145fd11085SJames Smart 
1155fd11085SJames Smart 	/* Word 12 - is zero */
1165fd11085SJames Smart 
1175fd11085SJames Smart 	/* Word 13, 14, 15 - PBDE is variable */
1185fd11085SJames Smart 
1195fd11085SJames Smart 	/* IWRITE template */
1205fd11085SJames Smart 	wqe = &lpfc_iwrite_cmd_template;
1215fd11085SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
1225fd11085SJames Smart 
1235fd11085SJames Smart 	/* Word 0, 1, 2 - BDE is variable */
1245fd11085SJames Smart 
1255fd11085SJames Smart 	/* Word 3 - cmd_buff_len, payload_offset_len is zero */
1265fd11085SJames Smart 
1275fd11085SJames Smart 	/* Word 4 - total_xfer_len is variable */
1285fd11085SJames Smart 
1295fd11085SJames Smart 	/* Word 5 - initial_xfer_len is variable */
1305fd11085SJames Smart 
1315fd11085SJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
1325fd11085SJames Smart 
1335fd11085SJames Smart 	/* Word 7 */
1345fd11085SJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_iwrite.wqe_com, CMD_FCP_IWRITE64_WQE);
1355fd11085SJames Smart 	bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, PARM_READ_CHECK);
1365fd11085SJames Smart 	bf_set(wqe_class, &wqe->fcp_iwrite.wqe_com, CLASS3);
1375fd11085SJames Smart 	bf_set(wqe_ct, &wqe->fcp_iwrite.wqe_com, SLI4_CT_RPI);
1385fd11085SJames Smart 
1395fd11085SJames Smart 	/* Word 8 - abort_tag is variable */
1405fd11085SJames Smart 
1415fd11085SJames Smart 	/* Word 9  - reqtag is variable */
1425fd11085SJames Smart 
1435fd11085SJames Smart 	/* Word 10 - dbde, wqes is variable */
1445fd11085SJames Smart 	bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
1455fd11085SJames Smart 	bf_set(wqe_nvme, &wqe->fcp_iwrite.wqe_com, 1);
1465fd11085SJames Smart 	bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
1475fd11085SJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_LENLOC_WORD4);
1485fd11085SJames Smart 	bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
1495fd11085SJames Smart 	bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
1505fd11085SJames Smart 
1515fd11085SJames Smart 	/* Word 11 - pbde is variable */
1525fd11085SJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_iwrite.wqe_com, NVME_WRITE_CMD);
1535fd11085SJames Smart 	bf_set(wqe_cqid, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1545fd11085SJames Smart 	bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
1555fd11085SJames Smart 
1565fd11085SJames Smart 	/* Word 12 - is zero */
1575fd11085SJames Smart 
1585fd11085SJames Smart 	/* Word 13, 14, 15 - PBDE is variable */
1595fd11085SJames Smart 
1605fd11085SJames Smart 	/* ICMND template */
1615fd11085SJames Smart 	wqe = &lpfc_icmnd_cmd_template;
1625fd11085SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
1635fd11085SJames Smart 
1645fd11085SJames Smart 	/* Word 0, 1, 2 - BDE is variable */
1655fd11085SJames Smart 
1665fd11085SJames Smart 	/* Word 3 - payload_offset_len is variable */
1675fd11085SJames Smart 
1685fd11085SJames Smart 	/* Word 4, 5 - is zero */
1695fd11085SJames Smart 
1705fd11085SJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
1715fd11085SJames Smart 
1725fd11085SJames Smart 	/* Word 7 */
1735fd11085SJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_icmd.wqe_com, CMD_FCP_ICMND64_WQE);
1745fd11085SJames Smart 	bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
1755fd11085SJames Smart 	bf_set(wqe_class, &wqe->fcp_icmd.wqe_com, CLASS3);
1765fd11085SJames Smart 	bf_set(wqe_ct, &wqe->fcp_icmd.wqe_com, SLI4_CT_RPI);
1775fd11085SJames Smart 
1785fd11085SJames Smart 	/* Word 8 - abort_tag is variable */
1795fd11085SJames Smart 
1805fd11085SJames Smart 	/* Word 9  - reqtag is variable */
1815fd11085SJames Smart 
1825fd11085SJames Smart 	/* Word 10 - dbde, wqes is variable */
1835fd11085SJames Smart 	bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
1845fd11085SJames Smart 	bf_set(wqe_nvme, &wqe->fcp_icmd.wqe_com, 1);
1855fd11085SJames Smart 	bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_NONE);
1865fd11085SJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com, LPFC_WQE_LENLOC_NONE);
1875fd11085SJames Smart 	bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
1885fd11085SJames Smart 	bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
1895fd11085SJames Smart 
1905fd11085SJames Smart 	/* Word 11 */
1915fd11085SJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_icmd.wqe_com, FCP_COMMAND);
1925fd11085SJames Smart 	bf_set(wqe_cqid, &wqe->fcp_icmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1935fd11085SJames Smart 	bf_set(wqe_pbde, &wqe->fcp_icmd.wqe_com, 0);
1945fd11085SJames Smart 
1955fd11085SJames Smart 	/* Word 12, 13, 14, 15 - is zero */
1965fd11085SJames Smart }
1975fd11085SJames Smart 
19801649561SJames Smart /**
19901649561SJames Smart  * lpfc_nvme_create_queue -
20001649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
20101649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
20201649561SJames Smart  * @handle: An opaque driver handle used in follow-up calls.
20301649561SJames Smart  *
20401649561SJames Smart  * Driver registers this routine to preallocate and initialize any
20501649561SJames Smart  * internal data structures to bind the @qidx to its internal IO queues.
20601649561SJames Smart  * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
20701649561SJames Smart  *
20801649561SJames Smart  * Return value :
20901649561SJames Smart  *   0 - Success
21001649561SJames Smart  *   -EINVAL - Unsupported input value.
21101649561SJames Smart  *   -ENOMEM - Could not alloc necessary memory
21201649561SJames Smart  **/
21301649561SJames Smart static int
21401649561SJames Smart lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
21501649561SJames Smart 		       unsigned int qidx, u16 qsize,
21601649561SJames Smart 		       void **handle)
21701649561SJames Smart {
21801649561SJames Smart 	struct lpfc_nvme_lport *lport;
21901649561SJames Smart 	struct lpfc_vport *vport;
22001649561SJames Smart 	struct lpfc_nvme_qhandle *qhandle;
22101649561SJames Smart 	char *str;
22201649561SJames Smart 
223c3725bdcSJames Smart 	if (!pnvme_lport->private)
224c3725bdcSJames Smart 		return -ENOMEM;
225c3725bdcSJames Smart 
22601649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
22701649561SJames Smart 	vport = lport->vport;
22801649561SJames Smart 	qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
22901649561SJames Smart 	if (qhandle == NULL)
23001649561SJames Smart 		return -ENOMEM;
23101649561SJames Smart 
232d6d189ceSBart Van Assche 	qhandle->cpu_id = raw_smp_processor_id();
23301649561SJames Smart 	qhandle->qidx = qidx;
23401649561SJames Smart 	/*
23501649561SJames Smart 	 * NVME qidx == 0 is the admin queue, so both admin queue
23601649561SJames Smart 	 * and first IO queue will use MSI-X vector and associated
23701649561SJames Smart 	 * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
23801649561SJames Smart 	 */
23901649561SJames Smart 	if (qidx) {
24001649561SJames Smart 		str = "IO ";  /* IO queue */
24101649561SJames Smart 		qhandle->index = ((qidx - 1) %
2426a828b0fSJames Smart 			lpfc_nvme_template.max_hw_queues);
24301649561SJames Smart 	} else {
24401649561SJames Smart 		str = "ADM";  /* Admin queue */
24501649561SJames Smart 		qhandle->index = qidx;
24601649561SJames Smart 	}
24701649561SJames Smart 
248d58734f0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
24901649561SJames Smart 			 "6073 Binding %s HdwQueue %d  (cpu %d) to "
250cdb42becSJames Smart 			 "hdw_queue %d qhandle %p\n", str,
25101649561SJames Smart 			 qidx, qhandle->cpu_id, qhandle->index, qhandle);
25201649561SJames Smart 	*handle = (void *)qhandle;
25301649561SJames Smart 	return 0;
25401649561SJames Smart }
25501649561SJames Smart 
25601649561SJames Smart /**
25701649561SJames Smart  * lpfc_nvme_delete_queue -
25801649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
25901649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
26001649561SJames Smart  * @handle: An opaque driver handle from lpfc_nvme_create_queue
26101649561SJames Smart  *
26201649561SJames Smart  * Driver registers this routine to free
26301649561SJames Smart  * any internal data structures to bind the @qidx to its internal
26401649561SJames Smart  * IO queues.
26501649561SJames Smart  *
26601649561SJames Smart  * Return value :
26701649561SJames Smart  *   0 - Success
26801649561SJames Smart  *   TODO:  What are the failure codes.
26901649561SJames Smart  **/
27001649561SJames Smart static void
27101649561SJames Smart lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
27201649561SJames Smart 		       unsigned int qidx,
27301649561SJames Smart 		       void *handle)
27401649561SJames Smart {
27501649561SJames Smart 	struct lpfc_nvme_lport *lport;
27601649561SJames Smart 	struct lpfc_vport *vport;
27701649561SJames Smart 
278c3725bdcSJames Smart 	if (!pnvme_lport->private)
279c3725bdcSJames Smart 		return;
280c3725bdcSJames Smart 
28101649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
28201649561SJames Smart 	vport = lport->vport;
28301649561SJames Smart 
28401649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2852879265fSJames Smart 			"6001 ENTER.  lpfc_pnvme %p, qidx x%x qhandle %p\n",
28601649561SJames Smart 			lport, qidx, handle);
28701649561SJames Smart 	kfree(handle);
28801649561SJames Smart }
28901649561SJames Smart 
29001649561SJames Smart static void
29101649561SJames Smart lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
29201649561SJames Smart {
29301649561SJames Smart 	struct lpfc_nvme_lport *lport = localport->private;
29401649561SJames Smart 
295add9d6beSJames Smart 	lpfc_printf_vlog(lport->vport, KERN_INFO, LOG_NVME,
296add9d6beSJames Smart 			 "6173 localport %p delete complete\n",
297add9d6beSJames Smart 			 lport);
298add9d6beSJames Smart 
29901649561SJames Smart 	/* release any threads waiting for the unreg to complete */
3007961cba6SEwan D. Milne 	if (lport->vport->localport)
3017961cba6SEwan D. Milne 		complete(lport->lport_unreg_cmp);
30201649561SJames Smart }
30301649561SJames Smart 
30401649561SJames Smart /* lpfc_nvme_remoteport_delete
30501649561SJames Smart  *
30601649561SJames Smart  * @remoteport: Pointer to an nvme transport remoteport instance.
30701649561SJames Smart  *
30801649561SJames Smart  * This is a template downcall.  NVME transport calls this function
30901649561SJames Smart  * when it has completed the unregistration of a previously
31001649561SJames Smart  * registered remoteport.
31101649561SJames Smart  *
31201649561SJames Smart  * Return value :
31301649561SJames Smart  * None
31401649561SJames Smart  */
3153999df75SBart Van Assche static void
31601649561SJames Smart lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
31701649561SJames Smart {
31801649561SJames Smart 	struct lpfc_nvme_rport *rport = remoteport->private;
31901649561SJames Smart 	struct lpfc_vport *vport;
32001649561SJames Smart 	struct lpfc_nodelist *ndlp;
32101649561SJames Smart 
32201649561SJames Smart 	ndlp = rport->ndlp;
32301649561SJames Smart 	if (!ndlp)
32401649561SJames Smart 		goto rport_err;
32501649561SJames Smart 
32601649561SJames Smart 	vport = ndlp->vport;
32701649561SJames Smart 	if (!vport)
32801649561SJames Smart 		goto rport_err;
32901649561SJames Smart 
33001649561SJames Smart 	/* Remove this rport from the lport's list - memory is owned by the
33101649561SJames Smart 	 * transport. Remove the ndlp reference for the NVME transport before
3327a06dcd3SJames Smart 	 * calling state machine to remove the node.
33301649561SJames Smart 	 */
33401649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
3353fd78355SJames Smart 			"6146 remoteport delete of remoteport %p\n",
33601649561SJames Smart 			remoteport);
3373fd78355SJames Smart 	spin_lock_irq(&vport->phba->hbalock);
338b15bd3e6SJames Smart 
339b15bd3e6SJames Smart 	/* The register rebind might have occurred before the delete
340b15bd3e6SJames Smart 	 * downcall.  Guard against this race.
341b15bd3e6SJames Smart 	 */
342b15bd3e6SJames Smart 	if (ndlp->upcall_flags & NLP_WAIT_FOR_UNREG) {
3437a06dcd3SJames Smart 		ndlp->nrport = NULL;
34401466024SJames Smart 		ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
345b15bd3e6SJames Smart 	}
3463fd78355SJames Smart 	spin_unlock_irq(&vport->phba->hbalock);
3473fd78355SJames Smart 
3483fd78355SJames Smart 	/* Remove original register reference. The host transport
3493fd78355SJames Smart 	 * won't reference this rport/remoteport any further.
3503fd78355SJames Smart 	 */
35101649561SJames Smart 	lpfc_nlp_put(ndlp);
35201649561SJames Smart 
35301649561SJames Smart  rport_err:
3543fd78355SJames Smart 	return;
35501649561SJames Smart }
35601649561SJames Smart 
35701649561SJames Smart static void
35801649561SJames Smart lpfc_nvme_cmpl_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
35901649561SJames Smart 		       struct lpfc_wcqe_complete *wcqe)
36001649561SJames Smart {
36101649561SJames Smart 	struct lpfc_vport *vport = cmdwqe->vport;
3624b056682SJames Smart 	struct lpfc_nvme_lport *lport;
36301649561SJames Smart 	uint32_t status;
36401649561SJames Smart 	struct nvmefc_ls_req *pnvme_lsreq;
36501649561SJames Smart 	struct lpfc_dmabuf *buf_ptr;
36601649561SJames Smart 	struct lpfc_nodelist *ndlp;
36701649561SJames Smart 
36801649561SJames Smart 	pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
36901649561SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
37066a210ffSJames Smart 
37166a85155SJames Smart 	if (vport->localport) {
37266a85155SJames Smart 		lport = (struct lpfc_nvme_lport *)vport->localport->private;
37366a210ffSJames Smart 		if (lport) {
37466a210ffSJames Smart 			atomic_inc(&lport->fc4NvmeLsCmpls);
3754b056682SJames Smart 			if (status) {
3764b056682SJames Smart 				if (bf_get(lpfc_wcqe_c_xb, wcqe))
3774b056682SJames Smart 					atomic_inc(&lport->cmpl_ls_xb);
3784b056682SJames Smart 				atomic_inc(&lport->cmpl_ls_err);
3794b056682SJames Smart 			}
38066a210ffSJames Smart 		}
38166a85155SJames Smart 	}
3824b056682SJames Smart 
38301649561SJames Smart 	ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
38401649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
38501649561SJames Smart 			 "6047 nvme cmpl Enter "
386815a9c43SJames Smart 			 "Data %p DID %x Xri: %x status %x reason x%x cmd:%p "
387815a9c43SJames Smart 			 "lsreg:%p bmp:%p ndlp:%p\n",
38801649561SJames Smart 			 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
38901649561SJames Smart 			 cmdwqe->sli4_xritag, status,
390815a9c43SJames Smart 			 (wcqe->parameter & 0xffff),
39101649561SJames Smart 			 cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
39201649561SJames Smart 
393bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  CMPL: xri x%x stat x%x parm x%x\n",
394bd2cdd5eSJames Smart 			 cmdwqe->sli4_xritag, status, wcqe->parameter);
395bd2cdd5eSJames Smart 
39601649561SJames Smart 	if (cmdwqe->context3) {
39701649561SJames Smart 		buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
39801649561SJames Smart 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
39901649561SJames Smart 		kfree(buf_ptr);
40001649561SJames Smart 		cmdwqe->context3 = NULL;
40101649561SJames Smart 	}
40201649561SJames Smart 	if (pnvme_lsreq->done)
40301649561SJames Smart 		pnvme_lsreq->done(pnvme_lsreq, status);
40401649561SJames Smart 	else
40501649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
40601649561SJames Smart 				 "6046 nvme cmpl without done call back? "
40701649561SJames Smart 				 "Data %p DID %x Xri: %x status %x\n",
40801649561SJames Smart 				pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
40901649561SJames Smart 				cmdwqe->sli4_xritag, status);
41001649561SJames Smart 	if (ndlp) {
41101649561SJames Smart 		lpfc_nlp_put(ndlp);
41201649561SJames Smart 		cmdwqe->context1 = NULL;
41301649561SJames Smart 	}
41401649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
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;
43901649561SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
44001649561SJames Smart 
44101649561SJames Smart 	genwqe->context3 = (uint8_t *)bmp;
44201649561SJames Smart 	genwqe->iocb_flag |= LPFC_IO_NVME_LS;
44301649561SJames Smart 
44401649561SJames Smart 	/* Save for completion so we can release these resources */
44501649561SJames Smart 	genwqe->context1 = lpfc_nlp_get(ndlp);
44601649561SJames Smart 	genwqe->context2 = (uint8_t *)pnvme_lsreq;
44701649561SJames Smart 	/* Fill in payload, bp points to frame payload */
44801649561SJames Smart 
44901649561SJames Smart 	if (!tmo)
45001649561SJames Smart 		/* FC spec states we need 3 * ratov for CT requests */
45101649561SJames Smart 		tmo = (3 * phba->fc_ratov);
45201649561SJames Smart 
45301649561SJames Smart 	/* For this command calculate the xmit length of the request bde. */
45401649561SJames Smart 	xmit_len = 0;
45501649561SJames Smart 	first_len = 0;
45601649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
45701649561SJames Smart 	for (i = 0; i < num_entry; i++) {
45801649561SJames Smart 		bde.tus.w = bpl[i].tus.w;
45901649561SJames Smart 		if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
46001649561SJames Smart 			break;
46101649561SJames Smart 		xmit_len += bde.tus.f.bdeSize;
46201649561SJames Smart 		if (i == 0)
46301649561SJames Smart 			first_len = xmit_len;
46401649561SJames Smart 	}
46501649561SJames Smart 
46601649561SJames Smart 	genwqe->rsvd2 = num_entry;
46701649561SJames Smart 	genwqe->hba_wqidx = 0;
46801649561SJames Smart 
46901649561SJames Smart 	/* Words 0 - 2 */
47001649561SJames Smart 	wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
47101649561SJames Smart 	wqe->generic.bde.tus.f.bdeSize = first_len;
47201649561SJames Smart 	wqe->generic.bde.addrLow = bpl[0].addrLow;
47301649561SJames Smart 	wqe->generic.bde.addrHigh = bpl[0].addrHigh;
47401649561SJames Smart 
47501649561SJames Smart 	/* Word 3 */
47601649561SJames Smart 	wqe->gen_req.request_payload_len = first_len;
47701649561SJames Smart 
47801649561SJames Smart 	/* Word 4 */
47901649561SJames Smart 
48001649561SJames Smart 	/* Word 5 */
48101649561SJames Smart 	bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
48201649561SJames Smart 	bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
48301649561SJames Smart 	bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
4848b361639SJames Smart 	bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
48501649561SJames Smart 	bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
48601649561SJames Smart 
48701649561SJames Smart 	/* Word 6 */
48801649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
48901649561SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
49001649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
49101649561SJames Smart 
49201649561SJames Smart 	/* Word 7 */
49301649561SJames Smart 	bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
49401649561SJames Smart 	bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
49501649561SJames Smart 	bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
49601649561SJames Smart 	bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
49701649561SJames Smart 
49801649561SJames Smart 	/* Word 8 */
49901649561SJames Smart 	wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
50001649561SJames Smart 
50101649561SJames Smart 	/* Word 9 */
50201649561SJames Smart 	bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
50301649561SJames Smart 
50401649561SJames Smart 	/* Word 10 */
50501649561SJames Smart 	bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
50601649561SJames Smart 	bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
50701649561SJames Smart 	bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
50801649561SJames Smart 	bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
50901649561SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
51001649561SJames Smart 
51101649561SJames Smart 	/* Word 11 */
51201649561SJames Smart 	bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
51301649561SJames Smart 	bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
51401649561SJames Smart 
51501649561SJames Smart 
51601649561SJames Smart 	/* Issue GEN REQ WQE for NPORT <did> */
51701649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
51801649561SJames Smart 			 "6050 Issue GEN REQ WQE to NPORT x%x "
51901649561SJames Smart 			 "Data: x%x x%x wq:%p lsreq:%p bmp:%p xmit:%d 1st:%d\n",
52001649561SJames Smart 			 ndlp->nlp_DID, genwqe->iotag,
52101649561SJames Smart 			 vport->port_state,
52201649561SJames Smart 			genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
52301649561SJames Smart 	genwqe->wqe_cmpl = cmpl;
52401649561SJames Smart 	genwqe->iocb_cmpl = NULL;
52501649561SJames Smart 	genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
52601649561SJames Smart 	genwqe->vport = vport;
52701649561SJames Smart 	genwqe->retry = retry;
52801649561SJames Smart 
529bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  XMIT: xri x%x iotag x%x to x%06x\n",
530bd2cdd5eSJames Smart 			 genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
531bd2cdd5eSJames Smart 
5321fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], genwqe);
533cd22d605SDick Kennedy 	if (rc) {
53401649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
53501649561SJames Smart 				 "6045 Issue GEN REQ WQE to NPORT x%x "
53601649561SJames Smart 				 "Data: x%x x%x\n",
53701649561SJames Smart 				 ndlp->nlp_DID, genwqe->iotag,
53801649561SJames Smart 				 vport->port_state);
53901649561SJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
54001649561SJames Smart 		return 1;
54101649561SJames Smart 	}
54201649561SJames Smart 	return 0;
54301649561SJames Smart }
54401649561SJames Smart 
54501649561SJames Smart /**
54601649561SJames Smart  * lpfc_nvme_ls_req - Issue an Link Service request
54701649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
54801649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
54901649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
55001649561SJames Smart  *
55101649561SJames Smart  * Driver registers this routine to handle any link service request
55201649561SJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
55301649561SJames Smart  *
55401649561SJames Smart  * Return value :
55501649561SJames Smart  *   0 - Success
55601649561SJames Smart  *   TODO: What are the failure codes.
55701649561SJames Smart  **/
55801649561SJames Smart static int
55901649561SJames Smart lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
56001649561SJames Smart 		 struct nvme_fc_remote_port *pnvme_rport,
56101649561SJames Smart 		 struct nvmefc_ls_req *pnvme_lsreq)
56201649561SJames Smart {
56301649561SJames Smart 	int ret = 0;
56401649561SJames Smart 	struct lpfc_nvme_lport *lport;
565815a9c43SJames Smart 	struct lpfc_nvme_rport *rport;
56601649561SJames Smart 	struct lpfc_vport *vport;
56701649561SJames Smart 	struct lpfc_nodelist *ndlp;
56801649561SJames Smart 	struct ulp_bde64 *bpl;
56901649561SJames Smart 	struct lpfc_dmabuf *bmp;
570ba43c4d0SJames Smart 	uint16_t ntype, nstate;
57101649561SJames Smart 
57201649561SJames Smart 	/* there are two dma buf in the request, actually there is one and
57301649561SJames Smart 	 * the second one is just the start address + cmd size.
57401649561SJames Smart 	 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
57501649561SJames Smart 	 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
57601649561SJames Smart 	 * because the nvem layer owns the data bufs.
57701649561SJames Smart 	 * We do not have to break these packets open, we don't care what is in
57801649561SJames Smart 	 * them. And we do not have to look at the resonse data, we only care
57901649561SJames Smart 	 * that we got a response. All of the caring is going to happen in the
58001649561SJames Smart 	 * nvme-fc layer.
58101649561SJames Smart 	 */
58201649561SJames Smart 
58301649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
584815a9c43SJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
58566a210ffSJames Smart 	if (unlikely(!lport) || unlikely(!rport))
58666a210ffSJames Smart 		return -EINVAL;
58766a210ffSJames Smart 
58801649561SJames Smart 	vport = lport->vport;
58901649561SJames Smart 
5901abcb371SDick Kennedy 	if (vport->load_flag & FC_UNLOADING)
5911abcb371SDick Kennedy 		return -ENODEV;
5921abcb371SDick Kennedy 
593815a9c43SJames Smart 	/* Need the ndlp.  It is stored in the driver's rport. */
594815a9c43SJames Smart 	ndlp = rport->ndlp;
595ba43c4d0SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
596ba43c4d0SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
597815a9c43SJames Smart 				 "6051 Remoteport %p, rport has invalid ndlp. "
598815a9c43SJames Smart 				 "Failing LS Req\n", pnvme_rport);
599ba43c4d0SJames Smart 		return -ENODEV;
600ba43c4d0SJames Smart 	}
601ba43c4d0SJames Smart 
602ba43c4d0SJames Smart 	/* The remote node has to be a mapped nvme target or an
603ba43c4d0SJames Smart 	 * unmapped nvme initiator or it's an error.
604ba43c4d0SJames Smart 	 */
605ba43c4d0SJames Smart 	ntype = ndlp->nlp_type;
606ba43c4d0SJames Smart 	nstate = ndlp->nlp_state;
607ba43c4d0SJames Smart 	if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
608ba43c4d0SJames Smart 	    (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
609ba43c4d0SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
610ba43c4d0SJames Smart 				 "6088 DID x%06x not ready for "
611ba43c4d0SJames Smart 				 "IO. State x%x, Type x%x\n",
612ba43c4d0SJames Smart 				 pnvme_rport->port_id,
613ba43c4d0SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type);
614ba43c4d0SJames Smart 		return -ENODEV;
61501649561SJames Smart 	}
61601649561SJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
61701649561SJames Smart 	if (!bmp) {
61801649561SJames Smart 
61901649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
62001649561SJames Smart 				 "6044 Could not find node for DID %x\n",
62101649561SJames Smart 				 pnvme_rport->port_id);
62201649561SJames Smart 		return 2;
62301649561SJames Smart 	}
62401649561SJames Smart 	INIT_LIST_HEAD(&bmp->list);
62501649561SJames Smart 	bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
62601649561SJames Smart 	if (!bmp->virt) {
62701649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
62801649561SJames Smart 				 "6042 Could not find node for DID %x\n",
62901649561SJames Smart 				 pnvme_rport->port_id);
63001649561SJames Smart 		kfree(bmp);
63101649561SJames Smart 		return 3;
63201649561SJames Smart 	}
63301649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
63401649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
63501649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
63601649561SJames Smart 	bpl->tus.f.bdeFlags = 0;
63701649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
63801649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
63901649561SJames Smart 	bpl++;
64001649561SJames Smart 
64101649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
64201649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
64301649561SJames Smart 	bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
64401649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
64501649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
64601649561SJames Smart 
64701649561SJames Smart 	/* Expand print to include key fields. */
64801649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
649815a9c43SJames Smart 			 "6149 Issue LS Req to DID 0x%06x lport %p, rport %p "
650815a9c43SJames Smart 			 "lsreq%p rqstlen:%d rsplen:%d %pad %pad\n",
651815a9c43SJames Smart 			 ndlp->nlp_DID,
65201649561SJames Smart 			 pnvme_lport, pnvme_rport,
65301649561SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
654825c6abbSArnd Bergmann 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
655825c6abbSArnd Bergmann 			 &pnvme_lsreq->rspdma);
65601649561SJames Smart 
65766a210ffSJames Smart 	atomic_inc(&lport->fc4NvmeLsRequests);
65801649561SJames Smart 
65901649561SJames Smart 	/* Hardcode the wait to 30 seconds.  Connections are failing otherwise.
66001649561SJames Smart 	 * This code allows it all to work.
66101649561SJames Smart 	 */
66201649561SJames Smart 	ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
66301649561SJames Smart 				pnvme_lsreq, lpfc_nvme_cmpl_gen_req,
66401649561SJames Smart 				ndlp, 2, 30, 0);
66501649561SJames Smart 	if (ret != WQE_SUCCESS) {
6664b056682SJames Smart 		atomic_inc(&lport->xmt_ls_err);
667815a9c43SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
66801649561SJames Smart 				 "6052 EXIT. issue ls wqe failed lport %p, "
66901649561SJames Smart 				 "rport %p lsreq%p Status %x DID %x\n",
67001649561SJames Smart 				 pnvme_lport, pnvme_rport, pnvme_lsreq,
67101649561SJames Smart 				 ret, ndlp->nlp_DID);
67201649561SJames Smart 		lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
67301649561SJames Smart 		kfree(bmp);
67401649561SJames Smart 		return ret;
67501649561SJames Smart 	}
67601649561SJames Smart 
67701649561SJames Smart 	/* Stub in routine and return 0 for now. */
67801649561SJames Smart 	return ret;
67901649561SJames Smart }
68001649561SJames Smart 
68101649561SJames Smart /**
68201649561SJames Smart  * lpfc_nvme_ls_abort - Issue an Link Service request
68301649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
68401649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
68501649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
68601649561SJames Smart  *
68701649561SJames Smart  * Driver registers this routine to handle any link service request
68801649561SJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
68901649561SJames Smart  *
69001649561SJames Smart  * Return value :
69101649561SJames Smart  *   0 - Success
69201649561SJames Smart  *   TODO: What are the failure codes.
69301649561SJames Smart  **/
69401649561SJames Smart static void
69501649561SJames Smart lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
69601649561SJames Smart 		   struct nvme_fc_remote_port *pnvme_rport,
69701649561SJames Smart 		   struct nvmefc_ls_req *pnvme_lsreq)
69801649561SJames Smart {
69901649561SJames Smart 	struct lpfc_nvme_lport *lport;
70001649561SJames Smart 	struct lpfc_vport *vport;
70101649561SJames Smart 	struct lpfc_hba *phba;
70201649561SJames Smart 	struct lpfc_nodelist *ndlp;
70301649561SJames Smart 	LIST_HEAD(abort_list);
70401649561SJames Smart 	struct lpfc_sli_ring *pring;
70501649561SJames Smart 	struct lpfc_iocbq *wqe, *next_wqe;
70601649561SJames Smart 
70701649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
70866a210ffSJames Smart 	if (unlikely(!lport))
70966a210ffSJames Smart 		return;
71001649561SJames Smart 	vport = lport->vport;
71101649561SJames Smart 	phba = vport->phba;
71201649561SJames Smart 
7133386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
7143386f4bdSJames Smart 		return;
7153386f4bdSJames Smart 
71601649561SJames Smart 	ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
71701649561SJames Smart 	if (!ndlp) {
71801649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
71901649561SJames Smart 				 "6049 Could not find node for DID %x\n",
72001649561SJames Smart 				 pnvme_rport->port_id);
72101649561SJames Smart 		return;
72201649561SJames Smart 	}
72301649561SJames Smart 
72401649561SJames Smart 	/* Expand print to include key fields. */
72501649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
72601649561SJames Smart 			 "6040 ENTER.  lport %p, rport %p lsreq %p rqstlen:%d "
727825c6abbSArnd Bergmann 			 "rsplen:%d %pad %pad\n",
72801649561SJames Smart 			 pnvme_lport, pnvme_rport,
72901649561SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
730825c6abbSArnd Bergmann 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
731825c6abbSArnd Bergmann 			 &pnvme_lsreq->rspdma);
73201649561SJames Smart 
73301649561SJames Smart 	/*
73401649561SJames Smart 	 * Lock the ELS ring txcmplq and build a local list of all ELS IOs
73501649561SJames Smart 	 * that need an ABTS.  The IOs need to stay on the txcmplq so that
73601649561SJames Smart 	 * the abort operation completes them successfully.
73701649561SJames Smart 	 */
73801649561SJames Smart 	pring = phba->sli4_hba.nvmels_wq->pring;
73901649561SJames Smart 	spin_lock_irq(&phba->hbalock);
74001649561SJames Smart 	spin_lock(&pring->ring_lock);
74101649561SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
74201649561SJames Smart 		/* Add to abort_list on on NDLP match. */
74301649561SJames Smart 		if (lpfc_check_sli_ndlp(phba, pring, wqe, ndlp)) {
74401649561SJames Smart 			wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
74501649561SJames Smart 			list_add_tail(&wqe->dlist, &abort_list);
74601649561SJames Smart 		}
74701649561SJames Smart 	}
74801649561SJames Smart 	spin_unlock(&pring->ring_lock);
74901649561SJames Smart 	spin_unlock_irq(&phba->hbalock);
75001649561SJames Smart 
75101649561SJames Smart 	/* Abort the targeted IOs and remove them from the abort list. */
75201649561SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &abort_list, dlist) {
7534b056682SJames Smart 		atomic_inc(&lport->xmt_ls_abort);
75401649561SJames Smart 		spin_lock_irq(&phba->hbalock);
75501649561SJames Smart 		list_del_init(&wqe->dlist);
75601649561SJames Smart 		lpfc_sli_issue_abort_iotag(phba, pring, wqe);
75701649561SJames Smart 		spin_unlock_irq(&phba->hbalock);
75801649561SJames Smart 	}
75901649561SJames Smart }
76001649561SJames Smart 
76101649561SJames Smart /* Fix up the existing sgls for NVME IO. */
7625fd11085SJames Smart static inline void
76301649561SJames Smart lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
764c490850aSJames Smart 		       struct lpfc_io_buf *lpfc_ncmd,
76501649561SJames Smart 		       struct nvmefc_fcp_req *nCmd)
76601649561SJames Smart {
7674e565cf0SJames Smart 	struct lpfc_hba  *phba = vport->phba;
76801649561SJames Smart 	struct sli4_sge *sgl;
76901649561SJames Smart 	union lpfc_wqe128 *wqe;
77001649561SJames Smart 	uint32_t *wptr, *dptr;
77101649561SJames Smart 
77201649561SJames Smart 	/*
7734e565cf0SJames Smart 	 * Get a local pointer to the built-in wqe and correct
7744e565cf0SJames Smart 	 * the cmd size to match NVME's 96 bytes and fix
7754e565cf0SJames Smart 	 * the dma address.
7764e565cf0SJames Smart 	 */
7774e565cf0SJames Smart 
778205e8240SJames Smart 	wqe = &lpfc_ncmd->cur_iocbq.wqe;
7794e565cf0SJames Smart 
7804e565cf0SJames Smart 	/*
78101649561SJames Smart 	 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
78201649561SJames Smart 	 * match NVME.  NVME sends 96 bytes. Also, use the
78301649561SJames Smart 	 * nvme commands command and response dma addresses
78401649561SJames Smart 	 * rather than the virtual memory to ease the restore
78501649561SJames Smart 	 * operation.
78601649561SJames Smart 	 */
7870794d601SJames Smart 	sgl = lpfc_ncmd->dma_sgl;
78801649561SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
7894e565cf0SJames Smart 	if (phba->cfg_nvme_embed_cmd) {
7904e565cf0SJames Smart 		sgl->addr_hi = 0;
7914e565cf0SJames Smart 		sgl->addr_lo = 0;
7924e565cf0SJames Smart 
7934e565cf0SJames Smart 		/* Word 0-2 - NVME CMND IU (embedded payload) */
7944e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
7954e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeSize = 56;
7964e565cf0SJames Smart 		wqe->generic.bde.addrHigh = 0;
7974e565cf0SJames Smart 		wqe->generic.bde.addrLow =  64;  /* Word 16 */
7984e565cf0SJames Smart 
7995fd11085SJames Smart 		/* Word 10  - dbde is 0, wqes is 1 in template */
80001649561SJames Smart 
80101649561SJames Smart 		/*
80201649561SJames Smart 		 * Embed the payload in the last half of the WQE
80301649561SJames Smart 		 * WQE words 16-30 get the NVME CMD IU payload
80401649561SJames Smart 		 *
805b06a622fSJames Smart 		 * WQE words 16-19 get payload Words 1-4
80601649561SJames Smart 		 * WQE words 20-21 get payload Words 6-7
80701649561SJames Smart 		 * WQE words 22-29 get payload Words 16-23
80801649561SJames Smart 		 */
809b06a622fSJames Smart 		wptr = &wqe->words[16];  /* WQE ptr */
81001649561SJames Smart 		dptr = (uint32_t *)nCmd->cmdaddr;  /* payload ptr */
811b06a622fSJames Smart 		dptr++;			/* Skip Word 0 in payload */
81201649561SJames Smart 
813b06a622fSJames Smart 		*wptr++ = *dptr++;	/* Word 1 */
81401649561SJames Smart 		*wptr++ = *dptr++;	/* Word 2 */
81501649561SJames Smart 		*wptr++ = *dptr++;	/* Word 3 */
81601649561SJames Smart 		*wptr++ = *dptr++;	/* Word 4 */
81701649561SJames Smart 		dptr++;			/* Skip Word 5 in payload */
81801649561SJames Smart 		*wptr++ = *dptr++;	/* Word 6 */
81901649561SJames Smart 		*wptr++ = *dptr++;	/* Word 7 */
82001649561SJames Smart 		dptr += 8;		/* Skip Words 8-15 in payload */
82101649561SJames Smart 		*wptr++ = *dptr++;	/* Word 16 */
82201649561SJames Smart 		*wptr++ = *dptr++;	/* Word 17 */
82301649561SJames Smart 		*wptr++ = *dptr++;	/* Word 18 */
82401649561SJames Smart 		*wptr++ = *dptr++;	/* Word 19 */
82501649561SJames Smart 		*wptr++ = *dptr++;	/* Word 20 */
82601649561SJames Smart 		*wptr++ = *dptr++;	/* Word 21 */
82701649561SJames Smart 		*wptr++ = *dptr++;	/* Word 22 */
82801649561SJames Smart 		*wptr   = *dptr;	/* Word 23 */
8295fd11085SJames Smart 	} else {
8305fd11085SJames Smart 		sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->cmddma));
8315fd11085SJames Smart 		sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->cmddma));
8325fd11085SJames Smart 
8335fd11085SJames Smart 		/* Word 0-2 - NVME CMND IU Inline BDE */
8345fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
8355fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeSize = nCmd->cmdlen;
8365fd11085SJames Smart 		wqe->generic.bde.addrHigh = sgl->addr_hi;
8375fd11085SJames Smart 		wqe->generic.bde.addrLow =  sgl->addr_lo;
8385fd11085SJames Smart 
8395fd11085SJames Smart 		/* Word 10 */
8405fd11085SJames Smart 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
8415fd11085SJames Smart 		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
8425fd11085SJames Smart 	}
8435fd11085SJames Smart 
8445fd11085SJames Smart 	sgl++;
8455fd11085SJames Smart 
8465fd11085SJames Smart 	/* Setup the physical region for the FCP RSP */
8475fd11085SJames Smart 	sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
8485fd11085SJames Smart 	sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
8495fd11085SJames Smart 	sgl->word2 = le32_to_cpu(sgl->word2);
8505fd11085SJames Smart 	if (nCmd->sg_cnt)
8515fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
8525fd11085SJames Smart 	else
8535fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 1);
8545fd11085SJames Smart 	sgl->word2 = cpu_to_le32(sgl->word2);
8555fd11085SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->rsplen);
85601649561SJames Smart }
85701649561SJames Smart 
858bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
859bd2cdd5eSJames Smart static void
860bd2cdd5eSJames Smart lpfc_nvme_ktime(struct lpfc_hba *phba,
861c490850aSJames Smart 		struct lpfc_io_buf *lpfc_ncmd)
862bd2cdd5eSJames Smart {
863bd2cdd5eSJames Smart 	uint64_t seg1, seg2, seg3, seg4;
864c8a4ce0bSDick Kennedy 	uint64_t segsum;
865bd2cdd5eSJames Smart 
866bd2cdd5eSJames Smart 	if (!lpfc_ncmd->ts_last_cmd ||
867bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_cmd_start ||
868bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_cmd_wqput ||
869bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_isr_cmpl ||
870bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_data_nvme)
871bd2cdd5eSJames Smart 		return;
872c8a4ce0bSDick Kennedy 
873c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_cmd_start)
874c8a4ce0bSDick Kennedy 		return;
875bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_cmd_start < lpfc_ncmd->ts_last_cmd)
876bd2cdd5eSJames Smart 		return;
877bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_cmd_wqput < lpfc_ncmd->ts_cmd_start)
878bd2cdd5eSJames Smart 		return;
879bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_isr_cmpl < lpfc_ncmd->ts_cmd_wqput)
880bd2cdd5eSJames Smart 		return;
881bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_isr_cmpl)
882bd2cdd5eSJames Smart 		return;
883bd2cdd5eSJames Smart 	/*
884bd2cdd5eSJames Smart 	 * Segment 1 - Time from Last FCP command cmpl is handed
885bd2cdd5eSJames Smart 	 * off to NVME Layer to start of next command.
886bd2cdd5eSJames Smart 	 * Segment 2 - Time from Driver receives a IO cmd start
887bd2cdd5eSJames Smart 	 * from NVME Layer to WQ put is done on IO cmd.
888bd2cdd5eSJames Smart 	 * Segment 3 - Time from Driver WQ put is done on IO cmd
889bd2cdd5eSJames Smart 	 * to MSI-X ISR for IO cmpl.
890bd2cdd5eSJames Smart 	 * Segment 4 - Time from MSI-X ISR for IO cmpl to when
891bd2cdd5eSJames Smart 	 * cmpl is handled off to the NVME Layer.
892bd2cdd5eSJames Smart 	 */
893bd2cdd5eSJames Smart 	seg1 = lpfc_ncmd->ts_cmd_start - lpfc_ncmd->ts_last_cmd;
894c8a4ce0bSDick Kennedy 	if (seg1 > 5000000)  /* 5 ms - for sequential IOs only */
895c8a4ce0bSDick Kennedy 		seg1 = 0;
896bd2cdd5eSJames Smart 
897bd2cdd5eSJames Smart 	/* Calculate times relative to start of IO */
898bd2cdd5eSJames Smart 	seg2 = (lpfc_ncmd->ts_cmd_wqput - lpfc_ncmd->ts_cmd_start);
899c8a4ce0bSDick Kennedy 	segsum = seg2;
900c8a4ce0bSDick Kennedy 	seg3 = lpfc_ncmd->ts_isr_cmpl - lpfc_ncmd->ts_cmd_start;
901c8a4ce0bSDick Kennedy 	if (segsum > seg3)
902c8a4ce0bSDick Kennedy 		return;
903c8a4ce0bSDick Kennedy 	seg3 -= segsum;
904c8a4ce0bSDick Kennedy 	segsum += seg3;
905c8a4ce0bSDick Kennedy 
906c8a4ce0bSDick Kennedy 	seg4 = lpfc_ncmd->ts_data_nvme - lpfc_ncmd->ts_cmd_start;
907c8a4ce0bSDick Kennedy 	if (segsum > seg4)
908c8a4ce0bSDick Kennedy 		return;
909c8a4ce0bSDick Kennedy 	seg4 -= segsum;
910c8a4ce0bSDick Kennedy 
911bd2cdd5eSJames Smart 	phba->ktime_data_samples++;
912bd2cdd5eSJames Smart 	phba->ktime_seg1_total += seg1;
913bd2cdd5eSJames Smart 	if (seg1 < phba->ktime_seg1_min)
914bd2cdd5eSJames Smart 		phba->ktime_seg1_min = seg1;
915bd2cdd5eSJames Smart 	else if (seg1 > phba->ktime_seg1_max)
916bd2cdd5eSJames Smart 		phba->ktime_seg1_max = seg1;
917bd2cdd5eSJames Smart 	phba->ktime_seg2_total += seg2;
918bd2cdd5eSJames Smart 	if (seg2 < phba->ktime_seg2_min)
919bd2cdd5eSJames Smart 		phba->ktime_seg2_min = seg2;
920bd2cdd5eSJames Smart 	else if (seg2 > phba->ktime_seg2_max)
921bd2cdd5eSJames Smart 		phba->ktime_seg2_max = seg2;
922bd2cdd5eSJames Smart 	phba->ktime_seg3_total += seg3;
923bd2cdd5eSJames Smart 	if (seg3 < phba->ktime_seg3_min)
924bd2cdd5eSJames Smart 		phba->ktime_seg3_min = seg3;
925bd2cdd5eSJames Smart 	else if (seg3 > phba->ktime_seg3_max)
926bd2cdd5eSJames Smart 		phba->ktime_seg3_max = seg3;
927bd2cdd5eSJames Smart 	phba->ktime_seg4_total += seg4;
928bd2cdd5eSJames Smart 	if (seg4 < phba->ktime_seg4_min)
929bd2cdd5eSJames Smart 		phba->ktime_seg4_min = seg4;
930bd2cdd5eSJames Smart 	else if (seg4 > phba->ktime_seg4_max)
931bd2cdd5eSJames Smart 		phba->ktime_seg4_max = seg4;
932bd2cdd5eSJames Smart 
933bd2cdd5eSJames Smart 	lpfc_ncmd->ts_last_cmd = 0;
934bd2cdd5eSJames Smart 	lpfc_ncmd->ts_cmd_start = 0;
935bd2cdd5eSJames Smart 	lpfc_ncmd->ts_cmd_wqput  = 0;
936bd2cdd5eSJames Smart 	lpfc_ncmd->ts_isr_cmpl = 0;
937bd2cdd5eSJames Smart 	lpfc_ncmd->ts_data_nvme = 0;
938bd2cdd5eSJames Smart }
939bd2cdd5eSJames Smart #endif
940bd2cdd5eSJames Smart 
94101649561SJames Smart /**
94201649561SJames Smart  * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
94301649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
94401649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
94501649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
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;
97101649561SJames Smart 
97201649561SJames Smart 	/* Sanity check on return of outstanding command */
9734d5e789aSJames Smart 	if (!lpfc_ncmd) {
9744d5e789aSJames Smart 		lpfc_printf_vlog(vport, KERN_ERR,
9754d5e789aSJames Smart 				 LOG_NODE | LOG_NVME_IOERR,
9764d5e789aSJames Smart 				 "6071 Null lpfc_ncmd pointer. No "
9774d5e789aSJames Smart 				 "release, skip completion\n");
9784d5e789aSJames Smart 		return;
9794d5e789aSJames Smart 	}
9804d5e789aSJames Smart 
981c2017260SJames Smart 	/* Guard against abort handler being called at same time */
982c2017260SJames Smart 	spin_lock(&lpfc_ncmd->buf_lock);
983c2017260SJames Smart 
984c2017260SJames Smart 	if (!lpfc_ncmd->nvmeCmd) {
985c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
98601649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
9874d5e789aSJames Smart 				 "6066 Missing cmpl ptrs: lpfc_ncmd %p, "
9880b05e9feSJames Smart 				 "nvmeCmd %p\n",
9890b05e9feSJames Smart 				 lpfc_ncmd, lpfc_ncmd->nvmeCmd);
9904d5e789aSJames Smart 
9914d5e789aSJames Smart 		/* Release the lpfc_ncmd regardless of the missing elements. */
9924d5e789aSJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
99301649561SJames Smart 		return;
99401649561SJames Smart 	}
99501649561SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
9964b056682SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
99766a210ffSJames Smart 
9984c47efc1SJames Smart 	idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
9994c47efc1SJames Smart 	phba->sli4_hba.hdwq[idx].nvme_cstat.io_cmpls++;
10004c47efc1SJames Smart 
100166a85155SJames Smart 	if (vport->localport) {
10024b056682SJames Smart 		lport = (struct lpfc_nvme_lport *)vport->localport->private;
10034c47efc1SJames Smart 		if (lport && status) {
10044b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
10054b056682SJames Smart 				atomic_inc(&lport->cmpl_fcp_xb);
10064b056682SJames Smart 			atomic_inc(&lport->cmpl_fcp_err);
10074b056682SJames Smart 		}
100866a210ffSJames Smart 	}
100901649561SJames Smart 
1010bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
1011bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
10124b056682SJames Smart 			 status, wcqe->parameter);
101301649561SJames Smart 	/*
101401649561SJames Smart 	 * Catch race where our node has transitioned, but the
101501649561SJames Smart 	 * transport is still transitioning.
101601649561SJames Smart 	 */
10170b05e9feSJames Smart 	ndlp = lpfc_ncmd->ndlp;
101801649561SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
101901649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
102001649561SJames Smart 				 "6062 Ignoring NVME cmpl.  No ndlp\n");
102101649561SJames Smart 		goto out_err;
102201649561SJames Smart 	}
102301649561SJames Smart 
102401649561SJames Smart 	code = bf_get(lpfc_wcqe_c_code, wcqe);
102501649561SJames Smart 	if (code == CQE_CODE_NVME_ERSP) {
102601649561SJames Smart 		/* For this type of CQE, we need to rebuild the rsp */
102701649561SJames Smart 		ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
102801649561SJames Smart 
102901649561SJames Smart 		/*
103001649561SJames Smart 		 * Get Command Id from cmd to plug into response. This
103101649561SJames Smart 		 * code is not needed in the next NVME Transport drop.
103201649561SJames Smart 		 */
103301649561SJames Smart 		cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
103401649561SJames Smart 		cid = cp->sqe.common.command_id;
103501649561SJames Smart 
103601649561SJames Smart 		/*
103701649561SJames Smart 		 * RSN is in CQE word 2
103801649561SJames Smart 		 * SQHD is in CQE Word 3 bits 15:0
103901649561SJames Smart 		 * Cmd Specific info is in CQE Word 1
104001649561SJames Smart 		 * and in CQE Word 0 bits 15:0
104101649561SJames Smart 		 */
104201649561SJames Smart 		sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
104301649561SJames Smart 
104401649561SJames Smart 		/* Now lets build the NVME ERSP IU */
104501649561SJames Smart 		ep->iu_len = cpu_to_be16(8);
104601649561SJames Smart 		ep->rsn = wcqe->parameter;
104701649561SJames Smart 		ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
104801649561SJames Smart 		ep->rsvd12 = 0;
104901649561SJames Smart 		ptr = (uint32_t *)&ep->cqe.result.u64;
105001649561SJames Smart 		*ptr++ = wcqe->total_data_placed;
105101649561SJames Smart 		data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
105201649561SJames Smart 		*ptr = (uint32_t)data;
105301649561SJames Smart 		ep->cqe.sq_head = sqhd;
105401649561SJames Smart 		ep->cqe.sq_id =  nCmd->sqid;
105501649561SJames Smart 		ep->cqe.command_id = cid;
105601649561SJames Smart 		ep->cqe.status = 0;
105701649561SJames Smart 
105801649561SJames Smart 		lpfc_ncmd->status = IOSTAT_SUCCESS;
105901649561SJames Smart 		lpfc_ncmd->result = 0;
106001649561SJames Smart 		nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
106101649561SJames Smart 		nCmd->transferred_length = nCmd->payload_length;
106201649561SJames Smart 	} else {
10634b056682SJames Smart 		lpfc_ncmd->status = (status & LPFC_IOCB_STATUS_MASK);
1064952c303bSDick Kennedy 		lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
106501649561SJames Smart 
106601649561SJames Smart 		/* For NVME, the only failure path that results in an
106701649561SJames Smart 		 * IO error is when the adapter rejects it.  All other
106801649561SJames Smart 		 * conditions are a success case and resolved by the
106901649561SJames Smart 		 * transport.
107001649561SJames Smart 		 * IOSTAT_FCP_RSP_ERROR means:
107101649561SJames Smart 		 * 1. Length of data received doesn't match total
107201649561SJames Smart 		 *    transfer length in WQE
107301649561SJames Smart 		 * 2. If the RSP payload does NOT match these cases:
107401649561SJames Smart 		 *    a. RSP length 12/24 bytes and all zeros
107501649561SJames Smart 		 *    b. NVME ERSP
107601649561SJames Smart 		 */
107701649561SJames Smart 		switch (lpfc_ncmd->status) {
107801649561SJames Smart 		case IOSTAT_SUCCESS:
107901649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
108001649561SJames Smart 			nCmd->rcv_rsplen = 0;
108101649561SJames Smart 			nCmd->status = 0;
108201649561SJames Smart 			break;
108301649561SJames Smart 		case IOSTAT_FCP_RSP_ERROR:
108401649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
108501649561SJames Smart 			nCmd->rcv_rsplen = wcqe->parameter;
108601649561SJames Smart 			nCmd->status = 0;
108701649561SJames Smart 			/* Sanity check */
108801649561SJames Smart 			if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
108901649561SJames Smart 				break;
109001649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
109101649561SJames Smart 					 "6081 NVME Completion Protocol Error: "
109286c67379SJames Smart 					 "xri %x status x%x result x%x "
109386c67379SJames Smart 					 "placed x%x\n",
109486c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
109501649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
109601649561SJames Smart 					 wcqe->total_data_placed);
109701649561SJames Smart 			break;
1098952c303bSDick Kennedy 		case IOSTAT_LOCAL_REJECT:
1099952c303bSDick Kennedy 			/* Let fall through to set command final state. */
1100952c303bSDick Kennedy 			if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
1101952c303bSDick Kennedy 				lpfc_printf_vlog(vport, KERN_INFO,
1102952c303bSDick Kennedy 					 LOG_NVME_IOERR,
1103952c303bSDick Kennedy 					 "6032 Delay Aborted cmd %p "
1104952c303bSDick Kennedy 					 "nvme cmd %p, xri x%x, "
1105952c303bSDick Kennedy 					 "xb %d\n",
1106952c303bSDick Kennedy 					 lpfc_ncmd, nCmd,
1107952c303bSDick Kennedy 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
1108952c303bSDick Kennedy 					 bf_get(lpfc_wcqe_c_xb, wcqe));
1109cd05c155SBart Van Assche 			/* fall through */
111001649561SJames Smart 		default:
111101649561SJames Smart out_err:
1112e3246a12SJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
111386c67379SJames Smart 					 "6072 NVME Completion Error: xri %x "
11142c013a3aSJames Smart 					 "status x%x result x%x [x%x] "
11152c013a3aSJames Smart 					 "placed x%x\n",
111686c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
111701649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
11182c013a3aSJames Smart 					 wcqe->parameter,
111901649561SJames Smart 					 wcqe->total_data_placed);
112001649561SJames Smart 			nCmd->transferred_length = 0;
112101649561SJames Smart 			nCmd->rcv_rsplen = 0;
11228e009ce8SJames Smart 			nCmd->status = NVME_SC_INTERNAL;
112301649561SJames Smart 		}
112401649561SJames Smart 	}
112501649561SJames Smart 
112601649561SJames Smart 	/* pick up SLI4 exhange busy condition */
112701649561SJames Smart 	if (bf_get(lpfc_wcqe_c_xb, wcqe))
112801649561SJames Smart 		lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
112901649561SJames Smart 	else
113001649561SJames Smart 		lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
113101649561SJames Smart 
113201649561SJames Smart 	/* Update stats and complete the IO.  There is
113301649561SJames Smart 	 * no need for dma unprep because the nvme_transport
113401649561SJames Smart 	 * owns the dma address.
113501649561SJames Smart 	 */
1136bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1137c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start) {
1138bd2cdd5eSJames Smart 		lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
1139bd2cdd5eSJames Smart 		lpfc_ncmd->ts_data_nvme = ktime_get_ns();
1140bd2cdd5eSJames Smart 		phba->ktime_last_cmd = lpfc_ncmd->ts_data_nvme;
1141bd2cdd5eSJames Smart 		lpfc_nvme_ktime(phba, lpfc_ncmd);
1142bd2cdd5eSJames Smart 	}
1143bd2cdd5eSJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1144352b205aSArnd Bergmann 		uint32_t cpu;
114563df6d63SJames Smart 		idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
1146d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
114763df6d63SJames Smart 		if (cpu < LPFC_CHECK_CPU_CNT) {
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);
115463df6d63SJames Smart 			phba->sli4_hba.hdwq[idx].cpucheck_cmpl_io[cpu]++;
115563df6d63SJames Smart 		}
1156bd2cdd5eSJames Smart 	}
1157bd2cdd5eSJames Smart #endif
1158952c303bSDick Kennedy 
1159952c303bSDick Kennedy 	/* NVME targets need completion held off until the abort exchange
1160add9d6beSJames Smart 	 * completes unless the NVME Rport is getting unregistered.
1161952c303bSDick Kennedy 	 */
1162add9d6beSJames Smart 
11633fd78355SJames Smart 	if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
116491455b85SJames Smart 		freqpriv = nCmd->private;
116591455b85SJames Smart 		freqpriv->nvme_buf = NULL;
11663fd78355SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
1167c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
1168c2017260SJames Smart 		nCmd->done(nCmd);
1169c2017260SJames Smart 	} else
1170c2017260SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
117101649561SJames Smart 
1172952c303bSDick Kennedy 	/* Call release with XB=1 to queue the IO into the abort list. */
117301649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
117401649561SJames Smart }
117501649561SJames Smart 
117601649561SJames Smart 
117701649561SJames Smart /**
117801649561SJames Smart  * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
117901649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
118001649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
118101649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
118201649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
118301649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
118401649561SJames Smart  *
118501649561SJames Smart  * Driver registers this routine as it io request handler.  This
118601649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
118701649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
118801649561SJames Smart  *
118901649561SJames Smart  * Return value :
119001649561SJames Smart  *   0 - Success
119101649561SJames Smart  *   TODO: What are the failure codes.
119201649561SJames Smart  **/
119301649561SJames Smart static int
119401649561SJames Smart lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
1195c490850aSJames Smart 		      struct lpfc_io_buf *lpfc_ncmd,
119666a210ffSJames Smart 		      struct lpfc_nodelist *pnode,
11974c47efc1SJames Smart 		      struct lpfc_fc4_ctrl_stat *cstat)
119801649561SJames Smart {
119901649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
120001649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
120101649561SJames Smart 	struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
1202205e8240SJames Smart 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
120301649561SJames Smart 	uint32_t req_len;
120401649561SJames Smart 
1205e960f5abSJames Smart 	if (!NLP_CHK_NODE_ACT(pnode))
120601649561SJames Smart 		return -EINVAL;
120701649561SJames Smart 
120801649561SJames Smart 	/*
120901649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
121001649561SJames Smart 	 * the single mapping, or neither.
121101649561SJames Smart 	 */
121201649561SJames Smart 	if (nCmd->sg_cnt) {
121301649561SJames Smart 		if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
12145fd11085SJames Smart 			/* From the iwrite template, initialize words 7 - 11 */
12155fd11085SJames Smart 			memcpy(&wqe->words[7],
12165fd11085SJames Smart 			       &lpfc_iwrite_cmd_template.words[7],
12175fd11085SJames Smart 			       sizeof(uint32_t) * 5);
12185fd11085SJames Smart 
12195fd11085SJames Smart 			/* Word 4 */
12205fd11085SJames Smart 			wqe->fcp_iwrite.total_xfer_len = nCmd->payload_length;
12215fd11085SJames Smart 
122201649561SJames Smart 			/* Word 5 */
122301649561SJames Smart 			if ((phba->cfg_nvme_enable_fb) &&
122401649561SJames Smart 			    (pnode->nlp_flag & NLP_FIRSTBURST)) {
122501649561SJames Smart 				req_len = lpfc_ncmd->nvmeCmd->payload_length;
122601649561SJames Smart 				if (req_len < pnode->nvme_fb_size)
122701649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
122801649561SJames Smart 						req_len;
122901649561SJames Smart 				else
123001649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
123101649561SJames Smart 						pnode->nvme_fb_size;
12325fd11085SJames Smart 			} else {
12335fd11085SJames Smart 				wqe->fcp_iwrite.initial_xfer_len = 0;
123401649561SJames Smart 			}
12354c47efc1SJames Smart 			cstat->output_requests++;
123601649561SJames Smart 		} else {
12375fd11085SJames Smart 			/* From the iread template, initialize words 7 - 11 */
12385fd11085SJames Smart 			memcpy(&wqe->words[7],
12395fd11085SJames Smart 			       &lpfc_iread_cmd_template.words[7],
12405fd11085SJames Smart 			       sizeof(uint32_t) * 5);
124101649561SJames Smart 
12425fd11085SJames Smart 			/* Word 4 */
12435fd11085SJames Smart 			wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
124401649561SJames Smart 
12455fd11085SJames Smart 			/* Word 5 */
12465fd11085SJames Smart 			wqe->fcp_iread.rsrvd5 = 0;
124701649561SJames Smart 
12484c47efc1SJames Smart 			cstat->input_requests++;
124901649561SJames Smart 		}
125001649561SJames Smart 	} else {
12515fd11085SJames Smart 		/* From the icmnd template, initialize words 4 - 11 */
12525fd11085SJames Smart 		memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
12535fd11085SJames Smart 		       sizeof(uint32_t) * 8);
12544c47efc1SJames Smart 		cstat->control_requests++;
125501649561SJames Smart 	}
125601649561SJames Smart 	/*
125701649561SJames Smart 	 * Finish initializing those WQE fields that are independent
125801649561SJames Smart 	 * of the nvme_cmnd request_buffer
125901649561SJames Smart 	 */
126001649561SJames Smart 
12615fd11085SJames Smart 	/* Word 3 */
12625fd11085SJames Smart 	bf_set(payload_offset_len, &wqe->fcp_icmd,
12635fd11085SJames Smart 	       (nCmd->rsplen + nCmd->cmdlen));
12645fd11085SJames Smart 
126501649561SJames Smart 	/* Word 6 */
126601649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
126701649561SJames Smart 	       phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
126801649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
126901649561SJames Smart 
127001649561SJames Smart 	/* Word 8 */
127101649561SJames Smart 	wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
127201649561SJames Smart 
127301649561SJames Smart 	/* Word 9 */
127401649561SJames Smart 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
127501649561SJames 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
128501649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
128601649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
128701649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
128801649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
128901649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
129001649561SJames Smart  *
129101649561SJames Smart  * Driver registers this routine as it io request handler.  This
129201649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
129301649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
129401649561SJames Smart  *
129501649561SJames Smart  * Return value :
129601649561SJames Smart  *   0 - Success
129701649561SJames Smart  *   TODO: What are the failure codes.
129801649561SJames Smart  **/
129901649561SJames Smart static int
130001649561SJames Smart lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
1301c490850aSJames Smart 		      struct lpfc_io_buf *lpfc_ncmd)
130201649561SJames Smart {
130301649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
130401649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1305205e8240SJames Smart 	union lpfc_wqe128 *wqe = &lpfc_ncmd->cur_iocbq.wqe;
13060794d601SJames Smart 	struct sli4_sge *sgl = lpfc_ncmd->dma_sgl;
130701649561SJames Smart 	struct scatterlist *data_sg;
130801649561SJames Smart 	struct sli4_sge *first_data_sgl;
13090bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
131001649561SJames Smart 	dma_addr_t physaddr;
131101649561SJames Smart 	uint32_t num_bde = 0;
131201649561SJames Smart 	uint32_t dma_len;
131301649561SJames Smart 	uint32_t dma_offset = 0;
131401649561SJames Smart 	int nseg, i;
131501649561SJames Smart 
131601649561SJames Smart 	/* Fix up the command and response DMA stuff. */
131701649561SJames Smart 	lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
131801649561SJames Smart 
131901649561SJames Smart 	/*
132001649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
132101649561SJames Smart 	 * the single mapping, or neither.
132201649561SJames Smart 	 */
132301649561SJames Smart 	if (nCmd->sg_cnt) {
132401649561SJames Smart 		/*
132501649561SJames Smart 		 * Jump over the cmd and rsp SGEs.  The fix routine
132601649561SJames Smart 		 * has already adjusted for this.
132701649561SJames Smart 		 */
132801649561SJames Smart 		sgl += 2;
132901649561SJames Smart 
133001649561SJames Smart 		first_data_sgl = sgl;
133101649561SJames Smart 		lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
133281e6a637SJames Smart 		if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
133301649561SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
133401649561SJames Smart 					"6058 Too many sg segments from "
133501649561SJames Smart 					"NVME Transport.  Max %d, "
133601649561SJames Smart 					"nvmeIO sg_cnt %d\n",
133729bfd55aSJames Smart 					phba->cfg_nvme_seg_cnt + 1,
133801649561SJames Smart 					lpfc_ncmd->seg_cnt);
133901649561SJames Smart 			lpfc_ncmd->seg_cnt = 0;
134001649561SJames Smart 			return 1;
134101649561SJames Smart 		}
134201649561SJames Smart 
134301649561SJames Smart 		/*
134401649561SJames Smart 		 * The driver established a maximum scatter-gather segment count
134501649561SJames Smart 		 * during probe that limits the number of sg elements in any
134601649561SJames Smart 		 * single nvme command.  Just run through the seg_cnt and format
134701649561SJames Smart 		 * the sge's.
134801649561SJames Smart 		 */
134901649561SJames Smart 		nseg = nCmd->sg_cnt;
135001649561SJames Smart 		data_sg = nCmd->first_sgl;
135101649561SJames Smart 		for (i = 0; i < nseg; i++) {
135201649561SJames Smart 			if (data_sg == NULL) {
135301649561SJames Smart 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
135401649561SJames Smart 						"6059 dptr err %d, nseg %d\n",
135501649561SJames Smart 						i, nseg);
135601649561SJames Smart 				lpfc_ncmd->seg_cnt = 0;
135701649561SJames Smart 				return 1;
135801649561SJames Smart 			}
135901649561SJames Smart 			physaddr = data_sg->dma_address;
136001649561SJames Smart 			dma_len = data_sg->length;
136101649561SJames Smart 			sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
136201649561SJames Smart 			sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
136301649561SJames Smart 			sgl->word2 = le32_to_cpu(sgl->word2);
136401649561SJames Smart 			if ((num_bde + 1) == nseg)
136501649561SJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 1);
136601649561SJames Smart 			else
136701649561SJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 0);
136801649561SJames Smart 			bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
136901649561SJames Smart 			bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
137001649561SJames Smart 			sgl->word2 = cpu_to_le32(sgl->word2);
137101649561SJames Smart 			sgl->sge_len = cpu_to_le32(dma_len);
137201649561SJames Smart 
137301649561SJames Smart 			dma_offset += dma_len;
137401649561SJames Smart 			data_sg = sg_next(data_sg);
137501649561SJames Smart 			sgl++;
137601649561SJames Smart 		}
1377414abe0aSJames Smart 		if (phba->cfg_enable_pbde) {
13780bc2b7c5SJames Smart 			/* Use PBDE support for first SGL only, offset == 0 */
13790bc2b7c5SJames Smart 			/* Words 13-15 */
13800bc2b7c5SJames Smart 			bde = (struct ulp_bde64 *)
13810bc2b7c5SJames Smart 				&wqe->words[13];
13820bc2b7c5SJames Smart 			bde->addrLow = first_data_sgl->addr_lo;
13830bc2b7c5SJames Smart 			bde->addrHigh = first_data_sgl->addr_hi;
13840bc2b7c5SJames Smart 			bde->tus.f.bdeSize =
13850bc2b7c5SJames Smart 				le32_to_cpu(first_data_sgl->sge_len);
13860bc2b7c5SJames Smart 			bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
13870bc2b7c5SJames Smart 			bde->tus.w = cpu_to_le32(bde->tus.w);
13885fd11085SJames Smart 			/* wqe_pbde is 1 in template */
13895fd11085SJames Smart 		} else {
13905fd11085SJames Smart 			memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
13910bc2b7c5SJames Smart 			bf_set(wqe_pbde, &wqe->generic.wqe_com, 0);
13925fd11085SJames Smart 		}
13930bc2b7c5SJames Smart 
1394414abe0aSJames Smart 	} else {
13950794d601SJames Smart 		lpfc_ncmd->seg_cnt = 0;
13960794d601SJames Smart 
139701649561SJames Smart 		/* For this clause to be valid, the payload_length
139801649561SJames Smart 		 * and sg_cnt must zero.
139901649561SJames Smart 		 */
140001649561SJames Smart 		if (nCmd->payload_length != 0) {
140101649561SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
140201649561SJames Smart 					"6063 NVME DMA Prep Err: sg_cnt %d "
140301649561SJames Smart 					"payload_length x%x\n",
140401649561SJames Smart 					nCmd->sg_cnt, nCmd->payload_length);
140501649561SJames Smart 			return 1;
140601649561SJames Smart 		}
140701649561SJames Smart 	}
140801649561SJames Smart 	return 0;
140901649561SJames Smart }
141001649561SJames Smart 
141101649561SJames Smart /**
141201649561SJames Smart  * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
141301649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
141401649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
141501649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
141601649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
141701649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
141801649561SJames Smart  *
141901649561SJames Smart  * Driver registers this routine as it io request handler.  This
142001649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
142101649561SJames Smart  * data structure to the rport
142201649561SJames Smart  indicated in @lpfc_nvme_rport.
142301649561SJames Smart  *
142401649561SJames Smart  * Return value :
142501649561SJames Smart  *   0 - Success
142601649561SJames Smart  *   TODO: What are the failure codes.
142701649561SJames Smart  **/
142801649561SJames Smart static int
142901649561SJames Smart lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
143001649561SJames Smart 			struct nvme_fc_remote_port *pnvme_rport,
143101649561SJames Smart 			void *hw_queue_handle,
143201649561SJames Smart 			struct nvmefc_fcp_req *pnvme_fcreq)
143301649561SJames Smart {
143401649561SJames Smart 	int ret = 0;
1435cf1a1d3eSJames Smart 	int expedite = 0;
143663df6d63SJames Smart 	int idx, cpu;
143701649561SJames Smart 	struct lpfc_nvme_lport *lport;
14384c47efc1SJames Smart 	struct lpfc_fc4_ctrl_stat *cstat;
143901649561SJames Smart 	struct lpfc_vport *vport;
144001649561SJames Smart 	struct lpfc_hba *phba;
144101649561SJames Smart 	struct lpfc_nodelist *ndlp;
1442c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
144301649561SJames Smart 	struct lpfc_nvme_rport *rport;
144401649561SJames Smart 	struct lpfc_nvme_qhandle *lpfc_queue_info;
1445c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
1446cf1a1d3eSJames Smart 	struct nvme_common_command *sqe;
1447bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1448bd2cdd5eSJames Smart 	uint64_t start = 0;
1449bd2cdd5eSJames Smart #endif
145001649561SJames Smart 
1451c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1452c3725bdcSJames Smart 	 * have timing races.
1453c3725bdcSJames Smart 	 */
145401649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1455c3725bdcSJames Smart 	if (unlikely(!lport)) {
1456c3725bdcSJames Smart 		ret = -EINVAL;
1457c3725bdcSJames Smart 		goto out_fail;
1458c3725bdcSJames Smart 	}
1459c3725bdcSJames Smart 
146001649561SJames Smart 	vport = lport->vport;
1461c3725bdcSJames Smart 
1462c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
146344c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
146444c2757bSJames Smart 				 "6117 Fail IO, NULL hw_queue_handle\n");
146544c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1466cd240071SJames Smart 		ret = -EBUSY;
1467c3725bdcSJames Smart 		goto out_fail;
1468c3725bdcSJames Smart 	}
1469c3725bdcSJames Smart 
147001649561SJames Smart 	phba = vport->phba;
147101649561SJames Smart 
14721abcb371SDick Kennedy 	if (vport->load_flag & FC_UNLOADING) {
14731abcb371SDick Kennedy 		ret = -ENODEV;
14741abcb371SDick Kennedy 		goto out_fail;
14751abcb371SDick Kennedy 	}
14761abcb371SDick Kennedy 
14773386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING) {
147844c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
147944c2757bSJames Smart 				 "6124 Fail IO, Driver unload\n");
148044c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
14813386f4bdSJames Smart 		ret = -ENODEV;
14823386f4bdSJames Smart 		goto out_fail;
14833386f4bdSJames Smart 	}
14843386f4bdSJames Smart 
1485c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1486c3725bdcSJames Smart 	if (unlikely(!freqpriv)) {
148744c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
148844c2757bSJames Smart 				 "6158 Fail IO, NULL request data\n");
148944c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1490c3725bdcSJames Smart 		ret = -EINVAL;
1491b7672ae6SDick Kennedy 		goto out_fail;
1492b7672ae6SDick Kennedy 	}
1493b7672ae6SDick Kennedy 
1494bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1495bd2cdd5eSJames Smart 	if (phba->ktime_on)
1496bd2cdd5eSJames Smart 		start = ktime_get_ns();
1497bd2cdd5eSJames Smart #endif
149801649561SJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
149901649561SJames Smart 	lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
150001649561SJames Smart 
150101649561SJames Smart 	/*
150201649561SJames Smart 	 * Catch race where our node has transitioned, but the
150301649561SJames Smart 	 * transport is still transitioning.
150401649561SJames Smart 	 */
150501649561SJames Smart 	ndlp = rport->ndlp;
150601649561SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
150744c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
150844c2757bSJames Smart 				 "6053 Fail IO, ndlp not ready: rport %p "
150944c2757bSJames Smart 				  "ndlp %p, DID x%06x\n",
151001649561SJames Smart 				 rport, ndlp, pnvme_rport->port_id);
151144c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
1512cd240071SJames Smart 		ret = -EBUSY;
151301649561SJames Smart 		goto out_fail;
151401649561SJames Smart 	}
151501649561SJames Smart 
151601649561SJames Smart 	/* The remote node has to be a mapped target or it's an error. */
151701649561SJames Smart 	if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
151801649561SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
151944c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
152044c2757bSJames Smart 				 "6036 Fail IO, DID x%06x not ready for "
1521cd240071SJames Smart 				 "IO. State x%x, Type x%x Flg x%x\n",
152244c2757bSJames Smart 				 pnvme_rport->port_id,
1523cd240071SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type,
1524cd240071SJames Smart 				 ndlp->upcall_flags);
15254b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_bad_ndlp);
1526cd240071SJames Smart 		ret = -EBUSY;
152701649561SJames Smart 		goto out_fail;
152801649561SJames Smart 
152901649561SJames Smart 	}
153001649561SJames Smart 
1531cf1a1d3eSJames Smart 	/* Currently only NVME Keep alive commands should be expedited
1532cf1a1d3eSJames Smart 	 * if the driver runs out of a resource. These should only be
1533cf1a1d3eSJames Smart 	 * issued on the admin queue, qidx 0
1534cf1a1d3eSJames Smart 	 */
1535cf1a1d3eSJames Smart 	if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
1536cf1a1d3eSJames Smart 		sqe = &((struct nvme_fc_cmd_iu *)
1537cf1a1d3eSJames Smart 			pnvme_fcreq->cmdaddr)->sqe.common;
1538cf1a1d3eSJames Smart 		if (sqe->opcode == nvme_admin_keep_alive)
1539cf1a1d3eSJames Smart 			expedite = 1;
1540cf1a1d3eSJames Smart 	}
1541cf1a1d3eSJames Smart 
154201649561SJames Smart 	/* The node is shared with FCP IO, make sure the IO pending count does
154301649561SJames Smart 	 * not exceed the programmed depth.
154401649561SJames Smart 	 */
15452a5b7d62SJames Smart 	if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1546cf1a1d3eSJames Smart 		if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
1547cf1a1d3eSJames Smart 		    !expedite) {
154844c2757bSJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
154944c2757bSJames Smart 					 "6174 Fail IO, ndlp qdepth exceeded: "
15504d5e789aSJames Smart 					 "idx %d DID %x pend %d qdepth %d\n",
15514d5e789aSJames Smart 					 lpfc_queue_info->index, ndlp->nlp_DID,
15524d5e789aSJames Smart 					 atomic_read(&ndlp->cmd_pending),
15534d5e789aSJames Smart 					 ndlp->cmd_qdepth);
15544b056682SJames Smart 			atomic_inc(&lport->xmt_fcp_qdepth);
1555cd22d605SDick Kennedy 			ret = -EBUSY;
155601649561SJames Smart 			goto out_fail;
155701649561SJames Smart 		}
15582a5b7d62SJames Smart 	}
155901649561SJames Smart 
15606a828b0fSJames Smart 	/* Lookup Hardware Queue index based on fcp_io_sched module parameter */
156145aa312eSJames Smart 	if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
156245aa312eSJames Smart 		idx = lpfc_queue_info->index;
156345aa312eSJames Smart 	} else {
1564d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
15656a828b0fSJames Smart 		idx = phba->sli4_hba.cpu_map[cpu].hdwq;
156645aa312eSJames Smart 	}
156745aa312eSJames Smart 
156845aa312eSJames Smart 	lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, idx, expedite);
156901649561SJames Smart 	if (lpfc_ncmd == NULL) {
15704b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_noxri);
157101649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
157244c2757bSJames Smart 				 "6065 Fail IO, driver buffer pool is empty: "
157344c2757bSJames Smart 				 "idx %d DID %x\n",
157444c2757bSJames Smart 				 lpfc_queue_info->index, ndlp->nlp_DID);
1575cd22d605SDick Kennedy 		ret = -EBUSY;
157601649561SJames Smart 		goto out_fail;
157701649561SJames Smart 	}
1578bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1579c8a4ce0bSDick Kennedy 	if (start) {
1580bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_start = start;
1581bd2cdd5eSJames Smart 		lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
1582c8a4ce0bSDick Kennedy 	} else {
1583c8a4ce0bSDick Kennedy 		lpfc_ncmd->ts_cmd_start = 0;
1584bd2cdd5eSJames Smart 	}
1585bd2cdd5eSJames Smart #endif
158601649561SJames Smart 
158701649561SJames Smart 	/*
158801649561SJames Smart 	 * Store the data needed by the driver to issue, abort, and complete
158901649561SJames Smart 	 * an IO.
159001649561SJames Smart 	 * Do not let the IO hang out forever.  There is no midlayer issuing
159101649561SJames Smart 	 * an abort so inform the FW of the maximum IO pending time.
159201649561SJames Smart 	 */
1593bbe3012bSJames Smart 	freqpriv->nvme_buf = lpfc_ncmd;
159401649561SJames Smart 	lpfc_ncmd->nvmeCmd = pnvme_fcreq;
1595318083adSJames Smart 	lpfc_ncmd->ndlp = ndlp;
15960794d601SJames Smart 	lpfc_ncmd->qidx = lpfc_queue_info->qidx;
159701649561SJames Smart 
159801649561SJames Smart 	/*
159901649561SJames Smart 	 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
160001649561SJames Smart 	 * This identfier was create in our hardware queue create callback
160101649561SJames Smart 	 * routine. The driver now is dependent on the IO queue steering from
160201649561SJames Smart 	 * the transport.  We are trusting the upper NVME layers know which
160301649561SJames Smart 	 * index to use and that they have affinitized a CPU to this hardware
160401649561SJames Smart 	 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
160501649561SJames Smart 	 */
160666a210ffSJames Smart 	lpfc_ncmd->cur_iocbq.hba_wqidx = idx;
16074c47efc1SJames Smart 	cstat = &phba->sli4_hba.hdwq[idx].nvme_cstat;
160866a210ffSJames Smart 
160966a210ffSJames Smart 	lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp, cstat);
161066a210ffSJames Smart 	ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
161166a210ffSJames Smart 	if (ret) {
161244c2757bSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
161344c2757bSJames Smart 				 "6175 Fail IO, Prep DMA: "
161444c2757bSJames Smart 				 "idx %d DID %x\n",
161544c2757bSJames Smart 				 lpfc_queue_info->index, ndlp->nlp_DID);
161644c2757bSJames Smart 		atomic_inc(&lport->xmt_fcp_err);
161766a210ffSJames Smart 		ret = -ENOMEM;
161866a210ffSJames Smart 		goto out_free_nvme_buf;
161966a210ffSJames Smart 	}
162066a210ffSJames Smart 
1621bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1622bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
1623bd2cdd5eSJames Smart 			 lpfc_queue_info->index, ndlp->nlp_DID);
1624bd2cdd5eSJames Smart 
16251fbf9742SJames Smart 	ret = lpfc_sli4_issue_wqe(phba, lpfc_ncmd->hdwq, &lpfc_ncmd->cur_iocbq);
162601649561SJames Smart 	if (ret) {
16274b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_wqerr);
1628e3246a12SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
162944c2757bSJames Smart 				 "6113 Fail IO, Could not issue WQE err %x "
163001649561SJames Smart 				 "sid: x%x did: x%x oxid: x%x\n",
163101649561SJames Smart 				 ret, vport->fc_myDID, ndlp->nlp_DID,
163201649561SJames Smart 				 lpfc_ncmd->cur_iocbq.sli4_xritag);
163301649561SJames Smart 		goto out_free_nvme_buf;
163401649561SJames Smart 	}
163501649561SJames Smart 
1636c490850aSJames Smart 	if (phba->cfg_xri_rebalancing)
1637c490850aSJames Smart 		lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_ncmd->hdwq_no);
1638c490850aSJames Smart 
1639bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1640c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start)
1641bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1642bd2cdd5eSJames Smart 
1643bd2cdd5eSJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1644d6d189ceSBart Van Assche 		cpu = raw_smp_processor_id();
164563df6d63SJames Smart 		if (cpu < LPFC_CHECK_CPU_CNT) {
164663df6d63SJames Smart 			lpfc_ncmd->cpu = cpu;
164763df6d63SJames Smart 			if (idx != cpu)
1648bd2cdd5eSJames Smart 				lpfc_printf_vlog(vport,
164963df6d63SJames Smart 						 KERN_INFO, LOG_NVME_IOERR,
1650bd2cdd5eSJames Smart 						"6702 CPU Check cmd: "
1651bd2cdd5eSJames Smart 						"cpu %d wq %d\n",
1652bd2cdd5eSJames Smart 						lpfc_ncmd->cpu,
1653bd2cdd5eSJames Smart 						lpfc_queue_info->index);
165463df6d63SJames Smart 			phba->sli4_hba.hdwq[idx].cpucheck_xmt_io[cpu]++;
1655bd2cdd5eSJames Smart 		}
1656bd2cdd5eSJames Smart 	}
1657bd2cdd5eSJames Smart #endif
165801649561SJames Smart 	return 0;
165901649561SJames Smart 
166001649561SJames Smart  out_free_nvme_buf:
16612cee7808SJames Smart 	if (lpfc_ncmd->nvmeCmd->sg_cnt) {
16622cee7808SJames Smart 		if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
16634c47efc1SJames Smart 			cstat->output_requests--;
16642cee7808SJames Smart 		else
16654c47efc1SJames Smart 			cstat->input_requests--;
16662cee7808SJames Smart 	} else
16674c47efc1SJames Smart 		cstat->control_requests--;
166801649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
166901649561SJames Smart  out_fail:
167001649561SJames Smart 	return ret;
167101649561SJames Smart }
167201649561SJames Smart 
167301649561SJames Smart /**
167401649561SJames Smart  * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
167501649561SJames Smart  * @phba: Pointer to HBA context object
167601649561SJames Smart  * @cmdiocb: Pointer to command iocb object.
167701649561SJames Smart  * @rspiocb: Pointer to response iocb object.
167801649561SJames Smart  *
167901649561SJames Smart  * This is the callback function for any NVME FCP IO that was aborted.
168001649561SJames Smart  *
168101649561SJames Smart  * Return value:
168201649561SJames Smart  *   None
168301649561SJames Smart  **/
168401649561SJames Smart void
168501649561SJames Smart lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
168601649561SJames Smart 			   struct lpfc_wcqe_complete *abts_cmpl)
168701649561SJames Smart {
1688e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
168901649561SJames Smart 			"6145 ABORT_XRI_CN completing on rpi x%x "
169001649561SJames Smart 			"original iotag x%x, abort cmd iotag x%x "
169101649561SJames Smart 			"req_tag x%x, status x%x, hwstatus x%x\n",
169201649561SJames Smart 			cmdiocb->iocb.un.acxri.abortContextTag,
169301649561SJames Smart 			cmdiocb->iocb.un.acxri.abortIoTag,
169401649561SJames Smart 			cmdiocb->iotag,
169501649561SJames Smart 			bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
169601649561SJames Smart 			bf_get(lpfc_wcqe_c_status, abts_cmpl),
169701649561SJames Smart 			bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
169801649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocb);
169901649561SJames Smart }
170001649561SJames Smart 
170101649561SJames Smart /**
170201649561SJames Smart  * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
170301649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
170401649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
170501649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
170601649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
170701649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
170801649561SJames Smart  *
170901649561SJames Smart  * Driver registers this routine as its nvme request io abort handler.  This
171001649561SJames Smart  * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
171101649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.  This routine
171201649561SJames Smart  * is executed asynchronously - one the target is validated as "MAPPED" and
171301649561SJames Smart  * ready for IO, the driver issues the abort request and returns.
171401649561SJames Smart  *
171501649561SJames Smart  * Return value:
171601649561SJames Smart  *   None
171701649561SJames Smart  **/
171801649561SJames Smart static void
171901649561SJames Smart lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
172001649561SJames Smart 		    struct nvme_fc_remote_port *pnvme_rport,
172101649561SJames Smart 		    void *hw_queue_handle,
172201649561SJames Smart 		    struct nvmefc_fcp_req *pnvme_fcreq)
172301649561SJames Smart {
172401649561SJames Smart 	struct lpfc_nvme_lport *lport;
172501649561SJames Smart 	struct lpfc_vport *vport;
172601649561SJames Smart 	struct lpfc_hba *phba;
1727c490850aSJames Smart 	struct lpfc_io_buf *lpfc_nbuf;
172801649561SJames Smart 	struct lpfc_iocbq *abts_buf;
172901649561SJames Smart 	struct lpfc_iocbq *nvmereq_wqe;
1730c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
1731205e8240SJames Smart 	union lpfc_wqe128 *abts_wqe;
173201649561SJames Smart 	unsigned long flags;
173301649561SJames Smart 	int ret_val;
173401649561SJames Smart 
1735c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1736c3725bdcSJames Smart 	 * have timing races.
1737c3725bdcSJames Smart 	 */
173801649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1739c3725bdcSJames Smart 	if (unlikely(!lport))
1740c3725bdcSJames Smart 		return;
174101649561SJames Smart 
1742c3725bdcSJames Smart 	vport = lport->vport;
1743c3725bdcSJames Smart 
1744c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
1745c3725bdcSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1746c3725bdcSJames Smart 				 "6129 Fail Abort, HW Queue Handle NULL.\n");
1747c3725bdcSJames Smart 		return;
1748c3725bdcSJames Smart 	}
1749c3725bdcSJames Smart 
1750c3725bdcSJames Smart 	phba = vport->phba;
1751c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1752c3725bdcSJames Smart 
1753c3725bdcSJames Smart 	if (unlikely(!freqpriv))
1754c3725bdcSJames Smart 		return;
17553386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
17563386f4bdSJames Smart 		return;
17573386f4bdSJames Smart 
175801649561SJames Smart 	/* Announce entry to new IO submit field. */
175986c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
176001649561SJames Smart 			 "6002 Abort Request to rport DID x%06x "
176101649561SJames Smart 			 "for nvme_fc_req %p\n",
176201649561SJames Smart 			 pnvme_rport->port_id,
176301649561SJames Smart 			 pnvme_fcreq);
176401649561SJames Smart 
176501649561SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
176601649561SJames Smart 	 * cleared when the reset is complete and rings reestablished.
176701649561SJames Smart 	 */
176801649561SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
176901649561SJames Smart 	/* driver queued commands are in process of being flushed */
177001649561SJames Smart 	if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
177101649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
177286c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
177301649561SJames Smart 				 "6139 Driver in reset cleanup - flushing "
177401649561SJames Smart 				 "NVME Req now.  hba_flag x%x\n",
177501649561SJames Smart 				 phba->hba_flag);
177601649561SJames Smart 		return;
177701649561SJames Smart 	}
177801649561SJames Smart 
1779bbe3012bSJames Smart 	lpfc_nbuf = freqpriv->nvme_buf;
178001649561SJames Smart 	if (!lpfc_nbuf) {
178101649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
178286c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
178301649561SJames Smart 				 "6140 NVME IO req has no matching lpfc nvme "
178401649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
178501649561SJames Smart 		return;
178601649561SJames Smart 	} else if (!lpfc_nbuf->nvmeCmd) {
178701649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
178886c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
178901649561SJames Smart 				 "6141 lpfc NVME IO req has no nvme_fcreq "
179001649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
179101649561SJames Smart 		return;
179201649561SJames Smart 	}
17932b7824d0SJames Smart 	nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
179401649561SJames Smart 
1795c2017260SJames Smart 	/* Guard against IO completion being called at same time */
1796c2017260SJames Smart 	spin_lock(&lpfc_nbuf->buf_lock);
1797c2017260SJames Smart 
179801649561SJames Smart 	/*
179901649561SJames Smart 	 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
180001649561SJames Smart 	 * state must match the nvme_fcreq passed by the nvme
180101649561SJames Smart 	 * transport.  If they don't match, it is likely the driver
180201649561SJames Smart 	 * has already completed the NVME IO and the nvme transport
180301649561SJames Smart 	 * has not seen it yet.
180401649561SJames Smart 	 */
180501649561SJames Smart 	if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
180686c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
180701649561SJames Smart 				 "6143 NVME req mismatch: "
180801649561SJames Smart 				 "lpfc_nbuf %p nvmeCmd %p, "
18092b7824d0SJames Smart 				 "pnvme_fcreq %p.  Skipping Abort xri x%x\n",
181001649561SJames Smart 				 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
18112b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1812c2017260SJames Smart 		goto out_unlock;
181301649561SJames Smart 	}
181401649561SJames Smart 
181501649561SJames Smart 	/* Don't abort IOs no longer on the pending queue. */
181601649561SJames Smart 	if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
181786c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
181801649561SJames Smart 				 "6142 NVME IO req %p not queued - skipping "
18192b7824d0SJames Smart 				 "abort req xri x%x\n",
18202b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1821c2017260SJames Smart 		goto out_unlock;
182201649561SJames Smart 	}
182301649561SJames Smart 
18244b056682SJames Smart 	atomic_inc(&lport->xmt_fcp_abort);
1825bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1826bd2cdd5eSJames Smart 			 nvmereq_wqe->sli4_xritag,
182700cefeb9SJames Smart 			 nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
1828bd2cdd5eSJames Smart 
182901649561SJames Smart 	/* Outstanding abort is in progress */
183001649561SJames Smart 	if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
183186c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
183201649561SJames Smart 				 "6144 Outstanding NVME I/O Abort Request "
183301649561SJames Smart 				 "still pending on nvme_fcreq %p, "
18342b7824d0SJames Smart 				 "lpfc_ncmd %p xri x%x\n",
18352b7824d0SJames Smart 				 pnvme_fcreq, lpfc_nbuf,
18362b7824d0SJames Smart 				 nvmereq_wqe->sli4_xritag);
1837c2017260SJames Smart 		goto out_unlock;
183801649561SJames Smart 	}
183901649561SJames Smart 
184001649561SJames Smart 	abts_buf = __lpfc_sli_get_iocbq(phba);
184101649561SJames Smart 	if (!abts_buf) {
184286c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
184301649561SJames Smart 				 "6136 No available abort wqes. Skipping "
18442b7824d0SJames Smart 				 "Abts req for nvme_fcreq %p xri x%x\n",
18452b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1846c2017260SJames Smart 		goto out_unlock;
184701649561SJames Smart 	}
184801649561SJames Smart 
184901649561SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
185001649561SJames Smart 	nvmereq_wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
185101649561SJames Smart 
185201649561SJames Smart 	/* Complete prepping the abort wqe and issue to the FW. */
185301649561SJames Smart 	abts_wqe = &abts_buf->wqe;
185401649561SJames Smart 
185501649561SJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
185601649561SJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
185701649561SJames Smart 	 */
185801649561SJames Smart 	memset(abts_wqe, 0, sizeof(union lpfc_wqe));
185901649561SJames Smart 	bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
186001649561SJames Smart 
186101649561SJames Smart 	/* word 7 */
186201649561SJames Smart 	bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
186301649561SJames Smart 	bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
186401649561SJames Smart 	       nvmereq_wqe->iocb.ulpClass);
186501649561SJames Smart 
186601649561SJames Smart 	/* word 8 - tell the FW to abort the IO associated with this
186701649561SJames Smart 	 * outstanding exchange ID.
186801649561SJames Smart 	 */
186901649561SJames Smart 	abts_wqe->abort_cmd.wqe_com.abort_tag = nvmereq_wqe->sli4_xritag;
187001649561SJames Smart 
187101649561SJames Smart 	/* word 9 - this is the iotag for the abts_wqe completion. */
187201649561SJames Smart 	bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
187301649561SJames Smart 	       abts_buf->iotag);
187401649561SJames Smart 
187501649561SJames Smart 	/* word 10 */
187601649561SJames Smart 	bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
187701649561SJames Smart 	bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
187801649561SJames Smart 
187901649561SJames Smart 	/* word 11 */
188001649561SJames Smart 	bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
188101649561SJames Smart 	bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
188201649561SJames Smart 	bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
188301649561SJames Smart 
188401649561SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
188501649561SJames Smart 	abts_buf->iocb_flag |= LPFC_IO_NVME;
188601649561SJames Smart 	abts_buf->hba_wqidx = nvmereq_wqe->hba_wqidx;
188701649561SJames Smart 	abts_buf->vport = vport;
188801649561SJames Smart 	abts_buf->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
18891fbf9742SJames Smart 	ret_val = lpfc_sli4_issue_wqe(phba, lpfc_nbuf->hdwq, abts_buf);
1890c2017260SJames Smart 	spin_unlock(&lpfc_nbuf->buf_lock);
189101649561SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1892cd22d605SDick Kennedy 	if (ret_val) {
189386c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
189401649561SJames Smart 				 "6137 Failed abts issue_wqe with status x%x "
189501649561SJames Smart 				 "for nvme_fcreq %p.\n",
189601649561SJames Smart 				 ret_val, pnvme_fcreq);
189701649561SJames Smart 		lpfc_sli_release_iocbq(phba, abts_buf);
189801649561SJames Smart 		return;
189901649561SJames Smart 	}
190001649561SJames Smart 
190186c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
19022b7824d0SJames Smart 			 "6138 Transport Abort NVME Request Issued for "
190301649561SJames Smart 			 "ox_id x%x on reqtag x%x\n",
190401649561SJames Smart 			 nvmereq_wqe->sli4_xritag,
190501649561SJames Smart 			 abts_buf->iotag);
1906c2017260SJames Smart 	return;
1907c2017260SJames Smart 
1908c2017260SJames Smart out_unlock:
1909c2017260SJames Smart 	spin_unlock(&lpfc_nbuf->buf_lock);
1910c2017260SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1911c2017260SJames Smart 	return;
191201649561SJames Smart }
191301649561SJames Smart 
191401649561SJames Smart /* Declare and initialization an instance of the FC NVME template. */
191501649561SJames Smart static struct nvme_fc_port_template lpfc_nvme_template = {
191601649561SJames Smart 	/* initiator-based functions */
191701649561SJames Smart 	.localport_delete  = lpfc_nvme_localport_delete,
191801649561SJames Smart 	.remoteport_delete = lpfc_nvme_remoteport_delete,
191901649561SJames Smart 	.create_queue = lpfc_nvme_create_queue,
192001649561SJames Smart 	.delete_queue = lpfc_nvme_delete_queue,
192101649561SJames Smart 	.ls_req       = lpfc_nvme_ls_req,
192201649561SJames Smart 	.fcp_io       = lpfc_nvme_fcp_io_submit,
192301649561SJames Smart 	.ls_abort     = lpfc_nvme_ls_abort,
192401649561SJames Smart 	.fcp_abort    = lpfc_nvme_fcp_abort,
192501649561SJames Smart 
192601649561SJames Smart 	.max_hw_queues = 1,
192701649561SJames Smart 	.max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
192801649561SJames Smart 	.max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
192901649561SJames Smart 	.dma_boundary = 0xFFFFFFFF,
193001649561SJames Smart 
193101649561SJames Smart 	/* Sizes of additional private data for data structures.
193201649561SJames Smart 	 * No use for the last two sizes at this time.
193301649561SJames Smart 	 */
193401649561SJames Smart 	.local_priv_sz = sizeof(struct lpfc_nvme_lport),
193501649561SJames Smart 	.remote_priv_sz = sizeof(struct lpfc_nvme_rport),
193601649561SJames Smart 	.lsrqst_priv_sz = 0,
1937bbe3012bSJames Smart 	.fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
193801649561SJames Smart };
193901649561SJames Smart 
194001649561SJames Smart /**
19415e5b511dSJames Smart  * lpfc_get_nvme_buf - Get a nvme buffer from io_buf_list of the HBA
194201649561SJames Smart  * @phba: The HBA for which this call is being executed.
194301649561SJames Smart  *
19445e5b511dSJames Smart  * This routine removes a nvme buffer from head of @hdwq io_buf_list
194501649561SJames Smart  * and returns to caller.
194601649561SJames Smart  *
194701649561SJames Smart  * Return codes:
194801649561SJames Smart  *   NULL - Error
194901649561SJames Smart  *   Pointer to lpfc_nvme_buf - Success
195001649561SJames Smart  **/
1951c490850aSJames Smart static struct lpfc_io_buf *
1952cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19535e5b511dSJames Smart 		  int idx, int expedite)
195401649561SJames Smart {
1955c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
19565e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
19570794d601SJames Smart 	struct sli4_sge *sgl;
19580794d601SJames Smart 	struct lpfc_iocbq *pwqeq;
19590794d601SJames Smart 	union lpfc_wqe128 *wqe;
196001649561SJames Smart 
1961c490850aSJames Smart 	lpfc_ncmd = lpfc_get_io_buf(phba, NULL, idx, expedite);
19622a5b7d62SJames Smart 
19630794d601SJames Smart 	if (lpfc_ncmd) {
19640794d601SJames Smart 		pwqeq = &(lpfc_ncmd->cur_iocbq);
19650794d601SJames Smart 		wqe = &pwqeq->wqe;
19660794d601SJames Smart 
19670794d601SJames Smart 		/* Setup key fields in buffer that may have been changed
19680794d601SJames Smart 		 * if other protocols used this buffer.
19690794d601SJames Smart 		 */
19700794d601SJames Smart 		pwqeq->iocb_flag = LPFC_IO_NVME;
19710794d601SJames Smart 		pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
19720794d601SJames Smart 		lpfc_ncmd->start_time = jiffies;
19730794d601SJames Smart 		lpfc_ncmd->flags = 0;
19740794d601SJames Smart 
19750794d601SJames Smart 		/* Rsp SGE will be filled in when we rcv an IO
19760794d601SJames Smart 		 * from the NVME Layer to be sent.
19770794d601SJames Smart 		 * The cmd is going to be embedded so we need a SKIP SGE.
19780794d601SJames Smart 		 */
19790794d601SJames Smart 		sgl = lpfc_ncmd->dma_sgl;
19800794d601SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
19810794d601SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
19820794d601SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
19830794d601SJames Smart 		/* Fill in word 3 / sgl_len during cmd submission */
19840794d601SJames Smart 
19850794d601SJames Smart 		/* Initialize WQE */
19860794d601SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
19870794d601SJames Smart 
19880794d601SJames Smart 		if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
19892a5b7d62SJames Smart 			atomic_inc(&ndlp->cmd_pending);
1990c490850aSJames Smart 			lpfc_ncmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
19912a5b7d62SJames Smart 		}
19925e5b511dSJames Smart 
1993c490850aSJames Smart 	} else {
1994c490850aSJames Smart 		qp = &phba->sli4_hba.hdwq[idx];
19955e5b511dSJames Smart 		qp->empty_io_bufs++;
1996c490850aSJames Smart 	}
19975e5b511dSJames Smart 
199801649561SJames Smart 	return  lpfc_ncmd;
199901649561SJames Smart }
200001649561SJames Smart 
200101649561SJames Smart /**
200201649561SJames Smart  * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
200301649561SJames Smart  * @phba: The Hba for which this call is being executed.
200401649561SJames Smart  * @lpfc_ncmd: The nvme buffer which is being released.
200501649561SJames Smart  *
200601649561SJames Smart  * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
20075e5b511dSJames Smart  * lpfc_io_buf_list list. For SLI4 XRI's are tied to the nvme buffer
200801649561SJames Smart  * and cannot be reused for at least RA_TOV amount of time if it was
200901649561SJames Smart  * aborted.
201001649561SJames Smart  **/
201101649561SJames Smart static void
2012c490850aSJames Smart lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd)
201301649561SJames Smart {
20145e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
201501649561SJames Smart 	unsigned long iflag = 0;
201601649561SJames Smart 
2017c490850aSJames Smart 	if ((lpfc_ncmd->flags & LPFC_SBUF_BUMP_QDEPTH) && lpfc_ncmd->ndlp)
20182a5b7d62SJames Smart 		atomic_dec(&lpfc_ncmd->ndlp->cmd_pending);
20192a5b7d62SJames Smart 
20202a5b7d62SJames Smart 	lpfc_ncmd->ndlp = NULL;
2021c490850aSJames Smart 	lpfc_ncmd->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
20222a5b7d62SJames Smart 
20231fbf9742SJames Smart 	qp = lpfc_ncmd->hdwq;
2024318083adSJames Smart 	if (lpfc_ncmd->flags & LPFC_SBUF_XBUSY) {
202586c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
202686c67379SJames Smart 				"6310 XB release deferred for "
202786c67379SJames Smart 				"ox_id x%x on reqtag x%x\n",
202886c67379SJames Smart 				lpfc_ncmd->cur_iocbq.sli4_xritag,
202986c67379SJames Smart 				lpfc_ncmd->cur_iocbq.iotag);
203086c67379SJames Smart 
20315e5b511dSJames Smart 		spin_lock_irqsave(&qp->abts_nvme_buf_list_lock, iflag);
203201649561SJames Smart 		list_add_tail(&lpfc_ncmd->list,
20335e5b511dSJames Smart 			&qp->lpfc_abts_nvme_buf_list);
20345e5b511dSJames Smart 		qp->abts_nvme_io_bufs++;
20355e5b511dSJames Smart 		spin_unlock_irqrestore(&qp->abts_nvme_buf_list_lock, iflag);
2036c490850aSJames Smart 	} else
2037c490850aSJames Smart 		lpfc_release_io_buf(phba, (struct lpfc_io_buf *)lpfc_ncmd, qp);
203801649561SJames Smart }
203901649561SJames Smart 
204001649561SJames Smart /**
204101649561SJames Smart  * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
204201649561SJames Smart  * @pvport - the lpfc_vport instance requesting a localport.
204301649561SJames Smart  *
204401649561SJames Smart  * This routine is invoked to create an nvme localport instance to bind
204501649561SJames Smart  * to the nvme_fc_transport.  It is called once during driver load
204601649561SJames Smart  * like lpfc_create_shost after all other services are initialized.
204701649561SJames Smart  * It requires a vport, vpi, and wwns at call time.  Other localport
204801649561SJames Smart  * parameters are modified as the driver's FCID and the Fabric WWN
204901649561SJames Smart  * are established.
205001649561SJames Smart  *
205101649561SJames Smart  * Return codes
205201649561SJames Smart  *      0 - successful
205301649561SJames Smart  *      -ENOMEM - no heap memory available
205401649561SJames Smart  *      other values - from nvme registration upcall
205501649561SJames Smart  **/
205601649561SJames Smart int
205701649561SJames Smart lpfc_nvme_create_localport(struct lpfc_vport *vport)
205801649561SJames Smart {
2059166d7211SJames Smart 	int ret = 0;
206001649561SJames Smart 	struct lpfc_hba  *phba = vport->phba;
206101649561SJames Smart 	struct nvme_fc_port_info nfcp_info;
206201649561SJames Smart 	struct nvme_fc_local_port *localport;
206301649561SJames Smart 	struct lpfc_nvme_lport *lport;
206401649561SJames Smart 
206501649561SJames Smart 	/* Initialize this localport instance.  The vport wwn usage ensures
206601649561SJames Smart 	 * that NPIV is accounted for.
206701649561SJames Smart 	 */
206801649561SJames Smart 	memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
206901649561SJames Smart 	nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
207001649561SJames Smart 	nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
207101649561SJames Smart 	nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
207201649561SJames Smart 
20735b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
20745b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
20755b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
20764d4c4a4aSJames Smart 	 */
20774d4c4a4aSJames Smart 	lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
207801649561SJames Smart 
20796a828b0fSJames Smart 	/* Advertise how many hw queues we support based on fcp_io_sched */
20806a828b0fSJames Smart 	if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ)
2081cdb42becSJames Smart 		lpfc_nvme_template.max_hw_queues = phba->cfg_hdw_queue;
20826a828b0fSJames Smart 	else
20836a828b0fSJames Smart 		lpfc_nvme_template.max_hw_queues =
20846a828b0fSJames Smart 			phba->sli4_hba.num_present_cpu;
208566a210ffSJames Smart 
2086faf5a744SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_FC))
2087faf5a744SArnd Bergmann 		return ret;
2088faf5a744SArnd Bergmann 
208901649561SJames Smart 	/* localport is allocated from the stack, but the registration
209001649561SJames Smart 	 * call allocates heap memory as well as the private area.
209101649561SJames Smart 	 */
2092faf5a744SArnd Bergmann 
209301649561SJames Smart 	ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
209401649561SJames Smart 					 &vport->phba->pcidev->dev, &localport);
209501649561SJames Smart 	if (!ret) {
209601649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
209701649561SJames Smart 				 "6005 Successfully registered local "
209801649561SJames Smart 				 "NVME port num %d, localP %p, private %p, "
209901649561SJames Smart 				 "sg_seg %d\n",
210001649561SJames Smart 				 localport->port_num, localport,
210101649561SJames Smart 				 localport->private,
210201649561SJames Smart 				 lpfc_nvme_template.max_sgl_segments);
210301649561SJames Smart 
210401649561SJames Smart 		/* Private is our lport size declared in the template. */
210501649561SJames Smart 		lport = (struct lpfc_nvme_lport *)localport->private;
210601649561SJames Smart 		vport->localport = localport;
210701649561SJames Smart 		lport->vport = vport;
210801649561SJames Smart 		vport->nvmei_support = 1;
21096b486ce9SDick Kennedy 
21104b056682SJames Smart 		atomic_set(&lport->xmt_fcp_noxri, 0);
21114b056682SJames Smart 		atomic_set(&lport->xmt_fcp_bad_ndlp, 0);
21124b056682SJames Smart 		atomic_set(&lport->xmt_fcp_qdepth, 0);
211344c2757bSJames Smart 		atomic_set(&lport->xmt_fcp_err, 0);
21144b056682SJames Smart 		atomic_set(&lport->xmt_fcp_wqerr, 0);
21154b056682SJames Smart 		atomic_set(&lport->xmt_fcp_abort, 0);
21164b056682SJames Smart 		atomic_set(&lport->xmt_ls_abort, 0);
21174b056682SJames Smart 		atomic_set(&lport->xmt_ls_err, 0);
21184b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_xb, 0);
21194b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_err, 0);
21204b056682SJames Smart 		atomic_set(&lport->cmpl_ls_xb, 0);
21214b056682SJames Smart 		atomic_set(&lport->cmpl_ls_err, 0);
212266a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsRequests, 0);
212366a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsCmpls, 0);
21246b486ce9SDick Kennedy 	}
21253ebd9b47SJames Smart 
212601649561SJames Smart 	return ret;
212701649561SJames Smart }
212801649561SJames Smart 
2129d964b3e5SBart Van Assche #if (IS_ENABLED(CONFIG_NVME_FC))
2130add9d6beSJames Smart /* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2131add9d6beSJames Smart  *
2132add9d6beSJames Smart  * The driver has to wait for the host nvme transport to callback
2133add9d6beSJames Smart  * indicating the localport has successfully unregistered all
2134add9d6beSJames Smart  * resources.  Since this is an uninterruptible wait, loop every ten
2135add9d6beSJames Smart  * seconds and print a message indicating no progress.
2136add9d6beSJames Smart  *
2137add9d6beSJames Smart  * An uninterruptible wait is used because of the risk of transport-to-
2138add9d6beSJames Smart  * driver state mismatch.
2139add9d6beSJames Smart  */
21403999df75SBart Van Assche static void
2141add9d6beSJames Smart lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
21427961cba6SEwan D. Milne 			   struct lpfc_nvme_lport *lport,
21437961cba6SEwan D. Milne 			   struct completion *lport_unreg_cmp)
2144add9d6beSJames Smart {
2145add9d6beSJames Smart 	u32 wait_tmo;
21462ab70c21SJames Smart 	int ret, i, pending = 0;
21472ab70c21SJames Smart 	struct lpfc_sli_ring  *pring;
21482ab70c21SJames Smart 	struct lpfc_hba  *phba = vport->phba;
2149add9d6beSJames Smart 
2150add9d6beSJames Smart 	/* Host transport has to clean up and confirm requiring an indefinite
2151add9d6beSJames Smart 	 * wait. Print a message if a 10 second wait expires and renew the
2152add9d6beSJames Smart 	 * wait. This is unexpected.
2153add9d6beSJames Smart 	 */
2154add9d6beSJames Smart 	wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2155add9d6beSJames Smart 	while (true) {
21567961cba6SEwan D. Milne 		ret = wait_for_completion_timeout(lport_unreg_cmp, wait_tmo);
2157add9d6beSJames Smart 		if (unlikely(!ret)) {
21582ab70c21SJames Smart 			pending = 0;
21592ab70c21SJames Smart 			for (i = 0; i < phba->cfg_hdw_queue; i++) {
21602ab70c21SJames Smart 				pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
21612ab70c21SJames Smart 				if (!pring)
21622ab70c21SJames Smart 					continue;
21632ab70c21SJames Smart 				if (pring->txcmplq_cnt)
21642ab70c21SJames Smart 					pending += pring->txcmplq_cnt;
21652ab70c21SJames Smart 			}
2166add9d6beSJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
2167add9d6beSJames Smart 					 "6176 Lport %p Localport %p wait "
21682ab70c21SJames Smart 					 "timed out. Pending %d. Renewing.\n",
21692ab70c21SJames Smart 					 lport, vport->localport, pending);
2170add9d6beSJames Smart 			continue;
2171add9d6beSJames Smart 		}
2172add9d6beSJames Smart 		break;
2173add9d6beSJames Smart 	}
2174add9d6beSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
2175add9d6beSJames Smart 			 "6177 Lport %p Localport %p Complete Success\n",
2176add9d6beSJames Smart 			 lport, vport->localport);
2177add9d6beSJames Smart }
2178d964b3e5SBart Van Assche #endif
2179add9d6beSJames Smart 
218001649561SJames Smart /**
218101649561SJames Smart  * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
218201649561SJames Smart  * @pnvme: pointer to lpfc nvme data structure.
218301649561SJames Smart  *
218401649561SJames Smart  * This routine is invoked to destroy all lports bound to the phba.
218501649561SJames Smart  * The lport memory was allocated by the nvme fc transport and is
218601649561SJames Smart  * released there.  This routine ensures all rports bound to the
218701649561SJames Smart  * lport have been disconnected.
218801649561SJames Smart  *
218901649561SJames Smart  **/
219001649561SJames Smart void
219101649561SJames Smart lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
219201649561SJames Smart {
21937d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
219401649561SJames Smart 	struct nvme_fc_local_port *localport;
219501649561SJames Smart 	struct lpfc_nvme_lport *lport;
219601649561SJames Smart 	int ret;
21977961cba6SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(lport_unreg_cmp);
219801649561SJames Smart 
219901649561SJames Smart 	if (vport->nvmei_support == 0)
220001649561SJames Smart 		return;
220101649561SJames Smart 
220201649561SJames Smart 	localport = vport->localport;
220301649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
220401649561SJames Smart 
220501649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
220601649561SJames Smart 			 "6011 Destroying NVME localport %p\n",
220701649561SJames Smart 			 localport);
2208166d7211SJames Smart 
220901649561SJames Smart 	/* lport's rport list is clear.  Unregister
221001649561SJames Smart 	 * lport and release resources.
221101649561SJames Smart 	 */
22127961cba6SEwan D. Milne 	lport->lport_unreg_cmp = &lport_unreg_cmp;
221301649561SJames Smart 	ret = nvme_fc_unregister_localport(localport);
2214add9d6beSJames Smart 
2215add9d6beSJames Smart 	/* Wait for completion.  This either blocks
2216add9d6beSJames Smart 	 * indefinitely or succeeds
2217add9d6beSJames Smart 	 */
22187961cba6SEwan D. Milne 	lpfc_nvme_lport_unreg_wait(vport, lport, &lport_unreg_cmp);
22197961cba6SEwan D. Milne 	vport->localport = NULL;
222001649561SJames Smart 
222101649561SJames Smart 	/* Regardless of the unregister upcall response, clear
222201649561SJames Smart 	 * nvmei_support.  All rports are unregistered and the
222301649561SJames Smart 	 * driver will clean up.
222401649561SJames Smart 	 */
222501649561SJames Smart 	vport->nvmei_support = 0;
222601649561SJames Smart 	if (ret == 0) {
222701649561SJames Smart 		lpfc_printf_vlog(vport,
222801649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
222901649561SJames Smart 				 "6009 Unregistered lport Success\n");
223001649561SJames Smart 	} else {
223101649561SJames Smart 		lpfc_printf_vlog(vport,
223201649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
223301649561SJames Smart 				 "6010 Unregistered lport "
223401649561SJames Smart 				 "Failed, status x%x\n",
223501649561SJames Smart 				 ret);
223601649561SJames Smart 	}
2237166d7211SJames Smart #endif
223801649561SJames Smart }
223901649561SJames Smart 
224001649561SJames Smart void
224101649561SJames Smart lpfc_nvme_update_localport(struct lpfc_vport *vport)
224201649561SJames Smart {
22434410a67aSJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
224401649561SJames Smart 	struct nvme_fc_local_port *localport;
224501649561SJames Smart 	struct lpfc_nvme_lport *lport;
224601649561SJames Smart 
224701649561SJames Smart 	localport = vport->localport;
22484410a67aSJames Smart 	if (!localport) {
22494410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
22504410a67aSJames Smart 				 "6710 Update NVME fail. No localport\n");
22514410a67aSJames Smart 		return;
22524410a67aSJames Smart 	}
225301649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
22544410a67aSJames Smart 	if (!lport) {
22554410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
22564410a67aSJames Smart 				 "6171 Update NVME fail. localP %p, No lport\n",
22574410a67aSJames Smart 				 localport);
22584410a67aSJames Smart 		return;
22594410a67aSJames Smart 	}
226001649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
226101649561SJames Smart 			 "6012 Update NVME lport %p did x%x\n",
226201649561SJames Smart 			 localport, vport->fc_myDID);
226301649561SJames Smart 
226401649561SJames Smart 	localport->port_id = vport->fc_myDID;
226501649561SJames Smart 	if (localport->port_id == 0)
226601649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
226701649561SJames Smart 	else
226801649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
226901649561SJames Smart 
227001649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
227101649561SJames Smart 			 "6030 bound lport %p to DID x%06x\n",
227201649561SJames Smart 			 lport, localport->port_id);
22734410a67aSJames Smart #endif
227401649561SJames Smart }
227501649561SJames Smart 
227601649561SJames Smart int
227701649561SJames Smart lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
227801649561SJames Smart {
22797d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
228001649561SJames Smart 	int ret = 0;
228101649561SJames Smart 	struct nvme_fc_local_port *localport;
228201649561SJames Smart 	struct lpfc_nvme_lport *lport;
228301649561SJames Smart 	struct lpfc_nvme_rport *rport;
228401466024SJames Smart 	struct lpfc_nvme_rport *oldrport;
228501649561SJames Smart 	struct nvme_fc_remote_port *remote_port;
228601649561SJames Smart 	struct nvme_fc_port_info rpinfo;
228793a3922dSJames Smart 	struct lpfc_nodelist *prev_ndlp = NULL;
228801649561SJames Smart 
228901649561SJames Smart 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
229001649561SJames Smart 			 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
229101649561SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type);
229201649561SJames Smart 
229301649561SJames Smart 	localport = vport->localport;
2294bb6a8a2cSDick Kennedy 	if (!localport)
2295bb6a8a2cSDick Kennedy 		return 0;
2296bb6a8a2cSDick Kennedy 
229701649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
229801649561SJames Smart 
22997a06dcd3SJames Smart 	/* NVME rports are not preserved across devloss.
23007a06dcd3SJames Smart 	 * Just register this instance.  Note, rpinfo->dev_loss_tmo
23017a06dcd3SJames Smart 	 * is left 0 to indicate accept transport defaults.  The
23027a06dcd3SJames Smart 	 * driver communicates port role capabilities consistent
23037a06dcd3SJames Smart 	 * with the PRLI response data.
230401649561SJames Smart 	 */
23057a06dcd3SJames Smart 	memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
23067a06dcd3SJames Smart 	rpinfo.port_id = ndlp->nlp_DID;
230701649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET)
23087a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
230901649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
23107a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
231101649561SJames Smart 
23127a06dcd3SJames Smart 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
23137a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
23147a06dcd3SJames Smart 
23157a06dcd3SJames Smart 	rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
23167a06dcd3SJames Smart 	rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
231701466024SJames Smart 
23189e210178SJames Smart 	spin_lock_irq(&vport->phba->hbalock);
231901466024SJames Smart 	oldrport = lpfc_ndlp_get_nrport(ndlp);
232061184f17SJames Smart 	if (oldrport) {
232161184f17SJames Smart 		prev_ndlp = oldrport->ndlp;
23229e210178SJames Smart 		spin_unlock_irq(&vport->phba->hbalock);
232361184f17SJames Smart 	} else {
232461184f17SJames Smart 		spin_unlock_irq(&vport->phba->hbalock);
23253fd78355SJames Smart 		lpfc_nlp_get(ndlp);
232661184f17SJames Smart 	}
23273fd78355SJames Smart 
23287a06dcd3SJames Smart 	ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
23297a06dcd3SJames Smart 	if (!ret) {
23307a06dcd3SJames Smart 		/* If the ndlp already has an nrport, this is just
23317a06dcd3SJames Smart 		 * a resume of the existing rport.  Else this is a
23327a06dcd3SJames Smart 		 * new rport.
23337a06dcd3SJames Smart 		 */
2334b15bd3e6SJames Smart 		/* Guard against an unregister/reregister
2335b15bd3e6SJames Smart 		 * race that leaves the WAIT flag set.
2336b15bd3e6SJames Smart 		 */
2337b15bd3e6SJames Smart 		spin_lock_irq(&vport->phba->hbalock);
2338b15bd3e6SJames Smart 		ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
2339b15bd3e6SJames Smart 		spin_unlock_irq(&vport->phba->hbalock);
23407a06dcd3SJames Smart 		rport = remote_port->private;
234101466024SJames Smart 		if (oldrport) {
234293a3922dSJames Smart 			/* New remoteport record does not guarantee valid
234393a3922dSJames Smart 			 * host private memory area.
234493a3922dSJames Smart 			 */
234501466024SJames Smart 			if (oldrport == remote_port->private) {
234693a3922dSJames Smart 				/* Same remoteport - ndlp should match.
234793a3922dSJames Smart 				 * Just reuse.
234893a3922dSJames Smart 				 */
234901649561SJames Smart 				lpfc_printf_vlog(ndlp->vport, KERN_INFO,
235001649561SJames Smart 						 LOG_NVME_DISC,
23516825b7bdSJames Smart 						 "6014 Rebind lport to current "
23523fd78355SJames Smart 						 "remoteport %p wwpn 0x%llx, "
235393a3922dSJames Smart 						 "Data: x%x x%x %p %p x%x x%06x\n",
23543fd78355SJames Smart 						 remote_port,
235501649561SJames Smart 						 remote_port->port_name,
235601649561SJames Smart 						 remote_port->port_id,
235701649561SJames Smart 						 remote_port->port_role,
235861184f17SJames Smart 						 oldrport->ndlp,
23593fd78355SJames Smart 						 ndlp,
236001649561SJames Smart 						 ndlp->nlp_type,
236101649561SJames Smart 						 ndlp->nlp_DID);
23626825b7bdSJames Smart 
23636825b7bdSJames Smart 				/* It's a complete rebind only if the driver
23646825b7bdSJames Smart 				 * is registering with the same ndlp. Otherwise
23656825b7bdSJames Smart 				 * the driver likely executed a node swap
23666825b7bdSJames Smart 				 * prior to this registration and the ndlp to
23676825b7bdSJames Smart 				 * remoteport binding needs to be redone.
23686825b7bdSJames Smart 				 */
23696825b7bdSJames Smart 				if (prev_ndlp == ndlp)
23703fd78355SJames Smart 					return 0;
23716825b7bdSJames Smart 
23723fd78355SJames Smart 			}
23732b75d0f9SDick Kennedy 
23743fd78355SJames Smart 			/* Sever the ndlp<->rport association
23753fd78355SJames Smart 			 * before dropping the ndlp ref from
23763fd78355SJames Smart 			 * register.
23772b75d0f9SDick Kennedy 			 */
23783fd78355SJames Smart 			spin_lock_irq(&vport->phba->hbalock);
23792b75d0f9SDick Kennedy 			ndlp->nrport = NULL;
238001466024SJames Smart 			ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
23813fd78355SJames Smart 			spin_unlock_irq(&vport->phba->hbalock);
23822b75d0f9SDick Kennedy 			rport->ndlp = NULL;
23833fd78355SJames Smart 			rport->remoteport = NULL;
2384b04744ceSJames Smart 
2385b04744ceSJames Smart 			/* Reference only removed if previous NDLP is no longer
2386b04744ceSJames Smart 			 * active. It might be just a swap and removing the
2387b04744ceSJames Smart 			 * reference would cause a premature cleanup.
2388b04744ceSJames Smart 			 */
2389b04744ceSJames Smart 			if (prev_ndlp && prev_ndlp != ndlp) {
2390b04744ceSJames Smart 				if ((!NLP_CHK_NODE_ACT(prev_ndlp)) ||
2391b04744ceSJames Smart 				    (!prev_ndlp->nrport))
2392b04744ceSJames Smart 					lpfc_nlp_put(prev_ndlp);
2393b04744ceSJames Smart 			}
23942b75d0f9SDick Kennedy 		}
23952b75d0f9SDick Kennedy 
23962b75d0f9SDick Kennedy 		/* Clean bind the rport to the ndlp. */
239701649561SJames Smart 		rport->remoteport = remote_port;
239801649561SJames Smart 		rport->lport = lport;
23993fd78355SJames Smart 		rport->ndlp = ndlp;
24003fd78355SJames Smart 		spin_lock_irq(&vport->phba->hbalock);
240101649561SJames Smart 		ndlp->nrport = rport;
24023fd78355SJames Smart 		spin_unlock_irq(&vport->phba->hbalock);
240301649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO,
240401649561SJames Smart 				 LOG_NVME_DISC | LOG_NODE,
24056825b7bdSJames Smart 				 "6022 Bind lport x%px to remoteport x%px "
24066825b7bdSJames Smart 				 "rport x%px WWNN 0x%llx, "
24077a06dcd3SJames Smart 				 "Rport WWPN 0x%llx DID "
240893a3922dSJames Smart 				 "x%06x Role x%x, ndlp %p prev_ndlp %p\n",
240993a3922dSJames Smart 				 lport, remote_port, rport,
241001649561SJames Smart 				 rpinfo.node_name, rpinfo.port_name,
24113fd78355SJames Smart 				 rpinfo.port_id, rpinfo.port_role,
241293a3922dSJames Smart 				 ndlp, prev_ndlp);
241301649561SJames Smart 	} else {
241401649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR,
241501649561SJames Smart 				 LOG_NVME_DISC | LOG_NODE,
241601649561SJames Smart 				 "6031 RemotePort Registration failed "
241701649561SJames Smart 				 "err: %d, DID x%06x\n",
241801649561SJames Smart 				 ret, ndlp->nlp_DID);
241901649561SJames Smart 	}
24207a06dcd3SJames Smart 
242101649561SJames Smart 	return ret;
2422166d7211SJames Smart #else
2423166d7211SJames Smart 	return 0;
2424166d7211SJames Smart #endif
242501649561SJames Smart }
242601649561SJames Smart 
24276f2589f4SJames Smart /**
24286f2589f4SJames Smart  * lpfc_nvme_rescan_port - Check to see if we should rescan this remoteport
24296f2589f4SJames Smart  *
24306f2589f4SJames Smart  * If the ndlp represents an NVME Target, that we are logged into,
24316f2589f4SJames Smart  * ping the NVME FC Transport layer to initiate a device rescan
24326f2589f4SJames Smart  * on this remote NPort.
24336f2589f4SJames Smart  */
24346f2589f4SJames Smart void
24356f2589f4SJames Smart lpfc_nvme_rescan_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
24366f2589f4SJames Smart {
24376f2589f4SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2438a6d10f24SJames Smart 	struct lpfc_nvme_rport *nrport;
2439a6d10f24SJames Smart 	struct nvme_fc_remote_port *remoteport = NULL;
24406f2589f4SJames Smart 
2441a6d10f24SJames Smart 	spin_lock_irq(&vport->phba->hbalock);
2442a6d10f24SJames Smart 	nrport = lpfc_ndlp_get_nrport(ndlp);
2443a6d10f24SJames Smart 	if (nrport)
2444a6d10f24SJames Smart 		remoteport = nrport->remoteport;
2445a6d10f24SJames Smart 	spin_unlock_irq(&vport->phba->hbalock);
24466f2589f4SJames Smart 
24476f2589f4SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
24486f2589f4SJames Smart 			 "6170 Rescan NPort DID x%06x type x%x "
2449a6d10f24SJames Smart 			 "state x%x nrport x%px remoteport x%px\n",
2450a6d10f24SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type, ndlp->nlp_state,
2451a6d10f24SJames Smart 			 nrport, remoteport);
2452a6d10f24SJames Smart 
2453a6d10f24SJames Smart 	if (!nrport || !remoteport)
2454a6d10f24SJames Smart 		goto rescan_exit;
24556f2589f4SJames Smart 
24566f2589f4SJames Smart 	/* Only rescan if we are an NVME target in the MAPPED state */
24576f2589f4SJames Smart 	if (remoteport->port_role & FC_PORT_ROLE_NVME_DISCOVERY &&
24586f2589f4SJames Smart 	    ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
24596f2589f4SJames Smart 		nvme_fc_rescan_remoteport(remoteport);
24606f2589f4SJames Smart 
24616f2589f4SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
24626f2589f4SJames Smart 				 "6172 NVME rescanned DID x%06x "
24636f2589f4SJames Smart 				 "port_state x%x\n",
24646f2589f4SJames Smart 				 ndlp->nlp_DID, remoteport->port_state);
24656f2589f4SJames Smart 	}
24666f2589f4SJames Smart 	return;
2467a6d10f24SJames Smart  rescan_exit:
2468a6d10f24SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2469a6d10f24SJames Smart 			 "6169 Skip NVME Rport Rescan, NVME remoteport "
2470a6d10f24SJames Smart 			 "unregistered\n");
24716f2589f4SJames Smart #endif
24726f2589f4SJames Smart }
24736f2589f4SJames Smart 
247401649561SJames Smart /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
247501649561SJames Smart  *
247601649561SJames Smart  * There is no notion of Devloss or rport recovery from the current
247701649561SJames Smart  * nvme_transport perspective.  Loss of an rport just means IO cannot
247801649561SJames Smart  * be sent and recovery is completely up to the initator.
247901649561SJames Smart  * For now, the driver just unbinds the DID and port_role so that
248001649561SJames Smart  * no further IO can be issued.  Changes are planned for later.
248101649561SJames Smart  *
248201649561SJames Smart  * Notes - the ndlp reference count is not decremented here since
248301649561SJames Smart  * since there is no nvme_transport api for devloss.  Node ref count
248401649561SJames Smart  * is only adjusted in driver unload.
248501649561SJames Smart  */
248601649561SJames Smart void
248701649561SJames Smart lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
248801649561SJames Smart {
24897d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
249001649561SJames Smart 	int ret;
249101649561SJames Smart 	struct nvme_fc_local_port *localport;
249201649561SJames Smart 	struct lpfc_nvme_lport *lport;
249301649561SJames Smart 	struct lpfc_nvme_rport *rport;
24949e210178SJames Smart 	struct nvme_fc_remote_port *remoteport = NULL;
249501649561SJames Smart 
249601649561SJames Smart 	localport = vport->localport;
249701649561SJames Smart 
249801649561SJames Smart 	/* This is fundamental error.  The localport is always
249901649561SJames Smart 	 * available until driver unload.  Just exit.
250001649561SJames Smart 	 */
250101649561SJames Smart 	if (!localport)
250201649561SJames Smart 		return;
250301649561SJames Smart 
250401649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
250501649561SJames Smart 	if (!lport)
250601649561SJames Smart 		goto input_err;
250701649561SJames Smart 
25089e210178SJames Smart 	spin_lock_irq(&vport->phba->hbalock);
250901466024SJames Smart 	rport = lpfc_ndlp_get_nrport(ndlp);
25109e210178SJames Smart 	if (rport)
25119e210178SJames Smart 		remoteport = rport->remoteport;
25129e210178SJames Smart 	spin_unlock_irq(&vport->phba->hbalock);
25139e210178SJames Smart 	if (!remoteport)
251401649561SJames Smart 		goto input_err;
251501649561SJames Smart 
251601649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
251701649561SJames Smart 			 "6033 Unreg nvme remoteport %p, portname x%llx, "
251801649561SJames Smart 			 "port_id x%06x, portstate x%x port type x%x\n",
251901649561SJames Smart 			 remoteport, remoteport->port_name,
252001649561SJames Smart 			 remoteport->port_id, remoteport->port_state,
252101649561SJames Smart 			 ndlp->nlp_type);
252201649561SJames Smart 
252301649561SJames Smart 	/* Sanity check ndlp type.  Only call for NVME ports. Don't
252401649561SJames Smart 	 * clear any rport state until the transport calls back.
252501649561SJames Smart 	 */
25263b5bde69SJames Smart 
25273b5bde69SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET) {
25287a06dcd3SJames Smart 		/* No concern about the role change on the nvme remoteport.
25297a06dcd3SJames Smart 		 * The transport will update it.
25307a06dcd3SJames Smart 		 */
2531add9d6beSJames Smart 		ndlp->upcall_flags |= NLP_WAIT_FOR_UNREG;
25327438273fSJames Smart 
25337438273fSJames Smart 		/* Don't let the host nvme transport keep sending keep-alives
25347438273fSJames Smart 		 * on this remoteport. Vport is unloading, no recovery. The
25357438273fSJames Smart 		 * return values is ignored.  The upcall is a courtesy to the
25367438273fSJames Smart 		 * transport.
25377438273fSJames Smart 		 */
25387438273fSJames Smart 		if (vport->load_flag & FC_UNLOADING)
25397438273fSJames Smart 			(void)nvme_fc_set_remoteport_devloss(remoteport, 0);
25407438273fSJames Smart 
254101649561SJames Smart 		ret = nvme_fc_unregister_remoteport(remoteport);
25423fd78355SJames Smart 		if (ret != 0) {
25433fd78355SJames Smart 			lpfc_nlp_put(ndlp);
254401649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
254501649561SJames Smart 					 "6167 NVME unregister failed %d "
254601649561SJames Smart 					 "port_state x%x\n",
254701649561SJames Smart 					 ret, remoteport->port_state);
25483fd78355SJames Smart 		}
254901649561SJames Smart 	}
255001649561SJames Smart 	return;
255101649561SJames Smart 
255201649561SJames Smart  input_err:
2553166d7211SJames Smart #endif
255401649561SJames Smart 	lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
25552b7824d0SJames Smart 			 "6168 State error: lport %p, rport%p FCID x%06x\n",
255601649561SJames Smart 			 vport->localport, ndlp->rport, ndlp->nlp_DID);
255701649561SJames Smart }
2558318083adSJames Smart 
2559318083adSJames Smart /**
2560318083adSJames Smart  * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2561318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
2562318083adSJames Smart  * @axri: pointer to the fcp xri abort wcqe structure.
2563318083adSJames Smart  *
2564318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
2565952c303bSDick Kennedy  * NVME aborted xri.  Aborted NVME IO commands are completed to the transport
2566952c303bSDick Kennedy  * here.
2567318083adSJames Smart  **/
2568318083adSJames Smart void
2569318083adSJames Smart lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
25705e5b511dSJames Smart 			   struct sli4_wcqe_xri_aborted *axri, int idx)
2571318083adSJames Smart {
2572318083adSJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
2573c490850aSJames Smart 	struct lpfc_io_buf *lpfc_ncmd, *next_lpfc_ncmd;
2574952c303bSDick Kennedy 	struct nvmefc_fcp_req *nvme_cmd = NULL;
2575318083adSJames Smart 	struct lpfc_nodelist *ndlp;
25765e5b511dSJames Smart 	struct lpfc_sli4_hdw_queue *qp;
2577318083adSJames Smart 	unsigned long iflag = 0;
2578318083adSJames Smart 
2579318083adSJames Smart 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
2580318083adSJames Smart 		return;
25815e5b511dSJames Smart 	qp = &phba->sli4_hba.hdwq[idx];
2582318083adSJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
25835e5b511dSJames Smart 	spin_lock(&qp->abts_nvme_buf_list_lock);
2584318083adSJames Smart 	list_for_each_entry_safe(lpfc_ncmd, next_lpfc_ncmd,
25855e5b511dSJames Smart 				 &qp->lpfc_abts_nvme_buf_list, list) {
2586318083adSJames Smart 		if (lpfc_ncmd->cur_iocbq.sli4_xritag == xri) {
2587bbe3012bSJames Smart 			list_del_init(&lpfc_ncmd->list);
25885e5b511dSJames Smart 			qp->abts_nvme_io_bufs--;
2589318083adSJames Smart 			lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
2590318083adSJames Smart 			lpfc_ncmd->status = IOSTAT_SUCCESS;
25915e5b511dSJames Smart 			spin_unlock(&qp->abts_nvme_buf_list_lock);
2592318083adSJames Smart 
2593318083adSJames Smart 			spin_unlock_irqrestore(&phba->hbalock, iflag);
2594318083adSJames Smart 			ndlp = lpfc_ncmd->ndlp;
2595952c303bSDick Kennedy 			if (ndlp)
2596318083adSJames Smart 				lpfc_sli4_abts_err_handler(phba, ndlp, axri);
259786c67379SJames Smart 
259886c67379SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2599952c303bSDick Kennedy 					"6311 nvme_cmd %p xri x%x tag x%x "
2600952c303bSDick Kennedy 					"abort complete and xri released\n",
2601952c303bSDick Kennedy 					lpfc_ncmd->nvmeCmd, xri,
2602952c303bSDick Kennedy 					lpfc_ncmd->cur_iocbq.iotag);
260386c67379SJames Smart 
2604952c303bSDick Kennedy 			/* Aborted NVME commands are required to not complete
2605952c303bSDick Kennedy 			 * before the abort exchange command fully completes.
2606952c303bSDick Kennedy 			 * Once completed, it is available via the put list.
2607952c303bSDick Kennedy 			 */
26083fd78355SJames Smart 			if (lpfc_ncmd->nvmeCmd) {
2609952c303bSDick Kennedy 				nvme_cmd = lpfc_ncmd->nvmeCmd;
2610952c303bSDick Kennedy 				nvme_cmd->done(nvme_cmd);
26113fd78355SJames Smart 				lpfc_ncmd->nvmeCmd = NULL;
26123fd78355SJames Smart 			}
2613318083adSJames Smart 			lpfc_release_nvme_buf(phba, lpfc_ncmd);
2614318083adSJames Smart 			return;
2615318083adSJames Smart 		}
2616318083adSJames Smart 	}
26175e5b511dSJames Smart 	spin_unlock(&qp->abts_nvme_buf_list_lock);
2618318083adSJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
261986c67379SJames Smart 
262086c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
262186c67379SJames Smart 			"6312 XRI Aborted xri x%x not found\n", xri);
262286c67379SJames Smart 
2623318083adSJames Smart }
2624c3725bdcSJames Smart 
2625c3725bdcSJames Smart /**
2626c3725bdcSJames Smart  * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2627c3725bdcSJames Smart  * @phba: Pointer to HBA context object.
2628c3725bdcSJames Smart  *
2629c3725bdcSJames Smart  * This function flushes all wqes in the nvme rings and frees all resources
2630c3725bdcSJames Smart  * in the txcmplq. This function does not issue abort wqes for the IO
2631c3725bdcSJames Smart  * commands in txcmplq, they will just be returned with
2632c3725bdcSJames Smart  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2633c3725bdcSJames Smart  * slot has been permanently disabled.
2634c3725bdcSJames Smart  **/
2635c3725bdcSJames Smart void
2636c3725bdcSJames Smart lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2637c3725bdcSJames Smart {
2638c3725bdcSJames Smart 	struct lpfc_sli_ring  *pring;
2639c3725bdcSJames Smart 	u32 i, wait_cnt = 0;
2640c3725bdcSJames Smart 
2641cdb42becSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4 || !phba->sli4_hba.hdwq)
2642c3725bdcSJames Smart 		return;
2643c3725bdcSJames Smart 
2644c3725bdcSJames Smart 	/* Cycle through all NVME rings and make sure all outstanding
2645c3725bdcSJames Smart 	 * WQEs have been removed from the txcmplqs.
2646c3725bdcSJames Smart 	 */
2647cdb42becSJames Smart 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
26486a828b0fSJames Smart 		if (!phba->sli4_hba.hdwq[i].nvme_wq)
26496a828b0fSJames Smart 			continue;
2650cdb42becSJames Smart 		pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
2651c3725bdcSJames Smart 
2652d580c613SJames Smart 		if (!pring)
2653d580c613SJames Smart 			continue;
2654d580c613SJames Smart 
2655c3725bdcSJames Smart 		/* Retrieve everything on the txcmplq */
2656c3725bdcSJames Smart 		while (!list_empty(&pring->txcmplq)) {
2657c3725bdcSJames Smart 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
2658c3725bdcSJames Smart 			wait_cnt++;
2659c3725bdcSJames Smart 
2660c3725bdcSJames Smart 			/* The sleep is 10mS.  Every ten seconds,
2661c3725bdcSJames Smart 			 * dump a message.  Something is wrong.
2662c3725bdcSJames Smart 			 */
2663c3725bdcSJames Smart 			if ((wait_cnt % 1000) == 0) {
2664c3725bdcSJames Smart 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2665c3725bdcSJames Smart 						"6178 NVME IO not empty, "
2666c3725bdcSJames Smart 						"cnt %d\n", wait_cnt);
2667c3725bdcSJames Smart 			}
2668c3725bdcSJames Smart 		}
2669c3725bdcSJames Smart 	}
2670c3725bdcSJames Smart }
2671*84f2ddf8SJames Smart 
2672*84f2ddf8SJames Smart void
2673*84f2ddf8SJames Smart lpfc_nvme_cancel_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn)
2674*84f2ddf8SJames Smart {
2675*84f2ddf8SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2676*84f2ddf8SJames Smart 	struct lpfc_io_buf *lpfc_ncmd;
2677*84f2ddf8SJames Smart 	struct nvmefc_fcp_req *nCmd;
2678*84f2ddf8SJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
2679*84f2ddf8SJames Smart 
2680*84f2ddf8SJames Smart 	if (!pwqeIn->context1) {
2681*84f2ddf8SJames Smart 		lpfc_sli_release_iocbq(phba, pwqeIn);
2682*84f2ddf8SJames Smart 		return;
2683*84f2ddf8SJames Smart 	}
2684*84f2ddf8SJames Smart 	/* For abort iocb just return, IO iocb will do a done call */
2685*84f2ddf8SJames Smart 	if (bf_get(wqe_cmnd, &pwqeIn->wqe.gen_req.wqe_com) ==
2686*84f2ddf8SJames Smart 	    CMD_ABORT_XRI_CX) {
2687*84f2ddf8SJames Smart 		lpfc_sli_release_iocbq(phba, pwqeIn);
2688*84f2ddf8SJames Smart 		return;
2689*84f2ddf8SJames Smart 	}
2690*84f2ddf8SJames Smart 	lpfc_ncmd = (struct lpfc_io_buf *)pwqeIn->context1;
2691*84f2ddf8SJames Smart 
2692*84f2ddf8SJames Smart 	spin_lock(&lpfc_ncmd->buf_lock);
2693*84f2ddf8SJames Smart 	if (!lpfc_ncmd->nvmeCmd) {
2694*84f2ddf8SJames Smart 		spin_unlock(&lpfc_ncmd->buf_lock);
2695*84f2ddf8SJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
2696*84f2ddf8SJames Smart 		return;
2697*84f2ddf8SJames Smart 	}
2698*84f2ddf8SJames Smart 
2699*84f2ddf8SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
2700*84f2ddf8SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2701*84f2ddf8SJames Smart 			"6194 NVME Cancel xri %x\n",
2702*84f2ddf8SJames Smart 			lpfc_ncmd->cur_iocbq.sli4_xritag);
2703*84f2ddf8SJames Smart 
2704*84f2ddf8SJames Smart 	nCmd->transferred_length = 0;
2705*84f2ddf8SJames Smart 	nCmd->rcv_rsplen = 0;
2706*84f2ddf8SJames Smart 	nCmd->status = NVME_SC_INTERNAL;
2707*84f2ddf8SJames Smart 	freqpriv = nCmd->private;
2708*84f2ddf8SJames Smart 	freqpriv->nvme_buf = NULL;
2709*84f2ddf8SJames Smart 	lpfc_ncmd->nvmeCmd = NULL;
2710*84f2ddf8SJames Smart 
2711*84f2ddf8SJames Smart 	spin_unlock(&lpfc_ncmd->buf_lock);
2712*84f2ddf8SJames Smart 	nCmd->done(nCmd);
2713*84f2ddf8SJames Smart 
2714*84f2ddf8SJames Smart 	/* Call release with XB=1 to queue the IO into the abort list. */
2715*84f2ddf8SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
2716*84f2ddf8SJames Smart #endif
2717*84f2ddf8SJames Smart }
2718