GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / scsi / qla2xxx / qla_nvme.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2017 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_nvme.h"
8 #include <linux/scatterlist.h>
9 #include <linux/delay.h>
10 #include <linux/nvme.h>
11 #include <linux/nvme-fc.h>
12
13 static struct nvme_fc_port_template qla_nvme_fc_transport;
14
15 static void qla_nvme_unregister_remote_port(struct work_struct *);
16
17 int qla_nvme_register_remote(struct scsi_qla_host *vha, struct fc_port *fcport)
18 {
19         struct qla_nvme_rport *rport;
20         struct nvme_fc_port_info req;
21         int ret;
22
23         if (!IS_ENABLED(CONFIG_NVME_FC))
24                 return 0;
25
26         if (!vha->flags.nvme_enabled) {
27                 ql_log(ql_log_info, vha, 0x2100,
28                     "%s: Not registering target since Host NVME is not enabled\n",
29                     __func__);
30                 return 0;
31         }
32
33         if (!vha->nvme_local_port && qla_nvme_register_hba(vha))
34                 return 0;
35
36         if (!(fcport->nvme_prli_service_param &
37             (NVME_PRLI_SP_TARGET | NVME_PRLI_SP_DISCOVERY)) ||
38                 (fcport->nvme_flag & NVME_FLAG_REGISTERED))
39                 return 0;
40
41         INIT_WORK(&fcport->nvme_del_work, qla_nvme_unregister_remote_port);
42         fcport->nvme_flag &= ~NVME_FLAG_RESETTING;
43
44         memset(&req, 0, sizeof(struct nvme_fc_port_info));
45         req.port_name = wwn_to_u64(fcport->port_name);
46         req.node_name = wwn_to_u64(fcport->node_name);
47         req.port_role = 0;
48         req.dev_loss_tmo = NVME_FC_DEV_LOSS_TMO;
49
50         if (fcport->nvme_prli_service_param & NVME_PRLI_SP_INITIATOR)
51                 req.port_role = FC_PORT_ROLE_NVME_INITIATOR;
52
53         if (fcport->nvme_prli_service_param & NVME_PRLI_SP_TARGET)
54                 req.port_role |= FC_PORT_ROLE_NVME_TARGET;
55
56         if (fcport->nvme_prli_service_param & NVME_PRLI_SP_DISCOVERY)
57                 req.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
58
59         req.port_id = fcport->d_id.b24;
60
61         ql_log(ql_log_info, vha, 0x2102,
62             "%s: traddr=nn-0x%016llx:pn-0x%016llx PortID:%06x\n",
63             __func__, req.node_name, req.port_name,
64             req.port_id);
65
66         ret = nvme_fc_register_remoteport(vha->nvme_local_port, &req,
67             &fcport->nvme_remote_port);
68         if (ret) {
69                 ql_log(ql_log_warn, vha, 0x212e,
70                     "Failed to register remote port. Transport returned %d\n",
71                     ret);
72                 return ret;
73         }
74
75         rport = fcport->nvme_remote_port->private;
76         rport->fcport = fcport;
77         list_add_tail(&rport->list, &vha->nvme_rport_list);
78
79         fcport->nvme_flag |= NVME_FLAG_REGISTERED;
80         return 0;
81 }
82
83 /* Allocate a queue for NVMe traffic */
84 static int qla_nvme_alloc_queue(struct nvme_fc_local_port *lport,
85     unsigned int qidx, u16 qsize, void **handle)
86 {
87         struct scsi_qla_host *vha;
88         struct qla_hw_data *ha;
89         struct qla_qpair *qpair;
90
91         /* Map admin queue and 1st IO queue to index 0 */
92         if (qidx)
93                 qidx--;
94
95         vha = (struct scsi_qla_host *)lport->private;
96         ha = vha->hw;
97
98         ql_log(ql_log_info, vha, 0x2104,
99             "%s: handle %p, idx =%d, qsize %d\n",
100             __func__, handle, qidx, qsize);
101
102         if (qidx > qla_nvme_fc_transport.max_hw_queues) {
103                 ql_log(ql_log_warn, vha, 0x212f,
104                     "%s: Illegal qidx=%d. Max=%d\n",
105                     __func__, qidx, qla_nvme_fc_transport.max_hw_queues);
106                 return -EINVAL;
107         }
108
109         if (ha->queue_pair_map[qidx]) {
110                 *handle = ha->queue_pair_map[qidx];
111                 ql_log(ql_log_info, vha, 0x2121,
112                     "Returning existing qpair of %p for idx=%x\n",
113                     *handle, qidx);
114                 return 0;
115         }
116
117         qpair = qla2xxx_create_qpair(vha, 5, vha->vp_idx, true);
118         if (qpair == NULL) {
119                 ql_log(ql_log_warn, vha, 0x2122,
120                     "Failed to allocate qpair\n");
121                 return -EINVAL;
122         }
123         *handle = qpair;
124
125         return 0;
126 }
127
128 static void qla_nvme_sp_ls_done(void *ptr, int res)
129 {
130         srb_t *sp = ptr;
131         struct srb_iocb *nvme;
132         struct nvmefc_ls_req   *fd;
133         struct nvme_private *priv;
134
135         if (atomic_read(&sp->ref_count) == 0) {
136                 ql_log(ql_log_warn, sp->fcport->vha, 0x2123,
137                     "SP reference-count to ZERO on LS_done -- sp=%p.\n", sp);
138                 return;
139         }
140
141         if (!atomic_dec_and_test(&sp->ref_count))
142                 return;
143
144         if (res)
145                 res = -EINVAL;
146
147         nvme = &sp->u.iocb_cmd;
148         fd = nvme->u.nvme.desc;
149         priv = fd->private;
150         priv->comp_status = res;
151         schedule_work(&priv->ls_work);
152         /* work schedule doesn't need the sp */
153         qla2x00_rel_sp(sp);
154 }
155
156 static void qla_nvme_sp_done(void *ptr, int res)
157 {
158         srb_t *sp = ptr;
159         struct srb_iocb *nvme;
160         struct nvmefc_fcp_req *fd;
161
162         nvme = &sp->u.iocb_cmd;
163         fd = nvme->u.nvme.desc;
164
165         if (!atomic_dec_and_test(&sp->ref_count))
166                 return;
167
168         if (res == QLA_SUCCESS)
169                 fd->status = 0;
170         else
171                 fd->status = NVME_SC_INTERNAL;
172
173         fd->rcv_rsplen = nvme->u.nvme.rsp_pyld_len;
174         fd->done(fd);
175         qla2xxx_rel_qpair_sp(sp->qpair, sp);
176
177         return;
178 }
179
180 static void qla_nvme_abort_work(struct work_struct *work)
181 {
182         struct nvme_private *priv =
183                 container_of(work, struct nvme_private, abort_work);
184         srb_t *sp = priv->sp;
185         fc_port_t *fcport = sp->fcport;
186         struct qla_hw_data *ha = fcport->vha->hw;
187         int rval;
188
189         rval = ha->isp_ops->abort_command(sp);
190
191         ql_dbg(ql_dbg_io, fcport->vha, 0x212b,
192             "%s: %s command for sp=%p, handle=%x on fcport=%p rval=%x\n",
193             __func__, (rval != QLA_SUCCESS) ? "Failed to abort" : "Aborted",
194             sp, sp->handle, fcport, rval);
195 }
196
197 static void qla_nvme_ls_abort(struct nvme_fc_local_port *lport,
198     struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
199 {
200         struct nvme_private *priv = fd->private;
201
202         INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
203         schedule_work(&priv->abort_work);
204 }
205
206 static void qla_nvme_ls_complete(struct work_struct *work)
207 {
208         struct nvme_private *priv =
209             container_of(work, struct nvme_private, ls_work);
210         struct nvmefc_ls_req *fd = priv->fd;
211
212         fd->done(fd, priv->comp_status);
213 }
214
215 static int qla_nvme_ls_req(struct nvme_fc_local_port *lport,
216     struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
217 {
218         struct qla_nvme_rport *qla_rport = rport->private;
219         fc_port_t *fcport = qla_rport->fcport;
220         struct srb_iocb   *nvme;
221         struct nvme_private *priv = fd->private;
222         struct scsi_qla_host *vha;
223         int     rval = QLA_FUNCTION_FAILED;
224         struct qla_hw_data *ha;
225         srb_t           *sp;
226
227         vha = fcport->vha;
228         ha = vha->hw;
229         /* Alloc SRB structure */
230         sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
231         if (!sp)
232                 return rval;
233
234         sp->type = SRB_NVME_LS;
235         sp->name = "nvme_ls";
236         sp->done = qla_nvme_sp_ls_done;
237         atomic_set(&sp->ref_count, 1);
238         nvme = &sp->u.iocb_cmd;
239         priv->sp = sp;
240         priv->fd = fd;
241         INIT_WORK(&priv->ls_work, qla_nvme_ls_complete);
242         nvme->u.nvme.desc = fd;
243         nvme->u.nvme.dir = 0;
244         nvme->u.nvme.dl = 0;
245         nvme->u.nvme.cmd_len = fd->rqstlen;
246         nvme->u.nvme.rsp_len = fd->rsplen;
247         nvme->u.nvme.rsp_dma = fd->rspdma;
248         nvme->u.nvme.timeout_sec = fd->timeout;
249         nvme->u.nvme.cmd_dma = dma_map_single(&ha->pdev->dev, fd->rqstaddr,
250             fd->rqstlen, DMA_TO_DEVICE);
251         dma_sync_single_for_device(&ha->pdev->dev, nvme->u.nvme.cmd_dma,
252             fd->rqstlen, DMA_TO_DEVICE);
253
254         rval = qla2x00_start_sp(sp);
255         if (rval != QLA_SUCCESS) {
256                 ql_log(ql_log_warn, vha, 0x700e,
257                     "qla2x00_start_sp failed = %d\n", rval);
258                 atomic_dec(&sp->ref_count);
259                 wake_up(&sp->nvme_ls_waitq);
260                 return rval;
261         }
262
263         return rval;
264 }
265
266 static void qla_nvme_fcp_abort(struct nvme_fc_local_port *lport,
267     struct nvme_fc_remote_port *rport, void *hw_queue_handle,
268     struct nvmefc_fcp_req *fd)
269 {
270         struct nvme_private *priv = fd->private;
271
272         INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
273         schedule_work(&priv->abort_work);
274 }
275
276 static void qla_nvme_poll(struct nvme_fc_local_port *lport, void *hw_queue_handle)
277 {
278         struct qla_qpair *qpair = hw_queue_handle;
279         unsigned long flags;
280         struct scsi_qla_host *vha = lport->private;
281
282         spin_lock_irqsave(&qpair->qp_lock, flags);
283         qla24xx_process_response_queue(vha, qpair->rsp);
284         spin_unlock_irqrestore(&qpair->qp_lock, flags);
285 }
286
287 static inline int qla2x00_start_nvme_mq(srb_t *sp)
288 {
289         unsigned long   flags;
290         uint32_t        *clr_ptr;
291         uint32_t        index;
292         uint32_t        handle;
293         struct cmd_nvme *cmd_pkt;
294         uint16_t        cnt, i;
295         uint16_t        req_cnt;
296         uint16_t        tot_dsds;
297         uint16_t        avail_dsds;
298         uint32_t        *cur_dsd;
299         struct req_que *req = NULL;
300         struct scsi_qla_host *vha = sp->fcport->vha;
301         struct qla_hw_data *ha = vha->hw;
302         struct qla_qpair *qpair = sp->qpair;
303         struct srb_iocb *nvme = &sp->u.iocb_cmd;
304         struct scatterlist *sgl, *sg;
305         struct nvmefc_fcp_req *fd = nvme->u.nvme.desc;
306         uint32_t        rval = QLA_SUCCESS;
307
308         /* Setup qpair pointers */
309         req = qpair->req;
310         tot_dsds = fd->sg_cnt;
311
312         /* Acquire qpair specific lock */
313         spin_lock_irqsave(&qpair->qp_lock, flags);
314
315         /* Check for room in outstanding command list. */
316         handle = req->current_outstanding_cmd;
317         for (index = 1; index < req->num_outstanding_cmds; index++) {
318                 handle++;
319                 if (handle == req->num_outstanding_cmds)
320                         handle = 1;
321                 if (!req->outstanding_cmds[handle])
322                         break;
323         }
324
325         if (index == req->num_outstanding_cmds) {
326                 rval = -EBUSY;
327                 goto queuing_error;
328         }
329         req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
330         if (req->cnt < (req_cnt + 2)) {
331                 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
332                     RD_REG_DWORD_RELAXED(req->req_q_out);
333
334                 if (req->ring_index < cnt)
335                         req->cnt = cnt - req->ring_index;
336                 else
337                         req->cnt = req->length - (req->ring_index - cnt);
338
339                 if (req->cnt < (req_cnt + 2)){
340                         rval = -EBUSY;
341                         goto queuing_error;
342                 }
343         }
344
345         if (unlikely(!fd->sqid)) {
346                 struct nvme_fc_cmd_iu *cmd = fd->cmdaddr;
347                 if (cmd->sqe.common.opcode == nvme_admin_async_event) {
348                         nvme->u.nvme.aen_op = 1;
349                         atomic_inc(&ha->nvme_active_aen_cnt);
350                 }
351         }
352
353         /* Build command packet. */
354         req->current_outstanding_cmd = handle;
355         req->outstanding_cmds[handle] = sp;
356         sp->handle = handle;
357         req->cnt -= req_cnt;
358
359         cmd_pkt = (struct cmd_nvme *)req->ring_ptr;
360         cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
361
362         /* Zero out remaining portion of packet. */
363         clr_ptr = (uint32_t *)cmd_pkt + 2;
364         memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
365
366         cmd_pkt->entry_status = 0;
367
368         /* Update entry type to indicate Command NVME IOCB */
369         cmd_pkt->entry_type = COMMAND_NVME;
370
371         /* No data transfer how do we check buffer len == 0?? */
372         if (fd->io_dir == NVMEFC_FCP_READ) {
373                 cmd_pkt->control_flags =
374                     cpu_to_le16(CF_READ_DATA | CF_NVME_ENABLE);
375                 vha->qla_stats.input_bytes += fd->payload_length;
376                 vha->qla_stats.input_requests++;
377         } else if (fd->io_dir == NVMEFC_FCP_WRITE) {
378                 cmd_pkt->control_flags =
379                     cpu_to_le16(CF_WRITE_DATA | CF_NVME_ENABLE);
380                 vha->qla_stats.output_bytes += fd->payload_length;
381                 vha->qla_stats.output_requests++;
382         } else if (fd->io_dir == 0) {
383                 cmd_pkt->control_flags = cpu_to_le16(CF_NVME_ENABLE);
384         }
385
386         /* Set NPORT-ID */
387         cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
388         cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
389         cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
390         cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
391         cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
392
393         /* NVME RSP IU */
394         cmd_pkt->nvme_rsp_dsd_len = cpu_to_le16(fd->rsplen);
395         cmd_pkt->nvme_rsp_dseg_address[0] = cpu_to_le32(LSD(fd->rspdma));
396         cmd_pkt->nvme_rsp_dseg_address[1] = cpu_to_le32(MSD(fd->rspdma));
397
398         /* NVME CNMD IU */
399         cmd_pkt->nvme_cmnd_dseg_len = cpu_to_le16(fd->cmdlen);
400         cmd_pkt->nvme_cmnd_dseg_address[0] = cpu_to_le32(LSD(fd->cmddma));
401         cmd_pkt->nvme_cmnd_dseg_address[1] = cpu_to_le32(MSD(fd->cmddma));
402
403         cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
404         cmd_pkt->byte_count = cpu_to_le32(fd->payload_length);
405
406         /* One DSD is available in the Command Type NVME IOCB */
407         avail_dsds = 1;
408         cur_dsd = (uint32_t *)&cmd_pkt->nvme_data_dseg_address[0];
409         sgl = fd->first_sgl;
410
411         /* Load data segments */
412         for_each_sg(sgl, sg, tot_dsds, i) {
413                 dma_addr_t      sle_dma;
414                 cont_a64_entry_t *cont_pkt;
415
416                 /* Allocate additional continuation packets? */
417                 if (avail_dsds == 0) {
418                         /*
419                          * Five DSDs are available in the Continuation
420                          * Type 1 IOCB.
421                          */
422
423                         /* Adjust ring index */
424                         req->ring_index++;
425                         if (req->ring_index == req->length) {
426                                 req->ring_index = 0;
427                                 req->ring_ptr = req->ring;
428                         } else {
429                                 req->ring_ptr++;
430                         }
431                         cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
432                         *((uint32_t *)(&cont_pkt->entry_type)) =
433                             cpu_to_le32(CONTINUE_A64_TYPE);
434
435                         cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
436                         avail_dsds = 5;
437                 }
438
439                 sle_dma = sg_dma_address(sg);
440                 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
441                 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
442                 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
443                 avail_dsds--;
444         }
445
446         /* Set total entry count. */
447         cmd_pkt->entry_count = (uint8_t)req_cnt;
448         wmb();
449
450         /* Adjust ring index. */
451         req->ring_index++;
452         if (req->ring_index == req->length) {
453                 req->ring_index = 0;
454                 req->ring_ptr = req->ring;
455         } else {
456                 req->ring_ptr++;
457         }
458
459         /* Set chip new ring index. */
460         WRT_REG_DWORD(req->req_q_in, req->ring_index);
461
462 queuing_error:
463         spin_unlock_irqrestore(&qpair->qp_lock, flags);
464         return rval;
465 }
466
467 /* Post a command */
468 static int qla_nvme_post_cmd(struct nvme_fc_local_port *lport,
469     struct nvme_fc_remote_port *rport, void *hw_queue_handle,
470     struct nvmefc_fcp_req *fd)
471 {
472         fc_port_t *fcport;
473         struct srb_iocb *nvme;
474         struct scsi_qla_host *vha;
475         int rval = -ENODEV;
476         srb_t *sp;
477         struct qla_qpair *qpair = hw_queue_handle;
478         struct nvme_private *priv = fd->private;
479         struct qla_nvme_rport *qla_rport = rport->private;
480
481         if (!priv) {
482                 /* nvme association has been torn down */
483                 return rval;
484         }
485
486         fcport = qla_rport->fcport;
487
488         vha = fcport->vha;
489
490         if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
491                 return rval;
492
493         /*
494          * If we know the dev is going away while the transport is still sending
495          * IO's return busy back to stall the IO Q.  This happens when the
496          * link goes away and fw hasn't notified us yet, but IO's are being
497          * returned. If the dev comes back quickly we won't exhaust the IO
498          * retry count at the core.
499          */
500         if (fcport->nvme_flag & NVME_FLAG_RESETTING)
501                 return -EBUSY;
502
503         /* Alloc SRB structure */
504         sp = qla2xxx_get_qpair_sp(qpair, fcport, GFP_ATOMIC);
505         if (!sp)
506                 return -EBUSY;
507
508         atomic_set(&sp->ref_count, 1);
509         init_waitqueue_head(&sp->nvme_ls_waitq);
510         priv->sp = sp;
511         sp->type = SRB_NVME_CMD;
512         sp->name = "nvme_cmd";
513         sp->done = qla_nvme_sp_done;
514         sp->qpair = qpair;
515         sp->vha = vha;
516         nvme = &sp->u.iocb_cmd;
517         nvme->u.nvme.desc = fd;
518
519         rval = qla2x00_start_nvme_mq(sp);
520         if (rval != QLA_SUCCESS) {
521                 ql_log(ql_log_warn, vha, 0x212d,
522                     "qla2x00_start_nvme_mq failed = %d\n", rval);
523                 atomic_dec(&sp->ref_count);
524                 wake_up(&sp->nvme_ls_waitq);
525         }
526
527         return rval;
528 }
529
530 static void qla_nvme_localport_delete(struct nvme_fc_local_port *lport)
531 {
532         struct scsi_qla_host *vha = lport->private;
533
534         ql_log(ql_log_info, vha, 0x210f,
535             "localport delete of %p completed.\n", vha->nvme_local_port);
536         vha->nvme_local_port = NULL;
537         complete(&vha->nvme_del_done);
538 }
539
540 static void qla_nvme_remoteport_delete(struct nvme_fc_remote_port *rport)
541 {
542         fc_port_t *fcport;
543         struct qla_nvme_rport *qla_rport = rport->private, *trport;
544
545         fcport = qla_rport->fcport;
546         fcport->nvme_remote_port = NULL;
547         fcport->nvme_flag &= ~NVME_FLAG_REGISTERED;
548
549         list_for_each_entry_safe(qla_rport, trport,
550             &fcport->vha->nvme_rport_list, list) {
551                 if (qla_rport->fcport == fcport) {
552                         list_del(&qla_rport->list);
553                         break;
554                 }
555         }
556         complete(&fcport->nvme_del_done);
557
558         if (!test_bit(UNLOADING, &fcport->vha->dpc_flags)) {
559                 INIT_WORK(&fcport->free_work, qlt_free_session_done);
560                 schedule_work(&fcport->free_work);
561         }
562
563         fcport->nvme_flag &= ~NVME_FLAG_DELETING;
564         ql_log(ql_log_info, fcport->vha, 0x2110,
565             "remoteport_delete of %p completed.\n", fcport);
566 }
567
568 static struct nvme_fc_port_template qla_nvme_fc_transport = {
569         .localport_delete = qla_nvme_localport_delete,
570         .remoteport_delete = qla_nvme_remoteport_delete,
571         .create_queue   = qla_nvme_alloc_queue,
572         .delete_queue   = NULL,
573         .ls_req         = qla_nvme_ls_req,
574         .ls_abort       = qla_nvme_ls_abort,
575         .fcp_io         = qla_nvme_post_cmd,
576         .fcp_abort      = qla_nvme_fcp_abort,
577         .poll_queue     = qla_nvme_poll,
578         .max_hw_queues  = 8,
579         .max_sgl_segments = 128,
580         .max_dif_sgl_segments = 64,
581         .dma_boundary = 0xFFFFFFFF,
582         .local_priv_sz  = 8,
583         .remote_priv_sz = sizeof(struct qla_nvme_rport),
584         .lsrqst_priv_sz = sizeof(struct nvme_private),
585         .fcprqst_priv_sz = sizeof(struct nvme_private),
586 };
587
588 #define NVME_ABORT_POLLING_PERIOD    2
589 static int qla_nvme_wait_on_command(srb_t *sp)
590 {
591         int ret = QLA_SUCCESS;
592
593         wait_event_timeout(sp->nvme_ls_waitq, (atomic_read(&sp->ref_count) > 1),
594             NVME_ABORT_POLLING_PERIOD*HZ);
595
596         if (atomic_read(&sp->ref_count) > 1)
597                 ret = QLA_FUNCTION_FAILED;
598
599         return ret;
600 }
601
602 void qla_nvme_abort(struct qla_hw_data *ha, struct srb *sp, int res)
603 {
604         int rval;
605
606         if (ha->flags.fw_started) {
607                 rval = ha->isp_ops->abort_command(sp);
608                 if (!rval && !qla_nvme_wait_on_command(sp))
609                         ql_log(ql_log_warn, NULL, 0x2112,
610                             "timed out waiting on sp=%p\n", sp);
611         } else {
612                 sp->done(sp, res);
613         }
614 }
615
616 static void qla_nvme_unregister_remote_port(struct work_struct *work)
617 {
618         struct fc_port *fcport = container_of(work, struct fc_port,
619             nvme_del_work);
620         struct qla_nvme_rport *qla_rport, *trport;
621
622         if (!IS_ENABLED(CONFIG_NVME_FC))
623                 return;
624
625         ql_log(ql_log_warn, NULL, 0x2112,
626             "%s: unregister remoteport on %p\n",__func__, fcport);
627
628         list_for_each_entry_safe(qla_rport, trport,
629             &fcport->vha->nvme_rport_list, list) {
630                 if (qla_rport->fcport == fcport) {
631                         ql_log(ql_log_info, fcport->vha, 0x2113,
632                             "%s: fcport=%p\n", __func__, fcport);
633                         init_completion(&fcport->nvme_del_done);
634                         nvme_fc_unregister_remoteport(
635                             fcport->nvme_remote_port);
636                         wait_for_completion(&fcport->nvme_del_done);
637                         break;
638                 }
639         }
640 }
641
642 void qla_nvme_delete(struct scsi_qla_host *vha)
643 {
644         struct qla_nvme_rport *qla_rport, *trport;
645         fc_port_t *fcport;
646         int nv_ret;
647
648         if (!IS_ENABLED(CONFIG_NVME_FC))
649                 return;
650
651         list_for_each_entry_safe(qla_rport, trport,
652             &vha->nvme_rport_list, list) {
653                 fcport = qla_rport->fcport;
654
655                 ql_log(ql_log_info, fcport->vha, 0x2114, "%s: fcport=%p\n",
656                     __func__, fcport);
657
658                 nvme_fc_set_remoteport_devloss(fcport->nvme_remote_port, 0);
659         }
660
661         if (vha->nvme_local_port) {
662                 init_completion(&vha->nvme_del_done);
663                 ql_log(ql_log_info, vha, 0x2116,
664                         "unregister localport=%p\n",
665                         vha->nvme_local_port);
666                 nv_ret = nvme_fc_unregister_localport(vha->nvme_local_port);
667                 if (nv_ret)
668                         ql_log(ql_log_info, vha, 0x2115,
669                             "Unregister of localport failed\n");
670                 else
671                         wait_for_completion(&vha->nvme_del_done);
672         }
673 }
674
675 int qla_nvme_register_hba(struct scsi_qla_host *vha)
676 {
677         struct nvme_fc_port_template *tmpl;
678         struct qla_hw_data *ha;
679         struct nvme_fc_port_info pinfo;
680         int ret = -EINVAL;
681
682         if (!IS_ENABLED(CONFIG_NVME_FC))
683                 return ret;
684
685         ha = vha->hw;
686         tmpl = &qla_nvme_fc_transport;
687
688         WARN_ON(vha->nvme_local_port);
689         WARN_ON(ha->max_req_queues < 3);
690
691         qla_nvme_fc_transport.max_hw_queues =
692             min((uint8_t)(qla_nvme_fc_transport.max_hw_queues),
693                 (uint8_t)(ha->max_req_queues - 2));
694
695         pinfo.node_name = wwn_to_u64(vha->node_name);
696         pinfo.port_name = wwn_to_u64(vha->port_name);
697         pinfo.port_role = FC_PORT_ROLE_NVME_INITIATOR;
698         pinfo.port_id = vha->d_id.b24;
699
700         ql_log(ql_log_info, vha, 0xffff,
701             "register_localport: host-traddr=nn-0x%llx:pn-0x%llx on portID:%x\n",
702             pinfo.node_name, pinfo.port_name, pinfo.port_id);
703         qla_nvme_fc_transport.dma_boundary = vha->host->dma_boundary;
704
705         ret = nvme_fc_register_localport(&pinfo, tmpl,
706             get_device(&ha->pdev->dev), &vha->nvme_local_port);
707         if (ret) {
708                 ql_log(ql_log_warn, vha, 0xffff,
709                     "register_localport failed: ret=%x\n", ret);
710         } else {
711                 vha->nvme_local_port->private = vha;
712         }
713
714         return ret;
715 }