bba25c38a118912b2b0af7433f87993552ddc534
[linux-2.6-block.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 = NVME_FC_DEV_LOSS_TMO;
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         rport = fcport->nvme_remote_port->private;
73         rport->fcport = fcport;
74
75         fcport->nvme_flag |= NVME_FLAG_REGISTERED;
76         return 0;
77 }
78
79 /* Allocate a queue for NVMe traffic */
80 static int qla_nvme_alloc_queue(struct nvme_fc_local_port *lport,
81     unsigned int qidx, u16 qsize, void **handle)
82 {
83         struct scsi_qla_host *vha;
84         struct qla_hw_data *ha;
85         struct qla_qpair *qpair;
86
87         if (!qidx)
88                 qidx++;
89
90         vha = (struct scsi_qla_host *)lport->private;
91         ha = vha->hw;
92
93         ql_log(ql_log_info, vha, 0x2104,
94             "%s: handle %p, idx =%d, qsize %d\n",
95             __func__, handle, qidx, qsize);
96
97         if (qidx > qla_nvme_fc_transport.max_hw_queues) {
98                 ql_log(ql_log_warn, vha, 0x212f,
99                     "%s: Illegal qidx=%d. Max=%d\n",
100                     __func__, qidx, qla_nvme_fc_transport.max_hw_queues);
101                 return -EINVAL;
102         }
103
104         if (ha->queue_pair_map[qidx]) {
105                 *handle = ha->queue_pair_map[qidx];
106                 ql_log(ql_log_info, vha, 0x2121,
107                     "Returning existing qpair of %p for idx=%x\n",
108                     *handle, qidx);
109                 return 0;
110         }
111
112         qpair = qla2xxx_create_qpair(vha, 5, vha->vp_idx, true);
113         if (qpair == NULL) {
114                 ql_log(ql_log_warn, vha, 0x2122,
115                     "Failed to allocate qpair\n");
116                 return -EINVAL;
117         }
118         *handle = qpair;
119
120         return 0;
121 }
122
123 static void qla_nvme_release_fcp_cmd_kref(struct kref *kref)
124 {
125         struct srb *sp = container_of(kref, struct srb, cmd_kref);
126         struct nvme_private *priv = (struct nvme_private *)sp->priv;
127         struct nvmefc_fcp_req *fd;
128         struct srb_iocb *nvme;
129         unsigned long flags;
130
131         if (!priv)
132                 goto out;
133
134         nvme = &sp->u.iocb_cmd;
135         fd = nvme->u.nvme.desc;
136
137         spin_lock_irqsave(&priv->cmd_lock, flags);
138         priv->sp = NULL;
139         sp->priv = NULL;
140         if (priv->comp_status == QLA_SUCCESS) {
141                 fd->rcv_rsplen = nvme->u.nvme.rsp_pyld_len;
142         } else {
143                 fd->rcv_rsplen = 0;
144                 fd->transferred_length = 0;
145         }
146         fd->status = 0;
147         spin_unlock_irqrestore(&priv->cmd_lock, flags);
148
149         fd->done(fd);
150 out:
151         qla2xxx_rel_qpair_sp(sp->qpair, sp);
152 }
153
154 static void qla_nvme_release_ls_cmd_kref(struct kref *kref)
155 {
156         struct srb *sp = container_of(kref, struct srb, cmd_kref);
157         struct nvme_private *priv = (struct nvme_private *)sp->priv;
158         struct nvmefc_ls_req *fd;
159         unsigned long flags;
160
161         if (!priv)
162                 goto out;
163
164         spin_lock_irqsave(&priv->cmd_lock, flags);
165         priv->sp = NULL;
166         sp->priv = NULL;
167         spin_unlock_irqrestore(&priv->cmd_lock, flags);
168
169         fd = priv->fd;
170         fd->done(fd, priv->comp_status);
171 out:
172         qla2x00_rel_sp(sp);
173 }
174
175 static void qla_nvme_ls_complete(struct work_struct *work)
176 {
177         struct nvme_private *priv =
178                 container_of(work, struct nvme_private, ls_work);
179
180         kref_put(&priv->sp->cmd_kref, qla_nvme_release_ls_cmd_kref);
181 }
182
183 static void qla_nvme_sp_ls_done(void *ptr, int res)
184 {
185         srb_t *sp = ptr;
186         struct nvme_private *priv;
187
188         if (WARN_ON_ONCE(kref_read(&sp->cmd_kref) == 0))
189                 return;
190
191         if (res)
192                 res = -EINVAL;
193
194         priv = (struct nvme_private *)sp->priv;
195         priv->comp_status = res;
196         INIT_WORK(&priv->ls_work, qla_nvme_ls_complete);
197         schedule_work(&priv->ls_work);
198 }
199
200 /* it assumed that QPair lock is held. */
201 static void qla_nvme_sp_done(void *ptr, int res)
202 {
203         srb_t *sp = ptr;
204         struct nvme_private *priv = (struct nvme_private *)sp->priv;
205
206         priv->comp_status = res;
207         kref_put(&sp->cmd_kref, qla_nvme_release_fcp_cmd_kref);
208
209         return;
210 }
211
212 static void qla_nvme_abort_work(struct work_struct *work)
213 {
214         struct nvme_private *priv =
215                 container_of(work, struct nvme_private, abort_work);
216         srb_t *sp = priv->sp;
217         fc_port_t *fcport = sp->fcport;
218         struct qla_hw_data *ha = fcport->vha->hw;
219         int rval;
220
221         ql_dbg(ql_dbg_io, fcport->vha, 0xffff,
222                "%s called for sp=%p, hndl=%x on fcport=%p deleted=%d\n",
223                __func__, sp, sp->handle, fcport, fcport->deleted);
224
225         if (!ha->flags.fw_started && fcport->deleted)
226                 goto out;
227
228         if (ha->flags.host_shutting_down) {
229                 ql_log(ql_log_info, sp->fcport->vha, 0xffff,
230                     "%s Calling done on sp: %p, type: 0x%x, sp->ref_count: 0x%x\n",
231                     __func__, sp, sp->type, atomic_read(&sp->ref_count));
232                 sp->done(sp, 0);
233                 goto out;
234         }
235
236         rval = ha->isp_ops->abort_command(sp);
237
238         ql_dbg(ql_dbg_io, fcport->vha, 0x212b,
239             "%s: %s command for sp=%p, handle=%x on fcport=%p rval=%x\n",
240             __func__, (rval != QLA_SUCCESS) ? "Failed to abort" : "Aborted",
241             sp, sp->handle, fcport, rval);
242
243 out:
244         /* kref_get was done before work was schedule. */
245         kref_put(&sp->cmd_kref, sp->put_fn);
246 }
247
248 static void qla_nvme_ls_abort(struct nvme_fc_local_port *lport,
249     struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
250 {
251         struct nvme_private *priv = fd->private;
252         unsigned long flags;
253
254         spin_lock_irqsave(&priv->cmd_lock, flags);
255         if (!priv->sp) {
256                 spin_unlock_irqrestore(&priv->cmd_lock, flags);
257                 return;
258         }
259
260         if (!kref_get_unless_zero(&priv->sp->cmd_kref)) {
261                 spin_unlock_irqrestore(&priv->cmd_lock, flags);
262                 return;
263         }
264         spin_unlock_irqrestore(&priv->cmd_lock, flags);
265
266         INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
267         schedule_work(&priv->abort_work);
268 }
269
270 static int qla_nvme_ls_req(struct nvme_fc_local_port *lport,
271     struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
272 {
273         struct qla_nvme_rport *qla_rport = rport->private;
274         fc_port_t *fcport = qla_rport->fcport;
275         struct srb_iocb   *nvme;
276         struct nvme_private *priv = fd->private;
277         struct scsi_qla_host *vha;
278         int     rval = QLA_FUNCTION_FAILED;
279         struct qla_hw_data *ha;
280         srb_t           *sp;
281
282
283         if (!fcport || (fcport && fcport->deleted))
284                 return rval;
285
286         vha = fcport->vha;
287         ha = vha->hw;
288
289         if (!ha->flags.fw_started)
290                 return rval;
291
292         /* Alloc SRB structure */
293         sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
294         if (!sp)
295                 return rval;
296
297         sp->type = SRB_NVME_LS;
298         sp->name = "nvme_ls";
299         sp->done = qla_nvme_sp_ls_done;
300         sp->put_fn = qla_nvme_release_ls_cmd_kref;
301         sp->priv = (void *)priv;
302         priv->sp = sp;
303         kref_init(&sp->cmd_kref);
304         spin_lock_init(&priv->cmd_lock);
305         nvme = &sp->u.iocb_cmd;
306         priv->fd = fd;
307         nvme->u.nvme.desc = fd;
308         nvme->u.nvme.dir = 0;
309         nvme->u.nvme.dl = 0;
310         nvme->u.nvme.cmd_len = fd->rqstlen;
311         nvme->u.nvme.rsp_len = fd->rsplen;
312         nvme->u.nvme.rsp_dma = fd->rspdma;
313         nvme->u.nvme.timeout_sec = fd->timeout;
314         nvme->u.nvme.cmd_dma = dma_map_single(&ha->pdev->dev, fd->rqstaddr,
315             fd->rqstlen, DMA_TO_DEVICE);
316         dma_sync_single_for_device(&ha->pdev->dev, nvme->u.nvme.cmd_dma,
317             fd->rqstlen, DMA_TO_DEVICE);
318
319         rval = qla2x00_start_sp(sp);
320         if (rval != QLA_SUCCESS) {
321                 ql_log(ql_log_warn, vha, 0x700e,
322                     "qla2x00_start_sp failed = %d\n", rval);
323                 wake_up(&sp->nvme_ls_waitq);
324                 sp->priv = NULL;
325                 priv->sp = NULL;
326                 qla2x00_rel_sp(sp);
327                 return rval;
328         }
329
330         return rval;
331 }
332
333 static void qla_nvme_fcp_abort(struct nvme_fc_local_port *lport,
334     struct nvme_fc_remote_port *rport, void *hw_queue_handle,
335     struct nvmefc_fcp_req *fd)
336 {
337         struct nvme_private *priv = fd->private;
338         unsigned long flags;
339
340         spin_lock_irqsave(&priv->cmd_lock, flags);
341         if (!priv->sp) {
342                 spin_unlock_irqrestore(&priv->cmd_lock, flags);
343                 return;
344         }
345         if (!kref_get_unless_zero(&priv->sp->cmd_kref)) {
346                 spin_unlock_irqrestore(&priv->cmd_lock, flags);
347                 return;
348         }
349         spin_unlock_irqrestore(&priv->cmd_lock, flags);
350
351         INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
352         schedule_work(&priv->abort_work);
353 }
354
355 static inline int qla2x00_start_nvme_mq(srb_t *sp)
356 {
357         unsigned long   flags;
358         uint32_t        *clr_ptr;
359         uint32_t        index;
360         uint32_t        handle;
361         struct cmd_nvme *cmd_pkt;
362         uint16_t        cnt, i;
363         uint16_t        req_cnt;
364         uint16_t        tot_dsds;
365         uint16_t        avail_dsds;
366         struct dsd64    *cur_dsd;
367         struct req_que *req = NULL;
368         struct scsi_qla_host *vha = sp->fcport->vha;
369         struct qla_hw_data *ha = vha->hw;
370         struct qla_qpair *qpair = sp->qpair;
371         struct srb_iocb *nvme = &sp->u.iocb_cmd;
372         struct scatterlist *sgl, *sg;
373         struct nvmefc_fcp_req *fd = nvme->u.nvme.desc;
374         uint32_t        rval = QLA_SUCCESS;
375
376         /* Setup qpair pointers */
377         req = qpair->req;
378         tot_dsds = fd->sg_cnt;
379
380         /* Acquire qpair specific lock */
381         spin_lock_irqsave(&qpair->qp_lock, flags);
382
383         /* Check for room in outstanding command list. */
384         handle = req->current_outstanding_cmd;
385         for (index = 1; index < req->num_outstanding_cmds; index++) {
386                 handle++;
387                 if (handle == req->num_outstanding_cmds)
388                         handle = 1;
389                 if (!req->outstanding_cmds[handle])
390                         break;
391         }
392
393         if (index == req->num_outstanding_cmds) {
394                 rval = -EBUSY;
395                 goto queuing_error;
396         }
397         req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
398         if (req->cnt < (req_cnt + 2)) {
399                 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
400                     RD_REG_DWORD_RELAXED(req->req_q_out);
401
402                 if (req->ring_index < cnt)
403                         req->cnt = cnt - req->ring_index;
404                 else
405                         req->cnt = req->length - (req->ring_index - cnt);
406
407                 if (req->cnt < (req_cnt + 2)){
408                         rval = -EBUSY;
409                         goto queuing_error;
410                 }
411         }
412
413         if (unlikely(!fd->sqid)) {
414                 struct nvme_fc_cmd_iu *cmd = fd->cmdaddr;
415
416                 if (cmd->sqe.common.opcode == nvme_admin_async_event) {
417                         nvme->u.nvme.aen_op = 1;
418                         atomic_inc(&ha->nvme_active_aen_cnt);
419                 }
420         }
421
422         /* Build command packet. */
423         req->current_outstanding_cmd = handle;
424         req->outstanding_cmds[handle] = sp;
425         sp->handle = handle;
426         req->cnt -= req_cnt;
427
428         cmd_pkt = (struct cmd_nvme *)req->ring_ptr;
429         cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
430
431         /* Zero out remaining portion of packet. */
432         clr_ptr = (uint32_t *)cmd_pkt + 2;
433         memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
434
435         cmd_pkt->entry_status = 0;
436
437         /* Update entry type to indicate Command NVME IOCB */
438         cmd_pkt->entry_type = COMMAND_NVME;
439
440         /* No data transfer how do we check buffer len == 0?? */
441         if (fd->io_dir == NVMEFC_FCP_READ) {
442                 cmd_pkt->control_flags = CF_READ_DATA;
443                 vha->qla_stats.input_bytes += fd->payload_length;
444                 vha->qla_stats.input_requests++;
445         } else if (fd->io_dir == NVMEFC_FCP_WRITE) {
446                 cmd_pkt->control_flags = CF_WRITE_DATA;
447                 if ((vha->flags.nvme_first_burst) &&
448                     (sp->fcport->nvme_prli_service_param &
449                         NVME_PRLI_SP_FIRST_BURST)) {
450                         if ((fd->payload_length <=
451                             sp->fcport->nvme_first_burst_size) ||
452                                 (sp->fcport->nvme_first_burst_size == 0))
453                                 cmd_pkt->control_flags |=
454                                     CF_NVME_FIRST_BURST_ENABLE;
455                 }
456                 vha->qla_stats.output_bytes += fd->payload_length;
457                 vha->qla_stats.output_requests++;
458         } else if (fd->io_dir == 0) {
459                 cmd_pkt->control_flags = 0;
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         fcport = qla_rport->fcport;
552
553         if (!qpair || !fcport || (qpair && !qpair->fw_started) ||
554             (fcport && fcport->deleted))
555                 return rval;
556
557         vha = fcport->vha;
558         /*
559          * If we know the dev is going away while the transport is still sending
560          * IO's return busy back to stall the IO Q.  This happens when the
561          * link goes away and fw hasn't notified us yet, but IO's are being
562          * returned. If the dev comes back quickly we won't exhaust the IO
563          * retry count at the core.
564          */
565         if (fcport->nvme_flag & NVME_FLAG_RESETTING)
566                 return -EBUSY;
567
568         /* Alloc SRB structure */
569         sp = qla2xxx_get_qpair_sp(vha, qpair, fcport, GFP_ATOMIC);
570         if (!sp)
571                 return -EBUSY;
572
573         init_waitqueue_head(&sp->nvme_ls_waitq);
574         kref_init(&sp->cmd_kref);
575         spin_lock_init(&priv->cmd_lock);
576         sp->priv = (void *)priv;
577         priv->sp = sp;
578         sp->type = SRB_NVME_CMD;
579         sp->name = "nvme_cmd";
580         sp->done = qla_nvme_sp_done;
581         sp->put_fn = qla_nvme_release_fcp_cmd_kref;
582         sp->qpair = qpair;
583         sp->vha = vha;
584         nvme = &sp->u.iocb_cmd;
585         nvme->u.nvme.desc = fd;
586
587         rval = qla2x00_start_nvme_mq(sp);
588         if (rval != QLA_SUCCESS) {
589                 ql_log(ql_log_warn, vha, 0x212d,
590                     "qla2x00_start_nvme_mq failed = %d\n", rval);
591                 wake_up(&sp->nvme_ls_waitq);
592                 sp->priv = NULL;
593                 priv->sp = NULL;
594                 qla2xxx_rel_qpair_sp(sp->qpair, sp);
595         }
596
597         return rval;
598 }
599
600 static void qla_nvme_localport_delete(struct nvme_fc_local_port *lport)
601 {
602         struct scsi_qla_host *vha = lport->private;
603
604         ql_log(ql_log_info, vha, 0x210f,
605             "localport delete of %p completed.\n", vha->nvme_local_port);
606         vha->nvme_local_port = NULL;
607         complete(&vha->nvme_del_done);
608 }
609
610 static void qla_nvme_remoteport_delete(struct nvme_fc_remote_port *rport)
611 {
612         fc_port_t *fcport;
613         struct qla_nvme_rport *qla_rport = rport->private;
614
615         fcport = qla_rport->fcport;
616         fcport->nvme_remote_port = NULL;
617         fcport->nvme_flag &= ~NVME_FLAG_REGISTERED;
618         fcport->nvme_flag &= ~NVME_FLAG_DELETING;
619         ql_log(ql_log_info, fcport->vha, 0x2110,
620             "remoteport_delete of %p %8phN completed.\n",
621             fcport, fcport->port_name);
622         complete(&fcport->nvme_del_done);
623 }
624
625 static struct nvme_fc_port_template qla_nvme_fc_transport = {
626         .localport_delete = qla_nvme_localport_delete,
627         .remoteport_delete = qla_nvme_remoteport_delete,
628         .create_queue   = qla_nvme_alloc_queue,
629         .delete_queue   = NULL,
630         .ls_req         = qla_nvme_ls_req,
631         .ls_abort       = qla_nvme_ls_abort,
632         .fcp_io         = qla_nvme_post_cmd,
633         .fcp_abort      = qla_nvme_fcp_abort,
634         .max_hw_queues  = 8,
635         .max_sgl_segments = 1024,
636         .max_dif_sgl_segments = 64,
637         .dma_boundary = 0xFFFFFFFF,
638         .local_priv_sz  = 8,
639         .remote_priv_sz = sizeof(struct qla_nvme_rport),
640         .lsrqst_priv_sz = sizeof(struct nvme_private),
641         .fcprqst_priv_sz = sizeof(struct nvme_private),
642 };
643
644 void qla_nvme_unregister_remote_port(struct fc_port *fcport)
645 {
646         int ret;
647
648         if (!IS_ENABLED(CONFIG_NVME_FC))
649                 return;
650
651         ql_log(ql_log_warn, NULL, 0x2112,
652             "%s: unregister remoteport on %p %8phN\n",
653             __func__, fcport, fcport->port_name);
654
655         if (test_bit(PFLG_DRIVER_REMOVING, &fcport->vha->pci_flags))
656                 nvme_fc_set_remoteport_devloss(fcport->nvme_remote_port, 0);
657
658         init_completion(&fcport->nvme_del_done);
659         ret = nvme_fc_unregister_remoteport(fcport->nvme_remote_port);
660         if (ret)
661                 ql_log(ql_log_info, fcport->vha, 0x2114,
662                         "%s: Failed to unregister nvme_remote_port (%d)\n",
663                             __func__, ret);
664         wait_for_completion(&fcport->nvme_del_done);
665 }
666
667 void qla_nvme_delete(struct scsi_qla_host *vha)
668 {
669         int nv_ret;
670
671         if (!IS_ENABLED(CONFIG_NVME_FC))
672                 return;
673
674         if (vha->nvme_local_port) {
675                 init_completion(&vha->nvme_del_done);
676                 ql_log(ql_log_info, vha, 0x2116,
677                         "unregister localport=%p\n",
678                         vha->nvme_local_port);
679                 nv_ret = nvme_fc_unregister_localport(vha->nvme_local_port);
680                 if (nv_ret)
681                         ql_log(ql_log_info, vha, 0x2115,
682                             "Unregister of localport failed\n");
683                 else
684                         wait_for_completion(&vha->nvme_del_done);
685         }
686 }
687
688 int qla_nvme_register_hba(struct scsi_qla_host *vha)
689 {
690         struct nvme_fc_port_template *tmpl;
691         struct qla_hw_data *ha;
692         struct nvme_fc_port_info pinfo;
693         int ret = EINVAL;
694
695         if (!IS_ENABLED(CONFIG_NVME_FC))
696                 return ret;
697
698         ha = vha->hw;
699         tmpl = &qla_nvme_fc_transport;
700
701         WARN_ON(vha->nvme_local_port);
702         WARN_ON(ha->max_req_queues < 3);
703
704         qla_nvme_fc_transport.max_hw_queues =
705             min((uint8_t)(qla_nvme_fc_transport.max_hw_queues),
706                 (uint8_t)(ha->max_req_queues - 2));
707
708         pinfo.node_name = wwn_to_u64(vha->node_name);
709         pinfo.port_name = wwn_to_u64(vha->port_name);
710         pinfo.port_role = FC_PORT_ROLE_NVME_INITIATOR;
711         pinfo.port_id = vha->d_id.b24;
712
713         ql_log(ql_log_info, vha, 0xffff,
714             "register_localport: host-traddr=nn-0x%llx:pn-0x%llx on portID:%x\n",
715             pinfo.node_name, pinfo.port_name, pinfo.port_id);
716         qla_nvme_fc_transport.dma_boundary = vha->host->dma_boundary;
717
718         ret = nvme_fc_register_localport(&pinfo, tmpl,
719             get_device(&ha->pdev->dev), &vha->nvme_local_port);
720         if (ret) {
721                 ql_log(ql_log_warn, vha, 0xffff,
722                     "register_localport failed: ret=%x\n", ret);
723         } else {
724                 vha->nvme_local_port->private = vha;
725         }
726
727         return ret;
728 }