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 2000 by Cisco Systems, Inc. All rights reserved. 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * 26 * iSCSI Software Initiator 27 */ 28 29 /* 30 * Framework interface routines for iSCSI 31 */ 32 #include "iscsi.h" /* main header */ 33 #include <sys/scsi/adapters/iscsi_if.h> /* ioctl interfaces */ 34 #include <sys/scsi/adapters/iscsi_protocol.h> 35 /* protocol structs and defines */ 36 37 #include "iscsi_targetparam.h" 38 #include "persistent.h" 39 #include <sys/scsi/adapters/iscsi_door.h> 40 #include <sys/dlpi.h> 41 #include <sys/utsname.h> 42 #include "isns_client.h" 43 #include "isns_protocol.h" 44 #include <sys/bootprops.h> 45 46 #define ISCSI_NAME_VERSION "iSCSI Initiator v-1.55" 47 48 #define MAX_GET_NAME_SIZE 1024 49 #define MAX_NAME_PROP_SIZE 256 50 #define UNDEFINED -1 51 52 /* 53 * +--------------------------------------------------------------------+ 54 * | iscsi globals | 55 * +--------------------------------------------------------------------+ 56 */ 57 void *iscsi_state; 58 kmutex_t iscsi_oid_mutex; 59 uint32_t iscsi_oid; 60 int iscsi_nop_delay = ISCSI_DEFAULT_NOP_DELAY; 61 int iscsi_rx_window = ISCSI_DEFAULT_RX_WINDOW; 62 int iscsi_rx_max_window = ISCSI_DEFAULT_RX_MAX_WINDOW; 63 64 extern ib_boot_prop_t *iscsiboot_prop; 65 66 /* 67 * +--------------------------------------------------------------------+ 68 * | iscsi.c prototypes | 69 * +--------------------------------------------------------------------+ 70 */ 71 static int iscsi_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, 72 void *arg, void **result); 73 static int iscsi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 74 static int iscsi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 75 76 /* scsi_tran prototypes */ 77 static int iscsi_tran_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip, 78 scsi_hba_tran_t *hba_tran, struct scsi_device *sd); 79 static int iscsi_tran_lun_probe(struct scsi_device *sd, int (*callback) ()); 80 static struct scsi_pkt *iscsi_tran_init_pkt(struct scsi_address *ap, 81 struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen, 82 int tgtlen, int flags, int (*callback) (), caddr_t arg); 83 static void iscsi_tran_lun_free(dev_info_t *hba_dip, dev_info_t *lun_dip, 84 scsi_hba_tran_t *hba_tran, struct scsi_device *sd); 85 static int iscsi_tran_start(struct scsi_address *ap, struct scsi_pkt *pkt); 86 static int iscsi_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt); 87 static int iscsi_tran_reset(struct scsi_address *ap, int level); 88 static int iscsi_tran_getcap(struct scsi_address *ap, char *cap, int whom); 89 static int iscsi_tran_setcap(struct scsi_address *ap, char *cap, 90 int value, int whom); 91 static void iscsi_tran_destroy_pkt(struct scsi_address *ap, 92 struct scsi_pkt *pkt); 93 static void iscsi_tran_dmafree(struct scsi_address *ap, 94 struct scsi_pkt *pkt); 95 static void iscsi_tran_sync_pkt(struct scsi_address *ap, 96 struct scsi_pkt *pkt); 97 static void iscsi_tran_sync_pkt(struct scsi_address *ap, 98 struct scsi_pkt *pkt); 99 static int iscsi_tran_reset_notify(struct scsi_address *ap, int flag, 100 void (*callback) (caddr_t), caddr_t arg); 101 static int iscsi_tran_bus_config(dev_info_t *parent, uint_t flags, 102 ddi_bus_config_op_t op, void *arg, dev_info_t **childp); 103 static int iscsi_tran_bus_unconfig(dev_info_t *parent, uint_t flags, 104 ddi_bus_config_op_t op, void *arg); 105 static int iscsi_tran_get_name(struct scsi_device *sd, char *name, int len); 106 static int iscsi_tran_get_bus_addr(struct scsi_device *sd, char *name, int len); 107 108 /* bus_ops prototypes */ 109 /* LINTED E_STATIC_UNUSED */ 110 static ddi_intrspec_t iscsi_get_intrspec(dev_info_t *dip, dev_info_t *rdip, 111 uint_t inumber); 112 /* LINTED E_STATIC_UNUSED */ 113 static int iscsi_add_intrspec(dev_info_t *dip, dev_info_t *rdip, 114 ddi_intrspec_t intrspec, ddi_iblock_cookie_t *iblock_cookiep, 115 ddi_idevice_cookie_t *idevice_cookiep, uint_t (*int_handler)(caddr_t 116 int_handler_arg), caddr_t int_handler_arg, int kind); 117 /* LINTED E_STATIC_UNUSED */ 118 static void iscsi_remove_intrspec(dev_info_t *dip, dev_info_t *rdip, 119 ddi_intrspec_t intrspec, ddi_iblock_cookie_t iblock_cookie); 120 /* LINTED E_STATIC_UNUSED */ 121 static int iscsi_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop, 122 void *arg, void *result); 123 124 /* cb_ops prototypes */ 125 static int iscsi_open(dev_t *devp, int flags, int otyp, cred_t *credp); 126 static int iscsi_close(dev_t dev, int flag, int otyp, cred_t *credp); 127 /* --- iscsi_ioctl is called by the discovery code so needs to be global --- */ 128 int iscsi_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 129 cred_t *credp, int *rvalp); 130 131 int iscsi_get_persisted_param(uchar_t *name, 132 iscsi_param_get_t *ipgp, 133 iscsi_login_params_t *params); 134 static void iscsi_override_target_default(iscsi_hba_t *ihp, 135 iscsi_param_get_t *ipg); 136 137 /* scsi_tran helpers */ 138 static int iscsi_virt_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip, 139 scsi_hba_tran_t *hba_tran, struct scsi_device *sd); 140 static int iscsi_phys_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip, 141 scsi_hba_tran_t *hba_tran, struct scsi_device *sd); 142 static int iscsi_i_commoncap(struct scsi_address *ap, char *cap, 143 int val, int lunonly, int doset); 144 static void iscsi_get_name_to_iqn(char *name, int name_max_len); 145 static void iscsi_get_name_from_iqn(char *name, int name_max_len); 146 static boolean_t iscsi_cmp_boot_sess_oid(iscsi_hba_t *ihp, uint32_t oid); 147 148 /* struct helpers prototypes */ 149 150 /* 151 * At this point this driver doesn't need this structure because nothing 152 * is done during the open, close or ioctl. Code put in place because 153 * some admin related work might be done in the ioctl routine. 154 */ 155 static struct cb_ops iscsi_cb_ops = { 156 iscsi_open, /* open */ 157 iscsi_close, /* close */ 158 nodev, /* strategy */ 159 nodev, /* print */ 160 nodev, /* dump */ 161 nodev, /* read */ 162 nodev, /* write */ 163 iscsi_ioctl, /* ioctl */ 164 nodev, /* devmap */ 165 nodev, /* mmap */ 166 nodev, /* segmap */ 167 nochpoll, /* poll */ 168 ddi_prop_op, /* prop_op */ 169 NULL, /* streamtab */ 170 D_NEW | D_MP | D_HOTPLUG, /* flags */ 171 CB_REV, /* cb_rev */ 172 nodev, /* aread */ 173 nodev, /* awrite */ 174 }; 175 176 static struct dev_ops iscsi_dev_ops = { 177 DEVO_REV, /* devo_rev */ 178 0, /* refcnt */ 179 iscsi_getinfo, /* getinfo */ 180 nulldev, /* identify */ 181 nulldev, /* probe */ 182 iscsi_attach, /* attach */ 183 iscsi_detach, /* detach */ 184 nodev, /* reset */ 185 &iscsi_cb_ops, /* driver operations */ 186 NULL, /* bus ops */ 187 NULL, /* power management */ 188 }; 189 190 static struct modldrv modldrv = { 191 &mod_driverops, /* drv_modops */ 192 ISCSI_NAME_VERSION, /* drv_linkinfo */ 193 &iscsi_dev_ops /* drv_dev_ops */ 194 }; 195 196 static struct modlinkage modlinkage = { 197 MODREV_1, /* ml_rev */ 198 &modldrv, /* ml_linkage[] */ 199 NULL /* NULL termination */ 200 }; 201 202 /* 203 * This structure is bogus. scsi_hba_attach_setup() requires, as in the kernel 204 * will panic if you don't pass this in to the routine, this information. 205 * Need to determine what the actual impact to the system is by providing 206 * this information if any. Since dma allocation is done in pkt_init it may 207 * not have any impact. These values are straight from the Writing Device 208 * Driver manual. 209 */ 210 static ddi_dma_attr_t iscsi_dma_attr = { 211 DMA_ATTR_V0, /* ddi_dma_attr version */ 212 0, /* low address */ 213 0xffffffff, /* high address */ 214 0x00ffffff, /* counter upper bound */ 215 1, /* alignment requirements */ 216 0x3f, /* burst sizes */ 217 1, /* minimum DMA access */ 218 0xffffffff, /* maximum DMA access */ 219 (1 << 24) - 1, /* segment boundary restrictions */ 220 1, /* scater/gather list length */ 221 512, /* device granularity */ 222 0 /* DMA flags */ 223 }; 224 225 /* 226 * _init - General driver init entry 227 */ 228 int 229 _init(void) 230 { 231 int rval = 0; 232 233 iscsi_net_init(); 234 235 mutex_init(&iscsi_oid_mutex, NULL, MUTEX_DRIVER, NULL); 236 iscsi_oid = ISCSI_INITIATOR_OID; 237 238 /* 239 * Set up the soft state structures. If this driver is actually 240 * being attached to the system then we'll have at least one 241 * HBA/NIC used. 242 */ 243 rval = ddi_soft_state_init(&iscsi_state, 244 sizeof (iscsi_hba_t), 1); 245 if (rval != 0) { 246 iscsi_net_fini(); 247 goto init_done; 248 } 249 250 rval = scsi_hba_init(&modlinkage); 251 if (rval != 0) { 252 ddi_soft_state_fini(&iscsi_state); 253 iscsi_net_fini(); 254 goto init_done; 255 } 256 257 rval = mod_install(&modlinkage); 258 if (rval != 0) { 259 ddi_soft_state_fini(&iscsi_state); 260 scsi_hba_fini(&modlinkage); 261 iscsi_net_fini(); 262 goto init_done; 263 } 264 (void) iscsi_door_ini(); 265 266 init_done: 267 return (rval); 268 } 269 270 /* 271 * _fini - General driver destructor entry 272 */ 273 int 274 _fini(void) 275 { 276 int rval = 0; 277 278 rval = mod_remove(&modlinkage); 279 if (rval == 0) { 280 scsi_hba_fini(&modlinkage); 281 ddi_soft_state_fini(&iscsi_state); 282 mutex_destroy(&iscsi_oid_mutex); 283 (void) iscsi_door_term(); 284 iscsi_net_fini(); 285 } 286 return (rval); 287 } 288 289 /* 290 * _info - General driver info entry 291 */ 292 int 293 _info(struct modinfo *mp) 294 { 295 int rval = 0; 296 297 rval = mod_info(&modlinkage, mp); 298 299 return (rval); 300 } 301 302 303 /* 304 * +--------------------------------------------------------------------+ 305 * | Start of dev_ops routines | 306 * +--------------------------------------------------------------------+ 307 */ 308 309 /* 310 * iscsi_getinfo - returns general driver information 311 */ 312 /* ARGSUSED */ 313 static int 314 iscsi_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, 315 void *arg, void **result) 316 { 317 int rval = DDI_SUCCESS; 318 int instance = getminor((dev_t)arg); 319 iscsi_hba_t *ip; 320 321 switch (infocmd) { 322 case DDI_INFO_DEVT2DEVINFO: 323 if ((ip = ddi_get_soft_state(iscsi_state, instance)) == NULL) { 324 return (DDI_FAILURE); 325 } 326 *result = ip->hba_dip; 327 if (ip->hba_dip == NULL) 328 rval = DDI_FAILURE; 329 else 330 rval = DDI_SUCCESS; 331 break; 332 333 case DDI_INFO_DEVT2INSTANCE: 334 *result = (void *)(uintptr_t)instance; 335 rval = DDI_SUCCESS; 336 break; 337 338 default: 339 rval = DDI_FAILURE; 340 break; 341 } 342 return (rval); 343 } 344 345 346 /* 347 * iscsi_attach -- Attach instance of an iSCSI HBA. We 348 * will attempt to create our HBA and register it with 349 * scsi_vhci. If it's not possible to create the HBA 350 * or register with vhci we will fail the attach. 351 */ 352 static int 353 iscsi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 354 { 355 int rval = DDI_SUCCESS; 356 int instance = ddi_get_instance(dip); 357 iscsi_hba_t *ihp = NULL; 358 scsi_hba_tran_t *tran = NULL; 359 char init_port_name[MAX_NAME_PROP_SIZE]; 360 361 switch (cmd) { 362 case DDI_ATTACH: 363 /* create iSCSH HBA devctl device node */ 364 if (ddi_create_minor_node(dip, ISCSI_DEVCTL, S_IFCHR, 0, 365 DDI_PSEUDO, 0) == DDI_SUCCESS) { 366 367 /* allocate HBA soft state */ 368 if (ddi_soft_state_zalloc(iscsi_state, instance) != 369 DDI_SUCCESS) { 370 ddi_remove_minor_node(dip, NULL); 371 rval = DDI_FAILURE; 372 break; 373 } 374 375 /* get reference to soft state */ 376 if ((ihp = (iscsi_hba_t *)ddi_get_soft_state( 377 iscsi_state, instance)) == NULL) { 378 ddi_remove_minor_node(dip, NULL); 379 ddi_soft_state_free(iscsi_state, instance); 380 rval = DDI_FAILURE; 381 break; 382 } 383 384 /* init HBA mutex used to protect discovery events */ 385 mutex_init(&ihp->hba_discovery_events_mutex, NULL, 386 MUTEX_DRIVER, NULL); 387 388 /* 389 * init SendTargets semaphore that is used to allow 390 * only one operation at a time 391 */ 392 sema_init(&ihp->hba_sendtgts_semaphore, 1, NULL, 393 SEMA_DRIVER, NULL); 394 395 ihp->hba_sess_list = NULL; 396 rw_init(&ihp->hba_sess_list_rwlock, NULL, 397 RW_DRIVER, NULL); 398 399 /* allocate scsi_hba_tran */ 400 if ((tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP)) 401 == NULL) { 402 ddi_remove_minor_node(dip, NULL); 403 goto iscsi_attach_failed2; 404 } 405 406 /* soft state setup */ 407 ihp->hba_sig = ISCSI_SIG_HBA; 408 ihp->hba_tran = tran; 409 ihp->hba_dip = dip; 410 411 mutex_enter(&iscsi_oid_mutex); 412 ihp->hba_oid = iscsi_oid++; 413 mutex_exit(&iscsi_oid_mutex); 414 415 ihp->hba_name[0] = '\0'; 416 ihp->hba_name_length = 0; 417 ihp->hba_alias_length = 0; 418 ihp->hba_alias[0] = '\0'; 419 420 iscsi_net->tweaks.rcvbuf = ddi_prop_get_int( 421 DDI_DEV_T_ANY, ihp->hba_dip, 0, "so-rcvbuf", 422 ISCSI_SOCKET_RCVBUF_SIZE); 423 424 iscsi_net->tweaks.sndbuf = ddi_prop_get_int( 425 DDI_DEV_T_ANY, ihp->hba_dip, 0, "so-sndbuf", 426 ISCSI_SOCKET_SNDBUF_SIZE); 427 428 iscsi_net->tweaks.nodelay = ddi_prop_get_int( 429 DDI_DEV_T_ANY, ihp->hba_dip, 0, "tcp-nodelay", 430 ISCSI_TCP_NODELAY_DEFAULT); 431 432 iscsi_net->tweaks.conn_notify_threshold = 433 ddi_prop_get_int(DDI_DEV_T_ANY, 434 ihp->hba_dip, 0, "tcp-conn-notify-threshold", 435 ISCSI_TCP_CNOTIFY_THRESHOLD_DEFAULT); 436 437 iscsi_net->tweaks.conn_abort_threshold = 438 ddi_prop_get_int(DDI_DEV_T_ANY, ihp->hba_dip, 439 0, "tcp-conn-abort-threshold", 440 ISCSI_TCP_CABORT_THRESHOLD_DEFAULT); 441 442 iscsi_net->tweaks.abort_threshold = ddi_prop_get_int( 443 DDI_DEV_T_ANY, ihp->hba_dip, 0, 444 "tcp-abort-threshold", 445 ISCSI_TCP_ABORT_THRESHOLD_DEFAULT); 446 447 ihp->hba_config_storm_delay = ddi_prop_get_int( 448 DDI_DEV_T_ANY, ihp->hba_dip, 0, 449 "config-storm-delay", 450 ISCSI_CONFIG_STORM_DELAY_DEFAULT); 451 452 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 453 "so-rcvbuf", iscsi_net->tweaks.rcvbuf); 454 455 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 456 "so-sndbuf", iscsi_net->tweaks.sndbuf); 457 458 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 459 "tcp-nodelay", iscsi_net->tweaks.nodelay); 460 461 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 462 "tcp-conn-notify-threshold", 463 iscsi_net->tweaks.conn_notify_threshold); 464 465 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 466 "tcp-conn-abort-threshold", 467 iscsi_net->tweaks.conn_abort_threshold); 468 469 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 470 "tcp-abort-threshold", 471 iscsi_net->tweaks.abort_threshold); 472 473 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 474 "config-storm-delay", 475 ihp->hba_config_storm_delay); 476 477 /* setup hba defaults */ 478 iscsi_set_default_login_params(&ihp->hba_params); 479 480 /* hba set up */ 481 tran->tran_hba_private = ihp; 482 tran->tran_tgt_private = NULL; 483 tran->tran_tgt_init = iscsi_tran_lun_init; 484 tran->tran_tgt_probe = iscsi_tran_lun_probe; 485 tran->tran_tgt_free = iscsi_tran_lun_free; 486 tran->tran_start = iscsi_tran_start; 487 tran->tran_abort = iscsi_tran_abort; 488 tran->tran_reset = iscsi_tran_reset; 489 tran->tran_getcap = iscsi_tran_getcap; 490 tran->tran_setcap = iscsi_tran_setcap; 491 tran->tran_init_pkt = iscsi_tran_init_pkt; 492 tran->tran_destroy_pkt = iscsi_tran_destroy_pkt; 493 tran->tran_dmafree = iscsi_tran_dmafree; 494 tran->tran_sync_pkt = iscsi_tran_sync_pkt; 495 tran->tran_reset_notify = iscsi_tran_reset_notify; 496 tran->tran_bus_config = iscsi_tran_bus_config; 497 tran->tran_bus_unconfig = iscsi_tran_bus_unconfig; 498 499 tran->tran_get_name = iscsi_tran_get_name; 500 tran->tran_get_bus_addr = iscsi_tran_get_bus_addr; 501 tran->tran_interconnect_type = INTERCONNECT_ISCSI; 502 503 /* register scsi hba with scsa */ 504 if (scsi_hba_attach_setup(dip, &iscsi_dma_attr, 505 tran, SCSI_HBA_TRAN_CLONE) != DDI_SUCCESS) { 506 goto iscsi_attach_failed1; 507 } 508 509 /* register scsi hba with mdi (MPxIO/vhci) */ 510 if (mdi_phci_register(MDI_HCI_CLASS_SCSI, dip, 0) != 511 MDI_SUCCESS) { 512 ihp->hba_mpxio_enabled = B_FALSE; 513 } else { 514 ihp->hba_mpxio_enabled = B_TRUE; 515 } 516 517 (void) iscsi_hba_kstat_init(ihp); 518 519 /* Initialize targetparam list */ 520 iscsi_targetparam_init(); 521 522 /* Initialize ISID */ 523 ihp->hba_isid[0] = ISCSI_SUN_ISID_0; 524 ihp->hba_isid[1] = ISCSI_SUN_ISID_1; 525 ihp->hba_isid[2] = ISCSI_SUN_ISID_2; 526 ihp->hba_isid[3] = ISCSI_SUN_ISID_3; 527 ihp->hba_isid[4] = ISCSI_SUN_ISID_4; 528 ihp->hba_isid[5] = ISCSI_SUN_ISID_5; 529 530 /* Setup iSNS transport services and client */ 531 isns_client_init(); 532 533 /* 534 * initialize the discovery processes and 535 * persistent store. 536 */ 537 ihp->persistent_loaded = B_FALSE; 538 if (iscsid_init(ihp, B_FALSE) == B_FALSE) { 539 goto iscsi_attach_failed0; 540 } 541 542 /* Setup init_port_name for MPAPI */ 543 (void) snprintf(init_port_name, MAX_NAME_PROP_SIZE, 544 "%s,%02x%02x%02x%02x%02x%02x", 545 (char *)ihp->hba_name, ihp->hba_isid[0], 546 ihp->hba_isid[1], ihp->hba_isid[2], 547 ihp->hba_isid[3], ihp->hba_isid[4], 548 ihp->hba_isid[5]); 549 550 if (ddi_prop_update_string(DDI_DEV_T_NONE, dip, 551 "initiator-port", init_port_name) != 552 DDI_PROP_SUCCESS) { 553 cmn_err(CE_WARN, "iscsi_attach: Creating " 554 "initiator-port property on iSCSI " 555 "HBA(%s) with dip(%d) Failed", 556 (char *)ihp->hba_name, 557 ddi_get_instance(dip)); 558 } 559 560 ddi_report_dev(dip); 561 } else { 562 rval = DDI_FAILURE; 563 } 564 break; 565 566 iscsi_attach_failed0: 567 isns_client_cleanup(); 568 if (ihp->stats.ks) { 569 (void) iscsi_hba_kstat_term(ihp); 570 } 571 if (ihp->hba_mpxio_enabled == B_TRUE) { 572 (void) mdi_phci_unregister(dip, 0); 573 } 574 (void) scsi_hba_detach(ihp->hba_dip); 575 iscsi_attach_failed1: 576 ddi_remove_minor_node(dip, NULL); 577 ddi_prop_remove_all(ihp->hba_dip); 578 scsi_hba_tran_free(tran); 579 iscsi_attach_failed2: 580 mutex_destroy(&ihp->hba_discovery_events_mutex); 581 sema_destroy(&ihp->hba_sendtgts_semaphore); 582 rw_destroy(&ihp->hba_sess_list_rwlock); 583 ddi_soft_state_free(iscsi_state, instance); 584 rval = DDI_FAILURE; 585 break; 586 587 case DDI_RESUME: 588 break; 589 590 default: 591 rval = DDI_FAILURE; 592 } 593 594 if (rval != DDI_SUCCESS) { 595 cmn_err(CE_WARN, "iscsi driver unable to attach " 596 "hba instance %d", instance); 597 } 598 599 return (rval); 600 } 601 602 /* 603 * iscsi_detach - called on unload of hba instance 604 */ 605 static int 606 iscsi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 607 { 608 int rval = DDI_SUCCESS; 609 scsi_hba_tran_t *tran = NULL; 610 iscsi_hba_t *ihp = NULL; 611 iscsi_hba_t *ihp_check = NULL; 612 int instance; 613 char *init_node_name; 614 615 instance = ddi_get_instance(dip); 616 617 switch (cmd) { 618 case DDI_DETACH: 619 if (!(tran = (scsi_hba_tran_t *)ddi_get_driver_private(dip))) { 620 rval = DDI_SUCCESS; 621 break; 622 } 623 624 if ((ihp = (iscsi_hba_t *)tran->tran_hba_private) == NULL) { 625 rval = DDI_FAILURE; 626 break; 627 } 628 629 /* 630 * Validate that what is stored by the DDI framework is still 631 * the same state structure referenced by the SCSI framework 632 */ 633 ihp_check = ddi_get_soft_state(iscsi_state, instance); 634 if (ihp_check != ihp) { 635 rval = DDI_FAILURE; 636 break; 637 } 638 639 /* If a session exists we can't safely detach */ 640 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 641 if (ihp->hba_sess_list != NULL) { 642 rw_exit(&ihp->hba_sess_list_rwlock); 643 rval = DDI_FAILURE; 644 break; 645 } 646 rw_exit(&ihp->hba_sess_list_rwlock); 647 648 /* Disable all discovery services */ 649 if (iscsid_disable_discovery(ihp, 650 ISCSI_ALL_DISCOVERY_METHODS) == B_FALSE) { 651 /* Disable failed. Fail detach */ 652 rval = DDI_FAILURE; 653 break; 654 } 655 656 /* Deregister from iSNS server(s). */ 657 init_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 658 if (persistent_initiator_name_get(init_node_name, 659 ISCSI_MAX_NAME_LEN) == B_TRUE) { 660 if (strlen(init_node_name) > 0) { 661 (void) isns_dereg(ihp->hba_isid, 662 (uint8_t *)init_node_name); 663 } 664 } 665 kmem_free(init_node_name, ISCSI_MAX_NAME_LEN); 666 init_node_name = NULL; 667 668 /* Cleanup iSNS Client */ 669 isns_client_cleanup(); 670 671 iscsi_targetparam_cleanup(); 672 673 /* Cleanup iscsid resources */ 674 iscsid_fini(); 675 676 if (rval != DDI_SUCCESS) { 677 break; 678 } 679 /* kstat hba. destroy */ 680 KSTAT_DEC_HBA_CNTR_SESS(ihp); 681 682 if (ihp->hba_mpxio_enabled == B_TRUE) { 683 (void) mdi_phci_unregister(dip, 0); 684 } 685 ddi_remove_minor_node(dip, NULL); 686 687 ddi_prop_remove_all(ihp->hba_dip); 688 mutex_destroy(&ihp->hba_discovery_events_mutex); 689 rw_destroy(&ihp->hba_sess_list_rwlock); 690 (void) iscsi_hba_kstat_term(ihp); 691 692 (void) scsi_hba_detach(dip); 693 if (tran != NULL) { 694 scsi_hba_tran_free(tran); 695 } 696 ddi_soft_state_free(iscsi_state, instance); 697 break; 698 default: 699 break; 700 } 701 702 if (rval != DDI_SUCCESS) { 703 cmn_err(CE_WARN, "iscsi driver unable to " 704 "detach hba instance %d", instance); 705 } 706 707 return (rval); 708 } 709 710 /* 711 * +--------------------------------------------------------------------+ 712 * | End of dev_ops routines | 713 * +--------------------------------------------------------------------+ 714 */ 715 716 /* 717 * +--------------------------------------------------------------------+ 718 * | scsi_tran(9E) routines | 719 * +--------------------------------------------------------------------+ 720 */ 721 722 /* 723 * iscsi_tran_lun_init - Find target device based on SCSI device 724 * Based on the information given (SCSI device, target dev_info) find 725 * the target iSCSI device and put a pointer to that information in 726 * the scsi_hba_tran_t structure. 727 */ 728 static int 729 iscsi_tran_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip, 730 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 731 { 732 int rval = 0; 733 int type = 0; 734 735 ASSERT(hba_tran->tran_hba_private != NULL); 736 737 /* 738 * Child node is getting initialized. Look at the mpxio component 739 * type on the child device to see if this device is mpxio managed 740 * or not. 741 */ 742 type = mdi_get_component_type(lun_dip); 743 if (type != MDI_COMPONENT_CLIENT) { 744 rval = iscsi_phys_lun_init(hba_dip, lun_dip, hba_tran, sd); 745 } else { 746 rval = iscsi_virt_lun_init(hba_dip, lun_dip, hba_tran, sd); 747 } 748 749 return (rval); 750 } 751 752 /* 753 * iscsi_tran_lun_probe - This function didn't need to be implemented. 754 * We could have left NULL in the tran table. Since this isn't a 755 * performance path this seems safe. We are just wrappering the 756 * function so we can see the call go through if we have debugging 757 * enabled. 758 */ 759 static int 760 iscsi_tran_lun_probe(struct scsi_device *sd, int (*callback) ()) 761 { 762 int rval = 0; 763 764 rval = scsi_hba_probe(sd, callback); 765 766 return (rval); 767 } 768 769 /* 770 * iscsi_init_pkt - Allocate SCSI packet and fill in required info. 771 */ 772 /* ARGSUSED */ 773 static struct scsi_pkt * 774 iscsi_tran_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 775 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 776 int (*callback) (), caddr_t arg) 777 { 778 iscsi_lun_t *ilp; 779 iscsi_cmd_t *icmdp; 780 781 ASSERT(ap != NULL); 782 ASSERT(callback == NULL_FUNC || callback == SLEEP_FUNC); 783 784 /* 785 * The software stack doesn't have DMA which means the iSCSI 786 * protocol layer will be doing a bcopy from bp to outgoing 787 * streams buffers. Make sure that the buffer is mapped in 788 * so that the copy won't panic the system. 789 */ 790 if (bp && (bp->b_bcount != 0) && 791 bp_mapin_common(bp, (callback == NULL_FUNC) ? 792 VM_NOSLEEP : VM_SLEEP) == NULL) { 793 return (NULL); 794 } 795 796 ilp = (iscsi_lun_t *)ap->a_hba_tran->tran_tgt_private; 797 ASSERT(ilp != NULL); 798 799 if (pkt == NULL) { 800 pkt = scsi_hba_pkt_alloc(ilp->lun_sess->sess_hba->hba_dip, 801 ap, cmdlen, statuslen, tgtlen, sizeof (iscsi_cmd_t), 802 callback, arg); 803 if (pkt == NULL) { 804 return (NULL); 805 } 806 icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private; 807 icmdp->cmd_sig = ISCSI_SIG_CMD; 808 icmdp->cmd_state = ISCSI_CMD_STATE_FREE; 809 icmdp->cmd_lun = ilp; 810 icmdp->cmd_type = ISCSI_CMD_TYPE_SCSI; 811 /* add the report lun addressing type on to the lun */ 812 icmdp->cmd_un.scsi.lun = ilp->lun_addr_type << 14; 813 icmdp->cmd_un.scsi.lun = icmdp->cmd_un.scsi.lun | 814 ilp->lun_num; 815 icmdp->cmd_un.scsi.pkt = pkt; 816 icmdp->cmd_un.scsi.bp = bp; 817 icmdp->cmd_un.scsi.cmdlen = cmdlen; 818 icmdp->cmd_un.scsi.statuslen = statuslen; 819 icmdp->cmd_crc_error_seen = B_FALSE; 820 icmdp->cmd_misc_flags = 0; 821 if (flags & PKT_XARQ) { 822 icmdp->cmd_misc_flags |= ISCSI_CMD_MISCFLAG_XARQ; 823 } 824 825 mutex_init(&icmdp->cmd_mutex, NULL, MUTEX_DRIVER, NULL); 826 cv_init(&icmdp->cmd_completion, NULL, CV_DRIVER, NULL); 827 828 pkt->pkt_address = *ap; 829 pkt->pkt_comp = (void (*)())NULL; 830 pkt->pkt_flags = 0; 831 pkt->pkt_time = 0; 832 pkt->pkt_resid = 0; 833 pkt->pkt_statistics = 0; 834 pkt->pkt_reason = 0; 835 } 836 return (pkt); 837 } 838 839 /* 840 * iscsi_tran_lun_free - Free a SCSI LUN 841 */ 842 static void 843 iscsi_tran_lun_free(dev_info_t *hba_dip, dev_info_t *lun_dip, 844 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 845 { 846 iscsi_lun_t *ilp = NULL; 847 848 ASSERT(hba_dip != NULL); 849 ASSERT(lun_dip != NULL); 850 ASSERT(hba_tran != NULL); 851 ASSERT(sd != NULL); 852 ilp = (iscsi_lun_t *)hba_tran->tran_tgt_private; 853 ASSERT(ilp != NULL); 854 855 (void) mdi_prop_remove(ilp->lun_pip, NULL); 856 } 857 858 /* 859 * iscsi_start -- Start a SCSI transaction based on the packet 860 * This will attempt to add the icmdp to the pending queue 861 * for the connection and kick the queue. If the enqueue 862 * fails that means the queue is full. 863 */ 864 static int 865 iscsi_tran_start(struct scsi_address *ap, struct scsi_pkt *pkt) 866 { 867 iscsi_lun_t *ilp = NULL; 868 iscsi_sess_t *isp = NULL; 869 iscsi_cmd_t *icmdp = NULL; 870 uint_t flags; 871 872 ASSERT(ap != NULL); 873 ASSERT(pkt != NULL); 874 ilp = (iscsi_lun_t *)ap->a_hba_tran->tran_tgt_private; 875 isp = (iscsi_sess_t *)ilp->lun_sess; 876 icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private; 877 flags = pkt->pkt_flags; 878 ASSERT(ilp != NULL); 879 ASSERT(isp != NULL); 880 ASSERT(icmdp != NULL); 881 882 /* 883 * If the session is in the FREE state then 884 * all connections are down and retries have 885 * been exhausted. Fail command with fatal error. 886 */ 887 mutex_enter(&isp->sess_state_mutex); 888 if (isp->sess_state == ISCSI_SESS_STATE_FREE) { 889 mutex_exit(&isp->sess_state_mutex); 890 return (TRAN_FATAL_ERROR); 891 } 892 893 /* 894 * If the session is not in LOGGED_IN then we have 895 * no connections LOGGED_IN, but we haven't exhuasted 896 * our retries. Fail the command with busy so the 897 * caller might try again later. Once retries are 898 * exhausted the state machine will move us to FREE. 899 */ 900 if (isp->sess_state != ISCSI_SESS_STATE_LOGGED_IN) { 901 mutex_exit(&isp->sess_state_mutex); 902 return (TRAN_BUSY); 903 } 904 905 /* 906 * If we haven't received data from the target in the 907 * max specified period something is wrong with the 908 * transport. Fail IO with FATAL_ERROR. 909 */ 910 if (isp->sess_rx_lbolt + SEC_TO_TICK(iscsi_rx_max_window) < 911 ddi_get_lbolt()) { 912 mutex_exit(&isp->sess_state_mutex); 913 return (TRAN_FATAL_ERROR); 914 } 915 916 /* 917 * If we haven't received data from the target in the 918 * specified period something is probably wrong with 919 * the transport. Just return back BUSY until either 920 * the problem is resolved of the transport fails. 921 */ 922 if (isp->sess_rx_lbolt + SEC_TO_TICK(iscsi_rx_window) < 923 ddi_get_lbolt()) { 924 mutex_exit(&isp->sess_state_mutex); 925 return (TRAN_BUSY); 926 } 927 928 929 /* reset cmd values in case upper level driver is retrying cmd */ 930 icmdp->cmd_prev = icmdp->cmd_next = NULL; 931 icmdp->cmd_crc_error_seen = B_FALSE; 932 icmdp->cmd_lbolt_pending = icmdp->cmd_lbolt_active = 933 icmdp->cmd_lbolt_aborting = icmdp->cmd_lbolt_timeout = 934 (clock_t)NULL; 935 icmdp->cmd_itt = icmdp->cmd_ttt = 0; 936 icmdp->cmd_un.scsi.abort_icmdp = NULL; 937 938 mutex_enter(&isp->sess_queue_pending.mutex); 939 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp); 940 mutex_exit(&isp->sess_queue_pending.mutex); 941 mutex_exit(&isp->sess_state_mutex); 942 943 /* 944 * If this packet doesn't have FLAG_NOINTR set, it could have 945 * already run to completion (and the memory freed) at this 946 * point, so check our local copy of pkt_flags. Otherwise we 947 * have to wait for completion before returning to the caller. 948 */ 949 if (flags & FLAG_NOINTR) { 950 mutex_enter(&icmdp->cmd_mutex); 951 while ((icmdp->cmd_state != ISCSI_CMD_STATE_COMPLETED) || 952 (icmdp->cmd_un.scsi.r2t_icmdp != NULL) || 953 (icmdp->cmd_un.scsi.abort_icmdp != NULL)) { 954 cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex); 955 } 956 icmdp->cmd_state = ISCSI_CMD_STATE_FREE; 957 mutex_exit(&icmdp->cmd_mutex); 958 } 959 960 return (TRAN_ACCEPT); 961 } 962 963 /* 964 * iscsi_tran_abort - Called when an upper level application 965 * or driver wants to kill a scsi_pkt that was already sent to 966 * this driver. 967 */ 968 /* ARGSUSED */ 969 static int 970 iscsi_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt) 971 { 972 return (0); 973 } 974 975 /* 976 * iscsi_tran_reset - Reset target at either BUS, TARGET, or LUN 977 * level. This will require the issuing of a task management 978 * command down to the target/lun. 979 */ 980 static int 981 iscsi_tran_reset(struct scsi_address *ap, int level) 982 { 983 int rval = ISCSI_STATUS_INTERNAL_ERROR; 984 iscsi_sess_t *isp = NULL; 985 iscsi_lun_t *ilp = NULL; 986 987 ilp = (iscsi_lun_t *)ap->a_hba_tran->tran_tgt_private; 988 ASSERT(ilp != NULL); 989 isp = ilp->lun_sess; 990 ASSERT(isp != NULL); 991 992 switch (level) { 993 case RESET_LUN: 994 /* reset attempt will block until attempt is complete */ 995 rval = iscsi_handle_reset(isp, level, ilp); 996 break; 997 case RESET_BUS: 998 /* 999 * What are we going to realy reset the ethernet 1000 * network!? Just fall through to a target reset. 1001 */ 1002 case RESET_TARGET: 1003 /* reset attempt will block until attempt is complete */ 1004 rval = iscsi_handle_reset(isp, level, NULL); 1005 break; 1006 case RESET_ALL: 1007 default: 1008 break; 1009 } 1010 1011 return (ISCSI_SUCCESS(rval) ? 1 : 0); 1012 } 1013 1014 /* 1015 * iscsi_tran_getcap - Get target/lun capabilities. 1016 */ 1017 static int 1018 iscsi_tran_getcap(struct scsi_address *ap, char *cap, int whom) 1019 { 1020 return (iscsi_i_commoncap(ap, cap, 0, whom, 0)); 1021 } 1022 1023 1024 /* 1025 * iscsi_tran_setcap - Set target/lun capabilities. 1026 */ 1027 /* ARGSUSED */ 1028 static int 1029 iscsi_tran_setcap(struct scsi_address *ap, char *cap, int value, int whom) 1030 { 1031 return (iscsi_i_commoncap(ap, cap, 0, whom, 1)); 1032 } 1033 1034 1035 /* 1036 * iscsi_tran_destroy_pkt - Clean up packet 1037 */ 1038 static void 1039 iscsi_tran_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 1040 { 1041 iscsi_cmd_t *icmdp; 1042 1043 icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private; 1044 1045 ASSERT(icmdp != NULL); 1046 ASSERT(icmdp->cmd_sig == ISCSI_SIG_CMD); 1047 ASSERT(icmdp->cmd_state == ISCSI_CMD_STATE_FREE); 1048 1049 mutex_destroy(&icmdp->cmd_mutex); 1050 cv_destroy(&icmdp->cmd_completion); 1051 scsi_hba_pkt_free(ap, pkt); 1052 } 1053 1054 /* 1055 * iscsi_tran_dmafree - This is a software driver, NO DMA 1056 */ 1057 /* ARGSUSED */ 1058 static void 1059 iscsi_tran_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 1060 { 1061 /* 1062 * The iSCSI interface doesn't deal with DMA 1063 */ 1064 } 1065 1066 /* 1067 * iscsi_tran_sync_pkt - This is a software driver, NO DMA 1068 */ 1069 /* ARGSUSED */ 1070 static void 1071 iscsi_tran_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 1072 { 1073 /* 1074 * The iSCSI interface doesn't deal with DMA 1075 */ 1076 } 1077 1078 /* 1079 * iscsi_tran_reset_notify - We don't support BUS_RESET so there 1080 * is no point in support callback. 1081 */ 1082 /* ARGSUSED */ 1083 static int 1084 iscsi_tran_reset_notify(struct scsi_address *ap, int flag, 1085 void (*callback) (caddr_t), caddr_t arg) 1086 { 1087 1088 /* 1089 * We never do BUS_RESETS so allowing this call 1090 * back to register has no point? 1091 */ 1092 return (DDI_SUCCESS); 1093 } 1094 1095 1096 /* 1097 * iscsi_tran_bus_config - on demand device configuration 1098 * 1099 * iscsi_tran_bus_config is called by the NDI layer at the completion 1100 * of a dev_node creation. There are two primary cases defined in this 1101 * function. The first is BUS_CONFIG_ALL. In this case the NDI is trying 1102 * to identify that targets/luns are available configured at that point 1103 * in time. It is safe to just complete the process succcessfully. The 1104 * second case is a new case that was defined in S10 for devfs. BUS_CONFIG_ONE 1105 * this is to help driver the top down discovery instead of bottom up. If 1106 * we receive a BUS_CONFIG_ONE we should check to see if the <addr> exists 1107 * if so complete successfull processing. Otherwise we should call the 1108 * deamon and see if we can plumb the <addr>. If it is possible to plumb the 1109 * <addr> block until plumbing is complete. In both cases of being able to 1110 * plumb <addr> or not continue with successfull processing. 1111 */ 1112 static int 1113 iscsi_tran_bus_config(dev_info_t *parent, uint_t flags, 1114 ddi_bus_config_op_t op, void *arg, dev_info_t **childp) 1115 { 1116 int rval = NDI_SUCCESS; 1117 iscsi_hba_t *ihp = NULL; 1118 int iflags = flags; 1119 char *name = NULL; 1120 char *ptr = NULL; 1121 1122 /* get reference to soft state */ 1123 ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state, 1124 ddi_get_instance(parent)); 1125 if (ihp == NULL) { 1126 return (NDI_FAILURE); 1127 } 1128 1129 /* lock so only one config operation occrs */ 1130 sema_p(&iscsid_config_semaphore); 1131 1132 switch (op) { 1133 case BUS_CONFIG_ONE: 1134 /* parse target name out of name given */ 1135 if ((ptr = strchr((char *)arg, '@')) == NULL) { 1136 rval = NDI_FAILURE; 1137 break; 1138 } 1139 ptr++; /* move past '@' */ 1140 name = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP); 1141 (void) strncpy(name, ptr, MAX_GET_NAME_SIZE); 1142 /* We need to strip the LUN */ 1143 if ((ptr = strchr(name, ',')) == NULL) { 1144 rval = NDI_FAILURE; 1145 kmem_free(name, MAX_GET_NAME_SIZE); 1146 name = NULL; 1147 break; 1148 } 1149 /* We also need to strip the 4 bytes of hex TPGT */ 1150 ptr -= 4; 1151 if (ptr <= name) { 1152 rval = NDI_FAILURE; 1153 kmem_free(name, MAX_GET_NAME_SIZE); 1154 name = NULL; 1155 break; 1156 } 1157 *ptr = '\0'; /* NULL terminate */ 1158 1159 /* translate name back to original iSCSI name */ 1160 iscsi_get_name_to_iqn(name, MAX_GET_NAME_SIZE); 1161 1162 /* configure target, skip 4 byte ISID */ 1163 iscsid_config_one(ihp, (name+4), B_TRUE); 1164 1165 kmem_free(name, MAX_GET_NAME_SIZE); 1166 name = NULL; 1167 1168 /* 1169 * DDI group instructed us to use this flag. 1170 */ 1171 iflags |= NDI_MDI_FALLBACK; 1172 break; 1173 case BUS_CONFIG_DRIVER: 1174 /* FALLTHRU */ 1175 case BUS_CONFIG_ALL: 1176 iscsid_config_all(ihp, B_TRUE); 1177 break; 1178 default: 1179 rval = NDI_FAILURE; 1180 break; 1181 } 1182 1183 if (rval == NDI_SUCCESS) { 1184 rval = ndi_busop_bus_config(parent, iflags, 1185 op, arg, childp, 0); 1186 } 1187 sema_v(&iscsid_config_semaphore); 1188 1189 return (rval); 1190 } 1191 1192 /* 1193 * iscsi_tran_bus_unconfig - on demand device unconfiguration 1194 * 1195 * Called by the os framework under low resource situations. 1196 * It will attempt to unload our minor nodes (logical units 1197 * ndi/mdi nodes). 1198 */ 1199 static int 1200 iscsi_tran_bus_unconfig(dev_info_t *parent, uint_t flag, 1201 ddi_bus_config_op_t op, void *arg) 1202 { 1203 return (ndi_busop_bus_unconfig(parent, flag, op, arg)); 1204 } 1205 1206 1207 /* 1208 * iscsi_tran_get_name - create private /devices name for LUN 1209 * 1210 * This creates the <addr> in /devices/iscsi/<driver>@<addr> 1211 * path. For this <addr> we return the <session/target_name>,<lun num> 1212 * Where <target_name> is an <iqn/eui/...> as defined by the iSCSI 1213 * specification. We do modify the name slightly so that it still 1214 * complies with the IEEE <addr> naming scheme. This means that we 1215 * will substitute out the ':', '@', ... and other reserved characters 1216 * defined in the IEEE definition with '%<hex value of special char>' 1217 * This routine is indirectly called by iscsi_lun_create_xxx. These 1218 * calling routines must prevent the session and lun lists from changing 1219 * during this routine. 1220 */ 1221 static int 1222 iscsi_tran_get_name(struct scsi_device *sd, char *name, int len) 1223 { 1224 int target = 0; 1225 int lun = 0; 1226 iscsi_hba_t *ihp = NULL; 1227 iscsi_sess_t *isp = NULL; 1228 iscsi_lun_t *ilp = NULL; 1229 dev_info_t *lun_dip = NULL; 1230 1231 ASSERT(sd != NULL); 1232 ASSERT(name != NULL); 1233 lun_dip = sd->sd_dev; 1234 ASSERT(lun_dip != NULL); 1235 1236 /* get reference to soft state */ 1237 ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state, 1238 ddi_get_instance(ddi_get_parent(lun_dip))); 1239 if (ihp == NULL) { 1240 name[0] = '\0'; 1241 return (0); 1242 } 1243 1244 /* Get the target num */ 1245 target = ddi_prop_get_int(DDI_DEV_T_ANY, sd->sd_dev, 1246 DDI_PROP_DONTPASS, TARGET_PROP, 0); 1247 1248 /* Get the target num */ 1249 lun = ddi_prop_get_int(DDI_DEV_T_ANY, sd->sd_dev, 1250 DDI_PROP_DONTPASS, LUN_PROP, 0); 1251 1252 /* 1253 * Now we need to find our ilp by walking the lists 1254 * off the ihp and isp. 1255 */ 1256 /* See if we already created this session */ 1257 1258 /* Walk the HBA's session list */ 1259 for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) { 1260 /* compare target name as the unique identifier */ 1261 if (target == isp->sess_oid) { 1262 /* found match */ 1263 break; 1264 } 1265 } 1266 1267 /* If we found matching session continue searching for tgt */ 1268 if (isp == NULL) { 1269 /* sess not found */ 1270 name[0] = '\0'; 1271 return (0); 1272 } 1273 1274 /* 1275 * Search for the matching iscsi lun structure. We don't 1276 * need to hold the READER for the lun list at this point. 1277 * because the tran_get_name is being called from the online 1278 * function which is already holding a reader on the lun 1279 * list. 1280 */ 1281 for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) { 1282 if (lun == ilp->lun_num) { 1283 /* found match */ 1284 break; 1285 } 1286 } 1287 1288 if (ilp == NULL) { 1289 /* tgt not found */ 1290 name[0] = '\0'; 1291 return (0); 1292 } 1293 1294 /* Ensure enough space for lun_addr is available */ 1295 ASSERT(ilp->lun_addr != NULL); 1296 if ((strlen(ilp->lun_addr) + 1) > len) { 1297 return (0); 1298 } 1299 1300 /* copy lun_addr name */ 1301 (void) strcpy(name, ilp->lun_addr); 1302 1303 /* 1304 * Based on IEEE-1275 we can't have any ':', ' ', '@', or '/' 1305 * characters in our naming. So replace all those characters 1306 * with '-' 1307 */ 1308 iscsi_get_name_from_iqn(name, len); 1309 1310 return (1); 1311 } 1312 1313 /* 1314 * iscsi_tran_get_bus_addr - This returns a human readable string 1315 * for the bus address. Examining most other drivers fcp, etc. They 1316 * all just return the same string as tran_get_name. In our case 1317 * our tran get name is already some what usable so leave alone. 1318 */ 1319 static int 1320 iscsi_tran_get_bus_addr(struct scsi_device *sd, char *name, int len) 1321 { 1322 return (iscsi_tran_get_name(sd, name, len)); 1323 } 1324 1325 1326 /* 1327 * +--------------------------------------------------------------------+ 1328 * | End of scsi_tran routines | 1329 * +--------------------------------------------------------------------+ 1330 */ 1331 1332 /* 1333 * +--------------------------------------------------------------------+ 1334 * | Start of cb_ops routines | 1335 * +--------------------------------------------------------------------+ 1336 */ 1337 1338 /* 1339 * iscsi_open - Driver should be made IOCTL MT safe. Otherwise 1340 * this function needs updated. 1341 */ 1342 /* ARGSUSED */ 1343 static int 1344 iscsi_open(dev_t *devp, int flags, int otyp, cred_t *credp) 1345 { 1346 return (0); 1347 } 1348 1349 /* 1350 * iscsi_close - 1351 */ 1352 /* ARGSUSED */ 1353 static int 1354 iscsi_close(dev_t dev, int flags, int otyp, cred_t *credp) 1355 { 1356 return (0); 1357 } 1358 1359 /* 1360 * iscsi_ioctl - 1361 */ 1362 /* ARGSUSED */ 1363 int 1364 iscsi_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 1365 cred_t *credp, int *rvalp) 1366 { 1367 int rtn = 0; 1368 int instance = 0; 1369 int list_space = 0; 1370 int lun_sz = 0; 1371 int did; 1372 int retry; 1373 iscsi_hba_t *ihp = NULL; 1374 iscsi_sess_t *isp = NULL; 1375 iscsi_conn_t *icp = NULL; 1376 iscsi_login_params_t *params = NULL; 1377 iscsi_login_params_t *tmpParams = NULL; 1378 uchar_t *name = NULL; 1379 dev_info_t *lun_dip = NULL; 1380 1381 entry_t e; 1382 iscsi_oid_t oid; 1383 iscsi_property_t *ipp; 1384 iscsi_static_property_t *ispp; 1385 iscsi_param_get_t *ilg; 1386 iscsi_param_set_t *ils; 1387 iscsi_target_list_t idl, *idlp = NULL; 1388 iscsi_addr_list_t ial, *ialp = NULL; 1389 iscsi_chap_props_t *chap = NULL; 1390 iscsi_radius_props_t *radius = NULL; 1391 iscsi_auth_props_t *auth = NULL; 1392 iscsi_lun_list_t *ll, *llp = NULL; 1393 iscsi_lun_props_t *lun = NULL; 1394 iscsi_lun_t *ilp = NULL; 1395 iSCSIDiscoveryMethod_t method; 1396 iSCSIDiscoveryProperties_t discovery_props; 1397 iscsi_uscsi_t iu; 1398 iscsi_uscsi_t iu_caller; 1399 #ifdef _MULTI_DATAMODEL 1400 /* For use when a 32 bit app makes a call into a 64 bit ioctl */ 1401 iscsi_uscsi32_t iu32_caller; 1402 model_t model; 1403 #endif /* _MULTI_DATAMODEL */ 1404 void *void_p; 1405 iscsi_sendtgts_list_t *stl_hdr; 1406 iscsi_sendtgts_list_t *istl; 1407 int stl_sz; 1408 iscsi_target_entry_t *target; 1409 uint32_t old_oid; 1410 uint32_t target_oid; 1411 iscsi_targetparam_entry_t *curr_entry; 1412 char *initiator_node_name; 1413 char *initiator_node_alias; 1414 isns_portal_group_list_t *pg_list = NULL; 1415 isns_server_portal_group_list_t *server_pg_list_hdr = NULL; 1416 isns_server_portal_group_list_t *server_pg_list = NULL; 1417 int pg_list_sz, pg_sz_copy_out, server_pg_list_sz; 1418 iscsi_config_sess_t *ics; 1419 int size; 1420 boolean_t rval; 1421 char init_port_name[MAX_NAME_PROP_SIZE]; 1422 iscsi_sockaddr_t addr_dsc; 1423 iscsi_boot_property_t *bootProp; 1424 boolean_t discovered = B_TRUE; 1425 1426 instance = getminor(dev); 1427 ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state, instance); 1428 if (ihp == NULL) 1429 return (EFAULT); 1430 1431 switch (cmd) { 1432 /* 1433 * ISCSI_CREATE_OID - Create a Object IDentifier for a TargetName 1434 */ 1435 case ISCSI_CREATE_OID: 1436 if (ddi_copyin((caddr_t)arg, &oid, sizeof (oid), mode)) { 1437 rtn = EFAULT; 1438 break; 1439 } 1440 if (oid.o_vers != ISCSI_INTERFACE_VERSION) { 1441 rtn = EINVAL; 1442 break; 1443 } 1444 1445 /* Set the target that this session is associated with */ 1446 oid.o_oid = iscsi_targetparam_get_oid(oid.o_name); 1447 1448 if (ddi_copyout(&oid, (caddr_t)arg, sizeof (oid), mode)) { 1449 rtn = EFAULT; 1450 break; 1451 } 1452 break; 1453 /* 1454 * ISCSI_PARAM_GET - Get param for specified 1455 * connection/session. 1456 */ 1457 case ISCSI_PARAM_GET: 1458 /* copyin user args */ 1459 ilg = (iscsi_param_get_t *)kmem_alloc(sizeof (*ilg), KM_SLEEP); 1460 if (ddi_copyin((caddr_t)arg, ilg, sizeof (*ilg), mode)) { 1461 rtn = EFAULT; 1462 kmem_free(ilg, sizeof (*ilg)); 1463 break; 1464 } 1465 1466 if (ilg->g_vers != ISCSI_INTERFACE_VERSION) { 1467 rtn = EINVAL; 1468 kmem_free(ilg, sizeof (*ilg)); 1469 break; 1470 } 1471 1472 /* handle special case for Initiator name */ 1473 if (ilg->g_param == ISCSI_LOGIN_PARAM_INITIATOR_NAME) { 1474 (void) strlcpy((char *)ilg->g_value.v_name, 1475 (char *)ihp->hba_name, ISCSI_MAX_NAME_LEN); 1476 } else if (ilg->g_param == ISCSI_LOGIN_PARAM_INITIATOR_ALIAS) { 1477 if (ihp->hba_alias_length == 0) { 1478 rtn = EINVAL; 1479 } else { 1480 (void) strlcpy((char *)ilg->g_value.v_name, 1481 (char *)ihp->hba_alias, ISCSI_MAX_NAME_LEN); 1482 } 1483 } else { 1484 /* To describe the validity of the requested param */ 1485 boolean_t valid_flag = B_TRUE; 1486 1487 name = NULL; 1488 1489 /* 1490 * switch login based if looking for initiator 1491 * params 1492 */ 1493 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 1494 if (ilg->g_oid == ihp->hba_oid) { 1495 /* initiator */ 1496 params = &ihp->hba_params; 1497 name = ihp->hba_name; 1498 if (iscsi_get_persisted_param(name, 1499 ilg, params) != 0) { 1500 valid_flag = B_FALSE; 1501 } 1502 } else { 1503 /* 1504 * If the oid does represent a session check 1505 * to see if it is a target oid. If so, 1506 * return the target's associated session. 1507 */ 1508 rtn = iscsi_sess_get(ilg->g_oid, ihp, &isp); 1509 if (rtn != 0) { 1510 rtn = iscsi_sess_get_by_target( 1511 ilg->g_oid, ihp, &isp); 1512 } 1513 1514 /* 1515 * If rtn is zero then we have found an 1516 * existing session. Use the session name to 1517 * do param lookup. If rtn is non-zero then 1518 * create a targetparam object and use its name 1519 * for param lookup. 1520 */ 1521 if (rtn == 0) { 1522 name = isp->sess_name; 1523 params = &isp->sess_params; 1524 } else { 1525 name = 1526 iscsi_targetparam_get_name( 1527 ilg->g_oid); 1528 if (ilg->g_param_type == 1529 ISCSI_SESS_PARAM) { 1530 tmpParams = 1531 (iscsi_login_params_t *) 1532 kmem_alloc( 1533 sizeof (*tmpParams), 1534 KM_SLEEP); 1535 params = tmpParams; 1536 } 1537 rtn = 0; 1538 } 1539 1540 if (name == NULL) { 1541 rw_exit( 1542 &ihp->hba_sess_list_rwlock); 1543 rtn = EFAULT; 1544 kmem_free(ilg, sizeof (*ilg)); 1545 if (tmpParams != NULL) 1546 kmem_free(tmpParams, 1547 sizeof (*tmpParams)); 1548 1549 break; 1550 } 1551 1552 if (ilg->g_param_type == ISCSI_SESS_PARAM) { 1553 /* session */ 1554 /* 1555 * Update sess_params with the 1556 * latest params from the 1557 * persistent store. 1558 */ 1559 if (iscsi_get_persisted_param(name, 1560 ilg, params) != 0) { 1561 /* 1562 * If the parameter in 1563 * question is not 1564 * overriden, no effect 1565 * on existing session 1566 * parameters. However, 1567 * the parameter is 1568 * marked invalid 1569 * (from the standpoint 1570 * of whether it is 1571 * overriden). 1572 */ 1573 valid_flag = B_FALSE; 1574 } 1575 } else if (ilg->g_param_type == 1576 ISCSI_CONN_PARAM && isp != NULL) { 1577 /* connection */ 1578 rw_enter(&isp->sess_conn_list_rwlock, 1579 RW_READER); 1580 /* Assuming 1 conn per sess. */ 1581 /* 1582 * MC/S - Need to be modified to 1583 * take g_conn_cid into account when 1584 * we go multi-connection. 1585 */ 1586 if ((isp->sess_conn_act != NULL) && 1587 (isp->sess_conn_act->conn_state == 1588 ISCSI_CONN_STATE_LOGGED_IN)) { 1589 params = &(isp-> 1590 sess_conn_act-> 1591 conn_params); 1592 } else { 1593 valid_flag = B_FALSE; 1594 } 1595 rw_exit(&isp->sess_conn_list_rwlock); 1596 } 1597 } 1598 1599 /* make sure we have params to get info from */ 1600 if (params) { 1601 rtn = iscsi_get_param(params, valid_flag, ilg); 1602 1603 /* 1604 * for target parameters, check if any 1605 * parameters were overridden at the initiator 1606 * level. If so, then change the default value 1607 * to the initiator's overridden value 1608 */ 1609 if ((rtn == 0) && 1610 (ilg->g_oid != ihp->hba_oid)) { 1611 iscsi_override_target_default(ihp, 1612 ilg); 1613 } 1614 } 1615 rw_exit(&ihp->hba_sess_list_rwlock); 1616 } 1617 1618 if (rtn == 0) { 1619 rtn = ddi_copyout(ilg, (caddr_t)arg, 1620 sizeof (iscsi_param_get_t), mode); 1621 } 1622 kmem_free(ilg, sizeof (*ilg)); 1623 if (tmpParams != NULL) 1624 kmem_free(tmpParams, sizeof (*tmpParams)); 1625 break; 1626 1627 /* 1628 * ISCSI_INIT_NODE_NAME_SET - Change the initiator-node name for 1629 * the specified connection/session. 1630 */ 1631 case ISCSI_INIT_NODE_NAME_SET: 1632 /* copyin user args */ 1633 ils = (iscsi_param_set_t *)kmem_alloc(sizeof (*ils), KM_SLEEP); 1634 if (ddi_copyin((caddr_t)arg, ils, sizeof (*ils), mode)) { 1635 rtn = EFAULT; 1636 kmem_free(ils, sizeof (*ils)); 1637 break; 1638 } 1639 1640 if (ils->s_vers != ISCSI_INTERFACE_VERSION) { 1641 rtn = EINVAL; 1642 kmem_free(ils, sizeof (*ils)); 1643 break; 1644 } 1645 1646 /* saving off the old initiator-node name */ 1647 initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 1648 rval = persistent_initiator_name_get(initiator_node_name, 1649 ISCSI_MAX_NAME_LEN); 1650 1651 rtn = iscsi_set_params(ils, ihp, B_TRUE); 1652 kmem_free(ils, sizeof (*ils)); 1653 if (rtn != 0) { 1654 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 1655 return (rtn); 1656 } 1657 1658 (void) snprintf(init_port_name, MAX_NAME_PROP_SIZE, 1659 "%s,%02x%02x%02x%02x%02x%02x", 1660 (char *)ihp->hba_name, ihp->hba_isid[0], 1661 ihp->hba_isid[1], ihp->hba_isid[2], 1662 ihp->hba_isid[3], ihp->hba_isid[4], 1663 ihp->hba_isid[5]); 1664 1665 if (ddi_prop_update_string(DDI_DEV_T_NONE, 1666 ihp->hba_dip, "initiator-port", 1667 init_port_name) != DDI_PROP_SUCCESS) { 1668 cmn_err(CE_WARN, "iscsi_ioctl: Updating " 1669 "initiator-port property on iSCSI " 1670 "HBA(%s) with dip(%d) Failed", 1671 (char *)ihp->hba_name, 1672 ddi_get_instance(ihp->hba_dip)); 1673 } 1674 1675 /* 1676 * Deregister the old initiator-node name from the iSNS 1677 * server 1678 * Register the new initiator-node name with the iSNS server 1679 */ 1680 method = persistent_disc_meth_get(); 1681 if (method & iSCSIDiscoveryMethodISNS) { 1682 if (rval == B_TRUE) { 1683 if (strlen(initiator_node_name) > 0) { 1684 /* 1685 * we will attempt to offline the targets. 1686 * if logouts fail, we will still continue 1687 */ 1688 #define STRING_INNO "initiator-node name - Offline " 1689 #define STRING_FFOMD "failed for one or more devices" 1690 if ((iscsid_del( 1691 ihp, NULL, method, NULL)) 1692 != B_TRUE) { 1693 cmn_err(CE_NOTE, 1694 "Attempting to change " 1695 STRING_INNO 1696 STRING_FFOMD); 1697 } 1698 (void) isns_dereg(ihp->hba_isid, 1699 (uint8_t *)initiator_node_name); 1700 #undef STRING_INNO 1701 #undef STRING_FFOMD 1702 } 1703 } 1704 if (persistent_initiator_name_get(initiator_node_name, 1705 ISCSI_MAX_NAME_LEN) != B_TRUE) { 1706 kmem_free(initiator_node_name, 1707 ISCSI_MAX_NAME_LEN); 1708 initiator_node_name = NULL; 1709 rtn = EIO; 1710 break; 1711 } 1712 if (strlen(initiator_node_name) == 0) { 1713 kmem_free(initiator_node_name, 1714 ISCSI_MAX_NAME_LEN); 1715 initiator_node_name = NULL; 1716 rtn = EIO; 1717 break; 1718 } 1719 1720 initiator_node_alias = kmem_zalloc(ISCSI_MAX_NAME_LEN, 1721 KM_SLEEP); 1722 if (persistent_alias_name_get(initiator_node_alias, 1723 ISCSI_MAX_NAME_LEN) != B_TRUE) { 1724 initiator_node_alias[0] = '\0'; 1725 } 1726 1727 (void) isns_reg(ihp->hba_isid, 1728 (uint8_t *)initiator_node_name, 1729 ISCSI_MAX_NAME_LEN, 1730 (uint8_t *)initiator_node_alias, 1731 ISCSI_MAX_NAME_LEN, 1732 ISNS_INITIATOR_NODE_TYPE, 1733 isns_scn_callback); 1734 iscsid_do_isns_query(ihp); 1735 1736 /* Done using the name and alias - free them. */ 1737 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 1738 initiator_node_name = NULL; 1739 kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN); 1740 initiator_node_alias = NULL; 1741 } 1742 break; 1743 1744 /* 1745 * ISCSI_PARAM_SET - Set param for specified connection/session. 1746 */ 1747 case ISCSI_PARAM_SET: 1748 /* copyin user args */ 1749 ils = (iscsi_param_set_t *)kmem_alloc(sizeof (*ils), KM_SLEEP); 1750 if (ddi_copyin((caddr_t)arg, ils, sizeof (*ils), mode)) { 1751 rtn = EFAULT; 1752 kmem_free(ils, sizeof (*ils)); 1753 break; 1754 } 1755 1756 if (ils->s_vers != ISCSI_INTERFACE_VERSION) { 1757 rtn = EINVAL; 1758 kmem_free(ils, sizeof (*ils)); 1759 break; 1760 } 1761 rtn = iscsi_set_params(ils, ihp, B_TRUE); 1762 if (iscsiboot_prop) { 1763 if (iscsi_cmp_boot_sess_oid(ihp, ils->s_oid)) { 1764 /* 1765 * found active session for this object 1766 * or this is initiator's object 1767 * with mpxio enabled 1768 */ 1769 if (!iscsi_reconfig_boot_sess(ihp)) { 1770 rtn = EINVAL; 1771 kmem_free(ils, sizeof (*ils)); 1772 break; 1773 } 1774 } 1775 } 1776 kmem_free(ils, sizeof (*ils)); 1777 break; 1778 1779 /* 1780 * ISCSI_TARGET_PARAM_CLEAR 1781 * - remove custom parameter settings for a target. 1782 */ 1783 case ISCSI_TARGET_PARAM_CLEAR: 1784 if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) { 1785 rtn = EFAULT; 1786 break; 1787 } else if (e.e_vers != ISCSI_INTERFACE_VERSION) { 1788 rtn = EINVAL; 1789 break; 1790 } 1791 1792 if ((e.e_oid != ihp->hba_oid) && 1793 (e.e_oid != ISCSI_OID_NOTSET)) { 1794 uchar_t *t_name; 1795 iscsi_sess_t *t_isp; 1796 1797 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 1798 /* 1799 * If the oid does represent a session check to see 1800 * if it is a target oid. If so, return the target's 1801 * associated session. 1802 */ 1803 rtn = iscsi_sess_get(e.e_oid, ihp, &isp); 1804 if (rtn != 0) { 1805 rtn = iscsi_sess_get_by_target(e.e_oid, ihp, 1806 &isp); 1807 } 1808 1809 /* 1810 * If rtn is zero then we have found an 1811 * existing session. Use the session name to 1812 * do param lookup. If rtn is non-zero then 1813 * create a targetparam object and use its name 1814 * for param lookup. 1815 */ 1816 if (rtn == 0) { 1817 t_name = isp->sess_name; 1818 } else { 1819 t_name = iscsi_targetparam_get_name(e.e_oid); 1820 rtn = 0; 1821 } 1822 1823 if (t_name == NULL) { 1824 rw_exit(&ihp->hba_sess_list_rwlock); 1825 rtn = EFAULT; 1826 break; 1827 } 1828 1829 name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 1830 (void) strncpy((char *)name, (char *)t_name, 1831 ISCSI_MAX_NAME_LEN); 1832 1833 if (persistent_param_clear((char *)name) == B_FALSE) { 1834 kmem_free(name, ISCSI_MAX_NAME_LEN); 1835 rw_exit(&ihp->hba_sess_list_rwlock); 1836 rtn = EIO; 1837 break; 1838 } 1839 1840 ics = kmem_zalloc(sizeof (*ics), KM_SLEEP); 1841 ics->ics_ver = ISCSI_INTERFACE_VERSION; 1842 ics->ics_oid = ISCSI_INITIATOR_OID; 1843 ics->ics_in = 1; 1844 1845 /* 1846 * We may have multiple sessions with different 1847 * tpgt values. So we need to loop through 1848 * the sessions and update all sessions. 1849 */ 1850 for (isp = ihp->hba_sess_list; isp; 1851 isp = t_isp) { 1852 t_isp = isp->sess_next; 1853 1854 if (strncmp((char *)isp->sess_name, 1855 (char *)name, ISCSI_MAX_NAME_LEN) == 0) { 1856 /* 1857 * When removing target-params we need 1858 * slightly different actions depending 1859 * on if the session should still exist. 1860 * Get the initiator-node value for 1861 * MS/T. If there is no initiator 1862 * value then assume the default value 1863 * of 1. If the initiator value is 1864 * less than this ISID then we need to 1865 * destroy the session. Otherwise 1866 * update the session information and 1867 * resync (N7 event). 1868 */ 1869 rtn = iscsi_ioctl_get_config_sess( 1870 ihp, ics); 1871 if (((rtn != 0) && 1872 (isp->sess_isid[5] > 0)) || 1873 ((rtn == 0) && 1874 (ics->ics_out <= 1875 isp->sess_isid[5]))) { 1876 1877 /* 1878 * This session should no 1879 * longer exist. Remove 1880 * session. 1881 */ 1882 if (!ISCSI_SUCCESS( 1883 iscsi_sess_destroy(isp))) { 1884 kmem_free(ics, 1885 sizeof (*ics)); 1886 kmem_free(name, 1887 ISCSI_MAX_NAME_LEN); 1888 rw_exit(&ihp-> 1889 hba_sess_list_rwlock); 1890 rtn = EBUSY; 1891 break; 1892 } 1893 isp = ihp->hba_sess_list; 1894 } else { 1895 /* 1896 * Reset the session 1897 * parameters. 1898 */ 1899 bcopy(&(isp->sess_hba-> 1900 hba_params), 1901 &(isp->sess_params), 1902 sizeof (isp->sess_params)); 1903 if (iscsiboot_prop && 1904 isp->sess_boot) { 1905 /* 1906 * reconfig boot 1907 * session later 1908 */ 1909 continue; 1910 } 1911 /* 1912 * Notify the session that the 1913 * login parameters have 1914 * changed. 1915 */ 1916 mutex_enter(&isp-> 1917 sess_state_mutex); 1918 iscsi_sess_state_machine(isp, 1919 ISCSI_SESS_EVENT_N7); 1920 mutex_exit(&isp-> 1921 sess_state_mutex); 1922 } 1923 } 1924 } 1925 kmem_free(ics, sizeof (*ics)); 1926 kmem_free(name, ISCSI_MAX_NAME_LEN); 1927 rw_exit(&ihp->hba_sess_list_rwlock); 1928 if (iscsiboot_prop) { 1929 if (iscsi_cmp_boot_sess_oid(ihp, e.e_oid)) { 1930 /* 1931 * found active session for this object 1932 * or this is initiator object 1933 * with mpxio enabled 1934 */ 1935 if (!iscsi_reconfig_boot_sess(ihp)) { 1936 rtn = EINVAL; 1937 break; 1938 } 1939 } 1940 } 1941 } 1942 break; 1943 1944 /* 1945 * ISCSI_TARGET_OID_LIST_GET - 1946 */ 1947 case ISCSI_TARGET_OID_LIST_GET: 1948 /* copyin user args */ 1949 if (ddi_copyin((caddr_t)arg, &idl, 1950 sizeof (idl), mode)) { 1951 rtn = EFAULT; 1952 break; 1953 } 1954 1955 if (idl.tl_vers != ISCSI_INTERFACE_VERSION) { 1956 rtn = EINVAL; 1957 break; 1958 } 1959 1960 list_space = sizeof (iscsi_target_list_t); 1961 if (idl.tl_in_cnt != 0) 1962 list_space += (sizeof (uint32_t) * 1963 (idl.tl_in_cnt - 1)); 1964 1965 idlp = kmem_zalloc(list_space, KM_SLEEP); 1966 bcopy(&idl, idlp, sizeof (idl)); 1967 idlp->tl_out_cnt = 0; 1968 1969 /* 1970 * If target list type is ISCSI_TGT_OID_LIST and discovery 1971 * has not been completed or in progress, poke the discovery 1972 * methods so target information is returned 1973 */ 1974 mutex_enter(&ihp->hba_discovery_events_mutex); 1975 method = ihp->hba_discovery_events; 1976 if ((idl.tl_tgt_list_type == ISCSI_TGT_OID_LIST) && 1977 (method != ISCSI_ALL_DISCOVERY_METHODS) && 1978 (ihp->hba_discovery_in_progress == B_FALSE)) { 1979 ihp->hba_discovery_in_progress = B_TRUE; 1980 mutex_exit(&ihp->hba_discovery_events_mutex); 1981 iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodUnknown); 1982 mutex_enter(&ihp->hba_discovery_events_mutex); 1983 ihp->hba_discovery_in_progress = B_FALSE; 1984 } 1985 mutex_exit(&ihp->hba_discovery_events_mutex); 1986 1987 /* 1988 * Return the correct list information based on the type 1989 */ 1990 switch (idl.tl_tgt_list_type) { 1991 /* ISCSI_TGT_PARAM_OID_LIST - iscsiadm list target-params */ 1992 case ISCSI_TGT_PARAM_OID_LIST: 1993 /* get params from persistent store */ 1994 iscsi_targetparam_lock_list(RW_READER); 1995 curr_entry = iscsi_targetparam_get_next_entry(NULL); 1996 while (curr_entry != NULL) { 1997 if (idlp->tl_out_cnt < idlp->tl_in_cnt) { 1998 idlp->tl_oid_list[idlp->tl_out_cnt] = 1999 curr_entry->target_oid; 2000 } 2001 idlp->tl_out_cnt++; 2002 curr_entry = iscsi_targetparam_get_next_entry( 2003 curr_entry); 2004 } 2005 iscsi_targetparam_unlock_list(); 2006 break; 2007 2008 /* ISCSI_STATIC_TGT_OID_LIST - iscsiadm list static-config */ 2009 case ISCSI_STATIC_TGT_OID_LIST: 2010 { 2011 char *target_name = NULL; 2012 void *v = NULL; 2013 2014 /* get static-config from persistent store */ 2015 target_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 2016 persistent_static_addr_lock(); 2017 while (persistent_static_addr_next(&v, 2018 (char *)target_name, &e) == B_TRUE) { 2019 2020 if (idlp->tl_out_cnt < idlp->tl_in_cnt) { 2021 idlp->tl_oid_list[idlp->tl_out_cnt] = 2022 e.e_oid; 2023 } 2024 idlp->tl_out_cnt++; 2025 2026 } 2027 2028 persistent_static_addr_unlock(); 2029 kmem_free(target_name, ISCSI_MAX_NAME_LEN); 2030 break; 2031 } 2032 2033 /* ISCSI_TGT_OID_LIST - iscsiadm list target */ 2034 case ISCSI_TGT_OID_LIST: 2035 2036 /* get sessions from hba's session list */ 2037 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 2038 for (isp = ihp->hba_sess_list; isp; 2039 isp = isp->sess_next) { 2040 2041 if (((isp->sess_state != 2042 ISCSI_SESS_STATE_FREE) || 2043 (isp->sess_discovered_by != 2044 iSCSIDiscoveryMethodUnknown)) && 2045 (isp->sess_type == 2046 ISCSI_SESS_TYPE_NORMAL)) { 2047 if (idlp->tl_out_cnt < 2048 idlp->tl_in_cnt) { 2049 idlp->tl_oid_list[ 2050 idlp->tl_out_cnt] = 2051 isp->sess_oid; 2052 } 2053 idlp->tl_out_cnt++; 2054 } 2055 2056 } 2057 rw_exit(&ihp->hba_sess_list_rwlock); 2058 break; 2059 2060 default: 2061 ASSERT(FALSE); 2062 } 2063 2064 rtn = ddi_copyout(idlp, (caddr_t)arg, list_space, mode); 2065 kmem_free(idlp, list_space); 2066 break; 2067 2068 /* 2069 * ISCSI_TARGET_PROPS_GET - 2070 */ 2071 case ISCSI_TARGET_PROPS_GET: 2072 /* ---- fall through sense the code is almost the same ---- */ 2073 2074 /* 2075 * ISCSI_TARGET_PROPS_SET - 2076 */ 2077 case ISCSI_TARGET_PROPS_SET: 2078 /* copyin user args */ 2079 ipp = (iscsi_property_t *)kmem_alloc(sizeof (*ipp), 2080 KM_SLEEP); 2081 if (ddi_copyin((caddr_t)arg, ipp, sizeof (*ipp), mode)) { 2082 rtn = EFAULT; 2083 kmem_free(ipp, sizeof (*ipp)); 2084 break; 2085 } 2086 2087 if (ipp->p_vers != ISCSI_INTERFACE_VERSION) { 2088 rtn = EINVAL; 2089 kmem_free(ipp, sizeof (*ipp)); 2090 break; 2091 } 2092 2093 rtn = iscsi_target_prop_mod(ihp, ipp, cmd); 2094 if (rtn == 0) 2095 rtn = ddi_copyout(ipp, (caddr_t)arg, 2096 sizeof (*ipp), mode); 2097 kmem_free(ipp, sizeof (*ipp)); 2098 break; 2099 2100 /* 2101 * ISCSI_TARGET_ADDRESS_GET - 2102 */ 2103 case ISCSI_TARGET_ADDRESS_GET: 2104 if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) { 2105 rtn = EFAULT; 2106 break; 2107 } 2108 2109 if (ial.al_vers != ISCSI_INTERFACE_VERSION) { 2110 rtn = EINVAL; 2111 break; 2112 } 2113 2114 /* 2115 * Find out how much space we need to malloc for the users 2116 * request. 2117 */ 2118 list_space = sizeof (iscsi_addr_list_t); 2119 if (ial.al_in_cnt != 0) { 2120 list_space += (sizeof (iscsi_addr_t) * 2121 (ial.al_in_cnt - 1)); 2122 } 2123 ialp = (iscsi_addr_list_t *)kmem_zalloc(list_space, KM_SLEEP); 2124 2125 /* Copy in the header portion */ 2126 bcopy(&ial, ialp, sizeof (ial)); 2127 2128 /* session */ 2129 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 2130 rtn = iscsi_sess_get(ialp->al_oid, ihp, &isp); 2131 if (rtn != 0) { 2132 rw_exit(&ihp->hba_sess_list_rwlock); 2133 rtn = EFAULT; 2134 break; 2135 } 2136 2137 ialp->al_out_cnt = 0; 2138 ialp->al_tpgt = isp->sess_tpgt_conf; 2139 rw_enter(&isp->sess_conn_list_rwlock, RW_READER); 2140 for (icp = isp->sess_conn_list; icp; icp = icp->conn_next) { 2141 if (icp->conn_state != ISCSI_CONN_STATE_LOGGED_IN) { 2142 continue; 2143 } 2144 if (ialp->al_out_cnt < ialp->al_in_cnt) { 2145 iscsi_addr_t *ap; 2146 2147 ap = &ialp->al_addrs[ialp->al_out_cnt]; 2148 if (icp->conn_base_addr.sin.sa_family 2149 == AF_INET) { 2150 2151 struct sockaddr_in *addr_in = 2152 (struct sockaddr_in *)&icp-> 2153 conn_base_addr.sin4; 2154 ap->a_addr.i_insize = 2155 sizeof (struct in_addr); 2156 bcopy(&addr_in->sin_addr.s_addr, 2157 &ap->a_addr.i_addr.in4.s_addr, 2158 sizeof (struct in_addr)); 2159 ap->a_port = addr_in->sin_port; 2160 2161 } else { 2162 2163 struct sockaddr_in6 *addr_in6 = 2164 (struct sockaddr_in6 *)&icp-> 2165 conn_base_addr.sin6; 2166 ap->a_addr.i_insize = 2167 sizeof (struct in6_addr); 2168 bcopy(&addr_in6->sin6_addr.s6_addr, 2169 &ap->a_addr.i_addr.in6.s6_addr, 2170 sizeof (struct in6_addr)); 2171 ap->a_port = addr_in6->sin6_port; 2172 2173 } 2174 } 2175 ialp->al_out_cnt++; 2176 } 2177 rw_exit(&isp->sess_conn_list_rwlock); 2178 rw_exit(&ihp->hba_sess_list_rwlock); 2179 2180 rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode); 2181 kmem_free(ialp, list_space); 2182 break; 2183 2184 /* 2185 * ISCSI_CHAP_SET - 2186 */ 2187 case ISCSI_CHAP_SET: 2188 chap = (iscsi_chap_props_t *)kmem_zalloc(sizeof (*chap), 2189 KM_SLEEP); 2190 if (ddi_copyin((caddr_t)arg, chap, sizeof (*chap), mode)) { 2191 rtn = EFAULT; 2192 kmem_free(chap, sizeof (*chap)); 2193 break; 2194 } else if (chap->c_vers != ISCSI_INTERFACE_VERSION) { 2195 rtn = EINVAL; 2196 kmem_free(chap, sizeof (*chap)); 2197 break; 2198 } 2199 2200 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 2201 if (chap->c_oid == ihp->hba_oid) 2202 name = ihp->hba_name; 2203 else { 2204 rtn = iscsi_sess_get(chap->c_oid, ihp, &isp); 2205 if (rtn != 0) { 2206 rtn = iscsi_sess_get_by_target( 2207 chap->c_oid, ihp, &isp); 2208 } 2209 2210 /* 2211 * If rtn is zero then we have found an 2212 * existing session. Use the session name to 2213 * do param lookup. If rtn is non-zero then 2214 * create a targetparam object and use its name 2215 * for param lookup. 2216 */ 2217 if (rtn == 0) { 2218 name = isp->sess_name; 2219 } else { 2220 name = 2221 iscsi_targetparam_get_name(chap->c_oid); 2222 rtn = 0; 2223 } 2224 } 2225 2226 if (name == NULL) { 2227 rw_exit( 2228 &ihp->hba_sess_list_rwlock); 2229 rtn = EFAULT; 2230 kmem_free(chap, sizeof (*chap)); 2231 break; 2232 } 2233 2234 if (persistent_chap_set((char *)name, chap) == 2235 B_FALSE) { 2236 rtn = EIO; 2237 } 2238 rw_exit(&ihp->hba_sess_list_rwlock); 2239 kmem_free(chap, sizeof (*chap)); 2240 break; 2241 2242 /* 2243 * ISCSI_CHAP_GET - 2244 */ 2245 case ISCSI_CHAP_GET: 2246 chap = (iscsi_chap_props_t *)kmem_zalloc(sizeof (*chap), 2247 KM_SLEEP); 2248 if (ddi_copyin((caddr_t)arg, chap, sizeof (*chap), mode)) { 2249 kmem_free(chap, sizeof (*chap)); 2250 rtn = EFAULT; 2251 break; 2252 } else if (chap->c_vers != ISCSI_INTERFACE_VERSION) { 2253 kmem_free(chap, sizeof (*chap)); 2254 rtn = EINVAL; 2255 break; 2256 } 2257 2258 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 2259 if (chap->c_oid == ihp->hba_oid) 2260 name = ihp->hba_name; 2261 else { 2262 rtn = iscsi_sess_get(chap->c_oid, ihp, &isp); 2263 if (rtn != 0) { 2264 rtn = iscsi_sess_get_by_target( 2265 chap->c_oid, ihp, &isp); 2266 } 2267 2268 /* 2269 * If rtn is zero then we have found an 2270 * existing session. Use the session name to 2271 * do param lookup. If rtn is non-zero then 2272 * create a targetparam object and use its name 2273 * for param lookup. 2274 */ 2275 if (rtn == 0) { 2276 name = isp->sess_name; 2277 } else { 2278 rtn = 0; 2279 name = 2280 iscsi_targetparam_get_name(chap->c_oid); 2281 } 2282 2283 if (name == NULL) { 2284 rw_exit(&ihp->hba_sess_list_rwlock); 2285 rtn = EFAULT; 2286 break; 2287 } 2288 /* 2289 * Initialize the target-side chap name to the 2290 * session name if no chap settings have been 2291 * saved for the current session. 2292 */ 2293 if (persistent_chap_get((char *)name, 2294 chap) == B_FALSE) { 2295 int name_len = strlen((char *)name); 2296 iscsi_chap_props_t *chap = NULL; 2297 chap = (iscsi_chap_props_t *)kmem_zalloc 2298 (sizeof (iscsi_chap_props_t), KM_SLEEP); 2299 bcopy((char *)name, chap->c_user, name_len); 2300 chap->c_user_len = name_len; 2301 (void) (persistent_chap_set((char *)name, 2302 chap)); 2303 kmem_free(chap, sizeof (*chap)); 2304 } 2305 } 2306 2307 if (name == NULL) { 2308 rw_exit( 2309 &ihp->hba_sess_list_rwlock); 2310 rtn = EFAULT; 2311 break; 2312 } 2313 2314 if (persistent_chap_get((char *)name, chap) == B_FALSE) { 2315 rw_exit(&ihp->hba_sess_list_rwlock); 2316 rtn = EIO; 2317 break; 2318 } 2319 rw_exit(&ihp->hba_sess_list_rwlock); 2320 2321 rtn = ddi_copyout(chap, (caddr_t)arg, sizeof (*chap), mode); 2322 kmem_free(chap, sizeof (*chap)); 2323 break; 2324 2325 /* 2326 * ISCSI_CHAP_CLEAR - 2327 */ 2328 case ISCSI_CHAP_CLEAR: 2329 chap = (iscsi_chap_props_t *)kmem_zalloc(sizeof (*chap), 2330 KM_SLEEP); 2331 if (ddi_copyin((caddr_t)arg, chap, sizeof (*chap), mode)) { 2332 rtn = EFAULT; 2333 kmem_free(chap, sizeof (*chap)); 2334 break; 2335 } else if (chap->c_vers != ISCSI_INTERFACE_VERSION) { 2336 rtn = EINVAL; 2337 kmem_free(chap, sizeof (*chap)); 2338 break; 2339 } 2340 2341 if (chap->c_oid == ihp->hba_oid) { 2342 iscsi_sess_t *sessp; 2343 2344 name = ihp->hba_name; 2345 2346 if (persistent_chap_clear( 2347 (char *)name) == B_FALSE) { 2348 rtn = EIO; 2349 } 2350 2351 /* 2352 * Loop through all sessions and memset their 2353 * (initiator's) passwords 2354 */ 2355 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 2356 for (sessp = ihp->hba_sess_list; sessp; 2357 sessp = sessp->sess_next) { 2358 (void) memset(sessp->sess_auth.password, 2359 0, iscsiAuthStringMaxLength); 2360 sessp->sess_auth.password_length = 0; 2361 } 2362 rw_exit(&ihp->hba_sess_list_rwlock); 2363 2364 } else { 2365 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 2366 /* 2367 * If the oid does represent a session check to see 2368 * if it is a target oid. If so, return the target's 2369 * associated session. 2370 */ 2371 rtn = iscsi_sess_get(chap->c_oid, ihp, &isp); 2372 if (rtn != 0) { 2373 rtn = iscsi_sess_get_by_target(chap->c_oid, 2374 ihp, &isp); 2375 } 2376 2377 rw_exit(&ihp->hba_sess_list_rwlock); 2378 2379 /* 2380 * If rtn is zero then we have found an 2381 * existing session. Use the session name to 2382 * do param lookup. If rtn is non-zero then 2383 * create a targetparam object and use its name 2384 * for param lookup. 2385 */ 2386 if (rtn == 0) { 2387 name = isp->sess_name; 2388 } else { 2389 name = 2390 iscsi_targetparam_get_name(chap->c_oid); 2391 rtn = 0; 2392 } 2393 2394 if (name == NULL) { 2395 rw_exit( 2396 &ihp->hba_sess_list_rwlock); 2397 rtn = EFAULT; 2398 break; 2399 } 2400 2401 if (persistent_chap_clear( 2402 (char *)name) == B_FALSE) { 2403 rtn = EIO; 2404 } 2405 2406 /* 2407 * Clear out session chap password if we found a 2408 * session above. 2409 */ 2410 if (isp != NULL) { 2411 (void) memset(isp->sess_auth.password_in, 2412 0, iscsiAuthStringMaxLength); 2413 isp->sess_auth.password_length_in = 0; 2414 } 2415 2416 } 2417 2418 kmem_free(chap, sizeof (*chap)); 2419 break; 2420 2421 /* 2422 * ISCSI_STATIC_GET - 2423 */ 2424 case ISCSI_STATIC_GET: 2425 ispp = (iscsi_static_property_t *)kmem_alloc( 2426 sizeof (*ispp), KM_SLEEP); 2427 2428 if (ddi_copyin((caddr_t)arg, ispp, sizeof (*ispp), mode)) { 2429 rtn = EFAULT; 2430 kmem_free(ispp, sizeof (*ispp)); 2431 break; 2432 } 2433 2434 if (ispp->p_vers != ISCSI_INTERFACE_VERSION) { 2435 rtn = EINVAL; 2436 kmem_free(ispp, sizeof (*ispp)); 2437 break; 2438 } 2439 2440 { 2441 void *v = NULL; 2442 boolean_t found = B_FALSE; 2443 2444 persistent_static_addr_lock(); 2445 while (persistent_static_addr_next(&v, 2446 (char *)ispp->p_name, &e) == B_TRUE) { 2447 2448 if (ispp->p_oid == e.e_oid) { 2449 /* 2450 * In case there are multiple 2451 * addresses associated with the 2452 * given target OID, pick the first 2453 * one. 2454 */ 2455 iscsi_addr_t *ap; 2456 2457 ap = &(ispp->p_addr_list.al_addrs[0]); 2458 ap->a_port = e.e_port; 2459 ap->a_addr.i_insize = e.e_insize; 2460 bcopy(e.e_u.u_in6.s6_addr, 2461 ap->a_addr.i_addr.in6.s6_addr, 2462 e.e_insize); 2463 ispp->p_name_len = 2464 strlen((char *)ispp->p_name); 2465 ispp->p_addr_list.al_tpgt = e.e_tpgt; 2466 ispp->p_addr_list.al_out_cnt = 1; 2467 2468 found = B_TRUE; 2469 break; 2470 } 2471 } 2472 persistent_static_addr_unlock(); 2473 2474 if (found == B_TRUE) { 2475 rtn = ddi_copyout(ispp, (caddr_t)arg, 2476 sizeof (*ispp), mode); 2477 } else { 2478 rtn = ENOENT; 2479 } 2480 } 2481 kmem_free(ispp, sizeof (*ispp)); 2482 2483 break; 2484 2485 /* 2486 * ISCSI_STATIC_SET - 2487 */ 2488 case ISCSI_STATIC_SET: 2489 target = iscsi_ioctl_copyin((caddr_t)arg, mode, 2490 sizeof (*target)); 2491 if (target == NULL) { 2492 rtn = EFAULT; 2493 break; 2494 } 2495 2496 if (target->te_entry.e_vers != ISCSI_INTERFACE_VERSION) { 2497 kmem_free(target, sizeof (*target)); 2498 rtn = EINVAL; 2499 break; 2500 } 2501 2502 /* Check if the target's already been added */ 2503 { 2504 boolean_t static_target_found = B_FALSE; 2505 void *v = NULL; 2506 2507 name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 2508 persistent_static_addr_lock(); 2509 while (persistent_static_addr_next(&v, (char *)name, 2510 &e) == B_TRUE) { 2511 /* 2512 * MC/S - Need to check IP address and port 2513 * number as well when we support MC/S. 2514 */ 2515 if ((strncmp((char *)name, 2516 (char *)target->te_name, 2517 ISCSI_MAX_NAME_LEN) == 0) && 2518 (target->te_entry.e_tpgt == e.e_tpgt) && 2519 (target->te_entry.e_insize == e.e_insize) && 2520 (bcmp(&target->te_entry.e_u, &e.e_u, 2521 e.e_insize) == 0)) { 2522 /* 2523 * We don't allow MC/S for now but 2524 * we do allow adding the same target 2525 * with different TPGTs (hence, 2526 * different sessions). 2527 */ 2528 static_target_found = B_TRUE; 2529 break; 2530 } 2531 } 2532 persistent_static_addr_unlock(); 2533 kmem_free(name, ISCSI_MAX_NAME_LEN); 2534 2535 if (static_target_found == B_TRUE) { 2536 /* Duplicate entry */ 2537 kmem_free(target, sizeof (*target)); 2538 rtn = EEXIST; 2539 break; 2540 } 2541 } 2542 2543 if (target->te_entry.e_oid == ISCSI_OID_NOTSET) { 2544 mutex_enter(&iscsi_oid_mutex); 2545 target->te_entry.e_oid = iscsi_oid++; 2546 mutex_exit(&iscsi_oid_mutex); 2547 } 2548 2549 persistent_static_addr_lock(); 2550 if (persistent_static_addr_set((char *)target->te_name, 2551 &target->te_entry) == B_FALSE) { 2552 persistent_static_addr_unlock(); 2553 kmem_free(target, sizeof (*target)); 2554 rtn = EIO; 2555 break; 2556 } 2557 persistent_static_addr_unlock(); 2558 2559 /* 2560 * If Static Targets discovery is enabled, then add 2561 * target to discovery queue. Otherwise, just create 2562 * the session for potential future use. 2563 */ 2564 method = persistent_disc_meth_get(); 2565 if (method & iSCSIDiscoveryMethodStatic) { 2566 iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodStatic); 2567 (void) iscsid_login_tgt(ihp, (char *)target->te_name, 2568 iSCSIDiscoveryMethodStatic, NULL); 2569 } 2570 2571 rtn = iscsi_ioctl_copyout(target, sizeof (*target), 2572 (caddr_t)arg, mode); 2573 break; 2574 2575 /* 2576 * ISCSI_STATIC_CLEAR - 2577 */ 2578 case ISCSI_STATIC_CLEAR: 2579 if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) { 2580 rtn = EFAULT; 2581 break; 2582 } else if (e.e_vers != ISCSI_INTERFACE_VERSION) { 2583 rtn = EINVAL; 2584 break; 2585 } 2586 2587 { 2588 boolean_t found = B_FALSE; 2589 void *v = NULL; 2590 entry_t tmp_e; 2591 char *name = NULL; 2592 2593 name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 2594 2595 /* Find name for matching static_tgt oid */ 2596 persistent_static_addr_lock(); 2597 while (persistent_static_addr_next(&v, 2598 (char *)name, &tmp_e) == B_TRUE) { 2599 if (e.e_oid == tmp_e.e_oid) { 2600 found = B_TRUE; 2601 break; 2602 } 2603 } 2604 2605 /* If static_tgt found logout and remove it */ 2606 if (found == B_TRUE) { 2607 2608 iscsid_addr_to_sockaddr(tmp_e.e_insize, 2609 &tmp_e.e_u, tmp_e.e_port, &addr_dsc.sin); 2610 2611 /* Attempt to logout of target */ 2612 if (iscsid_del(ihp, (char *)name, 2613 iSCSIDiscoveryMethodStatic, &addr_dsc.sin) 2614 == B_TRUE) { 2615 persistent_static_addr_unlock(); 2616 2617 /* remove from persistent store */ 2618 if (persistent_static_addr_clear( 2619 e.e_oid) == B_FALSE) { 2620 rtn = EIO; 2621 } 2622 2623 iscsid_poke_discovery(ihp, 2624 iSCSIDiscoveryMethodStatic); 2625 (void) iscsid_login_tgt(ihp, 2626 (char *)name, 2627 iSCSIDiscoveryMethodStatic, 2628 NULL); 2629 2630 } else { 2631 persistent_static_addr_unlock(); 2632 rtn = EBUSY; 2633 } 2634 } else { 2635 persistent_static_addr_unlock(); 2636 rtn = EIO; 2637 } 2638 kmem_free(name, ISCSI_MAX_NAME_LEN); 2639 } 2640 break; 2641 2642 /* 2643 * ISCSI_ISNS_SERVER_ADDR_SET: 2644 */ 2645 case ISCSI_ISNS_SERVER_ADDR_SET: 2646 if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) { 2647 rtn = EFAULT; 2648 break; 2649 } else if (e.e_vers != ISCSI_INTERFACE_VERSION) { 2650 rtn = EINVAL; 2651 break; 2652 } 2653 2654 if (persistent_isns_addr_set(&e) == B_FALSE) { 2655 rtn = EIO; 2656 break; 2657 } 2658 2659 /* 2660 * If iSNS server discovery is enabled, then kickoff 2661 * discovery of the targets advertised by the recently 2662 * added iSNS server address. 2663 */ 2664 method = persistent_disc_meth_get(); 2665 if (method & iSCSIDiscoveryMethodISNS) { 2666 initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, 2667 KM_SLEEP); 2668 if (persistent_initiator_name_get(initiator_node_name, 2669 ISCSI_MAX_NAME_LEN) != B_TRUE) { 2670 kmem_free(initiator_node_name, 2671 ISCSI_MAX_NAME_LEN); 2672 initiator_node_name = NULL; 2673 rtn = EIO; 2674 break; 2675 } 2676 if (strlen(initiator_node_name) == 0) { 2677 kmem_free(initiator_node_name, 2678 ISCSI_MAX_NAME_LEN); 2679 initiator_node_name = NULL; 2680 rtn = EIO; 2681 break; 2682 } 2683 2684 initiator_node_alias = kmem_zalloc(ISCSI_MAX_NAME_LEN, 2685 KM_SLEEP); 2686 if (persistent_alias_name_get(initiator_node_alias, 2687 ISCSI_MAX_NAME_LEN) != B_TRUE) { 2688 initiator_node_alias[0] = '\0'; 2689 } 2690 2691 /* 2692 * Register this initiator node against this iSNS 2693 * server. 2694 */ 2695 (void) isns_reg_one_server(&e, ihp->hba_isid, 2696 (uint8_t *)initiator_node_name, 2697 ISCSI_MAX_NAME_LEN, 2698 (uint8_t *)initiator_node_alias, 2699 ISCSI_MAX_NAME_LEN, 2700 ISNS_INITIATOR_NODE_TYPE, 2701 isns_scn_callback); 2702 2703 iscsid_do_isns_query_one_server(ihp, &e); 2704 2705 iscsid_addr_to_sockaddr(e.e_insize, 2706 &e.e_u, e.e_port, &addr_dsc.sin); 2707 2708 (void) iscsid_login_tgt(ihp, NULL, 2709 iSCSIDiscoveryMethodISNS, 2710 &addr_dsc.sin); 2711 2712 /* Done using the name and alias - free them. */ 2713 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 2714 initiator_node_name = NULL; 2715 kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN); 2716 initiator_node_alias = NULL; 2717 } 2718 break; 2719 2720 /* 2721 * ISCSI_DISCOVERY_ADDR_SET: 2722 */ 2723 case ISCSI_DISCOVERY_ADDR_SET: 2724 if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) { 2725 rtn = EFAULT; 2726 break; 2727 } else if (e.e_vers != ISCSI_INTERFACE_VERSION) { 2728 rtn = EINVAL; 2729 break; 2730 } 2731 2732 if (e.e_oid == ISCSI_OID_NOTSET) { 2733 mutex_enter(&iscsi_oid_mutex); 2734 e.e_oid = iscsi_oid++; 2735 mutex_exit(&iscsi_oid_mutex); 2736 } 2737 2738 if (persistent_disc_addr_set(&e) == B_FALSE) { 2739 rtn = EIO; 2740 break; 2741 } 2742 2743 /* 2744 * If Send Targets discovery is enabled, then kickoff 2745 * discovery of the targets advertised by the recently 2746 * added discovery address. 2747 */ 2748 method = persistent_disc_meth_get(); 2749 if (method & iSCSIDiscoveryMethodSendTargets) { 2750 2751 iscsid_addr_to_sockaddr(e.e_insize, 2752 &e.e_u, e.e_port, &addr_dsc.sin); 2753 iscsid_do_sendtgts(&e); 2754 (void) iscsid_login_tgt(ihp, NULL, 2755 iSCSIDiscoveryMethodSendTargets, 2756 &addr_dsc.sin); 2757 2758 } 2759 break; 2760 2761 /* 2762 * ISCSI_DISCOVERY_ADDR_LIST_GET 2763 */ 2764 case ISCSI_DISCOVERY_ADDR_LIST_GET: 2765 /* copyin user args */ 2766 if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) { 2767 rtn = EFAULT; 2768 break; 2769 } 2770 2771 if (ial.al_vers != ISCSI_INTERFACE_VERSION) { 2772 rtn = EINVAL; 2773 break; 2774 } 2775 2776 list_space = sizeof (iscsi_addr_list_t); 2777 if (ial.al_in_cnt != 0) { 2778 list_space += (sizeof (iscsi_addr_t) * 2779 (ial.al_in_cnt - 1)); 2780 } 2781 2782 ialp = kmem_zalloc(list_space, KM_SLEEP); 2783 bcopy(&ial, ialp, sizeof (iscsi_addr_list_t)); 2784 2785 void_p = NULL; 2786 ialp->al_out_cnt = 0; 2787 persistent_disc_addr_lock(); 2788 while (persistent_disc_addr_next(&void_p, &e) == B_TRUE) { 2789 if (ialp->al_out_cnt < ialp->al_in_cnt) { 2790 int i = ialp->al_out_cnt; 2791 iscsi_addr_t *addr = &ialp->al_addrs[i]; 2792 2793 addr->a_port = e.e_port; 2794 addr->a_addr.i_insize = e.e_insize; 2795 addr->a_oid = e.e_oid; 2796 2797 if (e.e_insize == sizeof (struct in_addr)) { 2798 /* IPv4 */ 2799 addr->a_addr.i_addr.in4.s_addr = 2800 e.e_u.u_in4.s_addr; 2801 } else if (e.e_insize == 2802 sizeof (struct in6_addr)) { 2803 /* IPv6 */ 2804 bcopy(e.e_u.u_in6.s6_addr, 2805 addr->a_addr.i_addr.in6.s6_addr, 2806 16); 2807 } 2808 } 2809 ialp->al_out_cnt++; 2810 } 2811 persistent_disc_addr_unlock(); 2812 2813 rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode); 2814 kmem_free(ialp, list_space); 2815 break; 2816 2817 /* 2818 * ISCSI_ISNS_SERVER_ADDR_LIST_GET 2819 */ 2820 case ISCSI_ISNS_SERVER_ADDR_LIST_GET: 2821 /* copyin user args */ 2822 if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) { 2823 rtn = EFAULT; 2824 break; 2825 } 2826 2827 if (ial.al_vers != ISCSI_INTERFACE_VERSION) { 2828 rtn = EINVAL; 2829 break; 2830 } 2831 2832 list_space = sizeof (iscsi_addr_list_t); 2833 if (ial.al_in_cnt != 0) { 2834 list_space += (sizeof (iscsi_addr_t) * 2835 (ial.al_in_cnt - 1)); 2836 } 2837 2838 ialp = kmem_zalloc(list_space, KM_SLEEP); 2839 bcopy(&ial, ialp, sizeof (iscsi_addr_list_t)); 2840 2841 void_p = NULL; 2842 ialp->al_out_cnt = 0; 2843 persistent_isns_addr_lock(); 2844 while (persistent_isns_addr_next(&void_p, &e) == B_TRUE) { 2845 if (ialp->al_out_cnt < ialp->al_in_cnt) { 2846 int i = ialp->al_out_cnt; 2847 iscsi_addr_t *addr = &ialp->al_addrs[i]; 2848 2849 addr->a_port = e.e_port; 2850 addr->a_addr.i_insize = e.e_insize; 2851 if (e.e_insize == sizeof (struct in_addr)) { 2852 /* IPv4 */ 2853 addr->a_addr.i_addr.in4.s_addr = 2854 e.e_u.u_in4.s_addr; 2855 } else if (e.e_insize == 2856 sizeof (struct in6_addr)) { 2857 /* IPv6 */ 2858 bcopy(e.e_u.u_in6.s6_addr, 2859 addr->a_addr.i_addr.in6.s6_addr, 2860 16); 2861 } 2862 } 2863 ialp->al_out_cnt++; 2864 } 2865 persistent_isns_addr_unlock(); 2866 2867 rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode); 2868 kmem_free(ialp, list_space); 2869 break; 2870 2871 /* 2872 * ISCSI_DISCOVERY_ADDR_CLEAR: 2873 */ 2874 case ISCSI_DISCOVERY_ADDR_CLEAR: 2875 if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) { 2876 rtn = EFAULT; 2877 break; 2878 } else if (e.e_vers != ISCSI_INTERFACE_VERSION) { 2879 rtn = EINVAL; 2880 break; 2881 } 2882 2883 iscsid_addr_to_sockaddr(e.e_insize, 2884 &e.e_u, e.e_port, &addr_dsc.sin); 2885 2886 /* Attempt to logout of associated targets */ 2887 if (iscsid_del(ihp, NULL, 2888 iSCSIDiscoveryMethodSendTargets, &addr_dsc.sin) == 2889 B_TRUE) { 2890 /* Logout successful remove disc. addr. */ 2891 if (persistent_disc_addr_clear(&e) == B_FALSE) { 2892 rtn = EIO; 2893 } 2894 } else { 2895 rtn = EBUSY; 2896 } 2897 break; 2898 2899 /* 2900 * ISCSI_ISNS_SERVER_CLEAR: 2901 */ 2902 case ISCSI_ISNS_SERVER_ADDR_CLEAR: 2903 if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) { 2904 rtn = EFAULT; 2905 break; 2906 } else if (e.e_vers != ISCSI_INTERFACE_VERSION) { 2907 rtn = EINVAL; 2908 break; 2909 } 2910 2911 iscsid_addr_to_sockaddr(e.e_insize, 2912 &e.e_u, e.e_port, &addr_dsc.sin); 2913 2914 /* Attempt logout of associated targets */ 2915 if (iscsid_del(ihp, NULL, iSCSIDiscoveryMethodISNS, 2916 &addr_dsc.sin) == B_TRUE) { 2917 /* Logout successful */ 2918 2919 if (persistent_isns_addr_clear(&e) == B_FALSE) { 2920 rtn = EIO; 2921 break; 2922 } 2923 2924 method = persistent_disc_meth_get(); 2925 if (method & iSCSIDiscoveryMethodISNS) { 2926 boolean_t is_last_isns_server_b = 2927 B_FALSE; 2928 int isns_server_count = 0; 2929 void *void_p = NULL; 2930 2931 /* 2932 * Check if the last iSNS server's been 2933 * removed. 2934 */ 2935 { 2936 entry_t tmp_e; 2937 persistent_isns_addr_lock(); 2938 while (persistent_isns_addr_next( 2939 &void_p, &tmp_e) == B_TRUE) { 2940 isns_server_count++; 2941 } 2942 } 2943 persistent_isns_addr_unlock(); 2944 if (isns_server_count == 0) { 2945 is_last_isns_server_b = B_TRUE; 2946 } 2947 2948 /* 2949 * Deregister this node from this iSNS 2950 * server. 2951 */ 2952 initiator_node_name = kmem_zalloc( 2953 ISCSI_MAX_NAME_LEN, KM_SLEEP); 2954 if (persistent_initiator_name_get( 2955 initiator_node_name, 2956 ISCSI_MAX_NAME_LEN) == B_TRUE) { 2957 2958 if (strlen(initiator_node_name) > 0) { 2959 (void) isns_dereg_one_server( 2960 &e, (uint8_t *) 2961 initiator_node_name, 2962 is_last_isns_server_b); 2963 } 2964 } 2965 kmem_free(initiator_node_name, 2966 ISCSI_MAX_NAME_LEN); 2967 initiator_node_name = NULL; 2968 } 2969 } else { 2970 rtn = EBUSY; 2971 } 2972 break; 2973 2974 /* 2975 * ISCSI_DISCOVERY_SET - 2976 */ 2977 case ISCSI_DISCOVERY_SET: 2978 if (ddi_copyin((caddr_t)arg, &method, sizeof (method), mode)) { 2979 rtn = EFAULT; 2980 break; 2981 } 2982 2983 if (persistent_disc_meth_set(method) == B_FALSE) { 2984 rtn = EIO; 2985 } else { 2986 (void) iscsid_enable_discovery(ihp, method, B_FALSE); 2987 iscsid_poke_discovery(ihp, method); 2988 (void) iscsid_login_tgt(ihp, NULL, method, NULL); 2989 } 2990 break; 2991 2992 /* 2993 * ISCSI_DISCOVERY_GET - 2994 */ 2995 case ISCSI_DISCOVERY_GET: 2996 method = persistent_disc_meth_get(); 2997 rtn = ddi_copyout(&method, (caddr_t)arg, 2998 sizeof (method), mode); 2999 break; 3000 3001 /* 3002 * ISCSI_DISCOVERY_CLEAR - 3003 */ 3004 #define ISCSI_DISCOVERY_DELAY 2 /* seconds */ 3005 case ISCSI_DISCOVERY_CLEAR: 3006 if (ddi_copyin((caddr_t)arg, &method, sizeof (method), mode)) { 3007 rtn = EFAULT; 3008 break; 3009 } 3010 3011 /* If discovery in progress, try few times before return busy */ 3012 retry = 0; 3013 mutex_enter(&ihp->hba_discovery_events_mutex); 3014 while (ihp->hba_discovery_in_progress == B_TRUE) { 3015 if (++retry == 5) { 3016 rtn = EBUSY; 3017 break; 3018 } 3019 mutex_exit(&ihp->hba_discovery_events_mutex); 3020 delay(SEC_TO_TICK(ISCSI_DISCOVERY_DELAY)); 3021 mutex_enter(&ihp->hba_discovery_events_mutex); 3022 } 3023 #undef ISCSI_DISCOVERY_DELAY 3024 3025 /* 3026 * Clear discovery first, so that any bus config or 3027 * discovery requests will ignore this discovery method 3028 */ 3029 if (rtn == 0 && persistent_disc_meth_clear(method) == B_FALSE) { 3030 rtn = EIO; 3031 } 3032 mutex_exit(&ihp->hba_discovery_events_mutex); 3033 3034 if (rtn != 0) { 3035 break; 3036 } 3037 3038 /* Attempt to logout from all associated targets */ 3039 if (iscsid_disable_discovery(ihp, method) == B_FALSE) { 3040 /* Failure!, reset the discovery */ 3041 if (persistent_disc_meth_set(method) == B_FALSE) { 3042 cmn_err(CE_WARN, "Failed to reset discovery " 3043 "method after discovery disable failure."); 3044 } 3045 rtn = EBUSY; 3046 } 3047 break; 3048 3049 /* 3050 * ISCSI_DISCOVERY_PROPS - 3051 */ 3052 case ISCSI_DISCOVERY_PROPS: 3053 iscsid_props(&discovery_props); 3054 if (ddi_copyout(&discovery_props, (caddr_t)arg, 3055 sizeof (discovery_props), mode)) 3056 rtn = EFAULT; 3057 break; 3058 3059 /* 3060 * ISCSI_LUN_OID_LIST -- 3061 */ 3062 case ISCSI_LUN_OID_LIST_GET: 3063 ll = (iscsi_lun_list_t *)kmem_alloc(sizeof (*ll), KM_SLEEP); 3064 if (ddi_copyin((caddr_t)arg, ll, sizeof (*ll), mode)) { 3065 rtn = EFAULT; 3066 kmem_free(ll, sizeof (*ll)); 3067 break; 3068 } 3069 3070 if (ll->ll_vers != ISCSI_INTERFACE_VERSION) { 3071 rtn = EINVAL; 3072 kmem_free(ll, sizeof (*ll)); 3073 break; 3074 } 3075 3076 /* 3077 * Find out how much space the user has allocated in their 3078 * structure. Match the same space for our structure. 3079 */ 3080 lun_sz = sizeof (iscsi_lun_list_t); 3081 if (ll->ll_in_cnt > 0) { 3082 lun_sz += (ll->ll_in_cnt - 1) * sizeof (iscsi_if_lun_t); 3083 } 3084 3085 llp = kmem_zalloc(lun_sz, KM_SLEEP); 3086 bcopy(ll, llp, sizeof (*ll)); 3087 kmem_free(ll, sizeof (*ll)); 3088 3089 /* 3090 * Check to see if oid references a target-param oid. If so, 3091 * find the associated session oid before getting lu list. 3092 */ 3093 if (iscsi_targetparam_get_name(llp->ll_tgt_oid) != NULL) { 3094 for (isp = ihp->hba_sess_list; isp; 3095 isp = isp->sess_next) { 3096 if (isp->sess_target_oid == llp->ll_tgt_oid) { 3097 target_oid = isp->sess_oid; 3098 break; 3099 } 3100 } 3101 } else { 3102 target_oid = llp->ll_tgt_oid; 3103 } 3104 3105 3106 /* 3107 * Look at the LUNs attached to the specified target. If there 3108 * is space in the user structure save that information locally. 3109 * Always add up the count to the total. By always adding 3110 * the count this code can be used if ll_in_cnt == 0 and 3111 * the user just wishes to know the appropriate size to 3112 * allocate. 3113 */ 3114 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 3115 for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) { 3116 if ((llp->ll_all_tgts == B_FALSE) && 3117 (isp->sess_oid != target_oid)) { 3118 continue; 3119 } 3120 rw_enter(&isp->sess_lun_list_rwlock, RW_READER); 3121 for (ilp = isp->sess_lun_list; ilp; 3122 ilp = ilp->lun_next) { 3123 if (ilp->lun_state == 3124 ISCSI_LUN_STATE_ONLINE) { 3125 if (llp->ll_out_cnt < 3126 llp->ll_in_cnt) { 3127 iscsi_if_lun_t *lp; 3128 lp = &llp->ll_luns[ 3129 llp->ll_out_cnt]; 3130 3131 lp->l_tgt_oid = 3132 isp->sess_oid; 3133 lp->l_oid = ilp->lun_oid; 3134 lp->l_num = ilp->lun_num; 3135 } 3136 llp->ll_out_cnt++; 3137 } 3138 } 3139 rw_exit(&isp->sess_lun_list_rwlock); 3140 } 3141 rw_exit(&ihp->hba_sess_list_rwlock); 3142 3143 if (ddi_copyout(llp, (caddr_t)arg, lun_sz, mode)) { 3144 rtn = EFAULT; 3145 } 3146 3147 kmem_free(llp, lun_sz); 3148 break; 3149 3150 /* 3151 * ISCSI_LUN_PROPS_GET -- 3152 */ 3153 case ISCSI_LUN_PROPS_GET: 3154 lun = (iscsi_lun_props_t *)kmem_zalloc(sizeof (*lun), KM_SLEEP); 3155 if (ddi_copyin((caddr_t)arg, lun, sizeof (*lun), mode)) { 3156 rtn = EFAULT; 3157 kmem_free(lun, sizeof (*lun)); 3158 break; 3159 } 3160 3161 if (lun->lp_vers != ISCSI_INTERFACE_VERSION) { 3162 rtn = EINVAL; 3163 kmem_free(lun, sizeof (*lun)); 3164 break; 3165 } 3166 3167 /* 3168 * For the target specified, find the LUN specified and 3169 * return its properties 3170 */ 3171 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 3172 rtn = iscsi_sess_get(lun->lp_tgt_oid, ihp, &isp); 3173 if (rtn != 0) { 3174 rw_exit(&ihp->hba_sess_list_rwlock); 3175 rtn = EFAULT; 3176 kmem_free(lun, sizeof (*lun)); 3177 break; 3178 } 3179 rtn = EINVAL; /* Set bad rtn, correct only if found */ 3180 rw_enter(&isp->sess_lun_list_rwlock, RW_READER); 3181 for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) { 3182 if (ilp->lun_oid == lun->lp_oid) { 3183 lun->lp_num = ilp->lun_num; 3184 lun->lp_status = LunValid; 3185 lun->lp_time_online = ilp->lun_time_online; 3186 3187 if (ilp->lun_pip != NULL) { 3188 lun_dip = mdi_pi_get_client( 3189 ilp->lun_pip); 3190 } else { 3191 lun_dip = ilp->lun_dip; 3192 } 3193 3194 if (lun_dip != NULL && 3195 ((i_ddi_devi_attached(lun_dip)) || 3196 (ddi_get_devstate(lun_dip) == 3197 DDI_DEVSTATE_UP))) { 3198 (void) ddi_pathname(lun_dip, 3199 lun->lp_pathname); 3200 } else { 3201 /* 3202 * The LUN is not exported to the 3203 * OS yet. It is in the process 3204 * of being added. 3205 */ 3206 lun->lp_status = LunDoesNotExist; 3207 } 3208 bcopy(ilp->lun_vid, lun->lp_vid, 3209 sizeof (lun->lp_vid)); 3210 bcopy(ilp->lun_pid, lun->lp_pid, 3211 sizeof (lun->lp_pid)); 3212 rtn = ddi_copyout(lun, (caddr_t)arg, 3213 sizeof (*lun), mode); 3214 if (rtn == -1) { 3215 rtn = EFAULT; 3216 } 3217 break; 3218 } 3219 } 3220 rw_exit(&isp->sess_lun_list_rwlock); 3221 rw_exit(&ihp->hba_sess_list_rwlock); 3222 3223 kmem_free(lun, sizeof (*lun)); 3224 break; 3225 3226 /* 3227 * ISCSI_CONN_OID_LIST_GET -- 3228 */ 3229 #define ISCSIIOCOLGC iscsi_ioctl_conn_oid_list_get_copyout 3230 case ISCSI_CONN_OID_LIST_GET: 3231 { 3232 iscsi_conn_list_t *cl; 3233 3234 /* Asuume the worst */ 3235 rtn = EFAULT; 3236 3237 /* Copy the input argument into kernel world. */ 3238 cl = iscsi_ioctl_conn_oid_list_get_copyin( 3239 (caddr_t)arg, 3240 mode); 3241 if (cl != NULL) { 3242 if (iscsi_ioctl_conn_oid_list_get(ihp, cl) == 3243 B_TRUE) { 3244 rtn = 3245 ISCSIIOCOLGC( 3246 cl, (caddr_t)arg, mode); 3247 } 3248 } 3249 break; 3250 } 3251 #undef ISCSIIOCOLGC 3252 /* 3253 * ISCSI_CONN_OID_LIST_GET -- 3254 */ 3255 case ISCSI_CONN_PROPS_GET: 3256 { 3257 iscsi_conn_props_t *cp; 3258 3259 /* Asuume the worst */ 3260 rtn = EFAULT; 3261 3262 /* Copy the input argument into kernel world. */ 3263 cp = iscsi_ioctl_copyin( 3264 (caddr_t)arg, 3265 mode, 3266 sizeof (iscsi_conn_props_t)); 3267 3268 if (cp != NULL) { 3269 /* Get the propereties. */ 3270 if (iscsi_ioctl_conn_props_get(ihp, cp) == 3271 B_TRUE) { 3272 rtn = 3273 iscsi_ioctl_copyout( 3274 cp, 3275 sizeof (*cp), 3276 (caddr_t)arg, 3277 mode); 3278 } else { 3279 kmem_free(cp, sizeof (*cp)); 3280 cp = NULL; 3281 } 3282 } 3283 break; 3284 } 3285 3286 /* 3287 * ISCSI_RADIUS_GET - 3288 */ 3289 case ISCSI_RADIUS_GET: 3290 { 3291 iscsi_nvfile_status_t status; 3292 3293 radius = (iscsi_radius_props_t *)kmem_zalloc(sizeof (*radius), 3294 KM_SLEEP); 3295 if (ddi_copyin((caddr_t)arg, radius, sizeof (*radius), mode)) { 3296 kmem_free(radius, sizeof (*radius)); 3297 rtn = EFAULT; 3298 break; 3299 } else if (radius->r_vers != ISCSI_INTERFACE_VERSION) { 3300 kmem_free(radius, sizeof (*radius)); 3301 rtn = EINVAL; 3302 break; 3303 } 3304 3305 old_oid = radius->r_oid; 3306 3307 if (radius->r_oid == ihp->hba_oid) { 3308 name = ihp->hba_name; 3309 } else { 3310 /* 3311 * RADIUS configuration should be done on a per 3312 * initiator basis. 3313 */ 3314 kmem_free(radius, sizeof (*radius)); 3315 rtn = EINVAL; 3316 break; 3317 } 3318 3319 status = persistent_radius_get(radius); 3320 if (status == ISCSI_NVFILE_SUCCESS) { 3321 /* 3322 * Restore the value for overridden (and bogus) oid. 3323 */ 3324 radius->r_oid = old_oid; 3325 rtn = ddi_copyout(radius, (caddr_t)arg, 3326 sizeof (*radius), mode); 3327 } else if (status == ISCSI_NVFILE_NAMEVAL_NOT_FOUND) { 3328 rtn = ENOENT; 3329 } else { 3330 rtn = EIO; 3331 } 3332 kmem_free(radius, sizeof (*radius)); 3333 break; 3334 } 3335 3336 /* 3337 * ISCSI_RADIUS_SET - 3338 */ 3339 case ISCSI_RADIUS_SET: 3340 radius = (iscsi_radius_props_t *)kmem_zalloc(sizeof (*radius), 3341 KM_SLEEP); 3342 if (ddi_copyin((caddr_t)arg, radius, sizeof (*radius), mode)) { 3343 rtn = EFAULT; 3344 kmem_free(radius, sizeof (*radius)); 3345 break; 3346 } else if (radius->r_vers != ISCSI_INTERFACE_VERSION) { 3347 rtn = EINVAL; 3348 kmem_free(radius, sizeof (*radius)); 3349 break; 3350 } 3351 3352 if (radius->r_oid == ihp->hba_oid) { 3353 name = ihp->hba_name; 3354 } else { 3355 /* 3356 * RADIUS configuration should be done on a per 3357 * initiator basis. 3358 */ 3359 kmem_free(radius, sizeof (*radius)); 3360 rtn = EINVAL; 3361 break; 3362 } 3363 3364 if (persistent_radius_set(radius) == B_FALSE) { 3365 rtn = EIO; 3366 } 3367 3368 kmem_free(radius, sizeof (*radius)); 3369 break; 3370 3371 /* 3372 * ISCSI_AUTH_GET - 3373 */ 3374 case ISCSI_AUTH_GET: 3375 auth = (iscsi_auth_props_t *)kmem_zalloc(sizeof (*auth), 3376 KM_SLEEP); 3377 if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) { 3378 kmem_free(auth, sizeof (*auth)); 3379 rtn = EFAULT; 3380 break; 3381 } else if (auth->a_vers != ISCSI_INTERFACE_VERSION) { 3382 kmem_free(auth, sizeof (*auth)); 3383 rtn = EINVAL; 3384 break; 3385 } 3386 3387 old_oid = auth->a_oid; 3388 3389 if (auth->a_oid == ihp->hba_oid) { 3390 name = ihp->hba_name; 3391 } else { 3392 3393 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 3394 /* 3395 * If the oid does represent a session check to see 3396 * if it is a target oid. If so, return the target's 3397 * associated session. 3398 */ 3399 rtn = iscsi_sess_get(auth->a_oid, ihp, &isp); 3400 if (rtn != 0) { 3401 rtn = iscsi_sess_get_by_target(auth->a_oid, 3402 ihp, &isp); 3403 } 3404 rw_exit(&ihp->hba_sess_list_rwlock); 3405 3406 /* 3407 * If rtn is zero then we have found an 3408 * existing session. Use the session name to 3409 * do param lookup. If rtn is non-zero then 3410 * create a targetparam object and use its name 3411 * for param lookup. 3412 */ 3413 if (rtn == 0) { 3414 name = isp->sess_name; 3415 } else { 3416 name = 3417 iscsi_targetparam_get_name(auth->a_oid); 3418 } 3419 } 3420 3421 if (name == NULL) { 3422 rw_exit( 3423 &ihp->hba_sess_list_rwlock); 3424 rtn = EFAULT; 3425 break; 3426 } 3427 3428 if (persistent_auth_get((char *)name, auth) == B_TRUE) { 3429 /* 3430 * Restore the value for overridden (and bogus) oid. 3431 */ 3432 auth->a_oid = old_oid; 3433 rtn = ddi_copyout(auth, (caddr_t)arg, 3434 sizeof (*auth), mode); 3435 } else { 3436 rtn = EIO; 3437 } 3438 3439 kmem_free(auth, sizeof (*auth)); 3440 break; 3441 3442 /* 3443 * ISCSI_AUTH_SET - 3444 */ 3445 case ISCSI_AUTH_SET: 3446 auth = (iscsi_auth_props_t *)kmem_zalloc(sizeof (*auth), 3447 KM_SLEEP); 3448 if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) { 3449 kmem_free(auth, sizeof (*auth)); 3450 rtn = EFAULT; 3451 break; 3452 } else if (auth->a_vers != ISCSI_INTERFACE_VERSION) { 3453 kmem_free(auth, sizeof (*auth)); 3454 rtn = EINVAL; 3455 break; 3456 } 3457 3458 if (auth->a_oid == ihp->hba_oid) { 3459 name = ihp->hba_name; 3460 } else { 3461 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 3462 /* 3463 * If the oid does represent a session check to see 3464 * if it is a target oid. If so, return the target's 3465 * associated session. 3466 */ 3467 rtn = iscsi_sess_get(auth->a_oid, ihp, &isp); 3468 if (rtn != 0) { 3469 rtn = iscsi_sess_get_by_target(auth->a_oid, 3470 ihp, &isp); 3471 } 3472 rw_exit(&ihp->hba_sess_list_rwlock); 3473 3474 /* 3475 * If rtn is zero then we have found an 3476 * existing session. Use the session name to 3477 * do param lookup. If rtn is non-zero then 3478 * create a targetparam object and use its name 3479 * for param lookup. 3480 */ 3481 if (rtn == 0) { 3482 name = isp->sess_name; 3483 } else { 3484 name = 3485 iscsi_targetparam_get_name(auth->a_oid); 3486 rtn = 0; 3487 } 3488 } 3489 3490 if (name == NULL) { 3491 rtn = EFAULT; 3492 } else if (persistent_auth_set((char *)name, auth) 3493 == B_FALSE) { 3494 rtn = EIO; 3495 } 3496 3497 kmem_free(auth, sizeof (*auth)); 3498 break; 3499 3500 /* 3501 * ISCSI_AUTH_CLEAR - 3502 */ 3503 case ISCSI_AUTH_CLEAR: 3504 auth = (iscsi_auth_props_t *)kmem_alloc(sizeof (*auth), 3505 KM_SLEEP); 3506 if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) { 3507 kmem_free(auth, sizeof (*auth)); 3508 rtn = EFAULT; 3509 break; 3510 } else if (auth->a_vers != ISCSI_INTERFACE_VERSION) { 3511 kmem_free(auth, sizeof (*auth)); 3512 rtn = EINVAL; 3513 break; 3514 } 3515 3516 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 3517 /* 3518 * If the oid does represent a session check to see 3519 * if it is a target oid. If so, return the target's 3520 * associated session. 3521 */ 3522 rtn = iscsi_sess_get(auth->a_oid, ihp, &isp); 3523 if (rtn != 0) { 3524 rtn = iscsi_sess_get_by_target(auth->a_oid, ihp, &isp); 3525 } 3526 rw_exit(&ihp->hba_sess_list_rwlock); 3527 3528 /* 3529 * If rtn is zero then we have found an 3530 * existing session. Use the session name to 3531 * do param lookup. If rtn is non-zero then 3532 * create a targetparam object and use its name 3533 * for param lookup. 3534 */ 3535 if (rtn == 0) { 3536 name = isp->sess_name; 3537 } else { 3538 name = 3539 iscsi_targetparam_get_name(auth->a_oid); 3540 rtn = 0; 3541 discovered = B_FALSE; 3542 } 3543 3544 if (name == NULL) { 3545 rw_exit( 3546 &ihp->hba_sess_list_rwlock); 3547 rtn = EFAULT; 3548 break; 3549 } 3550 3551 if (persistent_auth_clear((char *)name) == B_FALSE) { 3552 rtn = EIO; 3553 } 3554 3555 /* 3556 * ISCSI_TARGET_PARAM_CLEAR, ISCSI_CHAP_CLEAR and 3557 * ISCSI_AUTH_CLEAR ioctl are called sequentially to remove 3558 * target parameters. Here, the target that is not discovered 3559 * by initiator should be removed from the iscsi_targets list 3560 * residing in the memory. 3561 */ 3562 if (discovered == B_FALSE) { 3563 (void) iscsi_targetparam_remove_target(auth->a_oid); 3564 } 3565 3566 kmem_free(auth, sizeof (*auth)); 3567 break; 3568 3569 /* 3570 * ISCSI_DB_RELOAD - 3571 */ 3572 case ISCSI_DB_RELOAD: 3573 /* ---- database will be closed and reread ---- */ 3574 if (iscsid_init(ihp, B_TRUE) == B_FALSE) { 3575 rtn = EFAULT; 3576 } 3577 break; 3578 3579 /* 3580 * ISCSI_DB_DUMP - 3581 */ 3582 case ISCSI_DB_DUMP: 3583 persistent_dump_data(); 3584 break; 3585 3586 case ISCSI_USCSI: 3587 3588 #ifdef _MULTI_DATAMODEL 3589 model = ddi_model_convert_from(mode & FMODELS); 3590 switch (model) { 3591 case DDI_MODEL_ILP32: 3592 3593 if (ddi_copyin((caddr_t)arg, &iu32_caller, 3594 sizeof (iscsi_uscsi32_t), mode)) { 3595 rtn = EFAULT; 3596 break; 3597 } 3598 3599 /* perform conversion from 32 -> 64 */ 3600 iu_caller.iu_vers = iu32_caller.iu_vers; 3601 iu_caller.iu_oid = iu32_caller.iu_oid; 3602 iu_caller.iu_tpgt = iu32_caller.iu_tpgt; 3603 iu_caller.iu_len = iu32_caller.iu_len; 3604 iu_caller.iu_lun = iu32_caller.iu_lun; 3605 uscsi_cmd32touscsi_cmd((&iu32_caller.iu_ucmd), 3606 (&iu_caller.iu_ucmd)); 3607 3608 break; 3609 case DDI_MODEL_NONE: 3610 if (ddi_copyin((caddr_t)arg, &iu_caller, 3611 sizeof (iscsi_uscsi_t), mode)) { 3612 rtn = EFAULT; 3613 break; 3614 } 3615 break; 3616 default: 3617 ASSERT(FALSE); 3618 rtn = EINVAL; 3619 break; 3620 } 3621 #endif /* _MULTI_DATAMODEL */ 3622 3623 /* If failures earlier break */ 3624 if (rtn != 0) { 3625 break; 3626 } 3627 3628 /* copy from caller to internel cmd */ 3629 bcopy(&iu_caller, &iu, sizeof (iu)); 3630 3631 if (iu.iu_vers != ISCSI_INTERFACE_VERSION) { 3632 rtn = EINVAL; 3633 break; 3634 } 3635 /* 3636 * Check to see if oid references a target-param oid. If so, 3637 * find the associated session oid before getting lu list. 3638 */ 3639 if (iscsi_targetparam_get_name(iu.iu_oid) != NULL) { 3640 for (isp = ihp->hba_sess_list; isp; isp = 3641 isp->sess_next) { 3642 if (isp->sess_target_oid == iu.iu_oid) { 3643 target_oid = isp->sess_oid; 3644 break; 3645 } 3646 } 3647 } else { 3648 target_oid = iu.iu_oid; 3649 } 3650 3651 /* make sure we have a matching session for this command */ 3652 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 3653 rtn = iscsi_sess_get(target_oid, ihp, &isp); 3654 if (rtn != 0) { 3655 rtn = iscsi_sess_get_by_target(target_oid, ihp, 3656 &isp); 3657 if (rtn != 0) { 3658 rw_exit(&ihp->hba_sess_list_rwlock); 3659 rtn = EFAULT; 3660 break; 3661 } 3662 } 3663 /* 3664 * If a caller buffer is present allocate duplicate 3665 * kernel space and copyin caller memory. 3666 */ 3667 if (iu.iu_ucmd.uscsi_buflen > 0) { 3668 iu.iu_ucmd.uscsi_bufaddr = (caddr_t)kmem_alloc( 3669 iu.iu_ucmd.uscsi_buflen, KM_SLEEP); 3670 if (ddi_copyin(iu_caller.iu_ucmd.uscsi_bufaddr, 3671 iu.iu_ucmd.uscsi_bufaddr, 3672 iu.iu_ucmd.uscsi_buflen, mode)) { 3673 rw_exit(&ihp->hba_sess_list_rwlock); 3674 rtn = EFAULT; 3675 break; 3676 } 3677 } 3678 3679 /* 3680 * If a caller cdb is present allocate duplicate 3681 * kernel space and copyin caller memory. 3682 */ 3683 if (iu.iu_ucmd.uscsi_cdblen > 0) { 3684 iu.iu_ucmd.uscsi_cdb = (caddr_t)kmem_alloc( 3685 iu_caller.iu_ucmd.uscsi_cdblen, KM_SLEEP); 3686 if (ddi_copyin(iu_caller.iu_ucmd.uscsi_cdb, 3687 iu.iu_ucmd.uscsi_cdb, 3688 iu.iu_ucmd.uscsi_cdblen, mode)) { 3689 if (iu.iu_ucmd.uscsi_buflen > 0) { 3690 kmem_free(iu.iu_ucmd.uscsi_bufaddr, 3691 iu_caller.iu_ucmd.uscsi_buflen); 3692 } 3693 rw_exit(&ihp->hba_sess_list_rwlock); 3694 rtn = EFAULT; 3695 break; 3696 } 3697 } 3698 3699 /* 3700 * If a caller request sense is present allocate 3701 * duplicate kernel space. No need to copyin. 3702 */ 3703 if (iu.iu_ucmd.uscsi_rqlen > 0) { 3704 iu.iu_ucmd.uscsi_rqbuf = (caddr_t)kmem_alloc( 3705 iu.iu_ucmd.uscsi_rqlen, KM_SLEEP); 3706 } 3707 3708 /* issue passthru to io path handler */ 3709 rtn = iscsi_handle_passthru(isp, iu.iu_lun, &iu.iu_ucmd); 3710 if (rtn != 0) { 3711 rtn = EFAULT; 3712 } 3713 3714 /* 3715 * If the caller had a buf we need to do a copyout 3716 * and free the kernel memory 3717 */ 3718 if (iu.iu_ucmd.uscsi_buflen > 0) { 3719 if (ddi_copyout(iu.iu_ucmd.uscsi_bufaddr, 3720 iu_caller.iu_ucmd.uscsi_bufaddr, 3721 iu.iu_ucmd.uscsi_buflen, mode) != 0) { 3722 rtn = EFAULT; 3723 } 3724 kmem_free(iu.iu_ucmd.uscsi_bufaddr, 3725 iu.iu_ucmd.uscsi_buflen); 3726 } 3727 3728 /* We need to free kernel cdb, no need to copyout */ 3729 if (iu.iu_ucmd.uscsi_cdblen > 0) { 3730 kmem_free(iu.iu_ucmd.uscsi_cdb, 3731 iu.iu_ucmd.uscsi_cdblen); 3732 } 3733 3734 /* 3735 * If the caller had a request sense we need to 3736 * do a copyout and free the kernel memory 3737 */ 3738 if (iu.iu_ucmd.uscsi_rqlen > 0) { 3739 if (ddi_copyout(iu.iu_ucmd.uscsi_rqbuf, 3740 iu_caller.iu_ucmd.uscsi_rqbuf, 3741 iu.iu_ucmd.uscsi_rqlen, mode) != 0) { 3742 rtn = EFAULT; 3743 } 3744 kmem_free(iu.iu_ucmd.uscsi_rqbuf, 3745 iu.iu_ucmd.uscsi_rqlen); 3746 } 3747 3748 #ifdef _MULTI_DATAMODEL 3749 if (iu.iu_ucmd.uscsi_status != 0) { 3750 switch (model = ddi_model_convert_from( 3751 mode & FMODELS)) { 3752 case DDI_MODEL_ILP32: 3753 iu32_caller.iu_ucmd.uscsi_status = 3754 iu.iu_ucmd.uscsi_status; 3755 if (ddi_copyout((void *)&iu32_caller, 3756 (caddr_t)arg, sizeof (iscsi_uscsi32_t), 3757 mode) != 0) { 3758 rtn = EFAULT; 3759 } 3760 break; 3761 case DDI_MODEL_NONE: 3762 iu_caller.iu_ucmd.uscsi_status = 3763 iu.iu_ucmd.uscsi_status; 3764 if (ddi_copyout((void *)&iu_caller, 3765 (caddr_t)arg, sizeof (iscsi_uscsi_t), 3766 mode) != 0) { 3767 rtn = EFAULT; 3768 } 3769 break; 3770 default: 3771 ASSERT(FALSE); 3772 } 3773 } 3774 #endif /* _MULTI_DATAMODEL */ 3775 rw_exit(&ihp->hba_sess_list_rwlock); 3776 break; 3777 3778 /* 3779 * ISCSI_DOOR_HANDLE_SET - 3780 */ 3781 case ISCSI_DOOR_HANDLE_SET: 3782 if (ddi_copyin((caddr_t)arg, &did, sizeof (int), mode) != 0) { 3783 rtn = EFAULT; 3784 } 3785 if (iscsi_door_bind(did) == B_FALSE) { 3786 rtn = EFAULT; 3787 } 3788 break; 3789 3790 case ISCSI_DISCOVERY_EVENTS: 3791 /* 3792 * If discovery has not been completed and not in progress, 3793 * poke the discovery methods 3794 */ 3795 mutex_enter(&ihp->hba_discovery_events_mutex); 3796 method = ihp->hba_discovery_events; 3797 if ((method != ISCSI_ALL_DISCOVERY_METHODS) && 3798 (ihp->hba_discovery_in_progress == B_FALSE)) { 3799 ihp->hba_discovery_in_progress = B_TRUE; 3800 mutex_exit(&ihp->hba_discovery_events_mutex); 3801 iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodUnknown); 3802 mutex_enter(&ihp->hba_discovery_events_mutex); 3803 ihp->hba_discovery_in_progress = B_FALSE; 3804 method = ihp->hba_discovery_events; 3805 } 3806 mutex_exit(&ihp->hba_discovery_events_mutex); 3807 3808 if (ddi_copyout((void *)&method, (caddr_t)arg, 3809 sizeof (method), mode) != 0) 3810 rtn = EFAULT; 3811 break; 3812 3813 /* 3814 * ISCSI_SENDTGTS_GET -- 3815 */ 3816 case ISCSI_SENDTGTS_GET: 3817 stl_hdr = iscsi_ioctl_copyin((caddr_t)arg, mode, 3818 sizeof (*stl_hdr)); 3819 if (stl_hdr == NULL) { 3820 rtn = EFAULT; 3821 break; 3822 } 3823 3824 if (stl_hdr->stl_entry.e_vers != ISCSI_INTERFACE_VERSION) { 3825 rtn = EINVAL; 3826 kmem_free(stl_hdr, sizeof (*stl_hdr)); 3827 break; 3828 } 3829 3830 /* calculate how much memory user allocated for SendTgts */ 3831 stl_sz = sizeof (*stl_hdr); 3832 if (stl_hdr->stl_in_cnt > 0) { 3833 stl_sz += ((stl_hdr->stl_in_cnt - 1) * 3834 sizeof (iscsi_sendtgts_entry_t)); 3835 } 3836 3837 /* allocate local SendTgts list of the same size */ 3838 istl = kmem_zalloc(stl_sz, KM_SLEEP); 3839 bcopy(stl_hdr, istl, sizeof (*stl_hdr)); 3840 kmem_free(stl_hdr, sizeof (*stl_hdr)); 3841 3842 /* lock interface so only one SendTargets operation occurs */ 3843 sema_p(&ihp->hba_sendtgts_semaphore); 3844 3845 rtn = iscsi_ioctl_sendtgts_get(ihp, istl); 3846 3847 if (rtn == 0) { 3848 rtn = iscsi_ioctl_copyout(istl, stl_sz, 3849 (caddr_t)arg, mode); 3850 } 3851 3852 /* release lock to allow another SendTargets discovery */ 3853 sema_v(&ihp->hba_sendtgts_semaphore); 3854 3855 break; 3856 3857 /* 3858 * ISCSI_ISNS_SERVER_GET -- 3859 */ 3860 case ISCSI_ISNS_SERVER_GET: 3861 server_pg_list_hdr = iscsi_ioctl_copyin((caddr_t)arg, mode, 3862 sizeof (*server_pg_list_hdr)); 3863 if (server_pg_list_hdr == NULL) { 3864 rtn = EFAULT; 3865 break; 3866 } 3867 3868 /* If iSNS discovery mode is not set, return with zero entry */ 3869 method = persistent_disc_meth_get(); 3870 if ((method & iSCSIDiscoveryMethodISNS) == 0) { 3871 kmem_free(server_pg_list_hdr, 3872 sizeof (*server_pg_list_hdr)); 3873 server_pg_list_hdr = NULL; 3874 rtn = EACCES; 3875 break; 3876 } 3877 3878 initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 3879 if (persistent_initiator_name_get(initiator_node_name, 3880 ISCSI_MAX_NAME_LEN) != B_TRUE) { 3881 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 3882 initiator_node_name = NULL; 3883 kmem_free(server_pg_list_hdr, 3884 sizeof (*server_pg_list_hdr)); 3885 server_pg_list_hdr = NULL; 3886 rtn = EIO; 3887 break; 3888 } 3889 if (strlen(initiator_node_name) == 0) { 3890 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 3891 initiator_node_name = NULL; 3892 kmem_free(server_pg_list_hdr, 3893 sizeof (*server_pg_list_hdr)); 3894 server_pg_list_hdr = NULL; 3895 rtn = EIO; 3896 break; 3897 } 3898 3899 initiator_node_alias = kmem_zalloc( 3900 ISCSI_MAX_NAME_LEN, KM_SLEEP); 3901 if (persistent_alias_name_get(initiator_node_alias, 3902 ISCSI_MAX_NAME_LEN) != B_TRUE) { 3903 initiator_node_alias[0] = '\0'; 3904 } 3905 rtn = isns_query_one_server(&(server_pg_list_hdr->addr), 3906 ihp->hba_isid, 3907 (uint8_t *)initiator_node_name, 3908 (uint8_t *)initiator_node_alias, 3909 ISNS_INITIATOR_NODE_TYPE, 3910 &pg_list); 3911 if (rtn != isns_ok || pg_list == NULL) { 3912 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 3913 initiator_node_name = NULL; 3914 kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN); 3915 initiator_node_alias = NULL; 3916 kmem_free(server_pg_list_hdr, 3917 sizeof (*server_pg_list_hdr)); 3918 server_pg_list_hdr = NULL; 3919 rtn = EIO; 3920 break; 3921 } 3922 3923 /* 3924 * pg_list_sz is the size of the pg_list returned from the 3925 * isns_query_all 3926 * 3927 * pg_sz_copy_out is the size of the pg_list we are going to 3928 * return back to the caller 3929 * 3930 * server_pg_list_sz is total amount of data we are returning 3931 * back to the caller 3932 */ 3933 pg_list->pg_in_cnt = 3934 server_pg_list_hdr->addr_port_list.pg_in_cnt; 3935 pg_list_sz = sizeof (isns_portal_group_list_t); 3936 if (pg_list->pg_out_cnt > 0) { 3937 pg_list_sz += (pg_list->pg_out_cnt - 1) * 3938 sizeof (isns_portal_group_t); 3939 } 3940 /* 3941 * check if caller passed in a buffer with enough space 3942 * if there isn't enough space, fill the caller's buffer with 3943 * as much information as possible. 3944 * 3945 * if pg_out_cnt > pg_in_cnt, pg_out_cnt will be returned with 3946 * the total number of targets found 3947 * 3948 * if pg_out_cnt < pg_in_cnt, pg_out_cnt will be the number 3949 * of targets returned 3950 */ 3951 if (pg_list->pg_in_cnt < pg_list->pg_out_cnt) { 3952 pg_sz_copy_out = sizeof (isns_portal_group_list_t); 3953 if (pg_list->pg_in_cnt > 0) { 3954 pg_sz_copy_out += (pg_list->pg_in_cnt - 1) * 3955 sizeof (isns_portal_group_t); 3956 } 3957 server_pg_list_sz = 3958 sizeof (isns_server_portal_group_list_t); 3959 if (pg_list->pg_in_cnt > 0) { 3960 server_pg_list_sz += (pg_list->pg_in_cnt - 1) * 3961 sizeof (isns_portal_group_t); 3962 } 3963 } else { 3964 pg_sz_copy_out = pg_list_sz; 3965 server_pg_list_sz = 3966 sizeof (isns_server_portal_group_list_t); 3967 if (pg_list->pg_out_cnt > 0) { 3968 server_pg_list_sz += (pg_list->pg_out_cnt - 1) * 3969 sizeof (isns_portal_group_t); 3970 } 3971 } 3972 3973 server_pg_list = (isns_server_portal_group_list_t *)kmem_zalloc( 3974 server_pg_list_sz, KM_SLEEP); 3975 3976 bcopy(&(server_pg_list_hdr->addr), &(server_pg_list->addr), 3977 sizeof (server_pg_list->addr)); 3978 bcopy(pg_list, &server_pg_list->addr_port_list, pg_sz_copy_out); 3979 3980 if (ddi_copyout(server_pg_list, (caddr_t)arg, server_pg_list_sz, 3981 mode) != 0) { 3982 rtn = EFAULT; 3983 } 3984 DTRACE_PROBE1(iscsi_ioctl_iscsi_isns_server_get_pg_sz, 3985 int, pg_list_sz); 3986 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 3987 initiator_node_name = NULL; 3988 kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN); 3989 initiator_node_alias = NULL; 3990 kmem_free(pg_list, pg_list_sz); 3991 pg_list = NULL; 3992 kmem_free(server_pg_list, server_pg_list_sz); 3993 server_pg_list = NULL; 3994 kmem_free(server_pg_list_hdr, sizeof (*server_pg_list_hdr)); 3995 server_pg_list_hdr = NULL; 3996 break; 3997 3998 /* 3999 * ISCSI_GET_CONFIG_SESSIONS -- 4000 */ 4001 case ISCSI_GET_CONFIG_SESSIONS: 4002 /* FALLTHRU */ 4003 4004 case ISCSI_SET_CONFIG_SESSIONS: 4005 size = sizeof (*ics); 4006 ics = iscsi_ioctl_copyin((caddr_t)arg, mode, size); 4007 if (ics == NULL) { 4008 rtn = EFAULT; 4009 break; 4010 } 4011 4012 /* verify version infomration */ 4013 if (ics->ics_ver != ISCSI_INTERFACE_VERSION) { 4014 rtn = EINVAL; 4015 kmem_free(ics, size); 4016 ics = NULL; 4017 break; 4018 } 4019 4020 /* Check to see if we need to copy in more memory */ 4021 if (ics->ics_in > 1) { 4022 /* record correct size */ 4023 size = ISCSI_SESSION_CONFIG_SIZE(ics->ics_in); 4024 /* free old buffer */ 4025 kmem_free(ics, sizeof (*ics)); 4026 4027 /* copy in complete buffer size */ 4028 ics = iscsi_ioctl_copyin((caddr_t)arg, mode, size); 4029 if (ics == NULL) { 4030 rtn = EFAULT; 4031 break; 4032 } 4033 } 4034 4035 /* switch action based on get or set */ 4036 if (cmd == ISCSI_GET_CONFIG_SESSIONS) { 4037 /* get */ 4038 rtn = iscsi_ioctl_get_config_sess(ihp, ics); 4039 if (rtn == 0) { 4040 /* copyout data for gets */ 4041 rtn = iscsi_ioctl_copyout(ics, size, 4042 (caddr_t)arg, mode); 4043 } else { 4044 kmem_free(ics, size); 4045 ics = NULL; 4046 } 4047 } else { 4048 /* set */ 4049 rtn = iscsi_ioctl_set_config_sess(ihp, ics); 4050 if (iscsiboot_prop) { 4051 if (iscsi_cmp_boot_sess_oid(ihp, 4052 ics->ics_oid)) { 4053 /* 4054 * found active session for this object 4055 * or this is initiator object 4056 * with mpxio enabled 4057 */ 4058 if (!iscsi_reconfig_boot_sess(ihp)) { 4059 kmem_free(ics, size); 4060 ics = NULL; 4061 rtn = EINVAL; 4062 break; 4063 } 4064 } 4065 } 4066 kmem_free(ics, size); 4067 ics = NULL; 4068 } 4069 break; 4070 4071 case ISCSI_IS_ACTIVE: 4072 /* 4073 * dhcpagent calls here to check if there are 4074 * active iSCSI sessions 4075 */ 4076 instance = 0; 4077 if (iscsiboot_prop) { 4078 instance = 1; 4079 } 4080 if (!instance) { 4081 rw_enter(&ihp->hba_sess_list_rwlock, 4082 RW_READER); 4083 for (isp = ihp->hba_sess_list; isp; 4084 isp = isp->sess_next) { 4085 if ((isp->sess_state == 4086 ISCSI_SESS_STATE_LOGGED_IN) && 4087 (isp->sess_lun_list != 4088 NULL)) { 4089 instance = 1; 4090 break; 4091 } 4092 } 4093 rw_exit(&ihp->hba_sess_list_rwlock); 4094 } 4095 size = sizeof (instance); 4096 if (ddi_copyout(&instance, (caddr_t)arg, size, 4097 mode) != 0) { 4098 rtn = EFAULT; 4099 } 4100 break; 4101 4102 case ISCSI_BOOTPROP_GET: 4103 size = sizeof (*bootProp); 4104 bootProp = iscsi_ioctl_copyin((caddr_t)arg, mode, size); 4105 if (bootProp == NULL) { 4106 rtn = EFAULT; 4107 break; 4108 } 4109 bootProp->hba_mpxio_enabled = 4110 iscsi_chk_bootlun_mpxio(ihp); 4111 if (iscsiboot_prop == NULL) { 4112 bootProp->iscsiboot = 0; 4113 rtn = iscsi_ioctl_copyout(bootProp, size, 4114 (caddr_t)arg, mode); 4115 break; 4116 } else { 4117 bootProp->iscsiboot = 1; 4118 } 4119 4120 if (iscsiboot_prop->boot_init.ini_name != NULL) { 4121 (void) strncpy((char *)bootProp->ini_name.n_name, 4122 (char *)iscsiboot_prop->boot_init.ini_name, 4123 ISCSI_MAX_NAME_LEN); 4124 } 4125 if (iscsiboot_prop->boot_init.ini_chap_name != NULL) { 4126 bootProp->auth.a_auth_method = authMethodCHAP; 4127 (void) strncpy((char *)bootProp->ini_chap.c_user, 4128 (char *)iscsiboot_prop->boot_init.ini_chap_name, 4129 ISCSI_MAX_NAME_LEN); 4130 (void) strncpy((char *)bootProp->ini_chap.c_secret, 4131 (char *)iscsiboot_prop->boot_init.ini_chap_sec, 4132 ISCSI_CHAP_SECRET_LEN); 4133 if (iscsiboot_prop->boot_tgt.tgt_chap_name != 4134 NULL) { 4135 bootProp->auth.a_bi_auth = B_TRUE; 4136 } else { 4137 bootProp->auth.a_bi_auth = B_FALSE; 4138 } 4139 } 4140 if (iscsiboot_prop->boot_tgt.tgt_name != NULL) { 4141 (void) strncpy((char *)bootProp->tgt_name.n_name, 4142 (char *)iscsiboot_prop->boot_tgt.tgt_name, 4143 ISCSI_MAX_NAME_LEN); 4144 } 4145 if (iscsiboot_prop->boot_tgt.tgt_chap_name != NULL) { 4146 (void) strncpy((char *)bootProp->tgt_chap.c_user, 4147 (char *)iscsiboot_prop->boot_tgt.tgt_chap_name, 4148 ISCSI_MAX_NAME_LEN); 4149 (void) strncpy((char *)bootProp->tgt_chap.c_secret, 4150 (char *)iscsiboot_prop->boot_tgt.tgt_chap_sec, 4151 ISCSI_CHAP_SECRET_LEN); 4152 } 4153 4154 rtn = iscsi_ioctl_copyout(bootProp, size, (caddr_t)arg, mode); 4155 break; 4156 4157 default: 4158 rtn = ENOTTY; 4159 cmn_err(CE_NOTE, "unrecognized ioctl 0x%x", cmd); 4160 } /* end of ioctl type switch/cases */ 4161 4162 return (rtn); 4163 } 4164 4165 /* 4166 * +--------------------------------------------------------------------+ 4167 * | End of cb_ops routines | 4168 * +--------------------------------------------------------------------+ 4169 */ 4170 4171 4172 /* 4173 * +--------------------------------------------------------------------+ 4174 * | Common scsi_tran support routines | 4175 * +--------------------------------------------------------------------+ 4176 */ 4177 4178 /* 4179 * iscsi_i_commoncap -- SCSA host adapter get/set capability routines. 4180 * 4181 * Need to determine if any of these can be determined through the iSCSI 4182 * protocol. For now just return error on most. 4183 */ 4184 /* ARGSUSED */ 4185 static int 4186 iscsi_i_commoncap(struct scsi_address *ap, char *cap, int val, 4187 int tgtonly, int doset) 4188 { 4189 int rtn; 4190 int cidx; 4191 iscsi_lun_t *ilp; 4192 4193 ASSERT((ap)->a_hba_tran->tran_hba_private != NULL); 4194 ilp = (iscsi_lun_t *)((ap)->a_hba_tran->tran_tgt_private); 4195 ASSERT(ilp != NULL); 4196 4197 if (cap == (char *)0) { 4198 return (FALSE); 4199 } 4200 4201 cidx = scsi_hba_lookup_capstr(cap); 4202 if (cidx == -1) { 4203 return (cidx); 4204 } 4205 4206 /* 4207 * Process setcap request. 4208 */ 4209 if (doset) { 4210 /* 4211 * At present, we can only set binary (0/1) values 4212 */ 4213 switch (cidx) { 4214 case SCSI_CAP_LUN_RESET: 4215 if (val) { 4216 ilp->lun_cap |= ISCSI_LUN_CAP_RESET; 4217 } else { 4218 ilp->lun_cap &= ~ISCSI_LUN_CAP_RESET; 4219 } 4220 rtn = TRUE; 4221 break; 4222 default: 4223 /* 4224 * None of these are settable via 4225 * the capability interface. 4226 */ 4227 rtn = FALSE; 4228 break; 4229 } 4230 4231 /* 4232 * Process getcap request. 4233 */ 4234 } else { 4235 switch (cidx) { 4236 case SCSI_CAP_DMA_MAX: 4237 /* no DMA, Psuedo value */ 4238 rtn = INT32_MAX; 4239 break; 4240 case SCSI_CAP_INITIATOR_ID: 4241 rtn = 7; 4242 break; 4243 case SCSI_CAP_ARQ: 4244 case SCSI_CAP_RESET_NOTIFICATION: 4245 case SCSI_CAP_TAGGED_QING: 4246 rtn = TRUE; 4247 break; 4248 case SCSI_CAP_SCSI_VERSION: 4249 rtn = SCSI_VERSION_3; 4250 break; 4251 case SCSI_CAP_INTERCONNECT_TYPE: 4252 rtn = INTERCONNECT_FABRIC; 4253 break; 4254 case SCSI_CAP_LUN_RESET: 4255 rtn = ((ilp->lun_cap & ISCSI_LUN_CAP_RESET) != 0) ? 4256 TRUE : FALSE; 4257 break; 4258 case SCSI_CAP_CDB_LEN: 4259 /* 4260 * iSCSI RFC 3720 defines a default 16 byte 4261 * CDB as part of the Basic Header Segment 4262 * (BHS) (10.2.1) and allows for an Additional 4263 * Header Segment (AHS) Length of 255 * 4 4264 * (10.2.1.5). The AHS length can be used 4265 * for different purposes two of which are 4266 * Extended CDB ADS (10.2.2.3) and Bidirectional 4267 * Expected Read-Data Length AHS (10.2.2.4). 4268 * The largest header of these consumes is 4269 * 32 bytes. So the total Max CDB Length is 4270 * 16 + ((255 * 4 ) - 32) = 1004. 4271 */ 4272 rtn = 1004; 4273 break; 4274 default: 4275 rtn = UNDEFINED; 4276 break; 4277 } 4278 } 4279 return (rtn); 4280 } 4281 4282 /* 4283 * iscsi_virt_lun_init - attempts to complete a mdi/scsi_vhci binding 4284 * 4285 * This routine is used to associate the tran_tgt_private to our ilp 4286 * structure. This function is indirectly called from our 4287 * iscsi_lun_create_xxx routines. These routines must prevent 4288 * the session and lun lists from changing during this call. 4289 */ 4290 /* ARGSUSED */ 4291 static int 4292 iscsi_virt_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip, 4293 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 4294 { 4295 iscsi_lun_t *ilp = NULL; 4296 iscsi_lun_t *ilp_check = NULL; 4297 iscsi_sess_t *isp = NULL; 4298 char *lun_guid = NULL; 4299 mdi_pathinfo_t *pip = NULL; 4300 iscsi_hba_t *ihp = (iscsi_hba_t *)hba_tran->tran_hba_private; 4301 char target_port_name[MAX_NAME_PROP_SIZE]; 4302 4303 /* 4304 * Here's a nice little piece of undocumented stuff. 4305 */ 4306 if ((pip = (mdi_pathinfo_t *)sd->sd_private) == NULL) { 4307 /* 4308 * Very bad news if this occurs. Somehow SCSI_vhci has 4309 * lost the pathinfo node for this target. 4310 */ 4311 return (DDI_NOT_WELL_FORMED); 4312 } 4313 4314 ilp = (iscsi_lun_t *)mdi_pi_get_phci_private(pip); 4315 4316 /* 4317 * +----------------------------------------------------+ 4318 * | Looking to find the target device via the property | 4319 * | is not required since the driver can easily get | 4320 * | this information from the mdi_phci_get_private() | 4321 * | call above. This is just a consistency check | 4322 * | which can be removed. | 4323 */ 4324 if (mdi_prop_lookup_string(pip, MDI_GUID, &lun_guid) != 4325 DDI_PROP_SUCCESS) { 4326 return (DDI_NOT_WELL_FORMED); 4327 } 4328 4329 for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) { 4330 4331 /* If this isn't the matching session continue */ 4332 if (ilp->lun_sess != isp) { 4333 continue; 4334 } 4335 4336 /* 4337 * We are already holding the lun list rwlock 4338 * for this thread on the callers side of mdi_pi_online 4339 * or ndi_devi_online. Which lead to this functions 4340 * call. 4341 */ 4342 for (ilp_check = isp->sess_lun_list; ilp_check; 4343 ilp_check = ilp_check->lun_next) { 4344 4345 /* 4346 * If this is the matching LUN and contains 4347 * the same LUN GUID then break we found our 4348 * match. 4349 */ 4350 if ((ilp == ilp_check) && 4351 (strcmp(lun_guid, ilp_check->lun_guid) == 0)) { 4352 break; 4353 } 4354 } 4355 if (ilp_check != NULL) { 4356 break; 4357 } 4358 } 4359 4360 /* 4361 * Free resource that's no longer required. 4362 */ 4363 if (lun_guid != NULL) 4364 (void) mdi_prop_free(lun_guid); 4365 4366 if (ilp_check == NULL) { 4367 /* 4368 * Failed to find iSCSI LUN in HBA chain based 4369 * on the GUID that was stored as a property on 4370 * the pathinfo node. 4371 */ 4372 return (DDI_NOT_WELL_FORMED); 4373 } 4374 4375 if (ilp != ilp_check) { 4376 /* 4377 * The iSCSI target that we found on the HBA link is 4378 * different than the iSCSI target that was stored as 4379 * private data on the pathinfo node. 4380 */ 4381 return (DDI_NOT_WELL_FORMED); 4382 } 4383 /* 4384 * | End of consistency check | 4385 * +----------------------------------------------------+ 4386 */ 4387 4388 hba_tran->tran_tgt_private = ilp; 4389 4390 target_port_name[0] = '\0'; 4391 if (ilp->lun_sess->sess_tpgt_conf == ISCSI_DEFAULT_TPGT) { 4392 (void) snprintf(target_port_name, MAX_NAME_PROP_SIZE, 4393 "%02x%02x%02x%02x%02x%02x,%s", 4394 ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1], 4395 ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3], 4396 ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5], 4397 ilp->lun_sess->sess_name); 4398 } else { 4399 (void) snprintf(target_port_name, MAX_NAME_PROP_SIZE, 4400 "%02x%02x%02x%02x%02x%02x,%s,%d", 4401 ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1], 4402 ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3], 4403 ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5], 4404 ilp->lun_sess->sess_name, ilp->lun_sess->sess_tpgt_conf); 4405 } 4406 4407 if (mdi_prop_update_string(pip, "target-port", 4408 target_port_name) != DDI_PROP_SUCCESS) { 4409 cmn_err(CE_WARN, "iscsi_virt_lun_init: Creating 'target-port' " 4410 "property on Path(%p) for Target(%s), Lun(%d) Failed", 4411 (void *)pip, ilp->lun_sess->sess_name, ilp->lun_num); 4412 } 4413 4414 return (DDI_SUCCESS); 4415 } 4416 4417 /* 4418 * iscsi_phys_lun_init - attempts to complete a ndi binding 4419 * 4420 * This routine is used to associate the tran_tgt_private to our 4421 * ilp structure. This function is indirectly called from our 4422 * iscsi_lun_create_xxx routines. These routines must prevent 4423 * the session and lun lists from changing during this call. 4424 */ 4425 static int 4426 iscsi_phys_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip, 4427 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 4428 { 4429 int rtn = DDI_SUCCESS; 4430 iscsi_hba_t *ihp = NULL; 4431 iscsi_sess_t *isp = NULL; 4432 iscsi_lun_t *ilp = NULL; 4433 char target_port_name[MAX_NAME_PROP_SIZE]; 4434 int *words = NULL; 4435 uint_t nwords = 0; 4436 4437 ASSERT(hba_dip); 4438 ASSERT(lun_dip); 4439 ASSERT(hba_tran); 4440 ASSERT(sd); 4441 ihp = (iscsi_hba_t *)hba_tran->tran_hba_private; 4442 ASSERT(ihp); 4443 4444 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, lun_dip, 4445 DDI_PROP_DONTPASS, LUN_PROP, &words, &nwords) != DDI_PROP_SUCCESS) { 4446 cmn_err(CE_WARN, "iscsi_phys_lun_init: Returning DDI_FAILURE:" 4447 "lun for %s (instance %d)", ddi_get_name(lun_dip), 4448 ddi_get_instance(lun_dip)); 4449 return (DDI_FAILURE); 4450 } 4451 4452 if (nwords == 0) { 4453 ddi_prop_free(words); 4454 return (DDI_FAILURE); 4455 } 4456 4457 ASSERT(words != NULL); 4458 4459 /* See if we already created this session */ 4460 4461 /* Walk the HBA's session list */ 4462 for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) { 4463 /* compare target name as the unique identifier */ 4464 if (sd->sd_address.a_target == isp->sess_oid) { 4465 /* found match */ 4466 break; 4467 } 4468 } 4469 4470 /* If we found matching session continue searching for tgt */ 4471 if (isp != NULL) { 4472 /* 4473 * Search for the matching iscsi lun structure. We don't 4474 * need to hold the READER for the lun list at this point. 4475 * because the tran_get_name is being called from the online 4476 * function which is already holding a reader on the lun 4477 * list. 4478 */ 4479 for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) { 4480 if (*words == ilp->lun_num) { 4481 /* found match */ 4482 break; 4483 } 4484 } 4485 4486 if (ilp != NULL) { 4487 /* 4488 * tgt found path it to the tran_lun_private 4489 * this is used later for fast access on 4490 * init_pkt and start 4491 */ 4492 hba_tran->tran_tgt_private = ilp; 4493 } else { 4494 /* tgt not found */ 4495 ddi_prop_free(words); 4496 return (DDI_FAILURE); 4497 } 4498 } else { 4499 /* sess not found */ 4500 ddi_prop_free(words); 4501 return (DDI_FAILURE); 4502 } 4503 ddi_prop_free(words); 4504 4505 target_port_name[0] = '\0'; 4506 if (ilp->lun_sess->sess_tpgt_conf == ISCSI_DEFAULT_TPGT) { 4507 (void) snprintf(target_port_name, MAX_NAME_PROP_SIZE, 4508 "%02x%02x%02x%02x%02x%02x,%s", 4509 ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1], 4510 ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3], 4511 ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5], 4512 ilp->lun_sess->sess_name); 4513 } else { 4514 (void) snprintf(target_port_name, MAX_NAME_PROP_SIZE, 4515 "%02x%02x%02x%02x%02x%02x,%s,%d", 4516 ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1], 4517 ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3], 4518 ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5], 4519 ilp->lun_sess->sess_name, ilp->lun_sess->sess_tpgt_conf); 4520 } 4521 4522 if (ddi_prop_update_string(DDI_DEV_T_NONE, lun_dip, 4523 "target-port", target_port_name) != DDI_PROP_SUCCESS) { 4524 cmn_err(CE_WARN, "iscsi_phys_lun_init: Creating 'target-port' " 4525 "property on Target(%s), Lun(%d) Failed", 4526 ilp->lun_sess->sess_name, ilp->lun_num); 4527 } 4528 4529 return (rtn); 4530 } 4531 4532 /* 4533 * +--------------------------------------------------------------------+ 4534 * | End of scsi_tran support routines | 4535 * +--------------------------------------------------------------------+ 4536 */ 4537 4538 /* 4539 * +--------------------------------------------------------------------+ 4540 * | Begin of struct utility routines | 4541 * +--------------------------------------------------------------------+ 4542 */ 4543 4544 4545 /* 4546 * iscsi_set_default_login_params - This function sets the 4547 * driver default login params. This is using during the 4548 * creation of our iSCSI HBA structure initialization by 4549 * could be used at other times to reset back to the defaults. 4550 */ 4551 void 4552 iscsi_set_default_login_params(iscsi_login_params_t *params) 4553 { 4554 params->immediate_data = ISCSI_DEFAULT_IMMEDIATE_DATA; 4555 params->initial_r2t = ISCSI_DEFAULT_INITIALR2T; 4556 params->first_burst_length = ISCSI_DEFAULT_FIRST_BURST_LENGTH; 4557 params->max_burst_length = ISCSI_DEFAULT_MAX_BURST_LENGTH; 4558 params->data_pdu_in_order = ISCSI_DEFAULT_DATA_PDU_IN_ORDER; 4559 params->data_sequence_in_order = ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER; 4560 params->default_time_to_wait = ISCSI_DEFAULT_TIME_TO_WAIT; 4561 params->default_time_to_retain = ISCSI_DEFAULT_TIME_TO_RETAIN; 4562 params->header_digest = ISCSI_DEFAULT_HEADER_DIGEST; 4563 params->data_digest = ISCSI_DEFAULT_DATA_DIGEST; 4564 params->max_recv_data_seg_len = ISCSI_DEFAULT_MAX_RECV_SEG_LEN; 4565 params->max_xmit_data_seg_len = ISCSI_DEFAULT_MAX_XMIT_SEG_LEN; 4566 params->max_connections = ISCSI_DEFAULT_MAX_CONNECTIONS; 4567 params->max_outstanding_r2t = ISCSI_DEFAULT_MAX_OUT_R2T; 4568 params->error_recovery_level = ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL; 4569 params->ifmarker = ISCSI_DEFAULT_IFMARKER; 4570 params->ofmarker = ISCSI_DEFAULT_OFMARKER; 4571 } 4572 4573 4574 /* 4575 * +--------------------------------------------------------------------+ 4576 * | End of struct utility routines | 4577 * +--------------------------------------------------------------------+ 4578 */ 4579 4580 /* 4581 * +--------------------------------------------------------------------+ 4582 * | Begin of ioctl utility routines | 4583 * +--------------------------------------------------------------------+ 4584 */ 4585 4586 /* 4587 * iscsi_get_param - This function is a helper to ISCSI_GET_PARAM 4588 * IOCTL 4589 */ 4590 int 4591 iscsi_get_param(iscsi_login_params_t *params, boolean_t valid_flag, 4592 iscsi_param_get_t *ipgp) { 4593 int rtn = 0; 4594 4595 /* ---- Default to settable, possibly changed later ---- */ 4596 ipgp->g_value.v_valid = valid_flag; 4597 ipgp->g_value.v_settable = B_TRUE; 4598 4599 switch (ipgp->g_param) { 4600 /* 4601 * Boolean parameters 4602 */ 4603 case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER: 4604 ipgp->g_value.v_bool.b_current = 4605 params->data_sequence_in_order; 4606 ipgp->g_value.v_bool.b_default = 4607 ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER; 4608 break; 4609 case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA: 4610 ipgp->g_value.v_bool.b_current = 4611 params->immediate_data; 4612 ipgp->g_value.v_bool.b_default = 4613 ISCSI_DEFAULT_IMMEDIATE_DATA; 4614 break; 4615 case ISCSI_LOGIN_PARAM_INITIAL_R2T: 4616 ipgp->g_value.v_bool.b_current = 4617 params->initial_r2t; 4618 ipgp->g_value.v_bool.b_default = 4619 ISCSI_DEFAULT_IMMEDIATE_DATA; 4620 break; 4621 case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER: 4622 ipgp->g_value.v_bool.b_current = 4623 params->data_pdu_in_order; 4624 ipgp->g_value.v_bool.b_default = 4625 ISCSI_DEFAULT_DATA_PDU_IN_ORDER; 4626 break; 4627 4628 /* 4629 * Integer parameters 4630 */ 4631 case ISCSI_LOGIN_PARAM_HEADER_DIGEST: 4632 ipgp->g_value.v_integer.i_current = params->header_digest; 4633 ipgp->g_value.v_integer.i_default = ISCSI_DEFAULT_HEADER_DIGEST; 4634 ipgp->g_value.v_integer.i_min = 0; 4635 ipgp->g_value.v_integer.i_max = ISCSI_MAX_HEADER_DIGEST; 4636 ipgp->g_value.v_integer.i_incr = 1; 4637 break; 4638 case ISCSI_LOGIN_PARAM_DATA_DIGEST: 4639 ipgp->g_value.v_integer.i_current = params->data_digest; 4640 ipgp->g_value.v_integer.i_default = ISCSI_DEFAULT_DATA_DIGEST; 4641 ipgp->g_value.v_integer.i_min = 0; 4642 ipgp->g_value.v_integer.i_max = ISCSI_MAX_DATA_DIGEST; 4643 ipgp->g_value.v_integer.i_incr = 1; 4644 break; 4645 case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN: 4646 ipgp->g_value.v_integer.i_current = 4647 params->default_time_to_retain; 4648 ipgp->g_value.v_integer.i_default = 4649 ISCSI_DEFAULT_TIME_TO_RETAIN; 4650 ipgp->g_value.v_integer.i_min = 0; 4651 ipgp->g_value.v_integer.i_max = ISCSI_MAX_TIME2RETAIN; 4652 ipgp->g_value.v_integer.i_incr = 1; 4653 break; 4654 case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT: 4655 ipgp->g_value.v_integer.i_current = 4656 params->default_time_to_wait; 4657 ipgp->g_value.v_integer.i_default = 4658 ISCSI_DEFAULT_TIME_TO_WAIT; 4659 ipgp->g_value.v_integer.i_min = 0; 4660 ipgp->g_value.v_integer.i_max = ISCSI_MAX_TIME2WAIT; 4661 ipgp->g_value.v_integer.i_incr = 1; 4662 break; 4663 case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL: 4664 ipgp->g_value.v_integer.i_current = 4665 params->error_recovery_level; 4666 ipgp->g_value.v_integer.i_default = 4667 ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL; 4668 ipgp->g_value.v_integer.i_min = 0; 4669 ipgp->g_value.v_integer.i_max = ISCSI_MAX_ERROR_RECOVERY_LEVEL; 4670 ipgp->g_value.v_integer.i_incr = 1; 4671 ipgp->g_value.v_settable = B_FALSE; 4672 break; 4673 case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH: 4674 ipgp->g_value.v_integer.i_current = 4675 params->first_burst_length; 4676 ipgp->g_value.v_integer.i_default = 4677 ISCSI_DEFAULT_FIRST_BURST_LENGTH; 4678 ipgp->g_value.v_integer.i_min = 512; 4679 ipgp->g_value.v_integer.i_max = ISCSI_MAX_FIRST_BURST_LENGTH; 4680 ipgp->g_value.v_integer.i_incr = 1; 4681 break; 4682 case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH: 4683 ipgp->g_value.v_integer.i_current = 4684 params->max_burst_length; 4685 ipgp->g_value.v_integer.i_default = 4686 ISCSI_DEFAULT_MAX_BURST_LENGTH; 4687 ipgp->g_value.v_integer.i_min = 512; 4688 ipgp->g_value.v_integer.i_max = ISCSI_MAX_BURST_LENGTH; 4689 ipgp->g_value.v_integer.i_incr = 1; 4690 break; 4691 case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS: 4692 ipgp->g_value.v_integer.i_current = 4693 params->max_connections; 4694 ipgp->g_value.v_settable = B_FALSE; 4695 ipgp->g_value.v_integer.i_default = 4696 ISCSI_DEFAULT_MAX_CONNECTIONS; 4697 ipgp->g_value.v_integer.i_min = 1; 4698 ipgp->g_value.v_integer.i_max = ISCSI_MAX_CONNECTIONS; 4699 ipgp->g_value.v_integer.i_incr = 1; 4700 break; 4701 case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T: 4702 ipgp->g_value.v_integer.i_current = 4703 params->max_outstanding_r2t; 4704 ipgp->g_value.v_settable = B_FALSE; 4705 ipgp->g_value.v_integer.i_default = 4706 ISCSI_DEFAULT_MAX_OUT_R2T; 4707 ipgp->g_value.v_integer.i_min = 1; 4708 ipgp->g_value.v_integer.i_max = ISCSI_MAX_OUTSTANDING_R2T; 4709 ipgp->g_value.v_integer.i_incr = 1; 4710 break; 4711 case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH: 4712 ipgp->g_value.v_integer.i_current = 4713 params->max_recv_data_seg_len; 4714 ipgp->g_value.v_integer.i_default = 4715 ISCSI_DEFAULT_MAX_RECV_SEG_LEN; 4716 ipgp->g_value.v_integer.i_min = 512; 4717 ipgp->g_value.v_integer.i_max = 4718 ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; 4719 ipgp->g_value.v_integer.i_incr = 1; 4720 break; 4721 default: 4722 rtn = EINVAL; 4723 } 4724 4725 return (rtn); 4726 } 4727 4728 /* 4729 * +--------------------------------------------------------------------+ 4730 * | End of ioctl utility routines | 4731 * +--------------------------------------------------------------------+ 4732 */ 4733 4734 /* 4735 * iscsi_get_name_from_iqn - Translates a normal iqn/eui into a 4736 * IEEE safe address. IEEE addresses have a number of characters 4737 * set aside as reserved. 4738 */ 4739 static void 4740 iscsi_get_name_from_iqn(char *name, int name_max_len) 4741 { 4742 char *tmp = NULL; 4743 char *oldch = NULL; 4744 char *newch = NULL; 4745 4746 tmp = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP); 4747 4748 for (oldch = &name[0], newch = &tmp[0]; *oldch != '\0'; 4749 oldch++, newch++) { 4750 switch (*oldch) { 4751 case ':': 4752 *newch++ = '%'; 4753 *newch++ = '3'; 4754 *newch = 'A'; 4755 break; 4756 case ' ': 4757 *newch++ = '%'; 4758 *newch++ = '2'; 4759 *newch = '0'; 4760 break; 4761 case '@': 4762 *newch++ = '%'; 4763 *newch++ = '4'; 4764 *newch = '0'; 4765 break; 4766 case '/': 4767 *newch++ = '%'; 4768 *newch++ = '2'; 4769 *newch = 'F'; 4770 break; 4771 default: 4772 *newch = *oldch; 4773 } 4774 } 4775 (void) strncpy(name, tmp, name_max_len); 4776 kmem_free(tmp, MAX_GET_NAME_SIZE); 4777 } 4778 4779 /* 4780 * iscsi_get_name_to_iqn - Converts IEEE safe address back 4781 * into a iscsi iqn/eui. 4782 */ 4783 static void 4784 iscsi_get_name_to_iqn(char *name, int name_max_len) 4785 { 4786 char *tmp = NULL; 4787 char *oldch = NULL; 4788 char *newch = NULL; 4789 4790 tmp = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP); 4791 4792 for (oldch = &name[0], newch = &tmp[0]; *oldch != '\0'; 4793 oldch++, newch++) { 4794 if (*oldch == '%') { 4795 switch (*(oldch+1)) { 4796 case '2': 4797 if (*(oldch+2) == '0') { 4798 *newch = ' '; 4799 oldch += 2; 4800 } else if (*(oldch+2) == 'F') { 4801 *newch = '/'; 4802 oldch += 2; 4803 } else { 4804 *newch = *oldch; 4805 } 4806 break; 4807 case '3': 4808 if (*(oldch+2) == 'A') { 4809 *newch = ':'; 4810 oldch += 2; 4811 } else { 4812 *newch = *oldch; 4813 } 4814 break; 4815 case '4': 4816 if (*(oldch+2) == '0') { 4817 *newch = '@'; 4818 oldch += 2; 4819 } else { 4820 *newch = *oldch; 4821 } 4822 break; 4823 default: 4824 *newch = *oldch; 4825 } 4826 } else { 4827 *newch = *oldch; 4828 } 4829 } 4830 (void) strncpy(name, tmp, name_max_len); 4831 kmem_free(tmp, MAX_GET_NAME_SIZE); 4832 } 4833 4834 /* 4835 * iscsi_get_persisted_param * - a helper to ISCSI_GET_PARAM ioctl 4836 * 4837 * On return 0 means persisted parameter found 4838 */ 4839 int 4840 iscsi_get_persisted_param(uchar_t *name, iscsi_param_get_t *ipgp, 4841 iscsi_login_params_t *params) 4842 { 4843 int rtn = 1; 4844 persistent_param_t *pparam; 4845 4846 if (name == NULL || strlen((char *)name) == 0) { 4847 return (rtn); 4848 } 4849 4850 pparam = (persistent_param_t *)kmem_zalloc(sizeof (*pparam), KM_SLEEP); 4851 4852 if (persistent_param_get((char *)name, pparam) == B_TRUE) { 4853 if (pparam->p_bitmap & (1 << ipgp->g_param)) { 4854 /* Found configured parameter. */ 4855 bcopy(&pparam->p_params, params, sizeof (*params)); 4856 rtn = 0; 4857 } 4858 } 4859 4860 kmem_free(pparam, sizeof (*pparam)); 4861 4862 return (rtn); 4863 } 4864 4865 /* 4866 * iscsi_override_target_default - helper function set the target's default 4867 * login parameter if there is a configured initiator parameter. 4868 * 4869 */ 4870 static void 4871 iscsi_override_target_default(iscsi_hba_t *ihp, iscsi_param_get_t *ipg) 4872 { 4873 persistent_param_t *pp; 4874 iscsi_login_params_t *params; 4875 4876 pp = (persistent_param_t *)kmem_zalloc(sizeof (*pp), KM_SLEEP); 4877 if (persistent_param_get((char *)ihp->hba_name, pp) == B_TRUE) { 4878 if (pp->p_bitmap & (1 << ipg->g_param)) { 4879 params = &pp->p_params; 4880 switch (ipg->g_param) { 4881 case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER: 4882 ipg->g_value.v_bool.b_default = 4883 params->data_sequence_in_order; 4884 break; 4885 case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA: 4886 ipg->g_value.v_bool.b_default = 4887 params->immediate_data; 4888 break; 4889 case ISCSI_LOGIN_PARAM_INITIAL_R2T: 4890 ipg->g_value.v_bool.b_default = 4891 params->initial_r2t; 4892 break; 4893 case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER: 4894 ipg->g_value.v_bool.b_default = 4895 params->data_pdu_in_order; 4896 break; 4897 case ISCSI_LOGIN_PARAM_HEADER_DIGEST: 4898 ipg->g_value.v_integer.i_default = 4899 params->header_digest; 4900 break; 4901 case ISCSI_LOGIN_PARAM_DATA_DIGEST: 4902 ipg->g_value.v_integer.i_default = 4903 params->data_digest; 4904 break; 4905 case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN: 4906 ipg->g_value.v_integer.i_default = 4907 params->default_time_to_retain; 4908 break; 4909 case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT: 4910 ipg->g_value.v_integer.i_default = 4911 params->default_time_to_wait; 4912 break; 4913 case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL: 4914 ipg->g_value.v_integer.i_default = 4915 params->error_recovery_level; 4916 break; 4917 case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH: 4918 ipg->g_value.v_integer.i_default = 4919 params->first_burst_length; 4920 break; 4921 case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH: 4922 ipg->g_value.v_integer.i_default = 4923 params->max_burst_length; 4924 break; 4925 case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS: 4926 ipg->g_value.v_integer.i_default = 4927 params->max_connections; 4928 break; 4929 case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T: 4930 ipg->g_value.v_integer.i_default = 4931 params->max_outstanding_r2t; 4932 break; 4933 case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH: 4934 ipg->g_value.v_integer.i_default = 4935 params->max_xmit_data_seg_len; 4936 break; 4937 default: 4938 break; 4939 } 4940 } 4941 } 4942 kmem_free(pp, sizeof (*pp)); 4943 } 4944 4945 static boolean_t 4946 iscsi_cmp_boot_sess_oid(iscsi_hba_t *ihp, uint32_t oid) 4947 { 4948 iscsi_sess_t *isp = NULL; 4949 4950 if (iscsi_chk_bootlun_mpxio(ihp)) { 4951 for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) { 4952 if ((isp->sess_oid == oid) && isp->sess_boot) { 4953 /* oid is session object */ 4954 break; 4955 } 4956 if ((isp->sess_target_oid == oid) && isp->sess_boot) { 4957 /* 4958 * oid is target object while 4959 * this session is boot session 4960 */ 4961 break; 4962 } 4963 } 4964 if (oid == ihp->hba_oid) { 4965 /* oid is initiator object id */ 4966 return (B_TRUE); 4967 } else if ((isp != NULL) && (isp->sess_boot)) { 4968 /* oid is boot session object id */ 4969 return (B_TRUE); 4970 } 4971 } 4972 return (B_FALSE); 4973 } 4974