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