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