xref: /openbmc/linux/drivers/scsi/qedi/qedi_fw.c (revision d9565bf4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * QLogic iSCSI Offload Driver
4  * Copyright (c) 2016 Cavium Inc.
5  */
6 
7 #include <linux/blkdev.h>
8 #include <scsi/scsi_tcq.h>
9 #include <linux/delay.h>
10 
11 #include "qedi.h"
12 #include "qedi_iscsi.h"
13 #include "qedi_gbl.h"
14 #include "qedi_fw_iscsi.h"
15 #include "qedi_fw_scsi.h"
16 
17 static int send_iscsi_tmf(struct qedi_conn *qedi_conn,
18 			  struct iscsi_task *mtask, struct iscsi_task *ctask);
19 
20 void qedi_iscsi_unmap_sg_list(struct qedi_cmd *cmd)
21 {
22 	struct scsi_cmnd *sc = cmd->scsi_cmd;
23 
24 	if (cmd->io_tbl.sge_valid && sc) {
25 		cmd->io_tbl.sge_valid = 0;
26 		scsi_dma_unmap(sc);
27 	}
28 }
29 
30 static void qedi_process_logout_resp(struct qedi_ctx *qedi,
31 				     union iscsi_cqe *cqe,
32 				     struct iscsi_task *task,
33 				     struct qedi_conn *qedi_conn)
34 {
35 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
36 	struct iscsi_logout_rsp *resp_hdr;
37 	struct iscsi_session *session = conn->session;
38 	struct iscsi_logout_response_hdr *cqe_logout_response;
39 	struct qedi_cmd *cmd;
40 
41 	cmd = (struct qedi_cmd *)task->dd_data;
42 	cqe_logout_response = &cqe->cqe_common.iscsi_hdr.logout_response;
43 	spin_lock(&session->back_lock);
44 	resp_hdr = (struct iscsi_logout_rsp *)&qedi_conn->gen_pdu.resp_hdr;
45 	memset(resp_hdr, 0, sizeof(struct iscsi_hdr));
46 	resp_hdr->opcode = cqe_logout_response->opcode;
47 	resp_hdr->flags = cqe_logout_response->flags;
48 	resp_hdr->hlength = 0;
49 
50 	resp_hdr->itt = build_itt(cqe->cqe_solicited.itid, conn->session->age);
51 	resp_hdr->statsn = cpu_to_be32(cqe_logout_response->stat_sn);
52 	resp_hdr->exp_cmdsn = cpu_to_be32(cqe_logout_response->exp_cmd_sn);
53 	resp_hdr->max_cmdsn = cpu_to_be32(cqe_logout_response->max_cmd_sn);
54 
55 	resp_hdr->t2wait = cpu_to_be32(cqe_logout_response->time_2_wait);
56 	resp_hdr->t2retain = cpu_to_be32(cqe_logout_response->time_2_retain);
57 
58 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_TID,
59 		  "Freeing tid=0x%x for cid=0x%x\n",
60 		  cmd->task_id, qedi_conn->iscsi_conn_id);
61 
62 	spin_lock(&qedi_conn->list_lock);
63 	if (likely(cmd->io_cmd_in_list)) {
64 		cmd->io_cmd_in_list = false;
65 		list_del_init(&cmd->io_cmd);
66 		qedi_conn->active_cmd_count--;
67 	} else {
68 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
69 			  "Active cmd list node already deleted, tid=0x%x, cid=0x%x, io_cmd_node=%p\n",
70 			  cmd->task_id, qedi_conn->iscsi_conn_id,
71 			  &cmd->io_cmd);
72 	}
73 	spin_unlock(&qedi_conn->list_lock);
74 
75 	cmd->state = RESPONSE_RECEIVED;
76 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr, NULL, 0);
77 
78 	spin_unlock(&session->back_lock);
79 }
80 
81 static void qedi_process_text_resp(struct qedi_ctx *qedi,
82 				   union iscsi_cqe *cqe,
83 				   struct iscsi_task *task,
84 				   struct qedi_conn *qedi_conn)
85 {
86 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
87 	struct iscsi_session *session = conn->session;
88 	struct iscsi_task_context *task_ctx;
89 	struct iscsi_text_rsp *resp_hdr_ptr;
90 	struct iscsi_text_response_hdr *cqe_text_response;
91 	struct qedi_cmd *cmd;
92 	int pld_len;
93 
94 	cmd = (struct qedi_cmd *)task->dd_data;
95 	task_ctx = qedi_get_task_mem(&qedi->tasks, cmd->task_id);
96 
97 	cqe_text_response = &cqe->cqe_common.iscsi_hdr.text_response;
98 	spin_lock(&session->back_lock);
99 	resp_hdr_ptr =  (struct iscsi_text_rsp *)&qedi_conn->gen_pdu.resp_hdr;
100 	memset(resp_hdr_ptr, 0, sizeof(struct iscsi_hdr));
101 	resp_hdr_ptr->opcode = cqe_text_response->opcode;
102 	resp_hdr_ptr->flags = cqe_text_response->flags;
103 	resp_hdr_ptr->hlength = 0;
104 
105 	hton24(resp_hdr_ptr->dlength,
106 	       (cqe_text_response->hdr_second_dword &
107 		ISCSI_TEXT_RESPONSE_HDR_DATA_SEG_LEN_MASK));
108 
109 	resp_hdr_ptr->itt = build_itt(cqe->cqe_solicited.itid,
110 				      conn->session->age);
111 	resp_hdr_ptr->ttt = cqe_text_response->ttt;
112 	resp_hdr_ptr->statsn = cpu_to_be32(cqe_text_response->stat_sn);
113 	resp_hdr_ptr->exp_cmdsn = cpu_to_be32(cqe_text_response->exp_cmd_sn);
114 	resp_hdr_ptr->max_cmdsn = cpu_to_be32(cqe_text_response->max_cmd_sn);
115 
116 	pld_len = cqe_text_response->hdr_second_dword &
117 		  ISCSI_TEXT_RESPONSE_HDR_DATA_SEG_LEN_MASK;
118 	qedi_conn->gen_pdu.resp_wr_ptr = qedi_conn->gen_pdu.resp_buf + pld_len;
119 
120 	memset(task_ctx, '\0', sizeof(*task_ctx));
121 
122 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_TID,
123 		  "Freeing tid=0x%x for cid=0x%x\n",
124 		  cmd->task_id, qedi_conn->iscsi_conn_id);
125 
126 	spin_lock(&qedi_conn->list_lock);
127 	if (likely(cmd->io_cmd_in_list)) {
128 		cmd->io_cmd_in_list = false;
129 		list_del_init(&cmd->io_cmd);
130 		qedi_conn->active_cmd_count--;
131 	} else {
132 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
133 			  "Active cmd list node already deleted, tid=0x%x, cid=0x%x, io_cmd_node=%p\n",
134 			  cmd->task_id, qedi_conn->iscsi_conn_id,
135 			  &cmd->io_cmd);
136 	}
137 	spin_unlock(&qedi_conn->list_lock);
138 
139 	cmd->state = RESPONSE_RECEIVED;
140 
141 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr_ptr,
142 			     qedi_conn->gen_pdu.resp_buf,
143 			     (qedi_conn->gen_pdu.resp_wr_ptr -
144 			      qedi_conn->gen_pdu.resp_buf));
145 	spin_unlock(&session->back_lock);
146 }
147 
148 static void qedi_tmf_resp_work(struct work_struct *work)
149 {
150 	struct qedi_cmd *qedi_cmd =
151 				container_of(work, struct qedi_cmd, tmf_work);
152 	struct qedi_conn *qedi_conn = qedi_cmd->conn;
153 	struct qedi_ctx *qedi = qedi_conn->qedi;
154 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
155 	struct iscsi_session *session = conn->session;
156 	struct iscsi_tm_rsp *resp_hdr_ptr;
157 	int rval = 0;
158 
159 	resp_hdr_ptr =  (struct iscsi_tm_rsp *)qedi_cmd->tmf_resp_buf;
160 
161 	rval = qedi_cleanup_all_io(qedi, qedi_conn, qedi_cmd->task, true);
162 	if (rval)
163 		goto exit_tmf_resp;
164 
165 	spin_lock(&session->back_lock);
166 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr_ptr, NULL, 0);
167 	spin_unlock(&session->back_lock);
168 
169 exit_tmf_resp:
170 	kfree(resp_hdr_ptr);
171 
172 	spin_lock(&qedi_conn->tmf_work_lock);
173 	qedi_conn->fw_cleanup_works--;
174 	spin_unlock(&qedi_conn->tmf_work_lock);
175 }
176 
177 static void qedi_process_tmf_resp(struct qedi_ctx *qedi,
178 				  union iscsi_cqe *cqe,
179 				  struct iscsi_task *task,
180 				  struct qedi_conn *qedi_conn)
181 
182 {
183 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
184 	struct iscsi_session *session = conn->session;
185 	struct iscsi_tmf_response_hdr *cqe_tmp_response;
186 	struct iscsi_tm_rsp *resp_hdr_ptr;
187 	struct iscsi_tm *tmf_hdr;
188 	struct qedi_cmd *qedi_cmd = NULL;
189 
190 	cqe_tmp_response = &cqe->cqe_common.iscsi_hdr.tmf_response;
191 
192 	qedi_cmd = task->dd_data;
193 	qedi_cmd->tmf_resp_buf = kzalloc(sizeof(*resp_hdr_ptr), GFP_ATOMIC);
194 	if (!qedi_cmd->tmf_resp_buf) {
195 		QEDI_ERR(&qedi->dbg_ctx,
196 			 "Failed to allocate resp buf, cid=0x%x\n",
197 			  qedi_conn->iscsi_conn_id);
198 		return;
199 	}
200 
201 	spin_lock(&session->back_lock);
202 	resp_hdr_ptr =  (struct iscsi_tm_rsp *)qedi_cmd->tmf_resp_buf;
203 	memset(resp_hdr_ptr, 0, sizeof(struct iscsi_tm_rsp));
204 
205 	/* Fill up the header */
206 	resp_hdr_ptr->opcode = cqe_tmp_response->opcode;
207 	resp_hdr_ptr->flags = cqe_tmp_response->hdr_flags;
208 	resp_hdr_ptr->response = cqe_tmp_response->hdr_response;
209 	resp_hdr_ptr->hlength = 0;
210 
211 	hton24(resp_hdr_ptr->dlength,
212 	       (cqe_tmp_response->hdr_second_dword &
213 		ISCSI_TMF_RESPONSE_HDR_DATA_SEG_LEN_MASK));
214 	resp_hdr_ptr->itt = build_itt(cqe->cqe_solicited.itid,
215 				      conn->session->age);
216 	resp_hdr_ptr->statsn = cpu_to_be32(cqe_tmp_response->stat_sn);
217 	resp_hdr_ptr->exp_cmdsn  = cpu_to_be32(cqe_tmp_response->exp_cmd_sn);
218 	resp_hdr_ptr->max_cmdsn = cpu_to_be32(cqe_tmp_response->max_cmd_sn);
219 
220 	tmf_hdr = (struct iscsi_tm *)qedi_cmd->task->hdr;
221 
222 	spin_lock(&qedi_conn->list_lock);
223 	if (likely(qedi_cmd->io_cmd_in_list)) {
224 		qedi_cmd->io_cmd_in_list = false;
225 		list_del_init(&qedi_cmd->io_cmd);
226 		qedi_conn->active_cmd_count--;
227 	}
228 	spin_unlock(&qedi_conn->list_lock);
229 
230 	spin_lock(&qedi_conn->tmf_work_lock);
231 	switch (tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) {
232 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
233 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
234 	case ISCSI_TM_FUNC_TARGET_COLD_RESET:
235 		if (qedi_conn->ep_disconnect_starting) {
236 			/* Session is down so ep_disconnect will clean up */
237 			spin_unlock(&qedi_conn->tmf_work_lock);
238 			goto unblock_sess;
239 		}
240 
241 		qedi_conn->fw_cleanup_works++;
242 		spin_unlock(&qedi_conn->tmf_work_lock);
243 
244 		INIT_WORK(&qedi_cmd->tmf_work, qedi_tmf_resp_work);
245 		queue_work(qedi->tmf_thread, &qedi_cmd->tmf_work);
246 		goto unblock_sess;
247 	}
248 	spin_unlock(&qedi_conn->tmf_work_lock);
249 
250 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr_ptr, NULL, 0);
251 	kfree(resp_hdr_ptr);
252 
253 unblock_sess:
254 	spin_unlock(&session->back_lock);
255 }
256 
257 static void qedi_process_login_resp(struct qedi_ctx *qedi,
258 				    union iscsi_cqe *cqe,
259 				    struct iscsi_task *task,
260 				    struct qedi_conn *qedi_conn)
261 {
262 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
263 	struct iscsi_session *session = conn->session;
264 	struct iscsi_task_context *task_ctx;
265 	struct iscsi_login_rsp *resp_hdr_ptr;
266 	struct iscsi_login_response_hdr *cqe_login_response;
267 	struct qedi_cmd *cmd;
268 	int pld_len;
269 
270 	cmd = (struct qedi_cmd *)task->dd_data;
271 
272 	cqe_login_response = &cqe->cqe_common.iscsi_hdr.login_response;
273 	task_ctx = qedi_get_task_mem(&qedi->tasks, cmd->task_id);
274 
275 	spin_lock(&session->back_lock);
276 	resp_hdr_ptr =  (struct iscsi_login_rsp *)&qedi_conn->gen_pdu.resp_hdr;
277 	memset(resp_hdr_ptr, 0, sizeof(struct iscsi_login_rsp));
278 	resp_hdr_ptr->opcode = cqe_login_response->opcode;
279 	resp_hdr_ptr->flags = cqe_login_response->flags_attr;
280 	resp_hdr_ptr->hlength = 0;
281 
282 	hton24(resp_hdr_ptr->dlength,
283 	       (cqe_login_response->hdr_second_dword &
284 		ISCSI_LOGIN_RESPONSE_HDR_DATA_SEG_LEN_MASK));
285 	resp_hdr_ptr->itt = build_itt(cqe->cqe_solicited.itid,
286 				      conn->session->age);
287 	resp_hdr_ptr->tsih = cqe_login_response->tsih;
288 	resp_hdr_ptr->statsn = cpu_to_be32(cqe_login_response->stat_sn);
289 	resp_hdr_ptr->exp_cmdsn = cpu_to_be32(cqe_login_response->exp_cmd_sn);
290 	resp_hdr_ptr->max_cmdsn = cpu_to_be32(cqe_login_response->max_cmd_sn);
291 	resp_hdr_ptr->status_class = cqe_login_response->status_class;
292 	resp_hdr_ptr->status_detail = cqe_login_response->status_detail;
293 	pld_len = cqe_login_response->hdr_second_dword &
294 		  ISCSI_LOGIN_RESPONSE_HDR_DATA_SEG_LEN_MASK;
295 	qedi_conn->gen_pdu.resp_wr_ptr = qedi_conn->gen_pdu.resp_buf + pld_len;
296 
297 	spin_lock(&qedi_conn->list_lock);
298 	if (likely(cmd->io_cmd_in_list)) {
299 		cmd->io_cmd_in_list = false;
300 		list_del_init(&cmd->io_cmd);
301 		qedi_conn->active_cmd_count--;
302 	}
303 	spin_unlock(&qedi_conn->list_lock);
304 
305 	memset(task_ctx, '\0', sizeof(*task_ctx));
306 
307 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr_ptr,
308 			     qedi_conn->gen_pdu.resp_buf,
309 			     (qedi_conn->gen_pdu.resp_wr_ptr -
310 			     qedi_conn->gen_pdu.resp_buf));
311 
312 	spin_unlock(&session->back_lock);
313 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_TID,
314 		  "Freeing tid=0x%x for cid=0x%x\n",
315 		  cmd->task_id, qedi_conn->iscsi_conn_id);
316 	cmd->state = RESPONSE_RECEIVED;
317 }
318 
319 static void qedi_get_rq_bdq_buf(struct qedi_ctx *qedi,
320 				struct iscsi_cqe_unsolicited *cqe,
321 				char *ptr, int len)
322 {
323 	u16 idx = 0;
324 
325 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
326 		  "pld_len [%d], bdq_prod_idx [%d], idx [%d]\n",
327 		  len, qedi->bdq_prod_idx,
328 		  (qedi->bdq_prod_idx % qedi->rq_num_entries));
329 
330 	/* Obtain buffer address from rqe_opaque */
331 	idx = cqe->rqe_opaque;
332 	if (idx > (QEDI_BDQ_NUM - 1)) {
333 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
334 			  "wrong idx %d returned by FW, dropping the unsolicited pkt\n",
335 			  idx);
336 		return;
337 	}
338 
339 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
340 		  "rqe_opaque [0x%p], idx [%d]\n", cqe->rqe_opaque, idx);
341 
342 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
343 		  "unsol_cqe_type = %d\n", cqe->unsol_cqe_type);
344 	switch (cqe->unsol_cqe_type) {
345 	case ISCSI_CQE_UNSOLICITED_SINGLE:
346 	case ISCSI_CQE_UNSOLICITED_FIRST:
347 		if (len)
348 			memcpy(ptr, (void *)qedi->bdq[idx].buf_addr, len);
349 		break;
350 	case ISCSI_CQE_UNSOLICITED_MIDDLE:
351 	case ISCSI_CQE_UNSOLICITED_LAST:
352 		break;
353 	default:
354 		break;
355 	}
356 }
357 
358 static void qedi_put_rq_bdq_buf(struct qedi_ctx *qedi,
359 				struct iscsi_cqe_unsolicited *cqe,
360 				int count)
361 {
362 	u16 idx = 0;
363 	struct scsi_bd *pbl;
364 
365 	/* Obtain buffer address from rqe_opaque */
366 	idx = cqe->rqe_opaque;
367 	if (idx > (QEDI_BDQ_NUM - 1)) {
368 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
369 			  "wrong idx %d returned by FW, dropping the unsolicited pkt\n",
370 			  idx);
371 		return;
372 	}
373 
374 	pbl = (struct scsi_bd *)qedi->bdq_pbl;
375 	pbl += (qedi->bdq_prod_idx % qedi->rq_num_entries);
376 	pbl->address.hi = cpu_to_le32(QEDI_U64_HI(qedi->bdq[idx].buf_dma));
377 	pbl->address.lo = cpu_to_le32(QEDI_U64_LO(qedi->bdq[idx].buf_dma));
378 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
379 		  "pbl [0x%p] pbl->address hi [0x%llx] lo [0x%llx] idx [%d]\n",
380 		  pbl, pbl->address.hi, pbl->address.lo, idx);
381 	pbl->opaque.iscsi_opaque.reserved_zero[0] = 0;
382 	pbl->opaque.iscsi_opaque.reserved_zero[1] = 0;
383 	pbl->opaque.iscsi_opaque.reserved_zero[2] = 0;
384 	pbl->opaque.iscsi_opaque.opaque = cpu_to_le32(idx);
385 
386 	/* Increment producer to let f/w know we've handled the frame */
387 	qedi->bdq_prod_idx += count;
388 
389 	writew(qedi->bdq_prod_idx, qedi->bdq_primary_prod);
390 	readw(qedi->bdq_primary_prod);
391 
392 	writew(qedi->bdq_prod_idx, qedi->bdq_secondary_prod);
393 	readw(qedi->bdq_secondary_prod);
394 }
395 
396 static void qedi_unsol_pdu_adjust_bdq(struct qedi_ctx *qedi,
397 				      struct iscsi_cqe_unsolicited *cqe,
398 				      u32 pdu_len, u32 num_bdqs,
399 				      char *bdq_data)
400 {
401 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
402 		  "num_bdqs [%d]\n", num_bdqs);
403 
404 	qedi_get_rq_bdq_buf(qedi, cqe, bdq_data, pdu_len);
405 	qedi_put_rq_bdq_buf(qedi, cqe, (num_bdqs + 1));
406 }
407 
408 static int qedi_process_nopin_mesg(struct qedi_ctx *qedi,
409 				   union iscsi_cqe *cqe,
410 				   struct iscsi_task *task,
411 				   struct qedi_conn *qedi_conn, u16 que_idx)
412 {
413 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
414 	struct iscsi_session *session = conn->session;
415 	struct iscsi_nop_in_hdr *cqe_nop_in;
416 	struct iscsi_nopin *hdr;
417 	struct qedi_cmd *cmd;
418 	int tgt_async_nop = 0;
419 	u32 lun[2];
420 	u32 pdu_len, num_bdqs;
421 	char bdq_data[QEDI_BDQ_BUF_SIZE];
422 	unsigned long flags;
423 
424 	spin_lock_bh(&session->back_lock);
425 	cqe_nop_in = &cqe->cqe_common.iscsi_hdr.nop_in;
426 
427 	pdu_len = cqe_nop_in->hdr_second_dword &
428 		  ISCSI_NOP_IN_HDR_DATA_SEG_LEN_MASK;
429 	num_bdqs = pdu_len / QEDI_BDQ_BUF_SIZE;
430 
431 	hdr = (struct iscsi_nopin *)&qedi_conn->gen_pdu.resp_hdr;
432 	memset(hdr, 0, sizeof(struct iscsi_hdr));
433 	hdr->opcode = cqe_nop_in->opcode;
434 	hdr->max_cmdsn = cpu_to_be32(cqe_nop_in->max_cmd_sn);
435 	hdr->exp_cmdsn = cpu_to_be32(cqe_nop_in->exp_cmd_sn);
436 	hdr->statsn = cpu_to_be32(cqe_nop_in->stat_sn);
437 	hdr->ttt = cpu_to_be32(cqe_nop_in->ttt);
438 
439 	if (cqe->cqe_common.cqe_type == ISCSI_CQE_TYPE_UNSOLICITED) {
440 		spin_lock_irqsave(&qedi->hba_lock, flags);
441 		qedi_unsol_pdu_adjust_bdq(qedi, &cqe->cqe_unsolicited,
442 					  pdu_len, num_bdqs, bdq_data);
443 		hdr->itt = RESERVED_ITT;
444 		tgt_async_nop = 1;
445 		spin_unlock_irqrestore(&qedi->hba_lock, flags);
446 		goto done;
447 	}
448 
449 	/* Response to one of our nop-outs */
450 	if (task) {
451 		cmd = task->dd_data;
452 		hdr->flags = ISCSI_FLAG_CMD_FINAL;
453 		hdr->itt = build_itt(cqe->cqe_solicited.itid,
454 				     conn->session->age);
455 		lun[0] = 0xffffffff;
456 		lun[1] = 0xffffffff;
457 		memcpy(&hdr->lun, lun, sizeof(struct scsi_lun));
458 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_TID,
459 			  "Freeing tid=0x%x for cid=0x%x\n",
460 			  cmd->task_id, qedi_conn->iscsi_conn_id);
461 		cmd->state = RESPONSE_RECEIVED;
462 		spin_lock(&qedi_conn->list_lock);
463 		if (likely(cmd->io_cmd_in_list)) {
464 			cmd->io_cmd_in_list = false;
465 			list_del_init(&cmd->io_cmd);
466 			qedi_conn->active_cmd_count--;
467 		}
468 
469 		spin_unlock(&qedi_conn->list_lock);
470 	}
471 
472 done:
473 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, bdq_data, pdu_len);
474 
475 	spin_unlock_bh(&session->back_lock);
476 	return tgt_async_nop;
477 }
478 
479 static void qedi_process_async_mesg(struct qedi_ctx *qedi,
480 				    union iscsi_cqe *cqe,
481 				    struct iscsi_task *task,
482 				    struct qedi_conn *qedi_conn,
483 				    u16 que_idx)
484 {
485 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
486 	struct iscsi_session *session = conn->session;
487 	struct iscsi_async_msg_hdr *cqe_async_msg;
488 	struct iscsi_async *resp_hdr;
489 	u32 lun[2];
490 	u32 pdu_len, num_bdqs;
491 	char bdq_data[QEDI_BDQ_BUF_SIZE];
492 	unsigned long flags;
493 
494 	spin_lock_bh(&session->back_lock);
495 
496 	cqe_async_msg = &cqe->cqe_common.iscsi_hdr.async_msg;
497 	pdu_len = cqe_async_msg->hdr_second_dword &
498 		ISCSI_ASYNC_MSG_HDR_DATA_SEG_LEN_MASK;
499 	num_bdqs = pdu_len / QEDI_BDQ_BUF_SIZE;
500 
501 	if (cqe->cqe_common.cqe_type == ISCSI_CQE_TYPE_UNSOLICITED) {
502 		spin_lock_irqsave(&qedi->hba_lock, flags);
503 		qedi_unsol_pdu_adjust_bdq(qedi, &cqe->cqe_unsolicited,
504 					  pdu_len, num_bdqs, bdq_data);
505 		spin_unlock_irqrestore(&qedi->hba_lock, flags);
506 	}
507 
508 	resp_hdr = (struct iscsi_async *)&qedi_conn->gen_pdu.resp_hdr;
509 	memset(resp_hdr, 0, sizeof(struct iscsi_hdr));
510 	resp_hdr->opcode = cqe_async_msg->opcode;
511 	resp_hdr->flags = 0x80;
512 
513 	lun[0] = cpu_to_be32(cqe_async_msg->lun.lo);
514 	lun[1] = cpu_to_be32(cqe_async_msg->lun.hi);
515 	memcpy(&resp_hdr->lun, lun, sizeof(struct scsi_lun));
516 	resp_hdr->exp_cmdsn = cpu_to_be32(cqe_async_msg->exp_cmd_sn);
517 	resp_hdr->max_cmdsn = cpu_to_be32(cqe_async_msg->max_cmd_sn);
518 	resp_hdr->statsn = cpu_to_be32(cqe_async_msg->stat_sn);
519 
520 	resp_hdr->async_event = cqe_async_msg->async_event;
521 	resp_hdr->async_vcode = cqe_async_msg->async_vcode;
522 
523 	resp_hdr->param1 = cpu_to_be16(cqe_async_msg->param1_rsrv);
524 	resp_hdr->param2 = cpu_to_be16(cqe_async_msg->param2_rsrv);
525 	resp_hdr->param3 = cpu_to_be16(cqe_async_msg->param3_rsrv);
526 
527 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr, bdq_data,
528 			     pdu_len);
529 
530 	spin_unlock_bh(&session->back_lock);
531 }
532 
533 static void qedi_process_reject_mesg(struct qedi_ctx *qedi,
534 				     union iscsi_cqe *cqe,
535 				     struct iscsi_task *task,
536 				     struct qedi_conn *qedi_conn,
537 				     uint16_t que_idx)
538 {
539 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
540 	struct iscsi_session *session = conn->session;
541 	struct iscsi_reject_hdr *cqe_reject;
542 	struct iscsi_reject *hdr;
543 	u32 pld_len, num_bdqs;
544 	unsigned long flags;
545 
546 	spin_lock_bh(&session->back_lock);
547 	cqe_reject = &cqe->cqe_common.iscsi_hdr.reject;
548 	pld_len = cqe_reject->hdr_second_dword &
549 		  ISCSI_REJECT_HDR_DATA_SEG_LEN_MASK;
550 	num_bdqs = pld_len / QEDI_BDQ_BUF_SIZE;
551 
552 	if (cqe->cqe_common.cqe_type == ISCSI_CQE_TYPE_UNSOLICITED) {
553 		spin_lock_irqsave(&qedi->hba_lock, flags);
554 		qedi_unsol_pdu_adjust_bdq(qedi, &cqe->cqe_unsolicited,
555 					  pld_len, num_bdqs, conn->data);
556 		spin_unlock_irqrestore(&qedi->hba_lock, flags);
557 	}
558 	hdr = (struct iscsi_reject *)&qedi_conn->gen_pdu.resp_hdr;
559 	memset(hdr, 0, sizeof(struct iscsi_hdr));
560 	hdr->opcode = cqe_reject->opcode;
561 	hdr->reason = cqe_reject->hdr_reason;
562 	hdr->flags = cqe_reject->hdr_flags;
563 	hton24(hdr->dlength, (cqe_reject->hdr_second_dword &
564 			      ISCSI_REJECT_HDR_DATA_SEG_LEN_MASK));
565 	hdr->max_cmdsn = cpu_to_be32(cqe_reject->max_cmd_sn);
566 	hdr->exp_cmdsn = cpu_to_be32(cqe_reject->exp_cmd_sn);
567 	hdr->statsn = cpu_to_be32(cqe_reject->stat_sn);
568 	hdr->ffffffff = cpu_to_be32(0xffffffff);
569 
570 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr,
571 			     conn->data, pld_len);
572 	spin_unlock_bh(&session->back_lock);
573 }
574 
575 static void qedi_scsi_completion(struct qedi_ctx *qedi,
576 				 union iscsi_cqe *cqe,
577 				 struct iscsi_task *task,
578 				 struct iscsi_conn *conn)
579 {
580 	struct scsi_cmnd *sc_cmd;
581 	struct qedi_cmd *cmd = task->dd_data;
582 	struct iscsi_session *session = conn->session;
583 	struct iscsi_scsi_rsp *hdr;
584 	struct iscsi_data_in_hdr *cqe_data_in;
585 	int datalen = 0;
586 	struct qedi_conn *qedi_conn;
587 	u32 iscsi_cid;
588 	u8 cqe_err_bits = 0;
589 
590 	iscsi_cid  = cqe->cqe_common.conn_id;
591 	qedi_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid];
592 
593 	cqe_data_in = &cqe->cqe_common.iscsi_hdr.data_in;
594 	cqe_err_bits =
595 		cqe->cqe_common.error_bitmap.error_bits.cqe_error_status_bits;
596 
597 	spin_lock_bh(&session->back_lock);
598 	/* get the scsi command */
599 	sc_cmd = cmd->scsi_cmd;
600 
601 	if (!sc_cmd) {
602 		QEDI_WARN(&qedi->dbg_ctx, "sc_cmd is NULL!\n");
603 		goto error;
604 	}
605 
606 	if (!sc_cmd->SCp.ptr) {
607 		QEDI_WARN(&qedi->dbg_ctx,
608 			  "SCp.ptr is NULL, returned in another context.\n");
609 		goto error;
610 	}
611 
612 	if (!scsi_cmd_to_rq(sc_cmd)->q) {
613 		QEDI_WARN(&qedi->dbg_ctx,
614 			  "request->q is NULL so request is not valid, sc_cmd=%p.\n",
615 			  sc_cmd);
616 		goto error;
617 	}
618 
619 	qedi_iscsi_unmap_sg_list(cmd);
620 
621 	hdr = (struct iscsi_scsi_rsp *)task->hdr;
622 	hdr->opcode = cqe_data_in->opcode;
623 	hdr->max_cmdsn = cpu_to_be32(cqe_data_in->max_cmd_sn);
624 	hdr->exp_cmdsn = cpu_to_be32(cqe_data_in->exp_cmd_sn);
625 	hdr->itt = build_itt(cqe->cqe_solicited.itid, conn->session->age);
626 	hdr->response = cqe_data_in->reserved1;
627 	hdr->cmd_status = cqe_data_in->status_rsvd;
628 	hdr->flags = cqe_data_in->flags;
629 	hdr->residual_count = cpu_to_be32(cqe_data_in->residual_count);
630 
631 	if (hdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
632 		datalen = cqe_data_in->reserved2 &
633 			  ISCSI_COMMON_HDR_DATA_SEG_LEN_MASK;
634 		memcpy((char *)conn->data, (char *)cmd->sense_buffer, datalen);
635 	}
636 
637 	/* If f/w reports data underrun err then set residual to IO transfer
638 	 * length, set Underrun flag and clear Overrun flag explicitly
639 	 */
640 	if (unlikely(cqe_err_bits &&
641 		     GET_FIELD(cqe_err_bits, CQE_ERROR_BITMAP_UNDER_RUN_ERR))) {
642 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
643 			  "Under flow itt=0x%x proto flags=0x%x tid=0x%x cid 0x%x fw resid 0x%x sc dlen 0x%x\n",
644 			  hdr->itt, cqe_data_in->flags, cmd->task_id,
645 			  qedi_conn->iscsi_conn_id, hdr->residual_count,
646 			  scsi_bufflen(sc_cmd));
647 		hdr->residual_count = cpu_to_be32(scsi_bufflen(sc_cmd));
648 		hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
649 		hdr->flags &= (~ISCSI_FLAG_CMD_OVERFLOW);
650 	}
651 
652 	spin_lock(&qedi_conn->list_lock);
653 	if (likely(cmd->io_cmd_in_list)) {
654 		cmd->io_cmd_in_list = false;
655 		list_del_init(&cmd->io_cmd);
656 		qedi_conn->active_cmd_count--;
657 	}
658 	spin_unlock(&qedi_conn->list_lock);
659 
660 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_TID,
661 		  "Freeing tid=0x%x for cid=0x%x\n",
662 		  cmd->task_id, qedi_conn->iscsi_conn_id);
663 	cmd->state = RESPONSE_RECEIVED;
664 	if (qedi_io_tracing)
665 		qedi_trace_io(qedi, task, cmd->task_id, QEDI_IO_TRACE_RSP);
666 
667 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr,
668 			     conn->data, datalen);
669 error:
670 	spin_unlock_bh(&session->back_lock);
671 }
672 
673 static void qedi_mtask_completion(struct qedi_ctx *qedi,
674 				  union iscsi_cqe *cqe,
675 				  struct iscsi_task *task,
676 				  struct qedi_conn *conn, uint16_t que_idx)
677 {
678 	struct iscsi_conn *iscsi_conn;
679 	u32 hdr_opcode;
680 
681 	hdr_opcode = cqe->cqe_common.iscsi_hdr.common.hdr_first_byte;
682 	iscsi_conn = conn->cls_conn->dd_data;
683 
684 	switch (hdr_opcode) {
685 	case ISCSI_OPCODE_SCSI_RESPONSE:
686 	case ISCSI_OPCODE_DATA_IN:
687 		qedi_scsi_completion(qedi, cqe, task, iscsi_conn);
688 		break;
689 	case ISCSI_OPCODE_LOGIN_RESPONSE:
690 		qedi_process_login_resp(qedi, cqe, task, conn);
691 		break;
692 	case ISCSI_OPCODE_TMF_RESPONSE:
693 		qedi_process_tmf_resp(qedi, cqe, task, conn);
694 		break;
695 	case ISCSI_OPCODE_TEXT_RESPONSE:
696 		qedi_process_text_resp(qedi, cqe, task, conn);
697 		break;
698 	case ISCSI_OPCODE_LOGOUT_RESPONSE:
699 		qedi_process_logout_resp(qedi, cqe, task, conn);
700 		break;
701 	case ISCSI_OPCODE_NOP_IN:
702 		qedi_process_nopin_mesg(qedi, cqe, task, conn, que_idx);
703 		break;
704 	default:
705 		QEDI_ERR(&qedi->dbg_ctx, "unknown opcode\n");
706 	}
707 }
708 
709 static void qedi_process_nopin_local_cmpl(struct qedi_ctx *qedi,
710 					  struct iscsi_cqe_solicited *cqe,
711 					  struct iscsi_task *task,
712 					  struct qedi_conn *qedi_conn)
713 {
714 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
715 	struct iscsi_session *session = conn->session;
716 	struct qedi_cmd *cmd = task->dd_data;
717 
718 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_UNSOL,
719 		  "itid=0x%x, cmd task id=0x%x\n",
720 		  cqe->itid, cmd->task_id);
721 
722 	cmd->state = RESPONSE_RECEIVED;
723 
724 	spin_lock_bh(&session->back_lock);
725 	__iscsi_put_task(task);
726 	spin_unlock_bh(&session->back_lock);
727 }
728 
729 static void qedi_process_cmd_cleanup_resp(struct qedi_ctx *qedi,
730 					  struct iscsi_cqe_solicited *cqe,
731 					  struct iscsi_conn *conn)
732 {
733 	struct qedi_work_map *work, *work_tmp;
734 	u32 proto_itt = cqe->itid;
735 	int found = 0;
736 	struct qedi_cmd *qedi_cmd = NULL;
737 	u32 iscsi_cid;
738 	struct qedi_conn *qedi_conn;
739 	struct qedi_cmd *dbg_cmd;
740 	struct iscsi_task *mtask, *task;
741 	struct iscsi_tm *tmf_hdr = NULL;
742 
743 	iscsi_cid = cqe->conn_id;
744 	qedi_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid];
745 	if (!qedi_conn) {
746 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
747 			  "icid not found 0x%x\n", cqe->conn_id);
748 		return;
749 	}
750 
751 	/* Based on this itt get the corresponding qedi_cmd */
752 	spin_lock_bh(&qedi_conn->tmf_work_lock);
753 	list_for_each_entry_safe(work, work_tmp, &qedi_conn->tmf_work_list,
754 				 list) {
755 		if (work->rtid == proto_itt) {
756 			/* We found the command */
757 			qedi_cmd = work->qedi_cmd;
758 			if (!qedi_cmd->list_tmf_work) {
759 				QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
760 					  "TMF work not found, cqe->tid=0x%x, cid=0x%x\n",
761 					  proto_itt, qedi_conn->iscsi_conn_id);
762 				WARN_ON(1);
763 			}
764 			found = 1;
765 			mtask = qedi_cmd->task;
766 			task = work->ctask;
767 			tmf_hdr = (struct iscsi_tm *)mtask->hdr;
768 
769 			list_del_init(&work->list);
770 			kfree(work);
771 			qedi_cmd->list_tmf_work = NULL;
772 		}
773 	}
774 
775 	if (!found) {
776 		spin_unlock_bh(&qedi_conn->tmf_work_lock);
777 		goto check_cleanup_reqs;
778 	}
779 
780 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
781 		  "TMF work, cqe->tid=0x%x, tmf flags=0x%x, cid=0x%x\n",
782 		  proto_itt, tmf_hdr->flags, qedi_conn->iscsi_conn_id);
783 
784 	spin_lock_bh(&conn->session->back_lock);
785 	if (iscsi_task_is_completed(task)) {
786 		QEDI_NOTICE(&qedi->dbg_ctx,
787 			    "IO task completed, tmf rtt=0x%x, cid=0x%x\n",
788 			   get_itt(tmf_hdr->rtt), qedi_conn->iscsi_conn_id);
789 		goto unlock;
790 	}
791 
792 	dbg_cmd = task->dd_data;
793 
794 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
795 		  "Abort tmf rtt=0x%x, i/o itt=0x%x, i/o tid=0x%x, cid=0x%x\n",
796 		  get_itt(tmf_hdr->rtt), get_itt(task->itt), dbg_cmd->task_id,
797 		  qedi_conn->iscsi_conn_id);
798 
799 	spin_lock(&qedi_conn->list_lock);
800 	if (likely(dbg_cmd->io_cmd_in_list)) {
801 		dbg_cmd->io_cmd_in_list = false;
802 		list_del_init(&dbg_cmd->io_cmd);
803 		qedi_conn->active_cmd_count--;
804 	}
805 	spin_unlock(&qedi_conn->list_lock);
806 	qedi_cmd->state = CLEANUP_RECV;
807 unlock:
808 	spin_unlock_bh(&conn->session->back_lock);
809 	spin_unlock_bh(&qedi_conn->tmf_work_lock);
810 	wake_up_interruptible(&qedi_conn->wait_queue);
811 	return;
812 
813 check_cleanup_reqs:
814 	if (atomic_inc_return(&qedi_conn->cmd_cleanup_cmpl) ==
815 	    qedi_conn->cmd_cleanup_req) {
816 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
817 			  "Freeing tid=0x%x for cid=0x%x\n",
818 			  cqe->itid, qedi_conn->iscsi_conn_id);
819 		wake_up(&qedi_conn->wait_queue);
820 	}
821 }
822 
823 void qedi_fp_process_cqes(struct qedi_work *work)
824 {
825 	struct qedi_ctx *qedi = work->qedi;
826 	union iscsi_cqe *cqe = &work->cqe;
827 	struct iscsi_task *task = NULL;
828 	struct iscsi_nopout *nopout_hdr;
829 	struct qedi_conn *q_conn;
830 	struct iscsi_conn *conn;
831 	struct qedi_cmd *qedi_cmd;
832 	u32 comp_type;
833 	u32 iscsi_cid;
834 	u32 hdr_opcode;
835 	u16 que_idx = work->que_idx;
836 	u8 cqe_err_bits = 0;
837 
838 	comp_type = cqe->cqe_common.cqe_type;
839 	hdr_opcode = cqe->cqe_common.iscsi_hdr.common.hdr_first_byte;
840 	cqe_err_bits =
841 		cqe->cqe_common.error_bitmap.error_bits.cqe_error_status_bits;
842 
843 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
844 		  "fw_cid=0x%x, cqe type=0x%x, opcode=0x%x\n",
845 		  cqe->cqe_common.conn_id, comp_type, hdr_opcode);
846 
847 	if (comp_type >= MAX_ISCSI_CQES_TYPE) {
848 		QEDI_WARN(&qedi->dbg_ctx, "Invalid CqE type\n");
849 		return;
850 	}
851 
852 	iscsi_cid  = cqe->cqe_common.conn_id;
853 	q_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid];
854 	if (!q_conn) {
855 		QEDI_WARN(&qedi->dbg_ctx,
856 			  "Session no longer exists for cid=0x%x!!\n",
857 			  iscsi_cid);
858 		return;
859 	}
860 
861 	conn = q_conn->cls_conn->dd_data;
862 
863 	if (unlikely(cqe_err_bits &&
864 		     GET_FIELD(cqe_err_bits,
865 			       CQE_ERROR_BITMAP_DATA_DIGEST_ERR))) {
866 		iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
867 		return;
868 	}
869 
870 	switch (comp_type) {
871 	case ISCSI_CQE_TYPE_SOLICITED:
872 	case ISCSI_CQE_TYPE_SOLICITED_WITH_SENSE:
873 		qedi_cmd = container_of(work, struct qedi_cmd, cqe_work);
874 		task = qedi_cmd->task;
875 		if (!task) {
876 			QEDI_WARN(&qedi->dbg_ctx, "task is NULL\n");
877 			return;
878 		}
879 
880 		/* Process NOPIN local completion */
881 		nopout_hdr = (struct iscsi_nopout *)task->hdr;
882 		if ((nopout_hdr->itt == RESERVED_ITT) &&
883 		    (cqe->cqe_solicited.itid != (u16)RESERVED_ITT)) {
884 			qedi_process_nopin_local_cmpl(qedi, &cqe->cqe_solicited,
885 						      task, q_conn);
886 		} else {
887 			cqe->cqe_solicited.itid =
888 					       qedi_get_itt(cqe->cqe_solicited);
889 			/* Process other solicited responses */
890 			qedi_mtask_completion(qedi, cqe, task, q_conn, que_idx);
891 		}
892 		break;
893 	case ISCSI_CQE_TYPE_UNSOLICITED:
894 		switch (hdr_opcode) {
895 		case ISCSI_OPCODE_NOP_IN:
896 			qedi_process_nopin_mesg(qedi, cqe, task, q_conn,
897 						que_idx);
898 			break;
899 		case ISCSI_OPCODE_ASYNC_MSG:
900 			qedi_process_async_mesg(qedi, cqe, task, q_conn,
901 						que_idx);
902 			break;
903 		case ISCSI_OPCODE_REJECT:
904 			qedi_process_reject_mesg(qedi, cqe, task, q_conn,
905 						 que_idx);
906 			break;
907 		}
908 		goto exit_fp_process;
909 	case ISCSI_CQE_TYPE_DUMMY:
910 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM, "Dummy CqE\n");
911 		goto exit_fp_process;
912 	case ISCSI_CQE_TYPE_TASK_CLEANUP:
913 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM, "CleanUp CqE\n");
914 		qedi_process_cmd_cleanup_resp(qedi, &cqe->cqe_solicited, conn);
915 		goto exit_fp_process;
916 	default:
917 		QEDI_ERR(&qedi->dbg_ctx, "Error cqe.\n");
918 		break;
919 	}
920 
921 exit_fp_process:
922 	return;
923 }
924 
925 static void qedi_ring_doorbell(struct qedi_conn *qedi_conn)
926 {
927 	qedi_conn->ep->db_data.sq_prod = qedi_conn->ep->fw_sq_prod_idx;
928 
929 	/* wmb - Make sure fw idx is coherent */
930 	wmb();
931 	writel(*(u32 *)&qedi_conn->ep->db_data, qedi_conn->ep->p_doorbell);
932 
933 	/* Make sure fw write idx is coherent, and include both memory barriers
934 	 * as a failsafe as for some architectures the call is the same but on
935 	 * others they are two different assembly operations.
936 	 */
937 	wmb();
938 	QEDI_INFO(&qedi_conn->qedi->dbg_ctx, QEDI_LOG_MP_REQ,
939 		  "prod_idx=0x%x, fw_prod_idx=0x%x, cid=0x%x\n",
940 		  qedi_conn->ep->sq_prod_idx, qedi_conn->ep->fw_sq_prod_idx,
941 		  qedi_conn->iscsi_conn_id);
942 }
943 
944 static u16 qedi_get_wqe_idx(struct qedi_conn *qedi_conn)
945 {
946 	struct qedi_endpoint *ep;
947 	u16 rval;
948 
949 	ep = qedi_conn->ep;
950 	rval = ep->sq_prod_idx;
951 
952 	/* Increament SQ index */
953 	ep->sq_prod_idx++;
954 	ep->fw_sq_prod_idx++;
955 	if (ep->sq_prod_idx == QEDI_SQ_SIZE)
956 		ep->sq_prod_idx = 0;
957 
958 	return rval;
959 }
960 
961 int qedi_send_iscsi_login(struct qedi_conn *qedi_conn,
962 			  struct iscsi_task *task)
963 {
964 	struct iscsi_login_req_hdr login_req_pdu_header;
965 	struct scsi_sgl_task_params tx_sgl_task_params;
966 	struct scsi_sgl_task_params rx_sgl_task_params;
967 	struct iscsi_task_params task_params;
968 	struct iscsi_task_context *fw_task_ctx;
969 	struct qedi_ctx *qedi = qedi_conn->qedi;
970 	struct iscsi_login_req *login_hdr;
971 	struct scsi_sge *resp_sge = NULL;
972 	struct qedi_cmd *qedi_cmd;
973 	struct qedi_endpoint *ep;
974 	s16 tid = 0;
975 	u16 sq_idx = 0;
976 	int rval = 0;
977 
978 	resp_sge = (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
979 	qedi_cmd = (struct qedi_cmd *)task->dd_data;
980 	ep = qedi_conn->ep;
981 	login_hdr = (struct iscsi_login_req *)task->hdr;
982 
983 	tid = qedi_get_task_idx(qedi);
984 	if (tid == -1)
985 		return -ENOMEM;
986 
987 	fw_task_ctx =
988 	     (struct iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
989 							       tid);
990 	memset(fw_task_ctx, 0, sizeof(struct iscsi_task_context));
991 
992 	qedi_cmd->task_id = tid;
993 
994 	memset(&task_params, 0, sizeof(task_params));
995 	memset(&login_req_pdu_header, 0, sizeof(login_req_pdu_header));
996 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
997 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
998 	/* Update header info */
999 	login_req_pdu_header.opcode = login_hdr->opcode;
1000 	login_req_pdu_header.version_min = login_hdr->min_version;
1001 	login_req_pdu_header.version_max = login_hdr->max_version;
1002 	login_req_pdu_header.flags_attr = login_hdr->flags;
1003 	login_req_pdu_header.isid_tabc = swab32p((u32 *)login_hdr->isid);
1004 	login_req_pdu_header.isid_d = swab16p((u16 *)&login_hdr->isid[4]);
1005 
1006 	login_req_pdu_header.tsih = login_hdr->tsih;
1007 	login_req_pdu_header.hdr_second_dword = ntoh24(login_hdr->dlength);
1008 
1009 	qedi_update_itt_map(qedi, tid, task->itt, qedi_cmd);
1010 	login_req_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
1011 	login_req_pdu_header.cid = qedi_conn->iscsi_conn_id;
1012 	login_req_pdu_header.cmd_sn = be32_to_cpu(login_hdr->cmdsn);
1013 	login_req_pdu_header.exp_stat_sn = be32_to_cpu(login_hdr->exp_statsn);
1014 	login_req_pdu_header.exp_stat_sn = 0;
1015 
1016 	/* Fill tx AHS and rx buffer */
1017 	tx_sgl_task_params.sgl =
1018 			       (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
1019 	tx_sgl_task_params.sgl_phys_addr.lo =
1020 					 (u32)(qedi_conn->gen_pdu.req_dma_addr);
1021 	tx_sgl_task_params.sgl_phys_addr.hi =
1022 			      (u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
1023 	tx_sgl_task_params.total_buffer_size = ntoh24(login_hdr->dlength);
1024 	tx_sgl_task_params.num_sges = 1;
1025 
1026 	rx_sgl_task_params.sgl =
1027 			      (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1028 	rx_sgl_task_params.sgl_phys_addr.lo =
1029 					(u32)(qedi_conn->gen_pdu.resp_dma_addr);
1030 	rx_sgl_task_params.sgl_phys_addr.hi =
1031 			     (u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
1032 	rx_sgl_task_params.total_buffer_size = resp_sge->sge_len;
1033 	rx_sgl_task_params.num_sges = 1;
1034 
1035 	/* Fill fw input params */
1036 	task_params.context = fw_task_ctx;
1037 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1038 	task_params.itid = tid;
1039 	task_params.cq_rss_number = 0;
1040 	task_params.tx_io_size = ntoh24(login_hdr->dlength);
1041 	task_params.rx_io_size = resp_sge->sge_len;
1042 
1043 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1044 	task_params.sqe = &ep->sq[sq_idx];
1045 
1046 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1047 	rval = init_initiator_login_request_task(&task_params,
1048 						 &login_req_pdu_header,
1049 						 &tx_sgl_task_params,
1050 						 &rx_sgl_task_params);
1051 	if (rval)
1052 		return -1;
1053 
1054 	spin_lock(&qedi_conn->list_lock);
1055 	list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1056 	qedi_cmd->io_cmd_in_list = true;
1057 	qedi_conn->active_cmd_count++;
1058 	spin_unlock(&qedi_conn->list_lock);
1059 
1060 	qedi_ring_doorbell(qedi_conn);
1061 	return 0;
1062 }
1063 
1064 int qedi_send_iscsi_logout(struct qedi_conn *qedi_conn,
1065 			   struct iscsi_task *task)
1066 {
1067 	struct iscsi_logout_req_hdr logout_pdu_header;
1068 	struct scsi_sgl_task_params tx_sgl_task_params;
1069 	struct scsi_sgl_task_params rx_sgl_task_params;
1070 	struct iscsi_task_params task_params;
1071 	struct iscsi_task_context *fw_task_ctx;
1072 	struct iscsi_logout *logout_hdr = NULL;
1073 	struct qedi_ctx *qedi = qedi_conn->qedi;
1074 	struct qedi_cmd *qedi_cmd;
1075 	struct qedi_endpoint *ep;
1076 	s16 tid = 0;
1077 	u16 sq_idx = 0;
1078 	int rval = 0;
1079 
1080 	qedi_cmd = (struct qedi_cmd *)task->dd_data;
1081 	logout_hdr = (struct iscsi_logout *)task->hdr;
1082 	ep = qedi_conn->ep;
1083 
1084 	tid = qedi_get_task_idx(qedi);
1085 	if (tid == -1)
1086 		return -ENOMEM;
1087 
1088 	fw_task_ctx =
1089 	     (struct iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1090 							       tid);
1091 	memset(fw_task_ctx, 0, sizeof(struct iscsi_task_context));
1092 
1093 	qedi_cmd->task_id = tid;
1094 
1095 	memset(&task_params, 0, sizeof(task_params));
1096 	memset(&logout_pdu_header, 0, sizeof(logout_pdu_header));
1097 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
1098 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
1099 
1100 	/* Update header info */
1101 	logout_pdu_header.opcode = logout_hdr->opcode;
1102 	logout_pdu_header.reason_code = 0x80 | logout_hdr->flags;
1103 	qedi_update_itt_map(qedi, tid, task->itt, qedi_cmd);
1104 	logout_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
1105 	logout_pdu_header.exp_stat_sn = be32_to_cpu(logout_hdr->exp_statsn);
1106 	logout_pdu_header.cmd_sn = be32_to_cpu(logout_hdr->cmdsn);
1107 	logout_pdu_header.cid = qedi_conn->iscsi_conn_id;
1108 
1109 	/* Fill fw input params */
1110 	task_params.context = fw_task_ctx;
1111 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1112 	task_params.itid = tid;
1113 	task_params.cq_rss_number = 0;
1114 	task_params.tx_io_size = 0;
1115 	task_params.rx_io_size = 0;
1116 
1117 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1118 	task_params.sqe = &ep->sq[sq_idx];
1119 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1120 
1121 	rval = init_initiator_logout_request_task(&task_params,
1122 						  &logout_pdu_header,
1123 						  NULL, NULL);
1124 	if (rval)
1125 		return -1;
1126 
1127 	spin_lock(&qedi_conn->list_lock);
1128 	list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1129 	qedi_cmd->io_cmd_in_list = true;
1130 	qedi_conn->active_cmd_count++;
1131 	spin_unlock(&qedi_conn->list_lock);
1132 
1133 	qedi_ring_doorbell(qedi_conn);
1134 	return 0;
1135 }
1136 
1137 int qedi_cleanup_all_io(struct qedi_ctx *qedi, struct qedi_conn *qedi_conn,
1138 			struct iscsi_task *task, bool in_recovery)
1139 {
1140 	int rval;
1141 	struct iscsi_task *ctask;
1142 	struct qedi_cmd *cmd, *cmd_tmp;
1143 	struct iscsi_tm *tmf_hdr;
1144 	unsigned int lun = 0;
1145 	bool lun_reset = false;
1146 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
1147 	struct iscsi_session *session = conn->session;
1148 
1149 	/* From recovery, task is NULL or from tmf resp valid task */
1150 	if (task) {
1151 		tmf_hdr = (struct iscsi_tm *)task->hdr;
1152 
1153 		if ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1154 			ISCSI_TM_FUNC_LOGICAL_UNIT_RESET) {
1155 			lun_reset = true;
1156 			lun = scsilun_to_int(&tmf_hdr->lun);
1157 		}
1158 	}
1159 
1160 	qedi_conn->cmd_cleanup_req = 0;
1161 	atomic_set(&qedi_conn->cmd_cleanup_cmpl, 0);
1162 
1163 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1164 		  "active_cmd_count=%d, cid=0x%x, in_recovery=%d, lun_reset=%d\n",
1165 		  qedi_conn->active_cmd_count, qedi_conn->iscsi_conn_id,
1166 		  in_recovery, lun_reset);
1167 
1168 	if (lun_reset)
1169 		spin_lock_bh(&session->back_lock);
1170 
1171 	spin_lock(&qedi_conn->list_lock);
1172 
1173 	list_for_each_entry_safe(cmd, cmd_tmp, &qedi_conn->active_cmd_list,
1174 				 io_cmd) {
1175 		ctask = cmd->task;
1176 		if (ctask == task)
1177 			continue;
1178 
1179 		if (lun_reset) {
1180 			if (cmd->scsi_cmd && cmd->scsi_cmd->device) {
1181 				QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1182 					  "tid=0x%x itt=0x%x scsi_cmd_ptr=%p device=%p task_state=%d cmd_state=0%x cid=0x%x\n",
1183 					  cmd->task_id, get_itt(ctask->itt),
1184 					  cmd->scsi_cmd, cmd->scsi_cmd->device,
1185 					  ctask->state, cmd->state,
1186 					  qedi_conn->iscsi_conn_id);
1187 				if (cmd->scsi_cmd->device->lun != lun)
1188 					continue;
1189 			}
1190 		}
1191 		qedi_conn->cmd_cleanup_req++;
1192 		qedi_iscsi_cleanup_task(ctask, true);
1193 
1194 		cmd->io_cmd_in_list = false;
1195 		list_del_init(&cmd->io_cmd);
1196 		qedi_conn->active_cmd_count--;
1197 		QEDI_WARN(&qedi->dbg_ctx,
1198 			  "Deleted active cmd list node io_cmd=%p, cid=0x%x\n",
1199 			  &cmd->io_cmd, qedi_conn->iscsi_conn_id);
1200 	}
1201 
1202 	spin_unlock(&qedi_conn->list_lock);
1203 
1204 	if (lun_reset)
1205 		spin_unlock_bh(&session->back_lock);
1206 
1207 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1208 		  "cmd_cleanup_req=%d, cid=0x%x\n",
1209 		  qedi_conn->cmd_cleanup_req,
1210 		  qedi_conn->iscsi_conn_id);
1211 
1212 	rval  = wait_event_interruptible_timeout(qedi_conn->wait_queue,
1213 				(qedi_conn->cmd_cleanup_req ==
1214 				 atomic_read(&qedi_conn->cmd_cleanup_cmpl)) ||
1215 				test_bit(QEDI_IN_RECOVERY, &qedi->flags),
1216 				5 * HZ);
1217 	if (rval) {
1218 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1219 			  "i/o cmd_cleanup_req=%d, equal to cmd_cleanup_cmpl=%d, cid=0x%x\n",
1220 			  qedi_conn->cmd_cleanup_req,
1221 			  atomic_read(&qedi_conn->cmd_cleanup_cmpl),
1222 			  qedi_conn->iscsi_conn_id);
1223 
1224 		return 0;
1225 	}
1226 
1227 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1228 		  "i/o cmd_cleanup_req=%d, not equal to cmd_cleanup_cmpl=%d, cid=0x%x\n",
1229 		  qedi_conn->cmd_cleanup_req,
1230 		  atomic_read(&qedi_conn->cmd_cleanup_cmpl),
1231 		  qedi_conn->iscsi_conn_id);
1232 
1233 	iscsi_host_for_each_session(qedi->shost,
1234 				    qedi_mark_device_missing);
1235 	qedi_ops->common->drain(qedi->cdev);
1236 
1237 	/* Enable IOs for all other sessions except current.*/
1238 	if (!wait_event_interruptible_timeout(qedi_conn->wait_queue,
1239 				(qedi_conn->cmd_cleanup_req ==
1240 				 atomic_read(&qedi_conn->cmd_cleanup_cmpl)) ||
1241 				test_bit(QEDI_IN_RECOVERY, &qedi->flags),
1242 				5 * HZ)) {
1243 		iscsi_host_for_each_session(qedi->shost,
1244 					    qedi_mark_device_available);
1245 		return -1;
1246 	}
1247 
1248 	iscsi_host_for_each_session(qedi->shost,
1249 				    qedi_mark_device_available);
1250 
1251 	return 0;
1252 }
1253 
1254 void qedi_clearsq(struct qedi_ctx *qedi, struct qedi_conn *qedi_conn,
1255 		  struct iscsi_task *task)
1256 {
1257 	struct qedi_endpoint *qedi_ep;
1258 	int rval;
1259 
1260 	qedi_ep = qedi_conn->ep;
1261 	qedi_conn->cmd_cleanup_req = 0;
1262 	atomic_set(&qedi_conn->cmd_cleanup_cmpl, 0);
1263 
1264 	if (!qedi_ep) {
1265 		QEDI_WARN(&qedi->dbg_ctx,
1266 			  "Cannot proceed, ep already disconnected, cid=0x%x\n",
1267 			  qedi_conn->iscsi_conn_id);
1268 		return;
1269 	}
1270 
1271 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1272 		  "Clearing SQ for cid=0x%x, conn=%p, ep=%p\n",
1273 		  qedi_conn->iscsi_conn_id, qedi_conn, qedi_ep);
1274 
1275 	qedi_ops->clear_sq(qedi->cdev, qedi_ep->handle);
1276 
1277 	rval = qedi_cleanup_all_io(qedi, qedi_conn, task, true);
1278 	if (rval) {
1279 		QEDI_ERR(&qedi->dbg_ctx,
1280 			 "fatal error, need hard reset, cid=0x%x\n",
1281 			 qedi_conn->iscsi_conn_id);
1282 		WARN_ON(1);
1283 	}
1284 }
1285 
1286 static int qedi_wait_for_cleanup_request(struct qedi_ctx *qedi,
1287 					 struct qedi_conn *qedi_conn,
1288 					 struct iscsi_task *task,
1289 					 struct qedi_cmd *qedi_cmd,
1290 					 struct qedi_work_map *list_work)
1291 {
1292 	struct qedi_cmd *cmd = (struct qedi_cmd *)task->dd_data;
1293 	int wait;
1294 
1295 	wait  = wait_event_interruptible_timeout(qedi_conn->wait_queue,
1296 						 ((qedi_cmd->state ==
1297 						   CLEANUP_RECV) ||
1298 						 ((qedi_cmd->type == TYPEIO) &&
1299 						  (cmd->state ==
1300 						   RESPONSE_RECEIVED))),
1301 						 5 * HZ);
1302 	if (!wait) {
1303 		qedi_cmd->state = CLEANUP_WAIT_FAILED;
1304 
1305 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1306 			  "Cleanup timedout tid=0x%x, issue connection recovery, cid=0x%x\n",
1307 			  cmd->task_id, qedi_conn->iscsi_conn_id);
1308 
1309 		return -1;
1310 	}
1311 	return 0;
1312 }
1313 
1314 static void qedi_abort_work(struct work_struct *work)
1315 {
1316 	struct qedi_cmd *qedi_cmd =
1317 		container_of(work, struct qedi_cmd, tmf_work);
1318 	struct qedi_conn *qedi_conn = qedi_cmd->conn;
1319 	struct qedi_ctx *qedi = qedi_conn->qedi;
1320 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
1321 	struct qedi_work_map *list_work = NULL;
1322 	struct iscsi_task *mtask;
1323 	struct qedi_cmd *cmd;
1324 	struct iscsi_task *ctask;
1325 	struct iscsi_tm *tmf_hdr;
1326 	s16 rval = 0;
1327 
1328 	mtask = qedi_cmd->task;
1329 	tmf_hdr = (struct iscsi_tm *)mtask->hdr;
1330 
1331 	spin_lock_bh(&conn->session->back_lock);
1332 	ctask = iscsi_itt_to_ctask(conn, tmf_hdr->rtt);
1333 	if (!ctask) {
1334 		spin_unlock_bh(&conn->session->back_lock);
1335 		QEDI_ERR(&qedi->dbg_ctx, "Invalid RTT. Letting abort timeout.\n");
1336 		goto clear_cleanup;
1337 	}
1338 
1339 	if (iscsi_task_is_completed(ctask)) {
1340 		spin_unlock_bh(&conn->session->back_lock);
1341 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1342 			  "Task already completed\n");
1343 		/*
1344 		 * We have to still send the TMF because libiscsi needs the
1345 		 * response to avoid a timeout.
1346 		 */
1347 		goto send_tmf;
1348 	}
1349 	spin_unlock_bh(&conn->session->back_lock);
1350 
1351 	cmd = (struct qedi_cmd *)ctask->dd_data;
1352 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1353 		  "Abort tmf rtt=0x%x, cmd itt=0x%x, cmd tid=0x%x, cid=0x%x\n",
1354 		  get_itt(tmf_hdr->rtt), get_itt(ctask->itt), cmd->task_id,
1355 		  qedi_conn->iscsi_conn_id);
1356 
1357 	if (qedi_do_not_recover) {
1358 		QEDI_ERR(&qedi->dbg_ctx, "DONT SEND CLEANUP/ABORT %d\n",
1359 			 qedi_do_not_recover);
1360 		goto clear_cleanup;
1361 	}
1362 
1363 	list_work = kzalloc(sizeof(*list_work), GFP_NOIO);
1364 	if (!list_work) {
1365 		QEDI_ERR(&qedi->dbg_ctx, "Memory allocation failed\n");
1366 		goto clear_cleanup;
1367 	}
1368 
1369 	qedi_cmd->type = TYPEIO;
1370 	qedi_cmd->state = CLEANUP_WAIT;
1371 	list_work->qedi_cmd = qedi_cmd;
1372 	list_work->rtid = cmd->task_id;
1373 	list_work->state = QEDI_WORK_SCHEDULED;
1374 	list_work->ctask = ctask;
1375 	qedi_cmd->list_tmf_work = list_work;
1376 
1377 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1378 		  "Queue tmf work=%p, list node=%p, cid=0x%x, tmf flags=0x%x\n",
1379 		  list_work->ptr_tmf_work, list_work, qedi_conn->iscsi_conn_id,
1380 		  tmf_hdr->flags);
1381 
1382 	spin_lock_bh(&qedi_conn->tmf_work_lock);
1383 	list_add_tail(&list_work->list, &qedi_conn->tmf_work_list);
1384 	spin_unlock_bh(&qedi_conn->tmf_work_lock);
1385 
1386 	qedi_iscsi_cleanup_task(ctask, false);
1387 
1388 	rval = qedi_wait_for_cleanup_request(qedi, qedi_conn, ctask, qedi_cmd,
1389 					     list_work);
1390 	if (rval == -1) {
1391 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1392 			  "FW cleanup got escalated, cid=0x%x\n",
1393 			  qedi_conn->iscsi_conn_id);
1394 		goto ldel_exit;
1395 	}
1396 
1397 send_tmf:
1398 	send_iscsi_tmf(qedi_conn, qedi_cmd->task, ctask);
1399 	goto clear_cleanup;
1400 
1401 ldel_exit:
1402 	spin_lock_bh(&qedi_conn->tmf_work_lock);
1403 	if (qedi_cmd->list_tmf_work) {
1404 		list_del_init(&list_work->list);
1405 		qedi_cmd->list_tmf_work = NULL;
1406 		kfree(list_work);
1407 	}
1408 	spin_unlock_bh(&qedi_conn->tmf_work_lock);
1409 
1410 	spin_lock(&qedi_conn->list_lock);
1411 	if (likely(cmd->io_cmd_in_list)) {
1412 		cmd->io_cmd_in_list = false;
1413 		list_del_init(&cmd->io_cmd);
1414 		qedi_conn->active_cmd_count--;
1415 	}
1416 	spin_unlock(&qedi_conn->list_lock);
1417 
1418 clear_cleanup:
1419 	spin_lock(&qedi_conn->tmf_work_lock);
1420 	qedi_conn->fw_cleanup_works--;
1421 	spin_unlock(&qedi_conn->tmf_work_lock);
1422 }
1423 
1424 static int send_iscsi_tmf(struct qedi_conn *qedi_conn, struct iscsi_task *mtask,
1425 			  struct iscsi_task *ctask)
1426 {
1427 	struct iscsi_tmf_request_hdr tmf_pdu_header;
1428 	struct iscsi_task_params task_params;
1429 	struct qedi_ctx *qedi = qedi_conn->qedi;
1430 	struct iscsi_task_context *fw_task_ctx;
1431 	struct iscsi_tm *tmf_hdr;
1432 	struct qedi_cmd *qedi_cmd;
1433 	struct qedi_cmd *cmd;
1434 	struct qedi_endpoint *ep;
1435 	u32 scsi_lun[2];
1436 	s16 tid = 0;
1437 	u16 sq_idx = 0;
1438 
1439 	tmf_hdr = (struct iscsi_tm *)mtask->hdr;
1440 	qedi_cmd = (struct qedi_cmd *)mtask->dd_data;
1441 	ep = qedi_conn->ep;
1442 	if (!ep)
1443 		return -ENODEV;
1444 
1445 	tid = qedi_get_task_idx(qedi);
1446 	if (tid == -1)
1447 		return -ENOMEM;
1448 
1449 	fw_task_ctx =
1450 	     (struct iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1451 							       tid);
1452 	memset(fw_task_ctx, 0, sizeof(struct iscsi_task_context));
1453 
1454 	qedi_cmd->task_id = tid;
1455 
1456 	memset(&task_params, 0, sizeof(task_params));
1457 	memset(&tmf_pdu_header, 0, sizeof(tmf_pdu_header));
1458 
1459 	/* Update header info */
1460 	qedi_update_itt_map(qedi, tid, mtask->itt, qedi_cmd);
1461 	tmf_pdu_header.itt = qedi_set_itt(tid, get_itt(mtask->itt));
1462 	tmf_pdu_header.cmd_sn = be32_to_cpu(tmf_hdr->cmdsn);
1463 
1464 	memcpy(scsi_lun, &tmf_hdr->lun, sizeof(struct scsi_lun));
1465 	tmf_pdu_header.lun.lo = be32_to_cpu(scsi_lun[0]);
1466 	tmf_pdu_header.lun.hi = be32_to_cpu(scsi_lun[1]);
1467 
1468 	if ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1469 	     ISCSI_TM_FUNC_ABORT_TASK) {
1470 		cmd = (struct qedi_cmd *)ctask->dd_data;
1471 		tmf_pdu_header.rtt =
1472 				qedi_set_itt(cmd->task_id,
1473 					     get_itt(tmf_hdr->rtt));
1474 	} else {
1475 		tmf_pdu_header.rtt = ISCSI_RESERVED_TAG;
1476 	}
1477 
1478 	tmf_pdu_header.opcode = tmf_hdr->opcode;
1479 	tmf_pdu_header.function = tmf_hdr->flags;
1480 	tmf_pdu_header.hdr_second_dword = ntoh24(tmf_hdr->dlength);
1481 	tmf_pdu_header.ref_cmd_sn = be32_to_cpu(tmf_hdr->refcmdsn);
1482 
1483 	/* Fill fw input params */
1484 	task_params.context = fw_task_ctx;
1485 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1486 	task_params.itid = tid;
1487 	task_params.cq_rss_number = 0;
1488 	task_params.tx_io_size = 0;
1489 	task_params.rx_io_size = 0;
1490 
1491 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1492 	task_params.sqe = &ep->sq[sq_idx];
1493 
1494 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1495 	init_initiator_tmf_request_task(&task_params, &tmf_pdu_header);
1496 
1497 	spin_lock(&qedi_conn->list_lock);
1498 	list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1499 	qedi_cmd->io_cmd_in_list = true;
1500 	qedi_conn->active_cmd_count++;
1501 	spin_unlock(&qedi_conn->list_lock);
1502 
1503 	qedi_ring_doorbell(qedi_conn);
1504 	return 0;
1505 }
1506 
1507 int qedi_send_iscsi_tmf(struct qedi_conn *qedi_conn, struct iscsi_task *mtask)
1508 {
1509 	struct iscsi_tm *tmf_hdr = (struct iscsi_tm *)mtask->hdr;
1510 	struct qedi_cmd *qedi_cmd = mtask->dd_data;
1511 	struct qedi_ctx *qedi = qedi_conn->qedi;
1512 	int rc = 0;
1513 
1514 	switch (tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) {
1515 	case ISCSI_TM_FUNC_ABORT_TASK:
1516 		spin_lock(&qedi_conn->tmf_work_lock);
1517 		qedi_conn->fw_cleanup_works++;
1518 		spin_unlock(&qedi_conn->tmf_work_lock);
1519 
1520 		INIT_WORK(&qedi_cmd->tmf_work, qedi_abort_work);
1521 		queue_work(qedi->tmf_thread, &qedi_cmd->tmf_work);
1522 		break;
1523 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1524 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1525 	case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1526 		rc = send_iscsi_tmf(qedi_conn, mtask, NULL);
1527 		break;
1528 	default:
1529 		QEDI_ERR(&qedi->dbg_ctx, "Invalid tmf, cid=0x%x\n",
1530 			 qedi_conn->iscsi_conn_id);
1531 		return -EINVAL;
1532 	}
1533 
1534 	return rc;
1535 }
1536 
1537 int qedi_send_iscsi_text(struct qedi_conn *qedi_conn,
1538 			 struct iscsi_task *task)
1539 {
1540 	struct iscsi_text_request_hdr text_request_pdu_header;
1541 	struct scsi_sgl_task_params tx_sgl_task_params;
1542 	struct scsi_sgl_task_params rx_sgl_task_params;
1543 	struct iscsi_task_params task_params;
1544 	struct iscsi_task_context *fw_task_ctx;
1545 	struct qedi_ctx *qedi = qedi_conn->qedi;
1546 	struct iscsi_text *text_hdr;
1547 	struct scsi_sge *req_sge = NULL;
1548 	struct scsi_sge *resp_sge = NULL;
1549 	struct qedi_cmd *qedi_cmd;
1550 	struct qedi_endpoint *ep;
1551 	s16 tid = 0;
1552 	u16 sq_idx = 0;
1553 	int rval = 0;
1554 
1555 	req_sge = (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
1556 	resp_sge = (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1557 	qedi_cmd = (struct qedi_cmd *)task->dd_data;
1558 	text_hdr = (struct iscsi_text *)task->hdr;
1559 	ep = qedi_conn->ep;
1560 
1561 	tid = qedi_get_task_idx(qedi);
1562 	if (tid == -1)
1563 		return -ENOMEM;
1564 
1565 	fw_task_ctx =
1566 	     (struct iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1567 							       tid);
1568 	memset(fw_task_ctx, 0, sizeof(struct iscsi_task_context));
1569 
1570 	qedi_cmd->task_id = tid;
1571 
1572 	memset(&task_params, 0, sizeof(task_params));
1573 	memset(&text_request_pdu_header, 0, sizeof(text_request_pdu_header));
1574 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
1575 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
1576 
1577 	/* Update header info */
1578 	text_request_pdu_header.opcode = text_hdr->opcode;
1579 	text_request_pdu_header.flags_attr = text_hdr->flags;
1580 
1581 	qedi_update_itt_map(qedi, tid, task->itt, qedi_cmd);
1582 	text_request_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
1583 	text_request_pdu_header.ttt = text_hdr->ttt;
1584 	text_request_pdu_header.cmd_sn = be32_to_cpu(text_hdr->cmdsn);
1585 	text_request_pdu_header.exp_stat_sn = be32_to_cpu(text_hdr->exp_statsn);
1586 	text_request_pdu_header.hdr_second_dword = ntoh24(text_hdr->dlength);
1587 
1588 	/* Fill tx AHS and rx buffer */
1589 	tx_sgl_task_params.sgl =
1590 			       (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
1591 	tx_sgl_task_params.sgl_phys_addr.lo =
1592 					 (u32)(qedi_conn->gen_pdu.req_dma_addr);
1593 	tx_sgl_task_params.sgl_phys_addr.hi =
1594 			      (u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
1595 	tx_sgl_task_params.total_buffer_size = req_sge->sge_len;
1596 	tx_sgl_task_params.num_sges = 1;
1597 
1598 	rx_sgl_task_params.sgl =
1599 			      (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1600 	rx_sgl_task_params.sgl_phys_addr.lo =
1601 					(u32)(qedi_conn->gen_pdu.resp_dma_addr);
1602 	rx_sgl_task_params.sgl_phys_addr.hi =
1603 			     (u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
1604 	rx_sgl_task_params.total_buffer_size = resp_sge->sge_len;
1605 	rx_sgl_task_params.num_sges = 1;
1606 
1607 	/* Fill fw input params */
1608 	task_params.context = fw_task_ctx;
1609 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1610 	task_params.itid = tid;
1611 	task_params.cq_rss_number = 0;
1612 	task_params.tx_io_size = ntoh24(text_hdr->dlength);
1613 	task_params.rx_io_size = resp_sge->sge_len;
1614 
1615 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1616 	task_params.sqe = &ep->sq[sq_idx];
1617 
1618 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1619 	rval = init_initiator_text_request_task(&task_params,
1620 						&text_request_pdu_header,
1621 						&tx_sgl_task_params,
1622 						&rx_sgl_task_params);
1623 	if (rval)
1624 		return -1;
1625 
1626 	spin_lock(&qedi_conn->list_lock);
1627 	list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1628 	qedi_cmd->io_cmd_in_list = true;
1629 	qedi_conn->active_cmd_count++;
1630 	spin_unlock(&qedi_conn->list_lock);
1631 
1632 	qedi_ring_doorbell(qedi_conn);
1633 	return 0;
1634 }
1635 
1636 int qedi_send_iscsi_nopout(struct qedi_conn *qedi_conn,
1637 			   struct iscsi_task *task,
1638 			   char *datap, int data_len, int unsol)
1639 {
1640 	struct iscsi_nop_out_hdr nop_out_pdu_header;
1641 	struct scsi_sgl_task_params tx_sgl_task_params;
1642 	struct scsi_sgl_task_params rx_sgl_task_params;
1643 	struct iscsi_task_params task_params;
1644 	struct qedi_ctx *qedi = qedi_conn->qedi;
1645 	struct iscsi_task_context *fw_task_ctx;
1646 	struct iscsi_nopout *nopout_hdr;
1647 	struct scsi_sge *resp_sge = NULL;
1648 	struct qedi_cmd *qedi_cmd;
1649 	struct qedi_endpoint *ep;
1650 	u32 scsi_lun[2];
1651 	s16 tid = 0;
1652 	u16 sq_idx = 0;
1653 	int rval = 0;
1654 
1655 	resp_sge = (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1656 	qedi_cmd = (struct qedi_cmd *)task->dd_data;
1657 	nopout_hdr = (struct iscsi_nopout *)task->hdr;
1658 	ep = qedi_conn->ep;
1659 
1660 	tid = qedi_get_task_idx(qedi);
1661 	if (tid == -1)
1662 		return -ENOMEM;
1663 
1664 	fw_task_ctx =
1665 	     (struct iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1666 							       tid);
1667 	memset(fw_task_ctx, 0, sizeof(struct iscsi_task_context));
1668 
1669 	qedi_cmd->task_id = tid;
1670 
1671 	memset(&task_params, 0, sizeof(task_params));
1672 	memset(&nop_out_pdu_header, 0, sizeof(nop_out_pdu_header));
1673 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
1674 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
1675 
1676 	/* Update header info */
1677 	nop_out_pdu_header.opcode = nopout_hdr->opcode;
1678 	SET_FIELD(nop_out_pdu_header.flags_attr, ISCSI_NOP_OUT_HDR_CONST1, 1);
1679 	SET_FIELD(nop_out_pdu_header.flags_attr, ISCSI_NOP_OUT_HDR_RSRV, 0);
1680 
1681 	memcpy(scsi_lun, &nopout_hdr->lun, sizeof(struct scsi_lun));
1682 	nop_out_pdu_header.lun.lo = be32_to_cpu(scsi_lun[0]);
1683 	nop_out_pdu_header.lun.hi = be32_to_cpu(scsi_lun[1]);
1684 	nop_out_pdu_header.cmd_sn = be32_to_cpu(nopout_hdr->cmdsn);
1685 	nop_out_pdu_header.exp_stat_sn = be32_to_cpu(nopout_hdr->exp_statsn);
1686 
1687 	qedi_update_itt_map(qedi, tid, task->itt, qedi_cmd);
1688 
1689 	if (nopout_hdr->ttt != ISCSI_TTT_ALL_ONES) {
1690 		nop_out_pdu_header.itt = be32_to_cpu(nopout_hdr->itt);
1691 		nop_out_pdu_header.ttt = be32_to_cpu(nopout_hdr->ttt);
1692 	} else {
1693 		nop_out_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
1694 		nop_out_pdu_header.ttt = ISCSI_TTT_ALL_ONES;
1695 
1696 		spin_lock(&qedi_conn->list_lock);
1697 		list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1698 		qedi_cmd->io_cmd_in_list = true;
1699 		qedi_conn->active_cmd_count++;
1700 		spin_unlock(&qedi_conn->list_lock);
1701 	}
1702 
1703 	/* Fill tx AHS and rx buffer */
1704 	if (data_len) {
1705 		tx_sgl_task_params.sgl =
1706 			       (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
1707 		tx_sgl_task_params.sgl_phys_addr.lo =
1708 					 (u32)(qedi_conn->gen_pdu.req_dma_addr);
1709 		tx_sgl_task_params.sgl_phys_addr.hi =
1710 			      (u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
1711 		tx_sgl_task_params.total_buffer_size = data_len;
1712 		tx_sgl_task_params.num_sges = 1;
1713 
1714 		rx_sgl_task_params.sgl =
1715 			      (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1716 		rx_sgl_task_params.sgl_phys_addr.lo =
1717 					(u32)(qedi_conn->gen_pdu.resp_dma_addr);
1718 		rx_sgl_task_params.sgl_phys_addr.hi =
1719 			     (u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
1720 		rx_sgl_task_params.total_buffer_size = resp_sge->sge_len;
1721 		rx_sgl_task_params.num_sges = 1;
1722 	}
1723 
1724 	/* Fill fw input params */
1725 	task_params.context = fw_task_ctx;
1726 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1727 	task_params.itid = tid;
1728 	task_params.cq_rss_number = 0;
1729 	task_params.tx_io_size = data_len;
1730 	task_params.rx_io_size = resp_sge->sge_len;
1731 
1732 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1733 	task_params.sqe = &ep->sq[sq_idx];
1734 
1735 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1736 	rval = init_initiator_nop_out_task(&task_params,
1737 					   &nop_out_pdu_header,
1738 					   &tx_sgl_task_params,
1739 					   &rx_sgl_task_params);
1740 	if (rval)
1741 		return -1;
1742 
1743 	qedi_ring_doorbell(qedi_conn);
1744 	return 0;
1745 }
1746 
1747 static int qedi_split_bd(struct qedi_cmd *cmd, u64 addr, int sg_len,
1748 			 int bd_index)
1749 {
1750 	struct scsi_sge *bd = cmd->io_tbl.sge_tbl;
1751 	int frag_size, sg_frags;
1752 
1753 	sg_frags = 0;
1754 
1755 	while (sg_len) {
1756 		if (addr % QEDI_PAGE_SIZE)
1757 			frag_size =
1758 				   (QEDI_PAGE_SIZE - (addr % QEDI_PAGE_SIZE));
1759 		else
1760 			frag_size = (sg_len > QEDI_BD_SPLIT_SZ) ? 0 :
1761 				    (sg_len % QEDI_BD_SPLIT_SZ);
1762 
1763 		if (frag_size == 0)
1764 			frag_size = QEDI_BD_SPLIT_SZ;
1765 
1766 		bd[bd_index + sg_frags].sge_addr.lo = (addr & 0xffffffff);
1767 		bd[bd_index + sg_frags].sge_addr.hi = (addr >> 32);
1768 		bd[bd_index + sg_frags].sge_len = (u16)frag_size;
1769 		QEDI_INFO(&cmd->conn->qedi->dbg_ctx, QEDI_LOG_IO,
1770 			  "split sge %d: addr=%llx, len=%x",
1771 			  (bd_index + sg_frags), addr, frag_size);
1772 
1773 		addr += (u64)frag_size;
1774 		sg_frags++;
1775 		sg_len -= frag_size;
1776 	}
1777 	return sg_frags;
1778 }
1779 
1780 static int qedi_map_scsi_sg(struct qedi_ctx *qedi, struct qedi_cmd *cmd)
1781 {
1782 	struct scsi_cmnd *sc = cmd->scsi_cmd;
1783 	struct scsi_sge *bd = cmd->io_tbl.sge_tbl;
1784 	struct scatterlist *sg;
1785 	int byte_count = 0;
1786 	int bd_count = 0;
1787 	int sg_count;
1788 	int sg_len;
1789 	int sg_frags;
1790 	u64 addr, end_addr;
1791 	int i;
1792 
1793 	WARN_ON(scsi_sg_count(sc) > QEDI_ISCSI_MAX_BDS_PER_CMD);
1794 
1795 	sg_count = dma_map_sg(&qedi->pdev->dev, scsi_sglist(sc),
1796 			      scsi_sg_count(sc), sc->sc_data_direction);
1797 
1798 	/*
1799 	 * New condition to send single SGE as cached-SGL.
1800 	 * Single SGE with length less than 64K.
1801 	 */
1802 	sg = scsi_sglist(sc);
1803 	if ((sg_count == 1) && (sg_dma_len(sg) <= MAX_SGLEN_FOR_CACHESGL)) {
1804 		sg_len = sg_dma_len(sg);
1805 		addr = (u64)sg_dma_address(sg);
1806 
1807 		bd[bd_count].sge_addr.lo = (addr & 0xffffffff);
1808 		bd[bd_count].sge_addr.hi = (addr >> 32);
1809 		bd[bd_count].sge_len = (u16)sg_len;
1810 
1811 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
1812 			  "single-cached-sgl: bd_count:%d addr=%llx, len=%x",
1813 			  sg_count, addr, sg_len);
1814 
1815 		return ++bd_count;
1816 	}
1817 
1818 	scsi_for_each_sg(sc, sg, sg_count, i) {
1819 		sg_len = sg_dma_len(sg);
1820 		addr = (u64)sg_dma_address(sg);
1821 		end_addr = (addr + sg_len);
1822 
1823 		/*
1824 		 * first sg elem in the 'list',
1825 		 * check if end addr is page-aligned.
1826 		 */
1827 		if ((i == 0) && (sg_count > 1) && (end_addr % QEDI_PAGE_SIZE))
1828 			cmd->use_slowpath = true;
1829 
1830 		/*
1831 		 * last sg elem in the 'list',
1832 		 * check if start addr is page-aligned.
1833 		 */
1834 		else if ((i == (sg_count - 1)) &&
1835 			 (sg_count > 1) && (addr % QEDI_PAGE_SIZE))
1836 			cmd->use_slowpath = true;
1837 
1838 		/*
1839 		 * middle sg elements in list,
1840 		 * check if start and end addr is page-aligned
1841 		 */
1842 		else if ((i != 0) && (i != (sg_count - 1)) &&
1843 			 ((addr % QEDI_PAGE_SIZE) ||
1844 			 (end_addr % QEDI_PAGE_SIZE)))
1845 			cmd->use_slowpath = true;
1846 
1847 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO, "sg[%d] size=0x%x",
1848 			  i, sg_len);
1849 
1850 		if (sg_len > QEDI_BD_SPLIT_SZ) {
1851 			sg_frags = qedi_split_bd(cmd, addr, sg_len, bd_count);
1852 		} else {
1853 			sg_frags = 1;
1854 			bd[bd_count].sge_addr.lo = addr & 0xffffffff;
1855 			bd[bd_count].sge_addr.hi = addr >> 32;
1856 			bd[bd_count].sge_len = sg_len;
1857 		}
1858 		byte_count += sg_len;
1859 		bd_count += sg_frags;
1860 	}
1861 
1862 	if (byte_count != scsi_bufflen(sc))
1863 		QEDI_ERR(&qedi->dbg_ctx,
1864 			 "byte_count = %d != scsi_bufflen = %d\n", byte_count,
1865 			 scsi_bufflen(sc));
1866 	else
1867 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO, "byte_count = %d\n",
1868 			  byte_count);
1869 
1870 	WARN_ON(byte_count != scsi_bufflen(sc));
1871 
1872 	return bd_count;
1873 }
1874 
1875 static void qedi_iscsi_map_sg_list(struct qedi_cmd *cmd)
1876 {
1877 	int bd_count;
1878 	struct scsi_cmnd *sc = cmd->scsi_cmd;
1879 
1880 	if (scsi_sg_count(sc)) {
1881 		bd_count  = qedi_map_scsi_sg(cmd->conn->qedi, cmd);
1882 		if (bd_count == 0)
1883 			return;
1884 	} else {
1885 		struct scsi_sge *bd = cmd->io_tbl.sge_tbl;
1886 
1887 		bd[0].sge_addr.lo = 0;
1888 		bd[0].sge_addr.hi = 0;
1889 		bd[0].sge_len = 0;
1890 		bd_count = 0;
1891 	}
1892 	cmd->io_tbl.sge_valid = bd_count;
1893 }
1894 
1895 static void qedi_cpy_scsi_cdb(struct scsi_cmnd *sc, u32 *dstp)
1896 {
1897 	u32 dword;
1898 	int lpcnt;
1899 	u8 *srcp;
1900 
1901 	lpcnt = sc->cmd_len / sizeof(dword);
1902 	srcp = (u8 *)sc->cmnd;
1903 	while (lpcnt--) {
1904 		memcpy(&dword, (const void *)srcp, 4);
1905 		*dstp = cpu_to_be32(dword);
1906 		srcp += 4;
1907 		dstp++;
1908 	}
1909 	if (sc->cmd_len & 0x3) {
1910 		dword = (u32)srcp[0] | ((u32)srcp[1] << 8);
1911 		*dstp = cpu_to_be32(dword);
1912 	}
1913 }
1914 
1915 void qedi_trace_io(struct qedi_ctx *qedi, struct iscsi_task *task,
1916 		   u16 tid, int8_t direction)
1917 {
1918 	struct qedi_io_log *io_log;
1919 	struct iscsi_conn *conn = task->conn;
1920 	struct qedi_conn *qedi_conn = conn->dd_data;
1921 	struct scsi_cmnd *sc_cmd = task->sc;
1922 	unsigned long flags;
1923 
1924 	spin_lock_irqsave(&qedi->io_trace_lock, flags);
1925 
1926 	io_log = &qedi->io_trace_buf[qedi->io_trace_idx];
1927 	io_log->direction = direction;
1928 	io_log->task_id = tid;
1929 	io_log->cid = qedi_conn->iscsi_conn_id;
1930 	io_log->lun = sc_cmd->device->lun;
1931 	io_log->op = sc_cmd->cmnd[0];
1932 	io_log->lba[0] = sc_cmd->cmnd[2];
1933 	io_log->lba[1] = sc_cmd->cmnd[3];
1934 	io_log->lba[2] = sc_cmd->cmnd[4];
1935 	io_log->lba[3] = sc_cmd->cmnd[5];
1936 	io_log->bufflen = scsi_bufflen(sc_cmd);
1937 	io_log->sg_count = scsi_sg_count(sc_cmd);
1938 	io_log->fast_sgs = qedi->fast_sgls;
1939 	io_log->cached_sgs = qedi->cached_sgls;
1940 	io_log->slow_sgs = qedi->slow_sgls;
1941 	io_log->cached_sge = qedi->use_cached_sge;
1942 	io_log->slow_sge = qedi->use_slow_sge;
1943 	io_log->fast_sge = qedi->use_fast_sge;
1944 	io_log->result = sc_cmd->result;
1945 	io_log->jiffies = jiffies;
1946 	io_log->blk_req_cpu = smp_processor_id();
1947 
1948 	if (direction == QEDI_IO_TRACE_REQ) {
1949 		/* For requests we only care about the submission CPU */
1950 		io_log->req_cpu = smp_processor_id() % qedi->num_queues;
1951 		io_log->intr_cpu = 0;
1952 		io_log->blk_rsp_cpu = 0;
1953 	} else if (direction == QEDI_IO_TRACE_RSP) {
1954 		io_log->req_cpu = smp_processor_id() % qedi->num_queues;
1955 		io_log->intr_cpu = qedi->intr_cpu;
1956 		io_log->blk_rsp_cpu = smp_processor_id();
1957 	}
1958 
1959 	qedi->io_trace_idx++;
1960 	if (qedi->io_trace_idx == QEDI_IO_TRACE_SIZE)
1961 		qedi->io_trace_idx = 0;
1962 
1963 	qedi->use_cached_sge = false;
1964 	qedi->use_slow_sge = false;
1965 	qedi->use_fast_sge = false;
1966 
1967 	spin_unlock_irqrestore(&qedi->io_trace_lock, flags);
1968 }
1969 
1970 int qedi_iscsi_send_ioreq(struct iscsi_task *task)
1971 {
1972 	struct iscsi_conn *conn = task->conn;
1973 	struct iscsi_session *session = conn->session;
1974 	struct Scsi_Host *shost = iscsi_session_to_shost(session->cls_session);
1975 	struct qedi_ctx *qedi = iscsi_host_priv(shost);
1976 	struct qedi_conn *qedi_conn = conn->dd_data;
1977 	struct qedi_cmd *cmd = task->dd_data;
1978 	struct scsi_cmnd *sc = task->sc;
1979 	struct iscsi_cmd_hdr cmd_pdu_header;
1980 	struct scsi_sgl_task_params tx_sgl_task_params;
1981 	struct scsi_sgl_task_params rx_sgl_task_params;
1982 	struct scsi_sgl_task_params *prx_sgl = NULL;
1983 	struct scsi_sgl_task_params *ptx_sgl = NULL;
1984 	struct iscsi_task_params task_params;
1985 	struct iscsi_conn_params conn_params;
1986 	struct scsi_initiator_cmd_params cmd_params;
1987 	struct iscsi_task_context *fw_task_ctx;
1988 	struct iscsi_cls_conn *cls_conn;
1989 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)task->hdr;
1990 	enum iscsi_task_type task_type = MAX_ISCSI_TASK_TYPE;
1991 	struct qedi_endpoint *ep;
1992 	u32 scsi_lun[2];
1993 	s16 tid = 0;
1994 	u16 sq_idx = 0;
1995 	u16 cq_idx;
1996 	int rval = 0;
1997 
1998 	ep = qedi_conn->ep;
1999 	cls_conn = qedi_conn->cls_conn;
2000 	conn = cls_conn->dd_data;
2001 
2002 	qedi_iscsi_map_sg_list(cmd);
2003 	int_to_scsilun(sc->device->lun, (struct scsi_lun *)scsi_lun);
2004 
2005 	tid = qedi_get_task_idx(qedi);
2006 	if (tid == -1)
2007 		return -ENOMEM;
2008 
2009 	fw_task_ctx =
2010 	     (struct iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
2011 							       tid);
2012 	memset(fw_task_ctx, 0, sizeof(struct iscsi_task_context));
2013 
2014 	cmd->task_id = tid;
2015 
2016 	memset(&task_params, 0, sizeof(task_params));
2017 	memset(&cmd_pdu_header, 0, sizeof(cmd_pdu_header));
2018 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
2019 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
2020 	memset(&conn_params, 0, sizeof(conn_params));
2021 	memset(&cmd_params, 0, sizeof(cmd_params));
2022 
2023 	cq_idx = smp_processor_id() % qedi->num_queues;
2024 	/* Update header info */
2025 	SET_FIELD(cmd_pdu_header.flags_attr, ISCSI_CMD_HDR_ATTR,
2026 		  ISCSI_ATTR_SIMPLE);
2027 	if (hdr->cdb[0] != TEST_UNIT_READY) {
2028 		if (sc->sc_data_direction == DMA_TO_DEVICE) {
2029 			SET_FIELD(cmd_pdu_header.flags_attr,
2030 				  ISCSI_CMD_HDR_WRITE, 1);
2031 			task_type = ISCSI_TASK_TYPE_INITIATOR_WRITE;
2032 		} else {
2033 			SET_FIELD(cmd_pdu_header.flags_attr,
2034 				  ISCSI_CMD_HDR_READ, 1);
2035 			task_type = ISCSI_TASK_TYPE_INITIATOR_READ;
2036 		}
2037 	}
2038 
2039 	cmd_pdu_header.lun.lo = be32_to_cpu(scsi_lun[0]);
2040 	cmd_pdu_header.lun.hi = be32_to_cpu(scsi_lun[1]);
2041 
2042 	qedi_update_itt_map(qedi, tid, task->itt, cmd);
2043 	cmd_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
2044 	cmd_pdu_header.expected_transfer_length = cpu_to_be32(hdr->data_length);
2045 	cmd_pdu_header.hdr_second_dword = ntoh24(hdr->dlength);
2046 	cmd_pdu_header.cmd_sn = be32_to_cpu(hdr->cmdsn);
2047 	cmd_pdu_header.hdr_first_byte = hdr->opcode;
2048 	qedi_cpy_scsi_cdb(sc, (u32 *)cmd_pdu_header.cdb);
2049 
2050 	/* Fill tx AHS and rx buffer */
2051 	if (task_type == ISCSI_TASK_TYPE_INITIATOR_WRITE) {
2052 		tx_sgl_task_params.sgl = cmd->io_tbl.sge_tbl;
2053 		tx_sgl_task_params.sgl_phys_addr.lo =
2054 						 (u32)(cmd->io_tbl.sge_tbl_dma);
2055 		tx_sgl_task_params.sgl_phys_addr.hi =
2056 				      (u32)((u64)cmd->io_tbl.sge_tbl_dma >> 32);
2057 		tx_sgl_task_params.total_buffer_size = scsi_bufflen(sc);
2058 		tx_sgl_task_params.num_sges = cmd->io_tbl.sge_valid;
2059 		if (cmd->use_slowpath)
2060 			tx_sgl_task_params.small_mid_sge = true;
2061 	} else if (task_type == ISCSI_TASK_TYPE_INITIATOR_READ) {
2062 		rx_sgl_task_params.sgl = cmd->io_tbl.sge_tbl;
2063 		rx_sgl_task_params.sgl_phys_addr.lo =
2064 						 (u32)(cmd->io_tbl.sge_tbl_dma);
2065 		rx_sgl_task_params.sgl_phys_addr.hi =
2066 				      (u32)((u64)cmd->io_tbl.sge_tbl_dma >> 32);
2067 		rx_sgl_task_params.total_buffer_size = scsi_bufflen(sc);
2068 		rx_sgl_task_params.num_sges = cmd->io_tbl.sge_valid;
2069 	}
2070 
2071 	/* Add conn param */
2072 	conn_params.first_burst_length = conn->session->first_burst;
2073 	conn_params.max_send_pdu_length = conn->max_xmit_dlength;
2074 	conn_params.max_burst_length = conn->session->max_burst;
2075 	if (conn->session->initial_r2t_en)
2076 		conn_params.initial_r2t = true;
2077 	if (conn->session->imm_data_en)
2078 		conn_params.immediate_data = true;
2079 
2080 	/* Add cmd params */
2081 	cmd_params.sense_data_buffer_phys_addr.lo = (u32)cmd->sense_buffer_dma;
2082 	cmd_params.sense_data_buffer_phys_addr.hi =
2083 					(u32)((u64)cmd->sense_buffer_dma >> 32);
2084 	/* Fill fw input params */
2085 	task_params.context = fw_task_ctx;
2086 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
2087 	task_params.itid = tid;
2088 	task_params.cq_rss_number = cq_idx;
2089 	if (task_type == ISCSI_TASK_TYPE_INITIATOR_WRITE)
2090 		task_params.tx_io_size = scsi_bufflen(sc);
2091 	else if (task_type == ISCSI_TASK_TYPE_INITIATOR_READ)
2092 		task_params.rx_io_size = scsi_bufflen(sc);
2093 
2094 	sq_idx = qedi_get_wqe_idx(qedi_conn);
2095 	task_params.sqe = &ep->sq[sq_idx];
2096 
2097 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
2098 		  "%s: %s-SGL: sg_len=0x%x num_sges=0x%x first-sge-lo=0x%x first-sge-hi=0x%x\n",
2099 		  (task_type == ISCSI_TASK_TYPE_INITIATOR_WRITE) ?
2100 		  "Write " : "Read ", (cmd->io_tbl.sge_valid == 1) ?
2101 		  "Single" : (cmd->use_slowpath ? "SLOW" : "FAST"),
2102 		  (u16)cmd->io_tbl.sge_valid, scsi_bufflen(sc),
2103 		  (u32)(cmd->io_tbl.sge_tbl_dma),
2104 		  (u32)((u64)cmd->io_tbl.sge_tbl_dma >> 32));
2105 
2106 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
2107 
2108 	if (task_params.tx_io_size != 0)
2109 		ptx_sgl = &tx_sgl_task_params;
2110 	if (task_params.rx_io_size != 0)
2111 		prx_sgl = &rx_sgl_task_params;
2112 
2113 	rval = init_initiator_rw_iscsi_task(&task_params, &conn_params,
2114 					    &cmd_params, &cmd_pdu_header,
2115 					    ptx_sgl, prx_sgl,
2116 					    NULL);
2117 	if (rval)
2118 		return -1;
2119 
2120 	spin_lock(&qedi_conn->list_lock);
2121 	list_add_tail(&cmd->io_cmd, &qedi_conn->active_cmd_list);
2122 	cmd->io_cmd_in_list = true;
2123 	qedi_conn->active_cmd_count++;
2124 	spin_unlock(&qedi_conn->list_lock);
2125 
2126 	qedi_ring_doorbell(qedi_conn);
2127 	return 0;
2128 }
2129 
2130 int qedi_iscsi_cleanup_task(struct iscsi_task *task, bool mark_cmd_node_deleted)
2131 {
2132 	struct iscsi_task_params task_params;
2133 	struct qedi_endpoint *ep;
2134 	struct iscsi_conn *conn = task->conn;
2135 	struct qedi_conn *qedi_conn = conn->dd_data;
2136 	struct qedi_cmd *cmd = task->dd_data;
2137 	u16 sq_idx = 0;
2138 	int rval = 0;
2139 
2140 	QEDI_INFO(&qedi_conn->qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
2141 		  "issue cleanup tid=0x%x itt=0x%x task_state=%d cmd_state=0%x cid=0x%x\n",
2142 		  cmd->task_id, get_itt(task->itt), task->state,
2143 		  cmd->state, qedi_conn->iscsi_conn_id);
2144 
2145 	memset(&task_params, 0, sizeof(task_params));
2146 	ep = qedi_conn->ep;
2147 
2148 	sq_idx = qedi_get_wqe_idx(qedi_conn);
2149 
2150 	task_params.sqe = &ep->sq[sq_idx];
2151 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
2152 	task_params.itid = cmd->task_id;
2153 
2154 	rval = init_cleanup_task(&task_params);
2155 	if (rval)
2156 		return rval;
2157 
2158 	qedi_ring_doorbell(qedi_conn);
2159 	return 0;
2160 }
2161