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