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