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