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