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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25 #include <sys/conf.h> 26 #include <sys/file.h> 27 #include <sys/ddi.h> 28 #include <sys/sunddi.h> 29 #include <sys/modctl.h> 30 #include <sys/scsi/scsi.h> 31 #include <sys/scsi/impl/scsi_reset_notify.h> 32 #include <sys/disp.h> 33 #include <sys/byteorder.h> 34 #include <sys/atomic.h> 35 #include <sys/ethernet.h> 36 #include <sys/sdt.h> 37 #include <sys/nvpair.h> 38 #include <sys/zone.h> 39 #include <sys/id_space.h> 40 41 #include <stmf.h> 42 #include <lpif.h> 43 #include <portif.h> 44 #include <stmf_ioctl.h> 45 #include <stmf_impl.h> 46 #include <lun_map.h> 47 #include <stmf_state.h> 48 #include <pppt_ic_if.h> 49 #include <stmf_stats.h> 50 51 /* 52 * Lock order: 53 * stmf_state_lock --> ilport_lock/iss_lockp --> ilu_task_lock 54 */ 55 56 static uint64_t stmf_session_counter = 0; 57 static uint16_t stmf_rtpid_counter = 0; 58 /* start messages at 1 */ 59 static uint64_t stmf_proxy_msg_id = 1; 60 #define MSG_ID_TM_BIT 0x8000000000000000 61 62 static int stmf_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 63 static int stmf_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 64 static int stmf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, 65 void **result); 66 static int stmf_open(dev_t *devp, int flag, int otype, cred_t *credp); 67 static int stmf_close(dev_t dev, int flag, int otype, cred_t *credp); 68 static int stmf_ioctl(dev_t dev, int cmd, intptr_t data, int mode, 69 cred_t *credp, int *rval); 70 static int stmf_get_stmf_state(stmf_state_desc_t *std); 71 static int stmf_set_stmf_state(stmf_state_desc_t *std); 72 static void stmf_abort_task_offline(scsi_task_t *task, int offline_lu, 73 char *info); 74 static int stmf_set_alua_state(stmf_alua_state_desc_t *alua_state); 75 static void stmf_get_alua_state(stmf_alua_state_desc_t *alua_state); 76 stmf_xfer_data_t *stmf_prepare_tpgs_data(uint8_t ilu_alua); 77 void stmf_svc_init(); 78 stmf_status_t stmf_svc_fini(); 79 void stmf_svc(void *arg); 80 void stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info); 81 void stmf_check_freetask(); 82 void stmf_abort_target_reset(scsi_task_t *task); 83 stmf_status_t stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task, 84 int target_reset); 85 void stmf_target_reset_poll(struct scsi_task *task); 86 void stmf_handle_lun_reset(scsi_task_t *task); 87 void stmf_handle_target_reset(scsi_task_t *task); 88 void stmf_xd_to_dbuf(stmf_data_buf_t *dbuf, int set_rel_off); 89 int stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi, uint64_t *ppi_token, 90 uint32_t *err_ret); 91 int stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi); 92 int stmf_get_ppd_ioctl(stmf_ppioctl_data_t *ppi, stmf_ppioctl_data_t *ppi_out, 93 uint32_t *err_ret); 94 void stmf_delete_ppd(stmf_pp_data_t *ppd); 95 void stmf_delete_all_ppds(); 96 void stmf_trace_clear(); 97 void stmf_worker_init(); 98 stmf_status_t stmf_worker_fini(); 99 void stmf_worker_mgmt(); 100 void stmf_worker_task(void *arg); 101 static void stmf_task_lu_free(scsi_task_t *task, stmf_i_scsi_session_t *iss); 102 static stmf_status_t stmf_ic_lu_reg(stmf_ic_reg_dereg_lun_msg_t *msg, 103 uint32_t type); 104 static stmf_status_t stmf_ic_lu_dereg(stmf_ic_reg_dereg_lun_msg_t *msg); 105 static stmf_status_t stmf_ic_rx_scsi_status(stmf_ic_scsi_status_msg_t *msg); 106 static stmf_status_t stmf_ic_rx_status(stmf_ic_status_msg_t *msg); 107 static stmf_status_t stmf_ic_rx_scsi_data(stmf_ic_scsi_data_msg_t *msg); 108 void stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s); 109 110 /* pppt modhandle */ 111 ddi_modhandle_t pppt_mod; 112 113 /* pppt modload imported functions */ 114 stmf_ic_reg_port_msg_alloc_func_t ic_reg_port_msg_alloc; 115 stmf_ic_dereg_port_msg_alloc_func_t ic_dereg_port_msg_alloc; 116 stmf_ic_reg_lun_msg_alloc_func_t ic_reg_lun_msg_alloc; 117 stmf_ic_dereg_lun_msg_alloc_func_t ic_dereg_lun_msg_alloc; 118 stmf_ic_lun_active_msg_alloc_func_t ic_lun_active_msg_alloc; 119 stmf_ic_scsi_cmd_msg_alloc_func_t ic_scsi_cmd_msg_alloc; 120 stmf_ic_scsi_data_xfer_done_msg_alloc_func_t ic_scsi_data_xfer_done_msg_alloc; 121 stmf_ic_session_create_msg_alloc_func_t ic_session_reg_msg_alloc; 122 stmf_ic_session_destroy_msg_alloc_func_t ic_session_dereg_msg_alloc; 123 stmf_ic_tx_msg_func_t ic_tx_msg; 124 stmf_ic_msg_free_func_t ic_msg_free; 125 126 static void stmf_itl_task_start(stmf_i_scsi_task_t *itask); 127 static void stmf_itl_lu_new_task(stmf_i_scsi_task_t *itask); 128 static void stmf_itl_task_done(stmf_i_scsi_task_t *itask); 129 130 static void stmf_lport_xfer_start(stmf_i_scsi_task_t *itask, 131 stmf_data_buf_t *dbuf); 132 static void stmf_lport_xfer_done(stmf_i_scsi_task_t *itask, 133 stmf_data_buf_t *dbuf); 134 135 static void stmf_update_kstat_lu_q(scsi_task_t *, void()); 136 static void stmf_update_kstat_lport_q(scsi_task_t *, void()); 137 static void stmf_update_kstat_lu_io(scsi_task_t *, stmf_data_buf_t *); 138 static void stmf_update_kstat_lport_io(scsi_task_t *, stmf_data_buf_t *); 139 140 static int stmf_irport_compare(const void *void_irport1, 141 const void *void_irport2); 142 static stmf_i_remote_port_t *stmf_irport_create(scsi_devid_desc_t *rport_devid); 143 static void stmf_irport_destroy(stmf_i_remote_port_t *irport); 144 static stmf_i_remote_port_t *stmf_irport_register( 145 scsi_devid_desc_t *rport_devid); 146 static stmf_i_remote_port_t *stmf_irport_lookup_locked( 147 scsi_devid_desc_t *rport_devid); 148 static void stmf_irport_deregister(stmf_i_remote_port_t *irport); 149 150 static void stmf_teardown_itl_kstats(stmf_i_itl_kstat_t *ks); 151 static void stmf_delete_itl_kstat_by_lport(char *); 152 static void stmf_delete_itl_kstat_by_guid(char *); 153 static int stmf_itl_kstat_compare(const void*, const void*); 154 static stmf_i_itl_kstat_t *stmf_itl_kstat_lookup(char *kstat_nm); 155 static stmf_i_itl_kstat_t *stmf_itl_kstat_create(stmf_itl_data_t *itl, 156 char *nm, scsi_devid_desc_t *lport, scsi_devid_desc_t *lun); 157 158 extern struct mod_ops mod_driverops; 159 160 /* =====[ Tunables ]===== */ 161 /* Internal tracing */ 162 volatile int stmf_trace_on = 1; 163 volatile int stmf_trace_buf_size = (1 * 1024 * 1024); 164 /* 165 * The reason default task timeout is 75 is because we want the 166 * host to timeout 1st and mostly host timeout is 60 seconds. 167 */ 168 volatile int stmf_default_task_timeout = 75; 169 /* 170 * Setting this to one means, you are responsible for config load and keeping 171 * things in sync with persistent database. 172 */ 173 volatile int stmf_allow_modunload = 0; 174 175 volatile int stmf_max_nworkers = 256; 176 volatile int stmf_min_nworkers = 4; 177 volatile int stmf_worker_scale_down_delay = 20; 178 179 /* === [ Debugging and fault injection ] === */ 180 #ifdef DEBUG 181 volatile int stmf_drop_task_counter = 0; 182 volatile int stmf_drop_buf_counter = 0; 183 184 #endif 185 186 stmf_state_t stmf_state; 187 static stmf_lu_t *dlun0; 188 189 static uint8_t stmf_first_zero[] = 190 { 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0xff }; 191 static uint8_t stmf_first_one[] = 192 { 0xff, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 }; 193 194 static kmutex_t trace_buf_lock; 195 static int trace_buf_size; 196 static int trace_buf_curndx; 197 caddr_t stmf_trace_buf; 198 199 static enum { 200 STMF_WORKERS_DISABLED = 0, 201 STMF_WORKERS_ENABLING, 202 STMF_WORKERS_ENABLED 203 } stmf_workers_state = STMF_WORKERS_DISABLED; 204 static int stmf_i_max_nworkers; 205 static int stmf_i_min_nworkers; 206 static int stmf_nworkers_cur; /* # of workers currently running */ 207 static int stmf_nworkers_needed; /* # of workers need to be running */ 208 static int stmf_worker_sel_counter = 0; 209 static uint32_t stmf_cur_ntasks = 0; 210 static clock_t stmf_wm_last = 0; 211 /* 212 * This is equal to stmf_nworkers_cur while we are increasing # workers and 213 * stmf_nworkers_needed while we are decreasing the worker count. 214 */ 215 static int stmf_nworkers_accepting_cmds; 216 static stmf_worker_t *stmf_workers = NULL; 217 static clock_t stmf_worker_mgmt_delay = 2; 218 static clock_t stmf_worker_scale_down_timer = 0; 219 static int stmf_worker_scale_down_qd = 0; 220 221 static struct cb_ops stmf_cb_ops = { 222 stmf_open, /* open */ 223 stmf_close, /* close */ 224 nodev, /* strategy */ 225 nodev, /* print */ 226 nodev, /* dump */ 227 nodev, /* read */ 228 nodev, /* write */ 229 stmf_ioctl, /* ioctl */ 230 nodev, /* devmap */ 231 nodev, /* mmap */ 232 nodev, /* segmap */ 233 nochpoll, /* chpoll */ 234 ddi_prop_op, /* cb_prop_op */ 235 0, /* streamtab */ 236 D_NEW | D_MP, /* cb_flag */ 237 CB_REV, /* rev */ 238 nodev, /* aread */ 239 nodev /* awrite */ 240 }; 241 242 static struct dev_ops stmf_ops = { 243 DEVO_REV, 244 0, 245 stmf_getinfo, 246 nulldev, /* identify */ 247 nulldev, /* probe */ 248 stmf_attach, 249 stmf_detach, 250 nodev, /* reset */ 251 &stmf_cb_ops, 252 NULL, /* bus_ops */ 253 NULL /* power */ 254 }; 255 256 #define STMF_NAME "COMSTAR STMF" 257 #define STMF_MODULE_NAME "stmf" 258 259 static struct modldrv modldrv = { 260 &mod_driverops, 261 STMF_NAME, 262 &stmf_ops 263 }; 264 265 static struct modlinkage modlinkage = { 266 MODREV_1, 267 &modldrv, 268 NULL 269 }; 270 271 int 272 _init(void) 273 { 274 int ret; 275 276 ret = mod_install(&modlinkage); 277 if (ret) 278 return (ret); 279 stmf_trace_buf = kmem_zalloc(stmf_trace_buf_size, KM_SLEEP); 280 trace_buf_size = stmf_trace_buf_size; 281 trace_buf_curndx = 0; 282 mutex_init(&trace_buf_lock, NULL, MUTEX_DRIVER, 0); 283 bzero(&stmf_state, sizeof (stmf_state_t)); 284 /* STMF service is off by default */ 285 stmf_state.stmf_service_running = 0; 286 mutex_init(&stmf_state.stmf_lock, NULL, MUTEX_DRIVER, NULL); 287 cv_init(&stmf_state.stmf_cv, NULL, CV_DRIVER, NULL); 288 stmf_session_counter = (uint64_t)ddi_get_lbolt(); 289 avl_create(&stmf_state.stmf_irportlist, 290 stmf_irport_compare, sizeof (stmf_i_remote_port_t), 291 offsetof(stmf_i_remote_port_t, irport_ln)); 292 stmf_state.stmf_ilport_inst_space = 293 id_space_create("lport-instances", 0, MAX_ILPORT); 294 stmf_state.stmf_irport_inst_space = 295 id_space_create("rport-instances", 0, MAX_IRPORT); 296 avl_create(&stmf_state.stmf_itl_kstat_list, 297 stmf_itl_kstat_compare, sizeof (stmf_i_itl_kstat_t), 298 offsetof(stmf_i_itl_kstat_t, iitl_kstat_ln)); 299 stmf_view_init(); 300 stmf_svc_init(); 301 stmf_dlun_init(); 302 return (ret); 303 } 304 305 int 306 _fini(void) 307 { 308 int ret; 309 stmf_i_remote_port_t *irport; 310 stmf_i_itl_kstat_t *ks_itl; 311 void *avl_dest_cookie = NULL; 312 313 if (stmf_state.stmf_service_running) 314 return (EBUSY); 315 if ((!stmf_allow_modunload) && 316 (stmf_state.stmf_config_state != STMF_CONFIG_NONE)) { 317 return (EBUSY); 318 } 319 if (stmf_state.stmf_nlps || stmf_state.stmf_npps) { 320 return (EBUSY); 321 } 322 if (stmf_dlun_fini() != STMF_SUCCESS) 323 return (EBUSY); 324 if (stmf_worker_fini() != STMF_SUCCESS) { 325 stmf_dlun_init(); 326 return (EBUSY); 327 } 328 if (stmf_svc_fini() != STMF_SUCCESS) { 329 stmf_dlun_init(); 330 stmf_worker_init(); 331 return (EBUSY); 332 } 333 334 ret = mod_remove(&modlinkage); 335 if (ret) { 336 stmf_svc_init(); 337 stmf_dlun_init(); 338 stmf_worker_init(); 339 return (ret); 340 } 341 342 stmf_view_clear_config(); 343 344 while ((irport = avl_destroy_nodes(&stmf_state.stmf_irportlist, 345 &avl_dest_cookie)) != NULL) 346 stmf_irport_destroy(irport); 347 avl_destroy(&stmf_state.stmf_irportlist); 348 id_space_destroy(stmf_state.stmf_ilport_inst_space); 349 id_space_destroy(stmf_state.stmf_irport_inst_space); 350 351 avl_dest_cookie = NULL; 352 while ((ks_itl = avl_destroy_nodes(&stmf_state.stmf_itl_kstat_list, 353 &avl_dest_cookie)) != NULL) { 354 stmf_teardown_itl_kstats(ks_itl); 355 kmem_free(ks_itl, sizeof (ks_itl)); 356 } 357 avl_destroy(&stmf_state.stmf_itl_kstat_list); 358 359 kmem_free(stmf_trace_buf, stmf_trace_buf_size); 360 mutex_destroy(&trace_buf_lock); 361 mutex_destroy(&stmf_state.stmf_lock); 362 cv_destroy(&stmf_state.stmf_cv); 363 return (ret); 364 } 365 366 int 367 _info(struct modinfo *modinfop) 368 { 369 return (mod_info(&modlinkage, modinfop)); 370 } 371 372 /* ARGSUSED */ 373 static int 374 stmf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 375 { 376 switch (cmd) { 377 case DDI_INFO_DEVT2DEVINFO: 378 *result = stmf_state.stmf_dip; 379 break; 380 case DDI_INFO_DEVT2INSTANCE: 381 *result = 382 (void *)(uintptr_t)ddi_get_instance(stmf_state.stmf_dip); 383 break; 384 default: 385 return (DDI_FAILURE); 386 } 387 388 return (DDI_SUCCESS); 389 } 390 391 static int 392 stmf_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 393 { 394 switch (cmd) { 395 case DDI_ATTACH: 396 stmf_state.stmf_dip = dip; 397 398 if (ddi_create_minor_node(dip, "admin", S_IFCHR, 0, 399 DDI_NT_STMF, 0) != DDI_SUCCESS) { 400 break; 401 } 402 ddi_report_dev(dip); 403 return (DDI_SUCCESS); 404 } 405 406 return (DDI_FAILURE); 407 } 408 409 static int 410 stmf_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 411 { 412 switch (cmd) { 413 case DDI_DETACH: 414 ddi_remove_minor_node(dip, 0); 415 return (DDI_SUCCESS); 416 } 417 418 return (DDI_FAILURE); 419 } 420 421 /* ARGSUSED */ 422 static int 423 stmf_open(dev_t *devp, int flag, int otype, cred_t *credp) 424 { 425 mutex_enter(&stmf_state.stmf_lock); 426 if (stmf_state.stmf_exclusive_open) { 427 mutex_exit(&stmf_state.stmf_lock); 428 return (EBUSY); 429 } 430 if (flag & FEXCL) { 431 if (stmf_state.stmf_opened) { 432 mutex_exit(&stmf_state.stmf_lock); 433 return (EBUSY); 434 } 435 stmf_state.stmf_exclusive_open = 1; 436 } 437 stmf_state.stmf_opened = 1; 438 mutex_exit(&stmf_state.stmf_lock); 439 return (0); 440 } 441 442 /* ARGSUSED */ 443 static int 444 stmf_close(dev_t dev, int flag, int otype, cred_t *credp) 445 { 446 mutex_enter(&stmf_state.stmf_lock); 447 stmf_state.stmf_opened = 0; 448 if (stmf_state.stmf_exclusive_open && 449 (stmf_state.stmf_config_state != STMF_CONFIG_INIT_DONE)) { 450 stmf_state.stmf_config_state = STMF_CONFIG_NONE; 451 stmf_delete_all_ppds(); 452 stmf_view_clear_config(); 453 stmf_view_init(); 454 } 455 stmf_state.stmf_exclusive_open = 0; 456 mutex_exit(&stmf_state.stmf_lock); 457 return (0); 458 } 459 460 int 461 stmf_copyin_iocdata(intptr_t data, int mode, stmf_iocdata_t **iocd, 462 void **ibuf, void **obuf) 463 { 464 int ret; 465 466 *ibuf = NULL; 467 *obuf = NULL; 468 *iocd = kmem_zalloc(sizeof (stmf_iocdata_t), KM_SLEEP); 469 470 ret = ddi_copyin((void *)data, *iocd, sizeof (stmf_iocdata_t), mode); 471 if (ret) 472 return (EFAULT); 473 if ((*iocd)->stmf_version != STMF_VERSION_1) { 474 ret = EINVAL; 475 goto copyin_iocdata_done; 476 } 477 if ((*iocd)->stmf_ibuf_size) { 478 *ibuf = kmem_zalloc((*iocd)->stmf_ibuf_size, KM_SLEEP); 479 ret = ddi_copyin((void *)((unsigned long)(*iocd)->stmf_ibuf), 480 *ibuf, (*iocd)->stmf_ibuf_size, mode); 481 } 482 if ((*iocd)->stmf_obuf_size) 483 *obuf = kmem_zalloc((*iocd)->stmf_obuf_size, KM_SLEEP); 484 485 if (ret == 0) 486 return (0); 487 ret = EFAULT; 488 copyin_iocdata_done:; 489 if (*obuf) { 490 kmem_free(*obuf, (*iocd)->stmf_obuf_size); 491 *obuf = NULL; 492 } 493 if (*ibuf) { 494 kmem_free(*ibuf, (*iocd)->stmf_ibuf_size); 495 *ibuf = NULL; 496 } 497 kmem_free(*iocd, sizeof (stmf_iocdata_t)); 498 return (ret); 499 } 500 501 int 502 stmf_copyout_iocdata(intptr_t data, int mode, stmf_iocdata_t *iocd, void *obuf) 503 { 504 int ret; 505 506 if (iocd->stmf_obuf_size) { 507 ret = ddi_copyout(obuf, (void *)(unsigned long)iocd->stmf_obuf, 508 iocd->stmf_obuf_size, mode); 509 if (ret) 510 return (EFAULT); 511 } 512 ret = ddi_copyout(iocd, (void *)data, sizeof (stmf_iocdata_t), mode); 513 if (ret) 514 return (EFAULT); 515 return (0); 516 } 517 518 /* ARGSUSED */ 519 static int 520 stmf_ioctl(dev_t dev, int cmd, intptr_t data, int mode, 521 cred_t *credp, int *rval) 522 { 523 stmf_iocdata_t *iocd; 524 void *ibuf = NULL, *obuf = NULL; 525 slist_lu_t *luid_list; 526 slist_target_port_t *lportid_list; 527 stmf_i_lu_t *ilu; 528 stmf_i_local_port_t *ilport; 529 stmf_i_scsi_session_t *iss; 530 slist_scsi_session_t *iss_list; 531 sioc_lu_props_t *lup; 532 sioc_target_port_props_t *lportp; 533 stmf_ppioctl_data_t *ppi, *ppi_out = NULL; 534 uint64_t *ppi_token = NULL; 535 uint8_t *p_id, *id; 536 stmf_state_desc_t *std; 537 stmf_status_t ctl_ret; 538 stmf_state_change_info_t ssi; 539 int ret = 0; 540 uint32_t n; 541 int i; 542 stmf_group_op_data_t *grp_entry; 543 stmf_group_name_t *grpname; 544 stmf_view_op_entry_t *ve; 545 stmf_id_type_t idtype; 546 stmf_id_data_t *id_entry; 547 stmf_id_list_t *id_list; 548 stmf_view_entry_t *view_entry; 549 uint32_t veid; 550 551 if ((cmd & 0xff000000) != STMF_IOCTL) { 552 return (ENOTTY); 553 } 554 555 if (drv_priv(credp) != 0) { 556 return (EPERM); 557 } 558 559 ret = stmf_copyin_iocdata(data, mode, &iocd, &ibuf, &obuf); 560 if (ret) 561 return (ret); 562 iocd->stmf_error = 0; 563 564 switch (cmd) { 565 case STMF_IOCTL_LU_LIST: 566 /* retrieves both registered/unregistered */ 567 mutex_enter(&stmf_state.stmf_lock); 568 id_list = &stmf_state.stmf_luid_list; 569 n = min(id_list->id_count, 570 (iocd->stmf_obuf_size)/sizeof (slist_lu_t)); 571 iocd->stmf_obuf_max_nentries = id_list->id_count; 572 luid_list = (slist_lu_t *)obuf; 573 id_entry = id_list->idl_head; 574 for (i = 0; i < n; i++) { 575 bcopy(id_entry->id_data, luid_list[i].lu_guid, 16); 576 id_entry = id_entry->id_next; 577 } 578 579 n = iocd->stmf_obuf_size/sizeof (slist_lu_t); 580 for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) { 581 id = (uint8_t *)ilu->ilu_lu->lu_id; 582 if (stmf_lookup_id(id_list, 16, id + 4) == NULL) { 583 iocd->stmf_obuf_max_nentries++; 584 if (i < n) { 585 bcopy(id + 4, luid_list[i].lu_guid, 586 sizeof (slist_lu_t)); 587 i++; 588 } 589 } 590 } 591 iocd->stmf_obuf_nentries = i; 592 mutex_exit(&stmf_state.stmf_lock); 593 break; 594 595 case STMF_IOCTL_REG_LU_LIST: 596 mutex_enter(&stmf_state.stmf_lock); 597 iocd->stmf_obuf_max_nentries = stmf_state.stmf_nlus; 598 n = min(stmf_state.stmf_nlus, 599 (iocd->stmf_obuf_size)/sizeof (slist_lu_t)); 600 iocd->stmf_obuf_nentries = n; 601 ilu = stmf_state.stmf_ilulist; 602 luid_list = (slist_lu_t *)obuf; 603 for (i = 0; i < n; i++) { 604 uint8_t *id; 605 id = (uint8_t *)ilu->ilu_lu->lu_id; 606 bcopy(id + 4, luid_list[i].lu_guid, 16); 607 ilu = ilu->ilu_next; 608 } 609 mutex_exit(&stmf_state.stmf_lock); 610 break; 611 612 case STMF_IOCTL_VE_LU_LIST: 613 mutex_enter(&stmf_state.stmf_lock); 614 id_list = &stmf_state.stmf_luid_list; 615 n = min(id_list->id_count, 616 (iocd->stmf_obuf_size)/sizeof (slist_lu_t)); 617 iocd->stmf_obuf_max_nentries = id_list->id_count; 618 iocd->stmf_obuf_nentries = n; 619 luid_list = (slist_lu_t *)obuf; 620 id_entry = id_list->idl_head; 621 for (i = 0; i < n; i++) { 622 bcopy(id_entry->id_data, luid_list[i].lu_guid, 16); 623 id_entry = id_entry->id_next; 624 } 625 mutex_exit(&stmf_state.stmf_lock); 626 break; 627 628 case STMF_IOCTL_TARGET_PORT_LIST: 629 mutex_enter(&stmf_state.stmf_lock); 630 iocd->stmf_obuf_max_nentries = stmf_state.stmf_nlports; 631 n = min(stmf_state.stmf_nlports, 632 (iocd->stmf_obuf_size)/sizeof (slist_target_port_t)); 633 iocd->stmf_obuf_nentries = n; 634 ilport = stmf_state.stmf_ilportlist; 635 lportid_list = (slist_target_port_t *)obuf; 636 for (i = 0; i < n; i++) { 637 uint8_t *id; 638 id = (uint8_t *)ilport->ilport_lport->lport_id; 639 bcopy(id, lportid_list[i].target, id[3] + 4); 640 ilport = ilport->ilport_next; 641 } 642 mutex_exit(&stmf_state.stmf_lock); 643 break; 644 645 case STMF_IOCTL_SESSION_LIST: 646 p_id = (uint8_t *)ibuf; 647 if ((p_id == NULL) || (iocd->stmf_ibuf_size < 4) || 648 (iocd->stmf_ibuf_size < (p_id[3] + 4))) { 649 ret = EINVAL; 650 break; 651 } 652 mutex_enter(&stmf_state.stmf_lock); 653 for (ilport = stmf_state.stmf_ilportlist; ilport; ilport = 654 ilport->ilport_next) { 655 uint8_t *id; 656 id = (uint8_t *)ilport->ilport_lport->lport_id; 657 if ((p_id[3] == id[3]) && 658 (bcmp(p_id + 4, id + 4, id[3]) == 0)) { 659 break; 660 } 661 } 662 if (ilport == NULL) { 663 mutex_exit(&stmf_state.stmf_lock); 664 ret = ENOENT; 665 break; 666 } 667 iocd->stmf_obuf_max_nentries = ilport->ilport_nsessions; 668 n = min(ilport->ilport_nsessions, 669 (iocd->stmf_obuf_size)/sizeof (slist_scsi_session_t)); 670 iocd->stmf_obuf_nentries = n; 671 iss = ilport->ilport_ss_list; 672 iss_list = (slist_scsi_session_t *)obuf; 673 for (i = 0; i < n; i++) { 674 uint8_t *id; 675 id = (uint8_t *)iss->iss_ss->ss_rport_id; 676 bcopy(id, iss_list[i].initiator, id[3] + 4); 677 iss_list[i].creation_time = (uint32_t) 678 iss->iss_creation_time; 679 if (iss->iss_ss->ss_rport_alias) { 680 (void) strncpy(iss_list[i].alias, 681 iss->iss_ss->ss_rport_alias, 255); 682 iss_list[i].alias[255] = 0; 683 } else { 684 iss_list[i].alias[0] = 0; 685 } 686 iss = iss->iss_next; 687 } 688 mutex_exit(&stmf_state.stmf_lock); 689 break; 690 691 case STMF_IOCTL_GET_LU_PROPERTIES: 692 p_id = (uint8_t *)ibuf; 693 if ((iocd->stmf_ibuf_size < 16) || 694 (iocd->stmf_obuf_size < sizeof (sioc_lu_props_t)) || 695 (p_id[0] == 0)) { 696 ret = EINVAL; 697 break; 698 } 699 mutex_enter(&stmf_state.stmf_lock); 700 for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) { 701 if (bcmp(p_id, ilu->ilu_lu->lu_id->ident, 16) == 0) 702 break; 703 } 704 if (ilu == NULL) { 705 mutex_exit(&stmf_state.stmf_lock); 706 ret = ENOENT; 707 break; 708 } 709 lup = (sioc_lu_props_t *)obuf; 710 bcopy(ilu->ilu_lu->lu_id->ident, lup->lu_guid, 16); 711 lup->lu_state = ilu->ilu_state & 0x0f; 712 lup->lu_present = 1; /* XXX */ 713 (void) strncpy(lup->lu_provider_name, 714 ilu->ilu_lu->lu_lp->lp_name, 255); 715 lup->lu_provider_name[254] = 0; 716 if (ilu->ilu_lu->lu_alias) { 717 (void) strncpy(lup->lu_alias, 718 ilu->ilu_lu->lu_alias, 255); 719 lup->lu_alias[255] = 0; 720 } else { 721 lup->lu_alias[0] = 0; 722 } 723 mutex_exit(&stmf_state.stmf_lock); 724 break; 725 726 case STMF_IOCTL_GET_TARGET_PORT_PROPERTIES: 727 p_id = (uint8_t *)ibuf; 728 if ((p_id == NULL) || 729 (iocd->stmf_ibuf_size < (p_id[3] + 4)) || 730 (iocd->stmf_obuf_size < 731 sizeof (sioc_target_port_props_t))) { 732 ret = EINVAL; 733 break; 734 } 735 mutex_enter(&stmf_state.stmf_lock); 736 for (ilport = stmf_state.stmf_ilportlist; ilport; 737 ilport = ilport->ilport_next) { 738 uint8_t *id; 739 id = (uint8_t *)ilport->ilport_lport->lport_id; 740 if ((p_id[3] == id[3]) && 741 (bcmp(p_id+4, id+4, id[3]) == 0)) 742 break; 743 } 744 if (ilport == NULL) { 745 mutex_exit(&stmf_state.stmf_lock); 746 ret = ENOENT; 747 break; 748 } 749 lportp = (sioc_target_port_props_t *)obuf; 750 bcopy(ilport->ilport_lport->lport_id, lportp->tgt_id, 751 ilport->ilport_lport->lport_id->ident_length + 4); 752 lportp->tgt_state = ilport->ilport_state & 0x0f; 753 lportp->tgt_present = 1; /* XXX */ 754 (void) strncpy(lportp->tgt_provider_name, 755 ilport->ilport_lport->lport_pp->pp_name, 255); 756 lportp->tgt_provider_name[254] = 0; 757 if (ilport->ilport_lport->lport_alias) { 758 (void) strncpy(lportp->tgt_alias, 759 ilport->ilport_lport->lport_alias, 255); 760 lportp->tgt_alias[255] = 0; 761 } else { 762 lportp->tgt_alias[0] = 0; 763 } 764 mutex_exit(&stmf_state.stmf_lock); 765 break; 766 767 case STMF_IOCTL_SET_STMF_STATE: 768 if ((ibuf == NULL) || 769 (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) { 770 ret = EINVAL; 771 break; 772 } 773 ret = stmf_set_stmf_state((stmf_state_desc_t *)ibuf); 774 break; 775 776 case STMF_IOCTL_GET_STMF_STATE: 777 if ((obuf == NULL) || 778 (iocd->stmf_obuf_size < sizeof (stmf_state_desc_t))) { 779 ret = EINVAL; 780 break; 781 } 782 ret = stmf_get_stmf_state((stmf_state_desc_t *)obuf); 783 break; 784 785 case STMF_IOCTL_SET_ALUA_STATE: 786 if ((ibuf == NULL) || 787 (iocd->stmf_ibuf_size < sizeof (stmf_alua_state_desc_t))) { 788 ret = EINVAL; 789 break; 790 } 791 ret = stmf_set_alua_state((stmf_alua_state_desc_t *)ibuf); 792 break; 793 794 case STMF_IOCTL_GET_ALUA_STATE: 795 if ((obuf == NULL) || 796 (iocd->stmf_obuf_size < sizeof (stmf_alua_state_desc_t))) { 797 ret = EINVAL; 798 break; 799 } 800 stmf_get_alua_state((stmf_alua_state_desc_t *)obuf); 801 break; 802 803 case STMF_IOCTL_SET_LU_STATE: 804 ssi.st_rflags = STMF_RFLAG_USER_REQUEST; 805 ssi.st_additional_info = NULL; 806 std = (stmf_state_desc_t *)ibuf; 807 if ((ibuf == NULL) || 808 (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) { 809 ret = EINVAL; 810 break; 811 } 812 p_id = std->ident; 813 mutex_enter(&stmf_state.stmf_lock); 814 if (stmf_state.stmf_inventory_locked) { 815 mutex_exit(&stmf_state.stmf_lock); 816 ret = EBUSY; 817 break; 818 } 819 for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) { 820 if (bcmp(p_id, ilu->ilu_lu->lu_id->ident, 16) == 0) 821 break; 822 } 823 if (ilu == NULL) { 824 mutex_exit(&stmf_state.stmf_lock); 825 ret = ENOENT; 826 break; 827 } 828 stmf_state.stmf_inventory_locked = 1; 829 mutex_exit(&stmf_state.stmf_lock); 830 cmd = (std->state == STMF_STATE_ONLINE) ? STMF_CMD_LU_ONLINE : 831 STMF_CMD_LU_OFFLINE; 832 ctl_ret = stmf_ctl(cmd, (void *)ilu->ilu_lu, &ssi); 833 if (ctl_ret == STMF_ALREADY) 834 ret = 0; 835 else if (ctl_ret == STMF_BUSY) 836 ret = EBUSY; 837 else if (ctl_ret != STMF_SUCCESS) 838 ret = EIO; 839 mutex_enter(&stmf_state.stmf_lock); 840 stmf_state.stmf_inventory_locked = 0; 841 mutex_exit(&stmf_state.stmf_lock); 842 break; 843 844 case STMF_IOCTL_SET_TARGET_PORT_STATE: 845 ssi.st_rflags = STMF_RFLAG_USER_REQUEST; 846 ssi.st_additional_info = NULL; 847 std = (stmf_state_desc_t *)ibuf; 848 if ((ibuf == NULL) || 849 (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) { 850 ret = EINVAL; 851 break; 852 } 853 p_id = std->ident; 854 mutex_enter(&stmf_state.stmf_lock); 855 if (stmf_state.stmf_inventory_locked) { 856 mutex_exit(&stmf_state.stmf_lock); 857 ret = EBUSY; 858 break; 859 } 860 for (ilport = stmf_state.stmf_ilportlist; ilport; 861 ilport = ilport->ilport_next) { 862 uint8_t *id; 863 id = (uint8_t *)ilport->ilport_lport->lport_id; 864 if ((id[3] == p_id[3]) && 865 (bcmp(id+4, p_id+4, id[3]) == 0)) { 866 break; 867 } 868 } 869 if (ilport == NULL) { 870 mutex_exit(&stmf_state.stmf_lock); 871 ret = ENOENT; 872 break; 873 } 874 stmf_state.stmf_inventory_locked = 1; 875 mutex_exit(&stmf_state.stmf_lock); 876 cmd = (std->state == STMF_STATE_ONLINE) ? 877 STMF_CMD_LPORT_ONLINE : STMF_CMD_LPORT_OFFLINE; 878 ctl_ret = stmf_ctl(cmd, (void *)ilport->ilport_lport, &ssi); 879 if (ctl_ret == STMF_ALREADY) 880 ret = 0; 881 else if (ctl_ret == STMF_BUSY) 882 ret = EBUSY; 883 else if (ctl_ret != STMF_SUCCESS) 884 ret = EIO; 885 mutex_enter(&stmf_state.stmf_lock); 886 stmf_state.stmf_inventory_locked = 0; 887 mutex_exit(&stmf_state.stmf_lock); 888 break; 889 890 case STMF_IOCTL_ADD_HG_ENTRY: 891 idtype = STMF_ID_TYPE_HOST; 892 /* FALLTHROUGH */ 893 case STMF_IOCTL_ADD_TG_ENTRY: 894 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 895 ret = EACCES; 896 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 897 break; 898 } 899 if (cmd == STMF_IOCTL_ADD_TG_ENTRY) { 900 idtype = STMF_ID_TYPE_TARGET; 901 } 902 grp_entry = (stmf_group_op_data_t *)ibuf; 903 if ((ibuf == NULL) || 904 (iocd->stmf_ibuf_size < sizeof (stmf_group_op_data_t))) { 905 ret = EINVAL; 906 break; 907 } 908 if (grp_entry->group.name[0] == '*') { 909 ret = EINVAL; 910 break; /* not allowed */ 911 } 912 mutex_enter(&stmf_state.stmf_lock); 913 ret = stmf_add_group_member(grp_entry->group.name, 914 grp_entry->group.name_size, 915 grp_entry->ident + 4, 916 grp_entry->ident[3], 917 idtype, 918 &iocd->stmf_error); 919 mutex_exit(&stmf_state.stmf_lock); 920 break; 921 case STMF_IOCTL_REMOVE_HG_ENTRY: 922 idtype = STMF_ID_TYPE_HOST; 923 /* FALLTHROUGH */ 924 case STMF_IOCTL_REMOVE_TG_ENTRY: 925 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 926 ret = EACCES; 927 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 928 break; 929 } 930 if (cmd == STMF_IOCTL_REMOVE_TG_ENTRY) { 931 idtype = STMF_ID_TYPE_TARGET; 932 } 933 grp_entry = (stmf_group_op_data_t *)ibuf; 934 if ((ibuf == NULL) || 935 (iocd->stmf_ibuf_size < sizeof (stmf_group_op_data_t))) { 936 ret = EINVAL; 937 break; 938 } 939 if (grp_entry->group.name[0] == '*') { 940 ret = EINVAL; 941 break; /* not allowed */ 942 } 943 mutex_enter(&stmf_state.stmf_lock); 944 ret = stmf_remove_group_member(grp_entry->group.name, 945 grp_entry->group.name_size, 946 grp_entry->ident + 4, 947 grp_entry->ident[3], 948 idtype, 949 &iocd->stmf_error); 950 mutex_exit(&stmf_state.stmf_lock); 951 break; 952 case STMF_IOCTL_CREATE_HOST_GROUP: 953 idtype = STMF_ID_TYPE_HOST_GROUP; 954 /* FALLTHROUGH */ 955 case STMF_IOCTL_CREATE_TARGET_GROUP: 956 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 957 ret = EACCES; 958 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 959 break; 960 } 961 grpname = (stmf_group_name_t *)ibuf; 962 963 if (cmd == STMF_IOCTL_CREATE_TARGET_GROUP) 964 idtype = STMF_ID_TYPE_TARGET_GROUP; 965 if ((ibuf == NULL) || 966 (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) { 967 ret = EINVAL; 968 break; 969 } 970 if (grpname->name[0] == '*') { 971 ret = EINVAL; 972 break; /* not allowed */ 973 } 974 mutex_enter(&stmf_state.stmf_lock); 975 ret = stmf_add_group(grpname->name, 976 grpname->name_size, idtype, &iocd->stmf_error); 977 mutex_exit(&stmf_state.stmf_lock); 978 break; 979 case STMF_IOCTL_REMOVE_HOST_GROUP: 980 idtype = STMF_ID_TYPE_HOST_GROUP; 981 /* FALLTHROUGH */ 982 case STMF_IOCTL_REMOVE_TARGET_GROUP: 983 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 984 ret = EACCES; 985 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 986 break; 987 } 988 grpname = (stmf_group_name_t *)ibuf; 989 if (cmd == STMF_IOCTL_REMOVE_TARGET_GROUP) 990 idtype = STMF_ID_TYPE_TARGET_GROUP; 991 if ((ibuf == NULL) || 992 (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) { 993 ret = EINVAL; 994 break; 995 } 996 if (grpname->name[0] == '*') { 997 ret = EINVAL; 998 break; /* not allowed */ 999 } 1000 mutex_enter(&stmf_state.stmf_lock); 1001 ret = stmf_remove_group(grpname->name, 1002 grpname->name_size, idtype, &iocd->stmf_error); 1003 mutex_exit(&stmf_state.stmf_lock); 1004 break; 1005 case STMF_IOCTL_VALIDATE_VIEW: 1006 case STMF_IOCTL_ADD_VIEW_ENTRY: 1007 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 1008 ret = EACCES; 1009 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 1010 break; 1011 } 1012 ve = (stmf_view_op_entry_t *)ibuf; 1013 if ((ibuf == NULL) || 1014 (iocd->stmf_ibuf_size < sizeof (stmf_view_op_entry_t))) { 1015 ret = EINVAL; 1016 break; 1017 } 1018 if (!ve->ve_lu_number_valid) 1019 ve->ve_lu_nbr[2] = 0xFF; 1020 if (ve->ve_all_hosts) { 1021 ve->ve_host_group.name[0] = '*'; 1022 ve->ve_host_group.name_size = 1; 1023 } 1024 if (ve->ve_all_targets) { 1025 ve->ve_target_group.name[0] = '*'; 1026 ve->ve_target_group.name_size = 1; 1027 } 1028 if (ve->ve_ndx_valid) 1029 veid = ve->ve_ndx; 1030 else 1031 veid = 0xffffffff; 1032 mutex_enter(&stmf_state.stmf_lock); 1033 if (cmd == STMF_IOCTL_ADD_VIEW_ENTRY) { 1034 ret = stmf_add_ve(ve->ve_host_group.name, 1035 ve->ve_host_group.name_size, 1036 ve->ve_target_group.name, 1037 ve->ve_target_group.name_size, 1038 ve->ve_guid, 1039 &veid, 1040 ve->ve_lu_nbr, 1041 &iocd->stmf_error); 1042 } else { /* STMF_IOCTL_VALIDATE_VIEW */ 1043 ret = stmf_validate_lun_ve(ve->ve_host_group.name, 1044 ve->ve_host_group.name_size, 1045 ve->ve_target_group.name, 1046 ve->ve_target_group.name_size, 1047 ve->ve_lu_nbr, 1048 &iocd->stmf_error); 1049 } 1050 mutex_exit(&stmf_state.stmf_lock); 1051 if (ret == 0 && 1052 (!ve->ve_ndx_valid || !ve->ve_lu_number_valid) && 1053 iocd->stmf_obuf_size >= sizeof (stmf_view_op_entry_t)) { 1054 stmf_view_op_entry_t *ve_ret = 1055 (stmf_view_op_entry_t *)obuf; 1056 iocd->stmf_obuf_nentries = 1; 1057 iocd->stmf_obuf_max_nentries = 1; 1058 if (!ve->ve_ndx_valid) { 1059 ve_ret->ve_ndx = veid; 1060 ve_ret->ve_ndx_valid = 1; 1061 } 1062 if (!ve->ve_lu_number_valid) { 1063 ve_ret->ve_lu_number_valid = 1; 1064 bcopy(ve->ve_lu_nbr, ve_ret->ve_lu_nbr, 8); 1065 } 1066 } 1067 break; 1068 case STMF_IOCTL_REMOVE_VIEW_ENTRY: 1069 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 1070 ret = EACCES; 1071 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 1072 break; 1073 } 1074 ve = (stmf_view_op_entry_t *)ibuf; 1075 if ((ibuf == NULL) || 1076 (iocd->stmf_ibuf_size < sizeof (stmf_view_op_entry_t))) { 1077 ret = EINVAL; 1078 break; 1079 } 1080 if (!ve->ve_ndx_valid) { 1081 ret = EINVAL; 1082 break; 1083 } 1084 mutex_enter(&stmf_state.stmf_lock); 1085 ret = stmf_remove_ve_by_id(ve->ve_guid, ve->ve_ndx, 1086 &iocd->stmf_error); 1087 mutex_exit(&stmf_state.stmf_lock); 1088 break; 1089 case STMF_IOCTL_GET_HG_LIST: 1090 id_list = &stmf_state.stmf_hg_list; 1091 /* FALLTHROUGH */ 1092 case STMF_IOCTL_GET_TG_LIST: 1093 if (cmd == STMF_IOCTL_GET_TG_LIST) 1094 id_list = &stmf_state.stmf_tg_list; 1095 mutex_enter(&stmf_state.stmf_lock); 1096 iocd->stmf_obuf_max_nentries = id_list->id_count; 1097 n = min(id_list->id_count, 1098 (iocd->stmf_obuf_size)/sizeof (stmf_group_name_t)); 1099 iocd->stmf_obuf_nentries = n; 1100 id_entry = id_list->idl_head; 1101 grpname = (stmf_group_name_t *)obuf; 1102 for (i = 0; i < n; i++) { 1103 if (id_entry->id_data[0] == '*') { 1104 if (iocd->stmf_obuf_nentries > 0) { 1105 iocd->stmf_obuf_nentries--; 1106 } 1107 id_entry = id_entry->id_next; 1108 continue; 1109 } 1110 grpname->name_size = id_entry->id_data_size; 1111 bcopy(id_entry->id_data, grpname->name, 1112 id_entry->id_data_size); 1113 grpname++; 1114 id_entry = id_entry->id_next; 1115 } 1116 mutex_exit(&stmf_state.stmf_lock); 1117 break; 1118 case STMF_IOCTL_GET_HG_ENTRIES: 1119 id_list = &stmf_state.stmf_hg_list; 1120 /* FALLTHROUGH */ 1121 case STMF_IOCTL_GET_TG_ENTRIES: 1122 grpname = (stmf_group_name_t *)ibuf; 1123 if ((ibuf == NULL) || 1124 (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) { 1125 ret = EINVAL; 1126 break; 1127 } 1128 if (cmd == STMF_IOCTL_GET_TG_ENTRIES) { 1129 id_list = &stmf_state.stmf_tg_list; 1130 } 1131 mutex_enter(&stmf_state.stmf_lock); 1132 id_entry = stmf_lookup_id(id_list, grpname->name_size, 1133 grpname->name); 1134 if (!id_entry) 1135 ret = ENODEV; 1136 else { 1137 stmf_ge_ident_t *grp_entry; 1138 id_list = (stmf_id_list_t *)id_entry->id_impl_specific; 1139 iocd->stmf_obuf_max_nentries = id_list->id_count; 1140 n = min(id_list->id_count, 1141 iocd->stmf_obuf_size/sizeof (stmf_ge_ident_t)); 1142 iocd->stmf_obuf_nentries = n; 1143 id_entry = id_list->idl_head; 1144 grp_entry = (stmf_ge_ident_t *)obuf; 1145 for (i = 0; i < n; i++) { 1146 bcopy(id_entry->id_data, grp_entry->ident, 1147 id_entry->id_data_size); 1148 grp_entry->ident_size = id_entry->id_data_size; 1149 id_entry = id_entry->id_next; 1150 grp_entry++; 1151 } 1152 } 1153 mutex_exit(&stmf_state.stmf_lock); 1154 break; 1155 1156 case STMF_IOCTL_GET_VE_LIST: 1157 n = iocd->stmf_obuf_size/sizeof (stmf_view_op_entry_t); 1158 mutex_enter(&stmf_state.stmf_lock); 1159 ve = (stmf_view_op_entry_t *)obuf; 1160 for (id_entry = stmf_state.stmf_luid_list.idl_head; 1161 id_entry; id_entry = id_entry->id_next) { 1162 for (view_entry = (stmf_view_entry_t *) 1163 id_entry->id_impl_specific; view_entry; 1164 view_entry = view_entry->ve_next) { 1165 iocd->stmf_obuf_max_nentries++; 1166 if (iocd->stmf_obuf_nentries >= n) 1167 continue; 1168 ve->ve_ndx_valid = 1; 1169 ve->ve_ndx = view_entry->ve_id; 1170 ve->ve_lu_number_valid = 1; 1171 bcopy(view_entry->ve_lun, ve->ve_lu_nbr, 8); 1172 bcopy(view_entry->ve_luid->id_data, ve->ve_guid, 1173 view_entry->ve_luid->id_data_size); 1174 if (view_entry->ve_hg->id_data[0] == '*') { 1175 ve->ve_all_hosts = 1; 1176 } else { 1177 bcopy(view_entry->ve_hg->id_data, 1178 ve->ve_host_group.name, 1179 view_entry->ve_hg->id_data_size); 1180 ve->ve_host_group.name_size = 1181 view_entry->ve_hg->id_data_size; 1182 } 1183 1184 if (view_entry->ve_tg->id_data[0] == '*') { 1185 ve->ve_all_targets = 1; 1186 } else { 1187 bcopy(view_entry->ve_tg->id_data, 1188 ve->ve_target_group.name, 1189 view_entry->ve_tg->id_data_size); 1190 ve->ve_target_group.name_size = 1191 view_entry->ve_tg->id_data_size; 1192 } 1193 ve++; 1194 iocd->stmf_obuf_nentries++; 1195 } 1196 } 1197 mutex_exit(&stmf_state.stmf_lock); 1198 break; 1199 1200 case STMF_IOCTL_LU_VE_LIST: 1201 p_id = (uint8_t *)ibuf; 1202 if ((iocd->stmf_ibuf_size != 16) || 1203 (iocd->stmf_obuf_size < sizeof (stmf_view_op_entry_t))) { 1204 ret = EINVAL; 1205 break; 1206 } 1207 1208 n = iocd->stmf_obuf_size/sizeof (stmf_view_op_entry_t); 1209 mutex_enter(&stmf_state.stmf_lock); 1210 ve = (stmf_view_op_entry_t *)obuf; 1211 for (id_entry = stmf_state.stmf_luid_list.idl_head; 1212 id_entry; id_entry = id_entry->id_next) { 1213 if (bcmp(id_entry->id_data, p_id, 16) != 0) 1214 continue; 1215 for (view_entry = (stmf_view_entry_t *) 1216 id_entry->id_impl_specific; view_entry; 1217 view_entry = view_entry->ve_next) { 1218 iocd->stmf_obuf_max_nentries++; 1219 if (iocd->stmf_obuf_nentries >= n) 1220 continue; 1221 ve->ve_ndx_valid = 1; 1222 ve->ve_ndx = view_entry->ve_id; 1223 ve->ve_lu_number_valid = 1; 1224 bcopy(view_entry->ve_lun, ve->ve_lu_nbr, 8); 1225 bcopy(view_entry->ve_luid->id_data, ve->ve_guid, 1226 view_entry->ve_luid->id_data_size); 1227 if (view_entry->ve_hg->id_data[0] == '*') { 1228 ve->ve_all_hosts = 1; 1229 } else { 1230 bcopy(view_entry->ve_hg->id_data, 1231 ve->ve_host_group.name, 1232 view_entry->ve_hg->id_data_size); 1233 ve->ve_host_group.name_size = 1234 view_entry->ve_hg->id_data_size; 1235 } 1236 1237 if (view_entry->ve_tg->id_data[0] == '*') { 1238 ve->ve_all_targets = 1; 1239 } else { 1240 bcopy(view_entry->ve_tg->id_data, 1241 ve->ve_target_group.name, 1242 view_entry->ve_tg->id_data_size); 1243 ve->ve_target_group.name_size = 1244 view_entry->ve_tg->id_data_size; 1245 } 1246 ve++; 1247 iocd->stmf_obuf_nentries++; 1248 } 1249 break; 1250 } 1251 mutex_exit(&stmf_state.stmf_lock); 1252 break; 1253 1254 case STMF_IOCTL_LOAD_PP_DATA: 1255 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 1256 ret = EACCES; 1257 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 1258 break; 1259 } 1260 ppi = (stmf_ppioctl_data_t *)ibuf; 1261 if ((ppi == NULL) || 1262 (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) { 1263 ret = EINVAL; 1264 break; 1265 } 1266 /* returned token */ 1267 ppi_token = (uint64_t *)obuf; 1268 if ((ppi_token == NULL) || 1269 (iocd->stmf_obuf_size < sizeof (uint64_t))) { 1270 ret = EINVAL; 1271 break; 1272 } 1273 ret = stmf_load_ppd_ioctl(ppi, ppi_token, &iocd->stmf_error); 1274 break; 1275 1276 case STMF_IOCTL_GET_PP_DATA: 1277 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 1278 ret = EACCES; 1279 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 1280 break; 1281 } 1282 ppi = (stmf_ppioctl_data_t *)ibuf; 1283 if (ppi == NULL || 1284 (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) { 1285 ret = EINVAL; 1286 break; 1287 } 1288 ppi_out = (stmf_ppioctl_data_t *)obuf; 1289 if ((ppi_out == NULL) || 1290 (iocd->stmf_obuf_size < sizeof (stmf_ppioctl_data_t))) { 1291 ret = EINVAL; 1292 break; 1293 } 1294 ret = stmf_get_ppd_ioctl(ppi, ppi_out, &iocd->stmf_error); 1295 break; 1296 1297 case STMF_IOCTL_CLEAR_PP_DATA: 1298 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 1299 ret = EACCES; 1300 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 1301 break; 1302 } 1303 ppi = (stmf_ppioctl_data_t *)ibuf; 1304 if ((ppi == NULL) || 1305 (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) { 1306 ret = EINVAL; 1307 break; 1308 } 1309 ret = stmf_delete_ppd_ioctl(ppi); 1310 break; 1311 1312 case STMF_IOCTL_CLEAR_TRACE: 1313 stmf_trace_clear(); 1314 break; 1315 1316 case STMF_IOCTL_ADD_TRACE: 1317 if (iocd->stmf_ibuf_size && ibuf) { 1318 ((uint8_t *)ibuf)[iocd->stmf_ibuf_size - 1] = 0; 1319 stmf_trace("\nstradm", "%s\n", ibuf); 1320 } 1321 break; 1322 1323 case STMF_IOCTL_GET_TRACE_POSITION: 1324 if (obuf && (iocd->stmf_obuf_size > 3)) { 1325 mutex_enter(&trace_buf_lock); 1326 *((int *)obuf) = trace_buf_curndx; 1327 mutex_exit(&trace_buf_lock); 1328 } else { 1329 ret = EINVAL; 1330 } 1331 break; 1332 1333 case STMF_IOCTL_GET_TRACE: 1334 if ((iocd->stmf_obuf_size == 0) || (iocd->stmf_ibuf_size < 4)) { 1335 ret = EINVAL; 1336 break; 1337 } 1338 i = *((int *)ibuf); 1339 if ((i > trace_buf_size) || ((i + iocd->stmf_obuf_size) > 1340 trace_buf_size)) { 1341 ret = EINVAL; 1342 break; 1343 } 1344 mutex_enter(&trace_buf_lock); 1345 bcopy(stmf_trace_buf + i, obuf, iocd->stmf_obuf_size); 1346 mutex_exit(&trace_buf_lock); 1347 break; 1348 1349 default: 1350 ret = ENOTTY; 1351 } 1352 1353 if (ret == 0) { 1354 ret = stmf_copyout_iocdata(data, mode, iocd, obuf); 1355 } else if (iocd->stmf_error) { 1356 (void) stmf_copyout_iocdata(data, mode, iocd, obuf); 1357 } 1358 if (obuf) { 1359 kmem_free(obuf, iocd->stmf_obuf_size); 1360 obuf = NULL; 1361 } 1362 if (ibuf) { 1363 kmem_free(ibuf, iocd->stmf_ibuf_size); 1364 ibuf = NULL; 1365 } 1366 kmem_free(iocd, sizeof (stmf_iocdata_t)); 1367 return (ret); 1368 } 1369 1370 static int 1371 stmf_get_service_state() 1372 { 1373 stmf_i_local_port_t *ilport; 1374 stmf_i_lu_t *ilu; 1375 int online = 0; 1376 int offline = 0; 1377 int onlining = 0; 1378 int offlining = 0; 1379 1380 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 1381 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 1382 ilport = ilport->ilport_next) { 1383 if (ilport->ilport_state == STMF_STATE_OFFLINE) 1384 offline++; 1385 else if (ilport->ilport_state == STMF_STATE_ONLINE) 1386 online++; 1387 else if (ilport->ilport_state == STMF_STATE_ONLINING) 1388 onlining++; 1389 else if (ilport->ilport_state == STMF_STATE_OFFLINING) 1390 offlining++; 1391 } 1392 1393 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; 1394 ilu = ilu->ilu_next) { 1395 if (ilu->ilu_state == STMF_STATE_OFFLINE) 1396 offline++; 1397 else if (ilu->ilu_state == STMF_STATE_ONLINE) 1398 online++; 1399 else if (ilu->ilu_state == STMF_STATE_ONLINING) 1400 onlining++; 1401 else if (ilu->ilu_state == STMF_STATE_OFFLINING) 1402 offlining++; 1403 } 1404 1405 if (stmf_state.stmf_service_running) { 1406 if (onlining) 1407 return (STMF_STATE_ONLINING); 1408 else 1409 return (STMF_STATE_ONLINE); 1410 } 1411 1412 if (offlining) { 1413 return (STMF_STATE_OFFLINING); 1414 } 1415 1416 return (STMF_STATE_OFFLINE); 1417 } 1418 1419 static int 1420 stmf_set_stmf_state(stmf_state_desc_t *std) 1421 { 1422 stmf_i_local_port_t *ilport; 1423 stmf_i_lu_t *ilu; 1424 stmf_state_change_info_t ssi; 1425 int svc_state; 1426 1427 ssi.st_rflags = STMF_RFLAG_USER_REQUEST; 1428 ssi.st_additional_info = NULL; 1429 1430 mutex_enter(&stmf_state.stmf_lock); 1431 if (!stmf_state.stmf_exclusive_open) { 1432 mutex_exit(&stmf_state.stmf_lock); 1433 return (EACCES); 1434 } 1435 1436 if (stmf_state.stmf_inventory_locked) { 1437 mutex_exit(&stmf_state.stmf_lock); 1438 return (EBUSY); 1439 } 1440 1441 if ((std->state != STMF_STATE_ONLINE) && 1442 (std->state != STMF_STATE_OFFLINE)) { 1443 mutex_exit(&stmf_state.stmf_lock); 1444 return (EINVAL); 1445 } 1446 1447 svc_state = stmf_get_service_state(); 1448 if ((svc_state == STMF_STATE_OFFLINING) || 1449 (svc_state == STMF_STATE_ONLINING)) { 1450 mutex_exit(&stmf_state.stmf_lock); 1451 return (EBUSY); 1452 } 1453 1454 if (svc_state == STMF_STATE_OFFLINE) { 1455 if (std->config_state == STMF_CONFIG_INIT) { 1456 if (std->state != STMF_STATE_OFFLINE) { 1457 mutex_exit(&stmf_state.stmf_lock); 1458 return (EINVAL); 1459 } 1460 stmf_state.stmf_config_state = STMF_CONFIG_INIT; 1461 stmf_delete_all_ppds(); 1462 stmf_view_clear_config(); 1463 stmf_view_init(); 1464 mutex_exit(&stmf_state.stmf_lock); 1465 return (0); 1466 } 1467 if ((stmf_state.stmf_config_state == STMF_CONFIG_INIT) || 1468 (stmf_state.stmf_config_state == STMF_CONFIG_NONE)) { 1469 if (std->config_state != STMF_CONFIG_INIT_DONE) { 1470 mutex_exit(&stmf_state.stmf_lock); 1471 return (EINVAL); 1472 } 1473 stmf_state.stmf_config_state = STMF_CONFIG_INIT_DONE; 1474 } 1475 if (std->state == STMF_STATE_OFFLINE) { 1476 mutex_exit(&stmf_state.stmf_lock); 1477 return (0); 1478 } 1479 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) { 1480 mutex_exit(&stmf_state.stmf_lock); 1481 return (EINVAL); 1482 } 1483 stmf_state.stmf_inventory_locked = 1; 1484 stmf_state.stmf_service_running = 1; 1485 mutex_exit(&stmf_state.stmf_lock); 1486 1487 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 1488 ilport = ilport->ilport_next) { 1489 if (ilport->ilport_prev_state != STMF_STATE_ONLINE) 1490 continue; 1491 (void) stmf_ctl(STMF_CMD_LPORT_ONLINE, 1492 ilport->ilport_lport, &ssi); 1493 } 1494 1495 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; 1496 ilu = ilu->ilu_next) { 1497 if (ilu->ilu_prev_state != STMF_STATE_ONLINE) 1498 continue; 1499 (void) stmf_ctl(STMF_CMD_LU_ONLINE, ilu->ilu_lu, &ssi); 1500 } 1501 mutex_enter(&stmf_state.stmf_lock); 1502 stmf_state.stmf_inventory_locked = 0; 1503 mutex_exit(&stmf_state.stmf_lock); 1504 return (0); 1505 } 1506 1507 /* svc_state is STMF_STATE_ONLINE here */ 1508 if ((std->state != STMF_STATE_OFFLINE) || 1509 (std->config_state == STMF_CONFIG_INIT)) { 1510 mutex_exit(&stmf_state.stmf_lock); 1511 return (EACCES); 1512 } 1513 1514 stmf_state.stmf_inventory_locked = 1; 1515 stmf_state.stmf_service_running = 0; 1516 1517 mutex_exit(&stmf_state.stmf_lock); 1518 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 1519 ilport = ilport->ilport_next) { 1520 if (ilport->ilport_state != STMF_STATE_ONLINE) 1521 continue; 1522 (void) stmf_ctl(STMF_CMD_LPORT_OFFLINE, 1523 ilport->ilport_lport, &ssi); 1524 } 1525 1526 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; 1527 ilu = ilu->ilu_next) { 1528 if (ilu->ilu_state != STMF_STATE_ONLINE) 1529 continue; 1530 (void) stmf_ctl(STMF_CMD_LU_OFFLINE, ilu->ilu_lu, &ssi); 1531 } 1532 mutex_enter(&stmf_state.stmf_lock); 1533 stmf_state.stmf_inventory_locked = 0; 1534 mutex_exit(&stmf_state.stmf_lock); 1535 return (0); 1536 } 1537 1538 static int 1539 stmf_get_stmf_state(stmf_state_desc_t *std) 1540 { 1541 mutex_enter(&stmf_state.stmf_lock); 1542 std->state = stmf_get_service_state(); 1543 std->config_state = stmf_state.stmf_config_state; 1544 mutex_exit(&stmf_state.stmf_lock); 1545 1546 return (0); 1547 } 1548 1549 /* 1550 * handles registration message from pppt for a logical unit 1551 */ 1552 stmf_status_t 1553 stmf_ic_lu_reg(stmf_ic_reg_dereg_lun_msg_t *msg, uint32_t type) 1554 { 1555 stmf_i_lu_provider_t *ilp; 1556 stmf_lu_provider_t *lp; 1557 mutex_enter(&stmf_state.stmf_lock); 1558 for (ilp = stmf_state.stmf_ilplist; ilp != NULL; ilp = ilp->ilp_next) { 1559 if (strcmp(msg->icrl_lu_provider_name, 1560 ilp->ilp_lp->lp_name) == 0) { 1561 lp = ilp->ilp_lp; 1562 mutex_exit(&stmf_state.stmf_lock); 1563 lp->lp_proxy_msg(msg->icrl_lun_id, msg->icrl_cb_arg, 1564 msg->icrl_cb_arg_len, type); 1565 return (STMF_SUCCESS); 1566 } 1567 } 1568 mutex_exit(&stmf_state.stmf_lock); 1569 return (STMF_SUCCESS); 1570 } 1571 1572 /* 1573 * handles de-registration message from pppt for a logical unit 1574 */ 1575 stmf_status_t 1576 stmf_ic_lu_dereg(stmf_ic_reg_dereg_lun_msg_t *msg) 1577 { 1578 stmf_i_lu_provider_t *ilp; 1579 stmf_lu_provider_t *lp; 1580 mutex_enter(&stmf_state.stmf_lock); 1581 for (ilp = stmf_state.stmf_ilplist; ilp != NULL; ilp = ilp->ilp_next) { 1582 if (strcmp(msg->icrl_lu_provider_name, 1583 ilp->ilp_lp->lp_name) == 0) { 1584 lp = ilp->ilp_lp; 1585 mutex_exit(&stmf_state.stmf_lock); 1586 lp->lp_proxy_msg(msg->icrl_lun_id, NULL, 0, 1587 STMF_MSG_LU_DEREGISTER); 1588 return (STMF_SUCCESS); 1589 } 1590 } 1591 mutex_exit(&stmf_state.stmf_lock); 1592 return (STMF_SUCCESS); 1593 } 1594 1595 /* 1596 * helper function to find a task that matches a task_msgid 1597 */ 1598 scsi_task_t * 1599 find_task_from_msgid(uint8_t *lu_id, stmf_ic_msgid_t task_msgid) 1600 { 1601 stmf_i_lu_t *ilu; 1602 stmf_i_scsi_task_t *itask; 1603 1604 mutex_enter(&stmf_state.stmf_lock); 1605 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) { 1606 if (bcmp(lu_id, ilu->ilu_lu->lu_id->ident, 16) == 0) { 1607 break; 1608 } 1609 } 1610 1611 if (ilu == NULL) { 1612 mutex_exit(&stmf_state.stmf_lock); 1613 return (NULL); 1614 } 1615 1616 mutex_enter(&ilu->ilu_task_lock); 1617 for (itask = ilu->ilu_tasks; itask != NULL; 1618 itask = itask->itask_lu_next) { 1619 if (itask->itask_flags & (ITASK_IN_FREE_LIST | 1620 ITASK_BEING_ABORTED)) { 1621 continue; 1622 } 1623 if (itask->itask_proxy_msg_id == task_msgid) { 1624 break; 1625 } 1626 } 1627 mutex_exit(&ilu->ilu_task_lock); 1628 mutex_exit(&stmf_state.stmf_lock); 1629 1630 if (itask != NULL) { 1631 return (itask->itask_task); 1632 } else { 1633 /* task not found. Likely already aborted. */ 1634 return (NULL); 1635 } 1636 } 1637 1638 /* 1639 * message received from pppt/ic 1640 */ 1641 stmf_status_t 1642 stmf_msg_rx(stmf_ic_msg_t *msg) 1643 { 1644 mutex_enter(&stmf_state.stmf_lock); 1645 if (stmf_state.stmf_alua_state != 1) { 1646 mutex_exit(&stmf_state.stmf_lock); 1647 cmn_err(CE_WARN, "stmf alua state is disabled"); 1648 ic_msg_free(msg); 1649 return (STMF_FAILURE); 1650 } 1651 mutex_exit(&stmf_state.stmf_lock); 1652 1653 switch (msg->icm_msg_type) { 1654 case STMF_ICM_REGISTER_LUN: 1655 (void) stmf_ic_lu_reg( 1656 (stmf_ic_reg_dereg_lun_msg_t *)msg->icm_msg, 1657 STMF_MSG_LU_REGISTER); 1658 break; 1659 case STMF_ICM_LUN_ACTIVE: 1660 (void) stmf_ic_lu_reg( 1661 (stmf_ic_reg_dereg_lun_msg_t *)msg->icm_msg, 1662 STMF_MSG_LU_ACTIVE); 1663 break; 1664 case STMF_ICM_DEREGISTER_LUN: 1665 (void) stmf_ic_lu_dereg( 1666 (stmf_ic_reg_dereg_lun_msg_t *)msg->icm_msg); 1667 break; 1668 case STMF_ICM_SCSI_DATA: 1669 (void) stmf_ic_rx_scsi_data( 1670 (stmf_ic_scsi_data_msg_t *)msg->icm_msg); 1671 break; 1672 case STMF_ICM_SCSI_STATUS: 1673 (void) stmf_ic_rx_scsi_status( 1674 (stmf_ic_scsi_status_msg_t *)msg->icm_msg); 1675 break; 1676 case STMF_ICM_STATUS: 1677 (void) stmf_ic_rx_status( 1678 (stmf_ic_status_msg_t *)msg->icm_msg); 1679 break; 1680 default: 1681 cmn_err(CE_WARN, "unknown message received %d", 1682 msg->icm_msg_type); 1683 ic_msg_free(msg); 1684 return (STMF_FAILURE); 1685 } 1686 ic_msg_free(msg); 1687 return (STMF_SUCCESS); 1688 } 1689 1690 stmf_status_t 1691 stmf_ic_rx_status(stmf_ic_status_msg_t *msg) 1692 { 1693 stmf_i_local_port_t *ilport; 1694 1695 if (msg->ics_msg_type != STMF_ICM_REGISTER_PROXY_PORT) { 1696 /* for now, ignore other message status */ 1697 return (STMF_SUCCESS); 1698 } 1699 1700 if (msg->ics_status != STMF_SUCCESS) { 1701 return (STMF_SUCCESS); 1702 } 1703 1704 mutex_enter(&stmf_state.stmf_lock); 1705 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 1706 ilport = ilport->ilport_next) { 1707 if (msg->ics_msgid == ilport->ilport_reg_msgid) { 1708 ilport->ilport_proxy_registered = 1; 1709 break; 1710 } 1711 } 1712 mutex_exit(&stmf_state.stmf_lock); 1713 return (STMF_SUCCESS); 1714 } 1715 1716 /* 1717 * handles scsi status message from pppt 1718 */ 1719 stmf_status_t 1720 stmf_ic_rx_scsi_status(stmf_ic_scsi_status_msg_t *msg) 1721 { 1722 scsi_task_t *task; 1723 1724 /* is this a task management command */ 1725 if (msg->icss_task_msgid & MSG_ID_TM_BIT) { 1726 return (STMF_SUCCESS); 1727 } 1728 1729 task = find_task_from_msgid(msg->icss_lun_id, msg->icss_task_msgid); 1730 1731 if (task == NULL) { 1732 return (STMF_SUCCESS); 1733 } 1734 1735 task->task_scsi_status = msg->icss_status; 1736 task->task_sense_data = msg->icss_sense; 1737 task->task_sense_length = msg->icss_sense_len; 1738 (void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE); 1739 1740 return (STMF_SUCCESS); 1741 } 1742 1743 /* 1744 * handles scsi data message from pppt 1745 */ 1746 stmf_status_t 1747 stmf_ic_rx_scsi_data(stmf_ic_scsi_data_msg_t *msg) 1748 { 1749 stmf_i_scsi_task_t *itask; 1750 scsi_task_t *task; 1751 stmf_xfer_data_t *xd = NULL; 1752 stmf_data_buf_t *dbuf; 1753 uint32_t sz, minsz, xd_sz, asz; 1754 1755 /* is this a task management command */ 1756 if (msg->icsd_task_msgid & MSG_ID_TM_BIT) { 1757 return (STMF_SUCCESS); 1758 } 1759 1760 task = find_task_from_msgid(msg->icsd_lun_id, msg->icsd_task_msgid); 1761 if (task == NULL) { 1762 stmf_ic_msg_t *ic_xfer_done_msg = NULL; 1763 static uint64_t data_msg_id; 1764 stmf_status_t ic_ret = STMF_FAILURE; 1765 mutex_enter(&stmf_state.stmf_lock); 1766 data_msg_id = stmf_proxy_msg_id++; 1767 mutex_exit(&stmf_state.stmf_lock); 1768 /* 1769 * send xfer done status to pppt 1770 * for now, set the session id to 0 as we cannot 1771 * ascertain it since we cannot find the task 1772 */ 1773 ic_xfer_done_msg = ic_scsi_data_xfer_done_msg_alloc( 1774 msg->icsd_task_msgid, 0, STMF_FAILURE, data_msg_id); 1775 if (ic_xfer_done_msg) { 1776 ic_ret = ic_tx_msg(ic_xfer_done_msg); 1777 if (ic_ret != STMF_IC_MSG_SUCCESS) { 1778 cmn_err(CE_WARN, "unable to xmit proxy msg"); 1779 } 1780 } 1781 return (STMF_FAILURE); 1782 } 1783 1784 itask = (stmf_i_scsi_task_t *)task->task_stmf_private; 1785 dbuf = itask->itask_proxy_dbuf; 1786 1787 task->task_cmd_xfer_length += msg->icsd_data_len; 1788 1789 if (task->task_additional_flags & 1790 TASK_AF_NO_EXPECTED_XFER_LENGTH) { 1791 task->task_expected_xfer_length = 1792 task->task_cmd_xfer_length; 1793 } 1794 1795 sz = min(task->task_expected_xfer_length, 1796 task->task_cmd_xfer_length); 1797 1798 xd_sz = msg->icsd_data_len; 1799 asz = xd_sz + sizeof (*xd) - 4; 1800 xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP); 1801 1802 if (xd == NULL) { 1803 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 1804 STMF_ALLOC_FAILURE, NULL); 1805 return (STMF_FAILURE); 1806 } 1807 1808 xd->alloc_size = asz; 1809 xd->size_left = xd_sz; 1810 bcopy(msg->icsd_data, xd->buf, xd_sz); 1811 1812 sz = min(sz, xd->size_left); 1813 xd->size_left = sz; 1814 minsz = min(512, sz); 1815 1816 if (dbuf == NULL) 1817 dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0); 1818 if (dbuf == NULL) { 1819 kmem_free(xd, xd->alloc_size); 1820 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 1821 STMF_ALLOC_FAILURE, NULL); 1822 return (STMF_FAILURE); 1823 } 1824 dbuf->db_lu_private = xd; 1825 dbuf->db_relative_offset = task->task_nbytes_transferred; 1826 stmf_xd_to_dbuf(dbuf, 0); 1827 1828 dbuf->db_flags = DB_DIRECTION_TO_RPORT; 1829 (void) stmf_xfer_data(task, dbuf, 0); 1830 return (STMF_SUCCESS); 1831 } 1832 1833 stmf_status_t 1834 stmf_proxy_scsi_cmd(scsi_task_t *task, stmf_data_buf_t *dbuf) 1835 { 1836 stmf_i_scsi_task_t *itask = 1837 (stmf_i_scsi_task_t *)task->task_stmf_private; 1838 stmf_i_local_port_t *ilport = 1839 (stmf_i_local_port_t *)task->task_lport->lport_stmf_private; 1840 stmf_ic_msg_t *ic_cmd_msg; 1841 stmf_ic_msg_status_t ic_ret; 1842 stmf_status_t ret = STMF_FAILURE; 1843 1844 if (stmf_state.stmf_alua_state != 1) { 1845 cmn_err(CE_WARN, "stmf alua state is disabled"); 1846 return (STMF_FAILURE); 1847 } 1848 1849 if (ilport->ilport_proxy_registered == 0) { 1850 return (STMF_FAILURE); 1851 } 1852 1853 mutex_enter(&stmf_state.stmf_lock); 1854 itask->itask_proxy_msg_id = stmf_proxy_msg_id++; 1855 mutex_exit(&stmf_state.stmf_lock); 1856 itask->itask_proxy_dbuf = dbuf; 1857 1858 /* 1859 * stmf will now take over the task handling for this task 1860 * but it still needs to be treated differently from other 1861 * default handled tasks, hence the ITASK_PROXY_TASK. 1862 * If this is a task management function, we're really just 1863 * duping the command to the peer. Set the TM bit so that 1864 * we can recognize this on return since we won't be completing 1865 * the proxied task in that case. 1866 */ 1867 if (task->task_mgmt_function) { 1868 itask->itask_proxy_msg_id |= MSG_ID_TM_BIT; 1869 } else { 1870 itask->itask_flags |= ITASK_DEFAULT_HANDLING | ITASK_PROXY_TASK; 1871 } 1872 if (dbuf) { 1873 ic_cmd_msg = ic_scsi_cmd_msg_alloc(itask->itask_proxy_msg_id, 1874 task, dbuf->db_data_size, dbuf->db_sglist[0].seg_addr, 1875 itask->itask_proxy_msg_id); 1876 } else { 1877 ic_cmd_msg = ic_scsi_cmd_msg_alloc(itask->itask_proxy_msg_id, 1878 task, 0, NULL, itask->itask_proxy_msg_id); 1879 } 1880 if (ic_cmd_msg) { 1881 ic_ret = ic_tx_msg(ic_cmd_msg); 1882 if (ic_ret == STMF_IC_MSG_SUCCESS) { 1883 ret = STMF_SUCCESS; 1884 } 1885 } 1886 return (ret); 1887 } 1888 1889 1890 stmf_status_t 1891 pppt_modload() 1892 { 1893 int error; 1894 1895 if (pppt_mod == NULL && ((pppt_mod = 1896 ddi_modopen("drv/pppt", KRTLD_MODE_FIRST, &error)) == NULL)) { 1897 cmn_err(CE_WARN, "Unable to load pppt"); 1898 return (STMF_FAILURE); 1899 } 1900 1901 if (ic_reg_port_msg_alloc == NULL && ((ic_reg_port_msg_alloc = 1902 (stmf_ic_reg_port_msg_alloc_func_t) 1903 ddi_modsym(pppt_mod, "stmf_ic_reg_port_msg_alloc", 1904 &error)) == NULL)) { 1905 cmn_err(CE_WARN, 1906 "Unable to find symbol - stmf_ic_reg_port_msg_alloc"); 1907 return (STMF_FAILURE); 1908 } 1909 1910 1911 if (ic_dereg_port_msg_alloc == NULL && ((ic_dereg_port_msg_alloc = 1912 (stmf_ic_dereg_port_msg_alloc_func_t) 1913 ddi_modsym(pppt_mod, "stmf_ic_dereg_port_msg_alloc", 1914 &error)) == NULL)) { 1915 cmn_err(CE_WARN, 1916 "Unable to find symbol - stmf_ic_dereg_port_msg_alloc"); 1917 return (STMF_FAILURE); 1918 } 1919 1920 if (ic_reg_lun_msg_alloc == NULL && ((ic_reg_lun_msg_alloc = 1921 (stmf_ic_reg_lun_msg_alloc_func_t) 1922 ddi_modsym(pppt_mod, "stmf_ic_reg_lun_msg_alloc", 1923 &error)) == NULL)) { 1924 cmn_err(CE_WARN, 1925 "Unable to find symbol - stmf_ic_reg_lun_msg_alloc"); 1926 return (STMF_FAILURE); 1927 } 1928 1929 if (ic_lun_active_msg_alloc == NULL && ((ic_lun_active_msg_alloc = 1930 (stmf_ic_lun_active_msg_alloc_func_t) 1931 ddi_modsym(pppt_mod, "stmf_ic_lun_active_msg_alloc", 1932 &error)) == NULL)) { 1933 cmn_err(CE_WARN, 1934 "Unable to find symbol - stmf_ic_lun_active_msg_alloc"); 1935 return (STMF_FAILURE); 1936 } 1937 1938 if (ic_dereg_lun_msg_alloc == NULL && ((ic_dereg_lun_msg_alloc = 1939 (stmf_ic_dereg_lun_msg_alloc_func_t) 1940 ddi_modsym(pppt_mod, "stmf_ic_dereg_lun_msg_alloc", 1941 &error)) == NULL)) { 1942 cmn_err(CE_WARN, 1943 "Unable to find symbol - stmf_ic_dereg_lun_msg_alloc"); 1944 return (STMF_FAILURE); 1945 } 1946 1947 if (ic_scsi_cmd_msg_alloc == NULL && ((ic_scsi_cmd_msg_alloc = 1948 (stmf_ic_scsi_cmd_msg_alloc_func_t) 1949 ddi_modsym(pppt_mod, "stmf_ic_scsi_cmd_msg_alloc", 1950 &error)) == NULL)) { 1951 cmn_err(CE_WARN, 1952 "Unable to find symbol - stmf_ic_scsi_cmd_msg_alloc"); 1953 return (STMF_FAILURE); 1954 } 1955 1956 if (ic_scsi_data_xfer_done_msg_alloc == NULL && 1957 ((ic_scsi_data_xfer_done_msg_alloc = 1958 (stmf_ic_scsi_data_xfer_done_msg_alloc_func_t) 1959 ddi_modsym(pppt_mod, "stmf_ic_scsi_data_xfer_done_msg_alloc", 1960 &error)) == NULL)) { 1961 cmn_err(CE_WARN, 1962 "Unable to find symbol -" 1963 "stmf_ic_scsi_data_xfer_done_msg_alloc"); 1964 return (STMF_FAILURE); 1965 } 1966 1967 if (ic_session_reg_msg_alloc == NULL && 1968 ((ic_session_reg_msg_alloc = 1969 (stmf_ic_session_create_msg_alloc_func_t) 1970 ddi_modsym(pppt_mod, "stmf_ic_session_create_msg_alloc", 1971 &error)) == NULL)) { 1972 cmn_err(CE_WARN, 1973 "Unable to find symbol -" 1974 "stmf_ic_session_create_msg_alloc"); 1975 return (STMF_FAILURE); 1976 } 1977 1978 if (ic_session_dereg_msg_alloc == NULL && 1979 ((ic_session_dereg_msg_alloc = 1980 (stmf_ic_session_destroy_msg_alloc_func_t) 1981 ddi_modsym(pppt_mod, "stmf_ic_session_destroy_msg_alloc", 1982 &error)) == NULL)) { 1983 cmn_err(CE_WARN, 1984 "Unable to find symbol -" 1985 "stmf_ic_session_destroy_msg_alloc"); 1986 return (STMF_FAILURE); 1987 } 1988 1989 if (ic_tx_msg == NULL && ((ic_tx_msg = 1990 (stmf_ic_tx_msg_func_t)ddi_modsym(pppt_mod, "stmf_ic_tx_msg", 1991 &error)) == NULL)) { 1992 cmn_err(CE_WARN, "Unable to find symbol - stmf_ic_tx_msg"); 1993 return (STMF_FAILURE); 1994 } 1995 1996 if (ic_msg_free == NULL && ((ic_msg_free = 1997 (stmf_ic_msg_free_func_t)ddi_modsym(pppt_mod, "stmf_ic_msg_free", 1998 &error)) == NULL)) { 1999 cmn_err(CE_WARN, "Unable to find symbol - stmf_ic_msg_free"); 2000 return (STMF_FAILURE); 2001 } 2002 return (STMF_SUCCESS); 2003 } 2004 2005 static void 2006 stmf_get_alua_state(stmf_alua_state_desc_t *alua_state) 2007 { 2008 mutex_enter(&stmf_state.stmf_lock); 2009 alua_state->alua_node = stmf_state.stmf_alua_node; 2010 alua_state->alua_state = stmf_state.stmf_alua_state; 2011 mutex_exit(&stmf_state.stmf_lock); 2012 } 2013 2014 2015 static int 2016 stmf_set_alua_state(stmf_alua_state_desc_t *alua_state) 2017 { 2018 stmf_i_local_port_t *ilport; 2019 stmf_i_lu_t *ilu; 2020 stmf_lu_t *lu; 2021 stmf_ic_msg_status_t ic_ret; 2022 stmf_ic_msg_t *ic_reg_lun, *ic_reg_port; 2023 stmf_local_port_t *lport; 2024 int ret = 0; 2025 2026 if (alua_state->alua_state > 1 || alua_state->alua_node > 1) { 2027 return (EINVAL); 2028 } 2029 2030 mutex_enter(&stmf_state.stmf_lock); 2031 if (alua_state->alua_state == 1) { 2032 if (pppt_modload() == STMF_FAILURE) { 2033 ret = EIO; 2034 goto err; 2035 } 2036 if (alua_state->alua_node != 0) { 2037 /* reset existing rtpids to new base */ 2038 stmf_rtpid_counter = 255; 2039 } 2040 stmf_state.stmf_alua_node = alua_state->alua_node; 2041 stmf_state.stmf_alua_state = 1; 2042 /* register existing local ports with ppp */ 2043 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 2044 ilport = ilport->ilport_next) { 2045 /* skip standby ports and non-alua participants */ 2046 if (ilport->ilport_standby == 1 || 2047 ilport->ilport_alua == 0) { 2048 continue; 2049 } 2050 if (alua_state->alua_node != 0) { 2051 ilport->ilport_rtpid = 2052 atomic_add_16_nv(&stmf_rtpid_counter, 1); 2053 } 2054 lport = ilport->ilport_lport; 2055 ic_reg_port = ic_reg_port_msg_alloc( 2056 lport->lport_id, ilport->ilport_rtpid, 2057 0, NULL, stmf_proxy_msg_id); 2058 if (ic_reg_port) { 2059 ic_ret = ic_tx_msg(ic_reg_port); 2060 if (ic_ret == STMF_IC_MSG_SUCCESS) { 2061 ilport->ilport_reg_msgid = 2062 stmf_proxy_msg_id++; 2063 } else { 2064 cmn_err(CE_WARN, 2065 "error on port registration " 2066 "port - %s", 2067 ilport->ilport_kstat_tgt_name); 2068 } 2069 } 2070 } 2071 /* register existing logical units */ 2072 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; 2073 ilu = ilu->ilu_next) { 2074 if (ilu->ilu_access != STMF_LU_ACTIVE) { 2075 continue; 2076 } 2077 /* register with proxy module */ 2078 lu = ilu->ilu_lu; 2079 if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 && 2080 lu->lu_lp->lp_alua_support) { 2081 ilu->ilu_alua = 1; 2082 /* allocate the register message */ 2083 ic_reg_lun = ic_reg_lun_msg_alloc( 2084 lu->lu_id->ident, lu->lu_lp->lp_name, 2085 lu->lu_proxy_reg_arg_len, 2086 (uint8_t *)lu->lu_proxy_reg_arg, 2087 stmf_proxy_msg_id); 2088 /* send the message */ 2089 if (ic_reg_lun) { 2090 ic_ret = ic_tx_msg(ic_reg_lun); 2091 if (ic_ret == STMF_IC_MSG_SUCCESS) { 2092 stmf_proxy_msg_id++; 2093 } 2094 } 2095 } 2096 } 2097 } else { 2098 stmf_state.stmf_alua_state = 0; 2099 } 2100 2101 err: 2102 mutex_exit(&stmf_state.stmf_lock); 2103 return (ret); 2104 } 2105 2106 2107 typedef struct { 2108 void *bp; /* back pointer from internal struct to main struct */ 2109 int alloc_size; 2110 } __istmf_t; 2111 2112 typedef struct { 2113 __istmf_t *fp; /* Framework private */ 2114 void *cp; /* Caller private */ 2115 void *ss; /* struct specific */ 2116 } __stmf_t; 2117 2118 static struct { 2119 int shared; 2120 int fw_private; 2121 } stmf_sizes[] = { { 0, 0 }, 2122 { GET_STRUCT_SIZE(stmf_lu_provider_t), 2123 GET_STRUCT_SIZE(stmf_i_lu_provider_t) }, 2124 { GET_STRUCT_SIZE(stmf_port_provider_t), 2125 GET_STRUCT_SIZE(stmf_i_port_provider_t) }, 2126 { GET_STRUCT_SIZE(stmf_local_port_t), 2127 GET_STRUCT_SIZE(stmf_i_local_port_t) }, 2128 { GET_STRUCT_SIZE(stmf_lu_t), 2129 GET_STRUCT_SIZE(stmf_i_lu_t) }, 2130 { GET_STRUCT_SIZE(stmf_scsi_session_t), 2131 GET_STRUCT_SIZE(stmf_i_scsi_session_t) }, 2132 { GET_STRUCT_SIZE(scsi_task_t), 2133 GET_STRUCT_SIZE(stmf_i_scsi_task_t) }, 2134 { GET_STRUCT_SIZE(stmf_data_buf_t), 2135 GET_STRUCT_SIZE(__istmf_t) }, 2136 { GET_STRUCT_SIZE(stmf_dbuf_store_t), 2137 GET_STRUCT_SIZE(__istmf_t) } 2138 2139 }; 2140 2141 void * 2142 stmf_alloc(stmf_struct_id_t struct_id, int additional_size, int flags) 2143 { 2144 int stmf_size; 2145 int kmem_flag; 2146 __stmf_t *sh; 2147 2148 if ((struct_id == 0) || (struct_id >= STMF_MAX_STRUCT_IDS)) 2149 return (NULL); 2150 2151 if ((curthread->t_flag & T_INTR_THREAD) || (flags & AF_FORCE_NOSLEEP)) { 2152 kmem_flag = KM_NOSLEEP; 2153 } else { 2154 kmem_flag = KM_SLEEP; 2155 } 2156 2157 additional_size = (additional_size + 7) & (~7); 2158 stmf_size = stmf_sizes[struct_id].shared + 2159 stmf_sizes[struct_id].fw_private + additional_size; 2160 2161 if (flags & AF_DONTZERO) 2162 sh = (__stmf_t *)kmem_alloc(stmf_size, kmem_flag); 2163 else 2164 sh = (__stmf_t *)kmem_zalloc(stmf_size, kmem_flag); 2165 2166 if (sh == NULL) 2167 return (NULL); 2168 2169 /* 2170 * In principle, the implementation inside stmf_alloc should not 2171 * be changed anyway. But the original order of framework private 2172 * data and caller private data does not support sglist in the caller 2173 * private data. 2174 * To work around this, the memory segments of framework private 2175 * data and caller private data are re-ordered here. 2176 * A better solution is to provide a specific interface to allocate 2177 * the sglist, then we will not need this workaround any more. 2178 * But before the new interface is available, the memory segment 2179 * ordering should be kept as is. 2180 */ 2181 sh->cp = GET_BYTE_OFFSET(sh, stmf_sizes[struct_id].shared); 2182 sh->fp = (__istmf_t *)GET_BYTE_OFFSET(sh, 2183 stmf_sizes[struct_id].shared + additional_size); 2184 2185 sh->fp->bp = sh; 2186 /* Just store the total size instead of storing additional size */ 2187 sh->fp->alloc_size = stmf_size; 2188 2189 return (sh); 2190 } 2191 2192 void 2193 stmf_free(void *ptr) 2194 { 2195 __stmf_t *sh = (__stmf_t *)ptr; 2196 2197 /* 2198 * So far we dont need any struct specific processing. If such 2199 * a need ever arises, then store the struct id in the framework 2200 * private section and get it here as sh->fp->struct_id. 2201 */ 2202 kmem_free(ptr, sh->fp->alloc_size); 2203 } 2204 2205 /* 2206 * Given a pointer to stmf_lu_t, verifies if this lu is registered with the 2207 * framework and returns a pointer to framework private data for the lu. 2208 * Returns NULL if the lu was not found. 2209 */ 2210 stmf_i_lu_t * 2211 stmf_lookup_lu(stmf_lu_t *lu) 2212 { 2213 stmf_i_lu_t *ilu; 2214 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 2215 2216 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) { 2217 if (ilu->ilu_lu == lu) 2218 return (ilu); 2219 } 2220 return (NULL); 2221 } 2222 2223 /* 2224 * Given a pointer to stmf_local_port_t, verifies if this lport is registered 2225 * with the framework and returns a pointer to framework private data for 2226 * the lport. 2227 * Returns NULL if the lport was not found. 2228 */ 2229 stmf_i_local_port_t * 2230 stmf_lookup_lport(stmf_local_port_t *lport) 2231 { 2232 stmf_i_local_port_t *ilport; 2233 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 2234 2235 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 2236 ilport = ilport->ilport_next) { 2237 if (ilport->ilport_lport == lport) 2238 return (ilport); 2239 } 2240 return (NULL); 2241 } 2242 2243 stmf_status_t 2244 stmf_register_lu_provider(stmf_lu_provider_t *lp) 2245 { 2246 stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private; 2247 stmf_pp_data_t *ppd; 2248 uint32_t cb_flags; 2249 2250 if (lp->lp_lpif_rev != LPIF_REV_1 && lp->lp_lpif_rev != LPIF_REV_2) 2251 return (STMF_FAILURE); 2252 2253 mutex_enter(&stmf_state.stmf_lock); 2254 ilp->ilp_next = stmf_state.stmf_ilplist; 2255 stmf_state.stmf_ilplist = ilp; 2256 stmf_state.stmf_nlps++; 2257 2258 /* See if we need to do a callback */ 2259 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) { 2260 if (strcmp(ppd->ppd_name, lp->lp_name) == 0) { 2261 break; 2262 } 2263 } 2264 if ((ppd == NULL) || (ppd->ppd_nv == NULL)) { 2265 goto rlp_bail_out; 2266 } 2267 ilp->ilp_ppd = ppd; 2268 ppd->ppd_provider = ilp; 2269 if (lp->lp_cb == NULL) 2270 goto rlp_bail_out; 2271 ilp->ilp_cb_in_progress = 1; 2272 cb_flags = STMF_PCB_PREG_COMPLETE; 2273 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) 2274 cb_flags |= STMF_PCB_STMF_ONLINING; 2275 mutex_exit(&stmf_state.stmf_lock); 2276 lp->lp_cb(lp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags); 2277 mutex_enter(&stmf_state.stmf_lock); 2278 ilp->ilp_cb_in_progress = 0; 2279 2280 rlp_bail_out: 2281 mutex_exit(&stmf_state.stmf_lock); 2282 2283 return (STMF_SUCCESS); 2284 } 2285 2286 stmf_status_t 2287 stmf_deregister_lu_provider(stmf_lu_provider_t *lp) 2288 { 2289 stmf_i_lu_provider_t **ppilp; 2290 stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private; 2291 2292 mutex_enter(&stmf_state.stmf_lock); 2293 if (ilp->ilp_nlus || ilp->ilp_cb_in_progress) { 2294 mutex_exit(&stmf_state.stmf_lock); 2295 return (STMF_BUSY); 2296 } 2297 for (ppilp = &stmf_state.stmf_ilplist; *ppilp != NULL; 2298 ppilp = &((*ppilp)->ilp_next)) { 2299 if (*ppilp == ilp) { 2300 *ppilp = ilp->ilp_next; 2301 stmf_state.stmf_nlps--; 2302 if (ilp->ilp_ppd) { 2303 ilp->ilp_ppd->ppd_provider = NULL; 2304 ilp->ilp_ppd = NULL; 2305 } 2306 mutex_exit(&stmf_state.stmf_lock); 2307 return (STMF_SUCCESS); 2308 } 2309 } 2310 mutex_exit(&stmf_state.stmf_lock); 2311 return (STMF_NOT_FOUND); 2312 } 2313 2314 stmf_status_t 2315 stmf_register_port_provider(stmf_port_provider_t *pp) 2316 { 2317 stmf_i_port_provider_t *ipp = 2318 (stmf_i_port_provider_t *)pp->pp_stmf_private; 2319 stmf_pp_data_t *ppd; 2320 uint32_t cb_flags; 2321 2322 if (pp->pp_portif_rev != PORTIF_REV_1) 2323 return (STMF_FAILURE); 2324 2325 mutex_enter(&stmf_state.stmf_lock); 2326 ipp->ipp_next = stmf_state.stmf_ipplist; 2327 stmf_state.stmf_ipplist = ipp; 2328 stmf_state.stmf_npps++; 2329 /* See if we need to do a callback */ 2330 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) { 2331 if (strcmp(ppd->ppd_name, pp->pp_name) == 0) { 2332 break; 2333 } 2334 } 2335 if ((ppd == NULL) || (ppd->ppd_nv == NULL)) { 2336 goto rpp_bail_out; 2337 } 2338 ipp->ipp_ppd = ppd; 2339 ppd->ppd_provider = ipp; 2340 if (pp->pp_cb == NULL) 2341 goto rpp_bail_out; 2342 ipp->ipp_cb_in_progress = 1; 2343 cb_flags = STMF_PCB_PREG_COMPLETE; 2344 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) 2345 cb_flags |= STMF_PCB_STMF_ONLINING; 2346 mutex_exit(&stmf_state.stmf_lock); 2347 pp->pp_cb(pp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags); 2348 mutex_enter(&stmf_state.stmf_lock); 2349 ipp->ipp_cb_in_progress = 0; 2350 2351 rpp_bail_out: 2352 mutex_exit(&stmf_state.stmf_lock); 2353 2354 return (STMF_SUCCESS); 2355 } 2356 2357 stmf_status_t 2358 stmf_deregister_port_provider(stmf_port_provider_t *pp) 2359 { 2360 stmf_i_port_provider_t *ipp = 2361 (stmf_i_port_provider_t *)pp->pp_stmf_private; 2362 stmf_i_port_provider_t **ppipp; 2363 2364 mutex_enter(&stmf_state.stmf_lock); 2365 if (ipp->ipp_npps || ipp->ipp_cb_in_progress) { 2366 mutex_exit(&stmf_state.stmf_lock); 2367 return (STMF_BUSY); 2368 } 2369 for (ppipp = &stmf_state.stmf_ipplist; *ppipp != NULL; 2370 ppipp = &((*ppipp)->ipp_next)) { 2371 if (*ppipp == ipp) { 2372 *ppipp = ipp->ipp_next; 2373 stmf_state.stmf_npps--; 2374 if (ipp->ipp_ppd) { 2375 ipp->ipp_ppd->ppd_provider = NULL; 2376 ipp->ipp_ppd = NULL; 2377 } 2378 mutex_exit(&stmf_state.stmf_lock); 2379 return (STMF_SUCCESS); 2380 } 2381 } 2382 mutex_exit(&stmf_state.stmf_lock); 2383 return (STMF_NOT_FOUND); 2384 } 2385 2386 int 2387 stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi, uint64_t *ppi_token, 2388 uint32_t *err_ret) 2389 { 2390 stmf_i_port_provider_t *ipp; 2391 stmf_i_lu_provider_t *ilp; 2392 stmf_pp_data_t *ppd; 2393 nvlist_t *nv; 2394 int s; 2395 int ret; 2396 2397 *err_ret = 0; 2398 2399 if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) { 2400 return (EINVAL); 2401 } 2402 2403 mutex_enter(&stmf_state.stmf_lock); 2404 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) { 2405 if (ppi->ppi_lu_provider) { 2406 if (!ppd->ppd_lu_provider) 2407 continue; 2408 } else if (ppi->ppi_port_provider) { 2409 if (!ppd->ppd_port_provider) 2410 continue; 2411 } 2412 if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0) 2413 break; 2414 } 2415 2416 if (ppd == NULL) { 2417 /* New provider */ 2418 s = strlen(ppi->ppi_name); 2419 if (s > 254) { 2420 mutex_exit(&stmf_state.stmf_lock); 2421 return (EINVAL); 2422 } 2423 s += sizeof (stmf_pp_data_t) - 7; 2424 2425 ppd = kmem_zalloc(s, KM_NOSLEEP); 2426 if (ppd == NULL) { 2427 mutex_exit(&stmf_state.stmf_lock); 2428 return (ENOMEM); 2429 } 2430 ppd->ppd_alloc_size = s; 2431 (void) strcpy(ppd->ppd_name, ppi->ppi_name); 2432 2433 /* See if this provider already exists */ 2434 if (ppi->ppi_lu_provider) { 2435 ppd->ppd_lu_provider = 1; 2436 for (ilp = stmf_state.stmf_ilplist; ilp != NULL; 2437 ilp = ilp->ilp_next) { 2438 if (strcmp(ppi->ppi_name, 2439 ilp->ilp_lp->lp_name) == 0) { 2440 ppd->ppd_provider = ilp; 2441 ilp->ilp_ppd = ppd; 2442 break; 2443 } 2444 } 2445 } else { 2446 ppd->ppd_port_provider = 1; 2447 for (ipp = stmf_state.stmf_ipplist; ipp != NULL; 2448 ipp = ipp->ipp_next) { 2449 if (strcmp(ppi->ppi_name, 2450 ipp->ipp_pp->pp_name) == 0) { 2451 ppd->ppd_provider = ipp; 2452 ipp->ipp_ppd = ppd; 2453 break; 2454 } 2455 } 2456 } 2457 2458 /* Link this ppd in */ 2459 ppd->ppd_next = stmf_state.stmf_ppdlist; 2460 stmf_state.stmf_ppdlist = ppd; 2461 } 2462 2463 /* 2464 * User is requesting that the token be checked. 2465 * If there was another set after the user's get 2466 * it's an error 2467 */ 2468 if (ppi->ppi_token_valid) { 2469 if (ppi->ppi_token != ppd->ppd_token) { 2470 *err_ret = STMF_IOCERR_PPD_UPDATED; 2471 mutex_exit(&stmf_state.stmf_lock); 2472 return (EINVAL); 2473 } 2474 } 2475 2476 if ((ret = nvlist_unpack((char *)ppi->ppi_data, 2477 (size_t)ppi->ppi_data_size, &nv, KM_NOSLEEP)) != 0) { 2478 mutex_exit(&stmf_state.stmf_lock); 2479 return (ret); 2480 } 2481 2482 /* Free any existing lists and add this one to the ppd */ 2483 if (ppd->ppd_nv) 2484 nvlist_free(ppd->ppd_nv); 2485 ppd->ppd_nv = nv; 2486 2487 /* set the token for writes */ 2488 ppd->ppd_token++; 2489 /* return token to caller */ 2490 if (ppi_token) { 2491 *ppi_token = ppd->ppd_token; 2492 } 2493 2494 /* If there is a provider registered, do the notifications */ 2495 if (ppd->ppd_provider) { 2496 uint32_t cb_flags = 0; 2497 2498 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) 2499 cb_flags |= STMF_PCB_STMF_ONLINING; 2500 if (ppi->ppi_lu_provider) { 2501 ilp = (stmf_i_lu_provider_t *)ppd->ppd_provider; 2502 if (ilp->ilp_lp->lp_cb == NULL) 2503 goto bail_out; 2504 ilp->ilp_cb_in_progress = 1; 2505 mutex_exit(&stmf_state.stmf_lock); 2506 ilp->ilp_lp->lp_cb(ilp->ilp_lp, 2507 STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags); 2508 mutex_enter(&stmf_state.stmf_lock); 2509 ilp->ilp_cb_in_progress = 0; 2510 } else { 2511 ipp = (stmf_i_port_provider_t *)ppd->ppd_provider; 2512 if (ipp->ipp_pp->pp_cb == NULL) 2513 goto bail_out; 2514 ipp->ipp_cb_in_progress = 1; 2515 mutex_exit(&stmf_state.stmf_lock); 2516 ipp->ipp_pp->pp_cb(ipp->ipp_pp, 2517 STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags); 2518 mutex_enter(&stmf_state.stmf_lock); 2519 ipp->ipp_cb_in_progress = 0; 2520 } 2521 } 2522 2523 bail_out: 2524 mutex_exit(&stmf_state.stmf_lock); 2525 2526 return (0); 2527 } 2528 2529 void 2530 stmf_delete_ppd(stmf_pp_data_t *ppd) 2531 { 2532 stmf_pp_data_t **pppd; 2533 2534 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 2535 if (ppd->ppd_provider) { 2536 if (ppd->ppd_lu_provider) { 2537 ((stmf_i_lu_provider_t *) 2538 ppd->ppd_provider)->ilp_ppd = NULL; 2539 } else { 2540 ((stmf_i_port_provider_t *) 2541 ppd->ppd_provider)->ipp_ppd = NULL; 2542 } 2543 ppd->ppd_provider = NULL; 2544 } 2545 2546 for (pppd = &stmf_state.stmf_ppdlist; *pppd != NULL; 2547 pppd = &((*pppd)->ppd_next)) { 2548 if (*pppd == ppd) 2549 break; 2550 } 2551 2552 if (*pppd == NULL) 2553 return; 2554 2555 *pppd = ppd->ppd_next; 2556 if (ppd->ppd_nv) 2557 nvlist_free(ppd->ppd_nv); 2558 2559 kmem_free(ppd, ppd->ppd_alloc_size); 2560 } 2561 2562 int 2563 stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi) 2564 { 2565 stmf_pp_data_t *ppd; 2566 int ret = ENOENT; 2567 2568 if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) { 2569 return (EINVAL); 2570 } 2571 2572 mutex_enter(&stmf_state.stmf_lock); 2573 2574 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) { 2575 if (ppi->ppi_lu_provider) { 2576 if (!ppd->ppd_lu_provider) 2577 continue; 2578 } else if (ppi->ppi_port_provider) { 2579 if (!ppd->ppd_port_provider) 2580 continue; 2581 } 2582 if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0) 2583 break; 2584 } 2585 2586 if (ppd) { 2587 ret = 0; 2588 stmf_delete_ppd(ppd); 2589 } 2590 mutex_exit(&stmf_state.stmf_lock); 2591 2592 return (ret); 2593 } 2594 2595 int 2596 stmf_get_ppd_ioctl(stmf_ppioctl_data_t *ppi, stmf_ppioctl_data_t *ppi_out, 2597 uint32_t *err_ret) 2598 { 2599 stmf_pp_data_t *ppd; 2600 size_t req_size; 2601 int ret = ENOENT; 2602 char *bufp = (char *)ppi_out->ppi_data; 2603 2604 if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) { 2605 return (EINVAL); 2606 } 2607 2608 mutex_enter(&stmf_state.stmf_lock); 2609 2610 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) { 2611 if (ppi->ppi_lu_provider) { 2612 if (!ppd->ppd_lu_provider) 2613 continue; 2614 } else if (ppi->ppi_port_provider) { 2615 if (!ppd->ppd_port_provider) 2616 continue; 2617 } 2618 if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0) 2619 break; 2620 } 2621 2622 if (ppd && ppd->ppd_nv) { 2623 ppi_out->ppi_token = ppd->ppd_token; 2624 if ((ret = nvlist_size(ppd->ppd_nv, &req_size, 2625 NV_ENCODE_XDR)) != 0) { 2626 goto done; 2627 } 2628 ppi_out->ppi_data_size = req_size; 2629 if (req_size > ppi->ppi_data_size) { 2630 *err_ret = STMF_IOCERR_INSUFFICIENT_BUF; 2631 ret = EINVAL; 2632 goto done; 2633 } 2634 2635 if ((ret = nvlist_pack(ppd->ppd_nv, &bufp, &req_size, 2636 NV_ENCODE_XDR, 0)) != 0) { 2637 goto done; 2638 } 2639 ret = 0; 2640 } 2641 2642 done: 2643 mutex_exit(&stmf_state.stmf_lock); 2644 2645 return (ret); 2646 } 2647 2648 void 2649 stmf_delete_all_ppds() 2650 { 2651 stmf_pp_data_t *ppd, *nppd; 2652 2653 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 2654 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = nppd) { 2655 nppd = ppd->ppd_next; 2656 stmf_delete_ppd(ppd); 2657 } 2658 } 2659 2660 /* 2661 * 16 is the max string length of a protocol_ident, increase 2662 * the size if needed. 2663 */ 2664 #define STMF_KSTAT_LU_SZ (STMF_GUID_INPUT + 1 + 256) 2665 #define STMF_KSTAT_TGT_SZ (256 * 2 + 16) 2666 2667 /* 2668 * This array matches the Protocol Identifier in stmf_ioctl.h 2669 */ 2670 #define MAX_PROTO_STR_LEN 32 2671 2672 char *protocol_ident[PROTOCOL_ANY] = { 2673 "Fibre Channel", 2674 "Parallel SCSI", 2675 "SSA", 2676 "IEEE_1394", 2677 "SRP", 2678 "iSCSI", 2679 "SAS", 2680 "ADT", 2681 "ATAPI", 2682 "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN" 2683 }; 2684 2685 /* 2686 * Update the lun wait/run queue count 2687 */ 2688 static void 2689 stmf_update_kstat_lu_q(scsi_task_t *task, void func()) 2690 { 2691 stmf_i_lu_t *ilu; 2692 kstat_io_t *kip; 2693 2694 if (task->task_lu == dlun0) 2695 return; 2696 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 2697 if (ilu != NULL && ilu->ilu_kstat_io != NULL) { 2698 kip = KSTAT_IO_PTR(ilu->ilu_kstat_io); 2699 if (kip != NULL) { 2700 func(kip); 2701 } 2702 } 2703 } 2704 2705 /* 2706 * Update the target(lport) wait/run queue count 2707 */ 2708 static void 2709 stmf_update_kstat_lport_q(scsi_task_t *task, void func()) 2710 { 2711 stmf_i_local_port_t *ilp; 2712 kstat_io_t *kip; 2713 2714 ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private; 2715 if (ilp != NULL && ilp->ilport_kstat_io != NULL) { 2716 kip = KSTAT_IO_PTR(ilp->ilport_kstat_io); 2717 if (kip != NULL) { 2718 mutex_enter(ilp->ilport_kstat_io->ks_lock); 2719 func(kip); 2720 mutex_exit(ilp->ilport_kstat_io->ks_lock); 2721 } 2722 } 2723 } 2724 2725 static void 2726 stmf_update_kstat_lport_io(scsi_task_t *task, stmf_data_buf_t *dbuf) 2727 { 2728 stmf_i_local_port_t *ilp; 2729 kstat_io_t *kip; 2730 2731 ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private; 2732 if (ilp != NULL && ilp->ilport_kstat_io != NULL) { 2733 kip = KSTAT_IO_PTR(ilp->ilport_kstat_io); 2734 if (kip != NULL) { 2735 mutex_enter(ilp->ilport_kstat_io->ks_lock); 2736 STMF_UPDATE_KSTAT_IO(kip, dbuf); 2737 mutex_exit(ilp->ilport_kstat_io->ks_lock); 2738 } 2739 } 2740 } 2741 2742 static void 2743 stmf_update_kstat_lu_io(scsi_task_t *task, stmf_data_buf_t *dbuf) 2744 { 2745 stmf_i_lu_t *ilu; 2746 kstat_io_t *kip; 2747 2748 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 2749 if (ilu != NULL && ilu->ilu_kstat_io != NULL) { 2750 kip = KSTAT_IO_PTR(ilu->ilu_kstat_io); 2751 if (kip != NULL) { 2752 mutex_enter(ilu->ilu_kstat_io->ks_lock); 2753 STMF_UPDATE_KSTAT_IO(kip, dbuf); 2754 mutex_exit(ilu->ilu_kstat_io->ks_lock); 2755 } 2756 } 2757 } 2758 2759 static void 2760 stmf_create_kstat_lu(stmf_i_lu_t *ilu) 2761 { 2762 char ks_nm[KSTAT_STRLEN]; 2763 stmf_kstat_lu_info_t *ks_lu; 2764 2765 /* create kstat lun info */ 2766 ks_lu = (stmf_kstat_lu_info_t *)kmem_zalloc(STMF_KSTAT_LU_SZ, 2767 KM_NOSLEEP); 2768 if (ks_lu == NULL) { 2769 cmn_err(CE_WARN, "STMF: kmem_zalloc failed"); 2770 return; 2771 } 2772 2773 bzero(ks_nm, sizeof (ks_nm)); 2774 (void) sprintf(ks_nm, "stmf_lu_%"PRIxPTR"", (uintptr_t)ilu); 2775 if ((ilu->ilu_kstat_info = kstat_create(STMF_MODULE_NAME, 0, 2776 ks_nm, "misc", KSTAT_TYPE_NAMED, 2777 sizeof (stmf_kstat_lu_info_t) / sizeof (kstat_named_t), 2778 KSTAT_FLAG_VIRTUAL)) == NULL) { 2779 kmem_free(ks_lu, STMF_KSTAT_LU_SZ); 2780 cmn_err(CE_WARN, "STMF: kstat_create lu failed"); 2781 return; 2782 } 2783 2784 ilu->ilu_kstat_info->ks_data_size = STMF_KSTAT_LU_SZ; 2785 ilu->ilu_kstat_info->ks_data = ks_lu; 2786 2787 kstat_named_init(&ks_lu->i_lun_guid, "lun-guid", 2788 KSTAT_DATA_STRING); 2789 kstat_named_init(&ks_lu->i_lun_alias, "lun-alias", 2790 KSTAT_DATA_STRING); 2791 2792 /* convert guid to hex string */ 2793 int i; 2794 uint8_t *p = ilu->ilu_lu->lu_id->ident; 2795 bzero(ilu->ilu_ascii_hex_guid, sizeof (ilu->ilu_ascii_hex_guid)); 2796 for (i = 0; i < STMF_GUID_INPUT / 2; i++) { 2797 (void) sprintf(&ilu->ilu_ascii_hex_guid[i * 2], "%02x", p[i]); 2798 } 2799 kstat_named_setstr(&ks_lu->i_lun_guid, 2800 (const char *)ilu->ilu_ascii_hex_guid); 2801 kstat_named_setstr(&ks_lu->i_lun_alias, 2802 (const char *)ilu->ilu_lu->lu_alias); 2803 kstat_install(ilu->ilu_kstat_info); 2804 2805 /* create kstat lun io */ 2806 bzero(ks_nm, sizeof (ks_nm)); 2807 (void) sprintf(ks_nm, "stmf_lu_io_%"PRIxPTR"", (uintptr_t)ilu); 2808 if ((ilu->ilu_kstat_io = kstat_create(STMF_MODULE_NAME, 0, 2809 ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) { 2810 cmn_err(CE_WARN, "STMF: kstat_create lu_io failed"); 2811 return; 2812 } 2813 mutex_init(&ilu->ilu_kstat_lock, NULL, MUTEX_DRIVER, 0); 2814 ilu->ilu_kstat_io->ks_lock = &ilu->ilu_kstat_lock; 2815 kstat_install(ilu->ilu_kstat_io); 2816 } 2817 2818 static void 2819 stmf_create_kstat_lport(stmf_i_local_port_t *ilport) 2820 { 2821 char ks_nm[KSTAT_STRLEN]; 2822 stmf_kstat_tgt_info_t *ks_tgt; 2823 int id, len; 2824 2825 /* create kstat lport info */ 2826 ks_tgt = (stmf_kstat_tgt_info_t *)kmem_zalloc(STMF_KSTAT_TGT_SZ, 2827 KM_NOSLEEP); 2828 if (ks_tgt == NULL) { 2829 cmn_err(CE_WARN, "STMF: kmem_zalloc failed"); 2830 return; 2831 } 2832 2833 bzero(ks_nm, sizeof (ks_nm)); 2834 (void) sprintf(ks_nm, "stmf_tgt_%"PRIxPTR"", (uintptr_t)ilport); 2835 if ((ilport->ilport_kstat_info = kstat_create(STMF_MODULE_NAME, 2836 0, ks_nm, "misc", KSTAT_TYPE_NAMED, 2837 sizeof (stmf_kstat_tgt_info_t) / sizeof (kstat_named_t), 2838 KSTAT_FLAG_VIRTUAL)) == NULL) { 2839 kmem_free(ks_tgt, STMF_KSTAT_TGT_SZ); 2840 cmn_err(CE_WARN, "STMF: kstat_create target failed"); 2841 return; 2842 } 2843 2844 ilport->ilport_kstat_info->ks_data_size = STMF_KSTAT_TGT_SZ; 2845 ilport->ilport_kstat_info->ks_data = ks_tgt; 2846 2847 kstat_named_init(&ks_tgt->i_tgt_name, "target-name", 2848 KSTAT_DATA_STRING); 2849 kstat_named_init(&ks_tgt->i_tgt_alias, "target-alias", 2850 KSTAT_DATA_STRING); 2851 kstat_named_init(&ks_tgt->i_protocol, "protocol", 2852 KSTAT_DATA_STRING); 2853 2854 /* ident might not be null terminated */ 2855 len = ilport->ilport_lport->lport_id->ident_length; 2856 bcopy(ilport->ilport_lport->lport_id->ident, 2857 ilport->ilport_kstat_tgt_name, len); 2858 ilport->ilport_kstat_tgt_name[len + 1] = NULL; 2859 kstat_named_setstr(&ks_tgt->i_tgt_name, 2860 (const char *)ilport->ilport_kstat_tgt_name); 2861 kstat_named_setstr(&ks_tgt->i_tgt_alias, 2862 (const char *)ilport->ilport_lport->lport_alias); 2863 /* protocol */ 2864 if ((id = ilport->ilport_lport->lport_id->protocol_id) > PROTOCOL_ANY) { 2865 cmn_err(CE_WARN, "STMF: protocol_id out of bound"); 2866 id = PROTOCOL_ANY; 2867 } 2868 kstat_named_setstr(&ks_tgt->i_protocol, protocol_ident[id]); 2869 kstat_install(ilport->ilport_kstat_info); 2870 2871 /* create kstat lport io */ 2872 bzero(ks_nm, sizeof (ks_nm)); 2873 (void) sprintf(ks_nm, "stmf_tgt_io_%"PRIxPTR"", (uintptr_t)ilport); 2874 if ((ilport->ilport_kstat_io = kstat_create(STMF_MODULE_NAME, 0, 2875 ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) { 2876 cmn_err(CE_WARN, "STMF: kstat_create target_io failed"); 2877 return; 2878 } 2879 mutex_init(&ilport->ilport_kstat_lock, NULL, MUTEX_DRIVER, 0); 2880 ilport->ilport_kstat_io->ks_lock = &ilport->ilport_kstat_lock; 2881 kstat_install(ilport->ilport_kstat_io); 2882 } 2883 2884 /* 2885 * set the asymmetric access state for a logical unit 2886 * caller is responsible for establishing SCSI unit attention on 2887 * state change 2888 */ 2889 stmf_status_t 2890 stmf_set_lu_access(stmf_lu_t *lu, uint8_t access_state) 2891 { 2892 stmf_i_lu_t *ilu; 2893 uint8_t *p1, *p2; 2894 2895 if ((access_state != STMF_LU_STANDBY) && 2896 (access_state != STMF_LU_ACTIVE)) { 2897 return (STMF_INVALID_ARG); 2898 } 2899 2900 p1 = &lu->lu_id->ident[0]; 2901 mutex_enter(&stmf_state.stmf_lock); 2902 if (stmf_state.stmf_inventory_locked) { 2903 mutex_exit(&stmf_state.stmf_lock); 2904 return (STMF_BUSY); 2905 } 2906 2907 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) { 2908 p2 = &ilu->ilu_lu->lu_id->ident[0]; 2909 if (bcmp(p1, p2, 16) == 0) { 2910 break; 2911 } 2912 } 2913 2914 if (!ilu) { 2915 ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 2916 } else { 2917 /* 2918 * We're changing access state on an existing logical unit 2919 * Send the proxy registration message for this logical unit 2920 * if we're in alua mode. 2921 * If the requested state is STMF_LU_ACTIVE, we want to register 2922 * this logical unit. 2923 * If the requested state is STMF_LU_STANDBY, we're going to 2924 * abort all tasks for this logical unit. 2925 */ 2926 if (stmf_state.stmf_alua_state == 1 && 2927 access_state == STMF_LU_ACTIVE) { 2928 stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS; 2929 stmf_ic_msg_t *ic_reg_lun; 2930 if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 && 2931 lu->lu_lp->lp_alua_support) { 2932 ilu->ilu_alua = 1; 2933 /* allocate the register message */ 2934 ic_reg_lun = ic_lun_active_msg_alloc(p1, 2935 lu->lu_lp->lp_name, 2936 lu->lu_proxy_reg_arg_len, 2937 (uint8_t *)lu->lu_proxy_reg_arg, 2938 stmf_proxy_msg_id); 2939 /* send the message */ 2940 if (ic_reg_lun) { 2941 ic_ret = ic_tx_msg(ic_reg_lun); 2942 if (ic_ret == STMF_IC_MSG_SUCCESS) { 2943 stmf_proxy_msg_id++; 2944 } 2945 } 2946 } 2947 } else if (stmf_state.stmf_alua_state == 1 && 2948 access_state == STMF_LU_STANDBY) { 2949 /* abort all tasks for this lu */ 2950 stmf_task_lu_killall(lu, NULL, STMF_ABORTED); 2951 } 2952 } 2953 2954 ilu->ilu_access = access_state; 2955 2956 mutex_exit(&stmf_state.stmf_lock); 2957 return (STMF_SUCCESS); 2958 } 2959 2960 2961 stmf_status_t 2962 stmf_register_lu(stmf_lu_t *lu) 2963 { 2964 stmf_i_lu_t *ilu; 2965 uint8_t *p1, *p2; 2966 stmf_state_change_info_t ssci; 2967 stmf_id_data_t *luid; 2968 2969 if ((lu->lu_id->ident_type != ID_TYPE_NAA) || 2970 (lu->lu_id->ident_length != 16) || 2971 ((lu->lu_id->ident[0] & 0xf0) != 0x60)) { 2972 return (STMF_INVALID_ARG); 2973 } 2974 p1 = &lu->lu_id->ident[0]; 2975 mutex_enter(&stmf_state.stmf_lock); 2976 if (stmf_state.stmf_inventory_locked) { 2977 mutex_exit(&stmf_state.stmf_lock); 2978 return (STMF_BUSY); 2979 } 2980 2981 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) { 2982 p2 = &ilu->ilu_lu->lu_id->ident[0]; 2983 if (bcmp(p1, p2, 16) == 0) { 2984 mutex_exit(&stmf_state.stmf_lock); 2985 return (STMF_ALREADY); 2986 } 2987 } 2988 2989 ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 2990 luid = stmf_lookup_id(&stmf_state.stmf_luid_list, 2991 lu->lu_id->ident_length, lu->lu_id->ident); 2992 if (luid) { 2993 luid->id_pt_to_object = (void *)ilu; 2994 ilu->ilu_luid = luid; 2995 } 2996 ilu->ilu_alias = NULL; 2997 2998 ilu->ilu_next = stmf_state.stmf_ilulist; 2999 ilu->ilu_prev = NULL; 3000 if (ilu->ilu_next) 3001 ilu->ilu_next->ilu_prev = ilu; 3002 stmf_state.stmf_ilulist = ilu; 3003 stmf_state.stmf_nlus++; 3004 if (lu->lu_lp) { 3005 ((stmf_i_lu_provider_t *) 3006 (lu->lu_lp->lp_stmf_private))->ilp_nlus++; 3007 } 3008 ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1; 3009 STMF_EVENT_ALLOC_HANDLE(ilu->ilu_event_hdl); 3010 stmf_create_kstat_lu(ilu); 3011 /* 3012 * register with proxy module if available and logical unit 3013 * is in active state 3014 */ 3015 if (stmf_state.stmf_alua_state == 1 && 3016 ilu->ilu_access == STMF_LU_ACTIVE) { 3017 stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS; 3018 stmf_ic_msg_t *ic_reg_lun; 3019 if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 && 3020 lu->lu_lp->lp_alua_support) { 3021 ilu->ilu_alua = 1; 3022 /* allocate the register message */ 3023 ic_reg_lun = ic_reg_lun_msg_alloc(p1, 3024 lu->lu_lp->lp_name, lu->lu_proxy_reg_arg_len, 3025 (uint8_t *)lu->lu_proxy_reg_arg, stmf_proxy_msg_id); 3026 /* send the message */ 3027 if (ic_reg_lun) { 3028 ic_ret = ic_tx_msg(ic_reg_lun); 3029 if (ic_ret == STMF_IC_MSG_SUCCESS) { 3030 stmf_proxy_msg_id++; 3031 } 3032 } 3033 } 3034 } 3035 mutex_exit(&stmf_state.stmf_lock); 3036 3037 /* XXX we should probably check if this lu can be brought online */ 3038 ilu->ilu_prev_state = STMF_STATE_ONLINE; 3039 if (stmf_state.stmf_service_running) { 3040 ssci.st_rflags = 0; 3041 ssci.st_additional_info = NULL; 3042 (void) stmf_ctl(STMF_CMD_LU_ONLINE, lu, &ssci); 3043 } 3044 3045 /* XXX: Generate event */ 3046 return (STMF_SUCCESS); 3047 } 3048 3049 stmf_status_t 3050 stmf_deregister_lu(stmf_lu_t *lu) 3051 { 3052 stmf_i_lu_t *ilu; 3053 3054 mutex_enter(&stmf_state.stmf_lock); 3055 if (stmf_state.stmf_inventory_locked) { 3056 mutex_exit(&stmf_state.stmf_lock); 3057 return (STMF_BUSY); 3058 } 3059 ilu = stmf_lookup_lu(lu); 3060 if (ilu == NULL) { 3061 mutex_exit(&stmf_state.stmf_lock); 3062 return (STMF_INVALID_ARG); 3063 } 3064 if (ilu->ilu_state == STMF_STATE_OFFLINE) { 3065 ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free); 3066 while (ilu->ilu_flags & ILU_STALL_DEREGISTER) { 3067 cv_wait(&stmf_state.stmf_cv, &stmf_state.stmf_lock); 3068 } 3069 if (ilu->ilu_ntasks) { 3070 stmf_i_scsi_task_t *itask, *nitask; 3071 3072 nitask = ilu->ilu_tasks; 3073 do { 3074 itask = nitask; 3075 nitask = itask->itask_lu_next; 3076 lu->lu_task_free(itask->itask_task); 3077 stmf_free(itask->itask_task); 3078 } while (nitask != NULL); 3079 3080 ilu->ilu_tasks = ilu->ilu_free_tasks = NULL; 3081 ilu->ilu_ntasks = ilu->ilu_ntasks_free = 0; 3082 } 3083 /* de-register with proxy if available */ 3084 if (ilu->ilu_access == STMF_LU_ACTIVE && 3085 stmf_state.stmf_alua_state == 1) { 3086 /* de-register with proxy module */ 3087 stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS; 3088 stmf_ic_msg_t *ic_dereg_lun; 3089 if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 && 3090 lu->lu_lp->lp_alua_support) { 3091 ilu->ilu_alua = 1; 3092 /* allocate the de-register message */ 3093 ic_dereg_lun = ic_dereg_lun_msg_alloc( 3094 lu->lu_id->ident, lu->lu_lp->lp_name, 0, 3095 NULL, stmf_proxy_msg_id); 3096 /* send the message */ 3097 if (ic_dereg_lun) { 3098 ic_ret = ic_tx_msg(ic_dereg_lun); 3099 if (ic_ret == STMF_IC_MSG_SUCCESS) { 3100 stmf_proxy_msg_id++; 3101 } 3102 } 3103 } 3104 } 3105 3106 if (ilu->ilu_next) 3107 ilu->ilu_next->ilu_prev = ilu->ilu_prev; 3108 if (ilu->ilu_prev) 3109 ilu->ilu_prev->ilu_next = ilu->ilu_next; 3110 else 3111 stmf_state.stmf_ilulist = ilu->ilu_next; 3112 stmf_state.stmf_nlus--; 3113 3114 if (ilu == stmf_state.stmf_svc_ilu_draining) { 3115 stmf_state.stmf_svc_ilu_draining = ilu->ilu_next; 3116 } 3117 if (ilu == stmf_state.stmf_svc_ilu_timing) { 3118 stmf_state.stmf_svc_ilu_timing = ilu->ilu_next; 3119 } 3120 if (lu->lu_lp) { 3121 ((stmf_i_lu_provider_t *) 3122 (lu->lu_lp->lp_stmf_private))->ilp_nlus--; 3123 } 3124 if (ilu->ilu_luid) { 3125 ((stmf_id_data_t *)ilu->ilu_luid)->id_pt_to_object = 3126 NULL; 3127 ilu->ilu_luid = NULL; 3128 } 3129 STMF_EVENT_FREE_HANDLE(ilu->ilu_event_hdl); 3130 } else { 3131 mutex_exit(&stmf_state.stmf_lock); 3132 return (STMF_BUSY); 3133 } 3134 if (ilu->ilu_kstat_info) { 3135 kmem_free(ilu->ilu_kstat_info->ks_data, 3136 ilu->ilu_kstat_info->ks_data_size); 3137 kstat_delete(ilu->ilu_kstat_info); 3138 } 3139 if (ilu->ilu_kstat_io) { 3140 kstat_delete(ilu->ilu_kstat_io); 3141 mutex_destroy(&ilu->ilu_kstat_lock); 3142 } 3143 stmf_delete_itl_kstat_by_guid(ilu->ilu_ascii_hex_guid); 3144 mutex_exit(&stmf_state.stmf_lock); 3145 return (STMF_SUCCESS); 3146 } 3147 3148 void 3149 stmf_set_port_standby(stmf_local_port_t *lport, uint16_t rtpid) 3150 { 3151 stmf_i_local_port_t *ilport = 3152 (stmf_i_local_port_t *)lport->lport_stmf_private; 3153 ilport->ilport_rtpid = rtpid; 3154 ilport->ilport_standby = 1; 3155 } 3156 3157 void 3158 stmf_set_port_alua(stmf_local_port_t *lport) 3159 { 3160 stmf_i_local_port_t *ilport = 3161 (stmf_i_local_port_t *)lport->lport_stmf_private; 3162 ilport->ilport_alua = 1; 3163 } 3164 3165 stmf_status_t 3166 stmf_register_local_port(stmf_local_port_t *lport) 3167 { 3168 stmf_i_local_port_t *ilport; 3169 stmf_state_change_info_t ssci; 3170 int start_workers = 0; 3171 3172 mutex_enter(&stmf_state.stmf_lock); 3173 if (stmf_state.stmf_inventory_locked) { 3174 mutex_exit(&stmf_state.stmf_lock); 3175 return (STMF_BUSY); 3176 } 3177 ilport = (stmf_i_local_port_t *)lport->lport_stmf_private; 3178 rw_init(&ilport->ilport_lock, NULL, RW_DRIVER, NULL); 3179 3180 ilport->ilport_instance = 3181 id_alloc_nosleep(stmf_state.stmf_ilport_inst_space); 3182 if (ilport->ilport_instance == -1) { 3183 mutex_exit(&stmf_state.stmf_lock); 3184 return (STMF_FAILURE); 3185 } 3186 ilport->ilport_next = stmf_state.stmf_ilportlist; 3187 ilport->ilport_prev = NULL; 3188 if (ilport->ilport_next) 3189 ilport->ilport_next->ilport_prev = ilport; 3190 stmf_state.stmf_ilportlist = ilport; 3191 stmf_state.stmf_nlports++; 3192 if (lport->lport_pp) { 3193 ((stmf_i_port_provider_t *) 3194 (lport->lport_pp->pp_stmf_private))->ipp_npps++; 3195 } 3196 ilport->ilport_tg = 3197 stmf_lookup_group_for_target(lport->lport_id->ident, 3198 lport->lport_id->ident_length); 3199 3200 /* 3201 * rtpid will/must be set if this is a standby port 3202 * only register ports that are not standby (proxy) ports 3203 * and ports that are alua participants (ilport_alua == 1) 3204 */ 3205 if (ilport->ilport_standby == 0) { 3206 ilport->ilport_rtpid = atomic_add_16_nv(&stmf_rtpid_counter, 1); 3207 } 3208 3209 if (stmf_state.stmf_alua_state == 1 && 3210 ilport->ilport_standby == 0 && 3211 ilport->ilport_alua == 1) { 3212 stmf_ic_msg_t *ic_reg_port; 3213 stmf_ic_msg_status_t ic_ret; 3214 stmf_local_port_t *lport; 3215 lport = ilport->ilport_lport; 3216 ic_reg_port = ic_reg_port_msg_alloc( 3217 lport->lport_id, ilport->ilport_rtpid, 3218 0, NULL, stmf_proxy_msg_id); 3219 if (ic_reg_port) { 3220 ic_ret = ic_tx_msg(ic_reg_port); 3221 if (ic_ret == STMF_IC_MSG_SUCCESS) { 3222 ilport->ilport_reg_msgid = stmf_proxy_msg_id++; 3223 } else { 3224 cmn_err(CE_WARN, "error on port registration " 3225 "port - %s", ilport->ilport_kstat_tgt_name); 3226 } 3227 } 3228 } 3229 STMF_EVENT_ALLOC_HANDLE(ilport->ilport_event_hdl); 3230 stmf_create_kstat_lport(ilport); 3231 if (stmf_workers_state == STMF_WORKERS_DISABLED) { 3232 stmf_workers_state = STMF_WORKERS_ENABLING; 3233 start_workers = 1; 3234 } 3235 mutex_exit(&stmf_state.stmf_lock); 3236 3237 if (start_workers) 3238 stmf_worker_init(); 3239 3240 /* XXX we should probably check if this lport can be brought online */ 3241 ilport->ilport_prev_state = STMF_STATE_ONLINE; 3242 if (stmf_state.stmf_service_running) { 3243 ssci.st_rflags = 0; 3244 ssci.st_additional_info = NULL; 3245 (void) stmf_ctl(STMF_CMD_LPORT_ONLINE, lport, &ssci); 3246 } 3247 3248 /* XXX: Generate event */ 3249 return (STMF_SUCCESS); 3250 } 3251 3252 stmf_status_t 3253 stmf_deregister_local_port(stmf_local_port_t *lport) 3254 { 3255 stmf_i_local_port_t *ilport; 3256 3257 mutex_enter(&stmf_state.stmf_lock); 3258 if (stmf_state.stmf_inventory_locked) { 3259 mutex_exit(&stmf_state.stmf_lock); 3260 return (STMF_BUSY); 3261 } 3262 3263 ilport = (stmf_i_local_port_t *)lport->lport_stmf_private; 3264 3265 /* 3266 * deregister ports that are not standby (proxy) 3267 */ 3268 if (stmf_state.stmf_alua_state == 1 && 3269 ilport->ilport_standby == 0 && 3270 ilport->ilport_alua == 1) { 3271 stmf_ic_msg_t *ic_dereg_port; 3272 stmf_ic_msg_status_t ic_ret; 3273 ic_dereg_port = ic_dereg_port_msg_alloc( 3274 lport->lport_id, 0, NULL, stmf_proxy_msg_id); 3275 if (ic_dereg_port) { 3276 ic_ret = ic_tx_msg(ic_dereg_port); 3277 if (ic_ret == STMF_IC_MSG_SUCCESS) { 3278 stmf_proxy_msg_id++; 3279 } 3280 } 3281 } 3282 3283 if (ilport->ilport_nsessions == 0) { 3284 if (ilport->ilport_next) 3285 ilport->ilport_next->ilport_prev = ilport->ilport_prev; 3286 if (ilport->ilport_prev) 3287 ilport->ilport_prev->ilport_next = ilport->ilport_next; 3288 else 3289 stmf_state.stmf_ilportlist = ilport->ilport_next; 3290 id_free(stmf_state.stmf_ilport_inst_space, 3291 ilport->ilport_instance); 3292 rw_destroy(&ilport->ilport_lock); 3293 stmf_state.stmf_nlports--; 3294 if (lport->lport_pp) { 3295 ((stmf_i_port_provider_t *) 3296 (lport->lport_pp->pp_stmf_private))->ipp_npps--; 3297 } 3298 ilport->ilport_tg = NULL; 3299 STMF_EVENT_FREE_HANDLE(ilport->ilport_event_hdl); 3300 } else { 3301 mutex_exit(&stmf_state.stmf_lock); 3302 return (STMF_BUSY); 3303 } 3304 if (ilport->ilport_kstat_info) { 3305 kmem_free(ilport->ilport_kstat_info->ks_data, 3306 ilport->ilport_kstat_info->ks_data_size); 3307 kstat_delete(ilport->ilport_kstat_info); 3308 } 3309 if (ilport->ilport_kstat_io) { 3310 kstat_delete(ilport->ilport_kstat_io); 3311 mutex_destroy(&ilport->ilport_kstat_lock); 3312 } 3313 stmf_delete_itl_kstat_by_lport(ilport->ilport_kstat_tgt_name); 3314 mutex_exit(&stmf_state.stmf_lock); 3315 return (STMF_SUCCESS); 3316 } 3317 3318 /* 3319 * Rport id/instance mappings remain valid until STMF is unloaded 3320 */ 3321 static int 3322 stmf_irport_compare(const void *void_irport1, const void *void_irport2) 3323 { 3324 const stmf_i_remote_port_t *irport1 = void_irport1; 3325 const stmf_i_remote_port_t *irport2 = void_irport2; 3326 int result; 3327 3328 /* Sort by code set then ident */ 3329 if (irport1->irport_id->code_set < 3330 irport2->irport_id->code_set) { 3331 return (-1); 3332 } else if (irport1->irport_id->code_set > 3333 irport2->irport_id->code_set) { 3334 return (1); 3335 } 3336 3337 /* Next by ident length */ 3338 if (irport1->irport_id->ident_length < 3339 irport2->irport_id->ident_length) { 3340 return (-1); 3341 } else if (irport1->irport_id->ident_length > 3342 irport2->irport_id->ident_length) { 3343 return (1); 3344 } 3345 3346 /* Code set and ident length both match, now compare idents */ 3347 result = memcmp(irport1->irport_id->ident, 3348 irport2->irport_id->ident, 3349 irport1->irport_id->ident_length); 3350 3351 if (result < 0) { 3352 return (-1); 3353 } else if (result > 0) { 3354 return (1); 3355 } 3356 3357 return (0); 3358 } 3359 3360 static stmf_i_remote_port_t * 3361 stmf_irport_create(scsi_devid_desc_t *rport_devid) 3362 { 3363 int alloc_len; 3364 stmf_i_remote_port_t *irport; 3365 3366 /* 3367 * Lookup will bump the refcnt if there's an existing rport 3368 * context for this identifier. 3369 */ 3370 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 3371 3372 alloc_len = sizeof (*irport) + sizeof (scsi_devid_desc_t) + 3373 rport_devid->ident_length - 1; 3374 irport = kmem_zalloc(alloc_len, KM_NOSLEEP); 3375 if (irport == NULL) { 3376 return (NULL); 3377 } 3378 3379 irport->irport_instance = 3380 id_alloc_nosleep(stmf_state.stmf_irport_inst_space); 3381 if (irport->irport_instance == -1) { 3382 kmem_free(irport, alloc_len); 3383 return (NULL); 3384 } 3385 3386 irport->irport_id = 3387 (struct scsi_devid_desc *)(irport + 1); /* Ptr. Arith. */ 3388 bcopy(rport_devid, irport->irport_id, 3389 sizeof (scsi_devid_desc_t) + rport_devid->ident_length - 1); 3390 irport->irport_refcnt = 1; 3391 mutex_init(&irport->irport_mutex, NULL, MUTEX_DEFAULT, NULL); 3392 3393 return (irport); 3394 } 3395 3396 static void 3397 stmf_irport_destroy(stmf_i_remote_port_t *irport) 3398 { 3399 id_free(stmf_state.stmf_irport_inst_space, irport->irport_instance); 3400 mutex_destroy(&irport->irport_mutex); 3401 kmem_free(irport, sizeof (*irport) + sizeof (scsi_devid_desc_t) + 3402 irport->irport_id->ident_length - 1); 3403 } 3404 3405 static stmf_i_remote_port_t * 3406 stmf_irport_register(scsi_devid_desc_t *rport_devid) 3407 { 3408 stmf_i_remote_port_t *irport; 3409 3410 mutex_enter(&stmf_state.stmf_lock); 3411 3412 /* 3413 * Lookup will bump the refcnt if there's an existing rport 3414 * context for this identifier. 3415 */ 3416 if ((irport = stmf_irport_lookup_locked(rport_devid)) != NULL) { 3417 mutex_exit(&stmf_state.stmf_lock); 3418 return (irport); 3419 } 3420 3421 irport = stmf_irport_create(rport_devid); 3422 if (irport == NULL) { 3423 mutex_exit(&stmf_state.stmf_lock); 3424 return (NULL); 3425 } 3426 3427 avl_add(&stmf_state.stmf_irportlist, irport); 3428 mutex_exit(&stmf_state.stmf_lock); 3429 3430 return (irport); 3431 } 3432 3433 static stmf_i_remote_port_t * 3434 stmf_irport_lookup_locked(scsi_devid_desc_t *rport_devid) 3435 { 3436 stmf_i_remote_port_t *irport; 3437 stmf_i_remote_port_t tmp_irport; 3438 3439 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 3440 tmp_irport.irport_id = rport_devid; 3441 irport = avl_find(&stmf_state.stmf_irportlist, &tmp_irport, NULL); 3442 if (irport != NULL) { 3443 mutex_enter(&irport->irport_mutex); 3444 irport->irport_refcnt++; 3445 mutex_exit(&irport->irport_mutex); 3446 } 3447 3448 return (irport); 3449 } 3450 3451 static void 3452 stmf_irport_deregister(stmf_i_remote_port_t *irport) 3453 { 3454 /* 3455 * If we were actually going to remove unreferenced remote ports 3456 * we would want to acquire stmf_state.stmf_lock before getting 3457 * the irport mutex. 3458 * 3459 * Instead we're just going to leave it there even if unreferenced. 3460 */ 3461 mutex_enter(&irport->irport_mutex); 3462 irport->irport_refcnt--; 3463 mutex_exit(&irport->irport_mutex); 3464 } 3465 3466 /* 3467 * Port provider has to make sure that register/deregister session and 3468 * port are serialized calls. 3469 */ 3470 stmf_status_t 3471 stmf_register_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss) 3472 { 3473 stmf_i_scsi_session_t *iss; 3474 stmf_i_local_port_t *ilport = (stmf_i_local_port_t *) 3475 lport->lport_stmf_private; 3476 uint8_t lun[8]; 3477 3478 /* 3479 * Port state has to be online to register a scsi session. It is 3480 * possible that we started an offline operation and a new SCSI 3481 * session started at the same time (in that case also we are going 3482 * to fail the registeration). But any other state is simply 3483 * a bad port provider implementation. 3484 */ 3485 if (ilport->ilport_state != STMF_STATE_ONLINE) { 3486 if (ilport->ilport_state != STMF_STATE_OFFLINING) { 3487 stmf_trace(lport->lport_alias, "Port is trying to " 3488 "register a session while the state is neither " 3489 "online nor offlining"); 3490 } 3491 return (STMF_FAILURE); 3492 } 3493 bzero(lun, 8); 3494 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private; 3495 if ((iss->iss_irport = stmf_irport_register(ss->ss_rport_id)) == NULL) { 3496 stmf_trace(lport->lport_alias, "Could not register " 3497 "remote port during session registration"); 3498 return (STMF_FAILURE); 3499 } 3500 3501 iss->iss_flags |= ISS_BEING_CREATED; 3502 3503 /* sessions use the ilport_lock. No separate lock is required */ 3504 iss->iss_lockp = &ilport->ilport_lock; 3505 3506 if (iss->iss_sm != NULL) 3507 cmn_err(CE_PANIC, "create lun map called with non NULL map"); 3508 iss->iss_sm = (stmf_lun_map_t *)kmem_zalloc(sizeof (stmf_lun_map_t), 3509 KM_SLEEP); 3510 3511 mutex_enter(&stmf_state.stmf_lock); 3512 rw_enter(&ilport->ilport_lock, RW_WRITER); 3513 (void) stmf_session_create_lun_map(ilport, iss); 3514 ilport->ilport_nsessions++; 3515 iss->iss_next = ilport->ilport_ss_list; 3516 ilport->ilport_ss_list = iss; 3517 rw_exit(&ilport->ilport_lock); 3518 mutex_exit(&stmf_state.stmf_lock); 3519 3520 iss->iss_creation_time = ddi_get_time(); 3521 ss->ss_session_id = atomic_add_64_nv(&stmf_session_counter, 1); 3522 iss->iss_flags &= ~ISS_BEING_CREATED; 3523 /* XXX should we remove ISS_LUN_INVENTORY_CHANGED on new session? */ 3524 iss->iss_flags &= ~ISS_LUN_INVENTORY_CHANGED; 3525 DTRACE_PROBE2(session__online, stmf_local_port_t *, lport, 3526 stmf_scsi_session_t *, ss); 3527 return (STMF_SUCCESS); 3528 } 3529 3530 void 3531 stmf_deregister_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss) 3532 { 3533 stmf_i_local_port_t *ilport = (stmf_i_local_port_t *) 3534 lport->lport_stmf_private; 3535 stmf_i_scsi_session_t *iss, **ppss; 3536 int found = 0; 3537 stmf_ic_msg_t *ic_session_dereg; 3538 stmf_status_t ic_ret = STMF_FAILURE; 3539 3540 DTRACE_PROBE2(session__offline, stmf_local_port_t *, lport, 3541 stmf_scsi_session_t *, ss); 3542 3543 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private; 3544 if (ss->ss_rport_alias) { 3545 ss->ss_rport_alias = NULL; 3546 } 3547 3548 try_dereg_ss_again: 3549 mutex_enter(&stmf_state.stmf_lock); 3550 atomic_and_32(&iss->iss_flags, 3551 ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS)); 3552 if (iss->iss_flags & ISS_EVENT_ACTIVE) { 3553 mutex_exit(&stmf_state.stmf_lock); 3554 delay(1); 3555 goto try_dereg_ss_again; 3556 } 3557 3558 /* dereg proxy session if not standby port */ 3559 if (stmf_state.stmf_alua_state == 1 && 3560 ilport->ilport_standby == 0 && 3561 ilport->ilport_alua == 1) { 3562 ic_session_dereg = ic_session_dereg_msg_alloc( 3563 ss, stmf_proxy_msg_id); 3564 if (ic_session_dereg) { 3565 ic_ret = ic_tx_msg(ic_session_dereg); 3566 if (ic_ret == STMF_IC_MSG_SUCCESS) { 3567 stmf_proxy_msg_id++; 3568 } 3569 } 3570 } 3571 3572 rw_enter(&ilport->ilport_lock, RW_WRITER); 3573 for (ppss = &ilport->ilport_ss_list; *ppss != NULL; 3574 ppss = &((*ppss)->iss_next)) { 3575 if (iss == (*ppss)) { 3576 *ppss = (*ppss)->iss_next; 3577 found = 1; 3578 break; 3579 } 3580 } 3581 if (!found) { 3582 cmn_err(CE_PANIC, "Deregister session called for non existent" 3583 " session"); 3584 } 3585 ilport->ilport_nsessions--; 3586 3587 stmf_irport_deregister(iss->iss_irport); 3588 (void) stmf_session_destroy_lun_map(ilport, iss); 3589 rw_exit(&ilport->ilport_lock); 3590 mutex_exit(&stmf_state.stmf_lock); 3591 } 3592 3593 stmf_i_scsi_session_t * 3594 stmf_session_id_to_issptr(uint64_t session_id, int stay_locked) 3595 { 3596 stmf_i_local_port_t *ilport; 3597 stmf_i_scsi_session_t *iss; 3598 3599 mutex_enter(&stmf_state.stmf_lock); 3600 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 3601 ilport = ilport->ilport_next) { 3602 rw_enter(&ilport->ilport_lock, RW_WRITER); 3603 for (iss = ilport->ilport_ss_list; iss != NULL; 3604 iss = iss->iss_next) { 3605 if (iss->iss_ss->ss_session_id == session_id) { 3606 if (!stay_locked) 3607 rw_exit(&ilport->ilport_lock); 3608 mutex_exit(&stmf_state.stmf_lock); 3609 return (iss); 3610 } 3611 } 3612 rw_exit(&ilport->ilport_lock); 3613 } 3614 mutex_exit(&stmf_state.stmf_lock); 3615 return (NULL); 3616 } 3617 3618 #define MAX_ALIAS 128 3619 3620 static int 3621 stmf_itl_kstat_compare(const void *itl_kstat_1, const void *itl_kstat_2) 3622 { 3623 const stmf_i_itl_kstat_t *kstat_nm1 = itl_kstat_1; 3624 const stmf_i_itl_kstat_t *kstat_nm2 = itl_kstat_2; 3625 int ret; 3626 3627 ret = strcmp(kstat_nm1->iitl_kstat_nm, kstat_nm2->iitl_kstat_nm); 3628 if (ret < 0) { 3629 return (-1); 3630 } else if (ret > 0) { 3631 return (1); 3632 } 3633 return (0); 3634 } 3635 3636 static stmf_i_itl_kstat_t * 3637 stmf_itl_kstat_lookup(char *kstat_nm) 3638 { 3639 stmf_i_itl_kstat_t tmp; 3640 stmf_i_itl_kstat_t *itl_kstat; 3641 3642 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 3643 (void) strcpy(tmp.iitl_kstat_nm, kstat_nm); 3644 itl_kstat = avl_find(&stmf_state.stmf_itl_kstat_list, &tmp, NULL); 3645 return (itl_kstat); 3646 } 3647 3648 static void 3649 stmf_delete_itl_kstat_by_lport(char *tgt) 3650 { 3651 stmf_i_itl_kstat_t *ks_itl, *next; 3652 3653 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 3654 ks_itl = avl_first(&stmf_state.stmf_itl_kstat_list); 3655 for (; ks_itl != NULL; ks_itl = next) { 3656 next = AVL_NEXT(&stmf_state.stmf_itl_kstat_list, ks_itl); 3657 if (strcmp(ks_itl->iitl_kstat_lport, tgt) == 0) { 3658 stmf_teardown_itl_kstats(ks_itl); 3659 avl_remove(&stmf_state.stmf_itl_kstat_list, ks_itl); 3660 kmem_free(ks_itl, sizeof (stmf_i_itl_kstat_t)); 3661 } 3662 } 3663 } 3664 3665 static void 3666 stmf_delete_itl_kstat_by_guid(char *guid) 3667 { 3668 stmf_i_itl_kstat_t *ks_itl, *next; 3669 3670 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 3671 ks_itl = avl_first(&stmf_state.stmf_itl_kstat_list); 3672 for (; ks_itl != NULL; ks_itl = next) { 3673 next = AVL_NEXT(&stmf_state.stmf_itl_kstat_list, ks_itl); 3674 if (strcmp(ks_itl->iitl_kstat_guid, guid) == 0) { 3675 stmf_teardown_itl_kstats(ks_itl); 3676 avl_remove(&stmf_state.stmf_itl_kstat_list, ks_itl); 3677 kmem_free(ks_itl, sizeof (stmf_i_itl_kstat_t)); 3678 } 3679 } 3680 } 3681 3682 static stmf_i_itl_kstat_t * 3683 stmf_itl_kstat_create(stmf_itl_data_t *itl, char *nm, 3684 scsi_devid_desc_t *lport, scsi_devid_desc_t *lun) 3685 { 3686 stmf_i_itl_kstat_t *ks_itl; 3687 int i, len; 3688 3689 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 3690 if ((ks_itl = stmf_itl_kstat_lookup(nm)) != NULL) 3691 return (ks_itl); 3692 3693 len = sizeof (stmf_i_itl_kstat_t); 3694 ks_itl = kmem_zalloc(len, KM_NOSLEEP); 3695 if (ks_itl == NULL) 3696 return (NULL); 3697 3698 (void) strcpy(ks_itl->iitl_kstat_nm, nm); 3699 bcopy(lport->ident, ks_itl->iitl_kstat_lport, lport->ident_length); 3700 ks_itl->iitl_kstat_lport[lport->ident_length] = '\0'; 3701 for (i = 0; i < STMF_GUID_INPUT / 2; i++) { 3702 (void) sprintf(&ks_itl->iitl_kstat_guid[i * 2], "%02x", 3703 lun->ident[i]); 3704 } 3705 ks_itl->iitl_kstat_strbuf = itl->itl_kstat_strbuf; 3706 ks_itl->iitl_kstat_strbuflen = itl->itl_kstat_strbuflen; 3707 ks_itl->iitl_kstat_info = itl->itl_kstat_info; 3708 ks_itl->iitl_kstat_taskq = itl->itl_kstat_taskq; 3709 ks_itl->iitl_kstat_lu_xfer = itl->itl_kstat_lu_xfer; 3710 ks_itl->iitl_kstat_lport_xfer = itl->itl_kstat_lport_xfer; 3711 avl_add(&stmf_state.stmf_itl_kstat_list, ks_itl); 3712 3713 return (ks_itl); 3714 } 3715 3716 stmf_status_t 3717 stmf_setup_itl_kstats(stmf_itl_data_t *itl) 3718 { 3719 char ks_itl_id[32]; 3720 char ks_nm[KSTAT_STRLEN]; 3721 char ks_itl_nm[KSTAT_STRLEN]; 3722 stmf_kstat_itl_info_t *ks_itl; 3723 stmf_scsi_session_t *ss; 3724 stmf_i_scsi_session_t *iss; 3725 stmf_i_local_port_t *ilport; 3726 char *strbuf; 3727 int id, len, i; 3728 char *rport_alias; 3729 char *lport_alias; 3730 char *lu_alias; 3731 stmf_i_itl_kstat_t *tmp_kstat; 3732 3733 /* 3734 * Allocate enough memory in the ITL to hold the relevant 3735 * identifiers. 3736 * rport and lport identifiers come from the stmf_scsi_session_t. 3737 * ident might not be null terminated. 3738 */ 3739 ss = itl->itl_session->iss_ss; 3740 iss = ss->ss_stmf_private; 3741 ilport = ss->ss_lport->lport_stmf_private; 3742 (void) snprintf(ks_itl_id, 32, "%d.%d.%d", 3743 iss->iss_irport->irport_instance, ilport->ilport_instance, 3744 itl->itl_lun); 3745 3746 (void) snprintf(ks_itl_nm, KSTAT_STRLEN, "itl_%s", ks_itl_id); 3747 /* 3748 * let's verify this itl_kstat already exist 3749 */ 3750 if ((tmp_kstat = stmf_itl_kstat_lookup(ks_itl_nm)) != NULL) { 3751 itl->itl_kstat_strbuf = tmp_kstat->iitl_kstat_strbuf; 3752 itl->itl_kstat_strbuflen = tmp_kstat->iitl_kstat_strbuflen; 3753 itl->itl_kstat_info = tmp_kstat->iitl_kstat_info; 3754 itl->itl_kstat_taskq = tmp_kstat->iitl_kstat_taskq; 3755 itl->itl_kstat_lu_xfer = tmp_kstat->iitl_kstat_lu_xfer; 3756 itl->itl_kstat_lport_xfer = tmp_kstat->iitl_kstat_lport_xfer; 3757 return (STMF_SUCCESS); 3758 } 3759 3760 /* New itl_kstat */ 3761 rport_alias = (ss->ss_rport_alias == NULL) ? 3762 "" : ss->ss_rport_alias; 3763 lport_alias = (ss->ss_lport->lport_alias == NULL) ? 3764 "" : ss->ss_lport->lport_alias; 3765 lu_alias = (itl->itl_ilu->ilu_lu->lu_alias == NULL) ? 3766 "" : itl->itl_ilu->ilu_lu->lu_alias; 3767 3768 itl->itl_kstat_strbuflen = (ss->ss_rport_id->ident_length + 1) + 3769 (strnlen(rport_alias, MAX_ALIAS) + 1) + 3770 (ss->ss_lport->lport_id->ident_length + 1) + 3771 (strnlen(lport_alias, MAX_ALIAS) + 1) + 3772 (STMF_GUID_INPUT + 1) + 3773 (strnlen(lu_alias, MAX_ALIAS) + 1) + 3774 MAX_PROTO_STR_LEN; 3775 itl->itl_kstat_strbuf = kmem_zalloc(itl->itl_kstat_strbuflen, 3776 KM_NOSLEEP); 3777 if (itl->itl_kstat_strbuf == NULL) { 3778 return (STMF_ALLOC_FAILURE); 3779 } 3780 3781 ks_itl = (stmf_kstat_itl_info_t *)kmem_zalloc(sizeof (*ks_itl), 3782 KM_NOSLEEP); 3783 if (ks_itl == NULL) { 3784 kmem_free(itl->itl_kstat_strbuf, itl->itl_kstat_strbuflen); 3785 return (STMF_ALLOC_FAILURE); 3786 } 3787 3788 if ((itl->itl_kstat_info = kstat_create(STMF_MODULE_NAME, 3789 0, ks_itl_nm, "misc", KSTAT_TYPE_NAMED, 3790 sizeof (stmf_kstat_itl_info_t) / sizeof (kstat_named_t), 3791 KSTAT_FLAG_VIRTUAL)) == NULL) { 3792 goto itl_kstat_cleanup; 3793 } 3794 3795 itl->itl_kstat_info->ks_data_size += itl->itl_kstat_strbuflen; 3796 itl->itl_kstat_info->ks_data = ks_itl; 3797 3798 kstat_named_init(&ks_itl->i_rport_name, "rport-name", 3799 KSTAT_DATA_STRING); 3800 kstat_named_init(&ks_itl->i_rport_alias, "rport-alias", 3801 KSTAT_DATA_STRING); 3802 kstat_named_init(&ks_itl->i_lport_name, "lport-name", 3803 KSTAT_DATA_STRING); 3804 kstat_named_init(&ks_itl->i_lport_alias, "lport-alias", 3805 KSTAT_DATA_STRING); 3806 kstat_named_init(&ks_itl->i_protocol, "protocol", 3807 KSTAT_DATA_STRING); 3808 kstat_named_init(&ks_itl->i_lu_guid, "lu-guid", 3809 KSTAT_DATA_STRING); 3810 kstat_named_init(&ks_itl->i_lu_alias, "lu-alias", 3811 KSTAT_DATA_STRING); 3812 kstat_named_init(&ks_itl->i_lu_number, "lu-number", 3813 KSTAT_DATA_UINT64); 3814 kstat_named_init(&ks_itl->i_task_waitq_elapsed, "task-waitq-elapsed", 3815 KSTAT_DATA_UINT64); 3816 kstat_named_init(&ks_itl->i_task_read_elapsed, "task-read-elapsed", 3817 KSTAT_DATA_UINT64); 3818 kstat_named_init(&ks_itl->i_task_write_elapsed, "task-write-elapsed", 3819 KSTAT_DATA_UINT64); 3820 kstat_named_init(&ks_itl->i_lu_read_elapsed, "lu-read-elapsed", 3821 KSTAT_DATA_UINT64); 3822 kstat_named_init(&ks_itl->i_lu_write_elapsed, "lu-write-elapsed", 3823 KSTAT_DATA_UINT64); 3824 kstat_named_init(&ks_itl->i_lport_read_elapsed, "lport-read-elapsed", 3825 KSTAT_DATA_UINT64); 3826 kstat_named_init(&ks_itl->i_lport_write_elapsed, "lport-write-elapsed", 3827 KSTAT_DATA_UINT64); 3828 3829 strbuf = itl->itl_kstat_strbuf; 3830 3831 /* Rport */ 3832 len = ss->ss_rport_id->ident_length; 3833 bcopy(ss->ss_rport_id->ident, strbuf, len); 3834 strbuf += len; 3835 *strbuf = '\0'; 3836 kstat_named_setstr(&ks_itl->i_rport_name, strbuf - len); 3837 strbuf++; 3838 3839 len = strnlen(rport_alias, MAX_ALIAS); 3840 (void) strncpy(strbuf, rport_alias, len + 1); 3841 kstat_named_setstr(&ks_itl->i_rport_alias, strbuf); 3842 strbuf += len + 1; 3843 3844 /* Lport */ 3845 len = ss->ss_lport->lport_id->ident_length; 3846 bcopy(ss->ss_lport->lport_id->ident, strbuf, len); 3847 strbuf += len; 3848 *strbuf = '\0'; 3849 kstat_named_setstr(&ks_itl->i_lport_name, strbuf - len); 3850 strbuf++; 3851 3852 len = strnlen(lport_alias, MAX_ALIAS); 3853 (void) strncpy(strbuf, lport_alias, len + 1); 3854 kstat_named_setstr(&ks_itl->i_lport_alias, strbuf); 3855 strbuf += len + 1; 3856 3857 id = (ss->ss_lport->lport_id->protocol_id > PROTOCOL_ANY) ? 3858 PROTOCOL_ANY : ss->ss_lport->lport_id->protocol_id; 3859 kstat_named_setstr(&ks_itl->i_protocol, protocol_ident[id]); 3860 3861 /* LU */ 3862 for (i = 0; i < STMF_GUID_INPUT / 2; i++) { 3863 (void) sprintf(&strbuf[i * 2], "%02x", 3864 itl->itl_ilu->ilu_lu->lu_id->ident[i]); 3865 } 3866 kstat_named_setstr(&ks_itl->i_lu_guid, strbuf); 3867 strbuf += STMF_GUID_INPUT + 1; 3868 3869 len = strnlen(lu_alias, MAX_ALIAS); 3870 (void) strncpy(strbuf, lu_alias, len + 1); 3871 kstat_named_setstr(&ks_itl->i_lu_alias, strbuf); 3872 strbuf += len + 1; 3873 3874 ks_itl->i_lu_number.value.ui64 = itl->itl_lun; 3875 3876 /* Now create the I/O kstats */ 3877 (void) snprintf(ks_nm, KSTAT_STRLEN, "itl_tasks_%s", ks_itl_id); 3878 if ((itl->itl_kstat_taskq = kstat_create(STMF_MODULE_NAME, 0, 3879 ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) { 3880 goto itl_kstat_cleanup; 3881 } 3882 3883 (void) snprintf(ks_nm, KSTAT_STRLEN, "itl_lu_%s", ks_itl_id); 3884 if ((itl->itl_kstat_lu_xfer = kstat_create(STMF_MODULE_NAME, 0, 3885 ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) { 3886 goto itl_kstat_cleanup; 3887 } 3888 3889 (void) snprintf(ks_nm, KSTAT_STRLEN, "itl_lport_%s", ks_itl_id); 3890 if ((itl->itl_kstat_lport_xfer = kstat_create(STMF_MODULE_NAME, 0, 3891 ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) { 3892 goto itl_kstat_cleanup; 3893 } 3894 3895 /* Install all the kstats */ 3896 kstat_install(itl->itl_kstat_info); 3897 kstat_install(itl->itl_kstat_taskq); 3898 kstat_install(itl->itl_kstat_lu_xfer); 3899 kstat_install(itl->itl_kstat_lport_xfer); 3900 3901 /* Add new itl_kstat to stmf_itl_kstat_list */ 3902 if (stmf_itl_kstat_create(itl, ks_itl_nm, ss->ss_lport->lport_id, 3903 itl->itl_ilu->ilu_lu->lu_id) != NULL) 3904 return (STMF_SUCCESS); 3905 3906 itl_kstat_cleanup: 3907 if (itl->itl_kstat_taskq) 3908 kstat_delete(itl->itl_kstat_taskq); 3909 if (itl->itl_kstat_lu_xfer) 3910 kstat_delete(itl->itl_kstat_lu_xfer); 3911 if (itl->itl_kstat_lport_xfer) 3912 kstat_delete(itl->itl_kstat_lport_xfer); 3913 if (itl->itl_kstat_info) 3914 kstat_delete(itl->itl_kstat_info); 3915 kmem_free(ks_itl, sizeof (*ks_itl)); 3916 kmem_free(itl->itl_kstat_strbuf, itl->itl_kstat_strbuflen); 3917 cmn_err(CE_WARN, "STMF: kstat_create itl failed"); 3918 return (STMF_ALLOC_FAILURE); 3919 } 3920 3921 static void 3922 stmf_teardown_itl_kstats(stmf_i_itl_kstat_t *ks) 3923 { 3924 kstat_delete(ks->iitl_kstat_lport_xfer); 3925 kstat_delete(ks->iitl_kstat_lu_xfer); 3926 kstat_delete(ks->iitl_kstat_taskq); 3927 kmem_free(ks->iitl_kstat_info->ks_data, sizeof (stmf_kstat_itl_info_t)); 3928 kstat_delete(ks->iitl_kstat_info); 3929 kmem_free(ks->iitl_kstat_strbuf, ks->iitl_kstat_strbuflen); 3930 } 3931 3932 void 3933 stmf_release_itl_handle(stmf_lu_t *lu, stmf_itl_data_t *itl) 3934 { 3935 stmf_itl_data_t **itlpp; 3936 stmf_i_lu_t *ilu; 3937 3938 ASSERT(itl->itl_flags & STMF_ITL_BEING_TERMINATED); 3939 3940 ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 3941 mutex_enter(&ilu->ilu_task_lock); 3942 for (itlpp = &ilu->ilu_itl_list; (*itlpp) != NULL; 3943 itlpp = &(*itlpp)->itl_next) { 3944 if ((*itlpp) == itl) 3945 break; 3946 } 3947 ASSERT((*itlpp) != NULL); 3948 *itlpp = itl->itl_next; 3949 mutex_exit(&ilu->ilu_task_lock); 3950 lu->lu_abort(lu, STMF_LU_ITL_HANDLE_REMOVED, itl->itl_handle, 3951 (uint32_t)itl->itl_hdlrm_reason); 3952 3953 kmem_free(itl, sizeof (*itl)); 3954 } 3955 3956 stmf_status_t 3957 stmf_register_itl_handle(stmf_lu_t *lu, uint8_t *lun, 3958 stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle) 3959 { 3960 stmf_itl_data_t *itl; 3961 stmf_i_scsi_session_t *iss; 3962 stmf_lun_map_ent_t *lun_map_ent; 3963 stmf_i_lu_t *ilu; 3964 uint16_t n; 3965 3966 ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 3967 if (ss == NULL) { 3968 iss = stmf_session_id_to_issptr(session_id, 1); 3969 if (iss == NULL) 3970 return (STMF_NOT_FOUND); 3971 } else { 3972 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private; 3973 } 3974 3975 /* 3976 * Acquire stmf_lock for stmf_itl_kstat_lookup. 3977 */ 3978 mutex_enter(&stmf_state.stmf_lock); 3979 rw_enter(iss->iss_lockp, RW_WRITER); 3980 n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8)); 3981 lun_map_ent = (stmf_lun_map_ent_t *) 3982 stmf_get_ent_from_map(iss->iss_sm, n); 3983 if ((lun_map_ent == NULL) || (lun_map_ent->ent_lu != lu)) { 3984 rw_exit(iss->iss_lockp); 3985 mutex_exit(&stmf_state.stmf_lock); 3986 return (STMF_NOT_FOUND); 3987 } 3988 if (lun_map_ent->ent_itl_datap != NULL) { 3989 rw_exit(iss->iss_lockp); 3990 mutex_exit(&stmf_state.stmf_lock); 3991 return (STMF_ALREADY); 3992 } 3993 3994 itl = (stmf_itl_data_t *)kmem_zalloc(sizeof (*itl), KM_NOSLEEP); 3995 if (itl == NULL) { 3996 rw_exit(iss->iss_lockp); 3997 mutex_exit(&stmf_state.stmf_lock); 3998 return (STMF_ALLOC_FAILURE); 3999 } 4000 4001 itl->itl_ilu = ilu; 4002 itl->itl_session = iss; 4003 itl->itl_counter = 1; 4004 itl->itl_lun = n; 4005 itl->itl_handle = itl_handle; 4006 4007 if (stmf_setup_itl_kstats(itl) != STMF_SUCCESS) { 4008 kmem_free(itl, sizeof (*itl)); 4009 rw_exit(iss->iss_lockp); 4010 mutex_exit(&stmf_state.stmf_lock); 4011 return (STMF_ALLOC_FAILURE); 4012 } 4013 4014 mutex_enter(&ilu->ilu_task_lock); 4015 itl->itl_next = ilu->ilu_itl_list; 4016 ilu->ilu_itl_list = itl; 4017 mutex_exit(&ilu->ilu_task_lock); 4018 lun_map_ent->ent_itl_datap = itl; 4019 rw_exit(iss->iss_lockp); 4020 mutex_exit(&stmf_state.stmf_lock); 4021 4022 return (STMF_SUCCESS); 4023 } 4024 4025 void 4026 stmf_do_itl_dereg(stmf_lu_t *lu, stmf_itl_data_t *itl, uint8_t hdlrm_reason) 4027 { 4028 uint8_t old, new; 4029 4030 do { 4031 old = new = itl->itl_flags; 4032 if (old & STMF_ITL_BEING_TERMINATED) 4033 return; 4034 new |= STMF_ITL_BEING_TERMINATED; 4035 } while (atomic_cas_8(&itl->itl_flags, old, new) != old); 4036 itl->itl_hdlrm_reason = hdlrm_reason; 4037 4038 ASSERT(itl->itl_counter); 4039 4040 if (atomic_add_32_nv(&itl->itl_counter, -1)) 4041 return; 4042 4043 drv_usecwait(10); 4044 if (itl->itl_counter) 4045 return; 4046 4047 stmf_release_itl_handle(lu, itl); 4048 } 4049 4050 stmf_status_t 4051 stmf_deregister_all_lu_itl_handles(stmf_lu_t *lu) 4052 { 4053 stmf_i_lu_t *ilu; 4054 stmf_i_local_port_t *ilport; 4055 stmf_i_scsi_session_t *iss; 4056 stmf_lun_map_t *lm; 4057 stmf_lun_map_ent_t *ent; 4058 uint32_t nmaps, nu; 4059 stmf_itl_data_t **itl_list; 4060 int i; 4061 4062 ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 4063 4064 dereg_itl_start:; 4065 nmaps = ilu->ilu_ref_cnt; 4066 if (nmaps == 0) 4067 return (STMF_NOT_FOUND); 4068 itl_list = (stmf_itl_data_t **)kmem_zalloc( 4069 nmaps * sizeof (stmf_itl_data_t *), KM_SLEEP); 4070 mutex_enter(&stmf_state.stmf_lock); 4071 if (nmaps != ilu->ilu_ref_cnt) { 4072 /* Something changed, start all over */ 4073 mutex_exit(&stmf_state.stmf_lock); 4074 kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *)); 4075 goto dereg_itl_start; 4076 } 4077 nu = 0; 4078 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 4079 ilport = ilport->ilport_next) { 4080 rw_enter(&ilport->ilport_lock, RW_WRITER); 4081 for (iss = ilport->ilport_ss_list; iss != NULL; 4082 iss = iss->iss_next) { 4083 lm = iss->iss_sm; 4084 if (!lm) 4085 continue; 4086 for (i = 0; i < lm->lm_nentries; i++) { 4087 if (lm->lm_plus[i] == NULL) 4088 continue; 4089 ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 4090 if ((ent->ent_lu == lu) && 4091 (ent->ent_itl_datap)) { 4092 itl_list[nu++] = ent->ent_itl_datap; 4093 ent->ent_itl_datap = NULL; 4094 if (nu == nmaps) { 4095 rw_exit(&ilport->ilport_lock); 4096 goto dai_scan_done; 4097 } 4098 } 4099 } /* lun table for a session */ 4100 } /* sessions */ 4101 rw_exit(&ilport->ilport_lock); 4102 } /* ports */ 4103 4104 dai_scan_done: 4105 mutex_exit(&stmf_state.stmf_lock); 4106 4107 for (i = 0; i < nu; i++) { 4108 stmf_do_itl_dereg(lu, itl_list[i], 4109 STMF_ITL_REASON_DEREG_REQUEST); 4110 } 4111 kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *)); 4112 4113 return (STMF_SUCCESS); 4114 } 4115 4116 stmf_status_t 4117 stmf_deregister_itl_handle(stmf_lu_t *lu, uint8_t *lun, 4118 stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle) 4119 { 4120 stmf_i_scsi_session_t *iss; 4121 stmf_itl_data_t *itl; 4122 stmf_lun_map_ent_t *ent; 4123 stmf_lun_map_t *lm; 4124 int i; 4125 uint16_t n; 4126 4127 if (ss == NULL) { 4128 if (session_id == STMF_SESSION_ID_NONE) 4129 return (STMF_INVALID_ARG); 4130 iss = stmf_session_id_to_issptr(session_id, 1); 4131 if (iss == NULL) 4132 return (STMF_NOT_FOUND); 4133 } else { 4134 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private; 4135 rw_enter(iss->iss_lockp, RW_WRITER); 4136 } 4137 lm = iss->iss_sm; 4138 if (lm == NULL) { 4139 rw_exit(iss->iss_lockp); 4140 return (STMF_NOT_FOUND); 4141 } 4142 4143 if (lun) { 4144 n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8)); 4145 ent = (stmf_lun_map_ent_t *) 4146 stmf_get_ent_from_map(iss->iss_sm, n); 4147 } else { 4148 if (itl_handle == NULL) { 4149 rw_exit(iss->iss_lockp); 4150 return (STMF_INVALID_ARG); 4151 } 4152 ent = NULL; 4153 for (i = 0; i < lm->lm_nentries; i++) { 4154 if (lm->lm_plus[i] == NULL) 4155 continue; 4156 ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 4157 if (ent->ent_itl_datap && 4158 (ent->ent_itl_datap->itl_handle == itl_handle)) { 4159 break; 4160 } 4161 } 4162 } 4163 if ((ent == NULL) || (ent->ent_lu != lu) || 4164 (ent->ent_itl_datap == NULL)) { 4165 rw_exit(iss->iss_lockp); 4166 return (STMF_NOT_FOUND); 4167 } 4168 itl = ent->ent_itl_datap; 4169 ent->ent_itl_datap = NULL; 4170 rw_exit(iss->iss_lockp); 4171 stmf_do_itl_dereg(lu, itl, STMF_ITL_REASON_DEREG_REQUEST); 4172 4173 return (STMF_SUCCESS); 4174 } 4175 4176 stmf_status_t 4177 stmf_get_itl_handle(stmf_lu_t *lu, uint8_t *lun, stmf_scsi_session_t *ss, 4178 uint64_t session_id, void **itl_handle_retp) 4179 { 4180 stmf_i_scsi_session_t *iss; 4181 stmf_lun_map_ent_t *ent; 4182 stmf_lun_map_t *lm; 4183 stmf_status_t ret; 4184 int i; 4185 uint16_t n; 4186 4187 if (ss == NULL) { 4188 iss = stmf_session_id_to_issptr(session_id, 1); 4189 if (iss == NULL) 4190 return (STMF_NOT_FOUND); 4191 } else { 4192 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private; 4193 rw_enter(iss->iss_lockp, RW_WRITER); 4194 } 4195 4196 ent = NULL; 4197 if (lun == NULL) { 4198 lm = iss->iss_sm; 4199 for (i = 0; i < lm->lm_nentries; i++) { 4200 if (lm->lm_plus[i] == NULL) 4201 continue; 4202 ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 4203 if (ent->ent_lu == lu) 4204 break; 4205 } 4206 } else { 4207 n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8)); 4208 ent = (stmf_lun_map_ent_t *) 4209 stmf_get_ent_from_map(iss->iss_sm, n); 4210 if (lu && (ent->ent_lu != lu)) 4211 ent = NULL; 4212 } 4213 if (ent && ent->ent_itl_datap) { 4214 *itl_handle_retp = ent->ent_itl_datap->itl_handle; 4215 ret = STMF_SUCCESS; 4216 } else { 4217 ret = STMF_NOT_FOUND; 4218 } 4219 4220 rw_exit(iss->iss_lockp); 4221 return (ret); 4222 } 4223 4224 stmf_data_buf_t * 4225 stmf_alloc_dbuf(scsi_task_t *task, uint32_t size, uint32_t *pminsize, 4226 uint32_t flags) 4227 { 4228 stmf_i_scsi_task_t *itask = 4229 (stmf_i_scsi_task_t *)task->task_stmf_private; 4230 stmf_local_port_t *lport = task->task_lport; 4231 stmf_data_buf_t *dbuf; 4232 uint8_t ndx; 4233 4234 ndx = stmf_first_zero[itask->itask_allocated_buf_map]; 4235 if (ndx == 0xff) 4236 return (NULL); 4237 dbuf = itask->itask_dbufs[ndx] = lport->lport_ds->ds_alloc_data_buf( 4238 task, size, pminsize, flags); 4239 if (dbuf) { 4240 task->task_cur_nbufs++; 4241 itask->itask_allocated_buf_map |= (1 << ndx); 4242 dbuf->db_handle = ndx; 4243 return (dbuf); 4244 } 4245 4246 return (NULL); 4247 } 4248 4249 stmf_status_t 4250 stmf_setup_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t flags) 4251 { 4252 stmf_i_scsi_task_t *itask = 4253 (stmf_i_scsi_task_t *)task->task_stmf_private; 4254 stmf_local_port_t *lport = task->task_lport; 4255 uint8_t ndx; 4256 stmf_status_t ret; 4257 4258 ASSERT(task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF); 4259 ASSERT(lport->lport_ds->ds_setup_dbuf != NULL); 4260 ASSERT(dbuf->db_flags & DB_LU_DATA_BUF); 4261 4262 if ((task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF) == 0) 4263 return (STMF_FAILURE); 4264 if (lport->lport_ds->ds_setup_dbuf == NULL) 4265 return (STMF_FAILURE); 4266 4267 ndx = stmf_first_zero[itask->itask_allocated_buf_map]; 4268 if (ndx == 0xff) 4269 return (STMF_FAILURE); 4270 ret = lport->lport_ds->ds_setup_dbuf(task, dbuf, flags); 4271 if (ret == STMF_FAILURE) 4272 return (STMF_FAILURE); 4273 itask->itask_dbufs[ndx] = dbuf; 4274 task->task_cur_nbufs++; 4275 itask->itask_allocated_buf_map |= (1 << ndx); 4276 dbuf->db_handle = ndx; 4277 4278 return (STMF_SUCCESS); 4279 } 4280 4281 void 4282 stmf_teardown_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf) 4283 { 4284 stmf_i_scsi_task_t *itask = 4285 (stmf_i_scsi_task_t *)task->task_stmf_private; 4286 stmf_local_port_t *lport = task->task_lport; 4287 4288 ASSERT(task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF); 4289 ASSERT(lport->lport_ds->ds_teardown_dbuf != NULL); 4290 ASSERT(dbuf->db_flags & DB_LU_DATA_BUF); 4291 4292 itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle); 4293 task->task_cur_nbufs--; 4294 lport->lport_ds->ds_teardown_dbuf(lport->lport_ds, dbuf); 4295 } 4296 4297 void 4298 stmf_free_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf) 4299 { 4300 stmf_i_scsi_task_t *itask = 4301 (stmf_i_scsi_task_t *)task->task_stmf_private; 4302 stmf_local_port_t *lport = task->task_lport; 4303 4304 itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle); 4305 task->task_cur_nbufs--; 4306 lport->lport_ds->ds_free_data_buf(lport->lport_ds, dbuf); 4307 } 4308 4309 stmf_data_buf_t * 4310 stmf_handle_to_buf(scsi_task_t *task, uint8_t h) 4311 { 4312 stmf_i_scsi_task_t *itask; 4313 4314 itask = (stmf_i_scsi_task_t *)task->task_stmf_private; 4315 if (h > 3) 4316 return (NULL); 4317 return (itask->itask_dbufs[h]); 4318 } 4319 4320 /* ARGSUSED */ 4321 struct scsi_task * 4322 stmf_task_alloc(struct stmf_local_port *lport, stmf_scsi_session_t *ss, 4323 uint8_t *lun, uint16_t cdb_length_in, uint16_t ext_id) 4324 { 4325 stmf_lu_t *lu; 4326 stmf_i_scsi_session_t *iss; 4327 stmf_i_lu_t *ilu; 4328 stmf_i_scsi_task_t *itask; 4329 stmf_i_scsi_task_t **ppitask; 4330 scsi_task_t *task; 4331 uint8_t *l; 4332 stmf_lun_map_ent_t *lun_map_ent; 4333 uint16_t cdb_length; 4334 uint16_t luNbr; 4335 uint8_t new_task = 0; 4336 4337 /* 4338 * We allocate 7 extra bytes for CDB to provide a cdb pointer which 4339 * is guaranteed to be 8 byte aligned. Some LU providers like OSD 4340 * depend upon this alignment. 4341 */ 4342 if (cdb_length_in >= 16) 4343 cdb_length = cdb_length_in + 7; 4344 else 4345 cdb_length = 16 + 7; 4346 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private; 4347 luNbr = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8)); 4348 rw_enter(iss->iss_lockp, RW_READER); 4349 lun_map_ent = 4350 (stmf_lun_map_ent_t *)stmf_get_ent_from_map(iss->iss_sm, luNbr); 4351 if (!lun_map_ent) { 4352 lu = dlun0; 4353 } else { 4354 lu = lun_map_ent->ent_lu; 4355 } 4356 ilu = lu->lu_stmf_private; 4357 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 4358 rw_exit(iss->iss_lockp); 4359 return (NULL); 4360 } 4361 do { 4362 if (ilu->ilu_free_tasks == NULL) { 4363 new_task = 1; 4364 break; 4365 } 4366 mutex_enter(&ilu->ilu_task_lock); 4367 for (ppitask = &ilu->ilu_free_tasks; (*ppitask != NULL) && 4368 ((*ppitask)->itask_cdb_buf_size < cdb_length); 4369 ppitask = &((*ppitask)->itask_lu_free_next)) 4370 ; 4371 if (*ppitask) { 4372 itask = *ppitask; 4373 *ppitask = (*ppitask)->itask_lu_free_next; 4374 ilu->ilu_ntasks_free--; 4375 if (ilu->ilu_ntasks_free < ilu->ilu_ntasks_min_free) 4376 ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free; 4377 } else { 4378 new_task = 1; 4379 } 4380 mutex_exit(&ilu->ilu_task_lock); 4381 /* CONSTCOND */ 4382 } while (0); 4383 4384 if (!new_task) { 4385 /* 4386 * Save the task_cdb pointer and zero per cmd fields. 4387 * We know the task_cdb_length is large enough by task 4388 * selection process above. 4389 */ 4390 uint8_t *save_cdb; 4391 uintptr_t t_start, t_end; 4392 4393 task = itask->itask_task; 4394 save_cdb = task->task_cdb; /* save */ 4395 t_start = (uintptr_t)&task->task_flags; 4396 t_end = (uintptr_t)&task->task_extended_cmd; 4397 bzero((void *)t_start, (size_t)(t_end - t_start)); 4398 task->task_cdb = save_cdb; /* restore */ 4399 itask->itask_ncmds = 0; 4400 } else { 4401 task = (scsi_task_t *)stmf_alloc(STMF_STRUCT_SCSI_TASK, 4402 cdb_length, AF_FORCE_NOSLEEP); 4403 if (task == NULL) { 4404 rw_exit(iss->iss_lockp); 4405 return (NULL); 4406 } 4407 task->task_lu = lu; 4408 l = task->task_lun_no; 4409 l[0] = lun[0]; 4410 l[1] = lun[1]; 4411 l[2] = lun[2]; 4412 l[3] = lun[3]; 4413 l[4] = lun[4]; 4414 l[5] = lun[5]; 4415 l[6] = lun[6]; 4416 l[7] = lun[7]; 4417 task->task_cdb = (uint8_t *)task->task_port_private; 4418 if ((ulong_t)(task->task_cdb) & 7ul) { 4419 task->task_cdb = (uint8_t *)(((ulong_t) 4420 (task->task_cdb) + 7ul) & ~(7ul)); 4421 } 4422 itask = (stmf_i_scsi_task_t *)task->task_stmf_private; 4423 itask->itask_cdb_buf_size = cdb_length; 4424 } 4425 task->task_session = ss; 4426 task->task_lport = lport; 4427 task->task_cdb_length = cdb_length_in; 4428 itask->itask_flags = ITASK_IN_TRANSITION; 4429 itask->itask_waitq_time = 0; 4430 itask->itask_lu_read_time = itask->itask_lu_write_time = 0; 4431 itask->itask_lport_read_time = itask->itask_lport_write_time = 0; 4432 itask->itask_read_xfer = itask->itask_write_xfer = 0; 4433 4434 if (new_task) { 4435 if (lu->lu_task_alloc(task) != STMF_SUCCESS) { 4436 rw_exit(iss->iss_lockp); 4437 stmf_free(task); 4438 return (NULL); 4439 } 4440 mutex_enter(&ilu->ilu_task_lock); 4441 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 4442 mutex_exit(&ilu->ilu_task_lock); 4443 rw_exit(iss->iss_lockp); 4444 stmf_free(task); 4445 return (NULL); 4446 } 4447 itask->itask_lu_next = ilu->ilu_tasks; 4448 if (ilu->ilu_tasks) 4449 ilu->ilu_tasks->itask_lu_prev = itask; 4450 ilu->ilu_tasks = itask; 4451 /* kmem_zalloc automatically makes itask->itask_lu_prev NULL */ 4452 ilu->ilu_ntasks++; 4453 mutex_exit(&ilu->ilu_task_lock); 4454 } 4455 4456 itask->itask_ilu_task_cntr = ilu->ilu_cur_task_cntr; 4457 atomic_add_32(itask->itask_ilu_task_cntr, 1); 4458 itask->itask_start_time = ddi_get_lbolt(); 4459 4460 if ((lun_map_ent != NULL) && ((itask->itask_itl_datap = 4461 lun_map_ent->ent_itl_datap) != NULL)) { 4462 atomic_add_32(&itask->itask_itl_datap->itl_counter, 1); 4463 task->task_lu_itl_handle = itask->itask_itl_datap->itl_handle; 4464 } else { 4465 itask->itask_itl_datap = NULL; 4466 task->task_lu_itl_handle = NULL; 4467 } 4468 4469 rw_exit(iss->iss_lockp); 4470 return (task); 4471 } 4472 4473 static void 4474 stmf_task_lu_free(scsi_task_t *task, stmf_i_scsi_session_t *iss) 4475 { 4476 stmf_i_scsi_task_t *itask = 4477 (stmf_i_scsi_task_t *)task->task_stmf_private; 4478 stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 4479 4480 ASSERT(rw_lock_held(iss->iss_lockp)); 4481 itask->itask_flags = ITASK_IN_FREE_LIST; 4482 itask->itask_proxy_msg_id = 0; 4483 mutex_enter(&ilu->ilu_task_lock); 4484 itask->itask_lu_free_next = ilu->ilu_free_tasks; 4485 ilu->ilu_free_tasks = itask; 4486 ilu->ilu_ntasks_free++; 4487 mutex_exit(&ilu->ilu_task_lock); 4488 atomic_add_32(itask->itask_ilu_task_cntr, -1); 4489 } 4490 4491 void 4492 stmf_task_lu_check_freelist(stmf_i_lu_t *ilu) 4493 { 4494 uint32_t num_to_release, ndx; 4495 stmf_i_scsi_task_t *itask; 4496 stmf_lu_t *lu = ilu->ilu_lu; 4497 4498 ASSERT(ilu->ilu_ntasks_min_free <= ilu->ilu_ntasks_free); 4499 4500 /* free half of the minimal free of the free tasks */ 4501 num_to_release = (ilu->ilu_ntasks_min_free + 1) / 2; 4502 if (!num_to_release) { 4503 return; 4504 } 4505 for (ndx = 0; ndx < num_to_release; ndx++) { 4506 mutex_enter(&ilu->ilu_task_lock); 4507 itask = ilu->ilu_free_tasks; 4508 if (itask == NULL) { 4509 mutex_exit(&ilu->ilu_task_lock); 4510 break; 4511 } 4512 ilu->ilu_free_tasks = itask->itask_lu_free_next; 4513 ilu->ilu_ntasks_free--; 4514 mutex_exit(&ilu->ilu_task_lock); 4515 4516 lu->lu_task_free(itask->itask_task); 4517 mutex_enter(&ilu->ilu_task_lock); 4518 if (itask->itask_lu_next) 4519 itask->itask_lu_next->itask_lu_prev = 4520 itask->itask_lu_prev; 4521 if (itask->itask_lu_prev) 4522 itask->itask_lu_prev->itask_lu_next = 4523 itask->itask_lu_next; 4524 else 4525 ilu->ilu_tasks = itask->itask_lu_next; 4526 4527 ilu->ilu_ntasks--; 4528 mutex_exit(&ilu->ilu_task_lock); 4529 stmf_free(itask->itask_task); 4530 } 4531 } 4532 4533 /* 4534 * Called with stmf_lock held 4535 */ 4536 void 4537 stmf_check_freetask() 4538 { 4539 stmf_i_lu_t *ilu; 4540 clock_t endtime = ddi_get_lbolt() + drv_usectohz(10000); 4541 4542 /* stmf_svc_ilu_draining may get changed after stmf_lock is released */ 4543 while ((ilu = stmf_state.stmf_svc_ilu_draining) != NULL) { 4544 stmf_state.stmf_svc_ilu_draining = ilu->ilu_next; 4545 if (!ilu->ilu_ntasks_min_free) { 4546 ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free; 4547 continue; 4548 } 4549 ilu->ilu_flags |= ILU_STALL_DEREGISTER; 4550 mutex_exit(&stmf_state.stmf_lock); 4551 stmf_task_lu_check_freelist(ilu); 4552 /* 4553 * we do not care about the accuracy of 4554 * ilu_ntasks_min_free, so we don't lock here 4555 */ 4556 ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free; 4557 mutex_enter(&stmf_state.stmf_lock); 4558 ilu->ilu_flags &= ~ILU_STALL_DEREGISTER; 4559 cv_broadcast(&stmf_state.stmf_cv); 4560 if (ddi_get_lbolt() >= endtime) 4561 break; 4562 } 4563 } 4564 4565 void 4566 stmf_do_ilu_timeouts(stmf_i_lu_t *ilu) 4567 { 4568 clock_t l = ddi_get_lbolt(); 4569 clock_t ps = drv_usectohz(1000000); 4570 stmf_i_scsi_task_t *itask; 4571 scsi_task_t *task; 4572 uint32_t to; 4573 4574 mutex_enter(&ilu->ilu_task_lock); 4575 for (itask = ilu->ilu_tasks; itask != NULL; 4576 itask = itask->itask_lu_next) { 4577 if (itask->itask_flags & (ITASK_IN_FREE_LIST | 4578 ITASK_BEING_ABORTED)) { 4579 continue; 4580 } 4581 task = itask->itask_task; 4582 if (task->task_timeout == 0) 4583 to = stmf_default_task_timeout; 4584 else 4585 to = task->task_timeout; 4586 if ((itask->itask_start_time + (to * ps)) > l) 4587 continue; 4588 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 4589 STMF_TIMEOUT, NULL); 4590 } 4591 mutex_exit(&ilu->ilu_task_lock); 4592 } 4593 4594 /* 4595 * Called with stmf_lock held 4596 */ 4597 void 4598 stmf_check_ilu_timing() 4599 { 4600 stmf_i_lu_t *ilu; 4601 clock_t endtime = ddi_get_lbolt() + drv_usectohz(10000); 4602 4603 /* stmf_svc_ilu_timing may get changed after stmf_lock is released */ 4604 while ((ilu = stmf_state.stmf_svc_ilu_timing) != NULL) { 4605 stmf_state.stmf_svc_ilu_timing = ilu->ilu_next; 4606 if (ilu->ilu_cur_task_cntr == (&ilu->ilu_task_cntr1)) { 4607 if (ilu->ilu_task_cntr2 == 0) { 4608 ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr2; 4609 continue; 4610 } 4611 } else { 4612 if (ilu->ilu_task_cntr1 == 0) { 4613 ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1; 4614 continue; 4615 } 4616 } 4617 /* 4618 * If we are here then it means that there is some slowdown 4619 * in tasks on this lu. We need to check. 4620 */ 4621 ilu->ilu_flags |= ILU_STALL_DEREGISTER; 4622 mutex_exit(&stmf_state.stmf_lock); 4623 stmf_do_ilu_timeouts(ilu); 4624 mutex_enter(&stmf_state.stmf_lock); 4625 ilu->ilu_flags &= ~ILU_STALL_DEREGISTER; 4626 cv_broadcast(&stmf_state.stmf_cv); 4627 if (ddi_get_lbolt() >= endtime) 4628 break; 4629 } 4630 } 4631 4632 /* 4633 * Kills all tasks on a lu except tm_task 4634 */ 4635 void 4636 stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s) 4637 { 4638 stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 4639 stmf_i_scsi_task_t *itask; 4640 4641 mutex_enter(&ilu->ilu_task_lock); 4642 4643 for (itask = ilu->ilu_tasks; itask != NULL; 4644 itask = itask->itask_lu_next) { 4645 if (itask->itask_flags & ITASK_IN_FREE_LIST) 4646 continue; 4647 if (itask->itask_task == tm_task) 4648 continue; 4649 stmf_abort(STMF_QUEUE_TASK_ABORT, itask->itask_task, s, NULL); 4650 } 4651 mutex_exit(&ilu->ilu_task_lock); 4652 } 4653 4654 void 4655 stmf_free_task_bufs(stmf_i_scsi_task_t *itask, stmf_local_port_t *lport) 4656 { 4657 int i; 4658 uint8_t map; 4659 4660 if ((map = itask->itask_allocated_buf_map) == 0) 4661 return; 4662 for (i = 0; i < 4; i++) { 4663 if (map & 1) { 4664 stmf_data_buf_t *dbuf; 4665 4666 dbuf = itask->itask_dbufs[i]; 4667 if (dbuf->db_xfer_start_timestamp) { 4668 stmf_lport_xfer_done(itask, dbuf); 4669 } 4670 if (dbuf->db_flags & DB_LU_DATA_BUF) { 4671 /* 4672 * LU needs to clean up buffer. 4673 * LU is required to free the buffer 4674 * in the xfer_done handler. 4675 */ 4676 scsi_task_t *task = itask->itask_task; 4677 stmf_lu_t *lu = task->task_lu; 4678 4679 lu->lu_dbuf_free(task, dbuf); 4680 ASSERT(((itask->itask_allocated_buf_map>>i) 4681 & 1) == 0); /* must be gone */ 4682 } else { 4683 ASSERT(dbuf->db_lu_private == NULL); 4684 dbuf->db_lu_private = NULL; 4685 lport->lport_ds->ds_free_data_buf( 4686 lport->lport_ds, dbuf); 4687 } 4688 } 4689 map >>= 1; 4690 } 4691 itask->itask_allocated_buf_map = 0; 4692 } 4693 4694 void 4695 stmf_task_free(scsi_task_t *task) 4696 { 4697 stmf_local_port_t *lport = task->task_lport; 4698 stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *) 4699 task->task_stmf_private; 4700 stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *) 4701 task->task_session->ss_stmf_private; 4702 4703 stmf_free_task_bufs(itask, lport); 4704 stmf_itl_task_done(itask); 4705 DTRACE_PROBE2(stmf__task__end, scsi_task_t *, task, 4706 hrtime_t, 4707 itask->itask_done_timestamp - itask->itask_start_timestamp); 4708 if (itask->itask_itl_datap) { 4709 if (atomic_add_32_nv(&itask->itask_itl_datap->itl_counter, 4710 -1) == 0) { 4711 stmf_release_itl_handle(task->task_lu, 4712 itask->itask_itl_datap); 4713 } 4714 } 4715 4716 rw_enter(iss->iss_lockp, RW_READER); 4717 lport->lport_task_free(task); 4718 if (itask->itask_worker) { 4719 atomic_add_32(&stmf_cur_ntasks, -1); 4720 atomic_add_32(&itask->itask_worker->worker_ref_count, -1); 4721 } 4722 /* 4723 * After calling stmf_task_lu_free, the task pointer can no longer 4724 * be trusted. 4725 */ 4726 stmf_task_lu_free(task, iss); 4727 rw_exit(iss->iss_lockp); 4728 } 4729 4730 void 4731 stmf_post_task(scsi_task_t *task, stmf_data_buf_t *dbuf) 4732 { 4733 stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *) 4734 task->task_stmf_private; 4735 stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 4736 int nv; 4737 uint32_t old, new; 4738 uint32_t ct; 4739 stmf_worker_t *w, *w1; 4740 uint8_t tm; 4741 4742 if (task->task_max_nbufs > 4) 4743 task->task_max_nbufs = 4; 4744 task->task_cur_nbufs = 0; 4745 /* Latest value of currently running tasks */ 4746 ct = atomic_add_32_nv(&stmf_cur_ntasks, 1); 4747 4748 /* Select the next worker using round robin */ 4749 nv = (int)atomic_add_32_nv((uint32_t *)&stmf_worker_sel_counter, 1); 4750 if (nv >= stmf_nworkers_accepting_cmds) { 4751 int s = nv; 4752 do { 4753 nv -= stmf_nworkers_accepting_cmds; 4754 } while (nv >= stmf_nworkers_accepting_cmds); 4755 if (nv < 0) 4756 nv = 0; 4757 /* Its ok if this cas fails */ 4758 (void) atomic_cas_32((uint32_t *)&stmf_worker_sel_counter, 4759 s, nv); 4760 } 4761 w = &stmf_workers[nv]; 4762 4763 /* 4764 * A worker can be pinned by interrupt. So select the next one 4765 * if it has lower load. 4766 */ 4767 if ((nv + 1) >= stmf_nworkers_accepting_cmds) { 4768 w1 = stmf_workers; 4769 } else { 4770 w1 = &stmf_workers[nv + 1]; 4771 } 4772 if (w1->worker_queue_depth < w->worker_queue_depth) 4773 w = w1; 4774 4775 mutex_enter(&w->worker_lock); 4776 if (((w->worker_flags & STMF_WORKER_STARTED) == 0) || 4777 (w->worker_flags & STMF_WORKER_TERMINATE)) { 4778 /* 4779 * Maybe we are in the middle of a change. Just go to 4780 * the 1st worker. 4781 */ 4782 mutex_exit(&w->worker_lock); 4783 w = stmf_workers; 4784 mutex_enter(&w->worker_lock); 4785 } 4786 itask->itask_worker = w; 4787 /* 4788 * Track max system load inside the worker as we already have the 4789 * worker lock (no point implementing another lock). The service 4790 * thread will do the comparisons and figure out the max overall 4791 * system load. 4792 */ 4793 if (w->worker_max_sys_qdepth_pu < ct) 4794 w->worker_max_sys_qdepth_pu = ct; 4795 4796 do { 4797 old = new = itask->itask_flags; 4798 new |= ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE; 4799 if (task->task_mgmt_function) { 4800 tm = task->task_mgmt_function; 4801 if ((tm == TM_TARGET_RESET) || 4802 (tm == TM_TARGET_COLD_RESET) || 4803 (tm == TM_TARGET_WARM_RESET)) { 4804 new |= ITASK_DEFAULT_HANDLING; 4805 } 4806 } else if (task->task_cdb[0] == SCMD_REPORT_LUNS) { 4807 new |= ITASK_DEFAULT_HANDLING; 4808 } 4809 new &= ~ITASK_IN_TRANSITION; 4810 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 4811 4812 stmf_itl_task_start(itask); 4813 4814 itask->itask_worker_next = NULL; 4815 if (w->worker_task_tail) { 4816 w->worker_task_tail->itask_worker_next = itask; 4817 } else { 4818 w->worker_task_head = itask; 4819 } 4820 w->worker_task_tail = itask; 4821 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) { 4822 w->worker_max_qdepth_pu = w->worker_queue_depth; 4823 } 4824 /* Measure task waitq time */ 4825 itask->itask_waitq_enter_timestamp = gethrtime(); 4826 atomic_add_32(&w->worker_ref_count, 1); 4827 itask->itask_cmd_stack[0] = ITASK_CMD_NEW_TASK; 4828 itask->itask_ncmds = 1; 4829 if (dbuf) { 4830 itask->itask_allocated_buf_map = 1; 4831 itask->itask_dbufs[0] = dbuf; 4832 dbuf->db_handle = 0; 4833 } else { 4834 itask->itask_allocated_buf_map = 0; 4835 itask->itask_dbufs[0] = NULL; 4836 } 4837 4838 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) { 4839 w->worker_signal_timestamp = gethrtime(); 4840 DTRACE_PROBE2(worker__signal, stmf_worker_t *, w, 4841 scsi_task_t *, task); 4842 cv_signal(&w->worker_cv); 4843 } 4844 mutex_exit(&w->worker_lock); 4845 4846 /* 4847 * This can only happen if during stmf_task_alloc(), ILU_RESET_ACTIVE 4848 * was set between checking of ILU_RESET_ACTIVE and clearing of the 4849 * ITASK_IN_FREE_LIST flag. Take care of these "sneaked-in" tasks here. 4850 */ 4851 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 4852 stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ABORTED, NULL); 4853 } 4854 } 4855 4856 /* 4857 * ++++++++++++++ ABORT LOGIC ++++++++++++++++++++ 4858 * Once ITASK_BEING_ABORTED is set, ITASK_KNOWN_TO_LU can be reset already 4859 * i.e. before ITASK_BEING_ABORTED being set. But if it was not, it cannot 4860 * be reset until the LU explicitly calls stmf_task_lu_aborted(). Of course 4861 * the LU will make this call only if we call the LU's abort entry point. 4862 * we will only call that entry point if ITASK_KNOWN_TO_LU was set. 4863 * 4864 * Same logic applies for the port. 4865 * 4866 * Also ITASK_BEING_ABORTED will not be allowed to set if both KNOWN_TO_LU 4867 * and KNOWN_TO_TGT_PORT are reset. 4868 * 4869 * +++++++++++++++++++++++++++++++++++++++++++++++ 4870 */ 4871 4872 stmf_status_t 4873 stmf_xfer_data(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t ioflags) 4874 { 4875 stmf_status_t ret; 4876 4877 stmf_i_scsi_task_t *itask = 4878 (stmf_i_scsi_task_t *)task->task_stmf_private; 4879 4880 if (ioflags & STMF_IOF_LU_DONE) { 4881 uint32_t new, old; 4882 do { 4883 new = old = itask->itask_flags; 4884 if (new & ITASK_BEING_ABORTED) 4885 return (STMF_ABORTED); 4886 new &= ~ITASK_KNOWN_TO_LU; 4887 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 4888 } 4889 if (itask->itask_flags & ITASK_BEING_ABORTED) 4890 return (STMF_ABORTED); 4891 #ifdef DEBUG 4892 if (!(ioflags & STMF_IOF_STATS_ONLY) && stmf_drop_buf_counter > 0) { 4893 if (atomic_add_32_nv((uint32_t *)&stmf_drop_buf_counter, -1) == 4894 1) 4895 return (STMF_SUCCESS); 4896 } 4897 #endif 4898 4899 stmf_update_kstat_lu_io(task, dbuf); 4900 stmf_update_kstat_lport_io(task, dbuf); 4901 stmf_lport_xfer_start(itask, dbuf); 4902 if (ioflags & STMF_IOF_STATS_ONLY) { 4903 stmf_lport_xfer_done(itask, dbuf); 4904 return (STMF_SUCCESS); 4905 } 4906 4907 ret = task->task_lport->lport_xfer_data(task, dbuf, ioflags); 4908 4909 /* 4910 * Port provider may have already called the buffer callback in 4911 * which case dbuf->db_xfer_start_timestamp will be 0. 4912 */ 4913 if ((ret != STMF_SUCCESS) && (dbuf->db_xfer_start_timestamp != 0)) { 4914 stmf_lport_xfer_done(itask, dbuf); 4915 } 4916 4917 return (ret); 4918 } 4919 4920 void 4921 stmf_data_xfer_done(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t iof) 4922 { 4923 stmf_i_scsi_task_t *itask = 4924 (stmf_i_scsi_task_t *)task->task_stmf_private; 4925 stmf_worker_t *w = itask->itask_worker; 4926 uint32_t new, old; 4927 uint8_t update_queue_flags, free_it, queue_it; 4928 4929 stmf_lport_xfer_done(itask, dbuf); 4930 4931 mutex_enter(&w->worker_lock); 4932 do { 4933 new = old = itask->itask_flags; 4934 if (old & ITASK_BEING_ABORTED) { 4935 mutex_exit(&w->worker_lock); 4936 return; 4937 } 4938 free_it = 0; 4939 if (iof & STMF_IOF_LPORT_DONE) { 4940 new &= ~ITASK_KNOWN_TO_TGT_PORT; 4941 task->task_completion_status = dbuf->db_xfer_status; 4942 free_it = 1; 4943 } 4944 /* 4945 * If the task is known to LU then queue it. But if 4946 * it is already queued (multiple completions) then 4947 * just update the buffer information by grabbing the 4948 * worker lock. If the task is not known to LU, 4949 * completed/aborted, then see if we need to 4950 * free this task. 4951 */ 4952 if (old & ITASK_KNOWN_TO_LU) { 4953 free_it = 0; 4954 update_queue_flags = 1; 4955 if (old & ITASK_IN_WORKER_QUEUE) { 4956 queue_it = 0; 4957 } else { 4958 queue_it = 1; 4959 new |= ITASK_IN_WORKER_QUEUE; 4960 } 4961 } else { 4962 update_queue_flags = 0; 4963 queue_it = 0; 4964 } 4965 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 4966 4967 if (update_queue_flags) { 4968 uint8_t cmd = (dbuf->db_handle << 5) | ITASK_CMD_DATA_XFER_DONE; 4969 4970 ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS); 4971 itask->itask_cmd_stack[itask->itask_ncmds++] = cmd; 4972 if (queue_it) { 4973 itask->itask_worker_next = NULL; 4974 if (w->worker_task_tail) { 4975 w->worker_task_tail->itask_worker_next = itask; 4976 } else { 4977 w->worker_task_head = itask; 4978 } 4979 w->worker_task_tail = itask; 4980 /* Measure task waitq time */ 4981 itask->itask_waitq_enter_timestamp = gethrtime(); 4982 if (++(w->worker_queue_depth) > 4983 w->worker_max_qdepth_pu) { 4984 w->worker_max_qdepth_pu = w->worker_queue_depth; 4985 } 4986 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 4987 cv_signal(&w->worker_cv); 4988 } 4989 } 4990 mutex_exit(&w->worker_lock); 4991 4992 if (free_it) { 4993 if ((itask->itask_flags & (ITASK_KNOWN_TO_LU | 4994 ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE | 4995 ITASK_BEING_ABORTED)) == 0) { 4996 stmf_task_free(task); 4997 } 4998 } 4999 } 5000 5001 stmf_status_t 5002 stmf_send_scsi_status(scsi_task_t *task, uint32_t ioflags) 5003 { 5004 DTRACE_PROBE1(scsi__send__status, scsi_task_t *, task); 5005 5006 stmf_i_scsi_task_t *itask = 5007 (stmf_i_scsi_task_t *)task->task_stmf_private; 5008 if (ioflags & STMF_IOF_LU_DONE) { 5009 uint32_t new, old; 5010 do { 5011 new = old = itask->itask_flags; 5012 if (new & ITASK_BEING_ABORTED) 5013 return (STMF_ABORTED); 5014 new &= ~ITASK_KNOWN_TO_LU; 5015 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 5016 } 5017 5018 if (!(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT)) { 5019 return (STMF_SUCCESS); 5020 } 5021 5022 if (itask->itask_flags & ITASK_BEING_ABORTED) 5023 return (STMF_ABORTED); 5024 5025 if (task->task_additional_flags & TASK_AF_NO_EXPECTED_XFER_LENGTH) { 5026 task->task_status_ctrl = 0; 5027 task->task_resid = 0; 5028 } else if (task->task_cmd_xfer_length > 5029 task->task_expected_xfer_length) { 5030 task->task_status_ctrl = TASK_SCTRL_OVER; 5031 task->task_resid = task->task_cmd_xfer_length - 5032 task->task_expected_xfer_length; 5033 } else if (task->task_nbytes_transferred < 5034 task->task_expected_xfer_length) { 5035 task->task_status_ctrl = TASK_SCTRL_UNDER; 5036 task->task_resid = task->task_expected_xfer_length - 5037 task->task_nbytes_transferred; 5038 } else { 5039 task->task_status_ctrl = 0; 5040 task->task_resid = 0; 5041 } 5042 return (task->task_lport->lport_send_status(task, ioflags)); 5043 } 5044 5045 void 5046 stmf_send_status_done(scsi_task_t *task, stmf_status_t s, uint32_t iof) 5047 { 5048 stmf_i_scsi_task_t *itask = 5049 (stmf_i_scsi_task_t *)task->task_stmf_private; 5050 stmf_worker_t *w = itask->itask_worker; 5051 uint32_t new, old; 5052 uint8_t free_it, queue_it; 5053 5054 mutex_enter(&w->worker_lock); 5055 do { 5056 new = old = itask->itask_flags; 5057 if (old & ITASK_BEING_ABORTED) { 5058 mutex_exit(&w->worker_lock); 5059 return; 5060 } 5061 free_it = 0; 5062 if (iof & STMF_IOF_LPORT_DONE) { 5063 new &= ~ITASK_KNOWN_TO_TGT_PORT; 5064 free_it = 1; 5065 } 5066 /* 5067 * If the task is known to LU then queue it. But if 5068 * it is already queued (multiple completions) then 5069 * just update the buffer information by grabbing the 5070 * worker lock. If the task is not known to LU, 5071 * completed/aborted, then see if we need to 5072 * free this task. 5073 */ 5074 if (old & ITASK_KNOWN_TO_LU) { 5075 free_it = 0; 5076 queue_it = 1; 5077 if (old & ITASK_IN_WORKER_QUEUE) { 5078 cmn_err(CE_PANIC, "status completion received" 5079 " when task is already in worker queue " 5080 " task = %p", (void *)task); 5081 } 5082 new |= ITASK_IN_WORKER_QUEUE; 5083 } else { 5084 queue_it = 0; 5085 } 5086 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 5087 task->task_completion_status = s; 5088 5089 5090 if (queue_it) { 5091 ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS); 5092 itask->itask_cmd_stack[itask->itask_ncmds++] = 5093 ITASK_CMD_STATUS_DONE; 5094 itask->itask_worker_next = NULL; 5095 if (w->worker_task_tail) { 5096 w->worker_task_tail->itask_worker_next = itask; 5097 } else { 5098 w->worker_task_head = itask; 5099 } 5100 w->worker_task_tail = itask; 5101 /* Measure task waitq time */ 5102 itask->itask_waitq_enter_timestamp = gethrtime(); 5103 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) { 5104 w->worker_max_qdepth_pu = w->worker_queue_depth; 5105 } 5106 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 5107 cv_signal(&w->worker_cv); 5108 } 5109 mutex_exit(&w->worker_lock); 5110 5111 if (free_it) { 5112 if ((itask->itask_flags & (ITASK_KNOWN_TO_LU | 5113 ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE | 5114 ITASK_BEING_ABORTED)) == 0) { 5115 stmf_task_free(task); 5116 } else { 5117 cmn_err(CE_PANIC, "LU is done with the task but LPORT " 5118 " is not done, itask %p itask_flags %x", 5119 (void *)itask, itask->itask_flags); 5120 } 5121 } 5122 } 5123 5124 void 5125 stmf_task_lu_done(scsi_task_t *task) 5126 { 5127 stmf_i_scsi_task_t *itask = 5128 (stmf_i_scsi_task_t *)task->task_stmf_private; 5129 stmf_worker_t *w = itask->itask_worker; 5130 uint32_t new, old; 5131 5132 mutex_enter(&w->worker_lock); 5133 do { 5134 new = old = itask->itask_flags; 5135 if (old & ITASK_BEING_ABORTED) { 5136 mutex_exit(&w->worker_lock); 5137 return; 5138 } 5139 if (old & ITASK_IN_WORKER_QUEUE) { 5140 cmn_err(CE_PANIC, "task_lu_done received" 5141 " when task is in worker queue " 5142 " task = %p", (void *)task); 5143 } 5144 new &= ~ITASK_KNOWN_TO_LU; 5145 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 5146 5147 mutex_exit(&w->worker_lock); 5148 5149 if ((itask->itask_flags & (ITASK_KNOWN_TO_LU | 5150 ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE | 5151 ITASK_BEING_ABORTED)) == 0) { 5152 stmf_task_free(task); 5153 } else { 5154 cmn_err(CE_PANIC, "stmf_lu_done should be the last stage but " 5155 " the task is still not done, task = %p", (void *)task); 5156 } 5157 } 5158 5159 void 5160 stmf_queue_task_for_abort(scsi_task_t *task, stmf_status_t s) 5161 { 5162 stmf_i_scsi_task_t *itask = 5163 (stmf_i_scsi_task_t *)task->task_stmf_private; 5164 stmf_worker_t *w; 5165 uint32_t old, new; 5166 5167 do { 5168 old = new = itask->itask_flags; 5169 if ((old & ITASK_BEING_ABORTED) || 5170 ((old & (ITASK_KNOWN_TO_TGT_PORT | 5171 ITASK_KNOWN_TO_LU)) == 0)) { 5172 return; 5173 } 5174 new |= ITASK_BEING_ABORTED; 5175 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 5176 task->task_completion_status = s; 5177 itask->itask_start_time = ddi_get_lbolt(); 5178 5179 if (((w = itask->itask_worker) == NULL) || 5180 (itask->itask_flags & ITASK_IN_TRANSITION)) { 5181 return; 5182 } 5183 5184 /* Queue it and get out */ 5185 mutex_enter(&w->worker_lock); 5186 if (itask->itask_flags & ITASK_IN_WORKER_QUEUE) { 5187 mutex_exit(&w->worker_lock); 5188 return; 5189 } 5190 atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE); 5191 itask->itask_worker_next = NULL; 5192 if (w->worker_task_tail) { 5193 w->worker_task_tail->itask_worker_next = itask; 5194 } else { 5195 w->worker_task_head = itask; 5196 } 5197 w->worker_task_tail = itask; 5198 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) { 5199 w->worker_max_qdepth_pu = w->worker_queue_depth; 5200 } 5201 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 5202 cv_signal(&w->worker_cv); 5203 mutex_exit(&w->worker_lock); 5204 } 5205 5206 void 5207 stmf_abort(int abort_cmd, scsi_task_t *task, stmf_status_t s, void *arg) 5208 { 5209 stmf_i_scsi_task_t *itask = NULL; 5210 uint32_t old, new, f, rf; 5211 5212 DTRACE_PROBE2(scsi__task__abort, scsi_task_t *, task, 5213 stmf_status_t, s); 5214 5215 switch (abort_cmd) { 5216 case STMF_QUEUE_ABORT_LU: 5217 stmf_task_lu_killall((stmf_lu_t *)arg, task, s); 5218 return; 5219 case STMF_QUEUE_TASK_ABORT: 5220 stmf_queue_task_for_abort(task, s); 5221 return; 5222 case STMF_REQUEUE_TASK_ABORT_LPORT: 5223 rf = ITASK_TGT_PORT_ABORT_CALLED; 5224 f = ITASK_KNOWN_TO_TGT_PORT; 5225 break; 5226 case STMF_REQUEUE_TASK_ABORT_LU: 5227 rf = ITASK_LU_ABORT_CALLED; 5228 f = ITASK_KNOWN_TO_LU; 5229 break; 5230 default: 5231 return; 5232 } 5233 itask = (stmf_i_scsi_task_t *)task->task_stmf_private; 5234 f |= ITASK_BEING_ABORTED | rf; 5235 do { 5236 old = new = itask->itask_flags; 5237 if ((old & f) != f) { 5238 return; 5239 } 5240 new &= ~rf; 5241 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 5242 } 5243 5244 void 5245 stmf_task_lu_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof) 5246 { 5247 char info[STMF_CHANGE_INFO_LEN]; 5248 stmf_i_scsi_task_t *itask = TASK_TO_ITASK(task); 5249 unsigned long long st; 5250 5251 st = s; /* gcc fix */ 5252 if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) { 5253 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 5254 "task %p, lu failed to abort ret=%llx", (void *)task, st); 5255 } else if ((iof & STMF_IOF_LU_DONE) == 0) { 5256 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 5257 "Task aborted but LU is not finished, task =" 5258 "%p, s=%llx, iof=%x", (void *)task, st, iof); 5259 } else { 5260 /* 5261 * LU abort successfully 5262 */ 5263 atomic_and_32(&itask->itask_flags, ~ITASK_KNOWN_TO_LU); 5264 return; 5265 } 5266 5267 info[STMF_CHANGE_INFO_LEN - 1] = 0; 5268 stmf_abort_task_offline(task, 1, info); 5269 } 5270 5271 void 5272 stmf_task_lport_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof) 5273 { 5274 char info[STMF_CHANGE_INFO_LEN]; 5275 stmf_i_scsi_task_t *itask = TASK_TO_ITASK(task); 5276 unsigned long long st; 5277 uint32_t old, new; 5278 5279 st = s; 5280 if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) { 5281 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 5282 "task %p, tgt port failed to abort ret=%llx", (void *)task, 5283 st); 5284 } else if ((iof & STMF_IOF_LPORT_DONE) == 0) { 5285 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 5286 "Task aborted but tgt port is not finished, " 5287 "task=%p, s=%llx, iof=%x", (void *)task, st, iof); 5288 } else { 5289 /* 5290 * LPORT abort successfully 5291 */ 5292 do { 5293 old = new = itask->itask_flags; 5294 if (!(old & ITASK_KNOWN_TO_TGT_PORT)) 5295 return; 5296 new &= ~ITASK_KNOWN_TO_TGT_PORT; 5297 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 5298 return; 5299 } 5300 5301 info[STMF_CHANGE_INFO_LEN - 1] = 0; 5302 stmf_abort_task_offline(task, 0, info); 5303 } 5304 5305 stmf_status_t 5306 stmf_task_poll_lu(scsi_task_t *task, uint32_t timeout) 5307 { 5308 stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *) 5309 task->task_stmf_private; 5310 stmf_worker_t *w = itask->itask_worker; 5311 int i; 5312 5313 ASSERT(itask->itask_flags & ITASK_KNOWN_TO_LU); 5314 mutex_enter(&w->worker_lock); 5315 if (itask->itask_ncmds >= ITASK_MAX_NCMDS) { 5316 mutex_exit(&w->worker_lock); 5317 return (STMF_BUSY); 5318 } 5319 for (i = 0; i < itask->itask_ncmds; i++) { 5320 if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LU) { 5321 mutex_exit(&w->worker_lock); 5322 return (STMF_SUCCESS); 5323 } 5324 } 5325 itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LU; 5326 if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) { 5327 itask->itask_poll_timeout = ddi_get_lbolt() + 1; 5328 } else { 5329 clock_t t = drv_usectohz(timeout * 1000); 5330 if (t == 0) 5331 t = 1; 5332 itask->itask_poll_timeout = ddi_get_lbolt() + t; 5333 } 5334 if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) { 5335 itask->itask_worker_next = NULL; 5336 if (w->worker_task_tail) { 5337 w->worker_task_tail->itask_worker_next = itask; 5338 } else { 5339 w->worker_task_head = itask; 5340 } 5341 w->worker_task_tail = itask; 5342 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) { 5343 w->worker_max_qdepth_pu = w->worker_queue_depth; 5344 } 5345 atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE); 5346 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 5347 cv_signal(&w->worker_cv); 5348 } 5349 mutex_exit(&w->worker_lock); 5350 return (STMF_SUCCESS); 5351 } 5352 5353 stmf_status_t 5354 stmf_task_poll_lport(scsi_task_t *task, uint32_t timeout) 5355 { 5356 stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *) 5357 task->task_stmf_private; 5358 stmf_worker_t *w = itask->itask_worker; 5359 int i; 5360 5361 ASSERT(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT); 5362 mutex_enter(&w->worker_lock); 5363 if (itask->itask_ncmds >= ITASK_MAX_NCMDS) { 5364 mutex_exit(&w->worker_lock); 5365 return (STMF_BUSY); 5366 } 5367 for (i = 0; i < itask->itask_ncmds; i++) { 5368 if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LPORT) { 5369 mutex_exit(&w->worker_lock); 5370 return (STMF_SUCCESS); 5371 } 5372 } 5373 itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LPORT; 5374 if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) { 5375 itask->itask_poll_timeout = ddi_get_lbolt() + 1; 5376 } else { 5377 clock_t t = drv_usectohz(timeout * 1000); 5378 if (t == 0) 5379 t = 1; 5380 itask->itask_poll_timeout = ddi_get_lbolt() + t; 5381 } 5382 if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) { 5383 itask->itask_worker_next = NULL; 5384 if (w->worker_task_tail) { 5385 w->worker_task_tail->itask_worker_next = itask; 5386 } else { 5387 w->worker_task_head = itask; 5388 } 5389 w->worker_task_tail = itask; 5390 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) { 5391 w->worker_max_qdepth_pu = w->worker_queue_depth; 5392 } 5393 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 5394 cv_signal(&w->worker_cv); 5395 } 5396 mutex_exit(&w->worker_lock); 5397 return (STMF_SUCCESS); 5398 } 5399 5400 void 5401 stmf_do_task_abort(scsi_task_t *task) 5402 { 5403 stmf_i_scsi_task_t *itask = TASK_TO_ITASK(task); 5404 stmf_lu_t *lu; 5405 stmf_local_port_t *lport; 5406 unsigned long long ret; 5407 uint32_t old, new; 5408 uint8_t call_lu_abort, call_port_abort; 5409 char info[STMF_CHANGE_INFO_LEN]; 5410 5411 lu = task->task_lu; 5412 lport = task->task_lport; 5413 do { 5414 old = new = itask->itask_flags; 5415 if ((old & (ITASK_KNOWN_TO_LU | ITASK_LU_ABORT_CALLED)) == 5416 ITASK_KNOWN_TO_LU) { 5417 new |= ITASK_LU_ABORT_CALLED; 5418 call_lu_abort = 1; 5419 } else { 5420 call_lu_abort = 0; 5421 } 5422 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 5423 5424 if (call_lu_abort) { 5425 if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0) { 5426 ret = lu->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0); 5427 } else { 5428 ret = dlun0->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0); 5429 } 5430 if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) { 5431 stmf_task_lu_aborted(task, ret, STMF_IOF_LU_DONE); 5432 } else if (ret == STMF_BUSY) { 5433 atomic_and_32(&itask->itask_flags, 5434 ~ITASK_LU_ABORT_CALLED); 5435 } else if (ret != STMF_SUCCESS) { 5436 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 5437 "Abort failed by LU %p, ret %llx", (void *)lu, ret); 5438 info[STMF_CHANGE_INFO_LEN - 1] = 0; 5439 stmf_abort_task_offline(task, 1, info); 5440 } 5441 } else if (itask->itask_flags & ITASK_KNOWN_TO_LU) { 5442 if (ddi_get_lbolt() > (itask->itask_start_time + 5443 STMF_SEC2TICK(lu->lu_abort_timeout? 5444 lu->lu_abort_timeout : ITASK_DEFAULT_ABORT_TIMEOUT))) { 5445 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 5446 "lu abort timed out"); 5447 info[STMF_CHANGE_INFO_LEN - 1] = 0; 5448 stmf_abort_task_offline(itask->itask_task, 1, info); 5449 } 5450 } 5451 5452 do { 5453 old = new = itask->itask_flags; 5454 if ((old & (ITASK_KNOWN_TO_TGT_PORT | 5455 ITASK_TGT_PORT_ABORT_CALLED)) == ITASK_KNOWN_TO_TGT_PORT) { 5456 new |= ITASK_TGT_PORT_ABORT_CALLED; 5457 call_port_abort = 1; 5458 } else { 5459 call_port_abort = 0; 5460 } 5461 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 5462 if (call_port_abort) { 5463 ret = lport->lport_abort(lport, STMF_LPORT_ABORT_TASK, task, 0); 5464 if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) { 5465 stmf_task_lport_aborted(task, ret, STMF_IOF_LPORT_DONE); 5466 } else if (ret == STMF_BUSY) { 5467 atomic_and_32(&itask->itask_flags, 5468 ~ITASK_TGT_PORT_ABORT_CALLED); 5469 } else if (ret != STMF_SUCCESS) { 5470 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 5471 "Abort failed by tgt port %p ret %llx", 5472 (void *)lport, ret); 5473 info[STMF_CHANGE_INFO_LEN - 1] = 0; 5474 stmf_abort_task_offline(task, 0, info); 5475 } 5476 } else if (itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT) { 5477 if (ddi_get_lbolt() > (itask->itask_start_time + 5478 STMF_SEC2TICK(lport->lport_abort_timeout? 5479 lport->lport_abort_timeout : 5480 ITASK_DEFAULT_ABORT_TIMEOUT))) { 5481 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 5482 "lport abort timed out"); 5483 info[STMF_CHANGE_INFO_LEN - 1] = 0; 5484 stmf_abort_task_offline(itask->itask_task, 0, info); 5485 } 5486 } 5487 } 5488 5489 stmf_status_t 5490 stmf_ctl(int cmd, void *obj, void *arg) 5491 { 5492 stmf_status_t ret; 5493 stmf_i_lu_t *ilu; 5494 stmf_i_local_port_t *ilport; 5495 stmf_state_change_info_t *ssci = (stmf_state_change_info_t *)arg; 5496 5497 mutex_enter(&stmf_state.stmf_lock); 5498 ret = STMF_INVALID_ARG; 5499 if (cmd & STMF_CMD_LU_OP) { 5500 ilu = stmf_lookup_lu((stmf_lu_t *)obj); 5501 if (ilu == NULL) { 5502 goto stmf_ctl_lock_exit; 5503 } 5504 DTRACE_PROBE3(lu__state__change, 5505 stmf_lu_t *, ilu->ilu_lu, 5506 int, cmd, stmf_state_change_info_t *, ssci); 5507 } else if (cmd & STMF_CMD_LPORT_OP) { 5508 ilport = stmf_lookup_lport((stmf_local_port_t *)obj); 5509 if (ilport == NULL) { 5510 goto stmf_ctl_lock_exit; 5511 } 5512 DTRACE_PROBE3(lport__state__change, 5513 stmf_local_port_t *, ilport->ilport_lport, 5514 int, cmd, stmf_state_change_info_t *, ssci); 5515 } else { 5516 goto stmf_ctl_lock_exit; 5517 } 5518 5519 switch (cmd) { 5520 case STMF_CMD_LU_ONLINE: 5521 switch (ilu->ilu_state) { 5522 case STMF_STATE_OFFLINE: 5523 ret = STMF_SUCCESS; 5524 break; 5525 case STMF_STATE_ONLINE: 5526 case STMF_STATE_ONLINING: 5527 ret = STMF_ALREADY; 5528 break; 5529 case STMF_STATE_OFFLINING: 5530 ret = STMF_BUSY; 5531 break; 5532 default: 5533 ret = STMF_BADSTATE; 5534 break; 5535 } 5536 if (ret != STMF_SUCCESS) 5537 goto stmf_ctl_lock_exit; 5538 5539 ilu->ilu_state = STMF_STATE_ONLINING; 5540 mutex_exit(&stmf_state.stmf_lock); 5541 stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg); 5542 break; 5543 5544 case STMF_CMD_LU_ONLINE_COMPLETE: 5545 if (ilu->ilu_state != STMF_STATE_ONLINING) { 5546 ret = STMF_BADSTATE; 5547 goto stmf_ctl_lock_exit; 5548 } 5549 if (((stmf_change_status_t *)arg)->st_completion_status == 5550 STMF_SUCCESS) { 5551 ilu->ilu_state = STMF_STATE_ONLINE; 5552 mutex_exit(&stmf_state.stmf_lock); 5553 ((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj, 5554 STMF_ACK_LU_ONLINE_COMPLETE, arg); 5555 mutex_enter(&stmf_state.stmf_lock); 5556 stmf_add_lu_to_active_sessions((stmf_lu_t *)obj); 5557 } else { 5558 /* XXX: should throw a meesage an record more data */ 5559 ilu->ilu_state = STMF_STATE_OFFLINE; 5560 } 5561 ret = STMF_SUCCESS; 5562 goto stmf_ctl_lock_exit; 5563 5564 case STMF_CMD_LU_OFFLINE: 5565 switch (ilu->ilu_state) { 5566 case STMF_STATE_ONLINE: 5567 ret = STMF_SUCCESS; 5568 break; 5569 case STMF_STATE_OFFLINE: 5570 case STMF_STATE_OFFLINING: 5571 ret = STMF_ALREADY; 5572 break; 5573 case STMF_STATE_ONLINING: 5574 ret = STMF_BUSY; 5575 break; 5576 default: 5577 ret = STMF_BADSTATE; 5578 break; 5579 } 5580 if (ret != STMF_SUCCESS) 5581 goto stmf_ctl_lock_exit; 5582 ilu->ilu_state = STMF_STATE_OFFLINING; 5583 mutex_exit(&stmf_state.stmf_lock); 5584 stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg); 5585 break; 5586 5587 case STMF_CMD_LU_OFFLINE_COMPLETE: 5588 if (ilu->ilu_state != STMF_STATE_OFFLINING) { 5589 ret = STMF_BADSTATE; 5590 goto stmf_ctl_lock_exit; 5591 } 5592 if (((stmf_change_status_t *)arg)->st_completion_status == 5593 STMF_SUCCESS) { 5594 ilu->ilu_state = STMF_STATE_OFFLINE; 5595 mutex_exit(&stmf_state.stmf_lock); 5596 ((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj, 5597 STMF_ACK_LU_OFFLINE_COMPLETE, arg); 5598 mutex_enter(&stmf_state.stmf_lock); 5599 } else { 5600 ilu->ilu_state = STMF_STATE_ONLINE; 5601 stmf_add_lu_to_active_sessions((stmf_lu_t *)obj); 5602 } 5603 mutex_exit(&stmf_state.stmf_lock); 5604 break; 5605 5606 /* 5607 * LPORT_ONLINE/OFFLINE has nothing to do with link offline/online. 5608 * It's related with hardware disable/enable. 5609 */ 5610 case STMF_CMD_LPORT_ONLINE: 5611 switch (ilport->ilport_state) { 5612 case STMF_STATE_OFFLINE: 5613 ret = STMF_SUCCESS; 5614 break; 5615 case STMF_STATE_ONLINE: 5616 case STMF_STATE_ONLINING: 5617 ret = STMF_ALREADY; 5618 break; 5619 case STMF_STATE_OFFLINING: 5620 ret = STMF_BUSY; 5621 break; 5622 default: 5623 ret = STMF_BADSTATE; 5624 break; 5625 } 5626 if (ret != STMF_SUCCESS) 5627 goto stmf_ctl_lock_exit; 5628 5629 /* 5630 * Only user request can recover the port from the 5631 * FORCED_OFFLINE state 5632 */ 5633 if (ilport->ilport_flags & ILPORT_FORCED_OFFLINE) { 5634 if (!(ssci->st_rflags & STMF_RFLAG_USER_REQUEST)) { 5635 ret = STMF_FAILURE; 5636 goto stmf_ctl_lock_exit; 5637 } 5638 } 5639 5640 /* 5641 * Avoid too frequent request to online 5642 */ 5643 if (ssci->st_rflags & STMF_RFLAG_USER_REQUEST) { 5644 ilport->ilport_online_times = 0; 5645 ilport->ilport_avg_interval = 0; 5646 } 5647 if ((ilport->ilport_avg_interval < STMF_AVG_ONLINE_INTERVAL) && 5648 (ilport->ilport_online_times >= 4)) { 5649 ret = STMF_FAILURE; 5650 ilport->ilport_flags |= ILPORT_FORCED_OFFLINE; 5651 stmf_trace(NULL, "stmf_ctl: too frequent request to " 5652 "online the port"); 5653 cmn_err(CE_WARN, "stmf_ctl: too frequent request to " 5654 "online the port, set FORCED_OFFLINE now"); 5655 goto stmf_ctl_lock_exit; 5656 } 5657 if (ilport->ilport_online_times > 0) { 5658 if (ilport->ilport_online_times == 1) { 5659 ilport->ilport_avg_interval = ddi_get_lbolt() - 5660 ilport->ilport_last_online_clock; 5661 } else { 5662 ilport->ilport_avg_interval = 5663 (ilport->ilport_avg_interval + 5664 ddi_get_lbolt() - 5665 ilport->ilport_last_online_clock) >> 1; 5666 } 5667 } 5668 ilport->ilport_last_online_clock = ddi_get_lbolt(); 5669 ilport->ilport_online_times++; 5670 5671 /* 5672 * Submit online service request 5673 */ 5674 ilport->ilport_flags &= ~ILPORT_FORCED_OFFLINE; 5675 ilport->ilport_state = STMF_STATE_ONLINING; 5676 mutex_exit(&stmf_state.stmf_lock); 5677 stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg); 5678 break; 5679 5680 case STMF_CMD_LPORT_ONLINE_COMPLETE: 5681 if (ilport->ilport_state != STMF_STATE_ONLINING) { 5682 ret = STMF_BADSTATE; 5683 goto stmf_ctl_lock_exit; 5684 } 5685 if (((stmf_change_status_t *)arg)->st_completion_status == 5686 STMF_SUCCESS) { 5687 ilport->ilport_state = STMF_STATE_ONLINE; 5688 mutex_exit(&stmf_state.stmf_lock); 5689 ((stmf_local_port_t *)obj)->lport_ctl( 5690 (stmf_local_port_t *)obj, 5691 STMF_ACK_LPORT_ONLINE_COMPLETE, arg); 5692 mutex_enter(&stmf_state.stmf_lock); 5693 } else { 5694 ilport->ilport_state = STMF_STATE_OFFLINE; 5695 } 5696 ret = STMF_SUCCESS; 5697 goto stmf_ctl_lock_exit; 5698 5699 case STMF_CMD_LPORT_OFFLINE: 5700 switch (ilport->ilport_state) { 5701 case STMF_STATE_ONLINE: 5702 ret = STMF_SUCCESS; 5703 break; 5704 case STMF_STATE_OFFLINE: 5705 case STMF_STATE_OFFLINING: 5706 ret = STMF_ALREADY; 5707 break; 5708 case STMF_STATE_ONLINING: 5709 ret = STMF_BUSY; 5710 break; 5711 default: 5712 ret = STMF_BADSTATE; 5713 break; 5714 } 5715 if (ret != STMF_SUCCESS) 5716 goto stmf_ctl_lock_exit; 5717 5718 ilport->ilport_state = STMF_STATE_OFFLINING; 5719 mutex_exit(&stmf_state.stmf_lock); 5720 stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg); 5721 break; 5722 5723 case STMF_CMD_LPORT_OFFLINE_COMPLETE: 5724 if (ilport->ilport_state != STMF_STATE_OFFLINING) { 5725 ret = STMF_BADSTATE; 5726 goto stmf_ctl_lock_exit; 5727 } 5728 if (((stmf_change_status_t *)arg)->st_completion_status == 5729 STMF_SUCCESS) { 5730 ilport->ilport_state = STMF_STATE_OFFLINE; 5731 mutex_exit(&stmf_state.stmf_lock); 5732 ((stmf_local_port_t *)obj)->lport_ctl( 5733 (stmf_local_port_t *)obj, 5734 STMF_ACK_LPORT_OFFLINE_COMPLETE, arg); 5735 mutex_enter(&stmf_state.stmf_lock); 5736 } else { 5737 ilport->ilport_state = STMF_STATE_ONLINE; 5738 } 5739 mutex_exit(&stmf_state.stmf_lock); 5740 break; 5741 5742 default: 5743 cmn_err(CE_WARN, "Invalid ctl cmd received %x", cmd); 5744 ret = STMF_INVALID_ARG; 5745 goto stmf_ctl_lock_exit; 5746 } 5747 5748 return (STMF_SUCCESS); 5749 5750 stmf_ctl_lock_exit:; 5751 mutex_exit(&stmf_state.stmf_lock); 5752 return (ret); 5753 } 5754 5755 /* ARGSUSED */ 5756 stmf_status_t 5757 stmf_info_impl(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf, 5758 uint32_t *bufsizep) 5759 { 5760 return (STMF_NOT_SUPPORTED); 5761 } 5762 5763 /* ARGSUSED */ 5764 stmf_status_t 5765 stmf_info(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf, 5766 uint32_t *bufsizep) 5767 { 5768 uint32_t cl = SI_GET_CLASS(cmd); 5769 5770 if (cl == SI_STMF) { 5771 return (stmf_info_impl(cmd, arg1, arg2, buf, bufsizep)); 5772 } 5773 if (cl == SI_LPORT) { 5774 return (((stmf_local_port_t *)arg1)->lport_info(cmd, arg1, 5775 arg2, buf, bufsizep)); 5776 } else if (cl == SI_LU) { 5777 return (((stmf_lu_t *)arg1)->lu_info(cmd, arg1, arg2, buf, 5778 bufsizep)); 5779 } 5780 5781 return (STMF_NOT_SUPPORTED); 5782 } 5783 5784 /* 5785 * Used by port providers. pwwn is 8 byte wwn, sdid is the devid used by 5786 * stmf to register local ports. The ident should have 20 bytes in buffer 5787 * space to convert the wwn to "wwn.xxxxxxxxxxxxxxxx" string. 5788 */ 5789 void 5790 stmf_wwn_to_devid_desc(scsi_devid_desc_t *sdid, uint8_t *wwn, 5791 uint8_t protocol_id) 5792 { 5793 char wwn_str[20+1]; 5794 5795 sdid->protocol_id = protocol_id; 5796 sdid->piv = 1; 5797 sdid->code_set = CODE_SET_ASCII; 5798 sdid->association = ID_IS_TARGET_PORT; 5799 sdid->ident_length = 20; 5800 /* Convert wwn value to "wwn.XXXXXXXXXXXXXXXX" format */ 5801 (void) snprintf(wwn_str, sizeof (wwn_str), 5802 "wwn.%02X%02X%02X%02X%02X%02X%02X%02X", 5803 wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]); 5804 bcopy(wwn_str, (char *)sdid->ident, 20); 5805 } 5806 5807 5808 stmf_xfer_data_t * 5809 stmf_prepare_tpgs_data(uint8_t ilu_alua) 5810 { 5811 stmf_xfer_data_t *xd; 5812 stmf_i_local_port_t *ilport; 5813 uint8_t *p; 5814 uint32_t sz, asz, nports = 0, nports_standby = 0; 5815 5816 mutex_enter(&stmf_state.stmf_lock); 5817 /* check if any ports are standby and create second group */ 5818 for (ilport = stmf_state.stmf_ilportlist; ilport; 5819 ilport = ilport->ilport_next) { 5820 if (ilport->ilport_standby == 1) { 5821 nports_standby++; 5822 } else { 5823 nports++; 5824 } 5825 } 5826 5827 /* The spec only allows for 255 ports to be reported per group */ 5828 nports = min(nports, 255); 5829 nports_standby = min(nports_standby, 255); 5830 sz = (nports * 4) + 12; 5831 if (nports_standby && ilu_alua) { 5832 sz += (nports_standby * 4) + 8; 5833 } 5834 asz = sz + sizeof (*xd) - 4; 5835 xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP); 5836 if (xd == NULL) { 5837 mutex_exit(&stmf_state.stmf_lock); 5838 return (NULL); 5839 } 5840 xd->alloc_size = asz; 5841 xd->size_left = sz; 5842 5843 p = xd->buf; 5844 5845 *((uint32_t *)p) = BE_32(sz - 4); 5846 p += 4; 5847 p[0] = 0x80; /* PREF */ 5848 p[1] = 5; /* AO_SUP, S_SUP */ 5849 if (stmf_state.stmf_alua_node == 1) { 5850 p[3] = 1; /* Group 1 */ 5851 } else { 5852 p[3] = 0; /* Group 0 */ 5853 } 5854 p[7] = nports & 0xff; 5855 p += 8; 5856 for (ilport = stmf_state.stmf_ilportlist; ilport; 5857 ilport = ilport->ilport_next) { 5858 if (ilport->ilport_standby == 1) { 5859 continue; 5860 } 5861 ((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid); 5862 p += 4; 5863 } 5864 if (nports_standby && ilu_alua) { 5865 p[0] = 0x02; /* Non PREF, Standby */ 5866 p[1] = 5; /* AO_SUP, S_SUP */ 5867 if (stmf_state.stmf_alua_node == 1) { 5868 p[3] = 0; /* Group 0 */ 5869 } else { 5870 p[3] = 1; /* Group 1 */ 5871 } 5872 p[7] = nports_standby & 0xff; 5873 p += 8; 5874 for (ilport = stmf_state.stmf_ilportlist; ilport; 5875 ilport = ilport->ilport_next) { 5876 if (ilport->ilport_standby == 0) { 5877 continue; 5878 } 5879 ((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid); 5880 p += 4; 5881 } 5882 } 5883 5884 mutex_exit(&stmf_state.stmf_lock); 5885 5886 return (xd); 5887 } 5888 5889 struct scsi_devid_desc * 5890 stmf_scsilib_get_devid_desc(uint16_t rtpid) 5891 { 5892 scsi_devid_desc_t *devid = NULL; 5893 stmf_i_local_port_t *ilport; 5894 5895 mutex_enter(&stmf_state.stmf_lock); 5896 5897 for (ilport = stmf_state.stmf_ilportlist; ilport; 5898 ilport = ilport->ilport_next) { 5899 if (ilport->ilport_rtpid == rtpid) { 5900 scsi_devid_desc_t *id = ilport->ilport_lport->lport_id; 5901 uint32_t id_sz = sizeof (scsi_devid_desc_t) - 1 + 5902 id->ident_length; 5903 devid = (scsi_devid_desc_t *)kmem_zalloc(id_sz, 5904 KM_NOSLEEP); 5905 if (devid != NULL) { 5906 bcopy(id, devid, id_sz); 5907 } 5908 break; 5909 } 5910 } 5911 5912 mutex_exit(&stmf_state.stmf_lock); 5913 return (devid); 5914 } 5915 5916 uint16_t 5917 stmf_scsilib_get_lport_rtid(struct scsi_devid_desc *devid) 5918 { 5919 stmf_i_local_port_t *ilport; 5920 scsi_devid_desc_t *id; 5921 uint16_t rtpid = 0; 5922 5923 mutex_enter(&stmf_state.stmf_lock); 5924 for (ilport = stmf_state.stmf_ilportlist; ilport; 5925 ilport = ilport->ilport_next) { 5926 id = ilport->ilport_lport->lport_id; 5927 if ((devid->ident_length == id->ident_length) && 5928 (memcmp(devid->ident, id->ident, id->ident_length) == 0)) { 5929 rtpid = ilport->ilport_rtpid; 5930 break; 5931 } 5932 } 5933 mutex_exit(&stmf_state.stmf_lock); 5934 return (rtpid); 5935 } 5936 5937 static uint16_t stmf_lu_id_gen_number = 0; 5938 5939 stmf_status_t 5940 stmf_scsilib_uniq_lu_id(uint32_t company_id, scsi_devid_desc_t *lu_id) 5941 { 5942 return (stmf_scsilib_uniq_lu_id2(company_id, 0, lu_id)); 5943 } 5944 5945 stmf_status_t 5946 stmf_scsilib_uniq_lu_id2(uint32_t company_id, uint32_t host_id, 5947 scsi_devid_desc_t *lu_id) 5948 { 5949 uint8_t *p; 5950 struct timeval32 timestamp32; 5951 uint32_t *t = (uint32_t *)×tamp32; 5952 struct ether_addr mac; 5953 uint8_t *e = (uint8_t *)&mac; 5954 int hid = (int)host_id; 5955 5956 if (company_id == COMPANY_ID_NONE) 5957 company_id = COMPANY_ID_SUN; 5958 5959 if (lu_id->ident_length != 0x10) 5960 return (STMF_INVALID_ARG); 5961 5962 p = (uint8_t *)lu_id; 5963 5964 atomic_add_16(&stmf_lu_id_gen_number, 1); 5965 5966 p[0] = 0xf1; p[1] = 3; p[2] = 0; p[3] = 0x10; 5967 p[4] = ((company_id >> 20) & 0xf) | 0x60; 5968 p[5] = (company_id >> 12) & 0xff; 5969 p[6] = (company_id >> 4) & 0xff; 5970 p[7] = (company_id << 4) & 0xf0; 5971 if (hid == 0 && !localetheraddr((struct ether_addr *)NULL, &mac)) { 5972 hid = BE_32((int)zone_get_hostid(NULL)); 5973 } 5974 if (hid != 0) { 5975 e[0] = (hid >> 24) & 0xff; 5976 e[1] = (hid >> 16) & 0xff; 5977 e[2] = (hid >> 8) & 0xff; 5978 e[3] = hid & 0xff; 5979 e[4] = e[5] = 0; 5980 } 5981 bcopy(e, p+8, 6); 5982 uniqtime32(×tamp32); 5983 *t = BE_32(*t); 5984 bcopy(t, p+14, 4); 5985 p[18] = (stmf_lu_id_gen_number >> 8) & 0xff; 5986 p[19] = stmf_lu_id_gen_number & 0xff; 5987 5988 return (STMF_SUCCESS); 5989 } 5990 5991 /* 5992 * saa is sense key, ASC, ASCQ 5993 */ 5994 void 5995 stmf_scsilib_send_status(scsi_task_t *task, uint8_t st, uint32_t saa) 5996 { 5997 uint8_t sd[18]; 5998 task->task_scsi_status = st; 5999 if (st == 2) { 6000 bzero(sd, 18); 6001 sd[0] = 0x70; 6002 sd[2] = (saa >> 16) & 0xf; 6003 sd[7] = 10; 6004 sd[12] = (saa >> 8) & 0xff; 6005 sd[13] = saa & 0xff; 6006 task->task_sense_data = sd; 6007 task->task_sense_length = 18; 6008 } else { 6009 task->task_sense_data = NULL; 6010 task->task_sense_length = 0; 6011 } 6012 (void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE); 6013 } 6014 6015 uint32_t 6016 stmf_scsilib_prepare_vpd_page83(scsi_task_t *task, uint8_t *page, 6017 uint32_t page_len, uint8_t byte0, uint32_t vpd_mask) 6018 { 6019 uint8_t *p = NULL; 6020 uint8_t small_buf[32]; 6021 uint32_t sz = 0; 6022 uint32_t n = 4; 6023 uint32_t m = 0; 6024 uint32_t last_bit = 0; 6025 6026 if (page_len < 4) 6027 return (0); 6028 if (page_len > 65535) 6029 page_len = 65535; 6030 6031 page[0] = byte0; 6032 page[1] = 0x83; 6033 6034 /* CONSTCOND */ 6035 while (1) { 6036 m += sz; 6037 if (sz && (page_len > n)) { 6038 uint32_t copysz; 6039 copysz = page_len > (n + sz) ? sz : page_len - n; 6040 bcopy(p, page + n, copysz); 6041 n += copysz; 6042 } 6043 vpd_mask &= ~last_bit; 6044 if (vpd_mask == 0) 6045 break; 6046 6047 if (vpd_mask & STMF_VPD_LU_ID) { 6048 last_bit = STMF_VPD_LU_ID; 6049 sz = task->task_lu->lu_id->ident_length + 4; 6050 p = (uint8_t *)task->task_lu->lu_id; 6051 continue; 6052 } else if (vpd_mask & STMF_VPD_TARGET_ID) { 6053 last_bit = STMF_VPD_TARGET_ID; 6054 sz = task->task_lport->lport_id->ident_length + 4; 6055 p = (uint8_t *)task->task_lport->lport_id; 6056 continue; 6057 } else if (vpd_mask & STMF_VPD_TP_GROUP) { 6058 stmf_i_local_port_t *ilport; 6059 last_bit = STMF_VPD_TP_GROUP; 6060 p = small_buf; 6061 bzero(p, 8); 6062 p[0] = 1; 6063 p[1] = 0x15; 6064 p[3] = 4; 6065 ilport = (stmf_i_local_port_t *) 6066 task->task_lport->lport_stmf_private; 6067 /* 6068 * If we're in alua mode, group 1 contains all alua 6069 * participating ports and all standby ports 6070 * > 255. Otherwise, if we're in alua mode, any local 6071 * ports (non standby/pppt) are also in group 1 if the 6072 * alua node is 1. Otherwise the group is 0. 6073 */ 6074 if ((stmf_state.stmf_alua_state && 6075 (ilport->ilport_alua || ilport->ilport_standby) && 6076 ilport->ilport_rtpid > 255) || 6077 (stmf_state.stmf_alua_node == 1 && 6078 ilport->ilport_standby != 1)) { 6079 p[7] = 1; /* Group 1 */ 6080 } 6081 sz = 8; 6082 continue; 6083 } else if (vpd_mask & STMF_VPD_RELATIVE_TP_ID) { 6084 stmf_i_local_port_t *ilport; 6085 6086 last_bit = STMF_VPD_RELATIVE_TP_ID; 6087 p = small_buf; 6088 bzero(p, 8); 6089 p[0] = 1; 6090 p[1] = 0x14; 6091 p[3] = 4; 6092 ilport = (stmf_i_local_port_t *) 6093 task->task_lport->lport_stmf_private; 6094 p[6] = (ilport->ilport_rtpid >> 8) & 0xff; 6095 p[7] = ilport->ilport_rtpid & 0xff; 6096 sz = 8; 6097 continue; 6098 } else { 6099 cmn_err(CE_WARN, "Invalid vpd_mask"); 6100 break; 6101 } 6102 } 6103 6104 page[2] = (m >> 8) & 0xff; 6105 page[3] = m & 0xff; 6106 6107 return (n); 6108 } 6109 6110 void 6111 stmf_scsilib_handle_report_tpgs(scsi_task_t *task, stmf_data_buf_t *dbuf) 6112 { 6113 stmf_i_scsi_task_t *itask = 6114 (stmf_i_scsi_task_t *)task->task_stmf_private; 6115 stmf_i_lu_t *ilu = 6116 (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 6117 stmf_xfer_data_t *xd; 6118 uint32_t sz, minsz; 6119 6120 itask->itask_flags |= ITASK_DEFAULT_HANDLING; 6121 task->task_cmd_xfer_length = 6122 ((((uint32_t)task->task_cdb[6]) << 24) | 6123 (((uint32_t)task->task_cdb[7]) << 16) | 6124 (((uint32_t)task->task_cdb[8]) << 8) | 6125 ((uint32_t)task->task_cdb[9])); 6126 6127 if (task->task_additional_flags & 6128 TASK_AF_NO_EXPECTED_XFER_LENGTH) { 6129 task->task_expected_xfer_length = 6130 task->task_cmd_xfer_length; 6131 } 6132 6133 if (task->task_cmd_xfer_length == 0) { 6134 stmf_scsilib_send_status(task, STATUS_GOOD, 0); 6135 return; 6136 } 6137 if (task->task_cmd_xfer_length < 4) { 6138 stmf_scsilib_send_status(task, STATUS_CHECK, 6139 STMF_SAA_INVALID_FIELD_IN_CDB); 6140 return; 6141 } 6142 6143 sz = min(task->task_expected_xfer_length, 6144 task->task_cmd_xfer_length); 6145 6146 xd = stmf_prepare_tpgs_data(ilu->ilu_alua); 6147 6148 if (xd == NULL) { 6149 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 6150 STMF_ALLOC_FAILURE, NULL); 6151 return; 6152 } 6153 6154 sz = min(sz, xd->size_left); 6155 xd->size_left = sz; 6156 minsz = min(512, sz); 6157 6158 if (dbuf == NULL) 6159 dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0); 6160 if (dbuf == NULL) { 6161 kmem_free(xd, xd->alloc_size); 6162 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 6163 STMF_ALLOC_FAILURE, NULL); 6164 return; 6165 } 6166 dbuf->db_lu_private = xd; 6167 stmf_xd_to_dbuf(dbuf, 1); 6168 6169 dbuf->db_flags = DB_DIRECTION_TO_RPORT; 6170 (void) stmf_xfer_data(task, dbuf, 0); 6171 6172 } 6173 6174 void 6175 stmf_scsilib_handle_task_mgmt(scsi_task_t *task) 6176 { 6177 6178 switch (task->task_mgmt_function) { 6179 /* 6180 * For now we will abort all I/Os on the LU in case of ABORT_TASK_SET 6181 * and ABORT_TASK. But unlike LUN_RESET we will not reset LU state 6182 * in these cases. This needs to be changed to abort only the required 6183 * set. 6184 */ 6185 case TM_ABORT_TASK: 6186 case TM_ABORT_TASK_SET: 6187 case TM_CLEAR_TASK_SET: 6188 case TM_LUN_RESET: 6189 stmf_handle_lun_reset(task); 6190 /* issue the reset to the proxy node as well */ 6191 if (stmf_state.stmf_alua_state == 1) { 6192 (void) stmf_proxy_scsi_cmd(task, NULL); 6193 } 6194 return; 6195 case TM_TARGET_RESET: 6196 case TM_TARGET_COLD_RESET: 6197 case TM_TARGET_WARM_RESET: 6198 stmf_handle_target_reset(task); 6199 return; 6200 default: 6201 /* We dont support this task mgmt function */ 6202 stmf_scsilib_send_status(task, STATUS_CHECK, 6203 STMF_SAA_INVALID_FIELD_IN_CMD_IU); 6204 return; 6205 } 6206 } 6207 6208 void 6209 stmf_handle_lun_reset(scsi_task_t *task) 6210 { 6211 stmf_i_scsi_task_t *itask; 6212 stmf_i_lu_t *ilu; 6213 6214 itask = (stmf_i_scsi_task_t *)task->task_stmf_private; 6215 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 6216 6217 /* 6218 * To sync with target reset, grab this lock. The LU is not going 6219 * anywhere as there is atleast one task pending (this task). 6220 */ 6221 mutex_enter(&stmf_state.stmf_lock); 6222 6223 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 6224 mutex_exit(&stmf_state.stmf_lock); 6225 stmf_scsilib_send_status(task, STATUS_CHECK, 6226 STMF_SAA_OPERATION_IN_PROGRESS); 6227 return; 6228 } 6229 atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE); 6230 mutex_exit(&stmf_state.stmf_lock); 6231 6232 /* 6233 * Mark this task as the one causing LU reset so that we know who 6234 * was responsible for setting the ILU_RESET_ACTIVE. In case this 6235 * task itself gets aborted, we will clear ILU_RESET_ACTIVE. 6236 */ 6237 itask->itask_flags |= ITASK_DEFAULT_HANDLING | ITASK_CAUSING_LU_RESET; 6238 6239 /* Initiatiate abort on all commands on this LU except this one */ 6240 stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED, task->task_lu); 6241 6242 /* Start polling on this task */ 6243 if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT) 6244 != STMF_SUCCESS) { 6245 stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE, 6246 NULL); 6247 return; 6248 } 6249 } 6250 6251 void 6252 stmf_handle_target_reset(scsi_task_t *task) 6253 { 6254 stmf_i_scsi_task_t *itask; 6255 stmf_i_lu_t *ilu; 6256 stmf_i_scsi_session_t *iss; 6257 stmf_lun_map_t *lm; 6258 stmf_lun_map_ent_t *lm_ent; 6259 int i, lf; 6260 6261 itask = (stmf_i_scsi_task_t *)task->task_stmf_private; 6262 iss = (stmf_i_scsi_session_t *)task->task_session->ss_stmf_private; 6263 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 6264 6265 /* 6266 * To sync with LUN reset, grab this lock. The session is not going 6267 * anywhere as there is atleast one task pending (this task). 6268 */ 6269 mutex_enter(&stmf_state.stmf_lock); 6270 6271 /* Grab the session lock as a writer to prevent any changes in it */ 6272 rw_enter(iss->iss_lockp, RW_WRITER); 6273 6274 if (iss->iss_flags & ISS_RESET_ACTIVE) { 6275 rw_exit(iss->iss_lockp); 6276 mutex_exit(&stmf_state.stmf_lock); 6277 stmf_scsilib_send_status(task, STATUS_CHECK, 6278 STMF_SAA_OPERATION_IN_PROGRESS); 6279 return; 6280 } 6281 atomic_or_32(&iss->iss_flags, ISS_RESET_ACTIVE); 6282 6283 /* 6284 * Now go through each LUN in this session and make sure all of them 6285 * can be reset. 6286 */ 6287 lm = iss->iss_sm; 6288 for (i = 0, lf = 0; i < lm->lm_nentries; i++) { 6289 if (lm->lm_plus[i] == NULL) 6290 continue; 6291 lf++; 6292 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 6293 ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private); 6294 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 6295 atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE); 6296 rw_exit(iss->iss_lockp); 6297 mutex_exit(&stmf_state.stmf_lock); 6298 stmf_scsilib_send_status(task, STATUS_CHECK, 6299 STMF_SAA_OPERATION_IN_PROGRESS); 6300 return; 6301 } 6302 } 6303 if (lf == 0) { 6304 /* No luns in this session */ 6305 atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE); 6306 rw_exit(iss->iss_lockp); 6307 mutex_exit(&stmf_state.stmf_lock); 6308 stmf_scsilib_send_status(task, STATUS_GOOD, 0); 6309 return; 6310 } 6311 6312 /* ok, start the damage */ 6313 itask->itask_flags |= ITASK_DEFAULT_HANDLING | 6314 ITASK_CAUSING_TARGET_RESET; 6315 for (i = 0; i < lm->lm_nentries; i++) { 6316 if (lm->lm_plus[i] == NULL) 6317 continue; 6318 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 6319 ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private); 6320 atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE); 6321 } 6322 6323 for (i = 0; i < lm->lm_nentries; i++) { 6324 if (lm->lm_plus[i] == NULL) 6325 continue; 6326 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 6327 stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED, 6328 lm_ent->ent_lu); 6329 } 6330 6331 rw_exit(iss->iss_lockp); 6332 mutex_exit(&stmf_state.stmf_lock); 6333 6334 /* Start polling on this task */ 6335 if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT) 6336 != STMF_SUCCESS) { 6337 stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE, 6338 NULL); 6339 return; 6340 } 6341 } 6342 6343 int 6344 stmf_handle_cmd_during_ic(stmf_i_scsi_task_t *itask) 6345 { 6346 scsi_task_t *task = itask->itask_task; 6347 stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *) 6348 task->task_session->ss_stmf_private; 6349 6350 rw_enter(iss->iss_lockp, RW_WRITER); 6351 if (((iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) == 0) || 6352 (task->task_cdb[0] == SCMD_INQUIRY)) { 6353 rw_exit(iss->iss_lockp); 6354 return (0); 6355 } 6356 atomic_and_32(&iss->iss_flags, 6357 ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS)); 6358 rw_exit(iss->iss_lockp); 6359 6360 if (task->task_cdb[0] == SCMD_REPORT_LUNS) { 6361 return (0); 6362 } 6363 stmf_scsilib_send_status(task, STATUS_CHECK, 6364 STMF_SAA_REPORT_LUN_DATA_HAS_CHANGED); 6365 return (1); 6366 } 6367 6368 void 6369 stmf_worker_init() 6370 { 6371 uint32_t i; 6372 6373 /* Make local copy of global tunables */ 6374 stmf_i_max_nworkers = stmf_max_nworkers; 6375 stmf_i_min_nworkers = stmf_min_nworkers; 6376 6377 ASSERT(stmf_workers == NULL); 6378 if (stmf_i_min_nworkers < 4) { 6379 stmf_i_min_nworkers = 4; 6380 } 6381 if (stmf_i_max_nworkers < stmf_i_min_nworkers) { 6382 stmf_i_max_nworkers = stmf_i_min_nworkers; 6383 } 6384 stmf_workers = (stmf_worker_t *)kmem_zalloc( 6385 sizeof (stmf_worker_t) * stmf_i_max_nworkers, KM_SLEEP); 6386 for (i = 0; i < stmf_i_max_nworkers; i++) { 6387 stmf_worker_t *w = &stmf_workers[i]; 6388 mutex_init(&w->worker_lock, NULL, MUTEX_DRIVER, NULL); 6389 cv_init(&w->worker_cv, NULL, CV_DRIVER, NULL); 6390 } 6391 stmf_worker_mgmt_delay = drv_usectohz(20 * 1000); 6392 stmf_workers_state = STMF_WORKERS_ENABLED; 6393 6394 /* Workers will be started by stmf_worker_mgmt() */ 6395 6396 /* Lets wait for atleast one worker to start */ 6397 while (stmf_nworkers_cur == 0) 6398 delay(drv_usectohz(20 * 1000)); 6399 stmf_worker_mgmt_delay = drv_usectohz(3 * 1000 * 1000); 6400 } 6401 6402 stmf_status_t 6403 stmf_worker_fini() 6404 { 6405 int i; 6406 clock_t sb; 6407 6408 if (stmf_workers_state == STMF_WORKERS_DISABLED) 6409 return (STMF_SUCCESS); 6410 ASSERT(stmf_workers); 6411 stmf_workers_state = STMF_WORKERS_DISABLED; 6412 stmf_worker_mgmt_delay = drv_usectohz(20 * 1000); 6413 cv_signal(&stmf_state.stmf_cv); 6414 6415 sb = ddi_get_lbolt() + drv_usectohz(10 * 1000 * 1000); 6416 /* Wait for all the threads to die */ 6417 while (stmf_nworkers_cur != 0) { 6418 if (ddi_get_lbolt() > sb) { 6419 stmf_workers_state = STMF_WORKERS_ENABLED; 6420 return (STMF_BUSY); 6421 } 6422 delay(drv_usectohz(100 * 1000)); 6423 } 6424 for (i = 0; i < stmf_i_max_nworkers; i++) { 6425 stmf_worker_t *w = &stmf_workers[i]; 6426 mutex_destroy(&w->worker_lock); 6427 cv_destroy(&w->worker_cv); 6428 } 6429 kmem_free(stmf_workers, sizeof (stmf_worker_t) * stmf_i_max_nworkers); 6430 stmf_workers = NULL; 6431 6432 return (STMF_SUCCESS); 6433 } 6434 6435 void 6436 stmf_worker_task(void *arg) 6437 { 6438 stmf_worker_t *w; 6439 stmf_i_scsi_session_t *iss; 6440 scsi_task_t *task; 6441 stmf_i_scsi_task_t *itask; 6442 stmf_data_buf_t *dbuf; 6443 stmf_lu_t *lu; 6444 clock_t wait_timer = 0; 6445 clock_t wait_ticks, wait_delta = 0; 6446 uint32_t old, new; 6447 uint8_t curcmd; 6448 uint8_t abort_free; 6449 uint8_t wait_queue; 6450 uint8_t dec_qdepth; 6451 6452 w = (stmf_worker_t *)arg; 6453 wait_ticks = drv_usectohz(10000); 6454 6455 DTRACE_PROBE1(worker__create, stmf_worker_t, w); 6456 mutex_enter(&w->worker_lock); 6457 w->worker_flags |= STMF_WORKER_STARTED | STMF_WORKER_ACTIVE; 6458 stmf_worker_loop:; 6459 if ((w->worker_ref_count == 0) && 6460 (w->worker_flags & STMF_WORKER_TERMINATE)) { 6461 w->worker_flags &= ~(STMF_WORKER_STARTED | 6462 STMF_WORKER_ACTIVE | STMF_WORKER_TERMINATE); 6463 w->worker_tid = NULL; 6464 mutex_exit(&w->worker_lock); 6465 DTRACE_PROBE1(worker__destroy, stmf_worker_t, w); 6466 thread_exit(); 6467 } 6468 /* CONSTCOND */ 6469 while (1) { 6470 dec_qdepth = 0; 6471 if (wait_timer && (ddi_get_lbolt() >= wait_timer)) { 6472 wait_timer = 0; 6473 wait_delta = 0; 6474 if (w->worker_wait_head) { 6475 ASSERT(w->worker_wait_tail); 6476 if (w->worker_task_head == NULL) 6477 w->worker_task_head = 6478 w->worker_wait_head; 6479 else 6480 w->worker_task_tail->itask_worker_next = 6481 w->worker_wait_head; 6482 w->worker_task_tail = w->worker_wait_tail; 6483 w->worker_wait_head = w->worker_wait_tail = 6484 NULL; 6485 } 6486 } 6487 if ((itask = w->worker_task_head) == NULL) { 6488 break; 6489 } 6490 task = itask->itask_task; 6491 DTRACE_PROBE2(worker__active, stmf_worker_t, w, 6492 scsi_task_t *, task); 6493 w->worker_task_head = itask->itask_worker_next; 6494 if (w->worker_task_head == NULL) 6495 w->worker_task_tail = NULL; 6496 6497 wait_queue = 0; 6498 abort_free = 0; 6499 if (itask->itask_ncmds > 0) { 6500 curcmd = itask->itask_cmd_stack[itask->itask_ncmds - 1]; 6501 } else { 6502 ASSERT(itask->itask_flags & ITASK_BEING_ABORTED); 6503 } 6504 do { 6505 old = itask->itask_flags; 6506 if (old & ITASK_BEING_ABORTED) { 6507 itask->itask_ncmds = 1; 6508 curcmd = itask->itask_cmd_stack[0] = 6509 ITASK_CMD_ABORT; 6510 goto out_itask_flag_loop; 6511 } else if ((curcmd & ITASK_CMD_MASK) == 6512 ITASK_CMD_NEW_TASK) { 6513 /* 6514 * set ITASK_KSTAT_IN_RUNQ, this flag 6515 * will not reset until task completed 6516 */ 6517 new = old | ITASK_KNOWN_TO_LU | 6518 ITASK_KSTAT_IN_RUNQ; 6519 } else { 6520 goto out_itask_flag_loop; 6521 } 6522 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 6523 6524 out_itask_flag_loop: 6525 6526 /* 6527 * Decide if this task needs to go to a queue and/or if 6528 * we can decrement the itask_cmd_stack. 6529 */ 6530 if (curcmd == ITASK_CMD_ABORT) { 6531 if (itask->itask_flags & (ITASK_KNOWN_TO_LU | 6532 ITASK_KNOWN_TO_TGT_PORT)) { 6533 wait_queue = 1; 6534 } else { 6535 abort_free = 1; 6536 } 6537 } else if ((curcmd & ITASK_CMD_POLL) && 6538 (itask->itask_poll_timeout > ddi_get_lbolt())) { 6539 wait_queue = 1; 6540 } 6541 6542 if (wait_queue) { 6543 itask->itask_worker_next = NULL; 6544 if (w->worker_wait_tail) { 6545 w->worker_wait_tail->itask_worker_next = itask; 6546 } else { 6547 w->worker_wait_head = itask; 6548 } 6549 w->worker_wait_tail = itask; 6550 if (wait_timer == 0) { 6551 wait_timer = ddi_get_lbolt() + wait_ticks; 6552 wait_delta = wait_ticks; 6553 } 6554 } else if ((--(itask->itask_ncmds)) != 0) { 6555 itask->itask_worker_next = NULL; 6556 if (w->worker_task_tail) { 6557 w->worker_task_tail->itask_worker_next = itask; 6558 } else { 6559 w->worker_task_head = itask; 6560 } 6561 w->worker_task_tail = itask; 6562 } else { 6563 atomic_and_32(&itask->itask_flags, 6564 ~ITASK_IN_WORKER_QUEUE); 6565 /* 6566 * This is where the queue depth should go down by 6567 * one but we delay that on purpose to account for 6568 * the call into the provider. The actual decrement 6569 * happens after the worker has done its job. 6570 */ 6571 dec_qdepth = 1; 6572 itask->itask_waitq_time += 6573 gethrtime() - itask->itask_waitq_enter_timestamp; 6574 } 6575 6576 /* We made it here means we are going to call LU */ 6577 if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0) 6578 lu = task->task_lu; 6579 else 6580 lu = dlun0; 6581 dbuf = itask->itask_dbufs[ITASK_CMD_BUF_NDX(curcmd)]; 6582 mutex_exit(&w->worker_lock); 6583 curcmd &= ITASK_CMD_MASK; 6584 switch (curcmd) { 6585 case ITASK_CMD_NEW_TASK: 6586 iss = (stmf_i_scsi_session_t *) 6587 task->task_session->ss_stmf_private; 6588 stmf_itl_lu_new_task(itask); 6589 if (iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) { 6590 if (stmf_handle_cmd_during_ic(itask)) 6591 break; 6592 } 6593 #ifdef DEBUG 6594 if (stmf_drop_task_counter > 0) { 6595 if (atomic_add_32_nv( 6596 (uint32_t *)&stmf_drop_task_counter, 6597 -1) == 1) { 6598 break; 6599 } 6600 } 6601 #endif 6602 DTRACE_PROBE1(scsi__task__start, scsi_task_t *, task); 6603 lu->lu_new_task(task, dbuf); 6604 break; 6605 case ITASK_CMD_DATA_XFER_DONE: 6606 lu->lu_dbuf_xfer_done(task, dbuf); 6607 break; 6608 case ITASK_CMD_STATUS_DONE: 6609 lu->lu_send_status_done(task); 6610 break; 6611 case ITASK_CMD_ABORT: 6612 if (abort_free) { 6613 stmf_task_free(task); 6614 } else { 6615 stmf_do_task_abort(task); 6616 } 6617 break; 6618 case ITASK_CMD_POLL_LU: 6619 if (!wait_queue) { 6620 lu->lu_task_poll(task); 6621 } 6622 break; 6623 case ITASK_CMD_POLL_LPORT: 6624 if (!wait_queue) 6625 task->task_lport->lport_task_poll(task); 6626 break; 6627 case ITASK_CMD_SEND_STATUS: 6628 /* case ITASK_CMD_XFER_DATA: */ 6629 break; 6630 } 6631 mutex_enter(&w->worker_lock); 6632 if (dec_qdepth) { 6633 w->worker_queue_depth--; 6634 } 6635 } 6636 if ((w->worker_flags & STMF_WORKER_TERMINATE) && (wait_timer == 0)) { 6637 if (w->worker_ref_count == 0) 6638 goto stmf_worker_loop; 6639 else { 6640 wait_timer = ddi_get_lbolt() + 1; 6641 wait_delta = 1; 6642 } 6643 } 6644 w->worker_flags &= ~STMF_WORKER_ACTIVE; 6645 if (wait_timer) { 6646 DTRACE_PROBE1(worker__timed__sleep, stmf_worker_t, w); 6647 (void) cv_reltimedwait(&w->worker_cv, &w->worker_lock, 6648 wait_delta, TR_CLOCK_TICK); 6649 } else { 6650 DTRACE_PROBE1(worker__sleep, stmf_worker_t, w); 6651 cv_wait(&w->worker_cv, &w->worker_lock); 6652 } 6653 DTRACE_PROBE1(worker__wakeup, stmf_worker_t, w); 6654 w->worker_flags |= STMF_WORKER_ACTIVE; 6655 goto stmf_worker_loop; 6656 } 6657 6658 void 6659 stmf_worker_mgmt() 6660 { 6661 int i; 6662 int workers_needed; 6663 uint32_t qd; 6664 clock_t tps, d = 0; 6665 uint32_t cur_max_ntasks = 0; 6666 stmf_worker_t *w; 6667 6668 /* Check if we are trying to increase the # of threads */ 6669 for (i = stmf_nworkers_cur; i < stmf_nworkers_needed; i++) { 6670 if (stmf_workers[i].worker_flags & STMF_WORKER_STARTED) { 6671 stmf_nworkers_cur++; 6672 stmf_nworkers_accepting_cmds++; 6673 } else { 6674 /* Wait for transition to complete */ 6675 return; 6676 } 6677 } 6678 /* Check if we are trying to decrease the # of workers */ 6679 for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) { 6680 if ((stmf_workers[i].worker_flags & STMF_WORKER_STARTED) == 0) { 6681 stmf_nworkers_cur--; 6682 /* 6683 * stmf_nworkers_accepting_cmds has already been 6684 * updated by the request to reduce the # of workers. 6685 */ 6686 } else { 6687 /* Wait for transition to complete */ 6688 return; 6689 } 6690 } 6691 /* Check if we are being asked to quit */ 6692 if (stmf_workers_state != STMF_WORKERS_ENABLED) { 6693 if (stmf_nworkers_cur) { 6694 workers_needed = 0; 6695 goto worker_mgmt_trigger_change; 6696 } 6697 return; 6698 } 6699 /* Check if we are starting */ 6700 if (stmf_nworkers_cur < stmf_i_min_nworkers) { 6701 workers_needed = stmf_i_min_nworkers; 6702 goto worker_mgmt_trigger_change; 6703 } 6704 6705 tps = drv_usectohz(1 * 1000 * 1000); 6706 if ((stmf_wm_last != 0) && 6707 ((d = ddi_get_lbolt() - stmf_wm_last) > tps)) { 6708 qd = 0; 6709 for (i = 0; i < stmf_nworkers_accepting_cmds; i++) { 6710 qd += stmf_workers[i].worker_max_qdepth_pu; 6711 stmf_workers[i].worker_max_qdepth_pu = 0; 6712 if (stmf_workers[i].worker_max_sys_qdepth_pu > 6713 cur_max_ntasks) { 6714 cur_max_ntasks = 6715 stmf_workers[i].worker_max_sys_qdepth_pu; 6716 } 6717 stmf_workers[i].worker_max_sys_qdepth_pu = 0; 6718 } 6719 } 6720 stmf_wm_last = ddi_get_lbolt(); 6721 if (d <= tps) { 6722 /* still ramping up */ 6723 return; 6724 } 6725 /* max qdepth cannot be more than max tasks */ 6726 if (qd > cur_max_ntasks) 6727 qd = cur_max_ntasks; 6728 6729 /* See if we have more workers */ 6730 if (qd < stmf_nworkers_accepting_cmds) { 6731 /* 6732 * Since we dont reduce the worker count right away, monitor 6733 * the highest load during the scale_down_delay. 6734 */ 6735 if (qd > stmf_worker_scale_down_qd) 6736 stmf_worker_scale_down_qd = qd; 6737 if (stmf_worker_scale_down_timer == 0) { 6738 stmf_worker_scale_down_timer = ddi_get_lbolt() + 6739 drv_usectohz(stmf_worker_scale_down_delay * 6740 1000 * 1000); 6741 return; 6742 } 6743 if (ddi_get_lbolt() < stmf_worker_scale_down_timer) { 6744 return; 6745 } 6746 /* Its time to reduce the workers */ 6747 if (stmf_worker_scale_down_qd < stmf_i_min_nworkers) 6748 stmf_worker_scale_down_qd = stmf_i_min_nworkers; 6749 if (stmf_worker_scale_down_qd > stmf_i_max_nworkers) 6750 stmf_worker_scale_down_qd = stmf_i_max_nworkers; 6751 if (stmf_worker_scale_down_qd == stmf_nworkers_cur) 6752 return; 6753 workers_needed = stmf_worker_scale_down_qd; 6754 stmf_worker_scale_down_qd = 0; 6755 goto worker_mgmt_trigger_change; 6756 } 6757 stmf_worker_scale_down_qd = 0; 6758 stmf_worker_scale_down_timer = 0; 6759 if (qd > stmf_i_max_nworkers) 6760 qd = stmf_i_max_nworkers; 6761 if (qd < stmf_i_min_nworkers) 6762 qd = stmf_i_min_nworkers; 6763 if (qd == stmf_nworkers_cur) 6764 return; 6765 workers_needed = qd; 6766 goto worker_mgmt_trigger_change; 6767 6768 /* NOTREACHED */ 6769 return; 6770 6771 worker_mgmt_trigger_change: 6772 ASSERT(workers_needed != stmf_nworkers_cur); 6773 if (workers_needed > stmf_nworkers_cur) { 6774 stmf_nworkers_needed = workers_needed; 6775 for (i = stmf_nworkers_cur; i < workers_needed; i++) { 6776 w = &stmf_workers[i]; 6777 w->worker_tid = thread_create(NULL, 0, stmf_worker_task, 6778 (void *)&stmf_workers[i], 0, &p0, TS_RUN, 6779 minclsyspri); 6780 } 6781 return; 6782 } 6783 /* At this point we know that we are decreasing the # of workers */ 6784 stmf_nworkers_accepting_cmds = workers_needed; 6785 stmf_nworkers_needed = workers_needed; 6786 /* Signal the workers that its time to quit */ 6787 for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) { 6788 w = &stmf_workers[i]; 6789 ASSERT(w && (w->worker_flags & STMF_WORKER_STARTED)); 6790 mutex_enter(&w->worker_lock); 6791 w->worker_flags |= STMF_WORKER_TERMINATE; 6792 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 6793 cv_signal(&w->worker_cv); 6794 mutex_exit(&w->worker_lock); 6795 } 6796 } 6797 6798 /* 6799 * Fills out a dbuf from stmf_xfer_data_t (contained in the db_lu_private). 6800 * If all the data has been filled out, frees the xd and makes 6801 * db_lu_private NULL. 6802 */ 6803 void 6804 stmf_xd_to_dbuf(stmf_data_buf_t *dbuf, int set_rel_off) 6805 { 6806 stmf_xfer_data_t *xd; 6807 uint8_t *p; 6808 int i; 6809 uint32_t s; 6810 6811 xd = (stmf_xfer_data_t *)dbuf->db_lu_private; 6812 dbuf->db_data_size = 0; 6813 if (set_rel_off) 6814 dbuf->db_relative_offset = xd->size_done; 6815 for (i = 0; i < dbuf->db_sglist_length; i++) { 6816 s = min(xd->size_left, dbuf->db_sglist[i].seg_length); 6817 p = &xd->buf[xd->size_done]; 6818 bcopy(p, dbuf->db_sglist[i].seg_addr, s); 6819 xd->size_left -= s; 6820 xd->size_done += s; 6821 dbuf->db_data_size += s; 6822 if (xd->size_left == 0) { 6823 kmem_free(xd, xd->alloc_size); 6824 dbuf->db_lu_private = NULL; 6825 return; 6826 } 6827 } 6828 } 6829 6830 /* ARGSUSED */ 6831 stmf_status_t 6832 stmf_dlun0_task_alloc(scsi_task_t *task) 6833 { 6834 return (STMF_SUCCESS); 6835 } 6836 6837 void 6838 stmf_dlun0_new_task(scsi_task_t *task, stmf_data_buf_t *dbuf) 6839 { 6840 uint8_t *cdbp = (uint8_t *)&task->task_cdb[0]; 6841 stmf_i_scsi_session_t *iss; 6842 uint32_t sz, minsz; 6843 uint8_t *p; 6844 stmf_xfer_data_t *xd; 6845 uint8_t inq_page_length = 31; 6846 6847 if (task->task_mgmt_function) { 6848 stmf_scsilib_handle_task_mgmt(task); 6849 return; 6850 } 6851 6852 switch (cdbp[0]) { 6853 case SCMD_INQUIRY: 6854 /* 6855 * Basic protocol checks. In addition, only reply to 6856 * standard inquiry. Otherwise, the LU provider needs 6857 * to respond. 6858 */ 6859 6860 if (cdbp[2] || (cdbp[1] & 1) || cdbp[5]) { 6861 stmf_scsilib_send_status(task, STATUS_CHECK, 6862 STMF_SAA_INVALID_FIELD_IN_CDB); 6863 return; 6864 } 6865 6866 task->task_cmd_xfer_length = 6867 (((uint32_t)cdbp[3]) << 8) | cdbp[4]; 6868 6869 if (task->task_additional_flags & 6870 TASK_AF_NO_EXPECTED_XFER_LENGTH) { 6871 task->task_expected_xfer_length = 6872 task->task_cmd_xfer_length; 6873 } 6874 6875 sz = min(task->task_expected_xfer_length, 6876 min(36, task->task_cmd_xfer_length)); 6877 minsz = 36; 6878 6879 if (sz == 0) { 6880 stmf_scsilib_send_status(task, STATUS_GOOD, 0); 6881 return; 6882 } 6883 6884 if (dbuf && (dbuf->db_sglist[0].seg_length < 36)) { 6885 /* 6886 * Ignore any preallocated dbuf if the size is less 6887 * than 36. It will be freed during the task_free. 6888 */ 6889 dbuf = NULL; 6890 } 6891 if (dbuf == NULL) 6892 dbuf = stmf_alloc_dbuf(task, minsz, &minsz, 0); 6893 if ((dbuf == NULL) || (dbuf->db_sglist[0].seg_length < sz)) { 6894 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 6895 STMF_ALLOC_FAILURE, NULL); 6896 return; 6897 } 6898 dbuf->db_lu_private = NULL; 6899 6900 p = dbuf->db_sglist[0].seg_addr; 6901 6902 /* 6903 * Standard inquiry handling only. 6904 */ 6905 6906 bzero(p, inq_page_length + 5); 6907 6908 p[0] = DPQ_SUPPORTED | DTYPE_UNKNOWN; 6909 p[2] = 5; 6910 p[3] = 0x12; 6911 p[4] = inq_page_length; 6912 p[6] = 0x80; 6913 6914 (void) strncpy((char *)p+8, "SUN ", 8); 6915 (void) strncpy((char *)p+16, "COMSTAR ", 16); 6916 (void) strncpy((char *)p+32, "1.0 ", 4); 6917 6918 dbuf->db_data_size = sz; 6919 dbuf->db_relative_offset = 0; 6920 dbuf->db_flags = DB_DIRECTION_TO_RPORT; 6921 (void) stmf_xfer_data(task, dbuf, 0); 6922 6923 return; 6924 6925 case SCMD_REPORT_LUNS: 6926 task->task_cmd_xfer_length = 6927 ((((uint32_t)task->task_cdb[6]) << 24) | 6928 (((uint32_t)task->task_cdb[7]) << 16) | 6929 (((uint32_t)task->task_cdb[8]) << 8) | 6930 ((uint32_t)task->task_cdb[9])); 6931 6932 if (task->task_additional_flags & 6933 TASK_AF_NO_EXPECTED_XFER_LENGTH) { 6934 task->task_expected_xfer_length = 6935 task->task_cmd_xfer_length; 6936 } 6937 6938 sz = min(task->task_expected_xfer_length, 6939 task->task_cmd_xfer_length); 6940 6941 if (sz < 16) { 6942 stmf_scsilib_send_status(task, STATUS_CHECK, 6943 STMF_SAA_INVALID_FIELD_IN_CDB); 6944 return; 6945 } 6946 6947 iss = (stmf_i_scsi_session_t *) 6948 task->task_session->ss_stmf_private; 6949 rw_enter(iss->iss_lockp, RW_WRITER); 6950 xd = stmf_session_prepare_report_lun_data(iss->iss_sm); 6951 rw_exit(iss->iss_lockp); 6952 6953 if (xd == NULL) { 6954 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 6955 STMF_ALLOC_FAILURE, NULL); 6956 return; 6957 } 6958 6959 sz = min(sz, xd->size_left); 6960 xd->size_left = sz; 6961 minsz = min(512, sz); 6962 6963 if (dbuf == NULL) 6964 dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0); 6965 if (dbuf == NULL) { 6966 kmem_free(xd, xd->alloc_size); 6967 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 6968 STMF_ALLOC_FAILURE, NULL); 6969 return; 6970 } 6971 dbuf->db_lu_private = xd; 6972 stmf_xd_to_dbuf(dbuf, 1); 6973 6974 atomic_and_32(&iss->iss_flags, 6975 ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS)); 6976 dbuf->db_flags = DB_DIRECTION_TO_RPORT; 6977 (void) stmf_xfer_data(task, dbuf, 0); 6978 return; 6979 } 6980 6981 stmf_scsilib_send_status(task, STATUS_CHECK, STMF_SAA_INVALID_OPCODE); 6982 } 6983 6984 void 6985 stmf_dlun0_dbuf_done(scsi_task_t *task, stmf_data_buf_t *dbuf) 6986 { 6987 stmf_i_scsi_task_t *itask = 6988 (stmf_i_scsi_task_t *)task->task_stmf_private; 6989 6990 if (dbuf->db_xfer_status != STMF_SUCCESS) { 6991 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 6992 dbuf->db_xfer_status, NULL); 6993 return; 6994 } 6995 task->task_nbytes_transferred += dbuf->db_data_size; 6996 if (dbuf->db_lu_private) { 6997 /* There is more */ 6998 stmf_xd_to_dbuf(dbuf, 1); 6999 (void) stmf_xfer_data(task, dbuf, 0); 7000 return; 7001 } 7002 7003 stmf_free_dbuf(task, dbuf); 7004 /* 7005 * If this is a proxy task, it will need to be completed from the 7006 * proxy port provider. This message lets pppt know that the xfer 7007 * is complete. When we receive the status from pppt, we will 7008 * then relay that status back to the lport. 7009 */ 7010 if (itask->itask_flags & ITASK_PROXY_TASK) { 7011 stmf_ic_msg_t *ic_xfer_done_msg = NULL; 7012 stmf_status_t ic_ret = STMF_FAILURE; 7013 uint64_t session_msg_id; 7014 mutex_enter(&stmf_state.stmf_lock); 7015 session_msg_id = stmf_proxy_msg_id++; 7016 mutex_exit(&stmf_state.stmf_lock); 7017 /* send xfer done status to pppt */ 7018 ic_xfer_done_msg = ic_scsi_data_xfer_done_msg_alloc( 7019 itask->itask_proxy_msg_id, 7020 task->task_session->ss_session_id, 7021 STMF_SUCCESS, session_msg_id); 7022 if (ic_xfer_done_msg) { 7023 ic_ret = ic_tx_msg(ic_xfer_done_msg); 7024 if (ic_ret != STMF_IC_MSG_SUCCESS) { 7025 cmn_err(CE_WARN, "unable to xmit session msg"); 7026 } 7027 } 7028 /* task will be completed from pppt */ 7029 return; 7030 } 7031 stmf_scsilib_send_status(task, STATUS_GOOD, 0); 7032 } 7033 7034 /* ARGSUSED */ 7035 void 7036 stmf_dlun0_status_done(scsi_task_t *task) 7037 { 7038 } 7039 7040 /* ARGSUSED */ 7041 void 7042 stmf_dlun0_task_free(scsi_task_t *task) 7043 { 7044 } 7045 7046 /* ARGSUSED */ 7047 stmf_status_t 7048 stmf_dlun0_abort(struct stmf_lu *lu, int abort_cmd, void *arg, uint32_t flags) 7049 { 7050 scsi_task_t *task = (scsi_task_t *)arg; 7051 stmf_i_scsi_task_t *itask = 7052 (stmf_i_scsi_task_t *)task->task_stmf_private; 7053 stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 7054 int i; 7055 uint8_t map; 7056 7057 if ((task->task_mgmt_function) && (itask->itask_flags & 7058 (ITASK_CAUSING_LU_RESET | ITASK_CAUSING_TARGET_RESET))) { 7059 switch (task->task_mgmt_function) { 7060 case TM_ABORT_TASK: 7061 case TM_ABORT_TASK_SET: 7062 case TM_CLEAR_TASK_SET: 7063 case TM_LUN_RESET: 7064 atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE); 7065 break; 7066 case TM_TARGET_RESET: 7067 case TM_TARGET_COLD_RESET: 7068 case TM_TARGET_WARM_RESET: 7069 stmf_abort_target_reset(task); 7070 break; 7071 } 7072 return (STMF_ABORT_SUCCESS); 7073 } 7074 7075 /* 7076 * OK so its not a task mgmt. Make sure we free any xd sitting 7077 * inside any dbuf. 7078 */ 7079 if ((map = itask->itask_allocated_buf_map) != 0) { 7080 for (i = 0; i < 4; i++) { 7081 if ((map & 1) && 7082 ((itask->itask_dbufs[i])->db_lu_private)) { 7083 stmf_xfer_data_t *xd; 7084 stmf_data_buf_t *dbuf; 7085 7086 dbuf = itask->itask_dbufs[i]; 7087 xd = (stmf_xfer_data_t *)dbuf->db_lu_private; 7088 dbuf->db_lu_private = NULL; 7089 kmem_free(xd, xd->alloc_size); 7090 } 7091 map >>= 1; 7092 } 7093 } 7094 return (STMF_ABORT_SUCCESS); 7095 } 7096 7097 void 7098 stmf_dlun0_task_poll(struct scsi_task *task) 7099 { 7100 /* Right now we only do this for handling task management functions */ 7101 ASSERT(task->task_mgmt_function); 7102 7103 switch (task->task_mgmt_function) { 7104 case TM_ABORT_TASK: 7105 case TM_ABORT_TASK_SET: 7106 case TM_CLEAR_TASK_SET: 7107 case TM_LUN_RESET: 7108 (void) stmf_lun_reset_poll(task->task_lu, task, 0); 7109 return; 7110 case TM_TARGET_RESET: 7111 case TM_TARGET_COLD_RESET: 7112 case TM_TARGET_WARM_RESET: 7113 stmf_target_reset_poll(task); 7114 return; 7115 } 7116 } 7117 7118 /* ARGSUSED */ 7119 void 7120 stmf_dlun0_ctl(struct stmf_lu *lu, int cmd, void *arg) 7121 { 7122 /* This function will never be called */ 7123 cmn_err(CE_WARN, "stmf_dlun0_ctl called with cmd %x", cmd); 7124 } 7125 7126 void 7127 stmf_dlun_init() 7128 { 7129 stmf_i_lu_t *ilu; 7130 7131 dlun0 = stmf_alloc(STMF_STRUCT_STMF_LU, 0, 0); 7132 dlun0->lu_task_alloc = stmf_dlun0_task_alloc; 7133 dlun0->lu_new_task = stmf_dlun0_new_task; 7134 dlun0->lu_dbuf_xfer_done = stmf_dlun0_dbuf_done; 7135 dlun0->lu_send_status_done = stmf_dlun0_status_done; 7136 dlun0->lu_task_free = stmf_dlun0_task_free; 7137 dlun0->lu_abort = stmf_dlun0_abort; 7138 dlun0->lu_task_poll = stmf_dlun0_task_poll; 7139 dlun0->lu_ctl = stmf_dlun0_ctl; 7140 7141 ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private; 7142 ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1; 7143 } 7144 7145 stmf_status_t 7146 stmf_dlun_fini() 7147 { 7148 stmf_i_lu_t *ilu; 7149 7150 ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private; 7151 7152 ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free); 7153 if (ilu->ilu_ntasks) { 7154 stmf_i_scsi_task_t *itask, *nitask; 7155 7156 nitask = ilu->ilu_tasks; 7157 do { 7158 itask = nitask; 7159 nitask = itask->itask_lu_next; 7160 dlun0->lu_task_free(itask->itask_task); 7161 stmf_free(itask->itask_task); 7162 } while (nitask != NULL); 7163 7164 } 7165 stmf_free(dlun0); 7166 return (STMF_SUCCESS); 7167 } 7168 7169 void 7170 stmf_abort_target_reset(scsi_task_t *task) 7171 { 7172 stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *) 7173 task->task_session->ss_stmf_private; 7174 stmf_lun_map_t *lm; 7175 stmf_lun_map_ent_t *lm_ent; 7176 stmf_i_lu_t *ilu; 7177 int i; 7178 7179 rw_enter(iss->iss_lockp, RW_READER); 7180 lm = iss->iss_sm; 7181 for (i = 0; i < lm->lm_nentries; i++) { 7182 if (lm->lm_plus[i] == NULL) 7183 continue; 7184 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 7185 ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private; 7186 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 7187 atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE); 7188 } 7189 } 7190 atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE); 7191 rw_exit(iss->iss_lockp); 7192 } 7193 7194 /* 7195 * The return value is only used by function managing target reset. 7196 */ 7197 stmf_status_t 7198 stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task, int target_reset) 7199 { 7200 stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 7201 int ntasks_pending; 7202 7203 ntasks_pending = ilu->ilu_ntasks - ilu->ilu_ntasks_free; 7204 /* 7205 * This function is also used during Target reset. The idea is that 7206 * once all the commands are aborted, call the LU's reset entry 7207 * point (abort entry point with a reset flag). But if this Task 7208 * mgmt is running on this LU then all the tasks cannot be aborted. 7209 * one task (this task) will still be running which is OK. 7210 */ 7211 if ((ntasks_pending == 0) || ((task->task_lu == lu) && 7212 (ntasks_pending == 1))) { 7213 stmf_status_t ret; 7214 7215 if ((task->task_mgmt_function == TM_LUN_RESET) || 7216 (task->task_mgmt_function == TM_TARGET_RESET) || 7217 (task->task_mgmt_function == TM_TARGET_WARM_RESET) || 7218 (task->task_mgmt_function == TM_TARGET_COLD_RESET)) { 7219 ret = lu->lu_abort(lu, STMF_LU_RESET_STATE, task, 0); 7220 } else { 7221 ret = STMF_SUCCESS; 7222 } 7223 if (ret == STMF_SUCCESS) { 7224 atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE); 7225 } 7226 if (target_reset) { 7227 return (ret); 7228 } 7229 if (ret == STMF_SUCCESS) { 7230 stmf_scsilib_send_status(task, STATUS_GOOD, 0); 7231 return (ret); 7232 } 7233 if (ret != STMF_BUSY) { 7234 stmf_abort(STMF_QUEUE_TASK_ABORT, task, ret, NULL); 7235 return (ret); 7236 } 7237 } 7238 7239 if (target_reset) { 7240 /* Tell target reset polling code that we are not done */ 7241 return (STMF_BUSY); 7242 } 7243 7244 if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT) 7245 != STMF_SUCCESS) { 7246 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 7247 STMF_ALLOC_FAILURE, NULL); 7248 return (STMF_SUCCESS); 7249 } 7250 7251 return (STMF_SUCCESS); 7252 } 7253 7254 void 7255 stmf_target_reset_poll(struct scsi_task *task) 7256 { 7257 stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *) 7258 task->task_session->ss_stmf_private; 7259 stmf_lun_map_t *lm; 7260 stmf_lun_map_ent_t *lm_ent; 7261 stmf_i_lu_t *ilu; 7262 stmf_status_t ret; 7263 int i; 7264 int not_done = 0; 7265 7266 ASSERT(iss->iss_flags & ISS_RESET_ACTIVE); 7267 7268 rw_enter(iss->iss_lockp, RW_READER); 7269 lm = iss->iss_sm; 7270 for (i = 0; i < lm->lm_nentries; i++) { 7271 if (lm->lm_plus[i] == NULL) 7272 continue; 7273 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 7274 ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private; 7275 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 7276 rw_exit(iss->iss_lockp); 7277 ret = stmf_lun_reset_poll(lm_ent->ent_lu, task, 1); 7278 rw_enter(iss->iss_lockp, RW_READER); 7279 if (ret == STMF_SUCCESS) 7280 continue; 7281 not_done = 1; 7282 if (ret != STMF_BUSY) { 7283 rw_exit(iss->iss_lockp); 7284 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 7285 STMF_ABORTED, NULL); 7286 return; 7287 } 7288 } 7289 } 7290 rw_exit(iss->iss_lockp); 7291 7292 if (not_done) { 7293 if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT) 7294 != STMF_SUCCESS) { 7295 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 7296 STMF_ALLOC_FAILURE, NULL); 7297 return; 7298 } 7299 return; 7300 } 7301 7302 atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE); 7303 7304 stmf_scsilib_send_status(task, STATUS_GOOD, 0); 7305 } 7306 7307 stmf_status_t 7308 stmf_lu_add_event(stmf_lu_t *lu, int eventid) 7309 { 7310 stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 7311 7312 if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) { 7313 return (STMF_INVALID_ARG); 7314 } 7315 7316 STMF_EVENT_ADD(ilu->ilu_event_hdl, eventid); 7317 return (STMF_SUCCESS); 7318 } 7319 7320 stmf_status_t 7321 stmf_lu_remove_event(stmf_lu_t *lu, int eventid) 7322 { 7323 stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 7324 7325 if (eventid == STMF_EVENT_ALL) { 7326 STMF_EVENT_CLEAR_ALL(ilu->ilu_event_hdl); 7327 return (STMF_SUCCESS); 7328 } 7329 7330 if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) { 7331 return (STMF_INVALID_ARG); 7332 } 7333 7334 STMF_EVENT_REMOVE(ilu->ilu_event_hdl, eventid); 7335 return (STMF_SUCCESS); 7336 } 7337 7338 stmf_status_t 7339 stmf_lport_add_event(stmf_local_port_t *lport, int eventid) 7340 { 7341 stmf_i_local_port_t *ilport = 7342 (stmf_i_local_port_t *)lport->lport_stmf_private; 7343 7344 if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) { 7345 return (STMF_INVALID_ARG); 7346 } 7347 7348 STMF_EVENT_ADD(ilport->ilport_event_hdl, eventid); 7349 return (STMF_SUCCESS); 7350 } 7351 7352 stmf_status_t 7353 stmf_lport_remove_event(stmf_local_port_t *lport, int eventid) 7354 { 7355 stmf_i_local_port_t *ilport = 7356 (stmf_i_local_port_t *)lport->lport_stmf_private; 7357 7358 if (eventid == STMF_EVENT_ALL) { 7359 STMF_EVENT_CLEAR_ALL(ilport->ilport_event_hdl); 7360 return (STMF_SUCCESS); 7361 } 7362 7363 if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) { 7364 return (STMF_INVALID_ARG); 7365 } 7366 7367 STMF_EVENT_REMOVE(ilport->ilport_event_hdl, eventid); 7368 return (STMF_SUCCESS); 7369 } 7370 7371 void 7372 stmf_generate_lu_event(stmf_i_lu_t *ilu, int eventid, void *arg, uint32_t flags) 7373 { 7374 if (STMF_EVENT_ENABLED(ilu->ilu_event_hdl, eventid) && 7375 (ilu->ilu_lu->lu_event_handler != NULL)) { 7376 ilu->ilu_lu->lu_event_handler(ilu->ilu_lu, eventid, arg, flags); 7377 } 7378 } 7379 7380 void 7381 stmf_generate_lport_event(stmf_i_local_port_t *ilport, int eventid, void *arg, 7382 uint32_t flags) 7383 { 7384 if (STMF_EVENT_ENABLED(ilport->ilport_event_hdl, eventid) && 7385 (ilport->ilport_lport->lport_event_handler != NULL)) { 7386 ilport->ilport_lport->lport_event_handler( 7387 ilport->ilport_lport, eventid, arg, flags); 7388 } 7389 } 7390 7391 /* 7392 * With the possibility of having multiple itl sessions pointing to the 7393 * same itl_kstat_info, the ilu_kstat_lock mutex is used to synchronize 7394 * the kstat update of the ilu_kstat_io, itl_kstat_taskq and itl_kstat_lu_xfer 7395 * statistics. 7396 */ 7397 void 7398 stmf_itl_task_start(stmf_i_scsi_task_t *itask) 7399 { 7400 stmf_itl_data_t *itl = itask->itask_itl_datap; 7401 scsi_task_t *task = itask->itask_task; 7402 stmf_i_lu_t *ilu; 7403 7404 if (itl == NULL || task->task_lu == dlun0) 7405 return; 7406 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 7407 mutex_enter(ilu->ilu_kstat_io->ks_lock); 7408 itask->itask_start_timestamp = gethrtime(); 7409 kstat_waitq_enter(KSTAT_IO_PTR(itl->itl_kstat_taskq)); 7410 stmf_update_kstat_lu_q(itask->itask_task, kstat_waitq_enter); 7411 mutex_exit(ilu->ilu_kstat_io->ks_lock); 7412 7413 stmf_update_kstat_lport_q(itask->itask_task, kstat_waitq_enter); 7414 } 7415 7416 void 7417 stmf_itl_lu_new_task(stmf_i_scsi_task_t *itask) 7418 { 7419 stmf_itl_data_t *itl = itask->itask_itl_datap; 7420 scsi_task_t *task = itask->itask_task; 7421 stmf_i_lu_t *ilu; 7422 7423 if (itl == NULL || task->task_lu == dlun0) 7424 return; 7425 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 7426 mutex_enter(ilu->ilu_kstat_io->ks_lock); 7427 kstat_waitq_to_runq(KSTAT_IO_PTR(itl->itl_kstat_taskq)); 7428 stmf_update_kstat_lu_q(itask->itask_task, kstat_waitq_to_runq); 7429 mutex_exit(ilu->ilu_kstat_io->ks_lock); 7430 7431 stmf_update_kstat_lport_q(itask->itask_task, kstat_waitq_to_runq); 7432 } 7433 7434 void 7435 stmf_itl_task_done(stmf_i_scsi_task_t *itask) 7436 { 7437 stmf_itl_data_t *itl = itask->itask_itl_datap; 7438 scsi_task_t *task = itask->itask_task; 7439 kstat_io_t *kip; 7440 hrtime_t elapsed_time; 7441 stmf_kstat_itl_info_t *itli; 7442 stmf_i_lu_t *ilu; 7443 7444 if (itl == NULL || task->task_lu == dlun0) 7445 return; 7446 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 7447 7448 mutex_enter(ilu->ilu_kstat_io->ks_lock); 7449 itli = (stmf_kstat_itl_info_t *)KSTAT_NAMED_PTR(itl->itl_kstat_info); 7450 kip = KSTAT_IO_PTR(itl->itl_kstat_taskq); 7451 7452 itli->i_task_waitq_elapsed.value.ui64 += itask->itask_waitq_time; 7453 7454 itask->itask_done_timestamp = gethrtime(); 7455 elapsed_time = 7456 itask->itask_done_timestamp - itask->itask_start_timestamp; 7457 7458 if (task->task_flags & TF_READ_DATA) { 7459 kip->reads++; 7460 kip->nread += itask->itask_read_xfer; 7461 itli->i_task_read_elapsed.value.ui64 += elapsed_time; 7462 itli->i_lu_read_elapsed.value.ui64 += 7463 itask->itask_lu_read_time; 7464 itli->i_lport_read_elapsed.value.ui64 += 7465 itask->itask_lport_read_time; 7466 } 7467 7468 if (task->task_flags & TF_WRITE_DATA) { 7469 kip->writes++; 7470 kip->nwritten += itask->itask_write_xfer; 7471 itli->i_task_write_elapsed.value.ui64 += elapsed_time; 7472 itli->i_lu_write_elapsed.value.ui64 += 7473 itask->itask_lu_write_time; 7474 itli->i_lport_write_elapsed.value.ui64 += 7475 itask->itask_lport_write_time; 7476 } 7477 7478 if (itask->itask_flags & ITASK_KSTAT_IN_RUNQ) { 7479 kstat_runq_exit(kip); 7480 stmf_update_kstat_lu_q(task, kstat_runq_exit); 7481 mutex_exit(ilu->ilu_kstat_io->ks_lock); 7482 stmf_update_kstat_lport_q(task, kstat_runq_exit); 7483 } else { 7484 kstat_waitq_exit(kip); 7485 stmf_update_kstat_lu_q(task, kstat_waitq_exit); 7486 mutex_exit(ilu->ilu_kstat_io->ks_lock); 7487 stmf_update_kstat_lport_q(task, kstat_waitq_exit); 7488 } 7489 } 7490 7491 void 7492 stmf_lu_xfer_start(scsi_task_t *task) 7493 { 7494 stmf_i_scsi_task_t *itask = task->task_stmf_private; 7495 stmf_itl_data_t *itl = itask->itask_itl_datap; 7496 stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 7497 kstat_io_t *kip; 7498 7499 if (itl == NULL || task->task_lu == dlun0) 7500 return; 7501 7502 kip = KSTAT_IO_PTR(itl->itl_kstat_lu_xfer); 7503 mutex_enter(ilu->ilu_kstat_io->ks_lock); 7504 kstat_runq_enter(kip); 7505 mutex_exit(ilu->ilu_kstat_io->ks_lock); 7506 } 7507 7508 void 7509 stmf_lu_xfer_done(scsi_task_t *task, boolean_t read, uint64_t xfer_bytes, 7510 hrtime_t elapsed_time) 7511 { 7512 stmf_i_scsi_task_t *itask = task->task_stmf_private; 7513 stmf_itl_data_t *itl = itask->itask_itl_datap; 7514 stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 7515 kstat_io_t *kip; 7516 7517 if (itl == NULL || task->task_lu == dlun0) 7518 return; 7519 7520 if (read) { 7521 atomic_add_64((uint64_t *)&itask->itask_lu_read_time, 7522 elapsed_time); 7523 } else { 7524 atomic_add_64((uint64_t *)&itask->itask_lu_write_time, 7525 elapsed_time); 7526 } 7527 7528 kip = KSTAT_IO_PTR(itl->itl_kstat_lu_xfer); 7529 mutex_enter(ilu->ilu_kstat_io->ks_lock); 7530 kstat_runq_exit(kip); 7531 if (read) { 7532 kip->reads++; 7533 kip->nread += xfer_bytes; 7534 } else { 7535 kip->writes++; 7536 kip->nwritten += xfer_bytes; 7537 } 7538 mutex_exit(ilu->ilu_kstat_io->ks_lock); 7539 } 7540 7541 static void 7542 stmf_lport_xfer_start(stmf_i_scsi_task_t *itask, stmf_data_buf_t *dbuf) 7543 { 7544 stmf_itl_data_t *itl = itask->itask_itl_datap; 7545 7546 if (itl == NULL) 7547 return; 7548 7549 DTRACE_PROBE2(scsi__xfer__start, scsi_task_t *, itask->itask_task, 7550 stmf_data_buf_t *, dbuf); 7551 7552 dbuf->db_xfer_start_timestamp = gethrtime(); 7553 } 7554 7555 static void 7556 stmf_lport_xfer_done(stmf_i_scsi_task_t *itask, stmf_data_buf_t *dbuf) 7557 { 7558 stmf_itl_data_t *itl = itask->itask_itl_datap; 7559 scsi_task_t *task; 7560 stmf_i_local_port_t *ilp; 7561 kstat_io_t *kip; 7562 hrtime_t elapsed_time; 7563 uint64_t xfer_size; 7564 7565 if (itl == NULL) 7566 return; 7567 7568 task = (scsi_task_t *)itask->itask_task; 7569 ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private; 7570 xfer_size = (dbuf->db_xfer_status == STMF_SUCCESS) ? 7571 dbuf->db_data_size : 0; 7572 7573 elapsed_time = gethrtime() - dbuf->db_xfer_start_timestamp; 7574 if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) { 7575 atomic_add_64((uint64_t *)&itask->itask_lport_read_time, 7576 elapsed_time); 7577 atomic_add_64((uint64_t *)&itask->itask_read_xfer, 7578 xfer_size); 7579 } else { 7580 atomic_add_64((uint64_t *)&itask->itask_lport_write_time, 7581 elapsed_time); 7582 atomic_add_64((uint64_t *)&itask->itask_write_xfer, 7583 xfer_size); 7584 } 7585 7586 DTRACE_PROBE3(scsi__xfer__end, scsi_task_t *, itask->itask_task, 7587 stmf_data_buf_t *, dbuf, hrtime_t, elapsed_time); 7588 7589 kip = KSTAT_IO_PTR(itl->itl_kstat_lport_xfer); 7590 mutex_enter(ilp->ilport_kstat_io->ks_lock); 7591 if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) { 7592 kip->reads++; 7593 kip->nread += xfer_size; 7594 } else { 7595 kip->writes++; 7596 kip->nwritten += xfer_size; 7597 } 7598 mutex_exit(ilp->ilport_kstat_io->ks_lock); 7599 7600 dbuf->db_xfer_start_timestamp = 0; 7601 } 7602 7603 void 7604 stmf_svc_init() 7605 { 7606 if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) 7607 return; 7608 stmf_state.stmf_svc_taskq = ddi_taskq_create(0, "STMF_SVC_TASKQ", 1, 7609 TASKQ_DEFAULTPRI, 0); 7610 (void) ddi_taskq_dispatch(stmf_state.stmf_svc_taskq, 7611 stmf_svc, 0, DDI_SLEEP); 7612 } 7613 7614 stmf_status_t 7615 stmf_svc_fini() 7616 { 7617 uint32_t i; 7618 7619 mutex_enter(&stmf_state.stmf_lock); 7620 if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) { 7621 stmf_state.stmf_svc_flags |= STMF_SVC_TERMINATE; 7622 cv_signal(&stmf_state.stmf_cv); 7623 } 7624 mutex_exit(&stmf_state.stmf_lock); 7625 7626 /* Wait for 5 seconds */ 7627 for (i = 0; i < 500; i++) { 7628 if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) 7629 delay(drv_usectohz(10000)); 7630 else 7631 break; 7632 } 7633 if (i == 500) 7634 return (STMF_BUSY); 7635 7636 ddi_taskq_destroy(stmf_state.stmf_svc_taskq); 7637 7638 return (STMF_SUCCESS); 7639 } 7640 7641 /* ARGSUSED */ 7642 void 7643 stmf_svc(void *arg) 7644 { 7645 stmf_svc_req_t *req, **preq; 7646 clock_t td; 7647 clock_t drain_start, drain_next = 0; 7648 clock_t timing_start, timing_next = 0; 7649 clock_t worker_delay = 0; 7650 int deq; 7651 stmf_lu_t *lu; 7652 stmf_i_lu_t *ilu; 7653 stmf_local_port_t *lport; 7654 stmf_i_local_port_t *ilport, *next_ilport; 7655 stmf_i_scsi_session_t *iss; 7656 7657 td = drv_usectohz(20000); 7658 7659 mutex_enter(&stmf_state.stmf_lock); 7660 stmf_state.stmf_svc_flags |= STMF_SVC_STARTED | STMF_SVC_ACTIVE; 7661 7662 stmf_svc_loop: 7663 if (stmf_state.stmf_svc_flags & STMF_SVC_TERMINATE) { 7664 stmf_state.stmf_svc_flags &= 7665 ~(STMF_SVC_STARTED | STMF_SVC_ACTIVE); 7666 mutex_exit(&stmf_state.stmf_lock); 7667 return; 7668 } 7669 7670 if (stmf_state.stmf_svc_active) { 7671 int waitq_add = 0; 7672 req = stmf_state.stmf_svc_active; 7673 stmf_state.stmf_svc_active = req->svc_next; 7674 7675 switch (req->svc_cmd) { 7676 case STMF_CMD_LPORT_ONLINE: 7677 /* Fallthrough */ 7678 case STMF_CMD_LPORT_OFFLINE: 7679 /* Fallthrough */ 7680 case STMF_CMD_LU_ONLINE: 7681 /* Nothing to do */ 7682 waitq_add = 1; 7683 break; 7684 7685 case STMF_CMD_LU_OFFLINE: 7686 /* Remove all mappings of this LU */ 7687 stmf_session_lu_unmapall((stmf_lu_t *)req->svc_obj); 7688 /* Kill all the pending I/Os for this LU */ 7689 mutex_exit(&stmf_state.stmf_lock); 7690 stmf_task_lu_killall((stmf_lu_t *)req->svc_obj, NULL, 7691 STMF_ABORTED); 7692 mutex_enter(&stmf_state.stmf_lock); 7693 waitq_add = 1; 7694 break; 7695 default: 7696 cmn_err(CE_PANIC, "stmf_svc: unknown cmd %d", 7697 req->svc_cmd); 7698 } 7699 7700 if (waitq_add) { 7701 /* Put it in the wait queue */ 7702 req->svc_next = stmf_state.stmf_svc_waiting; 7703 stmf_state.stmf_svc_waiting = req; 7704 } 7705 } 7706 7707 /* The waiting list is not going to be modified by anybody else */ 7708 mutex_exit(&stmf_state.stmf_lock); 7709 7710 for (preq = &stmf_state.stmf_svc_waiting; (*preq) != NULL; ) { 7711 req = *preq; 7712 deq = 0; 7713 7714 switch (req->svc_cmd) { 7715 case STMF_CMD_LU_ONLINE: 7716 lu = (stmf_lu_t *)req->svc_obj; 7717 deq = 1; 7718 lu->lu_ctl(lu, req->svc_cmd, &req->svc_info); 7719 break; 7720 7721 case STMF_CMD_LU_OFFLINE: 7722 lu = (stmf_lu_t *)req->svc_obj; 7723 ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 7724 if (ilu->ilu_ntasks != ilu->ilu_ntasks_free) 7725 break; 7726 deq = 1; 7727 lu->lu_ctl(lu, req->svc_cmd, &req->svc_info); 7728 break; 7729 7730 case STMF_CMD_LPORT_OFFLINE: 7731 /* Fallthrough */ 7732 case STMF_CMD_LPORT_ONLINE: 7733 lport = (stmf_local_port_t *)req->svc_obj; 7734 deq = 1; 7735 lport->lport_ctl(lport, req->svc_cmd, &req->svc_info); 7736 break; 7737 } 7738 if (deq) { 7739 *preq = req->svc_next; 7740 kmem_free(req, req->svc_req_alloc_size); 7741 } else { 7742 preq = &req->svc_next; 7743 } 7744 } 7745 7746 mutex_enter(&stmf_state.stmf_lock); 7747 if (stmf_state.stmf_svc_active == NULL) { 7748 /* Do timeouts */ 7749 if (stmf_state.stmf_nlus && 7750 ((!timing_next) || (ddi_get_lbolt() >= timing_next))) { 7751 if (!stmf_state.stmf_svc_ilu_timing) { 7752 /* we are starting a new round */ 7753 stmf_state.stmf_svc_ilu_timing = 7754 stmf_state.stmf_ilulist; 7755 timing_start = ddi_get_lbolt(); 7756 } 7757 stmf_check_ilu_timing(); 7758 if (!stmf_state.stmf_svc_ilu_timing) { 7759 /* we finished a complete round */ 7760 timing_next = 7761 timing_start + drv_usectohz(5*1000*1000); 7762 } else { 7763 /* we still have some ilu items to check */ 7764 timing_next = 7765 ddi_get_lbolt() + drv_usectohz(1*1000*1000); 7766 } 7767 if (stmf_state.stmf_svc_active) 7768 goto stmf_svc_loop; 7769 } 7770 /* Check if there are free tasks to clear */ 7771 if (stmf_state.stmf_nlus && 7772 ((!drain_next) || (ddi_get_lbolt() >= drain_next))) { 7773 if (!stmf_state.stmf_svc_ilu_draining) { 7774 /* we are starting a new round */ 7775 stmf_state.stmf_svc_ilu_draining = 7776 stmf_state.stmf_ilulist; 7777 drain_start = ddi_get_lbolt(); 7778 } 7779 stmf_check_freetask(); 7780 if (!stmf_state.stmf_svc_ilu_draining) { 7781 /* we finished a complete round */ 7782 drain_next = 7783 drain_start + drv_usectohz(10*1000*1000); 7784 } else { 7785 /* we still have some ilu items to check */ 7786 drain_next = 7787 ddi_get_lbolt() + drv_usectohz(1*1000*1000); 7788 } 7789 if (stmf_state.stmf_svc_active) 7790 goto stmf_svc_loop; 7791 } 7792 7793 /* Check if we need to run worker_mgmt */ 7794 if (ddi_get_lbolt() > worker_delay) { 7795 stmf_worker_mgmt(); 7796 worker_delay = ddi_get_lbolt() + 7797 stmf_worker_mgmt_delay; 7798 } 7799 7800 /* Check if any active session got its 1st LUN */ 7801 if (stmf_state.stmf_process_initial_luns) { 7802 int stmf_level = 0; 7803 int port_level; 7804 for (ilport = stmf_state.stmf_ilportlist; ilport; 7805 ilport = next_ilport) { 7806 int ilport_lock_held; 7807 next_ilport = ilport->ilport_next; 7808 if ((ilport->ilport_flags & 7809 ILPORT_SS_GOT_INITIAL_LUNS) == 0) { 7810 continue; 7811 } 7812 port_level = 0; 7813 rw_enter(&ilport->ilport_lock, RW_READER); 7814 ilport_lock_held = 1; 7815 for (iss = ilport->ilport_ss_list; iss; 7816 iss = iss->iss_next) { 7817 if ((iss->iss_flags & 7818 ISS_GOT_INITIAL_LUNS) == 0) { 7819 continue; 7820 } 7821 port_level++; 7822 stmf_level++; 7823 atomic_and_32(&iss->iss_flags, 7824 ~ISS_GOT_INITIAL_LUNS); 7825 atomic_or_32(&iss->iss_flags, 7826 ISS_EVENT_ACTIVE); 7827 rw_exit(&ilport->ilport_lock); 7828 ilport_lock_held = 0; 7829 mutex_exit(&stmf_state.stmf_lock); 7830 stmf_generate_lport_event(ilport, 7831 LPORT_EVENT_INITIAL_LUN_MAPPED, 7832 iss->iss_ss, 0); 7833 atomic_and_32(&iss->iss_flags, 7834 ~ISS_EVENT_ACTIVE); 7835 mutex_enter(&stmf_state.stmf_lock); 7836 /* 7837 * scan all the ilports again as the 7838 * ilport list might have changed. 7839 */ 7840 next_ilport = 7841 stmf_state.stmf_ilportlist; 7842 break; 7843 } 7844 if (port_level == 0) { 7845 atomic_and_32(&ilport->ilport_flags, 7846 ~ILPORT_SS_GOT_INITIAL_LUNS); 7847 } 7848 /* drop the lock if we are holding it. */ 7849 if (ilport_lock_held == 1) 7850 rw_exit(&ilport->ilport_lock); 7851 7852 /* Max 4 session at a time */ 7853 if (stmf_level >= 4) { 7854 break; 7855 } 7856 } 7857 if (stmf_level == 0) { 7858 stmf_state.stmf_process_initial_luns = 0; 7859 } 7860 } 7861 7862 stmf_state.stmf_svc_flags &= ~STMF_SVC_ACTIVE; 7863 (void) cv_reltimedwait(&stmf_state.stmf_cv, 7864 &stmf_state.stmf_lock, td, TR_CLOCK_TICK); 7865 stmf_state.stmf_svc_flags |= STMF_SVC_ACTIVE; 7866 } 7867 goto stmf_svc_loop; 7868 } 7869 7870 void 7871 stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info) 7872 { 7873 stmf_svc_req_t *req; 7874 int s; 7875 7876 ASSERT(!mutex_owned(&stmf_state.stmf_lock)); 7877 s = sizeof (stmf_svc_req_t); 7878 if (info->st_additional_info) { 7879 s += strlen(info->st_additional_info) + 1; 7880 } 7881 req = kmem_zalloc(s, KM_SLEEP); 7882 7883 req->svc_cmd = cmd; 7884 req->svc_obj = obj; 7885 req->svc_info.st_rflags = info->st_rflags; 7886 if (info->st_additional_info) { 7887 req->svc_info.st_additional_info = (char *)(GET_BYTE_OFFSET(req, 7888 sizeof (stmf_svc_req_t))); 7889 (void) strcpy(req->svc_info.st_additional_info, 7890 info->st_additional_info); 7891 } 7892 req->svc_req_alloc_size = s; 7893 7894 mutex_enter(&stmf_state.stmf_lock); 7895 req->svc_next = stmf_state.stmf_svc_active; 7896 stmf_state.stmf_svc_active = req; 7897 if ((stmf_state.stmf_svc_flags & STMF_SVC_ACTIVE) == 0) { 7898 cv_signal(&stmf_state.stmf_cv); 7899 } 7900 mutex_exit(&stmf_state.stmf_lock); 7901 } 7902 7903 void 7904 stmf_trace(caddr_t ident, const char *fmt, ...) 7905 { 7906 va_list args; 7907 char tbuf[160]; 7908 int len; 7909 7910 if (!stmf_trace_on) 7911 return; 7912 len = snprintf(tbuf, 158, "%s:%07lu: ", ident ? ident : "", 7913 ddi_get_lbolt()); 7914 va_start(args, fmt); 7915 len += vsnprintf(tbuf + len, 158 - len, fmt, args); 7916 va_end(args); 7917 7918 if (len > 158) { 7919 len = 158; 7920 } 7921 tbuf[len++] = '\n'; 7922 tbuf[len] = 0; 7923 7924 mutex_enter(&trace_buf_lock); 7925 bcopy(tbuf, &stmf_trace_buf[trace_buf_curndx], len+1); 7926 trace_buf_curndx += len; 7927 if (trace_buf_curndx > (trace_buf_size - 320)) 7928 trace_buf_curndx = 0; 7929 mutex_exit(&trace_buf_lock); 7930 } 7931 7932 void 7933 stmf_trace_clear() 7934 { 7935 if (!stmf_trace_on) 7936 return; 7937 mutex_enter(&trace_buf_lock); 7938 trace_buf_curndx = 0; 7939 if (trace_buf_size > 0) 7940 stmf_trace_buf[0] = 0; 7941 mutex_exit(&trace_buf_lock); 7942 } 7943 7944 static void 7945 stmf_abort_task_offline(scsi_task_t *task, int offline_lu, char *info) 7946 { 7947 stmf_state_change_info_t change_info; 7948 void *ctl_private; 7949 uint32_t ctl_cmd; 7950 int msg = 0; 7951 7952 stmf_trace("FROM STMF", "abort_task_offline called for %s: %s", 7953 offline_lu ? "LU" : "LPORT", info ? info : "no additional info"); 7954 change_info.st_additional_info = info; 7955 if (offline_lu) { 7956 change_info.st_rflags = STMF_RFLAG_RESET | 7957 STMF_RFLAG_LU_ABORT; 7958 ctl_private = task->task_lu; 7959 if (((stmf_i_lu_t *) 7960 task->task_lu->lu_stmf_private)->ilu_state == 7961 STMF_STATE_ONLINE) { 7962 msg = 1; 7963 } 7964 ctl_cmd = STMF_CMD_LU_OFFLINE; 7965 } else { 7966 change_info.st_rflags = STMF_RFLAG_RESET | 7967 STMF_RFLAG_LPORT_ABORT; 7968 ctl_private = task->task_lport; 7969 if (((stmf_i_local_port_t *) 7970 task->task_lport->lport_stmf_private)->ilport_state == 7971 STMF_STATE_ONLINE) { 7972 msg = 1; 7973 } 7974 ctl_cmd = STMF_CMD_LPORT_OFFLINE; 7975 } 7976 7977 if (msg) { 7978 stmf_trace(0, "Calling stmf_ctl to offline %s : %s", 7979 offline_lu ? "LU" : "LPORT", info ? info : 7980 "<no additional info>"); 7981 } 7982 (void) stmf_ctl(ctl_cmd, ctl_private, &change_info); 7983 } 7984