Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This file is part of the Emulex Linux Device Driver for Enterprise iSCSI
0004  * Host Bus Adapters. Refer to the README file included with this package
0005  * for driver version and adapter compatibility.
0006  *
0007  * Copyright (c) 2018 Broadcom. All Rights Reserved.
0008  * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
0009  *
0010  * Contact Information:
0011  * linux-drivers@broadcom.com
0012  */
0013 
0014 #include <scsi/libiscsi.h>
0015 #include <scsi/scsi_transport_iscsi.h>
0016 #include <scsi/scsi_transport.h>
0017 #include <scsi/scsi_cmnd.h>
0018 #include <scsi/scsi_device.h>
0019 #include <scsi/scsi_host.h>
0020 #include <scsi/scsi_netlink.h>
0021 #include <net/netlink.h>
0022 #include <scsi/scsi.h>
0023 
0024 #include "be_iscsi.h"
0025 
0026 extern struct iscsi_transport beiscsi_iscsi_transport;
0027 
0028 /**
0029  * beiscsi_session_create - creates a new iscsi session
0030  * @ep: pointer to iscsi ep
0031  * @cmds_max: max commands supported
0032  * @qdepth: max queue depth supported
0033  * @initial_cmdsn: initial iscsi CMDSN
0034  */
0035 struct iscsi_cls_session *beiscsi_session_create(struct iscsi_endpoint *ep,
0036                          u16 cmds_max,
0037                          u16 qdepth,
0038                          u32 initial_cmdsn)
0039 {
0040     struct Scsi_Host *shost;
0041     struct beiscsi_endpoint *beiscsi_ep;
0042     struct iscsi_cls_session *cls_session;
0043     struct beiscsi_hba *phba;
0044     struct iscsi_session *sess;
0045     struct beiscsi_session *beiscsi_sess;
0046     struct beiscsi_io_task *io_task;
0047 
0048 
0049     if (!ep) {
0050         pr_err("beiscsi_session_create: invalid ep\n");
0051         return NULL;
0052     }
0053     beiscsi_ep = ep->dd_data;
0054     phba = beiscsi_ep->phba;
0055 
0056     if (!beiscsi_hba_is_online(phba)) {
0057         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0058                 "BS_%d : HBA in error 0x%lx\n", phba->state);
0059         return NULL;
0060     }
0061 
0062     beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0063             "BS_%d : In beiscsi_session_create\n");
0064     if (cmds_max > beiscsi_ep->phba->params.wrbs_per_cxn) {
0065         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
0066                 "BS_%d : Cannot handle %d cmds."
0067                 "Max cmds per session supported is %d. Using %d."
0068                 "\n", cmds_max,
0069                 beiscsi_ep->phba->params.wrbs_per_cxn,
0070                 beiscsi_ep->phba->params.wrbs_per_cxn);
0071 
0072         cmds_max = beiscsi_ep->phba->params.wrbs_per_cxn;
0073     }
0074 
0075     shost = phba->shost;
0076     cls_session = iscsi_session_setup(&beiscsi_iscsi_transport,
0077                       shost, cmds_max,
0078                       sizeof(*beiscsi_sess),
0079                       sizeof(*io_task),
0080                       initial_cmdsn, ISCSI_MAX_TARGET);
0081     if (!cls_session)
0082         return NULL;
0083     sess = cls_session->dd_data;
0084     beiscsi_sess = sess->dd_data;
0085     beiscsi_sess->bhs_pool =  dma_pool_create("beiscsi_bhs_pool",
0086                            &phba->pcidev->dev,
0087                            sizeof(struct be_cmd_bhs),
0088                            64, 0);
0089     if (!beiscsi_sess->bhs_pool)
0090         goto destroy_sess;
0091 
0092     return cls_session;
0093 destroy_sess:
0094     iscsi_session_teardown(cls_session);
0095     return NULL;
0096 }
0097 
0098 /**
0099  * beiscsi_session_destroy - destroys iscsi session
0100  * @cls_session:    pointer to iscsi cls session
0101  *
0102  * Destroys iSCSI session instance and releases
0103  * resources allocated for it.
0104  */
0105 void beiscsi_session_destroy(struct iscsi_cls_session *cls_session)
0106 {
0107     struct iscsi_session *sess = cls_session->dd_data;
0108     struct beiscsi_session *beiscsi_sess = sess->dd_data;
0109 
0110     printk(KERN_INFO "In beiscsi_session_destroy\n");
0111     dma_pool_destroy(beiscsi_sess->bhs_pool);
0112     iscsi_session_teardown(cls_session);
0113 }
0114 
0115 /**
0116  * beiscsi_session_fail(): Closing session with appropriate error
0117  * @cls_session: ptr to session
0118  **/
0119 void beiscsi_session_fail(struct iscsi_cls_session *cls_session)
0120 {
0121     iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
0122 }
0123 
0124 
0125 /**
0126  * beiscsi_conn_create - create an instance of iscsi connection
0127  * @cls_session: ptr to iscsi_cls_session
0128  * @cid: iscsi cid
0129  */
0130 struct iscsi_cls_conn *
0131 beiscsi_conn_create(struct iscsi_cls_session *cls_session, u32 cid)
0132 {
0133     struct beiscsi_hba *phba;
0134     struct Scsi_Host *shost;
0135     struct iscsi_cls_conn *cls_conn;
0136     struct beiscsi_conn *beiscsi_conn;
0137     struct iscsi_conn *conn;
0138     struct iscsi_session *sess;
0139     struct beiscsi_session *beiscsi_sess;
0140 
0141     shost = iscsi_session_to_shost(cls_session);
0142     phba = iscsi_host_priv(shost);
0143 
0144     beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0145             "BS_%d : In beiscsi_conn_create ,cid"
0146             "from iscsi layer=%d\n", cid);
0147 
0148     cls_conn = iscsi_conn_setup(cls_session, sizeof(*beiscsi_conn), cid);
0149     if (!cls_conn)
0150         return NULL;
0151 
0152     conn = cls_conn->dd_data;
0153     beiscsi_conn = conn->dd_data;
0154     beiscsi_conn->ep = NULL;
0155     beiscsi_conn->phba = phba;
0156     beiscsi_conn->conn = conn;
0157     sess = cls_session->dd_data;
0158     beiscsi_sess = sess->dd_data;
0159     beiscsi_conn->beiscsi_sess = beiscsi_sess;
0160     return cls_conn;
0161 }
0162 
0163 /**
0164  * beiscsi_conn_bind - Binds iscsi session/connection with TCP connection
0165  * @cls_session: pointer to iscsi cls session
0166  * @cls_conn: pointer to iscsi cls conn
0167  * @transport_fd: EP handle(64 bit)
0168  * @is_leading: indicate if this is the session leading connection (MCS)
0169  *
0170  * This function binds the TCP Conn with iSCSI Connection and Session.
0171  */
0172 int beiscsi_conn_bind(struct iscsi_cls_session *cls_session,
0173               struct iscsi_cls_conn *cls_conn,
0174               u64 transport_fd, int is_leading)
0175 {
0176     struct iscsi_conn *conn = cls_conn->dd_data;
0177     struct beiscsi_conn *beiscsi_conn = conn->dd_data;
0178     struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
0179     struct beiscsi_hba *phba = iscsi_host_priv(shost);
0180     struct hwi_controller *phwi_ctrlr = phba->phwi_ctrlr;
0181     struct hwi_wrb_context *pwrb_context;
0182     struct beiscsi_endpoint *beiscsi_ep;
0183     struct iscsi_endpoint *ep;
0184     uint16_t cri_index;
0185     int rc = 0;
0186 
0187     ep = iscsi_lookup_endpoint(transport_fd);
0188     if (!ep)
0189         return -EINVAL;
0190 
0191     beiscsi_ep = ep->dd_data;
0192 
0193     if (iscsi_conn_bind(cls_session, cls_conn, is_leading)) {
0194         rc = -EINVAL;
0195         goto put_ep;
0196     }
0197 
0198     if (beiscsi_ep->phba != phba) {
0199         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
0200                 "BS_%d : beiscsi_ep->hba=%p not equal to phba=%p\n",
0201                 beiscsi_ep->phba, phba);
0202         rc = -EEXIST;
0203         goto put_ep;
0204     }
0205     cri_index = BE_GET_CRI_FROM_CID(beiscsi_ep->ep_cid);
0206     if (phba->conn_table[cri_index]) {
0207         if (beiscsi_conn != phba->conn_table[cri_index] ||
0208             beiscsi_ep != phba->conn_table[cri_index]->ep) {
0209             __beiscsi_log(phba, KERN_ERR,
0210                       "BS_%d : conn_table not empty at %u: cid %u conn %p:%p\n",
0211                       cri_index,
0212                       beiscsi_ep->ep_cid,
0213                       beiscsi_conn,
0214                       phba->conn_table[cri_index]);
0215             rc = -EINVAL;
0216             goto put_ep;
0217         }
0218     }
0219 
0220     beiscsi_conn->beiscsi_conn_cid = beiscsi_ep->ep_cid;
0221     beiscsi_conn->ep = beiscsi_ep;
0222     beiscsi_ep->conn = beiscsi_conn;
0223     /**
0224      * Each connection is associated with a WRBQ kept in wrb_context.
0225      * Store doorbell offset for transmit path.
0226      */
0227     pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
0228     beiscsi_conn->doorbell_offset = pwrb_context->doorbell_offset;
0229     beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0230             "BS_%d : cid %d phba->conn_table[%u]=%p\n",
0231             beiscsi_ep->ep_cid, cri_index, beiscsi_conn);
0232     phba->conn_table[cri_index] = beiscsi_conn;
0233 
0234 put_ep:
0235     iscsi_put_endpoint(ep);
0236     return rc;
0237 }
0238 
0239 static int beiscsi_iface_create_ipv4(struct beiscsi_hba *phba)
0240 {
0241     if (phba->ipv4_iface)
0242         return 0;
0243 
0244     phba->ipv4_iface = iscsi_create_iface(phba->shost,
0245                           &beiscsi_iscsi_transport,
0246                           ISCSI_IFACE_TYPE_IPV4,
0247                           0, 0);
0248     if (!phba->ipv4_iface) {
0249         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
0250                 "BS_%d : Could not "
0251                 "create default IPv4 address.\n");
0252         return -ENODEV;
0253     }
0254 
0255     return 0;
0256 }
0257 
0258 static int beiscsi_iface_create_ipv6(struct beiscsi_hba *phba)
0259 {
0260     if (phba->ipv6_iface)
0261         return 0;
0262 
0263     phba->ipv6_iface = iscsi_create_iface(phba->shost,
0264                           &beiscsi_iscsi_transport,
0265                           ISCSI_IFACE_TYPE_IPV6,
0266                           0, 0);
0267     if (!phba->ipv6_iface) {
0268         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
0269                 "BS_%d : Could not "
0270                 "create default IPv6 address.\n");
0271         return -ENODEV;
0272     }
0273 
0274     return 0;
0275 }
0276 
0277 void beiscsi_iface_create_default(struct beiscsi_hba *phba)
0278 {
0279     struct be_cmd_get_if_info_resp *if_info;
0280 
0281     if (!beiscsi_if_get_info(phba, BEISCSI_IP_TYPE_V4, &if_info)) {
0282         beiscsi_iface_create_ipv4(phba);
0283         kfree(if_info);
0284     }
0285 
0286     if (!beiscsi_if_get_info(phba, BEISCSI_IP_TYPE_V6, &if_info)) {
0287         beiscsi_iface_create_ipv6(phba);
0288         kfree(if_info);
0289     }
0290 }
0291 
0292 void beiscsi_iface_destroy_default(struct beiscsi_hba *phba)
0293 {
0294     if (phba->ipv6_iface) {
0295         iscsi_destroy_iface(phba->ipv6_iface);
0296         phba->ipv6_iface = NULL;
0297     }
0298     if (phba->ipv4_iface) {
0299         iscsi_destroy_iface(phba->ipv4_iface);
0300         phba->ipv4_iface = NULL;
0301     }
0302 }
0303 
0304 /**
0305  * beiscsi_iface_config_vlan()- Set the VLAN TAG
0306  * @shost: Scsi Host for the driver instance
0307  * @iface_param: Interface paramters
0308  *
0309  * Set the VLAN TAG for the adapter or disable
0310  * the VLAN config
0311  *
0312  * returns
0313  *  Success: 0
0314  *  Failure: Non-Zero Value
0315  **/
0316 static int
0317 beiscsi_iface_config_vlan(struct Scsi_Host *shost,
0318               struct iscsi_iface_param_info *iface_param)
0319 {
0320     struct beiscsi_hba *phba = iscsi_host_priv(shost);
0321     int ret = -EPERM;
0322 
0323     switch (iface_param->param) {
0324     case ISCSI_NET_PARAM_VLAN_ENABLED:
0325         ret = 0;
0326         if (iface_param->value[0] != ISCSI_VLAN_ENABLE)
0327             ret = beiscsi_if_set_vlan(phba, BEISCSI_VLAN_DISABLE);
0328         break;
0329     case ISCSI_NET_PARAM_VLAN_TAG:
0330         ret = beiscsi_if_set_vlan(phba,
0331                       *((uint16_t *)iface_param->value));
0332         break;
0333     }
0334     return ret;
0335 }
0336 
0337 
0338 static int
0339 beiscsi_iface_config_ipv4(struct Scsi_Host *shost,
0340               struct iscsi_iface_param_info *info,
0341               void *data, uint32_t dt_len)
0342 {
0343     struct beiscsi_hba *phba = iscsi_host_priv(shost);
0344     u8 *ip = NULL, *subnet = NULL, *gw;
0345     struct nlattr *nla;
0346     int ret = -EPERM;
0347 
0348     /* Check the param */
0349     switch (info->param) {
0350     case ISCSI_NET_PARAM_IFACE_ENABLE:
0351         if (info->value[0] == ISCSI_IFACE_ENABLE)
0352             ret = beiscsi_iface_create_ipv4(phba);
0353         else {
0354             iscsi_destroy_iface(phba->ipv4_iface);
0355             phba->ipv4_iface = NULL;
0356         }
0357         break;
0358     case ISCSI_NET_PARAM_IPV4_GW:
0359         gw = info->value;
0360         ret = beiscsi_if_set_gw(phba, BEISCSI_IP_TYPE_V4, gw);
0361         break;
0362     case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
0363         if (info->value[0] == ISCSI_BOOTPROTO_DHCP)
0364             ret = beiscsi_if_en_dhcp(phba, BEISCSI_IP_TYPE_V4);
0365         else if (info->value[0] == ISCSI_BOOTPROTO_STATIC)
0366             /* release DHCP IP address */
0367             ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V4,
0368                            NULL, NULL);
0369         else
0370             beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
0371                     "BS_%d : Invalid BOOTPROTO: %d\n",
0372                     info->value[0]);
0373         break;
0374     case ISCSI_NET_PARAM_IPV4_ADDR:
0375         ip = info->value;
0376         nla = nla_find(data, dt_len, ISCSI_NET_PARAM_IPV4_SUBNET);
0377         if (nla) {
0378             info = nla_data(nla);
0379             subnet = info->value;
0380         }
0381         ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V4,
0382                        ip, subnet);
0383         break;
0384     case ISCSI_NET_PARAM_IPV4_SUBNET:
0385         /*
0386          * OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR ioctl needs IP
0387          * and subnet both. Find IP to be applied for this subnet.
0388          */
0389         subnet = info->value;
0390         nla = nla_find(data, dt_len, ISCSI_NET_PARAM_IPV4_ADDR);
0391         if (nla) {
0392             info = nla_data(nla);
0393             ip = info->value;
0394         }
0395         ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V4,
0396                        ip, subnet);
0397         break;
0398     }
0399 
0400     return ret;
0401 }
0402 
0403 static int
0404 beiscsi_iface_config_ipv6(struct Scsi_Host *shost,
0405               struct iscsi_iface_param_info *iface_param,
0406               void *data, uint32_t dt_len)
0407 {
0408     struct beiscsi_hba *phba = iscsi_host_priv(shost);
0409     int ret = -EPERM;
0410 
0411     switch (iface_param->param) {
0412     case ISCSI_NET_PARAM_IFACE_ENABLE:
0413         if (iface_param->value[0] == ISCSI_IFACE_ENABLE)
0414             ret = beiscsi_iface_create_ipv6(phba);
0415         else {
0416             iscsi_destroy_iface(phba->ipv6_iface);
0417             phba->ipv6_iface = NULL;
0418         }
0419         break;
0420     case ISCSI_NET_PARAM_IPV6_ADDR:
0421         ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V6,
0422                        iface_param->value, NULL);
0423         break;
0424     }
0425 
0426     return ret;
0427 }
0428 
0429 int beiscsi_iface_set_param(struct Scsi_Host *shost,
0430                 void *data, uint32_t dt_len)
0431 {
0432     struct iscsi_iface_param_info *iface_param = NULL;
0433     struct beiscsi_hba *phba = iscsi_host_priv(shost);
0434     struct nlattr *attrib;
0435     uint32_t rm_len = dt_len;
0436     int ret;
0437 
0438     if (!beiscsi_hba_is_online(phba)) {
0439         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0440                 "BS_%d : HBA in error 0x%lx\n", phba->state);
0441         return -EBUSY;
0442     }
0443 
0444     /* update interface_handle */
0445     ret = beiscsi_if_get_handle(phba);
0446     if (ret) {
0447         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
0448                 "BS_%d : Getting Interface Handle Failed\n");
0449         return ret;
0450     }
0451 
0452     nla_for_each_attr(attrib, data, dt_len, rm_len) {
0453         iface_param = nla_data(attrib);
0454 
0455         if (iface_param->param_type != ISCSI_NET_PARAM)
0456             continue;
0457 
0458         /*
0459          * BE2ISCSI only supports 1 interface
0460          */
0461         if (iface_param->iface_num) {
0462             beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
0463                     "BS_%d : Invalid iface_num %d."
0464                     "Only iface_num 0 is supported.\n",
0465                     iface_param->iface_num);
0466 
0467             return -EINVAL;
0468         }
0469 
0470         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0471                 "BS_%d : %s.0 set param %d",
0472                 (iface_param->iface_type == ISCSI_IFACE_TYPE_IPV4) ?
0473                 "ipv4" : "ipv6", iface_param->param);
0474 
0475         ret = -EPERM;
0476         switch (iface_param->param) {
0477         case ISCSI_NET_PARAM_VLAN_ENABLED:
0478         case ISCSI_NET_PARAM_VLAN_TAG:
0479             ret = beiscsi_iface_config_vlan(shost, iface_param);
0480             break;
0481         default:
0482             switch (iface_param->iface_type) {
0483             case ISCSI_IFACE_TYPE_IPV4:
0484                 ret = beiscsi_iface_config_ipv4(shost,
0485                                 iface_param,
0486                                 data, dt_len);
0487                 break;
0488             case ISCSI_IFACE_TYPE_IPV6:
0489                 ret = beiscsi_iface_config_ipv6(shost,
0490                                 iface_param,
0491                                 data, dt_len);
0492                 break;
0493             }
0494         }
0495 
0496         if (ret == -EPERM) {
0497             __beiscsi_log(phba, KERN_ERR,
0498                       "BS_%d : %s.0 set param %d not permitted",
0499                       (iface_param->iface_type ==
0500                        ISCSI_IFACE_TYPE_IPV4) ? "ipv4" : "ipv6",
0501                       iface_param->param);
0502             ret = 0;
0503         }
0504         if (ret)
0505             break;
0506     }
0507 
0508     return ret;
0509 }
0510 
0511 static int __beiscsi_iface_get_param(struct beiscsi_hba *phba,
0512                      struct iscsi_iface *iface,
0513                      int param, char *buf)
0514 {
0515     struct be_cmd_get_if_info_resp *if_info;
0516     int len, ip_type = BEISCSI_IP_TYPE_V4;
0517 
0518     if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
0519         ip_type = BEISCSI_IP_TYPE_V6;
0520 
0521     len = beiscsi_if_get_info(phba, ip_type, &if_info);
0522     if (len)
0523         return len;
0524 
0525     switch (param) {
0526     case ISCSI_NET_PARAM_IPV4_ADDR:
0527         len = sprintf(buf, "%pI4\n", if_info->ip_addr.addr);
0528         break;
0529     case ISCSI_NET_PARAM_IPV6_ADDR:
0530         len = sprintf(buf, "%pI6\n", if_info->ip_addr.addr);
0531         break;
0532     case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
0533         if (!if_info->dhcp_state)
0534             len = sprintf(buf, "static\n");
0535         else
0536             len = sprintf(buf, "dhcp\n");
0537         break;
0538     case ISCSI_NET_PARAM_IPV4_SUBNET:
0539         len = sprintf(buf, "%pI4\n", if_info->ip_addr.subnet_mask);
0540         break;
0541     case ISCSI_NET_PARAM_VLAN_ENABLED:
0542         len = sprintf(buf, "%s\n",
0543                   (if_info->vlan_priority == BEISCSI_VLAN_DISABLE) ?
0544                   "disable" : "enable");
0545         break;
0546     case ISCSI_NET_PARAM_VLAN_ID:
0547         if (if_info->vlan_priority == BEISCSI_VLAN_DISABLE)
0548             len = -EINVAL;
0549         else
0550             len = sprintf(buf, "%d\n",
0551                       (if_info->vlan_priority &
0552                        ISCSI_MAX_VLAN_ID));
0553         break;
0554     case ISCSI_NET_PARAM_VLAN_PRIORITY:
0555         if (if_info->vlan_priority == BEISCSI_VLAN_DISABLE)
0556             len = -EINVAL;
0557         else
0558             len = sprintf(buf, "%d\n",
0559                       ((if_info->vlan_priority >> 13) &
0560                        ISCSI_MAX_VLAN_PRIORITY));
0561         break;
0562     default:
0563         WARN_ON(1);
0564     }
0565 
0566     kfree(if_info);
0567     return len;
0568 }
0569 
0570 int beiscsi_iface_get_param(struct iscsi_iface *iface,
0571                 enum iscsi_param_type param_type,
0572                 int param, char *buf)
0573 {
0574     struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
0575     struct beiscsi_hba *phba = iscsi_host_priv(shost);
0576     struct be_cmd_get_def_gateway_resp gateway;
0577     int len = -EPERM;
0578 
0579     if (param_type != ISCSI_NET_PARAM)
0580         return 0;
0581     if (!beiscsi_hba_is_online(phba)) {
0582         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0583                 "BS_%d : HBA in error 0x%lx\n", phba->state);
0584         return -EBUSY;
0585     }
0586 
0587     switch (param) {
0588     case ISCSI_NET_PARAM_IPV4_ADDR:
0589     case ISCSI_NET_PARAM_IPV4_SUBNET:
0590     case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
0591     case ISCSI_NET_PARAM_IPV6_ADDR:
0592     case ISCSI_NET_PARAM_VLAN_ENABLED:
0593     case ISCSI_NET_PARAM_VLAN_ID:
0594     case ISCSI_NET_PARAM_VLAN_PRIORITY:
0595         len = __beiscsi_iface_get_param(phba, iface, param, buf);
0596         break;
0597     case ISCSI_NET_PARAM_IFACE_ENABLE:
0598         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
0599             len = sprintf(buf, "%s\n",
0600                       phba->ipv4_iface ? "enable" : "disable");
0601         else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
0602             len = sprintf(buf, "%s\n",
0603                       phba->ipv6_iface ? "enable" : "disable");
0604         break;
0605     case ISCSI_NET_PARAM_IPV4_GW:
0606         memset(&gateway, 0, sizeof(gateway));
0607         len = beiscsi_if_get_gw(phba, BEISCSI_IP_TYPE_V4, &gateway);
0608         if (!len)
0609             len = sprintf(buf, "%pI4\n", &gateway.ip_addr.addr);
0610         break;
0611     }
0612 
0613     return len;
0614 }
0615 
0616 /**
0617  * beiscsi_ep_get_param - get the iscsi parameter
0618  * @ep: pointer to iscsi ep
0619  * @param: parameter type identifier
0620  * @buf: buffer pointer
0621  *
0622  * returns iscsi parameter
0623  */
0624 int beiscsi_ep_get_param(struct iscsi_endpoint *ep,
0625                enum iscsi_param param, char *buf)
0626 {
0627     struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
0628     int len;
0629 
0630     beiscsi_log(beiscsi_ep->phba, KERN_INFO,
0631             BEISCSI_LOG_CONFIG,
0632             "BS_%d : In beiscsi_ep_get_param,"
0633             " param= %d\n", param);
0634 
0635     switch (param) {
0636     case ISCSI_PARAM_CONN_PORT:
0637         len = sprintf(buf, "%hu\n", beiscsi_ep->dst_tcpport);
0638         break;
0639     case ISCSI_PARAM_CONN_ADDRESS:
0640         if (beiscsi_ep->ip_type == BEISCSI_IP_TYPE_V4)
0641             len = sprintf(buf, "%pI4\n", &beiscsi_ep->dst_addr);
0642         else
0643             len = sprintf(buf, "%pI6\n", &beiscsi_ep->dst6_addr);
0644         break;
0645     default:
0646         len = -EPERM;
0647     }
0648     return len;
0649 }
0650 
0651 int beiscsi_set_param(struct iscsi_cls_conn *cls_conn,
0652               enum iscsi_param param, char *buf, int buflen)
0653 {
0654     struct iscsi_conn *conn = cls_conn->dd_data;
0655     struct iscsi_session *session = conn->session;
0656     struct beiscsi_hba *phba = NULL;
0657     int ret;
0658 
0659     phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
0660     beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0661             "BS_%d : In beiscsi_conn_set_param,"
0662             " param= %d\n", param);
0663 
0664     ret = iscsi_set_param(cls_conn, param, buf, buflen);
0665     if (ret)
0666         return ret;
0667     /*
0668      * If userspace tried to set the value to higher than we can
0669      * support override here.
0670      */
0671     switch (param) {
0672     case ISCSI_PARAM_FIRST_BURST:
0673         if (session->first_burst > 8192)
0674             session->first_burst = 8192;
0675         break;
0676     case ISCSI_PARAM_MAX_RECV_DLENGTH:
0677         if (conn->max_recv_dlength > 65536)
0678             conn->max_recv_dlength = 65536;
0679         break;
0680     case ISCSI_PARAM_MAX_BURST:
0681         if (session->max_burst > 262144)
0682             session->max_burst = 262144;
0683         break;
0684     case ISCSI_PARAM_MAX_XMIT_DLENGTH:
0685         if (conn->max_xmit_dlength > 65536)
0686             conn->max_xmit_dlength = 65536;
0687         fallthrough;
0688     default:
0689         return 0;
0690     }
0691 
0692     return 0;
0693 }
0694 
0695 /**
0696  * beiscsi_get_port_state - Get the Port State
0697  * @shost : pointer to scsi_host structure
0698  *
0699  */
0700 static void beiscsi_get_port_state(struct Scsi_Host *shost)
0701 {
0702     struct beiscsi_hba *phba = iscsi_host_priv(shost);
0703     struct iscsi_cls_host *ihost = shost->shost_data;
0704 
0705     ihost->port_state = test_bit(BEISCSI_HBA_LINK_UP, &phba->state) ?
0706         ISCSI_PORT_STATE_UP : ISCSI_PORT_STATE_DOWN;
0707 }
0708 
0709 /**
0710  * beiscsi_get_port_speed  - Get the Port Speed from Adapter
0711  * @shost : pointer to scsi_host structure
0712  *
0713  */
0714 static void beiscsi_get_port_speed(struct Scsi_Host *shost)
0715 {
0716     struct beiscsi_hba *phba = iscsi_host_priv(shost);
0717     struct iscsi_cls_host *ihost = shost->shost_data;
0718 
0719     switch (phba->port_speed) {
0720     case BE2ISCSI_LINK_SPEED_10MBPS:
0721         ihost->port_speed = ISCSI_PORT_SPEED_10MBPS;
0722         break;
0723     case BE2ISCSI_LINK_SPEED_100MBPS:
0724         ihost->port_speed = ISCSI_PORT_SPEED_100MBPS;
0725         break;
0726     case BE2ISCSI_LINK_SPEED_1GBPS:
0727         ihost->port_speed = ISCSI_PORT_SPEED_1GBPS;
0728         break;
0729     case BE2ISCSI_LINK_SPEED_10GBPS:
0730         ihost->port_speed = ISCSI_PORT_SPEED_10GBPS;
0731         break;
0732     case BE2ISCSI_LINK_SPEED_25GBPS:
0733         ihost->port_speed = ISCSI_PORT_SPEED_25GBPS;
0734         break;
0735     case BE2ISCSI_LINK_SPEED_40GBPS:
0736         ihost->port_speed = ISCSI_PORT_SPEED_40GBPS;
0737         break;
0738     default:
0739         ihost->port_speed = ISCSI_PORT_SPEED_UNKNOWN;
0740     }
0741 }
0742 
0743 /**
0744  * beiscsi_get_host_param - get the iscsi parameter
0745  * @shost: pointer to scsi_host structure
0746  * @param: parameter type identifier
0747  * @buf: buffer pointer
0748  *
0749  */
0750 int beiscsi_get_host_param(struct Scsi_Host *shost,
0751                enum iscsi_host_param param, char *buf)
0752 {
0753     struct beiscsi_hba *phba = iscsi_host_priv(shost);
0754     int status = 0;
0755 
0756     if (!beiscsi_hba_is_online(phba)) {
0757         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0758                 "BS_%d : HBA in error 0x%lx\n", phba->state);
0759         return 0;
0760     }
0761     beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0762             "BS_%d : In beiscsi_get_host_param, param = %d\n", param);
0763 
0764     switch (param) {
0765     case ISCSI_HOST_PARAM_HWADDRESS:
0766         status = beiscsi_get_macaddr(buf, phba);
0767         if (status < 0) {
0768             beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
0769                     "BS_%d : beiscsi_get_macaddr Failed\n");
0770             return 0;
0771         }
0772         break;
0773     case ISCSI_HOST_PARAM_INITIATOR_NAME:
0774         /* try fetching user configured name first */
0775         status = beiscsi_get_initiator_name(phba, buf, true);
0776         if (status < 0) {
0777             status = beiscsi_get_initiator_name(phba, buf, false);
0778             if (status < 0) {
0779                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
0780                         "BS_%d : Retrieving Initiator Name Failed\n");
0781                 status = 0;
0782             }
0783         }
0784         break;
0785     case ISCSI_HOST_PARAM_PORT_STATE:
0786         beiscsi_get_port_state(shost);
0787         status = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
0788         break;
0789     case ISCSI_HOST_PARAM_PORT_SPEED:
0790         beiscsi_get_port_speed(shost);
0791         status = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
0792         break;
0793     default:
0794         return iscsi_host_get_param(shost, param, buf);
0795     }
0796     return status;
0797 }
0798 
0799 int beiscsi_get_macaddr(char *buf, struct beiscsi_hba *phba)
0800 {
0801     struct be_cmd_get_nic_conf_resp resp;
0802     int rc;
0803 
0804     if (phba->mac_addr_set)
0805         return sysfs_format_mac(buf, phba->mac_address, ETH_ALEN);
0806 
0807     memset(&resp, 0, sizeof(resp));
0808     rc = mgmt_get_nic_conf(phba, &resp);
0809     if (rc)
0810         return rc;
0811 
0812     phba->mac_addr_set = true;
0813     memcpy(phba->mac_address, resp.mac_address, ETH_ALEN);
0814     return sysfs_format_mac(buf, phba->mac_address, ETH_ALEN);
0815 }
0816 
0817 /**
0818  * beiscsi_conn_get_stats - get the iscsi stats
0819  * @cls_conn: pointer to iscsi cls conn
0820  * @stats: pointer to iscsi_stats structure
0821  *
0822  * returns iscsi stats
0823  */
0824 void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
0825                 struct iscsi_stats *stats)
0826 {
0827     struct iscsi_conn *conn = cls_conn->dd_data;
0828     struct beiscsi_hba *phba = NULL;
0829 
0830     phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
0831     beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0832             "BS_%d : In beiscsi_conn_get_stats\n");
0833 
0834     stats->txdata_octets = conn->txdata_octets;
0835     stats->rxdata_octets = conn->rxdata_octets;
0836     stats->dataout_pdus = conn->dataout_pdus_cnt;
0837     stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
0838     stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
0839     stats->datain_pdus = conn->datain_pdus_cnt;
0840     stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
0841     stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
0842     stats->r2t_pdus = conn->r2t_pdus_cnt;
0843     stats->digest_err = 0;
0844     stats->timeout_err = 0;
0845     stats->custom_length = 1;
0846     strcpy(stats->custom[0].desc, "eh_abort_cnt");
0847     stats->custom[0].value = conn->eh_abort_cnt;
0848 }
0849 
0850 /**
0851  * beiscsi_set_params_for_offld - get the parameters for offload
0852  * @beiscsi_conn: pointer to beiscsi_conn
0853  * @params: pointer to offload_params structure
0854  */
0855 static void  beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
0856                       struct beiscsi_offload_params *params)
0857 {
0858     struct iscsi_conn *conn = beiscsi_conn->conn;
0859     struct iscsi_session *session = conn->session;
0860 
0861     AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
0862               params, session->max_burst);
0863     AMAP_SET_BITS(struct amap_beiscsi_offload_params,
0864               max_send_data_segment_length, params,
0865               conn->max_xmit_dlength);
0866     AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
0867               params, session->first_burst);
0868     AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
0869               session->erl);
0870     AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
0871               conn->datadgst_en);
0872     AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
0873               conn->hdrdgst_en);
0874     AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
0875               session->initial_r2t_en);
0876     AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
0877               session->imm_data_en);
0878     AMAP_SET_BITS(struct amap_beiscsi_offload_params,
0879               data_seq_inorder, params,
0880               session->dataseq_inorder_en);
0881     AMAP_SET_BITS(struct amap_beiscsi_offload_params,
0882               pdu_seq_inorder, params,
0883               session->pdu_inorder_en);
0884     AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_r2t, params,
0885               session->max_r2t);
0886     AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
0887               (conn->exp_statsn - 1));
0888     AMAP_SET_BITS(struct amap_beiscsi_offload_params,
0889               max_recv_data_segment_length, params,
0890               conn->max_recv_dlength);
0891 
0892 }
0893 
0894 /**
0895  * beiscsi_conn_start - offload of session to chip
0896  * @cls_conn: pointer to beiscsi_conn
0897  */
0898 int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
0899 {
0900     struct iscsi_conn *conn = cls_conn->dd_data;
0901     struct beiscsi_conn *beiscsi_conn = conn->dd_data;
0902     struct beiscsi_endpoint *beiscsi_ep;
0903     struct beiscsi_offload_params params;
0904     struct beiscsi_hba *phba;
0905 
0906     phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
0907 
0908     if (!beiscsi_hba_is_online(phba)) {
0909         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0910                 "BS_%d : HBA in error 0x%lx\n", phba->state);
0911         return -EBUSY;
0912     }
0913     beiscsi_log(beiscsi_conn->phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0914             "BS_%d : In beiscsi_conn_start\n");
0915 
0916     memset(&params, 0, sizeof(struct beiscsi_offload_params));
0917     beiscsi_ep = beiscsi_conn->ep;
0918     if (!beiscsi_ep)
0919         beiscsi_log(beiscsi_conn->phba, KERN_ERR,
0920                 BEISCSI_LOG_CONFIG,
0921                 "BS_%d : In beiscsi_conn_start , no beiscsi_ep\n");
0922 
0923     beiscsi_conn->login_in_progress = 0;
0924     beiscsi_set_params_for_offld(beiscsi_conn, &params);
0925     beiscsi_offload_connection(beiscsi_conn, &params);
0926     iscsi_conn_start(cls_conn);
0927     return 0;
0928 }
0929 
0930 /**
0931  * beiscsi_get_cid - Allocate a cid
0932  * @phba: The phba instance
0933  */
0934 static int beiscsi_get_cid(struct beiscsi_hba *phba)
0935 {
0936     uint16_t cid_avlbl_ulp0, cid_avlbl_ulp1;
0937     unsigned short cid, cid_from_ulp;
0938     struct ulp_cid_info *cid_info;
0939 
0940     /* Find the ULP which has more CID available */
0941     cid_avlbl_ulp0 = (phba->cid_array_info[BEISCSI_ULP0]) ?
0942               BEISCSI_ULP0_AVLBL_CID(phba) : 0;
0943     cid_avlbl_ulp1 = (phba->cid_array_info[BEISCSI_ULP1]) ?
0944               BEISCSI_ULP1_AVLBL_CID(phba) : 0;
0945     cid_from_ulp = (cid_avlbl_ulp0 > cid_avlbl_ulp1) ?
0946             BEISCSI_ULP0 : BEISCSI_ULP1;
0947     /**
0948      * If iSCSI protocol is loaded only on ULP 0, and when cid_avlbl_ulp
0949      * is ZERO for both, ULP 1 is returned.
0950      * Check if ULP is loaded before getting new CID.
0951      */
0952     if (!test_bit(cid_from_ulp, (void *)&phba->fw_config.ulp_supported))
0953         return BE_INVALID_CID;
0954 
0955     cid_info = phba->cid_array_info[cid_from_ulp];
0956     cid = cid_info->cid_array[cid_info->cid_alloc];
0957     if (!cid_info->avlbl_cids || cid == BE_INVALID_CID) {
0958         __beiscsi_log(phba, KERN_ERR,
0959                 "BS_%d : failed to get cid: available %u:%u\n",
0960                 cid_info->avlbl_cids, cid_info->cid_free);
0961         return BE_INVALID_CID;
0962     }
0963     /* empty the slot */
0964     cid_info->cid_array[cid_info->cid_alloc++] = BE_INVALID_CID;
0965     if (cid_info->cid_alloc == BEISCSI_GET_CID_COUNT(phba, cid_from_ulp))
0966         cid_info->cid_alloc = 0;
0967     cid_info->avlbl_cids--;
0968     return cid;
0969 }
0970 
0971 /**
0972  * beiscsi_put_cid - Free the cid
0973  * @phba: The phba for which the cid is being freed
0974  * @cid: The cid to free
0975  */
0976 static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
0977 {
0978     uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
0979     struct hwi_wrb_context *pwrb_context;
0980     struct hwi_controller *phwi_ctrlr;
0981     struct ulp_cid_info *cid_info;
0982     uint16_t cid_post_ulp;
0983 
0984     phwi_ctrlr = phba->phwi_ctrlr;
0985     pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
0986     cid_post_ulp = pwrb_context->ulp_num;
0987 
0988     cid_info = phba->cid_array_info[cid_post_ulp];
0989     /* fill only in empty slot */
0990     if (cid_info->cid_array[cid_info->cid_free] != BE_INVALID_CID) {
0991         __beiscsi_log(phba, KERN_ERR,
0992                   "BS_%d : failed to put cid %u: available %u:%u\n",
0993                   cid, cid_info->avlbl_cids, cid_info->cid_free);
0994         return;
0995     }
0996     cid_info->cid_array[cid_info->cid_free++] = cid;
0997     if (cid_info->cid_free == BEISCSI_GET_CID_COUNT(phba, cid_post_ulp))
0998         cid_info->cid_free = 0;
0999     cid_info->avlbl_cids++;
1000 }
1001 
1002 /**
1003  * beiscsi_free_ep - free endpoint
1004  * @beiscsi_ep: pointer to device endpoint struct
1005  */
1006 static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
1007 {
1008     struct beiscsi_hba *phba = beiscsi_ep->phba;
1009     struct beiscsi_conn *beiscsi_conn;
1010 
1011     beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
1012     beiscsi_ep->phba = NULL;
1013     /* clear this to track freeing in beiscsi_ep_disconnect */
1014     phba->ep_array[BE_GET_CRI_FROM_CID(beiscsi_ep->ep_cid)] = NULL;
1015 
1016     /**
1017      * Check if any connection resource allocated by driver
1018      * is to be freed.This case occurs when target redirection
1019      * or connection retry is done.
1020      **/
1021     if (!beiscsi_ep->conn)
1022         return;
1023 
1024     beiscsi_conn = beiscsi_ep->conn;
1025     /**
1026      * Break ep->conn link here so that completions after
1027      * this are ignored.
1028      */
1029     beiscsi_ep->conn = NULL;
1030     if (beiscsi_conn->login_in_progress) {
1031         beiscsi_free_mgmt_task_handles(beiscsi_conn,
1032                            beiscsi_conn->task);
1033         beiscsi_conn->login_in_progress = 0;
1034     }
1035 }
1036 
1037 /**
1038  * beiscsi_open_conn - Ask FW to open a TCP connection
1039  * @ep: pointer to device endpoint struct
1040  * @src_addr: The source IP address
1041  * @dst_addr: The Destination  IP address
1042  * @non_blocking: blocking or non-blocking call
1043  *
1044  * Asks the FW to open a TCP connection
1045  */
1046 static int beiscsi_open_conn(struct iscsi_endpoint *ep,
1047                  struct sockaddr *src_addr,
1048                  struct sockaddr *dst_addr, int non_blocking)
1049 {
1050     struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
1051     struct beiscsi_hba *phba = beiscsi_ep->phba;
1052     struct tcp_connect_and_offload_out *ptcpcnct_out;
1053     struct be_dma_mem nonemb_cmd;
1054     unsigned int tag, req_memsize;
1055     int ret = -ENOMEM;
1056 
1057     beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1058             "BS_%d : In beiscsi_open_conn\n");
1059 
1060     beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
1061     if (beiscsi_ep->ep_cid == BE_INVALID_CID) {
1062         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1063                 "BS_%d : No free cid available\n");
1064         return ret;
1065     }
1066 
1067     beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1068             "BS_%d : In beiscsi_open_conn, ep_cid=%d\n",
1069             beiscsi_ep->ep_cid);
1070 
1071     phba->ep_array[BE_GET_CRI_FROM_CID
1072                (beiscsi_ep->ep_cid)] = ep;
1073 
1074     beiscsi_ep->cid_vld = 0;
1075 
1076     if (is_chip_be2_be3r(phba))
1077         req_memsize = sizeof(struct tcp_connect_and_offload_in);
1078     else
1079         req_memsize = sizeof(struct tcp_connect_and_offload_in_v1);
1080 
1081     nonemb_cmd.va = dma_alloc_coherent(&phba->ctrl.pdev->dev,
1082                 req_memsize,
1083                 &nonemb_cmd.dma, GFP_KERNEL);
1084     if (nonemb_cmd.va == NULL) {
1085 
1086         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1087                 "BS_%d : Failed to allocate memory for"
1088                 " mgmt_open_connection\n");
1089 
1090         beiscsi_free_ep(beiscsi_ep);
1091         return -ENOMEM;
1092     }
1093     nonemb_cmd.size = req_memsize;
1094     memset(nonemb_cmd.va, 0, nonemb_cmd.size);
1095     tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep, &nonemb_cmd);
1096     if (!tag) {
1097         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1098                 "BS_%d : mgmt_open_connection Failed for cid=%d\n",
1099                 beiscsi_ep->ep_cid);
1100 
1101         dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
1102                     nonemb_cmd.va, nonemb_cmd.dma);
1103         beiscsi_free_ep(beiscsi_ep);
1104         return -EAGAIN;
1105     }
1106 
1107     ret = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
1108     if (ret) {
1109         beiscsi_log(phba, KERN_ERR,
1110                 BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
1111                 "BS_%d : mgmt_open_connection Failed");
1112 
1113         if (ret != -EBUSY)
1114             dma_free_coherent(&phba->ctrl.pdev->dev,
1115                     nonemb_cmd.size, nonemb_cmd.va,
1116                     nonemb_cmd.dma);
1117 
1118         beiscsi_free_ep(beiscsi_ep);
1119         return ret;
1120     }
1121 
1122     ptcpcnct_out = (struct tcp_connect_and_offload_out *)nonemb_cmd.va;
1123     beiscsi_ep = ep->dd_data;
1124     beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
1125     beiscsi_ep->cid_vld = 1;
1126     beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1127             "BS_%d : mgmt_open_connection Success\n");
1128 
1129     dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
1130                 nonemb_cmd.va, nonemb_cmd.dma);
1131     return 0;
1132 }
1133 
1134 /**
1135  * beiscsi_ep_connect - Ask chip to create TCP Conn
1136  * @shost: Pointer to scsi_host structure
1137  * @dst_addr: The IP address of Target
1138  * @non_blocking: blocking or non-blocking call
1139  *
1140  * This routines first asks chip to create a connection and then allocates an EP
1141  */
1142 struct iscsi_endpoint *
1143 beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
1144            int non_blocking)
1145 {
1146     struct beiscsi_hba *phba;
1147     struct beiscsi_endpoint *beiscsi_ep;
1148     struct iscsi_endpoint *ep;
1149     int ret;
1150 
1151     if (!shost) {
1152         ret = -ENXIO;
1153         pr_err("beiscsi_ep_connect shost is NULL\n");
1154         return ERR_PTR(ret);
1155     }
1156 
1157     phba = iscsi_host_priv(shost);
1158     if (!beiscsi_hba_is_online(phba)) {
1159         ret = -EIO;
1160         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1161                 "BS_%d : HBA in error 0x%lx\n", phba->state);
1162         return ERR_PTR(ret);
1163     }
1164     if (!test_bit(BEISCSI_HBA_LINK_UP, &phba->state)) {
1165         ret = -EBUSY;
1166         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1167                 "BS_%d : The Adapter Port state is Down!!!\n");
1168         return ERR_PTR(ret);
1169     }
1170 
1171     ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
1172     if (!ep) {
1173         ret = -ENOMEM;
1174         return ERR_PTR(ret);
1175     }
1176 
1177     beiscsi_ep = ep->dd_data;
1178     beiscsi_ep->phba = phba;
1179     beiscsi_ep->openiscsi_ep = ep;
1180     ret = beiscsi_open_conn(ep, NULL, dst_addr, non_blocking);
1181     if (ret) {
1182         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1183                 "BS_%d : Failed in beiscsi_open_conn\n");
1184         goto free_ep;
1185     }
1186 
1187     return ep;
1188 
1189 free_ep:
1190     iscsi_destroy_endpoint(ep);
1191     return ERR_PTR(ret);
1192 }
1193 
1194 /**
1195  * beiscsi_ep_poll - Poll to see if connection is established
1196  * @ep: endpoint to be used
1197  * @timeout_ms: timeout specified in millisecs
1198  *
1199  * Poll to see if TCP connection established
1200  */
1201 int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1202 {
1203     struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
1204 
1205     beiscsi_log(beiscsi_ep->phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1206             "BS_%d : In  beiscsi_ep_poll\n");
1207 
1208     if (beiscsi_ep->cid_vld == 1)
1209         return 1;
1210     else
1211         return 0;
1212 }
1213 
1214 /**
1215  * beiscsi_flush_cq()- Flush the CQ created.
1216  * @phba: ptr device priv structure.
1217  *
1218  * Before the connection resource are freed flush
1219  * all the CQ enteries
1220  **/
1221 static void beiscsi_flush_cq(struct beiscsi_hba *phba)
1222 {
1223     uint16_t i;
1224     struct be_eq_obj *pbe_eq;
1225     struct hwi_controller *phwi_ctrlr;
1226     struct hwi_context_memory *phwi_context;
1227 
1228     phwi_ctrlr = phba->phwi_ctrlr;
1229     phwi_context = phwi_ctrlr->phwi_ctxt;
1230 
1231     for (i = 0; i < phba->num_cpus; i++) {
1232         pbe_eq = &phwi_context->be_eq[i];
1233         irq_poll_disable(&pbe_eq->iopoll);
1234         beiscsi_process_cq(pbe_eq, BE2_MAX_NUM_CQ_PROC);
1235         irq_poll_enable(&pbe_eq->iopoll);
1236     }
1237 }
1238 
1239 /**
1240  * beiscsi_conn_close - Invalidate and upload connection
1241  * @beiscsi_ep: pointer to device endpoint struct
1242  *
1243  * Returns 0 on success,  -1 on failure.
1244  */
1245 static int beiscsi_conn_close(struct beiscsi_endpoint *beiscsi_ep)
1246 {
1247     struct beiscsi_hba *phba = beiscsi_ep->phba;
1248     unsigned int tag, attempts;
1249     int ret;
1250 
1251     /**
1252      * Without successfully invalidating and uploading connection
1253      * driver can't reuse the CID so attempt more than once.
1254      */
1255     attempts = 0;
1256     while (attempts++ < 3) {
1257         tag = beiscsi_invalidate_cxn(phba, beiscsi_ep);
1258         if (tag) {
1259             ret = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
1260             if (!ret)
1261                 break;
1262             beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1263                     "BS_%d : invalidate conn failed cid %d\n",
1264                     beiscsi_ep->ep_cid);
1265         }
1266     }
1267 
1268     /* wait for all completions to arrive, then process them */
1269     msleep(250);
1270     /* flush CQ entries */
1271     beiscsi_flush_cq(phba);
1272 
1273     if (attempts > 3)
1274         return -1;
1275 
1276     attempts = 0;
1277     while (attempts++ < 3) {
1278         tag = beiscsi_upload_cxn(phba, beiscsi_ep);
1279         if (tag) {
1280             ret = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
1281             if (!ret)
1282                 break;
1283             beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1284                     "BS_%d : upload conn failed cid %d\n",
1285                     beiscsi_ep->ep_cid);
1286         }
1287     }
1288     if (attempts > 3)
1289         return -1;
1290 
1291     return 0;
1292 }
1293 
1294 /**
1295  * beiscsi_ep_disconnect - Tears down the TCP connection
1296  * @ep: endpoint to be used
1297  *
1298  * Tears down the TCP connection
1299  */
1300 void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
1301 {
1302     struct beiscsi_endpoint *beiscsi_ep;
1303     struct beiscsi_hba *phba;
1304     uint16_t cri_index;
1305 
1306     beiscsi_ep = ep->dd_data;
1307     phba = beiscsi_ep->phba;
1308     beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1309             "BS_%d : In beiscsi_ep_disconnect for ep_cid = %u\n",
1310             beiscsi_ep->ep_cid);
1311 
1312     cri_index = BE_GET_CRI_FROM_CID(beiscsi_ep->ep_cid);
1313     if (!phba->ep_array[cri_index]) {
1314         __beiscsi_log(phba, KERN_ERR,
1315                   "BS_%d : ep_array at %u cid %u empty\n",
1316                   cri_index,
1317                   beiscsi_ep->ep_cid);
1318         return;
1319     }
1320 
1321     if (!beiscsi_hba_is_online(phba)) {
1322         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1323                 "BS_%d : HBA in error 0x%lx\n", phba->state);
1324     } else {
1325         /**
1326          * Make CID available even if close fails.
1327          * If not freed, FW might fail open using the CID.
1328          */
1329         if (beiscsi_conn_close(beiscsi_ep) < 0)
1330             __beiscsi_log(phba, KERN_ERR,
1331                       "BS_%d : close conn failed cid %d\n",
1332                       beiscsi_ep->ep_cid);
1333     }
1334 
1335     beiscsi_free_ep(beiscsi_ep);
1336     if (!phba->conn_table[cri_index])
1337         __beiscsi_log(phba, KERN_ERR,
1338                   "BS_%d : conn_table empty at %u: cid %u\n",
1339                   cri_index, beiscsi_ep->ep_cid);
1340     phba->conn_table[cri_index] = NULL;
1341     iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
1342 }
1343 
1344 umode_t beiscsi_attr_is_visible(int param_type, int param)
1345 {
1346     switch (param_type) {
1347     case ISCSI_NET_PARAM:
1348         switch (param) {
1349         case ISCSI_NET_PARAM_IFACE_ENABLE:
1350         case ISCSI_NET_PARAM_IPV4_ADDR:
1351         case ISCSI_NET_PARAM_IPV4_SUBNET:
1352         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1353         case ISCSI_NET_PARAM_IPV4_GW:
1354         case ISCSI_NET_PARAM_IPV6_ADDR:
1355         case ISCSI_NET_PARAM_VLAN_ID:
1356         case ISCSI_NET_PARAM_VLAN_PRIORITY:
1357         case ISCSI_NET_PARAM_VLAN_ENABLED:
1358             return S_IRUGO;
1359         default:
1360             return 0;
1361         }
1362     case ISCSI_HOST_PARAM:
1363         switch (param) {
1364         case ISCSI_HOST_PARAM_HWADDRESS:
1365         case ISCSI_HOST_PARAM_INITIATOR_NAME:
1366         case ISCSI_HOST_PARAM_PORT_STATE:
1367         case ISCSI_HOST_PARAM_PORT_SPEED:
1368             return S_IRUGO;
1369         default:
1370             return 0;
1371         }
1372     case ISCSI_PARAM:
1373         switch (param) {
1374         case ISCSI_PARAM_MAX_RECV_DLENGTH:
1375         case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1376         case ISCSI_PARAM_HDRDGST_EN:
1377         case ISCSI_PARAM_DATADGST_EN:
1378         case ISCSI_PARAM_CONN_ADDRESS:
1379         case ISCSI_PARAM_CONN_PORT:
1380         case ISCSI_PARAM_EXP_STATSN:
1381         case ISCSI_PARAM_PERSISTENT_ADDRESS:
1382         case ISCSI_PARAM_PERSISTENT_PORT:
1383         case ISCSI_PARAM_PING_TMO:
1384         case ISCSI_PARAM_RECV_TMO:
1385         case ISCSI_PARAM_INITIAL_R2T_EN:
1386         case ISCSI_PARAM_MAX_R2T:
1387         case ISCSI_PARAM_IMM_DATA_EN:
1388         case ISCSI_PARAM_FIRST_BURST:
1389         case ISCSI_PARAM_MAX_BURST:
1390         case ISCSI_PARAM_PDU_INORDER_EN:
1391         case ISCSI_PARAM_DATASEQ_INORDER_EN:
1392         case ISCSI_PARAM_ERL:
1393         case ISCSI_PARAM_TARGET_NAME:
1394         case ISCSI_PARAM_TPGT:
1395         case ISCSI_PARAM_USERNAME:
1396         case ISCSI_PARAM_PASSWORD:
1397         case ISCSI_PARAM_USERNAME_IN:
1398         case ISCSI_PARAM_PASSWORD_IN:
1399         case ISCSI_PARAM_FAST_ABORT:
1400         case ISCSI_PARAM_ABORT_TMO:
1401         case ISCSI_PARAM_LU_RESET_TMO:
1402         case ISCSI_PARAM_IFACE_NAME:
1403         case ISCSI_PARAM_INITIATOR_NAME:
1404             return S_IRUGO;
1405         default:
1406             return 0;
1407         }
1408     }
1409 
1410     return 0;
1411 }