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 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 #ifndef _FCT_IMPL_H
  27 #define _FCT_IMPL_H
  28 
  29 #ifdef  __cplusplus
  30 extern "C" {
  31 #endif
  32 
  33 #define RSCN_OPTION_VERIFY      0x0001
  34 
  35 typedef enum fct_li_state {
  36         LI_STATE_DO_FLOGI = 0,  /* FLOGI handled by FCA */
  37         LI_STATE_FINI_TOPOLOGY, /* Finalize topology */
  38         LI_STATE_N2N_PLOGI,     /* In case of a N2N connection */
  39 
  40         LI_STATE_DO_FCLOGIN,    /* Login into 0xFFFFFD */
  41         LI_STATE_DO_SCR,        /* State change registration */
  42 
  43         LI_STATE_DO_NSLOGIN,    /* Login into 0xFFFFFC */
  44         LI_STATE_DO_RNN,        /* Register node name */
  45         LI_STATE_DO_RCS,        /* Register classes of service */
  46         LI_STATE_DO_RFT,        /* Register FC-4 types */
  47         LI_STATE_DO_RSPN,       /* Register symbolic port name */
  48         LI_STATE_DO_RSNN,       /* Register symbolic node name */
  49 
  50         LI_STATE_MAX            /* Not a real state */
  51 } fct_li_state_t;
  52 
  53 #define LI_STATE_START                  0
  54 #define LI_STATE_MASK                   0x3F
  55 /* Next state depends on the return value */
  56 #define LI_STATE_FLAG_CMD_RETCHECK      0x40
  57 /* Link init cmd is still outstanding */
  58 #define LI_STATE_FLAG_CMD_WAITING       0x80
  59 /* Flag to indicate that link info is not available yet */
  60 #define LI_STATE_FLAG_NO_LI_YET         0x100
  61 
  62 #define FCT_MAX_CACHED_CMDS     256
  63 #define USEC_ELS_TIMEOUT        (10 * 1000 * 1000)
  64 #define USEC_SOL_TIMEOUT        (10 * 1000 * 1000)
  65 #define USEC_DEREG_RP_TIMEOUT   (25 * 1000 * 1000)
  66 #define USEC_DEREG_RP_INTERVAL  (2 * 1000 * 1000)
  67 
  68 struct fct_i_cmd;
  69 typedef void (* fct_icmd_cb_t)(struct fct_i_cmd *icmd);
  70 typedef struct fct_i_cmd {
  71         fct_cmd_t               *icmd_cmd;
  72         uint32_t                 icmd_alloc_size;
  73         fct_struct_id_t          icmd_struct_id;
  74         uint32_t                 icmd_flags;
  75         clock_t                  icmd_start_time;
  76         list_node_t             icmd_node;      /* iport_abort_queue and irp */
  77         struct fct_i_cmd        *icmd_solcmd_next;      /* iport_solcmd_queue */
  78         fct_icmd_cb_t            icmd_cb;
  79         void                    *icmd_cb_private;
  80 } fct_i_cmd_t;
  81 
  82 /*
  83  * icmd_flags
  84  */
  85 #define ICMD_SESSION_AFFECTING          0x0002
  86 #define ICMD_IN_IRP_QUEUE               0x0004
  87 #define ICMD_BEING_ABORTED              0x0008
  88 #define ICMD_KNOWN_TO_FCA               0x0020
  89 #define ICMD_FCA_ABORT_CALLED           0x0040
  90 #define ICMD_CMD_COMPLETE               0x0080
  91 #define ICMD_IN_TRANSITION              0x0100
  92 #define ICMD_ABTS_RECEIVED              0x0200
  93 #define ICMD_IMPLICIT                   0x0400
  94 #define ICMD_IMPLICIT_CMD_HAS_RESOURCE  0x0800
  95 /* High order are debug flags */
  96 #define ICMD_ELS_PROCESSING_STARTED     0x80000000
  97 
  98 /*
  99  * For solicited commands, there's only 3 states:
 100  * 1) it's new. We need send it to FCA. ICMD_SOLCMD_NEW is set
 101  * 2) it's running. We are waiting for completion.
 102  * 3) it's completed. We need free it. ICMD_CMD_COMPLETE is set
 103  * ICMD_SOLCMD_NEW and ICMD_CMD_COMPLETE should not be set in the same time
 104  */
 105 #define ICMD_IN_SOLCMD_QUEUE            0x010000
 106 #define ICMD_SOLCMD_NEW                 0x020000
 107 
 108 typedef struct fct_i_remote_port {
 109         fct_remote_port_t               *irp_rp;
 110         uint32_t                        irp_alloc_size;
 111         fct_struct_id_t                 irp_struct_id;
 112         krwlock_t                       irp_lock;
 113 
 114         /* For queueing to local port */
 115         struct fct_i_remote_port        *irp_next;
 116 
 117         /* For queueing to handle elses */
 118         struct fct_i_remote_port        *irp_discovery_next;
 119 
 120         list_t                          irp_els_list;
 121 
 122         /*
 123          * sa stands for session affecting, nsa is non session affecting.
 124          * The els counts only represent elses under progress not the ones
 125          * that are terminated. active_xchg_count covers everything including
 126          * the ones waiting to be terminated.
 127          */
 128         uint16_t                        irp_sa_elses_count;
 129         uint16_t                        irp_nsa_elses_count;
 130         uint16_t                        irp_fcp_xchg_count;
 131         uint16_t                        irp_nonfcp_xchg_count;
 132 
 133         uint32_t                        irp_flags;
 134         clock_t                         irp_deregister_timer;
 135         uint32_t                        irp_dereg_count;
 136 
 137         uint32_t                        irp_portid;
 138         uint8_t                         irp_id[24];
 139         uint32_t                        irp_rcvd_prli_params;
 140         uint32_t                        irp_sent_prli_params;
 141 
 142         /*
 143          * Most HBAs will only register symbolic node name instead of port name,
 144          * so we use SNN as session alias.
 145          */
 146         stmf_scsi_session_t             *irp_session;
 147         char                            *irp_snn;
 148         uint16_t                        irp_snn_len;
 149 
 150         /* items will be filled in ns cmd */
 151         uint8_t                         irp_fc4types[32]; /* FC-4 types */
 152         char                            *irp_spn;       /* port symbolic name */
 153         uint16_t                        irp_spn_len;
 154         uint32_t                        irp_cos;        /* class of service */
 155 
 156         uint32_t                        irp_rscn_counter;
 157 } fct_i_remote_port_t;
 158 
 159 /*
 160  * structure used for fct_rls_cb() callback private data
 161  */
 162 typedef struct fct_rls_cb_data {
 163         struct fct_port_link_status     *fct_link_status;
 164         fct_status_t                    fct_els_res;
 165 } fct_rls_cb_data_t;
 166 
 167 /*
 168  * irp flags
 169  */
 170 #define IRP_PLOGI_DONE                  0x0001
 171 #define IRP_PRLI_DONE                   0x0002
 172 #define IRP_IN_DISCOVERY_QUEUE          0x0004
 173 #define IRP_FCP_CLEANUP                 0x0008
 174 #define IRP_SESSION_CLEANUP             (IRP_FCP_CLEANUP | 0x0010)
 175 #define IRP_HANDLE_OPENED               0x0020
 176 #define IRP_SCSI_SESSION_STARTED        0x0040
 177 #define IRP_RSCN_QUEUED                 0x0080
 178 #define IRP_SOL_PLOGI_IN_PROGRESS       0x0100
 179 
 180 typedef struct fct_cmd_slot {
 181         fct_i_cmd_t             *slot_cmd;
 182         uint16_t                slot_no;
 183         uint16_t                slot_next;
 184         uint8_t                 slot_uniq_cntr;
 185 } fct_cmd_slot_t;
 186 #define FCT_SLOT_EOL            0xffff
 187 
 188 #define FCT_HASH_TABLE_SIZE             256
 189 #define FCT_LOOP_HASH(portid)           (portid & 0xff)
 190 #define FCT_FABRIC_HASH(portid)         (((portid & 0x1f00) | \
 191         ((portid & 0x70000)>>3)) >> 8)
 192 #define FCT_PORTID_HASH_FUNC(portid) \
 193         ((portid & 0xFFFF00)?FCT_FABRIC_HASH(portid):FCT_LOOP_HASH(portid))
 194 
 195 typedef struct fct_i_local_port {
 196         fct_local_port_t        *iport_port;
 197         uint32_t                iport_alloc_size;
 198         fct_struct_id_t         iport_struct_id;
 199 
 200         struct fct_i_local_port *iport_next;
 201         struct fct_i_local_port *iport_prev;
 202 
 203         char                    *iport_alias;
 204         char                    iport_alias_mem[16];
 205         uint8_t                 iport_id[24];   /* scsi_devid_desc_t */
 206         krwlock_t               iport_lock;
 207         uint32_t                iport_flags;
 208         uint16_t                iport_link_state;
 209         uint8_t                 iport_state:7,
 210             iport_state_not_acked:1;
 211         uint8_t                 iport_offline_prstate;
 212         struct fct_link_info    iport_link_info;
 213 
 214         fct_i_remote_port_t     **iport_rp_slots;
 215         fct_i_remote_port_t     **iport_rp_tb;
 216         uint32_t                iport_nrps_login; /* currently logged in */
 217         uint32_t                iport_nrps;     /* items in hash table */
 218         uint64_t                iport_last_change;
 219 
 220         /*
 221          * These variables are used to manage fct_cmd_t cache for SCSI traffic
 222          */
 223         /*
 224          * Total # of cmds allocated by the driver. Some of which are free
 225          * and sitting on iport_cached_cmdlist. And some are executing.
 226          */
 227         uint32_t                iport_total_alloced_ncmds;
 228 
 229         /*
 230          * Max active cmds in last interval (10 or 30 seconds)
 231          */
 232         uint32_t                iport_max_active_ncmds;
 233 
 234         /*
 235          * # of free cmds sitting on the iport_cached_cmdlist
 236          */
 237         uint32_t                iport_cached_ncmds;
 238         list_t                  iport_cached_cmdlist;
 239         kmutex_t                iport_cached_cmd_lock;
 240 
 241         /*
 242          * To release free cmds periodically
 243          */
 244         clock_t                 iport_cmdcheck_clock;
 245 
 246         uint16_t                iport_task_green_limit;
 247         uint16_t                iport_task_yellow_limit;
 248         uint16_t                iport_task_red_limit;
 249         /* cmd slots */
 250         uint16_t                iport_nslots_free;
 251 
 252         /* upper 16 bits is just a counter to avoid ABA issues */
 253         uint32_t                iport_next_free_slot;
 254 
 255         uint8_t                 iport_login_retry; /* for flogi and N2N plogi */
 256         uint8_t                 iport_link_old_topology;
 257         uint8_t                 iport_link_cleanup_retry;
 258         clock_t                 iport_li_cmd_timeout; /* for li state m/c */
 259         fct_cmd_slot_t          *iport_cmd_slots;
 260 
 261         /* worker thread data */
 262         ddi_taskq_t             *iport_worker_taskq;
 263         kmutex_t                iport_worker_lock;
 264         kcondvar_t              iport_worker_cv;
 265         struct fct_i_event      *iport_event_head;
 266         struct fct_i_event      *iport_event_tail;
 267         list_t                  iport_abort_queue;
 268         struct fct_i_cmd        *iport_ppicmd_term;
 269 
 270         /* link initialization */
 271         fct_status_t            iport_li_comp_status;
 272         enum fct_li_state       iport_li_state;
 273 
 274         /* solicited cmd link */
 275         struct fct_i_cmd        *iport_solcmd_queue;
 276 
 277         /* rpwe = remote port with pending els(es) */
 278         fct_i_remote_port_t     *iport_rpwe_head;
 279         fct_i_remote_port_t     *iport_rpwe_tail;
 280         kstat_t                 *iport_kstat_portstat;
 281         ksema_t                 iport_rls_sema;
 282         fct_rls_cb_data_t       iport_rls_cb_data;
 283 } fct_i_local_port_t;
 284 
 285 #define IPORT_FLOGI_DONE(iport) PORT_FLOGI_DONE(&(iport)->iport_link_info)
 286 
 287 /*
 288  * iport flags
 289  */
 290 #define IPORT_WORKER_RUNNING            0x0001
 291 #define IPORT_TERMINATE_WORKER          0x0002
 292 #define IPORT_WORKER_DOING_TIMEDWAIT    0x0004
 293 #define IPORT_WORKER_DOING_WAIT         0x0008
 294 #define IPORT_FLAG_PORT_OFFLINED        0x0010
 295 #define IPORT_ALLOW_UNSOL_FLOGI         0x0020
 296 
 297 #define IS_WORKER_SLEEPING(iport)       ((iport)->iport_flags & \
 298         (IPORT_WORKER_DOING_TIMEDWAIT | IPORT_WORKER_DOING_WAIT))
 299 
 300 /* Limits for scsi task load of local port */
 301 #define FCT_TASK_GREEN_LIMIT            80
 302 #define FCT_TASK_YELLOW_LIMIT           90
 303 #define FCT_TASK_RED_LIMIT              95
 304 
 305 typedef struct fct_i_event {
 306         struct fct_i_event      *event_next;
 307         int                     event_type;
 308 } fct_i_event_t;
 309 
 310 typedef enum { /* Seggested action values for discovery thread */
 311     DISC_ACTION_NO_WORK = 0,
 312     DISC_ACTION_RESCAN = 1,
 313     DISC_ACTION_DELAY_RESCAN = 2,
 314     DISC_ACTION_USE_SHORT_DELAY = 4
 315 } disc_action_t;
 316 
 317 /*
 318  * Local port state definitions
 319  * NOTE that every time there is a state change, the newly set bit suggests
 320  * the action. So far there are 3 actions S_PORT_CLEANUP, S_ADAPTER_FATAL
 321  * and S_INIT_LINK.
 322  */
 323 #define S_RCVD_LINK_DOWN        0x01
 324 #define S_RCVD_LINK_UP          0x02
 325 #define S_LINK_ONLINE           0x04
 326 #define S_INIT_LINK             0x08
 327 #define S_PORT_CLEANUP          0x10
 328 
 329 #define PORT_STATE_LINK_DOWN            0x00
 330 #define PORT_STATE_LINK_INIT_START      (S_RCVD_LINK_UP | S_LINK_ONLINE |\
 331     S_INIT_LINK)
 332 #define PORT_STATE_LINK_INIT_DONE       (S_LINK_ONLINE)
 333 #define PORT_STATE_LINK_UP_CLEANING     (S_RCVD_LINK_UP | S_PORT_CLEANUP)
 334 #define PORT_STATE_LINK_DOWN_CLEANING   (S_RCVD_LINK_DOWN | S_PORT_CLEANUP)
 335 
 336 /*
 337  * Internal events
 338  */
 339 #define FCT_I_EVENT_LINK_INIT_DONE      0x80
 340 #define FCT_I_EVENT_CLEANUP_POLL        0x81
 341 
 342 /*
 343  * Offline processing states, used by worker thread.
 344  */
 345 #define FCT_OPR_DONE                    0
 346 #define FCT_OPR_START                   1
 347 #define FCT_OPR_CMD_CLEANUP_WAIT        2
 348 #define FCT_OPR_INT_CLEANUP_WAIT        3
 349 
 350 /*
 351  * Check time
 352  */
 353 #define FCT_CMDLIST_CHECK_SECONDS       10
 354 
 355 /*
 356  * Define frequently used macros
 357  */
 358 #define ICMD_TO_CT(x_icmd)      \
 359         ((fct_sol_ct_t *)(x_icmd)->icmd_cmd->cmd_specific)
 360 
 361 #define ICMD_TO_ELS(x_icmd)     \
 362         ((fct_els_t *)(x_icmd)->icmd_cmd->cmd_specific)
 363 
 364 #define ICMD_TO_IPORT(x_icmd)   \
 365         ((fct_i_local_port_t *)(x_icmd)->icmd_cmd->cmd_port->port_fct_private)
 366 
 367 #define ICMD_TO_PORT(x_icmd)    \
 368         ((x_icmd)->icmd_cmd->cmd_port)
 369 
 370 #define ICMD_TO_IRP(x_icmd)     \
 371         ((fct_i_remote_port_t *)(x_icmd)->icmd_cmd->cmd_rp->rp_fct_private)
 372 
 373 #define CMD_TO_ICMD(x_cmd)      ((fct_i_cmd_t *)(x_cmd)->cmd_fct_private)
 374 
 375 #define RP_TO_IRP(x_rp)         ((fct_i_remote_port_t *)(x_rp)->rp_fct_private)
 376 
 377 #define PORT_TO_IPORT(x_port)   \
 378         ((fct_i_local_port_t *)(x_port)->port_fct_private)
 379 
 380 #define FCT_IS_ELS_ACC(x_icmd)  \
 381         (((x_icmd)->icmd_cmd->cmd_comp_status == FCT_SUCCESS) &&  \
 382         (ICMD_TO_ELS(x_icmd)->els_resp_payload[0] == ELS_OP_ACC))
 383 
 384 #define FCT_IS_CT_ACC(x_icmd)   \
 385         (((x_icmd)->icmd_cmd->cmd_comp_status == FCT_SUCCESS) &&  \
 386         (ICMD_TO_CT(x_icmd)->ct_resp_payload[8] == 0x80) &&\
 387         (ICMD_TO_CT(x_icmd)->ct_resp_payload[9] == 0x02))
 388 
 389 #define IPORT_IN_NS_TOPO(x_iport)       \
 390         ((x_iport)->iport_link_info.port_topology & PORT_TOPOLOGY_FABRIC_BIT)
 391 
 392 #define IS_LOGO_ELS(icmd)       \
 393         (ICMD_TO_ELS(icmd)->els_req_payload[0] == ELS_OP_LOGO)
 394 
 395 stmf_status_t fct_xfer_scsi_data(scsi_task_t *task,
 396     stmf_data_buf_t *dbuf, uint32_t ioflags);
 397 stmf_status_t fct_send_scsi_status(scsi_task_t *task, uint32_t ioflags);
 398 fct_i_remote_port_t *fct_portid_to_portptr(fct_i_local_port_t *iport,
 399     uint32_t portid);
 400 fct_i_remote_port_t *fct_lookup_irp_by_nodewwn(fct_i_local_port_t *iport,
 401     uint8_t *nodewwn);
 402 fct_i_remote_port_t *fct_lookup_irp_by_portwwn(fct_i_local_port_t *iport,
 403     uint8_t *portwwn);
 404 void fct_queue_rp(fct_i_local_port_t *iport, fct_i_remote_port_t *irp);
 405 void fct_deque_rp(fct_i_local_port_t *iport, fct_i_remote_port_t *irp);
 406 int fct_implicitly_logo_all(fct_i_local_port_t *iport, int force_implicit);
 407 void fct_post_implicit_logo(fct_cmd_t *cmd);
 408 void fct_rehash(fct_i_local_port_t *iport);
 409 uint8_t fct_local_port_cleanup_done(fct_i_local_port_t *iport);
 410 void fct_handle_rcvd_abts(fct_cmd_t *cmd);
 411 void fct_fill_abts_acc(fct_cmd_t *cmd);
 412 void fct_q_for_termination_lock_held(fct_i_local_port_t *iport,
 413     fct_i_cmd_t *icmd, fct_status_t s);
 414 disc_action_t fct_handle_port_offline(fct_i_local_port_t *iport);
 415 disc_action_t fct_cmd_terminator(fct_i_local_port_t *iport);
 416 void fct_cmd_free(fct_cmd_t *cmd);
 417 void fct_scsi_task_free(scsi_task_t *task);
 418 stmf_status_t fct_scsi_abort(stmf_local_port_t *lport, int abort_cmd,
 419     void *arg, uint32_t flags);
 420 stmf_status_t fct_info(uint32_t cmd, stmf_local_port_t *lport,
 421     void *arg, uint8_t *buf, uint32_t *bufsizep);
 422 void fct_event_handler(stmf_local_port_t *lport, int eventid,
 423     void *arg, uint32_t flags);
 424 uint16_t fct_alloc_cmd_slot(fct_i_local_port_t *iport, fct_cmd_t *cmd);
 425 void fct_post_to_discovery_queue(fct_i_local_port_t *iport,
 426     fct_i_remote_port_t *irp, fct_i_cmd_t *icmd);
 427 fct_cmd_t *fct_create_solct(fct_local_port_t *port, fct_remote_port_t *rp,
 428     uint16_t ctop, fct_icmd_cb_t icmdcb);
 429 fct_cmd_t *fct_create_solels(fct_local_port_t *port, fct_remote_port_t *rp,
 430     int implicit, uchar_t elsop, uint32_t wkdid, fct_icmd_cb_t icmdcb);
 431 void fct_handle_solct(fct_cmd_t *cmd);
 432 void fct_post_to_solcmd_queue(fct_local_port_t *port, fct_cmd_t *cmd);
 433 void fct_logo_cb(fct_i_cmd_t *icmd);
 434 void fct_link_init_cb(fct_i_cmd_t *icmd);
 435 void fct_gsnn_cb(fct_i_cmd_t *icmd);
 436 void fct_gcs_cb(fct_i_cmd_t *icmd);
 437 void fct_gft_cb(fct_i_cmd_t *icmd);
 438 void fct_gspn_cb(fct_i_cmd_t *icmd);
 439 void fct_rls_cb(fct_i_cmd_t *icmd);
 440 disc_action_t fct_process_link_init(fct_i_local_port_t *iport);
 441 void fct_cmd_unlink_els(fct_i_remote_port_t *irp, fct_i_cmd_t *icmd);
 442 
 443 #ifdef  __cplusplus
 444 }
 445 #endif
 446 
 447 #endif /* _FCT_IMPL_H */