aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Smart <jsmart2021@gmail.com>2017-02-12 16:52:32 -0500
committerMartin K. Petersen <martin.petersen@oracle.com>2017-02-22 18:41:43 -0500
commit01649561a8b4b77247bd234f240d737367bb8a52 (patch)
treea37a5b805516a422edf898463d395a87ecc50d2a
parenta0f2d3ef374fd8d2f51b8cc1ea723014b1aa2c9b (diff)
scsi: lpfc: NVME Initiator: bind to nvme_fc api
NVME Initiator: Tie in to NVME Fabrics nvme_fc LLDD initiator api Adds the routines to: - register and deregister the FC port as a nvme-fc initiator localport - register and deregister remote FC ports as a nvme-fc remoteport - binding of nvme queues to adapter WQs - send/perform NVME LS's - send/perform NVME FCP initiator io operations Signed-off-by: Dick Kennedy <dick.kennedy@broadcom.com> Signed-off-by: James Smart <james.smart@broadcom.com> Reviewed-by: Hannes Reinecke <hare@suse.com> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
-rw-r--r--drivers/scsi/lpfc/Makefile7
-rw-r--r--drivers/scsi/lpfc/lpfc.h5
-rw-r--r--drivers/scsi/lpfc/lpfc_crtn.h10
-rw-r--r--drivers/scsi/lpfc/lpfc_ct.c2
-rw-r--r--drivers/scsi/lpfc/lpfc_els.c4
-rw-r--r--drivers/scsi/lpfc/lpfc_hbadisc.c15
-rw-r--r--drivers/scsi/lpfc/lpfc_init.c39
-rw-r--r--drivers/scsi/lpfc/lpfc_nportdisc.c4
-rw-r--r--drivers/scsi/lpfc/lpfc_nvme.c2319
-rw-r--r--drivers/scsi/lpfc/lpfc_nvme.h6
-rw-r--r--drivers/scsi/lpfc/lpfc_sli.c21
-rw-r--r--drivers/scsi/lpfc/lpfc_sli4.h1
-rw-r--r--drivers/scsi/lpfc/lpfc_vport.c16
13 files changed, 2411 insertions, 38 deletions
diff --git a/drivers/scsi/lpfc/Makefile b/drivers/scsi/lpfc/Makefile
index e2516ba8ebfa..cd7e1fcf52c6 100644
--- a/drivers/scsi/lpfc/Makefile
+++ b/drivers/scsi/lpfc/Makefile
@@ -28,6 +28,7 @@ endif
28 28
29obj-$(CONFIG_SCSI_LPFC) := lpfc.o 29obj-$(CONFIG_SCSI_LPFC) := lpfc.o
30 30
31lpfc-objs := lpfc_mem.o lpfc_sli.o lpfc_ct.o lpfc_els.o lpfc_hbadisc.o \ 31lpfc-objs := lpfc_mem.o lpfc_sli.o lpfc_ct.o lpfc_els.o \
32 lpfc_init.o lpfc_mbox.o lpfc_nportdisc.o lpfc_scsi.o lpfc_attr.o \ 32 lpfc_hbadisc.o lpfc_init.o lpfc_mbox.o lpfc_nportdisc.o \
33 lpfc_vport.o lpfc_debugfs.o lpfc_bsg.o 33 lpfc_scsi.o lpfc_attr.o lpfc_vport.o lpfc_debugfs.o lpfc_bsg.o \
34 lpfc_nvme.o
diff --git a/drivers/scsi/lpfc/lpfc.h b/drivers/scsi/lpfc/lpfc.h
index 81c47d1aebb2..9ad63a47425b 100644
--- a/drivers/scsi/lpfc/lpfc.h
+++ b/drivers/scsi/lpfc/lpfc.h
@@ -123,6 +123,11 @@ struct perf_prof {
123 uint16_t wqidx[40]; 123 uint16_t wqidx[40];
124}; 124};
125 125
126/*
127 * Provide for FC4 TYPE x28 - NVME. The
128 * bit mask for FCP and NVME is 0x8 identically
129 * because they are 32 bit positions distance.
130 */
126#define LPFC_FC4_TYPE_BITMASK 0x00000100 131#define LPFC_FC4_TYPE_BITMASK 0x00000100
127 132
128/* Provide DMA memory definitions the driver uses per port instance. */ 133/* Provide DMA memory definitions the driver uses per port instance. */
diff --git a/drivers/scsi/lpfc/lpfc_crtn.h b/drivers/scsi/lpfc/lpfc_crtn.h
index 2e6345ebd6c5..a9c8a0a41b16 100644
--- a/drivers/scsi/lpfc/lpfc_crtn.h
+++ b/drivers/scsi/lpfc/lpfc_crtn.h
@@ -518,4 +518,14 @@ int lpfc_sli4_dump_page_a0(struct lpfc_hba *phba, struct lpfcMboxq *mbox);
518void lpfc_mbx_cmpl_rdp_page_a0(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb); 518void lpfc_mbx_cmpl_rdp_page_a0(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb);
519 519
520/* NVME interfaces. */ 520/* NVME interfaces. */
521void lpfc_nvme_unregister_port(struct lpfc_vport *vport,
522 struct lpfc_nodelist *ndlp);
523int lpfc_nvme_register_port(struct lpfc_vport *vport,
524 struct lpfc_nodelist *ndlp);
525int lpfc_nvme_create_localport(struct lpfc_vport *vport);
526void lpfc_nvme_destroy_localport(struct lpfc_vport *vport);
527void lpfc_nvme_update_localport(struct lpfc_vport *vport);
521void lpfc_nvme_mod_param_dep(struct lpfc_hba *phba); 528void lpfc_nvme_mod_param_dep(struct lpfc_hba *phba);
529void lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba,
530 struct lpfc_iocbq *cmdiocb,
531 struct lpfc_wcqe_complete *abts_cmpl);
diff --git a/drivers/scsi/lpfc/lpfc_ct.c b/drivers/scsi/lpfc/lpfc_ct.c
index afb25369bc3b..8cfeffe312e0 100644
--- a/drivers/scsi/lpfc/lpfc_ct.c
+++ b/drivers/scsi/lpfc/lpfc_ct.c
@@ -1412,7 +1412,7 @@ lpfc_ns_cmd(struct lpfc_vport *vport, int cmdcode,
1412 if (((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) || 1412 if (((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1413 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) && 1413 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
1414 (context == FC_TYPE_NVME)) { 1414 (context == FC_TYPE_NVME)) {
1415 /* todo: init: revise localport nvme attributes */ 1415 lpfc_nvme_update_localport(vport);
1416 CtReq->un.rff.type_code = context; 1416 CtReq->un.rff.type_code = context;
1417 1417
1418 } else if (((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) || 1418 } else if (((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
diff --git a/drivers/scsi/lpfc/lpfc_els.c b/drivers/scsi/lpfc/lpfc_els.c
index e164eed25e3d..23546b3c950c 100644
--- a/drivers/scsi/lpfc/lpfc_els.c
+++ b/drivers/scsi/lpfc/lpfc_els.c
@@ -2618,7 +2618,9 @@ out:
2618 !(vport->fc_flag & FC_PT2PT_PLOGI)) { 2618 !(vport->fc_flag & FC_PT2PT_PLOGI)) {
2619 phba->pport->fc_myDID = 0; 2619 phba->pport->fc_myDID = 0;
2620 2620
2621 /* todo: init: revise localport nvme attributes */ 2621 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
2622 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
2623 lpfc_nvme_update_localport(phba->pport);
2622 2624
2623 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL); 2625 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2624 if (mbox) { 2626 if (mbox) {
diff --git a/drivers/scsi/lpfc/lpfc_hbadisc.c b/drivers/scsi/lpfc/lpfc_hbadisc.c
index 6e64c8e8e44f..8936f5d91c87 100644
--- a/drivers/scsi/lpfc/lpfc_hbadisc.c
+++ b/drivers/scsi/lpfc/lpfc_hbadisc.c
@@ -909,7 +909,9 @@ lpfc_linkdown(struct lpfc_hba *phba)
909 909
910 vports[i]->fc_myDID = 0; 910 vports[i]->fc_myDID = 0;
911 911
912 /* todo: init: revise localport nvme attributes */ 912 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
913 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
914 lpfc_nvme_update_localport(vports[i]);
913 } 915 }
914 } 916 }
915 lpfc_destroy_vport_work_array(phba, vports); 917 lpfc_destroy_vport_work_array(phba, vports);
@@ -3580,7 +3582,9 @@ lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3580 spin_unlock_irq(shost->host_lock); 3582 spin_unlock_irq(shost->host_lock);
3581 vport->fc_myDID = 0; 3583 vport->fc_myDID = 0;
3582 3584
3583 /* todo: init: revise localport nvme attributes */ 3585 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3586 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
3587 lpfc_nvme_update_localport(vport);
3584 goto out; 3588 goto out;
3585 } 3589 }
3586 3590
@@ -3950,7 +3954,8 @@ out:
3950 3954
3951 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) || 3955 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3952 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) 3956 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
3953 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_NVME); 3957 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
3958 FC_TYPE_NVME);
3954 3959
3955 /* Issue SCR just before NameServer GID_FT Query */ 3960 /* Issue SCR just before NameServer GID_FT Query */
3956 lpfc_issue_els_scr(vport, SCR_DID, 0); 3961 lpfc_issue_els_scr(vport, SCR_DID, 0);
@@ -4144,7 +4149,7 @@ lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4144 ((ndlp->nlp_fc4_type & NLP_FC4_NVME) || 4149 ((ndlp->nlp_fc4_type & NLP_FC4_NVME) ||
4145 (ndlp->nlp_DID == Fabric_DID))) { 4150 (ndlp->nlp_DID == Fabric_DID))) {
4146 vport->phba->nport_event_cnt++; 4151 vport->phba->nport_event_cnt++;
4147 /* todo: init: unregister rport from nvme */ 4152 lpfc_nvme_unregister_port(vport, ndlp);
4148 } 4153 }
4149 } 4154 }
4150 4155
@@ -4169,7 +4174,7 @@ lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4169 * the register. 4174 * the register.
4170 */ 4175 */
4171 vport->phba->nport_event_cnt++; 4176 vport->phba->nport_event_cnt++;
4172 /* todo: init: register rport with nvme */ 4177 lpfc_nvme_register_port(vport, ndlp);
4173 } 4178 }
4174 } 4179 }
4175 } 4180 }
diff --git a/drivers/scsi/lpfc/lpfc_init.c b/drivers/scsi/lpfc/lpfc_init.c
index e609afaa472a..474bafc63055 100644
--- a/drivers/scsi/lpfc/lpfc_init.c
+++ b/drivers/scsi/lpfc/lpfc_init.c
@@ -3128,7 +3128,6 @@ static void
3128lpfc_scsi_free(struct lpfc_hba *phba) 3128lpfc_scsi_free(struct lpfc_hba *phba)
3129{ 3129{
3130 struct lpfc_scsi_buf *sb, *sb_next; 3130 struct lpfc_scsi_buf *sb, *sb_next;
3131 struct lpfc_iocbq *io, *io_next;
3132 3131
3133 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)) 3132 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3134 return; 3133 return;
@@ -3158,14 +3157,6 @@ lpfc_scsi_free(struct lpfc_hba *phba)
3158 phba->total_scsi_bufs--; 3157 phba->total_scsi_bufs--;
3159 } 3158 }
3160 spin_unlock(&phba->scsi_buf_list_get_lock); 3159 spin_unlock(&phba->scsi_buf_list_get_lock);
3161
3162 /* Release all the lpfc_iocbq entries maintained by this host. */
3163 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
3164 list_del(&io->list);
3165 kfree(io);
3166 phba->total_iocbq_bufs--;
3167 }
3168
3169 spin_unlock_irq(&phba->hbalock); 3160 spin_unlock_irq(&phba->hbalock);
3170} 3161}
3171/** 3162/**
@@ -3180,7 +3171,6 @@ static void
3180lpfc_nvme_free(struct lpfc_hba *phba) 3171lpfc_nvme_free(struct lpfc_hba *phba)
3181{ 3172{
3182 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next; 3173 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
3183 struct lpfc_iocbq *io, *io_next;
3184 3174
3185 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) 3175 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3186 return; 3176 return;
@@ -3209,14 +3199,6 @@ lpfc_nvme_free(struct lpfc_hba *phba)
3209 phba->total_nvme_bufs--; 3199 phba->total_nvme_bufs--;
3210 } 3200 }
3211 spin_unlock(&phba->nvme_buf_list_get_lock); 3201 spin_unlock(&phba->nvme_buf_list_get_lock);
3212
3213 /* Release all the lpfc_iocbq entries maintained by this host. */
3214 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
3215 list_del(&io->list);
3216 kfree(io);
3217 phba->total_iocbq_bufs--;
3218 }
3219
3220 spin_unlock_irq(&phba->hbalock); 3202 spin_unlock_irq(&phba->hbalock);
3221} 3203}
3222/** 3204/**
@@ -10685,7 +10667,23 @@ lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
10685 /* Perform post initialization setup */ 10667 /* Perform post initialization setup */
10686 lpfc_post_init_setup(phba); 10668 lpfc_post_init_setup(phba);
10687 10669
10688 /* todo: init: register port with nvme */ 10670 /* NVME support in FW earlier in the driver load corrects the
10671 * FC4 type making a check for nvme_support unnecessary.
10672 */
10673 if ((phba->nvmet_support == 0) &&
10674 (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
10675 /* Create NVME binding with nvme_fc_transport. This
10676 * ensures the vport is initialized.
10677 */
10678 error = lpfc_nvme_create_localport(vport);
10679 if (error) {
10680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10681 "6004 NVME registration failed, "
10682 "error x%x\n",
10683 error);
10684 goto out_disable_intr;
10685 }
10686 }
10689 10687
10690 /* check for firmware upgrade or downgrade */ 10688 /* check for firmware upgrade or downgrade */
10691 if (phba->cfg_request_firmware_upgrade) 10689 if (phba->cfg_request_firmware_upgrade)
@@ -10761,8 +10759,8 @@ lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10761 /* Perform ndlp cleanup on the physical port. The nvme localport 10759 /* Perform ndlp cleanup on the physical port. The nvme localport
10762 * is destroyed after to ensure all rports are io-disabled. 10760 * is destroyed after to ensure all rports are io-disabled.
10763 */ 10761 */
10762 lpfc_nvme_destroy_localport(vport);
10764 lpfc_cleanup(vport); 10763 lpfc_cleanup(vport);
10765 /* todo: init: unregister port with nvme */
10766 10764
10767 /* 10765 /*
10768 * Bring down the SLI Layer. This step disables all interrupts, 10766 * Bring down the SLI Layer. This step disables all interrupts,
@@ -10781,6 +10779,7 @@ lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10781 */ 10779 */
10782 lpfc_scsi_free(phba); 10780 lpfc_scsi_free(phba);
10783 lpfc_nvme_free(phba); 10781 lpfc_nvme_free(phba);
10782 lpfc_free_iocb_list(phba);
10784 10783
10785 lpfc_sli4_driver_resource_unset(phba); 10784 lpfc_sli4_driver_resource_unset(phba);
10786 10785
diff --git a/drivers/scsi/lpfc/lpfc_nportdisc.c b/drivers/scsi/lpfc/lpfc_nportdisc.c
index 65e7b2433ee7..470f9586192f 100644
--- a/drivers/scsi/lpfc/lpfc_nportdisc.c
+++ b/drivers/scsi/lpfc/lpfc_nportdisc.c
@@ -1655,9 +1655,7 @@ lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
1655 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) { 1655 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
1656 ndlp->nlp_fc4_type |= NLP_FC4_NVME; 1656 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1657 /* We need to update the localport also */ 1657 /* We need to update the localport also */
1658 /* todo: init: revise localport nvme 1658 lpfc_nvme_update_localport(vport);
1659 * attributes
1660 */
1661 } 1659 }
1662 1660
1663 } else if (ndlp->nlp_fc4_type == 0) { 1661 } else if (ndlp->nlp_fc4_type == 0) {
diff --git a/drivers/scsi/lpfc/lpfc_nvme.c b/drivers/scsi/lpfc/lpfc_nvme.c
new file mode 100644
index 000000000000..729803dacf15
--- /dev/null
+++ b/drivers/scsi/lpfc/lpfc_nvme.c
@@ -0,0 +1,2319 @@
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
20 ********************************************************************/
21#include <linux/pci.h>
22#include <linux/slab.h>
23#include <linux/interrupt.h>
24#include <linux/delay.h>
25#include <asm/unaligned.h>
26#include <linux/crc-t10dif.h>
27#include <net/checksum.h>
28
29#include <scsi/scsi.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_host.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_transport_fc.h>
35#include <scsi/fc/fc_fs.h>
36
37#include <linux/nvme.h>
38#include <linux/nvme-fc-driver.h>
39#include <linux/nvme-fc.h>
40#include "lpfc_version.h"
41#include "lpfc_hw4.h"
42#include "lpfc_hw.h"
43#include "lpfc_sli.h"
44#include "lpfc_sli4.h"
45#include "lpfc_nl.h"
46#include "lpfc_disc.h"
47#include "lpfc.h"
48#include "lpfc_nvme.h"
49#include "lpfc_scsi.h"
50#include "lpfc_logmsg.h"
51#include "lpfc_crtn.h"
52#include "lpfc_vport.h"
53
54/* NVME initiator-based functions */
55
56static struct lpfc_nvme_buf *
57lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp);
58
59static void
60lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_nvme_buf *);
61
62
63/**
64 * lpfc_nvme_create_queue -
65 * @lpfc_pnvme: Pointer to the driver's nvme instance data
66 * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
67 * @handle: An opaque driver handle used in follow-up calls.
68 *
69 * Driver registers this routine to preallocate and initialize any
70 * internal data structures to bind the @qidx to its internal IO queues.
71 * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
72 *
73 * Return value :
74 * 0 - Success
75 * -EINVAL - Unsupported input value.
76 * -ENOMEM - Could not alloc necessary memory
77 **/
78static int
79lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
80 unsigned int qidx, u16 qsize,
81 void **handle)
82{
83 struct lpfc_nvme_lport *lport;
84 struct lpfc_vport *vport;
85 struct lpfc_nvme_qhandle *qhandle;
86 char *str;
87
88 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
89 vport = lport->vport;
90 qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
91 if (qhandle == NULL)
92 return -ENOMEM;
93
94 qhandle->cpu_id = smp_processor_id();
95 qhandle->qidx = qidx;
96 /*
97 * NVME qidx == 0 is the admin queue, so both admin queue
98 * and first IO queue will use MSI-X vector and associated
99 * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
100 */
101 if (qidx) {
102 str = "IO "; /* IO queue */
103 qhandle->index = ((qidx - 1) %
104 vport->phba->cfg_nvme_io_channel);
105 } else {
106 str = "ADM"; /* Admin queue */
107 qhandle->index = qidx;
108 }
109
110 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
111 "6073 Binding %s HdwQueue %d (cpu %d) to "
112 "io_channel %d qhandle %p\n", str,
113 qidx, qhandle->cpu_id, qhandle->index, qhandle);
114 *handle = (void *)qhandle;
115 return 0;
116}
117
118/**
119 * lpfc_nvme_delete_queue -
120 * @lpfc_pnvme: Pointer to the driver's nvme instance data
121 * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
122 * @handle: An opaque driver handle from lpfc_nvme_create_queue
123 *
124 * Driver registers this routine to free
125 * any internal data structures to bind the @qidx to its internal
126 * IO queues.
127 *
128 * Return value :
129 * 0 - Success
130 * TODO: What are the failure codes.
131 **/
132static void
133lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
134 unsigned int qidx,
135 void *handle)
136{
137 struct lpfc_nvme_lport *lport;
138 struct lpfc_vport *vport;
139
140 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
141 vport = lport->vport;
142
143 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
144 "6001 ENTER. lpfc_pnvme %p, qidx x%xi qhandle %p\n",
145 lport, qidx, handle);
146 kfree(handle);
147}
148
149static void
150lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
151{
152 struct lpfc_nvme_lport *lport = localport->private;
153
154 /* release any threads waiting for the unreg to complete */
155 complete(&lport->lport_unreg_done);
156}
157
158/* lpfc_nvme_remoteport_delete
159 *
160 * @remoteport: Pointer to an nvme transport remoteport instance.
161 *
162 * This is a template downcall. NVME transport calls this function
163 * when it has completed the unregistration of a previously
164 * registered remoteport.
165 *
166 * Return value :
167 * None
168 */
169void
170lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
171{
172 struct lpfc_nvme_rport *rport = remoteport->private;
173 struct lpfc_vport *vport;
174 struct lpfc_nodelist *ndlp;
175
176 ndlp = rport->ndlp;
177 if (!ndlp)
178 goto rport_err;
179
180 vport = ndlp->vport;
181 if (!vport)
182 goto rport_err;
183
184 /* Remove this rport from the lport's list - memory is owned by the
185 * transport. Remove the ndlp reference for the NVME transport before
186 * calling state machine to remove the node, this is devloss = 0
187 * semantics.
188 */
189 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
190 "6146 remoteport delete complete %p\n",
191 remoteport);
192 list_del(&rport->list);
193 lpfc_nlp_put(ndlp);
194
195 rport_err:
196 /* This call has to execute as long as the rport is valid.
197 * Release any threads waiting for the unreg to complete.
198 */
199 complete(&rport->rport_unreg_done);
200}
201
202static void
203lpfc_nvme_cmpl_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
204 struct lpfc_wcqe_complete *wcqe)
205{
206 struct lpfc_vport *vport = cmdwqe->vport;
207 uint32_t status;
208 struct nvmefc_ls_req *pnvme_lsreq;
209 struct lpfc_dmabuf *buf_ptr;
210 struct lpfc_nodelist *ndlp;
211
212 vport->phba->fc4NvmeLsCmpls++;
213
214 pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
215 status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
216 ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
217 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
218 "6047 nvme cmpl Enter "
219 "Data %p DID %x Xri: %x status %x cmd:%p lsreg:%p "
220 "bmp:%p ndlp:%p\n",
221 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
222 cmdwqe->sli4_xritag, status,
223 cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
224
225 if (cmdwqe->context3) {
226 buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
227 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
228 kfree(buf_ptr);
229 cmdwqe->context3 = NULL;
230 }
231 if (pnvme_lsreq->done)
232 pnvme_lsreq->done(pnvme_lsreq, status);
233 else
234 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
235 "6046 nvme cmpl without done call back? "
236 "Data %p DID %x Xri: %x status %x\n",
237 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
238 cmdwqe->sli4_xritag, status);
239 if (ndlp) {
240 lpfc_nlp_put(ndlp);
241 cmdwqe->context1 = NULL;
242 }
243 lpfc_sli_release_iocbq(phba, cmdwqe);
244}
245
246static int
247lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
248 struct lpfc_dmabuf *inp,
249 struct nvmefc_ls_req *pnvme_lsreq,
250 void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
251 struct lpfc_wcqe_complete *),
252 struct lpfc_nodelist *ndlp, uint32_t num_entry,
253 uint32_t tmo, uint8_t retry)
254{
255 struct lpfc_hba *phba = vport->phba;
256 union lpfc_wqe *wqe;
257 struct lpfc_iocbq *genwqe;
258 struct ulp_bde64 *bpl;
259 struct ulp_bde64 bde;
260 int i, rc, xmit_len, first_len;
261
262 /* Allocate buffer for command WQE */
263 genwqe = lpfc_sli_get_iocbq(phba);
264 if (genwqe == NULL)
265 return 1;
266
267 wqe = &genwqe->wqe;
268 memset(wqe, 0, sizeof(union lpfc_wqe));
269
270 genwqe->context3 = (uint8_t *)bmp;
271 genwqe->iocb_flag |= LPFC_IO_NVME_LS;
272
273 /* Save for completion so we can release these resources */
274 genwqe->context1 = lpfc_nlp_get(ndlp);
275 genwqe->context2 = (uint8_t *)pnvme_lsreq;
276 /* Fill in payload, bp points to frame payload */
277
278 if (!tmo)
279 /* FC spec states we need 3 * ratov for CT requests */
280 tmo = (3 * phba->fc_ratov);
281
282 /* For this command calculate the xmit length of the request bde. */
283 xmit_len = 0;
284 first_len = 0;
285 bpl = (struct ulp_bde64 *)bmp->virt;
286 for (i = 0; i < num_entry; i++) {
287 bde.tus.w = bpl[i].tus.w;
288 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
289 break;
290 xmit_len += bde.tus.f.bdeSize;
291 if (i == 0)
292 first_len = xmit_len;
293 }
294
295 genwqe->rsvd2 = num_entry;
296 genwqe->hba_wqidx = 0;
297
298 /* Words 0 - 2 */
299 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
300 wqe->generic.bde.tus.f.bdeSize = first_len;
301 wqe->generic.bde.addrLow = bpl[0].addrLow;
302 wqe->generic.bde.addrHigh = bpl[0].addrHigh;
303
304 /* Word 3 */
305 wqe->gen_req.request_payload_len = first_len;
306
307 /* Word 4 */
308
309 /* Word 5 */
310 bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
311 bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
312 bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
313 bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_DD_UNSOL_CTL);
314 bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
315
316 /* Word 6 */
317 bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
318 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
319 bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
320
321 /* Word 7 */
322 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
323 bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
324 bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
325 bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
326
327 /* Word 8 */
328 wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
329
330 /* Word 9 */
331 bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
332
333 /* Word 10 */
334 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
335 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
336 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
337 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
338 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
339
340 /* Word 11 */
341 bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
342 bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
343
344
345 /* Issue GEN REQ WQE for NPORT <did> */
346 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
347 "6050 Issue GEN REQ WQE to NPORT x%x "
348 "Data: x%x x%x wq:%p lsreq:%p bmp:%p xmit:%d 1st:%d\n",
349 ndlp->nlp_DID, genwqe->iotag,
350 vport->port_state,
351 genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
352 genwqe->wqe_cmpl = cmpl;
353 genwqe->iocb_cmpl = NULL;
354 genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
355 genwqe->vport = vport;
356 genwqe->retry = retry;
357
358 rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, genwqe);
359 if (rc == WQE_ERROR) {
360 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
361 "6045 Issue GEN REQ WQE to NPORT x%x "
362 "Data: x%x x%x\n",
363 ndlp->nlp_DID, genwqe->iotag,
364 vport->port_state);
365 lpfc_sli_release_iocbq(phba, genwqe);
366 return 1;
367 }
368 return 0;
369}
370
371/**
372 * lpfc_nvme_ls_req - Issue an Link Service request
373 * @lpfc_pnvme: Pointer to the driver's nvme instance data
374 * @lpfc_nvme_lport: Pointer to the driver's local port data
375 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
376 *
377 * Driver registers this routine to handle any link service request
378 * from the nvme_fc transport to a remote nvme-aware port.
379 *
380 * Return value :
381 * 0 - Success
382 * TODO: What are the failure codes.
383 **/
384static int
385lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
386 struct nvme_fc_remote_port *pnvme_rport,
387 struct nvmefc_ls_req *pnvme_lsreq)
388{
389 int ret = 0;
390 struct lpfc_nvme_lport *lport;
391 struct lpfc_vport *vport;
392 struct lpfc_nodelist *ndlp;
393 struct ulp_bde64 *bpl;
394 struct lpfc_dmabuf *bmp;
395
396 /* there are two dma buf in the request, actually there is one and
397 * the second one is just the start address + cmd size.
398 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
399 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
400 * because the nvem layer owns the data bufs.
401 * We do not have to break these packets open, we don't care what is in
402 * them. And we do not have to look at the resonse data, we only care
403 * that we got a response. All of the caring is going to happen in the
404 * nvme-fc layer.
405 */
406
407 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
408 vport = lport->vport;
409
410 ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
411 if (!ndlp) {
412 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
413 "6043 Could not find node for DID %x\n",
414 pnvme_rport->port_id);
415 return 1;
416 }
417 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
418 if (!bmp) {
419
420 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
421 "6044 Could not find node for DID %x\n",
422 pnvme_rport->port_id);
423 return 2;
424 }
425 INIT_LIST_HEAD(&bmp->list);
426 bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
427 if (!bmp->virt) {
428 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
429 "6042 Could not find node for DID %x\n",
430 pnvme_rport->port_id);
431 kfree(bmp);
432 return 3;
433 }
434 bpl = (struct ulp_bde64 *)bmp->virt;
435 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
436 bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
437 bpl->tus.f.bdeFlags = 0;
438 bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
439 bpl->tus.w = le32_to_cpu(bpl->tus.w);
440 bpl++;
441
442 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
443 bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
444 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
445 bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
446 bpl->tus.w = le32_to_cpu(bpl->tus.w);
447
448 /* Expand print to include key fields. */
449 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
450 "6051 ENTER. lport %p, rport %p lsreq%p rqstlen:%d "
451 "rsplen:%d %llux %llux\n",
452 pnvme_lport, pnvme_rport,
453 pnvme_lsreq, pnvme_lsreq->rqstlen,
454 pnvme_lsreq->rsplen, pnvme_lsreq->rqstdma,
455 pnvme_lsreq->rspdma);
456
457 vport->phba->fc4NvmeLsRequests++;
458
459 /* Hardcode the wait to 30 seconds. Connections are failing otherwise.
460 * This code allows it all to work.
461 */
462 ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
463 pnvme_lsreq, lpfc_nvme_cmpl_gen_req,
464 ndlp, 2, 30, 0);
465 if (ret != WQE_SUCCESS) {
466 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
467 "6052 EXIT. issue ls wqe failed lport %p, "
468 "rport %p lsreq%p Status %x DID %x\n",
469 pnvme_lport, pnvme_rport, pnvme_lsreq,
470 ret, ndlp->nlp_DID);
471 lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
472 kfree(bmp);
473 return ret;
474 }
475
476 /* Stub in routine and return 0 for now. */
477 return ret;
478}
479
480/**
481 * lpfc_nvme_ls_abort - Issue an Link Service request
482 * @lpfc_pnvme: Pointer to the driver's nvme instance data
483 * @lpfc_nvme_lport: Pointer to the driver's local port data
484 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
485 *
486 * Driver registers this routine to handle any link service request
487 * from the nvme_fc transport to a remote nvme-aware port.
488 *
489 * Return value :
490 * 0 - Success
491 * TODO: What are the failure codes.
492 **/
493static void
494lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
495 struct nvme_fc_remote_port *pnvme_rport,
496 struct nvmefc_ls_req *pnvme_lsreq)
497{
498 struct lpfc_nvme_lport *lport;
499 struct lpfc_vport *vport;
500 struct lpfc_hba *phba;
501 struct lpfc_nodelist *ndlp;
502 LIST_HEAD(abort_list);
503 struct lpfc_sli_ring *pring;
504 struct lpfc_iocbq *wqe, *next_wqe;
505
506 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
507 vport = lport->vport;
508 phba = vport->phba;
509
510 ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
511 if (!ndlp) {
512 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
513 "6049 Could not find node for DID %x\n",
514 pnvme_rport->port_id);
515 return;
516 }
517
518 /* Expand print to include key fields. */
519 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
520 "6040 ENTER. lport %p, rport %p lsreq %p rqstlen:%d "
521 "rsplen:%d %llux %llux\n",
522 pnvme_lport, pnvme_rport,
523 pnvme_lsreq, pnvme_lsreq->rqstlen,
524 pnvme_lsreq->rsplen, pnvme_lsreq->rqstdma,
525 pnvme_lsreq->rspdma);
526
527 /*
528 * Lock the ELS ring txcmplq and build a local list of all ELS IOs
529 * that need an ABTS. The IOs need to stay on the txcmplq so that
530 * the abort operation completes them successfully.
531 */
532 pring = phba->sli4_hba.nvmels_wq->pring;
533 spin_lock_irq(&phba->hbalock);
534 spin_lock(&pring->ring_lock);
535 list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
536 /* Add to abort_list on on NDLP match. */
537 if (lpfc_check_sli_ndlp(phba, pring, wqe, ndlp)) {
538 wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
539 list_add_tail(&wqe->dlist, &abort_list);
540 }
541 }
542 spin_unlock(&pring->ring_lock);
543 spin_unlock_irq(&phba->hbalock);
544
545 /* Abort the targeted IOs and remove them from the abort list. */
546 list_for_each_entry_safe(wqe, next_wqe, &abort_list, dlist) {
547 spin_lock_irq(&phba->hbalock);
548 list_del_init(&wqe->dlist);
549 lpfc_sli_issue_abort_iotag(phba, pring, wqe);
550 spin_unlock_irq(&phba->hbalock);
551 }
552}
553
554/* Fix up the existing sgls for NVME IO. */
555static void
556lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
557 struct lpfc_nvme_buf *lpfc_ncmd,
558 struct nvmefc_fcp_req *nCmd)
559{
560 struct sli4_sge *sgl;
561 union lpfc_wqe128 *wqe;
562 uint32_t *wptr, *dptr;
563
564 /*
565 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
566 * match NVME. NVME sends 96 bytes. Also, use the
567 * nvme commands command and response dma addresses
568 * rather than the virtual memory to ease the restore
569 * operation.
570 */
571 sgl = lpfc_ncmd->nvme_sgl;
572 sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
573
574 sgl++;
575
576 /* Setup the physical region for the FCP RSP */
577 sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
578 sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
579 sgl->word2 = le32_to_cpu(sgl->word2);
580 if (nCmd->sg_cnt)
581 bf_set(lpfc_sli4_sge_last, sgl, 0);
582 else
583 bf_set(lpfc_sli4_sge_last, sgl, 1);
584 sgl->word2 = cpu_to_le32(sgl->word2);
585 sgl->sge_len = cpu_to_le32(nCmd->rsplen);
586
587 /*
588 * Get a local pointer to the built-in wqe and correct
589 * the cmd size to match NVME's 96 bytes and fix
590 * the dma address.
591 */
592
593 /* 128 byte wqe support here */
594 wqe = (union lpfc_wqe128 *)&lpfc_ncmd->cur_iocbq.wqe;
595
596 /* Word 0-2 - NVME CMND IU (embedded payload) */
597 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
598 wqe->generic.bde.tus.f.bdeSize = 60;
599 wqe->generic.bde.addrHigh = 0;
600 wqe->generic.bde.addrLow = 64; /* Word 16 */
601
602 /* Word 3 */
603 bf_set(payload_offset_len, &wqe->fcp_icmd,
604 (nCmd->rsplen + nCmd->cmdlen));
605
606 /* Word 10 */
607 bf_set(wqe_nvme, &wqe->fcp_icmd.wqe_com, 1);
608 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
609
610 /*
611 * Embed the payload in the last half of the WQE
612 * WQE words 16-30 get the NVME CMD IU payload
613 *
614 * WQE Word 16 is already setup with flags
615 * WQE words 17-19 get payload Words 2-4
616 * WQE words 20-21 get payload Words 6-7
617 * WQE words 22-29 get payload Words 16-23
618 */
619 wptr = &wqe->words[17]; /* WQE ptr */
620 dptr = (uint32_t *)nCmd->cmdaddr; /* payload ptr */
621 dptr += 2; /* Skip Words 0-1 in payload */
622
623 *wptr++ = *dptr++; /* Word 2 */
624 *wptr++ = *dptr++; /* Word 3 */
625 *wptr++ = *dptr++; /* Word 4 */
626 dptr++; /* Skip Word 5 in payload */
627 *wptr++ = *dptr++; /* Word 6 */
628 *wptr++ = *dptr++; /* Word 7 */
629 dptr += 8; /* Skip Words 8-15 in payload */
630 *wptr++ = *dptr++; /* Word 16 */
631 *wptr++ = *dptr++; /* Word 17 */
632 *wptr++ = *dptr++; /* Word 18 */
633 *wptr++ = *dptr++; /* Word 19 */
634 *wptr++ = *dptr++; /* Word 20 */
635 *wptr++ = *dptr++; /* Word 21 */
636 *wptr++ = *dptr++; /* Word 22 */
637 *wptr = *dptr; /* Word 23 */
638}
639
640/**
641 * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
642 * @lpfc_pnvme: Pointer to the driver's nvme instance data
643 * @lpfc_nvme_lport: Pointer to the driver's local port data
644 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
645 *
646 * Driver registers this routine as it io request handler. This
647 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
648 * data structure to the rport indicated in @lpfc_nvme_rport.
649 *
650 * Return value :
651 * 0 - Success
652 * TODO: What are the failure codes.
653 **/
654static void
655lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
656 struct lpfc_wcqe_complete *wcqe)
657{
658 struct lpfc_nvme_buf *lpfc_ncmd =
659 (struct lpfc_nvme_buf *)pwqeIn->context1;
660 struct lpfc_vport *vport = pwqeIn->vport;
661 struct nvmefc_fcp_req *nCmd;
662 struct nvme_fc_ersp_iu *ep;
663 struct nvme_fc_cmd_iu *cp;
664 struct lpfc_nvme_rport *rport;
665 struct lpfc_nodelist *ndlp;
666 unsigned long flags;
667 uint32_t code;
668 uint16_t cid, sqhd, data;
669 uint32_t *ptr;
670
671 /* Sanity check on return of outstanding command */
672 if (!lpfc_ncmd || !lpfc_ncmd->nvmeCmd || !lpfc_ncmd->nrport) {
673 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
674 "6071 Completion pointers bad on wqe %p.\n",
675 wcqe);
676 return;
677 }
678 phba->fc4NvmeIoCmpls++;
679
680 nCmd = lpfc_ncmd->nvmeCmd;
681 rport = lpfc_ncmd->nrport;
682
683 /*
684 * Catch race where our node has transitioned, but the
685 * transport is still transitioning.
686 */
687 ndlp = rport->ndlp;
688 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
689 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
690 "6061 rport %p, ndlp %p, DID x%06x ndlp "
691 "not ready.\n",
692 rport, ndlp, rport->remoteport->port_id);
693
694 ndlp = lpfc_findnode_did(vport, rport->remoteport->port_id);
695 if (!ndlp) {
696 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
697 "6062 Ignoring NVME cmpl. No ndlp\n");
698 goto out_err;
699 }
700 }
701
702 code = bf_get(lpfc_wcqe_c_code, wcqe);
703 if (code == CQE_CODE_NVME_ERSP) {
704 /* For this type of CQE, we need to rebuild the rsp */
705 ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
706
707 /*
708 * Get Command Id from cmd to plug into response. This
709 * code is not needed in the next NVME Transport drop.
710 */
711 cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
712 cid = cp->sqe.common.command_id;
713
714 /*
715 * RSN is in CQE word 2
716 * SQHD is in CQE Word 3 bits 15:0
717 * Cmd Specific info is in CQE Word 1
718 * and in CQE Word 0 bits 15:0
719 */
720 sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
721
722 /* Now lets build the NVME ERSP IU */
723 ep->iu_len = cpu_to_be16(8);
724 ep->rsn = wcqe->parameter;
725 ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
726 ep->rsvd12 = 0;
727 ptr = (uint32_t *)&ep->cqe.result.u64;
728 *ptr++ = wcqe->total_data_placed;
729 data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
730 *ptr = (uint32_t)data;
731 ep->cqe.sq_head = sqhd;
732 ep->cqe.sq_id = nCmd->sqid;
733 ep->cqe.command_id = cid;
734 ep->cqe.status = 0;
735
736 lpfc_ncmd->status = IOSTAT_SUCCESS;
737 lpfc_ncmd->result = 0;
738 nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
739 nCmd->transferred_length = nCmd->payload_length;
740 } else {
741 lpfc_ncmd->status = (bf_get(lpfc_wcqe_c_status, wcqe) &
742 LPFC_IOCB_STATUS_MASK);
743 lpfc_ncmd->result = wcqe->parameter;
744
745 /* For NVME, the only failure path that results in an
746 * IO error is when the adapter rejects it. All other
747 * conditions are a success case and resolved by the
748 * transport.
749 * IOSTAT_FCP_RSP_ERROR means:
750 * 1. Length of data received doesn't match total
751 * transfer length in WQE
752 * 2. If the RSP payload does NOT match these cases:
753 * a. RSP length 12/24 bytes and all zeros
754 * b. NVME ERSP
755 */
756 switch (lpfc_ncmd->status) {
757 case IOSTAT_SUCCESS:
758 nCmd->transferred_length = wcqe->total_data_placed;
759 nCmd->rcv_rsplen = 0;
760 nCmd->status = 0;
761 break;
762 case IOSTAT_FCP_RSP_ERROR:
763 nCmd->transferred_length = wcqe->total_data_placed;
764 nCmd->rcv_rsplen = wcqe->parameter;
765 nCmd->status = 0;
766 /* Sanity check */
767 if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
768 break;
769 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
770 "6081 NVME Completion Protocol Error: "
771 "status x%x result x%x placed x%x\n",
772 lpfc_ncmd->status, lpfc_ncmd->result,
773 wcqe->total_data_placed);
774 break;
775 default:
776out_err:
777 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
778 "6072 NVME Completion Error: "
779 "status x%x result x%x placed x%x\n",
780 lpfc_ncmd->status, lpfc_ncmd->result,
781 wcqe->total_data_placed);
782 nCmd->transferred_length = 0;
783 nCmd->rcv_rsplen = 0;
784 nCmd->status = NVME_SC_FC_TRANSPORT_ERROR;
785 }
786 }
787
788 /* pick up SLI4 exhange busy condition */
789 if (bf_get(lpfc_wcqe_c_xb, wcqe))
790 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
791 else
792 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
793
794 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
795 atomic_dec(&ndlp->cmd_pending);
796
797 /* Update stats and complete the IO. There is
798 * no need for dma unprep because the nvme_transport
799 * owns the dma address.
800 */
801 nCmd->done(nCmd);
802
803 spin_lock_irqsave(&phba->hbalock, flags);
804 lpfc_ncmd->nrport = NULL;
805 spin_unlock_irqrestore(&phba->hbalock, flags);
806
807 lpfc_release_nvme_buf(phba, lpfc_ncmd);
808}
809
810
811/**
812 * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
813 * @lpfc_pnvme: Pointer to the driver's nvme instance data
814 * @lpfc_nvme_lport: Pointer to the driver's local port data
815 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
816 * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
817 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
818 *
819 * Driver registers this routine as it io request handler. This
820 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
821 * data structure to the rport indicated in @lpfc_nvme_rport.
822 *
823 * Return value :
824 * 0 - Success
825 * TODO: What are the failure codes.
826 **/
827static int
828lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
829 struct lpfc_nvme_buf *lpfc_ncmd,
830 struct lpfc_nodelist *pnode)
831{
832 struct lpfc_hba *phba = vport->phba;
833 struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
834 struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
835 union lpfc_wqe128 *wqe = (union lpfc_wqe128 *)&pwqeq->wqe;
836 uint32_t req_len;
837
838 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
839 return -EINVAL;
840
841 /*
842 * There are three possibilities here - use scatter-gather segment, use
843 * the single mapping, or neither.
844 */
845 wqe->fcp_iwrite.initial_xfer_len = 0;
846 if (nCmd->sg_cnt) {
847 if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
848 /* Word 5 */
849 if ((phba->cfg_nvme_enable_fb) &&
850 (pnode->nlp_flag & NLP_FIRSTBURST)) {
851 req_len = lpfc_ncmd->nvmeCmd->payload_length;
852 if (req_len < pnode->nvme_fb_size)
853 wqe->fcp_iwrite.initial_xfer_len =
854 req_len;
855 else
856 wqe->fcp_iwrite.initial_xfer_len =
857 pnode->nvme_fb_size;
858 }
859
860 /* Word 7 */
861 bf_set(wqe_cmnd, &wqe->generic.wqe_com,
862 CMD_FCP_IWRITE64_WQE);
863 bf_set(wqe_pu, &wqe->generic.wqe_com,
864 PARM_READ_CHECK);
865
866 /* Word 10 */
867 bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
868 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com,
869 LPFC_WQE_IOD_WRITE);
870 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
871 LPFC_WQE_LENLOC_WORD4);
872 if (phba->cfg_nvme_oas)
873 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
874
875 /* Word 11 */
876 bf_set(wqe_cmd_type, &wqe->generic.wqe_com,
877 NVME_WRITE_CMD);
878
879 /* Word 16 */
880 wqe->words[16] = LPFC_NVME_EMBED_WRITE;
881
882 phba->fc4NvmeOutputRequests++;
883 } else {
884 /* Word 7 */
885 bf_set(wqe_cmnd, &wqe->generic.wqe_com,
886 CMD_FCP_IREAD64_WQE);
887 bf_set(wqe_pu, &wqe->generic.wqe_com,
888 PARM_READ_CHECK);
889
890 /* Word 10 */
891 bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
892 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com,
893 LPFC_WQE_IOD_READ);
894 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
895 LPFC_WQE_LENLOC_WORD4);
896 if (phba->cfg_nvme_oas)
897 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
898
899 /* Word 11 */
900 bf_set(wqe_cmd_type, &wqe->generic.wqe_com,
901 NVME_READ_CMD);
902
903 /* Word 16 */
904 wqe->words[16] = LPFC_NVME_EMBED_READ;
905
906 phba->fc4NvmeInputRequests++;
907 }
908 } else {
909 /* Word 4 */
910 wqe->fcp_icmd.rsrvd4 = 0;
911
912 /* Word 7 */
913 bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_FCP_ICMND64_WQE);
914 bf_set(wqe_pu, &wqe->generic.wqe_com, 0);
915
916 /* Word 10 */
917 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
918 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
919 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
920 LPFC_WQE_LENLOC_NONE);
921 if (phba->cfg_nvme_oas)
922 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
923
924 /* Word 11 */
925 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, NVME_READ_CMD);
926
927 /* Word 16 */
928 wqe->words[16] = LPFC_NVME_EMBED_CMD;
929
930 phba->fc4NvmeControlRequests++;
931 }
932 /*
933 * Finish initializing those WQE fields that are independent
934 * of the nvme_cmnd request_buffer
935 */
936
937 /* Word 6 */
938 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
939 phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
940 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
941
942 /* Word 7 */
943 /* Preserve Class data in the ndlp. */
944 bf_set(wqe_class, &wqe->generic.wqe_com,
945 (pnode->nlp_fcp_info & 0x0f));
946
947 /* Word 8 */
948 wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
949
950 /* Word 9 */
951 bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
952
953 /* Word 11 */
954 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
955
956 pwqeq->vport = vport;
957 return 0;
958}
959
960
961/**
962 * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
963 * @lpfc_pnvme: Pointer to the driver's nvme instance data
964 * @lpfc_nvme_lport: Pointer to the driver's local port data
965 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
966 * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
967 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
968 *
969 * Driver registers this routine as it io request handler. This
970 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
971 * data structure to the rport indicated in @lpfc_nvme_rport.
972 *
973 * Return value :
974 * 0 - Success
975 * TODO: What are the failure codes.
976 **/
977static int
978lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
979 struct lpfc_nvme_buf *lpfc_ncmd)
980{
981 struct lpfc_hba *phba = vport->phba;
982 struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
983 union lpfc_wqe128 *wqe = (union lpfc_wqe128 *)&lpfc_ncmd->cur_iocbq.wqe;
984 struct sli4_sge *sgl = lpfc_ncmd->nvme_sgl;
985 struct scatterlist *data_sg;
986 struct sli4_sge *first_data_sgl;
987 dma_addr_t physaddr;
988 uint32_t num_bde = 0;
989 uint32_t dma_len;
990 uint32_t dma_offset = 0;
991 int nseg, i;
992
993 /* Fix up the command and response DMA stuff. */
994 lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
995
996 /*
997 * There are three possibilities here - use scatter-gather segment, use
998 * the single mapping, or neither.
999 */
1000 if (nCmd->sg_cnt) {
1001 /*
1002 * Jump over the cmd and rsp SGEs. The fix routine
1003 * has already adjusted for this.
1004 */
1005 sgl += 2;
1006
1007 first_data_sgl = sgl;
1008 lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
1009 if (lpfc_ncmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1010 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1011 "6058 Too many sg segments from "
1012 "NVME Transport. Max %d, "
1013 "nvmeIO sg_cnt %d\n",
1014 phba->cfg_sg_seg_cnt,
1015 lpfc_ncmd->seg_cnt);
1016 lpfc_ncmd->seg_cnt = 0;
1017 return 1;
1018 }
1019
1020 /*
1021 * The driver established a maximum scatter-gather segment count
1022 * during probe that limits the number of sg elements in any
1023 * single nvme command. Just run through the seg_cnt and format
1024 * the sge's.
1025 */
1026 nseg = nCmd->sg_cnt;
1027 data_sg = nCmd->first_sgl;
1028 for (i = 0; i < nseg; i++) {
1029 if (data_sg == NULL) {
1030 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1031 "6059 dptr err %d, nseg %d\n",
1032 i, nseg);
1033 lpfc_ncmd->seg_cnt = 0;
1034 return 1;
1035 }
1036 physaddr = data_sg->dma_address;
1037 dma_len = data_sg->length;
1038 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1039 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1040 sgl->word2 = le32_to_cpu(sgl->word2);
1041 if ((num_bde + 1) == nseg)
1042 bf_set(lpfc_sli4_sge_last, sgl, 1);
1043 else
1044 bf_set(lpfc_sli4_sge_last, sgl, 0);
1045 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1046 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
1047 sgl->word2 = cpu_to_le32(sgl->word2);
1048 sgl->sge_len = cpu_to_le32(dma_len);
1049
1050 dma_offset += dma_len;
1051 data_sg = sg_next(data_sg);
1052 sgl++;
1053 }
1054 } else {
1055 /* For this clause to be valid, the payload_length
1056 * and sg_cnt must zero.
1057 */
1058 if (nCmd->payload_length != 0) {
1059 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1060 "6063 NVME DMA Prep Err: sg_cnt %d "
1061 "payload_length x%x\n",
1062 nCmd->sg_cnt, nCmd->payload_length);
1063 return 1;
1064 }
1065 }
1066
1067 /*
1068 * Due to difference in data length between DIF/non-DIF paths,
1069 * we need to set word 4 of WQE here
1070 */
1071 wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
1072 return 0;
1073}
1074
1075/**
1076 * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
1077 * @lpfc_pnvme: Pointer to the driver's nvme instance data
1078 * @lpfc_nvme_lport: Pointer to the driver's local port data
1079 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1080 * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1081 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1082 *
1083 * Driver registers this routine as it io request handler. This
1084 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1085 * data structure to the rport
1086 indicated in @lpfc_nvme_rport.
1087 *
1088 * Return value :
1089 * 0 - Success
1090 * TODO: What are the failure codes.
1091 **/
1092static int
1093lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
1094 struct nvme_fc_remote_port *pnvme_rport,
1095 void *hw_queue_handle,
1096 struct nvmefc_fcp_req *pnvme_fcreq)
1097{
1098 int ret = 0;
1099 struct lpfc_nvme_lport *lport;
1100 struct lpfc_vport *vport;
1101 struct lpfc_hba *phba;
1102 struct lpfc_nodelist *ndlp;
1103 struct lpfc_nvme_buf *lpfc_ncmd;
1104 struct lpfc_nvme_rport *rport;
1105 struct lpfc_nvme_qhandle *lpfc_queue_info;
1106
1107 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1108 vport = lport->vport;
1109 phba = vport->phba;
1110
1111 rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
1112 lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
1113
1114 /*
1115 * Catch race where our node has transitioned, but the
1116 * transport is still transitioning.
1117 */
1118 ndlp = rport->ndlp;
1119 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1120 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
1121 "6053 rport %p, ndlp %p, DID x%06x "
1122 "ndlp not ready.\n",
1123 rport, ndlp, pnvme_rport->port_id);
1124
1125 ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
1126 if (!ndlp) {
1127 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1128 "6066 Missing node for DID %x\n",
1129 pnvme_rport->port_id);
1130 ret = -ENODEV;
1131 goto out_fail;
1132 }
1133 }
1134
1135 /* The remote node has to be a mapped target or it's an error. */
1136 if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
1137 (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
1138 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
1139 "6036 rport %p, DID x%06x not ready for "
1140 "IO. State x%x, Type x%x\n",
1141 rport, pnvme_rport->port_id,
1142 ndlp->nlp_state, ndlp->nlp_type);
1143 ret = -ENODEV;
1144 goto out_fail;
1145
1146 }
1147
1148 /* The node is shared with FCP IO, make sure the IO pending count does
1149 * not exceed the programmed depth.
1150 */
1151 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
1152 ret = -EAGAIN;
1153 goto out_fail;
1154 }
1155
1156 lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp);
1157 if (lpfc_ncmd == NULL) {
1158 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1159 "6065 driver's buffer pool is empty, "
1160 "IO failed\n");
1161 ret = -ENOMEM;
1162 goto out_fail;
1163 }
1164
1165 /*
1166 * Store the data needed by the driver to issue, abort, and complete
1167 * an IO.
1168 * Do not let the IO hang out forever. There is no midlayer issuing
1169 * an abort so inform the FW of the maximum IO pending time.
1170 */
1171 pnvme_fcreq->private = (void *)lpfc_ncmd;
1172 lpfc_ncmd->nvmeCmd = pnvme_fcreq;
1173 lpfc_ncmd->nrport = rport;
1174 lpfc_ncmd->start_time = jiffies;
1175
1176 lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp);
1177 ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
1178 if (ret) {
1179 ret = -ENOMEM;
1180 goto out_free_nvme_buf;
1181 }
1182
1183 atomic_inc(&ndlp->cmd_pending);
1184
1185 /*
1186 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
1187 * This identfier was create in our hardware queue create callback
1188 * routine. The driver now is dependent on the IO queue steering from
1189 * the transport. We are trusting the upper NVME layers know which
1190 * index to use and that they have affinitized a CPU to this hardware
1191 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
1192 */
1193 lpfc_ncmd->cur_iocbq.hba_wqidx = lpfc_queue_info->index;
1194
1195 ret = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, &lpfc_ncmd->cur_iocbq);
1196 if (ret) {
1197 atomic_dec(&ndlp->cmd_pending);
1198 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1199 "6113 FCP could not issue WQE err %x "
1200 "sid: x%x did: x%x oxid: x%x\n",
1201 ret, vport->fc_myDID, ndlp->nlp_DID,
1202 lpfc_ncmd->cur_iocbq.sli4_xritag);
1203 ret = -EINVAL;
1204 goto out_free_nvme_buf;
1205 }
1206
1207 return 0;
1208
1209 out_free_nvme_buf:
1210 lpfc_release_nvme_buf(phba, lpfc_ncmd);
1211 out_fail:
1212 return ret;
1213}
1214
1215/**
1216 * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
1217 * @phba: Pointer to HBA context object
1218 * @cmdiocb: Pointer to command iocb object.
1219 * @rspiocb: Pointer to response iocb object.
1220 *
1221 * This is the callback function for any NVME FCP IO that was aborted.
1222 *
1223 * Return value:
1224 * None
1225 **/
1226void
1227lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1228 struct lpfc_wcqe_complete *abts_cmpl)
1229{
1230 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1231 "6145 ABORT_XRI_CN completing on rpi x%x "
1232 "original iotag x%x, abort cmd iotag x%x "
1233 "req_tag x%x, status x%x, hwstatus x%x\n",
1234 cmdiocb->iocb.un.acxri.abortContextTag,
1235 cmdiocb->iocb.un.acxri.abortIoTag,
1236 cmdiocb->iotag,
1237 bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
1238 bf_get(lpfc_wcqe_c_status, abts_cmpl),
1239 bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
1240 lpfc_sli_release_iocbq(phba, cmdiocb);
1241}
1242
1243/**
1244 * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
1245 * @lpfc_pnvme: Pointer to the driver's nvme instance data
1246 * @lpfc_nvme_lport: Pointer to the driver's local port data
1247 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1248 * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1249 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1250 *
1251 * Driver registers this routine as its nvme request io abort handler. This
1252 * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
1253 * data structure to the rport indicated in @lpfc_nvme_rport. This routine
1254 * is executed asynchronously - one the target is validated as "MAPPED" and
1255 * ready for IO, the driver issues the abort request and returns.
1256 *
1257 * Return value:
1258 * None
1259 **/
1260static void
1261lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
1262 struct nvme_fc_remote_port *pnvme_rport,
1263 void *hw_queue_handle,
1264 struct nvmefc_fcp_req *pnvme_fcreq)
1265{
1266 struct lpfc_nvme_lport *lport;
1267 struct lpfc_vport *vport;
1268 struct lpfc_hba *phba;
1269 struct lpfc_nodelist *ndlp;
1270 struct lpfc_nvme_rport *rport;
1271 struct lpfc_nvme_buf *lpfc_nbuf;
1272 struct lpfc_iocbq *abts_buf;
1273 struct lpfc_iocbq *nvmereq_wqe;
1274 union lpfc_wqe *abts_wqe;
1275 unsigned long flags;
1276 int ret_val;
1277
1278 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1279 rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
1280 vport = lport->vport;
1281 phba = vport->phba;
1282
1283 /* Announce entry to new IO submit field. */
1284 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1285 "6002 Abort Request to rport DID x%06x "
1286 "for nvme_fc_req %p\n",
1287 pnvme_rport->port_id,
1288 pnvme_fcreq);
1289
1290 /*
1291 * Catch race where our node has transitioned, but the
1292 * transport is still transitioning.
1293 */
1294 ndlp = rport->ndlp;
1295 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1296 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_ABTS,
1297 "6054 rport %p, ndlp %p, DID x%06x ndlp "
1298 " not ready.\n",
1299 rport, ndlp, pnvme_rport->port_id);
1300
1301 ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
1302 if (!ndlp) {
1303 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1304 "6055 Could not find node for "
1305 "DID %x\n",
1306 pnvme_rport->port_id);
1307 return;
1308 }
1309 }
1310
1311 /* The remote node has to be ready to send an abort. */
1312 if ((ndlp->nlp_state != NLP_STE_MAPPED_NODE) &&
1313 !(ndlp->nlp_type & NLP_NVME_TARGET)) {
1314 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_ABTS,
1315 "6048 rport %p, DID x%06x not ready for "
1316 "IO. State x%x, Type x%x\n",
1317 rport, pnvme_rport->port_id,
1318 ndlp->nlp_state, ndlp->nlp_type);
1319 return;
1320 }
1321
1322 /* If the hba is getting reset, this flag is set. It is
1323 * cleared when the reset is complete and rings reestablished.
1324 */
1325 spin_lock_irqsave(&phba->hbalock, flags);
1326 /* driver queued commands are in process of being flushed */
1327 if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
1328 spin_unlock_irqrestore(&phba->hbalock, flags);
1329 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
1330 "6139 Driver in reset cleanup - flushing "
1331 "NVME Req now. hba_flag x%x\n",
1332 phba->hba_flag);
1333 return;
1334 }
1335
1336 lpfc_nbuf = (struct lpfc_nvme_buf *)pnvme_fcreq->private;
1337 if (!lpfc_nbuf) {
1338 spin_unlock_irqrestore(&phba->hbalock, flags);
1339 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
1340 "6140 NVME IO req has no matching lpfc nvme "
1341 "io buffer. Skipping abort req.\n");
1342 return;
1343 } else if (!lpfc_nbuf->nvmeCmd) {
1344 spin_unlock_irqrestore(&phba->hbalock, flags);
1345 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
1346 "6141 lpfc NVME IO req has no nvme_fcreq "
1347 "io buffer. Skipping abort req.\n");
1348 return;
1349 }
1350
1351 /*
1352 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
1353 * state must match the nvme_fcreq passed by the nvme
1354 * transport. If they don't match, it is likely the driver
1355 * has already completed the NVME IO and the nvme transport
1356 * has not seen it yet.
1357 */
1358 if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
1359 spin_unlock_irqrestore(&phba->hbalock, flags);
1360 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
1361 "6143 NVME req mismatch: "
1362 "lpfc_nbuf %p nvmeCmd %p, "
1363 "pnvme_fcreq %p. Skipping Abort\n",
1364 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
1365 pnvme_fcreq);
1366 return;
1367 }
1368
1369 /* Don't abort IOs no longer on the pending queue. */
1370 nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
1371 if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
1372 spin_unlock_irqrestore(&phba->hbalock, flags);
1373 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
1374 "6142 NVME IO req %p not queued - skipping "
1375 "abort req\n",
1376 pnvme_fcreq);
1377 return;
1378 }
1379
1380 /* Outstanding abort is in progress */
1381 if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
1382 spin_unlock_irqrestore(&phba->hbalock, flags);
1383 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
1384 "6144 Outstanding NVME I/O Abort Request "
1385 "still pending on nvme_fcreq %p, "
1386 "lpfc_ncmd %p\n",
1387 pnvme_fcreq, lpfc_nbuf);
1388 return;
1389 }
1390
1391 abts_buf = __lpfc_sli_get_iocbq(phba);
1392 if (!abts_buf) {
1393 spin_unlock_irqrestore(&phba->hbalock, flags);
1394 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
1395 "6136 No available abort wqes. Skipping "
1396 "Abts req for nvme_fcreq %p.\n",
1397 pnvme_fcreq);
1398 return;
1399 }
1400
1401 /* Ready - mark outstanding as aborted by driver. */
1402 nvmereq_wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
1403
1404 /* Complete prepping the abort wqe and issue to the FW. */
1405 abts_wqe = &abts_buf->wqe;
1406
1407 /* WQEs are reused. Clear stale data and set key fields to
1408 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
1409 */
1410 memset(abts_wqe, 0, sizeof(union lpfc_wqe));
1411 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
1412
1413 /* word 7 */
1414 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
1415 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
1416 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
1417 nvmereq_wqe->iocb.ulpClass);
1418
1419 /* word 8 - tell the FW to abort the IO associated with this
1420 * outstanding exchange ID.
1421 */
1422 abts_wqe->abort_cmd.wqe_com.abort_tag = nvmereq_wqe->sli4_xritag;
1423
1424 /* word 9 - this is the iotag for the abts_wqe completion. */
1425 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
1426 abts_buf->iotag);
1427
1428 /* word 10 */
1429 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, nvmereq_wqe->hba_wqidx);
1430 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
1431 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
1432
1433 /* word 11 */
1434 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
1435 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
1436 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1437
1438 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
1439 abts_buf->iocb_flag |= LPFC_IO_NVME;
1440 abts_buf->hba_wqidx = nvmereq_wqe->hba_wqidx;
1441 abts_buf->vport = vport;
1442 abts_buf->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
1443 ret_val = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_buf);
1444 spin_unlock_irqrestore(&phba->hbalock, flags);
1445 if (ret_val == IOCB_ERROR) {
1446 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
1447 "6137 Failed abts issue_wqe with status x%x "
1448 "for nvme_fcreq %p.\n",
1449 ret_val, pnvme_fcreq);
1450 lpfc_sli_release_iocbq(phba, abts_buf);
1451 return;
1452 }
1453
1454 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
1455 "6138 Transport Abort NVME Request Issued for\n"
1456 "ox_id x%x on reqtag x%x\n",
1457 nvmereq_wqe->sli4_xritag,
1458 abts_buf->iotag);
1459}
1460
1461/* Declare and initialization an instance of the FC NVME template. */
1462static struct nvme_fc_port_template lpfc_nvme_template = {
1463 /* initiator-based functions */
1464 .localport_delete = lpfc_nvme_localport_delete,
1465 .remoteport_delete = lpfc_nvme_remoteport_delete,
1466 .create_queue = lpfc_nvme_create_queue,
1467 .delete_queue = lpfc_nvme_delete_queue,
1468 .ls_req = lpfc_nvme_ls_req,
1469 .fcp_io = lpfc_nvme_fcp_io_submit,
1470 .ls_abort = lpfc_nvme_ls_abort,
1471 .fcp_abort = lpfc_nvme_fcp_abort,
1472
1473 .max_hw_queues = 1,
1474 .max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
1475 .max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
1476 .dma_boundary = 0xFFFFFFFF,
1477
1478 /* Sizes of additional private data for data structures.
1479 * No use for the last two sizes at this time.
1480 */
1481 .local_priv_sz = sizeof(struct lpfc_nvme_lport),
1482 .remote_priv_sz = sizeof(struct lpfc_nvme_rport),
1483 .lsrqst_priv_sz = 0,
1484 .fcprqst_priv_sz = 0,
1485};
1486
1487/**
1488 * lpfc_sli4_post_nvme_sgl_block - post a block of nvme sgl list to firmware
1489 * @phba: pointer to lpfc hba data structure.
1490 * @nblist: pointer to nvme buffer list.
1491 * @count: number of scsi buffers on the list.
1492 *
1493 * This routine is invoked to post a block of @count scsi sgl pages from a
1494 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
1495 * No Lock is held.
1496 *
1497 **/
1498static int
1499lpfc_sli4_post_nvme_sgl_block(struct lpfc_hba *phba,
1500 struct list_head *nblist,
1501 int count)
1502{
1503 struct lpfc_nvme_buf *lpfc_ncmd;
1504 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
1505 struct sgl_page_pairs *sgl_pg_pairs;
1506 void *viraddr;
1507 LPFC_MBOXQ_t *mbox;
1508 uint32_t reqlen, alloclen, pg_pairs;
1509 uint32_t mbox_tmo;
1510 uint16_t xritag_start = 0;
1511 int rc = 0;
1512 uint32_t shdr_status, shdr_add_status;
1513 dma_addr_t pdma_phys_bpl1;
1514 union lpfc_sli4_cfg_shdr *shdr;
1515
1516 /* Calculate the requested length of the dma memory */
1517 reqlen = count * sizeof(struct sgl_page_pairs) +
1518 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
1519 if (reqlen > SLI4_PAGE_SIZE) {
1520 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1521 "6118 Block sgl registration required DMA "
1522 "size (%d) great than a page\n", reqlen);
1523 return -ENOMEM;
1524 }
1525 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1526 if (!mbox) {
1527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1528 "6119 Failed to allocate mbox cmd memory\n");
1529 return -ENOMEM;
1530 }
1531
1532 /* Allocate DMA memory and set up the non-embedded mailbox command */
1533 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
1534 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
1535 LPFC_SLI4_MBX_NEMBED);
1536
1537 if (alloclen < reqlen) {
1538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1539 "6120 Allocated DMA memory size (%d) is "
1540 "less than the requested DMA memory "
1541 "size (%d)\n", alloclen, reqlen);
1542 lpfc_sli4_mbox_cmd_free(phba, mbox);
1543 return -ENOMEM;
1544 }
1545
1546 /* Get the first SGE entry from the non-embedded DMA memory */
1547 viraddr = mbox->sge_array->addr[0];
1548
1549 /* Set up the SGL pages in the non-embedded DMA pages */
1550 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
1551 sgl_pg_pairs = &sgl->sgl_pg_pairs;
1552
1553 pg_pairs = 0;
1554 list_for_each_entry(lpfc_ncmd, nblist, list) {
1555 /* Set up the sge entry */
1556 sgl_pg_pairs->sgl_pg0_addr_lo =
1557 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
1558 sgl_pg_pairs->sgl_pg0_addr_hi =
1559 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
1560 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
1561 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
1562 SGL_PAGE_SIZE;
1563 else
1564 pdma_phys_bpl1 = 0;
1565 sgl_pg_pairs->sgl_pg1_addr_lo =
1566 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
1567 sgl_pg_pairs->sgl_pg1_addr_hi =
1568 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
1569 /* Keep the first xritag on the list */
1570 if (pg_pairs == 0)
1571 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
1572 sgl_pg_pairs++;
1573 pg_pairs++;
1574 }
1575 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
1576 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
1577 /* Perform endian conversion if necessary */
1578 sgl->word0 = cpu_to_le32(sgl->word0);
1579
1580 if (!phba->sli4_hba.intr_enable)
1581 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
1582 else {
1583 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
1584 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
1585 }
1586 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
1587 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1588 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
1589 if (rc != MBX_TIMEOUT)
1590 lpfc_sli4_mbox_cmd_free(phba, mbox);
1591 if (shdr_status || shdr_add_status || rc) {
1592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1593 "6125 POST_SGL_BLOCK mailbox command failed "
1594 "status x%x add_status x%x mbx status x%x\n",
1595 shdr_status, shdr_add_status, rc);
1596 rc = -ENXIO;
1597 }
1598 return rc;
1599}
1600
1601/**
1602 * lpfc_post_nvme_sgl_list - Post blocks of nvme buffer sgls from a list
1603 * @phba: pointer to lpfc hba data structure.
1604 * @post_nblist: pointer to the nvme buffer list.
1605 *
1606 * This routine walks a list of nvme buffers that was passed in. It attempts
1607 * to construct blocks of nvme buffer sgls which contains contiguous xris and
1608 * uses the non-embedded SGL block post mailbox commands to post to the port.
1609 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
1610 * embedded SGL post mailbox command for posting. The @post_nblist passed in
1611 * must be local list, thus no lock is needed when manipulate the list.
1612 *
1613 * Returns: 0 = failure, non-zero number of successfully posted buffers.
1614 **/
1615static int
1616lpfc_post_nvme_sgl_list(struct lpfc_hba *phba,
1617 struct list_head *post_nblist, int sb_count)
1618{
1619 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
1620 int status, sgl_size;
1621 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
1622 dma_addr_t pdma_phys_sgl1;
1623 int last_xritag = NO_XRI;
1624 int cur_xritag;
1625 LIST_HEAD(prep_nblist);
1626 LIST_HEAD(blck_nblist);
1627 LIST_HEAD(nvme_nblist);
1628
1629 /* sanity check */
1630 if (sb_count <= 0)
1631 return -EINVAL;
1632
1633 sgl_size = phba->cfg_sg_dma_buf_size;
1634
1635 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
1636 list_del_init(&lpfc_ncmd->list);
1637 block_cnt++;
1638 if ((last_xritag != NO_XRI) &&
1639 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
1640 /* a hole in xri block, form a sgl posting block */
1641 list_splice_init(&prep_nblist, &blck_nblist);
1642 post_cnt = block_cnt - 1;
1643 /* prepare list for next posting block */
1644 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
1645 block_cnt = 1;
1646 } else {
1647 /* prepare list for next posting block */
1648 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
1649 /* enough sgls for non-embed sgl mbox command */
1650 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
1651 list_splice_init(&prep_nblist, &blck_nblist);
1652 post_cnt = block_cnt;
1653 block_cnt = 0;
1654 }
1655 }
1656 num_posting++;
1657 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
1658
1659 /* end of repost sgl list condition for NVME buffers */
1660 if (num_posting == sb_count) {
1661 if (post_cnt == 0) {
1662 /* last sgl posting block */
1663 list_splice_init(&prep_nblist, &blck_nblist);
1664 post_cnt = block_cnt;
1665 } else if (block_cnt == 1) {
1666 /* last single sgl with non-contiguous xri */
1667 if (sgl_size > SGL_PAGE_SIZE)
1668 pdma_phys_sgl1 =
1669 lpfc_ncmd->dma_phys_sgl +
1670 SGL_PAGE_SIZE;
1671 else
1672 pdma_phys_sgl1 = 0;
1673 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
1674 status = lpfc_sli4_post_sgl(phba,
1675 lpfc_ncmd->dma_phys_sgl,
1676 pdma_phys_sgl1, cur_xritag);
1677 if (status) {
1678 /* failure, put on abort nvme list */
1679 lpfc_ncmd->exch_busy = 1;
1680 } else {
1681 /* success, put on NVME buffer list */
1682 lpfc_ncmd->exch_busy = 0;
1683 lpfc_ncmd->status = IOSTAT_SUCCESS;
1684 num_posted++;
1685 }
1686 /* success, put on NVME buffer sgl list */
1687 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
1688 }
1689 }
1690
1691 /* continue until a nembed page worth of sgls */
1692 if (post_cnt == 0)
1693 continue;
1694
1695 /* post block of NVME buffer list sgls */
1696 status = lpfc_sli4_post_nvme_sgl_block(phba, &blck_nblist,
1697 post_cnt);
1698
1699 /* don't reset xirtag due to hole in xri block */
1700 if (block_cnt == 0)
1701 last_xritag = NO_XRI;
1702
1703 /* reset NVME buffer post count for next round of posting */
1704 post_cnt = 0;
1705
1706 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
1707 while (!list_empty(&blck_nblist)) {
1708 list_remove_head(&blck_nblist, lpfc_ncmd,
1709 struct lpfc_nvme_buf, list);
1710 if (status) {
1711 /* failure, put on abort nvme list */
1712 lpfc_ncmd->exch_busy = 1;
1713 } else {
1714 /* success, put on NVME buffer list */
1715 lpfc_ncmd->exch_busy = 0;
1716 lpfc_ncmd->status = IOSTAT_SUCCESS;
1717 num_posted++;
1718 }
1719 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
1720 }
1721 }
1722 /* Push NVME buffers with sgl posted to the available list */
1723 while (!list_empty(&nvme_nblist)) {
1724 list_remove_head(&nvme_nblist, lpfc_ncmd,
1725 struct lpfc_nvme_buf, list);
1726 lpfc_release_nvme_buf(phba, lpfc_ncmd);
1727 }
1728 return num_posted;
1729}
1730
1731/**
1732 * lpfc_repost_nvme_sgl_list - Repost all the allocated nvme buffer sgls
1733 * @phba: pointer to lpfc hba data structure.
1734 *
1735 * This routine walks the list of nvme buffers that have been allocated and
1736 * repost them to the port by using SGL block post. This is needed after a
1737 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
1738 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
1739 * to the lpfc_nvme_buf_list. If the repost fails, reject all nvme buffers.
1740 *
1741 * Returns: 0 = success, non-zero failure.
1742 **/
1743int
1744lpfc_repost_nvme_sgl_list(struct lpfc_hba *phba)
1745{
1746 LIST_HEAD(post_nblist);
1747 int num_posted, rc = 0;
1748
1749 /* get all NVME buffers need to repost to a local list */
1750 spin_lock_irq(&phba->nvme_buf_list_get_lock);
1751 spin_lock(&phba->nvme_buf_list_put_lock);
1752 list_splice_init(&phba->lpfc_nvme_buf_list_get, &post_nblist);
1753 list_splice(&phba->lpfc_nvme_buf_list_put, &post_nblist);
1754 spin_unlock(&phba->nvme_buf_list_put_lock);
1755 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
1756
1757 /* post the list of nvme buffer sgls to port if available */
1758 if (!list_empty(&post_nblist)) {
1759 num_posted = lpfc_post_nvme_sgl_list(phba, &post_nblist,
1760 phba->sli4_hba.nvme_xri_cnt);
1761 /* failed to post any nvme buffer, return error */
1762 if (num_posted == 0)
1763 rc = -EIO;
1764 }
1765 return rc;
1766}
1767
1768/**
1769 * lpfc_new_nvme_buf - Scsi buffer allocator for HBA with SLI4 IF spec
1770 * @vport: The virtual port for which this call being executed.
1771 * @num_to_allocate: The requested number of buffers to allocate.
1772 *
1773 * This routine allocates nvme buffers for device with SLI-4 interface spec,
1774 * the nvme buffer contains all the necessary information needed to initiate
1775 * a NVME I/O. After allocating up to @num_to_allocate NVME buffers and put
1776 * them on a list, it post them to the port by using SGL block post.
1777 *
1778 * Return codes:
1779 * int - number of nvme buffers that were allocated and posted.
1780 * 0 = failure, less than num_to_alloc is a partial failure.
1781 **/
1782static int
1783lpfc_new_nvme_buf(struct lpfc_vport *vport, int num_to_alloc)
1784{
1785 struct lpfc_hba *phba = vport->phba;
1786 struct lpfc_nvme_buf *lpfc_ncmd;
1787 struct lpfc_iocbq *pwqeq;
1788 union lpfc_wqe128 *wqe;
1789 struct sli4_sge *sgl;
1790 dma_addr_t pdma_phys_sgl;
1791 uint16_t iotag, lxri = 0;
1792 int bcnt, num_posted, sgl_size;
1793 LIST_HEAD(prep_nblist);
1794 LIST_HEAD(post_nblist);
1795 LIST_HEAD(nvme_nblist);
1796
1797 sgl_size = phba->cfg_sg_dma_buf_size;
1798
1799 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
1800 lpfc_ncmd = kzalloc(sizeof(struct lpfc_nvme_buf), GFP_KERNEL);
1801 if (!lpfc_ncmd)
1802 break;
1803 /*
1804 * Get memory from the pci pool to map the virt space to
1805 * pci bus space for an I/O. The DMA buffer includes the
1806 * number of SGE's necessary to support the sg_tablesize.
1807 */
1808 lpfc_ncmd->data = pci_pool_alloc(phba->lpfc_sg_dma_buf_pool,
1809 GFP_KERNEL,
1810 &lpfc_ncmd->dma_handle);
1811 if (!lpfc_ncmd->data) {
1812 kfree(lpfc_ncmd);
1813 break;
1814 }
1815 memset(lpfc_ncmd->data, 0, phba->cfg_sg_dma_buf_size);
1816
1817 lxri = lpfc_sli4_next_xritag(phba);
1818 if (lxri == NO_XRI) {
1819 pci_pool_free(phba->lpfc_sg_dma_buf_pool,
1820 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
1821 kfree(lpfc_ncmd);
1822 break;
1823 }
1824 pwqeq = &(lpfc_ncmd->cur_iocbq);
1825 wqe = (union lpfc_wqe128 *)&pwqeq->wqe;
1826
1827 /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
1828 iotag = lpfc_sli_next_iotag(phba, pwqeq);
1829 if (iotag == 0) {
1830 pci_pool_free(phba->lpfc_sg_dma_buf_pool,
1831 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
1832 kfree(lpfc_ncmd);
1833 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1834 "6121 Failed to allocated IOTAG for"
1835 " XRI:0x%x\n", lxri);
1836 lpfc_sli4_free_xri(phba, lxri);
1837 break;
1838 }
1839 pwqeq->sli4_lxritag = lxri;
1840 pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
1841 pwqeq->iocb_flag |= LPFC_IO_NVME;
1842 pwqeq->context1 = lpfc_ncmd;
1843 pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
1844
1845 /* Initialize local short-hand pointers. */
1846 lpfc_ncmd->nvme_sgl = lpfc_ncmd->data;
1847 sgl = lpfc_ncmd->nvme_sgl;
1848 pdma_phys_sgl = lpfc_ncmd->dma_handle;
1849 lpfc_ncmd->dma_phys_sgl = pdma_phys_sgl;
1850
1851 /* Rsp SGE will be filled in when we rcv an IO
1852 * from the NVME Layer to be sent.
1853 * The cmd is going to be embedded so we need a SKIP SGE.
1854 */
1855 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
1856 bf_set(lpfc_sli4_sge_last, sgl, 0);
1857 sgl->word2 = cpu_to_le32(sgl->word2);
1858 /* Fill in word 3 / sgl_len during cmd submission */
1859
1860 lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
1861
1862 /* Word 7 */
1863 bf_set(wqe_erp, &wqe->generic.wqe_com, 0);
1864 /* NVME upper layers will time things out, if needed */
1865 bf_set(wqe_tmo, &wqe->generic.wqe_com, 0);
1866
1867 /* Word 10 */
1868 bf_set(wqe_ebde_cnt, &wqe->generic.wqe_com, 0);
1869 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
1870
1871 /* add the nvme buffer to a post list */
1872 list_add_tail(&lpfc_ncmd->list, &post_nblist);
1873 spin_lock_irq(&phba->nvme_buf_list_get_lock);
1874 phba->sli4_hba.nvme_xri_cnt++;
1875 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
1876 }
1877 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
1878 "6114 Allocate %d out of %d requested new NVME "
1879 "buffers\n", bcnt, num_to_alloc);
1880
1881 /* post the list of nvme buffer sgls to port if available */
1882 if (!list_empty(&post_nblist))
1883 num_posted = lpfc_post_nvme_sgl_list(phba,
1884 &post_nblist, bcnt);
1885 else
1886 num_posted = 0;
1887
1888 return num_posted;
1889}
1890
1891/**
1892 * lpfc_get_nvme_buf - Get a nvme buffer from lpfc_nvme_buf_list of the HBA
1893 * @phba: The HBA for which this call is being executed.
1894 *
1895 * This routine removes a nvme buffer from head of @phba lpfc_nvme_buf_list list
1896 * and returns to caller.
1897 *
1898 * Return codes:
1899 * NULL - Error
1900 * Pointer to lpfc_nvme_buf - Success
1901 **/
1902static struct lpfc_nvme_buf *
1903lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1904{
1905 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
1906 unsigned long iflag = 0;
1907 int found = 0;
1908
1909 spin_lock_irqsave(&phba->nvme_buf_list_get_lock, iflag);
1910 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
1911 &phba->lpfc_nvme_buf_list_get, list) {
1912 if (lpfc_test_rrq_active(phba, ndlp,
1913 lpfc_ncmd->cur_iocbq.sli4_lxritag))
1914 continue;
1915 list_del(&lpfc_ncmd->list);
1916 found = 1;
1917 break;
1918 }
1919 if (!found) {
1920 spin_lock(&phba->nvme_buf_list_put_lock);
1921 list_splice(&phba->lpfc_nvme_buf_list_put,
1922 &phba->lpfc_nvme_buf_list_get);
1923 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
1924 spin_unlock(&phba->nvme_buf_list_put_lock);
1925 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
1926 &phba->lpfc_nvme_buf_list_get, list) {
1927 if (lpfc_test_rrq_active(
1928 phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
1929 continue;
1930 list_del(&lpfc_ncmd->list);
1931 found = 1;
1932 break;
1933 }
1934 }
1935 spin_unlock_irqrestore(&phba->nvme_buf_list_get_lock, iflag);
1936 if (!found)
1937 return NULL;
1938 return lpfc_ncmd;
1939}
1940
1941/**
1942 * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
1943 * @phba: The Hba for which this call is being executed.
1944 * @lpfc_ncmd: The nvme buffer which is being released.
1945 *
1946 * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
1947 * lpfc_nvme_buf_list list. For SLI4 XRI's are tied to the nvme buffer
1948 * and cannot be reused for at least RA_TOV amount of time if it was
1949 * aborted.
1950 **/
1951static void
1952lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_nvme_buf *lpfc_ncmd)
1953{
1954 unsigned long iflag = 0;
1955
1956 lpfc_ncmd->nonsg_phys = 0;
1957 if (lpfc_ncmd->exch_busy) {
1958 spin_lock_irqsave(&phba->sli4_hba.abts_nvme_buf_list_lock,
1959 iflag);
1960 lpfc_ncmd->nvmeCmd = NULL;
1961 list_add_tail(&lpfc_ncmd->list,
1962 &phba->sli4_hba.lpfc_abts_nvme_buf_list);
1963 spin_unlock_irqrestore(&phba->sli4_hba.abts_nvme_buf_list_lock,
1964 iflag);
1965 } else {
1966 lpfc_ncmd->nvmeCmd = NULL;
1967 lpfc_ncmd->cur_iocbq.iocb_flag = LPFC_IO_NVME;
1968 spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
1969 list_add_tail(&lpfc_ncmd->list, &phba->lpfc_nvme_buf_list_put);
1970 spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
1971 }
1972}
1973
1974/**
1975 * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
1976 * @pvport - the lpfc_vport instance requesting a localport.
1977 *
1978 * This routine is invoked to create an nvme localport instance to bind
1979 * to the nvme_fc_transport. It is called once during driver load
1980 * like lpfc_create_shost after all other services are initialized.
1981 * It requires a vport, vpi, and wwns at call time. Other localport
1982 * parameters are modified as the driver's FCID and the Fabric WWN
1983 * are established.
1984 *
1985 * Return codes
1986 * 0 - successful
1987 * -ENOMEM - no heap memory available
1988 * other values - from nvme registration upcall
1989 **/
1990int
1991lpfc_nvme_create_localport(struct lpfc_vport *vport)
1992{
1993 struct lpfc_hba *phba = vport->phba;
1994 struct nvme_fc_port_info nfcp_info;
1995 struct nvme_fc_local_port *localport;
1996 struct lpfc_nvme_lport *lport;
1997 int len, ret = 0;
1998
1999 /* Initialize this localport instance. The vport wwn usage ensures
2000 * that NPIV is accounted for.
2001 */
2002 memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
2003 nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
2004 nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
2005 nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
2006
2007 /* For now need + 1 to get around NVME transport logic */
2008 lpfc_nvme_template.max_sgl_segments = phba->cfg_sg_seg_cnt + 1;
2009 lpfc_nvme_template.max_hw_queues = phba->cfg_nvme_io_channel;
2010
2011 /* localport is allocated from the stack, but the registration
2012 * call allocates heap memory as well as the private area.
2013 */
2014 ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
2015 &vport->phba->pcidev->dev, &localport);
2016 if (!ret) {
2017 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
2018 "6005 Successfully registered local "
2019 "NVME port num %d, localP %p, private %p, "
2020 "sg_seg %d\n",
2021 localport->port_num, localport,
2022 localport->private,
2023 lpfc_nvme_template.max_sgl_segments);
2024
2025 /* Private is our lport size declared in the template. */
2026 lport = (struct lpfc_nvme_lport *)localport->private;
2027 vport->localport = localport;
2028 lport->vport = vport;
2029 INIT_LIST_HEAD(&lport->rport_list);
2030 vport->nvmei_support = 1;
2031 }
2032
2033 len = lpfc_new_nvme_buf(vport, phba->sli4_hba.nvme_xri_max);
2034 vport->phba->total_nvme_bufs += len;
2035 return ret;
2036}
2037
2038/**
2039 * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
2040 * @pnvme: pointer to lpfc nvme data structure.
2041 *
2042 * This routine is invoked to destroy all lports bound to the phba.
2043 * The lport memory was allocated by the nvme fc transport and is
2044 * released there. This routine ensures all rports bound to the
2045 * lport have been disconnected.
2046 *
2047 **/
2048void
2049lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
2050{
2051 struct nvme_fc_local_port *localport;
2052 struct lpfc_nvme_lport *lport;
2053 struct lpfc_nvme_rport *rport = NULL, *rport_next = NULL;
2054 int ret;
2055
2056 if (vport->nvmei_support == 0)
2057 return;
2058
2059 localport = vport->localport;
2060 vport->localport = NULL;
2061 lport = (struct lpfc_nvme_lport *)localport->private;
2062
2063 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2064 "6011 Destroying NVME localport %p\n",
2065 localport);
2066
2067 list_for_each_entry_safe(rport, rport_next, &lport->rport_list, list) {
2068 /* The last node ref has to get released now before the rport
2069 * private memory area is released by the transport.
2070 */
2071 list_del(&rport->list);
2072
2073 init_completion(&rport->rport_unreg_done);
2074 ret = nvme_fc_unregister_remoteport(rport->remoteport);
2075 if (ret)
2076 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
2077 "6008 rport fail destroy %x\n", ret);
2078 wait_for_completion_timeout(&rport->rport_unreg_done, 5);
2079 }
2080 /* lport's rport list is clear. Unregister
2081 * lport and release resources.
2082 */
2083 init_completion(&lport->lport_unreg_done);
2084 ret = nvme_fc_unregister_localport(localport);
2085 wait_for_completion_timeout(&lport->lport_unreg_done, 5);
2086
2087 /* Regardless of the unregister upcall response, clear
2088 * nvmei_support. All rports are unregistered and the
2089 * driver will clean up.
2090 */
2091 vport->nvmei_support = 0;
2092 if (ret == 0) {
2093 lpfc_printf_vlog(vport,
2094 KERN_INFO, LOG_NVME_DISC,
2095 "6009 Unregistered lport Success\n");
2096 } else {
2097 lpfc_printf_vlog(vport,
2098 KERN_INFO, LOG_NVME_DISC,
2099 "6010 Unregistered lport "
2100 "Failed, status x%x\n",
2101 ret);
2102 }
2103}
2104
2105void
2106lpfc_nvme_update_localport(struct lpfc_vport *vport)
2107{
2108 struct nvme_fc_local_port *localport;
2109 struct lpfc_nvme_lport *lport;
2110
2111 localport = vport->localport;
2112 lport = (struct lpfc_nvme_lport *)localport->private;
2113
2114 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2115 "6012 Update NVME lport %p did x%x\n",
2116 localport, vport->fc_myDID);
2117
2118 localport->port_id = vport->fc_myDID;
2119 if (localport->port_id == 0)
2120 localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
2121 else
2122 localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
2123
2124 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2125 "6030 bound lport %p to DID x%06x\n",
2126 lport, localport->port_id);
2127
2128}
2129
2130int
2131lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2132{
2133 int ret = 0;
2134 struct nvme_fc_local_port *localport;
2135 struct lpfc_nvme_lport *lport;
2136 struct lpfc_nvme_rport *rport;
2137 struct nvme_fc_remote_port *remote_port;
2138 struct nvme_fc_port_info rpinfo;
2139
2140 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
2141 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
2142 ndlp->nlp_DID, ndlp->nlp_type);
2143
2144 localport = vport->localport;
2145 lport = (struct lpfc_nvme_lport *)localport->private;
2146
2147 if (ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_INITIATOR)) {
2148
2149 /* The driver isn't expecting the rport wwn to change
2150 * but it might get a different DID on a different
2151 * fabric.
2152 */
2153 list_for_each_entry(rport, &lport->rport_list, list) {
2154 if (rport->remoteport->port_name !=
2155 wwn_to_u64(ndlp->nlp_portname.u.wwn))
2156 continue;
2157 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
2158 "6035 lport %p, found matching rport "
2159 "at wwpn 0x%llx, Data: x%x x%x x%x "
2160 "x%06x\n",
2161 lport,
2162 rport->remoteport->port_name,
2163 rport->remoteport->port_id,
2164 rport->remoteport->port_role,
2165 ndlp->nlp_type,
2166 ndlp->nlp_DID);
2167 remote_port = rport->remoteport;
2168 if ((remote_port->port_id == 0) &&
2169 (remote_port->port_role ==
2170 FC_PORT_ROLE_NVME_DISCOVERY)) {
2171 remote_port->port_id = ndlp->nlp_DID;
2172 remote_port->port_role &=
2173 ~FC_PORT_ROLE_NVME_DISCOVERY;
2174 if (ndlp->nlp_type & NLP_NVME_TARGET)
2175 remote_port->port_role |=
2176 FC_PORT_ROLE_NVME_TARGET;
2177 if (ndlp->nlp_type & NLP_NVME_INITIATOR)
2178 remote_port->port_role |=
2179 FC_PORT_ROLE_NVME_INITIATOR;
2180
2181 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
2182 LOG_NVME_DISC,
2183 "6014 Rebinding lport to "
2184 "rport wwpn 0x%llx, "
2185 "Data: x%x x%x x%x x%06x\n",
2186 remote_port->port_name,
2187 remote_port->port_id,
2188 remote_port->port_role,
2189 ndlp->nlp_type,
2190 ndlp->nlp_DID);
2191 }
2192 return 0;
2193 }
2194
2195 /* NVME rports are not preserved across devloss.
2196 * Just register this instance.
2197 */
2198 rpinfo.port_id = ndlp->nlp_DID;
2199 rpinfo.port_role = 0;
2200 if (ndlp->nlp_type & NLP_NVME_TARGET)
2201 rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
2202 if (ndlp->nlp_type & NLP_NVME_INITIATOR)
2203 rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
2204 rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
2205 rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2206
2207 ret = nvme_fc_register_remoteport(localport, &rpinfo,
2208 &remote_port);
2209 if (!ret) {
2210 rport = remote_port->private;
2211 rport->remoteport = remote_port;
2212 rport->lport = lport;
2213 rport->ndlp = lpfc_nlp_get(ndlp);
2214 if (!rport->ndlp)
2215 return -1;
2216 ndlp->nrport = rport;
2217 INIT_LIST_HEAD(&rport->list);
2218 list_add_tail(&rport->list, &lport->rport_list);
2219 lpfc_printf_vlog(vport, KERN_INFO,
2220 LOG_NVME_DISC | LOG_NODE,
2221 "6022 Binding new rport to lport %p "
2222 "Rport WWNN 0x%llx, Rport WWPN 0x%llx "
2223 "DID x%06x Role x%x\n",
2224 lport,
2225 rpinfo.node_name, rpinfo.port_name,
2226 rpinfo.port_id, rpinfo.port_role);
2227 } else {
2228 lpfc_printf_vlog(vport, KERN_ERR,
2229 LOG_NVME_DISC | LOG_NODE,
2230 "6031 RemotePort Registration failed "
2231 "err: %d, DID x%06x\n",
2232 ret, ndlp->nlp_DID);
2233 }
2234 } else {
2235 ret = -EINVAL;
2236 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2237 "6027 Unknown nlp_type x%x on DID x%06x "
2238 "ndlp %p. Not Registering nvme rport\n",
2239 ndlp->nlp_type, ndlp->nlp_DID, ndlp);
2240 }
2241 return ret;
2242}
2243
2244/* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
2245 *
2246 * There is no notion of Devloss or rport recovery from the current
2247 * nvme_transport perspective. Loss of an rport just means IO cannot
2248 * be sent and recovery is completely up to the initator.
2249 * For now, the driver just unbinds the DID and port_role so that
2250 * no further IO can be issued. Changes are planned for later.
2251 *
2252 * Notes - the ndlp reference count is not decremented here since
2253 * since there is no nvme_transport api for devloss. Node ref count
2254 * is only adjusted in driver unload.
2255 */
2256void
2257lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2258{
2259 int ret;
2260 struct nvme_fc_local_port *localport;
2261 struct lpfc_nvme_lport *lport;
2262 struct lpfc_nvme_rport *rport;
2263 struct nvme_fc_remote_port *remoteport;
2264
2265 localport = vport->localport;
2266
2267 /* This is fundamental error. The localport is always
2268 * available until driver unload. Just exit.
2269 */
2270 if (!localport)
2271 return;
2272
2273 lport = (struct lpfc_nvme_lport *)localport->private;
2274 if (!lport)
2275 goto input_err;
2276
2277 rport = ndlp->nrport;
2278 if (!rport)
2279 goto input_err;
2280
2281 remoteport = rport->remoteport;
2282 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2283 "6033 Unreg nvme remoteport %p, portname x%llx, "
2284 "port_id x%06x, portstate x%x port type x%x\n",
2285 remoteport, remoteport->port_name,
2286 remoteport->port_id, remoteport->port_state,
2287 ndlp->nlp_type);
2288
2289 /* Sanity check ndlp type. Only call for NVME ports. Don't
2290 * clear any rport state until the transport calls back.
2291 */
2292 if (ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_INITIATOR)) {
2293 init_completion(&rport->rport_unreg_done);
2294 ret = nvme_fc_unregister_remoteport(remoteport);
2295 if (ret != 0) {
2296 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
2297 "6167 NVME unregister failed %d "
2298 "port_state x%x\n",
2299 ret, remoteport->port_state);
2300 }
2301
2302 /* Wait for the driver's delete completion routine to finish
2303 * before proceeding. This guarantees the transport and driver
2304 * have completed the unreg process.
2305 */
2306 ret = wait_for_completion_timeout(&rport->rport_unreg_done, 5);
2307 if (ret == 0) {
2308 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
2309 "6169 Unreg nvme wait failed %d\n",
2310 ret);
2311 }
2312 }
2313 return;
2314
2315 input_err:
2316 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
2317 "6168: State error: lport %p, rport%p FCID x%06x\n",
2318 vport->localport, ndlp->rport, ndlp->nlp_DID);
2319}
diff --git a/drivers/scsi/lpfc/lpfc_nvme.h b/drivers/scsi/lpfc/lpfc_nvme.h
index 2b167933fd26..4cc51086a5f7 100644
--- a/drivers/scsi/lpfc/lpfc_nvme.h
+++ b/drivers/scsi/lpfc/lpfc_nvme.h
@@ -29,6 +29,12 @@
29 29
30#define LPFC_NVME_ERSP_LEN 0x20 30#define LPFC_NVME_ERSP_LEN 0x20
31 31
32struct lpfc_nvme_qhandle {
33 uint32_t index; /* WQ index to use */
34 uint32_t qidx; /* queue index passed to create */
35 uint32_t cpu_id; /* current cpu id at time of create */
36};
37
32/* Declare nvme-based local and remote port definitions. */ 38/* Declare nvme-based local and remote port definitions. */
33struct lpfc_nvme_lport { 39struct lpfc_nvme_lport {
34 struct lpfc_vport *vport; 40 struct lpfc_vport *vport;
diff --git a/drivers/scsi/lpfc/lpfc_sli.c b/drivers/scsi/lpfc/lpfc_sli.c
index 52fa5c77f3bc..ccda1f73ae84 100644
--- a/drivers/scsi/lpfc/lpfc_sli.c
+++ b/drivers/scsi/lpfc/lpfc_sli.c
@@ -6795,6 +6795,22 @@ lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6795 } 6795 }
6796 } 6796 }
6797 6797
6798 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
6799 (phba->nvmet_support == 0)) {
6800
6801 /* register the allocated nvme sgl pool to the port */
6802 rc = lpfc_repost_nvme_sgl_list(phba);
6803 if (unlikely(rc)) {
6804 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6805 "6116 Error %d during nvme sgl post "
6806 "operation\n", rc);
6807 /* Some NVME buffers were moved to abort nvme list */
6808 /* A pci function reset will repost them */
6809 rc = -ENODEV;
6810 goto out_destroy_queue;
6811 }
6812 }
6813
6798 /* Post the rpi header region to the device. */ 6814 /* Post the rpi header region to the device. */
6799 rc = lpfc_sli4_post_all_rpi_hdrs(phba); 6815 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6800 if (unlikely(rc)) { 6816 if (unlikely(rc)) {
@@ -10492,10 +10508,7 @@ lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10492 /* ABTS WQE must go to the same WQ as the WQE to be aborted */ 10508 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10493 abtsiocbp->iocb_flag |= LPFC_IO_NVME; 10509 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10494 abtsiocbp->vport = vport; 10510 abtsiocbp->vport = vport;
10495 /* todo: assign wqe_cmpl to lpfc_nvme_abort_fcreq_cmpl 10511 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
10496 * subsequent patch will add routine. For now, just skip assignment
10497 * as won't ever be called.
10498 */
10499 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp); 10512 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10500 if (retval == IOCB_ERROR) { 10513 if (retval == IOCB_ERROR) {
10501 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME, 10514 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
diff --git a/drivers/scsi/lpfc/lpfc_sli4.h b/drivers/scsi/lpfc/lpfc_sli4.h
index b5b4f6b843c0..9cb8508b4b4b 100644
--- a/drivers/scsi/lpfc/lpfc_sli4.h
+++ b/drivers/scsi/lpfc/lpfc_sli4.h
@@ -753,6 +753,7 @@ int lpfc_sli4_queue_setup(struct lpfc_hba *);
753void lpfc_sli4_queue_unset(struct lpfc_hba *); 753void lpfc_sli4_queue_unset(struct lpfc_hba *);
754int lpfc_sli4_post_sgl(struct lpfc_hba *, dma_addr_t, dma_addr_t, uint16_t); 754int lpfc_sli4_post_sgl(struct lpfc_hba *, dma_addr_t, dma_addr_t, uint16_t);
755int lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *); 755int lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *);
756int lpfc_repost_nvme_sgl_list(struct lpfc_hba *phba);
756uint16_t lpfc_sli4_next_xritag(struct lpfc_hba *); 757uint16_t lpfc_sli4_next_xritag(struct lpfc_hba *);
757void lpfc_sli4_free_xri(struct lpfc_hba *, int); 758void lpfc_sli4_free_xri(struct lpfc_hba *, int);
758int lpfc_sli4_post_async_mbox(struct lpfc_hba *); 759int lpfc_sli4_post_async_mbox(struct lpfc_hba *);
diff --git a/drivers/scsi/lpfc/lpfc_vport.c b/drivers/scsi/lpfc/lpfc_vport.c
index 07c7c5f9fdde..b0d94f2e5b44 100644
--- a/drivers/scsi/lpfc/lpfc_vport.c
+++ b/drivers/scsi/lpfc/lpfc_vport.c
@@ -403,7 +403,21 @@ lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
403 vport->fdmi_port_mask = phba->pport->fdmi_port_mask; 403 vport->fdmi_port_mask = phba->pport->fdmi_port_mask;
404 } 404 }
405 405
406 /* todo: init: register port with nvme */ 406 if ((phba->nvmet_support == 0) &&
407 ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
408 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))) {
409 /* Create NVME binding with nvme_fc_transport. This
410 * ensures the vport is initialized.
411 */
412 rc = lpfc_nvme_create_localport(vport);
413 if (rc) {
414 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
415 "6003 %s status x%x\n",
416 "NVME registration failed, ",
417 rc);
418 goto error_out;
419 }
420 }
407 421
408 /* 422 /*
409 * In SLI4, the vpi must be activated before it can be used 423 * In SLI4, the vpi must be activated before it can be used