blob: adeda6a4e4fd3a6ceff725eb961e23d03afb0519 [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"
Duane Grigsbye84067d2017-06-21 13:48:43 -07008#include <linux/scatterlist.h>
9#include <linux/delay.h>
10#include <linux/nvme.h>
11#include <linux/nvme-fc.h>
12
13static struct nvme_fc_port_template qla_nvme_fc_transport;
14
15static void qla_nvme_unregister_remote_port(struct work_struct *);
16
himanshu.madhani@cavium.com0f7e51f2017-07-21 09:32:24 -070017int qla_nvme_register_remote(struct scsi_qla_host *vha, struct fc_port *fcport)
Duane Grigsbye84067d2017-06-21 13:48:43 -070018{
Darren Trapp9dd96862018-03-20 23:09:32 -070019 struct qla_nvme_rport *rport;
20 struct nvme_fc_port_info req;
Duane Grigsbye84067d2017-06-21 13:48:43 -070021 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 (!vha->flags.nvme_enabled) {
27 ql_log(ql_log_info, vha, 0x2100,
28 "%s: Not registering target since Host NVME is not enabled\n",
29 __func__);
30 return 0;
31 }
32
33 if (!(fcport->nvme_prli_service_param &
Darren Trapp9dd96862018-03-20 23:09:32 -070034 (NVME_PRLI_SP_TARGET | NVME_PRLI_SP_DISCOVERY)) ||
35 (fcport->nvme_flag & NVME_FLAG_REGISTERED))
Duane Grigsbye84067d2017-06-21 13:48:43 -070036 return 0;
37
38 INIT_WORK(&fcport->nvme_del_work, qla_nvme_unregister_remote_port);
Darren Trapp870fe242018-03-20 23:09:35 -070039 fcport->nvme_flag &= ~NVME_FLAG_RESETTING;
Duane Grigsbye84067d2017-06-21 13:48:43 -070040
Darren Trapp9dd96862018-03-20 23:09:32 -070041 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;
Duane Grigsbye84067d2017-06-21 13:48:43 -070046
47 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_INITIATOR)
Darren Trapp9dd96862018-03-20 23:09:32 -070048 req.port_role = FC_PORT_ROLE_NVME_INITIATOR;
Duane Grigsbye84067d2017-06-21 13:48:43 -070049
50 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_TARGET)
Darren Trapp9dd96862018-03-20 23:09:32 -070051 req.port_role |= FC_PORT_ROLE_NVME_TARGET;
Duane Grigsbye84067d2017-06-21 13:48:43 -070052
53 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_DISCOVERY)
Darren Trapp9dd96862018-03-20 23:09:32 -070054 req.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
Duane Grigsbye84067d2017-06-21 13:48:43 -070055
Darren Trapp9dd96862018-03-20 23:09:32 -070056 req.port_id = fcport->d_id.b24;
Duane Grigsbye84067d2017-06-21 13:48:43 -070057
58 ql_log(ql_log_info, vha, 0x2102,
Darren Trapd7936a92017-08-23 15:04:59 -070059 "%s: traddr=nn-0x%016llx:pn-0x%016llx PortID:%06x\n",
Darren Trapp9dd96862018-03-20 23:09:32 -070060 __func__, req.node_name, req.port_name,
61 req.port_id);
Duane Grigsbye84067d2017-06-21 13:48:43 -070062
Darren Trapp9dd96862018-03-20 23:09:32 -070063 ret = nvme_fc_register_remoteport(vha->nvme_local_port, &req,
Duane Grigsbye84067d2017-06-21 13:48:43 -070064 &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
Darren Trapp9dd96862018-03-20 23:09:32 -070072 rport = fcport->nvme_remote_port->private;
Duane Grigsbye84067d2017-06-21 13:48:43 -070073 rport->fcport = fcport;
74 list_add_tail(&rport->list, &vha->nvme_rport_list);
Darren Trapp9dd96862018-03-20 23:09:32 -070075
76 fcport->nvme_flag |= NVME_FLAG_REGISTERED;
Duane Grigsbye84067d2017-06-21 13:48:43 -070077 return 0;
78}
79
80/* Allocate a queue for NVMe traffic */
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -070081static int qla_nvme_alloc_queue(struct nvme_fc_local_port *lport,
82 unsigned int qidx, u16 qsize, void **handle)
Duane Grigsbye84067d2017-06-21 13:48:43 -070083{
84 struct scsi_qla_host *vha;
85 struct qla_hw_data *ha;
86 struct qla_qpair *qpair;
87
88 if (!qidx)
89 qidx++;
90
91 vha = (struct scsi_qla_host *)lport->private;
92 ha = vha->hw;
93
94 ql_log(ql_log_info, vha, 0x2104,
95 "%s: handle %p, idx =%d, qsize %d\n",
96 __func__, handle, qidx, qsize);
97
98 if (qidx > qla_nvme_fc_transport.max_hw_queues) {
99 ql_log(ql_log_warn, vha, 0x212f,
100 "%s: Illegal qidx=%d. Max=%d\n",
101 __func__, qidx, qla_nvme_fc_transport.max_hw_queues);
102 return -EINVAL;
103 }
104
105 if (ha->queue_pair_map[qidx]) {
106 *handle = ha->queue_pair_map[qidx];
107 ql_log(ql_log_info, vha, 0x2121,
108 "Returning existing qpair of %p for idx=%x\n",
109 *handle, qidx);
110 return 0;
111 }
112
Duane Grigsbye84067d2017-06-21 13:48:43 -0700113 qpair = qla2xxx_create_qpair(vha, 5, vha->vp_idx, true);
114 if (qpair == NULL) {
115 ql_log(ql_log_warn, vha, 0x2122,
116 "Failed to allocate qpair\n");
117 return -EINVAL;
118 }
119 *handle = qpair;
120
121 return 0;
122}
123
124static void qla_nvme_sp_ls_done(void *ptr, int res)
125{
126 srb_t *sp = ptr;
127 struct srb_iocb *nvme;
128 struct nvmefc_ls_req *fd;
129 struct nvme_private *priv;
130
131 if (atomic_read(&sp->ref_count) == 0) {
132 ql_log(ql_log_warn, sp->fcport->vha, 0x2123,
133 "SP reference-count to ZERO on LS_done -- sp=%p.\n", sp);
134 return;
135 }
136
137 if (!atomic_dec_and_test(&sp->ref_count))
138 return;
139
140 if (res)
141 res = -EINVAL;
142
143 nvme = &sp->u.iocb_cmd;
144 fd = nvme->u.nvme.desc;
145 priv = fd->private;
146 priv->comp_status = res;
147 schedule_work(&priv->ls_work);
148 /* work schedule doesn't need the sp */
149 qla2x00_rel_sp(sp);
150}
151
Duane Grigsbycf19c452017-08-23 15:04:58 -0700152void qla_nvme_cmpl_io(struct srb_iocb *nvme)
153{
154 srb_t *sp;
155 struct nvmefc_fcp_req *fd = nvme->u.nvme.desc;
156
157 sp = container_of(nvme, srb_t, u.iocb_cmd);
158 fd->done(fd);
159 qla2xxx_rel_qpair_sp(sp->qpair, sp);
160}
161
Duane Grigsbye84067d2017-06-21 13:48:43 -0700162static void qla_nvme_sp_done(void *ptr, int res)
163{
164 srb_t *sp = ptr;
165 struct srb_iocb *nvme;
166 struct nvmefc_fcp_req *fd;
167
168 nvme = &sp->u.iocb_cmd;
169 fd = nvme->u.nvme.desc;
170
171 if (!atomic_dec_and_test(&sp->ref_count))
172 return;
173
Darren Trapp9dd96862018-03-20 23:09:32 -0700174 if (res == QLA_SUCCESS)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700175 fd->status = 0;
Darren Trapp9dd96862018-03-20 23:09:32 -0700176 else
177 fd->status = NVME_SC_INTERNAL;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700178
179 fd->rcv_rsplen = nvme->u.nvme.rsp_pyld_len;
Duane Grigsbycf19c452017-08-23 15:04:58 -0700180 list_add_tail(&nvme->u.nvme.entry, &sp->qpair->nvme_done_list);
Darren Trapp9dd96862018-03-20 23:09:32 -0700181
Duane Grigsbycf19c452017-08-23 15:04:58 -0700182 return;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700183}
184
Darren Trappe473b302018-03-20 23:09:33 -0700185static void qla_nvme_abort_work(struct work_struct *work)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700186{
Darren Trappe473b302018-03-20 23:09:33 -0700187 struct nvme_private *priv =
188 container_of(work, struct nvme_private, abort_work);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700189 srb_t *sp = priv->sp;
Darren Trappe473b302018-03-20 23:09:33 -0700190 fc_port_t *fcport = sp->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700191 struct qla_hw_data *ha = fcport->vha->hw;
Darren Trappe473b302018-03-20 23:09:33 -0700192 int rval;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700193
194 rval = ha->isp_ops->abort_command(sp);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700195
196 ql_dbg(ql_dbg_io, fcport->vha, 0x212b,
Darren Trapp870fe242018-03-20 23:09:35 -0700197 "%s: %s command for sp=%p, handle=%x on fcport=%p rval=%x\n",
198 __func__, (rval != QLA_SUCCESS) ? "Failed to abort" : "Aborted",
199 sp, sp->handle, fcport, rval);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700200}
201
Darren Trappe473b302018-03-20 23:09:33 -0700202static void qla_nvme_ls_abort(struct nvme_fc_local_port *lport,
203 struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
204{
205 struct nvme_private *priv = fd->private;
206
207 INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
208 schedule_work(&priv->abort_work);
209}
210
Duane Grigsbye84067d2017-06-21 13:48:43 -0700211static void qla_nvme_ls_complete(struct work_struct *work)
212{
213 struct nvme_private *priv =
214 container_of(work, struct nvme_private, ls_work);
215 struct nvmefc_ls_req *fd = priv->fd;
216
217 fd->done(fd, priv->comp_status);
218}
219
220static int qla_nvme_ls_req(struct nvme_fc_local_port *lport,
221 struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
222{
Darren Trapp9dd96862018-03-20 23:09:32 -0700223 struct qla_nvme_rport *qla_rport = rport->private;
224 fc_port_t *fcport = qla_rport->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700225 struct srb_iocb *nvme;
226 struct nvme_private *priv = fd->private;
227 struct scsi_qla_host *vha;
228 int rval = QLA_FUNCTION_FAILED;
229 struct qla_hw_data *ha;
230 srb_t *sp;
231
Duane Grigsbye84067d2017-06-21 13:48:43 -0700232 vha = fcport->vha;
233 ha = vha->hw;
234 /* Alloc SRB structure */
235 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
236 if (!sp)
237 return rval;
238
239 sp->type = SRB_NVME_LS;
240 sp->name = "nvme_ls";
241 sp->done = qla_nvme_sp_ls_done;
242 atomic_set(&sp->ref_count, 1);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700243 nvme = &sp->u.iocb_cmd;
244 priv->sp = sp;
245 priv->fd = fd;
246 INIT_WORK(&priv->ls_work, qla_nvme_ls_complete);
247 nvme->u.nvme.desc = fd;
248 nvme->u.nvme.dir = 0;
249 nvme->u.nvme.dl = 0;
250 nvme->u.nvme.cmd_len = fd->rqstlen;
251 nvme->u.nvme.rsp_len = fd->rsplen;
252 nvme->u.nvme.rsp_dma = fd->rspdma;
253 nvme->u.nvme.timeout_sec = fd->timeout;
254 nvme->u.nvme.cmd_dma = dma_map_single(&ha->pdev->dev, fd->rqstaddr,
255 fd->rqstlen, DMA_TO_DEVICE);
256 dma_sync_single_for_device(&ha->pdev->dev, nvme->u.nvme.cmd_dma,
257 fd->rqstlen, DMA_TO_DEVICE);
258
259 rval = qla2x00_start_sp(sp);
260 if (rval != QLA_SUCCESS) {
261 ql_log(ql_log_warn, vha, 0x700e,
262 "qla2x00_start_sp failed = %d\n", rval);
263 atomic_dec(&sp->ref_count);
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700264 wake_up(&sp->nvme_ls_waitq);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700265 return rval;
266 }
267
268 return rval;
269}
270
271static void qla_nvme_fcp_abort(struct nvme_fc_local_port *lport,
272 struct nvme_fc_remote_port *rport, void *hw_queue_handle,
273 struct nvmefc_fcp_req *fd)
274{
275 struct nvme_private *priv = fd->private;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700276
Darren Trappe473b302018-03-20 23:09:33 -0700277 INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
278 schedule_work(&priv->abort_work);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700279}
280
281static void qla_nvme_poll(struct nvme_fc_local_port *lport, void *hw_queue_handle)
282{
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700283 struct qla_qpair *qpair = hw_queue_handle;
Darren Trapp9dd96862018-03-20 23:09:32 -0700284 unsigned long flags;
285 struct scsi_qla_host *vha = lport->private;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700286
Duane Grigsbye84067d2017-06-21 13:48:43 -0700287 spin_lock_irqsave(&qpair->qp_lock, flags);
288 qla24xx_process_response_queue(vha, qpair->rsp);
289 spin_unlock_irqrestore(&qpair->qp_lock, flags);
290}
291
292static int qla2x00_start_nvme_mq(srb_t *sp)
293{
294 unsigned long flags;
295 uint32_t *clr_ptr;
296 uint32_t index;
297 uint32_t handle;
298 struct cmd_nvme *cmd_pkt;
299 uint16_t cnt, i;
300 uint16_t req_cnt;
301 uint16_t tot_dsds;
302 uint16_t avail_dsds;
303 uint32_t *cur_dsd;
304 struct req_que *req = NULL;
305 struct scsi_qla_host *vha = sp->fcport->vha;
306 struct qla_hw_data *ha = vha->hw;
307 struct qla_qpair *qpair = sp->qpair;
308 struct srb_iocb *nvme = &sp->u.iocb_cmd;
309 struct scatterlist *sgl, *sg;
310 struct nvmefc_fcp_req *fd = nvme->u.nvme.desc;
311 uint32_t rval = QLA_SUCCESS;
312
himanshu.madhani@cavium.com1d4614e2018-03-20 23:09:30 -0700313 /* Setup qpair pointers */
314 req = qpair->req;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700315 tot_dsds = fd->sg_cnt;
316
317 /* Acquire qpair specific lock */
318 spin_lock_irqsave(&qpair->qp_lock, flags);
319
320 /* Check for room in outstanding command list. */
321 handle = req->current_outstanding_cmd;
322 for (index = 1; index < req->num_outstanding_cmds; index++) {
323 handle++;
324 if (handle == req->num_outstanding_cmds)
325 handle = 1;
326 if (!req->outstanding_cmds[handle])
327 break;
328 }
329
330 if (index == req->num_outstanding_cmds) {
Darren Trapp870fe242018-03-20 23:09:35 -0700331 rval = -EBUSY;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700332 goto queuing_error;
333 }
334 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
335 if (req->cnt < (req_cnt + 2)) {
336 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
337 RD_REG_DWORD_RELAXED(req->req_q_out);
338
339 if (req->ring_index < cnt)
340 req->cnt = cnt - req->ring_index;
341 else
342 req->cnt = req->length - (req->ring_index - cnt);
343
344 if (req->cnt < (req_cnt + 2)){
Darren Trapp870fe242018-03-20 23:09:35 -0700345 rval = -EBUSY;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700346 goto queuing_error;
347 }
348 }
349
350 if (unlikely(!fd->sqid)) {
351 struct nvme_fc_cmd_iu *cmd = fd->cmdaddr;
352 if (cmd->sqe.common.opcode == nvme_admin_async_event) {
353 nvme->u.nvme.aen_op = 1;
himanshu.madhani@cavium.com1d4614e2018-03-20 23:09:30 -0700354 atomic_inc(&ha->nvme_active_aen_cnt);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700355 }
356 }
357
358 /* Build command packet. */
359 req->current_outstanding_cmd = handle;
360 req->outstanding_cmds[handle] = sp;
361 sp->handle = handle;
362 req->cnt -= req_cnt;
363
364 cmd_pkt = (struct cmd_nvme *)req->ring_ptr;
365 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
366
367 /* Zero out remaining portion of packet. */
368 clr_ptr = (uint32_t *)cmd_pkt + 2;
369 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
370
371 cmd_pkt->entry_status = 0;
372
373 /* Update entry type to indicate Command NVME IOCB */
374 cmd_pkt->entry_type = COMMAND_NVME;
375
376 /* No data transfer how do we check buffer len == 0?? */
377 if (fd->io_dir == NVMEFC_FCP_READ) {
378 cmd_pkt->control_flags =
379 cpu_to_le16(CF_READ_DATA | CF_NVME_ENABLE);
380 vha->qla_stats.input_bytes += fd->payload_length;
381 vha->qla_stats.input_requests++;
382 } else if (fd->io_dir == NVMEFC_FCP_WRITE) {
383 cmd_pkt->control_flags =
384 cpu_to_le16(CF_WRITE_DATA | CF_NVME_ENABLE);
385 vha->qla_stats.output_bytes += fd->payload_length;
386 vha->qla_stats.output_requests++;
387 } else if (fd->io_dir == 0) {
388 cmd_pkt->control_flags = cpu_to_le16(CF_NVME_ENABLE);
389 }
390
391 /* Set NPORT-ID */
392 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
393 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
394 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
395 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
396 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
397
398 /* NVME RSP IU */
399 cmd_pkt->nvme_rsp_dsd_len = cpu_to_le16(fd->rsplen);
400 cmd_pkt->nvme_rsp_dseg_address[0] = cpu_to_le32(LSD(fd->rspdma));
401 cmd_pkt->nvme_rsp_dseg_address[1] = cpu_to_le32(MSD(fd->rspdma));
402
403 /* NVME CNMD IU */
404 cmd_pkt->nvme_cmnd_dseg_len = cpu_to_le16(fd->cmdlen);
405 cmd_pkt->nvme_cmnd_dseg_address[0] = cpu_to_le32(LSD(fd->cmddma));
406 cmd_pkt->nvme_cmnd_dseg_address[1] = cpu_to_le32(MSD(fd->cmddma));
407
408 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
409 cmd_pkt->byte_count = cpu_to_le32(fd->payload_length);
410
411 /* One DSD is available in the Command Type NVME IOCB */
412 avail_dsds = 1;
413 cur_dsd = (uint32_t *)&cmd_pkt->nvme_data_dseg_address[0];
414 sgl = fd->first_sgl;
415
416 /* Load data segments */
417 for_each_sg(sgl, sg, tot_dsds, i) {
418 dma_addr_t sle_dma;
419 cont_a64_entry_t *cont_pkt;
420
421 /* Allocate additional continuation packets? */
422 if (avail_dsds == 0) {
423 /*
424 * Five DSDs are available in the Continuation
425 * Type 1 IOCB.
426 */
427
428 /* Adjust ring index */
429 req->ring_index++;
430 if (req->ring_index == req->length) {
431 req->ring_index = 0;
432 req->ring_ptr = req->ring;
433 } else {
434 req->ring_ptr++;
435 }
436 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Himanshu Madhanic345c6c2017-06-30 19:32:53 -0700437 *((uint32_t *)(&cont_pkt->entry_type)) =
438 cpu_to_le32(CONTINUE_A64_TYPE);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700439
440 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
441 avail_dsds = 5;
442 }
443
444 sle_dma = sg_dma_address(sg);
445 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
446 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
447 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
448 avail_dsds--;
449 }
450
451 /* Set total entry count. */
452 cmd_pkt->entry_count = (uint8_t)req_cnt;
453 wmb();
454
455 /* Adjust ring index. */
456 req->ring_index++;
457 if (req->ring_index == req->length) {
458 req->ring_index = 0;
459 req->ring_ptr = req->ring;
460 } else {
461 req->ring_ptr++;
462 }
463
464 /* Set chip new ring index. */
465 WRT_REG_DWORD(req->req_q_in, req->ring_index);
466
467queuing_error:
468 spin_unlock_irqrestore(&qpair->qp_lock, flags);
469 return rval;
470}
471
472/* Post a command */
473static int qla_nvme_post_cmd(struct nvme_fc_local_port *lport,
474 struct nvme_fc_remote_port *rport, void *hw_queue_handle,
475 struct nvmefc_fcp_req *fd)
476{
477 fc_port_t *fcport;
478 struct srb_iocb *nvme;
479 struct scsi_qla_host *vha;
Darren Trapp870fe242018-03-20 23:09:35 -0700480 int rval = -ENODEV;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700481 srb_t *sp;
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700482 struct qla_qpair *qpair = hw_queue_handle;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700483 struct nvme_private *priv;
Darren Trapp9dd96862018-03-20 23:09:32 -0700484 struct qla_nvme_rport *qla_rport = rport->private;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700485
Darren Trapp870fe242018-03-20 23:09:35 -0700486 if (!fd || !qpair) {
487 ql_log(ql_log_warn, NULL, 0x2134,
488 "NO NVMe request or Queue Handle\n");
Duane Grigsbye84067d2017-06-21 13:48:43 -0700489 return rval;
490 }
491
492 priv = fd->private;
Darren Trapp9dd96862018-03-20 23:09:32 -0700493 fcport = qla_rport->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700494 if (!fcport) {
495 ql_log(ql_log_warn, NULL, 0x210e, "No fcport ptr\n");
496 return rval;
497 }
498
499 vha = fcport->vha;
Darren Trapp870fe242018-03-20 23:09:35 -0700500
501 /*
502 * If we know the dev is going away while the transport is still sending
503 * IO's return busy back to stall the IO Q. This happens when the
504 * link goes away and fw hasn't notified us yet, but IO's are being
505 * returned. If the dev comes back quickly we won't exhaust the IO
506 * retry count at the core.
507 */
508 if (fcport->nvme_flag & NVME_FLAG_RESETTING)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700509 return -EBUSY;
510
511 /* Alloc SRB structure */
512 sp = qla2xxx_get_qpair_sp(qpair, fcport, GFP_ATOMIC);
513 if (!sp)
Darren Trapp870fe242018-03-20 23:09:35 -0700514 return -EBUSY;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700515
516 atomic_set(&sp->ref_count, 1);
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700517 init_waitqueue_head(&sp->nvme_ls_waitq);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700518 priv->sp = sp;
519 sp->type = SRB_NVME_CMD;
520 sp->name = "nvme_cmd";
521 sp->done = qla_nvme_sp_done;
522 sp->qpair = qpair;
523 nvme = &sp->u.iocb_cmd;
524 nvme->u.nvme.desc = fd;
525
526 rval = qla2x00_start_nvme_mq(sp);
527 if (rval != QLA_SUCCESS) {
528 ql_log(ql_log_warn, vha, 0x212d,
529 "qla2x00_start_nvme_mq failed = %d\n", rval);
530 atomic_dec(&sp->ref_count);
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700531 wake_up(&sp->nvme_ls_waitq);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700532 }
533
534 return rval;
535}
536
537static void qla_nvme_localport_delete(struct nvme_fc_local_port *lport)
538{
539 struct scsi_qla_host *vha = lport->private;
540
Duane Grigsbye84067d2017-06-21 13:48:43 -0700541 ql_log(ql_log_info, vha, 0x210f,
542 "localport delete of %p completed.\n", vha->nvme_local_port);
543 vha->nvme_local_port = NULL;
himanshu.madhani@cavium.com5621b0d2017-07-21 09:32:26 -0700544 complete(&vha->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700545}
546
547static void qla_nvme_remoteport_delete(struct nvme_fc_remote_port *rport)
548{
549 fc_port_t *fcport;
Darren Trapp9dd96862018-03-20 23:09:32 -0700550 struct qla_nvme_rport *qla_rport = rport->private, *trport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700551
Darren Trapp9dd96862018-03-20 23:09:32 -0700552 fcport = qla_rport->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700553 fcport->nvme_remote_port = NULL;
554 fcport->nvme_flag &= ~NVME_FLAG_REGISTERED;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700555
Darren Trapp9dd96862018-03-20 23:09:32 -0700556 list_for_each_entry_safe(qla_rport, trport,
Duane Grigsbye84067d2017-06-21 13:48:43 -0700557 &fcport->vha->nvme_rport_list, list) {
Darren Trapp9dd96862018-03-20 23:09:32 -0700558 if (qla_rport->fcport == fcport) {
559 list_del(&qla_rport->list);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700560 break;
561 }
562 }
himanshu.madhani@cavium.com5621b0d2017-07-21 09:32:26 -0700563 complete(&fcport->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700564
Darren Trapp9dd96862018-03-20 23:09:32 -0700565 if (!test_bit(UNLOADING, &fcport->vha->dpc_flags)) {
566 INIT_WORK(&fcport->free_work, qlt_free_session_done);
567 schedule_work(&fcport->free_work);
568 }
569
570 fcport->nvme_flag &= ~(NVME_FLAG_REGISTERED | NVME_FLAG_DELETING);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700571 ql_log(ql_log_info, fcport->vha, 0x2110,
572 "remoteport_delete of %p completed.\n", fcport);
573}
574
575static struct nvme_fc_port_template qla_nvme_fc_transport = {
576 .localport_delete = qla_nvme_localport_delete,
577 .remoteport_delete = qla_nvme_remoteport_delete,
578 .create_queue = qla_nvme_alloc_queue,
579 .delete_queue = NULL,
580 .ls_req = qla_nvme_ls_req,
581 .ls_abort = qla_nvme_ls_abort,
582 .fcp_io = qla_nvme_post_cmd,
583 .fcp_abort = qla_nvme_fcp_abort,
584 .poll_queue = qla_nvme_poll,
585 .max_hw_queues = 8,
586 .max_sgl_segments = 128,
587 .max_dif_sgl_segments = 64,
588 .dma_boundary = 0xFFFFFFFF,
589 .local_priv_sz = 8,
Darren Trapp9dd96862018-03-20 23:09:32 -0700590 .remote_priv_sz = sizeof(struct qla_nvme_rport),
Duane Grigsbye84067d2017-06-21 13:48:43 -0700591 .lsrqst_priv_sz = sizeof(struct nvme_private),
592 .fcprqst_priv_sz = sizeof(struct nvme_private),
593};
594
595#define NVME_ABORT_POLLING_PERIOD 2
596static int qla_nvme_wait_on_command(srb_t *sp)
597{
598 int ret = QLA_SUCCESS;
599
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700600 wait_event_timeout(sp->nvme_ls_waitq, (atomic_read(&sp->ref_count) > 1),
Duane Grigsbye84067d2017-06-21 13:48:43 -0700601 NVME_ABORT_POLLING_PERIOD*HZ);
602
603 if (atomic_read(&sp->ref_count) > 1)
604 ret = QLA_FUNCTION_FAILED;
605
606 return ret;
607}
608
himanshu.madhani@cavium.com0f7e51f2017-07-21 09:32:24 -0700609void qla_nvme_abort(struct qla_hw_data *ha, struct srb *sp)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700610{
611 int rval;
612
613 rval = ha->isp_ops->abort_command(sp);
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700614 if (!rval && !qla_nvme_wait_on_command(sp))
615 ql_log(ql_log_warn, NULL, 0x2112,
616 "nvme_wait_on_comand timed out waiting on sp=%p\n", sp);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700617}
618
Duane Grigsbye84067d2017-06-21 13:48:43 -0700619static void qla_nvme_unregister_remote_port(struct work_struct *work)
620{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700621 struct fc_port *fcport = container_of(work, struct fc_port,
622 nvme_del_work);
Darren Trapp9dd96862018-03-20 23:09:32 -0700623 struct qla_nvme_rport *qla_rport, *trport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700624
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200625 if (!IS_ENABLED(CONFIG_NVME_FC))
626 return;
627
himanshu.madhani@cavium.com49b3d5f62017-07-21 09:32:27 -0700628 ql_log(ql_log_warn, NULL, 0x2112,
629 "%s: unregister remoteport on %p\n",__func__, fcport);
630
Darren Trapp9dd96862018-03-20 23:09:32 -0700631 list_for_each_entry_safe(qla_rport, trport,
Duane Grigsbye84067d2017-06-21 13:48:43 -0700632 &fcport->vha->nvme_rport_list, list) {
Darren Trapp9dd96862018-03-20 23:09:32 -0700633 if (qla_rport->fcport == fcport) {
Duane Grigsbye84067d2017-06-21 13:48:43 -0700634 ql_log(ql_log_info, fcport->vha, 0x2113,
635 "%s: fcport=%p\n", __func__, fcport);
himanshu.madhani@cavium.com49b3d5f62017-07-21 09:32:27 -0700636 init_completion(&fcport->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700637 nvme_fc_unregister_remoteport(
638 fcport->nvme_remote_port);
Darren Trapp9dd96862018-03-20 23:09:32 -0700639 wait_for_completion(&fcport->nvme_del_done);
640 break;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700641 }
642 }
Duane Grigsbye84067d2017-06-21 13:48:43 -0700643}
644
himanshu.madhani@cavium.com0f7e51f2017-07-21 09:32:24 -0700645void qla_nvme_delete(struct scsi_qla_host *vha)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700646{
Darren Trapp9dd96862018-03-20 23:09:32 -0700647 struct qla_nvme_rport *qla_rport, *trport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700648 fc_port_t *fcport;
649 int nv_ret;
650
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200651 if (!IS_ENABLED(CONFIG_NVME_FC))
652 return;
653
Darren Trapp9dd96862018-03-20 23:09:32 -0700654 list_for_each_entry_safe(qla_rport, trport,
655 &vha->nvme_rport_list, list) {
656 fcport = qla_rport->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700657
658 ql_log(ql_log_info, fcport->vha, 0x2114, "%s: fcport=%p\n",
659 __func__, fcport);
660
Darren Trappe473b302018-03-20 23:09:33 -0700661 nvme_fc_set_remoteport_devloss(fcport->nvme_remote_port, 0);
himanshu.madhani@cavium.com5621b0d2017-07-21 09:32:26 -0700662 init_completion(&fcport->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700663 nvme_fc_unregister_remoteport(fcport->nvme_remote_port);
Darren Trapp9dd96862018-03-20 23:09:32 -0700664 wait_for_completion(&fcport->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700665 }
666
667 if (vha->nvme_local_port) {
himanshu.madhani@cavium.com5621b0d2017-07-21 09:32:26 -0700668 init_completion(&vha->nvme_del_done);
Darren Trapp9dd96862018-03-20 23:09:32 -0700669 ql_log(ql_log_info, vha, 0x2116,
670 "unregister localport=%p\n",
671 vha->nvme_local_port);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700672 nv_ret = nvme_fc_unregister_localport(vha->nvme_local_port);
Darren Trapp9dd96862018-03-20 23:09:32 -0700673 if (nv_ret)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700674 ql_log(ql_log_info, vha, 0x2115,
675 "Unregister of localport failed\n");
Darren Trapp9dd96862018-03-20 23:09:32 -0700676 else
677 wait_for_completion(&vha->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700678 }
Duane Grigsbye84067d2017-06-21 13:48:43 -0700679}
680
himanshu.madhani@cavium.com0f7e51f2017-07-21 09:32:24 -0700681void qla_nvme_register_hba(struct scsi_qla_host *vha)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700682{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700683 struct nvme_fc_port_template *tmpl;
684 struct qla_hw_data *ha;
685 struct nvme_fc_port_info pinfo;
686 int ret;
687
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200688 if (!IS_ENABLED(CONFIG_NVME_FC))
689 return;
690
Duane Grigsbye84067d2017-06-21 13:48:43 -0700691 ha = vha->hw;
692 tmpl = &qla_nvme_fc_transport;
693
694 WARN_ON(vha->nvme_local_port);
695 WARN_ON(ha->max_req_queues < 3);
696
697 qla_nvme_fc_transport.max_hw_queues =
698 min((uint8_t)(qla_nvme_fc_transport.max_hw_queues),
699 (uint8_t)(ha->max_req_queues - 2));
700
701 pinfo.node_name = wwn_to_u64(vha->node_name);
702 pinfo.port_name = wwn_to_u64(vha->port_name);
703 pinfo.port_role = FC_PORT_ROLE_NVME_INITIATOR;
704 pinfo.port_id = vha->d_id.b24;
705
706 ql_log(ql_log_info, vha, 0xffff,
Darren Trapd7936a92017-08-23 15:04:59 -0700707 "register_localport: host-traddr=nn-0x%llx:pn-0x%llx on portID:%x\n",
708 pinfo.node_name, pinfo.port_name, pinfo.port_id);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700709 qla_nvme_fc_transport.dma_boundary = vha->host->dma_boundary;
710
711 ret = nvme_fc_register_localport(&pinfo, tmpl,
712 get_device(&ha->pdev->dev), &vha->nvme_local_port);
713 if (ret) {
714 ql_log(ql_log_warn, vha, 0xffff,
715 "register_localport failed: ret=%x\n", ret);
716 return;
717 }
Duane Grigsbye84067d2017-06-21 13:48:43 -0700718 vha->nvme_local_port->private = vha;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700719}