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