xref: /openbmc/linux/drivers/scsi/be2iscsi/be_iscsi.c (revision e285d5bf)
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 	default:
683 		return 0;
684 	}
685 
686 	return 0;
687 }
688 
689 /**
690  * beiscsi_get_port_state - Get the Port State
691  * @shost : pointer to scsi_host structure
692  *
693  */
694 static void beiscsi_get_port_state(struct Scsi_Host *shost)
695 {
696 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
697 	struct iscsi_cls_host *ihost = shost->shost_data;
698 
699 	ihost->port_state = test_bit(BEISCSI_HBA_LINK_UP, &phba->state) ?
700 		ISCSI_PORT_STATE_UP : ISCSI_PORT_STATE_DOWN;
701 }
702 
703 /**
704  * beiscsi_get_port_speed  - Get the Port Speed from Adapter
705  * @shost : pointer to scsi_host structure
706  *
707  */
708 static void beiscsi_get_port_speed(struct Scsi_Host *shost)
709 {
710 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
711 	struct iscsi_cls_host *ihost = shost->shost_data;
712 
713 	switch (phba->port_speed) {
714 	case BE2ISCSI_LINK_SPEED_10MBPS:
715 		ihost->port_speed = ISCSI_PORT_SPEED_10MBPS;
716 		break;
717 	case BE2ISCSI_LINK_SPEED_100MBPS:
718 		ihost->port_speed = ISCSI_PORT_SPEED_100MBPS;
719 		break;
720 	case BE2ISCSI_LINK_SPEED_1GBPS:
721 		ihost->port_speed = ISCSI_PORT_SPEED_1GBPS;
722 		break;
723 	case BE2ISCSI_LINK_SPEED_10GBPS:
724 		ihost->port_speed = ISCSI_PORT_SPEED_10GBPS;
725 		break;
726 	case BE2ISCSI_LINK_SPEED_25GBPS:
727 		ihost->port_speed = ISCSI_PORT_SPEED_25GBPS;
728 		break;
729 	case BE2ISCSI_LINK_SPEED_40GBPS:
730 		ihost->port_speed = ISCSI_PORT_SPEED_40GBPS;
731 		break;
732 	default:
733 		ihost->port_speed = ISCSI_PORT_SPEED_UNKNOWN;
734 	}
735 }
736 
737 /**
738  * beiscsi_get_host_param - get the iscsi parameter
739  * @shost: pointer to scsi_host structure
740  * @param: parameter type identifier
741  * @buf: buffer pointer
742  *
743  */
744 int beiscsi_get_host_param(struct Scsi_Host *shost,
745 			   enum iscsi_host_param param, char *buf)
746 {
747 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
748 	int status = 0;
749 
750 	if (!beiscsi_hba_is_online(phba)) {
751 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
752 			    "BS_%d : HBA in error 0x%lx\n", phba->state);
753 		return 0;
754 	}
755 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
756 		    "BS_%d : In beiscsi_get_host_param, param = %d\n", param);
757 
758 	switch (param) {
759 	case ISCSI_HOST_PARAM_HWADDRESS:
760 		status = beiscsi_get_macaddr(buf, phba);
761 		if (status < 0) {
762 			beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
763 				    "BS_%d : beiscsi_get_macaddr Failed\n");
764 			return 0;
765 		}
766 		break;
767 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
768 		/* try fetching user configured name first */
769 		status = beiscsi_get_initiator_name(phba, buf, true);
770 		if (status < 0) {
771 			status = beiscsi_get_initiator_name(phba, buf, false);
772 			if (status < 0) {
773 				beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
774 					    "BS_%d : Retreiving Initiator Name Failed\n");
775 				status = 0;
776 			}
777 		}
778 		break;
779 	case ISCSI_HOST_PARAM_PORT_STATE:
780 		beiscsi_get_port_state(shost);
781 		status = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
782 		break;
783 	case ISCSI_HOST_PARAM_PORT_SPEED:
784 		beiscsi_get_port_speed(shost);
785 		status = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
786 		break;
787 	default:
788 		return iscsi_host_get_param(shost, param, buf);
789 	}
790 	return status;
791 }
792 
793 int beiscsi_get_macaddr(char *buf, struct beiscsi_hba *phba)
794 {
795 	struct be_cmd_get_nic_conf_resp resp;
796 	int rc;
797 
798 	if (phba->mac_addr_set)
799 		return sysfs_format_mac(buf, phba->mac_address, ETH_ALEN);
800 
801 	memset(&resp, 0, sizeof(resp));
802 	rc = mgmt_get_nic_conf(phba, &resp);
803 	if (rc)
804 		return rc;
805 
806 	phba->mac_addr_set = true;
807 	memcpy(phba->mac_address, resp.mac_address, ETH_ALEN);
808 	return sysfs_format_mac(buf, phba->mac_address, ETH_ALEN);
809 }
810 
811 /**
812  * beiscsi_conn_get_stats - get the iscsi stats
813  * @cls_conn: pointer to iscsi cls conn
814  * @stats: pointer to iscsi_stats structure
815  *
816  * returns iscsi stats
817  */
818 void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
819 			    struct iscsi_stats *stats)
820 {
821 	struct iscsi_conn *conn = cls_conn->dd_data;
822 	struct beiscsi_hba *phba = NULL;
823 
824 	phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
825 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
826 		    "BS_%d : In beiscsi_conn_get_stats\n");
827 
828 	stats->txdata_octets = conn->txdata_octets;
829 	stats->rxdata_octets = conn->rxdata_octets;
830 	stats->dataout_pdus = conn->dataout_pdus_cnt;
831 	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
832 	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
833 	stats->datain_pdus = conn->datain_pdus_cnt;
834 	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
835 	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
836 	stats->r2t_pdus = conn->r2t_pdus_cnt;
837 	stats->digest_err = 0;
838 	stats->timeout_err = 0;
839 	stats->custom_length = 1;
840 	strcpy(stats->custom[0].desc, "eh_abort_cnt");
841 	stats->custom[0].value = conn->eh_abort_cnt;
842 }
843 
844 /**
845  * beiscsi_set_params_for_offld - get the parameters for offload
846  * @beiscsi_conn: pointer to beiscsi_conn
847  * @params: pointer to offload_params structure
848  */
849 static void  beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
850 					  struct beiscsi_offload_params *params)
851 {
852 	struct iscsi_conn *conn = beiscsi_conn->conn;
853 	struct iscsi_session *session = conn->session;
854 
855 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
856 		      params, session->max_burst);
857 	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
858 		      max_send_data_segment_length, params,
859 		      conn->max_xmit_dlength);
860 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
861 		      params, session->first_burst);
862 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
863 		      session->erl);
864 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
865 		      conn->datadgst_en);
866 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
867 		      conn->hdrdgst_en);
868 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
869 		      session->initial_r2t_en);
870 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
871 		      session->imm_data_en);
872 	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
873 		      data_seq_inorder, params,
874 		      session->dataseq_inorder_en);
875 	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
876 		      pdu_seq_inorder, params,
877 		      session->pdu_inorder_en);
878 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_r2t, params,
879 		      session->max_r2t);
880 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
881 		      (conn->exp_statsn - 1));
882 	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
883 		      max_recv_data_segment_length, params,
884 		      conn->max_recv_dlength);
885 
886 }
887 
888 /**
889  * beiscsi_conn_start - offload of session to chip
890  * @cls_conn: pointer to beiscsi_conn
891  */
892 int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
893 {
894 	struct iscsi_conn *conn = cls_conn->dd_data;
895 	struct beiscsi_conn *beiscsi_conn = conn->dd_data;
896 	struct beiscsi_endpoint *beiscsi_ep;
897 	struct beiscsi_offload_params params;
898 	struct beiscsi_hba *phba;
899 
900 	phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
901 
902 	if (!beiscsi_hba_is_online(phba)) {
903 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
904 			    "BS_%d : HBA in error 0x%lx\n", phba->state);
905 		return -EBUSY;
906 	}
907 	beiscsi_log(beiscsi_conn->phba, KERN_INFO, BEISCSI_LOG_CONFIG,
908 		    "BS_%d : In beiscsi_conn_start\n");
909 
910 	memset(&params, 0, sizeof(struct beiscsi_offload_params));
911 	beiscsi_ep = beiscsi_conn->ep;
912 	if (!beiscsi_ep)
913 		beiscsi_log(beiscsi_conn->phba, KERN_ERR,
914 			    BEISCSI_LOG_CONFIG,
915 			    "BS_%d : In beiscsi_conn_start , no beiscsi_ep\n");
916 
917 	beiscsi_conn->login_in_progress = 0;
918 	beiscsi_set_params_for_offld(beiscsi_conn, &params);
919 	beiscsi_offload_connection(beiscsi_conn, &params);
920 	iscsi_conn_start(cls_conn);
921 	return 0;
922 }
923 
924 /**
925  * beiscsi_get_cid - Allocate a cid
926  * @phba: The phba instance
927  */
928 static int beiscsi_get_cid(struct beiscsi_hba *phba)
929 {
930 	uint16_t cid_avlbl_ulp0, cid_avlbl_ulp1;
931 	unsigned short cid, cid_from_ulp;
932 	struct ulp_cid_info *cid_info;
933 
934 	/* Find the ULP which has more CID available */
935 	cid_avlbl_ulp0 = (phba->cid_array_info[BEISCSI_ULP0]) ?
936 			  BEISCSI_ULP0_AVLBL_CID(phba) : 0;
937 	cid_avlbl_ulp1 = (phba->cid_array_info[BEISCSI_ULP1]) ?
938 			  BEISCSI_ULP1_AVLBL_CID(phba) : 0;
939 	cid_from_ulp = (cid_avlbl_ulp0 > cid_avlbl_ulp1) ?
940 			BEISCSI_ULP0 : BEISCSI_ULP1;
941 	/**
942 	 * If iSCSI protocol is loaded only on ULP 0, and when cid_avlbl_ulp
943 	 * is ZERO for both, ULP 1 is returned.
944 	 * Check if ULP is loaded before getting new CID.
945 	 */
946 	if (!test_bit(cid_from_ulp, (void *)&phba->fw_config.ulp_supported))
947 		return BE_INVALID_CID;
948 
949 	cid_info = phba->cid_array_info[cid_from_ulp];
950 	cid = cid_info->cid_array[cid_info->cid_alloc];
951 	if (!cid_info->avlbl_cids || cid == BE_INVALID_CID) {
952 		__beiscsi_log(phba, KERN_ERR,
953 				"BS_%d : failed to get cid: available %u:%u\n",
954 				cid_info->avlbl_cids, cid_info->cid_free);
955 		return BE_INVALID_CID;
956 	}
957 	/* empty the slot */
958 	cid_info->cid_array[cid_info->cid_alloc++] = BE_INVALID_CID;
959 	if (cid_info->cid_alloc == BEISCSI_GET_CID_COUNT(phba, cid_from_ulp))
960 		cid_info->cid_alloc = 0;
961 	cid_info->avlbl_cids--;
962 	return cid;
963 }
964 
965 /**
966  * beiscsi_put_cid - Free the cid
967  * @phba: The phba for which the cid is being freed
968  * @cid: The cid to free
969  */
970 static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
971 {
972 	uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
973 	struct hwi_wrb_context *pwrb_context;
974 	struct hwi_controller *phwi_ctrlr;
975 	struct ulp_cid_info *cid_info;
976 	uint16_t cid_post_ulp;
977 
978 	phwi_ctrlr = phba->phwi_ctrlr;
979 	pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
980 	cid_post_ulp = pwrb_context->ulp_num;
981 
982 	cid_info = phba->cid_array_info[cid_post_ulp];
983 	/* fill only in empty slot */
984 	if (cid_info->cid_array[cid_info->cid_free] != BE_INVALID_CID) {
985 		__beiscsi_log(phba, KERN_ERR,
986 			      "BS_%d : failed to put cid %u: available %u:%u\n",
987 			      cid, cid_info->avlbl_cids, cid_info->cid_free);
988 		return;
989 	}
990 	cid_info->cid_array[cid_info->cid_free++] = cid;
991 	if (cid_info->cid_free == BEISCSI_GET_CID_COUNT(phba, cid_post_ulp))
992 		cid_info->cid_free = 0;
993 	cid_info->avlbl_cids++;
994 }
995 
996 /**
997  * beiscsi_free_ep - free endpoint
998  * @ep:	pointer to iscsi endpoint structure
999  */
1000 static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
1001 {
1002 	struct beiscsi_hba *phba = beiscsi_ep->phba;
1003 	struct beiscsi_conn *beiscsi_conn;
1004 
1005 	beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
1006 	beiscsi_ep->phba = NULL;
1007 	/* clear this to track freeing in beiscsi_ep_disconnect */
1008 	phba->ep_array[BE_GET_CRI_FROM_CID(beiscsi_ep->ep_cid)] = NULL;
1009 
1010 	/**
1011 	 * Check if any connection resource allocated by driver
1012 	 * is to be freed.This case occurs when target redirection
1013 	 * or connection retry is done.
1014 	 **/
1015 	if (!beiscsi_ep->conn)
1016 		return;
1017 
1018 	beiscsi_conn = beiscsi_ep->conn;
1019 	/**
1020 	 * Break ep->conn link here so that completions after
1021 	 * this are ignored.
1022 	 */
1023 	beiscsi_ep->conn = NULL;
1024 	if (beiscsi_conn->login_in_progress) {
1025 		beiscsi_free_mgmt_task_handles(beiscsi_conn,
1026 					       beiscsi_conn->task);
1027 		beiscsi_conn->login_in_progress = 0;
1028 	}
1029 }
1030 
1031 /**
1032  * beiscsi_open_conn - Ask FW to open a TCP connection
1033  * @ep:	endpoint to be used
1034  * @src_addr: The source IP address
1035  * @dst_addr: The Destination  IP address
1036  *
1037  * Asks the FW to open a TCP connection
1038  */
1039 static int beiscsi_open_conn(struct iscsi_endpoint *ep,
1040 			     struct sockaddr *src_addr,
1041 			     struct sockaddr *dst_addr, int non_blocking)
1042 {
1043 	struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
1044 	struct beiscsi_hba *phba = beiscsi_ep->phba;
1045 	struct tcp_connect_and_offload_out *ptcpcnct_out;
1046 	struct be_dma_mem nonemb_cmd;
1047 	unsigned int tag, req_memsize;
1048 	int ret = -ENOMEM;
1049 
1050 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1051 		    "BS_%d : In beiscsi_open_conn\n");
1052 
1053 	beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
1054 	if (beiscsi_ep->ep_cid == BE_INVALID_CID) {
1055 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1056 			    "BS_%d : No free cid available\n");
1057 		return ret;
1058 	}
1059 
1060 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1061 		    "BS_%d : In beiscsi_open_conn, ep_cid=%d\n",
1062 		    beiscsi_ep->ep_cid);
1063 
1064 	phba->ep_array[BE_GET_CRI_FROM_CID
1065 		       (beiscsi_ep->ep_cid)] = ep;
1066 
1067 	beiscsi_ep->cid_vld = 0;
1068 
1069 	if (is_chip_be2_be3r(phba))
1070 		req_memsize = sizeof(struct tcp_connect_and_offload_in);
1071 	else
1072 		req_memsize = sizeof(struct tcp_connect_and_offload_in_v1);
1073 
1074 	nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
1075 				req_memsize,
1076 				&nonemb_cmd.dma);
1077 	if (nonemb_cmd.va == NULL) {
1078 
1079 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1080 			    "BS_%d : Failed to allocate memory for"
1081 			    " mgmt_open_connection\n");
1082 
1083 		beiscsi_free_ep(beiscsi_ep);
1084 		return -ENOMEM;
1085 	}
1086 	nonemb_cmd.size = req_memsize;
1087 	memset(nonemb_cmd.va, 0, nonemb_cmd.size);
1088 	tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep, &nonemb_cmd);
1089 	if (!tag) {
1090 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1091 			    "BS_%d : mgmt_open_connection Failed for cid=%d\n",
1092 			    beiscsi_ep->ep_cid);
1093 
1094 		pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
1095 				    nonemb_cmd.va, nonemb_cmd.dma);
1096 		beiscsi_free_ep(beiscsi_ep);
1097 		return -EAGAIN;
1098 	}
1099 
1100 	ret = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
1101 	if (ret) {
1102 		beiscsi_log(phba, KERN_ERR,
1103 			    BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
1104 			    "BS_%d : mgmt_open_connection Failed");
1105 
1106 		if (ret != -EBUSY)
1107 			pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
1108 					    nonemb_cmd.va, nonemb_cmd.dma);
1109 
1110 		beiscsi_free_ep(beiscsi_ep);
1111 		return ret;
1112 	}
1113 
1114 	ptcpcnct_out = (struct tcp_connect_and_offload_out *)nonemb_cmd.va;
1115 	beiscsi_ep = ep->dd_data;
1116 	beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
1117 	beiscsi_ep->cid_vld = 1;
1118 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1119 		    "BS_%d : mgmt_open_connection Success\n");
1120 
1121 	pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
1122 			    nonemb_cmd.va, nonemb_cmd.dma);
1123 	return 0;
1124 }
1125 
1126 /**
1127  * beiscsi_ep_connect - Ask chip to create TCP Conn
1128  * @scsi_host: Pointer to scsi_host structure
1129  * @dst_addr: The IP address of Target
1130  * @non_blocking: blocking or non-blocking call
1131  *
1132  * This routines first asks chip to create a connection and then allocates an EP
1133  */
1134 struct iscsi_endpoint *
1135 beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
1136 		   int non_blocking)
1137 {
1138 	struct beiscsi_hba *phba;
1139 	struct beiscsi_endpoint *beiscsi_ep;
1140 	struct iscsi_endpoint *ep;
1141 	int ret;
1142 
1143 	if (!shost) {
1144 		ret = -ENXIO;
1145 		pr_err("beiscsi_ep_connect shost is NULL\n");
1146 		return ERR_PTR(ret);
1147 	}
1148 
1149 	phba = iscsi_host_priv(shost);
1150 	if (!beiscsi_hba_is_online(phba)) {
1151 		ret = -EIO;
1152 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1153 			    "BS_%d : HBA in error 0x%lx\n", phba->state);
1154 		return ERR_PTR(ret);
1155 	}
1156 	if (!test_bit(BEISCSI_HBA_LINK_UP, &phba->state)) {
1157 		ret = -EBUSY;
1158 		beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1159 			    "BS_%d : The Adapter Port state is Down!!!\n");
1160 		return ERR_PTR(ret);
1161 	}
1162 
1163 	ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
1164 	if (!ep) {
1165 		ret = -ENOMEM;
1166 		return ERR_PTR(ret);
1167 	}
1168 
1169 	beiscsi_ep = ep->dd_data;
1170 	beiscsi_ep->phba = phba;
1171 	beiscsi_ep->openiscsi_ep = ep;
1172 	ret = beiscsi_open_conn(ep, NULL, dst_addr, non_blocking);
1173 	if (ret) {
1174 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1175 			    "BS_%d : Failed in beiscsi_open_conn\n");
1176 		goto free_ep;
1177 	}
1178 
1179 	return ep;
1180 
1181 free_ep:
1182 	iscsi_destroy_endpoint(ep);
1183 	return ERR_PTR(ret);
1184 }
1185 
1186 /**
1187  * beiscsi_ep_poll - Poll to see if connection is established
1188  * @ep:	endpoint to be used
1189  * @timeout_ms: timeout specified in millisecs
1190  *
1191  * Poll to see if TCP connection established
1192  */
1193 int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1194 {
1195 	struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
1196 
1197 	beiscsi_log(beiscsi_ep->phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1198 		    "BS_%d : In  beiscsi_ep_poll\n");
1199 
1200 	if (beiscsi_ep->cid_vld == 1)
1201 		return 1;
1202 	else
1203 		return 0;
1204 }
1205 
1206 /**
1207  * beiscsi_flush_cq()- Flush the CQ created.
1208  * @phba: ptr device priv structure.
1209  *
1210  * Before the connection resource are freed flush
1211  * all the CQ enteries
1212  **/
1213 static void beiscsi_flush_cq(struct beiscsi_hba *phba)
1214 {
1215 	uint16_t i;
1216 	struct be_eq_obj *pbe_eq;
1217 	struct hwi_controller *phwi_ctrlr;
1218 	struct hwi_context_memory *phwi_context;
1219 
1220 	phwi_ctrlr = phba->phwi_ctrlr;
1221 	phwi_context = phwi_ctrlr->phwi_ctxt;
1222 
1223 	for (i = 0; i < phba->num_cpus; i++) {
1224 		pbe_eq = &phwi_context->be_eq[i];
1225 		irq_poll_disable(&pbe_eq->iopoll);
1226 		beiscsi_process_cq(pbe_eq, BE2_MAX_NUM_CQ_PROC);
1227 		irq_poll_enable(&pbe_eq->iopoll);
1228 	}
1229 }
1230 
1231 /**
1232  * beiscsi_conn_close - Invalidate and upload connection
1233  * @ep: The iscsi endpoint
1234  *
1235  * Returns 0 on success,  -1 on failure.
1236  */
1237 static int beiscsi_conn_close(struct beiscsi_endpoint *beiscsi_ep)
1238 {
1239 	struct beiscsi_hba *phba = beiscsi_ep->phba;
1240 	unsigned int tag, attempts;
1241 	int ret;
1242 
1243 	/**
1244 	 * Without successfully invalidating and uploading connection
1245 	 * driver can't reuse the CID so attempt more than once.
1246 	 */
1247 	attempts = 0;
1248 	while (attempts++ < 3) {
1249 		tag = beiscsi_invalidate_cxn(phba, beiscsi_ep);
1250 		if (tag) {
1251 			ret = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
1252 			if (!ret)
1253 				break;
1254 			beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1255 				    "BS_%d : invalidate conn failed cid %d\n",
1256 				    beiscsi_ep->ep_cid);
1257 		}
1258 	}
1259 
1260 	/* wait for all completions to arrive, then process them */
1261 	msleep(250);
1262 	/* flush CQ entries */
1263 	beiscsi_flush_cq(phba);
1264 
1265 	if (attempts > 3)
1266 		return -1;
1267 
1268 	attempts = 0;
1269 	while (attempts++ < 3) {
1270 		tag = beiscsi_upload_cxn(phba, beiscsi_ep);
1271 		if (tag) {
1272 			ret = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
1273 			if (!ret)
1274 				break;
1275 			beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1276 				    "BS_%d : upload conn failed cid %d\n",
1277 				    beiscsi_ep->ep_cid);
1278 		}
1279 	}
1280 	if (attempts > 3)
1281 		return -1;
1282 
1283 	return 0;
1284 }
1285 
1286 /**
1287  * beiscsi_ep_disconnect - Tears down the TCP connection
1288  * @ep:	endpoint to be used
1289  *
1290  * Tears down the TCP connection
1291  */
1292 void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
1293 {
1294 	struct beiscsi_endpoint *beiscsi_ep;
1295 	struct beiscsi_conn *beiscsi_conn;
1296 	struct beiscsi_hba *phba;
1297 	uint16_t cri_index;
1298 
1299 	beiscsi_ep = ep->dd_data;
1300 	phba = beiscsi_ep->phba;
1301 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1302 		    "BS_%d : In beiscsi_ep_disconnect for ep_cid = %u\n",
1303 		    beiscsi_ep->ep_cid);
1304 
1305 	cri_index = BE_GET_CRI_FROM_CID(beiscsi_ep->ep_cid);
1306 	if (!phba->ep_array[cri_index]) {
1307 		__beiscsi_log(phba, KERN_ERR,
1308 			      "BS_%d : ep_array at %u cid %u empty\n",
1309 			      cri_index,
1310 			      beiscsi_ep->ep_cid);
1311 		return;
1312 	}
1313 
1314 	if (beiscsi_ep->conn) {
1315 		beiscsi_conn = beiscsi_ep->conn;
1316 		iscsi_suspend_queue(beiscsi_conn->conn);
1317 	}
1318 
1319 	if (!beiscsi_hba_is_online(phba)) {
1320 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1321 			    "BS_%d : HBA in error 0x%lx\n", phba->state);
1322 	} else {
1323 		/**
1324 		 * Make CID available even if close fails.
1325 		 * If not freed, FW might fail open using the CID.
1326 		 */
1327 		if (beiscsi_conn_close(beiscsi_ep) < 0)
1328 			__beiscsi_log(phba, KERN_ERR,
1329 				      "BS_%d : close conn failed cid %d\n",
1330 				      beiscsi_ep->ep_cid);
1331 	}
1332 
1333 	beiscsi_free_ep(beiscsi_ep);
1334 	if (!phba->conn_table[cri_index])
1335 		__beiscsi_log(phba, KERN_ERR,
1336 			      "BS_%d : conn_table empty at %u: cid %u\n",
1337 			      cri_index, beiscsi_ep->ep_cid);
1338 	phba->conn_table[cri_index] = NULL;
1339 	iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
1340 }
1341 
1342 umode_t beiscsi_attr_is_visible(int param_type, int param)
1343 {
1344 	switch (param_type) {
1345 	case ISCSI_NET_PARAM:
1346 		switch (param) {
1347 		case ISCSI_NET_PARAM_IFACE_ENABLE:
1348 		case ISCSI_NET_PARAM_IPV4_ADDR:
1349 		case ISCSI_NET_PARAM_IPV4_SUBNET:
1350 		case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1351 		case ISCSI_NET_PARAM_IPV4_GW:
1352 		case ISCSI_NET_PARAM_IPV6_ADDR:
1353 		case ISCSI_NET_PARAM_VLAN_ID:
1354 		case ISCSI_NET_PARAM_VLAN_PRIORITY:
1355 		case ISCSI_NET_PARAM_VLAN_ENABLED:
1356 			return S_IRUGO;
1357 		default:
1358 			return 0;
1359 		}
1360 	case ISCSI_HOST_PARAM:
1361 		switch (param) {
1362 		case ISCSI_HOST_PARAM_HWADDRESS:
1363 		case ISCSI_HOST_PARAM_INITIATOR_NAME:
1364 		case ISCSI_HOST_PARAM_PORT_STATE:
1365 		case ISCSI_HOST_PARAM_PORT_SPEED:
1366 			return S_IRUGO;
1367 		default:
1368 			return 0;
1369 		}
1370 	case ISCSI_PARAM:
1371 		switch (param) {
1372 		case ISCSI_PARAM_MAX_RECV_DLENGTH:
1373 		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1374 		case ISCSI_PARAM_HDRDGST_EN:
1375 		case ISCSI_PARAM_DATADGST_EN:
1376 		case ISCSI_PARAM_CONN_ADDRESS:
1377 		case ISCSI_PARAM_CONN_PORT:
1378 		case ISCSI_PARAM_EXP_STATSN:
1379 		case ISCSI_PARAM_PERSISTENT_ADDRESS:
1380 		case ISCSI_PARAM_PERSISTENT_PORT:
1381 		case ISCSI_PARAM_PING_TMO:
1382 		case ISCSI_PARAM_RECV_TMO:
1383 		case ISCSI_PARAM_INITIAL_R2T_EN:
1384 		case ISCSI_PARAM_MAX_R2T:
1385 		case ISCSI_PARAM_IMM_DATA_EN:
1386 		case ISCSI_PARAM_FIRST_BURST:
1387 		case ISCSI_PARAM_MAX_BURST:
1388 		case ISCSI_PARAM_PDU_INORDER_EN:
1389 		case ISCSI_PARAM_DATASEQ_INORDER_EN:
1390 		case ISCSI_PARAM_ERL:
1391 		case ISCSI_PARAM_TARGET_NAME:
1392 		case ISCSI_PARAM_TPGT:
1393 		case ISCSI_PARAM_USERNAME:
1394 		case ISCSI_PARAM_PASSWORD:
1395 		case ISCSI_PARAM_USERNAME_IN:
1396 		case ISCSI_PARAM_PASSWORD_IN:
1397 		case ISCSI_PARAM_FAST_ABORT:
1398 		case ISCSI_PARAM_ABORT_TMO:
1399 		case ISCSI_PARAM_LU_RESET_TMO:
1400 		case ISCSI_PARAM_IFACE_NAME:
1401 		case ISCSI_PARAM_INITIATOR_NAME:
1402 			return S_IRUGO;
1403 		default:
1404 			return 0;
1405 		}
1406 	}
1407 
1408 	return 0;
1409 }
1410