xref: /openbmc/linux/drivers/scsi/qedi/qedi_iscsi.c (revision b40f3894)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * QLogic iSCSI Offload Driver
4  * Copyright (c) 2016 Cavium Inc.
5  */
6 
7 #include <linux/blkdev.h>
8 #include <linux/etherdevice.h>
9 #include <linux/if_ether.h>
10 #include <linux/if_vlan.h>
11 #include <scsi/scsi_tcq.h>
12 
13 #include "qedi.h"
14 #include "qedi_iscsi.h"
15 #include "qedi_gbl.h"
16 
17 int qedi_recover_all_conns(struct qedi_ctx *qedi)
18 {
19 	struct qedi_conn *qedi_conn;
20 	int i;
21 
22 	for (i = 0; i < qedi->max_active_conns; i++) {
23 		qedi_conn = qedi_get_conn_from_id(qedi, i);
24 		if (!qedi_conn)
25 			continue;
26 
27 		qedi_start_conn_recovery(qedi, qedi_conn);
28 	}
29 
30 	return SUCCESS;
31 }
32 
33 static int qedi_eh_host_reset(struct scsi_cmnd *cmd)
34 {
35 	struct Scsi_Host *shost = cmd->device->host;
36 	struct qedi_ctx *qedi;
37 
38 	qedi = iscsi_host_priv(shost);
39 
40 	return qedi_recover_all_conns(qedi);
41 }
42 
43 struct scsi_host_template qedi_host_template = {
44 	.module = THIS_MODULE,
45 	.name = "QLogic QEDI 25/40/100Gb iSCSI Initiator Driver",
46 	.proc_name = QEDI_MODULE_NAME,
47 	.queuecommand = iscsi_queuecommand,
48 	.eh_timed_out = iscsi_eh_cmd_timed_out,
49 	.eh_abort_handler = iscsi_eh_abort,
50 	.eh_device_reset_handler = iscsi_eh_device_reset,
51 	.eh_target_reset_handler = iscsi_eh_recover_target,
52 	.eh_host_reset_handler = qedi_eh_host_reset,
53 	.target_alloc = iscsi_target_alloc,
54 	.change_queue_depth = scsi_change_queue_depth,
55 	.can_queue = QEDI_MAX_ISCSI_TASK,
56 	.this_id = -1,
57 	.sg_tablesize = QEDI_ISCSI_MAX_BDS_PER_CMD,
58 	.max_sectors = 0xffff,
59 	.dma_boundary = QEDI_HW_DMA_BOUNDARY,
60 	.cmd_per_lun = 128,
61 	.shost_attrs = qedi_shost_attrs,
62 };
63 
64 static void qedi_conn_free_login_resources(struct qedi_ctx *qedi,
65 					   struct qedi_conn *qedi_conn)
66 {
67 	if (qedi_conn->gen_pdu.resp_bd_tbl) {
68 		dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
69 				  qedi_conn->gen_pdu.resp_bd_tbl,
70 				  qedi_conn->gen_pdu.resp_bd_dma);
71 		qedi_conn->gen_pdu.resp_bd_tbl = NULL;
72 	}
73 
74 	if (qedi_conn->gen_pdu.req_bd_tbl) {
75 		dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
76 				  qedi_conn->gen_pdu.req_bd_tbl,
77 				  qedi_conn->gen_pdu.req_bd_dma);
78 		qedi_conn->gen_pdu.req_bd_tbl = NULL;
79 	}
80 
81 	if (qedi_conn->gen_pdu.resp_buf) {
82 		dma_free_coherent(&qedi->pdev->dev,
83 				  ISCSI_DEF_MAX_RECV_SEG_LEN,
84 				  qedi_conn->gen_pdu.resp_buf,
85 				  qedi_conn->gen_pdu.resp_dma_addr);
86 		qedi_conn->gen_pdu.resp_buf = NULL;
87 	}
88 
89 	if (qedi_conn->gen_pdu.req_buf) {
90 		dma_free_coherent(&qedi->pdev->dev,
91 				  ISCSI_DEF_MAX_RECV_SEG_LEN,
92 				  qedi_conn->gen_pdu.req_buf,
93 				  qedi_conn->gen_pdu.req_dma_addr);
94 		qedi_conn->gen_pdu.req_buf = NULL;
95 	}
96 }
97 
98 static int qedi_conn_alloc_login_resources(struct qedi_ctx *qedi,
99 					   struct qedi_conn *qedi_conn)
100 {
101 	qedi_conn->gen_pdu.req_buf =
102 		dma_alloc_coherent(&qedi->pdev->dev,
103 				   ISCSI_DEF_MAX_RECV_SEG_LEN,
104 				   &qedi_conn->gen_pdu.req_dma_addr,
105 				   GFP_KERNEL);
106 	if (!qedi_conn->gen_pdu.req_buf)
107 		goto login_req_buf_failure;
108 
109 	qedi_conn->gen_pdu.req_buf_size = 0;
110 	qedi_conn->gen_pdu.req_wr_ptr = qedi_conn->gen_pdu.req_buf;
111 
112 	qedi_conn->gen_pdu.resp_buf =
113 		dma_alloc_coherent(&qedi->pdev->dev,
114 				   ISCSI_DEF_MAX_RECV_SEG_LEN,
115 				   &qedi_conn->gen_pdu.resp_dma_addr,
116 				   GFP_KERNEL);
117 	if (!qedi_conn->gen_pdu.resp_buf)
118 		goto login_resp_buf_failure;
119 
120 	qedi_conn->gen_pdu.resp_buf_size = ISCSI_DEF_MAX_RECV_SEG_LEN;
121 	qedi_conn->gen_pdu.resp_wr_ptr = qedi_conn->gen_pdu.resp_buf;
122 
123 	qedi_conn->gen_pdu.req_bd_tbl =
124 		dma_alloc_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
125 				   &qedi_conn->gen_pdu.req_bd_dma, GFP_KERNEL);
126 	if (!qedi_conn->gen_pdu.req_bd_tbl)
127 		goto login_req_bd_tbl_failure;
128 
129 	qedi_conn->gen_pdu.resp_bd_tbl =
130 		dma_alloc_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
131 				   &qedi_conn->gen_pdu.resp_bd_dma,
132 				   GFP_KERNEL);
133 	if (!qedi_conn->gen_pdu.resp_bd_tbl)
134 		goto login_resp_bd_tbl_failure;
135 
136 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SESS,
137 		  "Allocation successful, cid=0x%x\n",
138 		  qedi_conn->iscsi_conn_id);
139 	return 0;
140 
141 login_resp_bd_tbl_failure:
142 	dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
143 			  qedi_conn->gen_pdu.req_bd_tbl,
144 			  qedi_conn->gen_pdu.req_bd_dma);
145 	qedi_conn->gen_pdu.req_bd_tbl = NULL;
146 
147 login_req_bd_tbl_failure:
148 	dma_free_coherent(&qedi->pdev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
149 			  qedi_conn->gen_pdu.resp_buf,
150 			  qedi_conn->gen_pdu.resp_dma_addr);
151 	qedi_conn->gen_pdu.resp_buf = NULL;
152 login_resp_buf_failure:
153 	dma_free_coherent(&qedi->pdev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
154 			  qedi_conn->gen_pdu.req_buf,
155 			  qedi_conn->gen_pdu.req_dma_addr);
156 	qedi_conn->gen_pdu.req_buf = NULL;
157 login_req_buf_failure:
158 	iscsi_conn_printk(KERN_ERR, qedi_conn->cls_conn->dd_data,
159 			  "login resource alloc failed!!\n");
160 	return -ENOMEM;
161 }
162 
163 static void qedi_destroy_cmd_pool(struct qedi_ctx *qedi,
164 				  struct iscsi_session *session)
165 {
166 	int i;
167 
168 	for (i = 0; i < session->cmds_max; i++) {
169 		struct iscsi_task *task = session->cmds[i];
170 		struct qedi_cmd *cmd = task->dd_data;
171 
172 		if (cmd->io_tbl.sge_tbl)
173 			dma_free_coherent(&qedi->pdev->dev,
174 					  QEDI_ISCSI_MAX_BDS_PER_CMD *
175 					  sizeof(struct scsi_sge),
176 					  cmd->io_tbl.sge_tbl,
177 					  cmd->io_tbl.sge_tbl_dma);
178 
179 		if (cmd->sense_buffer)
180 			dma_free_coherent(&qedi->pdev->dev,
181 					  SCSI_SENSE_BUFFERSIZE,
182 					  cmd->sense_buffer,
183 					  cmd->sense_buffer_dma);
184 	}
185 }
186 
187 static int qedi_alloc_sget(struct qedi_ctx *qedi, struct iscsi_session *session,
188 			   struct qedi_cmd *cmd)
189 {
190 	struct qedi_io_bdt *io = &cmd->io_tbl;
191 	struct scsi_sge *sge;
192 
193 	io->sge_tbl = dma_alloc_coherent(&qedi->pdev->dev,
194 					 QEDI_ISCSI_MAX_BDS_PER_CMD *
195 					 sizeof(*sge),
196 					 &io->sge_tbl_dma, GFP_KERNEL);
197 	if (!io->sge_tbl) {
198 		iscsi_session_printk(KERN_ERR, session,
199 				     "Could not allocate BD table.\n");
200 		return -ENOMEM;
201 	}
202 
203 	io->sge_valid = 0;
204 	return 0;
205 }
206 
207 static int qedi_setup_cmd_pool(struct qedi_ctx *qedi,
208 			       struct iscsi_session *session)
209 {
210 	int i;
211 
212 	for (i = 0; i < session->cmds_max; i++) {
213 		struct iscsi_task *task = session->cmds[i];
214 		struct qedi_cmd *cmd = task->dd_data;
215 
216 		task->hdr = &cmd->hdr;
217 		task->hdr_max = sizeof(struct iscsi_hdr);
218 
219 		if (qedi_alloc_sget(qedi, session, cmd))
220 			goto free_sgets;
221 
222 		cmd->sense_buffer = dma_alloc_coherent(&qedi->pdev->dev,
223 						       SCSI_SENSE_BUFFERSIZE,
224 						       &cmd->sense_buffer_dma,
225 						       GFP_KERNEL);
226 		if (!cmd->sense_buffer)
227 			goto free_sgets;
228 	}
229 
230 	return 0;
231 
232 free_sgets:
233 	qedi_destroy_cmd_pool(qedi, session);
234 	return -ENOMEM;
235 }
236 
237 static struct iscsi_cls_session *
238 qedi_session_create(struct iscsi_endpoint *ep, u16 cmds_max,
239 		    u16 qdepth, uint32_t initial_cmdsn)
240 {
241 	struct Scsi_Host *shost;
242 	struct iscsi_cls_session *cls_session;
243 	struct qedi_ctx *qedi;
244 	struct qedi_endpoint *qedi_ep;
245 
246 	if (!ep)
247 		return NULL;
248 
249 	qedi_ep = ep->dd_data;
250 	shost = qedi_ep->qedi->shost;
251 	qedi = iscsi_host_priv(shost);
252 
253 	if (cmds_max > qedi->max_sqes)
254 		cmds_max = qedi->max_sqes;
255 	else if (cmds_max < QEDI_SQ_WQES_MIN)
256 		cmds_max = QEDI_SQ_WQES_MIN;
257 
258 	cls_session = iscsi_session_setup(&qedi_iscsi_transport, shost,
259 					  cmds_max, 0, sizeof(struct qedi_cmd),
260 					  initial_cmdsn, ISCSI_MAX_TARGET);
261 	if (!cls_session) {
262 		QEDI_ERR(&qedi->dbg_ctx,
263 			 "Failed to setup session for ep=%p\n", qedi_ep);
264 		return NULL;
265 	}
266 
267 	if (qedi_setup_cmd_pool(qedi, cls_session->dd_data)) {
268 		QEDI_ERR(&qedi->dbg_ctx,
269 			 "Failed to setup cmd pool for ep=%p\n", qedi_ep);
270 		goto session_teardown;
271 	}
272 
273 	return cls_session;
274 
275 session_teardown:
276 	iscsi_session_teardown(cls_session);
277 	return NULL;
278 }
279 
280 static void qedi_session_destroy(struct iscsi_cls_session *cls_session)
281 {
282 	struct iscsi_session *session = cls_session->dd_data;
283 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
284 	struct qedi_ctx *qedi = iscsi_host_priv(shost);
285 
286 	qedi_destroy_cmd_pool(qedi, session);
287 	iscsi_session_teardown(cls_session);
288 }
289 
290 static struct iscsi_cls_conn *
291 qedi_conn_create(struct iscsi_cls_session *cls_session, uint32_t cid)
292 {
293 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
294 	struct qedi_ctx *qedi = iscsi_host_priv(shost);
295 	struct iscsi_cls_conn *cls_conn;
296 	struct qedi_conn *qedi_conn;
297 	struct iscsi_conn *conn;
298 
299 	cls_conn = iscsi_conn_setup(cls_session, sizeof(*qedi_conn),
300 				    cid);
301 	if (!cls_conn) {
302 		QEDI_ERR(&qedi->dbg_ctx,
303 			 "conn_new: iscsi conn setup failed, cid=0x%x, cls_sess=%p!\n",
304 			 cid, cls_session);
305 		return NULL;
306 	}
307 
308 	conn = cls_conn->dd_data;
309 	qedi_conn = conn->dd_data;
310 	qedi_conn->cls_conn = cls_conn;
311 	qedi_conn->qedi = qedi;
312 	qedi_conn->ep = NULL;
313 	qedi_conn->active_cmd_count = 0;
314 	INIT_LIST_HEAD(&qedi_conn->active_cmd_list);
315 	spin_lock_init(&qedi_conn->list_lock);
316 
317 	if (qedi_conn_alloc_login_resources(qedi, qedi_conn)) {
318 		iscsi_conn_printk(KERN_ALERT, conn,
319 				  "conn_new: login resc alloc failed, cid=0x%x, cls_sess=%p!!\n",
320 				   cid, cls_session);
321 		goto free_conn;
322 	}
323 
324 	return cls_conn;
325 
326 free_conn:
327 	iscsi_conn_teardown(cls_conn);
328 	return NULL;
329 }
330 
331 void qedi_mark_device_missing(struct iscsi_cls_session *cls_session)
332 {
333 	struct iscsi_session *session = cls_session->dd_data;
334 	struct qedi_conn *qedi_conn = session->leadconn->dd_data;
335 
336 	spin_lock_bh(&session->frwd_lock);
337 	set_bit(QEDI_BLOCK_IO, &qedi_conn->qedi->flags);
338 	spin_unlock_bh(&session->frwd_lock);
339 }
340 
341 void qedi_mark_device_available(struct iscsi_cls_session *cls_session)
342 {
343 	struct iscsi_session *session = cls_session->dd_data;
344 	struct qedi_conn *qedi_conn = session->leadconn->dd_data;
345 
346 	spin_lock_bh(&session->frwd_lock);
347 	clear_bit(QEDI_BLOCK_IO, &qedi_conn->qedi->flags);
348 	spin_unlock_bh(&session->frwd_lock);
349 }
350 
351 static int qedi_bind_conn_to_iscsi_cid(struct qedi_ctx *qedi,
352 				       struct qedi_conn *qedi_conn)
353 {
354 	u32 iscsi_cid = qedi_conn->iscsi_conn_id;
355 
356 	if (qedi->cid_que.conn_cid_tbl[iscsi_cid]) {
357 		iscsi_conn_printk(KERN_ALERT, qedi_conn->cls_conn->dd_data,
358 				  "conn bind - entry #%d not free\n",
359 				  iscsi_cid);
360 		return -EBUSY;
361 	}
362 
363 	qedi->cid_que.conn_cid_tbl[iscsi_cid] = qedi_conn;
364 	return 0;
365 }
366 
367 struct qedi_conn *qedi_get_conn_from_id(struct qedi_ctx *qedi, u32 iscsi_cid)
368 {
369 	if (!qedi->cid_que.conn_cid_tbl) {
370 		QEDI_ERR(&qedi->dbg_ctx, "missing conn<->cid table\n");
371 		return NULL;
372 
373 	} else if (iscsi_cid >= qedi->max_active_conns) {
374 		QEDI_ERR(&qedi->dbg_ctx, "wrong cid #%d\n", iscsi_cid);
375 		return NULL;
376 	}
377 	return qedi->cid_que.conn_cid_tbl[iscsi_cid];
378 }
379 
380 static int qedi_conn_bind(struct iscsi_cls_session *cls_session,
381 			  struct iscsi_cls_conn *cls_conn,
382 			  u64 transport_fd, int is_leading)
383 {
384 	struct iscsi_conn *conn = cls_conn->dd_data;
385 	struct qedi_conn *qedi_conn = conn->dd_data;
386 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
387 	struct qedi_ctx *qedi = iscsi_host_priv(shost);
388 	struct qedi_endpoint *qedi_ep;
389 	struct iscsi_endpoint *ep;
390 	int rc = 0;
391 
392 	ep = iscsi_lookup_endpoint(transport_fd);
393 	if (!ep)
394 		return -EINVAL;
395 
396 	qedi_ep = ep->dd_data;
397 	if ((qedi_ep->state == EP_STATE_TCP_FIN_RCVD) ||
398 	    (qedi_ep->state == EP_STATE_TCP_RST_RCVD)) {
399 		rc = -EINVAL;
400 		goto put_ep;
401 	}
402 
403 	if (iscsi_conn_bind(cls_session, cls_conn, is_leading)) {
404 		rc = -EINVAL;
405 		goto put_ep;
406 	}
407 
408 
409 	qedi_ep->conn = qedi_conn;
410 	qedi_conn->ep = qedi_ep;
411 	qedi_conn->iscsi_ep = ep;
412 	qedi_conn->iscsi_conn_id = qedi_ep->iscsi_cid;
413 	qedi_conn->fw_cid = qedi_ep->fw_cid;
414 	qedi_conn->cmd_cleanup_req = 0;
415 	qedi_conn->cmd_cleanup_cmpl = 0;
416 
417 	if (qedi_bind_conn_to_iscsi_cid(qedi, qedi_conn)) {
418 		rc = -EINVAL;
419 		goto put_ep;
420 	}
421 
422 
423 	spin_lock_init(&qedi_conn->tmf_work_lock);
424 	INIT_LIST_HEAD(&qedi_conn->tmf_work_list);
425 	init_waitqueue_head(&qedi_conn->wait_queue);
426 put_ep:
427 	iscsi_put_endpoint(ep);
428 	return rc;
429 }
430 
431 static int qedi_iscsi_update_conn(struct qedi_ctx *qedi,
432 				  struct qedi_conn *qedi_conn)
433 {
434 	struct qed_iscsi_params_update *conn_info;
435 	struct iscsi_cls_conn *cls_conn = qedi_conn->cls_conn;
436 	struct iscsi_conn *conn = cls_conn->dd_data;
437 	struct qedi_endpoint *qedi_ep;
438 	int rval;
439 
440 	qedi_ep = qedi_conn->ep;
441 
442 	conn_info = kzalloc(sizeof(*conn_info), GFP_KERNEL);
443 	if (!conn_info) {
444 		QEDI_ERR(&qedi->dbg_ctx, "memory alloc failed\n");
445 		return -ENOMEM;
446 	}
447 
448 	conn_info->update_flag = 0;
449 
450 	if (conn->hdrdgst_en)
451 		SET_FIELD(conn_info->update_flag,
452 			  ISCSI_CONN_UPDATE_RAMROD_PARAMS_HD_EN, true);
453 	if (conn->datadgst_en)
454 		SET_FIELD(conn_info->update_flag,
455 			  ISCSI_CONN_UPDATE_RAMROD_PARAMS_DD_EN, true);
456 	if (conn->session->initial_r2t_en)
457 		SET_FIELD(conn_info->update_flag,
458 			  ISCSI_CONN_UPDATE_RAMROD_PARAMS_INITIAL_R2T,
459 			  true);
460 	if (conn->session->imm_data_en)
461 		SET_FIELD(conn_info->update_flag,
462 			  ISCSI_CONN_UPDATE_RAMROD_PARAMS_IMMEDIATE_DATA,
463 			  true);
464 
465 	conn_info->max_seq_size = conn->session->max_burst;
466 	conn_info->max_recv_pdu_length = conn->max_recv_dlength;
467 	conn_info->max_send_pdu_length = conn->max_xmit_dlength;
468 	conn_info->first_seq_length = conn->session->first_burst;
469 	conn_info->exp_stat_sn = conn->exp_statsn;
470 
471 	rval = qedi_ops->update_conn(qedi->cdev, qedi_ep->handle,
472 				     conn_info);
473 	if (rval) {
474 		rval = -ENXIO;
475 		QEDI_ERR(&qedi->dbg_ctx, "Could not update connection\n");
476 	}
477 
478 	kfree(conn_info);
479 	return rval;
480 }
481 
482 static u16 qedi_calc_mss(u16 pmtu, u8 is_ipv6, u8 tcp_ts_en, u8 vlan_en)
483 {
484 	u16 mss = 0;
485 	u16 hdrs = TCP_HDR_LEN;
486 
487 	if (is_ipv6)
488 		hdrs += IPV6_HDR_LEN;
489 	else
490 		hdrs += IPV4_HDR_LEN;
491 
492 	mss = pmtu - hdrs;
493 
494 	if (!mss)
495 		mss = DEF_MSS;
496 
497 	return mss;
498 }
499 
500 static int qedi_iscsi_offload_conn(struct qedi_endpoint *qedi_ep)
501 {
502 	struct qedi_ctx *qedi = qedi_ep->qedi;
503 	struct qed_iscsi_params_offload *conn_info;
504 	int rval;
505 	int i;
506 
507 	conn_info = kzalloc(sizeof(*conn_info), GFP_KERNEL);
508 	if (!conn_info) {
509 		QEDI_ERR(&qedi->dbg_ctx,
510 			 "Failed to allocate memory ep=%p\n", qedi_ep);
511 		return -ENOMEM;
512 	}
513 
514 	ether_addr_copy(conn_info->src.mac, qedi_ep->src_mac);
515 	ether_addr_copy(conn_info->dst.mac, qedi_ep->dst_mac);
516 
517 	conn_info->src.ip[0] = ntohl(qedi_ep->src_addr[0]);
518 	conn_info->dst.ip[0] = ntohl(qedi_ep->dst_addr[0]);
519 
520 	if (qedi_ep->ip_type == TCP_IPV4) {
521 		conn_info->ip_version = 0;
522 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
523 			  "After ntohl: src_addr=%pI4, dst_addr=%pI4\n",
524 			  qedi_ep->src_addr, qedi_ep->dst_addr);
525 	} else {
526 		for (i = 1; i < 4; i++) {
527 			conn_info->src.ip[i] = ntohl(qedi_ep->src_addr[i]);
528 			conn_info->dst.ip[i] = ntohl(qedi_ep->dst_addr[i]);
529 		}
530 
531 		conn_info->ip_version = 1;
532 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
533 			  "After ntohl: src_addr=%pI6, dst_addr=%pI6\n",
534 			  qedi_ep->src_addr, qedi_ep->dst_addr);
535 	}
536 
537 	conn_info->src.port = qedi_ep->src_port;
538 	conn_info->dst.port = qedi_ep->dst_port;
539 
540 	conn_info->layer_code = ISCSI_SLOW_PATH_LAYER_CODE;
541 	conn_info->sq_pbl_addr = qedi_ep->sq_pbl_dma;
542 	conn_info->vlan_id = qedi_ep->vlan_id;
543 
544 	SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_TS_EN, 1);
545 	SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_DA_EN, 1);
546 	SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_DA_CNT_EN, 1);
547 	SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_KA_EN, 1);
548 
549 	conn_info->default_cq = (qedi_ep->fw_cid % qedi->num_queues);
550 
551 	conn_info->ka_max_probe_cnt = DEF_KA_MAX_PROBE_COUNT;
552 	conn_info->dup_ack_theshold = 3;
553 	conn_info->rcv_wnd = 65535;
554 
555 	conn_info->ss_thresh = 65535;
556 	conn_info->srtt = 300;
557 	conn_info->rtt_var = 150;
558 	conn_info->flow_label = 0;
559 	conn_info->ka_timeout = DEF_KA_TIMEOUT;
560 	conn_info->ka_interval = DEF_KA_INTERVAL;
561 	conn_info->max_rt_time = DEF_MAX_RT_TIME;
562 	conn_info->ttl = DEF_TTL;
563 	conn_info->tos_or_tc = DEF_TOS;
564 	conn_info->remote_port = qedi_ep->dst_port;
565 	conn_info->local_port = qedi_ep->src_port;
566 
567 	conn_info->mss = qedi_calc_mss(qedi_ep->pmtu,
568 				       (qedi_ep->ip_type == TCP_IPV6),
569 				       1, (qedi_ep->vlan_id != 0));
570 
571 	conn_info->cwnd = DEF_MAX_CWND * conn_info->mss;
572 	conn_info->rcv_wnd_scale = 4;
573 	conn_info->da_timeout_value = 200;
574 	conn_info->ack_frequency = 2;
575 
576 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
577 		  "Default cq index [%d], mss [%d]\n",
578 		  conn_info->default_cq, conn_info->mss);
579 
580 	rval = qedi_ops->offload_conn(qedi->cdev, qedi_ep->handle, conn_info);
581 	if (rval)
582 		QEDI_ERR(&qedi->dbg_ctx, "offload_conn returned %d, ep=%p\n",
583 			 rval, qedi_ep);
584 
585 	kfree(conn_info);
586 	return rval;
587 }
588 
589 static int qedi_conn_start(struct iscsi_cls_conn *cls_conn)
590 {
591 	struct iscsi_conn *conn = cls_conn->dd_data;
592 	struct qedi_conn *qedi_conn = conn->dd_data;
593 	struct qedi_ctx *qedi;
594 	int rval;
595 
596 	qedi = qedi_conn->qedi;
597 
598 	rval = qedi_iscsi_update_conn(qedi, qedi_conn);
599 	if (rval) {
600 		iscsi_conn_printk(KERN_ALERT, conn,
601 				  "conn_start: FW offload conn failed.\n");
602 		rval = -EINVAL;
603 		goto start_err;
604 	}
605 
606 	spin_lock(&qedi_conn->tmf_work_lock);
607 	qedi_conn->fw_cleanup_works = 0;
608 	qedi_conn->ep_disconnect_starting = false;
609 	spin_unlock(&qedi_conn->tmf_work_lock);
610 
611 	qedi_conn->abrt_conn = 0;
612 
613 	rval = iscsi_conn_start(cls_conn);
614 	if (rval) {
615 		iscsi_conn_printk(KERN_ALERT, conn,
616 				  "iscsi_conn_start: FW offload conn failed!!\n");
617 	}
618 
619 start_err:
620 	return rval;
621 }
622 
623 static void qedi_conn_destroy(struct iscsi_cls_conn *cls_conn)
624 {
625 	struct iscsi_conn *conn = cls_conn->dd_data;
626 	struct qedi_conn *qedi_conn = conn->dd_data;
627 	struct Scsi_Host *shost;
628 	struct qedi_ctx *qedi;
629 
630 	shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
631 	qedi = iscsi_host_priv(shost);
632 
633 	qedi_conn_free_login_resources(qedi, qedi_conn);
634 	iscsi_conn_teardown(cls_conn);
635 }
636 
637 static int qedi_ep_get_param(struct iscsi_endpoint *ep,
638 			     enum iscsi_param param, char *buf)
639 {
640 	struct qedi_endpoint *qedi_ep = ep->dd_data;
641 	int len;
642 
643 	if (!qedi_ep)
644 		return -ENOTCONN;
645 
646 	switch (param) {
647 	case ISCSI_PARAM_CONN_PORT:
648 		len = sprintf(buf, "%hu\n", qedi_ep->dst_port);
649 		break;
650 	case ISCSI_PARAM_CONN_ADDRESS:
651 		if (qedi_ep->ip_type == TCP_IPV4)
652 			len = sprintf(buf, "%pI4\n", qedi_ep->dst_addr);
653 		else
654 			len = sprintf(buf, "%pI6\n", qedi_ep->dst_addr);
655 		break;
656 	default:
657 		return -ENOTCONN;
658 	}
659 
660 	return len;
661 }
662 
663 static int qedi_host_get_param(struct Scsi_Host *shost,
664 			       enum iscsi_host_param param, char *buf)
665 {
666 	struct qedi_ctx *qedi;
667 	int len;
668 
669 	qedi = iscsi_host_priv(shost);
670 
671 	switch (param) {
672 	case ISCSI_HOST_PARAM_HWADDRESS:
673 		len = sysfs_format_mac(buf, qedi->mac, 6);
674 		break;
675 	case ISCSI_HOST_PARAM_NETDEV_NAME:
676 		len = sprintf(buf, "host%d\n", shost->host_no);
677 		break;
678 	case ISCSI_HOST_PARAM_IPADDRESS:
679 		if (qedi->ip_type == TCP_IPV4)
680 			len = sprintf(buf, "%pI4\n", qedi->src_ip);
681 		else
682 			len = sprintf(buf, "%pI6\n", qedi->src_ip);
683 		break;
684 	default:
685 		return iscsi_host_get_param(shost, param, buf);
686 	}
687 
688 	return len;
689 }
690 
691 static void qedi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
692 				struct iscsi_stats *stats)
693 {
694 	struct iscsi_conn *conn = cls_conn->dd_data;
695 	struct qed_iscsi_stats iscsi_stats;
696 	struct Scsi_Host *shost;
697 	struct qedi_ctx *qedi;
698 
699 	shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
700 	qedi = iscsi_host_priv(shost);
701 	qedi_ops->get_stats(qedi->cdev, &iscsi_stats);
702 
703 	conn->txdata_octets = iscsi_stats.iscsi_tx_bytes_cnt;
704 	conn->rxdata_octets = iscsi_stats.iscsi_rx_bytes_cnt;
705 	conn->dataout_pdus_cnt = (uint32_t)iscsi_stats.iscsi_tx_data_pdu_cnt;
706 	conn->datain_pdus_cnt = (uint32_t)iscsi_stats.iscsi_rx_data_pdu_cnt;
707 	conn->r2t_pdus_cnt = (uint32_t)iscsi_stats.iscsi_rx_r2t_pdu_cnt;
708 
709 	stats->txdata_octets = conn->txdata_octets;
710 	stats->rxdata_octets = conn->rxdata_octets;
711 	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
712 	stats->dataout_pdus = conn->dataout_pdus_cnt;
713 	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
714 	stats->datain_pdus = conn->datain_pdus_cnt;
715 	stats->r2t_pdus = conn->r2t_pdus_cnt;
716 	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
717 	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
718 	stats->digest_err = 0;
719 	stats->timeout_err = 0;
720 	strcpy(stats->custom[0].desc, "eh_abort_cnt");
721 	stats->custom[0].value = conn->eh_abort_cnt;
722 	stats->custom_length = 1;
723 }
724 
725 static void qedi_iscsi_prep_generic_pdu_bd(struct qedi_conn *qedi_conn)
726 {
727 	struct scsi_sge *bd_tbl;
728 
729 	bd_tbl = (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
730 
731 	bd_tbl->sge_addr.hi =
732 		(u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
733 	bd_tbl->sge_addr.lo = (u32)qedi_conn->gen_pdu.req_dma_addr;
734 	bd_tbl->sge_len = qedi_conn->gen_pdu.req_wr_ptr -
735 				qedi_conn->gen_pdu.req_buf;
736 	bd_tbl = (struct scsi_sge  *)qedi_conn->gen_pdu.resp_bd_tbl;
737 	bd_tbl->sge_addr.hi =
738 			(u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
739 	bd_tbl->sge_addr.lo = (u32)qedi_conn->gen_pdu.resp_dma_addr;
740 	bd_tbl->sge_len = ISCSI_DEF_MAX_RECV_SEG_LEN;
741 }
742 
743 static int qedi_iscsi_send_generic_request(struct iscsi_task *task)
744 {
745 	struct qedi_cmd *cmd = task->dd_data;
746 	struct qedi_conn *qedi_conn = cmd->conn;
747 	char *buf;
748 	int data_len;
749 	int rc = 0;
750 
751 	qedi_iscsi_prep_generic_pdu_bd(qedi_conn);
752 	switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
753 	case ISCSI_OP_LOGIN:
754 		qedi_send_iscsi_login(qedi_conn, task);
755 		break;
756 	case ISCSI_OP_NOOP_OUT:
757 		data_len = qedi_conn->gen_pdu.req_buf_size;
758 		buf = qedi_conn->gen_pdu.req_buf;
759 		if (data_len)
760 			rc = qedi_send_iscsi_nopout(qedi_conn, task,
761 						    buf, data_len, 1);
762 		else
763 			rc = qedi_send_iscsi_nopout(qedi_conn, task,
764 						    NULL, 0, 1);
765 		break;
766 	case ISCSI_OP_LOGOUT:
767 		rc = qedi_send_iscsi_logout(qedi_conn, task);
768 		break;
769 	case ISCSI_OP_SCSI_TMFUNC:
770 		rc = qedi_send_iscsi_tmf(qedi_conn, task);
771 		break;
772 	case ISCSI_OP_TEXT:
773 		rc = qedi_send_iscsi_text(qedi_conn, task);
774 		break;
775 	default:
776 		iscsi_conn_printk(KERN_ALERT, qedi_conn->cls_conn->dd_data,
777 				  "unsupported op 0x%x\n", task->hdr->opcode);
778 	}
779 
780 	return rc;
781 }
782 
783 static int qedi_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
784 {
785 	struct qedi_conn *qedi_conn = conn->dd_data;
786 	struct qedi_cmd *cmd = task->dd_data;
787 
788 	memset(qedi_conn->gen_pdu.req_buf, 0, ISCSI_DEF_MAX_RECV_SEG_LEN);
789 
790 	qedi_conn->gen_pdu.req_buf_size = task->data_count;
791 
792 	if (task->data_count) {
793 		memcpy(qedi_conn->gen_pdu.req_buf, task->data,
794 		       task->data_count);
795 		qedi_conn->gen_pdu.req_wr_ptr =
796 			qedi_conn->gen_pdu.req_buf + task->data_count;
797 	}
798 
799 	cmd->conn = conn->dd_data;
800 	return qedi_iscsi_send_generic_request(task);
801 }
802 
803 static int qedi_task_xmit(struct iscsi_task *task)
804 {
805 	struct iscsi_conn *conn = task->conn;
806 	struct qedi_conn *qedi_conn = conn->dd_data;
807 	struct qedi_cmd *cmd = task->dd_data;
808 	struct scsi_cmnd *sc = task->sc;
809 
810 	/* Clear now so in cleanup_task we know it didn't make it */
811 	cmd->scsi_cmd = NULL;
812 	cmd->task_id = U16_MAX;
813 
814 	if (test_bit(QEDI_IN_SHUTDOWN, &qedi_conn->qedi->flags))
815 		return -ENODEV;
816 
817 	if (test_bit(QEDI_BLOCK_IO, &qedi_conn->qedi->flags))
818 		return -EACCES;
819 
820 	cmd->state = 0;
821 	cmd->task = NULL;
822 	cmd->use_slowpath = false;
823 	cmd->conn = qedi_conn;
824 	cmd->task = task;
825 	cmd->io_cmd_in_list = false;
826 	INIT_LIST_HEAD(&cmd->io_cmd);
827 
828 	if (!sc)
829 		return qedi_mtask_xmit(conn, task);
830 
831 	cmd->scsi_cmd = sc;
832 	return qedi_iscsi_send_ioreq(task);
833 }
834 
835 static struct iscsi_endpoint *
836 qedi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
837 		int non_blocking)
838 {
839 	struct qedi_ctx *qedi;
840 	struct iscsi_endpoint *ep;
841 	struct qedi_endpoint *qedi_ep;
842 	struct sockaddr_in *addr;
843 	struct sockaddr_in6 *addr6;
844 	struct iscsi_path path_req;
845 	u32 msg_type = ISCSI_KEVENT_IF_DOWN;
846 	u32 iscsi_cid = QEDI_CID_RESERVED;
847 	u16 len = 0;
848 	char *buf = NULL;
849 	int ret, tmp;
850 
851 	if (!shost) {
852 		ret = -ENXIO;
853 		QEDI_ERR(NULL, "shost is NULL\n");
854 		return ERR_PTR(ret);
855 	}
856 
857 	if (qedi_do_not_recover) {
858 		ret = -ENOMEM;
859 		return ERR_PTR(ret);
860 	}
861 
862 	qedi = iscsi_host_priv(shost);
863 
864 	if (test_bit(QEDI_IN_OFFLINE, &qedi->flags) ||
865 	    test_bit(QEDI_IN_RECOVERY, &qedi->flags)) {
866 		ret = -ENOMEM;
867 		return ERR_PTR(ret);
868 	}
869 
870 	if (atomic_read(&qedi->link_state) != QEDI_LINK_UP) {
871 		QEDI_WARN(&qedi->dbg_ctx, "qedi link down\n");
872 		return ERR_PTR(-ENXIO);
873 	}
874 
875 	ep = iscsi_create_endpoint(sizeof(struct qedi_endpoint));
876 	if (!ep) {
877 		QEDI_ERR(&qedi->dbg_ctx, "endpoint create fail\n");
878 		ret = -ENOMEM;
879 		return ERR_PTR(ret);
880 	}
881 	qedi_ep = ep->dd_data;
882 	memset(qedi_ep, 0, sizeof(struct qedi_endpoint));
883 	qedi_ep->state = EP_STATE_IDLE;
884 	qedi_ep->iscsi_cid = (u32)-1;
885 	qedi_ep->qedi = qedi;
886 
887 	if (dst_addr->sa_family == AF_INET) {
888 		addr = (struct sockaddr_in *)dst_addr;
889 		memcpy(qedi_ep->dst_addr, &addr->sin_addr.s_addr,
890 		       sizeof(struct in_addr));
891 		qedi_ep->dst_port = ntohs(addr->sin_port);
892 		qedi_ep->ip_type = TCP_IPV4;
893 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
894 			  "dst_addr=%pI4, dst_port=%u\n",
895 			  qedi_ep->dst_addr, qedi_ep->dst_port);
896 	} else if (dst_addr->sa_family == AF_INET6) {
897 		addr6 = (struct sockaddr_in6 *)dst_addr;
898 		memcpy(qedi_ep->dst_addr, &addr6->sin6_addr,
899 		       sizeof(struct in6_addr));
900 		qedi_ep->dst_port = ntohs(addr6->sin6_port);
901 		qedi_ep->ip_type = TCP_IPV6;
902 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
903 			  "dst_addr=%pI6, dst_port=%u\n",
904 			  qedi_ep->dst_addr, qedi_ep->dst_port);
905 	} else {
906 		QEDI_ERR(&qedi->dbg_ctx, "Invalid endpoint\n");
907 	}
908 
909 	ret = qedi_alloc_sq(qedi, qedi_ep);
910 	if (ret)
911 		goto ep_conn_exit;
912 
913 	ret = qedi_ops->acquire_conn(qedi->cdev, &qedi_ep->handle,
914 				     &qedi_ep->fw_cid, &qedi_ep->p_doorbell);
915 
916 	if (ret) {
917 		QEDI_ERR(&qedi->dbg_ctx, "Could not acquire connection\n");
918 		ret = -ENXIO;
919 		goto ep_free_sq;
920 	}
921 
922 	iscsi_cid = qedi_ep->handle;
923 	qedi_ep->iscsi_cid = iscsi_cid;
924 
925 	init_waitqueue_head(&qedi_ep->ofld_wait);
926 	init_waitqueue_head(&qedi_ep->tcp_ofld_wait);
927 	qedi_ep->state = EP_STATE_OFLDCONN_START;
928 	qedi->ep_tbl[iscsi_cid] = qedi_ep;
929 
930 	buf = (char *)&path_req;
931 	len = sizeof(path_req);
932 	memset(&path_req, 0, len);
933 
934 	msg_type = ISCSI_KEVENT_PATH_REQ;
935 	path_req.handle = (u64)qedi_ep->iscsi_cid;
936 	path_req.pmtu = qedi->ll2_mtu;
937 	qedi_ep->pmtu = qedi->ll2_mtu;
938 	if (qedi_ep->ip_type == TCP_IPV4) {
939 		memcpy(&path_req.dst.v4_addr, &qedi_ep->dst_addr,
940 		       sizeof(struct in_addr));
941 		path_req.ip_addr_len = 4;
942 	} else {
943 		memcpy(&path_req.dst.v6_addr, &qedi_ep->dst_addr,
944 		       sizeof(struct in6_addr));
945 		path_req.ip_addr_len = 16;
946 	}
947 
948 	ret = iscsi_offload_mesg(shost, &qedi_iscsi_transport, msg_type, buf,
949 				 len);
950 	if (ret) {
951 		QEDI_ERR(&qedi->dbg_ctx,
952 			 "iscsi_offload_mesg() failed for cid=0x%x ret=%d\n",
953 			 iscsi_cid, ret);
954 		goto ep_rel_conn;
955 	}
956 
957 	atomic_inc(&qedi->num_offloads);
958 	return ep;
959 
960 ep_rel_conn:
961 	qedi->ep_tbl[iscsi_cid] = NULL;
962 	tmp = qedi_ops->release_conn(qedi->cdev, qedi_ep->handle);
963 	if (tmp)
964 		QEDI_WARN(&qedi->dbg_ctx, "release_conn returned %d\n",
965 			  tmp);
966 ep_free_sq:
967 	qedi_free_sq(qedi, qedi_ep);
968 ep_conn_exit:
969 	iscsi_destroy_endpoint(ep);
970 	return ERR_PTR(ret);
971 }
972 
973 static int qedi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
974 {
975 	struct qedi_endpoint *qedi_ep;
976 	int ret = 0;
977 
978 	if (qedi_do_not_recover)
979 		return 1;
980 
981 	qedi_ep = ep->dd_data;
982 	if (qedi_ep->state == EP_STATE_IDLE ||
983 	    qedi_ep->state == EP_STATE_OFLDCONN_NONE ||
984 	    qedi_ep->state == EP_STATE_OFLDCONN_FAILED)
985 		return -1;
986 
987 	if (qedi_ep->state == EP_STATE_OFLDCONN_COMPL)
988 		ret = 1;
989 
990 	ret = wait_event_interruptible_timeout(qedi_ep->ofld_wait,
991 					       QEDI_OFLD_WAIT_STATE(qedi_ep),
992 					       msecs_to_jiffies(timeout_ms));
993 
994 	if (qedi_ep->state == EP_STATE_OFLDCONN_FAILED)
995 		ret = -1;
996 
997 	if (ret > 0)
998 		return 1;
999 	else if (!ret)
1000 		return 0;
1001 	else
1002 		return ret;
1003 }
1004 
1005 static void qedi_cleanup_active_cmd_list(struct qedi_conn *qedi_conn)
1006 {
1007 	struct qedi_cmd *cmd, *cmd_tmp;
1008 
1009 	spin_lock(&qedi_conn->list_lock);
1010 	list_for_each_entry_safe(cmd, cmd_tmp, &qedi_conn->active_cmd_list,
1011 				 io_cmd) {
1012 		list_del_init(&cmd->io_cmd);
1013 		qedi_conn->active_cmd_count--;
1014 	}
1015 	spin_unlock(&qedi_conn->list_lock);
1016 }
1017 
1018 static void qedi_ep_disconnect(struct iscsi_endpoint *ep)
1019 {
1020 	struct qedi_endpoint *qedi_ep;
1021 	struct qedi_conn *qedi_conn = NULL;
1022 	struct qedi_ctx *qedi;
1023 	int ret = 0;
1024 	int wait_delay;
1025 	int abrt_conn = 0;
1026 
1027 	wait_delay = 60 * HZ + DEF_MAX_RT_TIME;
1028 	qedi_ep = ep->dd_data;
1029 	qedi = qedi_ep->qedi;
1030 
1031 	if (qedi_ep->state == EP_STATE_OFLDCONN_START)
1032 		goto ep_exit_recover;
1033 
1034 	if (qedi_ep->state != EP_STATE_OFLDCONN_NONE)
1035 		flush_work(&qedi_ep->offload_work);
1036 
1037 	if (qedi_ep->conn) {
1038 		qedi_conn = qedi_ep->conn;
1039 		abrt_conn = qedi_conn->abrt_conn;
1040 
1041 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1042 			  "cid=0x%x qedi_ep=%p waiting for %d tmfs\n",
1043 			  qedi_ep->iscsi_cid, qedi_ep,
1044 			  qedi_conn->fw_cleanup_works);
1045 
1046 		spin_lock(&qedi_conn->tmf_work_lock);
1047 		qedi_conn->ep_disconnect_starting = true;
1048 		while (qedi_conn->fw_cleanup_works > 0) {
1049 			spin_unlock(&qedi_conn->tmf_work_lock);
1050 			msleep(1000);
1051 			spin_lock(&qedi_conn->tmf_work_lock);
1052 		}
1053 		spin_unlock(&qedi_conn->tmf_work_lock);
1054 
1055 		if (test_bit(QEDI_IN_RECOVERY, &qedi->flags)) {
1056 			if (qedi_do_not_recover) {
1057 				QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1058 					  "Do not recover cid=0x%x\n",
1059 					  qedi_ep->iscsi_cid);
1060 				goto ep_exit_recover;
1061 			}
1062 			QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1063 				  "Reset recovery cid=0x%x, qedi_ep=%p, state=0x%x\n",
1064 				  qedi_ep->iscsi_cid, qedi_ep, qedi_ep->state);
1065 			qedi_cleanup_active_cmd_list(qedi_conn);
1066 			goto ep_release_conn;
1067 		}
1068 	}
1069 
1070 	if (qedi_do_not_recover)
1071 		goto ep_exit_recover;
1072 
1073 	switch (qedi_ep->state) {
1074 	case EP_STATE_OFLDCONN_START:
1075 	case EP_STATE_OFLDCONN_NONE:
1076 		goto ep_release_conn;
1077 	case EP_STATE_OFLDCONN_FAILED:
1078 			break;
1079 	case EP_STATE_OFLDCONN_COMPL:
1080 		if (unlikely(!qedi_conn))
1081 			break;
1082 
1083 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1084 			  "Active cmd count=%d, abrt_conn=%d, ep state=0x%x, cid=0x%x, qedi_conn=%p\n",
1085 			  qedi_conn->active_cmd_count, abrt_conn,
1086 			  qedi_ep->state,
1087 			  qedi_ep->iscsi_cid,
1088 			  qedi_ep->conn
1089 			  );
1090 
1091 		if (!qedi_conn->active_cmd_count)
1092 			abrt_conn = 0;
1093 		else
1094 			abrt_conn = 1;
1095 
1096 		if (abrt_conn)
1097 			qedi_clearsq(qedi, qedi_conn, NULL);
1098 		break;
1099 	default:
1100 		break;
1101 	}
1102 
1103 	if (!abrt_conn)
1104 		wait_delay += qedi->pf_params.iscsi_pf_params.two_msl_timer;
1105 
1106 	qedi_ep->state = EP_STATE_DISCONN_START;
1107 
1108 	if (test_bit(QEDI_IN_SHUTDOWN, &qedi->flags) ||
1109 	    test_bit(QEDI_IN_RECOVERY, &qedi->flags))
1110 		goto ep_release_conn;
1111 
1112 	ret = qedi_ops->destroy_conn(qedi->cdev, qedi_ep->handle, abrt_conn);
1113 	if (ret) {
1114 		QEDI_WARN(&qedi->dbg_ctx,
1115 			  "destroy_conn failed returned %d\n", ret);
1116 	} else {
1117 		ret = wait_event_interruptible_timeout(
1118 					qedi_ep->tcp_ofld_wait,
1119 					(qedi_ep->state !=
1120 					 EP_STATE_DISCONN_START),
1121 					wait_delay);
1122 		if ((ret <= 0) || (qedi_ep->state == EP_STATE_DISCONN_START)) {
1123 			QEDI_WARN(&qedi->dbg_ctx,
1124 				  "Destroy conn timedout or interrupted, ret=%d, delay=%d, cid=0x%x\n",
1125 				  ret, wait_delay, qedi_ep->iscsi_cid);
1126 		}
1127 	}
1128 
1129 ep_release_conn:
1130 	ret = qedi_ops->release_conn(qedi->cdev, qedi_ep->handle);
1131 	if (ret)
1132 		QEDI_WARN(&qedi->dbg_ctx,
1133 			  "release_conn returned %d, cid=0x%x\n",
1134 			  ret, qedi_ep->iscsi_cid);
1135 ep_exit_recover:
1136 	qedi_ep->state = EP_STATE_IDLE;
1137 	qedi->ep_tbl[qedi_ep->iscsi_cid] = NULL;
1138 	qedi->cid_que.conn_cid_tbl[qedi_ep->iscsi_cid] = NULL;
1139 	qedi_free_id(&qedi->lcl_port_tbl, qedi_ep->src_port);
1140 	qedi_free_sq(qedi, qedi_ep);
1141 
1142 	if (qedi_conn)
1143 		qedi_conn->ep = NULL;
1144 
1145 	qedi_ep->conn = NULL;
1146 	qedi_ep->qedi = NULL;
1147 	atomic_dec(&qedi->num_offloads);
1148 
1149 	iscsi_destroy_endpoint(ep);
1150 }
1151 
1152 static int qedi_data_avail(struct qedi_ctx *qedi, u16 vlanid)
1153 {
1154 	struct qed_dev *cdev = qedi->cdev;
1155 	struct qedi_uio_dev *udev;
1156 	struct qedi_uio_ctrl *uctrl;
1157 	struct sk_buff *skb;
1158 	u32 len;
1159 	int rc = 0;
1160 
1161 	udev = qedi->udev;
1162 	if (!udev) {
1163 		QEDI_ERR(&qedi->dbg_ctx, "udev is NULL.\n");
1164 		return -EINVAL;
1165 	}
1166 
1167 	uctrl = (struct qedi_uio_ctrl *)udev->uctrl;
1168 	if (!uctrl) {
1169 		QEDI_ERR(&qedi->dbg_ctx, "uctlr is NULL.\n");
1170 		return -EINVAL;
1171 	}
1172 
1173 	len = uctrl->host_tx_pkt_len;
1174 	if (!len) {
1175 		QEDI_ERR(&qedi->dbg_ctx, "Invalid len %u\n", len);
1176 		return -EINVAL;
1177 	}
1178 
1179 	skb = alloc_skb(len, GFP_ATOMIC);
1180 	if (!skb) {
1181 		QEDI_ERR(&qedi->dbg_ctx, "alloc_skb failed\n");
1182 		return -EINVAL;
1183 	}
1184 
1185 	skb_put(skb, len);
1186 	memcpy(skb->data, udev->tx_pkt, len);
1187 	skb->ip_summed = CHECKSUM_NONE;
1188 
1189 	if (vlanid)
1190 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
1191 
1192 	rc = qedi_ops->ll2->start_xmit(cdev, skb, 0);
1193 	if (rc) {
1194 		QEDI_ERR(&qedi->dbg_ctx, "ll2 start_xmit returned %d\n",
1195 			 rc);
1196 		kfree_skb(skb);
1197 	}
1198 
1199 	uctrl->host_tx_pkt_len = 0;
1200 	uctrl->hw_tx_cons++;
1201 
1202 	return rc;
1203 }
1204 
1205 static void qedi_offload_work(struct work_struct *work)
1206 {
1207 	struct qedi_endpoint *qedi_ep =
1208 		container_of(work, struct qedi_endpoint, offload_work);
1209 	struct qedi_ctx *qedi;
1210 	int wait_delay = 5 * HZ;
1211 	int ret;
1212 
1213 	qedi = qedi_ep->qedi;
1214 
1215 	ret = qedi_iscsi_offload_conn(qedi_ep);
1216 	if (ret) {
1217 		QEDI_ERR(&qedi->dbg_ctx,
1218 			 "offload error: iscsi_cid=%u, qedi_ep=%p, ret=%d\n",
1219 			 qedi_ep->iscsi_cid, qedi_ep, ret);
1220 		qedi_ep->state = EP_STATE_OFLDCONN_FAILED;
1221 		return;
1222 	}
1223 
1224 	ret = wait_event_interruptible_timeout(qedi_ep->tcp_ofld_wait,
1225 					       (qedi_ep->state ==
1226 					       EP_STATE_OFLDCONN_COMPL),
1227 					       wait_delay);
1228 	if ((ret <= 0) || (qedi_ep->state != EP_STATE_OFLDCONN_COMPL)) {
1229 		qedi_ep->state = EP_STATE_OFLDCONN_FAILED;
1230 		QEDI_ERR(&qedi->dbg_ctx,
1231 			 "Offload conn TIMEOUT iscsi_cid=%u, qedi_ep=%p\n",
1232 			 qedi_ep->iscsi_cid, qedi_ep);
1233 	}
1234 }
1235 
1236 static int qedi_set_path(struct Scsi_Host *shost, struct iscsi_path *path_data)
1237 {
1238 	struct qedi_ctx *qedi;
1239 	struct qedi_endpoint *qedi_ep;
1240 	int ret = 0;
1241 	u32 iscsi_cid;
1242 	u16 port_id = 0;
1243 
1244 	if (!shost) {
1245 		ret = -ENXIO;
1246 		QEDI_ERR(NULL, "shost is NULL\n");
1247 		return ret;
1248 	}
1249 
1250 	if (strcmp(shost->hostt->proc_name, "qedi")) {
1251 		ret = -ENXIO;
1252 		QEDI_ERR(NULL, "shost %s is invalid\n",
1253 			 shost->hostt->proc_name);
1254 		return ret;
1255 	}
1256 
1257 	qedi = iscsi_host_priv(shost);
1258 	if (path_data->handle == QEDI_PATH_HANDLE) {
1259 		ret = qedi_data_avail(qedi, path_data->vlan_id);
1260 		goto set_path_exit;
1261 	}
1262 
1263 	iscsi_cid = (u32)path_data->handle;
1264 	if (iscsi_cid >= qedi->max_active_conns) {
1265 		ret = -EINVAL;
1266 		goto set_path_exit;
1267 	}
1268 	qedi_ep = qedi->ep_tbl[iscsi_cid];
1269 	QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1270 		  "iscsi_cid=0x%x, qedi_ep=%p\n", iscsi_cid, qedi_ep);
1271 	if (!qedi_ep) {
1272 		ret = -EINVAL;
1273 		goto set_path_exit;
1274 	}
1275 
1276 	if (!is_valid_ether_addr(&path_data->mac_addr[0])) {
1277 		QEDI_NOTICE(&qedi->dbg_ctx, "dst mac NOT VALID\n");
1278 		qedi_ep->state = EP_STATE_OFLDCONN_NONE;
1279 		ret = -EIO;
1280 		goto set_path_exit;
1281 	}
1282 
1283 	ether_addr_copy(&qedi_ep->src_mac[0], &qedi->mac[0]);
1284 	ether_addr_copy(&qedi_ep->dst_mac[0], &path_data->mac_addr[0]);
1285 
1286 	qedi_ep->vlan_id = path_data->vlan_id;
1287 	if (path_data->pmtu < DEF_PATH_MTU) {
1288 		qedi_ep->pmtu = qedi->ll2_mtu;
1289 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1290 			  "MTU cannot be %u, using default MTU %u\n",
1291 			   path_data->pmtu, qedi_ep->pmtu);
1292 	}
1293 
1294 	if (path_data->pmtu != qedi->ll2_mtu) {
1295 		if (path_data->pmtu > JUMBO_MTU) {
1296 			ret = -EINVAL;
1297 			QEDI_ERR(NULL, "Invalid MTU %u\n", path_data->pmtu);
1298 			goto set_path_exit;
1299 		}
1300 
1301 		qedi_reset_host_mtu(qedi, path_data->pmtu);
1302 		qedi_ep->pmtu = qedi->ll2_mtu;
1303 	}
1304 
1305 	port_id = qedi_ep->src_port;
1306 	if (port_id >= QEDI_LOCAL_PORT_MIN &&
1307 	    port_id < QEDI_LOCAL_PORT_MAX) {
1308 		if (qedi_alloc_id(&qedi->lcl_port_tbl, port_id))
1309 			port_id = 0;
1310 	} else {
1311 		port_id = 0;
1312 	}
1313 
1314 	if (!port_id) {
1315 		port_id = qedi_alloc_new_id(&qedi->lcl_port_tbl);
1316 		if (port_id == QEDI_LOCAL_PORT_INVALID) {
1317 			QEDI_ERR(&qedi->dbg_ctx,
1318 				 "Failed to allocate port id for iscsi_cid=0x%x\n",
1319 				 iscsi_cid);
1320 			ret = -ENOMEM;
1321 			goto set_path_exit;
1322 		}
1323 	}
1324 
1325 	qedi_ep->src_port = port_id;
1326 
1327 	if (qedi_ep->ip_type == TCP_IPV4) {
1328 		memcpy(&qedi_ep->src_addr[0], &path_data->src.v4_addr,
1329 		       sizeof(struct in_addr));
1330 		memcpy(&qedi->src_ip[0], &path_data->src.v4_addr,
1331 		       sizeof(struct in_addr));
1332 		qedi->ip_type = TCP_IPV4;
1333 
1334 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1335 			  "src addr:port=%pI4:%u, dst addr:port=%pI4:%u\n",
1336 			  qedi_ep->src_addr, qedi_ep->src_port,
1337 			  qedi_ep->dst_addr, qedi_ep->dst_port);
1338 	} else {
1339 		memcpy(&qedi_ep->src_addr[0], &path_data->src.v6_addr,
1340 		       sizeof(struct in6_addr));
1341 		memcpy(&qedi->src_ip[0], &path_data->src.v6_addr,
1342 		       sizeof(struct in6_addr));
1343 		qedi->ip_type = TCP_IPV6;
1344 
1345 		QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1346 			  "src addr:port=%pI6:%u, dst addr:port=%pI6:%u\n",
1347 			  qedi_ep->src_addr, qedi_ep->src_port,
1348 			  qedi_ep->dst_addr, qedi_ep->dst_port);
1349 	}
1350 
1351 	INIT_WORK(&qedi_ep->offload_work, qedi_offload_work);
1352 	queue_work(qedi->offload_thread, &qedi_ep->offload_work);
1353 
1354 	ret = 0;
1355 
1356 set_path_exit:
1357 	return ret;
1358 }
1359 
1360 static umode_t qedi_attr_is_visible(int param_type, int param)
1361 {
1362 	switch (param_type) {
1363 	case ISCSI_HOST_PARAM:
1364 		switch (param) {
1365 		case ISCSI_HOST_PARAM_NETDEV_NAME:
1366 		case ISCSI_HOST_PARAM_HWADDRESS:
1367 		case ISCSI_HOST_PARAM_IPADDRESS:
1368 			return 0444;
1369 		default:
1370 			return 0;
1371 		}
1372 	case ISCSI_PARAM:
1373 		switch (param) {
1374 		case ISCSI_PARAM_MAX_RECV_DLENGTH:
1375 		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1376 		case ISCSI_PARAM_HDRDGST_EN:
1377 		case ISCSI_PARAM_DATADGST_EN:
1378 		case ISCSI_PARAM_CONN_ADDRESS:
1379 		case ISCSI_PARAM_CONN_PORT:
1380 		case ISCSI_PARAM_EXP_STATSN:
1381 		case ISCSI_PARAM_PERSISTENT_ADDRESS:
1382 		case ISCSI_PARAM_PERSISTENT_PORT:
1383 		case ISCSI_PARAM_PING_TMO:
1384 		case ISCSI_PARAM_RECV_TMO:
1385 		case ISCSI_PARAM_INITIAL_R2T_EN:
1386 		case ISCSI_PARAM_MAX_R2T:
1387 		case ISCSI_PARAM_IMM_DATA_EN:
1388 		case ISCSI_PARAM_FIRST_BURST:
1389 		case ISCSI_PARAM_MAX_BURST:
1390 		case ISCSI_PARAM_PDU_INORDER_EN:
1391 		case ISCSI_PARAM_DATASEQ_INORDER_EN:
1392 		case ISCSI_PARAM_ERL:
1393 		case ISCSI_PARAM_TARGET_NAME:
1394 		case ISCSI_PARAM_TPGT:
1395 		case ISCSI_PARAM_USERNAME:
1396 		case ISCSI_PARAM_PASSWORD:
1397 		case ISCSI_PARAM_USERNAME_IN:
1398 		case ISCSI_PARAM_PASSWORD_IN:
1399 		case ISCSI_PARAM_FAST_ABORT:
1400 		case ISCSI_PARAM_ABORT_TMO:
1401 		case ISCSI_PARAM_LU_RESET_TMO:
1402 		case ISCSI_PARAM_TGT_RESET_TMO:
1403 		case ISCSI_PARAM_IFACE_NAME:
1404 		case ISCSI_PARAM_INITIATOR_NAME:
1405 		case ISCSI_PARAM_BOOT_ROOT:
1406 		case ISCSI_PARAM_BOOT_NIC:
1407 		case ISCSI_PARAM_BOOT_TARGET:
1408 			return 0444;
1409 		default:
1410 			return 0;
1411 		}
1412 	}
1413 
1414 	return 0;
1415 }
1416 
1417 static void qedi_cleanup_task(struct iscsi_task *task)
1418 {
1419 	struct qedi_cmd *cmd;
1420 
1421 	if (task->state == ISCSI_TASK_PENDING) {
1422 		QEDI_INFO(NULL, QEDI_LOG_IO, "Returning ref_cnt=%d\n",
1423 			  refcount_read(&task->refcount));
1424 		return;
1425 	}
1426 
1427 	if (task->sc)
1428 		qedi_iscsi_unmap_sg_list(task->dd_data);
1429 
1430 	cmd = task->dd_data;
1431 	if (cmd->task_id != U16_MAX)
1432 		qedi_clear_task_idx(iscsi_host_priv(task->conn->session->host),
1433 				    cmd->task_id);
1434 
1435 	cmd->task_id = U16_MAX;
1436 	cmd->scsi_cmd = NULL;
1437 }
1438 
1439 struct iscsi_transport qedi_iscsi_transport = {
1440 	.owner = THIS_MODULE,
1441 	.name = QEDI_MODULE_NAME,
1442 	.caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_MULTI_R2T | CAP_DATADGST |
1443 		CAP_DATA_PATH_OFFLOAD | CAP_TEXT_NEGO,
1444 	.create_session = qedi_session_create,
1445 	.destroy_session = qedi_session_destroy,
1446 	.create_conn = qedi_conn_create,
1447 	.bind_conn = qedi_conn_bind,
1448 	.unbind_conn = iscsi_conn_unbind,
1449 	.start_conn = qedi_conn_start,
1450 	.stop_conn = iscsi_conn_stop,
1451 	.destroy_conn = qedi_conn_destroy,
1452 	.set_param = iscsi_set_param,
1453 	.get_ep_param = qedi_ep_get_param,
1454 	.get_conn_param = iscsi_conn_get_param,
1455 	.get_session_param = iscsi_session_get_param,
1456 	.get_host_param = qedi_host_get_param,
1457 	.send_pdu = iscsi_conn_send_pdu,
1458 	.get_stats = qedi_conn_get_stats,
1459 	.xmit_task = qedi_task_xmit,
1460 	.cleanup_task = qedi_cleanup_task,
1461 	.session_recovery_timedout = iscsi_session_recovery_timedout,
1462 	.ep_connect = qedi_ep_connect,
1463 	.ep_poll = qedi_ep_poll,
1464 	.ep_disconnect = qedi_ep_disconnect,
1465 	.set_path = qedi_set_path,
1466 	.attr_is_visible = qedi_attr_is_visible,
1467 };
1468 
1469 void qedi_start_conn_recovery(struct qedi_ctx *qedi,
1470 			      struct qedi_conn *qedi_conn)
1471 {
1472 	struct iscsi_cls_session *cls_sess;
1473 	struct iscsi_cls_conn *cls_conn;
1474 	struct iscsi_conn *conn;
1475 
1476 	cls_conn = qedi_conn->cls_conn;
1477 	conn = cls_conn->dd_data;
1478 	cls_sess = iscsi_conn_to_session(cls_conn);
1479 
1480 	if (iscsi_is_session_online(cls_sess)) {
1481 		qedi_conn->abrt_conn = 1;
1482 		QEDI_ERR(&qedi->dbg_ctx,
1483 			 "Failing connection, state=0x%x, cid=0x%x\n",
1484 			 conn->session->state, qedi_conn->iscsi_conn_id);
1485 		iscsi_conn_failure(qedi_conn->cls_conn->dd_data,
1486 				   ISCSI_ERR_CONN_FAILED);
1487 	}
1488 }
1489 
1490 static const struct {
1491 	enum iscsi_error_types error_code;
1492 	char *err_string;
1493 } qedi_iscsi_error[] = {
1494 	{ ISCSI_STATUS_NONE,
1495 	  "tcp_error none"
1496 	},
1497 	{ ISCSI_CONN_ERROR_TASK_CID_MISMATCH,
1498 	  "task cid mismatch"
1499 	},
1500 	{ ISCSI_CONN_ERROR_TASK_NOT_VALID,
1501 	  "invalid task"
1502 	},
1503 	{ ISCSI_CONN_ERROR_RQ_RING_IS_FULL,
1504 	  "rq ring full"
1505 	},
1506 	{ ISCSI_CONN_ERROR_CMDQ_RING_IS_FULL,
1507 	  "cmdq ring full"
1508 	},
1509 	{ ISCSI_CONN_ERROR_HQE_CACHING_FAILED,
1510 	  "sge caching failed"
1511 	},
1512 	{ ISCSI_CONN_ERROR_HEADER_DIGEST_ERROR,
1513 	  "hdr digest error"
1514 	},
1515 	{ ISCSI_CONN_ERROR_LOCAL_COMPLETION_ERROR,
1516 	  "local cmpl error"
1517 	},
1518 	{ ISCSI_CONN_ERROR_DATA_OVERRUN,
1519 	  "invalid task"
1520 	},
1521 	{ ISCSI_CONN_ERROR_OUT_OF_SGES_ERROR,
1522 	  "out of sge error"
1523 	},
1524 	{ ISCSI_CONN_ERROR_TCP_IP_FRAGMENT_ERROR,
1525 	  "tcp ip fragment error"
1526 	},
1527 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_AHS_LEN,
1528 	  "AHS len protocol error"
1529 	},
1530 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_ITT_OUT_OF_RANGE,
1531 	  "itt out of range error"
1532 	},
1533 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_SEG_LEN_EXCEEDS_PDU_SIZE,
1534 	  "data seg more than pdu size"
1535 	},
1536 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_INVALID_OPCODE,
1537 	  "invalid opcode"
1538 	},
1539 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_INVALID_OPCODE_BEFORE_UPDATE,
1540 	  "invalid opcode before update"
1541 	},
1542 	{ ISCSI_CONN_ERROR_UNVALID_NOPIN_DSL,
1543 	  "unexpected opcode"
1544 	},
1545 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_CARRIES_NO_DATA,
1546 	  "r2t carries no data"
1547 	},
1548 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_SN,
1549 	  "data sn error"
1550 	},
1551 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_IN_TTT,
1552 	  "data TTT error"
1553 	},
1554 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_TTT,
1555 	  "r2t TTT error"
1556 	},
1557 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_BUFFER_OFFSET,
1558 	  "buffer offset error"
1559 	},
1560 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_BUFFER_OFFSET_OOO,
1561 	  "buffer offset ooo"
1562 	},
1563 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_SN,
1564 	  "data seg len 0"
1565 	},
1566 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_0,
1567 	  "data xer len error"
1568 	},
1569 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_1,
1570 	  "data xer len1 error"
1571 	},
1572 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_2,
1573 	  "data xer len2 error"
1574 	},
1575 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_LUN,
1576 	  "protocol lun error"
1577 	},
1578 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_F_BIT_ZERO,
1579 	  "f bit zero error"
1580 	},
1581 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_EXP_STAT_SN,
1582 	  "exp stat sn error"
1583 	},
1584 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_DSL_NOT_ZERO,
1585 	  "dsl not zero error"
1586 	},
1587 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_INVALID_DSL,
1588 	  "invalid dsl"
1589 	},
1590 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_SEG_LEN_TOO_BIG,
1591 	  "data seg len too big"
1592 	},
1593 	{ ISCSI_CONN_ERROR_PROTOCOL_ERR_OUTSTANDING_R2T_COUNT,
1594 	  "outstanding r2t count error"
1595 	},
1596 	{ ISCSI_CONN_ERROR_SENSE_DATA_LENGTH,
1597 	  "sense datalen error"
1598 	},
1599 };
1600 
1601 static char *qedi_get_iscsi_error(enum iscsi_error_types err_code)
1602 {
1603 	int i;
1604 	char *msg = NULL;
1605 
1606 	for (i = 0; i < ARRAY_SIZE(qedi_iscsi_error); i++) {
1607 		if (qedi_iscsi_error[i].error_code == err_code) {
1608 			msg = qedi_iscsi_error[i].err_string;
1609 			break;
1610 		}
1611 	}
1612 	return msg;
1613 }
1614 
1615 void qedi_process_iscsi_error(struct qedi_endpoint *ep,
1616 			      struct iscsi_eqe_data *data)
1617 {
1618 	struct qedi_conn *qedi_conn;
1619 	struct qedi_ctx *qedi;
1620 	char warn_notice[] = "iscsi_warning";
1621 	char error_notice[] = "iscsi_error";
1622 	char unknown_msg[] = "Unknown error";
1623 	char *message;
1624 	int need_recovery = 0;
1625 	u32 err_mask = 0;
1626 	char *msg;
1627 
1628 	if (!ep)
1629 		return;
1630 
1631 	qedi_conn = ep->conn;
1632 	if (!qedi_conn)
1633 		return;
1634 
1635 	qedi = ep->qedi;
1636 
1637 	QEDI_ERR(&qedi->dbg_ctx, "async event iscsi error:0x%x\n",
1638 		 data->error_code);
1639 
1640 	if (err_mask) {
1641 		need_recovery = 0;
1642 		message = warn_notice;
1643 	} else {
1644 		need_recovery = 1;
1645 		message = error_notice;
1646 	}
1647 
1648 	msg = qedi_get_iscsi_error(data->error_code);
1649 	if (!msg) {
1650 		need_recovery = 0;
1651 		msg = unknown_msg;
1652 	}
1653 
1654 	iscsi_conn_printk(KERN_ALERT,
1655 			  qedi_conn->cls_conn->dd_data,
1656 			  "qedi: %s - %s\n", message, msg);
1657 
1658 	if (need_recovery)
1659 		qedi_start_conn_recovery(qedi_conn->qedi, qedi_conn);
1660 }
1661 
1662 void qedi_clear_session_ctx(struct iscsi_cls_session *cls_sess)
1663 {
1664 	struct iscsi_session *session = cls_sess->dd_data;
1665 	struct iscsi_conn *conn = session->leadconn;
1666 	struct qedi_conn *qedi_conn = conn->dd_data;
1667 
1668 	if (iscsi_is_session_online(cls_sess)) {
1669 		if (conn)
1670 			iscsi_suspend_queue(conn);
1671 		qedi_ep_disconnect(qedi_conn->iscsi_ep);
1672 	}
1673 
1674 	qedi_conn_destroy(qedi_conn->cls_conn);
1675 
1676 	qedi_session_destroy(cls_sess);
1677 }
1678 
1679 void qedi_process_tcp_error(struct qedi_endpoint *ep,
1680 			    struct iscsi_eqe_data *data)
1681 {
1682 	struct qedi_conn *qedi_conn;
1683 
1684 	if (!ep)
1685 		return;
1686 
1687 	qedi_conn = ep->conn;
1688 	if (!qedi_conn)
1689 		return;
1690 
1691 	QEDI_ERR(&ep->qedi->dbg_ctx, "async event TCP error:0x%x\n",
1692 		 data->error_code);
1693 
1694 	qedi_start_conn_recovery(qedi_conn->qedi, qedi_conn);
1695 }
1696