2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2017 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
8 #include <linux/scatterlist.h>
9 #include <linux/delay.h>
10 #include <linux/nvme.h>
11 #include <linux/nvme-fc.h>
13 static struct nvme_fc_port_template qla_nvme_fc_transport;
15 int qla_nvme_register_remote(struct scsi_qla_host *vha, struct fc_port *fcport)
17 struct qla_nvme_rport *rport;
18 struct nvme_fc_port_info req;
21 if (!IS_ENABLED(CONFIG_NVME_FC))
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",
31 if (!vha->nvme_local_port && qla_nvme_register_hba(vha))
34 if (!(fcport->nvme_prli_service_param &
35 (NVME_PRLI_SP_TARGET | NVME_PRLI_SP_DISCOVERY)) ||
36 (fcport->nvme_flag & NVME_FLAG_REGISTERED))
39 if (atomic_read(&fcport->state) == FCS_ONLINE)
42 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
44 fcport->nvme_flag &= ~NVME_FLAG_RESETTING;
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);
50 req.dev_loss_tmo = NVME_FC_DEV_LOSS_TMO;
52 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_INITIATOR)
53 req.port_role = FC_PORT_ROLE_NVME_INITIATOR;
55 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_TARGET)
56 req.port_role |= FC_PORT_ROLE_NVME_TARGET;
58 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_DISCOVERY)
59 req.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
61 req.port_id = fcport->d_id.b24;
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,
68 ret = nvme_fc_register_remoteport(vha->nvme_local_port, &req,
69 &fcport->nvme_remote_port);
71 ql_log(ql_log_warn, vha, 0x212e,
72 "Failed to register remote port. Transport returned %d\n",
77 rport = fcport->nvme_remote_port->private;
78 rport->fcport = fcport;
80 fcport->nvme_flag |= NVME_FLAG_REGISTERED;
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)
88 struct scsi_qla_host *vha;
89 struct qla_hw_data *ha;
90 struct qla_qpair *qpair;
92 /* Map admin queue and 1st IO queue to index 0 */
96 vha = (struct scsi_qla_host *)lport->private;
99 ql_log(ql_log_info, vha, 0x2104,
100 "%s: handle %p, idx =%d, qsize %d\n",
101 __func__, handle, qidx, qsize);
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);
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",
118 qpair = qla2xxx_create_qpair(vha, 5, vha->vp_idx, true);
120 ql_log(ql_log_warn, vha, 0x2122,
121 "Failed to allocate qpair\n");
129 static void qla_nvme_release_fcp_cmd_kref(struct kref *kref)
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;
140 nvme = &sp->u.iocb_cmd;
141 fd = nvme->u.nvme.desc;
143 spin_lock_irqsave(&priv->cmd_lock, flags);
146 if (priv->comp_status == QLA_SUCCESS) {
147 fd->rcv_rsplen = nvme->u.nvme.rsp_pyld_len;
150 fd->transferred_length = 0;
153 spin_unlock_irqrestore(&priv->cmd_lock, flags);
157 qla2xxx_rel_qpair_sp(sp->qpair, sp);
160 static void qla_nvme_ls_unmap(struct srb *sp, struct nvmefc_ls_req *fd)
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;
166 dma_unmap_single(&ha->pdev->dev, nvme->u.nvme.cmd_dma,
167 fd->rqstlen, DMA_TO_DEVICE);
168 sp->flags &= ~SRB_DMA_VALID;
172 static void qla_nvme_release_ls_cmd_kref(struct kref *kref)
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;
182 spin_lock_irqsave(&priv->cmd_lock, flags);
185 spin_unlock_irqrestore(&priv->cmd_lock, flags);
189 qla_nvme_ls_unmap(sp, fd);
190 fd->done(fd, priv->comp_status);
195 static void qla_nvme_ls_complete(struct work_struct *work)
197 struct nvme_private *priv =
198 container_of(work, struct nvme_private, ls_work);
200 kref_put(&priv->sp->cmd_kref, qla_nvme_release_ls_cmd_kref);
203 static void qla_nvme_sp_ls_done(srb_t *sp, int res)
205 struct nvme_private *priv = sp->priv;
207 if (WARN_ON_ONCE(kref_read(&sp->cmd_kref) == 0))
213 priv->comp_status = res;
214 INIT_WORK(&priv->ls_work, qla_nvme_ls_complete);
215 schedule_work(&priv->ls_work);
218 /* it assumed that QPair lock is held. */
219 static void qla_nvme_sp_done(srb_t *sp, int res)
221 struct nvme_private *priv = sp->priv;
223 priv->comp_status = res;
224 kref_put(&sp->cmd_kref, qla_nvme_release_fcp_cmd_kref);
229 static void qla_nvme_abort_work(struct work_struct *work)
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;
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);
242 if (!ha->flags.fw_started && fcport->deleted)
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);
253 rval = ha->isp_ops->abort_command(sp);
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);
261 /* kref_get was done before work was schedule. */
262 kref_put(&sp->cmd_kref, sp->put_fn);
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)
268 struct nvme_private *priv = fd->private;
271 spin_lock_irqsave(&priv->cmd_lock, flags);
273 spin_unlock_irqrestore(&priv->cmd_lock, flags);
277 if (!kref_get_unless_zero(&priv->sp->cmd_kref)) {
278 spin_unlock_irqrestore(&priv->cmd_lock, flags);
281 spin_unlock_irqrestore(&priv->cmd_lock, flags);
283 INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
284 schedule_work(&priv->abort_work);
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)
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;
300 if (!fcport || (fcport && fcport->deleted))
306 if (!ha->flags.fw_started)
309 /* Alloc SRB structure */
310 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
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;
320 kref_init(&sp->cmd_kref);
321 spin_lock_init(&priv->cmd_lock);
322 nvme = &sp->u.iocb_cmd;
324 nvme->u.nvme.desc = fd;
325 nvme->u.nvme.dir = 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);
336 sp->flags |= SRB_DMA_VALID;
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);
345 qla_nvme_ls_unmap(sp, fd);
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)
357 struct nvme_private *priv = fd->private;
360 spin_lock_irqsave(&priv->cmd_lock, flags);
362 spin_unlock_irqrestore(&priv->cmd_lock, flags);
365 if (!kref_get_unless_zero(&priv->sp->cmd_kref)) {
366 spin_unlock_irqrestore(&priv->cmd_lock, flags);
369 spin_unlock_irqrestore(&priv->cmd_lock, flags);
371 INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
372 schedule_work(&priv->abort_work);
375 static inline int qla2x00_start_nvme_mq(srb_t *sp)
380 struct cmd_nvme *cmd_pkt;
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;
395 /* Setup qpair pointers */
397 tot_dsds = fd->sg_cnt;
399 /* Acquire qpair specific lock */
400 spin_lock_irqsave(&qpair->qp_lock, flags);
402 handle = qla2xxx_get_next_handle(req);
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);
412 if (req->ring_index < cnt)
413 req->cnt = cnt - req->ring_index;
415 req->cnt = req->length - (req->ring_index - cnt);
417 if (req->cnt < (req_cnt + 2)){
423 if (unlikely(!fd->sqid)) {
424 struct nvme_fc_cmd_iu *cmd = fd->cmdaddr;
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);
432 /* Build command packet. */
433 req->current_outstanding_cmd = handle;
434 req->outstanding_cmds[handle] = sp;
438 cmd_pkt = (struct cmd_nvme *)req->ring_ptr;
439 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
441 /* Zero out remaining portion of packet. */
442 clr_ptr = (uint32_t *)cmd_pkt + 2;
443 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
445 cmd_pkt->entry_status = 0;
447 /* Update entry type to indicate Command NVME IOCB */
448 cmd_pkt->entry_type = COMMAND_NVME;
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;
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;
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;
480 cmd_pkt->nvme_rsp_dsd_len = cpu_to_le16(fd->rsplen);
481 put_unaligned_le64(fd->rspdma, &cmd_pkt->nvme_rsp_dseg_address);
484 cmd_pkt->nvme_cmnd_dseg_len = cpu_to_le16(fd->cmdlen);
485 cmd_pkt->nvme_cmnd_dseg_address = cpu_to_le64(fd->cmddma);
487 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
488 cmd_pkt->byte_count = cpu_to_le32(fd->payload_length);
490 /* One DSD is available in the Command Type NVME IOCB */
492 cur_dsd = &cmd_pkt->nvme_dsd;
495 /* Load data segments */
496 for_each_sg(sgl, sg, tot_dsds, i) {
497 cont_a64_entry_t *cont_pkt;
499 /* Allocate additional continuation packets? */
500 if (avail_dsds == 0) {
502 * Five DSDs are available in the Continuation
506 /* Adjust ring index */
508 if (req->ring_index == req->length) {
510 req->ring_ptr = req->ring;
514 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
515 put_unaligned_le32(CONTINUE_A64_TYPE,
516 &cont_pkt->entry_type);
518 cur_dsd = cont_pkt->dsd;
519 avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
522 append_dsd64(&cur_dsd, sg);
526 /* Set total entry count. */
527 cmd_pkt->entry_count = (uint8_t)req_cnt;
530 /* Adjust ring index. */
532 if (req->ring_index == req->length) {
534 req->ring_ptr = req->ring;
539 /* Set chip new ring index. */
540 WRT_REG_DWORD(req->req_q_in, req->ring_index);
543 spin_unlock_irqrestore(&qpair->qp_lock, flags);
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)
553 struct srb_iocb *nvme;
554 struct scsi_qla_host *vha;
557 struct qla_qpair *qpair = hw_queue_handle;
558 struct nvme_private *priv = fd->private;
559 struct qla_nvme_rport *qla_rport = rport->private;
562 /* nvme association has been torn down */
566 fcport = qla_rport->fcport;
568 if (!qpair || !fcport || (qpair && !qpair->fw_started) ||
569 (fcport && fcport->deleted))
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.
580 if (fcport->nvme_flag & NVME_FLAG_RESETTING)
583 /* Alloc SRB structure */
584 sp = qla2xxx_get_qpair_sp(vha, qpair, fcport, GFP_ATOMIC);
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;
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;
599 nvme = &sp->u.iocb_cmd;
600 nvme->u.nvme.desc = fd;
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);
609 qla2xxx_rel_qpair_sp(sp->qpair, sp);
615 static void qla_nvme_localport_delete(struct nvme_fc_local_port *lport)
617 struct scsi_qla_host *vha = lport->private;
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);
625 static void qla_nvme_remoteport_delete(struct nvme_fc_remote_port *rport)
628 struct qla_nvme_rport *qla_rport = rport->private;
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);
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,
650 .max_sgl_segments = 1024,
651 .max_dif_sgl_segments = 64,
652 .dma_boundary = 0xFFFFFFFF,
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),
659 void qla_nvme_unregister_remote_port(struct fc_port *fcport)
663 if (!IS_ENABLED(CONFIG_NVME_FC))
666 ql_log(ql_log_warn, NULL, 0x2112,
667 "%s: unregister remoteport on %p %8phN\n",
668 __func__, fcport, fcport->port_name);
670 if (test_bit(PFLG_DRIVER_REMOVING, &fcport->vha->pci_flags))
671 nvme_fc_set_remoteport_devloss(fcport->nvme_remote_port, 0);
673 init_completion(&fcport->nvme_del_done);
674 ret = nvme_fc_unregister_remoteport(fcport->nvme_remote_port);
676 ql_log(ql_log_info, fcport->vha, 0x2114,
677 "%s: Failed to unregister nvme_remote_port (%d)\n",
679 wait_for_completion(&fcport->nvme_del_done);
682 void qla_nvme_delete(struct scsi_qla_host *vha)
686 if (!IS_ENABLED(CONFIG_NVME_FC))
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);
696 ql_log(ql_log_info, vha, 0x2115,
697 "Unregister of localport failed\n");
699 wait_for_completion(&vha->nvme_del_done);
703 int qla_nvme_register_hba(struct scsi_qla_host *vha)
705 struct nvme_fc_port_template *tmpl;
706 struct qla_hw_data *ha;
707 struct nvme_fc_port_info pinfo;
710 if (!IS_ENABLED(CONFIG_NVME_FC))
714 tmpl = &qla_nvme_fc_transport;
716 WARN_ON(vha->nvme_local_port);
717 WARN_ON(ha->max_req_queues < 3);
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));
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;
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;
733 ret = nvme_fc_register_localport(&pinfo, tmpl,
734 get_device(&ha->pdev->dev), &vha->nvme_local_port);
736 ql_log(ql_log_warn, vha, 0xffff,
737 "register_localport failed: ret=%x\n", ret);
739 vha->nvme_local_port->private = vha;