1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/conf.h> 27 #include <sys/file.h> 28 #include <sys/ddi.h> 29 #include <sys/sunddi.h> 30 #include <sys/modctl.h> 31 #include <sys/scsi/scsi.h> 32 #include <sys/scsi/impl/scsi_reset_notify.h> 33 #include <sys/disp.h> 34 #include <sys/byteorder.h> 35 #include <sys/atomic.h> 36 #include <sys/ethernet.h> 37 #include <sys/sdt.h> 38 #include <sys/nvpair.h> 39 #include <sys/zone.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 49 static uint64_t stmf_session_counter = 0; 50 static uint16_t stmf_rtpid_counter = 0; 51 52 static int stmf_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 53 static int stmf_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 54 static int stmf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, 55 void **result); 56 static int stmf_open(dev_t *devp, int flag, int otype, cred_t *credp); 57 static int stmf_close(dev_t dev, int flag, int otype, cred_t *credp); 58 static int stmf_ioctl(dev_t dev, int cmd, intptr_t data, int mode, 59 cred_t *credp, int *rval); 60 static int stmf_get_stmf_state(stmf_state_desc_t *std); 61 static int stmf_set_stmf_state(stmf_state_desc_t *std); 62 static void stmf_abort_task_offline(scsi_task_t *task, int offline_lu, 63 char *info); 64 void stmf_svc_init(); 65 stmf_status_t stmf_svc_fini(); 66 void stmf_svc(void *arg); 67 void stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info); 68 void stmf_check_freetask(); 69 void stmf_abort_target_reset(scsi_task_t *task); 70 stmf_status_t stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task, 71 int target_reset); 72 void stmf_target_reset_poll(struct scsi_task *task); 73 void stmf_handle_lun_reset(scsi_task_t *task); 74 void stmf_handle_target_reset(scsi_task_t *task); 75 void stmf_xd_to_dbuf(stmf_data_buf_t *dbuf); 76 int stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi); 77 int stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi); 78 void stmf_delete_ppd(stmf_pp_data_t *ppd); 79 void stmf_delete_all_ppds(); 80 void stmf_trace_clear(); 81 void stmf_worker_init(); 82 stmf_status_t stmf_worker_fini(); 83 void stmf_worker_mgmt(); 84 void stmf_worker_task(void *arg); 85 86 static void stmf_update_kstat_lu_q(scsi_task_t *, void()); 87 static void stmf_update_kstat_lport_q(scsi_task_t *, void()); 88 static void stmf_update_kstat_lu_io(scsi_task_t *, stmf_data_buf_t *); 89 static void stmf_update_kstat_lport_io(scsi_task_t *, stmf_data_buf_t *); 90 91 extern struct mod_ops mod_driverops; 92 93 /* =====[ Tunables ]===== */ 94 /* Internal tracing */ 95 volatile int stmf_trace_on = 1; 96 volatile int stmf_trace_buf_size = (1 * 1024 * 1024); 97 /* 98 * The reason default task timeout is 75 is because we want the 99 * host to timeout 1st and mostly host timeout is 60 seconds. 100 */ 101 volatile int stmf_default_task_timeout = 75; 102 /* 103 * Setting this to one means, you are responsible for config load and keeping 104 * things in sync with persistent database. 105 */ 106 volatile int stmf_allow_modunload = 0; 107 108 volatile int stmf_max_nworkers = 256; 109 volatile int stmf_min_nworkers = 4; 110 volatile int stmf_worker_scale_down_delay = 20; 111 112 /* === [ Debugging and fault injection ] === */ 113 #ifdef DEBUG 114 volatile int stmf_drop_task_counter = 0; 115 volatile int stmf_drop_buf_counter = 0; 116 117 #endif 118 119 stmf_state_t stmf_state; 120 static stmf_lu_t *dlun0; 121 122 static uint8_t stmf_first_zero[] = 123 { 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0xff }; 124 static uint8_t stmf_first_one[] = 125 { 0xff, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 }; 126 127 static kmutex_t trace_buf_lock; 128 static int trace_buf_size; 129 static int trace_buf_curndx; 130 caddr_t stmf_trace_buf; 131 132 static enum { 133 STMF_WORKERS_DISABLED = 0, 134 STMF_WORKERS_ENABLING, 135 STMF_WORKERS_ENABLED 136 } stmf_workers_state = STMF_WORKERS_DISABLED; 137 static int stmf_i_max_nworkers; 138 static int stmf_i_min_nworkers; 139 static int stmf_nworkers_cur; /* # of workers currently running */ 140 static int stmf_nworkers_needed; /* # of workers need to be running */ 141 static int stmf_worker_sel_counter = 0; 142 static uint32_t stmf_cur_ntasks = 0; 143 static clock_t stmf_wm_last = 0; 144 /* 145 * This is equal to stmf_nworkers_cur while we are increasing # workers and 146 * stmf_nworkers_needed while we are decreasing the worker count. 147 */ 148 static int stmf_nworkers_accepting_cmds; 149 static stmf_worker_t *stmf_workers = NULL; 150 static clock_t stmf_worker_mgmt_delay = 2; 151 static clock_t stmf_worker_scale_down_timer = 0; 152 static int stmf_worker_scale_down_qd = 0; 153 154 static struct cb_ops stmf_cb_ops = { 155 stmf_open, /* open */ 156 stmf_close, /* close */ 157 nodev, /* strategy */ 158 nodev, /* print */ 159 nodev, /* dump */ 160 nodev, /* read */ 161 nodev, /* write */ 162 stmf_ioctl, /* ioctl */ 163 nodev, /* devmap */ 164 nodev, /* mmap */ 165 nodev, /* segmap */ 166 nochpoll, /* chpoll */ 167 ddi_prop_op, /* cb_prop_op */ 168 0, /* streamtab */ 169 D_NEW | D_MP, /* cb_flag */ 170 CB_REV, /* rev */ 171 nodev, /* aread */ 172 nodev /* awrite */ 173 }; 174 175 static struct dev_ops stmf_ops = { 176 DEVO_REV, 177 0, 178 stmf_getinfo, 179 nulldev, /* identify */ 180 nulldev, /* probe */ 181 stmf_attach, 182 stmf_detach, 183 nodev, /* reset */ 184 &stmf_cb_ops, 185 NULL, /* bus_ops */ 186 NULL /* power */ 187 }; 188 189 #define STMF_NAME "COMSTAR STMF" 190 #define STMF_MODULE_NAME "stmf" 191 192 static struct modldrv modldrv = { 193 &mod_driverops, 194 STMF_NAME, 195 &stmf_ops 196 }; 197 198 static struct modlinkage modlinkage = { 199 MODREV_1, 200 &modldrv, 201 NULL 202 }; 203 204 int 205 _init(void) 206 { 207 int ret; 208 209 ret = mod_install(&modlinkage); 210 if (ret) 211 return (ret); 212 stmf_trace_buf = kmem_zalloc(stmf_trace_buf_size, KM_SLEEP); 213 trace_buf_size = stmf_trace_buf_size; 214 trace_buf_curndx = 0; 215 mutex_init(&trace_buf_lock, NULL, MUTEX_DRIVER, 0); 216 bzero(&stmf_state, sizeof (stmf_state_t)); 217 /* STMF service is off by default */ 218 stmf_state.stmf_service_running = 0; 219 mutex_init(&stmf_state.stmf_lock, NULL, MUTEX_DRIVER, NULL); 220 cv_init(&stmf_state.stmf_cv, NULL, CV_DRIVER, NULL); 221 stmf_session_counter = (uint64_t)ddi_get_lbolt(); 222 stmf_view_init(); 223 stmf_svc_init(); 224 stmf_dlun_init(); 225 return (ret); 226 } 227 228 int 229 _fini(void) 230 { 231 int ret; 232 233 if (stmf_state.stmf_service_running) 234 return (EBUSY); 235 if ((!stmf_allow_modunload) && 236 (stmf_state.stmf_config_state != STMF_CONFIG_NONE)) { 237 return (EBUSY); 238 } 239 if (stmf_state.stmf_nlps || stmf_state.stmf_npps) { 240 return (EBUSY); 241 } 242 if (stmf_dlun_fini() != STMF_SUCCESS) 243 return (EBUSY); 244 if (stmf_worker_fini() != STMF_SUCCESS) { 245 stmf_dlun_init(); 246 return (EBUSY); 247 } 248 if (stmf_svc_fini() != STMF_SUCCESS) { 249 stmf_dlun_init(); 250 stmf_worker_init(); 251 return (EBUSY); 252 } 253 254 ret = mod_remove(&modlinkage); 255 if (ret) { 256 stmf_svc_init(); 257 stmf_dlun_init(); 258 stmf_worker_init(); 259 return (ret); 260 } 261 262 stmf_view_clear_config(); 263 kmem_free(stmf_trace_buf, stmf_trace_buf_size); 264 mutex_destroy(&trace_buf_lock); 265 mutex_destroy(&stmf_state.stmf_lock); 266 cv_destroy(&stmf_state.stmf_cv); 267 return (ret); 268 } 269 270 int 271 _info(struct modinfo *modinfop) 272 { 273 return (mod_info(&modlinkage, modinfop)); 274 } 275 276 /* ARGSUSED */ 277 static int 278 stmf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 279 { 280 switch (cmd) { 281 case DDI_INFO_DEVT2DEVINFO: 282 *result = stmf_state.stmf_dip; 283 break; 284 case DDI_INFO_DEVT2INSTANCE: 285 *result = (void *)(uintptr_t)ddi_get_instance(dip); 286 break; 287 default: 288 return (DDI_FAILURE); 289 } 290 291 return (DDI_SUCCESS); 292 } 293 294 static int 295 stmf_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 296 { 297 switch (cmd) { 298 case DDI_ATTACH: 299 stmf_state.stmf_dip = dip; 300 301 if (ddi_create_minor_node(dip, "admin", S_IFCHR, 0, 302 DDI_NT_STMF, 0) != DDI_SUCCESS) { 303 break; 304 } 305 ddi_report_dev(dip); 306 return (DDI_SUCCESS); 307 } 308 309 return (DDI_FAILURE); 310 } 311 312 static int 313 stmf_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 314 { 315 switch (cmd) { 316 case DDI_DETACH: 317 ddi_remove_minor_node(dip, 0); 318 return (DDI_SUCCESS); 319 } 320 321 return (DDI_FAILURE); 322 } 323 324 /* ARGSUSED */ 325 static int 326 stmf_open(dev_t *devp, int flag, int otype, cred_t *credp) 327 { 328 mutex_enter(&stmf_state.stmf_lock); 329 if (stmf_state.stmf_exclusive_open) { 330 mutex_exit(&stmf_state.stmf_lock); 331 return (EBUSY); 332 } 333 if (flag & FEXCL) { 334 if (stmf_state.stmf_opened) { 335 mutex_exit(&stmf_state.stmf_lock); 336 return (EBUSY); 337 } 338 stmf_state.stmf_exclusive_open = 1; 339 } 340 stmf_state.stmf_opened = 1; 341 mutex_exit(&stmf_state.stmf_lock); 342 return (0); 343 } 344 345 /* ARGSUSED */ 346 static int 347 stmf_close(dev_t dev, int flag, int otype, cred_t *credp) 348 { 349 mutex_enter(&stmf_state.stmf_lock); 350 stmf_state.stmf_opened = 0; 351 if (stmf_state.stmf_exclusive_open && 352 (stmf_state.stmf_config_state != STMF_CONFIG_INIT_DONE)) { 353 stmf_state.stmf_config_state = STMF_CONFIG_NONE; 354 stmf_delete_all_ppds(); 355 stmf_view_clear_config(); 356 stmf_view_init(); 357 } 358 stmf_state.stmf_exclusive_open = 0; 359 mutex_exit(&stmf_state.stmf_lock); 360 return (0); 361 } 362 363 int 364 stmf_copyin_iocdata(intptr_t data, int mode, stmf_iocdata_t **iocd, 365 void **ibuf, void **obuf) 366 { 367 int ret; 368 369 *ibuf = NULL; 370 *obuf = NULL; 371 *iocd = kmem_zalloc(sizeof (stmf_iocdata_t), KM_SLEEP); 372 373 ret = ddi_copyin((void *)data, *iocd, sizeof (stmf_iocdata_t), mode); 374 if (ret) 375 return (EFAULT); 376 if ((*iocd)->stmf_version != STMF_VERSION_1) { 377 ret = EINVAL; 378 goto copyin_iocdata_done; 379 } 380 if ((*iocd)->stmf_ibuf_size) { 381 *ibuf = kmem_zalloc((*iocd)->stmf_ibuf_size, KM_SLEEP); 382 ret = ddi_copyin((void *)((unsigned long)(*iocd)->stmf_ibuf), 383 *ibuf, (*iocd)->stmf_ibuf_size, mode); 384 } 385 if ((*iocd)->stmf_obuf_size) 386 *obuf = kmem_zalloc((*iocd)->stmf_obuf_size, KM_SLEEP); 387 388 if (ret == 0) 389 return (0); 390 ret = EFAULT; 391 copyin_iocdata_done:; 392 if (*obuf) { 393 kmem_free(*obuf, (*iocd)->stmf_obuf_size); 394 *obuf = NULL; 395 } 396 if (*ibuf) { 397 kmem_free(*ibuf, (*iocd)->stmf_ibuf_size); 398 *ibuf = NULL; 399 } 400 kmem_free(*iocd, sizeof (stmf_iocdata_t)); 401 return (ret); 402 } 403 404 int 405 stmf_copyout_iocdata(intptr_t data, int mode, stmf_iocdata_t *iocd, void *obuf) 406 { 407 int ret; 408 409 if (iocd->stmf_obuf_size) { 410 ret = ddi_copyout(obuf, (void *)(unsigned long)iocd->stmf_obuf, 411 iocd->stmf_obuf_size, mode); 412 if (ret) 413 return (EFAULT); 414 } 415 ret = ddi_copyout(iocd, (void *)data, sizeof (stmf_iocdata_t), mode); 416 if (ret) 417 return (EFAULT); 418 return (0); 419 } 420 421 /* ARGSUSED */ 422 static int 423 stmf_ioctl(dev_t dev, int cmd, intptr_t data, int mode, 424 cred_t *credp, int *rval) 425 { 426 stmf_iocdata_t *iocd; 427 void *ibuf = NULL, *obuf = NULL; 428 slist_lu_t *luid_list; 429 slist_target_port_t *lportid_list; 430 stmf_i_lu_t *ilu; 431 stmf_i_local_port_t *ilport; 432 stmf_i_scsi_session_t *iss; 433 slist_scsi_session_t *iss_list; 434 sioc_lu_props_t *lup; 435 sioc_target_port_props_t *lportp; 436 stmf_ppioctl_data_t *ppi; 437 uint8_t *p_id; 438 stmf_state_desc_t *std; 439 stmf_status_t ctl_ret; 440 stmf_state_change_info_t ssi; 441 int ret = 0; 442 uint32_t n; 443 int i; 444 stmf_group_op_data_t *grp_entry; 445 stmf_group_name_t *grpname; 446 stmf_view_op_entry_t *ve; 447 stmf_id_type_t idtype; 448 #if 0 449 stmf_id_data_t *id_entry; 450 stmf_id_list_t *id_list; 451 stmf_view_entry_t *view_entry; 452 #endif 453 uint32_t veid; 454 455 if ((cmd & 0xff000000) != STMF_IOCTL) { 456 return (ENOTTY); 457 } 458 459 if (drv_priv(credp) != 0) { 460 return (EPERM); 461 } 462 463 ret = stmf_copyin_iocdata(data, mode, &iocd, &ibuf, &obuf); 464 if (ret) 465 return (ret); 466 iocd->stmf_error = 0; 467 468 switch (cmd) { 469 case STMF_IOCTL_LU_LIST: 470 mutex_enter(&stmf_state.stmf_lock); 471 iocd->stmf_obuf_max_nentries = stmf_state.stmf_nlus; 472 n = min(stmf_state.stmf_nlus, 473 (iocd->stmf_obuf_size)/sizeof (slist_lu_t)); 474 iocd->stmf_obuf_nentries = n; 475 ilu = stmf_state.stmf_ilulist; 476 luid_list = (slist_lu_t *)obuf; 477 for (i = 0; i < n; i++) { 478 uint8_t *id; 479 id = (uint8_t *)ilu->ilu_lu->lu_id; 480 bcopy(id + 4, luid_list[i].lu_guid, 16); 481 ilu = ilu->ilu_next; 482 } 483 mutex_exit(&stmf_state.stmf_lock); 484 break; 485 486 case STMF_IOCTL_TARGET_PORT_LIST: 487 mutex_enter(&stmf_state.stmf_lock); 488 iocd->stmf_obuf_max_nentries = stmf_state.stmf_nlports; 489 n = min(stmf_state.stmf_nlports, 490 (iocd->stmf_obuf_size)/sizeof (slist_target_port_t)); 491 iocd->stmf_obuf_nentries = n; 492 ilport = stmf_state.stmf_ilportlist; 493 lportid_list = (slist_target_port_t *)obuf; 494 for (i = 0; i < n; i++) { 495 uint8_t *id; 496 id = (uint8_t *)ilport->ilport_lport->lport_id; 497 bcopy(id, lportid_list[i].target, id[3] + 4); 498 ilport = ilport->ilport_next; 499 } 500 mutex_exit(&stmf_state.stmf_lock); 501 break; 502 503 case STMF_IOCTL_SESSION_LIST: 504 p_id = (uint8_t *)ibuf; 505 if ((p_id == NULL) || (iocd->stmf_ibuf_size < 4) || 506 (iocd->stmf_ibuf_size < (p_id[3] + 4))) { 507 ret = EINVAL; 508 break; 509 } 510 mutex_enter(&stmf_state.stmf_lock); 511 for (ilport = stmf_state.stmf_ilportlist; ilport; ilport = 512 ilport->ilport_next) { 513 uint8_t *id; 514 id = (uint8_t *)ilport->ilport_lport->lport_id; 515 if ((p_id[3] == id[3]) && 516 (bcmp(p_id + 4, id + 4, id[3]) == 0)) { 517 break; 518 } 519 } 520 if (ilport == NULL) { 521 mutex_exit(&stmf_state.stmf_lock); 522 ret = ENOENT; 523 break; 524 } 525 iocd->stmf_obuf_max_nentries = ilport->ilport_nsessions; 526 n = min(ilport->ilport_nsessions, 527 (iocd->stmf_obuf_size)/sizeof (slist_scsi_session_t)); 528 iocd->stmf_obuf_nentries = n; 529 iss = ilport->ilport_ss_list; 530 iss_list = (slist_scsi_session_t *)obuf; 531 for (i = 0; i < n; i++) { 532 uint8_t *id; 533 id = (uint8_t *)iss->iss_ss->ss_rport_id; 534 bcopy(id, iss_list[i].initiator, id[3] + 4); 535 iss_list[i].creation_time = (uint32_t) 536 iss->iss_creation_time; 537 if (iss->iss_ss->ss_rport_alias) { 538 (void) strncpy(iss_list[i].alias, 539 iss->iss_ss->ss_rport_alias, 255); 540 iss_list[i].alias[255] = 0; 541 } else { 542 iss_list[i].alias[0] = 0; 543 } 544 iss = iss->iss_next; 545 } 546 mutex_exit(&stmf_state.stmf_lock); 547 break; 548 549 case STMF_IOCTL_GET_LU_PROPERTIES: 550 p_id = (uint8_t *)ibuf; 551 if ((iocd->stmf_ibuf_size < 16) || 552 (iocd->stmf_obuf_size < sizeof (sioc_lu_props_t)) || 553 (p_id[0] == 0)) { 554 ret = EINVAL; 555 break; 556 } 557 mutex_enter(&stmf_state.stmf_lock); 558 for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) { 559 if (bcmp(p_id, ilu->ilu_lu->lu_id->ident, 16) == 0) 560 break; 561 } 562 if (ilu == NULL) { 563 mutex_exit(&stmf_state.stmf_lock); 564 ret = ENOENT; 565 break; 566 } 567 lup = (sioc_lu_props_t *)obuf; 568 bcopy(ilu->ilu_lu->lu_id->ident, lup->lu_guid, 16); 569 lup->lu_state = ilu->ilu_state & 0x0f; 570 lup->lu_present = 1; /* XXX */ 571 (void) strncpy(lup->lu_provider_name, 572 ilu->ilu_lu->lu_lp->lp_name, 255); 573 lup->lu_provider_name[254] = 0; 574 if (ilu->ilu_lu->lu_alias) { 575 (void) strncpy(lup->lu_alias, 576 ilu->ilu_lu->lu_alias, 255); 577 lup->lu_alias[255] = 0; 578 } else { 579 lup->lu_alias[0] = 0; 580 } 581 mutex_exit(&stmf_state.stmf_lock); 582 break; 583 584 case STMF_IOCTL_GET_TARGET_PORT_PROPERTIES: 585 p_id = (uint8_t *)ibuf; 586 if ((p_id == NULL) || 587 (iocd->stmf_ibuf_size < (p_id[3] + 4)) || 588 (iocd->stmf_obuf_size < 589 sizeof (sioc_target_port_props_t))) { 590 ret = EINVAL; 591 break; 592 } 593 mutex_enter(&stmf_state.stmf_lock); 594 for (ilport = stmf_state.stmf_ilportlist; ilport; 595 ilport = ilport->ilport_next) { 596 uint8_t *id; 597 id = (uint8_t *)ilport->ilport_lport->lport_id; 598 if ((p_id[3] == id[3]) && 599 (bcmp(p_id+4, id+4, id[3]) == 0)) 600 break; 601 } 602 if (ilport == NULL) { 603 mutex_exit(&stmf_state.stmf_lock); 604 ret = ENOENT; 605 break; 606 } 607 lportp = (sioc_target_port_props_t *)obuf; 608 bcopy(ilport->ilport_lport->lport_id, lportp->tgt_id, 609 ilport->ilport_lport->lport_id->ident_length + 4); 610 lportp->tgt_state = ilport->ilport_state & 0x0f; 611 lportp->tgt_present = 1; /* XXX */ 612 (void) strncpy(lportp->tgt_provider_name, 613 ilport->ilport_lport->lport_pp->pp_name, 255); 614 lportp->tgt_provider_name[254] = 0; 615 if (ilport->ilport_lport->lport_alias) { 616 (void) strncpy(lportp->tgt_alias, 617 ilport->ilport_lport->lport_alias, 255); 618 lportp->tgt_alias[255] = 0; 619 } else { 620 lportp->tgt_alias[0] = 0; 621 } 622 mutex_exit(&stmf_state.stmf_lock); 623 break; 624 625 case STMF_IOCTL_SET_STMF_STATE: 626 if ((ibuf == NULL) || 627 (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) { 628 ret = EINVAL; 629 break; 630 } 631 ret = stmf_set_stmf_state((stmf_state_desc_t *)ibuf); 632 break; 633 634 case STMF_IOCTL_GET_STMF_STATE: 635 if ((obuf == NULL) || 636 (iocd->stmf_obuf_size < sizeof (stmf_state_desc_t))) { 637 ret = EINVAL; 638 break; 639 } 640 ret = stmf_get_stmf_state((stmf_state_desc_t *)obuf); 641 break; 642 643 case STMF_IOCTL_SET_LU_STATE: 644 ssi.st_rflags = STMF_RFLAG_USER_REQUEST; 645 ssi.st_additional_info = NULL; 646 std = (stmf_state_desc_t *)ibuf; 647 if ((ibuf == NULL) || 648 (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) { 649 ret = EINVAL; 650 break; 651 } 652 p_id = std->ident; 653 mutex_enter(&stmf_state.stmf_lock); 654 if (stmf_state.stmf_inventory_locked) { 655 mutex_exit(&stmf_state.stmf_lock); 656 ret = EBUSY; 657 break; 658 } 659 for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) { 660 if (bcmp(p_id, ilu->ilu_lu->lu_id->ident, 16) == 0) 661 break; 662 } 663 if (ilu == NULL) { 664 mutex_exit(&stmf_state.stmf_lock); 665 ret = ENOENT; 666 break; 667 } 668 stmf_state.stmf_inventory_locked = 1; 669 mutex_exit(&stmf_state.stmf_lock); 670 cmd = (std->state == STMF_STATE_ONLINE) ? STMF_CMD_LU_ONLINE : 671 STMF_CMD_LU_OFFLINE; 672 ctl_ret = stmf_ctl(cmd, (void *)ilu->ilu_lu, &ssi); 673 if (ctl_ret == STMF_ALREADY) 674 ret = 0; 675 else if (ctl_ret != STMF_SUCCESS) 676 ret = EIO; 677 mutex_enter(&stmf_state.stmf_lock); 678 stmf_state.stmf_inventory_locked = 0; 679 mutex_exit(&stmf_state.stmf_lock); 680 break; 681 682 case STMF_IOCTL_SET_TARGET_PORT_STATE: 683 ssi.st_rflags = STMF_RFLAG_USER_REQUEST; 684 ssi.st_additional_info = NULL; 685 std = (stmf_state_desc_t *)ibuf; 686 if ((ibuf == NULL) || 687 (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) { 688 ret = EINVAL; 689 break; 690 } 691 p_id = std->ident; 692 mutex_enter(&stmf_state.stmf_lock); 693 if (stmf_state.stmf_inventory_locked) { 694 mutex_exit(&stmf_state.stmf_lock); 695 ret = EBUSY; 696 break; 697 } 698 for (ilport = stmf_state.stmf_ilportlist; ilport; 699 ilport = ilport->ilport_next) { 700 uint8_t *id; 701 id = (uint8_t *)ilport->ilport_lport->lport_id; 702 if ((id[3] == p_id[3]) && 703 (bcmp(id+4, p_id+4, id[3]) == 0)) { 704 break; 705 } 706 } 707 if (ilport == NULL) { 708 mutex_exit(&stmf_state.stmf_lock); 709 ret = ENOENT; 710 break; 711 } 712 stmf_state.stmf_inventory_locked = 1; 713 mutex_exit(&stmf_state.stmf_lock); 714 cmd = (std->state == STMF_STATE_ONLINE) ? 715 STMF_CMD_LPORT_ONLINE : STMF_CMD_LPORT_OFFLINE; 716 ctl_ret = stmf_ctl(cmd, (void *)ilport->ilport_lport, &ssi); 717 if (ctl_ret == STMF_ALREADY) 718 ret = 0; 719 else if (ctl_ret != STMF_SUCCESS) 720 ret = EIO; 721 mutex_enter(&stmf_state.stmf_lock); 722 stmf_state.stmf_inventory_locked = 0; 723 mutex_exit(&stmf_state.stmf_lock); 724 break; 725 726 case STMF_IOCTL_ADD_HG_ENTRY: 727 idtype = STMF_ID_TYPE_HOST; 728 /* FALLTHROUGH */ 729 case STMF_IOCTL_ADD_TG_ENTRY: 730 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 731 ret = EACCES; 732 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 733 break; 734 } 735 if (cmd == STMF_IOCTL_ADD_TG_ENTRY) { 736 idtype = STMF_ID_TYPE_TARGET; 737 } 738 grp_entry = (stmf_group_op_data_t *)ibuf; 739 if ((ibuf == NULL) || 740 (iocd->stmf_ibuf_size < sizeof (stmf_group_op_data_t))) { 741 ret = EINVAL; 742 break; 743 } 744 if (grp_entry->group.name[0] == '*') { 745 ret = EINVAL; 746 break; /* not allowed */ 747 } 748 mutex_enter(&stmf_state.stmf_lock); 749 if (idtype == STMF_ID_TYPE_TARGET && 750 stmf_state.stmf_service_running) { 751 mutex_exit(&stmf_state.stmf_lock); 752 iocd->stmf_error = 753 STMF_IOCERR_TG_UPDATE_NEED_SVC_OFFLINE; 754 ret = EBUSY; 755 break; /* not allowed */ 756 } 757 ret = stmf_add_group_member(grp_entry->group.name, 758 grp_entry->group.name_size, 759 grp_entry->ident + 4, 760 grp_entry->ident[3], 761 idtype, 762 &iocd->stmf_error); 763 mutex_exit(&stmf_state.stmf_lock); 764 break; 765 case STMF_IOCTL_REMOVE_HG_ENTRY: 766 idtype = STMF_ID_TYPE_HOST; 767 /* FALLTHROUGH */ 768 case STMF_IOCTL_REMOVE_TG_ENTRY: 769 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 770 ret = EACCES; 771 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 772 break; 773 } 774 if (cmd == STMF_IOCTL_REMOVE_TG_ENTRY) { 775 idtype = STMF_ID_TYPE_TARGET; 776 } 777 grp_entry = (stmf_group_op_data_t *)ibuf; 778 if ((ibuf == NULL) || 779 (iocd->stmf_ibuf_size < sizeof (stmf_group_op_data_t))) { 780 ret = EINVAL; 781 break; 782 } 783 if (grp_entry->group.name[0] == '*') { 784 ret = EINVAL; 785 break; /* not allowed */ 786 } 787 mutex_enter(&stmf_state.stmf_lock); 788 if (idtype == STMF_ID_TYPE_TARGET && 789 stmf_state.stmf_service_running) { 790 mutex_exit(&stmf_state.stmf_lock); 791 iocd->stmf_error = 792 STMF_IOCERR_TG_UPDATE_NEED_SVC_OFFLINE; 793 ret = EBUSY; 794 break; /* not allowed */ 795 } 796 ret = stmf_remove_group_member(grp_entry->group.name, 797 grp_entry->group.name_size, 798 grp_entry->ident + 4, 799 grp_entry->ident[3], 800 idtype, 801 &iocd->stmf_error); 802 mutex_exit(&stmf_state.stmf_lock); 803 break; 804 case STMF_IOCTL_CREATE_HOST_GROUP: 805 idtype = STMF_ID_TYPE_HOST_GROUP; 806 /* FALLTHROUGH */ 807 case STMF_IOCTL_CREATE_TARGET_GROUP: 808 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 809 ret = EACCES; 810 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 811 break; 812 } 813 grpname = (stmf_group_name_t *)ibuf; 814 815 if (cmd == STMF_IOCTL_CREATE_TARGET_GROUP) 816 idtype = STMF_ID_TYPE_TARGET_GROUP; 817 if ((ibuf == NULL) || 818 (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) { 819 ret = EINVAL; 820 break; 821 } 822 if (grpname->name[0] == '*') { 823 ret = EINVAL; 824 break; /* not allowed */ 825 } 826 mutex_enter(&stmf_state.stmf_lock); 827 ret = stmf_add_group(grpname->name, 828 grpname->name_size, idtype, &iocd->stmf_error); 829 mutex_exit(&stmf_state.stmf_lock); 830 break; 831 case STMF_IOCTL_REMOVE_HOST_GROUP: 832 idtype = STMF_ID_TYPE_HOST_GROUP; 833 /* FALLTHROUGH */ 834 case STMF_IOCTL_REMOVE_TARGET_GROUP: 835 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 836 ret = EACCES; 837 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 838 break; 839 } 840 grpname = (stmf_group_name_t *)ibuf; 841 if (cmd == STMF_IOCTL_REMOVE_TARGET_GROUP) 842 idtype = STMF_ID_TYPE_TARGET_GROUP; 843 if ((ibuf == NULL) || 844 (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) { 845 ret = EINVAL; 846 break; 847 } 848 if (grpname->name[0] == '*') { 849 ret = EINVAL; 850 break; /* not allowed */ 851 } 852 mutex_enter(&stmf_state.stmf_lock); 853 ret = stmf_remove_group(grpname->name, 854 grpname->name_size, idtype, &iocd->stmf_error); 855 mutex_exit(&stmf_state.stmf_lock); 856 break; 857 case STMF_IOCTL_ADD_VIEW_ENTRY: 858 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 859 ret = EACCES; 860 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 861 break; 862 } 863 ve = (stmf_view_op_entry_t *)ibuf; 864 if ((ibuf == NULL) || 865 (iocd->stmf_ibuf_size < sizeof (stmf_view_op_entry_t))) { 866 ret = EINVAL; 867 break; 868 } 869 if (!ve->ve_lu_number_valid) 870 ve->ve_lu_nbr[2] = 0xFF; 871 if (ve->ve_all_hosts) { 872 ve->ve_host_group.name[0] = '*'; 873 ve->ve_host_group.name_size = 1; 874 } 875 if (ve->ve_all_targets) { 876 ve->ve_target_group.name[0] = '*'; 877 ve->ve_target_group.name_size = 1; 878 } 879 if (ve->ve_ndx_valid) 880 veid = ve->ve_ndx; 881 else 882 veid = 0xffffffff; 883 mutex_enter(&stmf_state.stmf_lock); 884 ret = stmf_add_ve(ve->ve_host_group.name, 885 ve->ve_host_group.name_size, 886 ve->ve_target_group.name, 887 ve->ve_target_group.name_size, 888 ve->ve_guid, 889 &veid, 890 ve->ve_lu_nbr, 891 &iocd->stmf_error); 892 mutex_exit(&stmf_state.stmf_lock); 893 if (ret == 0 && 894 (!ve->ve_ndx_valid || !ve->ve_lu_number_valid) && 895 iocd->stmf_obuf_size >= sizeof (stmf_view_op_entry_t)) { 896 stmf_view_op_entry_t *ve_ret = 897 (stmf_view_op_entry_t *)obuf; 898 iocd->stmf_obuf_nentries = 1; 899 iocd->stmf_obuf_max_nentries = 1; 900 if (!ve->ve_ndx_valid) { 901 ve_ret->ve_ndx = veid; 902 ve_ret->ve_ndx_valid = 1; 903 } 904 if (!ve->ve_lu_number_valid) { 905 ve_ret->ve_lu_number_valid = 1; 906 bcopy(ve->ve_lu_nbr, ve_ret->ve_lu_nbr, 8); 907 } 908 } 909 break; 910 case STMF_IOCTL_REMOVE_VIEW_ENTRY: 911 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 912 ret = EACCES; 913 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 914 break; 915 } 916 ve = (stmf_view_op_entry_t *)ibuf; 917 if ((ibuf == NULL) || 918 (iocd->stmf_ibuf_size < sizeof (stmf_view_op_entry_t))) { 919 ret = EINVAL; 920 break; 921 } 922 if (!ve->ve_ndx_valid) { 923 ret = EINVAL; 924 break; 925 } 926 mutex_enter(&stmf_state.stmf_lock); 927 ret = stmf_remove_ve_by_id(ve->ve_guid, ve->ve_ndx, 928 &iocd->stmf_error); 929 mutex_exit(&stmf_state.stmf_lock); 930 break; 931 #if 0 932 case STMF_IOCTL_GET_HG_LIST: 933 id_list = &stmf_state.stmf_hg_list; 934 /* FALLTHROUGH */ 935 case STMF_IOCTL_GET_TG_LIST: 936 if (cmd == STMF_IOCTL_GET_TG_LIST) 937 id_list = &stmf_state.stmf_tg_list; 938 mutex_enter(&stmf_state.stmf_lock); 939 iocd->stmf_obuf_max_nentries = id_list->id_count; 940 n = min(id_list->id_count, 941 (iocd->stmf_obuf_size)/sizeof (stmf_group_name_t)); 942 iocd->stmf_obuf_nentries = n; 943 id_entry = id_list->idl_head; 944 grpname = (stmf_group_name_t *)obuf; 945 for (i = 0; i < n; i++) { 946 grpname[i].name_size = id_entry->id_data_size; 947 bcopy(id_entry->id_data, grpname[i].name, 948 id_entry->id_data_size); 949 id_entry = id_entry->id_next; 950 } 951 mutex_exit(&stmf_state.stmf_lock); 952 break; 953 case STMF_IOCTL_GET_HG_ENTRIES: 954 id_list = &stmf_state.stmf_hg_list; 955 /* FALLTHROUGH */ 956 case STMF_IOCTL_GET_TG_ENTRIES: 957 grpname = (stmf_group_name_t *)ibuf; 958 if ((ibuf == NULL) || 959 (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) { 960 ret = EINVAL; 961 break; 962 } 963 if (cmd == STMF_IOCTL_GET_TG_ENTRIES) { 964 id_list = &stmf_state.stmf_tg_list; 965 } 966 mutex_enter(&stmf_state.stmf_lock); 967 id_entry = stmf_lookup_id(id_list, grpname->name_size, 968 grpname->name); 969 if (!id_entry) 970 ret = ENODEV; 971 else { 972 stmf_ge_ident_t *grp_entry; 973 id_list = (stmf_id_list_t *)id_entry->id_impl_specific; 974 iocd->stmf_obuf_max_nentries = id_list->id_count; 975 n = min(id_list->id_count, 976 iocd->stmf_obuf_size/sizeof (stmf_ge_ident_t)); 977 iocd->stmf_obuf_nentries = n; 978 id_entry = id_list->idl_head; 979 grp_entry = (stmf_ge_ident_t *)obuf; 980 for (i = 0; i < n; i++) { 981 bcopy(id_entry->id_data, grp_entry, 982 id_entry->id_data_size); 983 id_entry = id_entry->id_next; 984 } 985 } 986 mutex_exit(&stmf_state.stmf_lock); 987 break; 988 case STMF_IOCTL_GET_VE_LIST: 989 n = iocd->stmf_obuf_size/sizeof (stmf_view_op_entry_t); 990 mutex_enter(&stmf_state.stmf_lock); 991 id_entry = stmf_state.stmf_luid_list.idl_head; 992 ve = (stmf_view_op_entry_t *)obuf; 993 while (id_entry) { 994 view_entry = 995 (stmf_view_entry_t *)id_entry->id_impl_specific; 996 for (; view_entry; view_entry = view_entry->ve_next) { 997 ve->ve_ndx_valid = 1; 998 ve->ve_ndx = view_entry->ve_id; 999 ve->ve_lu_number_valid = 1; 1000 bcopy(view_entry->ve_lun, ve->ve_lu_nbr, 8); 1001 bcopy(view_entry->ve_luid->id_data, ve->ve_guid, 1002 view_entry->ve_luid->id_data_size); 1003 if (view_entry->ve_hg->id_data[0] == '*') 1004 ve->ve_all_hosts = 1; 1005 else 1006 bcopy(view_entry->ve_hg->id_data, 1007 ve->ve_host_group.name, 1008 view_entry->ve_hg->id_data_size); 1009 if (view_entry->ve_tg->id_data[0] == '*') 1010 ve->ve_all_targets = 1; 1011 else 1012 bcopy(view_entry->ve_tg->id_data, 1013 ve->ve_target_group.name, 1014 view_entry->ve_tg->id_data_size); 1015 iocd->stmf_obuf_nentries++; 1016 if (iocd->stmf_obuf_nentries >= n) 1017 break; 1018 } 1019 if (iocd->stmf_obuf_nentries >= n) 1020 break; 1021 } 1022 mutex_exit(&stmf_state.stmf_lock); 1023 break; 1024 #endif 1025 case STMF_IOCTL_LOAD_PP_DATA: 1026 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 1027 ret = EACCES; 1028 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 1029 break; 1030 } 1031 ppi = (stmf_ppioctl_data_t *)ibuf; 1032 if ((ppi == NULL) || 1033 (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) { 1034 ret = EINVAL; 1035 break; 1036 } 1037 ret = stmf_load_ppd_ioctl(ppi); 1038 break; 1039 1040 case STMF_IOCTL_CLEAR_PP_DATA: 1041 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) { 1042 ret = EACCES; 1043 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT; 1044 break; 1045 } 1046 ppi = (stmf_ppioctl_data_t *)ibuf; 1047 if ((ppi == NULL) || 1048 (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) { 1049 ret = EINVAL; 1050 break; 1051 } 1052 ret = stmf_delete_ppd_ioctl(ppi); 1053 break; 1054 1055 case STMF_IOCTL_CLEAR_TRACE: 1056 stmf_trace_clear(); 1057 break; 1058 1059 case STMF_IOCTL_ADD_TRACE: 1060 if (iocd->stmf_ibuf_size && ibuf) { 1061 ((uint8_t *)ibuf)[iocd->stmf_ibuf_size - 1] = 0; 1062 stmf_trace("\nstradm", "%s\n", ibuf); 1063 } 1064 break; 1065 1066 case STMF_IOCTL_GET_TRACE_POSITION: 1067 if (obuf && (iocd->stmf_obuf_size > 3)) { 1068 mutex_enter(&trace_buf_lock); 1069 *((int *)obuf) = trace_buf_curndx; 1070 mutex_exit(&trace_buf_lock); 1071 } else { 1072 ret = EINVAL; 1073 } 1074 break; 1075 1076 case STMF_IOCTL_GET_TRACE: 1077 if ((iocd->stmf_obuf_size == 0) || (iocd->stmf_ibuf_size < 4)) { 1078 ret = EINVAL; 1079 break; 1080 } 1081 i = *((int *)ibuf); 1082 if ((i > trace_buf_size) || ((i + iocd->stmf_obuf_size) > 1083 trace_buf_size)) { 1084 ret = EINVAL; 1085 break; 1086 } 1087 mutex_enter(&trace_buf_lock); 1088 bcopy(stmf_trace_buf + i, obuf, iocd->stmf_obuf_size); 1089 mutex_exit(&trace_buf_lock); 1090 break; 1091 1092 default: 1093 ret = ENOTTY; 1094 } 1095 1096 if (ret == 0) { 1097 ret = stmf_copyout_iocdata(data, mode, iocd, obuf); 1098 } else if (iocd->stmf_error) { 1099 (void) stmf_copyout_iocdata(data, mode, iocd, obuf); 1100 } 1101 if (obuf) { 1102 kmem_free(obuf, iocd->stmf_obuf_size); 1103 obuf = NULL; 1104 } 1105 if (ibuf) { 1106 kmem_free(ibuf, iocd->stmf_ibuf_size); 1107 ibuf = NULL; 1108 } 1109 kmem_free(iocd, sizeof (stmf_iocdata_t)); 1110 return (ret); 1111 } 1112 1113 static int 1114 stmf_get_service_state() 1115 { 1116 stmf_i_local_port_t *ilport; 1117 stmf_i_lu_t *ilu; 1118 int online = 0; 1119 int offline = 0; 1120 int onlining = 0; 1121 int offlining = 0; 1122 1123 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 1124 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 1125 ilport = ilport->ilport_next) { 1126 if (ilport->ilport_state == STMF_STATE_OFFLINE) 1127 offline++; 1128 else if (ilport->ilport_state == STMF_STATE_ONLINE) 1129 online++; 1130 else if (ilport->ilport_state == STMF_STATE_ONLINING) 1131 onlining++; 1132 else if (ilport->ilport_state == STMF_STATE_OFFLINING) 1133 offlining++; 1134 } 1135 1136 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; 1137 ilu = ilu->ilu_next) { 1138 if (ilu->ilu_state == STMF_STATE_OFFLINE) 1139 offline++; 1140 else if (ilu->ilu_state == STMF_STATE_ONLINE) 1141 online++; 1142 else if (ilu->ilu_state == STMF_STATE_ONLINING) 1143 onlining++; 1144 else if (ilu->ilu_state == STMF_STATE_OFFLINING) 1145 offlining++; 1146 } 1147 1148 if (stmf_state.stmf_service_running) { 1149 if (onlining) 1150 return (STMF_STATE_ONLINING); 1151 else 1152 return (STMF_STATE_ONLINE); 1153 } 1154 1155 if (offlining) { 1156 return (STMF_STATE_OFFLINING); 1157 } 1158 1159 return (STMF_STATE_OFFLINE); 1160 } 1161 1162 static int 1163 stmf_set_stmf_state(stmf_state_desc_t *std) 1164 { 1165 stmf_i_local_port_t *ilport; 1166 stmf_i_lu_t *ilu; 1167 stmf_state_change_info_t ssi; 1168 int svc_state; 1169 1170 ssi.st_rflags = STMF_RFLAG_USER_REQUEST; 1171 ssi.st_additional_info = NULL; 1172 1173 mutex_enter(&stmf_state.stmf_lock); 1174 if (!stmf_state.stmf_exclusive_open) { 1175 mutex_exit(&stmf_state.stmf_lock); 1176 return (EACCES); 1177 } 1178 1179 if (stmf_state.stmf_inventory_locked) { 1180 mutex_exit(&stmf_state.stmf_lock); 1181 return (EBUSY); 1182 } 1183 1184 if ((std->state != STMF_STATE_ONLINE) && 1185 (std->state != STMF_STATE_OFFLINE)) { 1186 mutex_exit(&stmf_state.stmf_lock); 1187 return (EINVAL); 1188 } 1189 1190 svc_state = stmf_get_service_state(); 1191 if ((svc_state == STMF_STATE_OFFLINING) || 1192 (svc_state == STMF_STATE_ONLINING)) { 1193 mutex_exit(&stmf_state.stmf_lock); 1194 return (EBUSY); 1195 } 1196 1197 if (svc_state == STMF_STATE_OFFLINE) { 1198 if (std->config_state == STMF_CONFIG_INIT) { 1199 if (std->state != STMF_STATE_OFFLINE) { 1200 mutex_exit(&stmf_state.stmf_lock); 1201 return (EINVAL); 1202 } 1203 stmf_state.stmf_config_state = STMF_CONFIG_INIT; 1204 stmf_delete_all_ppds(); 1205 stmf_view_clear_config(); 1206 stmf_view_init(); 1207 mutex_exit(&stmf_state.stmf_lock); 1208 return (0); 1209 } 1210 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) { 1211 if (std->config_state != STMF_CONFIG_INIT_DONE) { 1212 mutex_exit(&stmf_state.stmf_lock); 1213 return (EINVAL); 1214 } 1215 stmf_state.stmf_config_state = STMF_CONFIG_INIT_DONE; 1216 } 1217 if (std->state == STMF_STATE_OFFLINE) { 1218 mutex_exit(&stmf_state.stmf_lock); 1219 return (0); 1220 } 1221 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) { 1222 mutex_exit(&stmf_state.stmf_lock); 1223 return (EINVAL); 1224 } 1225 stmf_state.stmf_inventory_locked = 1; 1226 stmf_state.stmf_service_running = 1; 1227 mutex_exit(&stmf_state.stmf_lock); 1228 1229 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 1230 ilport = ilport->ilport_next) { 1231 if (ilport->ilport_prev_state != STMF_STATE_ONLINE) 1232 continue; 1233 (void) stmf_ctl(STMF_CMD_LPORT_ONLINE, 1234 ilport->ilport_lport, &ssi); 1235 } 1236 1237 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; 1238 ilu = ilu->ilu_next) { 1239 if (ilu->ilu_prev_state != STMF_STATE_ONLINE) 1240 continue; 1241 (void) stmf_ctl(STMF_CMD_LU_ONLINE, ilu->ilu_lu, &ssi); 1242 } 1243 mutex_enter(&stmf_state.stmf_lock); 1244 stmf_state.stmf_inventory_locked = 0; 1245 mutex_exit(&stmf_state.stmf_lock); 1246 return (0); 1247 } 1248 1249 /* svc_state is STMF_STATE_ONLINE here */ 1250 if ((std->state != STMF_STATE_OFFLINE) || 1251 (std->config_state == STMF_CONFIG_INIT)) { 1252 mutex_exit(&stmf_state.stmf_lock); 1253 return (EACCES); 1254 } 1255 1256 stmf_state.stmf_inventory_locked = 1; 1257 stmf_state.stmf_service_running = 0; 1258 stmf_delete_all_ppds(); 1259 mutex_exit(&stmf_state.stmf_lock); 1260 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 1261 ilport = ilport->ilport_next) { 1262 if (ilport->ilport_state != STMF_STATE_ONLINE) 1263 continue; 1264 (void) stmf_ctl(STMF_CMD_LPORT_OFFLINE, 1265 ilport->ilport_lport, &ssi); 1266 } 1267 1268 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; 1269 ilu = ilu->ilu_next) { 1270 if (ilu->ilu_state != STMF_STATE_ONLINE) 1271 continue; 1272 (void) stmf_ctl(STMF_CMD_LU_OFFLINE, ilu->ilu_lu, &ssi); 1273 } 1274 mutex_enter(&stmf_state.stmf_lock); 1275 stmf_state.stmf_inventory_locked = 0; 1276 mutex_exit(&stmf_state.stmf_lock); 1277 return (0); 1278 } 1279 1280 static int 1281 stmf_get_stmf_state(stmf_state_desc_t *std) 1282 { 1283 mutex_enter(&stmf_state.stmf_lock); 1284 std->state = stmf_get_service_state(); 1285 std->config_state = stmf_state.stmf_config_state; 1286 mutex_exit(&stmf_state.stmf_lock); 1287 1288 return (0); 1289 } 1290 1291 typedef struct { 1292 void *bp; /* back pointer from internal struct to main struct */ 1293 int alloc_size; 1294 } __istmf_t; 1295 1296 typedef struct { 1297 __istmf_t *fp; /* Framework private */ 1298 void *cp; /* Caller private */ 1299 void *ss; /* struct specific */ 1300 } __stmf_t; 1301 1302 static struct { 1303 int shared; 1304 int fw_private; 1305 } stmf_sizes[] = { { 0, 0 }, 1306 { GET_STRUCT_SIZE(stmf_lu_provider_t), 1307 GET_STRUCT_SIZE(stmf_i_lu_provider_t) }, 1308 { GET_STRUCT_SIZE(stmf_port_provider_t), 1309 GET_STRUCT_SIZE(stmf_i_port_provider_t) }, 1310 { GET_STRUCT_SIZE(stmf_local_port_t), 1311 GET_STRUCT_SIZE(stmf_i_local_port_t) }, 1312 { GET_STRUCT_SIZE(stmf_lu_t), 1313 GET_STRUCT_SIZE(stmf_i_lu_t) }, 1314 { GET_STRUCT_SIZE(stmf_scsi_session_t), 1315 GET_STRUCT_SIZE(stmf_i_scsi_session_t) }, 1316 { GET_STRUCT_SIZE(scsi_task_t), 1317 GET_STRUCT_SIZE(stmf_i_scsi_task_t) }, 1318 { GET_STRUCT_SIZE(stmf_data_buf_t), 1319 GET_STRUCT_SIZE(__istmf_t) }, 1320 { GET_STRUCT_SIZE(stmf_dbuf_store_t), 1321 GET_STRUCT_SIZE(__istmf_t) } 1322 1323 }; 1324 1325 void * 1326 stmf_alloc(stmf_struct_id_t struct_id, int additional_size, int flags) 1327 { 1328 int stmf_size; 1329 int kmem_flag; 1330 __stmf_t *sh; 1331 1332 if ((struct_id == 0) || (struct_id >= STMF_MAX_STRUCT_IDS)) 1333 return (NULL); 1334 1335 if ((curthread->t_flag & T_INTR_THREAD) || (flags & AF_FORCE_NOSLEEP)) { 1336 kmem_flag = KM_NOSLEEP; 1337 } else { 1338 kmem_flag = KM_SLEEP; 1339 } 1340 1341 additional_size = (additional_size + 7) & (~7); 1342 stmf_size = stmf_sizes[struct_id].shared + 1343 stmf_sizes[struct_id].fw_private + additional_size; 1344 1345 sh = (__stmf_t *)kmem_zalloc(stmf_size, kmem_flag); 1346 1347 if (sh == NULL) 1348 return (NULL); 1349 1350 /* 1351 * In principle, the implementation inside stmf_alloc should not 1352 * be changed anyway. But the original order of framework private 1353 * data and caller private data does not support sglist in the caller 1354 * private data. 1355 * To work around this, the memory segments of framework private 1356 * data and caller private data are re-ordered here. 1357 * A better solution is to provide a specific interface to allocate 1358 * the sglist, then we will not need this workaround any more. 1359 * But before the new interface is available, the memory segment 1360 * ordering should be kept as is. 1361 */ 1362 sh->cp = GET_BYTE_OFFSET(sh, stmf_sizes[struct_id].shared); 1363 sh->fp = (__istmf_t *)GET_BYTE_OFFSET(sh, 1364 stmf_sizes[struct_id].shared + additional_size); 1365 1366 sh->fp->bp = sh; 1367 /* Just store the total size instead of storing additional size */ 1368 sh->fp->alloc_size = stmf_size; 1369 1370 return (sh); 1371 } 1372 1373 void 1374 stmf_free(void *ptr) 1375 { 1376 __stmf_t *sh = (__stmf_t *)ptr; 1377 1378 /* 1379 * So far we dont need any struct specific processing. If such 1380 * a need ever arises, then store the struct id in the framework 1381 * private section and get it here as sh->fp->struct_id. 1382 */ 1383 kmem_free(ptr, sh->fp->alloc_size); 1384 } 1385 1386 /* 1387 * Given a pointer to stmf_lu_t, verifies if this lu is registered with the 1388 * framework and returns a pointer to framework private data for the lu. 1389 * Returns NULL if the lu was not found. 1390 */ 1391 stmf_i_lu_t * 1392 stmf_lookup_lu(stmf_lu_t *lu) 1393 { 1394 stmf_i_lu_t *ilu; 1395 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 1396 1397 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) { 1398 if (ilu->ilu_lu == lu) 1399 return (ilu); 1400 } 1401 return (NULL); 1402 } 1403 1404 /* 1405 * Given a pointer to stmf_local_port_t, verifies if this lport is registered 1406 * with the framework and returns a pointer to framework private data for 1407 * the lport. 1408 * Returns NULL if the lport was not found. 1409 */ 1410 stmf_i_local_port_t * 1411 stmf_lookup_lport(stmf_local_port_t *lport) 1412 { 1413 stmf_i_local_port_t *ilport; 1414 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 1415 1416 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 1417 ilport = ilport->ilport_next) { 1418 if (ilport->ilport_lport == lport) 1419 return (ilport); 1420 } 1421 return (NULL); 1422 } 1423 1424 stmf_status_t 1425 stmf_register_lu_provider(stmf_lu_provider_t *lp) 1426 { 1427 stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private; 1428 stmf_pp_data_t *ppd; 1429 uint32_t cb_flags; 1430 1431 if (lp->lp_lpif_rev != LPIF_REV_1) 1432 return (STMF_FAILURE); 1433 1434 mutex_enter(&stmf_state.stmf_lock); 1435 ilp->ilp_next = stmf_state.stmf_ilplist; 1436 stmf_state.stmf_ilplist = ilp; 1437 stmf_state.stmf_nlps++; 1438 1439 /* See if we need to do a callback */ 1440 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) { 1441 if (strcmp(ppd->ppd_name, lp->lp_name) == 0) { 1442 break; 1443 } 1444 } 1445 if ((ppd == NULL) || (ppd->ppd_nv == NULL)) { 1446 goto rlp_bail_out; 1447 } 1448 ilp->ilp_ppd = ppd; 1449 ppd->ppd_provider = ilp; 1450 if (lp->lp_cb == NULL) 1451 goto rlp_bail_out; 1452 ilp->ilp_cb_in_progress = 1; 1453 cb_flags = STMF_PCB_PREG_COMPLETE; 1454 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) 1455 cb_flags |= STMF_PCB_STMF_ONLINING; 1456 mutex_exit(&stmf_state.stmf_lock); 1457 lp->lp_cb(lp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags); 1458 mutex_enter(&stmf_state.stmf_lock); 1459 ilp->ilp_cb_in_progress = 0; 1460 1461 rlp_bail_out: 1462 mutex_exit(&stmf_state.stmf_lock); 1463 1464 return (STMF_SUCCESS); 1465 } 1466 1467 stmf_status_t 1468 stmf_deregister_lu_provider(stmf_lu_provider_t *lp) 1469 { 1470 stmf_i_lu_provider_t **ppilp; 1471 stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private; 1472 1473 mutex_enter(&stmf_state.stmf_lock); 1474 if (ilp->ilp_nlus || ilp->ilp_cb_in_progress) { 1475 mutex_exit(&stmf_state.stmf_lock); 1476 return (STMF_BUSY); 1477 } 1478 for (ppilp = &stmf_state.stmf_ilplist; *ppilp != NULL; 1479 ppilp = &((*ppilp)->ilp_next)) { 1480 if (*ppilp == ilp) { 1481 *ppilp = ilp->ilp_next; 1482 stmf_state.stmf_nlps--; 1483 if (ilp->ilp_ppd) { 1484 ilp->ilp_ppd->ppd_provider = NULL; 1485 ilp->ilp_ppd = NULL; 1486 } 1487 mutex_exit(&stmf_state.stmf_lock); 1488 return (STMF_SUCCESS); 1489 } 1490 } 1491 mutex_exit(&stmf_state.stmf_lock); 1492 return (STMF_NOT_FOUND); 1493 } 1494 1495 stmf_status_t 1496 stmf_register_port_provider(stmf_port_provider_t *pp) 1497 { 1498 stmf_i_port_provider_t *ipp = 1499 (stmf_i_port_provider_t *)pp->pp_stmf_private; 1500 stmf_pp_data_t *ppd; 1501 uint32_t cb_flags; 1502 1503 if (pp->pp_portif_rev != PORTIF_REV_1) 1504 return (STMF_FAILURE); 1505 1506 mutex_enter(&stmf_state.stmf_lock); 1507 ipp->ipp_next = stmf_state.stmf_ipplist; 1508 stmf_state.stmf_ipplist = ipp; 1509 stmf_state.stmf_npps++; 1510 /* See if we need to do a callback */ 1511 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) { 1512 if (strcmp(ppd->ppd_name, pp->pp_name) == 0) { 1513 break; 1514 } 1515 } 1516 if ((ppd == NULL) || (ppd->ppd_nv == NULL)) { 1517 goto rpp_bail_out; 1518 } 1519 ipp->ipp_ppd = ppd; 1520 ppd->ppd_provider = ipp; 1521 if (pp->pp_cb == NULL) 1522 goto rpp_bail_out; 1523 ipp->ipp_cb_in_progress = 1; 1524 cb_flags = STMF_PCB_PREG_COMPLETE; 1525 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) 1526 cb_flags |= STMF_PCB_STMF_ONLINING; 1527 mutex_exit(&stmf_state.stmf_lock); 1528 pp->pp_cb(pp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags); 1529 mutex_enter(&stmf_state.stmf_lock); 1530 ipp->ipp_cb_in_progress = 0; 1531 1532 rpp_bail_out: 1533 mutex_exit(&stmf_state.stmf_lock); 1534 1535 return (STMF_SUCCESS); 1536 } 1537 1538 stmf_status_t 1539 stmf_deregister_port_provider(stmf_port_provider_t *pp) 1540 { 1541 stmf_i_port_provider_t *ipp = 1542 (stmf_i_port_provider_t *)pp->pp_stmf_private; 1543 stmf_i_port_provider_t **ppipp; 1544 1545 mutex_enter(&stmf_state.stmf_lock); 1546 if (ipp->ipp_npps || ipp->ipp_cb_in_progress) { 1547 mutex_exit(&stmf_state.stmf_lock); 1548 return (STMF_BUSY); 1549 } 1550 for (ppipp = &stmf_state.stmf_ipplist; *ppipp != NULL; 1551 ppipp = &((*ppipp)->ipp_next)) { 1552 if (*ppipp == ipp) { 1553 *ppipp = ipp->ipp_next; 1554 stmf_state.stmf_npps--; 1555 if (ipp->ipp_ppd) { 1556 ipp->ipp_ppd->ppd_provider = NULL; 1557 ipp->ipp_ppd = NULL; 1558 } 1559 mutex_exit(&stmf_state.stmf_lock); 1560 return (STMF_SUCCESS); 1561 } 1562 } 1563 mutex_exit(&stmf_state.stmf_lock); 1564 return (STMF_NOT_FOUND); 1565 } 1566 1567 int 1568 stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi) 1569 { 1570 stmf_i_port_provider_t *ipp; 1571 stmf_i_lu_provider_t *ilp; 1572 stmf_pp_data_t *ppd; 1573 nvlist_t *nv; 1574 int s; 1575 int ret; 1576 1577 if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) { 1578 return (EINVAL); 1579 } 1580 1581 mutex_enter(&stmf_state.stmf_lock); 1582 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) { 1583 if (ppi->ppi_lu_provider) { 1584 if (!ppd->ppd_lu_provider) 1585 continue; 1586 } else if (ppi->ppi_port_provider) { 1587 if (!ppd->ppd_port_provider) 1588 continue; 1589 } 1590 if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0) 1591 break; 1592 } 1593 1594 if (ppd == NULL) { 1595 /* New provider */ 1596 s = strlen(ppi->ppi_name); 1597 if (s > 254) { 1598 mutex_exit(&stmf_state.stmf_lock); 1599 return (EINVAL); 1600 } 1601 s += sizeof (stmf_pp_data_t) - 7; 1602 1603 ppd = kmem_zalloc(s, KM_NOSLEEP); 1604 if (ppd == NULL) { 1605 mutex_exit(&stmf_state.stmf_lock); 1606 return (ENOMEM); 1607 } 1608 ppd->ppd_alloc_size = s; 1609 (void) strcpy(ppd->ppd_name, ppi->ppi_name); 1610 1611 /* See if this provider already exists */ 1612 if (ppi->ppi_lu_provider) { 1613 ppd->ppd_lu_provider = 1; 1614 for (ilp = stmf_state.stmf_ilplist; ilp != NULL; 1615 ilp = ilp->ilp_next) { 1616 if (strcmp(ppi->ppi_name, 1617 ilp->ilp_lp->lp_name) == 0) { 1618 ppd->ppd_provider = ilp; 1619 ilp->ilp_ppd = ppd; 1620 break; 1621 } 1622 } 1623 } else { 1624 ppd->ppd_port_provider = 1; 1625 for (ipp = stmf_state.stmf_ipplist; ipp != NULL; 1626 ipp = ipp->ipp_next) { 1627 if (strcmp(ppi->ppi_name, 1628 ipp->ipp_pp->pp_name) == 0) { 1629 ppd->ppd_provider = ipp; 1630 ipp->ipp_ppd = ppd; 1631 break; 1632 } 1633 } 1634 } 1635 1636 /* Link this ppd in */ 1637 ppd->ppd_next = stmf_state.stmf_ppdlist; 1638 stmf_state.stmf_ppdlist = ppd; 1639 } 1640 1641 if ((ret = nvlist_unpack((char *)ppi->ppi_data, 1642 (size_t)ppi->ppi_data_size, &nv, KM_NOSLEEP)) != 0) { 1643 mutex_exit(&stmf_state.stmf_lock); 1644 return (ret); 1645 } 1646 1647 /* Free any existing lists and add this one to the ppd */ 1648 if (ppd->ppd_nv) 1649 nvlist_free(ppd->ppd_nv); 1650 ppd->ppd_nv = nv; 1651 1652 /* If there is a provider registered, do the notifications */ 1653 if (ppd->ppd_provider) { 1654 uint32_t cb_flags = 0; 1655 1656 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) 1657 cb_flags |= STMF_PCB_STMF_ONLINING; 1658 if (ppi->ppi_lu_provider) { 1659 ilp = (stmf_i_lu_provider_t *)ppd->ppd_provider; 1660 if (ilp->ilp_lp->lp_cb == NULL) 1661 goto bail_out; 1662 ilp->ilp_cb_in_progress = 1; 1663 mutex_exit(&stmf_state.stmf_lock); 1664 ilp->ilp_lp->lp_cb(ilp->ilp_lp, 1665 STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags); 1666 mutex_enter(&stmf_state.stmf_lock); 1667 ilp->ilp_cb_in_progress = 0; 1668 } else { 1669 ipp = (stmf_i_port_provider_t *)ppd->ppd_provider; 1670 if (ipp->ipp_pp->pp_cb == NULL) 1671 goto bail_out; 1672 ipp->ipp_cb_in_progress = 1; 1673 mutex_exit(&stmf_state.stmf_lock); 1674 ipp->ipp_pp->pp_cb(ipp->ipp_pp, 1675 STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags); 1676 mutex_enter(&stmf_state.stmf_lock); 1677 ipp->ipp_cb_in_progress = 0; 1678 } 1679 } 1680 1681 bail_out: 1682 mutex_exit(&stmf_state.stmf_lock); 1683 1684 return (0); 1685 } 1686 1687 void 1688 stmf_delete_ppd(stmf_pp_data_t *ppd) 1689 { 1690 stmf_pp_data_t **pppd; 1691 1692 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 1693 if (ppd->ppd_provider) { 1694 if (ppd->ppd_lu_provider) { 1695 ((stmf_i_lu_provider_t *) 1696 ppd->ppd_provider)->ilp_ppd = NULL; 1697 } else { 1698 ((stmf_i_port_provider_t *) 1699 ppd->ppd_provider)->ipp_ppd = NULL; 1700 } 1701 ppd->ppd_provider = NULL; 1702 } 1703 1704 for (pppd = &stmf_state.stmf_ppdlist; *pppd != NULL; 1705 pppd = &((*pppd)->ppd_next)) { 1706 if (*pppd == ppd) 1707 break; 1708 } 1709 1710 if (*pppd == NULL) 1711 return; 1712 1713 *pppd = ppd->ppd_next; 1714 if (ppd->ppd_nv) 1715 nvlist_free(ppd->ppd_nv); 1716 1717 kmem_free(ppd, ppd->ppd_alloc_size); 1718 } 1719 1720 int 1721 stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi) 1722 { 1723 stmf_pp_data_t *ppd; 1724 int ret = ENOENT; 1725 1726 if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) { 1727 return (EINVAL); 1728 } 1729 1730 mutex_enter(&stmf_state.stmf_lock); 1731 1732 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) { 1733 if (ppi->ppi_lu_provider) { 1734 if (!ppd->ppd_lu_provider) 1735 continue; 1736 } else if (ppi->ppi_port_provider) { 1737 if (!ppd->ppd_port_provider) 1738 continue; 1739 } 1740 if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0) 1741 break; 1742 } 1743 1744 if (ppd) { 1745 ret = 0; 1746 stmf_delete_ppd(ppd); 1747 } 1748 mutex_exit(&stmf_state.stmf_lock); 1749 1750 return (ret); 1751 } 1752 1753 void 1754 stmf_delete_all_ppds() 1755 { 1756 stmf_pp_data_t *ppd, *nppd; 1757 1758 ASSERT(mutex_owned(&stmf_state.stmf_lock)); 1759 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = nppd) { 1760 nppd = ppd->ppd_next; 1761 stmf_delete_ppd(ppd); 1762 } 1763 } 1764 1765 /* 1766 * 16 is the max string length of a protocol_ident, increase 1767 * the size if needed. 1768 */ 1769 #define STMF_KSTAT_LU_SZ (STMF_GUID_INPUT + 1 + 256) 1770 #define STMF_KSTAT_TGT_SZ (256 * 2 + 16) 1771 1772 typedef struct stmf_kstat_lu_info { 1773 kstat_named_t i_lun_guid; 1774 kstat_named_t i_lun_alias; 1775 } stmf_kstat_lu_info_t; 1776 1777 typedef struct stmf_kstat_tgt_info { 1778 kstat_named_t i_tgt_name; 1779 kstat_named_t i_tgt_alias; 1780 kstat_named_t i_protocol; 1781 } stmf_kstat_tgt_info_t; 1782 1783 /* 1784 * This array matches the Protocol Identifier in stmf_ioctl.h 1785 */ 1786 char *protocol_ident[PROTOCOL_ANY] = { 1787 "Fibre Channel", 1788 "Parallel SCSI", 1789 "SSA", 1790 "IEEE_1394", 1791 "SRP", 1792 "iSCSI", 1793 "SAS", 1794 "ADT", 1795 "ATAPI", 1796 "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN" 1797 }; 1798 1799 /* 1800 * Update the lun wait/run queue count 1801 */ 1802 static void 1803 stmf_update_kstat_lu_q(scsi_task_t *task, void func()) 1804 { 1805 stmf_i_lu_t *ilu; 1806 kstat_io_t *kip; 1807 1808 if (task->task_lu == dlun0) 1809 return; 1810 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 1811 if (ilu != NULL && ilu->ilu_kstat_io != NULL) { 1812 kip = KSTAT_IO_PTR(ilu->ilu_kstat_io); 1813 if (kip != NULL) { 1814 mutex_enter(ilu->ilu_kstat_io->ks_lock); 1815 func(kip); 1816 mutex_exit(ilu->ilu_kstat_io->ks_lock); 1817 } 1818 } 1819 } 1820 1821 /* 1822 * Update the target(lport) wait/run queue count 1823 */ 1824 static void 1825 stmf_update_kstat_lport_q(scsi_task_t *task, void func()) 1826 { 1827 stmf_i_local_port_t *ilp; 1828 kstat_io_t *kip; 1829 1830 ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private; 1831 if (ilp != NULL && ilp->ilport_kstat_io != NULL) { 1832 kip = KSTAT_IO_PTR(ilp->ilport_kstat_io); 1833 if (kip != NULL) { 1834 mutex_enter(ilp->ilport_kstat_io->ks_lock); 1835 func(kip); 1836 mutex_exit(ilp->ilport_kstat_io->ks_lock); 1837 } 1838 } 1839 } 1840 1841 static void 1842 stmf_update_kstat_lport_io(scsi_task_t *task, stmf_data_buf_t *dbuf) 1843 { 1844 stmf_i_local_port_t *ilp; 1845 kstat_io_t *kip; 1846 1847 ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private; 1848 if (ilp != NULL && ilp->ilport_kstat_io != NULL) { 1849 kip = KSTAT_IO_PTR(ilp->ilport_kstat_io); 1850 if (kip != NULL) { 1851 mutex_enter(ilp->ilport_kstat_io->ks_lock); 1852 STMF_UPDATE_KSTAT_IO(kip, dbuf); 1853 mutex_exit(ilp->ilport_kstat_io->ks_lock); 1854 } 1855 } 1856 } 1857 1858 static void 1859 stmf_update_kstat_lu_io(scsi_task_t *task, stmf_data_buf_t *dbuf) 1860 { 1861 stmf_i_lu_t *ilu; 1862 kstat_io_t *kip; 1863 1864 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 1865 if (ilu != NULL && ilu->ilu_kstat_io != NULL) { 1866 kip = KSTAT_IO_PTR(ilu->ilu_kstat_io); 1867 if (kip != NULL) { 1868 mutex_enter(ilu->ilu_kstat_io->ks_lock); 1869 STMF_UPDATE_KSTAT_IO(kip, dbuf); 1870 mutex_exit(ilu->ilu_kstat_io->ks_lock); 1871 } 1872 } 1873 } 1874 1875 static void 1876 stmf_create_kstat_lu(stmf_i_lu_t *ilu) 1877 { 1878 char ks_nm[KSTAT_STRLEN]; 1879 stmf_kstat_lu_info_t *ks_lu; 1880 1881 /* create kstat lun info */ 1882 ks_lu = (stmf_kstat_lu_info_t *)kmem_zalloc(STMF_KSTAT_LU_SZ, 1883 KM_NOSLEEP); 1884 if (ks_lu == NULL) { 1885 cmn_err(CE_WARN, "STMF: kmem_zalloc failed"); 1886 return; 1887 } 1888 1889 bzero(ks_nm, sizeof (ks_nm)); 1890 (void) sprintf(ks_nm, "stmf_lu_%"PRIxPTR"", (uintptr_t)ilu); 1891 if ((ilu->ilu_kstat_info = kstat_create(STMF_MODULE_NAME, 0, 1892 ks_nm, "misc", KSTAT_TYPE_NAMED, 1893 sizeof (stmf_kstat_lu_info_t) / sizeof (kstat_named_t), 1894 KSTAT_FLAG_VIRTUAL)) == NULL) { 1895 kmem_free(ks_lu, STMF_KSTAT_LU_SZ); 1896 cmn_err(CE_WARN, "STMF: kstat_create lu failed"); 1897 return; 1898 } 1899 1900 ilu->ilu_kstat_info->ks_data_size = STMF_KSTAT_LU_SZ; 1901 ilu->ilu_kstat_info->ks_data = ks_lu; 1902 1903 kstat_named_init(&ks_lu->i_lun_guid, "lun-guid", 1904 KSTAT_DATA_STRING); 1905 kstat_named_init(&ks_lu->i_lun_alias, "lun-alias", 1906 KSTAT_DATA_STRING); 1907 1908 /* convert guid to hex string */ 1909 int i; 1910 uint8_t *p = ilu->ilu_lu->lu_id->ident; 1911 bzero(ilu->ilu_ascii_hex_guid, sizeof (ilu->ilu_ascii_hex_guid)); 1912 for (i = 0; i < STMF_GUID_INPUT / 2; i++) { 1913 (void) sprintf(&ilu->ilu_ascii_hex_guid[i * 2], "%02x", p[i]); 1914 } 1915 kstat_named_setstr(&ks_lu->i_lun_guid, 1916 (const char *)ilu->ilu_ascii_hex_guid); 1917 kstat_named_setstr(&ks_lu->i_lun_alias, 1918 (const char *)ilu->ilu_lu->lu_alias); 1919 kstat_install(ilu->ilu_kstat_info); 1920 1921 /* create kstat lun io */ 1922 bzero(ks_nm, sizeof (ks_nm)); 1923 (void) sprintf(ks_nm, "stmf_lu_io_%"PRIxPTR"", (uintptr_t)ilu); 1924 if ((ilu->ilu_kstat_io = kstat_create(STMF_MODULE_NAME, 0, 1925 ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) { 1926 cmn_err(CE_WARN, "STMF: kstat_create lu_io failed"); 1927 return; 1928 } 1929 mutex_init(&ilu->ilu_kstat_lock, NULL, MUTEX_DRIVER, 0); 1930 ilu->ilu_kstat_io->ks_lock = &ilu->ilu_kstat_lock; 1931 kstat_install(ilu->ilu_kstat_io); 1932 } 1933 1934 static void 1935 stmf_create_kstat_lport(stmf_i_local_port_t *ilport) 1936 { 1937 char ks_nm[KSTAT_STRLEN]; 1938 stmf_kstat_tgt_info_t *ks_tgt; 1939 int id, len; 1940 1941 /* create kstat lport info */ 1942 ks_tgt = (stmf_kstat_tgt_info_t *)kmem_zalloc(STMF_KSTAT_TGT_SZ, 1943 KM_NOSLEEP); 1944 if (ks_tgt == NULL) { 1945 cmn_err(CE_WARN, "STMF: kmem_zalloc failed"); 1946 return; 1947 } 1948 1949 bzero(ks_nm, sizeof (ks_nm)); 1950 (void) sprintf(ks_nm, "stmf_tgt_%"PRIxPTR"", (uintptr_t)ilport); 1951 if ((ilport->ilport_kstat_info = kstat_create(STMF_MODULE_NAME, 1952 0, ks_nm, "misc", KSTAT_TYPE_NAMED, 1953 sizeof (stmf_kstat_tgt_info_t) / sizeof (kstat_named_t), 1954 KSTAT_FLAG_VIRTUAL)) == NULL) { 1955 kmem_free(ks_tgt, STMF_KSTAT_TGT_SZ); 1956 cmn_err(CE_WARN, "STMF: kstat_create target failed"); 1957 return; 1958 } 1959 1960 ilport->ilport_kstat_info->ks_data_size = STMF_KSTAT_TGT_SZ; 1961 ilport->ilport_kstat_info->ks_data = ks_tgt; 1962 1963 kstat_named_init(&ks_tgt->i_tgt_name, "target-name", 1964 KSTAT_DATA_STRING); 1965 kstat_named_init(&ks_tgt->i_tgt_alias, "target-alias", 1966 KSTAT_DATA_STRING); 1967 kstat_named_init(&ks_tgt->i_protocol, "protocol", 1968 KSTAT_DATA_STRING); 1969 1970 /* ident might not be null terminated */ 1971 len = ilport->ilport_lport->lport_id->ident_length; 1972 bcopy(ilport->ilport_lport->lport_id->ident, 1973 ilport->ilport_kstat_tgt_name, len); 1974 ilport->ilport_kstat_tgt_name[len + 1] = NULL; 1975 kstat_named_setstr(&ks_tgt->i_tgt_name, 1976 (const char *)ilport->ilport_kstat_tgt_name); 1977 kstat_named_setstr(&ks_tgt->i_tgt_alias, 1978 (const char *)ilport->ilport_lport->lport_alias); 1979 /* protocol */ 1980 if ((id = ilport->ilport_lport->lport_id->protocol_id) > PROTOCOL_ANY) { 1981 cmn_err(CE_WARN, "STMF: protocol_id out of bound"); 1982 id = PROTOCOL_ANY; 1983 } 1984 kstat_named_setstr(&ks_tgt->i_protocol, protocol_ident[id]); 1985 kstat_install(ilport->ilport_kstat_info); 1986 1987 /* create kstat lport io */ 1988 bzero(ks_nm, sizeof (ks_nm)); 1989 (void) sprintf(ks_nm, "stmf_tgt_io_%"PRIxPTR"", (uintptr_t)ilport); 1990 if ((ilport->ilport_kstat_io = kstat_create(STMF_MODULE_NAME, 0, 1991 ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) { 1992 cmn_err(CE_WARN, "STMF: kstat_create target_io failed"); 1993 return; 1994 } 1995 mutex_init(&ilport->ilport_kstat_lock, NULL, MUTEX_DRIVER, 0); 1996 ilport->ilport_kstat_io->ks_lock = &ilport->ilport_kstat_lock; 1997 kstat_install(ilport->ilport_kstat_io); 1998 } 1999 2000 stmf_status_t 2001 stmf_register_lu(stmf_lu_t *lu) 2002 { 2003 stmf_i_lu_t *ilu; 2004 uint8_t *p1, *p2; 2005 stmf_state_change_info_t ssci; 2006 stmf_id_data_t *luid; 2007 2008 if ((lu->lu_id->ident_type != ID_TYPE_NAA) || 2009 (lu->lu_id->ident_length != 16) || 2010 ((lu->lu_id->ident[0] & 0xf0) != 0x60)) { 2011 return (STMF_INVALID_ARG); 2012 } 2013 p1 = &lu->lu_id->ident[0]; 2014 mutex_enter(&stmf_state.stmf_lock); 2015 if (stmf_state.stmf_inventory_locked) { 2016 mutex_exit(&stmf_state.stmf_lock); 2017 return (STMF_BUSY); 2018 } 2019 2020 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) { 2021 p2 = &ilu->ilu_lu->lu_id->ident[0]; 2022 if (bcmp(p1, p2, 16) == 0) { 2023 mutex_exit(&stmf_state.stmf_lock); 2024 return (STMF_ALREADY); 2025 } 2026 } 2027 2028 ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 2029 luid = stmf_lookup_id(&stmf_state.stmf_luid_list, 2030 lu->lu_id->ident_length, lu->lu_id->ident); 2031 if (luid) { 2032 luid->id_pt_to_object = (void *)ilu; 2033 ilu->ilu_luid = luid; 2034 } 2035 ilu->ilu_alias = NULL; 2036 2037 ilu->ilu_next = stmf_state.stmf_ilulist; 2038 ilu->ilu_prev = NULL; 2039 if (ilu->ilu_next) 2040 ilu->ilu_next->ilu_prev = ilu; 2041 stmf_state.stmf_ilulist = ilu; 2042 stmf_state.stmf_nlus++; 2043 if (lu->lu_lp) { 2044 ((stmf_i_lu_provider_t *) 2045 (lu->lu_lp->lp_stmf_private))->ilp_nlus++; 2046 } 2047 ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1; 2048 STMF_EVENT_ALLOC_HANDLE(ilu->ilu_event_hdl); 2049 stmf_create_kstat_lu(ilu); 2050 mutex_exit(&stmf_state.stmf_lock); 2051 2052 /* XXX we should probably check if this lu can be brought online */ 2053 ilu->ilu_prev_state = STMF_STATE_ONLINE; 2054 if (stmf_state.stmf_service_running) { 2055 ssci.st_rflags = 0; 2056 ssci.st_additional_info = NULL; 2057 (void) stmf_ctl(STMF_CMD_LU_ONLINE, lu, &ssci); 2058 } 2059 2060 /* XXX: Generate event */ 2061 return (STMF_SUCCESS); 2062 } 2063 2064 stmf_status_t 2065 stmf_deregister_lu(stmf_lu_t *lu) 2066 { 2067 stmf_i_lu_t *ilu; 2068 2069 mutex_enter(&stmf_state.stmf_lock); 2070 if (stmf_state.stmf_inventory_locked) { 2071 mutex_exit(&stmf_state.stmf_lock); 2072 return (STMF_BUSY); 2073 } 2074 ilu = stmf_lookup_lu(lu); 2075 if (ilu == NULL) { 2076 mutex_exit(&stmf_state.stmf_lock); 2077 return (STMF_INVALID_ARG); 2078 } 2079 if (ilu->ilu_state == STMF_STATE_OFFLINE) { 2080 ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free); 2081 while (ilu->ilu_flags & ILU_STALL_DEREGISTER) { 2082 cv_wait(&stmf_state.stmf_cv, &stmf_state.stmf_lock); 2083 } 2084 if (ilu->ilu_ntasks) { 2085 stmf_i_scsi_task_t *itask, *nitask; 2086 2087 nitask = ilu->ilu_tasks; 2088 do { 2089 itask = nitask; 2090 nitask = itask->itask_lu_next; 2091 lu->lu_task_free(itask->itask_task); 2092 stmf_free(itask->itask_task); 2093 } while (nitask != NULL); 2094 2095 ilu->ilu_tasks = ilu->ilu_free_tasks = NULL; 2096 ilu->ilu_ntasks = ilu->ilu_ntasks_free = 0; 2097 } 2098 2099 if (ilu->ilu_next) 2100 ilu->ilu_next->ilu_prev = ilu->ilu_prev; 2101 if (ilu->ilu_prev) 2102 ilu->ilu_prev->ilu_next = ilu->ilu_next; 2103 else 2104 stmf_state.stmf_ilulist = ilu->ilu_next; 2105 stmf_state.stmf_nlus--; 2106 2107 if (ilu == stmf_state.stmf_svc_ilu_draining) { 2108 stmf_state.stmf_svc_ilu_draining = ilu->ilu_next; 2109 } 2110 if (ilu == stmf_state.stmf_svc_ilu_timing) { 2111 stmf_state.stmf_svc_ilu_timing = ilu->ilu_next; 2112 } 2113 if (lu->lu_lp) { 2114 ((stmf_i_lu_provider_t *) 2115 (lu->lu_lp->lp_stmf_private))->ilp_nlus--; 2116 } 2117 if (ilu->ilu_luid) { 2118 ((stmf_id_data_t *)ilu->ilu_luid)->id_pt_to_object = 2119 NULL; 2120 ilu->ilu_luid = NULL; 2121 } 2122 STMF_EVENT_FREE_HANDLE(ilu->ilu_event_hdl); 2123 } else { 2124 mutex_exit(&stmf_state.stmf_lock); 2125 return (STMF_BUSY); 2126 } 2127 if (ilu->ilu_kstat_info) { 2128 kstat_delete(ilu->ilu_kstat_info); 2129 } 2130 if (ilu->ilu_kstat_io) { 2131 kstat_delete(ilu->ilu_kstat_io); 2132 mutex_destroy(&ilu->ilu_kstat_lock); 2133 } 2134 mutex_exit(&stmf_state.stmf_lock); 2135 return (STMF_SUCCESS); 2136 } 2137 2138 stmf_status_t 2139 stmf_register_local_port(stmf_local_port_t *lport) 2140 { 2141 stmf_i_local_port_t *ilport; 2142 stmf_state_change_info_t ssci; 2143 int start_workers = 0; 2144 2145 mutex_enter(&stmf_state.stmf_lock); 2146 if (stmf_state.stmf_inventory_locked) { 2147 mutex_exit(&stmf_state.stmf_lock); 2148 return (STMF_BUSY); 2149 } 2150 ilport = (stmf_i_local_port_t *)lport->lport_stmf_private; 2151 rw_init(&ilport->ilport_lock, NULL, RW_DRIVER, NULL); 2152 2153 ilport->ilport_next = stmf_state.stmf_ilportlist; 2154 ilport->ilport_prev = NULL; 2155 if (ilport->ilport_next) 2156 ilport->ilport_next->ilport_prev = ilport; 2157 stmf_state.stmf_ilportlist = ilport; 2158 stmf_state.stmf_nlports++; 2159 if (lport->lport_pp) { 2160 ((stmf_i_port_provider_t *) 2161 (lport->lport_pp->pp_stmf_private))->ipp_npps++; 2162 } 2163 ilport->ilport_tg = 2164 stmf_lookup_group_for_target(lport->lport_id->ident, 2165 lport->lport_id->ident_length); 2166 ilport->ilport_rtpid = atomic_add_16_nv(&stmf_rtpid_counter, 1); 2167 STMF_EVENT_ALLOC_HANDLE(ilport->ilport_event_hdl); 2168 stmf_create_kstat_lport(ilport); 2169 if (stmf_workers_state == STMF_WORKERS_DISABLED) { 2170 stmf_workers_state = STMF_WORKERS_ENABLING; 2171 start_workers = 1; 2172 } 2173 mutex_exit(&stmf_state.stmf_lock); 2174 2175 if (start_workers) 2176 stmf_worker_init(); 2177 2178 /* XXX we should probably check if this lport can be brought online */ 2179 ilport->ilport_prev_state = STMF_STATE_ONLINE; 2180 if (stmf_state.stmf_service_running) { 2181 ssci.st_rflags = 0; 2182 ssci.st_additional_info = NULL; 2183 (void) stmf_ctl(STMF_CMD_LPORT_ONLINE, lport, &ssci); 2184 } 2185 2186 /* XXX: Generate event */ 2187 return (STMF_SUCCESS); 2188 } 2189 2190 stmf_status_t 2191 stmf_deregister_local_port(stmf_local_port_t *lport) 2192 { 2193 stmf_i_local_port_t *ilport; 2194 2195 mutex_enter(&stmf_state.stmf_lock); 2196 if (stmf_state.stmf_inventory_locked) { 2197 mutex_exit(&stmf_state.stmf_lock); 2198 return (STMF_BUSY); 2199 } 2200 ilport = (stmf_i_local_port_t *)lport->lport_stmf_private; 2201 if (ilport->ilport_nsessions == 0) { 2202 if (ilport->ilport_next) 2203 ilport->ilport_next->ilport_prev = ilport->ilport_prev; 2204 if (ilport->ilport_prev) 2205 ilport->ilport_prev->ilport_next = ilport->ilport_next; 2206 else 2207 stmf_state.stmf_ilportlist = ilport->ilport_next; 2208 rw_destroy(&ilport->ilport_lock); 2209 stmf_state.stmf_nlports--; 2210 if (lport->lport_pp) { 2211 ((stmf_i_port_provider_t *) 2212 (lport->lport_pp->pp_stmf_private))->ipp_npps--; 2213 } 2214 ilport->ilport_tg = NULL; 2215 STMF_EVENT_FREE_HANDLE(ilport->ilport_event_hdl); 2216 } else { 2217 mutex_exit(&stmf_state.stmf_lock); 2218 return (STMF_BUSY); 2219 } 2220 if (ilport->ilport_kstat_info) { 2221 kstat_delete(ilport->ilport_kstat_info); 2222 } 2223 if (ilport->ilport_kstat_io) { 2224 kstat_delete(ilport->ilport_kstat_io); 2225 mutex_destroy(&ilport->ilport_kstat_lock); 2226 } 2227 mutex_exit(&stmf_state.stmf_lock); 2228 return (STMF_SUCCESS); 2229 } 2230 2231 /* 2232 * Port provider has to make sure that register/deregister session and 2233 * port are serialized calls. 2234 */ 2235 stmf_status_t 2236 stmf_register_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss) 2237 { 2238 stmf_i_scsi_session_t *iss; 2239 stmf_i_local_port_t *ilport = (stmf_i_local_port_t *) 2240 lport->lport_stmf_private; 2241 uint8_t lun[8]; 2242 2243 /* 2244 * Port state has to be online to register a scsi session. It is 2245 * possible that we started an offline operation and a new SCSI 2246 * session started at the same time (in that case also we are going 2247 * to fail the registeration). But any other state is simply 2248 * a bad port provider implementation. 2249 */ 2250 if (ilport->ilport_state != STMF_STATE_ONLINE) { 2251 if (ilport->ilport_state != STMF_STATE_OFFLINING) { 2252 stmf_trace(lport->lport_alias, "Port is trying to " 2253 "register a session while the state is neither " 2254 "online nor offlining"); 2255 } 2256 return (STMF_FAILURE); 2257 } 2258 bzero(lun, 8); 2259 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private; 2260 iss->iss_flags |= ISS_BEING_CREATED; 2261 2262 /* sessions use the ilport_lock. No separate lock is required */ 2263 iss->iss_lockp = &ilport->ilport_lock; 2264 (void) stmf_session_create_lun_map(ilport, iss); 2265 2266 rw_enter(&ilport->ilport_lock, RW_WRITER); 2267 ilport->ilport_nsessions++; 2268 iss->iss_next = ilport->ilport_ss_list; 2269 ilport->ilport_ss_list = iss; 2270 rw_exit(&ilport->ilport_lock); 2271 2272 iss->iss_creation_time = ddi_get_time(); 2273 ss->ss_session_id = atomic_add_64_nv(&stmf_session_counter, 1); 2274 iss->iss_flags &= ~ISS_BEING_CREATED; 2275 DTRACE_PROBE2(session__online, stmf_local_port_t *, lport, 2276 stmf_scsi_session_t *, ss); 2277 return (STMF_SUCCESS); 2278 } 2279 2280 void 2281 stmf_deregister_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss) 2282 { 2283 stmf_i_local_port_t *ilport = (stmf_i_local_port_t *) 2284 lport->lport_stmf_private; 2285 stmf_i_scsi_session_t *iss, **ppss; 2286 int found = 0; 2287 2288 DTRACE_PROBE2(session__offline, stmf_local_port_t *, lport, 2289 stmf_scsi_session_t *, ss); 2290 2291 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private; 2292 if (ss->ss_rport_alias) { 2293 ss->ss_rport_alias = NULL; 2294 } 2295 2296 try_dereg_ss_again: 2297 mutex_enter(&stmf_state.stmf_lock); 2298 atomic_and_32(&iss->iss_flags, 2299 ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS)); 2300 if (iss->iss_flags & ISS_EVENT_ACTIVE) { 2301 mutex_exit(&stmf_state.stmf_lock); 2302 delay(1); 2303 goto try_dereg_ss_again; 2304 } 2305 mutex_exit(&stmf_state.stmf_lock); 2306 rw_enter(&ilport->ilport_lock, RW_WRITER); 2307 for (ppss = &ilport->ilport_ss_list; *ppss != NULL; 2308 ppss = &((*ppss)->iss_next)) { 2309 if (iss == (*ppss)) { 2310 *ppss = (*ppss)->iss_next; 2311 found = 1; 2312 break; 2313 } 2314 } 2315 if (!found) { 2316 cmn_err(CE_PANIC, "Deregister session called for non existent" 2317 " session"); 2318 } 2319 ilport->ilport_nsessions--; 2320 rw_exit(&ilport->ilport_lock); 2321 2322 (void) stmf_session_destroy_lun_map(ilport, iss); 2323 } 2324 2325 stmf_i_scsi_session_t * 2326 stmf_session_id_to_issptr(uint64_t session_id, int stay_locked) 2327 { 2328 stmf_i_local_port_t *ilport; 2329 stmf_i_scsi_session_t *iss; 2330 2331 mutex_enter(&stmf_state.stmf_lock); 2332 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 2333 ilport = ilport->ilport_next) { 2334 rw_enter(&ilport->ilport_lock, RW_WRITER); 2335 for (iss = ilport->ilport_ss_list; iss != NULL; 2336 iss = iss->iss_next) { 2337 if (iss->iss_ss->ss_session_id == session_id) { 2338 if (!stay_locked) 2339 rw_exit(&ilport->ilport_lock); 2340 mutex_exit(&stmf_state.stmf_lock); 2341 return (iss); 2342 } 2343 } 2344 rw_exit(&ilport->ilport_lock); 2345 } 2346 mutex_exit(&stmf_state.stmf_lock); 2347 return (NULL); 2348 } 2349 2350 void 2351 stmf_release_itl_handle(stmf_lu_t *lu, stmf_itl_data_t *itl) 2352 { 2353 stmf_itl_data_t **itlpp; 2354 stmf_i_lu_t *ilu; 2355 2356 ASSERT(itl->itl_flags & STMF_ITL_BEING_TERMINATED); 2357 2358 ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 2359 mutex_enter(&ilu->ilu_task_lock); 2360 for (itlpp = &ilu->ilu_itl_list; (*itlpp) != NULL; 2361 itlpp = &(*itlpp)->itl_next) { 2362 if ((*itlpp) == itl) 2363 break; 2364 } 2365 ASSERT((*itlpp) != NULL); 2366 *itlpp = itl->itl_next; 2367 mutex_exit(&ilu->ilu_task_lock); 2368 lu->lu_abort(lu, STMF_LU_ITL_HANDLE_REMOVED, itl->itl_handle, 2369 (uint32_t)itl->itl_hdlrm_reason); 2370 kmem_free(itl, sizeof (*itl)); 2371 } 2372 2373 stmf_status_t 2374 stmf_register_itl_handle(stmf_lu_t *lu, uint8_t *lun, 2375 stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle) 2376 { 2377 stmf_itl_data_t *itl; 2378 stmf_i_scsi_session_t *iss; 2379 stmf_lun_map_ent_t *lun_map_ent; 2380 stmf_i_lu_t *ilu; 2381 uint16_t n; 2382 2383 ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 2384 if (ss == NULL) { 2385 iss = stmf_session_id_to_issptr(session_id, 1); 2386 if (iss == NULL) 2387 return (STMF_NOT_FOUND); 2388 } else { 2389 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private; 2390 rw_enter(iss->iss_lockp, RW_WRITER); 2391 } 2392 2393 n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8)); 2394 lun_map_ent = (stmf_lun_map_ent_t *) 2395 stmf_get_ent_from_map(iss->iss_sm, n); 2396 if ((lun_map_ent == NULL) || (lun_map_ent->ent_lu != lu)) { 2397 rw_exit(iss->iss_lockp); 2398 return (STMF_NOT_FOUND); 2399 } 2400 if (lun_map_ent->ent_itl_datap != NULL) { 2401 rw_exit(iss->iss_lockp); 2402 return (STMF_ALREADY); 2403 } 2404 2405 itl = (stmf_itl_data_t *)kmem_zalloc(sizeof (*itl), KM_NOSLEEP); 2406 if (itl == NULL) { 2407 rw_exit(iss->iss_lockp); 2408 return (STMF_ALLOC_FAILURE); 2409 } 2410 2411 itl->itl_counter = 1; 2412 itl->itl_lun = n; 2413 itl->itl_handle = itl_handle; 2414 itl->itl_session = iss; 2415 mutex_enter(&ilu->ilu_task_lock); 2416 itl->itl_next = ilu->ilu_itl_list; 2417 ilu->ilu_itl_list = itl; 2418 mutex_exit(&ilu->ilu_task_lock); 2419 lun_map_ent->ent_itl_datap = itl; 2420 rw_exit(iss->iss_lockp); 2421 2422 return (STMF_SUCCESS); 2423 } 2424 2425 void 2426 stmf_do_itl_dereg(stmf_lu_t *lu, stmf_itl_data_t *itl, uint8_t hdlrm_reason) 2427 { 2428 uint8_t old, new; 2429 2430 do { 2431 old = new = itl->itl_flags; 2432 if (old & STMF_ITL_BEING_TERMINATED) 2433 return; 2434 new |= STMF_ITL_BEING_TERMINATED; 2435 } while (atomic_cas_8(&itl->itl_flags, old, new) != old); 2436 itl->itl_hdlrm_reason = hdlrm_reason; 2437 2438 ASSERT(itl->itl_counter); 2439 2440 if (atomic_add_32_nv(&itl->itl_counter, -1)) 2441 return; 2442 2443 drv_usecwait(10); 2444 if (itl->itl_counter) 2445 return; 2446 2447 stmf_release_itl_handle(lu, itl); 2448 } 2449 2450 stmf_status_t 2451 stmf_deregister_all_lu_itl_handles(stmf_lu_t *lu) 2452 { 2453 stmf_i_lu_t *ilu; 2454 stmf_i_local_port_t *ilport; 2455 stmf_i_scsi_session_t *iss; 2456 stmf_lun_map_t *lm; 2457 stmf_lun_map_ent_t *ent; 2458 uint32_t nmaps, nu; 2459 stmf_itl_data_t **itl_list; 2460 int i; 2461 2462 ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 2463 2464 dereg_itl_start:; 2465 nmaps = ilu->ilu_ref_cnt; 2466 if (nmaps == 0) 2467 return (STMF_NOT_FOUND); 2468 itl_list = (stmf_itl_data_t **)kmem_zalloc( 2469 nmaps * sizeof (stmf_itl_data_t *), KM_SLEEP); 2470 mutex_enter(&stmf_state.stmf_lock); 2471 if (nmaps != ilu->ilu_ref_cnt) { 2472 /* Something changed, start all over */ 2473 mutex_exit(&stmf_state.stmf_lock); 2474 kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *)); 2475 goto dereg_itl_start; 2476 } 2477 nu = 0; 2478 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL; 2479 ilport = ilport->ilport_next) { 2480 rw_enter(&ilport->ilport_lock, RW_WRITER); 2481 for (iss = ilport->ilport_ss_list; iss != NULL; 2482 iss = iss->iss_next) { 2483 lm = iss->iss_sm; 2484 if (!lm) 2485 continue; 2486 for (i = 0; i < lm->lm_nentries; i++) { 2487 if (lm->lm_plus[i] == NULL) 2488 continue; 2489 ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 2490 if ((ent->ent_lu == lu) && 2491 (ent->ent_itl_datap)) { 2492 itl_list[nu++] = ent->ent_itl_datap; 2493 ent->ent_itl_datap = NULL; 2494 if (nu == nmaps) { 2495 rw_exit(&ilport->ilport_lock); 2496 goto dai_scan_done; 2497 } 2498 } 2499 } /* lun table for a session */ 2500 } /* sessions */ 2501 rw_exit(&ilport->ilport_lock); 2502 } /* ports */ 2503 2504 dai_scan_done: 2505 mutex_exit(&stmf_state.stmf_lock); 2506 2507 for (i = 0; i < nu; i++) { 2508 stmf_do_itl_dereg(lu, itl_list[i], 2509 STMF_ITL_REASON_DEREG_REQUEST); 2510 } 2511 kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *)); 2512 2513 return (STMF_SUCCESS); 2514 } 2515 2516 stmf_status_t 2517 stmf_deregister_itl_handle(stmf_lu_t *lu, uint8_t *lun, 2518 stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle) 2519 { 2520 stmf_i_scsi_session_t *iss; 2521 stmf_itl_data_t *itl; 2522 stmf_lun_map_ent_t *ent; 2523 stmf_lun_map_t *lm; 2524 int i; 2525 uint16_t n; 2526 2527 if (ss == NULL) { 2528 if (session_id == STMF_SESSION_ID_NONE) 2529 return (STMF_INVALID_ARG); 2530 iss = stmf_session_id_to_issptr(session_id, 1); 2531 if (iss == NULL) 2532 return (STMF_NOT_FOUND); 2533 } else { 2534 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private; 2535 rw_enter(iss->iss_lockp, RW_WRITER); 2536 } 2537 lm = iss->iss_sm; 2538 if (lm == NULL) { 2539 rw_exit(iss->iss_lockp); 2540 return (STMF_NOT_FOUND); 2541 } 2542 2543 if (lun) { 2544 n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8)); 2545 ent = (stmf_lun_map_ent_t *) 2546 stmf_get_ent_from_map(iss->iss_sm, n); 2547 } else { 2548 if (itl_handle == NULL) { 2549 rw_exit(iss->iss_lockp); 2550 return (STMF_INVALID_ARG); 2551 } 2552 ent = NULL; 2553 for (i = 0; i < lm->lm_nentries; i++) { 2554 if (lm->lm_plus[i] == NULL) 2555 continue; 2556 ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 2557 if (ent->ent_itl_datap && 2558 (ent->ent_itl_datap->itl_handle == itl_handle)) { 2559 break; 2560 } 2561 } 2562 } 2563 if ((ent == NULL) || (ent->ent_lu != lu) || 2564 (ent->ent_itl_datap == NULL)) { 2565 rw_exit(iss->iss_lockp); 2566 return (STMF_NOT_FOUND); 2567 } 2568 itl = ent->ent_itl_datap; 2569 ent->ent_itl_datap = NULL; 2570 rw_exit(iss->iss_lockp); 2571 stmf_do_itl_dereg(lu, itl, STMF_ITL_REASON_DEREG_REQUEST); 2572 2573 return (STMF_SUCCESS); 2574 } 2575 2576 stmf_status_t 2577 stmf_get_itl_handle(stmf_lu_t *lu, uint8_t *lun, stmf_scsi_session_t *ss, 2578 uint64_t session_id, void **itl_handle_retp) 2579 { 2580 stmf_i_scsi_session_t *iss; 2581 stmf_lun_map_ent_t *ent; 2582 stmf_lun_map_t *lm; 2583 stmf_status_t ret; 2584 int i; 2585 uint16_t n; 2586 2587 if (ss == NULL) { 2588 iss = stmf_session_id_to_issptr(session_id, 1); 2589 if (iss == NULL) 2590 return (STMF_NOT_FOUND); 2591 } else { 2592 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private; 2593 rw_enter(iss->iss_lockp, RW_WRITER); 2594 } 2595 2596 ent = NULL; 2597 if (lun == NULL) { 2598 lm = iss->iss_sm; 2599 for (i = 0; i < lm->lm_nentries; i++) { 2600 if (lm->lm_plus[i] == NULL) 2601 continue; 2602 ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 2603 if (ent->ent_lu == lu) 2604 break; 2605 } 2606 } else { 2607 n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8)); 2608 ent = (stmf_lun_map_ent_t *) 2609 stmf_get_ent_from_map(iss->iss_sm, n); 2610 if (lu && (ent->ent_lu != lu)) 2611 ent = NULL; 2612 } 2613 if (ent && ent->ent_itl_datap) { 2614 *itl_handle_retp = ent->ent_itl_datap->itl_handle; 2615 ret = STMF_SUCCESS; 2616 } else { 2617 ret = STMF_NOT_FOUND; 2618 } 2619 2620 rw_exit(iss->iss_lockp); 2621 return (ret); 2622 } 2623 2624 stmf_data_buf_t * 2625 stmf_alloc_dbuf(scsi_task_t *task, uint32_t size, uint32_t *pminsize, 2626 uint32_t flags) 2627 { 2628 stmf_i_scsi_task_t *itask = 2629 (stmf_i_scsi_task_t *)task->task_stmf_private; 2630 stmf_local_port_t *lport = task->task_lport; 2631 stmf_data_buf_t *dbuf; 2632 uint8_t ndx; 2633 2634 ndx = stmf_first_zero[itask->itask_allocated_buf_map]; 2635 if (ndx == 0xff) 2636 return (NULL); 2637 dbuf = itask->itask_dbufs[ndx] = lport->lport_ds->ds_alloc_data_buf( 2638 task, size, pminsize, flags); 2639 if (dbuf) { 2640 task->task_cur_nbufs++; 2641 itask->itask_allocated_buf_map |= (1 << ndx); 2642 dbuf->db_handle = ndx; 2643 return (dbuf); 2644 } 2645 2646 return (NULL); 2647 } 2648 2649 void 2650 stmf_free_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf) 2651 { 2652 stmf_i_scsi_task_t *itask = 2653 (stmf_i_scsi_task_t *)task->task_stmf_private; 2654 stmf_local_port_t *lport = task->task_lport; 2655 2656 itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle); 2657 task->task_cur_nbufs--; 2658 lport->lport_ds->ds_free_data_buf(lport->lport_ds, dbuf); 2659 } 2660 2661 stmf_data_buf_t * 2662 stmf_handle_to_buf(scsi_task_t *task, uint8_t h) 2663 { 2664 stmf_i_scsi_task_t *itask; 2665 2666 itask = (stmf_i_scsi_task_t *)task->task_stmf_private; 2667 if (h > 3) 2668 return (NULL); 2669 return (itask->itask_dbufs[h]); 2670 } 2671 2672 /* ARGSUSED */ 2673 struct scsi_task * 2674 stmf_task_alloc(struct stmf_local_port *lport, stmf_scsi_session_t *ss, 2675 uint8_t *lun, uint16_t cdb_length_in, uint16_t ext_id) 2676 { 2677 stmf_lu_t *lu; 2678 stmf_i_scsi_session_t *iss; 2679 stmf_i_lu_t *ilu; 2680 stmf_i_scsi_task_t *itask; 2681 stmf_i_scsi_task_t **ppitask; 2682 scsi_task_t *task; 2683 uint64_t *p; 2684 uint8_t *l; 2685 stmf_lun_map_ent_t *lun_map_ent; 2686 uint16_t cdb_length; 2687 uint16_t luNbr; 2688 uint8_t new_task = 0; 2689 2690 /* 2691 * We allocate 7 extra bytes for CDB to provide a cdb pointer which 2692 * is guaranteed to be 8 byte aligned. Some LU providers like OSD 2693 * depend upon this alignment. 2694 */ 2695 if (cdb_length_in >= 16) 2696 cdb_length = cdb_length_in + 7; 2697 else 2698 cdb_length = 16 + 7; 2699 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private; 2700 luNbr = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8)); 2701 rw_enter(iss->iss_lockp, RW_READER); 2702 lun_map_ent = 2703 (stmf_lun_map_ent_t *)stmf_get_ent_from_map(iss->iss_sm, luNbr); 2704 if (!lun_map_ent) { 2705 lu = dlun0; 2706 } else { 2707 lu = lun_map_ent->ent_lu; 2708 } 2709 ilu = lu->lu_stmf_private; 2710 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 2711 rw_exit(iss->iss_lockp); 2712 return (NULL); 2713 } 2714 do { 2715 if (ilu->ilu_free_tasks == NULL) { 2716 new_task = 1; 2717 break; 2718 } 2719 mutex_enter(&ilu->ilu_task_lock); 2720 for (ppitask = &ilu->ilu_free_tasks; (*ppitask != NULL) && 2721 ((*ppitask)->itask_cdb_buf_size < cdb_length); 2722 ppitask = &((*ppitask)->itask_lu_free_next)) 2723 ; 2724 if (*ppitask) { 2725 itask = *ppitask; 2726 *ppitask = (*ppitask)->itask_lu_free_next; 2727 ilu->ilu_ntasks_free--; 2728 if (ilu->ilu_ntasks_free < ilu->ilu_ntasks_min_free) 2729 ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free; 2730 } else { 2731 new_task = 1; 2732 } 2733 mutex_exit(&ilu->ilu_task_lock); 2734 /* CONSTCOND */ 2735 } while (0); 2736 2737 if (!new_task) { 2738 task = itask->itask_task; 2739 task->task_timeout = 0; 2740 p = (uint64_t *)&task->task_flags; 2741 *p++ = 0; *p++ = 0; p++; p++; *p++ = 0; *p++ = 0; *p = 0; 2742 itask->itask_ncmds = 0; 2743 } else { 2744 task = (scsi_task_t *)stmf_alloc(STMF_STRUCT_SCSI_TASK, 2745 cdb_length, AF_FORCE_NOSLEEP); 2746 if (task == NULL) { 2747 rw_exit(iss->iss_lockp); 2748 return (NULL); 2749 } 2750 task->task_lu = lu; 2751 l = task->task_lun_no; 2752 l[0] = lun[0]; 2753 l[1] = lun[1]; 2754 l[2] = lun[2]; 2755 l[3] = lun[3]; 2756 l[4] = lun[4]; 2757 l[5] = lun[5]; 2758 l[6] = lun[6]; 2759 l[7] = lun[7]; 2760 task->task_cdb = (uint8_t *)task->task_port_private; 2761 if ((ulong_t)(task->task_cdb) & 7ul) { 2762 task->task_cdb = (uint8_t *)(((ulong_t) 2763 (task->task_cdb) + 7ul) & ~(7ul)); 2764 } 2765 itask = (stmf_i_scsi_task_t *)task->task_stmf_private; 2766 itask->itask_cdb_buf_size = cdb_length; 2767 } 2768 task->task_session = ss; 2769 task->task_lport = lport; 2770 task->task_cdb_length = cdb_length_in; 2771 itask->itask_flags = ITASK_IN_TRANSITION; 2772 2773 if (new_task) { 2774 if (lu->lu_task_alloc(task) != STMF_SUCCESS) { 2775 rw_exit(iss->iss_lockp); 2776 stmf_free(task); 2777 return (NULL); 2778 } 2779 mutex_enter(&ilu->ilu_task_lock); 2780 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 2781 mutex_exit(&ilu->ilu_task_lock); 2782 rw_exit(iss->iss_lockp); 2783 stmf_free(task); 2784 return (NULL); 2785 } 2786 itask->itask_lu_next = ilu->ilu_tasks; 2787 if (ilu->ilu_tasks) 2788 ilu->ilu_tasks->itask_lu_prev = itask; 2789 ilu->ilu_tasks = itask; 2790 /* kmem_zalloc automatically makes itask->itask_lu_prev NULL */ 2791 ilu->ilu_ntasks++; 2792 mutex_exit(&ilu->ilu_task_lock); 2793 } 2794 2795 itask->itask_ilu_task_cntr = ilu->ilu_cur_task_cntr; 2796 atomic_add_32(itask->itask_ilu_task_cntr, 1); 2797 itask->itask_start_time = ddi_get_lbolt(); 2798 2799 if ((lun_map_ent != NULL) && ((itask->itask_itl_datap = 2800 lun_map_ent->ent_itl_datap) != NULL)) { 2801 atomic_add_32(&itask->itask_itl_datap->itl_counter, 1); 2802 task->task_lu_itl_handle = itask->itask_itl_datap->itl_handle; 2803 } else { 2804 itask->itask_itl_datap = NULL; 2805 task->task_lu_itl_handle = NULL; 2806 } 2807 2808 rw_exit(iss->iss_lockp); 2809 return (task); 2810 } 2811 2812 void 2813 stmf_task_lu_free(scsi_task_t *task) 2814 { 2815 stmf_i_scsi_task_t *itask = 2816 (stmf_i_scsi_task_t *)task->task_stmf_private; 2817 stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *) 2818 task->task_session->ss_stmf_private; 2819 stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 2820 2821 rw_enter(iss->iss_lockp, RW_READER); 2822 itask->itask_flags = ITASK_IN_FREE_LIST; 2823 mutex_enter(&ilu->ilu_task_lock); 2824 itask->itask_lu_free_next = ilu->ilu_free_tasks; 2825 ilu->ilu_free_tasks = itask; 2826 ilu->ilu_ntasks_free++; 2827 mutex_exit(&ilu->ilu_task_lock); 2828 atomic_add_32(itask->itask_ilu_task_cntr, -1); 2829 rw_exit(iss->iss_lockp); 2830 } 2831 2832 void 2833 stmf_task_lu_check_freelist(stmf_i_lu_t *ilu) 2834 { 2835 uint32_t num_to_release, ndx; 2836 stmf_i_scsi_task_t *itask; 2837 stmf_lu_t *lu = ilu->ilu_lu; 2838 2839 ASSERT(ilu->ilu_ntasks_min_free <= ilu->ilu_ntasks_free); 2840 2841 /* free half of the minimal free of the free tasks */ 2842 num_to_release = (ilu->ilu_ntasks_min_free + 1) / 2; 2843 if (!num_to_release) { 2844 return; 2845 } 2846 for (ndx = 0; ndx < num_to_release; ndx++) { 2847 mutex_enter(&ilu->ilu_task_lock); 2848 itask = ilu->ilu_free_tasks; 2849 if (itask == NULL) { 2850 mutex_exit(&ilu->ilu_task_lock); 2851 break; 2852 } 2853 ilu->ilu_free_tasks = itask->itask_lu_free_next; 2854 ilu->ilu_ntasks_free--; 2855 mutex_exit(&ilu->ilu_task_lock); 2856 2857 lu->lu_task_free(itask->itask_task); 2858 mutex_enter(&ilu->ilu_task_lock); 2859 if (itask->itask_lu_next) 2860 itask->itask_lu_next->itask_lu_prev = 2861 itask->itask_lu_prev; 2862 if (itask->itask_lu_prev) 2863 itask->itask_lu_prev->itask_lu_next = 2864 itask->itask_lu_next; 2865 else 2866 ilu->ilu_tasks = itask->itask_lu_next; 2867 2868 ilu->ilu_ntasks--; 2869 mutex_exit(&ilu->ilu_task_lock); 2870 stmf_free(itask->itask_task); 2871 } 2872 } 2873 2874 /* 2875 * Called with stmf_lock held 2876 */ 2877 void 2878 stmf_check_freetask() 2879 { 2880 stmf_i_lu_t *ilu; 2881 clock_t endtime = ddi_get_lbolt() + drv_usectohz(10000); 2882 2883 /* stmf_svc_ilu_draining may get changed after stmf_lock is released */ 2884 while ((ilu = stmf_state.stmf_svc_ilu_draining) != NULL) { 2885 stmf_state.stmf_svc_ilu_draining = ilu->ilu_next; 2886 if (!ilu->ilu_ntasks_min_free) { 2887 ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free; 2888 continue; 2889 } 2890 ilu->ilu_flags |= ILU_STALL_DEREGISTER; 2891 mutex_exit(&stmf_state.stmf_lock); 2892 stmf_task_lu_check_freelist(ilu); 2893 /* 2894 * we do not care about the accuracy of 2895 * ilu_ntasks_min_free, so we don't lock here 2896 */ 2897 ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free; 2898 mutex_enter(&stmf_state.stmf_lock); 2899 ilu->ilu_flags &= ~ILU_STALL_DEREGISTER; 2900 cv_broadcast(&stmf_state.stmf_cv); 2901 if (ddi_get_lbolt() >= endtime) 2902 break; 2903 } 2904 } 2905 2906 void 2907 stmf_do_ilu_timeouts(stmf_i_lu_t *ilu) 2908 { 2909 clock_t l = ddi_get_lbolt(); 2910 clock_t ps = drv_usectohz(1000000); 2911 stmf_i_scsi_task_t *itask; 2912 scsi_task_t *task; 2913 uint32_t to; 2914 2915 mutex_enter(&ilu->ilu_task_lock); 2916 for (itask = ilu->ilu_tasks; itask != NULL; 2917 itask = itask->itask_lu_next) { 2918 if (itask->itask_flags & (ITASK_IN_FREE_LIST | 2919 ITASK_BEING_ABORTED)) { 2920 continue; 2921 } 2922 task = itask->itask_task; 2923 if (task->task_timeout == 0) 2924 to = stmf_default_task_timeout; 2925 else 2926 to = task->task_timeout; 2927 if ((itask->itask_start_time + (to * ps)) > l) 2928 continue; 2929 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 2930 STMF_TIMEOUT, NULL); 2931 } 2932 mutex_exit(&ilu->ilu_task_lock); 2933 } 2934 2935 /* 2936 * Called with stmf_lock held 2937 */ 2938 void 2939 stmf_check_ilu_timing() 2940 { 2941 stmf_i_lu_t *ilu; 2942 clock_t endtime = ddi_get_lbolt() + drv_usectohz(10000); 2943 2944 /* stmf_svc_ilu_timing may get changed after stmf_lock is released */ 2945 while ((ilu = stmf_state.stmf_svc_ilu_timing) != NULL) { 2946 stmf_state.stmf_svc_ilu_timing = ilu->ilu_next; 2947 if (ilu->ilu_cur_task_cntr == (&ilu->ilu_task_cntr1)) { 2948 if (ilu->ilu_task_cntr2 == 0) { 2949 ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr2; 2950 continue; 2951 } 2952 } else { 2953 if (ilu->ilu_task_cntr1 == 0) { 2954 ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1; 2955 continue; 2956 } 2957 } 2958 /* 2959 * If we are here then it means that there is some slowdown 2960 * in tasks on this lu. We need to check. 2961 */ 2962 ilu->ilu_flags |= ILU_STALL_DEREGISTER; 2963 mutex_exit(&stmf_state.stmf_lock); 2964 stmf_do_ilu_timeouts(ilu); 2965 mutex_enter(&stmf_state.stmf_lock); 2966 ilu->ilu_flags &= ~ILU_STALL_DEREGISTER; 2967 cv_broadcast(&stmf_state.stmf_cv); 2968 if (ddi_get_lbolt() >= endtime) 2969 break; 2970 } 2971 } 2972 2973 /* 2974 * Kills all tasks on a lu except tm_task 2975 */ 2976 void 2977 stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s) 2978 { 2979 stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 2980 stmf_i_scsi_task_t *itask; 2981 2982 mutex_enter(&ilu->ilu_task_lock); 2983 2984 for (itask = ilu->ilu_tasks; itask != NULL; 2985 itask = itask->itask_lu_next) { 2986 if (itask->itask_flags & ITASK_IN_FREE_LIST) 2987 continue; 2988 if (itask->itask_task == tm_task) 2989 continue; 2990 stmf_abort(STMF_QUEUE_TASK_ABORT, itask->itask_task, s, NULL); 2991 } 2992 mutex_exit(&ilu->ilu_task_lock); 2993 } 2994 2995 void 2996 stmf_free_task_bufs(stmf_i_scsi_task_t *itask, stmf_local_port_t *lport) 2997 { 2998 int i; 2999 uint8_t map; 3000 3001 if ((map = itask->itask_allocated_buf_map) != 0) { 3002 for (i = 0; i < 4; i++) { 3003 if (map & 1) { 3004 stmf_data_buf_t *dbuf; 3005 3006 dbuf = itask->itask_dbufs[i]; 3007 if (dbuf->db_lu_private) { 3008 dbuf->db_lu_private = NULL; 3009 } 3010 lport->lport_ds->ds_free_data_buf( 3011 lport->lport_ds, dbuf); 3012 } 3013 map >>= 1; 3014 } 3015 itask->itask_allocated_buf_map = 0; 3016 } 3017 } 3018 3019 void 3020 stmf_task_free(scsi_task_t *task) 3021 { 3022 stmf_local_port_t *lport = task->task_lport; 3023 stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *) 3024 task->task_stmf_private; 3025 3026 DTRACE_PROBE1(stmf__task__end, scsi_task_t *, task); 3027 stmf_free_task_bufs(itask, lport); 3028 if (itask->itask_itl_datap) { 3029 if (atomic_add_32_nv(&itask->itask_itl_datap->itl_counter, 3030 -1) == 0) { 3031 stmf_release_itl_handle(task->task_lu, 3032 itask->itask_itl_datap); 3033 } 3034 } 3035 lport->lport_task_free(task); 3036 if (itask->itask_worker) { 3037 atomic_add_32(&stmf_cur_ntasks, -1); 3038 atomic_add_32(&itask->itask_worker->worker_ref_count, -1); 3039 } 3040 /* 3041 * After calling stmf_task_lu_free, the task pointer can no longer 3042 * be trusted. 3043 */ 3044 stmf_task_lu_free(task); 3045 } 3046 3047 void 3048 stmf_post_task(scsi_task_t *task, stmf_data_buf_t *dbuf) 3049 { 3050 stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *) 3051 task->task_stmf_private; 3052 stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 3053 int nv; 3054 uint32_t old, new; 3055 uint32_t ct; 3056 stmf_worker_t *w, *w1; 3057 uint8_t tm; 3058 3059 if (task->task_max_nbufs > 4) 3060 task->task_max_nbufs = 4; 3061 task->task_cur_nbufs = 0; 3062 /* Latest value of currently running tasks */ 3063 ct = atomic_add_32_nv(&stmf_cur_ntasks, 1); 3064 3065 /* Select the next worker using round robin */ 3066 nv = (int)atomic_add_32_nv((uint32_t *)&stmf_worker_sel_counter, 1); 3067 if (nv >= stmf_nworkers_accepting_cmds) { 3068 int s = nv; 3069 do { 3070 nv -= stmf_nworkers_accepting_cmds; 3071 } while (nv >= stmf_nworkers_accepting_cmds); 3072 if (nv < 0) 3073 nv = 0; 3074 /* Its ok if this cas fails */ 3075 (void) atomic_cas_32((uint32_t *)&stmf_worker_sel_counter, 3076 s, nv); 3077 } 3078 w = &stmf_workers[nv]; 3079 3080 /* 3081 * A worker can be pinned by interrupt. So select the next one 3082 * if it has lower load. 3083 */ 3084 if ((nv + 1) >= stmf_nworkers_accepting_cmds) { 3085 w1 = stmf_workers; 3086 } else { 3087 w1 = &stmf_workers[nv + 1]; 3088 } 3089 if (w1->worker_queue_depth < w->worker_queue_depth) 3090 w = w1; 3091 3092 mutex_enter(&w->worker_lock); 3093 if (((w->worker_flags & STMF_WORKER_STARTED) == 0) || 3094 (w->worker_flags & STMF_WORKER_TERMINATE)) { 3095 /* 3096 * Maybe we are in the middle of a change. Just go to 3097 * the 1st worker. 3098 */ 3099 mutex_exit(&w->worker_lock); 3100 w = stmf_workers; 3101 mutex_enter(&w->worker_lock); 3102 } 3103 itask->itask_worker = w; 3104 /* 3105 * Track max system load inside the worker as we already have the 3106 * worker lock (no point implementing another lock). The service 3107 * thread will do the comparisons and figure out the max overall 3108 * system load. 3109 */ 3110 if (w->worker_max_sys_qdepth_pu < ct) 3111 w->worker_max_sys_qdepth_pu = ct; 3112 3113 do { 3114 old = new = itask->itask_flags; 3115 new |= ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE; 3116 if (task->task_mgmt_function) { 3117 tm = task->task_mgmt_function; 3118 if ((tm == TM_TARGET_RESET) || 3119 (tm == TM_TARGET_COLD_RESET) || 3120 (tm == TM_TARGET_WARM_RESET)) { 3121 new |= ITASK_DEFAULT_HANDLING; 3122 } 3123 } else if (task->task_cdb[0] == SCMD_REPORT_LUNS) { 3124 new |= ITASK_DEFAULT_HANDLING; 3125 } 3126 new &= ~ITASK_IN_TRANSITION; 3127 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 3128 itask->itask_worker_next = NULL; 3129 if (w->worker_task_tail) { 3130 w->worker_task_tail->itask_worker_next = itask; 3131 } else { 3132 w->worker_task_head = itask; 3133 } 3134 w->worker_task_tail = itask; 3135 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) { 3136 w->worker_max_qdepth_pu = w->worker_queue_depth; 3137 } 3138 atomic_add_32(&w->worker_ref_count, 1); 3139 itask->itask_cmd_stack[0] = ITASK_CMD_NEW_TASK; 3140 itask->itask_ncmds = 1; 3141 if (dbuf) { 3142 itask->itask_allocated_buf_map = 1; 3143 itask->itask_dbufs[0] = dbuf; 3144 dbuf->db_handle = 0; 3145 } else { 3146 itask->itask_allocated_buf_map = 0; 3147 itask->itask_dbufs[0] = NULL; 3148 } 3149 3150 stmf_update_kstat_lu_q(task, kstat_waitq_enter); 3151 stmf_update_kstat_lport_q(task, kstat_waitq_enter); 3152 3153 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 3154 cv_signal(&w->worker_cv); 3155 mutex_exit(&w->worker_lock); 3156 3157 /* 3158 * This can only happen if during stmf_task_alloc(), ILU_RESET_ACTIVE 3159 * was set between checking of ILU_RESET_ACTIVE and clearing of the 3160 * ITASK_IN_FREE_LIST flag. Take care of these "sneaked-in" tasks here. 3161 */ 3162 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 3163 stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ABORTED, NULL); 3164 } 3165 } 3166 3167 /* 3168 * ++++++++++++++ ABORT LOGIC ++++++++++++++++++++ 3169 * Once ITASK_BEING_ABORTED is set, ITASK_KNOWN_TO_LU can be reset already 3170 * i.e. before ITASK_BEING_ABORTED being set. But if it was not, it cannot 3171 * be reset until the LU explicitly calls stmf_task_lu_aborted(). Of course 3172 * the LU will make this call only if we call the LU's abort entry point. 3173 * we will only call that entry point if ITASK_KNOWN_TO_LU was set. 3174 * 3175 * Same logic applies for the port. 3176 * 3177 * Also ITASK_BEING_ABORTED will not be allowed to set if both KNOWN_TO_LU 3178 * and KNOWN_TO_TGT_PORT are reset. 3179 * 3180 * +++++++++++++++++++++++++++++++++++++++++++++++ 3181 */ 3182 3183 stmf_status_t 3184 stmf_xfer_data(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t ioflags) 3185 { 3186 stmf_status_t ret; 3187 3188 stmf_i_scsi_task_t *itask = 3189 (stmf_i_scsi_task_t *)task->task_stmf_private; 3190 3191 if (ioflags & STMF_IOF_LU_DONE) { 3192 uint32_t new, old; 3193 do { 3194 new = old = itask->itask_flags; 3195 if (new & ITASK_BEING_ABORTED) 3196 return (STMF_ABORTED); 3197 new &= ~ITASK_KNOWN_TO_LU; 3198 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 3199 } 3200 if (itask->itask_flags & ITASK_BEING_ABORTED) 3201 return (STMF_ABORTED); 3202 #ifdef DEBUG 3203 if (stmf_drop_buf_counter > 0) { 3204 if (atomic_add_32_nv((uint32_t *)&stmf_drop_buf_counter, -1) == 3205 1) 3206 return (STMF_SUCCESS); 3207 } 3208 #endif 3209 3210 stmf_update_kstat_lu_io(task, dbuf); 3211 stmf_update_kstat_lport_io(task, dbuf); 3212 3213 DTRACE_PROBE2(scsi__xfer__start, scsi_task_t *, task, 3214 stmf_data_buf_t *, dbuf); 3215 ret = task->task_lport->lport_xfer_data(task, dbuf, ioflags); 3216 DTRACE_PROBE2(scsi__xfer__end, scsi_task_t *, task, 3217 stmf_data_buf_t *, dbuf); 3218 return (ret); 3219 } 3220 3221 void 3222 stmf_data_xfer_done(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t iof) 3223 { 3224 stmf_i_scsi_task_t *itask = 3225 (stmf_i_scsi_task_t *)task->task_stmf_private; 3226 stmf_worker_t *w = itask->itask_worker; 3227 uint32_t new, old; 3228 uint8_t update_queue_flags, free_it, queue_it; 3229 3230 mutex_enter(&w->worker_lock); 3231 do { 3232 new = old = itask->itask_flags; 3233 if (old & ITASK_BEING_ABORTED) { 3234 mutex_exit(&w->worker_lock); 3235 return; 3236 } 3237 free_it = 0; 3238 if (iof & STMF_IOF_LPORT_DONE) { 3239 new &= ~ITASK_KNOWN_TO_TGT_PORT; 3240 task->task_completion_status = dbuf->db_xfer_status; 3241 free_it = 1; 3242 } 3243 /* 3244 * If the task is known to LU then queue it. But if 3245 * it is already queued (multiple completions) then 3246 * just update the buffer information by grabbing the 3247 * worker lock. If the task is not known to LU, 3248 * completed/aborted, then see if we need to 3249 * free this task. 3250 */ 3251 if (old & ITASK_KNOWN_TO_LU) { 3252 free_it = 0; 3253 update_queue_flags = 1; 3254 if (old & ITASK_IN_WORKER_QUEUE) { 3255 queue_it = 0; 3256 } else { 3257 queue_it = 1; 3258 new |= ITASK_IN_WORKER_QUEUE; 3259 } 3260 } else { 3261 update_queue_flags = 0; 3262 queue_it = 0; 3263 } 3264 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 3265 3266 if (update_queue_flags) { 3267 uint8_t cmd = (dbuf->db_handle << 5) | ITASK_CMD_DATA_XFER_DONE; 3268 3269 ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS); 3270 itask->itask_cmd_stack[itask->itask_ncmds++] = cmd; 3271 if (queue_it) { 3272 itask->itask_worker_next = NULL; 3273 if (w->worker_task_tail) { 3274 w->worker_task_tail->itask_worker_next = itask; 3275 } else { 3276 w->worker_task_head = itask; 3277 } 3278 w->worker_task_tail = itask; 3279 if (++(w->worker_queue_depth) > 3280 w->worker_max_qdepth_pu) { 3281 w->worker_max_qdepth_pu = w->worker_queue_depth; 3282 } 3283 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 3284 cv_signal(&w->worker_cv); 3285 } 3286 } 3287 mutex_exit(&w->worker_lock); 3288 3289 if (free_it) { 3290 if ((itask->itask_flags & (ITASK_KNOWN_TO_LU | 3291 ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE | 3292 ITASK_BEING_ABORTED)) == 0) { 3293 stmf_update_kstat_lu_q(task, kstat_runq_exit); 3294 stmf_update_kstat_lport_q(task, kstat_runq_exit); 3295 stmf_task_free(task); 3296 } 3297 } 3298 } 3299 3300 stmf_status_t 3301 stmf_send_scsi_status(scsi_task_t *task, uint32_t ioflags) 3302 { 3303 DTRACE_PROBE1(scsi__send__status, scsi_task_t *, task); 3304 3305 stmf_i_scsi_task_t *itask = 3306 (stmf_i_scsi_task_t *)task->task_stmf_private; 3307 if (ioflags & STMF_IOF_LU_DONE) { 3308 uint32_t new, old; 3309 do { 3310 new = old = itask->itask_flags; 3311 if (new & ITASK_BEING_ABORTED) 3312 return (STMF_ABORTED); 3313 new &= ~ITASK_KNOWN_TO_LU; 3314 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 3315 } 3316 3317 if (!(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT)) { 3318 return (STMF_SUCCESS); 3319 } 3320 3321 if (itask->itask_flags & ITASK_BEING_ABORTED) 3322 return (STMF_ABORTED); 3323 3324 if (task->task_additional_flags & TASK_AF_NO_EXPECTED_XFER_LENGTH) { 3325 task->task_status_ctrl = 0; 3326 task->task_resid = 0; 3327 } else if (task->task_cmd_xfer_length > 3328 task->task_expected_xfer_length) { 3329 task->task_status_ctrl = TASK_SCTRL_OVER; 3330 task->task_resid = task->task_cmd_xfer_length - 3331 task->task_expected_xfer_length; 3332 } else if (task->task_nbytes_transferred < 3333 task->task_expected_xfer_length) { 3334 task->task_status_ctrl = TASK_SCTRL_UNDER; 3335 task->task_resid = task->task_expected_xfer_length - 3336 task->task_nbytes_transferred; 3337 } else { 3338 task->task_status_ctrl = 0; 3339 task->task_resid = 0; 3340 } 3341 return (task->task_lport->lport_send_status(task, ioflags)); 3342 } 3343 3344 void 3345 stmf_send_status_done(scsi_task_t *task, stmf_status_t s, uint32_t iof) 3346 { 3347 stmf_i_scsi_task_t *itask = 3348 (stmf_i_scsi_task_t *)task->task_stmf_private; 3349 stmf_worker_t *w = itask->itask_worker; 3350 uint32_t new, old; 3351 uint8_t free_it, queue_it; 3352 3353 mutex_enter(&w->worker_lock); 3354 do { 3355 new = old = itask->itask_flags; 3356 if (old & ITASK_BEING_ABORTED) { 3357 mutex_exit(&w->worker_lock); 3358 return; 3359 } 3360 free_it = 0; 3361 if (iof & STMF_IOF_LPORT_DONE) { 3362 new &= ~ITASK_KNOWN_TO_TGT_PORT; 3363 free_it = 1; 3364 } 3365 /* 3366 * If the task is known to LU then queue it. But if 3367 * it is already queued (multiple completions) then 3368 * just update the buffer information by grabbing the 3369 * worker lock. If the task is not known to LU, 3370 * completed/aborted, then see if we need to 3371 * free this task. 3372 */ 3373 if (old & ITASK_KNOWN_TO_LU) { 3374 free_it = 0; 3375 queue_it = 1; 3376 if (old & ITASK_IN_WORKER_QUEUE) { 3377 cmn_err(CE_PANIC, "status completion received" 3378 " when task is already in worker queue " 3379 " task = %p", (void *)task); 3380 } 3381 new |= ITASK_IN_WORKER_QUEUE; 3382 } else { 3383 queue_it = 0; 3384 } 3385 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 3386 task->task_completion_status = s; 3387 3388 stmf_update_kstat_lu_q(task, kstat_runq_exit); 3389 stmf_update_kstat_lport_q(task, kstat_runq_exit); 3390 3391 if (queue_it) { 3392 ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS); 3393 itask->itask_cmd_stack[itask->itask_ncmds++] = 3394 ITASK_CMD_STATUS_DONE; 3395 itask->itask_worker_next = NULL; 3396 if (w->worker_task_tail) { 3397 w->worker_task_tail->itask_worker_next = itask; 3398 } else { 3399 w->worker_task_head = itask; 3400 } 3401 w->worker_task_tail = itask; 3402 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) { 3403 w->worker_max_qdepth_pu = w->worker_queue_depth; 3404 } 3405 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 3406 cv_signal(&w->worker_cv); 3407 } 3408 mutex_exit(&w->worker_lock); 3409 3410 if (free_it) { 3411 if ((itask->itask_flags & (ITASK_KNOWN_TO_LU | 3412 ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE | 3413 ITASK_BEING_ABORTED)) == 0) { 3414 stmf_task_free(task); 3415 } else { 3416 cmn_err(CE_PANIC, "LU is done with the task but LPORT " 3417 " is not done, itask %p", (void *)itask); 3418 } 3419 } 3420 } 3421 3422 void 3423 stmf_task_lu_done(scsi_task_t *task) 3424 { 3425 stmf_i_scsi_task_t *itask = 3426 (stmf_i_scsi_task_t *)task->task_stmf_private; 3427 stmf_worker_t *w = itask->itask_worker; 3428 uint32_t new, old; 3429 3430 mutex_enter(&w->worker_lock); 3431 do { 3432 new = old = itask->itask_flags; 3433 if (old & ITASK_BEING_ABORTED) { 3434 mutex_exit(&w->worker_lock); 3435 return; 3436 } 3437 if (old & ITASK_IN_WORKER_QUEUE) { 3438 cmn_err(CE_PANIC, "task_lu_done received" 3439 " when task is in worker queue " 3440 " task = %p", (void *)task); 3441 } 3442 new &= ~ITASK_KNOWN_TO_LU; 3443 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 3444 3445 mutex_exit(&w->worker_lock); 3446 3447 if ((itask->itask_flags & (ITASK_KNOWN_TO_LU | 3448 ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE | 3449 ITASK_BEING_ABORTED)) == 0) { 3450 stmf_task_free(task); 3451 } else { 3452 cmn_err(CE_PANIC, "stmf_lu_done should be the last stage but " 3453 " the task is still not done, task = %p", (void *)task); 3454 } 3455 } 3456 3457 void 3458 stmf_queue_task_for_abort(scsi_task_t *task, stmf_status_t s) 3459 { 3460 stmf_i_scsi_task_t *itask = 3461 (stmf_i_scsi_task_t *)task->task_stmf_private; 3462 stmf_worker_t *w; 3463 uint32_t old, new; 3464 3465 do { 3466 old = new = itask->itask_flags; 3467 if ((old & ITASK_BEING_ABORTED) || 3468 ((old & (ITASK_KNOWN_TO_TGT_PORT | 3469 ITASK_KNOWN_TO_LU)) == 0)) { 3470 return; 3471 } 3472 new |= ITASK_BEING_ABORTED; 3473 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 3474 task->task_completion_status = s; 3475 itask->itask_start_time = ddi_get_lbolt(); 3476 3477 if (((w = itask->itask_worker) == NULL) || 3478 (itask->itask_flags & ITASK_IN_TRANSITION)) { 3479 return; 3480 } 3481 3482 /* Queue it and get out */ 3483 mutex_enter(&w->worker_lock); 3484 if (itask->itask_flags & ITASK_IN_WORKER_QUEUE) { 3485 mutex_exit(&w->worker_lock); 3486 return; 3487 } 3488 atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE); 3489 itask->itask_worker_next = NULL; 3490 if (w->worker_task_tail) { 3491 w->worker_task_tail->itask_worker_next = itask; 3492 } else { 3493 w->worker_task_head = itask; 3494 } 3495 w->worker_task_tail = itask; 3496 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) { 3497 w->worker_max_qdepth_pu = w->worker_queue_depth; 3498 } 3499 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 3500 cv_signal(&w->worker_cv); 3501 mutex_exit(&w->worker_lock); 3502 } 3503 3504 void 3505 stmf_abort(int abort_cmd, scsi_task_t *task, stmf_status_t s, void *arg) 3506 { 3507 stmf_i_scsi_task_t *itask = NULL; 3508 uint32_t old, new, f, rf; 3509 3510 DTRACE_PROBE2(scsi__task__abort, scsi_task_t *, task, 3511 stmf_status_t, s); 3512 3513 switch (abort_cmd) { 3514 case STMF_QUEUE_ABORT_LU: 3515 stmf_task_lu_killall((stmf_lu_t *)arg, task, s); 3516 return; 3517 case STMF_QUEUE_TASK_ABORT: 3518 stmf_queue_task_for_abort(task, s); 3519 return; 3520 case STMF_REQUEUE_TASK_ABORT_LPORT: 3521 rf = ITASK_TGT_PORT_ABORT_CALLED; 3522 f = ITASK_KNOWN_TO_TGT_PORT; 3523 break; 3524 case STMF_REQUEUE_TASK_ABORT_LU: 3525 rf = ITASK_LU_ABORT_CALLED; 3526 f = ITASK_KNOWN_TO_LU; 3527 break; 3528 default: 3529 return; 3530 } 3531 itask = (stmf_i_scsi_task_t *)task->task_stmf_private; 3532 f |= ITASK_BEING_ABORTED | rf; 3533 do { 3534 old = new = itask->itask_flags; 3535 if ((old & f) != f) { 3536 return; 3537 } 3538 new &= ~rf; 3539 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 3540 } 3541 3542 void 3543 stmf_task_lu_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof) 3544 { 3545 char info[STMF_CHANGE_INFO_LEN]; 3546 stmf_i_scsi_task_t *itask = TASK_TO_ITASK(task); 3547 unsigned long long st; 3548 3549 st = s; /* gcc fix */ 3550 if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) { 3551 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 3552 "task %p, lu failed to abort ret=%llx", (void *)task, st); 3553 } else if ((iof & STMF_IOF_LU_DONE) == 0) { 3554 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 3555 "Task aborted but LU is not finished, task =" 3556 "%p, s=%llx, iof=%x", (void *)task, st, iof); 3557 } else { 3558 /* 3559 * LU abort successfully 3560 */ 3561 atomic_and_32(&itask->itask_flags, ~ITASK_KNOWN_TO_LU); 3562 return; 3563 } 3564 3565 info[STMF_CHANGE_INFO_LEN - 1] = 0; 3566 stmf_abort_task_offline(task, 1, info); 3567 } 3568 3569 void 3570 stmf_task_lport_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof) 3571 { 3572 char info[STMF_CHANGE_INFO_LEN]; 3573 stmf_i_scsi_task_t *itask = TASK_TO_ITASK(task); 3574 unsigned long long st; 3575 3576 st = s; 3577 if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) { 3578 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 3579 "task %p, tgt port failed to abort ret=%llx", (void *)task, 3580 st); 3581 } else if ((iof & STMF_IOF_LPORT_DONE) == 0) { 3582 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 3583 "Task aborted but tgt port is not finished, " 3584 "task=%p, s=%llx, iof=%x", (void *)task, st, iof); 3585 } else { 3586 /* 3587 * LU abort successfully 3588 */ 3589 atomic_and_32(&itask->itask_flags, ~ITASK_KNOWN_TO_TGT_PORT); 3590 return; 3591 } 3592 3593 info[STMF_CHANGE_INFO_LEN - 1] = 0; 3594 stmf_abort_task_offline(task, 0, info); 3595 } 3596 3597 stmf_status_t 3598 stmf_task_poll_lu(scsi_task_t *task, uint32_t timeout) 3599 { 3600 stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *) 3601 task->task_stmf_private; 3602 stmf_worker_t *w = itask->itask_worker; 3603 int i; 3604 3605 ASSERT(itask->itask_flags & ITASK_KNOWN_TO_LU); 3606 mutex_enter(&w->worker_lock); 3607 if (itask->itask_ncmds >= ITASK_MAX_NCMDS) { 3608 mutex_exit(&w->worker_lock); 3609 return (STMF_BUSY); 3610 } 3611 for (i = 0; i < itask->itask_ncmds; i++) { 3612 if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LU) { 3613 mutex_exit(&w->worker_lock); 3614 return (STMF_SUCCESS); 3615 } 3616 } 3617 itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LU; 3618 if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) { 3619 itask->itask_poll_timeout = ddi_get_lbolt() + 1; 3620 } else { 3621 clock_t t = drv_usectohz(timeout * 1000); 3622 if (t == 0) 3623 t = 1; 3624 itask->itask_poll_timeout = ddi_get_lbolt() + t; 3625 } 3626 if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) { 3627 itask->itask_worker_next = NULL; 3628 if (w->worker_task_tail) { 3629 w->worker_task_tail->itask_worker_next = itask; 3630 } else { 3631 w->worker_task_head = itask; 3632 } 3633 w->worker_task_tail = itask; 3634 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) { 3635 w->worker_max_qdepth_pu = w->worker_queue_depth; 3636 } 3637 atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE); 3638 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 3639 cv_signal(&w->worker_cv); 3640 } 3641 mutex_exit(&w->worker_lock); 3642 return (STMF_SUCCESS); 3643 } 3644 3645 stmf_status_t 3646 stmf_task_poll_lport(scsi_task_t *task, uint32_t timeout) 3647 { 3648 stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *) 3649 task->task_stmf_private; 3650 stmf_worker_t *w = itask->itask_worker; 3651 int i; 3652 3653 ASSERT(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT); 3654 mutex_enter(&w->worker_lock); 3655 if (itask->itask_ncmds >= ITASK_MAX_NCMDS) { 3656 mutex_exit(&w->worker_lock); 3657 return (STMF_BUSY); 3658 } 3659 for (i = 0; i < itask->itask_ncmds; i++) { 3660 if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LPORT) { 3661 mutex_exit(&w->worker_lock); 3662 return (STMF_SUCCESS); 3663 } 3664 } 3665 itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LPORT; 3666 if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) { 3667 itask->itask_poll_timeout = ddi_get_lbolt() + 1; 3668 } else { 3669 clock_t t = drv_usectohz(timeout * 1000); 3670 if (t == 0) 3671 t = 1; 3672 itask->itask_poll_timeout = ddi_get_lbolt() + t; 3673 } 3674 if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) { 3675 itask->itask_worker_next = NULL; 3676 if (w->worker_task_tail) { 3677 w->worker_task_tail->itask_worker_next = itask; 3678 } else { 3679 w->worker_task_head = itask; 3680 } 3681 w->worker_task_tail = itask; 3682 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) { 3683 w->worker_max_qdepth_pu = w->worker_queue_depth; 3684 } 3685 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 3686 cv_signal(&w->worker_cv); 3687 } 3688 mutex_exit(&w->worker_lock); 3689 return (STMF_SUCCESS); 3690 } 3691 3692 void 3693 stmf_do_task_abort(scsi_task_t *task) 3694 { 3695 stmf_i_scsi_task_t *itask = TASK_TO_ITASK(task); 3696 stmf_lu_t *lu; 3697 stmf_local_port_t *lport; 3698 unsigned long long ret; 3699 uint32_t old, new; 3700 uint8_t call_lu_abort, call_port_abort; 3701 char info[STMF_CHANGE_INFO_LEN]; 3702 3703 lu = task->task_lu; 3704 lport = task->task_lport; 3705 do { 3706 old = new = itask->itask_flags; 3707 if ((old & (ITASK_KNOWN_TO_LU | ITASK_LU_ABORT_CALLED)) == 3708 ITASK_KNOWN_TO_LU) { 3709 new |= ITASK_LU_ABORT_CALLED; 3710 call_lu_abort = 1; 3711 } else { 3712 call_lu_abort = 0; 3713 } 3714 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 3715 3716 if (call_lu_abort) { 3717 if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0) { 3718 ret = lu->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0); 3719 } else { 3720 ret = dlun0->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0); 3721 } 3722 if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) { 3723 stmf_task_lu_aborted(task, ret, STMF_IOF_LU_DONE); 3724 } else if (ret == STMF_BUSY) { 3725 atomic_and_32(&itask->itask_flags, 3726 ~ITASK_LU_ABORT_CALLED); 3727 } else if (ret != STMF_SUCCESS) { 3728 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 3729 "Abort failed by LU %p, ret %llx", (void *)lu, ret); 3730 info[STMF_CHANGE_INFO_LEN - 1] = 0; 3731 stmf_abort_task_offline(task, 1, info); 3732 } 3733 } else if (itask->itask_flags & ITASK_KNOWN_TO_LU) { 3734 if (ddi_get_lbolt() > (itask->itask_start_time + 3735 STMF_SEC2TICK(lu->lu_abort_timeout? 3736 lu->lu_abort_timeout : ITASK_DEFAULT_ABORT_TIMEOUT))) { 3737 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 3738 "lu abort timed out"); 3739 info[STMF_CHANGE_INFO_LEN - 1] = 0; 3740 stmf_abort_task_offline(itask->itask_task, 1, info); 3741 } 3742 } 3743 3744 do { 3745 old = new = itask->itask_flags; 3746 if ((old & (ITASK_KNOWN_TO_TGT_PORT | 3747 ITASK_TGT_PORT_ABORT_CALLED)) == ITASK_KNOWN_TO_TGT_PORT) { 3748 new |= ITASK_TGT_PORT_ABORT_CALLED; 3749 call_port_abort = 1; 3750 } else { 3751 call_port_abort = 0; 3752 } 3753 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 3754 if (call_port_abort) { 3755 ret = lport->lport_abort(lport, STMF_LPORT_ABORT_TASK, task, 0); 3756 if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) { 3757 stmf_task_lport_aborted(task, ret, STMF_IOF_LPORT_DONE); 3758 } else if (ret == STMF_BUSY) { 3759 atomic_and_32(&itask->itask_flags, 3760 ~ITASK_TGT_PORT_ABORT_CALLED); 3761 } else if (ret != STMF_SUCCESS) { 3762 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 3763 "Abort failed by tgt port %p ret %llx", 3764 (void *)lport, ret); 3765 info[STMF_CHANGE_INFO_LEN - 1] = 0; 3766 stmf_abort_task_offline(task, 0, info); 3767 } 3768 } else if (itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT) { 3769 if (ddi_get_lbolt() > (itask->itask_start_time + 3770 STMF_SEC2TICK(lport->lport_abort_timeout? 3771 lport->lport_abort_timeout : 3772 ITASK_DEFAULT_ABORT_TIMEOUT))) { 3773 (void) snprintf(info, STMF_CHANGE_INFO_LEN, 3774 "lport abort timed out"); 3775 info[STMF_CHANGE_INFO_LEN - 1] = 0; 3776 stmf_abort_task_offline(itask->itask_task, 0, info); 3777 } 3778 } 3779 } 3780 3781 stmf_status_t 3782 stmf_ctl(int cmd, void *obj, void *arg) 3783 { 3784 stmf_status_t ret; 3785 stmf_i_lu_t *ilu; 3786 stmf_i_local_port_t *ilport; 3787 stmf_state_change_info_t *ssci = (stmf_state_change_info_t *)arg; 3788 3789 mutex_enter(&stmf_state.stmf_lock); 3790 ret = STMF_INVALID_ARG; 3791 if (cmd & STMF_CMD_LU_OP) { 3792 ilu = stmf_lookup_lu((stmf_lu_t *)obj); 3793 if (ilu == NULL) { 3794 goto stmf_ctl_lock_exit; 3795 } 3796 DTRACE_PROBE3(lu__state__change, 3797 stmf_lu_t *, ilu->ilu_lu, 3798 int, cmd, stmf_state_change_info_t *, ssci); 3799 } else if (cmd & STMF_CMD_LPORT_OP) { 3800 ilport = stmf_lookup_lport((stmf_local_port_t *)obj); 3801 if (ilport == NULL) { 3802 goto stmf_ctl_lock_exit; 3803 } 3804 DTRACE_PROBE3(lport__state__change, 3805 stmf_local_port_t *, ilport->ilport_lport, 3806 int, cmd, stmf_state_change_info_t *, ssci); 3807 } else { 3808 goto stmf_ctl_lock_exit; 3809 } 3810 3811 switch (cmd) { 3812 case STMF_CMD_LU_ONLINE: 3813 if (ilu->ilu_state == STMF_STATE_ONLINE) { 3814 ret = STMF_ALREADY; 3815 goto stmf_ctl_lock_exit; 3816 } 3817 if (ilu->ilu_state != STMF_STATE_OFFLINE) { 3818 ret = STMF_INVALID_ARG; 3819 goto stmf_ctl_lock_exit; 3820 } 3821 ilu->ilu_state = STMF_STATE_ONLINING; 3822 mutex_exit(&stmf_state.stmf_lock); 3823 stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg); 3824 break; 3825 3826 case STMF_CMD_LU_ONLINE_COMPLETE: 3827 if (ilu->ilu_state != STMF_STATE_ONLINING) { 3828 ret = STMF_INVALID_ARG; 3829 goto stmf_ctl_lock_exit; 3830 } 3831 if (((stmf_change_status_t *)arg)->st_completion_status == 3832 STMF_SUCCESS) { 3833 ilu->ilu_state = STMF_STATE_ONLINE; 3834 mutex_exit(&stmf_state.stmf_lock); 3835 ((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj, 3836 STMF_ACK_LU_ONLINE_COMPLETE, arg); 3837 mutex_enter(&stmf_state.stmf_lock); 3838 stmf_add_lu_to_active_sessions((stmf_lu_t *)obj); 3839 } else { 3840 /* XXX: should throw a meesage an record more data */ 3841 ilu->ilu_state = STMF_STATE_OFFLINE; 3842 } 3843 ret = STMF_SUCCESS; 3844 goto stmf_ctl_lock_exit; 3845 3846 case STMF_CMD_LU_OFFLINE: 3847 if (ilu->ilu_state == STMF_STATE_OFFLINE) { 3848 ret = STMF_ALREADY; 3849 goto stmf_ctl_lock_exit; 3850 } 3851 if (ilu->ilu_state != STMF_STATE_ONLINE) { 3852 ret = STMF_INVALID_ARG; 3853 goto stmf_ctl_lock_exit; 3854 } 3855 ilu->ilu_state = STMF_STATE_OFFLINING; 3856 mutex_exit(&stmf_state.stmf_lock); 3857 stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg); 3858 break; 3859 3860 case STMF_CMD_LU_OFFLINE_COMPLETE: 3861 if (ilu->ilu_state != STMF_STATE_OFFLINING) { 3862 ret = STMF_INVALID_ARG; 3863 goto stmf_ctl_lock_exit; 3864 } 3865 if (((stmf_change_status_t *)arg)->st_completion_status == 3866 STMF_SUCCESS) { 3867 ilu->ilu_state = STMF_STATE_OFFLINE; 3868 mutex_exit(&stmf_state.stmf_lock); 3869 ((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj, 3870 STMF_ACK_LU_OFFLINE_COMPLETE, arg); 3871 mutex_enter(&stmf_state.stmf_lock); 3872 } else { 3873 ilu->ilu_state = STMF_STATE_ONLINE; 3874 stmf_add_lu_to_active_sessions((stmf_lu_t *)obj); 3875 } 3876 mutex_exit(&stmf_state.stmf_lock); 3877 break; 3878 3879 /* 3880 * LPORT_ONLINE/OFFLINE has nothing to do with link offline/online. 3881 * It's related with hardware disable/enable. 3882 */ 3883 case STMF_CMD_LPORT_ONLINE: 3884 if (ilport->ilport_state == STMF_STATE_ONLINE) { 3885 ret = STMF_ALREADY; 3886 goto stmf_ctl_lock_exit; 3887 } 3888 if (ilport->ilport_state != STMF_STATE_OFFLINE) { 3889 ret = STMF_INVALID_ARG; 3890 goto stmf_ctl_lock_exit; 3891 } 3892 3893 /* 3894 * Only user request can recover the port from the 3895 * FORCED_OFFLINE state 3896 */ 3897 if (ilport->ilport_flags & ILPORT_FORCED_OFFLINE) { 3898 if (!(ssci->st_rflags & STMF_RFLAG_USER_REQUEST)) { 3899 ret = STMF_FAILURE; 3900 goto stmf_ctl_lock_exit; 3901 } 3902 } 3903 3904 /* 3905 * Avoid too frequent request to online 3906 */ 3907 if (ssci->st_rflags & STMF_RFLAG_USER_REQUEST) { 3908 ilport->ilport_online_times = 0; 3909 ilport->ilport_avg_interval = 0; 3910 } 3911 if ((ilport->ilport_avg_interval < STMF_AVG_ONLINE_INTERVAL) && 3912 (ilport->ilport_online_times >= 4)) { 3913 ret = STMF_FAILURE; 3914 ilport->ilport_flags |= ILPORT_FORCED_OFFLINE; 3915 stmf_trace(NULL, "stmf_ctl: too frequent request to " 3916 "online the port"); 3917 cmn_err(CE_WARN, "stmf_ctl: too frequent request to " 3918 "online the port, set FORCED_OFFLINE now"); 3919 goto stmf_ctl_lock_exit; 3920 } 3921 if (ilport->ilport_online_times > 0) { 3922 if (ilport->ilport_online_times == 1) { 3923 ilport->ilport_avg_interval = ddi_get_lbolt() - 3924 ilport->ilport_last_online_clock; 3925 } else { 3926 ilport->ilport_avg_interval = 3927 (ilport->ilport_avg_interval + 3928 ddi_get_lbolt() - 3929 ilport->ilport_last_online_clock) >> 1; 3930 } 3931 } 3932 ilport->ilport_last_online_clock = ddi_get_lbolt(); 3933 ilport->ilport_online_times++; 3934 3935 /* 3936 * Submit online service request 3937 */ 3938 ilport->ilport_flags &= ~ILPORT_FORCED_OFFLINE; 3939 ilport->ilport_state = STMF_STATE_ONLINING; 3940 mutex_exit(&stmf_state.stmf_lock); 3941 stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg); 3942 break; 3943 3944 case STMF_CMD_LPORT_ONLINE_COMPLETE: 3945 if (ilport->ilport_state != STMF_STATE_ONLINING) { 3946 ret = STMF_INVALID_ARG; 3947 goto stmf_ctl_lock_exit; 3948 } 3949 if (((stmf_change_status_t *)arg)->st_completion_status == 3950 STMF_SUCCESS) { 3951 ilport->ilport_state = STMF_STATE_ONLINE; 3952 mutex_exit(&stmf_state.stmf_lock); 3953 ((stmf_local_port_t *)obj)->lport_ctl( 3954 (stmf_local_port_t *)obj, 3955 STMF_ACK_LPORT_ONLINE_COMPLETE, arg); 3956 mutex_enter(&stmf_state.stmf_lock); 3957 } else { 3958 ilport->ilport_state = STMF_STATE_OFFLINE; 3959 } 3960 ret = STMF_SUCCESS; 3961 goto stmf_ctl_lock_exit; 3962 3963 case STMF_CMD_LPORT_OFFLINE: 3964 if (ilport->ilport_state == STMF_STATE_OFFLINE) { 3965 ret = STMF_ALREADY; 3966 goto stmf_ctl_lock_exit; 3967 } 3968 if (ilport->ilport_state != STMF_STATE_ONLINE) { 3969 ret = STMF_INVALID_ARG; 3970 goto stmf_ctl_lock_exit; 3971 } 3972 ilport->ilport_state = STMF_STATE_OFFLINING; 3973 mutex_exit(&stmf_state.stmf_lock); 3974 stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg); 3975 break; 3976 3977 case STMF_CMD_LPORT_OFFLINE_COMPLETE: 3978 if (ilport->ilport_state != STMF_STATE_OFFLINING) { 3979 ret = STMF_INVALID_ARG; 3980 goto stmf_ctl_lock_exit; 3981 } 3982 if (((stmf_change_status_t *)arg)->st_completion_status == 3983 STMF_SUCCESS) { 3984 ilport->ilport_state = STMF_STATE_OFFLINE; 3985 mutex_exit(&stmf_state.stmf_lock); 3986 ((stmf_local_port_t *)obj)->lport_ctl( 3987 (stmf_local_port_t *)obj, 3988 STMF_ACK_LPORT_OFFLINE_COMPLETE, arg); 3989 mutex_enter(&stmf_state.stmf_lock); 3990 } else { 3991 ilport->ilport_state = STMF_STATE_ONLINE; 3992 } 3993 mutex_exit(&stmf_state.stmf_lock); 3994 break; 3995 3996 default: 3997 cmn_err(CE_WARN, "Invalid ctl cmd received %x", cmd); 3998 ret = STMF_INVALID_ARG; 3999 goto stmf_ctl_lock_exit; 4000 } 4001 4002 return (STMF_SUCCESS); 4003 4004 stmf_ctl_lock_exit:; 4005 mutex_exit(&stmf_state.stmf_lock); 4006 return (ret); 4007 } 4008 4009 /* ARGSUSED */ 4010 stmf_status_t 4011 stmf_info_impl(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf, 4012 uint32_t *bufsizep) 4013 { 4014 return (STMF_NOT_SUPPORTED); 4015 } 4016 4017 /* ARGSUSED */ 4018 stmf_status_t 4019 stmf_info(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf, 4020 uint32_t *bufsizep) 4021 { 4022 uint32_t cl = SI_GET_CLASS(cmd); 4023 4024 if (cl == SI_STMF) { 4025 return (stmf_info_impl(cmd, arg1, arg2, buf, bufsizep)); 4026 } 4027 if (cl == SI_LPORT) { 4028 return (((stmf_local_port_t *)arg1)->lport_info(cmd, arg1, 4029 arg2, buf, bufsizep)); 4030 } else if (cl == SI_LU) { 4031 return (((stmf_lu_t *)arg1)->lu_info(cmd, arg1, arg2, buf, 4032 bufsizep)); 4033 } 4034 4035 return (STMF_NOT_SUPPORTED); 4036 } 4037 4038 /* 4039 * Used by port providers. pwwn is 8 byte wwn, sdid is th devid used by 4040 * stmf to register local ports. The ident should have 20 bytes in buffer 4041 * space to convert the wwn to "wwn.xxxxxxxxxxxxxxxx" string. 4042 */ 4043 void 4044 stmf_wwn_to_devid_desc(scsi_devid_desc_t *sdid, uint8_t *wwn, 4045 uint8_t protocol_id) 4046 { 4047 sdid->protocol_id = protocol_id; 4048 sdid->piv = 1; 4049 sdid->code_set = CODE_SET_ASCII; 4050 sdid->association = ID_IS_TARGET_PORT; 4051 sdid->ident_length = 20; 4052 (void) sprintf((char *)sdid->ident, 4053 "wwn.%02X%02X%02X%02X%02X%02X%02X%02X", 4054 wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]); 4055 } 4056 4057 stmf_xfer_data_t * 4058 stmf_prepare_tpgs_data() 4059 { 4060 stmf_xfer_data_t *xd; 4061 stmf_i_local_port_t *ilport; 4062 uint8_t *p; 4063 uint32_t sz, asz, nports; 4064 4065 mutex_enter(&stmf_state.stmf_lock); 4066 /* The spec only allows for 255 ports to be reported */ 4067 nports = min(stmf_state.stmf_nlports, 255); 4068 sz = (nports * 4) + 12; 4069 asz = sz + sizeof (*xd) - 4; 4070 xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP); 4071 if (xd == NULL) { 4072 mutex_exit(&stmf_state.stmf_lock); 4073 return (NULL); 4074 } 4075 xd->alloc_size = asz; 4076 xd->size_left = sz; 4077 4078 p = xd->buf; 4079 4080 *((uint32_t *)p) = BE_32(sz - 4); 4081 p += 4; 4082 p[0] = 0x80; /* PREF */ 4083 p[1] = 1; /* AO_SUP */ 4084 p[7] = nports & 0xff; 4085 p += 8; 4086 for (ilport = stmf_state.stmf_ilportlist; ilport && nports; 4087 nports++, ilport = ilport->ilport_next, p += 4) { 4088 ((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid); 4089 } 4090 mutex_exit(&stmf_state.stmf_lock); 4091 4092 return (xd); 4093 } 4094 4095 4096 static uint16_t stmf_lu_id_gen_number = 0; 4097 4098 stmf_status_t 4099 stmf_scsilib_uniq_lu_id(uint32_t company_id, scsi_devid_desc_t *lu_id) 4100 { 4101 uint8_t *p; 4102 struct timeval32 timestamp32; 4103 uint32_t *t = (uint32_t *)×tamp32; 4104 struct ether_addr mac; 4105 uint8_t *e = (uint8_t *)&mac; 4106 4107 if (company_id == COMPANY_ID_NONE) 4108 company_id = COMPANY_ID_SUN; 4109 4110 if (lu_id->ident_length != 0x10) 4111 return (STMF_INVALID_ARG); 4112 4113 p = (uint8_t *)lu_id; 4114 4115 atomic_add_16(&stmf_lu_id_gen_number, 1); 4116 4117 p[0] = 0xf1; p[1] = 3; p[2] = 0; p[3] = 0x10; 4118 p[4] = ((company_id >> 20) & 0xf) | 0x60; 4119 p[5] = (company_id >> 12) & 0xff; 4120 p[6] = (company_id >> 4) & 0xff; 4121 p[7] = (company_id << 4) & 0xf0; 4122 if (!localetheraddr((struct ether_addr *)NULL, &mac)) { 4123 int hid = BE_32((int)zone_get_hostid(NULL)); 4124 4125 e[0] = (hid >> 24) & 0xff; 4126 e[1] = (hid >> 16) & 0xff; 4127 e[2] = (hid >> 8) & 0xff; 4128 e[3] = hid & 0xff; 4129 e[4] = e[5] = 0; 4130 } 4131 bcopy(e, p+8, 6); 4132 uniqtime32(×tamp32); 4133 *t = BE_32(*t); 4134 bcopy(t, p+14, 4); 4135 p[18] = (stmf_lu_id_gen_number >> 8) & 0xff; 4136 p[19] = stmf_lu_id_gen_number & 0xff; 4137 4138 return (STMF_SUCCESS); 4139 } 4140 4141 /* 4142 * saa is sense key, ASC, ASCQ 4143 */ 4144 void 4145 stmf_scsilib_send_status(scsi_task_t *task, uint8_t st, uint32_t saa) 4146 { 4147 uint8_t sd[18]; 4148 task->task_scsi_status = st; 4149 if (st == 2) { 4150 bzero(sd, 18); 4151 sd[0] = 0x70; 4152 sd[2] = (saa >> 16) & 0xf; 4153 sd[7] = 10; 4154 sd[12] = (saa >> 8) & 0xff; 4155 sd[13] = saa & 0xff; 4156 task->task_sense_data = sd; 4157 task->task_sense_length = 18; 4158 } else { 4159 task->task_sense_data = NULL; 4160 task->task_sense_length = 0; 4161 } 4162 (void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE); 4163 } 4164 4165 uint32_t 4166 stmf_scsilib_prepare_vpd_page83(scsi_task_t *task, uint8_t *page, 4167 uint32_t page_len, uint8_t byte0, uint32_t vpd_mask) 4168 { 4169 uint8_t *p = NULL; 4170 uint8_t small_buf[32]; 4171 uint32_t sz = 0; 4172 uint32_t n = 4; 4173 uint32_t m = 0; 4174 uint32_t last_bit = 0; 4175 4176 if (page_len < 4) 4177 return (0); 4178 if (page_len > 65535) 4179 page_len = 65535; 4180 4181 page[0] = byte0; 4182 page[1] = 0x83; 4183 4184 /* CONSTCOND */ 4185 while (1) { 4186 m += sz; 4187 if (sz && (page_len > n)) { 4188 uint32_t copysz; 4189 copysz = page_len > (n + sz) ? sz : page_len - n; 4190 bcopy(p, page + n, copysz); 4191 n += copysz; 4192 } 4193 vpd_mask &= ~last_bit; 4194 if (vpd_mask == 0) 4195 break; 4196 4197 if (vpd_mask & STMF_VPD_LU_ID) { 4198 last_bit = STMF_VPD_LU_ID; 4199 sz = task->task_lu->lu_id->ident_length + 4; 4200 p = (uint8_t *)task->task_lu->lu_id; 4201 continue; 4202 } else if (vpd_mask & STMF_VPD_TARGET_ID) { 4203 last_bit = STMF_VPD_TARGET_ID; 4204 sz = task->task_lport->lport_id->ident_length + 4; 4205 p = (uint8_t *)task->task_lport->lport_id; 4206 continue; 4207 } else if (vpd_mask & STMF_VPD_TP_GROUP) { 4208 last_bit = STMF_VPD_TP_GROUP; 4209 p = small_buf; 4210 bzero(p, 8); 4211 p[0] = 1; 4212 p[1] = 0x15; 4213 p[3] = 4; 4214 /* Group ID is always 0 */ 4215 sz = 8; 4216 continue; 4217 } else if (vpd_mask & STMF_VPD_RELATIVE_TP_ID) { 4218 stmf_i_local_port_t *ilport; 4219 4220 last_bit = STMF_VPD_RELATIVE_TP_ID; 4221 p = small_buf; 4222 bzero(p, 8); 4223 p[0] = 1; 4224 p[1] = 0x14; 4225 p[3] = 4; 4226 ilport = (stmf_i_local_port_t *) 4227 task->task_lport->lport_stmf_private; 4228 p[6] = (ilport->ilport_rtpid >> 8) & 0xff; 4229 p[7] = ilport->ilport_rtpid & 0xff; 4230 sz = 8; 4231 continue; 4232 } else { 4233 cmn_err(CE_WARN, "Invalid vpd_mask"); 4234 break; 4235 } 4236 } 4237 4238 page[2] = (m >> 8) & 0xff; 4239 page[3] = m & 0xff; 4240 4241 return (n); 4242 } 4243 4244 void 4245 stmf_scsilib_handle_report_tpgs(scsi_task_t *task, stmf_data_buf_t *dbuf) 4246 { 4247 stmf_i_scsi_task_t *itask = 4248 (stmf_i_scsi_task_t *)task->task_stmf_private; 4249 stmf_xfer_data_t *xd; 4250 uint32_t sz, minsz; 4251 4252 itask->itask_flags |= ITASK_DEFAULT_HANDLING; 4253 task->task_cmd_xfer_length = 4254 ((((uint32_t)task->task_cdb[6]) << 24) | 4255 (((uint32_t)task->task_cdb[7]) << 16) | 4256 (((uint32_t)task->task_cdb[8]) << 8) | 4257 ((uint32_t)task->task_cdb[9])); 4258 4259 if (task->task_additional_flags & 4260 TASK_AF_NO_EXPECTED_XFER_LENGTH) { 4261 task->task_expected_xfer_length = 4262 task->task_cmd_xfer_length; 4263 } 4264 4265 if (task->task_cmd_xfer_length < 16) { 4266 stmf_scsilib_send_status(task, STATUS_CHECK, 4267 STMF_SAA_INVALID_FIELD_IN_CDB); 4268 return; 4269 } 4270 4271 sz = min(task->task_expected_xfer_length, 4272 task->task_cmd_xfer_length); 4273 4274 xd = stmf_prepare_tpgs_data(); 4275 4276 if (xd == NULL) { 4277 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 4278 STMF_ALLOC_FAILURE, NULL); 4279 return; 4280 } 4281 4282 sz = min(sz, xd->size_left); 4283 xd->size_left = sz; 4284 minsz = min(512, sz); 4285 4286 if (dbuf == NULL) 4287 dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0); 4288 if (dbuf == NULL) { 4289 kmem_free(xd, xd->alloc_size); 4290 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 4291 STMF_ALLOC_FAILURE, NULL); 4292 return; 4293 } 4294 dbuf->db_lu_private = xd; 4295 stmf_xd_to_dbuf(dbuf); 4296 4297 dbuf->db_flags = DB_DIRECTION_TO_RPORT; 4298 (void) stmf_xfer_data(task, dbuf, 0); 4299 4300 } 4301 4302 void 4303 stmf_scsilib_handle_task_mgmt(scsi_task_t *task) 4304 { 4305 switch (task->task_mgmt_function) { 4306 /* 4307 * For now we will abort all I/Os on the LU in case of ABORT_TASK_SET 4308 * and ABORT_TASK. But unlike LUN_RESET we will not reset LU state 4309 * in these cases. This needs to be changed to abort only the required 4310 * set. 4311 */ 4312 case TM_ABORT_TASK: 4313 case TM_ABORT_TASK_SET: 4314 case TM_CLEAR_TASK_SET: 4315 case TM_LUN_RESET: 4316 stmf_handle_lun_reset(task); 4317 return; 4318 case TM_TARGET_RESET: 4319 case TM_TARGET_COLD_RESET: 4320 case TM_TARGET_WARM_RESET: 4321 stmf_handle_target_reset(task); 4322 return; 4323 default: 4324 /* We dont support this task mgmt function */ 4325 stmf_scsilib_send_status(task, STATUS_CHECK, 4326 STMF_SAA_INVALID_FIELD_IN_CMD_IU); 4327 return; 4328 } 4329 } 4330 4331 void 4332 stmf_handle_lun_reset(scsi_task_t *task) 4333 { 4334 stmf_i_scsi_task_t *itask; 4335 stmf_i_lu_t *ilu; 4336 4337 itask = (stmf_i_scsi_task_t *)task->task_stmf_private; 4338 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 4339 4340 /* 4341 * To sync with target reset, grab this lock. The LU is not going 4342 * anywhere as there is atleast one task pending (this task). 4343 */ 4344 mutex_enter(&stmf_state.stmf_lock); 4345 4346 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 4347 mutex_exit(&stmf_state.stmf_lock); 4348 stmf_scsilib_send_status(task, STATUS_CHECK, 4349 STMF_SAA_OPERATION_IN_PROGRESS); 4350 return; 4351 } 4352 atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE); 4353 mutex_exit(&stmf_state.stmf_lock); 4354 4355 /* 4356 * Mark this task as the one causing LU reset so that we know who 4357 * was responsible for setting the ILU_RESET_ACTIVE. In case this 4358 * task itself gets aborted, we will clear ILU_RESET_ACTIVE. 4359 */ 4360 itask->itask_flags |= ITASK_DEFAULT_HANDLING | ITASK_CAUSING_LU_RESET; 4361 4362 /* Initiatiate abort on all commands on this LU except this one */ 4363 stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED, task->task_lu); 4364 4365 /* Start polling on this task */ 4366 if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT) 4367 != STMF_SUCCESS) { 4368 stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE, 4369 NULL); 4370 return; 4371 } 4372 } 4373 4374 void 4375 stmf_handle_target_reset(scsi_task_t *task) 4376 { 4377 stmf_i_scsi_task_t *itask; 4378 stmf_i_lu_t *ilu; 4379 stmf_i_scsi_session_t *iss; 4380 stmf_lun_map_t *lm; 4381 stmf_lun_map_ent_t *lm_ent; 4382 int i, lf; 4383 4384 itask = (stmf_i_scsi_task_t *)task->task_stmf_private; 4385 iss = (stmf_i_scsi_session_t *)task->task_session->ss_stmf_private; 4386 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 4387 4388 /* 4389 * To sync with LUN reset, grab this lock. The session is not going 4390 * anywhere as there is atleast one task pending (this task). 4391 */ 4392 mutex_enter(&stmf_state.stmf_lock); 4393 4394 /* Grab the session lock as a writer to prevent any changes in it */ 4395 rw_enter(iss->iss_lockp, RW_WRITER); 4396 4397 if (iss->iss_flags & ISS_RESET_ACTIVE) { 4398 rw_exit(iss->iss_lockp); 4399 mutex_exit(&stmf_state.stmf_lock); 4400 stmf_scsilib_send_status(task, STATUS_CHECK, 4401 STMF_SAA_OPERATION_IN_PROGRESS); 4402 return; 4403 } 4404 atomic_or_32(&iss->iss_flags, ISS_RESET_ACTIVE); 4405 4406 /* 4407 * Now go through each LUN in this session and make sure all of them 4408 * can be reset. 4409 */ 4410 lm = iss->iss_sm; 4411 for (i = 0, lf = 0; i < lm->lm_nentries; i++) { 4412 if (lm->lm_plus[i] == NULL) 4413 continue; 4414 lf++; 4415 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 4416 ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private); 4417 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 4418 atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE); 4419 rw_exit(iss->iss_lockp); 4420 mutex_exit(&stmf_state.stmf_lock); 4421 stmf_scsilib_send_status(task, STATUS_CHECK, 4422 STMF_SAA_OPERATION_IN_PROGRESS); 4423 return; 4424 } 4425 } 4426 if (lf == 0) { 4427 /* No luns in this session */ 4428 atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE); 4429 rw_exit(iss->iss_lockp); 4430 mutex_exit(&stmf_state.stmf_lock); 4431 stmf_scsilib_send_status(task, STATUS_GOOD, 0); 4432 return; 4433 } 4434 4435 /* ok, start the damage */ 4436 itask->itask_flags |= ITASK_DEFAULT_HANDLING | 4437 ITASK_CAUSING_TARGET_RESET; 4438 for (i = 0; i < lm->lm_nentries; i++) { 4439 if (lm->lm_plus[i] == NULL) 4440 continue; 4441 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 4442 ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private); 4443 atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE); 4444 } 4445 rw_exit(iss->iss_lockp); 4446 mutex_exit(&stmf_state.stmf_lock); 4447 4448 for (i = 0; i < lm->lm_nentries; i++) { 4449 if (lm->lm_plus[i] == NULL) 4450 continue; 4451 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 4452 stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED, 4453 lm_ent->ent_lu); 4454 } 4455 4456 /* Start polling on this task */ 4457 if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT) 4458 != STMF_SUCCESS) { 4459 stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE, 4460 NULL); 4461 return; 4462 } 4463 } 4464 4465 int 4466 stmf_handle_cmd_during_ic(stmf_i_scsi_task_t *itask) 4467 { 4468 scsi_task_t *task = itask->itask_task; 4469 stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *) 4470 task->task_session->ss_stmf_private; 4471 4472 rw_enter(iss->iss_lockp, RW_WRITER); 4473 if (((iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) == 0) || 4474 (task->task_cdb[0] == SCMD_INQUIRY)) { 4475 rw_exit(iss->iss_lockp); 4476 return (0); 4477 } 4478 atomic_and_32(&iss->iss_flags, 4479 ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS)); 4480 rw_exit(iss->iss_lockp); 4481 4482 if (task->task_cdb[0] == SCMD_REPORT_LUNS) { 4483 return (0); 4484 } 4485 stmf_scsilib_send_status(task, STATUS_CHECK, 4486 STMF_SAA_REPORT_LUN_DATA_HAS_CHANGED); 4487 return (1); 4488 } 4489 4490 void 4491 stmf_worker_init() 4492 { 4493 uint32_t i; 4494 4495 /* Make local copy of global tunables */ 4496 stmf_i_max_nworkers = stmf_max_nworkers; 4497 stmf_i_min_nworkers = stmf_min_nworkers; 4498 4499 ASSERT(stmf_workers == NULL); 4500 if (stmf_i_min_nworkers < 4) { 4501 stmf_i_min_nworkers = 4; 4502 } 4503 if (stmf_i_max_nworkers < stmf_i_min_nworkers) { 4504 stmf_i_max_nworkers = stmf_i_min_nworkers; 4505 } 4506 stmf_workers = (stmf_worker_t *)kmem_zalloc( 4507 sizeof (stmf_worker_t) * stmf_i_max_nworkers, KM_SLEEP); 4508 for (i = 0; i < stmf_i_max_nworkers; i++) { 4509 stmf_worker_t *w = &stmf_workers[i]; 4510 mutex_init(&w->worker_lock, NULL, MUTEX_DRIVER, NULL); 4511 cv_init(&w->worker_cv, NULL, CV_DRIVER, NULL); 4512 } 4513 stmf_worker_mgmt_delay = drv_usectohz(20 * 1000); 4514 stmf_workers_state = STMF_WORKERS_ENABLED; 4515 4516 /* Workers will be started by stmf_worker_mgmt() */ 4517 4518 /* Lets wait for atleast one worker to start */ 4519 while (stmf_nworkers_cur == 0) 4520 delay(drv_usectohz(20 * 1000)); 4521 stmf_worker_mgmt_delay = drv_usectohz(3 * 1000 * 1000); 4522 } 4523 4524 stmf_status_t 4525 stmf_worker_fini() 4526 { 4527 int i; 4528 clock_t sb; 4529 4530 if (stmf_workers_state == STMF_WORKERS_DISABLED) 4531 return (STMF_SUCCESS); 4532 ASSERT(stmf_workers); 4533 stmf_workers_state = STMF_WORKERS_DISABLED; 4534 stmf_worker_mgmt_delay = drv_usectohz(20 * 1000); 4535 cv_signal(&stmf_state.stmf_cv); 4536 4537 sb = ddi_get_lbolt() + drv_usectohz(10 * 1000 * 1000); 4538 /* Wait for all the threads to die */ 4539 while (stmf_nworkers_cur != 0) { 4540 if (ddi_get_lbolt() > sb) { 4541 stmf_workers_state = STMF_WORKERS_ENABLED; 4542 return (STMF_BUSY); 4543 } 4544 delay(drv_usectohz(100 * 1000)); 4545 } 4546 for (i = 0; i < stmf_i_max_nworkers; i++) { 4547 stmf_worker_t *w = &stmf_workers[i]; 4548 mutex_destroy(&w->worker_lock); 4549 cv_destroy(&w->worker_cv); 4550 } 4551 kmem_free(stmf_workers, sizeof (stmf_worker_t) * stmf_i_max_nworkers); 4552 stmf_workers = NULL; 4553 4554 return (STMF_SUCCESS); 4555 } 4556 4557 void 4558 stmf_worker_task(void *arg) 4559 { 4560 stmf_worker_t *w; 4561 stmf_i_scsi_session_t *iss; 4562 scsi_task_t *task; 4563 stmf_i_scsi_task_t *itask; 4564 stmf_data_buf_t *dbuf; 4565 stmf_lu_t *lu; 4566 clock_t wait_timer = 0; 4567 clock_t wait_ticks; 4568 uint32_t old, new; 4569 uint8_t curcmd; 4570 uint8_t abort_free; 4571 uint8_t wait_queue; 4572 uint8_t dec_qdepth; 4573 4574 w = (stmf_worker_t *)arg; 4575 wait_ticks = drv_usectohz(10000); 4576 4577 mutex_enter(&w->worker_lock); 4578 w->worker_flags |= STMF_WORKER_STARTED | STMF_WORKER_ACTIVE; 4579 stmf_worker_loop:; 4580 if ((w->worker_ref_count == 0) && 4581 (w->worker_flags & STMF_WORKER_TERMINATE)) { 4582 w->worker_flags &= ~(STMF_WORKER_STARTED | 4583 STMF_WORKER_ACTIVE | STMF_WORKER_TERMINATE); 4584 w->worker_tid = NULL; 4585 mutex_exit(&w->worker_lock); 4586 thread_exit(); 4587 } 4588 /* CONSTCOND */ 4589 while (1) { 4590 dec_qdepth = 0; 4591 if (wait_timer && (ddi_get_lbolt() >= wait_timer)) { 4592 wait_timer = 0; 4593 if (w->worker_wait_head) { 4594 ASSERT(w->worker_wait_tail); 4595 if (w->worker_task_head == NULL) 4596 w->worker_task_head = 4597 w->worker_wait_head; 4598 else 4599 w->worker_task_tail->itask_worker_next = 4600 w->worker_wait_head; 4601 w->worker_task_tail = w->worker_wait_tail; 4602 w->worker_wait_head = w->worker_wait_tail = 4603 NULL; 4604 } 4605 } 4606 if ((itask = w->worker_task_head) == NULL) { 4607 break; 4608 } 4609 task = itask->itask_task; 4610 w->worker_task_head = itask->itask_worker_next; 4611 if (w->worker_task_head == NULL) 4612 w->worker_task_tail = NULL; 4613 4614 wait_queue = 0; 4615 abort_free = 0; 4616 if (itask->itask_ncmds > 0) { 4617 curcmd = itask->itask_cmd_stack[itask->itask_ncmds - 1]; 4618 } else { 4619 ASSERT(itask->itask_flags & ITASK_BEING_ABORTED); 4620 } 4621 do { 4622 old = itask->itask_flags; 4623 if (old & ITASK_BEING_ABORTED) { 4624 itask->itask_ncmds = 1; 4625 curcmd = itask->itask_cmd_stack[0] = 4626 ITASK_CMD_ABORT; 4627 goto out_itask_flag_loop; 4628 } else if ((curcmd & ITASK_CMD_MASK) == 4629 ITASK_CMD_NEW_TASK) { 4630 new = old | ITASK_KNOWN_TO_LU; 4631 } else { 4632 goto out_itask_flag_loop; 4633 } 4634 } while (atomic_cas_32(&itask->itask_flags, old, new) != old); 4635 4636 out_itask_flag_loop: 4637 4638 /* 4639 * Decide if this task needs to go to a queue and/or if 4640 * we can decrement the itask_cmd_stack. 4641 */ 4642 if (curcmd == ITASK_CMD_ABORT) { 4643 if (itask->itask_flags & (ITASK_KNOWN_TO_LU | 4644 ITASK_KNOWN_TO_TGT_PORT)) { 4645 wait_queue = 1; 4646 } else { 4647 abort_free = 1; 4648 } 4649 } else if ((curcmd & ITASK_CMD_POLL) && 4650 (itask->itask_poll_timeout > ddi_get_lbolt())) { 4651 wait_queue = 1; 4652 } 4653 4654 if (wait_queue) { 4655 itask->itask_worker_next = NULL; 4656 if (w->worker_wait_tail) { 4657 w->worker_wait_tail->itask_worker_next = itask; 4658 } else { 4659 w->worker_wait_head = itask; 4660 } 4661 w->worker_wait_tail = itask; 4662 if (wait_timer == 0) { 4663 wait_timer = ddi_get_lbolt() + wait_ticks; 4664 } 4665 } else if ((--(itask->itask_ncmds)) != 0) { 4666 itask->itask_worker_next = NULL; 4667 if (w->worker_task_tail) { 4668 w->worker_task_tail->itask_worker_next = itask; 4669 } else { 4670 w->worker_task_head = itask; 4671 } 4672 w->worker_task_tail = itask; 4673 } else { 4674 atomic_and_32(&itask->itask_flags, 4675 ~ITASK_IN_WORKER_QUEUE); 4676 /* 4677 * This is where the queue depth should go down by 4678 * one but we delay that on purpose to account for 4679 * the call into the provider. The actual decrement 4680 * happens after the worker has done its job. 4681 */ 4682 dec_qdepth = 1; 4683 } 4684 4685 /* We made it here means we are going to call LU */ 4686 if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0) 4687 lu = task->task_lu; 4688 else 4689 lu = dlun0; 4690 dbuf = itask->itask_dbufs[ITASK_CMD_BUF_NDX(curcmd)]; 4691 mutex_exit(&w->worker_lock); 4692 curcmd &= ITASK_CMD_MASK; 4693 switch (curcmd) { 4694 case ITASK_CMD_NEW_TASK: 4695 iss = (stmf_i_scsi_session_t *) 4696 task->task_session->ss_stmf_private; 4697 stmf_update_kstat_lu_q(task, kstat_waitq_to_runq); 4698 stmf_update_kstat_lport_q(task, kstat_waitq_to_runq); 4699 if (iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) { 4700 if (stmf_handle_cmd_during_ic(itask)) 4701 break; 4702 } 4703 #ifdef DEBUG 4704 if (stmf_drop_task_counter > 0) { 4705 if (atomic_add_32_nv( 4706 (uint32_t *)&stmf_drop_task_counter, 4707 -1) == 1) { 4708 break; 4709 } 4710 } 4711 #endif 4712 DTRACE_PROBE1(scsi__task__start, scsi_task_t *, task); 4713 lu->lu_new_task(task, dbuf); 4714 break; 4715 case ITASK_CMD_DATA_XFER_DONE: 4716 lu->lu_dbuf_xfer_done(task, dbuf); 4717 break; 4718 case ITASK_CMD_STATUS_DONE: 4719 lu->lu_send_status_done(task); 4720 break; 4721 case ITASK_CMD_ABORT: 4722 if (abort_free) { 4723 stmf_task_free(task); 4724 } else { 4725 stmf_do_task_abort(task); 4726 } 4727 break; 4728 case ITASK_CMD_POLL_LU: 4729 if (!wait_queue) { 4730 lu->lu_task_poll(task); 4731 } 4732 break; 4733 case ITASK_CMD_POLL_LPORT: 4734 if (!wait_queue) 4735 task->task_lport->lport_task_poll(task); 4736 break; 4737 case ITASK_CMD_SEND_STATUS: 4738 /* case ITASK_CMD_XFER_DATA: */ 4739 break; 4740 } 4741 mutex_enter(&w->worker_lock); 4742 if (dec_qdepth) { 4743 w->worker_queue_depth--; 4744 } 4745 } 4746 if ((w->worker_flags & STMF_WORKER_TERMINATE) && (wait_timer == 0)) { 4747 if (w->worker_ref_count == 0) 4748 goto stmf_worker_loop; 4749 else 4750 wait_timer = ddi_get_lbolt() + 1; 4751 } 4752 w->worker_flags &= ~STMF_WORKER_ACTIVE; 4753 if (wait_timer) { 4754 (void) cv_timedwait(&w->worker_cv, &w->worker_lock, wait_timer); 4755 } else { 4756 cv_wait(&w->worker_cv, &w->worker_lock); 4757 } 4758 w->worker_flags |= STMF_WORKER_ACTIVE; 4759 goto stmf_worker_loop; 4760 } 4761 4762 void 4763 stmf_worker_mgmt() 4764 { 4765 int i; 4766 int workers_needed; 4767 uint32_t qd; 4768 clock_t tps, d = 0; 4769 uint32_t cur_max_ntasks = 0; 4770 stmf_worker_t *w; 4771 4772 /* Check if we are trying to increase the # of threads */ 4773 for (i = stmf_nworkers_cur; i < stmf_nworkers_needed; i++) { 4774 if (stmf_workers[i].worker_flags & STMF_WORKER_STARTED) { 4775 stmf_nworkers_cur++; 4776 stmf_nworkers_accepting_cmds++; 4777 } else { 4778 /* Wait for transition to complete */ 4779 return; 4780 } 4781 } 4782 /* Check if we are trying to decrease the # of workers */ 4783 for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) { 4784 if ((stmf_workers[i].worker_flags & STMF_WORKER_STARTED) == 0) { 4785 stmf_nworkers_cur--; 4786 /* 4787 * stmf_nworkers_accepting_cmds has already been 4788 * updated by the request to reduce the # of workers. 4789 */ 4790 } else { 4791 /* Wait for transition to complete */ 4792 return; 4793 } 4794 } 4795 /* Check if we are being asked to quit */ 4796 if (stmf_workers_state != STMF_WORKERS_ENABLED) { 4797 if (stmf_nworkers_cur) { 4798 workers_needed = 0; 4799 goto worker_mgmt_trigger_change; 4800 } 4801 return; 4802 } 4803 /* Check if we are starting */ 4804 if (stmf_nworkers_cur < stmf_i_min_nworkers) { 4805 workers_needed = stmf_i_min_nworkers; 4806 goto worker_mgmt_trigger_change; 4807 } 4808 4809 tps = drv_usectohz(1 * 1000 * 1000); 4810 if ((stmf_wm_last != 0) && 4811 ((d = ddi_get_lbolt() - stmf_wm_last) > tps)) { 4812 qd = 0; 4813 for (i = 0; i < stmf_nworkers_accepting_cmds; i++) { 4814 qd += stmf_workers[i].worker_max_qdepth_pu; 4815 stmf_workers[i].worker_max_qdepth_pu = 0; 4816 if (stmf_workers[i].worker_max_sys_qdepth_pu > 4817 cur_max_ntasks) { 4818 cur_max_ntasks = 4819 stmf_workers[i].worker_max_sys_qdepth_pu; 4820 } 4821 stmf_workers[i].worker_max_sys_qdepth_pu = 0; 4822 } 4823 } 4824 stmf_wm_last = ddi_get_lbolt(); 4825 if (d <= tps) { 4826 /* still ramping up */ 4827 return; 4828 } 4829 /* max qdepth cannot be more than max tasks */ 4830 if (qd > cur_max_ntasks) 4831 qd = cur_max_ntasks; 4832 4833 /* See if we have more workers */ 4834 if (qd < stmf_nworkers_accepting_cmds) { 4835 /* 4836 * Since we dont reduce the worker count right away, monitor 4837 * the highest load during the scale_down_delay. 4838 */ 4839 if (qd > stmf_worker_scale_down_qd) 4840 stmf_worker_scale_down_qd = qd; 4841 if (stmf_worker_scale_down_timer == 0) { 4842 stmf_worker_scale_down_timer = ddi_get_lbolt() + 4843 drv_usectohz(stmf_worker_scale_down_delay * 4844 1000 * 1000); 4845 return; 4846 } 4847 if (ddi_get_lbolt() < stmf_worker_scale_down_timer) { 4848 return; 4849 } 4850 /* Its time to reduce the workers */ 4851 if (stmf_worker_scale_down_qd < stmf_i_min_nworkers) 4852 stmf_worker_scale_down_qd = stmf_i_min_nworkers; 4853 if (stmf_worker_scale_down_qd > stmf_i_max_nworkers) 4854 stmf_worker_scale_down_qd = stmf_i_max_nworkers; 4855 if (stmf_worker_scale_down_qd == stmf_nworkers_cur) 4856 return; 4857 workers_needed = stmf_worker_scale_down_qd; 4858 stmf_worker_scale_down_qd = 0; 4859 goto worker_mgmt_trigger_change; 4860 } 4861 stmf_worker_scale_down_qd = 0; 4862 stmf_worker_scale_down_timer = 0; 4863 if (qd > stmf_i_max_nworkers) 4864 qd = stmf_i_max_nworkers; 4865 if (qd < stmf_i_min_nworkers) 4866 qd = stmf_i_min_nworkers; 4867 if (qd == stmf_nworkers_cur) 4868 return; 4869 workers_needed = qd; 4870 goto worker_mgmt_trigger_change; 4871 4872 /* NOTREACHED */ 4873 return; 4874 4875 worker_mgmt_trigger_change: 4876 ASSERT(workers_needed != stmf_nworkers_cur); 4877 if (workers_needed > stmf_nworkers_cur) { 4878 stmf_nworkers_needed = workers_needed; 4879 for (i = stmf_nworkers_cur; i < workers_needed; i++) { 4880 w = &stmf_workers[i]; 4881 w->worker_tid = thread_create(NULL, 0, stmf_worker_task, 4882 (void *)&stmf_workers[i], 0, &p0, TS_RUN, 4883 minclsyspri); 4884 } 4885 return; 4886 } 4887 /* At this point we know that we are decreasing the # of workers */ 4888 stmf_nworkers_accepting_cmds = workers_needed; 4889 stmf_nworkers_needed = workers_needed; 4890 /* Signal the workers that its time to quit */ 4891 for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) { 4892 w = &stmf_workers[i]; 4893 ASSERT(w && (w->worker_flags & STMF_WORKER_STARTED)); 4894 mutex_enter(&w->worker_lock); 4895 w->worker_flags |= STMF_WORKER_TERMINATE; 4896 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) 4897 cv_signal(&w->worker_cv); 4898 mutex_exit(&w->worker_lock); 4899 } 4900 } 4901 4902 /* 4903 * Fills out a dbuf from stmf_xfer_data_t (contained in the db_lu_private). 4904 * If all the data has been filled out, frees the xd and makes 4905 * db_lu_private NULL. 4906 */ 4907 void 4908 stmf_xd_to_dbuf(stmf_data_buf_t *dbuf) 4909 { 4910 stmf_xfer_data_t *xd; 4911 uint8_t *p; 4912 int i; 4913 uint32_t s; 4914 4915 xd = (stmf_xfer_data_t *)dbuf->db_lu_private; 4916 dbuf->db_data_size = 0; 4917 dbuf->db_relative_offset = xd->size_done; 4918 for (i = 0; i < dbuf->db_sglist_length; i++) { 4919 s = min(xd->size_left, dbuf->db_sglist[i].seg_length); 4920 p = &xd->buf[xd->size_done]; 4921 bcopy(p, dbuf->db_sglist[i].seg_addr, s); 4922 xd->size_left -= s; 4923 xd->size_done += s; 4924 dbuf->db_data_size += s; 4925 if (xd->size_left == 0) { 4926 kmem_free(xd, xd->alloc_size); 4927 dbuf->db_lu_private = NULL; 4928 return; 4929 } 4930 } 4931 } 4932 4933 /* ARGSUSED */ 4934 stmf_status_t 4935 stmf_dlun0_task_alloc(scsi_task_t *task) 4936 { 4937 return (STMF_SUCCESS); 4938 } 4939 4940 void 4941 stmf_dlun0_new_task(scsi_task_t *task, stmf_data_buf_t *dbuf) 4942 { 4943 uint8_t *cdbp = (uint8_t *)&task->task_cdb[0]; 4944 stmf_i_scsi_session_t *iss; 4945 uint32_t sz, minsz; 4946 uint8_t *p; 4947 stmf_xfer_data_t *xd; 4948 uint8_t inq_page_length = 31; 4949 4950 if (task->task_mgmt_function) { 4951 stmf_scsilib_handle_task_mgmt(task); 4952 return; 4953 } 4954 4955 switch (cdbp[0]) { 4956 case SCMD_INQUIRY: 4957 /* 4958 * Basic protocol checks. In addition, only reply to 4959 * standard inquiry. Otherwise, the LU provider needs 4960 * to respond. 4961 */ 4962 4963 if (cdbp[2] || (cdbp[1] & 1) || cdbp[5]) { 4964 stmf_scsilib_send_status(task, STATUS_CHECK, 4965 STMF_SAA_INVALID_FIELD_IN_CDB); 4966 return; 4967 } 4968 4969 task->task_cmd_xfer_length = 4970 (((uint32_t)cdbp[3]) << 8) | cdbp[4]; 4971 4972 if (task->task_additional_flags & 4973 TASK_AF_NO_EXPECTED_XFER_LENGTH) { 4974 task->task_expected_xfer_length = 4975 task->task_cmd_xfer_length; 4976 } 4977 4978 sz = min(task->task_expected_xfer_length, 4979 min(36, task->task_cmd_xfer_length)); 4980 minsz = 36; 4981 4982 if (sz == 0) { 4983 stmf_scsilib_send_status(task, STATUS_GOOD, 0); 4984 return; 4985 } 4986 4987 if (dbuf && (dbuf->db_sglist[0].seg_length < 36)) { 4988 /* 4989 * Ignore any preallocated dbuf if the size is less 4990 * than 36. It will be freed during the task_free. 4991 */ 4992 dbuf = NULL; 4993 } 4994 if (dbuf == NULL) 4995 dbuf = stmf_alloc_dbuf(task, minsz, &minsz, 0); 4996 if ((dbuf == NULL) || (dbuf->db_sglist[0].seg_length < sz)) { 4997 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 4998 STMF_ALLOC_FAILURE, NULL); 4999 return; 5000 } 5001 dbuf->db_lu_private = NULL; 5002 5003 p = dbuf->db_sglist[0].seg_addr; 5004 5005 /* 5006 * Standard inquiry handling only. 5007 */ 5008 5009 bzero(p, inq_page_length + 5); 5010 5011 p[0] = DPQ_SUPPORTED | DTYPE_UNKNOWN; 5012 p[2] = 5; 5013 p[3] = 0x12; 5014 p[4] = inq_page_length; 5015 p[6] = 0x80; 5016 5017 (void) strncpy((char *)p+8, "SUN ", 8); 5018 (void) strncpy((char *)p+16, "COMSTAR ", 16); 5019 (void) strncpy((char *)p+32, "1.0 ", 4); 5020 5021 dbuf->db_data_size = sz; 5022 dbuf->db_relative_offset = 0; 5023 dbuf->db_flags = DB_DIRECTION_TO_RPORT; 5024 (void) stmf_xfer_data(task, dbuf, 0); 5025 5026 return; 5027 5028 case SCMD_REPORT_LUNS: 5029 task->task_cmd_xfer_length = 5030 ((((uint32_t)task->task_cdb[6]) << 24) | 5031 (((uint32_t)task->task_cdb[7]) << 16) | 5032 (((uint32_t)task->task_cdb[8]) << 8) | 5033 ((uint32_t)task->task_cdb[9])); 5034 5035 if (task->task_additional_flags & 5036 TASK_AF_NO_EXPECTED_XFER_LENGTH) { 5037 task->task_expected_xfer_length = 5038 task->task_cmd_xfer_length; 5039 } 5040 5041 sz = min(task->task_expected_xfer_length, 5042 task->task_cmd_xfer_length); 5043 5044 if (sz < 16) { 5045 stmf_scsilib_send_status(task, STATUS_CHECK, 5046 STMF_SAA_INVALID_FIELD_IN_CDB); 5047 return; 5048 } 5049 5050 iss = (stmf_i_scsi_session_t *) 5051 task->task_session->ss_stmf_private; 5052 rw_enter(iss->iss_lockp, RW_WRITER); 5053 xd = stmf_session_prepare_report_lun_data(iss->iss_sm); 5054 rw_exit(iss->iss_lockp); 5055 5056 if (xd == NULL) { 5057 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 5058 STMF_ALLOC_FAILURE, NULL); 5059 return; 5060 } 5061 5062 sz = min(sz, xd->size_left); 5063 xd->size_left = sz; 5064 minsz = min(512, sz); 5065 5066 if (dbuf == NULL) 5067 dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0); 5068 if (dbuf == NULL) { 5069 kmem_free(xd, xd->alloc_size); 5070 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 5071 STMF_ALLOC_FAILURE, NULL); 5072 return; 5073 } 5074 dbuf->db_lu_private = xd; 5075 stmf_xd_to_dbuf(dbuf); 5076 5077 atomic_and_32(&iss->iss_flags, 5078 ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS)); 5079 dbuf->db_flags = DB_DIRECTION_TO_RPORT; 5080 (void) stmf_xfer_data(task, dbuf, 0); 5081 return; 5082 } 5083 5084 stmf_scsilib_send_status(task, STATUS_CHECK, STMF_SAA_INVALID_OPCODE); 5085 } 5086 5087 void 5088 stmf_dlun0_dbuf_done(scsi_task_t *task, stmf_data_buf_t *dbuf) 5089 { 5090 if (dbuf->db_xfer_status != STMF_SUCCESS) { 5091 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 5092 dbuf->db_xfer_status, NULL); 5093 return; 5094 } 5095 task->task_nbytes_transferred = dbuf->db_data_size; 5096 if (dbuf->db_lu_private) { 5097 /* There is more */ 5098 stmf_xd_to_dbuf(dbuf); 5099 (void) stmf_xfer_data(task, dbuf, 0); 5100 return; 5101 } 5102 stmf_scsilib_send_status(task, STATUS_GOOD, 0); 5103 } 5104 5105 /* ARGSUSED */ 5106 void 5107 stmf_dlun0_status_done(scsi_task_t *task) 5108 { 5109 } 5110 5111 /* ARGSUSED */ 5112 void 5113 stmf_dlun0_task_free(scsi_task_t *task) 5114 { 5115 } 5116 5117 /* ARGSUSED */ 5118 stmf_status_t 5119 stmf_dlun0_abort(struct stmf_lu *lu, int abort_cmd, void *arg, uint32_t flags) 5120 { 5121 scsi_task_t *task = (scsi_task_t *)arg; 5122 stmf_i_scsi_task_t *itask = 5123 (stmf_i_scsi_task_t *)task->task_stmf_private; 5124 stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private; 5125 int i; 5126 uint8_t map; 5127 5128 ASSERT(abort_cmd == STMF_LU_ABORT_TASK); 5129 if ((task->task_mgmt_function) && (itask->itask_flags & 5130 (ITASK_CAUSING_LU_RESET | ITASK_CAUSING_TARGET_RESET))) { 5131 switch (task->task_mgmt_function) { 5132 case TM_ABORT_TASK: 5133 case TM_ABORT_TASK_SET: 5134 case TM_CLEAR_TASK_SET: 5135 case TM_LUN_RESET: 5136 atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE); 5137 break; 5138 case TM_TARGET_RESET: 5139 case TM_TARGET_COLD_RESET: 5140 case TM_TARGET_WARM_RESET: 5141 stmf_abort_target_reset(task); 5142 break; 5143 } 5144 return (STMF_ABORT_SUCCESS); 5145 } 5146 5147 /* 5148 * OK so its not a task mgmt. Make sure we free any xd sitting 5149 * inside any dbuf. 5150 */ 5151 if ((map = itask->itask_allocated_buf_map) != 0) { 5152 for (i = 0; i < 4; i++) { 5153 if ((map & 1) && 5154 ((itask->itask_dbufs[i])->db_lu_private)) { 5155 stmf_xfer_data_t *xd; 5156 stmf_data_buf_t *dbuf; 5157 5158 dbuf = itask->itask_dbufs[i]; 5159 xd = (stmf_xfer_data_t *)dbuf->db_lu_private; 5160 dbuf->db_lu_private = NULL; 5161 kmem_free(xd, xd->alloc_size); 5162 } 5163 map >>= 1; 5164 } 5165 } 5166 return (STMF_ABORT_SUCCESS); 5167 } 5168 5169 void 5170 stmf_dlun0_task_poll(struct scsi_task *task) 5171 { 5172 /* Right now we only do this for handling task management functions */ 5173 ASSERT(task->task_mgmt_function); 5174 5175 switch (task->task_mgmt_function) { 5176 case TM_ABORT_TASK: 5177 case TM_ABORT_TASK_SET: 5178 case TM_CLEAR_TASK_SET: 5179 case TM_LUN_RESET: 5180 (void) stmf_lun_reset_poll(task->task_lu, task, 0); 5181 return; 5182 case TM_TARGET_RESET: 5183 case TM_TARGET_COLD_RESET: 5184 case TM_TARGET_WARM_RESET: 5185 stmf_target_reset_poll(task); 5186 return; 5187 } 5188 } 5189 5190 /* ARGSUSED */ 5191 void 5192 stmf_dlun0_ctl(struct stmf_lu *lu, int cmd, void *arg) 5193 { 5194 /* This function will never be called */ 5195 cmn_err(CE_WARN, "stmf_dlun0_ctl called with cmd %x", cmd); 5196 } 5197 5198 void 5199 stmf_dlun_init() 5200 { 5201 stmf_i_lu_t *ilu; 5202 5203 dlun0 = stmf_alloc(STMF_STRUCT_STMF_LU, 0, 0); 5204 dlun0->lu_task_alloc = stmf_dlun0_task_alloc; 5205 dlun0->lu_new_task = stmf_dlun0_new_task; 5206 dlun0->lu_dbuf_xfer_done = stmf_dlun0_dbuf_done; 5207 dlun0->lu_send_status_done = stmf_dlun0_status_done; 5208 dlun0->lu_task_free = stmf_dlun0_task_free; 5209 dlun0->lu_abort = stmf_dlun0_abort; 5210 dlun0->lu_task_poll = stmf_dlun0_task_poll; 5211 dlun0->lu_ctl = stmf_dlun0_ctl; 5212 5213 ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private; 5214 ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1; 5215 } 5216 5217 stmf_status_t 5218 stmf_dlun_fini() 5219 { 5220 stmf_i_lu_t *ilu; 5221 5222 ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private; 5223 5224 ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free); 5225 if (ilu->ilu_ntasks) { 5226 stmf_i_scsi_task_t *itask, *nitask; 5227 5228 nitask = ilu->ilu_tasks; 5229 do { 5230 itask = nitask; 5231 nitask = itask->itask_lu_next; 5232 dlun0->lu_task_free(itask->itask_task); 5233 stmf_free(itask->itask_task); 5234 } while (nitask != NULL); 5235 5236 } 5237 stmf_free(dlun0); 5238 return (STMF_SUCCESS); 5239 } 5240 5241 void 5242 stmf_abort_target_reset(scsi_task_t *task) 5243 { 5244 stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *) 5245 task->task_session->ss_stmf_private; 5246 stmf_lun_map_t *lm; 5247 stmf_lun_map_ent_t *lm_ent; 5248 stmf_i_lu_t *ilu; 5249 int i; 5250 5251 ASSERT(iss->iss_flags & ISS_RESET_ACTIVE); 5252 5253 rw_enter(iss->iss_lockp, RW_READER); 5254 lm = iss->iss_sm; 5255 for (i = 0; i < lm->lm_nentries; i++) { 5256 if (lm->lm_plus[i] == NULL) 5257 continue; 5258 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 5259 ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private; 5260 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 5261 atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE); 5262 } 5263 } 5264 atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE); 5265 rw_exit(iss->iss_lockp); 5266 } 5267 5268 /* 5269 * The return value is only used by function managing target reset. 5270 */ 5271 stmf_status_t 5272 stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task, int target_reset) 5273 { 5274 stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 5275 int ntasks_pending; 5276 5277 ntasks_pending = ilu->ilu_ntasks - ilu->ilu_ntasks_free; 5278 /* 5279 * This function is also used during Target reset. The idea is that 5280 * once all the commands are aborted, call the LU's reset entry 5281 * point (abort entry point with a reset flag). But if this Task 5282 * mgmt is running on this LU then all the tasks cannot be aborted. 5283 * one task (this task) will still be running which is OK. 5284 */ 5285 if ((ntasks_pending == 0) || ((task->task_lu == lu) && 5286 (ntasks_pending == 1))) { 5287 stmf_status_t ret; 5288 5289 if ((task->task_mgmt_function == TM_LUN_RESET) || 5290 (task->task_mgmt_function == TM_TARGET_RESET) || 5291 (task->task_mgmt_function == TM_TARGET_WARM_RESET) || 5292 (task->task_mgmt_function == TM_TARGET_COLD_RESET)) { 5293 ret = lu->lu_abort(lu, STMF_LU_RESET_STATE, task, 0); 5294 } else { 5295 ret = STMF_SUCCESS; 5296 } 5297 if (ret == STMF_SUCCESS) { 5298 atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE); 5299 } 5300 if (target_reset) { 5301 return (ret); 5302 } 5303 if (ret == STMF_SUCCESS) { 5304 stmf_scsilib_send_status(task, STATUS_GOOD, 0); 5305 return (ret); 5306 } 5307 if (ret != STMF_BUSY) { 5308 stmf_abort(STMF_QUEUE_TASK_ABORT, task, ret, NULL); 5309 return (ret); 5310 } 5311 } 5312 5313 if (target_reset) { 5314 /* Tell target reset polling code that we are not done */ 5315 return (STMF_BUSY); 5316 } 5317 5318 if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT) 5319 != STMF_SUCCESS) { 5320 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 5321 STMF_ALLOC_FAILURE, NULL); 5322 return (STMF_SUCCESS); 5323 } 5324 5325 return (STMF_SUCCESS); 5326 } 5327 5328 void 5329 stmf_target_reset_poll(struct scsi_task *task) 5330 { 5331 stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *) 5332 task->task_session->ss_stmf_private; 5333 stmf_lun_map_t *lm; 5334 stmf_lun_map_ent_t *lm_ent; 5335 stmf_i_lu_t *ilu; 5336 stmf_status_t ret; 5337 int i; 5338 int not_done = 0; 5339 5340 ASSERT(iss->iss_flags & ISS_RESET_ACTIVE); 5341 5342 rw_enter(iss->iss_lockp, RW_READER); 5343 lm = iss->iss_sm; 5344 for (i = 0; i < lm->lm_nentries; i++) { 5345 if (lm->lm_plus[i] == NULL) 5346 continue; 5347 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i]; 5348 ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private; 5349 if (ilu->ilu_flags & ILU_RESET_ACTIVE) { 5350 rw_exit(iss->iss_lockp); 5351 ret = stmf_lun_reset_poll(lm_ent->ent_lu, task, 1); 5352 rw_enter(iss->iss_lockp, RW_READER); 5353 if (ret == STMF_SUCCESS) 5354 continue; 5355 not_done = 1; 5356 if (ret != STMF_BUSY) { 5357 rw_exit(iss->iss_lockp); 5358 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 5359 STMF_ABORTED, NULL); 5360 return; 5361 } 5362 } 5363 } 5364 rw_exit(iss->iss_lockp); 5365 5366 if (not_done) { 5367 if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT) 5368 != STMF_SUCCESS) { 5369 stmf_abort(STMF_QUEUE_TASK_ABORT, task, 5370 STMF_ALLOC_FAILURE, NULL); 5371 return; 5372 } 5373 return; 5374 } 5375 5376 atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE); 5377 5378 stmf_scsilib_send_status(task, STATUS_GOOD, 0); 5379 } 5380 5381 stmf_status_t 5382 stmf_lu_add_event(stmf_lu_t *lu, int eventid) 5383 { 5384 stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 5385 5386 if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) { 5387 return (STMF_INVALID_ARG); 5388 } 5389 5390 STMF_EVENT_ADD(ilu->ilu_event_hdl, eventid); 5391 return (STMF_SUCCESS); 5392 } 5393 5394 stmf_status_t 5395 stmf_lu_remove_event(stmf_lu_t *lu, int eventid) 5396 { 5397 stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 5398 5399 if (eventid == STMF_EVENT_ALL) { 5400 STMF_EVENT_CLEAR_ALL(ilu->ilu_event_hdl); 5401 return (STMF_SUCCESS); 5402 } 5403 5404 if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) { 5405 return (STMF_INVALID_ARG); 5406 } 5407 5408 STMF_EVENT_REMOVE(ilu->ilu_event_hdl, eventid); 5409 return (STMF_SUCCESS); 5410 } 5411 5412 stmf_status_t 5413 stmf_lport_add_event(stmf_local_port_t *lport, int eventid) 5414 { 5415 stmf_i_local_port_t *ilport = 5416 (stmf_i_local_port_t *)lport->lport_stmf_private; 5417 5418 if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) { 5419 return (STMF_INVALID_ARG); 5420 } 5421 5422 STMF_EVENT_ADD(ilport->ilport_event_hdl, eventid); 5423 return (STMF_SUCCESS); 5424 } 5425 5426 stmf_status_t 5427 stmf_lport_remove_event(stmf_local_port_t *lport, int eventid) 5428 { 5429 stmf_i_local_port_t *ilport = 5430 (stmf_i_local_port_t *)lport->lport_stmf_private; 5431 5432 if (eventid == STMF_EVENT_ALL) { 5433 STMF_EVENT_CLEAR_ALL(ilport->ilport_event_hdl); 5434 return (STMF_SUCCESS); 5435 } 5436 5437 if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) { 5438 return (STMF_INVALID_ARG); 5439 } 5440 5441 STMF_EVENT_REMOVE(ilport->ilport_event_hdl, eventid); 5442 return (STMF_SUCCESS); 5443 } 5444 5445 void 5446 stmf_generate_lu_event(stmf_i_lu_t *ilu, int eventid, void *arg, uint32_t flags) 5447 { 5448 if (STMF_EVENT_ENABLED(ilu->ilu_event_hdl, eventid) && 5449 (ilu->ilu_lu->lu_event_handler != NULL)) { 5450 ilu->ilu_lu->lu_event_handler(ilu->ilu_lu, eventid, arg, flags); 5451 } 5452 } 5453 5454 void 5455 stmf_generate_lport_event(stmf_i_local_port_t *ilport, int eventid, void *arg, 5456 uint32_t flags) 5457 { 5458 if (STMF_EVENT_ENABLED(ilport->ilport_event_hdl, eventid) && 5459 (ilport->ilport_lport->lport_event_handler != NULL)) { 5460 ilport->ilport_lport->lport_event_handler( 5461 ilport->ilport_lport, eventid, arg, flags); 5462 } 5463 } 5464 5465 void 5466 stmf_svc_init() 5467 { 5468 if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) 5469 return; 5470 stmf_state.stmf_svc_taskq = ddi_taskq_create(0, "STMF_SVC_TASKQ", 1, 5471 TASKQ_DEFAULTPRI, 0); 5472 (void) ddi_taskq_dispatch(stmf_state.stmf_svc_taskq, 5473 stmf_svc, 0, DDI_SLEEP); 5474 } 5475 5476 stmf_status_t 5477 stmf_svc_fini() 5478 { 5479 uint32_t i; 5480 5481 mutex_enter(&stmf_state.stmf_lock); 5482 if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) { 5483 stmf_state.stmf_svc_flags |= STMF_SVC_TERMINATE; 5484 cv_signal(&stmf_state.stmf_cv); 5485 } 5486 mutex_exit(&stmf_state.stmf_lock); 5487 5488 /* Wait for 5 seconds */ 5489 for (i = 0; i < 500; i++) { 5490 if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) 5491 delay(drv_usectohz(10000)); 5492 else 5493 break; 5494 } 5495 if (i == 500) 5496 return (STMF_BUSY); 5497 5498 ddi_taskq_destroy(stmf_state.stmf_svc_taskq); 5499 5500 return (STMF_SUCCESS); 5501 } 5502 5503 /* ARGSUSED */ 5504 void 5505 stmf_svc(void *arg) 5506 { 5507 stmf_svc_req_t *req, **preq; 5508 clock_t td; 5509 clock_t drain_start, drain_next = 0; 5510 clock_t timing_start, timing_next = 0; 5511 clock_t worker_delay = 0; 5512 int deq; 5513 stmf_lu_t *lu; 5514 stmf_i_lu_t *ilu; 5515 stmf_local_port_t *lport; 5516 stmf_i_local_port_t *ilport, *next_ilport; 5517 stmf_i_scsi_session_t *iss; 5518 5519 td = drv_usectohz(20000); 5520 5521 mutex_enter(&stmf_state.stmf_lock); 5522 stmf_state.stmf_svc_flags |= STMF_SVC_STARTED | STMF_SVC_ACTIVE; 5523 5524 stmf_svc_loop: 5525 if (stmf_state.stmf_svc_flags & STMF_SVC_TERMINATE) { 5526 stmf_state.stmf_svc_flags &= 5527 ~(STMF_SVC_STARTED | STMF_SVC_ACTIVE); 5528 mutex_exit(&stmf_state.stmf_lock); 5529 return; 5530 } 5531 5532 if (stmf_state.stmf_svc_active) { 5533 int waitq_add = 0; 5534 req = stmf_state.stmf_svc_active; 5535 stmf_state.stmf_svc_active = req->svc_next; 5536 5537 switch (req->svc_cmd) { 5538 case STMF_CMD_LPORT_ONLINE: 5539 /* Fallthrough */ 5540 case STMF_CMD_LPORT_OFFLINE: 5541 /* Fallthrough */ 5542 case STMF_CMD_LU_ONLINE: 5543 /* Nothing to do */ 5544 waitq_add = 1; 5545 break; 5546 5547 case STMF_CMD_LU_OFFLINE: 5548 /* Remove all mappings of this LU */ 5549 stmf_session_lu_unmapall((stmf_lu_t *)req->svc_obj); 5550 /* Kill all the pending I/Os for this LU */ 5551 mutex_exit(&stmf_state.stmf_lock); 5552 stmf_task_lu_killall((stmf_lu_t *)req->svc_obj, NULL, 5553 STMF_ABORTED); 5554 mutex_enter(&stmf_state.stmf_lock); 5555 waitq_add = 1; 5556 break; 5557 default: 5558 cmn_err(CE_PANIC, "stmf_svc: unknown cmd %d", 5559 req->svc_cmd); 5560 } 5561 5562 if (waitq_add) { 5563 /* Put it in the wait queue */ 5564 req->svc_next = stmf_state.stmf_svc_waiting; 5565 stmf_state.stmf_svc_waiting = req; 5566 } 5567 } 5568 5569 /* The waiting list is not going to be modified by anybody else */ 5570 mutex_exit(&stmf_state.stmf_lock); 5571 5572 for (preq = &stmf_state.stmf_svc_waiting; (*preq) != NULL; ) { 5573 req = *preq; 5574 deq = 0; 5575 switch (req->svc_cmd) { 5576 case STMF_CMD_LU_ONLINE: 5577 lu = (stmf_lu_t *)req->svc_obj; 5578 deq = 1; 5579 lu->lu_ctl(lu, req->svc_cmd, &req->svc_info); 5580 break; 5581 5582 case STMF_CMD_LU_OFFLINE: 5583 lu = (stmf_lu_t *)req->svc_obj; 5584 ilu = (stmf_i_lu_t *)lu->lu_stmf_private; 5585 if (ilu->ilu_ntasks != ilu->ilu_ntasks_free) 5586 break; 5587 deq = 1; 5588 lu->lu_ctl(lu, req->svc_cmd, &req->svc_info); 5589 break; 5590 5591 case STMF_CMD_LPORT_OFFLINE: 5592 /* Fallthrough */ 5593 case STMF_CMD_LPORT_ONLINE: 5594 lport = (stmf_local_port_t *)req->svc_obj; 5595 deq = 1; 5596 lport->lport_ctl(lport, req->svc_cmd, &req->svc_info); 5597 break; 5598 } 5599 if (deq) { 5600 *preq = req->svc_next; 5601 kmem_free(req, req->svc_req_alloc_size); 5602 } else { 5603 preq = &req->svc_next; 5604 } 5605 } 5606 5607 mutex_enter(&stmf_state.stmf_lock); 5608 if (stmf_state.stmf_svc_active == NULL) { 5609 /* Do timeouts */ 5610 if (stmf_state.stmf_nlus && 5611 ((!timing_next) || (ddi_get_lbolt() >= timing_next))) { 5612 if (!stmf_state.stmf_svc_ilu_timing) { 5613 /* we are starting a new round */ 5614 stmf_state.stmf_svc_ilu_timing = 5615 stmf_state.stmf_ilulist; 5616 timing_start = ddi_get_lbolt(); 5617 } 5618 stmf_check_ilu_timing(); 5619 if (!stmf_state.stmf_svc_ilu_timing) { 5620 /* we finished a complete round */ 5621 timing_next = 5622 timing_start + drv_usectohz(5*1000*1000); 5623 } else { 5624 /* we still have some ilu items to check */ 5625 timing_next = 5626 ddi_get_lbolt() + drv_usectohz(1*1000*1000); 5627 } 5628 if (stmf_state.stmf_svc_active) 5629 goto stmf_svc_loop; 5630 } 5631 /* Check if there are free tasks to clear */ 5632 if (stmf_state.stmf_nlus && 5633 ((!drain_next) || (ddi_get_lbolt() >= drain_next))) { 5634 if (!stmf_state.stmf_svc_ilu_draining) { 5635 /* we are starting a new round */ 5636 stmf_state.stmf_svc_ilu_draining = 5637 stmf_state.stmf_ilulist; 5638 drain_start = ddi_get_lbolt(); 5639 } 5640 stmf_check_freetask(); 5641 if (!stmf_state.stmf_svc_ilu_draining) { 5642 /* we finished a complete round */ 5643 drain_next = 5644 drain_start + drv_usectohz(10*1000*1000); 5645 } else { 5646 /* we still have some ilu items to check */ 5647 drain_next = 5648 ddi_get_lbolt() + drv_usectohz(1*1000*1000); 5649 } 5650 if (stmf_state.stmf_svc_active) 5651 goto stmf_svc_loop; 5652 } 5653 5654 /* Check if we need to run worker_mgmt */ 5655 if (ddi_get_lbolt() > worker_delay) { 5656 stmf_worker_mgmt(); 5657 worker_delay = ddi_get_lbolt() + 5658 stmf_worker_mgmt_delay; 5659 } 5660 5661 /* Check if any active session got its 1st LUN */ 5662 if (stmf_state.stmf_process_initial_luns) { 5663 int stmf_level = 0; 5664 int port_level; 5665 for (ilport = stmf_state.stmf_ilportlist; ilport; 5666 ilport = next_ilport) { 5667 next_ilport = ilport->ilport_next; 5668 if ((ilport->ilport_flags & 5669 ILPORT_SS_GOT_INITIAL_LUNS) == 0) { 5670 continue; 5671 } 5672 port_level = 0; 5673 rw_enter(&ilport->ilport_lock, RW_READER); 5674 for (iss = ilport->ilport_ss_list; iss; 5675 iss = iss->iss_next) { 5676 if ((iss->iss_flags & 5677 ISS_GOT_INITIAL_LUNS) == 0) { 5678 continue; 5679 } 5680 port_level++; 5681 stmf_level++; 5682 atomic_and_32(&iss->iss_flags, 5683 ~ISS_GOT_INITIAL_LUNS); 5684 atomic_or_32(&iss->iss_flags, 5685 ISS_EVENT_ACTIVE); 5686 rw_exit(&ilport->ilport_lock); 5687 mutex_exit(&stmf_state.stmf_lock); 5688 stmf_generate_lport_event(ilport, 5689 LPORT_EVENT_INITIAL_LUN_MAPPED, 5690 iss->iss_ss, 0); 5691 atomic_and_32(&iss->iss_flags, 5692 ~ISS_EVENT_ACTIVE); 5693 mutex_enter(&stmf_state.stmf_lock); 5694 /* 5695 * scan all the ilports again as the 5696 * ilport list might have changed. 5697 */ 5698 next_ilport = 5699 stmf_state.stmf_ilportlist; 5700 break; 5701 } 5702 if (port_level == 0) { 5703 atomic_and_32(&ilport->ilport_flags, 5704 ~ILPORT_SS_GOT_INITIAL_LUNS); 5705 } 5706 /* drop the lock if we are holding it. */ 5707 if (rw_lock_held(&ilport->ilport_lock)) 5708 rw_exit(&ilport->ilport_lock); 5709 5710 /* Max 4 session at a time */ 5711 if (stmf_level >= 4) { 5712 break; 5713 } 5714 } 5715 if (stmf_level == 0) { 5716 stmf_state.stmf_process_initial_luns = 0; 5717 } 5718 } 5719 5720 stmf_state.stmf_svc_flags &= ~STMF_SVC_ACTIVE; 5721 (void) cv_timedwait(&stmf_state.stmf_cv, &stmf_state.stmf_lock, 5722 ddi_get_lbolt() + td); 5723 stmf_state.stmf_svc_flags |= STMF_SVC_ACTIVE; 5724 } 5725 goto stmf_svc_loop; 5726 } 5727 5728 void 5729 stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info) 5730 { 5731 stmf_svc_req_t *req; 5732 int s; 5733 5734 ASSERT(!mutex_owned(&stmf_state.stmf_lock)); 5735 s = sizeof (stmf_svc_req_t); 5736 if (info->st_additional_info) { 5737 s += strlen(info->st_additional_info) + 1; 5738 } 5739 req = kmem_zalloc(s, KM_SLEEP); 5740 5741 req->svc_cmd = cmd; 5742 req->svc_obj = obj; 5743 req->svc_info.st_rflags = info->st_rflags; 5744 if (info->st_additional_info) { 5745 req->svc_info.st_additional_info = (char *)(GET_BYTE_OFFSET(req, 5746 sizeof (stmf_svc_req_t))); 5747 (void) strcpy(req->svc_info.st_additional_info, 5748 info->st_additional_info); 5749 } 5750 req->svc_req_alloc_size = s; 5751 5752 mutex_enter(&stmf_state.stmf_lock); 5753 req->svc_next = stmf_state.stmf_svc_active; 5754 stmf_state.stmf_svc_active = req; 5755 if ((stmf_state.stmf_svc_flags & STMF_SVC_ACTIVE) == 0) { 5756 cv_signal(&stmf_state.stmf_cv); 5757 } 5758 mutex_exit(&stmf_state.stmf_lock); 5759 } 5760 5761 void 5762 stmf_trace(caddr_t ident, const char *fmt, ...) 5763 { 5764 va_list args; 5765 char tbuf[160]; 5766 int len; 5767 5768 if (!stmf_trace_on) 5769 return; 5770 len = snprintf(tbuf, 158, "%s:%07lu: ", ident ? ident : "", 5771 ddi_get_lbolt()); 5772 va_start(args, fmt); 5773 len += vsnprintf(tbuf + len, 158 - len, fmt, args); 5774 va_end(args); 5775 5776 if (len > 158) { 5777 len = 158; 5778 } 5779 tbuf[len++] = '\n'; 5780 tbuf[len] = 0; 5781 5782 mutex_enter(&trace_buf_lock); 5783 bcopy(tbuf, &stmf_trace_buf[trace_buf_curndx], len+1); 5784 trace_buf_curndx += len; 5785 if (trace_buf_curndx > (trace_buf_size - 320)) 5786 trace_buf_curndx = 0; 5787 mutex_exit(&trace_buf_lock); 5788 } 5789 5790 void 5791 stmf_trace_clear() 5792 { 5793 if (!stmf_trace_on) 5794 return; 5795 mutex_enter(&trace_buf_lock); 5796 trace_buf_curndx = 0; 5797 if (trace_buf_size > 0) 5798 stmf_trace_buf[0] = 0; 5799 mutex_exit(&trace_buf_lock); 5800 } 5801 5802 static void 5803 stmf_abort_task_offline(scsi_task_t *task, int offline_lu, char *info) 5804 { 5805 stmf_state_change_info_t change_info; 5806 void *ctl_private; 5807 uint32_t ctl_cmd; 5808 int msg = 0; 5809 5810 stmf_trace("FROM STMF", "abort_task_offline called for %s: %s", 5811 offline_lu ? "LU" : "LPORT", info ? info : "no additional info"); 5812 change_info.st_additional_info = info; 5813 if (offline_lu) { 5814 change_info.st_rflags = STMF_RFLAG_RESET | 5815 STMF_RFLAG_LU_ABORT; 5816 ctl_private = task->task_lu; 5817 if (((stmf_i_lu_t *) 5818 task->task_lu->lu_stmf_private)->ilu_state == 5819 STMF_STATE_ONLINE) { 5820 msg = 1; 5821 } 5822 ctl_cmd = STMF_CMD_LU_OFFLINE; 5823 } else { 5824 change_info.st_rflags = STMF_RFLAG_RESET | 5825 STMF_RFLAG_LPORT_ABORT; 5826 ctl_private = task->task_lport; 5827 if (((stmf_i_local_port_t *) 5828 task->task_lport->lport_stmf_private)->ilport_state == 5829 STMF_STATE_ONLINE) { 5830 msg = 1; 5831 } 5832 ctl_cmd = STMF_CMD_LPORT_OFFLINE; 5833 } 5834 5835 if (msg) { 5836 stmf_trace(0, "Calling stmf_ctl to offline %s : %s", 5837 offline_lu ? "LU" : "LPORT", info ? info : 5838 "<no additional info>"); 5839 } 5840 (void) stmf_ctl(ctl_cmd, ctl_private, &change_info); 5841 } 5842