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