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