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