blob: 99a56ca1fb1637ab44f4ff470de761100927c763 [file] [log] [blame]
Thomas Gleixner3287e962019-06-01 10:08:54 +02001// SPDX-License-Identifier: GPL-2.0-only
Dupuis, Chad61d86582017-02-15 06:28:23 -08002/*
3 * QLogic FCoE Offload Driver
Chad Dupuis5d1c8b52018-04-25 06:09:04 -07004 * Copyright (c) 2016-2018 Cavium Inc.
Dupuis, Chad61d86582017-02-15 06:28:23 -08005 */
6#include <linux/spinlock.h>
7#include <linux/vmalloc.h>
8#include "qedf.h"
9#include <scsi/scsi_tcq.h>
10
11void qedf_cmd_timer_set(struct qedf_ctx *qedf, struct qedf_ioreq *io_req,
12 unsigned int timer_msec)
13{
14 queue_delayed_work(qedf->timer_work_queue, &io_req->timeout_work,
15 msecs_to_jiffies(timer_msec));
16}
17
18static void qedf_cmd_timeout(struct work_struct *work)
19{
20
21 struct qedf_ioreq *io_req =
22 container_of(work, struct qedf_ioreq, timeout_work.work);
Chad Dupuisf3690a82018-04-25 06:09:03 -070023 struct qedf_ctx *qedf;
24 struct qedf_rport *fcport;
Dupuis, Chad61d86582017-02-15 06:28:23 -080025
Chad Dupuisf3690a82018-04-25 06:09:03 -070026 fcport = io_req->fcport;
27 if (io_req->fcport == NULL) {
28 QEDF_INFO(NULL, QEDF_LOG_IO, "fcport is NULL.\n");
29 return;
30 }
31
32 qedf = fcport->qedf;
33
Dupuis, Chad61d86582017-02-15 06:28:23 -080034 switch (io_req->cmd_type) {
35 case QEDF_ABTS:
Chad Dupuisf3690a82018-04-25 06:09:03 -070036 if (qedf == NULL) {
Saurav Kashyap69ef2c62019-03-26 00:38:38 -070037 QEDF_INFO(NULL, QEDF_LOG_IO,
38 "qedf is NULL for ABTS xid=0x%x.\n",
39 io_req->xid);
Chad Dupuisf3690a82018-04-25 06:09:03 -070040 return;
41 }
42
Dupuis, Chad61d86582017-02-15 06:28:23 -080043 QEDF_ERR((&qedf->dbg_ctx), "ABTS timeout, xid=0x%x.\n",
44 io_req->xid);
45 /* Cleanup timed out ABTS */
46 qedf_initiate_cleanup(io_req, true);
47 complete(&io_req->abts_done);
48
49 /*
50 * Need to call kref_put for reference taken when initiate_abts
51 * was called since abts_compl won't be called now that we've
52 * cleaned up the task.
53 */
54 kref_put(&io_req->refcount, qedf_release_cmd);
55
Saurav Kashyap69ef2c62019-03-26 00:38:38 -070056 /* Clear in abort bit now that we're done with the command */
57 clear_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
58
Dupuis, Chad61d86582017-02-15 06:28:23 -080059 /*
60 * Now that the original I/O and the ABTS are complete see
61 * if we need to reconnect to the target.
62 */
63 qedf_restart_rport(fcport);
64 break;
65 case QEDF_ELS:
Saurav Kashyap69ef2c62019-03-26 00:38:38 -070066 if (!qedf) {
67 QEDF_INFO(NULL, QEDF_LOG_IO,
68 "qedf is NULL for ELS xid=0x%x.\n",
69 io_req->xid);
70 return;
71 }
72 /* ELS request no longer outstanding since it timed out */
73 clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
74
Dupuis, Chad61d86582017-02-15 06:28:23 -080075 kref_get(&io_req->refcount);
76 /*
77 * Don't attempt to clean an ELS timeout as any subseqeunt
78 * ABTS or cleanup requests just hang. For now just free
79 * the resources of the original I/O and the RRQ
80 */
81 QEDF_ERR(&(qedf->dbg_ctx), "ELS timeout, xid=0x%x.\n",
82 io_req->xid);
Saurav Kashyap39d03572020-08-07 04:06:53 -070083 qedf_initiate_cleanup(io_req, true);
Dupuis, Chad61d86582017-02-15 06:28:23 -080084 io_req->event = QEDF_IOREQ_EV_ELS_TMO;
85 /* Call callback function to complete command */
86 if (io_req->cb_func && io_req->cb_arg) {
Dupuis, Chad61d86582017-02-15 06:28:23 -080087 io_req->cb_func(io_req->cb_arg);
88 io_req->cb_arg = NULL;
89 }
Dupuis, Chad61d86582017-02-15 06:28:23 -080090 kref_put(&io_req->refcount, qedf_release_cmd);
91 break;
92 case QEDF_SEQ_CLEANUP:
93 QEDF_ERR(&(qedf->dbg_ctx), "Sequence cleanup timeout, "
94 "xid=0x%x.\n", io_req->xid);
95 qedf_initiate_cleanup(io_req, true);
96 io_req->event = QEDF_IOREQ_EV_ELS_TMO;
97 qedf_process_seq_cleanup_compl(qedf, NULL, io_req);
98 break;
99 default:
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700100 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
101 "Hit default case, xid=0x%x.\n", io_req->xid);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800102 break;
103 }
104}
105
106void qedf_cmd_mgr_free(struct qedf_cmd_mgr *cmgr)
107{
108 struct io_bdt *bdt_info;
109 struct qedf_ctx *qedf = cmgr->qedf;
110 size_t bd_tbl_sz;
Chad Dupuis650ce642019-03-26 00:38:34 -0700111 u16 min_xid = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800112 u16 max_xid = (FCOE_PARAMS_NUM_TASKS - 1);
113 int num_ios;
114 int i;
115 struct qedf_ioreq *io_req;
116
117 num_ios = max_xid - min_xid + 1;
118
119 /* Free fcoe_bdt_ctx structures */
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700120 if (!cmgr->io_bdt_pool) {
121 QEDF_ERR(&qedf->dbg_ctx, "io_bdt_pool is NULL.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -0800122 goto free_cmd_pool;
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700123 }
Dupuis, Chad61d86582017-02-15 06:28:23 -0800124
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200125 bd_tbl_sz = QEDF_MAX_BDS_PER_CMD * sizeof(struct scsi_sge);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800126 for (i = 0; i < num_ios; i++) {
127 bdt_info = cmgr->io_bdt_pool[i];
128 if (bdt_info->bd_tbl) {
129 dma_free_coherent(&qedf->pdev->dev, bd_tbl_sz,
130 bdt_info->bd_tbl, bdt_info->bd_tbl_dma);
131 bdt_info->bd_tbl = NULL;
132 }
133 }
134
135 /* Destroy io_bdt pool */
136 for (i = 0; i < num_ios; i++) {
137 kfree(cmgr->io_bdt_pool[i]);
138 cmgr->io_bdt_pool[i] = NULL;
139 }
140
141 kfree(cmgr->io_bdt_pool);
142 cmgr->io_bdt_pool = NULL;
143
144free_cmd_pool:
145
146 for (i = 0; i < num_ios; i++) {
147 io_req = &cmgr->cmds[i];
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200148 kfree(io_req->sgl_task_params);
149 kfree(io_req->task_params);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800150 /* Make sure we free per command sense buffer */
151 if (io_req->sense_buffer)
152 dma_free_coherent(&qedf->pdev->dev,
153 QEDF_SCSI_SENSE_BUFFERSIZE, io_req->sense_buffer,
154 io_req->sense_buffer_dma);
155 cancel_delayed_work_sync(&io_req->rrq_work);
156 }
157
158 /* Free command manager itself */
159 vfree(cmgr);
160}
161
162static void qedf_handle_rrq(struct work_struct *work)
163{
164 struct qedf_ioreq *io_req =
165 container_of(work, struct qedf_ioreq, rrq_work.work);
166
Shyam Sundarfaea5712019-03-26 00:38:55 -0700167 atomic_set(&io_req->state, QEDFC_CMD_ST_RRQ_ACTIVE);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800168 qedf_send_rrq(io_req);
169
170}
171
172struct qedf_cmd_mgr *qedf_cmd_mgr_alloc(struct qedf_ctx *qedf)
173{
174 struct qedf_cmd_mgr *cmgr;
175 struct io_bdt *bdt_info;
176 struct qedf_ioreq *io_req;
177 u16 xid;
178 int i;
179 int num_ios;
Chad Dupuis650ce642019-03-26 00:38:34 -0700180 u16 min_xid = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800181 u16 max_xid = (FCOE_PARAMS_NUM_TASKS - 1);
182
183 /* Make sure num_queues is already set before calling this function */
184 if (!qedf->num_queues) {
185 QEDF_ERR(&(qedf->dbg_ctx), "num_queues is not set.\n");
186 return NULL;
187 }
188
189 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
190 QEDF_WARN(&(qedf->dbg_ctx), "Invalid min_xid 0x%x and "
191 "max_xid 0x%x.\n", min_xid, max_xid);
192 return NULL;
193 }
194
195 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "min xid 0x%x, max xid "
196 "0x%x.\n", min_xid, max_xid);
197
198 num_ios = max_xid - min_xid + 1;
199
200 cmgr = vzalloc(sizeof(struct qedf_cmd_mgr));
201 if (!cmgr) {
202 QEDF_WARN(&(qedf->dbg_ctx), "Failed to alloc cmd mgr.\n");
203 return NULL;
204 }
205
206 cmgr->qedf = qedf;
207 spin_lock_init(&cmgr->lock);
208
209 /*
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200210 * Initialize I/O request fields.
Dupuis, Chad61d86582017-02-15 06:28:23 -0800211 */
Chad Dupuis650ce642019-03-26 00:38:34 -0700212 xid = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800213
214 for (i = 0; i < num_ios; i++) {
215 io_req = &cmgr->cmds[i];
216 INIT_DELAYED_WORK(&io_req->timeout_work, qedf_cmd_timeout);
217
218 io_req->xid = xid++;
219
220 INIT_DELAYED_WORK(&io_req->rrq_work, qedf_handle_rrq);
221
222 /* Allocate DMA memory to hold sense buffer */
223 io_req->sense_buffer = dma_alloc_coherent(&qedf->pdev->dev,
224 QEDF_SCSI_SENSE_BUFFERSIZE, &io_req->sense_buffer_dma,
225 GFP_KERNEL);
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700226 if (!io_req->sense_buffer) {
227 QEDF_ERR(&qedf->dbg_ctx,
228 "Failed to alloc sense buffer.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -0800229 goto mem_err;
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700230 }
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200231
232 /* Allocate task parameters to pass to f/w init funcions */
233 io_req->task_params = kzalloc(sizeof(*io_req->task_params),
234 GFP_KERNEL);
235 if (!io_req->task_params) {
236 QEDF_ERR(&(qedf->dbg_ctx),
237 "Failed to allocate task_params for xid=0x%x\n",
238 i);
239 goto mem_err;
240 }
241
242 /*
243 * Allocate scatter/gather list info to pass to f/w init
244 * functions.
245 */
246 io_req->sgl_task_params = kzalloc(
247 sizeof(struct scsi_sgl_task_params), GFP_KERNEL);
248 if (!io_req->sgl_task_params) {
249 QEDF_ERR(&(qedf->dbg_ctx),
250 "Failed to allocate sgl_task_params for xid=0x%x\n",
251 i);
252 goto mem_err;
253 }
Dupuis, Chad61d86582017-02-15 06:28:23 -0800254 }
255
256 /* Allocate pool of io_bdts - one for each qedf_ioreq */
257 cmgr->io_bdt_pool = kmalloc_array(num_ios, sizeof(struct io_bdt *),
258 GFP_KERNEL);
259
260 if (!cmgr->io_bdt_pool) {
261 QEDF_WARN(&(qedf->dbg_ctx), "Failed to alloc io_bdt_pool.\n");
262 goto mem_err;
263 }
264
265 for (i = 0; i < num_ios; i++) {
266 cmgr->io_bdt_pool[i] = kmalloc(sizeof(struct io_bdt),
267 GFP_KERNEL);
268 if (!cmgr->io_bdt_pool[i]) {
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200269 QEDF_WARN(&(qedf->dbg_ctx),
270 "Failed to alloc io_bdt_pool[%d].\n", i);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800271 goto mem_err;
272 }
273 }
274
275 for (i = 0; i < num_ios; i++) {
276 bdt_info = cmgr->io_bdt_pool[i];
277 bdt_info->bd_tbl = dma_alloc_coherent(&qedf->pdev->dev,
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200278 QEDF_MAX_BDS_PER_CMD * sizeof(struct scsi_sge),
Dupuis, Chad61d86582017-02-15 06:28:23 -0800279 &bdt_info->bd_tbl_dma, GFP_KERNEL);
280 if (!bdt_info->bd_tbl) {
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200281 QEDF_WARN(&(qedf->dbg_ctx),
282 "Failed to alloc bdt_tbl[%d].\n", i);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800283 goto mem_err;
284 }
285 }
286 atomic_set(&cmgr->free_list_cnt, num_ios);
287 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
288 "cmgr->free_list_cnt=%d.\n",
289 atomic_read(&cmgr->free_list_cnt));
290
291 return cmgr;
292
293mem_err:
294 qedf_cmd_mgr_free(cmgr);
295 return NULL;
296}
297
298struct qedf_ioreq *qedf_alloc_cmd(struct qedf_rport *fcport, u8 cmd_type)
299{
300 struct qedf_ctx *qedf = fcport->qedf;
301 struct qedf_cmd_mgr *cmd_mgr = qedf->cmd_mgr;
302 struct qedf_ioreq *io_req = NULL;
303 struct io_bdt *bd_tbl;
304 u16 xid;
305 uint32_t free_sqes;
306 int i;
307 unsigned long flags;
308
309 free_sqes = atomic_read(&fcport->free_sqes);
310
311 if (!free_sqes) {
312 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
313 "Returning NULL, free_sqes=%d.\n ",
314 free_sqes);
315 goto out_failed;
316 }
317
318 /* Limit the number of outstanding R/W tasks */
319 if ((atomic_read(&fcport->num_active_ios) >=
320 NUM_RW_TASKS_PER_CONNECTION)) {
321 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
322 "Returning NULL, num_active_ios=%d.\n",
323 atomic_read(&fcport->num_active_ios));
324 goto out_failed;
325 }
326
327 /* Limit global TIDs certain tasks */
328 if (atomic_read(&cmd_mgr->free_list_cnt) <= GBL_RSVD_TASKS) {
329 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
330 "Returning NULL, free_list_cnt=%d.\n",
331 atomic_read(&cmd_mgr->free_list_cnt));
332 goto out_failed;
333 }
334
335 spin_lock_irqsave(&cmd_mgr->lock, flags);
336 for (i = 0; i < FCOE_PARAMS_NUM_TASKS; i++) {
337 io_req = &cmd_mgr->cmds[cmd_mgr->idx];
338 cmd_mgr->idx++;
339 if (cmd_mgr->idx == FCOE_PARAMS_NUM_TASKS)
340 cmd_mgr->idx = 0;
341
342 /* Check to make sure command was previously freed */
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700343 if (!io_req->alloc)
Dupuis, Chad61d86582017-02-15 06:28:23 -0800344 break;
345 }
346
347 if (i == FCOE_PARAMS_NUM_TASKS) {
348 spin_unlock_irqrestore(&cmd_mgr->lock, flags);
349 goto out_failed;
350 }
351
Shyam Sundarfeac47f2019-03-26 00:38:53 -0700352 if (test_bit(QEDF_CMD_DIRTY, &io_req->flags))
353 QEDF_ERR(&qedf->dbg_ctx,
354 "io_req found to be dirty ox_id = 0x%x.\n",
355 io_req->xid);
356
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700357 /* Clear any flags now that we've reallocated the xid */
358 io_req->flags = 0;
359 io_req->alloc = 1;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800360 spin_unlock_irqrestore(&cmd_mgr->lock, flags);
361
362 atomic_inc(&fcport->num_active_ios);
363 atomic_dec(&fcport->free_sqes);
364 xid = io_req->xid;
365 atomic_dec(&cmd_mgr->free_list_cnt);
366
367 io_req->cmd_mgr = cmd_mgr;
368 io_req->fcport = fcport;
369
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700370 /* Clear any stale sc_cmd back pointer */
371 io_req->sc_cmd = NULL;
372 io_req->lun = -1;
373
Dupuis, Chad61d86582017-02-15 06:28:23 -0800374 /* Hold the io_req against deletion */
Shyam Sundarfaea5712019-03-26 00:38:55 -0700375 kref_init(&io_req->refcount); /* ID: 001 */
376 atomic_set(&io_req->state, QEDFC_CMD_ST_IO_ACTIVE);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800377
378 /* Bind io_bdt for this io_req */
379 /* Have a static link between io_req and io_bdt_pool */
380 bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
381 if (bd_tbl == NULL) {
382 QEDF_ERR(&(qedf->dbg_ctx), "bd_tbl is NULL, xid=%x.\n", xid);
383 kref_put(&io_req->refcount, qedf_release_cmd);
384 goto out_failed;
385 }
386 bd_tbl->io_req = io_req;
387 io_req->cmd_type = cmd_type;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200388 io_req->tm_flags = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800389
390 /* Reset sequence offset data */
391 io_req->rx_buf_off = 0;
392 io_req->tx_buf_off = 0;
393 io_req->rx_id = 0xffff; /* No OX_ID */
394
395 return io_req;
396
397out_failed:
398 /* Record failure for stats and return NULL to caller */
399 qedf->alloc_failures++;
400 return NULL;
401}
402
403static void qedf_free_mp_resc(struct qedf_ioreq *io_req)
404{
405 struct qedf_mp_req *mp_req = &(io_req->mp_req);
406 struct qedf_ctx *qedf = io_req->fcport->qedf;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200407 uint64_t sz = sizeof(struct scsi_sge);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800408
409 /* clear tm flags */
Dupuis, Chad61d86582017-02-15 06:28:23 -0800410 if (mp_req->mp_req_bd) {
411 dma_free_coherent(&qedf->pdev->dev, sz,
412 mp_req->mp_req_bd, mp_req->mp_req_bd_dma);
413 mp_req->mp_req_bd = NULL;
414 }
415 if (mp_req->mp_resp_bd) {
416 dma_free_coherent(&qedf->pdev->dev, sz,
417 mp_req->mp_resp_bd, mp_req->mp_resp_bd_dma);
418 mp_req->mp_resp_bd = NULL;
419 }
420 if (mp_req->req_buf) {
421 dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
422 mp_req->req_buf, mp_req->req_buf_dma);
423 mp_req->req_buf = NULL;
424 }
425 if (mp_req->resp_buf) {
426 dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
427 mp_req->resp_buf, mp_req->resp_buf_dma);
428 mp_req->resp_buf = NULL;
429 }
430}
431
432void qedf_release_cmd(struct kref *ref)
433{
434 struct qedf_ioreq *io_req =
435 container_of(ref, struct qedf_ioreq, refcount);
436 struct qedf_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
437 struct qedf_rport *fcport = io_req->fcport;
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700438 unsigned long flags;
439
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700440 if (io_req->cmd_type == QEDF_SCSI_CMD) {
441 QEDF_WARN(&fcport->qedf->dbg_ctx,
442 "Cmd released called without scsi_done called, io_req %p xid=0x%x.\n",
443 io_req, io_req->xid);
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700444 WARN_ON(io_req->sc_cmd);
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700445 }
Dupuis, Chad61d86582017-02-15 06:28:23 -0800446
447 if (io_req->cmd_type == QEDF_ELS ||
448 io_req->cmd_type == QEDF_TASK_MGMT_CMD)
449 qedf_free_mp_resc(io_req);
450
451 atomic_inc(&cmd_mgr->free_list_cnt);
452 atomic_dec(&fcport->num_active_ios);
Shyam Sundarfaea5712019-03-26 00:38:55 -0700453 atomic_set(&io_req->state, QEDF_CMD_ST_INACTIVE);
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700454 if (atomic_read(&fcport->num_active_ios) < 0) {
Dupuis, Chad61d86582017-02-15 06:28:23 -0800455 QEDF_WARN(&(fcport->qedf->dbg_ctx), "active_ios < 0.\n");
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700456 WARN_ON(1);
457 }
Dupuis, Chad61d86582017-02-15 06:28:23 -0800458
459 /* Increment task retry identifier now that the request is released */
460 io_req->task_retry_identifier++;
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700461 io_req->fcport = NULL;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800462
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700463 clear_bit(QEDF_CMD_DIRTY, &io_req->flags);
464 io_req->cpu = 0;
465 spin_lock_irqsave(&cmd_mgr->lock, flags);
466 io_req->fcport = NULL;
467 io_req->alloc = 0;
468 spin_unlock_irqrestore(&cmd_mgr->lock, flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800469}
470
Dupuis, Chad61d86582017-02-15 06:28:23 -0800471static int qedf_map_sg(struct qedf_ioreq *io_req)
472{
473 struct scsi_cmnd *sc = io_req->sc_cmd;
474 struct Scsi_Host *host = sc->device->host;
475 struct fc_lport *lport = shost_priv(host);
476 struct qedf_ctx *qedf = lport_priv(lport);
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200477 struct scsi_sge *bd = io_req->bd_tbl->bd_tbl;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800478 struct scatterlist *sg;
479 int byte_count = 0;
480 int sg_count = 0;
481 int bd_count = 0;
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700482 u32 sg_len;
Lee Jones50efc512020-07-13 08:46:32 +0100483 u64 addr;
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700484 int i = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800485
486 sg_count = dma_map_sg(&qedf->pdev->dev, scsi_sglist(sc),
487 scsi_sg_count(sc), sc->sc_data_direction);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800488 sg = scsi_sglist(sc);
489
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700490 io_req->sge_type = QEDF_IOREQ_UNKNOWN_SGE;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800491
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700492 if (sg_count <= 8 || io_req->io_req_flags == QEDF_READ)
493 io_req->sge_type = QEDF_IOREQ_FAST_SGE;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800494
495 scsi_for_each_sg(sc, sg, sg_count, i) {
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700496 sg_len = (u32)sg_dma_len(sg);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800497 addr = (u64)sg_dma_address(sg);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800498
499 /*
Lee Jones50efc512020-07-13 08:46:32 +0100500 * Intermediate s/g element so check if start address
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700501 * is page aligned. Only required for writes and only if the
502 * number of scatter/gather elements is 8 or more.
Dupuis, Chad61d86582017-02-15 06:28:23 -0800503 */
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700504 if (io_req->sge_type == QEDF_IOREQ_UNKNOWN_SGE && (i) &&
505 (i != (sg_count - 1)) && sg_len < QEDF_PAGE_SIZE)
506 io_req->sge_type = QEDF_IOREQ_SLOW_SGE;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800507
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700508 bd[bd_count].sge_addr.lo = cpu_to_le32(U64_LO(addr));
509 bd[bd_count].sge_addr.hi = cpu_to_le32(U64_HI(addr));
510 bd[bd_count].sge_len = cpu_to_le32(sg_len);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800511
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700512 bd_count++;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800513 byte_count += sg_len;
514 }
515
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700516 /* To catch a case where FAST and SLOW nothing is set, set FAST */
517 if (io_req->sge_type == QEDF_IOREQ_UNKNOWN_SGE)
518 io_req->sge_type = QEDF_IOREQ_FAST_SGE;
519
Dupuis, Chad61d86582017-02-15 06:28:23 -0800520 if (byte_count != scsi_bufflen(sc))
521 QEDF_ERR(&(qedf->dbg_ctx), "byte_count = %d != "
522 "scsi_bufflen = %d, task_id = 0x%x.\n", byte_count,
523 scsi_bufflen(sc), io_req->xid);
524
525 return bd_count;
526}
527
528static int qedf_build_bd_list_from_sg(struct qedf_ioreq *io_req)
529{
530 struct scsi_cmnd *sc = io_req->sc_cmd;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200531 struct scsi_sge *bd = io_req->bd_tbl->bd_tbl;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800532 int bd_count;
533
534 if (scsi_sg_count(sc)) {
535 bd_count = qedf_map_sg(io_req);
536 if (bd_count == 0)
537 return -ENOMEM;
538 } else {
539 bd_count = 0;
540 bd[0].sge_addr.lo = bd[0].sge_addr.hi = 0;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200541 bd[0].sge_len = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800542 }
543 io_req->bd_tbl->bd_valid = bd_count;
544
545 return 0;
546}
547
548static void qedf_build_fcp_cmnd(struct qedf_ioreq *io_req,
549 struct fcp_cmnd *fcp_cmnd)
550{
551 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
552
553 /* fcp_cmnd is 32 bytes */
554 memset(fcp_cmnd, 0, FCP_CMND_LEN);
555
556 /* 8 bytes: SCSI LUN info */
557 int_to_scsilun(sc_cmd->device->lun,
558 (struct scsi_lun *)&fcp_cmnd->fc_lun);
559
560 /* 4 bytes: flag info */
561 fcp_cmnd->fc_pri_ta = 0;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200562 fcp_cmnd->fc_tm_flags = io_req->tm_flags;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800563 fcp_cmnd->fc_flags = io_req->io_req_flags;
564 fcp_cmnd->fc_cmdref = 0;
565
566 /* Populate data direction */
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200567 if (io_req->cmd_type == QEDF_TASK_MGMT_CMD) {
Dupuis, Chad61d86582017-02-15 06:28:23 -0800568 fcp_cmnd->fc_flags |= FCP_CFL_RDDATA;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200569 } else {
570 if (sc_cmd->sc_data_direction == DMA_TO_DEVICE)
571 fcp_cmnd->fc_flags |= FCP_CFL_WRDATA;
572 else if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE)
573 fcp_cmnd->fc_flags |= FCP_CFL_RDDATA;
574 }
Dupuis, Chad61d86582017-02-15 06:28:23 -0800575
576 fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
577
578 /* 16 bytes: CDB information */
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200579 if (io_req->cmd_type != QEDF_TASK_MGMT_CMD)
580 memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800581
582 /* 4 bytes: FCP data length */
583 fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800584}
585
586static void qedf_init_task(struct qedf_rport *fcport, struct fc_lport *lport,
Shai Malinfb09a1e2021-10-04 09:58:40 +0300587 struct qedf_ioreq *io_req, struct fcoe_task_context *task_ctx,
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200588 struct fcoe_wqe *sqe)
Dupuis, Chad61d86582017-02-15 06:28:23 -0800589{
590 enum fcoe_task_type task_type;
591 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
592 struct io_bdt *bd_tbl = io_req->bd_tbl;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200593 u8 fcp_cmnd[32];
Dupuis, Chad61d86582017-02-15 06:28:23 -0800594 u32 tmp_fcp_cmnd[8];
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200595 int bd_count = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800596 struct qedf_ctx *qedf = fcport->qedf;
597 uint16_t cq_idx = smp_processor_id() % qedf->num_queues;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200598 struct regpair sense_data_buffer_phys_addr;
599 u32 tx_io_size = 0;
600 u32 rx_io_size = 0;
601 int i, cnt;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800602
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200603 /* Note init_initiator_rw_fcoe_task memsets the task context */
Dupuis, Chad61d86582017-02-15 06:28:23 -0800604 io_req->task = task_ctx;
Shai Malinfb09a1e2021-10-04 09:58:40 +0300605 memset(task_ctx, 0, sizeof(struct fcoe_task_context));
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200606 memset(io_req->task_params, 0, sizeof(struct fcoe_task_params));
607 memset(io_req->sgl_task_params, 0, sizeof(struct scsi_sgl_task_params));
Dupuis, Chad61d86582017-02-15 06:28:23 -0800608
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200609 /* Set task type bassed on DMA directio of command */
610 if (io_req->cmd_type == QEDF_TASK_MGMT_CMD) {
Dupuis, Chad61d86582017-02-15 06:28:23 -0800611 task_type = FCOE_TASK_TYPE_READ_INITIATOR;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800612 } else {
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200613 if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
614 task_type = FCOE_TASK_TYPE_WRITE_INITIATOR;
615 tx_io_size = io_req->data_xfer_len;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800616 } else {
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200617 task_type = FCOE_TASK_TYPE_READ_INITIATOR;
618 rx_io_size = io_req->data_xfer_len;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800619 }
Dupuis, Chad61d86582017-02-15 06:28:23 -0800620 }
621
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200622 /* Setup the fields for fcoe_task_params */
623 io_req->task_params->context = task_ctx;
624 io_req->task_params->sqe = sqe;
625 io_req->task_params->task_type = task_type;
626 io_req->task_params->tx_io_size = tx_io_size;
627 io_req->task_params->rx_io_size = rx_io_size;
628 io_req->task_params->conn_cid = fcport->fw_cid;
629 io_req->task_params->itid = io_req->xid;
630 io_req->task_params->cq_rss_number = cq_idx;
631 io_req->task_params->is_tape_device = fcport->dev_type;
632
633 /* Fill in information for scatter/gather list */
634 if (io_req->cmd_type != QEDF_TASK_MGMT_CMD) {
635 bd_count = bd_tbl->bd_valid;
636 io_req->sgl_task_params->sgl = bd_tbl->bd_tbl;
637 io_req->sgl_task_params->sgl_phys_addr.lo =
638 U64_LO(bd_tbl->bd_tbl_dma);
639 io_req->sgl_task_params->sgl_phys_addr.hi =
640 U64_HI(bd_tbl->bd_tbl_dma);
641 io_req->sgl_task_params->num_sges = bd_count;
642 io_req->sgl_task_params->total_buffer_size =
643 scsi_bufflen(io_req->sc_cmd);
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700644 if (io_req->sge_type == QEDF_IOREQ_SLOW_SGE)
645 io_req->sgl_task_params->small_mid_sge = 1;
646 else
647 io_req->sgl_task_params->small_mid_sge = 0;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200648 }
649
650 /* Fill in physical address of sense buffer */
651 sense_data_buffer_phys_addr.lo = U64_LO(io_req->sense_buffer_dma);
652 sense_data_buffer_phys_addr.hi = U64_HI(io_req->sense_buffer_dma);
653
Dupuis, Chad61d86582017-02-15 06:28:23 -0800654 /* fill FCP_CMND IU */
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200655 qedf_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tmp_fcp_cmnd);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800656
657 /* Swap fcp_cmnd since FC is big endian */
658 cnt = sizeof(struct fcp_cmnd) / sizeof(u32);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800659 for (i = 0; i < cnt; i++) {
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200660 tmp_fcp_cmnd[i] = cpu_to_be32(tmp_fcp_cmnd[i]);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800661 }
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200662 memcpy(fcp_cmnd, tmp_fcp_cmnd, sizeof(struct fcp_cmnd));
Dupuis, Chad61d86582017-02-15 06:28:23 -0800663
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200664 init_initiator_rw_fcoe_task(io_req->task_params,
665 io_req->sgl_task_params,
666 sense_data_buffer_phys_addr,
667 io_req->task_retry_identifier, fcp_cmnd);
668
669 /* Increment SGL type counters */
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700670 if (io_req->sge_type == QEDF_IOREQ_SLOW_SGE)
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200671 qedf->slow_sge_ios++;
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700672 else
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200673 qedf->fast_sge_ios++;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800674}
675
676void qedf_init_mp_task(struct qedf_ioreq *io_req,
Shai Malinfb09a1e2021-10-04 09:58:40 +0300677 struct fcoe_task_context *task_ctx, struct fcoe_wqe *sqe)
Dupuis, Chad61d86582017-02-15 06:28:23 -0800678{
679 struct qedf_mp_req *mp_req = &(io_req->mp_req);
680 struct qedf_rport *fcport = io_req->fcport;
681 struct qedf_ctx *qedf = io_req->fcport->qedf;
682 struct fc_frame_header *fc_hdr;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200683 struct fcoe_tx_mid_path_params task_fc_hdr;
684 struct scsi_sgl_task_params tx_sgl_task_params;
685 struct scsi_sgl_task_params rx_sgl_task_params;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800686
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200687 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
688 "Initializing MP task for cmd_type=%d\n",
689 io_req->cmd_type);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800690
691 qedf->control_requests++;
692
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200693 memset(&tx_sgl_task_params, 0, sizeof(struct scsi_sgl_task_params));
694 memset(&rx_sgl_task_params, 0, sizeof(struct scsi_sgl_task_params));
Shai Malinfb09a1e2021-10-04 09:58:40 +0300695 memset(task_ctx, 0, sizeof(struct fcoe_task_context));
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200696 memset(&task_fc_hdr, 0, sizeof(struct fcoe_tx_mid_path_params));
Dupuis, Chad61d86582017-02-15 06:28:23 -0800697
698 /* Setup the task from io_req for easy reference */
699 io_req->task = task_ctx;
700
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200701 /* Setup the fields for fcoe_task_params */
702 io_req->task_params->context = task_ctx;
703 io_req->task_params->sqe = sqe;
704 io_req->task_params->task_type = FCOE_TASK_TYPE_MIDPATH;
705 io_req->task_params->tx_io_size = io_req->data_xfer_len;
706 /* rx_io_size tells the f/w how large a response buffer we have */
707 io_req->task_params->rx_io_size = PAGE_SIZE;
708 io_req->task_params->conn_cid = fcport->fw_cid;
709 io_req->task_params->itid = io_req->xid;
710 /* Return middle path commands on CQ 0 */
711 io_req->task_params->cq_rss_number = 0;
712 io_req->task_params->is_tape_device = fcport->dev_type;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800713
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200714 fc_hdr = &(mp_req->req_fc_hdr);
715 /* Set OX_ID and RX_ID based on driver task id */
716 fc_hdr->fh_ox_id = io_req->xid;
717 fc_hdr->fh_rx_id = htons(0xffff);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800718
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200719 /* Set up FC header information */
720 task_fc_hdr.parameter = fc_hdr->fh_parm_offset;
721 task_fc_hdr.r_ctl = fc_hdr->fh_r_ctl;
722 task_fc_hdr.type = fc_hdr->fh_type;
723 task_fc_hdr.cs_ctl = fc_hdr->fh_cs_ctl;
724 task_fc_hdr.df_ctl = fc_hdr->fh_df_ctl;
725 task_fc_hdr.rx_id = fc_hdr->fh_rx_id;
726 task_fc_hdr.ox_id = fc_hdr->fh_ox_id;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800727
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200728 /* Set up s/g list parameters for request buffer */
729 tx_sgl_task_params.sgl = mp_req->mp_req_bd;
730 tx_sgl_task_params.sgl_phys_addr.lo = U64_LO(mp_req->mp_req_bd_dma);
731 tx_sgl_task_params.sgl_phys_addr.hi = U64_HI(mp_req->mp_req_bd_dma);
732 tx_sgl_task_params.num_sges = 1;
733 /* Set PAGE_SIZE for now since sg element is that size ??? */
734 tx_sgl_task_params.total_buffer_size = io_req->data_xfer_len;
735 tx_sgl_task_params.small_mid_sge = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800736
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200737 /* Set up s/g list parameters for request buffer */
738 rx_sgl_task_params.sgl = mp_req->mp_resp_bd;
739 rx_sgl_task_params.sgl_phys_addr.lo = U64_LO(mp_req->mp_resp_bd_dma);
740 rx_sgl_task_params.sgl_phys_addr.hi = U64_HI(mp_req->mp_resp_bd_dma);
741 rx_sgl_task_params.num_sges = 1;
742 /* Set PAGE_SIZE for now since sg element is that size ??? */
743 rx_sgl_task_params.total_buffer_size = PAGE_SIZE;
744 rx_sgl_task_params.small_mid_sge = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800745
Dupuis, Chad61d86582017-02-15 06:28:23 -0800746
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200747 /*
748 * Last arg is 0 as previous code did not set that we wanted the
749 * fc header information.
750 */
751 init_initiator_midpath_unsolicited_fcoe_task(io_req->task_params,
752 &task_fc_hdr,
753 &tx_sgl_task_params,
754 &rx_sgl_task_params, 0);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800755}
756
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200757/* Presumed that fcport->rport_lock is held */
758u16 qedf_get_sqe_idx(struct qedf_rport *fcport)
Dupuis, Chad61d86582017-02-15 06:28:23 -0800759{
Dupuis, Chad61d86582017-02-15 06:28:23 -0800760 uint16_t total_sqe = (fcport->sq_mem_size)/(sizeof(struct fcoe_wqe));
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200761 u16 rval;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800762
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200763 rval = fcport->sq_prod_idx;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800764
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200765 /* Adjust ring index */
Dupuis, Chad61d86582017-02-15 06:28:23 -0800766 fcport->sq_prod_idx++;
767 fcport->fw_sq_prod_idx++;
768 if (fcport->sq_prod_idx == total_sqe)
769 fcport->sq_prod_idx = 0;
770
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200771 return rval;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800772}
773
774void qedf_ring_doorbell(struct qedf_rport *fcport)
775{
776 struct fcoe_db_data dbell = { 0 };
777
778 dbell.agg_flags = 0;
779
780 dbell.params |= DB_DEST_XCM << FCOE_DB_DATA_DEST_SHIFT;
781 dbell.params |= DB_AGG_CMD_SET << FCOE_DB_DATA_AGG_CMD_SHIFT;
782 dbell.params |= DQ_XCM_FCOE_SQ_PROD_CMD <<
783 FCOE_DB_DATA_AGG_VAL_SEL_SHIFT;
784
785 dbell.sq_prod = fcport->fw_sq_prod_idx;
Andrew Vasquez90ccf752019-03-26 00:38:40 -0700786 /* wmb makes sure that the BDs data is updated before updating the
787 * producer, otherwise FW may read old data from the BDs.
788 */
Dupuis, Chad61d86582017-02-15 06:28:23 -0800789 wmb();
Andrew Vasquez90ccf752019-03-26 00:38:40 -0700790 barrier();
791 writel(*(u32 *)&dbell, fcport->p_doorbell);
792 /*
793 * Fence required to flush the write combined buffer, since another
794 * CPU may write to the same doorbell address and data may be lost
795 * due to relaxed order nature of write combined bar.
796 */
797 wmb();
Dupuis, Chad61d86582017-02-15 06:28:23 -0800798}
799
800static void qedf_trace_io(struct qedf_rport *fcport, struct qedf_ioreq *io_req,
801 int8_t direction)
802{
803 struct qedf_ctx *qedf = fcport->qedf;
804 struct qedf_io_log *io_log;
805 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
806 unsigned long flags;
807 uint8_t op;
808
809 spin_lock_irqsave(&qedf->io_trace_lock, flags);
810
811 io_log = &qedf->io_trace_buf[qedf->io_trace_idx];
812 io_log->direction = direction;
813 io_log->task_id = io_req->xid;
814 io_log->port_id = fcport->rdata->ids.port_id;
815 io_log->lun = sc_cmd->device->lun;
816 io_log->op = op = sc_cmd->cmnd[0];
817 io_log->lba[0] = sc_cmd->cmnd[2];
818 io_log->lba[1] = sc_cmd->cmnd[3];
819 io_log->lba[2] = sc_cmd->cmnd[4];
820 io_log->lba[3] = sc_cmd->cmnd[5];
821 io_log->bufflen = scsi_bufflen(sc_cmd);
822 io_log->sg_count = scsi_sg_count(sc_cmd);
823 io_log->result = sc_cmd->result;
824 io_log->jiffies = jiffies;
Dupuis, Chad1afca6b2017-02-23 07:01:03 -0800825 io_log->refcount = kref_read(&io_req->refcount);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800826
827 if (direction == QEDF_IO_TRACE_REQ) {
828 /* For requests we only care abot the submission CPU */
829 io_log->req_cpu = io_req->cpu;
830 io_log->int_cpu = 0;
831 io_log->rsp_cpu = 0;
832 } else if (direction == QEDF_IO_TRACE_RSP) {
833 io_log->req_cpu = io_req->cpu;
834 io_log->int_cpu = io_req->int_cpu;
835 io_log->rsp_cpu = smp_processor_id();
836 }
837
838 io_log->sge_type = io_req->sge_type;
839
840 qedf->io_trace_idx++;
841 if (qedf->io_trace_idx == QEDF_IO_TRACE_SIZE)
842 qedf->io_trace_idx = 0;
843
844 spin_unlock_irqrestore(&qedf->io_trace_lock, flags);
845}
846
847int qedf_post_io_req(struct qedf_rport *fcport, struct qedf_ioreq *io_req)
848{
849 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
850 struct Scsi_Host *host = sc_cmd->device->host;
851 struct fc_lport *lport = shost_priv(host);
852 struct qedf_ctx *qedf = lport_priv(lport);
Shai Malinfb09a1e2021-10-04 09:58:40 +0300853 struct fcoe_task_context *task_ctx;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800854 u16 xid;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200855 struct fcoe_wqe *sqe;
856 u16 sqe_idx;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800857
858 /* Initialize rest of io_req fileds */
859 io_req->data_xfer_len = scsi_bufflen(sc_cmd);
860 sc_cmd->SCp.ptr = (char *)io_req;
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700861 io_req->sge_type = QEDF_IOREQ_FAST_SGE; /* Assume fast SGL by default */
Dupuis, Chad61d86582017-02-15 06:28:23 -0800862
863 /* Record which cpu this request is associated with */
864 io_req->cpu = smp_processor_id();
865
866 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
Dupuis, Chad61d86582017-02-15 06:28:23 -0800867 io_req->io_req_flags = QEDF_READ;
868 qedf->input_requests++;
869 } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
Dupuis, Chad61d86582017-02-15 06:28:23 -0800870 io_req->io_req_flags = QEDF_WRITE;
871 qedf->output_requests++;
872 } else {
873 io_req->io_req_flags = 0;
874 qedf->control_requests++;
875 }
876
877 xid = io_req->xid;
878
879 /* Build buffer descriptor list for firmware from sg list */
880 if (qedf_build_bd_list_from_sg(io_req)) {
881 QEDF_ERR(&(qedf->dbg_ctx), "BD list creation failed.\n");
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700882 /* Release cmd will release io_req, but sc_cmd is assigned */
883 io_req->sc_cmd = NULL;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800884 kref_put(&io_req->refcount, qedf_release_cmd);
885 return -EAGAIN;
886 }
887
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700888 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags) ||
889 test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200890 QEDF_ERR(&(qedf->dbg_ctx), "Session not offloaded yet.\n");
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700891 /* Release cmd will release io_req, but sc_cmd is assigned */
892 io_req->sc_cmd = NULL;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200893 kref_put(&io_req->refcount, qedf_release_cmd);
Chad Dupuisc5e06ba2019-03-26 00:38:35 -0700894 return -EINVAL;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200895 }
896
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700897 /* Record LUN number for later use if we neeed them */
898 io_req->lun = (int)sc_cmd->device->lun;
899
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200900 /* Obtain free SQE */
901 sqe_idx = qedf_get_sqe_idx(fcport);
902 sqe = &fcport->sq[sqe_idx];
903 memset(sqe, 0, sizeof(struct fcoe_wqe));
904
Dupuis, Chad61d86582017-02-15 06:28:23 -0800905 /* Get the task context */
906 task_ctx = qedf_get_task_mem(&qedf->tasks, xid);
907 if (!task_ctx) {
908 QEDF_WARN(&(qedf->dbg_ctx), "task_ctx is NULL, xid=%d.\n",
909 xid);
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700910 /* Release cmd will release io_req, but sc_cmd is assigned */
911 io_req->sc_cmd = NULL;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800912 kref_put(&io_req->refcount, qedf_release_cmd);
913 return -EINVAL;
914 }
915
Mintz, Yuvalbe086e72017-03-11 18:39:18 +0200916 qedf_init_task(fcport, lport, io_req, task_ctx, sqe);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800917
918 /* Ring doorbell */
919 qedf_ring_doorbell(fcport);
920
Shyam Sundar5d5e55652019-03-26 00:38:37 -0700921 /* Set that command is with the firmware now */
922 set_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
923
Dupuis, Chad61d86582017-02-15 06:28:23 -0800924 if (qedf_io_tracing && io_req->sc_cmd)
925 qedf_trace_io(fcport, io_req, QEDF_IO_TRACE_REQ);
926
927 return false;
928}
929
930int
931qedf_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc_cmd)
932{
933 struct fc_lport *lport = shost_priv(host);
934 struct qedf_ctx *qedf = lport_priv(lport);
935 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
936 struct fc_rport_libfc_priv *rp = rport->dd_data;
Colin Ian King8d6febb2018-02-06 14:03:16 +0000937 struct qedf_rport *fcport;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800938 struct qedf_ioreq *io_req;
939 int rc = 0;
940 int rval;
941 unsigned long flags = 0;
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700942 int num_sgs = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800943
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700944 num_sgs = scsi_sg_count(sc_cmd);
945 if (scsi_sg_count(sc_cmd) > QEDF_MAX_BDS_PER_CMD) {
946 QEDF_ERR(&qedf->dbg_ctx,
947 "Number of SG elements %d exceeds what hardware limitation of %d.\n",
948 num_sgs, QEDF_MAX_BDS_PER_CMD);
949 sc_cmd->result = DID_ERROR;
Bart Van Asscheef697682021-10-07 13:28:58 -0700950 scsi_done(sc_cmd);
Chad Dupuis3e2c11b2019-03-26 00:38:36 -0700951 return 0;
952 }
Dupuis, Chad61d86582017-02-15 06:28:23 -0800953
954 if (test_bit(QEDF_UNLOADING, &qedf->flags) ||
955 test_bit(QEDF_DBG_STOP_IO, &qedf->flags)) {
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700956 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
957 "Returning DNC as unloading or stop io, flags 0x%lx.\n",
958 qedf->flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800959 sc_cmd->result = DID_NO_CONNECT << 16;
Bart Van Asscheef697682021-10-07 13:28:58 -0700960 scsi_done(sc_cmd);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800961 return 0;
962 }
963
Chad Dupuisa8f192b2018-04-25 06:08:54 -0700964 if (!qedf->pdev->msix_enabled) {
965 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
966 "Completing sc_cmd=%p DID_NO_CONNECT as MSI-X is not enabled.\n",
967 sc_cmd);
968 sc_cmd->result = DID_NO_CONNECT << 16;
Bart Van Asscheef697682021-10-07 13:28:58 -0700969 scsi_done(sc_cmd);
Chad Dupuisa8f192b2018-04-25 06:08:54 -0700970 return 0;
971 }
972
Dupuis, Chad61d86582017-02-15 06:28:23 -0800973 rval = fc_remote_port_chkready(rport);
974 if (rval) {
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700975 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
976 "fc_remote_port_chkready failed=0x%x for port_id=0x%06x.\n",
977 rval, rport->port_id);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800978 sc_cmd->result = rval;
Bart Van Asscheef697682021-10-07 13:28:58 -0700979 scsi_done(sc_cmd);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800980 return 0;
981 }
982
983 /* Retry command if we are doing a qed drain operation */
984 if (test_bit(QEDF_DRAIN_ACTIVE, &qedf->flags)) {
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700985 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Drain active.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -0800986 rc = SCSI_MLQUEUE_HOST_BUSY;
987 goto exit_qcmd;
988 }
989
990 if (lport->state != LPORT_ST_READY ||
991 atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
Saurav Kashyape82e6ff2019-08-23 02:52:31 -0700992 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Link down.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -0800993 rc = SCSI_MLQUEUE_HOST_BUSY;
994 goto exit_qcmd;
995 }
996
997 /* rport and tgt are allocated together, so tgt should be non-NULL */
998 fcport = (struct qedf_rport *)&rp[1];
999
Shyam Sundarff543e22019-03-26 00:38:51 -07001000 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags) ||
1001 test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
Dupuis, Chad61d86582017-02-15 06:28:23 -08001002 /*
1003 * Session is not offloaded yet. Let SCSI-ml retry
1004 * the command.
1005 */
1006 rc = SCSI_MLQUEUE_TARGET_BUSY;
1007 goto exit_qcmd;
1008 }
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001009
1010 atomic_inc(&fcport->ios_to_queue);
1011
Dupuis, Chad61d86582017-02-15 06:28:23 -08001012 if (fcport->retry_delay_timestamp) {
Javed Hasan334b4f92020-04-16 01:43:08 -07001013 /* Take fcport->rport_lock for resetting the delay_timestamp */
1014 spin_lock_irqsave(&fcport->rport_lock, flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001015 if (time_after(jiffies, fcport->retry_delay_timestamp)) {
1016 fcport->retry_delay_timestamp = 0;
1017 } else {
Javed Hasan334b4f92020-04-16 01:43:08 -07001018 spin_unlock_irqrestore(&fcport->rport_lock, flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001019 /* If retry_delay timer is active, flow off the ML */
1020 rc = SCSI_MLQUEUE_TARGET_BUSY;
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001021 atomic_dec(&fcport->ios_to_queue);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001022 goto exit_qcmd;
1023 }
Javed Hasan334b4f92020-04-16 01:43:08 -07001024 spin_unlock_irqrestore(&fcport->rport_lock, flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001025 }
1026
1027 io_req = qedf_alloc_cmd(fcport, QEDF_SCSI_CMD);
1028 if (!io_req) {
1029 rc = SCSI_MLQUEUE_HOST_BUSY;
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001030 atomic_dec(&fcport->ios_to_queue);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001031 goto exit_qcmd;
1032 }
1033
1034 io_req->sc_cmd = sc_cmd;
1035
1036 /* Take fcport->rport_lock for posting to fcport send queue */
1037 spin_lock_irqsave(&fcport->rport_lock, flags);
1038 if (qedf_post_io_req(fcport, io_req)) {
1039 QEDF_WARN(&(qedf->dbg_ctx), "Unable to post io_req\n");
1040 /* Return SQE to pool */
1041 atomic_inc(&fcport->free_sqes);
1042 rc = SCSI_MLQUEUE_HOST_BUSY;
1043 }
1044 spin_unlock_irqrestore(&fcport->rport_lock, flags);
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001045 atomic_dec(&fcport->ios_to_queue);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001046
1047exit_qcmd:
1048 return rc;
1049}
1050
1051static void qedf_parse_fcp_rsp(struct qedf_ioreq *io_req,
1052 struct fcoe_cqe_rsp_info *fcp_rsp)
1053{
1054 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1055 struct qedf_ctx *qedf = io_req->fcport->qedf;
1056 u8 rsp_flags = fcp_rsp->rsp_flags.flags;
1057 int fcp_sns_len = 0;
1058 int fcp_rsp_len = 0;
1059 uint8_t *rsp_info, *sense_data;
1060
1061 io_req->fcp_status = FC_GOOD;
1062 io_req->fcp_resid = 0;
1063 if (rsp_flags & (FCOE_FCP_RSP_FLAGS_FCP_RESID_OVER |
1064 FCOE_FCP_RSP_FLAGS_FCP_RESID_UNDER))
1065 io_req->fcp_resid = fcp_rsp->fcp_resid;
1066
1067 io_req->scsi_comp_flags = rsp_flags;
1068 CMD_SCSI_STATUS(sc_cmd) = io_req->cdb_status =
1069 fcp_rsp->scsi_status_code;
1070
1071 if (rsp_flags &
1072 FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID)
1073 fcp_rsp_len = fcp_rsp->fcp_rsp_len;
1074
1075 if (rsp_flags &
1076 FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID)
1077 fcp_sns_len = fcp_rsp->fcp_sns_len;
1078
1079 io_req->fcp_rsp_len = fcp_rsp_len;
1080 io_req->fcp_sns_len = fcp_sns_len;
1081 rsp_info = sense_data = io_req->sense_buffer;
1082
1083 /* fetch fcp_rsp_code */
1084 if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
1085 /* Only for task management function */
1086 io_req->fcp_rsp_code = rsp_info[3];
1087 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1088 "fcp_rsp_code = %d\n", io_req->fcp_rsp_code);
1089 /* Adjust sense-data location. */
1090 sense_data += fcp_rsp_len;
1091 }
1092
1093 if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
1094 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1095 "Truncating sense buffer\n");
1096 fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
1097 }
1098
Dupuis, Chad16a61112017-06-02 12:02:05 -07001099 /* The sense buffer can be NULL for TMF commands */
1100 if (sc_cmd->sense_buffer) {
1101 memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1102 if (fcp_sns_len)
1103 memcpy(sc_cmd->sense_buffer, sense_data,
1104 fcp_sns_len);
1105 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001106}
1107
1108static void qedf_unmap_sg_list(struct qedf_ctx *qedf, struct qedf_ioreq *io_req)
1109{
1110 struct scsi_cmnd *sc = io_req->sc_cmd;
1111
1112 if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) {
1113 dma_unmap_sg(&qedf->pdev->dev, scsi_sglist(sc),
1114 scsi_sg_count(sc), sc->sc_data_direction);
1115 io_req->bd_tbl->bd_valid = 0;
1116 }
1117}
1118
1119void qedf_scsi_completion(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
1120 struct qedf_ioreq *io_req)
1121{
Dupuis, Chad61d86582017-02-15 06:28:23 -08001122 struct scsi_cmnd *sc_cmd;
1123 struct fcoe_cqe_rsp_info *fcp_rsp;
1124 struct qedf_rport *fcport;
1125 int refcount;
1126 u16 scope, qualifier = 0;
1127 u8 fw_residual_flag = 0;
Javed Hasanfedc1732020-04-16 01:43:09 -07001128 unsigned long flags = 0;
1129 u16 chk_scope = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001130
1131 if (!io_req)
1132 return;
1133 if (!cqe)
1134 return;
1135
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001136 if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
1137 test_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags) ||
1138 test_bit(QEDF_CMD_IN_ABORT, &io_req->flags)) {
1139 QEDF_ERR(&qedf->dbg_ctx,
1140 "io_req xid=0x%x already in cleanup or abort processing or already completed.\n",
1141 io_req->xid);
1142 return;
1143 }
1144
Dupuis, Chad61d86582017-02-15 06:28:23 -08001145 sc_cmd = io_req->sc_cmd;
1146 fcp_rsp = &cqe->cqe_info.rsp_info;
1147
1148 if (!sc_cmd) {
1149 QEDF_WARN(&(qedf->dbg_ctx), "sc_cmd is NULL!\n");
1150 return;
1151 }
1152
1153 if (!sc_cmd->SCp.ptr) {
1154 QEDF_WARN(&(qedf->dbg_ctx), "SCp.ptr is NULL, returned in "
1155 "another context.\n");
1156 return;
1157 }
1158
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001159 if (!sc_cmd->device) {
1160 QEDF_ERR(&qedf->dbg_ctx,
1161 "Device for sc_cmd %p is NULL.\n", sc_cmd);
1162 return;
1163 }
1164
Bart Van Assched995da62021-08-09 16:03:38 -07001165 if (!scsi_cmd_to_rq(sc_cmd)->q) {
Dupuis, Chad61d86582017-02-15 06:28:23 -08001166 QEDF_WARN(&(qedf->dbg_ctx), "request->q is NULL so request "
1167 "is not valid, sc_cmd=%p.\n", sc_cmd);
1168 return;
1169 }
1170
1171 fcport = io_req->fcport;
1172
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07001173 /*
1174 * When flush is active, let the cmds be completed from the cleanup
1175 * context
1176 */
1177 if (test_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags) ||
1178 (test_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags) &&
1179 sc_cmd->device->lun == (u64)fcport->lun_reset_lun)) {
1180 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1181 "Dropping good completion xid=0x%x as fcport is flushing",
1182 io_req->xid);
1183 return;
1184 }
1185
Dupuis, Chad61d86582017-02-15 06:28:23 -08001186 qedf_parse_fcp_rsp(io_req, fcp_rsp);
1187
1188 qedf_unmap_sg_list(qedf, io_req);
1189
1190 /* Check for FCP transport error */
1191 if (io_req->fcp_rsp_len > 3 && io_req->fcp_rsp_code) {
1192 QEDF_ERR(&(qedf->dbg_ctx),
1193 "FCP I/O protocol failure xid=0x%x fcp_rsp_len=%d "
1194 "fcp_rsp_code=%d.\n", io_req->xid, io_req->fcp_rsp_len,
1195 io_req->fcp_rsp_code);
1196 sc_cmd->result = DID_BUS_BUSY << 16;
1197 goto out;
1198 }
1199
1200 fw_residual_flag = GET_FIELD(cqe->cqe_info.rsp_info.fw_error_flags,
1201 FCOE_CQE_RSP_INFO_FW_UNDERRUN);
1202 if (fw_residual_flag) {
Chad Dupuis1c8162472019-04-21 22:44:53 -07001203 QEDF_ERR(&qedf->dbg_ctx,
1204 "Firmware detected underrun: xid=0x%x fcp_rsp.flags=0x%02x fcp_resid=%d fw_residual=0x%x lba=%02x%02x%02x%02x.\n",
1205 io_req->xid, fcp_rsp->rsp_flags.flags,
1206 io_req->fcp_resid,
1207 cqe->cqe_info.rsp_info.fw_residual, sc_cmd->cmnd[2],
1208 sc_cmd->cmnd[3], sc_cmd->cmnd[4], sc_cmd->cmnd[5]);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001209
1210 if (io_req->cdb_status == 0)
1211 sc_cmd->result = (DID_ERROR << 16) | io_req->cdb_status;
1212 else
1213 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1214
Dupuis, Chad61d86582017-02-15 06:28:23 -08001215 /*
1216 * Set resid to the whole buffer length so we won't try to resue
1217 * any previously data.
1218 */
1219 scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
1220 goto out;
1221 }
1222
1223 switch (io_req->fcp_status) {
1224 case FC_GOOD:
1225 if (io_req->cdb_status == 0) {
1226 /* Good I/O completion */
1227 sc_cmd->result = DID_OK << 16;
1228 } else {
Dupuis, Chad1afca6b2017-02-23 07:01:03 -08001229 refcount = kref_read(&io_req->refcount);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001230 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
Joe Perchesdb6b2062017-03-04 00:07:04 -08001231 "%d:0:%d:%lld xid=0x%0x op=0x%02x "
Dupuis, Chad61d86582017-02-15 06:28:23 -08001232 "lba=%02x%02x%02x%02x cdb_status=%d "
1233 "fcp_resid=0x%x refcount=%d.\n",
1234 qedf->lport->host->host_no, sc_cmd->device->id,
1235 sc_cmd->device->lun, io_req->xid,
1236 sc_cmd->cmnd[0], sc_cmd->cmnd[2], sc_cmd->cmnd[3],
1237 sc_cmd->cmnd[4], sc_cmd->cmnd[5],
1238 io_req->cdb_status, io_req->fcp_resid,
1239 refcount);
1240 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1241
1242 if (io_req->cdb_status == SAM_STAT_TASK_SET_FULL ||
1243 io_req->cdb_status == SAM_STAT_BUSY) {
1244 /*
1245 * Check whether we need to set retry_delay at
1246 * all based on retry_delay module parameter
1247 * and the status qualifier.
1248 */
1249
1250 /* Upper 2 bits */
1251 scope = fcp_rsp->retry_delay_timer & 0xC000;
1252 /* Lower 14 bits */
1253 qualifier = fcp_rsp->retry_delay_timer & 0x3FFF;
1254
Javed Hasanfedc1732020-04-16 01:43:09 -07001255 if (qedf_retry_delay)
1256 chk_scope = 1;
Chad Dupuis642a0b32018-05-22 00:28:43 -07001257 /* Record stats */
1258 if (io_req->cdb_status ==
1259 SAM_STAT_TASK_SET_FULL)
1260 qedf->task_set_fulls++;
1261 else
1262 qedf->busy++;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001263 }
1264 }
1265 if (io_req->fcp_resid)
1266 scsi_set_resid(sc_cmd, io_req->fcp_resid);
Javed Hasanfedc1732020-04-16 01:43:09 -07001267
1268 if (chk_scope == 1) {
1269 if ((scope == 1 || scope == 2) &&
1270 (qualifier > 0 && qualifier <= 0x3FEF)) {
1271 /* Check we don't go over the max */
1272 if (qualifier > QEDF_RETRY_DELAY_MAX) {
1273 qualifier = QEDF_RETRY_DELAY_MAX;
1274 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1275 "qualifier = %d\n",
1276 (fcp_rsp->retry_delay_timer &
1277 0x3FFF));
1278 }
1279 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1280 "Scope = %d and qualifier = %d",
1281 scope, qualifier);
1282 /* Take fcport->rport_lock to
1283 * update the retry_delay_timestamp
1284 */
1285 spin_lock_irqsave(&fcport->rport_lock, flags);
1286 fcport->retry_delay_timestamp =
1287 jiffies + (qualifier * HZ / 10);
1288 spin_unlock_irqrestore(&fcport->rport_lock,
1289 flags);
1290
1291 } else {
1292 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1293 "combination of scope = %d and qualifier = %d is not handled in qedf.\n",
1294 scope, qualifier);
1295 }
1296 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001297 break;
1298 default:
1299 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO, "fcp_status=%d.\n",
1300 io_req->fcp_status);
1301 break;
1302 }
1303
1304out:
1305 if (qedf_io_tracing)
1306 qedf_trace_io(fcport, io_req, QEDF_IO_TRACE_RSP);
1307
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001308 /*
1309 * We wait till the end of the function to clear the
1310 * outstanding bit in case we need to send an abort
1311 */
1312 clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
1313
Dupuis, Chad61d86582017-02-15 06:28:23 -08001314 io_req->sc_cmd = NULL;
1315 sc_cmd->SCp.ptr = NULL;
Bart Van Asscheef697682021-10-07 13:28:58 -07001316 scsi_done(sc_cmd);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001317 kref_put(&io_req->refcount, qedf_release_cmd);
1318}
1319
1320/* Return a SCSI command in some other context besides a normal completion */
1321void qedf_scsi_done(struct qedf_ctx *qedf, struct qedf_ioreq *io_req,
1322 int result)
1323{
Dupuis, Chad61d86582017-02-15 06:28:23 -08001324 struct scsi_cmnd *sc_cmd;
1325 int refcount;
1326
Saurav Kashyape82e6ff2019-08-23 02:52:31 -07001327 if (!io_req) {
1328 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "io_req is NULL\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08001329 return;
Saurav Kashyape82e6ff2019-08-23 02:52:31 -07001330 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001331
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001332 if (test_and_set_bit(QEDF_CMD_ERR_SCSI_DONE, &io_req->flags)) {
1333 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1334 "io_req:%p scsi_done handling already done\n",
1335 io_req);
1336 return;
1337 }
1338
1339 /*
1340 * We will be done with this command after this call so clear the
1341 * outstanding bit.
1342 */
1343 clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
1344
Dupuis, Chad61d86582017-02-15 06:28:23 -08001345 sc_cmd = io_req->sc_cmd;
1346
1347 if (!sc_cmd) {
1348 QEDF_WARN(&(qedf->dbg_ctx), "sc_cmd is NULL!\n");
1349 return;
1350 }
1351
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001352 if (!virt_addr_valid(sc_cmd)) {
1353 QEDF_ERR(&qedf->dbg_ctx, "sc_cmd=%p is not valid.", sc_cmd);
Chad Dupuis627cc7d2019-03-26 00:38:46 -07001354 goto bad_scsi_ptr;
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001355 }
1356
Dupuis, Chad61d86582017-02-15 06:28:23 -08001357 if (!sc_cmd->SCp.ptr) {
1358 QEDF_WARN(&(qedf->dbg_ctx), "SCp.ptr is NULL, returned in "
1359 "another context.\n");
1360 return;
1361 }
1362
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001363 if (!sc_cmd->device) {
1364 QEDF_ERR(&qedf->dbg_ctx, "Device for sc_cmd %p is NULL.\n",
1365 sc_cmd);
Chad Dupuis627cc7d2019-03-26 00:38:46 -07001366 goto bad_scsi_ptr;
1367 }
1368
1369 if (!virt_addr_valid(sc_cmd->device)) {
1370 QEDF_ERR(&qedf->dbg_ctx,
1371 "Device pointer for sc_cmd %p is bad.\n", sc_cmd);
1372 goto bad_scsi_ptr;
1373 }
1374
1375 if (!sc_cmd->sense_buffer) {
1376 QEDF_ERR(&qedf->dbg_ctx,
1377 "sc_cmd->sense_buffer for sc_cmd %p is NULL.\n",
1378 sc_cmd);
1379 goto bad_scsi_ptr;
1380 }
1381
1382 if (!virt_addr_valid(sc_cmd->sense_buffer)) {
1383 QEDF_ERR(&qedf->dbg_ctx,
1384 "sc_cmd->sense_buffer for sc_cmd %p is bad.\n",
1385 sc_cmd);
1386 goto bad_scsi_ptr;
1387 }
1388
Dupuis, Chad61d86582017-02-15 06:28:23 -08001389 qedf_unmap_sg_list(qedf, io_req);
1390
1391 sc_cmd->result = result << 16;
Dupuis, Chad1afca6b2017-02-23 07:01:03 -08001392 refcount = kref_read(&io_req->refcount);
Joe Perchesdb6b2062017-03-04 00:07:04 -08001393 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO, "%d:0:%d:%lld: Completing "
Dupuis, Chad61d86582017-02-15 06:28:23 -08001394 "sc_cmd=%p result=0x%08x op=0x%02x lba=0x%02x%02x%02x%02x, "
1395 "allowed=%d retries=%d refcount=%d.\n",
1396 qedf->lport->host->host_no, sc_cmd->device->id,
1397 sc_cmd->device->lun, sc_cmd, sc_cmd->result, sc_cmd->cmnd[0],
1398 sc_cmd->cmnd[2], sc_cmd->cmnd[3], sc_cmd->cmnd[4],
1399 sc_cmd->cmnd[5], sc_cmd->allowed, sc_cmd->retries,
1400 refcount);
1401
1402 /*
1403 * Set resid to the whole buffer length so we won't try to resue any
1404 * previously read data
1405 */
1406 scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
1407
1408 if (qedf_io_tracing)
1409 qedf_trace_io(io_req->fcport, io_req, QEDF_IO_TRACE_RSP);
1410
1411 io_req->sc_cmd = NULL;
1412 sc_cmd->SCp.ptr = NULL;
Bart Van Asscheef697682021-10-07 13:28:58 -07001413 scsi_done(sc_cmd);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001414 kref_put(&io_req->refcount, qedf_release_cmd);
Chad Dupuis6f15d0c2019-03-26 00:38:48 -07001415 return;
Chad Dupuis627cc7d2019-03-26 00:38:46 -07001416
1417bad_scsi_ptr:
1418 /*
1419 * Clear the io_req->sc_cmd backpointer so we don't try to process
1420 * this again
1421 */
1422 io_req->sc_cmd = NULL;
1423 kref_put(&io_req->refcount, qedf_release_cmd); /* ID: 001 */
Dupuis, Chad61d86582017-02-15 06:28:23 -08001424}
1425
1426/*
1427 * Handle warning type CQE completions. This is mainly used for REC timer
1428 * popping.
1429 */
1430void qedf_process_warning_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
1431 struct qedf_ioreq *io_req)
1432{
1433 int rval, i;
1434 struct qedf_rport *fcport = io_req->fcport;
1435 u64 err_warn_bit_map;
1436 u8 err_warn = 0xff;
1437
Saurav Kashyape82e6ff2019-08-23 02:52:31 -07001438 if (!cqe) {
1439 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1440 "cqe is NULL for io_req %p xid=0x%x\n",
1441 io_req, io_req->xid);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001442 return;
Saurav Kashyape82e6ff2019-08-23 02:52:31 -07001443 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001444
1445 QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "Warning CQE, "
1446 "xid=0x%x\n", io_req->xid);
1447 QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx),
1448 "err_warn_bitmap=%08x:%08x\n",
1449 le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_hi),
1450 le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_lo));
1451 QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "tx_buff_off=%08x, "
1452 "rx_buff_off=%08x, rx_id=%04x\n",
1453 le32_to_cpu(cqe->cqe_info.err_info.tx_buf_off),
1454 le32_to_cpu(cqe->cqe_info.err_info.rx_buf_off),
1455 le32_to_cpu(cqe->cqe_info.err_info.rx_id));
1456
1457 /* Normalize the error bitmap value to an just an unsigned int */
1458 err_warn_bit_map = (u64)
1459 ((u64)cqe->cqe_info.err_info.err_warn_bitmap_hi << 32) |
1460 (u64)cqe->cqe_info.err_info.err_warn_bitmap_lo;
1461 for (i = 0; i < 64; i++) {
1462 if (err_warn_bit_map & (u64)((u64)1 << i)) {
1463 err_warn = i;
1464 break;
1465 }
1466 }
1467
1468 /* Check if REC TOV expired if this is a tape device */
1469 if (fcport->dev_type == QEDF_RPORT_TYPE_TAPE) {
1470 if (err_warn ==
1471 FCOE_WARNING_CODE_REC_TOV_TIMER_EXPIRATION) {
1472 QEDF_ERR(&(qedf->dbg_ctx), "REC timer expired.\n");
1473 if (!test_bit(QEDF_CMD_SRR_SENT, &io_req->flags)) {
1474 io_req->rx_buf_off =
1475 cqe->cqe_info.err_info.rx_buf_off;
1476 io_req->tx_buf_off =
1477 cqe->cqe_info.err_info.tx_buf_off;
1478 io_req->rx_id = cqe->cqe_info.err_info.rx_id;
1479 rval = qedf_send_rec(io_req);
1480 /*
1481 * We only want to abort the io_req if we
1482 * can't queue the REC command as we want to
1483 * keep the exchange open for recovery.
1484 */
1485 if (rval)
1486 goto send_abort;
1487 }
1488 return;
1489 }
1490 }
1491
1492send_abort:
1493 init_completion(&io_req->abts_done);
1494 rval = qedf_initiate_abts(io_req, true);
1495 if (rval)
1496 QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
1497}
1498
1499/* Cleanup a command when we receive an error detection completion */
1500void qedf_process_error_detect(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
1501 struct qedf_ioreq *io_req)
1502{
1503 int rval;
1504
Javed Hasandf994462021-06-24 10:18:02 -07001505 if (io_req == NULL) {
1506 QEDF_INFO(NULL, QEDF_LOG_IO, "io_req is NULL.\n");
1507 return;
1508 }
1509
1510 if (io_req->fcport == NULL) {
1511 QEDF_INFO(NULL, QEDF_LOG_IO, "fcport is NULL.\n");
1512 return;
1513 }
1514
Saurav Kashyape82e6ff2019-08-23 02:52:31 -07001515 if (!cqe) {
1516 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
Javed Hasandf994462021-06-24 10:18:02 -07001517 "cqe is NULL for io_req %p\n", io_req);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001518 return;
Saurav Kashyape82e6ff2019-08-23 02:52:31 -07001519 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001520
1521 QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "Error detection CQE, "
1522 "xid=0x%x\n", io_req->xid);
1523 QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx),
1524 "err_warn_bitmap=%08x:%08x\n",
1525 le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_hi),
1526 le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_lo));
1527 QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "tx_buff_off=%08x, "
1528 "rx_buff_off=%08x, rx_id=%04x\n",
1529 le32_to_cpu(cqe->cqe_info.err_info.tx_buf_off),
1530 le32_to_cpu(cqe->cqe_info.err_info.rx_buf_off),
1531 le32_to_cpu(cqe->cqe_info.err_info.rx_id));
1532
Javed Hasandf994462021-06-24 10:18:02 -07001533 /* When flush is active, let the cmds be flushed out from the cleanup context */
1534 if (test_bit(QEDF_RPORT_IN_TARGET_RESET, &io_req->fcport->flags) ||
1535 (test_bit(QEDF_RPORT_IN_LUN_RESET, &io_req->fcport->flags) &&
1536 io_req->sc_cmd->device->lun == (u64)io_req->fcport->lun_reset_lun)) {
1537 QEDF_ERR(&qedf->dbg_ctx,
1538 "Dropping EQE for xid=0x%x as fcport is flushing",
1539 io_req->xid);
1540 return;
1541 }
1542
Dupuis, Chad61d86582017-02-15 06:28:23 -08001543 if (qedf->stop_io_on_error) {
1544 qedf_stop_all_io(qedf);
1545 return;
1546 }
1547
1548 init_completion(&io_req->abts_done);
1549 rval = qedf_initiate_abts(io_req, true);
1550 if (rval)
1551 QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
1552}
1553
1554static void qedf_flush_els_req(struct qedf_ctx *qedf,
1555 struct qedf_ioreq *els_req)
1556{
1557 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1558 "Flushing ELS request xid=0x%x refcount=%d.\n", els_req->xid,
Dupuis, Chad1afca6b2017-02-23 07:01:03 -08001559 kref_read(&els_req->refcount));
Dupuis, Chad61d86582017-02-15 06:28:23 -08001560
1561 /*
1562 * Need to distinguish this from a timeout when calling the
1563 * els_req->cb_func.
1564 */
1565 els_req->event = QEDF_IOREQ_EV_ELS_FLUSH;
1566
Saurav Kashyap30792852020-08-07 04:06:56 -07001567 clear_bit(QEDF_CMD_OUTSTANDING, &els_req->flags);
1568
Dupuis, Chad61d86582017-02-15 06:28:23 -08001569 /* Cancel the timer */
1570 cancel_delayed_work_sync(&els_req->timeout_work);
1571
1572 /* Call callback function to complete command */
1573 if (els_req->cb_func && els_req->cb_arg) {
1574 els_req->cb_func(els_req->cb_arg);
1575 els_req->cb_arg = NULL;
1576 }
1577
1578 /* Release kref for original initiate_els */
1579 kref_put(&els_req->refcount, qedf_release_cmd);
1580}
1581
1582/* A value of -1 for lun is a wild card that means flush all
1583 * active SCSI I/Os for the target.
1584 */
1585void qedf_flush_active_ios(struct qedf_rport *fcport, int lun)
1586{
1587 struct qedf_ioreq *io_req;
1588 struct qedf_ctx *qedf;
1589 struct qedf_cmd_mgr *cmd_mgr;
1590 int i, rc;
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001591 unsigned long flags;
1592 int flush_cnt = 0;
1593 int wait_cnt = 100;
1594 int refcount = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001595
Saurav Kashyape82e6ff2019-08-23 02:52:31 -07001596 if (!fcport) {
1597 QEDF_ERR(NULL, "fcport is NULL\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08001598 return;
Saurav Kashyape82e6ff2019-08-23 02:52:31 -07001599 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001600
Chad Dupuis766639c2018-04-25 06:08:49 -07001601 /* Check that fcport is still offloaded */
1602 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1603 QEDF_ERR(NULL, "fcport is no longer offloaded.\n");
1604 return;
1605 }
1606
Dupuis, Chad61d86582017-02-15 06:28:23 -08001607 qedf = fcport->qedf;
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001608
1609 if (!qedf) {
1610 QEDF_ERR(NULL, "qedf is NULL.\n");
1611 return;
1612 }
1613
1614 /* Only wait for all commands to be queued in the Upload context */
1615 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags) &&
1616 (lun == -1)) {
1617 while (atomic_read(&fcport->ios_to_queue)) {
1618 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1619 "Waiting for %d I/Os to be queued\n",
1620 atomic_read(&fcport->ios_to_queue));
1621 if (wait_cnt == 0) {
1622 QEDF_ERR(NULL,
1623 "%d IOs request could not be queued\n",
1624 atomic_read(&fcport->ios_to_queue));
1625 }
1626 msleep(20);
1627 wait_cnt--;
1628 }
1629 }
1630
Dupuis, Chad61d86582017-02-15 06:28:23 -08001631 cmd_mgr = qedf->cmd_mgr;
1632
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001633 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1634 "Flush active i/o's num=0x%x fcport=0x%p port_id=0x%06x scsi_id=%d.\n",
1635 atomic_read(&fcport->num_active_ios), fcport,
1636 fcport->rdata->ids.port_id, fcport->rport->scsi_target_id);
1637 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Locking flush mutex.\n");
1638
1639 mutex_lock(&qedf->flush_mutex);
1640 if (lun == -1) {
1641 set_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags);
1642 } else {
1643 set_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags);
1644 fcport->lun_reset_lun = lun;
1645 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001646
1647 for (i = 0; i < FCOE_PARAMS_NUM_TASKS; i++) {
1648 io_req = &cmd_mgr->cmds[i];
1649
1650 if (!io_req)
1651 continue;
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001652 if (!io_req->fcport)
1653 continue;
1654
1655 spin_lock_irqsave(&cmd_mgr->lock, flags);
1656
1657 if (io_req->alloc) {
1658 if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags)) {
1659 if (io_req->cmd_type == QEDF_SCSI_CMD)
1660 QEDF_ERR(&qedf->dbg_ctx,
1661 "Allocated but not queued, xid=0x%x\n",
1662 io_req->xid);
1663 }
1664 spin_unlock_irqrestore(&cmd_mgr->lock, flags);
1665 } else {
1666 spin_unlock_irqrestore(&cmd_mgr->lock, flags);
1667 continue;
1668 }
1669
Dupuis, Chad61d86582017-02-15 06:28:23 -08001670 if (io_req->fcport != fcport)
1671 continue;
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001672
1673 /* In case of ABTS, CMD_OUTSTANDING is cleared on ABTS response,
1674 * but RRQ is still pending.
1675 * Workaround: Within qedf_send_rrq, we check if the fcport is
1676 * NULL, and we drop the ref on the io_req to clean it up.
1677 */
1678 if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags)) {
1679 refcount = kref_read(&io_req->refcount);
1680 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1681 "Not outstanding, xid=0x%x, cmd_type=%d refcount=%d.\n",
1682 io_req->xid, io_req->cmd_type, refcount);
Shyam Sundarfaea5712019-03-26 00:38:55 -07001683 /* If RRQ work has been queue, try to cancel it and
1684 * free the io_req
1685 */
1686 if (atomic_read(&io_req->state) ==
1687 QEDFC_CMD_ST_RRQ_WAIT) {
1688 if (cancel_delayed_work_sync
1689 (&io_req->rrq_work)) {
1690 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1691 "Putting reference for pending RRQ work xid=0x%x.\n",
1692 io_req->xid);
1693 /* ID: 003 */
1694 kref_put(&io_req->refcount,
1695 qedf_release_cmd);
1696 }
1697 }
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001698 continue;
1699 }
1700
1701 /* Only consider flushing ELS during target reset */
1702 if (io_req->cmd_type == QEDF_ELS &&
1703 lun == -1) {
Dupuis, Chad61d86582017-02-15 06:28:23 -08001704 rc = kref_get_unless_zero(&io_req->refcount);
1705 if (!rc) {
1706 QEDF_ERR(&(qedf->dbg_ctx),
Chad Dupuis8025c842018-04-25 06:08:56 -07001707 "Could not get kref for ELS io_req=0x%p xid=0x%x.\n",
1708 io_req, io_req->xid);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001709 continue;
1710 }
Saurav Kashyap1f6d1d42020-08-07 04:06:54 -07001711 qedf_initiate_cleanup(io_req, false);
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001712 flush_cnt++;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001713 qedf_flush_els_req(qedf, io_req);
Saurav Kashyap1f6d1d42020-08-07 04:06:54 -07001714
Dupuis, Chad61d86582017-02-15 06:28:23 -08001715 /*
1716 * Release the kref and go back to the top of the
1717 * loop.
1718 */
1719 goto free_cmd;
1720 }
1721
Chad Dupuis92bbccd2018-04-25 06:09:01 -07001722 if (io_req->cmd_type == QEDF_ABTS) {
Shyam Sundarfaea5712019-03-26 00:38:55 -07001723 /* ID: 004 */
Chad Dupuis92bbccd2018-04-25 06:09:01 -07001724 rc = kref_get_unless_zero(&io_req->refcount);
1725 if (!rc) {
1726 QEDF_ERR(&(qedf->dbg_ctx),
1727 "Could not get kref for abort io_req=0x%p xid=0x%x.\n",
1728 io_req, io_req->xid);
1729 continue;
1730 }
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001731 if (lun != -1 && io_req->lun != lun)
1732 goto free_cmd;
1733
Chad Dupuis92bbccd2018-04-25 06:09:01 -07001734 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1735 "Flushing abort xid=0x%x.\n", io_req->xid);
1736
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001737 if (cancel_delayed_work_sync(&io_req->rrq_work)) {
1738 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
Shyam Sundarfaea5712019-03-26 00:38:55 -07001739 "Putting ref for cancelled RRQ work xid=0x%x.\n",
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001740 io_req->xid);
1741 kref_put(&io_req->refcount, qedf_release_cmd);
Chad Dupuis92bbccd2018-04-25 06:09:01 -07001742 }
1743
Shyam Sundarfaea5712019-03-26 00:38:55 -07001744 if (cancel_delayed_work_sync(&io_req->timeout_work)) {
1745 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1746 "Putting ref for cancelled tmo work xid=0x%x.\n",
1747 io_req->xid);
1748 qedf_initiate_cleanup(io_req, true);
1749 /* Notify eh_abort handler that ABTS is
1750 * complete
1751 */
1752 complete(&io_req->abts_done);
1753 clear_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
1754 /* ID: 002 */
1755 kref_put(&io_req->refcount, qedf_release_cmd);
1756 }
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001757 flush_cnt++;
Chad Dupuis92bbccd2018-04-25 06:09:01 -07001758 goto free_cmd;
1759 }
1760
Dupuis, Chad61d86582017-02-15 06:28:23 -08001761 if (!io_req->sc_cmd)
1762 continue;
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001763 if (!io_req->sc_cmd->device) {
1764 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1765 "Device backpointer NULL for sc_cmd=%p.\n",
1766 io_req->sc_cmd);
1767 /* Put reference for non-existent scsi_cmnd */
1768 io_req->sc_cmd = NULL;
1769 qedf_initiate_cleanup(io_req, false);
1770 kref_put(&io_req->refcount, qedf_release_cmd);
1771 continue;
1772 }
1773 if (lun > -1) {
1774 if (io_req->lun != lun)
Dupuis, Chad61d86582017-02-15 06:28:23 -08001775 continue;
1776 }
1777
1778 /*
1779 * Use kref_get_unless_zero in the unlikely case the command
1780 * we're about to flush was completed in the normal SCSI path
1781 */
1782 rc = kref_get_unless_zero(&io_req->refcount);
1783 if (!rc) {
1784 QEDF_ERR(&(qedf->dbg_ctx), "Could not get kref for "
Chad Dupuis8025c842018-04-25 06:08:56 -07001785 "io_req=0x%p xid=0x%x\n", io_req, io_req->xid);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001786 continue;
1787 }
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001788
Dupuis, Chad61d86582017-02-15 06:28:23 -08001789 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1790 "Cleanup xid=0x%x.\n", io_req->xid);
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001791 flush_cnt++;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001792
1793 /* Cleanup task and return I/O mid-layer */
1794 qedf_initiate_cleanup(io_req, true);
1795
1796free_cmd:
Shyam Sundarfaea5712019-03-26 00:38:55 -07001797 kref_put(&io_req->refcount, qedf_release_cmd); /* ID: 004 */
Dupuis, Chad61d86582017-02-15 06:28:23 -08001798 }
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001799
1800 wait_cnt = 60;
1801 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1802 "Flushed 0x%x I/Os, active=0x%x.\n",
1803 flush_cnt, atomic_read(&fcport->num_active_ios));
1804 /* Only wait for all commands to complete in the Upload context */
1805 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags) &&
1806 (lun == -1)) {
1807 while (atomic_read(&fcport->num_active_ios)) {
1808 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1809 "Flushed 0x%x I/Os, active=0x%x cnt=%d.\n",
1810 flush_cnt,
1811 atomic_read(&fcport->num_active_ios),
1812 wait_cnt);
1813 if (wait_cnt == 0) {
1814 QEDF_ERR(&qedf->dbg_ctx,
1815 "Flushed %d I/Os, active=%d.\n",
1816 flush_cnt,
1817 atomic_read(&fcport->num_active_ios));
1818 for (i = 0; i < FCOE_PARAMS_NUM_TASKS; i++) {
1819 io_req = &cmd_mgr->cmds[i];
1820 if (io_req->fcport &&
1821 io_req->fcport == fcport) {
1822 refcount =
1823 kref_read(&io_req->refcount);
Shyam Sundarfeac47f2019-03-26 00:38:53 -07001824 set_bit(QEDF_CMD_DIRTY,
1825 &io_req->flags);
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001826 QEDF_ERR(&qedf->dbg_ctx,
1827 "Outstanding io_req =%p xid=0x%x flags=0x%lx, sc_cmd=%p refcount=%d cmd_type=%d.\n",
1828 io_req, io_req->xid,
1829 io_req->flags,
1830 io_req->sc_cmd,
1831 refcount,
1832 io_req->cmd_type);
1833 }
1834 }
1835 WARN_ON(1);
1836 break;
1837 }
1838 msleep(500);
1839 wait_cnt--;
1840 }
1841 }
1842
1843 clear_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags);
1844 clear_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags);
1845 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Unlocking flush mutex.\n");
1846 mutex_unlock(&qedf->flush_mutex);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001847}
1848
1849/*
1850 * Initiate a ABTS middle path command. Note that we don't have to initialize
1851 * the task context for an ABTS task.
1852 */
1853int qedf_initiate_abts(struct qedf_ioreq *io_req, bool return_scsi_cmd_on_abts)
1854{
1855 struct fc_lport *lport;
1856 struct qedf_rport *fcport = io_req->fcport;
Chad Dupuisff34e8e2017-05-31 06:33:52 -07001857 struct fc_rport_priv *rdata;
1858 struct qedf_ctx *qedf;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001859 u16 xid;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001860 int rc = 0;
1861 unsigned long flags;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02001862 struct fcoe_wqe *sqe;
1863 u16 sqe_idx;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07001864 int refcount = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001865
Chad Dupuisff34e8e2017-05-31 06:33:52 -07001866 /* Sanity check qedf_rport before dereferencing any pointers */
Dupuis, Chad61d86582017-02-15 06:28:23 -08001867 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
Chad Dupuisff34e8e2017-05-31 06:33:52 -07001868 QEDF_ERR(NULL, "tgt not offloaded\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08001869 rc = 1;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07001870 goto out;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001871 }
1872
Hannes Reinecke56efc302019-03-26 00:38:49 -07001873 qedf = fcport->qedf;
Chad Dupuisff34e8e2017-05-31 06:33:52 -07001874 rdata = fcport->rdata;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07001875
1876 if (!rdata || !kref_get_unless_zero(&rdata->kref)) {
1877 QEDF_ERR(&qedf->dbg_ctx, "stale rport\n");
1878 rc = 1;
1879 goto out;
1880 }
1881
Chad Dupuisff34e8e2017-05-31 06:33:52 -07001882 lport = qedf->lport;
1883
Dupuis, Chad61d86582017-02-15 06:28:23 -08001884 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
1885 QEDF_ERR(&(qedf->dbg_ctx), "link is not ready\n");
1886 rc = 1;
Hannes Reinecke56efc302019-03-26 00:38:49 -07001887 goto drop_rdata_kref;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001888 }
1889
1890 if (atomic_read(&qedf->link_down_tmo_valid) > 0) {
1891 QEDF_ERR(&(qedf->dbg_ctx), "link_down_tmo active.\n");
1892 rc = 1;
Hannes Reinecke56efc302019-03-26 00:38:49 -07001893 goto drop_rdata_kref;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001894 }
1895
1896 /* Ensure room on SQ */
1897 if (!atomic_read(&fcport->free_sqes)) {
1898 QEDF_ERR(&(qedf->dbg_ctx), "No SQ entries available\n");
1899 rc = 1;
Hannes Reinecke56efc302019-03-26 00:38:49 -07001900 goto drop_rdata_kref;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001901 }
1902
Chad Dupuis92bbccd2018-04-25 06:09:01 -07001903 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
1904 QEDF_ERR(&qedf->dbg_ctx, "fcport is uploading.\n");
1905 rc = 1;
Hannes Reinecke56efc302019-03-26 00:38:49 -07001906 goto drop_rdata_kref;
Chad Dupuis92bbccd2018-04-25 06:09:01 -07001907 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001908
Chad Dupuisf3690a82018-04-25 06:09:03 -07001909 if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
1910 test_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags) ||
1911 test_bit(QEDF_CMD_IN_ABORT, &io_req->flags)) {
Chad Dupuis276eb3e2019-04-21 22:44:52 -07001912 QEDF_ERR(&qedf->dbg_ctx,
1913 "io_req xid=0x%x sc_cmd=%p already in cleanup or abort processing or already completed.\n",
1914 io_req->xid, io_req->sc_cmd);
Chad Dupuisf3690a82018-04-25 06:09:03 -07001915 rc = 1;
Hannes Reinecke56efc302019-03-26 00:38:49 -07001916 goto drop_rdata_kref;
Chad Dupuisf3690a82018-04-25 06:09:03 -07001917 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001918
1919 kref_get(&io_req->refcount);
1920
1921 xid = io_req->xid;
1922 qedf->control_requests++;
1923 qedf->packet_aborts++;
1924
Dupuis, Chad61d86582017-02-15 06:28:23 -08001925 /* Set the command type to abort */
1926 io_req->cmd_type = QEDF_ABTS;
1927 io_req->return_scsi_cmd_on_abts = return_scsi_cmd_on_abts;
1928
1929 set_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07001930 refcount = kref_read(&io_req->refcount);
1931 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
1932 "ABTS io_req xid = 0x%x refcount=%d\n",
1933 xid, refcount);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001934
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07001935 qedf_cmd_timer_set(qedf, io_req, QEDF_ABORT_TIMEOUT);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001936
1937 spin_lock_irqsave(&fcport->rport_lock, flags);
1938
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02001939 sqe_idx = qedf_get_sqe_idx(fcport);
1940 sqe = &fcport->sq[sqe_idx];
1941 memset(sqe, 0, sizeof(struct fcoe_wqe));
1942 io_req->task_params->sqe = sqe;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001943
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02001944 init_initiator_abort_fcoe_task(io_req->task_params);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001945 qedf_ring_doorbell(fcport);
1946
1947 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1948
Hannes Reinecke56efc302019-03-26 00:38:49 -07001949drop_rdata_kref:
1950 kref_put(&rdata->kref, fc_rport_destroy);
Chad Dupuis92bbccd2018-04-25 06:09:01 -07001951out:
Dupuis, Chad61d86582017-02-15 06:28:23 -08001952 return rc;
1953}
1954
1955void qedf_process_abts_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
1956 struct qedf_ioreq *io_req)
1957{
1958 uint32_t r_ctl;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07001959 int rc;
1960 struct qedf_rport *fcport = io_req->fcport;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001961
1962 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM, "Entered with xid = "
1963 "0x%x cmd_type = %d\n", io_req->xid, io_req->cmd_type);
1964
Dupuis, Chad61d86582017-02-15 06:28:23 -08001965 r_ctl = cqe->cqe_info.abts_info.r_ctl;
1966
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07001967 /* This was added at a point when we were scheduling abts_compl &
1968 * cleanup_compl on different CPUs and there was a possibility of
1969 * the io_req to be freed from the other context before we got here.
1970 */
1971 if (!fcport) {
1972 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1973 "Dropping ABTS completion xid=0x%x as fcport is NULL",
1974 io_req->xid);
1975 return;
1976 }
1977
1978 /*
1979 * When flush is active, let the cmds be completed from the cleanup
1980 * context
1981 */
1982 if (test_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags) ||
1983 test_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags)) {
1984 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1985 "Dropping ABTS completion xid=0x%x as fcport is flushing",
1986 io_req->xid);
1987 return;
1988 }
1989
1990 if (!cancel_delayed_work(&io_req->timeout_work)) {
1991 QEDF_ERR(&qedf->dbg_ctx,
1992 "Wasn't able to cancel abts timeout work.\n");
1993 }
1994
Dupuis, Chad61d86582017-02-15 06:28:23 -08001995 switch (r_ctl) {
1996 case FC_RCTL_BA_ACC:
1997 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM,
1998 "ABTS response - ACC Send RRQ after R_A_TOV\n");
1999 io_req->event = QEDF_IOREQ_EV_ABORT_SUCCESS;
Shyam Sundarfaea5712019-03-26 00:38:55 -07002000 rc = kref_get_unless_zero(&io_req->refcount); /* ID: 003 */
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002001 if (!rc) {
2002 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
2003 "kref is already zero so ABTS was already completed or flushed xid=0x%x.\n",
2004 io_req->xid);
2005 return;
2006 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08002007 /*
2008 * Dont release this cmd yet. It will be relesed
2009 * after we get RRQ response
2010 */
Dupuis, Chad61d86582017-02-15 06:28:23 -08002011 queue_delayed_work(qedf->dpc_wq, &io_req->rrq_work,
2012 msecs_to_jiffies(qedf->lport->r_a_tov));
Shyam Sundarfaea5712019-03-26 00:38:55 -07002013 atomic_set(&io_req->state, QEDFC_CMD_ST_RRQ_WAIT);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002014 break;
2015 /* For error cases let the cleanup return the command */
2016 case FC_RCTL_BA_RJT:
2017 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM,
2018 "ABTS response - RJT\n");
2019 io_req->event = QEDF_IOREQ_EV_ABORT_FAILED;
2020 break;
2021 default:
2022 QEDF_ERR(&(qedf->dbg_ctx), "Unknown ABTS response\n");
2023 break;
2024 }
2025
2026 clear_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
2027
2028 if (io_req->sc_cmd) {
Saurav Kashyape82e6ff2019-08-23 02:52:31 -07002029 if (!io_req->return_scsi_cmd_on_abts)
2030 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
2031 "Not call scsi_done for xid=0x%x.\n",
2032 io_req->xid);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002033 if (io_req->return_scsi_cmd_on_abts)
2034 qedf_scsi_done(qedf, io_req, DID_ERROR);
2035 }
2036
2037 /* Notify eh_abort handler that ABTS is complete */
2038 complete(&io_req->abts_done);
2039
2040 kref_put(&io_req->refcount, qedf_release_cmd);
2041}
2042
2043int qedf_init_mp_req(struct qedf_ioreq *io_req)
2044{
2045 struct qedf_mp_req *mp_req;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02002046 struct scsi_sge *mp_req_bd;
2047 struct scsi_sge *mp_resp_bd;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002048 struct qedf_ctx *qedf = io_req->fcport->qedf;
2049 dma_addr_t addr;
2050 uint64_t sz;
2051
2052 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_MP_REQ, "Entered.\n");
2053
2054 mp_req = (struct qedf_mp_req *)&(io_req->mp_req);
2055 memset(mp_req, 0, sizeof(struct qedf_mp_req));
2056
2057 if (io_req->cmd_type != QEDF_ELS) {
2058 mp_req->req_len = sizeof(struct fcp_cmnd);
2059 io_req->data_xfer_len = mp_req->req_len;
2060 } else
2061 mp_req->req_len = io_req->data_xfer_len;
2062
2063 mp_req->req_buf = dma_alloc_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
2064 &mp_req->req_buf_dma, GFP_KERNEL);
2065 if (!mp_req->req_buf) {
2066 QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc MP req buffer\n");
2067 qedf_free_mp_resc(io_req);
2068 return -ENOMEM;
2069 }
2070
2071 mp_req->resp_buf = dma_alloc_coherent(&qedf->pdev->dev,
2072 QEDF_PAGE_SIZE, &mp_req->resp_buf_dma, GFP_KERNEL);
2073 if (!mp_req->resp_buf) {
2074 QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc TM resp "
2075 "buffer\n");
2076 qedf_free_mp_resc(io_req);
2077 return -ENOMEM;
2078 }
2079
2080 /* Allocate and map mp_req_bd and mp_resp_bd */
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02002081 sz = sizeof(struct scsi_sge);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002082 mp_req->mp_req_bd = dma_alloc_coherent(&qedf->pdev->dev, sz,
2083 &mp_req->mp_req_bd_dma, GFP_KERNEL);
2084 if (!mp_req->mp_req_bd) {
2085 QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc MP req bd\n");
2086 qedf_free_mp_resc(io_req);
2087 return -ENOMEM;
2088 }
2089
2090 mp_req->mp_resp_bd = dma_alloc_coherent(&qedf->pdev->dev, sz,
2091 &mp_req->mp_resp_bd_dma, GFP_KERNEL);
2092 if (!mp_req->mp_resp_bd) {
2093 QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc MP resp bd\n");
2094 qedf_free_mp_resc(io_req);
2095 return -ENOMEM;
2096 }
2097
2098 /* Fill bd table */
2099 addr = mp_req->req_buf_dma;
2100 mp_req_bd = mp_req->mp_req_bd;
2101 mp_req_bd->sge_addr.lo = U64_LO(addr);
2102 mp_req_bd->sge_addr.hi = U64_HI(addr);
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02002103 mp_req_bd->sge_len = QEDF_PAGE_SIZE;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002104
2105 /*
2106 * MP buffer is either a task mgmt command or an ELS.
2107 * So the assumption is that it consumes a single bd
2108 * entry in the bd table
2109 */
2110 mp_resp_bd = mp_req->mp_resp_bd;
2111 addr = mp_req->resp_buf_dma;
2112 mp_resp_bd->sge_addr.lo = U64_LO(addr);
2113 mp_resp_bd->sge_addr.hi = U64_HI(addr);
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02002114 mp_resp_bd->sge_len = QEDF_PAGE_SIZE;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002115
2116 return 0;
2117}
2118
2119/*
2120 * Last ditch effort to clear the port if it's stuck. Used only after a
2121 * cleanup task times out.
2122 */
2123static void qedf_drain_request(struct qedf_ctx *qedf)
2124{
2125 if (test_bit(QEDF_DRAIN_ACTIVE, &qedf->flags)) {
2126 QEDF_ERR(&(qedf->dbg_ctx), "MCP drain already active.\n");
2127 return;
2128 }
2129
2130 /* Set bit to return all queuecommand requests as busy */
2131 set_bit(QEDF_DRAIN_ACTIVE, &qedf->flags);
2132
2133 /* Call qed drain request for function. Should be synchronous */
2134 qed_ops->common->drain(qedf->cdev);
2135
2136 /* Settle time for CQEs to be returned */
2137 msleep(100);
2138
2139 /* Unplug and continue */
2140 clear_bit(QEDF_DRAIN_ACTIVE, &qedf->flags);
2141}
2142
2143/*
2144 * Returns SUCCESS if the cleanup task does not timeout, otherwise return
2145 * FAILURE.
2146 */
2147int qedf_initiate_cleanup(struct qedf_ioreq *io_req,
2148 bool return_scsi_cmd_on_abts)
2149{
2150 struct qedf_rport *fcport;
2151 struct qedf_ctx *qedf;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002152 int tmo = 0;
2153 int rc = SUCCESS;
2154 unsigned long flags;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02002155 struct fcoe_wqe *sqe;
2156 u16 sqe_idx;
Shyam Sundar5d5e55652019-03-26 00:38:37 -07002157 int refcount = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002158
2159 fcport = io_req->fcport;
2160 if (!fcport) {
2161 QEDF_ERR(NULL, "fcport is NULL.\n");
2162 return SUCCESS;
2163 }
2164
Chad Dupuisff34e8e2017-05-31 06:33:52 -07002165 /* Sanity check qedf_rport before dereferencing any pointers */
2166 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
2167 QEDF_ERR(NULL, "tgt not offloaded\n");
Chad Dupuisff34e8e2017-05-31 06:33:52 -07002168 return SUCCESS;
2169 }
2170
Dupuis, Chad61d86582017-02-15 06:28:23 -08002171 qedf = fcport->qedf;
2172 if (!qedf) {
2173 QEDF_ERR(NULL, "qedf is NULL.\n");
2174 return SUCCESS;
2175 }
2176
Saurav Kashyap1f6d1d42020-08-07 04:06:54 -07002177 if (io_req->cmd_type == QEDF_ELS) {
2178 goto process_els;
2179 }
2180
Dupuis, Chad61d86582017-02-15 06:28:23 -08002181 if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
Shyam Sundar5d5e55652019-03-26 00:38:37 -07002182 test_and_set_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags)) {
Dupuis, Chad61d86582017-02-15 06:28:23 -08002183 QEDF_ERR(&(qedf->dbg_ctx), "io_req xid=0x%x already in "
2184 "cleanup processing or already completed.\n",
2185 io_req->xid);
2186 return SUCCESS;
2187 }
Chad Dupuis96b17652019-03-26 00:38:39 -07002188 set_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002189
Saurav Kashyap1f6d1d42020-08-07 04:06:54 -07002190process_els:
Dupuis, Chad61d86582017-02-15 06:28:23 -08002191 /* Ensure room on SQ */
2192 if (!atomic_read(&fcport->free_sqes)) {
2193 QEDF_ERR(&(qedf->dbg_ctx), "No SQ entries available\n");
Chad Dupuis96b17652019-03-26 00:38:39 -07002194 /* Need to make sure we clear the flag since it was set */
2195 clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002196 return FAILED;
2197 }
2198
Shyam Sundar5d5e55652019-03-26 00:38:37 -07002199 if (io_req->cmd_type == QEDF_CLEANUP) {
2200 QEDF_ERR(&qedf->dbg_ctx,
2201 "io_req=0x%x is already a cleanup command cmd_type=%d.\n",
2202 io_req->xid, io_req->cmd_type);
2203 clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2204 return SUCCESS;
2205 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08002206
Shyam Sundar5d5e55652019-03-26 00:38:37 -07002207 refcount = kref_read(&io_req->refcount);
2208
2209 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
Chad Dupuis50ed27c2019-04-21 22:44:54 -07002210 "Entered xid=0x%x sc_cmd=%p cmd_type=%d flags=0x%lx refcount=%d fcport=%p port_id=0x%06x\n",
Shyam Sundar5d5e55652019-03-26 00:38:37 -07002211 io_req->xid, io_req->sc_cmd, io_req->cmd_type, io_req->flags,
Chad Dupuis50ed27c2019-04-21 22:44:54 -07002212 refcount, fcport, fcport->rdata->ids.port_id);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002213
2214 /* Cleanup cmds re-use the same TID as the original I/O */
Dupuis, Chad61d86582017-02-15 06:28:23 -08002215 io_req->cmd_type = QEDF_CLEANUP;
2216 io_req->return_scsi_cmd_on_abts = return_scsi_cmd_on_abts;
2217
Chad Dupuis96b17652019-03-26 00:38:39 -07002218 init_completion(&io_req->cleanup_done);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002219
Dupuis, Chad61d86582017-02-15 06:28:23 -08002220 spin_lock_irqsave(&fcport->rport_lock, flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002221
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02002222 sqe_idx = qedf_get_sqe_idx(fcport);
2223 sqe = &fcport->sq[sqe_idx];
2224 memset(sqe, 0, sizeof(struct fcoe_wqe));
2225 io_req->task_params->sqe = sqe;
2226
2227 init_initiator_cleanup_fcoe_task(io_req->task_params);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002228 qedf_ring_doorbell(fcport);
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02002229
Dupuis, Chad61d86582017-02-15 06:28:23 -08002230 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2231
Chad Dupuis96b17652019-03-26 00:38:39 -07002232 tmo = wait_for_completion_timeout(&io_req->cleanup_done,
2233 QEDF_CLEANUP_TIMEOUT * HZ);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002234
2235 if (!tmo) {
2236 rc = FAILED;
2237 /* Timeout case */
2238 QEDF_ERR(&(qedf->dbg_ctx), "Cleanup command timeout, "
2239 "xid=%x.\n", io_req->xid);
2240 clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2241 /* Issue a drain request if cleanup task times out */
2242 QEDF_ERR(&(qedf->dbg_ctx), "Issuing MCP drain request.\n");
2243 qedf_drain_request(qedf);
2244 }
2245
Shyam Sundar5d5e55652019-03-26 00:38:37 -07002246 /* If it TASK MGMT handle it, reference will be decreased
2247 * in qedf_execute_tmf
2248 */
Saurav Kashyapf2c98af2019-03-26 00:38:54 -07002249 if (io_req->tm_flags == FCP_TMF_LUN_RESET ||
2250 io_req->tm_flags == FCP_TMF_TGT_RESET) {
Shyam Sundar5d5e55652019-03-26 00:38:37 -07002251 clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
2252 io_req->sc_cmd = NULL;
2253 complete(&io_req->tm_done);
2254 }
2255
Dupuis, Chad61d86582017-02-15 06:28:23 -08002256 if (io_req->sc_cmd) {
Saurav Kashyape82e6ff2019-08-23 02:52:31 -07002257 if (!io_req->return_scsi_cmd_on_abts)
2258 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
2259 "Not call scsi_done for xid=0x%x.\n",
2260 io_req->xid);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002261 if (io_req->return_scsi_cmd_on_abts)
2262 qedf_scsi_done(qedf, io_req, DID_ERROR);
2263 }
2264
2265 if (rc == SUCCESS)
2266 io_req->event = QEDF_IOREQ_EV_CLEANUP_SUCCESS;
2267 else
2268 io_req->event = QEDF_IOREQ_EV_CLEANUP_FAILED;
2269
2270 return rc;
2271}
2272
2273void qedf_process_cleanup_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
2274 struct qedf_ioreq *io_req)
2275{
2276 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO, "Entered xid = 0x%x\n",
2277 io_req->xid);
2278
2279 clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2280
2281 /* Complete so we can finish cleaning up the I/O */
Chad Dupuis96b17652019-03-26 00:38:39 -07002282 complete(&io_req->cleanup_done);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002283}
2284
2285static int qedf_execute_tmf(struct qedf_rport *fcport, struct scsi_cmnd *sc_cmd,
2286 uint8_t tm_flags)
2287{
2288 struct qedf_ioreq *io_req;
Shai Malinfb09a1e2021-10-04 09:58:40 +03002289 struct fcoe_task_context *task;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002290 struct qedf_ctx *qedf = fcport->qedf;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02002291 struct fc_lport *lport = qedf->lport;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002292 int rc = 0;
2293 uint16_t xid;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002294 int tmo = 0;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002295 int lun = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002296 unsigned long flags;
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02002297 struct fcoe_wqe *sqe;
2298 u16 sqe_idx;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002299
2300 if (!sc_cmd) {
Saurav Kashyape82e6ff2019-08-23 02:52:31 -07002301 QEDF_ERR(&qedf->dbg_ctx, "sc_cmd is NULL\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08002302 return FAILED;
2303 }
2304
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002305 lun = (int)sc_cmd->device->lun;
Chad Dupuis57a35482017-05-31 06:33:58 -07002306 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
Dupuis, Chad61d86582017-02-15 06:28:23 -08002307 QEDF_ERR(&(qedf->dbg_ctx), "fcport not offloaded\n");
2308 rc = FAILED;
Saurav Kashyapfe2043d2019-03-26 00:38:56 -07002309 goto no_flush;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002310 }
2311
Dupuis, Chad61d86582017-02-15 06:28:23 -08002312 io_req = qedf_alloc_cmd(fcport, QEDF_TASK_MGMT_CMD);
2313 if (!io_req) {
2314 QEDF_ERR(&(qedf->dbg_ctx), "Failed TMF");
2315 rc = -EAGAIN;
Saurav Kashyapfe2043d2019-03-26 00:38:56 -07002316 goto no_flush;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002317 }
2318
Chad Dupuis642a0b32018-05-22 00:28:43 -07002319 if (tm_flags == FCP_TMF_LUN_RESET)
2320 qedf->lun_resets++;
2321 else if (tm_flags == FCP_TMF_TGT_RESET)
2322 qedf->target_resets++;
2323
Dupuis, Chad61d86582017-02-15 06:28:23 -08002324 /* Initialize rest of io_req fields */
2325 io_req->sc_cmd = sc_cmd;
2326 io_req->fcport = fcport;
2327 io_req->cmd_type = QEDF_TASK_MGMT_CMD;
2328
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002329 /* Record which cpu this request is associated with */
Dupuis, Chad61d86582017-02-15 06:28:23 -08002330 io_req->cpu = smp_processor_id();
2331
Dupuis, Chad61d86582017-02-15 06:28:23 -08002332 /* Set TM flags */
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02002333 io_req->io_req_flags = QEDF_READ;
2334 io_req->data_xfer_len = 0;
2335 io_req->tm_flags = tm_flags;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002336
2337 /* Default is to return a SCSI command when an error occurs */
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002338 io_req->return_scsi_cmd_on_abts = false;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002339
Dupuis, Chad61d86582017-02-15 06:28:23 -08002340 /* Obtain exchange id */
2341 xid = io_req->xid;
2342
2343 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM, "TMF io_req xid = "
2344 "0x%x\n", xid);
2345
2346 /* Initialize task context for this IO request */
2347 task = qedf_get_task_mem(&qedf->tasks, xid);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002348
2349 init_completion(&io_req->tm_done);
2350
Dupuis, Chad61d86582017-02-15 06:28:23 -08002351 spin_lock_irqsave(&fcport->rport_lock, flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002352
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02002353 sqe_idx = qedf_get_sqe_idx(fcport);
2354 sqe = &fcport->sq[sqe_idx];
2355 memset(sqe, 0, sizeof(struct fcoe_wqe));
2356
2357 qedf_init_task(fcport, lport, io_req, task, sqe);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002358 qedf_ring_doorbell(fcport);
Mintz, Yuvalbe086e72017-03-11 18:39:18 +02002359
Dupuis, Chad61d86582017-02-15 06:28:23 -08002360 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2361
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002362 set_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002363 tmo = wait_for_completion_timeout(&io_req->tm_done,
2364 QEDF_TM_TIMEOUT * HZ);
2365
2366 if (!tmo) {
2367 rc = FAILED;
2368 QEDF_ERR(&(qedf->dbg_ctx), "wait for tm_cmpl timeout!\n");
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002369 /* Clear outstanding bit since command timed out */
2370 clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
2371 io_req->sc_cmd = NULL;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002372 } else {
2373 /* Check TMF response code */
2374 if (io_req->fcp_rsp_code == 0)
2375 rc = SUCCESS;
2376 else
2377 rc = FAILED;
2378 }
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002379 /*
2380 * Double check that fcport has not gone into an uploading state before
2381 * executing the command flush for the LUN/target.
2382 */
2383 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
2384 QEDF_ERR(&qedf->dbg_ctx,
2385 "fcport is uploading, not executing flush.\n");
2386 goto no_flush;
2387 }
2388 /* We do not need this io_req any more */
2389 kref_put(&io_req->refcount, qedf_release_cmd);
2390
Dupuis, Chad61d86582017-02-15 06:28:23 -08002391
2392 if (tm_flags == FCP_TMF_LUN_RESET)
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002393 qedf_flush_active_ios(fcport, lun);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002394 else
2395 qedf_flush_active_ios(fcport, -1);
2396
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002397no_flush:
Dupuis, Chad61d86582017-02-15 06:28:23 -08002398 if (rc != SUCCESS) {
2399 QEDF_ERR(&(qedf->dbg_ctx), "task mgmt command failed...\n");
2400 rc = FAILED;
2401 } else {
2402 QEDF_ERR(&(qedf->dbg_ctx), "task mgmt command success...\n");
2403 rc = SUCCESS;
2404 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08002405 return rc;
2406}
2407
2408int qedf_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
2409{
2410 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
2411 struct fc_rport_libfc_priv *rp = rport->dd_data;
2412 struct qedf_rport *fcport = (struct qedf_rport *)&rp[1];
2413 struct qedf_ctx *qedf;
Saurav Kashyapefc8fe92019-03-26 00:38:57 -07002414 struct fc_lport *lport = shost_priv(sc_cmd->device->host);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002415 int rc = SUCCESS;
2416 int rval;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002417 struct qedf_ioreq *io_req = NULL;
2418 int ref_cnt = 0;
2419 struct fc_rport_priv *rdata = fcport->rdata;
2420
2421 QEDF_ERR(NULL,
2422 "tm_flags 0x%x sc_cmd %p op = 0x%02x target_id = 0x%x lun=%d\n",
Arun Easi47aeee52019-08-23 02:52:33 -07002423 tm_flags, sc_cmd, sc_cmd->cmd_len ? sc_cmd->cmnd[0] : 0xff,
2424 rport->scsi_target_id, (int)sc_cmd->device->lun);
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002425
2426 if (!rdata || !kref_get_unless_zero(&rdata->kref)) {
2427 QEDF_ERR(NULL, "stale rport\n");
2428 return FAILED;
2429 }
2430
2431 QEDF_ERR(NULL, "portid=%06x tm_flags =%s\n", rdata->ids.port_id,
2432 (tm_flags == FCP_TMF_TGT_RESET) ? "TARGET RESET" :
2433 "LUN RESET");
2434
2435 if (sc_cmd->SCp.ptr) {
2436 io_req = (struct qedf_ioreq *)sc_cmd->SCp.ptr;
2437 ref_cnt = kref_read(&io_req->refcount);
2438 QEDF_ERR(NULL,
2439 "orig io_req = %p xid = 0x%x ref_cnt = %d.\n",
2440 io_req, io_req->xid, ref_cnt);
2441 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08002442
2443 rval = fc_remote_port_chkready(rport);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002444 if (rval) {
2445 QEDF_ERR(NULL, "device_reset rport not ready\n");
2446 rc = FAILED;
2447 goto tmf_err;
2448 }
2449
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002450 rc = fc_block_scsi_eh(sc_cmd);
2451 if (rc)
Saurav Kashyapfe2043d2019-03-26 00:38:56 -07002452 goto tmf_err;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002453
2454 if (!fcport) {
Dupuis, Chad61d86582017-02-15 06:28:23 -08002455 QEDF_ERR(NULL, "device_reset: rport is NULL\n");
2456 rc = FAILED;
2457 goto tmf_err;
2458 }
2459
2460 qedf = fcport->qedf;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002461
2462 if (!qedf) {
2463 QEDF_ERR(NULL, "qedf is NULL.\n");
2464 rc = FAILED;
2465 goto tmf_err;
2466 }
2467
Shyam Sundarff543e22019-03-26 00:38:51 -07002468 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
2469 QEDF_ERR(&qedf->dbg_ctx, "Connection is getting uploaded.\n");
2470 rc = SUCCESS;
2471 goto tmf_err;
2472 }
2473
Dupuis, Chad61d86582017-02-15 06:28:23 -08002474 if (test_bit(QEDF_UNLOADING, &qedf->flags) ||
2475 test_bit(QEDF_DBG_STOP_IO, &qedf->flags)) {
2476 rc = SUCCESS;
2477 goto tmf_err;
2478 }
2479
2480 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
2481 QEDF_ERR(&(qedf->dbg_ctx), "link is not ready\n");
2482 rc = FAILED;
2483 goto tmf_err;
2484 }
2485
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002486 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
Saurav Kashyapfe2043d2019-03-26 00:38:56 -07002487 if (!fcport->rdata)
2488 QEDF_ERR(&qedf->dbg_ctx, "fcport %p is uploading.\n",
2489 fcport);
2490 else
2491 QEDF_ERR(&qedf->dbg_ctx,
2492 "fcport %p port_id=%06x is uploading.\n",
2493 fcport, fcport->rdata->ids.port_id);
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002494 rc = FAILED;
2495 goto tmf_err;
2496 }
2497
Dupuis, Chad61d86582017-02-15 06:28:23 -08002498 rc = qedf_execute_tmf(fcport, sc_cmd, tm_flags);
2499
2500tmf_err:
Saurav Kashyapfe2043d2019-03-26 00:38:56 -07002501 kref_put(&rdata->kref, fc_rport_destroy);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002502 return rc;
2503}
2504
2505void qedf_process_tmf_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
2506 struct qedf_ioreq *io_req)
2507{
2508 struct fcoe_cqe_rsp_info *fcp_rsp;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002509
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07002510 clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
2511
Dupuis, Chad61d86582017-02-15 06:28:23 -08002512 fcp_rsp = &cqe->cqe_info.rsp_info;
2513 qedf_parse_fcp_rsp(io_req, fcp_rsp);
2514
2515 io_req->sc_cmd = NULL;
2516 complete(&io_req->tm_done);
2517}
2518
2519void qedf_process_unsol_compl(struct qedf_ctx *qedf, uint16_t que_idx,
2520 struct fcoe_cqe *cqe)
2521{
2522 unsigned long flags;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002523 uint16_t pktlen = cqe->cqe_info.unsolic_info.pkt_len;
2524 u32 payload_len, crc;
2525 struct fc_frame_header *fh;
2526 struct fc_frame *fp;
2527 struct qedf_io_work *io_work;
2528 u32 bdq_idx;
2529 void *bdq_addr;
Tomer Tayarda090912017-12-27 19:30:07 +02002530 struct scsi_bd *p_bd_info;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002531
Tomer Tayarda090912017-12-27 19:30:07 +02002532 p_bd_info = &cqe->cqe_info.unsolic_info.bd_info;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002533 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
Tomer Tayarda090912017-12-27 19:30:07 +02002534 "address.hi=%x, address.lo=%x, opaque_data.hi=%x, opaque_data.lo=%x, bdq_prod_idx=%u, len=%u\n",
2535 le32_to_cpu(p_bd_info->address.hi),
2536 le32_to_cpu(p_bd_info->address.lo),
2537 le32_to_cpu(p_bd_info->opaque.fcoe_opaque.hi),
2538 le32_to_cpu(p_bd_info->opaque.fcoe_opaque.lo),
2539 qedf->bdq_prod_idx, pktlen);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002540
Tomer Tayarda090912017-12-27 19:30:07 +02002541 bdq_idx = le32_to_cpu(p_bd_info->opaque.fcoe_opaque.lo);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002542 if (bdq_idx >= QEDF_BDQ_SIZE) {
2543 QEDF_ERR(&(qedf->dbg_ctx), "bdq_idx is out of range %d.\n",
2544 bdq_idx);
2545 goto increment_prod;
2546 }
2547
2548 bdq_addr = qedf->bdq[bdq_idx].buf_addr;
2549 if (!bdq_addr) {
2550 QEDF_ERR(&(qedf->dbg_ctx), "bdq_addr is NULL, dropping "
2551 "unsolicited packet.\n");
2552 goto increment_prod;
2553 }
2554
2555 if (qedf_dump_frames) {
2556 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
2557 "BDQ frame is at addr=%p.\n", bdq_addr);
2558 print_hex_dump(KERN_WARNING, "bdq ", DUMP_PREFIX_OFFSET, 16, 1,
2559 (void *)bdq_addr, pktlen, false);
2560 }
2561
2562 /* Allocate frame */
2563 payload_len = pktlen - sizeof(struct fc_frame_header);
2564 fp = fc_frame_alloc(qedf->lport, payload_len);
2565 if (!fp) {
2566 QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate fp.\n");
2567 goto increment_prod;
2568 }
2569
2570 /* Copy data from BDQ buffer into fc_frame struct */
2571 fh = (struct fc_frame_header *)fc_frame_header_get(fp);
2572 memcpy(fh, (void *)bdq_addr, pktlen);
2573
Saurav Kashyapaa5175a2019-08-23 02:52:38 -07002574 QEDF_WARN(&qedf->dbg_ctx,
2575 "Processing Unsolicated frame, src=%06x dest=%06x r_ctl=0x%x type=0x%x cmd=%02x\n",
2576 ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl,
2577 fh->fh_type, fc_frame_payload_op(fp));
2578
Dupuis, Chad61d86582017-02-15 06:28:23 -08002579 /* Initialize the frame so libfc sees it as a valid frame */
2580 crc = fcoe_fc_crc(fp);
2581 fc_frame_init(fp);
2582 fr_dev(fp) = qedf->lport;
2583 fr_sof(fp) = FC_SOF_I3;
2584 fr_eof(fp) = FC_EOF_T;
2585 fr_crc(fp) = cpu_to_le32(~crc);
2586
2587 /*
2588 * We need to return the frame back up to libfc in a non-atomic
2589 * context
2590 */
2591 io_work = mempool_alloc(qedf->io_mempool, GFP_ATOMIC);
2592 if (!io_work) {
2593 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate "
2594 "work for I/O completion.\n");
2595 fc_frame_free(fp);
2596 goto increment_prod;
2597 }
2598 memset(io_work, 0, sizeof(struct qedf_io_work));
2599
2600 INIT_WORK(&io_work->work, qedf_fp_io_handler);
2601
2602 /* Copy contents of CQE for deferred processing */
2603 memcpy(&io_work->cqe, cqe, sizeof(struct fcoe_cqe));
2604
2605 io_work->qedf = qedf;
2606 io_work->fp = fp;
2607
2608 queue_work_on(smp_processor_id(), qedf_io_wq, &io_work->work);
2609increment_prod:
2610 spin_lock_irqsave(&qedf->hba_lock, flags);
2611
2612 /* Increment producer to let f/w know we've handled the frame */
2613 qedf->bdq_prod_idx++;
2614
2615 /* Producer index wraps at uint16_t boundary */
2616 if (qedf->bdq_prod_idx == 0xffff)
2617 qedf->bdq_prod_idx = 0;
2618
2619 writew(qedf->bdq_prod_idx, qedf->bdq_primary_prod);
Lee Jones50efc512020-07-13 08:46:32 +01002620 readw(qedf->bdq_primary_prod);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002621 writew(qedf->bdq_prod_idx, qedf->bdq_secondary_prod);
Lee Jones50efc512020-07-13 08:46:32 +01002622 readw(qedf->bdq_secondary_prod);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002623
2624 spin_unlock_irqrestore(&qedf->hba_lock, flags);
2625}