]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/lpfc/lpfc_nvme.c
c0f59fbaa17388943d3d484873a885ea46c1c85c
[linux.git] / drivers / scsi / lpfc / lpfc_nvme.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  ********************************************************************/
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <asm/unaligned.h>
28 #include <linux/crc-t10dif.h>
29 #include <net/checksum.h>
30
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_eh.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_tcq.h>
36 #include <scsi/scsi_transport_fc.h>
37 #include <scsi/fc/fc_fs.h>
38
39 #include <linux/nvme.h>
40 #include <linux/nvme-fc-driver.h>
41 #include <linux/nvme-fc.h>
42 #include "lpfc_version.h"
43 #include "lpfc_hw4.h"
44 #include "lpfc_hw.h"
45 #include "lpfc_sli.h"
46 #include "lpfc_sli4.h"
47 #include "lpfc_nl.h"
48 #include "lpfc_disc.h"
49 #include "lpfc.h"
50 #include "lpfc_nvme.h"
51 #include "lpfc_scsi.h"
52 #include "lpfc_logmsg.h"
53 #include "lpfc_crtn.h"
54 #include "lpfc_vport.h"
55 #include "lpfc_debugfs.h"
56
57 /* NVME initiator-based functions */
58
59 static struct lpfc_nvme_buf *
60 lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
61                   int expedite);
62
63 static void
64 lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_nvme_buf *);
65
66 static struct nvme_fc_port_template lpfc_nvme_template;
67
68 static union lpfc_wqe128 lpfc_iread_cmd_template;
69 static union lpfc_wqe128 lpfc_iwrite_cmd_template;
70 static union lpfc_wqe128 lpfc_icmnd_cmd_template;
71
72 /* Setup WQE templates for NVME IOs */
73 void
74 lpfc_nvme_cmd_template(void)
75 {
76         union lpfc_wqe128 *wqe;
77
78         /* IREAD template */
79         wqe = &lpfc_iread_cmd_template;
80         memset(wqe, 0, sizeof(union lpfc_wqe128));
81
82         /* Word 0, 1, 2 - BDE is variable */
83
84         /* Word 3 - cmd_buff_len, payload_offset_len is zero */
85
86         /* Word 4 - total_xfer_len is variable */
87
88         /* Word 5 - is zero */
89
90         /* Word 6 - ctxt_tag, xri_tag is variable */
91
92         /* Word 7 */
93         bf_set(wqe_cmnd, &wqe->fcp_iread.wqe_com, CMD_FCP_IREAD64_WQE);
94         bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, PARM_READ_CHECK);
95         bf_set(wqe_class, &wqe->fcp_iread.wqe_com, CLASS3);
96         bf_set(wqe_ct, &wqe->fcp_iread.wqe_com, SLI4_CT_RPI);
97
98         /* Word 8 - abort_tag is variable */
99
100         /* Word 9  - reqtag is variable */
101
102         /* Word 10 - dbde, wqes is variable */
103         bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
104         bf_set(wqe_nvme, &wqe->fcp_iread.wqe_com, 1);
105         bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
106         bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com, LPFC_WQE_LENLOC_WORD4);
107         bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
108         bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
109
110         /* Word 11 - pbde is variable */
111         bf_set(wqe_cmd_type, &wqe->fcp_iread.wqe_com, NVME_READ_CMD);
112         bf_set(wqe_cqid, &wqe->fcp_iread.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
113         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
114
115         /* Word 12 - is zero */
116
117         /* Word 13, 14, 15 - PBDE is variable */
118
119         /* IWRITE template */
120         wqe = &lpfc_iwrite_cmd_template;
121         memset(wqe, 0, sizeof(union lpfc_wqe128));
122
123         /* Word 0, 1, 2 - BDE is variable */
124
125         /* Word 3 - cmd_buff_len, payload_offset_len is zero */
126
127         /* Word 4 - total_xfer_len is variable */
128
129         /* Word 5 - initial_xfer_len is variable */
130
131         /* Word 6 - ctxt_tag, xri_tag is variable */
132
133         /* Word 7 */
134         bf_set(wqe_cmnd, &wqe->fcp_iwrite.wqe_com, CMD_FCP_IWRITE64_WQE);
135         bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, PARM_READ_CHECK);
136         bf_set(wqe_class, &wqe->fcp_iwrite.wqe_com, CLASS3);
137         bf_set(wqe_ct, &wqe->fcp_iwrite.wqe_com, SLI4_CT_RPI);
138
139         /* Word 8 - abort_tag is variable */
140
141         /* Word 9  - reqtag is variable */
142
143         /* Word 10 - dbde, wqes is variable */
144         bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
145         bf_set(wqe_nvme, &wqe->fcp_iwrite.wqe_com, 1);
146         bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
147         bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_LENLOC_WORD4);
148         bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
149         bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
150
151         /* Word 11 - pbde is variable */
152         bf_set(wqe_cmd_type, &wqe->fcp_iwrite.wqe_com, NVME_WRITE_CMD);
153         bf_set(wqe_cqid, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
154         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
155
156         /* Word 12 - is zero */
157
158         /* Word 13, 14, 15 - PBDE is variable */
159
160         /* ICMND template */
161         wqe = &lpfc_icmnd_cmd_template;
162         memset(wqe, 0, sizeof(union lpfc_wqe128));
163
164         /* Word 0, 1, 2 - BDE is variable */
165
166         /* Word 3 - payload_offset_len is variable */
167
168         /* Word 4, 5 - is zero */
169
170         /* Word 6 - ctxt_tag, xri_tag is variable */
171
172         /* Word 7 */
173         bf_set(wqe_cmnd, &wqe->fcp_icmd.wqe_com, CMD_FCP_ICMND64_WQE);
174         bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
175         bf_set(wqe_class, &wqe->fcp_icmd.wqe_com, CLASS3);
176         bf_set(wqe_ct, &wqe->fcp_icmd.wqe_com, SLI4_CT_RPI);
177
178         /* Word 8 - abort_tag is variable */
179
180         /* Word 9  - reqtag is variable */
181
182         /* Word 10 - dbde, wqes is variable */
183         bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
184         bf_set(wqe_nvme, &wqe->fcp_icmd.wqe_com, 1);
185         bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_NONE);
186         bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com, LPFC_WQE_LENLOC_NONE);
187         bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
188         bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
189
190         /* Word 11 */
191         bf_set(wqe_cmd_type, &wqe->fcp_icmd.wqe_com, FCP_COMMAND);
192         bf_set(wqe_cqid, &wqe->fcp_icmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
193         bf_set(wqe_pbde, &wqe->fcp_icmd.wqe_com, 0);
194
195         /* Word 12, 13, 14, 15 - is zero */
196 }
197
198 /**
199  * lpfc_nvme_create_queue -
200  * @lpfc_pnvme: Pointer to the driver's nvme instance data
201  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
202  * @handle: An opaque driver handle used in follow-up calls.
203  *
204  * Driver registers this routine to preallocate and initialize any
205  * internal data structures to bind the @qidx to its internal IO queues.
206  * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
207  *
208  * Return value :
209  *   0 - Success
210  *   -EINVAL - Unsupported input value.
211  *   -ENOMEM - Could not alloc necessary memory
212  **/
213 static int
214 lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
215                        unsigned int qidx, u16 qsize,
216                        void **handle)
217 {
218         struct lpfc_nvme_lport *lport;
219         struct lpfc_vport *vport;
220         struct lpfc_nvme_qhandle *qhandle;
221         char *str;
222
223         if (!pnvme_lport->private)
224                 return -ENOMEM;
225
226         lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
227         vport = lport->vport;
228         qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
229         if (qhandle == NULL)
230                 return -ENOMEM;
231
232         qhandle->cpu_id = smp_processor_id();
233         qhandle->qidx = qidx;
234         /*
235          * NVME qidx == 0 is the admin queue, so both admin queue
236          * and first IO queue will use MSI-X vector and associated
237          * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
238          */
239         if (qidx) {
240                 str = "IO ";  /* IO queue */
241                 qhandle->index = ((qidx - 1) %
242                         vport->phba->cfg_hdw_queue);
243         } else {
244                 str = "ADM";  /* Admin queue */
245                 qhandle->index = qidx;
246         }
247
248         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
249                          "6073 Binding %s HdwQueue %d  (cpu %d) to "
250                          "hdw_queue %d qhandle %p\n", str,
251                          qidx, qhandle->cpu_id, qhandle->index, qhandle);
252         *handle = (void *)qhandle;
253         return 0;
254 }
255
256 /**
257  * lpfc_nvme_delete_queue -
258  * @lpfc_pnvme: Pointer to the driver's nvme instance data
259  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
260  * @handle: An opaque driver handle from lpfc_nvme_create_queue
261  *
262  * Driver registers this routine to free
263  * any internal data structures to bind the @qidx to its internal
264  * IO queues.
265  *
266  * Return value :
267  *   0 - Success
268  *   TODO:  What are the failure codes.
269  **/
270 static void
271 lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
272                        unsigned int qidx,
273                        void *handle)
274 {
275         struct lpfc_nvme_lport *lport;
276         struct lpfc_vport *vport;
277
278         if (!pnvme_lport->private)
279                 return;
280
281         lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
282         vport = lport->vport;
283
284         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
285                         "6001 ENTER.  lpfc_pnvme %p, qidx x%x qhandle %p\n",
286                         lport, qidx, handle);
287         kfree(handle);
288 }
289
290 static void
291 lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
292 {
293         struct lpfc_nvme_lport *lport = localport->private;
294
295         lpfc_printf_vlog(lport->vport, KERN_INFO, LOG_NVME,
296                          "6173 localport %p delete complete\n",
297                          lport);
298
299         /* release any threads waiting for the unreg to complete */
300         complete(&lport->lport_unreg_done);
301 }
302
303 /* lpfc_nvme_remoteport_delete
304  *
305  * @remoteport: Pointer to an nvme transport remoteport instance.
306  *
307  * This is a template downcall.  NVME transport calls this function
308  * when it has completed the unregistration of a previously
309  * registered remoteport.
310  *
311  * Return value :
312  * None
313  */
314 void
315 lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
316 {
317         struct lpfc_nvme_rport *rport = remoteport->private;
318         struct lpfc_vport *vport;
319         struct lpfc_nodelist *ndlp;
320
321         ndlp = rport->ndlp;
322         if (!ndlp)
323                 goto rport_err;
324
325         vport = ndlp->vport;
326         if (!vport)
327                 goto rport_err;
328
329         /* Remove this rport from the lport's list - memory is owned by the
330          * transport. Remove the ndlp reference for the NVME transport before
331          * calling state machine to remove the node.
332          */
333         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
334                         "6146 remoteport delete of remoteport %p\n",
335                         remoteport);
336         spin_lock_irq(&vport->phba->hbalock);
337
338         /* The register rebind might have occurred before the delete
339          * downcall.  Guard against this race.
340          */
341         if (ndlp->upcall_flags & NLP_WAIT_FOR_UNREG) {
342                 ndlp->nrport = NULL;
343                 ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
344         }
345         spin_unlock_irq(&vport->phba->hbalock);
346
347         /* Remove original register reference. The host transport
348          * won't reference this rport/remoteport any further.
349          */
350         lpfc_nlp_put(ndlp);
351
352  rport_err:
353         return;
354 }
355
356 static void
357 lpfc_nvme_cmpl_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
358                        struct lpfc_wcqe_complete *wcqe)
359 {
360         struct lpfc_vport *vport = cmdwqe->vport;
361         struct lpfc_nvme_lport *lport;
362         uint32_t status;
363         struct nvmefc_ls_req *pnvme_lsreq;
364         struct lpfc_dmabuf *buf_ptr;
365         struct lpfc_nodelist *ndlp;
366
367         pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
368         status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
369
370         if (vport->localport) {
371                 lport = (struct lpfc_nvme_lport *)vport->localport->private;
372                 if (lport) {
373                         atomic_inc(&lport->fc4NvmeLsCmpls);
374                         if (status) {
375                                 if (bf_get(lpfc_wcqe_c_xb, wcqe))
376                                         atomic_inc(&lport->cmpl_ls_xb);
377                                 atomic_inc(&lport->cmpl_ls_err);
378                         }
379                 }
380         }
381
382         ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
383         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
384                          "6047 nvme cmpl Enter "
385                          "Data %p DID %x Xri: %x status %x reason x%x cmd:%p "
386                          "lsreg:%p bmp:%p ndlp:%p\n",
387                          pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
388                          cmdwqe->sli4_xritag, status,
389                          (wcqe->parameter & 0xffff),
390                          cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
391
392         lpfc_nvmeio_data(phba, "NVME LS  CMPL: xri x%x stat x%x parm x%x\n",
393                          cmdwqe->sli4_xritag, status, wcqe->parameter);
394
395         if (cmdwqe->context3) {
396                 buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
397                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
398                 kfree(buf_ptr);
399                 cmdwqe->context3 = NULL;
400         }
401         if (pnvme_lsreq->done)
402                 pnvme_lsreq->done(pnvme_lsreq, status);
403         else
404                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
405                                  "6046 nvme cmpl without done call back? "
406                                  "Data %p DID %x Xri: %x status %x\n",
407                                 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
408                                 cmdwqe->sli4_xritag, status);
409         if (ndlp) {
410                 lpfc_nlp_put(ndlp);
411                 cmdwqe->context1 = NULL;
412         }
413         lpfc_sli_release_iocbq(phba, cmdwqe);
414 }
415
416 static int
417 lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
418                   struct lpfc_dmabuf *inp,
419                   struct nvmefc_ls_req *pnvme_lsreq,
420                   void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
421                                struct lpfc_wcqe_complete *),
422                   struct lpfc_nodelist *ndlp, uint32_t num_entry,
423                   uint32_t tmo, uint8_t retry)
424 {
425         struct lpfc_hba *phba = vport->phba;
426         union lpfc_wqe128 *wqe;
427         struct lpfc_iocbq *genwqe;
428         struct ulp_bde64 *bpl;
429         struct ulp_bde64 bde;
430         int i, rc, xmit_len, first_len;
431
432         /* Allocate buffer for  command WQE */
433         genwqe = lpfc_sli_get_iocbq(phba);
434         if (genwqe == NULL)
435                 return 1;
436
437         wqe = &genwqe->wqe;
438         memset(wqe, 0, sizeof(union lpfc_wqe));
439
440         genwqe->context3 = (uint8_t *)bmp;
441         genwqe->iocb_flag |= LPFC_IO_NVME_LS;
442
443         /* Save for completion so we can release these resources */
444         genwqe->context1 = lpfc_nlp_get(ndlp);
445         genwqe->context2 = (uint8_t *)pnvme_lsreq;
446         /* Fill in payload, bp points to frame payload */
447
448         if (!tmo)
449                 /* FC spec states we need 3 * ratov for CT requests */
450                 tmo = (3 * phba->fc_ratov);
451
452         /* For this command calculate the xmit length of the request bde. */
453         xmit_len = 0;
454         first_len = 0;
455         bpl = (struct ulp_bde64 *)bmp->virt;
456         for (i = 0; i < num_entry; i++) {
457                 bde.tus.w = bpl[i].tus.w;
458                 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
459                         break;
460                 xmit_len += bde.tus.f.bdeSize;
461                 if (i == 0)
462                         first_len = xmit_len;
463         }
464
465         genwqe->rsvd2 = num_entry;
466         genwqe->hba_wqidx = 0;
467
468         /* Words 0 - 2 */
469         wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
470         wqe->generic.bde.tus.f.bdeSize = first_len;
471         wqe->generic.bde.addrLow = bpl[0].addrLow;
472         wqe->generic.bde.addrHigh = bpl[0].addrHigh;
473
474         /* Word 3 */
475         wqe->gen_req.request_payload_len = first_len;
476
477         /* Word 4 */
478
479         /* Word 5 */
480         bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
481         bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
482         bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
483         bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
484         bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
485
486         /* Word 6 */
487         bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
488                phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
489         bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
490
491         /* Word 7 */
492         bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
493         bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
494         bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
495         bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
496
497         /* Word 8 */
498         wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
499
500         /* Word 9 */
501         bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
502
503         /* Word 10 */
504         bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
505         bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
506         bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
507         bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
508         bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
509
510         /* Word 11 */
511         bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
512         bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
513
514
515         /* Issue GEN REQ WQE for NPORT <did> */
516         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
517                          "6050 Issue GEN REQ WQE to NPORT x%x "
518                          "Data: x%x x%x wq:%p lsreq:%p bmp:%p xmit:%d 1st:%d\n",
519                          ndlp->nlp_DID, genwqe->iotag,
520                          vport->port_state,
521                         genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
522         genwqe->wqe_cmpl = cmpl;
523         genwqe->iocb_cmpl = NULL;
524         genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
525         genwqe->vport = vport;
526         genwqe->retry = retry;
527
528         lpfc_nvmeio_data(phba, "NVME LS  XMIT: xri x%x iotag x%x to x%06x\n",
529                          genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
530
531         rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, genwqe);
532         if (rc) {
533                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
534                                  "6045 Issue GEN REQ WQE to NPORT x%x "
535                                  "Data: x%x x%x\n",
536                                  ndlp->nlp_DID, genwqe->iotag,
537                                  vport->port_state);
538                 lpfc_sli_release_iocbq(phba, genwqe);
539                 return 1;
540         }
541         return 0;
542 }
543
544 /**
545  * lpfc_nvme_ls_req - Issue an Link Service request
546  * @lpfc_pnvme: Pointer to the driver's nvme instance data
547  * @lpfc_nvme_lport: Pointer to the driver's local port data
548  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
549  *
550  * Driver registers this routine to handle any link service request
551  * from the nvme_fc transport to a remote nvme-aware port.
552  *
553  * Return value :
554  *   0 - Success
555  *   TODO: What are the failure codes.
556  **/
557 static int
558 lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
559                  struct nvme_fc_remote_port *pnvme_rport,
560                  struct nvmefc_ls_req *pnvme_lsreq)
561 {
562         int ret = 0;
563         struct lpfc_nvme_lport *lport;
564         struct lpfc_nvme_rport *rport;
565         struct lpfc_vport *vport;
566         struct lpfc_nodelist *ndlp;
567         struct ulp_bde64 *bpl;
568         struct lpfc_dmabuf *bmp;
569         uint16_t ntype, nstate;
570
571         /* there are two dma buf in the request, actually there is one and
572          * the second one is just the start address + cmd size.
573          * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
574          * in a lpfc_dmabuf struct. When freeing we just free the wrapper
575          * because the nvem layer owns the data bufs.
576          * We do not have to break these packets open, we don't care what is in
577          * them. And we do not have to look at the resonse data, we only care
578          * that we got a response. All of the caring is going to happen in the
579          * nvme-fc layer.
580          */
581
582         lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
583         rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
584         if (unlikely(!lport) || unlikely(!rport))
585                 return -EINVAL;
586
587         vport = lport->vport;
588
589         if (vport->load_flag & FC_UNLOADING)
590                 return -ENODEV;
591
592         /* Need the ndlp.  It is stored in the driver's rport. */
593         ndlp = rport->ndlp;
594         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
595                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
596                                  "6051 Remoteport %p, rport has invalid ndlp. "
597                                  "Failing LS Req\n", pnvme_rport);
598                 return -ENODEV;
599         }
600
601         /* The remote node has to be a mapped nvme target or an
602          * unmapped nvme initiator or it's an error.
603          */
604         ntype = ndlp->nlp_type;
605         nstate = ndlp->nlp_state;
606         if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
607             (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
608                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
609                                  "6088 DID x%06x not ready for "
610                                  "IO. State x%x, Type x%x\n",
611                                  pnvme_rport->port_id,
612                                  ndlp->nlp_state, ndlp->nlp_type);
613                 return -ENODEV;
614         }
615         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
616         if (!bmp) {
617
618                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
619                                  "6044 Could not find node for DID %x\n",
620                                  pnvme_rport->port_id);
621                 return 2;
622         }
623         INIT_LIST_HEAD(&bmp->list);
624         bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
625         if (!bmp->virt) {
626                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
627                                  "6042 Could not find node for DID %x\n",
628                                  pnvme_rport->port_id);
629                 kfree(bmp);
630                 return 3;
631         }
632         bpl = (struct ulp_bde64 *)bmp->virt;
633         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
634         bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
635         bpl->tus.f.bdeFlags = 0;
636         bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
637         bpl->tus.w = le32_to_cpu(bpl->tus.w);
638         bpl++;
639
640         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
641         bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
642         bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
643         bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
644         bpl->tus.w = le32_to_cpu(bpl->tus.w);
645
646         /* Expand print to include key fields. */
647         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
648                          "6149 Issue LS Req to DID 0x%06x lport %p, rport %p "
649                          "lsreq%p rqstlen:%d rsplen:%d %pad %pad\n",
650                          ndlp->nlp_DID,
651                          pnvme_lport, pnvme_rport,
652                          pnvme_lsreq, pnvme_lsreq->rqstlen,
653                          pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
654                          &pnvme_lsreq->rspdma);
655
656         atomic_inc(&lport->fc4NvmeLsRequests);
657
658         /* Hardcode the wait to 30 seconds.  Connections are failing otherwise.
659          * This code allows it all to work.
660          */
661         ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
662                                 pnvme_lsreq, lpfc_nvme_cmpl_gen_req,
663                                 ndlp, 2, 30, 0);
664         if (ret != WQE_SUCCESS) {
665                 atomic_inc(&lport->xmt_ls_err);
666                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
667                                  "6052 EXIT. issue ls wqe failed lport %p, "
668                                  "rport %p lsreq%p Status %x DID %x\n",
669                                  pnvme_lport, pnvme_rport, pnvme_lsreq,
670                                  ret, ndlp->nlp_DID);
671                 lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
672                 kfree(bmp);
673                 return ret;
674         }
675
676         /* Stub in routine and return 0 for now. */
677         return ret;
678 }
679
680 /**
681  * lpfc_nvme_ls_abort - Issue an Link Service request
682  * @lpfc_pnvme: Pointer to the driver's nvme instance data
683  * @lpfc_nvme_lport: Pointer to the driver's local port data
684  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
685  *
686  * Driver registers this routine to handle any link service request
687  * from the nvme_fc transport to a remote nvme-aware port.
688  *
689  * Return value :
690  *   0 - Success
691  *   TODO: What are the failure codes.
692  **/
693 static void
694 lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
695                    struct nvme_fc_remote_port *pnvme_rport,
696                    struct nvmefc_ls_req *pnvme_lsreq)
697 {
698         struct lpfc_nvme_lport *lport;
699         struct lpfc_vport *vport;
700         struct lpfc_hba *phba;
701         struct lpfc_nodelist *ndlp;
702         LIST_HEAD(abort_list);
703         struct lpfc_sli_ring *pring;
704         struct lpfc_iocbq *wqe, *next_wqe;
705
706         lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
707         if (unlikely(!lport))
708                 return;
709         vport = lport->vport;
710         phba = vport->phba;
711
712         if (vport->load_flag & FC_UNLOADING)
713                 return;
714
715         ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
716         if (!ndlp) {
717                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
718                                  "6049 Could not find node for DID %x\n",
719                                  pnvme_rport->port_id);
720                 return;
721         }
722
723         /* Expand print to include key fields. */
724         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
725                          "6040 ENTER.  lport %p, rport %p lsreq %p rqstlen:%d "
726                          "rsplen:%d %pad %pad\n",
727                          pnvme_lport, pnvme_rport,
728                          pnvme_lsreq, pnvme_lsreq->rqstlen,
729                          pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
730                          &pnvme_lsreq->rspdma);
731
732         /*
733          * Lock the ELS ring txcmplq and build a local list of all ELS IOs
734          * that need an ABTS.  The IOs need to stay on the txcmplq so that
735          * the abort operation completes them successfully.
736          */
737         pring = phba->sli4_hba.nvmels_wq->pring;
738         spin_lock_irq(&phba->hbalock);
739         spin_lock(&pring->ring_lock);
740         list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
741                 /* Add to abort_list on on NDLP match. */
742                 if (lpfc_check_sli_ndlp(phba, pring, wqe, ndlp)) {
743                         wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
744                         list_add_tail(&wqe->dlist, &abort_list);
745                 }
746         }
747         spin_unlock(&pring->ring_lock);
748         spin_unlock_irq(&phba->hbalock);
749
750         /* Abort the targeted IOs and remove them from the abort list. */
751         list_for_each_entry_safe(wqe, next_wqe, &abort_list, dlist) {
752                 atomic_inc(&lport->xmt_ls_abort);
753                 spin_lock_irq(&phba->hbalock);
754                 list_del_init(&wqe->dlist);
755                 lpfc_sli_issue_abort_iotag(phba, pring, wqe);
756                 spin_unlock_irq(&phba->hbalock);
757         }
758 }
759
760 /* Fix up the existing sgls for NVME IO. */
761 static inline void
762 lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
763                        struct lpfc_nvme_buf *lpfc_ncmd,
764                        struct nvmefc_fcp_req *nCmd)
765 {
766         struct lpfc_hba  *phba = vport->phba;
767         struct sli4_sge *sgl;
768         union lpfc_wqe128 *wqe;
769         uint32_t *wptr, *dptr;
770
771         /*
772          * Get a local pointer to the built-in wqe and correct
773          * the cmd size to match NVME's 96 bytes and fix
774          * the dma address.
775          */
776
777         wqe = &lpfc_ncmd->cur_iocbq.wqe;
778
779         /*
780          * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
781          * match NVME.  NVME sends 96 bytes. Also, use the
782          * nvme commands command and response dma addresses
783          * rather than the virtual memory to ease the restore
784          * operation.
785          */
786         sgl = lpfc_ncmd->dma_sgl;
787         sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
788         if (phba->cfg_nvme_embed_cmd) {
789                 sgl->addr_hi = 0;
790                 sgl->addr_lo = 0;
791
792                 /* Word 0-2 - NVME CMND IU (embedded payload) */
793                 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
794                 wqe->generic.bde.tus.f.bdeSize = 56;
795                 wqe->generic.bde.addrHigh = 0;
796                 wqe->generic.bde.addrLow =  64;  /* Word 16 */
797
798                 /* Word 10  - dbde is 0, wqes is 1 in template */
799
800                 /*
801                  * Embed the payload in the last half of the WQE
802                  * WQE words 16-30 get the NVME CMD IU payload
803                  *
804                  * WQE words 16-19 get payload Words 1-4
805                  * WQE words 20-21 get payload Words 6-7
806                  * WQE words 22-29 get payload Words 16-23
807                  */
808                 wptr = &wqe->words[16];  /* WQE ptr */
809                 dptr = (uint32_t *)nCmd->cmdaddr;  /* payload ptr */
810                 dptr++;                 /* Skip Word 0 in payload */
811
812                 *wptr++ = *dptr++;      /* Word 1 */
813                 *wptr++ = *dptr++;      /* Word 2 */
814                 *wptr++ = *dptr++;      /* Word 3 */
815                 *wptr++ = *dptr++;      /* Word 4 */
816                 dptr++;                 /* Skip Word 5 in payload */
817                 *wptr++ = *dptr++;      /* Word 6 */
818                 *wptr++ = *dptr++;      /* Word 7 */
819                 dptr += 8;              /* Skip Words 8-15 in payload */
820                 *wptr++ = *dptr++;      /* Word 16 */
821                 *wptr++ = *dptr++;      /* Word 17 */
822                 *wptr++ = *dptr++;      /* Word 18 */
823                 *wptr++ = *dptr++;      /* Word 19 */
824                 *wptr++ = *dptr++;      /* Word 20 */
825                 *wptr++ = *dptr++;      /* Word 21 */
826                 *wptr++ = *dptr++;      /* Word 22 */
827                 *wptr   = *dptr;        /* Word 23 */
828         } else {
829                 sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->cmddma));
830                 sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->cmddma));
831
832                 /* Word 0-2 - NVME CMND IU Inline BDE */
833                 wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
834                 wqe->generic.bde.tus.f.bdeSize = nCmd->cmdlen;
835                 wqe->generic.bde.addrHigh = sgl->addr_hi;
836                 wqe->generic.bde.addrLow =  sgl->addr_lo;
837
838                 /* Word 10 */
839                 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
840                 bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
841         }
842
843         sgl++;
844
845         /* Setup the physical region for the FCP RSP */
846         sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
847         sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
848         sgl->word2 = le32_to_cpu(sgl->word2);
849         if (nCmd->sg_cnt)
850                 bf_set(lpfc_sli4_sge_last, sgl, 0);
851         else
852                 bf_set(lpfc_sli4_sge_last, sgl, 1);
853         sgl->word2 = cpu_to_le32(sgl->word2);
854         sgl->sge_len = cpu_to_le32(nCmd->rsplen);
855 }
856
857 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
858 static void
859 lpfc_nvme_ktime(struct lpfc_hba *phba,
860                 struct lpfc_nvme_buf *lpfc_ncmd)
861 {
862         uint64_t seg1, seg2, seg3, seg4;
863         uint64_t segsum;
864
865         if (!lpfc_ncmd->ts_last_cmd ||
866             !lpfc_ncmd->ts_cmd_start ||
867             !lpfc_ncmd->ts_cmd_wqput ||
868             !lpfc_ncmd->ts_isr_cmpl ||
869             !lpfc_ncmd->ts_data_nvme)
870                 return;
871
872         if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_cmd_start)
873                 return;
874         if (lpfc_ncmd->ts_cmd_start < lpfc_ncmd->ts_last_cmd)
875                 return;
876         if (lpfc_ncmd->ts_cmd_wqput < lpfc_ncmd->ts_cmd_start)
877                 return;
878         if (lpfc_ncmd->ts_isr_cmpl < lpfc_ncmd->ts_cmd_wqput)
879                 return;
880         if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_isr_cmpl)
881                 return;
882         /*
883          * Segment 1 - Time from Last FCP command cmpl is handed
884          * off to NVME Layer to start of next command.
885          * Segment 2 - Time from Driver receives a IO cmd start
886          * from NVME Layer to WQ put is done on IO cmd.
887          * Segment 3 - Time from Driver WQ put is done on IO cmd
888          * to MSI-X ISR for IO cmpl.
889          * Segment 4 - Time from MSI-X ISR for IO cmpl to when
890          * cmpl is handled off to the NVME Layer.
891          */
892         seg1 = lpfc_ncmd->ts_cmd_start - lpfc_ncmd->ts_last_cmd;
893         if (seg1 > 5000000)  /* 5 ms - for sequential IOs only */
894                 seg1 = 0;
895
896         /* Calculate times relative to start of IO */
897         seg2 = (lpfc_ncmd->ts_cmd_wqput - lpfc_ncmd->ts_cmd_start);
898         segsum = seg2;
899         seg3 = lpfc_ncmd->ts_isr_cmpl - lpfc_ncmd->ts_cmd_start;
900         if (segsum > seg3)
901                 return;
902         seg3 -= segsum;
903         segsum += seg3;
904
905         seg4 = lpfc_ncmd->ts_data_nvme - lpfc_ncmd->ts_cmd_start;
906         if (segsum > seg4)
907                 return;
908         seg4 -= segsum;
909
910         phba->ktime_data_samples++;
911         phba->ktime_seg1_total += seg1;
912         if (seg1 < phba->ktime_seg1_min)
913                 phba->ktime_seg1_min = seg1;
914         else if (seg1 > phba->ktime_seg1_max)
915                 phba->ktime_seg1_max = seg1;
916         phba->ktime_seg2_total += seg2;
917         if (seg2 < phba->ktime_seg2_min)
918                 phba->ktime_seg2_min = seg2;
919         else if (seg2 > phba->ktime_seg2_max)
920                 phba->ktime_seg2_max = seg2;
921         phba->ktime_seg3_total += seg3;
922         if (seg3 < phba->ktime_seg3_min)
923                 phba->ktime_seg3_min = seg3;
924         else if (seg3 > phba->ktime_seg3_max)
925                 phba->ktime_seg3_max = seg3;
926         phba->ktime_seg4_total += seg4;
927         if (seg4 < phba->ktime_seg4_min)
928                 phba->ktime_seg4_min = seg4;
929         else if (seg4 > phba->ktime_seg4_max)
930                 phba->ktime_seg4_max = seg4;
931
932         lpfc_ncmd->ts_last_cmd = 0;
933         lpfc_ncmd->ts_cmd_start = 0;
934         lpfc_ncmd->ts_cmd_wqput  = 0;
935         lpfc_ncmd->ts_isr_cmpl = 0;
936         lpfc_ncmd->ts_data_nvme = 0;
937 }
938 #endif
939
940 /**
941  * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
942  * @lpfc_pnvme: Pointer to the driver's nvme instance data
943  * @lpfc_nvme_lport: Pointer to the driver's local port data
944  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
945  *
946  * Driver registers this routine as it io request handler.  This
947  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
948  * data structure to the rport indicated in @lpfc_nvme_rport.
949  *
950  * Return value :
951  *   0 - Success
952  *   TODO: What are the failure codes.
953  **/
954 static void
955 lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
956                           struct lpfc_wcqe_complete *wcqe)
957 {
958         struct lpfc_nvme_buf *lpfc_ncmd =
959                 (struct lpfc_nvme_buf *)pwqeIn->context1;
960         struct lpfc_vport *vport = pwqeIn->vport;
961         struct nvmefc_fcp_req *nCmd;
962         struct nvme_fc_ersp_iu *ep;
963         struct nvme_fc_cmd_iu *cp;
964         struct lpfc_nodelist *ndlp;
965         struct lpfc_nvme_fcpreq_priv *freqpriv;
966         struct lpfc_nvme_lport *lport;
967         struct lpfc_nvme_ctrl_stat *cstat;
968         uint32_t code, status, idx;
969         uint16_t cid, sqhd, data;
970         uint32_t *ptr;
971
972         /* Sanity check on return of outstanding command */
973         if (!lpfc_ncmd || !lpfc_ncmd->nvmeCmd) {
974                 if (!lpfc_ncmd) {
975                         lpfc_printf_vlog(vport, KERN_ERR,
976                                          LOG_NODE | LOG_NVME_IOERR,
977                                          "6071 Null lpfc_ncmd pointer. No "
978                                          "release, skip completion\n");
979                         return;
980                 }
981
982                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
983                                  "6066 Missing cmpl ptrs: lpfc_ncmd %p, "
984                                  "nvmeCmd %p\n",
985                                  lpfc_ncmd, lpfc_ncmd->nvmeCmd);
986
987                 /* Release the lpfc_ncmd regardless of the missing elements. */
988                 lpfc_release_nvme_buf(phba, lpfc_ncmd);
989                 return;
990         }
991         nCmd = lpfc_ncmd->nvmeCmd;
992         status = bf_get(lpfc_wcqe_c_status, wcqe);
993
994         if (vport->localport) {
995                 lport = (struct lpfc_nvme_lport *)vport->localport->private;
996                 if (lport) {
997                         idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
998                         cstat = &lport->cstat[idx];
999                         atomic_inc(&cstat->fc4NvmeIoCmpls);
1000                         if (status) {
1001                                 if (bf_get(lpfc_wcqe_c_xb, wcqe))
1002                                         atomic_inc(&lport->cmpl_fcp_xb);
1003                                 atomic_inc(&lport->cmpl_fcp_err);
1004                         }
1005                 }
1006         }
1007
1008         lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
1009                          lpfc_ncmd->cur_iocbq.sli4_xritag,
1010                          status, wcqe->parameter);
1011         /*
1012          * Catch race where our node has transitioned, but the
1013          * transport is still transitioning.
1014          */
1015         ndlp = lpfc_ncmd->ndlp;
1016         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1017                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1018                                  "6062 Ignoring NVME cmpl.  No ndlp\n");
1019                 goto out_err;
1020         }
1021
1022         code = bf_get(lpfc_wcqe_c_code, wcqe);
1023         if (code == CQE_CODE_NVME_ERSP) {
1024                 /* For this type of CQE, we need to rebuild the rsp */
1025                 ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
1026
1027                 /*
1028                  * Get Command Id from cmd to plug into response. This
1029                  * code is not needed in the next NVME Transport drop.
1030                  */
1031                 cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
1032                 cid = cp->sqe.common.command_id;
1033
1034                 /*
1035                  * RSN is in CQE word 2
1036                  * SQHD is in CQE Word 3 bits 15:0
1037                  * Cmd Specific info is in CQE Word 1
1038                  * and in CQE Word 0 bits 15:0
1039                  */
1040                 sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
1041
1042                 /* Now lets build the NVME ERSP IU */
1043                 ep->iu_len = cpu_to_be16(8);
1044                 ep->rsn = wcqe->parameter;
1045                 ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
1046                 ep->rsvd12 = 0;
1047                 ptr = (uint32_t *)&ep->cqe.result.u64;
1048                 *ptr++ = wcqe->total_data_placed;
1049                 data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
1050                 *ptr = (uint32_t)data;
1051                 ep->cqe.sq_head = sqhd;
1052                 ep->cqe.sq_id =  nCmd->sqid;
1053                 ep->cqe.command_id = cid;
1054                 ep->cqe.status = 0;
1055
1056                 lpfc_ncmd->status = IOSTAT_SUCCESS;
1057                 lpfc_ncmd->result = 0;
1058                 nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
1059                 nCmd->transferred_length = nCmd->payload_length;
1060         } else {
1061                 lpfc_ncmd->status = (status & LPFC_IOCB_STATUS_MASK);
1062                 lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
1063
1064                 /* For NVME, the only failure path that results in an
1065                  * IO error is when the adapter rejects it.  All other
1066                  * conditions are a success case and resolved by the
1067                  * transport.
1068                  * IOSTAT_FCP_RSP_ERROR means:
1069                  * 1. Length of data received doesn't match total
1070                  *    transfer length in WQE
1071                  * 2. If the RSP payload does NOT match these cases:
1072                  *    a. RSP length 12/24 bytes and all zeros
1073                  *    b. NVME ERSP
1074                  */
1075                 switch (lpfc_ncmd->status) {
1076                 case IOSTAT_SUCCESS:
1077                         nCmd->transferred_length = wcqe->total_data_placed;
1078                         nCmd->rcv_rsplen = 0;
1079                         nCmd->status = 0;
1080                         break;
1081                 case IOSTAT_FCP_RSP_ERROR:
1082                         nCmd->transferred_length = wcqe->total_data_placed;
1083                         nCmd->rcv_rsplen = wcqe->parameter;
1084                         nCmd->status = 0;
1085                         /* Sanity check */
1086                         if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
1087                                 break;
1088                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1089                                          "6081 NVME Completion Protocol Error: "
1090                                          "xri %x status x%x result x%x "
1091                                          "placed x%x\n",
1092                                          lpfc_ncmd->cur_iocbq.sli4_xritag,
1093                                          lpfc_ncmd->status, lpfc_ncmd->result,
1094                                          wcqe->total_data_placed);
1095                         break;
1096                 case IOSTAT_LOCAL_REJECT:
1097                         /* Let fall through to set command final state. */
1098                         if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
1099                                 lpfc_printf_vlog(vport, KERN_INFO,
1100                                          LOG_NVME_IOERR,
1101                                          "6032 Delay Aborted cmd %p "
1102                                          "nvme cmd %p, xri x%x, "
1103                                          "xb %d\n",
1104                                          lpfc_ncmd, nCmd,
1105                                          lpfc_ncmd->cur_iocbq.sli4_xritag,
1106                                          bf_get(lpfc_wcqe_c_xb, wcqe));
1107                 default:
1108 out_err:
1109                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1110                                          "6072 NVME Completion Error: xri %x "
1111                                          "status x%x result x%x placed x%x\n",
1112                                          lpfc_ncmd->cur_iocbq.sli4_xritag,
1113                                          lpfc_ncmd->status, lpfc_ncmd->result,
1114                                          wcqe->total_data_placed);
1115                         nCmd->transferred_length = 0;
1116                         nCmd->rcv_rsplen = 0;
1117                         nCmd->status = NVME_SC_INTERNAL;
1118                 }
1119         }
1120
1121         /* pick up SLI4 exhange busy condition */
1122         if (bf_get(lpfc_wcqe_c_xb, wcqe))
1123                 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
1124         else
1125                 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
1126
1127         /* Update stats and complete the IO.  There is
1128          * no need for dma unprep because the nvme_transport
1129          * owns the dma address.
1130          */
1131 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1132         if (lpfc_ncmd->ts_cmd_start) {
1133                 lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
1134                 lpfc_ncmd->ts_data_nvme = ktime_get_ns();
1135                 phba->ktime_last_cmd = lpfc_ncmd->ts_data_nvme;
1136                 lpfc_nvme_ktime(phba, lpfc_ncmd);
1137         }
1138         if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1139                 if (lpfc_ncmd->cpu != smp_processor_id())
1140                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1141                                          "6701 CPU Check cmpl: "
1142                                          "cpu %d expect %d\n",
1143                                          smp_processor_id(), lpfc_ncmd->cpu);
1144                 if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
1145                         phba->cpucheck_cmpl_io[lpfc_ncmd->cpu]++;
1146         }
1147 #endif
1148
1149         /* NVME targets need completion held off until the abort exchange
1150          * completes unless the NVME Rport is getting unregistered.
1151          */
1152
1153         if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
1154                 freqpriv = nCmd->private;
1155                 freqpriv->nvme_buf = NULL;
1156                 nCmd->done(nCmd);
1157                 lpfc_ncmd->nvmeCmd = NULL;
1158         }
1159
1160         /* Call release with XB=1 to queue the IO into the abort list. */
1161         lpfc_release_nvme_buf(phba, lpfc_ncmd);
1162 }
1163
1164
1165 /**
1166  * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
1167  * @lpfc_pnvme: Pointer to the driver's nvme instance data
1168  * @lpfc_nvme_lport: Pointer to the driver's local port data
1169  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1170  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1171  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1172  *
1173  * Driver registers this routine as it io request handler.  This
1174  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1175  * data structure to the rport indicated in @lpfc_nvme_rport.
1176  *
1177  * Return value :
1178  *   0 - Success
1179  *   TODO: What are the failure codes.
1180  **/
1181 static int
1182 lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
1183                       struct lpfc_nvme_buf *lpfc_ncmd,
1184                       struct lpfc_nodelist *pnode,
1185                       struct lpfc_nvme_ctrl_stat *cstat)
1186 {
1187         struct lpfc_hba *phba = vport->phba;
1188         struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1189         struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
1190         union lpfc_wqe128 *wqe = &pwqeq->wqe;
1191         uint32_t req_len;
1192
1193         if (!NLP_CHK_NODE_ACT(pnode))
1194                 return -EINVAL;
1195
1196         /*
1197          * There are three possibilities here - use scatter-gather segment, use
1198          * the single mapping, or neither.
1199          */
1200         if (nCmd->sg_cnt) {
1201                 if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
1202                         /* From the iwrite template, initialize words 7 - 11 */
1203                         memcpy(&wqe->words[7],
1204                                &lpfc_iwrite_cmd_template.words[7],
1205                                sizeof(uint32_t) * 5);
1206
1207                         /* Word 4 */
1208                         wqe->fcp_iwrite.total_xfer_len = nCmd->payload_length;
1209
1210                         /* Word 5 */
1211                         if ((phba->cfg_nvme_enable_fb) &&
1212                             (pnode->nlp_flag & NLP_FIRSTBURST)) {
1213                                 req_len = lpfc_ncmd->nvmeCmd->payload_length;
1214                                 if (req_len < pnode->nvme_fb_size)
1215                                         wqe->fcp_iwrite.initial_xfer_len =
1216                                                 req_len;
1217                                 else
1218                                         wqe->fcp_iwrite.initial_xfer_len =
1219                                                 pnode->nvme_fb_size;
1220                         } else {
1221                                 wqe->fcp_iwrite.initial_xfer_len = 0;
1222                         }
1223                         atomic_inc(&cstat->fc4NvmeOutputRequests);
1224                 } else {
1225                         /* From the iread template, initialize words 7 - 11 */
1226                         memcpy(&wqe->words[7],
1227                                &lpfc_iread_cmd_template.words[7],
1228                                sizeof(uint32_t) * 5);
1229
1230                         /* Word 4 */
1231                         wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
1232
1233                         /* Word 5 */
1234                         wqe->fcp_iread.rsrvd5 = 0;
1235
1236                         atomic_inc(&cstat->fc4NvmeInputRequests);
1237                 }
1238         } else {
1239                 /* From the icmnd template, initialize words 4 - 11 */
1240                 memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
1241                        sizeof(uint32_t) * 8);
1242                 atomic_inc(&cstat->fc4NvmeControlRequests);
1243         }
1244         /*
1245          * Finish initializing those WQE fields that are independent
1246          * of the nvme_cmnd request_buffer
1247          */
1248
1249         /* Word 3 */
1250         bf_set(payload_offset_len, &wqe->fcp_icmd,
1251                (nCmd->rsplen + nCmd->cmdlen));
1252
1253         /* Word 6 */
1254         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
1255                phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
1256         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
1257
1258         /* Word 8 */
1259         wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
1260
1261         /* Word 9 */
1262         bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
1263
1264         /* Words 13 14 15 are for PBDE support */
1265
1266         pwqeq->vport = vport;
1267         return 0;
1268 }
1269
1270
1271 /**
1272  * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
1273  * @lpfc_pnvme: Pointer to the driver's nvme instance data
1274  * @lpfc_nvme_lport: Pointer to the driver's local port data
1275  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1276  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1277  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1278  *
1279  * Driver registers this routine as it io request handler.  This
1280  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1281  * data structure to the rport indicated in @lpfc_nvme_rport.
1282  *
1283  * Return value :
1284  *   0 - Success
1285  *   TODO: What are the failure codes.
1286  **/
1287 static int
1288 lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
1289                       struct lpfc_nvme_buf *lpfc_ncmd)
1290 {
1291         struct lpfc_hba *phba = vport->phba;
1292         struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1293         union lpfc_wqe128 *wqe = &lpfc_ncmd->cur_iocbq.wqe;
1294         struct sli4_sge *sgl = lpfc_ncmd->dma_sgl;
1295         struct scatterlist *data_sg;
1296         struct sli4_sge *first_data_sgl;
1297         struct ulp_bde64 *bde;
1298         dma_addr_t physaddr;
1299         uint32_t num_bde = 0;
1300         uint32_t dma_len;
1301         uint32_t dma_offset = 0;
1302         int nseg, i;
1303
1304         /* Fix up the command and response DMA stuff. */
1305         lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
1306
1307         /*
1308          * There are three possibilities here - use scatter-gather segment, use
1309          * the single mapping, or neither.
1310          */
1311         if (nCmd->sg_cnt) {
1312                 /*
1313                  * Jump over the cmd and rsp SGEs.  The fix routine
1314                  * has already adjusted for this.
1315                  */
1316                 sgl += 2;
1317
1318                 first_data_sgl = sgl;
1319                 lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
1320                 if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
1321                         lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1322                                         "6058 Too many sg segments from "
1323                                         "NVME Transport.  Max %d, "
1324                                         "nvmeIO sg_cnt %d\n",
1325                                         phba->cfg_nvme_seg_cnt + 1,
1326                                         lpfc_ncmd->seg_cnt);
1327                         lpfc_ncmd->seg_cnt = 0;
1328                         return 1;
1329                 }
1330
1331                 /*
1332                  * The driver established a maximum scatter-gather segment count
1333                  * during probe that limits the number of sg elements in any
1334                  * single nvme command.  Just run through the seg_cnt and format
1335                  * the sge's.
1336                  */
1337                 nseg = nCmd->sg_cnt;
1338                 data_sg = nCmd->first_sgl;
1339                 for (i = 0; i < nseg; i++) {
1340                         if (data_sg == NULL) {
1341                                 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1342                                                 "6059 dptr err %d, nseg %d\n",
1343                                                 i, nseg);
1344                                 lpfc_ncmd->seg_cnt = 0;
1345                                 return 1;
1346                         }
1347                         physaddr = data_sg->dma_address;
1348                         dma_len = data_sg->length;
1349                         sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1350                         sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1351                         sgl->word2 = le32_to_cpu(sgl->word2);
1352                         if ((num_bde + 1) == nseg)
1353                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
1354                         else
1355                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
1356                         bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1357                         bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
1358                         sgl->word2 = cpu_to_le32(sgl->word2);
1359                         sgl->sge_len = cpu_to_le32(dma_len);
1360
1361                         dma_offset += dma_len;
1362                         data_sg = sg_next(data_sg);
1363                         sgl++;
1364                 }
1365                 if (phba->cfg_enable_pbde) {
1366                         /* Use PBDE support for first SGL only, offset == 0 */
1367                         /* Words 13-15 */
1368                         bde = (struct ulp_bde64 *)
1369                                 &wqe->words[13];
1370                         bde->addrLow = first_data_sgl->addr_lo;
1371                         bde->addrHigh = first_data_sgl->addr_hi;
1372                         bde->tus.f.bdeSize =
1373                                 le32_to_cpu(first_data_sgl->sge_len);
1374                         bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1375                         bde->tus.w = cpu_to_le32(bde->tus.w);
1376                         /* wqe_pbde is 1 in template */
1377                 } else {
1378                         memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
1379                         bf_set(wqe_pbde, &wqe->generic.wqe_com, 0);
1380                 }
1381
1382         } else {
1383                 lpfc_ncmd->seg_cnt = 0;
1384
1385                 /* For this clause to be valid, the payload_length
1386                  * and sg_cnt must zero.
1387                  */
1388                 if (nCmd->payload_length != 0) {
1389                         lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1390                                         "6063 NVME DMA Prep Err: sg_cnt %d "
1391                                         "payload_length x%x\n",
1392                                         nCmd->sg_cnt, nCmd->payload_length);
1393                         return 1;
1394                 }
1395         }
1396         return 0;
1397 }
1398
1399 /**
1400  * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
1401  * @lpfc_pnvme: Pointer to the driver's nvme instance data
1402  * @lpfc_nvme_lport: Pointer to the driver's local port data
1403  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1404  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1405  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1406  *
1407  * Driver registers this routine as it io request handler.  This
1408  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1409  * data structure to the rport
1410  indicated in @lpfc_nvme_rport.
1411  *
1412  * Return value :
1413  *   0 - Success
1414  *   TODO: What are the failure codes.
1415  **/
1416 static int
1417 lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
1418                         struct nvme_fc_remote_port *pnvme_rport,
1419                         void *hw_queue_handle,
1420                         struct nvmefc_fcp_req *pnvme_fcreq)
1421 {
1422         int ret = 0;
1423         int expedite = 0;
1424         int idx;
1425         struct lpfc_nvme_lport *lport;
1426         struct lpfc_nvme_ctrl_stat *cstat;
1427         struct lpfc_vport *vport;
1428         struct lpfc_hba *phba;
1429         struct lpfc_nodelist *ndlp;
1430         struct lpfc_nvme_buf *lpfc_ncmd;
1431         struct lpfc_nvme_rport *rport;
1432         struct lpfc_nvme_qhandle *lpfc_queue_info;
1433         struct lpfc_nvme_fcpreq_priv *freqpriv;
1434         struct nvme_common_command *sqe;
1435 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1436         uint64_t start = 0;
1437 #endif
1438
1439         /* Validate pointers. LLDD fault handling with transport does
1440          * have timing races.
1441          */
1442         lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1443         if (unlikely(!lport)) {
1444                 ret = -EINVAL;
1445                 goto out_fail;
1446         }
1447
1448         vport = lport->vport;
1449
1450         if (unlikely(!hw_queue_handle)) {
1451                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1452                                  "6117 Fail IO, NULL hw_queue_handle\n");
1453                 atomic_inc(&lport->xmt_fcp_err);
1454                 ret = -EBUSY;
1455                 goto out_fail;
1456         }
1457
1458         phba = vport->phba;
1459
1460         if (vport->load_flag & FC_UNLOADING) {
1461                 ret = -ENODEV;
1462                 goto out_fail;
1463         }
1464
1465         if (vport->load_flag & FC_UNLOADING) {
1466                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1467                                  "6124 Fail IO, Driver unload\n");
1468                 atomic_inc(&lport->xmt_fcp_err);
1469                 ret = -ENODEV;
1470                 goto out_fail;
1471         }
1472
1473         freqpriv = pnvme_fcreq->private;
1474         if (unlikely(!freqpriv)) {
1475                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1476                                  "6158 Fail IO, NULL request data\n");
1477                 atomic_inc(&lport->xmt_fcp_err);
1478                 ret = -EINVAL;
1479                 goto out_fail;
1480         }
1481
1482 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1483         if (phba->ktime_on)
1484                 start = ktime_get_ns();
1485 #endif
1486         rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
1487         lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
1488
1489         /*
1490          * Catch race where our node has transitioned, but the
1491          * transport is still transitioning.
1492          */
1493         ndlp = rport->ndlp;
1494         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1495                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
1496                                  "6053 Fail IO, ndlp not ready: rport %p "
1497                                   "ndlp %p, DID x%06x\n",
1498                                  rport, ndlp, pnvme_rport->port_id);
1499                 atomic_inc(&lport->xmt_fcp_err);
1500                 ret = -EBUSY;
1501                 goto out_fail;
1502         }
1503
1504         /* The remote node has to be a mapped target or it's an error. */
1505         if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
1506             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
1507                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
1508                                  "6036 Fail IO, DID x%06x not ready for "
1509                                  "IO. State x%x, Type x%x Flg x%x\n",
1510                                  pnvme_rport->port_id,
1511                                  ndlp->nlp_state, ndlp->nlp_type,
1512                                  ndlp->upcall_flags);
1513                 atomic_inc(&lport->xmt_fcp_bad_ndlp);
1514                 ret = -EBUSY;
1515                 goto out_fail;
1516
1517         }
1518
1519         /* Currently only NVME Keep alive commands should be expedited
1520          * if the driver runs out of a resource. These should only be
1521          * issued on the admin queue, qidx 0
1522          */
1523         if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
1524                 sqe = &((struct nvme_fc_cmd_iu *)
1525                         pnvme_fcreq->cmdaddr)->sqe.common;
1526                 if (sqe->opcode == nvme_admin_keep_alive)
1527                         expedite = 1;
1528         }
1529
1530         /* The node is shared with FCP IO, make sure the IO pending count does
1531          * not exceed the programmed depth.
1532          */
1533         if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1534                 if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
1535                     !expedite) {
1536                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1537                                          "6174 Fail IO, ndlp qdepth exceeded: "
1538                                          "idx %d DID %x pend %d qdepth %d\n",
1539                                          lpfc_queue_info->index, ndlp->nlp_DID,
1540                                          atomic_read(&ndlp->cmd_pending),
1541                                          ndlp->cmd_qdepth);
1542                         atomic_inc(&lport->xmt_fcp_qdepth);
1543                         ret = -EBUSY;
1544                         goto out_fail;
1545                 }
1546         }
1547
1548         lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, expedite);
1549         if (lpfc_ncmd == NULL) {
1550                 atomic_inc(&lport->xmt_fcp_noxri);
1551                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1552                                  "6065 Fail IO, driver buffer pool is empty: "
1553                                  "idx %d DID %x\n",
1554                                  lpfc_queue_info->index, ndlp->nlp_DID);
1555                 ret = -EBUSY;
1556                 goto out_fail;
1557         }
1558 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1559         if (start) {
1560                 lpfc_ncmd->ts_cmd_start = start;
1561                 lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
1562         } else {
1563                 lpfc_ncmd->ts_cmd_start = 0;
1564         }
1565 #endif
1566
1567         /*
1568          * Store the data needed by the driver to issue, abort, and complete
1569          * an IO.
1570          * Do not let the IO hang out forever.  There is no midlayer issuing
1571          * an abort so inform the FW of the maximum IO pending time.
1572          */
1573         freqpriv->nvme_buf = lpfc_ncmd;
1574         lpfc_ncmd->nvmeCmd = pnvme_fcreq;
1575         lpfc_ncmd->ndlp = ndlp;
1576         lpfc_ncmd->qidx = lpfc_queue_info->qidx;
1577
1578         /*
1579          * Issue the IO on the WQ indicated by index in the hw_queue_handle.
1580          * This identfier was create in our hardware queue create callback
1581          * routine. The driver now is dependent on the IO queue steering from
1582          * the transport.  We are trusting the upper NVME layers know which
1583          * index to use and that they have affinitized a CPU to this hardware
1584          * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
1585          */
1586         idx = lpfc_queue_info->index;
1587         lpfc_ncmd->cur_iocbq.hba_wqidx = idx;
1588         cstat = &lport->cstat[idx];
1589
1590         lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp, cstat);
1591         ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
1592         if (ret) {
1593                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1594                                  "6175 Fail IO, Prep DMA: "
1595                                  "idx %d DID %x\n",
1596                                  lpfc_queue_info->index, ndlp->nlp_DID);
1597                 atomic_inc(&lport->xmt_fcp_err);
1598                 ret = -ENOMEM;
1599                 goto out_free_nvme_buf;
1600         }
1601
1602         lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1603                          lpfc_ncmd->cur_iocbq.sli4_xritag,
1604                          lpfc_queue_info->index, ndlp->nlp_DID);
1605
1606         ret = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, &lpfc_ncmd->cur_iocbq);
1607         if (ret) {
1608                 atomic_inc(&lport->xmt_fcp_wqerr);
1609                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1610                                  "6113 Fail IO, Could not issue WQE err %x "
1611                                  "sid: x%x did: x%x oxid: x%x\n",
1612                                  ret, vport->fc_myDID, ndlp->nlp_DID,
1613                                  lpfc_ncmd->cur_iocbq.sli4_xritag);
1614                 goto out_free_nvme_buf;
1615         }
1616
1617 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1618         if (lpfc_ncmd->ts_cmd_start)
1619                 lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1620
1621         if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1622                 lpfc_ncmd->cpu = smp_processor_id();
1623                 if (lpfc_ncmd->cpu != lpfc_queue_info->index) {
1624                         /* Check for admin queue */
1625                         if (lpfc_queue_info->qidx) {
1626                                 lpfc_printf_vlog(vport,
1627                                                  KERN_ERR, LOG_NVME_IOERR,
1628                                                 "6702 CPU Check cmd: "
1629                                                 "cpu %d wq %d\n",
1630                                                 lpfc_ncmd->cpu,
1631                                                 lpfc_queue_info->index);
1632                         }
1633                         lpfc_ncmd->cpu = lpfc_queue_info->index;
1634                 }
1635                 if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
1636                         phba->cpucheck_xmt_io[lpfc_ncmd->cpu]++;
1637         }
1638 #endif
1639         return 0;
1640
1641  out_free_nvme_buf:
1642         if (lpfc_ncmd->nvmeCmd->sg_cnt) {
1643                 if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
1644                         atomic_dec(&cstat->fc4NvmeOutputRequests);
1645                 else
1646                         atomic_dec(&cstat->fc4NvmeInputRequests);
1647         } else
1648                 atomic_dec(&cstat->fc4NvmeControlRequests);
1649         lpfc_release_nvme_buf(phba, lpfc_ncmd);
1650  out_fail:
1651         return ret;
1652 }
1653
1654 /**
1655  * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
1656  * @phba: Pointer to HBA context object
1657  * @cmdiocb: Pointer to command iocb object.
1658  * @rspiocb: Pointer to response iocb object.
1659  *
1660  * This is the callback function for any NVME FCP IO that was aborted.
1661  *
1662  * Return value:
1663  *   None
1664  **/
1665 void
1666 lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1667                            struct lpfc_wcqe_complete *abts_cmpl)
1668 {
1669         lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
1670                         "6145 ABORT_XRI_CN completing on rpi x%x "
1671                         "original iotag x%x, abort cmd iotag x%x "
1672                         "req_tag x%x, status x%x, hwstatus x%x\n",
1673                         cmdiocb->iocb.un.acxri.abortContextTag,
1674                         cmdiocb->iocb.un.acxri.abortIoTag,
1675                         cmdiocb->iotag,
1676                         bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
1677                         bf_get(lpfc_wcqe_c_status, abts_cmpl),
1678                         bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
1679         lpfc_sli_release_iocbq(phba, cmdiocb);
1680 }
1681
1682 /**
1683  * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
1684  * @lpfc_pnvme: Pointer to the driver's nvme instance data
1685  * @lpfc_nvme_lport: Pointer to the driver's local port data
1686  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1687  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1688  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1689  *
1690  * Driver registers this routine as its nvme request io abort handler.  This
1691  * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
1692  * data structure to the rport indicated in @lpfc_nvme_rport.  This routine
1693  * is executed asynchronously - one the target is validated as "MAPPED" and
1694  * ready for IO, the driver issues the abort request and returns.
1695  *
1696  * Return value:
1697  *   None
1698  **/
1699 static void
1700 lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
1701                     struct nvme_fc_remote_port *pnvme_rport,
1702                     void *hw_queue_handle,
1703                     struct nvmefc_fcp_req *pnvme_fcreq)
1704 {
1705         struct lpfc_nvme_lport *lport;
1706         struct lpfc_vport *vport;
1707         struct lpfc_hba *phba;
1708         struct lpfc_nvme_buf *lpfc_nbuf;
1709         struct lpfc_iocbq *abts_buf;
1710         struct lpfc_iocbq *nvmereq_wqe;
1711         struct lpfc_nvme_fcpreq_priv *freqpriv;
1712         union lpfc_wqe128 *abts_wqe;
1713         unsigned long flags;
1714         int ret_val;
1715
1716         /* Validate pointers. LLDD fault handling with transport does
1717          * have timing races.
1718          */
1719         lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1720         if (unlikely(!lport))
1721                 return;
1722
1723         vport = lport->vport;
1724
1725         if (unlikely(!hw_queue_handle)) {
1726                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1727                                  "6129 Fail Abort, HW Queue Handle NULL.\n");
1728                 return;
1729         }
1730
1731         phba = vport->phba;
1732         freqpriv = pnvme_fcreq->private;
1733
1734         if (unlikely(!freqpriv))
1735                 return;
1736         if (vport->load_flag & FC_UNLOADING)
1737                 return;
1738
1739         /* Announce entry to new IO submit field. */
1740         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1741                          "6002 Abort Request to rport DID x%06x "
1742                          "for nvme_fc_req %p\n",
1743                          pnvme_rport->port_id,
1744                          pnvme_fcreq);
1745
1746         /* If the hba is getting reset, this flag is set.  It is
1747          * cleared when the reset is complete and rings reestablished.
1748          */
1749         spin_lock_irqsave(&phba->hbalock, flags);
1750         /* driver queued commands are in process of being flushed */
1751         if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
1752                 spin_unlock_irqrestore(&phba->hbalock, flags);
1753                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1754                                  "6139 Driver in reset cleanup - flushing "
1755                                  "NVME Req now.  hba_flag x%x\n",
1756                                  phba->hba_flag);
1757                 return;
1758         }
1759
1760         lpfc_nbuf = freqpriv->nvme_buf;
1761         if (!lpfc_nbuf) {
1762                 spin_unlock_irqrestore(&phba->hbalock, flags);
1763                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1764                                  "6140 NVME IO req has no matching lpfc nvme "
1765                                  "io buffer.  Skipping abort req.\n");
1766                 return;
1767         } else if (!lpfc_nbuf->nvmeCmd) {
1768                 spin_unlock_irqrestore(&phba->hbalock, flags);
1769                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1770                                  "6141 lpfc NVME IO req has no nvme_fcreq "
1771                                  "io buffer.  Skipping abort req.\n");
1772                 return;
1773         }
1774         nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
1775
1776         /*
1777          * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
1778          * state must match the nvme_fcreq passed by the nvme
1779          * transport.  If they don't match, it is likely the driver
1780          * has already completed the NVME IO and the nvme transport
1781          * has not seen it yet.
1782          */
1783         if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
1784                 spin_unlock_irqrestore(&phba->hbalock, flags);
1785                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1786                                  "6143 NVME req mismatch: "
1787                                  "lpfc_nbuf %p nvmeCmd %p, "
1788                                  "pnvme_fcreq %p.  Skipping Abort xri x%x\n",
1789                                  lpfc_nbuf, lpfc_nbuf->nvmeCmd,
1790                                  pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1791                 return;
1792         }
1793
1794         /* Don't abort IOs no longer on the pending queue. */
1795         if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
1796                 spin_unlock_irqrestore(&phba->hbalock, flags);
1797                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1798                                  "6142 NVME IO req %p not queued - skipping "
1799                                  "abort req xri x%x\n",
1800                                  pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1801                 return;
1802         }
1803
1804         atomic_inc(&lport->xmt_fcp_abort);
1805         lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1806                          nvmereq_wqe->sli4_xritag,
1807                          nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
1808
1809         /* Outstanding abort is in progress */
1810         if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
1811                 spin_unlock_irqrestore(&phba->hbalock, flags);
1812                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1813                                  "6144 Outstanding NVME I/O Abort Request "
1814                                  "still pending on nvme_fcreq %p, "
1815                                  "lpfc_ncmd %p xri x%x\n",
1816                                  pnvme_fcreq, lpfc_nbuf,
1817                                  nvmereq_wqe->sli4_xritag);
1818                 return;
1819         }
1820
1821         abts_buf = __lpfc_sli_get_iocbq(phba);
1822         if (!abts_buf) {
1823                 spin_unlock_irqrestore(&phba->hbalock, flags);
1824                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1825                                  "6136 No available abort wqes. Skipping "
1826                                  "Abts req for nvme_fcreq %p xri x%x\n",
1827                                  pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1828                 return;
1829         }
1830
1831         /* Ready - mark outstanding as aborted by driver. */
1832         nvmereq_wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
1833
1834         /* Complete prepping the abort wqe and issue to the FW. */
1835         abts_wqe = &abts_buf->wqe;
1836
1837         /* WQEs are reused.  Clear stale data and set key fields to
1838          * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
1839          */
1840         memset(abts_wqe, 0, sizeof(union lpfc_wqe));
1841         bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
1842
1843         /* word 7 */
1844         bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
1845         bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
1846                nvmereq_wqe->iocb.ulpClass);
1847
1848         /* word 8 - tell the FW to abort the IO associated with this
1849          * outstanding exchange ID.
1850          */
1851         abts_wqe->abort_cmd.wqe_com.abort_tag = nvmereq_wqe->sli4_xritag;
1852
1853         /* word 9 - this is the iotag for the abts_wqe completion. */
1854         bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
1855                abts_buf->iotag);
1856
1857         /* word 10 */
1858         bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
1859         bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
1860
1861         /* word 11 */
1862         bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
1863         bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
1864         bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1865
1866         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
1867         abts_buf->iocb_flag |= LPFC_IO_NVME;
1868         abts_buf->hba_wqidx = nvmereq_wqe->hba_wqidx;
1869         abts_buf->vport = vport;
1870         abts_buf->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
1871         ret_val = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_buf);
1872         spin_unlock_irqrestore(&phba->hbalock, flags);
1873         if (ret_val) {
1874                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1875                                  "6137 Failed abts issue_wqe with status x%x "
1876                                  "for nvme_fcreq %p.\n",
1877                                  ret_val, pnvme_fcreq);
1878                 lpfc_sli_release_iocbq(phba, abts_buf);
1879                 return;
1880         }
1881
1882         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1883                          "6138 Transport Abort NVME Request Issued for "
1884                          "ox_id x%x on reqtag x%x\n",
1885                          nvmereq_wqe->sli4_xritag,
1886                          abts_buf->iotag);
1887 }
1888
1889 /* Declare and initialization an instance of the FC NVME template. */
1890 static struct nvme_fc_port_template lpfc_nvme_template = {
1891         /* initiator-based functions */
1892         .localport_delete  = lpfc_nvme_localport_delete,
1893         .remoteport_delete = lpfc_nvme_remoteport_delete,
1894         .create_queue = lpfc_nvme_create_queue,
1895         .delete_queue = lpfc_nvme_delete_queue,
1896         .ls_req       = lpfc_nvme_ls_req,
1897         .fcp_io       = lpfc_nvme_fcp_io_submit,
1898         .ls_abort     = lpfc_nvme_ls_abort,
1899         .fcp_abort    = lpfc_nvme_fcp_abort,
1900
1901         .max_hw_queues = 1,
1902         .max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
1903         .max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
1904         .dma_boundary = 0xFFFFFFFF,
1905
1906         /* Sizes of additional private data for data structures.
1907          * No use for the last two sizes at this time.
1908          */
1909         .local_priv_sz = sizeof(struct lpfc_nvme_lport),
1910         .remote_priv_sz = sizeof(struct lpfc_nvme_rport),
1911         .lsrqst_priv_sz = 0,
1912         .fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
1913 };
1914
1915 static inline struct lpfc_nvme_buf *
1916 lpfc_nvme_buf(struct lpfc_hba *phba)
1917 {
1918         struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
1919
1920         list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
1921                                  &phba->lpfc_common_buf_list_get, list) {
1922                 list_del_init(&lpfc_ncmd->list);
1923                 phba->get_common_bufs--;
1924                 return lpfc_ncmd;
1925         }
1926         return NULL;
1927 }
1928
1929 /**
1930  * lpfc_get_nvme_buf - Get a nvme buffer from lpfc_common_buf_list of the HBA
1931  * @phba: The HBA for which this call is being executed.
1932  *
1933  * This routine removes a nvme buffer from head of @phba lpfc_common_buf_list
1934  * and returns to caller.
1935  *
1936  * Return codes:
1937  *   NULL - Error
1938  *   Pointer to lpfc_nvme_buf - Success
1939  **/
1940 static struct lpfc_nvme_buf *
1941 lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1942                   int expedite)
1943 {
1944         struct lpfc_nvme_buf *lpfc_ncmd = NULL;
1945         struct sli4_sge *sgl;
1946         struct lpfc_iocbq *pwqeq;
1947         union lpfc_wqe128 *wqe;
1948         unsigned long iflag = 0;
1949
1950         spin_lock_irqsave(&phba->common_buf_list_get_lock, iflag);
1951         if (phba->get_common_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
1952                 lpfc_ncmd = lpfc_nvme_buf(phba);
1953         if (!lpfc_ncmd) {
1954                 spin_lock(&phba->common_buf_list_put_lock);
1955                 list_splice(&phba->lpfc_common_buf_list_put,
1956                             &phba->lpfc_common_buf_list_get);
1957                 phba->get_common_bufs += phba->put_common_bufs;
1958                 INIT_LIST_HEAD(&phba->lpfc_common_buf_list_put);
1959                 phba->put_common_bufs = 0;
1960                 spin_unlock(&phba->common_buf_list_put_lock);
1961                 if (phba->get_common_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
1962                     expedite)
1963                         lpfc_ncmd = lpfc_nvme_buf(phba);
1964         }
1965         spin_unlock_irqrestore(&phba->common_buf_list_get_lock, iflag);
1966
1967         if (lpfc_ncmd) {
1968                 pwqeq = &(lpfc_ncmd->cur_iocbq);
1969                 wqe = &pwqeq->wqe;
1970
1971                 /* Setup key fields in buffer that may have been changed
1972                  * if other protocols used this buffer.
1973                  */
1974                 pwqeq->iocb_flag = LPFC_IO_NVME;
1975                 pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
1976                 lpfc_ncmd->start_time = jiffies;
1977                 lpfc_ncmd->flags = 0;
1978
1979                 /* Rsp SGE will be filled in when we rcv an IO
1980                  * from the NVME Layer to be sent.
1981                  * The cmd is going to be embedded so we need a SKIP SGE.
1982                  */
1983                 sgl = lpfc_ncmd->dma_sgl;
1984                 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
1985                 bf_set(lpfc_sli4_sge_last, sgl, 0);
1986                 sgl->word2 = cpu_to_le32(sgl->word2);
1987                 /* Fill in word 3 / sgl_len during cmd submission */
1988
1989                 /* Initialize WQE */
1990                 memset(wqe, 0, sizeof(union lpfc_wqe));
1991
1992                 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1993                         atomic_inc(&ndlp->cmd_pending);
1994                         lpfc_ncmd->flags |= LPFC_BUMP_QDEPTH;
1995                 }
1996         }
1997         return  lpfc_ncmd;
1998 }
1999
2000 /**
2001  * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
2002  * @phba: The Hba for which this call is being executed.
2003  * @lpfc_ncmd: The nvme buffer which is being released.
2004  *
2005  * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
2006  * lpfc_common_buf_list list. For SLI4 XRI's are tied to the nvme buffer
2007  * and cannot be reused for at least RA_TOV amount of time if it was
2008  * aborted.
2009  **/
2010 static void
2011 lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_nvme_buf *lpfc_ncmd)
2012 {
2013         unsigned long iflag = 0;
2014
2015         if ((lpfc_ncmd->flags & LPFC_BUMP_QDEPTH) && lpfc_ncmd->ndlp)
2016                 atomic_dec(&lpfc_ncmd->ndlp->cmd_pending);
2017
2018         lpfc_ncmd->ndlp = NULL;
2019         lpfc_ncmd->flags &= ~LPFC_BUMP_QDEPTH;
2020
2021         if (lpfc_ncmd->flags & LPFC_SBUF_XBUSY) {
2022                 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2023                                 "6310 XB release deferred for "
2024                                 "ox_id x%x on reqtag x%x\n",
2025                                 lpfc_ncmd->cur_iocbq.sli4_xritag,
2026                                 lpfc_ncmd->cur_iocbq.iotag);
2027
2028                 spin_lock_irqsave(&phba->sli4_hba.abts_nvme_buf_list_lock,
2029                                         iflag);
2030                 list_add_tail(&lpfc_ncmd->list,
2031                         &phba->sli4_hba.lpfc_abts_nvme_buf_list);
2032                 spin_unlock_irqrestore(&phba->sli4_hba.abts_nvme_buf_list_lock,
2033                                         iflag);
2034         } else {
2035                 /* MUST zero fields if buffer is reused by another protocol */
2036                 lpfc_ncmd->nvmeCmd = NULL;
2037                 lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
2038                 spin_lock_irqsave(&phba->common_buf_list_put_lock, iflag);
2039                 list_add_tail(&lpfc_ncmd->list,
2040                               &phba->lpfc_common_buf_list_put);
2041                 phba->put_common_bufs++;
2042                 spin_unlock_irqrestore(&phba->common_buf_list_put_lock, iflag);
2043         }
2044 }
2045
2046 /**
2047  * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
2048  * @pvport - the lpfc_vport instance requesting a localport.
2049  *
2050  * This routine is invoked to create an nvme localport instance to bind
2051  * to the nvme_fc_transport.  It is called once during driver load
2052  * like lpfc_create_shost after all other services are initialized.
2053  * It requires a vport, vpi, and wwns at call time.  Other localport
2054  * parameters are modified as the driver's FCID and the Fabric WWN
2055  * are established.
2056  *
2057  * Return codes
2058  *      0 - successful
2059  *      -ENOMEM - no heap memory available
2060  *      other values - from nvme registration upcall
2061  **/
2062 int
2063 lpfc_nvme_create_localport(struct lpfc_vport *vport)
2064 {
2065         int ret = 0;
2066         struct lpfc_hba  *phba = vport->phba;
2067         struct nvme_fc_port_info nfcp_info;
2068         struct nvme_fc_local_port *localport;
2069         struct lpfc_nvme_lport *lport;
2070         struct lpfc_nvme_ctrl_stat *cstat;
2071         int i;
2072
2073         /* Initialize this localport instance.  The vport wwn usage ensures
2074          * that NPIV is accounted for.
2075          */
2076         memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
2077         nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
2078         nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
2079         nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
2080
2081         /* We need to tell the transport layer + 1 because it takes page
2082          * alignment into account. When space for the SGL is allocated we
2083          * allocate + 3, one for cmd, one for rsp and one for this alignment
2084          */
2085         lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
2086         lpfc_nvme_template.max_hw_queues = phba->cfg_hdw_queue;
2087
2088         cstat = kmalloc((sizeof(struct lpfc_nvme_ctrl_stat) *
2089                         phba->cfg_hdw_queue), GFP_KERNEL);
2090         if (!cstat)
2091                 return -ENOMEM;
2092
2093         /* localport is allocated from the stack, but the registration
2094          * call allocates heap memory as well as the private area.
2095          */
2096 #if (IS_ENABLED(CONFIG_NVME_FC))
2097         ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
2098                                          &vport->phba->pcidev->dev, &localport);
2099 #else
2100         ret = -ENOMEM;
2101 #endif
2102         if (!ret) {
2103                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
2104                                  "6005 Successfully registered local "
2105                                  "NVME port num %d, localP %p, private %p, "
2106                                  "sg_seg %d\n",
2107                                  localport->port_num, localport,
2108                                  localport->private,
2109                                  lpfc_nvme_template.max_sgl_segments);
2110
2111                 /* Private is our lport size declared in the template. */
2112                 lport = (struct lpfc_nvme_lport *)localport->private;
2113                 vport->localport = localport;
2114                 lport->vport = vport;
2115                 lport->cstat = cstat;
2116                 vport->nvmei_support = 1;
2117
2118                 atomic_set(&lport->xmt_fcp_noxri, 0);
2119                 atomic_set(&lport->xmt_fcp_bad_ndlp, 0);
2120                 atomic_set(&lport->xmt_fcp_qdepth, 0);
2121                 atomic_set(&lport->xmt_fcp_err, 0);
2122                 atomic_set(&lport->xmt_fcp_wqerr, 0);
2123                 atomic_set(&lport->xmt_fcp_abort, 0);
2124                 atomic_set(&lport->xmt_ls_abort, 0);
2125                 atomic_set(&lport->xmt_ls_err, 0);
2126                 atomic_set(&lport->cmpl_fcp_xb, 0);
2127                 atomic_set(&lport->cmpl_fcp_err, 0);
2128                 atomic_set(&lport->cmpl_ls_xb, 0);
2129                 atomic_set(&lport->cmpl_ls_err, 0);
2130                 atomic_set(&lport->fc4NvmeLsRequests, 0);
2131                 atomic_set(&lport->fc4NvmeLsCmpls, 0);
2132
2133                 for (i = 0; i < phba->cfg_hdw_queue; i++) {
2134                         cstat = &lport->cstat[i];
2135                         atomic_set(&cstat->fc4NvmeInputRequests, 0);
2136                         atomic_set(&cstat->fc4NvmeOutputRequests, 0);
2137                         atomic_set(&cstat->fc4NvmeControlRequests, 0);
2138                         atomic_set(&cstat->fc4NvmeIoCmpls, 0);
2139                 }
2140         } else
2141                 kfree(cstat);
2142
2143         return ret;
2144 }
2145
2146 /* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2147  *
2148  * The driver has to wait for the host nvme transport to callback
2149  * indicating the localport has successfully unregistered all
2150  * resources.  Since this is an uninterruptible wait, loop every ten
2151  * seconds and print a message indicating no progress.
2152  *
2153  * An uninterruptible wait is used because of the risk of transport-to-
2154  * driver state mismatch.
2155  */
2156 void
2157 lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
2158                            struct lpfc_nvme_lport *lport)
2159 {
2160 #if (IS_ENABLED(CONFIG_NVME_FC))
2161         u32 wait_tmo;
2162         int ret;
2163
2164         /* Host transport has to clean up and confirm requiring an indefinite
2165          * wait. Print a message if a 10 second wait expires and renew the
2166          * wait. This is unexpected.
2167          */
2168         wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2169         while (true) {
2170                 ret = wait_for_completion_timeout(&lport->lport_unreg_done,
2171                                                   wait_tmo);
2172                 if (unlikely(!ret)) {
2173                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
2174                                          "6176 Lport %p Localport %p wait "
2175                                          "timed out. Renewing.\n",
2176                                          lport, vport->localport);
2177                         continue;
2178                 }
2179                 break;
2180         }
2181         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
2182                          "6177 Lport %p Localport %p Complete Success\n",
2183                          lport, vport->localport);
2184 #endif
2185 }
2186
2187 /**
2188  * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
2189  * @pnvme: pointer to lpfc nvme data structure.
2190  *
2191  * This routine is invoked to destroy all lports bound to the phba.
2192  * The lport memory was allocated by the nvme fc transport and is
2193  * released there.  This routine ensures all rports bound to the
2194  * lport have been disconnected.
2195  *
2196  **/
2197 void
2198 lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
2199 {
2200 #if (IS_ENABLED(CONFIG_NVME_FC))
2201         struct nvme_fc_local_port *localport;
2202         struct lpfc_nvme_lport *lport;
2203         struct lpfc_nvme_ctrl_stat *cstat;
2204         int ret;
2205
2206         if (vport->nvmei_support == 0)
2207                 return;
2208
2209         localport = vport->localport;
2210         vport->localport = NULL;
2211         lport = (struct lpfc_nvme_lport *)localport->private;
2212         cstat = lport->cstat;
2213
2214         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2215                          "6011 Destroying NVME localport %p\n",
2216                          localport);
2217
2218         /* lport's rport list is clear.  Unregister
2219          * lport and release resources.
2220          */
2221         init_completion(&lport->lport_unreg_done);
2222         ret = nvme_fc_unregister_localport(localport);
2223
2224         /* Wait for completion.  This either blocks
2225          * indefinitely or succeeds
2226          */
2227         lpfc_nvme_lport_unreg_wait(vport, lport);
2228         kfree(cstat);
2229
2230         /* Regardless of the unregister upcall response, clear
2231          * nvmei_support.  All rports are unregistered and the
2232          * driver will clean up.
2233          */
2234         vport->nvmei_support = 0;
2235         if (ret == 0) {
2236                 lpfc_printf_vlog(vport,
2237                                  KERN_INFO, LOG_NVME_DISC,
2238                                  "6009 Unregistered lport Success\n");
2239         } else {
2240                 lpfc_printf_vlog(vport,
2241                                  KERN_INFO, LOG_NVME_DISC,
2242                                  "6010 Unregistered lport "
2243                                  "Failed, status x%x\n",
2244                                  ret);
2245         }
2246 #endif
2247 }
2248
2249 void
2250 lpfc_nvme_update_localport(struct lpfc_vport *vport)
2251 {
2252 #if (IS_ENABLED(CONFIG_NVME_FC))
2253         struct nvme_fc_local_port *localport;
2254         struct lpfc_nvme_lport *lport;
2255
2256         localport = vport->localport;
2257         if (!localport) {
2258                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
2259                                  "6710 Update NVME fail. No localport\n");
2260                 return;
2261         }
2262         lport = (struct lpfc_nvme_lport *)localport->private;
2263         if (!lport) {
2264                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
2265                                  "6171 Update NVME fail. localP %p, No lport\n",
2266                                  localport);
2267                 return;
2268         }
2269         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2270                          "6012 Update NVME lport %p did x%x\n",
2271                          localport, vport->fc_myDID);
2272
2273         localport->port_id = vport->fc_myDID;
2274         if (localport->port_id == 0)
2275                 localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
2276         else
2277                 localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
2278
2279         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2280                          "6030 bound lport %p to DID x%06x\n",
2281                          lport, localport->port_id);
2282 #endif
2283 }
2284
2285 int
2286 lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2287 {
2288 #if (IS_ENABLED(CONFIG_NVME_FC))
2289         int ret = 0;
2290         struct nvme_fc_local_port *localport;
2291         struct lpfc_nvme_lport *lport;
2292         struct lpfc_nvme_rport *rport;
2293         struct lpfc_nvme_rport *oldrport;
2294         struct nvme_fc_remote_port *remote_port;
2295         struct nvme_fc_port_info rpinfo;
2296         struct lpfc_nodelist *prev_ndlp = NULL;
2297
2298         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
2299                          "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
2300                          ndlp->nlp_DID, ndlp->nlp_type);
2301
2302         localport = vport->localport;
2303         if (!localport)
2304                 return 0;
2305
2306         lport = (struct lpfc_nvme_lport *)localport->private;
2307
2308         /* NVME rports are not preserved across devloss.
2309          * Just register this instance.  Note, rpinfo->dev_loss_tmo
2310          * is left 0 to indicate accept transport defaults.  The
2311          * driver communicates port role capabilities consistent
2312          * with the PRLI response data.
2313          */
2314         memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
2315         rpinfo.port_id = ndlp->nlp_DID;
2316         if (ndlp->nlp_type & NLP_NVME_TARGET)
2317                 rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
2318         if (ndlp->nlp_type & NLP_NVME_INITIATOR)
2319                 rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
2320
2321         if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
2322                 rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
2323
2324         rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
2325         rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2326
2327         spin_lock_irq(&vport->phba->hbalock);
2328         oldrport = lpfc_ndlp_get_nrport(ndlp);
2329         spin_unlock_irq(&vport->phba->hbalock);
2330         if (!oldrport)
2331                 lpfc_nlp_get(ndlp);
2332
2333         ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
2334         if (!ret) {
2335                 /* If the ndlp already has an nrport, this is just
2336                  * a resume of the existing rport.  Else this is a
2337                  * new rport.
2338                  */
2339                 /* Guard against an unregister/reregister
2340                  * race that leaves the WAIT flag set.
2341                  */
2342                 spin_lock_irq(&vport->phba->hbalock);
2343                 ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
2344                 spin_unlock_irq(&vport->phba->hbalock);
2345                 rport = remote_port->private;
2346                 if (oldrport) {
2347                         /* New remoteport record does not guarantee valid
2348                          * host private memory area.
2349                          */
2350                         prev_ndlp = oldrport->ndlp;
2351                         if (oldrport == remote_port->private) {
2352                                 /* Same remoteport - ndlp should match.
2353                                  * Just reuse.
2354                                  */
2355                                 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
2356                                                  LOG_NVME_DISC,
2357                                                  "6014 Rebinding lport to "
2358                                                  "remoteport %p wwpn 0x%llx, "
2359                                                  "Data: x%x x%x %p %p x%x x%06x\n",
2360                                                  remote_port,
2361                                                  remote_port->port_name,
2362                                                  remote_port->port_id,
2363                                                  remote_port->port_role,
2364                                                  prev_ndlp,
2365                                                  ndlp,
2366                                                  ndlp->nlp_type,
2367                                                  ndlp->nlp_DID);
2368                                 return 0;
2369                         }
2370
2371                         /* Sever the ndlp<->rport association
2372                          * before dropping the ndlp ref from
2373                          * register.
2374                          */
2375                         spin_lock_irq(&vport->phba->hbalock);
2376                         ndlp->nrport = NULL;
2377                         ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
2378                         spin_unlock_irq(&vport->phba->hbalock);
2379                         rport->ndlp = NULL;
2380                         rport->remoteport = NULL;
2381
2382                         /* Reference only removed if previous NDLP is no longer
2383                          * active. It might be just a swap and removing the
2384                          * reference would cause a premature cleanup.
2385                          */
2386                         if (prev_ndlp && prev_ndlp != ndlp) {
2387                                 if ((!NLP_CHK_NODE_ACT(prev_ndlp)) ||
2388                                     (!prev_ndlp->nrport))
2389                                         lpfc_nlp_put(prev_ndlp);
2390                         }
2391                 }
2392
2393                 /* Clean bind the rport to the ndlp. */
2394                 rport->remoteport = remote_port;
2395                 rport->lport = lport;
2396                 rport->ndlp = ndlp;
2397                 spin_lock_irq(&vport->phba->hbalock);
2398                 ndlp->nrport = rport;
2399                 spin_unlock_irq(&vport->phba->hbalock);
2400                 lpfc_printf_vlog(vport, KERN_INFO,
2401                                  LOG_NVME_DISC | LOG_NODE,
2402                                  "6022 Binding new rport to "
2403                                  "lport %p Remoteport %p rport %p WWNN 0x%llx, "
2404                                  "Rport WWPN 0x%llx DID "
2405                                  "x%06x Role x%x, ndlp %p prev_ndlp %p\n",
2406                                  lport, remote_port, rport,
2407                                  rpinfo.node_name, rpinfo.port_name,
2408                                  rpinfo.port_id, rpinfo.port_role,
2409                                  ndlp, prev_ndlp);
2410         } else {
2411                 lpfc_printf_vlog(vport, KERN_ERR,
2412                                  LOG_NVME_DISC | LOG_NODE,
2413                                  "6031 RemotePort Registration failed "
2414                                  "err: %d, DID x%06x\n",
2415                                  ret, ndlp->nlp_DID);
2416         }
2417
2418         return ret;
2419 #else
2420         return 0;
2421 #endif
2422 }
2423
2424 /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
2425  *
2426  * There is no notion of Devloss or rport recovery from the current
2427  * nvme_transport perspective.  Loss of an rport just means IO cannot
2428  * be sent and recovery is completely up to the initator.
2429  * For now, the driver just unbinds the DID and port_role so that
2430  * no further IO can be issued.  Changes are planned for later.
2431  *
2432  * Notes - the ndlp reference count is not decremented here since
2433  * since there is no nvme_transport api for devloss.  Node ref count
2434  * is only adjusted in driver unload.
2435  */
2436 void
2437 lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2438 {
2439 #if (IS_ENABLED(CONFIG_NVME_FC))
2440         int ret;
2441         struct nvme_fc_local_port *localport;
2442         struct lpfc_nvme_lport *lport;
2443         struct lpfc_nvme_rport *rport;
2444         struct nvme_fc_remote_port *remoteport = NULL;
2445
2446         localport = vport->localport;
2447
2448         /* This is fundamental error.  The localport is always
2449          * available until driver unload.  Just exit.
2450          */
2451         if (!localport)
2452                 return;
2453
2454         lport = (struct lpfc_nvme_lport *)localport->private;
2455         if (!lport)
2456                 goto input_err;
2457
2458         spin_lock_irq(&vport->phba->hbalock);
2459         rport = lpfc_ndlp_get_nrport(ndlp);
2460         if (rport)
2461                 remoteport = rport->remoteport;
2462         spin_unlock_irq(&vport->phba->hbalock);
2463         if (!remoteport)
2464                 goto input_err;
2465
2466         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2467                          "6033 Unreg nvme remoteport %p, portname x%llx, "
2468                          "port_id x%06x, portstate x%x port type x%x\n",
2469                          remoteport, remoteport->port_name,
2470                          remoteport->port_id, remoteport->port_state,
2471                          ndlp->nlp_type);
2472
2473         /* Sanity check ndlp type.  Only call for NVME ports. Don't
2474          * clear any rport state until the transport calls back.
2475          */
2476
2477         if (ndlp->nlp_type & NLP_NVME_TARGET) {
2478                 /* No concern about the role change on the nvme remoteport.
2479                  * The transport will update it.
2480                  */
2481                 ndlp->upcall_flags |= NLP_WAIT_FOR_UNREG;
2482
2483                 /* Don't let the host nvme transport keep sending keep-alives
2484                  * on this remoteport. Vport is unloading, no recovery. The
2485                  * return values is ignored.  The upcall is a courtesy to the
2486                  * transport.
2487                  */
2488                 if (vport->load_flag & FC_UNLOADING)
2489                         (void)nvme_fc_set_remoteport_devloss(remoteport, 0);
2490
2491                 ret = nvme_fc_unregister_remoteport(remoteport);
2492                 if (ret != 0) {
2493                         lpfc_nlp_put(ndlp);
2494                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
2495                                          "6167 NVME unregister failed %d "
2496                                          "port_state x%x\n",
2497                                          ret, remoteport->port_state);
2498                 }
2499         }
2500         return;
2501
2502  input_err:
2503 #endif
2504         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
2505                          "6168 State error: lport %p, rport%p FCID x%06x\n",
2506                          vport->localport, ndlp->rport, ndlp->nlp_DID);
2507 }
2508
2509 /**
2510  * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2511  * @phba: pointer to lpfc hba data structure.
2512  * @axri: pointer to the fcp xri abort wcqe structure.
2513  *
2514  * This routine is invoked by the worker thread to process a SLI4 fast-path
2515  * NVME aborted xri.  Aborted NVME IO commands are completed to the transport
2516  * here.
2517  **/
2518 void
2519 lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2520                            struct sli4_wcqe_xri_aborted *axri)
2521 {
2522         uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
2523         struct lpfc_nvme_buf *lpfc_ncmd, *next_lpfc_ncmd;
2524         struct nvmefc_fcp_req *nvme_cmd = NULL;
2525         struct lpfc_nodelist *ndlp;
2526         unsigned long iflag = 0;
2527
2528         if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
2529                 return;
2530         spin_lock_irqsave(&phba->hbalock, iflag);
2531         spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2532         list_for_each_entry_safe(lpfc_ncmd, next_lpfc_ncmd,
2533                                  &phba->sli4_hba.lpfc_abts_nvme_buf_list,
2534                                  list) {
2535                 if (lpfc_ncmd->cur_iocbq.sli4_xritag == xri) {
2536                         list_del_init(&lpfc_ncmd->list);
2537                         lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
2538                         lpfc_ncmd->status = IOSTAT_SUCCESS;
2539                         spin_unlock(
2540                                 &phba->sli4_hba.abts_nvme_buf_list_lock);
2541
2542                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2543                         ndlp = lpfc_ncmd->ndlp;
2544                         if (ndlp)
2545                                 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
2546
2547                         lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2548                                         "6311 nvme_cmd %p xri x%x tag x%x "
2549                                         "abort complete and xri released\n",
2550                                         lpfc_ncmd->nvmeCmd, xri,
2551                                         lpfc_ncmd->cur_iocbq.iotag);
2552
2553                         /* Aborted NVME commands are required to not complete
2554                          * before the abort exchange command fully completes.
2555                          * Once completed, it is available via the put list.
2556                          */
2557                         if (lpfc_ncmd->nvmeCmd) {
2558                                 nvme_cmd = lpfc_ncmd->nvmeCmd;
2559                                 nvme_cmd->done(nvme_cmd);
2560                                 lpfc_ncmd->nvmeCmd = NULL;
2561                         }
2562                         lpfc_release_nvme_buf(phba, lpfc_ncmd);
2563                         return;
2564                 }
2565         }
2566         spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2567         spin_unlock_irqrestore(&phba->hbalock, iflag);
2568
2569         lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2570                         "6312 XRI Aborted xri x%x not found\n", xri);
2571
2572 }
2573
2574 /**
2575  * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2576  * @phba: Pointer to HBA context object.
2577  *
2578  * This function flushes all wqes in the nvme rings and frees all resources
2579  * in the txcmplq. This function does not issue abort wqes for the IO
2580  * commands in txcmplq, they will just be returned with
2581  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2582  * slot has been permanently disabled.
2583  **/
2584 void
2585 lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2586 {
2587         struct lpfc_sli_ring  *pring;
2588         u32 i, wait_cnt = 0;
2589
2590         if (phba->sli_rev < LPFC_SLI_REV4 || !phba->sli4_hba.hdwq)
2591                 return;
2592
2593         /* Cycle through all NVME rings and make sure all outstanding
2594          * WQEs have been removed from the txcmplqs.
2595          */
2596         for (i = 0; i < phba->cfg_hdw_queue; i++) {
2597                 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
2598
2599                 if (!pring)
2600                         continue;
2601
2602                 /* Retrieve everything on the txcmplq */
2603                 while (!list_empty(&pring->txcmplq)) {
2604                         msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
2605                         wait_cnt++;
2606
2607                         /* The sleep is 10mS.  Every ten seconds,
2608                          * dump a message.  Something is wrong.
2609                          */
2610                         if ((wait_cnt % 1000) == 0) {
2611                                 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2612                                                 "6178 NVME IO not empty, "
2613                                                 "cnt %d\n", wait_cnt);
2614                         }
2615                 }
2616         }
2617 }