blob: 0829ab7f0d54da8a10d1682fbf6e0ea92d542168 [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
Quinn Tran8777e432018-08-02 13:16:57 -070033 if (!vha->nvme_local_port && qla_nvme_register_hba(vha))
34 return 0;
35
Duane Grigsbye84067d2017-06-21 13:48:43 -070036 if (!(fcport->nvme_prli_service_param &
Darren Trapp9dd96862018-03-20 23:09:32 -070037 (NVME_PRLI_SP_TARGET | NVME_PRLI_SP_DISCOVERY)) ||
38 (fcport->nvme_flag & NVME_FLAG_REGISTERED))
Duane Grigsbye84067d2017-06-21 13:48:43 -070039 return 0;
40
41 INIT_WORK(&fcport->nvme_del_work, qla_nvme_unregister_remote_port);
Darren Trapp870fe242018-03-20 23:09:35 -070042 fcport->nvme_flag &= ~NVME_FLAG_RESETTING;
Duane Grigsbye84067d2017-06-21 13:48:43 -070043
Darren Trapp9dd96862018-03-20 23:09:32 -070044 memset(&req, 0, sizeof(struct nvme_fc_port_info));
45 req.port_name = wwn_to_u64(fcport->port_name);
46 req.node_name = wwn_to_u64(fcport->node_name);
47 req.port_role = 0;
48 req.dev_loss_tmo = NVME_FC_DEV_LOSS_TMO;
Duane Grigsbye84067d2017-06-21 13:48:43 -070049
50 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_INITIATOR)
Darren Trapp9dd96862018-03-20 23:09:32 -070051 req.port_role = FC_PORT_ROLE_NVME_INITIATOR;
Duane Grigsbye84067d2017-06-21 13:48:43 -070052
53 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_TARGET)
Darren Trapp9dd96862018-03-20 23:09:32 -070054 req.port_role |= FC_PORT_ROLE_NVME_TARGET;
Duane Grigsbye84067d2017-06-21 13:48:43 -070055
56 if (fcport->nvme_prli_service_param & NVME_PRLI_SP_DISCOVERY)
Darren Trapp9dd96862018-03-20 23:09:32 -070057 req.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
Duane Grigsbye84067d2017-06-21 13:48:43 -070058
Darren Trapp9dd96862018-03-20 23:09:32 -070059 req.port_id = fcport->d_id.b24;
Duane Grigsbye84067d2017-06-21 13:48:43 -070060
61 ql_log(ql_log_info, vha, 0x2102,
Darren Trapd7936a92017-08-23 15:04:59 -070062 "%s: traddr=nn-0x%016llx:pn-0x%016llx PortID:%06x\n",
Darren Trapp9dd96862018-03-20 23:09:32 -070063 __func__, req.node_name, req.port_name,
64 req.port_id);
Duane Grigsbye84067d2017-06-21 13:48:43 -070065
Darren Trapp9dd96862018-03-20 23:09:32 -070066 ret = nvme_fc_register_remoteport(vha->nvme_local_port, &req,
Duane Grigsbye84067d2017-06-21 13:48:43 -070067 &fcport->nvme_remote_port);
68 if (ret) {
69 ql_log(ql_log_warn, vha, 0x212e,
70 "Failed to register remote port. Transport returned %d\n",
71 ret);
72 return ret;
73 }
74
Darren Trapp9dd96862018-03-20 23:09:32 -070075 rport = fcport->nvme_remote_port->private;
Duane Grigsbye84067d2017-06-21 13:48:43 -070076 rport->fcport = fcport;
77 list_add_tail(&rport->list, &vha->nvme_rport_list);
Darren Trapp9dd96862018-03-20 23:09:32 -070078
79 fcport->nvme_flag |= NVME_FLAG_REGISTERED;
Duane Grigsbye84067d2017-06-21 13:48:43 -070080 return 0;
81}
82
83/* Allocate a queue for NVMe traffic */
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -070084static int qla_nvme_alloc_queue(struct nvme_fc_local_port *lport,
85 unsigned int qidx, u16 qsize, void **handle)
Duane Grigsbye84067d2017-06-21 13:48:43 -070086{
87 struct scsi_qla_host *vha;
88 struct qla_hw_data *ha;
89 struct qla_qpair *qpair;
90
91 if (!qidx)
92 qidx++;
93
94 vha = (struct scsi_qla_host *)lport->private;
95 ha = vha->hw;
96
97 ql_log(ql_log_info, vha, 0x2104,
98 "%s: handle %p, idx =%d, qsize %d\n",
99 __func__, handle, qidx, qsize);
100
101 if (qidx > qla_nvme_fc_transport.max_hw_queues) {
102 ql_log(ql_log_warn, vha, 0x212f,
103 "%s: Illegal qidx=%d. Max=%d\n",
104 __func__, qidx, qla_nvme_fc_transport.max_hw_queues);
105 return -EINVAL;
106 }
107
108 if (ha->queue_pair_map[qidx]) {
109 *handle = ha->queue_pair_map[qidx];
110 ql_log(ql_log_info, vha, 0x2121,
111 "Returning existing qpair of %p for idx=%x\n",
112 *handle, qidx);
113 return 0;
114 }
115
Duane Grigsbye84067d2017-06-21 13:48:43 -0700116 qpair = qla2xxx_create_qpair(vha, 5, vha->vp_idx, true);
117 if (qpair == NULL) {
118 ql_log(ql_log_warn, vha, 0x2122,
119 "Failed to allocate qpair\n");
120 return -EINVAL;
121 }
122 *handle = qpair;
123
124 return 0;
125}
126
127static void qla_nvme_sp_ls_done(void *ptr, int res)
128{
129 srb_t *sp = ptr;
130 struct srb_iocb *nvme;
131 struct nvmefc_ls_req *fd;
132 struct nvme_private *priv;
133
134 if (atomic_read(&sp->ref_count) == 0) {
135 ql_log(ql_log_warn, sp->fcport->vha, 0x2123,
136 "SP reference-count to ZERO on LS_done -- sp=%p.\n", sp);
137 return;
138 }
139
Bart Van Assche219d27d2019-04-17 14:44:35 -0700140 atomic_dec(&sp->ref_count);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700141
142 if (res)
143 res = -EINVAL;
144
145 nvme = &sp->u.iocb_cmd;
146 fd = nvme->u.nvme.desc;
147 priv = fd->private;
148 priv->comp_status = res;
149 schedule_work(&priv->ls_work);
150 /* work schedule doesn't need the sp */
151 qla2x00_rel_sp(sp);
152}
153
154static void qla_nvme_sp_done(void *ptr, int res)
155{
156 srb_t *sp = ptr;
157 struct srb_iocb *nvme;
158 struct nvmefc_fcp_req *fd;
159
160 nvme = &sp->u.iocb_cmd;
161 fd = nvme->u.nvme.desc;
162
Bart Van Assche219d27d2019-04-17 14:44:35 -0700163 atomic_dec(&sp->ref_count);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700164
Giridhar Malavalib2d1453a2019-04-02 14:24:32 -0700165 if (res == QLA_SUCCESS) {
166 fd->rcv_rsplen = nvme->u.nvme.rsp_pyld_len;
167 } else {
168 fd->rcv_rsplen = 0;
169 fd->transferred_length = 0;
170 }
171 fd->status = 0;
Darren Trapp2e4c5d22018-03-20 23:09:36 -0700172 fd->done(fd);
173 qla2xxx_rel_qpair_sp(sp->qpair, sp);
Darren Trapp9dd96862018-03-20 23:09:32 -0700174
Duane Grigsbycf19c452017-08-23 15:04:58 -0700175 return;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700176}
177
Darren Trappe473b302018-03-20 23:09:33 -0700178static void qla_nvme_abort_work(struct work_struct *work)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700179{
Darren Trappe473b302018-03-20 23:09:33 -0700180 struct nvme_private *priv =
181 container_of(work, struct nvme_private, abort_work);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700182 srb_t *sp = priv->sp;
Darren Trappe473b302018-03-20 23:09:33 -0700183 fc_port_t *fcport = sp->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700184 struct qla_hw_data *ha = fcport->vha->hw;
Darren Trappe473b302018-03-20 23:09:33 -0700185 int rval;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700186
Bart Van Asschedafbe562019-04-17 14:44:18 -0700187 ql_dbg(ql_dbg_io, fcport->vha, 0xffff,
188 "%s called for sp=%p, hndl=%x on fcport=%p deleted=%d\n",
189 __func__, sp, sp->handle, fcport, fcport->deleted);
Himanshu Madhani471f8e02019-02-15 14:37:15 -0800190
191 if (!ha->flags.fw_started && (fcport && fcport->deleted))
192 return;
193
Giridhar Malavalib2d1453a2019-04-02 14:24:32 -0700194 if (ha->flags.host_shutting_down) {
195 ql_log(ql_log_info, sp->fcport->vha, 0xffff,
196 "%s Calling done on sp: %p, type: 0x%x, sp->ref_count: 0x%x\n",
197 __func__, sp, sp->type, atomic_read(&sp->ref_count));
198 sp->done(sp, 0);
199 return;
200 }
201
202 if (atomic_read(&sp->ref_count) == 0) {
203 WARN_ON(1);
204 ql_log(ql_log_info, fcport->vha, 0xffff,
Colin Ian Kingef19af92019-04-08 09:20:14 +0100205 "%s: command already aborted on sp: %p\n",
Giridhar Malavalib2d1453a2019-04-02 14:24:32 -0700206 __func__, sp);
207 return;
208 }
209
Duane Grigsbye84067d2017-06-21 13:48:43 -0700210 rval = ha->isp_ops->abort_command(sp);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700211
212 ql_dbg(ql_dbg_io, fcport->vha, 0x212b,
Darren Trapp870fe242018-03-20 23:09:35 -0700213 "%s: %s command for sp=%p, handle=%x on fcport=%p rval=%x\n",
214 __func__, (rval != QLA_SUCCESS) ? "Failed to abort" : "Aborted",
215 sp, sp->handle, fcport, rval);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700216}
217
Darren Trappe473b302018-03-20 23:09:33 -0700218static void qla_nvme_ls_abort(struct nvme_fc_local_port *lport,
219 struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
220{
221 struct nvme_private *priv = fd->private;
222
223 INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
224 schedule_work(&priv->abort_work);
225}
226
Duane Grigsbye84067d2017-06-21 13:48:43 -0700227static void qla_nvme_ls_complete(struct work_struct *work)
228{
229 struct nvme_private *priv =
230 container_of(work, struct nvme_private, ls_work);
231 struct nvmefc_ls_req *fd = priv->fd;
232
233 fd->done(fd, priv->comp_status);
234}
235
236static int qla_nvme_ls_req(struct nvme_fc_local_port *lport,
237 struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
238{
Darren Trapp9dd96862018-03-20 23:09:32 -0700239 struct qla_nvme_rport *qla_rport = rport->private;
240 fc_port_t *fcport = qla_rport->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700241 struct srb_iocb *nvme;
242 struct nvme_private *priv = fd->private;
243 struct scsi_qla_host *vha;
244 int rval = QLA_FUNCTION_FAILED;
245 struct qla_hw_data *ha;
246 srb_t *sp;
247
Duane Grigsbye84067d2017-06-21 13:48:43 -0700248 vha = fcport->vha;
249 ha = vha->hw;
250 /* Alloc SRB structure */
251 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
252 if (!sp)
253 return rval;
254
255 sp->type = SRB_NVME_LS;
256 sp->name = "nvme_ls";
257 sp->done = qla_nvme_sp_ls_done;
258 atomic_set(&sp->ref_count, 1);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700259 nvme = &sp->u.iocb_cmd;
260 priv->sp = sp;
261 priv->fd = fd;
262 INIT_WORK(&priv->ls_work, qla_nvme_ls_complete);
263 nvme->u.nvme.desc = fd;
264 nvme->u.nvme.dir = 0;
265 nvme->u.nvme.dl = 0;
266 nvme->u.nvme.cmd_len = fd->rqstlen;
267 nvme->u.nvme.rsp_len = fd->rsplen;
268 nvme->u.nvme.rsp_dma = fd->rspdma;
269 nvme->u.nvme.timeout_sec = fd->timeout;
270 nvme->u.nvme.cmd_dma = dma_map_single(&ha->pdev->dev, fd->rqstaddr,
271 fd->rqstlen, DMA_TO_DEVICE);
272 dma_sync_single_for_device(&ha->pdev->dev, nvme->u.nvme.cmd_dma,
273 fd->rqstlen, DMA_TO_DEVICE);
274
275 rval = qla2x00_start_sp(sp);
276 if (rval != QLA_SUCCESS) {
277 ql_log(ql_log_warn, vha, 0x700e,
278 "qla2x00_start_sp failed = %d\n", rval);
279 atomic_dec(&sp->ref_count);
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700280 wake_up(&sp->nvme_ls_waitq);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700281 return rval;
282 }
283
284 return rval;
285}
286
287static void qla_nvme_fcp_abort(struct nvme_fc_local_port *lport,
288 struct nvme_fc_remote_port *rport, void *hw_queue_handle,
289 struct nvmefc_fcp_req *fd)
290{
291 struct nvme_private *priv = fd->private;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700292
Darren Trappe473b302018-03-20 23:09:33 -0700293 INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
294 schedule_work(&priv->abort_work);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700295}
296
Darren Trapp60dd6e8e2018-03-20 23:09:39 -0700297static inline int qla2x00_start_nvme_mq(srb_t *sp)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700298{
299 unsigned long flags;
300 uint32_t *clr_ptr;
301 uint32_t index;
302 uint32_t handle;
303 struct cmd_nvme *cmd_pkt;
304 uint16_t cnt, i;
305 uint16_t req_cnt;
306 uint16_t tot_dsds;
307 uint16_t avail_dsds;
308 uint32_t *cur_dsd;
309 struct req_que *req = NULL;
310 struct scsi_qla_host *vha = sp->fcport->vha;
311 struct qla_hw_data *ha = vha->hw;
312 struct qla_qpair *qpair = sp->qpair;
313 struct srb_iocb *nvme = &sp->u.iocb_cmd;
314 struct scatterlist *sgl, *sg;
315 struct nvmefc_fcp_req *fd = nvme->u.nvme.desc;
316 uint32_t rval = QLA_SUCCESS;
317
himanshu.madhani@cavium.com1d4614e2018-03-20 23:09:30 -0700318 /* Setup qpair pointers */
319 req = qpair->req;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700320 tot_dsds = fd->sg_cnt;
321
322 /* Acquire qpair specific lock */
323 spin_lock_irqsave(&qpair->qp_lock, flags);
324
325 /* Check for room in outstanding command list. */
326 handle = req->current_outstanding_cmd;
327 for (index = 1; index < req->num_outstanding_cmds; index++) {
328 handle++;
329 if (handle == req->num_outstanding_cmds)
330 handle = 1;
331 if (!req->outstanding_cmds[handle])
332 break;
333 }
334
335 if (index == req->num_outstanding_cmds) {
Darren Trapp870fe242018-03-20 23:09:35 -0700336 rval = -EBUSY;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700337 goto queuing_error;
338 }
339 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
340 if (req->cnt < (req_cnt + 2)) {
341 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
342 RD_REG_DWORD_RELAXED(req->req_q_out);
343
344 if (req->ring_index < cnt)
345 req->cnt = cnt - req->ring_index;
346 else
347 req->cnt = req->length - (req->ring_index - cnt);
348
349 if (req->cnt < (req_cnt + 2)){
Darren Trapp870fe242018-03-20 23:09:35 -0700350 rval = -EBUSY;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700351 goto queuing_error;
352 }
353 }
354
355 if (unlikely(!fd->sqid)) {
356 struct nvme_fc_cmd_iu *cmd = fd->cmdaddr;
Bart Van Asschebd432bb2019-04-11 14:53:17 -0700357
Duane Grigsbye84067d2017-06-21 13:48:43 -0700358 if (cmd->sqe.common.opcode == nvme_admin_async_event) {
359 nvme->u.nvme.aen_op = 1;
himanshu.madhani@cavium.com1d4614e2018-03-20 23:09:30 -0700360 atomic_inc(&ha->nvme_active_aen_cnt);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700361 }
362 }
363
364 /* Build command packet. */
365 req->current_outstanding_cmd = handle;
366 req->outstanding_cmds[handle] = sp;
367 sp->handle = handle;
368 req->cnt -= req_cnt;
369
370 cmd_pkt = (struct cmd_nvme *)req->ring_ptr;
371 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
372
373 /* Zero out remaining portion of packet. */
374 clr_ptr = (uint32_t *)cmd_pkt + 2;
375 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
376
377 cmd_pkt->entry_status = 0;
378
379 /* Update entry type to indicate Command NVME IOCB */
380 cmd_pkt->entry_type = COMMAND_NVME;
381
382 /* No data transfer how do we check buffer len == 0?? */
383 if (fd->io_dir == NVMEFC_FCP_READ) {
Darren Trapp03aaa892019-02-15 14:37:13 -0800384 cmd_pkt->control_flags = CF_READ_DATA;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700385 vha->qla_stats.input_bytes += fd->payload_length;
386 vha->qla_stats.input_requests++;
387 } else if (fd->io_dir == NVMEFC_FCP_WRITE) {
Darren Trapp03aaa892019-02-15 14:37:13 -0800388 cmd_pkt->control_flags = CF_WRITE_DATA;
389 if ((vha->flags.nvme_first_burst) &&
390 (sp->fcport->nvme_prli_service_param &
391 NVME_PRLI_SP_FIRST_BURST)) {
392 if ((fd->payload_length <=
393 sp->fcport->nvme_first_burst_size) ||
394 (sp->fcport->nvme_first_burst_size == 0))
395 cmd_pkt->control_flags |=
396 CF_NVME_FIRST_BURST_ENABLE;
397 }
Duane Grigsbye84067d2017-06-21 13:48:43 -0700398 vha->qla_stats.output_bytes += fd->payload_length;
399 vha->qla_stats.output_requests++;
400 } else if (fd->io_dir == 0) {
Darren Trapp03aaa892019-02-15 14:37:13 -0800401 cmd_pkt->control_flags = 0;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700402 }
403
404 /* Set NPORT-ID */
405 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
406 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
407 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
408 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
409 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
410
411 /* NVME RSP IU */
412 cmd_pkt->nvme_rsp_dsd_len = cpu_to_le16(fd->rsplen);
413 cmd_pkt->nvme_rsp_dseg_address[0] = cpu_to_le32(LSD(fd->rspdma));
414 cmd_pkt->nvme_rsp_dseg_address[1] = cpu_to_le32(MSD(fd->rspdma));
415
416 /* NVME CNMD IU */
417 cmd_pkt->nvme_cmnd_dseg_len = cpu_to_le16(fd->cmdlen);
418 cmd_pkt->nvme_cmnd_dseg_address[0] = cpu_to_le32(LSD(fd->cmddma));
419 cmd_pkt->nvme_cmnd_dseg_address[1] = cpu_to_le32(MSD(fd->cmddma));
420
421 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
422 cmd_pkt->byte_count = cpu_to_le32(fd->payload_length);
423
424 /* One DSD is available in the Command Type NVME IOCB */
425 avail_dsds = 1;
426 cur_dsd = (uint32_t *)&cmd_pkt->nvme_data_dseg_address[0];
427 sgl = fd->first_sgl;
428
429 /* Load data segments */
430 for_each_sg(sgl, sg, tot_dsds, i) {
431 dma_addr_t sle_dma;
432 cont_a64_entry_t *cont_pkt;
433
434 /* Allocate additional continuation packets? */
435 if (avail_dsds == 0) {
436 /*
437 * Five DSDs are available in the Continuation
438 * Type 1 IOCB.
439 */
440
441 /* Adjust ring index */
442 req->ring_index++;
443 if (req->ring_index == req->length) {
444 req->ring_index = 0;
445 req->ring_ptr = req->ring;
446 } else {
447 req->ring_ptr++;
448 }
449 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Bart Van Assche2c263482019-04-04 12:44:45 -0700450 put_unaligned_le32(CONTINUE_A64_TYPE,
451 &cont_pkt->entry_type);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700452
453 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
454 avail_dsds = 5;
455 }
456
457 sle_dma = sg_dma_address(sg);
458 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
459 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
460 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
461 avail_dsds--;
462 }
463
464 /* Set total entry count. */
465 cmd_pkt->entry_count = (uint8_t)req_cnt;
466 wmb();
467
468 /* Adjust ring index. */
469 req->ring_index++;
470 if (req->ring_index == req->length) {
471 req->ring_index = 0;
472 req->ring_ptr = req->ring;
473 } else {
474 req->ring_ptr++;
475 }
476
477 /* Set chip new ring index. */
478 WRT_REG_DWORD(req->req_q_in, req->ring_index);
479
480queuing_error:
481 spin_unlock_irqrestore(&qpair->qp_lock, flags);
482 return rval;
483}
484
485/* Post a command */
486static int qla_nvme_post_cmd(struct nvme_fc_local_port *lport,
487 struct nvme_fc_remote_port *rport, void *hw_queue_handle,
488 struct nvmefc_fcp_req *fd)
489{
490 fc_port_t *fcport;
491 struct srb_iocb *nvme;
492 struct scsi_qla_host *vha;
Darren Trapp870fe242018-03-20 23:09:35 -0700493 int rval = -ENODEV;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700494 srb_t *sp;
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700495 struct qla_qpair *qpair = hw_queue_handle;
Himanshu Madhani5e6803b2018-12-10 12:36:23 -0800496 struct nvme_private *priv = fd->private;
Darren Trapp9dd96862018-03-20 23:09:32 -0700497 struct qla_nvme_rport *qla_rport = rport->private;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700498
Darren Trapp9dd96862018-03-20 23:09:32 -0700499 fcport = qla_rport->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700500
501 vha = fcport->vha;
Darren Trapp870fe242018-03-20 23:09:35 -0700502
Darren Trapp623ee8242018-03-20 23:09:38 -0700503 if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
504 return rval;
505
Darren Trapp870fe242018-03-20 23:09:35 -0700506 /*
507 * If we know the dev is going away while the transport is still sending
508 * IO's return busy back to stall the IO Q. This happens when the
509 * link goes away and fw hasn't notified us yet, but IO's are being
510 * returned. If the dev comes back quickly we won't exhaust the IO
511 * retry count at the core.
512 */
513 if (fcport->nvme_flag & NVME_FLAG_RESETTING)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700514 return -EBUSY;
515
516 /* Alloc SRB structure */
Quinn Tran6a629462018-09-04 14:19:15 -0700517 sp = qla2xxx_get_qpair_sp(vha, qpair, fcport, GFP_ATOMIC);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700518 if (!sp)
Darren Trapp870fe242018-03-20 23:09:35 -0700519 return -EBUSY;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700520
521 atomic_set(&sp->ref_count, 1);
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700522 init_waitqueue_head(&sp->nvme_ls_waitq);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700523 priv->sp = sp;
524 sp->type = SRB_NVME_CMD;
525 sp->name = "nvme_cmd";
526 sp->done = qla_nvme_sp_done;
527 sp->qpair = qpair;
Himanshu Madhani5e6803b2018-12-10 12:36:23 -0800528 sp->vha = vha;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700529 nvme = &sp->u.iocb_cmd;
530 nvme->u.nvme.desc = fd;
531
532 rval = qla2x00_start_nvme_mq(sp);
533 if (rval != QLA_SUCCESS) {
534 ql_log(ql_log_warn, vha, 0x212d,
535 "qla2x00_start_nvme_mq failed = %d\n", rval);
536 atomic_dec(&sp->ref_count);
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700537 wake_up(&sp->nvme_ls_waitq);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700538 }
539
540 return rval;
541}
542
543static void qla_nvme_localport_delete(struct nvme_fc_local_port *lport)
544{
545 struct scsi_qla_host *vha = lport->private;
546
Duane Grigsbye84067d2017-06-21 13:48:43 -0700547 ql_log(ql_log_info, vha, 0x210f,
548 "localport delete of %p completed.\n", vha->nvme_local_port);
549 vha->nvme_local_port = NULL;
himanshu.madhani@cavium.com5621b0d2017-07-21 09:32:26 -0700550 complete(&vha->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700551}
552
553static void qla_nvme_remoteport_delete(struct nvme_fc_remote_port *rport)
554{
555 fc_port_t *fcport;
Darren Trapp9dd96862018-03-20 23:09:32 -0700556 struct qla_nvme_rport *qla_rport = rport->private, *trport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700557
Darren Trapp9dd96862018-03-20 23:09:32 -0700558 fcport = qla_rport->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700559 fcport->nvme_remote_port = NULL;
560 fcport->nvme_flag &= ~NVME_FLAG_REGISTERED;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700561
Darren Trapp9dd96862018-03-20 23:09:32 -0700562 list_for_each_entry_safe(qla_rport, trport,
Duane Grigsbye84067d2017-06-21 13:48:43 -0700563 &fcport->vha->nvme_rport_list, list) {
Darren Trapp9dd96862018-03-20 23:09:32 -0700564 if (qla_rport->fcport == fcport) {
565 list_del(&qla_rport->list);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700566 break;
567 }
568 }
himanshu.madhani@cavium.com5621b0d2017-07-21 09:32:26 -0700569 complete(&fcport->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700570
Darren Trapp9dd96862018-03-20 23:09:32 -0700571 if (!test_bit(UNLOADING, &fcport->vha->dpc_flags)) {
572 INIT_WORK(&fcport->free_work, qlt_free_session_done);
573 schedule_work(&fcport->free_work);
574 }
575
Himanshu Madhani5e6803b2018-12-10 12:36:23 -0800576 fcport->nvme_flag &= ~NVME_FLAG_DELETING;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700577 ql_log(ql_log_info, fcport->vha, 0x2110,
578 "remoteport_delete of %p completed.\n", fcport);
579}
580
581static struct nvme_fc_port_template qla_nvme_fc_transport = {
582 .localport_delete = qla_nvme_localport_delete,
583 .remoteport_delete = qla_nvme_remoteport_delete,
584 .create_queue = qla_nvme_alloc_queue,
585 .delete_queue = NULL,
586 .ls_req = qla_nvme_ls_req,
587 .ls_abort = qla_nvme_ls_abort,
588 .fcp_io = qla_nvme_post_cmd,
589 .fcp_abort = qla_nvme_fcp_abort,
Duane Grigsbye84067d2017-06-21 13:48:43 -0700590 .max_hw_queues = 8,
Giridhar Malavali6b1f4442019-04-02 14:24:23 -0700591 .max_sgl_segments = 1024,
Duane Grigsbye84067d2017-06-21 13:48:43 -0700592 .max_dif_sgl_segments = 64,
593 .dma_boundary = 0xFFFFFFFF,
594 .local_priv_sz = 8,
Darren Trapp9dd96862018-03-20 23:09:32 -0700595 .remote_priv_sz = sizeof(struct qla_nvme_rport),
Duane Grigsbye84067d2017-06-21 13:48:43 -0700596 .lsrqst_priv_sz = sizeof(struct nvme_private),
597 .fcprqst_priv_sz = sizeof(struct nvme_private),
598};
599
Duane Grigsbye84067d2017-06-21 13:48:43 -0700600static void qla_nvme_unregister_remote_port(struct work_struct *work)
601{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700602 struct fc_port *fcport = container_of(work, struct fc_port,
603 nvme_del_work);
Darren Trapp9dd96862018-03-20 23:09:32 -0700604 struct qla_nvme_rport *qla_rport, *trport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700605
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200606 if (!IS_ENABLED(CONFIG_NVME_FC))
607 return;
608
himanshu.madhani@cavium.com49b3d5f62017-07-21 09:32:27 -0700609 ql_log(ql_log_warn, NULL, 0x2112,
610 "%s: unregister remoteport on %p\n",__func__, fcport);
611
Darren Trapp9dd96862018-03-20 23:09:32 -0700612 list_for_each_entry_safe(qla_rport, trport,
Duane Grigsbye84067d2017-06-21 13:48:43 -0700613 &fcport->vha->nvme_rport_list, list) {
Darren Trapp9dd96862018-03-20 23:09:32 -0700614 if (qla_rport->fcport == fcport) {
Duane Grigsbye84067d2017-06-21 13:48:43 -0700615 ql_log(ql_log_info, fcport->vha, 0x2113,
616 "%s: fcport=%p\n", __func__, fcport);
Giridhar Malavaliffc81fc02019-04-02 14:24:30 -0700617 nvme_fc_set_remoteport_devloss
618 (fcport->nvme_remote_port, 0);
himanshu.madhani@cavium.com49b3d5f62017-07-21 09:32:27 -0700619 init_completion(&fcport->nvme_del_done);
Giridhar Malavaliffc81fc02019-04-02 14:24:30 -0700620 if (nvme_fc_unregister_remoteport
621 (fcport->nvme_remote_port))
622 ql_log(ql_log_info, fcport->vha, 0x2114,
623 "%s: Failed to unregister nvme_remote_port\n",
624 __func__);
Darren Trapp9dd96862018-03-20 23:09:32 -0700625 wait_for_completion(&fcport->nvme_del_done);
626 break;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700627 }
628 }
Duane Grigsbye84067d2017-06-21 13:48:43 -0700629}
630
himanshu.madhani@cavium.com0f7e51f2017-07-21 09:32:24 -0700631void qla_nvme_delete(struct scsi_qla_host *vha)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700632{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700633 int nv_ret;
634
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200635 if (!IS_ENABLED(CONFIG_NVME_FC))
636 return;
637
Duane Grigsbye84067d2017-06-21 13:48:43 -0700638 if (vha->nvme_local_port) {
himanshu.madhani@cavium.com5621b0d2017-07-21 09:32:26 -0700639 init_completion(&vha->nvme_del_done);
Darren Trapp9dd96862018-03-20 23:09:32 -0700640 ql_log(ql_log_info, vha, 0x2116,
641 "unregister localport=%p\n",
642 vha->nvme_local_port);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700643 nv_ret = nvme_fc_unregister_localport(vha->nvme_local_port);
Darren Trapp9dd96862018-03-20 23:09:32 -0700644 if (nv_ret)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700645 ql_log(ql_log_info, vha, 0x2115,
646 "Unregister of localport failed\n");
Darren Trapp9dd96862018-03-20 23:09:32 -0700647 else
648 wait_for_completion(&vha->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700649 }
Duane Grigsbye84067d2017-06-21 13:48:43 -0700650}
651
Quinn Tran8777e432018-08-02 13:16:57 -0700652int qla_nvme_register_hba(struct scsi_qla_host *vha)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700653{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700654 struct nvme_fc_port_template *tmpl;
655 struct qla_hw_data *ha;
656 struct nvme_fc_port_info pinfo;
Quinn Tran8777e432018-08-02 13:16:57 -0700657 int ret = EINVAL;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700658
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200659 if (!IS_ENABLED(CONFIG_NVME_FC))
Quinn Tran8777e432018-08-02 13:16:57 -0700660 return ret;
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200661
Duane Grigsbye84067d2017-06-21 13:48:43 -0700662 ha = vha->hw;
663 tmpl = &qla_nvme_fc_transport;
664
665 WARN_ON(vha->nvme_local_port);
666 WARN_ON(ha->max_req_queues < 3);
667
668 qla_nvme_fc_transport.max_hw_queues =
669 min((uint8_t)(qla_nvme_fc_transport.max_hw_queues),
670 (uint8_t)(ha->max_req_queues - 2));
671
672 pinfo.node_name = wwn_to_u64(vha->node_name);
673 pinfo.port_name = wwn_to_u64(vha->port_name);
674 pinfo.port_role = FC_PORT_ROLE_NVME_INITIATOR;
675 pinfo.port_id = vha->d_id.b24;
676
677 ql_log(ql_log_info, vha, 0xffff,
Darren Trapd7936a92017-08-23 15:04:59 -0700678 "register_localport: host-traddr=nn-0x%llx:pn-0x%llx on portID:%x\n",
679 pinfo.node_name, pinfo.port_name, pinfo.port_id);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700680 qla_nvme_fc_transport.dma_boundary = vha->host->dma_boundary;
681
682 ret = nvme_fc_register_localport(&pinfo, tmpl,
683 get_device(&ha->pdev->dev), &vha->nvme_local_port);
684 if (ret) {
685 ql_log(ql_log_warn, vha, 0xffff,
686 "register_localport failed: ret=%x\n", ret);
Quinn Tran8777e432018-08-02 13:16:57 -0700687 } else {
688 vha->nvme_local_port->private = vha;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700689 }
Quinn Tran8777e432018-08-02 13:16:57 -0700690
691 return ret;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700692}