xref: /openbmc/linux/drivers/scsi/qedi/qedi_fw.c (revision d2574c33)
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 	mmiowb();
989 	QEDI_INFO(&qedi_conn->qedi->dbg_ctx, QEDI_LOG_MP_REQ,
990 		  "prod_idx=0x%x, fw_prod_idx=0x%x, cid=0x%x\n",
991 		  qedi_conn->ep->sq_prod_idx, qedi_conn->ep->fw_sq_prod_idx,
992 		  qedi_conn->iscsi_conn_id);
993 }
994 
995 static u16 qedi_get_wqe_idx(struct qedi_conn *qedi_conn)
996 {
997 	struct qedi_endpoint *ep;
998 	u16 rval;
999 
1000 	ep = qedi_conn->ep;
1001 	rval = ep->sq_prod_idx;
1002 
1003 	/* Increament SQ index */
1004 	ep->sq_prod_idx++;
1005 	ep->fw_sq_prod_idx++;
1006 	if (ep->sq_prod_idx == QEDI_SQ_SIZE)
1007 		ep->sq_prod_idx = 0;
1008 
1009 	return rval;
1010 }
1011 
1012 int qedi_send_iscsi_login(struct qedi_conn *qedi_conn,
1013 			  struct iscsi_task *task)
1014 {
1015 	struct iscsi_login_req_hdr login_req_pdu_header;
1016 	struct scsi_sgl_task_params tx_sgl_task_params;
1017 	struct scsi_sgl_task_params rx_sgl_task_params;
1018 	struct iscsi_task_params task_params;
1019 	struct e4_iscsi_task_context *fw_task_ctx;
1020 	struct qedi_ctx *qedi = qedi_conn->qedi;
1021 	struct iscsi_login_req *login_hdr;
1022 	struct scsi_sge *resp_sge = NULL;
1023 	struct qedi_cmd *qedi_cmd;
1024 	struct qedi_endpoint *ep;
1025 	s16 tid = 0;
1026 	u16 sq_idx = 0;
1027 	int rval = 0;
1028 
1029 	resp_sge = (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1030 	qedi_cmd = (struct qedi_cmd *)task->dd_data;
1031 	ep = qedi_conn->ep;
1032 	login_hdr = (struct iscsi_login_req *)task->hdr;
1033 
1034 	tid = qedi_get_task_idx(qedi);
1035 	if (tid == -1)
1036 		return -ENOMEM;
1037 
1038 	fw_task_ctx =
1039 	     (struct e4_iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1040 							       tid);
1041 	memset(fw_task_ctx, 0, sizeof(struct e4_iscsi_task_context));
1042 
1043 	qedi_cmd->task_id = tid;
1044 
1045 	memset(&task_params, 0, sizeof(task_params));
1046 	memset(&login_req_pdu_header, 0, sizeof(login_req_pdu_header));
1047 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
1048 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
1049 	/* Update header info */
1050 	login_req_pdu_header.opcode = login_hdr->opcode;
1051 	login_req_pdu_header.version_min = login_hdr->min_version;
1052 	login_req_pdu_header.version_max = login_hdr->max_version;
1053 	login_req_pdu_header.flags_attr = login_hdr->flags;
1054 	login_req_pdu_header.isid_tabc = swab32p((u32 *)login_hdr->isid);
1055 	login_req_pdu_header.isid_d = swab16p((u16 *)&login_hdr->isid[4]);
1056 
1057 	login_req_pdu_header.tsih = login_hdr->tsih;
1058 	login_req_pdu_header.hdr_second_dword = ntoh24(login_hdr->dlength);
1059 
1060 	qedi_update_itt_map(qedi, tid, task->itt, qedi_cmd);
1061 	login_req_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
1062 	login_req_pdu_header.cid = qedi_conn->iscsi_conn_id;
1063 	login_req_pdu_header.cmd_sn = be32_to_cpu(login_hdr->cmdsn);
1064 	login_req_pdu_header.exp_stat_sn = be32_to_cpu(login_hdr->exp_statsn);
1065 	login_req_pdu_header.exp_stat_sn = 0;
1066 
1067 	/* Fill tx AHS and rx buffer */
1068 	tx_sgl_task_params.sgl =
1069 			       (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
1070 	tx_sgl_task_params.sgl_phys_addr.lo =
1071 					 (u32)(qedi_conn->gen_pdu.req_dma_addr);
1072 	tx_sgl_task_params.sgl_phys_addr.hi =
1073 			      (u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
1074 	tx_sgl_task_params.total_buffer_size = ntoh24(login_hdr->dlength);
1075 	tx_sgl_task_params.num_sges = 1;
1076 
1077 	rx_sgl_task_params.sgl =
1078 			      (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1079 	rx_sgl_task_params.sgl_phys_addr.lo =
1080 					(u32)(qedi_conn->gen_pdu.resp_dma_addr);
1081 	rx_sgl_task_params.sgl_phys_addr.hi =
1082 			     (u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
1083 	rx_sgl_task_params.total_buffer_size = resp_sge->sge_len;
1084 	rx_sgl_task_params.num_sges = 1;
1085 
1086 	/* Fill fw input params */
1087 	task_params.context = fw_task_ctx;
1088 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1089 	task_params.itid = tid;
1090 	task_params.cq_rss_number = 0;
1091 	task_params.tx_io_size = ntoh24(login_hdr->dlength);
1092 	task_params.rx_io_size = resp_sge->sge_len;
1093 
1094 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1095 	task_params.sqe = &ep->sq[sq_idx];
1096 
1097 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1098 	rval = init_initiator_login_request_task(&task_params,
1099 						 &login_req_pdu_header,
1100 						 &tx_sgl_task_params,
1101 						 &rx_sgl_task_params);
1102 	if (rval)
1103 		return -1;
1104 
1105 	spin_lock(&qedi_conn->list_lock);
1106 	list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1107 	qedi_cmd->io_cmd_in_list = true;
1108 	qedi_conn->active_cmd_count++;
1109 	spin_unlock(&qedi_conn->list_lock);
1110 
1111 	qedi_ring_doorbell(qedi_conn);
1112 	return 0;
1113 }
1114 
1115 int qedi_send_iscsi_logout(struct qedi_conn *qedi_conn,
1116 			   struct iscsi_task *task)
1117 {
1118 	struct iscsi_logout_req_hdr logout_pdu_header;
1119 	struct scsi_sgl_task_params tx_sgl_task_params;
1120 	struct scsi_sgl_task_params rx_sgl_task_params;
1121 	struct iscsi_task_params task_params;
1122 	struct e4_iscsi_task_context *fw_task_ctx;
1123 	struct iscsi_logout *logout_hdr = NULL;
1124 	struct qedi_ctx *qedi = qedi_conn->qedi;
1125 	struct qedi_cmd *qedi_cmd;
1126 	struct qedi_endpoint *ep;
1127 	s16 tid = 0;
1128 	u16 sq_idx = 0;
1129 	int rval = 0;
1130 
1131 	qedi_cmd = (struct qedi_cmd *)task->dd_data;
1132 	logout_hdr = (struct iscsi_logout *)task->hdr;
1133 	ep = qedi_conn->ep;
1134 
1135 	tid = qedi_get_task_idx(qedi);
1136 	if (tid == -1)
1137 		return -ENOMEM;
1138 
1139 	fw_task_ctx =
1140 	     (struct e4_iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1141 							       tid);
1142 	memset(fw_task_ctx, 0, sizeof(struct e4_iscsi_task_context));
1143 
1144 	qedi_cmd->task_id = tid;
1145 
1146 	memset(&task_params, 0, sizeof(task_params));
1147 	memset(&logout_pdu_header, 0, sizeof(logout_pdu_header));
1148 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
1149 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
1150 
1151 	/* Update header info */
1152 	logout_pdu_header.opcode = logout_hdr->opcode;
1153 	logout_pdu_header.reason_code = 0x80 | logout_hdr->flags;
1154 	qedi_update_itt_map(qedi, tid, task->itt, qedi_cmd);
1155 	logout_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
1156 	logout_pdu_header.exp_stat_sn = be32_to_cpu(logout_hdr->exp_statsn);
1157 	logout_pdu_header.cmd_sn = be32_to_cpu(logout_hdr->cmdsn);
1158 	logout_pdu_header.cid = qedi_conn->iscsi_conn_id;
1159 
1160 	/* Fill fw input params */
1161 	task_params.context = fw_task_ctx;
1162 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1163 	task_params.itid = tid;
1164 	task_params.cq_rss_number = 0;
1165 	task_params.tx_io_size = 0;
1166 	task_params.rx_io_size = 0;
1167 
1168 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1169 	task_params.sqe = &ep->sq[sq_idx];
1170 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1171 
1172 	rval = init_initiator_logout_request_task(&task_params,
1173 						  &logout_pdu_header,
1174 						  NULL, NULL);
1175 	if (rval)
1176 		return -1;
1177 
1178 	spin_lock(&qedi_conn->list_lock);
1179 	list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1180 	qedi_cmd->io_cmd_in_list = true;
1181 	qedi_conn->active_cmd_count++;
1182 	spin_unlock(&qedi_conn->list_lock);
1183 
1184 	qedi_ring_doorbell(qedi_conn);
1185 	return 0;
1186 }
1187 
1188 int qedi_cleanup_all_io(struct qedi_ctx *qedi, struct qedi_conn *qedi_conn,
1189 			struct iscsi_task *task, bool in_recovery)
1190 {
1191 	int rval;
1192 	struct iscsi_task *ctask;
1193 	struct qedi_cmd *cmd, *cmd_tmp;
1194 	struct iscsi_tm *tmf_hdr;
1195 	unsigned int lun = 0;
1196 	bool lun_reset = false;
1197 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
1198 	struct iscsi_session *session = conn->session;
1199 
1200 	/* From recovery, task is NULL or from tmf resp valid task */
1201 	if (task) {
1202 		tmf_hdr = (struct iscsi_tm *)task->hdr;
1203 
1204 		if ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1205 			ISCSI_TM_FUNC_LOGICAL_UNIT_RESET) {
1206 			lun_reset = true;
1207 			lun = scsilun_to_int(&tmf_hdr->lun);
1208 		}
1209 	}
1210 
1211 	qedi_conn->cmd_cleanup_req = 0;
1212 	qedi_conn->cmd_cleanup_cmpl = 0;
1213 
1214 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1215 		  "active_cmd_count=%d, cid=0x%x, in_recovery=%d, lun_reset=%d\n",
1216 		  qedi_conn->active_cmd_count, qedi_conn->iscsi_conn_id,
1217 		  in_recovery, lun_reset);
1218 
1219 	if (lun_reset)
1220 		spin_lock_bh(&session->back_lock);
1221 
1222 	spin_lock(&qedi_conn->list_lock);
1223 
1224 	list_for_each_entry_safe(cmd, cmd_tmp, &qedi_conn->active_cmd_list,
1225 				 io_cmd) {
1226 		ctask = cmd->task;
1227 		if (ctask == task)
1228 			continue;
1229 
1230 		if (lun_reset) {
1231 			if (cmd->scsi_cmd && cmd->scsi_cmd->device) {
1232 				QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1233 					  "tid=0x%x itt=0x%x scsi_cmd_ptr=%p device=%p task_state=%d cmd_state=0%x cid=0x%x\n",
1234 					  cmd->task_id, get_itt(ctask->itt),
1235 					  cmd->scsi_cmd, cmd->scsi_cmd->device,
1236 					  ctask->state, cmd->state,
1237 					  qedi_conn->iscsi_conn_id);
1238 				if (cmd->scsi_cmd->device->lun != lun)
1239 					continue;
1240 			}
1241 		}
1242 		qedi_conn->cmd_cleanup_req++;
1243 		qedi_iscsi_cleanup_task(ctask, true);
1244 
1245 		list_del_init(&cmd->io_cmd);
1246 		qedi_conn->active_cmd_count--;
1247 		QEDI_WARN(&qedi->dbg_ctx,
1248 			  "Deleted active cmd list node io_cmd=%p, cid=0x%x\n",
1249 			  &cmd->io_cmd, qedi_conn->iscsi_conn_id);
1250 	}
1251 
1252 	spin_unlock(&qedi_conn->list_lock);
1253 
1254 	if (lun_reset)
1255 		spin_unlock_bh(&session->back_lock);
1256 
1257 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1258 		  "cmd_cleanup_req=%d, cid=0x%x\n",
1259 		  qedi_conn->cmd_cleanup_req,
1260 		  qedi_conn->iscsi_conn_id);
1261 
1262 	rval  = wait_event_interruptible_timeout(qedi_conn->wait_queue,
1263 						 ((qedi_conn->cmd_cleanup_req ==
1264 						 qedi_conn->cmd_cleanup_cmpl) ||
1265 						 qedi_conn->ep),
1266 						 5 * HZ);
1267 	if (rval) {
1268 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1269 			  "i/o cmd_cleanup_req=%d, equal to cmd_cleanup_cmpl=%d, cid=0x%x\n",
1270 			  qedi_conn->cmd_cleanup_req,
1271 			  qedi_conn->cmd_cleanup_cmpl,
1272 			  qedi_conn->iscsi_conn_id);
1273 
1274 		return 0;
1275 	}
1276 
1277 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1278 		  "i/o cmd_cleanup_req=%d, not equal to cmd_cleanup_cmpl=%d, cid=0x%x\n",
1279 		  qedi_conn->cmd_cleanup_req,
1280 		  qedi_conn->cmd_cleanup_cmpl,
1281 		  qedi_conn->iscsi_conn_id);
1282 
1283 	iscsi_host_for_each_session(qedi->shost,
1284 				    qedi_mark_device_missing);
1285 	qedi_ops->common->drain(qedi->cdev);
1286 
1287 	/* Enable IOs for all other sessions except current.*/
1288 	if (!wait_event_interruptible_timeout(qedi_conn->wait_queue,
1289 					      (qedi_conn->cmd_cleanup_req ==
1290 					       qedi_conn->cmd_cleanup_cmpl),
1291 					      5 * HZ)) {
1292 		iscsi_host_for_each_session(qedi->shost,
1293 					    qedi_mark_device_available);
1294 		return -1;
1295 	}
1296 
1297 	iscsi_host_for_each_session(qedi->shost,
1298 				    qedi_mark_device_available);
1299 
1300 	return 0;
1301 }
1302 
1303 void qedi_clearsq(struct qedi_ctx *qedi, struct qedi_conn *qedi_conn,
1304 		  struct iscsi_task *task)
1305 {
1306 	struct qedi_endpoint *qedi_ep;
1307 	int rval;
1308 
1309 	qedi_ep = qedi_conn->ep;
1310 	qedi_conn->cmd_cleanup_req = 0;
1311 	qedi_conn->cmd_cleanup_cmpl = 0;
1312 
1313 	if (!qedi_ep) {
1314 		QEDI_WARN(&qedi->dbg_ctx,
1315 			  "Cannot proceed, ep already disconnected, cid=0x%x\n",
1316 			  qedi_conn->iscsi_conn_id);
1317 		return;
1318 	}
1319 
1320 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1321 		  "Clearing SQ for cid=0x%x, conn=%p, ep=%p\n",
1322 		  qedi_conn->iscsi_conn_id, qedi_conn, qedi_ep);
1323 
1324 	qedi_ops->clear_sq(qedi->cdev, qedi_ep->handle);
1325 
1326 	rval = qedi_cleanup_all_io(qedi, qedi_conn, task, true);
1327 	if (rval) {
1328 		QEDI_ERR(&qedi->dbg_ctx,
1329 			 "fatal error, need hard reset, cid=0x%x\n",
1330 			 qedi_conn->iscsi_conn_id);
1331 		WARN_ON(1);
1332 	}
1333 }
1334 
1335 static int qedi_wait_for_cleanup_request(struct qedi_ctx *qedi,
1336 					 struct qedi_conn *qedi_conn,
1337 					 struct iscsi_task *task,
1338 					 struct qedi_cmd *qedi_cmd,
1339 					 struct qedi_work_map *list_work)
1340 {
1341 	struct qedi_cmd *cmd = (struct qedi_cmd *)task->dd_data;
1342 	int wait;
1343 
1344 	wait  = wait_event_interruptible_timeout(qedi_conn->wait_queue,
1345 						 ((qedi_cmd->state ==
1346 						   CLEANUP_RECV) ||
1347 						 ((qedi_cmd->type == TYPEIO) &&
1348 						  (cmd->state ==
1349 						   RESPONSE_RECEIVED))),
1350 						 5 * HZ);
1351 	if (!wait) {
1352 		qedi_cmd->state = CLEANUP_WAIT_FAILED;
1353 
1354 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1355 			  "Cleanup timedout tid=0x%x, issue connection recovery, cid=0x%x\n",
1356 			  cmd->task_id, qedi_conn->iscsi_conn_id);
1357 
1358 		return -1;
1359 	}
1360 	return 0;
1361 }
1362 
1363 static void qedi_tmf_work(struct work_struct *work)
1364 {
1365 	struct qedi_cmd *qedi_cmd =
1366 		container_of(work, struct qedi_cmd, tmf_work);
1367 	struct qedi_conn *qedi_conn = qedi_cmd->conn;
1368 	struct qedi_ctx *qedi = qedi_conn->qedi;
1369 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
1370 	struct iscsi_cls_session *cls_sess;
1371 	struct qedi_work_map *list_work = NULL;
1372 	struct iscsi_task *mtask;
1373 	struct qedi_cmd *cmd;
1374 	struct iscsi_task *ctask;
1375 	struct iscsi_tm *tmf_hdr;
1376 	s16 rval = 0;
1377 	s16 tid = 0;
1378 
1379 	mtask = qedi_cmd->task;
1380 	tmf_hdr = (struct iscsi_tm *)mtask->hdr;
1381 	cls_sess = iscsi_conn_to_session(qedi_conn->cls_conn);
1382 	set_bit(QEDI_CONN_FW_CLEANUP, &qedi_conn->flags);
1383 
1384 	ctask = iscsi_itt_to_task(conn, tmf_hdr->rtt);
1385 	if (!ctask || !ctask->sc) {
1386 		QEDI_ERR(&qedi->dbg_ctx, "Task already completed\n");
1387 		goto abort_ret;
1388 	}
1389 
1390 	cmd = (struct qedi_cmd *)ctask->dd_data;
1391 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1392 		  "Abort tmf rtt=0x%x, cmd itt=0x%x, cmd tid=0x%x, cid=0x%x\n",
1393 		  get_itt(tmf_hdr->rtt), get_itt(ctask->itt), cmd->task_id,
1394 		  qedi_conn->iscsi_conn_id);
1395 
1396 	if (qedi_do_not_recover) {
1397 		QEDI_ERR(&qedi->dbg_ctx, "DONT SEND CLEANUP/ABORT %d\n",
1398 			 qedi_do_not_recover);
1399 		goto abort_ret;
1400 	}
1401 
1402 	list_work = kzalloc(sizeof(*list_work), GFP_ATOMIC);
1403 	if (!list_work) {
1404 		QEDI_ERR(&qedi->dbg_ctx, "Memory allocation failed\n");
1405 		goto abort_ret;
1406 	}
1407 
1408 	qedi_cmd->type = TYPEIO;
1409 	list_work->qedi_cmd = qedi_cmd;
1410 	list_work->rtid = cmd->task_id;
1411 	list_work->state = QEDI_WORK_SCHEDULED;
1412 	qedi_cmd->list_tmf_work = list_work;
1413 
1414 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1415 		  "Queue tmf work=%p, list node=%p, cid=0x%x, tmf flags=0x%x\n",
1416 		  list_work->ptr_tmf_work, list_work, qedi_conn->iscsi_conn_id,
1417 		  tmf_hdr->flags);
1418 
1419 	spin_lock_bh(&qedi_conn->tmf_work_lock);
1420 	list_add_tail(&list_work->list, &qedi_conn->tmf_work_list);
1421 	spin_unlock_bh(&qedi_conn->tmf_work_lock);
1422 
1423 	qedi_iscsi_cleanup_task(ctask, false);
1424 
1425 	rval = qedi_wait_for_cleanup_request(qedi, qedi_conn, ctask, qedi_cmd,
1426 					     list_work);
1427 	if (rval == -1) {
1428 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1429 			  "FW cleanup got escalated, cid=0x%x\n",
1430 			  qedi_conn->iscsi_conn_id);
1431 		goto ldel_exit;
1432 	}
1433 
1434 	tid = qedi_get_task_idx(qedi);
1435 	if (tid == -1) {
1436 		QEDI_ERR(&qedi->dbg_ctx, "Invalid tid, cid=0x%x\n",
1437 			 qedi_conn->iscsi_conn_id);
1438 		goto ldel_exit;
1439 	}
1440 
1441 	qedi_cmd->task_id = tid;
1442 	qedi_send_iscsi_tmf(qedi_conn, qedi_cmd->task);
1443 
1444 abort_ret:
1445 	clear_bit(QEDI_CONN_FW_CLEANUP, &qedi_conn->flags);
1446 	return;
1447 
1448 ldel_exit:
1449 	spin_lock_bh(&qedi_conn->tmf_work_lock);
1450 	if (!qedi_cmd->list_tmf_work) {
1451 		list_del_init(&list_work->list);
1452 		qedi_cmd->list_tmf_work = NULL;
1453 		kfree(list_work);
1454 	}
1455 	spin_unlock_bh(&qedi_conn->tmf_work_lock);
1456 
1457 	spin_lock(&qedi_conn->list_lock);
1458 	list_del_init(&cmd->io_cmd);
1459 	qedi_conn->active_cmd_count--;
1460 	spin_unlock(&qedi_conn->list_lock);
1461 
1462 	clear_bit(QEDI_CONN_FW_CLEANUP, &qedi_conn->flags);
1463 }
1464 
1465 static int qedi_send_iscsi_tmf(struct qedi_conn *qedi_conn,
1466 			       struct iscsi_task *mtask)
1467 {
1468 	struct iscsi_tmf_request_hdr tmf_pdu_header;
1469 	struct iscsi_task_params task_params;
1470 	struct qedi_ctx *qedi = qedi_conn->qedi;
1471 	struct e4_iscsi_task_context *fw_task_ctx;
1472 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
1473 	struct iscsi_task *ctask;
1474 	struct iscsi_tm *tmf_hdr;
1475 	struct qedi_cmd *qedi_cmd;
1476 	struct qedi_cmd *cmd;
1477 	struct qedi_endpoint *ep;
1478 	u32 scsi_lun[2];
1479 	s16 tid = 0;
1480 	u16 sq_idx = 0;
1481 	int rval = 0;
1482 
1483 	tmf_hdr = (struct iscsi_tm *)mtask->hdr;
1484 	qedi_cmd = (struct qedi_cmd *)mtask->dd_data;
1485 	ep = qedi_conn->ep;
1486 	if (!ep)
1487 		return -ENODEV;
1488 
1489 	tid = qedi_get_task_idx(qedi);
1490 	if (tid == -1)
1491 		return -ENOMEM;
1492 
1493 	fw_task_ctx =
1494 	     (struct e4_iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1495 							       tid);
1496 	memset(fw_task_ctx, 0, sizeof(struct e4_iscsi_task_context));
1497 
1498 	qedi_cmd->task_id = tid;
1499 
1500 	memset(&task_params, 0, sizeof(task_params));
1501 	memset(&tmf_pdu_header, 0, sizeof(tmf_pdu_header));
1502 
1503 	/* Update header info */
1504 	qedi_update_itt_map(qedi, tid, mtask->itt, qedi_cmd);
1505 	tmf_pdu_header.itt = qedi_set_itt(tid, get_itt(mtask->itt));
1506 	tmf_pdu_header.cmd_sn = be32_to_cpu(tmf_hdr->cmdsn);
1507 
1508 	memcpy(scsi_lun, &tmf_hdr->lun, sizeof(struct scsi_lun));
1509 	tmf_pdu_header.lun.lo = be32_to_cpu(scsi_lun[0]);
1510 	tmf_pdu_header.lun.hi = be32_to_cpu(scsi_lun[1]);
1511 
1512 	if ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1513 	     ISCSI_TM_FUNC_ABORT_TASK) {
1514 		ctask = iscsi_itt_to_task(conn, tmf_hdr->rtt);
1515 		if (!ctask || !ctask->sc) {
1516 			QEDI_ERR(&qedi->dbg_ctx,
1517 				 "Could not get reference task\n");
1518 			return 0;
1519 		}
1520 		cmd = (struct qedi_cmd *)ctask->dd_data;
1521 		tmf_pdu_header.rtt =
1522 				qedi_set_itt(cmd->task_id,
1523 					     get_itt(tmf_hdr->rtt));
1524 	} else {
1525 		tmf_pdu_header.rtt = ISCSI_RESERVED_TAG;
1526 	}
1527 
1528 	tmf_pdu_header.opcode = tmf_hdr->opcode;
1529 	tmf_pdu_header.function = tmf_hdr->flags;
1530 	tmf_pdu_header.hdr_second_dword = ntoh24(tmf_hdr->dlength);
1531 	tmf_pdu_header.ref_cmd_sn = be32_to_cpu(tmf_hdr->refcmdsn);
1532 
1533 	/* Fill fw input params */
1534 	task_params.context = fw_task_ctx;
1535 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1536 	task_params.itid = tid;
1537 	task_params.cq_rss_number = 0;
1538 	task_params.tx_io_size = 0;
1539 	task_params.rx_io_size = 0;
1540 
1541 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1542 	task_params.sqe = &ep->sq[sq_idx];
1543 
1544 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1545 	rval = init_initiator_tmf_request_task(&task_params,
1546 					       &tmf_pdu_header);
1547 	if (rval)
1548 		return -1;
1549 
1550 	spin_lock(&qedi_conn->list_lock);
1551 	list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1552 	qedi_cmd->io_cmd_in_list = true;
1553 	qedi_conn->active_cmd_count++;
1554 	spin_unlock(&qedi_conn->list_lock);
1555 
1556 	qedi_ring_doorbell(qedi_conn);
1557 	return 0;
1558 }
1559 
1560 int qedi_iscsi_abort_work(struct qedi_conn *qedi_conn,
1561 			  struct iscsi_task *mtask)
1562 {
1563 	struct qedi_ctx *qedi = qedi_conn->qedi;
1564 	struct iscsi_tm *tmf_hdr;
1565 	struct qedi_cmd *qedi_cmd = (struct qedi_cmd *)mtask->dd_data;
1566 	s16 tid = 0;
1567 
1568 	tmf_hdr = (struct iscsi_tm *)mtask->hdr;
1569 	qedi_cmd->task = mtask;
1570 
1571 	/* If abort task then schedule the work and return */
1572 	if ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1573 	    ISCSI_TM_FUNC_ABORT_TASK) {
1574 		qedi_cmd->state = CLEANUP_WAIT;
1575 		INIT_WORK(&qedi_cmd->tmf_work, qedi_tmf_work);
1576 		queue_work(qedi->tmf_thread, &qedi_cmd->tmf_work);
1577 
1578 	} else if (((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1579 		    ISCSI_TM_FUNC_LOGICAL_UNIT_RESET) ||
1580 		   ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1581 		    ISCSI_TM_FUNC_TARGET_WARM_RESET) ||
1582 		   ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1583 		    ISCSI_TM_FUNC_TARGET_COLD_RESET)) {
1584 		tid = qedi_get_task_idx(qedi);
1585 		if (tid == -1) {
1586 			QEDI_ERR(&qedi->dbg_ctx, "Invalid tid, cid=0x%x\n",
1587 				 qedi_conn->iscsi_conn_id);
1588 			return -1;
1589 		}
1590 		qedi_cmd->task_id = tid;
1591 
1592 		qedi_send_iscsi_tmf(qedi_conn, qedi_cmd->task);
1593 
1594 	} else {
1595 		QEDI_ERR(&qedi->dbg_ctx, "Invalid tmf, cid=0x%x\n",
1596 			 qedi_conn->iscsi_conn_id);
1597 		return -1;
1598 	}
1599 
1600 	return 0;
1601 }
1602 
1603 int qedi_send_iscsi_text(struct qedi_conn *qedi_conn,
1604 			 struct iscsi_task *task)
1605 {
1606 	struct iscsi_text_request_hdr text_request_pdu_header;
1607 	struct scsi_sgl_task_params tx_sgl_task_params;
1608 	struct scsi_sgl_task_params rx_sgl_task_params;
1609 	struct iscsi_task_params task_params;
1610 	struct e4_iscsi_task_context *fw_task_ctx;
1611 	struct qedi_ctx *qedi = qedi_conn->qedi;
1612 	struct iscsi_text *text_hdr;
1613 	struct scsi_sge *req_sge = NULL;
1614 	struct scsi_sge *resp_sge = NULL;
1615 	struct qedi_cmd *qedi_cmd;
1616 	struct qedi_endpoint *ep;
1617 	s16 tid = 0;
1618 	u16 sq_idx = 0;
1619 	int rval = 0;
1620 
1621 	req_sge = (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
1622 	resp_sge = (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1623 	qedi_cmd = (struct qedi_cmd *)task->dd_data;
1624 	text_hdr = (struct iscsi_text *)task->hdr;
1625 	ep = qedi_conn->ep;
1626 
1627 	tid = qedi_get_task_idx(qedi);
1628 	if (tid == -1)
1629 		return -ENOMEM;
1630 
1631 	fw_task_ctx =
1632 	     (struct e4_iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1633 							       tid);
1634 	memset(fw_task_ctx, 0, sizeof(struct e4_iscsi_task_context));
1635 
1636 	qedi_cmd->task_id = tid;
1637 
1638 	memset(&task_params, 0, sizeof(task_params));
1639 	memset(&text_request_pdu_header, 0, sizeof(text_request_pdu_header));
1640 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
1641 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
1642 
1643 	/* Update header info */
1644 	text_request_pdu_header.opcode = text_hdr->opcode;
1645 	text_request_pdu_header.flags_attr = text_hdr->flags;
1646 
1647 	qedi_update_itt_map(qedi, tid, task->itt, qedi_cmd);
1648 	text_request_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
1649 	text_request_pdu_header.ttt = text_hdr->ttt;
1650 	text_request_pdu_header.cmd_sn = be32_to_cpu(text_hdr->cmdsn);
1651 	text_request_pdu_header.exp_stat_sn = be32_to_cpu(text_hdr->exp_statsn);
1652 	text_request_pdu_header.hdr_second_dword = ntoh24(text_hdr->dlength);
1653 
1654 	/* Fill tx AHS and rx buffer */
1655 	tx_sgl_task_params.sgl =
1656 			       (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
1657 	tx_sgl_task_params.sgl_phys_addr.lo =
1658 					 (u32)(qedi_conn->gen_pdu.req_dma_addr);
1659 	tx_sgl_task_params.sgl_phys_addr.hi =
1660 			      (u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
1661 	tx_sgl_task_params.total_buffer_size = req_sge->sge_len;
1662 	tx_sgl_task_params.num_sges = 1;
1663 
1664 	rx_sgl_task_params.sgl =
1665 			      (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1666 	rx_sgl_task_params.sgl_phys_addr.lo =
1667 					(u32)(qedi_conn->gen_pdu.resp_dma_addr);
1668 	rx_sgl_task_params.sgl_phys_addr.hi =
1669 			     (u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
1670 	rx_sgl_task_params.total_buffer_size = resp_sge->sge_len;
1671 	rx_sgl_task_params.num_sges = 1;
1672 
1673 	/* Fill fw input params */
1674 	task_params.context = fw_task_ctx;
1675 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1676 	task_params.itid = tid;
1677 	task_params.cq_rss_number = 0;
1678 	task_params.tx_io_size = ntoh24(text_hdr->dlength);
1679 	task_params.rx_io_size = resp_sge->sge_len;
1680 
1681 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1682 	task_params.sqe = &ep->sq[sq_idx];
1683 
1684 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1685 	rval = init_initiator_text_request_task(&task_params,
1686 						&text_request_pdu_header,
1687 						&tx_sgl_task_params,
1688 						&rx_sgl_task_params);
1689 	if (rval)
1690 		return -1;
1691 
1692 	spin_lock(&qedi_conn->list_lock);
1693 	list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1694 	qedi_cmd->io_cmd_in_list = true;
1695 	qedi_conn->active_cmd_count++;
1696 	spin_unlock(&qedi_conn->list_lock);
1697 
1698 	qedi_ring_doorbell(qedi_conn);
1699 	return 0;
1700 }
1701 
1702 int qedi_send_iscsi_nopout(struct qedi_conn *qedi_conn,
1703 			   struct iscsi_task *task,
1704 			   char *datap, int data_len, int unsol)
1705 {
1706 	struct iscsi_nop_out_hdr nop_out_pdu_header;
1707 	struct scsi_sgl_task_params tx_sgl_task_params;
1708 	struct scsi_sgl_task_params rx_sgl_task_params;
1709 	struct iscsi_task_params task_params;
1710 	struct qedi_ctx *qedi = qedi_conn->qedi;
1711 	struct e4_iscsi_task_context *fw_task_ctx;
1712 	struct iscsi_nopout *nopout_hdr;
1713 	struct scsi_sge *resp_sge = NULL;
1714 	struct qedi_cmd *qedi_cmd;
1715 	struct qedi_endpoint *ep;
1716 	u32 scsi_lun[2];
1717 	s16 tid = 0;
1718 	u16 sq_idx = 0;
1719 	int rval = 0;
1720 
1721 	resp_sge = (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1722 	qedi_cmd = (struct qedi_cmd *)task->dd_data;
1723 	nopout_hdr = (struct iscsi_nopout *)task->hdr;
1724 	ep = qedi_conn->ep;
1725 
1726 	tid = qedi_get_task_idx(qedi);
1727 	if (tid == -1)
1728 		return -ENOMEM;
1729 
1730 	fw_task_ctx =
1731 	     (struct e4_iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1732 							       tid);
1733 	memset(fw_task_ctx, 0, sizeof(struct e4_iscsi_task_context));
1734 
1735 	qedi_cmd->task_id = tid;
1736 
1737 	memset(&task_params, 0, sizeof(task_params));
1738 	memset(&nop_out_pdu_header, 0, sizeof(nop_out_pdu_header));
1739 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
1740 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
1741 
1742 	/* Update header info */
1743 	nop_out_pdu_header.opcode = nopout_hdr->opcode;
1744 	SET_FIELD(nop_out_pdu_header.flags_attr, ISCSI_NOP_OUT_HDR_CONST1, 1);
1745 	SET_FIELD(nop_out_pdu_header.flags_attr, ISCSI_NOP_OUT_HDR_RSRV, 0);
1746 
1747 	memcpy(scsi_lun, &nopout_hdr->lun, sizeof(struct scsi_lun));
1748 	nop_out_pdu_header.lun.lo = be32_to_cpu(scsi_lun[0]);
1749 	nop_out_pdu_header.lun.hi = be32_to_cpu(scsi_lun[1]);
1750 	nop_out_pdu_header.cmd_sn = be32_to_cpu(nopout_hdr->cmdsn);
1751 	nop_out_pdu_header.exp_stat_sn = be32_to_cpu(nopout_hdr->exp_statsn);
1752 
1753 	qedi_update_itt_map(qedi, tid, task->itt, qedi_cmd);
1754 
1755 	if (nopout_hdr->ttt != ISCSI_TTT_ALL_ONES) {
1756 		nop_out_pdu_header.itt = be32_to_cpu(nopout_hdr->itt);
1757 		nop_out_pdu_header.ttt = be32_to_cpu(nopout_hdr->ttt);
1758 	} else {
1759 		nop_out_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
1760 		nop_out_pdu_header.ttt = ISCSI_TTT_ALL_ONES;
1761 
1762 		spin_lock(&qedi_conn->list_lock);
1763 		list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1764 		qedi_cmd->io_cmd_in_list = true;
1765 		qedi_conn->active_cmd_count++;
1766 		spin_unlock(&qedi_conn->list_lock);
1767 	}
1768 
1769 	/* Fill tx AHS and rx buffer */
1770 	if (data_len) {
1771 		tx_sgl_task_params.sgl =
1772 			       (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
1773 		tx_sgl_task_params.sgl_phys_addr.lo =
1774 					 (u32)(qedi_conn->gen_pdu.req_dma_addr);
1775 		tx_sgl_task_params.sgl_phys_addr.hi =
1776 			      (u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
1777 		tx_sgl_task_params.total_buffer_size = data_len;
1778 		tx_sgl_task_params.num_sges = 1;
1779 
1780 		rx_sgl_task_params.sgl =
1781 			      (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1782 		rx_sgl_task_params.sgl_phys_addr.lo =
1783 					(u32)(qedi_conn->gen_pdu.resp_dma_addr);
1784 		rx_sgl_task_params.sgl_phys_addr.hi =
1785 			     (u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
1786 		rx_sgl_task_params.total_buffer_size = resp_sge->sge_len;
1787 		rx_sgl_task_params.num_sges = 1;
1788 	}
1789 
1790 	/* Fill fw input params */
1791 	task_params.context = fw_task_ctx;
1792 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1793 	task_params.itid = tid;
1794 	task_params.cq_rss_number = 0;
1795 	task_params.tx_io_size = data_len;
1796 	task_params.rx_io_size = resp_sge->sge_len;
1797 
1798 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1799 	task_params.sqe = &ep->sq[sq_idx];
1800 
1801 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1802 	rval = init_initiator_nop_out_task(&task_params,
1803 					   &nop_out_pdu_header,
1804 					   &tx_sgl_task_params,
1805 					   &rx_sgl_task_params);
1806 	if (rval)
1807 		return -1;
1808 
1809 	qedi_ring_doorbell(qedi_conn);
1810 	return 0;
1811 }
1812 
1813 static int qedi_split_bd(struct qedi_cmd *cmd, u64 addr, int sg_len,
1814 			 int bd_index)
1815 {
1816 	struct scsi_sge *bd = cmd->io_tbl.sge_tbl;
1817 	int frag_size, sg_frags;
1818 
1819 	sg_frags = 0;
1820 
1821 	while (sg_len) {
1822 		if (addr % QEDI_PAGE_SIZE)
1823 			frag_size =
1824 				   (QEDI_PAGE_SIZE - (addr % QEDI_PAGE_SIZE));
1825 		else
1826 			frag_size = (sg_len > QEDI_BD_SPLIT_SZ) ? 0 :
1827 				    (sg_len % QEDI_BD_SPLIT_SZ);
1828 
1829 		if (frag_size == 0)
1830 			frag_size = QEDI_BD_SPLIT_SZ;
1831 
1832 		bd[bd_index + sg_frags].sge_addr.lo = (addr & 0xffffffff);
1833 		bd[bd_index + sg_frags].sge_addr.hi = (addr >> 32);
1834 		bd[bd_index + sg_frags].sge_len = (u16)frag_size;
1835 		QEDI_INFO(&cmd->conn->qedi->dbg_ctx, QEDI_LOG_IO,
1836 			  "split sge %d: addr=%llx, len=%x",
1837 			  (bd_index + sg_frags), addr, frag_size);
1838 
1839 		addr += (u64)frag_size;
1840 		sg_frags++;
1841 		sg_len -= frag_size;
1842 	}
1843 	return sg_frags;
1844 }
1845 
1846 static int qedi_map_scsi_sg(struct qedi_ctx *qedi, struct qedi_cmd *cmd)
1847 {
1848 	struct scsi_cmnd *sc = cmd->scsi_cmd;
1849 	struct scsi_sge *bd = cmd->io_tbl.sge_tbl;
1850 	struct scatterlist *sg;
1851 	int byte_count = 0;
1852 	int bd_count = 0;
1853 	int sg_count;
1854 	int sg_len;
1855 	int sg_frags;
1856 	u64 addr, end_addr;
1857 	int i;
1858 
1859 	WARN_ON(scsi_sg_count(sc) > QEDI_ISCSI_MAX_BDS_PER_CMD);
1860 
1861 	sg_count = dma_map_sg(&qedi->pdev->dev, scsi_sglist(sc),
1862 			      scsi_sg_count(sc), sc->sc_data_direction);
1863 
1864 	/*
1865 	 * New condition to send single SGE as cached-SGL.
1866 	 * Single SGE with length less than 64K.
1867 	 */
1868 	sg = scsi_sglist(sc);
1869 	if ((sg_count == 1) && (sg_dma_len(sg) <= MAX_SGLEN_FOR_CACHESGL)) {
1870 		sg_len = sg_dma_len(sg);
1871 		addr = (u64)sg_dma_address(sg);
1872 
1873 		bd[bd_count].sge_addr.lo = (addr & 0xffffffff);
1874 		bd[bd_count].sge_addr.hi = (addr >> 32);
1875 		bd[bd_count].sge_len = (u16)sg_len;
1876 
1877 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
1878 			  "single-cached-sgl: bd_count:%d addr=%llx, len=%x",
1879 			  sg_count, addr, sg_len);
1880 
1881 		return ++bd_count;
1882 	}
1883 
1884 	scsi_for_each_sg(sc, sg, sg_count, i) {
1885 		sg_len = sg_dma_len(sg);
1886 		addr = (u64)sg_dma_address(sg);
1887 		end_addr = (addr + sg_len);
1888 
1889 		/*
1890 		 * first sg elem in the 'list',
1891 		 * check if end addr is page-aligned.
1892 		 */
1893 		if ((i == 0) && (sg_count > 1) && (end_addr % QEDI_PAGE_SIZE))
1894 			cmd->use_slowpath = true;
1895 
1896 		/*
1897 		 * last sg elem in the 'list',
1898 		 * check if start addr is page-aligned.
1899 		 */
1900 		else if ((i == (sg_count - 1)) &&
1901 			 (sg_count > 1) && (addr % QEDI_PAGE_SIZE))
1902 			cmd->use_slowpath = true;
1903 
1904 		/*
1905 		 * middle sg elements in list,
1906 		 * check if start and end addr is page-aligned
1907 		 */
1908 		else if ((i != 0) && (i != (sg_count - 1)) &&
1909 			 ((addr % QEDI_PAGE_SIZE) ||
1910 			 (end_addr % QEDI_PAGE_SIZE)))
1911 			cmd->use_slowpath = true;
1912 
1913 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO, "sg[%d] size=0x%x",
1914 			  i, sg_len);
1915 
1916 		if (sg_len > QEDI_BD_SPLIT_SZ) {
1917 			sg_frags = qedi_split_bd(cmd, addr, sg_len, bd_count);
1918 		} else {
1919 			sg_frags = 1;
1920 			bd[bd_count].sge_addr.lo = addr & 0xffffffff;
1921 			bd[bd_count].sge_addr.hi = addr >> 32;
1922 			bd[bd_count].sge_len = sg_len;
1923 		}
1924 		byte_count += sg_len;
1925 		bd_count += sg_frags;
1926 	}
1927 
1928 	if (byte_count != scsi_bufflen(sc))
1929 		QEDI_ERR(&qedi->dbg_ctx,
1930 			 "byte_count = %d != scsi_bufflen = %d\n", byte_count,
1931 			 scsi_bufflen(sc));
1932 	else
1933 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO, "byte_count = %d\n",
1934 			  byte_count);
1935 
1936 	WARN_ON(byte_count != scsi_bufflen(sc));
1937 
1938 	return bd_count;
1939 }
1940 
1941 static void qedi_iscsi_map_sg_list(struct qedi_cmd *cmd)
1942 {
1943 	int bd_count;
1944 	struct scsi_cmnd *sc = cmd->scsi_cmd;
1945 
1946 	if (scsi_sg_count(sc)) {
1947 		bd_count  = qedi_map_scsi_sg(cmd->conn->qedi, cmd);
1948 		if (bd_count == 0)
1949 			return;
1950 	} else {
1951 		struct scsi_sge *bd = cmd->io_tbl.sge_tbl;
1952 
1953 		bd[0].sge_addr.lo = 0;
1954 		bd[0].sge_addr.hi = 0;
1955 		bd[0].sge_len = 0;
1956 		bd_count = 0;
1957 	}
1958 	cmd->io_tbl.sge_valid = bd_count;
1959 }
1960 
1961 static void qedi_cpy_scsi_cdb(struct scsi_cmnd *sc, u32 *dstp)
1962 {
1963 	u32 dword;
1964 	int lpcnt;
1965 	u8 *srcp;
1966 
1967 	lpcnt = sc->cmd_len / sizeof(dword);
1968 	srcp = (u8 *)sc->cmnd;
1969 	while (lpcnt--) {
1970 		memcpy(&dword, (const void *)srcp, 4);
1971 		*dstp = cpu_to_be32(dword);
1972 		srcp += 4;
1973 		dstp++;
1974 	}
1975 	if (sc->cmd_len & 0x3) {
1976 		dword = (u32)srcp[0] | ((u32)srcp[1] << 8);
1977 		*dstp = cpu_to_be32(dword);
1978 	}
1979 }
1980 
1981 void qedi_trace_io(struct qedi_ctx *qedi, struct iscsi_task *task,
1982 		   u16 tid, int8_t direction)
1983 {
1984 	struct qedi_io_log *io_log;
1985 	struct iscsi_conn *conn = task->conn;
1986 	struct qedi_conn *qedi_conn = conn->dd_data;
1987 	struct scsi_cmnd *sc_cmd = task->sc;
1988 	unsigned long flags;
1989 
1990 	spin_lock_irqsave(&qedi->io_trace_lock, flags);
1991 
1992 	io_log = &qedi->io_trace_buf[qedi->io_trace_idx];
1993 	io_log->direction = direction;
1994 	io_log->task_id = tid;
1995 	io_log->cid = qedi_conn->iscsi_conn_id;
1996 	io_log->lun = sc_cmd->device->lun;
1997 	io_log->op = sc_cmd->cmnd[0];
1998 	io_log->lba[0] = sc_cmd->cmnd[2];
1999 	io_log->lba[1] = sc_cmd->cmnd[3];
2000 	io_log->lba[2] = sc_cmd->cmnd[4];
2001 	io_log->lba[3] = sc_cmd->cmnd[5];
2002 	io_log->bufflen = scsi_bufflen(sc_cmd);
2003 	io_log->sg_count = scsi_sg_count(sc_cmd);
2004 	io_log->fast_sgs = qedi->fast_sgls;
2005 	io_log->cached_sgs = qedi->cached_sgls;
2006 	io_log->slow_sgs = qedi->slow_sgls;
2007 	io_log->cached_sge = qedi->use_cached_sge;
2008 	io_log->slow_sge = qedi->use_slow_sge;
2009 	io_log->fast_sge = qedi->use_fast_sge;
2010 	io_log->result = sc_cmd->result;
2011 	io_log->jiffies = jiffies;
2012 	io_log->blk_req_cpu = smp_processor_id();
2013 
2014 	if (direction == QEDI_IO_TRACE_REQ) {
2015 		/* For requests we only care about the submission CPU */
2016 		io_log->req_cpu = smp_processor_id() % qedi->num_queues;
2017 		io_log->intr_cpu = 0;
2018 		io_log->blk_rsp_cpu = 0;
2019 	} else if (direction == QEDI_IO_TRACE_RSP) {
2020 		io_log->req_cpu = smp_processor_id() % qedi->num_queues;
2021 		io_log->intr_cpu = qedi->intr_cpu;
2022 		io_log->blk_rsp_cpu = smp_processor_id();
2023 	}
2024 
2025 	qedi->io_trace_idx++;
2026 	if (qedi->io_trace_idx == QEDI_IO_TRACE_SIZE)
2027 		qedi->io_trace_idx = 0;
2028 
2029 	qedi->use_cached_sge = false;
2030 	qedi->use_slow_sge = false;
2031 	qedi->use_fast_sge = false;
2032 
2033 	spin_unlock_irqrestore(&qedi->io_trace_lock, flags);
2034 }
2035 
2036 int qedi_iscsi_send_ioreq(struct iscsi_task *task)
2037 {
2038 	struct iscsi_conn *conn = task->conn;
2039 	struct iscsi_session *session = conn->session;
2040 	struct Scsi_Host *shost = iscsi_session_to_shost(session->cls_session);
2041 	struct qedi_ctx *qedi = iscsi_host_priv(shost);
2042 	struct qedi_conn *qedi_conn = conn->dd_data;
2043 	struct qedi_cmd *cmd = task->dd_data;
2044 	struct scsi_cmnd *sc = task->sc;
2045 	struct iscsi_cmd_hdr cmd_pdu_header;
2046 	struct scsi_sgl_task_params tx_sgl_task_params;
2047 	struct scsi_sgl_task_params rx_sgl_task_params;
2048 	struct scsi_sgl_task_params *prx_sgl = NULL;
2049 	struct scsi_sgl_task_params *ptx_sgl = NULL;
2050 	struct iscsi_task_params task_params;
2051 	struct iscsi_conn_params conn_params;
2052 	struct scsi_initiator_cmd_params cmd_params;
2053 	struct e4_iscsi_task_context *fw_task_ctx;
2054 	struct iscsi_cls_conn *cls_conn;
2055 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)task->hdr;
2056 	enum iscsi_task_type task_type = MAX_ISCSI_TASK_TYPE;
2057 	struct qedi_endpoint *ep;
2058 	u32 scsi_lun[2];
2059 	s16 tid = 0;
2060 	u16 sq_idx = 0;
2061 	u16 cq_idx;
2062 	int rval = 0;
2063 
2064 	ep = qedi_conn->ep;
2065 	cls_conn = qedi_conn->cls_conn;
2066 	conn = cls_conn->dd_data;
2067 
2068 	qedi_iscsi_map_sg_list(cmd);
2069 	int_to_scsilun(sc->device->lun, (struct scsi_lun *)scsi_lun);
2070 
2071 	tid = qedi_get_task_idx(qedi);
2072 	if (tid == -1)
2073 		return -ENOMEM;
2074 
2075 	fw_task_ctx =
2076 	     (struct e4_iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
2077 							       tid);
2078 	memset(fw_task_ctx, 0, sizeof(struct e4_iscsi_task_context));
2079 
2080 	cmd->task_id = tid;
2081 
2082 	memset(&task_params, 0, sizeof(task_params));
2083 	memset(&cmd_pdu_header, 0, sizeof(cmd_pdu_header));
2084 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
2085 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
2086 	memset(&conn_params, 0, sizeof(conn_params));
2087 	memset(&cmd_params, 0, sizeof(cmd_params));
2088 
2089 	cq_idx = smp_processor_id() % qedi->num_queues;
2090 	/* Update header info */
2091 	SET_FIELD(cmd_pdu_header.flags_attr, ISCSI_CMD_HDR_ATTR,
2092 		  ISCSI_ATTR_SIMPLE);
2093 	if (hdr->cdb[0] != TEST_UNIT_READY) {
2094 		if (sc->sc_data_direction == DMA_TO_DEVICE) {
2095 			SET_FIELD(cmd_pdu_header.flags_attr,
2096 				  ISCSI_CMD_HDR_WRITE, 1);
2097 			task_type = ISCSI_TASK_TYPE_INITIATOR_WRITE;
2098 		} else {
2099 			SET_FIELD(cmd_pdu_header.flags_attr,
2100 				  ISCSI_CMD_HDR_READ, 1);
2101 			task_type = ISCSI_TASK_TYPE_INITIATOR_READ;
2102 		}
2103 	}
2104 
2105 	cmd_pdu_header.lun.lo = be32_to_cpu(scsi_lun[0]);
2106 	cmd_pdu_header.lun.hi = be32_to_cpu(scsi_lun[1]);
2107 
2108 	qedi_update_itt_map(qedi, tid, task->itt, cmd);
2109 	cmd_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
2110 	cmd_pdu_header.expected_transfer_length = cpu_to_be32(hdr->data_length);
2111 	cmd_pdu_header.hdr_second_dword = ntoh24(hdr->dlength);
2112 	cmd_pdu_header.cmd_sn = be32_to_cpu(hdr->cmdsn);
2113 	cmd_pdu_header.hdr_first_byte = hdr->opcode;
2114 	qedi_cpy_scsi_cdb(sc, (u32 *)cmd_pdu_header.cdb);
2115 
2116 	/* Fill tx AHS and rx buffer */
2117 	if (task_type == ISCSI_TASK_TYPE_INITIATOR_WRITE) {
2118 		tx_sgl_task_params.sgl = cmd->io_tbl.sge_tbl;
2119 		tx_sgl_task_params.sgl_phys_addr.lo =
2120 						 (u32)(cmd->io_tbl.sge_tbl_dma);
2121 		tx_sgl_task_params.sgl_phys_addr.hi =
2122 				      (u32)((u64)cmd->io_tbl.sge_tbl_dma >> 32);
2123 		tx_sgl_task_params.total_buffer_size = scsi_bufflen(sc);
2124 		tx_sgl_task_params.num_sges = cmd->io_tbl.sge_valid;
2125 		if (cmd->use_slowpath)
2126 			tx_sgl_task_params.small_mid_sge = true;
2127 	} else if (task_type == ISCSI_TASK_TYPE_INITIATOR_READ) {
2128 		rx_sgl_task_params.sgl = cmd->io_tbl.sge_tbl;
2129 		rx_sgl_task_params.sgl_phys_addr.lo =
2130 						 (u32)(cmd->io_tbl.sge_tbl_dma);
2131 		rx_sgl_task_params.sgl_phys_addr.hi =
2132 				      (u32)((u64)cmd->io_tbl.sge_tbl_dma >> 32);
2133 		rx_sgl_task_params.total_buffer_size = scsi_bufflen(sc);
2134 		rx_sgl_task_params.num_sges = cmd->io_tbl.sge_valid;
2135 	}
2136 
2137 	/* Add conn param */
2138 	conn_params.first_burst_length = conn->session->first_burst;
2139 	conn_params.max_send_pdu_length = conn->max_xmit_dlength;
2140 	conn_params.max_burst_length = conn->session->max_burst;
2141 	if (conn->session->initial_r2t_en)
2142 		conn_params.initial_r2t = true;
2143 	if (conn->session->imm_data_en)
2144 		conn_params.immediate_data = true;
2145 
2146 	/* Add cmd params */
2147 	cmd_params.sense_data_buffer_phys_addr.lo = (u32)cmd->sense_buffer_dma;
2148 	cmd_params.sense_data_buffer_phys_addr.hi =
2149 					(u32)((u64)cmd->sense_buffer_dma >> 32);
2150 	/* Fill fw input params */
2151 	task_params.context = fw_task_ctx;
2152 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
2153 	task_params.itid = tid;
2154 	task_params.cq_rss_number = cq_idx;
2155 	if (task_type == ISCSI_TASK_TYPE_INITIATOR_WRITE)
2156 		task_params.tx_io_size = scsi_bufflen(sc);
2157 	else if (task_type == ISCSI_TASK_TYPE_INITIATOR_READ)
2158 		task_params.rx_io_size = scsi_bufflen(sc);
2159 
2160 	sq_idx = qedi_get_wqe_idx(qedi_conn);
2161 	task_params.sqe = &ep->sq[sq_idx];
2162 
2163 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
2164 		  "%s: %s-SGL: sg_len=0x%x num_sges=0x%x first-sge-lo=0x%x first-sge-hi=0x%x\n",
2165 		  (task_type == ISCSI_TASK_TYPE_INITIATOR_WRITE) ?
2166 		  "Write " : "Read ", (cmd->io_tbl.sge_valid == 1) ?
2167 		  "Single" : (cmd->use_slowpath ? "SLOW" : "FAST"),
2168 		  (u16)cmd->io_tbl.sge_valid, scsi_bufflen(sc),
2169 		  (u32)(cmd->io_tbl.sge_tbl_dma),
2170 		  (u32)((u64)cmd->io_tbl.sge_tbl_dma >> 32));
2171 
2172 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
2173 
2174 	if (task_params.tx_io_size != 0)
2175 		ptx_sgl = &tx_sgl_task_params;
2176 	if (task_params.rx_io_size != 0)
2177 		prx_sgl = &rx_sgl_task_params;
2178 
2179 	rval = init_initiator_rw_iscsi_task(&task_params, &conn_params,
2180 					    &cmd_params, &cmd_pdu_header,
2181 					    ptx_sgl, prx_sgl,
2182 					    NULL);
2183 	if (rval)
2184 		return -1;
2185 
2186 	spin_lock(&qedi_conn->list_lock);
2187 	list_add_tail(&cmd->io_cmd, &qedi_conn->active_cmd_list);
2188 	cmd->io_cmd_in_list = true;
2189 	qedi_conn->active_cmd_count++;
2190 	spin_unlock(&qedi_conn->list_lock);
2191 
2192 	qedi_ring_doorbell(qedi_conn);
2193 	return 0;
2194 }
2195 
2196 int qedi_iscsi_cleanup_task(struct iscsi_task *task, bool mark_cmd_node_deleted)
2197 {
2198 	struct iscsi_task_params task_params;
2199 	struct qedi_endpoint *ep;
2200 	struct iscsi_conn *conn = task->conn;
2201 	struct qedi_conn *qedi_conn = conn->dd_data;
2202 	struct qedi_cmd *cmd = task->dd_data;
2203 	u16 sq_idx = 0;
2204 	int rval = 0;
2205 
2206 	QEDI_INFO(&qedi_conn->qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
2207 		  "issue cleanup tid=0x%x itt=0x%x task_state=%d cmd_state=0%x cid=0x%x\n",
2208 		  cmd->task_id, get_itt(task->itt), task->state,
2209 		  cmd->state, qedi_conn->iscsi_conn_id);
2210 
2211 	memset(&task_params, 0, sizeof(task_params));
2212 	ep = qedi_conn->ep;
2213 
2214 	sq_idx = qedi_get_wqe_idx(qedi_conn);
2215 
2216 	task_params.sqe = &ep->sq[sq_idx];
2217 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
2218 	task_params.itid = cmd->task_id;
2219 
2220 	rval = init_cleanup_task(&task_params);
2221 	if (rval)
2222 		return rval;
2223 
2224 	qedi_ring_doorbell(qedi_conn);
2225 	return 0;
2226 }
2227