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