18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * This file is part of the Emulex Linux Device Driver for Enterprise iSCSI
48c2ecf20Sopenharmony_ci * Host Bus Adapters. Refer to the README file included with this package
58c2ecf20Sopenharmony_ci * for driver version and adapter compatibility.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (c) 2018 Broadcom. All Rights Reserved.
88c2ecf20Sopenharmony_ci * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Contact Information:
118c2ecf20Sopenharmony_ci * linux-drivers@broadcom.com
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <scsi/libiscsi.h>
158c2ecf20Sopenharmony_ci#include <scsi/scsi_transport_iscsi.h>
168c2ecf20Sopenharmony_ci#include <scsi/scsi_transport.h>
178c2ecf20Sopenharmony_ci#include <scsi/scsi_cmnd.h>
188c2ecf20Sopenharmony_ci#include <scsi/scsi_device.h>
198c2ecf20Sopenharmony_ci#include <scsi/scsi_host.h>
208c2ecf20Sopenharmony_ci#include <scsi/scsi_netlink.h>
218c2ecf20Sopenharmony_ci#include <net/netlink.h>
228c2ecf20Sopenharmony_ci#include <scsi/scsi.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "be_iscsi.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciextern struct iscsi_transport beiscsi_iscsi_transport;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/**
298c2ecf20Sopenharmony_ci * beiscsi_session_create - creates a new iscsi session
308c2ecf20Sopenharmony_ci * @ep: pointer to iscsi ep
318c2ecf20Sopenharmony_ci * @cmds_max: max commands supported
328c2ecf20Sopenharmony_ci * @qdepth: max queue depth supported
338c2ecf20Sopenharmony_ci * @initial_cmdsn: initial iscsi CMDSN
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_cistruct iscsi_cls_session *beiscsi_session_create(struct iscsi_endpoint *ep,
368c2ecf20Sopenharmony_ci						 u16 cmds_max,
378c2ecf20Sopenharmony_ci						 u16 qdepth,
388c2ecf20Sopenharmony_ci						 u32 initial_cmdsn)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct Scsi_Host *shost;
418c2ecf20Sopenharmony_ci	struct beiscsi_endpoint *beiscsi_ep;
428c2ecf20Sopenharmony_ci	struct iscsi_cls_session *cls_session;
438c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba;
448c2ecf20Sopenharmony_ci	struct iscsi_session *sess;
458c2ecf20Sopenharmony_ci	struct beiscsi_session *beiscsi_sess;
468c2ecf20Sopenharmony_ci	struct beiscsi_io_task *io_task;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	if (!ep) {
508c2ecf20Sopenharmony_ci		pr_err("beiscsi_session_create: invalid ep\n");
518c2ecf20Sopenharmony_ci		return NULL;
528c2ecf20Sopenharmony_ci	}
538c2ecf20Sopenharmony_ci	beiscsi_ep = ep->dd_data;
548c2ecf20Sopenharmony_ci	phba = beiscsi_ep->phba;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	if (!beiscsi_hba_is_online(phba)) {
578c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
588c2ecf20Sopenharmony_ci			    "BS_%d : HBA in error 0x%lx\n", phba->state);
598c2ecf20Sopenharmony_ci		return NULL;
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
638c2ecf20Sopenharmony_ci		    "BS_%d : In beiscsi_session_create\n");
648c2ecf20Sopenharmony_ci	if (cmds_max > beiscsi_ep->phba->params.wrbs_per_cxn) {
658c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
668c2ecf20Sopenharmony_ci			    "BS_%d : Cannot handle %d cmds."
678c2ecf20Sopenharmony_ci			    "Max cmds per session supported is %d. Using %d."
688c2ecf20Sopenharmony_ci			    "\n", cmds_max,
698c2ecf20Sopenharmony_ci			    beiscsi_ep->phba->params.wrbs_per_cxn,
708c2ecf20Sopenharmony_ci			    beiscsi_ep->phba->params.wrbs_per_cxn);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci		cmds_max = beiscsi_ep->phba->params.wrbs_per_cxn;
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	shost = phba->shost;
768c2ecf20Sopenharmony_ci	cls_session = iscsi_session_setup(&beiscsi_iscsi_transport,
778c2ecf20Sopenharmony_ci					  shost, cmds_max,
788c2ecf20Sopenharmony_ci					  sizeof(*beiscsi_sess),
798c2ecf20Sopenharmony_ci					  sizeof(*io_task),
808c2ecf20Sopenharmony_ci					  initial_cmdsn, ISCSI_MAX_TARGET);
818c2ecf20Sopenharmony_ci	if (!cls_session)
828c2ecf20Sopenharmony_ci		return NULL;
838c2ecf20Sopenharmony_ci	sess = cls_session->dd_data;
848c2ecf20Sopenharmony_ci	beiscsi_sess = sess->dd_data;
858c2ecf20Sopenharmony_ci	beiscsi_sess->bhs_pool =  dma_pool_create("beiscsi_bhs_pool",
868c2ecf20Sopenharmony_ci						   &phba->pcidev->dev,
878c2ecf20Sopenharmony_ci						   sizeof(struct be_cmd_bhs),
888c2ecf20Sopenharmony_ci						   64, 0);
898c2ecf20Sopenharmony_ci	if (!beiscsi_sess->bhs_pool)
908c2ecf20Sopenharmony_ci		goto destroy_sess;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	return cls_session;
938c2ecf20Sopenharmony_cidestroy_sess:
948c2ecf20Sopenharmony_ci	iscsi_session_teardown(cls_session);
958c2ecf20Sopenharmony_ci	return NULL;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/**
998c2ecf20Sopenharmony_ci * beiscsi_session_destroy - destroys iscsi session
1008c2ecf20Sopenharmony_ci * @cls_session:	pointer to iscsi cls session
1018c2ecf20Sopenharmony_ci *
1028c2ecf20Sopenharmony_ci * Destroys iSCSI session instance and releases
1038c2ecf20Sopenharmony_ci * resources allocated for it.
1048c2ecf20Sopenharmony_ci */
1058c2ecf20Sopenharmony_civoid beiscsi_session_destroy(struct iscsi_cls_session *cls_session)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct iscsi_session *sess = cls_session->dd_data;
1088c2ecf20Sopenharmony_ci	struct beiscsi_session *beiscsi_sess = sess->dd_data;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	printk(KERN_INFO "In beiscsi_session_destroy\n");
1118c2ecf20Sopenharmony_ci	dma_pool_destroy(beiscsi_sess->bhs_pool);
1128c2ecf20Sopenharmony_ci	iscsi_session_teardown(cls_session);
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/**
1168c2ecf20Sopenharmony_ci * beiscsi_session_fail(): Closing session with appropriate error
1178c2ecf20Sopenharmony_ci * @cls_session: ptr to session
1188c2ecf20Sopenharmony_ci **/
1198c2ecf20Sopenharmony_civoid beiscsi_session_fail(struct iscsi_cls_session *cls_session)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/**
1268c2ecf20Sopenharmony_ci * beiscsi_conn_create - create an instance of iscsi connection
1278c2ecf20Sopenharmony_ci * @cls_session: ptr to iscsi_cls_session
1288c2ecf20Sopenharmony_ci * @cid: iscsi cid
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_cistruct iscsi_cls_conn *
1318c2ecf20Sopenharmony_cibeiscsi_conn_create(struct iscsi_cls_session *cls_session, u32 cid)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba;
1348c2ecf20Sopenharmony_ci	struct Scsi_Host *shost;
1358c2ecf20Sopenharmony_ci	struct iscsi_cls_conn *cls_conn;
1368c2ecf20Sopenharmony_ci	struct beiscsi_conn *beiscsi_conn;
1378c2ecf20Sopenharmony_ci	struct iscsi_conn *conn;
1388c2ecf20Sopenharmony_ci	struct iscsi_session *sess;
1398c2ecf20Sopenharmony_ci	struct beiscsi_session *beiscsi_sess;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	shost = iscsi_session_to_shost(cls_session);
1428c2ecf20Sopenharmony_ci	phba = iscsi_host_priv(shost);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1458c2ecf20Sopenharmony_ci		    "BS_%d : In beiscsi_conn_create ,cid"
1468c2ecf20Sopenharmony_ci		    "from iscsi layer=%d\n", cid);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	cls_conn = iscsi_conn_setup(cls_session, sizeof(*beiscsi_conn), cid);
1498c2ecf20Sopenharmony_ci	if (!cls_conn)
1508c2ecf20Sopenharmony_ci		return NULL;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	conn = cls_conn->dd_data;
1538c2ecf20Sopenharmony_ci	beiscsi_conn = conn->dd_data;
1548c2ecf20Sopenharmony_ci	beiscsi_conn->ep = NULL;
1558c2ecf20Sopenharmony_ci	beiscsi_conn->phba = phba;
1568c2ecf20Sopenharmony_ci	beiscsi_conn->conn = conn;
1578c2ecf20Sopenharmony_ci	sess = cls_session->dd_data;
1588c2ecf20Sopenharmony_ci	beiscsi_sess = sess->dd_data;
1598c2ecf20Sopenharmony_ci	beiscsi_conn->beiscsi_sess = beiscsi_sess;
1608c2ecf20Sopenharmony_ci	return cls_conn;
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/**
1648c2ecf20Sopenharmony_ci * beiscsi_conn_bind - Binds iscsi session/connection with TCP connection
1658c2ecf20Sopenharmony_ci * @cls_session: pointer to iscsi cls session
1668c2ecf20Sopenharmony_ci * @cls_conn: pointer to iscsi cls conn
1678c2ecf20Sopenharmony_ci * @transport_fd: EP handle(64 bit)
1688c2ecf20Sopenharmony_ci * @is_leading: indicate if this is the session leading connection (MCS)
1698c2ecf20Sopenharmony_ci *
1708c2ecf20Sopenharmony_ci * This function binds the TCP Conn with iSCSI Connection and Session.
1718c2ecf20Sopenharmony_ci */
1728c2ecf20Sopenharmony_ciint beiscsi_conn_bind(struct iscsi_cls_session *cls_session,
1738c2ecf20Sopenharmony_ci		      struct iscsi_cls_conn *cls_conn,
1748c2ecf20Sopenharmony_ci		      u64 transport_fd, int is_leading)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	struct iscsi_conn *conn = cls_conn->dd_data;
1778c2ecf20Sopenharmony_ci	struct beiscsi_conn *beiscsi_conn = conn->dd_data;
1788c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1798c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = iscsi_host_priv(shost);
1808c2ecf20Sopenharmony_ci	struct hwi_controller *phwi_ctrlr = phba->phwi_ctrlr;
1818c2ecf20Sopenharmony_ci	struct hwi_wrb_context *pwrb_context;
1828c2ecf20Sopenharmony_ci	struct beiscsi_endpoint *beiscsi_ep;
1838c2ecf20Sopenharmony_ci	struct iscsi_endpoint *ep;
1848c2ecf20Sopenharmony_ci	uint16_t cri_index;
1858c2ecf20Sopenharmony_ci	int rc = 0;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	ep = iscsi_lookup_endpoint(transport_fd);
1888c2ecf20Sopenharmony_ci	if (!ep)
1898c2ecf20Sopenharmony_ci		return -EINVAL;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	beiscsi_ep = ep->dd_data;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (iscsi_conn_bind(cls_session, cls_conn, is_leading)) {
1948c2ecf20Sopenharmony_ci		rc = -EINVAL;
1958c2ecf20Sopenharmony_ci		goto put_ep;
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	if (beiscsi_ep->phba != phba) {
1998c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
2008c2ecf20Sopenharmony_ci			    "BS_%d : beiscsi_ep->hba=%p not equal to phba=%p\n",
2018c2ecf20Sopenharmony_ci			    beiscsi_ep->phba, phba);
2028c2ecf20Sopenharmony_ci		rc = -EEXIST;
2038c2ecf20Sopenharmony_ci		goto put_ep;
2048c2ecf20Sopenharmony_ci	}
2058c2ecf20Sopenharmony_ci	cri_index = BE_GET_CRI_FROM_CID(beiscsi_ep->ep_cid);
2068c2ecf20Sopenharmony_ci	if (phba->conn_table[cri_index]) {
2078c2ecf20Sopenharmony_ci		if (beiscsi_conn != phba->conn_table[cri_index] ||
2088c2ecf20Sopenharmony_ci		    beiscsi_ep != phba->conn_table[cri_index]->ep) {
2098c2ecf20Sopenharmony_ci			__beiscsi_log(phba, KERN_ERR,
2108c2ecf20Sopenharmony_ci				      "BS_%d : conn_table not empty at %u: cid %u conn %p:%p\n",
2118c2ecf20Sopenharmony_ci				      cri_index,
2128c2ecf20Sopenharmony_ci				      beiscsi_ep->ep_cid,
2138c2ecf20Sopenharmony_ci				      beiscsi_conn,
2148c2ecf20Sopenharmony_ci				      phba->conn_table[cri_index]);
2158c2ecf20Sopenharmony_ci			rc = -EINVAL;
2168c2ecf20Sopenharmony_ci			goto put_ep;
2178c2ecf20Sopenharmony_ci		}
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	beiscsi_conn->beiscsi_conn_cid = beiscsi_ep->ep_cid;
2218c2ecf20Sopenharmony_ci	beiscsi_conn->ep = beiscsi_ep;
2228c2ecf20Sopenharmony_ci	beiscsi_ep->conn = beiscsi_conn;
2238c2ecf20Sopenharmony_ci	/**
2248c2ecf20Sopenharmony_ci	 * Each connection is associated with a WRBQ kept in wrb_context.
2258c2ecf20Sopenharmony_ci	 * Store doorbell offset for transmit path.
2268c2ecf20Sopenharmony_ci	 */
2278c2ecf20Sopenharmony_ci	pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
2288c2ecf20Sopenharmony_ci	beiscsi_conn->doorbell_offset = pwrb_context->doorbell_offset;
2298c2ecf20Sopenharmony_ci	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
2308c2ecf20Sopenharmony_ci		    "BS_%d : cid %d phba->conn_table[%u]=%p\n",
2318c2ecf20Sopenharmony_ci		    beiscsi_ep->ep_cid, cri_index, beiscsi_conn);
2328c2ecf20Sopenharmony_ci	phba->conn_table[cri_index] = beiscsi_conn;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ciput_ep:
2358c2ecf20Sopenharmony_ci	iscsi_put_endpoint(ep);
2368c2ecf20Sopenharmony_ci	return rc;
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic int beiscsi_iface_create_ipv4(struct beiscsi_hba *phba)
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci	if (phba->ipv4_iface)
2428c2ecf20Sopenharmony_ci		return 0;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	phba->ipv4_iface = iscsi_create_iface(phba->shost,
2458c2ecf20Sopenharmony_ci					      &beiscsi_iscsi_transport,
2468c2ecf20Sopenharmony_ci					      ISCSI_IFACE_TYPE_IPV4,
2478c2ecf20Sopenharmony_ci					      0, 0);
2488c2ecf20Sopenharmony_ci	if (!phba->ipv4_iface) {
2498c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
2508c2ecf20Sopenharmony_ci			    "BS_%d : Could not "
2518c2ecf20Sopenharmony_ci			    "create default IPv4 address.\n");
2528c2ecf20Sopenharmony_ci		return -ENODEV;
2538c2ecf20Sopenharmony_ci	}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	return 0;
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistatic int beiscsi_iface_create_ipv6(struct beiscsi_hba *phba)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	if (phba->ipv6_iface)
2618c2ecf20Sopenharmony_ci		return 0;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	phba->ipv6_iface = iscsi_create_iface(phba->shost,
2648c2ecf20Sopenharmony_ci					      &beiscsi_iscsi_transport,
2658c2ecf20Sopenharmony_ci					      ISCSI_IFACE_TYPE_IPV6,
2668c2ecf20Sopenharmony_ci					      0, 0);
2678c2ecf20Sopenharmony_ci	if (!phba->ipv6_iface) {
2688c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
2698c2ecf20Sopenharmony_ci			    "BS_%d : Could not "
2708c2ecf20Sopenharmony_ci			    "create default IPv6 address.\n");
2718c2ecf20Sopenharmony_ci		return -ENODEV;
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	return 0;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_civoid beiscsi_iface_create_default(struct beiscsi_hba *phba)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	struct be_cmd_get_if_info_resp *if_info;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	if (!beiscsi_if_get_info(phba, BEISCSI_IP_TYPE_V4, &if_info)) {
2828c2ecf20Sopenharmony_ci		beiscsi_iface_create_ipv4(phba);
2838c2ecf20Sopenharmony_ci		kfree(if_info);
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	if (!beiscsi_if_get_info(phba, BEISCSI_IP_TYPE_V6, &if_info)) {
2878c2ecf20Sopenharmony_ci		beiscsi_iface_create_ipv6(phba);
2888c2ecf20Sopenharmony_ci		kfree(if_info);
2898c2ecf20Sopenharmony_ci	}
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_civoid beiscsi_iface_destroy_default(struct beiscsi_hba *phba)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	if (phba->ipv6_iface) {
2958c2ecf20Sopenharmony_ci		iscsi_destroy_iface(phba->ipv6_iface);
2968c2ecf20Sopenharmony_ci		phba->ipv6_iface = NULL;
2978c2ecf20Sopenharmony_ci	}
2988c2ecf20Sopenharmony_ci	if (phba->ipv4_iface) {
2998c2ecf20Sopenharmony_ci		iscsi_destroy_iface(phba->ipv4_iface);
3008c2ecf20Sopenharmony_ci		phba->ipv4_iface = NULL;
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci/**
3058c2ecf20Sopenharmony_ci * beiscsi_set_vlan_tag()- Set the VLAN TAG
3068c2ecf20Sopenharmony_ci * @shost: Scsi Host for the driver instance
3078c2ecf20Sopenharmony_ci * @iface_param: Interface paramters
3088c2ecf20Sopenharmony_ci *
3098c2ecf20Sopenharmony_ci * Set the VLAN TAG for the adapter or disable
3108c2ecf20Sopenharmony_ci * the VLAN config
3118c2ecf20Sopenharmony_ci *
3128c2ecf20Sopenharmony_ci * returns
3138c2ecf20Sopenharmony_ci *	Success: 0
3148c2ecf20Sopenharmony_ci *	Failure: Non-Zero Value
3158c2ecf20Sopenharmony_ci **/
3168c2ecf20Sopenharmony_cistatic int
3178c2ecf20Sopenharmony_cibeiscsi_iface_config_vlan(struct Scsi_Host *shost,
3188c2ecf20Sopenharmony_ci			  struct iscsi_iface_param_info *iface_param)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = iscsi_host_priv(shost);
3218c2ecf20Sopenharmony_ci	int ret = -EPERM;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	switch (iface_param->param) {
3248c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_VLAN_ENABLED:
3258c2ecf20Sopenharmony_ci		ret = 0;
3268c2ecf20Sopenharmony_ci		if (iface_param->value[0] != ISCSI_VLAN_ENABLE)
3278c2ecf20Sopenharmony_ci			ret = beiscsi_if_set_vlan(phba, BEISCSI_VLAN_DISABLE);
3288c2ecf20Sopenharmony_ci		break;
3298c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_VLAN_TAG:
3308c2ecf20Sopenharmony_ci		ret = beiscsi_if_set_vlan(phba,
3318c2ecf20Sopenharmony_ci					  *((uint16_t *)iface_param->value));
3328c2ecf20Sopenharmony_ci		break;
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci	return ret;
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic int
3398c2ecf20Sopenharmony_cibeiscsi_iface_config_ipv4(struct Scsi_Host *shost,
3408c2ecf20Sopenharmony_ci			  struct iscsi_iface_param_info *info,
3418c2ecf20Sopenharmony_ci			  void *data, uint32_t dt_len)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = iscsi_host_priv(shost);
3448c2ecf20Sopenharmony_ci	u8 *ip = NULL, *subnet = NULL, *gw;
3458c2ecf20Sopenharmony_ci	struct nlattr *nla;
3468c2ecf20Sopenharmony_ci	int ret = -EPERM;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	/* Check the param */
3498c2ecf20Sopenharmony_ci	switch (info->param) {
3508c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IFACE_ENABLE:
3518c2ecf20Sopenharmony_ci		if (info->value[0] == ISCSI_IFACE_ENABLE)
3528c2ecf20Sopenharmony_ci			ret = beiscsi_iface_create_ipv4(phba);
3538c2ecf20Sopenharmony_ci		else {
3548c2ecf20Sopenharmony_ci			iscsi_destroy_iface(phba->ipv4_iface);
3558c2ecf20Sopenharmony_ci			phba->ipv4_iface = NULL;
3568c2ecf20Sopenharmony_ci		}
3578c2ecf20Sopenharmony_ci		break;
3588c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV4_GW:
3598c2ecf20Sopenharmony_ci		gw = info->value;
3608c2ecf20Sopenharmony_ci		ret = beiscsi_if_set_gw(phba, BEISCSI_IP_TYPE_V4, gw);
3618c2ecf20Sopenharmony_ci		break;
3628c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
3638c2ecf20Sopenharmony_ci		if (info->value[0] == ISCSI_BOOTPROTO_DHCP)
3648c2ecf20Sopenharmony_ci			ret = beiscsi_if_en_dhcp(phba, BEISCSI_IP_TYPE_V4);
3658c2ecf20Sopenharmony_ci		else if (info->value[0] == ISCSI_BOOTPROTO_STATIC)
3668c2ecf20Sopenharmony_ci			/* release DHCP IP address */
3678c2ecf20Sopenharmony_ci			ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V4,
3688c2ecf20Sopenharmony_ci						   NULL, NULL);
3698c2ecf20Sopenharmony_ci		else
3708c2ecf20Sopenharmony_ci			beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
3718c2ecf20Sopenharmony_ci				    "BS_%d : Invalid BOOTPROTO: %d\n",
3728c2ecf20Sopenharmony_ci				    info->value[0]);
3738c2ecf20Sopenharmony_ci		break;
3748c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV4_ADDR:
3758c2ecf20Sopenharmony_ci		ip = info->value;
3768c2ecf20Sopenharmony_ci		nla = nla_find(data, dt_len, ISCSI_NET_PARAM_IPV4_SUBNET);
3778c2ecf20Sopenharmony_ci		if (nla) {
3788c2ecf20Sopenharmony_ci			info = nla_data(nla);
3798c2ecf20Sopenharmony_ci			subnet = info->value;
3808c2ecf20Sopenharmony_ci		}
3818c2ecf20Sopenharmony_ci		ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V4,
3828c2ecf20Sopenharmony_ci					   ip, subnet);
3838c2ecf20Sopenharmony_ci		break;
3848c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV4_SUBNET:
3858c2ecf20Sopenharmony_ci		/*
3868c2ecf20Sopenharmony_ci		 * OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR ioctl needs IP
3878c2ecf20Sopenharmony_ci		 * and subnet both. Find IP to be applied for this subnet.
3888c2ecf20Sopenharmony_ci		 */
3898c2ecf20Sopenharmony_ci		subnet = info->value;
3908c2ecf20Sopenharmony_ci		nla = nla_find(data, dt_len, ISCSI_NET_PARAM_IPV4_ADDR);
3918c2ecf20Sopenharmony_ci		if (nla) {
3928c2ecf20Sopenharmony_ci			info = nla_data(nla);
3938c2ecf20Sopenharmony_ci			ip = info->value;
3948c2ecf20Sopenharmony_ci		}
3958c2ecf20Sopenharmony_ci		ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V4,
3968c2ecf20Sopenharmony_ci					   ip, subnet);
3978c2ecf20Sopenharmony_ci		break;
3988c2ecf20Sopenharmony_ci	}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	return ret;
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic int
4048c2ecf20Sopenharmony_cibeiscsi_iface_config_ipv6(struct Scsi_Host *shost,
4058c2ecf20Sopenharmony_ci			  struct iscsi_iface_param_info *iface_param,
4068c2ecf20Sopenharmony_ci			  void *data, uint32_t dt_len)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = iscsi_host_priv(shost);
4098c2ecf20Sopenharmony_ci	int ret = -EPERM;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	switch (iface_param->param) {
4128c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IFACE_ENABLE:
4138c2ecf20Sopenharmony_ci		if (iface_param->value[0] == ISCSI_IFACE_ENABLE)
4148c2ecf20Sopenharmony_ci			ret = beiscsi_iface_create_ipv6(phba);
4158c2ecf20Sopenharmony_ci		else {
4168c2ecf20Sopenharmony_ci			iscsi_destroy_iface(phba->ipv6_iface);
4178c2ecf20Sopenharmony_ci			phba->ipv6_iface = NULL;
4188c2ecf20Sopenharmony_ci		}
4198c2ecf20Sopenharmony_ci		break;
4208c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV6_ADDR:
4218c2ecf20Sopenharmony_ci		ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V6,
4228c2ecf20Sopenharmony_ci					   iface_param->value, NULL);
4238c2ecf20Sopenharmony_ci		break;
4248c2ecf20Sopenharmony_ci	}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	return ret;
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ciint beiscsi_iface_set_param(struct Scsi_Host *shost,
4308c2ecf20Sopenharmony_ci			    void *data, uint32_t dt_len)
4318c2ecf20Sopenharmony_ci{
4328c2ecf20Sopenharmony_ci	struct iscsi_iface_param_info *iface_param = NULL;
4338c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = iscsi_host_priv(shost);
4348c2ecf20Sopenharmony_ci	struct nlattr *attrib;
4358c2ecf20Sopenharmony_ci	uint32_t rm_len = dt_len;
4368c2ecf20Sopenharmony_ci	int ret;
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	if (!beiscsi_hba_is_online(phba)) {
4398c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
4408c2ecf20Sopenharmony_ci			    "BS_%d : HBA in error 0x%lx\n", phba->state);
4418c2ecf20Sopenharmony_ci		return -EBUSY;
4428c2ecf20Sopenharmony_ci	}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	/* update interface_handle */
4458c2ecf20Sopenharmony_ci	ret = beiscsi_if_get_handle(phba);
4468c2ecf20Sopenharmony_ci	if (ret) {
4478c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4488c2ecf20Sopenharmony_ci			    "BS_%d : Getting Interface Handle Failed\n");
4498c2ecf20Sopenharmony_ci		return ret;
4508c2ecf20Sopenharmony_ci	}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	nla_for_each_attr(attrib, data, dt_len, rm_len) {
4538c2ecf20Sopenharmony_ci		/* ignore nla_type as it is never used */
4548c2ecf20Sopenharmony_ci		if (nla_len(attrib) < sizeof(*iface_param))
4558c2ecf20Sopenharmony_ci			return -EINVAL;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci		iface_param = nla_data(attrib);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci		if (iface_param->param_type != ISCSI_NET_PARAM)
4608c2ecf20Sopenharmony_ci			continue;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci		/*
4638c2ecf20Sopenharmony_ci		 * BE2ISCSI only supports 1 interface
4648c2ecf20Sopenharmony_ci		 */
4658c2ecf20Sopenharmony_ci		if (iface_param->iface_num) {
4668c2ecf20Sopenharmony_ci			beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4678c2ecf20Sopenharmony_ci				    "BS_%d : Invalid iface_num %d."
4688c2ecf20Sopenharmony_ci				    "Only iface_num 0 is supported.\n",
4698c2ecf20Sopenharmony_ci				    iface_param->iface_num);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci			return -EINVAL;
4728c2ecf20Sopenharmony_ci		}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
4758c2ecf20Sopenharmony_ci			    "BS_%d : %s.0 set param %d",
4768c2ecf20Sopenharmony_ci			    (iface_param->iface_type == ISCSI_IFACE_TYPE_IPV4) ?
4778c2ecf20Sopenharmony_ci			    "ipv4" : "ipv6", iface_param->param);
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci		ret = -EPERM;
4808c2ecf20Sopenharmony_ci		switch (iface_param->param) {
4818c2ecf20Sopenharmony_ci		case ISCSI_NET_PARAM_VLAN_ENABLED:
4828c2ecf20Sopenharmony_ci		case ISCSI_NET_PARAM_VLAN_TAG:
4838c2ecf20Sopenharmony_ci			ret = beiscsi_iface_config_vlan(shost, iface_param);
4848c2ecf20Sopenharmony_ci			break;
4858c2ecf20Sopenharmony_ci		default:
4868c2ecf20Sopenharmony_ci			switch (iface_param->iface_type) {
4878c2ecf20Sopenharmony_ci			case ISCSI_IFACE_TYPE_IPV4:
4888c2ecf20Sopenharmony_ci				ret = beiscsi_iface_config_ipv4(shost,
4898c2ecf20Sopenharmony_ci								iface_param,
4908c2ecf20Sopenharmony_ci								data, dt_len);
4918c2ecf20Sopenharmony_ci				break;
4928c2ecf20Sopenharmony_ci			case ISCSI_IFACE_TYPE_IPV6:
4938c2ecf20Sopenharmony_ci				ret = beiscsi_iface_config_ipv6(shost,
4948c2ecf20Sopenharmony_ci								iface_param,
4958c2ecf20Sopenharmony_ci								data, dt_len);
4968c2ecf20Sopenharmony_ci				break;
4978c2ecf20Sopenharmony_ci			}
4988c2ecf20Sopenharmony_ci		}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci		if (ret == -EPERM) {
5018c2ecf20Sopenharmony_ci			__beiscsi_log(phba, KERN_ERR,
5028c2ecf20Sopenharmony_ci				      "BS_%d : %s.0 set param %d not permitted",
5038c2ecf20Sopenharmony_ci				      (iface_param->iface_type ==
5048c2ecf20Sopenharmony_ci				       ISCSI_IFACE_TYPE_IPV4) ? "ipv4" : "ipv6",
5058c2ecf20Sopenharmony_ci				      iface_param->param);
5068c2ecf20Sopenharmony_ci			ret = 0;
5078c2ecf20Sopenharmony_ci		}
5088c2ecf20Sopenharmony_ci		if (ret)
5098c2ecf20Sopenharmony_ci			break;
5108c2ecf20Sopenharmony_ci	}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	return ret;
5138c2ecf20Sopenharmony_ci}
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_cistatic int __beiscsi_iface_get_param(struct beiscsi_hba *phba,
5168c2ecf20Sopenharmony_ci				     struct iscsi_iface *iface,
5178c2ecf20Sopenharmony_ci				     int param, char *buf)
5188c2ecf20Sopenharmony_ci{
5198c2ecf20Sopenharmony_ci	struct be_cmd_get_if_info_resp *if_info;
5208c2ecf20Sopenharmony_ci	int len, ip_type = BEISCSI_IP_TYPE_V4;
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
5238c2ecf20Sopenharmony_ci		ip_type = BEISCSI_IP_TYPE_V6;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	len = beiscsi_if_get_info(phba, ip_type, &if_info);
5268c2ecf20Sopenharmony_ci	if (len)
5278c2ecf20Sopenharmony_ci		return len;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	switch (param) {
5308c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV4_ADDR:
5318c2ecf20Sopenharmony_ci		len = sprintf(buf, "%pI4\n", if_info->ip_addr.addr);
5328c2ecf20Sopenharmony_ci		break;
5338c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV6_ADDR:
5348c2ecf20Sopenharmony_ci		len = sprintf(buf, "%pI6\n", if_info->ip_addr.addr);
5358c2ecf20Sopenharmony_ci		break;
5368c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
5378c2ecf20Sopenharmony_ci		if (!if_info->dhcp_state)
5388c2ecf20Sopenharmony_ci			len = sprintf(buf, "static\n");
5398c2ecf20Sopenharmony_ci		else
5408c2ecf20Sopenharmony_ci			len = sprintf(buf, "dhcp\n");
5418c2ecf20Sopenharmony_ci		break;
5428c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV4_SUBNET:
5438c2ecf20Sopenharmony_ci		len = sprintf(buf, "%pI4\n", if_info->ip_addr.subnet_mask);
5448c2ecf20Sopenharmony_ci		break;
5458c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_VLAN_ENABLED:
5468c2ecf20Sopenharmony_ci		len = sprintf(buf, "%s\n",
5478c2ecf20Sopenharmony_ci			      (if_info->vlan_priority == BEISCSI_VLAN_DISABLE) ?
5488c2ecf20Sopenharmony_ci			      "disable" : "enable");
5498c2ecf20Sopenharmony_ci		break;
5508c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_VLAN_ID:
5518c2ecf20Sopenharmony_ci		if (if_info->vlan_priority == BEISCSI_VLAN_DISABLE)
5528c2ecf20Sopenharmony_ci			len = -EINVAL;
5538c2ecf20Sopenharmony_ci		else
5548c2ecf20Sopenharmony_ci			len = sprintf(buf, "%d\n",
5558c2ecf20Sopenharmony_ci				      (if_info->vlan_priority &
5568c2ecf20Sopenharmony_ci				       ISCSI_MAX_VLAN_ID));
5578c2ecf20Sopenharmony_ci		break;
5588c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_VLAN_PRIORITY:
5598c2ecf20Sopenharmony_ci		if (if_info->vlan_priority == BEISCSI_VLAN_DISABLE)
5608c2ecf20Sopenharmony_ci			len = -EINVAL;
5618c2ecf20Sopenharmony_ci		else
5628c2ecf20Sopenharmony_ci			len = sprintf(buf, "%d\n",
5638c2ecf20Sopenharmony_ci				      ((if_info->vlan_priority >> 13) &
5648c2ecf20Sopenharmony_ci				       ISCSI_MAX_VLAN_PRIORITY));
5658c2ecf20Sopenharmony_ci		break;
5668c2ecf20Sopenharmony_ci	default:
5678c2ecf20Sopenharmony_ci		WARN_ON(1);
5688c2ecf20Sopenharmony_ci	}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	kfree(if_info);
5718c2ecf20Sopenharmony_ci	return len;
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ciint beiscsi_iface_get_param(struct iscsi_iface *iface,
5758c2ecf20Sopenharmony_ci			    enum iscsi_param_type param_type,
5768c2ecf20Sopenharmony_ci			    int param, char *buf)
5778c2ecf20Sopenharmony_ci{
5788c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
5798c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = iscsi_host_priv(shost);
5808c2ecf20Sopenharmony_ci	struct be_cmd_get_def_gateway_resp gateway;
5818c2ecf20Sopenharmony_ci	int len = -EPERM;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	if (param_type != ISCSI_NET_PARAM)
5848c2ecf20Sopenharmony_ci		return 0;
5858c2ecf20Sopenharmony_ci	if (!beiscsi_hba_is_online(phba)) {
5868c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
5878c2ecf20Sopenharmony_ci			    "BS_%d : HBA in error 0x%lx\n", phba->state);
5888c2ecf20Sopenharmony_ci		return -EBUSY;
5898c2ecf20Sopenharmony_ci	}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	switch (param) {
5928c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV4_ADDR:
5938c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV4_SUBNET:
5948c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
5958c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV6_ADDR:
5968c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_VLAN_ENABLED:
5978c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_VLAN_ID:
5988c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_VLAN_PRIORITY:
5998c2ecf20Sopenharmony_ci		len = __beiscsi_iface_get_param(phba, iface, param, buf);
6008c2ecf20Sopenharmony_ci		break;
6018c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IFACE_ENABLE:
6028c2ecf20Sopenharmony_ci		if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
6038c2ecf20Sopenharmony_ci			len = sprintf(buf, "%s\n",
6048c2ecf20Sopenharmony_ci				      phba->ipv4_iface ? "enable" : "disable");
6058c2ecf20Sopenharmony_ci		else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
6068c2ecf20Sopenharmony_ci			len = sprintf(buf, "%s\n",
6078c2ecf20Sopenharmony_ci				      phba->ipv6_iface ? "enable" : "disable");
6088c2ecf20Sopenharmony_ci		break;
6098c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM_IPV4_GW:
6108c2ecf20Sopenharmony_ci		memset(&gateway, 0, sizeof(gateway));
6118c2ecf20Sopenharmony_ci		len = beiscsi_if_get_gw(phba, BEISCSI_IP_TYPE_V4, &gateway);
6128c2ecf20Sopenharmony_ci		if (!len)
6138c2ecf20Sopenharmony_ci			len = sprintf(buf, "%pI4\n", &gateway.ip_addr.addr);
6148c2ecf20Sopenharmony_ci		break;
6158c2ecf20Sopenharmony_ci	}
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	return len;
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci/**
6218c2ecf20Sopenharmony_ci * beiscsi_ep_get_param - get the iscsi parameter
6228c2ecf20Sopenharmony_ci * @ep: pointer to iscsi ep
6238c2ecf20Sopenharmony_ci * @param: parameter type identifier
6248c2ecf20Sopenharmony_ci * @buf: buffer pointer
6258c2ecf20Sopenharmony_ci *
6268c2ecf20Sopenharmony_ci * returns iscsi parameter
6278c2ecf20Sopenharmony_ci */
6288c2ecf20Sopenharmony_ciint beiscsi_ep_get_param(struct iscsi_endpoint *ep,
6298c2ecf20Sopenharmony_ci			   enum iscsi_param param, char *buf)
6308c2ecf20Sopenharmony_ci{
6318c2ecf20Sopenharmony_ci	struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
6328c2ecf20Sopenharmony_ci	int len;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	beiscsi_log(beiscsi_ep->phba, KERN_INFO,
6358c2ecf20Sopenharmony_ci		    BEISCSI_LOG_CONFIG,
6368c2ecf20Sopenharmony_ci		    "BS_%d : In beiscsi_ep_get_param,"
6378c2ecf20Sopenharmony_ci		    " param= %d\n", param);
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	switch (param) {
6408c2ecf20Sopenharmony_ci	case ISCSI_PARAM_CONN_PORT:
6418c2ecf20Sopenharmony_ci		len = sprintf(buf, "%hu\n", beiscsi_ep->dst_tcpport);
6428c2ecf20Sopenharmony_ci		break;
6438c2ecf20Sopenharmony_ci	case ISCSI_PARAM_CONN_ADDRESS:
6448c2ecf20Sopenharmony_ci		if (beiscsi_ep->ip_type == BEISCSI_IP_TYPE_V4)
6458c2ecf20Sopenharmony_ci			len = sprintf(buf, "%pI4\n", &beiscsi_ep->dst_addr);
6468c2ecf20Sopenharmony_ci		else
6478c2ecf20Sopenharmony_ci			len = sprintf(buf, "%pI6\n", &beiscsi_ep->dst6_addr);
6488c2ecf20Sopenharmony_ci		break;
6498c2ecf20Sopenharmony_ci	default:
6508c2ecf20Sopenharmony_ci		len = -EPERM;
6518c2ecf20Sopenharmony_ci	}
6528c2ecf20Sopenharmony_ci	return len;
6538c2ecf20Sopenharmony_ci}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ciint beiscsi_set_param(struct iscsi_cls_conn *cls_conn,
6568c2ecf20Sopenharmony_ci		      enum iscsi_param param, char *buf, int buflen)
6578c2ecf20Sopenharmony_ci{
6588c2ecf20Sopenharmony_ci	struct iscsi_conn *conn = cls_conn->dd_data;
6598c2ecf20Sopenharmony_ci	struct iscsi_session *session = conn->session;
6608c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = NULL;
6618c2ecf20Sopenharmony_ci	int ret;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
6648c2ecf20Sopenharmony_ci	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
6658c2ecf20Sopenharmony_ci		    "BS_%d : In beiscsi_conn_set_param,"
6668c2ecf20Sopenharmony_ci		    " param= %d\n", param);
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	ret = iscsi_set_param(cls_conn, param, buf, buflen);
6698c2ecf20Sopenharmony_ci	if (ret)
6708c2ecf20Sopenharmony_ci		return ret;
6718c2ecf20Sopenharmony_ci	/*
6728c2ecf20Sopenharmony_ci	 * If userspace tried to set the value to higher than we can
6738c2ecf20Sopenharmony_ci	 * support override here.
6748c2ecf20Sopenharmony_ci	 */
6758c2ecf20Sopenharmony_ci	switch (param) {
6768c2ecf20Sopenharmony_ci	case ISCSI_PARAM_FIRST_BURST:
6778c2ecf20Sopenharmony_ci		if (session->first_burst > 8192)
6788c2ecf20Sopenharmony_ci			session->first_burst = 8192;
6798c2ecf20Sopenharmony_ci		break;
6808c2ecf20Sopenharmony_ci	case ISCSI_PARAM_MAX_RECV_DLENGTH:
6818c2ecf20Sopenharmony_ci		if (conn->max_recv_dlength > 65536)
6828c2ecf20Sopenharmony_ci			conn->max_recv_dlength = 65536;
6838c2ecf20Sopenharmony_ci		break;
6848c2ecf20Sopenharmony_ci	case ISCSI_PARAM_MAX_BURST:
6858c2ecf20Sopenharmony_ci		if (session->max_burst > 262144)
6868c2ecf20Sopenharmony_ci			session->max_burst = 262144;
6878c2ecf20Sopenharmony_ci		break;
6888c2ecf20Sopenharmony_ci	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
6898c2ecf20Sopenharmony_ci		if (conn->max_xmit_dlength > 65536)
6908c2ecf20Sopenharmony_ci			conn->max_xmit_dlength = 65536;
6918c2ecf20Sopenharmony_ci		fallthrough;
6928c2ecf20Sopenharmony_ci	default:
6938c2ecf20Sopenharmony_ci		return 0;
6948c2ecf20Sopenharmony_ci	}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	return 0;
6978c2ecf20Sopenharmony_ci}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci/**
7008c2ecf20Sopenharmony_ci * beiscsi_get_port_state - Get the Port State
7018c2ecf20Sopenharmony_ci * @shost : pointer to scsi_host structure
7028c2ecf20Sopenharmony_ci *
7038c2ecf20Sopenharmony_ci */
7048c2ecf20Sopenharmony_cistatic void beiscsi_get_port_state(struct Scsi_Host *shost)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = iscsi_host_priv(shost);
7078c2ecf20Sopenharmony_ci	struct iscsi_cls_host *ihost = shost->shost_data;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	ihost->port_state = test_bit(BEISCSI_HBA_LINK_UP, &phba->state) ?
7108c2ecf20Sopenharmony_ci		ISCSI_PORT_STATE_UP : ISCSI_PORT_STATE_DOWN;
7118c2ecf20Sopenharmony_ci}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci/**
7148c2ecf20Sopenharmony_ci * beiscsi_get_port_speed  - Get the Port Speed from Adapter
7158c2ecf20Sopenharmony_ci * @shost : pointer to scsi_host structure
7168c2ecf20Sopenharmony_ci *
7178c2ecf20Sopenharmony_ci */
7188c2ecf20Sopenharmony_cistatic void beiscsi_get_port_speed(struct Scsi_Host *shost)
7198c2ecf20Sopenharmony_ci{
7208c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = iscsi_host_priv(shost);
7218c2ecf20Sopenharmony_ci	struct iscsi_cls_host *ihost = shost->shost_data;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	switch (phba->port_speed) {
7248c2ecf20Sopenharmony_ci	case BE2ISCSI_LINK_SPEED_10MBPS:
7258c2ecf20Sopenharmony_ci		ihost->port_speed = ISCSI_PORT_SPEED_10MBPS;
7268c2ecf20Sopenharmony_ci		break;
7278c2ecf20Sopenharmony_ci	case BE2ISCSI_LINK_SPEED_100MBPS:
7288c2ecf20Sopenharmony_ci		ihost->port_speed = ISCSI_PORT_SPEED_100MBPS;
7298c2ecf20Sopenharmony_ci		break;
7308c2ecf20Sopenharmony_ci	case BE2ISCSI_LINK_SPEED_1GBPS:
7318c2ecf20Sopenharmony_ci		ihost->port_speed = ISCSI_PORT_SPEED_1GBPS;
7328c2ecf20Sopenharmony_ci		break;
7338c2ecf20Sopenharmony_ci	case BE2ISCSI_LINK_SPEED_10GBPS:
7348c2ecf20Sopenharmony_ci		ihost->port_speed = ISCSI_PORT_SPEED_10GBPS;
7358c2ecf20Sopenharmony_ci		break;
7368c2ecf20Sopenharmony_ci	case BE2ISCSI_LINK_SPEED_25GBPS:
7378c2ecf20Sopenharmony_ci		ihost->port_speed = ISCSI_PORT_SPEED_25GBPS;
7388c2ecf20Sopenharmony_ci		break;
7398c2ecf20Sopenharmony_ci	case BE2ISCSI_LINK_SPEED_40GBPS:
7408c2ecf20Sopenharmony_ci		ihost->port_speed = ISCSI_PORT_SPEED_40GBPS;
7418c2ecf20Sopenharmony_ci		break;
7428c2ecf20Sopenharmony_ci	default:
7438c2ecf20Sopenharmony_ci		ihost->port_speed = ISCSI_PORT_SPEED_UNKNOWN;
7448c2ecf20Sopenharmony_ci	}
7458c2ecf20Sopenharmony_ci}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci/**
7488c2ecf20Sopenharmony_ci * beiscsi_get_host_param - get the iscsi parameter
7498c2ecf20Sopenharmony_ci * @shost: pointer to scsi_host structure
7508c2ecf20Sopenharmony_ci * @param: parameter type identifier
7518c2ecf20Sopenharmony_ci * @buf: buffer pointer
7528c2ecf20Sopenharmony_ci *
7538c2ecf20Sopenharmony_ci */
7548c2ecf20Sopenharmony_ciint beiscsi_get_host_param(struct Scsi_Host *shost,
7558c2ecf20Sopenharmony_ci			   enum iscsi_host_param param, char *buf)
7568c2ecf20Sopenharmony_ci{
7578c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = iscsi_host_priv(shost);
7588c2ecf20Sopenharmony_ci	int status = 0;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	if (!beiscsi_hba_is_online(phba)) {
7618c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
7628c2ecf20Sopenharmony_ci			    "BS_%d : HBA in error 0x%lx\n", phba->state);
7638c2ecf20Sopenharmony_ci		return 0;
7648c2ecf20Sopenharmony_ci	}
7658c2ecf20Sopenharmony_ci	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
7668c2ecf20Sopenharmony_ci		    "BS_%d : In beiscsi_get_host_param, param = %d\n", param);
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	switch (param) {
7698c2ecf20Sopenharmony_ci	case ISCSI_HOST_PARAM_HWADDRESS:
7708c2ecf20Sopenharmony_ci		status = beiscsi_get_macaddr(buf, phba);
7718c2ecf20Sopenharmony_ci		if (status < 0) {
7728c2ecf20Sopenharmony_ci			beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
7738c2ecf20Sopenharmony_ci				    "BS_%d : beiscsi_get_macaddr Failed\n");
7748c2ecf20Sopenharmony_ci			return 0;
7758c2ecf20Sopenharmony_ci		}
7768c2ecf20Sopenharmony_ci		break;
7778c2ecf20Sopenharmony_ci	case ISCSI_HOST_PARAM_INITIATOR_NAME:
7788c2ecf20Sopenharmony_ci		/* try fetching user configured name first */
7798c2ecf20Sopenharmony_ci		status = beiscsi_get_initiator_name(phba, buf, true);
7808c2ecf20Sopenharmony_ci		if (status < 0) {
7818c2ecf20Sopenharmony_ci			status = beiscsi_get_initiator_name(phba, buf, false);
7828c2ecf20Sopenharmony_ci			if (status < 0) {
7838c2ecf20Sopenharmony_ci				beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
7848c2ecf20Sopenharmony_ci					    "BS_%d : Retrieving Initiator Name Failed\n");
7858c2ecf20Sopenharmony_ci				status = 0;
7868c2ecf20Sopenharmony_ci			}
7878c2ecf20Sopenharmony_ci		}
7888c2ecf20Sopenharmony_ci		break;
7898c2ecf20Sopenharmony_ci	case ISCSI_HOST_PARAM_PORT_STATE:
7908c2ecf20Sopenharmony_ci		beiscsi_get_port_state(shost);
7918c2ecf20Sopenharmony_ci		status = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
7928c2ecf20Sopenharmony_ci		break;
7938c2ecf20Sopenharmony_ci	case ISCSI_HOST_PARAM_PORT_SPEED:
7948c2ecf20Sopenharmony_ci		beiscsi_get_port_speed(shost);
7958c2ecf20Sopenharmony_ci		status = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
7968c2ecf20Sopenharmony_ci		break;
7978c2ecf20Sopenharmony_ci	default:
7988c2ecf20Sopenharmony_ci		return iscsi_host_get_param(shost, param, buf);
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci	return status;
8018c2ecf20Sopenharmony_ci}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ciint beiscsi_get_macaddr(char *buf, struct beiscsi_hba *phba)
8048c2ecf20Sopenharmony_ci{
8058c2ecf20Sopenharmony_ci	struct be_cmd_get_nic_conf_resp resp;
8068c2ecf20Sopenharmony_ci	int rc;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	if (phba->mac_addr_set)
8098c2ecf20Sopenharmony_ci		return sysfs_format_mac(buf, phba->mac_address, ETH_ALEN);
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	memset(&resp, 0, sizeof(resp));
8128c2ecf20Sopenharmony_ci	rc = mgmt_get_nic_conf(phba, &resp);
8138c2ecf20Sopenharmony_ci	if (rc)
8148c2ecf20Sopenharmony_ci		return rc;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	phba->mac_addr_set = true;
8178c2ecf20Sopenharmony_ci	memcpy(phba->mac_address, resp.mac_address, ETH_ALEN);
8188c2ecf20Sopenharmony_ci	return sysfs_format_mac(buf, phba->mac_address, ETH_ALEN);
8198c2ecf20Sopenharmony_ci}
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci/**
8228c2ecf20Sopenharmony_ci * beiscsi_conn_get_stats - get the iscsi stats
8238c2ecf20Sopenharmony_ci * @cls_conn: pointer to iscsi cls conn
8248c2ecf20Sopenharmony_ci * @stats: pointer to iscsi_stats structure
8258c2ecf20Sopenharmony_ci *
8268c2ecf20Sopenharmony_ci * returns iscsi stats
8278c2ecf20Sopenharmony_ci */
8288c2ecf20Sopenharmony_civoid beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
8298c2ecf20Sopenharmony_ci			    struct iscsi_stats *stats)
8308c2ecf20Sopenharmony_ci{
8318c2ecf20Sopenharmony_ci	struct iscsi_conn *conn = cls_conn->dd_data;
8328c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = NULL;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
8358c2ecf20Sopenharmony_ci	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
8368c2ecf20Sopenharmony_ci		    "BS_%d : In beiscsi_conn_get_stats\n");
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	stats->txdata_octets = conn->txdata_octets;
8398c2ecf20Sopenharmony_ci	stats->rxdata_octets = conn->rxdata_octets;
8408c2ecf20Sopenharmony_ci	stats->dataout_pdus = conn->dataout_pdus_cnt;
8418c2ecf20Sopenharmony_ci	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
8428c2ecf20Sopenharmony_ci	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
8438c2ecf20Sopenharmony_ci	stats->datain_pdus = conn->datain_pdus_cnt;
8448c2ecf20Sopenharmony_ci	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
8458c2ecf20Sopenharmony_ci	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
8468c2ecf20Sopenharmony_ci	stats->r2t_pdus = conn->r2t_pdus_cnt;
8478c2ecf20Sopenharmony_ci	stats->digest_err = 0;
8488c2ecf20Sopenharmony_ci	stats->timeout_err = 0;
8498c2ecf20Sopenharmony_ci	stats->custom_length = 1;
8508c2ecf20Sopenharmony_ci	strcpy(stats->custom[0].desc, "eh_abort_cnt");
8518c2ecf20Sopenharmony_ci	stats->custom[0].value = conn->eh_abort_cnt;
8528c2ecf20Sopenharmony_ci}
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci/**
8558c2ecf20Sopenharmony_ci * beiscsi_set_params_for_offld - get the parameters for offload
8568c2ecf20Sopenharmony_ci * @beiscsi_conn: pointer to beiscsi_conn
8578c2ecf20Sopenharmony_ci * @params: pointer to offload_params structure
8588c2ecf20Sopenharmony_ci */
8598c2ecf20Sopenharmony_cistatic void  beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
8608c2ecf20Sopenharmony_ci					  struct beiscsi_offload_params *params)
8618c2ecf20Sopenharmony_ci{
8628c2ecf20Sopenharmony_ci	struct iscsi_conn *conn = beiscsi_conn->conn;
8638c2ecf20Sopenharmony_ci	struct iscsi_session *session = conn->session;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
8668c2ecf20Sopenharmony_ci		      params, session->max_burst);
8678c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
8688c2ecf20Sopenharmony_ci		      max_send_data_segment_length, params,
8698c2ecf20Sopenharmony_ci		      conn->max_xmit_dlength);
8708c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
8718c2ecf20Sopenharmony_ci		      params, session->first_burst);
8728c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
8738c2ecf20Sopenharmony_ci		      session->erl);
8748c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
8758c2ecf20Sopenharmony_ci		      conn->datadgst_en);
8768c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
8778c2ecf20Sopenharmony_ci		      conn->hdrdgst_en);
8788c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
8798c2ecf20Sopenharmony_ci		      session->initial_r2t_en);
8808c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
8818c2ecf20Sopenharmony_ci		      session->imm_data_en);
8828c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
8838c2ecf20Sopenharmony_ci		      data_seq_inorder, params,
8848c2ecf20Sopenharmony_ci		      session->dataseq_inorder_en);
8858c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
8868c2ecf20Sopenharmony_ci		      pdu_seq_inorder, params,
8878c2ecf20Sopenharmony_ci		      session->pdu_inorder_en);
8888c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_r2t, params,
8898c2ecf20Sopenharmony_ci		      session->max_r2t);
8908c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
8918c2ecf20Sopenharmony_ci		      (conn->exp_statsn - 1));
8928c2ecf20Sopenharmony_ci	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
8938c2ecf20Sopenharmony_ci		      max_recv_data_segment_length, params,
8948c2ecf20Sopenharmony_ci		      conn->max_recv_dlength);
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci}
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci/**
8998c2ecf20Sopenharmony_ci * beiscsi_conn_start - offload of session to chip
9008c2ecf20Sopenharmony_ci * @cls_conn: pointer to beiscsi_conn
9018c2ecf20Sopenharmony_ci */
9028c2ecf20Sopenharmony_ciint beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
9038c2ecf20Sopenharmony_ci{
9048c2ecf20Sopenharmony_ci	struct iscsi_conn *conn = cls_conn->dd_data;
9058c2ecf20Sopenharmony_ci	struct beiscsi_conn *beiscsi_conn = conn->dd_data;
9068c2ecf20Sopenharmony_ci	struct beiscsi_endpoint *beiscsi_ep;
9078c2ecf20Sopenharmony_ci	struct beiscsi_offload_params params;
9088c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba;
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	if (!beiscsi_hba_is_online(phba)) {
9138c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
9148c2ecf20Sopenharmony_ci			    "BS_%d : HBA in error 0x%lx\n", phba->state);
9158c2ecf20Sopenharmony_ci		return -EBUSY;
9168c2ecf20Sopenharmony_ci	}
9178c2ecf20Sopenharmony_ci	beiscsi_log(beiscsi_conn->phba, KERN_INFO, BEISCSI_LOG_CONFIG,
9188c2ecf20Sopenharmony_ci		    "BS_%d : In beiscsi_conn_start\n");
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	memset(&params, 0, sizeof(struct beiscsi_offload_params));
9218c2ecf20Sopenharmony_ci	beiscsi_ep = beiscsi_conn->ep;
9228c2ecf20Sopenharmony_ci	if (!beiscsi_ep)
9238c2ecf20Sopenharmony_ci		beiscsi_log(beiscsi_conn->phba, KERN_ERR,
9248c2ecf20Sopenharmony_ci			    BEISCSI_LOG_CONFIG,
9258c2ecf20Sopenharmony_ci			    "BS_%d : In beiscsi_conn_start , no beiscsi_ep\n");
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	beiscsi_conn->login_in_progress = 0;
9288c2ecf20Sopenharmony_ci	beiscsi_set_params_for_offld(beiscsi_conn, &params);
9298c2ecf20Sopenharmony_ci	beiscsi_offload_connection(beiscsi_conn, &params);
9308c2ecf20Sopenharmony_ci	iscsi_conn_start(cls_conn);
9318c2ecf20Sopenharmony_ci	return 0;
9328c2ecf20Sopenharmony_ci}
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci/**
9358c2ecf20Sopenharmony_ci * beiscsi_get_cid - Allocate a cid
9368c2ecf20Sopenharmony_ci * @phba: The phba instance
9378c2ecf20Sopenharmony_ci */
9388c2ecf20Sopenharmony_cistatic int beiscsi_get_cid(struct beiscsi_hba *phba)
9398c2ecf20Sopenharmony_ci{
9408c2ecf20Sopenharmony_ci	uint16_t cid_avlbl_ulp0, cid_avlbl_ulp1;
9418c2ecf20Sopenharmony_ci	unsigned short cid, cid_from_ulp;
9428c2ecf20Sopenharmony_ci	struct ulp_cid_info *cid_info;
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	/* Find the ULP which has more CID available */
9458c2ecf20Sopenharmony_ci	cid_avlbl_ulp0 = (phba->cid_array_info[BEISCSI_ULP0]) ?
9468c2ecf20Sopenharmony_ci			  BEISCSI_ULP0_AVLBL_CID(phba) : 0;
9478c2ecf20Sopenharmony_ci	cid_avlbl_ulp1 = (phba->cid_array_info[BEISCSI_ULP1]) ?
9488c2ecf20Sopenharmony_ci			  BEISCSI_ULP1_AVLBL_CID(phba) : 0;
9498c2ecf20Sopenharmony_ci	cid_from_ulp = (cid_avlbl_ulp0 > cid_avlbl_ulp1) ?
9508c2ecf20Sopenharmony_ci			BEISCSI_ULP0 : BEISCSI_ULP1;
9518c2ecf20Sopenharmony_ci	/**
9528c2ecf20Sopenharmony_ci	 * If iSCSI protocol is loaded only on ULP 0, and when cid_avlbl_ulp
9538c2ecf20Sopenharmony_ci	 * is ZERO for both, ULP 1 is returned.
9548c2ecf20Sopenharmony_ci	 * Check if ULP is loaded before getting new CID.
9558c2ecf20Sopenharmony_ci	 */
9568c2ecf20Sopenharmony_ci	if (!test_bit(cid_from_ulp, (void *)&phba->fw_config.ulp_supported))
9578c2ecf20Sopenharmony_ci		return BE_INVALID_CID;
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	cid_info = phba->cid_array_info[cid_from_ulp];
9608c2ecf20Sopenharmony_ci	cid = cid_info->cid_array[cid_info->cid_alloc];
9618c2ecf20Sopenharmony_ci	if (!cid_info->avlbl_cids || cid == BE_INVALID_CID) {
9628c2ecf20Sopenharmony_ci		__beiscsi_log(phba, KERN_ERR,
9638c2ecf20Sopenharmony_ci				"BS_%d : failed to get cid: available %u:%u\n",
9648c2ecf20Sopenharmony_ci				cid_info->avlbl_cids, cid_info->cid_free);
9658c2ecf20Sopenharmony_ci		return BE_INVALID_CID;
9668c2ecf20Sopenharmony_ci	}
9678c2ecf20Sopenharmony_ci	/* empty the slot */
9688c2ecf20Sopenharmony_ci	cid_info->cid_array[cid_info->cid_alloc++] = BE_INVALID_CID;
9698c2ecf20Sopenharmony_ci	if (cid_info->cid_alloc == BEISCSI_GET_CID_COUNT(phba, cid_from_ulp))
9708c2ecf20Sopenharmony_ci		cid_info->cid_alloc = 0;
9718c2ecf20Sopenharmony_ci	cid_info->avlbl_cids--;
9728c2ecf20Sopenharmony_ci	return cid;
9738c2ecf20Sopenharmony_ci}
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci/**
9768c2ecf20Sopenharmony_ci * beiscsi_put_cid - Free the cid
9778c2ecf20Sopenharmony_ci * @phba: The phba for which the cid is being freed
9788c2ecf20Sopenharmony_ci * @cid: The cid to free
9798c2ecf20Sopenharmony_ci */
9808c2ecf20Sopenharmony_cistatic void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
9818c2ecf20Sopenharmony_ci{
9828c2ecf20Sopenharmony_ci	uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
9838c2ecf20Sopenharmony_ci	struct hwi_wrb_context *pwrb_context;
9848c2ecf20Sopenharmony_ci	struct hwi_controller *phwi_ctrlr;
9858c2ecf20Sopenharmony_ci	struct ulp_cid_info *cid_info;
9868c2ecf20Sopenharmony_ci	uint16_t cid_post_ulp;
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	phwi_ctrlr = phba->phwi_ctrlr;
9898c2ecf20Sopenharmony_ci	pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
9908c2ecf20Sopenharmony_ci	cid_post_ulp = pwrb_context->ulp_num;
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	cid_info = phba->cid_array_info[cid_post_ulp];
9938c2ecf20Sopenharmony_ci	/* fill only in empty slot */
9948c2ecf20Sopenharmony_ci	if (cid_info->cid_array[cid_info->cid_free] != BE_INVALID_CID) {
9958c2ecf20Sopenharmony_ci		__beiscsi_log(phba, KERN_ERR,
9968c2ecf20Sopenharmony_ci			      "BS_%d : failed to put cid %u: available %u:%u\n",
9978c2ecf20Sopenharmony_ci			      cid, cid_info->avlbl_cids, cid_info->cid_free);
9988c2ecf20Sopenharmony_ci		return;
9998c2ecf20Sopenharmony_ci	}
10008c2ecf20Sopenharmony_ci	cid_info->cid_array[cid_info->cid_free++] = cid;
10018c2ecf20Sopenharmony_ci	if (cid_info->cid_free == BEISCSI_GET_CID_COUNT(phba, cid_post_ulp))
10028c2ecf20Sopenharmony_ci		cid_info->cid_free = 0;
10038c2ecf20Sopenharmony_ci	cid_info->avlbl_cids++;
10048c2ecf20Sopenharmony_ci}
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci/**
10078c2ecf20Sopenharmony_ci * beiscsi_free_ep - free endpoint
10088c2ecf20Sopenharmony_ci * @beiscsi_ep: pointer to device endpoint struct
10098c2ecf20Sopenharmony_ci */
10108c2ecf20Sopenharmony_cistatic void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
10118c2ecf20Sopenharmony_ci{
10128c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = beiscsi_ep->phba;
10138c2ecf20Sopenharmony_ci	struct beiscsi_conn *beiscsi_conn;
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
10168c2ecf20Sopenharmony_ci	beiscsi_ep->phba = NULL;
10178c2ecf20Sopenharmony_ci	/* clear this to track freeing in beiscsi_ep_disconnect */
10188c2ecf20Sopenharmony_ci	phba->ep_array[BE_GET_CRI_FROM_CID(beiscsi_ep->ep_cid)] = NULL;
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	/**
10218c2ecf20Sopenharmony_ci	 * Check if any connection resource allocated by driver
10228c2ecf20Sopenharmony_ci	 * is to be freed.This case occurs when target redirection
10238c2ecf20Sopenharmony_ci	 * or connection retry is done.
10248c2ecf20Sopenharmony_ci	 **/
10258c2ecf20Sopenharmony_ci	if (!beiscsi_ep->conn)
10268c2ecf20Sopenharmony_ci		return;
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	beiscsi_conn = beiscsi_ep->conn;
10298c2ecf20Sopenharmony_ci	/**
10308c2ecf20Sopenharmony_ci	 * Break ep->conn link here so that completions after
10318c2ecf20Sopenharmony_ci	 * this are ignored.
10328c2ecf20Sopenharmony_ci	 */
10338c2ecf20Sopenharmony_ci	beiscsi_ep->conn = NULL;
10348c2ecf20Sopenharmony_ci	if (beiscsi_conn->login_in_progress) {
10358c2ecf20Sopenharmony_ci		beiscsi_free_mgmt_task_handles(beiscsi_conn,
10368c2ecf20Sopenharmony_ci					       beiscsi_conn->task);
10378c2ecf20Sopenharmony_ci		beiscsi_conn->login_in_progress = 0;
10388c2ecf20Sopenharmony_ci	}
10398c2ecf20Sopenharmony_ci}
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci/**
10428c2ecf20Sopenharmony_ci * beiscsi_open_conn - Ask FW to open a TCP connection
10438c2ecf20Sopenharmony_ci * @ep: pointer to device endpoint struct
10448c2ecf20Sopenharmony_ci * @src_addr: The source IP address
10458c2ecf20Sopenharmony_ci * @dst_addr: The Destination  IP address
10468c2ecf20Sopenharmony_ci * @non_blocking: blocking or non-blocking call
10478c2ecf20Sopenharmony_ci *
10488c2ecf20Sopenharmony_ci * Asks the FW to open a TCP connection
10498c2ecf20Sopenharmony_ci */
10508c2ecf20Sopenharmony_cistatic int beiscsi_open_conn(struct iscsi_endpoint *ep,
10518c2ecf20Sopenharmony_ci			     struct sockaddr *src_addr,
10528c2ecf20Sopenharmony_ci			     struct sockaddr *dst_addr, int non_blocking)
10538c2ecf20Sopenharmony_ci{
10548c2ecf20Sopenharmony_ci	struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
10558c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = beiscsi_ep->phba;
10568c2ecf20Sopenharmony_ci	struct tcp_connect_and_offload_out *ptcpcnct_out;
10578c2ecf20Sopenharmony_ci	struct be_dma_mem nonemb_cmd;
10588c2ecf20Sopenharmony_ci	unsigned int tag, req_memsize;
10598c2ecf20Sopenharmony_ci	int ret = -ENOMEM;
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
10628c2ecf20Sopenharmony_ci		    "BS_%d : In beiscsi_open_conn\n");
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
10658c2ecf20Sopenharmony_ci	if (beiscsi_ep->ep_cid == BE_INVALID_CID) {
10668c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
10678c2ecf20Sopenharmony_ci			    "BS_%d : No free cid available\n");
10688c2ecf20Sopenharmony_ci		return ret;
10698c2ecf20Sopenharmony_ci	}
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
10728c2ecf20Sopenharmony_ci		    "BS_%d : In beiscsi_open_conn, ep_cid=%d\n",
10738c2ecf20Sopenharmony_ci		    beiscsi_ep->ep_cid);
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	phba->ep_array[BE_GET_CRI_FROM_CID
10768c2ecf20Sopenharmony_ci		       (beiscsi_ep->ep_cid)] = ep;
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci	beiscsi_ep->cid_vld = 0;
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci	if (is_chip_be2_be3r(phba))
10818c2ecf20Sopenharmony_ci		req_memsize = sizeof(struct tcp_connect_and_offload_in);
10828c2ecf20Sopenharmony_ci	else
10838c2ecf20Sopenharmony_ci		req_memsize = sizeof(struct tcp_connect_and_offload_in_v1);
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	nonemb_cmd.va = dma_alloc_coherent(&phba->ctrl.pdev->dev,
10868c2ecf20Sopenharmony_ci				req_memsize,
10878c2ecf20Sopenharmony_ci				&nonemb_cmd.dma, GFP_KERNEL);
10888c2ecf20Sopenharmony_ci	if (nonemb_cmd.va == NULL) {
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
10918c2ecf20Sopenharmony_ci			    "BS_%d : Failed to allocate memory for"
10928c2ecf20Sopenharmony_ci			    " mgmt_open_connection\n");
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci		beiscsi_free_ep(beiscsi_ep);
10958c2ecf20Sopenharmony_ci		return -ENOMEM;
10968c2ecf20Sopenharmony_ci	}
10978c2ecf20Sopenharmony_ci	nonemb_cmd.size = req_memsize;
10988c2ecf20Sopenharmony_ci	memset(nonemb_cmd.va, 0, nonemb_cmd.size);
10998c2ecf20Sopenharmony_ci	tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep, &nonemb_cmd);
11008c2ecf20Sopenharmony_ci	if (!tag) {
11018c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
11028c2ecf20Sopenharmony_ci			    "BS_%d : mgmt_open_connection Failed for cid=%d\n",
11038c2ecf20Sopenharmony_ci			    beiscsi_ep->ep_cid);
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci		dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
11068c2ecf20Sopenharmony_ci				    nonemb_cmd.va, nonemb_cmd.dma);
11078c2ecf20Sopenharmony_ci		beiscsi_free_ep(beiscsi_ep);
11088c2ecf20Sopenharmony_ci		return -EAGAIN;
11098c2ecf20Sopenharmony_ci	}
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	ret = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
11128c2ecf20Sopenharmony_ci	if (ret) {
11138c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_ERR,
11148c2ecf20Sopenharmony_ci			    BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
11158c2ecf20Sopenharmony_ci			    "BS_%d : mgmt_open_connection Failed");
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci		if (ret != -EBUSY)
11188c2ecf20Sopenharmony_ci			dma_free_coherent(&phba->ctrl.pdev->dev,
11198c2ecf20Sopenharmony_ci					nonemb_cmd.size, nonemb_cmd.va,
11208c2ecf20Sopenharmony_ci					nonemb_cmd.dma);
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci		beiscsi_free_ep(beiscsi_ep);
11238c2ecf20Sopenharmony_ci		return ret;
11248c2ecf20Sopenharmony_ci	}
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	ptcpcnct_out = (struct tcp_connect_and_offload_out *)nonemb_cmd.va;
11278c2ecf20Sopenharmony_ci	beiscsi_ep = ep->dd_data;
11288c2ecf20Sopenharmony_ci	beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
11298c2ecf20Sopenharmony_ci	beiscsi_ep->cid_vld = 1;
11308c2ecf20Sopenharmony_ci	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
11318c2ecf20Sopenharmony_ci		    "BS_%d : mgmt_open_connection Success\n");
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
11348c2ecf20Sopenharmony_ci			    nonemb_cmd.va, nonemb_cmd.dma);
11358c2ecf20Sopenharmony_ci	return 0;
11368c2ecf20Sopenharmony_ci}
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci/**
11398c2ecf20Sopenharmony_ci * beiscsi_ep_connect - Ask chip to create TCP Conn
11408c2ecf20Sopenharmony_ci * @shost: Pointer to scsi_host structure
11418c2ecf20Sopenharmony_ci * @dst_addr: The IP address of Target
11428c2ecf20Sopenharmony_ci * @non_blocking: blocking or non-blocking call
11438c2ecf20Sopenharmony_ci *
11448c2ecf20Sopenharmony_ci * This routines first asks chip to create a connection and then allocates an EP
11458c2ecf20Sopenharmony_ci */
11468c2ecf20Sopenharmony_cistruct iscsi_endpoint *
11478c2ecf20Sopenharmony_cibeiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
11488c2ecf20Sopenharmony_ci		   int non_blocking)
11498c2ecf20Sopenharmony_ci{
11508c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba;
11518c2ecf20Sopenharmony_ci	struct beiscsi_endpoint *beiscsi_ep;
11528c2ecf20Sopenharmony_ci	struct iscsi_endpoint *ep;
11538c2ecf20Sopenharmony_ci	int ret;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	if (!shost) {
11568c2ecf20Sopenharmony_ci		ret = -ENXIO;
11578c2ecf20Sopenharmony_ci		pr_err("beiscsi_ep_connect shost is NULL\n");
11588c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
11598c2ecf20Sopenharmony_ci	}
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	phba = iscsi_host_priv(shost);
11628c2ecf20Sopenharmony_ci	if (!beiscsi_hba_is_online(phba)) {
11638c2ecf20Sopenharmony_ci		ret = -EIO;
11648c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
11658c2ecf20Sopenharmony_ci			    "BS_%d : HBA in error 0x%lx\n", phba->state);
11668c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
11678c2ecf20Sopenharmony_ci	}
11688c2ecf20Sopenharmony_ci	if (!test_bit(BEISCSI_HBA_LINK_UP, &phba->state)) {
11698c2ecf20Sopenharmony_ci		ret = -EBUSY;
11708c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
11718c2ecf20Sopenharmony_ci			    "BS_%d : The Adapter Port state is Down!!!\n");
11728c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
11738c2ecf20Sopenharmony_ci	}
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
11768c2ecf20Sopenharmony_ci	if (!ep) {
11778c2ecf20Sopenharmony_ci		ret = -ENOMEM;
11788c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
11798c2ecf20Sopenharmony_ci	}
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci	beiscsi_ep = ep->dd_data;
11828c2ecf20Sopenharmony_ci	beiscsi_ep->phba = phba;
11838c2ecf20Sopenharmony_ci	beiscsi_ep->openiscsi_ep = ep;
11848c2ecf20Sopenharmony_ci	ret = beiscsi_open_conn(ep, NULL, dst_addr, non_blocking);
11858c2ecf20Sopenharmony_ci	if (ret) {
11868c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
11878c2ecf20Sopenharmony_ci			    "BS_%d : Failed in beiscsi_open_conn\n");
11888c2ecf20Sopenharmony_ci		goto free_ep;
11898c2ecf20Sopenharmony_ci	}
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci	return ep;
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_cifree_ep:
11948c2ecf20Sopenharmony_ci	iscsi_destroy_endpoint(ep);
11958c2ecf20Sopenharmony_ci	return ERR_PTR(ret);
11968c2ecf20Sopenharmony_ci}
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_ci/**
11998c2ecf20Sopenharmony_ci * beiscsi_ep_poll - Poll to see if connection is established
12008c2ecf20Sopenharmony_ci * @ep:	endpoint to be used
12018c2ecf20Sopenharmony_ci * @timeout_ms: timeout specified in millisecs
12028c2ecf20Sopenharmony_ci *
12038c2ecf20Sopenharmony_ci * Poll to see if TCP connection established
12048c2ecf20Sopenharmony_ci */
12058c2ecf20Sopenharmony_ciint beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
12068c2ecf20Sopenharmony_ci{
12078c2ecf20Sopenharmony_ci	struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ci	beiscsi_log(beiscsi_ep->phba, KERN_INFO, BEISCSI_LOG_CONFIG,
12108c2ecf20Sopenharmony_ci		    "BS_%d : In  beiscsi_ep_poll\n");
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	if (beiscsi_ep->cid_vld == 1)
12138c2ecf20Sopenharmony_ci		return 1;
12148c2ecf20Sopenharmony_ci	else
12158c2ecf20Sopenharmony_ci		return 0;
12168c2ecf20Sopenharmony_ci}
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci/**
12198c2ecf20Sopenharmony_ci * beiscsi_flush_cq()- Flush the CQ created.
12208c2ecf20Sopenharmony_ci * @phba: ptr device priv structure.
12218c2ecf20Sopenharmony_ci *
12228c2ecf20Sopenharmony_ci * Before the connection resource are freed flush
12238c2ecf20Sopenharmony_ci * all the CQ enteries
12248c2ecf20Sopenharmony_ci **/
12258c2ecf20Sopenharmony_cistatic void beiscsi_flush_cq(struct beiscsi_hba *phba)
12268c2ecf20Sopenharmony_ci{
12278c2ecf20Sopenharmony_ci	uint16_t i;
12288c2ecf20Sopenharmony_ci	struct be_eq_obj *pbe_eq;
12298c2ecf20Sopenharmony_ci	struct hwi_controller *phwi_ctrlr;
12308c2ecf20Sopenharmony_ci	struct hwi_context_memory *phwi_context;
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	phwi_ctrlr = phba->phwi_ctrlr;
12338c2ecf20Sopenharmony_ci	phwi_context = phwi_ctrlr->phwi_ctxt;
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci	for (i = 0; i < phba->num_cpus; i++) {
12368c2ecf20Sopenharmony_ci		pbe_eq = &phwi_context->be_eq[i];
12378c2ecf20Sopenharmony_ci		irq_poll_disable(&pbe_eq->iopoll);
12388c2ecf20Sopenharmony_ci		beiscsi_process_cq(pbe_eq, BE2_MAX_NUM_CQ_PROC);
12398c2ecf20Sopenharmony_ci		irq_poll_enable(&pbe_eq->iopoll);
12408c2ecf20Sopenharmony_ci	}
12418c2ecf20Sopenharmony_ci}
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci/**
12448c2ecf20Sopenharmony_ci * beiscsi_conn_close - Invalidate and upload connection
12458c2ecf20Sopenharmony_ci * @beiscsi_ep: pointer to device endpoint struct
12468c2ecf20Sopenharmony_ci *
12478c2ecf20Sopenharmony_ci * Returns 0 on success,  -1 on failure.
12488c2ecf20Sopenharmony_ci */
12498c2ecf20Sopenharmony_cistatic int beiscsi_conn_close(struct beiscsi_endpoint *beiscsi_ep)
12508c2ecf20Sopenharmony_ci{
12518c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba = beiscsi_ep->phba;
12528c2ecf20Sopenharmony_ci	unsigned int tag, attempts;
12538c2ecf20Sopenharmony_ci	int ret;
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci	/**
12568c2ecf20Sopenharmony_ci	 * Without successfully invalidating and uploading connection
12578c2ecf20Sopenharmony_ci	 * driver can't reuse the CID so attempt more than once.
12588c2ecf20Sopenharmony_ci	 */
12598c2ecf20Sopenharmony_ci	attempts = 0;
12608c2ecf20Sopenharmony_ci	while (attempts++ < 3) {
12618c2ecf20Sopenharmony_ci		tag = beiscsi_invalidate_cxn(phba, beiscsi_ep);
12628c2ecf20Sopenharmony_ci		if (tag) {
12638c2ecf20Sopenharmony_ci			ret = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
12648c2ecf20Sopenharmony_ci			if (!ret)
12658c2ecf20Sopenharmony_ci				break;
12668c2ecf20Sopenharmony_ci			beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
12678c2ecf20Sopenharmony_ci				    "BS_%d : invalidate conn failed cid %d\n",
12688c2ecf20Sopenharmony_ci				    beiscsi_ep->ep_cid);
12698c2ecf20Sopenharmony_ci		}
12708c2ecf20Sopenharmony_ci	}
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_ci	/* wait for all completions to arrive, then process them */
12738c2ecf20Sopenharmony_ci	msleep(250);
12748c2ecf20Sopenharmony_ci	/* flush CQ entries */
12758c2ecf20Sopenharmony_ci	beiscsi_flush_cq(phba);
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci	if (attempts > 3)
12788c2ecf20Sopenharmony_ci		return -1;
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	attempts = 0;
12818c2ecf20Sopenharmony_ci	while (attempts++ < 3) {
12828c2ecf20Sopenharmony_ci		tag = beiscsi_upload_cxn(phba, beiscsi_ep);
12838c2ecf20Sopenharmony_ci		if (tag) {
12848c2ecf20Sopenharmony_ci			ret = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
12858c2ecf20Sopenharmony_ci			if (!ret)
12868c2ecf20Sopenharmony_ci				break;
12878c2ecf20Sopenharmony_ci			beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
12888c2ecf20Sopenharmony_ci				    "BS_%d : upload conn failed cid %d\n",
12898c2ecf20Sopenharmony_ci				    beiscsi_ep->ep_cid);
12908c2ecf20Sopenharmony_ci		}
12918c2ecf20Sopenharmony_ci	}
12928c2ecf20Sopenharmony_ci	if (attempts > 3)
12938c2ecf20Sopenharmony_ci		return -1;
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	return 0;
12968c2ecf20Sopenharmony_ci}
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci/**
12998c2ecf20Sopenharmony_ci * beiscsi_ep_disconnect - Tears down the TCP connection
13008c2ecf20Sopenharmony_ci * @ep:	endpoint to be used
13018c2ecf20Sopenharmony_ci *
13028c2ecf20Sopenharmony_ci * Tears down the TCP connection
13038c2ecf20Sopenharmony_ci */
13048c2ecf20Sopenharmony_civoid beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
13058c2ecf20Sopenharmony_ci{
13068c2ecf20Sopenharmony_ci	struct beiscsi_endpoint *beiscsi_ep;
13078c2ecf20Sopenharmony_ci	struct beiscsi_conn *beiscsi_conn;
13088c2ecf20Sopenharmony_ci	struct beiscsi_hba *phba;
13098c2ecf20Sopenharmony_ci	uint16_t cri_index;
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	beiscsi_ep = ep->dd_data;
13128c2ecf20Sopenharmony_ci	phba = beiscsi_ep->phba;
13138c2ecf20Sopenharmony_ci	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
13148c2ecf20Sopenharmony_ci		    "BS_%d : In beiscsi_ep_disconnect for ep_cid = %u\n",
13158c2ecf20Sopenharmony_ci		    beiscsi_ep->ep_cid);
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ci	cri_index = BE_GET_CRI_FROM_CID(beiscsi_ep->ep_cid);
13188c2ecf20Sopenharmony_ci	if (!phba->ep_array[cri_index]) {
13198c2ecf20Sopenharmony_ci		__beiscsi_log(phba, KERN_ERR,
13208c2ecf20Sopenharmony_ci			      "BS_%d : ep_array at %u cid %u empty\n",
13218c2ecf20Sopenharmony_ci			      cri_index,
13228c2ecf20Sopenharmony_ci			      beiscsi_ep->ep_cid);
13238c2ecf20Sopenharmony_ci		return;
13248c2ecf20Sopenharmony_ci	}
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	if (beiscsi_ep->conn) {
13278c2ecf20Sopenharmony_ci		beiscsi_conn = beiscsi_ep->conn;
13288c2ecf20Sopenharmony_ci		iscsi_suspend_queue(beiscsi_conn->conn);
13298c2ecf20Sopenharmony_ci	}
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ci	if (!beiscsi_hba_is_online(phba)) {
13328c2ecf20Sopenharmony_ci		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
13338c2ecf20Sopenharmony_ci			    "BS_%d : HBA in error 0x%lx\n", phba->state);
13348c2ecf20Sopenharmony_ci	} else {
13358c2ecf20Sopenharmony_ci		/**
13368c2ecf20Sopenharmony_ci		 * Make CID available even if close fails.
13378c2ecf20Sopenharmony_ci		 * If not freed, FW might fail open using the CID.
13388c2ecf20Sopenharmony_ci		 */
13398c2ecf20Sopenharmony_ci		if (beiscsi_conn_close(beiscsi_ep) < 0)
13408c2ecf20Sopenharmony_ci			__beiscsi_log(phba, KERN_ERR,
13418c2ecf20Sopenharmony_ci				      "BS_%d : close conn failed cid %d\n",
13428c2ecf20Sopenharmony_ci				      beiscsi_ep->ep_cid);
13438c2ecf20Sopenharmony_ci	}
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci	beiscsi_free_ep(beiscsi_ep);
13468c2ecf20Sopenharmony_ci	if (!phba->conn_table[cri_index])
13478c2ecf20Sopenharmony_ci		__beiscsi_log(phba, KERN_ERR,
13488c2ecf20Sopenharmony_ci			      "BS_%d : conn_table empty at %u: cid %u\n",
13498c2ecf20Sopenharmony_ci			      cri_index, beiscsi_ep->ep_cid);
13508c2ecf20Sopenharmony_ci	phba->conn_table[cri_index] = NULL;
13518c2ecf20Sopenharmony_ci	iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
13528c2ecf20Sopenharmony_ci}
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_ciumode_t beiscsi_attr_is_visible(int param_type, int param)
13558c2ecf20Sopenharmony_ci{
13568c2ecf20Sopenharmony_ci	switch (param_type) {
13578c2ecf20Sopenharmony_ci	case ISCSI_NET_PARAM:
13588c2ecf20Sopenharmony_ci		switch (param) {
13598c2ecf20Sopenharmony_ci		case ISCSI_NET_PARAM_IFACE_ENABLE:
13608c2ecf20Sopenharmony_ci		case ISCSI_NET_PARAM_IPV4_ADDR:
13618c2ecf20Sopenharmony_ci		case ISCSI_NET_PARAM_IPV4_SUBNET:
13628c2ecf20Sopenharmony_ci		case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
13638c2ecf20Sopenharmony_ci		case ISCSI_NET_PARAM_IPV4_GW:
13648c2ecf20Sopenharmony_ci		case ISCSI_NET_PARAM_IPV6_ADDR:
13658c2ecf20Sopenharmony_ci		case ISCSI_NET_PARAM_VLAN_ID:
13668c2ecf20Sopenharmony_ci		case ISCSI_NET_PARAM_VLAN_PRIORITY:
13678c2ecf20Sopenharmony_ci		case ISCSI_NET_PARAM_VLAN_ENABLED:
13688c2ecf20Sopenharmony_ci			return S_IRUGO;
13698c2ecf20Sopenharmony_ci		default:
13708c2ecf20Sopenharmony_ci			return 0;
13718c2ecf20Sopenharmony_ci		}
13728c2ecf20Sopenharmony_ci	case ISCSI_HOST_PARAM:
13738c2ecf20Sopenharmony_ci		switch (param) {
13748c2ecf20Sopenharmony_ci		case ISCSI_HOST_PARAM_HWADDRESS:
13758c2ecf20Sopenharmony_ci		case ISCSI_HOST_PARAM_INITIATOR_NAME:
13768c2ecf20Sopenharmony_ci		case ISCSI_HOST_PARAM_PORT_STATE:
13778c2ecf20Sopenharmony_ci		case ISCSI_HOST_PARAM_PORT_SPEED:
13788c2ecf20Sopenharmony_ci			return S_IRUGO;
13798c2ecf20Sopenharmony_ci		default:
13808c2ecf20Sopenharmony_ci			return 0;
13818c2ecf20Sopenharmony_ci		}
13828c2ecf20Sopenharmony_ci	case ISCSI_PARAM:
13838c2ecf20Sopenharmony_ci		switch (param) {
13848c2ecf20Sopenharmony_ci		case ISCSI_PARAM_MAX_RECV_DLENGTH:
13858c2ecf20Sopenharmony_ci		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
13868c2ecf20Sopenharmony_ci		case ISCSI_PARAM_HDRDGST_EN:
13878c2ecf20Sopenharmony_ci		case ISCSI_PARAM_DATADGST_EN:
13888c2ecf20Sopenharmony_ci		case ISCSI_PARAM_CONN_ADDRESS:
13898c2ecf20Sopenharmony_ci		case ISCSI_PARAM_CONN_PORT:
13908c2ecf20Sopenharmony_ci		case ISCSI_PARAM_EXP_STATSN:
13918c2ecf20Sopenharmony_ci		case ISCSI_PARAM_PERSISTENT_ADDRESS:
13928c2ecf20Sopenharmony_ci		case ISCSI_PARAM_PERSISTENT_PORT:
13938c2ecf20Sopenharmony_ci		case ISCSI_PARAM_PING_TMO:
13948c2ecf20Sopenharmony_ci		case ISCSI_PARAM_RECV_TMO:
13958c2ecf20Sopenharmony_ci		case ISCSI_PARAM_INITIAL_R2T_EN:
13968c2ecf20Sopenharmony_ci		case ISCSI_PARAM_MAX_R2T:
13978c2ecf20Sopenharmony_ci		case ISCSI_PARAM_IMM_DATA_EN:
13988c2ecf20Sopenharmony_ci		case ISCSI_PARAM_FIRST_BURST:
13998c2ecf20Sopenharmony_ci		case ISCSI_PARAM_MAX_BURST:
14008c2ecf20Sopenharmony_ci		case ISCSI_PARAM_PDU_INORDER_EN:
14018c2ecf20Sopenharmony_ci		case ISCSI_PARAM_DATASEQ_INORDER_EN:
14028c2ecf20Sopenharmony_ci		case ISCSI_PARAM_ERL:
14038c2ecf20Sopenharmony_ci		case ISCSI_PARAM_TARGET_NAME:
14048c2ecf20Sopenharmony_ci		case ISCSI_PARAM_TPGT:
14058c2ecf20Sopenharmony_ci		case ISCSI_PARAM_USERNAME:
14068c2ecf20Sopenharmony_ci		case ISCSI_PARAM_PASSWORD:
14078c2ecf20Sopenharmony_ci		case ISCSI_PARAM_USERNAME_IN:
14088c2ecf20Sopenharmony_ci		case ISCSI_PARAM_PASSWORD_IN:
14098c2ecf20Sopenharmony_ci		case ISCSI_PARAM_FAST_ABORT:
14108c2ecf20Sopenharmony_ci		case ISCSI_PARAM_ABORT_TMO:
14118c2ecf20Sopenharmony_ci		case ISCSI_PARAM_LU_RESET_TMO:
14128c2ecf20Sopenharmony_ci		case ISCSI_PARAM_IFACE_NAME:
14138c2ecf20Sopenharmony_ci		case ISCSI_PARAM_INITIATOR_NAME:
14148c2ecf20Sopenharmony_ci			return S_IRUGO;
14158c2ecf20Sopenharmony_ci		default:
14168c2ecf20Sopenharmony_ci			return 0;
14178c2ecf20Sopenharmony_ci		}
14188c2ecf20Sopenharmony_ci	}
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	return 0;
14218c2ecf20Sopenharmony_ci}
1422