1 /* $OpenBSD: scsiconf.c,v 1.138 2009/02/16 21:19:07 miod Exp $ */ 2 /* $NetBSD: scsiconf.c,v 1.57 1996/05/02 01:09:01 neil Exp $ */ 3 4 /* 5 * Copyright (c) 1994 Charles Hannum. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Charles Hannum. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Originally written by Julian Elischer (julian@tfs.com) 35 * for TRW Financial Systems for use under the MACH(2.5) operating system. 36 * 37 * TRW Financial Systems, in accordance with their agreement with Carnegie 38 * Mellon University, makes this software available to CMU to distribute 39 * or use in any manner that they see fit as long as this message is kept with 40 * the software. For this reason TFS also grants any other persons or 41 * organisations permission to use or modify this software. 42 * 43 * TFS supplies this software to be publicly redistributed 44 * on the understanding that TFS is not responsible for the correct 45 * functioning of this software in any circumstances. 46 * 47 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 48 */ 49 50 #include "bio.h" 51 52 #include <sys/types.h> 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/malloc.h> 56 #include <sys/device.h> 57 #include <sys/buf.h> 58 59 #include <scsi/scsi_all.h> 60 #include <scsi/scsiconf.h> 61 62 #if NBIO > 0 63 #include <sys/ioctl.h> 64 #include <sys/scsiio.h> 65 #include <dev/biovar.h> 66 #endif 67 68 /* 69 * Declarations 70 */ 71 int scsi_probedev(struct scsibus_softc *, int, int); 72 73 void scsi_devid(struct scsi_link *); 74 int scsi_devid_pg83(struct scsi_link *); 75 76 struct scsi_device probe_switch = { 77 NULL, 78 NULL, 79 NULL, 80 NULL, 81 }; 82 83 int scsibusmatch(struct device *, void *, void *); 84 void scsibusattach(struct device *, struct device *, void *); 85 int scsibusactivate(struct device *, enum devact); 86 int scsibusdetach(struct device *, int); 87 88 int scsibussubmatch(struct device *, void *, void *); 89 90 #if NBIO > 0 91 int scsibus_bioctl(struct device *, u_long, caddr_t); 92 #endif 93 94 struct cfattach scsibus_ca = { 95 sizeof(struct scsibus_softc), scsibusmatch, scsibusattach, 96 scsibusdetach, scsibusactivate 97 }; 98 99 struct cfdriver scsibus_cd = { 100 NULL, "scsibus", DV_DULL 101 }; 102 103 #ifdef SCSIDEBUG 104 int scsidebug_buses = SCSIDEBUG_BUSES; 105 int scsidebug_targets = SCSIDEBUG_TARGETS; 106 int scsidebug_luns = SCSIDEBUG_LUNS; 107 int scsidebug_level = SCSIDEBUG_LEVEL; 108 #endif 109 110 int scsi_autoconf = SCSI_AUTOCONF; 111 112 int scsibusprint(void *, const char *); 113 114 const u_int8_t version_to_spc [] = { 115 0, /* 0x00: The device does not claim conformance to any standard. */ 116 1, /* 0x01: (Obsolete) SCSI-1 in olden times. */ 117 2, /* 0x02: (Obsolete) SCSI-2 in olden times. */ 118 3, /* 0x03: The device complies to ANSI INCITS 301-1997 (SPC-3). */ 119 2, /* 0x04: The device complies to ANSI INCITS 351-2001 (SPC-2). */ 120 3, /* 0x05: The device complies to ANSI INCITS 408-2005 (SPC-3). */ 121 4, /* 0x06: The device complies to SPC-4. */ 122 0, /* 0x07: RESERVED. */ 123 }; 124 125 int 126 scsiprint(void *aux, const char *pnp) 127 { 128 /* only "scsibus"es can attach to "scsi"s; easy. */ 129 if (pnp) 130 printf("scsibus at %s", pnp); 131 132 return (UNCONF); 133 } 134 135 int 136 scsibusmatch(struct device *parent, void *match, void *aux) 137 { 138 return (1); 139 } 140 141 /* 142 * The routine called by the adapter boards to get all their 143 * devices configured in. 144 */ 145 void 146 scsibusattach(struct device *parent, struct device *self, void *aux) 147 { 148 struct scsibus_softc *sb = (struct scsibus_softc *)self; 149 struct scsibus_attach_args *saa = aux; 150 struct scsi_link *sc_link_proto = saa->saa_sc_link; 151 int nbytes, i; 152 153 if (!cold) 154 scsi_autoconf = 0; 155 156 sc_link_proto->bus = sb; 157 sc_link_proto->scsibus = sb->sc_dev.dv_unit; 158 sb->adapter_link = sc_link_proto; 159 if (sb->adapter_link->adapter_buswidth == 0) 160 sb->adapter_link->adapter_buswidth = 8; 161 sb->sc_buswidth = sb->adapter_link->adapter_buswidth; 162 if (sb->adapter_link->luns == 0) 163 sb->adapter_link->luns = 8; 164 165 printf(": %d targets", sb->sc_buswidth); 166 if (sb->adapter_link->adapter_target < sb->sc_buswidth) 167 printf(", initiator %d", sb->adapter_link->adapter_target); 168 printf("\n"); 169 170 /* Initialize shared data. */ 171 scsi_init(); 172 173 nbytes = sb->sc_buswidth * sizeof(struct scsi_link **); 174 sb->sc_link = malloc(nbytes, M_DEVBUF, M_NOWAIT); 175 if (sb->sc_link == NULL) 176 panic("scsibusattach: can't allocate target links"); 177 nbytes = sb->adapter_link->luns * sizeof(struct scsi_link *); 178 for (i = 0; i < sb->sc_buswidth; i++) { 179 sb->sc_link[i] = malloc(nbytes, M_DEVBUF, M_NOWAIT | M_ZERO); 180 if (sb->sc_link[i] == NULL) 181 panic("scsibusattach: can't allocate lun links"); 182 } 183 184 #if NBIO > 0 185 if (bio_register(&sb->sc_dev, scsibus_bioctl) != 0) 186 printf("%s: unable to register bio\n", sb->sc_dev.dv_xname); 187 #endif 188 189 scsi_probe_bus(sb); 190 } 191 192 int 193 scsibusactivate(struct device *dev, enum devact act) 194 { 195 return (config_activate_children(dev, act)); 196 } 197 198 int 199 scsibusdetach(struct device *dev, int type) 200 { 201 struct scsibus_softc *sb = (struct scsibus_softc *)dev; 202 int i, j, error; 203 204 #if NBIO > 0 205 bio_unregister(&sb->sc_dev); 206 #endif 207 208 if ((error = config_detach_children(dev, type)) != 0) 209 return (error); 210 211 for (i = 0; i < sb->sc_buswidth; i++) { 212 if (sb->sc_link[i] != NULL) { 213 for (j = 0; j < sb->adapter_link->luns; j++) { 214 if (sb->sc_link[i][j] != NULL) 215 free(sb->sc_link[i][j], M_DEVBUF); 216 } 217 free(sb->sc_link[i], M_DEVBUF); 218 } 219 } 220 221 free(sb->sc_link, M_DEVBUF); 222 223 /* Free shared data. */ 224 scsi_deinit(); 225 226 return (0); 227 } 228 229 int 230 scsibussubmatch(struct device *parent, void *match, void *aux) 231 { 232 struct cfdata *cf = match; 233 struct scsi_attach_args *sa = aux; 234 struct scsi_link *sc_link = sa->sa_sc_link; 235 236 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != sc_link->target) 237 return (0); 238 if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != sc_link->lun) 239 return (0); 240 241 return ((*cf->cf_attach->ca_match)(parent, match, aux)); 242 } 243 244 #if NBIO > 0 245 int 246 scsibus_bioctl(struct device *dev, u_long cmd, caddr_t addr) 247 { 248 struct scsibus_softc *sc = (struct scsibus_softc *)dev; 249 struct sbioc_device *sdev; 250 251 switch (cmd) { 252 case SBIOCPROBE: 253 sdev = (struct sbioc_device *)addr; 254 255 if (sdev->sd_target == -1 && sdev->sd_lun == -1) 256 return (scsi_probe_bus(sc)); 257 258 /* specific lun and wildcard target is bad */ 259 if (sdev->sd_target == -1) 260 return (EINVAL); 261 262 if (sdev->sd_lun == -1) 263 return (scsi_probe_target(sc, sdev->sd_target)); 264 265 return (scsi_probe_lun(sc, sdev->sd_target, sdev->sd_lun)); 266 267 case SBIOCDETACH: 268 sdev = (struct sbioc_device *)addr; 269 270 if (sdev->sd_target == -1) 271 return (EINVAL); 272 273 if (sdev->sd_lun == -1) 274 return (scsi_detach_target(sc, sdev->sd_target, 0)); 275 276 return (scsi_detach_lun(sc, sdev->sd_target, sdev->sd_lun, 0)); 277 278 default: 279 return (ENOTTY); 280 } 281 } 282 #endif 283 284 int 285 scsi_probe_bus(struct scsibus_softc *sc) 286 { 287 struct scsi_link *alink = sc->adapter_link; 288 int i; 289 290 for (i = 0; i < alink->adapter_buswidth; i++) 291 scsi_probe_target(sc, i); 292 293 return (0); 294 } 295 296 int 297 scsi_probe_target(struct scsibus_softc *sc, int target) 298 { 299 struct scsi_link *alink = sc->adapter_link; 300 struct scsi_link *link; 301 struct scsi_report_luns_data *report; 302 int i, nluns, lun; 303 304 if (scsi_probe_lun(sc, target, 0) == EINVAL) 305 return (EINVAL); 306 307 link = sc->sc_link[target][0]; 308 if (link == NULL) 309 return (ENXIO); 310 311 if ((link->flags & (SDEV_UMASS | SDEV_ATAPI)) == 0 && 312 SCSISPC(link->inqdata.version) > 2) { 313 report = malloc(sizeof(*report), M_TEMP, M_WAITOK); 314 if (report == NULL) 315 goto dumbscan; 316 317 if (scsi_report_luns(link, REPORT_NORMAL, report, 318 sizeof(*report), scsi_autoconf | SCSI_SILENT | 319 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY | 320 SCSI_IGNORE_MEDIA_CHANGE, 10000) != 0) { 321 free(report, M_TEMP); 322 goto dumbscan; 323 } 324 325 /* 326 * XXX In theory we should check if data is full, which 327 * would indicate it needs to be enlarged and REPORT 328 * LUNS tried again. Solaris tries up to 3 times with 329 * larger sizes for data. 330 */ 331 nluns = _4btol(report->length) / RPL_LUNDATA_SIZE; 332 for (i = 0; i < nluns; i++) { 333 if (report->luns[i].lundata[0] != 0) 334 continue; 335 lun = report->luns[i].lundata[RPL_LUNDATA_T0LUN]; 336 if (lun == 0) 337 continue; 338 339 /* Probe the provided LUN. Don't check LUN 0. */ 340 sc->sc_link[target][0] = NULL; 341 scsi_probe_lun(sc, target, lun); 342 sc->sc_link[target][0] = link; 343 } 344 345 free(report, M_TEMP); 346 return (0); 347 } 348 349 dumbscan: 350 for (i = 1; i < alink->luns; i++) { 351 if (scsi_probe_lun(sc, target, i) == EINVAL) 352 break; 353 } 354 355 return (0); 356 } 357 358 int 359 scsi_probe_lun(struct scsibus_softc *sc, int target, int lun) 360 { 361 struct scsi_link *alink = sc->adapter_link; 362 363 if (target < 0 || target >= alink->adapter_buswidth || 364 target == alink->adapter_target || 365 lun < 0 || lun >= alink->luns) 366 return (ENXIO); 367 368 return (scsi_probedev(sc, target, lun)); 369 } 370 371 int 372 scsi_detach_bus(struct scsibus_softc *sc, int flags) 373 { 374 struct scsi_link *alink = sc->adapter_link; 375 int i; 376 377 for (i = 0; i < alink->adapter_buswidth; i++) 378 scsi_detach_target(sc, i, flags); 379 380 return (0); 381 } 382 383 int 384 scsi_detach_target(struct scsibus_softc *sc, int target, int flags) 385 { 386 struct scsi_link *alink = sc->adapter_link; 387 int i, err, rv = 0, detached = 0; 388 389 if (target < 0 || target >= alink->adapter_buswidth || 390 target == alink->adapter_target) 391 return (ENXIO); 392 393 if (sc->sc_link[target] == NULL) 394 return (ENXIO); 395 396 for (i = 0; i < alink->luns; i++) { /* nicer backwards? */ 397 if (sc->sc_link[target][i] == NULL) 398 continue; 399 400 err = scsi_detach_lun(sc, target, i, flags); 401 if (err != 0) 402 rv = err; 403 detached = 1; 404 } 405 406 return (detached ? rv : ENXIO); 407 } 408 409 int 410 scsi_detach_lun(struct scsibus_softc *sc, int target, int lun, int flags) 411 { 412 struct scsi_link *alink = sc->adapter_link; 413 struct scsi_link *link; 414 int rv; 415 416 if (target < 0 || target >= alink->adapter_buswidth || 417 target == alink->adapter_target || 418 lun < 0 || lun >= alink->luns) 419 return (ENXIO); 420 421 if (sc->sc_link[target] == NULL) 422 return (ENXIO); 423 424 link = sc->sc_link[target][lun]; 425 if (link == NULL) 426 return (ENXIO); 427 428 if (((flags & DETACH_FORCE) == 0) && (link->flags & SDEV_OPEN)) 429 return (EBUSY); 430 431 /* detaching a device from scsibus is a three step process... */ 432 433 /* 1. detach the device */ 434 rv = config_detach(link->device_softc, flags); 435 if (rv != 0) 436 return (rv); 437 438 /* 2. free up its state in the adapter */ 439 if (alink->adapter->dev_free != NULL) 440 alink->adapter->dev_free(link); 441 442 /* 3. free up its state in the midlayer */ 443 free(link, M_DEVBUF); 444 sc->sc_link[target][lun] = NULL; 445 446 return (0); 447 } 448 449 void 450 scsi_strvis(u_char *dst, u_char *src, int len) 451 { 452 u_char last; 453 454 /* Trim leading and trailing whitespace and NULs. */ 455 while (len > 0 && (src[0] == ' ' || src[0] == '\t' || src[0] == '\n' || 456 src[0] == '\0' || src[0] == 0xff)) 457 ++src, --len; 458 while (len > 0 && (src[len-1] == ' ' || src[len-1] == '\t' || 459 src[len-1] == '\n' || src[len-1] == '\0' || src[len-1] == 0xff)) 460 --len; 461 462 last = 0xff; 463 while (len > 0) { 464 switch (*src) { 465 case ' ': 466 case '\t': 467 case '\n': 468 case '\0': 469 case 0xff: 470 /* collapse whitespace and NULs to a single space */ 471 if (last != ' ') 472 *dst++ = ' '; 473 last = ' '; 474 break; 475 case '\\': 476 /* quote characters */ 477 *dst++ = '\\'; 478 *dst++ = '\\'; 479 last = '\\'; 480 break; 481 default: 482 if (*src < 0x20 || *src >= 0x80) { 483 /* non-printable characters */ 484 *dst++ = '\\'; 485 *dst++ = ((*src & 0300) >> 6) + '0'; 486 *dst++ = ((*src & 0070) >> 3) + '0'; 487 *dst++ = ((*src & 0007) >> 0) + '0'; 488 } else { 489 /* normal characters */ 490 *dst++ = *src; 491 } 492 last = *src; 493 break; 494 } 495 ++src, --len; 496 } 497 498 *dst++ = 0; 499 } 500 501 struct scsi_quirk_inquiry_pattern { 502 struct scsi_inquiry_pattern pattern; 503 u_int16_t quirks; 504 }; 505 506 const struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = { 507 {{T_CDROM, T_REMOV, 508 "PLEXTOR", "CD-ROM PX-40TS", "1.01"}, SDEV_NOSYNC}, 509 510 {{T_DIRECT, T_FIXED, 511 "MICROP ", "1588-15MBSUN0669", ""}, SDEV_AUTOSAVE}, 512 {{T_DIRECT, T_FIXED, 513 "DEC ", "RZ55 (C) DEC", ""}, SDEV_AUTOSAVE}, 514 {{T_DIRECT, T_FIXED, 515 "EMULEX ", "MD21/S2 ESDI", "A00"}, SDEV_AUTOSAVE}, 516 {{T_DIRECT, T_FIXED, 517 "IBMRAID ", "0662S", ""}, SDEV_AUTOSAVE}, 518 {{T_DIRECT, T_FIXED, 519 "IBM ", "0663H", ""}, SDEV_AUTOSAVE}, 520 {{T_DIRECT, T_FIXED, 521 "IBM", "0664", ""}, SDEV_AUTOSAVE}, 522 {{T_DIRECT, T_FIXED, 523 "IBM ", "H3171-S2", ""}, SDEV_AUTOSAVE}, 524 {{T_DIRECT, T_FIXED, 525 "IBM ", "KZ-C", ""}, SDEV_AUTOSAVE}, 526 /* Broken IBM disk */ 527 {{T_DIRECT, T_FIXED, 528 "" , "DFRSS2F", ""}, SDEV_AUTOSAVE}, 529 {{T_DIRECT, T_FIXED, 530 "QUANTUM ", "ELS85S ", ""}, SDEV_AUTOSAVE}, 531 {{T_DIRECT, T_REMOV, 532 "iomega", "jaz 1GB", ""}, SDEV_NOTAGS}, 533 {{T_DIRECT, T_FIXED, 534 "MICROP", "4421-07", ""}, SDEV_NOTAGS}, 535 {{T_DIRECT, T_FIXED, 536 "SEAGATE", "ST150176LW", "0002"}, SDEV_NOTAGS}, 537 {{T_DIRECT, T_FIXED, 538 "HP", "C3725S", ""}, SDEV_NOTAGS}, 539 {{T_DIRECT, T_FIXED, 540 "IBM", "DCAS", ""}, SDEV_NOTAGS}, 541 542 {{T_SEQUENTIAL, T_REMOV, 543 "SONY ", "SDT-5000 ", "3."}, SDEV_NOSYNC|SDEV_NOWIDE}, 544 {{T_SEQUENTIAL, T_REMOV, 545 "WangDAT ", "Model 1300 ", "02.4"}, SDEV_NOSYNC|SDEV_NOWIDE}, 546 {{T_SEQUENTIAL, T_REMOV, 547 "WangDAT ", "Model 2600 ", "01.7"}, SDEV_NOSYNC|SDEV_NOWIDE}, 548 {{T_SEQUENTIAL, T_REMOV, 549 "WangDAT ", "Model 3200 ", "02.2"}, SDEV_NOSYNC|SDEV_NOWIDE}, 550 551 /* ATAPI device quirks */ 552 {{T_CDROM, T_REMOV, 553 "CR-2801TE", "", "1.07"}, ADEV_NOSENSE}, 554 {{T_CDROM, T_REMOV, 555 "CREATIVECD3630E", "", "AC101"}, ADEV_NOSENSE}, 556 {{T_CDROM, T_REMOV, 557 "FX320S", "", "q01"}, ADEV_NOSENSE}, 558 {{T_CDROM, T_REMOV, 559 "GCD-R580B", "", "1.00"}, ADEV_LITTLETOC}, 560 {{T_CDROM, T_REMOV, 561 "MATSHITA CR-574", "", "1.02"}, ADEV_NOCAPACITY}, 562 {{T_CDROM, T_REMOV, 563 "MATSHITA CR-574", "", "1.06"}, ADEV_NOCAPACITY}, 564 {{T_CDROM, T_REMOV, 565 "Memorex CRW-2642", "", "1.0g"}, ADEV_NOSENSE}, 566 {{T_CDROM, T_REMOV, 567 "SANYO CRD-256P", "", "1.02"}, ADEV_NOCAPACITY}, 568 {{T_CDROM, T_REMOV, 569 "SANYO CRD-254P", "", "1.02"}, ADEV_NOCAPACITY}, 570 {{T_CDROM, T_REMOV, 571 "SANYO CRD-S54P", "", "1.08"}, ADEV_NOCAPACITY}, 572 {{T_CDROM, T_REMOV, 573 "CD-ROM CDR-S1", "", "1.70"}, ADEV_NOCAPACITY}, /* Sanyo */ 574 {{T_CDROM, T_REMOV, 575 "CD-ROM CDR-N16", "", "1.25"}, ADEV_NOCAPACITY}, /* Sanyo */ 576 {{T_CDROM, T_REMOV, 577 "UJDCD8730", "", "1.14"}, ADEV_NODOORLOCK}, /* Acer */ 578 }; 579 580 581 /* 582 * Print out autoconfiguration information for a subdevice. 583 * 584 * This is a slight abuse of 'standard' autoconfiguration semantics, 585 * because 'print' functions don't normally print the colon and 586 * device information. However, in this case that's better than 587 * either printing redundant information before the attach message, 588 * or having the device driver call a special function to print out 589 * the standard device information. 590 */ 591 int 592 scsibusprint(void *aux, const char *pnp) 593 { 594 struct scsi_attach_args *sa = aux; 595 struct scsi_inquiry_data *inqbuf; 596 u_int8_t type; 597 int removable; 598 char *dtype, *qtype; 599 char vendor[33], product[65], revision[17]; 600 int target, lun; 601 602 if (pnp != NULL) 603 printf("%s", pnp); 604 605 inqbuf = sa->sa_inqbuf; 606 607 target = sa->sa_sc_link->target; 608 lun = sa->sa_sc_link->lun; 609 610 type = inqbuf->device & SID_TYPE; 611 removable = inqbuf->dev_qual2 & SID_REMOVABLE ? 1 : 0; 612 613 /* 614 * Figure out basic device type and qualifier. 615 */ 616 dtype = 0; 617 switch (inqbuf->device & SID_QUAL) { 618 case SID_QUAL_LU_OK: 619 qtype = ""; 620 break; 621 622 case SID_QUAL_LU_OFFLINE: 623 qtype = " offline"; 624 break; 625 626 case SID_QUAL_RSVD: 627 panic("scsibusprint: qualifier == SID_QUAL_RSVD"); 628 629 case SID_QUAL_BAD_LU: 630 panic("scsibusprint: qualifier == SID_QUAL_BAD_LU"); 631 632 default: 633 qtype = ""; 634 dtype = "vendor-unique"; 635 break; 636 } 637 if (dtype == 0) { 638 switch (type) { 639 case T_DIRECT: 640 dtype = "direct"; 641 break; 642 case T_SEQUENTIAL: 643 dtype = "sequential"; 644 break; 645 case T_PRINTER: 646 dtype = "printer"; 647 break; 648 case T_PROCESSOR: 649 dtype = "processor"; 650 break; 651 case T_CDROM: 652 dtype = "cdrom"; 653 break; 654 case T_WORM: 655 dtype = "worm"; 656 break; 657 case T_SCANNER: 658 dtype = "scanner"; 659 break; 660 case T_OPTICAL: 661 dtype = "optical"; 662 break; 663 case T_CHANGER: 664 dtype = "changer"; 665 break; 666 case T_COMM: 667 dtype = "communication"; 668 break; 669 case T_ENCLOSURE: 670 dtype = "enclosure services"; 671 break; 672 case T_RDIRECT: 673 dtype = "simplified direct"; 674 break; 675 case T_NODEVICE: 676 panic("scsibusprint: device type T_NODEVICE"); 677 default: 678 dtype = "unknown"; 679 break; 680 } 681 } 682 683 scsi_strvis(vendor, inqbuf->vendor, 8); 684 scsi_strvis(product, inqbuf->product, 16); 685 scsi_strvis(revision, inqbuf->revision, 4); 686 687 printf(" targ %d lun %d: <%s, %s, %s> ", target, lun, vendor, product, 688 revision); 689 if (sa->sa_sc_link->flags & SDEV_ATAPI) 690 printf("ATAPI"); 691 else 692 printf("SCSI%d", SCSISPC(inqbuf->version)); 693 printf(" %d/%s %s%s", type, dtype, removable ? "removable" : "fixed", 694 qtype); 695 696 return (UNCONF); 697 } 698 699 /* 700 * Given a target and lun, ask the device what it is, and find the correct 701 * driver table entry. 702 * 703 * Return 0 if further LUNs are possible, EINVAL if not. 704 */ 705 int 706 scsi_probedev(struct scsibus_softc *scsi, int target, int lun) 707 { 708 const struct scsi_quirk_inquiry_pattern *finger; 709 struct scsi_inquiry_data *inqbuf; 710 struct scsi_attach_args sa; 711 struct scsi_link *sc_link; 712 struct cfdata *cf; 713 int priority, rslt = 0; 714 715 /* Skip this slot if it is already attached and try the next LUN. */ 716 if (scsi->sc_link[target][lun] != NULL) 717 return (0); 718 719 sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT); 720 if (sc_link == NULL) 721 return (EINVAL); 722 723 *sc_link = *scsi->adapter_link; 724 sc_link->target = target; 725 sc_link->lun = lun; 726 sc_link->device = &probe_switch; 727 inqbuf = &sc_link->inqdata; 728 729 SC_DEBUG(sc_link, SDEV_DB2, ("scsi_link created.\n")); 730 731 /* ask the adapter if this will be a valid device */ 732 if (scsi->adapter_link->adapter->dev_probe != NULL && 733 scsi->adapter_link->adapter->dev_probe(sc_link) != 0) { 734 if (lun == 0) 735 rslt = EINVAL; 736 goto free; 737 } 738 739 /* 740 * Tell drivers that are paying attention to avoid sync/wide/tags until 741 * INQUIRY data has been processed and the quirks information is 742 * complete. Some drivers set bits in quirks before we get here, so 743 * just add NOTAGS, NOWIDE and NOSYNC. 744 */ 745 sc_link->quirks |= SDEV_NOSYNC | SDEV_NOWIDE | SDEV_NOTAGS; 746 747 /* 748 * Ask the device what it is 749 */ 750 #ifdef SCSIDEBUG 751 if (((1 << sc_link->scsibus) & scsidebug_buses) && 752 ((target < 32) && ((1 << target) & scsidebug_targets)) && 753 ((lun < 32) && ((1 << lun) & scsidebug_luns))) 754 sc_link->flags |= scsidebug_level; 755 #endif /* SCSIDEBUG */ 756 757 if (lun == 0) { 758 /* Clear any outstanding errors. */ 759 scsi_test_unit_ready(sc_link, TEST_READY_RETRIES, 760 scsi_autoconf | SCSI_IGNORE_ILLEGAL_REQUEST | 761 SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE); 762 } 763 764 /* Now go ask the device all about itself. */ 765 rslt = scsi_inquire(sc_link, inqbuf, scsi_autoconf | SCSI_SILENT); 766 if (rslt != 0) { 767 SC_DEBUG(sc_link, SDEV_DB2, ("Bad LUN. rslt = %i\n", rslt)); 768 if (lun == 0) 769 rslt = EINVAL; 770 goto bad; 771 } 772 773 switch (inqbuf->device & SID_QUAL) { 774 case SID_QUAL_RSVD: 775 case SID_QUAL_BAD_LU: 776 case SID_QUAL_LU_OFFLINE: 777 SC_DEBUG(sc_link, SDEV_DB1, ("Bad LUN. SID_QUAL = 0x%02x\n", 778 inqbuf->device & SID_QUAL)); 779 goto bad; 780 781 case SID_QUAL_LU_OK: 782 if ((inqbuf->device & SID_TYPE) == T_NODEVICE) { 783 SC_DEBUG(sc_link, SDEV_DB1, 784 ("Bad LUN. SID_TYPE = T_NODEVICE\n")); 785 goto bad; 786 } 787 break; 788 789 default: 790 break; 791 } 792 793 scsi_devid(sc_link); 794 795 if (lun == 0 || scsi->sc_link[target][0] == NULL) 796 ; 797 else if (sc_link->flags & SDEV_UMASS) 798 ; 799 else if (sc_link->id.d_type != DEVID_NONE && 800 !DEVID_CMP(&scsi->sc_link[target][0]->id, &sc_link->id)) 801 ; 802 else if (memcmp(inqbuf, &scsi->sc_link[target][0]->inqdata, 803 sizeof(*inqbuf)) == 0) { 804 /* The device doesn't distinguish between LUNs. */ 805 SC_DEBUG(sc_link, SDEV_DB1, ("IDENTIFY not supported.\n")); 806 rslt = EINVAL; 807 goto bad; 808 } 809 810 finger = (const struct scsi_quirk_inquiry_pattern *)scsi_inqmatch( 811 inqbuf, scsi_quirk_patterns, 812 sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]), 813 sizeof(scsi_quirk_patterns[0]), &priority); 814 815 /* 816 * Based upon the inquiry flags we got back, and if we're 817 * at SCSI-2 or better, remove some limiting quirks. 818 */ 819 if (SCSISPC(inqbuf->version) >= 2) { 820 if ((inqbuf->flags & SID_CmdQue) != 0) 821 sc_link->quirks &= ~SDEV_NOTAGS; 822 if ((inqbuf->flags & SID_Sync) != 0) 823 sc_link->quirks &= ~SDEV_NOSYNC; 824 if ((inqbuf->flags & SID_WBus16) != 0) 825 sc_link->quirks &= ~SDEV_NOWIDE; 826 } else 827 /* Older devices do not have SYNCHRONIZE CACHE capability. */ 828 sc_link->quirks |= SDEV_NOSYNCCACHE; 829 830 /* 831 * Now apply any quirks from the table. 832 */ 833 if (priority != 0) 834 sc_link->quirks |= finger->quirks; 835 836 /* 837 * note what BASIC type of device it is 838 */ 839 if ((inqbuf->dev_qual2 & SID_REMOVABLE) != 0) 840 sc_link->flags |= SDEV_REMOVABLE; 841 842 sa.sa_sc_link = sc_link; 843 sa.sa_inqbuf = &sc_link->inqdata; 844 845 if ((cf = config_search(scsibussubmatch, (struct device *)scsi, 846 &sa)) == 0) { 847 scsibusprint(&sa, scsi->sc_dev.dv_xname); 848 printf(" not configured\n"); 849 goto bad; 850 } 851 852 /* 853 * Braindead USB devices, especially some x-in-1 media readers, try to 854 * 'help' by pretending any LUN is actually LUN 0 until they see a 855 * different LUN used in a command. So do an INQUIRY on LUN 1 at this 856 * point to prevent such helpfulness before it causes confusion. 857 */ 858 if (lun == 0 && (sc_link->flags & SDEV_UMASS) && 859 scsi->sc_link[target][1] == NULL && sc_link->luns > 1) { 860 struct scsi_inquiry_data tmpinq; 861 862 sc_link->lun = 1; 863 scsi_inquire(sc_link, &tmpinq, scsi_autoconf | SCSI_SILENT); 864 sc_link->lun = 0; 865 } 866 867 scsi->sc_link[target][lun] = sc_link; 868 869 /* 870 * Generate a TEST_UNIT_READY command. This gives drivers waiting for 871 * valid quirks data a chance to set wide/sync/tag options 872 * appropriately. It also clears any outstanding ACA conditions that 873 * INQUIRY may leave behind. 874 * 875 * Do this now so that any messages generated by config_attach() do not 876 * have negotiation messages inserted into their midst. 877 */ 878 scsi_test_unit_ready(sc_link, TEST_READY_RETRIES, 879 scsi_autoconf | SCSI_IGNORE_ILLEGAL_REQUEST | 880 SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE); 881 882 config_attach((struct device *)scsi, cf, &sa, scsibusprint); 883 884 return (0); 885 886 bad: 887 if (scsi->adapter_link->adapter->dev_free != NULL) 888 scsi->adapter_link->adapter->dev_free(sc_link); 889 free: 890 free(sc_link, M_DEVBUF); 891 return (rslt); 892 } 893 894 /* 895 * Return a priority based on how much of the inquiry data matches 896 * the patterns for the particular driver. 897 */ 898 const void * 899 scsi_inqmatch(struct scsi_inquiry_data *inqbuf, const void *_base, 900 int nmatches, int matchsize, int *bestpriority) 901 { 902 u_int8_t type; 903 int removable; 904 const void *bestmatch; 905 const unsigned char *base = (const unsigned char *)_base; 906 907 /* Include the qualifier to catch vendor-unique types. */ 908 type = inqbuf->device; 909 removable = inqbuf->dev_qual2 & SID_REMOVABLE ? T_REMOV : T_FIXED; 910 911 for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) { 912 struct scsi_inquiry_pattern *match = (void *)base; 913 int priority, len; 914 915 if (type != match->type) 916 continue; 917 if (removable != match->removable) 918 continue; 919 priority = 2; 920 len = strlen(match->vendor); 921 if (bcmp(inqbuf->vendor, match->vendor, len)) 922 continue; 923 priority += len; 924 len = strlen(match->product); 925 if (bcmp(inqbuf->product, match->product, len)) 926 continue; 927 priority += len; 928 len = strlen(match->revision); 929 if (bcmp(inqbuf->revision, match->revision, len)) 930 continue; 931 priority += len; 932 933 #if SCSIDEBUG 934 printf("scsi_inqmatch: %d/%d/%d <%s, %s, %s>\n", 935 priority, match->type, match->removable, 936 match->vendor, match->product, match->revision); 937 #endif 938 if (priority > *bestpriority) { 939 *bestpriority = priority; 940 bestmatch = base; 941 } 942 } 943 944 return (bestmatch); 945 } 946 947 void 948 scsi_devid(struct scsi_link *link) 949 { 950 struct { 951 struct scsi_vpd_hdr hdr; 952 u_int8_t list[32]; 953 } __packed pg; 954 int pg80 = 0, pg83 = 0, i; 955 956 if (SCSISPC(link->inqdata.version) >= 2) { 957 if (scsi_inquire_vpd(link, &pg, sizeof(pg), SI_PG_SUPPORTED, 958 scsi_autoconf) != 0) 959 return; 960 961 for (i = 0; i < MIN(sizeof(pg.list), pg.hdr.page_length); i++) { 962 switch (pg.list[i]) { 963 case SI_PG_SERIAL: 964 pg80 = 1; 965 break; 966 case SI_PG_DEVID: 967 pg83 = 1; 968 break; 969 } 970 } 971 972 if (pg83 && scsi_devid_pg83(link) == 0) 973 return; 974 #ifdef notyet 975 if (pg80 && scsi_devid_pg80(link) == 0) 976 return; 977 #endif 978 } 979 } 980 981 int 982 scsi_devid_pg83(struct scsi_link *link) 983 { 984 struct scsi_vpd_hdr hdr; 985 struct scsi_vpd_devid_hdr dhdr; 986 u_int8_t *pg, *id; 987 int type, idtype = 0, idlen; 988 int len, pos; 989 int rv; 990 991 rv = scsi_inquire_vpd(link, &hdr, sizeof(hdr), SI_PG_DEVID, 992 scsi_autoconf); 993 if (rv != 0) 994 return (rv); 995 996 len = sizeof(hdr) + hdr.page_length; 997 pg = malloc(len, M_TEMP, M_WAITOK); 998 999 rv = scsi_inquire_vpd(link, pg, len, SI_PG_DEVID, scsi_autoconf); 1000 if (rv != 0) 1001 goto err; 1002 1003 pos = sizeof(hdr); 1004 1005 do { 1006 if (len - pos < sizeof(dhdr)) { 1007 rv = EIO; 1008 goto err; 1009 } 1010 memcpy(&dhdr, &pg[pos], sizeof(dhdr)); 1011 pos += sizeof(dhdr); 1012 if (len - pos < dhdr.len) { 1013 rv = EIO; 1014 goto err; 1015 } 1016 1017 if (VPD_DEVID_ASSOC(dhdr.flags) == VPD_DEVID_ASSOC_LU) { 1018 type = VPD_DEVID_TYPE(dhdr.flags); 1019 switch (type) { 1020 case VPD_DEVID_TYPE_NAA: 1021 case VPD_DEVID_TYPE_EUI64: 1022 case VPD_DEVID_TYPE_T10: 1023 if (type >= idtype) { 1024 idtype = type; 1025 idlen = dhdr.len; 1026 id = &pg[pos]; 1027 } 1028 break; 1029 1030 default: 1031 /* skip */ 1032 break; 1033 } 1034 } 1035 1036 pos += dhdr.len; 1037 } while (idtype != VPD_DEVID_TYPE_NAA && len != pos); 1038 1039 if (idtype > 0) { 1040 link->id.d_id = malloc(idlen, M_DEVBUF, M_WAITOK); 1041 1042 switch (idtype) { 1043 case VPD_DEVID_TYPE_NAA: 1044 link->id.d_type = DEVID_NAA; 1045 break; 1046 case VPD_DEVID_TYPE_EUI64: 1047 link->id.d_type = DEVID_EUI; 1048 break; 1049 case VPD_DEVID_TYPE_T10: 1050 link->id.d_type = DEVID_T10; 1051 break; 1052 } 1053 link->id.d_len = idlen; 1054 memcpy(link->id.d_id, id, idlen); 1055 } else 1056 rv = ENODEV; 1057 1058 err: 1059 free(pg, M_TEMP); 1060 return (rv); 1061 } 1062 1063 /* 1064 * scsi_minphys member of struct scsi_adapter for drivers which don't 1065 * need any specific routine. 1066 */ 1067 void 1068 scsi_minphys(struct buf *bp, struct scsi_link *sl) 1069 { 1070 minphys(bp); 1071 } 1072