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