blob: ec15a49532a605dc9756e481b86d87838c021ae0 [file] [log] [blame]
Duane Grigsbye84067d2017-06-21 13:48:43 -07001/*
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 "qla_def.h"
9#include <linux/scatterlist.h>
10#include <linux/delay.h>
11#include <linux/nvme.h>
12#include <linux/nvme-fc.h>
13
14static struct nvme_fc_port_template qla_nvme_fc_transport;
15
16static void qla_nvme_unregister_remote_port(struct work_struct *);
17
18int qla_nvme_register_remote(scsi_qla_host_t *vha, fc_port_t *fcport)
19{
Duane Grigsbye84067d2017-06-21 13:48:43 -070020 struct nvme_rport *rport;
21 int ret;
22
Arnd Bergmannbcda7712017-06-30 18:10:40 +020023 if (!IS_ENABLED(CONFIG_NVME_FC))
24 return 0;
25
Duane Grigsbye84067d2017-06-21 13:48:43 -070026 if (fcport->nvme_flag & NVME_FLAG_REGISTERED)
27 return 0;
28
29 if (!vha->flags.nvme_enabled) {
30 ql_log(ql_log_info, vha, 0x2100,
31 "%s: Not registering target since Host NVME is not enabled\n",
32 __func__);
33 return 0;
34 }
35
36 if (!(fcport->nvme_prli_service_param &
37 (NVME_PRLI_SP_TARGET | NVME_PRLI_SP_DISCOVERY)))
38 return 0;
39
40 INIT_WORK(&fcport->nvme_del_work, qla_nvme_unregister_remote_port);
41 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
42 if (!rport) {
43 ql_log(ql_log_warn, vha, 0x2101,
44 "%s: unable to alloc memory\n", __func__);
45 return -ENOMEM;
46 }
47
48 rport->req.port_name = wwn_to_u64(fcport->port_name);
49 rport->req.node_name = wwn_to_u64(fcport->node_name);
50 rport->req.port_role = 0;
51
52 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_INITIATOR)
53 rport->req.port_role = FC_PORT_ROLE_NVME_INITIATOR;
54
55 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_TARGET)
56 rport->req.port_role |= FC_PORT_ROLE_NVME_TARGET;
57
58 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_DISCOVERY)
59 rport->req.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
60
61 rport->req.port_id = fcport->d_id.b24;
62
63 ql_log(ql_log_info, vha, 0x2102,
64 "%s: traddr=pn-0x%016llx:nn-0x%016llx PortID:%06x\n",
65 __func__, rport->req.port_name, rport->req.node_name,
66 rport->req.port_id);
67
68 ret = nvme_fc_register_remoteport(vha->nvme_local_port, &rport->req,
69 &fcport->nvme_remote_port);
70 if (ret) {
71 ql_log(ql_log_warn, vha, 0x212e,
72 "Failed to register remote port. Transport returned %d\n",
73 ret);
74 return ret;
75 }
76
77 fcport->nvme_remote_port->private = fcport;
78 fcport->nvme_flag |= NVME_FLAG_REGISTERED;
79 atomic_set(&fcport->nvme_ref_count, 1);
80 init_waitqueue_head(&fcport->nvme_waitQ);
81 rport->fcport = fcport;
82 list_add_tail(&rport->list, &vha->nvme_rport_list);
Duane Grigsbye84067d2017-06-21 13:48:43 -070083 return 0;
84}
85
86/* Allocate a queue for NVMe traffic */
87static int qla_nvme_alloc_queue(struct nvme_fc_local_port *lport, unsigned int qidx,
88 u16 qsize, void **handle)
89{
90 struct scsi_qla_host *vha;
91 struct qla_hw_data *ha;
92 struct qla_qpair *qpair;
93
94 if (!qidx)
95 qidx++;
96
97 vha = (struct scsi_qla_host *)lport->private;
98 ha = vha->hw;
99
100 ql_log(ql_log_info, vha, 0x2104,
101 "%s: handle %p, idx =%d, qsize %d\n",
102 __func__, handle, qidx, qsize);
103
104 if (qidx > qla_nvme_fc_transport.max_hw_queues) {
105 ql_log(ql_log_warn, vha, 0x212f,
106 "%s: Illegal qidx=%d. Max=%d\n",
107 __func__, qidx, qla_nvme_fc_transport.max_hw_queues);
108 return -EINVAL;
109 }
110
111 if (ha->queue_pair_map[qidx]) {
112 *handle = ha->queue_pair_map[qidx];
113 ql_log(ql_log_info, vha, 0x2121,
114 "Returning existing qpair of %p for idx=%x\n",
115 *handle, qidx);
116 return 0;
117 }
118
119 ql_log(ql_log_warn, vha, 0xffff,
120 "allocating q for idx=%x w/o cpu mask\n", qidx);
121 qpair = qla2xxx_create_qpair(vha, 5, vha->vp_idx, true);
122 if (qpair == NULL) {
123 ql_log(ql_log_warn, vha, 0x2122,
124 "Failed to allocate qpair\n");
125 return -EINVAL;
126 }
127 *handle = qpair;
128
129 return 0;
130}
131
132static void qla_nvme_sp_ls_done(void *ptr, int res)
133{
134 srb_t *sp = ptr;
135 struct srb_iocb *nvme;
136 struct nvmefc_ls_req *fd;
137 struct nvme_private *priv;
138
139 if (atomic_read(&sp->ref_count) == 0) {
140 ql_log(ql_log_warn, sp->fcport->vha, 0x2123,
141 "SP reference-count to ZERO on LS_done -- sp=%p.\n", sp);
142 return;
143 }
144
145 if (!atomic_dec_and_test(&sp->ref_count))
146 return;
147
148 if (res)
149 res = -EINVAL;
150
151 nvme = &sp->u.iocb_cmd;
152 fd = nvme->u.nvme.desc;
153 priv = fd->private;
154 priv->comp_status = res;
155 schedule_work(&priv->ls_work);
156 /* work schedule doesn't need the sp */
157 qla2x00_rel_sp(sp);
158}
159
160static void qla_nvme_sp_done(void *ptr, int res)
161{
162 srb_t *sp = ptr;
163 struct srb_iocb *nvme;
164 struct nvmefc_fcp_req *fd;
165
166 nvme = &sp->u.iocb_cmd;
167 fd = nvme->u.nvme.desc;
168
169 if (!atomic_dec_and_test(&sp->ref_count))
170 return;
171
172 if (!(sp->fcport->nvme_flag & NVME_FLAG_REGISTERED))
173 goto rel;
174
175 if (unlikely(nvme->u.nvme.comp_status || res))
176 fd->status = -EINVAL;
177 else
178 fd->status = 0;
179
180 fd->rcv_rsplen = nvme->u.nvme.rsp_pyld_len;
181 fd->done(fd);
182rel:
183 qla2xxx_rel_qpair_sp(sp->qpair, sp);
184}
185
186static void qla_nvme_ls_abort(struct nvme_fc_local_port *lport,
187 struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
188{
189 struct nvme_private *priv = fd->private;
190 fc_port_t *fcport = rport->private;
191 srb_t *sp = priv->sp;
192 int rval;
193 struct qla_hw_data *ha = fcport->vha->hw;
194
195 rval = ha->isp_ops->abort_command(sp);
196 if (rval != QLA_SUCCESS)
197 ql_log(ql_log_warn, fcport->vha, 0x2125,
198 "%s: failed to abort LS command for SP:%p rval=%x\n",
199 __func__, sp, rval);
200
201 ql_dbg(ql_dbg_io, fcport->vha, 0x212b,
202 "%s: aborted sp:%p on fcport:%p\n", __func__, sp, fcport);
203}
204
205static void qla_nvme_ls_complete(struct work_struct *work)
206{
207 struct nvme_private *priv =
208 container_of(work, struct nvme_private, ls_work);
209 struct nvmefc_ls_req *fd = priv->fd;
210
211 fd->done(fd, priv->comp_status);
212}
213
214static int qla_nvme_ls_req(struct nvme_fc_local_port *lport,
215 struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
216{
217 fc_port_t *fcport = (fc_port_t *)rport->private;
218 struct srb_iocb *nvme;
219 struct nvme_private *priv = fd->private;
220 struct scsi_qla_host *vha;
221 int rval = QLA_FUNCTION_FAILED;
222 struct qla_hw_data *ha;
223 srb_t *sp;
224
225 if (!(fcport->nvme_flag & NVME_FLAG_REGISTERED))
226 return rval;
227
228 vha = fcport->vha;
229 ha = vha->hw;
230 /* Alloc SRB structure */
231 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
232 if (!sp)
233 return rval;
234
235 sp->type = SRB_NVME_LS;
236 sp->name = "nvme_ls";
237 sp->done = qla_nvme_sp_ls_done;
238 atomic_set(&sp->ref_count, 1);
239 init_waitqueue_head(&sp->nvme_ls_waitQ);
240 nvme = &sp->u.iocb_cmd;
241 priv->sp = sp;
242 priv->fd = fd;
243 INIT_WORK(&priv->ls_work, qla_nvme_ls_complete);
244 nvme->u.nvme.desc = fd;
245 nvme->u.nvme.dir = 0;
246 nvme->u.nvme.dl = 0;
247 nvme->u.nvme.cmd_len = fd->rqstlen;
248 nvme->u.nvme.rsp_len = fd->rsplen;
249 nvme->u.nvme.rsp_dma = fd->rspdma;
250 nvme->u.nvme.timeout_sec = fd->timeout;
251 nvme->u.nvme.cmd_dma = dma_map_single(&ha->pdev->dev, fd->rqstaddr,
252 fd->rqstlen, DMA_TO_DEVICE);
253 dma_sync_single_for_device(&ha->pdev->dev, nvme->u.nvme.cmd_dma,
254 fd->rqstlen, DMA_TO_DEVICE);
255
256 rval = qla2x00_start_sp(sp);
257 if (rval != QLA_SUCCESS) {
258 ql_log(ql_log_warn, vha, 0x700e,
259 "qla2x00_start_sp failed = %d\n", rval);
260 atomic_dec(&sp->ref_count);
261 wake_up(&sp->nvme_ls_waitQ);
262 return rval;
263 }
264
265 return rval;
266}
267
268static void qla_nvme_fcp_abort(struct nvme_fc_local_port *lport,
269 struct nvme_fc_remote_port *rport, void *hw_queue_handle,
270 struct nvmefc_fcp_req *fd)
271{
272 struct nvme_private *priv = fd->private;
273 srb_t *sp = priv->sp;
274 int rval;
275 fc_port_t *fcport = rport->private;
276 struct qla_hw_data *ha = fcport->vha->hw;
277
278 rval = ha->isp_ops->abort_command(sp);
279 if (!rval)
280 ql_log(ql_log_warn, fcport->vha, 0x2127,
281 "%s: failed to abort command for SP:%p rval=%x\n",
282 __func__, sp, rval);
283
284 ql_dbg(ql_dbg_io, fcport->vha, 0x2126,
285 "%s: aborted sp:%p on fcport:%p\n", __func__, sp, fcport);
286}
287
288static void qla_nvme_poll(struct nvme_fc_local_port *lport, void *hw_queue_handle)
289{
290 struct scsi_qla_host *vha = lport->private;
291 unsigned long flags;
292 struct qla_qpair *qpair = (struct qla_qpair *)hw_queue_handle;
293
294 /* Acquire ring specific lock */
295 spin_lock_irqsave(&qpair->qp_lock, flags);
296 qla24xx_process_response_queue(vha, qpair->rsp);
297 spin_unlock_irqrestore(&qpair->qp_lock, flags);
298}
299
300static int qla2x00_start_nvme_mq(srb_t *sp)
301{
302 unsigned long flags;
303 uint32_t *clr_ptr;
304 uint32_t index;
305 uint32_t handle;
306 struct cmd_nvme *cmd_pkt;
307 uint16_t cnt, i;
308 uint16_t req_cnt;
309 uint16_t tot_dsds;
310 uint16_t avail_dsds;
311 uint32_t *cur_dsd;
312 struct req_que *req = NULL;
313 struct scsi_qla_host *vha = sp->fcport->vha;
314 struct qla_hw_data *ha = vha->hw;
315 struct qla_qpair *qpair = sp->qpair;
316 struct srb_iocb *nvme = &sp->u.iocb_cmd;
317 struct scatterlist *sgl, *sg;
318 struct nvmefc_fcp_req *fd = nvme->u.nvme.desc;
319 uint32_t rval = QLA_SUCCESS;
320
321 /* Setup qpair pointers */
322 req = qpair->req;
323 tot_dsds = fd->sg_cnt;
324
325 /* Acquire qpair specific lock */
326 spin_lock_irqsave(&qpair->qp_lock, flags);
327
328 /* Check for room in outstanding command list. */
329 handle = req->current_outstanding_cmd;
330 for (index = 1; index < req->num_outstanding_cmds; index++) {
331 handle++;
332 if (handle == req->num_outstanding_cmds)
333 handle = 1;
334 if (!req->outstanding_cmds[handle])
335 break;
336 }
337
338 if (index == req->num_outstanding_cmds) {
339 rval = -1;
340 goto queuing_error;
341 }
342 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
343 if (req->cnt < (req_cnt + 2)) {
344 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
345 RD_REG_DWORD_RELAXED(req->req_q_out);
346
347 if (req->ring_index < cnt)
348 req->cnt = cnt - req->ring_index;
349 else
350 req->cnt = req->length - (req->ring_index - cnt);
351
352 if (req->cnt < (req_cnt + 2)){
353 rval = -1;
354 goto queuing_error;
355 }
356 }
357
358 if (unlikely(!fd->sqid)) {
359 struct nvme_fc_cmd_iu *cmd = fd->cmdaddr;
360 if (cmd->sqe.common.opcode == nvme_admin_async_event) {
361 nvme->u.nvme.aen_op = 1;
362 atomic_inc(&vha->nvme_active_aen_cnt);
363 }
364 }
365
366 /* Build command packet. */
367 req->current_outstanding_cmd = handle;
368 req->outstanding_cmds[handle] = sp;
369 sp->handle = handle;
370 req->cnt -= req_cnt;
371
372 cmd_pkt = (struct cmd_nvme *)req->ring_ptr;
373 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
374
375 /* Zero out remaining portion of packet. */
376 clr_ptr = (uint32_t *)cmd_pkt + 2;
377 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
378
379 cmd_pkt->entry_status = 0;
380
381 /* Update entry type to indicate Command NVME IOCB */
382 cmd_pkt->entry_type = COMMAND_NVME;
383
384 /* No data transfer how do we check buffer len == 0?? */
385 if (fd->io_dir == NVMEFC_FCP_READ) {
386 cmd_pkt->control_flags =
387 cpu_to_le16(CF_READ_DATA | CF_NVME_ENABLE);
388 vha->qla_stats.input_bytes += fd->payload_length;
389 vha->qla_stats.input_requests++;
390 } else if (fd->io_dir == NVMEFC_FCP_WRITE) {
391 cmd_pkt->control_flags =
392 cpu_to_le16(CF_WRITE_DATA | CF_NVME_ENABLE);
393 vha->qla_stats.output_bytes += fd->payload_length;
394 vha->qla_stats.output_requests++;
395 } else if (fd->io_dir == 0) {
396 cmd_pkt->control_flags = cpu_to_le16(CF_NVME_ENABLE);
397 }
398
399 /* Set NPORT-ID */
400 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
401 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
402 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
403 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
404 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
405
406 /* NVME RSP IU */
407 cmd_pkt->nvme_rsp_dsd_len = cpu_to_le16(fd->rsplen);
408 cmd_pkt->nvme_rsp_dseg_address[0] = cpu_to_le32(LSD(fd->rspdma));
409 cmd_pkt->nvme_rsp_dseg_address[1] = cpu_to_le32(MSD(fd->rspdma));
410
411 /* NVME CNMD IU */
412 cmd_pkt->nvme_cmnd_dseg_len = cpu_to_le16(fd->cmdlen);
413 cmd_pkt->nvme_cmnd_dseg_address[0] = cpu_to_le32(LSD(fd->cmddma));
414 cmd_pkt->nvme_cmnd_dseg_address[1] = cpu_to_le32(MSD(fd->cmddma));
415
416 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
417 cmd_pkt->byte_count = cpu_to_le32(fd->payload_length);
418
419 /* One DSD is available in the Command Type NVME IOCB */
420 avail_dsds = 1;
421 cur_dsd = (uint32_t *)&cmd_pkt->nvme_data_dseg_address[0];
422 sgl = fd->first_sgl;
423
424 /* Load data segments */
425 for_each_sg(sgl, sg, tot_dsds, i) {
426 dma_addr_t sle_dma;
427 cont_a64_entry_t *cont_pkt;
428
429 /* Allocate additional continuation packets? */
430 if (avail_dsds == 0) {
431 /*
432 * Five DSDs are available in the Continuation
433 * Type 1 IOCB.
434 */
435
436 /* Adjust ring index */
437 req->ring_index++;
438 if (req->ring_index == req->length) {
439 req->ring_index = 0;
440 req->ring_ptr = req->ring;
441 } else {
442 req->ring_ptr++;
443 }
444 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
445 cont_pkt->entry_type = cpu_to_le32(CONTINUE_A64_TYPE);
446
447 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
448 avail_dsds = 5;
449 }
450
451 sle_dma = sg_dma_address(sg);
452 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
453 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
454 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
455 avail_dsds--;
456 }
457
458 /* Set total entry count. */
459 cmd_pkt->entry_count = (uint8_t)req_cnt;
460 wmb();
461
462 /* Adjust ring index. */
463 req->ring_index++;
464 if (req->ring_index == req->length) {
465 req->ring_index = 0;
466 req->ring_ptr = req->ring;
467 } else {
468 req->ring_ptr++;
469 }
470
471 /* Set chip new ring index. */
472 WRT_REG_DWORD(req->req_q_in, req->ring_index);
473
474queuing_error:
475 spin_unlock_irqrestore(&qpair->qp_lock, flags);
476 return rval;
477}
478
479/* Post a command */
480static int qla_nvme_post_cmd(struct nvme_fc_local_port *lport,
481 struct nvme_fc_remote_port *rport, void *hw_queue_handle,
482 struct nvmefc_fcp_req *fd)
483{
484 fc_port_t *fcport;
485 struct srb_iocb *nvme;
486 struct scsi_qla_host *vha;
487 int rval = QLA_FUNCTION_FAILED;
488 srb_t *sp;
489 struct qla_qpair *qpair = (struct qla_qpair *)hw_queue_handle;
490 struct nvme_private *priv;
491
492 if (!fd) {
Colin Ian King0bf0efa2017-06-30 14:47:41 +0100493 ql_log(ql_log_warn, NULL, 0x2134, "NO NVMe FCP request\n");
Duane Grigsbye84067d2017-06-21 13:48:43 -0700494 return rval;
495 }
496
497 priv = fd->private;
498 fcport = (fc_port_t *)rport->private;
499 if (!fcport) {
500 ql_log(ql_log_warn, NULL, 0x210e, "No fcport ptr\n");
501 return rval;
502 }
503
504 vha = fcport->vha;
505 if ((!qpair) || (!(fcport->nvme_flag & NVME_FLAG_REGISTERED)))
506 return -EBUSY;
507
508 /* Alloc SRB structure */
509 sp = qla2xxx_get_qpair_sp(qpair, fcport, GFP_ATOMIC);
510 if (!sp)
511 return -EIO;
512
513 atomic_set(&sp->ref_count, 1);
514 init_waitqueue_head(&sp->nvme_ls_waitQ);
515 priv->sp = sp;
516 sp->type = SRB_NVME_CMD;
517 sp->name = "nvme_cmd";
518 sp->done = qla_nvme_sp_done;
519 sp->qpair = qpair;
520 nvme = &sp->u.iocb_cmd;
521 nvme->u.nvme.desc = fd;
522
523 rval = qla2x00_start_nvme_mq(sp);
524 if (rval != QLA_SUCCESS) {
525 ql_log(ql_log_warn, vha, 0x212d,
526 "qla2x00_start_nvme_mq failed = %d\n", rval);
527 atomic_dec(&sp->ref_count);
528 wake_up(&sp->nvme_ls_waitQ);
529 return -EIO;
530 }
531
532 return rval;
533}
534
535static void qla_nvme_localport_delete(struct nvme_fc_local_port *lport)
536{
537 struct scsi_qla_host *vha = lport->private;
538
539 atomic_dec(&vha->nvme_ref_count);
540 wake_up_all(&vha->nvme_waitQ);
541
542 ql_log(ql_log_info, vha, 0x210f,
543 "localport delete of %p completed.\n", vha->nvme_local_port);
544 vha->nvme_local_port = NULL;
545}
546
547static void qla_nvme_remoteport_delete(struct nvme_fc_remote_port *rport)
548{
549 fc_port_t *fcport;
550 struct nvme_rport *r_port, *trport;
551
552 fcport = (fc_port_t *)rport->private;
553 fcport->nvme_remote_port = NULL;
554 fcport->nvme_flag &= ~NVME_FLAG_REGISTERED;
555 atomic_dec(&fcport->nvme_ref_count);
556 wake_up_all(&fcport->nvme_waitQ);
557
558 list_for_each_entry_safe(r_port, trport,
559 &fcport->vha->nvme_rport_list, list) {
560 if (r_port->fcport == fcport) {
561 list_del(&r_port->list);
562 break;
563 }
564 }
565 kfree(r_port);
566
567 ql_log(ql_log_info, fcport->vha, 0x2110,
568 "remoteport_delete of %p completed.\n", fcport);
569}
570
571static struct nvme_fc_port_template qla_nvme_fc_transport = {
572 .localport_delete = qla_nvme_localport_delete,
573 .remoteport_delete = qla_nvme_remoteport_delete,
574 .create_queue = qla_nvme_alloc_queue,
575 .delete_queue = NULL,
576 .ls_req = qla_nvme_ls_req,
577 .ls_abort = qla_nvme_ls_abort,
578 .fcp_io = qla_nvme_post_cmd,
579 .fcp_abort = qla_nvme_fcp_abort,
580 .poll_queue = qla_nvme_poll,
581 .max_hw_queues = 8,
582 .max_sgl_segments = 128,
583 .max_dif_sgl_segments = 64,
584 .dma_boundary = 0xFFFFFFFF,
585 .local_priv_sz = 8,
586 .remote_priv_sz = 0,
587 .lsrqst_priv_sz = sizeof(struct nvme_private),
588 .fcprqst_priv_sz = sizeof(struct nvme_private),
589};
590
591#define NVME_ABORT_POLLING_PERIOD 2
592static int qla_nvme_wait_on_command(srb_t *sp)
593{
594 int ret = QLA_SUCCESS;
595
596 wait_event_timeout(sp->nvme_ls_waitQ, (atomic_read(&sp->ref_count) > 1),
597 NVME_ABORT_POLLING_PERIOD*HZ);
598
599 if (atomic_read(&sp->ref_count) > 1)
600 ret = QLA_FUNCTION_FAILED;
601
602 return ret;
603}
604
605static int qla_nvme_wait_on_rport_del(fc_port_t *fcport)
606{
607 int ret = QLA_SUCCESS;
608
609 wait_event_timeout(fcport->nvme_waitQ,
610 atomic_read(&fcport->nvme_ref_count),
611 NVME_ABORT_POLLING_PERIOD*HZ);
612
613 if (atomic_read(&fcport->nvme_ref_count)) {
614 ret = QLA_FUNCTION_FAILED;
615 ql_log(ql_log_info, fcport->vha, 0x2111,
616 "timed out waiting for fcport=%p to delete\n", fcport);
617 }
618
619 return ret;
620}
621
622void qla_nvme_abort(struct qla_hw_data *ha, srb_t *sp)
623{
624 int rval;
625
626 rval = ha->isp_ops->abort_command(sp);
627 if (!rval) {
628 if (!qla_nvme_wait_on_command(sp))
629 ql_log(ql_log_warn, NULL, 0x2112,
Colin Ian King0bf0efa2017-06-30 14:47:41 +0100630 "nvme_wait_on_command timed out waiting on sp=%p\n",
Duane Grigsbye84067d2017-06-21 13:48:43 -0700631 sp);
632 }
633}
634
635static void qla_nvme_abort_all(fc_port_t *fcport)
636{
637 int que, cnt;
638 unsigned long flags;
639 srb_t *sp;
640 struct qla_hw_data *ha = fcport->vha->hw;
641 struct req_que *req;
642
643 spin_lock_irqsave(&ha->hardware_lock, flags);
644 for (que = 0; que < ha->max_req_queues; que++) {
645 req = ha->req_q_map[que];
646 if (!req)
647 continue;
648 if (!req->outstanding_cmds)
649 continue;
650 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
651 sp = req->outstanding_cmds[cnt];
652 if ((sp) && ((sp->type == SRB_NVME_CMD) ||
653 (sp->type == SRB_NVME_LS)) &&
654 (sp->fcport == fcport)) {
655 atomic_inc(&sp->ref_count);
656 spin_unlock_irqrestore(&ha->hardware_lock,
657 flags);
658 qla_nvme_abort(ha, sp);
659 spin_lock_irqsave(&ha->hardware_lock, flags);
660 req->outstanding_cmds[cnt] = NULL;
661 sp->done(sp, 1);
662 }
663 }
664 }
665 spin_unlock_irqrestore(&ha->hardware_lock, flags);
666}
667
668static void qla_nvme_unregister_remote_port(struct work_struct *work)
669{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700670 struct fc_port *fcport = container_of(work, struct fc_port,
671 nvme_del_work);
672 struct nvme_rport *rport, *trport;
673
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200674 if (!IS_ENABLED(CONFIG_NVME_FC))
675 return;
676
Duane Grigsbye84067d2017-06-21 13:48:43 -0700677 list_for_each_entry_safe(rport, trport,
678 &fcport->vha->nvme_rport_list, list) {
679 if (rport->fcport == fcport) {
680 ql_log(ql_log_info, fcport->vha, 0x2113,
681 "%s: fcport=%p\n", __func__, fcport);
682 nvme_fc_unregister_remoteport(
683 fcport->nvme_remote_port);
684 }
685 }
Duane Grigsbye84067d2017-06-21 13:48:43 -0700686}
687
688void qla_nvme_delete(scsi_qla_host_t *vha)
689{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700690 struct nvme_rport *rport, *trport;
691 fc_port_t *fcport;
692 int nv_ret;
693
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200694 if (!IS_ENABLED(CONFIG_NVME_FC))
695 return;
696
Duane Grigsbye84067d2017-06-21 13:48:43 -0700697 list_for_each_entry_safe(rport, trport, &vha->nvme_rport_list, list) {
698 fcport = rport->fcport;
699
700 ql_log(ql_log_info, fcport->vha, 0x2114, "%s: fcport=%p\n",
701 __func__, fcport);
702
703 nvme_fc_unregister_remoteport(fcport->nvme_remote_port);
704 qla_nvme_wait_on_rport_del(fcport);
705 qla_nvme_abort_all(fcport);
706 }
707
708 if (vha->nvme_local_port) {
709 nv_ret = nvme_fc_unregister_localport(vha->nvme_local_port);
710 if (nv_ret == 0)
711 ql_log(ql_log_info, vha, 0x2116,
712 "unregistered localport=%p\n",
713 vha->nvme_local_port);
714 else
715 ql_log(ql_log_info, vha, 0x2115,
716 "Unregister of localport failed\n");
717 }
Duane Grigsbye84067d2017-06-21 13:48:43 -0700718}
719
720void qla_nvme_register_hba(scsi_qla_host_t *vha)
721{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700722 struct nvme_fc_port_template *tmpl;
723 struct qla_hw_data *ha;
724 struct nvme_fc_port_info pinfo;
725 int ret;
726
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200727 if (!IS_ENABLED(CONFIG_NVME_FC))
728 return;
729
Duane Grigsbye84067d2017-06-21 13:48:43 -0700730 ha = vha->hw;
731 tmpl = &qla_nvme_fc_transport;
732
733 WARN_ON(vha->nvme_local_port);
734 WARN_ON(ha->max_req_queues < 3);
735
736 qla_nvme_fc_transport.max_hw_queues =
737 min((uint8_t)(qla_nvme_fc_transport.max_hw_queues),
738 (uint8_t)(ha->max_req_queues - 2));
739
740 pinfo.node_name = wwn_to_u64(vha->node_name);
741 pinfo.port_name = wwn_to_u64(vha->port_name);
742 pinfo.port_role = FC_PORT_ROLE_NVME_INITIATOR;
743 pinfo.port_id = vha->d_id.b24;
744
745 ql_log(ql_log_info, vha, 0xffff,
746 "register_localport: host-traddr=pn-0x%llx:nn-0x%llx on portID:%x\n",
747 pinfo.port_name, pinfo.node_name, pinfo.port_id);
748 qla_nvme_fc_transport.dma_boundary = vha->host->dma_boundary;
749
750 ret = nvme_fc_register_localport(&pinfo, tmpl,
751 get_device(&ha->pdev->dev), &vha->nvme_local_port);
752 if (ret) {
753 ql_log(ql_log_warn, vha, 0xffff,
754 "register_localport failed: ret=%x\n", ret);
755 return;
756 }
757 atomic_set(&vha->nvme_ref_count, 1);
758 vha->nvme_local_port->private = vha;
759 init_waitqueue_head(&vha->nvme_waitQ);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700760}