xref: /openbsd-src/sys/scsi/scsiconf.c (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
1 /*	$OpenBSD: scsiconf.c,v 1.236 2020/08/18 16:30:38 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					 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 	memcpy(&link->inqdata, inqbuf, sizeof(link->inqdata));
591 	dma_free(inqbuf, sizeof(*inqbuf));
592 
593 	if (rslt != 0) {
594 		if (lun == 0) {
595 			SC_DEBUG(link, SDEV_DB2, ("Bad LUN 0. inquiry rslt = "
596 			    "%i\n", rslt));
597 			rslt = EINVAL;
598 		}
599 		goto bad;
600 	}
601 	inqbuf = &link->inqdata;
602 
603 	switch (inqbuf->device & SID_QUAL) {
604 	case SID_QUAL_RSVD:
605 	case SID_QUAL_BAD_LU:
606 	case SID_QUAL_LU_OFFLINE:
607 		SC_DEBUG(link, SDEV_DB1, ("Bad LUN. SID_QUAL = 0x%02x\n",
608 		    inqbuf->device & SID_QUAL));
609 		goto bad;
610 
611 	case SID_QUAL_LU_OK:
612 		break;
613 
614 	default:
615 		SC_DEBUG(link, SDEV_DB1, ("Vendor-specific SID_QUAL = 0x%02x\n",
616 		    inqbuf->device & SID_QUAL));
617 		break;
618 	}
619 
620 	if ((inqbuf->device & SID_TYPE) == T_NODEVICE) {
621 		SC_DEBUG(link, SDEV_DB1,
622 		    ("Bad LUN. SID_TYPE = T_NODEVICE\n"));
623 		goto bad;
624 	}
625 
626 	scsi_devid(link);
627 
628 	link0 = scsi_get_link(sb, target, 0);
629 	if (lun == 0 || link0 == NULL)
630 		;
631 	else if (ISSET(link->flags, SDEV_UMASS))
632 		;
633 	else if (link->id != NULL && !DEVID_CMP(link0->id, link->id))
634 		;
635 	else if (dumbscan == 1 && memcmp(inqbuf, &link0->inqdata,
636 	    sizeof(*inqbuf)) == 0) {
637 		/* The device doesn't distinguish between LUNs. */
638 		SC_DEBUG(link, SDEV_DB1, ("Bad LUN. IDENTIFY not supported."
639 		    "\n"));
640 		rslt = EINVAL;
641 		goto free_devid;
642 	}
643 
644 	link->quirks = devquirks;	/* Restore what the device wanted. */
645 
646 	finger = (const struct scsi_quirk_inquiry_pattern *)scsi_inqmatch(
647 	    inqbuf, scsi_quirk_patterns,
648 	    nitems(scsi_quirk_patterns),
649 	    sizeof(scsi_quirk_patterns[0]), &priority);
650 	if (priority != 0)
651 		SET(link->quirks, finger->quirks);
652 
653 	switch (SID_ANSII_REV(inqbuf)) {
654 	case SCSI_REV_0:
655 	case SCSI_REV_1:
656 		SET(link->quirks, SDEV_NOTAGS | SDEV_NOSYNC | SDEV_NOWIDE |
657 		    SDEV_NOSYNCCACHE);
658 		break;
659 	case SCSI_REV_2:
660 	case SCSI_REV_SPC:
661 	case SCSI_REV_SPC2:
662 		if (!ISSET(inqbuf->flags, SID_CmdQue))
663 			SET(link->quirks, SDEV_NOTAGS);
664 		if (!ISSET(inqbuf->flags, SID_Sync))
665 			SET(link->quirks, SDEV_NOSYNC);
666 		if (!ISSET(inqbuf->flags, SID_WBus16))
667 			SET(link->quirks, SDEV_NOWIDE);
668 		break;
669 	case SCSI_REV_SPC3:
670 	case SCSI_REV_SPC4:
671 	case SCSI_REV_SPC5:
672 		/* By this time SID_Sync and SID_WBus16 were obsolete. */
673 		if (!ISSET(inqbuf->flags, SID_CmdQue))
674 			SET(link->quirks, SDEV_NOTAGS);
675 		break;
676 	default:
677 		break;
678 	}
679 
680 	/*
681 	 * If the device can't use tags, >1 opening may confuse it.
682 	 */
683 	if (ISSET(link->quirks, SDEV_NOTAGS))
684 		link->openings = 1;
685 
686 	/*
687 	 * note what BASIC type of device it is
688 	 */
689 	if (ISSET(inqbuf->dev_qual2, SID_REMOVABLE))
690 		SET(link->flags, SDEV_REMOVABLE);
691 
692 	sa.sa_sc_link = link;
693 
694 	if ((cf = config_search(scsibussubmatch, (struct device *)sb,
695 	    &sa)) == 0) {
696 		scsibussubprint(&sa, sb->sc_dev.dv_xname);
697 		printf(" not configured\n");
698 		goto free_devid;
699 	}
700 
701 	/*
702 	 * Braindead USB devices, especially some x-in-1 media readers, try to
703 	 * 'help' by pretending any LUN is actually LUN 0 until they see a
704 	 * different LUN used in a command. So do an INQUIRY on LUN 1 at this
705 	 * point to prevent such helpfulness before it causes confusion.
706 	 */
707 	if (lun == 0 && ISSET(link->flags, SDEV_UMASS) &&
708 	    scsi_get_link(sb, target, 1) == NULL && sb->sb_luns > 1 &&
709 	    (usbinqbuf = dma_alloc(sizeof(*usbinqbuf), M_NOWAIT)) != NULL) {
710 
711 		link->lun = 1;
712 		scsi_inquire(link, usbinqbuf, scsi_autoconf | SCSI_SILENT);
713 		link->lun = 0;
714 
715 		dma_free(usbinqbuf, sizeof(*usbinqbuf));
716 	}
717 
718 	scsi_add_link(link);
719 
720 	/*
721 	 * Generate a TEST_UNIT_READY command. This gives drivers waiting for
722 	 * valid quirks data a chance to set wide/sync/tag options
723 	 * appropriately. It also clears any outstanding ACA conditions that
724 	 * INQUIRY may leave behind.
725 	 *
726 	 * Do this now so that any messages generated by config_attach() do not
727 	 * have negotiation messages inserted into their midst.
728 	 */
729 	scsi_test_unit_ready(link, TEST_READY_RETRIES,
730 	    scsi_autoconf | SCSI_IGNORE_ILLEGAL_REQUEST |
731 	    SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE);
732 
733 	config_attach((struct device *)sb, cf, &sa, scsibussubprint);
734 
735 	return 0;
736 
737 free_devid:
738 	if (link->id)
739 		devid_free(link->id);
740 bad:
741 	if (ISSET(link->flags, SDEV_OWN_IOPL))
742 		free(link->pool, M_DEVBUF, sizeof(*link->pool));
743 
744 	if (sb->sb_adapter->dev_free != NULL)
745 		sb->sb_adapter->dev_free(link);
746 free:
747 	free(link, M_DEVBUF, sizeof(*link));
748 	return rslt;
749 }
750 
751 int
752 scsi_detach(struct scsibus_softc *sb, int target, int lun, int flags)
753 {
754 	if (target == -1 && lun == -1)
755 		return scsi_detach_bus(sb, flags);
756 	else if (lun == -1)
757 		return scsi_detach_target(sb, target, flags);
758 	else
759 		return scsi_detach_lun(sb, target, lun, flags);
760 }
761 
762 int
763 scsi_detach_bus(struct scsibus_softc *sb, int flags)
764 {
765 	struct scsi_link	*link, *tmp;
766 	int			 r, rv = 0;
767 
768 	/* Detach all links from bus. */
769 	SLIST_FOREACH_SAFE(link, &sb->sc_link_list, bus_list, tmp) {
770 		r = scsi_detach_link(link, flags);
771 		if (r != 0 && r != ENXIO)
772 			rv = r;
773 	}
774 	return rv;
775 }
776 
777 int
778 scsi_detach_target(struct scsibus_softc *sb, int target, int flags)
779 {
780 	struct scsi_link	*link, *tmp;
781 	int			 r, rv = 0;
782 
783 	/* Detach all links from target. */
784 	SLIST_FOREACH_SAFE(link, &sb->sc_link_list, bus_list, tmp) {
785 		if (link->target == target) {
786 			r = scsi_detach_link(link, flags);
787 			if (r != 0 && r != ENXIO)
788 				rv = r;
789 		}
790 	}
791 	return rv;
792 }
793 
794 int
795 scsi_detach_lun(struct scsibus_softc *sb, int target, int lun, int flags)
796 {
797 	struct scsi_link	*link;
798 
799 	/* Detach (target, lun) link. */
800 	link = scsi_get_link(sb, target, lun);
801 	if (link == NULL)
802 		return EINVAL;
803 
804 	return scsi_detach_link(link, flags);
805 }
806 
807 int
808 scsi_detach_link(struct scsi_link *link, int flags)
809 {
810 	struct scsibus_softc		*sb = link->bus;
811 	int				 rv;
812 
813 	if (!ISSET(flags, DETACH_FORCE) && ISSET(link->flags, SDEV_OPEN))
814 		return EBUSY;
815 
816 	/* Detaching a device from scsibus is a five step process. */
817 
818 	/* 1. Wake up processes sleeping for an xs. */
819 	scsi_link_shutdown(link);
820 
821 	/* 2. Detach the device. */
822 	rv = config_detach(link->device_softc, flags);
823 
824 	if (rv != 0)
825 		return rv;
826 
827 	/* 3. If it's using the openings io allocator, clean that up. */
828 	if (ISSET(link->flags, SDEV_OWN_IOPL)) {
829 		scsi_iopool_destroy(link->pool);
830 		free(link->pool, M_DEVBUF, sizeof(*link->pool));
831 	}
832 
833 	/* 4. Free up its state in the adapter. */
834 	if (sb->sb_adapter->dev_free != NULL)
835 		sb->sb_adapter->dev_free(link);
836 
837 	/* 5. Free up its state in the midlayer. */
838 	if (link->id != NULL)
839 		devid_free(link->id);
840 	scsi_remove_link(link);
841 	free(link, M_DEVBUF, sizeof(*link));
842 
843 	return 0;
844 }
845 
846 struct scsi_link *
847 scsi_get_link(struct scsibus_softc *sb, int target, int lun)
848 {
849 	struct scsi_link *link;
850 
851 	SLIST_FOREACH(link, &sb->sc_link_list, bus_list) {
852 		if (link->target == target && link->lun == lun)
853 			return link;
854 	}
855 
856 	return NULL;
857 }
858 
859 void
860 scsi_add_link(struct scsi_link *link)
861 {
862 	SLIST_INSERT_HEAD(&link->bus->sc_link_list, link, bus_list);
863 }
864 
865 void
866 scsi_remove_link(struct scsi_link *link)
867 {
868 	SLIST_REMOVE(&link->bus->sc_link_list, link, scsi_link, bus_list);
869 }
870 
871 void
872 scsi_get_target_luns(struct scsi_link *link0, struct scsi_lun_array *lunarray)
873 {
874 	struct scsi_report_luns_data	*report;
875 	int				 i, nluns, rv = 0;
876 
877 	/* Initialize dumbscan result. Just in case. */
878 	report = NULL;
879 	for (i = 0; i < link0->bus->sb_luns; i++)
880 		lunarray->luns[i] = i;
881 	lunarray->count = link0->bus->sb_luns;
882 	lunarray->dumbscan = 1;
883 
884 	/*
885 	 * ATAPI, USB and pre-SPC (i.e. pre-SCSI-3) devices can't ask
886 	 * for a report of valid LUNs.
887 	 */
888 	if ((link0->flags & (SDEV_UMASS | SDEV_ATAPI)) != 0 ||
889 	    SID_ANSII_REV(&link0->inqdata) < SCSI_REV_SPC)
890 		goto dumbscan;
891 
892 	report = dma_alloc(sizeof(*report), PR_WAITOK);
893 	if (report == NULL)
894 		goto dumbscan;
895 
896 	rv = scsi_report_luns(link0, REPORT_NORMAL, report,
897 	    sizeof(*report), scsi_autoconf | SCSI_SILENT |
898 	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY |
899 	    SCSI_IGNORE_MEDIA_CHANGE, 10000);
900 	if (rv != 0)
901 		goto dumbscan;
902 
903 	/*
904 	 * XXX In theory we should check if data is full, which
905 	 * would indicate it needs to be enlarged and REPORT
906 	 * LUNS tried again. Solaris tries up to 3 times with
907 	 * larger sizes for data.
908 	 */
909 
910 	/* Return the reported Type-0 LUNs. Type-0 only! */
911 	lunarray->count = 0;
912 	lunarray->dumbscan = 0;
913 	nluns = _4btol(report->length) / RPL_LUNDATA_SIZE;
914 	for (i = 0; i < nluns; i++) {
915 		if (report->luns[i].lundata[0] != 0)
916 			continue;
917 		lunarray->luns[lunarray->count++] =
918 		    report->luns[i].lundata[RPL_LUNDATA_T0LUN];
919 	}
920 
921 dumbscan:
922 	if (report != NULL)
923 		dma_free(report, sizeof(*report));
924 }
925 
926 void
927 scsi_strvis(u_char *dst, u_char *src, int len)
928 {
929 	u_char				last;
930 
931 	/* Trim leading and trailing whitespace and NULs. */
932 	while (len > 0 && (src[0] == ' ' || src[0] == '\t' || src[0] == '\n' ||
933 	    src[0] == '\0' || src[0] == 0xff))
934 		++src, --len;
935 	while (len > 0 && (src[len-1] == ' ' || src[len-1] == '\t' ||
936 	    src[len-1] == '\n' || src[len-1] == '\0' || src[len-1] == 0xff))
937 		--len;
938 
939 	last = 0xff;
940 	while (len > 0) {
941 		switch (*src) {
942 		case ' ':
943 		case '\t':
944 		case '\n':
945 		case '\0':
946 		case 0xff:
947 			/* Collapse whitespace and NULs to a single space. */
948 			if (last != ' ')
949 				*dst++ = ' ';
950 			last = ' ';
951 			break;
952 		case '\\':
953 			/* Quote backslashes. */
954 			*dst++ = '\\';
955 			*dst++ = '\\';
956 			last = '\\';
957 			break;
958 		default:
959 			if (*src < 0x20 || *src >= 0x80) {
960 				/* Non-printable characters to octal. */
961 				*dst++ = '\\';
962 				*dst++ = ((*src & 0300) >> 6) + '0';
963 				*dst++ = ((*src & 0070) >> 3) + '0';
964 				*dst++ = ((*src & 0007) >> 0) + '0';
965 			} else {
966 				/* Copy normal characters. */
967 				*dst++ = *src;
968 			}
969 			last = *src;
970 			break;
971 		}
972 		++src, --len;
973 	}
974 
975 	*dst++ = 0;
976 }
977 
978 void
979 scsi_print_link(struct scsi_link *link)
980 {
981 	char				 visbuf[65];
982 	struct scsi_inquiry_data	*inqbuf;
983 	u_int8_t			*id;
984 	int				 i;
985 
986 	printf(" targ %d lun %d: ", link->target, link->lun);
987 
988 	inqbuf = &link->inqdata;
989 
990 	scsi_strvis(visbuf, inqbuf->vendor, 8);
991 	printf("<%s, ", visbuf);
992 	scsi_strvis(visbuf, inqbuf->product, 16);
993 	printf("%s, ", visbuf);
994 	scsi_strvis(visbuf, inqbuf->revision, 4);
995 	printf("%s>", visbuf);
996 
997 #ifdef SCSIDEBUG
998 	if (ISSET(link->flags, SDEV_ATAPI))
999 		printf(" ATAPI");
1000 	else if (SID_ANSII_REV(inqbuf) < SCSI_REV_SPC)
1001 		printf(" SCSI/%d", SID_ANSII_REV(inqbuf));
1002 	else if (SID_ANSII_REV(inqbuf) == SCSI_REV_SPC)
1003 		printf(" SCSI/SPC");
1004 	else
1005 		printf(" SCSI/SPC-%d", SID_ANSII_REV(inqbuf) - 2);
1006 #endif /* SCSIDEBUG */
1007 
1008 	if (ISSET(link->flags, SDEV_REMOVABLE))
1009 		printf(" removable");
1010 
1011 	if (link->id != NULL && link->id->d_type != DEVID_NONE) {
1012 		id = (u_int8_t *)(link->id + 1);
1013 		switch (link->id->d_type) {
1014 		case DEVID_NAA:
1015 			printf(" naa.");
1016 			break;
1017 		case DEVID_EUI:
1018 			printf(" eui.");
1019 			break;
1020 		case DEVID_T10:
1021 			printf(" t10.");
1022 			break;
1023 		case DEVID_SERIAL:
1024 			printf(" serial.");
1025 			break;
1026 		case DEVID_WWN:
1027 			printf(" wwn.");
1028 			break;
1029 		}
1030 
1031 		if (ISSET(link->id->d_flags, DEVID_F_PRINT)) {
1032 			for (i = 0; i < link->id->d_len; i++) {
1033 				if (id[i] == '\0' || id[i] == ' ') {
1034 					/* skip leading blanks */
1035 					/* collapse multiple blanks into one */
1036 					if (i > 0 && id[i-1] != id[i])
1037 						printf("_");
1038 				} else if (id[i] < 0x20 || id[i] >= 0x80) {
1039 					/* non-printable characters */
1040 					printf("~");
1041 				} else {
1042 					/* normal characters */
1043 					printf("%c", id[i]);
1044 				}
1045 			}
1046 		} else {
1047 			for (i = 0; i < link->id->d_len; i++)
1048 				printf("%02x", id[i]);
1049 		}
1050 	}
1051 #ifdef SCSIDEBUG
1052 	printf("\n");
1053 	sc_print_addr(link);
1054 	printf("state %u, luns %u, openings %u\n",
1055 	    link->state, link->bus->sb_luns, link->openings);
1056 
1057 	sc_print_addr(link);
1058 	printf("flags (0x%04x) ", link->flags);
1059 	scsi_show_flags(link->flags, flagnames);
1060 	printf("\n");
1061 
1062 	sc_print_addr(link);
1063 	printf("quirks (0x%04x) ", link->quirks);
1064 	scsi_show_flags(link->quirks, quirknames);
1065 #endif /* SCSIDEBUG */
1066 }
1067 
1068 /*
1069  * Return a priority based on how much of the inquiry data matches
1070  * the patterns for the particular driver.
1071  */
1072 const void *
1073 scsi_inqmatch(struct scsi_inquiry_data *inqbuf, const void *_base,
1074     int nmatches, int matchsize, int *bestpriority)
1075 {
1076 	const unsigned char		*base = (const unsigned char *)_base;
1077 	const void			*bestmatch;
1078 	int				 removable;
1079 
1080 	/* Include the qualifier to catch vendor-unique types. */
1081 	removable = ISSET(inqbuf->dev_qual2, SID_REMOVABLE) ? T_REMOV : T_FIXED;
1082 
1083 	for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) {
1084 		struct scsi_inquiry_pattern *match = (void *)base;
1085 		int priority, len;
1086 
1087 		if (inqbuf->device != match->type)
1088 			continue;
1089 		if (removable != match->removable)
1090 			continue;
1091 		priority = 2;
1092 		len = strlen(match->vendor);
1093 		if (bcmp(inqbuf->vendor, match->vendor, len))
1094 			continue;
1095 		priority += len;
1096 		len = strlen(match->product);
1097 		if (bcmp(inqbuf->product, match->product, len))
1098 			continue;
1099 		priority += len;
1100 		len = strlen(match->revision);
1101 		if (bcmp(inqbuf->revision, match->revision, len))
1102 			continue;
1103 		priority += len;
1104 
1105 #ifdef SCSIDEBUG
1106 		printf("scsi_inqmatch: ");
1107 		if (_base == &scsi_quirk_patterns)
1108 			printf(" quirk ");
1109 		else
1110 			printf(" match ");
1111 
1112 		printf("priority %d. %s %s <\"%s\", \"%s\", \"%s\">", priority,
1113 		    devicetypenames[(match->type & SID_TYPE)],
1114 		    (match->removable == T_FIXED) ? "T_FIXED" : "T_REMOV",
1115 		    match->vendor, match->product, match->revision);
1116 
1117 		if (_base == &scsi_quirk_patterns)
1118 			printf(" quirks: 0x%04x",
1119 			    ((struct scsi_quirk_inquiry_pattern *)match)->quirks
1120 			);
1121 
1122 		printf("\n");
1123 #endif /* SCSIDEBUG */
1124 		if (priority > *bestpriority) {
1125 			*bestpriority = priority;
1126 			bestmatch = base;
1127 		}
1128 	}
1129 
1130 	return bestmatch;
1131 }
1132 
1133 void
1134 scsi_devid(struct scsi_link *link)
1135 {
1136 	struct {
1137 		struct scsi_vpd_hdr hdr;
1138 		u_int8_t list[32];
1139 	} __packed			*pg;
1140 	size_t				 len;
1141 	int				 pg80 = 0, pg83 = 0, i;
1142 
1143 	if (link->id != NULL)
1144 		return;
1145 
1146 	pg = dma_alloc(sizeof(*pg), PR_WAITOK | PR_ZERO);
1147 
1148 	if (SID_ANSII_REV(&link->inqdata) >= SCSI_REV_2) {
1149 		if (scsi_inquire_vpd(link, pg, sizeof(*pg), SI_PG_SUPPORTED,
1150 		    scsi_autoconf) != 0)
1151 			goto wwn;
1152 
1153 		len = MIN(sizeof(pg->list), _2btol(pg->hdr.page_length));
1154 		for (i = 0; i < len; i++) {
1155 			switch (pg->list[i]) {
1156 			case SI_PG_SERIAL:
1157 				pg80 = 1;
1158 				break;
1159 			case SI_PG_DEVID:
1160 				pg83 = 1;
1161 				break;
1162 			}
1163 		}
1164 
1165 		if (pg83 && scsi_devid_pg83(link) == 0)
1166 			goto done;
1167 		if (pg80 && scsi_devid_pg80(link) == 0)
1168 			goto done;
1169 	}
1170 
1171 wwn:
1172 	scsi_devid_wwn(link);
1173 done:
1174 	dma_free(pg, sizeof(*pg));
1175 }
1176 
1177 int
1178 scsi_devid_pg83(struct scsi_link *link)
1179 {
1180 	struct scsi_vpd_devid_hdr	 dhdr, chdr;
1181 	struct scsi_vpd_hdr		*hdr = NULL;
1182 	u_int8_t			*pg = NULL, *id;
1183 	int				 len, pos, rv, type;
1184 	int				 idtype = 0;
1185 	u_char				 idflags;
1186 
1187 	hdr = dma_alloc(sizeof(*hdr), PR_WAITOK | PR_ZERO);
1188 
1189 	rv = scsi_inquire_vpd(link, hdr, sizeof(*hdr), SI_PG_DEVID,
1190 	    scsi_autoconf);
1191 	if (rv != 0)
1192 		goto done;
1193 
1194 	len = sizeof(*hdr) + _2btol(hdr->page_length);
1195 	pg = dma_alloc(len, PR_WAITOK | PR_ZERO);
1196 
1197 	rv = scsi_inquire_vpd(link, pg, len, SI_PG_DEVID, scsi_autoconf);
1198 	if (rv != 0)
1199 		goto done;
1200 
1201 	pos = sizeof(*hdr);
1202 
1203 	do {
1204 		if (len - pos < sizeof(dhdr)) {
1205 			rv = EIO;
1206 			goto done;
1207 		}
1208 		memcpy(&dhdr, &pg[pos], sizeof(dhdr));
1209 		pos += sizeof(dhdr);
1210 		if (len - pos < dhdr.len) {
1211 			rv = EIO;
1212 			goto done;
1213 		}
1214 
1215 		if (VPD_DEVID_ASSOC(dhdr.flags) == VPD_DEVID_ASSOC_LU) {
1216 			type = VPD_DEVID_TYPE(dhdr.flags);
1217 			switch (type) {
1218 			case VPD_DEVID_TYPE_NAA:
1219 			case VPD_DEVID_TYPE_EUI64:
1220 			case VPD_DEVID_TYPE_T10:
1221 				if (type >= idtype) {
1222 					idtype = type;
1223 
1224 					chdr = dhdr;
1225 					id = &pg[pos];
1226 				}
1227 				break;
1228 
1229 			default:
1230 				/* skip */
1231 				break;
1232 			}
1233 		}
1234 
1235 		pos += dhdr.len;
1236 	} while (idtype != VPD_DEVID_TYPE_NAA && len != pos);
1237 
1238 	if (idtype > 0) {
1239 		switch (VPD_DEVID_TYPE(chdr.flags)) {
1240 		case VPD_DEVID_TYPE_NAA:
1241 			idtype = DEVID_NAA;
1242 			break;
1243 		case VPD_DEVID_TYPE_EUI64:
1244 			idtype = DEVID_EUI;
1245 			break;
1246 		case VPD_DEVID_TYPE_T10:
1247 			idtype = DEVID_T10;
1248 			break;
1249 		}
1250 		switch (VPD_DEVID_CODE(chdr.pi_code)) {
1251 		case VPD_DEVID_CODE_ASCII:
1252 		case VPD_DEVID_CODE_UTF8:
1253 			idflags = DEVID_F_PRINT;
1254 			break;
1255 		default:
1256 			idflags = 0;
1257 			break;
1258 		}
1259 		link->id = devid_alloc(idtype, idflags, chdr.len, id);
1260 	} else
1261 		rv = ENODEV;
1262 
1263 done:
1264 	if (pg)
1265 		dma_free(pg, len);
1266 	if (hdr)
1267 		dma_free(hdr, sizeof(*hdr));
1268 	return rv;
1269 }
1270 
1271 int
1272 scsi_devid_pg80(struct scsi_link *link)
1273 {
1274 	struct scsi_vpd_hdr		*hdr = NULL;
1275 	u_int8_t			*pg = NULL;
1276 	char				*id;
1277 	size_t				 idlen;
1278 	int				 len, pglen, rv;
1279 
1280 	hdr = dma_alloc(sizeof(*hdr), PR_WAITOK | PR_ZERO);
1281 
1282 	rv = scsi_inquire_vpd(link, hdr, sizeof(*hdr), SI_PG_SERIAL,
1283 	    scsi_autoconf);
1284 	if (rv != 0)
1285 		goto freehdr;
1286 
1287 	len = _2btol(hdr->page_length);
1288 	if (len == 0) {
1289 		rv = EINVAL;
1290 		goto freehdr;
1291 	}
1292 
1293 	pglen = sizeof(*hdr) + len;
1294 	pg = dma_alloc(pglen, PR_WAITOK | PR_ZERO);
1295 
1296 	rv = scsi_inquire_vpd(link, pg, pglen, SI_PG_SERIAL, scsi_autoconf);
1297 	if (rv != 0)
1298 		goto free;
1299 
1300 	idlen = sizeof(link->inqdata.vendor) +
1301 	    sizeof(link->inqdata.product) + len;
1302 	id = malloc(idlen, M_TEMP, M_WAITOK);
1303 	memcpy(id, link->inqdata.vendor, sizeof(link->inqdata.vendor));
1304 	memcpy(id + sizeof(link->inqdata.vendor), link->inqdata.product,
1305 	    sizeof(link->inqdata.product));
1306 	memcpy(id + sizeof(link->inqdata.vendor) +
1307 	    sizeof(link->inqdata.product), pg + sizeof(*hdr), len);
1308 
1309 	link->id = devid_alloc(DEVID_SERIAL, DEVID_F_PRINT,
1310 	    sizeof(link->inqdata.vendor) + sizeof(link->inqdata.product) + len,
1311 	    id);
1312 
1313 	free(id, M_TEMP, idlen);
1314 
1315 free:
1316 	dma_free(pg, pglen);
1317 freehdr:
1318 	dma_free(hdr, sizeof(*hdr));
1319 	return rv;
1320 }
1321 
1322 int
1323 scsi_devid_wwn(struct scsi_link *link)
1324 {
1325 	u_int64_t wwnn;
1326 
1327 	if (link->lun != 0 || link->node_wwn == 0)
1328 		return EOPNOTSUPP;
1329 
1330 	wwnn = htobe64(link->node_wwn);
1331 	link->id = devid_alloc(DEVID_WWN, 0, sizeof(wwnn), (u_int8_t *)&wwnn);
1332 
1333 	return 0;
1334 }
1335 
1336 struct devid *
1337 devid_alloc(u_int8_t type, u_int8_t flags, u_int8_t len, u_int8_t *id)
1338 {
1339 	struct devid *d;
1340 
1341 	d = malloc(sizeof(*d) + len, M_DEVBUF, M_WAITOK|M_CANFAIL);
1342 	if (d == NULL)
1343 		return NULL;
1344 
1345 	d->d_type = type;
1346 	d->d_flags = flags;
1347 	d->d_len = len;
1348 	d->d_refcount = 1;
1349 	memcpy(d + 1, id, len);
1350 
1351 	return d;
1352 }
1353 
1354 struct devid *
1355 devid_copy(struct devid *d)
1356 {
1357 	d->d_refcount++;
1358 	return d;
1359 }
1360 
1361 void
1362 devid_free(struct devid *d)
1363 {
1364 	if (--d->d_refcount == 0)
1365 		free(d, M_DEVBUF, sizeof(*d) + d->d_len);
1366 }
1367