linux/drivers/scsi/qla4xxx/ql4_os.c
<<
>>
Prefs
   1/*
   2 * QLogic iSCSI HBA Driver
   3 * Copyright (c)  2003-2013 QLogic Corporation
   4 *
   5 * See LICENSE.qla4xxx for copyright and licensing details.
   6 */
   7#include <linux/moduleparam.h>
   8#include <linux/slab.h>
   9#include <linux/blkdev.h>
  10#include <linux/iscsi_boot_sysfs.h>
  11#include <linux/inet.h>
  12
  13#include <scsi/scsi_tcq.h>
  14#include <scsi/scsicam.h>
  15
  16#include "ql4_def.h"
  17#include "ql4_version.h"
  18#include "ql4_glbl.h"
  19#include "ql4_dbg.h"
  20#include "ql4_inline.h"
  21#include "ql4_83xx.h"
  22
  23/*
  24 * Driver version
  25 */
  26static char qla4xxx_version_str[40];
  27
  28/*
  29 * SRB allocation cache
  30 */
  31static struct kmem_cache *srb_cachep;
  32
  33/*
  34 * Module parameter information and variables
  35 */
  36static int ql4xdisablesysfsboot = 1;
  37module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
  38MODULE_PARM_DESC(ql4xdisablesysfsboot,
  39                 " Set to disable exporting boot targets to sysfs.\n"
  40                 "\t\t  0 - Export boot targets\n"
  41                 "\t\t  1 - Do not export boot targets (Default)");
  42
  43int ql4xdontresethba;
  44module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
  45MODULE_PARM_DESC(ql4xdontresethba,
  46                 " Don't reset the HBA for driver recovery.\n"
  47                 "\t\t  0 - It will reset HBA (Default)\n"
  48                 "\t\t  1 - It will NOT reset HBA");
  49
  50int ql4xextended_error_logging;
  51module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
  52MODULE_PARM_DESC(ql4xextended_error_logging,
  53                 " Option to enable extended error logging.\n"
  54                 "\t\t  0 - no logging (Default)\n"
  55                 "\t\t  2 - debug logging");
  56
  57int ql4xenablemsix = 1;
  58module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
  59MODULE_PARM_DESC(ql4xenablemsix,
  60                 " Set to enable MSI or MSI-X interrupt mechanism.\n"
  61                 "\t\t  0 = enable INTx interrupt mechanism.\n"
  62                 "\t\t  1 = enable MSI-X interrupt mechanism (Default).\n"
  63                 "\t\t  2 = enable MSI interrupt mechanism.");
  64
  65#define QL4_DEF_QDEPTH 32
  66static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
  67module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
  68MODULE_PARM_DESC(ql4xmaxqdepth,
  69                 " Maximum queue depth to report for target devices.\n"
  70                 "\t\t  Default: 32.");
  71
  72static int ql4xqfulltracking = 1;
  73module_param(ql4xqfulltracking, int, S_IRUGO | S_IWUSR);
  74MODULE_PARM_DESC(ql4xqfulltracking,
  75                 " Enable or disable dynamic tracking and adjustment of\n"
  76                 "\t\t scsi device queue depth.\n"
  77                 "\t\t  0 - Disable.\n"
  78                 "\t\t  1 - Enable. (Default)");
  79
  80static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
  81module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
  82MODULE_PARM_DESC(ql4xsess_recovery_tmo,
  83                " Target Session Recovery Timeout.\n"
  84                "\t\t  Default: 120 sec.");
  85
  86int ql4xmdcapmask = 0;
  87module_param(ql4xmdcapmask, int, S_IRUGO);
  88MODULE_PARM_DESC(ql4xmdcapmask,
  89                 " Set the Minidump driver capture mask level.\n"
  90                 "\t\t  Default is 0 (firmware default capture mask)\n"
  91                 "\t\t  Can be set to 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF");
  92
  93int ql4xenablemd = 1;
  94module_param(ql4xenablemd, int, S_IRUGO | S_IWUSR);
  95MODULE_PARM_DESC(ql4xenablemd,
  96                 " Set to enable minidump.\n"
  97                 "\t\t  0 - disable minidump\n"
  98                 "\t\t  1 - enable minidump (Default)");
  99
 100static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
 101/*
 102 * SCSI host template entry points
 103 */
 104static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
 105
 106/*
 107 * iSCSI template entry points
 108 */
 109static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
 110                                     enum iscsi_param param, char *buf);
 111static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
 112                                  enum iscsi_param param, char *buf);
 113static int qla4xxx_host_get_param(struct Scsi_Host *shost,
 114                                  enum iscsi_host_param param, char *buf);
 115static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
 116                                   uint32_t len);
 117static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
 118                                   enum iscsi_param_type param_type,
 119                                   int param, char *buf);
 120static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
 121static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
 122                                                 struct sockaddr *dst_addr,
 123                                                 int non_blocking);
 124static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
 125static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
 126static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
 127                                enum iscsi_param param, char *buf);
 128static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
 129static struct iscsi_cls_conn *
 130qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
 131static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
 132                             struct iscsi_cls_conn *cls_conn,
 133                             uint64_t transport_fd, int is_leading);
 134static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
 135static struct iscsi_cls_session *
 136qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
 137                        uint16_t qdepth, uint32_t initial_cmdsn);
 138static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
 139static void qla4xxx_task_work(struct work_struct *wdata);
 140static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
 141static int qla4xxx_task_xmit(struct iscsi_task *);
 142static void qla4xxx_task_cleanup(struct iscsi_task *);
 143static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
 144static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
 145                                   struct iscsi_stats *stats);
 146static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
 147                             uint32_t iface_type, uint32_t payload_size,
 148                             uint32_t pid, struct sockaddr *dst_addr);
 149static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
 150                                 uint32_t *num_entries, char *buf);
 151static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx);
 152static int qla4xxx_set_chap_entry(struct Scsi_Host *shost, void  *data,
 153                                  int len);
 154static int qla4xxx_get_host_stats(struct Scsi_Host *shost, char *buf, int len);
 155
 156/*
 157 * SCSI host template entry points
 158 */
 159static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
 160static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
 161static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
 162static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
 163static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
 164static int qla4xxx_slave_alloc(struct scsi_device *device);
 165static int qla4xxx_slave_configure(struct scsi_device *device);
 166static void qla4xxx_slave_destroy(struct scsi_device *sdev);
 167static umode_t qla4_attr_is_visible(int param_type, int param);
 168static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
 169static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
 170                                      int reason);
 171
 172/*
 173 * iSCSI Flash DDB sysfs entry points
 174 */
 175static int
 176qla4xxx_sysfs_ddb_set_param(struct iscsi_bus_flash_session *fnode_sess,
 177                            struct iscsi_bus_flash_conn *fnode_conn,
 178                            void *data, int len);
 179static int
 180qla4xxx_sysfs_ddb_get_param(struct iscsi_bus_flash_session *fnode_sess,
 181                            int param, char *buf);
 182static int qla4xxx_sysfs_ddb_add(struct Scsi_Host *shost, const char *buf,
 183                                 int len);
 184static int
 185qla4xxx_sysfs_ddb_delete(struct iscsi_bus_flash_session *fnode_sess);
 186static int qla4xxx_sysfs_ddb_login(struct iscsi_bus_flash_session *fnode_sess,
 187                                   struct iscsi_bus_flash_conn *fnode_conn);
 188static int qla4xxx_sysfs_ddb_logout(struct iscsi_bus_flash_session *fnode_sess,
 189                                    struct iscsi_bus_flash_conn *fnode_conn);
 190static int qla4xxx_sysfs_ddb_logout_sid(struct iscsi_cls_session *cls_sess);
 191
 192static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
 193    QLA82XX_LEGACY_INTR_CONFIG;
 194
 195static struct scsi_host_template qla4xxx_driver_template = {
 196        .module                 = THIS_MODULE,
 197        .name                   = DRIVER_NAME,
 198        .proc_name              = DRIVER_NAME,
 199        .queuecommand           = qla4xxx_queuecommand,
 200
 201        .eh_abort_handler       = qla4xxx_eh_abort,
 202        .eh_device_reset_handler = qla4xxx_eh_device_reset,
 203        .eh_target_reset_handler = qla4xxx_eh_target_reset,
 204        .eh_host_reset_handler  = qla4xxx_eh_host_reset,
 205        .eh_timed_out           = qla4xxx_eh_cmd_timed_out,
 206
 207        .slave_configure        = qla4xxx_slave_configure,
 208        .slave_alloc            = qla4xxx_slave_alloc,
 209        .slave_destroy          = qla4xxx_slave_destroy,
 210        .change_queue_depth     = qla4xxx_change_queue_depth,
 211
 212        .this_id                = -1,
 213        .cmd_per_lun            = 3,
 214        .use_clustering         = ENABLE_CLUSTERING,
 215        .sg_tablesize           = SG_ALL,
 216
 217        .max_sectors            = 0xFFFF,
 218        .shost_attrs            = qla4xxx_host_attrs,
 219        .host_reset             = qla4xxx_host_reset,
 220        .vendor_id              = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
 221};
 222
 223static struct iscsi_transport qla4xxx_iscsi_transport = {
 224        .owner                  = THIS_MODULE,
 225        .name                   = DRIVER_NAME,
 226        .caps                   = CAP_TEXT_NEGO |
 227                                  CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
 228                                  CAP_DATADGST | CAP_LOGIN_OFFLOAD |
 229                                  CAP_MULTI_R2T,
 230        .attr_is_visible        = qla4_attr_is_visible,
 231        .create_session         = qla4xxx_session_create,
 232        .destroy_session        = qla4xxx_session_destroy,
 233        .start_conn             = qla4xxx_conn_start,
 234        .create_conn            = qla4xxx_conn_create,
 235        .bind_conn              = qla4xxx_conn_bind,
 236        .stop_conn              = iscsi_conn_stop,
 237        .destroy_conn           = qla4xxx_conn_destroy,
 238        .set_param              = iscsi_set_param,
 239        .get_conn_param         = qla4xxx_conn_get_param,
 240        .get_session_param      = qla4xxx_session_get_param,
 241        .get_ep_param           = qla4xxx_get_ep_param,
 242        .ep_connect             = qla4xxx_ep_connect,
 243        .ep_poll                = qla4xxx_ep_poll,
 244        .ep_disconnect          = qla4xxx_ep_disconnect,
 245        .get_stats              = qla4xxx_conn_get_stats,
 246        .send_pdu               = iscsi_conn_send_pdu,
 247        .xmit_task              = qla4xxx_task_xmit,
 248        .cleanup_task           = qla4xxx_task_cleanup,
 249        .alloc_pdu              = qla4xxx_alloc_pdu,
 250
 251        .get_host_param         = qla4xxx_host_get_param,
 252        .set_iface_param        = qla4xxx_iface_set_param,
 253        .get_iface_param        = qla4xxx_get_iface_param,
 254        .bsg_request            = qla4xxx_bsg_request,
 255        .send_ping              = qla4xxx_send_ping,
 256        .get_chap               = qla4xxx_get_chap_list,
 257        .delete_chap            = qla4xxx_delete_chap,
 258        .set_chap               = qla4xxx_set_chap_entry,
 259        .get_flashnode_param    = qla4xxx_sysfs_ddb_get_param,
 260        .set_flashnode_param    = qla4xxx_sysfs_ddb_set_param,
 261        .new_flashnode          = qla4xxx_sysfs_ddb_add,
 262        .del_flashnode          = qla4xxx_sysfs_ddb_delete,
 263        .login_flashnode        = qla4xxx_sysfs_ddb_login,
 264        .logout_flashnode       = qla4xxx_sysfs_ddb_logout,
 265        .logout_flashnode_sid   = qla4xxx_sysfs_ddb_logout_sid,
 266        .get_host_stats         = qla4xxx_get_host_stats,
 267};
 268
 269static struct scsi_transport_template *qla4xxx_scsi_transport;
 270
 271static int qla4xxx_isp_check_reg(struct scsi_qla_host *ha)
 272{
 273        uint32_t reg_val = 0;
 274        int rval = QLA_SUCCESS;
 275
 276        if (is_qla8022(ha))
 277                reg_val = readl(&ha->qla4_82xx_reg->host_status);
 278        else if (is_qla8032(ha) || is_qla8042(ha))
 279                reg_val = qla4_8xxx_rd_direct(ha, QLA8XXX_PEG_ALIVE_COUNTER);
 280        else
 281                reg_val = readw(&ha->reg->ctrl_status);
 282
 283        if (reg_val == QL4_ISP_REG_DISCONNECT)
 284                rval = QLA_ERROR;
 285
 286        return rval;
 287}
 288
 289static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
 290                             uint32_t iface_type, uint32_t payload_size,
 291                             uint32_t pid, struct sockaddr *dst_addr)
 292{
 293        struct scsi_qla_host *ha = to_qla_host(shost);
 294        struct sockaddr_in *addr;
 295        struct sockaddr_in6 *addr6;
 296        uint32_t options = 0;
 297        uint8_t ipaddr[IPv6_ADDR_LEN];
 298        int rval;
 299
 300        memset(ipaddr, 0, IPv6_ADDR_LEN);
 301        /* IPv4 to IPv4 */
 302        if ((iface_type == ISCSI_IFACE_TYPE_IPV4) &&
 303            (dst_addr->sa_family == AF_INET)) {
 304                addr = (struct sockaddr_in *)dst_addr;
 305                memcpy(ipaddr, &addr->sin_addr.s_addr, IP_ADDR_LEN);
 306                DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv4 Ping src: %pI4 "
 307                                  "dest: %pI4\n", __func__,
 308                                  &ha->ip_config.ip_address, ipaddr));
 309                rval = qla4xxx_ping_iocb(ha, options, payload_size, pid,
 310                                         ipaddr);
 311                if (rval)
 312                        rval = -EINVAL;
 313        } else if ((iface_type == ISCSI_IFACE_TYPE_IPV6) &&
 314                   (dst_addr->sa_family == AF_INET6)) {
 315                /* IPv6 to IPv6 */
 316                addr6 = (struct sockaddr_in6 *)dst_addr;
 317                memcpy(ipaddr, &addr6->sin6_addr.in6_u.u6_addr8, IPv6_ADDR_LEN);
 318
 319                options |= PING_IPV6_PROTOCOL_ENABLE;
 320
 321                /* Ping using LinkLocal address */
 322                if ((iface_num == 0) || (iface_num == 1)) {
 323                        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: LinkLocal Ping "
 324                                          "src: %pI6 dest: %pI6\n", __func__,
 325                                          &ha->ip_config.ipv6_link_local_addr,
 326                                          ipaddr));
 327                        options |= PING_IPV6_LINKLOCAL_ADDR;
 328                        rval = qla4xxx_ping_iocb(ha, options, payload_size,
 329                                                 pid, ipaddr);
 330                } else {
 331                        ql4_printk(KERN_WARNING, ha, "%s: iface num = %d "
 332                                   "not supported\n", __func__, iface_num);
 333                        rval = -ENOSYS;
 334                        goto exit_send_ping;
 335                }
 336
 337                /*
 338                 * If ping using LinkLocal address fails, try ping using
 339                 * IPv6 address
 340                 */
 341                if (rval != QLA_SUCCESS) {
 342                        options &= ~PING_IPV6_LINKLOCAL_ADDR;
 343                        if (iface_num == 0) {
 344                                options |= PING_IPV6_ADDR0;
 345                                DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
 346                                                  "Ping src: %pI6 "
 347                                                  "dest: %pI6\n", __func__,
 348                                                  &ha->ip_config.ipv6_addr0,
 349                                                  ipaddr));
 350                        } else if (iface_num == 1) {
 351                                options |= PING_IPV6_ADDR1;
 352                                DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
 353                                                  "Ping src: %pI6 "
 354                                                  "dest: %pI6\n", __func__,
 355                                                  &ha->ip_config.ipv6_addr1,
 356                                                  ipaddr));
 357                        }
 358                        rval = qla4xxx_ping_iocb(ha, options, payload_size,
 359                                                 pid, ipaddr);
 360                        if (rval)
 361                                rval = -EINVAL;
 362                }
 363        } else
 364                rval = -ENOSYS;
 365exit_send_ping:
 366        return rval;
 367}
 368
 369static umode_t qla4_attr_is_visible(int param_type, int param)
 370{
 371        switch (param_type) {
 372        case ISCSI_HOST_PARAM:
 373                switch (param) {
 374                case ISCSI_HOST_PARAM_HWADDRESS:
 375                case ISCSI_HOST_PARAM_IPADDRESS:
 376                case ISCSI_HOST_PARAM_INITIATOR_NAME:
 377                case ISCSI_HOST_PARAM_PORT_STATE:
 378                case ISCSI_HOST_PARAM_PORT_SPEED:
 379                        return S_IRUGO;
 380                default:
 381                        return 0;
 382                }
 383        case ISCSI_PARAM:
 384                switch (param) {
 385                case ISCSI_PARAM_PERSISTENT_ADDRESS:
 386                case ISCSI_PARAM_PERSISTENT_PORT:
 387                case ISCSI_PARAM_CONN_ADDRESS:
 388                case ISCSI_PARAM_CONN_PORT:
 389                case ISCSI_PARAM_TARGET_NAME:
 390                case ISCSI_PARAM_TPGT:
 391                case ISCSI_PARAM_TARGET_ALIAS:
 392                case ISCSI_PARAM_MAX_BURST:
 393                case ISCSI_PARAM_MAX_R2T:
 394                case ISCSI_PARAM_FIRST_BURST:
 395                case ISCSI_PARAM_MAX_RECV_DLENGTH:
 396                case ISCSI_PARAM_MAX_XMIT_DLENGTH:
 397                case ISCSI_PARAM_IFACE_NAME:
 398                case ISCSI_PARAM_CHAP_OUT_IDX:
 399                case ISCSI_PARAM_CHAP_IN_IDX:
 400                case ISCSI_PARAM_USERNAME:
 401                case ISCSI_PARAM_PASSWORD:
 402                case ISCSI_PARAM_USERNAME_IN:
 403                case ISCSI_PARAM_PASSWORD_IN:
 404                case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
 405                case ISCSI_PARAM_DISCOVERY_SESS:
 406                case ISCSI_PARAM_PORTAL_TYPE:
 407                case ISCSI_PARAM_CHAP_AUTH_EN:
 408                case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
 409                case ISCSI_PARAM_BIDI_CHAP_EN:
 410                case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
 411                case ISCSI_PARAM_DEF_TIME2WAIT:
 412                case ISCSI_PARAM_DEF_TIME2RETAIN:
 413                case ISCSI_PARAM_HDRDGST_EN:
 414                case ISCSI_PARAM_DATADGST_EN:
 415                case ISCSI_PARAM_INITIAL_R2T_EN:
 416                case ISCSI_PARAM_IMM_DATA_EN:
 417                case ISCSI_PARAM_PDU_INORDER_EN:
 418                case ISCSI_PARAM_DATASEQ_INORDER_EN:
 419                case ISCSI_PARAM_MAX_SEGMENT_SIZE:
 420                case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
 421                case ISCSI_PARAM_TCP_WSF_DISABLE:
 422                case ISCSI_PARAM_TCP_NAGLE_DISABLE:
 423                case ISCSI_PARAM_TCP_TIMER_SCALE:
 424                case ISCSI_PARAM_TCP_TIMESTAMP_EN:
 425                case ISCSI_PARAM_TCP_XMIT_WSF:
 426                case ISCSI_PARAM_TCP_RECV_WSF:
 427                case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
 428                case ISCSI_PARAM_IPV4_TOS:
 429                case ISCSI_PARAM_IPV6_TC:
 430                case ISCSI_PARAM_IPV6_FLOW_LABEL:
 431                case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
 432                case ISCSI_PARAM_KEEPALIVE_TMO:
 433                case ISCSI_PARAM_LOCAL_PORT:
 434                case ISCSI_PARAM_ISID:
 435                case ISCSI_PARAM_TSID:
 436                case ISCSI_PARAM_DEF_TASKMGMT_TMO:
 437                case ISCSI_PARAM_ERL:
 438                case ISCSI_PARAM_STATSN:
 439                case ISCSI_PARAM_EXP_STATSN:
 440                case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
 441                case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
 442                case ISCSI_PARAM_LOCAL_IPADDR:
 443                        return S_IRUGO;
 444                default:
 445                        return 0;
 446                }
 447        case ISCSI_NET_PARAM:
 448                switch (param) {
 449                case ISCSI_NET_PARAM_IPV4_ADDR:
 450                case ISCSI_NET_PARAM_IPV4_SUBNET:
 451                case ISCSI_NET_PARAM_IPV4_GW:
 452                case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
 453                case ISCSI_NET_PARAM_IFACE_ENABLE:
 454                case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
 455                case ISCSI_NET_PARAM_IPV6_ADDR:
 456                case ISCSI_NET_PARAM_IPV6_ROUTER:
 457                case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
 458                case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
 459                case ISCSI_NET_PARAM_VLAN_ID:
 460                case ISCSI_NET_PARAM_VLAN_PRIORITY:
 461                case ISCSI_NET_PARAM_VLAN_ENABLED:
 462                case ISCSI_NET_PARAM_MTU:
 463                case ISCSI_NET_PARAM_PORT:
 464                case ISCSI_NET_PARAM_IPADDR_STATE:
 465                case ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE:
 466                case ISCSI_NET_PARAM_IPV6_ROUTER_STATE:
 467                case ISCSI_NET_PARAM_DELAYED_ACK_EN:
 468                case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
 469                case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
 470                case ISCSI_NET_PARAM_TCP_WSF:
 471                case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
 472                case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
 473                case ISCSI_NET_PARAM_CACHE_ID:
 474                case ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN:
 475                case ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN:
 476                case ISCSI_NET_PARAM_IPV4_TOS_EN:
 477                case ISCSI_NET_PARAM_IPV4_TOS:
 478                case ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN:
 479                case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN:
 480                case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID:
 481                case ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN:
 482                case ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN:
 483                case ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID:
 484                case ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN:
 485                case ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE:
 486                case ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN:
 487                case ISCSI_NET_PARAM_REDIRECT_EN:
 488                case ISCSI_NET_PARAM_IPV4_TTL:
 489                case ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN:
 490                case ISCSI_NET_PARAM_IPV6_MLD_EN:
 491                case ISCSI_NET_PARAM_IPV6_FLOW_LABEL:
 492                case ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS:
 493                case ISCSI_NET_PARAM_IPV6_HOP_LIMIT:
 494                case ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO:
 495                case ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME:
 496                case ISCSI_NET_PARAM_IPV6_ND_STALE_TMO:
 497                case ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT:
 498                case ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU:
 499                        return S_IRUGO;
 500                default:
 501                        return 0;
 502                }
 503        case ISCSI_IFACE_PARAM:
 504                switch (param) {
 505                case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
 506                case ISCSI_IFACE_PARAM_HDRDGST_EN:
 507                case ISCSI_IFACE_PARAM_DATADGST_EN:
 508                case ISCSI_IFACE_PARAM_IMM_DATA_EN:
 509                case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
 510                case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
 511                case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
 512                case ISCSI_IFACE_PARAM_ERL:
 513                case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
 514                case ISCSI_IFACE_PARAM_FIRST_BURST:
 515                case ISCSI_IFACE_PARAM_MAX_R2T:
 516                case ISCSI_IFACE_PARAM_MAX_BURST:
 517                case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
 518                case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
 519                case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
 520                case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
 521                case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
 522                case ISCSI_IFACE_PARAM_INITIATOR_NAME:
 523                        return S_IRUGO;
 524                default:
 525                        return 0;
 526                }
 527        case ISCSI_FLASHNODE_PARAM:
 528                switch (param) {
 529                case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
 530                case ISCSI_FLASHNODE_PORTAL_TYPE:
 531                case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
 532                case ISCSI_FLASHNODE_DISCOVERY_SESS:
 533                case ISCSI_FLASHNODE_ENTRY_EN:
 534                case ISCSI_FLASHNODE_HDR_DGST_EN:
 535                case ISCSI_FLASHNODE_DATA_DGST_EN:
 536                case ISCSI_FLASHNODE_IMM_DATA_EN:
 537                case ISCSI_FLASHNODE_INITIAL_R2T_EN:
 538                case ISCSI_FLASHNODE_DATASEQ_INORDER:
 539                case ISCSI_FLASHNODE_PDU_INORDER:
 540                case ISCSI_FLASHNODE_CHAP_AUTH_EN:
 541                case ISCSI_FLASHNODE_SNACK_REQ_EN:
 542                case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
 543                case ISCSI_FLASHNODE_BIDI_CHAP_EN:
 544                case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
 545                case ISCSI_FLASHNODE_ERL:
 546                case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
 547                case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
 548                case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
 549                case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
 550                case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
 551                case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
 552                case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
 553                case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
 554                case ISCSI_FLASHNODE_FIRST_BURST:
 555                case ISCSI_FLASHNODE_DEF_TIME2WAIT:
 556                case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
 557                case ISCSI_FLASHNODE_MAX_R2T:
 558                case ISCSI_FLASHNODE_KEEPALIVE_TMO:
 559                case ISCSI_FLASHNODE_ISID:
 560                case ISCSI_FLASHNODE_TSID:
 561                case ISCSI_FLASHNODE_PORT:
 562                case ISCSI_FLASHNODE_MAX_BURST:
 563                case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
 564                case ISCSI_FLASHNODE_IPADDR:
 565                case ISCSI_FLASHNODE_ALIAS:
 566                case ISCSI_FLASHNODE_REDIRECT_IPADDR:
 567                case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
 568                case ISCSI_FLASHNODE_LOCAL_PORT:
 569                case ISCSI_FLASHNODE_IPV4_TOS:
 570                case ISCSI_FLASHNODE_IPV6_TC:
 571                case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
 572                case ISCSI_FLASHNODE_NAME:
 573                case ISCSI_FLASHNODE_TPGT:
 574                case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
 575                case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
 576                case ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE:
 577                case ISCSI_FLASHNODE_TCP_XMIT_WSF:
 578                case ISCSI_FLASHNODE_TCP_RECV_WSF:
 579                case ISCSI_FLASHNODE_CHAP_OUT_IDX:
 580                case ISCSI_FLASHNODE_USERNAME:
 581                case ISCSI_FLASHNODE_PASSWORD:
 582                case ISCSI_FLASHNODE_STATSN:
 583                case ISCSI_FLASHNODE_EXP_STATSN:
 584                case ISCSI_FLASHNODE_IS_BOOT_TGT:
 585                        return S_IRUGO;
 586                default:
 587                        return 0;
 588                }
 589        }
 590
 591        return 0;
 592}
 593
 594/**
 595 * qla4xxx_create chap_list - Create CHAP list from FLASH
 596 * @ha: pointer to adapter structure
 597 *
 598 * Read flash and make a list of CHAP entries, during login when a CHAP entry
 599 * is received, it will be checked in this list. If entry exist then the CHAP
 600 * entry index is set in the DDB. If CHAP entry does not exist in this list
 601 * then a new entry is added in FLASH in CHAP table and the index obtained is
 602 * used in the DDB.
 603 **/
 604static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
 605{
 606        int rval = 0;
 607        uint8_t *chap_flash_data = NULL;
 608        uint32_t offset;
 609        dma_addr_t chap_dma;
 610        uint32_t chap_size = 0;
 611
 612        if (is_qla40XX(ha))
 613                chap_size = MAX_CHAP_ENTRIES_40XX *
 614                            sizeof(struct ql4_chap_table);
 615        else    /* Single region contains CHAP info for both
 616                 * ports which is divided into half for each port.
 617                 */
 618                chap_size = ha->hw.flt_chap_size / 2;
 619
 620        chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
 621                                             &chap_dma, GFP_KERNEL);
 622        if (!chap_flash_data) {
 623                ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
 624                return;
 625        }
 626
 627        if (is_qla40XX(ha)) {
 628                offset = FLASH_CHAP_OFFSET;
 629        } else {
 630                offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
 631                if (ha->port_num == 1)
 632                        offset += chap_size;
 633        }
 634
 635        rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
 636        if (rval != QLA_SUCCESS)
 637                goto exit_chap_list;
 638
 639        if (ha->chap_list == NULL)
 640                ha->chap_list = vmalloc(chap_size);
 641        if (ha->chap_list == NULL) {
 642                ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
 643                goto exit_chap_list;
 644        }
 645
 646        memset(ha->chap_list, 0, chap_size);
 647        memcpy(ha->chap_list, chap_flash_data, chap_size);
 648
 649exit_chap_list:
 650        dma_free_coherent(&ha->pdev->dev, chap_size, chap_flash_data, chap_dma);
 651}
 652
 653static int qla4xxx_get_chap_by_index(struct scsi_qla_host *ha,
 654                                     int16_t chap_index,
 655                                     struct ql4_chap_table **chap_entry)
 656{
 657        int rval = QLA_ERROR;
 658        int max_chap_entries;
 659
 660        if (!ha->chap_list) {
 661                ql4_printk(KERN_ERR, ha, "CHAP table cache is empty!\n");
 662                rval = QLA_ERROR;
 663                goto exit_get_chap;
 664        }
 665
 666        if (is_qla80XX(ha))
 667                max_chap_entries = (ha->hw.flt_chap_size / 2) /
 668                                   sizeof(struct ql4_chap_table);
 669        else
 670                max_chap_entries = MAX_CHAP_ENTRIES_40XX;
 671
 672        if (chap_index > max_chap_entries) {
 673                ql4_printk(KERN_ERR, ha, "Invalid Chap index\n");
 674                rval = QLA_ERROR;
 675                goto exit_get_chap;
 676        }
 677
 678        *chap_entry = (struct ql4_chap_table *)ha->chap_list + chap_index;
 679        if ((*chap_entry)->cookie !=
 680             __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
 681                rval = QLA_ERROR;
 682                *chap_entry = NULL;
 683        } else {
 684                rval = QLA_SUCCESS;
 685        }
 686
 687exit_get_chap:
 688        return rval;
 689}
 690
 691/**
 692 * qla4xxx_find_free_chap_index - Find the first free chap index
 693 * @ha: pointer to adapter structure
 694 * @chap_index: CHAP index to be returned
 695 *
 696 * Find the first free chap index available in the chap table
 697 *
 698 * Note: Caller should acquire the chap lock before getting here.
 699 **/
 700static int qla4xxx_find_free_chap_index(struct scsi_qla_host *ha,
 701                                        uint16_t *chap_index)
 702{
 703        int i, rval;
 704        int free_index = -1;
 705        int max_chap_entries = 0;
 706        struct ql4_chap_table *chap_table;
 707
 708        if (is_qla80XX(ha))
 709                max_chap_entries = (ha->hw.flt_chap_size / 2) /
 710                                                sizeof(struct ql4_chap_table);
 711        else
 712                max_chap_entries = MAX_CHAP_ENTRIES_40XX;
 713
 714        if (!ha->chap_list) {
 715                ql4_printk(KERN_ERR, ha, "CHAP table cache is empty!\n");
 716                rval = QLA_ERROR;
 717                goto exit_find_chap;
 718        }
 719
 720        for (i = 0; i < max_chap_entries; i++) {
 721                chap_table = (struct ql4_chap_table *)ha->chap_list + i;
 722
 723                if ((chap_table->cookie !=
 724                    __constant_cpu_to_le16(CHAP_VALID_COOKIE)) &&
 725                   (i > MAX_RESRV_CHAP_IDX)) {
 726                                free_index = i;
 727                                break;
 728                }
 729        }
 730
 731        if (free_index != -1) {
 732                *chap_index = free_index;
 733                rval = QLA_SUCCESS;
 734        } else {
 735                rval = QLA_ERROR;
 736        }
 737
 738exit_find_chap:
 739        return rval;
 740}
 741
 742static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
 743                                  uint32_t *num_entries, char *buf)
 744{
 745        struct scsi_qla_host *ha = to_qla_host(shost);
 746        struct ql4_chap_table *chap_table;
 747        struct iscsi_chap_rec *chap_rec;
 748        int max_chap_entries = 0;
 749        int valid_chap_entries = 0;
 750        int ret = 0, i;
 751
 752        if (is_qla80XX(ha))
 753                max_chap_entries = (ha->hw.flt_chap_size / 2) /
 754                                        sizeof(struct ql4_chap_table);
 755        else
 756                max_chap_entries = MAX_CHAP_ENTRIES_40XX;
 757
 758        ql4_printk(KERN_INFO, ha, "%s: num_entries = %d, CHAP idx = %d\n",
 759                        __func__, *num_entries, chap_tbl_idx);
 760
 761        if (!buf) {
 762                ret = -ENOMEM;
 763                goto exit_get_chap_list;
 764        }
 765
 766        qla4xxx_create_chap_list(ha);
 767
 768        chap_rec = (struct iscsi_chap_rec *) buf;
 769        mutex_lock(&ha->chap_sem);
 770        for (i = chap_tbl_idx; i < max_chap_entries; i++) {
 771                chap_table = (struct ql4_chap_table *)ha->chap_list + i;
 772                if (chap_table->cookie !=
 773                    __constant_cpu_to_le16(CHAP_VALID_COOKIE))
 774                        continue;
 775
 776                chap_rec->chap_tbl_idx = i;
 777                strlcpy(chap_rec->username, chap_table->name,
 778                        ISCSI_CHAP_AUTH_NAME_MAX_LEN);
 779                strlcpy(chap_rec->password, chap_table->secret,
 780                        QL4_CHAP_MAX_SECRET_LEN);
 781                chap_rec->password_length = chap_table->secret_len;
 782
 783                if (chap_table->flags & BIT_7) /* local */
 784                        chap_rec->chap_type = CHAP_TYPE_OUT;
 785
 786                if (chap_table->flags & BIT_6) /* peer */
 787                        chap_rec->chap_type = CHAP_TYPE_IN;
 788
 789                chap_rec++;
 790
 791                valid_chap_entries++;
 792                if (valid_chap_entries == *num_entries)
 793                        break;
 794                else
 795                        continue;
 796        }
 797        mutex_unlock(&ha->chap_sem);
 798
 799exit_get_chap_list:
 800        ql4_printk(KERN_INFO, ha, "%s: Valid CHAP Entries = %d\n",
 801                        __func__,  valid_chap_entries);
 802        *num_entries = valid_chap_entries;
 803        return ret;
 804}
 805
 806static int __qla4xxx_is_chap_active(struct device *dev, void *data)
 807{
 808        int ret = 0;
 809        uint16_t *chap_tbl_idx = (uint16_t *) data;
 810        struct iscsi_cls_session *cls_session;
 811        struct iscsi_session *sess;
 812        struct ddb_entry *ddb_entry;
 813
 814        if (!iscsi_is_session_dev(dev))
 815                goto exit_is_chap_active;
 816
 817        cls_session = iscsi_dev_to_session(dev);
 818        sess = cls_session->dd_data;
 819        ddb_entry = sess->dd_data;
 820
 821        if (iscsi_session_chkready(cls_session))
 822                goto exit_is_chap_active;
 823
 824        if (ddb_entry->chap_tbl_idx == *chap_tbl_idx)
 825                ret = 1;
 826
 827exit_is_chap_active:
 828        return ret;
 829}
 830
 831static int qla4xxx_is_chap_active(struct Scsi_Host *shost,
 832                                  uint16_t chap_tbl_idx)
 833{
 834        int ret = 0;
 835
 836        ret = device_for_each_child(&shost->shost_gendev, &chap_tbl_idx,
 837                                    __qla4xxx_is_chap_active);
 838
 839        return ret;
 840}
 841
 842static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx)
 843{
 844        struct scsi_qla_host *ha = to_qla_host(shost);
 845        struct ql4_chap_table *chap_table;
 846        dma_addr_t chap_dma;
 847        int max_chap_entries = 0;
 848        uint32_t offset = 0;
 849        uint32_t chap_size;
 850        int ret = 0;
 851
 852        chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
 853        if (chap_table == NULL)
 854                return -ENOMEM;
 855
 856        memset(chap_table, 0, sizeof(struct ql4_chap_table));
 857
 858        if (is_qla80XX(ha))
 859                max_chap_entries = (ha->hw.flt_chap_size / 2) /
 860                                   sizeof(struct ql4_chap_table);
 861        else
 862                max_chap_entries = MAX_CHAP_ENTRIES_40XX;
 863
 864        if (chap_tbl_idx > max_chap_entries) {
 865                ret = -EINVAL;
 866                goto exit_delete_chap;
 867        }
 868
 869        /* Check if chap index is in use.
 870         * If chap is in use don't delet chap entry */
 871        ret = qla4xxx_is_chap_active(shost, chap_tbl_idx);
 872        if (ret) {
 873                ql4_printk(KERN_INFO, ha, "CHAP entry %d is in use, cannot "
 874                           "delete from flash\n", chap_tbl_idx);
 875                ret = -EBUSY;
 876                goto exit_delete_chap;
 877        }
 878
 879        chap_size = sizeof(struct ql4_chap_table);
 880        if (is_qla40XX(ha))
 881                offset = FLASH_CHAP_OFFSET | (chap_tbl_idx * chap_size);
 882        else {
 883                offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
 884                /* flt_chap_size is CHAP table size for both ports
 885                 * so divide it by 2 to calculate the offset for second port
 886                 */
 887                if (ha->port_num == 1)
 888                        offset += (ha->hw.flt_chap_size / 2);
 889                offset += (chap_tbl_idx * chap_size);
 890        }
 891
 892        ret = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
 893        if (ret != QLA_SUCCESS) {
 894                ret = -EINVAL;
 895                goto exit_delete_chap;
 896        }
 897
 898        DEBUG2(ql4_printk(KERN_INFO, ha, "Chap Cookie: x%x\n",
 899                          __le16_to_cpu(chap_table->cookie)));
 900
 901        if (__le16_to_cpu(chap_table->cookie) != CHAP_VALID_COOKIE) {
 902                ql4_printk(KERN_ERR, ha, "No valid chap entry found\n");
 903                goto exit_delete_chap;
 904        }
 905
 906        chap_table->cookie = __constant_cpu_to_le16(0xFFFF);
 907
 908        offset = FLASH_CHAP_OFFSET |
 909                        (chap_tbl_idx * sizeof(struct ql4_chap_table));
 910        ret = qla4xxx_set_flash(ha, chap_dma, offset, chap_size,
 911                                FLASH_OPT_RMW_COMMIT);
 912        if (ret == QLA_SUCCESS && ha->chap_list) {
 913                mutex_lock(&ha->chap_sem);
 914                /* Update ha chap_list cache */
 915                memcpy((struct ql4_chap_table *)ha->chap_list + chap_tbl_idx,
 916                        chap_table, sizeof(struct ql4_chap_table));
 917                mutex_unlock(&ha->chap_sem);
 918        }
 919        if (ret != QLA_SUCCESS)
 920                ret =  -EINVAL;
 921
 922exit_delete_chap:
 923        dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
 924        return ret;
 925}
 926
 927/**
 928 * qla4xxx_set_chap_entry - Make chap entry with given information
 929 * @shost: pointer to host
 930 * @data: chap info - credentials, index and type to make chap entry
 931 * @len: length of data
 932 *
 933 * Add or update chap entry with the given information
 934 **/
 935static int qla4xxx_set_chap_entry(struct Scsi_Host *shost, void *data, int len)
 936{
 937        struct scsi_qla_host *ha = to_qla_host(shost);
 938        struct iscsi_chap_rec chap_rec;
 939        struct ql4_chap_table *chap_entry = NULL;
 940        struct iscsi_param_info *param_info;
 941        struct nlattr *attr;
 942        int max_chap_entries = 0;
 943        int type;
 944        int rem = len;
 945        int rc = 0;
 946        int size;
 947
 948        memset(&chap_rec, 0, sizeof(chap_rec));
 949
 950        nla_for_each_attr(attr, data, len, rem) {
 951                param_info = nla_data(attr);
 952
 953                switch (param_info->param) {
 954                case ISCSI_CHAP_PARAM_INDEX:
 955                        chap_rec.chap_tbl_idx = *(uint16_t *)param_info->value;
 956                        break;
 957                case ISCSI_CHAP_PARAM_CHAP_TYPE:
 958                        chap_rec.chap_type = param_info->value[0];
 959                        break;
 960                case ISCSI_CHAP_PARAM_USERNAME:
 961                        size = min_t(size_t, sizeof(chap_rec.username),
 962                                     param_info->len);
 963                        memcpy(chap_rec.username, param_info->value, size);
 964                        break;
 965                case ISCSI_CHAP_PARAM_PASSWORD:
 966                        size = min_t(size_t, sizeof(chap_rec.password),
 967                                     param_info->len);
 968                        memcpy(chap_rec.password, param_info->value, size);
 969                        break;
 970                case ISCSI_CHAP_PARAM_PASSWORD_LEN:
 971                        chap_rec.password_length = param_info->value[0];
 972                        break;
 973                default:
 974                        ql4_printk(KERN_ERR, ha,
 975                                   "%s: No such sysfs attribute\n", __func__);
 976                        rc = -ENOSYS;
 977                        goto exit_set_chap;
 978                };
 979        }
 980
 981        if (chap_rec.chap_type == CHAP_TYPE_IN)
 982                type = BIDI_CHAP;
 983        else
 984                type = LOCAL_CHAP;
 985
 986        if (is_qla80XX(ha))
 987                max_chap_entries = (ha->hw.flt_chap_size / 2) /
 988                                   sizeof(struct ql4_chap_table);
 989        else
 990                max_chap_entries = MAX_CHAP_ENTRIES_40XX;
 991
 992        mutex_lock(&ha->chap_sem);
 993        if (chap_rec.chap_tbl_idx < max_chap_entries) {
 994                rc = qla4xxx_get_chap_by_index(ha, chap_rec.chap_tbl_idx,
 995                                               &chap_entry);
 996                if (!rc) {
 997                        if (!(type == qla4xxx_get_chap_type(chap_entry))) {
 998                                ql4_printk(KERN_INFO, ha,
 999                                           "Type mismatch for CHAP entry %d\n",
1000                                           chap_rec.chap_tbl_idx);
1001                                rc = -EINVAL;
1002                                goto exit_unlock_chap;
1003                        }
1004
1005                        /* If chap index is in use then don't modify it */
1006                        rc = qla4xxx_is_chap_active(shost,
1007                                                    chap_rec.chap_tbl_idx);
1008                        if (rc) {
1009                                ql4_printk(KERN_INFO, ha,
1010                                           "CHAP entry %d is in use\n",
1011                                           chap_rec.chap_tbl_idx);
1012                                rc = -EBUSY;
1013                                goto exit_unlock_chap;
1014                        }
1015                }
1016        } else {
1017                rc = qla4xxx_find_free_chap_index(ha, &chap_rec.chap_tbl_idx);
1018                if (rc) {
1019                        ql4_printk(KERN_INFO, ha, "CHAP entry not available\n");
1020                        rc = -EBUSY;
1021                        goto exit_unlock_chap;
1022                }
1023        }
1024
1025        rc = qla4xxx_set_chap(ha, chap_rec.username, chap_rec.password,
1026                              chap_rec.chap_tbl_idx, type);
1027
1028exit_unlock_chap:
1029        mutex_unlock(&ha->chap_sem);
1030
1031exit_set_chap:
1032        return rc;
1033}
1034
1035
1036static int qla4xxx_get_host_stats(struct Scsi_Host *shost, char *buf, int len)
1037{
1038        struct scsi_qla_host *ha = to_qla_host(shost);
1039        struct iscsi_offload_host_stats *host_stats = NULL;
1040        int host_stats_size;
1041        int ret = 0;
1042        int ddb_idx = 0;
1043        struct ql_iscsi_stats *ql_iscsi_stats = NULL;
1044        int stats_size;
1045        dma_addr_t iscsi_stats_dma;
1046
1047        DEBUG2(ql4_printk(KERN_INFO, ha, "Func: %s\n", __func__));
1048
1049        host_stats_size = sizeof(struct iscsi_offload_host_stats);
1050
1051        if (host_stats_size != len) {
1052                ql4_printk(KERN_INFO, ha, "%s: host_stats size mismatch expected = %d, is = %d\n",
1053                           __func__, len, host_stats_size);
1054                ret = -EINVAL;
1055                goto exit_host_stats;
1056        }
1057        host_stats = (struct iscsi_offload_host_stats *)buf;
1058
1059        if (!buf) {
1060                ret = -ENOMEM;
1061                goto exit_host_stats;
1062        }
1063
1064        stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
1065
1066        ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
1067                                            &iscsi_stats_dma, GFP_KERNEL);
1068        if (!ql_iscsi_stats) {
1069                ql4_printk(KERN_ERR, ha,
1070                           "Unable to allocate memory for iscsi stats\n");
1071                ret = -ENOMEM;
1072                goto exit_host_stats;
1073        }
1074
1075        ret =  qla4xxx_get_mgmt_data(ha, ddb_idx, stats_size,
1076                                     iscsi_stats_dma);
1077        if (ret != QLA_SUCCESS) {
1078                ql4_printk(KERN_ERR, ha,
1079                           "Unable to retrieve iscsi stats\n");
1080                ret = -EIO;
1081                goto exit_host_stats;
1082        }
1083        host_stats->mactx_frames = le64_to_cpu(ql_iscsi_stats->mac_tx_frames);
1084        host_stats->mactx_bytes = le64_to_cpu(ql_iscsi_stats->mac_tx_bytes);
1085        host_stats->mactx_multicast_frames =
1086                        le64_to_cpu(ql_iscsi_stats->mac_tx_multicast_frames);
1087        host_stats->mactx_broadcast_frames =
1088                        le64_to_cpu(ql_iscsi_stats->mac_tx_broadcast_frames);
1089        host_stats->mactx_pause_frames =
1090                        le64_to_cpu(ql_iscsi_stats->mac_tx_pause_frames);
1091        host_stats->mactx_control_frames =
1092                        le64_to_cpu(ql_iscsi_stats->mac_tx_control_frames);
1093        host_stats->mactx_deferral =
1094                        le64_to_cpu(ql_iscsi_stats->mac_tx_deferral);
1095        host_stats->mactx_excess_deferral =
1096                        le64_to_cpu(ql_iscsi_stats->mac_tx_excess_deferral);
1097        host_stats->mactx_late_collision =
1098                        le64_to_cpu(ql_iscsi_stats->mac_tx_late_collision);
1099        host_stats->mactx_abort = le64_to_cpu(ql_iscsi_stats->mac_tx_abort);
1100        host_stats->mactx_single_collision =
1101                        le64_to_cpu(ql_iscsi_stats->mac_tx_single_collision);
1102        host_stats->mactx_multiple_collision =
1103                        le64_to_cpu(ql_iscsi_stats->mac_tx_multiple_collision);
1104        host_stats->mactx_collision =
1105                        le64_to_cpu(ql_iscsi_stats->mac_tx_collision);
1106        host_stats->mactx_frames_dropped =
1107                        le64_to_cpu(ql_iscsi_stats->mac_tx_frames_dropped);
1108        host_stats->mactx_jumbo_frames =
1109                        le64_to_cpu(ql_iscsi_stats->mac_tx_jumbo_frames);
1110        host_stats->macrx_frames = le64_to_cpu(ql_iscsi_stats->mac_rx_frames);
1111        host_stats->macrx_bytes = le64_to_cpu(ql_iscsi_stats->mac_rx_bytes);
1112        host_stats->macrx_unknown_control_frames =
1113                le64_to_cpu(ql_iscsi_stats->mac_rx_unknown_control_frames);
1114        host_stats->macrx_pause_frames =
1115                        le64_to_cpu(ql_iscsi_stats->mac_rx_pause_frames);
1116        host_stats->macrx_control_frames =
1117                        le64_to_cpu(ql_iscsi_stats->mac_rx_control_frames);
1118        host_stats->macrx_dribble =
1119                        le64_to_cpu(ql_iscsi_stats->mac_rx_dribble);
1120        host_stats->macrx_frame_length_error =
1121                        le64_to_cpu(ql_iscsi_stats->mac_rx_frame_length_error);
1122        host_stats->macrx_jabber = le64_to_cpu(ql_iscsi_stats->mac_rx_jabber);
1123        host_stats->macrx_carrier_sense_error =
1124                le64_to_cpu(ql_iscsi_stats->mac_rx_carrier_sense_error);
1125        host_stats->macrx_frame_discarded =
1126                        le64_to_cpu(ql_iscsi_stats->mac_rx_frame_discarded);
1127        host_stats->macrx_frames_dropped =
1128                        le64_to_cpu(ql_iscsi_stats->mac_rx_frames_dropped);
1129        host_stats->mac_crc_error = le64_to_cpu(ql_iscsi_stats->mac_crc_error);
1130        host_stats->mac_encoding_error =
1131                        le64_to_cpu(ql_iscsi_stats->mac_encoding_error);
1132        host_stats->macrx_length_error_large =
1133                        le64_to_cpu(ql_iscsi_stats->mac_rx_length_error_large);
1134        host_stats->macrx_length_error_small =
1135                        le64_to_cpu(ql_iscsi_stats->mac_rx_length_error_small);
1136        host_stats->macrx_multicast_frames =
1137                        le64_to_cpu(ql_iscsi_stats->mac_rx_multicast_frames);
1138        host_stats->macrx_broadcast_frames =
1139                        le64_to_cpu(ql_iscsi_stats->mac_rx_broadcast_frames);
1140        host_stats->iptx_packets = le64_to_cpu(ql_iscsi_stats->ip_tx_packets);
1141        host_stats->iptx_bytes = le64_to_cpu(ql_iscsi_stats->ip_tx_bytes);
1142        host_stats->iptx_fragments =
1143                        le64_to_cpu(ql_iscsi_stats->ip_tx_fragments);
1144        host_stats->iprx_packets = le64_to_cpu(ql_iscsi_stats->ip_rx_packets);
1145        host_stats->iprx_bytes = le64_to_cpu(ql_iscsi_stats->ip_rx_bytes);
1146        host_stats->iprx_fragments =
1147                        le64_to_cpu(ql_iscsi_stats->ip_rx_fragments);
1148        host_stats->ip_datagram_reassembly =
1149                        le64_to_cpu(ql_iscsi_stats->ip_datagram_reassembly);
1150        host_stats->ip_invalid_address_error =
1151                        le64_to_cpu(ql_iscsi_stats->ip_invalid_address_error);
1152        host_stats->ip_error_packets =
1153                        le64_to_cpu(ql_iscsi_stats->ip_error_packets);
1154        host_stats->ip_fragrx_overlap =
1155                        le64_to_cpu(ql_iscsi_stats->ip_fragrx_overlap);
1156        host_stats->ip_fragrx_outoforder =
1157                        le64_to_cpu(ql_iscsi_stats->ip_fragrx_outoforder);
1158        host_stats->ip_datagram_reassembly_timeout =
1159                le64_to_cpu(ql_iscsi_stats->ip_datagram_reassembly_timeout);
1160        host_stats->ipv6tx_packets =
1161                        le64_to_cpu(ql_iscsi_stats->ipv6_tx_packets);
1162        host_stats->ipv6tx_bytes = le64_to_cpu(ql_iscsi_stats->ipv6_tx_bytes);
1163        host_stats->ipv6tx_fragments =
1164                        le64_to_cpu(ql_iscsi_stats->ipv6_tx_fragments);
1165        host_stats->ipv6rx_packets =
1166                        le64_to_cpu(ql_iscsi_stats->ipv6_rx_packets);
1167        host_stats->ipv6rx_bytes = le64_to_cpu(ql_iscsi_stats->ipv6_rx_bytes);
1168        host_stats->ipv6rx_fragments =
1169                        le64_to_cpu(ql_iscsi_stats->ipv6_rx_fragments);
1170        host_stats->ipv6_datagram_reassembly =
1171                        le64_to_cpu(ql_iscsi_stats->ipv6_datagram_reassembly);
1172        host_stats->ipv6_invalid_address_error =
1173                le64_to_cpu(ql_iscsi_stats->ipv6_invalid_address_error);
1174        host_stats->ipv6_error_packets =
1175                        le64_to_cpu(ql_iscsi_stats->ipv6_error_packets);
1176        host_stats->ipv6_fragrx_overlap =
1177                        le64_to_cpu(ql_iscsi_stats->ipv6_fragrx_overlap);
1178        host_stats->ipv6_fragrx_outoforder =
1179                        le64_to_cpu(ql_iscsi_stats->ipv6_fragrx_outoforder);
1180        host_stats->ipv6_datagram_reassembly_timeout =
1181                le64_to_cpu(ql_iscsi_stats->ipv6_datagram_reassembly_timeout);
1182        host_stats->tcptx_segments =
1183                        le64_to_cpu(ql_iscsi_stats->tcp_tx_segments);
1184        host_stats->tcptx_bytes = le64_to_cpu(ql_iscsi_stats->tcp_tx_bytes);
1185        host_stats->tcprx_segments =
1186                        le64_to_cpu(ql_iscsi_stats->tcp_rx_segments);
1187        host_stats->tcprx_byte = le64_to_cpu(ql_iscsi_stats->tcp_rx_byte);
1188        host_stats->tcp_duplicate_ack_retx =
1189                        le64_to_cpu(ql_iscsi_stats->tcp_duplicate_ack_retx);
1190        host_stats->tcp_retx_timer_expired =
1191                        le64_to_cpu(ql_iscsi_stats->tcp_retx_timer_expired);
1192        host_stats->tcprx_duplicate_ack =
1193                        le64_to_cpu(ql_iscsi_stats->tcp_rx_duplicate_ack);
1194        host_stats->tcprx_pure_ackr =
1195                        le64_to_cpu(ql_iscsi_stats->tcp_rx_pure_ackr);
1196        host_stats->tcptx_delayed_ack =
1197                        le64_to_cpu(ql_iscsi_stats->tcp_tx_delayed_ack);
1198        host_stats->tcptx_pure_ack =
1199                        le64_to_cpu(ql_iscsi_stats->tcp_tx_pure_ack);
1200        host_stats->tcprx_segment_error =
1201                        le64_to_cpu(ql_iscsi_stats->tcp_rx_segment_error);
1202        host_stats->tcprx_segment_outoforder =
1203                        le64_to_cpu(ql_iscsi_stats->tcp_rx_segment_outoforder);
1204        host_stats->tcprx_window_probe =
1205                        le64_to_cpu(ql_iscsi_stats->tcp_rx_window_probe);
1206        host_stats->tcprx_window_update =
1207                        le64_to_cpu(ql_iscsi_stats->tcp_rx_window_update);
1208        host_stats->tcptx_window_probe_persist =
1209                le64_to_cpu(ql_iscsi_stats->tcp_tx_window_probe_persist);
1210        host_stats->ecc_error_correction =
1211                        le64_to_cpu(ql_iscsi_stats->ecc_error_correction);
1212        host_stats->iscsi_pdu_tx = le64_to_cpu(ql_iscsi_stats->iscsi_pdu_tx);
1213        host_stats->iscsi_data_bytes_tx =
1214                        le64_to_cpu(ql_iscsi_stats->iscsi_data_bytes_tx);
1215        host_stats->iscsi_pdu_rx = le64_to_cpu(ql_iscsi_stats->iscsi_pdu_rx);
1216        host_stats->iscsi_data_bytes_rx =
1217                        le64_to_cpu(ql_iscsi_stats->iscsi_data_bytes_rx);
1218        host_stats->iscsi_io_completed =
1219                        le64_to_cpu(ql_iscsi_stats->iscsi_io_completed);
1220        host_stats->iscsi_unexpected_io_rx =
1221                        le64_to_cpu(ql_iscsi_stats->iscsi_unexpected_io_rx);
1222        host_stats->iscsi_format_error =
1223                        le64_to_cpu(ql_iscsi_stats->iscsi_format_error);
1224        host_stats->iscsi_hdr_digest_error =
1225                        le64_to_cpu(ql_iscsi_stats->iscsi_hdr_digest_error);
1226        host_stats->iscsi_data_digest_error =
1227                        le64_to_cpu(ql_iscsi_stats->iscsi_data_digest_error);
1228        host_stats->iscsi_sequence_error =
1229                        le64_to_cpu(ql_iscsi_stats->iscsi_sequence_error);
1230exit_host_stats:
1231        if (ql_iscsi_stats)
1232                dma_free_coherent(&ha->pdev->dev, host_stats_size,
1233                                  ql_iscsi_stats, iscsi_stats_dma);
1234
1235        ql4_printk(KERN_INFO, ha, "%s: Get host stats done\n",
1236                   __func__);
1237        return ret;
1238}
1239
1240static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
1241                                   enum iscsi_param_type param_type,
1242                                   int param, char *buf)
1243{
1244        struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
1245        struct scsi_qla_host *ha = to_qla_host(shost);
1246        int ival;
1247        char *pval = NULL;
1248        int len = -ENOSYS;
1249
1250        if (param_type == ISCSI_NET_PARAM) {
1251                switch (param) {
1252                case ISCSI_NET_PARAM_IPV4_ADDR:
1253                        len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
1254                        break;
1255                case ISCSI_NET_PARAM_IPV4_SUBNET:
1256                        len = sprintf(buf, "%pI4\n",
1257                                      &ha->ip_config.subnet_mask);
1258                        break;
1259                case ISCSI_NET_PARAM_IPV4_GW:
1260                        len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
1261                        break;
1262                case ISCSI_NET_PARAM_IFACE_ENABLE:
1263                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1264                                OP_STATE(ha->ip_config.ipv4_options,
1265                                         IPOPT_IPV4_PROTOCOL_ENABLE, pval);
1266                        } else {
1267                                OP_STATE(ha->ip_config.ipv6_options,
1268                                         IPV6_OPT_IPV6_PROTOCOL_ENABLE, pval);
1269                        }
1270
1271                        len = sprintf(buf, "%s\n", pval);
1272                        break;
1273                case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1274                        len = sprintf(buf, "%s\n",
1275                                      (ha->ip_config.tcp_options &
1276                                       TCPOPT_DHCP_ENABLE) ?
1277                                      "dhcp" : "static");
1278                        break;
1279                case ISCSI_NET_PARAM_IPV6_ADDR:
1280                        if (iface->iface_num == 0)
1281                                len = sprintf(buf, "%pI6\n",
1282                                              &ha->ip_config.ipv6_addr0);
1283                        if (iface->iface_num == 1)
1284                                len = sprintf(buf, "%pI6\n",
1285                                              &ha->ip_config.ipv6_addr1);
1286                        break;
1287                case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
1288                        len = sprintf(buf, "%pI6\n",
1289                                      &ha->ip_config.ipv6_link_local_addr);
1290                        break;
1291                case ISCSI_NET_PARAM_IPV6_ROUTER:
1292                        len = sprintf(buf, "%pI6\n",
1293                                      &ha->ip_config.ipv6_default_router_addr);
1294                        break;
1295                case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
1296                        pval = (ha->ip_config.ipv6_addl_options &
1297                                IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
1298                                "nd" : "static";
1299
1300                        len = sprintf(buf, "%s\n", pval);
1301                        break;
1302                case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
1303                        pval = (ha->ip_config.ipv6_addl_options &
1304                                IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
1305                                "auto" : "static";
1306
1307                        len = sprintf(buf, "%s\n", pval);
1308                        break;
1309                case ISCSI_NET_PARAM_VLAN_ID:
1310                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1311                                ival = ha->ip_config.ipv4_vlan_tag &
1312                                       ISCSI_MAX_VLAN_ID;
1313                        else
1314                                ival = ha->ip_config.ipv6_vlan_tag &
1315                                       ISCSI_MAX_VLAN_ID;
1316
1317                        len = sprintf(buf, "%d\n", ival);
1318                        break;
1319                case ISCSI_NET_PARAM_VLAN_PRIORITY:
1320                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1321                                ival = (ha->ip_config.ipv4_vlan_tag >> 13) &
1322                                       ISCSI_MAX_VLAN_PRIORITY;
1323                        else
1324                                ival = (ha->ip_config.ipv6_vlan_tag >> 13) &
1325                                       ISCSI_MAX_VLAN_PRIORITY;
1326
1327                        len = sprintf(buf, "%d\n", ival);
1328                        break;
1329                case ISCSI_NET_PARAM_VLAN_ENABLED:
1330                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1331                                OP_STATE(ha->ip_config.ipv4_options,
1332                                         IPOPT_VLAN_TAGGING_ENABLE, pval);
1333                        } else {
1334                                OP_STATE(ha->ip_config.ipv6_options,
1335                                         IPV6_OPT_VLAN_TAGGING_ENABLE, pval);
1336                        }
1337                        len = sprintf(buf, "%s\n", pval);
1338                        break;
1339                case ISCSI_NET_PARAM_MTU:
1340                        len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
1341                        break;
1342                case ISCSI_NET_PARAM_PORT:
1343                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1344                                len = sprintf(buf, "%d\n",
1345                                              ha->ip_config.ipv4_port);
1346                        else
1347                                len = sprintf(buf, "%d\n",
1348                                              ha->ip_config.ipv6_port);
1349                        break;
1350                case ISCSI_NET_PARAM_IPADDR_STATE:
1351                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1352                                pval = iscsi_get_ipaddress_state_name(
1353                                                ha->ip_config.ipv4_addr_state);
1354                        } else {
1355                                if (iface->iface_num == 0)
1356                                        pval = iscsi_get_ipaddress_state_name(
1357                                                ha->ip_config.ipv6_addr0_state);
1358                                else if (iface->iface_num == 1)
1359                                        pval = iscsi_get_ipaddress_state_name(
1360                                                ha->ip_config.ipv6_addr1_state);
1361                        }
1362
1363                        len = sprintf(buf, "%s\n", pval);
1364                        break;
1365                case ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE:
1366                        pval = iscsi_get_ipaddress_state_name(
1367                                        ha->ip_config.ipv6_link_local_state);
1368                        len = sprintf(buf, "%s\n", pval);
1369                        break;
1370                case ISCSI_NET_PARAM_IPV6_ROUTER_STATE:
1371                        pval = iscsi_get_router_state_name(
1372                                      ha->ip_config.ipv6_default_router_state);
1373                        len = sprintf(buf, "%s\n", pval);
1374                        break;
1375                case ISCSI_NET_PARAM_DELAYED_ACK_EN:
1376                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1377                                OP_STATE(~ha->ip_config.tcp_options,
1378                                         TCPOPT_DELAYED_ACK_DISABLE, pval);
1379                        } else {
1380                                OP_STATE(~ha->ip_config.ipv6_tcp_options,
1381                                         IPV6_TCPOPT_DELAYED_ACK_DISABLE, pval);
1382                        }
1383                        len = sprintf(buf, "%s\n", pval);
1384                        break;
1385                case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
1386                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1387                                OP_STATE(~ha->ip_config.tcp_options,
1388                                         TCPOPT_NAGLE_ALGO_DISABLE, pval);
1389                        } else {
1390                                OP_STATE(~ha->ip_config.ipv6_tcp_options,
1391                                         IPV6_TCPOPT_NAGLE_ALGO_DISABLE, pval);
1392                        }
1393                        len = sprintf(buf, "%s\n", pval);
1394                        break;
1395                case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
1396                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1397                                OP_STATE(~ha->ip_config.tcp_options,
1398                                         TCPOPT_WINDOW_SCALE_DISABLE, pval);
1399                        } else {
1400                                OP_STATE(~ha->ip_config.ipv6_tcp_options,
1401                                         IPV6_TCPOPT_WINDOW_SCALE_DISABLE,
1402                                         pval);
1403                        }
1404                        len = sprintf(buf, "%s\n", pval);
1405                        break;
1406                case ISCSI_NET_PARAM_TCP_WSF:
1407                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1408                                len = sprintf(buf, "%d\n",
1409                                              ha->ip_config.tcp_wsf);
1410                        else
1411                                len = sprintf(buf, "%d\n",
1412                                              ha->ip_config.ipv6_tcp_wsf);
1413                        break;
1414                case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
1415                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1416                                ival = (ha->ip_config.tcp_options &
1417                                        TCPOPT_TIMER_SCALE) >> 1;
1418                        else
1419                                ival = (ha->ip_config.ipv6_tcp_options &
1420                                        IPV6_TCPOPT_TIMER_SCALE) >> 1;
1421
1422                        len = sprintf(buf, "%d\n", ival);
1423                        break;
1424                case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
1425                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1426                                OP_STATE(ha->ip_config.tcp_options,
1427                                         TCPOPT_TIMESTAMP_ENABLE, pval);
1428                        } else {
1429                                OP_STATE(ha->ip_config.ipv6_tcp_options,
1430                                         IPV6_TCPOPT_TIMESTAMP_EN, pval);
1431                        }
1432                        len = sprintf(buf, "%s\n", pval);
1433                        break;
1434                case ISCSI_NET_PARAM_CACHE_ID:
1435                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
1436                                len = sprintf(buf, "%d\n",
1437                                              ha->ip_config.ipv4_cache_id);
1438                        else
1439                                len = sprintf(buf, "%d\n",
1440                                              ha->ip_config.ipv6_cache_id);
1441                        break;
1442                case ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN:
1443                        OP_STATE(ha->ip_config.tcp_options,
1444                                 TCPOPT_DNS_SERVER_IP_EN, pval);
1445
1446                        len = sprintf(buf, "%s\n", pval);
1447                        break;
1448                case ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN:
1449                        OP_STATE(ha->ip_config.tcp_options,
1450                                 TCPOPT_SLP_DA_INFO_EN, pval);
1451
1452                        len = sprintf(buf, "%s\n", pval);
1453                        break;
1454                case ISCSI_NET_PARAM_IPV4_TOS_EN:
1455                        OP_STATE(ha->ip_config.ipv4_options,
1456                                 IPOPT_IPV4_TOS_EN, pval);
1457
1458                        len = sprintf(buf, "%s\n", pval);
1459                        break;
1460                case ISCSI_NET_PARAM_IPV4_TOS:
1461                        len = sprintf(buf, "%d\n", ha->ip_config.ipv4_tos);
1462                        break;
1463                case ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN:
1464                        OP_STATE(ha->ip_config.ipv4_options,
1465                                 IPOPT_GRAT_ARP_EN, pval);
1466
1467                        len = sprintf(buf, "%s\n", pval);
1468                        break;
1469                case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN:
1470                        OP_STATE(ha->ip_config.ipv4_options, IPOPT_ALT_CID_EN,
1471                                 pval);
1472
1473                        len = sprintf(buf, "%s\n", pval);
1474                        break;
1475                case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID:
1476                        pval = (ha->ip_config.ipv4_alt_cid_len) ?
1477                               (char *)ha->ip_config.ipv4_alt_cid : "";
1478
1479                        len = sprintf(buf, "%s\n", pval);
1480                        break;
1481                case ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN:
1482                        OP_STATE(ha->ip_config.ipv4_options,
1483                                 IPOPT_REQ_VID_EN, pval);
1484
1485                        len = sprintf(buf, "%s\n", pval);
1486                        break;
1487                case ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN:
1488                        OP_STATE(ha->ip_config.ipv4_options,
1489                                 IPOPT_USE_VID_EN, pval);
1490
1491                        len = sprintf(buf, "%s\n", pval);
1492                        break;
1493                case ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID:
1494                        pval = (ha->ip_config.ipv4_vid_len) ?
1495                               (char *)ha->ip_config.ipv4_vid : "";
1496
1497                        len = sprintf(buf, "%s\n", pval);
1498                        break;
1499                case ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN:
1500                        OP_STATE(ha->ip_config.ipv4_options,
1501                                 IPOPT_LEARN_IQN_EN, pval);
1502
1503                        len = sprintf(buf, "%s\n", pval);
1504                        break;
1505                case ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE:
1506                        OP_STATE(~ha->ip_config.ipv4_options,
1507                                 IPOPT_FRAGMENTATION_DISABLE, pval);
1508
1509                        len = sprintf(buf, "%s\n", pval);
1510                        break;
1511                case ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN:
1512                        OP_STATE(ha->ip_config.ipv4_options,
1513                                 IPOPT_IN_FORWARD_EN, pval);
1514
1515                        len = sprintf(buf, "%s\n", pval);
1516                        break;
1517                case ISCSI_NET_PARAM_REDIRECT_EN:
1518                        if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
1519                                OP_STATE(ha->ip_config.ipv4_options,
1520                                         IPOPT_ARP_REDIRECT_EN, pval);
1521                        } else {
1522                                OP_STATE(ha->ip_config.ipv6_options,
1523                                         IPV6_OPT_REDIRECT_EN, pval);
1524                        }
1525                        len = sprintf(buf, "%s\n", pval);
1526                        break;
1527                case ISCSI_NET_PARAM_IPV4_TTL:
1528                        len = sprintf(buf, "%d\n", ha->ip_config.ipv4_ttl);
1529                        break;
1530                case ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN:
1531                        OP_STATE(ha->ip_config.ipv6_options,
1532                                 IPV6_OPT_GRAT_NEIGHBOR_ADV_EN, pval);
1533
1534                        len = sprintf(buf, "%s\n", pval);
1535                        break;
1536                case ISCSI_NET_PARAM_IPV6_MLD_EN:
1537                        OP_STATE(ha->ip_config.ipv6_addl_options,
1538                                 IPV6_ADDOPT_MLD_EN, pval);
1539
1540                        len = sprintf(buf, "%s\n", pval);
1541                        break;
1542                case ISCSI_NET_PARAM_IPV6_FLOW_LABEL:
1543                        len = sprintf(buf, "%u\n", ha->ip_config.ipv6_flow_lbl);
1544                        break;
1545                case ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS:
1546                        len = sprintf(buf, "%d\n",
1547                                      ha->ip_config.ipv6_traffic_class);
1548                        break;
1549                case ISCSI_NET_PARAM_IPV6_HOP_LIMIT:
1550                        len = sprintf(buf, "%d\n",
1551                                      ha->ip_config.ipv6_hop_limit);
1552                        break;
1553                case ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO:
1554                        len = sprintf(buf, "%d\n",
1555                                      ha->ip_config.ipv6_nd_reach_time);
1556                        break;
1557                case ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME:
1558                        len = sprintf(buf, "%d\n",
1559                                      ha->ip_config.ipv6_nd_rexmit_timer);
1560                        break;
1561                case ISCSI_NET_PARAM_IPV6_ND_STALE_TMO:
1562                        len = sprintf(buf, "%d\n",
1563                                      ha->ip_config.ipv6_nd_stale_timeout);
1564                        break;
1565                case ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT:
1566                        len = sprintf(buf, "%d\n",
1567                                      ha->ip_config.ipv6_dup_addr_detect_count);
1568                        break;
1569                case ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU:
1570                        len = sprintf(buf, "%d\n",
1571                                      ha->ip_config.ipv6_gw_advrt_mtu);
1572                        break;
1573                default:
1574                        len = -ENOSYS;
1575                }
1576        } else if (param_type == ISCSI_IFACE_PARAM) {
1577                switch (param) {
1578                case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
1579                        len = sprintf(buf, "%d\n", ha->ip_config.def_timeout);
1580                        break;
1581                case ISCSI_IFACE_PARAM_HDRDGST_EN:
1582                        OP_STATE(ha->ip_config.iscsi_options,
1583                                 ISCSIOPTS_HEADER_DIGEST_EN, pval);
1584
1585                        len = sprintf(buf, "%s\n", pval);
1586                        break;
1587                case ISCSI_IFACE_PARAM_DATADGST_EN:
1588                        OP_STATE(ha->ip_config.iscsi_options,
1589                                 ISCSIOPTS_DATA_DIGEST_EN, pval);
1590
1591                        len = sprintf(buf, "%s\n", pval);
1592                        break;
1593                case ISCSI_IFACE_PARAM_IMM_DATA_EN:
1594                        OP_STATE(ha->ip_config.iscsi_options,
1595                                 ISCSIOPTS_IMMEDIATE_DATA_EN, pval);
1596
1597                        len = sprintf(buf, "%s\n", pval);
1598                        break;
1599                case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
1600                        OP_STATE(ha->ip_config.iscsi_options,
1601                                 ISCSIOPTS_INITIAL_R2T_EN, pval);
1602
1603                        len = sprintf(buf, "%s\n", pval);
1604                        break;
1605                case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
1606                        OP_STATE(ha->ip_config.iscsi_options,
1607                                 ISCSIOPTS_DATA_SEQ_INORDER_EN, pval);
1608
1609                        len = sprintf(buf, "%s\n", pval);
1610                        break;
1611                case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
1612                        OP_STATE(ha->ip_config.iscsi_options,
1613                                 ISCSIOPTS_DATA_PDU_INORDER_EN, pval);
1614
1615                        len = sprintf(buf, "%s\n", pval);
1616                        break;
1617                case ISCSI_IFACE_PARAM_ERL:
1618                        len = sprintf(buf, "%d\n",
1619                                      (ha->ip_config.iscsi_options &
1620                                       ISCSIOPTS_ERL));
1621                        break;
1622                case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
1623                        len = sprintf(buf, "%u\n",
1624                                      ha->ip_config.iscsi_max_pdu_size *
1625                                      BYTE_UNITS);
1626                        break;
1627                case ISCSI_IFACE_PARAM_FIRST_BURST:
1628                        len = sprintf(buf, "%u\n",
1629                                      ha->ip_config.iscsi_first_burst_len *
1630                                      BYTE_UNITS);
1631                        break;
1632                case ISCSI_IFACE_PARAM_MAX_R2T:
1633                        len = sprintf(buf, "%d\n",
1634                                      ha->ip_config.iscsi_max_outstnd_r2t);
1635                        break;
1636                case ISCSI_IFACE_PARAM_MAX_BURST:
1637                        len = sprintf(buf, "%u\n",
1638                                      ha->ip_config.iscsi_max_burst_len *
1639                                      BYTE_UNITS);
1640                        break;
1641                case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
1642                        OP_STATE(ha->ip_config.iscsi_options,
1643                                 ISCSIOPTS_CHAP_AUTH_EN, pval);
1644
1645                        len = sprintf(buf, "%s\n", pval);
1646                        break;
1647                case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
1648                        OP_STATE(ha->ip_config.iscsi_options,
1649                                 ISCSIOPTS_BIDI_CHAP_EN, pval);
1650
1651                        len = sprintf(buf, "%s\n", pval);
1652                        break;
1653                case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
1654                        OP_STATE(ha->ip_config.iscsi_options,
1655                                 ISCSIOPTS_DISCOVERY_AUTH_EN, pval);
1656
1657                        len = sprintf(buf, "%s\n", pval);
1658                        break;
1659                case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
1660                        OP_STATE(ha->ip_config.iscsi_options,
1661                                 ISCSIOPTS_DISCOVERY_LOGOUT_EN, pval);
1662
1663                        len = sprintf(buf, "%s\n", pval);
1664                        break;
1665                case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
1666                        OP_STATE(ha->ip_config.iscsi_options,
1667                                 ISCSIOPTS_STRICT_LOGIN_COMP_EN, pval);
1668
1669                        len = sprintf(buf, "%s\n", pval);
1670                        break;
1671                case ISCSI_IFACE_PARAM_INITIATOR_NAME:
1672                        len = sprintf(buf, "%s\n", ha->ip_config.iscsi_name);
1673                        break;
1674                default:
1675                        len = -ENOSYS;
1676                }
1677        }
1678
1679        return len;
1680}
1681
1682static struct iscsi_endpoint *
1683qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
1684                   int non_blocking)
1685{
1686        int ret;
1687        struct iscsi_endpoint *ep;
1688        struct qla_endpoint *qla_ep;
1689        struct scsi_qla_host *ha;
1690        struct sockaddr_in *addr;
1691        struct sockaddr_in6 *addr6;
1692
1693        if (!shost) {
1694                ret = -ENXIO;
1695                pr_err("%s: shost is NULL\n", __func__);
1696                return ERR_PTR(ret);
1697        }
1698
1699        ha = iscsi_host_priv(shost);
1700        ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
1701        if (!ep) {
1702                ret = -ENOMEM;
1703                return ERR_PTR(ret);
1704        }
1705
1706        qla_ep = ep->dd_data;
1707        memset(qla_ep, 0, sizeof(struct qla_endpoint));
1708        if (dst_addr->sa_family == AF_INET) {
1709                memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
1710                addr = (struct sockaddr_in *)&qla_ep->dst_addr;
1711                DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
1712                                  (char *)&addr->sin_addr));
1713        } else if (dst_addr->sa_family == AF_INET6) {
1714                memcpy(&qla_ep->dst_addr, dst_addr,
1715                       sizeof(struct sockaddr_in6));
1716                addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
1717                DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
1718                                  (char *)&addr6->sin6_addr));
1719        } else {
1720                ql4_printk(KERN_WARNING, ha, "%s: Invalid endpoint\n",
1721                           __func__);
1722        }
1723
1724        qla_ep->host = shost;
1725
1726        return ep;
1727}
1728
1729static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1730{
1731        struct qla_endpoint *qla_ep;
1732        struct scsi_qla_host *ha;
1733        int ret = 0;
1734
1735        qla_ep = ep->dd_data;
1736        ha = to_qla_host(qla_ep->host);
1737        DEBUG2(pr_info_ratelimited("%s: host: %ld\n", __func__, ha->host_no));
1738
1739        if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
1740                ret = 1;
1741
1742        return ret;
1743}
1744
1745static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
1746{
1747        struct qla_endpoint *qla_ep;
1748        struct scsi_qla_host *ha;
1749
1750        qla_ep = ep->dd_data;
1751        ha = to_qla_host(qla_ep->host);
1752        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
1753                          ha->host_no));
1754        iscsi_destroy_endpoint(ep);
1755}
1756
1757static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
1758                                enum iscsi_param param,
1759                                char *buf)
1760{
1761        struct qla_endpoint *qla_ep = ep->dd_data;
1762        struct sockaddr *dst_addr;
1763        struct scsi_qla_host *ha;
1764
1765        if (!qla_ep)
1766                return -ENOTCONN;
1767
1768        ha = to_qla_host(qla_ep->host);
1769        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
1770                          ha->host_no));
1771
1772        switch (param) {
1773        case ISCSI_PARAM_CONN_PORT:
1774        case ISCSI_PARAM_CONN_ADDRESS:
1775                dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1776                if (!dst_addr)
1777                        return -ENOTCONN;
1778
1779                return iscsi_conn_get_addr_param((struct sockaddr_storage *)
1780                                                 &qla_ep->dst_addr, param, buf);
1781        default:
1782                return -ENOSYS;
1783        }
1784}
1785
1786static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1787                                   struct iscsi_stats *stats)
1788{
1789        struct iscsi_session *sess;
1790        struct iscsi_cls_session *cls_sess;
1791        struct ddb_entry *ddb_entry;
1792        struct scsi_qla_host *ha;
1793        struct ql_iscsi_stats *ql_iscsi_stats;
1794        int stats_size;
1795        int ret;
1796        dma_addr_t iscsi_stats_dma;
1797
1798        cls_sess = iscsi_conn_to_session(cls_conn);
1799        sess = cls_sess->dd_data;
1800        ddb_entry = sess->dd_data;
1801        ha = ddb_entry->ha;
1802
1803        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
1804                          ha->host_no));
1805        stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
1806        /* Allocate memory */
1807        ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
1808                                            &iscsi_stats_dma, GFP_KERNEL);
1809        if (!ql_iscsi_stats) {
1810                ql4_printk(KERN_ERR, ha,
1811                           "Unable to allocate memory for iscsi stats\n");
1812                goto exit_get_stats;
1813        }
1814
1815        ret =  qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
1816                                     iscsi_stats_dma);
1817        if (ret != QLA_SUCCESS) {
1818                ql4_printk(KERN_ERR, ha,
1819                           "Unable to retrieve iscsi stats\n");
1820                goto free_stats;
1821        }
1822
1823        /* octets */
1824        stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
1825        stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
1826        /* xmit pdus */
1827        stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
1828        stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
1829        stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
1830        stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
1831        stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
1832        stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
1833        stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
1834        stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
1835        /* recv pdus */
1836        stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
1837        stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
1838        stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
1839        stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
1840        stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
1841        stats->logoutrsp_pdus =
1842                        le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
1843        stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
1844        stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
1845        stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
1846
1847free_stats:
1848        dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
1849                          iscsi_stats_dma);
1850exit_get_stats:
1851        return;
1852}
1853
1854static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
1855{
1856        struct iscsi_cls_session *session;
1857        struct iscsi_session *sess;
1858        unsigned long flags;
1859        enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
1860
1861        session = starget_to_session(scsi_target(sc->device));
1862        sess = session->dd_data;
1863
1864        spin_lock_irqsave(&session->lock, flags);
1865        if (session->state == ISCSI_SESSION_FAILED)
1866                ret = BLK_EH_RESET_TIMER;
1867        spin_unlock_irqrestore(&session->lock, flags);
1868
1869        return ret;
1870}
1871
1872static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
1873{
1874        struct scsi_qla_host *ha = to_qla_host(shost);
1875        struct iscsi_cls_host *ihost = shost->shost_data;
1876        uint32_t speed = ISCSI_PORT_SPEED_UNKNOWN;
1877
1878        qla4xxx_get_firmware_state(ha);
1879
1880        switch (ha->addl_fw_state & 0x0F00) {
1881        case FW_ADDSTATE_LINK_SPEED_10MBPS:
1882                speed = ISCSI_PORT_SPEED_10MBPS;
1883                break;
1884        case FW_ADDSTATE_LINK_SPEED_100MBPS:
1885                speed = ISCSI_PORT_SPEED_100MBPS;
1886                break;
1887        case FW_ADDSTATE_LINK_SPEED_1GBPS:
1888                speed = ISCSI_PORT_SPEED_1GBPS;
1889                break;
1890        case FW_ADDSTATE_LINK_SPEED_10GBPS:
1891                speed = ISCSI_PORT_SPEED_10GBPS;
1892                break;
1893        }
1894        ihost->port_speed = speed;
1895}
1896
1897static void qla4xxx_set_port_state(struct Scsi_Host *shost)
1898{
1899        struct scsi_qla_host *ha = to_qla_host(shost);
1900        struct iscsi_cls_host *ihost = shost->shost_data;
1901        uint32_t state = ISCSI_PORT_STATE_DOWN;
1902
1903        if (test_bit(AF_LINK_UP, &ha->flags))
1904                state = ISCSI_PORT_STATE_UP;
1905
1906        ihost->port_state = state;
1907}
1908
1909static int qla4xxx_host_get_param(struct Scsi_Host *shost,
1910                                  enum iscsi_host_param param, char *buf)
1911{
1912        struct scsi_qla_host *ha = to_qla_host(shost);
1913        int len;
1914
1915        switch (param) {
1916        case ISCSI_HOST_PARAM_HWADDRESS:
1917                len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
1918                break;
1919        case ISCSI_HOST_PARAM_IPADDRESS:
1920                len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
1921                break;
1922        case ISCSI_HOST_PARAM_INITIATOR_NAME:
1923                len = sprintf(buf, "%s\n", ha->name_string);
1924                break;
1925        case ISCSI_HOST_PARAM_PORT_STATE:
1926                qla4xxx_set_port_state(shost);
1927                len = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
1928                break;
1929        case ISCSI_HOST_PARAM_PORT_SPEED:
1930                qla4xxx_set_port_speed(shost);
1931                len = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
1932                break;
1933        default:
1934                return -ENOSYS;
1935        }
1936
1937        return len;
1938}
1939
1940static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
1941{
1942        if (ha->iface_ipv4)
1943                return;
1944
1945        /* IPv4 */
1946        ha->iface_ipv4 = iscsi_create_iface(ha->host,
1947                                            &qla4xxx_iscsi_transport,
1948                                            ISCSI_IFACE_TYPE_IPV4, 0, 0);
1949        if (!ha->iface_ipv4)
1950                ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
1951                           "iface0.\n");
1952}
1953
1954static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
1955{
1956        if (!ha->iface_ipv6_0)
1957                /* IPv6 iface-0 */
1958                ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
1959                                                      &qla4xxx_iscsi_transport,
1960                                                      ISCSI_IFACE_TYPE_IPV6, 0,
1961                                                      0);
1962        if (!ha->iface_ipv6_0)
1963                ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
1964                           "iface0.\n");
1965
1966        if (!ha->iface_ipv6_1)
1967                /* IPv6 iface-1 */
1968                ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
1969                                                      &qla4xxx_iscsi_transport,
1970                                                      ISCSI_IFACE_TYPE_IPV6, 1,
1971                                                      0);
1972        if (!ha->iface_ipv6_1)
1973                ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
1974                           "iface1.\n");
1975}
1976
1977static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
1978{
1979        if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
1980                qla4xxx_create_ipv4_iface(ha);
1981
1982        if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
1983                qla4xxx_create_ipv6_iface(ha);
1984}
1985
1986static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
1987{
1988        if (ha->iface_ipv4) {
1989                iscsi_destroy_iface(ha->iface_ipv4);
1990                ha->iface_ipv4 = NULL;
1991        }
1992}
1993
1994static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
1995{
1996        if (ha->iface_ipv6_0) {
1997                iscsi_destroy_iface(ha->iface_ipv6_0);
1998                ha->iface_ipv6_0 = NULL;
1999        }
2000        if (ha->iface_ipv6_1) {
2001                iscsi_destroy_iface(ha->iface_ipv6_1);
2002                ha->iface_ipv6_1 = NULL;
2003        }
2004}
2005
2006static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
2007{
2008        qla4xxx_destroy_ipv4_iface(ha);
2009        qla4xxx_destroy_ipv6_iface(ha);
2010}
2011
2012static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
2013                             struct iscsi_iface_param_info *iface_param,
2014                             struct addr_ctrl_blk *init_fw_cb)
2015{
2016        /*
2017         * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
2018         * iface_num 1 is valid only for IPv6 Addr.
2019         */
2020        switch (iface_param->param) {
2021        case ISCSI_NET_PARAM_IPV6_ADDR:
2022                if (iface_param->iface_num & 0x1)
2023                        /* IPv6 Addr 1 */
2024                        memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
2025                               sizeof(init_fw_cb->ipv6_addr1));
2026                else
2027                        /* IPv6 Addr 0 */
2028                        memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
2029                               sizeof(init_fw_cb->ipv6_addr0));
2030                break;
2031        case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
2032                if (iface_param->iface_num & 0x1)
2033                        break;
2034                memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
2035                       sizeof(init_fw_cb->ipv6_if_id));
2036                break;
2037        case ISCSI_NET_PARAM_IPV6_ROUTER:
2038                if (iface_param->iface_num & 0x1)
2039                        break;
2040                memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
2041                       sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
2042                break;
2043        case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
2044                /* Autocfg applies to even interface */
2045                if (iface_param->iface_num & 0x1)
2046                        break;
2047
2048                if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
2049                        init_fw_cb->ipv6_addtl_opts &=
2050                                cpu_to_le16(
2051                                  ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
2052                else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
2053                        init_fw_cb->ipv6_addtl_opts |=
2054                                cpu_to_le16(
2055                                  IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
2056                else
2057                        ql4_printk(KERN_ERR, ha,
2058                                   "Invalid autocfg setting for IPv6 addr\n");
2059                break;
2060        case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
2061                /* Autocfg applies to even interface */
2062                if (iface_param->iface_num & 0x1)
2063                        break;
2064
2065                if (iface_param->value[0] ==
2066                    ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
2067                        init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
2068                                        IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
2069                else if (iface_param->value[0] ==
2070                         ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
2071                        init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
2072                                       ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
2073                else
2074                        ql4_printk(KERN_ERR, ha,
2075                                   "Invalid autocfg setting for IPv6 linklocal addr\n");
2076                break;
2077        case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
2078                /* Autocfg applies to even interface */
2079                if (iface_param->iface_num & 0x1)
2080                        break;
2081
2082                if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
2083                        memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
2084                               sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
2085                break;
2086        case ISCSI_NET_PARAM_IFACE_ENABLE:
2087                if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
2088                        init_fw_cb->ipv6_opts |=
2089                                cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
2090                        qla4xxx_create_ipv6_iface(ha);
2091                } else {
2092                        init_fw_cb->ipv6_opts &=
2093                                cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
2094                                            0xFFFF);
2095                        qla4xxx_destroy_ipv6_iface(ha);
2096                }
2097                break;
2098        case ISCSI_NET_PARAM_VLAN_TAG:
2099                if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
2100                        break;
2101                init_fw_cb->ipv6_vlan_tag =
2102                                cpu_to_be16(*(uint16_t *)iface_param->value);
2103                break;
2104        case ISCSI_NET_PARAM_VLAN_ENABLED:
2105                if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
2106                        init_fw_cb->ipv6_opts |=
2107                                cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
2108                else
2109                        init_fw_cb->ipv6_opts &=
2110                                cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
2111                break;
2112        case ISCSI_NET_PARAM_MTU:
2113                init_fw_cb->eth_mtu_size =
2114                                cpu_to_le16(*(uint16_t *)iface_param->value);
2115                break;
2116        case ISCSI_NET_PARAM_PORT:
2117                /* Autocfg applies to even interface */
2118                if (iface_param->iface_num & 0x1)
2119                        break;
2120
2121                init_fw_cb->ipv6_port =
2122                                cpu_to_le16(*(uint16_t *)iface_param->value);
2123                break;
2124        case ISCSI_NET_PARAM_DELAYED_ACK_EN:
2125                if (iface_param->iface_num & 0x1)
2126                        break;
2127                if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2128                        init_fw_cb->ipv6_tcp_opts |=
2129                                cpu_to_le16(IPV6_TCPOPT_DELAYED_ACK_DISABLE);
2130                else
2131                        init_fw_cb->ipv6_tcp_opts &=
2132                                cpu_to_le16(~IPV6_TCPOPT_DELAYED_ACK_DISABLE &
2133                                            0xFFFF);
2134                break;
2135        case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
2136                if (iface_param->iface_num & 0x1)
2137                        break;
2138                if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2139                        init_fw_cb->ipv6_tcp_opts |=
2140                                cpu_to_le16(IPV6_TCPOPT_NAGLE_ALGO_DISABLE);
2141                else
2142                        init_fw_cb->ipv6_tcp_opts &=
2143                                cpu_to_le16(~IPV6_TCPOPT_NAGLE_ALGO_DISABLE);
2144                break;
2145        case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
2146                if (iface_param->iface_num & 0x1)
2147                        break;
2148                if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2149                        init_fw_cb->ipv6_tcp_opts |=
2150                                cpu_to_le16(IPV6_TCPOPT_WINDOW_SCALE_DISABLE);
2151                else
2152                        init_fw_cb->ipv6_tcp_opts &=
2153                                cpu_to_le16(~IPV6_TCPOPT_WINDOW_SCALE_DISABLE);
2154                break;
2155        case ISCSI_NET_PARAM_TCP_WSF:
2156                if (iface_param->iface_num & 0x1)
2157                        break;
2158                init_fw_cb->ipv6_tcp_wsf = iface_param->value[0];
2159                break;
2160        case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
2161                if (iface_param->iface_num & 0x1)
2162                        break;
2163                init_fw_cb->ipv6_tcp_opts &=
2164                                        cpu_to_le16(~IPV6_TCPOPT_TIMER_SCALE);
2165                init_fw_cb->ipv6_tcp_opts |=
2166                                cpu_to_le16((iface_param->value[0] << 1) &
2167                                            IPV6_TCPOPT_TIMER_SCALE);
2168                break;
2169        case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
2170                if (iface_param->iface_num & 0x1)
2171                        break;
2172                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2173                        init_fw_cb->ipv6_tcp_opts |=
2174                                cpu_to_le16(IPV6_TCPOPT_TIMESTAMP_EN);
2175                else
2176                        init_fw_cb->ipv6_tcp_opts &=
2177                                cpu_to_le16(~IPV6_TCPOPT_TIMESTAMP_EN);
2178                break;
2179        case ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN:
2180                if (iface_param->iface_num & 0x1)
2181                        break;
2182                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2183                        init_fw_cb->ipv6_opts |=
2184                                cpu_to_le16(IPV6_OPT_GRAT_NEIGHBOR_ADV_EN);
2185                else
2186                        init_fw_cb->ipv6_opts &=
2187                                cpu_to_le16(~IPV6_OPT_GRAT_NEIGHBOR_ADV_EN);
2188                break;
2189        case ISCSI_NET_PARAM_REDIRECT_EN:
2190                if (iface_param->iface_num & 0x1)
2191                        break;
2192                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2193                        init_fw_cb->ipv6_opts |=
2194                                cpu_to_le16(IPV6_OPT_REDIRECT_EN);
2195                else
2196                        init_fw_cb->ipv6_opts &=
2197                                cpu_to_le16(~IPV6_OPT_REDIRECT_EN);
2198                break;
2199        case ISCSI_NET_PARAM_IPV6_MLD_EN:
2200                if (iface_param->iface_num & 0x1)
2201                        break;
2202                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2203                        init_fw_cb->ipv6_addtl_opts |=
2204                                cpu_to_le16(IPV6_ADDOPT_MLD_EN);
2205                else
2206                        init_fw_cb->ipv6_addtl_opts &=
2207                                cpu_to_le16(~IPV6_ADDOPT_MLD_EN);
2208                break;
2209        case ISCSI_NET_PARAM_IPV6_FLOW_LABEL:
2210                if (iface_param->iface_num & 0x1)
2211                        break;
2212                init_fw_cb->ipv6_flow_lbl =
2213                                cpu_to_le16(*(uint16_t *)iface_param->value);
2214                break;
2215        case ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS:
2216                if (iface_param->iface_num & 0x1)
2217                        break;
2218                init_fw_cb->ipv6_traffic_class = iface_param->value[0];
2219                break;
2220        case ISCSI_NET_PARAM_IPV6_HOP_LIMIT:
2221                if (iface_param->iface_num & 0x1)
2222                        break;
2223                init_fw_cb->ipv6_hop_limit = iface_param->value[0];
2224                break;
2225        case ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO:
2226                if (iface_param->iface_num & 0x1)
2227                        break;
2228                init_fw_cb->ipv6_nd_reach_time =
2229                                cpu_to_le32(*(uint32_t *)iface_param->value);
2230                break;
2231        case ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME:
2232                if (iface_param->iface_num & 0x1)
2233                        break;
2234                init_fw_cb->ipv6_nd_rexmit_timer =
2235                                cpu_to_le32(*(uint32_t *)iface_param->value);
2236                break;
2237        case ISCSI_NET_PARAM_IPV6_ND_STALE_TMO:
2238                if (iface_param->iface_num & 0x1)
2239                        break;
2240                init_fw_cb->ipv6_nd_stale_timeout =
2241                                cpu_to_le32(*(uint32_t *)iface_param->value);
2242                break;
2243        case ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT:
2244                if (iface_param->iface_num & 0x1)
2245                        break;
2246                init_fw_cb->ipv6_dup_addr_detect_count = iface_param->value[0];
2247                break;
2248        case ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU:
2249                if (iface_param->iface_num & 0x1)
2250                        break;
2251                init_fw_cb->ipv6_gw_advrt_mtu =
2252                                cpu_to_le32(*(uint32_t *)iface_param->value);
2253                break;
2254        default:
2255                ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
2256                           iface_param->param);
2257                break;
2258        }
2259}
2260
2261static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
2262                             struct iscsi_iface_param_info *iface_param,
2263                             struct addr_ctrl_blk *init_fw_cb)
2264{
2265        switch (iface_param->param) {
2266        case ISCSI_NET_PARAM_IPV4_ADDR:
2267                memcpy(init_fw_cb->ipv4_addr, iface_param->value,
2268                       sizeof(init_fw_cb->ipv4_addr));
2269                break;
2270        case ISCSI_NET_PARAM_IPV4_SUBNET:
2271                memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
2272                       sizeof(init_fw_cb->ipv4_subnet));
2273                break;
2274        case ISCSI_NET_PARAM_IPV4_GW:
2275                memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
2276                       sizeof(init_fw_cb->ipv4_gw_addr));
2277                break;
2278        case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
2279                if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
2280                        init_fw_cb->ipv4_tcp_opts |=
2281                                        cpu_to_le16(TCPOPT_DHCP_ENABLE);
2282                else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
2283                        init_fw_cb->ipv4_tcp_opts &=
2284                                        cpu_to_le16(~TCPOPT_DHCP_ENABLE);
2285                else
2286                        ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
2287                break;
2288        case ISCSI_NET_PARAM_IFACE_ENABLE:
2289                if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
2290                        init_fw_cb->ipv4_ip_opts |=
2291                                cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
2292                        qla4xxx_create_ipv4_iface(ha);
2293                } else {
2294                        init_fw_cb->ipv4_ip_opts &=
2295                                cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
2296                                            0xFFFF);
2297                        qla4xxx_destroy_ipv4_iface(ha);
2298                }
2299                break;
2300        case ISCSI_NET_PARAM_VLAN_TAG:
2301                if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
2302                        break;
2303                init_fw_cb->ipv4_vlan_tag =
2304                                cpu_to_be16(*(uint16_t *)iface_param->value);
2305                break;
2306        case ISCSI_NET_PARAM_VLAN_ENABLED:
2307                if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
2308                        init_fw_cb->ipv4_ip_opts |=
2309                                        cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
2310                else
2311                        init_fw_cb->ipv4_ip_opts &=
2312                                        cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
2313                break;
2314        case ISCSI_NET_PARAM_MTU:
2315                init_fw_cb->eth_mtu_size =
2316                                cpu_to_le16(*(uint16_t *)iface_param->value);
2317                break;
2318        case ISCSI_NET_PARAM_PORT:
2319                init_fw_cb->ipv4_port =
2320                                cpu_to_le16(*(uint16_t *)iface_param->value);
2321                break;
2322        case ISCSI_NET_PARAM_DELAYED_ACK_EN:
2323                if (iface_param->iface_num & 0x1)
2324                        break;
2325                if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2326                        init_fw_cb->ipv4_tcp_opts |=
2327                                cpu_to_le16(TCPOPT_DELAYED_ACK_DISABLE);
2328                else
2329                        init_fw_cb->ipv4_tcp_opts &=
2330                                cpu_to_le16(~TCPOPT_DELAYED_ACK_DISABLE &
2331                                            0xFFFF);
2332                break;
2333        case ISCSI_NET_PARAM_TCP_NAGLE_DISABLE:
2334                if (iface_param->iface_num & 0x1)
2335                        break;
2336                if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2337                        init_fw_cb->ipv4_tcp_opts |=
2338                                cpu_to_le16(TCPOPT_NAGLE_ALGO_DISABLE);
2339                else
2340                        init_fw_cb->ipv4_tcp_opts &=
2341                                cpu_to_le16(~TCPOPT_NAGLE_ALGO_DISABLE);
2342                break;
2343        case ISCSI_NET_PARAM_TCP_WSF_DISABLE:
2344                if (iface_param->iface_num & 0x1)
2345                        break;
2346                if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2347                        init_fw_cb->ipv4_tcp_opts |=
2348                                cpu_to_le16(TCPOPT_WINDOW_SCALE_DISABLE);
2349                else
2350                        init_fw_cb->ipv4_tcp_opts &=
2351                                cpu_to_le16(~TCPOPT_WINDOW_SCALE_DISABLE);
2352                break;
2353        case ISCSI_NET_PARAM_TCP_WSF:
2354                if (iface_param->iface_num & 0x1)
2355                        break;
2356                init_fw_cb->ipv4_tcp_wsf = iface_param->value[0];
2357                break;
2358        case ISCSI_NET_PARAM_TCP_TIMER_SCALE:
2359                if (iface_param->iface_num & 0x1)
2360                        break;
2361                init_fw_cb->ipv4_tcp_opts &= cpu_to_le16(~TCPOPT_TIMER_SCALE);
2362                init_fw_cb->ipv4_tcp_opts |=
2363                                cpu_to_le16((iface_param->value[0] << 1) &
2364                                            TCPOPT_TIMER_SCALE);
2365                break;
2366        case ISCSI_NET_PARAM_TCP_TIMESTAMP_EN:
2367                if (iface_param->iface_num & 0x1)
2368                        break;
2369                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2370                        init_fw_cb->ipv4_tcp_opts |=
2371                                cpu_to_le16(TCPOPT_TIMESTAMP_ENABLE);
2372                else
2373                        init_fw_cb->ipv4_tcp_opts &=
2374                                cpu_to_le16(~TCPOPT_TIMESTAMP_ENABLE);
2375                break;
2376        case ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN:
2377                if (iface_param->iface_num & 0x1)
2378                        break;
2379                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2380                        init_fw_cb->ipv4_tcp_opts |=
2381                                cpu_to_le16(TCPOPT_DNS_SERVER_IP_EN);
2382                else
2383                        init_fw_cb->ipv4_tcp_opts &=
2384                                cpu_to_le16(~TCPOPT_DNS_SERVER_IP_EN);
2385                break;
2386        case ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN:
2387                if (iface_param->iface_num & 0x1)
2388                        break;
2389                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2390                        init_fw_cb->ipv4_tcp_opts |=
2391                                cpu_to_le16(TCPOPT_SLP_DA_INFO_EN);
2392                else
2393                        init_fw_cb->ipv4_tcp_opts &=
2394                                cpu_to_le16(~TCPOPT_SLP_DA_INFO_EN);
2395                break;
2396        case ISCSI_NET_PARAM_IPV4_TOS_EN:
2397                if (iface_param->iface_num & 0x1)
2398                        break;
2399                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2400                        init_fw_cb->ipv4_ip_opts |=
2401                                cpu_to_le16(IPOPT_IPV4_TOS_EN);
2402                else
2403                        init_fw_cb->ipv4_ip_opts &=
2404                                cpu_to_le16(~IPOPT_IPV4_TOS_EN);
2405                break;
2406        case ISCSI_NET_PARAM_IPV4_TOS:
2407                if (iface_param->iface_num & 0x1)
2408                        break;
2409                init_fw_cb->ipv4_tos = iface_param->value[0];
2410                break;
2411        case ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN:
2412                if (iface_param->iface_num & 0x1)
2413                        break;
2414                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2415                        init_fw_cb->ipv4_ip_opts |=
2416                                        cpu_to_le16(IPOPT_GRAT_ARP_EN);
2417                else
2418                        init_fw_cb->ipv4_ip_opts &=
2419                                        cpu_to_le16(~IPOPT_GRAT_ARP_EN);
2420                break;
2421        case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN:
2422                if (iface_param->iface_num & 0x1)
2423                        break;
2424                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2425                        init_fw_cb->ipv4_ip_opts |=
2426                                cpu_to_le16(IPOPT_ALT_CID_EN);
2427                else
2428                        init_fw_cb->ipv4_ip_opts &=
2429                                cpu_to_le16(~IPOPT_ALT_CID_EN);
2430                break;
2431        case ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID:
2432                if (iface_param->iface_num & 0x1)
2433                        break;
2434                memcpy(init_fw_cb->ipv4_dhcp_alt_cid, iface_param->value,
2435                       (sizeof(init_fw_cb->ipv4_dhcp_alt_cid) - 1));
2436                init_fw_cb->ipv4_dhcp_alt_cid_len =
2437                                        strlen(init_fw_cb->ipv4_dhcp_alt_cid);
2438                break;
2439        case ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN:
2440                if (iface_param->iface_num & 0x1)
2441                        break;
2442                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2443                        init_fw_cb->ipv4_ip_opts |=
2444                                        cpu_to_le16(IPOPT_REQ_VID_EN);
2445                else
2446                        init_fw_cb->ipv4_ip_opts &=
2447                                        cpu_to_le16(~IPOPT_REQ_VID_EN);
2448                break;
2449        case ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN:
2450                if (iface_param->iface_num & 0x1)
2451                        break;
2452                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2453                        init_fw_cb->ipv4_ip_opts |=
2454                                        cpu_to_le16(IPOPT_USE_VID_EN);
2455                else
2456                        init_fw_cb->ipv4_ip_opts &=
2457                                        cpu_to_le16(~IPOPT_USE_VID_EN);
2458                break;
2459        case ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID:
2460                if (iface_param->iface_num & 0x1)
2461                        break;
2462                memcpy(init_fw_cb->ipv4_dhcp_vid, iface_param->value,
2463                       (sizeof(init_fw_cb->ipv4_dhcp_vid) - 1));
2464                init_fw_cb->ipv4_dhcp_vid_len =
2465                                        strlen(init_fw_cb->ipv4_dhcp_vid);
2466                break;
2467        case ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN:
2468                if (iface_param->iface_num & 0x1)
2469                        break;
2470                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2471                        init_fw_cb->ipv4_ip_opts |=
2472                                        cpu_to_le16(IPOPT_LEARN_IQN_EN);
2473                else
2474                        init_fw_cb->ipv4_ip_opts &=
2475                                        cpu_to_le16(~IPOPT_LEARN_IQN_EN);
2476                break;
2477        case ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE:
2478                if (iface_param->iface_num & 0x1)
2479                        break;
2480                if (iface_param->value[0] == ISCSI_NET_PARAM_DISABLE)
2481                        init_fw_cb->ipv4_ip_opts |=
2482                                cpu_to_le16(IPOPT_FRAGMENTATION_DISABLE);
2483                else
2484                        init_fw_cb->ipv4_ip_opts &=
2485                                cpu_to_le16(~IPOPT_FRAGMENTATION_DISABLE);
2486                break;
2487        case ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN:
2488                if (iface_param->iface_num & 0x1)
2489                        break;
2490                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2491                        init_fw_cb->ipv4_ip_opts |=
2492                                cpu_to_le16(IPOPT_IN_FORWARD_EN);
2493                else
2494                        init_fw_cb->ipv4_ip_opts &=
2495                                cpu_to_le16(~IPOPT_IN_FORWARD_EN);
2496                break;
2497        case ISCSI_NET_PARAM_REDIRECT_EN:
2498                if (iface_param->iface_num & 0x1)
2499                        break;
2500                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2501                        init_fw_cb->ipv4_ip_opts |=
2502                                cpu_to_le16(IPOPT_ARP_REDIRECT_EN);
2503                else
2504                        init_fw_cb->ipv4_ip_opts &=
2505                                cpu_to_le16(~IPOPT_ARP_REDIRECT_EN);
2506                break;
2507        case ISCSI_NET_PARAM_IPV4_TTL:
2508                if (iface_param->iface_num & 0x1)
2509                        break;
2510                init_fw_cb->ipv4_ttl = iface_param->value[0];
2511                break;
2512        default:
2513                ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
2514                           iface_param->param);
2515                break;
2516        }
2517}
2518
2519static void qla4xxx_set_iscsi_param(struct scsi_qla_host *ha,
2520                                    struct iscsi_iface_param_info *iface_param,
2521                                    struct addr_ctrl_blk *init_fw_cb)
2522{
2523        switch (iface_param->param) {
2524        case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
2525                if (iface_param->iface_num & 0x1)
2526                        break;
2527                init_fw_cb->def_timeout =
2528                                cpu_to_le16(*(uint16_t *)iface_param->value);
2529                break;
2530        case ISCSI_IFACE_PARAM_HDRDGST_EN:
2531                if (iface_param->iface_num & 0x1)
2532                        break;
2533                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2534                        init_fw_cb->iscsi_opts |=
2535                                cpu_to_le16(ISCSIOPTS_HEADER_DIGEST_EN);
2536                else
2537                        init_fw_cb->iscsi_opts &=
2538                                cpu_to_le16(~ISCSIOPTS_HEADER_DIGEST_EN);
2539                break;
2540        case ISCSI_IFACE_PARAM_DATADGST_EN:
2541                if (iface_param->iface_num & 0x1)
2542                        break;
2543                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2544                        init_fw_cb->iscsi_opts |=
2545                                cpu_to_le16(ISCSIOPTS_DATA_DIGEST_EN);
2546                else
2547                        init_fw_cb->iscsi_opts &=
2548                                cpu_to_le16(~ISCSIOPTS_DATA_DIGEST_EN);
2549                break;
2550        case ISCSI_IFACE_PARAM_IMM_DATA_EN:
2551                if (iface_param->iface_num & 0x1)
2552                        break;
2553                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2554                        init_fw_cb->iscsi_opts |=
2555                                cpu_to_le16(ISCSIOPTS_IMMEDIATE_DATA_EN);
2556                else
2557                        init_fw_cb->iscsi_opts &=
2558                                cpu_to_le16(~ISCSIOPTS_IMMEDIATE_DATA_EN);
2559                break;
2560        case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
2561                if (iface_param->iface_num & 0x1)
2562                        break;
2563                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2564                        init_fw_cb->iscsi_opts |=
2565                                cpu_to_le16(ISCSIOPTS_INITIAL_R2T_EN);
2566                else
2567                        init_fw_cb->iscsi_opts &=
2568                                cpu_to_le16(~ISCSIOPTS_INITIAL_R2T_EN);
2569                break;
2570        case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
2571                if (iface_param->iface_num & 0x1)
2572                        break;
2573                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2574                        init_fw_cb->iscsi_opts |=
2575                                cpu_to_le16(ISCSIOPTS_DATA_SEQ_INORDER_EN);
2576                else
2577                        init_fw_cb->iscsi_opts &=
2578                                cpu_to_le16(~ISCSIOPTS_DATA_SEQ_INORDER_EN);
2579                break;
2580        case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
2581                if (iface_param->iface_num & 0x1)
2582                        break;
2583                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2584                        init_fw_cb->iscsi_opts |=
2585                                cpu_to_le16(ISCSIOPTS_DATA_PDU_INORDER_EN);
2586                else
2587                        init_fw_cb->iscsi_opts &=
2588                                cpu_to_le16(~ISCSIOPTS_DATA_PDU_INORDER_EN);
2589                break;
2590        case ISCSI_IFACE_PARAM_ERL:
2591                if (iface_param->iface_num & 0x1)
2592                        break;
2593                init_fw_cb->iscsi_opts &= cpu_to_le16(~ISCSIOPTS_ERL);
2594                init_fw_cb->iscsi_opts |= cpu_to_le16(iface_param->value[0] &
2595                                                      ISCSIOPTS_ERL);
2596                break;
2597        case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
2598                if (iface_param->iface_num & 0x1)
2599                        break;
2600                init_fw_cb->iscsi_max_pdu_size =
2601                                cpu_to_le32(*(uint32_t *)iface_param->value) /
2602                                BYTE_UNITS;
2603                break;
2604        case ISCSI_IFACE_PARAM_FIRST_BURST:
2605                if (iface_param->iface_num & 0x1)
2606                        break;
2607                init_fw_cb->iscsi_fburst_len =
2608                                cpu_to_le32(*(uint32_t *)iface_param->value) /
2609                                BYTE_UNITS;
2610                break;
2611        case ISCSI_IFACE_PARAM_MAX_R2T:
2612                if (iface_param->iface_num & 0x1)
2613                        break;
2614                init_fw_cb->iscsi_max_outstnd_r2t =
2615                                cpu_to_le16(*(uint16_t *)iface_param->value);
2616                break;
2617        case ISCSI_IFACE_PARAM_MAX_BURST:
2618                if (iface_param->iface_num & 0x1)
2619                        break;
2620                init_fw_cb->iscsi_max_burst_len =
2621                                cpu_to_le32(*(uint32_t *)iface_param->value) /
2622                                BYTE_UNITS;
2623                break;
2624        case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
2625                if (iface_param->iface_num & 0x1)
2626                        break;
2627                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2628                        init_fw_cb->iscsi_opts |=
2629                                cpu_to_le16(ISCSIOPTS_CHAP_AUTH_EN);
2630                else
2631                        init_fw_cb->iscsi_opts &=
2632                                cpu_to_le16(~ISCSIOPTS_CHAP_AUTH_EN);
2633                break;
2634        case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
2635                if (iface_param->iface_num & 0x1)
2636                        break;
2637                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2638                        init_fw_cb->iscsi_opts |=
2639                                cpu_to_le16(ISCSIOPTS_BIDI_CHAP_EN);
2640                else
2641                        init_fw_cb->iscsi_opts &=
2642                                cpu_to_le16(~ISCSIOPTS_BIDI_CHAP_EN);
2643                break;
2644        case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
2645                if (iface_param->iface_num & 0x1)
2646                        break;
2647                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2648                        init_fw_cb->iscsi_opts |=
2649                                cpu_to_le16(ISCSIOPTS_DISCOVERY_AUTH_EN);
2650                else
2651                        init_fw_cb->iscsi_opts &=
2652                                cpu_to_le16(~ISCSIOPTS_DISCOVERY_AUTH_EN);
2653                break;
2654        case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
2655                if (iface_param->iface_num & 0x1)
2656                        break;
2657                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2658                        init_fw_cb->iscsi_opts |=
2659                                cpu_to_le16(ISCSIOPTS_DISCOVERY_LOGOUT_EN);
2660                else
2661                        init_fw_cb->iscsi_opts &=
2662                                cpu_to_le16(~ISCSIOPTS_DISCOVERY_LOGOUT_EN);
2663                break;
2664        case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
2665                if (iface_param->iface_num & 0x1)
2666                        break;
2667                if (iface_param->value[0] == ISCSI_NET_PARAM_ENABLE)
2668                        init_fw_cb->iscsi_opts |=
2669                                cpu_to_le16(ISCSIOPTS_STRICT_LOGIN_COMP_EN);
2670                else
2671                        init_fw_cb->iscsi_opts &=
2672                                cpu_to_le16(~ISCSIOPTS_STRICT_LOGIN_COMP_EN);
2673                break;
2674        default:
2675                ql4_printk(KERN_ERR, ha, "Unknown iscsi param = %d\n",
2676                           iface_param->param);
2677                break;
2678        }
2679}
2680
2681static void
2682qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
2683{
2684        struct addr_ctrl_blk_def *acb;
2685        acb = (struct addr_ctrl_blk_def *)init_fw_cb;
2686        memset(acb->reserved1, 0, sizeof(acb->reserved1));
2687        memset(acb->reserved2, 0, sizeof(acb->reserved2));
2688        memset(acb->reserved3, 0, sizeof(acb->reserved3));
2689        memset(acb->reserved4, 0, sizeof(acb->reserved4));
2690        memset(acb->reserved5, 0, sizeof(acb->reserved5));
2691        memset(acb->reserved6, 0, sizeof(acb->reserved6));
2692        memset(acb->reserved7, 0, sizeof(acb->reserved7));
2693        memset(acb->reserved8, 0, sizeof(acb->reserved8));
2694        memset(acb->reserved9, 0, sizeof(acb->reserved9));
2695        memset(acb->reserved10, 0, sizeof(acb->reserved10));
2696        memset(acb->reserved11, 0, sizeof(acb->reserved11));
2697        memset(acb->reserved12, 0, sizeof(acb->reserved12));
2698        memset(acb->reserved13, 0, sizeof(acb->reserved13));
2699        memset(acb->reserved14, 0, sizeof(acb->reserved14));
2700        memset(acb->reserved15, 0, sizeof(acb->reserved15));
2701}
2702
2703static int
2704qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
2705{
2706        struct scsi_qla_host *ha = to_qla_host(shost);
2707        int rval = 0;
2708        struct iscsi_iface_param_info *iface_param = NULL;
2709        struct addr_ctrl_blk *init_fw_cb = NULL;
2710        dma_addr_t init_fw_cb_dma;
2711        uint32_t mbox_cmd[MBOX_REG_COUNT];
2712        uint32_t mbox_sts[MBOX_REG_COUNT];
2713        uint32_t rem = len;
2714        struct nlattr *attr;
2715
2716        init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
2717                                        sizeof(struct addr_ctrl_blk),
2718                                        &init_fw_cb_dma, GFP_KERNEL);
2719        if (!init_fw_cb) {
2720                ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
2721                           __func__);
2722                return -ENOMEM;
2723        }
2724
2725        memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
2726        memset(&mbox_cmd, 0, sizeof(mbox_cmd));
2727        memset(&mbox_sts, 0, sizeof(mbox_sts));
2728
2729        if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
2730                ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
2731                rval = -EIO;
2732                goto exit_init_fw_cb;
2733        }
2734
2735        nla_for_each_attr(attr, data, len, rem) {
2736                iface_param = nla_data(attr);
2737
2738                if (iface_param->param_type == ISCSI_NET_PARAM) {
2739                        switch (iface_param->iface_type) {
2740                        case ISCSI_IFACE_TYPE_IPV4:
2741                                switch (iface_param->iface_num) {
2742                                case 0:
2743                                        qla4xxx_set_ipv4(ha, iface_param,
2744                                                         init_fw_cb);
2745                                        break;
2746                                default:
2747                                /* Cannot have more than one IPv4 interface */
2748                                        ql4_printk(KERN_ERR, ha,
2749                                                   "Invalid IPv4 iface number = %d\n",
2750                                                   iface_param->iface_num);
2751                                        break;
2752                                }
2753                                break;
2754                        case ISCSI_IFACE_TYPE_IPV6:
2755                                switch (iface_param->iface_num) {
2756                                case 0:
2757                                case 1:
2758                                        qla4xxx_set_ipv6(ha, iface_param,
2759                                                         init_fw_cb);
2760                                        break;
2761                                default:
2762                                /* Cannot have more than two IPv6 interface */
2763                                        ql4_printk(KERN_ERR, ha,
2764                                                   "Invalid IPv6 iface number = %d\n",
2765                                                   iface_param->iface_num);
2766                                        break;
2767                                }
2768                                break;
2769                        default:
2770                                ql4_printk(KERN_ERR, ha,
2771                                           "Invalid iface type\n");
2772                                break;
2773                        }
2774                } else if (iface_param->param_type == ISCSI_IFACE_PARAM) {
2775                                qla4xxx_set_iscsi_param(ha, iface_param,
2776                                                        init_fw_cb);
2777                } else {
2778                        continue;
2779                }
2780        }
2781
2782        init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
2783
2784        rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
2785                                 sizeof(struct addr_ctrl_blk),
2786                                 FLASH_OPT_RMW_COMMIT);
2787        if (rval != QLA_SUCCESS) {
2788                ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
2789                           __func__);
2790                rval = -EIO;
2791                goto exit_init_fw_cb;
2792        }
2793
2794        rval = qla4xxx_disable_acb(ha);
2795        if (rval != QLA_SUCCESS) {
2796                ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
2797                           __func__);
2798                rval = -EIO;
2799                goto exit_init_fw_cb;
2800        }
2801
2802        wait_for_completion_timeout(&ha->disable_acb_comp,
2803                                    DISABLE_ACB_TOV * HZ);
2804
2805        qla4xxx_initcb_to_acb(init_fw_cb);
2806
2807        rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
2808        if (rval != QLA_SUCCESS) {
2809                ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
2810                           __func__);
2811                rval = -EIO;
2812                goto exit_init_fw_cb;
2813        }
2814
2815        memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
2816        qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
2817                                  init_fw_cb_dma);
2818
2819exit_init_fw_cb:
2820        dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
2821                          init_fw_cb, init_fw_cb_dma);
2822
2823        return rval;
2824}
2825
2826static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
2827                                     enum iscsi_param param, char *buf)
2828{
2829        struct iscsi_session *sess = cls_sess->dd_data;
2830        struct ddb_entry *ddb_entry = sess->dd_data;
2831        struct scsi_qla_host *ha = ddb_entry->ha;
2832        struct iscsi_cls_conn *cls_conn = ddb_entry->conn;
2833        struct ql4_chap_table chap_tbl;
2834        int rval, len;
2835        uint16_t idx;
2836
2837        memset(&chap_tbl, 0, sizeof(chap_tbl));
2838        switch (param) {
2839        case ISCSI_PARAM_CHAP_IN_IDX:
2840                rval = qla4xxx_get_chap_index(ha, sess->username_in,
2841                                              sess->password_in, BIDI_CHAP,
2842                                              &idx);
2843                if (rval)
2844                        len = sprintf(buf, "\n");
2845                else
2846                        len = sprintf(buf, "%hu\n", idx);
2847                break;
2848        case ISCSI_PARAM_CHAP_OUT_IDX:
2849                if (ddb_entry->ddb_type == FLASH_DDB) {
2850                        if (ddb_entry->chap_tbl_idx != INVALID_ENTRY) {
2851                                idx = ddb_entry->chap_tbl_idx;
2852                                rval = QLA_SUCCESS;
2853                        } else {
2854                                rval = QLA_ERROR;
2855                        }
2856                } else {
2857                        rval = qla4xxx_get_chap_index(ha, sess->username,
2858                                                      sess->password,
2859                                                      LOCAL_CHAP, &idx);
2860                }
2861                if (rval)
2862                        len = sprintf(buf, "\n");
2863                else
2864                        len = sprintf(buf, "%hu\n", idx);
2865                break;
2866        case ISCSI_PARAM_USERNAME:
2867        case ISCSI_PARAM_PASSWORD:
2868                /* First, populate session username and password for FLASH DDB,
2869                 * if not already done. This happens when session login fails
2870                 * for a FLASH DDB.
2871                 */
2872                if (ddb_entry->ddb_type == FLASH_DDB &&
2873                    ddb_entry->chap_tbl_idx != INVALID_ENTRY &&
2874                    !sess->username && !sess->password) {
2875                        idx = ddb_entry->chap_tbl_idx;
2876                        rval = qla4xxx_get_uni_chap_at_index(ha, chap_tbl.name,
2877                                                            chap_tbl.secret,
2878                                                            idx);
2879                        if (!rval) {
2880                                iscsi_set_param(cls_conn, ISCSI_PARAM_USERNAME,
2881                                                (char *)chap_tbl.name,
2882                                                strlen((char *)chap_tbl.name));
2883                                iscsi_set_param(cls_conn, ISCSI_PARAM_PASSWORD,
2884                                                (char *)chap_tbl.secret,
2885                                                chap_tbl.secret_len);
2886                        }
2887                }
2888                /* allow fall-through */
2889        default:
2890                return iscsi_session_get_param(cls_sess, param, buf);
2891        }
2892
2893        return len;
2894}
2895
2896static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
2897                                  enum iscsi_param param, char *buf)
2898{
2899        struct iscsi_conn *conn;
2900        struct qla_conn *qla_conn;
2901        struct sockaddr *dst_addr;
2902
2903        conn = cls_conn->dd_data;
2904        qla_conn = conn->dd_data;
2905        dst_addr = (struct sockaddr *)&qla_conn->qla_ep->dst_addr;
2906
2907        switch (param) {
2908        case ISCSI_PARAM_CONN_PORT:
2909        case ISCSI_PARAM_CONN_ADDRESS:
2910                return iscsi_conn_get_addr_param((struct sockaddr_storage *)
2911                                                 dst_addr, param, buf);
2912        default:
2913                return iscsi_conn_get_param(cls_conn, param, buf);
2914        }
2915}
2916
2917int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
2918{
2919        uint32_t mbx_sts = 0;
2920        uint16_t tmp_ddb_index;
2921        int ret;
2922
2923get_ddb_index:
2924        tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
2925
2926        if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
2927                DEBUG2(ql4_printk(KERN_INFO, ha,
2928                                  "Free DDB index not available\n"));
2929                ret = QLA_ERROR;
2930                goto exit_get_ddb_index;
2931        }
2932
2933        if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
2934                goto get_ddb_index;
2935
2936        DEBUG2(ql4_printk(KERN_INFO, ha,
2937                          "Found a free DDB index at %d\n", tmp_ddb_index));
2938        ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
2939        if (ret == QLA_ERROR) {
2940                if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
2941                        ql4_printk(KERN_INFO, ha,
2942                                   "DDB index = %d not available trying next\n",
2943                                   tmp_ddb_index);
2944                        goto get_ddb_index;
2945                }
2946                DEBUG2(ql4_printk(KERN_INFO, ha,
2947                                  "Free FW DDB not available\n"));
2948        }
2949
2950        *ddb_index = tmp_ddb_index;
2951
2952exit_get_ddb_index:
2953        return ret;
2954}
2955
2956static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
2957                                   struct ddb_entry *ddb_entry,
2958                                   char *existing_ipaddr,
2959                                   char *user_ipaddr)
2960{
2961        uint8_t dst_ipaddr[IPv6_ADDR_LEN];
2962        char formatted_ipaddr[DDB_IPADDR_LEN];
2963        int status = QLA_SUCCESS, ret = 0;
2964
2965        if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
2966                ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
2967                               '\0', NULL);
2968                if (ret == 0) {
2969                        status = QLA_ERROR;
2970                        goto out_match;
2971                }
2972                ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
2973        } else {
2974                ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
2975                               '\0', NULL);
2976                if (ret == 0) {
2977                        status = QLA_ERROR;
2978                        goto out_match;
2979                }
2980                ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
2981        }
2982
2983        if (strcmp(existing_ipaddr, formatted_ipaddr))
2984                status = QLA_ERROR;
2985
2986out_match:
2987        return status;
2988}
2989
2990static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
2991                                      struct iscsi_cls_conn *cls_conn)
2992{
2993        int idx = 0, max_ddbs, rval;
2994        struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
2995        struct iscsi_session *sess, *existing_sess;
2996        struct iscsi_conn *conn, *existing_conn;
2997        struct ddb_entry *ddb_entry;
2998
2999        sess = cls_sess->dd_data;
3000        conn = cls_conn->dd_data;
3001
3002        if (sess->targetname == NULL ||
3003            conn->persistent_address == NULL ||
3004            conn->persistent_port == 0)
3005                return QLA_ERROR;
3006
3007        max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
3008                                     MAX_DEV_DB_ENTRIES;
3009
3010        for (idx = 0; idx < max_ddbs; idx++) {
3011                ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
3012                if (ddb_entry == NULL)
3013                        continue;
3014
3015                if (ddb_entry->ddb_type != FLASH_DDB)
3016                        continue;
3017
3018                existing_sess = ddb_entry->sess->dd_data;
3019                existing_conn = ddb_entry->conn->dd_data;
3020
3021                if (existing_sess->targetname == NULL ||
3022                    existing_conn->persistent_address == NULL ||
3023                    existing_conn->persistent_port == 0)
3024                        continue;
3025
3026                DEBUG2(ql4_printk(KERN_INFO, ha,
3027                                  "IQN = %s User IQN = %s\n",
3028                                  existing_sess->targetname,
3029                                  sess->targetname));
3030
3031                DEBUG2(ql4_printk(KERN_INFO, ha,
3032                                  "IP = %s User IP = %s\n",
3033                                  existing_conn->persistent_address,
3034                                  conn->persistent_address));
3035
3036                DEBUG2(ql4_printk(KERN_INFO, ha,
3037                                  "Port = %d User Port = %d\n",
3038                                  existing_conn->persistent_port,
3039                                  conn->persistent_port));
3040
3041                if (strcmp(existing_sess->targetname, sess->targetname))
3042                        continue;
3043                rval = qla4xxx_match_ipaddress(ha, ddb_entry,
3044                                        existing_conn->persistent_address,
3045                                        conn->persistent_address);
3046                if (rval == QLA_ERROR)
3047                        continue;
3048                if (existing_conn->persistent_port != conn->persistent_port)
3049                        continue;
3050                break;
3051        }
3052
3053        if (idx == max_ddbs)
3054                return QLA_ERROR;
3055
3056        DEBUG2(ql4_printk(KERN_INFO, ha,
3057                          "Match found in fwdb sessions\n"));
3058        return QLA_SUCCESS;
3059}
3060
3061static struct iscsi_cls_session *
3062qla4xxx_session_create(struct iscsi_endpoint *ep,
3063                        uint16_t cmds_max, uint16_t qdepth,
3064                        uint32_t initial_cmdsn)
3065{
3066        struct iscsi_cls_session *cls_sess;
3067        struct scsi_qla_host *ha;
3068        struct qla_endpoint *qla_ep;
3069        struct ddb_entry *ddb_entry;
3070        uint16_t ddb_index;
3071        struct iscsi_session *sess;
3072        struct sockaddr *dst_addr;
3073        int ret;
3074
3075        if (!ep) {
3076                printk(KERN_ERR "qla4xxx: missing ep.\n");
3077                return NULL;
3078        }
3079
3080        qla_ep = ep->dd_data;
3081        dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
3082        ha = to_qla_host(qla_ep->host);
3083        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
3084                          ha->host_no));
3085
3086        ret = qla4xxx_get_ddb_index(ha, &ddb_index);
3087        if (ret == QLA_ERROR)
3088                return NULL;
3089
3090        cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
3091                                       cmds_max, sizeof(struct ddb_entry),
3092                                       sizeof(struct ql4_task_data),
3093                                       initial_cmdsn, ddb_index);
3094        if (!cls_sess)
3095                return NULL;
3096
3097        sess = cls_sess->dd_data;
3098        ddb_entry = sess->dd_data;
3099        ddb_entry->fw_ddb_index = ddb_index;
3100        ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
3101        ddb_entry->ha = ha;
3102        ddb_entry->sess = cls_sess;
3103        ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
3104        ddb_entry->ddb_change = qla4xxx_ddb_change;
3105        clear_bit(DDB_CONN_CLOSE_FAILURE, &ddb_entry->flags);
3106        cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
3107        ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
3108        ha->tot_ddbs++;
3109
3110        return cls_sess;
3111}
3112
3113static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
3114{
3115        struct iscsi_session *sess;
3116        struct ddb_entry *ddb_entry;
3117        struct scsi_qla_host *ha;
3118        unsigned long flags, wtime;
3119        struct dev_db_entry *fw_ddb_entry = NULL;
3120        dma_addr_t fw_ddb_entry_dma;
3121        uint32_t ddb_state;
3122        int ret;
3123
3124        sess = cls_sess->dd_data;
3125        ddb_entry = sess->dd_data;
3126        ha = ddb_entry->ha;
3127        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: host: %ld\n", __func__,
3128                          ha->host_no));
3129
3130        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3131                                          &fw_ddb_entry_dma, GFP_KERNEL);
3132        if (!fw_ddb_entry) {
3133                ql4_printk(KERN_ERR, ha,
3134                           "%s: Unable to allocate dma buffer\n", __func__);
3135                goto destroy_session;
3136        }
3137
3138        wtime = jiffies + (HZ * LOGOUT_TOV);
3139        do {
3140                ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
3141                                              fw_ddb_entry, fw_ddb_entry_dma,
3142                                              NULL, NULL, &ddb_state, NULL,
3143                                              NULL, NULL);
3144                if (ret == QLA_ERROR)
3145                        goto destroy_session;
3146
3147                if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
3148                    (ddb_state == DDB_DS_SESSION_FAILED))
3149                        goto destroy_session;
3150
3151                schedule_timeout_uninterruptible(HZ);
3152        } while ((time_after(wtime, jiffies)));
3153
3154destroy_session:
3155        qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
3156        if (test_and_clear_bit(DDB_CONN_CLOSE_FAILURE, &ddb_entry->flags))
3157                clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
3158        spin_lock_irqsave(&ha->hardware_lock, flags);
3159        qla4xxx_free_ddb(ha, ddb_entry);
3160        spin_unlock_irqrestore(&ha->hardware_lock, flags);
3161
3162        iscsi_session_teardown(cls_sess);
3163
3164        if (fw_ddb_entry)
3165                dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3166                                  fw_ddb_entry, fw_ddb_entry_dma);
3167}
3168
3169static struct iscsi_cls_conn *
3170qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
3171{
3172        struct iscsi_cls_conn *cls_conn;
3173        struct iscsi_session *sess;
3174        struct ddb_entry *ddb_entry;
3175        struct scsi_qla_host *ha;
3176
3177        cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
3178                                    conn_idx);
3179        if (!cls_conn) {
3180                pr_info("%s: Can not create connection for conn_idx = %u\n",
3181                        __func__, conn_idx);
3182                return NULL;
3183        }
3184
3185        sess = cls_sess->dd_data;
3186        ddb_entry = sess->dd_data;
3187        ddb_entry->conn = cls_conn;
3188
3189        ha = ddb_entry->ha;
3190        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: conn_idx = %u\n", __func__,
3191                          conn_idx));
3192        return cls_conn;
3193}
3194
3195static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
3196                             struct iscsi_cls_conn *cls_conn,
3197                             uint64_t transport_fd, int is_leading)
3198{
3199        struct iscsi_conn *conn;
3200        struct qla_conn *qla_conn;
3201        struct iscsi_endpoint *ep;
3202        struct ddb_entry *ddb_entry;
3203        struct scsi_qla_host *ha;
3204        struct iscsi_session *sess;
3205
3206        sess = cls_session->dd_data;
3207        ddb_entry = sess->dd_data;
3208        ha = ddb_entry->ha;
3209
3210        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: sid = %d, cid = %d\n", __func__,
3211                          cls_session->sid, cls_conn->cid));
3212
3213        if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
3214                return -EINVAL;
3215        ep = iscsi_lookup_endpoint(transport_fd);
3216        conn = cls_conn->dd_data;
3217        qla_conn = conn->dd_data;
3218        qla_conn->qla_ep = ep->dd_data;
3219        return 0;
3220}
3221
3222static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
3223{
3224        struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
3225        struct iscsi_session *sess;
3226        struct ddb_entry *ddb_entry;
3227        struct scsi_qla_host *ha;
3228        struct dev_db_entry *fw_ddb_entry = NULL;
3229        dma_addr_t fw_ddb_entry_dma;
3230        uint32_t mbx_sts = 0;
3231        int ret = 0;
3232        int status = QLA_SUCCESS;
3233
3234        sess = cls_sess->dd_data;
3235        ddb_entry = sess->dd_data;
3236        ha = ddb_entry->ha;
3237        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: sid = %d, cid = %d\n", __func__,
3238                          cls_sess->sid, cls_conn->cid));
3239
3240        /* Check if we have  matching FW DDB, if yes then do not
3241         * login to this target. This could cause target to logout previous
3242         * connection
3243         */
3244        ret = qla4xxx_match_fwdb_session(ha, cls_conn);
3245        if (ret == QLA_SUCCESS) {
3246                ql4_printk(KERN_INFO, ha,
3247                           "Session already exist in FW.\n");
3248                ret = -EEXIST;
3249                goto exit_conn_start;
3250        }
3251
3252        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3253                                          &fw_ddb_entry_dma, GFP_KERNEL);
3254        if (!fw_ddb_entry) {
3255                ql4_printk(KERN_ERR, ha,
3256                           "%s: Unable to allocate dma buffer\n", __func__);
3257                ret = -ENOMEM;
3258                goto exit_conn_start;
3259        }
3260
3261        ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
3262        if (ret) {
3263                /* If iscsid is stopped and started then no need to do
3264                * set param again since ddb state will be already
3265                * active and FW does not allow set ddb to an
3266                * active session.
3267                */
3268                if (mbx_sts)
3269                        if (ddb_entry->fw_ddb_device_state ==
3270                                                DDB_DS_SESSION_ACTIVE) {
3271                                ddb_entry->unblock_sess(ddb_entry->sess);
3272                                goto exit_set_param;
3273                        }
3274
3275                ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
3276                           __func__, ddb_entry->fw_ddb_index);
3277                goto exit_conn_start;
3278        }
3279
3280        status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
3281        if (status == QLA_ERROR) {
3282                ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
3283                           sess->targetname);
3284                ret = -EINVAL;
3285                goto exit_conn_start;
3286        }
3287
3288        if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
3289                ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
3290
3291        DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
3292                      ddb_entry->fw_ddb_device_state));
3293
3294exit_set_param:
3295        ret = 0;
3296
3297exit_conn_start:
3298        if (fw_ddb_entry)
3299                dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3300                                  fw_ddb_entry, fw_ddb_entry_dma);
3301        return ret;
3302}
3303
3304static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
3305{
3306        struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
3307        struct iscsi_session *sess;
3308        struct scsi_qla_host *ha;
3309        struct ddb_entry *ddb_entry;
3310        int options;
3311
3312        sess = cls_sess->dd_data;
3313        ddb_entry = sess->dd_data;
3314        ha = ddb_entry->ha;
3315        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: cid = %d\n", __func__,
3316                          cls_conn->cid));
3317
3318        options = LOGOUT_OPTION_CLOSE_SESSION;
3319        if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
3320                ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
3321}
3322
3323static void qla4xxx_task_work(struct work_struct *wdata)
3324{
3325        struct ql4_task_data *task_data;
3326        struct scsi_qla_host *ha;
3327        struct passthru_status *sts;
3328        struct iscsi_task *task;
3329        struct iscsi_hdr *hdr;
3330        uint8_t *data;
3331        uint32_t data_len;
3332        struct iscsi_conn *conn;
3333        int hdr_len;
3334        itt_t itt;
3335
3336        task_data = container_of(wdata, struct ql4_task_data, task_work);
3337        ha = task_data->ha;
3338        task = task_data->task;
3339        sts = &task_data->sts;
3340        hdr_len = sizeof(struct iscsi_hdr);
3341
3342        DEBUG3(printk(KERN_INFO "Status returned\n"));
3343        DEBUG3(qla4xxx_dump_buffer(sts, 64));
3344        DEBUG3(printk(KERN_INFO "Response buffer"));
3345        DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
3346
3347        conn = task->conn;
3348
3349        switch (sts->completionStatus) {
3350        case PASSTHRU_STATUS_COMPLETE:
3351                hdr = (struct iscsi_hdr *)task_data->resp_buffer;
3352                /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
3353                itt = sts->handle;
3354                hdr->itt = itt;
3355                data = task_data->resp_buffer + hdr_len;
3356                data_len = task_data->resp_len - hdr_len;
3357                iscsi_complete_pdu(conn, hdr, data, data_len);
3358                break;
3359        default:
3360                ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
3361                           sts->completionStatus);
3362                break;
3363        }
3364        return;
3365}
3366
3367static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
3368{
3369        struct ql4_task_data *task_data;
3370        struct iscsi_session *sess;
3371        struct ddb_entry *ddb_entry;
3372        struct scsi_qla_host *ha;
3373        int hdr_len;
3374
3375        sess = task->conn->session;
3376        ddb_entry = sess->dd_data;
3377        ha = ddb_entry->ha;
3378        task_data = task->dd_data;
3379        memset(task_data, 0, sizeof(struct ql4_task_data));
3380
3381        if (task->sc) {
3382                ql4_printk(KERN_INFO, ha,
3383                           "%s: SCSI Commands not implemented\n", __func__);
3384                return -EINVAL;
3385        }
3386
3387        hdr_len = sizeof(struct iscsi_hdr);
3388        task_data->ha = ha;
3389        task_data->task = task;
3390
3391        if (task->data_count) {
3392                task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
3393                                                     task->data_count,
3394                                                     PCI_DMA_TODEVICE);
3395        }
3396
3397        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
3398                      __func__, task->conn->max_recv_dlength, hdr_len));
3399
3400        task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
3401        task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
3402                                                    task_data->resp_len,
3403                                                    &task_data->resp_dma,
3404                                                    GFP_ATOMIC);
3405        if (!task_data->resp_buffer)
3406                goto exit_alloc_pdu;
3407
3408        task_data->req_len = task->data_count + hdr_len;
3409        task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
3410                                                   task_data->req_len,
3411                                                   &task_data->req_dma,
3412                                                   GFP_ATOMIC);
3413        if (!task_data->req_buffer)
3414                goto exit_alloc_pdu;
3415
3416        task->hdr = task_data->req_buffer;
3417
3418        INIT_WORK(&task_data->task_work, qla4xxx_task_work);
3419
3420        return 0;
3421
3422exit_alloc_pdu:
3423        if (task_data->resp_buffer)
3424                dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
3425                                  task_data->resp_buffer, task_data->resp_dma);
3426
3427        if (task_data->req_buffer)
3428                dma_free_coherent(&ha->pdev->dev, task_data->req_len,
3429                                  task_data->req_buffer, task_data->req_dma);
3430        return -ENOMEM;
3431}
3432
3433static void qla4xxx_task_cleanup(struct iscsi_task *task)
3434{
3435        struct ql4_task_data *task_data;
3436        struct iscsi_session *sess;
3437        struct ddb_entry *ddb_entry;
3438        struct scsi_qla_host *ha;
3439        int hdr_len;
3440
3441        hdr_len = sizeof(struct iscsi_hdr);
3442        sess = task->conn->session;
3443        ddb_entry = sess->dd_data;
3444        ha = ddb_entry->ha;
3445        task_data = task->dd_data;
3446
3447        if (task->data_count) {
3448                dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
3449                                 task->data_count, PCI_DMA_TODEVICE);
3450        }
3451
3452        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
3453                      __func__, task->conn->max_recv_dlength, hdr_len));
3454
3455        dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
3456                          task_data->resp_buffer, task_data->resp_dma);
3457        dma_free_coherent(&ha->pdev->dev, task_data->req_len,
3458                          task_data->req_buffer, task_data->req_dma);
3459        return;
3460}
3461
3462static int qla4xxx_task_xmit(struct iscsi_task *task)
3463{
3464        struct scsi_cmnd *sc = task->sc;
3465        struct iscsi_session *sess = task->conn->session;
3466        struct ddb_entry *ddb_entry = sess->dd_data;
3467        struct scsi_qla_host *ha = ddb_entry->ha;
3468
3469        if (!sc)
3470                return qla4xxx_send_passthru0(task);
3471
3472        ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
3473                   __func__);
3474        return -ENOSYS;
3475}
3476
3477static int qla4xxx_copy_from_fwddb_param(struct iscsi_bus_flash_session *sess,
3478                                         struct iscsi_bus_flash_conn *conn,
3479                                         struct dev_db_entry *fw_ddb_entry)
3480{
3481        unsigned long options = 0;
3482        int rc = 0;
3483
3484        options = le16_to_cpu(fw_ddb_entry->options);
3485        conn->is_fw_assigned_ipv6 = test_bit(OPT_IS_FW_ASSIGNED_IPV6, &options);
3486        if (test_bit(OPT_IPV6_DEVICE, &options)) {
3487                rc = iscsi_switch_str_param(&sess->portal_type,
3488                                            PORTAL_TYPE_IPV6);
3489                if (rc)
3490                        goto exit_copy;
3491        } else {
3492                rc = iscsi_switch_str_param(&sess->portal_type,
3493                                            PORTAL_TYPE_IPV4);
3494                if (rc)
3495                        goto exit_copy;
3496        }
3497
3498        sess->auto_snd_tgt_disable = test_bit(OPT_AUTO_SENDTGTS_DISABLE,
3499                                              &options);
3500        sess->discovery_sess = test_bit(OPT_DISC_SESSION, &options);
3501        sess->entry_state = test_bit(OPT_ENTRY_STATE, &options);
3502
3503        options = le16_to_cpu(fw_ddb_entry->iscsi_options);
3504        conn->hdrdgst_en = test_bit(ISCSIOPT_HEADER_DIGEST_EN, &options);
3505        conn->datadgst_en = test_bit(ISCSIOPT_DATA_DIGEST_EN, &options);
3506        sess->imm_data_en = test_bit(ISCSIOPT_IMMEDIATE_DATA_EN, &options);
3507        sess->initial_r2t_en = test_bit(ISCSIOPT_INITIAL_R2T_EN, &options);
3508        sess->dataseq_inorder_en = test_bit(ISCSIOPT_DATA_SEQ_IN_ORDER,
3509                                            &options);
3510        sess->pdu_inorder_en = test_bit(ISCSIOPT_DATA_PDU_IN_ORDER, &options);
3511        sess->chap_auth_en = test_bit(ISCSIOPT_CHAP_AUTH_EN, &options);
3512        conn->snack_req_en = test_bit(ISCSIOPT_SNACK_REQ_EN, &options);
3513        sess->discovery_logout_en = test_bit(ISCSIOPT_DISCOVERY_LOGOUT_EN,
3514                                             &options);
3515        sess->bidi_chap_en = test_bit(ISCSIOPT_BIDI_CHAP_EN, &options);
3516        sess->discovery_auth_optional =
3517                        test_bit(ISCSIOPT_DISCOVERY_AUTH_OPTIONAL, &options);
3518        if (test_bit(ISCSIOPT_ERL1, &options))
3519                sess->erl |= BIT_1;
3520        if (test_bit(ISCSIOPT_ERL0, &options))
3521                sess->erl |= BIT_0;
3522
3523        options = le16_to_cpu(fw_ddb_entry->tcp_options);
3524        conn->tcp_timestamp_stat = test_bit(TCPOPT_TIMESTAMP_STAT, &options);
3525        conn->tcp_nagle_disable = test_bit(TCPOPT_NAGLE_DISABLE, &options);
3526        conn->tcp_wsf_disable = test_bit(TCPOPT_WSF_DISABLE, &options);
3527        if (test_bit(TCPOPT_TIMER_SCALE3, &options))
3528                conn->tcp_timer_scale |= BIT_3;
3529        if (test_bit(TCPOPT_TIMER_SCALE2, &options))
3530                conn->tcp_timer_scale |= BIT_2;
3531        if (test_bit(TCPOPT_TIMER_SCALE1, &options))
3532                conn->tcp_timer_scale |= BIT_1;
3533
3534        conn->tcp_timer_scale >>= 1;
3535        conn->tcp_timestamp_en = test_bit(TCPOPT_TIMESTAMP_EN, &options);
3536
3537        options = le16_to_cpu(fw_ddb_entry->ip_options);
3538        conn->fragment_disable = test_bit(IPOPT_FRAGMENT_DISABLE, &options);
3539
3540        conn->max_recv_dlength = BYTE_UNITS *
3541                          le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
3542        conn->max_xmit_dlength = BYTE_UNITS *
3543                          le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
3544        sess->first_burst = BYTE_UNITS *
3545                               le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
3546        sess->max_burst = BYTE_UNITS *
3547                                 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
3548        sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
3549        sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
3550        sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
3551        sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3552        conn->max_segment_size = le16_to_cpu(fw_ddb_entry->mss);
3553        conn->tcp_xmit_wsf = fw_ddb_entry->tcp_xmt_wsf;
3554        conn->tcp_recv_wsf = fw_ddb_entry->tcp_rcv_wsf;
3555        conn->ipv6_flow_label = le16_to_cpu(fw_ddb_entry->ipv6_flow_lbl);
3556        conn->keepalive_timeout = le16_to_cpu(fw_ddb_entry->ka_timeout);
3557        conn->local_port = le16_to_cpu(fw_ddb_entry->lcl_port);
3558        conn->statsn = le32_to_cpu(fw_ddb_entry->stat_sn);
3559        conn->exp_statsn = le32_to_cpu(fw_ddb_entry->exp_stat_sn);
3560        sess->discovery_parent_idx = le16_to_cpu(fw_ddb_entry->ddb_link);
3561        sess->discovery_parent_type = le16_to_cpu(fw_ddb_entry->ddb_link);
3562        sess->chap_out_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3563        sess->tsid = le16_to_cpu(fw_ddb_entry->tsid);
3564
3565        sess->default_taskmgmt_timeout =
3566                                le16_to_cpu(fw_ddb_entry->def_timeout);
3567        conn->port = le16_to_cpu(fw_ddb_entry->port);
3568
3569        options = le16_to_cpu(fw_ddb_entry->options);
3570        conn->ipaddress = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
3571        if (!conn->ipaddress) {
3572                rc = -ENOMEM;
3573                goto exit_copy;
3574        }
3575
3576        conn->redirect_ipaddr = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
3577        if (!conn->redirect_ipaddr) {
3578                rc = -ENOMEM;
3579                goto exit_copy;
3580        }
3581
3582        memcpy(conn->ipaddress, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
3583        memcpy(conn->redirect_ipaddr, fw_ddb_entry->tgt_addr, IPv6_ADDR_LEN);
3584
3585        if (test_bit(OPT_IPV6_DEVICE, &options)) {
3586                conn->ipv6_traffic_class = fw_ddb_entry->ipv4_tos;
3587
3588                conn->link_local_ipv6_addr = kmemdup(
3589                                        fw_ddb_entry->link_local_ipv6_addr,
3590                                        IPv6_ADDR_LEN, GFP_KERNEL);
3591                if (!conn->link_local_ipv6_addr) {
3592                        rc = -ENOMEM;
3593                        goto exit_copy;
3594                }
3595        } else {
3596                conn->ipv4_tos = fw_ddb_entry->ipv4_tos;
3597        }
3598
3599        if (fw_ddb_entry->iscsi_name[0]) {
3600                rc = iscsi_switch_str_param(&sess->targetname,
3601                                            (char *)fw_ddb_entry->iscsi_name);
3602                if (rc)
3603                        goto exit_copy;
3604        }
3605
3606        if (fw_ddb_entry->iscsi_alias[0]) {
3607                rc = iscsi_switch_str_param(&sess->targetalias,
3608                                            (char *)fw_ddb_entry->iscsi_alias);
3609                if (rc)
3610                        goto exit_copy;
3611        }
3612
3613        COPY_ISID(sess->isid, fw_ddb_entry->isid);
3614
3615exit_copy:
3616        return rc;
3617}
3618
3619static int qla4xxx_copy_to_fwddb_param(struct iscsi_bus_flash_session *sess,
3620                                       struct iscsi_bus_flash_conn *conn,
3621                                       struct dev_db_entry *fw_ddb_entry)
3622{
3623        uint16_t options;
3624        int rc = 0;
3625
3626        options = le16_to_cpu(fw_ddb_entry->options);
3627        SET_BITVAL(conn->is_fw_assigned_ipv6,  options, BIT_11);
3628        if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
3629                options |= BIT_8;
3630        else
3631                options &= ~BIT_8;
3632
3633        SET_BITVAL(sess->auto_snd_tgt_disable, options, BIT_6);
3634        SET_BITVAL(sess->discovery_sess, options, BIT_4);
3635        SET_BITVAL(sess->entry_state, options, BIT_3);
3636        fw_ddb_entry->options = cpu_to_le16(options);
3637
3638        options = le16_to_cpu(fw_ddb_entry->iscsi_options);
3639        SET_BITVAL(conn->hdrdgst_en, options, BIT_13);
3640        SET_BITVAL(conn->datadgst_en, options, BIT_12);
3641        SET_BITVAL(sess->imm_data_en, options, BIT_11);
3642        SET_BITVAL(sess->initial_r2t_en, options, BIT_10);
3643        SET_BITVAL(sess->dataseq_inorder_en, options, BIT_9);
3644        SET_BITVAL(sess->pdu_inorder_en, options, BIT_8);
3645        SET_BITVAL(sess->chap_auth_en, options, BIT_7);
3646        SET_BITVAL(conn->snack_req_en, options, BIT_6);
3647        SET_BITVAL(sess->discovery_logout_en, options, BIT_5);
3648        SET_BITVAL(sess->bidi_chap_en, options, BIT_4);
3649        SET_BITVAL(sess->discovery_auth_optional, options, BIT_3);
3650        SET_BITVAL(sess->erl & BIT_1, options, BIT_1);
3651        SET_BITVAL(sess->erl & BIT_0, options, BIT_0);
3652        fw_ddb_entry->iscsi_options = cpu_to_le16(options);
3653
3654        options = le16_to_cpu(fw_ddb_entry->tcp_options);
3655        SET_BITVAL(conn->tcp_timestamp_stat, options, BIT_6);
3656        SET_BITVAL(conn->tcp_nagle_disable, options, BIT_5);
3657        SET_BITVAL(conn->tcp_wsf_disable, options, BIT_4);
3658        SET_BITVAL(conn->tcp_timer_scale & BIT_2, options, BIT_3);
3659        SET_BITVAL(conn->tcp_timer_scale & BIT_1, options, BIT_2);
3660        SET_BITVAL(conn->tcp_timer_scale & BIT_0, options, BIT_1);
3661        SET_BITVAL(conn->tcp_timestamp_en, options, BIT_0);
3662        fw_ddb_entry->tcp_options = cpu_to_le16(options);
3663
3664        options = le16_to_cpu(fw_ddb_entry->ip_options);
3665        SET_BITVAL(conn->fragment_disable, options, BIT_4);
3666        fw_ddb_entry->ip_options = cpu_to_le16(options);
3667
3668        fw_ddb_entry->iscsi_max_outsnd_r2t = cpu_to_le16(sess->max_r2t);
3669        fw_ddb_entry->iscsi_max_rcv_data_seg_len =
3670                               cpu_to_le16(conn->max_recv_dlength / BYTE_UNITS);
3671        fw_ddb_entry->iscsi_max_snd_data_seg_len =
3672                               cpu_to_le16(conn->max_xmit_dlength / BYTE_UNITS);
3673        fw_ddb_entry->iscsi_first_burst_len =
3674                                cpu_to_le16(sess->first_burst / BYTE_UNITS);
3675        fw_ddb_entry->iscsi_max_burst_len = cpu_to_le16(sess->max_burst /
3676                                            BYTE_UNITS);
3677        fw_ddb_entry->iscsi_def_time2wait = cpu_to_le16(sess->time2wait);
3678        fw_ddb_entry->iscsi_def_time2retain = cpu_to_le16(sess->time2retain);
3679        fw_ddb_entry->tgt_portal_grp = cpu_to_le16(sess->tpgt);
3680        fw_ddb_entry->mss = cpu_to_le16(conn->max_segment_size);
3681        fw_ddb_entry->tcp_xmt_wsf = (uint8_t) cpu_to_le32(conn->tcp_xmit_wsf);
3682        fw_ddb_entry->tcp_rcv_wsf = (uint8_t) cpu_to_le32(conn->tcp_recv_wsf);
3683        fw_ddb_entry->ipv6_flow_lbl = cpu_to_le16(conn->ipv6_flow_label);
3684        fw_ddb_entry->ka_timeout = cpu_to_le16(conn->keepalive_timeout);
3685        fw_ddb_entry->lcl_port = cpu_to_le16(conn->local_port);
3686        fw_ddb_entry->stat_sn = cpu_to_le32(conn->statsn);
3687        fw_ddb_entry->exp_stat_sn = cpu_to_le32(conn->exp_statsn);
3688        fw_ddb_entry->ddb_link = cpu_to_le16(sess->discovery_parent_idx);
3689        fw_ddb_entry->chap_tbl_idx = cpu_to_le16(sess->chap_out_idx);
3690        fw_ddb_entry->tsid = cpu_to_le16(sess->tsid);
3691        fw_ddb_entry->port = cpu_to_le16(conn->port);
3692        fw_ddb_entry->def_timeout =
3693                                cpu_to_le16(sess->default_taskmgmt_timeout);
3694
3695        if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
3696                fw_ddb_entry->ipv4_tos = conn->ipv6_traffic_class;
3697        else
3698                fw_ddb_entry->ipv4_tos = conn->ipv4_tos;
3699
3700        if (conn->ipaddress)
3701                memcpy(fw_ddb_entry->ip_addr, conn->ipaddress,
3702                       sizeof(fw_ddb_entry->ip_addr));
3703
3704        if (conn->redirect_ipaddr)
3705                memcpy(fw_ddb_entry->tgt_addr, conn->redirect_ipaddr,
3706                       sizeof(fw_ddb_entry->tgt_addr));
3707
3708        if (conn->link_local_ipv6_addr)
3709                memcpy(fw_ddb_entry->link_local_ipv6_addr,
3710                       conn->link_local_ipv6_addr,
3711                       sizeof(fw_ddb_entry->link_local_ipv6_addr));
3712
3713        if (sess->targetname)
3714                memcpy(fw_ddb_entry->iscsi_name, sess->targetname,
3715                       sizeof(fw_ddb_entry->iscsi_name));
3716
3717        if (sess->targetalias)
3718                memcpy(fw_ddb_entry->iscsi_alias, sess->targetalias,
3719                       sizeof(fw_ddb_entry->iscsi_alias));
3720
3721        COPY_ISID(fw_ddb_entry->isid, sess->isid);
3722
3723        return rc;
3724}
3725
3726static void qla4xxx_copy_to_sess_conn_params(struct iscsi_conn *conn,
3727                                             struct iscsi_session *sess,
3728                                             struct dev_db_entry *fw_ddb_entry)
3729{
3730        unsigned long options = 0;
3731        uint16_t ddb_link;
3732        uint16_t disc_parent;
3733        char ip_addr[DDB_IPADDR_LEN];
3734
3735        options = le16_to_cpu(fw_ddb_entry->options);
3736        conn->is_fw_assigned_ipv6 = test_bit(OPT_IS_FW_ASSIGNED_IPV6, &options);
3737        sess->auto_snd_tgt_disable = test_bit(OPT_AUTO_SENDTGTS_DISABLE,
3738                                              &options);
3739        sess->discovery_sess = test_bit(OPT_DISC_SESSION, &options);
3740
3741        options = le16_to_cpu(fw_ddb_entry->iscsi_options);
3742        conn->hdrdgst_en = test_bit(ISCSIOPT_HEADER_DIGEST_EN, &options);
3743        conn->datadgst_en = test_bit(ISCSIOPT_DATA_DIGEST_EN, &options);
3744        sess->imm_data_en = test_bit(ISCSIOPT_IMMEDIATE_DATA_EN, &options);
3745        sess->initial_r2t_en = test_bit(ISCSIOPT_INITIAL_R2T_EN, &options);
3746        sess->dataseq_inorder_en = test_bit(ISCSIOPT_DATA_SEQ_IN_ORDER,
3747                                            &options);
3748        sess->pdu_inorder_en = test_bit(ISCSIOPT_DATA_PDU_IN_ORDER, &options);
3749        sess->chap_auth_en = test_bit(ISCSIOPT_CHAP_AUTH_EN, &options);
3750        sess->discovery_logout_en = test_bit(ISCSIOPT_DISCOVERY_LOGOUT_EN,
3751                                             &options);
3752        sess->bidi_chap_en = test_bit(ISCSIOPT_BIDI_CHAP_EN, &options);
3753        sess->discovery_auth_optional =
3754                        test_bit(ISCSIOPT_DISCOVERY_AUTH_OPTIONAL, &options);
3755        if (test_bit(ISCSIOPT_ERL1, &options))
3756                sess->erl |= BIT_1;
3757        if (test_bit(ISCSIOPT_ERL0, &options))
3758                sess->erl |= BIT_0;
3759
3760        options = le16_to_cpu(fw_ddb_entry->tcp_options);
3761        conn->tcp_timestamp_stat = test_bit(TCPOPT_TIMESTAMP_STAT, &options);
3762        conn->tcp_nagle_disable = test_bit(TCPOPT_NAGLE_DISABLE, &options);
3763        conn->tcp_wsf_disable = test_bit(TCPOPT_WSF_DISABLE, &options);
3764        if (test_bit(TCPOPT_TIMER_SCALE3, &options))
3765                conn->tcp_timer_scale |= BIT_3;
3766        if (test_bit(TCPOPT_TIMER_SCALE2, &options))
3767                conn->tcp_timer_scale |= BIT_2;
3768        if (test_bit(TCPOPT_TIMER_SCALE1, &options))
3769                conn->tcp_timer_scale |= BIT_1;
3770
3771        conn->tcp_timer_scale >>= 1;
3772        conn->tcp_timestamp_en = test_bit(TCPOPT_TIMESTAMP_EN, &options);
3773
3774        options = le16_to_cpu(fw_ddb_entry->ip_options);
3775        conn->fragment_disable = test_bit(IPOPT_FRAGMENT_DISABLE, &options);
3776
3777        conn->max_recv_dlength = BYTE_UNITS *
3778                          le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
3779        conn->max_xmit_dlength = BYTE_UNITS *
3780                          le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
3781        sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
3782        sess->first_burst = BYTE_UNITS *
3783                               le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
3784        sess->max_burst = BYTE_UNITS *
3785                                 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
3786        sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
3787        sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
3788        sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
3789        conn->max_segment_size = le16_to_cpu(fw_ddb_entry->mss);
3790        conn->tcp_xmit_wsf = fw_ddb_entry->tcp_xmt_wsf;
3791        conn->tcp_recv_wsf = fw_ddb_entry->tcp_rcv_wsf;
3792        conn->ipv4_tos = fw_ddb_entry->ipv4_tos;
3793        conn->keepalive_tmo = le16_to_cpu(fw_ddb_entry->ka_timeout);
3794        conn->local_port = le16_to_cpu(fw_ddb_entry->lcl_port);
3795        conn->statsn = le32_to_cpu(fw_ddb_entry->stat_sn);
3796        conn->exp_statsn = le32_to_cpu(fw_ddb_entry->exp_stat_sn);
3797        sess->tsid = le16_to_cpu(fw_ddb_entry->tsid);
3798        COPY_ISID(sess->isid, fw_ddb_entry->isid);
3799
3800        ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
3801        if (ddb_link == DDB_ISNS)
3802                disc_parent = ISCSI_DISC_PARENT_ISNS;
3803        else if (ddb_link == DDB_NO_LINK)
3804                disc_parent = ISCSI_DISC_PARENT_UNKNOWN;
3805        else if (ddb_link < MAX_DDB_ENTRIES)
3806                disc_parent = ISCSI_DISC_PARENT_SENDTGT;
3807        else
3808                disc_parent = ISCSI_DISC_PARENT_UNKNOWN;
3809
3810        iscsi_set_param(conn->cls_conn, ISCSI_PARAM_DISCOVERY_PARENT_TYPE,
3811                        iscsi_get_discovery_parent_name(disc_parent), 0);
3812
3813        iscsi_set_param(conn->cls_conn, ISCSI_PARAM_TARGET_ALIAS,
3814                        (char *)fw_ddb_entry->iscsi_alias, 0);
3815
3816        options = le16_to_cpu(fw_ddb_entry->options);
3817        if (options & DDB_OPT_IPV6_DEVICE) {
3818                memset(ip_addr, 0, sizeof(ip_addr));
3819                sprintf(ip_addr, "%pI6", fw_ddb_entry->link_local_ipv6_addr);
3820                iscsi_set_param(conn->cls_conn, ISCSI_PARAM_LOCAL_IPADDR,
3821                                (char *)ip_addr, 0);
3822        }
3823}
3824
3825static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
3826                                     struct dev_db_entry *fw_ddb_entry,
3827                                     struct iscsi_cls_session *cls_sess,
3828                                     struct iscsi_cls_conn *cls_conn)
3829{
3830        int buflen = 0;
3831        struct iscsi_session *sess;
3832        struct ddb_entry *ddb_entry;
3833        struct ql4_chap_table chap_tbl;
3834        struct iscsi_conn *conn;
3835        char ip_addr[DDB_IPADDR_LEN];
3836        uint16_t options = 0;
3837
3838        sess = cls_sess->dd_data;
3839        ddb_entry = sess->dd_data;
3840        conn = cls_conn->dd_data;
3841        memset(&chap_tbl, 0, sizeof(chap_tbl));
3842
3843        ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3844
3845        qla4xxx_copy_to_sess_conn_params(conn, sess, fw_ddb_entry);
3846
3847        sess->def_taskmgmt_tmo = le16_to_cpu(fw_ddb_entry->def_timeout);
3848        conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
3849
3850        memset(ip_addr, 0, sizeof(ip_addr));
3851        options = le16_to_cpu(fw_ddb_entry->options);
3852        if (options & DDB_OPT_IPV6_DEVICE) {
3853                iscsi_set_param(cls_conn, ISCSI_PARAM_PORTAL_TYPE, "ipv6", 4);
3854
3855                memset(ip_addr, 0, sizeof(ip_addr));
3856                sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
3857        } else {
3858                iscsi_set_param(cls_conn, ISCSI_PARAM_PORTAL_TYPE, "ipv4", 4);
3859                sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
3860        }
3861
3862        iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
3863                        (char *)ip_addr, buflen);
3864        iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
3865                        (char *)fw_ddb_entry->iscsi_name, buflen);
3866        iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
3867                        (char *)ha->name_string, buflen);
3868
3869        if (ddb_entry->chap_tbl_idx != INVALID_ENTRY) {
3870                if (!qla4xxx_get_uni_chap_at_index(ha, chap_tbl.name,
3871                                                   chap_tbl.secret,
3872                                                   ddb_entry->chap_tbl_idx)) {
3873                        iscsi_set_param(cls_conn, ISCSI_PARAM_USERNAME,
3874                                        (char *)chap_tbl.name,
3875                                        strlen((char *)chap_tbl.name));
3876                        iscsi_set_param(cls_conn, ISCSI_PARAM_PASSWORD,
3877                                        (char *)chap_tbl.secret,
3878                                        chap_tbl.secret_len);
3879                }
3880        }
3881}
3882
3883void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
3884                                             struct ddb_entry *ddb_entry)
3885{
3886        struct iscsi_cls_session *cls_sess;
3887        struct iscsi_cls_conn *cls_conn;
3888        uint32_t ddb_state;
3889        dma_addr_t fw_ddb_entry_dma;
3890        struct dev_db_entry *fw_ddb_entry;
3891
3892        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3893                                          &fw_ddb_entry_dma, GFP_KERNEL);
3894        if (!fw_ddb_entry) {
3895                ql4_printk(KERN_ERR, ha,
3896                           "%s: Unable to allocate dma buffer\n", __func__);
3897                goto exit_session_conn_fwddb_param;
3898        }
3899
3900        if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
3901                                    fw_ddb_entry_dma, NULL, NULL, &ddb_state,
3902                                    NULL, NULL, NULL) == QLA_ERROR) {
3903                DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
3904                                  "get_ddb_entry for fw_ddb_index %d\n",
3905                                  ha->host_no, __func__,
3906                                  ddb_entry->fw_ddb_index));
3907                goto exit_session_conn_fwddb_param;
3908        }
3909
3910        cls_sess = ddb_entry->sess;
3911
3912        cls_conn = ddb_entry->conn;
3913
3914        /* Update params */
3915        qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
3916
3917exit_session_conn_fwddb_param:
3918        if (fw_ddb_entry)
3919                dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3920                                  fw_ddb_entry, fw_ddb_entry_dma);
3921}
3922
3923void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
3924                                       struct ddb_entry *ddb_entry)
3925{
3926        struct iscsi_cls_session *cls_sess;
3927        struct iscsi_cls_conn *cls_conn;
3928        struct iscsi_session *sess;
3929        struct iscsi_conn *conn;
3930        uint32_t ddb_state;
3931        dma_addr_t fw_ddb_entry_dma;
3932        struct dev_db_entry *fw_ddb_entry;
3933
3934        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3935                                          &fw_ddb_entry_dma, GFP_KERNEL);
3936        if (!fw_ddb_entry) {
3937                ql4_printk(KERN_ERR, ha,
3938                           "%s: Unable to allocate dma buffer\n", __func__);
3939                goto exit_session_conn_param;
3940        }
3941
3942        if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
3943                                    fw_ddb_entry_dma, NULL, NULL, &ddb_state,
3944                                    NULL, NULL, NULL) == QLA_ERROR) {
3945                DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
3946                                  "get_ddb_entry for fw_ddb_index %d\n",
3947                                  ha->host_no, __func__,
3948                                  ddb_entry->fw_ddb_index));
3949                goto exit_session_conn_param;
3950        }
3951
3952        cls_sess = ddb_entry->sess;
3953        sess = cls_sess->dd_data;
3954
3955        cls_conn = ddb_entry->conn;
3956        conn = cls_conn->dd_data;
3957
3958        /* Update timers after login */
3959        ddb_entry->default_relogin_timeout =
3960                (le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
3961                 (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
3962                 le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
3963        ddb_entry->default_time2wait =
3964                                le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
3965
3966        /* Update params */
3967        ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
3968        qla4xxx_copy_to_sess_conn_params(conn, sess, fw_ddb_entry);
3969
3970        memcpy(sess->initiatorname, ha->name_string,
3971               min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
3972
3973exit_session_conn_param:
3974        if (fw_ddb_entry)
3975                dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
3976                                  fw_ddb_entry, fw_ddb_entry_dma);
3977}
3978
3979/*
3980 * Timer routines
3981 */
3982
3983static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
3984                                unsigned long interval)
3985{
3986        DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
3987                     __func__, ha->host->host_no));
3988        init_timer(&ha->timer);
3989        ha->timer.expires = jiffies + interval * HZ;
3990        ha->timer.data = (unsigned long)ha;
3991        ha->timer.function = (void (*)(unsigned long))func;
3992        add_timer(&ha->timer);
3993        ha->timer_active = 1;
3994}
3995
3996static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
3997{
3998        del_timer_sync(&ha->timer);
3999        ha->timer_active = 0;
4000}
4001
4002/***
4003 * qla4xxx_mark_device_missing - blocks the session
4004 * @cls_session: Pointer to the session to be blocked
4005 * @ddb_entry: Pointer to device database entry
4006 *
4007 * This routine marks a device missing and close connection.
4008 **/
4009void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
4010{
4011        iscsi_block_session(cls_session);
4012}
4013
4014/**
4015 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
4016 * @ha: Pointer to host adapter structure.
4017 *
4018 * This routine marks a device missing and resets the relogin retry count.
4019 **/
4020void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
4021{
4022        iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
4023}
4024
4025static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
4026                                       struct ddb_entry *ddb_entry,
4027                                       struct scsi_cmnd *cmd)
4028{
4029        struct srb *srb;
4030
4031        srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
4032        if (!srb)
4033                return srb;
4034
4035        kref_init(&srb->srb_ref);
4036        srb->ha = ha;
4037        srb->ddb = ddb_entry;
4038        srb->cmd = cmd;
4039        srb->flags = 0;
4040        CMD_SP(cmd) = (void *)srb;
4041
4042        return srb;
4043}
4044
4045static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
4046{
4047        struct scsi_cmnd *cmd = srb->cmd;
4048
4049        if (srb->flags & SRB_DMA_VALID) {
4050                scsi_dma_unmap(cmd);
4051                srb->flags &= ~SRB_DMA_VALID;
4052        }
4053        CMD_SP(cmd) = NULL;
4054}
4055
4056void qla4xxx_srb_compl(struct kref *ref)
4057{
4058        struct srb *srb = container_of(ref, struct srb, srb_ref);
4059        struct scsi_cmnd *cmd = srb->cmd;
4060        struct scsi_qla_host *ha = srb->ha;
4061
4062        qla4xxx_srb_free_dma(ha, srb);
4063
4064        mempool_free(srb, ha->srb_mempool);
4065
4066        cmd->scsi_done(cmd);
4067}
4068
4069/**
4070 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
4071 * @host: scsi host
4072 * @cmd: Pointer to Linux's SCSI command structure
4073 *
4074 * Remarks:
4075 * This routine is invoked by Linux to send a SCSI command to the driver.
4076 * The mid-level driver tries to ensure that queuecommand never gets
4077 * invoked concurrently with itself or the interrupt handler (although
4078 * the interrupt handler may call this routine as part of request-
4079 * completion handling).   Unfortunely, it sometimes calls the scheduler
4080 * in interrupt context which is a big NO! NO!.
4081 **/
4082static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
4083{
4084        struct scsi_qla_host *ha = to_qla_host(host);
4085        struct ddb_entry *ddb_entry = cmd->device->hostdata;
4086        struct iscsi_cls_session *sess = ddb_entry->sess;
4087        struct srb *srb;
4088        int rval;
4089
4090        if (test_bit(AF_EEH_BUSY, &ha->flags)) {
4091                if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
4092                        cmd->result = DID_NO_CONNECT << 16;
4093                else
4094                        cmd->result = DID_REQUEUE << 16;
4095                goto qc_fail_command;
4096        }
4097
4098        if (!sess) {
4099                cmd->result = DID_IMM_RETRY << 16;
4100                goto qc_fail_command;
4101        }
4102
4103        rval = iscsi_session_chkready(sess);
4104        if (rval) {
4105                cmd->result = rval;
4106                goto qc_fail_command;
4107        }
4108
4109        if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
4110            test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
4111            test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
4112            test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
4113            test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
4114            !test_bit(AF_ONLINE, &ha->flags) ||
4115            !test_bit(AF_LINK_UP, &ha->flags) ||
4116            test_bit(AF_LOOPBACK, &ha->flags) ||
4117            test_bit(DPC_POST_IDC_ACK, &ha->dpc_flags) ||
4118            test_bit(DPC_RESTORE_ACB, &ha->dpc_flags) ||
4119            test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
4120                goto qc_host_busy;
4121
4122        srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
4123        if (!srb)
4124                goto qc_host_busy;
4125
4126        rval = qla4xxx_send_command_to_isp(ha, srb);
4127        if (rval != QLA_SUCCESS)
4128                goto qc_host_busy_free_sp;
4129
4130        return 0;
4131
4132qc_host_busy_free_sp:
4133        qla4xxx_srb_free_dma(ha, srb);
4134        mempool_free(srb, ha->srb_mempool);
4135
4136qc_host_busy:
4137        return SCSI_MLQUEUE_HOST_BUSY;
4138
4139qc_fail_command:
4140        cmd->scsi_done(cmd);
4141
4142        return 0;
4143}
4144
4145/**
4146 * qla4xxx_mem_free - frees memory allocated to adapter
4147 * @ha: Pointer to host adapter structure.
4148 *
4149 * Frees memory previously allocated by qla4xxx_mem_alloc
4150 **/
4151static void qla4xxx_mem_free(struct scsi_qla_host *ha)
4152{
4153        if (ha->queues)
4154                dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
4155                                  ha->queues_dma);
4156
4157         if (ha->fw_dump)
4158                vfree(ha->fw_dump);
4159
4160        ha->queues_len = 0;
4161        ha->queues = NULL;
4162        ha->queues_dma = 0;
4163        ha->request_ring = NULL;
4164        ha->request_dma = 0;
4165        ha->response_ring = NULL;
4166        ha->response_dma = 0;
4167        ha->shadow_regs = NULL;
4168        ha->shadow_regs_dma = 0;
4169        ha->fw_dump = NULL;
4170        ha->fw_dump_size = 0;
4171
4172        /* Free srb pool. */
4173        if (ha->srb_mempool)
4174                mempool_destroy(ha->srb_mempool);
4175
4176        ha->srb_mempool = NULL;
4177
4178        if (ha->chap_dma_pool)
4179                dma_pool_destroy(ha->chap_dma_pool);
4180
4181        if (ha->chap_list)
4182                vfree(ha->chap_list);
4183        ha->chap_list = NULL;
4184
4185        if (ha->fw_ddb_dma_pool)
4186                dma_pool_destroy(ha->fw_ddb_dma_pool);
4187
4188        /* release io space registers  */
4189        if (is_qla8022(ha)) {
4190                if (ha->nx_pcibase)
4191                        iounmap(
4192                            (struct device_reg_82xx __iomem *)ha->nx_pcibase);
4193        } else if (is_qla8032(ha) || is_qla8042(ha)) {
4194                if (ha->nx_pcibase)
4195                        iounmap(
4196                            (struct device_reg_83xx __iomem *)ha->nx_pcibase);
4197        } else if (ha->reg) {
4198                iounmap(ha->reg);
4199        }
4200
4201        if (ha->reset_tmplt.buff)
4202                vfree(ha->reset_tmplt.buff);
4203
4204        pci_release_regions(ha->pdev);
4205}
4206
4207/**
4208 * qla4xxx_mem_alloc - allocates memory for use by adapter.
4209 * @ha: Pointer to host adapter structure
4210 *
4211 * Allocates DMA memory for request and response queues. Also allocates memory
4212 * for srbs.
4213 **/
4214static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
4215{
4216        unsigned long align;
4217
4218        /* Allocate contiguous block of DMA memory for queues. */
4219        ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
4220                          (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
4221                          sizeof(struct shadow_regs) +
4222                          MEM_ALIGN_VALUE +
4223                          (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
4224        ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
4225                                        &ha->queues_dma, GFP_KERNEL);
4226        if (ha->queues == NULL) {
4227                ql4_printk(KERN_WARNING, ha,
4228                    "Memory Allocation failed - queues.\n");
4229
4230                goto mem_alloc_error_exit;
4231        }
4232        memset(ha->queues, 0, ha->queues_len);
4233
4234        /*
4235         * As per RISC alignment requirements -- the bus-address must be a
4236         * multiple of the request-ring size (in bytes).
4237         */
4238        align = 0;
4239        if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
4240                align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
4241                                           (MEM_ALIGN_VALUE - 1));
4242
4243        /* Update request and response queue pointers. */
4244        ha->request_dma = ha->queues_dma + align;
4245        ha->request_ring = (struct queue_entry *) (ha->queues + align);
4246        ha->response_dma = ha->queues_dma + align +
4247                (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
4248        ha->response_ring = (struct queue_entry *) (ha->queues + align +
4249                                                    (REQUEST_QUEUE_DEPTH *
4250                                                     QUEUE_SIZE));
4251        ha->shadow_regs_dma = ha->queues_dma + align +
4252                (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
4253                (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
4254        ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
4255                                                  (REQUEST_QUEUE_DEPTH *
4256                                                   QUEUE_SIZE) +
4257                                                  (RESPONSE_QUEUE_DEPTH *
4258                                                   QUEUE_SIZE));
4259
4260        /* Allocate memory for srb pool. */
4261        ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
4262                                         mempool_free_slab, srb_cachep);
4263        if (ha->srb_mempool == NULL) {
4264                ql4_printk(KERN_WARNING, ha,
4265                    "Memory Allocation failed - SRB Pool.\n");
4266
4267                goto mem_alloc_error_exit;
4268        }
4269
4270        ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
4271                                            CHAP_DMA_BLOCK_SIZE, 8, 0);
4272
4273        if (ha->chap_dma_pool == NULL) {
4274                ql4_printk(KERN_WARNING, ha,
4275                    "%s: chap_dma_pool allocation failed..\n", __func__);
4276                goto mem_alloc_error_exit;
4277        }
4278
4279        ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
4280                                              DDB_DMA_BLOCK_SIZE, 8, 0);
4281
4282        if (ha->fw_ddb_dma_pool == NULL) {
4283                ql4_printk(KERN_WARNING, ha,
4284                           "%s: fw_ddb_dma_pool allocation failed..\n",
4285                           __func__);
4286                goto mem_alloc_error_exit;
4287        }
4288
4289        return QLA_SUCCESS;
4290
4291mem_alloc_error_exit:
4292        qla4xxx_mem_free(ha);
4293        return QLA_ERROR;
4294}
4295
4296/**
4297 * qla4_8xxx_check_temp - Check the ISP82XX temperature.
4298 * @ha: adapter block pointer.
4299 *
4300 * Note: The caller should not hold the idc lock.
4301 **/
4302static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
4303{
4304        uint32_t temp, temp_state, temp_val;
4305        int status = QLA_SUCCESS;
4306
4307        temp = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_TEMP_STATE);
4308
4309        temp_state = qla82xx_get_temp_state(temp);
4310        temp_val = qla82xx_get_temp_val(temp);
4311
4312        if (temp_state == QLA82XX_TEMP_PANIC) {
4313                ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
4314                           " exceeds maximum allowed. Hardware has been shut"
4315                           " down.\n", temp_val);
4316                status = QLA_ERROR;
4317        } else if (temp_state == QLA82XX_TEMP_WARN) {
4318                if (ha->temperature == QLA82XX_TEMP_NORMAL)
4319                        ql4_printk(KERN_WARNING, ha, "Device temperature %d"
4320                                   " degrees C exceeds operating range."
4321                                   " Immediate action needed.\n", temp_val);
4322        } else {
4323                if (ha->temperature == QLA82XX_TEMP_WARN)
4324                        ql4_printk(KERN_INFO, ha, "Device temperature is"
4325                                   " now %d degrees C in normal range.\n",
4326                                   temp_val);
4327        }
4328        ha->temperature = temp_state;
4329        return status;
4330}
4331
4332/**
4333 * qla4_8xxx_check_fw_alive  - Check firmware health
4334 * @ha: Pointer to host adapter structure.
4335 *
4336 * Context: Interrupt
4337 **/
4338static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
4339{
4340        uint32_t fw_heartbeat_counter;
4341        int status = QLA_SUCCESS;
4342
4343        fw_heartbeat_counter = qla4_8xxx_rd_direct(ha,
4344                                                   QLA8XXX_PEG_ALIVE_COUNTER);
4345        /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
4346        if (fw_heartbeat_counter == 0xffffffff) {
4347                DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
4348                    "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
4349                    ha->host_no, __func__));
4350                return status;
4351        }
4352
4353        if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
4354                ha->seconds_since_last_heartbeat++;
4355                /* FW not alive after 2 seconds */
4356                if (ha->seconds_since_last_heartbeat == 2) {
4357                        ha->seconds_since_last_heartbeat = 0;
4358                        qla4_8xxx_dump_peg_reg(ha);
4359                        status = QLA_ERROR;
4360                }
4361        } else
4362                ha->seconds_since_last_heartbeat = 0;
4363
4364        ha->fw_heartbeat_counter = fw_heartbeat_counter;
4365        return status;
4366}
4367
4368static void qla4_8xxx_process_fw_error(struct scsi_qla_host *ha)
4369{
4370        uint32_t halt_status;
4371        int halt_status_unrecoverable = 0;
4372
4373        halt_status = qla4_8xxx_rd_direct(ha, QLA8XXX_PEG_HALT_STATUS1);
4374
4375        if (is_qla8022(ha)) {
4376                ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
4377                           __func__);
4378                qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
4379                                CRB_NIU_XG_PAUSE_CTL_P0 |
4380                                CRB_NIU_XG_PAUSE_CTL_P1);
4381
4382                if (QLA82XX_FWERROR_CODE(halt_status) == 0x67)
4383                        ql4_printk(KERN_ERR, ha, "%s: Firmware aborted with error code 0x00006700. Device is being reset\n",
4384                                   __func__);
4385                if (halt_status & HALT_STATUS_UNRECOVERABLE)
4386                        halt_status_unrecoverable = 1;
4387        } else if (is_qla8032(ha) || is_qla8042(ha)) {
4388                if (halt_status & QLA83XX_HALT_STATUS_FW_RESET)
4389                        ql4_printk(KERN_ERR, ha, "%s: Firmware error detected device is being reset\n",
4390                                   __func__);
4391                else if (halt_status & QLA83XX_HALT_STATUS_UNRECOVERABLE)
4392                        halt_status_unrecoverable = 1;
4393        }
4394
4395        /*
4396         * Since we cannot change dev_state in interrupt context,
4397         * set appropriate DPC flag then wakeup DPC
4398         */
4399        if (halt_status_unrecoverable) {
4400                set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
4401        } else {
4402                ql4_printk(KERN_INFO, ha, "%s: detect abort needed!\n",
4403                           __func__);
4404                set_bit(DPC_RESET_HA, &ha->dpc_flags);
4405        }
4406        qla4xxx_mailbox_premature_completion(ha);
4407        qla4xxx_wake_dpc(ha);
4408}
4409
4410/**
4411 * qla4_8xxx_watchdog - Poll dev state
4412 * @ha: Pointer to host adapter structure.
4413 *
4414 * Context: Interrupt
4415 **/
4416void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
4417{
4418        uint32_t dev_state;
4419        uint32_t idc_ctrl;
4420
4421        if (is_qla8032(ha) &&
4422            (qla4_83xx_is_detached(ha) == QLA_SUCCESS))
4423                WARN_ONCE(1, "%s: iSCSI function %d marked invisible\n",
4424                          __func__, ha->func_num);
4425
4426        /* don't poll if reset is going on */
4427        if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
4428            test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
4429            test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
4430                dev_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE);
4431
4432                if (qla4_8xxx_check_temp(ha)) {
4433                        if (is_qla8022(ha)) {
4434                                ql4_printk(KERN_INFO, ha, "disabling pause transmit on port 0 & 1.\n");
4435                                qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
4436                                                CRB_NIU_XG_PAUSE_CTL_P0 |
4437                                                CRB_NIU_XG_PAUSE_CTL_P1);
4438                        }
4439                        set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
4440                        qla4xxx_wake_dpc(ha);
4441                } else if (dev_state == QLA8XXX_DEV_NEED_RESET &&
4442                           !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
4443
4444                        ql4_printk(KERN_INFO, ha, "%s: HW State: NEED RESET!\n",
4445                                   __func__);
4446
4447                        if (is_qla8032(ha) || is_qla8042(ha)) {
4448                                idc_ctrl = qla4_83xx_rd_reg(ha,
4449                                                        QLA83XX_IDC_DRV_CTRL);
4450                                if (!(idc_ctrl & GRACEFUL_RESET_BIT1)) {
4451                                        ql4_printk(KERN_INFO, ha, "%s: Graceful reset bit is not set\n",
4452                                                   __func__);
4453                                        qla4xxx_mailbox_premature_completion(
4454                                                                            ha);
4455                                }
4456                        }
4457
4458                        if ((is_qla8032(ha) || is_qla8042(ha)) ||
4459                            (is_qla8022(ha) && !ql4xdontresethba)) {
4460                                set_bit(DPC_RESET_HA, &ha->dpc_flags);
4461                                qla4xxx_wake_dpc(ha);
4462                        }
4463                } else if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT &&
4464                    !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
4465                        ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
4466                            __func__);
4467                        set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
4468                        qla4xxx_wake_dpc(ha);
4469                } else  {
4470                        /* Check firmware health */
4471                        if (qla4_8xxx_check_fw_alive(ha))
4472                                qla4_8xxx_process_fw_error(ha);
4473                }
4474        }
4475}
4476
4477static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
4478{
4479        struct iscsi_session *sess;
4480        struct ddb_entry *ddb_entry;
4481        struct scsi_qla_host *ha;
4482
4483        sess = cls_sess->dd_data;
4484        ddb_entry = sess->dd_data;
4485        ha = ddb_entry->ha;
4486
4487        if (!(ddb_entry->ddb_type == FLASH_DDB))
4488                return;
4489
4490        if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
4491            !iscsi_is_session_online(cls_sess)) {
4492                if (atomic_read(&ddb_entry->retry_relogin_timer) !=
4493                    INVALID_ENTRY) {
4494                        if (atomic_read(&ddb_entry->retry_relogin_timer) ==
4495                                        0) {
4496                                atomic_set(&ddb_entry->retry_relogin_timer,
4497                                           INVALID_ENTRY);
4498                                set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4499                                set_bit(DF_RELOGIN, &ddb_entry->flags);
4500                                DEBUG2(ql4_printk(KERN_INFO, ha,
4501                                       "%s: index [%d] login device\n",
4502                                        __func__, ddb_entry->fw_ddb_index));
4503                        } else
4504                                atomic_dec(&ddb_entry->retry_relogin_timer);
4505                }
4506        }
4507
4508        /* Wait for relogin to timeout */
4509        if (atomic_read(&ddb_entry->relogin_timer) &&
4510            (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
4511                /*
4512                 * If the relogin times out and the device is
4513                 * still NOT ONLINE then try and relogin again.
4514                 */
4515                if (!iscsi_is_session_online(cls_sess)) {
4516                        /* Reset retry relogin timer */
4517                        atomic_inc(&ddb_entry->relogin_retry_count);
4518                        DEBUG2(ql4_printk(KERN_INFO, ha,
4519                                "%s: index[%d] relogin timed out-retrying"
4520                                " relogin (%d), retry (%d)\n", __func__,
4521                                ddb_entry->fw_ddb_index,
4522                                atomic_read(&ddb_entry->relogin_retry_count),
4523                                ddb_entry->default_time2wait + 4));
4524                        set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4525                        atomic_set(&ddb_entry->retry_relogin_timer,
4526                                   ddb_entry->default_time2wait + 4);
4527                }
4528        }
4529}
4530
4531/**
4532 * qla4xxx_timer - checks every second for work to do.
4533 * @ha: Pointer to host adapter structure.
4534 **/
4535static void qla4xxx_timer(struct scsi_qla_host *ha)
4536{
4537        int start_dpc = 0;
4538        uint16_t w;
4539
4540        iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
4541
4542        /* If we are in the middle of AER/EEH processing
4543         * skip any processing and reschedule the timer
4544         */
4545        if (test_bit(AF_EEH_BUSY, &ha->flags)) {
4546                mod_timer(&ha->timer, jiffies + HZ);
4547                return;
4548        }
4549
4550        /* Hardware read to trigger an EEH error during mailbox waits. */
4551        if (!pci_channel_offline(ha->pdev))
4552                pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
4553
4554        if (is_qla80XX(ha))
4555                qla4_8xxx_watchdog(ha);
4556
4557        if (is_qla40XX(ha)) {
4558                /* Check for heartbeat interval. */
4559                if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
4560                    ha->heartbeat_interval != 0) {
4561                        ha->seconds_since_last_heartbeat++;
4562                        if (ha->seconds_since_last_heartbeat >
4563                            ha->heartbeat_interval + 2)
4564                                set_bit(DPC_RESET_HA, &ha->dpc_flags);
4565                }
4566        }
4567
4568        /* Process any deferred work. */
4569        if (!list_empty(&ha->work_list))
4570                start_dpc++;
4571
4572        /* Wakeup the dpc routine for this adapter, if needed. */
4573        if (start_dpc ||
4574             test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
4575             test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
4576             test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
4577             test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
4578             test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
4579             test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
4580             test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
4581             test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
4582             test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
4583             test_bit(DPC_SYSFS_DDB_EXPORT, &ha->dpc_flags) ||
4584             test_bit(DPC_AEN, &ha->dpc_flags)) {
4585                DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
4586                              " - dpc flags = 0x%lx\n",
4587                              ha->host_no, __func__, ha->dpc_flags));
4588                qla4xxx_wake_dpc(ha);
4589        }
4590
4591        /* Reschedule timer thread to call us back in one second */
4592        mod_timer(&ha->timer, jiffies + HZ);
4593
4594        DEBUG2(ha->seconds_since_last_intr++);
4595}
4596
4597/**
4598 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
4599 * @ha: Pointer to host adapter structure.
4600 *
4601 * This routine stalls the driver until all outstanding commands are returned.
4602 * Caller must release the Hardware Lock prior to calling this routine.
4603 **/
4604static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
4605{
4606        uint32_t index = 0;
4607        unsigned long flags;
4608        struct scsi_cmnd *cmd;
4609        unsigned long wtime;
4610        uint32_t wtmo;
4611
4612        if (is_qla40XX(ha))
4613                wtmo = WAIT_CMD_TOV;
4614        else
4615                wtmo = ha->nx_reset_timeout / 2;
4616
4617        wtime = jiffies + (wtmo * HZ);
4618
4619        DEBUG2(ql4_printk(KERN_INFO, ha,
4620                          "Wait up to %u seconds for cmds to complete\n",
4621                          wtmo));
4622
4623        while (!time_after_eq(jiffies, wtime)) {
4624                spin_lock_irqsave(&ha->hardware_lock, flags);
4625                /* Find a command that hasn't completed. */
4626                for (index = 0; index < ha->host->can_queue; index++) {
4627                        cmd = scsi_host_find_tag(ha->host, index);
4628                        /*
4629                         * We cannot just check if the index is valid,
4630                         * becase if we are run from the scsi eh, then
4631                         * the scsi/block layer is going to prevent
4632                         * the tag from being released.
4633                         */
4634                        if (cmd != NULL && CMD_SP(cmd))
4635                                break;
4636                }
4637                spin_unlock_irqrestore(&ha->hardware_lock, flags);
4638
4639                /* If No Commands are pending, wait is complete */
4640                if (index == ha->host->can_queue)
4641                        return QLA_SUCCESS;
4642
4643                msleep(1000);
4644        }
4645        /* If we timed out on waiting for commands to come back
4646         * return ERROR. */
4647        return QLA_ERROR;
4648}
4649
4650int qla4xxx_hw_reset(struct scsi_qla_host *ha)
4651{
4652        uint32_t ctrl_status;
4653        unsigned long flags = 0;
4654
4655        DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
4656
4657        if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
4658                return QLA_ERROR;
4659
4660        spin_lock_irqsave(&ha->hardware_lock, flags);
4661
4662        /*
4663         * If the SCSI Reset Interrupt bit is set, clear it.
4664         * Otherwise, the Soft Reset won't work.
4665         */
4666        ctrl_status = readw(&ha->reg->ctrl_status);
4667        if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
4668                writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
4669
4670        /* Issue Soft Reset */
4671        writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
4672        readl(&ha->reg->ctrl_status);
4673
4674        spin_unlock_irqrestore(&ha->hardware_lock, flags);
4675        return QLA_SUCCESS;
4676}
4677
4678/**
4679 * qla4xxx_soft_reset - performs soft reset.
4680 * @ha: Pointer to host adapter structure.
4681 **/
4682int qla4xxx_soft_reset(struct scsi_qla_host *ha)
4683{
4684        uint32_t max_wait_time;
4685        unsigned long flags = 0;
4686        int status;
4687        uint32_t ctrl_status;
4688
4689        status = qla4xxx_hw_reset(ha);
4690        if (status != QLA_SUCCESS)
4691                return status;
4692
4693        status = QLA_ERROR;
4694        /* Wait until the Network Reset Intr bit is cleared */
4695        max_wait_time = RESET_INTR_TOV;
4696        do {
4697                spin_lock_irqsave(&ha->hardware_lock, flags);
4698                ctrl_status = readw(&ha->reg->ctrl_status);
4699                spin_unlock_irqrestore(&ha->hardware_lock, flags);
4700
4701                if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
4702                        break;
4703
4704                msleep(1000);
4705        } while ((--max_wait_time));
4706
4707        if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
4708                DEBUG2(printk(KERN_WARNING
4709                              "scsi%ld: Network Reset Intr not cleared by "
4710                              "Network function, clearing it now!\n",
4711                              ha->host_no));
4712                spin_lock_irqsave(&ha->hardware_lock, flags);
4713                writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
4714                readl(&ha->reg->ctrl_status);
4715                spin_unlock_irqrestore(&ha->hardware_lock, flags);
4716        }
4717
4718        /* Wait until the firmware tells us the Soft Reset is done */
4719        max_wait_time = SOFT_RESET_TOV;
4720        do {
4721                spin_lock_irqsave(&ha->hardware_lock, flags);
4722                ctrl_status = readw(&ha->reg->ctrl_status);
4723                spin_unlock_irqrestore(&ha->hardware_lock, flags);
4724
4725                if ((ctrl_status & CSR_SOFT_RESET) == 0) {
4726                        status = QLA_SUCCESS;
4727                        break;
4728                }
4729
4730                msleep(1000);
4731        } while ((--max_wait_time));
4732
4733        /*
4734         * Also, make sure that the SCSI Reset Interrupt bit has been cleared
4735         * after the soft reset has taken place.
4736         */
4737        spin_lock_irqsave(&ha->hardware_lock, flags);
4738        ctrl_status = readw(&ha->reg->ctrl_status);
4739        if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
4740                writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
4741                readl(&ha->reg->ctrl_status);
4742        }
4743        spin_unlock_irqrestore(&ha->hardware_lock, flags);
4744
4745        /* If soft reset fails then most probably the bios on other
4746         * function is also enabled.
4747         * Since the initialization is sequential the other fn
4748         * wont be able to acknowledge the soft reset.
4749         * Issue a force soft reset to workaround this scenario.
4750         */
4751        if (max_wait_time == 0) {
4752                /* Issue Force Soft Reset */
4753                spin_lock_irqsave(&ha->hardware_lock, flags);
4754                writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
4755                readl(&ha->reg->ctrl_status);
4756                spin_unlock_irqrestore(&ha->hardware_lock, flags);
4757                /* Wait until the firmware tells us the Soft Reset is done */
4758                max_wait_time = SOFT_RESET_TOV;
4759                do {
4760                        spin_lock_irqsave(&ha->hardware_lock, flags);
4761                        ctrl_status = readw(&ha->reg->ctrl_status);
4762                        spin_unlock_irqrestore(&ha->hardware_lock, flags);
4763
4764                        if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
4765                                status = QLA_SUCCESS;
4766                                break;
4767                        }
4768
4769                        msleep(1000);
4770                } while ((--max_wait_time));
4771        }
4772
4773        return status;
4774}
4775
4776/**
4777 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
4778 * @ha: Pointer to host adapter structure.
4779 * @res: returned scsi status
4780 *
4781 * This routine is called just prior to a HARD RESET to return all
4782 * outstanding commands back to the Operating System.
4783 * Caller should make sure that the following locks are released
4784 * before this calling routine: Hardware lock, and io_request_lock.
4785 **/
4786static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
4787{
4788        struct srb *srb;
4789        int i;
4790        unsigned long flags;
4791
4792        spin_lock_irqsave(&ha->hardware_lock, flags);
4793        for (i = 0; i < ha->host->can_queue; i++) {
4794                srb = qla4xxx_del_from_active_array(ha, i);
4795                if (srb != NULL) {
4796                        srb->cmd->result = res;
4797                        kref_put(&srb->srb_ref, qla4xxx_srb_compl);
4798                }
4799        }
4800        spin_unlock_irqrestore(&ha->hardware_lock, flags);
4801}
4802
4803void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
4804{
4805        clear_bit(AF_ONLINE, &ha->flags);
4806
4807        /* Disable the board */
4808        ql4_printk(KERN_INFO, ha, "Disabling the board\n");
4809
4810        qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
4811        qla4xxx_mark_all_devices_missing(ha);
4812        clear_bit(AF_INIT_DONE, &ha->flags);
4813}
4814
4815static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
4816{
4817        struct iscsi_session *sess;
4818        struct ddb_entry *ddb_entry;
4819
4820        sess = cls_session->dd_data;
4821        ddb_entry = sess->dd_data;
4822        ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
4823
4824        if (ddb_entry->ddb_type == FLASH_DDB)
4825                iscsi_block_session(ddb_entry->sess);
4826        else
4827                iscsi_session_failure(cls_session->dd_data,
4828                                      ISCSI_ERR_CONN_FAILED);
4829}
4830
4831/**
4832 * qla4xxx_recover_adapter - recovers adapter after a fatal error
4833 * @ha: Pointer to host adapter structure.
4834 **/
4835static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
4836{
4837        int status = QLA_ERROR;
4838        uint8_t reset_chip = 0;
4839        uint32_t dev_state;
4840        unsigned long wait;
4841
4842        /* Stall incoming I/O until we are done */
4843        scsi_block_requests(ha->host);
4844        clear_bit(AF_ONLINE, &ha->flags);
4845        clear_bit(AF_LINK_UP, &ha->flags);
4846
4847        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
4848
4849        set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
4850
4851        if ((is_qla8032(ha) || is_qla8042(ha)) &&
4852            !test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
4853                ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
4854                           __func__);
4855                /* disable pause frame for ISP83xx */
4856                qla4_83xx_disable_pause(ha);
4857        }
4858
4859        iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
4860
4861        if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
4862                reset_chip = 1;
4863
4864        /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
4865         * do not reset adapter, jump to initialize_adapter */
4866        if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
4867                status = QLA_SUCCESS;
4868                goto recover_ha_init_adapter;
4869        }
4870
4871        /* For the ISP-8xxx adapter, issue a stop_firmware if invoked
4872         * from eh_host_reset or ioctl module */
4873        if (is_qla80XX(ha) && !reset_chip &&
4874            test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
4875
4876                DEBUG2(ql4_printk(KERN_INFO, ha,
4877                    "scsi%ld: %s - Performing stop_firmware...\n",
4878                    ha->host_no, __func__));
4879                status = ha->isp_ops->reset_firmware(ha);
4880                if (status == QLA_SUCCESS) {
4881                        if (!test_bit(AF_FW_RECOVERY, &ha->flags))
4882                                qla4xxx_cmd_wait(ha);
4883
4884                        ha->isp_ops->disable_intrs(ha);
4885                        qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4886                        qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
4887                } else {
4888                        /* If the stop_firmware fails then
4889                         * reset the entire chip */
4890                        reset_chip = 1;
4891                        clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
4892                        set_bit(DPC_RESET_HA, &ha->dpc_flags);
4893                }
4894        }
4895
4896        /* Issue full chip reset if recovering from a catastrophic error,
4897         * or if stop_firmware fails for ISP-8xxx.
4898         * This is the default case for ISP-4xxx */
4899        if (is_qla40XX(ha) || reset_chip) {
4900                if (is_qla40XX(ha))
4901                        goto chip_reset;
4902
4903                /* Check if 8XXX firmware is alive or not
4904                 * We may have arrived here from NEED_RESET
4905                 * detection only */
4906                if (test_bit(AF_FW_RECOVERY, &ha->flags))
4907                        goto chip_reset;
4908
4909                wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
4910                while (time_before(jiffies, wait)) {
4911                        if (qla4_8xxx_check_fw_alive(ha)) {
4912                                qla4xxx_mailbox_premature_completion(ha);
4913                                break;
4914                        }
4915
4916                        set_current_state(TASK_UNINTERRUPTIBLE);
4917                        schedule_timeout(HZ);
4918                }
4919chip_reset:
4920                if (!test_bit(AF_FW_RECOVERY, &ha->flags))
4921                        qla4xxx_cmd_wait(ha);
4922
4923                qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4924                DEBUG2(ql4_printk(KERN_INFO, ha,
4925                    "scsi%ld: %s - Performing chip reset..\n",
4926                    ha->host_no, __func__));
4927                status = ha->isp_ops->reset_chip(ha);
4928                qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
4929        }
4930
4931        /* Flush any pending ddb changed AENs */
4932        qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
4933
4934recover_ha_init_adapter:
4935        /* Upon successful firmware/chip reset, re-initialize the adapter */
4936        if (status == QLA_SUCCESS) {
4937                /* For ISP-4xxx, force function 1 to always initialize
4938                 * before function 3 to prevent both funcions from
4939                 * stepping on top of the other */
4940                if (is_qla40XX(ha) && (ha->mac_index == 3))
4941                        ssleep(6);
4942
4943                /* NOTE: AF_ONLINE flag set upon successful completion of
4944                 * qla4xxx_initialize_adapter */
4945                status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
4946                if (is_qla80XX(ha) && (status == QLA_ERROR)) {
4947                        status = qla4_8xxx_check_init_adapter_retry(ha);
4948                        if (status == QLA_ERROR) {
4949                                ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Don't retry recover adapter\n",
4950                                           ha->host_no, __func__);
4951                                qla4xxx_dead_adapter_cleanup(ha);
4952                                clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4953                                clear_bit(DPC_RESET_HA, &ha->dpc_flags);
4954                                clear_bit(DPC_RESET_HA_FW_CONTEXT,
4955                                          &ha->dpc_flags);
4956                                goto exit_recover;
4957                        }
4958                }
4959        }
4960
4961        /* Retry failed adapter initialization, if necessary
4962         * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
4963         * case to prevent ping-pong resets between functions */
4964        if (!test_bit(AF_ONLINE, &ha->flags) &&
4965            !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
4966                /* Adapter initialization failed, see if we can retry
4967                 * resetting the ha.
4968                 * Since we don't want to block the DPC for too long
4969                 * with multiple resets in the same thread,
4970                 * utilize DPC to retry */
4971                if (is_qla80XX(ha)) {
4972                        ha->isp_ops->idc_lock(ha);
4973                        dev_state = qla4_8xxx_rd_direct(ha,
4974                                                        QLA8XXX_CRB_DEV_STATE);
4975                        ha->isp_ops->idc_unlock(ha);
4976                        if (dev_state == QLA8XXX_DEV_FAILED) {
4977                                ql4_printk(KERN_INFO, ha, "%s: don't retry "
4978                                           "recover adapter. H/W is in Failed "
4979                                           "state\n", __func__);
4980                                qla4xxx_dead_adapter_cleanup(ha);
4981                                clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4982                                clear_bit(DPC_RESET_HA, &ha->dpc_flags);
4983                                clear_bit(DPC_RESET_HA_FW_CONTEXT,
4984                                                &ha->dpc_flags);
4985                                status = QLA_ERROR;
4986
4987                                goto exit_recover;
4988                        }
4989                }
4990
4991                if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
4992                        ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
4993                        DEBUG2(printk("scsi%ld: recover adapter - retrying "
4994                                      "(%d) more times\n", ha->host_no,
4995                                      ha->retry_reset_ha_cnt));
4996                        set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
4997                        status = QLA_ERROR;
4998                } else {
4999                        if (ha->retry_reset_ha_cnt > 0) {
5000                                /* Schedule another Reset HA--DPC will retry */
5001                                ha->retry_reset_ha_cnt--;
5002                                DEBUG2(printk("scsi%ld: recover adapter - "
5003                                              "retry remaining %d\n",
5004                                              ha->host_no,
5005                                              ha->retry_reset_ha_cnt));
5006                                status = QLA_ERROR;
5007                        }
5008
5009                        if (ha->retry_reset_ha_cnt == 0) {
5010                                /* Recover adapter retries have been exhausted.
5011                                 * Adapter DEAD */
5012                                DEBUG2(printk("scsi%ld: recover adapter "
5013                                              "failed - board disabled\n",
5014                                              ha->host_no));
5015                                qla4xxx_dead_adapter_cleanup(ha);
5016                                clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
5017                                clear_bit(DPC_RESET_HA, &ha->dpc_flags);
5018                                clear_bit(DPC_RESET_HA_FW_CONTEXT,
5019                                          &ha->dpc_flags);
5020                                status = QLA_ERROR;
5021                        }
5022                }
5023        } else {
5024                clear_bit(DPC_RESET_HA, &ha->dpc_flags);
5025                clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5026                clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
5027        }
5028
5029exit_recover:
5030        ha->adapter_error_count++;
5031
5032        if (test_bit(AF_ONLINE, &ha->flags))
5033                ha->isp_ops->enable_intrs(ha);
5034
5035        scsi_unblock_requests(ha->host);
5036
5037        clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
5038        DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
5039            status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
5040
5041        return status;
5042}
5043
5044static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
5045{
5046        struct iscsi_session *sess;
5047        struct ddb_entry *ddb_entry;
5048        struct scsi_qla_host *ha;
5049
5050        sess = cls_session->dd_data;
5051        ddb_entry = sess->dd_data;
5052        ha = ddb_entry->ha;
5053        if (!iscsi_is_session_online(cls_session)) {
5054                if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
5055                        ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5056                                   " unblock session\n", ha->host_no, __func__,
5057                                   ddb_entry->fw_ddb_index);
5058                        iscsi_unblock_session(ddb_entry->sess);
5059                } else {
5060                        /* Trigger relogin */
5061                        if (ddb_entry->ddb_type == FLASH_DDB) {
5062                                if (!(test_bit(DF_RELOGIN, &ddb_entry->flags) ||
5063                                      test_bit(DF_DISABLE_RELOGIN,
5064                                               &ddb_entry->flags)))
5065                                        qla4xxx_arm_relogin_timer(ddb_entry);
5066                        } else
5067                                iscsi_session_failure(cls_session->dd_data,
5068                                                      ISCSI_ERR_CONN_FAILED);
5069                }
5070        }
5071}
5072
5073int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
5074{
5075        struct iscsi_session *sess;
5076        struct ddb_entry *ddb_entry;
5077        struct scsi_qla_host *ha;
5078
5079        sess = cls_session->dd_data;
5080        ddb_entry = sess->dd_data;
5081        ha = ddb_entry->ha;
5082        ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5083                   " unblock session\n", ha->host_no, __func__,
5084                   ddb_entry->fw_ddb_index);
5085
5086        iscsi_unblock_session(ddb_entry->sess);
5087
5088        /* Start scan target */
5089        if (test_bit(AF_ONLINE, &ha->flags)) {
5090                ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5091                           " start scan\n", ha->host_no, __func__,
5092                           ddb_entry->fw_ddb_index);
5093                scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
5094        }
5095        return QLA_SUCCESS;
5096}
5097
5098int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
5099{
5100        struct iscsi_session *sess;
5101        struct ddb_entry *ddb_entry;
5102        struct scsi_qla_host *ha;
5103        int status = QLA_SUCCESS;
5104
5105        sess = cls_session->dd_data;
5106        ddb_entry = sess->dd_data;
5107        ha = ddb_entry->ha;
5108        ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
5109                   " unblock user space session\n", ha->host_no, __func__,
5110                   ddb_entry->fw_ddb_index);
5111
5112        if (!iscsi_is_session_online(cls_session)) {
5113                iscsi_conn_start(ddb_entry->conn);
5114                iscsi_conn_login_event(ddb_entry->conn,
5115                                       ISCSI_CONN_STATE_LOGGED_IN);
5116        } else {
5117                ql4_printk(KERN_INFO, ha,
5118                           "scsi%ld: %s: ddb[%d] session [%d] already logged in\n",
5119                           ha->host_no, __func__, ddb_entry->fw_ddb_index,
5120                           cls_session->sid);
5121                status = QLA_ERROR;
5122        }
5123
5124        return status;
5125}
5126
5127static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
5128{
5129        iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
5130}
5131
5132static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
5133{
5134        uint16_t relogin_timer;
5135        struct iscsi_session *sess;
5136        struct ddb_entry *ddb_entry;
5137        struct scsi_qla_host *ha;
5138
5139        sess = cls_sess->dd_data;
5140        ddb_entry = sess->dd_data;
5141        ha = ddb_entry->ha;
5142
5143        relogin_timer = max(ddb_entry->default_relogin_timeout,
5144                            (uint16_t)RELOGIN_TOV);
5145        atomic_set(&ddb_entry->relogin_timer, relogin_timer);
5146
5147        DEBUG2(ql4_printk(KERN_INFO, ha,
5148                          "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
5149                          ddb_entry->fw_ddb_index, relogin_timer));
5150
5151        qla4xxx_login_flash_ddb(cls_sess);
5152}
5153
5154static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
5155{
5156        struct iscsi_session *sess;
5157        struct ddb_entry *ddb_entry;
5158        struct scsi_qla_host *ha;
5159
5160        sess = cls_sess->dd_data;
5161        ddb_entry = sess->dd_data;
5162        ha = ddb_entry->ha;
5163
5164        if (!(ddb_entry->ddb_type == FLASH_DDB))
5165                return;
5166
5167        if (test_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags))
5168                return;
5169
5170        if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
5171            !iscsi_is_session_online(cls_sess)) {
5172                DEBUG2(ql4_printk(KERN_INFO, ha,
5173                                  "relogin issued\n"));
5174                qla4xxx_relogin_flash_ddb(cls_sess);
5175        }
5176}
5177
5178void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
5179{
5180        if (ha->dpc_thread)
5181                queue_work(ha->dpc_thread, &ha->dpc_work);
5182}
5183
5184static struct qla4_work_evt *
5185qla4xxx_alloc_work(struct scsi_qla_host *ha, uint32_t data_size,
5186                   enum qla4_work_type type)
5187{
5188        struct qla4_work_evt *e;
5189        uint32_t size = sizeof(struct qla4_work_evt) + data_size;
5190
5191        e = kzalloc(size, GFP_ATOMIC);
5192        if (!e)
5193                return NULL;
5194
5195        INIT_LIST_HEAD(&e->list);
5196        e->type = type;
5197        return e;
5198}
5199
5200static void qla4xxx_post_work(struct scsi_qla_host *ha,
5201                             struct qla4_work_evt *e)
5202{
5203        unsigned long flags;
5204
5205        spin_lock_irqsave(&ha->work_lock, flags);
5206        list_add_tail(&e->list, &ha->work_list);
5207        spin_unlock_irqrestore(&ha->work_lock, flags);
5208        qla4xxx_wake_dpc(ha);
5209}
5210
5211int qla4xxx_post_aen_work(struct scsi_qla_host *ha,
5212                          enum iscsi_host_event_code aen_code,
5213                          uint32_t data_size, uint8_t *data)
5214{
5215        struct qla4_work_evt *e;
5216
5217        e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_AEN);
5218        if (!e)
5219                return QLA_ERROR;
5220
5221        e->u.aen.code = aen_code;
5222        e->u.aen.data_size = data_size;
5223        memcpy(e->u.aen.data, data, data_size);
5224
5225        qla4xxx_post_work(ha, e);
5226
5227        return QLA_SUCCESS;
5228}
5229
5230int qla4xxx_post_ping_evt_work(struct scsi_qla_host *ha,
5231                               uint32_t status, uint32_t pid,
5232                               uint32_t data_size, uint8_t *data)
5233{
5234        struct qla4_work_evt *e;
5235
5236        e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_PING_STATUS);
5237        if (!e)
5238                return QLA_ERROR;
5239
5240        e->u.ping.status = status;
5241        e->u.ping.pid = pid;
5242        e->u.ping.data_size = data_size;
5243        memcpy(e->u.ping.data, data, data_size);
5244
5245        qla4xxx_post_work(ha, e);
5246
5247        return QLA_SUCCESS;
5248}
5249
5250static void qla4xxx_do_work(struct scsi_qla_host *ha)
5251{
5252        struct qla4_work_evt *e, *tmp;
5253        unsigned long flags;
5254        LIST_HEAD(work);
5255
5256        spin_lock_irqsave(&ha->work_lock, flags);
5257        list_splice_init(&ha->work_list, &work);
5258        spin_unlock_irqrestore(&ha->work_lock, flags);
5259
5260        list_for_each_entry_safe(e, tmp, &work, list) {
5261                list_del_init(&e->list);
5262
5263                switch (e->type) {
5264                case QLA4_EVENT_AEN:
5265                        iscsi_post_host_event(ha->host_no,
5266                                              &qla4xxx_iscsi_transport,
5267                                              e->u.aen.code,
5268                                              e->u.aen.data_size,
5269                                              e->u.aen.data);
5270                        break;
5271                case QLA4_EVENT_PING_STATUS:
5272                        iscsi_ping_comp_event(ha->host_no,
5273                                              &qla4xxx_iscsi_transport,
5274                                              e->u.ping.status,
5275                                              e->u.ping.pid,
5276                                              e->u.ping.data_size,
5277                                              e->u.ping.data);
5278                        break;
5279                default:
5280                        ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
5281                                   "supported", e->type);
5282                }
5283                kfree(e);
5284        }
5285}
5286
5287/**
5288 * qla4xxx_do_dpc - dpc routine
5289 * @data: in our case pointer to adapter structure
5290 *
5291 * This routine is a task that is schedule by the interrupt handler
5292 * to perform the background processing for interrupts.  We put it
5293 * on a task queue that is consumed whenever the scheduler runs; that's
5294 * so you can do anything (i.e. put the process to sleep etc).  In fact,
5295 * the mid-level tries to sleep when it reaches the driver threshold
5296 * "host->can_queue". This can cause a panic if we were in our interrupt code.
5297 **/
5298static void qla4xxx_do_dpc(struct work_struct *work)
5299{
5300        struct scsi_qla_host *ha =
5301                container_of(work, struct scsi_qla_host, dpc_work);
5302        int status = QLA_ERROR;
5303
5304        DEBUG2(ql4_printk(KERN_INFO, ha,
5305                          "scsi%ld: %s: DPC handler waking up. flags = 0x%08lx, dpc_flags = 0x%08lx\n",
5306                          ha->host_no, __func__, ha->flags, ha->dpc_flags));
5307
5308        /* Initialization not yet finished. Don't do anything yet. */
5309        if (!test_bit(AF_INIT_DONE, &ha->flags))
5310                return;
5311
5312        if (test_bit(AF_EEH_BUSY, &ha->flags)) {
5313                DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
5314                    ha->host_no, __func__, ha->flags));
5315                return;
5316        }
5317
5318        /* post events to application */
5319        qla4xxx_do_work(ha);
5320
5321        if (is_qla80XX(ha)) {
5322                if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
5323                        if (is_qla8032(ha) || is_qla8042(ha)) {
5324                                ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
5325                                           __func__);
5326                                /* disable pause frame for ISP83xx */
5327                                qla4_83xx_disable_pause(ha);
5328                        }
5329
5330                        ha->isp_ops->idc_lock(ha);
5331                        qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
5332                                            QLA8XXX_DEV_FAILED);
5333                        ha->isp_ops->idc_unlock(ha);
5334                        ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
5335                        qla4_8xxx_device_state_handler(ha);
5336                }
5337
5338                if (test_bit(DPC_POST_IDC_ACK, &ha->dpc_flags)) {
5339                        if (is_qla8042(ha)) {
5340                                if (ha->idc_info.info2 &
5341                                    ENABLE_INTERNAL_LOOPBACK) {
5342                                        ql4_printk(KERN_INFO, ha, "%s: Disabling ACB\n",
5343                                                   __func__);
5344                                        status = qla4_84xx_config_acb(ha,
5345                                                            ACB_CONFIG_DISABLE);
5346                                        if (status != QLA_SUCCESS) {
5347                                                ql4_printk(KERN_INFO, ha, "%s: ACB config failed\n",
5348                                                           __func__);
5349                                        }
5350                                }
5351                        }
5352                        qla4_83xx_post_idc_ack(ha);
5353                        clear_bit(DPC_POST_IDC_ACK, &ha->dpc_flags);
5354                }
5355
5356                if (is_qla8042(ha) &&
5357                    test_bit(DPC_RESTORE_ACB, &ha->dpc_flags)) {
5358                        ql4_printk(KERN_INFO, ha, "%s: Restoring ACB\n",
5359                                   __func__);
5360                        if (qla4_84xx_config_acb(ha, ACB_CONFIG_SET) !=
5361                            QLA_SUCCESS) {
5362                                ql4_printk(KERN_INFO, ha, "%s: ACB config failed ",
5363                                           __func__);
5364                        }
5365                        clear_bit(DPC_RESTORE_ACB, &ha->dpc_flags);
5366                }
5367
5368                if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
5369                        qla4_8xxx_need_qsnt_handler(ha);
5370                }
5371        }
5372
5373        if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
5374            (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
5375            test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
5376            test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
5377                if ((is_qla8022(ha) && ql4xdontresethba) ||
5378                    ((is_qla8032(ha) || is_qla8042(ha)) &&
5379                     qla4_83xx_idc_dontreset(ha))) {
5380                        DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
5381                            ha->host_no, __func__));
5382                        clear_bit(DPC_RESET_HA, &ha->dpc_flags);
5383                        clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
5384                        clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5385                        goto dpc_post_reset_ha;
5386                }
5387                if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
5388                    test_bit(DPC_RESET_HA, &ha->dpc_flags))
5389                        qla4xxx_recover_adapter(ha);
5390
5391                if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
5392                        uint8_t wait_time = RESET_INTR_TOV;
5393
5394                        while ((readw(&ha->reg->ctrl_status) &
5395                                (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
5396                                if (--wait_time == 0)
5397                                        break;
5398                                msleep(1000);
5399                        }
5400                        if (wait_time == 0)
5401                                DEBUG2(printk("scsi%ld: %s: SR|FSR "
5402                                              "bit not cleared-- resetting\n",
5403                                              ha->host_no, __func__));
5404                        qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
5405                        if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
5406                                qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
5407                                status = qla4xxx_recover_adapter(ha);
5408                        }
5409                        clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
5410                        if (status == QLA_SUCCESS)
5411                                ha->isp_ops->enable_intrs(ha);
5412                }
5413        }
5414
5415dpc_post_reset_ha:
5416        /* ---- process AEN? --- */
5417        if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
5418                qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
5419
5420        /* ---- Get DHCP IP Address? --- */
5421        if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
5422                qla4xxx_get_dhcp_ip_address(ha);
5423
5424        /* ---- relogin device? --- */
5425        if (adapter_up(ha) &&
5426            test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
5427                iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
5428        }
5429
5430        /* ---- link change? --- */
5431        if (!test_bit(AF_LOOPBACK, &ha->flags) &&
5432            test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
5433                if (!test_bit(AF_LINK_UP, &ha->flags)) {
5434                        /* ---- link down? --- */
5435                        qla4xxx_mark_all_devices_missing(ha);
5436                } else {
5437                        /* ---- link up? --- *
5438                         * F/W will auto login to all devices ONLY ONCE after
5439                         * link up during driver initialization and runtime
5440                         * fatal error recovery.  Therefore, the driver must
5441                         * manually relogin to devices when recovering from
5442                         * connection failures, logouts, expired KATO, etc. */
5443                        if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
5444                                qla4xxx_build_ddb_list(ha, ha->is_reset);
5445                                iscsi_host_for_each_session(ha->host,
5446                                                qla4xxx_login_flash_ddb);
5447                        } else
5448                                qla4xxx_relogin_all_devices(ha);
5449                }
5450        }
5451        if (test_and_clear_bit(DPC_SYSFS_DDB_EXPORT, &ha->dpc_flags)) {
5452                if (qla4xxx_sysfs_ddb_export(ha))
5453                        ql4_printk(KERN_ERR, ha, "%s: Error exporting ddb to sysfs\n",
5454                                   __func__);
5455        }
5456}
5457
5458/**
5459 * qla4xxx_free_adapter - release the adapter
5460 * @ha: pointer to adapter structure
5461 **/
5462static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
5463{
5464        qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
5465
5466        /* Turn-off interrupts on the card. */
5467        ha->isp_ops->disable_intrs(ha);
5468
5469        if (is_qla40XX(ha)) {
5470                writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
5471                       &ha->reg->ctrl_status);
5472                readl(&ha->reg->ctrl_status);
5473        } else if (is_qla8022(ha)) {
5474                writel(0, &ha->qla4_82xx_reg->host_int);
5475                readl(&ha->qla4_82xx_reg->host_int);
5476        } else if (is_qla8032(ha) || is_qla8042(ha)) {
5477                writel(0, &ha->qla4_83xx_reg->risc_intr);
5478                readl(&ha->qla4_83xx_reg->risc_intr);
5479        }
5480
5481        /* Remove timer thread, if present */
5482        if (ha->timer_active)
5483                qla4xxx_stop_timer(ha);
5484
5485        /* Kill the kernel thread for this host */
5486        if (ha->dpc_thread)
5487                destroy_workqueue(ha->dpc_thread);
5488
5489        /* Kill the kernel thread for this host */
5490        if (ha->task_wq)
5491                destroy_workqueue(ha->task_wq);
5492
5493        /* Put firmware in known state */
5494        ha->isp_ops->reset_firmware(ha);
5495
5496        if (is_qla80XX(ha)) {
5497                ha->isp_ops->idc_lock(ha);
5498                qla4_8xxx_clear_drv_active(ha);
5499                ha->isp_ops->idc_unlock(ha);
5500        }
5501
5502        /* Detach interrupts */
5503        qla4xxx_free_irqs(ha);
5504
5505        /* free extra memory */
5506        qla4xxx_mem_free(ha);
5507}
5508
5509int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
5510{
5511        int status = 0;
5512        unsigned long mem_base, mem_len, db_base, db_len;
5513        struct pci_dev *pdev = ha->pdev;
5514
5515        status = pci_request_regions(pdev, DRIVER_NAME);
5516        if (status) {
5517                printk(KERN_WARNING
5518                    "scsi(%ld) Failed to reserve PIO regions (%s) "
5519                    "status=%d\n", ha->host_no, pci_name(pdev), status);
5520                goto iospace_error_exit;
5521        }
5522
5523        DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
5524            __func__, pdev->revision));
5525        ha->revision_id = pdev->revision;
5526
5527        /* remap phys address */
5528        mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
5529        mem_len = pci_resource_len(pdev, 0);
5530        DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
5531            __func__, mem_base, mem_len));
5532
5533        /* mapping of pcibase pointer */
5534        ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
5535        if (!ha->nx_pcibase) {
5536                printk(KERN_ERR
5537                    "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
5538                pci_release_regions(ha->pdev);
5539                goto iospace_error_exit;
5540        }
5541
5542        /* Mapping of IO base pointer, door bell read and write pointer */
5543
5544        /* mapping of IO base pointer */
5545        if (is_qla8022(ha)) {
5546                ha->qla4_82xx_reg = (struct device_reg_82xx  __iomem *)
5547                                    ((uint8_t *)ha->nx_pcibase + 0xbc000 +
5548                                     (ha->pdev->devfn << 11));
5549                ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
5550                                    QLA82XX_CAM_RAM_DB2);
5551        } else if (is_qla8032(ha) || is_qla8042(ha)) {
5552                ha->qla4_83xx_reg = (struct device_reg_83xx __iomem *)
5553                                    ((uint8_t *)ha->nx_pcibase);
5554        }
5555
5556        db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
5557        db_len = pci_resource_len(pdev, 4);
5558
5559        return 0;
5560iospace_error_exit:
5561        return -ENOMEM;
5562}
5563
5564/***
5565 * qla4xxx_iospace_config - maps registers
5566 * @ha: pointer to adapter structure
5567 *
5568 * This routines maps HBA's registers from the pci address space
5569 * into the kernel virtual address space for memory mapped i/o.
5570 **/
5571int qla4xxx_iospace_config(struct scsi_qla_host *ha)
5572{
5573        unsigned long pio, pio_len, pio_flags;
5574        unsigned long mmio, mmio_len, mmio_flags;
5575
5576        pio = pci_resource_start(ha->pdev, 0);
5577        pio_len = pci_resource_len(ha->pdev, 0);
5578        pio_flags = pci_resource_flags(ha->pdev, 0);
5579        if (pio_flags & IORESOURCE_IO) {
5580                if (pio_len < MIN_IOBASE_LEN) {
5581                        ql4_printk(KERN_WARNING, ha,
5582                                "Invalid PCI I/O region size\n");
5583                        pio = 0;
5584                }
5585        } else {
5586                ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
5587                pio = 0;
5588        }
5589
5590        /* Use MMIO operations for all accesses. */
5591        mmio = pci_resource_start(ha->pdev, 1);
5592        mmio_len = pci_resource_len(ha->pdev, 1);
5593        mmio_flags = pci_resource_flags(ha->pdev, 1);
5594
5595        if (!(mmio_flags & IORESOURCE_MEM)) {
5596                ql4_printk(KERN_ERR, ha,
5597                    "region #0 not an MMIO resource, aborting\n");
5598
5599                goto iospace_error_exit;
5600        }
5601
5602        if (mmio_len < MIN_IOBASE_LEN) {
5603                ql4_printk(KERN_ERR, ha,
5604                    "Invalid PCI mem region size, aborting\n");
5605                goto iospace_error_exit;
5606        }
5607
5608        if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
5609                ql4_printk(KERN_WARNING, ha,
5610                    "Failed to reserve PIO/MMIO regions\n");
5611
5612                goto iospace_error_exit;
5613        }
5614
5615        ha->pio_address = pio;
5616        ha->pio_length = pio_len;
5617        ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
5618        if (!ha->reg) {
5619                ql4_printk(KERN_ERR, ha,
5620                    "cannot remap MMIO, aborting\n");
5621
5622                goto iospace_error_exit;
5623        }
5624
5625        return 0;
5626
5627iospace_error_exit:
5628        return -ENOMEM;
5629}
5630
5631static struct isp_operations qla4xxx_isp_ops = {
5632        .iospace_config         = qla4xxx_iospace_config,
5633        .pci_config             = qla4xxx_pci_config,
5634        .disable_intrs          = qla4xxx_disable_intrs,
5635        .enable_intrs           = qla4xxx_enable_intrs,
5636        .start_firmware         = qla4xxx_start_firmware,
5637        .intr_handler           = qla4xxx_intr_handler,
5638        .interrupt_service_routine = qla4xxx_interrupt_service_routine,
5639        .reset_chip             = qla4xxx_soft_reset,
5640        .reset_firmware         = qla4xxx_hw_reset,
5641        .queue_iocb             = qla4xxx_queue_iocb,
5642        .complete_iocb          = qla4xxx_complete_iocb,
5643        .rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
5644        .rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
5645        .get_sys_info           = qla4xxx_get_sys_info,
5646        .queue_mailbox_command  = qla4xxx_queue_mbox_cmd,
5647        .process_mailbox_interrupt = qla4xxx_process_mbox_intr,
5648};
5649
5650static struct isp_operations qla4_82xx_isp_ops = {
5651        .iospace_config         = qla4_8xxx_iospace_config,
5652        .pci_config             = qla4_8xxx_pci_config,
5653        .disable_intrs          = qla4_82xx_disable_intrs,
5654        .enable_intrs           = qla4_82xx_enable_intrs,
5655        .start_firmware         = qla4_8xxx_load_risc,
5656        .restart_firmware       = qla4_82xx_try_start_fw,
5657        .intr_handler           = qla4_82xx_intr_handler,
5658        .interrupt_service_routine = qla4_82xx_interrupt_service_routine,
5659        .need_reset             = qla4_8xxx_need_reset,
5660        .reset_chip             = qla4_82xx_isp_reset,
5661        .reset_firmware         = qla4_8xxx_stop_firmware,
5662        .queue_iocb             = qla4_82xx_queue_iocb,
5663        .complete_iocb          = qla4_82xx_complete_iocb,
5664        .rd_shdw_req_q_out      = qla4_82xx_rd_shdw_req_q_out,
5665        .rd_shdw_rsp_q_in       = qla4_82xx_rd_shdw_rsp_q_in,
5666        .get_sys_info           = qla4_8xxx_get_sys_info,
5667        .rd_reg_direct          = qla4_82xx_rd_32,
5668        .wr_reg_direct          = qla4_82xx_wr_32,
5669        .rd_reg_indirect        = qla4_82xx_md_rd_32,
5670        .wr_reg_indirect        = qla4_82xx_md_wr_32,
5671        .idc_lock               = qla4_82xx_idc_lock,
5672        .idc_unlock             = qla4_82xx_idc_unlock,
5673        .rom_lock_recovery      = qla4_82xx_rom_lock_recovery,
5674        .queue_mailbox_command  = qla4_82xx_queue_mbox_cmd,
5675        .process_mailbox_interrupt = qla4_82xx_process_mbox_intr,
5676};
5677
5678static struct isp_operations qla4_83xx_isp_ops = {
5679        .iospace_config         = qla4_8xxx_iospace_config,
5680        .pci_config             = qla4_8xxx_pci_config,
5681        .disable_intrs          = qla4_83xx_disable_intrs,
5682        .enable_intrs           = qla4_83xx_enable_intrs,
5683        .start_firmware         = qla4_8xxx_load_risc,
5684        .restart_firmware       = qla4_83xx_start_firmware,
5685        .intr_handler           = qla4_83xx_intr_handler,
5686        .interrupt_service_routine = qla4_83xx_interrupt_service_routine,
5687        .need_reset             = qla4_8xxx_need_reset,
5688        .reset_chip             = qla4_83xx_isp_reset,
5689        .reset_firmware         = qla4_8xxx_stop_firmware,
5690        .queue_iocb             = qla4_83xx_queue_iocb,
5691        .complete_iocb          = qla4_83xx_complete_iocb,
5692        .rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
5693        .rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
5694        .get_sys_info           = qla4_8xxx_get_sys_info,
5695        .rd_reg_direct          = qla4_83xx_rd_reg,
5696        .wr_reg_direct          = qla4_83xx_wr_reg,
5697        .rd_reg_indirect        = qla4_83xx_rd_reg_indirect,
5698        .wr_reg_indirect        = qla4_83xx_wr_reg_indirect,
5699        .idc_lock               = qla4_83xx_drv_lock,
5700        .idc_unlock             = qla4_83xx_drv_unlock,
5701        .rom_lock_recovery      = qla4_83xx_rom_lock_recovery,
5702        .queue_mailbox_command  = qla4_83xx_queue_mbox_cmd,
5703        .process_mailbox_interrupt = qla4_83xx_process_mbox_intr,
5704};
5705
5706uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
5707{
5708        return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
5709}
5710
5711uint16_t qla4_82xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
5712{
5713        return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->req_q_out));
5714}
5715
5716uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
5717{
5718        return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
5719}
5720
5721uint16_t qla4_82xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
5722{
5723        return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->rsp_q_in));
5724}
5725
5726static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
5727{
5728        struct scsi_qla_host *ha = data;
5729        char *str = buf;
5730        int rc;
5731
5732        switch (type) {
5733        case ISCSI_BOOT_ETH_FLAGS:
5734                rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
5735                break;
5736        case ISCSI_BOOT_ETH_INDEX:
5737                rc = sprintf(str, "0\n");
5738                break;
5739        case ISCSI_BOOT_ETH_MAC:
5740                rc = sysfs_format_mac(str, ha->my_mac,
5741                                      MAC_ADDR_LEN);
5742                break;
5743        default:
5744                rc = -ENOSYS;
5745                break;
5746        }
5747        return rc;
5748}
5749
5750static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
5751{
5752        int rc;
5753
5754        switch (type) {
5755        case ISCSI_BOOT_ETH_FLAGS:
5756        case ISCSI_BOOT_ETH_MAC:
5757        case ISCSI_BOOT_ETH_INDEX:
5758                rc = S_IRUGO;
5759                break;
5760        default:
5761                rc = 0;
5762                break;
5763        }
5764        return rc;
5765}
5766
5767static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
5768{
5769        struct scsi_qla_host *ha = data;
5770        char *str = buf;
5771        int rc;
5772
5773        switch (type) {
5774        case ISCSI_BOOT_INI_INITIATOR_NAME:
5775                rc = sprintf(str, "%s\n", ha->name_string);
5776                break;
5777        default:
5778                rc = -ENOSYS;
5779                break;
5780        }
5781        return rc;
5782}
5783
5784static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
5785{
5786        int rc;
5787
5788        switch (type) {
5789        case ISCSI_BOOT_INI_INITIATOR_NAME:
5790                rc = S_IRUGO;
5791                break;
5792        default:
5793                rc = 0;
5794                break;
5795        }
5796        return rc;
5797}
5798
5799static ssize_t
5800qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
5801                           char *buf)
5802{
5803        struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
5804        char *str = buf;
5805        int rc;
5806
5807        switch (type) {
5808        case ISCSI_BOOT_TGT_NAME:
5809                rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
5810                break;
5811        case ISCSI_BOOT_TGT_IP_ADDR:
5812                if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
5813                        rc = sprintf(buf, "%pI4\n",
5814                                     &boot_conn->dest_ipaddr.ip_address);
5815                else
5816                        rc = sprintf(str, "%pI6\n",
5817                                     &boot_conn->dest_ipaddr.ip_address);
5818                break;
5819        case ISCSI_BOOT_TGT_PORT:
5820                        rc = sprintf(str, "%d\n", boot_conn->dest_port);
5821                break;
5822        case ISCSI_BOOT_TGT_CHAP_NAME:
5823                rc = sprintf(str,  "%.*s\n",
5824                             boot_conn->chap.target_chap_name_length,
5825                             (char *)&boot_conn->chap.target_chap_name);
5826                break;
5827        case ISCSI_BOOT_TGT_CHAP_SECRET:
5828                rc = sprintf(str,  "%.*s\n",
5829                             boot_conn->chap.target_secret_length,
5830                             (char *)&boot_conn->chap.target_secret);
5831                break;
5832        case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5833                rc = sprintf(str,  "%.*s\n",
5834                             boot_conn->chap.intr_chap_name_length,
5835                             (char *)&boot_conn->chap.intr_chap_name);
5836                break;
5837        case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5838                rc = sprintf(str,  "%.*s\n",
5839                             boot_conn->chap.intr_secret_length,
5840                             (char *)&boot_conn->chap.intr_secret);
5841                break;
5842        case ISCSI_BOOT_TGT_FLAGS:
5843                rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
5844                break;
5845        case ISCSI_BOOT_TGT_NIC_ASSOC:
5846                rc = sprintf(str, "0\n");
5847                break;
5848        default:
5849                rc = -ENOSYS;
5850                break;
5851        }
5852        return rc;
5853}
5854
5855static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
5856{
5857        struct scsi_qla_host *ha = data;
5858        struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
5859
5860        return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
5861}
5862
5863static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
5864{
5865        struct scsi_qla_host *ha = data;
5866        struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
5867
5868        return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
5869}
5870
5871static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
5872{
5873        int rc;
5874
5875        switch (type) {
5876        case ISCSI_BOOT_TGT_NAME:
5877        case ISCSI_BOOT_TGT_IP_ADDR:
5878        case ISCSI_BOOT_TGT_PORT:
5879        case ISCSI_BOOT_TGT_CHAP_NAME:
5880        case ISCSI_BOOT_TGT_CHAP_SECRET:
5881        case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5882        case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5883        case ISCSI_BOOT_TGT_NIC_ASSOC:
5884        case ISCSI_BOOT_TGT_FLAGS:
5885                rc = S_IRUGO;
5886                break;
5887        default:
5888                rc = 0;
5889                break;
5890        }
5891        return rc;
5892}
5893
5894static void qla4xxx_boot_release(void *data)
5895{
5896        struct scsi_qla_host *ha = data;
5897
5898        scsi_host_put(ha->host);
5899}
5900
5901static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
5902{
5903        dma_addr_t buf_dma;
5904        uint32_t addr, pri_addr, sec_addr;
5905        uint32_t offset;
5906        uint16_t func_num;
5907        uint8_t val;
5908        uint8_t *buf = NULL;
5909        size_t size = 13 * sizeof(uint8_t);
5910        int ret = QLA_SUCCESS;
5911
5912        func_num = PCI_FUNC(ha->pdev->devfn);
5913
5914        ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
5915                   __func__, ha->pdev->device, func_num);
5916
5917        if (is_qla40XX(ha)) {
5918                if (func_num == 1) {
5919                        addr = NVRAM_PORT0_BOOT_MODE;
5920                        pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
5921                        sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
5922                } else if (func_num == 3) {
5923                        addr = NVRAM_PORT1_BOOT_MODE;
5924                        pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
5925                        sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
5926                } else {
5927                        ret = QLA_ERROR;
5928                        goto exit_boot_info;
5929                }
5930
5931                /* Check Boot Mode */
5932                val = rd_nvram_byte(ha, addr);
5933                if (!(val & 0x07)) {
5934                        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
5935                                          "options : 0x%x\n", __func__, val));
5936                        ret = QLA_ERROR;
5937                        goto exit_boot_info;
5938                }
5939
5940                /* get primary valid target index */
5941                val = rd_nvram_byte(ha, pri_addr);
5942                if (val & BIT_7)
5943                        ddb_index[0] = (val & 0x7f);
5944
5945                /* get secondary valid target index */
5946                val = rd_nvram_byte(ha, sec_addr);
5947                if (val & BIT_7)
5948                        ddb_index[1] = (val & 0x7f);
5949
5950        } else if (is_qla80XX(ha)) {
5951                buf = dma_alloc_coherent(&ha->pdev->dev, size,
5952                                         &buf_dma, GFP_KERNEL);
5953                if (!buf) {
5954                        DEBUG2(ql4_printk(KERN_ERR, ha,
5955                                          "%s: Unable to allocate dma buffer\n",
5956                                           __func__));
5957                        ret = QLA_ERROR;
5958                        goto exit_boot_info;
5959                }
5960
5961                if (ha->port_num == 0)
5962                        offset = BOOT_PARAM_OFFSET_PORT0;
5963                else if (ha->port_num == 1)
5964                        offset = BOOT_PARAM_OFFSET_PORT1;
5965                else {
5966                        ret = QLA_ERROR;
5967                        goto exit_boot_info_free;
5968                }
5969                addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
5970                       offset;
5971                if (qla4xxx_get_flash(ha, buf_dma, addr,
5972                                      13 * sizeof(uint8_t)) != QLA_SUCCESS) {
5973                        DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
5974                                          " failed\n", ha->host_no, __func__));
5975                        ret = QLA_ERROR;
5976                        goto exit_boot_info_free;
5977                }
5978                /* Check Boot Mode */
5979                if (!(buf[1] & 0x07)) {
5980                        DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
5981                                          " : 0x%x\n", buf[1]));
5982                        ret = QLA_ERROR;
5983                        goto exit_boot_info_free;
5984                }
5985
5986                /* get primary valid target index */
5987                if (buf[2] & BIT_7)
5988                        ddb_index[0] = buf[2] & 0x7f;
5989
5990                /* get secondary valid target index */
5991                if (buf[11] & BIT_7)
5992                        ddb_index[1] = buf[11] & 0x7f;
5993        } else {
5994                ret = QLA_ERROR;
5995                goto exit_boot_info;
5996        }
5997
5998        DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
5999                          " target ID %d\n", __func__, ddb_index[0],
6000                          ddb_index[1]));
6001
6002exit_boot_info_free:
6003        dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
6004exit_boot_info:
6005        ha->pri_ddb_idx = ddb_index[0];
6006        ha->sec_ddb_idx = ddb_index[1];
6007        return ret;
6008}
6009
6010/**
6011 * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
6012 * @ha: pointer to adapter structure
6013 * @username: CHAP username to be returned
6014 * @password: CHAP password to be returned
6015 *
6016 * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
6017 * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
6018 * So from the CHAP cache find the first BIDI CHAP entry and set it
6019 * to the boot record in sysfs.
6020 **/
6021static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
6022                            char *password)
6023{
6024        int i, ret = -EINVAL;
6025        int max_chap_entries = 0;
6026        struct ql4_chap_table *chap_table;
6027
6028        if (is_qla80XX(ha))
6029                max_chap_entries = (ha->hw.flt_chap_size / 2) /
6030                                                sizeof(struct ql4_chap_table);
6031        else
6032                max_chap_entries = MAX_CHAP_ENTRIES_40XX;
6033
6034        if (!ha->chap_list) {
6035                ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
6036                return ret;
6037        }
6038
6039        mutex_lock(&ha->chap_sem);
6040        for (i = 0; i < max_chap_entries; i++) {
6041                chap_table = (struct ql4_chap_table *)ha->chap_list + i;
6042                if (chap_table->cookie !=
6043                    __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
6044                        continue;
6045                }
6046
6047                if (chap_table->flags & BIT_7) /* local */
6048                        continue;
6049
6050                if (!(chap_table->flags & BIT_6)) /* Not BIDI */
6051                        continue;
6052
6053                strlcpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
6054                strlcpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
6055                ret = 0;
6056                break;
6057        }
6058        mutex_unlock(&ha->chap_sem);
6059
6060        return ret;
6061}
6062
6063
6064static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
6065                                   struct ql4_boot_session_info *boot_sess,
6066                                   uint16_t ddb_index)
6067{
6068        struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
6069        struct dev_db_entry *fw_ddb_entry;
6070        dma_addr_t fw_ddb_entry_dma;
6071        uint16_t idx;
6072        uint16_t options;
6073        int ret = QLA_SUCCESS;
6074
6075        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
6076                                          &fw_ddb_entry_dma, GFP_KERNEL);
6077        if (!fw_ddb_entry) {
6078                DEBUG2(ql4_printk(KERN_ERR, ha,
6079                                  "%s: Unable to allocate dma buffer.\n",
6080                                  __func__));
6081                ret = QLA_ERROR;
6082                return ret;
6083        }
6084
6085        if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
6086                                   fw_ddb_entry_dma, ddb_index)) {
6087                DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
6088                                  "index [%d]\n", __func__, ddb_index));
6089                ret = QLA_ERROR;
6090                goto exit_boot_target;
6091        }
6092
6093        /* Update target name and IP from DDB */
6094        memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
6095               min(sizeof(boot_sess->target_name),
6096                   sizeof(fw_ddb_entry->iscsi_name)));
6097
6098        options = le16_to_cpu(fw_ddb_entry->options);
6099        if (options & DDB_OPT_IPV6_DEVICE) {
6100                memcpy(&boot_conn->dest_ipaddr.ip_address,
6101                       &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
6102        } else {
6103                boot_conn->dest_ipaddr.ip_type = 0x1;
6104                memcpy(&boot_conn->dest_ipaddr.ip_address,
6105                       &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
6106        }
6107
6108        boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
6109
6110        /* update chap information */
6111        idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
6112
6113        if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options))   {
6114
6115                DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
6116
6117                ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
6118                                       target_chap_name,
6119                                       (char *)&boot_conn->chap.target_secret,
6120                                       idx);
6121                if (ret) {
6122                        ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
6123                        ret = QLA_ERROR;
6124                        goto exit_boot_target;
6125                }
6126
6127                boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
6128                boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
6129        }
6130
6131        if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
6132
6133                DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
6134
6135                ret = qla4xxx_get_bidi_chap(ha,
6136                                    (char *)&boot_conn->chap.intr_chap_name,
6137                                    (char *)&boot_conn->chap.intr_secret);
6138
6139                if (ret) {
6140                        ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
6141                        ret = QLA_ERROR;
6142                        goto exit_boot_target;
6143                }
6144
6145                boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
6146                boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
6147        }
6148
6149exit_boot_target:
6150        dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
6151                          fw_ddb_entry, fw_ddb_entry_dma);
6152        return ret;
6153}
6154
6155static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
6156{
6157        uint16_t ddb_index[2];
6158        int ret = QLA_ERROR;
6159        int rval;
6160
6161        memset(ddb_index, 0, sizeof(ddb_index));
6162        ddb_index[0] = 0xffff;
6163        ddb_index[1] = 0xffff;
6164        ret = get_fw_boot_info(ha, ddb_index);
6165        if (ret != QLA_SUCCESS) {
6166                DEBUG2(ql4_printk(KERN_INFO, ha,
6167                                "%s: No boot target configured.\n", __func__));
6168                return ret;
6169        }
6170
6171        if (ql4xdisablesysfsboot)
6172                return QLA_SUCCESS;
6173
6174        if (ddb_index[0] == 0xffff)
6175                goto sec_target;
6176
6177        rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
6178                                      ddb_index[0]);
6179        if (rval != QLA_SUCCESS) {
6180                DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
6181                                  "configured\n", __func__));
6182        } else
6183                ret = QLA_SUCCESS;
6184
6185sec_target:
6186        if (ddb_index[1] == 0xffff)
6187                goto exit_get_boot_info;
6188
6189        rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
6190                                      ddb_index[1]);
6191        if (rval != QLA_SUCCESS) {
6192                DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
6193                                  " configured\n", __func__));
6194        } else
6195                ret = QLA_SUCCESS;
6196
6197exit_get_boot_info:
6198        return ret;
6199}
6200
6201static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
6202{
6203        struct iscsi_boot_kobj *boot_kobj;
6204
6205        if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
6206                return QLA_ERROR;
6207
6208        if (ql4xdisablesysfsboot) {
6209                ql4_printk(KERN_INFO, ha,
6210                           "%s: syfsboot disabled - driver will trigger login "
6211                           "and publish session for discovery .\n", __func__);
6212                return QLA_SUCCESS;
6213        }
6214
6215
6216        ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
6217        if (!ha->boot_kset)
6218                goto kset_free;
6219
6220        if (!scsi_host_get(ha->host))
6221                goto kset_free;
6222        boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
6223                                             qla4xxx_show_boot_tgt_pri_info,
6224                                             qla4xxx_tgt_get_attr_visibility,
6225                                             qla4xxx_boot_release);
6226        if (!boot_kobj)
6227                goto put_host;
6228
6229        if (!scsi_host_get(ha->host))
6230                goto kset_free;
6231        boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
6232                                             qla4xxx_show_boot_tgt_sec_info,
6233                                             qla4xxx_tgt_get_attr_visibility,
6234                                             qla4xxx_boot_release);
6235        if (!boot_kobj)
6236                goto put_host;
6237
6238        if (!scsi_host_get(ha->host))
6239                goto kset_free;
6240        boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
6241                                               qla4xxx_show_boot_ini_info,
6242                                               qla4xxx_ini_get_attr_visibility,
6243                                               qla4xxx_boot_release);
6244        if (!boot_kobj)
6245                goto put_host;
6246
6247        if (!scsi_host_get(ha->host))
6248                goto kset_free;
6249        boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
6250                                               qla4xxx_show_boot_eth_info,
6251                                               qla4xxx_eth_get_attr_visibility,
6252                                               qla4xxx_boot_release);
6253        if (!boot_kobj)
6254                goto put_host;
6255
6256        return QLA_SUCCESS;
6257
6258put_host:
6259        scsi_host_put(ha->host);
6260kset_free:
6261        iscsi_boot_destroy_kset(ha->boot_kset);
6262        return -ENOMEM;
6263}
6264
6265
6266static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
6267                                  struct ql4_tuple_ddb *tddb)
6268{
6269        struct scsi_qla_host *ha;
6270        struct iscsi_cls_session *cls_sess;
6271        struct iscsi_cls_conn *cls_conn;
6272        struct iscsi_session *sess;
6273        struct iscsi_conn *conn;
6274
6275        DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
6276        ha = ddb_entry->ha;
6277        cls_sess = ddb_entry->sess;
6278        sess = cls_sess->dd_data;
6279        cls_conn = ddb_entry->conn;
6280        conn = cls_conn->dd_data;
6281
6282        tddb->tpgt = sess->tpgt;
6283        tddb->port = conn->persistent_port;
6284        strlcpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
6285        strlcpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
6286}
6287
6288static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
6289                                      struct ql4_tuple_ddb *tddb,
6290                                      uint8_t *flash_isid)
6291{
6292        uint16_t options = 0;
6293
6294        tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
6295        memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
6296               min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
6297
6298        options = le16_to_cpu(fw_ddb_entry->options);
6299        if (options & DDB_OPT_IPV6_DEVICE)
6300                sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
6301        else
6302                sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
6303
6304        tddb->port = le16_to_cpu(fw_ddb_entry->port);
6305
6306        if (flash_isid == NULL)
6307                memcpy(&tddb->isid[0], &fw_ddb_entry->isid[0],
6308                       sizeof(tddb->isid));
6309        else
6310                memcpy(&tddb->isid[0], &flash_isid[0], sizeof(tddb->isid));
6311}
6312
6313static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
6314                                     struct ql4_tuple_ddb *old_tddb,
6315                                     struct ql4_tuple_ddb *new_tddb,
6316                                     uint8_t is_isid_compare)
6317{
6318        if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
6319                return QLA_ERROR;
6320
6321        if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
6322                return QLA_ERROR;
6323
6324        if (old_tddb->port != new_tddb->port)
6325                return QLA_ERROR;
6326
6327        /* For multi sessions, driver generates the ISID, so do not compare
6328         * ISID in reset path since it would be a comparison between the
6329         * driver generated ISID and firmware generated ISID. This could
6330         * lead to adding duplicated DDBs in the list as driver generated
6331         * ISID would not match firmware generated ISID.
6332         */
6333        if (is_isid_compare) {
6334                DEBUG2(ql4_printk(KERN_INFO, ha, "%s: old ISID [%02x%02x%02x"
6335                        "%02x%02x%02x] New ISID [%02x%02x%02x%02x%02x%02x]\n",
6336                        __func__, old_tddb->isid[5], old_tddb->isid[4],
6337                        old_tddb->isid[3], old_tddb->isid[2], old_tddb->isid[1],
6338                        old_tddb->isid[0], new_tddb->isid[5], new_tddb->isid[4],
6339                        new_tddb->isid[3], new_tddb->isid[2], new_tddb->isid[1],
6340                        new_tddb->isid[0]));
6341
6342                if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
6343                           sizeof(old_tddb->isid)))
6344                        return QLA_ERROR;
6345        }
6346
6347        DEBUG2(ql4_printk(KERN_INFO, ha,
6348                          "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
6349                          old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
6350                          old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
6351                          new_tddb->ip_addr, new_tddb->iscsi_name));
6352
6353        return QLA_SUCCESS;
6354}
6355
6356static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
6357                                     struct dev_db_entry *fw_ddb_entry,
6358                                     uint32_t *index)
6359{
6360        struct ddb_entry *ddb_entry;
6361        struct ql4_tuple_ddb *fw_tddb = NULL;
6362        struct ql4_tuple_ddb *tmp_tddb = NULL;
6363        int idx;
6364        int ret = QLA_ERROR;
6365
6366        fw_tddb = vzalloc(sizeof(*fw_tddb));
6367        if (!fw_tddb) {
6368                DEBUG2(ql4_printk(KERN_WARNING, ha,
6369                                  "Memory Allocation failed.\n"));
6370                ret = QLA_SUCCESS;
6371                goto exit_check;
6372        }
6373
6374        tmp_tddb = vzalloc(sizeof(*tmp_tddb));
6375        if (!tmp_tddb) {
6376                DEBUG2(ql4_printk(KERN_WARNING, ha,
6377                                  "Memory Allocation failed.\n"));
6378                ret = QLA_SUCCESS;
6379                goto exit_check;
6380        }
6381
6382        qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
6383
6384        for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
6385                ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
6386                if (ddb_entry == NULL)
6387                        continue;
6388
6389                qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
6390                if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, false)) {
6391                        ret = QLA_SUCCESS; /* found */
6392                        if (index != NULL)
6393                                *index = idx;
6394                        goto exit_check;
6395                }
6396        }
6397
6398exit_check:
6399        if (fw_tddb)
6400                vfree(fw_tddb);
6401        if (tmp_tddb)
6402                vfree(tmp_tddb);
6403        return ret;
6404}
6405
6406/**
6407 * qla4xxx_check_existing_isid - check if target with same isid exist
6408 *                               in target list
6409 * @list_nt: list of target
6410 * @isid: isid to check
6411 *
6412 * This routine return QLA_SUCCESS if target with same isid exist
6413 **/
6414static int qla4xxx_check_existing_isid(struct list_head *list_nt, uint8_t *isid)
6415{
6416        struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
6417        struct dev_db_entry *fw_ddb_entry;
6418
6419        list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
6420                fw_ddb_entry = &nt_ddb_idx->fw_ddb;
6421
6422                if (memcmp(&fw_ddb_entry->isid[0], &isid[0],
6423                           sizeof(nt_ddb_idx->fw_ddb.isid)) == 0) {
6424                        return QLA_SUCCESS;
6425                }
6426        }
6427        return QLA_ERROR;
6428}
6429
6430/**
6431 * qla4xxx_update_isid - compare ddbs and updated isid
6432 * @ha: Pointer to host adapter structure.
6433 * @list_nt: list of nt target
6434 * @fw_ddb_entry: firmware ddb entry
6435 *
6436 * This routine update isid if ddbs have same iqn, same isid and
6437 * different IP addr.
6438 * Return QLA_SUCCESS if isid is updated.
6439 **/
6440static int qla4xxx_update_isid(struct scsi_qla_host *ha,
6441                               struct list_head *list_nt,
6442                               struct dev_db_entry *fw_ddb_entry)
6443{
6444        uint8_t base_value, i;
6445
6446        base_value = fw_ddb_entry->isid[1] & 0x1f;
6447        for (i = 0; i < 8; i++) {
6448                fw_ddb_entry->isid[1] = (base_value | (i << 5));
6449                if (qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
6450                        break;
6451        }
6452
6453        if (!qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
6454                return QLA_ERROR;
6455
6456        return QLA_SUCCESS;
6457}
6458
6459/**
6460 * qla4xxx_should_update_isid - check if isid need to update
6461 * @ha: Pointer to host adapter structure.
6462 * @old_tddb: ddb tuple
6463 * @new_tddb: ddb tuple
6464 *
6465 * Return QLA_SUCCESS if different IP, different PORT, same iqn,
6466 * same isid
6467 **/
6468static int qla4xxx_should_update_isid(struct scsi_qla_host *ha,
6469                                      struct ql4_tuple_ddb *old_tddb,
6470                                      struct ql4_tuple_ddb *new_tddb)
6471{
6472        if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr) == 0) {
6473                /* Same ip */
6474                if (old_tddb->port == new_tddb->port)
6475                        return QLA_ERROR;
6476        }
6477
6478        if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
6479                /* different iqn */
6480                return QLA_ERROR;
6481
6482        if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
6483                   sizeof(old_tddb->isid)))
6484                /* different isid */
6485                return QLA_ERROR;
6486
6487        return QLA_SUCCESS;
6488}
6489
6490/**
6491 * qla4xxx_is_flash_ddb_exists - check if fw_ddb_entry already exists in list_nt
6492 * @ha: Pointer to host adapter structure.
6493 * @list_nt: list of nt target.
6494 * @fw_ddb_entry: firmware ddb entry.
6495 *
6496 * This routine check if fw_ddb_entry already exists in list_nt to avoid
6497 * duplicate ddb in list_nt.
6498 * Return QLA_SUCCESS if duplicate ddb exit in list_nl.
6499 * Note: This function also update isid of DDB if required.
6500 **/
6501
6502static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
6503                                       struct list_head *list_nt,
6504                                       struct dev_db_entry *fw_ddb_entry)
6505{
6506        struct qla_ddb_index  *nt_ddb_idx, *nt_ddb_idx_tmp;
6507        struct ql4_tuple_ddb *fw_tddb = NULL;
6508        struct ql4_tuple_ddb *tmp_tddb = NULL;
6509        int rval, ret = QLA_ERROR;
6510
6511        fw_tddb = vzalloc(sizeof(*fw_tddb));
6512        if (!fw_tddb) {
6513                DEBUG2(ql4_printk(KERN_WARNING, ha,
6514                                  "Memory Allocation failed.\n"));
6515                ret = QLA_SUCCESS;
6516                goto exit_check;
6517        }
6518
6519        tmp_tddb = vzalloc(sizeof(*tmp_tddb));
6520        if (!tmp_tddb) {
6521                DEBUG2(ql4_printk(KERN_WARNING, ha,
6522                                  "Memory Allocation failed.\n"));
6523                ret = QLA_SUCCESS;
6524                goto exit_check;
6525        }
6526
6527        qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
6528
6529        list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
6530                qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb,
6531                                          nt_ddb_idx->flash_isid);
6532                ret = qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, true);
6533                /* found duplicate ddb */
6534                if (ret == QLA_SUCCESS)
6535                        goto exit_check;
6536        }
6537
6538        list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
6539                qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb, NULL);
6540
6541                ret = qla4xxx_should_update_isid(ha, tmp_tddb, fw_tddb);
6542                if (ret == QLA_SUCCESS) {
6543                        rval = qla4xxx_update_isid(ha, list_nt, fw_ddb_entry);
6544                        if (rval == QLA_SUCCESS)
6545                                ret = QLA_ERROR;
6546                        else
6547                                ret = QLA_SUCCESS;
6548
6549                        goto exit_check;
6550                }
6551        }
6552
6553exit_check:
6554        if (fw_tddb)
6555                vfree(fw_tddb);
6556        if (tmp_tddb)
6557                vfree(tmp_tddb);
6558        return ret;
6559}
6560
6561static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
6562{
6563        struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
6564
6565        list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
6566                list_del_init(&ddb_idx->list);
6567                vfree(ddb_idx);
6568        }
6569}
6570
6571static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
6572                                        struct dev_db_entry *fw_ddb_entry)
6573{
6574        struct iscsi_endpoint *ep;
6575        struct sockaddr_in *addr;
6576        struct sockaddr_in6 *addr6;
6577        struct sockaddr *t_addr;
6578        struct sockaddr_storage *dst_addr;
6579        char *ip;
6580
6581        /* TODO: need to destroy on unload iscsi_endpoint*/
6582        dst_addr = vmalloc(sizeof(*dst_addr));
6583        if (!dst_addr)
6584                return NULL;
6585
6586        if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
6587                t_addr = (struct sockaddr *)dst_addr;
6588                t_addr->sa_family = AF_INET6;
6589                addr6 = (struct sockaddr_in6 *)dst_addr;
6590                ip = (char *)&addr6->sin6_addr;
6591                memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
6592                addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
6593
6594        } else {
6595                t_addr = (struct sockaddr *)dst_addr;
6596                t_addr->sa_family = AF_INET;
6597                addr = (struct sockaddr_in *)dst_addr;
6598                ip = (char *)&addr->sin_addr;
6599                memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
6600                addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
6601        }
6602
6603        ep = qla4xxx_ep_connect(ha->host, (struct sockaddr *)dst_addr, 0);
6604        vfree(dst_addr);
6605        return ep;
6606}
6607
6608static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
6609{
6610        if (ql4xdisablesysfsboot)
6611                return QLA_SUCCESS;
6612        if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
6613                return QLA_ERROR;
6614        return QLA_SUCCESS;
6615}
6616
6617static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
6618                                          struct ddb_entry *ddb_entry,
6619                                          uint16_t idx)
6620{
6621        uint16_t def_timeout;
6622
6623        ddb_entry->ddb_type = FLASH_DDB;
6624        ddb_entry->fw_ddb_index = INVALID_ENTRY;
6625        ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
6626        ddb_entry->ha = ha;
6627        ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
6628        ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
6629        ddb_entry->chap_tbl_idx = INVALID_ENTRY;
6630
6631        atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
6632        atomic_set(&ddb_entry->relogin_timer, 0);
6633        atomic_set(&ddb_entry->relogin_retry_count, 0);
6634        def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
6635        ddb_entry->default_relogin_timeout =
6636                (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
6637                def_timeout : LOGIN_TOV;
6638        ddb_entry->default_time2wait =
6639                le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
6640
6641        if (ql4xdisablesysfsboot &&
6642            (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx))
6643                set_bit(DF_BOOT_TGT, &ddb_entry->flags);
6644}
6645
6646static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
6647{
6648        uint32_t idx = 0;
6649        uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
6650        uint32_t sts[MBOX_REG_COUNT];
6651        uint32_t ip_state;
6652        unsigned long wtime;
6653        int ret;
6654
6655        wtime = jiffies + (HZ * IP_CONFIG_TOV);
6656        do {
6657                for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
6658                        if (ip_idx[idx] == -1)
6659                                continue;
6660
6661                        ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
6662
6663                        if (ret == QLA_ERROR) {
6664                                ip_idx[idx] = -1;
6665                                continue;
6666                        }
6667
6668                        ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
6669
6670                        DEBUG2(ql4_printk(KERN_INFO, ha,
6671                                          "Waiting for IP state for idx = %d, state = 0x%x\n",
6672                                          ip_idx[idx], ip_state));
6673                        if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
6674                            ip_state == IP_ADDRSTATE_INVALID ||
6675                            ip_state == IP_ADDRSTATE_PREFERRED ||
6676                            ip_state == IP_ADDRSTATE_DEPRICATED ||
6677                            ip_state == IP_ADDRSTATE_DISABLING)
6678                                ip_idx[idx] = -1;
6679                }
6680
6681                /* Break if all IP states checked */
6682                if ((ip_idx[0] == -1) &&
6683                    (ip_idx[1] == -1) &&
6684                    (ip_idx[2] == -1) &&
6685                    (ip_idx[3] == -1))
6686                        break;
6687                schedule_timeout_uninterruptible(HZ);
6688        } while (time_after(wtime, jiffies));
6689}
6690
6691static int qla4xxx_cmp_fw_stentry(struct dev_db_entry *fw_ddb_entry,
6692                                  struct dev_db_entry *flash_ddb_entry)
6693{
6694        uint16_t options = 0;
6695        size_t ip_len = IP_ADDR_LEN;
6696
6697        options = le16_to_cpu(fw_ddb_entry->options);
6698        if (options & DDB_OPT_IPV6_DEVICE)
6699                ip_len = IPv6_ADDR_LEN;
6700
6701        if (memcmp(fw_ddb_entry->ip_addr, flash_ddb_entry->ip_addr, ip_len))
6702                return QLA_ERROR;
6703
6704        if (memcmp(&fw_ddb_entry->isid[0], &flash_ddb_entry->isid[0],
6705                   sizeof(fw_ddb_entry->isid)))
6706                return QLA_ERROR;
6707
6708        if (memcmp(&fw_ddb_entry->port, &flash_ddb_entry->port,
6709                   sizeof(fw_ddb_entry->port)))
6710                return QLA_ERROR;
6711
6712        return QLA_SUCCESS;
6713}
6714
6715static int qla4xxx_find_flash_st_idx(struct scsi_qla_host *ha,
6716                                     struct dev_db_entry *fw_ddb_entry,
6717                                     uint32_t fw_idx, uint32_t *flash_index)
6718{
6719        struct dev_db_entry *flash_ddb_entry;
6720        dma_addr_t flash_ddb_entry_dma;
6721        uint32_t idx = 0;
6722        int max_ddbs;
6723        int ret = QLA_ERROR, status;
6724
6725        max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6726                                     MAX_DEV_DB_ENTRIES;
6727
6728        flash_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
6729                                         &flash_ddb_entry_dma);
6730        if (flash_ddb_entry == NULL || fw_ddb_entry == NULL) {
6731                ql4_printk(KERN_ERR, ha, "Out of memory\n");
6732                goto exit_find_st_idx;
6733        }
6734
6735        status = qla4xxx_flashdb_by_index(ha, flash_ddb_entry,
6736                                          flash_ddb_entry_dma, fw_idx);
6737        if (status == QLA_SUCCESS) {
6738                status = qla4xxx_cmp_fw_stentry(fw_ddb_entry, flash_ddb_entry);
6739                if (status == QLA_SUCCESS) {
6740                        *flash_index = fw_idx;
6741                        ret = QLA_SUCCESS;
6742                        goto exit_find_st_idx;
6743                }
6744        }
6745
6746        for (idx = 0; idx < max_ddbs; idx++) {
6747                status = qla4xxx_flashdb_by_index(ha, flash_ddb_entry,
6748                                                  flash_ddb_entry_dma, idx);
6749                if (status == QLA_ERROR)
6750                        continue;
6751
6752                status = qla4xxx_cmp_fw_stentry(fw_ddb_entry, flash_ddb_entry);
6753                if (status == QLA_SUCCESS) {
6754                        *flash_index = idx;
6755                        ret = QLA_SUCCESS;
6756                        goto exit_find_st_idx;
6757                }
6758        }
6759
6760        if (idx == max_ddbs)
6761                ql4_printk(KERN_ERR, ha, "Failed to find ST [%d] in flash\n",
6762                           fw_idx);
6763
6764exit_find_st_idx:
6765        if (flash_ddb_entry)
6766                dma_pool_free(ha->fw_ddb_dma_pool, flash_ddb_entry,
6767                              flash_ddb_entry_dma);
6768
6769        return ret;
6770}
6771
6772static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
6773                                  struct list_head *list_st)
6774{
6775        struct qla_ddb_index  *st_ddb_idx;
6776        int max_ddbs;
6777        int fw_idx_size;
6778        struct dev_db_entry *fw_ddb_entry;
6779        dma_addr_t fw_ddb_dma;
6780        int ret;
6781        uint32_t idx = 0, next_idx = 0;
6782        uint32_t state = 0, conn_err = 0;
6783        uint32_t flash_index = -1;
6784        uint16_t conn_id = 0;
6785
6786        fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
6787                                      &fw_ddb_dma);
6788        if (fw_ddb_entry == NULL) {
6789                DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
6790                goto exit_st_list;
6791        }
6792
6793        max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6794                                     MAX_DEV_DB_ENTRIES;
6795        fw_idx_size = sizeof(struct qla_ddb_index);
6796
6797        for (idx = 0; idx < max_ddbs; idx = next_idx) {
6798                ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
6799                                              NULL, &next_idx, &state,
6800                                              &conn_err, NULL, &conn_id);
6801                if (ret == QLA_ERROR)
6802                        break;
6803
6804                /* Ignore DDB if invalid state (unassigned) */
6805                if (state == DDB_DS_UNASSIGNED)
6806                        goto continue_next_st;
6807
6808                /* Check if ST, add to the list_st */
6809                if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
6810                        goto continue_next_st;
6811
6812                st_ddb_idx = vzalloc(fw_idx_size);
6813                if (!st_ddb_idx)
6814                        break;
6815
6816                ret = qla4xxx_find_flash_st_idx(ha, fw_ddb_entry, idx,
6817                                                &flash_index);
6818                if (ret == QLA_ERROR) {
6819                        ql4_printk(KERN_ERR, ha,
6820                                   "No flash entry for ST at idx [%d]\n", idx);
6821                        st_ddb_idx->flash_ddb_idx = idx;
6822                } else {
6823                        ql4_printk(KERN_INFO, ha,
6824                                   "ST at idx [%d] is stored at flash [%d]\n",
6825                                   idx, flash_index);
6826                        st_ddb_idx->flash_ddb_idx = flash_index;
6827                }
6828
6829                st_ddb_idx->fw_ddb_idx = idx;
6830
6831                list_add_tail(&st_ddb_idx->list, list_st);
6832continue_next_st:
6833                if (next_idx == 0)
6834                        break;
6835        }
6836
6837exit_st_list:
6838        if (fw_ddb_entry)
6839                dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
6840}
6841
6842/**
6843 * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
6844 * @ha: pointer to adapter structure
6845 * @list_ddb: List from which failed ddb to be removed
6846 *
6847 * Iterate over the list of DDBs and find and remove DDBs that are either in
6848 * no connection active state or failed state
6849 **/
6850static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
6851                                      struct list_head *list_ddb)
6852{
6853        struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
6854        uint32_t next_idx = 0;
6855        uint32_t state = 0, conn_err = 0;
6856        int ret;
6857
6858        list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
6859                ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
6860                                              NULL, 0, NULL, &next_idx, &state,
6861                                              &conn_err, NULL, NULL);
6862                if (ret == QLA_ERROR)
6863                        continue;
6864
6865                if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
6866                    state == DDB_DS_SESSION_FAILED) {
6867                        list_del_init(&ddb_idx->list);
6868                        vfree(ddb_idx);
6869                }
6870        }
6871}
6872
6873static void qla4xxx_update_sess_disc_idx(struct scsi_qla_host *ha,
6874                                         struct ddb_entry *ddb_entry,
6875                                         struct dev_db_entry *fw_ddb_entry)
6876{
6877        struct iscsi_cls_session *cls_sess;
6878        struct iscsi_session *sess;
6879        uint32_t max_ddbs = 0;
6880        uint16_t ddb_link = -1;
6881
6882        max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6883                                     MAX_DEV_DB_ENTRIES;
6884
6885        cls_sess = ddb_entry->sess;
6886        sess = cls_sess->dd_data;
6887
6888        ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
6889        if (ddb_link < max_ddbs)
6890                sess->discovery_parent_idx = ddb_link;
6891        else
6892                sess->discovery_parent_idx = DDB_NO_LINK;
6893}
6894
6895static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
6896                                   struct dev_db_entry *fw_ddb_entry,
6897                                   int is_reset, uint16_t idx)
6898{
6899        struct iscsi_cls_session *cls_sess;
6900        struct iscsi_session *sess;
6901        struct iscsi_cls_conn *cls_conn;
6902        struct iscsi_endpoint *ep;
6903        uint16_t cmds_max = 32;
6904        uint16_t conn_id = 0;
6905        uint32_t initial_cmdsn = 0;
6906        int ret = QLA_SUCCESS;
6907
6908        struct ddb_entry *ddb_entry = NULL;
6909
6910        /* Create session object, with INVALID_ENTRY,
6911         * the targer_id would get set when we issue the login
6912         */
6913        cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
6914                                       cmds_max, sizeof(struct ddb_entry),
6915                                       sizeof(struct ql4_task_data),
6916                                       initial_cmdsn, INVALID_ENTRY);
6917        if (!cls_sess) {
6918                ret = QLA_ERROR;
6919                goto exit_setup;
6920        }
6921
6922        /*
6923         * so calling module_put function to decrement the
6924         * reference count.
6925         **/
6926        module_put(qla4xxx_iscsi_transport.owner);
6927        sess = cls_sess->dd_data;
6928        ddb_entry = sess->dd_data;
6929        ddb_entry->sess = cls_sess;
6930
6931        cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
6932        memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
6933               sizeof(struct dev_db_entry));
6934
6935        qla4xxx_setup_flash_ddb_entry(ha, ddb_entry, idx);
6936
6937        cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
6938
6939        if (!cls_conn) {
6940                ret = QLA_ERROR;
6941                goto exit_setup;
6942        }
6943
6944        ddb_entry->conn = cls_conn;
6945
6946        /* Setup ep, for displaying attributes in sysfs */
6947        ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
6948        if (ep) {
6949                ep->conn = cls_conn;
6950                cls_conn->ep = ep;
6951        } else {
6952                DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
6953                ret = QLA_ERROR;
6954                goto exit_setup;
6955        }
6956
6957        /* Update sess/conn params */
6958        qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
6959        qla4xxx_update_sess_disc_idx(ha, ddb_entry, fw_ddb_entry);
6960
6961        if (is_reset == RESET_ADAPTER) {
6962                iscsi_block_session(cls_sess);
6963                /* Use the relogin path to discover new devices
6964                 *  by short-circuting the logic of setting
6965                 *  timer to relogin - instead set the flags
6966                 *  to initiate login right away.
6967                 */
6968                set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
6969                set_bit(DF_RELOGIN, &ddb_entry->flags);
6970        }
6971
6972exit_setup:
6973        return ret;
6974}
6975
6976static void qla4xxx_update_fw_ddb_link(struct scsi_qla_host *ha,
6977                                       struct list_head *list_ddb,
6978                                       struct dev_db_entry *fw_ddb_entry)
6979{
6980        struct qla_ddb_index  *ddb_idx, *ddb_idx_tmp;
6981        uint16_t ddb_link;
6982
6983        ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
6984
6985        list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
6986                if (ddb_idx->fw_ddb_idx == ddb_link) {
6987                        DEBUG2(ql4_printk(KERN_INFO, ha,
6988                                          "Updating NT parent idx from [%d] to [%d]\n",
6989                                          ddb_link, ddb_idx->flash_ddb_idx));
6990                        fw_ddb_entry->ddb_link =
6991                                            cpu_to_le16(ddb_idx->flash_ddb_idx);
6992                        return;
6993                }
6994        }
6995}
6996
6997static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
6998                                  struct list_head *list_nt,
6999                                  struct list_head *list_st,
7000                                  int is_reset)
7001{
7002        struct dev_db_entry *fw_ddb_entry;
7003        struct ddb_entry *ddb_entry = NULL;
7004        dma_addr_t fw_ddb_dma;
7005        int max_ddbs;
7006        int fw_idx_size;
7007        int ret;
7008        uint32_t idx = 0, next_idx = 0;
7009        uint32_t state = 0, conn_err = 0;
7010        uint32_t ddb_idx = -1;
7011        uint16_t conn_id = 0;
7012        uint16_t ddb_link = -1;
7013        struct qla_ddb_index  *nt_ddb_idx;
7014
7015        fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
7016                                      &fw_ddb_dma);
7017        if (fw_ddb_entry == NULL) {
7018                DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
7019                goto exit_nt_list;
7020        }
7021        max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
7022                                     MAX_DEV_DB_ENTRIES;
7023        fw_idx_size = sizeof(struct qla_ddb_index);
7024
7025        for (idx = 0; idx < max_ddbs; idx = next_idx) {
7026                ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
7027                                              NULL, &next_idx, &state,
7028                                              &conn_err, NULL, &conn_id);
7029                if (ret == QLA_ERROR)
7030                        break;
7031
7032                if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
7033                        goto continue_next_nt;
7034
7035                /* Check if NT, then add to list it */
7036                if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
7037                        goto continue_next_nt;
7038
7039                ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
7040                if (ddb_link < max_ddbs)
7041                        qla4xxx_update_fw_ddb_link(ha, list_st, fw_ddb_entry);
7042
7043                if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
7044                    state == DDB_DS_SESSION_FAILED) &&
7045                    (is_reset == INIT_ADAPTER))
7046                        goto continue_next_nt;
7047
7048                DEBUG2(ql4_printk(KERN_INFO, ha,
7049                                  "Adding  DDB to session = 0x%x\n", idx));
7050
7051                if (is_reset == INIT_ADAPTER) {
7052                        nt_ddb_idx = vmalloc(fw_idx_size);
7053                        if (!nt_ddb_idx)
7054                                break;
7055
7056                        nt_ddb_idx->fw_ddb_idx = idx;
7057
7058                        /* Copy original isid as it may get updated in function
7059                         * qla4xxx_update_isid(). We need original isid in
7060                         * function qla4xxx_compare_tuple_ddb to find duplicate
7061                         * target */
7062                        memcpy(&nt_ddb_idx->flash_isid[0],
7063                               &fw_ddb_entry->isid[0],
7064                               sizeof(nt_ddb_idx->flash_isid));
7065
7066                        ret = qla4xxx_is_flash_ddb_exists(ha, list_nt,
7067                                                          fw_ddb_entry);
7068                        if (ret == QLA_SUCCESS) {
7069                                /* free nt_ddb_idx and do not add to list_nt */
7070                                vfree(nt_ddb_idx);
7071                                goto continue_next_nt;
7072                        }
7073
7074                        /* Copy updated isid */
7075                        memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
7076                               sizeof(struct dev_db_entry));
7077
7078                        list_add_tail(&nt_ddb_idx->list, list_nt);
7079                } else if (is_reset == RESET_ADAPTER) {
7080                        ret = qla4xxx_is_session_exists(ha, fw_ddb_entry,
7081                                                        &ddb_idx);
7082                        if (ret == QLA_SUCCESS) {
7083                                ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
7084                                                                       ddb_idx);
7085                                if (ddb_entry != NULL)
7086                                        qla4xxx_update_sess_disc_idx(ha,
7087                                                                     ddb_entry,
7088                                                                  fw_ddb_entry);
7089                                goto continue_next_nt;
7090                        }
7091                }
7092
7093                ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset, idx);
7094                if (ret == QLA_ERROR)
7095                        goto exit_nt_list;
7096
7097continue_next_nt:
7098                if (next_idx == 0)
7099                        break;
7100        }
7101
7102exit_nt_list:
7103        if (fw_ddb_entry)
7104                dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
7105}
7106
7107static void qla4xxx_build_new_nt_list(struct scsi_qla_host *ha,
7108                                      struct list_head *list_nt,
7109                                      uint16_t target_id)
7110{
7111        struct dev_db_entry *fw_ddb_entry;
7112        dma_addr_t fw_ddb_dma;
7113        int max_ddbs;
7114        int fw_idx_size;
7115        int ret;
7116        uint32_t idx = 0, next_idx = 0;
7117        uint32_t state = 0, conn_err = 0;
7118        uint16_t conn_id = 0;
7119        struct qla_ddb_index  *nt_ddb_idx;
7120
7121        fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
7122                                      &fw_ddb_dma);
7123        if (fw_ddb_entry == NULL) {
7124                DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
7125                goto exit_new_nt_list;
7126        }
7127        max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
7128                                     MAX_DEV_DB_ENTRIES;
7129        fw_idx_size = sizeof(struct qla_ddb_index);
7130
7131        for (idx = 0; idx < max_ddbs; idx = next_idx) {
7132                ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
7133                                              NULL, &next_idx, &state,
7134                                              &conn_err, NULL, &conn_id);
7135                if (ret == QLA_ERROR)
7136                        break;
7137
7138                /* Check if NT, then add it to list */
7139                if (strlen((char *)fw_ddb_entry->iscsi_name) == 0)
7140                        goto continue_next_new_nt;
7141
7142                if (!(state == DDB_DS_NO_CONNECTION_ACTIVE))
7143                        goto continue_next_new_nt;
7144
7145                DEBUG2(ql4_printk(KERN_INFO, ha,
7146                                  "Adding  DDB to session = 0x%x\n", idx));
7147
7148                nt_ddb_idx = vmalloc(fw_idx_size);
7149                if (!nt_ddb_idx)
7150                        break;
7151
7152                nt_ddb_idx->fw_ddb_idx = idx;
7153
7154                ret = qla4xxx_is_session_exists(ha, fw_ddb_entry, NULL);
7155                if (ret == QLA_SUCCESS) {
7156                        /* free nt_ddb_idx and do not add to list_nt */
7157                        vfree(nt_ddb_idx);
7158                        goto continue_next_new_nt;
7159                }
7160
7161                if (target_id < max_ddbs)
7162                        fw_ddb_entry->ddb_link = cpu_to_le16(target_id);
7163
7164                list_add_tail(&nt_ddb_idx->list, list_nt);
7165
7166                ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, RESET_ADAPTER,
7167                                              idx);
7168                if (ret == QLA_ERROR)
7169                        goto exit_new_nt_list;
7170
7171continue_next_new_nt:
7172                if (next_idx == 0)
7173                        break;
7174        }
7175
7176exit_new_nt_list:
7177        if (fw_ddb_entry)
7178                dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
7179}
7180
7181/**
7182 * qla4xxx_sysfs_ddb_is_non_persistent - check for non-persistence of ddb entry
7183 * @dev: dev associated with the sysfs entry
7184 * @data: pointer to flashnode session object
7185 *
7186 * Returns:
7187 *      1: if flashnode entry is non-persistent
7188 *      0: if flashnode entry is persistent
7189 **/
7190static int qla4xxx_sysfs_ddb_is_non_persistent(struct device *dev, void *data)
7191{
7192        struct iscsi_bus_flash_session *fnode_sess;
7193
7194        if (!iscsi_flashnode_bus_match(dev, NULL))
7195                return 0;
7196
7197        fnode_sess = iscsi_dev_to_flash_session(dev);
7198
7199        return (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT);
7200}
7201
7202/**
7203 * qla4xxx_sysfs_ddb_tgt_create - Create sysfs entry for target
7204 * @ha: pointer to host
7205 * @fw_ddb_entry: flash ddb data
7206 * @idx: target index
7207 * @user: if set then this call is made from userland else from kernel
7208 *
7209 * Returns:
7210 * On sucess: QLA_SUCCESS
7211 * On failure: QLA_ERROR
7212 *
7213 * This create separate sysfs entries for session and connection attributes of
7214 * the given fw ddb entry.
7215 * If this is invoked as a result of a userspace call then the entry is marked
7216 * as nonpersistent using flash_state field.
7217 **/
7218static int qla4xxx_sysfs_ddb_tgt_create(struct scsi_qla_host *ha,
7219                                        struct dev_db_entry *fw_ddb_entry,
7220                                        uint16_t *idx, int user)
7221{
7222        struct iscsi_bus_flash_session *fnode_sess = NULL;
7223        struct iscsi_bus_flash_conn *fnode_conn = NULL;
7224        int rc = QLA_ERROR;
7225
7226        fnode_sess = iscsi_create_flashnode_sess(ha->host, *idx,
7227                                                 &qla4xxx_iscsi_transport, 0);
7228        if (!fnode_sess) {
7229                ql4_printk(KERN_ERR, ha,
7230                           "%s: Unable to create session sysfs entry for flashnode %d of host%lu\n",
7231                           __func__, *idx, ha->host_no);
7232                goto exit_tgt_create;
7233        }
7234
7235        fnode_conn = iscsi_create_flashnode_conn(ha->host, fnode_sess,
7236                                                 &qla4xxx_iscsi_transport, 0);
7237        if (!fnode_conn) {
7238                ql4_printk(KERN_ERR, ha,
7239                           "%s: Unable to create conn sysfs entry for flashnode %d of host%lu\n",
7240                           __func__, *idx, ha->host_no);
7241                goto free_sess;
7242        }
7243
7244        if (user) {
7245                fnode_sess->flash_state = DEV_DB_NON_PERSISTENT;
7246        } else {
7247                fnode_sess->flash_state = DEV_DB_PERSISTENT;
7248
7249                if (*idx == ha->pri_ddb_idx || *idx == ha->sec_ddb_idx)
7250                        fnode_sess->is_boot_target = 1;
7251                else
7252                        fnode_sess->is_boot_target = 0;
7253        }
7254
7255        rc = qla4xxx_copy_from_fwddb_param(fnode_sess, fnode_conn,
7256                                           fw_ddb_entry);
7257
7258        ql4_printk(KERN_INFO, ha, "%s: sysfs entry %s created\n",
7259                   __func__, fnode_sess->dev.kobj.name);
7260
7261        ql4_printk(KERN_INFO, ha, "%s: sysfs entry %s created\n",
7262                   __func__, fnode_conn->dev.kobj.name);
7263
7264        return QLA_SUCCESS;
7265
7266free_sess:
7267        iscsi_destroy_flashnode_sess(fnode_sess);
7268
7269exit_tgt_create:
7270        return QLA_ERROR;
7271}
7272
7273/**
7274 * qla4xxx_sysfs_ddb_add - Add new ddb entry in flash
7275 * @shost: pointer to host
7276 * @buf: type of ddb entry (ipv4/ipv6)
7277 * @len: length of buf
7278 *
7279 * This creates new ddb entry in the flash by finding first free index and
7280 * storing default ddb there. And then create sysfs entry for the new ddb entry.
7281 **/
7282static int qla4xxx_sysfs_ddb_add(struct Scsi_Host *shost, const char *buf,
7283                                 int len)
7284{
7285        struct scsi_qla_host *ha = to_qla_host(shost);
7286        struct dev_db_entry *fw_ddb_entry = NULL;
7287        dma_addr_t fw_ddb_entry_dma;
7288        struct device *dev;
7289        uint16_t idx = 0;
7290        uint16_t max_ddbs = 0;
7291        uint32_t options = 0;
7292        uint32_t rval = QLA_ERROR;
7293
7294        if (strncasecmp(PORTAL_TYPE_IPV4, buf, 4) &&
7295            strncasecmp(PORTAL_TYPE_IPV6, buf, 4)) {
7296                DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Invalid portal type\n",
7297                                  __func__));
7298                goto exit_ddb_add;
7299        }
7300
7301        max_ddbs =  is_qla40XX(ha) ? MAX_PRST_DEV_DB_ENTRIES :
7302                                     MAX_DEV_DB_ENTRIES;
7303
7304        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7305                                          &fw_ddb_entry_dma, GFP_KERNEL);
7306        if (!fw_ddb_entry) {
7307                DEBUG2(ql4_printk(KERN_ERR, ha,
7308                                  "%s: Unable to allocate dma buffer\n",
7309                                  __func__));
7310                goto exit_ddb_add;
7311        }
7312
7313        dev = iscsi_find_flashnode_sess(ha->host, NULL,
7314                                        qla4xxx_sysfs_ddb_is_non_persistent);
7315        if (dev) {
7316                ql4_printk(KERN_ERR, ha,
7317                           "%s: A non-persistent entry %s found\n",
7318                           __func__, dev->kobj.name);
7319                put_device(dev);
7320                goto exit_ddb_add;
7321        }
7322
7323        /* Index 0 and 1 are reserved for boot target entries */
7324        for (idx = 2; idx < max_ddbs; idx++) {
7325                if (qla4xxx_flashdb_by_index(ha, fw_ddb_entry,
7326                                             fw_ddb_entry_dma, idx))
7327                        break;
7328        }
7329
7330        if (idx == max_ddbs)
7331                goto exit_ddb_add;
7332
7333        if (!strncasecmp("ipv6", buf, 4))
7334                options |= IPV6_DEFAULT_DDB_ENTRY;
7335
7336        rval = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
7337        if (rval == QLA_ERROR)
7338                goto exit_ddb_add;
7339
7340        rval = qla4xxx_sysfs_ddb_tgt_create(ha, fw_ddb_entry, &idx, 1);
7341
7342exit_ddb_add:
7343        if (fw_ddb_entry)
7344                dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7345                                  fw_ddb_entry, fw_ddb_entry_dma);
7346        if (rval == QLA_SUCCESS)
7347                return idx;
7348        else
7349                return -EIO;
7350}
7351
7352/**
7353 * qla4xxx_sysfs_ddb_apply - write the target ddb contents to Flash
7354 * @fnode_sess: pointer to session attrs of flash ddb entry
7355 * @fnode_conn: pointer to connection attrs of flash ddb entry
7356 *
7357 * This writes the contents of target ddb buffer to Flash with a valid cookie
7358 * value in order to make the ddb entry persistent.
7359 **/
7360static int  qla4xxx_sysfs_ddb_apply(struct iscsi_bus_flash_session *fnode_sess,
7361                                    struct iscsi_bus_flash_conn *fnode_conn)
7362{
7363        struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
7364        struct scsi_qla_host *ha = to_qla_host(shost);
7365        uint32_t dev_db_start_offset = FLASH_OFFSET_DB_INFO;
7366        struct dev_db_entry *fw_ddb_entry = NULL;
7367        dma_addr_t fw_ddb_entry_dma;
7368        uint32_t options = 0;
7369        int rval = 0;
7370
7371        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7372                                          &fw_ddb_entry_dma, GFP_KERNEL);
7373        if (!fw_ddb_entry) {
7374                DEBUG2(ql4_printk(KERN_ERR, ha,
7375                                  "%s: Unable to allocate dma buffer\n",
7376                                  __func__));
7377                rval = -ENOMEM;
7378                goto exit_ddb_apply;
7379        }
7380
7381        if (!strncasecmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7382                options |= IPV6_DEFAULT_DDB_ENTRY;
7383
7384        rval = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
7385        if (rval == QLA_ERROR)
7386                goto exit_ddb_apply;
7387
7388        dev_db_start_offset += (fnode_sess->target_id *
7389                                sizeof(*fw_ddb_entry));
7390
7391        qla4xxx_copy_to_fwddb_param(fnode_sess, fnode_conn, fw_ddb_entry);
7392        fw_ddb_entry->cookie = DDB_VALID_COOKIE;
7393
7394        rval = qla4xxx_set_flash(ha, fw_ddb_entry_dma, dev_db_start_offset,
7395                                 sizeof(*fw_ddb_entry), FLASH_OPT_RMW_COMMIT);
7396
7397        if (rval == QLA_SUCCESS) {
7398                fnode_sess->flash_state = DEV_DB_PERSISTENT;
7399                ql4_printk(KERN_INFO, ha,
7400                           "%s: flash node %u of host %lu written to flash\n",
7401                           __func__, fnode_sess->target_id, ha->host_no);
7402        } else {
7403                rval = -EIO;
7404                ql4_printk(KERN_ERR, ha,
7405                           "%s: Error while writing flash node %u of host %lu to flash\n",
7406                           __func__, fnode_sess->target_id, ha->host_no);
7407        }
7408
7409exit_ddb_apply:
7410        if (fw_ddb_entry)
7411                dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7412                                  fw_ddb_entry, fw_ddb_entry_dma);
7413        return rval;
7414}
7415
7416static ssize_t qla4xxx_sysfs_ddb_conn_open(struct scsi_qla_host *ha,
7417                                           struct dev_db_entry *fw_ddb_entry,
7418                                           uint16_t idx)
7419{
7420        struct dev_db_entry *ddb_entry = NULL;
7421        dma_addr_t ddb_entry_dma;
7422        unsigned long wtime;
7423        uint32_t mbx_sts = 0;
7424        uint32_t state = 0, conn_err = 0;
7425        uint16_t tmo = 0;
7426        int ret = 0;
7427
7428        ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*ddb_entry),
7429                                       &ddb_entry_dma, GFP_KERNEL);
7430        if (!ddb_entry) {
7431                DEBUG2(ql4_printk(KERN_ERR, ha,
7432                                  "%s: Unable to allocate dma buffer\n",
7433                                  __func__));
7434                return QLA_ERROR;
7435        }
7436
7437        memcpy(ddb_entry, fw_ddb_entry, sizeof(*ddb_entry));
7438
7439        ret = qla4xxx_set_ddb_entry(ha, idx, ddb_entry_dma, &mbx_sts);
7440        if (ret != QLA_SUCCESS) {
7441                DEBUG2(ql4_printk(KERN_ERR, ha,
7442                                  "%s: Unable to set ddb entry for index %d\n",
7443                                  __func__, idx));
7444                goto exit_ddb_conn_open;
7445        }
7446
7447        qla4xxx_conn_open(ha, idx);
7448
7449        /* To ensure that sendtargets is done, wait for at least 12 secs */
7450        tmo = ((ha->def_timeout > LOGIN_TOV) &&
7451               (ha->def_timeout < LOGIN_TOV * 10) ?
7452               ha->def_timeout : LOGIN_TOV);
7453
7454        DEBUG2(ql4_printk(KERN_INFO, ha,
7455                          "Default time to wait for login to ddb %d\n", tmo));
7456
7457        wtime = jiffies + (HZ * tmo);
7458        do {
7459                ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
7460                                              NULL, &state, &conn_err, NULL,
7461                                              NULL);
7462                if (ret == QLA_ERROR)
7463                        continue;
7464
7465                if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
7466                    state == DDB_DS_SESSION_FAILED)
7467                        break;
7468
7469                schedule_timeout_uninterruptible(HZ / 10);
7470        } while (time_after(wtime, jiffies));
7471
7472exit_ddb_conn_open:
7473        if (ddb_entry)
7474                dma_free_coherent(&ha->pdev->dev, sizeof(*ddb_entry),
7475                                  ddb_entry, ddb_entry_dma);
7476        return ret;
7477}
7478
7479static int qla4xxx_ddb_login_st(struct scsi_qla_host *ha,
7480                                struct dev_db_entry *fw_ddb_entry,
7481                                uint16_t target_id)
7482{
7483        struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
7484        struct list_head list_nt;
7485        uint16_t ddb_index;
7486        int ret = 0;
7487
7488        if (test_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags)) {
7489                ql4_printk(KERN_WARNING, ha,
7490                           "%s: A discovery already in progress!\n", __func__);
7491                return QLA_ERROR;
7492        }
7493
7494        INIT_LIST_HEAD(&list_nt);
7495
7496        set_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags);
7497
7498        ret = qla4xxx_get_ddb_index(ha, &ddb_index);
7499        if (ret == QLA_ERROR)
7500                goto exit_login_st_clr_bit;
7501
7502        ret = qla4xxx_sysfs_ddb_conn_open(ha, fw_ddb_entry, ddb_index);
7503        if (ret == QLA_ERROR)
7504                goto exit_login_st;
7505
7506        qla4xxx_build_new_nt_list(ha, &list_nt, target_id);
7507
7508        list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, &list_nt, list) {
7509                list_del_init(&ddb_idx->list);
7510                qla4xxx_clear_ddb_entry(ha, ddb_idx->fw_ddb_idx);
7511                vfree(ddb_idx);
7512        }
7513
7514exit_login_st:
7515        if (qla4xxx_clear_ddb_entry(ha, ddb_index) == QLA_ERROR) {
7516                ql4_printk(KERN_ERR, ha,
7517                           "Unable to clear DDB index = 0x%x\n", ddb_index);
7518        }
7519
7520        clear_bit(ddb_index, ha->ddb_idx_map);
7521
7522exit_login_st_clr_bit:
7523        clear_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags);
7524        return ret;
7525}
7526
7527static int qla4xxx_ddb_login_nt(struct scsi_qla_host *ha,
7528                                struct dev_db_entry *fw_ddb_entry,
7529                                uint16_t idx)
7530{
7531        int ret = QLA_ERROR;
7532
7533        ret = qla4xxx_is_session_exists(ha, fw_ddb_entry, NULL);
7534        if (ret != QLA_SUCCESS)
7535                ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, RESET_ADAPTER,
7536                                              idx);
7537        else
7538                ret = -EPERM;
7539
7540        return ret;
7541}
7542
7543/**
7544 * qla4xxx_sysfs_ddb_login - Login to the specified target
7545 * @fnode_sess: pointer to session attrs of flash ddb entry
7546 * @fnode_conn: pointer to connection attrs of flash ddb entry
7547 *
7548 * This logs in to the specified target
7549 **/
7550static int qla4xxx_sysfs_ddb_login(struct iscsi_bus_flash_session *fnode_sess,
7551                                   struct iscsi_bus_flash_conn *fnode_conn)
7552{
7553        struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
7554        struct scsi_qla_host *ha = to_qla_host(shost);
7555        struct dev_db_entry *fw_ddb_entry = NULL;
7556        dma_addr_t fw_ddb_entry_dma;
7557        uint32_t options = 0;
7558        int ret = 0;
7559
7560        if (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT) {
7561                ql4_printk(KERN_ERR, ha,
7562                           "%s: Target info is not persistent\n", __func__);
7563                ret = -EIO;
7564                goto exit_ddb_login;
7565        }
7566
7567        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7568                                          &fw_ddb_entry_dma, GFP_KERNEL);
7569        if (!fw_ddb_entry) {
7570                DEBUG2(ql4_printk(KERN_ERR, ha,
7571                                  "%s: Unable to allocate dma buffer\n",
7572                                  __func__));
7573                ret = -ENOMEM;
7574                goto exit_ddb_login;
7575        }
7576
7577        if (!strncasecmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7578                options |= IPV6_DEFAULT_DDB_ENTRY;
7579
7580        ret = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
7581        if (ret == QLA_ERROR)
7582                goto exit_ddb_login;
7583
7584        qla4xxx_copy_to_fwddb_param(fnode_sess, fnode_conn, fw_ddb_entry);
7585        fw_ddb_entry->cookie = DDB_VALID_COOKIE;
7586
7587        if (strlen((char *)fw_ddb_entry->iscsi_name) == 0)
7588                ret = qla4xxx_ddb_login_st(ha, fw_ddb_entry,
7589                                           fnode_sess->target_id);
7590        else
7591                ret = qla4xxx_ddb_login_nt(ha, fw_ddb_entry,
7592                                           fnode_sess->target_id);
7593
7594        if (ret > 0)
7595                ret = -EIO;
7596
7597exit_ddb_login:
7598        if (fw_ddb_entry)
7599                dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7600                                  fw_ddb_entry, fw_ddb_entry_dma);
7601        return ret;
7602}
7603
7604/**
7605 * qla4xxx_sysfs_ddb_logout_sid - Logout session for the specified target
7606 * @cls_sess: pointer to session to be logged out
7607 *
7608 * This performs session log out from the specified target
7609 **/
7610static int qla4xxx_sysfs_ddb_logout_sid(struct iscsi_cls_session *cls_sess)
7611{
7612        struct iscsi_session *sess;
7613        struct ddb_entry *ddb_entry = NULL;
7614        struct scsi_qla_host *ha;
7615        struct dev_db_entry *fw_ddb_entry = NULL;
7616        dma_addr_t fw_ddb_entry_dma;
7617        unsigned long flags;
7618        unsigned long wtime;
7619        uint32_t ddb_state;
7620        int options;
7621        int ret = 0;
7622
7623        sess = cls_sess->dd_data;
7624        ddb_entry = sess->dd_data;
7625        ha = ddb_entry->ha;
7626
7627        if (ddb_entry->ddb_type != FLASH_DDB) {
7628                ql4_printk(KERN_ERR, ha, "%s: Not a flash node session\n",
7629                           __func__);
7630                ret = -ENXIO;
7631                goto exit_ddb_logout;
7632        }
7633
7634        if (test_bit(DF_BOOT_TGT, &ddb_entry->flags)) {
7635                ql4_printk(KERN_ERR, ha,
7636                           "%s: Logout from boot target entry is not permitted.\n",
7637                           __func__);
7638                ret = -EPERM;
7639                goto exit_ddb_logout;
7640        }
7641
7642        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7643                                          &fw_ddb_entry_dma, GFP_KERNEL);
7644        if (!fw_ddb_entry) {
7645                ql4_printk(KERN_ERR, ha,
7646                           "%s: Unable to allocate dma buffer\n", __func__);
7647                ret = -ENOMEM;
7648                goto exit_ddb_logout;
7649        }
7650
7651        if (test_and_set_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags))
7652                goto ddb_logout_init;
7653
7654        ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
7655                                      fw_ddb_entry, fw_ddb_entry_dma,
7656                                      NULL, NULL, &ddb_state, NULL,
7657                                      NULL, NULL);
7658        if (ret == QLA_ERROR)
7659                goto ddb_logout_init;
7660
7661        if (ddb_state == DDB_DS_SESSION_ACTIVE)
7662                goto ddb_logout_init;
7663
7664        /* wait until next relogin is triggered using DF_RELOGIN and
7665         * clear DF_RELOGIN to avoid invocation of further relogin
7666         */
7667        wtime = jiffies + (HZ * RELOGIN_TOV);
7668        do {
7669                if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags))
7670                        goto ddb_logout_init;
7671
7672                schedule_timeout_uninterruptible(HZ);
7673        } while ((time_after(wtime, jiffies)));
7674
7675ddb_logout_init:
7676        atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
7677        atomic_set(&ddb_entry->relogin_timer, 0);
7678
7679        options = LOGOUT_OPTION_CLOSE_SESSION;
7680        qla4xxx_session_logout_ddb(ha, ddb_entry, options);
7681
7682        memset(fw_ddb_entry, 0, sizeof(*fw_ddb_entry));
7683        wtime = jiffies + (HZ * LOGOUT_TOV);
7684        do {
7685                ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
7686                                              fw_ddb_entry, fw_ddb_entry_dma,
7687                                              NULL, NULL, &ddb_state, NULL,
7688                                              NULL, NULL);
7689                if (ret == QLA_ERROR)
7690                        goto ddb_logout_clr_sess;
7691
7692                if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
7693                    (ddb_state == DDB_DS_SESSION_FAILED))
7694                        goto ddb_logout_clr_sess;
7695
7696                schedule_timeout_uninterruptible(HZ);
7697        } while ((time_after(wtime, jiffies)));
7698
7699ddb_logout_clr_sess:
7700        qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
7701        /*
7702         * we have decremented the reference count of the driver
7703         * when we setup the session to have the driver unload
7704         * to be seamless without actually destroying the
7705         * session
7706         **/
7707        try_module_get(qla4xxx_iscsi_transport.owner);
7708        iscsi_destroy_endpoint(ddb_entry->conn->ep);
7709
7710        spin_lock_irqsave(&ha->hardware_lock, flags);
7711        qla4xxx_free_ddb(ha, ddb_entry);
7712        clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
7713        spin_unlock_irqrestore(&ha->hardware_lock, flags);
7714
7715        iscsi_session_teardown(ddb_entry->sess);
7716
7717        clear_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags);
7718        ret = QLA_SUCCESS;
7719
7720exit_ddb_logout:
7721        if (fw_ddb_entry)
7722                dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
7723                                  fw_ddb_entry, fw_ddb_entry_dma);
7724        return ret;
7725}
7726
7727/**
7728 * qla4xxx_sysfs_ddb_logout - Logout from the specified target
7729 * @fnode_sess: pointer to session attrs of flash ddb entry
7730 * @fnode_conn: pointer to connection attrs of flash ddb entry
7731 *
7732 * This performs log out from the specified target
7733 **/
7734static int qla4xxx_sysfs_ddb_logout(struct iscsi_bus_flash_session *fnode_sess,
7735                                    struct iscsi_bus_flash_conn *fnode_conn)
7736{
7737        struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
7738        struct scsi_qla_host *ha = to_qla_host(shost);
7739        struct ql4_tuple_ddb *flash_tddb = NULL;
7740        struct ql4_tuple_ddb *tmp_tddb = NULL;
7741        struct dev_db_entry *fw_ddb_entry = NULL;
7742        struct ddb_entry *ddb_entry = NULL;
7743        dma_addr_t fw_ddb_dma;
7744        uint32_t next_idx = 0;
7745        uint32_t state = 0, conn_err = 0;
7746        uint16_t conn_id = 0;
7747        int idx, index;
7748        int status, ret = 0;
7749
7750        fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
7751                                      &fw_ddb_dma);
7752        if (fw_ddb_entry == NULL) {
7753                ql4_printk(KERN_ERR, ha, "%s:Out of memory\n", __func__);
7754                ret = -ENOMEM;
7755                goto exit_ddb_logout;
7756        }
7757
7758        flash_tddb = vzalloc(sizeof(*flash_tddb));
7759        if (!flash_tddb) {
7760                ql4_printk(KERN_WARNING, ha,
7761                           "%s:Memory Allocation failed.\n", __func__);
7762                ret = -ENOMEM;
7763                goto exit_ddb_logout;
7764        }
7765
7766        tmp_tddb = vzalloc(sizeof(*tmp_tddb));
7767        if (!tmp_tddb) {
7768                ql4_printk(KERN_WARNING, ha,
7769                           "%s:Memory Allocation failed.\n", __func__);
7770                ret = -ENOMEM;
7771                goto exit_ddb_logout;
7772        }
7773
7774        if (!fnode_sess->targetname) {
7775                ql4_printk(KERN_ERR, ha,
7776                           "%s:Cannot logout from SendTarget entry\n",
7777                           __func__);
7778                ret = -EPERM;
7779                goto exit_ddb_logout;
7780        }
7781
7782        if (fnode_sess->is_boot_target) {
7783                ql4_printk(KERN_ERR, ha,
7784                           "%s: Logout from boot target entry is not permitted.\n",
7785                           __func__);
7786                ret = -EPERM;
7787                goto exit_ddb_logout;
7788        }
7789
7790        strlcpy(flash_tddb->iscsi_name, fnode_sess->targetname,
7791                ISCSI_NAME_SIZE);
7792
7793        if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7794                sprintf(flash_tddb->ip_addr, "%pI6", fnode_conn->ipaddress);
7795        else
7796                sprintf(flash_tddb->ip_addr, "%pI4", fnode_conn->ipaddress);
7797
7798        flash_tddb->tpgt = fnode_sess->tpgt;
7799        flash_tddb->port = fnode_conn->port;
7800
7801        COPY_ISID(flash_tddb->isid, fnode_sess->isid);
7802
7803        for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
7804                ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
7805                if (ddb_entry == NULL)
7806                        continue;
7807
7808                if (ddb_entry->ddb_type != FLASH_DDB)
7809                        continue;
7810
7811                index = ddb_entry->sess->target_id;
7812                status = qla4xxx_get_fwddb_entry(ha, index, fw_ddb_entry,
7813                                                 fw_ddb_dma, NULL, &next_idx,
7814                                                 &state, &conn_err, NULL,
7815                                                 &conn_id);
7816                if (status == QLA_ERROR) {
7817                        ret = -ENOMEM;
7818                        break;
7819                }
7820
7821                qla4xxx_convert_param_ddb(fw_ddb_entry, tmp_tddb, NULL);
7822
7823                status = qla4xxx_compare_tuple_ddb(ha, flash_tddb, tmp_tddb,
7824                                                   true);
7825                if (status == QLA_SUCCESS) {
7826                        ret = qla4xxx_sysfs_ddb_logout_sid(ddb_entry->sess);
7827                        break;
7828                }
7829        }
7830
7831        if (idx == MAX_DDB_ENTRIES)
7832                ret = -ESRCH;
7833
7834exit_ddb_logout:
7835        if (flash_tddb)
7836                vfree(flash_tddb);
7837        if (tmp_tddb)
7838                vfree(tmp_tddb);
7839        if (fw_ddb_entry)
7840                dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
7841
7842        return ret;
7843}
7844
7845static int
7846qla4xxx_sysfs_ddb_get_param(struct iscsi_bus_flash_session *fnode_sess,
7847                            int param, char *buf)
7848{
7849        struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
7850        struct scsi_qla_host *ha = to_qla_host(shost);
7851        struct iscsi_bus_flash_conn *fnode_conn;
7852        struct ql4_chap_table chap_tbl;
7853        struct device *dev;
7854        int parent_type;
7855        int rc = 0;
7856
7857        dev = iscsi_find_flashnode_conn(fnode_sess);
7858        if (!dev)
7859                return -EIO;
7860
7861        fnode_conn = iscsi_dev_to_flash_conn(dev);
7862
7863        switch (param) {
7864        case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
7865                rc = sprintf(buf, "%u\n", fnode_conn->is_fw_assigned_ipv6);
7866                break;
7867        case ISCSI_FLASHNODE_PORTAL_TYPE:
7868                rc = sprintf(buf, "%s\n", fnode_sess->portal_type);
7869                break;
7870        case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
7871                rc = sprintf(buf, "%u\n", fnode_sess->auto_snd_tgt_disable);
7872                break;
7873        case ISCSI_FLASHNODE_DISCOVERY_SESS:
7874                rc = sprintf(buf, "%u\n", fnode_sess->discovery_sess);
7875                break;
7876        case ISCSI_FLASHNODE_ENTRY_EN:
7877                rc = sprintf(buf, "%u\n", fnode_sess->entry_state);
7878                break;
7879        case ISCSI_FLASHNODE_HDR_DGST_EN:
7880                rc = sprintf(buf, "%u\n", fnode_conn->hdrdgst_en);
7881                break;
7882        case ISCSI_FLASHNODE_DATA_DGST_EN:
7883                rc = sprintf(buf, "%u\n", fnode_conn->datadgst_en);
7884                break;
7885        case ISCSI_FLASHNODE_IMM_DATA_EN:
7886                rc = sprintf(buf, "%u\n", fnode_sess->imm_data_en);
7887                break;
7888        case ISCSI_FLASHNODE_INITIAL_R2T_EN:
7889                rc = sprintf(buf, "%u\n", fnode_sess->initial_r2t_en);
7890                break;
7891        case ISCSI_FLASHNODE_DATASEQ_INORDER:
7892                rc = sprintf(buf, "%u\n", fnode_sess->dataseq_inorder_en);
7893                break;
7894        case ISCSI_FLASHNODE_PDU_INORDER:
7895                rc = sprintf(buf, "%u\n", fnode_sess->pdu_inorder_en);
7896                break;
7897        case ISCSI_FLASHNODE_CHAP_AUTH_EN:
7898                rc = sprintf(buf, "%u\n", fnode_sess->chap_auth_en);
7899                break;
7900        case ISCSI_FLASHNODE_SNACK_REQ_EN:
7901                rc = sprintf(buf, "%u\n", fnode_conn->snack_req_en);
7902                break;
7903        case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
7904                rc = sprintf(buf, "%u\n", fnode_sess->discovery_logout_en);
7905                break;
7906        case ISCSI_FLASHNODE_BIDI_CHAP_EN:
7907                rc = sprintf(buf, "%u\n", fnode_sess->bidi_chap_en);
7908                break;
7909        case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
7910                rc = sprintf(buf, "%u\n", fnode_sess->discovery_auth_optional);
7911                break;
7912        case ISCSI_FLASHNODE_ERL:
7913                rc = sprintf(buf, "%u\n", fnode_sess->erl);
7914                break;
7915        case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
7916                rc = sprintf(buf, "%u\n", fnode_conn->tcp_timestamp_stat);
7917                break;
7918        case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
7919                rc = sprintf(buf, "%u\n", fnode_conn->tcp_nagle_disable);
7920                break;
7921        case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
7922                rc = sprintf(buf, "%u\n", fnode_conn->tcp_wsf_disable);
7923                break;
7924        case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
7925                rc = sprintf(buf, "%u\n", fnode_conn->tcp_timer_scale);
7926                break;
7927        case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
7928                rc = sprintf(buf, "%u\n", fnode_conn->tcp_timestamp_en);
7929                break;
7930        case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
7931                rc = sprintf(buf, "%u\n", fnode_conn->fragment_disable);
7932                break;
7933        case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
7934                rc = sprintf(buf, "%u\n", fnode_conn->max_recv_dlength);
7935                break;
7936        case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
7937                rc = sprintf(buf, "%u\n", fnode_conn->max_xmit_dlength);
7938                break;
7939        case ISCSI_FLASHNODE_FIRST_BURST:
7940                rc = sprintf(buf, "%u\n", fnode_sess->first_burst);
7941                break;
7942        case ISCSI_FLASHNODE_DEF_TIME2WAIT:
7943                rc = sprintf(buf, "%u\n", fnode_sess->time2wait);
7944                break;
7945        case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
7946                rc = sprintf(buf, "%u\n", fnode_sess->time2retain);
7947                break;
7948        case ISCSI_FLASHNODE_MAX_R2T:
7949                rc = sprintf(buf, "%u\n", fnode_sess->max_r2t);
7950                break;
7951        case ISCSI_FLASHNODE_KEEPALIVE_TMO:
7952                rc = sprintf(buf, "%u\n", fnode_conn->keepalive_timeout);
7953                break;
7954        case ISCSI_FLASHNODE_ISID:
7955                rc = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n",
7956                             fnode_sess->isid[0], fnode_sess->isid[1],
7957                             fnode_sess->isid[2], fnode_sess->isid[3],
7958                             fnode_sess->isid[4], fnode_sess->isid[5]);
7959                break;
7960        case ISCSI_FLASHNODE_TSID:
7961                rc = sprintf(buf, "%u\n", fnode_sess->tsid);
7962                break;
7963        case ISCSI_FLASHNODE_PORT:
7964                rc = sprintf(buf, "%d\n", fnode_conn->port);
7965                break;
7966        case ISCSI_FLASHNODE_MAX_BURST:
7967                rc = sprintf(buf, "%u\n", fnode_sess->max_burst);
7968                break;
7969        case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
7970                rc = sprintf(buf, "%u\n",
7971                             fnode_sess->default_taskmgmt_timeout);
7972                break;
7973        case ISCSI_FLASHNODE_IPADDR:
7974                if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7975                        rc = sprintf(buf, "%pI6\n", fnode_conn->ipaddress);
7976                else
7977                        rc = sprintf(buf, "%pI4\n", fnode_conn->ipaddress);
7978                break;
7979        case ISCSI_FLASHNODE_ALIAS:
7980                if (fnode_sess->targetalias)
7981                        rc = sprintf(buf, "%s\n", fnode_sess->targetalias);
7982                else
7983                        rc = sprintf(buf, "\n");
7984                break;
7985        case ISCSI_FLASHNODE_REDIRECT_IPADDR:
7986                if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
7987                        rc = sprintf(buf, "%pI6\n",
7988                                     fnode_conn->redirect_ipaddr);
7989                else
7990                        rc = sprintf(buf, "%pI4\n",
7991                                     fnode_conn->redirect_ipaddr);
7992                break;
7993        case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
7994                rc = sprintf(buf, "%u\n", fnode_conn->max_segment_size);
7995                break;
7996        case ISCSI_FLASHNODE_LOCAL_PORT:
7997                rc = sprintf(buf, "%u\n", fnode_conn->local_port);
7998                break;
7999        case ISCSI_FLASHNODE_IPV4_TOS:
8000                rc = sprintf(buf, "%u\n", fnode_conn->ipv4_tos);
8001                break;
8002        case ISCSI_FLASHNODE_IPV6_TC:
8003                if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
8004                        rc = sprintf(buf, "%u\n",
8005                                     fnode_conn->ipv6_traffic_class);
8006                else
8007                        rc = sprintf(buf, "\n");
8008                break;
8009        case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
8010                rc = sprintf(buf, "%u\n", fnode_conn->ipv6_flow_label);
8011                break;
8012        case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
8013                if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
8014                        rc = sprintf(buf, "%pI6\n",
8015                                     fnode_conn->link_local_ipv6_addr);
8016                else
8017                        rc = sprintf(buf, "\n");
8018                break;
8019        case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
8020                rc = sprintf(buf, "%u\n", fnode_sess->discovery_parent_idx);
8021                break;
8022        case ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE:
8023                if (fnode_sess->discovery_parent_type == DDB_ISNS)
8024                        parent_type = ISCSI_DISC_PARENT_ISNS;
8025                else if (fnode_sess->discovery_parent_type == DDB_NO_LINK)
8026                        parent_type = ISCSI_DISC_PARENT_UNKNOWN;
8027                else if (fnode_sess->discovery_parent_type < MAX_DDB_ENTRIES)
8028                        parent_type = ISCSI_DISC_PARENT_SENDTGT;
8029                else
8030                        parent_type = ISCSI_DISC_PARENT_UNKNOWN;
8031
8032                rc = sprintf(buf, "%s\n",
8033                             iscsi_get_discovery_parent_name(parent_type));
8034                break;
8035        case ISCSI_FLASHNODE_NAME:
8036                if (fnode_sess->targetname)
8037                        rc = sprintf(buf, "%s\n", fnode_sess->targetname);
8038                else
8039                        rc = sprintf(buf, "\n");
8040                break;
8041        case ISCSI_FLASHNODE_TPGT:
8042                rc = sprintf(buf, "%u\n", fnode_sess->tpgt);
8043                break;
8044        case ISCSI_FLASHNODE_TCP_XMIT_WSF:
8045                rc = sprintf(buf, "%u\n", fnode_conn->tcp_xmit_wsf);
8046                break;
8047        case ISCSI_FLASHNODE_TCP_RECV_WSF:
8048                rc = sprintf(buf, "%u\n", fnode_conn->tcp_recv_wsf);
8049                break;
8050        case ISCSI_FLASHNODE_CHAP_OUT_IDX:
8051                rc = sprintf(buf, "%u\n", fnode_sess->chap_out_idx);
8052                break;
8053        case ISCSI_FLASHNODE_USERNAME:
8054                if (fnode_sess->chap_auth_en) {
8055                        qla4xxx_get_uni_chap_at_index(ha,
8056                                                      chap_tbl.name,
8057                                                      chap_tbl.secret,
8058                                                      fnode_sess->chap_out_idx);
8059                        rc = sprintf(buf, "%s\n", chap_tbl.name);
8060                } else {
8061                        rc = sprintf(buf, "\n");
8062                }
8063                break;
8064        case ISCSI_FLASHNODE_PASSWORD:
8065                if (fnode_sess->chap_auth_en) {
8066                        qla4xxx_get_uni_chap_at_index(ha,
8067                                                      chap_tbl.name,
8068                                                      chap_tbl.secret,
8069                                                      fnode_sess->chap_out_idx);
8070                        rc = sprintf(buf, "%s\n", chap_tbl.secret);
8071                } else {
8072                        rc = sprintf(buf, "\n");
8073                }
8074                break;
8075        case ISCSI_FLASHNODE_STATSN:
8076                rc = sprintf(buf, "%u\n", fnode_conn->statsn);
8077                break;
8078        case ISCSI_FLASHNODE_EXP_STATSN:
8079                rc = sprintf(buf, "%u\n", fnode_conn->exp_statsn);
8080                break;
8081        case ISCSI_FLASHNODE_IS_BOOT_TGT:
8082                rc = sprintf(buf, "%u\n", fnode_sess->is_boot_target);
8083                break;
8084        default:
8085                rc = -ENOSYS;
8086                break;
8087        }
8088
8089        put_device(dev);
8090        return rc;
8091}
8092
8093/**
8094 * qla4xxx_sysfs_ddb_set_param - Set parameter for firmware DDB entry
8095 * @fnode_sess: pointer to session attrs of flash ddb entry
8096 * @fnode_conn: pointer to connection attrs of flash ddb entry
8097 * @data: Parameters and their values to update
8098 * @len: len of data
8099 *
8100 * This sets the parameter of flash ddb entry and writes them to flash
8101 **/
8102static int
8103qla4xxx_sysfs_ddb_set_param(struct iscsi_bus_flash_session *fnode_sess,
8104                            struct iscsi_bus_flash_conn *fnode_conn,
8105                            void *data, int len)
8106{
8107        struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
8108        struct scsi_qla_host *ha = to_qla_host(shost);
8109        struct iscsi_flashnode_param_info *fnode_param;
8110        struct ql4_chap_table chap_tbl;
8111        struct nlattr *attr;
8112        uint16_t chap_out_idx = INVALID_ENTRY;
8113        int rc = QLA_ERROR;
8114        uint32_t rem = len;
8115
8116        memset((void *)&chap_tbl, 0, sizeof(chap_tbl));
8117        nla_for_each_attr(attr, data, len, rem) {
8118                fnode_param = nla_data(attr);
8119
8120                switch (fnode_param->param) {
8121                case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
8122                        fnode_conn->is_fw_assigned_ipv6 = fnode_param->value[0];
8123                        break;
8124                case ISCSI_FLASHNODE_PORTAL_TYPE:
8125                        memcpy(fnode_sess->portal_type, fnode_param->value,
8126                               strlen(fnode_sess->portal_type));
8127                        break;
8128                case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
8129                        fnode_sess->auto_snd_tgt_disable =
8130                                                        fnode_param->value[0];
8131                        break;
8132                case ISCSI_FLASHNODE_DISCOVERY_SESS:
8133                        fnode_sess->discovery_sess = fnode_param->value[0];
8134                        break;
8135                case ISCSI_FLASHNODE_ENTRY_EN:
8136                        fnode_sess->entry_state = fnode_param->value[0];
8137                        break;
8138                case ISCSI_FLASHNODE_HDR_DGST_EN:
8139                        fnode_conn->hdrdgst_en = fnode_param->value[0];
8140                        break;
8141                case ISCSI_FLASHNODE_DATA_DGST_EN:
8142                        fnode_conn->datadgst_en = fnode_param->value[0];
8143                        break;
8144                case ISCSI_FLASHNODE_IMM_DATA_EN:
8145                        fnode_sess->imm_data_en = fnode_param->value[0];
8146                        break;
8147                case ISCSI_FLASHNODE_INITIAL_R2T_EN:
8148                        fnode_sess->initial_r2t_en = fnode_param->value[0];
8149                        break;
8150                case ISCSI_FLASHNODE_DATASEQ_INORDER:
8151                        fnode_sess->dataseq_inorder_en = fnode_param->value[0];
8152                        break;
8153                case ISCSI_FLASHNODE_PDU_INORDER:
8154                        fnode_sess->pdu_inorder_en = fnode_param->value[0];
8155                        break;
8156                case ISCSI_FLASHNODE_CHAP_AUTH_EN:
8157                        fnode_sess->chap_auth_en = fnode_param->value[0];
8158                        /* Invalidate chap index if chap auth is disabled */
8159                        if (!fnode_sess->chap_auth_en)
8160                                fnode_sess->chap_out_idx = INVALID_ENTRY;
8161
8162                        break;
8163                case ISCSI_FLASHNODE_SNACK_REQ_EN:
8164                        fnode_conn->snack_req_en = fnode_param->value[0];
8165                        break;
8166                case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
8167                        fnode_sess->discovery_logout_en = fnode_param->value[0];
8168                        break;
8169                case ISCSI_FLASHNODE_BIDI_CHAP_EN:
8170                        fnode_sess->bidi_chap_en = fnode_param->value[0];
8171                        break;
8172                case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
8173                        fnode_sess->discovery_auth_optional =
8174                                                        fnode_param->value[0];
8175                        break;
8176                case ISCSI_FLASHNODE_ERL:
8177                        fnode_sess->erl = fnode_param->value[0];
8178                        break;
8179                case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
8180                        fnode_conn->tcp_timestamp_stat = fnode_param->value[0];
8181                        break;
8182                case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
8183                        fnode_conn->tcp_nagle_disable = fnode_param->value[0];
8184                        break;
8185                case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
8186                        fnode_conn->tcp_wsf_disable = fnode_param->value[0];
8187                        break;
8188                case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
8189                        fnode_conn->tcp_timer_scale = fnode_param->value[0];
8190                        break;
8191                case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
8192                        fnode_conn->tcp_timestamp_en = fnode_param->value[0];
8193                        break;
8194                case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
8195                        fnode_conn->fragment_disable = fnode_param->value[0];
8196                        break;
8197                case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
8198                        fnode_conn->max_recv_dlength =
8199                                        *(unsigned *)fnode_param->value;
8200                        break;
8201                case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
8202                        fnode_conn->max_xmit_dlength =
8203                                        *(unsigned *)fnode_param->value;
8204                        break;
8205                case ISCSI_FLASHNODE_FIRST_BURST:
8206                        fnode_sess->first_burst =
8207                                        *(unsigned *)fnode_param->value;
8208                        break;
8209                case ISCSI_FLASHNODE_DEF_TIME2WAIT:
8210                        fnode_sess->time2wait = *(uint16_t *)fnode_param->value;
8211                        break;
8212                case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
8213                        fnode_sess->time2retain =
8214                                                *(uint16_t *)fnode_param->value;
8215                        break;
8216                case ISCSI_FLASHNODE_MAX_R2T:
8217                        fnode_sess->max_r2t =
8218                                        *(uint16_t *)fnode_param->value;
8219                        break;
8220                case ISCSI_FLASHNODE_KEEPALIVE_TMO:
8221                        fnode_conn->keepalive_timeout =
8222                                *(uint16_t *)fnode_param->value;
8223                        break;
8224                case ISCSI_FLASHNODE_ISID:
8225                        memcpy(fnode_sess->isid, fnode_param->value,
8226                               sizeof(fnode_sess->isid));
8227                        break;
8228                case ISCSI_FLASHNODE_TSID:
8229                        fnode_sess->tsid = *(uint16_t *)fnode_param->value;
8230                        break;
8231                case ISCSI_FLASHNODE_PORT:
8232                        fnode_conn->port = *(uint16_t *)fnode_param->value;
8233                        break;
8234                case ISCSI_FLASHNODE_MAX_BURST:
8235                        fnode_sess->max_burst = *(unsigned *)fnode_param->value;
8236                        break;
8237                case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
8238                        fnode_sess->default_taskmgmt_timeout =
8239                                                *(uint16_t *)fnode_param->value;
8240                        break;
8241                case ISCSI_FLASHNODE_IPADDR:
8242                        memcpy(fnode_conn->ipaddress, fnode_param->value,
8243                               IPv6_ADDR_LEN);
8244                        break;
8245                case ISCSI_FLASHNODE_ALIAS:
8246                        rc = iscsi_switch_str_param(&fnode_sess->targetalias,
8247                                                    (char *)fnode_param->value);
8248                        break;
8249                case ISCSI_FLASHNODE_REDIRECT_IPADDR:
8250                        memcpy(fnode_conn->redirect_ipaddr, fnode_param->value,
8251                               IPv6_ADDR_LEN);
8252                        break;
8253                case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
8254                        fnode_conn->max_segment_size =
8255                                        *(unsigned *)fnode_param->value;
8256                        break;
8257                case ISCSI_FLASHNODE_LOCAL_PORT:
8258                        fnode_conn->local_port =
8259                                                *(uint16_t *)fnode_param->value;
8260                        break;
8261                case ISCSI_FLASHNODE_IPV4_TOS:
8262                        fnode_conn->ipv4_tos = fnode_param->value[0];
8263                        break;
8264                case ISCSI_FLASHNODE_IPV6_TC:
8265                        fnode_conn->ipv6_traffic_class = fnode_param->value[0];
8266                        break;
8267                case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
8268                        fnode_conn->ipv6_flow_label = fnode_param->value[0];
8269                        break;
8270                case ISCSI_FLASHNODE_NAME:
8271                        rc = iscsi_switch_str_param(&fnode_sess->targetname,
8272                                                    (char *)fnode_param->value);
8273                        break;
8274                case ISCSI_FLASHNODE_TPGT:
8275                        fnode_sess->tpgt = *(uint16_t *)fnode_param->value;
8276                        break;
8277                case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
8278                        memcpy(fnode_conn->link_local_ipv6_addr,
8279                               fnode_param->value, IPv6_ADDR_LEN);
8280                        break;
8281                case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
8282                        fnode_sess->discovery_parent_idx =
8283                                                *(uint16_t *)fnode_param->value;
8284                        break;
8285                case ISCSI_FLASHNODE_TCP_XMIT_WSF:
8286                        fnode_conn->tcp_xmit_wsf =
8287                                                *(uint8_t *)fnode_param->value;
8288                        break;
8289                case ISCSI_FLASHNODE_TCP_RECV_WSF:
8290                        fnode_conn->tcp_recv_wsf =
8291                                                *(uint8_t *)fnode_param->value;
8292                        break;
8293                case ISCSI_FLASHNODE_STATSN:
8294                        fnode_conn->statsn = *(uint32_t *)fnode_param->value;
8295                        break;
8296                case ISCSI_FLASHNODE_EXP_STATSN:
8297                        fnode_conn->exp_statsn =
8298                                                *(uint32_t *)fnode_param->value;
8299                        break;
8300                case ISCSI_FLASHNODE_CHAP_OUT_IDX:
8301                        chap_out_idx = *(uint16_t *)fnode_param->value;
8302                        if (!qla4xxx_get_uni_chap_at_index(ha,
8303                                                           chap_tbl.name,
8304                                                           chap_tbl.secret,
8305                                                           chap_out_idx)) {
8306                                fnode_sess->chap_out_idx = chap_out_idx;
8307                                /* Enable chap auth if chap index is valid */
8308                                fnode_sess->chap_auth_en = QL4_PARAM_ENABLE;
8309                        }
8310                        break;
8311                default:
8312                        ql4_printk(KERN_ERR, ha,
8313                                   "%s: No such sysfs attribute\n", __func__);
8314                        rc = -ENOSYS;
8315                        goto exit_set_param;
8316                }
8317        }
8318
8319        rc = qla4xxx_sysfs_ddb_apply(fnode_sess, fnode_conn);
8320
8321exit_set_param:
8322        return rc;
8323}
8324
8325/**
8326 * qla4xxx_sysfs_ddb_delete - Delete firmware DDB entry
8327 * @fnode_sess: pointer to session attrs of flash ddb entry
8328 *
8329 * This invalidates the flash ddb entry at the given index
8330 **/
8331static int qla4xxx_sysfs_ddb_delete(struct iscsi_bus_flash_session *fnode_sess)
8332{
8333        struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
8334        struct scsi_qla_host *ha = to_qla_host(shost);
8335        uint32_t dev_db_start_offset;
8336        uint32_t dev_db_end_offset;
8337        struct dev_db_entry *fw_ddb_entry = NULL;
8338        dma_addr_t fw_ddb_entry_dma;
8339        uint16_t *ddb_cookie = NULL;
8340        size_t ddb_size = 0;
8341        void *pddb = NULL;
8342        int target_id;
8343        int rc = 0;
8344
8345        if (fnode_sess->is_boot_target) {
8346                rc = -EPERM;
8347                DEBUG2(ql4_printk(KERN_ERR, ha,
8348                                  "%s: Deletion of boot target entry is not permitted.\n",
8349                                  __func__));
8350                goto exit_ddb_del;
8351        }
8352
8353        if (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT)
8354                goto sysfs_ddb_del;
8355
8356        if (is_qla40XX(ha)) {
8357                dev_db_start_offset = FLASH_OFFSET_DB_INFO;
8358                dev_db_end_offset = FLASH_OFFSET_DB_END;
8359                dev_db_start_offset += (fnode_sess->target_id *
8360                                       sizeof(*fw_ddb_entry));
8361                ddb_size = sizeof(*fw_ddb_entry);
8362        } else {
8363                dev_db_start_offset = FLASH_RAW_ACCESS_ADDR +
8364                                      (ha->hw.flt_region_ddb << 2);
8365                /* flt_ddb_size is DDB table size for both ports
8366                 * so divide it by 2 to calculate the offset for second port
8367                 */
8368                if (ha->port_num == 1)
8369                        dev_db_start_offset += (ha->hw.flt_ddb_size / 2);
8370
8371                dev_db_end_offset = dev_db_start_offset +
8372                                    (ha->hw.flt_ddb_size / 2);
8373
8374                dev_db_start_offset += (fnode_sess->target_id *
8375                                       sizeof(*fw_ddb_entry));
8376                dev_db_start_offset += offsetof(struct dev_db_entry, cookie);
8377
8378                ddb_size = sizeof(*ddb_cookie);
8379        }
8380
8381        DEBUG2(ql4_printk(KERN_ERR, ha, "%s: start offset=%u, end offset=%u\n",
8382                          __func__, dev_db_start_offset, dev_db_end_offset));
8383
8384        if (dev_db_start_offset > dev_db_end_offset) {
8385                rc = -EIO;
8386                DEBUG2(ql4_printk(KERN_ERR, ha, "%s:Invalid DDB index %u\n",
8387                                  __func__, fnode_sess->target_id));
8388                goto exit_ddb_del;
8389        }
8390
8391        pddb = dma_alloc_coherent(&ha->pdev->dev, ddb_size,
8392                                  &fw_ddb_entry_dma, GFP_KERNEL);
8393        if (!pddb) {
8394                rc = -ENOMEM;
8395                DEBUG2(ql4_printk(KERN_ERR, ha,
8396                                  "%s: Unable to allocate dma buffer\n",
8397                                  __func__));
8398                goto exit_ddb_del;
8399        }
8400
8401        if (is_qla40XX(ha)) {
8402                fw_ddb_entry = pddb;
8403                memset(fw_ddb_entry, 0, ddb_size);
8404                ddb_cookie = &fw_ddb_entry->cookie;
8405        } else {
8406                ddb_cookie = pddb;
8407        }
8408
8409        /* invalidate the cookie */
8410        *ddb_cookie = 0xFFEE;
8411        qla4xxx_set_flash(ha, fw_ddb_entry_dma, dev_db_start_offset,
8412                          ddb_size, FLASH_OPT_RMW_COMMIT);
8413
8414sysfs_ddb_del:
8415        target_id = fnode_sess->target_id;
8416        iscsi_destroy_flashnode_sess(fnode_sess);
8417        ql4_printk(KERN_INFO, ha,
8418                   "%s: session and conn entries for flashnode %u of host %lu deleted\n",
8419                   __func__, target_id, ha->host_no);
8420exit_ddb_del:
8421        if (pddb)
8422                dma_free_coherent(&ha->pdev->dev, ddb_size, pddb,
8423                                  fw_ddb_entry_dma);
8424        return rc;
8425}
8426
8427/**
8428 * qla4xxx_sysfs_ddb_export - Create sysfs entries for firmware DDBs
8429 * @ha: pointer to adapter structure
8430 *
8431 * Export the firmware DDB for all send targets and normal targets to sysfs.
8432 **/
8433int qla4xxx_sysfs_ddb_export(struct scsi_qla_host *ha)
8434{
8435        struct dev_db_entry *fw_ddb_entry = NULL;
8436        dma_addr_t fw_ddb_entry_dma;
8437        uint16_t max_ddbs;
8438        uint16_t idx = 0;
8439        int ret = QLA_SUCCESS;
8440
8441        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
8442                                          sizeof(*fw_ddb_entry),
8443                                          &fw_ddb_entry_dma, GFP_KERNEL);
8444        if (!fw_ddb_entry) {
8445                DEBUG2(ql4_printk(KERN_ERR, ha,
8446                                  "%s: Unable to allocate dma buffer\n",
8447                                  __func__));
8448                return -ENOMEM;
8449        }
8450
8451        max_ddbs =  is_qla40XX(ha) ? MAX_PRST_DEV_DB_ENTRIES :
8452                                     MAX_DEV_DB_ENTRIES;
8453
8454        for (idx = 0; idx < max_ddbs; idx++) {
8455                if (qla4xxx_flashdb_by_index(ha, fw_ddb_entry, fw_ddb_entry_dma,
8456                                             idx))
8457                        continue;
8458
8459                ret = qla4xxx_sysfs_ddb_tgt_create(ha, fw_ddb_entry, &idx, 0);
8460                if (ret) {
8461                        ret = -EIO;
8462                        break;
8463                }
8464        }
8465
8466        dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
8467                          fw_ddb_entry_dma);
8468
8469        return ret;
8470}
8471
8472static void qla4xxx_sysfs_ddb_remove(struct scsi_qla_host *ha)
8473{
8474        iscsi_destroy_all_flashnode(ha->host);
8475}
8476
8477/**
8478 * qla4xxx_build_ddb_list - Build ddb list and setup sessions
8479 * @ha: pointer to adapter structure
8480 * @is_reset: Is this init path or reset path
8481 *
8482 * Create a list of sendtargets (st) from firmware DDBs, issue send targets
8483 * using connection open, then create the list of normal targets (nt)
8484 * from firmware DDBs. Based on the list of nt setup session and connection
8485 * objects.
8486 **/
8487void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
8488{
8489        uint16_t tmo = 0;
8490        struct list_head list_st, list_nt;
8491        struct qla_ddb_index  *st_ddb_idx, *st_ddb_idx_tmp;
8492        unsigned long wtime;
8493
8494        if (!test_bit(AF_LINK_UP, &ha->flags)) {
8495                set_bit(AF_BUILD_DDB_LIST, &ha->flags);
8496                ha->is_reset = is_reset;
8497                return;
8498        }
8499
8500        INIT_LIST_HEAD(&list_st);
8501        INIT_LIST_HEAD(&list_nt);
8502
8503        qla4xxx_build_st_list(ha, &list_st);
8504
8505        /* Before issuing conn open mbox, ensure all IPs states are configured
8506         * Note, conn open fails if IPs are not configured
8507         */
8508        qla4xxx_wait_for_ip_configuration(ha);
8509
8510        /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
8511        list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
8512                qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
8513        }
8514
8515        /* Wait to ensure all sendtargets are done for min 12 sec wait */
8516        tmo = ((ha->def_timeout > LOGIN_TOV) &&
8517               (ha->def_timeout < LOGIN_TOV * 10) ?
8518               ha->def_timeout : LOGIN_TOV);
8519
8520        DEBUG2(ql4_printk(KERN_INFO, ha,
8521                          "Default time to wait for build ddb %d\n", tmo));
8522
8523        wtime = jiffies + (HZ * tmo);
8524        do {
8525                if (list_empty(&list_st))
8526                        break;
8527
8528                qla4xxx_remove_failed_ddb(ha, &list_st);
8529                schedule_timeout_uninterruptible(HZ / 10);
8530        } while (time_after(wtime, jiffies));
8531
8532
8533        qla4xxx_build_nt_list(ha, &list_nt, &list_st, is_reset);
8534
8535        qla4xxx_free_ddb_list(&list_st);
8536        qla4xxx_free_ddb_list(&list_nt);
8537
8538        qla4xxx_free_ddb_index(ha);
8539}
8540
8541/**
8542 * qla4xxx_wait_login_resp_boot_tgt -  Wait for iSCSI boot target login
8543 * response.
8544 * @ha: pointer to adapter structure
8545 *
8546 * When the boot entry is normal iSCSI target then DF_BOOT_TGT flag will be
8547 * set in DDB and we will wait for login response of boot targets during
8548 * probe.
8549 **/
8550static void qla4xxx_wait_login_resp_boot_tgt(struct scsi_qla_host *ha)
8551{
8552        struct ddb_entry *ddb_entry;
8553        struct dev_db_entry *fw_ddb_entry = NULL;
8554        dma_addr_t fw_ddb_entry_dma;
8555        unsigned long wtime;
8556        uint32_t ddb_state;
8557        int max_ddbs, idx, ret;
8558
8559        max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
8560                                     MAX_DEV_DB_ENTRIES;
8561
8562        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
8563                                          &fw_ddb_entry_dma, GFP_KERNEL);
8564        if (!fw_ddb_entry) {
8565                ql4_printk(KERN_ERR, ha,
8566                           "%s: Unable to allocate dma buffer\n", __func__);
8567                goto exit_login_resp;
8568        }
8569
8570        wtime = jiffies + (HZ * BOOT_LOGIN_RESP_TOV);
8571
8572        for (idx = 0; idx < max_ddbs; idx++) {
8573                ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
8574                if (ddb_entry == NULL)
8575                        continue;
8576
8577                if (test_bit(DF_BOOT_TGT, &ddb_entry->flags)) {
8578                        DEBUG2(ql4_printk(KERN_INFO, ha,
8579                                          "%s: DDB index [%d]\n", __func__,
8580                                          ddb_entry->fw_ddb_index));
8581                        do {
8582                                ret = qla4xxx_get_fwddb_entry(ha,
8583                                                ddb_entry->fw_ddb_index,
8584                                                fw_ddb_entry, fw_ddb_entry_dma,
8585                                                NULL, NULL, &ddb_state, NULL,
8586                                                NULL, NULL);
8587                                if (ret == QLA_ERROR)
8588                                        goto exit_login_resp;
8589
8590                                if ((ddb_state == DDB_DS_SESSION_ACTIVE) ||
8591                                    (ddb_state == DDB_DS_SESSION_FAILED))
8592                                        break;
8593
8594                                schedule_timeout_uninterruptible(HZ);
8595
8596                        } while ((time_after(wtime, jiffies)));
8597
8598                        if (!time_after(wtime, jiffies)) {
8599                                DEBUG2(ql4_printk(KERN_INFO, ha,
8600                                                  "%s: Login response wait timer expired\n",
8601                                                  __func__));
8602                                 goto exit_login_resp;
8603                        }
8604                }
8605        }
8606
8607exit_login_resp:
8608        if (fw_ddb_entry)
8609                dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
8610                                  fw_ddb_entry, fw_ddb_entry_dma);
8611}
8612
8613/**
8614 * qla4xxx_probe_adapter - callback function to probe HBA
8615 * @pdev: pointer to pci_dev structure
8616 * @pci_device_id: pointer to pci_device entry
8617 *
8618 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
8619 * It returns zero if successful. It also initializes all data necessary for
8620 * the driver.
8621 **/
8622static int qla4xxx_probe_adapter(struct pci_dev *pdev,
8623                                 const struct pci_device_id *ent)
8624{
8625        int ret = -ENODEV, status;
8626        struct Scsi_Host *host;
8627        struct scsi_qla_host *ha;
8628        uint8_t init_retry_count = 0;
8629        char buf[34];
8630        struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
8631        uint32_t dev_state;
8632
8633        if (pci_enable_device(pdev))
8634                return -1;
8635
8636        host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
8637        if (host == NULL) {
8638                printk(KERN_WARNING
8639                       "qla4xxx: Couldn't allocate host from scsi layer!\n");
8640                goto probe_disable_device;
8641        }
8642
8643        /* Clear our data area */
8644        ha = to_qla_host(host);
8645        memset(ha, 0, sizeof(*ha));
8646
8647        /* Save the information from PCI BIOS.  */
8648        ha->pdev = pdev;
8649        ha->host = host;
8650        ha->host_no = host->host_no;
8651        ha->func_num = PCI_FUNC(ha->pdev->devfn);
8652
8653        pci_enable_pcie_error_reporting(pdev);
8654
8655        /* Setup Runtime configurable options */
8656        if (is_qla8022(ha)) {
8657                ha->isp_ops = &qla4_82xx_isp_ops;
8658                ha->reg_tbl = (uint32_t *) qla4_82xx_reg_tbl;
8659                ha->qdr_sn_window = -1;
8660                ha->ddr_mn_window = -1;
8661                ha->curr_window = 255;
8662                nx_legacy_intr = &legacy_intr[ha->func_num];
8663                ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
8664                ha->nx_legacy_intr.tgt_status_reg =
8665                        nx_legacy_intr->tgt_status_reg;
8666                ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
8667                ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
8668        } else if (is_qla8032(ha) || is_qla8042(ha)) {
8669                ha->isp_ops = &qla4_83xx_isp_ops;
8670                ha->reg_tbl = (uint32_t *)qla4_83xx_reg_tbl;
8671        } else {
8672                ha->isp_ops = &qla4xxx_isp_ops;
8673        }
8674
8675        if (is_qla80XX(ha)) {
8676                rwlock_init(&ha->hw_lock);
8677                ha->pf_bit = ha->func_num << 16;
8678                /* Set EEH reset type to fundamental if required by hba */
8679                pdev->needs_freset = 1;
8680        }
8681
8682        /* Configure PCI I/O space. */
8683        ret = ha->isp_ops->iospace_config(ha);
8684        if (ret)
8685                goto probe_failed_ioconfig;
8686
8687        ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
8688                   pdev->device, pdev->irq, ha->reg);
8689
8690        qla4xxx_config_dma_addressing(ha);
8691
8692        /* Initialize lists and spinlocks. */
8693        INIT_LIST_HEAD(&ha->free_srb_q);
8694
8695        mutex_init(&ha->mbox_sem);
8696        mutex_init(&ha->chap_sem);
8697        init_completion(&ha->mbx_intr_comp);
8698        init_completion(&ha->disable_acb_comp);
8699        init_completion(&ha->idc_comp);
8700        init_completion(&ha->link_up_comp);
8701        init_completion(&ha->disable_acb_comp);
8702
8703        spin_lock_init(&ha->hardware_lock);
8704        spin_lock_init(&ha->work_lock);
8705
8706        /* Initialize work list */
8707        INIT_LIST_HEAD(&ha->work_list);
8708
8709        /* Allocate dma buffers */
8710        if (qla4xxx_mem_alloc(ha)) {
8711                ql4_printk(KERN_WARNING, ha,
8712                    "[ERROR] Failed to allocate memory for adapter\n");
8713
8714                ret = -ENOMEM;
8715                goto probe_failed;
8716        }
8717
8718        host->cmd_per_lun = 3;
8719        host->max_channel = 0;
8720        host->max_lun = MAX_LUNS - 1;
8721        host->max_id = MAX_TARGETS;
8722        host->max_cmd_len = IOCB_MAX_CDB_LEN;
8723        host->can_queue = MAX_SRBS ;
8724        host->transportt = qla4xxx_scsi_transport;
8725
8726        ret = scsi_init_shared_tag_map(host, MAX_SRBS);
8727        if (ret) {
8728                ql4_printk(KERN_WARNING, ha,
8729                           "%s: scsi_init_shared_tag_map failed\n", __func__);
8730                goto probe_failed;
8731        }
8732
8733        pci_set_drvdata(pdev, ha);
8734
8735        ret = scsi_add_host(host, &pdev->dev);
8736        if (ret)
8737                goto probe_failed;
8738
8739        if (is_qla80XX(ha))
8740                qla4_8xxx_get_flash_info(ha);
8741
8742        if (is_qla8032(ha) || is_qla8042(ha)) {
8743                qla4_83xx_read_reset_template(ha);
8744                /*
8745                 * NOTE: If ql4dontresethba==1, set IDC_CTRL DONTRESET_BIT0.
8746                 * If DONRESET_BIT0 is set, drivers should not set dev_state
8747                 * to NEED_RESET. But if NEED_RESET is set, drivers should
8748                 * should honor the reset.
8749                 */
8750                if (ql4xdontresethba == 1)
8751                        qla4_83xx_set_idc_dontreset(ha);
8752        }
8753
8754        /*
8755         * Initialize the Host adapter request/response queues and
8756         * firmware
8757         * NOTE: interrupts enabled upon successful completion
8758         */
8759        status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
8760
8761        /* Dont retry adapter initialization if IRQ allocation failed */
8762        if (is_qla80XX(ha) && (status == QLA_ERROR))
8763                goto skip_retry_init;
8764
8765        while ((!test_bit(AF_ONLINE, &ha->flags)) &&
8766            init_retry_count++ < MAX_INIT_RETRIES) {
8767
8768                if (is_qla80XX(ha)) {
8769                        ha->isp_ops->idc_lock(ha);
8770                        dev_state = qla4_8xxx_rd_direct(ha,
8771                                                        QLA8XXX_CRB_DEV_STATE);
8772                        ha->isp_ops->idc_unlock(ha);
8773                        if (dev_state == QLA8XXX_DEV_FAILED) {
8774                                ql4_printk(KERN_WARNING, ha, "%s: don't retry "
8775                                    "initialize adapter. H/W is in failed state\n",
8776                                    __func__);
8777                                break;
8778                        }
8779                }
8780                DEBUG2(printk("scsi: %s: retrying adapter initialization "
8781                              "(%d)\n", __func__, init_retry_count));
8782
8783                if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
8784                        continue;
8785
8786                status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
8787                if (is_qla80XX(ha) && (status == QLA_ERROR)) {
8788                        if (qla4_8xxx_check_init_adapter_retry(ha) == QLA_ERROR)
8789                                goto skip_retry_init;
8790                }
8791        }
8792
8793skip_retry_init:
8794        if (!test_bit(AF_ONLINE, &ha->flags)) {
8795                ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
8796
8797                if ((is_qla8022(ha) && ql4xdontresethba) ||
8798                    ((is_qla8032(ha) || is_qla8042(ha)) &&
8799                     qla4_83xx_idc_dontreset(ha))) {
8800                        /* Put the device in failed state. */
8801                        DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
8802                        ha->isp_ops->idc_lock(ha);
8803                        qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
8804                                            QLA8XXX_DEV_FAILED);
8805                        ha->isp_ops->idc_unlock(ha);
8806                }
8807                ret = -ENODEV;
8808                goto remove_host;
8809        }
8810
8811        /* Startup the kernel thread for this host adapter. */
8812        DEBUG2(printk("scsi: %s: Starting kernel thread for "
8813                      "qla4xxx_dpc\n", __func__));
8814        sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
8815        ha->dpc_thread = create_singlethread_workqueue(buf);
8816        if (!ha->dpc_thread) {
8817                ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
8818                ret = -ENODEV;
8819                goto remove_host;
8820        }
8821        INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
8822
8823        ha->task_wq = alloc_workqueue("qla4xxx_%lu_task", WQ_MEM_RECLAIM, 1,
8824                                      ha->host_no);
8825        if (!ha->task_wq) {
8826                ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
8827                ret = -ENODEV;
8828                goto remove_host;
8829        }
8830
8831        /*
8832         * For ISP-8XXX, request_irqs is called in qla4_8xxx_load_risc
8833         * (which is called indirectly by qla4xxx_initialize_adapter),
8834         * so that irqs will be registered after crbinit but before
8835         * mbx_intr_enable.
8836         */
8837        if (is_qla40XX(ha)) {
8838                ret = qla4xxx_request_irqs(ha);
8839                if (ret) {
8840                        ql4_printk(KERN_WARNING, ha, "Failed to reserve "
8841                            "interrupt %d already in use.\n", pdev->irq);
8842                        goto remove_host;
8843                }
8844        }
8845
8846        pci_save_state(ha->pdev);
8847        ha->isp_ops->enable_intrs(ha);
8848
8849        /* Start timer thread. */
8850        qla4xxx_start_timer(ha, qla4xxx_timer, 1);
8851
8852        set_bit(AF_INIT_DONE, &ha->flags);
8853
8854        qla4_8xxx_alloc_sysfs_attr(ha);
8855
8856        printk(KERN_INFO
8857               " QLogic iSCSI HBA Driver version: %s\n"
8858               "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
8859               qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
8860               ha->host_no, ha->fw_info.fw_major, ha->fw_info.fw_minor,
8861               ha->fw_info.fw_patch, ha->fw_info.fw_build);
8862
8863        /* Set the driver version */
8864        if (is_qla80XX(ha))
8865                qla4_8xxx_set_param(ha, SET_DRVR_VERSION);
8866
8867        if (qla4xxx_setup_boot_info(ha))
8868                ql4_printk(KERN_ERR, ha,
8869                           "%s: No iSCSI boot target configured\n", __func__);
8870
8871        set_bit(DPC_SYSFS_DDB_EXPORT, &ha->dpc_flags);
8872        /* Perform the build ddb list and login to each */
8873        qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
8874        iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
8875        qla4xxx_wait_login_resp_boot_tgt(ha);
8876
8877        qla4xxx_create_chap_list(ha);
8878
8879        qla4xxx_create_ifaces(ha);
8880        return 0;
8881
8882remove_host:
8883        scsi_remove_host(ha->host);
8884
8885probe_failed:
8886        qla4xxx_free_adapter(ha);
8887
8888probe_failed_ioconfig:
8889        pci_disable_pcie_error_reporting(pdev);
8890        scsi_host_put(ha->host);
8891
8892probe_disable_device:
8893        pci_disable_device(pdev);
8894
8895        return ret;
8896}
8897
8898/**
8899 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
8900 * @ha: pointer to adapter structure
8901 *
8902 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
8903 * so that the other port will not re-initialize while in the process of
8904 * removing the ha due to driver unload or hba hotplug.
8905 **/
8906static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
8907{
8908        struct scsi_qla_host *other_ha = NULL;
8909        struct pci_dev *other_pdev = NULL;
8910        int fn = ISP4XXX_PCI_FN_2;
8911
8912        /*iscsi function numbers for ISP4xxx is 1 and 3*/
8913        if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
8914                fn = ISP4XXX_PCI_FN_1;
8915
8916        other_pdev =
8917                pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
8918                ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
8919                fn));
8920
8921        /* Get other_ha if other_pdev is valid and state is enable*/
8922        if (other_pdev) {
8923                if (atomic_read(&other_pdev->enable_cnt)) {
8924                        other_ha = pci_get_drvdata(other_pdev);
8925                        if (other_ha) {
8926                                set_bit(AF_HA_REMOVAL, &other_ha->flags);
8927                                DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
8928                                    "Prevent %s reinit\n", __func__,
8929                                    dev_name(&other_ha->pdev->dev)));
8930                        }
8931                }
8932                pci_dev_put(other_pdev);
8933        }
8934}
8935
8936static void qla4xxx_destroy_ddb(struct scsi_qla_host *ha,
8937                struct ddb_entry *ddb_entry)
8938{
8939        struct dev_db_entry *fw_ddb_entry = NULL;
8940        dma_addr_t fw_ddb_entry_dma;
8941        unsigned long wtime;
8942        uint32_t ddb_state;
8943        int options;
8944        int status;
8945
8946        options = LOGOUT_OPTION_CLOSE_SESSION;
8947        if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR) {
8948                ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
8949                goto clear_ddb;
8950        }
8951
8952        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
8953                                          &fw_ddb_entry_dma, GFP_KERNEL);
8954        if (!fw_ddb_entry) {
8955                ql4_printk(KERN_ERR, ha,
8956                           "%s: Unable to allocate dma buffer\n", __func__);
8957                goto clear_ddb;
8958        }
8959
8960        wtime = jiffies + (HZ * LOGOUT_TOV);
8961        do {
8962                status = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
8963                                                 fw_ddb_entry, fw_ddb_entry_dma,
8964                                                 NULL, NULL, &ddb_state, NULL,
8965                                                 NULL, NULL);
8966                if (status == QLA_ERROR)
8967                        goto free_ddb;
8968
8969                if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
8970                    (ddb_state == DDB_DS_SESSION_FAILED))
8971                        goto free_ddb;
8972
8973                schedule_timeout_uninterruptible(HZ);
8974        } while ((time_after(wtime, jiffies)));
8975
8976free_ddb:
8977        dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
8978                          fw_ddb_entry, fw_ddb_entry_dma);
8979
8980clear_ddb:
8981        qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
8982}
8983
8984static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
8985{
8986        struct ddb_entry *ddb_entry;
8987        int idx;
8988
8989        for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
8990
8991                ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
8992                if ((ddb_entry != NULL) &&
8993                    (ddb_entry->ddb_type == FLASH_DDB)) {
8994
8995                        qla4xxx_destroy_ddb(ha, ddb_entry);
8996                        /*
8997                         * we have decremented the reference count of the driver
8998                         * when we setup the session to have the driver unload
8999                         * to be seamless without actually destroying the
9000                         * session
9001                         **/
9002                        try_module_get(qla4xxx_iscsi_transport.owner);
9003                        iscsi_destroy_endpoint(ddb_entry->conn->ep);
9004                        qla4xxx_free_ddb(ha, ddb_entry);
9005                        iscsi_session_teardown(ddb_entry->sess);
9006                }
9007        }
9008}
9009/**
9010 * qla4xxx_remove_adapter - callback function to remove adapter.
9011 * @pci_dev: PCI device pointer
9012 **/
9013static void qla4xxx_remove_adapter(struct pci_dev *pdev)
9014{
9015        struct scsi_qla_host *ha;
9016
9017        /*
9018         * If the PCI device is disabled then it means probe_adapter had
9019         * failed and resources already cleaned up on probe_adapter exit.
9020         */
9021        if (!pci_is_enabled(pdev))
9022                return;
9023
9024        ha = pci_get_drvdata(pdev);
9025
9026        if (is_qla40XX(ha))
9027                qla4xxx_prevent_other_port_reinit(ha);
9028
9029        /* destroy iface from sysfs */
9030        qla4xxx_destroy_ifaces(ha);
9031
9032        if ((!ql4xdisablesysfsboot) && ha->boot_kset)
9033                iscsi_boot_destroy_kset(ha->boot_kset);
9034
9035        qla4xxx_destroy_fw_ddb_session(ha);
9036        qla4_8xxx_free_sysfs_attr(ha);
9037
9038        qla4xxx_sysfs_ddb_remove(ha);
9039        scsi_remove_host(ha->host);
9040
9041        qla4xxx_free_adapter(ha);
9042
9043        scsi_host_put(ha->host);
9044
9045        pci_disable_pcie_error_reporting(pdev);
9046        pci_disable_device(pdev);
9047        pci_set_drvdata(pdev, NULL);
9048}
9049
9050/**
9051 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
9052 * @ha: HA context
9053 *
9054 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
9055 * supported addressing method.
9056 */
9057static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
9058{
9059        int retval;
9060
9061        /* Update our PCI device dma_mask for full 64 bit mask */
9062        if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
9063                if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
9064                        dev_dbg(&ha->pdev->dev,
9065                                  "Failed to set 64 bit PCI consistent mask; "
9066                                   "using 32 bit.\n");
9067                        retval = pci_set_consistent_dma_mask(ha->pdev,
9068                                                             DMA_BIT_MASK(32));
9069                }
9070        } else
9071                retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
9072}
9073
9074static int qla4xxx_slave_alloc(struct scsi_device *sdev)
9075{
9076        struct iscsi_cls_session *cls_sess;
9077        struct iscsi_session *sess;
9078        struct ddb_entry *ddb;
9079        int queue_depth = QL4_DEF_QDEPTH;
9080
9081        cls_sess = starget_to_session(sdev->sdev_target);
9082        sess = cls_sess->dd_data;
9083        ddb = sess->dd_data;
9084
9085        sdev->hostdata = ddb;
9086        sdev->tagged_supported = 1;
9087
9088        if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
9089                queue_depth = ql4xmaxqdepth;
9090
9091        scsi_activate_tcq(sdev, queue_depth);
9092        return 0;
9093}
9094
9095static int qla4xxx_slave_configure(struct scsi_device *sdev)
9096{
9097        sdev->tagged_supported = 1;
9098        return 0;
9099}
9100
9101static void qla4xxx_slave_destroy(struct scsi_device *sdev)
9102{
9103        scsi_deactivate_tcq(sdev, 1);
9104}
9105
9106static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
9107                                      int reason)
9108{
9109        if (!ql4xqfulltracking)
9110                return -EOPNOTSUPP;
9111
9112        return iscsi_change_queue_depth(sdev, qdepth, reason);
9113}
9114
9115/**
9116 * qla4xxx_del_from_active_array - returns an active srb
9117 * @ha: Pointer to host adapter structure.
9118 * @index: index into the active_array
9119 *
9120 * This routine removes and returns the srb at the specified index
9121 **/
9122struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
9123    uint32_t index)
9124{
9125        struct srb *srb = NULL;
9126        struct scsi_cmnd *cmd = NULL;
9127
9128        cmd = scsi_host_find_tag(ha->host, index);
9129        if (!cmd)
9130                return srb;
9131
9132        srb = (struct srb *)CMD_SP(cmd);
9133        if (!srb)
9134                return srb;
9135
9136        /* update counters */
9137        if (srb->flags & SRB_DMA_VALID) {
9138                ha->iocb_cnt -= srb->iocb_cnt;
9139                if (srb->cmd)
9140                        srb->cmd->host_scribble =
9141                                (unsigned char *)(unsigned long) MAX_SRBS;
9142        }
9143        return srb;
9144}
9145
9146/**
9147 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
9148 * @ha: Pointer to host adapter structure.
9149 * @cmd: Scsi Command to wait on.
9150 *
9151 * This routine waits for the command to be returned by the Firmware
9152 * for some max time.
9153 **/
9154static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
9155                                      struct scsi_cmnd *cmd)
9156{
9157        int done = 0;
9158        struct srb *rp;
9159        uint32_t max_wait_time = EH_WAIT_CMD_TOV;
9160        int ret = SUCCESS;
9161
9162        /* Dont wait on command if PCI error is being handled
9163         * by PCI AER driver
9164         */
9165        if (unlikely(pci_channel_offline(ha->pdev)) ||
9166            (test_bit(AF_EEH_BUSY, &ha->flags))) {
9167                ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
9168                    ha->host_no, __func__);
9169                return ret;
9170        }
9171
9172        do {
9173                /* Checking to see if its returned to OS */
9174                rp = (struct srb *) CMD_SP(cmd);
9175                if (rp == NULL) {
9176                        done++;
9177                        break;
9178                }
9179
9180                msleep(2000);
9181        } while (max_wait_time--);
9182
9183        return done;
9184}
9185
9186/**
9187 * qla4xxx_wait_for_hba_online - waits for HBA to come online
9188 * @ha: Pointer to host adapter structure
9189 **/
9190static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
9191{
9192        unsigned long wait_online;
9193
9194        wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
9195        while (time_before(jiffies, wait_online)) {
9196
9197                if (adapter_up(ha))
9198                        return QLA_SUCCESS;
9199
9200                msleep(2000);
9201        }
9202
9203        return QLA_ERROR;
9204}
9205
9206/**
9207 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
9208 * @ha: pointer to HBA
9209 * @t: target id
9210 * @l: lun id
9211 *
9212 * This function waits for all outstanding commands to a lun to complete. It
9213 * returns 0 if all pending commands are returned and 1 otherwise.
9214 **/
9215static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
9216                                        struct scsi_target *stgt,
9217                                        struct scsi_device *sdev)
9218{
9219        int cnt;
9220        int status = 0;
9221        struct scsi_cmnd *cmd;
9222
9223        /*
9224         * Waiting for all commands for the designated target or dev
9225         * in the active array
9226         */
9227        for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
9228                cmd = scsi_host_find_tag(ha->host, cnt);
9229                if (cmd && stgt == scsi_target(cmd->device) &&
9230                    (!sdev || sdev == cmd->device)) {
9231                        if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
9232                                status++;
9233                                break;
9234                        }
9235                }
9236        }
9237        return status;
9238}
9239
9240/**
9241 * qla4xxx_eh_abort - callback for abort task.
9242 * @cmd: Pointer to Linux's SCSI command structure
9243 *
9244 * This routine is called by the Linux OS to abort the specified
9245 * command.
9246 **/
9247static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
9248{
9249        struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
9250        unsigned int id = cmd->device->id;
9251        unsigned int lun = cmd->device->lun;
9252        unsigned long flags;
9253        struct srb *srb = NULL;
9254        int ret = SUCCESS;
9255        int wait = 0;
9256        int rval;
9257
9258        ql4_printk(KERN_INFO, ha, "scsi%ld:%d:%d: Abort command issued cmd=%p, cdb=0x%x\n",
9259                   ha->host_no, id, lun, cmd, cmd->cmnd[0]);
9260
9261        rval = qla4xxx_isp_check_reg(ha);
9262        if (rval != QLA_SUCCESS) {
9263                ql4_printk(KERN_INFO, ha, "PCI/Register disconnect, exiting.\n");
9264                return FAILED;
9265        }
9266
9267        spin_lock_irqsave(&ha->hardware_lock, flags);
9268        srb = (struct srb *) CMD_SP(cmd);
9269        if (!srb) {
9270                spin_unlock_irqrestore(&ha->hardware_lock, flags);
9271                ql4_printk(KERN_INFO, ha, "scsi%ld:%d:%d: Specified command has already completed.\n",
9272                           ha->host_no, id, lun);
9273                return SUCCESS;
9274        }
9275        kref_get(&srb->srb_ref);
9276        spin_unlock_irqrestore(&ha->hardware_lock, flags);
9277
9278        if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
9279                DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
9280                    ha->host_no, id, lun));
9281                ret = FAILED;
9282        } else {
9283                DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
9284                    ha->host_no, id, lun));
9285                wait = 1;
9286        }
9287
9288        kref_put(&srb->srb_ref, qla4xxx_srb_compl);
9289
9290        /* Wait for command to complete */
9291        if (wait) {
9292                if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
9293                        DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
9294                            ha->host_no, id, lun));
9295                        ret = FAILED;
9296                }
9297        }
9298
9299        ql4_printk(KERN_INFO, ha,
9300            "scsi%ld:%d:%d: Abort command - %s\n",
9301            ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
9302
9303        return ret;
9304}
9305
9306/**
9307 * qla4xxx_eh_device_reset - callback for target reset.
9308 * @cmd: Pointer to Linux's SCSI command structure
9309 *
9310 * This routine is called by the Linux OS to reset all luns on the
9311 * specified target.
9312 **/
9313static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
9314{
9315        struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
9316        struct ddb_entry *ddb_entry = cmd->device->hostdata;
9317        int ret = FAILED, stat;
9318        int rval;
9319
9320        if (!ddb_entry)
9321                return ret;
9322
9323        ret = iscsi_block_scsi_eh(cmd);
9324        if (ret)
9325                return ret;
9326        ret = FAILED;
9327
9328        ql4_printk(KERN_INFO, ha,
9329                   "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
9330                   cmd->device->channel, cmd->device->id, cmd->device->lun);
9331
9332        DEBUG2(printk(KERN_INFO
9333                      "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
9334                      "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
9335                      cmd, jiffies, cmd->request->timeout / HZ,
9336                      ha->dpc_flags, cmd->result, cmd->allowed));
9337
9338        rval = qla4xxx_isp_check_reg(ha);
9339        if (rval != QLA_SUCCESS) {
9340                ql4_printk(KERN_INFO, ha, "PCI/Register disconnect, exiting.\n");
9341                return FAILED;
9342        }
9343
9344        /* FIXME: wait for hba to go online */
9345        stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
9346        if (stat != QLA_SUCCESS) {
9347                ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
9348                goto eh_dev_reset_done;
9349        }
9350
9351        if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
9352                                         cmd->device)) {
9353                ql4_printk(KERN_INFO, ha,
9354                           "DEVICE RESET FAILED - waiting for "
9355                           "commands.\n");
9356                goto eh_dev_reset_done;
9357        }
9358
9359        /* Send marker. */
9360        if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
9361                MM_LUN_RESET) != QLA_SUCCESS)
9362                goto eh_dev_reset_done;
9363
9364        ql4_printk(KERN_INFO, ha,
9365                   "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
9366                   ha->host_no, cmd->device->channel, cmd->device->id,
9367                   cmd->device->lun);
9368
9369        ret = SUCCESS;
9370
9371eh_dev_reset_done:
9372
9373        return ret;
9374}
9375
9376/**
9377 * qla4xxx_eh_target_reset - callback for target reset.
9378 * @cmd: Pointer to Linux's SCSI command structure
9379 *
9380 * This routine is called by the Linux OS to reset the target.
9381 **/
9382static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
9383{
9384        struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
9385        struct ddb_entry *ddb_entry = cmd->device->hostdata;
9386        int stat, ret;
9387        int rval;
9388
9389        if (!ddb_entry)
9390                return FAILED;
9391
9392        ret = iscsi_block_scsi_eh(cmd);
9393        if (ret)
9394                return ret;
9395
9396        starget_printk(KERN_INFO, scsi_target(cmd->device),
9397                       "WARM TARGET RESET ISSUED.\n");
9398
9399        DEBUG2(printk(KERN_INFO
9400                      "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
9401                      "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
9402                      ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
9403                      ha->dpc_flags, cmd->result, cmd->allowed));
9404
9405        rval = qla4xxx_isp_check_reg(ha);
9406        if (rval != QLA_SUCCESS) {
9407                ql4_printk(KERN_INFO, ha, "PCI/Register disconnect, exiting.\n");
9408                return FAILED;
9409        }
9410
9411        stat = qla4xxx_reset_target(ha, ddb_entry);
9412        if (stat != QLA_SUCCESS) {
9413                starget_printk(KERN_INFO, scsi_target(cmd->device),
9414                               "WARM TARGET RESET FAILED.\n");
9415                return FAILED;
9416        }
9417
9418        if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
9419                                         NULL)) {
9420                starget_printk(KERN_INFO, scsi_target(cmd->device),
9421                               "WARM TARGET DEVICE RESET FAILED - "
9422                               "waiting for commands.\n");
9423                return FAILED;
9424        }
9425
9426        /* Send marker. */
9427        if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
9428                MM_TGT_WARM_RESET) != QLA_SUCCESS) {
9429                starget_printk(KERN_INFO, scsi_target(cmd->device),
9430                               "WARM TARGET DEVICE RESET FAILED - "
9431                               "marker iocb failed.\n");
9432                return FAILED;
9433        }
9434
9435        starget_printk(KERN_INFO, scsi_target(cmd->device),
9436                       "WARM TARGET RESET SUCCEEDED.\n");
9437        return SUCCESS;
9438}
9439
9440/**
9441 * qla4xxx_is_eh_active - check if error handler is running
9442 * @shost: Pointer to SCSI Host struct
9443 *
9444 * This routine finds that if reset host is called in EH
9445 * scenario or from some application like sg_reset
9446 **/
9447static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
9448{
9449        if (shost->shost_state == SHOST_RECOVERY)
9450                return 1;
9451        return 0;
9452}
9453
9454/**
9455 * qla4xxx_eh_host_reset - kernel callback
9456 * @cmd: Pointer to Linux's SCSI command structure
9457 *
9458 * This routine is invoked by the Linux kernel to perform fatal error
9459 * recovery on the specified adapter.
9460 **/
9461static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
9462{
9463        int return_status = FAILED;
9464        struct scsi_qla_host *ha;
9465        int rval;
9466
9467        ha = to_qla_host(cmd->device->host);
9468
9469        rval = qla4xxx_isp_check_reg(ha);
9470        if (rval != QLA_SUCCESS) {
9471                ql4_printk(KERN_INFO, ha, "PCI/Register disconnect, exiting.\n");
9472                return FAILED;
9473        }
9474
9475        if ((is_qla8032(ha) || is_qla8042(ha)) && ql4xdontresethba)
9476                qla4_83xx_set_idc_dontreset(ha);
9477
9478        /*
9479         * For ISP8324 and ISP8042, if IDC_CTRL DONTRESET_BIT0 is set by other
9480         * protocol drivers, we should not set device_state to NEED_RESET
9481         */
9482        if (ql4xdontresethba ||
9483            ((is_qla8032(ha) || is_qla8042(ha)) &&
9484             qla4_83xx_idc_dontreset(ha))) {
9485                DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
9486                     ha->host_no, __func__));
9487
9488                /* Clear outstanding srb in queues */
9489                if (qla4xxx_is_eh_active(cmd->device->host))
9490                        qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
9491
9492                return FAILED;
9493        }
9494
9495        ql4_printk(KERN_INFO, ha,
9496                   "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
9497                   cmd->device->channel, cmd->device->id, cmd->device->lun);
9498
9499        if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
9500                DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
9501                              "DEAD.\n", ha->host_no, cmd->device->channel,
9502                              __func__));
9503
9504                return FAILED;
9505        }
9506
9507        if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
9508                if (is_qla80XX(ha))
9509                        set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
9510                else
9511                        set_bit(DPC_RESET_HA, &ha->dpc_flags);
9512        }
9513
9514        if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
9515                return_status = SUCCESS;
9516
9517        ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
9518                   return_status == FAILED ? "FAILED" : "SUCCEEDED");
9519
9520        return return_status;
9521}
9522
9523static int qla4xxx_context_reset(struct scsi_qla_host *ha)
9524{
9525        uint32_t mbox_cmd[MBOX_REG_COUNT];
9526        uint32_t mbox_sts[MBOX_REG_COUNT];
9527        struct addr_ctrl_blk_def *acb = NULL;
9528        uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
9529        int rval = QLA_SUCCESS;
9530        dma_addr_t acb_dma;
9531
9532        acb = dma_alloc_coherent(&ha->pdev->dev,
9533                                 sizeof(struct addr_ctrl_blk_def),
9534                                 &acb_dma, GFP_KERNEL);
9535        if (!acb) {
9536                ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
9537                           __func__);
9538                rval = -ENOMEM;
9539                goto exit_port_reset;
9540        }
9541
9542        memset(acb, 0, acb_len);
9543
9544        rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
9545        if (rval != QLA_SUCCESS) {
9546                rval = -EIO;
9547                goto exit_free_acb;
9548        }
9549
9550        rval = qla4xxx_disable_acb(ha);
9551        if (rval != QLA_SUCCESS) {
9552                rval = -EIO;
9553                goto exit_free_acb;
9554        }
9555
9556        wait_for_completion_timeout(&ha->disable_acb_comp,
9557                                    DISABLE_ACB_TOV * HZ);
9558
9559        rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
9560        if (rval != QLA_SUCCESS) {
9561                rval = -EIO;
9562                goto exit_free_acb;
9563        }
9564
9565exit_free_acb:
9566        dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
9567                          acb, acb_dma);
9568exit_port_reset:
9569        DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
9570                          rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
9571        return rval;
9572}
9573
9574static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
9575{
9576        struct scsi_qla_host *ha = to_qla_host(shost);
9577        int rval = QLA_SUCCESS;
9578        uint32_t idc_ctrl;
9579
9580        if (ql4xdontresethba) {
9581                DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
9582                                  __func__));
9583                rval = -EPERM;
9584                goto exit_host_reset;
9585        }
9586
9587        if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
9588                goto recover_adapter;
9589
9590        switch (reset_type) {
9591        case SCSI_ADAPTER_RESET:
9592                set_bit(DPC_RESET_HA, &ha->dpc_flags);
9593                break;
9594        case SCSI_FIRMWARE_RESET:
9595                if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
9596                        if (is_qla80XX(ha))
9597                                /* set firmware context reset */
9598                                set_bit(DPC_RESET_HA_FW_CONTEXT,
9599                                        &ha->dpc_flags);
9600                        else {
9601                                rval = qla4xxx_context_reset(ha);
9602                                goto exit_host_reset;
9603                        }
9604                }
9605                break;
9606        }
9607
9608recover_adapter:
9609        /* For ISP8324 and ISP8042 set graceful reset bit in IDC_DRV_CTRL if
9610         * reset is issued by application */
9611        if ((is_qla8032(ha) || is_qla8042(ha)) &&
9612            test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
9613                idc_ctrl = qla4_83xx_rd_reg(ha, QLA83XX_IDC_DRV_CTRL);
9614                qla4_83xx_wr_reg(ha, QLA83XX_IDC_DRV_CTRL,
9615                                 (idc_ctrl | GRACEFUL_RESET_BIT1));
9616        }
9617
9618        rval = qla4xxx_recover_adapter(ha);
9619        if (rval != QLA_SUCCESS) {
9620                DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
9621                                  __func__));
9622                rval = -EIO;
9623        }
9624
9625exit_host_reset:
9626        return rval;
9627}
9628
9629/* PCI AER driver recovers from all correctable errors w/o
9630 * driver intervention. For uncorrectable errors PCI AER
9631 * driver calls the following device driver's callbacks
9632 *
9633 * - Fatal Errors - link_reset
9634 * - Non-Fatal Errors - driver's pci_error_detected() which
9635 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
9636 *
9637 * PCI AER driver calls
9638 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
9639 *               returns RECOVERED or NEED_RESET if fw_hung
9640 * NEED_RESET - driver's slot_reset()
9641 * DISCONNECT - device is dead & cannot recover
9642 * RECOVERED - driver's pci_resume()
9643 */
9644static pci_ers_result_t
9645qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
9646{
9647        struct scsi_qla_host *ha = pci_get_drvdata(pdev);
9648
9649        ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
9650            ha->host_no, __func__, state);
9651
9652        if (!is_aer_supported(ha))
9653                return PCI_ERS_RESULT_NONE;
9654
9655        switch (state) {
9656        case pci_channel_io_normal:
9657                clear_bit(AF_EEH_BUSY, &ha->flags);
9658                return PCI_ERS_RESULT_CAN_RECOVER;
9659        case pci_channel_io_frozen:
9660                set_bit(AF_EEH_BUSY, &ha->flags);
9661                qla4xxx_mailbox_premature_completion(ha);
9662                qla4xxx_free_irqs(ha);
9663                pci_disable_device(pdev);
9664                /* Return back all IOs */
9665                qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
9666                return PCI_ERS_RESULT_NEED_RESET;
9667        case pci_channel_io_perm_failure:
9668                set_bit(AF_EEH_BUSY, &ha->flags);
9669                set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
9670                qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
9671                return PCI_ERS_RESULT_DISCONNECT;
9672        }
9673        return PCI_ERS_RESULT_NEED_RESET;
9674}
9675
9676/**
9677 * qla4xxx_pci_mmio_enabled() gets called if
9678 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
9679 * and read/write to the device still works.
9680 **/
9681static pci_ers_result_t
9682qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
9683{
9684        struct scsi_qla_host *ha = pci_get_drvdata(pdev);
9685
9686        if (!is_aer_supported(ha))
9687                return PCI_ERS_RESULT_NONE;
9688
9689        return PCI_ERS_RESULT_RECOVERED;
9690}
9691
9692static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
9693{
9694        uint32_t rval = QLA_ERROR;
9695        int fn;
9696        struct pci_dev *other_pdev = NULL;
9697
9698        ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
9699
9700        set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
9701
9702        if (test_bit(AF_ONLINE, &ha->flags)) {
9703                clear_bit(AF_ONLINE, &ha->flags);
9704                clear_bit(AF_LINK_UP, &ha->flags);
9705                iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
9706                qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
9707        }
9708
9709        fn = PCI_FUNC(ha->pdev->devfn);
9710        if (is_qla8022(ha)) {
9711                while (fn > 0) {
9712                        fn--;
9713                        ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at func %x\n",
9714                                   ha->host_no, __func__, fn);
9715                        /* Get the pci device given the domain, bus,
9716                         * slot/function number */
9717                        other_pdev = pci_get_domain_bus_and_slot(
9718                                           pci_domain_nr(ha->pdev->bus),
9719                                           ha->pdev->bus->number,
9720                                           PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
9721                                           fn));
9722
9723                        if (!other_pdev)
9724                                continue;
9725
9726                        if (atomic_read(&other_pdev->enable_cnt)) {
9727                                ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI func in enabled state%x\n",
9728                                           ha->host_no, __func__, fn);
9729                                pci_dev_put(other_pdev);
9730                                break;
9731                        }
9732                        pci_dev_put(other_pdev);
9733                }
9734        } else {
9735                /* this case is meant for ISP83xx/ISP84xx only */
9736                if (qla4_83xx_can_perform_reset(ha)) {
9737                        /* reset fn as iSCSI is going to perform the reset */
9738                        fn = 0;
9739                }
9740        }
9741
9742        /* The first function on the card, the reset owner will
9743         * start & initialize the firmware. The other functions
9744         * on the card will reset the firmware context
9745         */
9746        if (!fn) {
9747                ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
9748                    "0x%x is the owner\n", ha->host_no, __func__,
9749                    ha->pdev->devfn);
9750
9751                ha->isp_ops->idc_lock(ha);
9752                qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
9753                                    QLA8XXX_DEV_COLD);
9754                ha->isp_ops->idc_unlock(ha);
9755
9756                rval = qla4_8xxx_update_idc_reg(ha);
9757                if (rval == QLA_ERROR) {
9758                        ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: FAILED\n",
9759                                   ha->host_no, __func__);
9760                        ha->isp_ops->idc_lock(ha);
9761                        qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
9762                                            QLA8XXX_DEV_FAILED);
9763                        ha->isp_ops->idc_unlock(ha);
9764                        goto exit_error_recovery;
9765                }
9766
9767                clear_bit(AF_FW_RECOVERY, &ha->flags);
9768                rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
9769
9770                if (rval != QLA_SUCCESS) {
9771                        ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
9772                            "FAILED\n", ha->host_no, __func__);
9773                        qla4xxx_free_irqs(ha);
9774                        ha->isp_ops->idc_lock(ha);
9775                        qla4_8xxx_clear_drv_active(ha);
9776                        qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
9777                                            QLA8XXX_DEV_FAILED);
9778                        ha->isp_ops->idc_unlock(ha);
9779                } else {
9780                        ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
9781                            "READY\n", ha->host_no, __func__);
9782                        ha->isp_ops->idc_lock(ha);
9783                        qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
9784                                            QLA8XXX_DEV_READY);
9785                        /* Clear driver state register */
9786                        qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_STATE, 0);
9787                        qla4_8xxx_set_drv_active(ha);
9788                        ha->isp_ops->idc_unlock(ha);
9789                        ha->isp_ops->enable_intrs(ha);
9790                }
9791        } else {
9792                ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
9793                    "the reset owner\n", ha->host_no, __func__,
9794                    ha->pdev->devfn);
9795                if ((qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE) ==
9796                     QLA8XXX_DEV_READY)) {
9797                        clear_bit(AF_FW_RECOVERY, &ha->flags);
9798                        rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
9799                        if (rval == QLA_SUCCESS)
9800                                ha->isp_ops->enable_intrs(ha);
9801                        else
9802                                qla4xxx_free_irqs(ha);
9803
9804                        ha->isp_ops->idc_lock(ha);
9805                        qla4_8xxx_set_drv_active(ha);
9806                        ha->isp_ops->idc_unlock(ha);
9807                }
9808        }
9809exit_error_recovery:
9810        clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
9811        return rval;
9812}
9813
9814static pci_ers_result_t
9815qla4xxx_pci_slot_reset(struct pci_dev *pdev)
9816{
9817        pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
9818        struct scsi_qla_host *ha = pci_get_drvdata(pdev);
9819        int rc;
9820
9821        ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
9822            ha->host_no, __func__);
9823
9824        if (!is_aer_supported(ha))
9825                return PCI_ERS_RESULT_NONE;
9826
9827        /* Restore the saved state of PCIe device -
9828         * BAR registers, PCI Config space, PCIX, MSI,
9829         * IOV states
9830         */
9831        pci_restore_state(pdev);
9832
9833        /* pci_restore_state() clears the saved_state flag of the device
9834         * save restored state which resets saved_state flag
9835         */
9836        pci_save_state(pdev);
9837
9838        /* Initialize device or resume if in suspended state */
9839        rc = pci_enable_device(pdev);
9840        if (rc) {
9841                ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
9842                    "device after reset\n", ha->host_no, __func__);
9843                goto exit_slot_reset;
9844        }
9845
9846        ha->isp_ops->disable_intrs(ha);
9847
9848        if (is_qla80XX(ha)) {
9849                if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
9850                        ret = PCI_ERS_RESULT_RECOVERED;
9851                        goto exit_slot_reset;
9852                } else
9853                        goto exit_slot_reset;
9854        }
9855
9856exit_slot_reset:
9857        ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
9858            "device after reset\n", ha->host_no, __func__, ret);
9859        return ret;
9860}
9861
9862static void
9863qla4xxx_pci_resume(struct pci_dev *pdev)
9864{
9865        struct scsi_qla_host *ha = pci_get_drvdata(pdev);
9866        int ret;
9867
9868        ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
9869            ha->host_no, __func__);
9870
9871        ret = qla4xxx_wait_for_hba_online(ha);
9872        if (ret != QLA_SUCCESS) {
9873                ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
9874                    "resume I/O from slot/link_reset\n", ha->host_no,
9875                     __func__);
9876        }
9877
9878        pci_cleanup_aer_uncorrect_error_status(pdev);
9879        clear_bit(AF_EEH_BUSY, &ha->flags);
9880}
9881
9882static const struct pci_error_handlers qla4xxx_err_handler = {
9883        .error_detected = qla4xxx_pci_error_detected,
9884        .mmio_enabled = qla4xxx_pci_mmio_enabled,
9885        .slot_reset = qla4xxx_pci_slot_reset,
9886        .resume = qla4xxx_pci_resume,
9887};
9888
9889static struct pci_device_id qla4xxx_pci_tbl[] = {
9890        {
9891                .vendor         = PCI_VENDOR_ID_QLOGIC,
9892                .device         = PCI_DEVICE_ID_QLOGIC_ISP4010,
9893                .subvendor      = PCI_ANY_ID,
9894                .subdevice      = PCI_ANY_ID,
9895        },
9896        {
9897                .vendor         = PCI_VENDOR_ID_QLOGIC,
9898                .device         = PCI_DEVICE_ID_QLOGIC_ISP4022,
9899                .subvendor      = PCI_ANY_ID,
9900                .subdevice      = PCI_ANY_ID,
9901        },
9902        {
9903                .vendor         = PCI_VENDOR_ID_QLOGIC,
9904                .device         = PCI_DEVICE_ID_QLOGIC_ISP4032,
9905                .subvendor      = PCI_ANY_ID,
9906                .subdevice      = PCI_ANY_ID,
9907        },
9908        {
9909                .vendor         = PCI_VENDOR_ID_QLOGIC,
9910                .device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
9911                .subvendor      = PCI_ANY_ID,
9912                .subdevice      = PCI_ANY_ID,
9913        },
9914        {
9915                .vendor         = PCI_VENDOR_ID_QLOGIC,
9916                .device         = PCI_DEVICE_ID_QLOGIC_ISP8324,
9917                .subvendor      = PCI_ANY_ID,
9918                .subdevice      = PCI_ANY_ID,
9919        },
9920        {
9921                .vendor         = PCI_VENDOR_ID_QLOGIC,
9922                .device         = PCI_DEVICE_ID_QLOGIC_ISP8042,
9923                .subvendor      = PCI_ANY_ID,
9924                .subdevice      = PCI_ANY_ID,
9925        },
9926        {0, 0},
9927};
9928MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
9929
9930static struct pci_driver qla4xxx_pci_driver = {
9931        .name           = DRIVER_NAME,
9932        .id_table       = qla4xxx_pci_tbl,
9933        .probe          = qla4xxx_probe_adapter,
9934        .remove         = qla4xxx_remove_adapter,
9935        .err_handler = &qla4xxx_err_handler,
9936};
9937
9938static int __init qla4xxx_module_init(void)
9939{
9940        int ret;
9941
9942        /* Allocate cache for SRBs. */
9943        srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
9944                                       SLAB_HWCACHE_ALIGN, NULL);
9945        if (srb_cachep == NULL) {
9946                printk(KERN_ERR
9947                       "%s: Unable to allocate SRB cache..."
9948                       "Failing load!\n", DRIVER_NAME);
9949                ret = -ENOMEM;
9950                goto no_srp_cache;
9951        }
9952
9953        /* Derive version string. */
9954        strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
9955        if (ql4xextended_error_logging)
9956                strcat(qla4xxx_version_str, "-debug");
9957
9958        qla4xxx_scsi_transport =
9959                iscsi_register_transport(&qla4xxx_iscsi_transport);
9960        if (!qla4xxx_scsi_transport){
9961                ret = -ENODEV;
9962                goto release_srb_cache;
9963        }
9964
9965        ret = pci_register_driver(&qla4xxx_pci_driver);
9966        if (ret)
9967                goto unregister_transport;
9968
9969        printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
9970        return 0;
9971
9972unregister_transport:
9973        iscsi_unregister_transport(&qla4xxx_iscsi_transport);
9974release_srb_cache:
9975        kmem_cache_destroy(srb_cachep);
9976no_srp_cache:
9977        return ret;
9978}
9979
9980static void __exit qla4xxx_module_exit(void)
9981{
9982        pci_unregister_driver(&qla4xxx_pci_driver);
9983        iscsi_unregister_transport(&qla4xxx_iscsi_transport);
9984        kmem_cache_destroy(srb_cachep);
9985}
9986
9987module_init(qla4xxx_module_init);
9988module_exit(qla4xxx_module_exit);
9989
9990MODULE_AUTHOR("QLogic Corporation");
9991MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
9992MODULE_LICENSE("GPL");
9993MODULE_VERSION(QLA4XXX_DRIVER_VERSION);
9994