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 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * scsa2usb bridge nexus driver: 30 * 31 * This driver supports the following wire transports: 32 * a. Bulk Only transport (see usb_ms_bulkonly.c) 33 * b. CB transport (see usb_ms_cbi.c) 34 * c. CBI transport with interrupt status completion (see usb_ms_cbi.c) 35 * 36 * It handles the following command sets: 37 * a. SCSI 38 * b. ATAPI command set (subset of SCSI command set) 39 * c. UFI command set ( 40 * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf) 41 * 42 * For details on USB Mass Storage Class overview: 43 * http://www.usb.org/developers/devclass_docs/usbmassover_11.pdf 44 */ 45 #if defined(lint) && !defined(DEBUG) 46 #define DEBUG 1 47 #endif 48 49 #include <sys/usb/usba/usbai_version.h> 50 #include <sys/scsi/scsi.h> 51 #include <sys/cdio.h> 52 #include <sys/sunndi.h> 53 #include <sys/esunddi.h> 54 #include <sys/callb.h> 55 #include <sys/kobj.h> 56 #include <sys/kobj_lex.h> 57 #include <sys/strsubr.h> 58 #include <sys/sysmacros.h> 59 60 #include <sys/usb/usba.h> 61 #include <sys/usb/usba/usba_ugen.h> 62 63 #include <sys/usb/usba/usba_private.h> 64 #include <sys/usb/clients/mass_storage/usb_bulkonly.h> 65 #include <sys/usb/scsa2usb/scsa2usb.h> 66 67 /* 68 * Function Prototypes 69 */ 70 static int scsa2usb_attach(dev_info_t *, ddi_attach_cmd_t); 71 static int scsa2usb_info(dev_info_t *, ddi_info_cmd_t, void *, 72 void **); 73 static int scsa2usb_detach(dev_info_t *, ddi_detach_cmd_t); 74 static int scsa2usb_cleanup(dev_info_t *, scsa2usb_state_t *); 75 static void scsa2usb_validate_attrs(scsa2usb_state_t *); 76 static void scsa2usb_create_luns(scsa2usb_state_t *); 77 static int scsa2usb_is_usb(dev_info_t *); 78 static int scsa2usb_fake_inquiry(scsa2usb_state_t *, 79 scsa2usb_cmd_t *, uint_t); 80 static void scsa2usb_do_inquiry(scsa2usb_state_t *, 81 uint_t, uint_t); 82 static int scsa2usb_do_tur(scsa2usb_state_t *, struct scsi_address *); 83 84 /* override property handling */ 85 static void scsa2usb_override(scsa2usb_state_t *); 86 static int scsa2usb_parse_input_str(char *, scsa2usb_ov_t *, 87 scsa2usb_state_t *); 88 static void scsa2usb_override_error(char *, scsa2usb_state_t *); 89 static char *scsa2usb_strtok_r(char *, char *, char **); 90 91 92 /* PANIC callback handling */ 93 static void scsa2usb_panic_callb_init(scsa2usb_state_t *); 94 static void scsa2usb_panic_callb_fini(scsa2usb_state_t *); 95 static boolean_t scsa2usb_panic_callb(void *, int); 96 97 /* SCSA support */ 98 static int scsa2usb_scsi_tgt_probe(struct scsi_device *, int (*)(void)); 99 static int scsa2usb_scsi_tgt_init(dev_info_t *, dev_info_t *, 100 scsi_hba_tran_t *, struct scsi_device *); 101 static void scsa2usb_scsi_tgt_free(dev_info_t *, dev_info_t *, 102 scsi_hba_tran_t *, struct scsi_device *); 103 static struct scsi_pkt *scsa2usb_scsi_init_pkt(struct scsi_address *, 104 struct scsi_pkt *, struct buf *, int, int, 105 int, int, int (*)(), caddr_t); 106 static void scsa2usb_scsi_destroy_pkt(struct scsi_address *, 107 struct scsi_pkt *); 108 static int scsa2usb_scsi_start(struct scsi_address *, struct scsi_pkt *); 109 static int scsa2usb_scsi_abort(struct scsi_address *, struct scsi_pkt *); 110 static int scsa2usb_scsi_reset(struct scsi_address *, int); 111 static int scsa2usb_scsi_getcap(struct scsi_address *, char *, int); 112 static int scsa2usb_scsi_setcap(struct scsi_address *, char *, int, int); 113 static int scsa2usb_scsi_bus_config(dev_info_t *, uint_t, 114 ddi_bus_config_op_t, void *, dev_info_t **); 115 static int scsa2usb_scsi_bus_unconfig(dev_info_t *, uint_t, 116 ddi_bus_config_op_t, void *); 117 118 /* functions for command and transport support */ 119 static void scsa2usb_prepare_pkt(scsa2usb_state_t *, struct scsi_pkt *); 120 static int scsa2usb_cmd_transport(scsa2usb_state_t *, scsa2usb_cmd_t *); 121 static int scsa2usb_check_bulkonly_blacklist_attrs(scsa2usb_state_t *, 122 scsa2usb_cmd_t *, uchar_t); 123 static int scsa2usb_check_ufi_blacklist_attrs(scsa2usb_state_t *, uchar_t, 124 scsa2usb_cmd_t *); 125 static int scsa2usb_handle_scsi_cmd_sub_class(scsa2usb_state_t *, 126 scsa2usb_cmd_t *, struct scsi_pkt *); 127 static int scsa2usb_handle_ufi_subclass_cmd(scsa2usb_state_t *, 128 scsa2usb_cmd_t *, struct scsi_pkt *); 129 130 /* waitQ handling */ 131 static void scsa2usb_work_thread(void *); 132 static void scsa2usb_transport_request(scsa2usb_state_t *, uint_t); 133 static void scsa2usb_flush_waitQ(scsa2usb_state_t *, uint_t, uchar_t); 134 static int scsa2usb_all_waitQs_empty(scsa2usb_state_t *); 135 136 /* auto request sense handling */ 137 static int scsa2usb_create_arq_pkt(scsa2usb_state_t *, 138 struct scsi_address *); 139 static void scsa2usb_delete_arq_pkt(scsa2usb_state_t *); 140 static void scsa2usb_complete_arq_pkt(scsa2usb_state_t *, struct scsi_pkt *, 141 scsa2usb_cmd_t *, struct buf *); 142 143 /* utility functions for any transport */ 144 static int scsa2usb_open_usb_pipes(scsa2usb_state_t *); 145 void scsa2usb_close_usb_pipes(scsa2usb_state_t *); 146 147 static void scsa2usb_fill_up_cdb_len(scsa2usb_cmd_t *, int); 148 static void scsa2usb_fill_up_cdb_lba(scsa2usb_cmd_t *, int); 149 static void scsa2usb_fill_up_ReadCD_cdb_len(scsa2usb_cmd_t *, int, int); 150 static void scsa2usb_fill_up_12byte_cdb_len(scsa2usb_cmd_t *, int, int); 151 static int scsa2usb_read_cd_blk_size(uchar_t); 152 int scsa2usb_rw_transport(scsa2usb_state_t *, struct scsi_pkt *); 153 void scsa2usb_setup_next_xfer(scsa2usb_state_t *, scsa2usb_cmd_t *); 154 155 static mblk_t *scsa2usb_bp_to_mblk(scsa2usb_state_t *); 156 int scsa2usb_handle_data_start(scsa2usb_state_t *, 157 scsa2usb_cmd_t *, usb_bulk_req_t *); 158 void scsa2usb_handle_data_done(scsa2usb_state_t *, 159 scsa2usb_cmd_t *cmd, usb_bulk_req_t *); 160 161 usb_bulk_req_t *scsa2usb_init_bulk_req(scsa2usb_state_t *, 162 size_t, uint_t, usb_req_attrs_t, usb_flags_t); 163 int scsa2usb_bulk_timeout(int); 164 int scsa2usb_clear_ept_stall(scsa2usb_state_t *, uint_t, 165 usb_pipe_handle_t, char *); 166 static void scsa2usb_pkt_completion(scsa2usb_state_t *, struct scsi_pkt *); 167 168 /* event handling */ 169 static int scsa2usb_reconnect_event_cb(dev_info_t *); 170 static int scsa2usb_disconnect_event_cb(dev_info_t *); 171 static int scsa2usb_cpr_suspend(dev_info_t *); 172 static void scsa2usb_cpr_resume(dev_info_t *); 173 static void scsa2usb_restore_device_state(dev_info_t *, scsa2usb_state_t *); 174 175 /* PM handling */ 176 static void scsa2usb_create_pm_components(dev_info_t *, scsa2usb_state_t *); 177 static void scsa2usb_raise_power(scsa2usb_state_t *); 178 static int scsa2usb_pwrlvl0(scsa2usb_state_t *); 179 static int scsa2usb_pwrlvl1(scsa2usb_state_t *); 180 static int scsa2usb_pwrlvl2(scsa2usb_state_t *); 181 static int scsa2usb_pwrlvl3(scsa2usb_state_t *); 182 static int scsa2usb_power(dev_info_t *, int comp, int level); 183 static void scsa2usb_pm_busy_component(scsa2usb_state_t *); 184 static void scsa2usb_pm_idle_component(scsa2usb_state_t *); 185 186 /* external functions for Bulk only (BO) support */ 187 extern int scsa2usb_bulk_only_transport(scsa2usb_state_t *, 188 scsa2usb_cmd_t *); 189 extern int scsa2usb_bulk_only_get_max_lun(scsa2usb_state_t *); 190 191 /* external functions for CB/CBI support */ 192 extern int scsa2usb_cbi_transport(scsa2usb_state_t *, scsa2usb_cmd_t *); 193 extern void scsa2usb_cbi_stop_intr_polling(scsa2usb_state_t *); 194 195 196 /* cmd decoding */ 197 static char *scsa2usb_cmds[] = { 198 "\000tur", 199 "\001rezero", 200 "\003rqsense", 201 "\004format", 202 "\014cartprot", 203 "\022inquiry", 204 "\026tranlba", 205 "\030fmtverify", 206 "\032modesense", 207 "\033start", 208 "\035snddiag", 209 "\036doorlock", 210 "\043formatcap", 211 "\045readcap", 212 "\050read10", 213 "\052write10", 214 "\053seek10", 215 "\056writeverify", 216 "\057verify", 217 "\065synchcache", 218 "\076readlong", 219 "\077writelong", 220 "\102readsubchan", 221 "\103readtoc", 222 "\104readhdr", 223 "\105playaudio10", 224 "\107playaudio_msf", 225 "\110playaudio_ti", 226 "\111playtrk_r10", 227 "\112geteventnotify", 228 "\113pause_resume", 229 "\116stop/play_scan", 230 "\121readdiscinfo", 231 "\122readtrkinfo", 232 "\123reservedtrk", 233 "\124sendopcinfo", 234 "\125modeselect", 235 "\132modesense", 236 "\133closetrksession", 237 "\135sendcuesheet", 238 "\136prin", 239 "\137prout", 240 "\241blankcd", 241 "\245playaudio12", 242 "\250read12", 243 "\251playtrk12", 244 "\252write12", 245 "\254getperf", 246 "\271readcdmsf", 247 "\273setcdspeed", 248 "\275mechanism_sts", 249 "\276readcd", 250 NULL 251 }; 252 253 254 /* 255 * Mass-Storage devices masquerade as "sd" disks. 256 * 257 * These devices may not support all SCSI CDBs in their 258 * entirety due to their hardware implementation limitations. 259 * 260 * As such, following is a list of some of the black-listed 261 * devices w/ the attributes that they do not support. 262 * (See scsa2usb.h for description on each attribute) 263 */ 264 #define X ((uint16_t)(-1)) 265 266 static struct blacklist { 267 uint16_t idVendor; /* vendor ID */ 268 uint16_t idProduct; /* product ID */ 269 uint16_t bcdDevice; /* device release number in bcd */ 270 uint16_t attributes; /* attributes to blacklist */ 271 } scsa2usb_blacklist[] = { 272 /* Iomega Zip100 drive (prototype) with flaky bridge */ 273 {MS_IOMEGA_VID, MS_IOMEGA_PID1_ZIP100, 0, 274 SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_PM}, 275 276 /* Iomega Zip100 drive (newer model) with flaky bridge */ 277 {MS_IOMEGA_VID, MS_IOMEGA_PID2_ZIP100, 0, 278 SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_PM}, 279 280 /* Iomega Zip100 drive (newer model) with flaky bridge */ 281 {MS_IOMEGA_VID, MS_IOMEGA_PID3_ZIP100, 0, 282 SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_PM}, 283 284 /* Iomega Zip250 drive */ 285 {MS_IOMEGA_VID, MS_IOMEGA_PID_ZIP250, 0, SCSA2USB_ATTRS_GET_LUN}, 286 287 /* Iomega Clik! drive */ 288 {MS_IOMEGA_VID, MS_IOMEGA_PID_CLIK, 0, 289 SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_START_STOP}, 290 291 /* SMSC floppy Device - and its clones */ 292 {MS_SMSC_VID, X, 0, SCSA2USB_ATTRS_START_STOP}, 293 294 /* Hagiwara SmartMedia Device */ 295 {MS_HAGIWARA_SYS_COM_VID, MS_HAGIWARA_SYSCOM_PID1, 0, 296 SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_START_STOP}, 297 298 /* Hagiwara CompactFlash Device */ 299 {MS_HAGIWARA_SYS_COM_VID, MS_HAGIWARA_SYSCOM_PID2, 0, 300 SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_START_STOP}, 301 302 /* Hagiwara SmartMedia/CompactFlash Combo Device */ 303 {MS_HAGIWARA_SYS_COM_VID, MS_HAGIWARA_SYSCOM_PID3, 0, 304 SCSA2USB_ATTRS_START_STOP}, 305 306 /* Hagiwara new SM Device */ 307 {MS_HAGIWARA_SYS_COM_VID, MS_HAGIWARA_SYSCOM_PID4, 0, 308 SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_START_STOP}, 309 310 /* Hagiwara new CF Device */ 311 {MS_HAGIWARA_SYS_COM_VID, MS_HAGIWARA_SYSCOM_PID5, 0, 312 SCSA2USB_ATTRS_GET_LUN | SCSA2USB_ATTRS_START_STOP}, 313 314 /* Mitsumi CD-RW Device(s) */ 315 {MS_MITSUMI_VID, X, X, SCSA2USB_ATTRS_BIG_TIMEOUT | 316 SCSA2USB_ATTRS_GET_CONF | SCSA2USB_ATTRS_GET_PERF}, 317 318 /* Neodio Technologies Corporation SM/CF/MS/SD Combo Device */ 319 {MS_NEODIO_VID, MS_NEODIO_DEVICE_3050, 0, 320 SCSA2USB_ATTRS_MODE_SENSE }, 321 322 /* dumb flash devices */ 323 {MS_SONY_FLASH_VID, MS_SONY_FLASH_PID, 0, 324 SCSA2USB_ATTRS_REDUCED_CMD}, 325 326 {MS_TREK_FLASH_VID, MS_TREK_FLASH_PID, 0, 327 SCSA2USB_ATTRS_REDUCED_CMD}, 328 329 {MS_PENN_FLASH_VID, MS_PENN_FLASH_PID, 0, 330 SCSA2USB_ATTRS_REDUCED_CMD}, 331 332 /* SimpleTech UCF-100 CF Device */ 333 {MS_SIMPLETECH_VID, MS_SIMPLETECH_PID1, 0, 334 SCSA2USB_ATTRS_REDUCED_CMD}, 335 336 {MS_ADDONICS_CARD_READER_VID, MS_ADDONICS_CARD_READER_PID, 337 0, SCSA2USB_ATTRS_REDUCED_CMD}, 338 339 /* Acomdata 80GB USB/1394 Hard Disk */ 340 {MS_ACOMDATA_VID, MS_ACOMDATA_PID1, 0, 341 SCSA2USB_ATTRS_USE_CSW_RESIDUE}, 342 343 /* OTi6828 Flash Disk */ 344 {MS_OTI_VID, MS_OTI_DEVICE_6828, 0, 345 SCSA2USB_ATTRS_USE_CSW_RESIDUE}, 346 347 /* AMI Virtual Floppy */ 348 {MS_AMI_VID, MS_AMI_VIRTUAL_FLOPPY, 0, 349 SCSA2USB_ATTRS_NO_MEDIA_CHECK}, 350 351 /* ScanLogic USB Storage Device */ 352 {MS_SCANLOGIC_VID, MS_SCANLOGIC_PID1, 0, 353 SCSA2USB_ATTRS_NO_CAP_ADJUST}, 354 355 /* Super Top USB 2.0 IDE Device */ 356 {MS_SUPERTOP_VID, MS_SUPERTOP_DEVICE_6600, 0, 357 SCSA2USB_ATTRS_USE_CSW_RESIDUE} 358 }; 359 360 361 #define N_SCSA2USB_BLACKLIST (sizeof (scsa2usb_blacklist))/ \ 362 sizeof (struct blacklist) 363 364 /* 365 * Attribute values can be overridden by values 366 * contained in the scsa2usb.conf file. 367 * These arrays define possible user input values. 368 */ 369 370 struct scsa2usb_subclass_protocol_override { 371 char *name; 372 int value; 373 }; 374 375 static struct scsa2usb_subclass_protocol_override scsa2usb_protocol[] = { 376 {"CB", SCSA2USB_CB_PROTOCOL}, 377 {"CBI", SCSA2USB_CBI_PROTOCOL}, 378 {"BO", SCSA2USB_BULK_ONLY_PROTOCOL} 379 }; 380 381 static struct scsa2usb_subclass_protocol_override scsa2usb_subclass[] = { 382 {"SCSI", SCSA2USB_SCSI_CMDSET}, 383 {"ATAPI", SCSA2USB_ATAPI_CMDSET}, 384 {"UFI", SCSA2USB_UFI_CMDSET} 385 }; 386 387 388 #define N_SCSA2USB_SUBC_OVERRIDE (sizeof (scsa2usb_subclass))/ \ 389 sizeof (struct scsa2usb_subclass_protocol_override) 390 391 #define N_SCSA2USB_PROT_OVERRIDE (sizeof (scsa2usb_protocol))/ \ 392 sizeof (struct scsa2usb_subclass_protocol_override) 393 394 /* global variables */ 395 static void *scsa2usb_statep; /* for soft state */ 396 static boolean_t scsa2usb_sync_message = B_TRUE; /* for syncing */ 397 398 /* for debug messages */ 399 uint_t scsa2usb_errmask = (uint_t)DPRINT_MASK_ALL; 400 uint_t scsa2usb_errlevel = USB_LOG_L4; 401 uint_t scsa2usb_instance_debug = (uint_t)-1; 402 uint_t scsa2usb_scsi_bus_config_debug = 0; 403 uint_t scsa2usb_long_timeout = 50 * SCSA2USB_BULK_PIPE_TIMEOUT; 404 405 406 /* 407 * Some devices have problems with big bulk transfers, 408 * transfers >= 128kbytes hang the device. This tunable allows to 409 * limit the maximum bulk transfers rate. 410 */ 411 uint_t scsa2usb_max_bulk_xfer_size = SCSA2USB_MAX_BULK_XFER_SIZE; 412 413 414 #ifdef SCSA2USB_BULK_ONLY_TEST 415 /* 416 * Test BO 13 cases. (See USB Mass Storage Class - Bulk Only Transport). 417 * We are not covering test cases 1, 6, and 12 as these are the "good" 418 * test cases and are tested as part of the normal drive access operations. 419 * 420 * NOTE: This is for testing only. It will be replaced by a uscsi test. 421 * Some are listed here while; other test cases are moved to usb_bulkonly.c 422 */ 423 static int scsa2usb_test_case_5 = 0; 424 int scsa2usb_test_case_8 = 0; 425 int scsa2usb_test_case_10 = 0; 426 static int scsa2usb_test_case_11 = 0; 427 428 static void scsa2usb_test_mblk(scsa2usb_state_t *, boolean_t); 429 #endif /* SCSA2USB_BULK_ONLY_TEST */ 430 431 static int scsa2usb_ugen_open(dev_t *, int, int, cred_t *); 432 static int scsa2usb_ugen_close(dev_t, int, int, cred_t *); 433 static int scsa2usb_ugen_strategy(struct buf *); 434 static int scsa2usb_ugen_read(dev_t, struct uio *, cred_t *); 435 static int scsa2usb_ugen_write(dev_t, struct uio *, cred_t *); 436 static int scsa2usb_ugen_poll(dev_t, short, int, short *, 437 struct pollhead **); 438 439 /* scsa2usb cb_ops */ 440 static struct cb_ops scsa2usb_cbops = { 441 scsa2usb_ugen_open, /* open */ 442 scsa2usb_ugen_close, /* close */ 443 nodev, /* strategy */ 444 nodev, /* print */ 445 nodev, /* dump */ 446 scsa2usb_ugen_read, /* read */ 447 scsa2usb_ugen_write, /* write */ 448 nodev, /* ioctl */ 449 nodev, /* devmap */ 450 nodev, /* mmap */ 451 nodev, /* segmap */ 452 scsa2usb_ugen_poll, /* poll */ 453 ddi_prop_op, /* prop_op */ 454 NULL, /* stream */ 455 D_MP, /* cb_flag */ 456 CB_REV, /* rev */ 457 nodev, /* int (*cb_aread)() */ 458 nodev /* int (*cb_awrite)() */ 459 }; 460 461 /* modloading support */ 462 static struct dev_ops scsa2usb_ops = { 463 DEVO_REV, /* devo_rev, */ 464 0, /* refcnt */ 465 scsa2usb_info, /* info */ 466 nulldev, /* identify */ 467 nulldev, /* probe */ 468 scsa2usb_attach, /* attach */ 469 scsa2usb_detach, /* detach */ 470 nodev, /* reset */ 471 &scsa2usb_cbops, /* driver operations */ 472 NULL, /* bus operations */ 473 scsa2usb_power /* power */ 474 }; 475 476 static struct modldrv modldrv = { 477 &mod_driverops, /* Module type. This one is a driver */ 478 "SCSA to USB Driver", /* Name of the module. */ 479 &scsa2usb_ops, /* driver ops */ 480 }; 481 482 static struct modlinkage modlinkage = { 483 MODREV_1, (void *)&modldrv, NULL 484 }; 485 486 /* event support */ 487 static usb_event_t scsa2usb_events = { 488 scsa2usb_disconnect_event_cb, 489 scsa2usb_reconnect_event_cb, 490 NULL, NULL 491 }; 492 493 int 494 _init(void) 495 { 496 int rval; 497 498 if (((rval = ddi_soft_state_init(&scsa2usb_statep, 499 sizeof (scsa2usb_state_t), SCSA2USB_INITIAL_ALLOC)) != 0)) { 500 501 return (rval); 502 } 503 504 if ((rval = scsi_hba_init(&modlinkage)) != 0) { 505 ddi_soft_state_fini(&scsa2usb_statep); 506 507 return (rval); 508 } 509 510 if ((rval = mod_install(&modlinkage)) != 0) { 511 scsi_hba_fini(&modlinkage); 512 ddi_soft_state_fini(&scsa2usb_statep); 513 514 return (rval); 515 } 516 517 return (rval); 518 } 519 520 521 int 522 _fini(void) 523 { 524 int rval; 525 526 if ((rval = mod_remove(&modlinkage)) == 0) { 527 scsi_hba_fini(&modlinkage); 528 ddi_soft_state_fini(&scsa2usb_statep); 529 } 530 531 return (rval); 532 } 533 534 535 int 536 _info(struct modinfo *modinfop) 537 { 538 return (mod_info(&modlinkage, modinfop)); 539 } 540 541 542 /* 543 * scsa2usb_info : 544 * Get minor number, soft state structure etc. 545 */ 546 /*ARGSUSED*/ 547 static int 548 scsa2usb_info(dev_info_t *dip, ddi_info_cmd_t infocmd, 549 void *arg, void **result) 550 { 551 scsa2usb_state_t *scsa2usbp = NULL; 552 int error = DDI_FAILURE; 553 int instance = SCSA2USB_MINOR_TO_INSTANCE(getminor((dev_t)arg)); 554 555 switch (infocmd) { 556 case DDI_INFO_DEVT2DEVINFO: 557 if (((scsa2usbp = ddi_get_soft_state(scsa2usb_statep, 558 instance)) != NULL) && 559 scsa2usbp->scsa2usb_dip) { 560 *result = scsa2usbp->scsa2usb_dip; 561 error = DDI_SUCCESS; 562 } else { 563 *result = NULL; 564 } 565 break; 566 case DDI_INFO_DEVT2INSTANCE: 567 *result = (void *)(uintptr_t)instance; 568 error = DDI_SUCCESS; 569 break; 570 default: 571 break; 572 } 573 574 return (error); 575 } 576 577 578 /* 579 * scsa2usb_attach: 580 * Attach driver 581 * Allocate a "scsi_hba_tran" - call scsi_hba_tran_alloc() 582 * Invoke scsi_hba_attach_setup 583 * Get the serialno of the device 584 * Open bulk pipes 585 * Create disk child(ren) 586 * Register events 587 * Create and register panic callback 588 * 589 * NOTE: Replaced CBW_DIR_OUT with USB_EP_DIR_OUT and CBW_DIR_IN with 590 * USB_EP_DIR_IN as they are the same #defines. 591 */ 592 static int 593 scsa2usb_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 594 { 595 int instance = ddi_get_instance(dip); 596 int interface; 597 uint_t lun; 598 boolean_t ept_check = B_TRUE; 599 scsi_hba_tran_t *tran; /* scsi transport */ 600 scsa2usb_state_t *scsa2usbp; 601 usb_log_handle_t log_handle; 602 usb_ep_data_t *ep_data; 603 usb_client_dev_data_t *dev_data; 604 usb_alt_if_data_t *altif_data; 605 usb_ugen_info_t usb_ugen_info; 606 607 USB_DPRINTF_L4(DPRINT_MASK_SCSA, NULL, 608 "scsa2usb_attach: dip = 0x%p", (void *)dip); 609 610 switch (cmd) { 611 case DDI_ATTACH: 612 break; 613 case DDI_RESUME: 614 scsa2usb_cpr_resume(dip); 615 616 return (DDI_SUCCESS); 617 default: 618 USB_DPRINTF_L2(DPRINT_MASK_SCSA, NULL, 619 "scsa2usb_attach: failed"); 620 621 return (DDI_FAILURE); 622 } 623 624 /* Allocate softc information */ 625 if (ddi_soft_state_zalloc(scsa2usb_statep, instance) != DDI_SUCCESS) { 626 ddi_prop_remove_all(dip); 627 628 return (DDI_FAILURE); 629 } 630 631 /* get soft state space and initialize */ 632 if ((scsa2usbp = ddi_get_soft_state(scsa2usb_statep, 633 instance)) == NULL) { 634 USB_DPRINTF_L2(DPRINT_MASK_SCSA, NULL, 635 "scsa2usb%d: bad soft state", instance); 636 ddi_prop_remove_all(dip); 637 638 return (DDI_FAILURE); 639 } 640 641 scsa2usbp->scsa2usb_dip = dip; 642 scsa2usbp->scsa2usb_instance = instance; 643 644 /* allocate a log handle for debug/error messages */ 645 scsa2usbp->scsa2usb_log_handle = log_handle = 646 usb_alloc_log_hdl(dip, "s2u", 647 &scsa2usb_errlevel, 648 &scsa2usb_errmask, &scsa2usb_instance_debug, 649 0); 650 651 /* attach to USBA */ 652 if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) { 653 USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle, 654 "usb_client_attach failed"); 655 656 goto fail; 657 } 658 if (usb_get_dev_data(dip, &dev_data, USB_PARSE_LVL_IF, 0) != 659 USB_SUCCESS) { 660 USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle, 661 "usb_get_dev_data failed"); 662 663 goto fail; 664 } 665 666 /* initialize the mutex with the right cookie */ 667 mutex_init(&scsa2usbp->scsa2usb_mutex, NULL, MUTEX_DRIVER, 668 dev_data->dev_iblock_cookie); 669 cv_init(&scsa2usbp->scsa2usb_transport_busy_cv, NULL, CV_DRIVER, NULL); 670 671 for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) { 672 usba_init_list(&scsa2usbp->scsa2usb_waitQ[lun], NULL, 673 dev_data->dev_iblock_cookie); 674 } 675 mutex_enter(&scsa2usbp->scsa2usb_mutex); 676 scsa2usbp->scsa2usb_dip = dip; 677 scsa2usbp->scsa2usb_instance = instance; 678 scsa2usbp->scsa2usb_attrs = SCSA2USB_ALL_ATTRS; 679 scsa2usbp->scsa2usb_dev_data = dev_data; 680 681 682 /* save the default pipe handle */ 683 scsa2usbp->scsa2usb_default_pipe = dev_data->dev_default_ph; 684 685 /* basic inits are done */ 686 scsa2usbp->scsa2usb_flags |= SCSA2USB_FLAGS_LOCKS_INIT; 687 688 USB_DPRINTF_L4(DPRINT_MASK_SCSA, log_handle, 689 "curr_cfg=%ld, curr_if=%d", 690 (long)(dev_data->dev_curr_cfg - &dev_data->dev_cfg[0]), 691 dev_data->dev_curr_if); 692 693 interface = dev_data->dev_curr_if; 694 scsa2usbp->scsa2usb_intfc_num = dev_data->dev_curr_if; 695 696 /* now find out relevant descriptors for alternate 0 */ 697 altif_data = &dev_data->dev_curr_cfg->cfg_if[interface].if_alt[0]; 698 699 if (altif_data->altif_n_ep == 0) { 700 USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle, 701 "invalid alt 0 for interface %d", interface); 702 mutex_exit(&scsa2usbp->scsa2usb_mutex); 703 704 goto fail; 705 } 706 707 /* All CB/CBI, BO devices should have this value set */ 708 if (altif_data->altif_descr.bInterfaceClass != 709 USB_CLASS_MASS_STORAGE) { 710 USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle, 711 "invalid interface class (0x%x)", 712 altif_data->altif_descr.bInterfaceClass); 713 } 714 scsa2usbp->scsa2usb_intfc_descr = altif_data->altif_descr; 715 716 /* figure out the endpoints and copy the descr */ 717 if ((ep_data = usb_lookup_ep_data(dip, dev_data, interface, 0, 0, 718 USB_EP_ATTR_BULK, USB_EP_DIR_OUT)) != NULL) { 719 scsa2usbp->scsa2usb_bulkout_ept = ep_data->ep_descr; 720 } 721 if ((ep_data = usb_lookup_ep_data(dip, dev_data, interface, 0, 0, 722 USB_EP_ATTR_BULK, USB_EP_DIR_IN)) != NULL) { 723 scsa2usbp->scsa2usb_bulkin_ept = ep_data->ep_descr; 724 } 725 if ((ep_data = usb_lookup_ep_data(dip, dev_data, interface, 0, 0, 726 USB_EP_ATTR_INTR, USB_EP_DIR_IN)) != NULL) { 727 scsa2usbp->scsa2usb_intr_ept = ep_data->ep_descr; 728 } 729 730 /* 731 * check here for protocol and subclass supported by this driver 732 * 733 * first check if conf file has override values 734 * Note: override values are not used if supplied values are legal 735 */ 736 scsa2usb_override(scsa2usbp); 737 738 USB_DPRINTF_L3(DPRINT_MASK_SCSA, log_handle, 739 "protocol=0x%x override=0x%x subclass=0x%x override=0x%x", 740 scsa2usbp->scsa2usb_intfc_descr.bInterfaceProtocol, 741 scsa2usbp->scsa2usb_protocol_override, 742 scsa2usbp->scsa2usb_intfc_descr.bInterfaceSubClass, 743 scsa2usbp->scsa2usb_subclass_override); 744 745 switch (scsa2usbp->scsa2usb_intfc_descr.bInterfaceProtocol) { 746 case USB_PROTO_MS_CBI: 747 scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_CB_PROTOCOL; 748 break; 749 case USB_PROTO_MS_CBI_WC: 750 scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_CBI_PROTOCOL; 751 break; 752 case USB_PROTO_MS_ISD_1999_SILICN: 753 case USB_PROTO_MS_BULK_ONLY: 754 scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_BULK_ONLY_PROTOCOL; 755 break; 756 default: 757 if (scsa2usbp->scsa2usb_protocol_override) { 758 scsa2usbp->scsa2usb_cmd_protocol |= 759 scsa2usbp->scsa2usb_protocol_override; 760 USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle, 761 "overriding protocol %x", 762 scsa2usbp->scsa2usb_intfc_descr.bInterfaceProtocol); 763 break; 764 } 765 766 USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle, 767 "unsupported protocol = %x", 768 scsa2usbp->scsa2usb_intfc_descr.bInterfaceProtocol); 769 mutex_exit(&scsa2usbp->scsa2usb_mutex); 770 771 goto fail; 772 } 773 774 switch (scsa2usbp->scsa2usb_intfc_descr.bInterfaceSubClass) { 775 case USB_SUBCLS_MS_SCSI: /* transparent SCSI */ 776 scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_SCSI_CMDSET; 777 break; 778 case USB_SUBCLS_MS_SFF8020I: 779 case USB_SUBCLS_MS_SFF8070I: 780 scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_ATAPI_CMDSET; 781 break; 782 case USB_SUBCLS_MS_UFI: /* UFI */ 783 scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_UFI_CMDSET; 784 break; 785 default: 786 if (scsa2usbp->scsa2usb_subclass_override) { 787 scsa2usbp->scsa2usb_cmd_protocol |= 788 scsa2usbp->scsa2usb_subclass_override; 789 USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle, 790 "overriding subclass %x", 791 scsa2usbp->scsa2usb_intfc_descr.bInterfaceSubClass); 792 break; 793 } 794 795 USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle, 796 "unsupported subclass = %x", 797 scsa2usbp->scsa2usb_intfc_descr.bInterfaceSubClass); 798 mutex_exit(&scsa2usbp->scsa2usb_mutex); 799 800 goto fail; 801 } 802 803 /* check that we have the right set of endpoint descriptors */ 804 if (SCSA2USB_IS_BULK_ONLY(scsa2usbp) || SCSA2USB_IS_CB(scsa2usbp)) { 805 if ((scsa2usbp->scsa2usb_bulkout_ept.bLength == 0) || 806 (scsa2usbp->scsa2usb_bulkin_ept.bLength == 0)) { 807 ept_check = B_FALSE; 808 } 809 } else if (SCSA2USB_IS_CBI(scsa2usbp)) { 810 if ((scsa2usbp->scsa2usb_bulkout_ept.bLength == 0) || 811 (scsa2usbp->scsa2usb_bulkin_ept.bLength == 0) || 812 (scsa2usbp->scsa2usb_intr_ept.bLength == 0)) { 813 ept_check = B_FALSE; 814 } 815 } 816 817 if (ept_check == B_FALSE) { 818 USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle, 819 "scsa2usb%d doesn't support minimum required endpoints", 820 instance); 821 mutex_exit(&scsa2usbp->scsa2usb_mutex); 822 823 goto fail; 824 } 825 826 /* 827 * Validate the black-listed attributes 828 */ 829 scsa2usb_validate_attrs(scsa2usbp); 830 831 /* Print the serial number from the registration data */ 832 if (scsa2usbp->scsa2usb_dev_data->dev_serial) { 833 USB_DPRINTF_L4(DPRINT_MASK_SCSA, 834 scsa2usbp->scsa2usb_log_handle, "Serial Number = %s", 835 scsa2usbp->scsa2usb_dev_data->dev_serial); 836 } 837 838 /* 839 * Allocate a SCSA transport structure 840 */ 841 tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 842 scsa2usbp->scsa2usb_tran = tran; 843 844 /* 845 * initialize transport structure 846 */ 847 tran->tran_hba_private = scsa2usbp; 848 tran->tran_tgt_private = NULL; 849 tran->tran_tgt_init = scsa2usb_scsi_tgt_init; 850 tran->tran_tgt_probe = scsa2usb_scsi_tgt_probe; 851 tran->tran_tgt_free = scsa2usb_scsi_tgt_free; 852 tran->tran_start = scsa2usb_scsi_start; 853 tran->tran_abort = scsa2usb_scsi_abort; 854 tran->tran_reset = scsa2usb_scsi_reset; 855 tran->tran_getcap = scsa2usb_scsi_getcap; 856 tran->tran_setcap = scsa2usb_scsi_setcap; 857 tran->tran_init_pkt = scsa2usb_scsi_init_pkt; 858 tran->tran_destroy_pkt = scsa2usb_scsi_destroy_pkt; 859 tran->tran_dmafree = NULL; 860 tran->tran_sync_pkt = NULL; 861 tran->tran_reset_notify = NULL; 862 tran->tran_get_bus_addr = NULL; 863 tran->tran_get_name = NULL; 864 tran->tran_quiesce = NULL; 865 tran->tran_unquiesce = NULL; 866 tran->tran_bus_reset = NULL; 867 tran->tran_add_eventcall = NULL; 868 tran->tran_get_eventcookie = NULL; 869 tran->tran_post_event = NULL; 870 tran->tran_remove_eventcall = NULL; 871 tran->tran_bus_config = scsa2usb_scsi_bus_config; 872 tran->tran_bus_unconfig = scsa2usb_scsi_bus_unconfig; 873 874 /* 875 * register with SCSA as an HBA 876 * Note that the dma attributes are from parent nexus 877 */ 878 if (scsi_hba_attach_setup(dip, usba_get_hc_dma_attr(dip), tran, 0)) { 879 USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle, 880 "scsi_hba_attach_setup failed"); 881 mutex_exit(&scsa2usbp->scsa2usb_mutex); 882 883 goto fail; 884 } 885 886 scsa2usbp->scsa2usb_flags |= SCSA2USB_FLAGS_HBA_ATTACH_SETUP; 887 888 /* create minor node */ 889 if (ddi_create_minor_node(dip, "scsa2usb", S_IFCHR, 890 instance << SCSA2USB_MINOR_INSTANCE_SHIFT, 891 DDI_NT_SCSI_NEXUS, 0) != DDI_SUCCESS) { 892 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 893 "scsi_attach: ddi_create_minor_node failed"); 894 mutex_exit(&scsa2usbp->scsa2usb_mutex); 895 896 goto fail; 897 } 898 899 /* open pipes and set scsa2usb_flags */ 900 if (scsa2usb_open_usb_pipes(scsa2usbp) == USB_FAILURE) { 901 USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle, 902 "error opening pipes"); 903 mutex_exit(&scsa2usbp->scsa2usb_mutex); 904 905 goto fail; 906 } 907 908 /* set default block size. updated after read cap cmd */ 909 for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) { 910 scsa2usbp->scsa2usb_lbasize[lun] = DEV_BSIZE; 911 } 912 913 mutex_exit(&scsa2usbp->scsa2usb_mutex); 914 915 /* initialize PANIC callback */ 916 scsa2usb_panic_callb_init(scsa2usbp); 917 918 /* finally we are all done 'initializing' the device */ 919 mutex_enter(&scsa2usbp->scsa2usb_mutex); 920 scsa2usbp->scsa2usb_dev_state = USB_DEV_ONLINE; 921 922 /* enable PM, mutex needs to be held across this */ 923 scsa2usb_create_pm_components(dip, scsa2usbp); 924 mutex_exit(&scsa2usbp->scsa2usb_mutex); 925 926 /* register for connect/disconnect events */ 927 if (usb_register_event_cbs(scsa2usbp->scsa2usb_dip, &scsa2usb_events, 928 0) != USB_SUCCESS) { 929 USB_DPRINTF_L2(DPRINT_MASK_SCSA, log_handle, 930 "error cb registering"); 931 goto fail; 932 } 933 934 /* free the dev_data tree, we no longer need it */ 935 usb_free_descr_tree(dip, dev_data); 936 937 scsa2usb_pm_idle_component(scsa2usbp); 938 939 /* log the conf file override string if there is one */ 940 if (scsa2usbp->scsa2usb_override_str) { 941 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 942 "scsa2usb.conf override: %s", 943 scsa2usbp->scsa2usb_override_str); 944 } 945 946 if (usb_owns_device(dip)) { 947 /* get a ugen handle */ 948 bzero(&usb_ugen_info, sizeof (usb_ugen_info)); 949 usb_ugen_info.usb_ugen_flags = 0; 950 usb_ugen_info.usb_ugen_minor_node_ugen_bits_mask = 951 (dev_t)SCSA2USB_MINOR_UGEN_BITS_MASK; 952 usb_ugen_info.usb_ugen_minor_node_instance_mask = 953 (dev_t)~SCSA2USB_MINOR_UGEN_BITS_MASK; 954 scsa2usbp->scsa2usb_ugen_hdl = 955 usb_ugen_get_hdl(dip, &usb_ugen_info); 956 957 if (usb_ugen_attach(scsa2usbp->scsa2usb_ugen_hdl, cmd) != 958 USB_SUCCESS) { 959 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 960 scsa2usbp->scsa2usb_log_handle, 961 "usb_ugen_attach failed"); 962 963 usb_ugen_release_hdl(scsa2usbp->scsa2usb_ugen_hdl); 964 scsa2usbp->scsa2usb_ugen_hdl = NULL; 965 } 966 } 967 968 /* report device */ 969 ddi_report_dev(dip); 970 971 return (DDI_SUCCESS); 972 973 fail: 974 if (scsa2usbp) { 975 (void) scsa2usb_cleanup(dip, scsa2usbp); 976 } 977 978 return (DDI_FAILURE); 979 } 980 981 982 /* 983 * scsa2usb_detach: 984 * detach or suspend driver instance 985 */ 986 static int 987 scsa2usb_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 988 { 989 scsi_hba_tran_t *tran; 990 scsa2usb_state_t *scsa2usbp; 991 int rval; 992 993 tran = ddi_get_driver_private(dip); 994 ASSERT(tran != NULL); 995 996 scsa2usbp = (scsa2usb_state_t *)tran->tran_hba_private; 997 ASSERT(scsa2usbp); 998 999 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1000 "scsa2usb_detach: dip = 0x%p, cmd = %d", (void *)dip, cmd); 1001 1002 switch (cmd) { 1003 case DDI_DETACH: 1004 1005 if (scsa2usb_cleanup(dip, scsa2usbp) != USB_SUCCESS) { 1006 1007 return (DDI_FAILURE); 1008 } 1009 1010 return (DDI_SUCCESS); 1011 case DDI_SUSPEND: 1012 rval = scsa2usb_cpr_suspend(dip); 1013 1014 return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE); 1015 default: 1016 1017 return (DDI_FAILURE); 1018 } 1019 } 1020 1021 1022 /* 1023 * ugen support 1024 */ 1025 /* 1026 * scsa2usb_ugen_open() 1027 * (all ugen opens and pipe opens are by definition exclusive so it is OK 1028 * to count opens) 1029 */ 1030 static int 1031 scsa2usb_ugen_open(dev_t *devp, int flag, int sflag, cred_t *cr) 1032 { 1033 scsa2usb_state_t *scsa2usbp; 1034 int rval; 1035 1036 if ((scsa2usbp = ddi_get_soft_state(scsa2usb_statep, 1037 SCSA2USB_MINOR_TO_INSTANCE(getminor(*devp)))) == NULL) { 1038 /* deferred detach */ 1039 1040 return (ENXIO); 1041 } 1042 1043 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1044 "scsa2usb_ugen_open: dev_t=0x%lx", *devp); 1045 1046 mutex_enter(&scsa2usbp->scsa2usb_mutex); 1047 1048 /* if this is the first ugen open, check on transport busy */ 1049 if (scsa2usbp->scsa2usb_ugen_open_count == 0) { 1050 while (scsa2usbp->scsa2usb_transport_busy || 1051 (scsa2usb_all_waitQs_empty(scsa2usbp) != 1052 USB_SUCCESS)) { 1053 rval = cv_wait_sig( 1054 &scsa2usbp->scsa2usb_transport_busy_cv, 1055 &scsa2usbp->scsa2usb_mutex); 1056 if (rval == 0) { 1057 mutex_exit(&scsa2usbp->scsa2usb_mutex); 1058 1059 return (EINTR); 1060 } 1061 } 1062 scsa2usbp->scsa2usb_transport_busy++; 1063 scsa2usbp->scsa2usb_busy_thread = curthread; 1064 } 1065 scsa2usbp->scsa2usb_ugen_open_count++; 1066 1067 scsa2usb_raise_power(scsa2usbp); 1068 1069 scsa2usb_close_usb_pipes(scsa2usbp); 1070 1071 mutex_exit(&scsa2usbp->scsa2usb_mutex); 1072 1073 rval = usb_ugen_open(scsa2usbp->scsa2usb_ugen_hdl, devp, flag, 1074 sflag, cr); 1075 1076 if (rval) { 1077 mutex_enter(&scsa2usbp->scsa2usb_mutex); 1078 1079 /* reopen the pipes */ 1080 if (--scsa2usbp->scsa2usb_ugen_open_count == 0) { 1081 scsa2usbp->scsa2usb_transport_busy--; 1082 scsa2usbp->scsa2usb_busy_thread = NULL; 1083 cv_signal(&scsa2usbp->scsa2usb_transport_busy_cv); 1084 } 1085 mutex_exit(&scsa2usbp->scsa2usb_mutex); 1086 1087 scsa2usb_pm_idle_component(scsa2usbp); 1088 } 1089 1090 return (rval); 1091 } 1092 1093 1094 /* 1095 * scsa2usb_ugen_close() 1096 */ 1097 static int 1098 scsa2usb_ugen_close(dev_t dev, int flag, int otype, cred_t *cr) 1099 { 1100 int rval; 1101 1102 scsa2usb_state_t *scsa2usbp = ddi_get_soft_state(scsa2usb_statep, 1103 SCSA2USB_MINOR_TO_INSTANCE(getminor(dev))); 1104 1105 if (scsa2usbp == NULL) { 1106 1107 return (ENXIO); 1108 } 1109 1110 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1111 "scsa2usb_ugen_close: dev_t=0x%lx", dev); 1112 1113 rval = usb_ugen_close(scsa2usbp->scsa2usb_ugen_hdl, dev, flag, 1114 otype, cr); 1115 1116 if (rval == 0) { 1117 mutex_enter(&scsa2usbp->scsa2usb_mutex); 1118 1119 /* reopen the pipes */ 1120 if (--scsa2usbp->scsa2usb_ugen_open_count == 0) { 1121 scsa2usbp->scsa2usb_transport_busy--; 1122 scsa2usbp->scsa2usb_busy_thread = NULL; 1123 cv_signal(&scsa2usbp->scsa2usb_transport_busy_cv); 1124 } 1125 mutex_exit(&scsa2usbp->scsa2usb_mutex); 1126 1127 scsa2usb_pm_idle_component(scsa2usbp); 1128 } 1129 1130 return (rval); 1131 } 1132 1133 1134 /* 1135 * scsa2usb_ugen_read/write() 1136 */ 1137 /*ARGSUSED*/ 1138 static int 1139 scsa2usb_ugen_read(dev_t dev, struct uio *uiop, cred_t *credp) 1140 { 1141 scsa2usb_state_t *scsa2usbp = ddi_get_soft_state(scsa2usb_statep, 1142 SCSA2USB_MINOR_TO_INSTANCE(getminor(dev))); 1143 1144 if (scsa2usbp == NULL) { 1145 1146 return (ENXIO); 1147 } 1148 1149 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1150 "scsa2usb_ugen_read: dev_t=0x%lx", dev); 1151 1152 1153 return (usb_ugen_read(scsa2usbp->scsa2usb_ugen_hdl, dev, 1154 uiop, credp)); 1155 } 1156 1157 1158 /*ARGSUSED*/ 1159 static int 1160 scsa2usb_ugen_write(dev_t dev, struct uio *uiop, cred_t *credp) 1161 { 1162 scsa2usb_state_t *scsa2usbp = ddi_get_soft_state(scsa2usb_statep, 1163 SCSA2USB_MINOR_TO_INSTANCE(getminor(dev))); 1164 1165 if (scsa2usbp == NULL) { 1166 1167 return (ENXIO); 1168 } 1169 1170 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1171 "scsa2usb_ugen_write: dev_t=0x%lx", dev); 1172 1173 return (usb_ugen_write(scsa2usbp->scsa2usb_ugen_hdl, 1174 dev, uiop, credp)); 1175 } 1176 1177 1178 /* 1179 * scsa2usb_ugen_poll 1180 */ 1181 static int 1182 scsa2usb_ugen_poll(dev_t dev, short events, 1183 int anyyet, short *reventsp, struct pollhead **phpp) 1184 { 1185 scsa2usb_state_t *scsa2usbp = ddi_get_soft_state(scsa2usb_statep, 1186 SCSA2USB_MINOR_TO_INSTANCE(getminor(dev))); 1187 1188 if (scsa2usbp == NULL) { 1189 1190 return (ENXIO); 1191 } 1192 1193 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1194 "scsa2usb_ugen_poll: dev_t=0x%lx", dev); 1195 1196 return (usb_ugen_poll(scsa2usbp->scsa2usb_ugen_hdl, dev, events, 1197 anyyet, reventsp, phpp)); 1198 } 1199 1200 1201 /* 1202 * scsa2usb_cleanup: 1203 * cleanup whatever attach has setup 1204 */ 1205 static int 1206 scsa2usb_cleanup(dev_info_t *dip, scsa2usb_state_t *scsa2usbp) 1207 { 1208 int rval, i; 1209 scsa2usb_power_t *pm; 1210 uint_t lun; 1211 1212 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1213 "scsa2usb_cleanup:"); 1214 1215 /* wait till the work thread is done */ 1216 mutex_enter(&scsa2usbp->scsa2usb_mutex); 1217 for (i = 0; i < SCSA2USB_DRAIN_TIMEOUT; i++) { 1218 if (scsa2usbp->scsa2usb_work_thread_id == NULL) { 1219 1220 break; 1221 } 1222 mutex_exit(&scsa2usbp->scsa2usb_mutex); 1223 delay(drv_usectohz(1000000)); 1224 mutex_enter(&scsa2usbp->scsa2usb_mutex); 1225 } 1226 mutex_exit(&scsa2usbp->scsa2usb_mutex); 1227 1228 if (i >= SCSA2USB_DRAIN_TIMEOUT) { 1229 1230 return (USB_FAILURE); 1231 } 1232 1233 /* 1234 * Disable the event callbacks first, after this point, event 1235 * callbacks will never get called. Note we shouldn't hold 1236 * mutex while unregistering events because there may be a 1237 * competing event callback thread. Event callbacks are done 1238 * with ndi mutex held and this can cause a potential deadlock. 1239 */ 1240 usb_unregister_event_cbs(scsa2usbp->scsa2usb_dip, &scsa2usb_events); 1241 1242 if (scsa2usbp->scsa2usb_flags & SCSA2USB_FLAGS_LOCKS_INIT) { 1243 /* 1244 * if a waitQ exists, get rid of it before destroying it 1245 */ 1246 for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) { 1247 scsa2usb_flush_waitQ(scsa2usbp, lun, CMD_TRAN_ERR); 1248 usba_destroy_list(&scsa2usbp->scsa2usb_waitQ[lun]); 1249 } 1250 1251 mutex_enter(&scsa2usbp->scsa2usb_mutex); 1252 if (scsa2usbp->scsa2usb_flags & 1253 SCSA2USB_FLAGS_HBA_ATTACH_SETUP) { 1254 (void) scsi_hba_detach(dip); 1255 scsi_hba_tran_free(scsa2usbp->scsa2usb_tran); 1256 } 1257 1258 if (scsa2usbp->scsa2usb_flags & 1259 SCSA2USB_FLAGS_PIPES_OPENED) { 1260 scsa2usb_close_usb_pipes(scsa2usbp); 1261 } 1262 1263 /* Lower the power */ 1264 pm = scsa2usbp->scsa2usb_pm; 1265 1266 if (pm && (scsa2usbp->scsa2usb_dev_state != 1267 USB_DEV_DISCONNECTED)) { 1268 if (pm->scsa2usb_wakeup_enabled) { 1269 mutex_exit(&scsa2usbp->scsa2usb_mutex); 1270 (void) pm_raise_power(dip, 0, 1271 USB_DEV_OS_FULL_PWR); 1272 1273 if ((rval = usb_handle_remote_wakeup(dip, 1274 USB_REMOTE_WAKEUP_DISABLE)) != 1275 USB_SUCCESS) { 1276 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 1277 scsa2usbp->scsa2usb_log_handle, 1278 "disable remote wakeup failed " 1279 "(%d)", rval); 1280 } 1281 } else { 1282 mutex_exit(&scsa2usbp->scsa2usb_mutex); 1283 } 1284 1285 (void) pm_lower_power(dip, 0, USB_DEV_OS_PWR_OFF); 1286 1287 mutex_enter(&scsa2usbp->scsa2usb_mutex); 1288 } 1289 1290 if (pm) { 1291 kmem_free(pm, sizeof (scsa2usb_power_t)); 1292 } 1293 1294 if (scsa2usbp->scsa2usb_override_str) { 1295 kmem_free(scsa2usbp->scsa2usb_override_str, 1296 strlen(scsa2usbp->scsa2usb_override_str) + 1); 1297 scsa2usbp->scsa2usb_override_str = NULL; 1298 } 1299 1300 /* remove the minor nodes */ 1301 ddi_remove_minor_node(dip, NULL); 1302 1303 /* Cancel the registered panic callback */ 1304 scsa2usb_panic_callb_fini(scsa2usbp); 1305 1306 mutex_exit(&scsa2usbp->scsa2usb_mutex); 1307 1308 mutex_destroy(&scsa2usbp->scsa2usb_mutex); 1309 cv_destroy(&scsa2usbp->scsa2usb_transport_busy_cv); 1310 } 1311 1312 usb_client_detach(scsa2usbp->scsa2usb_dip, 1313 scsa2usbp->scsa2usb_dev_data); 1314 1315 if (scsa2usbp->scsa2usb_ugen_hdl) { 1316 (void) usb_ugen_detach(scsa2usbp->scsa2usb_ugen_hdl, 1317 DDI_DETACH); 1318 usb_ugen_release_hdl(scsa2usbp->scsa2usb_ugen_hdl); 1319 } 1320 1321 usb_free_log_hdl(scsa2usbp->scsa2usb_log_handle); 1322 1323 ddi_prop_remove_all(dip); 1324 1325 ddi_soft_state_free(scsa2usb_statep, ddi_get_instance(dip)); 1326 1327 return (USB_SUCCESS); 1328 } 1329 1330 1331 /* 1332 * scsa2usb_override: 1333 * some devices may be attached even though their subclass or 1334 * protocol info is not according to spec. 1335 * these can be determined by the 'subclass-protocol-override' 1336 * property set in the conf file. 1337 */ 1338 static void 1339 scsa2usb_override(scsa2usb_state_t *scsa2usbp) 1340 { 1341 scsa2usb_ov_t ov; 1342 char **override_str = NULL; 1343 char *override_str_cpy; 1344 uint_t override_str_len, override_str_cpy_len; 1345 uint_t i; 1346 usb_dev_descr_t *descr = scsa2usbp->scsa2usb_dev_data->dev_descr; 1347 1348 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 1349 1350 scsa2usbp->scsa2usb_subclass_override = 1351 scsa2usbp->scsa2usb_protocol_override = 0; 1352 1353 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, scsa2usbp->scsa2usb_dip, 1354 DDI_PROP_DONTPASS, "attribute-override-list", 1355 &override_str, &override_str_len) != DDI_PROP_SUCCESS) { 1356 1357 return; 1358 } 1359 1360 /* parse each string in the subclass-protocol-override property */ 1361 for (i = 0; i < override_str_len; i++) { 1362 1363 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1364 "override_str[%d] = %s", i, override_str[i]); 1365 1366 /* 1367 * save a copy of the override string for possible 1368 * inclusion in soft state later 1369 */ 1370 override_str_cpy_len = strlen(override_str[i]) + 1; 1371 override_str_cpy = kmem_zalloc(override_str_cpy_len, KM_SLEEP); 1372 (void) strcpy(override_str_cpy, override_str[i]); 1373 1374 bzero(&ov, sizeof (scsa2usb_ov_t)); 1375 1376 if (scsa2usb_parse_input_str(override_str[i], &ov, 1377 scsa2usbp) == USB_FAILURE) { 1378 kmem_free(override_str_cpy, override_str_cpy_len); 1379 continue; 1380 } 1381 1382 /* 1383 * see if subclass/protocol needs to be overridden for device 1384 * or if device should not be power managed 1385 * if there'a a match, save the override string in soft state 1386 */ 1387 if (((descr->idVendor == (uint16_t)ov.vid) || (ov.vid == 0)) && 1388 ((descr->idProduct == (uint16_t)ov.pid) || (ov.pid == 0)) && 1389 ((descr->bcdDevice == (uint16_t)ov.rev) || (ov.rev == 0))) { 1390 scsa2usbp->scsa2usb_subclass_override = ov.subclass; 1391 scsa2usbp->scsa2usb_protocol_override = ov.protocol; 1392 1393 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 1394 scsa2usbp->scsa2usb_log_handle, 1395 "vid=0x%x pid=0x%x rev=0x%x subclass=0x%x " 1396 "protocol=0x%x " 1397 "pmoff=%d fake_removable=%d modesense=%d " 1398 "reduced-cmd-support=%d", 1399 ov.vid, ov.pid, ov.rev, ov.subclass, ov.protocol, 1400 ov.pmoff, ov.fake_removable, ov.no_modesense, 1401 ov.reduced_cmd_support); 1402 1403 if (ov.pmoff) { 1404 scsa2usbp->scsa2usb_attrs &= ~SCSA2USB_ATTRS_PM; 1405 } 1406 if (ov.fake_removable) { 1407 scsa2usbp->scsa2usb_attrs &= 1408 ~SCSA2USB_ATTRS_RMB; 1409 } 1410 if (ov.no_modesense) { 1411 scsa2usbp->scsa2usb_attrs &= 1412 ~SCSA2USB_ATTRS_MODE_SENSE; 1413 } 1414 if (ov.reduced_cmd_support) { 1415 scsa2usbp->scsa2usb_attrs &= 1416 ~SCSA2USB_ATTRS_REDUCED_CMD; 1417 } 1418 scsa2usbp->scsa2usb_override_str = override_str_cpy; 1419 break; 1420 } else { 1421 kmem_free(override_str_cpy, override_str_cpy_len); 1422 } 1423 } 1424 1425 ddi_prop_free(override_str); 1426 } 1427 1428 1429 /* 1430 * scsa2usb_parse_input_str: 1431 * parse one conf file subclass-protocol-override string 1432 * return vendor id, product id, revision, subclass, protocol 1433 * function return is success or failure 1434 */ 1435 static int 1436 scsa2usb_parse_input_str(char *str, scsa2usb_ov_t *ovp, 1437 scsa2usb_state_t *scsa2usbp) 1438 { 1439 char *input_field, *input_value; 1440 char *lasts; 1441 uint_t i; 1442 u_longlong_t value; 1443 1444 /* parse all the input pairs in the string */ 1445 for (input_field = scsa2usb_strtok_r(str, "=", &lasts); 1446 input_field != NULL; 1447 input_field = scsa2usb_strtok_r(lasts, "=", &lasts)) { 1448 1449 if ((input_value = scsa2usb_strtok_r(lasts, " ", &lasts)) == 1450 NULL) { 1451 scsa2usb_override_error("format", scsa2usbp); 1452 1453 return (USB_FAILURE); 1454 } 1455 /* if input value is a 'don't care', skip to the next pair */ 1456 if (strcmp(input_value, "*") == 0) { 1457 continue; 1458 } 1459 if (strcasecmp(input_field, "vid") == 0) { 1460 if (kobj_getvalue(input_value, &value) == -1) { 1461 scsa2usb_override_error("vendor id", scsa2usbp); 1462 1463 return (USB_FAILURE); 1464 } 1465 ovp->vid = (int)value; 1466 } else if (strcasecmp(input_field, "pid") == 0) { 1467 if (kobj_getvalue(input_value, &value) == -1) { 1468 scsa2usb_override_error("product id", 1469 scsa2usbp); 1470 1471 return (USB_FAILURE); 1472 } 1473 ovp->pid = (int)value; 1474 } else if (strcasecmp(input_field, "rev") == 0) { 1475 if (kobj_getvalue(input_value, &value) == -1) { 1476 scsa2usb_override_error("revision id", 1477 scsa2usbp); 1478 1479 return (USB_FAILURE); 1480 } 1481 ovp->rev = (int)value; 1482 } else if (strcasecmp(input_field, "subclass") == 0) { 1483 for (i = 0; i < N_SCSA2USB_SUBC_OVERRIDE; i++) { 1484 if (strcasecmp(input_value, 1485 scsa2usb_subclass[i].name) == 0) { 1486 ovp->subclass = 1487 scsa2usb_subclass[i].value; 1488 break; 1489 } 1490 } 1491 if (ovp->subclass == 0) { 1492 scsa2usb_override_error("subclass", scsa2usbp); 1493 1494 return (USB_FAILURE); 1495 } 1496 } else if (strcasecmp(input_field, "protocol") == 0) { 1497 for (i = 0; i < N_SCSA2USB_PROT_OVERRIDE; i++) { 1498 if (strcasecmp(input_value, 1499 scsa2usb_protocol[i].name) == 0) { 1500 ovp->protocol = 1501 scsa2usb_protocol[i].value; 1502 break; 1503 } 1504 } 1505 if (ovp->protocol == 0) { 1506 scsa2usb_override_error("protocol", scsa2usbp); 1507 1508 return (USB_FAILURE); 1509 } 1510 } else if (strcasecmp(input_field, "pm") == 0) { 1511 if (strcasecmp(input_value, "off") == 0) { 1512 ovp->pmoff = 1; 1513 break; 1514 } else { 1515 scsa2usb_override_error("pm", scsa2usbp); 1516 1517 return (USB_FAILURE); 1518 } 1519 } else if (strcasecmp(input_field, "removable") == 0) { 1520 if (strcasecmp(input_value, "true") == 0) { 1521 ovp->fake_removable = 1; 1522 break; 1523 } else { 1524 scsa2usb_override_error("removable", scsa2usbp); 1525 1526 return (USB_FAILURE); 1527 } 1528 } else if (strcasecmp(input_field, "modesense") == 0) { 1529 if (strcasecmp(input_value, "false") == 0) { 1530 ovp->no_modesense = 1; 1531 break; 1532 } else { 1533 scsa2usb_override_error("modesense", 1534 scsa2usbp); 1535 1536 return (USB_FAILURE); 1537 } 1538 } else if (strcasecmp(input_field, 1539 "reduced-cmd-support") == 0) { 1540 if (strcasecmp(input_value, "true") == 0) { 1541 ovp->reduced_cmd_support = 1; 1542 break; 1543 } else { 1544 scsa2usb_override_error( 1545 "reduced-cmd-support", scsa2usbp); 1546 1547 return (USB_FAILURE); 1548 } 1549 } else { 1550 scsa2usb_override_error(input_field, scsa2usbp); 1551 1552 return (USB_FAILURE); 1553 } 1554 } 1555 1556 return (USB_SUCCESS); 1557 } 1558 1559 1560 /* 1561 * scsa2usb_override_error: 1562 * print an error message if conf file string is bad format 1563 */ 1564 static void 1565 scsa2usb_override_error(char *input_field, scsa2usb_state_t *scsa2usbp) 1566 { 1567 USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1568 "invalid %s in scsa2usb.conf file entry", input_field); 1569 } 1570 1571 /* 1572 * scsa2usb_strtok_r: 1573 * parse a list of tokens 1574 */ 1575 static char * 1576 scsa2usb_strtok_r(char *p, char *sep, char **lasts) 1577 { 1578 char *e; 1579 char *tok = NULL; 1580 1581 if (p == 0 || *p == 0) { 1582 1583 return (NULL); 1584 } 1585 1586 e = p+strlen(p); 1587 1588 do { 1589 if (strchr(sep, *p) != NULL) { 1590 if (tok != NULL) { 1591 *p = 0; 1592 *lasts = p+1; 1593 1594 return (tok); 1595 } 1596 } else if (tok == NULL) { 1597 tok = p; 1598 } 1599 } while (++p < e); 1600 1601 *lasts = NULL; 1602 1603 return (tok); 1604 } 1605 1606 1607 /* 1608 * scsa2usb_validate_attrs: 1609 * many devices have BO/CB/CBI protocol support issues. 1610 * use vendor/product info to reset the 1611 * individual erroneous attributes 1612 * 1613 * NOTE: we look at only device at a time (at attach time) 1614 */ 1615 static void 1616 scsa2usb_validate_attrs(scsa2usb_state_t *scsa2usbp) 1617 { 1618 int i, mask; 1619 usb_dev_descr_t *desc = scsa2usbp->scsa2usb_dev_data->dev_descr; 1620 1621 if (!SCSA2USB_IS_BULK_ONLY(scsa2usbp)) { 1622 scsa2usbp->scsa2usb_attrs &= ~SCSA2USB_ATTRS_GET_LUN; 1623 } 1624 1625 /* determine if this device is on the blacklist */ 1626 for (i = 0; i < N_SCSA2USB_BLACKLIST; i++) { 1627 if ((scsa2usb_blacklist[i].idVendor == desc->idVendor) && 1628 ((scsa2usb_blacklist[i].idProduct == desc->idProduct) || 1629 (scsa2usb_blacklist[i].idProduct == X))) { 1630 scsa2usbp->scsa2usb_attrs &= 1631 ~(scsa2usb_blacklist[i].attributes); 1632 break; 1633 } 1634 } 1635 1636 /* 1637 * Mitsumi's CD-RW drives subclass isn't UFI. 1638 * But they support UFI command-set (this code ensures that) 1639 * NOTE: This is a special case, and is being called out so. 1640 */ 1641 if (desc->idVendor == MS_MITSUMI_VID) { 1642 mask = scsa2usbp->scsa2usb_cmd_protocol & SCSA2USB_CMDSET_MASK; 1643 if (mask) { 1644 scsa2usbp->scsa2usb_cmd_protocol &= ~mask; 1645 } 1646 scsa2usbp->scsa2usb_cmd_protocol |= SCSA2USB_UFI_CMDSET; 1647 } 1648 1649 if (scsa2usbp->scsa2usb_attrs != SCSA2USB_ALL_ATTRS) { 1650 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 1651 scsa2usbp->scsa2usb_log_handle, 1652 "scsa2usb attributes modified: 0x%x", 1653 scsa2usbp->scsa2usb_attrs); 1654 } 1655 } 1656 1657 1658 /* 1659 * scsa2usb_create_luns: 1660 * check the number of luns but continue if the check fails, 1661 * create child nodes for each lun 1662 */ 1663 static void 1664 scsa2usb_create_luns(scsa2usb_state_t *scsa2usbp) 1665 { 1666 int lun, rval; 1667 char *compatible[MAX_COMPAT_NAMES]; /* compatible names */ 1668 dev_info_t *cdip; 1669 uchar_t dtype; 1670 char *node_name; 1671 char *driver_name = NULL; 1672 1673 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1674 "scsa2usb_create_luns:"); 1675 1676 mutex_enter(&scsa2usbp->scsa2usb_mutex); 1677 1678 /* Set n_luns to 1 by default (for floppies and other devices) */ 1679 scsa2usbp->scsa2usb_n_luns = 1; 1680 1681 /* 1682 * Check if there are any device out there which don't 1683 * support the GET_MAX_LUN command. If so, don't issue 1684 * control request to them. 1685 */ 1686 if ((scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_GET_LUN) == 0) { 1687 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1688 "get_max_lun cmd not supported"); 1689 } else { 1690 if (SCSA2USB_IS_BULK_ONLY(scsa2usbp)) { 1691 scsa2usbp->scsa2usb_n_luns = 1692 scsa2usb_bulk_only_get_max_lun(scsa2usbp); 1693 } 1694 } 1695 1696 USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1697 "scsa2usb_create_luns: %d luns found", scsa2usbp->scsa2usb_n_luns); 1698 1699 /* 1700 * create disk child for each lun 1701 */ 1702 for (lun = 0; lun < scsa2usbp->scsa2usb_n_luns; lun++) { 1703 ASSERT(scsa2usbp->scsa2usb_lun_dip[lun] == NULL); 1704 1705 /* do an inquiry to get the dtype of this lun */ 1706 scsa2usb_do_inquiry(scsa2usbp, 0, lun); 1707 1708 dtype = scsa2usbp->scsa2usb_lun_inquiry[lun]. 1709 inq_dtype & DTYPE_MASK; 1710 1711 USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1712 "dtype[%d]=0x%x", lun, dtype); 1713 1714 driver_name = NULL; 1715 1716 switch (dtype) { 1717 case DTYPE_DIRECT: 1718 case DTYPE_RODIRECT: 1719 case DTYPE_OPTICAL: 1720 node_name = "disk"; 1721 driver_name = "sd"; 1722 1723 break; 1724 case DTYPE_SEQUENTIAL: 1725 node_name = "tape"; 1726 driver_name = "st"; 1727 1728 break; 1729 case DTYPE_PRINTER: 1730 node_name = "printer"; 1731 1732 break; 1733 case DTYPE_PROCESSOR: 1734 node_name = "processor"; 1735 1736 break; 1737 case DTYPE_WORM: 1738 node_name = "worm"; 1739 1740 break; 1741 case DTYPE_SCANNER: 1742 node_name = "scanner"; 1743 1744 break; 1745 case DTYPE_CHANGER: 1746 node_name = "changer"; 1747 1748 break; 1749 case DTYPE_COMM: 1750 node_name = "comm"; 1751 1752 break; 1753 case DTYPE_ARRAY_CTRL: 1754 node_name = "array_ctrl"; 1755 1756 break; 1757 case DTYPE_ESI: 1758 node_name = "esi"; 1759 driver_name = "ses"; 1760 1761 break; 1762 default: 1763 node_name = "generic"; 1764 1765 break; 1766 } 1767 1768 if (driver_name) { 1769 compatible[0] = driver_name; 1770 } 1771 1772 ndi_devi_alloc_sleep(scsa2usbp->scsa2usb_dip, node_name, 1773 (pnode_t)DEVI_SID_NODEID, &cdip); 1774 1775 /* attach target & lun properties */ 1776 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", 0); 1777 if (rval != DDI_PROP_SUCCESS) { 1778 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 1779 scsa2usbp->scsa2usb_log_handle, 1780 "ndi_prop_update_int target failed %d", rval); 1781 (void) ndi_devi_free(cdip); 1782 continue; 1783 } 1784 1785 rval = ndi_prop_create_boolean(DDI_DEV_T_NONE, cdip, 1786 "hotpluggable"); 1787 if (rval != DDI_PROP_SUCCESS) { 1788 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 1789 scsa2usbp->scsa2usb_log_handle, 1790 "ndi_prop_create_boolean hotpluggable failed %d", 1791 rval); 1792 ddi_prop_remove_all(cdip); 1793 (void) ndi_devi_free(cdip); 1794 continue; 1795 } 1796 /* 1797 * Some devices don't support LOG SENSE, so tells 1798 * sd driver not to send this command. 1799 */ 1800 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, 1801 "pm-capable", 1); 1802 if (rval != DDI_PROP_SUCCESS) { 1803 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 1804 scsa2usbp->scsa2usb_log_handle, 1805 "ndi_prop_update_int pm-capable failed %d", rval); 1806 ddi_prop_remove_all(cdip); 1807 (void) ndi_devi_free(cdip); 1808 continue; 1809 } 1810 1811 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", lun); 1812 if (rval != DDI_PROP_SUCCESS) { 1813 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 1814 scsa2usbp->scsa2usb_log_handle, 1815 "ndi_prop_update_int lun failed %d", rval); 1816 ddi_prop_remove_all(cdip); 1817 (void) ndi_devi_free(cdip); 1818 continue; 1819 } 1820 1821 if (driver_name) { 1822 rval = ndi_prop_update_string_array(DDI_DEV_T_NONE, 1823 cdip, "compatible", (char **)compatible, 1824 MAX_COMPAT_NAMES); 1825 if (rval != DDI_PROP_SUCCESS) { 1826 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 1827 scsa2usbp->scsa2usb_log_handle, 1828 "ndi_prop_update_string_array failed %d", 1829 rval); 1830 ddi_prop_remove_all(cdip); 1831 (void) ndi_devi_free(cdip); 1832 continue; 1833 } 1834 } 1835 1836 /* 1837 * add property "usb" so we always verify that it is our child 1838 */ 1839 rval = ndi_prop_create_boolean(DDI_DEV_T_NONE, cdip, "usb"); 1840 if (rval != DDI_PROP_SUCCESS) { 1841 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 1842 scsa2usbp->scsa2usb_log_handle, 1843 "ndi_prop_create_boolean failed %d", rval); 1844 ddi_prop_remove_all(cdip); 1845 (void) ndi_devi_free(cdip); 1846 continue; 1847 } 1848 1849 mutex_exit(&scsa2usbp->scsa2usb_mutex); 1850 (void) ddi_initchild(scsa2usbp->scsa2usb_dip, cdip); 1851 mutex_enter(&scsa2usbp->scsa2usb_mutex); 1852 1853 usba_set_usba_device(cdip, 1854 usba_get_usba_device(scsa2usbp->scsa2usb_dip)); 1855 } 1856 mutex_exit(&scsa2usbp->scsa2usb_mutex); 1857 } 1858 1859 1860 /* 1861 * scsa2usb_is_usb: 1862 * scsa2usb gets called for all possible sd children. 1863 * we can only accept usb children 1864 */ 1865 static int 1866 scsa2usb_is_usb(dev_info_t *dip) 1867 { 1868 if (dip) { 1869 return (ddi_prop_exists(DDI_DEV_T_ANY, dip, 1870 DDI_PROP_DONTPASS, "usb")); 1871 } 1872 return (0); 1873 } 1874 1875 1876 /* 1877 * Panic Stuff 1878 * scsa2usb_panic_callb_init: 1879 * initialize PANIC callb and free allocated resources 1880 */ 1881 static void 1882 scsa2usb_panic_callb_init(scsa2usb_state_t *scsa2usbp) 1883 { 1884 /* 1885 * In case the system panics, the sync command flushes 1886 * dirty FS pages or buffers. This would cause a hang 1887 * in USB. 1888 * The reason for the failure is that we enter 1889 * polled mode (interrupts disabled) and HCD gets stuck 1890 * trying to execute bulk requests 1891 * The panic_callback registered below provides a warning 1892 * that a panic has occurred and from that point onwards, we 1893 * complete each request successfully and immediately. This 1894 * will fake successful syncing so at least the rest of the 1895 * filesystems complete syncing. 1896 */ 1897 scsa2usbp->scsa2usb_panic_info = 1898 kmem_zalloc(sizeof (scsa2usb_cpr_t), KM_SLEEP); 1899 mutex_init(&scsa2usbp->scsa2usb_panic_info->lockp, 1900 NULL, MUTEX_DRIVER, 1901 scsa2usbp->scsa2usb_dev_data->dev_iblock_cookie); 1902 scsa2usbp->scsa2usb_panic_info->statep = scsa2usbp; 1903 scsa2usbp->scsa2usb_panic_info->cpr.cc_lockp = 1904 &scsa2usbp->scsa2usb_panic_info->lockp; 1905 scsa2usbp->scsa2usb_panic_info->cpr.cc_id = 1906 callb_add(scsa2usb_panic_callb, 1907 (void *)scsa2usbp->scsa2usb_panic_info, 1908 CB_CL_PANIC, "scsa2usb"); 1909 } 1910 1911 1912 /* 1913 * scsa2usb_panic_callb_fini: 1914 * cancel out PANIC callb and free allocated resources 1915 */ 1916 static void 1917 scsa2usb_panic_callb_fini(scsa2usb_state_t *scsa2usbp) 1918 { 1919 if (scsa2usbp->scsa2usb_panic_info) { 1920 SCSA2USB_CANCEL_CB(scsa2usbp->scsa2usb_panic_info->cpr.cc_id); 1921 mutex_destroy(&scsa2usbp->scsa2usb_panic_info->lockp); 1922 scsa2usbp->scsa2usb_panic_info->statep = NULL; 1923 kmem_free(scsa2usbp->scsa2usb_panic_info, 1924 sizeof (scsa2usb_cpr_t)); 1925 scsa2usbp->scsa2usb_panic_info = NULL; 1926 } 1927 } 1928 1929 1930 /* 1931 * scsa2usb_panic_callb: 1932 * This routine is called when there is a system panic. 1933 */ 1934 /* ARGSUSED */ 1935 static boolean_t 1936 scsa2usb_panic_callb(void *arg, int code) 1937 { 1938 scsa2usb_cpr_t *cpr_infop; 1939 scsa2usb_state_t *scsa2usbp; 1940 uint_t lun; 1941 1942 _NOTE(NO_COMPETING_THREADS_NOW); 1943 cpr_infop = (scsa2usb_cpr_t *)arg; 1944 scsa2usbp = (scsa2usb_state_t *)cpr_infop->statep; 1945 1946 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1947 "scsa2usb_panic_callb: code=%d", code); 1948 1949 /* 1950 * If we return error here, "sd" prints lots of error 1951 * messages and could retry the same pkt over and over again. 1952 * The sync recovery isn't "smooth" in that case. By faking 1953 * a success return, instead, we force sync to complete. 1954 */ 1955 if (scsa2usbp->scsa2usb_cur_pkt) { 1956 /* 1957 * Do not print the "no sync" warning here. it will then be 1958 * displayed before we actually start syncing. Also we don't 1959 * replace this code with a call to scsa2usb_pkt_completion(). 1960 * NOTE: mutexes are disabled during panic. 1961 */ 1962 scsa2usbp->scsa2usb_cur_pkt->pkt_reason = CMD_CMPLT; 1963 mutex_enter(&scsa2usbp->scsa2usb_mutex); 1964 scsa2usb_pkt_completion(scsa2usbp, scsa2usbp->scsa2usb_cur_pkt); 1965 mutex_exit(&scsa2usbp->scsa2usb_mutex); 1966 } 1967 1968 /* get rid of waitQ */ 1969 for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) { 1970 scsa2usb_flush_waitQ(scsa2usbp, lun, CMD_CMPLT); 1971 } 1972 1973 #ifndef lint 1974 _NOTE(COMPETING_THREADS_NOW); 1975 #endif 1976 1977 return (B_TRUE); 1978 } 1979 1980 /* 1981 * scsa2usb_cpr_suspend 1982 * determine if the device's state can be changed to SUSPENDED 1983 * close pipes if there is no activity 1984 */ 1985 /* ARGSUSED */ 1986 static int 1987 scsa2usb_cpr_suspend(dev_info_t *dip) 1988 { 1989 scsa2usb_state_t *scsa2usbp; 1990 int prev_state; 1991 int rval = USB_FAILURE; 1992 1993 scsa2usbp = ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip)); 1994 1995 ASSERT(scsa2usbp != NULL); 1996 1997 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 1998 "scsa2usb_cpr_suspend:"); 1999 2000 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2001 switch (scsa2usbp->scsa2usb_dev_state) { 2002 case USB_DEV_ONLINE: 2003 case USB_DEV_PWRED_DOWN: 2004 case USB_DEV_DISCONNECTED: 2005 prev_state = scsa2usbp->scsa2usb_dev_state; 2006 scsa2usbp->scsa2usb_dev_state = USB_DEV_SUSPENDED; 2007 2008 /* 2009 * If the device is busy, we cannot suspend 2010 */ 2011 if (SCSA2USB_BUSY(scsa2usbp)) { 2012 USB_DPRINTF_L3(DPRINT_MASK_SCSA, 2013 scsa2usbp->scsa2usb_log_handle, 2014 "scsa2usb_cpr_suspend: I/O active"); 2015 2016 /* fall back to previous state */ 2017 scsa2usbp->scsa2usb_dev_state = prev_state; 2018 } else { 2019 rval = USB_SUCCESS; 2020 } 2021 2022 break; 2023 case USB_DEV_SUSPENDED: 2024 default: 2025 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2026 "scsa2usb_cpr_suspend: Illegal dev state: %d", 2027 scsa2usbp->scsa2usb_dev_state); 2028 2029 break; 2030 } 2031 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2032 2033 if ((rval == USB_SUCCESS) && scsa2usbp->scsa2usb_ugen_hdl) { 2034 rval = usb_ugen_detach(scsa2usbp->scsa2usb_ugen_hdl, 2035 DDI_SUSPEND); 2036 } 2037 2038 return (rval); 2039 } 2040 2041 2042 /* 2043 * scsa2usb_cpr_resume: 2044 * restore device's state 2045 */ 2046 static void 2047 scsa2usb_cpr_resume(dev_info_t *dip) 2048 { 2049 scsa2usb_state_t *scsa2usbp = 2050 ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip)); 2051 2052 ASSERT(scsa2usbp != NULL); 2053 2054 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2055 "scsa2usb_cpr_resume: dip = 0x%p", (void *)dip); 2056 2057 scsa2usb_restore_device_state(dip, scsa2usbp); 2058 2059 if (scsa2usbp->scsa2usb_ugen_hdl) { 2060 (void) usb_ugen_attach(scsa2usbp->scsa2usb_ugen_hdl, 2061 DDI_RESUME); 2062 } 2063 } 2064 2065 2066 /* 2067 * scsa2usb_restore_device_state: 2068 * - raise the device's power 2069 * - reopen all the pipes 2070 */ 2071 static void 2072 scsa2usb_restore_device_state(dev_info_t *dip, scsa2usb_state_t *scsa2usbp) 2073 { 2074 uint_t prev_state; 2075 2076 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2077 "scsa2usb_restore_device_state:"); 2078 2079 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2080 prev_state = scsa2usbp->scsa2usb_dev_state; 2081 2082 scsa2usb_raise_power(scsa2usbp); 2083 2084 ASSERT((prev_state == USB_DEV_DISCONNECTED) || 2085 (prev_state == USB_DEV_SUSPENDED)); 2086 2087 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2088 2089 /* Check for the same device */ 2090 if (usb_check_same_device(dip, scsa2usbp->scsa2usb_log_handle, 2091 USB_LOG_L0, DPRINT_MASK_ALL, USB_CHK_ALL, NULL) != USB_SUCCESS) { 2092 2093 /* change the flags to active */ 2094 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2095 scsa2usbp->scsa2usb_dev_state = USB_DEV_DISCONNECTED; 2096 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2097 2098 scsa2usb_pm_idle_component(scsa2usbp); 2099 2100 return; 2101 } 2102 2103 /* 2104 * if the device had remote wakeup earlier, 2105 * enable it again 2106 */ 2107 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2108 if (scsa2usbp->scsa2usb_pm && 2109 scsa2usbp->scsa2usb_pm->scsa2usb_wakeup_enabled) { 2110 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2111 (void) usb_handle_remote_wakeup(scsa2usbp->scsa2usb_dip, 2112 USB_REMOTE_WAKEUP_ENABLE); 2113 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2114 } 2115 2116 scsa2usbp->scsa2usb_dev_state = USB_DEV_ONLINE; 2117 scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE; 2118 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2119 2120 scsa2usb_pm_idle_component(scsa2usbp); 2121 } 2122 2123 2124 /* 2125 * SCSA entry points: 2126 * 2127 * scsa2usb_scsi_tgt_probe: 2128 * scsa functions are exported by means of the transport table 2129 * Issue a probe to get the inquiry data. 2130 */ 2131 /* ARGSUSED */ 2132 static int 2133 scsa2usb_scsi_tgt_probe(struct scsi_device *sd, int (*waitfunc)(void)) 2134 { 2135 scsi_hba_tran_t *tran; 2136 scsa2usb_state_t *scsa2usbp; 2137 dev_info_t *dip = ddi_get_parent(sd->sd_dev); 2138 int rval; 2139 2140 ASSERT(dip); 2141 2142 tran = ddi_get_driver_private(dip); 2143 ASSERT(tran != NULL); 2144 scsa2usbp = (scsa2usb_state_t *)tran->tran_hba_private; 2145 ASSERT(scsa2usbp); 2146 2147 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2148 "scsa2usb_scsi_tgt_probe:"); 2149 2150 /* if device is disconnected (ie. pipes closed), fail immediately */ 2151 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2152 if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) { 2153 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2154 2155 return (SCSIPROBE_FAILURE); 2156 } 2157 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2158 2159 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2160 "scsa2usb_scsi_tgt_probe: scsi_device = 0x%p", (void *)sd); 2161 2162 if ((rval = scsi_hba_probe(sd, waitfunc)) == SCSIPROBE_EXISTS) { 2163 /* 2164 * respect the removable bit on all USB storage devices 2165 * unless overridden by a scsa2usb.conf entry 2166 */ 2167 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2168 if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_RMB)) { 2169 _NOTE(SCHEME_PROTECTS_DATA("unshared", scsi_inquiry)) 2170 sd->sd_inq->inq_rmb = 1; 2171 } 2172 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2173 } 2174 2175 return (rval); 2176 } 2177 2178 2179 /* 2180 * scsa2usb_scsi_tgt_init: 2181 * check whether we created this child ourselves 2182 */ 2183 /* ARGSUSED */ 2184 static int 2185 scsa2usb_scsi_tgt_init(dev_info_t *dip, dev_info_t *cdip, 2186 scsi_hba_tran_t *tran, struct scsi_device *sd) 2187 { 2188 scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *) 2189 tran->tran_hba_private; 2190 int lun; 2191 int t_len = sizeof (lun); 2192 2193 if (ddi_prop_op(DDI_DEV_T_ANY, cdip, PROP_LEN_AND_VAL_BUF, 2194 DDI_PROP_DONTPASS|DDI_PROP_CANSLEEP, "lun", (caddr_t)&lun, 2195 &t_len) != DDI_PROP_SUCCESS) { 2196 2197 return (DDI_FAILURE); 2198 } 2199 2200 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2201 "scsa2usb_scsi_tgt_init: %s, lun%d", ddi_driver_name(cdip), lun); 2202 2203 /* is this a child we created? */ 2204 if (scsa2usb_is_usb(cdip) == 0) { 2205 2206 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2207 "scsa2usb_scsi_tgt_init: new child %s%d", 2208 ddi_driver_name(cdip), ddi_get_instance(cdip)); 2209 2210 /* 2211 * add property "usb" so we can always verify that it 2212 * is our child 2213 */ 2214 if (ndi_prop_create_boolean(DDI_DEV_T_NONE, cdip, "usb") != 2215 DDI_PROP_SUCCESS) { 2216 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 2217 scsa2usbp->scsa2usb_log_handle, 2218 "ndi_prop_create_boolean failed"); 2219 2220 return (DDI_FAILURE); 2221 } 2222 2223 usba_set_usba_device(cdip, 2224 usba_get_usba_device(scsa2usbp->scsa2usb_dip)); 2225 2226 /* 2227 * we don't store this dip in scsa2usb_lun_dip, there 2228 * might be multiple dips for the same device 2229 */ 2230 2231 return (DDI_SUCCESS); 2232 } 2233 2234 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2235 if ((lun >= scsa2usbp->scsa2usb_n_luns) || 2236 (scsa2usbp->scsa2usb_lun_dip[lun] != NULL)) { 2237 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2238 2239 return (DDI_FAILURE); 2240 } 2241 2242 scsa2usbp->scsa2usb_lun_dip[lun] = cdip; 2243 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2244 2245 return (DDI_SUCCESS); 2246 } 2247 2248 2249 /* 2250 * scsa2usb_scsi_tgt_free: 2251 */ 2252 /* ARGSUSED */ 2253 static void 2254 scsa2usb_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *cdip, 2255 scsi_hba_tran_t *tran, struct scsi_device *sd) 2256 { 2257 scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *) 2258 tran->tran_hba_private; 2259 int lun; 2260 int t_len = sizeof (lun); 2261 2262 /* is this our child? */ 2263 if (scsa2usb_is_usb(cdip) == 0) { 2264 2265 return; 2266 } 2267 2268 if (ddi_prop_op(DDI_DEV_T_ANY, cdip, PROP_LEN_AND_VAL_BUF, 2269 DDI_PROP_DONTPASS|DDI_PROP_CANSLEEP, "lun", (caddr_t)&lun, 2270 &t_len) != DDI_PROP_SUCCESS) { 2271 2272 return; 2273 } 2274 2275 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2276 "scsa2usb_scsi_tgt_free: %s lun%d", ddi_driver_name(cdip), lun); 2277 2278 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2279 if (lun < scsa2usbp->scsa2usb_n_luns) { 2280 if (scsa2usbp->scsa2usb_lun_dip[lun] == cdip) { 2281 scsa2usbp->scsa2usb_lun_dip[lun] = NULL; 2282 } 2283 } 2284 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2285 } 2286 2287 2288 /* 2289 * bus enumeration entry points 2290 */ 2291 static int 2292 scsa2usb_scsi_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op, 2293 void *arg, dev_info_t **child) 2294 { 2295 int circ; 2296 int rval; 2297 2298 scsa2usb_state_t *scsa2usbp = 2299 ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip)); 2300 2301 ASSERT(scsa2usbp != NULL); 2302 2303 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2304 "scsa2usb_scsi_bus_config: op=%d", op); 2305 2306 if (scsa2usb_scsi_bus_config_debug) { 2307 flag |= NDI_DEVI_DEBUG; 2308 } 2309 2310 ndi_devi_enter(dip, &circ); 2311 /* create children if necessary */ 2312 if (DEVI(dip)->devi_child == NULL) { 2313 scsa2usb_create_luns(scsa2usbp); 2314 } 2315 2316 rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0); 2317 2318 ndi_devi_exit(dip, circ); 2319 2320 return (rval); 2321 } 2322 2323 2324 static int 2325 scsa2usb_scsi_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op, 2326 void *arg) 2327 { 2328 scsa2usb_state_t *scsa2usbp = 2329 ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip)); 2330 2331 int circular_count; 2332 int rval = NDI_SUCCESS; 2333 uint_t save_flag = flag; 2334 2335 ASSERT(scsa2usbp != NULL); 2336 2337 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2338 "scsa2usb_scsi_bus_unconfig: op=%d", op); 2339 2340 if (scsa2usb_scsi_bus_config_debug) { 2341 flag |= NDI_DEVI_DEBUG; 2342 } 2343 2344 /* 2345 * first offline and if offlining successful, then 2346 * remove children 2347 */ 2348 if (op == BUS_UNCONFIG_ALL) { 2349 flag &= ~(NDI_DEVI_REMOVE | NDI_UNCONFIG); 2350 } 2351 2352 ndi_devi_enter(dip, &circular_count); 2353 rval = ndi_busop_bus_unconfig(dip, flag, op, arg); 2354 2355 /* 2356 * If unconfig is successful and not part of modunload 2357 * daemon, attempt to remove children. 2358 */ 2359 if (op == BUS_UNCONFIG_ALL && rval == NDI_SUCCESS && 2360 (flag & NDI_AUTODETACH) == 0) { 2361 flag |= NDI_DEVI_REMOVE; 2362 rval = ndi_busop_bus_unconfig(dip, flag, op, arg); 2363 } 2364 ndi_devi_exit(dip, circular_count); 2365 2366 if ((rval != NDI_SUCCESS) && (op == BUS_UNCONFIG_ALL) && 2367 (save_flag & NDI_DEVI_REMOVE)) { 2368 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2369 if (scsa2usbp->scsa2usb_warning_given != B_TRUE) { 2370 USB_DPRINTF_L0(DPRINT_MASK_SCSA, 2371 scsa2usbp->scsa2usb_log_handle, 2372 "Disconnected device was busy, " 2373 "please reconnect."); 2374 scsa2usbp->scsa2usb_warning_given = B_TRUE; 2375 } 2376 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2377 } 2378 2379 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2380 "scsa2usb_scsi_bus_unconfig: rval=%d", rval); 2381 2382 return (rval); 2383 } 2384 2385 2386 /* 2387 * scsa2usb_scsi_init_pkt: 2388 * Set up the scsi_pkt for transport. Also initialize 2389 * scsa2usb_cmd struct for the transport. 2390 * NOTE: We do not do any DMA setup here as USBA framework 2391 * does that for us. 2392 */ 2393 static struct scsi_pkt * 2394 scsa2usb_scsi_init_pkt(struct scsi_address *ap, 2395 struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen, 2396 int tgtlen, int flags, int (*callback)(), caddr_t arg) 2397 { 2398 scsa2usb_cmd_t *cmd; 2399 scsa2usb_state_t *scsa2usbp; 2400 struct scsi_pkt *in_pkt = pkt; 2401 2402 ASSERT(callback == NULL_FUNC || callback == SLEEP_FUNC); 2403 2404 scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap); 2405 2406 /* Print sync message */ 2407 if (ddi_in_panic()) { 2408 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2409 SCSA2USB_PRINT_SYNC_MSG(scsa2usb_sync_message, scsa2usbp); 2410 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2411 /* continue so caller will not hang or complain */ 2412 } 2413 2414 /* allocate a pkt, if none already allocated */ 2415 if (pkt == NULL) { 2416 if (statuslen < sizeof (struct scsi_arq_status)) { 2417 statuslen = sizeof (struct scsi_arq_status); 2418 } 2419 2420 pkt = scsi_hba_pkt_alloc(scsa2usbp->scsa2usb_dip, ap, cmdlen, 2421 statuslen, tgtlen, sizeof (scsa2usb_cmd_t), 2422 callback, arg); 2423 if (pkt == NULL) { 2424 2425 return (NULL); 2426 } 2427 2428 cmd = PKT2CMD(pkt); 2429 cmd->cmd_pkt = pkt; /* back link to pkt */ 2430 cmd->cmd_scblen = statuslen; 2431 cmd->cmd_cdblen = (uchar_t)cmdlen; 2432 2433 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2434 cmd->cmd_tag = scsa2usbp->scsa2usb_tag++; 2435 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2436 2437 cmd->cmd_bp = bp; 2438 /* 2439 * The buffer size of cmd->cmd_scb is constrained 2440 * to sizeof (struct scsi_arq_status), if the scblen 2441 * is bigger than that, we use pkt->pkt_scbp directly. 2442 */ 2443 if (cmd->cmd_scblen == sizeof (struct scsi_arq_status)) { 2444 pkt->pkt_scbp = (opaque_t)&cmd->cmd_scb; 2445 } 2446 2447 usba_init_list(&cmd->cmd_waitQ, (usb_opaque_t)cmd, 2448 scsa2usbp->scsa2usb_dev_data->dev_iblock_cookie); 2449 } else { 2450 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2451 "scsa2usb: pkt != NULL"); 2452 2453 /* nothing to do */ 2454 } 2455 2456 if (bp && (bp->b_bcount != 0)) { 2457 if ((bp_mapin_common(bp, (callback == SLEEP_FUNC) ? 2458 VM_SLEEP : VM_NOSLEEP)) == NULL) { 2459 if (pkt != in_pkt) { 2460 scsi_hba_pkt_free(ap, pkt); 2461 } 2462 2463 return (NULL); 2464 } 2465 2466 USB_DPRINTF_L3(DPRINT_MASK_SCSA, 2467 scsa2usbp->scsa2usb_log_handle, 2468 "scsa2usb_scsi_init_pkt: mapped in 0x%p, addr=0x%p", 2469 (void *)bp, (void *)bp->b_un.b_addr); 2470 } 2471 2472 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2473 "scsa2usb_scsi_init_pkt: ap = 0x%p pkt: 0x%p\n\t" 2474 "bp = 0x%p cmdlen = %x stlen = 0x%x tlen = 0x%x flags = 0x%x", 2475 (void *)ap, (void *)pkt, (void *)bp, cmdlen, statuslen, 2476 tgtlen, flags); 2477 2478 return (pkt); 2479 } 2480 2481 2482 /* 2483 * scsa2usb_scsi_destroy_pkt: 2484 * We are done with the packet. Get rid of it. 2485 */ 2486 static void 2487 scsa2usb_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2488 { 2489 scsa2usb_cmd_t *cmd = PKT2CMD(pkt); 2490 scsa2usb_state_t *scsa2usbp = ADDR2SCSA2USB(ap); 2491 2492 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2493 "scsa2usb_scsi_destroy_pkt: pkt=0x%p", (void *)pkt); 2494 2495 usba_destroy_list(&cmd->cmd_waitQ); 2496 scsi_hba_pkt_free(ap, pkt); 2497 } 2498 2499 2500 /* 2501 * scsa2usb_scsi_start: 2502 * For each command being issued, build up the CDB 2503 * and call scsi_transport to issue the command. This 2504 * function is based on the assumption that USB allows 2505 * a subset of SCSI commands. Other SCSI commands we fail. 2506 */ 2507 static int 2508 scsa2usb_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt) 2509 { 2510 scsa2usb_cmd_t *cmd; 2511 scsa2usb_state_t *scsa2usbp = ADDR2SCSA2USB(ap); 2512 uint_t lun = ap->a_lun; 2513 2514 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2515 2516 cmd = PKT2CMD(pkt); 2517 USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2518 "scsa2usb_scsi_start:\n\t" 2519 "bp: 0x%p ap: 0x%p pkt: 0x%p flag: 0x%x time: 0x%x\n\tcdb0: 0x%x " 2520 "dev_state: 0x%x pkt_state: 0x%x flags: 0x%x pipe_state: 0x%x", 2521 (void *)cmd->cmd_bp, (void *)ap, (void *)pkt, pkt->pkt_flags, 2522 pkt->pkt_time, pkt->pkt_cdbp[0], scsa2usbp->scsa2usb_dev_state, 2523 scsa2usbp->scsa2usb_pkt_state, scsa2usbp->scsa2usb_flags, 2524 scsa2usbp->scsa2usb_pipe_state); 2525 2526 if (pkt->pkt_time == 0) { 2527 USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2528 "pkt submitted with 0 timeout which may cause indefinite " 2529 "hangs"); 2530 } 2531 2532 /* 2533 * if we are in panic, we are in polled mode, so we can just 2534 * accept the request, drop it and return 2535 * if we fail this request, the rest of the file systems do not 2536 * get synced 2537 */ 2538 if (ddi_in_panic()) { 2539 extern int do_polled_io; 2540 2541 ASSERT(do_polled_io); 2542 scsa2usb_prepare_pkt(scsa2usbp, pkt); 2543 SCSA2USB_PRINT_SYNC_MSG(scsa2usb_sync_message, scsa2usbp); 2544 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2545 2546 return (TRAN_ACCEPT); 2547 } 2548 2549 /* we cannot do polling, this should not happen */ 2550 if (pkt->pkt_flags & FLAG_NOINTR) { 2551 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2552 "NOINTR packet: opcode = 0%x", pkt->pkt_cdbp[0]); 2553 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2554 2555 return (TRAN_BADPKT); 2556 } 2557 2558 /* is there a ugen open? */ 2559 if (scsa2usbp->scsa2usb_ugen_open_count) { 2560 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2561 "ugen access in progress (count=%d)", 2562 scsa2usbp->scsa2usb_ugen_open_count); 2563 2564 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2565 2566 return (TRAN_BUSY); 2567 } 2568 2569 /* prepare packet */ 2570 scsa2usb_prepare_pkt(scsa2usbp, pkt); 2571 2572 /* just queue up the requests in the waitQ if below max */ 2573 if (usba_list_entry_count(&scsa2usbp->scsa2usb_waitQ[lun]) > 2574 SCSA2USB_MAX_REQ_PER_LUN) { 2575 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 2576 scsa2usbp->scsa2usb_log_handle, 2577 "scsa2usb_scsi_start: limit (%d) exceeded", 2578 SCSA2USB_MAX_REQ_PER_LUN); 2579 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2580 2581 return (TRAN_BUSY); 2582 } 2583 2584 usba_add_to_list(&scsa2usbp->scsa2usb_waitQ[lun], &cmd->cmd_waitQ); 2585 2586 USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2587 "scsa2usb_work_thread_id=0x%p, count=%d, lun=%d", 2588 (void *)scsa2usbp->scsa2usb_work_thread_id, 2589 usba_list_entry_count(&scsa2usbp->scsa2usb_waitQ[lun]), lun); 2590 2591 /* fire up a thread to start executing the protocol */ 2592 if (scsa2usbp->scsa2usb_work_thread_id == 0) { 2593 if ((usb_async_req(scsa2usbp->scsa2usb_dip, 2594 scsa2usb_work_thread, 2595 (void *)scsa2usbp, USB_FLAGS_SLEEP)) != USB_SUCCESS) { 2596 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 2597 scsa2usbp->scsa2usb_log_handle, 2598 "no work thread started"); 2599 2600 if (usba_rm_from_list( 2601 &scsa2usbp->scsa2usb_waitQ[lun], 2602 &cmd->cmd_waitQ) == USB_SUCCESS) { 2603 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2604 2605 return (TRAN_BUSY); 2606 } else { 2607 2608 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2609 2610 return (TRAN_ACCEPT); 2611 } 2612 } 2613 scsa2usbp->scsa2usb_work_thread_id = (kthread_t *)1; 2614 } 2615 2616 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2617 2618 return (TRAN_ACCEPT); 2619 } 2620 2621 2622 /* 2623 * scsa2usb_scsi_abort: 2624 * Issue SCSI abort command. This function is a NOP. 2625 */ 2626 /* ARGSUSED */ 2627 static int 2628 scsa2usb_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt) 2629 { 2630 scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap); 2631 2632 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2633 "scsa2usb_scsi_abort: pkt = %p", (void *)pkt); 2634 2635 /* if device is disconnected (ie. pipes closed), fail immediately */ 2636 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2637 if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) { 2638 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2639 2640 return (0); 2641 } 2642 2643 /* flush waitQ if target and lun match */ 2644 if ((ap->a_target == pkt->pkt_address.a_target) && 2645 (ap->a_lun == pkt->pkt_address.a_lun)) { 2646 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2647 scsa2usb_flush_waitQ(scsa2usbp, ap->a_lun, CMD_ABORTED); 2648 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2649 } 2650 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2651 2652 return (0); 2653 } 2654 2655 2656 /* 2657 * scsa2usb_scsi_reset: 2658 * device reset may turn the device into a brick and bus reset 2659 * is not applicable. 2660 * just flush the waitQ 2661 * We return success, always. 2662 */ 2663 /* ARGSUSED */ 2664 static int 2665 scsa2usb_scsi_reset(struct scsi_address *ap, int level) 2666 { 2667 scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap); 2668 2669 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2670 "scsa2usb_scsi_reset: ap = 0x%p, level = %d", (void *)ap, level); 2671 2672 /* flush waitQ */ 2673 scsa2usb_flush_waitQ(scsa2usbp, ap->a_lun, CMD_RESET); 2674 2675 return (1); 2676 } 2677 2678 2679 /* 2680 * scsa2usb_scsi_getcap: 2681 * Get SCSI capabilities. 2682 */ 2683 /* ARGSUSED */ 2684 static int 2685 scsa2usb_scsi_getcap(struct scsi_address *ap, char *cap, int whom) 2686 { 2687 int rval = -1; 2688 uint_t cidx; 2689 size_t dev_bsize_cap; 2690 scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap); 2691 ASSERT(scsa2usbp); 2692 2693 if (cap == NULL) { 2694 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2695 "scsa2usb_scsi_getcap: invalid arg, " 2696 "cap = 0x%p whom = %d", (void *)cap, whom); 2697 2698 return (rval); 2699 } 2700 2701 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2702 "scsa2usb_scsi_getcap: cap = %s", cap); 2703 2704 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2705 2706 /* if device is disconnected (ie. pipes closed), fail immediately */ 2707 if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) { 2708 2709 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2710 2711 return (rval); 2712 } 2713 2714 cidx = scsi_hba_lookup_capstr(cap); 2715 switch (cidx) { 2716 case SCSI_CAP_GEOMETRY: 2717 /* Just check and fail immediately if zero, rarely happens */ 2718 if (scsa2usbp->scsa2usb_secsz[ap->a_lun] == 0) { 2719 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 2720 scsa2usbp->scsa2usb_log_handle, 2721 "scsa2usb_scsi_getcap failed:" 2722 "scsa2usbp->scsa2usb_secsz[ap->a_lun] == 0"); 2723 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2724 2725 return (rval); 2726 } 2727 2728 dev_bsize_cap = scsa2usbp->scsa2usb_totalsec[ap->a_lun]; 2729 2730 if (scsa2usbp->scsa2usb_secsz[ap->a_lun] > DEV_BSIZE) { 2731 dev_bsize_cap *= 2732 scsa2usbp->scsa2usb_secsz[ap->a_lun] / DEV_BSIZE; 2733 } else if (scsa2usbp->scsa2usb_secsz[ap->a_lun] < 2734 DEV_BSIZE) { 2735 dev_bsize_cap /= 2736 DEV_BSIZE / scsa2usbp->scsa2usb_secsz[ap->a_lun]; 2737 } 2738 2739 if (dev_bsize_cap < 65536 * 2 * 18) { /* < ~1GB */ 2740 /* unlabeled floppy, 18k per cylinder */ 2741 rval = ((2 << 16) | 18); 2742 } else if (dev_bsize_cap < 65536 * 64 * 32) { /* < 64GB */ 2743 /* 1024k per cylinder */ 2744 rval = ((64 << 16) | 32); 2745 } else if (dev_bsize_cap < 65536 * 255 * 63) { /* < ~500GB */ 2746 /* ~8m per cylinder */ 2747 rval = ((255 << 16) | 63); 2748 } else { /* .. 8TB */ 2749 /* 64m per cylinder */ 2750 rval = ((512 << 16) | 256); 2751 } 2752 break; 2753 2754 case SCSI_CAP_DMA_MAX: 2755 rval = scsa2usbp->scsa2usb_max_bulk_xfer_size; 2756 break; 2757 case SCSI_CAP_SCSI_VERSION: 2758 rval = SCSI_VERSION_2; 2759 break; 2760 case SCSI_CAP_INTERCONNECT_TYPE: 2761 rval = INTERCONNECT_USB; 2762 break; 2763 case SCSI_CAP_ARQ: 2764 /* FALLTHRU */ 2765 case SCSI_CAP_UNTAGGED_QING: 2766 rval = 1; 2767 break; 2768 default: 2769 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2770 "scsa2usb_scsi_getcap: unsupported cap = %s", cap); 2771 break; 2772 } 2773 2774 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2775 "scsa2usb_scsi_getcap: cap = %s, returned = %d", cap, rval); 2776 2777 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2778 2779 return (rval); 2780 } 2781 2782 2783 /* 2784 * scsa2usb_scsi_setcap: 2785 * Set SCSI capabilities. 2786 */ 2787 /* ARGSUSED */ 2788 static int 2789 scsa2usb_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom) 2790 { 2791 int rval = -1; /* default is cap undefined */ 2792 uint_t cidx; 2793 scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)ADDR2SCSA2USB(ap); 2794 ASSERT(scsa2usbp); 2795 2796 if (cap == NULL || whom == 0) { 2797 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2798 "scsa2usb_scsi_setcap: invalid arg"); 2799 2800 return (rval); 2801 } 2802 2803 mutex_enter(&scsa2usbp->scsa2usb_mutex); 2804 /* if device is disconnected (ie. pipes closed), fail immediately */ 2805 if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) { 2806 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2807 2808 return (rval); 2809 } 2810 2811 cidx = scsi_hba_lookup_capstr(cap); 2812 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2813 "scsa2usb_scsi_setcap: ap = 0x%p value = 0x%x whom = 0x%x " 2814 "cidx = 0x%x", (void *)ap, value, whom, cidx); 2815 2816 switch (cidx) { 2817 case SCSI_CAP_SECTOR_SIZE: 2818 if (value) { 2819 scsa2usbp->scsa2usb_secsz[ap->a_lun] = value; 2820 } 2821 break; 2822 case SCSI_CAP_TOTAL_SECTORS: 2823 if (value) { 2824 scsa2usbp->scsa2usb_totalsec[ap->a_lun] = value; 2825 } 2826 break; 2827 case SCSI_CAP_ARQ: 2828 rval = 1; 2829 break; 2830 case SCSI_CAP_DMA_MAX: 2831 case SCSI_CAP_SCSI_VERSION: 2832 case SCSI_CAP_INTERCONNECT_TYPE: 2833 case SCSI_CAP_UNTAGGED_QING: 2834 /* supported but not settable */ 2835 rval = 0; 2836 break; 2837 default: 2838 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2839 "scsa2usb_scsi_setcap: unsupported cap = %s", cap); 2840 break; 2841 } 2842 2843 mutex_exit(&scsa2usbp->scsa2usb_mutex); 2844 2845 return (rval); 2846 } 2847 2848 2849 /* 2850 * scsa2usb - cmd and transport stuff 2851 */ 2852 /* 2853 * scsa2usb_prepare_pkt: 2854 * initialize some fields of the pkt and cmd 2855 * (the pkt may have been resubmitted/retried) 2856 */ 2857 static void 2858 scsa2usb_prepare_pkt(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt) 2859 { 2860 scsa2usb_cmd_t *cmd = PKT2CMD(pkt); 2861 2862 USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2863 "scsa2usb_prepare_pkt: pkt=0x%p cdb: 0x%x (%s)", 2864 (void *)pkt, pkt->pkt_cdbp[0], 2865 scsi_cname(pkt->pkt_cdbp[0], scsa2usb_cmds)); 2866 2867 pkt->pkt_reason = CMD_CMPLT; /* Set reason to pkt_complete */ 2868 pkt->pkt_state = 0; /* Reset next three fields */ 2869 pkt->pkt_statistics = 0; 2870 pkt->pkt_resid = 0; 2871 bzero(pkt->pkt_scbp, cmd->cmd_scblen); /* Set status to good */ 2872 2873 if (cmd) { 2874 cmd->cmd_timeout = pkt->pkt_time; 2875 cmd->cmd_xfercount = 0; /* Reset the fields */ 2876 cmd->cmd_total_xfercount = 0; 2877 cmd->cmd_lba = 0; 2878 cmd->cmd_done = 0; 2879 cmd->cmd_dir = 0; 2880 cmd->cmd_offset = 0; 2881 cmd->cmd_actual_len = cmd->cmd_cdblen; 2882 } 2883 } 2884 2885 2886 /* 2887 * scsa2usb_force_invalid_request 2888 */ 2889 static void 2890 scsa2usb_force_invalid_request(scsa2usb_state_t *scsa2usbp, 2891 scsa2usb_cmd_t *cmd) 2892 { 2893 struct scsi_arq_status *arqp; 2894 2895 USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2896 "scsa2usb_force_invalid_request: pkt = 0x%p", (void *)cmd->cmd_pkt); 2897 2898 if (cmd->cmd_scblen >= sizeof (struct scsi_arq_status)) { 2899 arqp = (struct scsi_arq_status *)cmd->cmd_pkt->pkt_scbp; 2900 bzero(arqp, cmd->cmd_scblen); 2901 2902 arqp->sts_status.sts_chk = 1; 2903 arqp->sts_rqpkt_reason = CMD_CMPLT; 2904 arqp->sts_rqpkt_state = STATE_XFERRED_DATA | 2905 STATE_GOT_BUS | STATE_GOT_STATUS; 2906 arqp->sts_sensedata.es_valid = 1; 2907 arqp->sts_sensedata.es_class = 7; 2908 arqp->sts_sensedata.es_key = KEY_ILLEGAL_REQUEST; 2909 2910 cmd->cmd_pkt->pkt_state = STATE_ARQ_DONE | 2911 STATE_GOT_BUS | STATE_GOT_BUS | STATE_GOT_BUS | 2912 STATE_GOT_STATUS; 2913 #ifdef DEBUG 2914 { 2915 uchar_t *p = (uchar_t *)(&arqp->sts_sensedata); 2916 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 2917 scsa2usbp->scsa2usb_log_handle, 2918 "cdb: %x rqsense: " 2919 "%x %x %x %x %x %x %x %x %x %x " 2920 "%x %x %x %x %x %x %x %x %x %x", 2921 cmd->cmd_pkt->pkt_cdbp[0], 2922 p[0], p[1], p[2], p[3], p[4], 2923 p[5], p[6], p[7], p[8], p[9], 2924 p[10], p[11], p[12], p[13], p[14], 2925 p[15], p[16], p[17], p[18], p[19]); 2926 } 2927 #endif 2928 2929 } 2930 } 2931 2932 2933 /* 2934 * scsa2usb_cmd_transport: 2935 */ 2936 static int 2937 scsa2usb_cmd_transport(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd) 2938 { 2939 int rval, transport; 2940 struct scsi_pkt *pkt; 2941 2942 USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 2943 "scsa2usb_cmd_transport: pkt: 0x%p, cur_pkt = 0x%p", 2944 (void *)cmd->cmd_pkt, (void *)scsa2usbp->scsa2usb_cur_pkt); 2945 2946 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 2947 ASSERT(scsa2usbp->scsa2usb_cur_pkt == NULL); 2948 2949 pkt = scsa2usbp->scsa2usb_cur_pkt = cmd->cmd_pkt; 2950 2951 /* check black-listed attrs first */ 2952 if (SCSA2USB_IS_BULK_ONLY(scsa2usbp)) { 2953 transport = scsa2usb_check_bulkonly_blacklist_attrs(scsa2usbp, 2954 cmd, pkt->pkt_cdbp[0]); 2955 } else if (SCSA2USB_IS_CB(scsa2usbp) || SCSA2USB_IS_CBI(scsa2usbp)) { 2956 transport = scsa2usb_check_ufi_blacklist_attrs(scsa2usbp, 2957 pkt->pkt_cdbp[0], cmd); 2958 } 2959 2960 /* just accept the command */ 2961 if (transport == SCSA2USB_JUST_ACCEPT) { 2962 SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp); 2963 2964 return (TRAN_ACCEPT); 2965 } 2966 2967 /* check command set next */ 2968 if (SCSA2USB_IS_SCSI_CMDSET(scsa2usbp) || 2969 SCSA2USB_IS_ATAPI_CMDSET(scsa2usbp)) { 2970 transport = 2971 scsa2usb_handle_scsi_cmd_sub_class(scsa2usbp, cmd, pkt); 2972 } else if (SCSA2USB_IS_UFI_CMDSET(scsa2usbp)) { 2973 transport = 2974 scsa2usb_handle_ufi_subclass_cmd(scsa2usbp, cmd, pkt); 2975 } else { 2976 transport = SCSA2USB_REJECT; 2977 } 2978 2979 switch (transport) { 2980 case SCSA2USB_TRANSPORT: 2981 if (SCSA2USB_IS_BULK_ONLY(scsa2usbp)) { 2982 rval = scsa2usb_bulk_only_transport(scsa2usbp, cmd); 2983 } else if (SCSA2USB_IS_CB(scsa2usbp) || 2984 SCSA2USB_IS_CBI(scsa2usbp)) { 2985 rval = scsa2usb_cbi_transport(scsa2usbp, cmd); 2986 } else { 2987 rval = TRAN_FATAL_ERROR; 2988 } 2989 break; 2990 case SCSA2USB_JUST_ACCEPT: 2991 SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp); 2992 rval = TRAN_ACCEPT; 2993 break; 2994 default: 2995 rval = TRAN_FATAL_ERROR; 2996 } 2997 2998 return (rval); 2999 } 3000 3001 3002 /* 3003 * scsa2usb_check_bulkonly_blacklist_attrs: 3004 * validate "scsa2usb_blacklist_attrs" (see scsa2usb.h) 3005 * if blacklisted attrs match accept the request 3006 * attributes checked are:- 3007 * SCSA2USB_ATTRS_START_STOP 3008 */ 3009 int 3010 scsa2usb_check_bulkonly_blacklist_attrs(scsa2usb_state_t *scsa2usbp, 3011 scsa2usb_cmd_t *cmd, uchar_t opcode) 3012 { 3013 struct scsi_inquiry *inq = 3014 &scsa2usbp->scsa2usb_lun_inquiry[cmd->cmd_pkt->pkt_address.a_lun]; 3015 3016 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 3017 "scsa2usb_check_bulkonly_blacklist_attrs: opcode = %s", 3018 scsi_cname(opcode, scsa2usb_cmds)); 3019 3020 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 3021 3022 /* 3023 * decode and convert the packet 3024 * for most cmds, we can bcopy the cdb 3025 */ 3026 switch (opcode) { 3027 case SCMD_DOORLOCK: 3028 if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_DOORLOCK)) { 3029 3030 return (SCSA2USB_JUST_ACCEPT); 3031 3032 /* 3033 * only lock the door for CD and DVD drives 3034 */ 3035 } else if ((inq->inq_dtype == DTYPE_RODIRECT) || 3036 (inq->inq_dtype == DTYPE_OPTICAL)) { 3037 3038 if (inq->inq_rmb) { 3039 3040 break; 3041 } 3042 } 3043 3044 return (SCSA2USB_JUST_ACCEPT); 3045 3046 case SCMD_START_STOP: 3047 /* 3048 * these devices don't have mechanics that spin the 3049 * media up and down. So, it doesn't make much sense 3050 * to issue this cmd. 3051 * 3052 * Furthermore, Hagiwara devices do not handle these 3053 * cmds well. just accept this command as success. 3054 */ 3055 if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_START_STOP)) { 3056 3057 return (SCSA2USB_JUST_ACCEPT); 3058 3059 } else if (cmd->cmd_pkt->pkt_cdbp[4] & LOEJECT) { 3060 /* 3061 * if the device is really a removable then 3062 * pass it on to the device, else just accept 3063 */ 3064 if (inq->inq_rmb) { 3065 3066 break; 3067 } 3068 3069 return (SCSA2USB_JUST_ACCEPT); 3070 3071 } else if (!scsa2usbp->scsa2usb_rcvd_not_ready) { 3072 /* 3073 * if we have not received a NOT READY condition, 3074 * just accept since some device choke on this too. 3075 * we do have to let EJECT get through though 3076 */ 3077 return (SCSA2USB_JUST_ACCEPT); 3078 } 3079 3080 break; 3081 case SCMD_INQUIRY: 3082 /* 3083 * Some devices do not handle the inquiry cmd well 3084 * so build an inquiry and accept this command as 3085 * success. 3086 */ 3087 if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_INQUIRY)) { 3088 uchar_t evpd = 0x01; 3089 3090 if (cmd->cmd_cdb[1] & evpd) { 3091 3092 return (SCSA2USB_REJECT); 3093 } 3094 cmd->cmd_pkt->pkt_resid -= 3095 scsa2usb_fake_inquiry(scsa2usbp, cmd, 3096 cmd->cmd_pkt->pkt_address.a_lun); 3097 cmd->cmd_pkt->pkt_state |= STATE_XFERRED_DATA; 3098 3099 return (SCSA2USB_JUST_ACCEPT); 3100 } 3101 break; 3102 3103 /* 3104 * Fake accepting the following Opcodes 3105 * (as most drives don't support these) 3106 * These are needed by format command. 3107 */ 3108 case SCMD_RESERVE: 3109 case SCMD_RELEASE: 3110 case SCMD_PERSISTENT_RESERVE_IN: 3111 case SCMD_PERSISTENT_RESERVE_OUT: 3112 3113 return (SCSA2USB_JUST_ACCEPT); 3114 3115 case SCMD_MODE_SENSE: 3116 case SCMD_MODE_SELECT: 3117 case SCMD_MODE_SENSE_G1: 3118 case SCMD_MODE_SELECT_G1: 3119 if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_MODE_SENSE)) { 3120 if (cmd->cmd_bp) { 3121 cmd->cmd_pkt->pkt_resid = cmd->cmd_bp-> 3122 b_bcount; 3123 } 3124 scsa2usb_force_invalid_request(scsa2usbp, cmd); 3125 3126 return (SCSA2USB_JUST_ACCEPT); 3127 } 3128 3129 break; 3130 default: 3131 3132 break; 3133 } 3134 3135 return (SCSA2USB_TRANSPORT); 3136 } 3137 3138 3139 /* 3140 * scsa2usb_handle_scsi_cmd_sub_class: 3141 * prepare a scsi cmd 3142 * returns SCSA2USB_TRANSPORT, SCSA2USB_REJECT, SCSA2USB_JUST_ACCEPT 3143 */ 3144 int 3145 scsa2usb_handle_scsi_cmd_sub_class(scsa2usb_state_t *scsa2usbp, 3146 scsa2usb_cmd_t *cmd, struct scsi_pkt *pkt) 3147 { 3148 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 3149 "scsa2usb_handle_scsi_cmd_sub_class: cmd = 0x%p pkt = 0x%p", 3150 (void *)cmd, (void *)pkt); 3151 3152 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 3153 3154 bzero(&cmd->cmd_cdb, SCSI_CDB_SIZE); 3155 cmd->cmd_cdb[SCSA2USB_OPCODE] = pkt->pkt_cdbp[0]; /* Set the opcode */ 3156 cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1]; 3157 3158 /* 3159 * decode and convert the packet 3160 * for most cmds, we can bcopy the cdb 3161 */ 3162 switch (pkt->pkt_cdbp[0]) { 3163 case SCMD_FORMAT: 3164 /* 3165 * SCMD_FORMAT used to limit cmd->cmd_xfercount 3166 * to 4 bytes, but this hangs 3167 * formatting dvd media using cdrecord (that is, 3168 * a SCSI FORMAT UNIT command with a parameter list > 4 bytes) 3169 * (bit 4 in cdb1 is the Fmtdata bit) 3170 */ 3171 if ((pkt->pkt_cdbp[1] & 0x10) && cmd->cmd_bp) { 3172 cmd->cmd_xfercount = cmd->cmd_bp->b_bcount; 3173 } else { 3174 cmd->cmd_xfercount = 4; 3175 } 3176 cmd->cmd_dir = CBW_DIR_OUT; 3177 cmd->cmd_actual_len = CDB_GROUP0; 3178 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3179 break; 3180 3181 case SCMD_INQUIRY: 3182 cmd->cmd_dir = CBW_DIR_IN; 3183 cmd->cmd_actual_len = CDB_GROUP0; 3184 cmd->cmd_cdb[SCSA2USB_LBA_0] = pkt->pkt_cdbp[2]; 3185 cmd->cmd_cdb[SCSA2USB_LBA_2] = cmd->cmd_xfercount = 3186 min(SCSA2USB_MAX_INQ_LEN, 3187 cmd->cmd_bp ? cmd->cmd_bp->b_bcount : 0); 3188 break; 3189 3190 case SCMD_READ_CAPACITY: 3191 cmd->cmd_dir = CBW_DIR_IN; 3192 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3193 cmd->cmd_xfercount = sizeof (scsa2usb_read_cap_t); 3194 break; 3195 3196 /* 3197 * SCMD_READ/SCMD_WRITE are converted to G1 cmds 3198 * (as ATAPI devices don't recognize G0 commands) 3199 * 3200 * SCMD_READ_LONG/SCMD_WRITE_LONG are handled in 3201 * scsa2usb_rw_transport() along with other commands. 3202 * 3203 * USB Host Controllers cannot handle large (read/write) 3204 * xfers. We split the large request to chunks of 3205 * smaller ones to meet the HCD limitations. 3206 */ 3207 case SCMD_READ: 3208 case SCMD_WRITE: 3209 case SCMD_READ_G1: 3210 case SCMD_WRITE_G1: 3211 case SCMD_READ_G5: 3212 case SCMD_WRITE_G5: 3213 case SCMD_READ_LONG: 3214 case SCMD_WRITE_LONG: 3215 case SCMD_READ_CD: 3216 switch (scsa2usbp-> 3217 scsa2usb_lun_inquiry[pkt->pkt_address.a_lun]. 3218 inq_dtype & DTYPE_MASK) { 3219 case DTYPE_DIRECT: 3220 case DTYPE_RODIRECT: 3221 case DTYPE_OPTICAL: 3222 return (scsa2usb_rw_transport( 3223 scsa2usbp, pkt)); 3224 default: 3225 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3226 if (cmd->cmd_bp) { 3227 cmd->cmd_dir = 3228 (cmd->cmd_bp->b_flags & B_READ) ? 3229 CBW_DIR_IN : CBW_DIR_OUT; 3230 cmd->cmd_xfercount = 3231 cmd->cmd_bp->b_bcount; 3232 } 3233 break; 3234 } 3235 break; 3236 3237 case SCMD_REQUEST_SENSE: 3238 cmd->cmd_dir = CBW_DIR_IN; 3239 cmd->cmd_xfercount = pkt->pkt_cdbp[4]; 3240 cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4]; 3241 cmd->cmd_actual_len = CDB_GROUP0; 3242 break; 3243 3244 case SCMD_DOORLOCK: 3245 case SCMD_START_STOP: 3246 case SCMD_TEST_UNIT_READY: 3247 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3248 break; 3249 3250 /* 3251 * Needed by zip protocol to reset the device 3252 */ 3253 case SCMD_SDIAG: 3254 case SCMD_REZERO_UNIT: 3255 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3256 cmd->cmd_actual_len = CDB_GROUP1; 3257 break; 3258 3259 case SCMD_WRITE_VERIFY: 3260 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3261 cmd->cmd_dir = CBW_DIR_OUT; 3262 cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8]; 3263 cmd->cmd_actual_len = CDB_GROUP1; 3264 break; 3265 3266 /* 3267 * Next command does not have a SCSI equivalent as 3268 * it is vendor specific. 3269 * It was listed in the vendor's ATAPI Zip specs. 3270 */ 3271 case SCMD_READ_FORMAT_CAP: 3272 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3273 cmd->cmd_dir = CBW_DIR_IN; 3274 cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8]; 3275 cmd->cmd_actual_len = CDB_GROUP1; 3276 break; 3277 case IOMEGA_CMD_CARTRIDGE_PROTECT: 3278 cmd->cmd_dir = CBW_DIR_OUT; 3279 cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4]; 3280 cmd->cmd_cdb[SCSA2USB_LBA_2] &= ~1; /* Make it even */ 3281 cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1]; 3282 cmd->cmd_actual_len = CDB_GROUP0; 3283 cmd->cmd_xfercount = pkt->pkt_cdbp[4]; /* Length of password */ 3284 break; 3285 3286 /* 3287 * Do not convert SCMD_MODE_SENSE/SELECT to G1 cmds because 3288 * the mode header is different as well. USB devices don't 3289 * support 0x03 & 0x04 mode pages, which are already obsoleted 3290 * by SPC-2 specification. 3291 */ 3292 case SCMD_MODE_SENSE: 3293 case SCMD_MODE_SELECT: 3294 if ((pkt->pkt_cdbp[2] == SD_MODE_SENSE_PAGE3_CODE) || 3295 (pkt->pkt_cdbp[2] == SD_MODE_SENSE_PAGE4_CODE)) { 3296 if (cmd->cmd_bp) { 3297 cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->b_bcount; 3298 } 3299 scsa2usb_force_invalid_request(scsa2usbp, cmd); 3300 return (SCSA2USB_JUST_ACCEPT); 3301 } 3302 /* FALLTHROUGH */ 3303 3304 default: 3305 /* 3306 * an unknown command may be a uscsi cmd which we 3307 * should let go thru without mapping 3308 */ 3309 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3310 if (cmd->cmd_bp) { 3311 cmd->cmd_dir = (cmd->cmd_bp->b_flags & B_READ) ? 3312 CBW_DIR_IN : CBW_DIR_OUT; 3313 cmd->cmd_xfercount = cmd->cmd_bp->b_bcount; 3314 } 3315 3316 break; 3317 } /* end of switch */ 3318 3319 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 3320 "scsa2usb_handle_scsi_cmd_sub_class: opcode = 0x%x count = 0x%lx", 3321 pkt->pkt_cdbp[SCSA2USB_OPCODE], cmd->cmd_xfercount); 3322 3323 cmd->cmd_total_xfercount = cmd->cmd_xfercount; 3324 3325 return (SCSA2USB_TRANSPORT); 3326 } 3327 3328 3329 /* 3330 * scsa2usb_do_tur is performed before READ CAPACITY command is issued. 3331 * It returns media status, 0 for media ready, -1 for media not ready 3332 * or other errors. 3333 */ 3334 static int 3335 scsa2usb_do_tur(scsa2usb_state_t *scsa2usbp, struct scsi_address *ap) 3336 { 3337 struct scsi_pkt *pkt; 3338 scsa2usb_cmd_t *turcmd; 3339 int rval = -1; 3340 3341 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 3342 "scsa2usb_do_tur:"); 3343 3344 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 3345 3346 mutex_exit(&scsa2usbp->scsa2usb_mutex); 3347 if ((pkt = scsi_init_pkt(ap, NULL, NULL, CDB_GROUP0, 1, 3348 PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL)) == NULL) { 3349 mutex_enter(&scsa2usbp->scsa2usb_mutex); 3350 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 3351 scsa2usbp->scsa2usb_log_handle, 3352 "scsa2usb_do_tur: init pkt failed"); 3353 3354 return (rval); 3355 } 3356 3357 RQ_MAKECOM_G0(pkt, FLAG_HEAD | FLAG_NODISCON, 3358 (char)SCMD_TEST_UNIT_READY, 0, 0); 3359 3360 pkt->pkt_comp = NULL; 3361 pkt->pkt_time = PKT_DEFAULT_TIMEOUT; 3362 turcmd = PKT2CMD(pkt); 3363 3364 mutex_enter(&scsa2usbp->scsa2usb_mutex); 3365 scsa2usb_prepare_pkt(scsa2usbp, turcmd->cmd_pkt); 3366 3367 if (scsa2usb_cmd_transport(scsa2usbp, turcmd) != TRAN_ACCEPT) { 3368 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 3369 scsa2usbp->scsa2usb_log_handle, 3370 "scsa2usb_do_tur: cmd transport failed, " 3371 "pkt_reason=0x%x", turcmd->cmd_pkt->pkt_reason); 3372 } else if (*(turcmd->cmd_pkt->pkt_scbp) != STATUS_GOOD) { 3373 /* 3374 * Theoretically, the sense data should be retrieved and 3375 * sense key be checked when check condition happens. If 3376 * the sense key is UNIT ATTENTION, TEST UNIT READY cmd 3377 * needs to be sent again to clear the UNIT ATTENTION and 3378 * another TUR to be sent to get the real media status. 3379 * But the AMI virtual floppy device simply cannot recover 3380 * from UNIT ATTENTION by re-sending a TUR cmd, so it 3381 * doesn't make any difference whether to check sense key 3382 * or not. Just ignore sense key checking here and assume 3383 * the device is NOT READY. 3384 */ 3385 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 3386 scsa2usbp->scsa2usb_log_handle, 3387 "scsa2usb_do_tur: media not ready"); 3388 } else { 3389 rval = 0; 3390 } 3391 3392 mutex_exit(&scsa2usbp->scsa2usb_mutex); 3393 scsi_destroy_pkt(pkt); 3394 mutex_enter(&scsa2usbp->scsa2usb_mutex); 3395 3396 return (rval); 3397 } 3398 3399 3400 /* 3401 * scsa2usb_check_ufi_blacklist_attrs: 3402 * validate "scsa2usb_blacklist_attrs" (see scsa2usb.h) 3403 * if blacklisted attrs match accept the request 3404 * attributes checked are:- 3405 * SCSA2USB_ATTRS_GET_CONF 3406 * SCSA2USB_ATTRS_GET_PERF 3407 * SCSA2USB_ATTRS_GET_START_STOP 3408 */ 3409 static int 3410 scsa2usb_check_ufi_blacklist_attrs(scsa2usb_state_t *scsa2usbp, uchar_t opcode, 3411 scsa2usb_cmd_t *cmd) 3412 { 3413 int rval = SCSA2USB_TRANSPORT; 3414 3415 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 3416 3417 switch (opcode) { 3418 case SCMD_PRIN: 3419 case SCMD_PROUT: 3420 rval = SCSA2USB_JUST_ACCEPT; 3421 break; 3422 case SCMD_MODE_SENSE: 3423 case SCMD_MODE_SELECT: 3424 if (cmd->cmd_bp) { 3425 cmd->cmd_pkt->pkt_resid = cmd->cmd_bp->b_bcount; 3426 } 3427 scsa2usb_force_invalid_request(scsa2usbp, cmd); 3428 rval = SCSA2USB_JUST_ACCEPT; 3429 break; 3430 case SCMD_GET_CONFIGURATION: 3431 if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_GET_CONF)) { 3432 rval = SCSA2USB_JUST_ACCEPT; 3433 } 3434 break; 3435 case SCMD_GET_PERFORMANCE: 3436 if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_GET_PERF)) { 3437 rval = SCSA2USB_JUST_ACCEPT; 3438 } 3439 break; 3440 case SCMD_START_STOP: 3441 /* 3442 * some CB/CBI devices don't have mechanics that spin the 3443 * media up and down. So, it doesn't make much sense 3444 * to issue this cmd to those devices. 3445 */ 3446 if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_START_STOP)) { 3447 rval = SCSA2USB_JUST_ACCEPT; 3448 } 3449 break; 3450 case SCMD_READ_CAPACITY: 3451 /* 3452 * Some devices don't support READ CAPACITY command 3453 * when media is not ready. Need to check media status 3454 * before issuing the cmd to such device. 3455 */ 3456 if (!(scsa2usbp->scsa2usb_attrs & 3457 SCSA2USB_ATTRS_NO_MEDIA_CHECK)) { 3458 struct scsi_pkt *pkt = cmd->cmd_pkt; 3459 3460 ASSERT(scsa2usbp->scsa2usb_cur_pkt == pkt); 3461 scsa2usbp->scsa2usb_cur_pkt = NULL; 3462 3463 if (scsa2usb_do_tur(scsa2usbp, 3464 &pkt->pkt_address) != 0) { 3465 /* media not ready, force cmd invalid */ 3466 if (cmd->cmd_bp) { 3467 cmd->cmd_pkt->pkt_resid = 3468 cmd->cmd_bp->b_bcount; 3469 } 3470 scsa2usb_force_invalid_request(scsa2usbp, cmd); 3471 rval = SCSA2USB_JUST_ACCEPT; 3472 } 3473 3474 scsa2usbp->scsa2usb_cur_pkt = pkt; 3475 } 3476 break; 3477 default: 3478 break; 3479 } 3480 3481 return (rval); 3482 } 3483 3484 3485 /* 3486 * scsa2usb_handle_ufi_subclass_cmd: 3487 * prepare a UFI cmd 3488 * returns SCSA2USB_TRANSPORT, SCSA2USB_REJECT 3489 */ 3490 int 3491 scsa2usb_handle_ufi_subclass_cmd(scsa2usb_state_t *scsa2usbp, 3492 scsa2usb_cmd_t *cmd, struct scsi_pkt *pkt) 3493 { 3494 uchar_t opcode = pkt->pkt_cdbp[0]; 3495 3496 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 3497 "scsa2usb_handle_ufi_subclass_cmd: cmd = 0x%p pkt = 0x%p", 3498 (void *)cmd, (void *)pkt); 3499 3500 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 3501 3502 bzero(&cmd->cmd_cdb, SCSI_CDB_SIZE); 3503 cmd->cmd_cdb[SCSA2USB_OPCODE] = opcode; /* Set the opcode */ 3504 cmd->cmd_cdb[SCSA2USB_LUN] = pkt->pkt_cdbp[1]; 3505 3506 /* 3507 * decode and convert the packet if necessary 3508 * for most cmds, we can bcopy the cdb 3509 */ 3510 switch (opcode) { 3511 case SCMD_FORMAT: 3512 /* if parameter list is specified */ 3513 if (pkt->pkt_cdbp[1] & 0x10) { 3514 cmd->cmd_xfercount = 3515 (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8]; 3516 cmd->cmd_dir = USB_EP_DIR_OUT; 3517 cmd->cmd_actual_len = CDB_GROUP5; 3518 } 3519 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3520 break; 3521 case SCMD_INQUIRY: 3522 cmd->cmd_dir = USB_EP_DIR_IN; 3523 cmd->cmd_actual_len = CDB_GROUP0; 3524 cmd->cmd_cdb[SCSA2USB_LBA_0] = pkt->pkt_cdbp[2]; 3525 cmd->cmd_cdb[SCSA2USB_LBA_2] = cmd->cmd_xfercount = 3526 min(SCSA2USB_MAX_INQ_LEN, 3527 cmd->cmd_bp ? cmd->cmd_bp->b_bcount : 0); 3528 break; 3529 case SCMD_READ_CAPACITY: 3530 cmd->cmd_dir = USB_EP_DIR_IN; 3531 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3532 cmd->cmd_xfercount = sizeof (scsa2usb_read_cap_t); 3533 break; 3534 case SCMD_REQUEST_SENSE: 3535 cmd->cmd_dir = USB_EP_DIR_IN; 3536 cmd->cmd_xfercount = pkt->pkt_cdbp[4]; 3537 cmd->cmd_cdb[SCSA2USB_LBA_2] = pkt->pkt_cdbp[4]; 3538 cmd->cmd_actual_len = CDB_GROUP0; 3539 break; 3540 3541 /* 3542 * do not convert SCMD_MODE_SENSE/SELECT because the 3543 * mode header is different as well 3544 */ 3545 3546 /* 3547 * see usb_bulkonly.c for comments on the next set of commands 3548 */ 3549 case SCMD_READ: 3550 case SCMD_WRITE: 3551 case SCMD_READ_G1: 3552 case SCMD_WRITE_G1: 3553 case SCMD_READ_G5: 3554 case SCMD_WRITE_G5: 3555 case SCMD_READ_LONG: 3556 case SCMD_WRITE_LONG: 3557 case SCMD_READ_CD: 3558 3559 return (scsa2usb_rw_transport(scsa2usbp, pkt)); 3560 3561 case SCMD_TEST_UNIT_READY: 3562 /* 3563 * Some CB/CBI devices may not support TUR. 3564 */ 3565 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3566 break; 3567 case SCMD_READ_FORMAT_CAP: 3568 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3569 cmd->cmd_dir = USB_EP_DIR_IN; 3570 cmd->cmd_actual_len = CDB_GROUP1; 3571 cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8]; 3572 break; 3573 case SCMD_WRITE_VERIFY: 3574 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3575 cmd->cmd_dir = USB_EP_DIR_OUT; 3576 cmd->cmd_actual_len = CDB_GROUP1; 3577 cmd->cmd_xfercount = (pkt->pkt_cdbp[7] << 8) | pkt->pkt_cdbp[8]; 3578 break; 3579 case SCMD_START_STOP: 3580 /* A larger timeout is needed for 'flaky' CD-RW devices */ 3581 if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_BIG_TIMEOUT)) { 3582 cmd->cmd_timeout = max(cmd->cmd_timeout, 3583 20 * SCSA2USB_BULK_PIPE_TIMEOUT); 3584 } 3585 /* FALLTHRU */ 3586 default: 3587 /* 3588 * all other commands don't need special mapping 3589 */ 3590 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3591 if (cmd->cmd_bp) { 3592 cmd->cmd_dir = (cmd->cmd_bp->b_flags & B_READ) ? 3593 CBW_DIR_IN : CBW_DIR_OUT; 3594 cmd->cmd_xfercount = cmd->cmd_bp->b_bcount; 3595 } 3596 break; 3597 3598 } /* end of switch */ 3599 3600 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 3601 "scsa2usb_handle_ufi_subclass_cmd: opcode = 0x%x count = 0x%lx", 3602 opcode, cmd->cmd_xfercount); 3603 3604 cmd->cmd_total_xfercount = cmd->cmd_xfercount; 3605 3606 return (SCSA2USB_TRANSPORT); 3607 } 3608 3609 3610 /* 3611 * scsa2usb_rw_transport: 3612 * Handle splitting READ and WRITE requests to the 3613 * device to a size that the host controller allows. 3614 * 3615 * returns TRAN_* values and not USB_SUCCESS/FAILURE 3616 * 3617 * To support CD-R/CD-RW/DVD media, we need to support a 3618 * variety of block sizes for the different types of CD 3619 * data (audio, data, video, CD-XA, yellowbook, redbook etc.) 3620 * 3621 * Some of the block sizes used are:- 512, 1k, 2k, 2056, 2336 3622 * 2340, 2352, 2368, 2448, 2646, 2647 etc. 3623 * 3624 * NOTE: the driver could be entertaining a SCSI CDB that uses 3625 * any of the above listed block sizes at a given time, and a 3626 * totally different block size at any other given time for a 3627 * different CDB. 3628 * 3629 * We need to compute block size every time and figure out 3630 * matching LBA and LEN accordingly. 3631 * 3632 * Also UHCI has a limitation that it can only xfer 32k at a 3633 * given time. So, with "odd" sized blocks and a limitation of 3634 * how much we can xfer per shot, we need to compute xfer_count 3635 * as well each time. 3636 * 3637 * The same computation is also done in the function 3638 * scsa2usb_setup_next_xfer(). To save computing block_size in 3639 * this function, I am saving block_size in "cmd" now. 3640 */ 3641 int 3642 scsa2usb_rw_transport(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt) 3643 { 3644 scsa2usb_cmd_t *cmd = PKT2CMD(pkt); 3645 int lba, dir, opcode; 3646 struct buf *bp = cmd->cmd_bp; 3647 size_t len, xfer_count; 3648 size_t blk_size; /* calculate the block size to be used */ 3649 int sz; 3650 3651 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 3652 "scsa2usb_rw_transport:"); 3653 3654 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 3655 3656 opcode = pkt->pkt_cdbp[0]; 3657 blk_size = scsa2usbp->scsa2usb_lbasize[pkt->pkt_address.a_lun]; 3658 /* set to default */ 3659 3660 switch (opcode) { 3661 case SCMD_READ: 3662 /* 3663 * Note that READ/WRITE(6) are not supported by the drive. 3664 * convert it into a 10 byte read/write. 3665 */ 3666 lba = SCSA2USB_LBA_6BYTE(pkt); 3667 len = SCSA2USB_LEN_6BYTE(pkt); 3668 opcode = SCMD_READ_G1; /* Overwrite it w/ byte 10 cmd val */ 3669 dir = USB_EP_DIR_IN; 3670 break; 3671 case SCMD_WRITE: 3672 lba = SCSA2USB_LBA_6BYTE(pkt); 3673 len = SCSA2USB_LEN_6BYTE(pkt); 3674 opcode = SCMD_WRITE_G1; /* Overwrite it w/ byte 10 cmd val */ 3675 dir = USB_EP_DIR_OUT; 3676 break; 3677 case SCMD_READ_G1: 3678 case SCMD_READ_LONG: 3679 lba = SCSA2USB_LBA_10BYTE(pkt); 3680 len = SCSA2USB_LEN_10BYTE(pkt); 3681 dir = USB_EP_DIR_IN; 3682 break; 3683 case SCMD_WRITE_G1: 3684 case SCMD_WRITE_LONG: 3685 lba = SCSA2USB_LBA_10BYTE(pkt); 3686 len = SCSA2USB_LEN_10BYTE(pkt); 3687 dir = USB_EP_DIR_OUT; 3688 if (len) { 3689 sz = SCSA2USB_CDRW_BLKSZ(bp ? bp->b_bcount : 0, len); 3690 if (SCSA2USB_VALID_CDRW_BLKSZ(sz)) { 3691 blk_size = sz; /* change it accordingly */ 3692 } 3693 } 3694 break; 3695 case SCMD_READ_CD: 3696 lba = SCSA2USB_LBA_10BYTE(pkt); 3697 len = SCSA2USB_LEN_READ_CD(pkt); 3698 dir = USB_EP_DIR_IN; 3699 3700 /* Figure out the block size */ 3701 blk_size = scsa2usb_read_cd_blk_size(pkt->pkt_cdbp[1] >> 2); 3702 break; 3703 case SCMD_READ_G5: 3704 lba = SCSA2USB_LBA_12BYTE(pkt); 3705 len = SCSA2USB_LEN_12BYTE(pkt); 3706 dir = USB_EP_DIR_IN; 3707 break; 3708 case SCMD_WRITE_G5: 3709 lba = SCSA2USB_LBA_12BYTE(pkt); 3710 len = SCSA2USB_LEN_12BYTE(pkt); 3711 dir = USB_EP_DIR_OUT; 3712 break; 3713 } 3714 3715 cmd->cmd_total_xfercount = xfer_count = len * blk_size; 3716 3717 /* reduce xfer count if necessary */ 3718 if (blk_size && 3719 (xfer_count > scsa2usbp->scsa2usb_max_bulk_xfer_size)) { 3720 /* 3721 * For CD-RW devices reduce the xfer count based 3722 * on the block size used by these devices. The 3723 * block size could change for READ_CD and WRITE 3724 * opcodes. 3725 * 3726 * Also as UHCI allows a max xfer of 32k at a time; 3727 * compute the xfer_count based on the new block_size. 3728 * 3729 * The len part of the cdb changes as a result of that. 3730 */ 3731 if (SCSA2USB_VALID_CDRW_BLKSZ(blk_size)) { 3732 xfer_count = ((scsa2usbp->scsa2usb_max_bulk_xfer_size/ 3733 blk_size) * blk_size); 3734 len = xfer_count/blk_size; 3735 xfer_count = blk_size * len; 3736 } else { 3737 xfer_count = scsa2usbp->scsa2usb_max_bulk_xfer_size; 3738 len = xfer_count/blk_size; 3739 } 3740 } 3741 3742 cmd->cmd_xfercount = xfer_count; 3743 cmd->cmd_dir = (uchar_t)dir; 3744 cmd->cmd_blksize = blk_size; 3745 3746 /* 3747 * Having figure out the 'partial' xfer len based on he 3748 * block size; fill it in to the cmd->cmd_cdb 3749 */ 3750 cmd->cmd_cdb[SCSA2USB_OPCODE] = (uchar_t)opcode; 3751 switch (opcode) { 3752 case SCMD_READ_CD: 3753 bcopy(pkt->pkt_cdbp, &cmd->cmd_cdb, cmd->cmd_cdblen); 3754 scsa2usb_fill_up_ReadCD_cdb_len(cmd, len, CDB_GROUP5); 3755 break; 3756 case SCMD_WRITE_G5: 3757 case SCMD_READ_G5: 3758 scsa2usb_fill_up_12byte_cdb_len(cmd, len, CDB_GROUP5); 3759 break; 3760 default: 3761 scsa2usb_fill_up_cdb_len(cmd, len); 3762 cmd->cmd_actual_len = CDB_GROUP1; 3763 break; 3764 } 3765 3766 scsa2usb_fill_up_cdb_lba(cmd, lba); 3767 3768 USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 3769 "bcount=0x%lx lba=0x%x len=0x%lx xfercount=0x%lx total=0x%lx", 3770 bp ? bp->b_bcount : 0, lba, len, cmd->cmd_xfercount, 3771 cmd->cmd_total_xfercount); 3772 3773 /* Set the timeout value as per command request */ 3774 if ((opcode == SCMD_WRITE_G1) && SCSA2USB_VALID_CDRW_BLKSZ(blk_size)) { 3775 /* 3776 * We increase the time as CD-RW writes have two things 3777 * to do. After writing out the data to the media, a 3778 * TOC needs to be filled up at the beginning of the media 3779 * This is when the write gets "finalized". 3780 * Hence the actual write could take longer than the 3781 * value specified in cmd->cmd_timeout. 3782 */ 3783 cmd->cmd_timeout *= 4; 3784 3785 USB_DPRINTF_L4(DPRINT_MASK_SCSA, 3786 scsa2usbp->scsa2usb_log_handle, 3787 "new timeout value = 0x%x", cmd->cmd_timeout); 3788 } 3789 3790 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 3791 "lba 0x%x len 0x%lx xfercount 0x%lx total 0x%lx", 3792 lba, len, cmd->cmd_xfercount, cmd->cmd_total_xfercount); 3793 3794 return (SCSA2USB_TRANSPORT); 3795 } 3796 3797 3798 /* 3799 * scsa2usb_setup_next_xfer: 3800 * For READs and WRITEs we split up the transfer in terms of 3801 * HCD understood units. This function handles the split transfers. 3802 * 3803 * See comments in the previous function scsa2usb_rw_transport 3804 * 3805 * The lba computation was being done based on scsa2usb_max_bulk_xfer_size 3806 * earlier. With CD-RW devices, the xfer_count and the block_size may 3807 * no longer be a multiple of scsa2usb_max_bulk_xfer_size. So compute 3808 * xfer_count all over again. Adjust lba, based on the previous requests' 3809 * len. Find out the len and add it to cmd->cmd_lba to get the new lba 3810 */ 3811 void 3812 scsa2usb_setup_next_xfer(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd) 3813 { 3814 int xfer_len = min(scsa2usbp->scsa2usb_max_bulk_xfer_size, 3815 cmd->cmd_total_xfercount); 3816 int cdb_len; 3817 size_t blk_size; 3818 3819 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 3820 3821 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 3822 "scsa2usb_setup_next_xfer: opcode = 0x%x lba = 0x%x " 3823 "total count = 0x%lx", cmd->cmd_cdb[SCSA2USB_OPCODE], 3824 cmd->cmd_lba, cmd->cmd_total_xfercount); 3825 3826 ASSERT(cmd->cmd_total_xfercount > 0); 3827 cmd->cmd_xfercount = xfer_len; 3828 blk_size = scsa2usbp->scsa2usb_lbasize[ 3829 cmd->cmd_pkt->pkt_address.a_lun]; 3830 3831 /* 3832 * For CD-RW devices reduce the xfer count based on the 3833 * block_size used by these devices. See changes below 3834 * where xfer_count is being adjusted. 3835 * 3836 * Also adjust len/lba based on the block_size and xfer_count. 3837 * NOTE: Always calculate lba first, as it based on previous 3838 * commands' values. 3839 */ 3840 switch (cmd->cmd_cdb[SCSA2USB_OPCODE]) { 3841 case SCMD_READ_CD: 3842 /* calculate lba = current_lba + len_of_prev_cmd */ 3843 cmd->cmd_lba += (cmd->cmd_cdb[6] << 16) + 3844 (cmd->cmd_cdb[7] << 8) + cmd->cmd_cdb[8]; 3845 cdb_len = xfer_len/cmd->cmd_blksize; 3846 cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_2] = (uchar_t)cdb_len; 3847 /* re-adjust xfer count */ 3848 cmd->cmd_xfercount = cdb_len * cmd->cmd_blksize; 3849 break; 3850 case SCMD_WRITE_G5: 3851 case SCMD_READ_G5: 3852 /* calculate lba = current_lba + len_of_prev_cmd */ 3853 cmd->cmd_lba += (cmd->cmd_cdb[6] << 24) + 3854 (cmd->cmd_cdb[7] << 16) + (cmd->cmd_cdb[8] << 8) + 3855 cmd->cmd_cdb[9]; 3856 if (blk_size) { 3857 xfer_len /= blk_size; 3858 } 3859 scsa2usb_fill_up_12byte_cdb_len(cmd, xfer_len, CDB_GROUP5); 3860 break; 3861 case SCMD_WRITE_G1: 3862 case SCMD_WRITE_LONG: 3863 /* calculate lba = current_lba + len_of_prev_cmd */ 3864 cmd->cmd_lba += (cmd->cmd_cdb[7] << 8) + cmd->cmd_cdb[8]; 3865 if (SCSA2USB_VALID_CDRW_BLKSZ(cmd->cmd_blksize)) { 3866 blk_size = cmd->cmd_blksize; 3867 } 3868 cdb_len = xfer_len/blk_size; 3869 scsa2usb_fill_up_cdb_len(cmd, cdb_len); 3870 /* re-adjust xfer count */ 3871 cmd->cmd_xfercount = cdb_len * blk_size; 3872 break; 3873 default: 3874 if (blk_size) { 3875 xfer_len /= blk_size; 3876 } 3877 scsa2usb_fill_up_cdb_len(cmd, xfer_len); 3878 cmd->cmd_lba += scsa2usbp->scsa2usb_max_bulk_xfer_size/blk_size; 3879 } 3880 3881 /* fill in the lba */ 3882 scsa2usb_fill_up_cdb_lba(cmd, cmd->cmd_lba); 3883 3884 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 3885 "scsa2usb_setup_next_xfer:\n\tlba = 0x%x xfer_len = 0x%x " 3886 "xfercount = 0x%lx total = 0x%lx", cmd->cmd_lba, xfer_len, 3887 cmd->cmd_xfercount, cmd->cmd_total_xfercount); 3888 } 3889 3890 3891 /* 3892 * take one request from the lun's waitQ and transport it 3893 */ 3894 static void 3895 scsa2usb_transport_request(scsa2usb_state_t *scsa2usbp, uint_t lun) 3896 { 3897 int rval; 3898 struct scsi_pkt *pkt; 3899 struct scsa2usb_cmd *cmd, *arqcmd; 3900 3901 if ((cmd = (scsa2usb_cmd_t *) 3902 usba_rm_first_pvt_from_list( 3903 &scsa2usbp->scsa2usb_waitQ[lun])) == NULL) { 3904 3905 return; 3906 } 3907 pkt = cmd->cmd_pkt; 3908 3909 /* 3910 * if device has been disconnected, just complete it 3911 */ 3912 if (scsa2usbp->scsa2usb_dev_state == USB_DEV_DISCONNECTED) { 3913 USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 3914 "device not accessible"); 3915 pkt->pkt_reason = CMD_DEV_GONE; 3916 SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp); 3917 scsa2usb_pkt_completion(scsa2usbp, pkt); 3918 3919 return; 3920 } 3921 3922 USB_DPRINTF_L4(DPRINT_MASK_SCSA, 3923 scsa2usbp->scsa2usb_log_handle, 3924 "scsa2usb_transport_request: cmd=0x%p bp=0x%p addr=0x%p", 3925 (void *)cmd, (void *)cmd->cmd_bp, 3926 (void *)(cmd->cmd_bp ? cmd->cmd_bp->b_un.b_addr : NULL)); 3927 3928 rval = scsa2usb_cmd_transport(scsa2usbp, cmd); 3929 3930 USB_DPRINTF_L3(DPRINT_MASK_SCSA, 3931 scsa2usbp->scsa2usb_log_handle, 3932 "scsa2usb_transport_request: transport rval = %d", 3933 rval); 3934 3935 if (scsa2usbp->scsa2usb_cur_pkt == NULL) { 3936 3937 return; 3938 } 3939 3940 ASSERT(pkt == scsa2usbp->scsa2usb_cur_pkt); 3941 3942 if (ddi_in_panic()) { 3943 pkt->pkt_reason = CMD_CMPLT; 3944 scsa2usb_pkt_completion(scsa2usbp, pkt); 3945 3946 return; 3947 } 3948 3949 /* 3950 * start an auto-request sense iff 3951 * there was a check condition, we have enough 3952 * space in the status block, and we have not 3953 * faked an auto request sense 3954 */ 3955 if ((*(pkt->pkt_scbp) == STATUS_CHECK) && 3956 (cmd->cmd_scblen >= sizeof (struct scsi_arq_status)) && 3957 ((pkt->pkt_state & STATE_ARQ_DONE) == 0) && 3958 (scsa2usb_create_arq_pkt(scsa2usbp, 3959 &pkt->pkt_address) == USB_SUCCESS)) { 3960 arqcmd = scsa2usbp->scsa2usb_arq_cmd; 3961 3962 /* 3963 * copy the timeout from the 3964 * original packet 3965 * for lack of a better value 3966 */ 3967 arqcmd->cmd_pkt->pkt_time = pkt->pkt_time; 3968 scsa2usb_prepare_pkt(scsa2usbp, 3969 arqcmd->cmd_pkt); 3970 3971 scsa2usbp->scsa2usb_cur_pkt = NULL; 3972 if (scsa2usb_cmd_transport( 3973 scsa2usbp, arqcmd) == TRAN_ACCEPT) { 3974 3975 /* finish w/ this packet */ 3976 scsa2usb_complete_arq_pkt( 3977 scsa2usbp, arqcmd->cmd_pkt, cmd, 3978 scsa2usbp->scsa2usb_arq_bp); 3979 3980 /* 3981 * we have valid request sense 3982 * data so clear the pkt_reason 3983 */ 3984 pkt->pkt_reason = CMD_CMPLT; 3985 } 3986 scsa2usbp->scsa2usb_cur_pkt = pkt; 3987 scsa2usb_delete_arq_pkt(scsa2usbp); 3988 } 3989 3990 if ((rval != TRAN_ACCEPT) && 3991 (pkt->pkt_reason == CMD_CMPLT)) { 3992 pkt->pkt_reason = CMD_TRAN_ERR; 3993 } 3994 3995 SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp); 3996 scsa2usb_pkt_completion(scsa2usbp, pkt); 3997 3998 ASSERT(scsa2usbp->scsa2usb_cur_pkt == NULL); 3999 } 4000 4001 4002 /* 4003 * scsa2usb_work_thread: 4004 * The taskq thread that kicks off the transport (BO and CB/CBI) 4005 */ 4006 static void 4007 scsa2usb_work_thread(void *arg) 4008 { 4009 scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)arg; 4010 uint_t lun; 4011 uint_t count; 4012 4013 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4014 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4015 "scsa2usb_work_thread start: thread_id=0x%p", 4016 (void *)scsa2usbp->scsa2usb_work_thread_id); 4017 4018 ASSERT(scsa2usbp->scsa2usb_work_thread_id == (kthread_t *)1); 4019 scsa2usbp->scsa2usb_work_thread_id = curthread; 4020 4021 /* exclude ugen accesses */ 4022 while (scsa2usbp->scsa2usb_transport_busy) { 4023 cv_wait(&scsa2usbp->scsa2usb_transport_busy_cv, 4024 &scsa2usbp->scsa2usb_mutex); 4025 } 4026 ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0); 4027 scsa2usbp->scsa2usb_transport_busy++; 4028 scsa2usbp->scsa2usb_busy_thread = curthread; 4029 4030 scsa2usb_raise_power(scsa2usbp); 4031 4032 /* reopen the pipes if necessary */ 4033 (void) scsa2usb_open_usb_pipes(scsa2usbp); 4034 4035 for (;;) { 4036 ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0); 4037 for (lun = 0; lun < scsa2usbp->scsa2usb_n_luns; lun++) { 4038 scsa2usb_transport_request(scsa2usbp, lun); 4039 } 4040 count = 0; 4041 for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) { 4042 count += usba_list_entry_count( 4043 &scsa2usbp->scsa2usb_waitQ[lun]); 4044 } 4045 4046 if (count == 0) { 4047 4048 break; 4049 } 4050 } 4051 4052 scsa2usbp->scsa2usb_work_thread_id = 0; 4053 4054 ASSERT(scsa2usbp->scsa2usb_ugen_open_count == 0); 4055 4056 scsa2usbp->scsa2usb_transport_busy--; 4057 scsa2usbp->scsa2usb_busy_thread = NULL; 4058 cv_signal(&scsa2usbp->scsa2usb_transport_busy_cv); 4059 4060 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4061 "scsa2usb_work_thread: exit"); 4062 4063 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4064 4065 scsa2usb_pm_idle_component(scsa2usbp); 4066 } 4067 4068 4069 /* 4070 * scsa2usb_flush_waitQ: 4071 * empties the entire waitQ with errors asap. 4072 * 4073 * It is called from scsa2usb_scsi_reset and scsa2usb_panic_callb. 4074 * If the device is reset; we should empty the waitQ right away. 4075 * If the system has paniced; we should empty the waitQ right away. 4076 * 4077 * CPR suspend will only succeed if device is idle. No need to call 4078 * this function for CPR suspend case. 4079 */ 4080 static void 4081 scsa2usb_flush_waitQ(scsa2usb_state_t *scsa2usbp, uint_t lun, 4082 uchar_t error) 4083 { 4084 struct scsi_pkt *pkt; 4085 struct scsa2usb_cmd *cmd; 4086 usba_list_entry_t head; 4087 4088 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4089 4090 usba_move_list(&scsa2usbp->scsa2usb_waitQ[lun], &head, 4091 scsa2usbp->scsa2usb_dev_data->dev_iblock_cookie); 4092 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4093 4094 while ((cmd = (scsa2usb_cmd_t *)usba_rm_first_pvt_from_list(&head)) != 4095 NULL) { 4096 pkt = cmd->cmd_pkt; 4097 pkt->pkt_reason = error; /* set error */ 4098 4099 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4100 scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_DO_COMP; 4101 scsa2usb_pkt_completion(scsa2usbp, pkt); 4102 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4103 } /* end of while */ 4104 } 4105 4106 4107 /* 4108 * scsa2usb_do_inquiry is performed before INIT CHILD and we have 4109 * to fake a few things normally done by SCSA 4110 */ 4111 static void 4112 scsa2usb_do_inquiry(scsa2usb_state_t *scsa2usbp, uint_t target, uint_t lun) 4113 { 4114 struct buf *bp; 4115 struct scsi_pkt *pkt; 4116 struct scsi_address ap; 4117 int len = SCSA2USB_MAX_INQ_LEN; 4118 4119 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4120 "scsa2usb_do_inquiry: %d bytes", len); 4121 4122 /* is it inquiry-challenged? */ 4123 if (!(scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_INQUIRY)) { 4124 (void) scsa2usb_fake_inquiry(scsa2usbp, NULL, lun); 4125 4126 return; 4127 } 4128 4129 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 4130 4131 bzero(&ap, sizeof (struct scsi_address)); 4132 ap.a_hba_tran = scsa2usbp->scsa2usb_tran; 4133 ap.a_target = target; 4134 ap.a_lun = lun; 4135 4136 /* limit inquiry to 36 bytes */ 4137 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4138 if ((bp = scsi_alloc_consistent_buf(&ap, (struct buf *)NULL, 4139 len, B_READ, SLEEP_FUNC, NULL)) == NULL) { 4140 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4141 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 4142 scsa2usbp->scsa2usb_log_handle, 4143 "scsa2usb_do_inquiry: failed"); 4144 4145 return; 4146 } 4147 4148 pkt = scsi_init_pkt(&ap, NULL, bp, CDB_GROUP0, 1, 4149 PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL); 4150 4151 RQ_MAKECOM_G0(pkt, FLAG_NOINTR, (char)SCMD_INQUIRY, 0, len); 4152 4153 pkt->pkt_comp = NULL; 4154 pkt->pkt_time = 5; 4155 bzero(bp->b_un.b_addr, len); 4156 4157 USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4158 "scsa2usb_do_inquiry:INQUIRY"); 4159 4160 (void) scsi_transport(pkt); 4161 4162 if (pkt->pkt_reason) { 4163 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 4164 scsa2usbp->scsa2usb_log_handle, 4165 "INQUIRY failed, cannot determine device type, " 4166 "pkt_reason=0x%x", pkt->pkt_reason); 4167 4168 /* not much hope for other cmds, reduce */ 4169 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4170 scsa2usbp->scsa2usb_attrs &= 4171 ~SCSA2USB_ATTRS_REDUCED_CMD; 4172 (void) scsa2usb_fake_inquiry(scsa2usbp, NULL, lun); 4173 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4174 } 4175 4176 scsi_destroy_pkt(pkt); 4177 scsi_free_consistent_buf(bp); 4178 4179 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4180 } 4181 4182 4183 /* 4184 * scsa2usb_fake_inquiry: 4185 * build an inquiry for a given device that doesnt like inquiry 4186 * commands. 4187 */ 4188 static int 4189 scsa2usb_fake_inquiry(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd, 4190 uint_t lun) 4191 { 4192 usb_client_dev_data_t *dev_data = scsa2usbp->scsa2usb_dev_data; 4193 struct scsi_inquiry *inqp; 4194 int len; 4195 4196 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4197 "scsa2usb_fake_inquiry:"); 4198 4199 if (cmd) { 4200 inqp = (struct scsi_inquiry *)cmd->cmd_bp->b_un.b_addr; 4201 } else { 4202 inqp = &scsa2usbp->scsa2usb_lun_inquiry[lun]; 4203 } 4204 bzero(inqp, sizeof (struct scsi_inquiry)); 4205 for (len = 0; len < sizeof (inqp->inq_vid); len++) { 4206 *(inqp->inq_vid + len) = ' '; 4207 } 4208 4209 for (len = 0; len < sizeof (inqp->inq_pid); len++) { 4210 *(inqp->inq_pid + len) = ' '; 4211 } 4212 4213 inqp->inq_dtype = DTYPE_DIRECT; 4214 inqp->inq_rmb = 1; 4215 inqp->inq_ansi = 2; 4216 inqp->inq_rdf = RDF_SCSI2; 4217 inqp->inq_len = sizeof (struct scsi_inquiry)-4; 4218 4219 /* Fill in the Vendor id/Product id strings */ 4220 if (dev_data->dev_mfg) { 4221 if ((len = strlen(dev_data->dev_mfg)) > 4222 sizeof (inqp->inq_vid)) { 4223 len = sizeof (inqp->inq_vid); 4224 } 4225 bcopy(dev_data->dev_mfg, inqp->inq_vid, len); 4226 } 4227 4228 if (dev_data->dev_product) { 4229 if ((len = strlen(dev_data->dev_product)) > 4230 sizeof (inqp->inq_pid)) { 4231 len = sizeof (inqp->inq_pid); 4232 } 4233 bcopy(dev_data->dev_product, inqp->inq_pid, len); 4234 } 4235 4236 /* Set the Revision to the Device */ 4237 inqp->inq_revision[0] = 0x30 + 4238 ((dev_data->dev_descr->bcdDevice>>12) & 0xF); 4239 inqp->inq_revision[1] = 0x30 + 4240 ((dev_data->dev_descr->bcdDevice>>8) & 0xF); 4241 inqp->inq_revision[2] = 0x30 + 4242 ((dev_data->dev_descr->bcdDevice>>4) & 0xF); 4243 inqp->inq_revision[3] = 0x30 + 4244 ((dev_data->dev_descr->bcdDevice) & 0xF); 4245 4246 /* Copy inquiry data in to soft state */ 4247 bcopy(inqp, &scsa2usbp->scsa2usb_lun_inquiry[lun], 4248 sizeof (struct scsi_inquiry)); 4249 4250 return (sizeof (struct scsi_inquiry)); 4251 } 4252 4253 4254 /* 4255 * scsa2usb_create_arq_pkt: 4256 * Create and ARQ packet to get request sense data 4257 */ 4258 static int 4259 scsa2usb_create_arq_pkt(scsa2usb_state_t *scsa2usbp, struct scsi_address *ap) 4260 { 4261 struct buf *bp; 4262 scsa2usb_cmd_t *arq_cmd; 4263 4264 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4265 "scsa2usb_create_arq_pkt: scsa2usbp: %p, ap: %p", 4266 (void *)scsa2usbp, (void *)ap); 4267 4268 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 4269 4270 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4271 if ((bp = scsi_alloc_consistent_buf(ap, (struct buf *)NULL, 4272 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL)) == NULL) { 4273 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4274 4275 return (USB_FAILURE); 4276 } 4277 4278 arq_cmd = PKT2CMD(scsi_init_pkt(ap, NULL, bp, CDB_GROUP0, 1, 4279 PKT_PRIV_LEN, PKT_CONSISTENT, SLEEP_FUNC, NULL)); 4280 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4281 4282 RQ_MAKECOM_G0(arq_cmd->cmd_pkt, 4283 FLAG_SENSING | FLAG_HEAD | FLAG_NODISCON, 4284 (char)SCMD_REQUEST_SENSE, 0, (char)SENSE_LENGTH); 4285 4286 arq_cmd->cmd_pkt->pkt_ha_private = arq_cmd; 4287 scsa2usbp->scsa2usb_arq_cmd = arq_cmd; 4288 scsa2usbp->scsa2usb_arq_bp = bp; 4289 arq_cmd->cmd_pkt->pkt_comp = NULL; 4290 bzero(bp->b_un.b_addr, SENSE_LENGTH); 4291 4292 return (USB_SUCCESS); 4293 } 4294 4295 4296 /* 4297 * scsa2usb_delete_arq_pkt: 4298 * Destroy the ARQ packet 4299 */ 4300 static void 4301 scsa2usb_delete_arq_pkt(scsa2usb_state_t *scsa2usbp) 4302 { 4303 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4304 "scsa2usb_delete_arq_pkt: cmd: 0x%p", 4305 (void *)scsa2usbp->scsa2usb_arq_cmd); 4306 4307 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 4308 4309 if (scsa2usbp->scsa2usb_arq_cmd != NULL) { 4310 scsi_destroy_pkt(scsa2usbp->scsa2usb_arq_cmd->cmd_pkt); 4311 scsi_free_consistent_buf(scsa2usbp->scsa2usb_arq_bp); 4312 } 4313 scsa2usbp->scsa2usb_arq_cmd = NULL; 4314 scsa2usbp->scsa2usb_arq_bp = NULL; 4315 } 4316 4317 4318 /* 4319 * scsa2usb_complete_arq_pkt: 4320 * finish processing the arq packet 4321 */ 4322 static void 4323 scsa2usb_complete_arq_pkt(scsa2usb_state_t *scsa2usbp, 4324 struct scsi_pkt *pkt, scsa2usb_cmd_t *ssp, struct buf *bp) 4325 { 4326 scsa2usb_cmd_t *sp = pkt->pkt_ha_private; 4327 struct scsi_arq_status *arqp; 4328 4329 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 4330 4331 arqp = (struct scsi_arq_status *)(ssp->cmd_pkt->pkt_scbp); 4332 arqp->sts_rqpkt_status = *((struct scsi_status *) 4333 (sp->cmd_pkt->pkt_scbp)); 4334 arqp->sts_rqpkt_reason = CMD_CMPLT; 4335 arqp->sts_rqpkt_state |= STATE_XFERRED_DATA; 4336 arqp->sts_rqpkt_statistics = arqp->sts_rqpkt_resid = 0; 4337 4338 /* is this meaningful sense data */ 4339 if (*(bp->b_un.b_addr) != 0) { 4340 bcopy(bp->b_un.b_addr, &arqp->sts_sensedata, SENSE_LENGTH); 4341 ssp->cmd_pkt->pkt_state |= STATE_ARQ_DONE; 4342 } 4343 4344 /* we will not sense start cmd until we receive a NOT READY */ 4345 if (arqp->sts_sensedata.es_key == KEY_NOT_READY) { 4346 scsa2usbp->scsa2usb_rcvd_not_ready = B_TRUE; 4347 } 4348 } 4349 4350 4351 /* 4352 * Miscellaneous functions for any command/transport 4353 */ 4354 /* 4355 * scsa2usb_open_usb_pipes: 4356 * set up a pipe policy 4357 * open usb bulk pipes (BO and CB/CBI) 4358 * open usb interrupt pipe (CBI) 4359 */ 4360 static int 4361 scsa2usb_open_usb_pipes(scsa2usb_state_t *scsa2usbp) 4362 { 4363 int rval; 4364 usb_pipe_policy_t policy; /* bulk pipe policy */ 4365 size_t sz; 4366 4367 ASSERT(scsa2usbp); 4368 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 4369 4370 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4371 "scsa2usb_open_usb_pipes: dip = 0x%p flag = 0x%x", 4372 (void *)scsa2usbp->scsa2usb_dip, scsa2usbp->scsa2usb_flags); 4373 4374 if (!(scsa2usbp->scsa2usb_flags & SCSA2USB_FLAGS_PIPES_OPENED)) { 4375 4376 /* 4377 * one pipe policy for all bulk pipes 4378 */ 4379 bzero(&policy, sizeof (usb_pipe_policy_t)); 4380 /* at least 2, for the normal and exceptional callbacks */ 4381 policy.pp_max_async_reqs = 1; 4382 4383 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4384 "scsa2usb_open_usb_pipes: opening bulk pipes"); 4385 4386 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4387 4388 /* Open the USB bulk-in pipe */ 4389 if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip, 4390 &scsa2usbp->scsa2usb_bulkin_ept, &policy, USB_FLAGS_SLEEP, 4391 &scsa2usbp->scsa2usb_bulkin_pipe)) != USB_SUCCESS) { 4392 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4393 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 4394 scsa2usbp->scsa2usb_log_handle, 4395 "scsa2usb_open_usb_pipes: bulk/in pipe open " 4396 " failed rval = %d", rval); 4397 4398 return (USB_FAILURE); 4399 } 4400 4401 /* Open the bulk-out pipe using the same policy */ 4402 if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip, 4403 &scsa2usbp->scsa2usb_bulkout_ept, &policy, USB_FLAGS_SLEEP, 4404 &scsa2usbp->scsa2usb_bulkout_pipe)) != USB_SUCCESS) { 4405 usb_pipe_close(scsa2usbp->scsa2usb_dip, 4406 scsa2usbp->scsa2usb_bulkin_pipe, 4407 USB_FLAGS_SLEEP, NULL, NULL); 4408 4409 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4410 scsa2usbp->scsa2usb_bulkin_pipe = NULL; 4411 4412 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 4413 scsa2usbp->scsa2usb_log_handle, 4414 "scsa2usb_open_usb_pipes: bulk/out pipe open" 4415 " failed rval = %d", rval); 4416 4417 return (USB_FAILURE); 4418 } 4419 4420 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4421 4422 /* open interrupt pipe for CBI protocol */ 4423 if (SCSA2USB_IS_CBI(scsa2usbp)) { 4424 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4425 4426 if ((rval = usb_pipe_open(scsa2usbp->scsa2usb_dip, 4427 &scsa2usbp->scsa2usb_intr_ept, &policy, 4428 USB_FLAGS_SLEEP, &scsa2usbp->scsa2usb_intr_pipe)) != 4429 USB_SUCCESS) { 4430 usb_pipe_close(scsa2usbp->scsa2usb_dip, 4431 scsa2usbp->scsa2usb_bulkin_pipe, 4432 USB_FLAGS_SLEEP, NULL, NULL); 4433 4434 usb_pipe_close(scsa2usbp->scsa2usb_dip, 4435 scsa2usbp->scsa2usb_bulkout_pipe, 4436 USB_FLAGS_SLEEP, NULL, NULL); 4437 4438 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4439 scsa2usbp->scsa2usb_bulkin_pipe = NULL; 4440 scsa2usbp->scsa2usb_bulkout_pipe = NULL; 4441 4442 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 4443 scsa2usbp->scsa2usb_log_handle, 4444 "scsa2usb_open_usb_pipes: intr pipe open" 4445 " failed rval = %d", rval); 4446 4447 return (USB_FAILURE); 4448 } 4449 4450 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4451 } 4452 4453 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4454 4455 /* get the max transfer size of the bulk pipe */ 4456 if (usb_pipe_get_max_bulk_transfer_size(scsa2usbp->scsa2usb_dip, 4457 &sz) == USB_SUCCESS) { 4458 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4459 scsa2usbp->scsa2usb_max_bulk_xfer_size = sz; 4460 } else { 4461 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4462 scsa2usbp->scsa2usb_max_bulk_xfer_size = DEV_BSIZE; 4463 } 4464 4465 /* limit the xfer size */ 4466 scsa2usbp->scsa2usb_max_bulk_xfer_size = min( 4467 scsa2usbp->scsa2usb_max_bulk_xfer_size, 4468 scsa2usb_max_bulk_xfer_size); 4469 4470 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4471 "scsa2usb_open_usb_pipes: max bulk transfer size = %lx", 4472 scsa2usbp->scsa2usb_max_bulk_xfer_size); 4473 4474 /* Set the pipes opened flag */ 4475 scsa2usbp->scsa2usb_flags |= SCSA2USB_FLAGS_PIPES_OPENED; 4476 4477 scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_NORMAL; 4478 4479 /* Set the state to NONE */ 4480 scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE; 4481 } 4482 4483 return (USB_SUCCESS); 4484 } 4485 4486 4487 /* 4488 * scsa2usb_close_usb_pipes: 4489 * close all pipes synchronously 4490 */ 4491 void 4492 scsa2usb_close_usb_pipes(scsa2usb_state_t *scsa2usbp) 4493 { 4494 usb_flags_t flags = USB_FLAGS_SLEEP; 4495 4496 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4497 "scsa2usb_close_usb_pipes: scsa2usb_state = 0x%p", 4498 (void *)scsa2usbp); 4499 4500 ASSERT(scsa2usbp); 4501 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 4502 4503 if ((scsa2usbp->scsa2usb_flags & SCSA2USB_FLAGS_PIPES_OPENED) == 0) { 4504 4505 return; 4506 } 4507 4508 scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_CLOSING; 4509 /* to avoid races, reset the flag first */ 4510 scsa2usbp->scsa2usb_flags &= ~SCSA2USB_FLAGS_PIPES_OPENED; 4511 4512 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4513 4514 usb_pipe_close(scsa2usbp->scsa2usb_dip, 4515 scsa2usbp->scsa2usb_bulkout_pipe, flags, NULL, NULL); 4516 4517 usb_pipe_close(scsa2usbp->scsa2usb_dip, 4518 scsa2usbp->scsa2usb_bulkin_pipe, flags, NULL, NULL); 4519 4520 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4521 if (SCSA2USB_IS_CBI(scsa2usbp)) { 4522 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4523 usb_pipe_close(scsa2usbp->scsa2usb_dip, 4524 scsa2usbp->scsa2usb_intr_pipe, flags, NULL, NULL); 4525 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4526 } 4527 scsa2usbp->scsa2usb_bulkout_pipe = NULL; 4528 scsa2usbp->scsa2usb_bulkin_pipe = NULL; 4529 scsa2usbp->scsa2usb_intr_pipe = NULL; 4530 4531 scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_NORMAL; 4532 } 4533 4534 4535 /* 4536 * scsa2usb_fill_up_cdb_lba: 4537 * fill up command CDBs' LBA part 4538 */ 4539 static void 4540 scsa2usb_fill_up_cdb_lba(scsa2usb_cmd_t *cmd, int lba) 4541 { 4542 /* zero cdb1, lba bits so they won't get copied in the new cdb */ 4543 cmd->cmd_cdb[SCSA2USB_LUN] &= 0xE0; 4544 cmd->cmd_cdb[SCSA2USB_LBA_0] = lba >> 24; 4545 cmd->cmd_cdb[SCSA2USB_LBA_1] = lba >> 16; 4546 cmd->cmd_cdb[SCSA2USB_LBA_2] = lba >> 8; 4547 cmd->cmd_cdb[SCSA2USB_LBA_3] = (uchar_t)lba; 4548 cmd->cmd_lba = lba; 4549 } 4550 4551 4552 /* 4553 * scsa2usb_fill_up_ReadCD_cdb_len: 4554 * fill up READ_CD command CDBs' len part 4555 */ 4556 static void 4557 scsa2usb_fill_up_ReadCD_cdb_len(scsa2usb_cmd_t *cmd, int len, int actual_len) 4558 { 4559 cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_0] = len >> 16; 4560 cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_1] = len >> 8; 4561 cmd->cmd_cdb[SCSA2USB_READ_CD_LEN_2] = (uchar_t)len; 4562 cmd->cmd_actual_len = (uchar_t)actual_len; 4563 } 4564 4565 4566 /* 4567 * scsa2usb_fill_up_12byte_cdb_len: 4568 * fill up generic 12-byte command CDBs' len part 4569 */ 4570 static void 4571 scsa2usb_fill_up_12byte_cdb_len(scsa2usb_cmd_t *cmd, int len, int actual_len) 4572 { 4573 cmd->cmd_cdb[6] = len >> 24; 4574 cmd->cmd_cdb[7] = len >> 16; 4575 cmd->cmd_cdb[8] = len >> 8; 4576 cmd->cmd_cdb[9] = (uchar_t)len; 4577 cmd->cmd_actual_len = (uchar_t)actual_len; 4578 } 4579 4580 4581 /* 4582 * scsa2usb_fill_up_cdb_len: 4583 * fill up generic 10-byte command CDBs' len part 4584 */ 4585 static void 4586 scsa2usb_fill_up_cdb_len(scsa2usb_cmd_t *cmd, int len) 4587 { 4588 cmd->cmd_cdb[SCSA2USB_LEN_0] = len >> 8; 4589 cmd->cmd_cdb[SCSA2USB_LEN_1] = (uchar_t)len; 4590 } 4591 4592 4593 /* 4594 * scsa2usb_read_cd_blk_size: 4595 * For SCMD_READ_CD opcode (0xbe). Figure out the 4596 * block size based on expected sector type field 4597 * definition. See MMC SCSI Specs section 6.1.15 4598 * 4599 * Based on the value of the "expected_sector_type" 4600 * field, the block size could be different. 4601 */ 4602 static int 4603 scsa2usb_read_cd_blk_size(uchar_t expected_sector_type) 4604 { 4605 int blk_size; 4606 4607 switch (expected_sector_type) { 4608 case READ_CD_EST_CDDA: 4609 blk_size = CDROM_BLK_2352; 4610 break; 4611 case READ_CD_EST_MODE2: 4612 blk_size = CDROM_BLK_2336; 4613 break; 4614 case READ_CD_EST_MODE2FORM2: 4615 blk_size = CDROM_BLK_2324; 4616 break; 4617 case READ_CD_EST_MODE2FORM1: 4618 case READ_CD_EST_ALLTYPE: 4619 case READ_CD_EST_MODE1: 4620 default: 4621 blk_size = CDROM_BLK_2048; 4622 } 4623 4624 USB_DPRINTF_L4(DPRINT_MASK_SCSA, NULL, "scsa2usb_read_cd_blk_size: " 4625 "est = 0x%x blk_size = %d", expected_sector_type, blk_size); 4626 4627 return (blk_size); 4628 } 4629 4630 4631 /* needed for esballoc_wait() */ 4632 /*ARGSUSED*/ 4633 static void 4634 scsa2usb_null_free(char *arg) 4635 { 4636 } 4637 4638 static frtn_t fr = { 4639 scsa2usb_null_free, 4640 NULL 4641 }; 4642 4643 4644 /* 4645 * scsa2usb_bp_to_mblk: 4646 * Convert a bp to mblk_t. USBA framework understands mblk_t. 4647 */ 4648 static mblk_t * 4649 scsa2usb_bp_to_mblk(scsa2usb_state_t *scsa2usbp) 4650 { 4651 size_t size; 4652 mblk_t *mp; 4653 struct buf *bp; 4654 scsa2usb_cmd_t *cmd = PKT2CMD(scsa2usbp->scsa2usb_cur_pkt); 4655 4656 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4657 "scsa2usb_bp_to_mblk: "); 4658 4659 ASSERT(scsa2usbp->scsa2usb_cur_pkt); 4660 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 4661 4662 bp = cmd->cmd_bp; 4663 4664 if (bp && (bp->b_bcount > 0)) { 4665 size = ((bp->b_bcount > cmd->cmd_xfercount) ? 4666 cmd->cmd_xfercount : bp->b_bcount); 4667 } else { 4668 4669 return (NULL); 4670 } 4671 4672 mp = esballoc_wait((uchar_t *)bp->b_un.b_addr + cmd->cmd_offset, 4673 size, BPRI_LO, &fr); 4674 4675 USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4676 "scsa2usb_bp_to_mblk: " 4677 "mp=0x%p bp=0x%p pkt=0x%p off=0x%lx sz=%lu add=0x%p", 4678 (void *)mp, (void *)bp, (void *)scsa2usbp->scsa2usb_cur_pkt, 4679 cmd->cmd_offset, bp->b_bcount - cmd->cmd_offset, 4680 (void *)bp->b_un.b_addr); 4681 4682 mp->b_wptr += size; 4683 cmd->cmd_offset += size; 4684 4685 return (mp); 4686 } 4687 4688 4689 /* 4690 * scsa2usb_handle_data_start: 4691 * Initiate the data xfer. It could be IN/OUT direction. 4692 * 4693 * Data IN: 4694 * Send out the bulk-xfer request 4695 * if rval implies STALL 4696 * clear endpoint stall and reset bulk-in pipe 4697 * handle data read in so far; set cmd->cmd_done 4698 * also adjust data xfer length accordingly 4699 * else other error 4700 * report back to transport 4701 * typically transport will call reset recovery 4702 * else (no error) 4703 * return success 4704 * 4705 * Data OUT: 4706 * Send out the bulk-xfer request 4707 * if rval implies STALL 4708 * clear endpoint stall and reset bulk-in pipe 4709 * adjust data xfer length 4710 * else other error 4711 * report back to transport 4712 * typically transport will call reset recovery 4713 * else (no error) 4714 * return success 4715 * 4716 * NOTE: We call this function only if there is xfercount. 4717 */ 4718 int 4719 scsa2usb_handle_data_start(scsa2usb_state_t *scsa2usbp, 4720 scsa2usb_cmd_t *cmd, usb_bulk_req_t *req) 4721 { 4722 int rval = USB_SUCCESS; 4723 uint_t ept_addr; 4724 usb_flags_t flags = USB_FLAGS_SLEEP; 4725 #ifdef SCSA2USB_BULK_ONLY_TEST 4726 usb_req_attrs_t attrs = 0; 4727 #else 4728 usb_req_attrs_t attrs = USB_ATTRS_SHORT_XFER_OK; 4729 #endif 4730 4731 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4732 "scsa2usb_handle_data_start: BEGIN cmd = %p, req = %p", 4733 (void *)cmd, (void *)req); 4734 4735 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 4736 4737 switch (cmd->cmd_dir) { 4738 case USB_EP_DIR_IN: 4739 #ifdef SCSA2USB_BULK_ONLY_TEST 4740 /* 4741 * This case occurs when the host expects to receive 4742 * more data than the device actually transfers. Hi > Di 4743 */ 4744 if (scsa2usb_test_case_5) { 4745 usb_bulk_req_t *req2; 4746 4747 req->bulk_len = cmd->cmd_xfercount - 1; 4748 req->bulk_attributes = 0; 4749 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4750 SCSA2USB_FREE_MSG(req->bulk_data); 4751 req->bulk_data = allocb_wait(req->bulk_len, BPRI_LO, 4752 STR_NOSIG, NULL); 4753 4754 ASSERT(req->bulk_timeout); 4755 rval = usb_pipe_bulk_xfer( 4756 scsa2usbp->scsa2usb_bulkin_pipe, req, flags); 4757 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4758 USB_DPRINTF_L1(DPRINT_MASK_SCSA, 4759 scsa2usbp->scsa2usb_log_handle, "rval = %x", rval); 4760 4761 req2 = scsa2usb_init_bulk_req(scsa2usbp, 4762 cmd->cmd_xfercount + 2, 4763 cmd->cmd_timeout, 0, flags); 4764 req2->bulk_len = cmd->cmd_xfercount + 2; 4765 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4766 4767 ASSERT(req2->bulk_timeout); 4768 rval = usb_pipe_bulk_xfer( 4769 scsa2usbp->scsa2usb_bulkin_pipe, req2, flags); 4770 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4771 4772 USB_DPRINTF_L1(DPRINT_MASK_SCSA, 4773 scsa2usbp->scsa2usb_log_handle, 4774 "TEST 5: Hi > Di: rval = 0x%x", rval); 4775 scsa2usb_test_case_5 = 0; 4776 usb_free_bulk_req(req2); 4777 4778 return (rval); 4779 } 4780 4781 /* 4782 * This happens when the host expects to send data to the 4783 * device while the device intends to send data to the host. 4784 */ 4785 if (scsa2usb_test_case_8 && (cmd->cmd_cdb[0] == SCMD_READ_G1)) { 4786 USB_DPRINTF_L1(DPRINT_MASK_SCSA, 4787 scsa2usbp->scsa2usb_log_handle, 4788 "TEST 8: Hi <> Do: Step 2"); 4789 scsa2usb_test_mblk(scsa2usbp, B_TRUE); 4790 scsa2usb_test_case_8 = 0; 4791 4792 return (rval); 4793 } 4794 #endif /* SCSA2USB_BULK_ONLY_TEST */ 4795 4796 ept_addr = scsa2usbp->scsa2usb_bulkin_ept.bEndpointAddress; 4797 req->bulk_len = cmd->cmd_xfercount; 4798 req->bulk_attributes = attrs; 4799 SCSA2USB_FREE_MSG(req->bulk_data); 4800 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4801 4802 req->bulk_data = esballoc_wait( 4803 (uchar_t *)cmd->cmd_bp->b_un.b_addr + 4804 cmd->cmd_offset, 4805 req->bulk_len, BPRI_LO, &fr); 4806 4807 ASSERT(req->bulk_timeout); 4808 rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkin_pipe, 4809 req, flags); 4810 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4811 4812 break; 4813 4814 case USB_EP_DIR_OUT: 4815 #ifdef SCSA2USB_BULK_ONLY_TEST 4816 /* 4817 * This happens when the host expects to receive data 4818 * from the device while the device intends to receive 4819 * data from the host. 4820 */ 4821 if (scsa2usb_test_case_10 && 4822 (cmd->cmd_cdb[0] == SCMD_WRITE_G1)) { 4823 req->bulk_len = CSW_LEN; 4824 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4825 4826 ASSERT(req->bulk_timeout); 4827 rval = usb_pipe_bulk_xfer( 4828 scsa2usbp->scsa2usb_bulkin_pipe, req, flags); 4829 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4830 4831 USB_DPRINTF_L1(DPRINT_MASK_SCSA, 4832 scsa2usbp->scsa2usb_log_handle, 4833 "TEST 10: Ho <> Di: done rval = 0x%x", rval); 4834 scsa2usb_test_case_10 = 0; 4835 4836 return (rval); 4837 } 4838 #endif /* SCSA2USB_BULK_ONLY_TEST */ 4839 4840 req->bulk_data = scsa2usb_bp_to_mblk(scsa2usbp); 4841 if (req->bulk_data == NULL) { 4842 4843 return (USB_FAILURE); 4844 } 4845 4846 #ifdef SCSA2USB_BULK_ONLY_TEST 4847 if (scsa2usb_test_case_11) { 4848 /* 4849 * Host expects to send data to the device and 4850 * device doesn't expect to receive any data 4851 */ 4852 USB_DPRINTF_L1(DPRINT_MASK_SCSA, 4853 scsa2usbp->scsa2usb_log_handle, "TEST 11: Ho > Do"); 4854 4855 scsa2usb_test_mblk(scsa2usbp, B_FALSE); 4856 scsa2usb_test_case_11 = 0; 4857 } 4858 #endif /* SCSA2USB_BULK_ONLY_TEST */ 4859 4860 ept_addr = scsa2usbp->scsa2usb_bulkout_ept.bEndpointAddress; 4861 req->bulk_len = req->bulk_data->b_wptr - req->bulk_data->b_rptr; 4862 req->bulk_timeout = scsa2usb_bulk_timeout(cmd->cmd_timeout); 4863 mutex_exit(&scsa2usbp->scsa2usb_mutex); 4864 4865 ASSERT(req->bulk_timeout); 4866 rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkout_pipe, 4867 req, flags); 4868 mutex_enter(&scsa2usbp->scsa2usb_mutex); 4869 break; 4870 } 4871 4872 USB_DPRINTF_L3(DPRINT_MASK_SCSA, 4873 scsa2usbp->scsa2usb_log_handle, 4874 "scsa2usb_handle_data_start: rval=%d cr=%d", rval, 4875 req->bulk_completion_reason); 4876 4877 if (rval != USB_SUCCESS) { 4878 /* Handle Errors now */ 4879 if (req->bulk_completion_reason == USB_CR_STALL) { 4880 if (cmd->cmd_dir == USB_EP_DIR_IN) { 4881 (void) scsa2usb_clear_ept_stall( 4882 scsa2usbp, ept_addr, 4883 scsa2usbp-> scsa2usb_bulkin_pipe, 4884 "bulk-in"); 4885 } else { 4886 (void) scsa2usb_clear_ept_stall( 4887 scsa2usbp, ept_addr, 4888 scsa2usbp-> scsa2usb_bulkout_pipe, 4889 "bulk-out"); 4890 } 4891 } 4892 4893 /* no more data to transfer after this */ 4894 cmd->cmd_done = 1; 4895 } 4896 4897 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4898 "scsa2usb_handle_data_start: END %s data rval = %d", 4899 (cmd->cmd_dir == USB_EP_DIR_IN) ? "bulk-in" : "bulk-out", rval); 4900 4901 return (rval); 4902 } 4903 4904 4905 /* 4906 * scsa2usb_handle_data_done: 4907 * This function handles the completion of the data xfer. 4908 * It also massages the inquiry data. This function may 4909 * also be called after a stall. 4910 */ 4911 void 4912 scsa2usb_handle_data_done(scsa2usb_state_t *scsa2usbp, 4913 scsa2usb_cmd_t *cmd, usb_bulk_req_t *req) 4914 { 4915 struct buf *bp = cmd->cmd_bp; 4916 struct scsi_pkt *pkt = scsa2usbp->scsa2usb_cur_pkt; 4917 mblk_t *data = req->bulk_data; 4918 int len = data ? (data->b_wptr - data->b_rptr) : 0; 4919 uint32_t max_lba; 4920 4921 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 4922 4923 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 4924 "scsa2usb_handle_data_done:\n\tcmd = 0x%p data = 0x%p len = 0x%x", 4925 (void *)cmd, (void *)data, len); 4926 4927 cmd->cmd_resid_xfercount = cmd->cmd_xfercount - len; 4928 4929 if (len) { 4930 uchar_t *p; 4931 uchar_t dtype; 4932 scsa2usb_read_cap_t *cap; 4933 struct scsi_inquiry *inq; 4934 4935 switch (cmd->cmd_cdb[SCSA2USB_OPCODE]) { 4936 case SCMD_INQUIRY: 4937 /* 4938 * cache a copy of the inquiry data for our own use 4939 * but ensure that we have at least up to 4940 * inq_revision, inq_serial is not required. 4941 * ignore inquiry data returned for inquiry commands 4942 * with SCSI-3 EVPD, CmdDt bits set. 4943 */ 4944 if (((cmd->cmd_cdb[SCSA2USB_LUN] & 0x1f) == 0) && 4945 (len >= SCSA2USB_MAX_INQ_LEN)) { 4946 inq = (struct scsi_inquiry *)data->b_rptr; 4947 dtype = inq->inq_dtype & DTYPE_MASK; 4948 /* 4949 * scsi framework sends zero byte write(10) cmd 4950 * to (Simplified) direct-access devices with 4951 * inquiry version > 2 for reservation changes. 4952 * But some USB devices don't support zero byte 4953 * write(10) even though they have inquiry 4954 * version > 2. Considering scsa2usb driver 4955 * doesn't support reservation and all the 4956 * reservation cmds are being faked, we fake 4957 * the inquiry version to 0 to make scsi 4958 * framework send test unit ready cmd which is 4959 * supported by all the usb devices. 4960 */ 4961 if (((dtype == DTYPE_DIRECT) || 4962 (dtype == DTYPE_RBC)) && 4963 (inq->inq_ansi > 2)) { 4964 inq->inq_ansi = 0; 4965 } 4966 4967 bzero(&scsa2usbp->scsa2usb_lun_inquiry 4968 [pkt->pkt_address.a_lun], 4969 sizeof (struct scsi_inquiry)); 4970 bcopy(data->b_rptr, 4971 &scsa2usbp->scsa2usb_lun_inquiry 4972 [pkt->pkt_address.a_lun], len); 4973 } 4974 4975 USB_DPRINTF_L3(DPRINT_MASK_SCSA, 4976 scsa2usbp->scsa2usb_log_handle, 4977 "scsi inquiry type = 0x%x", 4978 scsa2usbp->scsa2usb_lun_inquiry 4979 [pkt->pkt_address.a_lun].inq_dtype); 4980 4981 cmd->cmd_done = 1; 4982 goto handle_data; 4983 4984 case SCMD_READ_CAPACITY: 4985 cap = (scsa2usb_read_cap_t *)data->b_rptr; 4986 4987 /* Figure out the logical block size */ 4988 if ((len >= sizeof (struct scsa2usb_read_cap)) && 4989 (req->bulk_completion_reason == USB_CR_OK)) { 4990 scsa2usbp-> 4991 scsa2usb_lbasize[pkt->pkt_address.a_lun] = 4992 SCSA2USB_MK_32BIT( 4993 cap->scsa2usb_read_cap_blen3, 4994 cap->scsa2usb_read_cap_blen2, 4995 cap->scsa2usb_read_cap_blen1, 4996 cap->scsa2usb_read_cap_blen0); 4997 4998 max_lba = SCSA2USB_MK_32BIT( 4999 cap->scsa2usb_read_cap_lba3, 5000 cap->scsa2usb_read_cap_lba2, 5001 cap->scsa2usb_read_cap_lba1, 5002 cap->scsa2usb_read_cap_lba0); 5003 5004 /* 5005 * Some devices return total logical block 5006 * number instead of highest logical block 5007 * address. Adjust the value by minus 1. 5008 */ 5009 if (max_lba > 0 && (scsa2usbp->scsa2usb_attrs & 5010 SCSA2USB_ATTRS_NO_CAP_ADJUST) == 0) { 5011 max_lba -= 1; 5012 cap->scsa2usb_read_cap_lba0 = 5013 (uchar_t)(max_lba & 0xFF); 5014 cap->scsa2usb_read_cap_lba1 = 5015 (uchar_t)(max_lba >> 8 & 0xFF); 5016 cap->scsa2usb_read_cap_lba2 = 5017 (uchar_t)(max_lba >> 16 & 0xFF); 5018 cap->scsa2usb_read_cap_lba3 = 5019 (uchar_t)(max_lba >> 24 & 0xFF); 5020 } 5021 5022 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 5023 scsa2usbp->scsa2usb_log_handle, 5024 "bytes in each logical block=0x%lx," 5025 "number of total logical blocks=0x%x", 5026 scsa2usbp-> 5027 scsa2usb_lbasize[pkt->pkt_address.a_lun], 5028 max_lba + 1); 5029 } 5030 cmd->cmd_done = 1; 5031 goto handle_data; 5032 5033 case SCMD_REQUEST_SENSE: 5034 p = data->b_rptr; 5035 USB_DPRINTF_L2(DPRINT_MASK_SCSA, 5036 scsa2usbp->scsa2usb_log_handle, 5037 "cdb: %x rqsense: " 5038 "%x %x %x %x %x %x %x %x %x %x\n\t" 5039 "%x %x %x %x %x %x %x %x %x %x", 5040 cmd->cmd_cdb[0], 5041 p[0], p[1], p[2], p[3], p[4], 5042 p[5], p[6], p[7], p[8], p[9], 5043 p[10], p[11], p[12], p[13], p[14], 5044 p[15], p[16], p[17], p[18], p[19]); 5045 5046 scsa2usbp->scsa2usb_last_cmd.status = p[2]; 5047 cmd->cmd_done = 1; 5048 /* FALLTHROUGH */ 5049 5050 default: 5051 handle_data: 5052 if (bp && len && (cmd->cmd_dir == USB_EP_DIR_IN)) { 5053 /* 5054 * we don't have to copy the data, the 5055 * data pointers for the mblk_t for 5056 * the bulk-in xfer points to the 5057 * struct buf * data. 5058 */ 5059 cmd->cmd_offset += len; 5060 } 5061 5062 USB_DPRINTF_L3(DPRINT_MASK_SCSA, 5063 scsa2usbp->scsa2usb_log_handle, 5064 "len = 0x%x total = 0x%lx offset = 0x%lx", 5065 len, cmd->cmd_total_xfercount, cmd->cmd_offset); 5066 5067 /* 5068 * update total_xfercount now but it may be 5069 * adjusted after receiving the residue 5070 */ 5071 cmd->cmd_total_xfercount -= len; 5072 5073 if ((req->bulk_completion_reason != USB_CR_OK) || 5074 (cmd->cmd_resid_xfercount != 0) || 5075 (cmd->cmd_total_xfercount == 0)) { 5076 /* set pkt_resid to total to be sure */ 5077 pkt->pkt_resid = cmd->cmd_total_xfercount; 5078 cmd->cmd_done = 1; 5079 } 5080 5081 break; 5082 } 5083 } else { 5084 if (cmd->cmd_dir == USB_EP_DIR_OUT) { 5085 if (cmd->cmd_total_xfercount == 0) { 5086 cmd->cmd_done = 1; 5087 } 5088 } 5089 } 5090 } 5091 5092 5093 /* 5094 * scsa2usb_init_bulk_req: 5095 * Allocate (synchronously) and fill in a bulk-request 5096 */ 5097 usb_bulk_req_t * 5098 scsa2usb_init_bulk_req(scsa2usb_state_t *scsa2usbp, size_t length, 5099 uint_t timeout, usb_req_attrs_t attrs, usb_flags_t flags) 5100 { 5101 usb_bulk_req_t *req; 5102 5103 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 5104 5105 req = usb_alloc_bulk_req(scsa2usbp->scsa2usb_dip, length, 5106 flags | USB_FLAGS_SLEEP); 5107 5108 req->bulk_len = length; /* xfer length */ 5109 req->bulk_timeout = scsa2usb_bulk_timeout(timeout); /* xfer timeout */ 5110 req->bulk_attributes = attrs; /* xfer attrs */ 5111 req->bulk_client_private = (usb_opaque_t)scsa2usbp; /* statep */ 5112 5113 return (req); 5114 } 5115 5116 5117 /* 5118 * scsa2usb_bulk_timeout: 5119 * ensure that bulk requests do not have infinite timeout values 5120 */ 5121 int 5122 scsa2usb_bulk_timeout(int timeout) 5123 { 5124 return ((timeout == 0) ? scsa2usb_long_timeout : timeout); 5125 } 5126 5127 5128 /* 5129 * scsa2usb_clear_ept_stall: 5130 * clear endpoint stall and reset pipes 5131 */ 5132 int 5133 scsa2usb_clear_ept_stall(scsa2usb_state_t *scsa2usbp, uint_t ept_addr, 5134 usb_pipe_handle_t ph, char *what) 5135 { 5136 int rval; 5137 dev_info_t *dip = scsa2usbp->scsa2usb_dip; 5138 5139 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 5140 if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) { 5141 5142 return (USB_FAILURE); 5143 } 5144 5145 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5146 rval = usb_clr_feature(dip, USB_DEV_REQ_RCPT_EP, 0, ept_addr, 5147 USB_FLAGS_SLEEP, NULL, NULL); 5148 5149 usb_pipe_reset(dip, ph, USB_FLAGS_SLEEP, NULL, NULL); 5150 5151 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5152 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 5153 "scsa2usb_clear_ept_stall: on %s: ept = 0x%x rval = %d", 5154 what, ept_addr, rval); 5155 5156 return (rval); 5157 } 5158 5159 5160 /* 5161 * scsa2usb_pkt_completion: 5162 * Handle pkt completion. 5163 */ 5164 static void 5165 scsa2usb_pkt_completion(scsa2usb_state_t *scsa2usbp, struct scsi_pkt *pkt) 5166 { 5167 scsa2usb_cmd_t *cmd = PKT2CMD(pkt); 5168 size_t len; 5169 5170 ASSERT(pkt); 5171 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 5172 5173 USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 5174 "scsa2usb_pkt_completion:\n\tscsa2usbp = 0x%p " 5175 "reason=%d, status=%d state=0x%x stats=0x%x resid=0x%lx", 5176 (void *)scsa2usbp, pkt->pkt_reason, *(pkt->pkt_scbp), 5177 pkt->pkt_state, pkt->pkt_statistics, pkt->pkt_resid); 5178 5179 if (pkt->pkt_reason == CMD_CMPLT) { 5180 pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET | 5181 STATE_SENT_CMD | STATE_GOT_STATUS; 5182 if (cmd->cmd_xfercount) { 5183 pkt->pkt_state |= STATE_XFERRED_DATA; 5184 } 5185 } else { 5186 pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET | 5187 STATE_SENT_CMD; 5188 } 5189 5190 /* 5191 * don't zap the current state when in panic as this will 5192 * make debugging harder 5193 */ 5194 if ((scsa2usbp->scsa2usb_cur_pkt == pkt) && !ddi_in_panic()) { 5195 SCSA2USB_RESET_CUR_PKT(scsa2usbp); 5196 5197 len = sizeof (scsa2usbp->scsa2usb_last_cmd.cdb); 5198 bzero(scsa2usbp->scsa2usb_last_cmd.cdb, len); 5199 5200 len = (len < cmd->cmd_cdblen) ? len : cmd->cmd_cdblen; 5201 USB_DPRINTF_L3(DPRINT_MASK_SCSA, 5202 scsa2usbp->scsa2usb_log_handle, 5203 "scsa2usb_pkt_completion: save last cmd, len=%ld", len); 5204 5205 /* save the last command */ 5206 bcopy(pkt->pkt_cdbp, scsa2usbp->scsa2usb_last_cmd.cdb, len); 5207 5208 /* reset the scsa2usb_last_cmd.status value */ 5209 if ((pkt->pkt_cdbp[0] != SCMD_REQUEST_SENSE) && 5210 (pkt->pkt_cdbp[0] != SCMD_INQUIRY)) { 5211 scsa2usbp->scsa2usb_last_cmd.status = 0; 5212 } 5213 5214 /* 5215 * set pkt state to NONE *before* calling back as the target 5216 * driver will immediately submit the next packet 5217 */ 5218 scsa2usbp->scsa2usb_pkt_state = SCSA2USB_PKT_NONE; 5219 } 5220 5221 if (pkt->pkt_comp) { 5222 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5223 pkt->pkt_comp(pkt); 5224 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5225 5226 } 5227 } 5228 5229 5230 /* 5231 * Even handling functions: 5232 * 5233 * scsa2usb_reconnect_event_cb: 5234 * event handling 5235 */ 5236 static int 5237 scsa2usb_reconnect_event_cb(dev_info_t *dip) 5238 { 5239 scsa2usb_state_t *scsa2usbp = 5240 ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip)); 5241 dev_info_t *cdip; 5242 int circ; 5243 int rval = USB_SUCCESS; 5244 5245 ASSERT(scsa2usbp != NULL); 5246 5247 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 5248 "scsa2usb_reconnect_event_cb: dip = 0x%p", (void *)dip); 5249 5250 scsa2usb_restore_device_state(dip, scsa2usbp); 5251 5252 USB_DPRINTF_L0(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 5253 "Reinserted device is accessible again."); 5254 5255 ndi_devi_enter(dip, &circ); 5256 for (cdip = ddi_get_child(dip); cdip; ) { 5257 dev_info_t *next = ddi_get_next_sibling(cdip); 5258 5259 mutex_enter(&DEVI(cdip)->devi_lock); 5260 DEVI_SET_DEVICE_REINSERTED(cdip); 5261 mutex_exit(&DEVI(cdip)->devi_lock); 5262 5263 cdip = next; 5264 } 5265 ndi_devi_exit(dip, circ); 5266 5267 /* stop suppressing warnings */ 5268 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5269 scsa2usbp->scsa2usb_warning_given = B_FALSE; 5270 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5271 5272 if (scsa2usbp->scsa2usb_ugen_hdl) { 5273 rval = usb_ugen_reconnect_ev_cb( 5274 scsa2usbp->scsa2usb_ugen_hdl); 5275 } 5276 5277 return (rval); 5278 } 5279 5280 5281 /* 5282 * scsa2usb_all_waitQs_empty: 5283 * check if all waitQs empty 5284 */ 5285 static int 5286 scsa2usb_all_waitQs_empty(scsa2usb_state_t *scsa2usbp) 5287 { 5288 uint_t lun; 5289 5290 for (lun = 0; lun < SCSA2USB_MAX_LUNS; lun++) { 5291 if (usba_list_entry_count( 5292 &scsa2usbp->scsa2usb_waitQ[lun])) { 5293 5294 return (USB_FAILURE); 5295 } 5296 } 5297 5298 return (USB_SUCCESS); 5299 } 5300 5301 5302 /* 5303 * scsa2usb_disconnect_event_cb: 5304 * callback for disconnect events 5305 */ 5306 static int 5307 scsa2usb_disconnect_event_cb(dev_info_t *dip) 5308 { 5309 scsa2usb_state_t *scsa2usbp = 5310 ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip)); 5311 dev_info_t *cdip; 5312 int circ, i; 5313 int rval = USB_SUCCESS; 5314 5315 ASSERT(scsa2usbp != NULL); 5316 5317 USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 5318 "scsa2usb_disconnect_event_cb: dip = 0x%p", (void *)dip); 5319 5320 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5321 scsa2usbp->scsa2usb_dev_state = USB_DEV_DISCONNECTED; 5322 5323 /* 5324 * wait till the work thread is done, carry on regardless 5325 * if not. 5326 */ 5327 for (i = 0; i < SCSA2USB_DRAIN_TIMEOUT; i++) { 5328 if ((scsa2usbp->scsa2usb_work_thread_id == NULL) && 5329 (scsa2usbp->scsa2usb_cur_pkt == NULL) && 5330 (scsa2usb_all_waitQs_empty(scsa2usbp) == 5331 USB_SUCCESS)) { 5332 5333 break; 5334 } 5335 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5336 delay(drv_usectohz(1000000)); 5337 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5338 } 5339 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5340 5341 ndi_devi_enter(dip, &circ); 5342 for (cdip = ddi_get_child(dip); cdip; ) { 5343 dev_info_t *next = ddi_get_next_sibling(cdip); 5344 5345 mutex_enter(&DEVI(cdip)->devi_lock); 5346 DEVI_SET_DEVICE_REMOVED(cdip); 5347 mutex_exit(&DEVI(cdip)->devi_lock); 5348 5349 cdip = next; 5350 } 5351 ndi_devi_exit(dip, circ); 5352 5353 if (scsa2usbp->scsa2usb_ugen_hdl) { 5354 rval = usb_ugen_disconnect_ev_cb( 5355 scsa2usbp->scsa2usb_ugen_hdl); 5356 } 5357 5358 return (rval); 5359 } 5360 5361 5362 /* 5363 * PM support 5364 * 5365 * scsa2usb_create_pm_components: 5366 * create the pm components required for power management 5367 * no mutex is need when calling USBA interfaces 5368 */ 5369 static void 5370 scsa2usb_create_pm_components(dev_info_t *dip, scsa2usb_state_t *scsa2usbp) 5371 { 5372 scsa2usb_power_t *pm; 5373 uint_t pwr_states; 5374 5375 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 5376 5377 USB_DPRINTF_L4(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle, 5378 "scsa2usb_create_pm_components: dip = 0x%p, scsa2usbp = 0x%p", 5379 (void *)dip, (void *)scsa2usbp); 5380 5381 /* 5382 * determine if this device is on the blacklist 5383 * or if a conf file entry has disabled PM 5384 */ 5385 if ((scsa2usbp->scsa2usb_attrs & SCSA2USB_ATTRS_PM) == 0) { 5386 USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle, 5387 "device cannot be power managed"); 5388 5389 return; 5390 } 5391 5392 /* Allocate the PM state structure */ 5393 pm = kmem_zalloc(sizeof (scsa2usb_power_t), KM_SLEEP); 5394 5395 scsa2usbp->scsa2usb_pm = pm; 5396 pm->scsa2usb_current_power = USB_DEV_OS_FULL_PWR; 5397 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5398 5399 if (usb_create_pm_components(dip, &pwr_states) == 5400 USB_SUCCESS) { 5401 if (usb_handle_remote_wakeup(dip, 5402 USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) { 5403 pm->scsa2usb_wakeup_enabled = 1; 5404 } 5405 5406 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5407 pm->scsa2usb_pwr_states = (uint8_t)pwr_states; 5408 scsa2usb_raise_power(scsa2usbp); 5409 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5410 } 5411 5412 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5413 } 5414 5415 5416 /* 5417 * scsa2usb_raise_power: 5418 * check if the device is using full power or not 5419 */ 5420 static void 5421 scsa2usb_raise_power(scsa2usb_state_t *scsa2usbp) 5422 { 5423 USB_DPRINTF_L4(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle, 5424 "scsa2usb_raise_power:"); 5425 5426 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 5427 5428 if (scsa2usbp->scsa2usb_pm) { 5429 scsa2usb_pm_busy_component(scsa2usbp); 5430 if (scsa2usbp->scsa2usb_pm->scsa2usb_current_power != 5431 USB_DEV_OS_FULL_PWR) { 5432 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5433 (void) pm_raise_power(scsa2usbp->scsa2usb_dip, 5434 0, USB_DEV_OS_FULL_PWR); 5435 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5436 } 5437 } 5438 } 5439 5440 5441 /* 5442 * functions to handle power transition for OS levels 0 -> 3 5443 */ 5444 static int 5445 scsa2usb_pwrlvl0(scsa2usb_state_t *scsa2usbp) 5446 { 5447 int rval; 5448 5449 switch (scsa2usbp->scsa2usb_dev_state) { 5450 case USB_DEV_ONLINE: 5451 /* Deny the powerdown request if the device is busy */ 5452 if (scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy != 0) { 5453 5454 return (USB_FAILURE); 5455 } 5456 5457 /* 5458 * stop polling on interrupt pipe 5459 */ 5460 scsa2usb_cbi_stop_intr_polling(scsa2usbp); 5461 5462 /* Issue USB D3 command to the device here */ 5463 rval = usb_set_device_pwrlvl3(scsa2usbp->scsa2usb_dip); 5464 ASSERT(rval == USB_SUCCESS); 5465 5466 scsa2usbp->scsa2usb_dev_state = USB_DEV_PWRED_DOWN; 5467 5468 /* FALLTHRU */ 5469 case USB_DEV_DISCONNECTED: 5470 case USB_DEV_SUSPENDED: 5471 case USB_DEV_PWRED_DOWN: 5472 default: 5473 scsa2usbp->scsa2usb_pm->scsa2usb_current_power = 5474 USB_DEV_OS_PWR_OFF; 5475 5476 return (USB_SUCCESS); 5477 } 5478 } 5479 5480 5481 static int 5482 scsa2usb_pwrlvl1(scsa2usb_state_t *scsa2usbp) 5483 { 5484 int rval; 5485 5486 /* Issue USB D2 command to the device here */ 5487 rval = usb_set_device_pwrlvl2(scsa2usbp->scsa2usb_dip); 5488 ASSERT(rval == USB_SUCCESS); 5489 5490 return (DDI_FAILURE); 5491 } 5492 5493 5494 static int 5495 scsa2usb_pwrlvl2(scsa2usb_state_t *scsa2usbp) 5496 { 5497 int rval; 5498 5499 /* Issue USB D1 command to the device here */ 5500 rval = usb_set_device_pwrlvl1(scsa2usbp->scsa2usb_dip); 5501 ASSERT(rval == USB_SUCCESS); 5502 5503 return (DDI_FAILURE); 5504 } 5505 5506 5507 static int 5508 scsa2usb_pwrlvl3(scsa2usb_state_t *scsa2usbp) 5509 { 5510 int rval; 5511 5512 /* 5513 * PM framework tries to put us in full power 5514 * during system shutdown. If we are disconnected 5515 * return success anyways 5516 */ 5517 if (scsa2usbp->scsa2usb_dev_state != USB_DEV_DISCONNECTED) { 5518 /* Issue USB D0 command to the device here */ 5519 rval = usb_set_device_pwrlvl0(scsa2usbp->scsa2usb_dip); 5520 ASSERT(rval == USB_SUCCESS); 5521 5522 scsa2usbp->scsa2usb_dev_state = USB_DEV_ONLINE; 5523 } 5524 scsa2usbp->scsa2usb_pm->scsa2usb_current_power = USB_DEV_OS_FULL_PWR; 5525 5526 return (DDI_SUCCESS); 5527 } 5528 5529 5530 /* 5531 * scsa2usb_power: 5532 * power entry point 5533 */ 5534 /* ARGSUSED */ 5535 static int 5536 scsa2usb_power(dev_info_t *dip, int comp, int level) 5537 { 5538 scsa2usb_state_t *scsa2usbp; 5539 scsa2usb_power_t *pm; 5540 int rval = DDI_FAILURE; 5541 5542 scsa2usbp = ddi_get_soft_state(scsa2usb_statep, ddi_get_instance(dip)); 5543 5544 USB_DPRINTF_L3(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle, 5545 "scsa2usb_power: Begin scsa2usbp (%p): level = %d", 5546 (void *)scsa2usbp, level); 5547 5548 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5549 if (SCSA2USB_BUSY(scsa2usbp)) { 5550 USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle, 5551 "scsa2usb_power: busy"); 5552 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5553 5554 return (rval); 5555 } 5556 5557 pm = scsa2usbp->scsa2usb_pm; 5558 if (pm == NULL) { 5559 USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle, 5560 "scsa2usb_power: pm NULL"); 5561 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5562 5563 return (rval); 5564 } 5565 5566 /* check if we are transitioning to a legal power level */ 5567 if (USB_DEV_PWRSTATE_OK(pm->scsa2usb_pwr_states, level)) { 5568 USB_DPRINTF_L2(DPRINT_MASK_PM, scsa2usbp->scsa2usb_log_handle, 5569 "scsa2usb_power: illegal power level = %d " 5570 "pwr_states: %x", level, pm->scsa2usb_pwr_states); 5571 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5572 5573 return (rval); 5574 } 5575 5576 switch (level) { 5577 case USB_DEV_OS_PWR_OFF : 5578 rval = scsa2usb_pwrlvl0(scsa2usbp); 5579 break; 5580 case USB_DEV_OS_PWR_1 : 5581 rval = scsa2usb_pwrlvl1(scsa2usbp); 5582 break; 5583 case USB_DEV_OS_PWR_2 : 5584 rval = scsa2usb_pwrlvl2(scsa2usbp); 5585 break; 5586 case USB_DEV_OS_FULL_PWR : 5587 rval = scsa2usb_pwrlvl3(scsa2usbp); 5588 break; 5589 } 5590 5591 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5592 5593 return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE); 5594 } 5595 5596 5597 static void 5598 scsa2usb_pm_busy_component(scsa2usb_state_t *scsa2usbp) 5599 { 5600 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 5601 5602 if (scsa2usbp->scsa2usb_pm) { 5603 scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy++; 5604 5605 USB_DPRINTF_L4(DPRINT_MASK_PM, 5606 scsa2usbp->scsa2usb_log_handle, 5607 "scsa2usb_pm_busy_component: %d", 5608 scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy); 5609 5610 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5611 5612 if (pm_busy_component(scsa2usbp->scsa2usb_dip, 0) != 5613 DDI_SUCCESS) { 5614 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5615 ASSERT(scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy > 0); 5616 scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy--; 5617 5618 USB_DPRINTF_L2(DPRINT_MASK_PM, 5619 scsa2usbp->scsa2usb_log_handle, 5620 "scsa2usb_pm_busy_component failed: %d", 5621 scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy); 5622 5623 return; 5624 } 5625 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5626 } 5627 } 5628 5629 5630 /* 5631 * scsa2usb_pm_idle_component: 5632 * idles the device 5633 */ 5634 static void 5635 scsa2usb_pm_idle_component(scsa2usb_state_t *scsa2usbp) 5636 { 5637 ASSERT(!mutex_owned(&scsa2usbp->scsa2usb_mutex)); 5638 5639 if (scsa2usbp->scsa2usb_pm) { 5640 if (pm_idle_component(scsa2usbp->scsa2usb_dip, 0) == 5641 DDI_SUCCESS) { 5642 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5643 ASSERT(scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy > 0); 5644 scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy--; 5645 5646 USB_DPRINTF_L4(DPRINT_MASK_PM, 5647 scsa2usbp->scsa2usb_log_handle, 5648 "scsa2usb_pm_idle_component: %d", 5649 scsa2usbp->scsa2usb_pm->scsa2usb_pm_busy); 5650 5651 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5652 } 5653 } 5654 } 5655 5656 5657 #ifdef DEBUG 5658 /* 5659 * scsa2usb_print_cdb: 5660 * prints CDB 5661 */ 5662 void 5663 scsa2usb_print_cdb(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd) 5664 { 5665 uchar_t *c = (uchar_t *)&cmd->cmd_cdb; 5666 5667 USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 5668 "cmd = 0x%p opcode=%s " 5669 "cdb: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", 5670 (void *)cmd, 5671 scsi_cname(cmd->cmd_cdb[SCSA2USB_OPCODE], scsa2usb_cmds), 5672 c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], 5673 c[9], c[10], c[11], c[12], c[13], c[14], c[15]); 5674 } 5675 #endif /* DEBUG */ 5676 5677 5678 #ifdef SCSA2USB_BULK_ONLY_TEST 5679 /* 5680 * scsa2usb_test_mblk: 5681 * This function sends a dummy data mblk_t to simulate 5682 * the following test cases: 5 and 11. 5683 */ 5684 static void 5685 scsa2usb_test_mblk(scsa2usb_state_t *scsa2usbp, boolean_t large) 5686 { 5687 int i, rval; 5688 size_t len; 5689 usb_flags_t flags = USB_FLAGS_SLEEP; 5690 usb_bulk_req_t *req; 5691 5692 ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex)); 5693 5694 /* should we create a larger mblk? */ 5695 len = (large == B_TRUE) ? DEV_BSIZE : USB_BULK_CBWCMD_LEN; 5696 5697 req = scsa2usb_init_bulk_req(scsa2usbp, len, 5698 SCSA2USB_BULK_PIPE_TIMEOUT, 0, flags); 5699 5700 /* fill up the data mblk */ 5701 for (i = 0; i < len; i++) { 5702 *req->bulk_data->b_wptr++ = (uchar_t)i; 5703 } 5704 5705 mutex_exit(&scsa2usbp->scsa2usb_mutex); 5706 ASSERT(req->bulk_timeout); 5707 rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkout_pipe, req, flags); 5708 mutex_enter(&scsa2usbp->scsa2usb_mutex); 5709 5710 USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle, 5711 "scsa2usb_test_mblk: Sent Data Out rval = 0x%x", rval); 5712 5713 usb_free_bulk_req(req); 5714 } 5715 #endif /* SCSA2USB_BULK_ONLY_TEST */ 5716