1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * QLogic Fibre Channel HBA Driver
4 * Copyright (c) 2003-2017 QLogic Corporation
5 */
6 #include "qla_nvme.h"
7 #include <linux/scatterlist.h>
8 #include <linux/delay.h>
9 #include <linux/nvme.h>
10 #include <linux/nvme-fc.h>
11
12 static struct nvme_fc_port_template qla_nvme_fc_transport;
13
qla_nvme_register_remote(struct scsi_qla_host * vha,struct fc_port * fcport)14 int qla_nvme_register_remote(struct scsi_qla_host *vha, struct fc_port *fcport)
15 {
16 struct qla_nvme_rport *rport;
17 struct nvme_fc_port_info req;
18 int ret;
19
20 if (!IS_ENABLED(CONFIG_NVME_FC))
21 return 0;
22
23 if (!vha->flags.nvme_enabled) {
24 ql_log(ql_log_info, vha, 0x2100,
25 "%s: Not registering target since Host NVME is not enabled\n",
26 __func__);
27 return 0;
28 }
29
30 if (!vha->nvme_local_port && qla_nvme_register_hba(vha))
31 return 0;
32
33 if (!(fcport->nvme_prli_service_param &
34 (NVME_PRLI_SP_TARGET | NVME_PRLI_SP_DISCOVERY)) ||
35 (fcport->nvme_flag & NVME_FLAG_REGISTERED))
36 return 0;
37
38 fcport->nvme_flag &= ~NVME_FLAG_RESETTING;
39
40 memset(&req, 0, sizeof(struct nvme_fc_port_info));
41 req.port_name = wwn_to_u64(fcport->port_name);
42 req.node_name = wwn_to_u64(fcport->node_name);
43 req.port_role = 0;
44 req.dev_loss_tmo = 0;
45
46 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_INITIATOR)
47 req.port_role = FC_PORT_ROLE_NVME_INITIATOR;
48
49 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_TARGET)
50 req.port_role |= FC_PORT_ROLE_NVME_TARGET;
51
52 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_DISCOVERY)
53 req.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
54
55 req.port_id = fcport->d_id.b24;
56
57 ql_log(ql_log_info, vha, 0x2102,
58 "%s: traddr=nn-0x%016llx:pn-0x%016llx PortID:%06x\n",
59 __func__, req.node_name, req.port_name,
60 req.port_id);
61
62 ret = nvme_fc_register_remoteport(vha->nvme_local_port, &req,
63 &fcport->nvme_remote_port);
64 if (ret) {
65 ql_log(ql_log_warn, vha, 0x212e,
66 "Failed to register remote port. Transport returned %d\n",
67 ret);
68 return ret;
69 }
70
71 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_SLER)
72 ql_log(ql_log_info, vha, 0x212a,
73 "PortID:%06x Supports SLER\n", req.port_id);
74
75 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_PI_CTRL)
76 ql_log(ql_log_info, vha, 0x212b,
77 "PortID:%06x Supports PI control\n", req.port_id);
78
79 rport = fcport->nvme_remote_port->private;
80 rport->fcport = fcport;
81
82 fcport->nvme_flag |= NVME_FLAG_REGISTERED;
83 return 0;
84 }
85
86 /* Allocate a queue for NVMe traffic */
qla_nvme_alloc_queue(struct nvme_fc_local_port * lport,unsigned int qidx,u16 qsize,void ** handle)87 static int qla_nvme_alloc_queue(struct nvme_fc_local_port *lport,
88 unsigned int qidx, u16 qsize, void **handle)
89 {
90 struct scsi_qla_host *vha;
91 struct qla_hw_data *ha;
92 struct qla_qpair *qpair;
93
94 /* Map admin queue and 1st IO queue to index 0 */
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 /* Use base qpair if max_qpairs is 0 */
113 if (!ha->max_qpairs) {
114 qpair = ha->base_qpair;
115 } else {
116 if (ha->queue_pair_map[qidx]) {
117 *handle = ha->queue_pair_map[qidx];
118 ql_log(ql_log_info, vha, 0x2121,
119 "Returning existing qpair of %p for idx=%x\n",
120 *handle, qidx);
121 return 0;
122 }
123
124 qpair = qla2xxx_create_qpair(vha, 5, vha->vp_idx, true);
125 if (!qpair) {
126 ql_log(ql_log_warn, vha, 0x2122,
127 "Failed to allocate qpair\n");
128 return -EINVAL;
129 }
130 qla_adjust_iocb_limit(vha);
131 }
132 *handle = qpair;
133
134 return 0;
135 }
136
qla_nvme_release_fcp_cmd_kref(struct kref * kref)137 static void qla_nvme_release_fcp_cmd_kref(struct kref *kref)
138 {
139 struct srb *sp = container_of(kref, struct srb, cmd_kref);
140 struct nvme_private *priv = (struct nvme_private *)sp->priv;
141 struct nvmefc_fcp_req *fd;
142 struct srb_iocb *nvme;
143 unsigned long flags;
144
145 if (!priv)
146 goto out;
147
148 nvme = &sp->u.iocb_cmd;
149 fd = nvme->u.nvme.desc;
150
151 spin_lock_irqsave(&priv->cmd_lock, flags);
152 priv->sp = NULL;
153 sp->priv = NULL;
154 if (priv->comp_status == QLA_SUCCESS) {
155 fd->rcv_rsplen = le16_to_cpu(nvme->u.nvme.rsp_pyld_len);
156 fd->status = NVME_SC_SUCCESS;
157 } else {
158 fd->rcv_rsplen = 0;
159 fd->transferred_length = 0;
160 fd->status = NVME_SC_INTERNAL;
161 }
162 spin_unlock_irqrestore(&priv->cmd_lock, flags);
163
164 fd->done(fd);
165 out:
166 qla2xxx_rel_qpair_sp(sp->qpair, sp);
167 }
168
qla_nvme_release_ls_cmd_kref(struct kref * kref)169 static void qla_nvme_release_ls_cmd_kref(struct kref *kref)
170 {
171 struct srb *sp = container_of(kref, struct srb, cmd_kref);
172 struct nvme_private *priv = (struct nvme_private *)sp->priv;
173 struct nvmefc_ls_req *fd;
174 unsigned long flags;
175
176 if (!priv)
177 goto out;
178
179 spin_lock_irqsave(&priv->cmd_lock, flags);
180 priv->sp = NULL;
181 sp->priv = NULL;
182 spin_unlock_irqrestore(&priv->cmd_lock, flags);
183
184 fd = priv->fd;
185
186 fd->done(fd, priv->comp_status);
187 out:
188 qla2x00_rel_sp(sp);
189 }
190
qla_nvme_ls_complete(struct work_struct * work)191 static void qla_nvme_ls_complete(struct work_struct *work)
192 {
193 struct nvme_private *priv =
194 container_of(work, struct nvme_private, ls_work);
195
196 kref_put(&priv->sp->cmd_kref, qla_nvme_release_ls_cmd_kref);
197 }
198
qla_nvme_sp_ls_done(srb_t * sp,int res)199 static void qla_nvme_sp_ls_done(srb_t *sp, int res)
200 {
201 struct nvme_private *priv = sp->priv;
202
203 if (WARN_ON_ONCE(kref_read(&sp->cmd_kref) == 0))
204 return;
205
206 if (res)
207 res = -EINVAL;
208
209 priv->comp_status = res;
210 INIT_WORK(&priv->ls_work, qla_nvme_ls_complete);
211 schedule_work(&priv->ls_work);
212 }
213
214 /* it assumed that QPair lock is held. */
qla_nvme_sp_done(srb_t * sp,int res)215 static void qla_nvme_sp_done(srb_t *sp, int res)
216 {
217 struct nvme_private *priv = sp->priv;
218
219 priv->comp_status = res;
220 kref_put(&sp->cmd_kref, qla_nvme_release_fcp_cmd_kref);
221
222 return;
223 }
224
qla_nvme_abort_work(struct work_struct * work)225 static void qla_nvme_abort_work(struct work_struct *work)
226 {
227 struct nvme_private *priv =
228 container_of(work, struct nvme_private, abort_work);
229 srb_t *sp = priv->sp;
230 fc_port_t *fcport = sp->fcport;
231 struct qla_hw_data *ha = fcport->vha->hw;
232 int rval, abts_done_called = 1;
233 bool io_wait_for_abort_done;
234 uint32_t handle;
235
236 ql_dbg(ql_dbg_io, fcport->vha, 0xffff,
237 "%s called for sp=%p, hndl=%x on fcport=%p desc=%p deleted=%d\n",
238 __func__, sp, sp->handle, fcport, sp->u.iocb_cmd.u.nvme.desc, fcport->deleted);
239
240 if (!ha->flags.fw_started || fcport->deleted == QLA_SESS_DELETED)
241 goto out;
242
243 if (ha->flags.host_shutting_down) {
244 ql_log(ql_log_info, sp->fcport->vha, 0xffff,
245 "%s Calling done on sp: %p, type: 0x%x\n",
246 __func__, sp, sp->type);
247 sp->done(sp, 0);
248 goto out;
249 }
250
251 /*
252 * sp may not be valid after abort_command if return code is either
253 * SUCCESS or ERR_FROM_FW codes, so cache the value here.
254 */
255 io_wait_for_abort_done = ql2xabts_wait_nvme &&
256 QLA_ABTS_WAIT_ENABLED(sp);
257 handle = sp->handle;
258
259 rval = ha->isp_ops->abort_command(sp);
260
261 ql_dbg(ql_dbg_io, fcport->vha, 0x212b,
262 "%s: %s command for sp=%p, handle=%x on fcport=%p rval=%x\n",
263 __func__, (rval != QLA_SUCCESS) ? "Failed to abort" : "Aborted",
264 sp, handle, fcport, rval);
265
266 /*
267 * If async tmf is enabled, the abort callback is called only on
268 * return codes QLA_SUCCESS and QLA_ERR_FROM_FW.
269 */
270 if (ql2xasynctmfenable &&
271 rval != QLA_SUCCESS && rval != QLA_ERR_FROM_FW)
272 abts_done_called = 0;
273
274 /*
275 * Returned before decreasing kref so that I/O requests
276 * are waited until ABTS complete. This kref is decreased
277 * at qla24xx_abort_sp_done function.
278 */
279 if (abts_done_called && io_wait_for_abort_done)
280 return;
281 out:
282 /* kref_get was done before work was schedule. */
283 kref_put(&sp->cmd_kref, sp->put_fn);
284 }
285
qla_nvme_ls_abort(struct nvme_fc_local_port * lport,struct nvme_fc_remote_port * rport,struct nvmefc_ls_req * fd)286 static void qla_nvme_ls_abort(struct nvme_fc_local_port *lport,
287 struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
288 {
289 struct nvme_private *priv = fd->private;
290 unsigned long flags;
291
292 spin_lock_irqsave(&priv->cmd_lock, flags);
293 if (!priv->sp) {
294 spin_unlock_irqrestore(&priv->cmd_lock, flags);
295 return;
296 }
297
298 if (!kref_get_unless_zero(&priv->sp->cmd_kref)) {
299 spin_unlock_irqrestore(&priv->cmd_lock, flags);
300 return;
301 }
302 spin_unlock_irqrestore(&priv->cmd_lock, flags);
303
304 INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
305 schedule_work(&priv->abort_work);
306 }
307
qla_nvme_ls_req(struct nvme_fc_local_port * lport,struct nvme_fc_remote_port * rport,struct nvmefc_ls_req * fd)308 static int qla_nvme_ls_req(struct nvme_fc_local_port *lport,
309 struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
310 {
311 struct qla_nvme_rport *qla_rport = rport->private;
312 fc_port_t *fcport = qla_rport->fcport;
313 struct srb_iocb *nvme;
314 struct nvme_private *priv = fd->private;
315 struct scsi_qla_host *vha;
316 int rval = QLA_FUNCTION_FAILED;
317 struct qla_hw_data *ha;
318 srb_t *sp;
319
320 if (!fcport || fcport->deleted)
321 return rval;
322
323 vha = fcport->vha;
324 ha = vha->hw;
325
326 if (!ha->flags.fw_started)
327 return rval;
328
329 /* Alloc SRB structure */
330 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
331 if (!sp)
332 return rval;
333
334 sp->type = SRB_NVME_LS;
335 sp->name = "nvme_ls";
336 sp->done = qla_nvme_sp_ls_done;
337 sp->put_fn = qla_nvme_release_ls_cmd_kref;
338 sp->priv = priv;
339 priv->sp = sp;
340 kref_init(&sp->cmd_kref);
341 spin_lock_init(&priv->cmd_lock);
342 nvme = &sp->u.iocb_cmd;
343 priv->fd = fd;
344 nvme->u.nvme.desc = fd;
345 nvme->u.nvme.dir = 0;
346 nvme->u.nvme.dl = 0;
347 nvme->u.nvme.cmd_len = fd->rqstlen;
348 nvme->u.nvme.rsp_len = fd->rsplen;
349 nvme->u.nvme.rsp_dma = fd->rspdma;
350 nvme->u.nvme.timeout_sec = fd->timeout;
351 nvme->u.nvme.cmd_dma = fd->rqstdma;
352 dma_sync_single_for_device(&ha->pdev->dev, nvme->u.nvme.cmd_dma,
353 fd->rqstlen, DMA_TO_DEVICE);
354
355 rval = qla2x00_start_sp(sp);
356 if (rval != QLA_SUCCESS) {
357 ql_log(ql_log_warn, vha, 0x700e,
358 "qla2x00_start_sp failed = %d\n", rval);
359 sp->priv = NULL;
360 priv->sp = NULL;
361 qla2x00_rel_sp(sp);
362 return rval;
363 }
364
365 return rval;
366 }
367
qla_nvme_fcp_abort(struct nvme_fc_local_port * lport,struct nvme_fc_remote_port * rport,void * hw_queue_handle,struct nvmefc_fcp_req * fd)368 static void qla_nvme_fcp_abort(struct nvme_fc_local_port *lport,
369 struct nvme_fc_remote_port *rport, void *hw_queue_handle,
370 struct nvmefc_fcp_req *fd)
371 {
372 struct nvme_private *priv = fd->private;
373 unsigned long flags;
374
375 spin_lock_irqsave(&priv->cmd_lock, flags);
376 if (!priv->sp) {
377 spin_unlock_irqrestore(&priv->cmd_lock, flags);
378 return;
379 }
380 if (!kref_get_unless_zero(&priv->sp->cmd_kref)) {
381 spin_unlock_irqrestore(&priv->cmd_lock, flags);
382 return;
383 }
384 spin_unlock_irqrestore(&priv->cmd_lock, flags);
385
386 INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
387 schedule_work(&priv->abort_work);
388 }
389
qla2x00_start_nvme_mq(srb_t * sp)390 static inline int qla2x00_start_nvme_mq(srb_t *sp)
391 {
392 unsigned long flags;
393 uint32_t *clr_ptr;
394 uint32_t handle;
395 struct cmd_nvme *cmd_pkt;
396 uint16_t cnt, i;
397 uint16_t req_cnt;
398 uint16_t tot_dsds;
399 uint16_t avail_dsds;
400 struct dsd64 *cur_dsd;
401 struct req_que *req = NULL;
402 struct scsi_qla_host *vha = sp->fcport->vha;
403 struct qla_hw_data *ha = vha->hw;
404 struct qla_qpair *qpair = sp->qpair;
405 struct srb_iocb *nvme = &sp->u.iocb_cmd;
406 struct scatterlist *sgl, *sg;
407 struct nvmefc_fcp_req *fd = nvme->u.nvme.desc;
408 struct nvme_fc_cmd_iu *cmd = fd->cmdaddr;
409 uint32_t rval = QLA_SUCCESS;
410
411 /* Setup qpair pointers */
412 req = qpair->req;
413 tot_dsds = fd->sg_cnt;
414
415 /* Acquire qpair specific lock */
416 spin_lock_irqsave(&qpair->qp_lock, flags);
417
418 handle = qla2xxx_get_next_handle(req);
419 if (handle == 0) {
420 rval = -EBUSY;
421 goto queuing_error;
422 }
423 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
424
425 sp->iores.res_type = RESOURCE_IOCB | RESOURCE_EXCH;
426 sp->iores.exch_cnt = 1;
427 sp->iores.iocb_cnt = req_cnt;
428 if (qla_get_fw_resources(sp->qpair, &sp->iores)) {
429 rval = -EBUSY;
430 goto queuing_error;
431 }
432
433 if (req->cnt < (req_cnt + 2)) {
434 if (IS_SHADOW_REG_CAPABLE(ha)) {
435 cnt = *req->out_ptr;
436 } else {
437 cnt = rd_reg_dword_relaxed(req->req_q_out);
438 if (qla2x00_check_reg16_for_disconnect(vha, cnt)) {
439 rval = -EBUSY;
440 goto queuing_error;
441 }
442 }
443
444 if (req->ring_index < cnt)
445 req->cnt = cnt - req->ring_index;
446 else
447 req->cnt = req->length - (req->ring_index - cnt);
448
449 if (req->cnt < (req_cnt + 2)){
450 rval = -EBUSY;
451 goto queuing_error;
452 }
453 }
454
455 if (unlikely(!fd->sqid)) {
456 if (cmd->sqe.common.opcode == nvme_admin_async_event) {
457 nvme->u.nvme.aen_op = 1;
458 atomic_inc(&ha->nvme_active_aen_cnt);
459 }
460 }
461
462 /* Build command packet. */
463 req->current_outstanding_cmd = handle;
464 req->outstanding_cmds[handle] = sp;
465 sp->handle = handle;
466 req->cnt -= req_cnt;
467
468 cmd_pkt = (struct cmd_nvme *)req->ring_ptr;
469 cmd_pkt->handle = make_handle(req->id, handle);
470
471 /* Zero out remaining portion of packet. */
472 clr_ptr = (uint32_t *)cmd_pkt + 2;
473 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
474
475 cmd_pkt->entry_status = 0;
476
477 /* Update entry type to indicate Command NVME IOCB */
478 cmd_pkt->entry_type = COMMAND_NVME;
479
480 /* No data transfer how do we check buffer len == 0?? */
481 if (fd->io_dir == NVMEFC_FCP_READ) {
482 cmd_pkt->control_flags = cpu_to_le16(CF_READ_DATA);
483 qpair->counters.input_bytes += fd->payload_length;
484 qpair->counters.input_requests++;
485 } else if (fd->io_dir == NVMEFC_FCP_WRITE) {
486 cmd_pkt->control_flags = cpu_to_le16(CF_WRITE_DATA);
487 if ((vha->flags.nvme_first_burst) &&
488 (sp->fcport->nvme_prli_service_param &
489 NVME_PRLI_SP_FIRST_BURST)) {
490 if ((fd->payload_length <=
491 sp->fcport->nvme_first_burst_size) ||
492 (sp->fcport->nvme_first_burst_size == 0))
493 cmd_pkt->control_flags |=
494 cpu_to_le16(CF_NVME_FIRST_BURST_ENABLE);
495 }
496 qpair->counters.output_bytes += fd->payload_length;
497 qpair->counters.output_requests++;
498 } else if (fd->io_dir == 0) {
499 cmd_pkt->control_flags = 0;
500 }
501
502 if (sp->fcport->edif.enable && fd->io_dir != 0)
503 cmd_pkt->control_flags |= cpu_to_le16(CF_EN_EDIF);
504
505 /* Set BIT_13 of control flags for Async event */
506 if (vha->flags.nvme2_enabled &&
507 cmd->sqe.common.opcode == nvme_admin_async_event) {
508 cmd_pkt->control_flags |= cpu_to_le16(CF_ADMIN_ASYNC_EVENT);
509 }
510
511 /* Set NPORT-ID */
512 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
513 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
514 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
515 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
516 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
517
518 /* NVME RSP IU */
519 cmd_pkt->nvme_rsp_dsd_len = cpu_to_le16(fd->rsplen);
520 put_unaligned_le64(fd->rspdma, &cmd_pkt->nvme_rsp_dseg_address);
521
522 /* NVME CNMD IU */
523 cmd_pkt->nvme_cmnd_dseg_len = cpu_to_le16(fd->cmdlen);
524 cmd_pkt->nvme_cmnd_dseg_address = cpu_to_le64(fd->cmddma);
525
526 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
527 cmd_pkt->byte_count = cpu_to_le32(fd->payload_length);
528
529 /* One DSD is available in the Command Type NVME IOCB */
530 avail_dsds = 1;
531 cur_dsd = &cmd_pkt->nvme_dsd;
532 sgl = fd->first_sgl;
533
534 /* Load data segments */
535 for_each_sg(sgl, sg, tot_dsds, i) {
536 cont_a64_entry_t *cont_pkt;
537
538 /* Allocate additional continuation packets? */
539 if (avail_dsds == 0) {
540 /*
541 * Five DSDs are available in the Continuation
542 * Type 1 IOCB.
543 */
544
545 /* Adjust ring index */
546 req->ring_index++;
547 if (req->ring_index == req->length) {
548 req->ring_index = 0;
549 req->ring_ptr = req->ring;
550 } else {
551 req->ring_ptr++;
552 }
553 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
554 put_unaligned_le32(CONTINUE_A64_TYPE,
555 &cont_pkt->entry_type);
556
557 cur_dsd = cont_pkt->dsd;
558 avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
559 }
560
561 append_dsd64(&cur_dsd, sg);
562 avail_dsds--;
563 }
564
565 /* Set total entry count. */
566 cmd_pkt->entry_count = (uint8_t)req_cnt;
567 wmb();
568
569 /* Adjust ring index. */
570 req->ring_index++;
571 if (req->ring_index == req->length) {
572 req->ring_index = 0;
573 req->ring_ptr = req->ring;
574 } else {
575 req->ring_ptr++;
576 }
577
578 /* ignore nvme async cmd due to long timeout */
579 if (!nvme->u.nvme.aen_op)
580 sp->qpair->cmd_cnt++;
581
582 /* Set chip new ring index. */
583 wrt_reg_dword(req->req_q_in, req->ring_index);
584
585 queuing_error:
586 if (rval)
587 qla_put_fw_resources(sp->qpair, &sp->iores);
588 spin_unlock_irqrestore(&qpair->qp_lock, flags);
589
590 return rval;
591 }
592
593 /* Post a command */
qla_nvme_post_cmd(struct nvme_fc_local_port * lport,struct nvme_fc_remote_port * rport,void * hw_queue_handle,struct nvmefc_fcp_req * fd)594 static int qla_nvme_post_cmd(struct nvme_fc_local_port *lport,
595 struct nvme_fc_remote_port *rport, void *hw_queue_handle,
596 struct nvmefc_fcp_req *fd)
597 {
598 fc_port_t *fcport;
599 struct srb_iocb *nvme;
600 struct scsi_qla_host *vha;
601 struct qla_hw_data *ha;
602 int rval;
603 srb_t *sp;
604 struct qla_qpair *qpair = hw_queue_handle;
605 struct nvme_private *priv = fd->private;
606 struct qla_nvme_rport *qla_rport = rport->private;
607
608 if (!priv) {
609 /* nvme association has been torn down */
610 return -ENODEV;
611 }
612
613 fcport = qla_rport->fcport;
614
615 if (unlikely(!qpair || !fcport || fcport->deleted))
616 return -EBUSY;
617
618 if (!(fcport->nvme_flag & NVME_FLAG_REGISTERED))
619 return -ENODEV;
620
621 vha = fcport->vha;
622 ha = vha->hw;
623
624 if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
625 return -EBUSY;
626
627 /*
628 * If we know the dev is going away while the transport is still sending
629 * IO's return busy back to stall the IO Q. This happens when the
630 * link goes away and fw hasn't notified us yet, but IO's are being
631 * returned. If the dev comes back quickly we won't exhaust the IO
632 * retry count at the core.
633 */
634 if (fcport->nvme_flag & NVME_FLAG_RESETTING)
635 return -EBUSY;
636
637 qpair = qla_mapq_nvme_select_qpair(ha, qpair);
638
639 /* Alloc SRB structure */
640 sp = qla2xxx_get_qpair_sp(vha, qpair, fcport, GFP_ATOMIC);
641 if (!sp)
642 return -EBUSY;
643
644 kref_init(&sp->cmd_kref);
645 spin_lock_init(&priv->cmd_lock);
646 sp->priv = priv;
647 priv->sp = sp;
648 sp->type = SRB_NVME_CMD;
649 sp->name = "nvme_cmd";
650 sp->done = qla_nvme_sp_done;
651 sp->put_fn = qla_nvme_release_fcp_cmd_kref;
652 sp->qpair = qpair;
653 sp->vha = vha;
654 sp->cmd_sp = sp;
655 nvme = &sp->u.iocb_cmd;
656 nvme->u.nvme.desc = fd;
657
658 rval = qla2x00_start_nvme_mq(sp);
659 if (rval != QLA_SUCCESS) {
660 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x212d,
661 "qla2x00_start_nvme_mq failed = %d\n", rval);
662 sp->priv = NULL;
663 priv->sp = NULL;
664 qla2xxx_rel_qpair_sp(sp->qpair, sp);
665 }
666
667 return rval;
668 }
669
qla_nvme_localport_delete(struct nvme_fc_local_port * lport)670 static void qla_nvme_localport_delete(struct nvme_fc_local_port *lport)
671 {
672 struct scsi_qla_host *vha = lport->private;
673
674 ql_log(ql_log_info, vha, 0x210f,
675 "localport delete of %p completed.\n", vha->nvme_local_port);
676 vha->nvme_local_port = NULL;
677 complete(&vha->nvme_del_done);
678 }
679
qla_nvme_remoteport_delete(struct nvme_fc_remote_port * rport)680 static void qla_nvme_remoteport_delete(struct nvme_fc_remote_port *rport)
681 {
682 fc_port_t *fcport;
683 struct qla_nvme_rport *qla_rport = rport->private;
684
685 fcport = qla_rport->fcport;
686 fcport->nvme_remote_port = NULL;
687 fcport->nvme_flag &= ~NVME_FLAG_REGISTERED;
688 fcport->nvme_flag &= ~NVME_FLAG_DELETING;
689 ql_log(ql_log_info, fcport->vha, 0x2110,
690 "remoteport_delete of %p %8phN completed.\n",
691 fcport, fcport->port_name);
692 complete(&fcport->nvme_del_done);
693 }
694
695 static struct nvme_fc_port_template qla_nvme_fc_transport = {
696 .localport_delete = qla_nvme_localport_delete,
697 .remoteport_delete = qla_nvme_remoteport_delete,
698 .create_queue = qla_nvme_alloc_queue,
699 .delete_queue = NULL,
700 .ls_req = qla_nvme_ls_req,
701 .ls_abort = qla_nvme_ls_abort,
702 .fcp_io = qla_nvme_post_cmd,
703 .fcp_abort = qla_nvme_fcp_abort,
704 .max_hw_queues = 8,
705 .max_sgl_segments = 1024,
706 .max_dif_sgl_segments = 64,
707 .dma_boundary = 0xFFFFFFFF,
708 .local_priv_sz = 8,
709 .remote_priv_sz = sizeof(struct qla_nvme_rport),
710 .lsrqst_priv_sz = sizeof(struct nvme_private),
711 .fcprqst_priv_sz = sizeof(struct nvme_private),
712 };
713
qla_nvme_unregister_remote_port(struct fc_port * fcport)714 void qla_nvme_unregister_remote_port(struct fc_port *fcport)
715 {
716 int ret;
717
718 if (!IS_ENABLED(CONFIG_NVME_FC))
719 return;
720
721 ql_log(ql_log_warn, fcport->vha, 0x2112,
722 "%s: unregister remoteport on %p %8phN\n",
723 __func__, fcport, fcport->port_name);
724
725 if (test_bit(PFLG_DRIVER_REMOVING, &fcport->vha->pci_flags))
726 nvme_fc_set_remoteport_devloss(fcport->nvme_remote_port, 0);
727
728 init_completion(&fcport->nvme_del_done);
729 ret = nvme_fc_unregister_remoteport(fcport->nvme_remote_port);
730 if (ret)
731 ql_log(ql_log_info, fcport->vha, 0x2114,
732 "%s: Failed to unregister nvme_remote_port (%d)\n",
733 __func__, ret);
734 wait_for_completion(&fcport->nvme_del_done);
735 }
736
qla_nvme_delete(struct scsi_qla_host * vha)737 void qla_nvme_delete(struct scsi_qla_host *vha)
738 {
739 int nv_ret;
740
741 if (!IS_ENABLED(CONFIG_NVME_FC))
742 return;
743
744 if (vha->nvme_local_port) {
745 init_completion(&vha->nvme_del_done);
746 ql_log(ql_log_info, vha, 0x2116,
747 "unregister localport=%p\n",
748 vha->nvme_local_port);
749 nv_ret = nvme_fc_unregister_localport(vha->nvme_local_port);
750 if (nv_ret)
751 ql_log(ql_log_info, vha, 0x2115,
752 "Unregister of localport failed\n");
753 else
754 wait_for_completion(&vha->nvme_del_done);
755 }
756 }
757
qla_nvme_register_hba(struct scsi_qla_host * vha)758 int qla_nvme_register_hba(struct scsi_qla_host *vha)
759 {
760 struct nvme_fc_port_template *tmpl;
761 struct qla_hw_data *ha;
762 struct nvme_fc_port_info pinfo;
763 int ret = -EINVAL;
764
765 if (!IS_ENABLED(CONFIG_NVME_FC))
766 return ret;
767
768 ha = vha->hw;
769 tmpl = &qla_nvme_fc_transport;
770
771
772 qla_nvme_fc_transport.max_hw_queues =
773 min((uint8_t)(qla_nvme_fc_transport.max_hw_queues),
774 (uint8_t)(ha->max_qpairs ? ha->max_qpairs : 1));
775
776 pinfo.node_name = wwn_to_u64(vha->node_name);
777 pinfo.port_name = wwn_to_u64(vha->port_name);
778 pinfo.port_role = FC_PORT_ROLE_NVME_INITIATOR;
779 pinfo.port_id = vha->d_id.b24;
780
781 mutex_lock(&ha->vport_lock);
782 /*
783 * Check again for nvme_local_port to see if any other thread raced
784 * with this one and finished registration.
785 */
786 if (!vha->nvme_local_port) {
787 ql_log(ql_log_info, vha, 0xffff,
788 "register_localport: host-traddr=nn-0x%llx:pn-0x%llx on portID:%x\n",
789 pinfo.node_name, pinfo.port_name, pinfo.port_id);
790 qla_nvme_fc_transport.dma_boundary = vha->host->dma_boundary;
791
792 ret = nvme_fc_register_localport(&pinfo, tmpl,
793 get_device(&ha->pdev->dev),
794 &vha->nvme_local_port);
795 mutex_unlock(&ha->vport_lock);
796 } else {
797 mutex_unlock(&ha->vport_lock);
798 return 0;
799 }
800 if (ret) {
801 ql_log(ql_log_warn, vha, 0xffff,
802 "register_localport failed: ret=%x\n", ret);
803 } else {
804 vha->nvme_local_port->private = vha;
805 }
806
807 return ret;
808 }
809
qla_nvme_abort_set_option(struct abort_entry_24xx * abt,srb_t * orig_sp)810 void qla_nvme_abort_set_option(struct abort_entry_24xx *abt, srb_t *orig_sp)
811 {
812 struct qla_hw_data *ha;
813
814 if (!(ql2xabts_wait_nvme && QLA_ABTS_WAIT_ENABLED(orig_sp)))
815 return;
816
817 ha = orig_sp->fcport->vha->hw;
818
819 WARN_ON_ONCE(abt->options & cpu_to_le16(BIT_0));
820 /* Use Driver Specified Retry Count */
821 abt->options |= cpu_to_le16(AOF_ABTS_RTY_CNT);
822 abt->drv.abts_rty_cnt = cpu_to_le16(2);
823 /* Use specified response timeout */
824 abt->options |= cpu_to_le16(AOF_RSP_TIMEOUT);
825 /* set it to 2 * r_a_tov in secs */
826 abt->drv.rsp_timeout = cpu_to_le16(2 * (ha->r_a_tov / 10));
827 }
828
qla_nvme_abort_process_comp_status(struct abort_entry_24xx * abt,srb_t * orig_sp)829 void qla_nvme_abort_process_comp_status(struct abort_entry_24xx *abt, srb_t *orig_sp)
830 {
831 u16 comp_status;
832 struct scsi_qla_host *vha;
833
834 if (!(ql2xabts_wait_nvme && QLA_ABTS_WAIT_ENABLED(orig_sp)))
835 return;
836
837 vha = orig_sp->fcport->vha;
838
839 comp_status = le16_to_cpu(abt->comp_status);
840 switch (comp_status) {
841 case CS_RESET: /* reset event aborted */
842 case CS_ABORTED: /* IOCB was cleaned */
843 /* N_Port handle is not currently logged in */
844 case CS_TIMEOUT:
845 /* N_Port handle was logged out while waiting for ABTS to complete */
846 case CS_PORT_UNAVAILABLE:
847 /* Firmware found that the port name changed */
848 case CS_PORT_LOGGED_OUT:
849 /* BA_RJT was received for the ABTS */
850 case CS_PORT_CONFIG_CHG:
851 ql_dbg(ql_dbg_async, vha, 0xf09d,
852 "Abort I/O IOCB completed with error, comp_status=%x\n",
853 comp_status);
854 break;
855
856 /* BA_RJT was received for the ABTS */
857 case CS_REJECT_RECEIVED:
858 ql_dbg(ql_dbg_async, vha, 0xf09e,
859 "BA_RJT was received for the ABTS rjt_vendorUnique = %u",
860 abt->fw.ba_rjt_vendorUnique);
861 ql_dbg(ql_dbg_async + ql_dbg_mbx, vha, 0xf09e,
862 "ba_rjt_reasonCodeExpl = %u, ba_rjt_reasonCode = %u\n",
863 abt->fw.ba_rjt_reasonCodeExpl, abt->fw.ba_rjt_reasonCode);
864 break;
865
866 case CS_COMPLETE:
867 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0xf09f,
868 "IOCB request is completed successfully comp_status=%x\n",
869 comp_status);
870 break;
871
872 case CS_IOCB_ERROR:
873 ql_dbg(ql_dbg_async, vha, 0xf0a0,
874 "IOCB request is failed, comp_status=%x\n", comp_status);
875 break;
876
877 default:
878 ql_dbg(ql_dbg_async, vha, 0xf0a1,
879 "Invalid Abort IO IOCB Completion Status %x\n",
880 comp_status);
881 break;
882 }
883 }
884
qla_wait_nvme_release_cmd_kref(srb_t * orig_sp)885 inline void qla_wait_nvme_release_cmd_kref(srb_t *orig_sp)
886 {
887 if (!(ql2xabts_wait_nvme && QLA_ABTS_WAIT_ENABLED(orig_sp)))
888 return;
889 kref_put(&orig_sp->cmd_kref, orig_sp->put_fn);
890 }
891