xref: /openbmc/linux/drivers/scsi/qedi/qedi_fw.c (revision e3b9f1e8)
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->special) {
620 		QEDI_WARN(&qedi->dbg_ctx,
621 			  "request->special is NULL so request not valid, sc_cmd=%p.\n",
622 			  sc_cmd);
623 		goto error;
624 	}
625 
626 	if (!sc_cmd->request->q) {
627 		QEDI_WARN(&qedi->dbg_ctx,
628 			  "request->q is NULL so request is not valid, sc_cmd=%p.\n",
629 			  sc_cmd);
630 		goto error;
631 	}
632 
633 	qedi_iscsi_unmap_sg_list(cmd);
634 
635 	hdr = (struct iscsi_scsi_rsp *)task->hdr;
636 	hdr->opcode = cqe_data_in->opcode;
637 	hdr->max_cmdsn = cpu_to_be32(cqe_data_in->max_cmd_sn);
638 	hdr->exp_cmdsn = cpu_to_be32(cqe_data_in->exp_cmd_sn);
639 	hdr->itt = build_itt(cqe->cqe_solicited.itid, conn->session->age);
640 	hdr->response = cqe_data_in->reserved1;
641 	hdr->cmd_status = cqe_data_in->status_rsvd;
642 	hdr->flags = cqe_data_in->flags;
643 	hdr->residual_count = cpu_to_be32(cqe_data_in->residual_count);
644 
645 	if (hdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
646 		datalen = cqe_data_in->reserved2 &
647 			  ISCSI_COMMON_HDR_DATA_SEG_LEN_MASK;
648 		memcpy((char *)conn->data, (char *)cmd->sense_buffer, datalen);
649 	}
650 
651 	/* If f/w reports data underrun err then set residual to IO transfer
652 	 * length, set Underrun flag and clear Overrun flag explicitly
653 	 */
654 	if (unlikely(cqe_err_bits &&
655 		     GET_FIELD(cqe_err_bits, CQE_ERROR_BITMAP_UNDER_RUN_ERR))) {
656 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
657 			  "Under flow itt=0x%x proto flags=0x%x tid=0x%x cid 0x%x fw resid 0x%x sc dlen 0x%x\n",
658 			  hdr->itt, cqe_data_in->flags, cmd->task_id,
659 			  qedi_conn->iscsi_conn_id, hdr->residual_count,
660 			  scsi_bufflen(sc_cmd));
661 		hdr->residual_count = cpu_to_be32(scsi_bufflen(sc_cmd));
662 		hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
663 		hdr->flags &= (~ISCSI_FLAG_CMD_OVERFLOW);
664 	}
665 
666 	spin_lock(&qedi_conn->list_lock);
667 	if (likely(cmd->io_cmd_in_list)) {
668 		cmd->io_cmd_in_list = false;
669 		list_del_init(&cmd->io_cmd);
670 		qedi_conn->active_cmd_count--;
671 	}
672 	spin_unlock(&qedi_conn->list_lock);
673 
674 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_TID,
675 		  "Freeing tid=0x%x for cid=0x%x\n",
676 		  cmd->task_id, qedi_conn->iscsi_conn_id);
677 	cmd->state = RESPONSE_RECEIVED;
678 	if (qedi_io_tracing)
679 		qedi_trace_io(qedi, task, cmd->task_id, QEDI_IO_TRACE_RSP);
680 
681 	qedi_clear_task_idx(qedi, cmd->task_id);
682 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr,
683 			     conn->data, datalen);
684 error:
685 	spin_unlock_bh(&session->back_lock);
686 }
687 
688 static void qedi_mtask_completion(struct qedi_ctx *qedi,
689 				  union iscsi_cqe *cqe,
690 				  struct iscsi_task *task,
691 				  struct qedi_conn *conn, uint16_t que_idx)
692 {
693 	struct iscsi_conn *iscsi_conn;
694 	u32 hdr_opcode;
695 
696 	hdr_opcode = cqe->cqe_common.iscsi_hdr.common.hdr_first_byte;
697 	iscsi_conn = conn->cls_conn->dd_data;
698 
699 	switch (hdr_opcode) {
700 	case ISCSI_OPCODE_SCSI_RESPONSE:
701 	case ISCSI_OPCODE_DATA_IN:
702 		qedi_scsi_completion(qedi, cqe, task, iscsi_conn);
703 		break;
704 	case ISCSI_OPCODE_LOGIN_RESPONSE:
705 		qedi_process_login_resp(qedi, cqe, task, conn);
706 		break;
707 	case ISCSI_OPCODE_TMF_RESPONSE:
708 		qedi_process_tmf_resp(qedi, cqe, task, conn);
709 		break;
710 	case ISCSI_OPCODE_TEXT_RESPONSE:
711 		qedi_process_text_resp(qedi, cqe, task, conn);
712 		break;
713 	case ISCSI_OPCODE_LOGOUT_RESPONSE:
714 		qedi_process_logout_resp(qedi, cqe, task, conn);
715 		break;
716 	case ISCSI_OPCODE_NOP_IN:
717 		qedi_process_nopin_mesg(qedi, cqe, task, conn, que_idx);
718 		break;
719 	default:
720 		QEDI_ERR(&qedi->dbg_ctx, "unknown opcode\n");
721 	}
722 }
723 
724 static void qedi_process_nopin_local_cmpl(struct qedi_ctx *qedi,
725 					  struct iscsi_cqe_solicited *cqe,
726 					  struct iscsi_task *task,
727 					  struct qedi_conn *qedi_conn)
728 {
729 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
730 	struct iscsi_session *session = conn->session;
731 	struct qedi_cmd *cmd = task->dd_data;
732 
733 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_UNSOL,
734 		  "itid=0x%x, cmd task id=0x%x\n",
735 		  cqe->itid, cmd->task_id);
736 
737 	cmd->state = RESPONSE_RECEIVED;
738 	qedi_clear_task_idx(qedi, cmd->task_id);
739 
740 	spin_lock_bh(&session->back_lock);
741 	__iscsi_put_task(task);
742 	spin_unlock_bh(&session->back_lock);
743 }
744 
745 static void qedi_process_cmd_cleanup_resp(struct qedi_ctx *qedi,
746 					  struct iscsi_cqe_solicited *cqe,
747 					  struct iscsi_task *task,
748 					  struct iscsi_conn *conn)
749 {
750 	struct qedi_work_map *work, *work_tmp;
751 	u32 proto_itt = cqe->itid;
752 	u32 ptmp_itt = 0;
753 	itt_t protoitt = 0;
754 	int found = 0;
755 	struct qedi_cmd *qedi_cmd = NULL;
756 	u32 rtid = 0;
757 	u32 iscsi_cid;
758 	struct qedi_conn *qedi_conn;
759 	struct qedi_cmd *dbg_cmd;
760 	struct iscsi_task *mtask;
761 	struct iscsi_tm *tmf_hdr = NULL;
762 
763 	iscsi_cid = cqe->conn_id;
764 	qedi_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid];
765 
766 	/* Based on this itt get the corresponding qedi_cmd */
767 	spin_lock_bh(&qedi_conn->tmf_work_lock);
768 	list_for_each_entry_safe(work, work_tmp, &qedi_conn->tmf_work_list,
769 				 list) {
770 		if (work->rtid == proto_itt) {
771 			/* We found the command */
772 			qedi_cmd = work->qedi_cmd;
773 			if (!qedi_cmd->list_tmf_work) {
774 				QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
775 					  "TMF work not found, cqe->tid=0x%x, cid=0x%x\n",
776 					  proto_itt, qedi_conn->iscsi_conn_id);
777 				WARN_ON(1);
778 			}
779 			found = 1;
780 			mtask = qedi_cmd->task;
781 			tmf_hdr = (struct iscsi_tm *)mtask->hdr;
782 			rtid = work->rtid;
783 
784 			list_del_init(&work->list);
785 			kfree(work);
786 			qedi_cmd->list_tmf_work = NULL;
787 		}
788 	}
789 	spin_unlock_bh(&qedi_conn->tmf_work_lock);
790 
791 	if (found) {
792 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
793 			  "TMF work, cqe->tid=0x%x, tmf flags=0x%x, cid=0x%x\n",
794 			  proto_itt, tmf_hdr->flags, qedi_conn->iscsi_conn_id);
795 
796 		if ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
797 		    ISCSI_TM_FUNC_ABORT_TASK) {
798 			spin_lock_bh(&conn->session->back_lock);
799 
800 			protoitt = build_itt(get_itt(tmf_hdr->rtt),
801 					     conn->session->age);
802 			task = iscsi_itt_to_task(conn, protoitt);
803 
804 			spin_unlock_bh(&conn->session->back_lock);
805 
806 			if (!task) {
807 				QEDI_NOTICE(&qedi->dbg_ctx,
808 					    "IO task completed, tmf rtt=0x%x, cid=0x%x\n",
809 					    get_itt(tmf_hdr->rtt),
810 					    qedi_conn->iscsi_conn_id);
811 				return;
812 			}
813 
814 			dbg_cmd = task->dd_data;
815 
816 			QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
817 				  "Abort tmf rtt=0x%x, i/o itt=0x%x, i/o tid=0x%x, cid=0x%x\n",
818 				  get_itt(tmf_hdr->rtt), get_itt(task->itt),
819 				  dbg_cmd->task_id, qedi_conn->iscsi_conn_id);
820 
821 			if (qedi_cmd->state == CLEANUP_WAIT_FAILED)
822 				qedi_cmd->state = CLEANUP_RECV;
823 
824 			qedi_clear_task_idx(qedi_conn->qedi, rtid);
825 
826 			spin_lock(&qedi_conn->list_lock);
827 			list_del_init(&dbg_cmd->io_cmd);
828 			qedi_conn->active_cmd_count--;
829 			spin_unlock(&qedi_conn->list_lock);
830 			qedi_cmd->state = CLEANUP_RECV;
831 			wake_up_interruptible(&qedi_conn->wait_queue);
832 		}
833 	} else if (qedi_conn->cmd_cleanup_req > 0) {
834 		spin_lock_bh(&conn->session->back_lock);
835 		qedi_get_proto_itt(qedi, cqe->itid, &ptmp_itt);
836 		protoitt = build_itt(ptmp_itt, conn->session->age);
837 		task = iscsi_itt_to_task(conn, protoitt);
838 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
839 			  "cleanup io itid=0x%x, protoitt=0x%x, cmd_cleanup_cmpl=%d, cid=0x%x\n",
840 			  cqe->itid, protoitt, qedi_conn->cmd_cleanup_cmpl,
841 			  qedi_conn->iscsi_conn_id);
842 
843 		spin_unlock_bh(&conn->session->back_lock);
844 		if (!task) {
845 			QEDI_NOTICE(&qedi->dbg_ctx,
846 				    "task is null, itid=0x%x, cid=0x%x\n",
847 				    cqe->itid, qedi_conn->iscsi_conn_id);
848 			return;
849 		}
850 		qedi_conn->cmd_cleanup_cmpl++;
851 		wake_up(&qedi_conn->wait_queue);
852 
853 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_TID,
854 			  "Freeing tid=0x%x for cid=0x%x\n",
855 			  cqe->itid, qedi_conn->iscsi_conn_id);
856 		qedi_clear_task_idx(qedi_conn->qedi, cqe->itid);
857 
858 	} else {
859 		qedi_get_proto_itt(qedi, cqe->itid, &ptmp_itt);
860 		protoitt = build_itt(ptmp_itt, conn->session->age);
861 		task = iscsi_itt_to_task(conn, protoitt);
862 		QEDI_ERR(&qedi->dbg_ctx,
863 			 "Delayed or untracked cleanup response, itt=0x%x, tid=0x%x, cid=0x%x, task=%p\n",
864 			 protoitt, cqe->itid, qedi_conn->iscsi_conn_id, task);
865 	}
866 }
867 
868 void qedi_fp_process_cqes(struct qedi_work *work)
869 {
870 	struct qedi_ctx *qedi = work->qedi;
871 	union iscsi_cqe *cqe = &work->cqe;
872 	struct iscsi_task *task = NULL;
873 	struct iscsi_nopout *nopout_hdr;
874 	struct qedi_conn *q_conn;
875 	struct iscsi_conn *conn;
876 	struct qedi_cmd *qedi_cmd;
877 	u32 comp_type;
878 	u32 iscsi_cid;
879 	u32 hdr_opcode;
880 	u16 que_idx = work->que_idx;
881 	u8 cqe_err_bits = 0;
882 
883 	comp_type = cqe->cqe_common.cqe_type;
884 	hdr_opcode = cqe->cqe_common.iscsi_hdr.common.hdr_first_byte;
885 	cqe_err_bits =
886 		cqe->cqe_common.error_bitmap.error_bits.cqe_error_status_bits;
887 
888 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
889 		  "fw_cid=0x%x, cqe type=0x%x, opcode=0x%x\n",
890 		  cqe->cqe_common.conn_id, comp_type, hdr_opcode);
891 
892 	if (comp_type >= MAX_ISCSI_CQES_TYPE) {
893 		QEDI_WARN(&qedi->dbg_ctx, "Invalid CqE type\n");
894 		return;
895 	}
896 
897 	iscsi_cid  = cqe->cqe_common.conn_id;
898 	q_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid];
899 	if (!q_conn) {
900 		QEDI_WARN(&qedi->dbg_ctx,
901 			  "Session no longer exists for cid=0x%x!!\n",
902 			  iscsi_cid);
903 		return;
904 	}
905 
906 	conn = q_conn->cls_conn->dd_data;
907 
908 	if (unlikely(cqe_err_bits &&
909 		     GET_FIELD(cqe_err_bits,
910 			       CQE_ERROR_BITMAP_DATA_DIGEST_ERR))) {
911 		iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
912 		return;
913 	}
914 
915 	switch (comp_type) {
916 	case ISCSI_CQE_TYPE_SOLICITED:
917 	case ISCSI_CQE_TYPE_SOLICITED_WITH_SENSE:
918 		qedi_cmd = container_of(work, struct qedi_cmd, cqe_work);
919 		task = qedi_cmd->task;
920 		if (!task) {
921 			QEDI_WARN(&qedi->dbg_ctx, "task is NULL\n");
922 			return;
923 		}
924 
925 		/* Process NOPIN local completion */
926 		nopout_hdr = (struct iscsi_nopout *)task->hdr;
927 		if ((nopout_hdr->itt == RESERVED_ITT) &&
928 		    (cqe->cqe_solicited.itid != (u16)RESERVED_ITT)) {
929 			qedi_process_nopin_local_cmpl(qedi, &cqe->cqe_solicited,
930 						      task, q_conn);
931 		} else {
932 			cqe->cqe_solicited.itid =
933 					       qedi_get_itt(cqe->cqe_solicited);
934 			/* Process other solicited responses */
935 			qedi_mtask_completion(qedi, cqe, task, q_conn, que_idx);
936 		}
937 		break;
938 	case ISCSI_CQE_TYPE_UNSOLICITED:
939 		switch (hdr_opcode) {
940 		case ISCSI_OPCODE_NOP_IN:
941 			qedi_process_nopin_mesg(qedi, cqe, task, q_conn,
942 						que_idx);
943 			break;
944 		case ISCSI_OPCODE_ASYNC_MSG:
945 			qedi_process_async_mesg(qedi, cqe, task, q_conn,
946 						que_idx);
947 			break;
948 		case ISCSI_OPCODE_REJECT:
949 			qedi_process_reject_mesg(qedi, cqe, task, q_conn,
950 						 que_idx);
951 			break;
952 		}
953 		goto exit_fp_process;
954 	case ISCSI_CQE_TYPE_DUMMY:
955 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM, "Dummy CqE\n");
956 		goto exit_fp_process;
957 	case ISCSI_CQE_TYPE_TASK_CLEANUP:
958 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM, "CleanUp CqE\n");
959 		qedi_process_cmd_cleanup_resp(qedi, &cqe->cqe_solicited, task,
960 					      conn);
961 		goto exit_fp_process;
962 	default:
963 		QEDI_ERR(&qedi->dbg_ctx, "Error cqe.\n");
964 		break;
965 	}
966 
967 exit_fp_process:
968 	return;
969 }
970 
971 static void qedi_ring_doorbell(struct qedi_conn *qedi_conn)
972 {
973 	struct iscsi_db_data dbell = { 0 };
974 
975 	dbell.agg_flags = 0;
976 
977 	dbell.params |= DB_DEST_XCM << ISCSI_DB_DATA_DEST_SHIFT;
978 	dbell.params |= DB_AGG_CMD_SET << ISCSI_DB_DATA_AGG_CMD_SHIFT;
979 	dbell.params |=
980 		   DQ_XCM_ISCSI_SQ_PROD_CMD << ISCSI_DB_DATA_AGG_VAL_SEL_SHIFT;
981 
982 	dbell.sq_prod = qedi_conn->ep->fw_sq_prod_idx;
983 	writel(*(u32 *)&dbell, qedi_conn->ep->p_doorbell);
984 
985 	/* Make sure fw write idx is coherent, and include both memory barriers
986 	 * as a failsafe as for some architectures the call is the same but on
987 	 * others they are two different assembly operations.
988 	 */
989 	wmb();
990 	mmiowb();
991 	QEDI_INFO(&qedi_conn->qedi->dbg_ctx, QEDI_LOG_MP_REQ,
992 		  "prod_idx=0x%x, fw_prod_idx=0x%x, cid=0x%x\n",
993 		  qedi_conn->ep->sq_prod_idx, qedi_conn->ep->fw_sq_prod_idx,
994 		  qedi_conn->iscsi_conn_id);
995 }
996 
997 static u16 qedi_get_wqe_idx(struct qedi_conn *qedi_conn)
998 {
999 	struct qedi_endpoint *ep;
1000 	u16 rval;
1001 
1002 	ep = qedi_conn->ep;
1003 	rval = ep->sq_prod_idx;
1004 
1005 	/* Increament SQ index */
1006 	ep->sq_prod_idx++;
1007 	ep->fw_sq_prod_idx++;
1008 	if (ep->sq_prod_idx == QEDI_SQ_SIZE)
1009 		ep->sq_prod_idx = 0;
1010 
1011 	return rval;
1012 }
1013 
1014 int qedi_send_iscsi_login(struct qedi_conn *qedi_conn,
1015 			  struct iscsi_task *task)
1016 {
1017 	struct iscsi_login_req_hdr login_req_pdu_header;
1018 	struct scsi_sgl_task_params tx_sgl_task_params;
1019 	struct scsi_sgl_task_params rx_sgl_task_params;
1020 	struct iscsi_task_params task_params;
1021 	struct e4_iscsi_task_context *fw_task_ctx;
1022 	struct qedi_ctx *qedi = qedi_conn->qedi;
1023 	struct iscsi_login_req *login_hdr;
1024 	struct scsi_sge *resp_sge = NULL;
1025 	struct qedi_cmd *qedi_cmd;
1026 	struct qedi_endpoint *ep;
1027 	s16 tid = 0;
1028 	u16 sq_idx = 0;
1029 	int rval = 0;
1030 
1031 	resp_sge = (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1032 	qedi_cmd = (struct qedi_cmd *)task->dd_data;
1033 	ep = qedi_conn->ep;
1034 	login_hdr = (struct iscsi_login_req *)task->hdr;
1035 
1036 	tid = qedi_get_task_idx(qedi);
1037 	if (tid == -1)
1038 		return -ENOMEM;
1039 
1040 	fw_task_ctx =
1041 	     (struct e4_iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1042 							       tid);
1043 	memset(fw_task_ctx, 0, sizeof(struct e4_iscsi_task_context));
1044 
1045 	qedi_cmd->task_id = tid;
1046 
1047 	memset(&task_params, 0, sizeof(task_params));
1048 	memset(&login_req_pdu_header, 0, sizeof(login_req_pdu_header));
1049 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
1050 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
1051 	/* Update header info */
1052 	login_req_pdu_header.opcode = login_hdr->opcode;
1053 	login_req_pdu_header.version_min = login_hdr->min_version;
1054 	login_req_pdu_header.version_max = login_hdr->max_version;
1055 	login_req_pdu_header.flags_attr = login_hdr->flags;
1056 	login_req_pdu_header.isid_tabc = swab32p((u32 *)login_hdr->isid);
1057 	login_req_pdu_header.isid_d = swab16p((u16 *)&login_hdr->isid[4]);
1058 
1059 	login_req_pdu_header.tsih = login_hdr->tsih;
1060 	login_req_pdu_header.hdr_second_dword = ntoh24(login_hdr->dlength);
1061 
1062 	qedi_update_itt_map(qedi, tid, task->itt, qedi_cmd);
1063 	login_req_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
1064 	login_req_pdu_header.cid = qedi_conn->iscsi_conn_id;
1065 	login_req_pdu_header.cmd_sn = be32_to_cpu(login_hdr->cmdsn);
1066 	login_req_pdu_header.exp_stat_sn = be32_to_cpu(login_hdr->exp_statsn);
1067 	login_req_pdu_header.exp_stat_sn = 0;
1068 
1069 	/* Fill tx AHS and rx buffer */
1070 	tx_sgl_task_params.sgl =
1071 			       (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
1072 	tx_sgl_task_params.sgl_phys_addr.lo =
1073 					 (u32)(qedi_conn->gen_pdu.req_dma_addr);
1074 	tx_sgl_task_params.sgl_phys_addr.hi =
1075 			      (u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
1076 	tx_sgl_task_params.total_buffer_size = ntoh24(login_hdr->dlength);
1077 	tx_sgl_task_params.num_sges = 1;
1078 
1079 	rx_sgl_task_params.sgl =
1080 			      (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1081 	rx_sgl_task_params.sgl_phys_addr.lo =
1082 					(u32)(qedi_conn->gen_pdu.resp_dma_addr);
1083 	rx_sgl_task_params.sgl_phys_addr.hi =
1084 			     (u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
1085 	rx_sgl_task_params.total_buffer_size = resp_sge->sge_len;
1086 	rx_sgl_task_params.num_sges = 1;
1087 
1088 	/* Fill fw input params */
1089 	task_params.context = fw_task_ctx;
1090 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1091 	task_params.itid = tid;
1092 	task_params.cq_rss_number = 0;
1093 	task_params.tx_io_size = ntoh24(login_hdr->dlength);
1094 	task_params.rx_io_size = resp_sge->sge_len;
1095 
1096 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1097 	task_params.sqe = &ep->sq[sq_idx];
1098 
1099 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1100 	rval = init_initiator_login_request_task(&task_params,
1101 						 &login_req_pdu_header,
1102 						 &tx_sgl_task_params,
1103 						 &rx_sgl_task_params);
1104 	if (rval)
1105 		return -1;
1106 
1107 	spin_lock(&qedi_conn->list_lock);
1108 	list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1109 	qedi_cmd->io_cmd_in_list = true;
1110 	qedi_conn->active_cmd_count++;
1111 	spin_unlock(&qedi_conn->list_lock);
1112 
1113 	qedi_ring_doorbell(qedi_conn);
1114 	return 0;
1115 }
1116 
1117 int qedi_send_iscsi_logout(struct qedi_conn *qedi_conn,
1118 			   struct iscsi_task *task)
1119 {
1120 	struct iscsi_logout_req_hdr logout_pdu_header;
1121 	struct scsi_sgl_task_params tx_sgl_task_params;
1122 	struct scsi_sgl_task_params rx_sgl_task_params;
1123 	struct iscsi_task_params task_params;
1124 	struct e4_iscsi_task_context *fw_task_ctx;
1125 	struct iscsi_logout *logout_hdr = NULL;
1126 	struct qedi_ctx *qedi = qedi_conn->qedi;
1127 	struct qedi_cmd *qedi_cmd;
1128 	struct qedi_endpoint *ep;
1129 	s16 tid = 0;
1130 	u16 sq_idx = 0;
1131 	int rval = 0;
1132 
1133 	qedi_cmd = (struct qedi_cmd *)task->dd_data;
1134 	logout_hdr = (struct iscsi_logout *)task->hdr;
1135 	ep = qedi_conn->ep;
1136 
1137 	tid = qedi_get_task_idx(qedi);
1138 	if (tid == -1)
1139 		return -ENOMEM;
1140 
1141 	fw_task_ctx =
1142 	     (struct e4_iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1143 							       tid);
1144 	memset(fw_task_ctx, 0, sizeof(struct e4_iscsi_task_context));
1145 
1146 	qedi_cmd->task_id = tid;
1147 
1148 	memset(&task_params, 0, sizeof(task_params));
1149 	memset(&logout_pdu_header, 0, sizeof(logout_pdu_header));
1150 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
1151 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
1152 
1153 	/* Update header info */
1154 	logout_pdu_header.opcode = logout_hdr->opcode;
1155 	logout_pdu_header.reason_code = 0x80 | logout_hdr->flags;
1156 	qedi_update_itt_map(qedi, tid, task->itt, qedi_cmd);
1157 	logout_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
1158 	logout_pdu_header.exp_stat_sn = be32_to_cpu(logout_hdr->exp_statsn);
1159 	logout_pdu_header.cmd_sn = be32_to_cpu(logout_hdr->cmdsn);
1160 	logout_pdu_header.cid = qedi_conn->iscsi_conn_id;
1161 
1162 	/* Fill fw input params */
1163 	task_params.context = fw_task_ctx;
1164 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1165 	task_params.itid = tid;
1166 	task_params.cq_rss_number = 0;
1167 	task_params.tx_io_size = 0;
1168 	task_params.rx_io_size = 0;
1169 
1170 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1171 	task_params.sqe = &ep->sq[sq_idx];
1172 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1173 
1174 	rval = init_initiator_logout_request_task(&task_params,
1175 						  &logout_pdu_header,
1176 						  NULL, NULL);
1177 	if (rval)
1178 		return -1;
1179 
1180 	spin_lock(&qedi_conn->list_lock);
1181 	list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1182 	qedi_cmd->io_cmd_in_list = true;
1183 	qedi_conn->active_cmd_count++;
1184 	spin_unlock(&qedi_conn->list_lock);
1185 
1186 	qedi_ring_doorbell(qedi_conn);
1187 	return 0;
1188 }
1189 
1190 int qedi_cleanup_all_io(struct qedi_ctx *qedi, struct qedi_conn *qedi_conn,
1191 			struct iscsi_task *task, bool in_recovery)
1192 {
1193 	int rval;
1194 	struct iscsi_task *ctask;
1195 	struct qedi_cmd *cmd, *cmd_tmp;
1196 	struct iscsi_tm *tmf_hdr;
1197 	unsigned int lun = 0;
1198 	bool lun_reset = false;
1199 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
1200 	struct iscsi_session *session = conn->session;
1201 
1202 	/* From recovery, task is NULL or from tmf resp valid task */
1203 	if (task) {
1204 		tmf_hdr = (struct iscsi_tm *)task->hdr;
1205 
1206 		if ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1207 			ISCSI_TM_FUNC_LOGICAL_UNIT_RESET) {
1208 			lun_reset = true;
1209 			lun = scsilun_to_int(&tmf_hdr->lun);
1210 		}
1211 	}
1212 
1213 	qedi_conn->cmd_cleanup_req = 0;
1214 	qedi_conn->cmd_cleanup_cmpl = 0;
1215 
1216 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1217 		  "active_cmd_count=%d, cid=0x%x, in_recovery=%d, lun_reset=%d\n",
1218 		  qedi_conn->active_cmd_count, qedi_conn->iscsi_conn_id,
1219 		  in_recovery, lun_reset);
1220 
1221 	if (lun_reset)
1222 		spin_lock_bh(&session->back_lock);
1223 
1224 	spin_lock(&qedi_conn->list_lock);
1225 
1226 	list_for_each_entry_safe(cmd, cmd_tmp, &qedi_conn->active_cmd_list,
1227 				 io_cmd) {
1228 		ctask = cmd->task;
1229 		if (ctask == task)
1230 			continue;
1231 
1232 		if (lun_reset) {
1233 			if (cmd->scsi_cmd && cmd->scsi_cmd->device) {
1234 				QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1235 					  "tid=0x%x itt=0x%x scsi_cmd_ptr=%p device=%p task_state=%d cmd_state=0%x cid=0x%x\n",
1236 					  cmd->task_id, get_itt(ctask->itt),
1237 					  cmd->scsi_cmd, cmd->scsi_cmd->device,
1238 					  ctask->state, cmd->state,
1239 					  qedi_conn->iscsi_conn_id);
1240 				if (cmd->scsi_cmd->device->lun != lun)
1241 					continue;
1242 			}
1243 		}
1244 		qedi_conn->cmd_cleanup_req++;
1245 		qedi_iscsi_cleanup_task(ctask, true);
1246 
1247 		list_del_init(&cmd->io_cmd);
1248 		qedi_conn->active_cmd_count--;
1249 		QEDI_WARN(&qedi->dbg_ctx,
1250 			  "Deleted active cmd list node io_cmd=%p, cid=0x%x\n",
1251 			  &cmd->io_cmd, qedi_conn->iscsi_conn_id);
1252 	}
1253 
1254 	spin_unlock(&qedi_conn->list_lock);
1255 
1256 	if (lun_reset)
1257 		spin_unlock_bh(&session->back_lock);
1258 
1259 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1260 		  "cmd_cleanup_req=%d, cid=0x%x\n",
1261 		  qedi_conn->cmd_cleanup_req,
1262 		  qedi_conn->iscsi_conn_id);
1263 
1264 	rval  = wait_event_interruptible_timeout(qedi_conn->wait_queue,
1265 						 ((qedi_conn->cmd_cleanup_req ==
1266 						 qedi_conn->cmd_cleanup_cmpl) ||
1267 						 qedi_conn->ep),
1268 						 5 * HZ);
1269 	if (rval) {
1270 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1271 			  "i/o cmd_cleanup_req=%d, equal to cmd_cleanup_cmpl=%d, cid=0x%x\n",
1272 			  qedi_conn->cmd_cleanup_req,
1273 			  qedi_conn->cmd_cleanup_cmpl,
1274 			  qedi_conn->iscsi_conn_id);
1275 
1276 		return 0;
1277 	}
1278 
1279 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1280 		  "i/o cmd_cleanup_req=%d, not equal to cmd_cleanup_cmpl=%d, cid=0x%x\n",
1281 		  qedi_conn->cmd_cleanup_req,
1282 		  qedi_conn->cmd_cleanup_cmpl,
1283 		  qedi_conn->iscsi_conn_id);
1284 
1285 	iscsi_host_for_each_session(qedi->shost,
1286 				    qedi_mark_device_missing);
1287 	qedi_ops->common->drain(qedi->cdev);
1288 
1289 	/* Enable IOs for all other sessions except current.*/
1290 	if (!wait_event_interruptible_timeout(qedi_conn->wait_queue,
1291 					      (qedi_conn->cmd_cleanup_req ==
1292 					       qedi_conn->cmd_cleanup_cmpl),
1293 					      5 * HZ)) {
1294 		iscsi_host_for_each_session(qedi->shost,
1295 					    qedi_mark_device_available);
1296 		return -1;
1297 	}
1298 
1299 	iscsi_host_for_each_session(qedi->shost,
1300 				    qedi_mark_device_available);
1301 
1302 	return 0;
1303 }
1304 
1305 void qedi_clearsq(struct qedi_ctx *qedi, struct qedi_conn *qedi_conn,
1306 		  struct iscsi_task *task)
1307 {
1308 	struct qedi_endpoint *qedi_ep;
1309 	int rval;
1310 
1311 	qedi_ep = qedi_conn->ep;
1312 	qedi_conn->cmd_cleanup_req = 0;
1313 	qedi_conn->cmd_cleanup_cmpl = 0;
1314 
1315 	if (!qedi_ep) {
1316 		QEDI_WARN(&qedi->dbg_ctx,
1317 			  "Cannot proceed, ep already disconnected, cid=0x%x\n",
1318 			  qedi_conn->iscsi_conn_id);
1319 		return;
1320 	}
1321 
1322 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1323 		  "Clearing SQ for cid=0x%x, conn=%p, ep=%p\n",
1324 		  qedi_conn->iscsi_conn_id, qedi_conn, qedi_ep);
1325 
1326 	qedi_ops->clear_sq(qedi->cdev, qedi_ep->handle);
1327 
1328 	rval = qedi_cleanup_all_io(qedi, qedi_conn, task, true);
1329 	if (rval) {
1330 		QEDI_ERR(&qedi->dbg_ctx,
1331 			 "fatal error, need hard reset, cid=0x%x\n",
1332 			 qedi_conn->iscsi_conn_id);
1333 		WARN_ON(1);
1334 	}
1335 }
1336 
1337 static int qedi_wait_for_cleanup_request(struct qedi_ctx *qedi,
1338 					 struct qedi_conn *qedi_conn,
1339 					 struct iscsi_task *task,
1340 					 struct qedi_cmd *qedi_cmd,
1341 					 struct qedi_work_map *list_work)
1342 {
1343 	struct qedi_cmd *cmd = (struct qedi_cmd *)task->dd_data;
1344 	int wait;
1345 
1346 	wait  = wait_event_interruptible_timeout(qedi_conn->wait_queue,
1347 						 ((qedi_cmd->state ==
1348 						   CLEANUP_RECV) ||
1349 						 ((qedi_cmd->type == TYPEIO) &&
1350 						  (cmd->state ==
1351 						   RESPONSE_RECEIVED))),
1352 						 5 * HZ);
1353 	if (!wait) {
1354 		qedi_cmd->state = CLEANUP_WAIT_FAILED;
1355 
1356 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1357 			  "Cleanup timedout tid=0x%x, issue connection recovery, cid=0x%x\n",
1358 			  cmd->task_id, qedi_conn->iscsi_conn_id);
1359 
1360 		return -1;
1361 	}
1362 	return 0;
1363 }
1364 
1365 static void qedi_tmf_work(struct work_struct *work)
1366 {
1367 	struct qedi_cmd *qedi_cmd =
1368 		container_of(work, struct qedi_cmd, tmf_work);
1369 	struct qedi_conn *qedi_conn = qedi_cmd->conn;
1370 	struct qedi_ctx *qedi = qedi_conn->qedi;
1371 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
1372 	struct iscsi_cls_session *cls_sess;
1373 	struct qedi_work_map *list_work = NULL;
1374 	struct iscsi_task *mtask;
1375 	struct qedi_cmd *cmd;
1376 	struct iscsi_task *ctask;
1377 	struct iscsi_tm *tmf_hdr;
1378 	s16 rval = 0;
1379 	s16 tid = 0;
1380 
1381 	mtask = qedi_cmd->task;
1382 	tmf_hdr = (struct iscsi_tm *)mtask->hdr;
1383 	cls_sess = iscsi_conn_to_session(qedi_conn->cls_conn);
1384 	set_bit(QEDI_CONN_FW_CLEANUP, &qedi_conn->flags);
1385 
1386 	ctask = iscsi_itt_to_task(conn, tmf_hdr->rtt);
1387 	if (!ctask || !ctask->sc) {
1388 		QEDI_ERR(&qedi->dbg_ctx, "Task already completed\n");
1389 		goto abort_ret;
1390 	}
1391 
1392 	cmd = (struct qedi_cmd *)ctask->dd_data;
1393 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1394 		  "Abort tmf rtt=0x%x, cmd itt=0x%x, cmd tid=0x%x, cid=0x%x\n",
1395 		  get_itt(tmf_hdr->rtt), get_itt(ctask->itt), cmd->task_id,
1396 		  qedi_conn->iscsi_conn_id);
1397 
1398 	if (qedi_do_not_recover) {
1399 		QEDI_ERR(&qedi->dbg_ctx, "DONT SEND CLEANUP/ABORT %d\n",
1400 			 qedi_do_not_recover);
1401 		goto abort_ret;
1402 	}
1403 
1404 	list_work = kzalloc(sizeof(*list_work), GFP_ATOMIC);
1405 	if (!list_work) {
1406 		QEDI_ERR(&qedi->dbg_ctx, "Memory allocation failed\n");
1407 		goto abort_ret;
1408 	}
1409 
1410 	qedi_cmd->type = TYPEIO;
1411 	list_work->qedi_cmd = qedi_cmd;
1412 	list_work->rtid = cmd->task_id;
1413 	list_work->state = QEDI_WORK_SCHEDULED;
1414 	qedi_cmd->list_tmf_work = list_work;
1415 
1416 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
1417 		  "Queue tmf work=%p, list node=%p, cid=0x%x, tmf flags=0x%x\n",
1418 		  list_work->ptr_tmf_work, list_work, qedi_conn->iscsi_conn_id,
1419 		  tmf_hdr->flags);
1420 
1421 	spin_lock_bh(&qedi_conn->tmf_work_lock);
1422 	list_add_tail(&list_work->list, &qedi_conn->tmf_work_list);
1423 	spin_unlock_bh(&qedi_conn->tmf_work_lock);
1424 
1425 	qedi_iscsi_cleanup_task(ctask, false);
1426 
1427 	rval = qedi_wait_for_cleanup_request(qedi, qedi_conn, ctask, qedi_cmd,
1428 					     list_work);
1429 	if (rval == -1) {
1430 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1431 			  "FW cleanup got escalated, cid=0x%x\n",
1432 			  qedi_conn->iscsi_conn_id);
1433 		goto ldel_exit;
1434 	}
1435 
1436 	tid = qedi_get_task_idx(qedi);
1437 	if (tid == -1) {
1438 		QEDI_ERR(&qedi->dbg_ctx, "Invalid tid, cid=0x%x\n",
1439 			 qedi_conn->iscsi_conn_id);
1440 		goto ldel_exit;
1441 	}
1442 
1443 	qedi_cmd->task_id = tid;
1444 	qedi_send_iscsi_tmf(qedi_conn, qedi_cmd->task);
1445 
1446 abort_ret:
1447 	clear_bit(QEDI_CONN_FW_CLEANUP, &qedi_conn->flags);
1448 	return;
1449 
1450 ldel_exit:
1451 	spin_lock_bh(&qedi_conn->tmf_work_lock);
1452 	if (!qedi_cmd->list_tmf_work) {
1453 		list_del_init(&list_work->list);
1454 		qedi_cmd->list_tmf_work = NULL;
1455 		kfree(list_work);
1456 	}
1457 	spin_unlock_bh(&qedi_conn->tmf_work_lock);
1458 
1459 	spin_lock(&qedi_conn->list_lock);
1460 	list_del_init(&cmd->io_cmd);
1461 	qedi_conn->active_cmd_count--;
1462 	spin_unlock(&qedi_conn->list_lock);
1463 
1464 	clear_bit(QEDI_CONN_FW_CLEANUP, &qedi_conn->flags);
1465 }
1466 
1467 static int qedi_send_iscsi_tmf(struct qedi_conn *qedi_conn,
1468 			       struct iscsi_task *mtask)
1469 {
1470 	struct iscsi_tmf_request_hdr tmf_pdu_header;
1471 	struct iscsi_task_params task_params;
1472 	struct qedi_ctx *qedi = qedi_conn->qedi;
1473 	struct e4_iscsi_task_context *fw_task_ctx;
1474 	struct iscsi_conn *conn = qedi_conn->cls_conn->dd_data;
1475 	struct iscsi_task *ctask;
1476 	struct iscsi_tm *tmf_hdr;
1477 	struct qedi_cmd *qedi_cmd;
1478 	struct qedi_cmd *cmd;
1479 	struct qedi_endpoint *ep;
1480 	u32 scsi_lun[2];
1481 	s16 tid = 0;
1482 	u16 sq_idx = 0;
1483 	int rval = 0;
1484 
1485 	tmf_hdr = (struct iscsi_tm *)mtask->hdr;
1486 	qedi_cmd = (struct qedi_cmd *)mtask->dd_data;
1487 	ep = qedi_conn->ep;
1488 	if (!ep)
1489 		return -ENODEV;
1490 
1491 	tid = qedi_get_task_idx(qedi);
1492 	if (tid == -1)
1493 		return -ENOMEM;
1494 
1495 	fw_task_ctx =
1496 	     (struct e4_iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1497 							       tid);
1498 	memset(fw_task_ctx, 0, sizeof(struct e4_iscsi_task_context));
1499 
1500 	qedi_cmd->task_id = tid;
1501 
1502 	memset(&task_params, 0, sizeof(task_params));
1503 	memset(&tmf_pdu_header, 0, sizeof(tmf_pdu_header));
1504 
1505 	/* Update header info */
1506 	qedi_update_itt_map(qedi, tid, mtask->itt, qedi_cmd);
1507 	tmf_pdu_header.itt = qedi_set_itt(tid, get_itt(mtask->itt));
1508 	tmf_pdu_header.cmd_sn = be32_to_cpu(tmf_hdr->cmdsn);
1509 
1510 	memcpy(scsi_lun, &tmf_hdr->lun, sizeof(struct scsi_lun));
1511 	tmf_pdu_header.lun.lo = be32_to_cpu(scsi_lun[0]);
1512 	tmf_pdu_header.lun.hi = be32_to_cpu(scsi_lun[1]);
1513 
1514 	if ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1515 	     ISCSI_TM_FUNC_ABORT_TASK) {
1516 		ctask = iscsi_itt_to_task(conn, tmf_hdr->rtt);
1517 		if (!ctask || !ctask->sc) {
1518 			QEDI_ERR(&qedi->dbg_ctx,
1519 				 "Could not get reference task\n");
1520 			return 0;
1521 		}
1522 		cmd = (struct qedi_cmd *)ctask->dd_data;
1523 		tmf_pdu_header.rtt =
1524 				qedi_set_itt(cmd->task_id,
1525 					     get_itt(tmf_hdr->rtt));
1526 	} else {
1527 		tmf_pdu_header.rtt = ISCSI_RESERVED_TAG;
1528 	}
1529 
1530 	tmf_pdu_header.opcode = tmf_hdr->opcode;
1531 	tmf_pdu_header.function = tmf_hdr->flags;
1532 	tmf_pdu_header.hdr_second_dword = ntoh24(tmf_hdr->dlength);
1533 	tmf_pdu_header.ref_cmd_sn = be32_to_cpu(tmf_hdr->refcmdsn);
1534 
1535 	/* Fill fw input params */
1536 	task_params.context = fw_task_ctx;
1537 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1538 	task_params.itid = tid;
1539 	task_params.cq_rss_number = 0;
1540 	task_params.tx_io_size = 0;
1541 	task_params.rx_io_size = 0;
1542 
1543 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1544 	task_params.sqe = &ep->sq[sq_idx];
1545 
1546 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1547 	rval = init_initiator_tmf_request_task(&task_params,
1548 					       &tmf_pdu_header);
1549 	if (rval)
1550 		return -1;
1551 
1552 	spin_lock(&qedi_conn->list_lock);
1553 	list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1554 	qedi_cmd->io_cmd_in_list = true;
1555 	qedi_conn->active_cmd_count++;
1556 	spin_unlock(&qedi_conn->list_lock);
1557 
1558 	qedi_ring_doorbell(qedi_conn);
1559 	return 0;
1560 }
1561 
1562 int qedi_iscsi_abort_work(struct qedi_conn *qedi_conn,
1563 			  struct iscsi_task *mtask)
1564 {
1565 	struct qedi_ctx *qedi = qedi_conn->qedi;
1566 	struct iscsi_tm *tmf_hdr;
1567 	struct qedi_cmd *qedi_cmd = (struct qedi_cmd *)mtask->dd_data;
1568 	s16 tid = 0;
1569 
1570 	tmf_hdr = (struct iscsi_tm *)mtask->hdr;
1571 	qedi_cmd->task = mtask;
1572 
1573 	/* If abort task then schedule the work and return */
1574 	if ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1575 	    ISCSI_TM_FUNC_ABORT_TASK) {
1576 		qedi_cmd->state = CLEANUP_WAIT;
1577 		INIT_WORK(&qedi_cmd->tmf_work, qedi_tmf_work);
1578 		queue_work(qedi->tmf_thread, &qedi_cmd->tmf_work);
1579 
1580 	} else if (((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1581 		    ISCSI_TM_FUNC_LOGICAL_UNIT_RESET) ||
1582 		   ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1583 		    ISCSI_TM_FUNC_TARGET_WARM_RESET) ||
1584 		   ((tmf_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) ==
1585 		    ISCSI_TM_FUNC_TARGET_COLD_RESET)) {
1586 		tid = qedi_get_task_idx(qedi);
1587 		if (tid == -1) {
1588 			QEDI_ERR(&qedi->dbg_ctx, "Invalid tid, cid=0x%x\n",
1589 				 qedi_conn->iscsi_conn_id);
1590 			return -1;
1591 		}
1592 		qedi_cmd->task_id = tid;
1593 
1594 		qedi_send_iscsi_tmf(qedi_conn, qedi_cmd->task);
1595 
1596 	} else {
1597 		QEDI_ERR(&qedi->dbg_ctx, "Invalid tmf, cid=0x%x\n",
1598 			 qedi_conn->iscsi_conn_id);
1599 		return -1;
1600 	}
1601 
1602 	return 0;
1603 }
1604 
1605 int qedi_send_iscsi_text(struct qedi_conn *qedi_conn,
1606 			 struct iscsi_task *task)
1607 {
1608 	struct iscsi_text_request_hdr text_request_pdu_header;
1609 	struct scsi_sgl_task_params tx_sgl_task_params;
1610 	struct scsi_sgl_task_params rx_sgl_task_params;
1611 	struct iscsi_task_params task_params;
1612 	struct e4_iscsi_task_context *fw_task_ctx;
1613 	struct qedi_ctx *qedi = qedi_conn->qedi;
1614 	struct iscsi_text *text_hdr;
1615 	struct scsi_sge *req_sge = NULL;
1616 	struct scsi_sge *resp_sge = NULL;
1617 	struct qedi_cmd *qedi_cmd;
1618 	struct qedi_endpoint *ep;
1619 	s16 tid = 0;
1620 	u16 sq_idx = 0;
1621 	int rval = 0;
1622 
1623 	req_sge = (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
1624 	resp_sge = (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1625 	qedi_cmd = (struct qedi_cmd *)task->dd_data;
1626 	text_hdr = (struct iscsi_text *)task->hdr;
1627 	ep = qedi_conn->ep;
1628 
1629 	tid = qedi_get_task_idx(qedi);
1630 	if (tid == -1)
1631 		return -ENOMEM;
1632 
1633 	fw_task_ctx =
1634 	     (struct e4_iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1635 							       tid);
1636 	memset(fw_task_ctx, 0, sizeof(struct e4_iscsi_task_context));
1637 
1638 	qedi_cmd->task_id = tid;
1639 
1640 	memset(&task_params, 0, sizeof(task_params));
1641 	memset(&text_request_pdu_header, 0, sizeof(text_request_pdu_header));
1642 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
1643 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
1644 
1645 	/* Update header info */
1646 	text_request_pdu_header.opcode = text_hdr->opcode;
1647 	text_request_pdu_header.flags_attr = text_hdr->flags;
1648 
1649 	qedi_update_itt_map(qedi, tid, task->itt, qedi_cmd);
1650 	text_request_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
1651 	text_request_pdu_header.ttt = text_hdr->ttt;
1652 	text_request_pdu_header.cmd_sn = be32_to_cpu(text_hdr->cmdsn);
1653 	text_request_pdu_header.exp_stat_sn = be32_to_cpu(text_hdr->exp_statsn);
1654 	text_request_pdu_header.hdr_second_dword = ntoh24(text_hdr->dlength);
1655 
1656 	/* Fill tx AHS and rx buffer */
1657 	tx_sgl_task_params.sgl =
1658 			       (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
1659 	tx_sgl_task_params.sgl_phys_addr.lo =
1660 					 (u32)(qedi_conn->gen_pdu.req_dma_addr);
1661 	tx_sgl_task_params.sgl_phys_addr.hi =
1662 			      (u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
1663 	tx_sgl_task_params.total_buffer_size = req_sge->sge_len;
1664 	tx_sgl_task_params.num_sges = 1;
1665 
1666 	rx_sgl_task_params.sgl =
1667 			      (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1668 	rx_sgl_task_params.sgl_phys_addr.lo =
1669 					(u32)(qedi_conn->gen_pdu.resp_dma_addr);
1670 	rx_sgl_task_params.sgl_phys_addr.hi =
1671 			     (u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
1672 	rx_sgl_task_params.total_buffer_size = resp_sge->sge_len;
1673 	rx_sgl_task_params.num_sges = 1;
1674 
1675 	/* Fill fw input params */
1676 	task_params.context = fw_task_ctx;
1677 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1678 	task_params.itid = tid;
1679 	task_params.cq_rss_number = 0;
1680 	task_params.tx_io_size = ntoh24(text_hdr->dlength);
1681 	task_params.rx_io_size = resp_sge->sge_len;
1682 
1683 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1684 	task_params.sqe = &ep->sq[sq_idx];
1685 
1686 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1687 	rval = init_initiator_text_request_task(&task_params,
1688 						&text_request_pdu_header,
1689 						&tx_sgl_task_params,
1690 						&rx_sgl_task_params);
1691 	if (rval)
1692 		return -1;
1693 
1694 	spin_lock(&qedi_conn->list_lock);
1695 	list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1696 	qedi_cmd->io_cmd_in_list = true;
1697 	qedi_conn->active_cmd_count++;
1698 	spin_unlock(&qedi_conn->list_lock);
1699 
1700 	qedi_ring_doorbell(qedi_conn);
1701 	return 0;
1702 }
1703 
1704 int qedi_send_iscsi_nopout(struct qedi_conn *qedi_conn,
1705 			   struct iscsi_task *task,
1706 			   char *datap, int data_len, int unsol)
1707 {
1708 	struct iscsi_nop_out_hdr nop_out_pdu_header;
1709 	struct scsi_sgl_task_params tx_sgl_task_params;
1710 	struct scsi_sgl_task_params rx_sgl_task_params;
1711 	struct iscsi_task_params task_params;
1712 	struct qedi_ctx *qedi = qedi_conn->qedi;
1713 	struct e4_iscsi_task_context *fw_task_ctx;
1714 	struct iscsi_nopout *nopout_hdr;
1715 	struct scsi_sge *resp_sge = NULL;
1716 	struct qedi_cmd *qedi_cmd;
1717 	struct qedi_endpoint *ep;
1718 	u32 scsi_lun[2];
1719 	s16 tid = 0;
1720 	u16 sq_idx = 0;
1721 	int rval = 0;
1722 
1723 	resp_sge = (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1724 	qedi_cmd = (struct qedi_cmd *)task->dd_data;
1725 	nopout_hdr = (struct iscsi_nopout *)task->hdr;
1726 	ep = qedi_conn->ep;
1727 
1728 	tid = qedi_get_task_idx(qedi);
1729 	if (tid == -1)
1730 		return -ENOMEM;
1731 
1732 	fw_task_ctx =
1733 	     (struct e4_iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
1734 							       tid);
1735 	memset(fw_task_ctx, 0, sizeof(struct e4_iscsi_task_context));
1736 
1737 	qedi_cmd->task_id = tid;
1738 
1739 	memset(&task_params, 0, sizeof(task_params));
1740 	memset(&nop_out_pdu_header, 0, sizeof(nop_out_pdu_header));
1741 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
1742 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
1743 
1744 	/* Update header info */
1745 	nop_out_pdu_header.opcode = nopout_hdr->opcode;
1746 	SET_FIELD(nop_out_pdu_header.flags_attr, ISCSI_NOP_OUT_HDR_CONST1, 1);
1747 	SET_FIELD(nop_out_pdu_header.flags_attr, ISCSI_NOP_OUT_HDR_RSRV, 0);
1748 
1749 	memcpy(scsi_lun, &nopout_hdr->lun, sizeof(struct scsi_lun));
1750 	nop_out_pdu_header.lun.lo = be32_to_cpu(scsi_lun[0]);
1751 	nop_out_pdu_header.lun.hi = be32_to_cpu(scsi_lun[1]);
1752 	nop_out_pdu_header.cmd_sn = be32_to_cpu(nopout_hdr->cmdsn);
1753 	nop_out_pdu_header.exp_stat_sn = be32_to_cpu(nopout_hdr->exp_statsn);
1754 
1755 	qedi_update_itt_map(qedi, tid, task->itt, qedi_cmd);
1756 
1757 	if (nopout_hdr->ttt != ISCSI_TTT_ALL_ONES) {
1758 		nop_out_pdu_header.itt = be32_to_cpu(nopout_hdr->itt);
1759 		nop_out_pdu_header.ttt = be32_to_cpu(nopout_hdr->ttt);
1760 	} else {
1761 		nop_out_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
1762 		nop_out_pdu_header.ttt = ISCSI_TTT_ALL_ONES;
1763 
1764 		spin_lock(&qedi_conn->list_lock);
1765 		list_add_tail(&qedi_cmd->io_cmd, &qedi_conn->active_cmd_list);
1766 		qedi_cmd->io_cmd_in_list = true;
1767 		qedi_conn->active_cmd_count++;
1768 		spin_unlock(&qedi_conn->list_lock);
1769 	}
1770 
1771 	/* Fill tx AHS and rx buffer */
1772 	if (data_len) {
1773 		tx_sgl_task_params.sgl =
1774 			       (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
1775 		tx_sgl_task_params.sgl_phys_addr.lo =
1776 					 (u32)(qedi_conn->gen_pdu.req_dma_addr);
1777 		tx_sgl_task_params.sgl_phys_addr.hi =
1778 			      (u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
1779 		tx_sgl_task_params.total_buffer_size = data_len;
1780 		tx_sgl_task_params.num_sges = 1;
1781 
1782 		rx_sgl_task_params.sgl =
1783 			      (struct scsi_sge *)qedi_conn->gen_pdu.resp_bd_tbl;
1784 		rx_sgl_task_params.sgl_phys_addr.lo =
1785 					(u32)(qedi_conn->gen_pdu.resp_dma_addr);
1786 		rx_sgl_task_params.sgl_phys_addr.hi =
1787 			     (u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
1788 		rx_sgl_task_params.total_buffer_size = resp_sge->sge_len;
1789 		rx_sgl_task_params.num_sges = 1;
1790 	}
1791 
1792 	/* Fill fw input params */
1793 	task_params.context = fw_task_ctx;
1794 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
1795 	task_params.itid = tid;
1796 	task_params.cq_rss_number = 0;
1797 	task_params.tx_io_size = data_len;
1798 	task_params.rx_io_size = resp_sge->sge_len;
1799 
1800 	sq_idx = qedi_get_wqe_idx(qedi_conn);
1801 	task_params.sqe = &ep->sq[sq_idx];
1802 
1803 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
1804 	rval = init_initiator_nop_out_task(&task_params,
1805 					   &nop_out_pdu_header,
1806 					   &tx_sgl_task_params,
1807 					   &rx_sgl_task_params);
1808 	if (rval)
1809 		return -1;
1810 
1811 	qedi_ring_doorbell(qedi_conn);
1812 	return 0;
1813 }
1814 
1815 static int qedi_split_bd(struct qedi_cmd *cmd, u64 addr, int sg_len,
1816 			 int bd_index)
1817 {
1818 	struct scsi_sge *bd = cmd->io_tbl.sge_tbl;
1819 	int frag_size, sg_frags;
1820 
1821 	sg_frags = 0;
1822 
1823 	while (sg_len) {
1824 		if (addr % QEDI_PAGE_SIZE)
1825 			frag_size =
1826 				   (QEDI_PAGE_SIZE - (addr % QEDI_PAGE_SIZE));
1827 		else
1828 			frag_size = (sg_len > QEDI_BD_SPLIT_SZ) ? 0 :
1829 				    (sg_len % QEDI_BD_SPLIT_SZ);
1830 
1831 		if (frag_size == 0)
1832 			frag_size = QEDI_BD_SPLIT_SZ;
1833 
1834 		bd[bd_index + sg_frags].sge_addr.lo = (addr & 0xffffffff);
1835 		bd[bd_index + sg_frags].sge_addr.hi = (addr >> 32);
1836 		bd[bd_index + sg_frags].sge_len = (u16)frag_size;
1837 		QEDI_INFO(&cmd->conn->qedi->dbg_ctx, QEDI_LOG_IO,
1838 			  "split sge %d: addr=%llx, len=%x",
1839 			  (bd_index + sg_frags), addr, frag_size);
1840 
1841 		addr += (u64)frag_size;
1842 		sg_frags++;
1843 		sg_len -= frag_size;
1844 	}
1845 	return sg_frags;
1846 }
1847 
1848 static int qedi_map_scsi_sg(struct qedi_ctx *qedi, struct qedi_cmd *cmd)
1849 {
1850 	struct scsi_cmnd *sc = cmd->scsi_cmd;
1851 	struct scsi_sge *bd = cmd->io_tbl.sge_tbl;
1852 	struct scatterlist *sg;
1853 	int byte_count = 0;
1854 	int bd_count = 0;
1855 	int sg_count;
1856 	int sg_len;
1857 	int sg_frags;
1858 	u64 addr, end_addr;
1859 	int i;
1860 
1861 	WARN_ON(scsi_sg_count(sc) > QEDI_ISCSI_MAX_BDS_PER_CMD);
1862 
1863 	sg_count = dma_map_sg(&qedi->pdev->dev, scsi_sglist(sc),
1864 			      scsi_sg_count(sc), sc->sc_data_direction);
1865 
1866 	/*
1867 	 * New condition to send single SGE as cached-SGL.
1868 	 * Single SGE with length less than 64K.
1869 	 */
1870 	sg = scsi_sglist(sc);
1871 	if ((sg_count == 1) && (sg_dma_len(sg) <= MAX_SGLEN_FOR_CACHESGL)) {
1872 		sg_len = sg_dma_len(sg);
1873 		addr = (u64)sg_dma_address(sg);
1874 
1875 		bd[bd_count].sge_addr.lo = (addr & 0xffffffff);
1876 		bd[bd_count].sge_addr.hi = (addr >> 32);
1877 		bd[bd_count].sge_len = (u16)sg_len;
1878 
1879 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
1880 			  "single-cashed-sgl: bd_count:%d addr=%llx, len=%x",
1881 			  sg_count, addr, sg_len);
1882 
1883 		return ++bd_count;
1884 	}
1885 
1886 	scsi_for_each_sg(sc, sg, sg_count, i) {
1887 		sg_len = sg_dma_len(sg);
1888 		addr = (u64)sg_dma_address(sg);
1889 		end_addr = (addr + sg_len);
1890 
1891 		/*
1892 		 * first sg elem in the 'list',
1893 		 * check if end addr is page-aligned.
1894 		 */
1895 		if ((i == 0) && (sg_count > 1) && (end_addr % QEDI_PAGE_SIZE))
1896 			cmd->use_slowpath = true;
1897 
1898 		/*
1899 		 * last sg elem in the 'list',
1900 		 * check if start addr is page-aligned.
1901 		 */
1902 		else if ((i == (sg_count - 1)) &&
1903 			 (sg_count > 1) && (addr % QEDI_PAGE_SIZE))
1904 			cmd->use_slowpath = true;
1905 
1906 		/*
1907 		 * middle sg elements in list,
1908 		 * check if start and end addr is page-aligned
1909 		 */
1910 		else if ((i != 0) && (i != (sg_count - 1)) &&
1911 			 ((addr % QEDI_PAGE_SIZE) ||
1912 			 (end_addr % QEDI_PAGE_SIZE)))
1913 			cmd->use_slowpath = true;
1914 
1915 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO, "sg[%d] size=0x%x",
1916 			  i, sg_len);
1917 
1918 		if (sg_len > QEDI_BD_SPLIT_SZ) {
1919 			sg_frags = qedi_split_bd(cmd, addr, sg_len, bd_count);
1920 		} else {
1921 			sg_frags = 1;
1922 			bd[bd_count].sge_addr.lo = addr & 0xffffffff;
1923 			bd[bd_count].sge_addr.hi = addr >> 32;
1924 			bd[bd_count].sge_len = sg_len;
1925 		}
1926 		byte_count += sg_len;
1927 		bd_count += sg_frags;
1928 	}
1929 
1930 	if (byte_count != scsi_bufflen(sc))
1931 		QEDI_ERR(&qedi->dbg_ctx,
1932 			 "byte_count = %d != scsi_bufflen = %d\n", byte_count,
1933 			 scsi_bufflen(sc));
1934 	else
1935 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO, "byte_count = %d\n",
1936 			  byte_count);
1937 
1938 	WARN_ON(byte_count != scsi_bufflen(sc));
1939 
1940 	return bd_count;
1941 }
1942 
1943 static void qedi_iscsi_map_sg_list(struct qedi_cmd *cmd)
1944 {
1945 	int bd_count;
1946 	struct scsi_cmnd *sc = cmd->scsi_cmd;
1947 
1948 	if (scsi_sg_count(sc)) {
1949 		bd_count  = qedi_map_scsi_sg(cmd->conn->qedi, cmd);
1950 		if (bd_count == 0)
1951 			return;
1952 	} else {
1953 		struct scsi_sge *bd = cmd->io_tbl.sge_tbl;
1954 
1955 		bd[0].sge_addr.lo = 0;
1956 		bd[0].sge_addr.hi = 0;
1957 		bd[0].sge_len = 0;
1958 		bd_count = 0;
1959 	}
1960 	cmd->io_tbl.sge_valid = bd_count;
1961 }
1962 
1963 static void qedi_cpy_scsi_cdb(struct scsi_cmnd *sc, u32 *dstp)
1964 {
1965 	u32 dword;
1966 	int lpcnt;
1967 	u8 *srcp;
1968 
1969 	lpcnt = sc->cmd_len / sizeof(dword);
1970 	srcp = (u8 *)sc->cmnd;
1971 	while (lpcnt--) {
1972 		memcpy(&dword, (const void *)srcp, 4);
1973 		*dstp = cpu_to_be32(dword);
1974 		srcp += 4;
1975 		dstp++;
1976 	}
1977 	if (sc->cmd_len & 0x3) {
1978 		dword = (u32)srcp[0] | ((u32)srcp[1] << 8);
1979 		*dstp = cpu_to_be32(dword);
1980 	}
1981 }
1982 
1983 void qedi_trace_io(struct qedi_ctx *qedi, struct iscsi_task *task,
1984 		   u16 tid, int8_t direction)
1985 {
1986 	struct qedi_io_log *io_log;
1987 	struct iscsi_conn *conn = task->conn;
1988 	struct qedi_conn *qedi_conn = conn->dd_data;
1989 	struct scsi_cmnd *sc_cmd = task->sc;
1990 	unsigned long flags;
1991 
1992 	spin_lock_irqsave(&qedi->io_trace_lock, flags);
1993 
1994 	io_log = &qedi->io_trace_buf[qedi->io_trace_idx];
1995 	io_log->direction = direction;
1996 	io_log->task_id = tid;
1997 	io_log->cid = qedi_conn->iscsi_conn_id;
1998 	io_log->lun = sc_cmd->device->lun;
1999 	io_log->op = sc_cmd->cmnd[0];
2000 	io_log->lba[0] = sc_cmd->cmnd[2];
2001 	io_log->lba[1] = sc_cmd->cmnd[3];
2002 	io_log->lba[2] = sc_cmd->cmnd[4];
2003 	io_log->lba[3] = sc_cmd->cmnd[5];
2004 	io_log->bufflen = scsi_bufflen(sc_cmd);
2005 	io_log->sg_count = scsi_sg_count(sc_cmd);
2006 	io_log->fast_sgs = qedi->fast_sgls;
2007 	io_log->cached_sgs = qedi->cached_sgls;
2008 	io_log->slow_sgs = qedi->slow_sgls;
2009 	io_log->cached_sge = qedi->use_cached_sge;
2010 	io_log->slow_sge = qedi->use_slow_sge;
2011 	io_log->fast_sge = qedi->use_fast_sge;
2012 	io_log->result = sc_cmd->result;
2013 	io_log->jiffies = jiffies;
2014 	io_log->blk_req_cpu = smp_processor_id();
2015 
2016 	if (direction == QEDI_IO_TRACE_REQ) {
2017 		/* For requests we only care about the submission CPU */
2018 		io_log->req_cpu = smp_processor_id() % qedi->num_queues;
2019 		io_log->intr_cpu = 0;
2020 		io_log->blk_rsp_cpu = 0;
2021 	} else if (direction == QEDI_IO_TRACE_RSP) {
2022 		io_log->req_cpu = smp_processor_id() % qedi->num_queues;
2023 		io_log->intr_cpu = qedi->intr_cpu;
2024 		io_log->blk_rsp_cpu = smp_processor_id();
2025 	}
2026 
2027 	qedi->io_trace_idx++;
2028 	if (qedi->io_trace_idx == QEDI_IO_TRACE_SIZE)
2029 		qedi->io_trace_idx = 0;
2030 
2031 	qedi->use_cached_sge = false;
2032 	qedi->use_slow_sge = false;
2033 	qedi->use_fast_sge = false;
2034 
2035 	spin_unlock_irqrestore(&qedi->io_trace_lock, flags);
2036 }
2037 
2038 int qedi_iscsi_send_ioreq(struct iscsi_task *task)
2039 {
2040 	struct iscsi_conn *conn = task->conn;
2041 	struct iscsi_session *session = conn->session;
2042 	struct Scsi_Host *shost = iscsi_session_to_shost(session->cls_session);
2043 	struct qedi_ctx *qedi = iscsi_host_priv(shost);
2044 	struct qedi_conn *qedi_conn = conn->dd_data;
2045 	struct qedi_cmd *cmd = task->dd_data;
2046 	struct scsi_cmnd *sc = task->sc;
2047 	struct iscsi_cmd_hdr cmd_pdu_header;
2048 	struct scsi_sgl_task_params tx_sgl_task_params;
2049 	struct scsi_sgl_task_params rx_sgl_task_params;
2050 	struct scsi_sgl_task_params *prx_sgl = NULL;
2051 	struct scsi_sgl_task_params *ptx_sgl = NULL;
2052 	struct iscsi_task_params task_params;
2053 	struct iscsi_conn_params conn_params;
2054 	struct scsi_initiator_cmd_params cmd_params;
2055 	struct e4_iscsi_task_context *fw_task_ctx;
2056 	struct iscsi_cls_conn *cls_conn;
2057 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)task->hdr;
2058 	enum iscsi_task_type task_type = MAX_ISCSI_TASK_TYPE;
2059 	struct qedi_endpoint *ep;
2060 	u32 scsi_lun[2];
2061 	s16 tid = 0;
2062 	u16 sq_idx = 0;
2063 	u16 cq_idx;
2064 	int rval = 0;
2065 
2066 	ep = qedi_conn->ep;
2067 	cls_conn = qedi_conn->cls_conn;
2068 	conn = cls_conn->dd_data;
2069 
2070 	qedi_iscsi_map_sg_list(cmd);
2071 	int_to_scsilun(sc->device->lun, (struct scsi_lun *)scsi_lun);
2072 
2073 	tid = qedi_get_task_idx(qedi);
2074 	if (tid == -1)
2075 		return -ENOMEM;
2076 
2077 	fw_task_ctx =
2078 	     (struct e4_iscsi_task_context *)qedi_get_task_mem(&qedi->tasks,
2079 							       tid);
2080 	memset(fw_task_ctx, 0, sizeof(struct e4_iscsi_task_context));
2081 
2082 	cmd->task_id = tid;
2083 
2084 	memset(&task_params, 0, sizeof(task_params));
2085 	memset(&cmd_pdu_header, 0, sizeof(cmd_pdu_header));
2086 	memset(&tx_sgl_task_params, 0, sizeof(tx_sgl_task_params));
2087 	memset(&rx_sgl_task_params, 0, sizeof(rx_sgl_task_params));
2088 	memset(&conn_params, 0, sizeof(conn_params));
2089 	memset(&cmd_params, 0, sizeof(cmd_params));
2090 
2091 	cq_idx = smp_processor_id() % qedi->num_queues;
2092 	/* Update header info */
2093 	SET_FIELD(cmd_pdu_header.flags_attr, ISCSI_CMD_HDR_ATTR,
2094 		  ISCSI_ATTR_SIMPLE);
2095 	if (hdr->cdb[0] != TEST_UNIT_READY) {
2096 		if (sc->sc_data_direction == DMA_TO_DEVICE) {
2097 			SET_FIELD(cmd_pdu_header.flags_attr,
2098 				  ISCSI_CMD_HDR_WRITE, 1);
2099 			task_type = ISCSI_TASK_TYPE_INITIATOR_WRITE;
2100 		} else {
2101 			SET_FIELD(cmd_pdu_header.flags_attr,
2102 				  ISCSI_CMD_HDR_READ, 1);
2103 			task_type = ISCSI_TASK_TYPE_INITIATOR_READ;
2104 		}
2105 	}
2106 
2107 	cmd_pdu_header.lun.lo = be32_to_cpu(scsi_lun[0]);
2108 	cmd_pdu_header.lun.hi = be32_to_cpu(scsi_lun[1]);
2109 
2110 	qedi_update_itt_map(qedi, tid, task->itt, cmd);
2111 	cmd_pdu_header.itt = qedi_set_itt(tid, get_itt(task->itt));
2112 	cmd_pdu_header.expected_transfer_length = cpu_to_be32(hdr->data_length);
2113 	cmd_pdu_header.hdr_second_dword = ntoh24(hdr->dlength);
2114 	cmd_pdu_header.cmd_sn = be32_to_cpu(hdr->cmdsn);
2115 	cmd_pdu_header.hdr_first_byte = hdr->opcode;
2116 	qedi_cpy_scsi_cdb(sc, (u32 *)cmd_pdu_header.cdb);
2117 
2118 	/* Fill tx AHS and rx buffer */
2119 	if (task_type == ISCSI_TASK_TYPE_INITIATOR_WRITE) {
2120 		tx_sgl_task_params.sgl = cmd->io_tbl.sge_tbl;
2121 		tx_sgl_task_params.sgl_phys_addr.lo =
2122 						 (u32)(cmd->io_tbl.sge_tbl_dma);
2123 		tx_sgl_task_params.sgl_phys_addr.hi =
2124 				      (u32)((u64)cmd->io_tbl.sge_tbl_dma >> 32);
2125 		tx_sgl_task_params.total_buffer_size = scsi_bufflen(sc);
2126 		tx_sgl_task_params.num_sges = cmd->io_tbl.sge_valid;
2127 		if (cmd->use_slowpath)
2128 			tx_sgl_task_params.small_mid_sge = true;
2129 	} else if (task_type == ISCSI_TASK_TYPE_INITIATOR_READ) {
2130 		rx_sgl_task_params.sgl = cmd->io_tbl.sge_tbl;
2131 		rx_sgl_task_params.sgl_phys_addr.lo =
2132 						 (u32)(cmd->io_tbl.sge_tbl_dma);
2133 		rx_sgl_task_params.sgl_phys_addr.hi =
2134 				      (u32)((u64)cmd->io_tbl.sge_tbl_dma >> 32);
2135 		rx_sgl_task_params.total_buffer_size = scsi_bufflen(sc);
2136 		rx_sgl_task_params.num_sges = cmd->io_tbl.sge_valid;
2137 	}
2138 
2139 	/* Add conn param */
2140 	conn_params.first_burst_length = conn->session->first_burst;
2141 	conn_params.max_send_pdu_length = conn->max_xmit_dlength;
2142 	conn_params.max_burst_length = conn->session->max_burst;
2143 	if (conn->session->initial_r2t_en)
2144 		conn_params.initial_r2t = true;
2145 	if (conn->session->imm_data_en)
2146 		conn_params.immediate_data = true;
2147 
2148 	/* Add cmd params */
2149 	cmd_params.sense_data_buffer_phys_addr.lo = (u32)cmd->sense_buffer_dma;
2150 	cmd_params.sense_data_buffer_phys_addr.hi =
2151 					(u32)((u64)cmd->sense_buffer_dma >> 32);
2152 	/* Fill fw input params */
2153 	task_params.context = fw_task_ctx;
2154 	task_params.conn_icid = (u16)qedi_conn->iscsi_conn_id;
2155 	task_params.itid = tid;
2156 	task_params.cq_rss_number = cq_idx;
2157 	if (task_type == ISCSI_TASK_TYPE_INITIATOR_WRITE)
2158 		task_params.tx_io_size = scsi_bufflen(sc);
2159 	else if (task_type == ISCSI_TASK_TYPE_INITIATOR_READ)
2160 		task_params.rx_io_size = scsi_bufflen(sc);
2161 
2162 	sq_idx = qedi_get_wqe_idx(qedi_conn);
2163 	task_params.sqe = &ep->sq[sq_idx];
2164 
2165 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
2166 		  "%s: %s-SGL: sg_len=0x%x num_sges=0x%x first-sge-lo=0x%x first-sge-hi=0x%x\n",
2167 		  (task_type == ISCSI_TASK_TYPE_INITIATOR_WRITE) ?
2168 		  "Write " : "Read ", (cmd->io_tbl.sge_valid == 1) ?
2169 		  "Single" : (cmd->use_slowpath ? "SLOW" : "FAST"),
2170 		  (u16)cmd->io_tbl.sge_valid, scsi_bufflen(sc),
2171 		  (u32)(cmd->io_tbl.sge_tbl_dma),
2172 		  (u32)((u64)cmd->io_tbl.sge_tbl_dma >> 32));
2173 
2174 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
2175 
2176 	if (task_params.tx_io_size != 0)
2177 		ptx_sgl = &tx_sgl_task_params;
2178 	if (task_params.rx_io_size != 0)
2179 		prx_sgl = &rx_sgl_task_params;
2180 
2181 	rval = init_initiator_rw_iscsi_task(&task_params, &conn_params,
2182 					    &cmd_params, &cmd_pdu_header,
2183 					    ptx_sgl, prx_sgl,
2184 					    NULL);
2185 	if (rval)
2186 		return -1;
2187 
2188 	spin_lock(&qedi_conn->list_lock);
2189 	list_add_tail(&cmd->io_cmd, &qedi_conn->active_cmd_list);
2190 	cmd->io_cmd_in_list = true;
2191 	qedi_conn->active_cmd_count++;
2192 	spin_unlock(&qedi_conn->list_lock);
2193 
2194 	qedi_ring_doorbell(qedi_conn);
2195 	return 0;
2196 }
2197 
2198 int qedi_iscsi_cleanup_task(struct iscsi_task *task, bool mark_cmd_node_deleted)
2199 {
2200 	struct iscsi_task_params task_params;
2201 	struct qedi_endpoint *ep;
2202 	struct iscsi_conn *conn = task->conn;
2203 	struct qedi_conn *qedi_conn = conn->dd_data;
2204 	struct qedi_cmd *cmd = task->dd_data;
2205 	u16 sq_idx = 0;
2206 	int rval = 0;
2207 
2208 	QEDI_INFO(&qedi_conn->qedi->dbg_ctx, QEDI_LOG_SCSI_TM,
2209 		  "issue cleanup tid=0x%x itt=0x%x task_state=%d cmd_state=0%x cid=0x%x\n",
2210 		  cmd->task_id, get_itt(task->itt), task->state,
2211 		  cmd->state, qedi_conn->iscsi_conn_id);
2212 
2213 	memset(&task_params, 0, sizeof(task_params));
2214 	ep = qedi_conn->ep;
2215 
2216 	sq_idx = qedi_get_wqe_idx(qedi_conn);
2217 
2218 	task_params.sqe = &ep->sq[sq_idx];
2219 	memset(task_params.sqe, 0, sizeof(struct iscsi_wqe));
2220 	task_params.itid = cmd->task_id;
2221 
2222 	rval = init_cleanup_task(&task_params);
2223 	if (rval)
2224 		return rval;
2225 
2226 	qedi_ring_doorbell(qedi_conn);
2227 	return 0;
2228 }
2229