GNU Linux-libre 4.19.245-gnu1
[releases.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_nvme_io_channel);
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                          "io_channel %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         if (lport->vport->localport)
301                 complete(lport->lport_unreg_cmp);
302 }
303
304 /* lpfc_nvme_remoteport_delete
305  *
306  * @remoteport: Pointer to an nvme transport remoteport instance.
307  *
308  * This is a template downcall.  NVME transport calls this function
309  * when it has completed the unregistration of a previously
310  * registered remoteport.
311  *
312  * Return value :
313  * None
314  */
315 void
316 lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
317 {
318         struct lpfc_nvme_rport *rport = remoteport->private;
319         struct lpfc_vport *vport;
320         struct lpfc_nodelist *ndlp;
321
322         ndlp = rport->ndlp;
323         if (!ndlp)
324                 goto rport_err;
325
326         vport = ndlp->vport;
327         if (!vport)
328                 goto rport_err;
329
330         /* Remove this rport from the lport's list - memory is owned by the
331          * transport. Remove the ndlp reference for the NVME transport before
332          * calling state machine to remove the node.
333          */
334         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
335                         "6146 remoteport delete of remoteport %p\n",
336                         remoteport);
337         spin_lock_irq(&vport->phba->hbalock);
338
339         /* The register rebind might have occurred before the delete
340          * downcall.  Guard against this race.
341          */
342         if (ndlp->upcall_flags & NLP_WAIT_FOR_UNREG) {
343                 ndlp->nrport = NULL;
344                 ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
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         } else {
352                 spin_unlock_irq(&vport->phba->hbalock);
353         }
354
355  rport_err:
356         return;
357 }
358
359 static void
360 lpfc_nvme_cmpl_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
361                        struct lpfc_wcqe_complete *wcqe)
362 {
363         struct lpfc_vport *vport = cmdwqe->vport;
364         struct lpfc_nvme_lport *lport;
365         uint32_t status;
366         struct nvmefc_ls_req *pnvme_lsreq;
367         struct lpfc_dmabuf *buf_ptr;
368         struct lpfc_nodelist *ndlp;
369
370         pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
371         status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
372
373         if (vport->localport) {
374                 lport = (struct lpfc_nvme_lport *)vport->localport->private;
375                 if (lport) {
376                         atomic_inc(&lport->fc4NvmeLsCmpls);
377                         if (status) {
378                                 if (bf_get(lpfc_wcqe_c_xb, wcqe))
379                                         atomic_inc(&lport->cmpl_ls_xb);
380                                 atomic_inc(&lport->cmpl_ls_err);
381                         }
382                 }
383         }
384
385         ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
386         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
387                          "6047 nvme cmpl Enter "
388                          "Data %p DID %x Xri: %x status %x reason x%x cmd:%p "
389                          "lsreg:%p bmp:%p ndlp:%p\n",
390                          pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
391                          cmdwqe->sli4_xritag, status,
392                          (wcqe->parameter & 0xffff),
393                          cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
394
395         lpfc_nvmeio_data(phba, "NVME LS  CMPL: xri x%x stat x%x parm x%x\n",
396                          cmdwqe->sli4_xritag, status, wcqe->parameter);
397
398         if (cmdwqe->context3) {
399                 buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
400                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
401                 kfree(buf_ptr);
402                 cmdwqe->context3 = NULL;
403         }
404         if (pnvme_lsreq->done)
405                 pnvme_lsreq->done(pnvme_lsreq, status);
406         else
407                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
408                                  "6046 nvme cmpl without done call back? "
409                                  "Data %p DID %x Xri: %x status %x\n",
410                                 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
411                                 cmdwqe->sli4_xritag, status);
412         if (ndlp) {
413                 lpfc_nlp_put(ndlp);
414                 cmdwqe->context1 = NULL;
415         }
416         lpfc_sli_release_iocbq(phba, cmdwqe);
417 }
418
419 static int
420 lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
421                   struct lpfc_dmabuf *inp,
422                   struct nvmefc_ls_req *pnvme_lsreq,
423                   void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
424                                struct lpfc_wcqe_complete *),
425                   struct lpfc_nodelist *ndlp, uint32_t num_entry,
426                   uint32_t tmo, uint8_t retry)
427 {
428         struct lpfc_hba *phba = vport->phba;
429         union lpfc_wqe128 *wqe;
430         struct lpfc_iocbq *genwqe;
431         struct ulp_bde64 *bpl;
432         struct ulp_bde64 bde;
433         int i, rc, xmit_len, first_len;
434
435         /* Allocate buffer for  command WQE */
436         genwqe = lpfc_sli_get_iocbq(phba);
437         if (genwqe == NULL)
438                 return 1;
439
440         wqe = &genwqe->wqe;
441         memset(wqe, 0, sizeof(union lpfc_wqe));
442
443         genwqe->context3 = (uint8_t *)bmp;
444         genwqe->iocb_flag |= LPFC_IO_NVME_LS;
445
446         /* Save for completion so we can release these resources */
447         genwqe->context1 = lpfc_nlp_get(ndlp);
448         genwqe->context2 = (uint8_t *)pnvme_lsreq;
449         /* Fill in payload, bp points to frame payload */
450
451         if (!tmo)
452                 /* FC spec states we need 3 * ratov for CT requests */
453                 tmo = (3 * phba->fc_ratov);
454
455         /* For this command calculate the xmit length of the request bde. */
456         xmit_len = 0;
457         first_len = 0;
458         bpl = (struct ulp_bde64 *)bmp->virt;
459         for (i = 0; i < num_entry; i++) {
460                 bde.tus.w = bpl[i].tus.w;
461                 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
462                         break;
463                 xmit_len += bde.tus.f.bdeSize;
464                 if (i == 0)
465                         first_len = xmit_len;
466         }
467
468         genwqe->rsvd2 = num_entry;
469         genwqe->hba_wqidx = 0;
470
471         /* Words 0 - 2 */
472         wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
473         wqe->generic.bde.tus.f.bdeSize = first_len;
474         wqe->generic.bde.addrLow = bpl[0].addrLow;
475         wqe->generic.bde.addrHigh = bpl[0].addrHigh;
476
477         /* Word 3 */
478         wqe->gen_req.request_payload_len = first_len;
479
480         /* Word 4 */
481
482         /* Word 5 */
483         bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
484         bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
485         bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
486         bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
487         bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
488
489         /* Word 6 */
490         bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
491                phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
492         bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
493
494         /* Word 7 */
495         bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
496         bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
497         bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
498         bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
499
500         /* Word 8 */
501         wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
502
503         /* Word 9 */
504         bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
505
506         /* Word 10 */
507         bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
508         bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
509         bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
510         bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
511         bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
512
513         /* Word 11 */
514         bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
515         bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
516
517
518         /* Issue GEN REQ WQE for NPORT <did> */
519         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
520                          "6050 Issue GEN REQ WQE to NPORT x%x "
521                          "Data: x%x x%x wq:%p lsreq:%p bmp:%p xmit:%d 1st:%d\n",
522                          ndlp->nlp_DID, genwqe->iotag,
523                          vport->port_state,
524                         genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
525         genwqe->wqe_cmpl = cmpl;
526         genwqe->iocb_cmpl = NULL;
527         genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
528         genwqe->vport = vport;
529         genwqe->retry = retry;
530
531         lpfc_nvmeio_data(phba, "NVME LS  XMIT: xri x%x iotag x%x to x%06x\n",
532                          genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
533
534         rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, genwqe);
535         if (rc) {
536                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
537                                  "6045 Issue GEN REQ WQE to NPORT x%x "
538                                  "Data: x%x x%x\n",
539                                  ndlp->nlp_DID, genwqe->iotag,
540                                  vport->port_state);
541                 lpfc_sli_release_iocbq(phba, genwqe);
542                 return 1;
543         }
544         return 0;
545 }
546
547 /**
548  * lpfc_nvme_ls_req - Issue an Link Service request
549  * @lpfc_pnvme: Pointer to the driver's nvme instance data
550  * @lpfc_nvme_lport: Pointer to the driver's local port data
551  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
552  *
553  * Driver registers this routine to handle any link service request
554  * from the nvme_fc transport to a remote nvme-aware port.
555  *
556  * Return value :
557  *   0 - Success
558  *   TODO: What are the failure codes.
559  **/
560 static int
561 lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
562                  struct nvme_fc_remote_port *pnvme_rport,
563                  struct nvmefc_ls_req *pnvme_lsreq)
564 {
565         int ret = 0;
566         struct lpfc_nvme_lport *lport;
567         struct lpfc_nvme_rport *rport;
568         struct lpfc_vport *vport;
569         struct lpfc_nodelist *ndlp;
570         struct ulp_bde64 *bpl;
571         struct lpfc_dmabuf *bmp;
572         uint16_t ntype, nstate;
573
574         /* there are two dma buf in the request, actually there is one and
575          * the second one is just the start address + cmd size.
576          * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
577          * in a lpfc_dmabuf struct. When freeing we just free the wrapper
578          * because the nvem layer owns the data bufs.
579          * We do not have to break these packets open, we don't care what is in
580          * them. And we do not have to look at the resonse data, we only care
581          * that we got a response. All of the caring is going to happen in the
582          * nvme-fc layer.
583          */
584
585         lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
586         rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
587         if (unlikely(!lport) || unlikely(!rport))
588                 return -EINVAL;
589
590         vport = lport->vport;
591
592         if (vport->load_flag & FC_UNLOADING)
593                 return -ENODEV;
594
595         /* Need the ndlp.  It is stored in the driver's rport. */
596         ndlp = rport->ndlp;
597         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
598                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
599                                  "6051 Remoteport %p, rport has invalid ndlp. "
600                                  "Failing LS Req\n", pnvme_rport);
601                 return -ENODEV;
602         }
603
604         /* The remote node has to be a mapped nvme target or an
605          * unmapped nvme initiator or it's an error.
606          */
607         ntype = ndlp->nlp_type;
608         nstate = ndlp->nlp_state;
609         if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
610             (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
611                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
612                                  "6088 DID x%06x not ready for "
613                                  "IO. State x%x, Type x%x\n",
614                                  pnvme_rport->port_id,
615                                  ndlp->nlp_state, ndlp->nlp_type);
616                 return -ENODEV;
617         }
618         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
619         if (!bmp) {
620
621                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
622                                  "6044 Could not find node for DID %x\n",
623                                  pnvme_rport->port_id);
624                 return 2;
625         }
626         INIT_LIST_HEAD(&bmp->list);
627         bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
628         if (!bmp->virt) {
629                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
630                                  "6042 Could not find node for DID %x\n",
631                                  pnvme_rport->port_id);
632                 kfree(bmp);
633                 return 3;
634         }
635         bpl = (struct ulp_bde64 *)bmp->virt;
636         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
637         bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
638         bpl->tus.f.bdeFlags = 0;
639         bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
640         bpl->tus.w = le32_to_cpu(bpl->tus.w);
641         bpl++;
642
643         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
644         bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
645         bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
646         bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
647         bpl->tus.w = le32_to_cpu(bpl->tus.w);
648
649         /* Expand print to include key fields. */
650         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
651                          "6149 Issue LS Req to DID 0x%06x lport %p, rport %p "
652                          "lsreq%p rqstlen:%d rsplen:%d %pad %pad\n",
653                          ndlp->nlp_DID,
654                          pnvme_lport, pnvme_rport,
655                          pnvme_lsreq, pnvme_lsreq->rqstlen,
656                          pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
657                          &pnvme_lsreq->rspdma);
658
659         atomic_inc(&lport->fc4NvmeLsRequests);
660
661         /* Hardcode the wait to 30 seconds.  Connections are failing otherwise.
662          * This code allows it all to work.
663          */
664         ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
665                                 pnvme_lsreq, lpfc_nvme_cmpl_gen_req,
666                                 ndlp, 2, 30, 0);
667         if (ret != WQE_SUCCESS) {
668                 atomic_inc(&lport->xmt_ls_err);
669                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
670                                  "6052 EXIT. issue ls wqe failed lport %p, "
671                                  "rport %p lsreq%p Status %x DID %x\n",
672                                  pnvme_lport, pnvme_rport, pnvme_lsreq,
673                                  ret, ndlp->nlp_DID);
674                 lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
675                 kfree(bmp);
676                 return ret;
677         }
678
679         /* Stub in routine and return 0 for now. */
680         return ret;
681 }
682
683 /**
684  * lpfc_nvme_ls_abort - Issue an Link Service request
685  * @lpfc_pnvme: Pointer to the driver's nvme instance data
686  * @lpfc_nvme_lport: Pointer to the driver's local port data
687  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
688  *
689  * Driver registers this routine to handle any link service request
690  * from the nvme_fc transport to a remote nvme-aware port.
691  *
692  * Return value :
693  *   0 - Success
694  *   TODO: What are the failure codes.
695  **/
696 static void
697 lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
698                    struct nvme_fc_remote_port *pnvme_rport,
699                    struct nvmefc_ls_req *pnvme_lsreq)
700 {
701         struct lpfc_nvme_lport *lport;
702         struct lpfc_vport *vport;
703         struct lpfc_hba *phba;
704         struct lpfc_nodelist *ndlp;
705         LIST_HEAD(abort_list);
706         struct lpfc_sli_ring *pring;
707         struct lpfc_iocbq *wqe, *next_wqe;
708
709         lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
710         if (unlikely(!lport))
711                 return;
712         vport = lport->vport;
713         phba = vport->phba;
714
715         if (vport->load_flag & FC_UNLOADING)
716                 return;
717
718         ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
719         if (!ndlp) {
720                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
721                                  "6049 Could not find node for DID %x\n",
722                                  pnvme_rport->port_id);
723                 return;
724         }
725
726         /* Expand print to include key fields. */
727         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
728                          "6040 ENTER.  lport %p, rport %p lsreq %p rqstlen:%d "
729                          "rsplen:%d %pad %pad\n",
730                          pnvme_lport, pnvme_rport,
731                          pnvme_lsreq, pnvme_lsreq->rqstlen,
732                          pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
733                          &pnvme_lsreq->rspdma);
734
735         /*
736          * Lock the ELS ring txcmplq and build a local list of all ELS IOs
737          * that need an ABTS.  The IOs need to stay on the txcmplq so that
738          * the abort operation completes them successfully.
739          */
740         pring = phba->sli4_hba.nvmels_wq->pring;
741         spin_lock_irq(&phba->hbalock);
742         spin_lock(&pring->ring_lock);
743         list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
744                 /* Add to abort_list on on NDLP match. */
745                 if (lpfc_check_sli_ndlp(phba, pring, wqe, ndlp)) {
746                         wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
747                         list_add_tail(&wqe->dlist, &abort_list);
748                 }
749         }
750         spin_unlock(&pring->ring_lock);
751         spin_unlock_irq(&phba->hbalock);
752
753         /* Abort the targeted IOs and remove them from the abort list. */
754         list_for_each_entry_safe(wqe, next_wqe, &abort_list, dlist) {
755                 atomic_inc(&lport->xmt_ls_abort);
756                 spin_lock_irq(&phba->hbalock);
757                 list_del_init(&wqe->dlist);
758                 lpfc_sli_issue_abort_iotag(phba, pring, wqe);
759                 spin_unlock_irq(&phba->hbalock);
760         }
761 }
762
763 /* Fix up the existing sgls for NVME IO. */
764 static inline void
765 lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
766                        struct lpfc_nvme_buf *lpfc_ncmd,
767                        struct nvmefc_fcp_req *nCmd)
768 {
769         struct lpfc_hba  *phba = vport->phba;
770         struct sli4_sge *sgl;
771         union lpfc_wqe128 *wqe;
772         uint32_t *wptr, *dptr;
773
774         /*
775          * Get a local pointer to the built-in wqe and correct
776          * the cmd size to match NVME's 96 bytes and fix
777          * the dma address.
778          */
779
780         wqe = &lpfc_ncmd->cur_iocbq.wqe;
781
782         /*
783          * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
784          * match NVME.  NVME sends 96 bytes. Also, use the
785          * nvme commands command and response dma addresses
786          * rather than the virtual memory to ease the restore
787          * operation.
788          */
789         sgl = lpfc_ncmd->nvme_sgl;
790         sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
791         if (phba->cfg_nvme_embed_cmd) {
792                 sgl->addr_hi = 0;
793                 sgl->addr_lo = 0;
794
795                 /* Word 0-2 - NVME CMND IU (embedded payload) */
796                 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
797                 wqe->generic.bde.tus.f.bdeSize = 56;
798                 wqe->generic.bde.addrHigh = 0;
799                 wqe->generic.bde.addrLow =  64;  /* Word 16 */
800
801                 /* Word 10  - dbde is 0, wqes is 1 in template */
802
803                 /*
804                  * Embed the payload in the last half of the WQE
805                  * WQE words 16-30 get the NVME CMD IU payload
806                  *
807                  * WQE words 16-19 get payload Words 1-4
808                  * WQE words 20-21 get payload Words 6-7
809                  * WQE words 22-29 get payload Words 16-23
810                  */
811                 wptr = &wqe->words[16];  /* WQE ptr */
812                 dptr = (uint32_t *)nCmd->cmdaddr;  /* payload ptr */
813                 dptr++;                 /* Skip Word 0 in payload */
814
815                 *wptr++ = *dptr++;      /* Word 1 */
816                 *wptr++ = *dptr++;      /* Word 2 */
817                 *wptr++ = *dptr++;      /* Word 3 */
818                 *wptr++ = *dptr++;      /* Word 4 */
819                 dptr++;                 /* Skip Word 5 in payload */
820                 *wptr++ = *dptr++;      /* Word 6 */
821                 *wptr++ = *dptr++;      /* Word 7 */
822                 dptr += 8;              /* Skip Words 8-15 in payload */
823                 *wptr++ = *dptr++;      /* Word 16 */
824                 *wptr++ = *dptr++;      /* Word 17 */
825                 *wptr++ = *dptr++;      /* Word 18 */
826                 *wptr++ = *dptr++;      /* Word 19 */
827                 *wptr++ = *dptr++;      /* Word 20 */
828                 *wptr++ = *dptr++;      /* Word 21 */
829                 *wptr++ = *dptr++;      /* Word 22 */
830                 *wptr   = *dptr;        /* Word 23 */
831         } else {
832                 sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->cmddma));
833                 sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->cmddma));
834
835                 /* Word 0-2 - NVME CMND IU Inline BDE */
836                 wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
837                 wqe->generic.bde.tus.f.bdeSize = nCmd->cmdlen;
838                 wqe->generic.bde.addrHigh = sgl->addr_hi;
839                 wqe->generic.bde.addrLow =  sgl->addr_lo;
840
841                 /* Word 10 */
842                 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
843                 bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
844         }
845
846         sgl++;
847
848         /* Setup the physical region for the FCP RSP */
849         sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
850         sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
851         sgl->word2 = le32_to_cpu(sgl->word2);
852         if (nCmd->sg_cnt)
853                 bf_set(lpfc_sli4_sge_last, sgl, 0);
854         else
855                 bf_set(lpfc_sli4_sge_last, sgl, 1);
856         sgl->word2 = cpu_to_le32(sgl->word2);
857         sgl->sge_len = cpu_to_le32(nCmd->rsplen);
858 }
859
860 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
861 static void
862 lpfc_nvme_ktime(struct lpfc_hba *phba,
863                 struct lpfc_nvme_buf *lpfc_ncmd)
864 {
865         uint64_t seg1, seg2, seg3, seg4;
866         uint64_t segsum;
867
868         if (!lpfc_ncmd->ts_last_cmd ||
869             !lpfc_ncmd->ts_cmd_start ||
870             !lpfc_ncmd->ts_cmd_wqput ||
871             !lpfc_ncmd->ts_isr_cmpl ||
872             !lpfc_ncmd->ts_data_nvme)
873                 return;
874
875         if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_cmd_start)
876                 return;
877         if (lpfc_ncmd->ts_cmd_start < lpfc_ncmd->ts_last_cmd)
878                 return;
879         if (lpfc_ncmd->ts_cmd_wqput < lpfc_ncmd->ts_cmd_start)
880                 return;
881         if (lpfc_ncmd->ts_isr_cmpl < lpfc_ncmd->ts_cmd_wqput)
882                 return;
883         if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_isr_cmpl)
884                 return;
885         /*
886          * Segment 1 - Time from Last FCP command cmpl is handed
887          * off to NVME Layer to start of next command.
888          * Segment 2 - Time from Driver receives a IO cmd start
889          * from NVME Layer to WQ put is done on IO cmd.
890          * Segment 3 - Time from Driver WQ put is done on IO cmd
891          * to MSI-X ISR for IO cmpl.
892          * Segment 4 - Time from MSI-X ISR for IO cmpl to when
893          * cmpl is handled off to the NVME Layer.
894          */
895         seg1 = lpfc_ncmd->ts_cmd_start - lpfc_ncmd->ts_last_cmd;
896         if (seg1 > 5000000)  /* 5 ms - for sequential IOs only */
897                 seg1 = 0;
898
899         /* Calculate times relative to start of IO */
900         seg2 = (lpfc_ncmd->ts_cmd_wqput - lpfc_ncmd->ts_cmd_start);
901         segsum = seg2;
902         seg3 = lpfc_ncmd->ts_isr_cmpl - lpfc_ncmd->ts_cmd_start;
903         if (segsum > seg3)
904                 return;
905         seg3 -= segsum;
906         segsum += seg3;
907
908         seg4 = lpfc_ncmd->ts_data_nvme - lpfc_ncmd->ts_cmd_start;
909         if (segsum > seg4)
910                 return;
911         seg4 -= segsum;
912
913         phba->ktime_data_samples++;
914         phba->ktime_seg1_total += seg1;
915         if (seg1 < phba->ktime_seg1_min)
916                 phba->ktime_seg1_min = seg1;
917         else if (seg1 > phba->ktime_seg1_max)
918                 phba->ktime_seg1_max = seg1;
919         phba->ktime_seg2_total += seg2;
920         if (seg2 < phba->ktime_seg2_min)
921                 phba->ktime_seg2_min = seg2;
922         else if (seg2 > phba->ktime_seg2_max)
923                 phba->ktime_seg2_max = seg2;
924         phba->ktime_seg3_total += seg3;
925         if (seg3 < phba->ktime_seg3_min)
926                 phba->ktime_seg3_min = seg3;
927         else if (seg3 > phba->ktime_seg3_max)
928                 phba->ktime_seg3_max = seg3;
929         phba->ktime_seg4_total += seg4;
930         if (seg4 < phba->ktime_seg4_min)
931                 phba->ktime_seg4_min = seg4;
932         else if (seg4 > phba->ktime_seg4_max)
933                 phba->ktime_seg4_max = seg4;
934
935         lpfc_ncmd->ts_last_cmd = 0;
936         lpfc_ncmd->ts_cmd_start = 0;
937         lpfc_ncmd->ts_cmd_wqput  = 0;
938         lpfc_ncmd->ts_isr_cmpl = 0;
939         lpfc_ncmd->ts_data_nvme = 0;
940 }
941 #endif
942
943 /**
944  * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
945  * @lpfc_pnvme: Pointer to the driver's nvme instance data
946  * @lpfc_nvme_lport: Pointer to the driver's local port data
947  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
948  *
949  * Driver registers this routine as it io request handler.  This
950  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
951  * data structure to the rport indicated in @lpfc_nvme_rport.
952  *
953  * Return value :
954  *   0 - Success
955  *   TODO: What are the failure codes.
956  **/
957 static void
958 lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
959                           struct lpfc_wcqe_complete *wcqe)
960 {
961         struct lpfc_nvme_buf *lpfc_ncmd =
962                 (struct lpfc_nvme_buf *)pwqeIn->context1;
963         struct lpfc_vport *vport = pwqeIn->vport;
964         struct nvmefc_fcp_req *nCmd;
965         struct nvme_fc_ersp_iu *ep;
966         struct nvme_fc_cmd_iu *cp;
967         struct lpfc_nvme_rport *rport;
968         struct lpfc_nodelist *ndlp;
969         struct lpfc_nvme_fcpreq_priv *freqpriv;
970         struct lpfc_nvme_lport *lport;
971         struct lpfc_nvme_ctrl_stat *cstat;
972         unsigned long flags;
973         uint32_t code, status, idx;
974         uint16_t cid, sqhd, data;
975         uint32_t *ptr;
976
977         /* Sanity check on return of outstanding command */
978         if (!lpfc_ncmd || !lpfc_ncmd->nvmeCmd || !lpfc_ncmd->nrport) {
979                 if (!lpfc_ncmd) {
980                         lpfc_printf_vlog(vport, KERN_ERR,
981                                          LOG_NODE | LOG_NVME_IOERR,
982                                          "6071 Null lpfc_ncmd pointer. No "
983                                          "release, skip completion\n");
984                         return;
985                 }
986
987                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
988                                  "6066 Missing cmpl ptrs: lpfc_ncmd %p, "
989                                  "nvmeCmd %p nrport %p\n",
990                                  lpfc_ncmd, lpfc_ncmd->nvmeCmd,
991                                  lpfc_ncmd->nrport);
992
993                 /* Release the lpfc_ncmd regardless of the missing elements. */
994                 lpfc_release_nvme_buf(phba, lpfc_ncmd);
995                 return;
996         }
997         nCmd = lpfc_ncmd->nvmeCmd;
998         rport = lpfc_ncmd->nrport;
999         status = bf_get(lpfc_wcqe_c_status, wcqe);
1000
1001         if (vport->localport) {
1002                 lport = (struct lpfc_nvme_lport *)vport->localport->private;
1003                 if (lport) {
1004                         idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
1005                         cstat = &lport->cstat[idx];
1006                         atomic_inc(&cstat->fc4NvmeIoCmpls);
1007                         if (status) {
1008                                 if (bf_get(lpfc_wcqe_c_xb, wcqe))
1009                                         atomic_inc(&lport->cmpl_fcp_xb);
1010                                 atomic_inc(&lport->cmpl_fcp_err);
1011                         }
1012                 }
1013         }
1014
1015         lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
1016                          lpfc_ncmd->cur_iocbq.sli4_xritag,
1017                          status, wcqe->parameter);
1018         /*
1019          * Catch race where our node has transitioned, but the
1020          * transport is still transitioning.
1021          */
1022         ndlp = rport->ndlp;
1023         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1024                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
1025                                  "6061 rport %p,  DID x%06x node not ready.\n",
1026                                  rport, rport->remoteport->port_id);
1027
1028                 ndlp = lpfc_findnode_did(vport, rport->remoteport->port_id);
1029                 if (!ndlp) {
1030                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1031                                          "6062 Ignoring NVME cmpl.  No ndlp\n");
1032                         goto out_err;
1033                 }
1034         }
1035
1036         code = bf_get(lpfc_wcqe_c_code, wcqe);
1037         if (code == CQE_CODE_NVME_ERSP) {
1038                 /* For this type of CQE, we need to rebuild the rsp */
1039                 ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
1040
1041                 /*
1042                  * Get Command Id from cmd to plug into response. This
1043                  * code is not needed in the next NVME Transport drop.
1044                  */
1045                 cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
1046                 cid = cp->sqe.common.command_id;
1047
1048                 /*
1049                  * RSN is in CQE word 2
1050                  * SQHD is in CQE Word 3 bits 15:0
1051                  * Cmd Specific info is in CQE Word 1
1052                  * and in CQE Word 0 bits 15:0
1053                  */
1054                 sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
1055
1056                 /* Now lets build the NVME ERSP IU */
1057                 ep->iu_len = cpu_to_be16(8);
1058                 ep->rsn = wcqe->parameter;
1059                 ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
1060                 ep->rsvd12 = 0;
1061                 ptr = (uint32_t *)&ep->cqe.result.u64;
1062                 *ptr++ = wcqe->total_data_placed;
1063                 data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
1064                 *ptr = (uint32_t)data;
1065                 ep->cqe.sq_head = sqhd;
1066                 ep->cqe.sq_id =  nCmd->sqid;
1067                 ep->cqe.command_id = cid;
1068                 ep->cqe.status = 0;
1069
1070                 lpfc_ncmd->status = IOSTAT_SUCCESS;
1071                 lpfc_ncmd->result = 0;
1072                 nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
1073                 nCmd->transferred_length = nCmd->payload_length;
1074         } else {
1075                 lpfc_ncmd->status = (status & LPFC_IOCB_STATUS_MASK);
1076                 lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
1077
1078                 /* For NVME, the only failure path that results in an
1079                  * IO error is when the adapter rejects it.  All other
1080                  * conditions are a success case and resolved by the
1081                  * transport.
1082                  * IOSTAT_FCP_RSP_ERROR means:
1083                  * 1. Length of data received doesn't match total
1084                  *    transfer length in WQE
1085                  * 2. If the RSP payload does NOT match these cases:
1086                  *    a. RSP length 12/24 bytes and all zeros
1087                  *    b. NVME ERSP
1088                  */
1089                 switch (lpfc_ncmd->status) {
1090                 case IOSTAT_SUCCESS:
1091                         nCmd->transferred_length = wcqe->total_data_placed;
1092                         nCmd->rcv_rsplen = 0;
1093                         nCmd->status = 0;
1094                         break;
1095                 case IOSTAT_FCP_RSP_ERROR:
1096                         nCmd->transferred_length = wcqe->total_data_placed;
1097                         nCmd->rcv_rsplen = wcqe->parameter;
1098                         nCmd->status = 0;
1099                         /* Sanity check */
1100                         if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
1101                                 break;
1102                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1103                                          "6081 NVME Completion Protocol Error: "
1104                                          "xri %x status x%x result x%x "
1105                                          "placed x%x\n",
1106                                          lpfc_ncmd->cur_iocbq.sli4_xritag,
1107                                          lpfc_ncmd->status, lpfc_ncmd->result,
1108                                          wcqe->total_data_placed);
1109                         break;
1110                 case IOSTAT_LOCAL_REJECT:
1111                         /* Let fall through to set command final state. */
1112                         if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
1113                                 lpfc_printf_vlog(vport, KERN_INFO,
1114                                          LOG_NVME_IOERR,
1115                                          "6032 Delay Aborted cmd %p "
1116                                          "nvme cmd %p, xri x%x, "
1117                                          "xb %d\n",
1118                                          lpfc_ncmd, nCmd,
1119                                          lpfc_ncmd->cur_iocbq.sli4_xritag,
1120                                          bf_get(lpfc_wcqe_c_xb, wcqe));
1121                 default:
1122 out_err:
1123                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1124                                          "6072 NVME Completion Error: xri %x "
1125                                          "status x%x result x%x placed x%x\n",
1126                                          lpfc_ncmd->cur_iocbq.sli4_xritag,
1127                                          lpfc_ncmd->status, lpfc_ncmd->result,
1128                                          wcqe->total_data_placed);
1129                         nCmd->transferred_length = 0;
1130                         nCmd->rcv_rsplen = 0;
1131                         nCmd->status = NVME_SC_INTERNAL;
1132                 }
1133         }
1134
1135         /* pick up SLI4 exhange busy condition */
1136         if (bf_get(lpfc_wcqe_c_xb, wcqe))
1137                 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
1138         else
1139                 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
1140
1141         /* Update stats and complete the IO.  There is
1142          * no need for dma unprep because the nvme_transport
1143          * owns the dma address.
1144          */
1145 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1146         if (lpfc_ncmd->ts_cmd_start) {
1147                 lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
1148                 lpfc_ncmd->ts_data_nvme = ktime_get_ns();
1149                 phba->ktime_last_cmd = lpfc_ncmd->ts_data_nvme;
1150                 lpfc_nvme_ktime(phba, lpfc_ncmd);
1151         }
1152         if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1153                 if (lpfc_ncmd->cpu != smp_processor_id())
1154                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1155                                          "6701 CPU Check cmpl: "
1156                                          "cpu %d expect %d\n",
1157                                          smp_processor_id(), lpfc_ncmd->cpu);
1158                 if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
1159                         phba->cpucheck_cmpl_io[lpfc_ncmd->cpu]++;
1160         }
1161 #endif
1162
1163         /* NVME targets need completion held off until the abort exchange
1164          * completes unless the NVME Rport is getting unregistered.
1165          */
1166
1167         if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
1168                 freqpriv = nCmd->private;
1169                 freqpriv->nvme_buf = NULL;
1170                 nCmd->done(nCmd);
1171                 lpfc_ncmd->nvmeCmd = NULL;
1172         }
1173
1174         spin_lock_irqsave(&phba->hbalock, flags);
1175         lpfc_ncmd->nrport = NULL;
1176         spin_unlock_irqrestore(&phba->hbalock, flags);
1177
1178         /* Call release with XB=1 to queue the IO into the abort list. */
1179         lpfc_release_nvme_buf(phba, lpfc_ncmd);
1180 }
1181
1182
1183 /**
1184  * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
1185  * @lpfc_pnvme: Pointer to the driver's nvme instance data
1186  * @lpfc_nvme_lport: Pointer to the driver's local port data
1187  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1188  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1189  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1190  *
1191  * Driver registers this routine as it io request handler.  This
1192  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1193  * data structure to the rport indicated in @lpfc_nvme_rport.
1194  *
1195  * Return value :
1196  *   0 - Success
1197  *   TODO: What are the failure codes.
1198  **/
1199 static int
1200 lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
1201                       struct lpfc_nvme_buf *lpfc_ncmd,
1202                       struct lpfc_nodelist *pnode,
1203                       struct lpfc_nvme_ctrl_stat *cstat)
1204 {
1205         struct lpfc_hba *phba = vport->phba;
1206         struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1207         struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
1208         union lpfc_wqe128 *wqe = &pwqeq->wqe;
1209         uint32_t req_len;
1210
1211         if (!pnode || !NLP_CHK_NODE_ACT(pnode))
1212                 return -EINVAL;
1213
1214         /*
1215          * There are three possibilities here - use scatter-gather segment, use
1216          * the single mapping, or neither.
1217          */
1218         if (nCmd->sg_cnt) {
1219                 if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
1220                         /* From the iwrite template, initialize words 7 - 11 */
1221                         memcpy(&wqe->words[7],
1222                                &lpfc_iwrite_cmd_template.words[7],
1223                                sizeof(uint32_t) * 5);
1224
1225                         /* Word 4 */
1226                         wqe->fcp_iwrite.total_xfer_len = nCmd->payload_length;
1227
1228                         /* Word 5 */
1229                         if ((phba->cfg_nvme_enable_fb) &&
1230                             (pnode->nlp_flag & NLP_FIRSTBURST)) {
1231                                 req_len = lpfc_ncmd->nvmeCmd->payload_length;
1232                                 if (req_len < pnode->nvme_fb_size)
1233                                         wqe->fcp_iwrite.initial_xfer_len =
1234                                                 req_len;
1235                                 else
1236                                         wqe->fcp_iwrite.initial_xfer_len =
1237                                                 pnode->nvme_fb_size;
1238                         } else {
1239                                 wqe->fcp_iwrite.initial_xfer_len = 0;
1240                         }
1241                         atomic_inc(&cstat->fc4NvmeOutputRequests);
1242                 } else {
1243                         /* From the iread template, initialize words 7 - 11 */
1244                         memcpy(&wqe->words[7],
1245                                &lpfc_iread_cmd_template.words[7],
1246                                sizeof(uint32_t) * 5);
1247
1248                         /* Word 4 */
1249                         wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
1250
1251                         /* Word 5 */
1252                         wqe->fcp_iread.rsrvd5 = 0;
1253
1254                         atomic_inc(&cstat->fc4NvmeInputRequests);
1255                 }
1256         } else {
1257                 /* From the icmnd template, initialize words 4 - 11 */
1258                 memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
1259                        sizeof(uint32_t) * 8);
1260                 atomic_inc(&cstat->fc4NvmeControlRequests);
1261         }
1262         /*
1263          * Finish initializing those WQE fields that are independent
1264          * of the nvme_cmnd request_buffer
1265          */
1266
1267         /* Word 3 */
1268         bf_set(payload_offset_len, &wqe->fcp_icmd,
1269                (nCmd->rsplen + nCmd->cmdlen));
1270
1271         /* Word 6 */
1272         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
1273                phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
1274         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
1275
1276         /* Word 8 */
1277         wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
1278
1279         /* Word 9 */
1280         bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
1281
1282         /* Words 13 14 15 are for PBDE support */
1283
1284         pwqeq->vport = vport;
1285         return 0;
1286 }
1287
1288
1289 /**
1290  * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
1291  * @lpfc_pnvme: Pointer to the driver's nvme instance data
1292  * @lpfc_nvme_lport: Pointer to the driver's local port data
1293  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1294  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1295  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1296  *
1297  * Driver registers this routine as it io request handler.  This
1298  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1299  * data structure to the rport indicated in @lpfc_nvme_rport.
1300  *
1301  * Return value :
1302  *   0 - Success
1303  *   TODO: What are the failure codes.
1304  **/
1305 static int
1306 lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
1307                       struct lpfc_nvme_buf *lpfc_ncmd)
1308 {
1309         struct lpfc_hba *phba = vport->phba;
1310         struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1311         union lpfc_wqe128 *wqe = &lpfc_ncmd->cur_iocbq.wqe;
1312         struct sli4_sge *sgl = lpfc_ncmd->nvme_sgl;
1313         struct scatterlist *data_sg;
1314         struct sli4_sge *first_data_sgl;
1315         struct ulp_bde64 *bde;
1316         dma_addr_t physaddr;
1317         uint32_t num_bde = 0;
1318         uint32_t dma_len;
1319         uint32_t dma_offset = 0;
1320         int nseg, i;
1321
1322         /* Fix up the command and response DMA stuff. */
1323         lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
1324
1325         /*
1326          * There are three possibilities here - use scatter-gather segment, use
1327          * the single mapping, or neither.
1328          */
1329         if (nCmd->sg_cnt) {
1330                 /*
1331                  * Jump over the cmd and rsp SGEs.  The fix routine
1332                  * has already adjusted for this.
1333                  */
1334                 sgl += 2;
1335
1336                 first_data_sgl = sgl;
1337                 lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
1338                 if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
1339                         lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1340                                         "6058 Too many sg segments from "
1341                                         "NVME Transport.  Max %d, "
1342                                         "nvmeIO sg_cnt %d\n",
1343                                         phba->cfg_nvme_seg_cnt + 1,
1344                                         lpfc_ncmd->seg_cnt);
1345                         lpfc_ncmd->seg_cnt = 0;
1346                         return 1;
1347                 }
1348
1349                 /*
1350                  * The driver established a maximum scatter-gather segment count
1351                  * during probe that limits the number of sg elements in any
1352                  * single nvme command.  Just run through the seg_cnt and format
1353                  * the sge's.
1354                  */
1355                 nseg = nCmd->sg_cnt;
1356                 data_sg = nCmd->first_sgl;
1357                 for (i = 0; i < nseg; i++) {
1358                         if (data_sg == NULL) {
1359                                 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1360                                                 "6059 dptr err %d, nseg %d\n",
1361                                                 i, nseg);
1362                                 lpfc_ncmd->seg_cnt = 0;
1363                                 return 1;
1364                         }
1365                         physaddr = data_sg->dma_address;
1366                         dma_len = data_sg->length;
1367                         sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1368                         sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1369                         sgl->word2 = le32_to_cpu(sgl->word2);
1370                         if ((num_bde + 1) == nseg)
1371                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
1372                         else
1373                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
1374                         bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1375                         bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
1376                         sgl->word2 = cpu_to_le32(sgl->word2);
1377                         sgl->sge_len = cpu_to_le32(dma_len);
1378
1379                         dma_offset += dma_len;
1380                         data_sg = sg_next(data_sg);
1381                         sgl++;
1382                 }
1383                 if (phba->cfg_enable_pbde) {
1384                         /* Use PBDE support for first SGL only, offset == 0 */
1385                         /* Words 13-15 */
1386                         bde = (struct ulp_bde64 *)
1387                                 &wqe->words[13];
1388                         bde->addrLow = first_data_sgl->addr_lo;
1389                         bde->addrHigh = first_data_sgl->addr_hi;
1390                         bde->tus.f.bdeSize =
1391                                 le32_to_cpu(first_data_sgl->sge_len);
1392                         bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1393                         bde->tus.w = cpu_to_le32(bde->tus.w);
1394                         /* wqe_pbde is 1 in template */
1395                 } else {
1396                         memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
1397                         bf_set(wqe_pbde, &wqe->generic.wqe_com, 0);
1398                 }
1399
1400         } else {
1401                 /* For this clause to be valid, the payload_length
1402                  * and sg_cnt must zero.
1403                  */
1404                 if (nCmd->payload_length != 0) {
1405                         lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1406                                         "6063 NVME DMA Prep Err: sg_cnt %d "
1407                                         "payload_length x%x\n",
1408                                         nCmd->sg_cnt, nCmd->payload_length);
1409                         return 1;
1410                 }
1411         }
1412         return 0;
1413 }
1414
1415 /**
1416  * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
1417  * @lpfc_pnvme: Pointer to the driver's nvme instance data
1418  * @lpfc_nvme_lport: Pointer to the driver's local port data
1419  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1420  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1421  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1422  *
1423  * Driver registers this routine as it io request handler.  This
1424  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1425  * data structure to the rport
1426  indicated in @lpfc_nvme_rport.
1427  *
1428  * Return value :
1429  *   0 - Success
1430  *   TODO: What are the failure codes.
1431  **/
1432 static int
1433 lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
1434                         struct nvme_fc_remote_port *pnvme_rport,
1435                         void *hw_queue_handle,
1436                         struct nvmefc_fcp_req *pnvme_fcreq)
1437 {
1438         int ret = 0;
1439         int expedite = 0;
1440         int idx;
1441         struct lpfc_nvme_lport *lport;
1442         struct lpfc_nvme_ctrl_stat *cstat;
1443         struct lpfc_vport *vport;
1444         struct lpfc_hba *phba;
1445         struct lpfc_nodelist *ndlp;
1446         struct lpfc_nvme_buf *lpfc_ncmd;
1447         struct lpfc_nvme_rport *rport;
1448         struct lpfc_nvme_qhandle *lpfc_queue_info;
1449         struct lpfc_nvme_fcpreq_priv *freqpriv;
1450         struct nvme_common_command *sqe;
1451 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1452         uint64_t start = 0;
1453 #endif
1454
1455         /* Validate pointers. LLDD fault handling with transport does
1456          * have timing races.
1457          */
1458         lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1459         if (unlikely(!lport)) {
1460                 ret = -EINVAL;
1461                 goto out_fail;
1462         }
1463
1464         vport = lport->vport;
1465
1466         if (unlikely(!hw_queue_handle)) {
1467                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1468                                  "6117 Fail IO, NULL hw_queue_handle\n");
1469                 atomic_inc(&lport->xmt_fcp_err);
1470                 ret = -EBUSY;
1471                 goto out_fail;
1472         }
1473
1474         phba = vport->phba;
1475
1476         if (vport->load_flag & FC_UNLOADING) {
1477                 ret = -ENODEV;
1478                 goto out_fail;
1479         }
1480
1481         if (vport->load_flag & FC_UNLOADING) {
1482                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1483                                  "6124 Fail IO, Driver unload\n");
1484                 atomic_inc(&lport->xmt_fcp_err);
1485                 ret = -ENODEV;
1486                 goto out_fail;
1487         }
1488
1489         freqpriv = pnvme_fcreq->private;
1490         if (unlikely(!freqpriv)) {
1491                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1492                                  "6158 Fail IO, NULL request data\n");
1493                 atomic_inc(&lport->xmt_fcp_err);
1494                 ret = -EINVAL;
1495                 goto out_fail;
1496         }
1497
1498 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1499         if (phba->ktime_on)
1500                 start = ktime_get_ns();
1501 #endif
1502         rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
1503         lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
1504
1505         /*
1506          * Catch race where our node has transitioned, but the
1507          * transport is still transitioning.
1508          */
1509         ndlp = rport->ndlp;
1510         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1511                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
1512                                  "6053 Fail IO, ndlp not ready: rport %p "
1513                                   "ndlp %p, DID x%06x\n",
1514                                  rport, ndlp, pnvme_rport->port_id);
1515                 atomic_inc(&lport->xmt_fcp_err);
1516                 ret = -EBUSY;
1517                 goto out_fail;
1518         }
1519
1520         /* The remote node has to be a mapped target or it's an error. */
1521         if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
1522             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
1523                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
1524                                  "6036 Fail IO, DID x%06x not ready for "
1525                                  "IO. State x%x, Type x%x Flg x%x\n",
1526                                  pnvme_rport->port_id,
1527                                  ndlp->nlp_state, ndlp->nlp_type,
1528                                  ndlp->upcall_flags);
1529                 atomic_inc(&lport->xmt_fcp_bad_ndlp);
1530                 ret = -EBUSY;
1531                 goto out_fail;
1532
1533         }
1534
1535         /* Currently only NVME Keep alive commands should be expedited
1536          * if the driver runs out of a resource. These should only be
1537          * issued on the admin queue, qidx 0
1538          */
1539         if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
1540                 sqe = &((struct nvme_fc_cmd_iu *)
1541                         pnvme_fcreq->cmdaddr)->sqe.common;
1542                 if (sqe->opcode == nvme_admin_keep_alive)
1543                         expedite = 1;
1544         }
1545
1546         /* The node is shared with FCP IO, make sure the IO pending count does
1547          * not exceed the programmed depth.
1548          */
1549         if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1550                 if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
1551                     !expedite) {
1552                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1553                                          "6174 Fail IO, ndlp qdepth exceeded: "
1554                                          "idx %d DID %x pend %d qdepth %d\n",
1555                                          lpfc_queue_info->index, ndlp->nlp_DID,
1556                                          atomic_read(&ndlp->cmd_pending),
1557                                          ndlp->cmd_qdepth);
1558                         atomic_inc(&lport->xmt_fcp_qdepth);
1559                         ret = -EBUSY;
1560                         goto out_fail;
1561                 }
1562         }
1563
1564         lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, expedite);
1565         if (lpfc_ncmd == NULL) {
1566                 atomic_inc(&lport->xmt_fcp_noxri);
1567                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1568                                  "6065 Fail IO, driver buffer pool is empty: "
1569                                  "idx %d DID %x\n",
1570                                  lpfc_queue_info->index, ndlp->nlp_DID);
1571                 ret = -EBUSY;
1572                 goto out_fail;
1573         }
1574 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1575         if (start) {
1576                 lpfc_ncmd->ts_cmd_start = start;
1577                 lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
1578         } else {
1579                 lpfc_ncmd->ts_cmd_start = 0;
1580         }
1581 #endif
1582
1583         /*
1584          * Store the data needed by the driver to issue, abort, and complete
1585          * an IO.
1586          * Do not let the IO hang out forever.  There is no midlayer issuing
1587          * an abort so inform the FW of the maximum IO pending time.
1588          */
1589         freqpriv->nvme_buf = lpfc_ncmd;
1590         lpfc_ncmd->nvmeCmd = pnvme_fcreq;
1591         lpfc_ncmd->nrport = rport;
1592         lpfc_ncmd->ndlp = ndlp;
1593         lpfc_ncmd->start_time = jiffies;
1594
1595         /*
1596          * Issue the IO on the WQ indicated by index in the hw_queue_handle.
1597          * This identfier was create in our hardware queue create callback
1598          * routine. The driver now is dependent on the IO queue steering from
1599          * the transport.  We are trusting the upper NVME layers know which
1600          * index to use and that they have affinitized a CPU to this hardware
1601          * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
1602          */
1603         idx = lpfc_queue_info->index;
1604         lpfc_ncmd->cur_iocbq.hba_wqidx = idx;
1605         cstat = &lport->cstat[idx];
1606
1607         lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp, cstat);
1608         ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
1609         if (ret) {
1610                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1611                                  "6175 Fail IO, Prep DMA: "
1612                                  "idx %d DID %x\n",
1613                                  lpfc_queue_info->index, ndlp->nlp_DID);
1614                 atomic_inc(&lport->xmt_fcp_err);
1615                 ret = -ENOMEM;
1616                 goto out_free_nvme_buf;
1617         }
1618
1619         lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1620                          lpfc_ncmd->cur_iocbq.sli4_xritag,
1621                          lpfc_queue_info->index, ndlp->nlp_DID);
1622
1623         ret = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, &lpfc_ncmd->cur_iocbq);
1624         if (ret) {
1625                 atomic_inc(&lport->xmt_fcp_wqerr);
1626                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1627                                  "6113 Fail IO, Could not issue WQE err %x "
1628                                  "sid: x%x did: x%x oxid: x%x\n",
1629                                  ret, vport->fc_myDID, ndlp->nlp_DID,
1630                                  lpfc_ncmd->cur_iocbq.sli4_xritag);
1631                 goto out_free_nvme_buf;
1632         }
1633
1634 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1635         if (lpfc_ncmd->ts_cmd_start)
1636                 lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1637
1638         if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1639                 lpfc_ncmd->cpu = smp_processor_id();
1640                 if (lpfc_ncmd->cpu != lpfc_queue_info->index) {
1641                         /* Check for admin queue */
1642                         if (lpfc_queue_info->qidx) {
1643                                 lpfc_printf_vlog(vport,
1644                                                  KERN_ERR, LOG_NVME_IOERR,
1645                                                 "6702 CPU Check cmd: "
1646                                                 "cpu %d wq %d\n",
1647                                                 lpfc_ncmd->cpu,
1648                                                 lpfc_queue_info->index);
1649                         }
1650                         lpfc_ncmd->cpu = lpfc_queue_info->index;
1651                 }
1652                 if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
1653                         phba->cpucheck_xmt_io[lpfc_ncmd->cpu]++;
1654         }
1655 #endif
1656         return 0;
1657
1658  out_free_nvme_buf:
1659         if (lpfc_ncmd->nvmeCmd->sg_cnt) {
1660                 if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
1661                         atomic_dec(&cstat->fc4NvmeOutputRequests);
1662                 else
1663                         atomic_dec(&cstat->fc4NvmeInputRequests);
1664         } else
1665                 atomic_dec(&cstat->fc4NvmeControlRequests);
1666         lpfc_release_nvme_buf(phba, lpfc_ncmd);
1667  out_fail:
1668         return ret;
1669 }
1670
1671 /**
1672  * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
1673  * @phba: Pointer to HBA context object
1674  * @cmdiocb: Pointer to command iocb object.
1675  * @rspiocb: Pointer to response iocb object.
1676  *
1677  * This is the callback function for any NVME FCP IO that was aborted.
1678  *
1679  * Return value:
1680  *   None
1681  **/
1682 void
1683 lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1684                            struct lpfc_wcqe_complete *abts_cmpl)
1685 {
1686         lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
1687                         "6145 ABORT_XRI_CN completing on rpi x%x "
1688                         "original iotag x%x, abort cmd iotag x%x "
1689                         "req_tag x%x, status x%x, hwstatus x%x\n",
1690                         cmdiocb->iocb.un.acxri.abortContextTag,
1691                         cmdiocb->iocb.un.acxri.abortIoTag,
1692                         cmdiocb->iotag,
1693                         bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
1694                         bf_get(lpfc_wcqe_c_status, abts_cmpl),
1695                         bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
1696         lpfc_sli_release_iocbq(phba, cmdiocb);
1697 }
1698
1699 /**
1700  * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
1701  * @lpfc_pnvme: Pointer to the driver's nvme instance data
1702  * @lpfc_nvme_lport: Pointer to the driver's local port data
1703  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1704  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1705  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1706  *
1707  * Driver registers this routine as its nvme request io abort handler.  This
1708  * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
1709  * data structure to the rport indicated in @lpfc_nvme_rport.  This routine
1710  * is executed asynchronously - one the target is validated as "MAPPED" and
1711  * ready for IO, the driver issues the abort request and returns.
1712  *
1713  * Return value:
1714  *   None
1715  **/
1716 static void
1717 lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
1718                     struct nvme_fc_remote_port *pnvme_rport,
1719                     void *hw_queue_handle,
1720                     struct nvmefc_fcp_req *pnvme_fcreq)
1721 {
1722         struct lpfc_nvme_lport *lport;
1723         struct lpfc_vport *vport;
1724         struct lpfc_hba *phba;
1725         struct lpfc_nvme_buf *lpfc_nbuf;
1726         struct lpfc_iocbq *abts_buf;
1727         struct lpfc_iocbq *nvmereq_wqe;
1728         struct lpfc_nvme_fcpreq_priv *freqpriv;
1729         union lpfc_wqe128 *abts_wqe;
1730         unsigned long flags;
1731         int ret_val;
1732
1733         /* Validate pointers. LLDD fault handling with transport does
1734          * have timing races.
1735          */
1736         lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1737         if (unlikely(!lport))
1738                 return;
1739
1740         vport = lport->vport;
1741
1742         if (unlikely(!hw_queue_handle)) {
1743                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1744                                  "6129 Fail Abort, HW Queue Handle NULL.\n");
1745                 return;
1746         }
1747
1748         phba = vport->phba;
1749         freqpriv = pnvme_fcreq->private;
1750
1751         if (unlikely(!freqpriv))
1752                 return;
1753         if (vport->load_flag & FC_UNLOADING)
1754                 return;
1755
1756         /* Announce entry to new IO submit field. */
1757         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1758                          "6002 Abort Request to rport DID x%06x "
1759                          "for nvme_fc_req %p\n",
1760                          pnvme_rport->port_id,
1761                          pnvme_fcreq);
1762
1763         /* If the hba is getting reset, this flag is set.  It is
1764          * cleared when the reset is complete and rings reestablished.
1765          */
1766         spin_lock_irqsave(&phba->hbalock, flags);
1767         /* driver queued commands are in process of being flushed */
1768         if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
1769                 spin_unlock_irqrestore(&phba->hbalock, flags);
1770                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1771                                  "6139 Driver in reset cleanup - flushing "
1772                                  "NVME Req now.  hba_flag x%x\n",
1773                                  phba->hba_flag);
1774                 return;
1775         }
1776
1777         lpfc_nbuf = freqpriv->nvme_buf;
1778         if (!lpfc_nbuf) {
1779                 spin_unlock_irqrestore(&phba->hbalock, flags);
1780                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1781                                  "6140 NVME IO req has no matching lpfc nvme "
1782                                  "io buffer.  Skipping abort req.\n");
1783                 return;
1784         } else if (!lpfc_nbuf->nvmeCmd) {
1785                 spin_unlock_irqrestore(&phba->hbalock, flags);
1786                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1787                                  "6141 lpfc NVME IO req has no nvme_fcreq "
1788                                  "io buffer.  Skipping abort req.\n");
1789                 return;
1790         }
1791         nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
1792
1793         /*
1794          * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
1795          * state must match the nvme_fcreq passed by the nvme
1796          * transport.  If they don't match, it is likely the driver
1797          * has already completed the NVME IO and the nvme transport
1798          * has not seen it yet.
1799          */
1800         if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
1801                 spin_unlock_irqrestore(&phba->hbalock, flags);
1802                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1803                                  "6143 NVME req mismatch: "
1804                                  "lpfc_nbuf %p nvmeCmd %p, "
1805                                  "pnvme_fcreq %p.  Skipping Abort xri x%x\n",
1806                                  lpfc_nbuf, lpfc_nbuf->nvmeCmd,
1807                                  pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1808                 return;
1809         }
1810
1811         /* Don't abort IOs no longer on the pending queue. */
1812         if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
1813                 spin_unlock_irqrestore(&phba->hbalock, flags);
1814                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1815                                  "6142 NVME IO req %p not queued - skipping "
1816                                  "abort req xri x%x\n",
1817                                  pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1818                 return;
1819         }
1820
1821         atomic_inc(&lport->xmt_fcp_abort);
1822         lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1823                          nvmereq_wqe->sli4_xritag,
1824                          nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
1825
1826         /* Outstanding abort is in progress */
1827         if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
1828                 spin_unlock_irqrestore(&phba->hbalock, flags);
1829                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1830                                  "6144 Outstanding NVME I/O Abort Request "
1831                                  "still pending on nvme_fcreq %p, "
1832                                  "lpfc_ncmd %p xri x%x\n",
1833                                  pnvme_fcreq, lpfc_nbuf,
1834                                  nvmereq_wqe->sli4_xritag);
1835                 return;
1836         }
1837
1838         abts_buf = __lpfc_sli_get_iocbq(phba);
1839         if (!abts_buf) {
1840                 spin_unlock_irqrestore(&phba->hbalock, flags);
1841                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1842                                  "6136 No available abort wqes. Skipping "
1843                                  "Abts req for nvme_fcreq %p xri x%x\n",
1844                                  pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1845                 return;
1846         }
1847
1848         /* Ready - mark outstanding as aborted by driver. */
1849         nvmereq_wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
1850
1851         /* Complete prepping the abort wqe and issue to the FW. */
1852         abts_wqe = &abts_buf->wqe;
1853
1854         /* WQEs are reused.  Clear stale data and set key fields to
1855          * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
1856          */
1857         memset(abts_wqe, 0, sizeof(union lpfc_wqe));
1858         bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
1859
1860         /* word 7 */
1861         bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
1862         bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
1863                nvmereq_wqe->iocb.ulpClass);
1864
1865         /* word 8 - tell the FW to abort the IO associated with this
1866          * outstanding exchange ID.
1867          */
1868         abts_wqe->abort_cmd.wqe_com.abort_tag = nvmereq_wqe->sli4_xritag;
1869
1870         /* word 9 - this is the iotag for the abts_wqe completion. */
1871         bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
1872                abts_buf->iotag);
1873
1874         /* word 10 */
1875         bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
1876         bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
1877
1878         /* word 11 */
1879         bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
1880         bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
1881         bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1882
1883         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
1884         abts_buf->iocb_flag |= LPFC_IO_NVME;
1885         abts_buf->hba_wqidx = nvmereq_wqe->hba_wqidx;
1886         abts_buf->vport = vport;
1887         abts_buf->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
1888         ret_val = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_buf);
1889         spin_unlock_irqrestore(&phba->hbalock, flags);
1890         if (ret_val) {
1891                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
1892                                  "6137 Failed abts issue_wqe with status x%x "
1893                                  "for nvme_fcreq %p.\n",
1894                                  ret_val, pnvme_fcreq);
1895                 lpfc_sli_release_iocbq(phba, abts_buf);
1896                 return;
1897         }
1898
1899         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1900                          "6138 Transport Abort NVME Request Issued for "
1901                          "ox_id x%x on reqtag x%x\n",
1902                          nvmereq_wqe->sli4_xritag,
1903                          abts_buf->iotag);
1904 }
1905
1906 /* Declare and initialization an instance of the FC NVME template. */
1907 static struct nvme_fc_port_template lpfc_nvme_template = {
1908         /* initiator-based functions */
1909         .localport_delete  = lpfc_nvme_localport_delete,
1910         .remoteport_delete = lpfc_nvme_remoteport_delete,
1911         .create_queue = lpfc_nvme_create_queue,
1912         .delete_queue = lpfc_nvme_delete_queue,
1913         .ls_req       = lpfc_nvme_ls_req,
1914         .fcp_io       = lpfc_nvme_fcp_io_submit,
1915         .ls_abort     = lpfc_nvme_ls_abort,
1916         .fcp_abort    = lpfc_nvme_fcp_abort,
1917
1918         .max_hw_queues = 1,
1919         .max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
1920         .max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
1921         .dma_boundary = 0xFFFFFFFF,
1922
1923         /* Sizes of additional private data for data structures.
1924          * No use for the last two sizes at this time.
1925          */
1926         .local_priv_sz = sizeof(struct lpfc_nvme_lport),
1927         .remote_priv_sz = sizeof(struct lpfc_nvme_rport),
1928         .lsrqst_priv_sz = 0,
1929         .fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
1930 };
1931
1932 /**
1933  * lpfc_sli4_post_nvme_sgl_block - post a block of nvme sgl list to firmware
1934  * @phba: pointer to lpfc hba data structure.
1935  * @nblist: pointer to nvme buffer list.
1936  * @count: number of scsi buffers on the list.
1937  *
1938  * This routine is invoked to post a block of @count scsi sgl pages from a
1939  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
1940  * No Lock is held.
1941  *
1942  **/
1943 static int
1944 lpfc_sli4_post_nvme_sgl_block(struct lpfc_hba *phba,
1945                               struct list_head *nblist,
1946                               int count)
1947 {
1948         struct lpfc_nvme_buf *lpfc_ncmd;
1949         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
1950         struct sgl_page_pairs *sgl_pg_pairs;
1951         void *viraddr;
1952         LPFC_MBOXQ_t *mbox;
1953         uint32_t reqlen, alloclen, pg_pairs;
1954         uint32_t mbox_tmo;
1955         uint16_t xritag_start = 0;
1956         int rc = 0;
1957         uint32_t shdr_status, shdr_add_status;
1958         dma_addr_t pdma_phys_bpl1;
1959         union lpfc_sli4_cfg_shdr *shdr;
1960
1961         /* Calculate the requested length of the dma memory */
1962         reqlen = count * sizeof(struct sgl_page_pairs) +
1963                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
1964         if (reqlen > SLI4_PAGE_SIZE) {
1965                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1966                                 "6118 Block sgl registration required DMA "
1967                                 "size (%d) great than a page\n", reqlen);
1968                 return -ENOMEM;
1969         }
1970         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1971         if (!mbox) {
1972                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1973                                 "6119 Failed to allocate mbox cmd memory\n");
1974                 return -ENOMEM;
1975         }
1976
1977         /* Allocate DMA memory and set up the non-embedded mailbox command */
1978         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
1979                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
1980                                 LPFC_SLI4_MBX_NEMBED);
1981
1982         if (alloclen < reqlen) {
1983                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1984                                 "6120 Allocated DMA memory size (%d) is "
1985                                 "less than the requested DMA memory "
1986                                 "size (%d)\n", alloclen, reqlen);
1987                 lpfc_sli4_mbox_cmd_free(phba, mbox);
1988                 return -ENOMEM;
1989         }
1990
1991         /* Get the first SGE entry from the non-embedded DMA memory */
1992         viraddr = mbox->sge_array->addr[0];
1993
1994         /* Set up the SGL pages in the non-embedded DMA pages */
1995         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
1996         sgl_pg_pairs = &sgl->sgl_pg_pairs;
1997
1998         pg_pairs = 0;
1999         list_for_each_entry(lpfc_ncmd, nblist, list) {
2000                 /* Set up the sge entry */
2001                 sgl_pg_pairs->sgl_pg0_addr_lo =
2002                         cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
2003                 sgl_pg_pairs->sgl_pg0_addr_hi =
2004                         cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
2005                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
2006                         pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
2007                                                 SGL_PAGE_SIZE;
2008                 else
2009                         pdma_phys_bpl1 = 0;
2010                 sgl_pg_pairs->sgl_pg1_addr_lo =
2011                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
2012                 sgl_pg_pairs->sgl_pg1_addr_hi =
2013                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
2014                 /* Keep the first xritag on the list */
2015                 if (pg_pairs == 0)
2016                         xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
2017                 sgl_pg_pairs++;
2018                 pg_pairs++;
2019         }
2020         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
2021         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
2022         /* Perform endian conversion if necessary */
2023         sgl->word0 = cpu_to_le32(sgl->word0);
2024
2025         if (!phba->sli4_hba.intr_enable)
2026                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
2027         else {
2028                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
2029                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
2030         }
2031         shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
2032         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2033         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2034         if (rc != MBX_TIMEOUT)
2035                 lpfc_sli4_mbox_cmd_free(phba, mbox);
2036         if (shdr_status || shdr_add_status || rc) {
2037                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2038                                 "6125 POST_SGL_BLOCK mailbox command failed "
2039                                 "status x%x add_status x%x mbx status x%x\n",
2040                                 shdr_status, shdr_add_status, rc);
2041                 rc = -ENXIO;
2042         }
2043         return rc;
2044 }
2045
2046 /**
2047  * lpfc_post_nvme_sgl_list - Post blocks of nvme buffer sgls from a list
2048  * @phba: pointer to lpfc hba data structure.
2049  * @post_nblist: pointer to the nvme buffer list.
2050  *
2051  * This routine walks a list of nvme buffers that was passed in. It attempts
2052  * to construct blocks of nvme buffer sgls which contains contiguous xris and
2053  * uses the non-embedded SGL block post mailbox commands to post to the port.
2054  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
2055  * embedded SGL post mailbox command for posting. The @post_nblist passed in
2056  * must be local list, thus no lock is needed when manipulate the list.
2057  *
2058  * Returns: 0 = failure, non-zero number of successfully posted buffers.
2059  **/
2060 static int
2061 lpfc_post_nvme_sgl_list(struct lpfc_hba *phba,
2062                              struct list_head *post_nblist, int sb_count)
2063 {
2064         struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
2065         int status, sgl_size;
2066         int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
2067         dma_addr_t pdma_phys_sgl1;
2068         int last_xritag = NO_XRI;
2069         int cur_xritag;
2070         LIST_HEAD(prep_nblist);
2071         LIST_HEAD(blck_nblist);
2072         LIST_HEAD(nvme_nblist);
2073
2074         /* sanity check */
2075         if (sb_count <= 0)
2076                 return -EINVAL;
2077
2078         sgl_size = phba->cfg_sg_dma_buf_size;
2079
2080         list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
2081                 list_del_init(&lpfc_ncmd->list);
2082                 block_cnt++;
2083                 if ((last_xritag != NO_XRI) &&
2084                     (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
2085                         /* a hole in xri block, form a sgl posting block */
2086                         list_splice_init(&prep_nblist, &blck_nblist);
2087                         post_cnt = block_cnt - 1;
2088                         /* prepare list for next posting block */
2089                         list_add_tail(&lpfc_ncmd->list, &prep_nblist);
2090                         block_cnt = 1;
2091                 } else {
2092                         /* prepare list for next posting block */
2093                         list_add_tail(&lpfc_ncmd->list, &prep_nblist);
2094                         /* enough sgls for non-embed sgl mbox command */
2095                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
2096                                 list_splice_init(&prep_nblist, &blck_nblist);
2097                                 post_cnt = block_cnt;
2098                                 block_cnt = 0;
2099                         }
2100                 }
2101                 num_posting++;
2102                 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
2103
2104                 /* end of repost sgl list condition for NVME buffers */
2105                 if (num_posting == sb_count) {
2106                         if (post_cnt == 0) {
2107                                 /* last sgl posting block */
2108                                 list_splice_init(&prep_nblist, &blck_nblist);
2109                                 post_cnt = block_cnt;
2110                         } else if (block_cnt == 1) {
2111                                 /* last single sgl with non-contiguous xri */
2112                                 if (sgl_size > SGL_PAGE_SIZE)
2113                                         pdma_phys_sgl1 =
2114                                                 lpfc_ncmd->dma_phys_sgl +
2115                                                 SGL_PAGE_SIZE;
2116                                 else
2117                                         pdma_phys_sgl1 = 0;
2118                                 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
2119                                 status = lpfc_sli4_post_sgl(phba,
2120                                                 lpfc_ncmd->dma_phys_sgl,
2121                                                 pdma_phys_sgl1, cur_xritag);
2122                                 if (status) {
2123                                         /* failure, put on abort nvme list */
2124                                         lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
2125                                 } else {
2126                                         /* success, put on NVME buffer list */
2127                                         lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
2128                                         lpfc_ncmd->status = IOSTAT_SUCCESS;
2129                                         num_posted++;
2130                                 }
2131                                 /* success, put on NVME buffer sgl list */
2132                                 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
2133                         }
2134                 }
2135
2136                 /* continue until a nembed page worth of sgls */
2137                 if (post_cnt == 0)
2138                         continue;
2139
2140                 /* post block of NVME buffer list sgls */
2141                 status = lpfc_sli4_post_nvme_sgl_block(phba, &blck_nblist,
2142                                                        post_cnt);
2143
2144                 /* don't reset xirtag due to hole in xri block */
2145                 if (block_cnt == 0)
2146                         last_xritag = NO_XRI;
2147
2148                 /* reset NVME buffer post count for next round of posting */
2149                 post_cnt = 0;
2150
2151                 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
2152                 while (!list_empty(&blck_nblist)) {
2153                         list_remove_head(&blck_nblist, lpfc_ncmd,
2154                                          struct lpfc_nvme_buf, list);
2155                         if (status) {
2156                                 /* failure, put on abort nvme list */
2157                                 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
2158                         } else {
2159                                 /* success, put on NVME buffer list */
2160                                 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
2161                                 lpfc_ncmd->status = IOSTAT_SUCCESS;
2162                                 num_posted++;
2163                         }
2164                         list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
2165                 }
2166         }
2167         /* Push NVME buffers with sgl posted to the available list */
2168         while (!list_empty(&nvme_nblist)) {
2169                 list_remove_head(&nvme_nblist, lpfc_ncmd,
2170                                  struct lpfc_nvme_buf, list);
2171                 lpfc_release_nvme_buf(phba, lpfc_ncmd);
2172         }
2173         return num_posted;
2174 }
2175
2176 /**
2177  * lpfc_repost_nvme_sgl_list - Repost all the allocated nvme buffer sgls
2178  * @phba: pointer to lpfc hba data structure.
2179  *
2180  * This routine walks the list of nvme buffers that have been allocated and
2181  * repost them to the port by using SGL block post. This is needed after a
2182  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
2183  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
2184  * to the lpfc_nvme_buf_list. If the repost fails, reject all nvme buffers.
2185  *
2186  * Returns: 0 = success, non-zero failure.
2187  **/
2188 int
2189 lpfc_repost_nvme_sgl_list(struct lpfc_hba *phba)
2190 {
2191         LIST_HEAD(post_nblist);
2192         int num_posted, rc = 0;
2193
2194         /* get all NVME buffers need to repost to a local list */
2195         spin_lock_irq(&phba->nvme_buf_list_get_lock);
2196         spin_lock(&phba->nvme_buf_list_put_lock);
2197         list_splice_init(&phba->lpfc_nvme_buf_list_get, &post_nblist);
2198         list_splice(&phba->lpfc_nvme_buf_list_put, &post_nblist);
2199         phba->get_nvme_bufs = 0;
2200         phba->put_nvme_bufs = 0;
2201         spin_unlock(&phba->nvme_buf_list_put_lock);
2202         spin_unlock_irq(&phba->nvme_buf_list_get_lock);
2203
2204         /* post the list of nvme buffer sgls to port if available */
2205         if (!list_empty(&post_nblist)) {
2206                 num_posted = lpfc_post_nvme_sgl_list(phba, &post_nblist,
2207                                                 phba->sli4_hba.nvme_xri_cnt);
2208                 /* failed to post any nvme buffer, return error */
2209                 if (num_posted == 0)
2210                         rc = -EIO;
2211         }
2212         return rc;
2213 }
2214
2215 /**
2216  * lpfc_new_nvme_buf - Scsi buffer allocator for HBA with SLI4 IF spec
2217  * @vport: The virtual port for which this call being executed.
2218  * @num_to_allocate: The requested number of buffers to allocate.
2219  *
2220  * This routine allocates nvme buffers for device with SLI-4 interface spec,
2221  * the nvme buffer contains all the necessary information needed to initiate
2222  * a NVME I/O. After allocating up to @num_to_allocate NVME buffers and put
2223  * them on a list, it post them to the port by using SGL block post.
2224  *
2225  * Return codes:
2226  *   int - number of nvme buffers that were allocated and posted.
2227  *   0 = failure, less than num_to_alloc is a partial failure.
2228  **/
2229 static int
2230 lpfc_new_nvme_buf(struct lpfc_vport *vport, int num_to_alloc)
2231 {
2232         struct lpfc_hba *phba = vport->phba;
2233         struct lpfc_nvme_buf *lpfc_ncmd;
2234         struct lpfc_iocbq *pwqeq;
2235         union lpfc_wqe128 *wqe;
2236         struct sli4_sge *sgl;
2237         dma_addr_t pdma_phys_sgl;
2238         uint16_t iotag, lxri = 0;
2239         int bcnt, num_posted, sgl_size;
2240         LIST_HEAD(prep_nblist);
2241         LIST_HEAD(post_nblist);
2242         LIST_HEAD(nvme_nblist);
2243
2244         sgl_size = phba->cfg_sg_dma_buf_size;
2245
2246         for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
2247                 lpfc_ncmd = kzalloc(sizeof(struct lpfc_nvme_buf), GFP_KERNEL);
2248                 if (!lpfc_ncmd)
2249                         break;
2250                 /*
2251                  * Get memory from the pci pool to map the virt space to
2252                  * pci bus space for an I/O. The DMA buffer includes the
2253                  * number of SGE's necessary to support the sg_tablesize.
2254                  */
2255                 lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
2256                                                   GFP_KERNEL,
2257                                                   &lpfc_ncmd->dma_handle);
2258                 if (!lpfc_ncmd->data) {
2259                         kfree(lpfc_ncmd);
2260                         break;
2261                 }
2262
2263                 lxri = lpfc_sli4_next_xritag(phba);
2264                 if (lxri == NO_XRI) {
2265                         dma_pool_free(phba->lpfc_sg_dma_buf_pool,
2266                                       lpfc_ncmd->data, lpfc_ncmd->dma_handle);
2267                         kfree(lpfc_ncmd);
2268                         break;
2269                 }
2270                 pwqeq = &(lpfc_ncmd->cur_iocbq);
2271                 wqe = &pwqeq->wqe;
2272
2273                 /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
2274                 iotag = lpfc_sli_next_iotag(phba, pwqeq);
2275                 if (iotag == 0) {
2276                         dma_pool_free(phba->lpfc_sg_dma_buf_pool,
2277                                       lpfc_ncmd->data, lpfc_ncmd->dma_handle);
2278                         kfree(lpfc_ncmd);
2279                         lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2280                                         "6121 Failed to allocated IOTAG for"
2281                                         " XRI:0x%x\n", lxri);
2282                         lpfc_sli4_free_xri(phba, lxri);
2283                         break;
2284                 }
2285                 pwqeq->sli4_lxritag = lxri;
2286                 pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
2287                 pwqeq->iocb_flag |= LPFC_IO_NVME;
2288                 pwqeq->context1 = lpfc_ncmd;
2289                 pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
2290
2291                 /* Initialize local short-hand pointers. */
2292                 lpfc_ncmd->nvme_sgl = lpfc_ncmd->data;
2293                 sgl = lpfc_ncmd->nvme_sgl;
2294                 pdma_phys_sgl = lpfc_ncmd->dma_handle;
2295                 lpfc_ncmd->dma_phys_sgl = pdma_phys_sgl;
2296
2297                 /* Rsp SGE will be filled in when we rcv an IO
2298                  * from the NVME Layer to be sent.
2299                  * The cmd is going to be embedded so we need a SKIP SGE.
2300                  */
2301                 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2302                 bf_set(lpfc_sli4_sge_last, sgl, 0);
2303                 sgl->word2 = cpu_to_le32(sgl->word2);
2304                 /* Fill in word 3 / sgl_len during cmd submission */
2305
2306                 lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
2307
2308                 /* Initialize WQE */
2309                 memset(wqe, 0, sizeof(union lpfc_wqe));
2310
2311                 /* add the nvme buffer to a post list */
2312                 list_add_tail(&lpfc_ncmd->list, &post_nblist);
2313                 spin_lock_irq(&phba->nvme_buf_list_get_lock);
2314                 phba->sli4_hba.nvme_xri_cnt++;
2315                 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
2316         }
2317         lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
2318                         "6114 Allocate %d out of %d requested new NVME "
2319                         "buffers\n", bcnt, num_to_alloc);
2320
2321         /* post the list of nvme buffer sgls to port if available */
2322         if (!list_empty(&post_nblist))
2323                 num_posted = lpfc_post_nvme_sgl_list(phba,
2324                                                      &post_nblist, bcnt);
2325         else
2326                 num_posted = 0;
2327
2328         return num_posted;
2329 }
2330
2331 static inline struct lpfc_nvme_buf *
2332 lpfc_nvme_buf(struct lpfc_hba *phba)
2333 {
2334         struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
2335
2336         list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
2337                                  &phba->lpfc_nvme_buf_list_get, list) {
2338                 list_del_init(&lpfc_ncmd->list);
2339                 phba->get_nvme_bufs--;
2340                 return lpfc_ncmd;
2341         }
2342         return NULL;
2343 }
2344
2345 /**
2346  * lpfc_get_nvme_buf - Get a nvme buffer from lpfc_nvme_buf_list of the HBA
2347  * @phba: The HBA for which this call is being executed.
2348  *
2349  * This routine removes a nvme buffer from head of @phba lpfc_nvme_buf_list list
2350  * and returns to caller.
2351  *
2352  * Return codes:
2353  *   NULL - Error
2354  *   Pointer to lpfc_nvme_buf - Success
2355  **/
2356 static struct lpfc_nvme_buf *
2357 lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
2358                   int expedite)
2359 {
2360         struct lpfc_nvme_buf *lpfc_ncmd = NULL;
2361         unsigned long iflag = 0;
2362
2363         spin_lock_irqsave(&phba->nvme_buf_list_get_lock, iflag);
2364         if (phba->get_nvme_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
2365                 lpfc_ncmd = lpfc_nvme_buf(phba);
2366         if (!lpfc_ncmd) {
2367                 spin_lock(&phba->nvme_buf_list_put_lock);
2368                 list_splice(&phba->lpfc_nvme_buf_list_put,
2369                             &phba->lpfc_nvme_buf_list_get);
2370                 phba->get_nvme_bufs += phba->put_nvme_bufs;
2371                 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
2372                 phba->put_nvme_bufs = 0;
2373                 spin_unlock(&phba->nvme_buf_list_put_lock);
2374                 if (phba->get_nvme_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
2375                         lpfc_ncmd = lpfc_nvme_buf(phba);
2376         }
2377         spin_unlock_irqrestore(&phba->nvme_buf_list_get_lock, iflag);
2378
2379         if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_ncmd) {
2380                 atomic_inc(&ndlp->cmd_pending);
2381                 lpfc_ncmd->flags |= LPFC_BUMP_QDEPTH;
2382         }
2383         return  lpfc_ncmd;
2384 }
2385
2386 /**
2387  * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
2388  * @phba: The Hba for which this call is being executed.
2389  * @lpfc_ncmd: The nvme buffer which is being released.
2390  *
2391  * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
2392  * lpfc_nvme_buf_list list. For SLI4 XRI's are tied to the nvme buffer
2393  * and cannot be reused for at least RA_TOV amount of time if it was
2394  * aborted.
2395  **/
2396 static void
2397 lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_nvme_buf *lpfc_ncmd)
2398 {
2399         unsigned long iflag = 0;
2400
2401         if ((lpfc_ncmd->flags & LPFC_BUMP_QDEPTH) && lpfc_ncmd->ndlp)
2402                 atomic_dec(&lpfc_ncmd->ndlp->cmd_pending);
2403
2404         lpfc_ncmd->nonsg_phys = 0;
2405         lpfc_ncmd->ndlp = NULL;
2406         lpfc_ncmd->flags &= ~LPFC_BUMP_QDEPTH;
2407
2408         if (lpfc_ncmd->flags & LPFC_SBUF_XBUSY) {
2409                 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2410                                 "6310 XB release deferred for "
2411                                 "ox_id x%x on reqtag x%x\n",
2412                                 lpfc_ncmd->cur_iocbq.sli4_xritag,
2413                                 lpfc_ncmd->cur_iocbq.iotag);
2414
2415                 spin_lock_irqsave(&phba->sli4_hba.abts_nvme_buf_list_lock,
2416                                         iflag);
2417                 list_add_tail(&lpfc_ncmd->list,
2418                         &phba->sli4_hba.lpfc_abts_nvme_buf_list);
2419                 spin_unlock_irqrestore(&phba->sli4_hba.abts_nvme_buf_list_lock,
2420                                         iflag);
2421         } else {
2422                 lpfc_ncmd->nvmeCmd = NULL;
2423                 lpfc_ncmd->cur_iocbq.iocb_flag = LPFC_IO_NVME;
2424                 spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
2425                 list_add_tail(&lpfc_ncmd->list, &phba->lpfc_nvme_buf_list_put);
2426                 phba->put_nvme_bufs++;
2427                 spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
2428         }
2429 }
2430
2431 /**
2432  * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
2433  * @pvport - the lpfc_vport instance requesting a localport.
2434  *
2435  * This routine is invoked to create an nvme localport instance to bind
2436  * to the nvme_fc_transport.  It is called once during driver load
2437  * like lpfc_create_shost after all other services are initialized.
2438  * It requires a vport, vpi, and wwns at call time.  Other localport
2439  * parameters are modified as the driver's FCID and the Fabric WWN
2440  * are established.
2441  *
2442  * Return codes
2443  *      0 - successful
2444  *      -ENOMEM - no heap memory available
2445  *      other values - from nvme registration upcall
2446  **/
2447 int
2448 lpfc_nvme_create_localport(struct lpfc_vport *vport)
2449 {
2450         int ret = 0;
2451         struct lpfc_hba  *phba = vport->phba;
2452         struct nvme_fc_port_info nfcp_info;
2453         struct nvme_fc_local_port *localport;
2454         struct lpfc_nvme_lport *lport;
2455         struct lpfc_nvme_ctrl_stat *cstat;
2456         int len, i;
2457
2458         /* Initialize this localport instance.  The vport wwn usage ensures
2459          * that NPIV is accounted for.
2460          */
2461         memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
2462         nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
2463         nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
2464         nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
2465
2466         /* Limit to LPFC_MAX_NVME_SEG_CNT.
2467          * For now need + 1 to get around NVME transport logic.
2468          */
2469         if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
2470                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_INIT,
2471                                  "6300 Reducing sg segment cnt to %d\n",
2472                                  LPFC_MAX_NVME_SEG_CNT);
2473                 phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
2474         } else {
2475                 phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
2476         }
2477         lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
2478         lpfc_nvme_template.max_hw_queues = phba->cfg_nvme_io_channel;
2479
2480         if (!IS_ENABLED(CONFIG_NVME_FC))
2481                 return ret;
2482
2483         cstat = kmalloc((sizeof(struct lpfc_nvme_ctrl_stat) *
2484                         phba->cfg_nvme_io_channel), GFP_KERNEL);
2485         if (!cstat)
2486                 return -ENOMEM;
2487
2488         /* localport is allocated from the stack, but the registration
2489          * call allocates heap memory as well as the private area.
2490          */
2491
2492         ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
2493                                          &vport->phba->pcidev->dev, &localport);
2494         if (!ret) {
2495                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
2496                                  "6005 Successfully registered local "
2497                                  "NVME port num %d, localP %p, private %p, "
2498                                  "sg_seg %d\n",
2499                                  localport->port_num, localport,
2500                                  localport->private,
2501                                  lpfc_nvme_template.max_sgl_segments);
2502
2503                 /* Private is our lport size declared in the template. */
2504                 lport = (struct lpfc_nvme_lport *)localport->private;
2505                 vport->localport = localport;
2506                 lport->vport = vport;
2507                 lport->cstat = cstat;
2508                 vport->nvmei_support = 1;
2509
2510                 atomic_set(&lport->xmt_fcp_noxri, 0);
2511                 atomic_set(&lport->xmt_fcp_bad_ndlp, 0);
2512                 atomic_set(&lport->xmt_fcp_qdepth, 0);
2513                 atomic_set(&lport->xmt_fcp_err, 0);
2514                 atomic_set(&lport->xmt_fcp_wqerr, 0);
2515                 atomic_set(&lport->xmt_fcp_abort, 0);
2516                 atomic_set(&lport->xmt_ls_abort, 0);
2517                 atomic_set(&lport->xmt_ls_err, 0);
2518                 atomic_set(&lport->cmpl_fcp_xb, 0);
2519                 atomic_set(&lport->cmpl_fcp_err, 0);
2520                 atomic_set(&lport->cmpl_ls_xb, 0);
2521                 atomic_set(&lport->cmpl_ls_err, 0);
2522                 atomic_set(&lport->fc4NvmeLsRequests, 0);
2523                 atomic_set(&lport->fc4NvmeLsCmpls, 0);
2524
2525                 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
2526                         cstat = &lport->cstat[i];
2527                         atomic_set(&cstat->fc4NvmeInputRequests, 0);
2528                         atomic_set(&cstat->fc4NvmeOutputRequests, 0);
2529                         atomic_set(&cstat->fc4NvmeControlRequests, 0);
2530                         atomic_set(&cstat->fc4NvmeIoCmpls, 0);
2531                 }
2532
2533                 /* Don't post more new bufs if repost already recovered
2534                  * the nvme sgls.
2535                  */
2536                 if (phba->sli4_hba.nvme_xri_cnt == 0) {
2537                         len  = lpfc_new_nvme_buf(vport,
2538                                                  phba->sli4_hba.nvme_xri_max);
2539                         vport->phba->total_nvme_bufs += len;
2540                 }
2541         } else {
2542                 kfree(cstat);
2543         }
2544
2545         return ret;
2546 }
2547
2548 /* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2549  *
2550  * The driver has to wait for the host nvme transport to callback
2551  * indicating the localport has successfully unregistered all
2552  * resources.  Since this is an uninterruptible wait, loop every ten
2553  * seconds and print a message indicating no progress.
2554  *
2555  * An uninterruptible wait is used because of the risk of transport-to-
2556  * driver state mismatch.
2557  */
2558 void
2559 lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
2560                            struct lpfc_nvme_lport *lport,
2561                            struct completion *lport_unreg_cmp)
2562 {
2563 #if (IS_ENABLED(CONFIG_NVME_FC))
2564         u32 wait_tmo;
2565         int ret;
2566
2567         /* Host transport has to clean up and confirm requiring an indefinite
2568          * wait. Print a message if a 10 second wait expires and renew the
2569          * wait. This is unexpected.
2570          */
2571         wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2572         while (true) {
2573                 ret = wait_for_completion_timeout(lport_unreg_cmp, wait_tmo);
2574                 if (unlikely(!ret)) {
2575                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
2576                                          "6176 Lport %p Localport %p wait "
2577                                          "timed out. Renewing.\n",
2578                                          lport, vport->localport);
2579                         continue;
2580                 }
2581                 break;
2582         }
2583         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
2584                          "6177 Lport %p Localport %p Complete Success\n",
2585                          lport, vport->localport);
2586 #endif
2587 }
2588
2589 /**
2590  * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
2591  * @pnvme: pointer to lpfc nvme data structure.
2592  *
2593  * This routine is invoked to destroy all lports bound to the phba.
2594  * The lport memory was allocated by the nvme fc transport and is
2595  * released there.  This routine ensures all rports bound to the
2596  * lport have been disconnected.
2597  *
2598  **/
2599 void
2600 lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
2601 {
2602 #if (IS_ENABLED(CONFIG_NVME_FC))
2603         struct nvme_fc_local_port *localport;
2604         struct lpfc_nvme_lport *lport;
2605         struct lpfc_nvme_ctrl_stat *cstat;
2606         int ret;
2607         DECLARE_COMPLETION_ONSTACK(lport_unreg_cmp);
2608
2609         if (vport->nvmei_support == 0)
2610                 return;
2611
2612         localport = vport->localport;
2613         lport = (struct lpfc_nvme_lport *)localport->private;
2614         cstat = lport->cstat;
2615
2616         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2617                          "6011 Destroying NVME localport %p\n",
2618                          localport);
2619
2620         /* lport's rport list is clear.  Unregister
2621          * lport and release resources.
2622          */
2623         lport->lport_unreg_cmp = &lport_unreg_cmp;
2624         ret = nvme_fc_unregister_localport(localport);
2625
2626         /* Wait for completion.  This either blocks
2627          * indefinitely or succeeds
2628          */
2629         lpfc_nvme_lport_unreg_wait(vport, lport, &lport_unreg_cmp);
2630         vport->localport = NULL;
2631         kfree(cstat);
2632
2633         /* Regardless of the unregister upcall response, clear
2634          * nvmei_support.  All rports are unregistered and the
2635          * driver will clean up.
2636          */
2637         vport->nvmei_support = 0;
2638         if (ret == 0) {
2639                 lpfc_printf_vlog(vport,
2640                                  KERN_INFO, LOG_NVME_DISC,
2641                                  "6009 Unregistered lport Success\n");
2642         } else {
2643                 lpfc_printf_vlog(vport,
2644                                  KERN_INFO, LOG_NVME_DISC,
2645                                  "6010 Unregistered lport "
2646                                  "Failed, status x%x\n",
2647                                  ret);
2648         }
2649 #endif
2650 }
2651
2652 void
2653 lpfc_nvme_update_localport(struct lpfc_vport *vport)
2654 {
2655 #if (IS_ENABLED(CONFIG_NVME_FC))
2656         struct nvme_fc_local_port *localport;
2657         struct lpfc_nvme_lport *lport;
2658
2659         localport = vport->localport;
2660         if (!localport) {
2661                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
2662                                  "6710 Update NVME fail. No localport\n");
2663                 return;
2664         }
2665         lport = (struct lpfc_nvme_lport *)localport->private;
2666         if (!lport) {
2667                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
2668                                  "6171 Update NVME fail. localP %p, No lport\n",
2669                                  localport);
2670                 return;
2671         }
2672         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2673                          "6012 Update NVME lport %p did x%x\n",
2674                          localport, vport->fc_myDID);
2675
2676         localport->port_id = vport->fc_myDID;
2677         if (localport->port_id == 0)
2678                 localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
2679         else
2680                 localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
2681
2682         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2683                          "6030 bound lport %p to DID x%06x\n",
2684                          lport, localport->port_id);
2685 #endif
2686 }
2687
2688 int
2689 lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2690 {
2691 #if (IS_ENABLED(CONFIG_NVME_FC))
2692         int ret = 0;
2693         struct nvme_fc_local_port *localport;
2694         struct lpfc_nvme_lport *lport;
2695         struct lpfc_nvme_rport *rport;
2696         struct lpfc_nvme_rport *oldrport;
2697         struct nvme_fc_remote_port *remote_port;
2698         struct nvme_fc_port_info rpinfo;
2699         struct lpfc_nodelist *prev_ndlp = NULL;
2700
2701         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
2702                          "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
2703                          ndlp->nlp_DID, ndlp->nlp_type);
2704
2705         localport = vport->localport;
2706         if (!localport)
2707                 return 0;
2708
2709         lport = (struct lpfc_nvme_lport *)localport->private;
2710
2711         /* NVME rports are not preserved across devloss.
2712          * Just register this instance.  Note, rpinfo->dev_loss_tmo
2713          * is left 0 to indicate accept transport defaults.  The
2714          * driver communicates port role capabilities consistent
2715          * with the PRLI response data.
2716          */
2717         memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
2718         rpinfo.port_id = ndlp->nlp_DID;
2719         if (ndlp->nlp_type & NLP_NVME_TARGET)
2720                 rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
2721         if (ndlp->nlp_type & NLP_NVME_INITIATOR)
2722                 rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
2723
2724         if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
2725                 rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
2726
2727         rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
2728         rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2729
2730         spin_lock_irq(&vport->phba->hbalock);
2731         oldrport = lpfc_ndlp_get_nrport(ndlp);
2732         spin_unlock_irq(&vport->phba->hbalock);
2733         if (!oldrport)
2734                 lpfc_nlp_get(ndlp);
2735
2736         ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
2737         if (!ret) {
2738                 /* If the ndlp already has an nrport, this is just
2739                  * a resume of the existing rport.  Else this is a
2740                  * new rport.
2741                  */
2742                 /* Guard against an unregister/reregister
2743                  * race that leaves the WAIT flag set.
2744                  */
2745                 spin_lock_irq(&vport->phba->hbalock);
2746                 ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
2747                 spin_unlock_irq(&vport->phba->hbalock);
2748                 rport = remote_port->private;
2749                 if (oldrport) {
2750                         /* New remoteport record does not guarantee valid
2751                          * host private memory area.
2752                          */
2753                         prev_ndlp = oldrport->ndlp;
2754                         if (oldrport == remote_port->private) {
2755                                 /* Same remoteport - ndlp should match.
2756                                  * Just reuse.
2757                                  */
2758                                 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
2759                                                  LOG_NVME_DISC,
2760                                                  "6014 Rebinding lport to "
2761                                                  "remoteport %p wwpn 0x%llx, "
2762                                                  "Data: x%x x%x %p %p x%x x%06x\n",
2763                                                  remote_port,
2764                                                  remote_port->port_name,
2765                                                  remote_port->port_id,
2766                                                  remote_port->port_role,
2767                                                  prev_ndlp,
2768                                                  ndlp,
2769                                                  ndlp->nlp_type,
2770                                                  ndlp->nlp_DID);
2771                                 return 0;
2772                         }
2773
2774                         /* Sever the ndlp<->rport association
2775                          * before dropping the ndlp ref from
2776                          * register.
2777                          */
2778                         spin_lock_irq(&vport->phba->hbalock);
2779                         ndlp->nrport = NULL;
2780                         ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
2781                         spin_unlock_irq(&vport->phba->hbalock);
2782                         rport->ndlp = NULL;
2783                         rport->remoteport = NULL;
2784
2785                         /* Reference only removed if previous NDLP is no longer
2786                          * active. It might be just a swap and removing the
2787                          * reference would cause a premature cleanup.
2788                          */
2789                         if (prev_ndlp && prev_ndlp != ndlp) {
2790                                 if ((!NLP_CHK_NODE_ACT(prev_ndlp)) ||
2791                                     (!prev_ndlp->nrport))
2792                                         lpfc_nlp_put(prev_ndlp);
2793                         }
2794                 }
2795
2796                 /* Clean bind the rport to the ndlp. */
2797                 rport->remoteport = remote_port;
2798                 rport->lport = lport;
2799                 rport->ndlp = ndlp;
2800                 spin_lock_irq(&vport->phba->hbalock);
2801                 ndlp->nrport = rport;
2802                 spin_unlock_irq(&vport->phba->hbalock);
2803                 lpfc_printf_vlog(vport, KERN_INFO,
2804                                  LOG_NVME_DISC | LOG_NODE,
2805                                  "6022 Binding new rport to "
2806                                  "lport %p Remoteport %p rport %p WWNN 0x%llx, "
2807                                  "Rport WWPN 0x%llx DID "
2808                                  "x%06x Role x%x, ndlp %p prev_ndlp %p\n",
2809                                  lport, remote_port, rport,
2810                                  rpinfo.node_name, rpinfo.port_name,
2811                                  rpinfo.port_id, rpinfo.port_role,
2812                                  ndlp, prev_ndlp);
2813         } else {
2814                 lpfc_printf_vlog(vport, KERN_ERR,
2815                                  LOG_NVME_DISC | LOG_NODE,
2816                                  "6031 RemotePort Registration failed "
2817                                  "err: %d, DID x%06x\n",
2818                                  ret, ndlp->nlp_DID);
2819         }
2820
2821         return ret;
2822 #else
2823         return 0;
2824 #endif
2825 }
2826
2827 /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
2828  *
2829  * There is no notion of Devloss or rport recovery from the current
2830  * nvme_transport perspective.  Loss of an rport just means IO cannot
2831  * be sent and recovery is completely up to the initator.
2832  * For now, the driver just unbinds the DID and port_role so that
2833  * no further IO can be issued.  Changes are planned for later.
2834  *
2835  * Notes - the ndlp reference count is not decremented here since
2836  * since there is no nvme_transport api for devloss.  Node ref count
2837  * is only adjusted in driver unload.
2838  */
2839 void
2840 lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2841 {
2842 #if (IS_ENABLED(CONFIG_NVME_FC))
2843         int ret;
2844         struct nvme_fc_local_port *localport;
2845         struct lpfc_nvme_lport *lport;
2846         struct lpfc_nvme_rport *rport;
2847         struct nvme_fc_remote_port *remoteport = NULL;
2848
2849         localport = vport->localport;
2850
2851         /* This is fundamental error.  The localport is always
2852          * available until driver unload.  Just exit.
2853          */
2854         if (!localport)
2855                 return;
2856
2857         lport = (struct lpfc_nvme_lport *)localport->private;
2858         if (!lport)
2859                 goto input_err;
2860
2861         spin_lock_irq(&vport->phba->hbalock);
2862         rport = lpfc_ndlp_get_nrport(ndlp);
2863         if (rport)
2864                 remoteport = rport->remoteport;
2865         spin_unlock_irq(&vport->phba->hbalock);
2866         if (!remoteport)
2867                 goto input_err;
2868
2869         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2870                          "6033 Unreg nvme remoteport %p, portname x%llx, "
2871                          "port_id x%06x, portstate x%x port type x%x\n",
2872                          remoteport, remoteport->port_name,
2873                          remoteport->port_id, remoteport->port_state,
2874                          ndlp->nlp_type);
2875
2876         /* Sanity check ndlp type.  Only call for NVME ports. Don't
2877          * clear any rport state until the transport calls back.
2878          */
2879
2880         if (ndlp->nlp_type & NLP_NVME_TARGET) {
2881                 /* No concern about the role change on the nvme remoteport.
2882                  * The transport will update it.
2883                  */
2884                 ndlp->upcall_flags |= NLP_WAIT_FOR_UNREG;
2885
2886                 /* Don't let the host nvme transport keep sending keep-alives
2887                  * on this remoteport. Vport is unloading, no recovery. The
2888                  * return values is ignored.  The upcall is a courtesy to the
2889                  * transport.
2890                  */
2891                 if (vport->load_flag & FC_UNLOADING)
2892                         (void)nvme_fc_set_remoteport_devloss(remoteport, 0);
2893
2894                 ret = nvme_fc_unregister_remoteport(remoteport);
2895                 if (ret != 0) {
2896                         lpfc_nlp_put(ndlp);
2897                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
2898                                          "6167 NVME unregister failed %d "
2899                                          "port_state x%x\n",
2900                                          ret, remoteport->port_state);
2901                 }
2902         }
2903         return;
2904
2905  input_err:
2906 #endif
2907         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
2908                          "6168 State error: lport %p, rport%p FCID x%06x\n",
2909                          vport->localport, ndlp->rport, ndlp->nlp_DID);
2910 }
2911
2912 /**
2913  * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2914  * @phba: pointer to lpfc hba data structure.
2915  * @axri: pointer to the fcp xri abort wcqe structure.
2916  *
2917  * This routine is invoked by the worker thread to process a SLI4 fast-path
2918  * NVME aborted xri.  Aborted NVME IO commands are completed to the transport
2919  * here.
2920  **/
2921 void
2922 lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2923                            struct sli4_wcqe_xri_aborted *axri)
2924 {
2925         uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
2926         struct lpfc_nvme_buf *lpfc_ncmd, *next_lpfc_ncmd;
2927         struct nvmefc_fcp_req *nvme_cmd = NULL;
2928         struct lpfc_nodelist *ndlp;
2929         unsigned long iflag = 0;
2930
2931         if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
2932                 return;
2933         spin_lock_irqsave(&phba->hbalock, iflag);
2934         spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2935         list_for_each_entry_safe(lpfc_ncmd, next_lpfc_ncmd,
2936                                  &phba->sli4_hba.lpfc_abts_nvme_buf_list,
2937                                  list) {
2938                 if (lpfc_ncmd->cur_iocbq.sli4_xritag == xri) {
2939                         list_del_init(&lpfc_ncmd->list);
2940                         lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
2941                         lpfc_ncmd->status = IOSTAT_SUCCESS;
2942                         spin_unlock(
2943                                 &phba->sli4_hba.abts_nvme_buf_list_lock);
2944
2945                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2946                         ndlp = lpfc_ncmd->ndlp;
2947                         if (ndlp)
2948                                 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
2949
2950                         lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2951                                         "6311 nvme_cmd %p xri x%x tag x%x "
2952                                         "abort complete and xri released\n",
2953                                         lpfc_ncmd->nvmeCmd, xri,
2954                                         lpfc_ncmd->cur_iocbq.iotag);
2955
2956                         /* Aborted NVME commands are required to not complete
2957                          * before the abort exchange command fully completes.
2958                          * Once completed, it is available via the put list.
2959                          */
2960                         if (lpfc_ncmd->nvmeCmd) {
2961                                 nvme_cmd = lpfc_ncmd->nvmeCmd;
2962                                 nvme_cmd->done(nvme_cmd);
2963                                 lpfc_ncmd->nvmeCmd = NULL;
2964                         }
2965                         lpfc_release_nvme_buf(phba, lpfc_ncmd);
2966                         return;
2967                 }
2968         }
2969         spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2970         spin_unlock_irqrestore(&phba->hbalock, iflag);
2971
2972         lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2973                         "6312 XRI Aborted xri x%x not found\n", xri);
2974
2975 }
2976
2977 /**
2978  * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2979  * @phba: Pointer to HBA context object.
2980  *
2981  * This function flushes all wqes in the nvme rings and frees all resources
2982  * in the txcmplq. This function does not issue abort wqes for the IO
2983  * commands in txcmplq, they will just be returned with
2984  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2985  * slot has been permanently disabled.
2986  **/
2987 void
2988 lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2989 {
2990         struct lpfc_sli_ring  *pring;
2991         u32 i, wait_cnt = 0;
2992
2993         if (phba->sli_rev < LPFC_SLI_REV4 || !phba->sli4_hba.nvme_wq)
2994                 return;
2995
2996         /* Cycle through all NVME rings and make sure all outstanding
2997          * WQEs have been removed from the txcmplqs.
2998          */
2999         for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3000                 pring = phba->sli4_hba.nvme_wq[i]->pring;
3001
3002                 if (!pring)
3003                         continue;
3004
3005                 /* Retrieve everything on the txcmplq */
3006                 while (!list_empty(&pring->txcmplq)) {
3007                         msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
3008                         wait_cnt++;
3009
3010                         /* The sleep is 10mS.  Every ten seconds,
3011                          * dump a message.  Something is wrong.
3012                          */
3013                         if ((wait_cnt % 1000) == 0) {
3014                                 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3015                                                 "6178 NVME IO not empty, "
3016                                                 "cnt %d\n", wait_cnt);
3017                         }
3018                 }
3019         }
3020 }