xref: /openbmc/linux/drivers/scsi/qedf/qedf_io.c (revision 582a4727)
1 /*
2  *  QLogic FCoE Offload Driver
3  *  Copyright (c) 2016-2018 Cavium Inc.
4  *
5  *  This software is available under the terms of the GNU General Public License
6  *  (GPL) Version 2, available from the file COPYING in the main directory of
7  *  this source tree.
8  */
9 #include <linux/spinlock.h>
10 #include <linux/vmalloc.h>
11 #include "qedf.h"
12 #include <scsi/scsi_tcq.h>
13 
14 void qedf_cmd_timer_set(struct qedf_ctx *qedf, struct qedf_ioreq *io_req,
15 	unsigned int timer_msec)
16 {
17 	queue_delayed_work(qedf->timer_work_queue, &io_req->timeout_work,
18 	    msecs_to_jiffies(timer_msec));
19 }
20 
21 static void qedf_cmd_timeout(struct work_struct *work)
22 {
23 
24 	struct qedf_ioreq *io_req =
25 	    container_of(work, struct qedf_ioreq, timeout_work.work);
26 	struct qedf_ctx *qedf;
27 	struct qedf_rport *fcport;
28 	u8 op = 0;
29 
30 	if (io_req == NULL) {
31 		QEDF_INFO(NULL, QEDF_LOG_IO, "io_req is NULL.\n");
32 		return;
33 	}
34 
35 	fcport = io_req->fcport;
36 	if (io_req->fcport == NULL) {
37 		QEDF_INFO(NULL, QEDF_LOG_IO,  "fcport is NULL.\n");
38 		return;
39 	}
40 
41 	qedf = fcport->qedf;
42 
43 	switch (io_req->cmd_type) {
44 	case QEDF_ABTS:
45 		if (qedf == NULL) {
46 			QEDF_INFO(NULL, QEDF_LOG_IO,
47 				  "qedf is NULL for ABTS xid=0x%x.\n",
48 				  io_req->xid);
49 			return;
50 		}
51 
52 		QEDF_ERR((&qedf->dbg_ctx), "ABTS timeout, xid=0x%x.\n",
53 		    io_req->xid);
54 		/* Cleanup timed out ABTS */
55 		qedf_initiate_cleanup(io_req, true);
56 		complete(&io_req->abts_done);
57 
58 		/*
59 		 * Need to call kref_put for reference taken when initiate_abts
60 		 * was called since abts_compl won't be called now that we've
61 		 * cleaned up the task.
62 		 */
63 		kref_put(&io_req->refcount, qedf_release_cmd);
64 
65 		/* Clear in abort bit now that we're done with the command */
66 		clear_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
67 
68 		/*
69 		 * Now that the original I/O and the ABTS are complete see
70 		 * if we need to reconnect to the target.
71 		 */
72 		qedf_restart_rport(fcport);
73 		break;
74 	case QEDF_ELS:
75 		if (!qedf) {
76 			QEDF_INFO(NULL, QEDF_LOG_IO,
77 				  "qedf is NULL for ELS xid=0x%x.\n",
78 				  io_req->xid);
79 			return;
80 		}
81 		/* ELS request no longer outstanding since it timed out */
82 		clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
83 
84 		kref_get(&io_req->refcount);
85 		/*
86 		 * Don't attempt to clean an ELS timeout as any subseqeunt
87 		 * ABTS or cleanup requests just hang.  For now just free
88 		 * the resources of the original I/O and the RRQ
89 		 */
90 		QEDF_ERR(&(qedf->dbg_ctx), "ELS timeout, xid=0x%x.\n",
91 			  io_req->xid);
92 		io_req->event = QEDF_IOREQ_EV_ELS_TMO;
93 		/* Call callback function to complete command */
94 		if (io_req->cb_func && io_req->cb_arg) {
95 			op = io_req->cb_arg->op;
96 			io_req->cb_func(io_req->cb_arg);
97 			io_req->cb_arg = NULL;
98 		}
99 		qedf_initiate_cleanup(io_req, true);
100 		kref_put(&io_req->refcount, qedf_release_cmd);
101 		break;
102 	case QEDF_SEQ_CLEANUP:
103 		QEDF_ERR(&(qedf->dbg_ctx), "Sequence cleanup timeout, "
104 		    "xid=0x%x.\n", io_req->xid);
105 		qedf_initiate_cleanup(io_req, true);
106 		io_req->event = QEDF_IOREQ_EV_ELS_TMO;
107 		qedf_process_seq_cleanup_compl(qedf, NULL, io_req);
108 		break;
109 	default:
110 		break;
111 	}
112 }
113 
114 void qedf_cmd_mgr_free(struct qedf_cmd_mgr *cmgr)
115 {
116 	struct io_bdt *bdt_info;
117 	struct qedf_ctx *qedf = cmgr->qedf;
118 	size_t bd_tbl_sz;
119 	u16 min_xid = 0;
120 	u16 max_xid = (FCOE_PARAMS_NUM_TASKS - 1);
121 	int num_ios;
122 	int i;
123 	struct qedf_ioreq *io_req;
124 
125 	num_ios = max_xid - min_xid + 1;
126 
127 	/* Free fcoe_bdt_ctx structures */
128 	if (!cmgr->io_bdt_pool)
129 		goto free_cmd_pool;
130 
131 	bd_tbl_sz = QEDF_MAX_BDS_PER_CMD * sizeof(struct scsi_sge);
132 	for (i = 0; i < num_ios; i++) {
133 		bdt_info = cmgr->io_bdt_pool[i];
134 		if (bdt_info->bd_tbl) {
135 			dma_free_coherent(&qedf->pdev->dev, bd_tbl_sz,
136 			    bdt_info->bd_tbl, bdt_info->bd_tbl_dma);
137 			bdt_info->bd_tbl = NULL;
138 		}
139 	}
140 
141 	/* Destroy io_bdt pool */
142 	for (i = 0; i < num_ios; i++) {
143 		kfree(cmgr->io_bdt_pool[i]);
144 		cmgr->io_bdt_pool[i] = NULL;
145 	}
146 
147 	kfree(cmgr->io_bdt_pool);
148 	cmgr->io_bdt_pool = NULL;
149 
150 free_cmd_pool:
151 
152 	for (i = 0; i < num_ios; i++) {
153 		io_req = &cmgr->cmds[i];
154 		kfree(io_req->sgl_task_params);
155 		kfree(io_req->task_params);
156 		/* Make sure we free per command sense buffer */
157 		if (io_req->sense_buffer)
158 			dma_free_coherent(&qedf->pdev->dev,
159 			    QEDF_SCSI_SENSE_BUFFERSIZE, io_req->sense_buffer,
160 			    io_req->sense_buffer_dma);
161 		cancel_delayed_work_sync(&io_req->rrq_work);
162 	}
163 
164 	/* Free command manager itself */
165 	vfree(cmgr);
166 }
167 
168 static void qedf_handle_rrq(struct work_struct *work)
169 {
170 	struct qedf_ioreq *io_req =
171 	    container_of(work, struct qedf_ioreq, rrq_work.work);
172 
173 	qedf_send_rrq(io_req);
174 
175 }
176 
177 struct qedf_cmd_mgr *qedf_cmd_mgr_alloc(struct qedf_ctx *qedf)
178 {
179 	struct qedf_cmd_mgr *cmgr;
180 	struct io_bdt *bdt_info;
181 	struct qedf_ioreq *io_req;
182 	u16 xid;
183 	int i;
184 	int num_ios;
185 	u16 min_xid = 0;
186 	u16 max_xid = (FCOE_PARAMS_NUM_TASKS - 1);
187 
188 	/* Make sure num_queues is already set before calling this function */
189 	if (!qedf->num_queues) {
190 		QEDF_ERR(&(qedf->dbg_ctx), "num_queues is not set.\n");
191 		return NULL;
192 	}
193 
194 	if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
195 		QEDF_WARN(&(qedf->dbg_ctx), "Invalid min_xid 0x%x and "
196 			   "max_xid 0x%x.\n", min_xid, max_xid);
197 		return NULL;
198 	}
199 
200 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "min xid 0x%x, max xid "
201 		   "0x%x.\n", min_xid, max_xid);
202 
203 	num_ios = max_xid - min_xid + 1;
204 
205 	cmgr = vzalloc(sizeof(struct qedf_cmd_mgr));
206 	if (!cmgr) {
207 		QEDF_WARN(&(qedf->dbg_ctx), "Failed to alloc cmd mgr.\n");
208 		return NULL;
209 	}
210 
211 	cmgr->qedf = qedf;
212 	spin_lock_init(&cmgr->lock);
213 
214 	/*
215 	 * Initialize I/O request fields.
216 	 */
217 	xid = 0;
218 
219 	for (i = 0; i < num_ios; i++) {
220 		io_req = &cmgr->cmds[i];
221 		INIT_DELAYED_WORK(&io_req->timeout_work, qedf_cmd_timeout);
222 
223 		io_req->xid = xid++;
224 
225 		INIT_DELAYED_WORK(&io_req->rrq_work, qedf_handle_rrq);
226 
227 		/* Allocate DMA memory to hold sense buffer */
228 		io_req->sense_buffer = dma_alloc_coherent(&qedf->pdev->dev,
229 		    QEDF_SCSI_SENSE_BUFFERSIZE, &io_req->sense_buffer_dma,
230 		    GFP_KERNEL);
231 		if (!io_req->sense_buffer)
232 			goto mem_err;
233 
234 		/* Allocate task parameters to pass to f/w init funcions */
235 		io_req->task_params = kzalloc(sizeof(*io_req->task_params),
236 					      GFP_KERNEL);
237 		if (!io_req->task_params) {
238 			QEDF_ERR(&(qedf->dbg_ctx),
239 				 "Failed to allocate task_params for xid=0x%x\n",
240 				 i);
241 			goto mem_err;
242 		}
243 
244 		/*
245 		 * Allocate scatter/gather list info to pass to f/w init
246 		 * functions.
247 		 */
248 		io_req->sgl_task_params = kzalloc(
249 		    sizeof(struct scsi_sgl_task_params), GFP_KERNEL);
250 		if (!io_req->sgl_task_params) {
251 			QEDF_ERR(&(qedf->dbg_ctx),
252 				 "Failed to allocate sgl_task_params for xid=0x%x\n",
253 				 i);
254 			goto mem_err;
255 		}
256 	}
257 
258 	/* Allocate pool of io_bdts - one for each qedf_ioreq */
259 	cmgr->io_bdt_pool = kmalloc_array(num_ios, sizeof(struct io_bdt *),
260 	    GFP_KERNEL);
261 
262 	if (!cmgr->io_bdt_pool) {
263 		QEDF_WARN(&(qedf->dbg_ctx), "Failed to alloc io_bdt_pool.\n");
264 		goto mem_err;
265 	}
266 
267 	for (i = 0; i < num_ios; i++) {
268 		cmgr->io_bdt_pool[i] = kmalloc(sizeof(struct io_bdt),
269 		    GFP_KERNEL);
270 		if (!cmgr->io_bdt_pool[i]) {
271 			QEDF_WARN(&(qedf->dbg_ctx),
272 				  "Failed to alloc io_bdt_pool[%d].\n", i);
273 			goto mem_err;
274 		}
275 	}
276 
277 	for (i = 0; i < num_ios; i++) {
278 		bdt_info = cmgr->io_bdt_pool[i];
279 		bdt_info->bd_tbl = dma_alloc_coherent(&qedf->pdev->dev,
280 		    QEDF_MAX_BDS_PER_CMD * sizeof(struct scsi_sge),
281 		    &bdt_info->bd_tbl_dma, GFP_KERNEL);
282 		if (!bdt_info->bd_tbl) {
283 			QEDF_WARN(&(qedf->dbg_ctx),
284 				  "Failed to alloc bdt_tbl[%d].\n", i);
285 			goto mem_err;
286 		}
287 	}
288 	atomic_set(&cmgr->free_list_cnt, num_ios);
289 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
290 	    "cmgr->free_list_cnt=%d.\n",
291 	    atomic_read(&cmgr->free_list_cnt));
292 
293 	return cmgr;
294 
295 mem_err:
296 	qedf_cmd_mgr_free(cmgr);
297 	return NULL;
298 }
299 
300 struct qedf_ioreq *qedf_alloc_cmd(struct qedf_rport *fcport, u8 cmd_type)
301 {
302 	struct qedf_ctx *qedf = fcport->qedf;
303 	struct qedf_cmd_mgr *cmd_mgr = qedf->cmd_mgr;
304 	struct qedf_ioreq *io_req = NULL;
305 	struct io_bdt *bd_tbl;
306 	u16 xid;
307 	uint32_t free_sqes;
308 	int i;
309 	unsigned long flags;
310 
311 	free_sqes = atomic_read(&fcport->free_sqes);
312 
313 	if (!free_sqes) {
314 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
315 		    "Returning NULL, free_sqes=%d.\n ",
316 		    free_sqes);
317 		goto out_failed;
318 	}
319 
320 	/* Limit the number of outstanding R/W tasks */
321 	if ((atomic_read(&fcport->num_active_ios) >=
322 	    NUM_RW_TASKS_PER_CONNECTION)) {
323 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
324 		    "Returning NULL, num_active_ios=%d.\n",
325 		    atomic_read(&fcport->num_active_ios));
326 		goto out_failed;
327 	}
328 
329 	/* Limit global TIDs certain tasks */
330 	if (atomic_read(&cmd_mgr->free_list_cnt) <= GBL_RSVD_TASKS) {
331 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
332 		    "Returning NULL, free_list_cnt=%d.\n",
333 		    atomic_read(&cmd_mgr->free_list_cnt));
334 		goto out_failed;
335 	}
336 
337 	spin_lock_irqsave(&cmd_mgr->lock, flags);
338 	for (i = 0; i < FCOE_PARAMS_NUM_TASKS; i++) {
339 		io_req = &cmd_mgr->cmds[cmd_mgr->idx];
340 		cmd_mgr->idx++;
341 		if (cmd_mgr->idx == FCOE_PARAMS_NUM_TASKS)
342 			cmd_mgr->idx = 0;
343 
344 		/* Check to make sure command was previously freed */
345 		if (!io_req->alloc)
346 			break;
347 	}
348 
349 	if (i == FCOE_PARAMS_NUM_TASKS) {
350 		spin_unlock_irqrestore(&cmd_mgr->lock, flags);
351 		goto out_failed;
352 	}
353 
354 	/* Clear any flags now that we've reallocated the xid */
355 	io_req->flags = 0;
356 	io_req->alloc = 1;
357 	spin_unlock_irqrestore(&cmd_mgr->lock, flags);
358 
359 	atomic_inc(&fcport->num_active_ios);
360 	atomic_dec(&fcport->free_sqes);
361 	xid = io_req->xid;
362 	atomic_dec(&cmd_mgr->free_list_cnt);
363 
364 	io_req->cmd_mgr = cmd_mgr;
365 	io_req->fcport = fcport;
366 
367 	/* Clear any stale sc_cmd back pointer */
368 	io_req->sc_cmd = NULL;
369 	io_req->lun = -1;
370 
371 	/* Hold the io_req against deletion */
372 	kref_init(&io_req->refcount);
373 
374 	/* Bind io_bdt for this io_req */
375 	/* Have a static link between io_req and io_bdt_pool */
376 	bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
377 	if (bd_tbl == NULL) {
378 		QEDF_ERR(&(qedf->dbg_ctx), "bd_tbl is NULL, xid=%x.\n", xid);
379 		kref_put(&io_req->refcount, qedf_release_cmd);
380 		goto out_failed;
381 	}
382 	bd_tbl->io_req = io_req;
383 	io_req->cmd_type = cmd_type;
384 	io_req->tm_flags = 0;
385 
386 	/* Reset sequence offset data */
387 	io_req->rx_buf_off = 0;
388 	io_req->tx_buf_off = 0;
389 	io_req->rx_id = 0xffff; /* No OX_ID */
390 
391 	return io_req;
392 
393 out_failed:
394 	/* Record failure for stats and return NULL to caller */
395 	qedf->alloc_failures++;
396 	return NULL;
397 }
398 
399 static void qedf_free_mp_resc(struct qedf_ioreq *io_req)
400 {
401 	struct qedf_mp_req *mp_req = &(io_req->mp_req);
402 	struct qedf_ctx *qedf = io_req->fcport->qedf;
403 	uint64_t sz = sizeof(struct scsi_sge);
404 
405 	/* clear tm flags */
406 	if (mp_req->mp_req_bd) {
407 		dma_free_coherent(&qedf->pdev->dev, sz,
408 		    mp_req->mp_req_bd, mp_req->mp_req_bd_dma);
409 		mp_req->mp_req_bd = NULL;
410 	}
411 	if (mp_req->mp_resp_bd) {
412 		dma_free_coherent(&qedf->pdev->dev, sz,
413 		    mp_req->mp_resp_bd, mp_req->mp_resp_bd_dma);
414 		mp_req->mp_resp_bd = NULL;
415 	}
416 	if (mp_req->req_buf) {
417 		dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
418 		    mp_req->req_buf, mp_req->req_buf_dma);
419 		mp_req->req_buf = NULL;
420 	}
421 	if (mp_req->resp_buf) {
422 		dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
423 		    mp_req->resp_buf, mp_req->resp_buf_dma);
424 		mp_req->resp_buf = NULL;
425 	}
426 }
427 
428 void qedf_release_cmd(struct kref *ref)
429 {
430 	struct qedf_ioreq *io_req =
431 	    container_of(ref, struct qedf_ioreq, refcount);
432 	struct qedf_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
433 	struct qedf_rport *fcport = io_req->fcport;
434 	unsigned long flags;
435 
436 	if (io_req->cmd_type == QEDF_SCSI_CMD)
437 		WARN_ON(io_req->sc_cmd);
438 
439 	if (io_req->cmd_type == QEDF_ELS ||
440 	    io_req->cmd_type == QEDF_TASK_MGMT_CMD)
441 		qedf_free_mp_resc(io_req);
442 
443 	atomic_inc(&cmd_mgr->free_list_cnt);
444 	atomic_dec(&fcport->num_active_ios);
445 	if (atomic_read(&fcport->num_active_ios) < 0)
446 		QEDF_WARN(&(fcport->qedf->dbg_ctx), "active_ios < 0.\n");
447 
448 	/* Increment task retry identifier now that the request is released */
449 	io_req->task_retry_identifier++;
450 	io_req->fcport = NULL;
451 
452 	clear_bit(QEDF_CMD_DIRTY, &io_req->flags);
453 	io_req->cpu = 0;
454 	spin_lock_irqsave(&cmd_mgr->lock, flags);
455 	io_req->fcport = NULL;
456 	io_req->alloc = 0;
457 	spin_unlock_irqrestore(&cmd_mgr->lock, flags);
458 }
459 
460 static int qedf_map_sg(struct qedf_ioreq *io_req)
461 {
462 	struct scsi_cmnd *sc = io_req->sc_cmd;
463 	struct Scsi_Host *host = sc->device->host;
464 	struct fc_lport *lport = shost_priv(host);
465 	struct qedf_ctx *qedf = lport_priv(lport);
466 	struct scsi_sge *bd = io_req->bd_tbl->bd_tbl;
467 	struct scatterlist *sg;
468 	int byte_count = 0;
469 	int sg_count = 0;
470 	int bd_count = 0;
471 	u32 sg_len;
472 	u64 addr, end_addr;
473 	int i = 0;
474 
475 	sg_count = dma_map_sg(&qedf->pdev->dev, scsi_sglist(sc),
476 	    scsi_sg_count(sc), sc->sc_data_direction);
477 	sg = scsi_sglist(sc);
478 
479 	io_req->sge_type = QEDF_IOREQ_UNKNOWN_SGE;
480 
481 	if (sg_count <= 8 || io_req->io_req_flags == QEDF_READ)
482 		io_req->sge_type = QEDF_IOREQ_FAST_SGE;
483 
484 	scsi_for_each_sg(sc, sg, sg_count, i) {
485 		sg_len = (u32)sg_dma_len(sg);
486 		addr = (u64)sg_dma_address(sg);
487 		end_addr = (u64)(addr + sg_len);
488 
489 		/*
490 		 * Intermediate s/g element so check if start and end address
491 		 * is page aligned.  Only required for writes and only if the
492 		 * number of scatter/gather elements is 8 or more.
493 		 */
494 		if (io_req->sge_type == QEDF_IOREQ_UNKNOWN_SGE && (i) &&
495 		    (i != (sg_count - 1)) && sg_len < QEDF_PAGE_SIZE)
496 			io_req->sge_type = QEDF_IOREQ_SLOW_SGE;
497 
498 		bd[bd_count].sge_addr.lo = cpu_to_le32(U64_LO(addr));
499 		bd[bd_count].sge_addr.hi  = cpu_to_le32(U64_HI(addr));
500 		bd[bd_count].sge_len = cpu_to_le32(sg_len);
501 
502 		bd_count++;
503 		byte_count += sg_len;
504 	}
505 
506 	/* To catch a case where FAST and SLOW nothing is set, set FAST */
507 	if (io_req->sge_type == QEDF_IOREQ_UNKNOWN_SGE)
508 		io_req->sge_type = QEDF_IOREQ_FAST_SGE;
509 
510 	if (byte_count != scsi_bufflen(sc))
511 		QEDF_ERR(&(qedf->dbg_ctx), "byte_count = %d != "
512 			  "scsi_bufflen = %d, task_id = 0x%x.\n", byte_count,
513 			   scsi_bufflen(sc), io_req->xid);
514 
515 	return bd_count;
516 }
517 
518 static int qedf_build_bd_list_from_sg(struct qedf_ioreq *io_req)
519 {
520 	struct scsi_cmnd *sc = io_req->sc_cmd;
521 	struct scsi_sge *bd = io_req->bd_tbl->bd_tbl;
522 	int bd_count;
523 
524 	if (scsi_sg_count(sc)) {
525 		bd_count = qedf_map_sg(io_req);
526 		if (bd_count == 0)
527 			return -ENOMEM;
528 	} else {
529 		bd_count = 0;
530 		bd[0].sge_addr.lo = bd[0].sge_addr.hi = 0;
531 		bd[0].sge_len = 0;
532 	}
533 	io_req->bd_tbl->bd_valid = bd_count;
534 
535 	return 0;
536 }
537 
538 static void qedf_build_fcp_cmnd(struct qedf_ioreq *io_req,
539 				  struct fcp_cmnd *fcp_cmnd)
540 {
541 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
542 
543 	/* fcp_cmnd is 32 bytes */
544 	memset(fcp_cmnd, 0, FCP_CMND_LEN);
545 
546 	/* 8 bytes: SCSI LUN info */
547 	int_to_scsilun(sc_cmd->device->lun,
548 			(struct scsi_lun *)&fcp_cmnd->fc_lun);
549 
550 	/* 4 bytes: flag info */
551 	fcp_cmnd->fc_pri_ta = 0;
552 	fcp_cmnd->fc_tm_flags = io_req->tm_flags;
553 	fcp_cmnd->fc_flags = io_req->io_req_flags;
554 	fcp_cmnd->fc_cmdref = 0;
555 
556 	/* Populate data direction */
557 	if (io_req->cmd_type == QEDF_TASK_MGMT_CMD) {
558 		fcp_cmnd->fc_flags |= FCP_CFL_RDDATA;
559 	} else {
560 		if (sc_cmd->sc_data_direction == DMA_TO_DEVICE)
561 			fcp_cmnd->fc_flags |= FCP_CFL_WRDATA;
562 		else if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE)
563 			fcp_cmnd->fc_flags |= FCP_CFL_RDDATA;
564 	}
565 
566 	fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
567 
568 	/* 16 bytes: CDB information */
569 	if (io_req->cmd_type != QEDF_TASK_MGMT_CMD)
570 		memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
571 
572 	/* 4 bytes: FCP data length */
573 	fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
574 }
575 
576 static void  qedf_init_task(struct qedf_rport *fcport, struct fc_lport *lport,
577 	struct qedf_ioreq *io_req, struct e4_fcoe_task_context *task_ctx,
578 	struct fcoe_wqe *sqe)
579 {
580 	enum fcoe_task_type task_type;
581 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
582 	struct io_bdt *bd_tbl = io_req->bd_tbl;
583 	u8 fcp_cmnd[32];
584 	u32 tmp_fcp_cmnd[8];
585 	int bd_count = 0;
586 	struct qedf_ctx *qedf = fcport->qedf;
587 	uint16_t cq_idx = smp_processor_id() % qedf->num_queues;
588 	struct regpair sense_data_buffer_phys_addr;
589 	u32 tx_io_size = 0;
590 	u32 rx_io_size = 0;
591 	int i, cnt;
592 
593 	/* Note init_initiator_rw_fcoe_task memsets the task context */
594 	io_req->task = task_ctx;
595 	memset(task_ctx, 0, sizeof(struct e4_fcoe_task_context));
596 	memset(io_req->task_params, 0, sizeof(struct fcoe_task_params));
597 	memset(io_req->sgl_task_params, 0, sizeof(struct scsi_sgl_task_params));
598 
599 	/* Set task type bassed on DMA directio of command */
600 	if (io_req->cmd_type == QEDF_TASK_MGMT_CMD) {
601 		task_type = FCOE_TASK_TYPE_READ_INITIATOR;
602 	} else {
603 		if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
604 			task_type = FCOE_TASK_TYPE_WRITE_INITIATOR;
605 			tx_io_size = io_req->data_xfer_len;
606 		} else {
607 			task_type = FCOE_TASK_TYPE_READ_INITIATOR;
608 			rx_io_size = io_req->data_xfer_len;
609 		}
610 	}
611 
612 	/* Setup the fields for fcoe_task_params */
613 	io_req->task_params->context = task_ctx;
614 	io_req->task_params->sqe = sqe;
615 	io_req->task_params->task_type = task_type;
616 	io_req->task_params->tx_io_size = tx_io_size;
617 	io_req->task_params->rx_io_size = rx_io_size;
618 	io_req->task_params->conn_cid = fcport->fw_cid;
619 	io_req->task_params->itid = io_req->xid;
620 	io_req->task_params->cq_rss_number = cq_idx;
621 	io_req->task_params->is_tape_device = fcport->dev_type;
622 
623 	/* Fill in information for scatter/gather list */
624 	if (io_req->cmd_type != QEDF_TASK_MGMT_CMD) {
625 		bd_count = bd_tbl->bd_valid;
626 		io_req->sgl_task_params->sgl = bd_tbl->bd_tbl;
627 		io_req->sgl_task_params->sgl_phys_addr.lo =
628 			U64_LO(bd_tbl->bd_tbl_dma);
629 		io_req->sgl_task_params->sgl_phys_addr.hi =
630 			U64_HI(bd_tbl->bd_tbl_dma);
631 		io_req->sgl_task_params->num_sges = bd_count;
632 		io_req->sgl_task_params->total_buffer_size =
633 		    scsi_bufflen(io_req->sc_cmd);
634 		if (io_req->sge_type == QEDF_IOREQ_SLOW_SGE)
635 			io_req->sgl_task_params->small_mid_sge = 1;
636 		else
637 			io_req->sgl_task_params->small_mid_sge = 0;
638 	}
639 
640 	/* Fill in physical address of sense buffer */
641 	sense_data_buffer_phys_addr.lo = U64_LO(io_req->sense_buffer_dma);
642 	sense_data_buffer_phys_addr.hi = U64_HI(io_req->sense_buffer_dma);
643 
644 	/* fill FCP_CMND IU */
645 	qedf_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tmp_fcp_cmnd);
646 
647 	/* Swap fcp_cmnd since FC is big endian */
648 	cnt = sizeof(struct fcp_cmnd) / sizeof(u32);
649 	for (i = 0; i < cnt; i++) {
650 		tmp_fcp_cmnd[i] = cpu_to_be32(tmp_fcp_cmnd[i]);
651 	}
652 	memcpy(fcp_cmnd, tmp_fcp_cmnd, sizeof(struct fcp_cmnd));
653 
654 	init_initiator_rw_fcoe_task(io_req->task_params,
655 				    io_req->sgl_task_params,
656 				    sense_data_buffer_phys_addr,
657 				    io_req->task_retry_identifier, fcp_cmnd);
658 
659 	/* Increment SGL type counters */
660 	if (io_req->sge_type == QEDF_IOREQ_SLOW_SGE)
661 		qedf->slow_sge_ios++;
662 	else
663 		qedf->fast_sge_ios++;
664 }
665 
666 void qedf_init_mp_task(struct qedf_ioreq *io_req,
667 	struct e4_fcoe_task_context *task_ctx, struct fcoe_wqe *sqe)
668 {
669 	struct qedf_mp_req *mp_req = &(io_req->mp_req);
670 	struct qedf_rport *fcport = io_req->fcport;
671 	struct qedf_ctx *qedf = io_req->fcport->qedf;
672 	struct fc_frame_header *fc_hdr;
673 	struct fcoe_tx_mid_path_params task_fc_hdr;
674 	struct scsi_sgl_task_params tx_sgl_task_params;
675 	struct scsi_sgl_task_params rx_sgl_task_params;
676 
677 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
678 		  "Initializing MP task for cmd_type=%d\n",
679 		  io_req->cmd_type);
680 
681 	qedf->control_requests++;
682 
683 	memset(&tx_sgl_task_params, 0, sizeof(struct scsi_sgl_task_params));
684 	memset(&rx_sgl_task_params, 0, sizeof(struct scsi_sgl_task_params));
685 	memset(task_ctx, 0, sizeof(struct e4_fcoe_task_context));
686 	memset(&task_fc_hdr, 0, sizeof(struct fcoe_tx_mid_path_params));
687 
688 	/* Setup the task from io_req for easy reference */
689 	io_req->task = task_ctx;
690 
691 	/* Setup the fields for fcoe_task_params */
692 	io_req->task_params->context = task_ctx;
693 	io_req->task_params->sqe = sqe;
694 	io_req->task_params->task_type = FCOE_TASK_TYPE_MIDPATH;
695 	io_req->task_params->tx_io_size = io_req->data_xfer_len;
696 	/* rx_io_size tells the f/w how large a response buffer we have */
697 	io_req->task_params->rx_io_size = PAGE_SIZE;
698 	io_req->task_params->conn_cid = fcport->fw_cid;
699 	io_req->task_params->itid = io_req->xid;
700 	/* Return middle path commands on CQ 0 */
701 	io_req->task_params->cq_rss_number = 0;
702 	io_req->task_params->is_tape_device = fcport->dev_type;
703 
704 	fc_hdr = &(mp_req->req_fc_hdr);
705 	/* Set OX_ID and RX_ID based on driver task id */
706 	fc_hdr->fh_ox_id = io_req->xid;
707 	fc_hdr->fh_rx_id = htons(0xffff);
708 
709 	/* Set up FC header information */
710 	task_fc_hdr.parameter = fc_hdr->fh_parm_offset;
711 	task_fc_hdr.r_ctl = fc_hdr->fh_r_ctl;
712 	task_fc_hdr.type = fc_hdr->fh_type;
713 	task_fc_hdr.cs_ctl = fc_hdr->fh_cs_ctl;
714 	task_fc_hdr.df_ctl = fc_hdr->fh_df_ctl;
715 	task_fc_hdr.rx_id = fc_hdr->fh_rx_id;
716 	task_fc_hdr.ox_id = fc_hdr->fh_ox_id;
717 
718 	/* Set up s/g list parameters for request buffer */
719 	tx_sgl_task_params.sgl = mp_req->mp_req_bd;
720 	tx_sgl_task_params.sgl_phys_addr.lo = U64_LO(mp_req->mp_req_bd_dma);
721 	tx_sgl_task_params.sgl_phys_addr.hi = U64_HI(mp_req->mp_req_bd_dma);
722 	tx_sgl_task_params.num_sges = 1;
723 	/* Set PAGE_SIZE for now since sg element is that size ??? */
724 	tx_sgl_task_params.total_buffer_size = io_req->data_xfer_len;
725 	tx_sgl_task_params.small_mid_sge = 0;
726 
727 	/* Set up s/g list parameters for request buffer */
728 	rx_sgl_task_params.sgl = mp_req->mp_resp_bd;
729 	rx_sgl_task_params.sgl_phys_addr.lo = U64_LO(mp_req->mp_resp_bd_dma);
730 	rx_sgl_task_params.sgl_phys_addr.hi = U64_HI(mp_req->mp_resp_bd_dma);
731 	rx_sgl_task_params.num_sges = 1;
732 	/* Set PAGE_SIZE for now since sg element is that size ??? */
733 	rx_sgl_task_params.total_buffer_size = PAGE_SIZE;
734 	rx_sgl_task_params.small_mid_sge = 0;
735 
736 
737 	/*
738 	 * Last arg is 0 as previous code did not set that we wanted the
739 	 * fc header information.
740 	 */
741 	init_initiator_midpath_unsolicited_fcoe_task(io_req->task_params,
742 						     &task_fc_hdr,
743 						     &tx_sgl_task_params,
744 						     &rx_sgl_task_params, 0);
745 }
746 
747 /* Presumed that fcport->rport_lock is held */
748 u16 qedf_get_sqe_idx(struct qedf_rport *fcport)
749 {
750 	uint16_t total_sqe = (fcport->sq_mem_size)/(sizeof(struct fcoe_wqe));
751 	u16 rval;
752 
753 	rval = fcport->sq_prod_idx;
754 
755 	/* Adjust ring index */
756 	fcport->sq_prod_idx++;
757 	fcport->fw_sq_prod_idx++;
758 	if (fcport->sq_prod_idx == total_sqe)
759 		fcport->sq_prod_idx = 0;
760 
761 	return rval;
762 }
763 
764 void qedf_ring_doorbell(struct qedf_rport *fcport)
765 {
766 	struct fcoe_db_data dbell = { 0 };
767 
768 	dbell.agg_flags = 0;
769 
770 	dbell.params |= DB_DEST_XCM << FCOE_DB_DATA_DEST_SHIFT;
771 	dbell.params |= DB_AGG_CMD_SET << FCOE_DB_DATA_AGG_CMD_SHIFT;
772 	dbell.params |= DQ_XCM_FCOE_SQ_PROD_CMD <<
773 	    FCOE_DB_DATA_AGG_VAL_SEL_SHIFT;
774 
775 	dbell.sq_prod = fcport->fw_sq_prod_idx;
776 	/* wmb makes sure that the BDs data is updated before updating the
777 	 * producer, otherwise FW may read old data from the BDs.
778 	 */
779 	wmb();
780 	barrier();
781 	writel(*(u32 *)&dbell, fcport->p_doorbell);
782 	/*
783 	 * Fence required to flush the write combined buffer, since another
784 	 * CPU may write to the same doorbell address and data may be lost
785 	 * due to relaxed order nature of write combined bar.
786 	 */
787 	wmb();
788 }
789 
790 static void qedf_trace_io(struct qedf_rport *fcport, struct qedf_ioreq *io_req,
791 			  int8_t direction)
792 {
793 	struct qedf_ctx *qedf = fcport->qedf;
794 	struct qedf_io_log *io_log;
795 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
796 	unsigned long flags;
797 	uint8_t op;
798 
799 	spin_lock_irqsave(&qedf->io_trace_lock, flags);
800 
801 	io_log = &qedf->io_trace_buf[qedf->io_trace_idx];
802 	io_log->direction = direction;
803 	io_log->task_id = io_req->xid;
804 	io_log->port_id = fcport->rdata->ids.port_id;
805 	io_log->lun = sc_cmd->device->lun;
806 	io_log->op = op = sc_cmd->cmnd[0];
807 	io_log->lba[0] = sc_cmd->cmnd[2];
808 	io_log->lba[1] = sc_cmd->cmnd[3];
809 	io_log->lba[2] = sc_cmd->cmnd[4];
810 	io_log->lba[3] = sc_cmd->cmnd[5];
811 	io_log->bufflen = scsi_bufflen(sc_cmd);
812 	io_log->sg_count = scsi_sg_count(sc_cmd);
813 	io_log->result = sc_cmd->result;
814 	io_log->jiffies = jiffies;
815 	io_log->refcount = kref_read(&io_req->refcount);
816 
817 	if (direction == QEDF_IO_TRACE_REQ) {
818 		/* For requests we only care abot the submission CPU */
819 		io_log->req_cpu = io_req->cpu;
820 		io_log->int_cpu = 0;
821 		io_log->rsp_cpu = 0;
822 	} else if (direction == QEDF_IO_TRACE_RSP) {
823 		io_log->req_cpu = io_req->cpu;
824 		io_log->int_cpu = io_req->int_cpu;
825 		io_log->rsp_cpu = smp_processor_id();
826 	}
827 
828 	io_log->sge_type = io_req->sge_type;
829 
830 	qedf->io_trace_idx++;
831 	if (qedf->io_trace_idx == QEDF_IO_TRACE_SIZE)
832 		qedf->io_trace_idx = 0;
833 
834 	spin_unlock_irqrestore(&qedf->io_trace_lock, flags);
835 }
836 
837 int qedf_post_io_req(struct qedf_rport *fcport, struct qedf_ioreq *io_req)
838 {
839 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
840 	struct Scsi_Host *host = sc_cmd->device->host;
841 	struct fc_lport *lport = shost_priv(host);
842 	struct qedf_ctx *qedf = lport_priv(lport);
843 	struct e4_fcoe_task_context *task_ctx;
844 	u16 xid;
845 	enum fcoe_task_type req_type = 0;
846 	struct fcoe_wqe *sqe;
847 	u16 sqe_idx;
848 
849 	/* Initialize rest of io_req fileds */
850 	io_req->data_xfer_len = scsi_bufflen(sc_cmd);
851 	sc_cmd->SCp.ptr = (char *)io_req;
852 	io_req->sge_type = QEDF_IOREQ_FAST_SGE; /* Assume fast SGL by default */
853 
854 	/* Record which cpu this request is associated with */
855 	io_req->cpu = smp_processor_id();
856 
857 	if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
858 		req_type = FCOE_TASK_TYPE_READ_INITIATOR;
859 		io_req->io_req_flags = QEDF_READ;
860 		qedf->input_requests++;
861 	} else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
862 		req_type = FCOE_TASK_TYPE_WRITE_INITIATOR;
863 		io_req->io_req_flags = QEDF_WRITE;
864 		qedf->output_requests++;
865 	} else {
866 		io_req->io_req_flags = 0;
867 		qedf->control_requests++;
868 	}
869 
870 	xid = io_req->xid;
871 
872 	/* Build buffer descriptor list for firmware from sg list */
873 	if (qedf_build_bd_list_from_sg(io_req)) {
874 		QEDF_ERR(&(qedf->dbg_ctx), "BD list creation failed.\n");
875 		/* Release cmd will release io_req, but sc_cmd is assigned */
876 		io_req->sc_cmd = NULL;
877 		kref_put(&io_req->refcount, qedf_release_cmd);
878 		return -EAGAIN;
879 	}
880 
881 	if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags) ||
882 	    test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
883 		QEDF_ERR(&(qedf->dbg_ctx), "Session not offloaded yet.\n");
884 		/* Release cmd will release io_req, but sc_cmd is assigned */
885 		io_req->sc_cmd = NULL;
886 		kref_put(&io_req->refcount, qedf_release_cmd);
887 		return -EINVAL;
888 	}
889 
890 	/* Record LUN number for later use if we neeed them */
891 	io_req->lun = (int)sc_cmd->device->lun;
892 
893 	/* Obtain free SQE */
894 	sqe_idx = qedf_get_sqe_idx(fcport);
895 	sqe = &fcport->sq[sqe_idx];
896 	memset(sqe, 0, sizeof(struct fcoe_wqe));
897 
898 	/* Get the task context */
899 	task_ctx = qedf_get_task_mem(&qedf->tasks, xid);
900 	if (!task_ctx) {
901 		QEDF_WARN(&(qedf->dbg_ctx), "task_ctx is NULL, xid=%d.\n",
902 			   xid);
903 		/* Release cmd will release io_req, but sc_cmd is assigned */
904 		io_req->sc_cmd = NULL;
905 		kref_put(&io_req->refcount, qedf_release_cmd);
906 		return -EINVAL;
907 	}
908 
909 	qedf_init_task(fcport, lport, io_req, task_ctx, sqe);
910 
911 	/* Ring doorbell */
912 	qedf_ring_doorbell(fcport);
913 
914 	/* Set that command is with the firmware now */
915 	set_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
916 
917 	if (qedf_io_tracing && io_req->sc_cmd)
918 		qedf_trace_io(fcport, io_req, QEDF_IO_TRACE_REQ);
919 
920 	return false;
921 }
922 
923 int
924 qedf_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc_cmd)
925 {
926 	struct fc_lport *lport = shost_priv(host);
927 	struct qedf_ctx *qedf = lport_priv(lport);
928 	struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
929 	struct fc_rport_libfc_priv *rp = rport->dd_data;
930 	struct qedf_rport *fcport;
931 	struct qedf_ioreq *io_req;
932 	int rc = 0;
933 	int rval;
934 	unsigned long flags = 0;
935 	int num_sgs = 0;
936 
937 	num_sgs = scsi_sg_count(sc_cmd);
938 	if (scsi_sg_count(sc_cmd) > QEDF_MAX_BDS_PER_CMD) {
939 		QEDF_ERR(&qedf->dbg_ctx,
940 			 "Number of SG elements %d exceeds what hardware limitation of %d.\n",
941 			 num_sgs, QEDF_MAX_BDS_PER_CMD);
942 		sc_cmd->result = DID_ERROR;
943 		sc_cmd->scsi_done(sc_cmd);
944 		return 0;
945 	}
946 
947 	if (test_bit(QEDF_UNLOADING, &qedf->flags) ||
948 	    test_bit(QEDF_DBG_STOP_IO, &qedf->flags)) {
949 		sc_cmd->result = DID_NO_CONNECT << 16;
950 		sc_cmd->scsi_done(sc_cmd);
951 		return 0;
952 	}
953 
954 	if (!qedf->pdev->msix_enabled) {
955 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
956 		    "Completing sc_cmd=%p DID_NO_CONNECT as MSI-X is not enabled.\n",
957 		    sc_cmd);
958 		sc_cmd->result = DID_NO_CONNECT << 16;
959 		sc_cmd->scsi_done(sc_cmd);
960 		return 0;
961 	}
962 
963 	rval = fc_remote_port_chkready(rport);
964 	if (rval) {
965 		sc_cmd->result = rval;
966 		sc_cmd->scsi_done(sc_cmd);
967 		return 0;
968 	}
969 
970 	/* Retry command if we are doing a qed drain operation */
971 	if (test_bit(QEDF_DRAIN_ACTIVE, &qedf->flags)) {
972 		rc = SCSI_MLQUEUE_HOST_BUSY;
973 		goto exit_qcmd;
974 	}
975 
976 	if (lport->state != LPORT_ST_READY ||
977 	    atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
978 		rc = SCSI_MLQUEUE_HOST_BUSY;
979 		goto exit_qcmd;
980 	}
981 
982 	/* rport and tgt are allocated together, so tgt should be non-NULL */
983 	fcport = (struct qedf_rport *)&rp[1];
984 
985 	if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags) ||
986 	    test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
987 		/*
988 		 * Session is not offloaded yet. Let SCSI-ml retry
989 		 * the command.
990 		 */
991 		rc = SCSI_MLQUEUE_TARGET_BUSY;
992 		goto exit_qcmd;
993 	}
994 
995 	atomic_inc(&fcport->ios_to_queue);
996 
997 	if (fcport->retry_delay_timestamp) {
998 		if (time_after(jiffies, fcport->retry_delay_timestamp)) {
999 			fcport->retry_delay_timestamp = 0;
1000 		} else {
1001 			/* If retry_delay timer is active, flow off the ML */
1002 			rc = SCSI_MLQUEUE_TARGET_BUSY;
1003 			atomic_dec(&fcport->ios_to_queue);
1004 			goto exit_qcmd;
1005 		}
1006 	}
1007 
1008 	io_req = qedf_alloc_cmd(fcport, QEDF_SCSI_CMD);
1009 	if (!io_req) {
1010 		rc = SCSI_MLQUEUE_HOST_BUSY;
1011 		atomic_dec(&fcport->ios_to_queue);
1012 		goto exit_qcmd;
1013 	}
1014 
1015 	io_req->sc_cmd = sc_cmd;
1016 
1017 	/* Take fcport->rport_lock for posting to fcport send queue */
1018 	spin_lock_irqsave(&fcport->rport_lock, flags);
1019 	if (qedf_post_io_req(fcport, io_req)) {
1020 		QEDF_WARN(&(qedf->dbg_ctx), "Unable to post io_req\n");
1021 		/* Return SQE to pool */
1022 		atomic_inc(&fcport->free_sqes);
1023 		rc = SCSI_MLQUEUE_HOST_BUSY;
1024 	}
1025 	spin_unlock_irqrestore(&fcport->rport_lock, flags);
1026 	atomic_dec(&fcport->ios_to_queue);
1027 
1028 exit_qcmd:
1029 	return rc;
1030 }
1031 
1032 static void qedf_parse_fcp_rsp(struct qedf_ioreq *io_req,
1033 				 struct fcoe_cqe_rsp_info *fcp_rsp)
1034 {
1035 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1036 	struct qedf_ctx *qedf = io_req->fcport->qedf;
1037 	u8 rsp_flags = fcp_rsp->rsp_flags.flags;
1038 	int fcp_sns_len = 0;
1039 	int fcp_rsp_len = 0;
1040 	uint8_t *rsp_info, *sense_data;
1041 
1042 	io_req->fcp_status = FC_GOOD;
1043 	io_req->fcp_resid = 0;
1044 	if (rsp_flags & (FCOE_FCP_RSP_FLAGS_FCP_RESID_OVER |
1045 	    FCOE_FCP_RSP_FLAGS_FCP_RESID_UNDER))
1046 		io_req->fcp_resid = fcp_rsp->fcp_resid;
1047 
1048 	io_req->scsi_comp_flags = rsp_flags;
1049 	CMD_SCSI_STATUS(sc_cmd) = io_req->cdb_status =
1050 	    fcp_rsp->scsi_status_code;
1051 
1052 	if (rsp_flags &
1053 	    FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID)
1054 		fcp_rsp_len = fcp_rsp->fcp_rsp_len;
1055 
1056 	if (rsp_flags &
1057 	    FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID)
1058 		fcp_sns_len = fcp_rsp->fcp_sns_len;
1059 
1060 	io_req->fcp_rsp_len = fcp_rsp_len;
1061 	io_req->fcp_sns_len = fcp_sns_len;
1062 	rsp_info = sense_data = io_req->sense_buffer;
1063 
1064 	/* fetch fcp_rsp_code */
1065 	if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
1066 		/* Only for task management function */
1067 		io_req->fcp_rsp_code = rsp_info[3];
1068 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1069 		    "fcp_rsp_code = %d\n", io_req->fcp_rsp_code);
1070 		/* Adjust sense-data location. */
1071 		sense_data += fcp_rsp_len;
1072 	}
1073 
1074 	if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
1075 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1076 		    "Truncating sense buffer\n");
1077 		fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
1078 	}
1079 
1080 	/* The sense buffer can be NULL for TMF commands */
1081 	if (sc_cmd->sense_buffer) {
1082 		memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1083 		if (fcp_sns_len)
1084 			memcpy(sc_cmd->sense_buffer, sense_data,
1085 			    fcp_sns_len);
1086 	}
1087 }
1088 
1089 static void qedf_unmap_sg_list(struct qedf_ctx *qedf, struct qedf_ioreq *io_req)
1090 {
1091 	struct scsi_cmnd *sc = io_req->sc_cmd;
1092 
1093 	if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) {
1094 		dma_unmap_sg(&qedf->pdev->dev, scsi_sglist(sc),
1095 		    scsi_sg_count(sc), sc->sc_data_direction);
1096 		io_req->bd_tbl->bd_valid = 0;
1097 	}
1098 }
1099 
1100 void qedf_scsi_completion(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
1101 	struct qedf_ioreq *io_req)
1102 {
1103 	u16 xid;
1104 	struct e4_fcoe_task_context *task_ctx;
1105 	struct scsi_cmnd *sc_cmd;
1106 	struct fcoe_cqe_rsp_info *fcp_rsp;
1107 	struct qedf_rport *fcport;
1108 	int refcount;
1109 	u16 scope, qualifier = 0;
1110 	u8 fw_residual_flag = 0;
1111 
1112 	if (!io_req)
1113 		return;
1114 	if (!cqe)
1115 		return;
1116 
1117 	if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
1118 	    test_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags) ||
1119 	    test_bit(QEDF_CMD_IN_ABORT, &io_req->flags)) {
1120 		QEDF_ERR(&qedf->dbg_ctx,
1121 			 "io_req xid=0x%x already in cleanup or abort processing or already completed.\n",
1122 			 io_req->xid);
1123 		return;
1124 	}
1125 
1126 	xid = io_req->xid;
1127 	task_ctx = qedf_get_task_mem(&qedf->tasks, xid);
1128 	sc_cmd = io_req->sc_cmd;
1129 	fcp_rsp = &cqe->cqe_info.rsp_info;
1130 
1131 	if (!sc_cmd) {
1132 		QEDF_WARN(&(qedf->dbg_ctx), "sc_cmd is NULL!\n");
1133 		return;
1134 	}
1135 
1136 	if (!sc_cmd->SCp.ptr) {
1137 		QEDF_WARN(&(qedf->dbg_ctx), "SCp.ptr is NULL, returned in "
1138 		    "another context.\n");
1139 		return;
1140 	}
1141 
1142 	if (!sc_cmd->device) {
1143 		QEDF_ERR(&qedf->dbg_ctx,
1144 			 "Device for sc_cmd %p is NULL.\n", sc_cmd);
1145 		return;
1146 	}
1147 
1148 	if (!sc_cmd->request) {
1149 		QEDF_WARN(&(qedf->dbg_ctx), "sc_cmd->request is NULL, "
1150 		    "sc_cmd=%p.\n", sc_cmd);
1151 		return;
1152 	}
1153 
1154 	if (!sc_cmd->request->q) {
1155 		QEDF_WARN(&(qedf->dbg_ctx), "request->q is NULL so request "
1156 		   "is not valid, sc_cmd=%p.\n", sc_cmd);
1157 		return;
1158 	}
1159 
1160 	fcport = io_req->fcport;
1161 
1162 	/*
1163 	 * When flush is active, let the cmds be completed from the cleanup
1164 	 * context
1165 	 */
1166 	if (test_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags) ||
1167 	    (test_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags) &&
1168 	     sc_cmd->device->lun == (u64)fcport->lun_reset_lun)) {
1169 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1170 			  "Dropping good completion xid=0x%x as fcport is flushing",
1171 			  io_req->xid);
1172 		return;
1173 	}
1174 
1175 	qedf_parse_fcp_rsp(io_req, fcp_rsp);
1176 
1177 	qedf_unmap_sg_list(qedf, io_req);
1178 
1179 	/* Check for FCP transport error */
1180 	if (io_req->fcp_rsp_len > 3 && io_req->fcp_rsp_code) {
1181 		QEDF_ERR(&(qedf->dbg_ctx),
1182 		    "FCP I/O protocol failure xid=0x%x fcp_rsp_len=%d "
1183 		    "fcp_rsp_code=%d.\n", io_req->xid, io_req->fcp_rsp_len,
1184 		    io_req->fcp_rsp_code);
1185 		sc_cmd->result = DID_BUS_BUSY << 16;
1186 		goto out;
1187 	}
1188 
1189 	fw_residual_flag = GET_FIELD(cqe->cqe_info.rsp_info.fw_error_flags,
1190 	    FCOE_CQE_RSP_INFO_FW_UNDERRUN);
1191 	if (fw_residual_flag) {
1192 		QEDF_ERR(&(qedf->dbg_ctx),
1193 		    "Firmware detected underrun: xid=0x%x fcp_rsp.flags=0x%02x "
1194 		    "fcp_resid=%d fw_residual=0x%x.\n", io_req->xid,
1195 		    fcp_rsp->rsp_flags.flags, io_req->fcp_resid,
1196 		    cqe->cqe_info.rsp_info.fw_residual);
1197 
1198 		if (io_req->cdb_status == 0)
1199 			sc_cmd->result = (DID_ERROR << 16) | io_req->cdb_status;
1200 		else
1201 			sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1202 
1203 		/*
1204 		 * Set resid to the whole buffer length so we won't try to resue
1205 		 * any previously data.
1206 		 */
1207 		scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
1208 		goto out;
1209 	}
1210 
1211 	switch (io_req->fcp_status) {
1212 	case FC_GOOD:
1213 		if (io_req->cdb_status == 0) {
1214 			/* Good I/O completion */
1215 			sc_cmd->result = DID_OK << 16;
1216 		} else {
1217 			refcount = kref_read(&io_req->refcount);
1218 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1219 			    "%d:0:%d:%lld xid=0x%0x op=0x%02x "
1220 			    "lba=%02x%02x%02x%02x cdb_status=%d "
1221 			    "fcp_resid=0x%x refcount=%d.\n",
1222 			    qedf->lport->host->host_no, sc_cmd->device->id,
1223 			    sc_cmd->device->lun, io_req->xid,
1224 			    sc_cmd->cmnd[0], sc_cmd->cmnd[2], sc_cmd->cmnd[3],
1225 			    sc_cmd->cmnd[4], sc_cmd->cmnd[5],
1226 			    io_req->cdb_status, io_req->fcp_resid,
1227 			    refcount);
1228 			sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1229 
1230 			if (io_req->cdb_status == SAM_STAT_TASK_SET_FULL ||
1231 			    io_req->cdb_status == SAM_STAT_BUSY) {
1232 				/*
1233 				 * Check whether we need to set retry_delay at
1234 				 * all based on retry_delay module parameter
1235 				 * and the status qualifier.
1236 				 */
1237 
1238 				/* Upper 2 bits */
1239 				scope = fcp_rsp->retry_delay_timer & 0xC000;
1240 				/* Lower 14 bits */
1241 				qualifier = fcp_rsp->retry_delay_timer & 0x3FFF;
1242 
1243 				if (qedf_retry_delay &&
1244 				    scope > 0 && qualifier > 0 &&
1245 				    qualifier <= 0x3FEF) {
1246 					/* Check we don't go over the max */
1247 					if (qualifier > QEDF_RETRY_DELAY_MAX)
1248 						qualifier =
1249 						    QEDF_RETRY_DELAY_MAX;
1250 					fcport->retry_delay_timestamp =
1251 					    jiffies + (qualifier * HZ / 10);
1252 				}
1253 				/* Record stats */
1254 				if (io_req->cdb_status ==
1255 				    SAM_STAT_TASK_SET_FULL)
1256 					qedf->task_set_fulls++;
1257 				else
1258 					qedf->busy++;
1259 			}
1260 		}
1261 		if (io_req->fcp_resid)
1262 			scsi_set_resid(sc_cmd, io_req->fcp_resid);
1263 		break;
1264 	default:
1265 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO, "fcp_status=%d.\n",
1266 			   io_req->fcp_status);
1267 		break;
1268 	}
1269 
1270 out:
1271 	if (qedf_io_tracing)
1272 		qedf_trace_io(fcport, io_req, QEDF_IO_TRACE_RSP);
1273 
1274 	/*
1275 	 * We wait till the end of the function to clear the
1276 	 * outstanding bit in case we need to send an abort
1277 	 */
1278 	clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
1279 
1280 	io_req->sc_cmd = NULL;
1281 	sc_cmd->SCp.ptr =  NULL;
1282 	sc_cmd->scsi_done(sc_cmd);
1283 	kref_put(&io_req->refcount, qedf_release_cmd);
1284 }
1285 
1286 /* Return a SCSI command in some other context besides a normal completion */
1287 void qedf_scsi_done(struct qedf_ctx *qedf, struct qedf_ioreq *io_req,
1288 	int result)
1289 {
1290 	u16 xid;
1291 	struct scsi_cmnd *sc_cmd;
1292 	int refcount;
1293 
1294 	if (!io_req)
1295 		return;
1296 
1297 	if (test_and_set_bit(QEDF_CMD_ERR_SCSI_DONE, &io_req->flags)) {
1298 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1299 			  "io_req:%p scsi_done handling already done\n",
1300 			  io_req);
1301 		return;
1302 	}
1303 
1304 	/*
1305 	 * We will be done with this command after this call so clear the
1306 	 * outstanding bit.
1307 	 */
1308 	clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
1309 
1310 	xid = io_req->xid;
1311 	sc_cmd = io_req->sc_cmd;
1312 
1313 	if (!sc_cmd) {
1314 		QEDF_WARN(&(qedf->dbg_ctx), "sc_cmd is NULL!\n");
1315 		return;
1316 	}
1317 
1318 	if (!virt_addr_valid(sc_cmd)) {
1319 		QEDF_ERR(&qedf->dbg_ctx, "sc_cmd=%p is not valid.", sc_cmd);
1320 		goto bad_scsi_ptr;
1321 	}
1322 
1323 	if (!sc_cmd->SCp.ptr) {
1324 		QEDF_WARN(&(qedf->dbg_ctx), "SCp.ptr is NULL, returned in "
1325 		    "another context.\n");
1326 		return;
1327 	}
1328 
1329 	if (!sc_cmd->device) {
1330 		QEDF_ERR(&qedf->dbg_ctx, "Device for sc_cmd %p is NULL.\n",
1331 			 sc_cmd);
1332 		goto bad_scsi_ptr;
1333 	}
1334 
1335 	if (!virt_addr_valid(sc_cmd->device)) {
1336 		QEDF_ERR(&qedf->dbg_ctx,
1337 			 "Device pointer for sc_cmd %p is bad.\n", sc_cmd);
1338 		goto bad_scsi_ptr;
1339 	}
1340 
1341 	if (!sc_cmd->sense_buffer) {
1342 		QEDF_ERR(&qedf->dbg_ctx,
1343 			 "sc_cmd->sense_buffer for sc_cmd %p is NULL.\n",
1344 			 sc_cmd);
1345 		goto bad_scsi_ptr;
1346 	}
1347 
1348 	if (!virt_addr_valid(sc_cmd->sense_buffer)) {
1349 		QEDF_ERR(&qedf->dbg_ctx,
1350 			 "sc_cmd->sense_buffer for sc_cmd %p is bad.\n",
1351 			 sc_cmd);
1352 		goto bad_scsi_ptr;
1353 	}
1354 
1355 	if (!sc_cmd->scsi_done) {
1356 		QEDF_ERR(&qedf->dbg_ctx,
1357 			 "sc_cmd->scsi_done for sc_cmd %p is NULL.\n",
1358 			 sc_cmd);
1359 		goto bad_scsi_ptr;
1360 	}
1361 
1362 	qedf_unmap_sg_list(qedf, io_req);
1363 
1364 	sc_cmd->result = result << 16;
1365 	refcount = kref_read(&io_req->refcount);
1366 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO, "%d:0:%d:%lld: Completing "
1367 	    "sc_cmd=%p result=0x%08x op=0x%02x lba=0x%02x%02x%02x%02x, "
1368 	    "allowed=%d retries=%d refcount=%d.\n",
1369 	    qedf->lport->host->host_no, sc_cmd->device->id,
1370 	    sc_cmd->device->lun, sc_cmd, sc_cmd->result, sc_cmd->cmnd[0],
1371 	    sc_cmd->cmnd[2], sc_cmd->cmnd[3], sc_cmd->cmnd[4],
1372 	    sc_cmd->cmnd[5], sc_cmd->allowed, sc_cmd->retries,
1373 	    refcount);
1374 
1375 	/*
1376 	 * Set resid to the whole buffer length so we won't try to resue any
1377 	 * previously read data
1378 	 */
1379 	scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
1380 
1381 	if (qedf_io_tracing)
1382 		qedf_trace_io(io_req->fcport, io_req, QEDF_IO_TRACE_RSP);
1383 
1384 	io_req->sc_cmd = NULL;
1385 	sc_cmd->SCp.ptr = NULL;
1386 	sc_cmd->scsi_done(sc_cmd);
1387 	kref_put(&io_req->refcount, qedf_release_cmd);
1388 	return;
1389 
1390 bad_scsi_ptr:
1391 	/*
1392 	 * Clear the io_req->sc_cmd backpointer so we don't try to process
1393 	 * this again
1394 	 */
1395 	io_req->sc_cmd = NULL;
1396 	kref_put(&io_req->refcount, qedf_release_cmd);  /* ID: 001 */
1397 }
1398 
1399 /*
1400  * Handle warning type CQE completions. This is mainly used for REC timer
1401  * popping.
1402  */
1403 void qedf_process_warning_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
1404 	struct qedf_ioreq *io_req)
1405 {
1406 	int rval, i;
1407 	struct qedf_rport *fcport = io_req->fcport;
1408 	u64 err_warn_bit_map;
1409 	u8 err_warn = 0xff;
1410 
1411 	if (!cqe)
1412 		return;
1413 
1414 	QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "Warning CQE, "
1415 		  "xid=0x%x\n", io_req->xid);
1416 	QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx),
1417 		  "err_warn_bitmap=%08x:%08x\n",
1418 		  le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_hi),
1419 		  le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_lo));
1420 	QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "tx_buff_off=%08x, "
1421 		  "rx_buff_off=%08x, rx_id=%04x\n",
1422 		  le32_to_cpu(cqe->cqe_info.err_info.tx_buf_off),
1423 		  le32_to_cpu(cqe->cqe_info.err_info.rx_buf_off),
1424 		  le32_to_cpu(cqe->cqe_info.err_info.rx_id));
1425 
1426 	/* Normalize the error bitmap value to an just an unsigned int */
1427 	err_warn_bit_map = (u64)
1428 	    ((u64)cqe->cqe_info.err_info.err_warn_bitmap_hi << 32) |
1429 	    (u64)cqe->cqe_info.err_info.err_warn_bitmap_lo;
1430 	for (i = 0; i < 64; i++) {
1431 		if (err_warn_bit_map & (u64)((u64)1 << i)) {
1432 			err_warn = i;
1433 			break;
1434 		}
1435 	}
1436 
1437 	/* Check if REC TOV expired if this is a tape device */
1438 	if (fcport->dev_type == QEDF_RPORT_TYPE_TAPE) {
1439 		if (err_warn ==
1440 		    FCOE_WARNING_CODE_REC_TOV_TIMER_EXPIRATION) {
1441 			QEDF_ERR(&(qedf->dbg_ctx), "REC timer expired.\n");
1442 			if (!test_bit(QEDF_CMD_SRR_SENT, &io_req->flags)) {
1443 				io_req->rx_buf_off =
1444 				    cqe->cqe_info.err_info.rx_buf_off;
1445 				io_req->tx_buf_off =
1446 				    cqe->cqe_info.err_info.tx_buf_off;
1447 				io_req->rx_id = cqe->cqe_info.err_info.rx_id;
1448 				rval = qedf_send_rec(io_req);
1449 				/*
1450 				 * We only want to abort the io_req if we
1451 				 * can't queue the REC command as we want to
1452 				 * keep the exchange open for recovery.
1453 				 */
1454 				if (rval)
1455 					goto send_abort;
1456 			}
1457 			return;
1458 		}
1459 	}
1460 
1461 send_abort:
1462 	init_completion(&io_req->abts_done);
1463 	rval = qedf_initiate_abts(io_req, true);
1464 	if (rval)
1465 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
1466 }
1467 
1468 /* Cleanup a command when we receive an error detection completion */
1469 void qedf_process_error_detect(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
1470 	struct qedf_ioreq *io_req)
1471 {
1472 	int rval;
1473 
1474 	if (!cqe)
1475 		return;
1476 
1477 	QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "Error detection CQE, "
1478 		  "xid=0x%x\n", io_req->xid);
1479 	QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx),
1480 		  "err_warn_bitmap=%08x:%08x\n",
1481 		  le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_hi),
1482 		  le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_lo));
1483 	QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "tx_buff_off=%08x, "
1484 		  "rx_buff_off=%08x, rx_id=%04x\n",
1485 		  le32_to_cpu(cqe->cqe_info.err_info.tx_buf_off),
1486 		  le32_to_cpu(cqe->cqe_info.err_info.rx_buf_off),
1487 		  le32_to_cpu(cqe->cqe_info.err_info.rx_id));
1488 
1489 	if (qedf->stop_io_on_error) {
1490 		qedf_stop_all_io(qedf);
1491 		return;
1492 	}
1493 
1494 	init_completion(&io_req->abts_done);
1495 	rval = qedf_initiate_abts(io_req, true);
1496 	if (rval)
1497 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
1498 }
1499 
1500 static void qedf_flush_els_req(struct qedf_ctx *qedf,
1501 	struct qedf_ioreq *els_req)
1502 {
1503 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1504 	    "Flushing ELS request xid=0x%x refcount=%d.\n", els_req->xid,
1505 	    kref_read(&els_req->refcount));
1506 
1507 	/*
1508 	 * Need to distinguish this from a timeout when calling the
1509 	 * els_req->cb_func.
1510 	 */
1511 	els_req->event = QEDF_IOREQ_EV_ELS_FLUSH;
1512 
1513 	/* Cancel the timer */
1514 	cancel_delayed_work_sync(&els_req->timeout_work);
1515 
1516 	/* Call callback function to complete command */
1517 	if (els_req->cb_func && els_req->cb_arg) {
1518 		els_req->cb_func(els_req->cb_arg);
1519 		els_req->cb_arg = NULL;
1520 	}
1521 
1522 	/* Release kref for original initiate_els */
1523 	kref_put(&els_req->refcount, qedf_release_cmd);
1524 }
1525 
1526 /* A value of -1 for lun is a wild card that means flush all
1527  * active SCSI I/Os for the target.
1528  */
1529 void qedf_flush_active_ios(struct qedf_rport *fcport, int lun)
1530 {
1531 	struct qedf_ioreq *io_req;
1532 	struct qedf_ctx *qedf;
1533 	struct qedf_cmd_mgr *cmd_mgr;
1534 	int i, rc;
1535 	unsigned long flags;
1536 	int flush_cnt = 0;
1537 	int wait_cnt = 100;
1538 	int refcount = 0;
1539 
1540 	if (!fcport)
1541 		return;
1542 
1543 	/* Check that fcport is still offloaded */
1544 	if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1545 		QEDF_ERR(NULL, "fcport is no longer offloaded.\n");
1546 		return;
1547 	}
1548 
1549 	qedf = fcport->qedf;
1550 
1551 	if (!qedf) {
1552 		QEDF_ERR(NULL, "qedf is NULL.\n");
1553 		return;
1554 	}
1555 
1556 	/* Only wait for all commands to be queued in the Upload context */
1557 	if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags) &&
1558 	    (lun == -1)) {
1559 		while (atomic_read(&fcport->ios_to_queue)) {
1560 			QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1561 				  "Waiting for %d I/Os to be queued\n",
1562 				  atomic_read(&fcport->ios_to_queue));
1563 			if (wait_cnt == 0) {
1564 				QEDF_ERR(NULL,
1565 					 "%d IOs request could not be queued\n",
1566 					 atomic_read(&fcport->ios_to_queue));
1567 			}
1568 			msleep(20);
1569 			wait_cnt--;
1570 		}
1571 	}
1572 
1573 	cmd_mgr = qedf->cmd_mgr;
1574 
1575 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1576 		  "Flush active i/o's num=0x%x fcport=0x%p port_id=0x%06x scsi_id=%d.\n",
1577 		  atomic_read(&fcport->num_active_ios), fcport,
1578 		  fcport->rdata->ids.port_id, fcport->rport->scsi_target_id);
1579 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Locking flush mutex.\n");
1580 
1581 	mutex_lock(&qedf->flush_mutex);
1582 	if (lun == -1) {
1583 		set_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags);
1584 	} else {
1585 		set_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags);
1586 		fcport->lun_reset_lun = lun;
1587 	}
1588 
1589 	for (i = 0; i < FCOE_PARAMS_NUM_TASKS; i++) {
1590 		io_req = &cmd_mgr->cmds[i];
1591 
1592 		if (!io_req)
1593 			continue;
1594 		if (!io_req->fcport)
1595 			continue;
1596 
1597 		spin_lock_irqsave(&cmd_mgr->lock, flags);
1598 
1599 		if (io_req->alloc) {
1600 			if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags)) {
1601 				if (io_req->cmd_type == QEDF_SCSI_CMD)
1602 					QEDF_ERR(&qedf->dbg_ctx,
1603 						 "Allocated but not queued, xid=0x%x\n",
1604 						 io_req->xid);
1605 			}
1606 			spin_unlock_irqrestore(&cmd_mgr->lock, flags);
1607 		} else {
1608 			spin_unlock_irqrestore(&cmd_mgr->lock, flags);
1609 			continue;
1610 		}
1611 
1612 		if (io_req->fcport != fcport)
1613 			continue;
1614 
1615 		/* In case of ABTS, CMD_OUTSTANDING is cleared on ABTS response,
1616 		 * but RRQ is still pending.
1617 		 * Workaround: Within qedf_send_rrq, we check if the fcport is
1618 		 * NULL, and we drop the ref on the io_req to clean it up.
1619 		 */
1620 		if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags)) {
1621 			refcount = kref_read(&io_req->refcount);
1622 			QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1623 				  "Not outstanding, xid=0x%x, cmd_type=%d refcount=%d.\n",
1624 				  io_req->xid, io_req->cmd_type, refcount);
1625 			continue;
1626 		}
1627 
1628 		/* Only consider flushing ELS during target reset */
1629 		if (io_req->cmd_type == QEDF_ELS &&
1630 		    lun == -1) {
1631 			rc = kref_get_unless_zero(&io_req->refcount);
1632 			if (!rc) {
1633 				QEDF_ERR(&(qedf->dbg_ctx),
1634 				    "Could not get kref for ELS io_req=0x%p xid=0x%x.\n",
1635 				    io_req, io_req->xid);
1636 				continue;
1637 			}
1638 			flush_cnt++;
1639 			qedf_flush_els_req(qedf, io_req);
1640 			/*
1641 			 * Release the kref and go back to the top of the
1642 			 * loop.
1643 			 */
1644 			goto free_cmd;
1645 		}
1646 
1647 		if (io_req->cmd_type == QEDF_ABTS) {
1648 			rc = kref_get_unless_zero(&io_req->refcount);
1649 			if (!rc) {
1650 				QEDF_ERR(&(qedf->dbg_ctx),
1651 				    "Could not get kref for abort io_req=0x%p xid=0x%x.\n",
1652 				    io_req, io_req->xid);
1653 				continue;
1654 			}
1655 			if (lun != -1 && io_req->lun != lun)
1656 				goto free_cmd;
1657 
1658 			QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1659 			    "Flushing abort xid=0x%x.\n", io_req->xid);
1660 
1661 			if (cancel_delayed_work_sync(&io_req->rrq_work)) {
1662 				QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1663 					  "Putting reference for pending RRQ work xid=0x%x.\n",
1664 					  io_req->xid);
1665 				kref_put(&io_req->refcount, qedf_release_cmd);
1666 			}
1667 
1668 			/* Cancel any timeout work */
1669 			cancel_delayed_work_sync(&io_req->timeout_work);
1670 
1671 			if (!test_bit(QEDF_CMD_IN_ABORT, &io_req->flags))
1672 				goto free_cmd;
1673 
1674 			qedf_initiate_cleanup(io_req, true);
1675 			flush_cnt++;
1676 
1677 			/* Notify eh_abort handler that ABTS is complete */
1678 			kref_put(&io_req->refcount, qedf_release_cmd);
1679 			complete(&io_req->abts_done);
1680 
1681 			goto free_cmd;
1682 		}
1683 
1684 		if (!io_req->sc_cmd)
1685 			continue;
1686 		if (!io_req->sc_cmd->device) {
1687 			QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1688 				  "Device backpointer NULL for sc_cmd=%p.\n",
1689 				  io_req->sc_cmd);
1690 			/* Put reference for non-existent scsi_cmnd */
1691 			io_req->sc_cmd = NULL;
1692 			qedf_initiate_cleanup(io_req, false);
1693 			kref_put(&io_req->refcount, qedf_release_cmd);
1694 			continue;
1695 		}
1696 		if (lun > -1) {
1697 			if (io_req->lun != lun)
1698 				continue;
1699 		}
1700 
1701 		/*
1702 		 * Use kref_get_unless_zero in the unlikely case the command
1703 		 * we're about to flush was completed in the normal SCSI path
1704 		 */
1705 		rc = kref_get_unless_zero(&io_req->refcount);
1706 		if (!rc) {
1707 			QEDF_ERR(&(qedf->dbg_ctx), "Could not get kref for "
1708 			    "io_req=0x%p xid=0x%x\n", io_req, io_req->xid);
1709 			continue;
1710 		}
1711 
1712 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1713 		    "Cleanup xid=0x%x.\n", io_req->xid);
1714 		flush_cnt++;
1715 
1716 		/* Cleanup task and return I/O mid-layer */
1717 		qedf_initiate_cleanup(io_req, true);
1718 
1719 free_cmd:
1720 		kref_put(&io_req->refcount, qedf_release_cmd);
1721 	}
1722 
1723 	wait_cnt = 60;
1724 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1725 		  "Flushed 0x%x I/Os, active=0x%x.\n",
1726 		  flush_cnt, atomic_read(&fcport->num_active_ios));
1727 	/* Only wait for all commands to complete in the Upload context */
1728 	if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags) &&
1729 	    (lun == -1)) {
1730 		while (atomic_read(&fcport->num_active_ios)) {
1731 			QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1732 				  "Flushed 0x%x I/Os, active=0x%x cnt=%d.\n",
1733 				  flush_cnt,
1734 				  atomic_read(&fcport->num_active_ios),
1735 				  wait_cnt);
1736 			if (wait_cnt == 0) {
1737 				QEDF_ERR(&qedf->dbg_ctx,
1738 					 "Flushed %d I/Os, active=%d.\n",
1739 					 flush_cnt,
1740 					 atomic_read(&fcport->num_active_ios));
1741 				for (i = 0; i < FCOE_PARAMS_NUM_TASKS; i++) {
1742 					io_req = &cmd_mgr->cmds[i];
1743 					if (io_req->fcport &&
1744 					    io_req->fcport == fcport) {
1745 						refcount =
1746 						kref_read(&io_req->refcount);
1747 						QEDF_ERR(&qedf->dbg_ctx,
1748 							 "Outstanding io_req =%p xid=0x%x flags=0x%lx, sc_cmd=%p refcount=%d cmd_type=%d.\n",
1749 							 io_req, io_req->xid,
1750 							 io_req->flags,
1751 							 io_req->sc_cmd,
1752 							 refcount,
1753 							 io_req->cmd_type);
1754 					}
1755 				}
1756 				WARN_ON(1);
1757 				break;
1758 			}
1759 			msleep(500);
1760 			wait_cnt--;
1761 		}
1762 	}
1763 
1764 	clear_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags);
1765 	clear_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags);
1766 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Unlocking flush mutex.\n");
1767 	mutex_unlock(&qedf->flush_mutex);
1768 }
1769 
1770 /*
1771  * Initiate a ABTS middle path command. Note that we don't have to initialize
1772  * the task context for an ABTS task.
1773  */
1774 int qedf_initiate_abts(struct qedf_ioreq *io_req, bool return_scsi_cmd_on_abts)
1775 {
1776 	struct fc_lport *lport;
1777 	struct qedf_rport *fcport = io_req->fcport;
1778 	struct fc_rport_priv *rdata;
1779 	struct qedf_ctx *qedf;
1780 	u16 xid;
1781 	u32 r_a_tov = 0;
1782 	int rc = 0;
1783 	unsigned long flags;
1784 	struct fcoe_wqe *sqe;
1785 	u16 sqe_idx;
1786 	int refcount = 0;
1787 
1788 	/* Sanity check qedf_rport before dereferencing any pointers */
1789 	if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1790 		QEDF_ERR(NULL, "tgt not offloaded\n");
1791 		rc = 1;
1792 		goto out;
1793 	}
1794 
1795 	qedf = fcport->qedf;
1796 	rdata = fcport->rdata;
1797 
1798 	if (!rdata || !kref_get_unless_zero(&rdata->kref)) {
1799 		QEDF_ERR(&qedf->dbg_ctx, "stale rport\n");
1800 		rc = 1;
1801 		goto out;
1802 	}
1803 
1804 	r_a_tov = rdata->r_a_tov;
1805 	lport = qedf->lport;
1806 
1807 	if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
1808 		QEDF_ERR(&(qedf->dbg_ctx), "link is not ready\n");
1809 		rc = 1;
1810 		goto drop_rdata_kref;
1811 	}
1812 
1813 	if (atomic_read(&qedf->link_down_tmo_valid) > 0) {
1814 		QEDF_ERR(&(qedf->dbg_ctx), "link_down_tmo active.\n");
1815 		rc = 1;
1816 		goto drop_rdata_kref;
1817 	}
1818 
1819 	/* Ensure room on SQ */
1820 	if (!atomic_read(&fcport->free_sqes)) {
1821 		QEDF_ERR(&(qedf->dbg_ctx), "No SQ entries available\n");
1822 		rc = 1;
1823 		goto drop_rdata_kref;
1824 	}
1825 
1826 	if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
1827 		QEDF_ERR(&qedf->dbg_ctx, "fcport is uploading.\n");
1828 		rc = 1;
1829 		goto drop_rdata_kref;
1830 	}
1831 
1832 	if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
1833 	    test_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags) ||
1834 	    test_bit(QEDF_CMD_IN_ABORT, &io_req->flags)) {
1835 		QEDF_ERR(&(qedf->dbg_ctx), "io_req xid=0x%x already in "
1836 			  "cleanup or abort processing or already "
1837 			  "completed.\n", io_req->xid);
1838 		rc = 1;
1839 		goto drop_rdata_kref;
1840 	}
1841 
1842 	kref_get(&io_req->refcount);
1843 
1844 	xid = io_req->xid;
1845 	qedf->control_requests++;
1846 	qedf->packet_aborts++;
1847 
1848 	/* Set the command type to abort */
1849 	io_req->cmd_type = QEDF_ABTS;
1850 	io_req->return_scsi_cmd_on_abts = return_scsi_cmd_on_abts;
1851 
1852 	set_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
1853 	refcount = kref_read(&io_req->refcount);
1854 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
1855 		  "ABTS io_req xid = 0x%x refcount=%d\n",
1856 		  xid, refcount);
1857 
1858 	qedf_cmd_timer_set(qedf, io_req, QEDF_ABORT_TIMEOUT);
1859 
1860 	spin_lock_irqsave(&fcport->rport_lock, flags);
1861 
1862 	sqe_idx = qedf_get_sqe_idx(fcport);
1863 	sqe = &fcport->sq[sqe_idx];
1864 	memset(sqe, 0, sizeof(struct fcoe_wqe));
1865 	io_req->task_params->sqe = sqe;
1866 
1867 	init_initiator_abort_fcoe_task(io_req->task_params);
1868 	qedf_ring_doorbell(fcport);
1869 
1870 	spin_unlock_irqrestore(&fcport->rport_lock, flags);
1871 
1872 drop_rdata_kref:
1873 	kref_put(&rdata->kref, fc_rport_destroy);
1874 out:
1875 	return rc;
1876 }
1877 
1878 void qedf_process_abts_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
1879 	struct qedf_ioreq *io_req)
1880 {
1881 	uint32_t r_ctl;
1882 	uint16_t xid;
1883 	int rc;
1884 	struct qedf_rport *fcport = io_req->fcport;
1885 
1886 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM, "Entered with xid = "
1887 		   "0x%x cmd_type = %d\n", io_req->xid, io_req->cmd_type);
1888 
1889 	xid = io_req->xid;
1890 	r_ctl = cqe->cqe_info.abts_info.r_ctl;
1891 
1892 	/* This was added at a point when we were scheduling abts_compl &
1893 	 * cleanup_compl on different CPUs and there was a possibility of
1894 	 * the io_req to be freed from the other context before we got here.
1895 	 */
1896 	if (!fcport) {
1897 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1898 			  "Dropping ABTS completion xid=0x%x as fcport is NULL",
1899 			  io_req->xid);
1900 		return;
1901 	}
1902 
1903 	/*
1904 	 * When flush is active, let the cmds be completed from the cleanup
1905 	 * context
1906 	 */
1907 	if (test_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags) ||
1908 	    test_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags)) {
1909 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1910 			  "Dropping ABTS completion xid=0x%x as fcport is flushing",
1911 			  io_req->xid);
1912 		return;
1913 	}
1914 
1915 	if (!cancel_delayed_work(&io_req->timeout_work)) {
1916 		QEDF_ERR(&qedf->dbg_ctx,
1917 			 "Wasn't able to cancel abts timeout work.\n");
1918 	}
1919 
1920 	switch (r_ctl) {
1921 	case FC_RCTL_BA_ACC:
1922 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM,
1923 		    "ABTS response - ACC Send RRQ after R_A_TOV\n");
1924 		io_req->event = QEDF_IOREQ_EV_ABORT_SUCCESS;
1925 		rc = kref_get_unless_zero(&io_req->refcount);
1926 		if (!rc) {
1927 			QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
1928 				  "kref is already zero so ABTS was already completed or flushed xid=0x%x.\n",
1929 				  io_req->xid);
1930 			return;
1931 		}
1932 		/*
1933 		 * Dont release this cmd yet. It will be relesed
1934 		 * after we get RRQ response
1935 		 */
1936 		queue_delayed_work(qedf->dpc_wq, &io_req->rrq_work,
1937 		    msecs_to_jiffies(qedf->lport->r_a_tov));
1938 		break;
1939 	/* For error cases let the cleanup return the command */
1940 	case FC_RCTL_BA_RJT:
1941 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM,
1942 		   "ABTS response - RJT\n");
1943 		io_req->event = QEDF_IOREQ_EV_ABORT_FAILED;
1944 		break;
1945 	default:
1946 		QEDF_ERR(&(qedf->dbg_ctx), "Unknown ABTS response\n");
1947 		break;
1948 	}
1949 
1950 	clear_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
1951 
1952 	if (io_req->sc_cmd) {
1953 		if (io_req->return_scsi_cmd_on_abts)
1954 			qedf_scsi_done(qedf, io_req, DID_ERROR);
1955 	}
1956 
1957 	/* Notify eh_abort handler that ABTS is complete */
1958 	complete(&io_req->abts_done);
1959 
1960 	kref_put(&io_req->refcount, qedf_release_cmd);
1961 }
1962 
1963 int qedf_init_mp_req(struct qedf_ioreq *io_req)
1964 {
1965 	struct qedf_mp_req *mp_req;
1966 	struct scsi_sge *mp_req_bd;
1967 	struct scsi_sge *mp_resp_bd;
1968 	struct qedf_ctx *qedf = io_req->fcport->qedf;
1969 	dma_addr_t addr;
1970 	uint64_t sz;
1971 
1972 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_MP_REQ, "Entered.\n");
1973 
1974 	mp_req = (struct qedf_mp_req *)&(io_req->mp_req);
1975 	memset(mp_req, 0, sizeof(struct qedf_mp_req));
1976 
1977 	if (io_req->cmd_type != QEDF_ELS) {
1978 		mp_req->req_len = sizeof(struct fcp_cmnd);
1979 		io_req->data_xfer_len = mp_req->req_len;
1980 	} else
1981 		mp_req->req_len = io_req->data_xfer_len;
1982 
1983 	mp_req->req_buf = dma_alloc_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
1984 	    &mp_req->req_buf_dma, GFP_KERNEL);
1985 	if (!mp_req->req_buf) {
1986 		QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc MP req buffer\n");
1987 		qedf_free_mp_resc(io_req);
1988 		return -ENOMEM;
1989 	}
1990 
1991 	mp_req->resp_buf = dma_alloc_coherent(&qedf->pdev->dev,
1992 	    QEDF_PAGE_SIZE, &mp_req->resp_buf_dma, GFP_KERNEL);
1993 	if (!mp_req->resp_buf) {
1994 		QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc TM resp "
1995 			  "buffer\n");
1996 		qedf_free_mp_resc(io_req);
1997 		return -ENOMEM;
1998 	}
1999 
2000 	/* Allocate and map mp_req_bd and mp_resp_bd */
2001 	sz = sizeof(struct scsi_sge);
2002 	mp_req->mp_req_bd = dma_alloc_coherent(&qedf->pdev->dev, sz,
2003 	    &mp_req->mp_req_bd_dma, GFP_KERNEL);
2004 	if (!mp_req->mp_req_bd) {
2005 		QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc MP req bd\n");
2006 		qedf_free_mp_resc(io_req);
2007 		return -ENOMEM;
2008 	}
2009 
2010 	mp_req->mp_resp_bd = dma_alloc_coherent(&qedf->pdev->dev, sz,
2011 	    &mp_req->mp_resp_bd_dma, GFP_KERNEL);
2012 	if (!mp_req->mp_resp_bd) {
2013 		QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc MP resp bd\n");
2014 		qedf_free_mp_resc(io_req);
2015 		return -ENOMEM;
2016 	}
2017 
2018 	/* Fill bd table */
2019 	addr = mp_req->req_buf_dma;
2020 	mp_req_bd = mp_req->mp_req_bd;
2021 	mp_req_bd->sge_addr.lo = U64_LO(addr);
2022 	mp_req_bd->sge_addr.hi = U64_HI(addr);
2023 	mp_req_bd->sge_len = QEDF_PAGE_SIZE;
2024 
2025 	/*
2026 	 * MP buffer is either a task mgmt command or an ELS.
2027 	 * So the assumption is that it consumes a single bd
2028 	 * entry in the bd table
2029 	 */
2030 	mp_resp_bd = mp_req->mp_resp_bd;
2031 	addr = mp_req->resp_buf_dma;
2032 	mp_resp_bd->sge_addr.lo = U64_LO(addr);
2033 	mp_resp_bd->sge_addr.hi = U64_HI(addr);
2034 	mp_resp_bd->sge_len = QEDF_PAGE_SIZE;
2035 
2036 	return 0;
2037 }
2038 
2039 /*
2040  * Last ditch effort to clear the port if it's stuck. Used only after a
2041  * cleanup task times out.
2042  */
2043 static void qedf_drain_request(struct qedf_ctx *qedf)
2044 {
2045 	if (test_bit(QEDF_DRAIN_ACTIVE, &qedf->flags)) {
2046 		QEDF_ERR(&(qedf->dbg_ctx), "MCP drain already active.\n");
2047 		return;
2048 	}
2049 
2050 	/* Set bit to return all queuecommand requests as busy */
2051 	set_bit(QEDF_DRAIN_ACTIVE, &qedf->flags);
2052 
2053 	/* Call qed drain request for function. Should be synchronous */
2054 	qed_ops->common->drain(qedf->cdev);
2055 
2056 	/* Settle time for CQEs to be returned */
2057 	msleep(100);
2058 
2059 	/* Unplug and continue */
2060 	clear_bit(QEDF_DRAIN_ACTIVE, &qedf->flags);
2061 }
2062 
2063 /*
2064  * Returns SUCCESS if the cleanup task does not timeout, otherwise return
2065  * FAILURE.
2066  */
2067 int qedf_initiate_cleanup(struct qedf_ioreq *io_req,
2068 	bool return_scsi_cmd_on_abts)
2069 {
2070 	struct qedf_rport *fcport;
2071 	struct qedf_ctx *qedf;
2072 	uint16_t xid;
2073 	struct e4_fcoe_task_context *task;
2074 	int tmo = 0;
2075 	int rc = SUCCESS;
2076 	unsigned long flags;
2077 	struct fcoe_wqe *sqe;
2078 	u16 sqe_idx;
2079 	int refcount = 0;
2080 
2081 	fcport = io_req->fcport;
2082 	if (!fcport) {
2083 		QEDF_ERR(NULL, "fcport is NULL.\n");
2084 		return SUCCESS;
2085 	}
2086 
2087 	/* Sanity check qedf_rport before dereferencing any pointers */
2088 	if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
2089 		QEDF_ERR(NULL, "tgt not offloaded\n");
2090 		rc = 1;
2091 		return SUCCESS;
2092 	}
2093 
2094 	qedf = fcport->qedf;
2095 	if (!qedf) {
2096 		QEDF_ERR(NULL, "qedf is NULL.\n");
2097 		return SUCCESS;
2098 	}
2099 
2100 	if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
2101 	    test_and_set_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags)) {
2102 		QEDF_ERR(&(qedf->dbg_ctx), "io_req xid=0x%x already in "
2103 			  "cleanup processing or already completed.\n",
2104 			  io_req->xid);
2105 		return SUCCESS;
2106 	}
2107 	set_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2108 
2109 	/* Ensure room on SQ */
2110 	if (!atomic_read(&fcport->free_sqes)) {
2111 		QEDF_ERR(&(qedf->dbg_ctx), "No SQ entries available\n");
2112 		/* Need to make sure we clear the flag since it was set */
2113 		clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2114 		return FAILED;
2115 	}
2116 
2117 	if (io_req->cmd_type == QEDF_CLEANUP) {
2118 		QEDF_ERR(&qedf->dbg_ctx,
2119 			 "io_req=0x%x is already a cleanup command cmd_type=%d.\n",
2120 			 io_req->xid, io_req->cmd_type);
2121 		clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2122 		return SUCCESS;
2123 	}
2124 
2125 	refcount = kref_read(&io_req->refcount);
2126 
2127 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
2128 		  "Entered xid=0x%x sc_cmd=%p cmd_type=%d flags=0x%lx refcount=%d\n",
2129 		  io_req->xid, io_req->sc_cmd, io_req->cmd_type, io_req->flags,
2130 		  refcount);
2131 
2132 	/* Cleanup cmds re-use the same TID as the original I/O */
2133 	xid = io_req->xid;
2134 	io_req->cmd_type = QEDF_CLEANUP;
2135 	io_req->return_scsi_cmd_on_abts = return_scsi_cmd_on_abts;
2136 
2137 	task = qedf_get_task_mem(&qedf->tasks, xid);
2138 
2139 	init_completion(&io_req->cleanup_done);
2140 
2141 	spin_lock_irqsave(&fcport->rport_lock, flags);
2142 
2143 	sqe_idx = qedf_get_sqe_idx(fcport);
2144 	sqe = &fcport->sq[sqe_idx];
2145 	memset(sqe, 0, sizeof(struct fcoe_wqe));
2146 	io_req->task_params->sqe = sqe;
2147 
2148 	init_initiator_cleanup_fcoe_task(io_req->task_params);
2149 	qedf_ring_doorbell(fcport);
2150 
2151 	spin_unlock_irqrestore(&fcport->rport_lock, flags);
2152 
2153 	tmo = wait_for_completion_timeout(&io_req->cleanup_done,
2154 					  QEDF_CLEANUP_TIMEOUT * HZ);
2155 
2156 	if (!tmo) {
2157 		rc = FAILED;
2158 		/* Timeout case */
2159 		QEDF_ERR(&(qedf->dbg_ctx), "Cleanup command timeout, "
2160 			  "xid=%x.\n", io_req->xid);
2161 		clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2162 		/* Issue a drain request if cleanup task times out */
2163 		QEDF_ERR(&(qedf->dbg_ctx), "Issuing MCP drain request.\n");
2164 		qedf_drain_request(qedf);
2165 	}
2166 
2167 	/* If it TASK MGMT handle it, reference will be decreased
2168 	 * in qedf_execute_tmf
2169 	 */
2170 	if (io_req->cmd_type == QEDF_TASK_MGMT_CMD) {
2171 		clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
2172 		io_req->sc_cmd = NULL;
2173 		complete(&io_req->tm_done);
2174 	}
2175 
2176 	if (io_req->sc_cmd) {
2177 		if (io_req->return_scsi_cmd_on_abts)
2178 			qedf_scsi_done(qedf, io_req, DID_ERROR);
2179 	}
2180 
2181 	if (rc == SUCCESS)
2182 		io_req->event = QEDF_IOREQ_EV_CLEANUP_SUCCESS;
2183 	else
2184 		io_req->event = QEDF_IOREQ_EV_CLEANUP_FAILED;
2185 
2186 	return rc;
2187 }
2188 
2189 void qedf_process_cleanup_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
2190 	struct qedf_ioreq *io_req)
2191 {
2192 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO, "Entered xid = 0x%x\n",
2193 		   io_req->xid);
2194 
2195 	clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2196 
2197 	/* Complete so we can finish cleaning up the I/O */
2198 	complete(&io_req->cleanup_done);
2199 }
2200 
2201 static int qedf_execute_tmf(struct qedf_rport *fcport, struct scsi_cmnd *sc_cmd,
2202 	uint8_t tm_flags)
2203 {
2204 	struct qedf_ioreq *io_req;
2205 	struct e4_fcoe_task_context *task;
2206 	struct qedf_ctx *qedf = fcport->qedf;
2207 	struct fc_lport *lport = qedf->lport;
2208 	int rc = 0;
2209 	uint16_t xid;
2210 	int tmo = 0;
2211 	int lun = 0;
2212 	unsigned long flags;
2213 	struct fcoe_wqe *sqe;
2214 	u16 sqe_idx;
2215 	struct fc_rport_priv *rdata = fcport->rdata;
2216 
2217 	if (!sc_cmd) {
2218 		QEDF_ERR(&(qedf->dbg_ctx), "invalid arg\n");
2219 		return FAILED;
2220 	}
2221 
2222 	lun = (int)sc_cmd->device->lun;
2223 	if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
2224 		QEDF_ERR(&(qedf->dbg_ctx), "fcport not offloaded\n");
2225 		rc = FAILED;
2226 		return FAILED;
2227 	}
2228 
2229 	if (!rdata || !kref_get_unless_zero(&rdata->kref)) {
2230 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM, "stale rport\n");
2231 		return FAILED;
2232 	}
2233 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
2234 		  "portid = 0x%x tm_flags = %d\n",
2235 		  rdata->ids.port_id, tm_flags);
2236 
2237 	io_req = qedf_alloc_cmd(fcport, QEDF_TASK_MGMT_CMD);
2238 	if (!io_req) {
2239 		QEDF_ERR(&(qedf->dbg_ctx), "Failed TMF");
2240 		rc = -EAGAIN;
2241 		goto reset_tmf_err;
2242 	}
2243 
2244 	if (tm_flags == FCP_TMF_LUN_RESET)
2245 		qedf->lun_resets++;
2246 	else if (tm_flags == FCP_TMF_TGT_RESET)
2247 		qedf->target_resets++;
2248 
2249 	/* Initialize rest of io_req fields */
2250 	io_req->sc_cmd = sc_cmd;
2251 	io_req->fcport = fcport;
2252 	io_req->cmd_type = QEDF_TASK_MGMT_CMD;
2253 
2254 	/* Record which cpu this request is associated with */
2255 	io_req->cpu = smp_processor_id();
2256 
2257 	/* Set TM flags */
2258 	io_req->io_req_flags = QEDF_READ;
2259 	io_req->data_xfer_len = 0;
2260 	io_req->tm_flags = tm_flags;
2261 
2262 	/* Default is to return a SCSI command when an error occurs */
2263 	io_req->return_scsi_cmd_on_abts = false;
2264 
2265 	/* Obtain exchange id */
2266 	xid = io_req->xid;
2267 
2268 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM, "TMF io_req xid = "
2269 		   "0x%x\n", xid);
2270 
2271 	/* Initialize task context for this IO request */
2272 	task = qedf_get_task_mem(&qedf->tasks, xid);
2273 
2274 	init_completion(&io_req->tm_done);
2275 
2276 	spin_lock_irqsave(&fcport->rport_lock, flags);
2277 
2278 	sqe_idx = qedf_get_sqe_idx(fcport);
2279 	sqe = &fcport->sq[sqe_idx];
2280 	memset(sqe, 0, sizeof(struct fcoe_wqe));
2281 
2282 	qedf_init_task(fcport, lport, io_req, task, sqe);
2283 	qedf_ring_doorbell(fcport);
2284 
2285 	spin_unlock_irqrestore(&fcport->rport_lock, flags);
2286 
2287 	set_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
2288 	tmo = wait_for_completion_timeout(&io_req->tm_done,
2289 	    QEDF_TM_TIMEOUT * HZ);
2290 
2291 	if (!tmo) {
2292 		rc = FAILED;
2293 		QEDF_ERR(&(qedf->dbg_ctx), "wait for tm_cmpl timeout!\n");
2294 		/* Clear outstanding bit since command timed out */
2295 		clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
2296 		io_req->sc_cmd = NULL;
2297 	} else {
2298 		/* Check TMF response code */
2299 		if (io_req->fcp_rsp_code == 0)
2300 			rc = SUCCESS;
2301 		else
2302 			rc = FAILED;
2303 	}
2304 	/*
2305 	 * Double check that fcport has not gone into an uploading state before
2306 	 * executing the command flush for the LUN/target.
2307 	 */
2308 	if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
2309 		QEDF_ERR(&qedf->dbg_ctx,
2310 			 "fcport is uploading, not executing flush.\n");
2311 		goto no_flush;
2312 	}
2313 	/* We do not need this io_req any more */
2314 	kref_put(&io_req->refcount, qedf_release_cmd);
2315 
2316 
2317 	if (tm_flags == FCP_TMF_LUN_RESET)
2318 		qedf_flush_active_ios(fcport, lun);
2319 	else
2320 		qedf_flush_active_ios(fcport, -1);
2321 
2322 no_flush:
2323 	if (rc != SUCCESS) {
2324 		QEDF_ERR(&(qedf->dbg_ctx), "task mgmt command failed...\n");
2325 		rc = FAILED;
2326 	} else {
2327 		QEDF_ERR(&(qedf->dbg_ctx), "task mgmt command success...\n");
2328 		rc = SUCCESS;
2329 	}
2330 reset_tmf_err:
2331 	kref_put(&rdata->kref, fc_rport_destroy);
2332 	return rc;
2333 }
2334 
2335 int qedf_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
2336 {
2337 	struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
2338 	struct fc_rport_libfc_priv *rp = rport->dd_data;
2339 	struct qedf_rport *fcport = (struct qedf_rport *)&rp[1];
2340 	struct qedf_ctx *qedf;
2341 	struct fc_lport *lport;
2342 	int rc = SUCCESS;
2343 	int rval;
2344 	struct qedf_ioreq *io_req = NULL;
2345 	int ref_cnt = 0;
2346 	struct fc_rport_priv *rdata = fcport->rdata;
2347 
2348 	QEDF_ERR(NULL,
2349 		 "tm_flags 0x%x sc_cmd %p op = 0x%02x target_id = 0x%x lun=%d\n",
2350 		 tm_flags, sc_cmd, sc_cmd->cmnd[0], rport->scsi_target_id,
2351 		 (int)sc_cmd->device->lun);
2352 
2353 	if (!rdata || !kref_get_unless_zero(&rdata->kref)) {
2354 		QEDF_ERR(NULL, "stale rport\n");
2355 		return FAILED;
2356 	}
2357 
2358 	QEDF_ERR(NULL, "portid=%06x tm_flags =%s\n", rdata->ids.port_id,
2359 		 (tm_flags == FCP_TMF_TGT_RESET) ? "TARGET RESET" :
2360 		 "LUN RESET");
2361 
2362 	if (sc_cmd->SCp.ptr) {
2363 		io_req = (struct qedf_ioreq *)sc_cmd->SCp.ptr;
2364 		ref_cnt = kref_read(&io_req->refcount);
2365 		QEDF_ERR(NULL,
2366 			 "orig io_req = %p xid = 0x%x ref_cnt = %d.\n",
2367 			 io_req, io_req->xid, ref_cnt);
2368 	}
2369 
2370 	rval = fc_remote_port_chkready(rport);
2371 	if (rval) {
2372 		QEDF_ERR(NULL, "device_reset rport not ready\n");
2373 		rc = FAILED;
2374 		goto tmf_err;
2375 	}
2376 
2377 	rc = fc_block_scsi_eh(sc_cmd);
2378 	if (rc)
2379 		return rc;
2380 
2381 	if (!fcport) {
2382 		QEDF_ERR(NULL, "device_reset: rport is NULL\n");
2383 		rc = FAILED;
2384 		goto tmf_err;
2385 	}
2386 
2387 	qedf = fcport->qedf;
2388 
2389 	if (!qedf) {
2390 		QEDF_ERR(NULL, "qedf is NULL.\n");
2391 		rc = FAILED;
2392 		goto tmf_err;
2393 	}
2394 
2395 	lport = qedf->lport;
2396 
2397 	if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
2398 		QEDF_ERR(&qedf->dbg_ctx, "Connection is getting uploaded.\n");
2399 		rc = SUCCESS;
2400 		goto tmf_err;
2401 	}
2402 
2403 	if (test_bit(QEDF_UNLOADING, &qedf->flags) ||
2404 	    test_bit(QEDF_DBG_STOP_IO, &qedf->flags)) {
2405 		rc = SUCCESS;
2406 		goto tmf_err;
2407 	}
2408 
2409 	if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
2410 		QEDF_ERR(&(qedf->dbg_ctx), "link is not ready\n");
2411 		rc = FAILED;
2412 		goto tmf_err;
2413 	}
2414 
2415 	if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
2416 		QEDF_ERR(&qedf->dbg_ctx, "fcport is uploading.\n");
2417 		rc = FAILED;
2418 		goto tmf_err;
2419 	}
2420 
2421 	rc = qedf_execute_tmf(fcport, sc_cmd, tm_flags);
2422 
2423 tmf_err:
2424 	return rc;
2425 }
2426 
2427 void qedf_process_tmf_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
2428 	struct qedf_ioreq *io_req)
2429 {
2430 	struct fcoe_cqe_rsp_info *fcp_rsp;
2431 
2432 	clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
2433 
2434 	fcp_rsp = &cqe->cqe_info.rsp_info;
2435 	qedf_parse_fcp_rsp(io_req, fcp_rsp);
2436 
2437 	io_req->sc_cmd = NULL;
2438 	complete(&io_req->tm_done);
2439 }
2440 
2441 void qedf_process_unsol_compl(struct qedf_ctx *qedf, uint16_t que_idx,
2442 	struct fcoe_cqe *cqe)
2443 {
2444 	unsigned long flags;
2445 	uint16_t tmp;
2446 	uint16_t pktlen = cqe->cqe_info.unsolic_info.pkt_len;
2447 	u32 payload_len, crc;
2448 	struct fc_frame_header *fh;
2449 	struct fc_frame *fp;
2450 	struct qedf_io_work *io_work;
2451 	u32 bdq_idx;
2452 	void *bdq_addr;
2453 	struct scsi_bd *p_bd_info;
2454 
2455 	p_bd_info = &cqe->cqe_info.unsolic_info.bd_info;
2456 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
2457 		  "address.hi=%x, address.lo=%x, opaque_data.hi=%x, opaque_data.lo=%x, bdq_prod_idx=%u, len=%u\n",
2458 		  le32_to_cpu(p_bd_info->address.hi),
2459 		  le32_to_cpu(p_bd_info->address.lo),
2460 		  le32_to_cpu(p_bd_info->opaque.fcoe_opaque.hi),
2461 		  le32_to_cpu(p_bd_info->opaque.fcoe_opaque.lo),
2462 		  qedf->bdq_prod_idx, pktlen);
2463 
2464 	bdq_idx = le32_to_cpu(p_bd_info->opaque.fcoe_opaque.lo);
2465 	if (bdq_idx >= QEDF_BDQ_SIZE) {
2466 		QEDF_ERR(&(qedf->dbg_ctx), "bdq_idx is out of range %d.\n",
2467 		    bdq_idx);
2468 		goto increment_prod;
2469 	}
2470 
2471 	bdq_addr = qedf->bdq[bdq_idx].buf_addr;
2472 	if (!bdq_addr) {
2473 		QEDF_ERR(&(qedf->dbg_ctx), "bdq_addr is NULL, dropping "
2474 		    "unsolicited packet.\n");
2475 		goto increment_prod;
2476 	}
2477 
2478 	if (qedf_dump_frames) {
2479 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
2480 		    "BDQ frame is at addr=%p.\n", bdq_addr);
2481 		print_hex_dump(KERN_WARNING, "bdq ", DUMP_PREFIX_OFFSET, 16, 1,
2482 		    (void *)bdq_addr, pktlen, false);
2483 	}
2484 
2485 	/* Allocate frame */
2486 	payload_len = pktlen - sizeof(struct fc_frame_header);
2487 	fp = fc_frame_alloc(qedf->lport, payload_len);
2488 	if (!fp) {
2489 		QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate fp.\n");
2490 		goto increment_prod;
2491 	}
2492 
2493 	/* Copy data from BDQ buffer into fc_frame struct */
2494 	fh = (struct fc_frame_header *)fc_frame_header_get(fp);
2495 	memcpy(fh, (void *)bdq_addr, pktlen);
2496 
2497 	/* Initialize the frame so libfc sees it as a valid frame */
2498 	crc = fcoe_fc_crc(fp);
2499 	fc_frame_init(fp);
2500 	fr_dev(fp) = qedf->lport;
2501 	fr_sof(fp) = FC_SOF_I3;
2502 	fr_eof(fp) = FC_EOF_T;
2503 	fr_crc(fp) = cpu_to_le32(~crc);
2504 
2505 	/*
2506 	 * We need to return the frame back up to libfc in a non-atomic
2507 	 * context
2508 	 */
2509 	io_work = mempool_alloc(qedf->io_mempool, GFP_ATOMIC);
2510 	if (!io_work) {
2511 		QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate "
2512 			   "work for I/O completion.\n");
2513 		fc_frame_free(fp);
2514 		goto increment_prod;
2515 	}
2516 	memset(io_work, 0, sizeof(struct qedf_io_work));
2517 
2518 	INIT_WORK(&io_work->work, qedf_fp_io_handler);
2519 
2520 	/* Copy contents of CQE for deferred processing */
2521 	memcpy(&io_work->cqe, cqe, sizeof(struct fcoe_cqe));
2522 
2523 	io_work->qedf = qedf;
2524 	io_work->fp = fp;
2525 
2526 	queue_work_on(smp_processor_id(), qedf_io_wq, &io_work->work);
2527 increment_prod:
2528 	spin_lock_irqsave(&qedf->hba_lock, flags);
2529 
2530 	/* Increment producer to let f/w know we've handled the frame */
2531 	qedf->bdq_prod_idx++;
2532 
2533 	/* Producer index wraps at uint16_t boundary */
2534 	if (qedf->bdq_prod_idx == 0xffff)
2535 		qedf->bdq_prod_idx = 0;
2536 
2537 	writew(qedf->bdq_prod_idx, qedf->bdq_primary_prod);
2538 	tmp = readw(qedf->bdq_primary_prod);
2539 	writew(qedf->bdq_prod_idx, qedf->bdq_secondary_prod);
2540 	tmp = readw(qedf->bdq_secondary_prod);
2541 
2542 	spin_unlock_irqrestore(&qedf->hba_lock, flags);
2543 }
2544