summaryrefslogtreecommitdiff
path: root/sys/scsi/scsiconf.c
blob: 92dfba2793fe9b46a2f3208837f5895357f3856a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
/*	$OpenBSD: scsiconf.c,v 1.238 2021/10/24 16:57:30 mpi Exp $	*/
/*	$NetBSD: scsiconf.c,v 1.57 1996/05/02 01:09:01 neil Exp $	*/

/*
 * Copyright (c) 1994 Charles Hannum.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Charles Hannum.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Originally written by Julian Elischer (julian@tfs.com)
 * for TRW Financial Systems for use under the MACH(2.5) operating system.
 *
 * TRW Financial Systems, in accordance with their agreement with Carnegie
 * Mellon University, makes this software available to CMU to distribute
 * or use in any manner that they see fit as long as this message is kept with
 * the software. For this reason TFS also grants any other persons or
 * organisations permission to use or modify this software.
 *
 * TFS supplies this software to be publicly redistributed
 * on the understanding that TFS is not responsible for the correct
 * functioning of this software in any circumstances.
 *
 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
 */

#include "bio.h"
#include "mpath.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/pool.h>
#include <sys/device.h>
#include <sys/buf.h>
#include <sys/atomic.h>

#include <scsi/scsi_all.h>
#include <scsi/scsi_debug.h>
#include <scsi/scsiconf.h>

int	scsibusmatch(struct device *, void *, void *);
void	scsibusattach(struct device *, struct device *, void *);
int	scsibusactivate(struct device *, int);
int	scsibusdetach(struct device *, int);
int	scsibussubmatch(struct device *, void *, void *);
int	scsibussubprint(void *, const char *);
#if NBIO > 0
#include <sys/ioctl.h>
#include <sys/scsiio.h>
#include <dev/biovar.h>
int	scsibusbioctl(struct device *, u_long, caddr_t);
#endif /* NBIO > 0 */

void	scsi_get_target_luns(struct scsi_link *, struct scsi_lun_array *);
void	scsi_add_link(struct scsi_link *);
void	scsi_remove_link(struct scsi_link *);
void	scsi_print_link(struct scsi_link *);
int	scsi_probe_link(struct scsibus_softc *, int, int, int);
int	scsi_activate_link(struct scsi_link *, int);
int	scsi_detach_link(struct scsi_link *, int);
int	scsi_detach_bus(struct scsibus_softc *, int);

void	scsi_devid(struct scsi_link *);
int	scsi_devid_pg80(struct scsi_link *);
int	scsi_devid_pg83(struct scsi_link *);
int	scsi_devid_wwn(struct scsi_link *);

int	scsi_activate_bus(struct scsibus_softc *, int);
int	scsi_activate_target(struct scsibus_softc *, int, int);
int	scsi_activate_lun(struct scsibus_softc *, int, int, int);

int	scsi_autoconf = SCSI_AUTOCONF;

const struct cfattach scsibus_ca = {
	sizeof(struct scsibus_softc), scsibusmatch, scsibusattach,
	scsibusdetach, scsibusactivate
};

struct cfdriver scsibus_cd = {
	NULL, "scsibus", DV_DULL
};

struct scsi_quirk_inquiry_pattern {
	struct scsi_inquiry_pattern	pattern;
	u_int16_t			quirks;
};

const struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
	{{T_CDROM, T_REMOV,
	 "PLEXTOR", "CD-ROM PX-40TS", "1.01"},    SDEV_NOSYNC},

	{{T_DIRECT, T_FIXED,
	 "MICROP  ", "1588-15MBSUN0669", ""},     SDEV_AUTOSAVE},
	{{T_DIRECT, T_FIXED,
	 "DEC     ", "RZ55     (C) DEC", ""},     SDEV_AUTOSAVE},
	{{T_DIRECT, T_FIXED,
	 "EMULEX  ", "MD21/S2     ESDI", "A00"},  SDEV_AUTOSAVE},
	{{T_DIRECT, T_FIXED,
	 "IBMRAID ", "0662S",            ""},     SDEV_AUTOSAVE},
	{{T_DIRECT, T_FIXED,
	 "IBM     ", "0663H",            ""},     SDEV_AUTOSAVE},
	{{T_DIRECT, T_FIXED,
	 "IBM",	  "0664",		 ""},	  SDEV_AUTOSAVE},
	{{T_DIRECT, T_FIXED,
	 "IBM     ", "H3171-S2",         ""},	  SDEV_AUTOSAVE},
	{{T_DIRECT, T_FIXED,
	 "IBM     ", "KZ-C",		 ""},	  SDEV_AUTOSAVE},
	/* Broken IBM disk */
	{{T_DIRECT, T_FIXED,
	 ""	   , "DFRSS2F",		 ""},	  SDEV_AUTOSAVE},
	{{T_DIRECT, T_FIXED,
	 "QUANTUM ", "ELS85S          ", ""},	  SDEV_AUTOSAVE},
	{{T_DIRECT, T_REMOV,
	 "iomega", "jaz 1GB",		 ""},	  SDEV_NOTAGS},
	{{T_DIRECT, T_FIXED,
	 "MICROP", "4421-07",		 ""},     SDEV_NOTAGS},
	{{T_DIRECT, T_FIXED,
	 "SEAGATE", "ST150176LW",        "0002"}, SDEV_NOTAGS},
	{{T_DIRECT, T_FIXED,
	 "HP", "C3725S",		 ""},     SDEV_NOTAGS},
	{{T_DIRECT, T_FIXED,
	 "IBM", "DCAS",			 ""},     SDEV_NOTAGS},

	{{T_SEQUENTIAL, T_REMOV,
	 "SONY    ", "SDT-5000        ", "3."},   SDEV_NOSYNC|SDEV_NOWIDE},
	{{T_SEQUENTIAL, T_REMOV,
	 "WangDAT ", "Model 1300      ", "02.4"}, SDEV_NOSYNC|SDEV_NOWIDE},
	{{T_SEQUENTIAL, T_REMOV,
	 "WangDAT ", "Model 2600      ", "01.7"}, SDEV_NOSYNC|SDEV_NOWIDE},
	{{T_SEQUENTIAL, T_REMOV,
	 "WangDAT ", "Model 3200      ", "02.2"}, SDEV_NOSYNC|SDEV_NOWIDE},

	/* ATAPI device quirks */
	{{T_CDROM, T_REMOV,
	 "CR-2801TE", "", "1.07"},              ADEV_NOSENSE},
	{{T_CDROM, T_REMOV,
	 "CREATIVECD3630E", "", "AC101"},       ADEV_NOSENSE},
	{{T_CDROM, T_REMOV,
	 "FX320S", "", "q01"},                  ADEV_NOSENSE},
	{{T_CDROM, T_REMOV,
	 "GCD-R580B", "", "1.00"},              ADEV_LITTLETOC},
	{{T_CDROM, T_REMOV,
	 "MATSHITA CR-574", "", "1.02"},        ADEV_NOCAPACITY},
	{{T_CDROM, T_REMOV,
	 "MATSHITA CR-574", "", "1.06"},        ADEV_NOCAPACITY},
	{{T_CDROM, T_REMOV,
	 "Memorex CRW-2642", "", "1.0g"},       ADEV_NOSENSE},
	{{T_CDROM, T_REMOV,
	 "SANYO CRD-256P", "", "1.02"},         ADEV_NOCAPACITY},
	{{T_CDROM, T_REMOV,
	 "SANYO CRD-254P", "", "1.02"},         ADEV_NOCAPACITY},
	{{T_CDROM, T_REMOV,
	 "SANYO CRD-S54P", "", "1.08"},         ADEV_NOCAPACITY},
	{{T_CDROM, T_REMOV,
	 "CD-ROM  CDR-S1", "", "1.70"},         ADEV_NOCAPACITY}, /* Sanyo */
	{{T_CDROM, T_REMOV,
	 "CD-ROM  CDR-N16", "", "1.25"},        ADEV_NOCAPACITY}, /* Sanyo */
	{{T_CDROM, T_REMOV,
	 "UJDCD8730", "", "1.14"},              ADEV_NODOORLOCK}, /* Acer */
};

int
scsiprint(void *aux, const char *pnp)
{
	/* Only "scsibus"es can attach to "scsi"s. */
	if (pnp)
		printf("scsibus at %s", pnp);

	return UNCONF;
}

int
scsibusmatch(struct device *parent, void *match, void *aux)
{
	return 1;
}

/*
 * The routine called by the adapter boards to get all their
 * devices configured in.
 */
void
scsibusattach(struct device *parent, struct device *self, void *aux)
{
	struct scsibus_softc		*sb = (struct scsibus_softc *)self;
	struct scsibus_attach_args	*saa = aux;

	if (!cold)
		scsi_autoconf = 0;

	SLIST_INIT(&sb->sc_link_list);
	sb->sb_adapter_softc = saa->saa_adapter_softc;
	sb->sb_adapter = saa->saa_adapter;
	sb->sb_pool = saa->saa_pool;
	sb->sb_quirks = saa->saa_quirks;
	sb->sb_flags = saa->saa_flags;
	sb->sb_openings = saa->saa_openings;
	sb->sb_adapter_buswidth = saa->saa_adapter_buswidth;
	sb->sb_adapter_target = saa->saa_adapter_target;
	sb->sb_luns = saa->saa_luns;

	if (sb->sb_adapter_buswidth == 0)
		sb->sb_adapter_buswidth = 8;
	if (sb->sb_luns == 0)
		sb->sb_luns = 8;

	printf(": %d targets", sb->sb_adapter_buswidth);
	if (sb->sb_adapter_target < sb->sb_adapter_buswidth)
		printf(", initiator %d", sb->sb_adapter_target);
	if (saa->saa_wwpn != 0x0 && saa->saa_wwnn != 0x0) {
		printf(", WWPN %016llx, WWNN %016llx", saa->saa_wwpn,
		    saa->saa_wwnn);
	}
	printf("\n");

	/* Initialize shared data. */
	scsi_init();

	SLIST_INIT(&sb->sc_link_list);

#if NBIO > 0
	if (bio_register(&sb->sc_dev, scsibusbioctl) != 0)
		printf("%s: unable to register bio\n", sb->sc_dev.dv_xname);
#endif /* NBIO > 0 */

	scsi_probe_bus(sb);
}

int
scsibusactivate(struct device *dev, int act)
{
	struct scsibus_softc *sb = (struct scsibus_softc *)dev;

	return scsi_activate_bus(sb, act);
}

int
scsibusdetach(struct device *dev, int type)
{
	struct scsibus_softc		*sb = (struct scsibus_softc *)dev;
	int				 error;

#if NBIO > 0
	bio_unregister(&sb->sc_dev);
#endif /* NBIO > 0 */

	error = scsi_detach_bus(sb, type);
	if (error != 0)
		return error;

	KASSERT(SLIST_EMPTY(&sb->sc_link_list));

	return 0;
}

int
scsibussubmatch(struct device *parent, void *match, void *aux)
{
	struct cfdata			*cf = match;
	struct scsi_attach_args		*sa = aux;
	struct scsi_link		*link = sa->sa_sc_link;

	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != link->target)
		return 0;
	if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != link->lun)
		return 0;

	return (*cf->cf_attach->ca_match)(parent, match, aux);
}

/*
 * Print out autoconfiguration information for a subdevice.
 *
 * This is a slight abuse of 'standard' autoconfiguration semantics,
 * because 'print' functions don't normally print the colon and
 * device information.  However, in this case that's better than
 * either printing redundant information before the attach message,
 * or having the device driver call a special function to print out
 * the standard device information.
 */
int
scsibussubprint(void *aux, const char *pnp)
{
	struct scsi_attach_args		*sa = aux;

	if (pnp != NULL)
		printf("%s", pnp);

	scsi_print_link(sa->sa_sc_link);

	return UNCONF;
}

#if NBIO > 0
int
scsibusbioctl(struct device *dev, u_long cmd, caddr_t addr)
{
	struct scsibus_softc		*sb = (struct scsibus_softc *)dev;
	struct sbioc_device		*sdev;

	switch (cmd) {
	case SBIOCPROBE:
		sdev = (struct sbioc_device *)addr;
		return scsi_probe(sb, sdev->sd_target, sdev->sd_lun);

	case SBIOCDETACH:
		sdev = (struct sbioc_device *)addr;
		return scsi_detach(sb, sdev->sd_target, sdev->sd_lun, 0);

	default:
		return ENOTTY;
	}
}
#endif /* NBIO > 0 */

int
scsi_activate(struct scsibus_softc *sb, int target, int lun, int act)
{
	if (target == -1 && lun == -1)
		return scsi_activate_bus(sb, act);
	else if (lun == -1)
		return scsi_activate_target(sb, target, act);
	else
		return scsi_activate_lun(sb, target, lun, act);
}

int
scsi_activate_bus(struct scsibus_softc *sb, int act)
{
	struct scsi_link		*link;
	int				 r, rv = 0;

	/*  Activate all links on the bus. */
	SLIST_FOREACH(link, &sb->sc_link_list, bus_list) {
		r = scsi_activate_link(link, act);
		if (r)
			rv = r;
	}
	return rv;
}

int
scsi_activate_target(struct scsibus_softc *sb, int target, int act)
{
	struct scsi_link		*link;
	int				 r, rv = 0;

	/*  Activate all links on the target. */
	SLIST_FOREACH(link, &sb->sc_link_list, bus_list) {
		if (link->target == target) {
			r = scsi_activate_link(link, act);
			if (r)
				rv = r;
		}
	}
	return rv;
}

int
scsi_activate_lun(struct scsibus_softc *sb, int target, int lun, int act)
{
	struct scsi_link *link;

	/*  Activate the (target, lun) link.*/
	link = scsi_get_link(sb, target, lun);
	if (link == NULL)
		return 0;

	return scsi_activate_link(link, act);
}

int
scsi_activate_link(struct scsi_link *link, int act)
{
	struct device			*dev;
	int				 rv = 0;

	dev = link->device_softc;
	switch (act) {
	case DVACT_DEACTIVATE:
		atomic_setbits_int(&link->state, SDEV_S_DYING);
		config_deactivate(dev);
		break;
	default:
		rv = config_suspend(dev, act);
		break;
	}
	return rv;
}

int
scsi_probe(struct scsibus_softc *sb, int target, int lun)
{
	if (target == -1 && lun == -1)
		return scsi_probe_bus(sb);
	else if (lun == -1)
		return scsi_probe_target(sb, target);
	else
		return scsi_probe_lun(sb, target, lun);
}

int
scsi_probe_bus(struct scsibus_softc *sb)
{
	int		target, r, rv = 0;

	/* Probe all possible targets on bus. */
	for (target = 0; target < sb->sb_adapter_buswidth; target++) {
		r = scsi_probe_target(sb, target);
		if (r != 0 && r != EINVAL)
			rv = r;
	}
	return rv;
}

int
scsi_probe_target(struct scsibus_softc *sb, int target)
{
	struct scsi_lun_array		 lunarray;
	struct scsi_link		*link0;
	int				 i, r, rv = 0;

	if (target < 0 || target == sb->sb_adapter_target)
		return EINVAL;

	/* Probe all possible luns on target. */
	scsi_probe_link(sb, target, 0, 0);
	link0 = scsi_get_link(sb, target, 0);
	if (link0 == NULL)
		return EINVAL;
	scsi_get_target_luns(link0, &lunarray);
	for (i = 0; i < lunarray.count; i++) {
		r = scsi_probe_link(sb, target, lunarray.luns[i],
		    lunarray.dumbscan);
		if (r == EINVAL && lunarray.dumbscan == 1)
			return 0;
		if (r != 0 && r != EINVAL)
			rv = r;
	}
	return rv;
}

int
scsi_probe_lun(struct scsibus_softc *sb, int target, int lun)
{
	if (target < 0 || target == sb->sb_adapter_target || lun < 0)
		return EINVAL;

	/* Probe lun on target. *NOT* a dumbscan! */
	return scsi_probe_link(sb, target, lun, 0);
}

/*
 * Given a target and lun, ask the device what it is, and find the correct
 * driver table entry.
 *
 * Return 0 if further LUNs are possible, EINVAL if not.
 */
int
scsi_probe_link(struct scsibus_softc *sb, int target, int lun, int dumbscan)
{
	struct scsi_attach_args			 sa;
	const struct scsi_quirk_inquiry_pattern	*finger;
	struct scsi_inquiry_data		*inqbuf, *usbinqbuf;
	struct scsi_link			*link, *link0;
	struct cfdata				*cf;
	int					 inqbytes, priority, rslt = 0;
	u_int16_t				 devquirks;

	/* Skip this slot if it is already attached and try the next LUN. */
	if (scsi_get_link(sb, target, lun) != NULL)
		return 0;

	link = malloc(sizeof(*link), M_DEVBUF, M_NOWAIT);
	if (link == NULL) {
		SC_DEBUG(link, SDEV_DB2, ("Bad LUN. can't allocate "
		    "scsi_link.\n"));
		return EINVAL;
	}

	link->state = 0;
	link->target = target;
	link->lun = lun;
	link->openings = sb->sb_openings;
	link->node_wwn = link->port_wwn = 0;
	link->flags = sb->sb_flags;
	link->quirks = sb->sb_quirks;
	link->interpret_sense = scsi_interpret_sense;
	link->device_softc = NULL;
	link->bus = sb;
	memset(&link->inqdata, 0, sizeof(link->inqdata));
	link->id = NULL;
	TAILQ_INIT(&link->queue);
	link->running = 0;
	link->pending = 0;
	link->pool = sb->sb_pool;

	SC_DEBUG(link, SDEV_DB2, ("scsi_link created.\n"));

	/* Ask the adapter if this will be a valid device. */
	if (sb->sb_adapter->dev_probe != NULL &&
	    sb->sb_adapter->dev_probe(link) != 0) {
		if (lun == 0) {
			SC_DEBUG(link, SDEV_DB2, ("Bad LUN 0. dev_probe() "
			    "failed.\n"));
			rslt = EINVAL;
		}
		goto free;
	}

	/*
	 * If we havent been given an io pool by now then fall back to
	 * using link->openings.
	 */
	if (link->pool == NULL) {
		link->pool = malloc(sizeof(*link->pool),
		    M_DEVBUF, M_NOWAIT);
		if (link->pool == NULL) {
			SC_DEBUG(link, SDEV_DB2, ("Bad LUN. can't allocate "
			    "link->pool.\n"));
			rslt = ENOMEM;
			goto bad;
		}
		scsi_iopool_init(link->pool, link,
		    scsi_default_get, scsi_default_put);

		SET(link->flags, SDEV_OWN_IOPL);
	}

	/*
	 * Tell drivers that are paying attention to avoid sync/wide/tags until
	 * INQUIRY data has been processed and the quirks information is
	 * complete. Some drivers set bits in quirks before we get here, so
	 * just add NOTAGS, NOWIDE and NOSYNC.
	 */
	devquirks = link->quirks;
	SET(link->quirks, SDEV_NOSYNC | SDEV_NOWIDE | SDEV_NOTAGS);

	/*
	 * Ask the device what it is.
	 */
#ifdef SCSIDEBUG
	if (((sb->sc_dev.dv_unit < 32) &&
	    ((1U << sb->sc_dev.dv_unit) & scsidebug_buses)) &&
	    ((target < 32) && ((1U << target) & scsidebug_targets)) &&
	    ((lun < 32) && ((1U << lun) & scsidebug_luns)))
		SET(link->flags, scsidebug_level);
#endif /* SCSIDEBUG */

	if (lun == 0) {
		/* Clear any outstanding errors. */
		scsi_test_unit_ready(link, TEST_READY_RETRIES,
		    scsi_autoconf | SCSI_IGNORE_ILLEGAL_REQUEST |
		    SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE);
	}

	/* Now go ask the device all about itself. */
	inqbuf = dma_alloc(sizeof(*inqbuf), PR_NOWAIT | PR_ZERO);
	if (inqbuf == NULL) {
		SC_DEBUG(link, SDEV_DB2, ("Bad LUN. can't allocate inqbuf.\n"));
		rslt = ENOMEM;
		goto bad;
	}

	rslt = scsi_inquire(link, inqbuf, scsi_autoconf | SCSI_SILENT);
	if (rslt != 0) {
		if (lun == 0) {
			SC_DEBUG(link, SDEV_DB2, ("Bad LUN 0. inquiry rslt = "
			    "%i\n", rslt));
			rslt = EINVAL;
		}
		dma_free(inqbuf, sizeof(*inqbuf));
		goto bad;
	}
	inqbytes = SID_SCSI2_HDRLEN + inqbuf->additional_length;
	memcpy(&link->inqdata, inqbuf, inqbytes);
	dma_free(inqbuf, sizeof(*inqbuf));
	inqbuf = &link->inqdata;
	if (inqbytes < offsetof(struct scsi_inquiry_data, vendor))
		memset(inqbuf->vendor, ' ', sizeof(inqbuf->vendor));
	if (inqbytes < offsetof(struct scsi_inquiry_data, product))
		memset(inqbuf->product, ' ', sizeof(inqbuf->product));
	if (inqbytes < offsetof(struct scsi_inquiry_data, revision))
		memset(inqbuf->revision, ' ', sizeof(inqbuf->revision));
	if (inqbytes < offsetof(struct scsi_inquiry_data, extra))
		memset(inqbuf->extra, ' ', sizeof(inqbuf->extra));

	switch (inqbuf->device & SID_QUAL) {
	case SID_QUAL_RSVD:
	case SID_QUAL_BAD_LU:
	case SID_QUAL_LU_OFFLINE:
		SC_DEBUG(link, SDEV_DB1, ("Bad LUN. SID_QUAL = 0x%02x\n",
		    inqbuf->device & SID_QUAL));
		goto bad;

	case SID_QUAL_LU_OK:
		break;

	default:
		SC_DEBUG(link, SDEV_DB1, ("Vendor-specific SID_QUAL = 0x%02x\n",
		    inqbuf->device & SID_QUAL));
		break;
	}

	if ((inqbuf->device & SID_TYPE) == T_NODEVICE) {
		SC_DEBUG(link, SDEV_DB1,
		    ("Bad LUN. SID_TYPE = T_NODEVICE\n"));
		goto bad;
	}

	scsi_devid(link);

	link0 = scsi_get_link(sb, target, 0);
	if (lun == 0 || link0 == NULL)
		;
	else if (ISSET(link->flags, SDEV_UMASS))
		;
	else if (link->id != NULL && !DEVID_CMP(link0->id, link->id))
		;
	else if (dumbscan == 1 && memcmp(inqbuf, &link0->inqdata,
	    sizeof(*inqbuf)) == 0) {
		/* The device doesn't distinguish between LUNs. */
		SC_DEBUG(link, SDEV_DB1, ("Bad LUN. IDENTIFY not supported."
		    "\n"));
		rslt = EINVAL;
		goto free_devid;
	}

	link->quirks = devquirks;	/* Restore what the device wanted. */

	finger = (const struct scsi_quirk_inquiry_pattern *)scsi_inqmatch(
	    inqbuf, scsi_quirk_patterns,
	    nitems(scsi_quirk_patterns),
	    sizeof(scsi_quirk_patterns[0]), &priority);
	if (priority != 0)
		SET(link->quirks, finger->quirks);

	switch (SID_ANSII_REV(inqbuf)) {
	case SCSI_REV_0:
	case SCSI_REV_1:
		SET(link->quirks, SDEV_NOTAGS | SDEV_NOSYNC | SDEV_NOWIDE |
		    SDEV_NOSYNCCACHE);
		break;
	case SCSI_REV_2:
	case SCSI_REV_SPC:
	case SCSI_REV_SPC2:
		if (!ISSET(inqbuf->flags, SID_CmdQue))
			SET(link->quirks, SDEV_NOTAGS);
		if (!ISSET(inqbuf->flags, SID_Sync))
			SET(link->quirks, SDEV_NOSYNC);
		if (!ISSET(inqbuf->flags, SID_WBus16))
			SET(link->quirks, SDEV_NOWIDE);
		break;
	case SCSI_REV_SPC3:
	case SCSI_REV_SPC4:
	case SCSI_REV_SPC5:
		/* By this time SID_Sync and SID_WBus16 were obsolete. */
		if (!ISSET(inqbuf->flags, SID_CmdQue))
			SET(link->quirks, SDEV_NOTAGS);
		break;
	default:
		break;
	}

	/*
	 * If the device can't use tags, >1 opening may confuse it.
	 */
	if (ISSET(link->quirks, SDEV_NOTAGS))
		link->openings = 1;

	/*
	 * note what BASIC type of device it is
	 */
	if (ISSET(inqbuf->dev_qual2, SID_REMOVABLE))
		SET(link->flags, SDEV_REMOVABLE);

	sa.sa_sc_link = link;

	if ((cf = config_search(scsibussubmatch, (struct device *)sb,
	    &sa)) == 0) {
		scsibussubprint(&sa, sb->sc_dev.dv_xname);
		printf(" not configured\n");
		goto free_devid;
	}

	/*
	 * Braindead USB devices, especially some x-in-1 media readers, try to
	 * 'help' by pretending any LUN is actually LUN 0 until they see a
	 * different LUN used in a command. So do an INQUIRY on LUN 1 at this
	 * point to prevent such helpfulness before it causes confusion.
	 */
	if (lun == 0 && ISSET(link->flags, SDEV_UMASS) &&
	    scsi_get_link(sb, target, 1) == NULL && sb->sb_luns > 1 &&
	    (usbinqbuf = dma_alloc(sizeof(*usbinqbuf), M_NOWAIT)) != NULL) {

		link->lun = 1;
		scsi_inquire(link, usbinqbuf, scsi_autoconf | SCSI_SILENT);
		link->lun = 0;

		dma_free(usbinqbuf, sizeof(*usbinqbuf));
	}

	scsi_add_link(link);

	/*
	 * Generate a TEST_UNIT_READY command. This gives drivers waiting for
	 * valid quirks data a chance to set wide/sync/tag options
	 * appropriately. It also clears any outstanding ACA conditions that
	 * INQUIRY may leave behind.
	 *
	 * Do this now so that any messages generated by config_attach() do not
	 * have negotiation messages inserted into their midst.
	 */
	scsi_test_unit_ready(link, TEST_READY_RETRIES,
	    scsi_autoconf | SCSI_IGNORE_ILLEGAL_REQUEST |
	    SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE);

	config_attach((struct device *)sb, cf, &sa, scsibussubprint);

	return 0;

free_devid:
	if (link->id)
		devid_free(link->id);
bad:
	if (ISSET(link->flags, SDEV_OWN_IOPL))
		free(link->pool, M_DEVBUF, sizeof(*link->pool));

	if (sb->sb_adapter->dev_free != NULL)
		sb->sb_adapter->dev_free(link);
free:
	free(link, M_DEVBUF, sizeof(*link));
	return rslt;
}

int
scsi_detach(struct scsibus_softc *sb, int target, int lun, int flags)
{
	if (target == -1 && lun == -1)
		return scsi_detach_bus(sb, flags);
	else if (lun == -1)
		return scsi_detach_target(sb, target, flags);
	else
		return scsi_detach_lun(sb, target, lun, flags);
}

int
scsi_detach_bus(struct scsibus_softc *sb, int flags)
{
	struct scsi_link	*link, *tmp;
	int			 r, rv = 0;

	/* Detach all links from bus. */
	SLIST_FOREACH_SAFE(link, &sb->sc_link_list, bus_list, tmp) {
		r = scsi_detach_link(link, flags);
		if (r != 0 && r != ENXIO)
			rv = r;
	}
	return rv;
}

int
scsi_detach_target(struct scsibus_softc *sb, int target, int flags)
{
	struct scsi_link	*link, *tmp;
	int			 r, rv = 0;

	/* Detach all links from target. */
	SLIST_FOREACH_SAFE(link, &sb->sc_link_list, bus_list, tmp) {
		if (link->target == target) {
			r = scsi_detach_link(link, flags);
			if (r != 0 && r != ENXIO)
				rv = r;
		}
	}
	return rv;
}

int
scsi_detach_lun(struct scsibus_softc *sb, int target, int lun, int flags)
{
	struct scsi_link	*link;

	/* Detach (target, lun) link. */
	link = scsi_get_link(sb, target, lun);
	if (link == NULL)
		return EINVAL;

	return scsi_detach_link(link, flags);
}

int
scsi_detach_link(struct scsi_link *link, int flags)
{
	struct scsibus_softc		*sb = link->bus;
	int				 rv;

	if (!ISSET(flags, DETACH_FORCE) && ISSET(link->flags, SDEV_OPEN))
		return EBUSY;

	/* Detaching a device from scsibus is a five step process. */

	/* 1. Wake up processes sleeping for an xs. */
	scsi_link_shutdown(link);

	/* 2. Detach the device. */
	rv = config_detach(link->device_softc, flags);

	if (rv != 0)
		return rv;

	/* 3. If it's using the openings io allocator, clean that up. */
	if (ISSET(link->flags, SDEV_OWN_IOPL)) {
		scsi_iopool_destroy(link->pool);
		free(link->pool, M_DEVBUF, sizeof(*link->pool));
	}

	/* 4. Free up its state in the adapter. */
	if (sb->sb_adapter->dev_free != NULL)
		sb->sb_adapter->dev_free(link);

	/* 5. Free up its state in the midlayer. */
	if (link->id != NULL)
		devid_free(link->id);
	scsi_remove_link(link);
	free(link, M_DEVBUF, sizeof(*link));

	return 0;
}

struct scsi_link *
scsi_get_link(struct scsibus_softc *sb, int target, int lun)
{
	struct scsi_link *link;

	SLIST_FOREACH(link, &sb->sc_link_list, bus_list) {
		if (link->target == target && link->lun == lun)
			return link;
	}

	return NULL;
}

void
scsi_add_link(struct scsi_link *link)
{
	SLIST_INSERT_HEAD(&link->bus->sc_link_list, link, bus_list);
}

void
scsi_remove_link(struct scsi_link *link)
{
	SLIST_REMOVE(&link->bus->sc_link_list, link, scsi_link, bus_list);
}

void
scsi_get_target_luns(struct scsi_link *link0, struct scsi_lun_array *lunarray)
{
	struct scsi_report_luns_data	*report;
	int				 i, nluns, rv = 0;

	/* Initialize dumbscan result. Just in case. */
	report = NULL;
	for (i = 0; i < link0->bus->sb_luns; i++)
		lunarray->luns[i] = i;
	lunarray->count = link0->bus->sb_luns;
	lunarray->dumbscan = 1;

	/*
	 * ATAPI, USB and pre-SPC (i.e. pre-SCSI-3) devices can't ask
	 * for a report of valid LUNs.
	 */
	if ((link0->flags & (SDEV_UMASS | SDEV_ATAPI)) != 0 ||
	    SID_ANSII_REV(&link0->inqdata) < SCSI_REV_SPC)
		goto dumbscan;

	report = dma_alloc(sizeof(*report), PR_WAITOK);
	if (report == NULL)
		goto dumbscan;

	rv = scsi_report_luns(link0, REPORT_NORMAL, report,
	    sizeof(*report), scsi_autoconf | SCSI_SILENT |
	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY |
	    SCSI_IGNORE_MEDIA_CHANGE, 10000);
	if (rv != 0)
		goto dumbscan;

	/*
	 * XXX In theory we should check if data is full, which
	 * would indicate it needs to be enlarged and REPORT
	 * LUNS tried again. Solaris tries up to 3 times with
	 * larger sizes for data.
	 */

	/* Return the reported Type-0 LUNs. Type-0 only! */
	lunarray->count = 0;
	lunarray->dumbscan = 0;
	nluns = _4btol(report->length) / RPL_LUNDATA_SIZE;
	for (i = 0; i < nluns; i++) {
		if (report->luns[i].lundata[0] != 0)
			continue;
		lunarray->luns[lunarray->count++] =
		    report->luns[i].lundata[RPL_LUNDATA_T0LUN];
	}

dumbscan:
	if (report != NULL)
		dma_free(report, sizeof(*report));
}

void
scsi_strvis(u_char *dst, u_char *src, int len)
{
	u_char				last;

	/* Trim leading and trailing whitespace and NULs. */
	while (len > 0 && (src[0] == ' ' || src[0] == '\t' || src[0] == '\n' ||
	    src[0] == '\0' || src[0] == 0xff))
		++src, --len;
	while (len > 0 && (src[len-1] == ' ' || src[len-1] == '\t' ||
	    src[len-1] == '\n' || src[len-1] == '\0' || src[len-1] == 0xff))
		--len;

	last = 0xff;
	while (len > 0) {
		switch (*src) {
		case ' ':
		case '\t':
		case '\n':
		case '\0':
		case 0xff:
			/* Collapse whitespace and NULs to a single space. */
			if (last != ' ')
				*dst++ = ' ';
			last = ' ';
			break;
		case '\\':
			/* Quote backslashes. */
			*dst++ = '\\';
			*dst++ = '\\';
			last = '\\';
			break;
		default:
			if (*src < 0x20 || *src >= 0x80) {
				/* Non-printable characters to octal. */
				*dst++ = '\\';
				*dst++ = ((*src & 0300) >> 6) + '0';
				*dst++ = ((*src & 0070) >> 3) + '0';
				*dst++ = ((*src & 0007) >> 0) + '0';
			} else {
				/* Copy normal characters. */
				*dst++ = *src;
			}
			last = *src;
			break;
		}
		++src, --len;
	}

	*dst++ = 0;
}

void
scsi_print_link(struct scsi_link *link)
{
	char				 visbuf[65];
	struct scsi_inquiry_data	*inqbuf;
	u_int8_t			*id;
	int				 i;

	printf(" targ %d lun %d: ", link->target, link->lun);

	inqbuf = &link->inqdata;

	scsi_strvis(visbuf, inqbuf->vendor, 8);
	printf("<%s, ", visbuf);
	scsi_strvis(visbuf, inqbuf->product, 16);
	printf("%s, ", visbuf);
	scsi_strvis(visbuf, inqbuf->revision, 4);
	printf("%s>", visbuf);

#ifdef SCSIDEBUG
	if (ISSET(link->flags, SDEV_ATAPI))
		printf(" ATAPI");
	else if (SID_ANSII_REV(inqbuf) < SCSI_REV_SPC)
		printf(" SCSI/%d", SID_ANSII_REV(inqbuf));
	else if (SID_ANSII_REV(inqbuf) == SCSI_REV_SPC)
		printf(" SCSI/SPC");
	else
		printf(" SCSI/SPC-%d", SID_ANSII_REV(inqbuf) - 2);
#endif /* SCSIDEBUG */

	if (ISSET(link->flags, SDEV_REMOVABLE))
		printf(" removable");

	if (link->id != NULL && link->id->d_type != DEVID_NONE) {
		id = (u_int8_t *)(link->id + 1);
		switch (link->id->d_type) {
		case DEVID_NAA:
			printf(" naa.");
			break;
		case DEVID_EUI:
			printf(" eui.");
			break;
		case DEVID_T10:
			printf(" t10.");
			break;
		case DEVID_SERIAL:
			printf(" serial.");
			break;
		case DEVID_WWN:
			printf(" wwn.");
			break;
		}

		if (ISSET(link->id->d_flags, DEVID_F_PRINT)) {
			for (i = 0; i < link->id->d_len; i++) {
				if (id[i] == '\0' || id[i] == ' ') {
					/* skip leading blanks */
					/* collapse multiple blanks into one */
					if (i > 0 && id[i-1] != id[i])
						printf("_");
				} else if (id[i] < 0x20 || id[i] >= 0x80) {
					/* non-printable characters */
					printf("~");
				} else {
					/* normal characters */
					printf("%c", id[i]);
				}
			}
		} else {
			for (i = 0; i < link->id->d_len; i++)
				printf("%02x", id[i]);
		}
	}
#ifdef SCSIDEBUG
	printf("\n");
	sc_print_addr(link);
	printf("state %u, luns %u, openings %u\n",
	    link->state, link->bus->sb_luns, link->openings);

	sc_print_addr(link);
	printf("flags (0x%04x) ", link->flags);
	scsi_show_flags(link->flags, flagnames);
	printf("\n");

	sc_print_addr(link);
	printf("quirks (0x%04x) ", link->quirks);
	scsi_show_flags(link->quirks, quirknames);
#endif /* SCSIDEBUG */
}

/*
 * Return a priority based on how much of the inquiry data matches
 * the patterns for the particular driver.
 */
const void *
scsi_inqmatch(struct scsi_inquiry_data *inqbuf, const void *_base,
    int nmatches, int matchsize, int *bestpriority)
{
	const unsigned char		*base = (const unsigned char *)_base;
	const void			*bestmatch;
	int				 removable;

	/* Include the qualifier to catch vendor-unique types. */
	removable = ISSET(inqbuf->dev_qual2, SID_REMOVABLE) ? T_REMOV : T_FIXED;

	for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) {
		struct scsi_inquiry_pattern *match = (void *)base;
		int priority, len;

		if (inqbuf->device != match->type)
			continue;
		if (removable != match->removable)
			continue;
		priority = 2;
		len = strlen(match->vendor);
		if (bcmp(inqbuf->vendor, match->vendor, len))
			continue;
		priority += len;
		len = strlen(match->product);
		if (bcmp(inqbuf->product, match->product, len))
			continue;
		priority += len;
		len = strlen(match->revision);
		if (bcmp(inqbuf->revision, match->revision, len))
			continue;
		priority += len;

#ifdef SCSIDEBUG
		printf("scsi_inqmatch: ");
		if (_base == &scsi_quirk_patterns)
			printf(" quirk ");
		else
			printf(" match ");

		printf("priority %d. %s %s <\"%s\", \"%s\", \"%s\">", priority,
		    devicetypenames[(match->type & SID_TYPE)],
		    (match->removable == T_FIXED) ? "T_FIXED" : "T_REMOV",
		    match->vendor, match->product, match->revision);

		if (_base == &scsi_quirk_patterns)
			printf(" quirks: 0x%04x",
			    ((struct scsi_quirk_inquiry_pattern *)match)->quirks
			);

		printf("\n");
#endif /* SCSIDEBUG */
		if (priority > *bestpriority) {
			*bestpriority = priority;
			bestmatch = base;
		}
	}

	return bestmatch;
}

void
scsi_devid(struct scsi_link *link)
{
	struct {
		struct scsi_vpd_hdr hdr;
		u_int8_t list[32];
	} __packed			*pg;
	size_t				 len;
	int				 pg80 = 0, pg83 = 0, i;

	if (link->id != NULL)
		return;

	pg = dma_alloc(sizeof(*pg), PR_WAITOK | PR_ZERO);

	if (SID_ANSII_REV(&link->inqdata) >= SCSI_REV_2) {
		if (scsi_inquire_vpd(link, pg, sizeof(*pg), SI_PG_SUPPORTED,
		    scsi_autoconf) != 0)
			goto wwn;

		len = MIN(sizeof(pg->list), _2btol(pg->hdr.page_length));
		for (i = 0; i < len; i++) {
			switch (pg->list[i]) {
			case SI_PG_SERIAL:
				pg80 = 1;
				break;
			case SI_PG_DEVID:
				pg83 = 1;
				break;
			}
		}

		if (pg83 && scsi_devid_pg83(link) == 0)
			goto done;
		if (pg80 && scsi_devid_pg80(link) == 0)
			goto done;
	}

wwn:
	scsi_devid_wwn(link);
done:
	dma_free(pg, sizeof(*pg));
}

int
scsi_devid_pg83(struct scsi_link *link)
{
	struct scsi_vpd_devid_hdr	 dhdr, chdr;
	struct scsi_vpd_hdr		*hdr = NULL;
	u_int8_t			*pg = NULL, *id;
	int				 len, pos, rv, type;
	int				 idtype = 0;
	u_char				 idflags;

	hdr = dma_alloc(sizeof(*hdr), PR_WAITOK | PR_ZERO);

	rv = scsi_inquire_vpd(link, hdr, sizeof(*hdr), SI_PG_DEVID,
	    scsi_autoconf);
	if (rv != 0)
		goto done;

	len = sizeof(*hdr) + _2btol(hdr->page_length);
	pg = dma_alloc(len, PR_WAITOK | PR_ZERO);

	rv = scsi_inquire_vpd(link, pg, len, SI_PG_DEVID, scsi_autoconf);
	if (rv != 0)
		goto done;

	pos = sizeof(*hdr);

	do {
		if (len - pos < sizeof(dhdr)) {
			rv = EIO;
			goto done;
		}
		memcpy(&dhdr, &pg[pos], sizeof(dhdr));
		pos += sizeof(dhdr);
		if (len - pos < dhdr.len) {
			rv = EIO;
			goto done;
		}

		if (VPD_DEVID_ASSOC(dhdr.flags) == VPD_DEVID_ASSOC_LU) {
			type = VPD_DEVID_TYPE(dhdr.flags);
			switch (type) {
			case VPD_DEVID_TYPE_NAA:
			case VPD_DEVID_TYPE_EUI64:
			case VPD_DEVID_TYPE_T10:
				if (type >= idtype) {
					idtype = type;

					chdr = dhdr;
					id = &pg[pos];
				}
				break;

			default:
				/* skip */
				break;
			}
		}

		pos += dhdr.len;
	} while (idtype != VPD_DEVID_TYPE_NAA && len != pos);

	if (idtype > 0) {
		switch (VPD_DEVID_TYPE(chdr.flags)) {
		case VPD_DEVID_TYPE_NAA:
			idtype = DEVID_NAA;
			break;
		case VPD_DEVID_TYPE_EUI64:
			idtype = DEVID_EUI;
			break;
		case VPD_DEVID_TYPE_T10:
			idtype = DEVID_T10;
			break;
		}
		switch (VPD_DEVID_CODE(chdr.pi_code)) {
		case VPD_DEVID_CODE_ASCII:
		case VPD_DEVID_CODE_UTF8:
			idflags = DEVID_F_PRINT;
			break;
		default:
			idflags = 0;
			break;
		}
		link->id = devid_alloc(idtype, idflags, chdr.len, id);
	} else
		rv = ENODEV;

done:
	if (pg)
		dma_free(pg, len);
	if (hdr)
		dma_free(hdr, sizeof(*hdr));
	return rv;
}

int
scsi_devid_pg80(struct scsi_link *link)
{
	struct scsi_vpd_hdr		*hdr = NULL;
	u_int8_t			*pg = NULL;
	char				*id;
	size_t				 idlen;
	int				 len, pglen, rv;

	hdr = dma_alloc(sizeof(*hdr), PR_WAITOK | PR_ZERO);

	rv = scsi_inquire_vpd(link, hdr, sizeof(*hdr), SI_PG_SERIAL,
	    scsi_autoconf);
	if (rv != 0)
		goto freehdr;

	len = _2btol(hdr->page_length);
	if (len == 0) {
		rv = EINVAL;
		goto freehdr;
	}

	pglen = sizeof(*hdr) + len;
	pg = dma_alloc(pglen, PR_WAITOK | PR_ZERO);

	rv = scsi_inquire_vpd(link, pg, pglen, SI_PG_SERIAL, scsi_autoconf);
	if (rv != 0)
		goto free;

	idlen = sizeof(link->inqdata.vendor) +
	    sizeof(link->inqdata.product) + len;
	id = malloc(idlen, M_TEMP, M_WAITOK);
	memcpy(id, link->inqdata.vendor, sizeof(link->inqdata.vendor));
	memcpy(id + sizeof(link->inqdata.vendor), link->inqdata.product,
	    sizeof(link->inqdata.product));
	memcpy(id + sizeof(link->inqdata.vendor) +
	    sizeof(link->inqdata.product), pg + sizeof(*hdr), len);

	link->id = devid_alloc(DEVID_SERIAL, DEVID_F_PRINT,
	    sizeof(link->inqdata.vendor) + sizeof(link->inqdata.product) + len,
	    id);

	free(id, M_TEMP, idlen);

free:
	dma_free(pg, pglen);
freehdr:
	dma_free(hdr, sizeof(*hdr));
	return rv;
}

int
scsi_devid_wwn(struct scsi_link *link)
{
	u_int64_t wwnn;

	if (link->lun != 0 || link->node_wwn == 0)
		return EOPNOTSUPP;

	wwnn = htobe64(link->node_wwn);
	link->id = devid_alloc(DEVID_WWN, 0, sizeof(wwnn), (u_int8_t *)&wwnn);

	return 0;
}

struct devid *
devid_alloc(u_int8_t type, u_int8_t flags, u_int8_t len, u_int8_t *id)
{
	struct devid *d;

	d = malloc(sizeof(*d) + len, M_DEVBUF, M_WAITOK|M_CANFAIL);
	if (d == NULL)
		return NULL;

	d->d_type = type;
	d->d_flags = flags;
	d->d_len = len;
	d->d_refcount = 1;
	memcpy(d + 1, id, len);

	return d;
}

struct devid *
devid_copy(struct devid *d)
{
	d->d_refcount++;
	return d;
}

void
devid_free(struct devid *d)
{
	if (--d->d_refcount == 0)
		free(d, M_DEVBUF, sizeof(*d) + d->d_len);
}