xref: /openbsd/sys/scsi/scsiconf.c (revision 771fbea0)
1 /*	$OpenBSD: scsiconf.c,v 1.237 2020/11/19 13:45:15 krw 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 #include "mpath.h"
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/malloc.h>
56 #include <sys/pool.h>
57 #include <sys/device.h>
58 #include <sys/buf.h>
59 #include <sys/atomic.h>
60 
61 #include <scsi/scsi_all.h>
62 #include <scsi/scsi_debug.h>
63 #include <scsi/scsiconf.h>
64 
65 int	scsibusmatch(struct device *, void *, void *);
66 void	scsibusattach(struct device *, struct device *, void *);
67 int	scsibusactivate(struct device *, int);
68 int	scsibusdetach(struct device *, int);
69 int	scsibussubmatch(struct device *, void *, void *);
70 int	scsibussubprint(void *, const char *);
71 #if NBIO > 0
72 #include <sys/ioctl.h>
73 #include <sys/scsiio.h>
74 #include <dev/biovar.h>
75 int	scsibusbioctl(struct device *, u_long, caddr_t);
76 #endif /* NBIO > 0 */
77 
78 void	scsi_get_target_luns(struct scsi_link *, struct scsi_lun_array *);
79 void	scsi_add_link(struct scsi_link *);
80 void	scsi_remove_link(struct scsi_link *);
81 void	scsi_print_link(struct scsi_link *);
82 int	scsi_probe_link(struct scsibus_softc *, int, int, int);
83 int	scsi_activate_link(struct scsi_link *, int);
84 int	scsi_detach_link(struct scsi_link *, int);
85 int	scsi_detach_bus(struct scsibus_softc *, int);
86 
87 void	scsi_devid(struct scsi_link *);
88 int	scsi_devid_pg80(struct scsi_link *);
89 int	scsi_devid_pg83(struct scsi_link *);
90 int	scsi_devid_wwn(struct scsi_link *);
91 
92 int	scsi_activate_bus(struct scsibus_softc *, int);
93 int	scsi_activate_target(struct scsibus_softc *, int, int);
94 int	scsi_activate_lun(struct scsibus_softc *, int, int, int);
95 
96 int	scsi_autoconf = SCSI_AUTOCONF;
97 
98 struct cfattach scsibus_ca = {
99 	sizeof(struct scsibus_softc), scsibusmatch, scsibusattach,
100 	scsibusdetach, scsibusactivate
101 };
102 
103 struct cfdriver scsibus_cd = {
104 	NULL, "scsibus", DV_DULL
105 };
106 
107 struct scsi_quirk_inquiry_pattern {
108 	struct scsi_inquiry_pattern	pattern;
109 	u_int16_t			quirks;
110 };
111 
112 const struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
113 	{{T_CDROM, T_REMOV,
114 	 "PLEXTOR", "CD-ROM PX-40TS", "1.01"},    SDEV_NOSYNC},
115 
116 	{{T_DIRECT, T_FIXED,
117 	 "MICROP  ", "1588-15MBSUN0669", ""},     SDEV_AUTOSAVE},
118 	{{T_DIRECT, T_FIXED,
119 	 "DEC     ", "RZ55     (C) DEC", ""},     SDEV_AUTOSAVE},
120 	{{T_DIRECT, T_FIXED,
121 	 "EMULEX  ", "MD21/S2     ESDI", "A00"},  SDEV_AUTOSAVE},
122 	{{T_DIRECT, T_FIXED,
123 	 "IBMRAID ", "0662S",            ""},     SDEV_AUTOSAVE},
124 	{{T_DIRECT, T_FIXED,
125 	 "IBM     ", "0663H",            ""},     SDEV_AUTOSAVE},
126 	{{T_DIRECT, T_FIXED,
127 	 "IBM",	  "0664",		 ""},	  SDEV_AUTOSAVE},
128 	{{T_DIRECT, T_FIXED,
129 	 "IBM     ", "H3171-S2",         ""},	  SDEV_AUTOSAVE},
130 	{{T_DIRECT, T_FIXED,
131 	 "IBM     ", "KZ-C",		 ""},	  SDEV_AUTOSAVE},
132 	/* Broken IBM disk */
133 	{{T_DIRECT, T_FIXED,
134 	 ""	   , "DFRSS2F",		 ""},	  SDEV_AUTOSAVE},
135 	{{T_DIRECT, T_FIXED,
136 	 "QUANTUM ", "ELS85S          ", ""},	  SDEV_AUTOSAVE},
137 	{{T_DIRECT, T_REMOV,
138 	 "iomega", "jaz 1GB",		 ""},	  SDEV_NOTAGS},
139 	{{T_DIRECT, T_FIXED,
140 	 "MICROP", "4421-07",		 ""},     SDEV_NOTAGS},
141 	{{T_DIRECT, T_FIXED,
142 	 "SEAGATE", "ST150176LW",        "0002"}, SDEV_NOTAGS},
143 	{{T_DIRECT, T_FIXED,
144 	 "HP", "C3725S",		 ""},     SDEV_NOTAGS},
145 	{{T_DIRECT, T_FIXED,
146 	 "IBM", "DCAS",			 ""},     SDEV_NOTAGS},
147 
148 	{{T_SEQUENTIAL, T_REMOV,
149 	 "SONY    ", "SDT-5000        ", "3."},   SDEV_NOSYNC|SDEV_NOWIDE},
150 	{{T_SEQUENTIAL, T_REMOV,
151 	 "WangDAT ", "Model 1300      ", "02.4"}, SDEV_NOSYNC|SDEV_NOWIDE},
152 	{{T_SEQUENTIAL, T_REMOV,
153 	 "WangDAT ", "Model 2600      ", "01.7"}, SDEV_NOSYNC|SDEV_NOWIDE},
154 	{{T_SEQUENTIAL, T_REMOV,
155 	 "WangDAT ", "Model 3200      ", "02.2"}, SDEV_NOSYNC|SDEV_NOWIDE},
156 
157 	/* ATAPI device quirks */
158 	{{T_CDROM, T_REMOV,
159 	 "CR-2801TE", "", "1.07"},              ADEV_NOSENSE},
160 	{{T_CDROM, T_REMOV,
161 	 "CREATIVECD3630E", "", "AC101"},       ADEV_NOSENSE},
162 	{{T_CDROM, T_REMOV,
163 	 "FX320S", "", "q01"},                  ADEV_NOSENSE},
164 	{{T_CDROM, T_REMOV,
165 	 "GCD-R580B", "", "1.00"},              ADEV_LITTLETOC},
166 	{{T_CDROM, T_REMOV,
167 	 "MATSHITA CR-574", "", "1.02"},        ADEV_NOCAPACITY},
168 	{{T_CDROM, T_REMOV,
169 	 "MATSHITA CR-574", "", "1.06"},        ADEV_NOCAPACITY},
170 	{{T_CDROM, T_REMOV,
171 	 "Memorex CRW-2642", "", "1.0g"},       ADEV_NOSENSE},
172 	{{T_CDROM, T_REMOV,
173 	 "SANYO CRD-256P", "", "1.02"},         ADEV_NOCAPACITY},
174 	{{T_CDROM, T_REMOV,
175 	 "SANYO CRD-254P", "", "1.02"},         ADEV_NOCAPACITY},
176 	{{T_CDROM, T_REMOV,
177 	 "SANYO CRD-S54P", "", "1.08"},         ADEV_NOCAPACITY},
178 	{{T_CDROM, T_REMOV,
179 	 "CD-ROM  CDR-S1", "", "1.70"},         ADEV_NOCAPACITY}, /* Sanyo */
180 	{{T_CDROM, T_REMOV,
181 	 "CD-ROM  CDR-N16", "", "1.25"},        ADEV_NOCAPACITY}, /* Sanyo */
182 	{{T_CDROM, T_REMOV,
183 	 "UJDCD8730", "", "1.14"},              ADEV_NODOORLOCK}, /* Acer */
184 };
185 
186 int
187 scsiprint(void *aux, const char *pnp)
188 {
189 	/* Only "scsibus"es can attach to "scsi"s. */
190 	if (pnp)
191 		printf("scsibus at %s", pnp);
192 
193 	return UNCONF;
194 }
195 
196 int
197 scsibusmatch(struct device *parent, void *match, void *aux)
198 {
199 	return 1;
200 }
201 
202 /*
203  * The routine called by the adapter boards to get all their
204  * devices configured in.
205  */
206 void
207 scsibusattach(struct device *parent, struct device *self, void *aux)
208 {
209 	struct scsibus_softc		*sb = (struct scsibus_softc *)self;
210 	struct scsibus_attach_args	*saa = aux;
211 
212 	if (!cold)
213 		scsi_autoconf = 0;
214 
215 	SLIST_INIT(&sb->sc_link_list);
216 	sb->sb_adapter_softc = saa->saa_adapter_softc;
217 	sb->sb_adapter = saa->saa_adapter;
218 	sb->sb_pool = saa->saa_pool;
219 	sb->sb_quirks = saa->saa_quirks;
220 	sb->sb_flags = saa->saa_flags;
221 	sb->sb_openings = saa->saa_openings;
222 	sb->sb_adapter_buswidth = saa->saa_adapter_buswidth;
223 	sb->sb_adapter_target = saa->saa_adapter_target;
224 	sb->sb_luns = saa->saa_luns;
225 
226 	if (sb->sb_adapter_buswidth == 0)
227 		sb->sb_adapter_buswidth = 8;
228 	if (sb->sb_luns == 0)
229 		sb->sb_luns = 8;
230 
231 	printf(": %d targets", sb->sb_adapter_buswidth);
232 	if (sb->sb_adapter_target < sb->sb_adapter_buswidth)
233 		printf(", initiator %d", sb->sb_adapter_target);
234 	if (saa->saa_wwpn != 0x0 && saa->saa_wwnn != 0x0) {
235 		printf(", WWPN %016llx, WWNN %016llx", saa->saa_wwpn,
236 		    saa->saa_wwnn);
237 	}
238 	printf("\n");
239 
240 	/* Initialize shared data. */
241 	scsi_init();
242 
243 	SLIST_INIT(&sb->sc_link_list);
244 
245 #if NBIO > 0
246 	if (bio_register(&sb->sc_dev, scsibusbioctl) != 0)
247 		printf("%s: unable to register bio\n", sb->sc_dev.dv_xname);
248 #endif /* NBIO > 0 */
249 
250 	scsi_probe_bus(sb);
251 }
252 
253 int
254 scsibusactivate(struct device *dev, int act)
255 {
256 	struct scsibus_softc *sb = (struct scsibus_softc *)dev;
257 
258 	return scsi_activate_bus(sb, act);
259 }
260 
261 int
262 scsibusdetach(struct device *dev, int type)
263 {
264 	struct scsibus_softc		*sb = (struct scsibus_softc *)dev;
265 	int				 error;
266 
267 #if NBIO > 0
268 	bio_unregister(&sb->sc_dev);
269 #endif /* NBIO > 0 */
270 
271 	error = scsi_detach_bus(sb, type);
272 	if (error != 0)
273 		return error;
274 
275 	KASSERT(SLIST_EMPTY(&sb->sc_link_list));
276 
277 	return 0;
278 }
279 
280 int
281 scsibussubmatch(struct device *parent, void *match, void *aux)
282 {
283 	struct cfdata			*cf = match;
284 	struct scsi_attach_args		*sa = aux;
285 	struct scsi_link		*link = sa->sa_sc_link;
286 
287 	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != link->target)
288 		return 0;
289 	if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != link->lun)
290 		return 0;
291 
292 	return (*cf->cf_attach->ca_match)(parent, match, aux);
293 }
294 
295 /*
296  * Print out autoconfiguration information for a subdevice.
297  *
298  * This is a slight abuse of 'standard' autoconfiguration semantics,
299  * because 'print' functions don't normally print the colon and
300  * device information.  However, in this case that's better than
301  * either printing redundant information before the attach message,
302  * or having the device driver call a special function to print out
303  * the standard device information.
304  */
305 int
306 scsibussubprint(void *aux, const char *pnp)
307 {
308 	struct scsi_attach_args		*sa = aux;
309 
310 	if (pnp != NULL)
311 		printf("%s", pnp);
312 
313 	scsi_print_link(sa->sa_sc_link);
314 
315 	return UNCONF;
316 }
317 
318 #if NBIO > 0
319 int
320 scsibusbioctl(struct device *dev, u_long cmd, caddr_t addr)
321 {
322 	struct scsibus_softc		*sb = (struct scsibus_softc *)dev;
323 	struct sbioc_device		*sdev;
324 
325 	switch (cmd) {
326 	case SBIOCPROBE:
327 		sdev = (struct sbioc_device *)addr;
328 		return scsi_probe(sb, sdev->sd_target, sdev->sd_lun);
329 
330 	case SBIOCDETACH:
331 		sdev = (struct sbioc_device *)addr;
332 		return scsi_detach(sb, sdev->sd_target, sdev->sd_lun, 0);
333 
334 	default:
335 		return ENOTTY;
336 	}
337 }
338 #endif /* NBIO > 0 */
339 
340 int
341 scsi_activate(struct scsibus_softc *sb, int target, int lun, int act)
342 {
343 	if (target == -1 && lun == -1)
344 		return scsi_activate_bus(sb, act);
345 	else if (lun == -1)
346 		return scsi_activate_target(sb, target, act);
347 	else
348 		return scsi_activate_lun(sb, target, lun, act);
349 }
350 
351 int
352 scsi_activate_bus(struct scsibus_softc *sb, int act)
353 {
354 	struct scsi_link		*link;
355 	int				 r, rv = 0;
356 
357 	/*  Activate all links on the bus. */
358 	SLIST_FOREACH(link, &sb->sc_link_list, bus_list) {
359 		r = scsi_activate_link(link, act);
360 		if (r)
361 			rv = r;
362 	}
363 	return rv;
364 }
365 
366 int
367 scsi_activate_target(struct scsibus_softc *sb, int target, int act)
368 {
369 	struct scsi_link		*link;
370 	int				 r, rv = 0;
371 
372 	/*  Activate all links on the target. */
373 	SLIST_FOREACH(link, &sb->sc_link_list, bus_list) {
374 		if (link->target == target) {
375 			r = scsi_activate_link(link, act);
376 			if (r)
377 				rv = r;
378 		}
379 	}
380 	return rv;
381 }
382 
383 int
384 scsi_activate_lun(struct scsibus_softc *sb, int target, int lun, int act)
385 {
386 	struct scsi_link *link;
387 
388 	/*  Activate the (target, lun) link.*/
389 	link = scsi_get_link(sb, target, lun);
390 	if (link == NULL)
391 		return 0;
392 
393 	return scsi_activate_link(link, act);
394 }
395 
396 int
397 scsi_activate_link(struct scsi_link *link, int act)
398 {
399 	struct device			*dev;
400 	int				 rv = 0;
401 
402 	dev = link->device_softc;
403 	switch (act) {
404 	case DVACT_DEACTIVATE:
405 		atomic_setbits_int(&link->state, SDEV_S_DYING);
406 		config_deactivate(dev);
407 		break;
408 	default:
409 		rv = config_suspend(dev, act);
410 		break;
411 	}
412 	return rv;
413 }
414 
415 int
416 scsi_probe(struct scsibus_softc *sb, int target, int lun)
417 {
418 	if (target == -1 && lun == -1)
419 		return scsi_probe_bus(sb);
420 	else if (lun == -1)
421 		return scsi_probe_target(sb, target);
422 	else
423 		return scsi_probe_lun(sb, target, lun);
424 }
425 
426 int
427 scsi_probe_bus(struct scsibus_softc *sb)
428 {
429 	int		target, r, rv = 0;
430 
431 	/* Probe all possible targets on bus. */
432 	for (target = 0; target < sb->sb_adapter_buswidth; target++) {
433 		r = scsi_probe_target(sb, target);
434 		if (r != 0 && r != EINVAL)
435 			rv = r;
436 	}
437 	return rv;
438 }
439 
440 int
441 scsi_probe_target(struct scsibus_softc *sb, int target)
442 {
443 	struct scsi_lun_array		 lunarray;
444 	struct scsi_link		*link0;
445 	int				 i, r, rv = 0;
446 
447 	if (target < 0 || target == sb->sb_adapter_target)
448 		return EINVAL;
449 
450 	/* Probe all possible luns on target. */
451 	scsi_probe_link(sb, target, 0, 0);
452 	link0 = scsi_get_link(sb, target, 0);
453 	if (link0 == NULL)
454 		return EINVAL;
455 	scsi_get_target_luns(link0, &lunarray);
456 	for (i = 0; i < lunarray.count; i++) {
457 		r = scsi_probe_link(sb, target, lunarray.luns[i],
458 		    lunarray.dumbscan);
459 		if (r == EINVAL && lunarray.dumbscan == 1)
460 			return 0;
461 		if (r != 0 && r != EINVAL)
462 			rv = r;
463 	}
464 	return rv;
465 }
466 
467 int
468 scsi_probe_lun(struct scsibus_softc *sb, int target, int lun)
469 {
470 	if (target < 0 || target == sb->sb_adapter_target || lun < 0)
471 		return EINVAL;
472 
473 	/* Probe lun on target. *NOT* a dumbscan! */
474 	return scsi_probe_link(sb, target, lun, 0);
475 }
476 
477 /*
478  * Given a target and lun, ask the device what it is, and find the correct
479  * driver table entry.
480  *
481  * Return 0 if further LUNs are possible, EINVAL if not.
482  */
483 int
484 scsi_probe_link(struct scsibus_softc *sb, int target, int lun, int dumbscan)
485 {
486 	struct scsi_attach_args			 sa;
487 	const struct scsi_quirk_inquiry_pattern	*finger;
488 	struct scsi_inquiry_data		*inqbuf, *usbinqbuf;
489 	struct scsi_link			*link, *link0;
490 	struct cfdata				*cf;
491 	int					 inqbytes, priority, rslt = 0;
492 	u_int16_t				 devquirks;
493 
494 	/* Skip this slot if it is already attached and try the next LUN. */
495 	if (scsi_get_link(sb, target, lun) != NULL)
496 		return 0;
497 
498 	link = malloc(sizeof(*link), M_DEVBUF, M_NOWAIT);
499 	if (link == NULL) {
500 		SC_DEBUG(link, SDEV_DB2, ("Bad LUN. can't allocate "
501 		    "scsi_link.\n"));
502 		return EINVAL;
503 	}
504 
505 	link->state = 0;
506 	link->target = target;
507 	link->lun = lun;
508 	link->openings = sb->sb_openings;
509 	link->node_wwn = link->port_wwn = 0;
510 	link->flags = sb->sb_flags;
511 	link->quirks = sb->sb_quirks;
512 	link->interpret_sense = scsi_interpret_sense;
513 	link->device_softc = NULL;
514 	link->bus = sb;
515 	memset(&link->inqdata, 0, sizeof(link->inqdata));
516 	link->id = NULL;
517 	TAILQ_INIT(&link->queue);
518 	link->running = 0;
519 	link->pending = 0;
520 	link->pool = sb->sb_pool;
521 
522 	SC_DEBUG(link, SDEV_DB2, ("scsi_link created.\n"));
523 
524 	/* Ask the adapter if this will be a valid device. */
525 	if (sb->sb_adapter->dev_probe != NULL &&
526 	    sb->sb_adapter->dev_probe(link) != 0) {
527 		if (lun == 0) {
528 			SC_DEBUG(link, SDEV_DB2, ("Bad LUN 0. dev_probe() "
529 			    "failed.\n"));
530 			rslt = EINVAL;
531 		}
532 		goto free;
533 	}
534 
535 	/*
536 	 * If we havent been given an io pool by now then fall back to
537 	 * using link->openings.
538 	 */
539 	if (link->pool == NULL) {
540 		link->pool = malloc(sizeof(*link->pool),
541 		    M_DEVBUF, M_NOWAIT);
542 		if (link->pool == NULL) {
543 			SC_DEBUG(link, SDEV_DB2, ("Bad LUN. can't allocate "
544 			    "link->pool.\n"));
545 			rslt = ENOMEM;
546 			goto bad;
547 		}
548 		scsi_iopool_init(link->pool, link,
549 		    scsi_default_get, scsi_default_put);
550 
551 		SET(link->flags, SDEV_OWN_IOPL);
552 	}
553 
554 	/*
555 	 * Tell drivers that are paying attention to avoid sync/wide/tags until
556 	 * INQUIRY data has been processed and the quirks information is
557 	 * complete. Some drivers set bits in quirks before we get here, so
558 	 * just add NOTAGS, NOWIDE and NOSYNC.
559 	 */
560 	devquirks = link->quirks;
561 	SET(link->quirks, SDEV_NOSYNC | SDEV_NOWIDE | SDEV_NOTAGS);
562 
563 	/*
564 	 * Ask the device what it is.
565 	 */
566 #ifdef SCSIDEBUG
567 	if (((sb->sc_dev.dv_unit < 32) &&
568 	    ((1U << sb->sc_dev.dv_unit) & scsidebug_buses)) &&
569 	    ((target < 32) && ((1U << target) & scsidebug_targets)) &&
570 	    ((lun < 32) && ((1U << lun) & scsidebug_luns)))
571 		SET(link->flags, scsidebug_level);
572 #endif /* SCSIDEBUG */
573 
574 	if (lun == 0) {
575 		/* Clear any outstanding errors. */
576 		scsi_test_unit_ready(link, TEST_READY_RETRIES,
577 		    scsi_autoconf | SCSI_IGNORE_ILLEGAL_REQUEST |
578 		    SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE);
579 	}
580 
581 	/* Now go ask the device all about itself. */
582 	inqbuf = dma_alloc(sizeof(*inqbuf), PR_NOWAIT | PR_ZERO);
583 	if (inqbuf == NULL) {
584 		SC_DEBUG(link, SDEV_DB2, ("Bad LUN. can't allocate inqbuf.\n"));
585 		rslt = ENOMEM;
586 		goto bad;
587 	}
588 
589 	rslt = scsi_inquire(link, inqbuf, scsi_autoconf | SCSI_SILENT);
590 	if (rslt != 0) {
591 		if (lun == 0) {
592 			SC_DEBUG(link, SDEV_DB2, ("Bad LUN 0. inquiry rslt = "
593 			    "%i\n", rslt));
594 			rslt = EINVAL;
595 		}
596 		dma_free(inqbuf, sizeof(*inqbuf));
597 		goto bad;
598 	}
599 	inqbytes = SID_SCSI2_HDRLEN + inqbuf->additional_length;
600 	memcpy(&link->inqdata, inqbuf, inqbytes);
601 	dma_free(inqbuf, sizeof(*inqbuf));
602 	inqbuf = &link->inqdata;
603 	if (inqbytes < offsetof(struct scsi_inquiry_data, vendor))
604 		memset(inqbuf->vendor, ' ', sizeof(inqbuf->vendor));
605 	if (inqbytes < offsetof(struct scsi_inquiry_data, product))
606 		memset(inqbuf->product, ' ', sizeof(inqbuf->product));
607 	if (inqbytes < offsetof(struct scsi_inquiry_data, revision))
608 		memset(inqbuf->revision, ' ', sizeof(inqbuf->revision));
609 	if (inqbytes < offsetof(struct scsi_inquiry_data, extra))
610 		memset(inqbuf->extra, ' ', sizeof(inqbuf->extra));
611 
612 	switch (inqbuf->device & SID_QUAL) {
613 	case SID_QUAL_RSVD:
614 	case SID_QUAL_BAD_LU:
615 	case SID_QUAL_LU_OFFLINE:
616 		SC_DEBUG(link, SDEV_DB1, ("Bad LUN. SID_QUAL = 0x%02x\n",
617 		    inqbuf->device & SID_QUAL));
618 		goto bad;
619 
620 	case SID_QUAL_LU_OK:
621 		break;
622 
623 	default:
624 		SC_DEBUG(link, SDEV_DB1, ("Vendor-specific SID_QUAL = 0x%02x\n",
625 		    inqbuf->device & SID_QUAL));
626 		break;
627 	}
628 
629 	if ((inqbuf->device & SID_TYPE) == T_NODEVICE) {
630 		SC_DEBUG(link, SDEV_DB1,
631 		    ("Bad LUN. SID_TYPE = T_NODEVICE\n"));
632 		goto bad;
633 	}
634 
635 	scsi_devid(link);
636 
637 	link0 = scsi_get_link(sb, target, 0);
638 	if (lun == 0 || link0 == NULL)
639 		;
640 	else if (ISSET(link->flags, SDEV_UMASS))
641 		;
642 	else if (link->id != NULL && !DEVID_CMP(link0->id, link->id))
643 		;
644 	else if (dumbscan == 1 && memcmp(inqbuf, &link0->inqdata,
645 	    sizeof(*inqbuf)) == 0) {
646 		/* The device doesn't distinguish between LUNs. */
647 		SC_DEBUG(link, SDEV_DB1, ("Bad LUN. IDENTIFY not supported."
648 		    "\n"));
649 		rslt = EINVAL;
650 		goto free_devid;
651 	}
652 
653 	link->quirks = devquirks;	/* Restore what the device wanted. */
654 
655 	finger = (const struct scsi_quirk_inquiry_pattern *)scsi_inqmatch(
656 	    inqbuf, scsi_quirk_patterns,
657 	    nitems(scsi_quirk_patterns),
658 	    sizeof(scsi_quirk_patterns[0]), &priority);
659 	if (priority != 0)
660 		SET(link->quirks, finger->quirks);
661 
662 	switch (SID_ANSII_REV(inqbuf)) {
663 	case SCSI_REV_0:
664 	case SCSI_REV_1:
665 		SET(link->quirks, SDEV_NOTAGS | SDEV_NOSYNC | SDEV_NOWIDE |
666 		    SDEV_NOSYNCCACHE);
667 		break;
668 	case SCSI_REV_2:
669 	case SCSI_REV_SPC:
670 	case SCSI_REV_SPC2:
671 		if (!ISSET(inqbuf->flags, SID_CmdQue))
672 			SET(link->quirks, SDEV_NOTAGS);
673 		if (!ISSET(inqbuf->flags, SID_Sync))
674 			SET(link->quirks, SDEV_NOSYNC);
675 		if (!ISSET(inqbuf->flags, SID_WBus16))
676 			SET(link->quirks, SDEV_NOWIDE);
677 		break;
678 	case SCSI_REV_SPC3:
679 	case SCSI_REV_SPC4:
680 	case SCSI_REV_SPC5:
681 		/* By this time SID_Sync and SID_WBus16 were obsolete. */
682 		if (!ISSET(inqbuf->flags, SID_CmdQue))
683 			SET(link->quirks, SDEV_NOTAGS);
684 		break;
685 	default:
686 		break;
687 	}
688 
689 	/*
690 	 * If the device can't use tags, >1 opening may confuse it.
691 	 */
692 	if (ISSET(link->quirks, SDEV_NOTAGS))
693 		link->openings = 1;
694 
695 	/*
696 	 * note what BASIC type of device it is
697 	 */
698 	if (ISSET(inqbuf->dev_qual2, SID_REMOVABLE))
699 		SET(link->flags, SDEV_REMOVABLE);
700 
701 	sa.sa_sc_link = link;
702 
703 	if ((cf = config_search(scsibussubmatch, (struct device *)sb,
704 	    &sa)) == 0) {
705 		scsibussubprint(&sa, sb->sc_dev.dv_xname);
706 		printf(" not configured\n");
707 		goto free_devid;
708 	}
709 
710 	/*
711 	 * Braindead USB devices, especially some x-in-1 media readers, try to
712 	 * 'help' by pretending any LUN is actually LUN 0 until they see a
713 	 * different LUN used in a command. So do an INQUIRY on LUN 1 at this
714 	 * point to prevent such helpfulness before it causes confusion.
715 	 */
716 	if (lun == 0 && ISSET(link->flags, SDEV_UMASS) &&
717 	    scsi_get_link(sb, target, 1) == NULL && sb->sb_luns > 1 &&
718 	    (usbinqbuf = dma_alloc(sizeof(*usbinqbuf), M_NOWAIT)) != NULL) {
719 
720 		link->lun = 1;
721 		scsi_inquire(link, usbinqbuf, scsi_autoconf | SCSI_SILENT);
722 		link->lun = 0;
723 
724 		dma_free(usbinqbuf, sizeof(*usbinqbuf));
725 	}
726 
727 	scsi_add_link(link);
728 
729 	/*
730 	 * Generate a TEST_UNIT_READY command. This gives drivers waiting for
731 	 * valid quirks data a chance to set wide/sync/tag options
732 	 * appropriately. It also clears any outstanding ACA conditions that
733 	 * INQUIRY may leave behind.
734 	 *
735 	 * Do this now so that any messages generated by config_attach() do not
736 	 * have negotiation messages inserted into their midst.
737 	 */
738 	scsi_test_unit_ready(link, TEST_READY_RETRIES,
739 	    scsi_autoconf | SCSI_IGNORE_ILLEGAL_REQUEST |
740 	    SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE);
741 
742 	config_attach((struct device *)sb, cf, &sa, scsibussubprint);
743 
744 	return 0;
745 
746 free_devid:
747 	if (link->id)
748 		devid_free(link->id);
749 bad:
750 	if (ISSET(link->flags, SDEV_OWN_IOPL))
751 		free(link->pool, M_DEVBUF, sizeof(*link->pool));
752 
753 	if (sb->sb_adapter->dev_free != NULL)
754 		sb->sb_adapter->dev_free(link);
755 free:
756 	free(link, M_DEVBUF, sizeof(*link));
757 	return rslt;
758 }
759 
760 int
761 scsi_detach(struct scsibus_softc *sb, int target, int lun, int flags)
762 {
763 	if (target == -1 && lun == -1)
764 		return scsi_detach_bus(sb, flags);
765 	else if (lun == -1)
766 		return scsi_detach_target(sb, target, flags);
767 	else
768 		return scsi_detach_lun(sb, target, lun, flags);
769 }
770 
771 int
772 scsi_detach_bus(struct scsibus_softc *sb, int flags)
773 {
774 	struct scsi_link	*link, *tmp;
775 	int			 r, rv = 0;
776 
777 	/* Detach all links from bus. */
778 	SLIST_FOREACH_SAFE(link, &sb->sc_link_list, bus_list, tmp) {
779 		r = scsi_detach_link(link, flags);
780 		if (r != 0 && r != ENXIO)
781 			rv = r;
782 	}
783 	return rv;
784 }
785 
786 int
787 scsi_detach_target(struct scsibus_softc *sb, int target, int flags)
788 {
789 	struct scsi_link	*link, *tmp;
790 	int			 r, rv = 0;
791 
792 	/* Detach all links from target. */
793 	SLIST_FOREACH_SAFE(link, &sb->sc_link_list, bus_list, tmp) {
794 		if (link->target == target) {
795 			r = scsi_detach_link(link, flags);
796 			if (r != 0 && r != ENXIO)
797 				rv = r;
798 		}
799 	}
800 	return rv;
801 }
802 
803 int
804 scsi_detach_lun(struct scsibus_softc *sb, int target, int lun, int flags)
805 {
806 	struct scsi_link	*link;
807 
808 	/* Detach (target, lun) link. */
809 	link = scsi_get_link(sb, target, lun);
810 	if (link == NULL)
811 		return EINVAL;
812 
813 	return scsi_detach_link(link, flags);
814 }
815 
816 int
817 scsi_detach_link(struct scsi_link *link, int flags)
818 {
819 	struct scsibus_softc		*sb = link->bus;
820 	int				 rv;
821 
822 	if (!ISSET(flags, DETACH_FORCE) && ISSET(link->flags, SDEV_OPEN))
823 		return EBUSY;
824 
825 	/* Detaching a device from scsibus is a five step process. */
826 
827 	/* 1. Wake up processes sleeping for an xs. */
828 	scsi_link_shutdown(link);
829 
830 	/* 2. Detach the device. */
831 	rv = config_detach(link->device_softc, flags);
832 
833 	if (rv != 0)
834 		return rv;
835 
836 	/* 3. If it's using the openings io allocator, clean that up. */
837 	if (ISSET(link->flags, SDEV_OWN_IOPL)) {
838 		scsi_iopool_destroy(link->pool);
839 		free(link->pool, M_DEVBUF, sizeof(*link->pool));
840 	}
841 
842 	/* 4. Free up its state in the adapter. */
843 	if (sb->sb_adapter->dev_free != NULL)
844 		sb->sb_adapter->dev_free(link);
845 
846 	/* 5. Free up its state in the midlayer. */
847 	if (link->id != NULL)
848 		devid_free(link->id);
849 	scsi_remove_link(link);
850 	free(link, M_DEVBUF, sizeof(*link));
851 
852 	return 0;
853 }
854 
855 struct scsi_link *
856 scsi_get_link(struct scsibus_softc *sb, int target, int lun)
857 {
858 	struct scsi_link *link;
859 
860 	SLIST_FOREACH(link, &sb->sc_link_list, bus_list) {
861 		if (link->target == target && link->lun == lun)
862 			return link;
863 	}
864 
865 	return NULL;
866 }
867 
868 void
869 scsi_add_link(struct scsi_link *link)
870 {
871 	SLIST_INSERT_HEAD(&link->bus->sc_link_list, link, bus_list);
872 }
873 
874 void
875 scsi_remove_link(struct scsi_link *link)
876 {
877 	SLIST_REMOVE(&link->bus->sc_link_list, link, scsi_link, bus_list);
878 }
879 
880 void
881 scsi_get_target_luns(struct scsi_link *link0, struct scsi_lun_array *lunarray)
882 {
883 	struct scsi_report_luns_data	*report;
884 	int				 i, nluns, rv = 0;
885 
886 	/* Initialize dumbscan result. Just in case. */
887 	report = NULL;
888 	for (i = 0; i < link0->bus->sb_luns; i++)
889 		lunarray->luns[i] = i;
890 	lunarray->count = link0->bus->sb_luns;
891 	lunarray->dumbscan = 1;
892 
893 	/*
894 	 * ATAPI, USB and pre-SPC (i.e. pre-SCSI-3) devices can't ask
895 	 * for a report of valid LUNs.
896 	 */
897 	if ((link0->flags & (SDEV_UMASS | SDEV_ATAPI)) != 0 ||
898 	    SID_ANSII_REV(&link0->inqdata) < SCSI_REV_SPC)
899 		goto dumbscan;
900 
901 	report = dma_alloc(sizeof(*report), PR_WAITOK);
902 	if (report == NULL)
903 		goto dumbscan;
904 
905 	rv = scsi_report_luns(link0, REPORT_NORMAL, report,
906 	    sizeof(*report), scsi_autoconf | SCSI_SILENT |
907 	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY |
908 	    SCSI_IGNORE_MEDIA_CHANGE, 10000);
909 	if (rv != 0)
910 		goto dumbscan;
911 
912 	/*
913 	 * XXX In theory we should check if data is full, which
914 	 * would indicate it needs to be enlarged and REPORT
915 	 * LUNS tried again. Solaris tries up to 3 times with
916 	 * larger sizes for data.
917 	 */
918 
919 	/* Return the reported Type-0 LUNs. Type-0 only! */
920 	lunarray->count = 0;
921 	lunarray->dumbscan = 0;
922 	nluns = _4btol(report->length) / RPL_LUNDATA_SIZE;
923 	for (i = 0; i < nluns; i++) {
924 		if (report->luns[i].lundata[0] != 0)
925 			continue;
926 		lunarray->luns[lunarray->count++] =
927 		    report->luns[i].lundata[RPL_LUNDATA_T0LUN];
928 	}
929 
930 dumbscan:
931 	if (report != NULL)
932 		dma_free(report, sizeof(*report));
933 }
934 
935 void
936 scsi_strvis(u_char *dst, u_char *src, int len)
937 {
938 	u_char				last;
939 
940 	/* Trim leading and trailing whitespace and NULs. */
941 	while (len > 0 && (src[0] == ' ' || src[0] == '\t' || src[0] == '\n' ||
942 	    src[0] == '\0' || src[0] == 0xff))
943 		++src, --len;
944 	while (len > 0 && (src[len-1] == ' ' || src[len-1] == '\t' ||
945 	    src[len-1] == '\n' || src[len-1] == '\0' || src[len-1] == 0xff))
946 		--len;
947 
948 	last = 0xff;
949 	while (len > 0) {
950 		switch (*src) {
951 		case ' ':
952 		case '\t':
953 		case '\n':
954 		case '\0':
955 		case 0xff:
956 			/* Collapse whitespace and NULs to a single space. */
957 			if (last != ' ')
958 				*dst++ = ' ';
959 			last = ' ';
960 			break;
961 		case '\\':
962 			/* Quote backslashes. */
963 			*dst++ = '\\';
964 			*dst++ = '\\';
965 			last = '\\';
966 			break;
967 		default:
968 			if (*src < 0x20 || *src >= 0x80) {
969 				/* Non-printable characters to octal. */
970 				*dst++ = '\\';
971 				*dst++ = ((*src & 0300) >> 6) + '0';
972 				*dst++ = ((*src & 0070) >> 3) + '0';
973 				*dst++ = ((*src & 0007) >> 0) + '0';
974 			} else {
975 				/* Copy normal characters. */
976 				*dst++ = *src;
977 			}
978 			last = *src;
979 			break;
980 		}
981 		++src, --len;
982 	}
983 
984 	*dst++ = 0;
985 }
986 
987 void
988 scsi_print_link(struct scsi_link *link)
989 {
990 	char				 visbuf[65];
991 	struct scsi_inquiry_data	*inqbuf;
992 	u_int8_t			*id;
993 	int				 i;
994 
995 	printf(" targ %d lun %d: ", link->target, link->lun);
996 
997 	inqbuf = &link->inqdata;
998 
999 	scsi_strvis(visbuf, inqbuf->vendor, 8);
1000 	printf("<%s, ", visbuf);
1001 	scsi_strvis(visbuf, inqbuf->product, 16);
1002 	printf("%s, ", visbuf);
1003 	scsi_strvis(visbuf, inqbuf->revision, 4);
1004 	printf("%s>", visbuf);
1005 
1006 #ifdef SCSIDEBUG
1007 	if (ISSET(link->flags, SDEV_ATAPI))
1008 		printf(" ATAPI");
1009 	else if (SID_ANSII_REV(inqbuf) < SCSI_REV_SPC)
1010 		printf(" SCSI/%d", SID_ANSII_REV(inqbuf));
1011 	else if (SID_ANSII_REV(inqbuf) == SCSI_REV_SPC)
1012 		printf(" SCSI/SPC");
1013 	else
1014 		printf(" SCSI/SPC-%d", SID_ANSII_REV(inqbuf) - 2);
1015 #endif /* SCSIDEBUG */
1016 
1017 	if (ISSET(link->flags, SDEV_REMOVABLE))
1018 		printf(" removable");
1019 
1020 	if (link->id != NULL && link->id->d_type != DEVID_NONE) {
1021 		id = (u_int8_t *)(link->id + 1);
1022 		switch (link->id->d_type) {
1023 		case DEVID_NAA:
1024 			printf(" naa.");
1025 			break;
1026 		case DEVID_EUI:
1027 			printf(" eui.");
1028 			break;
1029 		case DEVID_T10:
1030 			printf(" t10.");
1031 			break;
1032 		case DEVID_SERIAL:
1033 			printf(" serial.");
1034 			break;
1035 		case DEVID_WWN:
1036 			printf(" wwn.");
1037 			break;
1038 		}
1039 
1040 		if (ISSET(link->id->d_flags, DEVID_F_PRINT)) {
1041 			for (i = 0; i < link->id->d_len; i++) {
1042 				if (id[i] == '\0' || id[i] == ' ') {
1043 					/* skip leading blanks */
1044 					/* collapse multiple blanks into one */
1045 					if (i > 0 && id[i-1] != id[i])
1046 						printf("_");
1047 				} else if (id[i] < 0x20 || id[i] >= 0x80) {
1048 					/* non-printable characters */
1049 					printf("~");
1050 				} else {
1051 					/* normal characters */
1052 					printf("%c", id[i]);
1053 				}
1054 			}
1055 		} else {
1056 			for (i = 0; i < link->id->d_len; i++)
1057 				printf("%02x", id[i]);
1058 		}
1059 	}
1060 #ifdef SCSIDEBUG
1061 	printf("\n");
1062 	sc_print_addr(link);
1063 	printf("state %u, luns %u, openings %u\n",
1064 	    link->state, link->bus->sb_luns, link->openings);
1065 
1066 	sc_print_addr(link);
1067 	printf("flags (0x%04x) ", link->flags);
1068 	scsi_show_flags(link->flags, flagnames);
1069 	printf("\n");
1070 
1071 	sc_print_addr(link);
1072 	printf("quirks (0x%04x) ", link->quirks);
1073 	scsi_show_flags(link->quirks, quirknames);
1074 #endif /* SCSIDEBUG */
1075 }
1076 
1077 /*
1078  * Return a priority based on how much of the inquiry data matches
1079  * the patterns for the particular driver.
1080  */
1081 const void *
1082 scsi_inqmatch(struct scsi_inquiry_data *inqbuf, const void *_base,
1083     int nmatches, int matchsize, int *bestpriority)
1084 {
1085 	const unsigned char		*base = (const unsigned char *)_base;
1086 	const void			*bestmatch;
1087 	int				 removable;
1088 
1089 	/* Include the qualifier to catch vendor-unique types. */
1090 	removable = ISSET(inqbuf->dev_qual2, SID_REMOVABLE) ? T_REMOV : T_FIXED;
1091 
1092 	for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) {
1093 		struct scsi_inquiry_pattern *match = (void *)base;
1094 		int priority, len;
1095 
1096 		if (inqbuf->device != match->type)
1097 			continue;
1098 		if (removable != match->removable)
1099 			continue;
1100 		priority = 2;
1101 		len = strlen(match->vendor);
1102 		if (bcmp(inqbuf->vendor, match->vendor, len))
1103 			continue;
1104 		priority += len;
1105 		len = strlen(match->product);
1106 		if (bcmp(inqbuf->product, match->product, len))
1107 			continue;
1108 		priority += len;
1109 		len = strlen(match->revision);
1110 		if (bcmp(inqbuf->revision, match->revision, len))
1111 			continue;
1112 		priority += len;
1113 
1114 #ifdef SCSIDEBUG
1115 		printf("scsi_inqmatch: ");
1116 		if (_base == &scsi_quirk_patterns)
1117 			printf(" quirk ");
1118 		else
1119 			printf(" match ");
1120 
1121 		printf("priority %d. %s %s <\"%s\", \"%s\", \"%s\">", priority,
1122 		    devicetypenames[(match->type & SID_TYPE)],
1123 		    (match->removable == T_FIXED) ? "T_FIXED" : "T_REMOV",
1124 		    match->vendor, match->product, match->revision);
1125 
1126 		if (_base == &scsi_quirk_patterns)
1127 			printf(" quirks: 0x%04x",
1128 			    ((struct scsi_quirk_inquiry_pattern *)match)->quirks
1129 			);
1130 
1131 		printf("\n");
1132 #endif /* SCSIDEBUG */
1133 		if (priority > *bestpriority) {
1134 			*bestpriority = priority;
1135 			bestmatch = base;
1136 		}
1137 	}
1138 
1139 	return bestmatch;
1140 }
1141 
1142 void
1143 scsi_devid(struct scsi_link *link)
1144 {
1145 	struct {
1146 		struct scsi_vpd_hdr hdr;
1147 		u_int8_t list[32];
1148 	} __packed			*pg;
1149 	size_t				 len;
1150 	int				 pg80 = 0, pg83 = 0, i;
1151 
1152 	if (link->id != NULL)
1153 		return;
1154 
1155 	pg = dma_alloc(sizeof(*pg), PR_WAITOK | PR_ZERO);
1156 
1157 	if (SID_ANSII_REV(&link->inqdata) >= SCSI_REV_2) {
1158 		if (scsi_inquire_vpd(link, pg, sizeof(*pg), SI_PG_SUPPORTED,
1159 		    scsi_autoconf) != 0)
1160 			goto wwn;
1161 
1162 		len = MIN(sizeof(pg->list), _2btol(pg->hdr.page_length));
1163 		for (i = 0; i < len; i++) {
1164 			switch (pg->list[i]) {
1165 			case SI_PG_SERIAL:
1166 				pg80 = 1;
1167 				break;
1168 			case SI_PG_DEVID:
1169 				pg83 = 1;
1170 				break;
1171 			}
1172 		}
1173 
1174 		if (pg83 && scsi_devid_pg83(link) == 0)
1175 			goto done;
1176 		if (pg80 && scsi_devid_pg80(link) == 0)
1177 			goto done;
1178 	}
1179 
1180 wwn:
1181 	scsi_devid_wwn(link);
1182 done:
1183 	dma_free(pg, sizeof(*pg));
1184 }
1185 
1186 int
1187 scsi_devid_pg83(struct scsi_link *link)
1188 {
1189 	struct scsi_vpd_devid_hdr	 dhdr, chdr;
1190 	struct scsi_vpd_hdr		*hdr = NULL;
1191 	u_int8_t			*pg = NULL, *id;
1192 	int				 len, pos, rv, type;
1193 	int				 idtype = 0;
1194 	u_char				 idflags;
1195 
1196 	hdr = dma_alloc(sizeof(*hdr), PR_WAITOK | PR_ZERO);
1197 
1198 	rv = scsi_inquire_vpd(link, hdr, sizeof(*hdr), SI_PG_DEVID,
1199 	    scsi_autoconf);
1200 	if (rv != 0)
1201 		goto done;
1202 
1203 	len = sizeof(*hdr) + _2btol(hdr->page_length);
1204 	pg = dma_alloc(len, PR_WAITOK | PR_ZERO);
1205 
1206 	rv = scsi_inquire_vpd(link, pg, len, SI_PG_DEVID, scsi_autoconf);
1207 	if (rv != 0)
1208 		goto done;
1209 
1210 	pos = sizeof(*hdr);
1211 
1212 	do {
1213 		if (len - pos < sizeof(dhdr)) {
1214 			rv = EIO;
1215 			goto done;
1216 		}
1217 		memcpy(&dhdr, &pg[pos], sizeof(dhdr));
1218 		pos += sizeof(dhdr);
1219 		if (len - pos < dhdr.len) {
1220 			rv = EIO;
1221 			goto done;
1222 		}
1223 
1224 		if (VPD_DEVID_ASSOC(dhdr.flags) == VPD_DEVID_ASSOC_LU) {
1225 			type = VPD_DEVID_TYPE(dhdr.flags);
1226 			switch (type) {
1227 			case VPD_DEVID_TYPE_NAA:
1228 			case VPD_DEVID_TYPE_EUI64:
1229 			case VPD_DEVID_TYPE_T10:
1230 				if (type >= idtype) {
1231 					idtype = type;
1232 
1233 					chdr = dhdr;
1234 					id = &pg[pos];
1235 				}
1236 				break;
1237 
1238 			default:
1239 				/* skip */
1240 				break;
1241 			}
1242 		}
1243 
1244 		pos += dhdr.len;
1245 	} while (idtype != VPD_DEVID_TYPE_NAA && len != pos);
1246 
1247 	if (idtype > 0) {
1248 		switch (VPD_DEVID_TYPE(chdr.flags)) {
1249 		case VPD_DEVID_TYPE_NAA:
1250 			idtype = DEVID_NAA;
1251 			break;
1252 		case VPD_DEVID_TYPE_EUI64:
1253 			idtype = DEVID_EUI;
1254 			break;
1255 		case VPD_DEVID_TYPE_T10:
1256 			idtype = DEVID_T10;
1257 			break;
1258 		}
1259 		switch (VPD_DEVID_CODE(chdr.pi_code)) {
1260 		case VPD_DEVID_CODE_ASCII:
1261 		case VPD_DEVID_CODE_UTF8:
1262 			idflags = DEVID_F_PRINT;
1263 			break;
1264 		default:
1265 			idflags = 0;
1266 			break;
1267 		}
1268 		link->id = devid_alloc(idtype, idflags, chdr.len, id);
1269 	} else
1270 		rv = ENODEV;
1271 
1272 done:
1273 	if (pg)
1274 		dma_free(pg, len);
1275 	if (hdr)
1276 		dma_free(hdr, sizeof(*hdr));
1277 	return rv;
1278 }
1279 
1280 int
1281 scsi_devid_pg80(struct scsi_link *link)
1282 {
1283 	struct scsi_vpd_hdr		*hdr = NULL;
1284 	u_int8_t			*pg = NULL;
1285 	char				*id;
1286 	size_t				 idlen;
1287 	int				 len, pglen, rv;
1288 
1289 	hdr = dma_alloc(sizeof(*hdr), PR_WAITOK | PR_ZERO);
1290 
1291 	rv = scsi_inquire_vpd(link, hdr, sizeof(*hdr), SI_PG_SERIAL,
1292 	    scsi_autoconf);
1293 	if (rv != 0)
1294 		goto freehdr;
1295 
1296 	len = _2btol(hdr->page_length);
1297 	if (len == 0) {
1298 		rv = EINVAL;
1299 		goto freehdr;
1300 	}
1301 
1302 	pglen = sizeof(*hdr) + len;
1303 	pg = dma_alloc(pglen, PR_WAITOK | PR_ZERO);
1304 
1305 	rv = scsi_inquire_vpd(link, pg, pglen, SI_PG_SERIAL, scsi_autoconf);
1306 	if (rv != 0)
1307 		goto free;
1308 
1309 	idlen = sizeof(link->inqdata.vendor) +
1310 	    sizeof(link->inqdata.product) + len;
1311 	id = malloc(idlen, M_TEMP, M_WAITOK);
1312 	memcpy(id, link->inqdata.vendor, sizeof(link->inqdata.vendor));
1313 	memcpy(id + sizeof(link->inqdata.vendor), link->inqdata.product,
1314 	    sizeof(link->inqdata.product));
1315 	memcpy(id + sizeof(link->inqdata.vendor) +
1316 	    sizeof(link->inqdata.product), pg + sizeof(*hdr), len);
1317 
1318 	link->id = devid_alloc(DEVID_SERIAL, DEVID_F_PRINT,
1319 	    sizeof(link->inqdata.vendor) + sizeof(link->inqdata.product) + len,
1320 	    id);
1321 
1322 	free(id, M_TEMP, idlen);
1323 
1324 free:
1325 	dma_free(pg, pglen);
1326 freehdr:
1327 	dma_free(hdr, sizeof(*hdr));
1328 	return rv;
1329 }
1330 
1331 int
1332 scsi_devid_wwn(struct scsi_link *link)
1333 {
1334 	u_int64_t wwnn;
1335 
1336 	if (link->lun != 0 || link->node_wwn == 0)
1337 		return EOPNOTSUPP;
1338 
1339 	wwnn = htobe64(link->node_wwn);
1340 	link->id = devid_alloc(DEVID_WWN, 0, sizeof(wwnn), (u_int8_t *)&wwnn);
1341 
1342 	return 0;
1343 }
1344 
1345 struct devid *
1346 devid_alloc(u_int8_t type, u_int8_t flags, u_int8_t len, u_int8_t *id)
1347 {
1348 	struct devid *d;
1349 
1350 	d = malloc(sizeof(*d) + len, M_DEVBUF, M_WAITOK|M_CANFAIL);
1351 	if (d == NULL)
1352 		return NULL;
1353 
1354 	d->d_type = type;
1355 	d->d_flags = flags;
1356 	d->d_len = len;
1357 	d->d_refcount = 1;
1358 	memcpy(d + 1, id, len);
1359 
1360 	return d;
1361 }
1362 
1363 struct devid *
1364 devid_copy(struct devid *d)
1365 {
1366 	d->d_refcount++;
1367 	return d;
1368 }
1369 
1370 void
1371 devid_free(struct devid *d)
1372 {
1373 	if (--d->d_refcount == 0)
1374 		free(d, M_DEVBUF, sizeof(*d) + d->d_len);
1375 }
1376