blob: 4c1112107e8896c7127d5bd18b6dccb4411ec66a [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;
Bart Van Assche15b7a682019-04-17 14:44:38 -0700308 struct dsd64 *cur_dsd;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700309 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);
Bart Van Assched4556a42019-04-17 14:44:39 -0700413 put_unaligned_le64(fd->rspdma, &cmd_pkt->nvme_rsp_dseg_address);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700414
415 /* NVME CNMD IU */
416 cmd_pkt->nvme_cmnd_dseg_len = cpu_to_le16(fd->cmdlen);
Bart Van Assched4556a42019-04-17 14:44:39 -0700417 cmd_pkt->nvme_cmnd_dseg_address = cpu_to_le64(fd->cmddma);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700418
419 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
420 cmd_pkt->byte_count = cpu_to_le32(fd->payload_length);
421
422 /* One DSD is available in the Command Type NVME IOCB */
423 avail_dsds = 1;
Bart Van Assche15b7a682019-04-17 14:44:38 -0700424 cur_dsd = &cmd_pkt->nvme_dsd;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700425 sgl = fd->first_sgl;
426
427 /* Load data segments */
428 for_each_sg(sgl, sg, tot_dsds, i) {
Duane Grigsbye84067d2017-06-21 13:48:43 -0700429 cont_a64_entry_t *cont_pkt;
430
431 /* Allocate additional continuation packets? */
432 if (avail_dsds == 0) {
433 /*
434 * Five DSDs are available in the Continuation
435 * Type 1 IOCB.
436 */
437
438 /* Adjust ring index */
439 req->ring_index++;
440 if (req->ring_index == req->length) {
441 req->ring_index = 0;
442 req->ring_ptr = req->ring;
443 } else {
444 req->ring_ptr++;
445 }
446 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Bart Van Assche2c263482019-04-04 12:44:45 -0700447 put_unaligned_le32(CONTINUE_A64_TYPE,
448 &cont_pkt->entry_type);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700449
Bart Van Assche15b7a682019-04-17 14:44:38 -0700450 cur_dsd = cont_pkt->dsd;
451 avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700452 }
453
Bart Van Assche15b7a682019-04-17 14:44:38 -0700454 append_dsd64(&cur_dsd, sg);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700455 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;
Darren Trapp870fe242018-03-20 23:09:35 -0700487 int rval = -ENODEV;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700488 srb_t *sp;
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700489 struct qla_qpair *qpair = hw_queue_handle;
Himanshu Madhani5e6803b2018-12-10 12:36:23 -0800490 struct nvme_private *priv = fd->private;
Darren Trapp9dd96862018-03-20 23:09:32 -0700491 struct qla_nvme_rport *qla_rport = rport->private;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700492
Darren Trapp9dd96862018-03-20 23:09:32 -0700493 fcport = qla_rport->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700494
495 vha = fcport->vha;
Darren Trapp870fe242018-03-20 23:09:35 -0700496
Darren Trapp623ee8242018-03-20 23:09:38 -0700497 if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
498 return rval;
499
Darren Trapp870fe242018-03-20 23:09:35 -0700500 /*
501 * If we know the dev is going away while the transport is still sending
502 * IO's return busy back to stall the IO Q. This happens when the
503 * link goes away and fw hasn't notified us yet, but IO's are being
504 * returned. If the dev comes back quickly we won't exhaust the IO
505 * retry count at the core.
506 */
507 if (fcport->nvme_flag & NVME_FLAG_RESETTING)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700508 return -EBUSY;
509
510 /* Alloc SRB structure */
Quinn Tran6a629462018-09-04 14:19:15 -0700511 sp = qla2xxx_get_qpair_sp(vha, qpair, fcport, GFP_ATOMIC);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700512 if (!sp)
Darren Trapp870fe242018-03-20 23:09:35 -0700513 return -EBUSY;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700514
515 atomic_set(&sp->ref_count, 1);
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700516 init_waitqueue_head(&sp->nvme_ls_waitq);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700517 priv->sp = sp;
518 sp->type = SRB_NVME_CMD;
519 sp->name = "nvme_cmd";
520 sp->done = qla_nvme_sp_done;
521 sp->qpair = qpair;
Himanshu Madhani5e6803b2018-12-10 12:36:23 -0800522 sp->vha = vha;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700523 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
Himanshu Madhani5e6803b2018-12-10 12:36:23 -0800570 fcport->nvme_flag &= ~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,
Duane Grigsbye84067d2017-06-21 13:48:43 -0700584 .max_hw_queues = 8,
Giridhar Malavali6b1f4442019-04-02 14:24:23 -0700585 .max_sgl_segments = 1024,
Duane Grigsbye84067d2017-06-21 13:48:43 -0700586 .max_dif_sgl_segments = 64,
587 .dma_boundary = 0xFFFFFFFF,
588 .local_priv_sz = 8,
Darren Trapp9dd96862018-03-20 23:09:32 -0700589 .remote_priv_sz = sizeof(struct qla_nvme_rport),
Duane Grigsbye84067d2017-06-21 13:48:43 -0700590 .lsrqst_priv_sz = sizeof(struct nvme_private),
591 .fcprqst_priv_sz = sizeof(struct nvme_private),
592};
593
Duane Grigsbye84067d2017-06-21 13:48:43 -0700594static void qla_nvme_unregister_remote_port(struct work_struct *work)
595{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700596 struct fc_port *fcport = container_of(work, struct fc_port,
597 nvme_del_work);
Darren Trapp9dd96862018-03-20 23:09:32 -0700598 struct qla_nvme_rport *qla_rport, *trport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700599
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200600 if (!IS_ENABLED(CONFIG_NVME_FC))
601 return;
602
himanshu.madhani@cavium.com49b3d5f62017-07-21 09:32:27 -0700603 ql_log(ql_log_warn, NULL, 0x2112,
604 "%s: unregister remoteport on %p\n",__func__, fcport);
605
Darren Trapp9dd96862018-03-20 23:09:32 -0700606 list_for_each_entry_safe(qla_rport, trport,
Duane Grigsbye84067d2017-06-21 13:48:43 -0700607 &fcport->vha->nvme_rport_list, list) {
Darren Trapp9dd96862018-03-20 23:09:32 -0700608 if (qla_rport->fcport == fcport) {
Duane Grigsbye84067d2017-06-21 13:48:43 -0700609 ql_log(ql_log_info, fcport->vha, 0x2113,
610 "%s: fcport=%p\n", __func__, fcport);
Giridhar Malavaliffc81fc02019-04-02 14:24:30 -0700611 nvme_fc_set_remoteport_devloss
612 (fcport->nvme_remote_port, 0);
himanshu.madhani@cavium.com49b3d5f62017-07-21 09:32:27 -0700613 init_completion(&fcport->nvme_del_done);
Giridhar Malavaliffc81fc02019-04-02 14:24:30 -0700614 if (nvme_fc_unregister_remoteport
615 (fcport->nvme_remote_port))
616 ql_log(ql_log_info, fcport->vha, 0x2114,
617 "%s: Failed to unregister nvme_remote_port\n",
618 __func__);
Darren Trapp9dd96862018-03-20 23:09:32 -0700619 wait_for_completion(&fcport->nvme_del_done);
620 break;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700621 }
622 }
Duane Grigsbye84067d2017-06-21 13:48:43 -0700623}
624
himanshu.madhani@cavium.com0f7e51f2017-07-21 09:32:24 -0700625void qla_nvme_delete(struct scsi_qla_host *vha)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700626{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700627 int nv_ret;
628
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200629 if (!IS_ENABLED(CONFIG_NVME_FC))
630 return;
631
Duane Grigsbye84067d2017-06-21 13:48:43 -0700632 if (vha->nvme_local_port) {
himanshu.madhani@cavium.com5621b0d2017-07-21 09:32:26 -0700633 init_completion(&vha->nvme_del_done);
Darren Trapp9dd96862018-03-20 23:09:32 -0700634 ql_log(ql_log_info, vha, 0x2116,
635 "unregister localport=%p\n",
636 vha->nvme_local_port);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700637 nv_ret = nvme_fc_unregister_localport(vha->nvme_local_port);
Darren Trapp9dd96862018-03-20 23:09:32 -0700638 if (nv_ret)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700639 ql_log(ql_log_info, vha, 0x2115,
640 "Unregister of localport failed\n");
Darren Trapp9dd96862018-03-20 23:09:32 -0700641 else
642 wait_for_completion(&vha->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700643 }
Duane Grigsbye84067d2017-06-21 13:48:43 -0700644}
645
Quinn Tran8777e432018-08-02 13:16:57 -0700646int qla_nvme_register_hba(struct scsi_qla_host *vha)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700647{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700648 struct nvme_fc_port_template *tmpl;
649 struct qla_hw_data *ha;
650 struct nvme_fc_port_info pinfo;
Quinn Tran8777e432018-08-02 13:16:57 -0700651 int ret = EINVAL;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700652
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200653 if (!IS_ENABLED(CONFIG_NVME_FC))
Quinn Tran8777e432018-08-02 13:16:57 -0700654 return ret;
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200655
Duane Grigsbye84067d2017-06-21 13:48:43 -0700656 ha = vha->hw;
657 tmpl = &qla_nvme_fc_transport;
658
659 WARN_ON(vha->nvme_local_port);
660 WARN_ON(ha->max_req_queues < 3);
661
662 qla_nvme_fc_transport.max_hw_queues =
663 min((uint8_t)(qla_nvme_fc_transport.max_hw_queues),
664 (uint8_t)(ha->max_req_queues - 2));
665
666 pinfo.node_name = wwn_to_u64(vha->node_name);
667 pinfo.port_name = wwn_to_u64(vha->port_name);
668 pinfo.port_role = FC_PORT_ROLE_NVME_INITIATOR;
669 pinfo.port_id = vha->d_id.b24;
670
671 ql_log(ql_log_info, vha, 0xffff,
Darren Trapd7936a92017-08-23 15:04:59 -0700672 "register_localport: host-traddr=nn-0x%llx:pn-0x%llx on portID:%x\n",
673 pinfo.node_name, pinfo.port_name, pinfo.port_id);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700674 qla_nvme_fc_transport.dma_boundary = vha->host->dma_boundary;
675
676 ret = nvme_fc_register_localport(&pinfo, tmpl,
677 get_device(&ha->pdev->dev), &vha->nvme_local_port);
678 if (ret) {
679 ql_log(ql_log_warn, vha, 0xffff,
680 "register_localport failed: ret=%x\n", ret);
Quinn Tran8777e432018-08-02 13:16:57 -0700681 } else {
682 vha->nvme_local_port->private = vha;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700683 }
Quinn Tran8777e432018-08-02 13:16:57 -0700684
685 return ret;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700686}