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