xref: /openbmc/linux/drivers/scsi/be2iscsi/be_iscsi.c (revision 2c64e9cb)
1 /*
2  * This file is part of the Emulex Linux Device Driver for Enterprise iSCSI
3  * Host Bus Adapters. Refer to the README file included with this package
4  * for driver version and adapter compatibility.
5  *
6  * Copyright (c) 2018 Broadcom. All Rights Reserved.
7  * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of version 2 of the GNU General Public License as published
11  * by the Free Software Foundation.
12  *
13  * Contact Information:
14  * linux-drivers@broadcom.com
15  *
16  */
17 
18 #include <scsi/libiscsi.h>
19 #include <scsi/scsi_transport_iscsi.h>
20 #include <scsi/scsi_transport.h>
21 #include <scsi/scsi_cmnd.h>
22 #include <scsi/scsi_device.h>
23 #include <scsi/scsi_host.h>
24 #include <scsi/scsi_netlink.h>
25 #include <net/netlink.h>
26 #include <scsi/scsi.h>
27 
28 #include "be_iscsi.h"
29 
30 extern struct iscsi_transport beiscsi_iscsi_transport;
31 
32 /**
33  * beiscsi_session_create - creates a new iscsi session
34  * @cmds_max: max commands supported
35  * @qdepth: max queue depth supported
36  * @initial_cmdsn: initial iscsi CMDSN
37  */
38 struct iscsi_cls_session *beiscsi_session_create(struct iscsi_endpoint *ep,
39 						 u16 cmds_max,
40 						 u16 qdepth,
41 						 u32 initial_cmdsn)
42 {
43 	struct Scsi_Host *shost;
44 	struct beiscsi_endpoint *beiscsi_ep;
45 	struct iscsi_cls_session *cls_session;
46 	struct beiscsi_hba *phba;
47 	struct iscsi_session *sess;
48 	struct beiscsi_session *beiscsi_sess;
49 	struct beiscsi_io_task *io_task;
50 
51 
52 	if (!ep) {
53 		pr_err("beiscsi_session_create: invalid ep\n");
54 		return NULL;
55 	}
56 	beiscsi_ep = ep->dd_data;
57 	phba = beiscsi_ep->phba;
58 
59 	if (!beiscsi_hba_is_online(phba)) {
60 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
61 			    "BS_%d : HBA in error 0x%lx\n", phba->state);
62 		return NULL;
63 	}
64 
65 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
66 		    "BS_%d : In beiscsi_session_create\n");
67 	if (cmds_max > beiscsi_ep->phba->params.wrbs_per_cxn) {
68 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
69 			    "BS_%d : Cannot handle %d cmds."
70 			    "Max cmds per session supported is %d. Using %d."
71 			    "\n", cmds_max,
72 			    beiscsi_ep->phba->params.wrbs_per_cxn,
73 			    beiscsi_ep->phba->params.wrbs_per_cxn);
74 
75 		cmds_max = beiscsi_ep->phba->params.wrbs_per_cxn;
76 	}
77 
78 	shost = phba->shost;
79 	cls_session = iscsi_session_setup(&beiscsi_iscsi_transport,
80 					  shost, cmds_max,
81 					  sizeof(*beiscsi_sess),
82 					  sizeof(*io_task),
83 					  initial_cmdsn, ISCSI_MAX_TARGET);
84 	if (!cls_session)
85 		return NULL;
86 	sess = cls_session->dd_data;
87 	beiscsi_sess = sess->dd_data;
88 	beiscsi_sess->bhs_pool =  dma_pool_create("beiscsi_bhs_pool",
89 						   &phba->pcidev->dev,
90 						   sizeof(struct be_cmd_bhs),
91 						   64, 0);
92 	if (!beiscsi_sess->bhs_pool)
93 		goto destroy_sess;
94 
95 	return cls_session;
96 destroy_sess:
97 	iscsi_session_teardown(cls_session);
98 	return NULL;
99 }
100 
101 /**
102  * beiscsi_session_destroy - destroys iscsi session
103  * @cls_session:	pointer to iscsi cls session
104  *
105  * Destroys iSCSI session instance and releases
106  * resources allocated for it.
107  */
108 void beiscsi_session_destroy(struct iscsi_cls_session *cls_session)
109 {
110 	struct iscsi_session *sess = cls_session->dd_data;
111 	struct beiscsi_session *beiscsi_sess = sess->dd_data;
112 
113 	printk(KERN_INFO "In beiscsi_session_destroy\n");
114 	dma_pool_destroy(beiscsi_sess->bhs_pool);
115 	iscsi_session_teardown(cls_session);
116 }
117 
118 /**
119  * beiscsi_session_fail(): Closing session with appropriate error
120  * @cls_session: ptr to session
121  **/
122 void beiscsi_session_fail(struct iscsi_cls_session *cls_session)
123 {
124 	iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
125 }
126 
127 
128 /**
129  * beiscsi_conn_create - create an instance of iscsi connection
130  * @cls_session: ptr to iscsi_cls_session
131  * @cid: iscsi cid
132  */
133 struct iscsi_cls_conn *
134 beiscsi_conn_create(struct iscsi_cls_session *cls_session, u32 cid)
135 {
136 	struct beiscsi_hba *phba;
137 	struct Scsi_Host *shost;
138 	struct iscsi_cls_conn *cls_conn;
139 	struct beiscsi_conn *beiscsi_conn;
140 	struct iscsi_conn *conn;
141 	struct iscsi_session *sess;
142 	struct beiscsi_session *beiscsi_sess;
143 
144 	shost = iscsi_session_to_shost(cls_session);
145 	phba = iscsi_host_priv(shost);
146 
147 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
148 		    "BS_%d : In beiscsi_conn_create ,cid"
149 		    "from iscsi layer=%d\n", cid);
150 
151 	cls_conn = iscsi_conn_setup(cls_session, sizeof(*beiscsi_conn), cid);
152 	if (!cls_conn)
153 		return NULL;
154 
155 	conn = cls_conn->dd_data;
156 	beiscsi_conn = conn->dd_data;
157 	beiscsi_conn->ep = NULL;
158 	beiscsi_conn->phba = phba;
159 	beiscsi_conn->conn = conn;
160 	sess = cls_session->dd_data;
161 	beiscsi_sess = sess->dd_data;
162 	beiscsi_conn->beiscsi_sess = beiscsi_sess;
163 	return cls_conn;
164 }
165 
166 /**
167  * beiscsi_conn_bind - Binds iscsi session/connection with TCP connection
168  * @cls_session: pointer to iscsi cls session
169  * @cls_conn: pointer to iscsi cls conn
170  * @transport_fd: EP handle(64 bit)
171  *
172  * This function binds the TCP Conn with iSCSI Connection and Session.
173  */
174 int beiscsi_conn_bind(struct iscsi_cls_session *cls_session,
175 		      struct iscsi_cls_conn *cls_conn,
176 		      u64 transport_fd, int is_leading)
177 {
178 	struct iscsi_conn *conn = cls_conn->dd_data;
179 	struct beiscsi_conn *beiscsi_conn = conn->dd_data;
180 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
181 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
182 	struct hwi_controller *phwi_ctrlr = phba->phwi_ctrlr;
183 	struct hwi_wrb_context *pwrb_context;
184 	struct beiscsi_endpoint *beiscsi_ep;
185 	struct iscsi_endpoint *ep;
186 	uint16_t cri_index;
187 
188 	ep = iscsi_lookup_endpoint(transport_fd);
189 	if (!ep)
190 		return -EINVAL;
191 
192 	beiscsi_ep = ep->dd_data;
193 
194 	if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
195 		return -EINVAL;
196 
197 	if (beiscsi_ep->phba != phba) {
198 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
199 			    "BS_%d : beiscsi_ep->hba=%p not equal to phba=%p\n",
200 			    beiscsi_ep->phba, phba);
201 
202 		return -EEXIST;
203 	}
204 	cri_index = BE_GET_CRI_FROM_CID(beiscsi_ep->ep_cid);
205 	if (phba->conn_table[cri_index]) {
206 		if (beiscsi_conn != phba->conn_table[cri_index] ||
207 		    beiscsi_ep != phba->conn_table[cri_index]->ep) {
208 			__beiscsi_log(phba, KERN_ERR,
209 				      "BS_%d : conn_table not empty at %u: cid %u conn %p:%p\n",
210 				      cri_index,
211 				      beiscsi_ep->ep_cid,
212 				      beiscsi_conn,
213 				      phba->conn_table[cri_index]);
214 			return -EINVAL;
215 		}
216 	}
217 
218 	beiscsi_conn->beiscsi_conn_cid = beiscsi_ep->ep_cid;
219 	beiscsi_conn->ep = beiscsi_ep;
220 	beiscsi_ep->conn = beiscsi_conn;
221 	/**
222 	 * Each connection is associated with a WRBQ kept in wrb_context.
223 	 * Store doorbell offset for transmit path.
224 	 */
225 	pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
226 	beiscsi_conn->doorbell_offset = pwrb_context->doorbell_offset;
227 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
228 		    "BS_%d : cid %d phba->conn_table[%u]=%p\n",
229 		    beiscsi_ep->ep_cid, cri_index, beiscsi_conn);
230 	phba->conn_table[cri_index] = beiscsi_conn;
231 	return 0;
232 }
233 
234 static int beiscsi_iface_create_ipv4(struct beiscsi_hba *phba)
235 {
236 	if (phba->ipv4_iface)
237 		return 0;
238 
239 	phba->ipv4_iface = iscsi_create_iface(phba->shost,
240 					      &beiscsi_iscsi_transport,
241 					      ISCSI_IFACE_TYPE_IPV4,
242 					      0, 0);
243 	if (!phba->ipv4_iface) {
244 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
245 			    "BS_%d : Could not "
246 			    "create default IPv4 address.\n");
247 		return -ENODEV;
248 	}
249 
250 	return 0;
251 }
252 
253 static int beiscsi_iface_create_ipv6(struct beiscsi_hba *phba)
254 {
255 	if (phba->ipv6_iface)
256 		return 0;
257 
258 	phba->ipv6_iface = iscsi_create_iface(phba->shost,
259 					      &beiscsi_iscsi_transport,
260 					      ISCSI_IFACE_TYPE_IPV6,
261 					      0, 0);
262 	if (!phba->ipv6_iface) {
263 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
264 			    "BS_%d : Could not "
265 			    "create default IPv6 address.\n");
266 		return -ENODEV;
267 	}
268 
269 	return 0;
270 }
271 
272 void beiscsi_iface_create_default(struct beiscsi_hba *phba)
273 {
274 	struct be_cmd_get_if_info_resp *if_info;
275 
276 	if (!beiscsi_if_get_info(phba, BEISCSI_IP_TYPE_V4, &if_info)) {
277 		beiscsi_iface_create_ipv4(phba);
278 		kfree(if_info);
279 	}
280 
281 	if (!beiscsi_if_get_info(phba, BEISCSI_IP_TYPE_V6, &if_info)) {
282 		beiscsi_iface_create_ipv6(phba);
283 		kfree(if_info);
284 	}
285 }
286 
287 void beiscsi_iface_destroy_default(struct beiscsi_hba *phba)
288 {
289 	if (phba->ipv6_iface) {
290 		iscsi_destroy_iface(phba->ipv6_iface);
291 		phba->ipv6_iface = NULL;
292 	}
293 	if (phba->ipv4_iface) {
294 		iscsi_destroy_iface(phba->ipv4_iface);
295 		phba->ipv4_iface = NULL;
296 	}
297 }
298 
299 /**
300  * beiscsi_set_vlan_tag()- Set the VLAN TAG
301  * @shost: Scsi Host for the driver instance
302  * @iface_param: Interface paramters
303  *
304  * Set the VLAN TAG for the adapter or disable
305  * the VLAN config
306  *
307  * returns
308  *	Success: 0
309  *	Failure: Non-Zero Value
310  **/
311 static int
312 beiscsi_iface_config_vlan(struct Scsi_Host *shost,
313 			  struct iscsi_iface_param_info *iface_param)
314 {
315 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
316 	int ret = -EPERM;
317 
318 	switch (iface_param->param) {
319 	case ISCSI_NET_PARAM_VLAN_ENABLED:
320 		ret = 0;
321 		if (iface_param->value[0] != ISCSI_VLAN_ENABLE)
322 			ret = beiscsi_if_set_vlan(phba, BEISCSI_VLAN_DISABLE);
323 		break;
324 	case ISCSI_NET_PARAM_VLAN_TAG:
325 		ret = beiscsi_if_set_vlan(phba,
326 					  *((uint16_t *)iface_param->value));
327 		break;
328 	}
329 	return ret;
330 }
331 
332 
333 static int
334 beiscsi_iface_config_ipv4(struct Scsi_Host *shost,
335 			  struct iscsi_iface_param_info *info,
336 			  void *data, uint32_t dt_len)
337 {
338 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
339 	u8 *ip = NULL, *subnet = NULL, *gw;
340 	struct nlattr *nla;
341 	int ret = -EPERM;
342 
343 	/* Check the param */
344 	switch (info->param) {
345 	case ISCSI_NET_PARAM_IFACE_ENABLE:
346 		if (info->value[0] == ISCSI_IFACE_ENABLE)
347 			ret = beiscsi_iface_create_ipv4(phba);
348 		else {
349 			iscsi_destroy_iface(phba->ipv4_iface);
350 			phba->ipv4_iface = NULL;
351 		}
352 		break;
353 	case ISCSI_NET_PARAM_IPV4_GW:
354 		gw = info->value;
355 		ret = beiscsi_if_set_gw(phba, BEISCSI_IP_TYPE_V4, gw);
356 		break;
357 	case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
358 		if (info->value[0] == ISCSI_BOOTPROTO_DHCP)
359 			ret = beiscsi_if_en_dhcp(phba, BEISCSI_IP_TYPE_V4);
360 		else if (info->value[0] == ISCSI_BOOTPROTO_STATIC)
361 			/* release DHCP IP address */
362 			ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V4,
363 						   NULL, NULL);
364 		else
365 			beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
366 				    "BS_%d : Invalid BOOTPROTO: %d\n",
367 				    info->value[0]);
368 		break;
369 	case ISCSI_NET_PARAM_IPV4_ADDR:
370 		ip = info->value;
371 		nla = nla_find(data, dt_len, ISCSI_NET_PARAM_IPV4_SUBNET);
372 		if (nla) {
373 			info = nla_data(nla);
374 			subnet = info->value;
375 		}
376 		ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V4,
377 					   ip, subnet);
378 		break;
379 	case ISCSI_NET_PARAM_IPV4_SUBNET:
380 		/*
381 		 * OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR ioctl needs IP
382 		 * and subnet both. Find IP to be applied for this subnet.
383 		 */
384 		subnet = info->value;
385 		nla = nla_find(data, dt_len, ISCSI_NET_PARAM_IPV4_ADDR);
386 		if (nla) {
387 			info = nla_data(nla);
388 			ip = info->value;
389 		}
390 		ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V4,
391 					   ip, subnet);
392 		break;
393 	}
394 
395 	return ret;
396 }
397 
398 static int
399 beiscsi_iface_config_ipv6(struct Scsi_Host *shost,
400 			  struct iscsi_iface_param_info *iface_param,
401 			  void *data, uint32_t dt_len)
402 {
403 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
404 	int ret = -EPERM;
405 
406 	switch (iface_param->param) {
407 	case ISCSI_NET_PARAM_IFACE_ENABLE:
408 		if (iface_param->value[0] == ISCSI_IFACE_ENABLE)
409 			ret = beiscsi_iface_create_ipv6(phba);
410 		else {
411 			iscsi_destroy_iface(phba->ipv6_iface);
412 			phba->ipv6_iface = NULL;
413 		}
414 		break;
415 	case ISCSI_NET_PARAM_IPV6_ADDR:
416 		ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V6,
417 					   iface_param->value, NULL);
418 		break;
419 	}
420 
421 	return ret;
422 }
423 
424 int beiscsi_iface_set_param(struct Scsi_Host *shost,
425 			    void *data, uint32_t dt_len)
426 {
427 	struct iscsi_iface_param_info *iface_param = NULL;
428 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
429 	struct nlattr *attrib;
430 	uint32_t rm_len = dt_len;
431 	int ret;
432 
433 	if (!beiscsi_hba_is_online(phba)) {
434 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
435 			    "BS_%d : HBA in error 0x%lx\n", phba->state);
436 		return -EBUSY;
437 	}
438 
439 	/* update interface_handle */
440 	ret = beiscsi_if_get_handle(phba);
441 	if (ret) {
442 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
443 			    "BS_%d : Getting Interface Handle Failed\n");
444 		return ret;
445 	}
446 
447 	nla_for_each_attr(attrib, data, dt_len, rm_len) {
448 		iface_param = nla_data(attrib);
449 
450 		if (iface_param->param_type != ISCSI_NET_PARAM)
451 			continue;
452 
453 		/*
454 		 * BE2ISCSI only supports 1 interface
455 		 */
456 		if (iface_param->iface_num) {
457 			beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
458 				    "BS_%d : Invalid iface_num %d."
459 				    "Only iface_num 0 is supported.\n",
460 				    iface_param->iface_num);
461 
462 			return -EINVAL;
463 		}
464 
465 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
466 			    "BS_%d : %s.0 set param %d",
467 			    (iface_param->iface_type == ISCSI_IFACE_TYPE_IPV4) ?
468 			    "ipv4" : "ipv6", iface_param->param);
469 
470 		ret = -EPERM;
471 		switch (iface_param->param) {
472 		case ISCSI_NET_PARAM_VLAN_ENABLED:
473 		case ISCSI_NET_PARAM_VLAN_TAG:
474 			ret = beiscsi_iface_config_vlan(shost, iface_param);
475 			break;
476 		default:
477 			switch (iface_param->iface_type) {
478 			case ISCSI_IFACE_TYPE_IPV4:
479 				ret = beiscsi_iface_config_ipv4(shost,
480 								iface_param,
481 								data, dt_len);
482 				break;
483 			case ISCSI_IFACE_TYPE_IPV6:
484 				ret = beiscsi_iface_config_ipv6(shost,
485 								iface_param,
486 								data, dt_len);
487 				break;
488 			}
489 		}
490 
491 		if (ret == -EPERM) {
492 			__beiscsi_log(phba, KERN_ERR,
493 				      "BS_%d : %s.0 set param %d not permitted",
494 				      (iface_param->iface_type ==
495 				       ISCSI_IFACE_TYPE_IPV4) ? "ipv4" : "ipv6",
496 				      iface_param->param);
497 			ret = 0;
498 		}
499 		if (ret)
500 			break;
501 	}
502 
503 	return ret;
504 }
505 
506 static int __beiscsi_iface_get_param(struct beiscsi_hba *phba,
507 				     struct iscsi_iface *iface,
508 				     int param, char *buf)
509 {
510 	struct be_cmd_get_if_info_resp *if_info;
511 	int len, ip_type = BEISCSI_IP_TYPE_V4;
512 
513 	if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
514 		ip_type = BEISCSI_IP_TYPE_V6;
515 
516 	len = beiscsi_if_get_info(phba, ip_type, &if_info);
517 	if (len)
518 		return len;
519 
520 	switch (param) {
521 	case ISCSI_NET_PARAM_IPV4_ADDR:
522 		len = sprintf(buf, "%pI4\n", if_info->ip_addr.addr);
523 		break;
524 	case ISCSI_NET_PARAM_IPV6_ADDR:
525 		len = sprintf(buf, "%pI6\n", if_info->ip_addr.addr);
526 		break;
527 	case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
528 		if (!if_info->dhcp_state)
529 			len = sprintf(buf, "static\n");
530 		else
531 			len = sprintf(buf, "dhcp\n");
532 		break;
533 	case ISCSI_NET_PARAM_IPV4_SUBNET:
534 		len = sprintf(buf, "%pI4\n", if_info->ip_addr.subnet_mask);
535 		break;
536 	case ISCSI_NET_PARAM_VLAN_ENABLED:
537 		len = sprintf(buf, "%s\n",
538 			      (if_info->vlan_priority == BEISCSI_VLAN_DISABLE) ?
539 			      "disable" : "enable");
540 		break;
541 	case ISCSI_NET_PARAM_VLAN_ID:
542 		if (if_info->vlan_priority == BEISCSI_VLAN_DISABLE)
543 			len = -EINVAL;
544 		else
545 			len = sprintf(buf, "%d\n",
546 				      (if_info->vlan_priority &
547 				       ISCSI_MAX_VLAN_ID));
548 		break;
549 	case ISCSI_NET_PARAM_VLAN_PRIORITY:
550 		if (if_info->vlan_priority == BEISCSI_VLAN_DISABLE)
551 			len = -EINVAL;
552 		else
553 			len = sprintf(buf, "%d\n",
554 				      ((if_info->vlan_priority >> 13) &
555 				       ISCSI_MAX_VLAN_PRIORITY));
556 		break;
557 	default:
558 		WARN_ON(1);
559 	}
560 
561 	kfree(if_info);
562 	return len;
563 }
564 
565 int beiscsi_iface_get_param(struct iscsi_iface *iface,
566 			    enum iscsi_param_type param_type,
567 			    int param, char *buf)
568 {
569 	struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
570 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
571 	struct be_cmd_get_def_gateway_resp gateway;
572 	int len = -EPERM;
573 
574 	if (param_type != ISCSI_NET_PARAM)
575 		return 0;
576 	if (!beiscsi_hba_is_online(phba)) {
577 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
578 			    "BS_%d : HBA in error 0x%lx\n", phba->state);
579 		return -EBUSY;
580 	}
581 
582 	switch (param) {
583 	case ISCSI_NET_PARAM_IPV4_ADDR:
584 	case ISCSI_NET_PARAM_IPV4_SUBNET:
585 	case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
586 	case ISCSI_NET_PARAM_IPV6_ADDR:
587 	case ISCSI_NET_PARAM_VLAN_ENABLED:
588 	case ISCSI_NET_PARAM_VLAN_ID:
589 	case ISCSI_NET_PARAM_VLAN_PRIORITY:
590 		len = __beiscsi_iface_get_param(phba, iface, param, buf);
591 		break;
592 	case ISCSI_NET_PARAM_IFACE_ENABLE:
593 		if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
594 			len = sprintf(buf, "%s\n",
595 				      phba->ipv4_iface ? "enable" : "disable");
596 		else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
597 			len = sprintf(buf, "%s\n",
598 				      phba->ipv6_iface ? "enable" : "disable");
599 		break;
600 	case ISCSI_NET_PARAM_IPV4_GW:
601 		memset(&gateway, 0, sizeof(gateway));
602 		len = beiscsi_if_get_gw(phba, BEISCSI_IP_TYPE_V4, &gateway);
603 		if (!len)
604 			len = sprintf(buf, "%pI4\n", &gateway.ip_addr.addr);
605 		break;
606 	}
607 
608 	return len;
609 }
610 
611 /**
612  * beiscsi_ep_get_param - get the iscsi parameter
613  * @ep: pointer to iscsi ep
614  * @param: parameter type identifier
615  * @buf: buffer pointer
616  *
617  * returns iscsi parameter
618  */
619 int beiscsi_ep_get_param(struct iscsi_endpoint *ep,
620 			   enum iscsi_param param, char *buf)
621 {
622 	struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
623 	int len;
624 
625 	beiscsi_log(beiscsi_ep->phba, KERN_INFO,
626 		    BEISCSI_LOG_CONFIG,
627 		    "BS_%d : In beiscsi_ep_get_param,"
628 		    " param= %d\n", param);
629 
630 	switch (param) {
631 	case ISCSI_PARAM_CONN_PORT:
632 		len = sprintf(buf, "%hu\n", beiscsi_ep->dst_tcpport);
633 		break;
634 	case ISCSI_PARAM_CONN_ADDRESS:
635 		if (beiscsi_ep->ip_type == BEISCSI_IP_TYPE_V4)
636 			len = sprintf(buf, "%pI4\n", &beiscsi_ep->dst_addr);
637 		else
638 			len = sprintf(buf, "%pI6\n", &beiscsi_ep->dst6_addr);
639 		break;
640 	default:
641 		len = -EPERM;
642 	}
643 	return len;
644 }
645 
646 int beiscsi_set_param(struct iscsi_cls_conn *cls_conn,
647 		      enum iscsi_param param, char *buf, int buflen)
648 {
649 	struct iscsi_conn *conn = cls_conn->dd_data;
650 	struct iscsi_session *session = conn->session;
651 	struct beiscsi_hba *phba = NULL;
652 	int ret;
653 
654 	phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
655 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
656 		    "BS_%d : In beiscsi_conn_set_param,"
657 		    " param= %d\n", param);
658 
659 	ret = iscsi_set_param(cls_conn, param, buf, buflen);
660 	if (ret)
661 		return ret;
662 	/*
663 	 * If userspace tried to set the value to higher than we can
664 	 * support override here.
665 	 */
666 	switch (param) {
667 	case ISCSI_PARAM_FIRST_BURST:
668 		if (session->first_burst > 8192)
669 			session->first_burst = 8192;
670 		break;
671 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
672 		if (conn->max_recv_dlength > 65536)
673 			conn->max_recv_dlength = 65536;
674 		break;
675 	case ISCSI_PARAM_MAX_BURST:
676 		if (session->max_burst > 262144)
677 			session->max_burst = 262144;
678 		break;
679 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
680 		if (conn->max_xmit_dlength > 65536)
681 			conn->max_xmit_dlength = 65536;
682 		/* fall through */
683 	default:
684 		return 0;
685 	}
686 
687 	return 0;
688 }
689 
690 /**
691  * beiscsi_get_port_state - Get the Port State
692  * @shost : pointer to scsi_host structure
693  *
694  */
695 static void beiscsi_get_port_state(struct Scsi_Host *shost)
696 {
697 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
698 	struct iscsi_cls_host *ihost = shost->shost_data;
699 
700 	ihost->port_state = test_bit(BEISCSI_HBA_LINK_UP, &phba->state) ?
701 		ISCSI_PORT_STATE_UP : ISCSI_PORT_STATE_DOWN;
702 }
703 
704 /**
705  * beiscsi_get_port_speed  - Get the Port Speed from Adapter
706  * @shost : pointer to scsi_host structure
707  *
708  */
709 static void beiscsi_get_port_speed(struct Scsi_Host *shost)
710 {
711 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
712 	struct iscsi_cls_host *ihost = shost->shost_data;
713 
714 	switch (phba->port_speed) {
715 	case BE2ISCSI_LINK_SPEED_10MBPS:
716 		ihost->port_speed = ISCSI_PORT_SPEED_10MBPS;
717 		break;
718 	case BE2ISCSI_LINK_SPEED_100MBPS:
719 		ihost->port_speed = ISCSI_PORT_SPEED_100MBPS;
720 		break;
721 	case BE2ISCSI_LINK_SPEED_1GBPS:
722 		ihost->port_speed = ISCSI_PORT_SPEED_1GBPS;
723 		break;
724 	case BE2ISCSI_LINK_SPEED_10GBPS:
725 		ihost->port_speed = ISCSI_PORT_SPEED_10GBPS;
726 		break;
727 	case BE2ISCSI_LINK_SPEED_25GBPS:
728 		ihost->port_speed = ISCSI_PORT_SPEED_25GBPS;
729 		break;
730 	case BE2ISCSI_LINK_SPEED_40GBPS:
731 		ihost->port_speed = ISCSI_PORT_SPEED_40GBPS;
732 		break;
733 	default:
734 		ihost->port_speed = ISCSI_PORT_SPEED_UNKNOWN;
735 	}
736 }
737 
738 /**
739  * beiscsi_get_host_param - get the iscsi parameter
740  * @shost: pointer to scsi_host structure
741  * @param: parameter type identifier
742  * @buf: buffer pointer
743  *
744  */
745 int beiscsi_get_host_param(struct Scsi_Host *shost,
746 			   enum iscsi_host_param param, char *buf)
747 {
748 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
749 	int status = 0;
750 
751 	if (!beiscsi_hba_is_online(phba)) {
752 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
753 			    "BS_%d : HBA in error 0x%lx\n", phba->state);
754 		return 0;
755 	}
756 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
757 		    "BS_%d : In beiscsi_get_host_param, param = %d\n", param);
758 
759 	switch (param) {
760 	case ISCSI_HOST_PARAM_HWADDRESS:
761 		status = beiscsi_get_macaddr(buf, phba);
762 		if (status < 0) {
763 			beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
764 				    "BS_%d : beiscsi_get_macaddr Failed\n");
765 			return 0;
766 		}
767 		break;
768 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
769 		/* try fetching user configured name first */
770 		status = beiscsi_get_initiator_name(phba, buf, true);
771 		if (status < 0) {
772 			status = beiscsi_get_initiator_name(phba, buf, false);
773 			if (status < 0) {
774 				beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
775 					    "BS_%d : Retrieving Initiator Name Failed\n");
776 				status = 0;
777 			}
778 		}
779 		break;
780 	case ISCSI_HOST_PARAM_PORT_STATE:
781 		beiscsi_get_port_state(shost);
782 		status = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
783 		break;
784 	case ISCSI_HOST_PARAM_PORT_SPEED:
785 		beiscsi_get_port_speed(shost);
786 		status = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
787 		break;
788 	default:
789 		return iscsi_host_get_param(shost, param, buf);
790 	}
791 	return status;
792 }
793 
794 int beiscsi_get_macaddr(char *buf, struct beiscsi_hba *phba)
795 {
796 	struct be_cmd_get_nic_conf_resp resp;
797 	int rc;
798 
799 	if (phba->mac_addr_set)
800 		return sysfs_format_mac(buf, phba->mac_address, ETH_ALEN);
801 
802 	memset(&resp, 0, sizeof(resp));
803 	rc = mgmt_get_nic_conf(phba, &resp);
804 	if (rc)
805 		return rc;
806 
807 	phba->mac_addr_set = true;
808 	memcpy(phba->mac_address, resp.mac_address, ETH_ALEN);
809 	return sysfs_format_mac(buf, phba->mac_address, ETH_ALEN);
810 }
811 
812 /**
813  * beiscsi_conn_get_stats - get the iscsi stats
814  * @cls_conn: pointer to iscsi cls conn
815  * @stats: pointer to iscsi_stats structure
816  *
817  * returns iscsi stats
818  */
819 void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
820 			    struct iscsi_stats *stats)
821 {
822 	struct iscsi_conn *conn = cls_conn->dd_data;
823 	struct beiscsi_hba *phba = NULL;
824 
825 	phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
826 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
827 		    "BS_%d : In beiscsi_conn_get_stats\n");
828 
829 	stats->txdata_octets = conn->txdata_octets;
830 	stats->rxdata_octets = conn->rxdata_octets;
831 	stats->dataout_pdus = conn->dataout_pdus_cnt;
832 	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
833 	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
834 	stats->datain_pdus = conn->datain_pdus_cnt;
835 	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
836 	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
837 	stats->r2t_pdus = conn->r2t_pdus_cnt;
838 	stats->digest_err = 0;
839 	stats->timeout_err = 0;
840 	stats->custom_length = 1;
841 	strcpy(stats->custom[0].desc, "eh_abort_cnt");
842 	stats->custom[0].value = conn->eh_abort_cnt;
843 }
844 
845 /**
846  * beiscsi_set_params_for_offld - get the parameters for offload
847  * @beiscsi_conn: pointer to beiscsi_conn
848  * @params: pointer to offload_params structure
849  */
850 static void  beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
851 					  struct beiscsi_offload_params *params)
852 {
853 	struct iscsi_conn *conn = beiscsi_conn->conn;
854 	struct iscsi_session *session = conn->session;
855 
856 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
857 		      params, session->max_burst);
858 	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
859 		      max_send_data_segment_length, params,
860 		      conn->max_xmit_dlength);
861 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
862 		      params, session->first_burst);
863 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
864 		      session->erl);
865 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
866 		      conn->datadgst_en);
867 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
868 		      conn->hdrdgst_en);
869 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
870 		      session->initial_r2t_en);
871 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
872 		      session->imm_data_en);
873 	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
874 		      data_seq_inorder, params,
875 		      session->dataseq_inorder_en);
876 	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
877 		      pdu_seq_inorder, params,
878 		      session->pdu_inorder_en);
879 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_r2t, params,
880 		      session->max_r2t);
881 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
882 		      (conn->exp_statsn - 1));
883 	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
884 		      max_recv_data_segment_length, params,
885 		      conn->max_recv_dlength);
886 
887 }
888 
889 /**
890  * beiscsi_conn_start - offload of session to chip
891  * @cls_conn: pointer to beiscsi_conn
892  */
893 int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
894 {
895 	struct iscsi_conn *conn = cls_conn->dd_data;
896 	struct beiscsi_conn *beiscsi_conn = conn->dd_data;
897 	struct beiscsi_endpoint *beiscsi_ep;
898 	struct beiscsi_offload_params params;
899 	struct beiscsi_hba *phba;
900 
901 	phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
902 
903 	if (!beiscsi_hba_is_online(phba)) {
904 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
905 			    "BS_%d : HBA in error 0x%lx\n", phba->state);
906 		return -EBUSY;
907 	}
908 	beiscsi_log(beiscsi_conn->phba, KERN_INFO, BEISCSI_LOG_CONFIG,
909 		    "BS_%d : In beiscsi_conn_start\n");
910 
911 	memset(&params, 0, sizeof(struct beiscsi_offload_params));
912 	beiscsi_ep = beiscsi_conn->ep;
913 	if (!beiscsi_ep)
914 		beiscsi_log(beiscsi_conn->phba, KERN_ERR,
915 			    BEISCSI_LOG_CONFIG,
916 			    "BS_%d : In beiscsi_conn_start , no beiscsi_ep\n");
917 
918 	beiscsi_conn->login_in_progress = 0;
919 	beiscsi_set_params_for_offld(beiscsi_conn, &params);
920 	beiscsi_offload_connection(beiscsi_conn, &params);
921 	iscsi_conn_start(cls_conn);
922 	return 0;
923 }
924 
925 /**
926  * beiscsi_get_cid - Allocate a cid
927  * @phba: The phba instance
928  */
929 static int beiscsi_get_cid(struct beiscsi_hba *phba)
930 {
931 	uint16_t cid_avlbl_ulp0, cid_avlbl_ulp1;
932 	unsigned short cid, cid_from_ulp;
933 	struct ulp_cid_info *cid_info;
934 
935 	/* Find the ULP which has more CID available */
936 	cid_avlbl_ulp0 = (phba->cid_array_info[BEISCSI_ULP0]) ?
937 			  BEISCSI_ULP0_AVLBL_CID(phba) : 0;
938 	cid_avlbl_ulp1 = (phba->cid_array_info[BEISCSI_ULP1]) ?
939 			  BEISCSI_ULP1_AVLBL_CID(phba) : 0;
940 	cid_from_ulp = (cid_avlbl_ulp0 > cid_avlbl_ulp1) ?
941 			BEISCSI_ULP0 : BEISCSI_ULP1;
942 	/**
943 	 * If iSCSI protocol is loaded only on ULP 0, and when cid_avlbl_ulp
944 	 * is ZERO for both, ULP 1 is returned.
945 	 * Check if ULP is loaded before getting new CID.
946 	 */
947 	if (!test_bit(cid_from_ulp, (void *)&phba->fw_config.ulp_supported))
948 		return BE_INVALID_CID;
949 
950 	cid_info = phba->cid_array_info[cid_from_ulp];
951 	cid = cid_info->cid_array[cid_info->cid_alloc];
952 	if (!cid_info->avlbl_cids || cid == BE_INVALID_CID) {
953 		__beiscsi_log(phba, KERN_ERR,
954 				"BS_%d : failed to get cid: available %u:%u\n",
955 				cid_info->avlbl_cids, cid_info->cid_free);
956 		return BE_INVALID_CID;
957 	}
958 	/* empty the slot */
959 	cid_info->cid_array[cid_info->cid_alloc++] = BE_INVALID_CID;
960 	if (cid_info->cid_alloc == BEISCSI_GET_CID_COUNT(phba, cid_from_ulp))
961 		cid_info->cid_alloc = 0;
962 	cid_info->avlbl_cids--;
963 	return cid;
964 }
965 
966 /**
967  * beiscsi_put_cid - Free the cid
968  * @phba: The phba for which the cid is being freed
969  * @cid: The cid to free
970  */
971 static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
972 {
973 	uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
974 	struct hwi_wrb_context *pwrb_context;
975 	struct hwi_controller *phwi_ctrlr;
976 	struct ulp_cid_info *cid_info;
977 	uint16_t cid_post_ulp;
978 
979 	phwi_ctrlr = phba->phwi_ctrlr;
980 	pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
981 	cid_post_ulp = pwrb_context->ulp_num;
982 
983 	cid_info = phba->cid_array_info[cid_post_ulp];
984 	/* fill only in empty slot */
985 	if (cid_info->cid_array[cid_info->cid_free] != BE_INVALID_CID) {
986 		__beiscsi_log(phba, KERN_ERR,
987 			      "BS_%d : failed to put cid %u: available %u:%u\n",
988 			      cid, cid_info->avlbl_cids, cid_info->cid_free);
989 		return;
990 	}
991 	cid_info->cid_array[cid_info->cid_free++] = cid;
992 	if (cid_info->cid_free == BEISCSI_GET_CID_COUNT(phba, cid_post_ulp))
993 		cid_info->cid_free = 0;
994 	cid_info->avlbl_cids++;
995 }
996 
997 /**
998  * beiscsi_free_ep - free endpoint
999  * @ep:	pointer to iscsi endpoint structure
1000  */
1001 static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
1002 {
1003 	struct beiscsi_hba *phba = beiscsi_ep->phba;
1004 	struct beiscsi_conn *beiscsi_conn;
1005 
1006 	beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
1007 	beiscsi_ep->phba = NULL;
1008 	/* clear this to track freeing in beiscsi_ep_disconnect */
1009 	phba->ep_array[BE_GET_CRI_FROM_CID(beiscsi_ep->ep_cid)] = NULL;
1010 
1011 	/**
1012 	 * Check if any connection resource allocated by driver
1013 	 * is to be freed.This case occurs when target redirection
1014 	 * or connection retry is done.
1015 	 **/
1016 	if (!beiscsi_ep->conn)
1017 		return;
1018 
1019 	beiscsi_conn = beiscsi_ep->conn;
1020 	/**
1021 	 * Break ep->conn link here so that completions after
1022 	 * this are ignored.
1023 	 */
1024 	beiscsi_ep->conn = NULL;
1025 	if (beiscsi_conn->login_in_progress) {
1026 		beiscsi_free_mgmt_task_handles(beiscsi_conn,
1027 					       beiscsi_conn->task);
1028 		beiscsi_conn->login_in_progress = 0;
1029 	}
1030 }
1031 
1032 /**
1033  * beiscsi_open_conn - Ask FW to open a TCP connection
1034  * @ep:	endpoint to be used
1035  * @src_addr: The source IP address
1036  * @dst_addr: The Destination  IP address
1037  *
1038  * Asks the FW to open a TCP connection
1039  */
1040 static int beiscsi_open_conn(struct iscsi_endpoint *ep,
1041 			     struct sockaddr *src_addr,
1042 			     struct sockaddr *dst_addr, int non_blocking)
1043 {
1044 	struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
1045 	struct beiscsi_hba *phba = beiscsi_ep->phba;
1046 	struct tcp_connect_and_offload_out *ptcpcnct_out;
1047 	struct be_dma_mem nonemb_cmd;
1048 	unsigned int tag, req_memsize;
1049 	int ret = -ENOMEM;
1050 
1051 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1052 		    "BS_%d : In beiscsi_open_conn\n");
1053 
1054 	beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
1055 	if (beiscsi_ep->ep_cid == BE_INVALID_CID) {
1056 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1057 			    "BS_%d : No free cid available\n");
1058 		return ret;
1059 	}
1060 
1061 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1062 		    "BS_%d : In beiscsi_open_conn, ep_cid=%d\n",
1063 		    beiscsi_ep->ep_cid);
1064 
1065 	phba->ep_array[BE_GET_CRI_FROM_CID
1066 		       (beiscsi_ep->ep_cid)] = ep;
1067 
1068 	beiscsi_ep->cid_vld = 0;
1069 
1070 	if (is_chip_be2_be3r(phba))
1071 		req_memsize = sizeof(struct tcp_connect_and_offload_in);
1072 	else
1073 		req_memsize = sizeof(struct tcp_connect_and_offload_in_v1);
1074 
1075 	nonemb_cmd.va = dma_alloc_coherent(&phba->ctrl.pdev->dev,
1076 				req_memsize,
1077 				&nonemb_cmd.dma, GFP_KERNEL);
1078 	if (nonemb_cmd.va == NULL) {
1079 
1080 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1081 			    "BS_%d : Failed to allocate memory for"
1082 			    " mgmt_open_connection\n");
1083 
1084 		beiscsi_free_ep(beiscsi_ep);
1085 		return -ENOMEM;
1086 	}
1087 	nonemb_cmd.size = req_memsize;
1088 	memset(nonemb_cmd.va, 0, nonemb_cmd.size);
1089 	tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep, &nonemb_cmd);
1090 	if (!tag) {
1091 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1092 			    "BS_%d : mgmt_open_connection Failed for cid=%d\n",
1093 			    beiscsi_ep->ep_cid);
1094 
1095 		dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
1096 				    nonemb_cmd.va, nonemb_cmd.dma);
1097 		beiscsi_free_ep(beiscsi_ep);
1098 		return -EAGAIN;
1099 	}
1100 
1101 	ret = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
1102 	if (ret) {
1103 		beiscsi_log(phba, KERN_ERR,
1104 			    BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
1105 			    "BS_%d : mgmt_open_connection Failed");
1106 
1107 		if (ret != -EBUSY)
1108 			dma_free_coherent(&phba->ctrl.pdev->dev,
1109 					nonemb_cmd.size, nonemb_cmd.va,
1110 					nonemb_cmd.dma);
1111 
1112 		beiscsi_free_ep(beiscsi_ep);
1113 		return ret;
1114 	}
1115 
1116 	ptcpcnct_out = (struct tcp_connect_and_offload_out *)nonemb_cmd.va;
1117 	beiscsi_ep = ep->dd_data;
1118 	beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
1119 	beiscsi_ep->cid_vld = 1;
1120 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1121 		    "BS_%d : mgmt_open_connection Success\n");
1122 
1123 	dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
1124 			    nonemb_cmd.va, nonemb_cmd.dma);
1125 	return 0;
1126 }
1127 
1128 /**
1129  * beiscsi_ep_connect - Ask chip to create TCP Conn
1130  * @scsi_host: Pointer to scsi_host structure
1131  * @dst_addr: The IP address of Target
1132  * @non_blocking: blocking or non-blocking call
1133  *
1134  * This routines first asks chip to create a connection and then allocates an EP
1135  */
1136 struct iscsi_endpoint *
1137 beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
1138 		   int non_blocking)
1139 {
1140 	struct beiscsi_hba *phba;
1141 	struct beiscsi_endpoint *beiscsi_ep;
1142 	struct iscsi_endpoint *ep;
1143 	int ret;
1144 
1145 	if (!shost) {
1146 		ret = -ENXIO;
1147 		pr_err("beiscsi_ep_connect shost is NULL\n");
1148 		return ERR_PTR(ret);
1149 	}
1150 
1151 	phba = iscsi_host_priv(shost);
1152 	if (!beiscsi_hba_is_online(phba)) {
1153 		ret = -EIO;
1154 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1155 			    "BS_%d : HBA in error 0x%lx\n", phba->state);
1156 		return ERR_PTR(ret);
1157 	}
1158 	if (!test_bit(BEISCSI_HBA_LINK_UP, &phba->state)) {
1159 		ret = -EBUSY;
1160 		beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1161 			    "BS_%d : The Adapter Port state is Down!!!\n");
1162 		return ERR_PTR(ret);
1163 	}
1164 
1165 	ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
1166 	if (!ep) {
1167 		ret = -ENOMEM;
1168 		return ERR_PTR(ret);
1169 	}
1170 
1171 	beiscsi_ep = ep->dd_data;
1172 	beiscsi_ep->phba = phba;
1173 	beiscsi_ep->openiscsi_ep = ep;
1174 	ret = beiscsi_open_conn(ep, NULL, dst_addr, non_blocking);
1175 	if (ret) {
1176 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1177 			    "BS_%d : Failed in beiscsi_open_conn\n");
1178 		goto free_ep;
1179 	}
1180 
1181 	return ep;
1182 
1183 free_ep:
1184 	iscsi_destroy_endpoint(ep);
1185 	return ERR_PTR(ret);
1186 }
1187 
1188 /**
1189  * beiscsi_ep_poll - Poll to see if connection is established
1190  * @ep:	endpoint to be used
1191  * @timeout_ms: timeout specified in millisecs
1192  *
1193  * Poll to see if TCP connection established
1194  */
1195 int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1196 {
1197 	struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
1198 
1199 	beiscsi_log(beiscsi_ep->phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1200 		    "BS_%d : In  beiscsi_ep_poll\n");
1201 
1202 	if (beiscsi_ep->cid_vld == 1)
1203 		return 1;
1204 	else
1205 		return 0;
1206 }
1207 
1208 /**
1209  * beiscsi_flush_cq()- Flush the CQ created.
1210  * @phba: ptr device priv structure.
1211  *
1212  * Before the connection resource are freed flush
1213  * all the CQ enteries
1214  **/
1215 static void beiscsi_flush_cq(struct beiscsi_hba *phba)
1216 {
1217 	uint16_t i;
1218 	struct be_eq_obj *pbe_eq;
1219 	struct hwi_controller *phwi_ctrlr;
1220 	struct hwi_context_memory *phwi_context;
1221 
1222 	phwi_ctrlr = phba->phwi_ctrlr;
1223 	phwi_context = phwi_ctrlr->phwi_ctxt;
1224 
1225 	for (i = 0; i < phba->num_cpus; i++) {
1226 		pbe_eq = &phwi_context->be_eq[i];
1227 		irq_poll_disable(&pbe_eq->iopoll);
1228 		beiscsi_process_cq(pbe_eq, BE2_MAX_NUM_CQ_PROC);
1229 		irq_poll_enable(&pbe_eq->iopoll);
1230 	}
1231 }
1232 
1233 /**
1234  * beiscsi_conn_close - Invalidate and upload connection
1235  * @ep: The iscsi endpoint
1236  *
1237  * Returns 0 on success,  -1 on failure.
1238  */
1239 static int beiscsi_conn_close(struct beiscsi_endpoint *beiscsi_ep)
1240 {
1241 	struct beiscsi_hba *phba = beiscsi_ep->phba;
1242 	unsigned int tag, attempts;
1243 	int ret;
1244 
1245 	/**
1246 	 * Without successfully invalidating and uploading connection
1247 	 * driver can't reuse the CID so attempt more than once.
1248 	 */
1249 	attempts = 0;
1250 	while (attempts++ < 3) {
1251 		tag = beiscsi_invalidate_cxn(phba, beiscsi_ep);
1252 		if (tag) {
1253 			ret = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
1254 			if (!ret)
1255 				break;
1256 			beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1257 				    "BS_%d : invalidate conn failed cid %d\n",
1258 				    beiscsi_ep->ep_cid);
1259 		}
1260 	}
1261 
1262 	/* wait for all completions to arrive, then process them */
1263 	msleep(250);
1264 	/* flush CQ entries */
1265 	beiscsi_flush_cq(phba);
1266 
1267 	if (attempts > 3)
1268 		return -1;
1269 
1270 	attempts = 0;
1271 	while (attempts++ < 3) {
1272 		tag = beiscsi_upload_cxn(phba, beiscsi_ep);
1273 		if (tag) {
1274 			ret = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
1275 			if (!ret)
1276 				break;
1277 			beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1278 				    "BS_%d : upload conn failed cid %d\n",
1279 				    beiscsi_ep->ep_cid);
1280 		}
1281 	}
1282 	if (attempts > 3)
1283 		return -1;
1284 
1285 	return 0;
1286 }
1287 
1288 /**
1289  * beiscsi_ep_disconnect - Tears down the TCP connection
1290  * @ep:	endpoint to be used
1291  *
1292  * Tears down the TCP connection
1293  */
1294 void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
1295 {
1296 	struct beiscsi_endpoint *beiscsi_ep;
1297 	struct beiscsi_conn *beiscsi_conn;
1298 	struct beiscsi_hba *phba;
1299 	uint16_t cri_index;
1300 
1301 	beiscsi_ep = ep->dd_data;
1302 	phba = beiscsi_ep->phba;
1303 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1304 		    "BS_%d : In beiscsi_ep_disconnect for ep_cid = %u\n",
1305 		    beiscsi_ep->ep_cid);
1306 
1307 	cri_index = BE_GET_CRI_FROM_CID(beiscsi_ep->ep_cid);
1308 	if (!phba->ep_array[cri_index]) {
1309 		__beiscsi_log(phba, KERN_ERR,
1310 			      "BS_%d : ep_array at %u cid %u empty\n",
1311 			      cri_index,
1312 			      beiscsi_ep->ep_cid);
1313 		return;
1314 	}
1315 
1316 	if (beiscsi_ep->conn) {
1317 		beiscsi_conn = beiscsi_ep->conn;
1318 		iscsi_suspend_queue(beiscsi_conn->conn);
1319 	}
1320 
1321 	if (!beiscsi_hba_is_online(phba)) {
1322 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1323 			    "BS_%d : HBA in error 0x%lx\n", phba->state);
1324 	} else {
1325 		/**
1326 		 * Make CID available even if close fails.
1327 		 * If not freed, FW might fail open using the CID.
1328 		 */
1329 		if (beiscsi_conn_close(beiscsi_ep) < 0)
1330 			__beiscsi_log(phba, KERN_ERR,
1331 				      "BS_%d : close conn failed cid %d\n",
1332 				      beiscsi_ep->ep_cid);
1333 	}
1334 
1335 	beiscsi_free_ep(beiscsi_ep);
1336 	if (!phba->conn_table[cri_index])
1337 		__beiscsi_log(phba, KERN_ERR,
1338 			      "BS_%d : conn_table empty at %u: cid %u\n",
1339 			      cri_index, beiscsi_ep->ep_cid);
1340 	phba->conn_table[cri_index] = NULL;
1341 	iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
1342 }
1343 
1344 umode_t beiscsi_attr_is_visible(int param_type, int param)
1345 {
1346 	switch (param_type) {
1347 	case ISCSI_NET_PARAM:
1348 		switch (param) {
1349 		case ISCSI_NET_PARAM_IFACE_ENABLE:
1350 		case ISCSI_NET_PARAM_IPV4_ADDR:
1351 		case ISCSI_NET_PARAM_IPV4_SUBNET:
1352 		case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1353 		case ISCSI_NET_PARAM_IPV4_GW:
1354 		case ISCSI_NET_PARAM_IPV6_ADDR:
1355 		case ISCSI_NET_PARAM_VLAN_ID:
1356 		case ISCSI_NET_PARAM_VLAN_PRIORITY:
1357 		case ISCSI_NET_PARAM_VLAN_ENABLED:
1358 			return S_IRUGO;
1359 		default:
1360 			return 0;
1361 		}
1362 	case ISCSI_HOST_PARAM:
1363 		switch (param) {
1364 		case ISCSI_HOST_PARAM_HWADDRESS:
1365 		case ISCSI_HOST_PARAM_INITIATOR_NAME:
1366 		case ISCSI_HOST_PARAM_PORT_STATE:
1367 		case ISCSI_HOST_PARAM_PORT_SPEED:
1368 			return S_IRUGO;
1369 		default:
1370 			return 0;
1371 		}
1372 	case ISCSI_PARAM:
1373 		switch (param) {
1374 		case ISCSI_PARAM_MAX_RECV_DLENGTH:
1375 		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1376 		case ISCSI_PARAM_HDRDGST_EN:
1377 		case ISCSI_PARAM_DATADGST_EN:
1378 		case ISCSI_PARAM_CONN_ADDRESS:
1379 		case ISCSI_PARAM_CONN_PORT:
1380 		case ISCSI_PARAM_EXP_STATSN:
1381 		case ISCSI_PARAM_PERSISTENT_ADDRESS:
1382 		case ISCSI_PARAM_PERSISTENT_PORT:
1383 		case ISCSI_PARAM_PING_TMO:
1384 		case ISCSI_PARAM_RECV_TMO:
1385 		case ISCSI_PARAM_INITIAL_R2T_EN:
1386 		case ISCSI_PARAM_MAX_R2T:
1387 		case ISCSI_PARAM_IMM_DATA_EN:
1388 		case ISCSI_PARAM_FIRST_BURST:
1389 		case ISCSI_PARAM_MAX_BURST:
1390 		case ISCSI_PARAM_PDU_INORDER_EN:
1391 		case ISCSI_PARAM_DATASEQ_INORDER_EN:
1392 		case ISCSI_PARAM_ERL:
1393 		case ISCSI_PARAM_TARGET_NAME:
1394 		case ISCSI_PARAM_TPGT:
1395 		case ISCSI_PARAM_USERNAME:
1396 		case ISCSI_PARAM_PASSWORD:
1397 		case ISCSI_PARAM_USERNAME_IN:
1398 		case ISCSI_PARAM_PASSWORD_IN:
1399 		case ISCSI_PARAM_FAST_ABORT:
1400 		case ISCSI_PARAM_ABORT_TMO:
1401 		case ISCSI_PARAM_LU_RESET_TMO:
1402 		case ISCSI_PARAM_IFACE_NAME:
1403 		case ISCSI_PARAM_INITIATOR_NAME:
1404 			return S_IRUGO;
1405 		default:
1406 			return 0;
1407 		}
1408 	}
1409 
1410 	return 0;
1411 }
1412