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