1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2000 by Cisco Systems, Inc.  All rights reserved.
  23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
  25  */
  26 
  27 #ifndef _ISCSI_H
  28 #define _ISCSI_H
  29 
  30 /*
  31  * Block comment which describes the contents of this file.
  32  */
  33 
  34 #ifdef __cplusplus
  35 extern "C" {
  36 #endif
  37 
  38 #include <sys/scsi/scsi.h>
  39 #include <sys/ddi.h>
  40 #include <sys/sunddi.h>
  41 #include <sys/socket.h>
  42 #include <sys/kstat.h>
  43 #include <sys/sunddi.h>
  44 #include <sys/sunmdi.h>
  45 #include <sys/mdi_impldefs.h>
  46 #include <sys/time.h>
  47 #include <sys/nvpair.h>
  48 #include <sys/sdt.h>
  49 
  50 #include <sys/iscsi_protocol.h>
  51 #include <sys/scsi/adapters/iscsi_if.h>
  52 #include <iscsiAuthClient.h>
  53 #include <iscsi_stats.h>
  54 #include <iscsi_thread.h>
  55 #include <sys/idm/idm.h>
  56 #include <sys/idm/idm_conn_sm.h>
  57 #include <nvfile.h>
  58 #include <inet/ip.h>
  59 
  60 #ifndef MIN
  61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
  62 #endif
  63 
  64 #ifndef TRUE
  65 #define TRUE 1
  66 #endif
  67 
  68 #ifndef FALSE
  69 #define FALSE 0
  70 #endif
  71 
  72 #define LOGIN_PDU_BUFFER_SIZE   (16 * 1024)     /* move somewhere else */
  73 
  74 extern boolean_t iscsi_conn_logging;
  75 extern boolean_t iscsi_io_logging;
  76 extern boolean_t iscsi_login_logging;
  77 extern boolean_t iscsi_logging;
  78 extern boolean_t iscsi_sess_logging;
  79 #define ISCSI_CONN_LOG  if (iscsi_conn_logging) cmn_err
  80 #define ISCSI_IO_LOG    if (iscsi_io_logging) cmn_err
  81 #define ISCSI_LOGIN_LOG if (iscsi_login_logging) cmn_err
  82 #define ISCSI_LOG       if (iscsi_logging) cmn_err
  83 #define ISCSI_SESS_LOG  if (iscsi_sess_logging) cmn_err
  84 
  85 /*
  86  * Name Format of the different Task Queues
  87  */
  88 #define ISCSI_SESS_IOTH_NAME_FORMAT             "io_thrd_%d.%d"
  89 #define ISCSI_SESS_WD_NAME_FORMAT               "wd_thrd_%d.%d"
  90 #define ISCSI_SESS_LOGIN_TASKQ_NAME_FORMAT      "login_taskq_%d.%d"
  91 #define ISCSI_SESS_ENUM_TASKQ_NAME_FORMAT       "enum_taskq_%d.%d"
  92 #define ISCSI_CONN_CN_TASKQ_NAME_FORMAT         "conn_cn_taskq_%d.%d.%d"
  93 #define ISCSI_CONN_RXTH_NAME_FORMAT             "rx_thrd_%d.%d.%d"
  94 #define ISCSI_CONN_TXTH_NAME_FORMAT             "tx_thrd_%d.%d.%d"
  95 
  96 /*
  97  * The iSCSI driver will not build scatter/gather lists (iovec) longer
  98  * than the value defined here. Asserts have been include in the code
  99  * to check.
 100  */
 101 #define ISCSI_MAX_IOVEC         5
 102 
 103 #define ISCSI_DEFAULT_MAX_STORM_DELAY           32
 104 
 105 /*
 106  * The SNDBUF and RCVBUF size parameters for the sockets are just a
 107  * guess for the time being (I think it is the values used by CISCO
 108  * or UNH).  Testing will have to be done to figure * out the impact
 109  * of these values on performance.
 110  */
 111 #define ISCSI_SOCKET_SNDBUF_SIZE                (256 * 1024)
 112 #define ISCSI_SOCKET_RCVBUF_SIZE                (256 * 1024)
 113 #define ISCSI_TCP_NODELAY_DEFAULT               0
 114 #define ISCSI_TCP_CNOTIFY_THRESHOLD_DEFAULT     2000
 115 #define ISCSI_TCP_CABORT_THRESHOLD_DEFAULT      10000
 116 #define ISCSI_TCP_ABORT_THRESHOLD_DEFAULT       (30 * 1000) /* milliseconds */
 117 #define ISNS_TCP_ABORT_THRESHOLD_DEFAULT        (3 * 1000) /* milliseconds */
 118 
 119 /* Default values for tunable parameters */
 120 #define ISCSI_DEFAULT_RX_TIMEOUT_VALUE          60
 121 #define ISCSI_DEFAULT_CONN_DEFAULT_LOGIN_MAX    180
 122 #define ISCSI_DEFAULT_LOGIN_POLLING_DELAY       60
 123 
 124 /*
 125  * Convenient short hand defines
 126  */
 127 #define TARGET_PROP     "target"
 128 #define LUN_PROP        "lun"
 129 #define MDI_GUID        "wwn"
 130 #define NDI_GUID        "client-guid"
 131 
 132 #define ISCSI_SIG_CMD   0x11111111
 133 #define ISCSI_SIG_LUN   0x22222222
 134 #define ISCSI_SIG_CONN  0x33333333
 135 #define ISCSI_SIG_SESS  0x44444444
 136 #define ISCSI_SIG_HBA   0x55555555
 137 
 138 #define SENDTARGETS_DISCOVERY   "SENDTARGETS_DISCOVERY"
 139 
 140 #define ISCSI_LUN_MASK_MSB      0x00003f00
 141 #define ISCSI_LUN_MASK_LSB      0x000000ff
 142 #define ISCSI_LUN_MASK          (ISCSI_LUN_MASK_MSB | ISCSI_LUN_MASK_LSB)
 143 #define ISCSI_LUN_BYTE_COPY(lun, report_lun_data) \
 144         lun[0] = (report_lun_data & ISCSI_LUN_MASK_MSB) >> 8; \
 145         lun[1] = (report_lun_data & ISCSI_LUN_MASK_LSB);
 146 /*
 147  * Not defined by iSCSI, but used in the login code to
 148  * determine when to send the initial Login PDU
 149  */
 150 #define ISCSI_INITIAL_LOGIN_STAGE       -1
 151 
 152 typedef enum iscsi_status {
 153         /* Success */
 154         ISCSI_STATUS_SUCCESS = 0,
 155         /* Driver / Kernel / Code error */
 156         ISCSI_STATUS_INTERNAL_ERROR,
 157         /* ITT table is already full, unable to reserve slot */
 158         ISCSI_STATUS_ITT_TABLE_FULL,
 159         /* Login on connection failed */
 160         ISCSI_STATUS_LOGIN_FAILED,
 161         /* No connections are in the LOGGED_IN state */
 162         ISCSI_STATUS_NO_CONN_LOGGED_IN,
 163         /* TCP Transfer Error */
 164         ISCSI_STATUS_TCP_TX_ERROR,
 165         /* TCP Receive Error */
 166         ISCSI_STATUS_TCP_RX_ERROR,
 167         /* iSCSI packet RCV timeout */
 168         ISCSI_STATUS_RX_TIMEOUT,
 169         /* iSCSI Header Digest CRC error */
 170         ISCSI_STATUS_HEADER_DIGEST_ERROR,
 171         /* iSCSI Data Digest CRC error */
 172         ISCSI_STATUS_DATA_DIGEST_ERROR,
 173         /* kmem_alloc failure */
 174         ISCSI_STATUS_ALLOC_FAILURE,
 175         /* cmd (tran_abort/reset) failed */
 176         ISCSI_STATUS_CMD_FAILED,
 177         /* iSCSI protocol error */
 178         ISCSI_STATUS_PROTOCOL_ERROR,
 179         /* iSCSI protocol version mismatch */
 180         ISCSI_STATUS_VERSION_MISMATCH,
 181         /* iSCSI login negotiation failed */
 182         ISCSI_STATUS_NEGO_FAIL,
 183         /* iSCSI login authentication failed */
 184         ISCSI_STATUS_AUTHENTICATION_FAILED,
 185         /* iSCSI login redirection failed */
 186         ISCSI_STATUS_REDIRECTION_FAILED,
 187         /* iSCSI uscsi status failure */
 188         ISCSI_STATUS_USCSI_FAILED,
 189         /* data received would have overflowed given buffer */
 190         ISCSI_STATUS_DATA_OVERFLOW,
 191         /* session/connection needs to shutdown */
 192         ISCSI_STATUS_SHUTDOWN,
 193         /* logical unit in use */
 194         ISCSI_STATUS_BUSY,
 195         /* Login on connection failed, retries exceeded */
 196         ISCSI_STATUS_LOGIN_TIMED_OUT,
 197         /* iSCSI login tpgt negotiation failed */
 198         ISCSI_STATUS_LOGIN_TPGT_NEGO_FAIL
 199 } iscsi_status_t;
 200 #define ISCSI_SUCCESS(status) (status == ISCSI_STATUS_SUCCESS)
 201 
 202 /* SNA32 check value used on increment of CmdSn values */
 203 #define ISCSI_SNA32_CHECK 2147483648UL /* 2**31 */
 204 
 205 /*
 206  * This is the maximum number of commands that can be outstanding
 207  * on a iSCSI session at anyone point in time.
 208  */
 209 #define ISCSI_CMD_TABLE_SIZE            1024
 210 
 211 /* Used on connections thread create of receiver thread */
 212 extern pri_t minclsyspri;
 213 
 214 /*
 215  * Callers of iscsid_config_one/all must hold this
 216  * semaphore across the calls.  Otherwise a ndi_devi_enter()
 217  * deadlock in the DDI layer may occur.
 218  */
 219 extern ksema_t iscsid_config_semaphore;
 220 
 221 extern kmutex_t iscsi_oid_mutex;
 222 extern uint32_t iscsi_oid;
 223 extern void *iscsi_state;
 224 
 225 /*
 226  * NOP delay is used to send a iSCSI NOP (ie. ping) across the
 227  * wire to see if the target is still alive.  NOPs are only
 228  * sent when the RX thread hasn't received anything for the
 229  * below amount of time.
 230  */
 231 #define ISCSI_DEFAULT_NOP_DELAY                 5 /* seconds */
 232 extern int      iscsi_nop_delay;
 233 /*
 234  * If we haven't received anything in a specified period of time
 235  * we will stop accepting IO via tran start.  This will enable
 236  * upper level drivers to see we might be having a problem and
 237  * in the case of scsi_vhci will start to route IO down a better
 238  * path.
 239  */
 240 #define ISCSI_DEFAULT_RX_WINDOW                 20 /* seconds */
 241 extern int      iscsi_rx_window;
 242 /*
 243  * If we haven't received anything in a specified period of time
 244  * we will stop accepting IO via tran start.  This the max limit
 245  * when encountered we will start returning a fatal error.
 246  */
 247 #define ISCSI_DEFAULT_RX_MAX_WINDOW             180 /* seconds */
 248 extern int      iscsi_rx_max_window;
 249 
 250 /*
 251  * During iscsi boot, if the boot session has been created, the
 252  * initiator hasn't changed the boot lun to be online, we will wait
 253  * 180s here for lun online by default.
 254  */
 255 #define ISCSI_BOOT_DEFAULT_MAX_DELAY            180 /* seconds */
 256 /*
 257  * +--------------------------------------------------------------------+
 258  * | iSCSI Driver Structures                                            |
 259  * +--------------------------------------------------------------------+
 260  */
 261 
 262 /*
 263  * iSCSI Auth Information
 264  */
 265 typedef struct iscsi_auth {
 266         IscsiAuthStringBlock    auth_recv_string_block;
 267         IscsiAuthStringBlock    auth_send_string_block;
 268         IscsiAuthLargeBinary    auth_recv_binary_block;
 269         IscsiAuthLargeBinary    auth_send_binary_block;
 270         IscsiAuthClient         auth_client_block;
 271         int                     num_auth_buffers;
 272         IscsiAuthBufferDesc     auth_buffers[5];
 273 
 274         /*
 275          * To indicate if bi-directional authentication is enabled.
 276          * 0 means uni-directional authentication.
 277          * 1 means bi-directional authentication.
 278          */
 279         int                     bidirectional_auth;
 280 
 281         /* Initiator's authentication information. */
 282         char                    username[iscsiAuthStringMaxLength];
 283         uint8_t                 password[iscsiAuthStringMaxLength];
 284         int                     password_length;
 285 
 286         /* Target's authentication information. */
 287         char                    username_in[iscsiAuthStringMaxLength];
 288         uint8_t                 password_in[iscsiAuthStringMaxLength];
 289         int                     password_length_in;
 290 } iscsi_auth_t;
 291 
 292 /*
 293  * iSCSI Task
 294  */
 295 typedef struct iscsi_task {
 296         void                    *t_arg;
 297         boolean_t               t_blocking;
 298         uint32_t                t_event_count;
 299 } iscsi_task_t;
 300 
 301 /*
 302  * These are all the iscsi_cmd types that we use to track our
 303  * commands between queues and actions.
 304  */
 305 typedef enum iscsi_cmd_type {
 306         ISCSI_CMD_TYPE_SCSI = 1,        /* scsi cmd */
 307         ISCSI_CMD_TYPE_NOP,             /* nop / ping */
 308         ISCSI_CMD_TYPE_ABORT,           /* abort */
 309         ISCSI_CMD_TYPE_RESET,           /* reset */
 310         ISCSI_CMD_TYPE_LOGOUT,          /* logout */
 311         ISCSI_CMD_TYPE_LOGIN,           /* login */
 312         ISCSI_CMD_TYPE_TEXT             /* text */
 313 } iscsi_cmd_type_t;
 314 
 315 /*
 316  * iscsi_cmd_state - (reference iscsi_cmd.c for state diagram)
 317  */
 318 typedef enum iscsi_cmd_state {
 319         ISCSI_CMD_STATE_FREE = 0,
 320         ISCSI_CMD_STATE_PENDING,
 321         ISCSI_CMD_STATE_ACTIVE,
 322         ISCSI_CMD_STATE_ABORTING,
 323         ISCSI_CMD_STATE_IDM_ABORTING,
 324         ISCSI_CMD_STATE_COMPLETED,
 325         ISCSI_CMD_STATE_MAX
 326 } iscsi_cmd_state_t;
 327 
 328 #ifdef ISCSI_CMD_SM_STRINGS
 329 static const char *iscsi_cmd_state_names[ISCSI_CMD_STATE_MAX+1] = {
 330         "ISCSI_CMD_STATE_FREE",
 331         "ISCSI_CMD_STATE_PENDING",
 332         "ISCSI_CMD_STATE_ACTIVE",
 333         "ISCSI_CMD_STATE_ABORTING",
 334         "ISCSI_CMD_STATE_IDM_ABORTING",
 335         "ISCSI_CMD_STATE_COMPLETED",
 336         "ISCSI_CMD_STATE_MAX"
 337 };
 338 #endif
 339 
 340 /*
 341  * iscsi command events
 342  */
 343 typedef enum iscsi_cmd_event {
 344         ISCSI_CMD_EVENT_E1 = 0,
 345         ISCSI_CMD_EVENT_E2,
 346         ISCSI_CMD_EVENT_E3,
 347         ISCSI_CMD_EVENT_E4,
 348         ISCSI_CMD_EVENT_E6,
 349         ISCSI_CMD_EVENT_E7,
 350         ISCSI_CMD_EVENT_E8,
 351         ISCSI_CMD_EVENT_E9,
 352         ISCSI_CMD_EVENT_E10,
 353         ISCSI_CMD_EVENT_MAX
 354 } iscsi_cmd_event_t;
 355 
 356 #ifdef ISCSI_CMD_SM_STRINGS
 357 static const char *iscsi_cmd_event_names[ISCSI_CMD_EVENT_MAX+1] = {
 358         "ISCSI_CMD_EVENT_E1",
 359         "ISCSI_CMD_EVENT_E2",
 360         "ISCSI_CMD_EVENT_E3",
 361         "ISCSI_CMD_EVENT_E4",
 362         "ISCSI_CMD_EVENT_E6",
 363         "ISCSI_CMD_EVENT_E7",
 364         "ISCSI_CMD_EVENT_E8",
 365         "ISCSI_CMD_EVENT_E9",
 366         "ISCSI_CMD_EVENT_E10",
 367         "ISCSI_CMD_EVENT_MAX"
 368 };
 369 #endif
 370 
 371 /*
 372  * iscsi text command stages - these stages are used by iSCSI text
 373  * processing to manage long resonses.
 374  */
 375 typedef enum iscsi_cmd_text_stage {
 376         ISCSI_CMD_TEXT_INITIAL_REQ = 0,
 377         ISCSI_CMD_TEXT_CONTINUATION,
 378         ISCSI_CMD_TEXT_FINAL_RSP
 379 } iscsi_cmd_text_stage_t;
 380 
 381 /*
 382  * iscsi cmd misc flags - bitwise applicable
 383  */
 384 #define ISCSI_CMD_MISCFLAG_INTERNAL     0x1
 385 #define ISCSI_CMD_MISCFLAG_FREE         0x2
 386 #define ISCSI_CMD_MISCFLAG_STUCK        0x4
 387 #define ISCSI_CMD_MISCFLAG_XARQ         0x8
 388 #define ISCSI_CMD_MISCFLAG_SENT         0x10
 389 #define ISCSI_CMD_MISCFLAG_FLUSH        0x20
 390 
 391 /*
 392  * 1/2 of a 32 bit number, used for checking CmdSN
 393  * wrapped.
 394  */
 395 #define ISCSI_CMD_SN_WRAP               0x80000000
 396 
 397 #define ISCSI_CMD_PKT_STAT_INIT         0
 398 
 399 /*
 400  * iSCSI cmd/pkt Structure
 401  */
 402 typedef struct iscsi_cmd {
 403         uint32_t                cmd_sig;
 404         struct iscsi_cmd        *cmd_prev;
 405         struct iscsi_cmd        *cmd_next;
 406         struct iscsi_conn       *cmd_conn;
 407 
 408         iscsi_cmd_type_t        cmd_type;
 409         iscsi_cmd_state_t       cmd_state;
 410         iscsi_cmd_state_t       cmd_prev_state;
 411         clock_t                 cmd_lbolt_pending;
 412         clock_t                 cmd_lbolt_active;
 413         clock_t                 cmd_lbolt_aborting;
 414         clock_t                 cmd_lbolt_idm_aborting;
 415         clock_t                 cmd_lbolt_timeout;
 416         uint8_t                 cmd_misc_flags;
 417         idm_task_t              *cmd_itp;
 418 
 419         union {
 420                 /* ISCSI_CMD_TYPE_SCSI */
 421                 struct {
 422                         idm_buf_t               *ibp_ibuf;
 423                         idm_buf_t               *ibp_obuf;
 424                         struct scsi_pkt         *pkt;
 425                         struct buf              *bp;
 426                         int                     cmdlen;
 427                         int                     statuslen;
 428                         size_t                  data_transferred;
 429 
 430                         uint32_t                lun;
 431 
 432                         /*
 433                          * If SCSI_CMD_TYPE is in ABORTING_STATE
 434                          * then the abort_icmdp field will be a pointer
 435                          * to the abort command chasing this one.
 436                          */
 437                         struct iscsi_cmd        *abort_icmdp;
 438                         /*
 439                          * pointer to the r2t associated with this
 440                          * command (if any)
 441                          */
 442                         struct iscsi_cmd        *r2t_icmdp;
 443                         /*
 444                          * It will be true if this command has
 445                          * another R2T to handle.
 446                          */
 447                         boolean_t               r2t_more;
 448                         /*
 449                          * It is used to record pkt_statistics temporarily.
 450                          */
 451                         uint_t                  pkt_stat;
 452                 } scsi;
 453                 /* ISCSI_CMD_TYPE_ABORT */
 454                 struct {
 455                         /* pointer to original iscsi_cmd, for abort */
 456                         struct iscsi_cmd        *icmdp;
 457                 } abort;
 458                 /* ISCSI_CMD_TYPE_RESET */
 459                 struct {
 460                         int                     level;
 461                         uint8_t                 response;
 462                 } reset;
 463                 /* ISCSI_CMD_TYPE_NOP */
 464                 struct {
 465                         int rsvd;
 466                 } nop;
 467                 /* ISCSI_CMD_TYPE_R2T */
 468                 struct {
 469                         struct iscsi_cmd        *icmdp;
 470                         uint32_t                offset;
 471                         uint32_t                length;
 472                 } r2t;
 473                 /* ISCSI_CMD_TYPE_LOGIN */
 474                 struct {
 475                         int rvsd;
 476                 } login;
 477                 /* ISCSI_CMD_TYPE_LOGOUT */
 478                 struct {
 479                         int rsvd;
 480                 } logout;
 481                 /* ISCSI_CMD_TYPE_TEXT */
 482                 struct {
 483                         char                    *buf;
 484                         int                     buf_len;
 485                         uint32_t                offset;
 486                         uint32_t                data_len;
 487                         uint32_t                total_rx_len;
 488                         uint32_t                ttt;
 489                         uint8_t                 lun[8];
 490                         iscsi_cmd_text_stage_t  stage;
 491                 } text;
 492         } cmd_un;
 493 
 494         struct iscsi_lun        *cmd_lun; /* associated lun */
 495 
 496         uint32_t                cmd_itt;
 497         uint32_t                cmd_ttt;
 498 
 499         /*
 500          * If a data digest error is seem on a data pdu.  This flag
 501          * will get set.  We don't abort the cmd immediately because
 502          * we want to read in all the data to get it out of the
 503          * stream.  Once the completion for the cmd is received we
 504          * we will abort the cmd and state no sense data was available.
 505          */
 506         boolean_t               cmd_crc_error_seen;
 507 
 508         /*
 509          * Used to block and wake up caller until action is completed.
 510          * This is for ABORT, RESET, and PASSTHRU cmds.
 511          */
 512         int                     cmd_result;
 513         int                     cmd_completed;
 514         kmutex_t                cmd_mutex;
 515         kcondvar_t              cmd_completion;
 516 
 517         idm_pdu_t               cmd_pdu;
 518 
 519         sm_audit_buf_t          cmd_state_audit;
 520 
 521         uint32_t                cmd_sn;
 522 } iscsi_cmd_t;
 523 
 524 
 525 /*
 526  * iSCSI LUN Structure
 527  */
 528 typedef struct iscsi_lun {
 529         uint32_t                lun_sig;
 530         int                     lun_state;
 531 
 532         struct iscsi_lun        *lun_next;      /* next lun on this sess. */
 533         struct iscsi_sess       *lun_sess;      /* parent sess. for lun */
 534         dev_info_t              *lun_dip;
 535         mdi_pathinfo_t          *lun_pip;
 536 
 537         uint16_t                lun_num;        /* LUN */
 538         uint8_t                 lun_addr_type;  /* LUN addressing type */
 539         uint32_t                lun_oid;        /* OID */
 540         char                    *lun_guid;      /* GUID */
 541         int                     lun_guid_size;  /* GUID allocation size */
 542         char                    *lun_addr;      /* sess,lun */
 543         time_t                  lun_time_online;
 544 
 545         uchar_t                 lun_cap;        /* bitmap of scsi caps */
 546 
 547         uchar_t                 lun_vid[ISCSI_INQ_VID_BUF_LEN]; /* Vendor ID */
 548         uchar_t                 lun_pid[ISCSI_INQ_PID_BUF_LEN]; /* Product ID */
 549 
 550         uchar_t                 lun_type;
 551 } iscsi_lun_t;
 552 
 553 #define ISCSI_LUN_STATE_CLEAR       0           /* used to clear all states */
 554 #define ISCSI_LUN_STATE_OFFLINE     1
 555 #define ISCSI_LUN_STATE_ONLINE      2
 556 #define ISCSI_LUN_STATE_INVALID     4           /* offline failed */
 557 #define ISCSI_LUN_STATE_BUSY        8           /* logic unit is in reset */
 558 
 559 #define ISCSI_LUN_CAP_RESET         0x01
 560 
 561 #define ISCSI_SCSI_RESET_SENSE_CODE 0x29
 562 #define ISCSI_SCSI_LUNCHANGED_CODE      0x3f
 563 
 564 #define ISCSI_SCSI_LUNCHANGED_ASCQ      0x0e
 565 
 566 /*
 567  *
 568  *
 569  */
 570 typedef struct iscsi_queue {
 571         iscsi_cmd_t     *head;
 572         iscsi_cmd_t     *tail;
 573         int             count;
 574         kmutex_t        mutex;
 575 } iscsi_queue_t;
 576 
 577 #define ISCSI_CONN_DEFAULT_LOGIN_MIN            0
 578 #define ISCSI_CONN_DEFAULT_LOGIN_REDIRECT       10
 579 
 580 /* iSCSI tunable Parameters */
 581 typedef struct iscsi_tunable_params {
 582         int             recv_login_rsp_timeout; /* range: 0 - 60*60 */
 583         int             conn_login_max;         /* range: 0 - 60*60 */
 584         int             polling_login_delay;    /* range: 0 - 60*60 */
 585 } iscsi_tunable_params_t;
 586 
 587 typedef union iscsi_sockaddr {
 588         struct sockaddr         sin;
 589         struct sockaddr_in      sin4;
 590         struct sockaddr_in6     sin6;
 591 } iscsi_sockaddr_t;
 592 
 593 #define SIZEOF_SOCKADDR(so)     ((so)->sa_family == AF_INET ? \
 594         sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6))
 595 
 596 typedef enum {
 597         LOGIN_START,
 598         LOGIN_READY,
 599         LOGIN_TX,
 600         LOGIN_RX,
 601         LOGIN_ERROR,
 602         LOGIN_DONE,
 603         LOGIN_FFP,
 604         LOGIN_MAX
 605 } iscsi_login_state_t;
 606 
 607 #ifdef ISCSI_LOGIN_STATE_NAMES
 608 static const char *iscsi_login_state_names[LOGIN_MAX+1] = {
 609         "LOGIN_START",
 610         "LOGIN_READY",
 611         "LOGIN_TX",
 612         "LOGIN_RX",
 613         "LOGIN_ERROR",
 614         "LOGIN_DONE",
 615         "LOGIN_FFP",
 616         "LOGIN_MAX"
 617 };
 618 #endif
 619 
 620 /*
 621  * iscsi_conn_state
 622  */
 623 typedef enum iscsi_conn_state {
 624         ISCSI_CONN_STATE_UNDEFINED = 0,
 625         ISCSI_CONN_STATE_FREE,
 626         ISCSI_CONN_STATE_IN_LOGIN,
 627         ISCSI_CONN_STATE_LOGGED_IN,
 628         ISCSI_CONN_STATE_IN_LOGOUT,
 629         ISCSI_CONN_STATE_FAILED,
 630         ISCSI_CONN_STATE_POLLING,
 631         ISCSI_CONN_STATE_MAX
 632 } iscsi_conn_state_t;
 633 
 634 #ifdef ISCSI_ICS_NAMES
 635 static const char *iscsi_ics_name[ISCSI_CONN_STATE_MAX+1] = {
 636         "ISCSI_CONN_STATE_UNDEFINED",
 637         "ISCSI_CONN_STATE_FREE",
 638         "ISCSI_CONN_STATE_IN_LOGIN",
 639         "ISCSI_CONN_STATE_LOGGED_IN",
 640         "ISCSI_CONN_STATE_IN_LOGOUT",
 641         "ISCSI_CONN_STATE_FAILED",
 642         "ISCSI_CONN_STATE_POLLING",
 643         "ISCSI_CONN_STATE_MAX"
 644 };
 645 #endif
 646 
 647 #define ISCSI_CONN_STATE_FULL_FEATURE(state) \
 648         ((state == ISCSI_CONN_STATE_LOGGED_IN) || \
 649         (state == ISCSI_CONN_STATE_IN_LOGOUT))
 650 
 651 /*
 652  * iSCSI Connection Structure
 653  */
 654 typedef struct iscsi_conn {
 655         uint32_t                conn_sig;
 656         struct iscsi_conn       *conn_next;     /* next conn on this sess. */
 657         struct iscsi_sess       *conn_sess;     /* parent sess. for conn. */
 658 
 659         iscsi_conn_state_t      conn_state;     /* cur. conn. driver state */
 660         iscsi_conn_state_t      conn_prev_state; /* prev. conn. driver state */
 661         /* protects the session state and synchronizes the state machine */
 662         kmutex_t                conn_state_mutex;
 663         kcondvar_t              conn_state_change;
 664         boolean_t               conn_state_destroy;
 665         boolean_t               conn_state_ffp;
 666         boolean_t               conn_state_idm_connected;
 667         boolean_t               conn_async_logout;
 668         ddi_taskq_t             *conn_cn_taskq;
 669 
 670         idm_conn_t              *conn_ic;
 671 
 672         /* base connection information, may have been redirected */
 673         iscsi_sockaddr_t        conn_base_addr;
 674 
 675         /* current connection information, may have been redirected */
 676         iscsi_sockaddr_t        conn_curr_addr;
 677 
 678         boolean_t               conn_bound;
 679         iscsi_sockaddr_t        conn_bound_addr;
 680 
 681         uint32_t                conn_cid;       /* CID */
 682         uint32_t                conn_oid;       /* OID */
 683 
 684         int                     conn_current_stage;     /* iSCSI login stage */
 685         int                     conn_next_stage;        /* iSCSI login stage */
 686         int                     conn_partial_response;
 687 
 688         /*
 689          * The active queue contains iscsi_cmds that have already
 690          * been sent on this connection.  Any future responses to
 691          * these cmds require alligence to this connection.  If there
 692          * are issues with these cmds the command may need aborted
 693          * depending on the command type, and must be put back into
 694          * the session's pending queue or aborted.
 695          */
 696         iscsi_queue_t           conn_queue_active;
 697         iscsi_queue_t           conn_queue_idm_aborting;
 698 
 699         /* lbolt from the last receive, used for nop processing */
 700         clock_t                 conn_rx_lbolt;
 701         clock_t                 conn_nop_lbolt;
 702 
 703         iscsi_thread_t          *conn_tx_thread;
 704 
 705         /*
 706          * The expstatsn is the command status sn that is expected
 707          * next from the target.  Command status is carried on a number
 708          * of iSCSI PDUs (ex.  SCSI Cmd Response, SCSI Data IN with
 709          * S-Bit set, ...), not all PDUs.  If our expstatsn is different
 710          * than the received statsn.  Something got out of sync we need to
 711          * recover.
 712          */
 713         uint32_t                conn_expstatsn;
 714         uint32_t                conn_laststatsn;
 715 
 716         /* active login parameters */
 717         iscsi_login_params_t    conn_params;
 718 
 719         /* Statistics */
 720         struct {
 721                 kstat_t                 *ks;
 722                 iscsi_conn_stats_t      ks_data;
 723         } stats;
 724 
 725         /*
 726          * These fields are used to coordinate the asynchronous IDM
 727          * PDU operations with the synchronous login code.
 728          */
 729         kmutex_t                conn_login_mutex;
 730         kcondvar_t              conn_login_cv;
 731         iscsi_login_state_t     conn_login_state;
 732         iscsi_status_t          conn_login_status;
 733         iscsi_hdr_t             conn_login_resp_hdr;
 734         char                    *conn_login_data;
 735         int                     conn_login_datalen;
 736         int                     conn_login_max_data_length;
 737 
 738         /*
 739          * login min and max identify the amount of time
 740          * in lbolt that iscsi_start_login() should attempt
 741          * to log into a target portal.  The login will
 742          * delay until the min lbolt has been reached and
 743          * will end once max time has been reached.  These
 744          * values are normally set to the default but can
 745          * are also altered by async commands received from
 746          * the targetlogin.
 747          */
 748         clock_t                 conn_login_min;
 749         clock_t                 conn_login_max;
 750         sm_audit_buf_t          conn_state_audit;
 751 
 752         /* active tunable parameters */
 753         iscsi_tunable_params_t  conn_tunable_params;
 754         boolean_t               conn_timeout;
 755 } iscsi_conn_t;
 756 
 757 
 758 /*
 759  * iscsi_sess_state - (reference iscsi_sess.c for state diagram)
 760  */
 761 typedef enum iscsi_sess_state {
 762         ISCSI_SESS_STATE_FREE = 0,
 763         ISCSI_SESS_STATE_LOGGED_IN,
 764         ISCSI_SESS_STATE_FAILED,
 765         ISCSI_SESS_STATE_IN_FLUSH,
 766         ISCSI_SESS_STATE_FLUSHED,
 767         ISCSI_SESS_STATE_MAX
 768 } iscsi_sess_state_t;
 769 
 770 #ifdef ISCSI_SESS_SM_STRINGS
 771 static const char *iscsi_sess_state_names[ISCSI_SESS_STATE_MAX+1] = {
 772         "ISCSI_SESS_STATE_FREE",
 773         "ISCSI_SESS_STATE_LOGGED_IN",
 774         "ISCSI_SESS_STATE_FAILED",
 775         "ISCSI_SESS_STATE_IN_FLUSH",
 776         "ISCSI_SESS_STATE_FLUSHED",
 777         "ISCSI_SESS_STATE_MAX"
 778 };
 779 #endif
 780 
 781 #define ISCSI_SESS_STATE_FULL_FEATURE(state) \
 782         ((state == ISCSI_SESS_STATE_LOGGED_IN) || \
 783         (state == ISCSI_SESS_STATE_IN_FLUSH))
 784 
 785 
 786 typedef enum iscsi_sess_event {
 787         ISCSI_SESS_EVENT_N1 = 0,
 788         ISCSI_SESS_EVENT_N3,
 789         ISCSI_SESS_EVENT_N5,
 790         ISCSI_SESS_EVENT_N6,
 791         ISCSI_SESS_EVENT_N7,
 792         ISCSI_SESS_EVENT_MAX
 793 } iscsi_sess_event_t;
 794 
 795 #ifdef ISCSI_SESS_SM_STRINGS
 796 static const char *iscsi_sess_event_names[ISCSI_SESS_EVENT_MAX+1] = {
 797         "ISCSI_SESS_EVENT_N1",
 798         "ISCSI_SESS_EVENT_N3",
 799         "ISCSI_SESS_EVENT_N5",
 800         "ISCSI_SESS_EVENT_N6",
 801         "ISCSI_SESS_EVENT_N7",
 802         "ISCSI_SESS_EVENT_MAX"
 803 };
 804 #endif
 805 
 806 typedef enum iscsi_sess_type {
 807         ISCSI_SESS_TYPE_NORMAL = 0,
 808         ISCSI_SESS_TYPE_DISCOVERY
 809 } iscsi_sess_type_t;
 810 
 811 #define SESS_ABORT_TASK_MAX_THREADS     1
 812 
 813 /* Sun's initiator session ID */
 814 #define ISCSI_SUN_ISID_0    0x40    /* ISID - EN format */
 815 #define ISCSI_SUN_ISID_1    0x00    /* Sec B */
 816 #define ISCSI_SUN_ISID_2    0x00    /* Sec B */
 817 #define ISCSI_SUN_ISID_3    0x2A    /* Sec C - 42 = Sun's EN */
 818 /*
 819  * defines 4-5 are the reserved values.  These reserved values
 820  * are used as the ISID for an initiator-port in MP-API and used
 821  * for the send targets discovery sessions.  Byte 5 is overridden
 822  * for full feature sessions.  The default values of byte 5 for a
 823  * full feature session is 0.  When MS/T is enabled with more than
 824  * one session this byte 5 will increment > 0 up to
 825  * ISCSI_MAX_CONFIG_SESSIONS.
 826  */
 827 #define ISCSI_SUN_ISID_4    0x00
 828 #define ISCSI_SUN_ISID_5    0xFF
 829 
 830 #define ISCSI_DEFAULT_SESS_BOUND        B_FALSE
 831 #define ISCSI_DEFAULT_SESS_NUM          1
 832 
 833 typedef enum iscsi_enum_status {
 834         ISCSI_SESS_ENUM_FREE            =       0,
 835         ISCSI_SESS_ENUM_INPROG,
 836         ISCSI_SESS_ENUM_DONE
 837 } iscsi_enum_status_t;
 838 
 839 typedef enum iscsi_enum_result {
 840         ISCSI_SESS_ENUM_COMPLETE        =       0,
 841         ISCSI_SESS_ENUM_PARTIAL,
 842         ISCSI_SESS_ENUM_IOFAIL,
 843         ISCSI_SESS_ENUM_SUBMITTED,
 844         ISCSI_SESS_ENUM_SUBFAIL,
 845         ISCSI_SESS_ENUM_GONE,
 846         ISCSI_SESS_ENUM_TUR_FAIL
 847 } iscsi_enum_result_t;
 848 
 849 /*
 850  * iSCSI Session(Target) Structure
 851  */
 852 typedef struct iscsi_sess {
 853         uint32_t                sess_sig;
 854 
 855         iscsi_sess_state_t      sess_state;
 856         iscsi_sess_state_t      sess_prev_state;
 857         clock_t                 sess_state_lbolt;
 858         /* protects the session state and synchronizes the state machine */
 859         krwlock_t               sess_state_rwlock;
 860 
 861         /*
 862          * Associated target OID.
 863          */
 864         uint32_t                sess_target_oid;
 865 
 866         /*
 867          * Session OID.  Used by IMA, interfaces and exported as
 868          * TARGET_PROP which is checked by the NDI.  In addition
 869          * this is used in our tran_lun_init function.
 870          */
 871         uint32_t                sess_oid;
 872 
 873         struct iscsi_sess       *sess_next;
 874         struct iscsi_hba        *sess_hba;
 875 
 876         /* list of all luns relating to session */
 877         struct iscsi_lun        *sess_lun_list;
 878         krwlock_t               sess_lun_list_rwlock;
 879 
 880         /* list of all connections relating to session */
 881         struct iscsi_conn       *sess_conn_list;
 882         struct iscsi_conn       *sess_conn_list_last_ptr;
 883         /* pointer to active connection in session */
 884         struct iscsi_conn       *sess_conn_act;
 885         krwlock_t               sess_conn_list_rwlock;
 886 
 887         /* Connection ID for next connection to be added to session */
 888         uint32_t                sess_conn_next_cid;
 889 
 890         /*
 891          * last time any connection on this session received
 892          * data from the target.
 893          */
 894         clock_t                 sess_rx_lbolt;
 895 
 896         clock_t                 sess_failure_lbolt;
 897 
 898         int                     sess_storm_delay;
 899 
 900         /*
 901          * sess_cmdsn_mutex protects the cmdsn and itt table/values
 902          * Cmdsn isn't that big of a problem yet since we only have
 903          * one connection but in the future we will need to ensure
 904          * this locking is working so keep the sequence numbers in
 905          * sync on the wire.
 906          *
 907          * We also use this lock to protect the ITT table and it's
 908          * values.  We need to make sure someone doesn't assign
 909          * a duplicate ITT value or cell to a command.  Also we
 910          * need to make sure when someone is looking up an ITT
 911          * that the command is still in that correct queue location.
 912          */
 913         kmutex_t                sess_cmdsn_mutex;
 914 
 915         /*
 916          * iSCSI command sequencing / windowing.  The next
 917          * command to be sent via the pending queue will
 918          * get the sess_cmdsn.  If the maxcmdsn is less
 919          * than the next cmdsn then the iSCSI window is
 920          * closed and this command cannot be sent yet.
 921          * Most iscsi cmd responses from the target carry
 922          * a new maxcmdsn.  If this new maxcmdsn is greater
 923          * than the sess_maxcmdsn we will update it's value
 924          * and set a timer to fire in one tick and reprocess
 925          * the pending queue.
 926          *
 927          * The expcmdsn.   Is the value the target expects
 928          * to be sent for my next cmdsn.  If the expcmdsn
 929          * and the cmdsn get out of sync this could denote
 930          * a communication problem.
 931          */
 932         uint32_t                sess_cmdsn;
 933         uint32_t                sess_expcmdsn;
 934         uint32_t                sess_maxcmdsn;
 935 
 936         /* Next Initiator Task Tag (ITT) to use */
 937         uint32_t                sess_itt;
 938         /*
 939          * The session iscsi_cmd table is used to a fast performance
 940          * lookup of an ITT to a iscsi_cmd when we receive an iSCSI
 941          * PDU from the wire.  To reserve a location in the sess_cmd_table
 942          * we try the sess_itt % ISCSI_CMD_TABLE_SIZE if this cmd table
 943          * cell is already full.  Then increament the sess_itt and
 944          * try to get the cell position again, repeat until an empty
 945          * cell is found.  Once an empty cell is found place your
 946          * scsi_cmd point into the cell to reserve the location.  This
 947          * selection process should be done while holding the session's
 948          * mutex.
 949          */
 950         struct iscsi_cmd        *sess_cmd_table[ISCSI_CMD_TABLE_SIZE];
 951         int                     sess_cmd_table_count;
 952 
 953         /*
 954          * The pending queue contains all iscsi_cmds that require an
 955          * open MaxCmdSn window to be put on the wire and haven't
 956          * been placed on the wire.  Once placed on the wire they
 957          * will be moved to a connections specific active queue.
 958          */
 959         iscsi_queue_t           sess_queue_pending;
 960 
 961         iscsi_error_t           sess_last_err;
 962 
 963         iscsi_queue_t           sess_queue_completion;
 964         /* configured login parameters */
 965         iscsi_login_params_t    sess_params;
 966 
 967         /* general iSCSI protocol/session info */
 968         uchar_t                 sess_name[ISCSI_MAX_NAME_LEN];
 969         int                     sess_name_length;
 970         char                    sess_alias[ISCSI_MAX_NAME_LEN];
 971         int                     sess_alias_length;
 972         iSCSIDiscoveryMethod_t  sess_discovered_by;
 973         iscsi_sockaddr_t        sess_discovered_addr;
 974         uchar_t                 sess_isid[ISCSI_ISID_LEN]; /* Session ID */
 975         uint16_t                sess_tsid; /* Target ID */
 976         /*
 977          * If the target portal group tag(TPGT) is equal to ISCSI_DEFAULT_TPGT
 978          * then the initiator will accept a successful login with any TPGT
 979          * specified by the target.  If a none default TPGT is configured
 980          * then we will only successfully accept a login with that matching
 981          * TPGT value.
 982          */
 983         int                     sess_tpgt_conf;
 984         /* This field records the negotiated TPGT value, preserved for dtrace */
 985         int                     sess_tpgt_nego;
 986 
 987         /*
 988          * Authentication information.
 989          *
 990          * DCW: Again IMA seems to take a session view at this
 991          * information.
 992          */
 993         iscsi_auth_t            sess_auth;
 994 
 995         /* Statistics */
 996         struct {
 997                 kstat_t                 *ks;
 998                 iscsi_sess_stats_t      ks_data;
 999                 kstat_t                 *ks_io;
1000                 kstat_io_t              ks_io_data;
1001                 kmutex_t                ks_io_lock;
1002         } stats;
1003 
1004         iscsi_thread_t          *sess_ic_thread;
1005         boolean_t               sess_window_open;
1006         boolean_t               sess_boot;
1007         iscsi_sess_type_t       sess_type;
1008 
1009         ddi_taskq_t             *sess_login_taskq;
1010 
1011         iscsi_thread_t          *sess_wd_thread;
1012 
1013         sm_audit_buf_t          sess_state_audit;
1014 
1015         kmutex_t                sess_reset_mutex;
1016 
1017         boolean_t               sess_reset_in_progress;
1018 
1019         boolean_t               sess_boot_nic_reset;
1020         kmutex_t                sess_enum_lock;
1021         kcondvar_t              sess_enum_cv;
1022         iscsi_enum_status_t     sess_enum_status;
1023         iscsi_enum_result_t     sess_enum_result;
1024         uint32_t                sess_enum_result_count;
1025         ddi_taskq_t             *sess_enum_taskq;
1026 
1027         kmutex_t                sess_state_wmutex;
1028         kcondvar_t              sess_state_wcv;
1029         boolean_t               sess_state_hasw;
1030 
1031         /* to accelerate the state change in case of new event */
1032         volatile uint32_t       sess_state_event_count;
1033 } iscsi_sess_t;
1034 
1035 /*
1036  * This structure will be used to store sessions to be online
1037  * during normal login operation.
1038  */
1039 typedef struct iscsi_sess_list {
1040         iscsi_sess_t            *session;
1041         struct iscsi_sess_list  *next;
1042 } iscsi_sess_list_t;
1043 
1044 /*
1045  * iSCSI client notify task context for deferred IDM notifications processing
1046  */
1047 typedef struct iscsi_cn_task {
1048         idm_conn_t              *ct_ic;
1049         idm_client_notify_t     ct_icn;
1050         uintptr_t               ct_data;
1051 } iscsi_cn_task_t;
1052 
1053 /*
1054  * iscsi_network
1055  */
1056 typedef struct iscsi_network {
1057         void* (*socket)(int domain, int, int);
1058         int (*bind)(void *, struct sockaddr *, int, int, int);
1059         int (*connect)(void *, struct sockaddr *, int, int, int);
1060         int (*listen)(void *, int);
1061         void* (*accept)(void *, struct sockaddr *, int *);
1062         int (*getsockname)(void *, struct sockaddr *, socklen_t *);
1063         int (*getsockopt)(void *, int, int, void *, int *, int);
1064         int (*setsockopt)(void *, int, int, void *, int);
1065         int (*shutdown)(void *, int);
1066         void (*close)(void *);
1067 
1068         size_t (*poll)(void *, clock_t);
1069         size_t (*sendmsg)(void *, struct msghdr *);
1070         size_t (*recvmsg)(void *, struct msghdr *, int);
1071 
1072         iscsi_status_t (*sendpdu)(void *, iscsi_hdr_t *, char *, int);
1073         iscsi_status_t (*recvdata)(void *, iscsi_hdr_t *, char *,
1074             int, int, int);
1075         iscsi_status_t (*recvhdr)(void *, iscsi_hdr_t *, int, int, int);
1076 
1077         struct {
1078                 int                     sndbuf;
1079                 int                     rcvbuf;
1080                 int                     nodelay;
1081                 int                     conn_notify_threshold;
1082                 int                     conn_abort_threshold;
1083                 int                     abort_threshold;
1084         } tweaks;
1085 } iscsi_network_t;
1086 
1087 #define ISCSI_NET_HEADER_DIGEST 0x00000001
1088 #define ISCSI_NET_DATA_DIGEST   0x00000002
1089 
1090 extern iscsi_network_t *iscsi_net;
1091 
1092 /*
1093  * If we get bus_config requests in less than 5 seconds
1094  * apart skip the name services re-discovery and just
1095  * complete the requested logins.  This protects against
1096  * bus_config storms from stale /dev links.
1097  */
1098 #define ISCSI_CONFIG_STORM_DELAY_DEFAULT    5
1099 
1100 /*
1101  * iSCSI HBA Structure
1102  */
1103 typedef struct iscsi_hba {
1104         uint32_t                hba_sig;
1105         dev_info_t              *hba_dip;       /* dev info ptr */
1106         scsi_hba_tran_t         *hba_tran;      /* scsi tran ptr */
1107         ldi_ident_t             hba_li;
1108 
1109         struct iscsi_sess       *hba_sess_list; /* sess. list for hba */
1110         krwlock_t               hba_sess_list_rwlock; /* protect sess. list */
1111 
1112         /* lbolt of the last time we received a config request */
1113         clock_t                 hba_config_lbolt;
1114         /* current number of seconds to protect against bus config storms */
1115         int                     hba_config_storm_delay;
1116 
1117         /* general iSCSI protocol hba/initiator info */
1118         uchar_t                 hba_name[ISCSI_MAX_NAME_LEN];
1119         int                     hba_name_length;
1120         uchar_t                 hba_alias[ISCSI_MAX_NAME_LEN];
1121         int                     hba_alias_length;
1122 
1123         /* Default SessionID for HBA */
1124         uchar_t                 hba_isid[ISCSI_ISID_LEN];
1125 
1126         /* Default HBA wide settings */
1127         iscsi_login_params_t    hba_params;
1128 
1129         /*
1130          * There's only one HBA and it's set to ISCSI_INITIATOR_OID
1131          * (value of 1) at the beginning of time.
1132          */
1133         uint32_t                hba_oid;
1134 
1135         /*
1136          * Keep track of which events have been sent. User daemons request
1137          * this information so they don't wait for events which they won't
1138          * see.
1139          */
1140         kmutex_t                hba_discovery_events_mutex;
1141         iSCSIDiscoveryMethod_t  hba_discovery_events;
1142         boolean_t               hba_discovery_in_progress;
1143 
1144         boolean_t               hba_mpxio_enabled; /* mpxio-enabled */
1145         /* if the persistent store is loaded */
1146         boolean_t               hba_persistent_loaded;
1147 
1148         /*
1149          * Ensures only one SendTargets operation occurs at a time
1150          */
1151         ksema_t                 hba_sendtgts_semaphore;
1152 
1153         /*
1154          * Statistics
1155          */
1156         struct {
1157                 kstat_t                 *ks;
1158                 iscsi_hba_stats_t       ks_data;
1159         } stats;
1160 
1161         /*
1162          * track/control the service status and client
1163          *
1164          * service- service online ensures the operational of cli
1165          *        - and the availability of iSCSI discovery/devices
1166          *        - so obviously offline means the unusable of cli
1167          *        - , disabling of all discovery methods and to offline
1168          *        - all discovered devices
1169          *
1170          * client - here the client actually means 'exclusive client'
1171          *        - for operations these clients take may conflict
1172          *        - with the changing of service status and therefore
1173          *        - need to be exclusive
1174          *
1175          * The service has three status:
1176          *      ISCSI_SERVICE_ENABLED    -      client is permitted to
1177          *                               -      request service
1178          *
1179          *      ISCSI_SERVICE_DISABLED   -      client is not permitted to
1180          *                               -      request service
1181          *
1182          *      ISCSI_SERVICE_TRANSITION -      client must wait for
1183          *                               -      one of above two statuses
1184          *
1185          * The hba_service_client_count tracks the number of
1186          * current clients, it increases with new clients and decreases
1187          * with leaving clients. It stops to increase once the
1188          * ISCSI_SERVICE_TRANSITION is set, and causes later clients be
1189          * blocked there.
1190          *
1191          * The status of the service can only be changed when the number
1192          * of current clients reaches zero.
1193          *
1194          * Clients include:
1195          *      iscsi_ioctl
1196          *      iscsi_tran_bus_config
1197          *      iscsi_tran_bus_unconfig
1198          *      isns_scn_callback
1199          */
1200         kmutex_t                hba_service_lock;
1201         kcondvar_t              hba_service_cv;
1202         uint32_t                hba_service_status;
1203         uint32_t                hba_service_client_count;
1204 
1205         /* Default HBA tunable settings */
1206         iscsi_tunable_params_t  hba_tunable_params;
1207         boolean_t               hba_service_status_overwrite;
1208 } iscsi_hba_t;
1209 
1210 /*
1211  * +--------------------------------------------------------------------+
1212  * | iSCSI prototypes                                                   |
1213  * +--------------------------------------------------------------------+
1214  */
1215 
1216 /* IDM client callback entry points */
1217 idm_rx_pdu_cb_t iscsi_rx_scsi_rsp;
1218 idm_rx_pdu_cb_t iscsi_rx_misc_pdu;
1219 idm_rx_pdu_error_cb_t iscsi_rx_error_pdu;
1220 idm_build_hdr_cb_t iscsi_build_hdr;
1221 idm_task_cb_t iscsi_task_aborted;
1222 idm_client_notify_cb_t iscsi_client_notify;
1223 
1224 /* iscsi_io.c */
1225 int iscsi_sna_lte(uint32_t n1, uint32_t n2);
1226 char *iscsi_get_next_text(char *data, int data_length, char *curr_text);
1227 
1228 void iscsi_ic_thread(iscsi_thread_t *thread, void *arg);
1229 void iscsi_tx_thread(iscsi_thread_t *thread, void *arg);
1230 void iscsi_wd_thread(iscsi_thread_t *thread, void *arg);
1231 
1232 iscsi_status_t iscsi_tx_cmd(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
1233 
1234 void iscsi_task_cleanup(int opcode, iscsi_cmd_t *icmdp);
1235 
1236 void iscsi_handle_abort(void *arg);
1237 iscsi_status_t iscsi_handle_reset(iscsi_sess_t *isp, int level,
1238     iscsi_lun_t *ilp);
1239 iscsi_status_t iscsi_handle_logout(iscsi_conn_t *icp);
1240 iscsi_status_t iscsi_handle_passthru(iscsi_sess_t *isp, uint16_t lun,
1241     struct uscsi_cmd *ucmdp);
1242 iscsi_status_t iscsi_handle_text(iscsi_conn_t *icp,
1243     char *buf, uint32_t buf_len, uint32_t data_len, uint32_t *rx_data_len);
1244 
1245 void iscsi_iodone(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
1246 
1247 /* iscsi_crc.c */
1248 uint32_t iscsi_crc32c(void *address, unsigned long length);
1249 uint32_t iscsi_crc32c_continued(void *address, unsigned long length,
1250     uint32_t crc);
1251 
1252 /* iscsi_queue.c */
1253 void iscsi_init_queue(iscsi_queue_t *queue);
1254 void iscsi_destroy_queue(iscsi_queue_t *queue);
1255 void iscsi_enqueue_pending_cmd(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
1256 void iscsi_dequeue_pending_cmd(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
1257 void iscsi_enqueue_active_cmd(iscsi_conn_t *icp, iscsi_cmd_t *icmdp);
1258 void iscsi_dequeue_active_cmd(iscsi_conn_t *icp, iscsi_cmd_t *icmdp);
1259 void iscsi_enqueue_idm_aborting_cmd(iscsi_conn_t *icp, iscsi_cmd_t *icmdp);
1260 void iscsi_dequeue_idm_aborting_cmd(iscsi_conn_t *icp, iscsi_cmd_t *icmdp);
1261 void iscsi_enqueue_completed_cmd(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
1262 iscsi_status_t iscsi_dequeue_cmd(iscsi_cmd_t **, iscsi_cmd_t **, iscsi_cmd_t *);
1263 void iscsi_move_queue(iscsi_queue_t *src_queue, iscsi_queue_t *dst_queue);
1264 void iscsi_enqueue_cmd_head(iscsi_cmd_t **, iscsi_cmd_t **,
1265     iscsi_cmd_t *);
1266 
1267 /* iscsi_login.c */
1268 iscsi_status_t iscsi_login_start(void *arg);
1269 void iscsi_login_update_state(iscsi_conn_t *icp,
1270     iscsi_login_state_t next_state);
1271 void iscsi_login_update_state_locked(iscsi_conn_t *icp,
1272     iscsi_login_state_t next_state);
1273 
1274 
1275 /* iscsi_stats.c */
1276 boolean_t iscsi_hba_kstat_init(struct iscsi_hba *ihp);
1277 boolean_t iscsi_hba_kstat_term(struct iscsi_hba *ihp);
1278 boolean_t iscsi_sess_kstat_init(struct iscsi_sess *isp);
1279 boolean_t iscsi_sess_kstat_term(struct iscsi_sess *isp);
1280 boolean_t iscsi_conn_kstat_init(struct iscsi_conn       *icp);
1281 void iscsi_conn_kstat_term(struct iscsi_conn *icp);
1282 
1283 /* iscsi_net.c */
1284 void iscsi_net_init();
1285 void iscsi_net_fini();
1286 iscsi_status_t iscsi_net_interface(boolean_t reset);
1287 
1288 /* iscsi_sess.c */
1289 iscsi_sess_t *iscsi_sess_create(iscsi_hba_t *ihp,
1290     iSCSIDiscoveryMethod_t method, struct sockaddr *addr_dsc,
1291     char *target_name, int tpgt, uchar_t isid_lsb,
1292     iscsi_sess_type_t type, uint32_t *oid);
1293 void iscsi_sess_online(void *arg);
1294 int iscsi_sess_get(uint32_t oid, iscsi_hba_t *ihp, iscsi_sess_t **ispp);
1295 iscsi_status_t iscsi_sess_destroy(iscsi_sess_t *isp);
1296 void iscsi_sess_state_machine(iscsi_sess_t *isp, iscsi_sess_event_t event,
1297     uint32_t event_count);
1298 char *iscsi_sess_state_str(iscsi_sess_state_t state);
1299 boolean_t iscsi_sess_set_auth(iscsi_sess_t *isp);
1300 iscsi_status_t iscsi_sess_reserve_scsi_itt(iscsi_cmd_t *icmdp);
1301 void iscsi_sess_release_scsi_itt(iscsi_cmd_t *icmdp);
1302 iscsi_status_t iscsi_sess_reserve_itt(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
1303 void iscsi_sess_release_itt(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
1304 void iscsi_sess_redrive_io(iscsi_sess_t *isp);
1305 int iscsi_sess_get_by_target(uint32_t target_oid, iscsi_hba_t *ihp,
1306     iscsi_sess_t **ispp);
1307 iscsi_enum_result_t iscsi_sess_enum_request(iscsi_sess_t *isp,
1308     boolean_t wait, uint32_t event_count);
1309 iscsi_enum_result_t iscsi_sess_enum_query(iscsi_sess_t *isp);
1310 void iscsi_sess_enter_state_zone(iscsi_sess_t *isp);
1311 void iscsi_sess_exit_state_zone(iscsi_sess_t *isp);
1312 
1313 /* iscsi_conn.c */
1314 iscsi_status_t iscsi_conn_create(struct sockaddr *addr, iscsi_sess_t *isp,
1315     iscsi_conn_t **icpp);
1316 iscsi_status_t iscsi_conn_online(iscsi_conn_t *icp);
1317 iscsi_status_t iscsi_conn_offline(iscsi_conn_t *icp);
1318 iscsi_status_t iscsi_conn_destroy(iscsi_conn_t *icp);
1319 void iscsi_conn_set_login_min_max(iscsi_conn_t *icp, int min, int max);
1320 iscsi_status_t iscsi_conn_sync_params(iscsi_conn_t *icp);
1321 void iscsi_conn_retry(iscsi_sess_t *isp, iscsi_conn_t *icp);
1322 void iscsi_conn_update_state(iscsi_conn_t *icp, iscsi_conn_state_t next_state);
1323 void iscsi_conn_update_state_locked(iscsi_conn_t *icp,
1324                         iscsi_conn_state_t next_state);
1325 
1326 /* iscsi_lun.c */
1327 iscsi_status_t iscsi_lun_create(iscsi_sess_t *isp, uint16_t lun_num,
1328     uint8_t lun_addr_type, struct scsi_inquiry *inq, char *guid);
1329 iscsi_status_t iscsi_lun_destroy(iscsi_hba_t *ihp,
1330     iscsi_lun_t *ilp);
1331 void iscsi_lun_online(iscsi_hba_t *ihp,
1332     iscsi_lun_t *ilp);
1333 iscsi_status_t iscsi_lun_offline(iscsi_hba_t *ihp,
1334     iscsi_lun_t *ilp, boolean_t lun_free);
1335 
1336 /* iscsi_cmd.c */
1337 void iscsi_cmd_state_machine(iscsi_cmd_t *icmdp,
1338     iscsi_cmd_event_t event, void *arg);
1339 iscsi_cmd_t     *iscsi_cmd_alloc(iscsi_conn_t *icp, int km_flags);
1340 void            iscsi_cmd_free(iscsi_cmd_t *icmdp);
1341 
1342 /* iscsi_ioctl.c */
1343 void * iscsi_ioctl_copyin(caddr_t arg, int mode, size_t size);
1344 int iscsi_ioctl_copyout(void *data, size_t size, caddr_t arg, int mode);
1345 iscsi_conn_list_t *iscsi_ioctl_conn_oid_list_get_copyin(caddr_t, int);
1346 int iscsi_ioctl_conn_oid_list_get_copyout(iscsi_conn_list_t *, caddr_t, int);
1347 boolean_t iscsi_ioctl_conn_oid_list_get(iscsi_hba_t *ihp,
1348     iscsi_conn_list_t *cl);
1349 boolean_t iscsi_ioctl_conn_props_get(iscsi_hba_t *ihp, iscsi_conn_props_t *cp);
1350 int iscsi_ioctl_sendtgts_get(iscsi_hba_t *ihp, iscsi_sendtgts_list_t *stl);
1351 int iscsi_target_prop_mod(iscsi_hba_t *, iscsi_property_t *, int cmd);
1352 int iscsi_set_params(iscsi_param_set_t *, iscsi_hba_t *, boolean_t);
1353 int iscsi_get_persisted_param(uchar_t *, iscsi_param_get_t *,
1354     iscsi_login_params_t *);
1355 void iscsi_set_default_login_params(iscsi_login_params_t *params);
1356 int iscsi_ioctl_get_config_sess(iscsi_hba_t *ihp,
1357     iscsi_config_sess_t *ics);
1358 int iscsi_ioctl_set_config_sess(iscsi_hba_t *ihp,
1359     iscsi_config_sess_t *ics);
1360 int iscsi_ioctl_set_tunable_param(iscsi_hba_t *ihp,
1361     iscsi_tunable_object_t *tpss);
1362 /* ioctls  prototypes */
1363 int iscsi_get_param(iscsi_login_params_t *params,
1364     boolean_t valid_flag,
1365     iscsi_param_get_t *ipgp);
1366 
1367 /* iscsid.c */
1368 boolean_t iscsid_init(iscsi_hba_t *ihp);
1369 boolean_t iscsid_start(iscsi_hba_t *ihp);
1370 boolean_t iscsid_stop(iscsi_hba_t *ihp);
1371 void iscsid_fini();
1372 void iscsid_props(iSCSIDiscoveryProperties_t *props);
1373 boolean_t iscsid_enable_discovery(iscsi_hba_t *ihp,
1374     iSCSIDiscoveryMethod_t idm, boolean_t poke);
1375 boolean_t iscsid_disable_discovery(iscsi_hba_t *ihp,
1376     iSCSIDiscoveryMethod_t idm);
1377 void iscsid_poke_discovery(iscsi_hba_t *ihp, iSCSIDiscoveryMethod_t method);
1378 void iscsid_do_sendtgts(entry_t *discovery_addr);
1379 void iscsid_do_isns_query_one_server(
1380     iscsi_hba_t *ihp, entry_t *isns_addr);
1381 void iscsid_do_isns_query(iscsi_hba_t *ihp);
1382 void iscsid_config_one(iscsi_hba_t *ihp,
1383     char *name, boolean_t protect);
1384 void iscsid_config_all(iscsi_hba_t *ihp, boolean_t protect);
1385 void iscsid_unconfig_one(iscsi_hba_t *ihp, char *name);
1386 void iscsid_unconfig_all(iscsi_hba_t *ihp);
1387 void isns_scn_callback(void *arg);
1388 boolean_t iscsid_del(iscsi_hba_t *ihp, char *target_name,
1389     iSCSIDiscoveryMethod_t method, struct sockaddr *addr_dsc);
1390 boolean_t iscsid_login_tgt(iscsi_hba_t *ihp, char *target_name,
1391     iSCSIDiscoveryMethod_t method, struct sockaddr *addr_dsc);
1392 void iscsid_addr_to_sockaddr(int src_insize, void *src_addr, int src_port,
1393     struct sockaddr *dst_addr);
1394 void iscsid_set_default_initiator_node_settings(iscsi_hba_t *ihp,
1395     boolean_t minimal);
1396 
1397 void iscsi_send_sysevent(iscsi_hba_t *ihp, char *eventcalss,
1398     char *subclass, nvlist_t *np);
1399 boolean_t iscsi_reconfig_boot_sess(iscsi_hba_t *ihp);
1400 boolean_t iscsi_chk_bootlun_mpxio(iscsi_hba_t *ihp);
1401 boolean_t iscsi_cmp_boot_ini_name(char *name);
1402 boolean_t iscsi_cmp_boot_tgt_name(char *name);
1403 boolean_t iscsi_client_request_service(iscsi_hba_t *ihp);
1404 void iscsi_client_release_service(iscsi_hba_t *ihp);
1405 
1406 extern void bcopy(const void *s1, void *s2, size_t n);
1407 extern void bzero(void *s, size_t n);
1408 
1409 #ifdef __cplusplus
1410 }
1411 #endif
1412 
1413 #endif /* _ISCSI_H */