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