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 /* 27 * Multiplexed I/O SCSI vHCI implementation 28 */ 29 30 #include <sys/conf.h> 31 #include <sys/file.h> 32 #include <sys/ddi.h> 33 #include <sys/sunddi.h> 34 #include <sys/scsi/scsi.h> 35 #include <sys/scsi/impl/scsi_reset_notify.h> 36 #include <sys/scsi/impl/services.h> 37 #include <sys/sunmdi.h> 38 #include <sys/mdi_impldefs.h> 39 #include <sys/scsi/adapters/scsi_vhci.h> 40 #include <sys/disp.h> 41 #include <sys/byteorder.h> 42 43 extern uintptr_t scsi_callback_id; 44 extern ddi_dma_attr_t scsi_alloc_attr; 45 46 #ifdef DEBUG 47 int vhci_debug = VHCI_DEBUG_DEFAULT_VAL; 48 #endif 49 50 /* retry for the vhci_do_prout command when a not ready is returned */ 51 int vhci_prout_not_ready_retry = 180; 52 53 /* 54 * These values are defined to support the internal retry of 55 * SCSI packets for better sense code handling. 56 */ 57 #define VHCI_CMD_CMPLT 0 58 #define VHCI_CMD_RETRY 1 59 #define VHCI_CMD_ERROR -1 60 61 #define PROPFLAGS (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM) 62 #define VHCI_SCSI_PERR 0x47 63 #define VHCI_PGR_ILLEGALOP -2 64 #define VHCI_NUM_UPDATE_TASKQ 8 65 /* changed to 132 to accomodate HDS */ 66 67 /* 68 * Version Macros 69 */ 70 #define VHCI_NAME_VERSION "SCSI VHCI Driver" 71 char vhci_version_name[] = VHCI_NAME_VERSION; 72 73 int vhci_first_time = 0; 74 clock_t vhci_to_ticks = 0; 75 int vhci_init_wait_timeout = VHCI_INIT_WAIT_TIMEOUT; 76 kcondvar_t vhci_cv; 77 kmutex_t vhci_global_mutex; 78 void *vhci_softstate = NULL; /* for soft state */ 79 80 /* 81 * Flag to delay the retry of the reserve command 82 */ 83 int vhci_reserve_delay = 100000; 84 static int vhci_path_quiesce_timeout = 60; 85 static uchar_t zero_key[MHIOC_RESV_KEY_SIZE]; 86 87 /* uscsi delay for a TRAN_BUSY */ 88 static int vhci_uscsi_delay = 100000; 89 static int vhci_uscsi_retry_count = 180; 90 /* uscsi_restart_sense timeout id in case it needs to get canceled */ 91 static timeout_id_t vhci_restart_timeid = 0; 92 93 static int vhci_bus_config_debug = 0; 94 95 /* 96 * Bidirectional map of 'target-port' to port id <pid> for support of 97 * iostat(1M) '-Xx' and '-Yx' output. 98 */ 99 static kmutex_t vhci_targetmap_mutex; 100 static uint_t vhci_targetmap_pid = 1; 101 static mod_hash_t *vhci_targetmap_bypid; /* <pid> -> 'target-port' */ 102 static mod_hash_t *vhci_targetmap_byport; /* 'target-port' -> <pid> */ 103 104 /* 105 * functions exported by scsi_vhci struct cb_ops 106 */ 107 static int vhci_open(dev_t *, int, int, cred_t *); 108 static int vhci_close(dev_t, int, int, cred_t *); 109 static int vhci_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 110 111 /* 112 * functions exported by scsi_vhci struct dev_ops 113 */ 114 static int vhci_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 115 static int vhci_attach(dev_info_t *, ddi_attach_cmd_t); 116 static int vhci_detach(dev_info_t *, ddi_detach_cmd_t); 117 118 /* 119 * functions exported by scsi_vhci scsi_hba_tran_t transport table 120 */ 121 static int vhci_scsi_tgt_init(dev_info_t *, dev_info_t *, 122 scsi_hba_tran_t *, struct scsi_device *); 123 static void vhci_scsi_tgt_free(dev_info_t *, dev_info_t *, scsi_hba_tran_t *, 124 struct scsi_device *); 125 static int vhci_pgr_register_start(scsi_vhci_lun_t *, struct scsi_pkt *); 126 static int vhci_scsi_start(struct scsi_address *, struct scsi_pkt *); 127 static int vhci_scsi_abort(struct scsi_address *, struct scsi_pkt *); 128 static int vhci_scsi_reset(struct scsi_address *, int); 129 static int vhci_scsi_reset_target(struct scsi_address *, int level, 130 uint8_t select_path); 131 static int vhci_scsi_reset_bus(struct scsi_address *); 132 static int vhci_scsi_getcap(struct scsi_address *, char *, int); 133 static int vhci_scsi_setcap(struct scsi_address *, char *, int, int); 134 static int vhci_commoncap(struct scsi_address *, char *, int, int, int); 135 static int vhci_pHCI_cap(struct scsi_address *ap, char *cap, int val, int whom, 136 mdi_pathinfo_t *pip); 137 static struct scsi_pkt *vhci_scsi_init_pkt(struct scsi_address *, 138 struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(), caddr_t); 139 static void vhci_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *); 140 static void vhci_scsi_dmafree(struct scsi_address *, struct scsi_pkt *); 141 static void vhci_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *); 142 static int vhci_scsi_reset_notify(struct scsi_address *, int, void (*)(caddr_t), 143 caddr_t); 144 static int vhci_scsi_get_bus_addr(struct scsi_device *, char *, int); 145 static int vhci_scsi_get_name(struct scsi_device *, char *, int); 146 static int vhci_scsi_bus_power(dev_info_t *, void *, pm_bus_power_op_t, 147 void *, void *); 148 static int vhci_scsi_bus_config(dev_info_t *, uint_t, ddi_bus_config_op_t, 149 void *, dev_info_t **); 150 static int vhci_scsi_bus_unconfig(dev_info_t *, uint_t, ddi_bus_config_op_t, 151 void *); 152 static struct scsi_failover_ops *vhci_dev_fo(dev_info_t *, struct scsi_device *, 153 void **, char **); 154 155 /* 156 * functions registered with the mpxio framework via mdi_vhci_ops_t 157 */ 158 static int vhci_pathinfo_init(dev_info_t *, mdi_pathinfo_t *, int); 159 static int vhci_pathinfo_uninit(dev_info_t *, mdi_pathinfo_t *, int); 160 static int vhci_pathinfo_state_change(dev_info_t *, mdi_pathinfo_t *, 161 mdi_pathinfo_state_t, uint32_t, int); 162 static int vhci_pathinfo_online(dev_info_t *, mdi_pathinfo_t *, int); 163 static int vhci_pathinfo_offline(dev_info_t *, mdi_pathinfo_t *, int); 164 static int vhci_failover(dev_info_t *, dev_info_t *, int); 165 static void vhci_client_attached(dev_info_t *); 166 static int vhci_is_dev_supported(dev_info_t *, dev_info_t *, void *); 167 168 static int vhci_ctl(dev_t, int, intptr_t, int, cred_t *, int *); 169 static int vhci_devctl(dev_t, int, intptr_t, int, cred_t *, int *); 170 static int vhci_ioc_get_phci_path(sv_iocdata_t *, caddr_t, int, caddr_t); 171 static int vhci_ioc_get_client_path(sv_iocdata_t *, caddr_t, int, caddr_t); 172 static int vhci_ioc_get_paddr(sv_iocdata_t *, caddr_t, int, caddr_t); 173 static int vhci_ioc_send_client_path(caddr_t, sv_iocdata_t *, int, caddr_t); 174 static void vhci_ioc_devi_to_path(dev_info_t *, caddr_t); 175 static int vhci_get_phci_path_list(dev_info_t *, sv_path_info_t *, uint_t); 176 static int vhci_get_client_path_list(dev_info_t *, sv_path_info_t *, uint_t); 177 static int vhci_get_iocdata(const void *, sv_iocdata_t *, int, caddr_t); 178 static int vhci_get_iocswitchdata(const void *, sv_switch_to_cntlr_iocdata_t *, 179 int, caddr_t); 180 static int vhci_ioc_alloc_pathinfo(sv_path_info_t **, sv_path_info_t **, 181 uint_t, sv_iocdata_t *, int, caddr_t); 182 static void vhci_ioc_free_pathinfo(sv_path_info_t *, sv_path_info_t *, uint_t); 183 static int vhci_ioc_send_pathinfo(sv_path_info_t *, sv_path_info_t *, uint_t, 184 sv_iocdata_t *, int, caddr_t); 185 static int vhci_handle_ext_fo(struct scsi_pkt *, int); 186 static int vhci_efo_watch_cb(caddr_t, struct scsi_watch_result *); 187 static int vhci_quiesce_lun(struct scsi_vhci_lun *); 188 static int vhci_pgr_validate_and_register(scsi_vhci_priv_t *); 189 static void vhci_dispatch_scsi_start(void *); 190 static void vhci_efo_done(void *); 191 static void vhci_initiate_auto_failback(void *); 192 static void vhci_update_pHCI_pkt(struct vhci_pkt *, struct scsi_pkt *); 193 static int vhci_update_pathinfo(struct scsi_device *, mdi_pathinfo_t *, 194 struct scsi_failover_ops *, scsi_vhci_lun_t *, struct scsi_vhci *); 195 static void vhci_kstat_create_pathinfo(mdi_pathinfo_t *); 196 static int vhci_quiesce_paths(dev_info_t *, dev_info_t *, 197 scsi_vhci_lun_t *, char *, char *); 198 199 static char *vhci_devnm_to_guid(char *); 200 static int vhci_bind_transport(struct scsi_address *, struct vhci_pkt *, 201 int, int (*func)(caddr_t)); 202 static void vhci_intr(struct scsi_pkt *); 203 static int vhci_do_prout(scsi_vhci_priv_t *); 204 static void vhci_run_cmd(void *); 205 static int vhci_do_prin(struct vhci_pkt **); 206 static struct scsi_pkt *vhci_create_retry_pkt(struct vhci_pkt *); 207 static struct vhci_pkt *vhci_sync_retry_pkt(struct vhci_pkt *); 208 static struct scsi_vhci_lun *vhci_lun_lookup(dev_info_t *); 209 static struct scsi_vhci_lun *vhci_lun_lookup_alloc(dev_info_t *, char *, int *); 210 static void vhci_lun_free(dev_info_t *); 211 static int vhci_recovery_reset(scsi_vhci_lun_t *, struct scsi_address *, 212 uint8_t, uint8_t); 213 void vhci_update_pathstates(void *); 214 215 #ifdef DEBUG 216 static void vhci_print_prin_keys(vhci_prin_readkeys_t *, int); 217 static void vhci_print_cdb(dev_info_t *dip, uint_t level, 218 char *title, uchar_t *cdb); 219 static void vhci_clean_print(dev_info_t *dev, uint_t level, 220 char *title, uchar_t *data, int len); 221 #endif 222 static void vhci_print_prout_keys(scsi_vhci_lun_t *, char *); 223 static void vhci_uscsi_iodone(struct scsi_pkt *pkt); 224 static void vhci_invalidate_mpapi_lu(struct scsi_vhci *, scsi_vhci_lun_t *); 225 226 /* 227 * MP-API related functions 228 */ 229 extern int vhci_mpapi_init(struct scsi_vhci *); 230 extern void vhci_mpapi_add_dev_prod(struct scsi_vhci *, char *); 231 extern int vhci_mpapi_ctl(dev_t, int, intptr_t, int, cred_t *, int *); 232 extern void vhci_update_mpapi_data(struct scsi_vhci *, 233 scsi_vhci_lun_t *, mdi_pathinfo_t *); 234 extern void* vhci_get_mpapi_item(struct scsi_vhci *, mpapi_list_header_t *, 235 uint8_t, void*); 236 extern void vhci_mpapi_set_path_state(dev_info_t *, mdi_pathinfo_t *, int); 237 extern int vhci_mpapi_update_tpg_acc_state_for_lu(struct scsi_vhci *, 238 scsi_vhci_lun_t *); 239 240 #define VHCI_DMA_MAX_XFER_CAP INT_MAX 241 242 #define VHCI_MAX_PGR_RETRIES 3 243 244 /* 245 * Macros for the device-type mpxio options 246 */ 247 #define LOAD_BALANCE_OPTIONS "load-balance-options" 248 #define LOGICAL_BLOCK_REGION_SIZE "region-size" 249 #define MPXIO_OPTIONS_LIST "device-type-mpxio-options-list" 250 #define DEVICE_TYPE_STR "device-type" 251 #define isdigit(ch) ((ch) >= '0' && (ch) <= '9') 252 253 static struct cb_ops vhci_cb_ops = { 254 vhci_open, /* open */ 255 vhci_close, /* close */ 256 nodev, /* strategy */ 257 nodev, /* print */ 258 nodev, /* dump */ 259 nodev, /* read */ 260 nodev, /* write */ 261 vhci_ioctl, /* ioctl */ 262 nodev, /* devmap */ 263 nodev, /* mmap */ 264 nodev, /* segmap */ 265 nochpoll, /* chpoll */ 266 ddi_prop_op, /* cb_prop_op */ 267 0, /* streamtab */ 268 D_NEW | D_MP, /* cb_flag */ 269 CB_REV, /* rev */ 270 nodev, /* aread */ 271 nodev /* awrite */ 272 }; 273 274 static struct dev_ops vhci_ops = { 275 DEVO_REV, 276 0, 277 vhci_getinfo, 278 nulldev, /* identify */ 279 nulldev, /* probe */ 280 vhci_attach, /* attach and detach are mandatory */ 281 vhci_detach, 282 nodev, /* reset */ 283 &vhci_cb_ops, /* cb_ops */ 284 NULL, /* bus_ops */ 285 NULL, /* power */ 286 ddi_quiesce_not_needed, /* quiesce */ 287 }; 288 289 extern struct mod_ops mod_driverops; 290 291 static struct modldrv modldrv = { 292 &mod_driverops, 293 vhci_version_name, /* module name */ 294 &vhci_ops 295 }; 296 297 static struct modlinkage modlinkage = { 298 MODREV_1, 299 &modldrv, 300 NULL 301 }; 302 303 static mdi_vhci_ops_t vhci_opinfo = { 304 MDI_VHCI_OPS_REV, 305 vhci_pathinfo_init, /* Pathinfo node init callback */ 306 vhci_pathinfo_uninit, /* Pathinfo uninit callback */ 307 vhci_pathinfo_state_change, /* Pathinfo node state change */ 308 vhci_failover, /* failover callback */ 309 vhci_client_attached, /* client attached callback */ 310 vhci_is_dev_supported /* is device supported by mdi */ 311 }; 312 313 /* 314 * The scsi_failover table defines an ordered set of 'fops' modules supported 315 * by scsi_vhci. Currently, initialize this table from the 'ddi-forceload' 316 * property specified in scsi_vhci.conf. 317 */ 318 static struct scsi_failover { 319 ddi_modhandle_t sf_mod; 320 struct scsi_failover_ops *sf_sfo; 321 } *scsi_failover_table; 322 static uint_t scsi_nfailover; 323 324 int 325 _init(void) 326 { 327 int rval; 328 329 /* 330 * Allocate soft state and prepare to do ddi_soft_state_zalloc() 331 * before registering with the transport first. 332 */ 333 if ((rval = ddi_soft_state_init(&vhci_softstate, 334 sizeof (struct scsi_vhci), 1)) != 0) { 335 VHCI_DEBUG(1, (CE_NOTE, NULL, 336 "!_init:soft state init failed\n")); 337 return (rval); 338 } 339 340 if ((rval = scsi_hba_init(&modlinkage)) != 0) { 341 VHCI_DEBUG(1, (CE_NOTE, NULL, 342 "!_init: scsi hba init failed\n")); 343 ddi_soft_state_fini(&vhci_softstate); 344 return (rval); 345 } 346 347 mutex_init(&vhci_global_mutex, NULL, MUTEX_DRIVER, NULL); 348 cv_init(&vhci_cv, NULL, CV_DRIVER, NULL); 349 350 mutex_init(&vhci_targetmap_mutex, NULL, MUTEX_DRIVER, NULL); 351 vhci_targetmap_byport = mod_hash_create_strhash( 352 "vhci_targetmap_byport", 256, mod_hash_null_valdtor); 353 vhci_targetmap_bypid = mod_hash_create_idhash( 354 "vhci_targetmap_bypid", 256, mod_hash_null_valdtor); 355 356 if ((rval = mod_install(&modlinkage)) != 0) { 357 VHCI_DEBUG(1, (CE_NOTE, NULL, "!_init: mod_install failed\n")); 358 if (vhci_targetmap_bypid) 359 mod_hash_destroy_idhash(vhci_targetmap_bypid); 360 if (vhci_targetmap_byport) 361 mod_hash_destroy_strhash(vhci_targetmap_byport); 362 mutex_destroy(&vhci_targetmap_mutex); 363 cv_destroy(&vhci_cv); 364 mutex_destroy(&vhci_global_mutex); 365 scsi_hba_fini(&modlinkage); 366 ddi_soft_state_fini(&vhci_softstate); 367 } 368 return (rval); 369 } 370 371 372 /* 373 * the system is done with us as a driver, so clean up 374 */ 375 int 376 _fini(void) 377 { 378 int rval; 379 380 /* 381 * don't start cleaning up until we know that the module remove 382 * has worked -- if this works, then we know that each instance 383 * has successfully been DDI_DETACHed 384 */ 385 if ((rval = mod_remove(&modlinkage)) != 0) { 386 VHCI_DEBUG(4, (CE_NOTE, NULL, "!_fini: mod_remove failed\n")); 387 return (rval); 388 } 389 390 if (vhci_targetmap_bypid) 391 mod_hash_destroy_idhash(vhci_targetmap_bypid); 392 if (vhci_targetmap_byport) 393 mod_hash_destroy_strhash(vhci_targetmap_byport); 394 mutex_destroy(&vhci_targetmap_mutex); 395 cv_destroy(&vhci_cv); 396 mutex_destroy(&vhci_global_mutex); 397 scsi_hba_fini(&modlinkage); 398 ddi_soft_state_fini(&vhci_softstate); 399 400 return (rval); 401 } 402 403 int 404 _info(struct modinfo *modinfop) 405 { 406 return (mod_info(&modlinkage, modinfop)); 407 } 408 409 /* 410 * Lookup scsi_failover by "short name" of failover module. 411 */ 412 struct scsi_failover_ops * 413 vhci_failover_ops_by_name(char *name) 414 { 415 struct scsi_failover *sf; 416 417 for (sf = scsi_failover_table; sf->sf_mod; sf++) { 418 if (sf->sf_sfo == NULL) 419 continue; 420 if (strcmp(sf->sf_sfo->sfo_name, name) == 0) 421 return (sf->sf_sfo); 422 } 423 return (NULL); 424 } 425 426 /* 427 * Load all scsi_failover_ops 'fops' modules. 428 */ 429 static void 430 vhci_failover_modopen(struct scsi_vhci *vhci) 431 { 432 char **module; 433 int i; 434 struct scsi_failover *sf; 435 char **dt; 436 int e; 437 438 if (scsi_failover_table) 439 return; 440 441 /* Get the list of modules from scsi_vhci.conf */ 442 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, 443 vhci->vhci_dip, DDI_PROP_DONTPASS, "ddi-forceload", 444 &module, &scsi_nfailover) != DDI_PROP_SUCCESS) { 445 cmn_err(CE_WARN, "scsi_vhci: " 446 "scsi_vhci.conf is missing 'ddi-forceload'"); 447 return; 448 } 449 if (scsi_nfailover == 0) { 450 cmn_err(CE_WARN, "scsi_vhci: " 451 "scsi_vhci.conf has empty 'ddi-forceload'"); 452 ddi_prop_free(module); 453 return; 454 } 455 456 /* allocate failover table based on number of modules */ 457 scsi_failover_table = (struct scsi_failover *) 458 kmem_zalloc(sizeof (struct scsi_failover) * (scsi_nfailover + 1), 459 KM_SLEEP); 460 461 /* loop over modules specified in scsi_vhci.conf and open each module */ 462 for (i = 0, sf = scsi_failover_table; i < scsi_nfailover; i++) { 463 if (module[i] == NULL) 464 continue; 465 466 sf->sf_mod = ddi_modopen(module[i], KRTLD_MODE_FIRST, &e); 467 if (sf->sf_mod == NULL) { 468 /* 469 * A module returns EEXIST if other software is 470 * supporting the intended function: for example 471 * the scsi_vhci_f_sum_emc module returns EEXIST 472 * from _init if EMC powerpath software is installed. 473 */ 474 if (e != EEXIST) 475 cmn_err(CE_WARN, "scsi_vhci: unable to open " 476 "module '%s', error %d", module[i], e); 477 continue; 478 } 479 sf->sf_sfo = ddi_modsym(sf->sf_mod, 480 "scsi_vhci_failover_ops", &e); 481 if (sf->sf_sfo == NULL) { 482 cmn_err(CE_WARN, "scsi_vhci: " 483 "unable to import 'scsi_failover_ops' from '%s', " 484 "error %d", module[i], e); 485 (void) ddi_modclose(sf->sf_mod); 486 sf->sf_mod = NULL; 487 continue; 488 } 489 490 /* register vid/pid of devices supported with mpapi */ 491 for (dt = sf->sf_sfo->sfo_devices; *dt; dt++) 492 vhci_mpapi_add_dev_prod(vhci, *dt); 493 sf++; 494 } 495 496 /* verify that at least the "well-known" modules were there */ 497 if (vhci_failover_ops_by_name(SFO_NAME_SYM) == NULL) 498 cmn_err(CE_WARN, "scsi_vhci: well-known module \"" 499 SFO_NAME_SYM "\" not defined in scsi_vhci.conf's " 500 "'ddi-forceload'"); 501 if (vhci_failover_ops_by_name(SFO_NAME_TPGS) == NULL) 502 cmn_err(CE_WARN, "scsi_vhci: well-known module \"" 503 SFO_NAME_TPGS "\" not defined in scsi_vhci.conf's " 504 "'ddi-forceload'"); 505 506 /* call sfo_init for modules that need it */ 507 for (sf = scsi_failover_table; sf->sf_mod; sf++) { 508 if (sf->sf_sfo && sf->sf_sfo->sfo_init) 509 sf->sf_sfo->sfo_init(); 510 } 511 512 ddi_prop_free(module); 513 } 514 515 /* 516 * unload all loaded scsi_failover_ops modules 517 */ 518 static void 519 vhci_failover_modclose() 520 { 521 struct scsi_failover *sf; 522 523 for (sf = scsi_failover_table; sf->sf_mod; sf++) { 524 if ((sf->sf_mod == NULL) || (sf->sf_sfo == NULL)) 525 continue; 526 (void) ddi_modclose(sf->sf_mod); 527 sf->sf_mod = NULL; 528 sf->sf_sfo = NULL; 529 } 530 531 if (scsi_failover_table && scsi_nfailover) 532 kmem_free(scsi_failover_table, 533 sizeof (struct scsi_failover) * (scsi_nfailover + 1)); 534 scsi_failover_table = NULL; 535 scsi_nfailover = 0; 536 } 537 538 /* ARGSUSED */ 539 static int 540 vhci_open(dev_t *devp, int flag, int otype, cred_t *credp) 541 { 542 struct scsi_vhci *vhci; 543 544 if (otype != OTYP_CHR) { 545 return (EINVAL); 546 } 547 548 vhci = ddi_get_soft_state(vhci_softstate, MINOR2INST(getminor(*devp))); 549 if (vhci == NULL) { 550 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_open: failed ENXIO\n")); 551 return (ENXIO); 552 } 553 554 mutex_enter(&vhci->vhci_mutex); 555 if ((flag & FEXCL) && (vhci->vhci_state & VHCI_STATE_OPEN)) { 556 mutex_exit(&vhci->vhci_mutex); 557 vhci_log(CE_NOTE, vhci->vhci_dip, 558 "!vhci%d: Already open\n", getminor(*devp)); 559 return (EBUSY); 560 } 561 562 vhci->vhci_state |= VHCI_STATE_OPEN; 563 mutex_exit(&vhci->vhci_mutex); 564 return (0); 565 } 566 567 568 /* ARGSUSED */ 569 static int 570 vhci_close(dev_t dev, int flag, int otype, cred_t *credp) 571 { 572 struct scsi_vhci *vhci; 573 574 if (otype != OTYP_CHR) { 575 return (EINVAL); 576 } 577 578 vhci = ddi_get_soft_state(vhci_softstate, MINOR2INST(getminor(dev))); 579 if (vhci == NULL) { 580 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_close: failed ENXIO\n")); 581 return (ENXIO); 582 } 583 584 mutex_enter(&vhci->vhci_mutex); 585 vhci->vhci_state &= ~VHCI_STATE_OPEN; 586 mutex_exit(&vhci->vhci_mutex); 587 588 return (0); 589 } 590 591 /* ARGSUSED */ 592 static int 593 vhci_ioctl(dev_t dev, int cmd, intptr_t data, int mode, 594 cred_t *credp, int *rval) 595 { 596 if (IS_DEVCTL(cmd)) { 597 return (vhci_devctl(dev, cmd, data, mode, credp, rval)); 598 } else if (cmd == MP_CMD) { 599 return (vhci_mpapi_ctl(dev, cmd, data, mode, credp, rval)); 600 } else { 601 return (vhci_ctl(dev, cmd, data, mode, credp, rval)); 602 } 603 } 604 605 /* 606 * attach the module 607 */ 608 static int 609 vhci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 610 { 611 int rval = DDI_FAILURE; 612 int scsi_hba_attached = 0; 613 int vhci_attached = 0; 614 int mutex_initted = 0; 615 int instance; 616 struct scsi_vhci *vhci; 617 scsi_hba_tran_t *tran; 618 char cache_name_buf[64]; 619 char *data; 620 621 VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_attach: cmd=0x%x\n", cmd)); 622 623 instance = ddi_get_instance(dip); 624 625 switch (cmd) { 626 case DDI_ATTACH: 627 break; 628 629 case DDI_RESUME: 630 case DDI_PM_RESUME: 631 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_attach: resume not yet" 632 "implemented\n")); 633 return (rval); 634 635 default: 636 VHCI_DEBUG(1, (CE_NOTE, NULL, 637 "!vhci_attach: unknown ddi command\n")); 638 return (rval); 639 } 640 641 /* 642 * Allocate vhci data structure. 643 */ 644 if (ddi_soft_state_zalloc(vhci_softstate, instance) != DDI_SUCCESS) { 645 VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:" 646 "soft state alloc failed\n")); 647 return (DDI_FAILURE); 648 } 649 650 if ((vhci = ddi_get_soft_state(vhci_softstate, instance)) == NULL) { 651 VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:" 652 "bad soft state\n")); 653 ddi_soft_state_free(vhci_softstate, instance); 654 return (DDI_FAILURE); 655 } 656 657 /* Allocate packet cache */ 658 (void) snprintf(cache_name_buf, sizeof (cache_name_buf), 659 "vhci%d_cache", instance); 660 661 mutex_init(&vhci->vhci_mutex, NULL, MUTEX_DRIVER, NULL); 662 mutex_initted++; 663 664 /* 665 * Allocate a transport structure 666 */ 667 tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 668 ASSERT(tran != NULL); 669 670 vhci->vhci_tran = tran; 671 vhci->vhci_dip = dip; 672 vhci->vhci_instance = instance; 673 674 tran->tran_hba_private = vhci; 675 tran->tran_tgt_init = vhci_scsi_tgt_init; 676 tran->tran_tgt_probe = NULL; 677 tran->tran_tgt_free = vhci_scsi_tgt_free; 678 679 tran->tran_start = vhci_scsi_start; 680 tran->tran_abort = vhci_scsi_abort; 681 tran->tran_reset = vhci_scsi_reset; 682 tran->tran_getcap = vhci_scsi_getcap; 683 tran->tran_setcap = vhci_scsi_setcap; 684 tran->tran_init_pkt = vhci_scsi_init_pkt; 685 tran->tran_destroy_pkt = vhci_scsi_destroy_pkt; 686 tran->tran_dmafree = vhci_scsi_dmafree; 687 tran->tran_sync_pkt = vhci_scsi_sync_pkt; 688 tran->tran_reset_notify = vhci_scsi_reset_notify; 689 690 tran->tran_get_bus_addr = vhci_scsi_get_bus_addr; 691 tran->tran_get_name = vhci_scsi_get_name; 692 tran->tran_bus_reset = NULL; 693 tran->tran_quiesce = NULL; 694 tran->tran_unquiesce = NULL; 695 696 /* 697 * register event notification routines with scsa 698 */ 699 tran->tran_get_eventcookie = NULL; 700 tran->tran_add_eventcall = NULL; 701 tran->tran_remove_eventcall = NULL; 702 tran->tran_post_event = NULL; 703 704 tran->tran_bus_power = vhci_scsi_bus_power; 705 706 tran->tran_bus_config = vhci_scsi_bus_config; 707 tran->tran_bus_unconfig = vhci_scsi_bus_unconfig; 708 709 /* 710 * Attach this instance with the mpxio framework 711 */ 712 if (mdi_vhci_register(MDI_HCI_CLASS_SCSI, dip, &vhci_opinfo, 0) 713 != MDI_SUCCESS) { 714 VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:" 715 "mdi_vhci_register failed\n")); 716 goto attach_fail; 717 } 718 vhci_attached++; 719 720 /* 721 * Attach this instance of the hba. 722 * 723 * Regarding dma attributes: Since scsi_vhci is a virtual scsi HBA 724 * driver, it has nothing to do with DMA. However, when calling 725 * scsi_hba_attach_setup() we need to pass something valid in the 726 * dma attributes parameter. So we just use scsi_alloc_attr. 727 * SCSA itself seems to care only for dma_attr_minxfer and 728 * dma_attr_burstsizes fields of dma attributes structure. 729 * It expects those fileds to be non-zero. 730 */ 731 if (scsi_hba_attach_setup(dip, &scsi_alloc_attr, tran, 732 SCSI_HBA_ADDR_COMPLEX) != DDI_SUCCESS) { 733 VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:" 734 "hba attach failed\n")); 735 goto attach_fail; 736 } 737 scsi_hba_attached++; 738 739 if (ddi_create_minor_node(dip, "devctl", S_IFCHR, 740 INST2DEVCTL(instance), DDI_NT_SCSI_NEXUS, 0) != DDI_SUCCESS) { 741 VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:" 742 " ddi_create_minor_node failed\n")); 743 goto attach_fail; 744 } 745 746 /* 747 * Set pm-want-child-notification property for 748 * power management of the phci and client 749 */ 750 if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, 751 "pm-want-child-notification?", NULL, NULL) != DDI_PROP_SUCCESS) { 752 cmn_err(CE_WARN, 753 "%s%d fail to create pm-want-child-notification? prop", 754 ddi_driver_name(dip), ddi_get_instance(dip)); 755 goto attach_fail; 756 } 757 758 vhci->vhci_taskq = taskq_create("vhci_taskq", 1, MINCLSYSPRI, 1, 4, 0); 759 vhci->vhci_update_pathstates_taskq = 760 taskq_create("vhci_update_pathstates", VHCI_NUM_UPDATE_TASKQ, 761 MINCLSYSPRI, 1, 4, 0); 762 ASSERT(vhci->vhci_taskq); 763 ASSERT(vhci->vhci_update_pathstates_taskq); 764 765 /* 766 * Set appropriate configuration flags based on options set in 767 * conf file. 768 */ 769 vhci->vhci_conf_flags = 0; 770 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, PROPFLAGS, 771 "auto-failback", &data) == DDI_SUCCESS) { 772 if (strcmp(data, "enable") == 0) 773 vhci->vhci_conf_flags |= VHCI_CONF_FLAGS_AUTO_FAILBACK; 774 ddi_prop_free(data); 775 } 776 777 if (!(vhci->vhci_conf_flags & VHCI_CONF_FLAGS_AUTO_FAILBACK)) 778 vhci_log(CE_NOTE, dip, "!Auto-failback capability " 779 "disabled through scsi_vhci.conf file."); 780 781 /* 782 * Allocate an mpapi private structure 783 */ 784 vhci->mp_priv = kmem_zalloc(sizeof (mpapi_priv_t), KM_SLEEP); 785 if (vhci_mpapi_init(vhci) != 0) { 786 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_attach: " 787 "vhci_mpapi_init() failed")); 788 } 789 790 vhci_failover_modopen(vhci); /* load failover modules */ 791 792 ddi_report_dev(dip); 793 return (DDI_SUCCESS); 794 795 attach_fail: 796 if (vhci_attached) 797 (void) mdi_vhci_unregister(dip, 0); 798 799 if (scsi_hba_attached) 800 (void) scsi_hba_detach(dip); 801 802 if (vhci->vhci_tran) 803 scsi_hba_tran_free(vhci->vhci_tran); 804 805 if (mutex_initted) { 806 mutex_destroy(&vhci->vhci_mutex); 807 } 808 809 ddi_soft_state_free(vhci_softstate, instance); 810 return (DDI_FAILURE); 811 } 812 813 814 /*ARGSUSED*/ 815 static int 816 vhci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 817 { 818 int instance = ddi_get_instance(dip); 819 scsi_hba_tran_t *tran; 820 struct scsi_vhci *vhci; 821 822 VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_detach: cmd=0x%x\n", cmd)); 823 824 if ((tran = ddi_get_driver_private(dip)) == NULL) 825 return (DDI_FAILURE); 826 827 vhci = TRAN2HBAPRIVATE(tran); 828 if (!vhci) { 829 return (DDI_FAILURE); 830 } 831 832 switch (cmd) { 833 case DDI_DETACH: 834 break; 835 836 case DDI_SUSPEND: 837 case DDI_PM_SUSPEND: 838 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_detach: suspend/pm not yet" 839 "implemented\n")); 840 return (DDI_FAILURE); 841 842 default: 843 VHCI_DEBUG(1, (CE_NOTE, NULL, 844 "!vhci_detach: unknown ddi command\n")); 845 return (DDI_FAILURE); 846 } 847 848 (void) mdi_vhci_unregister(dip, 0); 849 (void) scsi_hba_detach(dip); 850 scsi_hba_tran_free(tran); 851 852 if (ddi_prop_remove(DDI_DEV_T_NONE, dip, 853 "pm-want-child-notification?") != DDI_PROP_SUCCESS) { 854 cmn_err(CE_WARN, 855 "%s%d unable to remove prop pm-want_child_notification?", 856 ddi_driver_name(dip), ddi_get_instance(dip)); 857 } 858 if (vhci_restart_timeid != 0) { 859 (void) untimeout(vhci_restart_timeid); 860 } 861 vhci_restart_timeid = 0; 862 863 mutex_destroy(&vhci->vhci_mutex); 864 vhci->vhci_dip = NULL; 865 vhci->vhci_tran = NULL; 866 taskq_destroy(vhci->vhci_taskq); 867 taskq_destroy(vhci->vhci_update_pathstates_taskq); 868 ddi_remove_minor_node(dip, NULL); 869 ddi_soft_state_free(vhci_softstate, instance); 870 871 vhci_failover_modclose(); /* unload failover modules */ 872 return (DDI_SUCCESS); 873 } 874 875 /* 876 * vhci_getinfo() 877 * Given the device number, return the devinfo pointer or the 878 * instance number. 879 * Note: always succeed DDI_INFO_DEVT2INSTANCE, even before attach. 880 */ 881 882 /*ARGSUSED*/ 883 static int 884 vhci_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 885 { 886 struct scsi_vhci *vhcip; 887 int instance = MINOR2INST(getminor((dev_t)arg)); 888 889 switch (cmd) { 890 case DDI_INFO_DEVT2DEVINFO: 891 vhcip = ddi_get_soft_state(vhci_softstate, instance); 892 if (vhcip != NULL) 893 *result = vhcip->vhci_dip; 894 else { 895 *result = NULL; 896 return (DDI_FAILURE); 897 } 898 break; 899 900 case DDI_INFO_DEVT2INSTANCE: 901 *result = (void *)(uintptr_t)instance; 902 break; 903 904 default: 905 return (DDI_FAILURE); 906 } 907 908 return (DDI_SUCCESS); 909 } 910 911 /*ARGSUSED*/ 912 static int 913 vhci_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 914 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 915 { 916 char *guid; 917 scsi_vhci_lun_t *vlun; 918 struct scsi_vhci *vhci; 919 clock_t from_ticks; 920 mdi_pathinfo_t *pip; 921 int rval; 922 923 ASSERT(hba_dip != NULL); 924 ASSERT(tgt_dip != NULL); 925 926 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip, PROPFLAGS, 927 MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS) { 928 /* 929 * This must be the .conf node without GUID property. 930 * The node under fp already inserts a delay, so we 931 * just return from here. We rely on this delay to have 932 * all dips be posted to the ndi hotplug thread's newdev 933 * list. This is necessary for the deferred attach 934 * mechanism to work and opens() done soon after boot to 935 * succeed. 936 */ 937 VHCI_DEBUG(4, (CE_WARN, hba_dip, "tgt_init: lun guid " 938 "property failed")); 939 return (DDI_NOT_WELL_FORMED); 940 } 941 942 if (ndi_dev_is_persistent_node(tgt_dip) == 0) { 943 /* 944 * This must be .conf node with the GUID property. We don't 945 * merge property by ndi_merge_node() here because the 946 * devi_addr_buf of .conf node is "" always according the 947 * implementation of vhci_scsi_get_name_bus_addr(). 948 */ 949 ddi_set_name_addr(tgt_dip, NULL); 950 return (DDI_FAILURE); 951 } 952 953 vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(hba_dip)); 954 ASSERT(vhci != NULL); 955 956 VHCI_DEBUG(4, (CE_NOTE, hba_dip, 957 "!tgt_init: called for %s (instance %d)\n", 958 ddi_driver_name(tgt_dip), ddi_get_instance(tgt_dip))); 959 960 vlun = vhci_lun_lookup(tgt_dip); 961 962 mutex_enter(&vhci_global_mutex); 963 964 from_ticks = ddi_get_lbolt(); 965 if (vhci_to_ticks == 0) { 966 vhci_to_ticks = from_ticks + 967 drv_usectohz(vhci_init_wait_timeout); 968 } 969 970 #if DEBUG 971 if (vlun) { 972 VHCI_DEBUG(1, (CE_WARN, hba_dip, "tgt_init: " 973 "vhci_scsi_tgt_init: guid %s : found vlun 0x%p " 974 "from_ticks %lx to_ticks %lx", 975 guid, (void *)vlun, from_ticks, vhci_to_ticks)); 976 } else { 977 VHCI_DEBUG(1, (CE_WARN, hba_dip, "tgt_init: " 978 "vhci_scsi_tgt_init: guid %s : vlun not found " 979 "from_ticks %lx to_ticks %lx", guid, from_ticks, 980 vhci_to_ticks)); 981 } 982 #endif 983 984 rval = mdi_select_path(tgt_dip, NULL, 985 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH), NULL, &pip); 986 if (rval == MDI_SUCCESS) { 987 mdi_rele_path(pip); 988 } 989 990 /* 991 * Wait for the following conditions : 992 * 1. no vlun available yet 993 * 2. no path established 994 * 3. timer did not expire 995 */ 996 while ((vlun == NULL) || (mdi_client_get_path_count(tgt_dip) == 0) || 997 (rval != MDI_SUCCESS)) { 998 if (vlun && vlun->svl_not_supported) { 999 VHCI_DEBUG(1, (CE_WARN, hba_dip, "tgt_init: " 1000 "vlun 0x%p lun guid %s not supported!", 1001 (void *)vlun, guid)); 1002 mutex_exit(&vhci_global_mutex); 1003 ddi_prop_free(guid); 1004 return (DDI_NOT_WELL_FORMED); 1005 } 1006 if ((vhci_first_time == 0) && (from_ticks >= vhci_to_ticks)) { 1007 vhci_first_time = 1; 1008 } 1009 if (vhci_first_time == 1) { 1010 VHCI_DEBUG(1, (CE_WARN, hba_dip, "vhci_scsi_tgt_init: " 1011 "no wait for %s. from_tick %lx, to_tick %lx", 1012 guid, from_ticks, vhci_to_ticks)); 1013 mutex_exit(&vhci_global_mutex); 1014 ddi_prop_free(guid); 1015 return (DDI_NOT_WELL_FORMED); 1016 } 1017 1018 if (cv_timedwait(&vhci_cv, 1019 &vhci_global_mutex, vhci_to_ticks) == -1) { 1020 /* Timed out */ 1021 #ifdef DEBUG 1022 if (vlun == NULL) { 1023 VHCI_DEBUG(1, (CE_WARN, hba_dip, 1024 "tgt_init: no vlun for %s!", guid)); 1025 } else if (mdi_client_get_path_count(tgt_dip) == 0) { 1026 VHCI_DEBUG(1, (CE_WARN, hba_dip, 1027 "tgt_init: client path count is " 1028 "zero for %s!", guid)); 1029 } else { 1030 VHCI_DEBUG(1, (CE_WARN, hba_dip, 1031 "tgt_init: client path not " 1032 "available yet for %s!", guid)); 1033 } 1034 #endif /* DEBUG */ 1035 mutex_exit(&vhci_global_mutex); 1036 ddi_prop_free(guid); 1037 return (DDI_NOT_WELL_FORMED); 1038 } 1039 vlun = vhci_lun_lookup(tgt_dip); 1040 rval = mdi_select_path(tgt_dip, NULL, 1041 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH), 1042 NULL, &pip); 1043 if (rval == MDI_SUCCESS) { 1044 mdi_rele_path(pip); 1045 } 1046 from_ticks = ddi_get_lbolt(); 1047 } 1048 mutex_exit(&vhci_global_mutex); 1049 1050 ASSERT(vlun != NULL); 1051 ddi_prop_free(guid); 1052 1053 scsi_device_hba_private_set(sd, vlun); 1054 1055 return (DDI_SUCCESS); 1056 } 1057 1058 /*ARGSUSED*/ 1059 static void 1060 vhci_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1061 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 1062 { 1063 } 1064 1065 /* 1066 * a PGR register command has started; copy the info we need 1067 */ 1068 int 1069 vhci_pgr_register_start(scsi_vhci_lun_t *vlun, struct scsi_pkt *pkt) 1070 { 1071 struct vhci_pkt *vpkt = TGTPKT2VHCIPKT(pkt); 1072 void *addr; 1073 1074 if (!vpkt->vpkt_tgt_init_bp) 1075 return (TRAN_BADPKT); 1076 1077 addr = bp_mapin_common(vpkt->vpkt_tgt_init_bp, 1078 (vpkt->vpkt_flags & CFLAG_NOWAIT) ? VM_NOSLEEP : VM_SLEEP); 1079 if (addr == NULL) 1080 return (TRAN_BUSY); 1081 1082 mutex_enter(&vlun->svl_mutex); 1083 1084 vhci_print_prout_keys(vlun, "v_pgr_reg_start: before bcopy:"); 1085 1086 bcopy(addr, &vlun->svl_prout, sizeof (vhci_prout_t) - 1087 (2 * MHIOC_RESV_KEY_SIZE*sizeof (char))); 1088 bcopy(pkt->pkt_cdbp, vlun->svl_cdb, sizeof (vlun->svl_cdb)); 1089 1090 vhci_print_prout_keys(vlun, "v_pgr_reg_start: after bcopy:"); 1091 1092 vlun->svl_time = pkt->pkt_time; 1093 vlun->svl_bcount = vpkt->vpkt_tgt_init_bp->b_bcount; 1094 vlun->svl_first_path = vpkt->vpkt_path; 1095 mutex_exit(&vlun->svl_mutex); 1096 return (0); 1097 } 1098 1099 /* 1100 * Function name : vhci_scsi_start() 1101 * 1102 * Return Values : TRAN_FATAL_ERROR - vhci has been shutdown 1103 * or other fatal failure 1104 * preventing packet transportation 1105 * TRAN_BUSY - request queue is full 1106 * TRAN_ACCEPT - pkt has been submitted to phci 1107 * (or is held in the waitQ) 1108 * Description : Implements SCSA's tran_start() entry point for 1109 * packet transport 1110 * 1111 */ 1112 static int 1113 vhci_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt) 1114 { 1115 int rval = TRAN_ACCEPT; 1116 int instance, held; 1117 struct scsi_vhci *vhci = ADDR2VHCI(ap); 1118 struct scsi_vhci_lun *vlun = ADDR2VLUN(ap); 1119 struct vhci_pkt *vpkt = TGTPKT2VHCIPKT(pkt); 1120 int flags = 0; 1121 scsi_vhci_priv_t *svp; 1122 dev_info_t *cdip; 1123 client_lb_t lbp; 1124 int restore_lbp = 0; 1125 /* set if pkt is SCSI-II RESERVE cmd */ 1126 int pkt_reserve_cmd = 0; 1127 int reserve_failed = 0; 1128 1129 ASSERT(vhci != NULL); 1130 ASSERT(vpkt != NULL); 1131 ASSERT(vpkt->vpkt_state != VHCI_PKT_ISSUED); 1132 cdip = ADDR2DIP(ap); 1133 1134 /* 1135 * Block IOs if LUN is held or QUIESCED for IOs. 1136 */ 1137 if ((VHCI_LUN_IS_HELD(vlun)) || 1138 ((vlun->svl_flags & VLUN_QUIESCED_FLG) == VLUN_QUIESCED_FLG)) { 1139 return (TRAN_BUSY); 1140 } 1141 1142 /* 1143 * vhci_lun needs to be quiesced before SCSI-II RESERVE command 1144 * can be issued. This may require a cv_timedwait, which is 1145 * dangerous to perform in an interrupt context. So if this 1146 * is a RESERVE command a taskq is dispatched to service it. 1147 * This taskq shall again call vhci_scsi_start, but we shall be 1148 * sure its not in an interrupt context. 1149 */ 1150 if ((pkt->pkt_cdbp[0] == SCMD_RESERVE) || 1151 (pkt->pkt_cdbp[0] == SCMD_RESERVE_G1)) { 1152 if (!(vpkt->vpkt_state & VHCI_PKT_THRU_TASKQ)) { 1153 if (taskq_dispatch(vhci->vhci_taskq, 1154 vhci_dispatch_scsi_start, (void *) vpkt, 1155 KM_NOSLEEP)) { 1156 return (TRAN_ACCEPT); 1157 } else { 1158 return (TRAN_BUSY); 1159 } 1160 } 1161 1162 /* 1163 * Here we ensure that simultaneous SCSI-II RESERVE cmds don't 1164 * get serviced for a lun. 1165 */ 1166 VHCI_HOLD_LUN(vlun, VH_NOSLEEP, held); 1167 if (!held) { 1168 return (TRAN_BUSY); 1169 } else if ((vlun->svl_flags & VLUN_QUIESCED_FLG) == 1170 VLUN_QUIESCED_FLG) { 1171 VHCI_RELEASE_LUN(vlun); 1172 return (TRAN_BUSY); 1173 } 1174 1175 /* 1176 * To ensure that no IOs occur for this LUN for the duration 1177 * of this pkt set the VLUN_QUIESCED_FLG. 1178 * In case this routine needs to exit on error make sure that 1179 * this flag is cleared. 1180 */ 1181 vlun->svl_flags |= VLUN_QUIESCED_FLG; 1182 pkt_reserve_cmd = 1; 1183 1184 /* 1185 * if this is a SCSI-II RESERVE command, set load balancing 1186 * policy to be ALTERNATE PATH to ensure that all subsequent 1187 * IOs are routed on the same path. This is because if commands 1188 * are routed across multiple paths then IOs on paths other than 1189 * the one on which the RESERVE was executed will get a 1190 * RESERVATION CONFLICT 1191 */ 1192 lbp = mdi_get_lb_policy(cdip); 1193 if (lbp != LOAD_BALANCE_NONE) { 1194 if (vhci_quiesce_lun(vlun) != 1) { 1195 vlun->svl_flags &= ~VLUN_QUIESCED_FLG; 1196 VHCI_RELEASE_LUN(vlun); 1197 return (TRAN_FATAL_ERROR); 1198 } 1199 vlun->svl_lb_policy_save = lbp; 1200 if (mdi_set_lb_policy(cdip, LOAD_BALANCE_NONE) != 1201 MDI_SUCCESS) { 1202 vlun->svl_flags &= ~VLUN_QUIESCED_FLG; 1203 VHCI_RELEASE_LUN(vlun); 1204 return (TRAN_FATAL_ERROR); 1205 } 1206 restore_lbp = 1; 1207 } 1208 /* 1209 * See comments for VLUN_RESERVE_ACTIVE_FLG in scsi_vhci.h 1210 * To narrow this window where a reserve command may be sent 1211 * down an inactive path the path states first need to be 1212 * updated. Before calling vhci_update_pathstates reset 1213 * VLUN_RESERVE_ACTIVE_FLG, just in case it was already set 1214 * for this lun. This shall prevent an unnecessary reset 1215 * from being sent out. 1216 */ 1217 vlun->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG; 1218 vhci_update_pathstates((void *)vlun); 1219 } 1220 1221 instance = ddi_get_instance(vhci->vhci_dip); 1222 1223 /* 1224 * If the command is PRIN with action of zero, then the cmd 1225 * is reading PR keys which requires filtering on completion. 1226 * Data cache sync must be guaranteed. 1227 */ 1228 if ((pkt->pkt_cdbp[0] == SCMD_PRIN) && (pkt->pkt_cdbp[1] == 0) && 1229 (vpkt->vpkt_org_vpkt == NULL)) { 1230 vpkt->vpkt_tgt_init_pkt_flags |= PKT_CONSISTENT; 1231 } 1232 1233 /* 1234 * Do not defer bind for PKT_DMA_PARTIAL 1235 */ 1236 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) { 1237 1238 /* This is a non pkt_dma_partial case */ 1239 if ((rval = vhci_bind_transport( 1240 ap, vpkt, vpkt->vpkt_tgt_init_pkt_flags, NULL_FUNC)) 1241 != TRAN_ACCEPT) { 1242 VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip, 1243 "!vhci%d %x: failed to bind transport: " 1244 "vlun 0x%p pkt_reserved %x restore_lbp %x," 1245 "lbp %x", instance, rval, (void *)vlun, 1246 pkt_reserve_cmd, restore_lbp, lbp)); 1247 if (restore_lbp) 1248 (void) mdi_set_lb_policy(cdip, lbp); 1249 if (pkt_reserve_cmd) 1250 vlun->svl_flags &= ~VLUN_QUIESCED_FLG; 1251 return (rval); 1252 } 1253 VHCI_DEBUG(8, (CE_NOTE, NULL, 1254 "vhci_scsi_start: v_b_t called 0x%p\n", (void *)vpkt)); 1255 } 1256 ASSERT(vpkt->vpkt_hba_pkt != NULL); 1257 ASSERT(vpkt->vpkt_path != NULL); 1258 1259 /* 1260 * This is the chance to adjust the pHCI's pkt and other information 1261 * from target driver's pkt. 1262 */ 1263 VHCI_DEBUG(8, (CE_NOTE, vhci->vhci_dip, "vhci_scsi_start vpkt %p\n", 1264 (void *)vpkt)); 1265 vhci_update_pHCI_pkt(vpkt, pkt); 1266 1267 if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) { 1268 if (vpkt->vpkt_path != vlun->svl_resrv_pip) { 1269 VHCI_DEBUG(1, (CE_WARN, vhci->vhci_dip, 1270 "!vhci_bind: reserve flag set for vlun 0x%p, but, " 1271 "pktpath 0x%p resrv path 0x%p differ. lb_policy %x", 1272 (void *)vlun, (void *)vpkt->vpkt_path, 1273 (void *)vlun->svl_resrv_pip, 1274 mdi_get_lb_policy(cdip))); 1275 reserve_failed = 1; 1276 } 1277 } 1278 1279 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(vpkt->vpkt_path); 1280 if (svp == NULL || reserve_failed) { 1281 if (pkt_reserve_cmd) { 1282 VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip, 1283 "!vhci_bind returned null svp vlun 0x%p", 1284 (void *)vlun)); 1285 vlun->svl_flags &= ~VLUN_QUIESCED_FLG; 1286 if (restore_lbp) 1287 (void) mdi_set_lb_policy(cdip, lbp); 1288 } 1289 pkt_cleanup: 1290 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) { 1291 scsi_destroy_pkt(vpkt->vpkt_hba_pkt); 1292 vpkt->vpkt_hba_pkt = NULL; 1293 if (vpkt->vpkt_path) { 1294 mdi_rele_path(vpkt->vpkt_path); 1295 vpkt->vpkt_path = NULL; 1296 } 1297 } 1298 if ((pkt->pkt_cdbp[0] == SCMD_PROUT) && 1299 (((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) || 1300 ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_R_AND_IGNORE))) { 1301 sema_v(&vlun->svl_pgr_sema); 1302 } 1303 return (TRAN_BUSY); 1304 } 1305 1306 VHCI_INCR_PATH_CMDCOUNT(svp); 1307 1308 /* 1309 * Ensure that no other IOs raced ahead, while a RESERVE cmd was 1310 * QUIESCING the same lun. 1311 */ 1312 if ((!pkt_reserve_cmd) && 1313 ((vlun->svl_flags & VLUN_QUIESCED_FLG) == VLUN_QUIESCED_FLG)) { 1314 VHCI_DECR_PATH_CMDCOUNT(svp); 1315 goto pkt_cleanup; 1316 } 1317 1318 if ((pkt->pkt_cdbp[0] == SCMD_PRIN) || 1319 (pkt->pkt_cdbp[0] == SCMD_PROUT)) { 1320 /* 1321 * currently this thread only handles running PGR 1322 * commands, so don't bother creating it unless 1323 * something interesting is going to happen (like 1324 * either a PGR out, or a PGR in with enough space 1325 * to hold the keys that are getting returned) 1326 */ 1327 mutex_enter(&vlun->svl_mutex); 1328 if (((vlun->svl_flags & VLUN_TASK_D_ALIVE_FLG) == 0) && 1329 (pkt->pkt_cdbp[0] == SCMD_PROUT)) { 1330 vlun->svl_taskq = taskq_create("vlun_pgr_task_daemon", 1331 1, MINCLSYSPRI, 1, 4, 0); 1332 vlun->svl_flags |= VLUN_TASK_D_ALIVE_FLG; 1333 } 1334 mutex_exit(&vlun->svl_mutex); 1335 if ((pkt->pkt_cdbp[0] == SCMD_PROUT) && 1336 (((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) || 1337 ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_R_AND_IGNORE))) { 1338 if (rval = vhci_pgr_register_start(vlun, pkt)) { 1339 /* an error */ 1340 sema_v(&vlun->svl_pgr_sema); 1341 return (rval); 1342 } 1343 } 1344 } 1345 1346 /* 1347 * SCSI-II RESERVE cmd is not expected in polled mode. 1348 * If this changes it needs to be handled for the polled scenario. 1349 */ 1350 flags = vpkt->vpkt_hba_pkt->pkt_flags; 1351 1352 /* 1353 * Set the path_instance *before* sending the scsi_pkt down the path 1354 * to mpxio's pHCI so that additional path abstractions at a pHCI 1355 * level (like maybe iSCSI at some point in the future) can update 1356 * the path_instance. 1357 */ 1358 if (scsi_pkt_allocated_correctly(vpkt->vpkt_hba_pkt)) 1359 vpkt->vpkt_hba_pkt->pkt_path_instance = 1360 mdi_pi_get_path_instance(vpkt->vpkt_path); 1361 1362 rval = scsi_transport(vpkt->vpkt_hba_pkt); 1363 if (rval == TRAN_ACCEPT) { 1364 if (flags & FLAG_NOINTR) { 1365 struct scsi_pkt *tpkt = vpkt->vpkt_tgt_pkt; 1366 struct scsi_pkt *pkt = vpkt->vpkt_hba_pkt; 1367 1368 ASSERT(tpkt != NULL); 1369 *(tpkt->pkt_scbp) = *(pkt->pkt_scbp); 1370 tpkt->pkt_resid = pkt->pkt_resid; 1371 tpkt->pkt_state = pkt->pkt_state; 1372 tpkt->pkt_statistics = pkt->pkt_statistics; 1373 tpkt->pkt_reason = pkt->pkt_reason; 1374 1375 if ((*(pkt->pkt_scbp) == STATUS_CHECK) && 1376 (pkt->pkt_state & STATE_ARQ_DONE)) { 1377 bcopy(pkt->pkt_scbp, tpkt->pkt_scbp, 1378 vpkt->vpkt_tgt_init_scblen); 1379 } 1380 1381 VHCI_DECR_PATH_CMDCOUNT(svp); 1382 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) { 1383 scsi_destroy_pkt(vpkt->vpkt_hba_pkt); 1384 vpkt->vpkt_hba_pkt = NULL; 1385 if (vpkt->vpkt_path) { 1386 mdi_rele_path(vpkt->vpkt_path); 1387 vpkt->vpkt_path = NULL; 1388 } 1389 } 1390 /* 1391 * This path will not automatically retry pkts 1392 * internally, therefore, vpkt_org_vpkt should 1393 * never be set. 1394 */ 1395 ASSERT(vpkt->vpkt_org_vpkt == NULL); 1396 scsi_hba_pkt_comp(tpkt); 1397 } 1398 return (rval); 1399 } else if ((pkt->pkt_cdbp[0] == SCMD_PROUT) && 1400 (((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) || 1401 ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_R_AND_IGNORE))) { 1402 /* the command exited with bad status */ 1403 sema_v(&vlun->svl_pgr_sema); 1404 } else if (vpkt->vpkt_tgt_pkt->pkt_cdbp[0] == SCMD_PRIN) { 1405 /* the command exited with bad status */ 1406 sema_v(&vlun->svl_pgr_sema); 1407 } else if (pkt_reserve_cmd) { 1408 VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip, 1409 "!vhci_scsi_start: reserve failed vlun 0x%p", 1410 (void *)vlun)); 1411 vlun->svl_flags &= ~VLUN_QUIESCED_FLG; 1412 if (restore_lbp) 1413 (void) mdi_set_lb_policy(cdip, lbp); 1414 } 1415 1416 ASSERT(vpkt->vpkt_hba_pkt != NULL); 1417 VHCI_DECR_PATH_CMDCOUNT(svp); 1418 1419 /* Do not destroy phci packet information for PKT_DMA_PARTIAL */ 1420 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) { 1421 scsi_destroy_pkt(vpkt->vpkt_hba_pkt); 1422 vpkt->vpkt_hba_pkt = NULL; 1423 if (vpkt->vpkt_path) { 1424 MDI_PI_ERRSTAT(vpkt->vpkt_path, MDI_PI_TRANSERR); 1425 mdi_rele_path(vpkt->vpkt_path); 1426 vpkt->vpkt_path = NULL; 1427 } 1428 } 1429 return (TRAN_BUSY); 1430 } 1431 1432 /* 1433 * Function name : vhci_scsi_reset() 1434 * 1435 * Return Values : 0 - reset failed 1436 * 1 - reset succeeded 1437 */ 1438 1439 /* ARGSUSED */ 1440 static int 1441 vhci_scsi_reset(struct scsi_address *ap, int level) 1442 { 1443 int rval = 0; 1444 1445 cmn_err(CE_WARN, "!vhci_scsi_reset 0x%x", level); 1446 if ((level == RESET_TARGET) || (level == RESET_LUN)) { 1447 return (vhci_scsi_reset_target(ap, level, TRUE)); 1448 } else if (level == RESET_ALL) { 1449 return (vhci_scsi_reset_bus(ap)); 1450 } 1451 1452 return (rval); 1453 } 1454 1455 /* 1456 * vhci_recovery_reset: 1457 * Issues reset to the device 1458 * Input: 1459 * vlun - vhci lun pointer of the device 1460 * ap - address of the device 1461 * select_path: 1462 * If select_path is FALSE, then the address specified in ap is 1463 * the path on which reset will be issued. 1464 * If select_path is TRUE, then path is obtained by calling 1465 * mdi_select_path. 1466 * 1467 * recovery_depth: 1468 * Caller can specify the level of reset. 1469 * VHCI_DEPTH_LUN - 1470 * Issues LUN RESET if device supports lun reset. 1471 * VHCI_DEPTH_TARGET - 1472 * If Lun Reset fails or the device does not support 1473 * Lun Reset, issues TARGET RESET 1474 * VHCI_DEPTH_ALL - 1475 * If Lun Reset fails or the device does not support 1476 * Lun Reset, issues TARGET RESET. 1477 * If TARGET RESET does not succeed, issues Bus Reset. 1478 */ 1479 1480 static int 1481 vhci_recovery_reset(scsi_vhci_lun_t *vlun, struct scsi_address *ap, 1482 uint8_t select_path, uint8_t recovery_depth) 1483 { 1484 int ret = 0; 1485 1486 ASSERT(ap != NULL); 1487 1488 if (vlun && vlun->svl_support_lun_reset == 1) { 1489 ret = vhci_scsi_reset_target(ap, RESET_LUN, 1490 select_path); 1491 } 1492 1493 recovery_depth--; 1494 1495 if ((ret == 0) && recovery_depth) { 1496 ret = vhci_scsi_reset_target(ap, RESET_TARGET, 1497 select_path); 1498 recovery_depth--; 1499 } 1500 1501 if ((ret == 0) && recovery_depth) { 1502 (void) scsi_reset(ap, RESET_ALL); 1503 } 1504 1505 return (ret); 1506 } 1507 1508 /* 1509 * Note: The scsi_address passed to this routine could be the scsi_address 1510 * for the virtual device or the physical device. No assumptions should be 1511 * made in this routine about the contents of the ap structure. 1512 * Further, note that the child dip would be the dip of the ssd node regardless 1513 * of the scsi_address passed in. 1514 */ 1515 static int 1516 vhci_scsi_reset_target(struct scsi_address *ap, int level, uint8_t select_path) 1517 { 1518 dev_info_t *vdip, *cdip; 1519 mdi_pathinfo_t *pip = NULL; 1520 mdi_pathinfo_t *npip = NULL; 1521 int rval = -1; 1522 scsi_vhci_priv_t *svp = NULL; 1523 struct scsi_address *pap = NULL; 1524 scsi_hba_tran_t *hba = NULL; 1525 int sps; 1526 struct scsi_vhci *vhci = NULL; 1527 1528 if (select_path != TRUE) { 1529 ASSERT(ap != NULL); 1530 if (level == RESET_LUN) { 1531 hba = ap->a_hba_tran; 1532 ASSERT(hba != NULL); 1533 return (hba->tran_reset(ap, RESET_LUN)); 1534 } 1535 return (scsi_reset(ap, level)); 1536 } 1537 1538 cdip = ADDR2DIP(ap); 1539 ASSERT(cdip != NULL); 1540 vdip = ddi_get_parent(cdip); 1541 ASSERT(vdip != NULL); 1542 vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip)); 1543 ASSERT(vhci != NULL); 1544 1545 rval = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH, NULL, &pip); 1546 if ((rval != MDI_SUCCESS) || (pip == NULL)) { 1547 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_scsi_reset_target: " 1548 "Unable to get a path, dip 0x%p", (void *)cdip)); 1549 return (0); 1550 } 1551 again: 1552 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip); 1553 if (svp == NULL) { 1554 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_scsi_reset_target: " 1555 "priv is NULL, pip 0x%p", (void *)pip)); 1556 mdi_rele_path(pip); 1557 return (0); 1558 } 1559 1560 if (svp->svp_psd == NULL) { 1561 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_scsi_reset_target: " 1562 "psd is NULL, pip 0x%p, svp 0x%p", 1563 (void *)pip, (void *)svp)); 1564 mdi_rele_path(pip); 1565 return (0); 1566 } 1567 1568 pap = &svp->svp_psd->sd_address; 1569 hba = pap->a_hba_tran; 1570 1571 ASSERT(pap != NULL); 1572 ASSERT(hba != NULL); 1573 1574 if (hba->tran_reset != NULL) { 1575 if (hba->tran_reset(pap, level) == 0) { 1576 vhci_log(CE_WARN, vdip, "!%s%d: " 1577 "path %s, reset %d failed", 1578 ddi_driver_name(cdip), ddi_get_instance(cdip), 1579 mdi_pi_spathname(pip), level); 1580 1581 /* 1582 * Select next path and issue the reset, repeat 1583 * until all paths are exhausted 1584 */ 1585 sps = mdi_select_path(cdip, NULL, 1586 MDI_SELECT_ONLINE_PATH, pip, &npip); 1587 if ((sps != MDI_SUCCESS) || (npip == NULL)) { 1588 mdi_rele_path(pip); 1589 return (0); 1590 } 1591 mdi_rele_path(pip); 1592 pip = npip; 1593 goto again; 1594 } 1595 mdi_rele_path(pip); 1596 mutex_enter(&vhci->vhci_mutex); 1597 scsi_hba_reset_notify_callback(&vhci->vhci_mutex, 1598 &vhci->vhci_reset_notify_listf); 1599 mutex_exit(&vhci->vhci_mutex); 1600 VHCI_DEBUG(6, (CE_NOTE, NULL, "!vhci_scsi_reset_target: " 1601 "reset %d sent down pip:%p for cdip:%p\n", level, 1602 (void *)pip, (void *)cdip)); 1603 return (1); 1604 } 1605 mdi_rele_path(pip); 1606 return (0); 1607 } 1608 1609 1610 /* ARGSUSED */ 1611 static int 1612 vhci_scsi_reset_bus(struct scsi_address *ap) 1613 { 1614 return (1); 1615 } 1616 1617 1618 /* 1619 * called by vhci_getcap and vhci_setcap to get and set (respectively) 1620 * SCSI capabilities 1621 */ 1622 /* ARGSUSED */ 1623 static int 1624 vhci_commoncap(struct scsi_address *ap, char *cap, 1625 int val, int tgtonly, int doset) 1626 { 1627 struct scsi_vhci *vhci = ADDR2VHCI(ap); 1628 struct scsi_vhci_lun *vlun = ADDR2VLUN(ap); 1629 int cidx; 1630 int rval = 0; 1631 1632 if (cap == (char *)0) { 1633 VHCI_DEBUG(3, (CE_WARN, vhci->vhci_dip, 1634 "!vhci_commoncap: invalid arg")); 1635 return (rval); 1636 } 1637 1638 if (vlun == NULL) { 1639 VHCI_DEBUG(3, (CE_WARN, vhci->vhci_dip, 1640 "!vhci_commoncap: vlun is null")); 1641 return (rval); 1642 } 1643 1644 if ((cidx = scsi_hba_lookup_capstr(cap)) == -1) { 1645 return (UNDEFINED); 1646 } 1647 1648 /* 1649 * Process setcap request. 1650 */ 1651 if (doset) { 1652 /* 1653 * At present, we can only set binary (0/1) values 1654 */ 1655 switch (cidx) { 1656 case SCSI_CAP_ARQ: 1657 if (val == 0) { 1658 rval = 0; 1659 } else { 1660 rval = 1; 1661 } 1662 break; 1663 1664 case SCSI_CAP_LUN_RESET: 1665 if (tgtonly == 0) { 1666 VHCI_DEBUG(1, (CE_WARN, vhci->vhci_dip, 1667 "scsi_vhci_setcap: " 1668 "Returning error since whom = 0")); 1669 rval = -1; 1670 break; 1671 } 1672 /* 1673 * Set the capability accordingly. 1674 */ 1675 mutex_enter(&vlun->svl_mutex); 1676 vlun->svl_support_lun_reset = val; 1677 rval = val; 1678 mutex_exit(&vlun->svl_mutex); 1679 break; 1680 1681 case SCSI_CAP_SECTOR_SIZE: 1682 mutex_enter(&vlun->svl_mutex); 1683 vlun->svl_sector_size = val; 1684 vlun->svl_setcap_done = 1; 1685 mutex_exit(&vlun->svl_mutex); 1686 (void) vhci_pHCI_cap(ap, cap, val, tgtonly, NULL); 1687 1688 /* Always return success */ 1689 rval = 1; 1690 break; 1691 1692 default: 1693 VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip, 1694 "!vhci_setcap: unsupported %d", cidx)); 1695 rval = UNDEFINED; 1696 break; 1697 } 1698 1699 VHCI_DEBUG(6, (CE_NOTE, vhci->vhci_dip, 1700 "!set cap: cap=%s, val/tgtonly/doset/rval = " 1701 "0x%x/0x%x/0x%x/%d\n", 1702 cap, val, tgtonly, doset, rval)); 1703 1704 } else { 1705 /* 1706 * Process getcap request. 1707 */ 1708 switch (cidx) { 1709 case SCSI_CAP_DMA_MAX: 1710 /* 1711 * For X86 this capability is caught in scsi_ifgetcap(). 1712 * XXX Should this be getting the value from the pHCI? 1713 */ 1714 rval = (int)VHCI_DMA_MAX_XFER_CAP; 1715 break; 1716 1717 case SCSI_CAP_INITIATOR_ID: 1718 rval = 0x00; 1719 break; 1720 1721 case SCSI_CAP_ARQ: 1722 case SCSI_CAP_RESET_NOTIFICATION: 1723 case SCSI_CAP_TAGGED_QING: 1724 rval = 1; 1725 break; 1726 1727 case SCSI_CAP_SCSI_VERSION: 1728 rval = 3; 1729 break; 1730 1731 case SCSI_CAP_INTERCONNECT_TYPE: 1732 rval = INTERCONNECT_FABRIC; 1733 break; 1734 1735 case SCSI_CAP_LUN_RESET: 1736 /* 1737 * scsi_vhci will always return success for LUN reset. 1738 * When request for doing LUN reset comes 1739 * through scsi_reset entry point, at that time attempt 1740 * will be made to do reset through all the possible 1741 * paths. 1742 */ 1743 mutex_enter(&vlun->svl_mutex); 1744 rval = vlun->svl_support_lun_reset; 1745 mutex_exit(&vlun->svl_mutex); 1746 VHCI_DEBUG(4, (CE_WARN, vhci->vhci_dip, 1747 "scsi_vhci_getcap:" 1748 "Getting the Lun reset capability %d", rval)); 1749 break; 1750 1751 case SCSI_CAP_SECTOR_SIZE: 1752 mutex_enter(&vlun->svl_mutex); 1753 rval = vlun->svl_sector_size; 1754 mutex_exit(&vlun->svl_mutex); 1755 break; 1756 1757 case SCSI_CAP_CDB_LEN: 1758 rval = VHCI_SCSI_CDB_SIZE; 1759 break; 1760 1761 case SCSI_CAP_DMA_MAX_ARCH: 1762 /* 1763 * For X86 this capability is caught in scsi_ifgetcap(). 1764 * XXX Should this be getting the value from the pHCI? 1765 */ 1766 rval = 0; 1767 break; 1768 1769 default: 1770 VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip, 1771 "!vhci_getcap: unsupported %d", cidx)); 1772 rval = UNDEFINED; 1773 break; 1774 } 1775 1776 VHCI_DEBUG(6, (CE_NOTE, vhci->vhci_dip, 1777 "!get cap: cap=%s, val/tgtonly/doset/rval = " 1778 "0x%x/0x%x/0x%x/%d\n", 1779 cap, val, tgtonly, doset, rval)); 1780 } 1781 return (rval); 1782 } 1783 1784 1785 /* 1786 * Function name : vhci_scsi_getcap() 1787 * 1788 */ 1789 static int 1790 vhci_scsi_getcap(struct scsi_address *ap, char *cap, int whom) 1791 { 1792 return (vhci_commoncap(ap, cap, 0, whom, 0)); 1793 } 1794 1795 static int 1796 vhci_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom) 1797 { 1798 return (vhci_commoncap(ap, cap, value, whom, 1)); 1799 } 1800 1801 /* 1802 * Function name : vhci_scsi_abort() 1803 */ 1804 /* ARGSUSED */ 1805 static int 1806 vhci_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt) 1807 { 1808 return (0); 1809 } 1810 1811 /* 1812 * Function name : vhci_scsi_init_pkt 1813 * 1814 * Return Values : pointer to scsi_pkt, or NULL 1815 */ 1816 /* ARGSUSED */ 1817 static struct scsi_pkt * 1818 vhci_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 1819 struct buf *bp, int cmdlen, int statuslen, int tgtlen, 1820 int flags, int (*callback)(caddr_t), caddr_t arg) 1821 { 1822 struct scsi_vhci *vhci = ADDR2VHCI(ap); 1823 struct vhci_pkt *vpkt; 1824 int rval; 1825 int newpkt = 0; 1826 struct scsi_pkt *pktp; 1827 1828 1829 if (pkt == NULL) { 1830 if (cmdlen > VHCI_SCSI_CDB_SIZE) { 1831 if ((cmdlen != VHCI_SCSI_OSD_CDB_SIZE) || 1832 ((flags & VHCI_SCSI_OSD_PKT_FLAGS) != 1833 VHCI_SCSI_OSD_PKT_FLAGS)) { 1834 VHCI_DEBUG(1, (CE_NOTE, NULL, 1835 "!init pkt: cdb size not supported\n")); 1836 return (NULL); 1837 } 1838 } 1839 1840 pktp = scsi_hba_pkt_alloc(vhci->vhci_dip, 1841 ap, cmdlen, statuslen, tgtlen, sizeof (*vpkt), callback, 1842 arg); 1843 1844 if (pktp == NULL) { 1845 return (NULL); 1846 } 1847 1848 /* Get the vhci's private structure */ 1849 vpkt = (struct vhci_pkt *)(pktp->pkt_ha_private); 1850 ASSERT(vpkt); 1851 1852 /* Save the target driver's packet */ 1853 vpkt->vpkt_tgt_pkt = pktp; 1854 1855 /* 1856 * Save pkt_tgt_init_pkt fields if deferred binding 1857 * is needed or for other purposes. 1858 */ 1859 vpkt->vpkt_tgt_init_pkt_flags = flags; 1860 vpkt->vpkt_flags = (callback == NULL_FUNC) ? CFLAG_NOWAIT : 0; 1861 vpkt->vpkt_state = VHCI_PKT_IDLE; 1862 vpkt->vpkt_tgt_init_cdblen = cmdlen; 1863 vpkt->vpkt_tgt_init_scblen = statuslen; 1864 newpkt = 1; 1865 } else { /* pkt not NULL */ 1866 vpkt = pkt->pkt_ha_private; 1867 } 1868 1869 VHCI_DEBUG(8, (CE_NOTE, NULL, "vhci_scsi_init_pkt " 1870 "vpkt %p flags %x\n", (void *)vpkt, flags)); 1871 1872 /* Clear any stale error flags */ 1873 if (bp) { 1874 bioerror(bp, 0); 1875 } 1876 1877 vpkt->vpkt_tgt_init_bp = bp; 1878 1879 if (flags & PKT_DMA_PARTIAL) { 1880 1881 /* 1882 * Immediate binding is needed. 1883 * Target driver may not set this flag in next invocation. 1884 * vhci has to remember this flag was set during first 1885 * invocation of vhci_scsi_init_pkt. 1886 */ 1887 vpkt->vpkt_flags |= CFLAG_DMA_PARTIAL; 1888 } 1889 1890 if (vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) { 1891 1892 /* 1893 * Re-initialize some of the target driver packet state 1894 * information. 1895 */ 1896 vpkt->vpkt_tgt_pkt->pkt_state = 0; 1897 vpkt->vpkt_tgt_pkt->pkt_statistics = 0; 1898 vpkt->vpkt_tgt_pkt->pkt_reason = 0; 1899 1900 /* 1901 * Binding a vpkt->vpkt_path for this IO at init_time. 1902 * If an IO error happens later, target driver will clear 1903 * this vpkt->vpkt_path binding before re-init IO again. 1904 */ 1905 VHCI_DEBUG(8, (CE_NOTE, NULL, 1906 "vhci_scsi_init_pkt: calling v_b_t %p, newpkt %d\n", 1907 (void *)vpkt, newpkt)); 1908 if (pkt && vpkt->vpkt_hba_pkt) { 1909 VHCI_DEBUG(4, (CE_NOTE, NULL, 1910 "v_s_i_p calling update_pHCI_pkt resid %ld\n", 1911 pkt->pkt_resid)); 1912 vhci_update_pHCI_pkt(vpkt, pkt); 1913 } 1914 if (callback == SLEEP_FUNC) { 1915 rval = vhci_bind_transport( 1916 ap, vpkt, flags, callback); 1917 } else { 1918 rval = vhci_bind_transport( 1919 ap, vpkt, flags, NULL_FUNC); 1920 } 1921 VHCI_DEBUG(8, (CE_NOTE, NULL, 1922 "vhci_scsi_init_pkt: v_b_t called 0x%p rval 0x%x\n", 1923 (void *)vpkt, rval)); 1924 if (bp) { 1925 if (rval == TRAN_FATAL_ERROR) { 1926 /* 1927 * No paths available. Could not bind 1928 * any pHCI. Setting EFAULT as a way 1929 * to indicate no DMA is mapped. 1930 */ 1931 bioerror(bp, EFAULT); 1932 } else { 1933 /* 1934 * Do not indicate any pHCI errors to 1935 * target driver otherwise. 1936 */ 1937 bioerror(bp, 0); 1938 } 1939 } 1940 if (rval != TRAN_ACCEPT) { 1941 VHCI_DEBUG(8, (CE_NOTE, NULL, 1942 "vhci_scsi_init_pkt: " 1943 "v_b_t failed 0x%p newpkt %x\n", 1944 (void *)vpkt, newpkt)); 1945 if (newpkt) { 1946 scsi_hba_pkt_free(ap, 1947 vpkt->vpkt_tgt_pkt); 1948 } 1949 return (NULL); 1950 } 1951 ASSERT(vpkt->vpkt_hba_pkt != NULL); 1952 ASSERT(vpkt->vpkt_path != NULL); 1953 1954 /* Update the resid for the target driver */ 1955 vpkt->vpkt_tgt_pkt->pkt_resid = 1956 vpkt->vpkt_hba_pkt->pkt_resid; 1957 } 1958 1959 return (vpkt->vpkt_tgt_pkt); 1960 } 1961 1962 /* 1963 * Function name : vhci_scsi_destroy_pkt 1964 * 1965 * Return Values : none 1966 */ 1967 static void 1968 vhci_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 1969 { 1970 struct vhci_pkt *vpkt = (struct vhci_pkt *)pkt->pkt_ha_private; 1971 1972 VHCI_DEBUG(8, (CE_NOTE, NULL, 1973 "vhci_scsi_destroy_pkt: vpkt 0x%p\n", (void *)vpkt)); 1974 1975 vpkt->vpkt_tgt_init_pkt_flags = 0; 1976 if (vpkt->vpkt_hba_pkt) { 1977 scsi_destroy_pkt(vpkt->vpkt_hba_pkt); 1978 vpkt->vpkt_hba_pkt = NULL; 1979 } 1980 if (vpkt->vpkt_path) { 1981 mdi_rele_path(vpkt->vpkt_path); 1982 vpkt->vpkt_path = NULL; 1983 } 1984 1985 ASSERT(vpkt->vpkt_state != VHCI_PKT_ISSUED); 1986 scsi_hba_pkt_free(ap, vpkt->vpkt_tgt_pkt); 1987 } 1988 1989 /* 1990 * Function name : vhci_scsi_dmafree() 1991 * 1992 * Return Values : none 1993 */ 1994 /*ARGSUSED*/ 1995 static void 1996 vhci_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 1997 { 1998 struct vhci_pkt *vpkt = (struct vhci_pkt *)pkt->pkt_ha_private; 1999 2000 VHCI_DEBUG(6, (CE_NOTE, NULL, 2001 "vhci_scsi_dmafree: vpkt 0x%p\n", (void *)vpkt)); 2002 2003 ASSERT(vpkt != NULL); 2004 if (vpkt->vpkt_hba_pkt) { 2005 scsi_destroy_pkt(vpkt->vpkt_hba_pkt); 2006 vpkt->vpkt_hba_pkt = NULL; 2007 } 2008 if (vpkt->vpkt_path) { 2009 mdi_rele_path(vpkt->vpkt_path); 2010 vpkt->vpkt_path = NULL; 2011 } 2012 } 2013 2014 /* 2015 * Function name : vhci_scsi_sync_pkt() 2016 * 2017 * Return Values : none 2018 */ 2019 /*ARGSUSED*/ 2020 static void 2021 vhci_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2022 { 2023 struct vhci_pkt *vpkt = (struct vhci_pkt *)pkt->pkt_ha_private; 2024 2025 ASSERT(vpkt != NULL); 2026 if (vpkt->vpkt_hba_pkt) { 2027 scsi_sync_pkt(vpkt->vpkt_hba_pkt); 2028 } 2029 } 2030 2031 /* 2032 * routine for reset notification setup, to register or cancel. 2033 */ 2034 static int 2035 vhci_scsi_reset_notify(struct scsi_address *ap, int flag, 2036 void (*callback)(caddr_t), caddr_t arg) 2037 { 2038 struct scsi_vhci *vhci = ADDR2VHCI(ap); 2039 return (scsi_hba_reset_notify_setup(ap, flag, callback, arg, 2040 &vhci->vhci_mutex, &vhci->vhci_reset_notify_listf)); 2041 } 2042 2043 static int 2044 vhci_scsi_get_name_bus_addr(struct scsi_device *sd, 2045 char *name, int len, int bus_addr) 2046 { 2047 dev_info_t *cdip; 2048 char *guid; 2049 scsi_vhci_lun_t *vlun; 2050 2051 ASSERT(sd != NULL); 2052 ASSERT(name != NULL); 2053 2054 *name = 0; 2055 cdip = sd->sd_dev; 2056 2057 ASSERT(cdip != NULL); 2058 2059 if (mdi_component_is_client(cdip, NULL) != MDI_SUCCESS) 2060 return (1); 2061 2062 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, cdip, PROPFLAGS, 2063 MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS) 2064 return (1); 2065 2066 /* 2067 * Message is "sd# at scsi_vhci0: unit-address <guid>: <bus_addr>". 2068 * <guid> bus_addr argument == 0 2069 * <bus_addr> bus_addr argument != 0 2070 * Since the <guid> is already provided with unit-address, we just 2071 * provide failover module in <bus_addr> to keep output shorter. 2072 */ 2073 vlun = ADDR2VLUN(&sd->sd_address); 2074 if (bus_addr == 0) { 2075 /* report the guid: */ 2076 (void) snprintf(name, len, "g%s", guid); 2077 } else if (vlun && vlun->svl_fops_name) { 2078 /* report the name of the failover module */ 2079 (void) snprintf(name, len, "%s", vlun->svl_fops_name); 2080 } 2081 2082 ddi_prop_free(guid); 2083 return (1); 2084 } 2085 2086 static int 2087 vhci_scsi_get_bus_addr(struct scsi_device *sd, char *name, int len) 2088 { 2089 return (vhci_scsi_get_name_bus_addr(sd, name, len, 1)); 2090 } 2091 2092 static int 2093 vhci_scsi_get_name(struct scsi_device *sd, char *name, int len) 2094 { 2095 return (vhci_scsi_get_name_bus_addr(sd, name, len, 0)); 2096 } 2097 2098 /* 2099 * Return a pointer to the guid part of the devnm. 2100 * devnm format is "nodename@busaddr", busaddr format is "gGUID". 2101 */ 2102 static char * 2103 vhci_devnm_to_guid(char *devnm) 2104 { 2105 char *cp = devnm; 2106 2107 if (devnm == NULL) 2108 return (NULL); 2109 2110 while (*cp != '\0' && *cp != '@') 2111 cp++; 2112 if (*cp == '@' && *(cp + 1) == 'g') 2113 return (cp + 2); 2114 return (NULL); 2115 } 2116 2117 static int 2118 vhci_bind_transport(struct scsi_address *ap, struct vhci_pkt *vpkt, int flags, 2119 int (*func)(caddr_t)) 2120 { 2121 struct scsi_vhci *vhci = ADDR2VHCI(ap); 2122 dev_info_t *cdip = ADDR2DIP(ap); 2123 mdi_pathinfo_t *pip = NULL; 2124 mdi_pathinfo_t *npip = NULL; 2125 scsi_vhci_priv_t *svp = NULL; 2126 struct scsi_device *psd = NULL; 2127 struct scsi_address *address = NULL; 2128 struct scsi_pkt *pkt = NULL; 2129 int rval = -1; 2130 int pgr_sema_held = 0; 2131 int held; 2132 int mps_flag = MDI_SELECT_ONLINE_PATH; 2133 struct scsi_vhci_lun *vlun; 2134 time_t tnow; 2135 int path_instance = 0; 2136 2137 vlun = ADDR2VLUN(ap); 2138 ASSERT(vlun != 0); 2139 2140 if ((vpkt->vpkt_tgt_pkt->pkt_cdbp[0] == SCMD_PROUT) && 2141 (((vpkt->vpkt_tgt_pkt->pkt_cdbp[1] & 0x1f) == 2142 VHCI_PROUT_REGISTER) || 2143 ((vpkt->vpkt_tgt_pkt->pkt_cdbp[1] & 0x1f) == 2144 VHCI_PROUT_R_AND_IGNORE))) { 2145 if (!sema_tryp(&vlun->svl_pgr_sema)) 2146 return (TRAN_BUSY); 2147 pgr_sema_held = 1; 2148 if (vlun->svl_first_path != NULL) { 2149 rval = mdi_select_path(cdip, NULL, 2150 MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH, 2151 NULL, &pip); 2152 if ((rval != MDI_SUCCESS) || (pip == NULL)) { 2153 VHCI_DEBUG(4, (CE_NOTE, NULL, 2154 "vhci_bind_transport: path select fail\n")); 2155 } else { 2156 npip = pip; 2157 do { 2158 if (npip == vlun->svl_first_path) { 2159 VHCI_DEBUG(4, (CE_NOTE, NULL, 2160 "vhci_bind_transport: " 2161 "valid first path 0x%p\n", 2162 (void *) 2163 vlun->svl_first_path)); 2164 pip = vlun->svl_first_path; 2165 goto bind_path; 2166 } 2167 pip = npip; 2168 rval = mdi_select_path(cdip, NULL, 2169 MDI_SELECT_ONLINE_PATH | 2170 MDI_SELECT_STANDBY_PATH, 2171 pip, &npip); 2172 mdi_rele_path(pip); 2173 } while ((rval == MDI_SUCCESS) && 2174 (npip != NULL)); 2175 } 2176 } 2177 2178 if (vlun->svl_first_path) { 2179 VHCI_DEBUG(4, (CE_NOTE, NULL, 2180 "vhci_bind_transport: invalid first path 0x%p\n", 2181 (void *)vlun->svl_first_path)); 2182 vlun->svl_first_path = NULL; 2183 } 2184 } else if (vpkt->vpkt_tgt_pkt->pkt_cdbp[0] == SCMD_PRIN) { 2185 if ((vpkt->vpkt_state & VHCI_PKT_THRU_TASKQ) == 0) { 2186 if (!sema_tryp(&vlun->svl_pgr_sema)) 2187 return (TRAN_BUSY); 2188 } 2189 pgr_sema_held = 1; 2190 } 2191 2192 /* 2193 * If the path is already bound for PKT_PARTIAL_DMA case, 2194 * try to use the same path. 2195 */ 2196 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) && vpkt->vpkt_path) { 2197 VHCI_DEBUG(4, (CE_NOTE, NULL, 2198 "vhci_bind_transport: PKT_PARTIAL_DMA " 2199 "vpkt 0x%p, path 0x%p\n", 2200 (void *)vpkt, (void *)vpkt->vpkt_path)); 2201 pip = vpkt->vpkt_path; 2202 goto bind_path; 2203 } 2204 2205 /* 2206 * Get path_instance. Non-zero with FLAG_PKT_PATH_INSTANCE set 2207 * indicates that mdi_select_path should be called to select a 2208 * specific instance. 2209 * 2210 * NB: Condition pkt_path_instance reference on proper allocation. 2211 */ 2212 if ((vpkt->vpkt_tgt_pkt->pkt_flags & FLAG_PKT_PATH_INSTANCE) && 2213 scsi_pkt_allocated_correctly(vpkt->vpkt_tgt_pkt)) { 2214 path_instance = vpkt->vpkt_tgt_pkt->pkt_path_instance; 2215 } 2216 2217 /* 2218 * If reservation is active bind the transport directly to the pip 2219 * with the reservation. 2220 */ 2221 if (vpkt->vpkt_hba_pkt == NULL) { 2222 if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) { 2223 if (MDI_PI_IS_ONLINE(vlun->svl_resrv_pip)) { 2224 pip = vlun->svl_resrv_pip; 2225 mdi_hold_path(pip); 2226 vlun->svl_waiting_for_activepath = 0; 2227 rval = MDI_SUCCESS; 2228 goto bind_path; 2229 } else { 2230 if (pgr_sema_held) { 2231 sema_v(&vlun->svl_pgr_sema); 2232 } 2233 return (TRAN_BUSY); 2234 } 2235 } 2236 try_again: 2237 rval = mdi_select_path(cdip, vpkt->vpkt_tgt_init_bp, 2238 path_instance ? MDI_SELECT_PATH_INSTANCE : 0, 2239 (void *)(intptr_t)path_instance, &pip); 2240 if (rval == MDI_BUSY) { 2241 if (pgr_sema_held) { 2242 sema_v(&vlun->svl_pgr_sema); 2243 } 2244 return (TRAN_BUSY); 2245 } else if (rval == MDI_DEVI_ONLINING) { 2246 /* 2247 * if we are here then we are in the midst of 2248 * an attach/probe of the client device. 2249 * We attempt to bind to ONLINE path if available, 2250 * else it is OK to bind to a STANDBY path (instead 2251 * of triggering a failover) because IO associated 2252 * with attach/probe (eg. INQUIRY, block 0 read) 2253 * are completed by targets even on passive paths 2254 * If no ONLINE paths available, it is important 2255 * to set svl_waiting_for_activepath for two 2256 * reasons: (1) avoid sense analysis in the 2257 * "external failure detection" codepath in 2258 * vhci_intr(). Failure to do so will result in 2259 * infinite loop (unless an ONLINE path becomes 2260 * available at some point) (2) avoid 2261 * unnecessary failover (see "---Waiting For Active 2262 * Path---" comment below). 2263 */ 2264 VHCI_DEBUG(1, (CE_NOTE, NULL, "!%p in onlining " 2265 "state\n", (void *)cdip)); 2266 pip = NULL; 2267 rval = mdi_select_path(cdip, vpkt->vpkt_tgt_init_bp, 2268 mps_flag, NULL, &pip); 2269 if ((rval != MDI_SUCCESS) || (pip == NULL)) { 2270 if (vlun->svl_waiting_for_activepath == 0) { 2271 vlun->svl_waiting_for_activepath = 1; 2272 vlun->svl_wfa_time = ddi_get_time(); 2273 } 2274 mps_flag |= MDI_SELECT_STANDBY_PATH; 2275 rval = mdi_select_path(cdip, 2276 vpkt->vpkt_tgt_init_bp, 2277 mps_flag, NULL, &pip); 2278 if ((rval != MDI_SUCCESS) || (pip == NULL)) { 2279 if (pgr_sema_held) { 2280 sema_v(&vlun->svl_pgr_sema); 2281 } 2282 return (TRAN_FATAL_ERROR); 2283 } 2284 goto bind_path; 2285 } 2286 } else if (rval == MDI_FAILURE) { 2287 if (pgr_sema_held) { 2288 sema_v(&vlun->svl_pgr_sema); 2289 } 2290 return (TRAN_FATAL_ERROR); 2291 } 2292 2293 if ((pip == NULL) || (rval == MDI_NOPATH)) { 2294 while (vlun->svl_waiting_for_activepath) { 2295 /* 2296 * ---Waiting For Active Path--- 2297 * This device was discovered across a 2298 * passive path; lets wait for a little 2299 * bit, hopefully an active path will 2300 * show up obviating the need for a 2301 * failover 2302 */ 2303 tnow = ddi_get_time(); 2304 if (tnow - vlun->svl_wfa_time >= 60) { 2305 vlun->svl_waiting_for_activepath = 0; 2306 } else { 2307 drv_usecwait(1000); 2308 if (vlun->svl_waiting_for_activepath 2309 == 0) { 2310 /* 2311 * an active path has come 2312 * online! 2313 */ 2314 goto try_again; 2315 } 2316 } 2317 } 2318 VHCI_HOLD_LUN(vlun, VH_NOSLEEP, held); 2319 if (!held) { 2320 VHCI_DEBUG(4, (CE_NOTE, NULL, 2321 "!Lun not held\n")); 2322 if (pgr_sema_held) { 2323 sema_v(&vlun->svl_pgr_sema); 2324 } 2325 return (TRAN_BUSY); 2326 } 2327 /* 2328 * now that the LUN is stable, one last check 2329 * to make sure no other changes sneaked in 2330 * (like a path coming online or a 2331 * failover initiated by another thread) 2332 */ 2333 pip = NULL; 2334 rval = mdi_select_path(cdip, vpkt->vpkt_tgt_init_bp, 2335 0, NULL, &pip); 2336 if (pip != NULL) { 2337 VHCI_RELEASE_LUN(vlun); 2338 vlun->svl_waiting_for_activepath = 0; 2339 goto bind_path; 2340 } 2341 2342 /* 2343 * Check if there is an ONLINE path OR a STANDBY path 2344 * available. If none is available, do not attempt 2345 * to do a failover, just return a fatal error at this 2346 * point. 2347 */ 2348 npip = NULL; 2349 rval = mdi_select_path(cdip, NULL, 2350 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH), 2351 NULL, &npip); 2352 if ((npip == NULL) || (rval != MDI_SUCCESS)) { 2353 /* 2354 * No paths available, jus return FATAL error. 2355 */ 2356 VHCI_RELEASE_LUN(vlun); 2357 if (pgr_sema_held) { 2358 sema_v(&vlun->svl_pgr_sema); 2359 } 2360 return (TRAN_FATAL_ERROR); 2361 } 2362 mdi_rele_path(npip); 2363 VHCI_DEBUG(1, (CE_NOTE, NULL, "!invoking " 2364 "mdi_failover\n")); 2365 rval = mdi_failover(vhci->vhci_dip, cdip, 2366 MDI_FAILOVER_ASYNC); 2367 if (rval == MDI_FAILURE) { 2368 VHCI_RELEASE_LUN(vlun); 2369 if (pgr_sema_held) { 2370 sema_v(&vlun->svl_pgr_sema); 2371 } 2372 return (TRAN_FATAL_ERROR); 2373 } else if (rval == MDI_BUSY) { 2374 VHCI_RELEASE_LUN(vlun); 2375 if (pgr_sema_held) { 2376 sema_v(&vlun->svl_pgr_sema); 2377 } 2378 return (TRAN_BUSY); 2379 } else { 2380 if (pgr_sema_held) { 2381 sema_v(&vlun->svl_pgr_sema); 2382 } 2383 return (TRAN_BUSY); 2384 } 2385 } 2386 vlun->svl_waiting_for_activepath = 0; 2387 bind_path: 2388 vpkt->vpkt_path = pip; 2389 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip); 2390 ASSERT(svp != NULL); 2391 2392 psd = svp->svp_psd; 2393 ASSERT(psd != NULL); 2394 address = &psd->sd_address; 2395 } else { 2396 pkt = vpkt->vpkt_hba_pkt; 2397 address = &pkt->pkt_address; 2398 } 2399 2400 /* Verify match of specified path_instance and selected path_instance */ 2401 ASSERT((path_instance == 0) || 2402 (path_instance == mdi_pi_get_path_instance(vpkt->vpkt_path))); 2403 2404 /* 2405 * For PKT_PARTIAL_DMA case, call pHCI's scsi_init_pkt whenever 2406 * target driver calls vhci_scsi_init_pkt. 2407 */ 2408 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) && 2409 vpkt->vpkt_path && vpkt->vpkt_hba_pkt) { 2410 VHCI_DEBUG(4, (CE_NOTE, NULL, 2411 "vhci_bind_transport: PKT_PARTIAL_DMA " 2412 "vpkt 0x%p, path 0x%p hba_pkt 0x%p\n", 2413 (void *)vpkt, (void *)vpkt->vpkt_path, (void *)pkt)); 2414 pkt = vpkt->vpkt_hba_pkt; 2415 address = &pkt->pkt_address; 2416 } 2417 2418 if (pkt == NULL || (vpkt->vpkt_flags & CFLAG_DMA_PARTIAL)) { 2419 pkt = scsi_init_pkt(address, pkt, 2420 vpkt->vpkt_tgt_init_bp, vpkt->vpkt_tgt_init_cdblen, 2421 vpkt->vpkt_tgt_init_scblen, 0, flags, func, NULL); 2422 2423 if (pkt == NULL) { 2424 VHCI_DEBUG(4, (CE_NOTE, NULL, 2425 "!bind transport: 0x%p 0x%p 0x%p\n", 2426 (void *)vhci, (void *)psd, (void *)vpkt)); 2427 if ((vpkt->vpkt_hba_pkt == NULL) && vpkt->vpkt_path) { 2428 MDI_PI_ERRSTAT(vpkt->vpkt_path, 2429 MDI_PI_TRANSERR); 2430 mdi_rele_path(vpkt->vpkt_path); 2431 vpkt->vpkt_path = NULL; 2432 } 2433 if (pgr_sema_held) { 2434 sema_v(&vlun->svl_pgr_sema); 2435 } 2436 /* 2437 * Consider it a fatal error if b_error is 2438 * set as a result of DMA binding failure 2439 * vs. a condition of being temporarily out of 2440 * some resource 2441 */ 2442 if (geterror(vpkt->vpkt_tgt_init_bp)) 2443 return (TRAN_FATAL_ERROR); 2444 else 2445 return (TRAN_BUSY); 2446 } 2447 } 2448 2449 pkt->pkt_private = vpkt; 2450 vpkt->vpkt_hba_pkt = pkt; 2451 return (TRAN_ACCEPT); 2452 } 2453 2454 2455 /*PRINTFLIKE3*/ 2456 void 2457 vhci_log(int level, dev_info_t *dip, const char *fmt, ...) 2458 { 2459 char buf[256]; 2460 va_list ap; 2461 2462 va_start(ap, fmt); 2463 (void) vsprintf(buf, fmt, ap); 2464 va_end(ap); 2465 2466 scsi_log(dip, "scsi_vhci", level, buf); 2467 } 2468 2469 /* do a PGR out with the information we've saved away */ 2470 static int 2471 vhci_do_prout(scsi_vhci_priv_t *svp) 2472 { 2473 2474 struct scsi_pkt *new_pkt; 2475 struct buf *bp; 2476 scsi_vhci_lun_t *vlun = svp->svp_svl; 2477 int rval, retry, nr_retry, ua_retry; 2478 uint8_t *sns, skey; 2479 2480 bp = getrbuf(KM_SLEEP); 2481 bp->b_flags = B_WRITE; 2482 bp->b_resid = 0; 2483 bp->b_un.b_addr = (caddr_t)&vlun->svl_prout; 2484 bp->b_bcount = vlun->svl_bcount; 2485 2486 VHCI_INCR_PATH_CMDCOUNT(svp); 2487 2488 new_pkt = scsi_init_pkt(&svp->svp_psd->sd_address, NULL, bp, 2489 CDB_GROUP1, sizeof (struct scsi_arq_status), 0, 0, 2490 SLEEP_FUNC, NULL); 2491 if (new_pkt == NULL) { 2492 VHCI_DECR_PATH_CMDCOUNT(svp); 2493 freerbuf(bp); 2494 cmn_err(CE_WARN, "!vhci_do_prout: scsi_init_pkt failed"); 2495 return (0); 2496 } 2497 mutex_enter(&vlun->svl_mutex); 2498 bp->b_un.b_addr = (caddr_t)&vlun->svl_prout; 2499 bp->b_bcount = vlun->svl_bcount; 2500 bcopy(vlun->svl_cdb, new_pkt->pkt_cdbp, 2501 sizeof (vlun->svl_cdb)); 2502 new_pkt->pkt_time = vlun->svl_time; 2503 mutex_exit(&vlun->svl_mutex); 2504 new_pkt->pkt_flags = FLAG_NOINTR; 2505 2506 ua_retry = nr_retry = retry = 0; 2507 again: 2508 rval = vhci_do_scsi_cmd(new_pkt); 2509 if (rval != 1) { 2510 if ((new_pkt->pkt_reason == CMD_CMPLT) && 2511 (SCBP_C(new_pkt) == STATUS_CHECK) && 2512 (new_pkt->pkt_state & STATE_ARQ_DONE)) { 2513 sns = (uint8_t *) 2514 &(((struct scsi_arq_status *)(uintptr_t) 2515 (new_pkt->pkt_scbp))->sts_sensedata); 2516 skey = scsi_sense_key(sns); 2517 if ((skey == KEY_UNIT_ATTENTION) || 2518 (skey == KEY_NOT_READY)) { 2519 int max_retry; 2520 struct scsi_failover_ops *fops; 2521 fops = vlun->svl_fops; 2522 rval = fops->sfo_analyze_sense(svp->svp_psd, 2523 sns, vlun->svl_fops_ctpriv); 2524 if (rval == SCSI_SENSE_NOT_READY) { 2525 max_retry = vhci_prout_not_ready_retry; 2526 retry = nr_retry++; 2527 delay(1*drv_usectohz(1000000)); 2528 } else { 2529 /* chk for state change and update */ 2530 if (rval == SCSI_SENSE_STATE_CHANGED) { 2531 int held; 2532 VHCI_HOLD_LUN(vlun, 2533 VH_NOSLEEP, held); 2534 if (!held) { 2535 rval = TRAN_BUSY; 2536 } else { 2537 /* chk for alua first */ 2538 vhci_update_pathstates( 2539 (void *)vlun); 2540 } 2541 } 2542 retry = ua_retry++; 2543 max_retry = VHCI_MAX_PGR_RETRIES; 2544 } 2545 if (retry < max_retry) { 2546 VHCI_DEBUG(4, (CE_WARN, NULL, 2547 "!vhci_do_prout retry 0x%x " 2548 "(0x%x 0x%x 0x%x)", 2549 SCBP_C(new_pkt), 2550 new_pkt->pkt_cdbp[0], 2551 new_pkt->pkt_cdbp[1], 2552 new_pkt->pkt_cdbp[2])); 2553 goto again; 2554 } 2555 rval = 0; 2556 VHCI_DEBUG(4, (CE_WARN, NULL, 2557 "!vhci_do_prout 0x%x " 2558 "(0x%x 0x%x 0x%x)", 2559 SCBP_C(new_pkt), 2560 new_pkt->pkt_cdbp[0], 2561 new_pkt->pkt_cdbp[1], 2562 new_pkt->pkt_cdbp[2])); 2563 } else if (skey == KEY_ILLEGAL_REQUEST) 2564 rval = VHCI_PGR_ILLEGALOP; 2565 } 2566 } else { 2567 rval = 1; 2568 } 2569 scsi_destroy_pkt(new_pkt); 2570 VHCI_DECR_PATH_CMDCOUNT(svp); 2571 freerbuf(bp); 2572 return (rval); 2573 } 2574 2575 static void 2576 vhci_run_cmd(void *arg) 2577 { 2578 struct scsi_pkt *pkt = (struct scsi_pkt *)arg; 2579 struct scsi_pkt *tpkt; 2580 scsi_vhci_priv_t *svp; 2581 mdi_pathinfo_t *pip, *npip; 2582 scsi_vhci_lun_t *vlun; 2583 dev_info_t *cdip; 2584 scsi_vhci_priv_t *nsvp; 2585 int fail = 0; 2586 int rval; 2587 struct vhci_pkt *vpkt; 2588 uchar_t cdb_1; 2589 vhci_prout_t *prout; 2590 2591 vpkt = (struct vhci_pkt *)pkt->pkt_private; 2592 tpkt = vpkt->vpkt_tgt_pkt; 2593 pip = vpkt->vpkt_path; 2594 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip); 2595 if (svp == NULL) { 2596 tpkt->pkt_reason = CMD_TRAN_ERR; 2597 tpkt->pkt_statistics = STAT_ABORTED; 2598 goto done; 2599 } 2600 vlun = svp->svp_svl; 2601 prout = &vlun->svl_prout; 2602 if (SCBP_C(pkt) != STATUS_GOOD) 2603 fail++; 2604 cdip = vlun->svl_dip; 2605 pip = npip = NULL; 2606 rval = mdi_select_path(cdip, NULL, 2607 MDI_SELECT_ONLINE_PATH|MDI_SELECT_STANDBY_PATH, NULL, &npip); 2608 if ((rval != MDI_SUCCESS) || (npip == NULL)) { 2609 VHCI_DEBUG(4, (CE_NOTE, NULL, 2610 "vhci_run_cmd: no path! 0x%p\n", (void *)svp)); 2611 tpkt->pkt_reason = CMD_TRAN_ERR; 2612 tpkt->pkt_statistics = STAT_ABORTED; 2613 goto done; 2614 } 2615 2616 cdb_1 = vlun->svl_cdb[1]; 2617 vlun->svl_cdb[1] &= 0xe0; 2618 vlun->svl_cdb[1] |= VHCI_PROUT_R_AND_IGNORE; 2619 2620 do { 2621 nsvp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(npip); 2622 if (nsvp == NULL) { 2623 VHCI_DEBUG(4, (CE_NOTE, NULL, 2624 "vhci_run_cmd: no " 2625 "client priv! 0x%p offlined?\n", 2626 (void *)npip)); 2627 goto next_path; 2628 } 2629 if (vlun->svl_first_path == npip) { 2630 goto next_path; 2631 } else { 2632 if (vhci_do_prout(nsvp) != 1) 2633 fail++; 2634 } 2635 next_path: 2636 pip = npip; 2637 rval = mdi_select_path(cdip, NULL, 2638 MDI_SELECT_ONLINE_PATH|MDI_SELECT_STANDBY_PATH, 2639 pip, &npip); 2640 mdi_rele_path(pip); 2641 } while ((rval == MDI_SUCCESS) && (npip != NULL)); 2642 2643 vlun->svl_cdb[1] = cdb_1; 2644 2645 if (fail) { 2646 VHCI_DEBUG(4, (CE_WARN, NULL, "%s%d: key registration failed, " 2647 "couldn't be replicated on all paths", 2648 ddi_driver_name(cdip), ddi_get_instance(cdip))); 2649 vhci_print_prout_keys(vlun, "vhci_run_cmd: "); 2650 2651 if (SCBP_C(pkt) != STATUS_GOOD) { 2652 tpkt->pkt_reason = CMD_TRAN_ERR; 2653 tpkt->pkt_statistics = STAT_ABORTED; 2654 } 2655 } else { 2656 vlun->svl_pgr_active = 1; 2657 vhci_print_prout_keys(vlun, "vhci_run_cmd: before bcopy:"); 2658 2659 bcopy((const void *)prout->service_key, 2660 (void *)prout->active_service_key, MHIOC_RESV_KEY_SIZE); 2661 bcopy((const void *)prout->res_key, 2662 (void *)prout->active_res_key, MHIOC_RESV_KEY_SIZE); 2663 2664 vhci_print_prout_keys(vlun, "vhci_run_cmd: after bcopy:"); 2665 } 2666 done: 2667 if (SCBP_C(pkt) == STATUS_GOOD) 2668 vlun->svl_first_path = NULL; 2669 2670 if (svp) 2671 VHCI_DECR_PATH_CMDCOUNT(svp); 2672 2673 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) { 2674 scsi_destroy_pkt(pkt); 2675 vpkt->vpkt_hba_pkt = NULL; 2676 if (vpkt->vpkt_path) { 2677 mdi_rele_path(vpkt->vpkt_path); 2678 vpkt->vpkt_path = NULL; 2679 } 2680 } 2681 2682 sema_v(&vlun->svl_pgr_sema); 2683 /* 2684 * The PROUT commands are not included in the automatic retry 2685 * mechanism, therefore, vpkt_org_vpkt should never be set here. 2686 */ 2687 ASSERT(vpkt->vpkt_org_vpkt == NULL); 2688 scsi_hba_pkt_comp(tpkt); 2689 } 2690 2691 /* 2692 * Get the keys registered with this target. Since we will have 2693 * registered the same key with multiple initiators, strip out 2694 * any duplicate keys. 2695 * 2696 * The pointers which will be used to filter the registered keys from 2697 * the device will be stored in filter_prin and filter_pkt. If the 2698 * allocation length of the buffer was sufficient for the number of 2699 * parameter data bytes available to be returned by the device then the 2700 * key filtering will use the keylist returned from the original 2701 * request. If the allocation length of the buffer was not sufficient, 2702 * then the filtering will use the keylist returned from the request 2703 * that is resent below. 2704 * 2705 * If the device returns an additional length field that is greater than 2706 * the allocation length of the buffer, then allocate a new buffer which 2707 * can accommodate the number of parameter data bytes available to be 2708 * returned. Resend the scsi PRIN command, filter out the duplicate 2709 * keys and return as many of the unique keys found that was originally 2710 * requested and set the additional length field equal to the data bytes 2711 * of unique reservation keys available to be returned. 2712 * 2713 * If the device returns an additional length field that is less than or 2714 * equal to the allocation length of the buffer, then all the available 2715 * keys registered were returned by the device. Filter out the 2716 * duplicate keys and return all of the unique keys found and set the 2717 * additional length field equal to the data bytes of the reservation 2718 * keys to be returned. 2719 */ 2720 2721 #define VHCI_PRIN_HEADER_SZ (sizeof (prin->length) + sizeof (prin->generation)) 2722 2723 static int 2724 vhci_do_prin(struct vhci_pkt **intr_vpkt) 2725 { 2726 scsi_vhci_priv_t *svp; 2727 struct vhci_pkt *vpkt = *intr_vpkt; 2728 vhci_prin_readkeys_t *prin; 2729 scsi_vhci_lun_t *vlun; 2730 struct scsi_vhci *vhci = ADDR2VHCI(&vpkt->vpkt_tgt_pkt->pkt_address); 2731 2732 struct buf *new_bp = NULL; 2733 struct scsi_pkt *new_pkt = NULL; 2734 struct vhci_pkt *new_vpkt = NULL; 2735 uint32_t needed_length; 2736 int rval = VHCI_CMD_CMPLT; 2737 uint32_t prin_length = 0; 2738 uint32_t svl_prin_length = 0; 2739 2740 ASSERT(vpkt->vpkt_path); 2741 svp = mdi_pi_get_vhci_private(vpkt->vpkt_path); 2742 ASSERT(svp); 2743 vlun = svp->svp_svl; 2744 ASSERT(vlun); 2745 2746 /* 2747 * If the caller only asked for an amount of data that would not 2748 * be enough to include any key data it is likely that they will 2749 * send the next command with a buffer size based on the information 2750 * from this header. Doing recovery on this would be a duplication 2751 * of efforts. 2752 */ 2753 if (vpkt->vpkt_tgt_init_bp->b_bcount <= VHCI_PRIN_HEADER_SZ) { 2754 rval = VHCI_CMD_CMPLT; 2755 goto exit; 2756 } 2757 2758 if (vpkt->vpkt_org_vpkt == NULL) { 2759 /* 2760 * Can fail as sleep is not allowed. 2761 */ 2762 prin = (vhci_prin_readkeys_t *) 2763 bp_mapin_common(vpkt->vpkt_tgt_init_bp, VM_NOSLEEP); 2764 } else { 2765 /* 2766 * The retry buf doesn't need to be mapped in. 2767 */ 2768 prin = (vhci_prin_readkeys_t *) 2769 vpkt->vpkt_tgt_init_bp->b_un.b_daddr; 2770 } 2771 2772 if (prin == NULL) { 2773 VHCI_DEBUG(5, (CE_WARN, NULL, 2774 "vhci_do_prin: bp_mapin_common failed.")); 2775 rval = VHCI_CMD_ERROR; 2776 goto fail; 2777 } 2778 2779 prin_length = BE_32(prin->length); 2780 2781 /* 2782 * According to SPC-3r22, sec 4.3.4.6: "If the amount of 2783 * information to be transferred exceeds the maximum value 2784 * that the ALLOCATION LENGTH field is capable of specifying, 2785 * the device server shall...terminate the command with CHECK 2786 * CONDITION status". The ALLOCATION LENGTH field of the 2787 * PERSISTENT RESERVE IN command is 2 bytes. We should never 2788 * get here with an ADDITIONAL LENGTH greater than 0xFFFF 2789 * so if we do, then it is an error! 2790 */ 2791 2792 2793 if ((prin_length + VHCI_PRIN_HEADER_SZ) > 0xFFFF) { 2794 VHCI_DEBUG(5, (CE_NOTE, NULL, 2795 "vhci_do_prin: Device returned invalid " 2796 "length 0x%x\n", prin_length)); 2797 rval = VHCI_CMD_ERROR; 2798 goto fail; 2799 } 2800 needed_length = prin_length + VHCI_PRIN_HEADER_SZ; 2801 2802 /* 2803 * If prin->length is greater than the byte count allocated in the 2804 * original buffer, then resend the request with enough buffer 2805 * allocated to get all of the available registered keys. 2806 */ 2807 if ((vpkt->vpkt_tgt_init_bp->b_bcount < needed_length) && 2808 (vpkt->vpkt_org_vpkt == NULL)) { 2809 2810 new_pkt = vhci_create_retry_pkt(vpkt); 2811 if (new_pkt == NULL) { 2812 rval = VHCI_CMD_ERROR; 2813 goto fail; 2814 } 2815 new_vpkt = TGTPKT2VHCIPKT(new_pkt); 2816 2817 /* 2818 * This is the buf with buffer pointer 2819 * where the prin readkeys will be 2820 * returned from the device 2821 */ 2822 new_bp = scsi_alloc_consistent_buf(&svp->svp_psd->sd_address, 2823 NULL, needed_length, B_READ, NULL_FUNC, NULL); 2824 if ((new_bp == NULL) || (new_bp->b_un.b_addr == NULL)) { 2825 if (new_bp) { 2826 scsi_free_consistent_buf(new_bp); 2827 } 2828 vhci_scsi_destroy_pkt(&new_pkt->pkt_address, new_pkt); 2829 rval = VHCI_CMD_ERROR; 2830 goto fail; 2831 } 2832 new_bp->b_bcount = needed_length; 2833 new_pkt->pkt_cdbp[7] = (uchar_t)(needed_length >> 8); 2834 new_pkt->pkt_cdbp[8] = (uchar_t)needed_length; 2835 2836 rval = VHCI_CMD_RETRY; 2837 2838 new_vpkt->vpkt_tgt_init_bp = new_bp; 2839 } 2840 2841 if (rval == VHCI_CMD_RETRY) { 2842 2843 /* 2844 * There were more keys then the original request asked for. 2845 */ 2846 mdi_pathinfo_t *path_holder = vpkt->vpkt_path; 2847 2848 /* 2849 * Release the old path because it does not matter which path 2850 * this command is sent down. This allows the normal bind 2851 * transport mechanism to be used. 2852 */ 2853 if (vpkt->vpkt_path != NULL) { 2854 mdi_rele_path(vpkt->vpkt_path); 2855 vpkt->vpkt_path = NULL; 2856 } 2857 2858 /* 2859 * Dispatch the retry command 2860 */ 2861 if (taskq_dispatch(vhci->vhci_taskq, vhci_dispatch_scsi_start, 2862 (void *) new_vpkt, KM_NOSLEEP) == NULL) { 2863 if (path_holder) { 2864 vpkt->vpkt_path = path_holder; 2865 mdi_hold_path(path_holder); 2866 } 2867 scsi_free_consistent_buf(new_bp); 2868 vhci_scsi_destroy_pkt(&new_pkt->pkt_address, new_pkt); 2869 rval = VHCI_CMD_ERROR; 2870 goto fail; 2871 } 2872 2873 /* 2874 * If we return VHCI_CMD_RETRY, that means the caller 2875 * is going to bail and wait for the reissued command 2876 * to complete. In that case, we need to decrement 2877 * the path command count right now. In any other 2878 * case, it'll be decremented by the caller. 2879 */ 2880 VHCI_DECR_PATH_CMDCOUNT(svp); 2881 goto exit; 2882 2883 } 2884 2885 if (rval == VHCI_CMD_CMPLT) { 2886 /* 2887 * The original request got all of the keys or the recovery 2888 * packet returns. 2889 */ 2890 int new; 2891 int old; 2892 int num_keys = prin_length / MHIOC_RESV_KEY_SIZE; 2893 2894 VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_do_prin: %d keys read\n", 2895 num_keys)); 2896 2897 #ifdef DEBUG 2898 VHCI_DEBUG(5, (CE_NOTE, NULL, "vhci_do_prin: from storage\n")); 2899 if (vhci_debug == 5) 2900 vhci_print_prin_keys(prin, num_keys); 2901 VHCI_DEBUG(5, (CE_NOTE, NULL, 2902 "vhci_do_prin: MPxIO old keys:\n")); 2903 if (vhci_debug == 5) 2904 vhci_print_prin_keys(&vlun->svl_prin, num_keys); 2905 #endif 2906 2907 /* 2908 * Filter out all duplicate keys returned from the device 2909 * We know that we use a different key for every host, so we 2910 * can simply strip out duplicates. Otherwise we would need to 2911 * do more bookkeeping to figure out which keys to strip out. 2912 */ 2913 2914 new = 0; 2915 2916 /* 2917 * If we got at least 1 key copy it. 2918 */ 2919 if (num_keys > 0) { 2920 vlun->svl_prin.keylist[0] = prin->keylist[0]; 2921 new++; 2922 } 2923 2924 /* 2925 * find next unique key. 2926 */ 2927 for (old = 1; old < num_keys; old++) { 2928 int j; 2929 int match = 0; 2930 2931 if (new >= VHCI_NUM_RESV_KEYS) 2932 break; 2933 for (j = 0; j < new; j++) { 2934 if (bcmp(&prin->keylist[old], 2935 &vlun->svl_prin.keylist[j], 2936 sizeof (mhioc_resv_key_t)) == 0) { 2937 match = 1; 2938 break; 2939 } 2940 } 2941 if (!match) { 2942 vlun->svl_prin.keylist[new] = 2943 prin->keylist[old]; 2944 new++; 2945 } 2946 } 2947 2948 /* Stored Big Endian */ 2949 vlun->svl_prin.generation = prin->generation; 2950 svl_prin_length = new * sizeof (mhioc_resv_key_t); 2951 /* Stored Big Endian */ 2952 vlun->svl_prin.length = BE_32(svl_prin_length); 2953 svl_prin_length += VHCI_PRIN_HEADER_SZ; 2954 2955 /* 2956 * If we arrived at this point after issuing a retry, make sure 2957 * that we put everything back the way it originally was so 2958 * that the target driver can complete the command correctly. 2959 */ 2960 if (vpkt->vpkt_org_vpkt != NULL) { 2961 new_bp = vpkt->vpkt_tgt_init_bp; 2962 2963 scsi_free_consistent_buf(new_bp); 2964 2965 vpkt = vhci_sync_retry_pkt(vpkt); 2966 *intr_vpkt = vpkt; 2967 2968 /* 2969 * Make sure the original buffer is mapped into kernel 2970 * space before we try to copy the filtered keys into 2971 * it. 2972 */ 2973 prin = (vhci_prin_readkeys_t *)bp_mapin_common( 2974 vpkt->vpkt_tgt_init_bp, VM_NOSLEEP); 2975 } 2976 2977 /* 2978 * Now copy the desired number of prin keys into the original 2979 * target buffer. 2980 */ 2981 if (svl_prin_length <= vpkt->vpkt_tgt_init_bp->b_bcount) { 2982 /* 2983 * It is safe to return all of the available unique 2984 * keys 2985 */ 2986 bcopy(&vlun->svl_prin, prin, svl_prin_length); 2987 } else { 2988 /* 2989 * Not all of the available keys were requested by the 2990 * original command. 2991 */ 2992 bcopy(&vlun->svl_prin, prin, 2993 vpkt->vpkt_tgt_init_bp->b_bcount); 2994 } 2995 #ifdef DEBUG 2996 VHCI_DEBUG(5, (CE_NOTE, NULL, 2997 "vhci_do_prin: To Application:\n")); 2998 if (vhci_debug == 5) 2999 vhci_print_prin_keys(prin, new); 3000 VHCI_DEBUG(5, (CE_NOTE, NULL, 3001 "vhci_do_prin: MPxIO new keys:\n")); 3002 if (vhci_debug == 5) 3003 vhci_print_prin_keys(&vlun->svl_prin, new); 3004 #endif 3005 } 3006 fail: 3007 if (rval == VHCI_CMD_ERROR) { 3008 /* 3009 * If we arrived at this point after issuing a 3010 * retry, make sure that we put everything back 3011 * the way it originally was so that ssd can 3012 * complete the command correctly. 3013 */ 3014 3015 if (vpkt->vpkt_org_vpkt != NULL) { 3016 new_bp = vpkt->vpkt_tgt_init_bp; 3017 if (new_bp != NULL) { 3018 scsi_free_consistent_buf(new_bp); 3019 } 3020 3021 new_vpkt = vpkt; 3022 vpkt = vpkt->vpkt_org_vpkt; 3023 3024 vhci_scsi_destroy_pkt(&svp->svp_psd->sd_address, 3025 new_vpkt->vpkt_tgt_pkt); 3026 } 3027 3028 /* 3029 * Mark this command completion as having an error so that 3030 * ssd will retry the command. 3031 */ 3032 3033 vpkt->vpkt_tgt_pkt->pkt_reason = CMD_ABORTED; 3034 vpkt->vpkt_tgt_pkt->pkt_statistics |= STAT_ABORTED; 3035 3036 rval = VHCI_CMD_CMPLT; 3037 } 3038 exit: 3039 /* 3040 * Make sure that the semaphore is only released once. 3041 */ 3042 if (rval == VHCI_CMD_CMPLT) { 3043 sema_v(&vlun->svl_pgr_sema); 3044 } 3045 3046 return (rval); 3047 } 3048 3049 static void 3050 vhci_intr(struct scsi_pkt *pkt) 3051 { 3052 struct vhci_pkt *vpkt = (struct vhci_pkt *)pkt->pkt_private; 3053 struct scsi_pkt *tpkt; 3054 scsi_vhci_priv_t *svp; 3055 scsi_vhci_lun_t *vlun; 3056 int rval, held; 3057 struct scsi_failover_ops *fops; 3058 uint8_t *sns, skey, asc, ascq; 3059 mdi_pathinfo_t *lpath; 3060 static char *timeout_err = "Command Timeout"; 3061 static char *parity_err = "Parity Error"; 3062 char *err_str = NULL; 3063 dev_info_t *vdip, *cdip; 3064 char *cpath; 3065 3066 ASSERT(vpkt != NULL); 3067 tpkt = vpkt->vpkt_tgt_pkt; 3068 ASSERT(tpkt != NULL); 3069 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(vpkt->vpkt_path); 3070 ASSERT(svp != NULL); 3071 vlun = svp->svp_svl; 3072 ASSERT(vlun != NULL); 3073 lpath = vpkt->vpkt_path; 3074 3075 /* 3076 * sync up the target driver's pkt with the pkt that 3077 * we actually used 3078 */ 3079 *(tpkt->pkt_scbp) = *(pkt->pkt_scbp); 3080 tpkt->pkt_resid = pkt->pkt_resid; 3081 tpkt->pkt_state = pkt->pkt_state; 3082 tpkt->pkt_statistics = pkt->pkt_statistics; 3083 tpkt->pkt_reason = pkt->pkt_reason; 3084 3085 /* Return path_instance information back to the target driver. */ 3086 if (scsi_pkt_allocated_correctly(tpkt)) { 3087 if (scsi_pkt_allocated_correctly(pkt)) { 3088 /* 3089 * If both packets were correctly allocated, 3090 * return path returned by pHCI. 3091 */ 3092 tpkt->pkt_path_instance = pkt->pkt_path_instance; 3093 } else { 3094 /* Otherwise return path of pHCI we used */ 3095 tpkt->pkt_path_instance = 3096 mdi_pi_get_path_instance(lpath); 3097 } 3098 } 3099 3100 if (pkt->pkt_cdbp[0] == SCMD_PROUT && 3101 ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) || 3102 ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_R_AND_IGNORE)) { 3103 if ((SCBP_C(pkt) != STATUS_GOOD) || 3104 (pkt->pkt_reason != CMD_CMPLT)) { 3105 sema_v(&vlun->svl_pgr_sema); 3106 } 3107 } else if (pkt->pkt_cdbp[0] == SCMD_PRIN) { 3108 if (pkt->pkt_reason != CMD_CMPLT || 3109 (SCBP_C(pkt) != STATUS_GOOD)) { 3110 sema_v(&vlun->svl_pgr_sema); 3111 } 3112 } 3113 3114 switch (pkt->pkt_reason) { 3115 case CMD_CMPLT: 3116 /* 3117 * cmd completed successfully, check for scsi errors 3118 */ 3119 switch (*(pkt->pkt_scbp)) { 3120 case STATUS_CHECK: 3121 if (pkt->pkt_state & STATE_ARQ_DONE) { 3122 sns = (uint8_t *) 3123 &(((struct scsi_arq_status *)(uintptr_t) 3124 (pkt->pkt_scbp))->sts_sensedata); 3125 skey = scsi_sense_key(sns); 3126 asc = scsi_sense_asc(sns); 3127 ascq = scsi_sense_ascq(sns); 3128 fops = vlun->svl_fops; 3129 ASSERT(fops != NULL); 3130 VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_intr: " 3131 "Received sns key %x esc %x escq %x\n", 3132 skey, asc, ascq)); 3133 3134 if (vlun->svl_waiting_for_activepath == 1) { 3135 /* 3136 * if we are here it means we are 3137 * in the midst of a probe/attach 3138 * through a passive path; this 3139 * case is exempt from sense analysis 3140 * for detection of ext. failover 3141 * because that would unnecessarily 3142 * increase attach time. 3143 */ 3144 bcopy(pkt->pkt_scbp, tpkt->pkt_scbp, 3145 vpkt->vpkt_tgt_init_scblen); 3146 break; 3147 } 3148 if (asc == VHCI_SCSI_PERR) { 3149 /* 3150 * parity error 3151 */ 3152 err_str = parity_err; 3153 bcopy(pkt->pkt_scbp, tpkt->pkt_scbp, 3154 vpkt->vpkt_tgt_init_scblen); 3155 break; 3156 } 3157 rval = fops->sfo_analyze_sense(svp->svp_psd, 3158 sns, vlun->svl_fops_ctpriv); 3159 if ((rval == SCSI_SENSE_NOFAILOVER) || 3160 (rval == SCSI_SENSE_UNKNOWN) || 3161 (rval == SCSI_SENSE_NOT_READY)) { 3162 bcopy(pkt->pkt_scbp, tpkt->pkt_scbp, 3163 vpkt->vpkt_tgt_init_scblen); 3164 break; 3165 } else if (rval == SCSI_SENSE_STATE_CHANGED) { 3166 struct scsi_vhci *vhci; 3167 vhci = ADDR2VHCI(&tpkt->pkt_address); 3168 VHCI_HOLD_LUN(vlun, VH_NOSLEEP, held); 3169 if (!held) { 3170 /* 3171 * looks like some other thread 3172 * has already detected this 3173 * condition 3174 */ 3175 tpkt->pkt_state &= 3176 ~STATE_ARQ_DONE; 3177 *(tpkt->pkt_scbp) = 3178 STATUS_BUSY; 3179 break; 3180 } 3181 (void) taskq_dispatch( 3182 vhci->vhci_update_pathstates_taskq, 3183 vhci_update_pathstates, 3184 (void *)vlun, KM_SLEEP); 3185 } else { 3186 /* 3187 * externally initiated failover 3188 * has occurred or is in progress 3189 */ 3190 VHCI_HOLD_LUN(vlun, VH_NOSLEEP, held); 3191 if (!held) { 3192 /* 3193 * looks like some other thread 3194 * has already detected this 3195 * condition 3196 */ 3197 tpkt->pkt_state &= 3198 ~STATE_ARQ_DONE; 3199 *(tpkt->pkt_scbp) = 3200 STATUS_BUSY; 3201 break; 3202 } else { 3203 rval = vhci_handle_ext_fo 3204 (pkt, rval); 3205 if (rval == BUSY_RETURN) { 3206 tpkt->pkt_state &= 3207 ~STATE_ARQ_DONE; 3208 *(tpkt->pkt_scbp) = 3209 STATUS_BUSY; 3210 break; 3211 } 3212 bcopy(pkt->pkt_scbp, 3213 tpkt->pkt_scbp, 3214 vpkt->vpkt_tgt_init_scblen); 3215 break; 3216 } 3217 } 3218 } 3219 break; 3220 3221 /* 3222 * If this is a good SCSI-II RELEASE cmd completion then restore 3223 * the load balancing policy and reset VLUN_RESERVE_ACTIVE_FLG. 3224 * If this is a good SCSI-II RESERVE cmd completion then set 3225 * VLUN_RESERVE_ACTIVE_FLG. 3226 */ 3227 case STATUS_GOOD: 3228 if ((pkt->pkt_cdbp[0] == SCMD_RELEASE) || 3229 (pkt->pkt_cdbp[0] == SCMD_RELEASE_G1)) { 3230 (void) mdi_set_lb_policy(vlun->svl_dip, 3231 vlun->svl_lb_policy_save); 3232 vlun->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG; 3233 VHCI_DEBUG(1, (CE_WARN, NULL, 3234 "!vhci_intr: vlun 0x%p release path 0x%p", 3235 (void *)vlun, (void *)vpkt->vpkt_path)); 3236 } 3237 3238 if ((pkt->pkt_cdbp[0] == SCMD_RESERVE) || 3239 (pkt->pkt_cdbp[0] == SCMD_RESERVE_G1)) { 3240 vlun->svl_flags |= VLUN_RESERVE_ACTIVE_FLG; 3241 vlun->svl_resrv_pip = vpkt->vpkt_path; 3242 VHCI_DEBUG(1, (CE_WARN, NULL, 3243 "!vhci_intr: vlun 0x%p reserved path 0x%p", 3244 (void *)vlun, (void *)vpkt->vpkt_path)); 3245 } 3246 break; 3247 3248 case STATUS_RESERVATION_CONFLICT: 3249 VHCI_DEBUG(1, (CE_WARN, NULL, 3250 "!vhci_intr: vlun 0x%p " 3251 "reserve conflict on path 0x%p", 3252 (void *)vlun, (void *)vpkt->vpkt_path)); 3253 /* FALLTHROUGH */ 3254 default: 3255 break; 3256 } 3257 3258 /* 3259 * Update I/O completion statistics for the path 3260 */ 3261 mdi_pi_kstat_iosupdate(vpkt->vpkt_path, vpkt->vpkt_tgt_init_bp); 3262 3263 /* 3264 * Command completed successfully, release the dma binding and 3265 * destroy the transport side of the packet. 3266 */ 3267 if ((pkt->pkt_cdbp[0] == SCMD_PROUT) && 3268 (((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) || 3269 ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_R_AND_IGNORE))) { 3270 if (SCBP_C(pkt) == STATUS_GOOD) { 3271 ASSERT(vlun->svl_taskq); 3272 svp->svp_last_pkt_reason = pkt->pkt_reason; 3273 (void) taskq_dispatch(vlun->svl_taskq, 3274 vhci_run_cmd, pkt, KM_SLEEP); 3275 return; 3276 } 3277 } 3278 if ((SCBP_C(pkt) == STATUS_GOOD) && 3279 (pkt->pkt_cdbp[0] == SCMD_PRIN) && vpkt->vpkt_tgt_init_bp) { 3280 /* 3281 * If the action (value in byte 1 of the cdb) is zero, 3282 * we're reading keys, and that's the only condition 3283 * where we need to be concerned with filtering keys 3284 * and potential retries. Otherwise, we simply signal 3285 * the semaphore and move on. 3286 */ 3287 if (pkt->pkt_cdbp[1] == 0) { 3288 /* 3289 * If this is the completion of an internal 3290 * retry then we need to make sure that the 3291 * pkt and tpkt pointers are readjusted so 3292 * the calls to scsi_destroy_pkt and pkt_comp 3293 * below work * correctly. 3294 */ 3295 if (vpkt->vpkt_org_vpkt != NULL) { 3296 pkt = vpkt->vpkt_org_vpkt->vpkt_hba_pkt; 3297 tpkt = vpkt->vpkt_org_vpkt-> 3298 vpkt_tgt_pkt; 3299 3300 /* 3301 * If this command was issued through 3302 * the taskq then we need to clear 3303 * this flag for proper processing in 3304 * the case of a retry from the target 3305 * driver. 3306 */ 3307 vpkt->vpkt_state &= 3308 ~VHCI_PKT_THRU_TASKQ; 3309 } 3310 3311 /* 3312 * if vhci_do_prin returns VHCI_CMD_CMPLT then 3313 * vpkt will contain the address of the 3314 * original vpkt 3315 */ 3316 if (vhci_do_prin(&vpkt) == VHCI_CMD_RETRY) { 3317 /* 3318 * The command has been resent to get 3319 * all the keys from the device. Don't 3320 * complete the command with ssd until 3321 * the retry completes. 3322 */ 3323 return; 3324 } 3325 } else { 3326 sema_v(&vlun->svl_pgr_sema); 3327 } 3328 } 3329 3330 break; 3331 3332 case CMD_TIMEOUT: 3333 if ((pkt->pkt_statistics & 3334 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 3335 3336 VHCI_DEBUG(1, (CE_NOTE, NULL, 3337 "!scsi vhci timeout invoked\n")); 3338 3339 (void) vhci_recovery_reset(vlun, &pkt->pkt_address, 3340 FALSE, VHCI_DEPTH_ALL); 3341 } 3342 MDI_PI_ERRSTAT(lpath, MDI_PI_TRANSERR); 3343 tpkt->pkt_statistics |= STAT_ABORTED; 3344 err_str = timeout_err; 3345 break; 3346 3347 case CMD_TRAN_ERR: 3348 /* 3349 * This status is returned if the transport has sent the cmd 3350 * down the link to the target and then some error occurs. 3351 * In case of SCSI-II RESERVE cmd, we don't know if the 3352 * reservation been accepted by the target or not, so we need 3353 * to clear the reservation. 3354 */ 3355 if ((pkt->pkt_cdbp[0] == SCMD_RESERVE) || 3356 (pkt->pkt_cdbp[0] == SCMD_RESERVE_G1)) { 3357 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_intr received" 3358 " cmd_tran_err for scsi-2 reserve cmd\n")); 3359 if (!vhci_recovery_reset(vlun, &pkt->pkt_address, 3360 TRUE, VHCI_DEPTH_TARGET)) { 3361 VHCI_DEBUG(1, (CE_WARN, NULL, 3362 "!vhci_intr cmd_tran_err reset failed!")); 3363 } 3364 } 3365 break; 3366 3367 case CMD_DEV_GONE: 3368 /* 3369 * If this is the last path then report CMD_DEV_GONE to the 3370 * target driver, otherwise report BUSY to triggger retry. 3371 */ 3372 if (vlun->svl_dip && 3373 (mdi_client_get_path_count(vlun->svl_dip) <= 1)) { 3374 struct scsi_vhci *vhci; 3375 vhci = ADDR2VHCI(&tpkt->pkt_address); 3376 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_intr received " 3377 "cmd_dev_gone on last path\n")); 3378 (void) vhci_invalidate_mpapi_lu(vhci, vlun); 3379 break; 3380 } 3381 3382 /* Report CMD_CMPLT-with-BUSY to cause retry. */ 3383 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_intr received " 3384 "cmd_dev_gone\n")); 3385 tpkt->pkt_reason = CMD_CMPLT; 3386 tpkt->pkt_state = STATE_GOT_BUS | 3387 STATE_GOT_TARGET | STATE_SENT_CMD | 3388 STATE_GOT_STATUS; 3389 *(tpkt->pkt_scbp) = STATUS_BUSY; 3390 break; 3391 3392 default: 3393 break; 3394 } 3395 3396 /* 3397 * SCSI-II RESERVE cmd has been serviced by the lower layers clear 3398 * the flag so the lun is not QUIESCED any longer. 3399 * Also clear the VHCI_PKT_THRU_TASKQ flag, to ensure that if this pkt 3400 * is retried, a taskq shall again be dispatched to service it. Else 3401 * it may lead to a system hang if the retry is within interrupt 3402 * context. 3403 */ 3404 if ((pkt->pkt_cdbp[0] == SCMD_RESERVE) || 3405 (pkt->pkt_cdbp[0] == SCMD_RESERVE_G1)) { 3406 vlun->svl_flags &= ~VLUN_QUIESCED_FLG; 3407 vpkt->vpkt_state &= ~VHCI_PKT_THRU_TASKQ; 3408 } 3409 3410 /* 3411 * vpkt_org_vpkt should always be NULL here if the retry command 3412 * has been successfully processed. If vpkt_org_vpkt != NULL at 3413 * this point, it is an error so restore the original vpkt and 3414 * return an error to the target driver so it can retry the 3415 * command as appropriate. 3416 */ 3417 if (vpkt->vpkt_org_vpkt != NULL) { 3418 struct vhci_pkt *new_vpkt = vpkt; 3419 vpkt = vpkt->vpkt_org_vpkt; 3420 3421 vhci_scsi_destroy_pkt(&svp->svp_psd->sd_address, 3422 new_vpkt->vpkt_tgt_pkt); 3423 3424 /* 3425 * Mark this command completion as having an error so that 3426 * ssd will retry the command. 3427 */ 3428 vpkt->vpkt_tgt_pkt->pkt_reason = CMD_ABORTED; 3429 vpkt->vpkt_tgt_pkt->pkt_statistics |= STAT_ABORTED; 3430 3431 pkt = vpkt->vpkt_hba_pkt; 3432 tpkt = vpkt->vpkt_tgt_pkt; 3433 } 3434 3435 if ((err_str != NULL) && (pkt->pkt_reason != 3436 svp->svp_last_pkt_reason)) { 3437 cdip = vlun->svl_dip; 3438 vdip = ddi_get_parent(cdip); 3439 cpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3440 vhci_log(CE_WARN, vdip, "!%s (%s%d): %s on path %s", 3441 ddi_pathname(cdip, cpath), ddi_driver_name(cdip), 3442 ddi_get_instance(cdip), err_str, 3443 mdi_pi_spathname(vpkt->vpkt_path)); 3444 kmem_free(cpath, MAXPATHLEN); 3445 } 3446 svp->svp_last_pkt_reason = pkt->pkt_reason; 3447 VHCI_DECR_PATH_CMDCOUNT(svp); 3448 3449 /* 3450 * For PARTIAL_DMA, vhci should not free the path. 3451 * Target driver will call into vhci_scsi_dmafree or 3452 * destroy pkt to release this path. 3453 */ 3454 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) { 3455 scsi_destroy_pkt(pkt); 3456 vpkt->vpkt_hba_pkt = NULL; 3457 if (vpkt->vpkt_path) { 3458 mdi_rele_path(vpkt->vpkt_path); 3459 vpkt->vpkt_path = NULL; 3460 } 3461 } 3462 3463 scsi_hba_pkt_comp(tpkt); 3464 } 3465 3466 /* 3467 * two possibilities: (1) failover has completed 3468 * or (2) is in progress; update our path states for 3469 * the former case; for the latter case, 3470 * initiate a scsi_watch request to 3471 * determine when failover completes - vlun is HELD 3472 * until failover completes; BUSY is returned to upper 3473 * layer in both the cases 3474 */ 3475 static int 3476 vhci_handle_ext_fo(struct scsi_pkt *pkt, int fostat) 3477 { 3478 struct vhci_pkt *vpkt = (struct vhci_pkt *)pkt->pkt_private; 3479 struct scsi_pkt *tpkt; 3480 scsi_vhci_priv_t *svp; 3481 scsi_vhci_lun_t *vlun; 3482 struct scsi_vhci *vhci; 3483 scsi_vhci_swarg_t *swarg; 3484 char *path; 3485 3486 ASSERT(vpkt != NULL); 3487 tpkt = vpkt->vpkt_tgt_pkt; 3488 ASSERT(tpkt != NULL); 3489 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(vpkt->vpkt_path); 3490 ASSERT(svp != NULL); 3491 vlun = svp->svp_svl; 3492 ASSERT(vlun != NULL); 3493 ASSERT(VHCI_LUN_IS_HELD(vlun)); 3494 3495 vhci = ADDR2VHCI(&tpkt->pkt_address); 3496 3497 if (fostat == SCSI_SENSE_INACTIVE) { 3498 VHCI_DEBUG(1, (CE_NOTE, NULL, "!Failover " 3499 "detected for %s; updating path states...\n", 3500 vlun->svl_lun_wwn)); 3501 /* 3502 * set the vlun flag to indicate to the task that the target 3503 * port group needs updating 3504 */ 3505 vlun->svl_flags |= VLUN_UPDATE_TPG; 3506 (void) taskq_dispatch(vhci->vhci_update_pathstates_taskq, 3507 vhci_update_pathstates, (void *)vlun, KM_SLEEP); 3508 } else { 3509 path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3510 vhci_log(CE_NOTE, ddi_get_parent(vlun->svl_dip), 3511 "!%s (%s%d): Waiting for externally initiated failover " 3512 "to complete", ddi_pathname(vlun->svl_dip, path), 3513 ddi_driver_name(vlun->svl_dip), 3514 ddi_get_instance(vlun->svl_dip)); 3515 kmem_free(path, MAXPATHLEN); 3516 swarg = kmem_alloc(sizeof (*swarg), KM_NOSLEEP); 3517 if (swarg == NULL) { 3518 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_handle_ext_fo: " 3519 "request packet allocation for %s failed....\n", 3520 vlun->svl_lun_wwn)); 3521 VHCI_RELEASE_LUN(vlun); 3522 return (PKT_RETURN); 3523 } 3524 swarg->svs_svp = svp; 3525 swarg->svs_tos = ddi_get_time(); 3526 swarg->svs_pi = vpkt->vpkt_path; 3527 swarg->svs_release_lun = 0; 3528 swarg->svs_done = 0; 3529 /* 3530 * place a hold on the path...we don't want it to 3531 * vanish while scsi_watch is in progress 3532 */ 3533 mdi_hold_path(vpkt->vpkt_path); 3534 svp->svp_sw_token = scsi_watch_request_submit(svp->svp_psd, 3535 VHCI_FOWATCH_INTERVAL, SENSE_LENGTH, vhci_efo_watch_cb, 3536 (caddr_t)swarg); 3537 } 3538 return (BUSY_RETURN); 3539 } 3540 3541 /* 3542 * vhci_efo_watch_cb: 3543 * Callback from scsi_watch request to check the failover status. 3544 * Completion is either due to successful failover or timeout. 3545 * Upon successful completion, vhci_update_path_states is called. 3546 * For timeout condition, vhci_efo_done is called. 3547 * Always returns 0 to scsi_watch to keep retrying till vhci_efo_done 3548 * terminates this request properly in a separate thread. 3549 */ 3550 3551 static int 3552 vhci_efo_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 3553 { 3554 struct scsi_status *statusp = resultp->statusp; 3555 uint8_t *sensep = (uint8_t *)resultp->sensep; 3556 struct scsi_pkt *pkt = resultp->pkt; 3557 scsi_vhci_swarg_t *swarg; 3558 scsi_vhci_priv_t *svp; 3559 scsi_vhci_lun_t *vlun; 3560 struct scsi_vhci *vhci; 3561 dev_info_t *vdip; 3562 int rval, updt_paths; 3563 3564 swarg = (scsi_vhci_swarg_t *)(uintptr_t)arg; 3565 svp = swarg->svs_svp; 3566 if (swarg->svs_done) { 3567 /* 3568 * Already completed failover or timedout. 3569 * Waiting for vhci_efo_done to terminate this scsi_watch. 3570 */ 3571 return (0); 3572 } 3573 3574 ASSERT(svp != NULL); 3575 vlun = svp->svp_svl; 3576 ASSERT(vlun != NULL); 3577 ASSERT(VHCI_LUN_IS_HELD(vlun)); 3578 vlun->svl_efo_update_path = 0; 3579 vdip = ddi_get_parent(vlun->svl_dip); 3580 vhci = ddi_get_soft_state(vhci_softstate, 3581 ddi_get_instance(vdip)); 3582 3583 updt_paths = 0; 3584 3585 if (pkt->pkt_reason != CMD_CMPLT) { 3586 if ((ddi_get_time() - swarg->svs_tos) >= VHCI_EXTFO_TIMEOUT) { 3587 swarg->svs_release_lun = 1; 3588 goto done; 3589 } 3590 return (0); 3591 } 3592 if (*((unsigned char *)statusp) == STATUS_CHECK) { 3593 rval = vlun->svl_fops->sfo_analyze_sense(svp->svp_psd, sensep, 3594 vlun->svl_fops_ctpriv); 3595 switch (rval) { 3596 /* 3597 * Only update path states in case path is definitely 3598 * inactive, or no failover occurred. For all other 3599 * check conditions continue pinging. A unexpected 3600 * check condition shouldn't cause pinging to complete 3601 * prematurely. 3602 */ 3603 case SCSI_SENSE_INACTIVE: 3604 case SCSI_SENSE_NOFAILOVER: 3605 updt_paths = 1; 3606 break; 3607 default: 3608 if ((ddi_get_time() - swarg->svs_tos) 3609 >= VHCI_EXTFO_TIMEOUT) { 3610 swarg->svs_release_lun = 1; 3611 goto done; 3612 } 3613 return (0); 3614 } 3615 } else if (*((unsigned char *)statusp) == 3616 STATUS_RESERVATION_CONFLICT) { 3617 updt_paths = 1; 3618 } else if ((*((unsigned char *)statusp)) & 3619 (STATUS_BUSY | STATUS_QFULL)) { 3620 return (0); 3621 } 3622 if ((*((unsigned char *)statusp) == STATUS_GOOD) || 3623 (updt_paths == 1)) { 3624 /* 3625 * we got here because we had detected an 3626 * externally initiated failover; things 3627 * have settled down now, so let's 3628 * start up a task to update the 3629 * path states and target port group 3630 */ 3631 vlun->svl_efo_update_path = 1; 3632 swarg->svs_done = 1; 3633 vlun->svl_swarg = swarg; 3634 vlun->svl_flags |= VLUN_UPDATE_TPG; 3635 (void) taskq_dispatch(vhci->vhci_update_pathstates_taskq, 3636 vhci_update_pathstates, (void *)vlun, 3637 KM_SLEEP); 3638 return (0); 3639 } 3640 if ((ddi_get_time() - swarg->svs_tos) >= VHCI_EXTFO_TIMEOUT) { 3641 swarg->svs_release_lun = 1; 3642 goto done; 3643 } 3644 return (0); 3645 done: 3646 swarg->svs_done = 1; 3647 (void) taskq_dispatch(vhci->vhci_taskq, 3648 vhci_efo_done, (void *)swarg, KM_SLEEP); 3649 return (0); 3650 } 3651 3652 /* 3653 * vhci_efo_done: 3654 * cleanly terminates scsi_watch and free up resources. 3655 * Called as taskq function in vhci_efo_watch_cb for EFO timeout condition 3656 * or by vhci_update_path_states invoked during external initiated 3657 * failover completion. 3658 */ 3659 static void 3660 vhci_efo_done(void *arg) 3661 { 3662 scsi_vhci_lun_t *vlun; 3663 scsi_vhci_swarg_t *swarg = (scsi_vhci_swarg_t *)arg; 3664 scsi_vhci_priv_t *svp = swarg->svs_svp; 3665 ASSERT(svp); 3666 3667 vlun = svp->svp_svl; 3668 ASSERT(vlun); 3669 3670 /* Wait for clean termination of scsi_watch */ 3671 (void) scsi_watch_request_terminate(svp->svp_sw_token, 3672 SCSI_WATCH_TERMINATE_ALL_WAIT); 3673 svp->svp_sw_token = NULL; 3674 3675 /* release path and freeup resources to indicate failover completion */ 3676 mdi_rele_path(swarg->svs_pi); 3677 if (swarg->svs_release_lun) { 3678 VHCI_RELEASE_LUN(vlun); 3679 } 3680 kmem_free((void *)swarg, sizeof (*swarg)); 3681 } 3682 3683 /* 3684 * Update the path states 3685 * vlun should be HELD when this is invoked. 3686 * Calls vhci_efo_done to cleanup resources allocated for EFO. 3687 */ 3688 void 3689 vhci_update_pathstates(void *arg) 3690 { 3691 mdi_pathinfo_t *pip, *npip; 3692 dev_info_t *dip; 3693 struct scsi_failover_ops *fo; 3694 struct scsi_vhci_priv *svp; 3695 struct scsi_device *psd; 3696 struct scsi_path_opinfo opinfo; 3697 char *pclass, *tptr; 3698 struct scsi_vhci_lun *vlun = (struct scsi_vhci_lun *)arg; 3699 int sps; /* mdi_select_path() status */ 3700 char *cpath; 3701 struct scsi_vhci *vhci; 3702 struct scsi_pkt *pkt; 3703 struct buf *bp; 3704 int reserve_conflict = 0; 3705 3706 ASSERT(VHCI_LUN_IS_HELD(vlun)); 3707 dip = vlun->svl_dip; 3708 pip = npip = NULL; 3709 3710 vhci = ddi_get_soft_state(vhci_softstate, 3711 ddi_get_instance(ddi_get_parent(dip))); 3712 3713 sps = mdi_select_path(dip, NULL, (MDI_SELECT_ONLINE_PATH | 3714 MDI_SELECT_STANDBY_PATH | MDI_SELECT_NO_PREFERRED), NULL, &npip); 3715 if ((npip == NULL) || (sps != MDI_SUCCESS)) { 3716 goto done; 3717 } 3718 3719 fo = vlun->svl_fops; 3720 do { 3721 pip = npip; 3722 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip); 3723 psd = svp->svp_psd; 3724 if (fo->sfo_path_get_opinfo(psd, &opinfo, 3725 vlun->svl_fops_ctpriv) != 0) { 3726 sps = mdi_select_path(dip, NULL, 3727 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH | 3728 MDI_SELECT_NO_PREFERRED), pip, &npip); 3729 mdi_rele_path(pip); 3730 continue; 3731 } 3732 3733 if (mdi_prop_lookup_string(pip, "path-class", &pclass) != 3734 MDI_SUCCESS) { 3735 VHCI_DEBUG(1, (CE_NOTE, NULL, 3736 "!vhci_update_pathstates: prop lookup failed for " 3737 "path 0x%p\n", (void *)pip)); 3738 sps = mdi_select_path(dip, NULL, 3739 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH | 3740 MDI_SELECT_NO_PREFERRED), pip, &npip); 3741 mdi_rele_path(pip); 3742 continue; 3743 } 3744 3745 /* 3746 * Need to update the "path-class" property 3747 * value in the device tree if different 3748 * from the existing value. 3749 */ 3750 if (strcmp(pclass, opinfo.opinfo_path_attr) != 0) { 3751 (void) mdi_prop_update_string(pip, "path-class", 3752 opinfo.opinfo_path_attr); 3753 } 3754 3755 /* 3756 * Only change the state if needed. i.e. Don't call 3757 * mdi_pi_set_state to ONLINE a path if its already 3758 * ONLINE. Same for STANDBY paths. 3759 */ 3760 3761 if ((opinfo.opinfo_path_state == SCSI_PATH_ACTIVE || 3762 opinfo.opinfo_path_state == SCSI_PATH_ACTIVE_NONOPT)) { 3763 if (!(MDI_PI_IS_ONLINE(pip))) { 3764 VHCI_DEBUG(1, (CE_NOTE, NULL, 3765 "!vhci_update_pathstates: marking path" 3766 " 0x%p as ONLINE\n", (void *)pip)); 3767 cpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3768 vhci_log(CE_NOTE, ddi_get_parent(dip), "!%s " 3769 "(%s%d): path %s " 3770 "is now ONLINE because of " 3771 "an externally initiated failover", 3772 ddi_pathname(dip, cpath), 3773 ddi_driver_name(dip), 3774 ddi_get_instance(dip), 3775 mdi_pi_spathname(pip)); 3776 kmem_free(cpath, MAXPATHLEN); 3777 mdi_pi_set_state(pip, 3778 MDI_PATHINFO_STATE_ONLINE); 3779 mdi_pi_set_preferred(pip, 3780 opinfo.opinfo_preferred); 3781 tptr = kmem_alloc(strlen 3782 (opinfo.opinfo_path_attr)+1, KM_SLEEP); 3783 (void) strlcpy(tptr, opinfo.opinfo_path_attr, 3784 (strlen(opinfo.opinfo_path_attr)+1)); 3785 mutex_enter(&vlun->svl_mutex); 3786 if (vlun->svl_active_pclass != NULL) { 3787 kmem_free(vlun->svl_active_pclass, 3788 strlen(vlun->svl_active_pclass)+1); 3789 } 3790 vlun->svl_active_pclass = tptr; 3791 if (vlun->svl_waiting_for_activepath) { 3792 vlun->svl_waiting_for_activepath = 0; 3793 } 3794 mutex_exit(&vlun->svl_mutex); 3795 /* Check for Reservation Conflict */ 3796 bp = scsi_alloc_consistent_buf( 3797 &svp->svp_psd->sd_address, 3798 (struct buf *)NULL, DEV_BSIZE, B_READ, 3799 NULL, NULL); 3800 if (!bp) { 3801 VHCI_DEBUG(1, (CE_NOTE, NULL, 3802 "vhci_update_pathstates: " 3803 "!No resources (buf)\n")); 3804 mdi_rele_path(pip); 3805 goto done; 3806 } 3807 pkt = scsi_init_pkt(&svp->svp_psd->sd_address, 3808 NULL, bp, CDB_GROUP1, 3809 sizeof (struct scsi_arq_status), 0, 3810 PKT_CONSISTENT, NULL, NULL); 3811 if (pkt) { 3812 (void) scsi_setup_cdb((union scsi_cdb *) 3813 (uintptr_t)pkt->pkt_cdbp, 3814 SCMD_READ, 1, 1, 0); 3815 pkt->pkt_time = 3*30; 3816 pkt->pkt_flags = FLAG_NOINTR; 3817 pkt->pkt_path_instance = 3818 mdi_pi_get_path_instance(pip); 3819 3820 if ((scsi_transport(pkt) == 3821 TRAN_ACCEPT) && (pkt->pkt_reason 3822 == CMD_CMPLT) && (SCBP_C(pkt) == 3823 STATUS_RESERVATION_CONFLICT)) { 3824 reserve_conflict = 1; 3825 } 3826 scsi_destroy_pkt(pkt); 3827 } 3828 scsi_free_consistent_buf(bp); 3829 } else if (MDI_PI_IS_ONLINE(pip)) { 3830 if (strcmp(pclass, opinfo.opinfo_path_attr) 3831 != 0) { 3832 mdi_pi_set_preferred(pip, 3833 opinfo.opinfo_preferred); 3834 mutex_enter(&vlun->svl_mutex); 3835 if (vlun->svl_active_pclass == NULL || 3836 strcmp(opinfo.opinfo_path_attr, 3837 vlun->svl_active_pclass) != 0) { 3838 mutex_exit(&vlun->svl_mutex); 3839 tptr = kmem_alloc(strlen 3840 (opinfo.opinfo_path_attr)+1, 3841 KM_SLEEP); 3842 (void) strlcpy(tptr, 3843 opinfo.opinfo_path_attr, 3844 (strlen 3845 (opinfo.opinfo_path_attr) 3846 +1)); 3847 mutex_enter(&vlun->svl_mutex); 3848 } else { 3849 /* 3850 * No need to update 3851 * svl_active_pclass 3852 */ 3853 tptr = NULL; 3854 mutex_exit(&vlun->svl_mutex); 3855 } 3856 if (tptr) { 3857 if (vlun->svl_active_pclass 3858 != NULL) { 3859 kmem_free(vlun-> 3860 svl_active_pclass, 3861 strlen(vlun-> 3862 svl_active_pclass) 3863 +1); 3864 } 3865 vlun->svl_active_pclass = tptr; 3866 mutex_exit(&vlun->svl_mutex); 3867 } 3868 } 3869 } 3870 } else if ((opinfo.opinfo_path_state == SCSI_PATH_INACTIVE) && 3871 !(MDI_PI_IS_STANDBY(pip))) { 3872 VHCI_DEBUG(1, (CE_NOTE, NULL, 3873 "!vhci_update_pathstates: marking path" 3874 " 0x%p as STANDBY\n", (void *)pip)); 3875 cpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3876 vhci_log(CE_NOTE, ddi_get_parent(dip), "!%s " 3877 "(%s%d): path %s " 3878 "is now STANDBY because of " 3879 "an externally initiated failover", 3880 ddi_pathname(dip, cpath), 3881 ddi_driver_name(dip), 3882 ddi_get_instance(dip), 3883 mdi_pi_spathname(pip)); 3884 kmem_free(cpath, MAXPATHLEN); 3885 mdi_pi_set_state(pip, 3886 MDI_PATHINFO_STATE_STANDBY); 3887 mdi_pi_set_preferred(pip, 3888 opinfo.opinfo_preferred); 3889 mutex_enter(&vlun->svl_mutex); 3890 if (vlun->svl_active_pclass != NULL) { 3891 if (strcmp(vlun->svl_active_pclass, 3892 opinfo.opinfo_path_attr) == 0) { 3893 kmem_free(vlun-> 3894 svl_active_pclass, 3895 strlen(vlun-> 3896 svl_active_pclass)+1); 3897 vlun->svl_active_pclass = NULL; 3898 } 3899 } 3900 mutex_exit(&vlun->svl_mutex); 3901 } 3902 (void) mdi_prop_free(pclass); 3903 sps = mdi_select_path(dip, NULL, 3904 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH | 3905 MDI_SELECT_NO_PREFERRED), pip, &npip); 3906 mdi_rele_path(pip); 3907 3908 } while ((npip != NULL) && (sps == MDI_SUCCESS)); 3909 3910 /* 3911 * Check to see if this vlun has an active SCSI-II RESERVE. If so 3912 * clear the reservation by sending a reset, so the host doesn't 3913 * receive a reservation conflict. 3914 * Reset VLUN_RESERVE_ACTIVE_FLG for this vlun. Also notify ssd 3915 * of the reset, explicitly. 3916 */ 3917 if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) { 3918 if (reserve_conflict && (vlun->svl_xlf_capable == 0)) { 3919 (void) vhci_recovery_reset(vlun, 3920 &svp->svp_psd->sd_address, FALSE, 3921 VHCI_DEPTH_TARGET); 3922 } 3923 vlun->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG; 3924 mutex_enter(&vhci->vhci_mutex); 3925 scsi_hba_reset_notify_callback(&vhci->vhci_mutex, 3926 &vhci->vhci_reset_notify_listf); 3927 mutex_exit(&vhci->vhci_mutex); 3928 } 3929 if (vlun->svl_flags & VLUN_UPDATE_TPG) { 3930 /* 3931 * Update the AccessState of related MP-API TPGs 3932 */ 3933 (void) vhci_mpapi_update_tpg_acc_state_for_lu(vhci, vlun); 3934 vlun->svl_flags &= ~VLUN_UPDATE_TPG; 3935 } 3936 done: 3937 if (vlun->svl_efo_update_path) { 3938 vlun->svl_efo_update_path = 0; 3939 vhci_efo_done(vlun->svl_swarg); 3940 vlun->svl_swarg = 0; 3941 } 3942 VHCI_RELEASE_LUN(vlun); 3943 } 3944 3945 /* ARGSUSED */ 3946 static int 3947 vhci_pathinfo_init(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags) 3948 { 3949 scsi_hba_tran_t *hba = NULL; 3950 struct scsi_device *psd = NULL; 3951 scsi_vhci_lun_t *vlun = NULL; 3952 dev_info_t *pdip = NULL; 3953 dev_info_t *tgt_dip; 3954 struct scsi_vhci *vhci; 3955 char *guid; 3956 scsi_vhci_priv_t *svp = NULL; 3957 int rval = MDI_FAILURE; 3958 int vlun_alloced = 0; 3959 3960 ASSERT(vdip != NULL); 3961 ASSERT(pip != NULL); 3962 3963 vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip)); 3964 ASSERT(vhci != NULL); 3965 3966 pdip = mdi_pi_get_phci(pip); 3967 ASSERT(pdip != NULL); 3968 3969 hba = ddi_get_driver_private(pdip); 3970 ASSERT(hba != NULL); 3971 3972 tgt_dip = mdi_pi_get_client(pip); 3973 ASSERT(tgt_dip != NULL); 3974 3975 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip, PROPFLAGS, 3976 MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS) { 3977 VHCI_DEBUG(1, (CE_WARN, NULL, 3978 "vhci_pathinfo_init: lun guid property failed")); 3979 goto failure; 3980 } 3981 3982 vlun = vhci_lun_lookup_alloc(tgt_dip, guid, &vlun_alloced); 3983 ddi_prop_free(guid); 3984 3985 vlun->svl_dip = tgt_dip; 3986 3987 svp = kmem_zalloc(sizeof (*svp), KM_SLEEP); 3988 svp->svp_svl = vlun; 3989 3990 vlun->svl_lb_policy_save = mdi_get_lb_policy(tgt_dip); 3991 mutex_init(&svp->svp_mutex, NULL, MUTEX_DRIVER, NULL); 3992 cv_init(&svp->svp_cv, NULL, CV_DRIVER, NULL); 3993 3994 psd = kmem_zalloc(sizeof (*psd), KM_SLEEP); 3995 mutex_init(&psd->sd_mutex, NULL, MUTEX_DRIVER, NULL); 3996 3997 if (hba->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX) { 3998 /* 3999 * For a SCSI_HBA_ADDR_COMPLEX transport we store a pointer to 4000 * scsi_device in the scsi_address structure. This allows an 4001 * an HBA driver to find its scsi_device(9S) and 4002 * per-scsi_device(9S) HBA private data given a 4003 * scsi_address(9S) by using scsi_address_device(9F) and 4004 * scsi_device_hba_private_get(9F)). 4005 */ 4006 psd->sd_address.a.a_sd = psd; 4007 } else if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) { 4008 /* 4009 * Clone transport structure if requested, so 4010 * Self enumerating HBAs always need to use cloning 4011 */ 4012 scsi_hba_tran_t *clone = 4013 kmem_alloc(sizeof (scsi_hba_tran_t), KM_SLEEP); 4014 bcopy(hba, clone, sizeof (scsi_hba_tran_t)); 4015 hba = clone; 4016 hba->tran_sd = psd; 4017 } else { 4018 /* 4019 * SPI pHCI unit-address. If we ever need to support this 4020 * we could set a.spi.a_target/a.spi.a_lun based on pathinfo 4021 * node unit-address properties. For now we fail... 4022 */ 4023 goto failure; 4024 } 4025 4026 psd->sd_dev = tgt_dip; 4027 psd->sd_address.a_hba_tran = hba; 4028 4029 /* 4030 * Mark scsi_device as being associated with a pathinfo node. For 4031 * a scsi_device structure associated with a devinfo node, 4032 * scsi_ctlops_initchild sets this field to NULL. 4033 */ 4034 psd->sd_pathinfo = pip; 4035 4036 /* 4037 * LEGACY: sd_private: set for older mpxio-capable pHCI drivers with 4038 * too much scsi_vhci/mdi/ndi knowledge. Remove this code when all 4039 * mpxio-capable pHCI drivers use SCSA enumeration services (or at 4040 * least have been changed to use sd_pathinfo instead). 4041 */ 4042 psd->sd_private = (caddr_t)pip; 4043 4044 /* See scsi_hba.c for info on sd_tran_safe kludge */ 4045 psd->sd_tran_safe = hba; 4046 4047 svp->svp_psd = psd; 4048 mdi_pi_set_vhci_private(pip, (caddr_t)svp); 4049 4050 /* 4051 * call hba's target init entry point if it exists 4052 */ 4053 if (hba->tran_tgt_init != NULL) { 4054 if ((rval = (*hba->tran_tgt_init)(pdip, tgt_dip, 4055 hba, psd)) != DDI_SUCCESS) { 4056 VHCI_DEBUG(1, (CE_WARN, pdip, 4057 "!vhci_pathinfo_init: tran_tgt_init failed for " 4058 "path=0x%p rval=%x", (void *)pip, rval)); 4059 goto failure; 4060 } 4061 } 4062 4063 svp->svp_new_path = 1; 4064 4065 VHCI_DEBUG(4, (CE_NOTE, NULL, "!vhci_pathinfo_init: path:%p\n", 4066 (void *)pip)); 4067 return (MDI_SUCCESS); 4068 4069 failure: 4070 if (psd) { 4071 mutex_destroy(&psd->sd_mutex); 4072 kmem_free(psd, sizeof (*psd)); 4073 } 4074 if (svp) { 4075 mdi_pi_set_vhci_private(pip, NULL); 4076 mutex_destroy(&svp->svp_mutex); 4077 cv_destroy(&svp->svp_cv); 4078 kmem_free(svp, sizeof (*svp)); 4079 } 4080 if (hba && (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE)) 4081 kmem_free(hba, sizeof (scsi_hba_tran_t)); 4082 4083 if (vlun_alloced) 4084 vhci_lun_free(tgt_dip); 4085 4086 return (rval); 4087 } 4088 4089 /* ARGSUSED */ 4090 static int 4091 vhci_pathinfo_uninit(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags) 4092 { 4093 scsi_hba_tran_t *hba = NULL; 4094 struct scsi_device *psd = NULL; 4095 dev_info_t *pdip = NULL; 4096 dev_info_t *cdip = NULL; 4097 scsi_vhci_priv_t *svp = NULL; 4098 4099 ASSERT(vdip != NULL); 4100 ASSERT(pip != NULL); 4101 4102 pdip = mdi_pi_get_phci(pip); 4103 ASSERT(pdip != NULL); 4104 4105 cdip = mdi_pi_get_client(pip); 4106 ASSERT(cdip != NULL); 4107 4108 hba = ddi_get_driver_private(pdip); 4109 ASSERT(hba != NULL); 4110 4111 vhci_mpapi_set_path_state(vdip, pip, MP_DRVR_PATH_STATE_UNINIT); 4112 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip); 4113 if (svp == NULL) { 4114 /* path already freed. Nothing to do. */ 4115 return (MDI_SUCCESS); 4116 } 4117 4118 psd = svp->svp_psd; 4119 ASSERT(psd != NULL); 4120 4121 if (hba->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX) { 4122 /* Verify plumbing */ 4123 ASSERT(psd->sd_address.a_hba_tran == hba); 4124 ASSERT(psd->sd_address.a.a_sd == psd); 4125 } else if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) { 4126 /* Switch to cloned scsi_hba_tran(9S) structure */ 4127 hba = psd->sd_address.a_hba_tran; 4128 ASSERT(hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE); 4129 ASSERT(hba->tran_sd == psd); 4130 } 4131 4132 if (hba->tran_tgt_free != NULL) { 4133 (*hba->tran_tgt_free) (pdip, cdip, hba, psd); 4134 } 4135 mutex_destroy(&psd->sd_mutex); 4136 if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) { 4137 kmem_free(hba, sizeof (*hba)); 4138 } 4139 4140 mdi_pi_set_vhci_private(pip, NULL); 4141 4142 /* 4143 * Free the pathinfo related scsi_device inquiry data. Note that this 4144 * matches what happens for scsi_hba.c devinfo case at uninitchild time. 4145 */ 4146 if (psd->sd_inq) 4147 kmem_free((caddr_t)psd->sd_inq, sizeof (struct scsi_inquiry)); 4148 kmem_free((caddr_t)psd, sizeof (*psd)); 4149 4150 mutex_destroy(&svp->svp_mutex); 4151 cv_destroy(&svp->svp_cv); 4152 kmem_free((caddr_t)svp, sizeof (*svp)); 4153 4154 /* 4155 * If this is the last path to the client, 4156 * then free up the vlun as well. 4157 */ 4158 if (mdi_client_get_path_count(cdip) == 1) { 4159 vhci_lun_free(cdip); 4160 } 4161 4162 VHCI_DEBUG(4, (CE_NOTE, NULL, "!vhci_pathinfo_uninit: path=0x%p\n", 4163 (void *)pip)); 4164 return (MDI_SUCCESS); 4165 } 4166 4167 /* ARGSUSED */ 4168 static int 4169 vhci_pathinfo_state_change(dev_info_t *vdip, mdi_pathinfo_t *pip, 4170 mdi_pathinfo_state_t state, uint32_t ext_state, int flags) 4171 { 4172 int rval = MDI_SUCCESS; 4173 scsi_vhci_priv_t *svp; 4174 scsi_vhci_lun_t *vlun; 4175 int held; 4176 int op = (flags & 0xf00) >> 8; 4177 struct scsi_vhci *vhci; 4178 4179 vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip)); 4180 4181 if (flags & MDI_EXT_STATE_CHANGE) { 4182 /* 4183 * We do not want to issue any commands down the path in case 4184 * sync flag is set. Lower layers might not be ready to accept 4185 * any I/O commands. 4186 */ 4187 if (op == DRIVER_DISABLE) 4188 return (MDI_SUCCESS); 4189 4190 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip); 4191 if (svp == NULL) { 4192 return (MDI_FAILURE); 4193 } 4194 vlun = svp->svp_svl; 4195 4196 if (flags & MDI_BEFORE_STATE_CHANGE) { 4197 /* 4198 * Hold the LUN. 4199 */ 4200 VHCI_HOLD_LUN(vlun, VH_SLEEP, held); 4201 if (flags & MDI_DISABLE_OP) { 4202 /* 4203 * Issue scsi reset if it happens to be 4204 * reserved path. 4205 */ 4206 if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) { 4207 /* 4208 * if reservation pending on 4209 * this path, dont' mark the 4210 * path busy 4211 */ 4212 if (op == DRIVER_DISABLE_TRANSIENT) { 4213 VHCI_DEBUG(1, (CE_NOTE, NULL, 4214 "!vhci_pathinfo" 4215 "_state_change (pip:%p): " 4216 " reservation: fail busy\n", 4217 (void *)pip)); 4218 return (MDI_FAILURE); 4219 } 4220 if (pip == vlun->svl_resrv_pip) { 4221 if (vhci_recovery_reset( 4222 svp->svp_svl, 4223 &svp->svp_psd->sd_address, 4224 TRUE, 4225 VHCI_DEPTH_TARGET) == 0) { 4226 VHCI_DEBUG(1, 4227 (CE_NOTE, NULL, 4228 "!vhci_pathinfo" 4229 "_state_change " 4230 " (pip:%p): " 4231 "reset failed, " 4232 "give up!\n", 4233 (void *)pip)); 4234 } 4235 vlun->svl_flags &= 4236 ~VLUN_RESERVE_ACTIVE_FLG; 4237 } 4238 } 4239 } else if (flags & MDI_ENABLE_OP) { 4240 if (((vhci->vhci_conf_flags & 4241 VHCI_CONF_FLAGS_AUTO_FAILBACK) == 4242 VHCI_CONF_FLAGS_AUTO_FAILBACK) && 4243 MDI_PI_IS_USER_DISABLE(pip) && 4244 MDI_PI_IS_STANDBY(pip)) { 4245 struct scsi_failover_ops *fo; 4246 char *best_pclass, *pclass = NULL; 4247 int best_class, rv; 4248 /* 4249 * Failback if enabling a standby path 4250 * and it is the primary class or 4251 * preferred class 4252 */ 4253 best_class = mdi_pi_get_preferred(pip); 4254 if (best_class == 0) { 4255 /* 4256 * if not preferred - compare 4257 * path-class with class 4258 */ 4259 fo = vlun->svl_fops; 4260 (void) fo->sfo_pathclass_next( 4261 NULL, &best_pclass, 4262 vlun->svl_fops_ctpriv); 4263 pclass = NULL; 4264 rv = mdi_prop_lookup_string(pip, 4265 "path-class", &pclass); 4266 if (rv != MDI_SUCCESS || 4267 pclass == NULL) { 4268 vhci_log(CE_NOTE, vdip, 4269 "!path-class " 4270 " lookup " 4271 "failed. rv: %d" 4272 "class: %p", rv, 4273 (void *)pclass); 4274 } else if (strncmp(pclass, 4275 best_pclass, 4276 strlen(best_pclass)) == 0) { 4277 best_class = 1; 4278 } 4279 if (rv == MDI_SUCCESS && 4280 pclass != NULL) { 4281 rv = mdi_prop_free( 4282 pclass); 4283 if (rv != 4284 DDI_PROP_SUCCESS) { 4285 vhci_log( 4286 CE_NOTE, 4287 vdip, 4288 "!path-" 4289 "class" 4290 " free" 4291 " failed" 4292 " rv: %d" 4293 " class: " 4294 "%p", 4295 rv, 4296 (void *) 4297 pclass); 4298 } 4299 } 4300 } 4301 if (best_class == 1) { 4302 VHCI_DEBUG(1, (CE_NOTE, NULL, 4303 "preferred path: %p " 4304 "USER_DISABLE->USER_ENABLE " 4305 "transition for lun %s\n", 4306 (void *)pip, 4307 vlun->svl_lun_wwn)); 4308 (void) taskq_dispatch( 4309 vhci->vhci_taskq, 4310 vhci_initiate_auto_failback, 4311 (void *) vlun, KM_SLEEP); 4312 } 4313 } 4314 /* 4315 * if PGR is active, revalidate key and 4316 * register on this path also, if key is 4317 * still valid 4318 */ 4319 sema_p(&vlun->svl_pgr_sema); 4320 if (vlun->svl_pgr_active) 4321 (void) 4322 vhci_pgr_validate_and_register(svp); 4323 sema_v(&vlun->svl_pgr_sema); 4324 /* 4325 * Inform target driver about any 4326 * reservations to be reinstated if target 4327 * has dropped reservation during the busy 4328 * period. 4329 */ 4330 mutex_enter(&vhci->vhci_mutex); 4331 scsi_hba_reset_notify_callback( 4332 &vhci->vhci_mutex, 4333 &vhci->vhci_reset_notify_listf); 4334 mutex_exit(&vhci->vhci_mutex); 4335 } 4336 } 4337 if (flags & MDI_AFTER_STATE_CHANGE) { 4338 if (flags & MDI_ENABLE_OP) { 4339 mutex_enter(&vhci_global_mutex); 4340 cv_broadcast(&vhci_cv); 4341 mutex_exit(&vhci_global_mutex); 4342 } 4343 if (vlun->svl_setcap_done) { 4344 (void) vhci_pHCI_cap(&svp->svp_psd->sd_address, 4345 "sector-size", vlun->svl_sector_size, 4346 1, pip); 4347 } 4348 4349 /* 4350 * Release the LUN 4351 */ 4352 VHCI_RELEASE_LUN(vlun); 4353 4354 /* 4355 * Path transition is complete. 4356 * Run callback to indicate target driver to 4357 * retry to prevent IO starvation. 4358 */ 4359 if (scsi_callback_id != 0) { 4360 ddi_run_callback(&scsi_callback_id); 4361 } 4362 } 4363 } else { 4364 switch (state) { 4365 case MDI_PATHINFO_STATE_ONLINE: 4366 rval = vhci_pathinfo_online(vdip, pip, flags); 4367 break; 4368 4369 case MDI_PATHINFO_STATE_OFFLINE: 4370 rval = vhci_pathinfo_offline(vdip, pip, flags); 4371 break; 4372 4373 default: 4374 break; 4375 } 4376 /* 4377 * Path transition is complete. 4378 * Run callback to indicate target driver to 4379 * retry to prevent IO starvation. 4380 */ 4381 if ((rval == MDI_SUCCESS) && (scsi_callback_id != 0)) { 4382 ddi_run_callback(&scsi_callback_id); 4383 } 4384 return (rval); 4385 } 4386 4387 return (MDI_SUCCESS); 4388 } 4389 4390 /* 4391 * Parse the mpxio load balancing options. The datanameptr 4392 * will point to a string containing the load-balance-options value. 4393 * The load-balance-options value will be a property that 4394 * defines the load-balance algorithm and any arguments to that 4395 * algorithm. 4396 * For example: 4397 * device-type-mpxio-options-list= 4398 * "device-type=SUN SENA", "load-balance-options=logical-block-options" 4399 * "device-type=SUN SE6920", "round-robin-options"; 4400 * logical-block-options="load-balance=logical-block", "region-size=15"; 4401 * round-robin-options="load-balance=round-robin"; 4402 * 4403 * If the load-balance is not defined the load balance algorithm will 4404 * default to the global setting. There will be default values assigned 4405 * to the arguments (region-size=18) and if an argument is one 4406 * that is not known, it will be ignored. 4407 */ 4408 static void 4409 vhci_parse_mpxio_lb_options(dev_info_t *dip, dev_info_t *cdip, 4410 caddr_t datanameptr) 4411 { 4412 char *dataptr, *next_entry; 4413 caddr_t config_list = NULL; 4414 int config_list_len = 0, list_len = 0; 4415 int region_size = -1; 4416 client_lb_t load_balance; 4417 4418 if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, datanameptr, 4419 (caddr_t)&config_list, &config_list_len) != DDI_PROP_SUCCESS) { 4420 return; 4421 } 4422 4423 list_len = config_list_len; 4424 next_entry = config_list; 4425 while (config_list_len > 0) { 4426 dataptr = next_entry; 4427 4428 if (strncmp(mdi_load_balance, dataptr, 4429 strlen(mdi_load_balance)) == 0) { 4430 /* get the load-balance scheme */ 4431 dataptr += strlen(mdi_load_balance) + 1; 4432 if (strcmp(dataptr, LOAD_BALANCE_PROP_RR) == 0) { 4433 (void) mdi_set_lb_policy(cdip, LOAD_BALANCE_RR); 4434 load_balance = LOAD_BALANCE_RR; 4435 } else if (strcmp(dataptr, 4436 LOAD_BALANCE_PROP_LBA) == 0) { 4437 (void) mdi_set_lb_policy(cdip, 4438 LOAD_BALANCE_LBA); 4439 load_balance = LOAD_BALANCE_LBA; 4440 } else if (strcmp(dataptr, 4441 LOAD_BALANCE_PROP_NONE) == 0) { 4442 (void) mdi_set_lb_policy(cdip, 4443 LOAD_BALANCE_NONE); 4444 load_balance = LOAD_BALANCE_NONE; 4445 } 4446 } else if (strncmp(dataptr, LOGICAL_BLOCK_REGION_SIZE, 4447 strlen(LOGICAL_BLOCK_REGION_SIZE)) == 0) { 4448 int i = 0; 4449 char *ptr; 4450 char *tmp; 4451 4452 tmp = dataptr + (strlen(LOGICAL_BLOCK_REGION_SIZE) + 1); 4453 /* check for numeric value */ 4454 for (ptr = tmp; i < strlen(tmp); i++, ptr++) { 4455 if (!isdigit(*ptr)) { 4456 cmn_err(CE_WARN, 4457 "Illegal region size: %s." 4458 " Setting to default value: %d", 4459 tmp, 4460 LOAD_BALANCE_DEFAULT_REGION_SIZE); 4461 region_size = 4462 LOAD_BALANCE_DEFAULT_REGION_SIZE; 4463 break; 4464 } 4465 } 4466 if (i >= strlen(tmp)) { 4467 region_size = stoi(&tmp); 4468 } 4469 (void) mdi_set_lb_region_size(cdip, region_size); 4470 } 4471 config_list_len -= (strlen(next_entry) + 1); 4472 next_entry += strlen(next_entry) + 1; 4473 } 4474 #ifdef DEBUG 4475 if ((region_size >= 0) && (load_balance != LOAD_BALANCE_LBA)) { 4476 VHCI_DEBUG(1, (CE_NOTE, dip, 4477 "!vhci_parse_mpxio_lb_options: region-size: %d" 4478 "only valid for load-balance=logical-block\n", 4479 region_size)); 4480 } 4481 #endif 4482 if ((region_size == -1) && (load_balance == LOAD_BALANCE_LBA)) { 4483 VHCI_DEBUG(1, (CE_NOTE, dip, 4484 "!vhci_parse_mpxio_lb_options: No region-size" 4485 " defined load-balance=logical-block." 4486 " Default to: %d\n", LOAD_BALANCE_DEFAULT_REGION_SIZE)); 4487 (void) mdi_set_lb_region_size(cdip, 4488 LOAD_BALANCE_DEFAULT_REGION_SIZE); 4489 } 4490 if (list_len > 0) { 4491 kmem_free(config_list, list_len); 4492 } 4493 } 4494 4495 /* 4496 * Parse the device-type-mpxio-options-list looking for the key of 4497 * "load-balance-options". If found, parse the load balancing options. 4498 * Check the comment of the vhci_get_device_type_mpxio_options() 4499 * for the device-type-mpxio-options-list. 4500 */ 4501 static void 4502 vhci_parse_mpxio_options(dev_info_t *dip, dev_info_t *cdip, 4503 caddr_t datanameptr, int list_len) 4504 { 4505 char *dataptr; 4506 int len; 4507 4508 /* 4509 * get the data list 4510 */ 4511 dataptr = datanameptr; 4512 len = 0; 4513 while (len < list_len && 4514 strncmp(dataptr, DEVICE_TYPE_STR, strlen(DEVICE_TYPE_STR)) 4515 != 0) { 4516 if (strncmp(dataptr, LOAD_BALANCE_OPTIONS, 4517 strlen(LOAD_BALANCE_OPTIONS)) == 0) { 4518 len += strlen(LOAD_BALANCE_OPTIONS) + 1; 4519 dataptr += strlen(LOAD_BALANCE_OPTIONS) + 1; 4520 vhci_parse_mpxio_lb_options(dip, cdip, dataptr); 4521 } 4522 len += strlen(dataptr) + 1; 4523 dataptr += strlen(dataptr) + 1; 4524 } 4525 } 4526 4527 /* 4528 * Check the inquriy string returned from the device with the device-type 4529 * Check for the existence of the device-type-mpxio-options-list and 4530 * if found parse the list checking for a match with the device-type 4531 * value and the inquiry string returned from the device. If a match 4532 * is found, parse the mpxio options list. The format of the 4533 * device-type-mpxio-options-list is: 4534 * device-type-mpxio-options-list= 4535 * "device-type=SUN SENA", "load-balance-options=logical-block-options" 4536 * "device-type=SUN SE6920", "round-robin-options"; 4537 * logical-block-options="load-balance=logical-block", "region-size=15"; 4538 * round-robin-options="load-balance=round-robin"; 4539 */ 4540 void 4541 vhci_get_device_type_mpxio_options(dev_info_t *dip, dev_info_t *cdip, 4542 struct scsi_device *devp) 4543 { 4544 4545 caddr_t config_list = NULL; 4546 caddr_t vidptr, datanameptr; 4547 int vidlen, dupletlen = 0; 4548 int config_list_len = 0, len; 4549 struct scsi_inquiry *inq = devp->sd_inq; 4550 4551 /* 4552 * look up the device-type-mpxio-options-list and walk thru 4553 * the list compare the vendor ids of the earlier inquiry command and 4554 * with those vids in the list if there is a match, lookup 4555 * the mpxio-options value 4556 */ 4557 if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 4558 MPXIO_OPTIONS_LIST, 4559 (caddr_t)&config_list, &config_list_len) == DDI_PROP_SUCCESS) { 4560 4561 /* 4562 * Compare vids in each duplet - if it matches, 4563 * parse the mpxio options list. 4564 */ 4565 for (len = config_list_len, vidptr = config_list; len > 0; 4566 len -= dupletlen) { 4567 4568 dupletlen = 0; 4569 4570 if (strlen(vidptr) != 0 && 4571 strncmp(vidptr, DEVICE_TYPE_STR, 4572 strlen(DEVICE_TYPE_STR)) == 0) { 4573 /* point to next duplet */ 4574 datanameptr = vidptr + strlen(vidptr) + 1; 4575 /* add len of this duplet */ 4576 dupletlen += strlen(vidptr) + 1; 4577 /* get to device type */ 4578 vidptr += strlen(DEVICE_TYPE_STR) + 1; 4579 vidlen = strlen(vidptr); 4580 if ((vidlen != 0) && 4581 bcmp(inq->inq_vid, vidptr, vidlen) == 0) { 4582 vhci_parse_mpxio_options(dip, cdip, 4583 datanameptr, len - dupletlen); 4584 break; 4585 } 4586 /* get to next duplet */ 4587 vidptr += strlen(vidptr) + 1; 4588 } 4589 /* get to the next device-type */ 4590 while (len - dupletlen > 0 && 4591 strlen(vidptr) != 0 && 4592 strncmp(vidptr, DEVICE_TYPE_STR, 4593 strlen(DEVICE_TYPE_STR)) != 0) { 4594 dupletlen += strlen(vidptr) + 1; 4595 vidptr += strlen(vidptr) + 1; 4596 } 4597 } 4598 if (config_list_len > 0) { 4599 kmem_free(config_list, config_list_len); 4600 } 4601 } 4602 } 4603 4604 static int 4605 vhci_update_pathinfo(struct scsi_device *psd, mdi_pathinfo_t *pip, 4606 struct scsi_failover_ops *fo, 4607 scsi_vhci_lun_t *vlun, 4608 struct scsi_vhci *vhci) 4609 { 4610 struct scsi_path_opinfo opinfo; 4611 char *pclass, *best_pclass; 4612 4613 if (fo->sfo_path_get_opinfo(psd, &opinfo, vlun->svl_fops_ctpriv) != 0) { 4614 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_update_pathinfo: " 4615 "Failed to get operation info for path:%p\n", (void *)pip)); 4616 return (MDI_FAILURE); 4617 } 4618 /* set the xlf capable flag in the vlun for future use */ 4619 vlun->svl_xlf_capable = opinfo.opinfo_xlf_capable; 4620 (void) mdi_prop_update_string(pip, "path-class", 4621 opinfo.opinfo_path_attr); 4622 4623 pclass = opinfo.opinfo_path_attr; 4624 if (opinfo.opinfo_path_state == SCSI_PATH_ACTIVE) { 4625 mutex_enter(&vlun->svl_mutex); 4626 if (vlun->svl_active_pclass != NULL) { 4627 if (strcmp(vlun->svl_active_pclass, pclass) != 0) { 4628 mutex_exit(&vlun->svl_mutex); 4629 /* 4630 * Externally initiated failover has happened; 4631 * force the path state to be STANDBY/ONLINE, 4632 * next IO will trigger failover and thus 4633 * sync-up the pathstates. Reason we don't 4634 * sync-up immediately by invoking 4635 * vhci_update_pathstates() is because it 4636 * needs a VHCI_HOLD_LUN() and we don't 4637 * want to block here. 4638 * 4639 * Further, if the device is an ALUA device, 4640 * then failure to exactly match 'pclass' and 4641 * 'svl_active_pclass'(as is the case here) 4642 * indicates that the currently active path 4643 * is a 'non-optimized' path - which means 4644 * that 'svl_active_pclass' needs to be 4645 * replaced with opinfo.opinfo_path_state 4646 * value. 4647 */ 4648 4649 if (SCSI_FAILOVER_IS_TPGS(vlun->svl_fops)) { 4650 char *tptr; 4651 4652 /* 4653 * The device is ALUA compliant. The 4654 * state need to be changed to online 4655 * rather than standby state which is 4656 * done typically for a asymmetric 4657 * device that is non ALUA compliant. 4658 */ 4659 mdi_pi_set_state(pip, 4660 MDI_PATHINFO_STATE_ONLINE); 4661 tptr = kmem_alloc(strlen 4662 (opinfo.opinfo_path_attr)+1, 4663 KM_SLEEP); 4664 (void) strlcpy(tptr, 4665 opinfo.opinfo_path_attr, 4666 (strlen(opinfo.opinfo_path_attr) 4667 +1)); 4668 mutex_enter(&vlun->svl_mutex); 4669 kmem_free(vlun->svl_active_pclass, 4670 strlen(vlun->svl_active_pclass)+1); 4671 vlun->svl_active_pclass = tptr; 4672 mutex_exit(&vlun->svl_mutex); 4673 } else { 4674 /* 4675 * Non ALUA device case. 4676 */ 4677 mdi_pi_set_state(pip, 4678 MDI_PATHINFO_STATE_STANDBY); 4679 } 4680 vlun->svl_fo_support = opinfo.opinfo_mode; 4681 mdi_pi_set_preferred(pip, 4682 opinfo.opinfo_preferred); 4683 return (MDI_SUCCESS); 4684 } 4685 } else { 4686 char *tptr; 4687 4688 /* 4689 * lets release the mutex before we try to 4690 * allocate since the potential to sleep is 4691 * possible. 4692 */ 4693 mutex_exit(&vlun->svl_mutex); 4694 tptr = kmem_alloc(strlen(pclass)+1, KM_SLEEP); 4695 (void) strlcpy(tptr, pclass, (strlen(pclass)+1)); 4696 mutex_enter(&vlun->svl_mutex); 4697 vlun->svl_active_pclass = tptr; 4698 } 4699 mutex_exit(&vlun->svl_mutex); 4700 mdi_pi_set_state(pip, MDI_PATHINFO_STATE_ONLINE); 4701 vlun->svl_waiting_for_activepath = 0; 4702 } else if (opinfo.opinfo_path_state == SCSI_PATH_ACTIVE_NONOPT) { 4703 mutex_enter(&vlun->svl_mutex); 4704 if (vlun->svl_active_pclass == NULL) { 4705 char *tptr; 4706 4707 mutex_exit(&vlun->svl_mutex); 4708 tptr = kmem_alloc(strlen(pclass)+1, KM_SLEEP); 4709 (void) strlcpy(tptr, pclass, (strlen(pclass)+1)); 4710 mutex_enter(&vlun->svl_mutex); 4711 vlun->svl_active_pclass = tptr; 4712 } 4713 mutex_exit(&vlun->svl_mutex); 4714 mdi_pi_set_state(pip, MDI_PATHINFO_STATE_ONLINE); 4715 vlun->svl_waiting_for_activepath = 0; 4716 } else if (opinfo.opinfo_path_state == SCSI_PATH_INACTIVE) { 4717 mutex_enter(&vlun->svl_mutex); 4718 if (vlun->svl_active_pclass != NULL) { 4719 if (strcmp(vlun->svl_active_pclass, pclass) == 0) { 4720 mutex_exit(&vlun->svl_mutex); 4721 /* 4722 * externally initiated failover has happened; 4723 * force state to ONLINE (see comment above) 4724 */ 4725 mdi_pi_set_state(pip, 4726 MDI_PATHINFO_STATE_ONLINE); 4727 vlun->svl_fo_support = opinfo.opinfo_mode; 4728 mdi_pi_set_preferred(pip, 4729 opinfo.opinfo_preferred); 4730 return (MDI_SUCCESS); 4731 } 4732 } 4733 mutex_exit(&vlun->svl_mutex); 4734 mdi_pi_set_state(pip, MDI_PATHINFO_STATE_STANDBY); 4735 4736 /* 4737 * Initiate auto-failback, if enabled, for path if path-state 4738 * is transitioning from OFFLINE->STANDBY and pathclass is the 4739 * preferred pathclass for this storage. 4740 * NOTE: In case where opinfo_path_state is SCSI_PATH_ACTIVE 4741 * (above), where the pi state is set to STANDBY, we don't 4742 * initiate auto-failback as the next IO shall take care of. 4743 * this. See comment above. 4744 */ 4745 (void) fo->sfo_pathclass_next(NULL, &best_pclass, 4746 vlun->svl_fops_ctpriv); 4747 if (((vhci->vhci_conf_flags & VHCI_CONF_FLAGS_AUTO_FAILBACK) == 4748 VHCI_CONF_FLAGS_AUTO_FAILBACK) && 4749 (strcmp(pclass, best_pclass) == 0) && 4750 ((MDI_PI_OLD_STATE(pip) == MDI_PATHINFO_STATE_OFFLINE)|| 4751 (MDI_PI_OLD_STATE(pip) == MDI_PATHINFO_STATE_INIT))) { 4752 VHCI_DEBUG(1, (CE_NOTE, NULL, "%s pathclass path: %p" 4753 " OFFLINE->STANDBY transition for lun %s\n", 4754 best_pclass, (void *)pip, vlun->svl_lun_wwn)); 4755 (void) taskq_dispatch(vhci->vhci_taskq, 4756 vhci_initiate_auto_failback, (void *) vlun, 4757 KM_SLEEP); 4758 } 4759 } 4760 vlun->svl_fo_support = opinfo.opinfo_mode; 4761 mdi_pi_set_preferred(pip, opinfo.opinfo_preferred); 4762 4763 VHCI_DEBUG(8, (CE_NOTE, NULL, "vhci_update_pathinfo: opinfo_rev = %x," 4764 " opinfo_path_state = %x opinfo_preferred = %x, opinfo_mode = %x\n", 4765 opinfo.opinfo_rev, opinfo.opinfo_path_state, 4766 opinfo.opinfo_preferred, opinfo.opinfo_mode)); 4767 4768 return (MDI_SUCCESS); 4769 } 4770 4771 /* 4772 * Form the kstat name and and call mdi_pi_kstat_create() 4773 */ 4774 void 4775 vhci_kstat_create_pathinfo(mdi_pathinfo_t *pip) 4776 { 4777 dev_info_t *tgt_dip; 4778 dev_info_t *pdip; 4779 char *guid; 4780 char *target_port, *target_port_dup; 4781 char ks_name[KSTAT_STRLEN]; 4782 uint_t pid; 4783 int by_id; 4784 mod_hash_val_t hv; 4785 4786 4787 /* return if we have already allocated kstats */ 4788 if (mdi_pi_kstat_exists(pip)) 4789 return; 4790 4791 /* 4792 * We need instance numbers to create a kstat name, return if we don't 4793 * have instance numbers assigned yet. 4794 */ 4795 tgt_dip = mdi_pi_get_client(pip); 4796 pdip = mdi_pi_get_phci(pip); 4797 if ((ddi_get_instance(tgt_dip) == -1) || (ddi_get_instance(pdip) == -1)) 4798 return; 4799 4800 /* 4801 * A path oriented kstat has a ks_name of the form: 4802 * 4803 * <client-driver><instance>.t<pid>.<pHCI-driver><instance> 4804 * 4805 * We maintain a bidirectional 'target-port' to <pid> map, 4806 * called targetmap. All pathinfo nodes with the same 4807 * 'target-port' map to the same <pid>. The iostat(1M) code, 4808 * when parsing a path oriented kstat name, uses the <pid> as 4809 * a SCSI_VHCI_GET_TARGET_LONGNAME ioctl argument in order 4810 * to get the 'target-port'. For KSTAT_FLAG_PERSISTENT kstats, 4811 * this ioctl needs to translate a <pid> to a 'target-port' 4812 * even after all pathinfo nodes associated with the 4813 * 'target-port' have been destroyed. This is needed to support 4814 * consistent first-iteration activity-since-boot iostat(1M) 4815 * output. Because of this requirement, the mapping can't be 4816 * based on pathinfo information in a devinfo snapshot. 4817 */ 4818 4819 /* determine 'target-port' */ 4820 if (mdi_prop_lookup_string(pip, 4821 SCSI_ADDR_PROP_TARGET_PORT, &target_port) == MDI_SUCCESS) { 4822 target_port_dup = i_ddi_strdup(target_port, KM_SLEEP); 4823 (void) mdi_prop_free(target_port); 4824 by_id = 1; 4825 } else { 4826 /* 4827 * If the pHCI did not set up 'target-port' on this 4828 * pathinfo node, assume that our client is the only 4829 * one with paths to the device by using the guid 4830 * value as the 'target-port'. Since no other client 4831 * will have the same guid, no other client will use 4832 * the same <pid>. NOTE: a client with an instance 4833 * number always has a guid. 4834 */ 4835 (void) ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip, 4836 PROPFLAGS, MDI_CLIENT_GUID_PROP, &guid); 4837 target_port_dup = i_ddi_strdup(guid, KM_SLEEP); 4838 ddi_prop_free(guid); 4839 4840 /* 4841 * For this type of mapping we don't want the 4842 * <id> -> 'target-port' mapping to be made. This 4843 * will cause the SCSI_VHCI_GET_TARGET_LONGNAME ioctl 4844 * to fail, and the iostat(1M) long '-n' output will 4845 * still use the <pid>. We do this because we just 4846 * made up the 'target-port' using the guid, and we 4847 * don't want to expose that fact in iostat output. 4848 */ 4849 by_id = 0; 4850 } 4851 4852 /* find/establish <pid> given 'target-port' */ 4853 mutex_enter(&vhci_targetmap_mutex); 4854 if (mod_hash_find(vhci_targetmap_byport, 4855 (mod_hash_key_t)target_port_dup, &hv) == 0) { 4856 pid = (int)(intptr_t)hv; /* mapping exists */ 4857 } else { 4858 pid = vhci_targetmap_pid++; /* new mapping */ 4859 4860 (void) mod_hash_insert(vhci_targetmap_byport, 4861 (mod_hash_key_t)target_port_dup, 4862 (mod_hash_val_t)(intptr_t)pid); 4863 if (by_id) { 4864 (void) mod_hash_insert(vhci_targetmap_bypid, 4865 (mod_hash_key_t)(uintptr_t)pid, 4866 (mod_hash_val_t)(uintptr_t)target_port_dup); 4867 } 4868 target_port_dup = NULL; /* owned by hash */ 4869 } 4870 mutex_exit(&vhci_targetmap_mutex); 4871 4872 /* form kstat name */ 4873 (void) snprintf(ks_name, KSTAT_STRLEN, "%s%d.t%d.%s%d", 4874 ddi_driver_name(tgt_dip), ddi_get_instance(tgt_dip), 4875 pid, ddi_driver_name(pdip), ddi_get_instance(pdip)); 4876 4877 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_path_online: path:%p " 4878 "kstat %s: pid %x <-> port %s\n", (void *)pip, 4879 ks_name, pid, target_port_dup)); 4880 if (target_port_dup) 4881 kmem_free(target_port_dup, strlen(target_port_dup) + 1); 4882 4883 /* call mdi to create kstats with the name we built */ 4884 (void) mdi_pi_kstat_create(pip, ks_name); 4885 } 4886 4887 /* ARGSUSED */ 4888 static int 4889 vhci_pathinfo_online(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags) 4890 { 4891 scsi_hba_tran_t *hba = NULL; 4892 struct scsi_device *psd = NULL; 4893 scsi_vhci_lun_t *vlun = NULL; 4894 dev_info_t *pdip = NULL; 4895 dev_info_t *cdip; 4896 dev_info_t *tgt_dip; 4897 struct scsi_vhci *vhci; 4898 char *guid; 4899 struct scsi_failover_ops *sfo; 4900 scsi_vhci_priv_t *svp = NULL; 4901 struct scsi_address *ap; 4902 struct scsi_pkt *pkt; 4903 int rval = MDI_FAILURE; 4904 mpapi_item_list_t *list_ptr; 4905 mpapi_lu_data_t *ld; 4906 int ce_type; 4907 4908 ASSERT(vdip != NULL); 4909 ASSERT(pip != NULL); 4910 4911 vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip)); 4912 ASSERT(vhci != NULL); 4913 4914 pdip = mdi_pi_get_phci(pip); 4915 hba = ddi_get_driver_private(pdip); 4916 ASSERT(hba != NULL); 4917 4918 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip); 4919 ASSERT(svp != NULL); 4920 4921 cdip = mdi_pi_get_client(pip); 4922 ASSERT(cdip != NULL); 4923 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, cdip, PROPFLAGS, 4924 MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS) { 4925 VHCI_DEBUG(1, (CE_WARN, NULL, "vhci_path_online: lun guid " 4926 "property failed")); 4927 goto failure; 4928 } 4929 4930 vlun = vhci_lun_lookup(cdip); 4931 ASSERT(vlun != NULL); 4932 4933 ddi_prop_free(guid); 4934 4935 vlun->svl_dip = mdi_pi_get_client(pip); 4936 ASSERT(vlun->svl_dip != NULL); 4937 4938 psd = svp->svp_psd; 4939 ASSERT(psd != NULL); 4940 4941 /* 4942 * Get inquiry data into pathinfo related scsi_device structure. 4943 * Free sq_inq when pathinfo related scsi_device structure is destroyed 4944 * by vhci_pathinfo_uninit(). In other words, vhci maintains its own 4945 * copy of scsi_device and scsi_inquiry data on a per-path basis. 4946 */ 4947 if (scsi_probe(psd, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 4948 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_pathinfo_online: " 4949 "scsi_probe failed path:%p rval:%x\n", (void *)pip, rval)); 4950 rval = MDI_FAILURE; 4951 goto failure; 4952 } 4953 4954 /* 4955 * See if we have a failover module to support the device. 4956 * 4957 * We re-probe to determine the failover ops for each path. This 4958 * is done in case there are any path-specific side-effects associated 4959 * with the sfo_device_probe implementation. 4960 * 4961 * Give the first successfull sfo_device_probe the opportunity to 4962 * establish 'ctpriv', vlun/client private data. The ctpriv will 4963 * then be passed into the failover module on all other sfo_device_*() 4964 * operations (and must be freed by sfo_device_unprobe implementation). 4965 * 4966 * NOTE: While sfo_device_probe is done once per path, 4967 * sfo_device_unprobe only occurs once - when the vlun is destroyed. 4968 * 4969 * NOTE: We don't currently support per-path fops private data 4970 * mechanism. 4971 */ 4972 sfo = vhci_dev_fo(vdip, psd, 4973 &vlun->svl_fops_ctpriv, &vlun->svl_fops_name); 4974 4975 /* check path configuration result with current vlun state */ 4976 if (((sfo && vlun->svl_fops) && (sfo != vlun->svl_fops)) || 4977 (sfo && vlun->svl_not_supported) || 4978 ((sfo == NULL) && vlun->svl_fops)) { 4979 /* Getting different results for different paths. */ 4980 VHCI_DEBUG(1, (CE_NOTE, vhci->vhci_dip, 4981 "!vhci_pathinfo_online: dev (path 0x%p) contradiction\n", 4982 (void *)pip)); 4983 #ifdef DEBUG 4984 ce_type = CE_PANIC; 4985 #else /* DEBUG */ 4986 ce_type = CE_WARN; 4987 #endif /* DEBUG */ 4988 cmn_err(ce_type, "scsi_vhci: failover contradiction: " 4989 "'%s'.vs.'%s': path %s\n", 4990 vlun->svl_fops ? vlun->svl_fops->sfo_name : "NULL", 4991 sfo ? sfo->sfo_name : "NULL", mdi_pi_pathname(pip)); 4992 vlun->svl_not_supported = 1; 4993 rval = MDI_NOT_SUPPORTED; 4994 goto done; 4995 } else if (sfo == NULL) { 4996 /* No failover module - device not supported under vHCI. */ 4997 VHCI_DEBUG(1, (CE_NOTE, vhci->vhci_dip, 4998 "!vhci_pathinfo_online: dev (path 0x%p) not " 4999 "supported\n", (void *)pip)); 5000 5001 /* XXX does this contradict vhci_is_dev_supported ? */ 5002 vlun->svl_not_supported = 1; 5003 rval = MDI_NOT_SUPPORTED; 5004 goto done; 5005 } 5006 5007 /* failover supported for device - save failover_ops in vlun */ 5008 vlun->svl_fops = sfo; 5009 ASSERT(vlun->svl_fops_name != NULL); 5010 5011 /* 5012 * Obtain the device-type based mpxio options as specified in 5013 * scsi_vhci.conf file. 5014 * 5015 * NOTE: currently, the end result is a call to 5016 * mdi_set_lb_region_size(). 5017 */ 5018 tgt_dip = psd->sd_dev; 5019 ASSERT(tgt_dip != NULL); 5020 vhci_get_device_type_mpxio_options(vdip, tgt_dip, psd); 5021 5022 /* 5023 * The device probe or options in conf file may have set/changed the 5024 * lb policy, save the current value. 5025 */ 5026 vlun->svl_lb_policy_save = mdi_get_lb_policy(tgt_dip); 5027 5028 /* 5029 * if PGR is active, revalidate key and register on this path also, 5030 * if key is still valid 5031 */ 5032 sema_p(&vlun->svl_pgr_sema); 5033 if (vlun->svl_pgr_active) { 5034 rval = vhci_pgr_validate_and_register(svp); 5035 if (rval != 1) { 5036 rval = MDI_FAILURE; 5037 sema_v(&vlun->svl_pgr_sema); 5038 goto failure; 5039 } 5040 } 5041 sema_v(&vlun->svl_pgr_sema); 5042 5043 if (svp->svp_new_path) { 5044 /* 5045 * Last chance to perform any cleanup operations on this 5046 * new path before making this path completely online. 5047 */ 5048 svp->svp_new_path = 0; 5049 5050 /* 5051 * If scsi_vhci knows the lun is alread RESERVE'd, 5052 * then skip the issue of RELEASE on new path. 5053 */ 5054 if ((vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) == 0) { 5055 /* 5056 * Issue SCSI-2 RELEASE only for the first time on 5057 * a new path just in case the host rebooted and 5058 * a reservation is still pending on this path. 5059 * IBM Shark storage does not clear RESERVE upon 5060 * host reboot. 5061 */ 5062 ap = &psd->sd_address; 5063 pkt = scsi_init_pkt(ap, NULL, NULL, CDB_GROUP0, 5064 sizeof (struct scsi_arq_status), 0, 0, 5065 SLEEP_FUNC, NULL); 5066 if (pkt == NULL) { 5067 VHCI_DEBUG(1, (CE_NOTE, NULL, 5068 "!vhci_pathinfo_online: " 5069 "Release init_pkt failed :%p\n", 5070 (void *)pip)); 5071 rval = MDI_FAILURE; 5072 goto failure; 5073 } 5074 pkt->pkt_cdbp[0] = SCMD_RELEASE; 5075 pkt->pkt_time = 60; 5076 5077 VHCI_DEBUG(1, (CE_NOTE, NULL, 5078 "!vhci_path_online: path:%p " 5079 "Issued SCSI-2 RELEASE\n", (void *)pip)); 5080 5081 /* Ignore the return value */ 5082 (void) vhci_do_scsi_cmd(pkt); 5083 scsi_destroy_pkt(pkt); 5084 } 5085 } 5086 5087 rval = vhci_update_pathinfo(psd, pip, sfo, vlun, vhci); 5088 if (rval == MDI_FAILURE) { 5089 goto failure; 5090 } 5091 5092 /* Initialize MP-API data */ 5093 vhci_update_mpapi_data(vhci, vlun, pip); 5094 5095 /* 5096 * MP-API also needs the Inquiry data to be maintained in the 5097 * mp_vendor_prop_t structure, so find the lun and update its 5098 * structure with this data. 5099 */ 5100 list_ptr = (mpapi_item_list_t *)vhci_get_mpapi_item(vhci, NULL, 5101 MP_OBJECT_TYPE_MULTIPATH_LU, (void *)vlun); 5102 ld = (mpapi_lu_data_t *)list_ptr->item->idata; 5103 if (ld != NULL) { 5104 bcopy(psd->sd_inq->inq_vid, ld->prop.prodInfo.vendor, 8); 5105 bcopy(psd->sd_inq->inq_pid, ld->prop.prodInfo.product, 16); 5106 bcopy(psd->sd_inq->inq_revision, ld->prop.prodInfo.revision, 4); 5107 } else { 5108 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_pathinfo_online: " 5109 "mpapi_lu_data_t is NULL")); 5110 } 5111 5112 /* create kstats for path */ 5113 vhci_kstat_create_pathinfo(pip); 5114 5115 done: 5116 mutex_enter(&vhci_global_mutex); 5117 cv_broadcast(&vhci_cv); 5118 mutex_exit(&vhci_global_mutex); 5119 5120 if (vlun->svl_setcap_done) { 5121 (void) vhci_pHCI_cap(ap, "sector-size", 5122 vlun->svl_sector_size, 1, pip); 5123 } 5124 5125 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_path_online: path:%p\n", 5126 (void *)pip)); 5127 5128 failure: 5129 return (rval); 5130 } 5131 5132 /* 5133 * path offline handler. Release all bindings that will not be 5134 * released by the normal packet transport/completion code path. 5135 * Since we don't (presently) keep any bindings alive outside of 5136 * the in-transport packets (which will be released on completion) 5137 * there is not much to do here. 5138 */ 5139 /* ARGSUSED */ 5140 static int 5141 vhci_pathinfo_offline(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags) 5142 { 5143 scsi_hba_tran_t *hba = NULL; 5144 struct scsi_device *psd = NULL; 5145 dev_info_t *pdip = NULL; 5146 dev_info_t *cdip = NULL; 5147 scsi_vhci_priv_t *svp = NULL; 5148 5149 ASSERT(vdip != NULL); 5150 ASSERT(pip != NULL); 5151 5152 pdip = mdi_pi_get_phci(pip); 5153 ASSERT(pdip != NULL); 5154 if (pdip == NULL) { 5155 VHCI_DEBUG(1, (CE_WARN, vdip, "Invalid path 0x%p: NULL " 5156 "phci dip", (void *)pip)); 5157 return (MDI_FAILURE); 5158 } 5159 5160 cdip = mdi_pi_get_client(pip); 5161 ASSERT(cdip != NULL); 5162 if (cdip == NULL) { 5163 VHCI_DEBUG(1, (CE_WARN, vdip, "Invalid path 0x%p: NULL " 5164 "client dip", (void *)pip)); 5165 return (MDI_FAILURE); 5166 } 5167 5168 hba = ddi_get_driver_private(pdip); 5169 ASSERT(hba != NULL); 5170 5171 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip); 5172 if (svp == NULL) { 5173 /* 5174 * mdi_pathinfo node in INIT state can have vHCI private 5175 * information set to null 5176 */ 5177 VHCI_DEBUG(1, (CE_NOTE, vdip, "!vhci_pathinfo_offline: " 5178 "svp is NULL for pip 0x%p\n", (void *)pip)); 5179 return (MDI_SUCCESS); 5180 } 5181 5182 psd = svp->svp_psd; 5183 ASSERT(psd != NULL); 5184 5185 mutex_enter(&svp->svp_mutex); 5186 5187 VHCI_DEBUG(1, (CE_NOTE, vdip, "!vhci_pathinfo_offline: " 5188 "%d cmds pending on path: 0x%p\n", svp->svp_cmds, (void *)pip)); 5189 while (svp->svp_cmds != 0) { 5190 if (cv_timedwait(&svp->svp_cv, &svp->svp_mutex, 5191 ddi_get_lbolt() + 5192 drv_usectohz(vhci_path_quiesce_timeout * 1000000)) == -1) { 5193 /* 5194 * The timeout time reached without the condition 5195 * being signaled. 5196 */ 5197 VHCI_DEBUG(1, (CE_NOTE, vdip, "!vhci_pathinfo_offline: " 5198 "Timeout reached on path 0x%p without the cond\n", 5199 (void *)pip)); 5200 VHCI_DEBUG(1, (CE_NOTE, vdip, "!vhci_pathinfo_offline: " 5201 "%d cmds still pending on path: 0x%p\n", 5202 svp->svp_cmds, (void *)pip)); 5203 break; 5204 } 5205 } 5206 mutex_exit(&svp->svp_mutex); 5207 5208 /* 5209 * Check to see if this vlun has an active SCSI-II RESERVE. And this 5210 * is the pip for the path that has been reserved. 5211 * If so clear the reservation by sending a reset, so the host will not 5212 * get a reservation conflict. Reset the flag VLUN_RESERVE_ACTIVE_FLG 5213 * for this lun. Also a reset notify is sent to the target driver 5214 * just in case the POR check condition is cleared by some other layer 5215 * in the stack. 5216 */ 5217 if (svp->svp_svl->svl_flags & VLUN_RESERVE_ACTIVE_FLG) { 5218 if (pip == svp->svp_svl->svl_resrv_pip) { 5219 if (vhci_recovery_reset(svp->svp_svl, 5220 &svp->svp_psd->sd_address, TRUE, 5221 VHCI_DEPTH_TARGET) == 0) { 5222 VHCI_DEBUG(1, (CE_NOTE, NULL, 5223 "!vhci_pathinfo_offline (pip:%p):" 5224 "reset failed, retrying\n", (void *)pip)); 5225 delay(1*drv_usectohz(1000000)); 5226 if (vhci_recovery_reset(svp->svp_svl, 5227 &svp->svp_psd->sd_address, TRUE, 5228 VHCI_DEPTH_TARGET) == 0) { 5229 VHCI_DEBUG(1, (CE_NOTE, NULL, 5230 "!vhci_pathinfo_offline " 5231 "(pip:%p): reset failed, " 5232 "giving up!\n", (void *)pip)); 5233 } 5234 } 5235 svp->svp_svl->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG; 5236 } 5237 } 5238 5239 mdi_pi_set_state(pip, MDI_PATHINFO_STATE_OFFLINE); 5240 vhci_mpapi_set_path_state(vdip, pip, MP_DRVR_PATH_STATE_REMOVED); 5241 5242 VHCI_DEBUG(1, (CE_NOTE, NULL, 5243 "!vhci_pathinfo_offline: offlined path 0x%p\n", (void *)pip)); 5244 return (MDI_SUCCESS); 5245 } 5246 5247 5248 /* 5249 * routine for SCSI VHCI IOCTL implementation. 5250 */ 5251 /* ARGSUSED */ 5252 static int 5253 vhci_ctl(dev_t dev, int cmd, intptr_t data, int mode, cred_t *credp, int *rval) 5254 { 5255 struct scsi_vhci *vhci; 5256 dev_info_t *vdip; 5257 mdi_pathinfo_t *pip; 5258 int instance, held; 5259 int retval = 0; 5260 caddr_t phci_path = NULL, client_path = NULL; 5261 caddr_t paddr = NULL; 5262 sv_iocdata_t ioc; 5263 sv_iocdata_t *pioc = &ioc; 5264 sv_switch_to_cntlr_iocdata_t iocsc; 5265 sv_switch_to_cntlr_iocdata_t *piocsc = &iocsc; 5266 caddr_t s; 5267 scsi_vhci_lun_t *vlun; 5268 struct scsi_failover_ops *fo; 5269 char *pclass; 5270 5271 /* Check for validity of vhci structure */ 5272 vhci = ddi_get_soft_state(vhci_softstate, MINOR2INST(getminor(dev))); 5273 if (vhci == NULL) { 5274 return (ENXIO); 5275 } 5276 5277 mutex_enter(&vhci->vhci_mutex); 5278 if ((vhci->vhci_state & VHCI_STATE_OPEN) == 0) { 5279 mutex_exit(&vhci->vhci_mutex); 5280 return (ENXIO); 5281 } 5282 mutex_exit(&vhci->vhci_mutex); 5283 5284 /* Get the vhci dip */ 5285 vdip = vhci->vhci_dip; 5286 ASSERT(vdip != NULL); 5287 instance = ddi_get_instance(vdip); 5288 5289 /* Allocate memory for getting parameters from userland */ 5290 phci_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5291 client_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5292 paddr = kmem_zalloc(MAXNAMELEN, KM_SLEEP); 5293 5294 /* 5295 * Set a local variable indicating the ioctl name. Used for 5296 * printing debug strings. 5297 */ 5298 switch (cmd) { 5299 case SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO: 5300 s = "GET_CLIENT_MULTIPATH_INFO"; 5301 break; 5302 5303 case SCSI_VHCI_GET_PHCI_MULTIPATH_INFO: 5304 s = "GET_PHCI_MULTIPATH_INFO"; 5305 break; 5306 5307 case SCSI_VHCI_GET_CLIENT_NAME: 5308 s = "GET_CLIENT_NAME"; 5309 break; 5310 5311 case SCSI_VHCI_PATH_ONLINE: 5312 s = "PATH_ONLINE"; 5313 break; 5314 5315 case SCSI_VHCI_PATH_OFFLINE: 5316 s = "PATH_OFFLINE"; 5317 break; 5318 5319 case SCSI_VHCI_PATH_STANDBY: 5320 s = "PATH_STANDBY"; 5321 break; 5322 5323 case SCSI_VHCI_PATH_TEST: 5324 s = "PATH_TEST"; 5325 break; 5326 5327 case SCSI_VHCI_SWITCH_TO_CNTLR: 5328 s = "SWITCH_TO_CNTLR"; 5329 break; 5330 case SCSI_VHCI_PATH_DISABLE: 5331 s = "PATH_DISABLE"; 5332 break; 5333 case SCSI_VHCI_PATH_ENABLE: 5334 s = "PATH_ENABLE"; 5335 break; 5336 5337 case SCSI_VHCI_GET_TARGET_LONGNAME: 5338 s = "GET_TARGET_LONGNAME"; 5339 break; 5340 5341 #ifdef DEBUG 5342 case SCSI_VHCI_CONFIGURE_PHCI: 5343 s = "CONFIGURE_PHCI"; 5344 break; 5345 5346 case SCSI_VHCI_UNCONFIGURE_PHCI: 5347 s = "UNCONFIGURE_PHCI"; 5348 break; 5349 #endif 5350 5351 default: 5352 s = "Unknown"; 5353 vhci_log(CE_NOTE, vdip, 5354 "!vhci%d: ioctl %x (unsupported ioctl)", instance, cmd); 5355 retval = ENOTSUP; 5356 break; 5357 } 5358 if (retval != 0) { 5359 goto end; 5360 } 5361 5362 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci%d: ioctl <%s>", instance, s)); 5363 5364 /* 5365 * Get IOCTL parameters from userland 5366 */ 5367 switch (cmd) { 5368 case SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO: 5369 case SCSI_VHCI_GET_PHCI_MULTIPATH_INFO: 5370 case SCSI_VHCI_GET_CLIENT_NAME: 5371 case SCSI_VHCI_PATH_ONLINE: 5372 case SCSI_VHCI_PATH_OFFLINE: 5373 case SCSI_VHCI_PATH_STANDBY: 5374 case SCSI_VHCI_PATH_TEST: 5375 case SCSI_VHCI_PATH_DISABLE: 5376 case SCSI_VHCI_PATH_ENABLE: 5377 case SCSI_VHCI_GET_TARGET_LONGNAME: 5378 #ifdef DEBUG 5379 case SCSI_VHCI_CONFIGURE_PHCI: 5380 case SCSI_VHCI_UNCONFIGURE_PHCI: 5381 #endif 5382 retval = vhci_get_iocdata((const void *)data, pioc, mode, s); 5383 break; 5384 5385 case SCSI_VHCI_SWITCH_TO_CNTLR: 5386 retval = vhci_get_iocswitchdata((const void *)data, piocsc, 5387 mode, s); 5388 break; 5389 } 5390 if (retval != 0) { 5391 goto end; 5392 } 5393 5394 5395 /* 5396 * Process the IOCTL 5397 */ 5398 switch (cmd) { 5399 case SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO: 5400 { 5401 uint_t num_paths; /* Num paths to client dev */ 5402 sv_path_info_t *upibuf = NULL; /* To keep userland values */ 5403 sv_path_info_t *kpibuf = NULL; /* Kernel data for ioctls */ 5404 dev_info_t *cdip; /* Client device dip */ 5405 5406 if (pioc->ret_elem == NULL) { 5407 retval = EINVAL; 5408 break; 5409 } 5410 5411 /* Get client device path from user land */ 5412 if (vhci_ioc_get_client_path(pioc, client_path, mode, s)) { 5413 retval = EFAULT; 5414 break; 5415 } 5416 5417 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> " 5418 "client <%s>", s, client_path)); 5419 5420 /* Get number of paths to this client device */ 5421 if ((cdip = mdi_client_path2devinfo(vdip, client_path)) 5422 == NULL) { 5423 retval = ENXIO; 5424 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5425 "client dip doesn't exist. invalid path <%s>", 5426 s, client_path)); 5427 break; 5428 } 5429 num_paths = mdi_client_get_path_count(cdip); 5430 5431 if (ddi_copyout(&num_paths, pioc->ret_elem, 5432 sizeof (num_paths), mode)) { 5433 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5434 "num_paths copyout failed", s)); 5435 retval = EFAULT; 5436 break; 5437 } 5438 5439 /* If user just wanted num_paths, then return */ 5440 if (pioc->buf_elem == 0 || pioc->ret_buf == NULL || 5441 num_paths == 0) { 5442 break; 5443 } 5444 5445 /* Set num_paths to value as much as can be sent to userland */ 5446 if (num_paths > pioc->buf_elem) { 5447 num_paths = pioc->buf_elem; 5448 } 5449 5450 /* Allocate memory and get userland pointers */ 5451 if (vhci_ioc_alloc_pathinfo(&upibuf, &kpibuf, num_paths, 5452 pioc, mode, s) != 0) { 5453 retval = EFAULT; 5454 break; 5455 } 5456 ASSERT(upibuf != NULL); 5457 ASSERT(kpibuf != NULL); 5458 5459 /* 5460 * Get the path information and send it to userland. 5461 */ 5462 if (vhci_get_client_path_list(cdip, kpibuf, num_paths) 5463 != MDI_SUCCESS) { 5464 retval = ENXIO; 5465 vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths); 5466 break; 5467 } 5468 5469 if (vhci_ioc_send_pathinfo(upibuf, kpibuf, num_paths, 5470 pioc, mode, s)) { 5471 retval = EFAULT; 5472 vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths); 5473 break; 5474 } 5475 5476 /* Free the memory allocated for path information */ 5477 vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths); 5478 break; 5479 } 5480 5481 case SCSI_VHCI_GET_PHCI_MULTIPATH_INFO: 5482 { 5483 uint_t num_paths; /* Num paths to client dev */ 5484 sv_path_info_t *upibuf = NULL; /* To keep userland values */ 5485 sv_path_info_t *kpibuf = NULL; /* Kernel data for ioctls */ 5486 dev_info_t *pdip; /* PHCI device dip */ 5487 5488 if (pioc->ret_elem == NULL) { 5489 retval = EINVAL; 5490 break; 5491 } 5492 5493 /* Get PHCI device path from user land */ 5494 if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s)) { 5495 retval = EFAULT; 5496 break; 5497 } 5498 5499 VHCI_DEBUG(6, (CE_WARN, vdip, 5500 "!vhci_ioctl: ioctl <%s> phci <%s>", s, phci_path)); 5501 5502 /* Get number of devices associated with this PHCI device */ 5503 if ((pdip = mdi_phci_path2devinfo(vdip, phci_path)) == NULL) { 5504 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5505 "phci dip doesn't exist. invalid path <%s>", 5506 s, phci_path)); 5507 retval = ENXIO; 5508 break; 5509 } 5510 5511 num_paths = mdi_phci_get_path_count(pdip); 5512 5513 if (ddi_copyout(&num_paths, pioc->ret_elem, 5514 sizeof (num_paths), mode)) { 5515 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5516 "num_paths copyout failed", s)); 5517 retval = EFAULT; 5518 break; 5519 } 5520 5521 /* If user just wanted num_paths, then return */ 5522 if (pioc->buf_elem == 0 || pioc->ret_buf == NULL || 5523 num_paths == 0) { 5524 break; 5525 } 5526 5527 /* Set num_paths to value as much as can be sent to userland */ 5528 if (num_paths > pioc->buf_elem) { 5529 num_paths = pioc->buf_elem; 5530 } 5531 5532 /* Allocate memory and get userland pointers */ 5533 if (vhci_ioc_alloc_pathinfo(&upibuf, &kpibuf, num_paths, 5534 pioc, mode, s) != 0) { 5535 retval = EFAULT; 5536 break; 5537 } 5538 ASSERT(upibuf != NULL); 5539 ASSERT(kpibuf != NULL); 5540 5541 /* 5542 * Get the path information and send it to userland. 5543 */ 5544 if (vhci_get_phci_path_list(pdip, kpibuf, num_paths) 5545 != MDI_SUCCESS) { 5546 retval = ENXIO; 5547 vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths); 5548 break; 5549 } 5550 5551 if (vhci_ioc_send_pathinfo(upibuf, kpibuf, num_paths, 5552 pioc, mode, s)) { 5553 retval = EFAULT; 5554 vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths); 5555 break; 5556 } 5557 5558 /* Free the memory allocated for path information */ 5559 vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths); 5560 break; 5561 } 5562 5563 case SCSI_VHCI_GET_CLIENT_NAME: 5564 { 5565 dev_info_t *cdip, *pdip; 5566 5567 /* Get PHCI path and device address from user land */ 5568 if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s) || 5569 vhci_ioc_get_paddr(pioc, paddr, mode, s)) { 5570 retval = EFAULT; 5571 break; 5572 } 5573 5574 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> " 5575 "phci <%s>, paddr <%s>", s, phci_path, paddr)); 5576 5577 /* Get the PHCI dip */ 5578 if ((pdip = mdi_phci_path2devinfo(vdip, phci_path)) == NULL) { 5579 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5580 "phci dip doesn't exist. invalid path <%s>", 5581 s, phci_path)); 5582 retval = ENXIO; 5583 break; 5584 } 5585 5586 if ((pip = mdi_pi_find(pdip, NULL, paddr)) == NULL) { 5587 VHCI_DEBUG(1, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> " 5588 "pathinfo doesn't exist. invalid device addr", s)); 5589 retval = ENXIO; 5590 break; 5591 } 5592 5593 /* Get the client device pathname and send to userland */ 5594 cdip = mdi_pi_get_client(pip); 5595 vhci_ioc_devi_to_path(cdip, client_path); 5596 5597 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> " 5598 "client <%s>", s, client_path)); 5599 5600 if (vhci_ioc_send_client_path(client_path, pioc, mode, s)) { 5601 retval = EFAULT; 5602 break; 5603 } 5604 break; 5605 } 5606 5607 case SCSI_VHCI_PATH_ONLINE: 5608 case SCSI_VHCI_PATH_OFFLINE: 5609 case SCSI_VHCI_PATH_STANDBY: 5610 case SCSI_VHCI_PATH_TEST: 5611 { 5612 dev_info_t *pdip; /* PHCI dip */ 5613 5614 /* Get PHCI path and device address from user land */ 5615 if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s) || 5616 vhci_ioc_get_paddr(pioc, paddr, mode, s)) { 5617 retval = EFAULT; 5618 break; 5619 } 5620 5621 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> " 5622 "phci <%s>, paddr <%s>", s, phci_path, paddr)); 5623 5624 /* Get the PHCI dip */ 5625 if ((pdip = mdi_phci_path2devinfo(vdip, phci_path)) == NULL) { 5626 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5627 "phci dip doesn't exist. invalid path <%s>", 5628 s, phci_path)); 5629 retval = ENXIO; 5630 break; 5631 } 5632 5633 if ((pip = mdi_pi_find(pdip, NULL, paddr)) == NULL) { 5634 VHCI_DEBUG(1, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> " 5635 "pathinfo doesn't exist. invalid device addr", s)); 5636 retval = ENXIO; 5637 break; 5638 } 5639 5640 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> " 5641 "Calling MDI function to change device state", s)); 5642 5643 switch (cmd) { 5644 case SCSI_VHCI_PATH_ONLINE: 5645 retval = mdi_pi_online(pip, 0); 5646 break; 5647 5648 case SCSI_VHCI_PATH_OFFLINE: 5649 retval = mdi_pi_offline(pip, 0); 5650 break; 5651 5652 case SCSI_VHCI_PATH_STANDBY: 5653 retval = mdi_pi_standby(pip, 0); 5654 break; 5655 5656 case SCSI_VHCI_PATH_TEST: 5657 break; 5658 } 5659 break; 5660 } 5661 5662 case SCSI_VHCI_SWITCH_TO_CNTLR: 5663 { 5664 dev_info_t *cdip; 5665 struct scsi_device *devp; 5666 5667 /* Get the client device pathname */ 5668 if (ddi_copyin(piocsc->client, client_path, 5669 MAXPATHLEN, mode)) { 5670 VHCI_DEBUG(2, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> " 5671 "client_path copyin failed", s)); 5672 retval = EFAULT; 5673 break; 5674 } 5675 5676 /* Get the path class to which user wants to switch */ 5677 if (ddi_copyin(piocsc->class, paddr, MAXNAMELEN, mode)) { 5678 VHCI_DEBUG(2, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> " 5679 "controller_class copyin failed", s)); 5680 retval = EFAULT; 5681 break; 5682 } 5683 5684 /* Perform validity checks */ 5685 if ((cdip = mdi_client_path2devinfo(vdip, 5686 client_path)) == NULL) { 5687 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5688 "client dip doesn't exist. invalid path <%s>", 5689 s, client_path)); 5690 retval = ENXIO; 5691 break; 5692 } 5693 5694 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: Calling MDI func " 5695 "to switch controller")); 5696 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: client <%s> " 5697 "class <%s>", client_path, paddr)); 5698 5699 if (strcmp(paddr, PCLASS_PRIMARY) && 5700 strcmp(paddr, PCLASS_SECONDARY)) { 5701 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5702 "invalid path class <%s>", s, paddr)); 5703 retval = ENXIO; 5704 break; 5705 } 5706 5707 devp = ddi_get_driver_private(cdip); 5708 if (devp == NULL) { 5709 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5710 "invalid scsi device <%s>", s, client_path)); 5711 retval = ENXIO; 5712 break; 5713 } 5714 vlun = ADDR2VLUN(&devp->sd_address); 5715 ASSERT(vlun); 5716 5717 /* 5718 * Checking to see if device has only one pclass, PRIMARY. 5719 * If so this device doesn't support failovers. Assumed 5720 * that the devices with one pclass is PRIMARY, as thats the 5721 * case today. If this is not true and in future other 5722 * symmetric devices are supported with other pclass, this 5723 * IOCTL shall have to be overhauled anyways as now the only 5724 * arguments it accepts are PRIMARY and SECONDARY. 5725 */ 5726 fo = vlun->svl_fops; 5727 if (fo->sfo_pathclass_next(PCLASS_PRIMARY, &pclass, 5728 vlun->svl_fops_ctpriv)) { 5729 retval = ENOTSUP; 5730 break; 5731 } 5732 5733 VHCI_HOLD_LUN(vlun, VH_SLEEP, held); 5734 mutex_enter(&vlun->svl_mutex); 5735 if (vlun->svl_active_pclass != NULL) { 5736 if (strcmp(vlun->svl_active_pclass, paddr) == 0) { 5737 mutex_exit(&vlun->svl_mutex); 5738 retval = EALREADY; 5739 VHCI_RELEASE_LUN(vlun); 5740 break; 5741 } 5742 } 5743 mutex_exit(&vlun->svl_mutex); 5744 /* Call mdi function to cause a switch over */ 5745 retval = mdi_failover(vdip, cdip, MDI_FAILOVER_SYNC); 5746 if (retval == MDI_SUCCESS) { 5747 retval = 0; 5748 } else if (retval == MDI_BUSY) { 5749 retval = EBUSY; 5750 } else { 5751 retval = EIO; 5752 } 5753 VHCI_RELEASE_LUN(vlun); 5754 break; 5755 } 5756 5757 case SCSI_VHCI_PATH_ENABLE: 5758 case SCSI_VHCI_PATH_DISABLE: 5759 { 5760 dev_info_t *cdip, *pdip; 5761 5762 /* 5763 * Get client device path from user land 5764 */ 5765 if (vhci_ioc_get_client_path(pioc, client_path, mode, s)) { 5766 retval = EFAULT; 5767 break; 5768 } 5769 5770 /* 5771 * Get Phci device path from user land 5772 */ 5773 if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s)) { 5774 retval = EFAULT; 5775 break; 5776 } 5777 5778 /* 5779 * Get the devinfo for the Phci. 5780 */ 5781 if ((pdip = mdi_phci_path2devinfo(vdip, phci_path)) == NULL) { 5782 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5783 "phci dip doesn't exist. invalid path <%s>", 5784 s, phci_path)); 5785 retval = ENXIO; 5786 break; 5787 } 5788 5789 /* 5790 * If the client path is set to /scsi_vhci then we need 5791 * to do the operation on all the clients so set cdip to NULL. 5792 * Else, try to get the client dip. 5793 */ 5794 if (strcmp(client_path, "/scsi_vhci") == 0) { 5795 cdip = NULL; 5796 } else { 5797 if ((cdip = mdi_client_path2devinfo(vdip, 5798 client_path)) == NULL) { 5799 retval = ENXIO; 5800 VHCI_DEBUG(1, (CE_WARN, NULL, 5801 "!vhci_ioctl: ioctl <%s> client dip " 5802 "doesn't exist. invalid path <%s>", 5803 s, client_path)); 5804 break; 5805 } 5806 } 5807 5808 if (cmd == SCSI_VHCI_PATH_ENABLE) 5809 retval = mdi_pi_enable(cdip, pdip, USER_DISABLE); 5810 else 5811 retval = mdi_pi_disable(cdip, pdip, USER_DISABLE); 5812 5813 break; 5814 } 5815 5816 case SCSI_VHCI_GET_TARGET_LONGNAME: 5817 { 5818 uint_t pid = pioc->buf_elem; 5819 char *target_port; 5820 mod_hash_val_t hv; 5821 5822 /* targetmap lookup of 'target-port' by <pid> */ 5823 if (mod_hash_find(vhci_targetmap_bypid, 5824 (mod_hash_key_t)(uintptr_t)pid, &hv) != 0) { 5825 /* 5826 * NOTE: failure to find the mapping is OK for guid 5827 * based 'target-port' values. 5828 */ 5829 VHCI_DEBUG(3, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5830 "targetport mapping doesn't exist: pid %d", 5831 s, pid)); 5832 retval = ENXIO; 5833 break; 5834 } 5835 5836 /* copyout 'target-port' result */ 5837 target_port = (char *)hv; 5838 if (copyoutstr(target_port, pioc->addr, MAXNAMELEN, NULL)) { 5839 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5840 "targetport copyout failed: len: %d", 5841 s, (int)strlen(target_port))); 5842 retval = EFAULT; 5843 } 5844 break; 5845 } 5846 5847 #ifdef DEBUG 5848 case SCSI_VHCI_CONFIGURE_PHCI: 5849 { 5850 dev_info_t *pdip; 5851 5852 /* Get PHCI path and device address from user land */ 5853 if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s)) { 5854 retval = EFAULT; 5855 break; 5856 } 5857 5858 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> " 5859 "phci <%s>", s, phci_path)); 5860 5861 /* Get the PHCI dip */ 5862 if ((pdip = e_ddi_hold_devi_by_path(phci_path, 0)) == NULL) { 5863 VHCI_DEBUG(3, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5864 "phci dip doesn't exist. invalid path <%s>", 5865 s, phci_path)); 5866 retval = ENXIO; 5867 break; 5868 } 5869 5870 if (ndi_devi_config(pdip, 5871 NDI_DEVFS_CLEAN|NDI_DEVI_PERSIST) != NDI_SUCCESS) { 5872 retval = EIO; 5873 } 5874 5875 ddi_release_devi(pdip); 5876 break; 5877 } 5878 5879 case SCSI_VHCI_UNCONFIGURE_PHCI: 5880 { 5881 dev_info_t *pdip; 5882 5883 /* Get PHCI path and device address from user land */ 5884 if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s)) { 5885 retval = EFAULT; 5886 break; 5887 } 5888 5889 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> " 5890 "phci <%s>", s, phci_path)); 5891 5892 /* Get the PHCI dip */ 5893 if ((pdip = e_ddi_hold_devi_by_path(phci_path, 0)) == NULL) { 5894 VHCI_DEBUG(3, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> " 5895 "phci dip doesn't exist. invalid path <%s>", 5896 s, phci_path)); 5897 retval = ENXIO; 5898 break; 5899 } 5900 5901 if (ndi_devi_unconfig(pdip, 5902 NDI_DEVI_REMOVE|NDI_DEVFS_CLEAN) != NDI_SUCCESS) { 5903 retval = EBUSY; 5904 } 5905 5906 ddi_release_devi(pdip); 5907 break; 5908 } 5909 #endif 5910 } 5911 5912 end: 5913 /* Free the memory allocated above */ 5914 if (phci_path != NULL) { 5915 kmem_free(phci_path, MAXPATHLEN); 5916 } 5917 if (client_path != NULL) { 5918 kmem_free(client_path, MAXPATHLEN); 5919 } 5920 if (paddr != NULL) { 5921 kmem_free(paddr, MAXNAMELEN); 5922 } 5923 return (retval); 5924 } 5925 5926 /* 5927 * devctl IOCTL support for client device DR 5928 */ 5929 /* ARGSUSED */ 5930 int 5931 vhci_devctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 5932 int *rvalp) 5933 { 5934 dev_info_t *self; 5935 dev_info_t *child; 5936 scsi_hba_tran_t *hba; 5937 struct devctl_iocdata *dcp; 5938 struct scsi_vhci *vhci; 5939 int rv = 0; 5940 int retval = 0; 5941 scsi_vhci_priv_t *svp; 5942 mdi_pathinfo_t *pip; 5943 5944 if ((vhci = ddi_get_soft_state(vhci_softstate, 5945 MINOR2INST(getminor(dev)))) == NULL) 5946 return (ENXIO); 5947 5948 /* 5949 * check if :devctl minor device has been opened 5950 */ 5951 mutex_enter(&vhci->vhci_mutex); 5952 if ((vhci->vhci_state & VHCI_STATE_OPEN) == 0) { 5953 mutex_exit(&vhci->vhci_mutex); 5954 return (ENXIO); 5955 } 5956 mutex_exit(&vhci->vhci_mutex); 5957 5958 self = vhci->vhci_dip; 5959 hba = ddi_get_driver_private(self); 5960 if (hba == NULL) 5961 return (ENXIO); 5962 5963 /* 5964 * We can use the generic implementation for these ioctls 5965 */ 5966 switch (cmd) { 5967 case DEVCTL_DEVICE_GETSTATE: 5968 case DEVCTL_DEVICE_ONLINE: 5969 case DEVCTL_DEVICE_OFFLINE: 5970 case DEVCTL_DEVICE_REMOVE: 5971 case DEVCTL_BUS_GETSTATE: 5972 return (ndi_devctl_ioctl(self, cmd, arg, mode, 0)); 5973 } 5974 5975 /* 5976 * read devctl ioctl data 5977 */ 5978 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 5979 return (EFAULT); 5980 5981 switch (cmd) { 5982 5983 case DEVCTL_DEVICE_RESET: 5984 /* 5985 * lookup and hold child device 5986 */ 5987 if ((child = ndi_devi_find(self, ndi_dc_getname(dcp), 5988 ndi_dc_getaddr(dcp))) == NULL) { 5989 rv = ENXIO; 5990 break; 5991 } 5992 retval = mdi_select_path(child, NULL, 5993 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH), 5994 NULL, &pip); 5995 if ((retval != MDI_SUCCESS) || (pip == NULL)) { 5996 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioctl:" 5997 "Unable to get a path, dip 0x%p", (void *)child)); 5998 rv = ENXIO; 5999 break; 6000 } 6001 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip); 6002 if (vhci_recovery_reset(svp->svp_svl, 6003 &svp->svp_psd->sd_address, TRUE, 6004 VHCI_DEPTH_TARGET) == 0) { 6005 VHCI_DEBUG(1, (CE_NOTE, NULL, 6006 "!vhci_ioctl(pip:%p): " 6007 "reset failed\n", (void *)pip)); 6008 rv = ENXIO; 6009 } 6010 mdi_rele_path(pip); 6011 break; 6012 6013 case DEVCTL_BUS_QUIESCE: 6014 case DEVCTL_BUS_UNQUIESCE: 6015 case DEVCTL_BUS_RESET: 6016 case DEVCTL_BUS_RESETALL: 6017 #ifdef DEBUG 6018 case DEVCTL_BUS_CONFIGURE: 6019 case DEVCTL_BUS_UNCONFIGURE: 6020 #endif 6021 rv = ENOTSUP; 6022 break; 6023 6024 default: 6025 rv = ENOTTY; 6026 } /* end of outer switch */ 6027 6028 ndi_dc_freehdl(dcp); 6029 return (rv); 6030 } 6031 6032 /* 6033 * Routine to get the PHCI pathname from ioctl structures in userland 6034 */ 6035 /* ARGSUSED */ 6036 static int 6037 vhci_ioc_get_phci_path(sv_iocdata_t *pioc, caddr_t phci_path, 6038 int mode, caddr_t s) 6039 { 6040 int retval = 0; 6041 6042 if (ddi_copyin(pioc->phci, phci_path, MAXPATHLEN, mode)) { 6043 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioc_get_phci: ioctl <%s> " 6044 "phci_path copyin failed", s)); 6045 retval = EFAULT; 6046 } 6047 return (retval); 6048 6049 } 6050 6051 6052 /* 6053 * Routine to get the Client device pathname from ioctl structures in userland 6054 */ 6055 /* ARGSUSED */ 6056 static int 6057 vhci_ioc_get_client_path(sv_iocdata_t *pioc, caddr_t client_path, 6058 int mode, caddr_t s) 6059 { 6060 int retval = 0; 6061 6062 if (ddi_copyin(pioc->client, client_path, MAXPATHLEN, mode)) { 6063 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioc_get_client: " 6064 "ioctl <%s> client_path copyin failed", s)); 6065 retval = EFAULT; 6066 } 6067 return (retval); 6068 } 6069 6070 6071 /* 6072 * Routine to get physical device address from ioctl structure in userland 6073 */ 6074 /* ARGSUSED */ 6075 static int 6076 vhci_ioc_get_paddr(sv_iocdata_t *pioc, caddr_t paddr, int mode, caddr_t s) 6077 { 6078 int retval = 0; 6079 6080 if (ddi_copyin(pioc->addr, paddr, MAXNAMELEN, mode)) { 6081 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioc_get_paddr: " 6082 "ioctl <%s> device addr copyin failed", s)); 6083 retval = EFAULT; 6084 } 6085 return (retval); 6086 } 6087 6088 6089 /* 6090 * Routine to send client device pathname to userland. 6091 */ 6092 /* ARGSUSED */ 6093 static int 6094 vhci_ioc_send_client_path(caddr_t client_path, sv_iocdata_t *pioc, 6095 int mode, caddr_t s) 6096 { 6097 int retval = 0; 6098 6099 if (ddi_copyout(client_path, pioc->client, MAXPATHLEN, mode)) { 6100 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioc_send_client: " 6101 "ioctl <%s> client_path copyout failed", s)); 6102 retval = EFAULT; 6103 } 6104 return (retval); 6105 } 6106 6107 6108 /* 6109 * Routine to translated dev_info pointer (dip) to device pathname. 6110 */ 6111 static void 6112 vhci_ioc_devi_to_path(dev_info_t *dip, caddr_t path) 6113 { 6114 (void) ddi_pathname(dip, path); 6115 } 6116 6117 6118 /* 6119 * vhci_get_phci_path_list: 6120 * get information about devices associated with a 6121 * given PHCI device. 6122 * 6123 * Return Values: 6124 * path information elements 6125 */ 6126 int 6127 vhci_get_phci_path_list(dev_info_t *pdip, sv_path_info_t *pibuf, 6128 uint_t num_elems) 6129 { 6130 uint_t count, done; 6131 mdi_pathinfo_t *pip; 6132 sv_path_info_t *ret_pip; 6133 int status; 6134 size_t prop_size; 6135 int circular; 6136 6137 /* 6138 * Get the PHCI structure and retrieve the path information 6139 * from the GUID hash table. 6140 */ 6141 6142 ret_pip = pibuf; 6143 count = 0; 6144 6145 ndi_devi_enter(pdip, &circular); 6146 6147 done = (count >= num_elems); 6148 pip = mdi_get_next_client_path(pdip, NULL); 6149 while (pip && !done) { 6150 mdi_pi_lock(pip); 6151 (void) ddi_pathname(mdi_pi_get_phci(pip), 6152 ret_pip->device.ret_phci); 6153 (void) strcpy(ret_pip->ret_addr, mdi_pi_get_addr(pip)); 6154 (void) mdi_pi_get_state2(pip, &ret_pip->ret_state, 6155 &ret_pip->ret_ext_state); 6156 6157 status = mdi_prop_size(pip, &prop_size); 6158 if (status == MDI_SUCCESS && ret_pip->ret_prop.ret_buf_size) { 6159 *ret_pip->ret_prop.ret_buf_size = (uint_t)prop_size; 6160 } 6161 6162 #ifdef DEBUG 6163 if (status != MDI_SUCCESS) { 6164 VHCI_DEBUG(2, (CE_WARN, NULL, 6165 "!vhci_get_phci_path_list: " 6166 "phci <%s>, prop size failure 0x%x", 6167 ret_pip->device.ret_phci, status)); 6168 } 6169 #endif /* DEBUG */ 6170 6171 6172 if (status == MDI_SUCCESS && ret_pip->ret_prop.buf && 6173 prop_size && ret_pip->ret_prop.buf_size >= prop_size) { 6174 status = mdi_prop_pack(pip, 6175 &ret_pip->ret_prop.buf, 6176 ret_pip->ret_prop.buf_size); 6177 6178 #ifdef DEBUG 6179 if (status != MDI_SUCCESS) { 6180 VHCI_DEBUG(2, (CE_WARN, NULL, 6181 "!vhci_get_phci_path_list: " 6182 "phci <%s>, prop pack failure 0x%x", 6183 ret_pip->device.ret_phci, status)); 6184 } 6185 #endif /* DEBUG */ 6186 } 6187 6188 mdi_pi_unlock(pip); 6189 pip = mdi_get_next_client_path(pdip, pip); 6190 ret_pip++; 6191 count++; 6192 done = (count >= num_elems); 6193 } 6194 6195 ndi_devi_exit(pdip, circular); 6196 6197 return (MDI_SUCCESS); 6198 } 6199 6200 6201 /* 6202 * vhci_get_client_path_list: 6203 * get information about various paths associated with a 6204 * given client device. 6205 * 6206 * Return Values: 6207 * path information elements 6208 */ 6209 int 6210 vhci_get_client_path_list(dev_info_t *cdip, sv_path_info_t *pibuf, 6211 uint_t num_elems) 6212 { 6213 uint_t count, done; 6214 mdi_pathinfo_t *pip; 6215 sv_path_info_t *ret_pip; 6216 int status; 6217 size_t prop_size; 6218 int circular; 6219 6220 ret_pip = pibuf; 6221 count = 0; 6222 6223 ndi_devi_enter(cdip, &circular); 6224 6225 done = (count >= num_elems); 6226 pip = mdi_get_next_phci_path(cdip, NULL); 6227 while (pip && !done) { 6228 mdi_pi_lock(pip); 6229 (void) ddi_pathname(mdi_pi_get_phci(pip), 6230 ret_pip->device.ret_phci); 6231 (void) strcpy(ret_pip->ret_addr, mdi_pi_get_addr(pip)); 6232 (void) mdi_pi_get_state2(pip, &ret_pip->ret_state, 6233 &ret_pip->ret_ext_state); 6234 6235 status = mdi_prop_size(pip, &prop_size); 6236 if (status == MDI_SUCCESS && ret_pip->ret_prop.ret_buf_size) { 6237 *ret_pip->ret_prop.ret_buf_size = (uint_t)prop_size; 6238 } 6239 6240 #ifdef DEBUG 6241 if (status != MDI_SUCCESS) { 6242 VHCI_DEBUG(2, (CE_WARN, NULL, 6243 "!vhci_get_client_path_list: " 6244 "phci <%s>, prop size failure 0x%x", 6245 ret_pip->device.ret_phci, status)); 6246 } 6247 #endif /* DEBUG */ 6248 6249 6250 if (status == MDI_SUCCESS && ret_pip->ret_prop.buf && 6251 prop_size && ret_pip->ret_prop.buf_size >= prop_size) { 6252 status = mdi_prop_pack(pip, 6253 &ret_pip->ret_prop.buf, 6254 ret_pip->ret_prop.buf_size); 6255 6256 #ifdef DEBUG 6257 if (status != MDI_SUCCESS) { 6258 VHCI_DEBUG(2, (CE_WARN, NULL, 6259 "!vhci_get_client_path_list: " 6260 "phci <%s>, prop pack failure 0x%x", 6261 ret_pip->device.ret_phci, status)); 6262 } 6263 #endif /* DEBUG */ 6264 } 6265 6266 mdi_pi_unlock(pip); 6267 pip = mdi_get_next_phci_path(cdip, pip); 6268 ret_pip++; 6269 count++; 6270 done = (count >= num_elems); 6271 } 6272 6273 ndi_devi_exit(cdip, circular); 6274 6275 return (MDI_SUCCESS); 6276 } 6277 6278 6279 /* 6280 * Routine to get ioctl argument structure from userland. 6281 */ 6282 /* ARGSUSED */ 6283 static int 6284 vhci_get_iocdata(const void *data, sv_iocdata_t *pioc, int mode, caddr_t s) 6285 { 6286 int retval = 0; 6287 6288 #ifdef _MULTI_DATAMODEL 6289 switch (ddi_model_convert_from(mode & FMODELS)) { 6290 case DDI_MODEL_ILP32: 6291 { 6292 sv_iocdata32_t ioc32; 6293 6294 if (ddi_copyin(data, &ioc32, sizeof (ioc32), mode)) { 6295 retval = EFAULT; 6296 break; 6297 } 6298 pioc->client = (caddr_t)(uintptr_t)ioc32.client; 6299 pioc->phci = (caddr_t)(uintptr_t)ioc32.phci; 6300 pioc->addr = (caddr_t)(uintptr_t)ioc32.addr; 6301 pioc->buf_elem = (uint_t)ioc32.buf_elem; 6302 pioc->ret_buf = (sv_path_info_t *)(uintptr_t)ioc32.ret_buf; 6303 pioc->ret_elem = (uint_t *)(uintptr_t)ioc32.ret_elem; 6304 break; 6305 } 6306 6307 case DDI_MODEL_NONE: 6308 if (ddi_copyin(data, pioc, sizeof (*pioc), mode)) { 6309 retval = EFAULT; 6310 break; 6311 } 6312 break; 6313 } 6314 #else /* _MULTI_DATAMODEL */ 6315 if (ddi_copyin(data, pioc, sizeof (*pioc), mode)) { 6316 retval = EFAULT; 6317 } 6318 #endif /* _MULTI_DATAMODEL */ 6319 6320 #ifdef DEBUG 6321 if (retval) { 6322 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_get_ioc: cmd <%s> " 6323 "iocdata copyin failed", s)); 6324 } 6325 #endif 6326 6327 return (retval); 6328 } 6329 6330 6331 /* 6332 * Routine to get the ioctl argument for ioctl causing controller switchover. 6333 */ 6334 /* ARGSUSED */ 6335 static int 6336 vhci_get_iocswitchdata(const void *data, sv_switch_to_cntlr_iocdata_t *piocsc, 6337 int mode, caddr_t s) 6338 { 6339 int retval = 0; 6340 6341 #ifdef _MULTI_DATAMODEL 6342 switch (ddi_model_convert_from(mode & FMODELS)) { 6343 case DDI_MODEL_ILP32: 6344 { 6345 sv_switch_to_cntlr_iocdata32_t ioc32; 6346 6347 if (ddi_copyin(data, &ioc32, sizeof (ioc32), mode)) { 6348 retval = EFAULT; 6349 break; 6350 } 6351 piocsc->client = (caddr_t)(uintptr_t)ioc32.client; 6352 piocsc->class = (caddr_t)(uintptr_t)ioc32.class; 6353 break; 6354 } 6355 6356 case DDI_MODEL_NONE: 6357 if (ddi_copyin(data, piocsc, sizeof (*piocsc), mode)) { 6358 retval = EFAULT; 6359 } 6360 break; 6361 } 6362 #else /* _MULTI_DATAMODEL */ 6363 if (ddi_copyin(data, piocsc, sizeof (*piocsc), mode)) { 6364 retval = EFAULT; 6365 } 6366 #endif /* _MULTI_DATAMODEL */ 6367 6368 #ifdef DEBUG 6369 if (retval) { 6370 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_get_ioc: cmd <%s> " 6371 "switch_to_cntlr_iocdata copyin failed", s)); 6372 } 6373 #endif 6374 6375 return (retval); 6376 } 6377 6378 6379 /* 6380 * Routine to allocate memory for the path information structures. 6381 * It allocates two chunks of memory - one for keeping userland 6382 * pointers/values for path information and path properties, second for 6383 * keeping allocating kernel memory for path properties. These path 6384 * properties are finally copied to userland. 6385 */ 6386 /* ARGSUSED */ 6387 static int 6388 vhci_ioc_alloc_pathinfo(sv_path_info_t **upibuf, sv_path_info_t **kpibuf, 6389 uint_t num_paths, sv_iocdata_t *pioc, int mode, caddr_t s) 6390 { 6391 sv_path_info_t *pi; 6392 uint_t bufsize; 6393 int retval = 0; 6394 int index; 6395 6396 /* Allocate memory */ 6397 *upibuf = (sv_path_info_t *) 6398 kmem_zalloc(sizeof (sv_path_info_t) * num_paths, KM_SLEEP); 6399 ASSERT(*upibuf != NULL); 6400 *kpibuf = (sv_path_info_t *) 6401 kmem_zalloc(sizeof (sv_path_info_t) * num_paths, KM_SLEEP); 6402 ASSERT(*kpibuf != NULL); 6403 6404 /* 6405 * Get the path info structure from the user space. 6406 * We are interested in the following fields: 6407 * - user size of buffer for per path properties. 6408 * - user address of buffer for path info properties. 6409 * - user pointer for returning actual buffer size 6410 * Keep these fields in the 'upibuf' structures. 6411 * Allocate buffer for per path info properties in kernel 6412 * structure ('kpibuf'). 6413 * Size of these buffers will be equal to the size of buffers 6414 * in the user space. 6415 */ 6416 #ifdef _MULTI_DATAMODEL 6417 switch (ddi_model_convert_from(mode & FMODELS)) { 6418 case DDI_MODEL_ILP32: 6419 { 6420 sv_path_info32_t *src; 6421 sv_path_info32_t pi32; 6422 6423 src = (sv_path_info32_t *)pioc->ret_buf; 6424 pi = (sv_path_info_t *)*upibuf; 6425 for (index = 0; index < num_paths; index++, src++, pi++) { 6426 if (ddi_copyin(src, &pi32, sizeof (pi32), mode)) { 6427 retval = EFAULT; 6428 break; 6429 } 6430 6431 pi->ret_prop.buf_size = 6432 (uint_t)pi32.ret_prop.buf_size; 6433 pi->ret_prop.ret_buf_size = 6434 (uint_t *)(uintptr_t)pi32.ret_prop.ret_buf_size; 6435 pi->ret_prop.buf = 6436 (caddr_t)(uintptr_t)pi32.ret_prop.buf; 6437 } 6438 break; 6439 } 6440 6441 case DDI_MODEL_NONE: 6442 if (ddi_copyin(pioc->ret_buf, *upibuf, 6443 sizeof (sv_path_info_t) * num_paths, mode)) { 6444 retval = EFAULT; 6445 } 6446 break; 6447 } 6448 #else /* _MULTI_DATAMODEL */ 6449 if (ddi_copyin(pioc->ret_buf, *upibuf, 6450 sizeof (sv_path_info_t) * num_paths, mode)) { 6451 retval = EFAULT; 6452 } 6453 #endif /* _MULTI_DATAMODEL */ 6454 6455 if (retval != 0) { 6456 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_alloc_path_info: " 6457 "ioctl <%s> normal: path_info copyin failed", s)); 6458 kmem_free(*upibuf, sizeof (sv_path_info_t) * num_paths); 6459 kmem_free(*kpibuf, sizeof (sv_path_info_t) * num_paths); 6460 *upibuf = NULL; 6461 *kpibuf = NULL; 6462 return (retval); 6463 } 6464 6465 /* 6466 * Allocate memory for per path properties. 6467 */ 6468 for (index = 0, pi = *kpibuf; index < num_paths; index++, pi++) { 6469 bufsize = (*upibuf)[index].ret_prop.buf_size; 6470 6471 if (bufsize && bufsize <= SV_PROP_MAX_BUF_SIZE) { 6472 pi->ret_prop.buf_size = bufsize; 6473 pi->ret_prop.buf = (caddr_t) 6474 kmem_zalloc(bufsize, KM_SLEEP); 6475 ASSERT(pi->ret_prop.buf != NULL); 6476 } else { 6477 pi->ret_prop.buf_size = 0; 6478 pi->ret_prop.buf = NULL; 6479 } 6480 6481 if ((*upibuf)[index].ret_prop.ret_buf_size != NULL) { 6482 pi->ret_prop.ret_buf_size = (uint_t *)kmem_zalloc( 6483 sizeof (*pi->ret_prop.ret_buf_size), KM_SLEEP); 6484 ASSERT(pi->ret_prop.ret_buf_size != NULL); 6485 } else { 6486 pi->ret_prop.ret_buf_size = NULL; 6487 } 6488 } 6489 6490 return (0); 6491 } 6492 6493 6494 /* 6495 * Routine to free memory for the path information structures. 6496 * This is the memory which was allocated earlier. 6497 */ 6498 /* ARGSUSED */ 6499 static void 6500 vhci_ioc_free_pathinfo(sv_path_info_t *upibuf, sv_path_info_t *kpibuf, 6501 uint_t num_paths) 6502 { 6503 sv_path_info_t *pi; 6504 int index; 6505 6506 /* Free memory for per path properties */ 6507 for (index = 0, pi = kpibuf; index < num_paths; index++, pi++) { 6508 if (pi->ret_prop.ret_buf_size != NULL) { 6509 kmem_free(pi->ret_prop.ret_buf_size, 6510 sizeof (*pi->ret_prop.ret_buf_size)); 6511 } 6512 6513 if (pi->ret_prop.buf != NULL) { 6514 kmem_free(pi->ret_prop.buf, pi->ret_prop.buf_size); 6515 } 6516 } 6517 6518 /* Free memory for path info structures */ 6519 kmem_free(upibuf, sizeof (sv_path_info_t) * num_paths); 6520 kmem_free(kpibuf, sizeof (sv_path_info_t) * num_paths); 6521 } 6522 6523 6524 /* 6525 * Routine to copy path information and path properties to userland. 6526 */ 6527 /* ARGSUSED */ 6528 static int 6529 vhci_ioc_send_pathinfo(sv_path_info_t *upibuf, sv_path_info_t *kpibuf, 6530 uint_t num_paths, sv_iocdata_t *pioc, int mode, caddr_t s) 6531 { 6532 int retval = 0, index; 6533 sv_path_info_t *upi_ptr; 6534 sv_path_info32_t *upi32_ptr; 6535 6536 #ifdef _MULTI_DATAMODEL 6537 switch (ddi_model_convert_from(mode & FMODELS)) { 6538 case DDI_MODEL_ILP32: 6539 goto copy_32bit; 6540 6541 case DDI_MODEL_NONE: 6542 goto copy_normal; 6543 } 6544 #else /* _MULTI_DATAMODEL */ 6545 6546 goto copy_normal; 6547 6548 #endif /* _MULTI_DATAMODEL */ 6549 6550 copy_normal: 6551 6552 /* 6553 * Copy path information and path properties to user land. 6554 * Pointer fields inside the path property structure were 6555 * saved in the 'upibuf' structure earlier. 6556 */ 6557 upi_ptr = pioc->ret_buf; 6558 for (index = 0; index < num_paths; index++) { 6559 if (ddi_copyout(kpibuf[index].device.ret_ct, 6560 upi_ptr[index].device.ret_ct, MAXPATHLEN, mode)) { 6561 retval = EFAULT; 6562 break; 6563 } 6564 6565 if (ddi_copyout(kpibuf[index].ret_addr, 6566 upi_ptr[index].ret_addr, MAXNAMELEN, mode)) { 6567 retval = EFAULT; 6568 break; 6569 } 6570 6571 if (ddi_copyout(&kpibuf[index].ret_state, 6572 &upi_ptr[index].ret_state, sizeof (kpibuf[index].ret_state), 6573 mode)) { 6574 retval = EFAULT; 6575 break; 6576 } 6577 6578 if (ddi_copyout(&kpibuf[index].ret_ext_state, 6579 &upi_ptr[index].ret_ext_state, 6580 sizeof (kpibuf[index].ret_ext_state), mode)) { 6581 retval = EFAULT; 6582 break; 6583 } 6584 6585 if ((kpibuf[index].ret_prop.ret_buf_size != NULL) && 6586 ddi_copyout(kpibuf[index].ret_prop.ret_buf_size, 6587 upibuf[index].ret_prop.ret_buf_size, 6588 sizeof (*upibuf[index].ret_prop.ret_buf_size), mode)) { 6589 retval = EFAULT; 6590 break; 6591 } 6592 6593 if ((kpibuf[index].ret_prop.buf != NULL) && 6594 ddi_copyout(kpibuf[index].ret_prop.buf, 6595 upibuf[index].ret_prop.buf, 6596 upibuf[index].ret_prop.buf_size, mode)) { 6597 retval = EFAULT; 6598 break; 6599 } 6600 } 6601 6602 #ifdef DEBUG 6603 if (retval) { 6604 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_get_ioc: ioctl <%s> " 6605 "normal: path_info copyout failed", s)); 6606 } 6607 #endif 6608 6609 return (retval); 6610 6611 copy_32bit: 6612 /* 6613 * Copy path information and path properties to user land. 6614 * Pointer fields inside the path property structure were 6615 * saved in the 'upibuf' structure earlier. 6616 */ 6617 upi32_ptr = (sv_path_info32_t *)pioc->ret_buf; 6618 for (index = 0; index < num_paths; index++) { 6619 if (ddi_copyout(kpibuf[index].device.ret_ct, 6620 upi32_ptr[index].device.ret_ct, MAXPATHLEN, mode)) { 6621 retval = EFAULT; 6622 break; 6623 } 6624 6625 if (ddi_copyout(kpibuf[index].ret_addr, 6626 upi32_ptr[index].ret_addr, MAXNAMELEN, mode)) { 6627 retval = EFAULT; 6628 break; 6629 } 6630 6631 if (ddi_copyout(&kpibuf[index].ret_state, 6632 &upi32_ptr[index].ret_state, 6633 sizeof (kpibuf[index].ret_state), mode)) { 6634 retval = EFAULT; 6635 break; 6636 } 6637 6638 if (ddi_copyout(&kpibuf[index].ret_ext_state, 6639 &upi32_ptr[index].ret_ext_state, 6640 sizeof (kpibuf[index].ret_ext_state), mode)) { 6641 retval = EFAULT; 6642 break; 6643 } 6644 if ((kpibuf[index].ret_prop.ret_buf_size != NULL) && 6645 ddi_copyout(kpibuf[index].ret_prop.ret_buf_size, 6646 upibuf[index].ret_prop.ret_buf_size, 6647 sizeof (*upibuf[index].ret_prop.ret_buf_size), mode)) { 6648 retval = EFAULT; 6649 break; 6650 } 6651 6652 if ((kpibuf[index].ret_prop.buf != NULL) && 6653 ddi_copyout(kpibuf[index].ret_prop.buf, 6654 upibuf[index].ret_prop.buf, 6655 upibuf[index].ret_prop.buf_size, mode)) { 6656 retval = EFAULT; 6657 break; 6658 } 6659 } 6660 6661 #ifdef DEBUG 6662 if (retval) { 6663 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_get_ioc: ioctl <%s> " 6664 "normal: path_info copyout failed", s)); 6665 } 6666 #endif 6667 6668 return (retval); 6669 } 6670 6671 6672 /* 6673 * vhci_failover() 6674 * This routine expects VHCI_HOLD_LUN before being invoked. It can be invoked 6675 * as MDI_FAILOVER_ASYNC or MDI_FAILOVER_SYNC. For Asynchronous failovers 6676 * this routine shall VHCI_RELEASE_LUN on exiting. For synchronous failovers 6677 * it is the callers responsibility to release lun. 6678 */ 6679 6680 /* ARGSUSED */ 6681 static int 6682 vhci_failover(dev_info_t *vdip, dev_info_t *cdip, int flags) 6683 { 6684 char *guid; 6685 scsi_vhci_lun_t *vlun = NULL; 6686 struct scsi_vhci *vhci; 6687 mdi_pathinfo_t *pip, *npip; 6688 char *s_pclass, *pclass1, *pclass2, *pclass; 6689 char active_pclass_copy[255], *active_pclass_ptr; 6690 char *ptr1, *ptr2; 6691 mdi_pathinfo_state_t pi_state; 6692 uint32_t pi_ext_state; 6693 scsi_vhci_priv_t *svp; 6694 struct scsi_device *sd; 6695 struct scsi_failover_ops *sfo; 6696 int sps; /* mdi_select_path() status */ 6697 int activation_done = 0; 6698 int rval, retval = MDI_FAILURE; 6699 int reserve_pending, check_condition, UA_condition; 6700 struct scsi_pkt *pkt; 6701 struct buf *bp; 6702 6703 vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip)); 6704 sd = ddi_get_driver_private(cdip); 6705 vlun = ADDR2VLUN(&sd->sd_address); 6706 ASSERT(vlun != 0); 6707 ASSERT(VHCI_LUN_IS_HELD(vlun)); 6708 guid = vlun->svl_lun_wwn; 6709 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(1): guid %s\n", guid)); 6710 vhci_log(CE_NOTE, vdip, "!Initiating failover for device %s " 6711 "(GUID %s)", ddi_node_name(cdip), guid); 6712 6713 /* 6714 * Lets maintain a local copy of the vlun->svl_active_pclass 6715 * for the rest of the processing. Accessing the field 6716 * directly in the loop below causes loop logic to break 6717 * especially when the field gets updated by other threads 6718 * update path status etc and causes 'paths are not currently 6719 * available' condition to be declared prematurely. 6720 */ 6721 mutex_enter(&vlun->svl_mutex); 6722 if (vlun->svl_active_pclass != NULL) { 6723 (void) strlcpy(active_pclass_copy, vlun->svl_active_pclass, 6724 sizeof (active_pclass_copy)); 6725 active_pclass_ptr = &active_pclass_copy[0]; 6726 mutex_exit(&vlun->svl_mutex); 6727 if (vhci_quiesce_paths(vdip, cdip, vlun, guid, 6728 active_pclass_ptr) != 0) { 6729 retval = MDI_FAILURE; 6730 } 6731 } else { 6732 /* 6733 * can happen only when the available path to device 6734 * discovered is a STANDBY path. 6735 */ 6736 mutex_exit(&vlun->svl_mutex); 6737 active_pclass_copy[0] = '\0'; 6738 active_pclass_ptr = NULL; 6739 } 6740 6741 sfo = vlun->svl_fops; 6742 ASSERT(sfo != NULL); 6743 pclass1 = s_pclass = active_pclass_ptr; 6744 VHCI_DEBUG(1, (CE_NOTE, NULL, "!(%s)failing over from %s\n", guid, 6745 (s_pclass == NULL ? "<none>" : s_pclass))); 6746 6747 next_pathclass: 6748 6749 rval = sfo->sfo_pathclass_next(pclass1, &pclass2, 6750 vlun->svl_fops_ctpriv); 6751 if (rval == ENOENT) { 6752 if (s_pclass == NULL) { 6753 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(4)(%s): " 6754 "failed, no more pathclasses\n", guid)); 6755 goto done; 6756 } else { 6757 (void) sfo->sfo_pathclass_next(NULL, &pclass2, 6758 vlun->svl_fops_ctpriv); 6759 } 6760 } else if (rval == EINVAL) { 6761 vhci_log(CE_NOTE, vdip, "!Failover operation failed for " 6762 "device %s (GUID %s): Invalid path-class %s", 6763 ddi_node_name(cdip), guid, 6764 ((pclass1 == NULL) ? "<none>" : pclass1)); 6765 goto done; 6766 } 6767 if ((s_pclass != NULL) && (strcmp(pclass2, s_pclass) == 0)) { 6768 /* 6769 * paths are not currently available 6770 */ 6771 vhci_log(CE_NOTE, vdip, "!Failover path currently unavailable" 6772 " for device %s (GUID %s)", 6773 ddi_node_name(cdip), guid); 6774 goto done; 6775 } 6776 pip = npip = NULL; 6777 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(5.2)(%s): considering " 6778 "%s as failover destination\n", guid, pclass2)); 6779 sps = mdi_select_path(cdip, NULL, MDI_SELECT_STANDBY_PATH, NULL, &npip); 6780 if ((npip == NULL) || (sps != MDI_SUCCESS)) { 6781 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(%s): no " 6782 "STANDBY paths found (status:%x)!\n", guid, sps)); 6783 pclass1 = pclass2; 6784 goto next_pathclass; 6785 } 6786 do { 6787 pclass = NULL; 6788 if ((mdi_prop_lookup_string(npip, "path-class", 6789 &pclass) != MDI_SUCCESS) || (strcmp(pclass2, 6790 pclass) != 0)) { 6791 VHCI_DEBUG(1, (CE_NOTE, NULL, 6792 "!vhci_failover(5.5)(%s): skipping path " 6793 "%p(%s)...\n", guid, (void *)npip, pclass)); 6794 pip = npip; 6795 sps = mdi_select_path(cdip, NULL, 6796 MDI_SELECT_STANDBY_PATH, pip, &npip); 6797 mdi_rele_path(pip); 6798 (void) mdi_prop_free(pclass); 6799 continue; 6800 } 6801 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(npip); 6802 6803 /* 6804 * Issue READ at non-zer block on this STANDBY path. 6805 * Purple returns 6806 * 1. RESERVATION_CONFLICT if reservation is pending 6807 * 2. POR check condition if it reset happened. 6808 * 2. failover Check Conditions if one is already in progress. 6809 */ 6810 reserve_pending = 0; 6811 check_condition = 0; 6812 UA_condition = 0; 6813 6814 bp = scsi_alloc_consistent_buf(&svp->svp_psd->sd_address, 6815 (struct buf *)NULL, DEV_BSIZE, B_READ, NULL, NULL); 6816 if (!bp) { 6817 VHCI_DEBUG(1, (CE_NOTE, NULL, 6818 "vhci_failover !No resources (buf)\n")); 6819 mdi_rele_path(npip); 6820 goto done; 6821 } 6822 pkt = scsi_init_pkt(&svp->svp_psd->sd_address, NULL, bp, 6823 CDB_GROUP1, sizeof (struct scsi_arq_status), 0, 6824 PKT_CONSISTENT, NULL, NULL); 6825 if (pkt) { 6826 (void) scsi_setup_cdb((union scsi_cdb *)(uintptr_t) 6827 pkt->pkt_cdbp, SCMD_READ, 1, 1, 0); 6828 pkt->pkt_flags = FLAG_NOINTR; 6829 check_path_again: 6830 pkt->pkt_path_instance = mdi_pi_get_path_instance(npip); 6831 pkt->pkt_time = 3*30; 6832 6833 if (scsi_transport(pkt) == TRAN_ACCEPT) { 6834 switch (pkt->pkt_reason) { 6835 case CMD_CMPLT: 6836 switch (SCBP_C(pkt)) { 6837 case STATUS_GOOD: 6838 /* Already failed over */ 6839 activation_done = 1; 6840 break; 6841 case STATUS_RESERVATION_CONFLICT: 6842 reserve_pending = 1; 6843 break; 6844 case STATUS_CHECK: 6845 check_condition = 1; 6846 break; 6847 } 6848 } 6849 } 6850 if (check_condition && 6851 (pkt->pkt_state & STATE_ARQ_DONE)) { 6852 uint8_t *sns, skey, asc, ascq; 6853 sns = (uint8_t *) 6854 &(((struct scsi_arq_status *)(uintptr_t) 6855 (pkt->pkt_scbp))->sts_sensedata); 6856 skey = scsi_sense_key(sns); 6857 asc = scsi_sense_asc(sns); 6858 ascq = scsi_sense_ascq(sns); 6859 if (skey == KEY_UNIT_ATTENTION && 6860 asc == 0x29) { 6861 /* Already failed over */ 6862 VHCI_DEBUG(1, (CE_NOTE, NULL, 6863 "!vhci_failover(7)(%s): " 6864 "path 0x%p POR UA condition\n", 6865 guid, (void *)npip)); 6866 if (UA_condition == 0) { 6867 UA_condition = 1; 6868 goto check_path_again; 6869 } 6870 } else { 6871 activation_done = 0; 6872 VHCI_DEBUG(1, (CE_NOTE, NULL, 6873 "!vhci_failover(%s): path 0x%p " 6874 "unhandled chkcond %x %x %x\n", 6875 guid, (void *)npip, skey, 6876 asc, ascq)); 6877 } 6878 } 6879 scsi_destroy_pkt(pkt); 6880 } 6881 scsi_free_consistent_buf(bp); 6882 6883 if (activation_done) { 6884 mdi_rele_path(npip); 6885 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(7)(%s): " 6886 "path 0x%p already failedover\n", guid, 6887 (void *)npip)); 6888 break; 6889 } 6890 if (reserve_pending && (vlun->svl_xlf_capable == 0)) { 6891 (void) vhci_recovery_reset(vlun, 6892 &svp->svp_psd->sd_address, 6893 FALSE, VHCI_DEPTH_ALL); 6894 } 6895 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(6)(%s): " 6896 "activating path 0x%p(psd:%p)\n", guid, (void *)npip, 6897 (void *)svp->svp_psd)); 6898 if (sfo->sfo_path_activate(svp->svp_psd, pclass2, 6899 vlun->svl_fops_ctpriv) == 0) { 6900 activation_done = 1; 6901 mdi_rele_path(npip); 6902 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(7)(%s): " 6903 "path 0x%p successfully activated\n", guid, 6904 (void *)npip)); 6905 break; 6906 } 6907 pip = npip; 6908 sps = mdi_select_path(cdip, NULL, MDI_SELECT_STANDBY_PATH, 6909 pip, &npip); 6910 mdi_rele_path(pip); 6911 } while ((npip != NULL) && (sps == MDI_SUCCESS)); 6912 if (activation_done == 0) { 6913 pclass1 = pclass2; 6914 goto next_pathclass; 6915 } 6916 6917 /* 6918 * if we are here, we have succeeded in activating path npip of 6919 * pathclass pclass2; let us validate all paths of pclass2 by 6920 * "ping"-ing each one and mark the good ones ONLINE 6921 * Also, set the state of the paths belonging to the previously 6922 * active pathclass to STANDBY 6923 */ 6924 pip = npip = NULL; 6925 sps = mdi_select_path(cdip, NULL, (MDI_SELECT_ONLINE_PATH | 6926 MDI_SELECT_STANDBY_PATH | MDI_SELECT_USER_DISABLE_PATH), 6927 NULL, &npip); 6928 if (npip == NULL || sps != MDI_SUCCESS) { 6929 VHCI_DEBUG(1, (CE_NOTE, NULL, "!Failover operation failed for " 6930 "device %s (GUID %s): paths may be busy\n", 6931 ddi_node_name(cdip), guid)); 6932 goto done; 6933 } 6934 do { 6935 (void) mdi_pi_get_state2(npip, &pi_state, &pi_ext_state); 6936 if (mdi_prop_lookup_string(npip, "path-class", &pclass) 6937 != MDI_SUCCESS) { 6938 pip = npip; 6939 sps = mdi_select_path(cdip, NULL, 6940 (MDI_SELECT_ONLINE_PATH | 6941 MDI_SELECT_STANDBY_PATH | 6942 MDI_SELECT_USER_DISABLE_PATH), 6943 pip, &npip); 6944 mdi_rele_path(pip); 6945 continue; 6946 } 6947 if (strcmp(pclass, pclass2) == 0) { 6948 if (pi_state == MDI_PATHINFO_STATE_STANDBY) { 6949 svp = (scsi_vhci_priv_t *) 6950 mdi_pi_get_vhci_private(npip); 6951 VHCI_DEBUG(1, (CE_NOTE, NULL, 6952 "!vhci_failover(8)(%s): " 6953 "pinging path 0x%p\n", 6954 guid, (void *)npip)); 6955 if (sfo->sfo_path_ping(svp->svp_psd, 6956 vlun->svl_fops_ctpriv) == 1) { 6957 mdi_pi_set_state(npip, 6958 MDI_PATHINFO_STATE_ONLINE); 6959 VHCI_DEBUG(1, (CE_NOTE, NULL, 6960 "!vhci_failover(9)(%s): " 6961 "path 0x%p ping successful, " 6962 "marked online\n", guid, 6963 (void *)npip)); 6964 MDI_PI_ERRSTAT(npip, MDI_PI_FAILTO); 6965 } 6966 } 6967 } else if ((s_pclass != NULL) && (strcmp(pclass, s_pclass) 6968 == 0)) { 6969 if (pi_state == MDI_PATHINFO_STATE_ONLINE) { 6970 mdi_pi_set_state(npip, 6971 MDI_PATHINFO_STATE_STANDBY); 6972 VHCI_DEBUG(1, (CE_NOTE, NULL, 6973 "!vhci_failover(10)(%s): path 0x%p marked " 6974 "STANDBY\n", guid, (void *)npip)); 6975 MDI_PI_ERRSTAT(npip, MDI_PI_FAILFROM); 6976 } 6977 } 6978 (void) mdi_prop_free(pclass); 6979 pip = npip; 6980 sps = mdi_select_path(cdip, NULL, (MDI_SELECT_ONLINE_PATH | 6981 MDI_SELECT_STANDBY_PATH|MDI_SELECT_USER_DISABLE_PATH), 6982 pip, &npip); 6983 mdi_rele_path(pip); 6984 } while ((npip != NULL) && (sps == MDI_SUCCESS)); 6985 6986 /* 6987 * Update the AccessState of related MP-API TPGs 6988 */ 6989 (void) vhci_mpapi_update_tpg_acc_state_for_lu(vhci, vlun); 6990 6991 vhci_log(CE_NOTE, vdip, "!Failover operation completed successfully " 6992 "for device %s (GUID %s): failed over from %s to %s", 6993 ddi_node_name(cdip), guid, ((s_pclass == NULL) ? "<none>" : 6994 s_pclass), pclass2); 6995 ptr1 = kmem_alloc(strlen(pclass2)+1, KM_SLEEP); 6996 (void) strlcpy(ptr1, pclass2, (strlen(pclass2)+1)); 6997 mutex_enter(&vlun->svl_mutex); 6998 ptr2 = vlun->svl_active_pclass; 6999 vlun->svl_active_pclass = ptr1; 7000 mutex_exit(&vlun->svl_mutex); 7001 if (ptr2) { 7002 kmem_free(ptr2, strlen(ptr2)+1); 7003 } 7004 mutex_enter(&vhci->vhci_mutex); 7005 scsi_hba_reset_notify_callback(&vhci->vhci_mutex, 7006 &vhci->vhci_reset_notify_listf); 7007 /* All reservations are cleared upon these resets. */ 7008 vlun->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG; 7009 mutex_exit(&vhci->vhci_mutex); 7010 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(11): DONE! Active " 7011 "pathclass for %s is now %s\n", guid, pclass2)); 7012 retval = MDI_SUCCESS; 7013 7014 done: 7015 if (flags == MDI_FAILOVER_ASYNC) { 7016 VHCI_RELEASE_LUN(vlun); 7017 VHCI_DEBUG(6, (CE_NOTE, NULL, "!vhci_failover(12): DONE! " 7018 "releasing lun, as failover was ASYNC\n")); 7019 } else { 7020 VHCI_DEBUG(6, (CE_NOTE, NULL, "!vhci_failover(12): DONE! " 7021 "NOT releasing lun, as failover was SYNC\n")); 7022 } 7023 return (retval); 7024 } 7025 7026 /* 7027 * vhci_client_attached is called after the successful attach of a 7028 * client devinfo node. 7029 */ 7030 static void 7031 vhci_client_attached(dev_info_t *cdip) 7032 { 7033 mdi_pathinfo_t *pip; 7034 int circular; 7035 7036 /* 7037 * At this point the client has attached and it's instance number is 7038 * valid, so we can set up kstats. We need to do this here because it 7039 * is possible for paths to go online prior to client attach, in which 7040 * case the call to vhci_kstat_create_pathinfo in vhci_pathinfo_online 7041 * was a noop. 7042 */ 7043 ndi_devi_enter(cdip, &circular); 7044 for (pip = mdi_get_next_phci_path(cdip, NULL); pip; 7045 pip = mdi_get_next_phci_path(cdip, pip)) 7046 vhci_kstat_create_pathinfo(pip); 7047 ndi_devi_exit(cdip, circular); 7048 } 7049 7050 /* 7051 * quiesce all of the online paths 7052 */ 7053 static int 7054 vhci_quiesce_paths(dev_info_t *vdip, dev_info_t *cdip, scsi_vhci_lun_t *vlun, 7055 char *guid, char *active_pclass_ptr) 7056 { 7057 scsi_vhci_priv_t *svp; 7058 char *s_pclass = NULL; 7059 mdi_pathinfo_t *npip, *pip; 7060 int sps; 7061 7062 /* quiesce currently active paths */ 7063 s_pclass = NULL; 7064 pip = npip = NULL; 7065 sps = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH, NULL, &npip); 7066 if ((npip == NULL) || (sps != MDI_SUCCESS)) { 7067 return (1); 7068 } 7069 do { 7070 if (mdi_prop_lookup_string(npip, "path-class", 7071 &s_pclass) != MDI_SUCCESS) { 7072 mdi_rele_path(npip); 7073 vhci_log(CE_NOTE, vdip, "!Failover operation failed " 7074 "for device %s (GUID %s) due to an internal " 7075 "error", ddi_node_name(cdip), guid); 7076 return (1); 7077 } 7078 if (strcmp(s_pclass, active_pclass_ptr) == 0) { 7079 /* 7080 * quiesce path. Free s_pclass since 7081 * we don't need it anymore 7082 */ 7083 VHCI_DEBUG(1, (CE_NOTE, NULL, 7084 "!vhci_failover(2)(%s): failing over " 7085 "from %s; quiescing path %p\n", 7086 guid, s_pclass, (void *)npip)); 7087 (void) mdi_prop_free(s_pclass); 7088 svp = (scsi_vhci_priv_t *) 7089 mdi_pi_get_vhci_private(npip); 7090 if (svp == NULL) { 7091 VHCI_DEBUG(1, (CE_NOTE, NULL, 7092 "!vhci_failover(2.5)(%s): no " 7093 "client priv! %p offlined?\n", 7094 guid, (void *)npip)); 7095 pip = npip; 7096 sps = mdi_select_path(cdip, NULL, 7097 MDI_SELECT_ONLINE_PATH, pip, &npip); 7098 mdi_rele_path(pip); 7099 continue; 7100 } 7101 if (scsi_abort(&svp->svp_psd->sd_address, NULL) 7102 == 0) { 7103 (void) vhci_recovery_reset(vlun, 7104 &svp->svp_psd->sd_address, FALSE, 7105 VHCI_DEPTH_TARGET); 7106 } 7107 mutex_enter(&svp->svp_mutex); 7108 if (svp->svp_cmds == 0) { 7109 VHCI_DEBUG(1, (CE_NOTE, NULL, 7110 "!vhci_failover(3)(%s):" 7111 "quiesced path %p\n", guid, (void *)npip)); 7112 } else { 7113 while (svp->svp_cmds != 0) { 7114 cv_wait(&svp->svp_cv, &svp->svp_mutex); 7115 VHCI_DEBUG(1, (CE_NOTE, NULL, 7116 "!vhci_failover(3.cv)(%s):" 7117 "quiesced path %p\n", guid, 7118 (void *)npip)); 7119 } 7120 } 7121 mutex_exit(&svp->svp_mutex); 7122 } else { 7123 /* 7124 * make sure we freeup the memory 7125 */ 7126 (void) mdi_prop_free(s_pclass); 7127 } 7128 pip = npip; 7129 sps = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH, 7130 pip, &npip); 7131 mdi_rele_path(pip); 7132 } while ((npip != NULL) && (sps == MDI_SUCCESS)); 7133 return (0); 7134 } 7135 7136 static struct scsi_vhci_lun * 7137 vhci_lun_lookup(dev_info_t *tgt_dip) 7138 { 7139 return ((struct scsi_vhci_lun *) 7140 mdi_client_get_vhci_private(tgt_dip)); 7141 } 7142 7143 static struct scsi_vhci_lun * 7144 vhci_lun_lookup_alloc(dev_info_t *tgt_dip, char *guid, int *didalloc) 7145 { 7146 struct scsi_vhci_lun *svl; 7147 7148 if (svl = vhci_lun_lookup(tgt_dip)) { 7149 return (svl); 7150 } 7151 7152 svl = kmem_zalloc(sizeof (*svl), KM_SLEEP); 7153 svl->svl_lun_wwn = kmem_zalloc(strlen(guid)+1, KM_SLEEP); 7154 (void) strcpy(svl->svl_lun_wwn, guid); 7155 mutex_init(&svl->svl_mutex, NULL, MUTEX_DRIVER, NULL); 7156 cv_init(&svl->svl_cv, NULL, CV_DRIVER, NULL); 7157 sema_init(&svl->svl_pgr_sema, 1, NULL, SEMA_DRIVER, NULL); 7158 svl->svl_waiting_for_activepath = 1; 7159 svl->svl_sector_size = 1; 7160 mdi_client_set_vhci_private(tgt_dip, svl); 7161 *didalloc = 1; 7162 VHCI_DEBUG(1, (CE_NOTE, NULL, 7163 "vhci_lun_lookup_alloc: guid %s vlun 0x%p\n", 7164 guid, (void *)svl)); 7165 return (svl); 7166 } 7167 7168 static void 7169 vhci_lun_free(dev_info_t *tgt_dip) 7170 { 7171 struct scsi_vhci_lun *dvlp; 7172 char *guid; 7173 struct scsi_device *sd; 7174 7175 /* 7176 * The scsi_device was set to driver private during child node 7177 * initialization in the scsi_hba_bus_ctl(). 7178 */ 7179 sd = (struct scsi_device *)ddi_get_driver_private(tgt_dip); 7180 7181 dvlp = (struct scsi_vhci_lun *) 7182 mdi_client_get_vhci_private(tgt_dip); 7183 ASSERT(dvlp != NULL); 7184 7185 mdi_client_set_vhci_private(tgt_dip, NULL); 7186 7187 guid = dvlp->svl_lun_wwn; 7188 ASSERT(guid != NULL); 7189 VHCI_DEBUG(4, (CE_NOTE, NULL, "!vhci_lun_free: %s\n", guid)); 7190 7191 mutex_enter(&dvlp->svl_mutex); 7192 if (dvlp->svl_active_pclass != NULL) { 7193 kmem_free(dvlp->svl_active_pclass, 7194 strlen(dvlp->svl_active_pclass)+1); 7195 } 7196 dvlp->svl_active_pclass = NULL; 7197 mutex_exit(&dvlp->svl_mutex); 7198 7199 if (dvlp->svl_lun_wwn != NULL) { 7200 kmem_free(dvlp->svl_lun_wwn, strlen(dvlp->svl_lun_wwn)+1); 7201 } 7202 dvlp->svl_lun_wwn = NULL; 7203 7204 if (dvlp->svl_fops_name) { 7205 kmem_free(dvlp->svl_fops_name, strlen(dvlp->svl_fops_name)+1); 7206 } 7207 dvlp->svl_fops_name = NULL; 7208 7209 if (dvlp->svl_fops_ctpriv != NULL && 7210 dvlp->svl_fops != NULL) { 7211 dvlp->svl_fops->sfo_device_unprobe(sd, dvlp->svl_fops_ctpriv); 7212 } 7213 7214 if (dvlp->svl_flags & VLUN_TASK_D_ALIVE_FLG) 7215 taskq_destroy(dvlp->svl_taskq); 7216 7217 mutex_destroy(&dvlp->svl_mutex); 7218 cv_destroy(&dvlp->svl_cv); 7219 sema_destroy(&dvlp->svl_pgr_sema); 7220 kmem_free(dvlp, sizeof (*dvlp)); 7221 /* 7222 * vhci_lun_free may be called before the tgt_dip 7223 * initialization so check if the sd is NULL. 7224 */ 7225 if (sd != NULL) 7226 scsi_device_hba_private_set(sd, NULL); 7227 } 7228 7229 int 7230 vhci_do_scsi_cmd(struct scsi_pkt *pkt) 7231 { 7232 int err = 0; 7233 int retry_cnt = 0; 7234 uint8_t *sns, skey; 7235 7236 #ifdef DEBUG 7237 if (vhci_debug > 5) { 7238 vhci_print_cdb(pkt->pkt_address.a_hba_tran->tran_hba_dip, 7239 CE_WARN, "Vhci command", pkt->pkt_cdbp); 7240 } 7241 #endif 7242 7243 retry: 7244 err = scsi_poll(pkt); 7245 if (err) { 7246 if (pkt->pkt_cdbp[0] == SCMD_RELEASE) { 7247 if (SCBP_C(pkt) == STATUS_RESERVATION_CONFLICT) { 7248 VHCI_DEBUG(1, (CE_NOTE, NULL, 7249 "!v_s_do_s_c: RELEASE conflict\n")); 7250 return (0); 7251 } 7252 } 7253 if (retry_cnt++ < 3) { 7254 VHCI_DEBUG(1, (CE_WARN, NULL, 7255 "!v_s_do_s_c:retry packet 0x%p " 7256 "status 0x%x reason %s", 7257 (void *)pkt, SCBP_C(pkt), 7258 scsi_rname(pkt->pkt_reason))); 7259 if ((pkt->pkt_reason == CMD_CMPLT) && 7260 (SCBP_C(pkt) == STATUS_CHECK) && 7261 (pkt->pkt_state & STATE_ARQ_DONE)) { 7262 sns = (uint8_t *) 7263 &(((struct scsi_arq_status *)(uintptr_t) 7264 (pkt->pkt_scbp))->sts_sensedata); 7265 skey = scsi_sense_key(sns); 7266 VHCI_DEBUG(1, (CE_WARN, NULL, 7267 "!v_s_do_s_c:retry " 7268 "packet 0x%p sense data %s", (void *)pkt, 7269 scsi_sname(skey))); 7270 } 7271 goto retry; 7272 } 7273 VHCI_DEBUG(1, (CE_WARN, NULL, 7274 "!v_s_do_s_c: failed transport 0x%p 0x%x", 7275 (void *)pkt, SCBP_C(pkt))); 7276 return (0); 7277 } 7278 7279 switch (pkt->pkt_reason) { 7280 case CMD_TIMEOUT: 7281 VHCI_DEBUG(1, (CE_WARN, NULL, "!pkt timed " 7282 "out (pkt 0x%p)", (void *)pkt)); 7283 return (0); 7284 case CMD_CMPLT: 7285 switch (SCBP_C(pkt)) { 7286 case STATUS_GOOD: 7287 break; 7288 case STATUS_CHECK: 7289 if (pkt->pkt_state & STATE_ARQ_DONE) { 7290 sns = (uint8_t *)&((( 7291 struct scsi_arq_status *) 7292 (uintptr_t) 7293 (pkt->pkt_scbp))-> 7294 sts_sensedata); 7295 skey = scsi_sense_key(sns); 7296 if ((skey == 7297 KEY_UNIT_ATTENTION) || 7298 (skey == 7299 KEY_NOT_READY)) { 7300 /* 7301 * clear unit attn. 7302 */ 7303 7304 VHCI_DEBUG(1, 7305 (CE_WARN, NULL, 7306 "!v_s_do_s_c: " 7307 "retry " 7308 "packet 0x%p sense " 7309 "data %s", 7310 (void *)pkt, 7311 scsi_sname 7312 (skey))); 7313 goto retry; 7314 } 7315 VHCI_DEBUG(4, (CE_WARN, NULL, 7316 "!ARQ while " 7317 "transporting " 7318 "(pkt 0x%p)", 7319 (void *)pkt)); 7320 return (0); 7321 } 7322 return (0); 7323 default: 7324 VHCI_DEBUG(1, (CE_WARN, NULL, 7325 "!Bad status returned " 7326 "(pkt 0x%p, status %x)", 7327 (void *)pkt, SCBP_C(pkt))); 7328 return (0); 7329 } 7330 break; 7331 case CMD_INCOMPLETE: 7332 case CMD_RESET: 7333 case CMD_ABORTED: 7334 case CMD_TRAN_ERR: 7335 if (retry_cnt++ < 1) { 7336 VHCI_DEBUG(1, (CE_WARN, NULL, 7337 "!v_s_do_s_c: retry packet 0x%p %s", 7338 (void *)pkt, scsi_rname(pkt->pkt_reason))); 7339 goto retry; 7340 } 7341 /* FALLTHROUGH */ 7342 default: 7343 VHCI_DEBUG(1, (CE_WARN, NULL, "!pkt did not " 7344 "complete successfully (pkt 0x%p," 7345 "reason %x)", (void *)pkt, pkt->pkt_reason)); 7346 return (0); 7347 } 7348 return (1); 7349 } 7350 7351 static int 7352 vhci_quiesce_lun(struct scsi_vhci_lun *vlun) 7353 { 7354 mdi_pathinfo_t *pip, *spip; 7355 dev_info_t *cdip; 7356 struct scsi_vhci_priv *svp; 7357 mdi_pathinfo_state_t pstate; 7358 uint32_t p_ext_state; 7359 int circular; 7360 7361 cdip = vlun->svl_dip; 7362 pip = spip = NULL; 7363 ndi_devi_enter(cdip, &circular); 7364 pip = mdi_get_next_phci_path(cdip, NULL); 7365 while (pip != NULL) { 7366 (void) mdi_pi_get_state2(pip, &pstate, &p_ext_state); 7367 if (pstate != MDI_PATHINFO_STATE_ONLINE) { 7368 spip = pip; 7369 pip = mdi_get_next_phci_path(cdip, spip); 7370 continue; 7371 } 7372 mdi_hold_path(pip); 7373 ndi_devi_exit(cdip, circular); 7374 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip); 7375 mutex_enter(&svp->svp_mutex); 7376 while (svp->svp_cmds != 0) { 7377 if (cv_timedwait(&svp->svp_cv, &svp->svp_mutex, 7378 ddi_get_lbolt() + drv_usectohz 7379 (vhci_path_quiesce_timeout * 1000000)) == -1) { 7380 mutex_exit(&svp->svp_mutex); 7381 mdi_rele_path(pip); 7382 VHCI_DEBUG(1, (CE_WARN, NULL, 7383 "Quiesce of lun is not successful " 7384 "vlun: 0x%p.", (void *)vlun)); 7385 return (0); 7386 } 7387 } 7388 mutex_exit(&svp->svp_mutex); 7389 ndi_devi_enter(cdip, &circular); 7390 spip = pip; 7391 pip = mdi_get_next_phci_path(cdip, spip); 7392 mdi_rele_path(spip); 7393 } 7394 ndi_devi_exit(cdip, circular); 7395 return (1); 7396 } 7397 7398 static int 7399 vhci_pgr_validate_and_register(scsi_vhci_priv_t *svp) 7400 { 7401 scsi_vhci_lun_t *vlun; 7402 vhci_prout_t *prout; 7403 int rval, success; 7404 mdi_pathinfo_t *pip, *npip; 7405 scsi_vhci_priv_t *osvp; 7406 dev_info_t *cdip; 7407 uchar_t cdb_1; 7408 uchar_t temp_res_key[MHIOC_RESV_KEY_SIZE]; 7409 7410 7411 /* 7412 * see if there are any other paths available; if none, 7413 * then there is nothing to do. 7414 */ 7415 cdip = svp->svp_svl->svl_dip; 7416 rval = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH | 7417 MDI_SELECT_STANDBY_PATH, NULL, &pip); 7418 if ((rval != MDI_SUCCESS) || (pip == NULL)) { 7419 VHCI_DEBUG(4, (CE_NOTE, NULL, 7420 "%s%d: vhci_pgr_validate_and_register: first path\n", 7421 ddi_driver_name(cdip), ddi_get_instance(cdip))); 7422 return (1); 7423 } 7424 7425 vlun = svp->svp_svl; 7426 prout = &vlun->svl_prout; 7427 ASSERT(vlun->svl_pgr_active != 0); 7428 7429 /* 7430 * When the path was busy/offlined, some other host might have 7431 * cleared this key. Validate key on some other path first. 7432 * If it fails, return failure. 7433 */ 7434 7435 npip = pip; 7436 pip = NULL; 7437 success = 0; 7438 7439 /* Save the res key */ 7440 bcopy(prout->res_key, temp_res_key, MHIOC_RESV_KEY_SIZE); 7441 7442 /* 7443 * Sometimes CDB from application can be a Register_And_Ignore. 7444 * Instead of validation, this cdb would result in force registration. 7445 * Convert it to normal cdb for validation. 7446 * After that be sure to restore the cdb. 7447 */ 7448 cdb_1 = vlun->svl_cdb[1]; 7449 vlun->svl_cdb[1] &= 0xe0; 7450 7451 do { 7452 osvp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(npip); 7453 if (osvp == NULL) { 7454 VHCI_DEBUG(4, (CE_NOTE, NULL, 7455 "vhci_pgr_validate_and_register: no " 7456 "client priv! 0x%p offlined?\n", 7457 (void *)npip)); 7458 goto next_path_1; 7459 } 7460 7461 if (osvp == svp) { 7462 VHCI_DEBUG(4, (CE_NOTE, NULL, 7463 "vhci_pgr_validate_and_register: same svp 0x%p" 7464 " npip 0x%p vlun 0x%p\n", 7465 (void *)svp, (void *)npip, (void *)vlun)); 7466 goto next_path_1; 7467 } 7468 7469 VHCI_DEBUG(4, (CE_NOTE, NULL, 7470 "vhci_pgr_validate_and_register: First validate on" 7471 " osvp 0x%p being done. vlun 0x%p thread 0x%p Before bcopy" 7472 " cdb1 %x\n", (void *)osvp, (void *)vlun, 7473 (void *)curthread, vlun->svl_cdb[1])); 7474 vhci_print_prout_keys(vlun, "v_pgr_val_reg: before bcopy:"); 7475 7476 bcopy(prout->service_key, prout->res_key, MHIOC_RESV_KEY_SIZE); 7477 7478 VHCI_DEBUG(4, (CE_WARN, NULL, "vlun 0x%p After bcopy", 7479 (void *)vlun)); 7480 vhci_print_prout_keys(vlun, "v_pgr_val_reg: after bcopy: "); 7481 7482 rval = vhci_do_prout(osvp); 7483 if (rval == 1) { 7484 VHCI_DEBUG(4, (CE_NOTE, NULL, 7485 "%s%d: vhci_pgr_validate_and_register: key" 7486 " validated thread 0x%p\n", ddi_driver_name(cdip), 7487 ddi_get_instance(cdip), (void *)curthread)); 7488 pip = npip; 7489 success = 1; 7490 break; 7491 } else { 7492 VHCI_DEBUG(4, (CE_NOTE, NULL, 7493 "vhci_pgr_validate_and_register: First validation" 7494 " on osvp 0x%p failed %x\n", (void *)osvp, rval)); 7495 vhci_print_prout_keys(vlun, "v_pgr_val_reg: failed:"); 7496 } 7497 7498 /* 7499 * Try other paths 7500 */ 7501 next_path_1: 7502 pip = npip; 7503 rval = mdi_select_path(cdip, NULL, 7504 MDI_SELECT_ONLINE_PATH|MDI_SELECT_STANDBY_PATH, 7505 pip, &npip); 7506 mdi_rele_path(pip); 7507 } while ((rval == MDI_SUCCESS) && (npip != NULL)); 7508 7509 7510 /* Be sure to restore original cdb */ 7511 vlun->svl_cdb[1] = cdb_1; 7512 7513 /* Restore the res_key */ 7514 bcopy(temp_res_key, prout->res_key, MHIOC_RESV_KEY_SIZE); 7515 7516 /* 7517 * If key could not be registered on any path for the first time, 7518 * return success as online should still continue. 7519 */ 7520 if (success == 0) { 7521 return (1); 7522 } 7523 7524 ASSERT(pip != NULL); 7525 7526 /* 7527 * Force register on new path 7528 */ 7529 cdb_1 = vlun->svl_cdb[1]; /* store the cdb */ 7530 7531 vlun->svl_cdb[1] &= 0xe0; 7532 vlun->svl_cdb[1] |= VHCI_PROUT_R_AND_IGNORE; 7533 7534 vhci_print_prout_keys(vlun, "v_pgr_val_reg: keys before bcopy: "); 7535 7536 bcopy(prout->active_service_key, prout->service_key, 7537 MHIOC_RESV_KEY_SIZE); 7538 bcopy(prout->active_res_key, prout->res_key, MHIOC_RESV_KEY_SIZE); 7539 7540 vhci_print_prout_keys(vlun, "v_pgr_val_reg:keys after bcopy: "); 7541 7542 rval = vhci_do_prout(svp); 7543 vlun->svl_cdb[1] = cdb_1; /* restore the cdb */ 7544 if (rval != 1) { 7545 VHCI_DEBUG(4, (CE_NOTE, NULL, 7546 "vhci_pgr_validate_and_register: register on new" 7547 " path 0x%p svp 0x%p failed %x\n", 7548 (void *)pip, (void *)svp, rval)); 7549 vhci_print_prout_keys(vlun, "v_pgr_val_reg: reg failed: "); 7550 mdi_rele_path(pip); 7551 return (0); 7552 } 7553 7554 if (bcmp(prout->service_key, zero_key, MHIOC_RESV_KEY_SIZE) == 0) { 7555 VHCI_DEBUG(4, (CE_NOTE, NULL, 7556 "vhci_pgr_validate_and_register: zero service key\n")); 7557 mdi_rele_path(pip); 7558 return (rval); 7559 } 7560 7561 /* 7562 * While the key was force registered, some other host might have 7563 * cleared the key. Re-validate key on another pre-existing path 7564 * before declaring success. 7565 */ 7566 npip = pip; 7567 pip = NULL; 7568 7569 /* 7570 * Sometimes CDB from application can be Register and Ignore. 7571 * Instead of validation, it would result in force registration. 7572 * Convert it to normal cdb for validation. 7573 * After that be sure to restore the cdb. 7574 */ 7575 cdb_1 = vlun->svl_cdb[1]; 7576 vlun->svl_cdb[1] &= 0xe0; 7577 success = 0; 7578 7579 do { 7580 osvp = (scsi_vhci_priv_t *) 7581 mdi_pi_get_vhci_private(npip); 7582 if (osvp == NULL) { 7583 VHCI_DEBUG(4, (CE_NOTE, NULL, 7584 "vhci_pgr_validate_and_register: no " 7585 "client priv! 0x%p offlined?\n", 7586 (void *)npip)); 7587 goto next_path_2; 7588 } 7589 7590 if (osvp == svp) { 7591 VHCI_DEBUG(4, (CE_NOTE, NULL, 7592 "vhci_pgr_validate_and_register: same osvp 0x%p" 7593 " npip 0x%p vlun 0x%p\n", 7594 (void *)svp, (void *)npip, (void *)vlun)); 7595 goto next_path_2; 7596 } 7597 7598 VHCI_DEBUG(4, (CE_NOTE, NULL, 7599 "vhci_pgr_validate_and_register: Re-validation on" 7600 " osvp 0x%p being done. vlun 0x%p Before bcopy cdb1 %x\n", 7601 (void *)osvp, (void *)vlun, vlun->svl_cdb[1])); 7602 vhci_print_prout_keys(vlun, "v_pgr_val_reg: before bcopy: "); 7603 7604 bcopy(prout->service_key, prout->res_key, MHIOC_RESV_KEY_SIZE); 7605 7606 vhci_print_prout_keys(vlun, "v_pgr_val_reg: after bcopy: "); 7607 7608 rval = vhci_do_prout(osvp); 7609 if (rval == 1) { 7610 VHCI_DEBUG(4, (CE_NOTE, NULL, 7611 "%s%d: vhci_pgr_validate_and_register: key" 7612 " validated thread 0x%p\n", ddi_driver_name(cdip), 7613 ddi_get_instance(cdip), (void *)curthread)); 7614 pip = npip; 7615 success = 1; 7616 break; 7617 } else { 7618 VHCI_DEBUG(4, (CE_NOTE, NULL, 7619 "vhci_pgr_validate_and_register: Re-validation on" 7620 " osvp 0x%p failed %x\n", (void *)osvp, rval)); 7621 vhci_print_prout_keys(vlun, 7622 "v_pgr_val_reg: reval failed: "); 7623 } 7624 7625 /* 7626 * Try other paths 7627 */ 7628 next_path_2: 7629 pip = npip; 7630 rval = mdi_select_path(cdip, NULL, 7631 MDI_SELECT_ONLINE_PATH|MDI_SELECT_STANDBY_PATH, 7632 pip, &npip); 7633 mdi_rele_path(pip); 7634 } while ((rval == MDI_SUCCESS) && (npip != NULL)); 7635 7636 /* Be sure to restore original cdb */ 7637 vlun->svl_cdb[1] = cdb_1; 7638 7639 if (success == 1) { 7640 /* Successfully validated registration */ 7641 mdi_rele_path(pip); 7642 return (1); 7643 } 7644 7645 VHCI_DEBUG(4, (CE_WARN, NULL, "key validation failed")); 7646 7647 /* 7648 * key invalid, back out by registering key value of 0 7649 */ 7650 VHCI_DEBUG(4, (CE_NOTE, NULL, 7651 "vhci_pgr_validate_and_register: backout on" 7652 " svp 0x%p being done\n", (void *)svp)); 7653 vhci_print_prout_keys(vlun, "v_pgr_val_reg: before bcopy: "); 7654 7655 bcopy(prout->service_key, prout->res_key, MHIOC_RESV_KEY_SIZE); 7656 bzero(prout->service_key, MHIOC_RESV_KEY_SIZE); 7657 7658 vhci_print_prout_keys(vlun, "v_pgr_val_reg: before bcopy: "); 7659 7660 /* 7661 * Get a new path 7662 */ 7663 rval = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH | 7664 MDI_SELECT_STANDBY_PATH, NULL, &pip); 7665 if ((rval != MDI_SUCCESS) || (pip == NULL)) { 7666 VHCI_DEBUG(4, (CE_NOTE, NULL, 7667 "%s%d: vhci_pgr_validate_and_register: no valid pip\n", 7668 ddi_driver_name(cdip), ddi_get_instance(cdip))); 7669 return (0); 7670 } 7671 7672 if ((rval = vhci_do_prout(svp)) != 1) { 7673 VHCI_DEBUG(4, (CE_NOTE, NULL, 7674 "vhci_pgr_validate_and_register: backout on" 7675 " svp 0x%p failed\n", (void *)svp)); 7676 vhci_print_prout_keys(vlun, "backout failed"); 7677 7678 VHCI_DEBUG(4, (CE_WARN, NULL, 7679 "%s%d: vhci_pgr_validate_and_register: key" 7680 " validation and backout failed", ddi_driver_name(cdip), 7681 ddi_get_instance(cdip))); 7682 if (rval == VHCI_PGR_ILLEGALOP) { 7683 VHCI_DEBUG(4, (CE_WARN, NULL, 7684 "%s%d: vhci_pgr_validate_and_register: key" 7685 " already cleared", ddi_driver_name(cdip), 7686 ddi_get_instance(cdip))); 7687 rval = 1; 7688 } else 7689 rval = 0; 7690 } else { 7691 VHCI_DEBUG(4, (CE_NOTE, NULL, 7692 "%s%d: vhci_pgr_validate_and_register: key" 7693 " validation failed, key backed out\n", 7694 ddi_driver_name(cdip), ddi_get_instance(cdip))); 7695 vhci_print_prout_keys(vlun, "v_pgr_val_reg: key backed out: "); 7696 } 7697 mdi_rele_path(pip); 7698 7699 return (rval); 7700 } 7701 7702 /* 7703 * taskq routine to dispatch a scsi cmd to vhci_scsi_start. This ensures 7704 * that vhci_scsi_start is not called in interrupt context. 7705 * As the upper layer gets TRAN_ACCEPT when the command is dispatched, we 7706 * need to complete the command if something goes wrong. 7707 */ 7708 static void 7709 vhci_dispatch_scsi_start(void *arg) 7710 { 7711 struct vhci_pkt *vpkt = (struct vhci_pkt *)arg; 7712 struct scsi_pkt *tpkt = vpkt->vpkt_tgt_pkt; 7713 int rval = TRAN_BUSY; 7714 7715 VHCI_DEBUG(6, (CE_NOTE, NULL, "!vhci_dispatch_scsi_start: sending" 7716 " scsi-2 reserve for 0x%p\n", 7717 (void *)ADDR2DIP(&(vpkt->vpkt_tgt_pkt->pkt_address)))); 7718 7719 /* 7720 * To prevent the taskq from being called recursively we set the 7721 * the VHCI_PKT_THRU_TASKQ bit in the vhci_pkt_states. 7722 */ 7723 vpkt->vpkt_state |= VHCI_PKT_THRU_TASKQ; 7724 7725 /* 7726 * Wait for the transport to get ready to send packets 7727 * and if it times out, it will return something other than 7728 * TRAN_BUSY. The vhci_reserve_delay may want to 7729 * get tuned for other transports and is therefore a global. 7730 * Using delay since this routine is called by taskq dispatch 7731 * and not called during interrupt context. 7732 */ 7733 while ((rval = vhci_scsi_start(&(vpkt->vpkt_tgt_pkt->pkt_address), 7734 vpkt->vpkt_tgt_pkt)) == TRAN_BUSY) { 7735 delay(drv_usectohz(vhci_reserve_delay)); 7736 } 7737 7738 switch (rval) { 7739 case TRAN_ACCEPT: 7740 return; 7741 7742 default: 7743 /* 7744 * This pkt shall be retried, and to ensure another taskq 7745 * is dispatched for it, clear the VHCI_PKT_THRU_TASKQ 7746 * flag. 7747 */ 7748 vpkt->vpkt_state &= ~VHCI_PKT_THRU_TASKQ; 7749 7750 /* Ensure that the pkt is retried without a reset */ 7751 tpkt->pkt_reason = CMD_ABORTED; 7752 tpkt->pkt_statistics |= STAT_ABORTED; 7753 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_dispatch_scsi_start: " 7754 "TRAN_rval %d returned for dip 0x%p", rval, 7755 (void *)ADDR2DIP(&(vpkt->vpkt_tgt_pkt->pkt_address)))); 7756 break; 7757 } 7758 7759 /* 7760 * vpkt_org_vpkt should always be NULL here if the retry command 7761 * has been successfully dispatched. If vpkt_org_vpkt != NULL at 7762 * this point, it is an error so restore the original vpkt and 7763 * return an error to the target driver so it can retry the 7764 * command as appropriate. 7765 */ 7766 if (vpkt->vpkt_org_vpkt != NULL) { 7767 struct vhci_pkt *new_vpkt = vpkt; 7768 scsi_vhci_priv_t *svp = (scsi_vhci_priv_t *) 7769 mdi_pi_get_vhci_private(vpkt->vpkt_path); 7770 7771 vpkt = vpkt->vpkt_org_vpkt; 7772 7773 vpkt->vpkt_tgt_pkt->pkt_reason = tpkt->pkt_reason; 7774 vpkt->vpkt_tgt_pkt->pkt_statistics = tpkt->pkt_statistics; 7775 7776 vhci_scsi_destroy_pkt(&svp->svp_psd->sd_address, 7777 new_vpkt->vpkt_tgt_pkt); 7778 7779 tpkt = vpkt->vpkt_tgt_pkt; 7780 } 7781 7782 scsi_hba_pkt_comp(tpkt); 7783 } 7784 7785 static void 7786 vhci_initiate_auto_failback(void *arg) 7787 { 7788 struct scsi_vhci_lun *vlun = (struct scsi_vhci_lun *)arg; 7789 dev_info_t *vdip, *cdip; 7790 int held; 7791 7792 cdip = vlun->svl_dip; 7793 vdip = ddi_get_parent(cdip); 7794 7795 VHCI_HOLD_LUN(vlun, VH_SLEEP, held); 7796 7797 /* 7798 * Perform a final check to see if the active path class is indeed 7799 * not the preferred path class. As in the time the auto failback 7800 * was dispatched, an external failover could have been detected. 7801 * [Some other host could have detected this condition and triggered 7802 * the auto failback before]. 7803 * In such a case if we go ahead with failover we will be negating the 7804 * whole purpose of auto failback. 7805 */ 7806 mutex_enter(&vlun->svl_mutex); 7807 if (vlun->svl_active_pclass != NULL) { 7808 char *best_pclass; 7809 struct scsi_failover_ops *fo; 7810 7811 fo = vlun->svl_fops; 7812 7813 (void) fo->sfo_pathclass_next(NULL, &best_pclass, 7814 vlun->svl_fops_ctpriv); 7815 if (strcmp(vlun->svl_active_pclass, best_pclass) == 0) { 7816 mutex_exit(&vlun->svl_mutex); 7817 VHCI_RELEASE_LUN(vlun); 7818 VHCI_DEBUG(1, (CE_NOTE, NULL, "Not initiating " 7819 "auto failback for %s as %s pathclass already " 7820 "active.\n", vlun->svl_lun_wwn, best_pclass)); 7821 return; 7822 } 7823 } 7824 mutex_exit(&vlun->svl_mutex); 7825 if (mdi_failover(vdip, vlun->svl_dip, MDI_FAILOVER_SYNC) 7826 == MDI_SUCCESS) { 7827 vhci_log(CE_NOTE, vdip, "!Auto failback operation " 7828 "succeeded for device %s (GUID %s)", 7829 ddi_node_name(cdip), vlun->svl_lun_wwn); 7830 } else { 7831 vhci_log(CE_NOTE, vdip, "!Auto failback operation " 7832 "failed for device %s (GUID %s)", 7833 ddi_node_name(cdip), vlun->svl_lun_wwn); 7834 } 7835 VHCI_RELEASE_LUN(vlun); 7836 } 7837 7838 #ifdef DEBUG 7839 static void 7840 vhci_print_prin_keys(vhci_prin_readkeys_t *prin, int numkeys) 7841 { 7842 vhci_clean_print(NULL, 5, "Current PGR Keys", 7843 (uchar_t *)prin, numkeys * 8); 7844 } 7845 #endif 7846 7847 static void 7848 vhci_print_prout_keys(scsi_vhci_lun_t *vlun, char *msg) 7849 { 7850 int i; 7851 vhci_prout_t *prout; 7852 char buf1[4*MHIOC_RESV_KEY_SIZE + 1]; 7853 char buf2[4*MHIOC_RESV_KEY_SIZE + 1]; 7854 char buf3[4*MHIOC_RESV_KEY_SIZE + 1]; 7855 char buf4[4*MHIOC_RESV_KEY_SIZE + 1]; 7856 7857 prout = &vlun->svl_prout; 7858 7859 for (i = 0; i < MHIOC_RESV_KEY_SIZE; i++) 7860 (void) sprintf(&buf1[4*i], "[%02x]", prout->res_key[i]); 7861 for (i = 0; i < MHIOC_RESV_KEY_SIZE; i++) 7862 (void) sprintf(&buf2[(4*i)], "[%02x]", prout->service_key[i]); 7863 for (i = 0; i < MHIOC_RESV_KEY_SIZE; i++) 7864 (void) sprintf(&buf3[4*i], "[%02x]", prout->active_res_key[i]); 7865 for (i = 0; i < MHIOC_RESV_KEY_SIZE; i++) 7866 (void) sprintf(&buf4[4*i], "[%02x]", 7867 prout->active_service_key[i]); 7868 7869 /* Printing all in one go. Otherwise it will jumble up */ 7870 VHCI_DEBUG(5, (CE_CONT, NULL, "%s vlun 0x%p, thread 0x%p\n" 7871 "res_key: : %s\n" 7872 "service_key : %s\n" 7873 "active_res_key : %s\n" 7874 "active_service_key: %s\n", 7875 msg, (void *)vlun, (void *)curthread, buf1, buf2, buf3, buf4)); 7876 } 7877 7878 /* 7879 * Called from vhci_scsi_start to update the pHCI pkt with target packet. 7880 */ 7881 static void 7882 vhci_update_pHCI_pkt(struct vhci_pkt *vpkt, struct scsi_pkt *pkt) 7883 { 7884 7885 ASSERT(vpkt->vpkt_hba_pkt); 7886 7887 vpkt->vpkt_hba_pkt->pkt_flags = pkt->pkt_flags; 7888 vpkt->vpkt_hba_pkt->pkt_flags |= FLAG_NOQUEUE; 7889 7890 if ((vpkt->vpkt_hba_pkt->pkt_flags & FLAG_NOINTR) || 7891 MDI_PI_IS_SUSPENDED(vpkt->vpkt_path)) { 7892 /* 7893 * Polled Command is requested or HBA is in 7894 * suspended state 7895 */ 7896 vpkt->vpkt_hba_pkt->pkt_flags |= FLAG_NOINTR; 7897 vpkt->vpkt_hba_pkt->pkt_comp = NULL; 7898 } else { 7899 vpkt->vpkt_hba_pkt->pkt_comp = vhci_intr; 7900 } 7901 vpkt->vpkt_hba_pkt->pkt_time = pkt->pkt_time; 7902 bcopy(pkt->pkt_cdbp, vpkt->vpkt_hba_pkt->pkt_cdbp, 7903 vpkt->vpkt_tgt_init_cdblen); 7904 vpkt->vpkt_hba_pkt->pkt_resid = pkt->pkt_resid; 7905 7906 /* Re-initialize the following pHCI packet state information */ 7907 vpkt->vpkt_hba_pkt->pkt_state = 0; 7908 vpkt->vpkt_hba_pkt->pkt_statistics = 0; 7909 vpkt->vpkt_hba_pkt->pkt_reason = 0; 7910 } 7911 7912 static int 7913 vhci_scsi_bus_power(dev_info_t *parent, void *impl_arg, pm_bus_power_op_t op, 7914 void *arg, void *result) 7915 { 7916 int ret = DDI_SUCCESS; 7917 7918 /* 7919 * Generic processing in MPxIO framework 7920 */ 7921 ret = mdi_bus_power(parent, impl_arg, op, arg, result); 7922 7923 switch (ret) { 7924 case MDI_SUCCESS: 7925 ret = DDI_SUCCESS; 7926 break; 7927 case MDI_FAILURE: 7928 ret = DDI_FAILURE; 7929 break; 7930 default: 7931 break; 7932 } 7933 7934 return (ret); 7935 } 7936 7937 static int 7938 vhci_pHCI_cap(struct scsi_address *ap, char *cap, int val, int whom, 7939 mdi_pathinfo_t *pip) 7940 { 7941 dev_info_t *cdip; 7942 mdi_pathinfo_t *npip = NULL; 7943 scsi_vhci_priv_t *svp = NULL; 7944 struct scsi_address *pap = NULL; 7945 scsi_hba_tran_t *hba = NULL; 7946 int sps; 7947 int mps_flag; 7948 int rval = 0; 7949 7950 mps_flag = (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH); 7951 if (pip) { 7952 /* 7953 * If the call is from vhci_pathinfo_state_change, 7954 * then this path was busy and is becoming ready to accept IO. 7955 */ 7956 ASSERT(ap != NULL); 7957 hba = ap->a_hba_tran; 7958 ASSERT(hba != NULL); 7959 rval = scsi_ifsetcap(ap, cap, val, whom); 7960 7961 VHCI_DEBUG(2, (CE_NOTE, NULL, 7962 "!vhci_pHCI_cap: only on path %p, ap %p, rval %x\n", 7963 (void *)pip, (void *)ap, rval)); 7964 7965 return (rval); 7966 } 7967 7968 /* 7969 * Set capability on all the pHCIs. 7970 * If any path is busy, then the capability would be set by 7971 * vhci_pathinfo_state_change. 7972 */ 7973 7974 cdip = ADDR2DIP(ap); 7975 ASSERT(cdip != NULL); 7976 sps = mdi_select_path(cdip, NULL, mps_flag, NULL, &pip); 7977 if ((sps != MDI_SUCCESS) || (pip == NULL)) { 7978 VHCI_DEBUG(2, (CE_WARN, NULL, 7979 "!vhci_pHCI_cap: Unable to get a path, dip 0x%p", 7980 (void *)cdip)); 7981 return (0); 7982 } 7983 7984 again: 7985 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip); 7986 if (svp == NULL) { 7987 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_pHCI_cap: " 7988 "priv is NULL, pip 0x%p", (void *)pip)); 7989 mdi_rele_path(pip); 7990 return (rval); 7991 } 7992 7993 if (svp->svp_psd == NULL) { 7994 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_pHCI_cap: " 7995 "psd is NULL, pip 0x%p, svp 0x%p", 7996 (void *)pip, (void *)svp)); 7997 mdi_rele_path(pip); 7998 return (rval); 7999 } 8000 8001 pap = &svp->svp_psd->sd_address; 8002 ASSERT(pap != NULL); 8003 hba = pap->a_hba_tran; 8004 ASSERT(hba != NULL); 8005 8006 if (hba->tran_setcap != NULL) { 8007 rval = scsi_ifsetcap(pap, cap, val, whom); 8008 8009 VHCI_DEBUG(2, (CE_NOTE, NULL, 8010 "!vhci_pHCI_cap: path %p, ap %p, rval %x\n", 8011 (void *)pip, (void *)ap, rval)); 8012 8013 /* 8014 * Select next path and issue the setcap, repeat 8015 * until all paths are exhausted 8016 */ 8017 sps = mdi_select_path(cdip, NULL, mps_flag, pip, &npip); 8018 if ((sps != MDI_SUCCESS) || (npip == NULL)) { 8019 mdi_rele_path(pip); 8020 return (1); 8021 } 8022 mdi_rele_path(pip); 8023 pip = npip; 8024 goto again; 8025 } 8026 mdi_rele_path(pip); 8027 return (rval); 8028 } 8029 8030 static int 8031 vhci_scsi_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op, 8032 void *arg, dev_info_t **child) 8033 { 8034 char *guid; 8035 8036 if (vhci_bus_config_debug) 8037 flags |= NDI_DEVI_DEBUG; 8038 8039 if (op == BUS_CONFIG_ONE || op == BUS_UNCONFIG_ONE) 8040 guid = vhci_devnm_to_guid((char *)arg); 8041 else 8042 guid = NULL; 8043 8044 if (mdi_vhci_bus_config(pdip, flags, op, arg, child, guid) 8045 == MDI_SUCCESS) 8046 return (NDI_SUCCESS); 8047 else 8048 return (NDI_FAILURE); 8049 } 8050 8051 static int 8052 vhci_scsi_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op, 8053 void *arg) 8054 { 8055 if (vhci_bus_config_debug) 8056 flags |= NDI_DEVI_DEBUG; 8057 8058 return (ndi_busop_bus_unconfig(pdip, flags, op, arg)); 8059 } 8060 8061 /* 8062 * Take the original vhci_pkt, create a duplicate of the pkt for resending 8063 * as though it originated in ssd. 8064 */ 8065 static struct scsi_pkt * 8066 vhci_create_retry_pkt(struct vhci_pkt *vpkt) 8067 { 8068 struct vhci_pkt *new_vpkt = NULL; 8069 struct scsi_pkt *pkt = NULL; 8070 8071 scsi_vhci_priv_t *svp = (scsi_vhci_priv_t *) 8072 mdi_pi_get_vhci_private(vpkt->vpkt_path); 8073 8074 /* 8075 * Ensure consistent data at completion time by setting PKT_CONSISTENT 8076 */ 8077 pkt = vhci_scsi_init_pkt(&svp->svp_psd->sd_address, pkt, 8078 vpkt->vpkt_tgt_init_bp, vpkt->vpkt_tgt_init_cdblen, 8079 vpkt->vpkt_tgt_init_scblen, 0, PKT_CONSISTENT, NULL_FUNC, NULL); 8080 if (pkt != NULL) { 8081 new_vpkt = TGTPKT2VHCIPKT(pkt); 8082 8083 pkt->pkt_address = vpkt->vpkt_tgt_pkt->pkt_address; 8084 pkt->pkt_flags = vpkt->vpkt_tgt_pkt->pkt_flags; 8085 pkt->pkt_time = vpkt->vpkt_tgt_pkt->pkt_time; 8086 pkt->pkt_comp = vpkt->vpkt_tgt_pkt->pkt_comp; 8087 8088 pkt->pkt_resid = 0; 8089 pkt->pkt_statistics = 0; 8090 pkt->pkt_reason = 0; 8091 8092 bcopy(vpkt->vpkt_tgt_pkt->pkt_cdbp, 8093 pkt->pkt_cdbp, vpkt->vpkt_tgt_init_cdblen); 8094 8095 /* 8096 * Save a pointer to the original vhci_pkt 8097 */ 8098 new_vpkt->vpkt_org_vpkt = vpkt; 8099 } 8100 8101 return (pkt); 8102 } 8103 8104 /* 8105 * Copy the successful completion information from the hba packet into 8106 * the original target pkt from the upper layer. Returns the original 8107 * vpkt and destroys the new vpkt from the internal retry. 8108 */ 8109 static struct vhci_pkt * 8110 vhci_sync_retry_pkt(struct vhci_pkt *vpkt) 8111 { 8112 struct vhci_pkt *ret_vpkt = NULL; 8113 struct scsi_pkt *tpkt = NULL; 8114 struct scsi_pkt *hba_pkt = NULL; 8115 scsi_vhci_priv_t *svp = (scsi_vhci_priv_t *) 8116 mdi_pi_get_vhci_private(vpkt->vpkt_path); 8117 8118 ASSERT(vpkt->vpkt_org_vpkt != NULL); 8119 VHCI_DEBUG(0, (CE_NOTE, NULL, "vhci_sync_retry_pkt: Retry pkt " 8120 "completed successfully!\n")); 8121 8122 ret_vpkt = vpkt->vpkt_org_vpkt; 8123 tpkt = ret_vpkt->vpkt_tgt_pkt; 8124 hba_pkt = vpkt->vpkt_hba_pkt; 8125 8126 /* 8127 * Copy the good status into the target driver's packet 8128 */ 8129 *(tpkt->pkt_scbp) = *(hba_pkt->pkt_scbp); 8130 tpkt->pkt_resid = hba_pkt->pkt_resid; 8131 tpkt->pkt_state = hba_pkt->pkt_state; 8132 tpkt->pkt_statistics = hba_pkt->pkt_statistics; 8133 tpkt->pkt_reason = hba_pkt->pkt_reason; 8134 8135 /* 8136 * Destroy the internally created vpkt for the retry 8137 */ 8138 vhci_scsi_destroy_pkt(&svp->svp_psd->sd_address, 8139 vpkt->vpkt_tgt_pkt); 8140 8141 return (ret_vpkt); 8142 } 8143 8144 /* restart the request sense request */ 8145 static void 8146 vhci_uscsi_restart_sense(void *arg) 8147 { 8148 struct buf *rqbp; 8149 struct buf *bp; 8150 struct scsi_pkt *rqpkt = (struct scsi_pkt *)arg; 8151 mp_uscsi_cmd_t *mp_uscmdp; 8152 8153 VHCI_DEBUG(4, (CE_WARN, NULL, 8154 "vhci_uscsi_restart_sense: enter: rqpkt: %p", (void *)rqpkt)); 8155 8156 if (scsi_transport(rqpkt) != TRAN_ACCEPT) { 8157 /* if it fails - need to wakeup the original command */ 8158 mp_uscmdp = rqpkt->pkt_private; 8159 bp = mp_uscmdp->cmdbp; 8160 rqbp = mp_uscmdp->rqbp; 8161 ASSERT(mp_uscmdp && bp && rqbp); 8162 scsi_free_consistent_buf(rqbp); 8163 scsi_destroy_pkt(rqpkt); 8164 bp->b_resid = bp->b_bcount; 8165 bioerror(bp, EIO); 8166 biodone(bp); 8167 } 8168 } 8169 8170 /* 8171 * auto-rqsense is not enabled so we have to retrieve the request sense 8172 * manually. 8173 */ 8174 static int 8175 vhci_uscsi_send_sense(struct scsi_pkt *pkt, mp_uscsi_cmd_t *mp_uscmdp) 8176 { 8177 struct buf *rqbp, *cmdbp; 8178 struct scsi_pkt *rqpkt; 8179 int rval = 0; 8180 8181 cmdbp = mp_uscmdp->cmdbp; 8182 ASSERT(cmdbp != NULL); 8183 8184 VHCI_DEBUG(4, (CE_WARN, NULL, 8185 "vhci_uscsi_send_sense: enter: bp: %p pkt: %p scmd: %p", 8186 (void *)cmdbp, (void *)pkt, (void *)mp_uscmdp)); 8187 /* set up the packet information and cdb */ 8188 if ((rqbp = scsi_alloc_consistent_buf(mp_uscmdp->ap, NULL, 8189 SENSE_LENGTH, B_READ, NULL, NULL)) == NULL) { 8190 return (-1); 8191 } 8192 8193 if ((rqpkt = scsi_init_pkt(mp_uscmdp->ap, NULL, rqbp, 8194 CDB_GROUP0, 1, 0, PKT_CONSISTENT, NULL, NULL)) == NULL) { 8195 scsi_free_consistent_buf(rqbp); 8196 return (-1); 8197 } 8198 8199 (void) scsi_setup_cdb((union scsi_cdb *)(intptr_t)rqpkt->pkt_cdbp, 8200 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 8201 8202 mp_uscmdp->rqbp = rqbp; 8203 rqbp->b_private = mp_uscmdp; 8204 rqpkt->pkt_flags |= FLAG_SENSING; 8205 rqpkt->pkt_time = 60; 8206 rqpkt->pkt_comp = vhci_uscsi_iodone; 8207 rqpkt->pkt_private = mp_uscmdp; 8208 8209 /* 8210 * NOTE: This code path is related to MPAPI uscsi(7I), so path 8211 * selection is not based on path_instance. 8212 */ 8213 if (scsi_pkt_allocated_correctly(rqpkt)) 8214 rqpkt->pkt_path_instance = 0; 8215 8216 /* get her done */ 8217 switch (scsi_transport(rqpkt)) { 8218 case TRAN_ACCEPT: 8219 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_uscsi_send_sense: " 8220 "transport accepted.")); 8221 break; 8222 case TRAN_BUSY: 8223 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_uscsi_send_sense: " 8224 "transport busy, setting timeout.")); 8225 vhci_restart_timeid = timeout(vhci_uscsi_restart_sense, rqpkt, 8226 (drv_usectohz(5 * 1000000))); 8227 break; 8228 default: 8229 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_uscsi_send_sense: " 8230 "transport failed")); 8231 scsi_free_consistent_buf(rqbp); 8232 scsi_destroy_pkt(rqpkt); 8233 rval = -1; 8234 } 8235 8236 return (rval); 8237 } 8238 8239 /* 8240 * done routine for the mpapi uscsi command - this is behaving as though 8241 * FLAG_DIAGNOSE is set meaning there are no retries except for a manual 8242 * request sense. 8243 */ 8244 void 8245 vhci_uscsi_iodone(struct scsi_pkt *pkt) 8246 { 8247 struct buf *bp; 8248 mp_uscsi_cmd_t *mp_uscmdp; 8249 struct uscsi_cmd *uscmdp; 8250 struct scsi_arq_status *arqstat; 8251 int err; 8252 8253 mp_uscmdp = (mp_uscsi_cmd_t *)pkt->pkt_private; 8254 uscmdp = mp_uscmdp->uscmdp; 8255 bp = mp_uscmdp->cmdbp; 8256 ASSERT(bp != NULL); 8257 VHCI_DEBUG(4, (CE_WARN, NULL, 8258 "vhci_uscsi_iodone: enter: bp: %p pkt: %p scmd: %p", 8259 (void *)bp, (void *)pkt, (void *)mp_uscmdp)); 8260 /* Save the status and the residual into the uscsi_cmd struct */ 8261 uscmdp->uscsi_status = ((*(pkt)->pkt_scbp) & STATUS_MASK); 8262 uscmdp->uscsi_resid = bp->b_resid; 8263 8264 /* return on a very successful command */ 8265 if (pkt->pkt_reason == CMD_CMPLT && 8266 SCBP_C(pkt) == 0 && ((pkt->pkt_flags & FLAG_SENSING) == 0) && 8267 pkt->pkt_resid == 0) { 8268 mdi_pi_kstat_iosupdate(mp_uscmdp->pip, bp); 8269 scsi_destroy_pkt(pkt); 8270 biodone(bp); 8271 return; 8272 } 8273 VHCI_DEBUG(4, (CE_NOTE, NULL, "iodone: reason=0x%x " 8274 " pkt_resid=%ld pkt_state: 0x%x b_count: %ld b_resid: %ld", 8275 pkt->pkt_reason, pkt->pkt_resid, 8276 pkt->pkt_state, bp->b_bcount, bp->b_resid)); 8277 8278 err = EIO; 8279 8280 arqstat = (struct scsi_arq_status *)(intptr_t)(pkt->pkt_scbp); 8281 if (pkt->pkt_reason != CMD_CMPLT) { 8282 /* 8283 * The command did not complete. 8284 */ 8285 VHCI_DEBUG(4, (CE_NOTE, NULL, 8286 "vhci_uscsi_iodone: command did not complete." 8287 " reason: %x flag: %x", pkt->pkt_reason, pkt->pkt_flags)); 8288 if (pkt->pkt_flags & FLAG_SENSING) { 8289 MDI_PI_ERRSTAT(mp_uscmdp->pip, MDI_PI_TRANSERR); 8290 } else if (pkt->pkt_reason == CMD_TIMEOUT) { 8291 MDI_PI_ERRSTAT(mp_uscmdp->pip, MDI_PI_HARDERR); 8292 err = ETIMEDOUT; 8293 } 8294 } else if (pkt->pkt_state & STATE_ARQ_DONE && mp_uscmdp->arq_enabled) { 8295 /* 8296 * The auto-rqsense happened, and the packet has a filled-in 8297 * scsi_arq_status structure, pointed to by pkt_scbp. 8298 */ 8299 VHCI_DEBUG(4, (CE_NOTE, NULL, 8300 "vhci_uscsi_iodone: received auto-requested sense")); 8301 if (uscmdp->uscsi_flags & USCSI_RQENABLE) { 8302 /* get the amount of data to copy into rqbuf */ 8303 int rqlen = SENSE_LENGTH - arqstat->sts_rqpkt_resid; 8304 rqlen = min(((int)uscmdp->uscsi_rqlen), rqlen); 8305 uscmdp->uscsi_rqresid = uscmdp->uscsi_rqlen - rqlen; 8306 uscmdp->uscsi_rqstatus = 8307 *((char *)&arqstat->sts_rqpkt_status); 8308 if (uscmdp->uscsi_rqbuf && uscmdp->uscsi_rqlen && 8309 rqlen != 0) { 8310 bcopy(&(arqstat->sts_sensedata), 8311 uscmdp->uscsi_rqbuf, rqlen); 8312 } 8313 mdi_pi_kstat_iosupdate(mp_uscmdp->pip, bp); 8314 VHCI_DEBUG(4, (CE_NOTE, NULL, 8315 "vhci_uscsi_iodone: ARQ " 8316 "uscsi_rqstatus=0x%x uscsi_rqresid=%d rqlen: %d " 8317 "xfer: %d rqpkt_resid: %d\n", 8318 uscmdp->uscsi_rqstatus, uscmdp->uscsi_rqresid, 8319 uscmdp->uscsi_rqlen, rqlen, 8320 arqstat->sts_rqpkt_resid)); 8321 } 8322 } else if (pkt->pkt_flags & FLAG_SENSING) { 8323 struct buf *rqbp; 8324 struct scsi_status *rqstatus; 8325 8326 rqstatus = (struct scsi_status *)pkt->pkt_scbp; 8327 /* a manual request sense was done - get the information */ 8328 if (uscmdp->uscsi_flags & USCSI_RQENABLE) { 8329 int rqlen = SENSE_LENGTH - pkt->pkt_resid; 8330 8331 rqbp = mp_uscmdp->rqbp; 8332 /* get the amount of data to copy into rqbuf */ 8333 rqlen = min(((int)uscmdp->uscsi_rqlen), rqlen); 8334 uscmdp->uscsi_rqresid = uscmdp->uscsi_rqlen - rqlen; 8335 uscmdp->uscsi_rqstatus = *((char *)rqstatus); 8336 if (uscmdp->uscsi_rqlen && uscmdp->uscsi_rqbuf) { 8337 bcopy(rqbp->b_un.b_addr, uscmdp->uscsi_rqbuf, 8338 rqlen); 8339 } 8340 MDI_PI_ERRSTAT(mp_uscmdp->pip, MDI_PI_TRANSERR); 8341 scsi_free_consistent_buf(rqbp); 8342 } 8343 VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_uscsi_iodone: FLAG_SENSING" 8344 "uscsi_rqstatus=0x%x uscsi_rqresid=%d\n", 8345 uscmdp->uscsi_rqstatus, uscmdp->uscsi_rqresid)); 8346 } else { 8347 struct scsi_status *status = 8348 (struct scsi_status *)pkt->pkt_scbp; 8349 /* 8350 * Command completed and we're not getting sense. Check for 8351 * errors and decide what to do next. 8352 */ 8353 VHCI_DEBUG(4, (CE_NOTE, NULL, 8354 "vhci_uscsi_iodone: command appears complete: reason: %x", 8355 pkt->pkt_reason)); 8356 if (status->sts_chk) { 8357 /* need to manually get the request sense */ 8358 if (vhci_uscsi_send_sense(pkt, mp_uscmdp) == 0) { 8359 scsi_destroy_pkt(pkt); 8360 return; 8361 } 8362 } else { 8363 VHCI_DEBUG(4, (CE_NOTE, NULL, 8364 "vhci_chk_err: appears complete")); 8365 err = 0; 8366 mdi_pi_kstat_iosupdate(mp_uscmdp->pip, bp); 8367 if (pkt->pkt_resid) { 8368 bp->b_resid += pkt->pkt_resid; 8369 } 8370 } 8371 } 8372 8373 if (err) { 8374 if (bp->b_resid == 0) 8375 bp->b_resid = bp->b_bcount; 8376 bioerror(bp, err); 8377 bp->b_flags |= B_ERROR; 8378 } 8379 8380 scsi_destroy_pkt(pkt); 8381 biodone(bp); 8382 8383 VHCI_DEBUG(4, (CE_WARN, NULL, "vhci_uscsi_iodone: exit")); 8384 } 8385 8386 /* 8387 * start routine for the mpapi uscsi command 8388 */ 8389 int 8390 vhci_uscsi_iostart(struct buf *bp) 8391 { 8392 struct scsi_pkt *pkt; 8393 struct uscsi_cmd *uscmdp; 8394 mp_uscsi_cmd_t *mp_uscmdp; 8395 int stat_size, rval; 8396 int retry = 0; 8397 8398 ASSERT(bp->b_private != NULL); 8399 8400 mp_uscmdp = (mp_uscsi_cmd_t *)bp->b_private; 8401 uscmdp = mp_uscmdp->uscmdp; 8402 if (uscmdp->uscsi_flags & USCSI_RQENABLE) { 8403 stat_size = SENSE_LENGTH; 8404 } else { 8405 stat_size = 1; 8406 } 8407 8408 pkt = scsi_init_pkt(mp_uscmdp->ap, NULL, bp, uscmdp->uscsi_cdblen, 8409 stat_size, 0, 0, SLEEP_FUNC, NULL); 8410 if (pkt == NULL) { 8411 VHCI_DEBUG(4, (CE_NOTE, NULL, 8412 "vhci_uscsi_iostart: rval: EINVAL")); 8413 bp->b_resid = bp->b_bcount; 8414 uscmdp->uscsi_resid = bp->b_bcount; 8415 bioerror(bp, EINVAL); 8416 biodone(bp); 8417 return (EINVAL); 8418 } 8419 8420 pkt->pkt_time = uscmdp->uscsi_timeout; 8421 bcopy(uscmdp->uscsi_cdb, pkt->pkt_cdbp, (size_t)uscmdp->uscsi_cdblen); 8422 pkt->pkt_comp = vhci_uscsi_iodone; 8423 pkt->pkt_private = mp_uscmdp; 8424 if (uscmdp->uscsi_flags & USCSI_SILENT) 8425 pkt->pkt_flags |= FLAG_SILENT; 8426 if (uscmdp->uscsi_flags & USCSI_ISOLATE) 8427 pkt->pkt_flags |= FLAG_ISOLATE; 8428 if (uscmdp->uscsi_flags & USCSI_DIAGNOSE) 8429 pkt->pkt_flags |= FLAG_DIAGNOSE; 8430 if (uscmdp->uscsi_flags & USCSI_RENEGOT) { 8431 pkt->pkt_flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 8432 } 8433 VHCI_DEBUG(4, (CE_WARN, NULL, 8434 "vhci_uscsi_iostart: ap: %p pkt: %p pcdbp: %p uscmdp: %p" 8435 " ucdbp: %p pcdblen: %d bp: %p count: %ld pip: %p" 8436 " stat_size: %d", 8437 (void *)mp_uscmdp->ap, (void *)pkt, (void *)pkt->pkt_cdbp, 8438 (void *)uscmdp, (void *)uscmdp->uscsi_cdb, pkt->pkt_cdblen, 8439 (void *)bp, bp->b_bcount, (void *)mp_uscmdp->pip, stat_size)); 8440 8441 /* 8442 * NOTE: This code path is related to MPAPI uscsi(7I), so path 8443 * selection is not based on path_instance. 8444 */ 8445 if (scsi_pkt_allocated_correctly(pkt)) 8446 pkt->pkt_path_instance = 0; 8447 8448 while (((rval = scsi_transport(pkt)) == TRAN_BUSY) && 8449 retry < vhci_uscsi_retry_count) { 8450 delay(drv_usectohz(vhci_uscsi_delay)); 8451 retry++; 8452 } 8453 if (retry >= vhci_uscsi_retry_count) { 8454 VHCI_DEBUG(4, (CE_NOTE, NULL, 8455 "vhci_uscsi_iostart: tran_busy - retry: %d", retry)); 8456 } 8457 switch (rval) { 8458 case TRAN_ACCEPT: 8459 rval = 0; 8460 break; 8461 8462 default: 8463 VHCI_DEBUG(4, (CE_NOTE, NULL, 8464 "vhci_uscsi_iostart: rval: %d count: %ld res: %ld", 8465 rval, bp->b_bcount, bp->b_resid)); 8466 bp->b_resid = bp->b_bcount; 8467 uscmdp->uscsi_resid = bp->b_bcount; 8468 bioerror(bp, EIO); 8469 scsi_destroy_pkt(pkt); 8470 biodone(bp); 8471 rval = EIO; 8472 MDI_PI_ERRSTAT(mp_uscmdp->pip, MDI_PI_TRANSERR); 8473 break; 8474 } 8475 VHCI_DEBUG(4, (CE_NOTE, NULL, 8476 "vhci_uscsi_iostart: exit: rval: %d", rval)); 8477 return (rval); 8478 } 8479 8480 /* ARGSUSED */ 8481 static struct scsi_failover_ops * 8482 vhci_dev_fo(dev_info_t *vdip, struct scsi_device *psd, 8483 void **ctprivp, char **fo_namep) 8484 { 8485 struct scsi_failover_ops *sfo; 8486 char *sfo_name; 8487 char *override; 8488 struct scsi_failover *sf; 8489 8490 ASSERT(psd && psd->sd_inq); 8491 if ((psd == NULL) || (psd->sd_inq == NULL)) { 8492 VHCI_DEBUG(1, (CE_NOTE, NULL, 8493 "!vhci_dev_fo:return NULL no scsi_device or inquiry")); 8494 return (NULL); 8495 } 8496 8497 /* 8498 * Determine if device is supported under scsi_vhci, and select 8499 * failover module. 8500 * 8501 * See if there is a scsi_vhci.conf file override for this devices's 8502 * VID/PID. The following values can be returned: 8503 * 8504 * NULL If the NULL is returned then there is no scsi_vhci.conf 8505 * override. For NULL, we determine the failover_ops for 8506 * this device by checking the sfo_device_probe entry 8507 * point for each 'fops' module, in order. 8508 * 8509 * NOTE: Correct operation may depend on module ordering 8510 * of 'specific' (failover modules that are completely 8511 * VID/PID table based) to 'generic' (failover modules 8512 * that based on T10 standards like TPGS). Currently, 8513 * the value of 'ddi-forceload' in scsi_vhci.conf is used 8514 * to establish the module list and probe order. 8515 * 8516 * "NONE" If value "NONE" is returned then there is a 8517 * scsi_vhci.conf VID/PID override to indicate the device 8518 * should not be supported under scsi_vhci (even if there 8519 * is an 'fops' module supporting the device). 8520 * 8521 * "<other>" If another value is returned then that value is the 8522 * name of the 'fops' module that should be used. 8523 */ 8524 sfo = NULL; /* "NONE" */ 8525 override = scsi_get_device_type_string( 8526 "scsi-vhci-failover-override", vdip, psd); 8527 if (override == NULL) { 8528 /* NULL: default: select based on sfo_device_probe results */ 8529 for (sf = scsi_failover_table; sf->sf_mod; sf++) { 8530 if ((sf->sf_sfo == NULL) || 8531 sf->sf_sfo->sfo_device_probe(psd, psd->sd_inq, 8532 ctprivp) == SFO_DEVICE_PROBE_PHCI) 8533 continue; 8534 8535 /* found failover module, supported under scsi_vhci */ 8536 sfo = sf->sf_sfo; 8537 if (fo_namep && (*fo_namep == NULL)) { 8538 sfo_name = i_ddi_strdup(sfo->sfo_name, 8539 KM_SLEEP); 8540 *fo_namep = sfo_name; 8541 } 8542 break; 8543 } 8544 } else if (strcasecmp(override, "NONE")) { 8545 /* !"NONE": select based on driver.conf specified name */ 8546 for (sf = scsi_failover_table, sfo = NULL; sf->sf_mod; sf++) { 8547 if ((sf->sf_sfo == NULL) || 8548 (sf->sf_sfo->sfo_name == NULL) || 8549 strcmp(override, sf->sf_sfo->sfo_name)) 8550 continue; 8551 8552 /* 8553 * NOTE: If sfo_device_probe() has side-effects, 8554 * including setting *ctprivp, these are not going 8555 * to occur with override config. 8556 */ 8557 8558 /* found failover module, supported under scsi_vhci */ 8559 sfo = sf->sf_sfo; 8560 if (fo_namep && (*fo_namep == NULL)) { 8561 sfo_name = kmem_alloc(strlen("conf ") + 8562 strlen(sfo->sfo_name) + 1, KM_SLEEP); 8563 (void) sprintf(sfo_name, "conf %s", 8564 sfo->sfo_name); 8565 *fo_namep = sfo_name; 8566 } 8567 break; 8568 } 8569 } 8570 if (override) 8571 kmem_free(override, strlen(override) + 1); 8572 return (sfo); 8573 } 8574 8575 /* 8576 * Determine the device described by cinfo should be enumerated under 8577 * the vHCI or the pHCI - if there is a failover ops then device is 8578 * supported under vHCI. By agreement with SCSA cinfo is a pointer 8579 * to a scsi_device structure associated with a decorated pHCI probe node. 8580 */ 8581 /* ARGSUSED */ 8582 int 8583 vhci_is_dev_supported(dev_info_t *vdip, dev_info_t *pdip, void *cinfo) 8584 { 8585 struct scsi_device *psd = (struct scsi_device *)cinfo; 8586 8587 return (vhci_dev_fo(vdip, psd, NULL, NULL) ? MDI_SUCCESS : MDI_FAILURE); 8588 } 8589 8590 8591 #ifdef DEBUG 8592 extern struct scsi_key_strings scsi_cmds[]; 8593 8594 static char * 8595 vhci_print_scsi_cmd(char cmd) 8596 { 8597 char tmp[64]; 8598 char *cpnt; 8599 8600 cpnt = scsi_cmd_name(cmd, scsi_cmds, tmp); 8601 /* tmp goes out of scope on return and caller sees garbage */ 8602 if (cpnt == tmp) { 8603 cpnt = "Unknown Command"; 8604 } 8605 return (cpnt); 8606 } 8607 8608 extern uchar_t scsi_cdb_size[]; 8609 8610 static void 8611 vhci_print_cdb(dev_info_t *dip, uint_t level, char *title, uchar_t *cdb) 8612 { 8613 int len = scsi_cdb_size[CDB_GROUPID(cdb[0])]; 8614 char buf[256]; 8615 8616 if (level == CE_NOTE) { 8617 vhci_log(level, dip, "path cmd %s\n", 8618 vhci_print_scsi_cmd(*cdb)); 8619 return; 8620 } 8621 8622 (void) sprintf(buf, "%s for cmd(%s)", title, vhci_print_scsi_cmd(*cdb)); 8623 vhci_clean_print(dip, level, buf, cdb, len); 8624 } 8625 8626 static void 8627 vhci_clean_print(dev_info_t *dev, uint_t level, char *title, uchar_t *data, 8628 int len) 8629 { 8630 int i; 8631 int c; 8632 char *format; 8633 char buf[256]; 8634 uchar_t byte; 8635 8636 (void) sprintf(buf, "%s:\n", title); 8637 vhci_log(level, dev, "%s", buf); 8638 level = CE_CONT; 8639 for (i = 0; i < len; ) { 8640 buf[0] = 0; 8641 for (c = 0; c < 8 && i < len; c++, i++) { 8642 byte = (uchar_t)data[i]; 8643 if (byte < 0x10) 8644 format = "0x0%x "; 8645 else 8646 format = "0x%x "; 8647 (void) sprintf(&buf[(int)strlen(buf)], format, byte); 8648 } 8649 (void) sprintf(&buf[(int)strlen(buf)], "\n"); 8650 8651 vhci_log(level, dev, "%s\n", buf); 8652 } 8653 } 8654 #endif 8655 static void 8656 vhci_invalidate_mpapi_lu(struct scsi_vhci *vhci, scsi_vhci_lun_t *vlun) 8657 { 8658 char *svl_wwn; 8659 mpapi_item_list_t *ilist; 8660 mpapi_lu_data_t *ld; 8661 8662 if (vlun == NULL) { 8663 return; 8664 } else { 8665 svl_wwn = vlun->svl_lun_wwn; 8666 } 8667 8668 ilist = vhci->mp_priv->obj_hdr_list[MP_OBJECT_TYPE_MULTIPATH_LU]->head; 8669 8670 while (ilist != NULL) { 8671 ld = (mpapi_lu_data_t *)(ilist->item->idata); 8672 if ((ld != NULL) && (strncmp(ld->prop.name, svl_wwn, 8673 strlen(svl_wwn)) == 0)) { 8674 ld->valid = 0; 8675 VHCI_DEBUG(6, (CE_WARN, NULL, 8676 "vhci_invalidate_mpapi_lu: " 8677 "Invalidated LU(%s)", svl_wwn)); 8678 return; 8679 } 8680 ilist = ilist->next; 8681 } 8682 VHCI_DEBUG(6, (CE_WARN, NULL, "vhci_invalidate_mpapi_lu: " 8683 "Could not find LU(%s) to invalidate.", svl_wwn)); 8684 } 8685