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