blob: a8da6be52b184b331b494e5ff5720c670ed9dcbd [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
himanshu.madhani@cavium.com0f7e51f2017-07-21 09:32:24 -070015int qla_nvme_register_remote(struct scsi_qla_host *vha, struct fc_port *fcport)
Duane Grigsbye84067d2017-06-21 13:48:43 -070016{
Darren Trapp9dd96862018-03-20 23:09:32 -070017 struct qla_nvme_rport *rport;
18 struct nvme_fc_port_info req;
Duane Grigsbye84067d2017-06-21 13:48:43 -070019 int ret;
20
Arnd Bergmannbcda7712017-06-30 18:10:40 +020021 if (!IS_ENABLED(CONFIG_NVME_FC))
22 return 0;
23
Duane Grigsbye84067d2017-06-21 13:48:43 -070024 if (!vha->flags.nvme_enabled) {
25 ql_log(ql_log_info, vha, 0x2100,
26 "%s: Not registering target since Host NVME is not enabled\n",
27 __func__);
28 return 0;
29 }
30
Quinn Tran8777e432018-08-02 13:16:57 -070031 if (!vha->nvme_local_port && qla_nvme_register_hba(vha))
32 return 0;
33
Duane Grigsbye84067d2017-06-21 13:48:43 -070034 if (!(fcport->nvme_prli_service_param &
Darren Trapp9dd96862018-03-20 23:09:32 -070035 (NVME_PRLI_SP_TARGET | NVME_PRLI_SP_DISCOVERY)) ||
36 (fcport->nvme_flag & NVME_FLAG_REGISTERED))
Duane Grigsbye84067d2017-06-21 13:48:43 -070037 return 0;
38
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;
Darren Trapp9dd96862018-03-20 23:09:32 -070074
75 fcport->nvme_flag |= NVME_FLAG_REGISTERED;
Duane Grigsbye84067d2017-06-21 13:48:43 -070076 return 0;
77}
78
79/* Allocate a queue for NVMe traffic */
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -070080static int qla_nvme_alloc_queue(struct nvme_fc_local_port *lport,
81 unsigned int qidx, u16 qsize, void **handle)
Duane Grigsbye84067d2017-06-21 13:48:43 -070082{
83 struct scsi_qla_host *vha;
84 struct qla_hw_data *ha;
85 struct qla_qpair *qpair;
86
87 if (!qidx)
88 qidx++;
89
90 vha = (struct scsi_qla_host *)lport->private;
91 ha = vha->hw;
92
93 ql_log(ql_log_info, vha, 0x2104,
94 "%s: handle %p, idx =%d, qsize %d\n",
95 __func__, handle, qidx, qsize);
96
97 if (qidx > qla_nvme_fc_transport.max_hw_queues) {
98 ql_log(ql_log_warn, vha, 0x212f,
99 "%s: Illegal qidx=%d. Max=%d\n",
100 __func__, qidx, qla_nvme_fc_transport.max_hw_queues);
101 return -EINVAL;
102 }
103
104 if (ha->queue_pair_map[qidx]) {
105 *handle = ha->queue_pair_map[qidx];
106 ql_log(ql_log_info, vha, 0x2121,
107 "Returning existing qpair of %p for idx=%x\n",
108 *handle, qidx);
109 return 0;
110 }
111
Duane Grigsbye84067d2017-06-21 13:48:43 -0700112 qpair = qla2xxx_create_qpair(vha, 5, vha->vp_idx, true);
113 if (qpair == NULL) {
114 ql_log(ql_log_warn, vha, 0x2122,
115 "Failed to allocate qpair\n");
116 return -EINVAL;
117 }
118 *handle = qpair;
119
120 return 0;
121}
122
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700123static void qla_nvme_release_fcp_cmd_kref(struct kref *kref)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700124{
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700125 struct srb *sp = container_of(kref, struct srb, cmd_kref);
126 struct nvme_private *priv = (struct nvme_private *)sp->priv;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700127 struct nvmefc_fcp_req *fd;
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700128 struct srb_iocb *nvme;
129 unsigned long flags;
130
131 if (!priv)
132 goto out;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700133
134 nvme = &sp->u.iocb_cmd;
135 fd = nvme->u.nvme.desc;
136
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700137 spin_lock_irqsave(&priv->cmd_lock, flags);
138 priv->sp = NULL;
139 sp->priv = NULL;
140 if (priv->comp_status == QLA_SUCCESS) {
Bart Van Assche7ffa5b92020-05-18 14:17:12 -0700141 fd->rcv_rsplen = le16_to_cpu(nvme->u.nvme.rsp_pyld_len);
Daniel Wagneref2e3ec2020-06-04 12:07:45 +0200142 fd->status = NVME_SC_SUCCESS;
Giridhar Malavalib2d1453a2019-04-02 14:24:32 -0700143 } else {
144 fd->rcv_rsplen = 0;
145 fd->transferred_length = 0;
Daniel Wagneref2e3ec2020-06-04 12:07:45 +0200146 fd->status = NVME_SC_INTERNAL;
Giridhar Malavalib2d1453a2019-04-02 14:24:32 -0700147 }
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700148 spin_unlock_irqrestore(&priv->cmd_lock, flags);
149
Darren Trapp2e4c5d22018-03-20 23:09:36 -0700150 fd->done(fd);
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700151out:
Darren Trapp2e4c5d22018-03-20 23:09:36 -0700152 qla2xxx_rel_qpair_sp(sp->qpair, sp);
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700153}
154
155static void qla_nvme_release_ls_cmd_kref(struct kref *kref)
156{
157 struct srb *sp = container_of(kref, struct srb, cmd_kref);
158 struct nvme_private *priv = (struct nvme_private *)sp->priv;
159 struct nvmefc_ls_req *fd;
160 unsigned long flags;
161
162 if (!priv)
163 goto out;
164
165 spin_lock_irqsave(&priv->cmd_lock, flags);
166 priv->sp = NULL;
167 sp->priv = NULL;
168 spin_unlock_irqrestore(&priv->cmd_lock, flags);
169
170 fd = priv->fd;
171 fd->done(fd, priv->comp_status);
172out:
173 qla2x00_rel_sp(sp);
174}
175
176static void qla_nvme_ls_complete(struct work_struct *work)
177{
178 struct nvme_private *priv =
179 container_of(work, struct nvme_private, ls_work);
180
181 kref_put(&priv->sp->cmd_kref, qla_nvme_release_ls_cmd_kref);
182}
183
Bart Van Assche6c18a432019-08-08 20:02:04 -0700184static void qla_nvme_sp_ls_done(srb_t *sp, int res)
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700185{
Bart Van Assche6c18a432019-08-08 20:02:04 -0700186 struct nvme_private *priv = sp->priv;
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700187
188 if (WARN_ON_ONCE(kref_read(&sp->cmd_kref) == 0))
189 return;
190
191 if (res)
192 res = -EINVAL;
193
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700194 priv->comp_status = res;
195 INIT_WORK(&priv->ls_work, qla_nvme_ls_complete);
196 schedule_work(&priv->ls_work);
197}
198
199/* it assumed that QPair lock is held. */
Bart Van Assche6c18a432019-08-08 20:02:04 -0700200static void qla_nvme_sp_done(srb_t *sp, int res)
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700201{
Bart Van Assche6c18a432019-08-08 20:02:04 -0700202 struct nvme_private *priv = sp->priv;
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700203
204 priv->comp_status = res;
205 kref_put(&sp->cmd_kref, qla_nvme_release_fcp_cmd_kref);
Darren Trapp9dd96862018-03-20 23:09:32 -0700206
Duane Grigsbycf19c452017-08-23 15:04:58 -0700207 return;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700208}
209
Darren Trappe473b302018-03-20 23:09:33 -0700210static void qla_nvme_abort_work(struct work_struct *work)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700211{
Darren Trappe473b302018-03-20 23:09:33 -0700212 struct nvme_private *priv =
213 container_of(work, struct nvme_private, abort_work);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700214 srb_t *sp = priv->sp;
Darren Trappe473b302018-03-20 23:09:33 -0700215 fc_port_t *fcport = sp->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700216 struct qla_hw_data *ha = fcport->vha->hw;
Darren Trappe473b302018-03-20 23:09:33 -0700217 int rval;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700218
Bart Van Asschedafbe562019-04-17 14:44:18 -0700219 ql_dbg(ql_dbg_io, fcport->vha, 0xffff,
220 "%s called for sp=%p, hndl=%x on fcport=%p deleted=%d\n",
221 __func__, sp, sp->handle, fcport, fcport->deleted);
Himanshu Madhani471f8e02019-02-15 14:37:15 -0800222
Bart Van Assche53be1002019-08-08 20:01:37 -0700223 if (!ha->flags.fw_started && fcport->deleted)
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700224 goto out;
Himanshu Madhani471f8e02019-02-15 14:37:15 -0800225
Giridhar Malavalib2d1453a2019-04-02 14:24:32 -0700226 if (ha->flags.host_shutting_down) {
227 ql_log(ql_log_info, sp->fcport->vha, 0xffff,
Quinn Tranf45bca82019-11-05 07:06:54 -0800228 "%s Calling done on sp: %p, type: 0x%x\n",
229 __func__, sp, sp->type);
Giridhar Malavalib2d1453a2019-04-02 14:24:32 -0700230 sp->done(sp, 0);
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700231 goto out;
Giridhar Malavalib2d1453a2019-04-02 14:24:32 -0700232 }
233
Duane Grigsbye84067d2017-06-21 13:48:43 -0700234 rval = ha->isp_ops->abort_command(sp);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700235
236 ql_dbg(ql_dbg_io, fcport->vha, 0x212b,
Darren Trapp870fe242018-03-20 23:09:35 -0700237 "%s: %s command for sp=%p, handle=%x on fcport=%p rval=%x\n",
238 __func__, (rval != QLA_SUCCESS) ? "Failed to abort" : "Aborted",
239 sp, sp->handle, fcport, rval);
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700240
241out:
242 /* kref_get was done before work was schedule. */
243 kref_put(&sp->cmd_kref, sp->put_fn);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700244}
245
Darren Trappe473b302018-03-20 23:09:33 -0700246static void qla_nvme_ls_abort(struct nvme_fc_local_port *lport,
247 struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
248{
249 struct nvme_private *priv = fd->private;
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700250 unsigned long flags;
251
252 spin_lock_irqsave(&priv->cmd_lock, flags);
253 if (!priv->sp) {
254 spin_unlock_irqrestore(&priv->cmd_lock, flags);
255 return;
256 }
257
258 if (!kref_get_unless_zero(&priv->sp->cmd_kref)) {
259 spin_unlock_irqrestore(&priv->cmd_lock, flags);
260 return;
261 }
262 spin_unlock_irqrestore(&priv->cmd_lock, flags);
Darren Trappe473b302018-03-20 23:09:33 -0700263
264 INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
265 schedule_work(&priv->abort_work);
266}
267
Duane Grigsbye84067d2017-06-21 13:48:43 -0700268static int qla_nvme_ls_req(struct nvme_fc_local_port *lport,
269 struct nvme_fc_remote_port *rport, struct nvmefc_ls_req *fd)
270{
Darren Trapp9dd96862018-03-20 23:09:32 -0700271 struct qla_nvme_rport *qla_rport = rport->private;
272 fc_port_t *fcport = qla_rport->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700273 struct srb_iocb *nvme;
274 struct nvme_private *priv = fd->private;
275 struct scsi_qla_host *vha;
276 int rval = QLA_FUNCTION_FAILED;
277 struct qla_hw_data *ha;
278 srb_t *sp;
279
Quinn Tran2eb92382019-06-21 09:50:23 -0700280
281 if (!fcport || (fcport && fcport->deleted))
282 return rval;
283
Duane Grigsbye84067d2017-06-21 13:48:43 -0700284 vha = fcport->vha;
285 ha = vha->hw;
Quinn Tran2eb92382019-06-21 09:50:23 -0700286
287 if (!ha->flags.fw_started)
288 return rval;
289
Duane Grigsbye84067d2017-06-21 13:48:43 -0700290 /* Alloc SRB structure */
291 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
292 if (!sp)
293 return rval;
294
295 sp->type = SRB_NVME_LS;
296 sp->name = "nvme_ls";
297 sp->done = qla_nvme_sp_ls_done;
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700298 sp->put_fn = qla_nvme_release_ls_cmd_kref;
Bart Van Asscheab053c02020-05-18 14:17:09 -0700299 sp->priv = priv;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700300 priv->sp = sp;
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700301 kref_init(&sp->cmd_kref);
302 spin_lock_init(&priv->cmd_lock);
303 nvme = &sp->u.iocb_cmd;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700304 priv->fd = fd;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700305 nvme->u.nvme.desc = fd;
306 nvme->u.nvme.dir = 0;
307 nvme->u.nvme.dl = 0;
308 nvme->u.nvme.cmd_len = fd->rqstlen;
309 nvme->u.nvme.rsp_len = fd->rsplen;
310 nvme->u.nvme.rsp_dma = fd->rspdma;
311 nvme->u.nvme.timeout_sec = fd->timeout;
312 nvme->u.nvme.cmd_dma = dma_map_single(&ha->pdev->dev, fd->rqstaddr,
313 fd->rqstlen, DMA_TO_DEVICE);
314 dma_sync_single_for_device(&ha->pdev->dev, nvme->u.nvme.cmd_dma,
315 fd->rqstlen, DMA_TO_DEVICE);
316
317 rval = qla2x00_start_sp(sp);
318 if (rval != QLA_SUCCESS) {
319 ql_log(ql_log_warn, vha, 0x700e,
320 "qla2x00_start_sp failed = %d\n", rval);
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700321 wake_up(&sp->nvme_ls_waitq);
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700322 sp->priv = NULL;
323 priv->sp = NULL;
324 qla2x00_rel_sp(sp);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700325 return rval;
326 }
327
328 return rval;
329}
330
331static void qla_nvme_fcp_abort(struct nvme_fc_local_port *lport,
332 struct nvme_fc_remote_port *rport, void *hw_queue_handle,
333 struct nvmefc_fcp_req *fd)
334{
335 struct nvme_private *priv = fd->private;
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700336 unsigned long flags;
337
338 spin_lock_irqsave(&priv->cmd_lock, flags);
339 if (!priv->sp) {
340 spin_unlock_irqrestore(&priv->cmd_lock, flags);
341 return;
342 }
343 if (!kref_get_unless_zero(&priv->sp->cmd_kref)) {
344 spin_unlock_irqrestore(&priv->cmd_lock, flags);
345 return;
346 }
347 spin_unlock_irqrestore(&priv->cmd_lock, flags);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700348
Darren Trappe473b302018-03-20 23:09:33 -0700349 INIT_WORK(&priv->abort_work, qla_nvme_abort_work);
350 schedule_work(&priv->abort_work);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700351}
352
Darren Trapp60dd6e8e2018-03-20 23:09:39 -0700353static inline int qla2x00_start_nvme_mq(srb_t *sp)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700354{
355 unsigned long flags;
356 uint32_t *clr_ptr;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700357 uint32_t handle;
358 struct cmd_nvme *cmd_pkt;
359 uint16_t cnt, i;
360 uint16_t req_cnt;
361 uint16_t tot_dsds;
362 uint16_t avail_dsds;
Bart Van Assche15b7a682019-04-17 14:44:38 -0700363 struct dsd64 *cur_dsd;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700364 struct req_que *req = NULL;
365 struct scsi_qla_host *vha = sp->fcport->vha;
366 struct qla_hw_data *ha = vha->hw;
367 struct qla_qpair *qpair = sp->qpair;
368 struct srb_iocb *nvme = &sp->u.iocb_cmd;
369 struct scatterlist *sgl, *sg;
370 struct nvmefc_fcp_req *fd = nvme->u.nvme.desc;
371 uint32_t rval = QLA_SUCCESS;
372
himanshu.madhani@cavium.com1d4614e2018-03-20 23:09:30 -0700373 /* Setup qpair pointers */
374 req = qpair->req;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700375 tot_dsds = fd->sg_cnt;
376
377 /* Acquire qpair specific lock */
378 spin_lock_irqsave(&qpair->qp_lock, flags);
379
Bart Van Asschebcc85652019-08-08 20:02:09 -0700380 handle = qla2xxx_get_next_handle(req);
381 if (handle == 0) {
Darren Trapp870fe242018-03-20 23:09:35 -0700382 rval = -EBUSY;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700383 goto queuing_error;
384 }
385 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
386 if (req->cnt < (req_cnt + 2)) {
387 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
Bart Van Assche04474d32020-05-18 14:17:08 -0700388 rd_reg_dword_relaxed(req->req_q_out);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700389
390 if (req->ring_index < cnt)
391 req->cnt = cnt - req->ring_index;
392 else
393 req->cnt = req->length - (req->ring_index - cnt);
394
395 if (req->cnt < (req_cnt + 2)){
Darren Trapp870fe242018-03-20 23:09:35 -0700396 rval = -EBUSY;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700397 goto queuing_error;
398 }
399 }
400
401 if (unlikely(!fd->sqid)) {
402 struct nvme_fc_cmd_iu *cmd = fd->cmdaddr;
Bart Van Asschebd432bb2019-04-11 14:53:17 -0700403
Duane Grigsbye84067d2017-06-21 13:48:43 -0700404 if (cmd->sqe.common.opcode == nvme_admin_async_event) {
405 nvme->u.nvme.aen_op = 1;
himanshu.madhani@cavium.com1d4614e2018-03-20 23:09:30 -0700406 atomic_inc(&ha->nvme_active_aen_cnt);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700407 }
408 }
409
410 /* Build command packet. */
411 req->current_outstanding_cmd = handle;
412 req->outstanding_cmds[handle] = sp;
413 sp->handle = handle;
414 req->cnt -= req_cnt;
415
416 cmd_pkt = (struct cmd_nvme *)req->ring_ptr;
Bart Van Asschec25eb702020-02-19 20:34:40 -0800417 cmd_pkt->handle = make_handle(req->id, handle);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700418
419 /* Zero out remaining portion of packet. */
420 clr_ptr = (uint32_t *)cmd_pkt + 2;
421 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
422
423 cmd_pkt->entry_status = 0;
424
425 /* Update entry type to indicate Command NVME IOCB */
426 cmd_pkt->entry_type = COMMAND_NVME;
427
428 /* No data transfer how do we check buffer len == 0?? */
429 if (fd->io_dir == NVMEFC_FCP_READ) {
Bart Van Assche7ffa5b92020-05-18 14:17:12 -0700430 cmd_pkt->control_flags = cpu_to_le16(CF_READ_DATA);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700431 vha->qla_stats.input_bytes += fd->payload_length;
432 vha->qla_stats.input_requests++;
433 } else if (fd->io_dir == NVMEFC_FCP_WRITE) {
Bart Van Assche7ffa5b92020-05-18 14:17:12 -0700434 cmd_pkt->control_flags = cpu_to_le16(CF_WRITE_DATA);
Darren Trapp03aaa892019-02-15 14:37:13 -0800435 if ((vha->flags.nvme_first_burst) &&
436 (sp->fcport->nvme_prli_service_param &
437 NVME_PRLI_SP_FIRST_BURST)) {
438 if ((fd->payload_length <=
439 sp->fcport->nvme_first_burst_size) ||
440 (sp->fcport->nvme_first_burst_size == 0))
441 cmd_pkt->control_flags |=
Bart Van Assche7ffa5b92020-05-18 14:17:12 -0700442 cpu_to_le16(CF_NVME_FIRST_BURST_ENABLE);
Darren Trapp03aaa892019-02-15 14:37:13 -0800443 }
Duane Grigsbye84067d2017-06-21 13:48:43 -0700444 vha->qla_stats.output_bytes += fd->payload_length;
445 vha->qla_stats.output_requests++;
446 } else if (fd->io_dir == 0) {
Darren Trapp03aaa892019-02-15 14:37:13 -0800447 cmd_pkt->control_flags = 0;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700448 }
449
450 /* Set NPORT-ID */
451 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
452 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
453 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
454 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
455 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
456
457 /* NVME RSP IU */
458 cmd_pkt->nvme_rsp_dsd_len = cpu_to_le16(fd->rsplen);
Bart Van Assched4556a42019-04-17 14:44:39 -0700459 put_unaligned_le64(fd->rspdma, &cmd_pkt->nvme_rsp_dseg_address);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700460
461 /* NVME CNMD IU */
462 cmd_pkt->nvme_cmnd_dseg_len = cpu_to_le16(fd->cmdlen);
Bart Van Assched4556a42019-04-17 14:44:39 -0700463 cmd_pkt->nvme_cmnd_dseg_address = cpu_to_le64(fd->cmddma);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700464
465 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
466 cmd_pkt->byte_count = cpu_to_le32(fd->payload_length);
467
468 /* One DSD is available in the Command Type NVME IOCB */
469 avail_dsds = 1;
Bart Van Assche15b7a682019-04-17 14:44:38 -0700470 cur_dsd = &cmd_pkt->nvme_dsd;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700471 sgl = fd->first_sgl;
472
473 /* Load data segments */
474 for_each_sg(sgl, sg, tot_dsds, i) {
Duane Grigsbye84067d2017-06-21 13:48:43 -0700475 cont_a64_entry_t *cont_pkt;
476
477 /* Allocate additional continuation packets? */
478 if (avail_dsds == 0) {
479 /*
480 * Five DSDs are available in the Continuation
481 * Type 1 IOCB.
482 */
483
484 /* Adjust ring index */
485 req->ring_index++;
486 if (req->ring_index == req->length) {
487 req->ring_index = 0;
488 req->ring_ptr = req->ring;
489 } else {
490 req->ring_ptr++;
491 }
492 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Bart Van Assche2c263482019-04-04 12:44:45 -0700493 put_unaligned_le32(CONTINUE_A64_TYPE,
494 &cont_pkt->entry_type);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700495
Bart Van Assche15b7a682019-04-17 14:44:38 -0700496 cur_dsd = cont_pkt->dsd;
497 avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700498 }
499
Bart Van Assche15b7a682019-04-17 14:44:38 -0700500 append_dsd64(&cur_dsd, sg);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700501 avail_dsds--;
502 }
503
504 /* Set total entry count. */
505 cmd_pkt->entry_count = (uint8_t)req_cnt;
506 wmb();
507
508 /* Adjust ring index. */
509 req->ring_index++;
510 if (req->ring_index == req->length) {
511 req->ring_index = 0;
512 req->ring_ptr = req->ring;
513 } else {
514 req->ring_ptr++;
515 }
516
517 /* Set chip new ring index. */
Bart Van Assche04474d32020-05-18 14:17:08 -0700518 wrt_reg_dword(req->req_q_in, req->ring_index);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700519
520queuing_error:
521 spin_unlock_irqrestore(&qpair->qp_lock, flags);
522 return rval;
523}
524
525/* Post a command */
526static int qla_nvme_post_cmd(struct nvme_fc_local_port *lport,
527 struct nvme_fc_remote_port *rport, void *hw_queue_handle,
528 struct nvmefc_fcp_req *fd)
529{
530 fc_port_t *fcport;
531 struct srb_iocb *nvme;
532 struct scsi_qla_host *vha;
Darren Trapp870fe242018-03-20 23:09:35 -0700533 int rval = -ENODEV;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700534 srb_t *sp;
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700535 struct qla_qpair *qpair = hw_queue_handle;
Himanshu Madhani5e6803b2018-12-10 12:36:23 -0800536 struct nvme_private *priv = fd->private;
Darren Trapp9dd96862018-03-20 23:09:32 -0700537 struct qla_nvme_rport *qla_rport = rport->private;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700538
Darren Trapp9dd96862018-03-20 23:09:32 -0700539 fcport = qla_rport->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700540
Quinn Tran2eb92382019-06-21 09:50:23 -0700541 if (!qpair || !fcport || (qpair && !qpair->fw_started) ||
542 (fcport && fcport->deleted))
Darren Trapp623ee8242018-03-20 23:09:38 -0700543 return rval;
544
Quinn Tran2eb92382019-06-21 09:50:23 -0700545 vha = fcport->vha;
Darren Trapp870fe242018-03-20 23:09:35 -0700546 /*
547 * If we know the dev is going away while the transport is still sending
548 * IO's return busy back to stall the IO Q. This happens when the
549 * link goes away and fw hasn't notified us yet, but IO's are being
550 * returned. If the dev comes back quickly we won't exhaust the IO
551 * retry count at the core.
552 */
553 if (fcport->nvme_flag & NVME_FLAG_RESETTING)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700554 return -EBUSY;
555
556 /* Alloc SRB structure */
Quinn Tran6a629462018-09-04 14:19:15 -0700557 sp = qla2xxx_get_qpair_sp(vha, qpair, fcport, GFP_ATOMIC);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700558 if (!sp)
Darren Trapp870fe242018-03-20 23:09:35 -0700559 return -EBUSY;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700560
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700561 init_waitqueue_head(&sp->nvme_ls_waitq);
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700562 kref_init(&sp->cmd_kref);
563 spin_lock_init(&priv->cmd_lock);
Bart Van Asscheab053c02020-05-18 14:17:09 -0700564 sp->priv = priv;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700565 priv->sp = sp;
566 sp->type = SRB_NVME_CMD;
567 sp->name = "nvme_cmd";
568 sp->done = qla_nvme_sp_done;
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700569 sp->put_fn = qla_nvme_release_fcp_cmd_kref;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700570 sp->qpair = qpair;
Himanshu Madhani5e6803b2018-12-10 12:36:23 -0800571 sp->vha = vha;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700572 nvme = &sp->u.iocb_cmd;
573 nvme->u.nvme.desc = fd;
574
575 rval = qla2x00_start_nvme_mq(sp);
576 if (rval != QLA_SUCCESS) {
577 ql_log(ql_log_warn, vha, 0x212d,
578 "qla2x00_start_nvme_mq failed = %d\n", rval);
himanshu.madhani@cavium.com6fcd98f2017-07-21 09:32:23 -0700579 wake_up(&sp->nvme_ls_waitq);
Quinn Tran4c2a2d02019-06-21 09:50:24 -0700580 sp->priv = NULL;
581 priv->sp = NULL;
582 qla2xxx_rel_qpair_sp(sp->qpair, sp);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700583 }
584
585 return rval;
586}
587
588static void qla_nvme_localport_delete(struct nvme_fc_local_port *lport)
589{
590 struct scsi_qla_host *vha = lport->private;
591
Duane Grigsbye84067d2017-06-21 13:48:43 -0700592 ql_log(ql_log_info, vha, 0x210f,
593 "localport delete of %p completed.\n", vha->nvme_local_port);
594 vha->nvme_local_port = NULL;
himanshu.madhani@cavium.com5621b0d2017-07-21 09:32:26 -0700595 complete(&vha->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700596}
597
598static void qla_nvme_remoteport_delete(struct nvme_fc_remote_port *rport)
599{
600 fc_port_t *fcport;
Arun Easi6a815332019-06-21 09:50:22 -0700601 struct qla_nvme_rport *qla_rport = rport->private;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700602
Darren Trapp9dd96862018-03-20 23:09:32 -0700603 fcport = qla_rport->fcport;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700604 fcport->nvme_remote_port = NULL;
605 fcport->nvme_flag &= ~NVME_FLAG_REGISTERED;
Himanshu Madhani5e6803b2018-12-10 12:36:23 -0800606 fcport->nvme_flag &= ~NVME_FLAG_DELETING;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700607 ql_log(ql_log_info, fcport->vha, 0x2110,
Quinn Tran2eb92382019-06-21 09:50:23 -0700608 "remoteport_delete of %p %8phN completed.\n",
609 fcport, fcport->port_name);
Quinn Tranbaf23ed2019-06-16 08:05:53 -0700610 complete(&fcport->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700611}
612
613static struct nvme_fc_port_template qla_nvme_fc_transport = {
614 .localport_delete = qla_nvme_localport_delete,
615 .remoteport_delete = qla_nvme_remoteport_delete,
616 .create_queue = qla_nvme_alloc_queue,
617 .delete_queue = NULL,
618 .ls_req = qla_nvme_ls_req,
619 .ls_abort = qla_nvme_ls_abort,
620 .fcp_io = qla_nvme_post_cmd,
621 .fcp_abort = qla_nvme_fcp_abort,
Duane Grigsbye84067d2017-06-21 13:48:43 -0700622 .max_hw_queues = 8,
Giridhar Malavali6b1f4442019-04-02 14:24:23 -0700623 .max_sgl_segments = 1024,
Duane Grigsbye84067d2017-06-21 13:48:43 -0700624 .max_dif_sgl_segments = 64,
625 .dma_boundary = 0xFFFFFFFF,
626 .local_priv_sz = 8,
Darren Trapp9dd96862018-03-20 23:09:32 -0700627 .remote_priv_sz = sizeof(struct qla_nvme_rport),
Duane Grigsbye84067d2017-06-21 13:48:43 -0700628 .lsrqst_priv_sz = sizeof(struct nvme_private),
629 .fcprqst_priv_sz = sizeof(struct nvme_private),
630};
631
Quinn Tranbaf23ed2019-06-16 08:05:53 -0700632void qla_nvme_unregister_remote_port(struct fc_port *fcport)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700633{
Arun Easi6a815332019-06-21 09:50:22 -0700634 int ret;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700635
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200636 if (!IS_ENABLED(CONFIG_NVME_FC))
637 return;
638
himanshu.madhani@cavium.com49b3d5f62017-07-21 09:32:27 -0700639 ql_log(ql_log_warn, NULL, 0x2112,
Quinn Tran2eb92382019-06-21 09:50:23 -0700640 "%s: unregister remoteport on %p %8phN\n",
641 __func__, fcport, fcport->port_name);
himanshu.madhani@cavium.com49b3d5f62017-07-21 09:32:27 -0700642
Quinn Tran03cc44b2019-07-26 09:07:39 -0700643 if (test_bit(PFLG_DRIVER_REMOVING, &fcport->vha->pci_flags))
644 nvme_fc_set_remoteport_devloss(fcport->nvme_remote_port, 0);
645
Arun Easi6a815332019-06-21 09:50:22 -0700646 init_completion(&fcport->nvme_del_done);
647 ret = nvme_fc_unregister_remoteport(fcport->nvme_remote_port);
648 if (ret)
649 ql_log(ql_log_info, fcport->vha, 0x2114,
650 "%s: Failed to unregister nvme_remote_port (%d)\n",
651 __func__, ret);
652 wait_for_completion(&fcport->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700653}
654
himanshu.madhani@cavium.com0f7e51f2017-07-21 09:32:24 -0700655void qla_nvme_delete(struct scsi_qla_host *vha)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700656{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700657 int nv_ret;
658
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200659 if (!IS_ENABLED(CONFIG_NVME_FC))
660 return;
661
Duane Grigsbye84067d2017-06-21 13:48:43 -0700662 if (vha->nvme_local_port) {
himanshu.madhani@cavium.com5621b0d2017-07-21 09:32:26 -0700663 init_completion(&vha->nvme_del_done);
Darren Trapp9dd96862018-03-20 23:09:32 -0700664 ql_log(ql_log_info, vha, 0x2116,
665 "unregister localport=%p\n",
666 vha->nvme_local_port);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700667 nv_ret = nvme_fc_unregister_localport(vha->nvme_local_port);
Darren Trapp9dd96862018-03-20 23:09:32 -0700668 if (nv_ret)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700669 ql_log(ql_log_info, vha, 0x2115,
670 "Unregister of localport failed\n");
Darren Trapp9dd96862018-03-20 23:09:32 -0700671 else
672 wait_for_completion(&vha->nvme_del_done);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700673 }
Duane Grigsbye84067d2017-06-21 13:48:43 -0700674}
675
Quinn Tran8777e432018-08-02 13:16:57 -0700676int qla_nvme_register_hba(struct scsi_qla_host *vha)
Duane Grigsbye84067d2017-06-21 13:48:43 -0700677{
Duane Grigsbye84067d2017-06-21 13:48:43 -0700678 struct nvme_fc_port_template *tmpl;
679 struct qla_hw_data *ha;
680 struct nvme_fc_port_info pinfo;
Tianjia Zhangca4fb89a2020-08-02 19:15:30 +0800681 int ret = -EINVAL;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700682
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200683 if (!IS_ENABLED(CONFIG_NVME_FC))
Quinn Tran8777e432018-08-02 13:16:57 -0700684 return ret;
Arnd Bergmannbcda7712017-06-30 18:10:40 +0200685
Duane Grigsbye84067d2017-06-21 13:48:43 -0700686 ha = vha->hw;
687 tmpl = &qla_nvme_fc_transport;
688
689 WARN_ON(vha->nvme_local_port);
690 WARN_ON(ha->max_req_queues < 3);
691
692 qla_nvme_fc_transport.max_hw_queues =
693 min((uint8_t)(qla_nvme_fc_transport.max_hw_queues),
694 (uint8_t)(ha->max_req_queues - 2));
695
696 pinfo.node_name = wwn_to_u64(vha->node_name);
697 pinfo.port_name = wwn_to_u64(vha->port_name);
698 pinfo.port_role = FC_PORT_ROLE_NVME_INITIATOR;
699 pinfo.port_id = vha->d_id.b24;
700
701 ql_log(ql_log_info, vha, 0xffff,
Darren Trapd7936a92017-08-23 15:04:59 -0700702 "register_localport: host-traddr=nn-0x%llx:pn-0x%llx on portID:%x\n",
703 pinfo.node_name, pinfo.port_name, pinfo.port_id);
Duane Grigsbye84067d2017-06-21 13:48:43 -0700704 qla_nvme_fc_transport.dma_boundary = vha->host->dma_boundary;
705
706 ret = nvme_fc_register_localport(&pinfo, tmpl,
707 get_device(&ha->pdev->dev), &vha->nvme_local_port);
708 if (ret) {
709 ql_log(ql_log_warn, vha, 0xffff,
710 "register_localport failed: ret=%x\n", ret);
Quinn Tran8777e432018-08-02 13:16:57 -0700711 } else {
712 vha->nvme_local_port->private = vha;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700713 }
Quinn Tran8777e432018-08-02 13:16:57 -0700714
715 return ret;
Duane Grigsbye84067d2017-06-21 13:48:43 -0700716}