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