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