aboutsummaryrefslogtreecommitdiff
path: root/sys/cam/cam_periph.c
blob: 98b9264f1069b73d635f4b26dfe0d13843fdf6ef (plain) (blame)
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
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
/*-
 * Common functions for CAM "type" (peripheral) drivers.
 *
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 1997, 1998 Justin T. Gibbs.
 * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry.
 * 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,
 *    without modification, immediately at the beginning of the file.
 * 2. 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/bio.h>
#include <sys/conf.h>
#include <sys/devctl.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/devicestat.h>
#include <sys/sbuf.h>
#include <sys/sysctl.h>
#include <vm/vm.h>
#include <vm/vm_extern.h>

#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/cam_queue.h>
#include <cam/cam_xpt_periph.h>
#include <cam/cam_xpt_internal.h>
#include <cam/cam_periph.h>
#include <cam/cam_debug.h>
#include <cam/cam_sim.h>

#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_message.h>
#include <cam/scsi/scsi_pass.h>

static	u_int		camperiphnextunit(struct periph_driver *p_drv,
					  u_int newunit, int wired,
					  path_id_t pathid, target_id_t target,
					  lun_id_t lun);
static	u_int		camperiphunit(struct periph_driver *p_drv,
				      path_id_t pathid, target_id_t target,
				      lun_id_t lun); 
static	void		camperiphdone(struct cam_periph *periph, 
					union ccb *done_ccb);
static  void		camperiphfree(struct cam_periph *periph);
static int		camperiphscsistatuserror(union ccb *ccb,
					        union ccb **orig_ccb,
						 cam_flags camflags,
						 u_int32_t sense_flags,
						 int *openings,
						 u_int32_t *relsim_flags,
						 u_int32_t *timeout,
						 u_int32_t  *action,
						 const char **action_string);
static	int		camperiphscsisenseerror(union ccb *ccb,
					        union ccb **orig_ccb,
					        cam_flags camflags,
					        u_int32_t sense_flags,
					        int *openings,
					        u_int32_t *relsim_flags,
					        u_int32_t *timeout,
					        u_int32_t *action,
					        const char **action_string);
static void		cam_periph_devctl_notify(union ccb *ccb);

static int nperiph_drivers;
static int initialized = 0;
struct periph_driver **periph_drivers;

static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");

static int periph_selto_delay = 1000;
TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
static int periph_noresrc_delay = 500;
TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
static int periph_busy_delay = 500;
TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);

static u_int periph_mapmem_thresh = 65536;
SYSCTL_UINT(_kern_cam, OID_AUTO, mapmem_thresh, CTLFLAG_RWTUN,
    &periph_mapmem_thresh, 0, "Threshold for user-space buffer mapping");

void
periphdriver_register(void *data)
{
	struct periph_driver *drv = (struct periph_driver *)data;
	struct periph_driver **newdrivers, **old;
	int ndrivers;

again:
	ndrivers = nperiph_drivers + 2;
	newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
			    M_WAITOK);
	xpt_lock_buses();
	if (ndrivers != nperiph_drivers + 2) {
		/*
		 * Lost race against itself; go around.
		 */
		xpt_unlock_buses();
		free(newdrivers, M_CAMPERIPH);
		goto again;
	}
	if (periph_drivers)
		bcopy(periph_drivers, newdrivers,
		      sizeof(*newdrivers) * nperiph_drivers);
	newdrivers[nperiph_drivers] = drv;
	newdrivers[nperiph_drivers + 1] = NULL;
	old = periph_drivers;
	periph_drivers = newdrivers;
	nperiph_drivers++;
	xpt_unlock_buses();
	if (old)
		free(old, M_CAMPERIPH);
	/* If driver marked as early or it is late now, initialize it. */
	if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
	    initialized > 1)
		(*drv->init)();
}

int
periphdriver_unregister(void *data)
{
	struct periph_driver *drv = (struct periph_driver *)data;
	int error, n;

	/* If driver marked as early or it is late now, deinitialize it. */
	if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
	    initialized > 1) {
		if (drv->deinit == NULL) {
			printf("CAM periph driver '%s' doesn't have deinit.\n",
			    drv->driver_name);
			return (EOPNOTSUPP);
		}
		error = drv->deinit();
		if (error != 0)
			return (error);
	}

	xpt_lock_buses();
	for (n = 0; n < nperiph_drivers && periph_drivers[n] != drv; n++)
		;
	KASSERT(n < nperiph_drivers,
	    ("Periph driver '%s' was not registered", drv->driver_name));
	for (; n + 1 < nperiph_drivers; n++)
		periph_drivers[n] = periph_drivers[n + 1];
	periph_drivers[n + 1] = NULL;
	nperiph_drivers--;
	xpt_unlock_buses();
	return (0);
}

void
periphdriver_init(int level)
{
	int	i, early;

	initialized = max(initialized, level);
	for (i = 0; periph_drivers[i] != NULL; i++) {
		early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
		if (early == initialized)
			(*periph_drivers[i]->init)();
	}
}

cam_status
cam_periph_alloc(periph_ctor_t *periph_ctor,
		 periph_oninv_t *periph_oninvalidate,
		 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
		 char *name, cam_periph_type type, struct cam_path *path,
		 ac_callback_t *ac_callback, ac_code code, void *arg)
{
	struct		periph_driver **p_drv;
	struct		cam_sim *sim;
	struct		cam_periph *periph;
	struct		cam_periph *cur_periph;
	path_id_t	path_id;
	target_id_t	target_id;
	lun_id_t	lun_id;
	cam_status	status;
	u_int		init_level;

	init_level = 0;
	/*
	 * Handle Hot-Plug scenarios.  If there is already a peripheral
	 * of our type assigned to this path, we are likely waiting for
	 * final close on an old, invalidated, peripheral.  If this is
	 * the case, queue up a deferred call to the peripheral's async
	 * handler.  If it looks like a mistaken re-allocation, complain.
	 */
	if ((periph = cam_periph_find(path, name)) != NULL) {
		if ((periph->flags & CAM_PERIPH_INVALID) != 0
		 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
			periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
			periph->deferred_callback = ac_callback;
			periph->deferred_ac = code;
			return (CAM_REQ_INPROG);
		} else {
			printf("cam_periph_alloc: attempt to re-allocate "
			       "valid device %s%d rejected flags %#x "
			       "refcount %d\n", periph->periph_name,
			       periph->unit_number, periph->flags,
			       periph->refcount);
		}
		return (CAM_REQ_INVALID);
	}

	periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
					     M_NOWAIT|M_ZERO);

	if (periph == NULL)
		return (CAM_RESRC_UNAVAIL);

	init_level++;

	sim = xpt_path_sim(path);
	path_id = xpt_path_path_id(path);
	target_id = xpt_path_target_id(path);
	lun_id = xpt_path_lun_id(path);
	periph->periph_start = periph_start;
	periph->periph_dtor = periph_dtor;
	periph->periph_oninval = periph_oninvalidate;
	periph->type = type;
	periph->periph_name = name;
	periph->scheduled_priority = CAM_PRIORITY_NONE;
	periph->immediate_priority = CAM_PRIORITY_NONE;
	periph->refcount = 1;		/* Dropped by invalidation. */
	periph->sim = sim;
	SLIST_INIT(&periph->ccb_list);
	status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
	if (status != CAM_REQ_CMP)
		goto failure;
	periph->path = path;

	xpt_lock_buses();
	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
		if (strcmp((*p_drv)->driver_name, name) == 0)
			break;
	}
	if (*p_drv == NULL) {
		printf("cam_periph_alloc: invalid periph name '%s'\n", name);
		xpt_unlock_buses();
		xpt_free_path(periph->path);
		free(periph, M_CAMPERIPH);
		return (CAM_REQ_INVALID);
	}
	periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
	cur_periph = TAILQ_FIRST(&(*p_drv)->units);
	while (cur_periph != NULL
	    && cur_periph->unit_number < periph->unit_number)
		cur_periph = TAILQ_NEXT(cur_periph, unit_links);
	if (cur_periph != NULL) {
		KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
		TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
	} else {
		TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
		(*p_drv)->generation++;
	}
	xpt_unlock_buses();

	init_level++;

	status = xpt_add_periph(periph);
	if (status != CAM_REQ_CMP)
		goto failure;

	init_level++;
	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));

	status = periph_ctor(periph, arg);

	if (status == CAM_REQ_CMP)
		init_level++;

failure:
	switch (init_level) {
	case 4:
		/* Initialized successfully */
		break;
	case 3:
		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
		xpt_remove_periph(periph);
		/* FALLTHROUGH */
	case 2:
		xpt_lock_buses();
		TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
		xpt_unlock_buses();
		xpt_free_path(periph->path);
		/* FALLTHROUGH */
	case 1:
		free(periph, M_CAMPERIPH);
		/* FALLTHROUGH */
	case 0:
		/* No cleanup to perform. */
		break;
	default:
		panic("%s: Unknown init level", __func__);
	}
	return(status);
}

/*
 * Find a peripheral structure with the specified path, target, lun, 
 * and (optionally) type.  If the name is NULL, this function will return
 * the first peripheral driver that matches the specified path.
 */
struct cam_periph *
cam_periph_find(struct cam_path *path, char *name)
{
	struct periph_driver **p_drv;
	struct cam_periph *periph;

	xpt_lock_buses();
	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
		if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
			continue;

		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
			if (xpt_path_comp(periph->path, path) == 0) {
				xpt_unlock_buses();
				cam_periph_assert(periph, MA_OWNED);
				return(periph);
			}
		}
		if (name != NULL) {
			xpt_unlock_buses();
			return(NULL);
		}
	}
	xpt_unlock_buses();
	return(NULL);
}

/*
 * Find peripheral driver instances attached to the specified path.
 */
int
cam_periph_list(struct cam_path *path, struct sbuf *sb)
{
	struct sbuf local_sb;
	struct periph_driver **p_drv;
	struct cam_periph *periph;
	int count;
	int sbuf_alloc_len;

	sbuf_alloc_len = 16;
retry:
	sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
	count = 0;
	xpt_lock_buses();
	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
			if (xpt_path_comp(periph->path, path) != 0)
				continue;

			if (sbuf_len(&local_sb) != 0)
				sbuf_cat(&local_sb, ",");

			sbuf_printf(&local_sb, "%s%d", periph->periph_name,
				    periph->unit_number);

			if (sbuf_error(&local_sb) == ENOMEM) {
				sbuf_alloc_len *= 2;
				xpt_unlock_buses();
				sbuf_delete(&local_sb);
				goto retry;
			}
			count++;
		}
	}
	xpt_unlock_buses();
	sbuf_finish(&local_sb);
	if (sbuf_len(sb) != 0)
		sbuf_cat(sb, ",");
	sbuf_cat(sb, sbuf_data(&local_sb));
	sbuf_delete(&local_sb);
	return (count);
}

int
cam_periph_acquire(struct cam_periph *periph)
{
	int status;

	if (periph == NULL)
		return (EINVAL);

	status = ENOENT;
	xpt_lock_buses();
	if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
		periph->refcount++;
		status = 0;
	}
	xpt_unlock_buses();

	return (status);
}

void
cam_periph_doacquire(struct cam_periph *periph)
{

	xpt_lock_buses();
	KASSERT(periph->refcount >= 1,
	    ("cam_periph_doacquire() with refcount == %d", periph->refcount));
	periph->refcount++;
	xpt_unlock_buses();
}

void
cam_periph_release_locked_buses(struct cam_periph *periph)
{

	cam_periph_assert(periph, MA_OWNED);
	KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
	if (--periph->refcount == 0)
		camperiphfree(periph);
}

void
cam_periph_release_locked(struct cam_periph *periph)
{

	if (periph == NULL)
		return;

	xpt_lock_buses();
	cam_periph_release_locked_buses(periph);
	xpt_unlock_buses();
}

void
cam_periph_release(struct cam_periph *periph)
{
	struct mtx *mtx;

	if (periph == NULL)
		return;

	cam_periph_assert(periph, MA_NOTOWNED);
	mtx = cam_periph_mtx(periph);
	mtx_lock(mtx);
	cam_periph_release_locked(periph);
	mtx_unlock(mtx);
}

/*
 * hold/unhold act as mutual exclusion for sections of the code that
 * need to sleep and want to make sure that other sections that
 * will interfere are held off. This only protects exclusive sections
 * from each other.
 */
int
cam_periph_hold(struct cam_periph *periph, int priority)
{
	int error;

	/*
	 * Increment the reference count on the peripheral
	 * while we wait for our lock attempt to succeed
	 * to ensure the peripheral doesn't disappear out
	 * from user us while we sleep.
	 */

	if (cam_periph_acquire(periph) != 0)
		return (ENXIO);

	cam_periph_assert(periph, MA_OWNED);
	while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
		periph->flags |= CAM_PERIPH_LOCK_WANTED;
		if ((error = cam_periph_sleep(periph, periph, priority,
		    "caplck", 0)) != 0) {
			cam_periph_release_locked(periph);
			return (error);
		}
		if (periph->flags & CAM_PERIPH_INVALID) {
			cam_periph_release_locked(periph);
			return (ENXIO);
		}
	}

	periph->flags |= CAM_PERIPH_LOCKED;
	return (0);
}

void
cam_periph_unhold(struct cam_periph *periph)
{

	cam_periph_assert(periph, MA_OWNED);

	periph->flags &= ~CAM_PERIPH_LOCKED;
	if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
		periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
		wakeup(periph);
	}

	cam_periph_release_locked(periph);
}

/*
 * Look for the next unit number that is not currently in use for this
 * peripheral type starting at "newunit".  Also exclude unit numbers that
 * are reserved by for future "hardwiring" unless we already know that this
 * is a potential wired device.  Only assume that the device is "wired" the
 * first time through the loop since after that we'll be looking at unit
 * numbers that did not match a wiring entry.
 */
static u_int
camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
		  path_id_t pathid, target_id_t target, lun_id_t lun)
{
	struct	cam_periph *periph;
	char	*periph_name;
	int	i, val, dunit, r;
	const char *dname, *strval;

	periph_name = p_drv->driver_name;
	for (;;newunit++) {
		for (periph = TAILQ_FIRST(&p_drv->units);
		     periph != NULL && periph->unit_number != newunit;
		     periph = TAILQ_NEXT(periph, unit_links))
			;

		if (periph != NULL && periph->unit_number == newunit) {
			if (wired != 0) {
				xpt_print(periph->path, "Duplicate Wired "
				    "Device entry!\n");
				xpt_print(periph->path, "Second device (%s "
				    "device at scbus%d target %d lun %d) will "
				    "not be wired\n", periph_name, pathid,
				    target, lun);
				wired = 0;
			}
			continue;
		}
		if (wired)
			break;

		/*
		 * Don't match entries like "da 4" as a wired down
		 * device, but do match entries like "da 4 target 5"
		 * or even "da 4 scbus 1". 
		 */
		i = 0;
		dname = periph_name;
		for (;;) {
			r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
			if (r != 0)
				break;
			/* if no "target" and no specific scbus, skip */
			if (resource_int_value(dname, dunit, "target", &val) &&
			    (resource_string_value(dname, dunit, "at",&strval)||
			     strcmp(strval, "scbus") == 0))
				continue;
			if (newunit == dunit)
				break;
		}
		if (r != 0)
			break;
	}
	return (newunit);
}

static u_int
camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
	      target_id_t target, lun_id_t lun)
{
	u_int	unit;
	int	wired, i, val, dunit;
	const char *dname, *strval;
	char	pathbuf[32], *periph_name;

	periph_name = p_drv->driver_name;
	snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
	unit = 0;
	i = 0;
	dname = periph_name;
	for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
	     wired = 0) {
		if (resource_string_value(dname, dunit, "at", &strval) == 0) {
			if (strcmp(strval, pathbuf) != 0)
				continue;
			wired++;
		}
		if (resource_int_value(dname, dunit, "target", &val) == 0) {
			if (val != target)
				continue;
			wired++;
		}
		if (resource_int_value(dname, dunit, "lun", &val) == 0) {
			if (val != lun)
				continue;
			wired++;
		}
		if (wired != 0) {
			unit = dunit;
			break;
		}
	}

	/*
	 * Either start from 0 looking for the next unit or from
	 * the unit number given in the resource config.  This way,
	 * if we have wildcard matches, we don't return the same
	 * unit number twice.
	 */
	unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);

	return (unit);
}

void
cam_periph_invalidate(struct cam_periph *periph)
{

	cam_periph_assert(periph, MA_OWNED);
	/*
	 * We only tear down the device the first time a peripheral is
	 * invalidated.
	 */
	if ((periph->flags & CAM_PERIPH_INVALID) != 0)
		return;

	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
	if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting) {
		struct sbuf sb;
		char buffer[160];

		sbuf_new(&sb, buffer, 160, SBUF_FIXEDLEN);
		xpt_denounce_periph_sbuf(periph, &sb);
		sbuf_finish(&sb);
		sbuf_putbuf(&sb);
	}
	periph->flags |= CAM_PERIPH_INVALID;
	periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
	if (periph->periph_oninval != NULL)
		periph->periph_oninval(periph);
	cam_periph_release_locked(periph);
}

static void
camperiphfree(struct cam_periph *periph)
{
	struct periph_driver **p_drv;
	struct periph_driver *drv;

	cam_periph_assert(periph, MA_OWNED);
	KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating",
	    periph->periph_name, periph->unit_number));
	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
		if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
			break;
	}
	if (*p_drv == NULL) {
		printf("camperiphfree: attempt to free non-existant periph\n");
		return;
	}
	/*
	 * Cache a pointer to the periph_driver structure.  If a
	 * periph_driver is added or removed from the array (see
	 * periphdriver_register()) while we drop the toplogy lock
	 * below, p_drv may change.  This doesn't protect against this
	 * particular periph_driver going away.  That will require full
	 * reference counting in the periph_driver infrastructure.
	 */
	drv = *p_drv;

	/*
	 * We need to set this flag before dropping the topology lock, to
	 * let anyone who is traversing the list that this peripheral is
	 * about to be freed, and there will be no more reference count
	 * checks.
	 */
	periph->flags |= CAM_PERIPH_FREE;

	/*
	 * The peripheral destructor semantics dictate calling with only the
	 * SIM mutex held.  Since it might sleep, it should not be called
	 * with the topology lock held.
	 */
	xpt_unlock_buses();

	/*
	 * We need to call the peripheral destructor prior to removing the
	 * peripheral from the list.  Otherwise, we risk running into a
	 * scenario where the peripheral unit number may get reused
	 * (because it has been removed from the list), but some resources
	 * used by the peripheral are still hanging around.  In particular,
	 * the devfs nodes used by some peripherals like the pass(4) driver
	 * aren't fully cleaned up until the destructor is run.  If the
	 * unit number is reused before the devfs instance is fully gone,
	 * devfs will panic.
	 */
	if (periph->periph_dtor != NULL)
		periph->periph_dtor(periph);

	/*
	 * The peripheral list is protected by the topology lock. We have to
	 * remove the periph from the drv list before we call deferred_ac. The
	 * AC_FOUND_DEVICE callback won't create a new periph if it's still there.
	 */
	xpt_lock_buses();

	TAILQ_REMOVE(&drv->units, periph, unit_links);
	drv->generation++;

	xpt_remove_periph(periph);

	xpt_unlock_buses();
	if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
		xpt_print(periph->path, "Periph destroyed\n");
	else
		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));

	if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
		union ccb ccb;
		void *arg;

		switch (periph->deferred_ac) {
		case AC_FOUND_DEVICE:
			ccb.ccb_h.func_code = XPT_GDEV_TYPE;
			xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
			xpt_action(&ccb);
			arg = &ccb;
			break;
		case AC_PATH_REGISTERED:
			xpt_path_inq(&ccb.cpi, periph->path);
			arg = &ccb;
			break;
		default:
			arg = NULL;
			break;
		}
		periph->deferred_callback(NULL, periph->deferred_ac,
					  periph->path, arg);
	}
	xpt_free_path(periph->path);
	free(periph, M_CAMPERIPH);
	xpt_lock_buses();
}

/*
 * Map user virtual pointers into kernel virtual address space, so we can
 * access the memory.  This is now a generic function that centralizes most
 * of the sanity checks on the data flags, if any.
 * This also only works for up to maxphys memory.  Since we use
 * buffers to map stuff in and out, we're limited to the buffer size.
 */
int
cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo,
    u_int maxmap)
{
	int numbufs, i;
	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
	u_int32_t dirs[CAM_PERIPH_MAXMAPS];

	bzero(mapinfo, sizeof(*mapinfo));
	if (maxmap == 0)
		maxmap = DFLTPHYS;	/* traditional default */
	else if (maxmap > maxphys)
		maxmap = maxphys;	/* for safety */
	switch(ccb->ccb_h.func_code) {
	case XPT_DEV_MATCH:
		if (ccb->cdm.match_buf_len == 0) {
			printf("cam_periph_mapmem: invalid match buffer "
			       "length 0\n");
			return(EINVAL);
		}
		if (ccb->cdm.pattern_buf_len > 0) {
			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
			lengths[0] = ccb->cdm.pattern_buf_len;
			dirs[0] = CAM_DIR_OUT;
			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
			lengths[1] = ccb->cdm.match_buf_len;
			dirs[1] = CAM_DIR_IN;
			numbufs = 2;
		} else {
			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
			lengths[0] = ccb->cdm.match_buf_len;
			dirs[0] = CAM_DIR_IN;
			numbufs = 1;
		}
		/*
		 * This request will not go to the hardware, no reason
		 * to be so strict. vmapbuf() is able to map up to maxphys.
		 */
		maxmap = maxphys;
		break;
	case XPT_SCSI_IO:
	case XPT_CONT_TARGET_IO:
		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
			return(0);
		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
			return (EINVAL);
		data_ptrs[0] = &ccb->csio.data_ptr;
		lengths[0] = ccb->csio.dxfer_len;
		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
		numbufs = 1;
		break;
	case XPT_ATA_IO:
		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
			return(0);
		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
			return (EINVAL);
		data_ptrs[0] = &ccb->ataio.data_ptr;
		lengths[0] = ccb->ataio.dxfer_len;
		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
		numbufs = 1;
		break;
	case XPT_MMC_IO:
		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
			return(0);
		/* Two mappings: one for cmd->data and one for cmd->data->data */
		data_ptrs[0] = (unsigned char **)&ccb->mmcio.cmd.data;
		lengths[0] = sizeof(struct mmc_data *);
		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
		data_ptrs[1] = (unsigned char **)&ccb->mmcio.cmd.data->data;
		lengths[1] = ccb->mmcio.cmd.data->len;
		dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK;
		numbufs = 2;
		break;
	case XPT_SMP_IO:
		data_ptrs[0] = &ccb->smpio.smp_request;
		lengths[0] = ccb->smpio.smp_request_len;
		dirs[0] = CAM_DIR_OUT;
		data_ptrs[1] = &ccb->smpio.smp_response;
		lengths[1] = ccb->smpio.smp_response_len;
		dirs[1] = CAM_DIR_IN;
		numbufs = 2;
		break;
	case XPT_NVME_IO:
	case XPT_NVME_ADMIN:
		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
			return (0);
		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
			return (EINVAL);
		data_ptrs[0] = &ccb->nvmeio.data_ptr;
		lengths[0] = ccb->nvmeio.dxfer_len;
		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
		numbufs = 1;
		break;
	case XPT_DEV_ADVINFO:
		if (ccb->cdai.bufsiz == 0)
			return (0);

		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
		lengths[0] = ccb->cdai.bufsiz;
		dirs[0] = CAM_DIR_IN;
		numbufs = 1;

		/*
		 * This request will not go to the hardware, no reason
		 * to be so strict. vmapbuf() is able to map up to maxphys.
		 */
		maxmap = maxphys;
		break;
	default:
		return(EINVAL);
		break; /* NOTREACHED */
	}

	/*
	 * Check the transfer length and permissions first, so we don't
	 * have to unmap any previously mapped buffers.
	 */
	for (i = 0; i < numbufs; i++) {
		if (lengths[i] > maxmap) {
			printf("cam_periph_mapmem: attempt to map %lu bytes, "
			       "which is greater than %lu\n",
			       (long)(lengths[i]), (u_long)maxmap);
			return (E2BIG);
		}
	}

	/*
	 * This keeps the kernel stack of current thread from getting
	 * swapped.  In low-memory situations where the kernel stack might
	 * otherwise get swapped out, this holds it and allows the thread
	 * to make progress and release the kernel mapped pages sooner.
	 *
	 * XXX KDM should I use P_NOSWAP instead?
	 */
	PHOLD(curproc);

	for (i = 0; i < numbufs; i++) {
		/* Save the user's data address. */
		mapinfo->orig[i] = *data_ptrs[i];

		/*
		 * For small buffers use malloc+copyin/copyout instead of
		 * mapping to KVA to avoid expensive TLB shootdowns.  For
		 * small allocations malloc is backed by UMA, and so much
		 * cheaper on SMP systems.
		 */
		if (lengths[i] <= periph_mapmem_thresh &&
		    ccb->ccb_h.func_code != XPT_MMC_IO) {
			*data_ptrs[i] = malloc(lengths[i], M_CAMPERIPH,
			    M_WAITOK);
			if (dirs[i] != CAM_DIR_IN) {
				if (copyin(mapinfo->orig[i], *data_ptrs[i],
				    lengths[i]) != 0) {
					free(*data_ptrs[i], M_CAMPERIPH);
					*data_ptrs[i] = mapinfo->orig[i];
					goto fail;
				}
			} else
				bzero(*data_ptrs[i], lengths[i]);
			continue;
		}

		/*
		 * Get the buffer.
		 */
		mapinfo->bp[i] = uma_zalloc(pbuf_zone, M_WAITOK);

		/* set the direction */
		mapinfo->bp[i]->b_iocmd = (dirs[i] == CAM_DIR_OUT) ?
		    BIO_WRITE : BIO_READ;

		/* Map the buffer into kernel memory. */
		if (vmapbuf(mapinfo->bp[i], *data_ptrs[i], lengths[i], 1) < 0) {
			uma_zfree(pbuf_zone, mapinfo->bp[i]);
			goto fail;
		}

		/* set our pointer to the new mapped area */
		*data_ptrs[i] = mapinfo->bp[i]->b_data;
	}

	/*
	 * Now that we've gotten this far, change ownership to the kernel
	 * of the buffers so that we don't run afoul of returning to user
	 * space with locks (on the buffer) held.
	 */
	for (i = 0; i < numbufs; i++) {
		if (mapinfo->bp[i])
			BUF_KERNPROC(mapinfo->bp[i]);
	}

	mapinfo->num_bufs_used = numbufs;
	return(0);

fail:
	for (i--; i >= 0; i--) {
		if (mapinfo->bp[i]) {
			vunmapbuf(mapinfo->bp[i]);
			uma_zfree(pbuf_zone, mapinfo->bp[i]);
		} else
			free(*data_ptrs[i], M_CAMPERIPH);
		*data_ptrs[i] = mapinfo->orig[i];
	}
	PRELE(curproc);
	return(EACCES);
}

/*
 * Unmap memory segments mapped into kernel virtual address space by
 * cam_periph_mapmem().
 */
void
cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
{
	int numbufs, i;
	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
	u_int32_t dirs[CAM_PERIPH_MAXMAPS];

	if (mapinfo->num_bufs_used <= 0) {
		/* nothing to free and the process wasn't held. */
		return;
	}

	switch (ccb->ccb_h.func_code) {
	case XPT_DEV_MATCH:
		if (ccb->cdm.pattern_buf_len > 0) {
			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
			lengths[0] = ccb->cdm.pattern_buf_len;
			dirs[0] = CAM_DIR_OUT;
			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
			lengths[1] = ccb->cdm.match_buf_len;
			dirs[1] = CAM_DIR_IN;
			numbufs = 2;
		} else {
			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
			lengths[0] = ccb->cdm.match_buf_len;
			dirs[0] = CAM_DIR_IN;
			numbufs = 1;
		}
		break;
	case XPT_SCSI_IO:
	case XPT_CONT_TARGET_IO:
		data_ptrs[0] = &ccb->csio.data_ptr;
		lengths[0] = ccb->csio.dxfer_len;
		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
		numbufs = 1;
		break;
	case XPT_ATA_IO:
		data_ptrs[0] = &ccb->ataio.data_ptr;
		lengths[0] = ccb->ataio.dxfer_len;
		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
		numbufs = 1;
		break;
	case XPT_MMC_IO:
		data_ptrs[0] = (u_int8_t **)&ccb->mmcio.cmd.data;
		lengths[0] = sizeof(struct mmc_data *);
		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
		data_ptrs[1] = (u_int8_t **)&ccb->mmcio.cmd.data->data;
		lengths[1] = ccb->mmcio.cmd.data->len;
		dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK;
		numbufs = 2;
		break;
	case XPT_SMP_IO:
		data_ptrs[0] = &ccb->smpio.smp_request;
		lengths[0] = ccb->smpio.smp_request_len;
		dirs[0] = CAM_DIR_OUT;
		data_ptrs[1] = &ccb->smpio.smp_response;
		lengths[1] = ccb->smpio.smp_response_len;
		dirs[1] = CAM_DIR_IN;
		numbufs = 2;
		break;
	case XPT_NVME_IO:
	case XPT_NVME_ADMIN:
		data_ptrs[0] = &ccb->nvmeio.data_ptr;
		lengths[0] = ccb->nvmeio.dxfer_len;
		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
		numbufs = 1;
		break;
	case XPT_DEV_ADVINFO:
		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
		lengths[0] = ccb->cdai.bufsiz;
		dirs[0] = CAM_DIR_IN;
		numbufs = 1;
		break;
	default:
		/* allow ourselves to be swapped once again */
		PRELE(curproc);
		return;
		break; /* NOTREACHED */ 
	}

	for (i = 0; i < numbufs; i++) {
		if (mapinfo->bp[i]) {
			/* unmap the buffer */
			vunmapbuf(mapinfo->bp[i]);

			/* release the buffer */
			uma_zfree(pbuf_zone, mapinfo->bp[i]);
		} else {
			if (dirs[i] != CAM_DIR_OUT) {
				copyout(*data_ptrs[i], mapinfo->orig[i],
				    lengths[i]);
			}
			free(*data_ptrs[i], M_CAMPERIPH);
		}

		/* Set the user's pointer back to the original value */
		*data_ptrs[i] = mapinfo->orig[i];
	}

	/* allow ourselves to be swapped once again */
	PRELE(curproc);
}

int
cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
		 int (*error_routine)(union ccb *ccb, 
				      cam_flags camflags,
				      u_int32_t sense_flags))
{
	union ccb 	     *ccb;
	int 		     error;
	int		     found;

	error = found = 0;

	switch(cmd){
	case CAMGETPASSTHRU:
		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
		xpt_setup_ccb(&ccb->ccb_h,
			      ccb->ccb_h.path,
			      CAM_PRIORITY_NORMAL);
		ccb->ccb_h.func_code = XPT_GDEVLIST;

		/*
		 * Basically, the point of this is that we go through
		 * getting the list of devices, until we find a passthrough
		 * device.  In the current version of the CAM code, the
		 * only way to determine what type of device we're dealing
		 * with is by its name.
		 */
		while (found == 0) {
			ccb->cgdl.index = 0;
			ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
			while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
				/* we want the next device in the list */
				xpt_action(ccb);
				if (strncmp(ccb->cgdl.periph_name, 
				    "pass", 4) == 0){
					found = 1;
					break;
				}
			}
			if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
			    (found == 0)) {
				ccb->cgdl.periph_name[0] = '\0';
				ccb->cgdl.unit_number = 0;
				break;
			}
		}

		/* copy the result back out */	
		bcopy(ccb, addr, sizeof(union ccb));

		/* and release the ccb */
		xpt_release_ccb(ccb);

		break;
	default:
		error = ENOTTY;
		break;
	}
	return(error);
}

static void
cam_periph_done_panic(struct cam_periph *periph, union ccb *done_ccb)
{

	panic("%s: already done with ccb %p", __func__, done_ccb);
}

static void
cam_periph_done(struct cam_periph *periph, union ccb *done_ccb)
{

	/* Caller will release the CCB */
	xpt_path_assert(done_ccb->ccb_h.path, MA_OWNED);
	done_ccb->ccb_h.cbfcnp = cam_periph_done_panic;
	wakeup(&done_ccb->ccb_h.cbfcnp);
}

static void
cam_periph_ccbwait(union ccb *ccb)
{

	if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
		while (ccb->ccb_h.cbfcnp != cam_periph_done_panic)
			xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp,
			    PRIBIO, "cbwait", 0);
	}
	KASSERT(ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX &&
	    (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG,
	    ("%s: proceeding with incomplete ccb: ccb=%p, func_code=%#x, "
	     "status=%#x, index=%d", __func__, ccb, ccb->ccb_h.func_code,
	     ccb->ccb_h.status, ccb->ccb_h.pinfo.index));
}

/*
 * Dispatch a CCB and wait for it to complete.  If the CCB has set a
 * callback function (ccb->ccb_h.cbfcnp), it will be overwritten and lost.
 */
int
cam_periph_runccb(union ccb *ccb,
		  int (*error_routine)(union ccb *ccb,
				       cam_flags camflags,
				       u_int32_t sense_flags),
		  cam_flags camflags, u_int32_t sense_flags,
		  struct devstat *ds)
{
	struct bintime *starttime;
	struct bintime ltime;
	int error;
	bool must_poll;
	uint32_t timeout = 1;

	starttime = NULL;
	xpt_path_assert(ccb->ccb_h.path, MA_OWNED);
	KASSERT((ccb->ccb_h.flags & CAM_UNLOCKED) == 0,
	    ("%s: ccb=%p, func_code=%#x, flags=%#x", __func__, ccb,
	     ccb->ccb_h.func_code, ccb->ccb_h.flags));

	/*
	 * If the user has supplied a stats structure, and if we understand
	 * this particular type of ccb, record the transaction start.
	 */
	if (ds != NULL &&
	    (ccb->ccb_h.func_code == XPT_SCSI_IO ||
	    ccb->ccb_h.func_code == XPT_ATA_IO ||
	    ccb->ccb_h.func_code == XPT_NVME_IO)) {
		starttime = &ltime;
		binuptime(starttime);
		devstat_start_transaction(ds, starttime);
	}

	/*
	 * We must poll the I/O while we're dumping. The scheduler is normally
	 * stopped for dumping, except when we call doadump from ddb. While the
	 * scheduler is running in this case, we still need to poll the I/O to
	 * avoid sleeping waiting for the ccb to complete.
	 *
	 * A panic triggered dump stops the scheduler, any callback from the
	 * shutdown_post_sync event will run with the scheduler stopped, but
	 * before we're officially dumping. To avoid hanging in adashutdown
	 * initiated commands (or other similar situations), we have to test for
	 * either SCHEDULER_STOPPED() here as well.
	 *
	 * To avoid locking problems, dumping/polling callers must call
	 * without a periph lock held.
	 */
	must_poll = dumping || SCHEDULER_STOPPED();
	ccb->ccb_h.cbfcnp = cam_periph_done;

	/*
	 * If we're polling, then we need to ensure that we have ample resources
	 * in the periph.  cam_periph_error can reschedule the ccb by calling
	 * xpt_action and returning ERESTART, so we have to effect the polling
	 * in the do loop below.
	 */
	if (must_poll) {
		if (cam_sim_pollable(ccb->ccb_h.path->bus->sim))
			timeout = xpt_poll_setup(ccb);
		else
			timeout = 0;
	}

	if (timeout == 0) {
		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
		error = EBUSY;
	} else {
		xpt_action(ccb);
		do {
			if (must_poll) {
				xpt_pollwait(ccb, timeout);
				timeout = ccb->ccb_h.timeout * 10;
			} else {
				cam_periph_ccbwait(ccb);
			}
			if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
				error = 0;
			else if (error_routine != NULL) {
				ccb->ccb_h.cbfcnp = cam_periph_done;
				error = (*error_routine)(ccb, camflags, sense_flags);
			} else
				error = 0;
		} while (error == ERESTART);
	}

	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
		cam_release_devq(ccb->ccb_h.path,
				 /* relsim_flags */0,
				 /* openings */0,
				 /* timeout */0,
				 /* getcount_only */ FALSE);
		ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
	}

	if (ds != NULL) {
		uint32_t bytes;
		devstat_tag_type tag;
		bool valid = true;

		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
			bytes = ccb->csio.dxfer_len - ccb->csio.resid;
			tag = (devstat_tag_type)(ccb->csio.tag_action & 0x3);
		} else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
			bytes = ccb->ataio.dxfer_len - ccb->ataio.resid;
			tag = (devstat_tag_type)0;
		} else if (ccb->ccb_h.func_code == XPT_NVME_IO) {
			bytes = ccb->nvmeio.dxfer_len; /* NB: resid no possible */
			tag = (devstat_tag_type)0;
		} else {
			valid = false;
		}
		if (valid)
			devstat_end_transaction(ds, bytes, tag,
			    ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) ?
			    DEVSTAT_NO_DATA : (ccb->ccb_h.flags & CAM_DIR_OUT) ?
			    DEVSTAT_WRITE : DEVSTAT_READ, NULL, starttime);
	}

	return(error);
}

void
cam_freeze_devq(struct cam_path *path)
{
	struct ccb_hdr ccb_h;

	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
	xpt_setup_ccb(&ccb_h, path, /*priority*/1);
	ccb_h.func_code = XPT_NOOP;
	ccb_h.flags = CAM_DEV_QFREEZE;
	xpt_action((union ccb *)&ccb_h);
}

u_int32_t
cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
		 u_int32_t openings, u_int32_t arg,
		 int getcount_only)
{
	struct ccb_relsim crs;

	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
	    relsim_flags, openings, arg, getcount_only));
	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
	crs.ccb_h.func_code = XPT_REL_SIMQ;
	crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
	crs.release_flags = relsim_flags;
	crs.openings = openings;
	crs.release_timeout = arg;
	xpt_action((union ccb *)&crs);
	return (crs.qfrozen_cnt);
}

#define saved_ccb_ptr ppriv_ptr0
static void
camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
{
	union ccb      *saved_ccb;
	cam_status	status;
	struct scsi_start_stop_unit *scsi_cmd;
	int		error = 0, error_code, sense_key, asc, ascq;

	scsi_cmd = (struct scsi_start_stop_unit *)
	    &done_ccb->csio.cdb_io.cdb_bytes;
	status = done_ccb->ccb_h.status;

	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
		if (scsi_extract_sense_ccb(done_ccb,
		    &error_code, &sense_key, &asc, &ascq)) {
			/*
			 * If the error is "invalid field in CDB",
			 * and the load/eject flag is set, turn the
			 * flag off and try again.  This is just in
			 * case the drive in question barfs on the
			 * load eject flag.  The CAM code should set
			 * the load/eject flag by default for
			 * removable media.
			 */
			if ((scsi_cmd->opcode == START_STOP_UNIT) &&
			    ((scsi_cmd->how & SSS_LOEJ) != 0) &&
			     (asc == 0x24) && (ascq == 0x00)) {
				scsi_cmd->how &= ~SSS_LOEJ;
				if (status & CAM_DEV_QFRZN) {
					cam_release_devq(done_ccb->ccb_h.path,
					    0, 0, 0, 0);
					done_ccb->ccb_h.status &=
					    ~CAM_DEV_QFRZN;
				}
				xpt_action(done_ccb);
				goto out;
			}
		}
		error = cam_periph_error(done_ccb, 0,
		    SF_RETRY_UA | SF_NO_PRINT);
		if (error == ERESTART)
			goto out;
		if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
			cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
			done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
		}
	} else {
		/*
		 * If we have successfully taken a device from the not
		 * ready to ready state, re-scan the device and re-get
		 * the inquiry information.  Many devices (mostly disks)
		 * don't properly report their inquiry information unless
		 * they are spun up.
		 */
		if (scsi_cmd->opcode == START_STOP_UNIT)
			xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
	}

	/* If we tried long wait and still failed, remember that. */
	if ((periph->flags & CAM_PERIPH_RECOVERY_WAIT) &&
	    (done_ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY)) {
		periph->flags &= ~CAM_PERIPH_RECOVERY_WAIT;
		if (error != 0 && done_ccb->ccb_h.retry_count == 0)
			periph->flags |= CAM_PERIPH_RECOVERY_WAIT_FAILED;
	}

	/*
	 * After recovery action(s) completed, return to the original CCB.
	 * If the recovery CCB has failed, considering its own possible
	 * retries and recovery, assume we are back in state where we have
	 * been originally, but without recovery hopes left.  In such case,
	 * after the final attempt below, we cancel any further retries,
	 * blocking by that also any new recovery attempts for this CCB,
	 * and the result will be the final one returned to the CCB owher.
	 */
	saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
	bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
	xpt_free_ccb(saved_ccb);
	if (done_ccb->ccb_h.cbfcnp != camperiphdone)
		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
	if (error != 0)
		done_ccb->ccb_h.retry_count = 0;
	xpt_action(done_ccb);

out:
	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
	cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
}

/*
 * Generic Async Event handler.  Peripheral drivers usually
 * filter out the events that require personal attention,
 * and leave the rest to this function.
 */
void
cam_periph_async(struct cam_periph *periph, u_int32_t code,
		 struct cam_path *path, void *arg)
{
	switch (code) {
	case AC_LOST_DEVICE:
		cam_periph_invalidate(periph);
		break; 
	default:
		break;
	}
}

void
cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
{
	struct ccb_getdevstats cgds;

	xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
	cgds.ccb_h.func_code = XPT_GDEV_STATS;
	xpt_action((union ccb *)&cgds);
	cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
}

void
cam_periph_freeze_after_event(struct cam_periph *periph,
			      struct timeval* event_time, u_int duration_ms)
{
	struct timeval delta;
	struct timeval duration_tv;

	if (!timevalisset(event_time))
		return;

	microtime(&delta);
	timevalsub(&delta, event_time);
	duration_tv.tv_sec = duration_ms / 1000;
	duration_tv.tv_usec = (duration_ms % 1000) * 1000;
	if (timevalcmp(&delta, &duration_tv, <)) {
		timevalsub(&duration_tv, &delta);

		duration_ms = duration_tv.tv_sec * 1000;
		duration_ms += duration_tv.tv_usec / 1000;
		cam_freeze_devq(periph->path); 
		cam_release_devq(periph->path,
				RELSIM_RELEASE_AFTER_TIMEOUT,
				/*reduction*/0,
				/*timeout*/duration_ms,
				/*getcount_only*/0);
	}

}

static int
camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
    cam_flags camflags, u_int32_t sense_flags,
    int *openings, u_int32_t *relsim_flags,
    u_int32_t *timeout, u_int32_t *action, const char **action_string)
{
	struct cam_periph *periph;
	int error;

	switch (ccb->csio.scsi_status) {
	case SCSI_STATUS_OK:
	case SCSI_STATUS_COND_MET:
	case SCSI_STATUS_INTERMED:
	case SCSI_STATUS_INTERMED_COND_MET:
		error = 0;
		break;
	case SCSI_STATUS_CMD_TERMINATED:
	case SCSI_STATUS_CHECK_COND:
		error = camperiphscsisenseerror(ccb, orig_ccb,
					        camflags,
					        sense_flags,
					        openings,
					        relsim_flags,
					        timeout,
					        action,
					        action_string);
		break;
	case SCSI_STATUS_QUEUE_FULL:
	{
		/* no decrement */
		struct ccb_getdevstats cgds;

		/*
		 * First off, find out what the current
		 * transaction counts are.
		 */
		xpt_setup_ccb(&cgds.ccb_h,
			      ccb->ccb_h.path,
			      CAM_PRIORITY_NORMAL);
		cgds.ccb_h.func_code = XPT_GDEV_STATS;
		xpt_action((union ccb *)&cgds);

		/*
		 * If we were the only transaction active, treat
		 * the QUEUE FULL as if it were a BUSY condition.
		 */
		if (cgds.dev_active != 0) {
			int total_openings;

			/*
		 	 * Reduce the number of openings to
			 * be 1 less than the amount it took
			 * to get a queue full bounded by the
			 * minimum allowed tag count for this
			 * device.
		 	 */
			total_openings = cgds.dev_active + cgds.dev_openings;
			*openings = cgds.dev_active;
			if (*openings < cgds.mintags)
				*openings = cgds.mintags;
			if (*openings < total_openings)
				*relsim_flags = RELSIM_ADJUST_OPENINGS;
			else {
				/*
				 * Some devices report queue full for
				 * temporary resource shortages.  For
				 * this reason, we allow a minimum
				 * tag count to be entered via a
				 * quirk entry to prevent the queue
				 * count on these devices from falling
				 * to a pessimisticly low value.  We
				 * still wait for the next successful
				 * completion, however, before queueing
				 * more transactions to the device.
				 */
				*relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
			}
			*timeout = 0;
			error = ERESTART;
			*action &= ~SSQ_PRINT_SENSE;
			break;
		}
		/* FALLTHROUGH */
	}
	case SCSI_STATUS_BUSY:
		/*
		 * Restart the queue after either another
		 * command completes or a 1 second timeout.
		 */
		periph = xpt_path_periph(ccb->ccb_h.path);
		if (periph->flags & CAM_PERIPH_INVALID) {
			error = EIO;
			*action_string = "Periph was invalidated";
		} else if ((sense_flags & SF_RETRY_BUSY) != 0 ||
		    ccb->ccb_h.retry_count > 0) {
			if ((sense_flags & SF_RETRY_BUSY) == 0)
				ccb->ccb_h.retry_count--;
			error = ERESTART;
			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
				      | RELSIM_RELEASE_AFTER_CMDCMPLT;
			*timeout = 1000;
		} else {
			error = EIO;
			*action_string = "Retries exhausted";
		}
		break;
	case SCSI_STATUS_RESERV_CONFLICT:
	default:
		error = EIO;
		break;
	}
	return (error);
}

static int
camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
    cam_flags camflags, u_int32_t sense_flags,
    int *openings, u_int32_t *relsim_flags,
    u_int32_t *timeout, u_int32_t *action, const char **action_string)
{
	struct cam_periph *periph;
	union ccb *orig_ccb = ccb;
	int error, recoveryccb;

#if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
	if (ccb->ccb_h.func_code == XPT_SCSI_IO && ccb->csio.bio != NULL)
		biotrack(ccb->csio.bio, __func__);
#endif

	periph = xpt_path_periph(ccb->ccb_h.path);
	recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
	if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
		/*
		 * If error recovery is already in progress, don't attempt
		 * to process this error, but requeue it unconditionally
		 * and attempt to process it once error recovery has
		 * completed.  This failed command is probably related to
		 * the error that caused the currently active error recovery
		 * action so our  current recovery efforts should also
		 * address this command.  Be aware that the error recovery
		 * code assumes that only one recovery action is in progress
		 * on a particular peripheral instance at any given time
		 * (e.g. only one saved CCB for error recovery) so it is
		 * imperitive that we don't violate this assumption.
		 */
		error = ERESTART;
		*action &= ~SSQ_PRINT_SENSE;
	} else {
		scsi_sense_action err_action;
		struct ccb_getdev cgd;

		/*
		 * Grab the inquiry data for this device.
		 */
		xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
		xpt_action((union ccb *)&cgd);

		err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
		    sense_flags);
		error = err_action & SS_ERRMASK;

		/*
		 * Do not autostart sequential access devices
		 * to avoid unexpected tape loading.
		 */
		if ((err_action & SS_MASK) == SS_START &&
		    SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
			*action_string = "Will not autostart a "
			    "sequential access device";
			goto sense_error_done;
		}

		/*
		 * Avoid recovery recursion if recovery action is the same.
		 */
		if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
			if (((err_action & SS_MASK) == SS_START &&
			     ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
			    ((err_action & SS_MASK) == SS_TUR &&
			     (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
				err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
				*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
				*timeout = 500;
			}
		}

		/*
		 * If the recovery action will consume a retry,
		 * make sure we actually have retries available.
		 */
		if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
		 	if (ccb->ccb_h.retry_count > 0 &&
			    (periph->flags & CAM_PERIPH_INVALID) == 0)
		 		ccb->ccb_h.retry_count--;
			else {
				*action_string = "Retries exhausted";
				goto sense_error_done;
			}
		}

		if ((err_action & SS_MASK) >= SS_START) {
			/*
			 * Do common portions of commands that
			 * use recovery CCBs.
			 */
			orig_ccb = xpt_alloc_ccb_nowait();
			if (orig_ccb == NULL) {
				*action_string = "Can't allocate recovery CCB";
				goto sense_error_done;
			}
			/*
			 * Clear freeze flag for original request here, as
			 * this freeze will be dropped as part of ERESTART.
			 */
			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
			bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
		}

		switch (err_action & SS_MASK) {
		case SS_NOP:
			*action_string = "No recovery action needed";
			error = 0;
			break;
		case SS_RETRY:
			*action_string = "Retrying command (per sense data)";
			error = ERESTART;
			break;
		case SS_FAIL:
			*action_string = "Unretryable error";
			break;
		case SS_START:
		{
			int le;

			/*
			 * Send a start unit command to the device, and
			 * then retry the command.
			 */
			*action_string = "Attempting to start unit";
			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;

			/*
			 * Check for removable media and set
			 * load/eject flag appropriately.
			 */
			if (SID_IS_REMOVABLE(&cgd.inq_data))
				le = TRUE;
			else
				le = FALSE;

			scsi_start_stop(&ccb->csio,
					/*retries*/1,
					camperiphdone,
					MSG_SIMPLE_Q_TAG,
					/*start*/TRUE,
					/*load/eject*/le,
					/*immediate*/FALSE,
					SSD_FULL_SIZE,
					/*timeout*/50000);
			break;
		}
		case SS_TUR:
		{
			/*
			 * Send a Test Unit Ready to the device.
			 * If the 'many' flag is set, we send 120
			 * test unit ready commands, one every half 
			 * second.  Otherwise, we just send one TUR.
			 * We only want to do this if the retry 
			 * count has not been exhausted.
			 */
			int retries;

			if ((err_action & SSQ_MANY) != 0 && (periph->flags &
			     CAM_PERIPH_RECOVERY_WAIT_FAILED) == 0) {
				periph->flags |= CAM_PERIPH_RECOVERY_WAIT;
				*action_string = "Polling device for readiness";
				retries = 120;
			} else {
				*action_string = "Testing device for readiness";
				retries = 1;
			}
			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
			scsi_test_unit_ready(&ccb->csio,
					     retries,
					     camperiphdone,
					     MSG_SIMPLE_Q_TAG,
					     SSD_FULL_SIZE,
					     /*timeout*/5000);

			/*
			 * Accomplish our 500ms delay by deferring
			 * the release of our device queue appropriately.
			 */
			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
			*timeout = 500;
			break;
		}
		default:
			panic("Unhandled error action %x", err_action);
		}
		
		if ((err_action & SS_MASK) >= SS_START) {
			/*
			 * Drop the priority, so that the recovery
			 * CCB is the first to execute.  Freeze the queue
			 * after this command is sent so that we can
			 * restore the old csio and have it queued in
			 * the proper order before we release normal 
			 * transactions to the device.
			 */
			ccb->ccb_h.pinfo.priority--;
			ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
			ccb->ccb_h.saved_ccb_ptr = orig_ccb;
			error = ERESTART;
			*orig = orig_ccb;
		}

sense_error_done:
		*action = err_action;
	}
	return (error);
}

/*
 * Generic error handler.  Peripheral drivers usually filter
 * out the errors that they handle in a unique manner, then
 * call this function.
 */
int
cam_periph_error(union ccb *ccb, cam_flags camflags,
		 u_int32_t sense_flags)
{
	struct cam_path *newpath;
	union ccb  *orig_ccb, *scan_ccb;
	struct cam_periph *periph;
	const char *action_string;
	cam_status  status;
	int	    frozen, error, openings, devctl_err;
	u_int32_t   action, relsim_flags, timeout;

	action = SSQ_PRINT_SENSE;
	periph = xpt_path_periph(ccb->ccb_h.path);
	action_string = NULL;
	status = ccb->ccb_h.status;
	frozen = (status & CAM_DEV_QFRZN) != 0;
	status &= CAM_STATUS_MASK;
	devctl_err = openings = relsim_flags = timeout = 0;
	orig_ccb = ccb;

	/* Filter the errors that should be reported via devctl */
	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
	case CAM_CMD_TIMEOUT:
	case CAM_REQ_ABORTED:
	case CAM_REQ_CMP_ERR:
	case CAM_REQ_TERMIO:
	case CAM_UNREC_HBA_ERROR:
	case CAM_DATA_RUN_ERR:
	case CAM_SCSI_STATUS_ERROR:
	case CAM_ATA_STATUS_ERROR:
	case CAM_SMP_STATUS_ERROR:
		devctl_err++;
		break;
	default:
		break;
	}

	switch (status) {
	case CAM_REQ_CMP:
		error = 0;
		action &= ~SSQ_PRINT_SENSE;
		break;
	case CAM_SCSI_STATUS_ERROR:
		error = camperiphscsistatuserror(ccb, &orig_ccb,
		    camflags, sense_flags, &openings, &relsim_flags,
		    &timeout, &action, &action_string);
		break;
	case CAM_AUTOSENSE_FAIL:
		error = EIO;	/* we have to kill the command */
		break;
	case CAM_UA_ABORT:
	case CAM_UA_TERMIO:
	case CAM_MSG_REJECT_REC:
		/* XXX Don't know that these are correct */
		error = EIO;
		break;
	case CAM_SEL_TIMEOUT:
		if ((camflags & CAM_RETRY_SELTO) != 0) {
			if (ccb->ccb_h.retry_count > 0 &&
			    (periph->flags & CAM_PERIPH_INVALID) == 0) {
				ccb->ccb_h.retry_count--;
				error = ERESTART;

				/*
				 * Wait a bit to give the device
				 * time to recover before we try again.
				 */
				relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
				timeout = periph_selto_delay;
				break;
			}
			action_string = "Retries exhausted";
		}
		/* FALLTHROUGH */
	case CAM_DEV_NOT_THERE:
		error = ENXIO;
		action = SSQ_LOST;
		break;
	case CAM_REQ_INVALID:
	case CAM_PATH_INVALID:
	case CAM_NO_HBA:
	case CAM_PROVIDE_FAIL:
	case CAM_REQ_TOO_BIG:
	case CAM_LUN_INVALID:
	case CAM_TID_INVALID:
	case CAM_FUNC_NOTAVAIL:
		error = EINVAL;
		break;
	case CAM_SCSI_BUS_RESET:
	case CAM_BDR_SENT:
		/*
		 * Commands that repeatedly timeout and cause these
		 * kinds of error recovery actions, should return
		 * CAM_CMD_TIMEOUT, which allows us to safely assume
		 * that this command was an innocent bystander to
		 * these events and should be unconditionally
		 * retried.
		 */
	case CAM_REQUEUE_REQ:
		/* Unconditional requeue if device is still there */
		if (periph->flags & CAM_PERIPH_INVALID) {
			action_string = "Periph was invalidated";
			error = EIO;
		} else if (sense_flags & SF_NO_RETRY) {
			error = EIO;
			action_string = "Retry was blocked";
		} else {
			error = ERESTART;
			action &= ~SSQ_PRINT_SENSE;
		}
		break;
	case CAM_RESRC_UNAVAIL:
		/* Wait a bit for the resource shortage to abate. */
		timeout = periph_noresrc_delay;
		/* FALLTHROUGH */
	case CAM_BUSY:
		if (timeout == 0) {
			/* Wait a bit for the busy condition to abate. */
			timeout = periph_busy_delay;
		}
		relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
		/* FALLTHROUGH */
	case CAM_ATA_STATUS_ERROR:
	case CAM_REQ_CMP_ERR:
	case CAM_CMD_TIMEOUT:
	case CAM_UNEXP_BUSFREE:
	case CAM_UNCOR_PARITY:
	case CAM_DATA_RUN_ERR:
	default:
		if (periph->flags & CAM_PERIPH_INVALID) {
			error = EIO;
			action_string = "Periph was invalidated";
		} else if (ccb->ccb_h.retry_count == 0) {
			error = EIO;
			action_string = "Retries exhausted";
		} else if (sense_flags & SF_NO_RETRY) {
			error = EIO;
			action_string = "Retry was blocked";
		} else {
			ccb->ccb_h.retry_count--;
			error = ERESTART;
		}
		break;
	}

	if ((sense_flags & SF_PRINT_ALWAYS) ||
	    CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
		action |= SSQ_PRINT_SENSE;
	else if (sense_flags & SF_NO_PRINT)
		action &= ~SSQ_PRINT_SENSE;
	if ((action & SSQ_PRINT_SENSE) != 0)
		cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
	if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
		if (error != ERESTART) {
			if (action_string == NULL)
				action_string = "Unretryable error";
			xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
			    error, action_string);
		} else if (action_string != NULL)
			xpt_print(ccb->ccb_h.path, "%s\n", action_string);
		else {
			xpt_print(ccb->ccb_h.path,
			    "Retrying command, %d more tries remain\n",
			    ccb->ccb_h.retry_count);
		}
	}

	if (devctl_err && (error != 0 || (action & SSQ_PRINT_SENSE) != 0))
		cam_periph_devctl_notify(orig_ccb);

	if ((action & SSQ_LOST) != 0) {
		lun_id_t lun_id;

		/*
		 * For a selection timeout, we consider all of the LUNs on
		 * the target to be gone.  If the status is CAM_DEV_NOT_THERE,
		 * then we only get rid of the device(s) specified by the
		 * path in the original CCB.
		 */
		if (status == CAM_SEL_TIMEOUT)
			lun_id = CAM_LUN_WILDCARD;
		else
			lun_id = xpt_path_lun_id(ccb->ccb_h.path);

		/* Should we do more if we can't create the path?? */
		if (xpt_create_path(&newpath, periph,
				    xpt_path_path_id(ccb->ccb_h.path),
				    xpt_path_target_id(ccb->ccb_h.path),
				    lun_id) == CAM_REQ_CMP) {
			/*
			 * Let peripheral drivers know that this
			 * device has gone away.
			 */
			xpt_async(AC_LOST_DEVICE, newpath, NULL);
			xpt_free_path(newpath);
		}
	}

	/* Broadcast UNIT ATTENTIONs to all periphs. */
	if ((action & SSQ_UA) != 0)
		xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);

	/* Rescan target on "Reported LUNs data has changed" */
	if ((action & SSQ_RESCAN) != 0) {
		if (xpt_create_path(&newpath, NULL,
				    xpt_path_path_id(ccb->ccb_h.path),
				    xpt_path_target_id(ccb->ccb_h.path),
				    CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
			scan_ccb = xpt_alloc_ccb_nowait();
			if (scan_ccb != NULL) {
				scan_ccb->ccb_h.path = newpath;
				scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
				scan_ccb->crcn.flags = 0;
				xpt_rescan(scan_ccb);
			} else {
				xpt_print(newpath,
				    "Can't allocate CCB to rescan target\n");
				xpt_free_path(newpath);
			}
		}
	}

	/* Attempt a retry */
	if (error == ERESTART || error == 0) {
		if (frozen != 0)
			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
		if (error == ERESTART)
			xpt_action(ccb);
		if (frozen != 0)
			cam_release_devq(ccb->ccb_h.path,
					 relsim_flags,
					 openings,
					 timeout,
					 /*getcount_only*/0);
	}

	return (error);
}

#define CAM_PERIPH_DEVD_MSG_SIZE	256

static void
cam_periph_devctl_notify(union ccb *ccb)
{
	struct cam_periph *periph;
	struct ccb_getdev *cgd;
	struct sbuf sb;
	int serr, sk, asc, ascq;
	char *sbmsg, *type;

	sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT);
	if (sbmsg == NULL)
		return;

	sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN);

	periph = xpt_path_periph(ccb->ccb_h.path);
	sbuf_printf(&sb, "device=%s%d ", periph->periph_name,
	    periph->unit_number);

	sbuf_printf(&sb, "serial=\"");
	if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) {
		xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path,
		    CAM_PRIORITY_NORMAL);
		cgd->ccb_h.func_code = XPT_GDEV_TYPE;
		xpt_action((union ccb *)cgd);

		if (cgd->ccb_h.status == CAM_REQ_CMP)
			sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len);
		xpt_free_ccb((union ccb *)cgd);
	}
	sbuf_printf(&sb, "\" ");
	sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status);

	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
	case CAM_CMD_TIMEOUT:
		sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout);
		type = "timeout";
		break;
	case CAM_SCSI_STATUS_ERROR:
		sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status);
		if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq))
			sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ",
			    serr, sk, asc, ascq);
		type = "error";
		break;
	case CAM_ATA_STATUS_ERROR:
		sbuf_printf(&sb, "RES=\"");
		ata_res_sbuf(&ccb->ataio.res, &sb);
		sbuf_printf(&sb, "\" ");
		type = "error";
		break;
	default:
		type = "error";
		break;
	}

	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
		sbuf_printf(&sb, "CDB=\"");
		scsi_cdb_sbuf(scsiio_cdb_ptr(&ccb->csio), &sb);
		sbuf_printf(&sb, "\" ");
	} else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
		sbuf_printf(&sb, "ACB=\"");
		ata_cmd_sbuf(&ccb->ataio.cmd, &sb);
		sbuf_printf(&sb, "\" ");
	}

	if (sbuf_finish(&sb) == 0)
		devctl_notify("CAM", "periph", type, sbuf_data(&sb));
	sbuf_delete(&sb);
	free(sbmsg, M_CAMPERIPH);
}

/*
 * Sysctl to force an invalidation of the drive right now. Can be
 * called with CTLFLAG_MPSAFE since we take periph lock.
 */
int
cam_periph_invalidate_sysctl(SYSCTL_HANDLER_ARGS)
{
	struct cam_periph *periph;
	int error, value;

	periph = arg1;
	value = 0;
	error = sysctl_handle_int(oidp, &value, 0, req);
	if (error != 0 || req->newptr == NULL || value != 1)
		return (error);

	cam_periph_lock(periph);
	cam_periph_invalidate(periph);
	cam_periph_unlock(periph);

	return (0);
}