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
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
21211
21212
21213
21214
21215
21216
21217
21218
21219
21220
21221
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
21537
21538
21539
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
21939
21940
21941
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
22310
22311
22312
22313
22314
22315
22316
22317
22318
22319
22320
22321
22322
22323
22324
22325
22326
22327
22328
22329
22330
22331
22332
22333
22334
22335
22336
22337
22338
22339
22340
22341
22342
22343
22344
22345
22346
22347
22348
22349
22350
22351
22352
22353
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
22389
22390
22391
22392
22393
22394
22395
22396
22397
22398
22399
22400
22401
22402
22403
22404
22405
22406
22407
22408
22409
22410
22411
22412
22413
22414
22415
22416
22417
22418
22419
22420
22421
22422
22423
22424
22425
22426
22427
22428
22429
22430
22431
22432
22433
22434
22435
22436
22437
22438
22439
22440
22441
22442
22443
22444
22445
22446
22447
22448
22449
22450
22451
22452
22453
22454
22455
22456
22457
22458
22459
22460
22461
22462
22463
22464
22465
22466
22467
22468
22469
22470
22471
22472
22473
22474
22475
22476
22477
22478
22479
22480
22481
22482
22483
22484
22485
22486
22487
22488
22489
22490
22491
22492
22493
22494
22495
22496
22497
22498
22499
22500
22501
22502
22503
22504
22505
22506
22507
22508
22509
22510
22511
22512
22513
22514
22515
22516
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529
22530
22531
22532
22533
22534
22535
22536
22537
22538
22539
22540
22541
22542
22543
22544
22545
22546
22547
22548
22549
22550
22551
22552
22553
22554
22555
22556
22557
22558
22559
22560
22561
22562
22563
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585
22586
22587
22588
22589
22590
22591
22592
22593
22594
22595
22596
22597
22598
22599
22600
22601
22602
22603
22604
22605
22606
22607
22608
22609
22610
22611
22612
22613
22614
22615
22616
22617
22618
22619
22620
22621
22622
22623
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635
22636
22637
22638
22639
22640
22641
22642
22643
22644
22645
22646
22647
22648
22649
22650
22651
22652
22653
22654
22655
22656
22657
22658
22659
22660
22661
22662
22663
22664
22665
22666
22667
22668
22669
22670
22671
22672
22673
22674
22675
22676
22677
22678
22679
22680
22681
22682
22683
22684
22685
22686
22687
22688
22689
22690
22691
22692
22693
22694
22695
22696
22697
22698
22699
22700
22701
22702
22703
22704
22705
22706
22707
22708
22709
22710
22711
22712
22713
22714
22715
22716
22717
22718
22719
22720
22721
22722
22723
22724
22725
22726
22727
22728
22729
22730
22731
22732
22733
22734
22735
22736
22737
22738
22739
22740
22741
22742
22743
22744
22745
22746
22747
22748
22749
22750
22751
22752
22753
22754
22755
22756
22757
22758
22759
22760
22761
22762
22763
22764
22765
22766
22767
22768
22769
22770
22771
22772
22773
22774
22775
22776
22777
22778
22779
22780
22781
22782
22783
22784
22785
22786
22787
22788
22789
22790
22791
22792
22793
22794
22795
22796
22797
22798
22799
22800
22801
22802
22803
22804
22805
22806
22807
22808
22809
22810
22811
22812
22813
22814
22815
22816
22817
22818
22819
22820
22821
22822
22823
22824
22825
22826
22827
22828
22829
22830
22831
22832
22833
22834
22835
22836
22837
22838
22839
22840
22841
22842
22843
22844
22845
22846
22847
22848
22849
22850
22851
22852
22853
22854
22855
22856
22857
22858
22859
22860
22861
22862
22863
22864
22865
22866
22867
22868
22869
22870
22871
22872
22873
22874
22875
22876
22877
22878
22879
22880
22881
22882
22883
22884
22885
22886
22887
22888
22889
22890
22891
22892
22893
22894
22895
22896
22897
22898
22899
22900
22901
22902
22903
22904
22905
22906
22907
22908
22909
22910
22911
22912
22913
22914
22915
22916
22917
22918
22919
22920
22921
22922
22923
22924
22925
22926
22927
22928
22929
22930
22931
22932
22933
22934
22935
22936
22937
22938
22939
22940
22941
22942
22943
22944
22945
22946
22947
22948
22949
22950
22951
22952
22953
22954
22955
22956
22957
22958
22959
22960
22961
22962
22963
22964
22965
22966
22967
22968
22969
22970
22971
22972
22973
22974
22975
22976
22977
22978
22979
22980
22981
22982
22983
22984
22985
22986
22987
22988
22989
22990
22991
22992
22993
22994
22995
22996
22997
22998
22999
23000
23001
23002
23003
23004
23005
23006
23007
23008
23009
23010
23011
23012
23013
23014
23015
23016
23017
23018
23019
23020
23021
23022
23023
23024
23025
23026
23027
23028
23029
23030
23031
23032
23033
23034
23035
23036
23037
23038
23039
23040
23041
23042
23043
23044
23045
23046
23047
23048
23049
23050
23051
23052
23053
23054
23055
23056
23057
23058
23059
23060
23061
23062
23063
23064
23065
23066
23067
23068
23069
23070
23071
23072
23073
23074
23075
23076
23077
23078
23079
23080
23081
23082
23083
23084
23085
23086
23087
23088
23089
23090
23091
23092
23093
23094
23095
23096
23097
23098
23099
23100
23101
23102
23103
23104
23105
23106
23107
23108
23109
23110
23111
23112
23113
23114
23115
23116
23117
23118
23119
23120
23121
23122
23123
23124
23125
23126
23127
23128
23129
23130
23131
23132
23133
23134
23135
23136
23137
23138
23139
23140
23141
23142
23143
23144
23145
23146
23147
23148
23149
23150
23151
23152
23153
23154
23155
23156
23157
23158
23159
23160
23161
23162
23163
23164
23165
23166
23167
23168
23169
23170
23171
23172
23173
23174
23175
23176
23177
23178
23179
23180
23181
23182
23183
23184
23185
23186
23187
23188
23189
23190
23191
23192
23193
23194
23195
23196
23197
23198
23199
23200
23201
23202
23203
23204
23205
23206
23207
23208
23209
23210
23211
23212
23213
23214
23215
23216
23217
23218
23219
23220
23221
23222
23223
23224
23225
23226
23227
23228
23229
23230
23231
23232
23233
23234
23235
23236
23237
23238
23239
23240
23241
23242
23243
23244
23245
23246
23247
23248
23249
23250
23251
23252
23253
23254
23255
23256
23257
23258
23259
23260
23261
23262
23263
23264
23265
23266
23267
23268
23269
23270
23271
23272
23273
23274
23275
23276
23277
23278
23279
23280
23281
23282
23283
23284
23285
23286
23287
23288
23289
23290
23291
23292
23293
23294
23295
23296
23297
23298
23299
23300
23301
23302
23303
23304
23305
23306
23307
23308
23309
23310
23311
23312
23313
23314
23315
23316
23317
23318
23319
23320
23321
23322
23323
23324
23325
23326
23327
23328
23329
23330
23331
23332
23333
23334
23335
23336
23337
23338
23339
23340
23341
23342
23343
23344
23345
23346
23347
23348
23349
23350
23351
23352
23353
23354
23355
23356
23357
23358
23359
23360
23361
23362
23363
23364
23365
23366
23367
23368
23369
23370
23371
23372
23373
23374
23375
23376
23377
23378
23379
23380
23381
23382
23383
23384
23385
23386
23387
23388
23389
23390
23391
23392
23393
23394
23395
23396
23397
23398
23399
23400
23401
23402
23403
23404
23405
23406
23407
23408
23409
23410
23411
23412
23413
23414
23415
23416
23417
23418
23419
23420
23421
23422
23423
23424
23425
23426
23427
23428
23429
23430
23431
23432
23433
23434
23435
23436
23437
23438
23439
23440
23441
23442
23443
23444
23445
23446
23447
23448
23449
23450
23451
23452
23453
23454
23455
23456
23457
23458
23459
23460
23461
23462
23463
23464
23465
23466
23467
23468
23469
23470
23471
23472
23473
23474
23475
23476
23477
23478
23479
23480
23481
23482
23483
23484
23485
23486
23487
23488
23489
23490
23491
23492
23493
23494
23495
23496
23497
23498
23499
23500
23501
23502
23503
23504
23505
23506
23507
23508
23509
23510
23511
23512
23513
23514
23515
23516
23517
23518
23519
23520
23521
23522
23523
23524
23525
23526
23527
23528
23529
23530
23531
23532
23533
23534
23535
23536
23537
23538
23539
23540
23541
23542
23543
23544
23545
23546
23547
23548
23549
23550
23551
23552
23553
23554
23555
23556
23557
23558
23559
23560
23561
23562
23563
23564
23565
23566
23567
23568
23569
23570
23571
23572
23573
23574
23575
23576
23577
23578
23579
23580
23581
23582
23583
23584
23585
23586
23587
23588
23589
23590
23591
23592
23593
23594
23595
23596
23597
23598
23599
23600
23601
23602
23603
23604
23605
23606
23607
23608
23609
23610
23611
23612
23613
23614
23615
23616
23617
23618
23619
23620
23621
23622
23623
23624
23625
23626
23627
23628
23629
23630
23631
23632
23633
23634
23635
23636
23637
23638
23639
23640
23641
23642
23643
23644
23645
23646
23647
23648
23649
23650
23651
23652
23653
23654
23655
23656
23657
23658
23659
23660
23661
23662
23663
23664
23665
23666
23667
23668
23669
23670
23671
23672
23673
23674
23675
23676
23677
23678
23679
23680
23681
23682
23683
23684
23685
23686
23687
23688
23689
23690
23691
23692
23693
23694
23695
23696
23697
23698
23699
23700
23701
23702
23703
23704
23705
23706
23707
23708
23709
23710
23711
23712
23713
23714
23715
23716
23717
23718
23719
23720
23721
23722
23723
23724
23725
23726
23727
23728
23729
23730
23731
23732
23733
23734
23735
23736
23737
23738
23739
23740
23741
23742
23743
23744
23745
23746
23747
23748
23749
23750
23751
23752
23753
23754
23755
23756
23757
23758
23759
23760
23761
23762
23763
23764
23765
23766
23767
23768
23769
23770
23771
23772
23773
23774
23775
23776
23777
23778
23779
23780
23781
23782
23783
23784
23785
23786
23787
23788
23789
23790
23791
23792
23793
23794
23795
23796
23797
23798
23799
23800
23801
23802
23803
23804
23805
23806
23807
23808
23809
23810
23811
23812
23813
23814
23815
23816
23817
23818
23819
23820
23821
23822
23823
23824
23825
23826
23827
23828
23829
23830
23831
23832
23833
23834
23835
23836
23837
23838
23839
23840
23841
23842
23843
23844
23845
23846
23847
23848
23849
23850
23851
23852
23853
23854
23855
23856
23857
23858
23859
23860
23861
23862
23863
23864
23865
23866
23867
23868
23869
23870
23871
23872
23873
23874
23875
23876
23877
23878
23879
23880
23881
23882
23883
23884
23885
23886
23887
23888
23889
23890
23891
23892
23893
23894
23895
23896
23897
23898
23899
23900
23901
23902
23903
23904
23905
23906
23907
23908
23909
23910
23911
23912
23913
23914
23915
23916
23917
23918
23919
23920
23921
23922
23923
23924
23925
23926
23927
23928
23929
23930
23931
23932
23933
23934
23935
23936
23937
23938
23939
23940
23941
23942
23943
23944
23945
23946
23947
23948
23949
23950
23951
23952
23953
23954
23955
23956
23957
23958
23959
23960
23961
23962
23963
23964
23965
23966
23967
23968
23969
23970
23971
23972
23973
23974
23975
23976
23977
23978
23979
23980
23981
23982
23983
23984
23985
23986
23987
23988
23989
23990
23991
23992
23993
23994
23995
23996
23997
23998
23999
24000
24001
24002
24003
24004
24005
24006
24007
24008
24009
24010
24011
24012
24013
24014
24015
24016
24017
24018
24019
24020
24021
24022
24023
24024
24025
24026
24027
24028
24029
24030
24031
24032
24033
24034
24035
24036
24037
24038
24039
24040
24041
24042
24043
24044
24045
24046
24047
24048
24049
24050
24051
24052
24053
24054
24055
24056
24057
24058
24059
24060
24061
24062
24063
24064
24065
24066
24067
24068
24069
24070
24071
24072
24073
24074
24075
24076
24077
24078
24079
24080
24081
24082
24083
24084
24085
24086
24087
24088
24089
24090
24091
24092
24093
24094
24095
24096
24097
24098
24099
24100
24101
24102
24103
24104
24105
24106
24107
24108
24109
24110
24111
24112
24113
24114
24115
24116
24117
24118
24119
24120
24121
24122
24123
24124
24125
24126
24127
24128
24129
24130
24131
24132
24133
24134
24135
24136
24137
24138
24139
24140
24141
24142
24143
24144
24145
24146
24147
24148
24149
24150
24151
24152
24153
24154
24155
24156
24157
24158
24159
24160
24161
24162
24163
24164
24165
24166
24167
24168
24169
24170
24171
24172
24173
24174
24175
24176
24177
24178
24179
24180
24181
24182
24183
24184
24185
24186
24187
24188
24189
24190
24191
24192
24193
24194
24195
24196
24197
24198
24199
24200
24201
24202
24203
24204
24205
24206
24207
24208
24209
24210
24211
24212
24213
24214
24215
24216
24217
24218
24219
24220
24221
24222
24223
24224
24225
24226
24227
24228
24229
24230
24231
24232
24233
24234
24235
24236
24237
24238
24239
24240
24241
24242
24243
24244
24245
24246
24247
24248
24249
24250
24251
24252
24253
24254
24255
24256
24257
24258
24259
24260
24261
24262
24263
24264
24265
24266
24267
24268
24269
24270
24271
24272
24273
24274
24275
24276
24277
24278
24279
24280
24281
24282
24283
24284
24285
24286
24287
24288
24289
24290
24291
24292
24293
24294
24295
24296
24297
24298
24299
24300
24301
24302
24303
24304
24305
24306
24307
24308
24309
24310
24311
24312
24313
24314
24315
24316
24317
24318
24319
24320
24321
24322
24323
24324
24325
24326
24327
24328
24329
24330
24331
24332
24333
24334
24335
24336
24337
24338
24339
24340
24341
24342
24343
24344
24345
24346
24347
24348
24349
24350
24351
24352
24353
24354
24355
24356
24357
24358
24359
24360
24361
24362
24363
24364
24365
24366
24367
24368
24369
24370
24371
24372
24373
24374
24375
24376
24377
24378
24379
24380
24381
24382
24383
24384
24385
24386
24387
24388
24389
24390
24391
24392
24393
24394
24395
24396
24397
24398
24399
24400
24401
24402
24403
24404
24405
24406
24407
24408
24409
24410
24411
24412
24413
24414
24415
24416
24417
24418
24419
24420
24421
24422
24423
24424
24425
24426
24427
24428
24429
24430
24431
24432
24433
24434
24435
24436
24437
24438
24439
24440
24441
24442
24443
24444
24445
24446
24447
24448
24449
24450
24451
24452
24453
24454
24455
24456
24457
24458
24459
24460
24461
24462
24463
24464
24465
24466
24467
24468
24469
24470
24471
24472
24473
24474
24475
24476
24477
24478
24479
24480
24481
24482
24483
24484
24485
24486
24487
24488
24489
24490
24491
24492
24493
24494
24495
24496
24497
24498
24499
24500
24501
24502
24503
24504
24505
24506
24507
24508
24509
24510
24511
24512
24513
24514
24515
24516
24517
24518
24519
24520
24521
24522
24523
24524
24525
24526
24527
24528
24529
24530
24531
24532
24533
24534
24535
24536
24537
24538
24539
24540
24541
24542
24543
24544
24545
24546
24547
24548
24549
24550
24551
24552
24553
24554
24555
24556
24557
24558
24559
24560
24561
24562
24563
24564
24565
24566
24567
24568
24569
24570
24571
24572
24573
24574
24575
24576
24577
24578
24579
24580
24581
24582
24583
24584
24585
24586
24587
24588
24589
24590
24591
24592
24593
24594
24595
24596
24597
24598
24599
24600
24601
24602
24603
24604
24605
24606
24607
24608
24609
24610
24611
24612
24613
24614
24615
24616
24617
24618
24619
24620
24621
24622
24623
24624
24625
24626
24627
24628
24629
24630
24631
24632
24633
24634
24635
24636
24637
24638
24639
24640
24641
24642
24643
24644
24645
24646
24647
24648
24649
24650
24651
24652
24653
24654
24655
24656
24657
24658
24659
24660
24661
24662
24663
24664
24665
24666
24667
24668
24669
24670
24671
24672
24673
24674
24675
24676
24677
24678
24679
24680
24681
24682
24683
24684
24685
24686
24687
24688
24689
24690
24691
24692
24693
24694
24695
24696
24697
24698
24699
24700
24701
24702
24703
24704
24705
24706
24707
24708
24709
24710
24711
24712
24713
24714
24715
24716
24717
24718
24719
24720
24721
24722
24723
24724
24725
24726
24727
24728
24729
24730
24731
24732
24733
24734
24735
24736
24737
24738
24739
24740
24741
24742
24743
24744
24745
24746
24747
24748
24749
24750
24751
24752
24753
24754
24755
24756
24757
24758
24759
24760
24761
24762
24763
24764
24765
24766
24767
24768
24769
24770
24771
24772
24773
24774
24775
24776
24777
24778
24779
24780
24781
24782
24783
24784
24785
24786
24787
24788
24789
24790
24791
24792
24793
24794
24795
24796
24797
24798
24799
24800
24801
24802
24803
24804
24805
24806
24807
24808
24809
24810
24811
24812
24813
24814
24815
24816
24817
24818
24819
24820
24821
24822
24823
24824
24825
24826
24827
24828
24829
24830
24831
24832
24833
24834
24835
24836
24837
24838
24839
24840
24841
24842
24843
24844
24845
24846
24847
24848
24849
24850
24851
24852
24853
24854
24855
24856
24857
24858
24859
24860
24861
24862
24863
24864
24865
24866
24867
24868
24869
24870
24871
24872
24873
24874
24875
24876
24877
24878
24879
24880
24881
24882
24883
24884
24885
24886
24887
24888
24889
24890
24891
24892
24893
24894
24895
24896
24897
24898
24899
24900
24901
24902
24903
24904
24905
24906
24907
24908
24909
24910
24911
24912
24913
24914
24915
24916
24917
24918
24919
24920
24921
24922
24923
24924
24925
24926
24927
24928
24929
24930
24931
24932
24933
24934
24935
24936
24937
24938
24939
24940
24941
24942
24943
24944
24945
24946
24947
24948
24949
24950
24951
24952
24953
24954
24955
24956
24957
24958
24959
24960
24961
24962
24963
24964
24965
24966
24967
24968
24969
24970
24971
24972
24973
24974
24975
24976
24977
24978
24979
24980
24981
24982
24983
24984
24985
24986
24987
24988
24989
24990
24991
24992
24993
24994
24995
24996
24997
24998
24999
25000
25001
25002
25003
25004
25005
25006
25007
25008
25009
25010
25011
25012
25013
25014
25015
25016
25017
25018
25019
25020
25021
25022
25023
25024
25025
25026
25027
25028
25029
25030
25031
25032
25033
25034
25035
25036
25037
25038
25039
25040
25041
25042
25043
25044
25045
25046
25047
25048
25049
25050
25051
25052
25053
25054
25055
25056
25057
25058
25059
25060
25061
25062
25063
25064
25065
25066
25067
25068
25069
25070
25071
25072
25073
25074
25075
25076
25077
25078
25079
25080
25081
25082
25083
25084
25085
25086
25087
25088
25089
25090
25091
25092
25093
25094
25095
25096
25097
25098
25099
25100
25101
25102
25103
25104
25105
25106
25107
25108
25109
25110
25111
25112
25113
25114
25115
25116
25117
25118
25119
25120
25121
25122
25123
25124
25125
25126
25127
25128
25129
25130
25131
25132
25133
25134
25135
25136
25137
25138
25139
25140
25141
25142
25143
25144
25145
25146
25147
25148
25149
25150
25151
25152
25153
25154
25155
25156
25157
25158
25159
25160
25161
25162
25163
25164
25165
25166
25167
25168
25169
25170
25171
25172
25173
25174
25175
25176
25177
25178
25179
25180
25181
25182
25183
25184
25185
25186
25187
25188
25189
25190
25191
25192
25193
25194
25195
25196
25197
25198
25199
25200
25201
25202
25203
25204
25205
25206
25207
25208
25209
25210
25211
25212
25213
25214
25215
25216
25217
25218
25219
25220
25221
25222
25223
25224
25225
25226
25227
25228
25229
25230
25231
25232
25233
25234
25235
25236
25237
25238
25239
25240
25241
25242
25243
25244
25245
25246
25247
25248
25249
25250
25251
25252
25253
25254
25255
25256
25257
25258
25259
25260
25261
25262
25263
25264
25265
25266
25267
25268
25269
25270
25271
25272
25273
25274
25275
25276
25277
25278
25279
25280
25281
25282
25283
25284
25285
25286
25287
25288
25289
25290
25291
25292
25293
25294
25295
25296
25297
25298
25299
25300
25301
25302
25303
25304
25305
25306
25307
25308
25309
25310
25311
25312
25313
25314
25315
25316
25317
25318
25319
25320
25321
25322
25323
25324
25325
25326
25327
25328
25329
25330
25331
25332
25333
25334
25335
25336
25337
25338
25339
25340
25341
25342
25343
25344
25345
25346
25347
25348
25349
25350
25351
25352
25353
25354
25355
25356
25357
25358
25359
25360
25361
25362
25363
25364
25365
25366
25367
25368
25369
25370
25371
25372
25373
25374
25375
25376
25377
25378
25379
25380
25381
25382
25383
25384
25385
25386
25387
25388
25389
25390
25391
25392
25393
25394
25395
25396
25397
25398
25399
25400
25401
25402
25403
25404
25405
25406
25407
25408
25409
25410
25411
25412
25413
25414
25415
25416
25417
25418
25419
25420
25421
25422
25423
25424
25425
25426
25427
25428
25429
25430
25431
25432
25433
25434
25435
25436
25437
25438
25439
25440
25441
25442
25443
25444
25445
25446
25447
25448
25449
25450
25451
25452
25453
25454
25455
25456
25457
25458
25459
25460
25461
25462
25463
25464
25465
25466
25467
25468
25469
25470
25471
25472
25473
25474
25475
25476
25477
25478
25479
25480
25481
25482
25483
25484
25485
25486
25487
25488
25489
25490
25491
25492
25493
25494
25495
25496
25497
25498
25499
25500
25501
25502
25503
25504
25505
25506
25507
25508
25509
25510
25511
25512
25513
25514
25515
25516
25517
25518
25519
25520
25521
25522
25523
25524
25525
25526
25527
25528
25529
25530
25531
25532
25533
25534
25535
25536
25537
25538
25539
25540
25541
25542
25543
25544
25545
25546
25547
25548
25549
25550
25551
25552
25553
25554
25555
25556
25557
25558
25559
25560
25561
25562
25563
25564
25565
25566
25567
25568
25569
25570
25571
25572
25573
25574
25575
25576
25577
25578
25579
25580
25581
25582
25583
25584
25585
25586
25587
25588
25589
25590
25591
25592
25593
25594
25595
25596
25597
25598
25599
25600
25601
25602
25603
25604
25605
25606
25607
25608
25609
25610
25611
25612
25613
25614
25615
25616
25617
25618
25619
25620
25621
25622
25623
25624
25625
25626
25627
25628
25629
25630
25631
25632
25633
25634
25635
25636
25637
25638
25639
25640
25641
25642
25643
25644
25645
25646
25647
25648
25649
25650
25651
25652
25653
25654
25655
25656
25657
25658
25659
25660
25661
25662
25663
25664
25665
25666
25667
25668
25669
25670
25671
25672
25673
25674
25675
25676
25677
25678
25679
25680
25681
25682
25683
25684
25685
25686
25687
25688
25689
25690
25691
25692
25693
25694
25695
25696
25697
25698
25699
25700
25701
25702
25703
25704
25705
25706
25707
25708
25709
25710
25711
25712
25713
25714
25715
25716
25717
25718
25719
25720
25721
25722
25723
25724
25725
25726
25727
25728
25729
25730
25731
25732
25733
25734
25735
25736
25737
25738
25739
25740
25741
25742
25743
25744
25745
25746
25747
25748
25749
25750
25751
25752
25753
25754
25755
25756
25757
25758
25759
25760
25761
25762
25763
25764
25765
25766
25767
25768
25769
25770
25771
25772
25773
25774
25775
25776
25777
25778
25779
25780
25781
25782
25783
25784
25785
25786
25787
25788
25789
25790
25791
25792
25793
25794
25795
25796
25797
25798
25799
25800
25801
25802
25803
25804
25805
25806
25807
25808
25809
25810
25811
25812
25813
25814
25815
25816
25817
25818
25819
25820
25821
25822
25823
25824
25825
25826
25827
25828
25829
25830
25831
25832
25833
25834
25835
25836
25837
25838
25839
25840
25841
25842
25843
25844
25845
25846
25847
25848
25849
25850
25851
25852
25853
25854
25855
25856
25857
25858
25859
25860
25861
25862
25863
25864
25865
25866
25867
25868
25869
25870
25871
25872
25873
25874
25875
25876
25877
25878
25879
25880
25881
25882
25883
25884
25885
25886
25887
25888
25889
25890
25891
25892
25893
25894
25895
25896
25897
25898
25899
25900
25901
25902
25903
25904
25905
25906
25907
25908
25909
25910
25911
25912
25913
25914
25915
25916
25917
25918
25919
25920
25921
25922
25923
25924
25925
25926
25927
25928
25929
25930
25931
25932
25933
25934
25935
25936
25937
25938
25939
25940
25941
25942
25943
25944
25945
25946
25947
25948
25949
25950
25951
25952
25953
25954
25955
25956
25957
25958
25959
25960
25961
25962
25963
25964
25965
25966
25967
25968
25969
25970
25971
25972
25973
25974
25975
25976
25977
25978
25979
25980
25981
25982
25983
25984
25985
25986
25987
25988
25989
25990
25991
25992
25993
25994
25995
25996
25997
25998
25999
26000
26001
26002
26003
26004
26005
26006
26007
26008
26009
26010
26011
26012
26013
26014
26015
26016
26017
26018
26019
26020
26021
26022
26023
26024
26025
26026
26027
26028
26029
26030
26031
26032
26033
26034
26035
26036
26037
26038
26039
26040
26041
26042
26043
26044
26045
26046
26047
26048
26049
26050
26051
26052
26053
26054
26055
26056
26057
26058
26059
26060
26061
26062
26063
26064
26065
26066
26067
26068
26069
26070
26071
26072
26073
26074
26075
26076
26077
26078
26079
26080
26081
26082
26083
26084
26085
26086
26087
26088
26089
26090
26091
26092
26093
26094
26095
26096
26097
26098
26099
26100
26101
26102
26103
26104
26105
26106
26107
26108
26109
26110
26111
26112
26113
26114
26115
26116
26117
26118
26119
26120
26121
26122
26123
26124
26125
26126
26127
26128
26129
26130
26131
26132
26133
26134
26135
26136
26137
26138
26139
26140
26141
26142
26143
26144
26145
26146
26147
26148
26149
26150
26151
26152
26153
26154
26155
26156
26157
26158
26159
26160
26161
26162
26163
26164
26165
26166
26167
26168
26169
26170
26171
26172
26173
26174
26175
26176
26177
26178
26179
26180
26181
26182
26183
26184
26185
26186
26187
26188
26189
26190
26191
26192
26193
26194
26195
26196
26197
26198
26199
26200
26201
26202
26203
26204
26205
26206
26207
26208
26209
26210
26211
26212
26213
26214
26215
26216
26217
26218
26219
26220
26221
26222
26223
26224
26225
26226
26227
26228
26229
26230
26231
26232
26233
26234
26235
26236
26237
26238
26239
26240
26241
26242
26243
26244
26245
26246
26247
26248
26249
26250
26251
26252
26253
26254
26255
26256
26257
26258
26259
26260
26261
26262
26263
26264
26265
26266
26267
26268
26269
26270
26271
26272
26273
26274
26275
26276
26277
26278
26279
26280
26281
26282
26283
26284
26285
26286
26287
26288
26289
26290
26291
26292
26293
26294
26295
26296
26297
26298
26299
26300
26301
26302
26303
26304
26305
26306
26307
26308
26309
26310
26311
26312
26313
26314
26315
26316
26317
26318
26319
26320
26321
26322
26323
26324
26325
26326
26327
26328
26329
26330
26331
26332
26333
26334
26335
26336
26337
26338
26339
26340
26341
26342
26343
26344
26345
26346
26347
26348
26349
26350
26351
26352
26353
26354
26355
26356
26357
26358
26359
26360
26361
26362
26363
26364
26365
26366
26367
26368
26369
26370
26371
26372
26373
26374
26375
26376
26377
26378
26379
26380
26381
26382
26383
26384
26385
26386
26387
26388
26389
26390
26391
26392
26393
26394
26395
26396
26397
26398
26399
26400
26401
26402
26403
26404
26405
26406
26407
26408
26409
26410
26411
26412
26413
26414
26415
26416
26417
26418
26419
26420
26421
26422
26423
26424
26425
26426
26427
26428
26429
26430
26431
26432
26433
26434
26435
26436
26437
26438
26439
26440
26441
26442
26443
26444
26445
26446
26447
26448
26449
26450
26451
26452
26453
26454
26455
26456
26457
26458
26459
26460
26461
26462
26463
26464
26465
26466
26467
26468
26469
26470
26471
26472
26473
26474
26475
26476
26477
26478
26479
26480
26481
26482
26483
26484
26485
26486
26487
26488
26489
26490
26491
26492
26493
26494
26495
26496
26497
26498
26499
26500
26501
26502
26503
26504
26505
26506
26507
26508
26509
26510
26511
26512
26513
26514
26515
26516
26517
26518
26519
26520
26521
26522
26523
26524
26525
26526
26527
26528
26529
26530
26531
26532
26533
26534
26535
26536
26537
26538
26539
26540
26541
26542
26543
26544
26545
26546
26547
26548
26549
26550
26551
26552
26553
26554
26555
26556
26557
26558
26559
26560
26561
26562
26563
26564
26565
26566
26567
26568
26569
26570
26571
26572
26573
26574
26575
26576
26577
26578
26579
26580
26581
26582
26583
26584
26585
26586
26587
26588
26589
26590
26591
26592
26593
26594
26595
26596
26597
26598
26599
26600
26601
26602
26603
26604
26605
26606
26607
26608
26609
26610
26611
26612
26613
26614
26615
26616
26617
26618
26619
26620
26621
26622
26623
26624
26625
26626
26627
26628
26629
26630
26631
26632
26633
26634
26635
26636
26637
26638
26639
26640
26641
26642
26643
26644
26645
26646
26647
26648
26649
26650
26651
26652
26653
26654
26655
26656
26657
26658
26659
26660
26661
26662
26663
26664
26665
26666
26667
26668
26669
26670
26671
26672
26673
26674
26675
26676
26677
26678
26679
26680
26681
26682
26683
26684
26685
26686
26687
26688
26689
26690
26691
26692
26693
26694
26695
26696
26697
26698
26699
26700
26701
26702
26703
26704
26705
26706
26707
26708
26709
26710
26711
26712
26713
26714
26715
26716
26717
26718
26719
26720
26721
26722
26723
26724
26725
26726
26727
26728
26729
26730
26731
26732
26733
26734
26735
26736
26737
26738
26739
26740
26741
26742
26743
26744
26745
26746
26747
26748
26749
26750
26751
26752
26753
26754
26755
26756
26757
26758
26759
26760
26761
26762
26763
26764
26765
26766
26767
26768
26769
26770
26771
26772
26773
26774
26775
26776
26777
26778
26779
26780
26781
26782
26783
26784
26785
26786
26787
26788
26789
26790
26791
26792
26793
26794
26795
26796
26797
26798
26799
26800
26801
26802
26803
26804
26805
26806
26807
26808
26809
26810
26811
26812
26813
26814
26815
26816
26817
26818
26819
26820
26821
26822
26823
26824
26825
26826
26827
26828
26829
26830
26831
26832
26833
26834
26835
26836
26837
26838
26839
26840
26841
26842
26843
26844
26845
26846
26847
26848
26849
26850
26851
26852
26853
26854
26855
26856
26857
26858
26859
26860
26861
26862
26863
26864
26865
26866
26867
26868
26869
26870
26871
26872
26873
26874
26875
26876
26877
26878
26879
26880
26881
26882
26883
26884
26885
26886
26887
26888
26889
26890
26891
26892
26893
26894
26895
26896
26897
26898
26899
26900
26901
26902
26903
26904
26905
26906
26907
26908
26909
26910
26911
26912
26913
26914
26915
26916
26917
26918
26919
26920
26921
26922
26923
26924
26925
26926
26927
26928
26929
26930
26931
26932
26933
26934
26935
26936
26937
26938
26939
26940
26941
26942
26943
26944
26945
26946
26947
26948
26949
26950
26951
26952
26953
26954
26955
26956
26957
26958
26959
26960
26961
26962
26963
26964
26965
26966
26967
26968
26969
26970
26971
26972
26973
26974
26975
26976
26977
26978
26979
26980
26981
26982
26983
26984
26985
26986
26987
26988
26989
26990
26991
26992
26993
26994
26995
26996
26997
26998
26999
27000
27001
27002
27003
27004
27005
27006
27007
27008
27009
27010
27011
27012
27013
27014
27015
27016
27017
27018
27019
27020
27021
27022
27023
27024
27025
27026
27027
27028
27029
27030
27031
27032
27033
27034
27035
27036
27037
27038
27039
27040
27041
27042
27043
27044
27045
27046
27047
27048
27049
27050
27051
27052
27053
27054
27055
27056
27057
27058
27059
27060
27061
27062
27063
27064
27065
27066
27067
27068
27069
27070
27071
27072
27073
27074
27075
27076
27077
27078
27079
27080
27081
27082
27083
27084
27085
27086
27087
27088
27089
27090
27091
27092
27093
27094
27095
27096
27097
27098
27099
27100
27101
27102
27103
27104
27105
27106
27107
27108
27109
27110
27111
27112
27113
27114
27115
27116
27117
27118
27119
27120
27121
27122
27123
27124
27125
27126
27127
27128
27129
27130
27131
27132
27133
27134
27135
27136
27137
27138
27139
27140
27141
27142
27143
27144
27145
27146
27147
27148
27149
27150
27151
27152
27153
27154
27155
27156
27157
27158
27159
27160
27161
27162
27163
27164
27165
27166
27167
27168
27169
27170
27171
27172
27173
27174
27175
27176
27177
27178
27179
27180
27181
27182
27183
27184
27185
27186
27187
27188
27189
27190
27191
27192
27193
27194
27195
27196
27197
27198
27199
27200
27201
27202
27203
27204
27205
27206
27207
27208
27209
27210
27211
27212
27213
27214
27215
27216
27217
27218
27219
27220
27221
27222
27223
27224
27225
27226
27227
27228
27229
27230
27231
27232
27233
27234
27235
27236
27237
27238
27239
27240
27241
27242
27243
27244
27245
27246
27247
27248
27249
27250
27251
27252
27253
27254
27255
27256
27257
27258
27259
27260
27261
27262
27263
27264
27265
27266
27267
27268
27269
27270
27271
27272
27273
27274
27275
27276
27277
27278
27279
27280
27281
27282
27283
27284
27285
27286
27287
27288
27289
27290
27291
27292
27293
27294
27295
27296
27297
27298
27299
27300
27301
27302
27303
27304
27305
27306
27307
27308
27309
27310
27311
27312
27313
27314
27315
27316
27317
27318
27319
27320
27321
27322
27323
27324
27325
27326
27327
27328
27329
27330
27331
27332
27333
27334
27335
27336
27337
27338
27339
27340
27341
27342
27343
27344
27345
27346
27347
27348
27349
27350
27351
27352
27353
27354
27355
27356
27357
27358
27359
27360
27361
27362
27363
27364
27365
27366
27367
27368
27369
27370
27371
27372
27373
27374
27375
27376
27377
27378
27379
27380
27381
27382
27383
27384
27385
27386
27387
27388
27389
27390
27391
27392
27393
27394
27395
27396
27397
27398
27399
27400
27401
27402
27403
27404
27405
27406
27407
27408
27409
27410
27411
27412
27413
27414
27415
27416
27417
27418
27419
27420
27421
27422
27423
27424
27425
27426
27427
27428
27429
27430
27431
27432
27433
27434
27435
27436
27437
27438
27439
27440
27441
27442
27443
27444
27445
27446
27447
27448
27449
27450
27451
27452
27453
27454
27455
27456
27457
27458
27459
27460
27461
27462
27463
27464
27465
27466
27467
27468
27469
27470
27471
27472
27473
27474
27475
27476
27477
27478
27479
27480
27481
27482
27483
27484
27485
27486
27487
27488
27489
27490
27491
27492
27493
27494
27495
27496
27497
27498
27499
27500
27501
27502
27503
27504
27505
27506
27507
27508
27509
27510
27511
27512
27513
27514
27515
27516
27517
27518
27519
27520
27521
27522
27523
27524
27525
27526
27527
27528
27529
27530
27531
27532
27533
27534
27535
27536
27537
27538
27539
27540
27541
27542
27543
27544
27545
27546
27547
27548
27549
27550
27551
27552
27553
27554
27555
27556
27557
27558
27559
27560
27561
27562
27563
27564
27565
27566
27567
27568
27569
27570
27571
27572
27573
27574
27575
27576
27577
27578
27579
27580
27581
27582
27583
27584
27585
27586
27587
27588
27589
27590
27591
27592
27593
27594
27595
27596
27597
27598
27599
27600
27601
27602
27603
27604
27605
27606
27607
27608
27609
27610
27611
27612
27613
27614
27615
27616
27617
27618
27619
27620
27621
27622
27623
27624
27625
27626
27627
27628
27629
27630
27631
27632
27633
27634
27635
27636
27637
27638
27639
27640
27641
27642
27643
27644
27645
27646
27647
27648
27649
27650
27651
27652
27653
27654
27655
27656
27657
27658
27659
27660
27661
27662
27663
27664
27665
27666
27667
27668
27669
27670
27671
27672
27673
27674
27675
27676
27677
27678
27679
27680
27681
27682
27683
27684
27685
27686
27687
27688
27689
27690
27691
27692
27693
27694
27695
27696
27697
27698
27699
27700
27701
27702
27703
27704
27705
27706
27707
27708
27709
27710
27711
27712
27713
27714
27715
27716
27717
27718
27719
27720
27721
27722
27723
27724
27725
27726
27727
27728
27729
27730
27731
27732
27733
27734
27735
27736
27737
27738
27739
27740
27741
27742
27743
27744
27745
27746
27747
27748
27749
27750
27751
27752
27753
27754
27755
27756
27757
27758
27759
27760
27761
27762
27763
27764
27765
27766
27767
27768
27769
27770
27771
27772
27773
27774
27775
27776
27777
27778
27779
27780
27781
27782
27783
27784
27785
27786
27787
27788
27789
27790
27791
27792
27793
27794
27795
27796
27797
27798
27799
27800
27801
27802
27803
27804
27805
27806
27807
27808
27809
27810
27811
27812
27813
27814
27815
27816
27817
27818
27819
27820
27821
27822
27823
27824
27825
27826
27827
27828
27829
27830
27831
27832
27833
27834
27835
27836
27837
27838
27839
27840
27841
27842
27843
27844
27845
27846
27847
27848
27849
27850
27851
27852
27853
27854
27855
27856
27857
27858
27859
27860
27861
27862
27863
27864
27865
27866
27867
27868
27869
27870
27871
27872
27873
27874
27875
27876
27877
27878
27879
27880
27881
27882
27883
27884
27885
27886
27887
27888
27889
27890
27891
27892
27893
27894
27895
27896
27897
27898
27899
27900
27901
27902
27903
27904
27905
27906
27907
27908
27909
27910
27911
27912
27913
27914
27915
27916
27917
27918
27919
27920
27921
27922
27923
27924
27925
27926
27927
27928
27929
27930
27931
27932
27933
27934
27935
27936
27937
27938
27939
27940
27941
27942
27943
27944
27945
27946
27947
27948
27949
27950
27951
27952
27953
27954
27955
27956
27957
27958
27959
27960
27961
27962
27963
27964
27965
27966
27967
27968
27969
27970
27971
27972
27973
27974
27975
27976
27977
27978
27979
27980
27981
27982
27983
27984
27985
27986
27987
27988
27989
27990
27991
27992
27993
27994
27995
27996
27997
27998
27999
28000
28001
28002
28003
28004
28005
28006
28007
28008
28009
28010
28011
28012
28013
28014
28015
28016
28017
28018
28019
28020
28021
28022
28023
28024
28025
28026
28027
28028
28029
28030
28031
28032
28033
28034
28035
28036
28037
28038
28039
28040
28041
28042
28043
28044
28045
28046
28047
28048
28049
28050
28051
28052
28053
28054
28055
28056
28057
28058
28059
28060
28061
28062
28063
28064
28065
28066
28067
28068
28069
28070
28071
28072
28073
28074
28075
28076
28077
28078
28079
28080
28081
28082
28083
28084
28085
28086
28087
28088
28089
28090
28091
28092
28093
28094
28095
28096
28097
28098
28099
28100
28101
28102
28103
28104
28105
28106
28107
28108
28109
28110
28111
28112
28113
28114
28115
28116
28117
28118
28119
28120
28121
28122
28123
28124
28125
28126
28127
28128
28129
28130
28131
28132
28133
28134
28135
28136
28137
28138
28139
28140
28141
28142
28143
28144
28145
28146
28147
28148
28149
28150
28151
28152
28153
28154
28155
28156
28157
28158
28159
28160
28161
28162
28163
28164
28165
28166
28167
28168
28169
28170
28171
28172
28173
28174
28175
28176
28177
28178
28179
28180
28181
28182
28183
28184
28185
28186
28187
28188
28189
28190
28191
28192
28193
28194
28195
28196
28197
28198
28199
28200
28201
28202
28203
28204
28205
28206
28207
28208
28209
28210
28211
28212
28213
28214
28215
28216
28217
28218
28219
28220
28221
28222
28223
28224
28225
28226
28227
28228
28229
28230
28231
28232
28233
28234
28235
28236
28237
28238
28239
28240
28241
28242
28243
28244
28245
28246
28247
28248
28249
28250
28251
28252
28253
28254
28255
28256
28257
28258
28259
28260
28261
28262
28263
28264
28265
28266
28267
28268
28269
28270
28271
28272
28273
28274
28275
28276
28277
28278
28279
28280
28281
28282
28283
28284
28285
28286
28287
28288
28289
28290
28291
28292
28293
28294
28295
28296
28297
28298
28299
28300
28301
28302
28303
28304
28305
28306
28307
28308
28309
28310
28311
28312
28313
28314
28315
28316
28317
28318
28319
28320
28321
28322
28323
28324
28325
28326
28327
28328
28329
28330
28331
28332
28333
28334
28335
28336
28337
28338
28339
28340
28341
28342
28343
28344
28345
28346
28347
28348
28349
28350
28351
28352
28353
28354
28355
28356
28357
28358
28359
28360
28361
28362
28363
28364
28365
28366
28367
28368
28369
28370
28371
28372
28373
28374
28375
28376
28377
28378
28379
28380
28381
28382
28383
28384
28385
28386
28387
28388
28389
28390
28391
28392
28393
28394
28395
28396
28397
28398
28399
28400
28401
28402
28403
28404
28405
28406
28407
28408
28409
28410
28411
28412
28413
28414
28415
28416
28417
28418
28419
28420
28421
28422
28423
28424
28425
28426
28427
28428
28429
28430
28431
28432
28433
28434
28435
28436
28437
28438
28439
28440
28441
28442
28443
28444
28445
28446
28447
28448
28449
28450
28451
28452
28453
28454
28455
28456
28457
28458
28459
28460
28461
28462
28463
28464
28465
28466
28467
28468
28469
28470
28471
28472
28473
28474
28475
28476
28477
28478
28479
28480
28481
28482
28483
28484
28485
28486
28487
28488
28489
28490
28491
28492
28493
28494
28495
28496
28497
28498
28499
28500
28501
28502
28503
28504
28505
28506
28507
28508
28509
28510
28511
28512
28513
28514
28515
28516
28517
28518
28519
28520
28521
28522
28523
28524
28525
28526
28527
28528
28529
28530
28531
28532
28533
28534
28535
28536
28537
28538
28539
28540
28541
28542
28543
28544
28545
28546
28547
28548
28549
28550
28551
28552
28553
28554
28555
28556
28557
28558
28559
28560
28561
28562
28563
28564
28565
28566
28567
28568
28569
28570
28571
28572
28573
28574
28575
28576
28577
28578
28579
28580
28581
28582
28583
28584
28585
28586
28587
28588
28589
28590
28591
28592
28593
28594
28595
28596
28597
28598
28599
28600
28601
28602
28603
28604
28605
28606
28607
28608
28609
28610
28611
28612
28613
28614
28615
28616
28617
28618
28619
28620
28621
28622
28623
28624
28625
28626
28627
28628
28629
28630
28631
28632
28633
28634
28635
28636
28637
28638
28639
28640
28641
28642
28643
28644
28645
28646
28647
28648
28649
28650
28651
28652
28653
28654
28655
28656
28657
28658
28659
28660
28661
28662
28663
28664
28665
28666
28667
28668
28669
28670
28671
28672
28673
28674
28675
28676
28677
28678
28679
28680
28681
28682
28683
28684
28685
28686
28687
28688
28689
28690
28691
28692
28693
28694
28695
28696
28697
28698
28699
28700
28701
28702
28703
28704
28705
28706
28707
28708
28709
28710
28711
28712
28713
28714
28715
28716
28717
28718
28719
28720
28721
28722
28723
28724
28725
28726
28727
28728
28729
28730
28731
28732
28733
28734
28735
28736
28737
28738
28739
28740
28741
28742
28743
28744
28745
28746
28747
28748
28749
28750
28751
28752
28753
28754
28755
28756
28757
28758
28759
28760
28761
28762
28763
28764
28765
28766
28767
28768
28769
28770
28771
28772
28773
28774
28775
28776
28777
28778
28779
28780
28781
28782
28783
28784
28785
28786
28787
28788
28789
28790
28791
28792
28793
28794
28795
28796
28797
28798
28799
28800
28801
28802
28803
28804
28805
28806
28807
28808
28809
28810
28811
28812
28813
28814
28815
28816
28817
28818
28819
28820
28821
28822
28823
28824
28825
28826
28827
28828
28829
28830
28831
28832
28833
28834
28835
28836
28837
28838
28839
28840
28841
28842
28843
28844
28845
28846
28847
28848
28849
28850
28851
28852
28853
28854
28855
28856
28857
28858
28859
28860
28861
28862
28863
28864
28865
28866
28867
28868
28869
28870
28871
28872
28873
28874
28875
28876
28877
28878
28879
28880
28881
28882
28883
28884
28885
28886
28887
28888
28889
28890
28891
28892
28893
28894
28895
28896
28897
28898
28899
28900
28901
28902
28903
28904
28905
28906
28907
28908
28909
28910
28911
28912
28913
28914
28915
28916
28917
28918
28919
28920
28921
28922
28923
28924
28925
28926
28927
28928
28929
28930
28931
28932
28933
28934
28935
28936
28937
28938
28939
28940
28941
28942
28943
28944
28945
28946
28947
28948
28949
28950
28951
28952
28953
28954
28955
28956
28957
28958
28959
28960
28961
28962
28963
28964
28965
28966
28967
28968
28969
28970
28971
28972
28973
28974
28975
28976
28977
28978
28979
28980
28981
28982
28983
28984
28985
28986
28987
28988
28989
28990
28991
28992
28993
28994
28995
28996
28997
28998
28999
29000
29001
29002
29003
29004
29005
29006
29007
29008
29009
29010
29011
29012
29013
29014
29015
29016
29017
29018
29019
29020
29021
29022
29023
29024
29025
29026
29027
29028
29029
29030
29031
29032
29033
29034
29035
29036
29037
29038
29039
29040
29041
29042
29043
29044
29045
29046
29047
29048
29049
29050
29051
29052
29053
29054
29055
29056
29057
29058
29059
29060
29061
29062
29063
29064
29065
29066
29067
29068
29069
29070
29071
29072
29073
29074
29075
29076
29077
29078
29079
29080
29081
29082
29083
29084
29085
29086
29087
29088
29089
29090
29091
29092
29093
29094
29095
29096
29097
29098
29099
29100
29101
29102
29103
29104
29105
29106
29107
29108
29109
29110
29111
29112
29113
29114
29115
29116
29117
29118
29119
29120
29121
29122
29123
29124
29125
29126
29127
29128
29129
29130
29131
29132
29133
29134
29135
29136
29137
29138
29139
29140
29141
29142
29143
29144
29145
29146
29147
29148
29149
29150
29151
29152
29153
29154
29155
29156
29157
29158
29159
29160
29161
29162
29163
29164
29165
29166
29167
29168
29169
29170
29171
29172
29173
29174
29175
29176
29177
29178
29179
29180
29181
29182
29183
29184
29185
29186
29187
29188
29189
29190
29191
29192
29193
29194
29195
29196
29197
29198
29199
29200
29201
29202
29203
29204
29205
29206
29207
29208
29209
29210
29211
29212
29213
29214
29215
29216
29217
29218
29219
29220
29221
29222
29223
29224
29225
29226
29227
29228
29229
29230
29231
29232
29233
29234
29235
29236
29237
29238
29239
29240
29241
29242
29243
29244
29245
29246
29247
29248
29249
29250
29251
29252
29253
29254
29255
29256
29257
29258
29259
29260
29261
29262
29263
29264
29265
29266
29267
29268
29269
29270
29271
29272
29273
29274
29275
29276
29277
29278
29279
29280
29281
29282
29283
29284
29285
29286
29287
29288
29289
29290
29291
29292
29293
29294
29295
29296
29297
29298
29299
29300
29301
29302
29303
29304
29305
29306
29307
29308
29309
29310
29311
29312
29313
29314
29315
29316
29317
29318
29319
29320
29321
29322
29323
29324
29325
29326
29327
29328
29329
29330
29331
29332
29333
29334
29335
29336
29337
29338
29339
29340
29341
29342
29343
29344
29345
29346
29347
29348
29349
29350
29351
29352
29353
29354
29355
29356
29357
29358
29359
29360
29361
29362
29363
29364
29365
29366
29367
29368
29369
29370
29371
29372
29373
29374
29375
29376
29377
29378
29379
29380
29381
29382
29383
29384
29385
29386
29387
29388
29389
29390
29391
29392
29393
29394
29395
29396
29397
29398
29399
29400
29401
29402
29403
29404
29405
29406
29407
29408
29409
29410
29411
29412
29413
29414
29415
29416
29417
29418
29419
29420
29421
29422
29423
29424
29425
29426
29427
29428
29429
29430
29431
29432
29433
29434
29435
29436
29437
29438
29439
29440
29441
29442
29443
29444
29445
29446
29447
29448
29449
29450
29451
29452
29453
29454
29455
29456
29457
29458
29459
29460
29461
29462
29463
29464
29465
29466
29467
29468
29469
29470
29471
29472
29473
29474
29475
29476
29477
29478
29479
29480
29481
29482
29483
29484
29485
29486
29487
29488
29489
29490
29491
29492
29493
29494
29495
29496
29497
29498
29499
29500
29501
29502
29503
29504
29505
29506
29507
29508
29509
29510
29511
29512
29513
29514
29515
29516
29517
29518
29519
29520
29521
29522
29523
29524
29525
29526
29527
29528
29529
29530
29531
29532
29533
29534
29535
29536
29537
29538
29539
29540
29541
29542
29543
29544
29545
29546
29547
29548
29549
29550
29551
29552
29553
29554
29555
29556
29557
29558
29559
29560
29561
29562
29563
29564
29565
29566
29567
29568
29569
29570
29571
29572
29573
29574
29575
29576
29577
29578
29579
29580
29581
29582
29583
29584
29585
29586
29587
29588
29589
29590
29591
29592
29593
29594
29595
29596
29597
29598
29599
29600
29601
29602
29603
29604
29605
29606
29607
29608
29609
29610
29611
29612
29613
29614
29615
29616
29617
29618
29619
29620
29621
29622
29623
29624
29625
29626
29627
29628
29629
29630
29631
29632
29633
29634
29635
29636
29637
29638
29639
29640
29641
29642
29643
29644
29645
29646
29647
29648
29649
29650
29651
29652
29653
29654
29655
29656
29657
29658
29659
29660
29661
29662
29663
29664
29665
29666
29667
29668
29669
29670
29671
29672
29673
29674
29675
29676
29677
29678
29679
29680
29681
29682
29683
29684
29685
29686
29687
29688
29689
29690
29691
29692
29693
29694
29695
29696
29697
29698
29699
29700
29701
29702
29703
29704
29705
29706
29707
29708
29709
29710
29711
29712
29713
29714
29715
29716
29717
29718
29719
29720
29721
29722
29723
29724
29725
29726
29727
29728
29729
29730
29731
29732
29733
29734
29735
29736
29737
29738
29739
29740
29741
29742
29743
29744
29745
29746
29747
29748
29749
29750
29751
29752
29753
29754
29755
29756
29757
29758
29759
29760
29761
29762
29763
29764
29765
29766
29767
29768
29769
29770
29771
29772
29773
29774
29775
29776
29777
29778
29779
29780
29781
29782
29783
29784
29785
29786
29787
29788
29789
29790
29791
29792
29793
29794
29795
29796
29797
29798
29799
29800
29801
29802
29803
29804
29805
29806
29807
29808
29809
29810
29811
29812
29813
29814
29815
29816
29817
29818
29819
29820
29821
29822
29823
29824
29825
29826
29827
29828
29829
29830
29831
29832
29833
29834
29835
29836
29837
29838
29839
29840
29841
29842
29843
29844
29845
29846
29847
29848
29849
29850
29851
29852
29853
29854
29855
29856
29857
29858
29859
29860
29861
29862
29863
29864
29865
29866
29867
29868
29869
29870
29871
29872
29873
29874
29875
29876
29877
29878
29879
29880
29881
29882
29883
29884
29885
29886
29887
29888
29889
29890
29891
29892
29893
29894
29895
29896
29897
29898
29899
29900
29901
29902
29903
29904
29905
29906
29907
29908
29909
29910
29911
29912
29913
29914
29915
29916
29917
29918
29919
29920
29921
29922
29923
29924
29925
29926
29927
29928
29929
29930
29931
29932
29933
29934
29935
29936
29937
29938
29939
29940
29941
29942
29943
29944
29945
29946
29947
29948
29949
29950
29951
29952
29953
29954
29955
29956
29957
29958
29959
29960
29961
29962
29963
29964
29965
29966
29967
29968
29969
29970
29971
29972
29973
29974
29975
29976
29977
29978
29979
29980
29981
29982
29983
29984
29985
29986
29987
29988
29989
29990
29991
29992
29993
29994
29995
29996
29997
29998
29999
30000
30001
30002
30003
30004
30005
30006
30007
30008
30009
30010
30011
30012
30013
30014
30015
30016
30017
30018
30019
30020
30021
30022
30023
30024
30025
30026
30027
30028
30029
30030
30031
30032
30033
30034
30035
30036
30037
30038
30039
30040
30041
30042
30043
30044
30045
30046
30047
30048
30049
30050
30051
30052
30053
30054
30055
30056
30057
30058
30059
30060
30061
30062
30063
30064
30065
30066
30067
30068
30069
30070
30071
30072
30073
30074
30075
30076
30077
30078
30079
30080
30081
30082
30083
30084
30085
30086
30087
30088
30089
30090
30091
30092
30093
30094
30095
30096
30097
30098
30099
30100
30101
30102
30103
30104
30105
30106
30107
30108
30109
30110
30111
30112
30113
30114
30115
30116
30117
30118
30119
30120
30121
30122
30123
30124
30125
30126
30127
30128
30129
30130
30131
30132
30133
30134
30135
30136
30137
30138
30139
30140
30141
30142
30143
30144
30145
30146
30147
30148
30149
30150
30151
30152
30153
30154
30155
30156
30157
30158
30159
30160
30161
30162
30163
30164
30165
30166
30167
30168
30169
30170
30171
30172
30173
30174
30175
30176
30177
30178
30179
30180
30181
30182
30183
30184
30185
30186
30187
30188
30189
30190
30191
30192
30193
30194
30195
30196
30197
30198
30199
30200
30201
30202
30203
30204
30205
30206
30207
30208
30209
30210
30211
30212
30213
30214
30215
30216
30217
30218
30219
30220
30221
30222
30223
30224
30225
30226
30227
30228
30229
30230
30231
30232
30233
30234
30235
30236
30237
30238
30239
30240
30241
30242
30243
30244
30245
30246
30247
30248
30249
30250
30251
30252
30253
30254
30255
30256
30257
30258
30259
30260
30261
30262
30263
30264
30265
30266
30267
30268
30269
30270
30271
30272
30273
30274
30275
30276
30277
30278
30279
30280
30281
30282
30283
30284
30285
30286
30287
30288
30289
30290
30291
30292
30293
30294
30295
30296
30297
30298
30299
30300
30301
30302
30303
30304
30305
30306
30307
30308
30309
30310
30311
30312
30313
30314
30315
30316
30317
30318
30319
30320
30321
30322
30323
30324
30325
30326
30327
30328
30329
30330
30331
30332
30333
30334
30335
30336
30337
30338
30339
30340
30341
30342
30343
30344
30345
30346
30347
30348
30349
30350
30351
30352
30353
30354
30355
30356
30357
30358
30359
30360
30361
30362
30363
30364
30365
30366
30367
30368
30369
30370
30371
30372
30373
30374
30375
30376
30377
30378
30379
30380
30381
30382
30383
30384
30385
30386
30387
30388
30389
30390
30391
30392
30393
30394
30395
30396
30397
30398
30399
30400
30401
30402
30403
30404
30405
30406
30407
30408
30409
30410
30411
30412
30413
30414
30415
30416
30417
30418
30419
30420
30421
30422
30423
30424
30425
30426
30427
30428
30429
30430
30431
30432
30433
30434
30435
30436
30437
30438
30439
30440
30441
30442
30443
30444
30445
30446
30447
30448
30449
30450
30451
30452
30453
30454
30455
30456
30457
30458
30459
30460
30461
30462
30463
30464
30465
30466
30467
30468
30469
30470
30471
30472
30473
30474
30475
30476
30477
30478
30479
30480
30481
30482
30483
30484
30485
30486
30487
30488
30489
30490
30491
30492
30493
30494
30495
30496
30497
30498
30499
30500
30501
30502
30503
30504
30505
30506
30507
30508
30509
30510
30511
30512
30513
30514
30515
30516
30517
30518
30519
30520
30521
30522
30523
30524
30525
30526
30527
30528
30529
30530
30531
30532
30533
30534
30535
30536
30537
30538
30539
30540
30541
30542
30543
30544
30545
30546
30547
30548
30549
30550
30551
30552
30553
30554
30555
30556
30557
30558
30559
30560
30561
30562
30563
30564
30565
30566
30567
30568
30569
30570
30571
30572
30573
30574
30575
30576
30577
30578
30579
30580
30581
30582
30583
30584
30585
30586
30587
30588
30589
30590
30591
30592
30593
30594
30595
30596
30597
30598
30599
30600
30601
30602
30603
30604
30605
30606
30607
30608
30609
30610
30611
30612
30613
30614
30615
30616
30617
30618
30619
30620
30621
30622
30623
30624
30625
30626
30627
30628
30629
30630
30631
30632
30633
30634
30635
30636
30637
30638
30639
30640
30641
30642
30643
30644
30645
30646
30647
30648
30649
30650
30651
30652
30653
30654
30655
30656
30657
30658
30659
30660
30661
30662
30663
30664
30665
30666
30667
30668
30669
30670
30671
30672
30673
30674
30675
30676
30677
30678
30679
30680
30681
30682
30683
30684
30685
30686
30687
30688
30689
30690
30691
30692
30693
30694
30695
30696
30697
30698
30699
30700
30701
30702
30703
30704
30705
30706
30707
30708
30709
30710
30711
30712
30713
30714
30715
30716
30717
30718
30719
30720
30721
30722
30723
30724
30725
30726
30727
30728
30729
30730
30731
30732
30733
30734
30735
30736
30737
30738
30739
30740
30741
30742
30743
30744
30745
30746
30747
30748
30749
30750
30751
30752
30753
30754
30755
30756
30757
30758
30759
30760
30761
30762
30763
30764
30765
30766
30767
30768
30769
30770
30771
30772
30773
30774
30775
30776
30777
30778
30779
30780
30781
30782
30783
30784
30785
30786
30787
30788
30789
30790
30791
30792
30793
30794
30795
30796
30797
30798
30799
30800
30801
30802
30803
30804
30805
30806
30807
30808
30809
30810
30811
30812
30813
30814
30815
30816
30817
30818
30819
30820
30821
30822
30823
30824
30825
30826
30827
30828
30829
30830
30831
30832
30833
30834
30835
30836
30837
30838
30839
30840
30841
30842
30843
30844
30845
30846
30847
30848
30849
30850
30851
30852
30853
30854
30855
30856
30857
30858
30859
30860
30861
30862
30863
30864
30865
30866
30867
30868
30869
30870
30871
30872
30873
30874
30875
30876
30877
30878
30879
30880
30881
30882
30883
30884
30885
30886
30887
30888
30889
30890
30891
30892
30893
30894
30895
30896
30897
30898
30899
30900
30901
30902
30903
30904
30905
30906
30907
30908
30909
30910
30911
30912
30913
30914
30915
30916
30917
30918
30919
30920
30921
30922
30923
30924
30925
30926
30927
30928
30929
30930
30931
30932
30933
30934
30935
30936
30937
30938
30939
30940
30941
30942
30943
30944
30945
30946
30947
30948
30949
30950
30951
30952
30953
30954
30955
30956
30957
30958
30959
30960
30961
30962
30963
30964
30965
30966
30967
30968
30969
30970
30971
30972
30973
30974
30975
30976
30977
30978
30979
30980
30981
30982
30983
30984
30985
30986
30987
30988
30989
30990
30991
30992
30993
30994
30995
30996
30997
30998
30999
31000
31001
31002
31003
31004
31005
31006
31007
31008
31009
31010
31011
31012
31013
31014
31015
31016
31017
31018
31019
31020
31021
31022
31023
31024
31025
31026
31027
31028
31029
31030
31031
31032
31033
31034
31035
31036
31037
31038
31039
31040
31041
31042
31043
31044
31045
31046
31047
31048
31049
31050
31051
31052
31053
31054
31055
31056
31057
31058
31059
31060
31061
31062
31063
31064
31065
31066
31067
31068
31069
31070
31071
31072
31073
31074
31075
31076
31077
31078
31079
31080
31081
31082
31083
31084
31085
31086
31087
31088
31089
31090
31091
31092
31093
31094
31095
31096
31097
31098
31099
31100
31101
31102
31103
31104
31105
31106
31107
31108
31109
31110
31111
31112
31113
31114
31115
31116
31117
31118
31119
31120
31121
31122
31123
31124
31125
31126
31127
31128
31129
31130
31131
31132
31133
31134
31135
31136
31137
31138
31139
31140
31141
31142
31143
31144
31145
31146
31147
31148
31149
31150
31151
31152
31153
31154
31155
31156
31157
31158
31159
31160
31161
31162
31163
31164
31165
31166
31167
31168
31169
31170
31171
31172
31173
31174
31175
31176
31177
31178
31179
31180
31181
31182
31183
31184
31185
31186
31187
31188
31189
31190
31191
31192
31193
31194
31195
31196
31197
31198
31199
31200
31201
31202
31203
31204
31205
31206
31207
31208
31209
31210
31211
31212
31213
31214
31215
31216
31217
31218
31219
31220
31221
31222
31223
31224
31225
31226
31227
31228
31229
31230
31231
31232
31233
31234
31235
31236
31237
31238
31239
31240
31241
31242
31243
31244
31245
31246
31247
31248
31249
31250
31251
31252
31253
31254
31255
31256
31257
31258
31259
31260
31261
31262
31263
31264
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Copyright (c) 2011 Bayard G. Bell. All rights reserved.
* Copyright (c) 2012, 2016 by Delphix. All rights reserved.
* Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
* Copyright 2019 Joyent, Inc.
* Copyright 2019 Racktop Systems
* Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
* Copyright 2022 Tintri by DDN, Inc. All rights reserved.
* Copyright 2022 Garrett D'Amore
*/
/*
* Copyright 2011 cyril.galibern@opensvc.com
*/
/*
* SCSI disk target driver.
*/
#include <sys/scsi/scsi.h>
#include <sys/dkbad.h>
#include <sys/dklabel.h>
#include <sys/dkio.h>
#include <sys/fdio.h>
#include <sys/cdio.h>
#include <sys/mhd.h>
#include <sys/vtoc.h>
#include <sys/dktp/fdisk.h>
#include <sys/kstat.h>
#include <sys/vtrace.h>
#include <sys/note.h>
#include <sys/thread.h>
#include <sys/proc.h>
#include <sys/efi_partition.h>
#include <sys/var.h>
#include <sys/aio_req.h>
#include <sys/dkioc_free_util.h>
#include <sys/taskq.h>
#include <sys/uuid.h>
#include <sys/byteorder.h>
#include <sys/sdt.h>
#include "sd_xbuf.h"
#include <sys/scsi/targets/sddef.h>
#include <sys/cmlb.h>
#include <sys/sysevent/eventdefs.h>
#include <sys/sysevent/dev.h>
#include <sys/fm/protocol.h>
/*
* Loadable module info.
*/
#define SD_MODULE_NAME "SCSI Disk Driver"
/*
* Define the interconnect type, to allow the driver to distinguish
* between parallel SCSI (sd) and fibre channel (ssd) behaviors.
*
* This is really for backward compatibility. In the future, the driver
* should actually check the "interconnect-type" property as reported by
* the HBA; however at present this property is not defined by all HBAs,
* so we will use this #define (1) to permit the driver to run in
* backward-compatibility mode; and (2) to print a notification message
* if an FC HBA does not support the "interconnect-type" property. The
* behavior of the driver will be to assume parallel SCSI behaviors unless
* the "interconnect-type" property is defined by the HBA **AND** has a
* value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
* INTERCONNECT_FABRIC, in which case the driver will assume Fibre
* Channel behaviors (as per the old ssd). (Note that the
* INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
* will result in the driver assuming parallel SCSI behaviors.)
*
* (see common/sys/scsi/impl/services.h)
*/
#define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL
/*
* The name of the driver, established from the module name in _init.
*/
static char *sd_label = NULL;
/*
* Driver name is unfortunately prefixed on some driver.conf properties.
*/
static char *sd_max_xfer_size = "sd_max_xfer_size";
static char *sd_config_list = "sd-config-list";
/*
* Driver global variables
*/
#ifdef SDDEBUG
int sd_force_pm_supported = 0;
#endif /* SDDEBUG */
void *sd_state = NULL;
int sd_io_time = SD_IO_TIME;
int sd_failfast_enable = 1;
int sd_ua_retry_count = SD_UA_RETRY_COUNT;
int sd_report_pfa = 1;
int sd_max_throttle = SD_MAX_THROTTLE;
int sd_min_throttle = SD_MIN_THROTTLE;
int sd_rot_delay = 4; /* Default 4ms Rotation delay */
int sd_qfull_throttle_enable = TRUE;
int sd_retry_on_reservation_conflict = 1;
int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
_NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
static int sd_dtype_optical_bind = -1;
/* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict";
/*
* Global data for debug logging. To enable debug printing, sd_component_mask
* and sd_level_mask should be set to the desired bit patterns as outlined in
* sddef.h.
*/
uint_t sd_component_mask = 0x0;
uint_t sd_level_mask = 0x0;
struct sd_lun *sd_debug_un = NULL;
uint_t sd_error_level = SCSI_ERR_RETRYABLE;
/* Note: these may go away in the future... */
static uint32_t sd_xbuf_active_limit = 512;
static uint32_t sd_xbuf_reserve_limit = 16;
static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
/*
* Timer value used to reset the throttle after it has been reduced
* (typically in response to TRAN_BUSY or STATUS_QFULL)
*/
static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT;
static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT;
/*
* Interval value associated with the media change scsi watch.
*/
static int sd_check_media_time = 3000000;
/*
* Wait value used for in progress operations during a DDI_SUSPEND
*/
static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE;
/*
* Global buffer and mutex for debug logging
*/
static char sd_log_buf[1024];
static kmutex_t sd_log_mutex;
/*
* Structs and globals for recording attached lun information.
* This maintains a chain. Each node in the chain represents a SCSI controller.
* The structure records the number of luns attached to each target connected
* with the controller.
* For parallel scsi device only.
*/
struct sd_scsi_hba_tgt_lun {
struct sd_scsi_hba_tgt_lun *next;
dev_info_t *pdip;
int nlun[NTARGETS_WIDE];
};
/*
* Flag to indicate the lun is attached or detached
*/
#define SD_SCSI_LUN_ATTACH 0
#define SD_SCSI_LUN_DETACH 1
static kmutex_t sd_scsi_target_lun_mutex;
static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL;
_NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
_NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
sd_scsi_target_lun_head))
/*
* "Smart" Probe Caching structs, globals, #defines, etc.
* For parallel scsi and non-self-identify device only.
*/
/*
* The following resources and routines are implemented to support
* "smart" probing, which caches the scsi_probe() results in an array,
* in order to help avoid long probe times.
*/
struct sd_scsi_probe_cache {
struct sd_scsi_probe_cache *next;
dev_info_t *pdip;
int cache[NTARGETS_WIDE];
};
static kmutex_t sd_scsi_probe_cache_mutex;
static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
/*
* Really we only need protection on the head of the linked list, but
* better safe than sorry.
*/
_NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
_NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
sd_scsi_probe_cache_head))
/*
* Power attribute table
*/
static sd_power_attr_ss sd_pwr_ss = {
{ "NAME=spindle-motor", "0=off", "1=on", NULL },
{0, 100},
{30, 0},
{20000, 0}
};
static sd_power_attr_pc sd_pwr_pc = {
{ "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
"3=active", NULL },
{0, 0, 0, 100},
{90, 90, 20, 0},
{15000, 15000, 1000, 0}
};
/*
* Power level to power condition
*/
static int sd_pl2pc[] = {
SD_TARGET_START_VALID,
SD_TARGET_STANDBY,
SD_TARGET_IDLE,
SD_TARGET_ACTIVE
};
/*
* Vendor specific data name property declarations
*/
static sd_tunables seagate_properties = {
SEAGATE_THROTTLE_VALUE,
0,
0,
0,
0,
0,
0,
0,
0
};
static sd_tunables fujitsu_properties = {
FUJITSU_THROTTLE_VALUE,
0,
0,
0,
0,
0,
0,
0,
0
};
static sd_tunables ibm_properties = {
IBM_THROTTLE_VALUE,
0,
0,
0,
0,
0,
0,
0,
0
};
static sd_tunables sve_properties = {
SVE_THROTTLE_VALUE,
0,
0,
SVE_BUSY_RETRIES,
SVE_RESET_RETRY_COUNT,
SVE_RESERVE_RELEASE_TIME,
SVE_MIN_THROTTLE_VALUE,
SVE_DISKSORT_DISABLED_FLAG,
0
};
static sd_tunables maserati_properties = {
0,
0,
0,
0,
0,
0,
0,
MASERATI_DISKSORT_DISABLED_FLAG,
MASERATI_LUN_RESET_ENABLED_FLAG
};
static sd_tunables pirus_properties = {
PIRUS_THROTTLE_VALUE,
0,
PIRUS_NRR_COUNT,
PIRUS_BUSY_RETRIES,
PIRUS_RESET_RETRY_COUNT,
0,
PIRUS_MIN_THROTTLE_VALUE,
PIRUS_DISKSORT_DISABLED_FLAG,
PIRUS_LUN_RESET_ENABLED_FLAG
};
static sd_tunables elite_properties = {
ELITE_THROTTLE_VALUE,
0,
0,
0,
0,
0,
0,
0,
0
};
static sd_tunables st31200n_properties = {
ST31200N_THROTTLE_VALUE,
0,
0,
0,
0,
0,
0,
0,
0
};
static sd_tunables lsi_properties_scsi = {
LSI_THROTTLE_VALUE,
0,
LSI_NOTREADY_RETRIES,
0,
0,
0,
0,
0,
0
};
static sd_tunables symbios_properties = {
SYMBIOS_THROTTLE_VALUE,
0,
SYMBIOS_NOTREADY_RETRIES,
0,
0,
0,
0,
0,
0
};
static sd_tunables lsi_properties = {
0,
0,
LSI_NOTREADY_RETRIES,
0,
0,
0,
0,
0,
0
};
static sd_tunables lsi_oem_properties = {
0,
0,
LSI_OEM_NOTREADY_RETRIES,
0,
0,
0,
0,
0,
0,
1
};
#if (defined(SD_PROP_TST))
#define SD_TST_CTYPE_VAL CTYPE_CDROM
#define SD_TST_THROTTLE_VAL 16
#define SD_TST_NOTREADY_VAL 12
#define SD_TST_BUSY_VAL 60
#define SD_TST_RST_RETRY_VAL 36
#define SD_TST_RSV_REL_TIME 60
static sd_tunables tst_properties = {
SD_TST_THROTTLE_VAL,
SD_TST_CTYPE_VAL,
SD_TST_NOTREADY_VAL,
SD_TST_BUSY_VAL,
SD_TST_RST_RETRY_VAL,
SD_TST_RSV_REL_TIME,
0,
0,
0
};
#endif
/* This is similar to the ANSI toupper implementation */
#define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
/*
* Static Driver Configuration Table
*
* This is the table of disks which need throttle adjustment (or, perhaps
* something else as defined by the flags at a future time.) device_id
* is a string consisting of concatenated vid (vendor), pid (product/model)
* and revision strings as defined in the scsi_inquiry structure. Offsets of
* the parts of the string are as defined by the sizes in the scsi_inquiry
* structure. Device type is searched as far as the device_id string is
* defined. Flags defines which values are to be set in the driver from the
* properties list.
*
* Entries below which begin and end with a "*" are a special case.
* These do not have a specific vendor, and the string which follows
* can appear anywhere in the 16 byte PID portion of the inquiry data.
*
* Entries below which begin and end with a " " (blank) are a special
* case. The comparison function will treat multiple consecutive blanks
* as equivalent to a single blank. For example, this causes a
* sd_disk_table entry of " NEC CDROM " to match a device's id string
* of "NEC CDROM".
*
* Note: The MD21 controller type has been obsoleted.
* ST318202F is a Legacy device
* MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
* made with an FC connection. The entries here are a legacy.
*/
static sd_disk_config_t sd_disk_table[] = {
{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
{ "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
{ "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
{ "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
{ "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
{ "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
{ "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
{ "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
{ "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
{ "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
{ "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties },
{ "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties },
{ "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties },
{ "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties },
{ "IBM 1724-100", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "IBM 1818", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "DELL MD3000", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "DELL MD3000i", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "*CSM100_*", SD_CONF_BSET_NRR_COUNT |
SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
{ "*CSM200_*", SD_CONF_BSET_NRR_COUNT |
SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
{ "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties },
{ "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties },
{ "SUN SESS01", SD_CONF_BSET_THROTTLE |
SD_CONF_BSET_BSY_RETRY_COUNT|
SD_CONF_BSET_RST_RETRIES|
SD_CONF_BSET_RSV_REL_TIME|
SD_CONF_BSET_MIN_THROTTLE|
SD_CONF_BSET_DISKSORT_DISABLED,
&sve_properties },
{ "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
SD_CONF_BSET_LUN_RESET_ENABLED,
&maserati_properties },
{ "SUN SE6920", SD_CONF_BSET_THROTTLE |
SD_CONF_BSET_NRR_COUNT|
SD_CONF_BSET_BSY_RETRY_COUNT|
SD_CONF_BSET_RST_RETRIES|
SD_CONF_BSET_MIN_THROTTLE|
SD_CONF_BSET_DISKSORT_DISABLED|
SD_CONF_BSET_LUN_RESET_ENABLED,
&pirus_properties },
{ "SUN SE6940", SD_CONF_BSET_THROTTLE |
SD_CONF_BSET_NRR_COUNT|
SD_CONF_BSET_BSY_RETRY_COUNT|
SD_CONF_BSET_RST_RETRIES|
SD_CONF_BSET_MIN_THROTTLE|
SD_CONF_BSET_DISKSORT_DISABLED|
SD_CONF_BSET_LUN_RESET_ENABLED,
&pirus_properties },
{ "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE |
SD_CONF_BSET_NRR_COUNT|
SD_CONF_BSET_BSY_RETRY_COUNT|
SD_CONF_BSET_RST_RETRIES|
SD_CONF_BSET_MIN_THROTTLE|
SD_CONF_BSET_DISKSORT_DISABLED|
SD_CONF_BSET_LUN_RESET_ENABLED,
&pirus_properties },
{ "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE |
SD_CONF_BSET_NRR_COUNT|
SD_CONF_BSET_BSY_RETRY_COUNT|
SD_CONF_BSET_RST_RETRIES|
SD_CONF_BSET_MIN_THROTTLE|
SD_CONF_BSET_DISKSORT_DISABLED|
SD_CONF_BSET_LUN_RESET_ENABLED,
&pirus_properties },
{ "SUN PSX1000", SD_CONF_BSET_THROTTLE |
SD_CONF_BSET_NRR_COUNT|
SD_CONF_BSET_BSY_RETRY_COUNT|
SD_CONF_BSET_RST_RETRIES|
SD_CONF_BSET_MIN_THROTTLE|
SD_CONF_BSET_DISKSORT_DISABLED|
SD_CONF_BSET_LUN_RESET_ENABLED,
&pirus_properties },
{ "SUN SE6330", SD_CONF_BSET_THROTTLE |
SD_CONF_BSET_NRR_COUNT|
SD_CONF_BSET_BSY_RETRY_COUNT|
SD_CONF_BSET_RST_RETRIES|
SD_CONF_BSET_MIN_THROTTLE|
SD_CONF_BSET_DISKSORT_DISABLED|
SD_CONF_BSET_LUN_RESET_ENABLED,
&pirus_properties },
{ "SUN STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "SUN SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
{ "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL },
{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
{ "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL },
{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
&symbios_properties },
{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
&lsi_properties_scsi },
{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
| SD_CONF_BSET_READSUB_BCD
| SD_CONF_BSET_READ_TOC_ADDR_BCD
| SD_CONF_BSET_NO_READ_HEADER
| SD_CONF_BSET_READ_CD_XD4), NULL },
{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
| SD_CONF_BSET_READSUB_BCD
| SD_CONF_BSET_READ_TOC_ADDR_BCD
| SD_CONF_BSET_NO_READ_HEADER
| SD_CONF_BSET_READ_CD_XD4), NULL },
#if (defined(SD_PROP_TST))
{ "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE
| SD_CONF_BSET_CTYPE
| SD_CONF_BSET_NRR_COUNT
| SD_CONF_BSET_FAB_DEVID
| SD_CONF_BSET_NOCACHE
| SD_CONF_BSET_BSY_RETRY_COUNT
| SD_CONF_BSET_PLAYMSF_BCD
| SD_CONF_BSET_READSUB_BCD
| SD_CONF_BSET_READ_TOC_TRK_BCD
| SD_CONF_BSET_READ_TOC_ADDR_BCD
| SD_CONF_BSET_NO_READ_HEADER
| SD_CONF_BSET_READ_CD_XD4
| SD_CONF_BSET_RST_RETRIES
| SD_CONF_BSET_RSV_REL_TIME
| SD_CONF_BSET_TUR_CHECK), &tst_properties},
#endif
};
static const int sd_disk_table_size =
sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
/*
* Emulation mode disk drive VID/PID table
*/
static char sd_flash_dev_table[][25] = {
"ATA MARVELL SD88SA02",
"MARVELL SD88SA02",
"TOSHIBA THNSNV05",
};
static const int sd_flash_dev_table_size =
sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
#define SD_INTERCONNECT_PARALLEL 0
#define SD_INTERCONNECT_FABRIC 1
#define SD_INTERCONNECT_FIBRE 2
#define SD_INTERCONNECT_SSA 3
#define SD_INTERCONNECT_SATA 4
#define SD_INTERCONNECT_SAS 5
#define SD_IS_PARALLEL_SCSI(un) \
((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
#define SD_IS_SERIAL(un) \
(((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
/*
* Definitions used by device id registration routines
*/
#define VPD_HEAD_OFFSET 3 /* size of head for vpd page */
#define VPD_PAGE_LENGTH 3 /* offset for pge length data */
#define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */
static kmutex_t sd_sense_mutex = {0};
/*
* Macros for updates of the driver state
*/
#define New_state(un, s) \
(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
#define Restore_state(un) \
{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
static struct sd_cdbinfo sd_cdbtab[] = {
{ CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, },
{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, },
{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, },
{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
};
/*
* Specifies the number of seconds that must have elapsed since the last
* cmd. has completed for a device to be declared idle to the PM framework.
*/
static int sd_pm_idletime = 1;
/*
* Internal function prototypes
*/
typedef struct unmap_param_hdr_s {
uint16_t uph_data_len;
uint16_t uph_descr_data_len;
uint32_t uph_reserved;
} unmap_param_hdr_t;
typedef struct unmap_blk_descr_s {
uint64_t ubd_lba;
uint32_t ubd_lba_cnt;
uint32_t ubd_reserved;
} unmap_blk_descr_t;
/* Max number of block descriptors in UNMAP command */
#define SD_UNMAP_MAX_DESCR \
((UINT16_MAX - sizeof (unmap_param_hdr_t)) / sizeof (unmap_blk_descr_t))
/* Max size of the UNMAP parameter list in bytes */
#define SD_UNMAP_PARAM_LIST_MAXSZ (sizeof (unmap_param_hdr_t) + \
SD_UNMAP_MAX_DESCR * sizeof (unmap_blk_descr_t))
int _init(void);
int _fini(void);
int _info(struct modinfo *modinfop);
/*PRINTFLIKE3*/
static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
/*PRINTFLIKE3*/
static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
/*PRINTFLIKE3*/
static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
static int sdprobe(dev_info_t *devi);
static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
void **result);
static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
int mod_flags, char *name, caddr_t valuep, int *lengthp);
/*
* Smart probe for parallel scsi
*/
static void sd_scsi_probe_cache_init(void);
static void sd_scsi_probe_cache_fini(void);
static void sd_scsi_clear_probe_cache(void);
static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
/*
* Attached luns on target for parallel scsi
*/
static void sd_scsi_target_lun_init(void);
static void sd_scsi_target_lun_fini(void);
static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
static int sd_spin_up_unit(sd_ssc_t *ssc);
/*
* Using sd_ssc_init to establish sd_ssc_t struct
* Using sd_ssc_send to send uscsi internal command
* Using sd_ssc_fini to free sd_ssc_t struct
*/
static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
int flag, enum uio_seg dataspace, int path_flag);
static void sd_ssc_fini(sd_ssc_t *ssc);
/*
* Using sd_ssc_assessment to set correct type-of-assessment
* Using sd_ssc_post to post ereport & system log
* sd_ssc_post will call sd_ssc_print to print system log
* sd_ssc_post will call sd_ssd_ereport_post to post ereport
*/
static void sd_ssc_assessment(sd_ssc_t *ssc,
enum sd_type_assessment tp_assess);
static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
static void sd_ssc_ereport_post(sd_ssc_t *ssc,
enum sd_driver_assessment drv_assess);
/*
* Using sd_ssc_set_info to mark an un-decodable-data error.
* Using sd_ssc_extract_info to transfer information from internal
* data structures to sd_ssc_t.
*/
static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
const char *fmt, ...);
static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
enum uio_seg dataspace, int path_flag);
static void sd_enable_descr_sense(sd_ssc_t *ssc);
static void sd_reenable_dsense_task(void *arg);
static void sd_set_mmc_caps(sd_ssc_t *ssc);
static void sd_read_unit_properties(struct sd_lun *un);
static int sd_process_sdconf_file(struct sd_lun *un);
static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
static void sd_set_properties(struct sd_lun *un, char *name, char *value);
static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
int *data_list, sd_tunables *values);
static void sd_process_sdconf_table(struct sd_lun *un);
static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
int list_len, char *dataname_ptr);
static void sd_set_vers1_properties(struct sd_lun *un, int flags,
sd_tunables *prop_list);
static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
int reservation_flag);
static int sd_get_devid(sd_ssc_t *ssc);
static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
static int sd_write_deviceid(sd_ssc_t *ssc);
static int sd_check_vpd_page_support(sd_ssc_t *ssc);
static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
static int sd_ddi_suspend(dev_info_t *devi);
static int sd_ddi_resume(dev_info_t *devi);
static int sd_pm_state_change(struct sd_lun *un, int level, int flag);
static int sdpower(dev_info_t *devi, int component, int level);
static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
static int sd_unit_attach(dev_info_t *devi);
static int sd_unit_detach(dev_info_t *devi);
static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
static void sd_create_errstats(struct sd_lun *un, int instance);
static void sd_set_errstats(struct sd_lun *un);
static void sd_set_pstats(struct sd_lun *un);
static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
static int sd_send_polled_RQS(struct sd_lun *un);
static int sd_ddi_scsi_poll(struct scsi_pkt *pkt);
/*
* Defines for sd_cache_control
*/
#define SD_CACHE_ENABLE 1
#define SD_CACHE_DISABLE 0
#define SD_CACHE_NOCHANGE -1
static int sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
static int sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
static void sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable);
static void sd_get_nv_sup(sd_ssc_t *ssc);
static dev_t sd_make_device(dev_info_t *devi);
static void sd_check_bdc_vpd(sd_ssc_t *ssc);
static void sd_check_emulation_mode(sd_ssc_t *ssc);
static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
uint64_t capacity);
/*
* Driver entry point functions.
*/
static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
static int sd_ready_and_valid(sd_ssc_t *ssc, int part);
static void sdmin(struct buf *bp);
static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
static int sdstrategy(struct buf *bp);
static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
/*
* Function prototypes for layering functions in the iostart chain.
*/
static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
struct buf *bp);
static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
struct buf *bp);
static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
struct buf *bp);
static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
/*
* Function prototypes for layering functions in the iodone chain.
*/
static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
struct buf *bp);
static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
struct buf *bp);
static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
struct buf *bp);
static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
/*
* Prototypes for functions to support buf(9S) based IO.
*/
static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
static void sd_destroypkt_for_buf(struct buf *);
static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
struct buf *bp, int flags,
int (*callback)(caddr_t), caddr_t callback_arg,
diskaddr_t lba, uint32_t blockcount);
static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
struct buf *bp, diskaddr_t lba, uint32_t blockcount);
/*
* Prototypes for functions to support USCSI IO.
*/
static int sd_uscsi_strategy(struct buf *bp);
static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
static void sd_destroypkt_for_uscsi(struct buf *);
static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
uchar_t chain_type, void *pktinfop);
static int sd_pm_entry(struct sd_lun *un);
static void sd_pm_exit(struct sd_lun *un);
static void sd_pm_idletimeout_handler(void *arg);
/*
* sd_core internal functions (used at the sd_core_io layer).
*/
static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
static void sdintr(struct scsi_pkt *pktp);
static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
enum uio_seg dataspace, int path_flag);
static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
daddr_t blkno, int (*func)(struct buf *));
static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
static void sd_bioclone_free(struct buf *bp);
static void sd_shadow_buf_free(struct buf *bp);
static void sd_print_transport_rejected_message(struct sd_lun *un,
struct sd_xbuf *xp, int code);
static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
void *arg, int code);
static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
void *arg, int code);
static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
void *arg, int code);
static void sd_retry_command(struct sd_lun *un, struct buf *bp,
int retry_check_flag,
void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int c),
void *user_arg, int failure_code, clock_t retry_delay,
void (*statp)(kstat_io_t *));
static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
clock_t retry_delay, void (*statp)(kstat_io_t *));
static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
struct scsi_pkt *pktp);
static void sd_start_retry_command(void *arg);
static void sd_start_direct_priority_command(void *arg);
static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
int errcode);
static void sd_return_failed_command_no_restart(struct sd_lun *un,
struct buf *bp, int errcode);
static void sd_return_command(struct sd_lun *un, struct buf *bp);
static void sd_sync_with_callback(struct sd_lun *un);
static int sdrunout(caddr_t arg);
static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
static void sd_restore_throttle(void *arg);
static void sd_init_cdb_limits(struct sd_lun *un);
static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
/*
* Error handling functions
*/
static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, size_t actual_len);
static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
void *arg, int code);
static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_sense_key_recoverable_error(struct sd_lun *un,
uint8_t *sense_datap,
struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_sense_key_not_ready(struct sd_lun *un,
uint8_t *sense_datap,
struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
uint8_t *sense_datap,
struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_sense_key_unit_attention(struct sd_lun *un,
uint8_t *sense_datap,
struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_sense_key_default(struct sd_lun *un,
uint8_t *sense_datap,
struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
void *arg, int flag);
static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp);
static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
static void sd_start_stop_unit_callback(void *arg);
static void sd_start_stop_unit_task(void *arg);
static void sd_taskq_create(void);
static void sd_taskq_delete(void);
static void sd_target_change_task(void *arg);
static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
static void sd_media_change_task(void *arg);
static int sd_handle_mchange(struct sd_lun *un);
static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
uint32_t *lbap, int path_flag);
static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
uint32_t *lbap, uint32_t *psp, int path_flag);
static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
int flag, int path_flag);
static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
uchar_t usr_cmd, uchar_t *usr_bufp);
static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
struct dk_callback *dkc);
static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
static int sd_send_scsi_UNMAP(dev_t dev, sd_ssc_t *ssc, dkioc_free_list_t *dfl,
int flag);
static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
uchar_t *bufaddr, uint_t buflen, int path_flag);
static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
size_t buflen, daddr_t start_block, int path_flag);
#define sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag) \
sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
path_flag)
#define sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
path_flag)
static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
uint16_t buflen, uchar_t page_code, uchar_t page_control,
uint16_t param_ptr, int path_flag);
static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
uchar_t *bufaddr, size_t buflen, uchar_t class_req);
static boolean_t sd_gesn_media_data_valid(uchar_t *data);
static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
static void sd_free_rqs(struct sd_lun *un);
static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
uchar_t *data, int len, int fmt);
static void sd_panic_for_res_conflict(struct sd_lun *un);
/*
* Disk Ioctl Function Prototypes
*/
static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
/*
* Multi-host Ioctl Prototypes
*/
static int sd_check_mhd(dev_t dev, int interval);
static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
static char *sd_sname(uchar_t status);
static void sd_mhd_resvd_recover(void *arg);
static void sd_resv_reclaim_thread();
static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
static int sd_reserve_release(dev_t dev, int cmd);
static void sd_rmv_resv_reclaim_req(dev_t dev);
static void sd_mhd_reset_notify_cb(caddr_t arg);
static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
mhioc_inkeys_t *usrp, int flag);
static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
mhioc_inresvs_t *usrp, int flag);
static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
static int sd_mhdioc_release(dev_t dev);
static int sd_mhdioc_register_devid(dev_t dev);
static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
/*
* SCSI removable prototypes
*/
static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
static int sr_pause_resume(dev_t dev, int mode);
static int sr_play_msf(dev_t dev, caddr_t data, int flag);
static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
static int sr_sector_mode(dev_t dev, uint32_t blksize);
static int sr_eject(dev_t dev);
static void sr_ejected(register struct sd_lun *un);
static int sr_check_wp(dev_t dev);
static opaque_t sd_watch_request_submit(struct sd_lun *un);
static int sd_check_media(dev_t dev, enum dkio_state state);
static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
static void sd_delayed_cv_broadcast(void *arg);
static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
/*
* Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
*/
static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
static int sd_wm_cache_constructor(void *wm, void *un, int flags);
static void sd_wm_cache_destructor(void *wm, void *un);
static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
daddr_t endb, ushort_t typ);
static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
daddr_t endb);
static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
static void sd_read_modify_write_task(void * arg);
static int
sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
struct buf **bpp);
/*
* Function prototypes for failfast support.
*/
static void sd_failfast_flushq(struct sd_lun *un);
static int sd_failfast_flushq_callback(struct buf *bp);
/*
* Function prototypes for partial DMA support
*/
static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
struct scsi_pkt *pkt, struct sd_xbuf *xp);
/* Function prototypes for cmlb */
static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
diskaddr_t start_block, size_t reqlength, void *tg_cookie);
static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
/*
* For printing RMW warning message timely
*/
static void sd_rmw_msg_print_handler(void *arg);
/*
* Constants for failfast support:
*
* SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
* failfast processing being performed.
*
* SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
* failfast processing on all bufs with B_FAILFAST set.
*/
#define SD_FAILFAST_INACTIVE 0
#define SD_FAILFAST_ACTIVE 1
/*
* Bitmask to control behavior of buf(9S) flushes when a transition to
* the failfast state occurs. Optional bits include:
*
* SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
* do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
* be flushed.
*
* SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
* driver, in addition to the regular wait queue. This includes the xbuf
* queues. When clear, only the driver's wait queue will be flushed.
*/
#define SD_FAILFAST_FLUSH_ALL_BUFS 0x01
#define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02
/*
* The default behavior is to only flush bufs that have B_FAILFAST set, but
* to flush all queues within the driver.
*/
static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
/*
* SD Testing Fault Injection
*/
#ifdef SD_FAULT_INJECTION
static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
static void sd_faultinjection(struct scsi_pkt *pktp);
static void sd_injection_log(char *buf, struct sd_lun *un);
#endif
/*
* Device driver ops vector
*/
static struct cb_ops sd_cb_ops = {
sdopen, /* open */
sdclose, /* close */
sdstrategy, /* strategy */
nodev, /* print */
sddump, /* dump */
sdread, /* read */
sdwrite, /* write */
sdioctl, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
nochpoll, /* poll */
sd_prop_op, /* cb_prop_op */
0, /* streamtab */
D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
CB_REV, /* cb_rev */
sdaread, /* async I/O read entry point */
sdawrite /* async I/O write entry point */
};
struct dev_ops sd_ops = {
DEVO_REV, /* devo_rev, */
0, /* refcnt */
sdinfo, /* info */
nulldev, /* identify */
sdprobe, /* probe */
sdattach, /* attach */
sddetach, /* detach */
nodev, /* reset */
&sd_cb_ops, /* driver operations */
NULL, /* bus operations */
sdpower, /* power */
ddi_quiesce_not_needed, /* quiesce */
};
/*
* This is the loadable module wrapper.
*/
#include <sys/modctl.h>
static struct modldrv modldrv = {
&mod_driverops, /* Type of module. This one is a driver */
SD_MODULE_NAME, /* Module name. */
&sd_ops /* driver ops */
};
static struct modlinkage modlinkage = {
MODREV_1, &modldrv, NULL
};
static cmlb_tg_ops_t sd_tgops = {
TG_DK_OPS_VERSION_1,
sd_tg_rdwr,
sd_tg_getinfo
};
static struct scsi_asq_key_strings sd_additional_codes[] = {
0x81, 0, "Logical Unit is Reserved",
0x85, 0, "Audio Address Not Valid",
0xb6, 0, "Media Load Mechanism Failed",
0xB9, 0, "Audio Play Operation Aborted",
0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
0x53, 2, "Medium removal prevented",
0x6f, 0, "Authentication failed during key exchange",
0x6f, 1, "Key not present",
0x6f, 2, "Key not established",
0x6f, 3, "Read without proper authentication",
0x6f, 4, "Mismatched region to this logical unit",
0x6f, 5, "Region reset count error",
0xffff, 0x0, NULL
};
/*
* Struct for passing printing information for sense data messages
*/
struct sd_sense_info {
int ssi_severity;
int ssi_pfa_flag;
};
/*
* Table of function pointers for iostart-side routines. Separate "chains"
* of layered function calls are formed by placing the function pointers
* sequentially in the desired order. Functions are called according to an
* incrementing table index ordering. The last function in each chain must
* be sd_core_iostart(). The corresponding iodone-side routines are expected
* in the sd_iodone_chain[] array.
*
* Note: It may seem more natural to organize both the iostart and iodone
* functions together, into an array of structures (or some similar
* organization) with a common index, rather than two separate arrays which
* must be maintained in synchronization. The purpose of this division is
* to achieve improved performance: individual arrays allows for more
* effective cache line utilization on certain platforms.
*/
typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
static sd_chain_t sd_iostart_chain[] = {
/* Chain for buf IO for disk drive targets (PM enabled) */
sd_mapblockaddr_iostart, /* Index: 0 */
sd_pm_iostart, /* Index: 1 */
sd_core_iostart, /* Index: 2 */
/* Chain for buf IO for disk drive targets (PM disabled) */
sd_mapblockaddr_iostart, /* Index: 3 */
sd_core_iostart, /* Index: 4 */
/*
* Chain for buf IO for removable-media or large sector size
* disk drive targets with RMW needed (PM enabled)
*/
sd_mapblockaddr_iostart, /* Index: 5 */
sd_mapblocksize_iostart, /* Index: 6 */
sd_pm_iostart, /* Index: 7 */
sd_core_iostart, /* Index: 8 */
/*
* Chain for buf IO for removable-media or large sector size
* disk drive targets with RMW needed (PM disabled)
*/
sd_mapblockaddr_iostart, /* Index: 9 */
sd_mapblocksize_iostart, /* Index: 10 */
sd_core_iostart, /* Index: 11 */
/* Chain for buf IO for disk drives with checksumming (PM enabled) */
sd_mapblockaddr_iostart, /* Index: 12 */
sd_checksum_iostart, /* Index: 13 */
sd_pm_iostart, /* Index: 14 */
sd_core_iostart, /* Index: 15 */
/* Chain for buf IO for disk drives with checksumming (PM disabled) */
sd_mapblockaddr_iostart, /* Index: 16 */
sd_checksum_iostart, /* Index: 17 */
sd_core_iostart, /* Index: 18 */
/* Chain for USCSI commands (all targets) */
sd_pm_iostart, /* Index: 19 */
sd_core_iostart, /* Index: 20 */
/* Chain for checksumming USCSI commands (all targets) */
sd_checksum_uscsi_iostart, /* Index: 21 */
sd_pm_iostart, /* Index: 22 */
sd_core_iostart, /* Index: 23 */
/* Chain for "direct" USCSI commands (all targets) */
sd_core_iostart, /* Index: 24 */
/* Chain for "direct priority" USCSI commands (all targets) */
sd_core_iostart, /* Index: 25 */
/*
* Chain for buf IO for large sector size disk drive targets
* with RMW needed with checksumming (PM enabled)
*/
sd_mapblockaddr_iostart, /* Index: 26 */
sd_mapblocksize_iostart, /* Index: 27 */
sd_checksum_iostart, /* Index: 28 */
sd_pm_iostart, /* Index: 29 */
sd_core_iostart, /* Index: 30 */
/*
* Chain for buf IO for large sector size disk drive targets
* with RMW needed with checksumming (PM disabled)
*/
sd_mapblockaddr_iostart, /* Index: 31 */
sd_mapblocksize_iostart, /* Index: 32 */
sd_checksum_iostart, /* Index: 33 */
sd_core_iostart, /* Index: 34 */
};
/*
* Macros to locate the first function of each iostart chain in the
* sd_iostart_chain[] array. These are located by the index in the array.
*/
#define SD_CHAIN_DISK_IOSTART 0
#define SD_CHAIN_DISK_IOSTART_NO_PM 3
#define SD_CHAIN_MSS_DISK_IOSTART 5
#define SD_CHAIN_RMMEDIA_IOSTART 5
#define SD_CHAIN_MSS_DISK_IOSTART_NO_PM 9
#define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9
#define SD_CHAIN_CHKSUM_IOSTART 12
#define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16
#define SD_CHAIN_USCSI_CMD_IOSTART 19
#define SD_CHAIN_USCSI_CHKSUM_IOSTART 21
#define SD_CHAIN_DIRECT_CMD_IOSTART 24
#define SD_CHAIN_PRIORITY_CMD_IOSTART 25
#define SD_CHAIN_MSS_CHKSUM_IOSTART 26
#define SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM 31
/*
* Table of function pointers for the iodone-side routines for the driver-
* internal layering mechanism. The calling sequence for iodone routines
* uses a decrementing table index, so the last routine called in a chain
* must be at the lowest array index location for that chain. The last
* routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
* or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering
* of the functions in an iodone side chain must correspond to the ordering
* of the iostart routines for that chain. Note that there is no iodone
* side routine that corresponds to sd_core_iostart(), so there is no
* entry in the table for this.
*/
static sd_chain_t sd_iodone_chain[] = {
/* Chain for buf IO for disk drive targets (PM enabled) */
sd_buf_iodone, /* Index: 0 */
sd_mapblockaddr_iodone, /* Index: 1 */
sd_pm_iodone, /* Index: 2 */
/* Chain for buf IO for disk drive targets (PM disabled) */
sd_buf_iodone, /* Index: 3 */
sd_mapblockaddr_iodone, /* Index: 4 */
/*
* Chain for buf IO for removable-media or large sector size
* disk drive targets with RMW needed (PM enabled)
*/
sd_buf_iodone, /* Index: 5 */
sd_mapblockaddr_iodone, /* Index: 6 */
sd_mapblocksize_iodone, /* Index: 7 */
sd_pm_iodone, /* Index: 8 */
/*
* Chain for buf IO for removable-media or large sector size
* disk drive targets with RMW needed (PM disabled)
*/
sd_buf_iodone, /* Index: 9 */
sd_mapblockaddr_iodone, /* Index: 10 */
sd_mapblocksize_iodone, /* Index: 11 */
/* Chain for buf IO for disk drives with checksumming (PM enabled) */
sd_buf_iodone, /* Index: 12 */
sd_mapblockaddr_iodone, /* Index: 13 */
sd_checksum_iodone, /* Index: 14 */
sd_pm_iodone, /* Index: 15 */
/* Chain for buf IO for disk drives with checksumming (PM disabled) */
sd_buf_iodone, /* Index: 16 */
sd_mapblockaddr_iodone, /* Index: 17 */
sd_checksum_iodone, /* Index: 18 */
/* Chain for USCSI commands (non-checksum targets) */
sd_uscsi_iodone, /* Index: 19 */
sd_pm_iodone, /* Index: 20 */
/* Chain for USCSI commands (checksum targets) */
sd_uscsi_iodone, /* Index: 21 */
sd_checksum_uscsi_iodone, /* Index: 22 */
sd_pm_iodone, /* Index: 22 */
/* Chain for "direct" USCSI commands (all targets) */
sd_uscsi_iodone, /* Index: 24 */
/* Chain for "direct priority" USCSI commands (all targets) */
sd_uscsi_iodone, /* Index: 25 */
/*
* Chain for buf IO for large sector size disk drive targets
* with checksumming (PM enabled)
*/
sd_buf_iodone, /* Index: 26 */
sd_mapblockaddr_iodone, /* Index: 27 */
sd_mapblocksize_iodone, /* Index: 28 */
sd_checksum_iodone, /* Index: 29 */
sd_pm_iodone, /* Index: 30 */
/*
* Chain for buf IO for large sector size disk drive targets
* with checksumming (PM disabled)
*/
sd_buf_iodone, /* Index: 31 */
sd_mapblockaddr_iodone, /* Index: 32 */
sd_mapblocksize_iodone, /* Index: 33 */
sd_checksum_iodone, /* Index: 34 */
};
/*
* Macros to locate the "first" function in the sd_iodone_chain[] array for
* each iodone-side chain. These are located by the array index, but as the
* iodone side functions are called in a decrementing-index order, the
* highest index number in each chain must be specified (as these correspond
* to the first function in the iodone chain that will be called by the core
* at IO completion time).
*/
#define SD_CHAIN_DISK_IODONE 2
#define SD_CHAIN_DISK_IODONE_NO_PM 4
#define SD_CHAIN_RMMEDIA_IODONE 8
#define SD_CHAIN_MSS_DISK_IODONE 8
#define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11
#define SD_CHAIN_MSS_DISK_IODONE_NO_PM 11
#define SD_CHAIN_CHKSUM_IODONE 15
#define SD_CHAIN_CHKSUM_IODONE_NO_PM 18
#define SD_CHAIN_USCSI_CMD_IODONE 20
#define SD_CHAIN_USCSI_CHKSUM_IODONE 22
#define SD_CHAIN_DIRECT_CMD_IODONE 24
#define SD_CHAIN_PRIORITY_CMD_IODONE 25
#define SD_CHAIN_MSS_CHKSUM_IODONE 30
#define SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM 34
/*
* Array to map a layering chain index to the appropriate initpkt routine.
* The redundant entries are present so that the index used for accessing
* the above sd_iostart_chain and sd_iodone_chain tables can be used directly
* with this table as well.
*/
typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
static sd_initpkt_t sd_initpkt_map[] = {
/* Chain for buf IO for disk drive targets (PM enabled) */
sd_initpkt_for_buf, /* Index: 0 */
sd_initpkt_for_buf, /* Index: 1 */
sd_initpkt_for_buf, /* Index: 2 */
/* Chain for buf IO for disk drive targets (PM disabled) */
sd_initpkt_for_buf, /* Index: 3 */
sd_initpkt_for_buf, /* Index: 4 */
/*
* Chain for buf IO for removable-media or large sector size
* disk drive targets (PM enabled)
*/
sd_initpkt_for_buf, /* Index: 5 */
sd_initpkt_for_buf, /* Index: 6 */
sd_initpkt_for_buf, /* Index: 7 */
sd_initpkt_for_buf, /* Index: 8 */
/*
* Chain for buf IO for removable-media or large sector size
* disk drive targets (PM disabled)
*/
sd_initpkt_for_buf, /* Index: 9 */
sd_initpkt_for_buf, /* Index: 10 */
sd_initpkt_for_buf, /* Index: 11 */
/* Chain for buf IO for disk drives with checksumming (PM enabled) */
sd_initpkt_for_buf, /* Index: 12 */
sd_initpkt_for_buf, /* Index: 13 */
sd_initpkt_for_buf, /* Index: 14 */
sd_initpkt_for_buf, /* Index: 15 */
/* Chain for buf IO for disk drives with checksumming (PM disabled) */
sd_initpkt_for_buf, /* Index: 16 */
sd_initpkt_for_buf, /* Index: 17 */
sd_initpkt_for_buf, /* Index: 18 */
/* Chain for USCSI commands (non-checksum targets) */
sd_initpkt_for_uscsi, /* Index: 19 */
sd_initpkt_for_uscsi, /* Index: 20 */
/* Chain for USCSI commands (checksum targets) */
sd_initpkt_for_uscsi, /* Index: 21 */
sd_initpkt_for_uscsi, /* Index: 22 */
sd_initpkt_for_uscsi, /* Index: 22 */
/* Chain for "direct" USCSI commands (all targets) */
sd_initpkt_for_uscsi, /* Index: 24 */
/* Chain for "direct priority" USCSI commands (all targets) */
sd_initpkt_for_uscsi, /* Index: 25 */
/*
* Chain for buf IO for large sector size disk drive targets
* with checksumming (PM enabled)
*/
sd_initpkt_for_buf, /* Index: 26 */
sd_initpkt_for_buf, /* Index: 27 */
sd_initpkt_for_buf, /* Index: 28 */
sd_initpkt_for_buf, /* Index: 29 */
sd_initpkt_for_buf, /* Index: 30 */
/*
* Chain for buf IO for large sector size disk drive targets
* with checksumming (PM disabled)
*/
sd_initpkt_for_buf, /* Index: 31 */
sd_initpkt_for_buf, /* Index: 32 */
sd_initpkt_for_buf, /* Index: 33 */
sd_initpkt_for_buf, /* Index: 34 */
};
/*
* Array to map a layering chain index to the appropriate destroypktpkt routine.
* The redundant entries are present so that the index used for accessing
* the above sd_iostart_chain and sd_iodone_chain tables can be used directly
* with this table as well.
*/
typedef void (*sd_destroypkt_t)(struct buf *);
static sd_destroypkt_t sd_destroypkt_map[] = {
/* Chain for buf IO for disk drive targets (PM enabled) */
sd_destroypkt_for_buf, /* Index: 0 */
sd_destroypkt_for_buf, /* Index: 1 */
sd_destroypkt_for_buf, /* Index: 2 */
/* Chain for buf IO for disk drive targets (PM disabled) */
sd_destroypkt_for_buf, /* Index: 3 */
sd_destroypkt_for_buf, /* Index: 4 */
/*
* Chain for buf IO for removable-media or large sector size
* disk drive targets (PM enabled)
*/
sd_destroypkt_for_buf, /* Index: 5 */
sd_destroypkt_for_buf, /* Index: 6 */
sd_destroypkt_for_buf, /* Index: 7 */
sd_destroypkt_for_buf, /* Index: 8 */
/*
* Chain for buf IO for removable-media or large sector size
* disk drive targets (PM disabled)
*/
sd_destroypkt_for_buf, /* Index: 9 */
sd_destroypkt_for_buf, /* Index: 10 */
sd_destroypkt_for_buf, /* Index: 11 */
/* Chain for buf IO for disk drives with checksumming (PM enabled) */
sd_destroypkt_for_buf, /* Index: 12 */
sd_destroypkt_for_buf, /* Index: 13 */
sd_destroypkt_for_buf, /* Index: 14 */
sd_destroypkt_for_buf, /* Index: 15 */
/* Chain for buf IO for disk drives with checksumming (PM disabled) */
sd_destroypkt_for_buf, /* Index: 16 */
sd_destroypkt_for_buf, /* Index: 17 */
sd_destroypkt_for_buf, /* Index: 18 */
/* Chain for USCSI commands (non-checksum targets) */
sd_destroypkt_for_uscsi, /* Index: 19 */
sd_destroypkt_for_uscsi, /* Index: 20 */
/* Chain for USCSI commands (checksum targets) */
sd_destroypkt_for_uscsi, /* Index: 21 */
sd_destroypkt_for_uscsi, /* Index: 22 */
sd_destroypkt_for_uscsi, /* Index: 22 */
/* Chain for "direct" USCSI commands (all targets) */
sd_destroypkt_for_uscsi, /* Index: 24 */
/* Chain for "direct priority" USCSI commands (all targets) */
sd_destroypkt_for_uscsi, /* Index: 25 */
/*
* Chain for buf IO for large sector size disk drive targets
* with checksumming (PM disabled)
*/
sd_destroypkt_for_buf, /* Index: 26 */
sd_destroypkt_for_buf, /* Index: 27 */
sd_destroypkt_for_buf, /* Index: 28 */
sd_destroypkt_for_buf, /* Index: 29 */
sd_destroypkt_for_buf, /* Index: 30 */
/*
* Chain for buf IO for large sector size disk drive targets
* with checksumming (PM enabled)
*/
sd_destroypkt_for_buf, /* Index: 31 */
sd_destroypkt_for_buf, /* Index: 32 */
sd_destroypkt_for_buf, /* Index: 33 */
sd_destroypkt_for_buf, /* Index: 34 */
};
/*
* Array to map a layering chain index to the appropriate chain "type".
* The chain type indicates a specific property/usage of the chain.
* The redundant entries are present so that the index used for accessing
* the above sd_iostart_chain and sd_iodone_chain tables can be used directly
* with this table as well.
*/
#define SD_CHAIN_NULL 0 /* for the special RQS cmd */
#define SD_CHAIN_BUFIO 1 /* regular buf IO */
#define SD_CHAIN_USCSI 2 /* regular USCSI commands */
#define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */
#define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */
/* (for error recovery) */
static int sd_chain_type_map[] = {
/* Chain for buf IO for disk drive targets (PM enabled) */
SD_CHAIN_BUFIO, /* Index: 0 */
SD_CHAIN_BUFIO, /* Index: 1 */
SD_CHAIN_BUFIO, /* Index: 2 */
/* Chain for buf IO for disk drive targets (PM disabled) */
SD_CHAIN_BUFIO, /* Index: 3 */
SD_CHAIN_BUFIO, /* Index: 4 */
/*
* Chain for buf IO for removable-media or large sector size
* disk drive targets (PM enabled)
*/
SD_CHAIN_BUFIO, /* Index: 5 */
SD_CHAIN_BUFIO, /* Index: 6 */
SD_CHAIN_BUFIO, /* Index: 7 */
SD_CHAIN_BUFIO, /* Index: 8 */
/*
* Chain for buf IO for removable-media or large sector size
* disk drive targets (PM disabled)
*/
SD_CHAIN_BUFIO, /* Index: 9 */
SD_CHAIN_BUFIO, /* Index: 10 */
SD_CHAIN_BUFIO, /* Index: 11 */
/* Chain for buf IO for disk drives with checksumming (PM enabled) */
SD_CHAIN_BUFIO, /* Index: 12 */
SD_CHAIN_BUFIO, /* Index: 13 */
SD_CHAIN_BUFIO, /* Index: 14 */
SD_CHAIN_BUFIO, /* Index: 15 */
/* Chain for buf IO for disk drives with checksumming (PM disabled) */
SD_CHAIN_BUFIO, /* Index: 16 */
SD_CHAIN_BUFIO, /* Index: 17 */
SD_CHAIN_BUFIO, /* Index: 18 */
/* Chain for USCSI commands (non-checksum targets) */
SD_CHAIN_USCSI, /* Index: 19 */
SD_CHAIN_USCSI, /* Index: 20 */
/* Chain for USCSI commands (checksum targets) */
SD_CHAIN_USCSI, /* Index: 21 */
SD_CHAIN_USCSI, /* Index: 22 */
SD_CHAIN_USCSI, /* Index: 23 */
/* Chain for "direct" USCSI commands (all targets) */
SD_CHAIN_DIRECT, /* Index: 24 */
/* Chain for "direct priority" USCSI commands (all targets) */
SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */
/*
* Chain for buf IO for large sector size disk drive targets
* with checksumming (PM enabled)
*/
SD_CHAIN_BUFIO, /* Index: 26 */
SD_CHAIN_BUFIO, /* Index: 27 */
SD_CHAIN_BUFIO, /* Index: 28 */
SD_CHAIN_BUFIO, /* Index: 29 */
SD_CHAIN_BUFIO, /* Index: 30 */
/*
* Chain for buf IO for large sector size disk drive targets
* with checksumming (PM disabled)
*/
SD_CHAIN_BUFIO, /* Index: 31 */
SD_CHAIN_BUFIO, /* Index: 32 */
SD_CHAIN_BUFIO, /* Index: 33 */
SD_CHAIN_BUFIO, /* Index: 34 */
};
/* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
#define SD_IS_BUFIO(xp) \
(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
/* Macro to return TRUE if the IO has come from the "direct priority" chain. */
#define SD_IS_DIRECT_PRIORITY(xp) \
(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
/*
* Struct, array, and macros to map a specific chain to the appropriate
* layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
*
* The sd_chain_index_map[] array is used at attach time to set the various
* un_xxx_chain type members of the sd_lun softstate to the specific layering
* chain to be used with the instance. This allows different instances to use
* different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
* and xb_chain_iodone index values in the sd_xbuf are initialized to these
* values at sd_xbuf init time, this allows (1) layering chains may be changed
* dynamically & without the use of locking; and (2) a layer may update the
* xb_chain_io[start|done] member in a given xbuf with its current index value,
* to allow for deferred processing of an IO within the same chain from a
* different execution context.
*/
struct sd_chain_index {
int sci_iostart_index;
int sci_iodone_index;
};
static struct sd_chain_index sd_chain_index_map[] = {
{ SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE },
{ SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM },
{ SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE },
{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM },
{ SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE },
{ SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM },
{ SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE },
{ SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE },
{ SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE },
{ SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE },
{ SD_CHAIN_MSS_CHKSUM_IOSTART, SD_CHAIN_MSS_CHKSUM_IODONE },
{ SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
};
/*
* The following are indexes into the sd_chain_index_map[] array.
*/
/* un->un_buf_chain_type must be set to one of these */
#define SD_CHAIN_INFO_DISK 0
#define SD_CHAIN_INFO_DISK_NO_PM 1
#define SD_CHAIN_INFO_RMMEDIA 2
#define SD_CHAIN_INFO_MSS_DISK 2
#define SD_CHAIN_INFO_RMMEDIA_NO_PM 3
#define SD_CHAIN_INFO_MSS_DSK_NO_PM 3
#define SD_CHAIN_INFO_CHKSUM 4
#define SD_CHAIN_INFO_CHKSUM_NO_PM 5
#define SD_CHAIN_INFO_MSS_DISK_CHKSUM 10
#define SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM 11
/* un->un_uscsi_chain_type must be set to one of these */
#define SD_CHAIN_INFO_USCSI_CMD 6
/* USCSI with PM disabled is the same as DIRECT */
#define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8
#define SD_CHAIN_INFO_USCSI_CHKSUM 7
/* un->un_direct_chain_type must be set to one of these */
#define SD_CHAIN_INFO_DIRECT_CMD 8
/* un->un_priority_chain_type must be set to one of these */
#define SD_CHAIN_INFO_PRIORITY_CMD 9
/* size for devid inquiries */
#define MAX_INQUIRY_SIZE 0xF0
/*
* Macros used by functions to pass a given buf(9S) struct along to the
* next function in the layering chain for further processing.
*
* In the following macros, passing more than three arguments to the called
* routines causes the optimizer for the SPARC compiler to stop doing tail
* call elimination which results in significant performance degradation.
*/
#define SD_BEGIN_IOSTART(index, un, bp) \
((*(sd_iostart_chain[index]))(index, un, bp))
#define SD_BEGIN_IODONE(index, un, bp) \
((*(sd_iodone_chain[index]))(index, un, bp))
#define SD_NEXT_IOSTART(index, un, bp) \
((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
#define SD_NEXT_IODONE(index, un, bp) \
((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
/*
* Function: _init
*
* Description: This is the driver _init(9E) entry point.
*
* Return Code: Returns the value from mod_install(9F) or
* ddi_soft_state_init(9F) as appropriate.
*
* Context: Called when driver module loaded.
*/
int
_init(void)
{
int err;
/* establish driver name from module name */
sd_label = (char *)mod_modname(&modlinkage);
err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
SD_MAXUNIT);
if (err != 0) {
return (err);
}
mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL);
mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
/*
* it's ok to init here even for fibre device
*/
sd_scsi_probe_cache_init();
sd_scsi_target_lun_init();
/*
* Creating taskq before mod_install ensures that all callers (threads)
* that enter the module after a successful mod_install encounter
* a valid taskq.
*/
sd_taskq_create();
err = mod_install(&modlinkage);
if (err != 0) {
/* delete taskq if install fails */
sd_taskq_delete();
mutex_destroy(&sd_log_mutex);
mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
cv_destroy(&sd_tr.srq_resv_reclaim_cv);
cv_destroy(&sd_tr.srq_inprocess_cv);
sd_scsi_probe_cache_fini();
sd_scsi_target_lun_fini();
ddi_soft_state_fini(&sd_state);
return (err);
}
return (err);
}
/*
* Function: _fini
*
* Description: This is the driver _fini(9E) entry point.
*
* Return Code: Returns the value from mod_remove(9F)
*
* Context: Called when driver module is unloaded.
*/
int
_fini(void)
{
int err;
if ((err = mod_remove(&modlinkage)) != 0) {
return (err);
}
sd_taskq_delete();
mutex_destroy(&sd_log_mutex);
mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
sd_scsi_probe_cache_fini();
sd_scsi_target_lun_fini();
cv_destroy(&sd_tr.srq_resv_reclaim_cv);
cv_destroy(&sd_tr.srq_inprocess_cv);
ddi_soft_state_fini(&sd_state);
return (err);
}
/*
* Function: _info
*
* Description: This is the driver _info(9E) entry point.
*
* Arguments: modinfop - pointer to the driver modinfo structure
*
* Return Code: Returns the value from mod_info(9F).
*
* Context: Kernel thread context
*/
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
/*
* The following routines implement the driver message logging facility.
* They provide component- and level- based debug output filtering.
* Output may also be restricted to messages for a single instance by
* specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
* to NULL, then messages for all instances are printed.
*
* These routines have been cloned from each other due to the language
* constraints of macros and variable argument list processing.
*/
/*
* Function: sd_log_err
*
* Description: This routine is called by the SD_ERROR macro for debug
* logging of error conditions.
*
* Arguments: comp - driver component being logged
* dev - pointer to driver info structure
* fmt - error string and format to be logged
*/
static void
sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
{
va_list ap;
dev_info_t *dev;
ASSERT(un != NULL);
dev = SD_DEVINFO(un);
ASSERT(dev != NULL);
/*
* Filter messages based on the global component and level masks.
* Also print if un matches the value of sd_debug_un, or if
* sd_debug_un is set to NULL.
*/
if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
((sd_debug_un == NULL) || (sd_debug_un == un))) {
mutex_enter(&sd_log_mutex);
va_start(ap, fmt);
(void) vsprintf(sd_log_buf, fmt, ap);
va_end(ap);
scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
mutex_exit(&sd_log_mutex);
}
#ifdef SD_FAULT_INJECTION
_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
if (un->sd_injection_mask & comp) {
mutex_enter(&sd_log_mutex);
va_start(ap, fmt);
(void) vsprintf(sd_log_buf, fmt, ap);
va_end(ap);
sd_injection_log(sd_log_buf, un);
mutex_exit(&sd_log_mutex);
}
#endif
}
/*
* Function: sd_log_info
*
* Description: This routine is called by the SD_INFO macro for debug
* logging of general purpose informational conditions.
*
* Arguments: comp - driver component being logged
* dev - pointer to driver info structure
* fmt - info string and format to be logged
*/
static void
sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
{
va_list ap;
dev_info_t *dev;
ASSERT(un != NULL);
dev = SD_DEVINFO(un);
ASSERT(dev != NULL);
/*
* Filter messages based on the global component and level masks.
* Also print if un matches the value of sd_debug_un, or if
* sd_debug_un is set to NULL.
*/
if ((sd_component_mask & component) &&
(sd_level_mask & SD_LOGMASK_INFO) &&
((sd_debug_un == NULL) || (sd_debug_un == un))) {
mutex_enter(&sd_log_mutex);
va_start(ap, fmt);
(void) vsprintf(sd_log_buf, fmt, ap);
va_end(ap);
scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
mutex_exit(&sd_log_mutex);
}
#ifdef SD_FAULT_INJECTION
_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
if (un->sd_injection_mask & component) {
mutex_enter(&sd_log_mutex);
va_start(ap, fmt);
(void) vsprintf(sd_log_buf, fmt, ap);
va_end(ap);
sd_injection_log(sd_log_buf, un);
mutex_exit(&sd_log_mutex);
}
#endif
}
/*
* Function: sd_log_trace
*
* Description: This routine is called by the SD_TRACE macro for debug
* logging of trace conditions (i.e. function entry/exit).
*
* Arguments: comp - driver component being logged
* dev - pointer to driver info structure
* fmt - trace string and format to be logged
*/
static void
sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
{
va_list ap;
dev_info_t *dev;
ASSERT(un != NULL);
dev = SD_DEVINFO(un);
ASSERT(dev != NULL);
/*
* Filter messages based on the global component and level masks.
* Also print if un matches the value of sd_debug_un, or if
* sd_debug_un is set to NULL.
*/
if ((sd_component_mask & component) &&
(sd_level_mask & SD_LOGMASK_TRACE) &&
((sd_debug_un == NULL) || (sd_debug_un == un))) {
mutex_enter(&sd_log_mutex);
va_start(ap, fmt);
(void) vsprintf(sd_log_buf, fmt, ap);
va_end(ap);
scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
mutex_exit(&sd_log_mutex);
}
#ifdef SD_FAULT_INJECTION
_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
if (un->sd_injection_mask & component) {
mutex_enter(&sd_log_mutex);
va_start(ap, fmt);
(void) vsprintf(sd_log_buf, fmt, ap);
va_end(ap);
sd_injection_log(sd_log_buf, un);
mutex_exit(&sd_log_mutex);
}
#endif
}
/*
* Function: sdprobe
*
* Description: This is the driver probe(9e) entry point function.
*
* Arguments: devi - opaque device info handle
*
* Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
* DDI_PROBE_FAILURE: If the probe failed.
* DDI_PROBE_PARTIAL: If the instance is not present now,
* but may be present in the future.
*/
static int
sdprobe(dev_info_t *devi)
{
struct scsi_device *devp;
int rval;
int instance = ddi_get_instance(devi);
if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
return (DDI_PROBE_DONTCARE);
}
devp = ddi_get_driver_private(devi);
if (devp == NULL) {
/* Ooops... nexus driver is mis-configured... */
return (DDI_PROBE_FAILURE);
}
if (ddi_get_soft_state(sd_state, instance) != NULL) {
return (DDI_PROBE_PARTIAL);
}
/*
* Call the SCSA utility probe routine to see if we actually
* have a target at this SCSI nexus.
*/
switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
case SCSIPROBE_EXISTS:
switch (devp->sd_inq->inq_dtype) {
case DTYPE_DIRECT:
rval = DDI_PROBE_SUCCESS;
break;
case DTYPE_RODIRECT:
/* CDs etc. Can be removable media */
rval = DDI_PROBE_SUCCESS;
break;
case DTYPE_OPTICAL:
/*
* Rewritable optical driver HP115AA
* Can also be removable media
*/
/*
* Do not attempt to bind to DTYPE_OPTICAL if
* pre solaris 9 sparc sd behavior is required
*
* If first time through and sd_dtype_optical_bind
* has not been set in /etc/system check properties
*/
if (sd_dtype_optical_bind < 0) {
sd_dtype_optical_bind = ddi_prop_get_int
(DDI_DEV_T_ANY, devi, 0,
"optical-device-bind", 1);
}
if (sd_dtype_optical_bind == 0) {
rval = DDI_PROBE_FAILURE;
} else {
rval = DDI_PROBE_SUCCESS;
}
break;
case DTYPE_NOTPRESENT:
default:
rval = DDI_PROBE_FAILURE;
break;
}
break;
default:
rval = DDI_PROBE_PARTIAL;
break;
}
/*
* This routine checks for resource allocation prior to freeing,
* so it will take care of the "smart probing" case where a
* scsi_probe() may or may not have been issued and will *not*
* free previously-freed resources.
*/
scsi_unprobe(devp);
return (rval);
}
/*
* Function: sdinfo
*
* Description: This is the driver getinfo(9e) entry point function.
* Given the device number, return the devinfo pointer from
* the scsi_device structure or the instance number
* associated with the dev_t.
*
* Arguments: dip - pointer to device info structure
* infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
* DDI_INFO_DEVT2INSTANCE)
* arg - driver dev_t
* resultp - user buffer for request response
*
* Return Code: DDI_SUCCESS
* DDI_FAILURE
*/
/* ARGSUSED */
static int
sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
struct sd_lun *un;
dev_t dev;
int instance;
int error;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
dev = (dev_t)arg;
instance = SDUNIT(dev);
if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
return (DDI_FAILURE);
}
*result = (void *) SD_DEVINFO(un);
error = DDI_SUCCESS;
break;
case DDI_INFO_DEVT2INSTANCE:
dev = (dev_t)arg;
instance = SDUNIT(dev);
*result = (void *)(uintptr_t)instance;
error = DDI_SUCCESS;
break;
default:
error = DDI_FAILURE;
}
return (error);
}
/*
* Function: sd_prop_op
*
* Description: This is the driver prop_op(9e) entry point function.
* Return the number of blocks for the partition in question
* or forward the request to the property facilities.
*
* Arguments: dev - device number
* dip - pointer to device info structure
* prop_op - property operator
* mod_flags - DDI_PROP_DONTPASS, don't pass to parent
* name - pointer to property name
* valuep - pointer or address of the user buffer
* lengthp - property length
*
* Return Code: DDI_PROP_SUCCESS
* DDI_PROP_NOT_FOUND
* DDI_PROP_UNDEFINED
* DDI_PROP_NO_MEMORY
* DDI_PROP_BUF_TOO_SMALL
*/
static int
sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
char *name, caddr_t valuep, int *lengthp)
{
struct sd_lun *un;
if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
return (ddi_prop_op(dev, dip, prop_op, mod_flags,
name, valuep, lengthp));
return (cmlb_prop_op(un->un_cmlbhandle,
dev, dip, prop_op, mod_flags, name, valuep, lengthp,
SDPART(dev), (void *)SD_PATH_DIRECT));
}
/*
* The following functions are for smart probing:
* sd_scsi_probe_cache_init()
* sd_scsi_probe_cache_fini()
* sd_scsi_clear_probe_cache()
* sd_scsi_probe_with_cache()
*/
/*
* Function: sd_scsi_probe_cache_init
*
* Description: Initializes the probe response cache mutex and head pointer.
*
* Context: Kernel thread context
*/
static void
sd_scsi_probe_cache_init(void)
{
mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
sd_scsi_probe_cache_head = NULL;
}
/*
* Function: sd_scsi_probe_cache_fini
*
* Description: Frees all resources associated with the probe response cache.
*
* Context: Kernel thread context
*/
static void
sd_scsi_probe_cache_fini(void)
{
struct sd_scsi_probe_cache *cp;
struct sd_scsi_probe_cache *ncp;
/* Clean up our smart probing linked list */
for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
ncp = cp->next;
kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
}
sd_scsi_probe_cache_head = NULL;
mutex_destroy(&sd_scsi_probe_cache_mutex);
}
/*
* Function: sd_scsi_clear_probe_cache
*
* Description: This routine clears the probe response cache. This is
* done when open() returns ENXIO so that when deferred
* attach is attempted (possibly after a device has been
* turned on) we will retry the probe. Since we don't know
* which target we failed to open, we just clear the
* entire cache.
*
* Context: Kernel thread context
*/
static void
sd_scsi_clear_probe_cache(void)
{
struct sd_scsi_probe_cache *cp;
int i;
mutex_enter(&sd_scsi_probe_cache_mutex);
for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
/*
* Reset all entries to SCSIPROBE_EXISTS. This will
* force probing to be performed the next time
* sd_scsi_probe_with_cache is called.
*/
for (i = 0; i < NTARGETS_WIDE; i++) {
cp->cache[i] = SCSIPROBE_EXISTS;
}
}
mutex_exit(&sd_scsi_probe_cache_mutex);
}
/*
* Function: sd_scsi_probe_with_cache
*
* Description: This routine implements support for a scsi device probe
* with cache. The driver maintains a cache of the target
* responses to scsi probes. If we get no response from a
* target during a probe inquiry, we remember that, and we
* avoid additional calls to scsi_probe on non-zero LUNs
* on the same target until the cache is cleared. By doing
* so we avoid the 1/4 sec selection timeout for nonzero
* LUNs. lun0 of a target is always probed.
*
* Arguments: devp - Pointer to a scsi_device(9S) structure
* waitfunc - indicates what the allocator routines should
* do when resources are not available. This value
* is passed on to scsi_probe() when that routine
* is called.
*
* Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
* otherwise the value returned by scsi_probe(9F).
*
* Context: Kernel thread context
*/
static int
sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
{
struct sd_scsi_probe_cache *cp;
dev_info_t *pdip = ddi_get_parent(devp->sd_dev);
int lun, tgt;
lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
SCSI_ADDR_PROP_LUN, 0);
tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
SCSI_ADDR_PROP_TARGET, -1);
/* Make sure caching enabled and target in range */
if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
/* do it the old way (no cache) */
return (scsi_probe(devp, waitfn));
}
mutex_enter(&sd_scsi_probe_cache_mutex);
/* Find the cache for this scsi bus instance */
for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
if (cp->pdip == pdip) {
break;
}
}
/* If we can't find a cache for this pdip, create one */
if (cp == NULL) {
int i;
cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
KM_SLEEP);
cp->pdip = pdip;
cp->next = sd_scsi_probe_cache_head;
sd_scsi_probe_cache_head = cp;
for (i = 0; i < NTARGETS_WIDE; i++) {
cp->cache[i] = SCSIPROBE_EXISTS;
}
}
mutex_exit(&sd_scsi_probe_cache_mutex);
/* Recompute the cache for this target if LUN zero */
if (lun == 0) {
cp->cache[tgt] = SCSIPROBE_EXISTS;
}
/* Don't probe if cache remembers a NORESP from a previous LUN. */
if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
return (SCSIPROBE_NORESP);
}
/* Do the actual probe; save & return the result */
return (cp->cache[tgt] = scsi_probe(devp, waitfn));
}
/*
* Function: sd_scsi_target_lun_init
*
* Description: Initializes the attached lun chain mutex and head pointer.
*
* Context: Kernel thread context
*/
static void
sd_scsi_target_lun_init(void)
{
mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
sd_scsi_target_lun_head = NULL;
}
/*
* Function: sd_scsi_target_lun_fini
*
* Description: Frees all resources associated with the attached lun
* chain
*
* Context: Kernel thread context
*/
static void
sd_scsi_target_lun_fini(void)
{
struct sd_scsi_hba_tgt_lun *cp;
struct sd_scsi_hba_tgt_lun *ncp;
for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
ncp = cp->next;
kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
}
sd_scsi_target_lun_head = NULL;
mutex_destroy(&sd_scsi_target_lun_mutex);
}
/*
* Function: sd_scsi_get_target_lun_count
*
* Description: This routine will check in the attached lun chain to see
* how many luns are attached on the required SCSI controller
* and target. Currently, some capabilities like tagged queue
* are supported per target based by HBA. So all luns in a
* target have the same capabilities. Based on this assumption,
* sd should only set these capabilities once per target. This
* function is called when sd needs to decide how many luns
* already attached on a target.
*
* Arguments: dip - Pointer to the system's dev_info_t for the SCSI
* controller device.
* target - The target ID on the controller's SCSI bus.
*
* Return Code: The number of luns attached on the required target and
* controller.
* -1 if target ID is not in parallel SCSI scope or the given
* dip is not in the chain.
*
* Context: Kernel thread context
*/
static int
sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
{
struct sd_scsi_hba_tgt_lun *cp;
if ((target < 0) || (target >= NTARGETS_WIDE)) {
return (-1);
}
mutex_enter(&sd_scsi_target_lun_mutex);
for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
if (cp->pdip == dip) {
break;
}
}
mutex_exit(&sd_scsi_target_lun_mutex);
if (cp == NULL) {
return (-1);
}
return (cp->nlun[target]);
}
/*
* Function: sd_scsi_update_lun_on_target
*
* Description: This routine is used to update the attached lun chain when a
* lun is attached or detached on a target.
*
* Arguments: dip - Pointer to the system's dev_info_t for the SCSI
* controller device.
* target - The target ID on the controller's SCSI bus.
* flag - Indicate the lun is attached or detached.
*
* Context: Kernel thread context
*/
static void
sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
{
struct sd_scsi_hba_tgt_lun *cp;
mutex_enter(&sd_scsi_target_lun_mutex);
for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
if (cp->pdip == dip) {
break;
}
}
if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
KM_SLEEP);
cp->pdip = dip;
cp->next = sd_scsi_target_lun_head;
sd_scsi_target_lun_head = cp;
}
mutex_exit(&sd_scsi_target_lun_mutex);
if (cp != NULL) {
if (flag == SD_SCSI_LUN_ATTACH) {
cp->nlun[target] ++;
} else {
cp->nlun[target] --;
}
}
}
/*
* Function: sd_spin_up_unit
*
* Description: Issues the following commands to spin-up the device:
* START STOP UNIT, and INQUIRY.
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
*
* Return Code: 0 - success
* EIO - failure
* EACCES - reservation conflict
*
* Context: Kernel thread context
*/
static int
sd_spin_up_unit(sd_ssc_t *ssc)
{
size_t resid = 0;
int has_conflict = FALSE;
uchar_t *bufaddr;
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
/*
* Send a throwaway START UNIT command.
*
* If we fail on this, we don't care presently what precisely
* is wrong. EMC's arrays will also fail this with a check
* condition (0x2/0x4/0x3) if the device is "inactive," but
* we don't want to fail the attach because it may become
* "active" later.
* We don't know if power condition is supported or not at
* this stage, use START STOP bit.
*/
status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
SD_TARGET_START, SD_PATH_DIRECT);
if (status != 0) {
if (status == EACCES)
has_conflict = TRUE;
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
/*
* Send another INQUIRY command to the target. This is necessary for
* non-removable media direct access devices because their INQUIRY data
* may not be fully qualified until they are spun up (perhaps via the
* START command above). Note: This seems to be needed for some
* legacy devices only.) The INQUIRY command should succeed even if a
* Reservation Conflict is present.
*/
bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
!= 0) {
kmem_free(bufaddr, SUN_INQSIZE);
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
return (EIO);
}
/*
* If we got enough INQUIRY data, copy it over the old INQUIRY data.
* Note that this routine does not return a failure here even if the
* INQUIRY command did not return any data. This is a legacy behavior.
*/
if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
}
kmem_free(bufaddr, SUN_INQSIZE);
/* If we hit a reservation conflict above, tell the caller. */
if (has_conflict == TRUE) {
return (EACCES);
}
return (0);
}
/*
* Function: sd_enable_descr_sense
*
* Description: This routine attempts to select descriptor sense format
* using the Control mode page. Devices that support 64 bit
* LBAs (for >2TB luns) should also implement descriptor
* sense data so we will call this function whenever we see
* a lun larger than 2TB. If for some reason the device
* supports 64 bit LBAs but doesn't support descriptor sense
* presumably the mode select will fail. Everything will
* continue to work normally except that we will not get
* complete sense data for commands that fail with an LBA
* larger than 32 bits.
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
*
* Context: Kernel thread context only
*/
static void
sd_enable_descr_sense(sd_ssc_t *ssc)
{
uchar_t *header;
struct mode_control_scsi3 *ctrl_bufp;
size_t buflen;
size_t bd_len;
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
/*
* Read MODE SENSE page 0xA, Control Mode Page
*/
buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
sizeof (struct mode_control_scsi3);
header = kmem_zalloc(buflen, KM_SLEEP);
status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
if (status != 0) {
SD_ERROR(SD_LOG_COMMON, un,
"sd_enable_descr_sense: mode sense ctrl page failed\n");
goto eds_exit;
}
/*
* Determine size of Block Descriptors in order to locate
* the mode page data. ATAPI devices return 0, SCSI devices
* should return MODE_BLK_DESC_LENGTH.
*/
bd_len = ((struct mode_header *)header)->bdesc_length;
/* Clear the mode data length field for MODE SELECT */
((struct mode_header *)header)->length = 0;
ctrl_bufp = (struct mode_control_scsi3 *)
(header + MODE_HEADER_LENGTH + bd_len);
/*
* If the page length is smaller than the expected value,
* the target device doesn't support D_SENSE. Bail out here.
*/
if (ctrl_bufp->mode_page.length <
sizeof (struct mode_control_scsi3) - 2) {
SD_ERROR(SD_LOG_COMMON, un,
"sd_enable_descr_sense: enable D_SENSE failed\n");
goto eds_exit;
}
/*
* Clear PS bit for MODE SELECT
*/
ctrl_bufp->mode_page.ps = 0;
/*
* Set D_SENSE to enable descriptor sense format.
*/
ctrl_bufp->d_sense = 1;
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
/*
* Use MODE SELECT to commit the change to the D_SENSE bit
*/
status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
if (status != 0) {
SD_INFO(SD_LOG_COMMON, un,
"sd_enable_descr_sense: mode select ctrl page failed\n");
} else {
kmem_free(header, buflen);
return;
}
eds_exit:
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
kmem_free(header, buflen);
}
/*
* Function: sd_reenable_dsense_task
*
* Description: Re-enable descriptor sense after device or bus reset
*
* Context: Executes in a taskq() thread context
*/
static void
sd_reenable_dsense_task(void *arg)
{
struct sd_lun *un = arg;
sd_ssc_t *ssc;
ASSERT(un != NULL);
ssc = sd_ssc_init(un);
sd_enable_descr_sense(ssc);
sd_ssc_fini(ssc);
}
/*
* Function: sd_set_mmc_caps
*
* Description: This routine determines if the device is MMC compliant and if
* the device supports CDDA via a mode sense of the CDVD
* capabilities mode page. Also checks if the device is a
* dvdram writable device.
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
*
* Context: Kernel thread context only
*/
static void
sd_set_mmc_caps(sd_ssc_t *ssc)
{
struct mode_header_grp2 *sense_mhp;
uchar_t *sense_page;
caddr_t buf;
int bd_len;
int status;
struct uscsi_cmd com;
int rtn;
uchar_t *out_data_rw, *out_data_hd;
uchar_t *rqbuf_rw, *rqbuf_hd;
uchar_t *out_data_gesn;
int gesn_len;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
/*
* The flags which will be set in this function are - mmc compliant,
* dvdram writable device, cdda support. Initialize them to FALSE
* and if a capability is detected - it will be set to TRUE.
*/
un->un_f_mmc_cap = FALSE;
un->un_f_dvdram_writable_device = FALSE;
un->un_f_cfg_cdda = FALSE;
buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
if (status != 0) {
/* command failed; just return */
kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
return;
}
/*
* If the mode sense request for the CDROM CAPABILITIES
* page (0x2A) succeeds the device is assumed to be MMC.
*/
un->un_f_mmc_cap = TRUE;
/* See if GET STATUS EVENT NOTIFICATION is supported */
if (un->un_f_mmc_gesn_polling) {
gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
un->un_f_mmc_gesn_polling = FALSE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_mmc_caps: gesn not supported "
"%d %x %x %x %x\n", rtn,
out_data_gesn[0], out_data_gesn[1],
out_data_gesn[2], out_data_gesn[3]);
}
kmem_free(out_data_gesn, gesn_len);
}
/* Get to the page data */
sense_mhp = (struct mode_header_grp2 *)buf;
bd_len = (sense_mhp->bdesc_length_hi << 8) |
sense_mhp->bdesc_length_lo;
if (bd_len > MODE_BLK_DESC_LENGTH) {
/*
* We did not get back the expected block descriptor
* length so we cannot determine if the device supports
* CDDA. However, we still indicate the device is MMC
* according to the successful response to the page
* 0x2A mode sense request.
*/
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sd_set_mmc_caps: Mode Sense returned "
"invalid block descriptor length\n");
kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
return;
}
/* See if read CDDA is supported */
sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
bd_len);
un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
/* See if writing DVD RAM is supported. */
un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
if (un->un_f_dvdram_writable_device == TRUE) {
kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
return;
}
/*
* If the device presents DVD or CD capabilities in the mode
* page, we can return here since a RRD will not have
* these capabilities.
*/
if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
return;
}
kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
/*
* If un->un_f_dvdram_writable_device is still FALSE,
* check for a Removable Rigid Disk (RRD). A RRD
* device is identified by the features RANDOM_WRITABLE and
* HARDWARE_DEFECT_MANAGEMENT.
*/
out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
RANDOM_WRITABLE, SD_PATH_STANDARD);
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
if (rtn != 0) {
kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
kmem_free(rqbuf_rw, SENSE_LENGTH);
return;
}
out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
if (rtn == 0) {
/*
* We have good information, check for random writable
* and hardware defect features.
*/
if ((out_data_rw[9] & RANDOM_WRITABLE) &&
(out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
un->un_f_dvdram_writable_device = TRUE;
}
}
kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
kmem_free(rqbuf_rw, SENSE_LENGTH);
kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
kmem_free(rqbuf_hd, SENSE_LENGTH);
}
/*
* Function: sd_check_for_writable_cd
*
* Description: This routine determines if the media in the device is
* writable or not. It uses the get configuration command (0x46)
* to determine if the media is writable
*
* Arguments: un - driver soft state (unit) structure
* path_flag - SD_PATH_DIRECT to use the USCSI "direct"
* chain and the normal command waitq, or
* SD_PATH_DIRECT_PRIORITY to use the USCSI
* "direct" chain and bypass the normal command
* waitq.
*
* Context: Never called at interrupt context.
*/
static void
sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
{
struct uscsi_cmd com;
uchar_t *out_data;
uchar_t *rqbuf;
int rtn;
uchar_t *out_data_rw, *out_data_hd;
uchar_t *rqbuf_rw, *rqbuf_hd;
struct mode_header_grp2 *sense_mhp;
uchar_t *sense_page;
caddr_t buf;
int bd_len;
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
/*
* Initialize the writable media to false, if configuration info.
* tells us otherwise then only we will set it.
*/
un->un_f_mmc_writable_media = FALSE;
mutex_exit(SD_MUTEX(un));
out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
out_data, SD_PROFILE_HEADER_LEN, path_flag);
if (rtn != 0)
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
mutex_enter(SD_MUTEX(un));
if (rtn == 0) {
/*
* We have good information, check for writable DVD.
*/
if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
un->un_f_mmc_writable_media = TRUE;
kmem_free(out_data, SD_PROFILE_HEADER_LEN);
kmem_free(rqbuf, SENSE_LENGTH);
return;
}
}
kmem_free(out_data, SD_PROFILE_HEADER_LEN);
kmem_free(rqbuf, SENSE_LENGTH);
/*
* Determine if this is a RRD type device.
*/
mutex_exit(SD_MUTEX(un));
buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
mutex_enter(SD_MUTEX(un));
if (status != 0) {
/* command failed; just return */
kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
return;
}
/* Get to the page data */
sense_mhp = (struct mode_header_grp2 *)buf;
bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
if (bd_len > MODE_BLK_DESC_LENGTH) {
/*
* We did not get back the expected block descriptor length so
* we cannot check the mode page.
*/
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sd_check_for_writable_cd: Mode Sense returned "
"invalid block descriptor length\n");
kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
return;
}
/*
* If the device presents DVD or CD capabilities in the mode
* page, we can return here since a RRD device will not have
* these capabilities.
*/
sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
return;
}
kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
/*
* If un->un_f_mmc_writable_media is still FALSE,
* check for RRD type media. A RRD device is identified
* by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
*/
mutex_exit(SD_MUTEX(un));
out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
RANDOM_WRITABLE, path_flag);
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
if (rtn != 0) {
kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
kmem_free(rqbuf_rw, SENSE_LENGTH);
mutex_enter(SD_MUTEX(un));
return;
}
out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
HARDWARE_DEFECT_MANAGEMENT, path_flag);
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
mutex_enter(SD_MUTEX(un));
if (rtn == 0) {
/*
* We have good information, check for random writable
* and hardware defect features as current.
*/
if ((out_data_rw[9] & RANDOM_WRITABLE) &&
(out_data_rw[10] & 0x1) &&
(out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
(out_data_hd[10] & 0x1)) {
un->un_f_mmc_writable_media = TRUE;
}
}
kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
kmem_free(rqbuf_rw, SENSE_LENGTH);
kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
kmem_free(rqbuf_hd, SENSE_LENGTH);
}
/*
* Function: sd_read_unit_properties
*
* Description: The following implements a property lookup mechanism.
* Properties for particular disks (keyed on vendor, model
* and rev numbers) are sought in the sd.conf file via
* sd_process_sdconf_file(), and if not found there, are
* looked for in a list hardcoded in this driver via
* sd_process_sdconf_table() Once located the properties
* are used to update the driver unit structure.
*
* Arguments: un - driver soft state (unit) structure
*/
static void
sd_read_unit_properties(struct sd_lun *un)
{
/*
* sd_process_sdconf_file returns SD_FAILURE if it cannot find
* the "sd-config-list" property (from the sd.conf file) or if
* there was not a match for the inquiry vid/pid. If this event
* occurs the static driver configuration table is searched for
* a match.
*/
ASSERT(un != NULL);
if (sd_process_sdconf_file(un) == SD_FAILURE) {
sd_process_sdconf_table(un);
}
}
/*
* Function: sd_process_sdconf_file
*
* Description: Use ddi_prop_lookup(9F) to obtain the properties from the
* driver's config file (ie, sd.conf) and update the driver
* soft state structure accordingly.
*
* Arguments: un - driver soft state (unit) structure
*
* Return Code: SD_SUCCESS - The properties were successfully set according
* to the driver configuration file.
* SD_FAILURE - The driver config list was not obtained or
* there was no vid/pid match. This indicates that
* the static config table should be used.
*
* The config file has a property, "sd-config-list". Currently we support
* two kinds of formats. For both formats, the value of this property
* is a list of duplets:
*
* sd-config-list=
* <duplet>,
* [,<duplet>]*;
*
* For the improved format, where
*
* <duplet>:= "<vid+pid>","<tunable-list>"
*
* and
*
* <tunable-list>:= <tunable> [, <tunable> ]*;
* <tunable> = <name> : <value>
*
* The <vid+pid> is the string that is returned by the target device on a
* SCSI inquiry command, the <tunable-list> contains one or more tunables
* to apply to all target devices with the specified <vid+pid>.
*
* Each <tunable> is a "<name> : <value>" pair.
*
* For the old format, the structure of each duplet is as follows:
*
* <duplet>:= "<vid+pid>","<data-property-name_list>"
*
* The first entry of the duplet is the device ID string (the concatenated
* vid & pid; not to be confused with a device_id). This is defined in
* the same way as in the sd_disk_table.
*
* The second part of the duplet is a string that identifies a
* data-property-name-list. The data-property-name-list is defined as
* follows:
*
* <data-property-name-list>:=<data-property-name> [<data-property-name>]
*
* The syntax of <data-property-name> depends on the <version> field.
*
* If version = SD_CONF_VERSION_1 we have the following syntax:
*
* <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
*
* where the prop0 value will be used to set prop0 if bit0 set in the
* flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
*
*/
static int
sd_process_sdconf_file(struct sd_lun *un)
{
char **config_list = NULL;
uint_t nelements;
char *vidptr;
int vidlen;
char *dnlist_ptr;
char *dataname_ptr;
char *dataname_lasts;
int *data_list = NULL;
uint_t data_list_len;
int rval = SD_FAILURE;
int i;
ASSERT(un != NULL);
/* Obtain the configuration list associated with the .conf file */
if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
&config_list, &nelements) != DDI_PROP_SUCCESS) {
return (SD_FAILURE);
}
/*
* Compare vids in each duplet to the inquiry vid - if a match is
* made, get the data value and update the soft state structure
* accordingly.
*
* Each duplet should show as a pair of strings, return SD_FAILURE
* otherwise.
*/
if (nelements & 1) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sd-config-list should show as pairs of strings.\n");
if (config_list)
ddi_prop_free(config_list);
return (SD_FAILURE);
}
for (i = 0; i < nelements; i += 2) {
/*
* Note: The assumption here is that each vid entry is on
* a unique line from its associated duplet.
*/
vidptr = config_list[i];
vidlen = (int)strlen(vidptr);
if (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS) {
continue;
}
/*
* dnlist contains 1 or more blank separated
* data-property-name entries
*/
dnlist_ptr = config_list[i + 1];
if (strchr(dnlist_ptr, ':') != NULL) {
/*
* Decode the improved format sd-config-list.
*/
sd_nvpair_str_decode(un, dnlist_ptr);
} else {
/*
* The old format sd-config-list, loop through all
* data-property-name entries in the
* data-property-name-list
* setting the properties for each.
*/
for (dataname_ptr = strtok_r(dnlist_ptr, " \t",
&dataname_lasts); dataname_ptr != NULL;
dataname_ptr = strtok_r(NULL, " \t",
&dataname_lasts)) {
int version;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_process_sdconf_file: disk:%s, "
"data:%s\n", vidptr, dataname_ptr);
/* Get the data list */
if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
SD_DEVINFO(un), 0, dataname_ptr, &data_list,
&data_list_len) != DDI_PROP_SUCCESS) {
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_process_sdconf_file: data "
"property (%s) has no value\n",
dataname_ptr);
continue;
}
version = data_list[0];
if (version == SD_CONF_VERSION_1) {
sd_tunables values;
/* Set the properties */
if (sd_chk_vers1_data(un, data_list[1],
&data_list[2], data_list_len,
dataname_ptr) == SD_SUCCESS) {
sd_get_tunables_from_conf(un,
data_list[1], &data_list[2],
&values);
sd_set_vers1_properties(un,
data_list[1], &values);
rval = SD_SUCCESS;
} else {
rval = SD_FAILURE;
}
} else {
scsi_log(SD_DEVINFO(un), sd_label,
CE_WARN, "data property %s version "
"0x%x is invalid.",
dataname_ptr, version);
rval = SD_FAILURE;
}
if (data_list)
ddi_prop_free(data_list);
}
}
}
/* free up the memory allocated by ddi_prop_lookup_string_array(). */
if (config_list) {
ddi_prop_free(config_list);
}
return (rval);
}
/*
* Function: sd_nvpair_str_decode()
*
* Description: Parse the improved format sd-config-list to get
* each entry of tunable, which includes a name-value pair.
* Then call sd_set_properties() to set the property.
*
* Arguments: un - driver soft state (unit) structure
* nvpair_str - the tunable list
*/
static void
sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
{
char *nv, *name, *value, *token;
char *nv_lasts, *v_lasts, *x_lasts;
for (nv = strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
nv = strtok_r(NULL, ",", &nv_lasts)) {
token = strtok_r(nv, ":", &v_lasts);
name = strtok_r(token, " \t", &x_lasts);
token = strtok_r(NULL, ":", &v_lasts);
value = strtok_r(token, " \t", &x_lasts);
if (name == NULL || value == NULL) {
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_nvpair_str_decode: "
"name or value is not valid!\n");
} else {
sd_set_properties(un, name, value);
}
}
}
/*
* Function: sd_set_properties()
*
* Description: Set device properties based on the improved
* format sd-config-list.
*
* Arguments: un - driver soft state (unit) structure
* name - supported tunable name
* value - tunable value
*/
static void
sd_set_properties(struct sd_lun *un, char *name, char *value)
{
char *endptr = NULL;
long val = 0;
if (strcasecmp(name, "cache-nonvolatile") == 0) {
if (strcasecmp(value, "true") == 0) {
un->un_f_suppress_cache_flush = TRUE;
} else if (strcasecmp(value, "false") == 0) {
un->un_f_suppress_cache_flush = FALSE;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"suppress_cache_flush flag set to %d\n",
un->un_f_suppress_cache_flush);
return;
}
if (strcasecmp(name, "controller-type") == 0) {
if (ddi_strtol(value, &endptr, 0, &val) == 0) {
un->un_ctype = val;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"ctype set to %d\n", un->un_ctype);
return;
}
if (strcasecmp(name, "delay-busy") == 0) {
if (ddi_strtol(value, &endptr, 0, &val) == 0) {
un->un_busy_timeout = drv_usectohz(val / 1000);
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"busy_timeout set to %d\n", un->un_busy_timeout);
return;
}
if (strcasecmp(name, "disksort") == 0) {
if (strcasecmp(value, "true") == 0) {
un->un_f_disksort_disabled = FALSE;
} else if (strcasecmp(value, "false") == 0) {
un->un_f_disksort_disabled = TRUE;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"disksort disabled flag set to %d\n",
un->un_f_disksort_disabled);
return;
}
if (strcasecmp(name, "power-condition") == 0) {
if (strcasecmp(value, "true") == 0) {
un->un_f_power_condition_disabled = FALSE;
} else if (strcasecmp(value, "false") == 0) {
un->un_f_power_condition_disabled = TRUE;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"power condition disabled flag set to %d\n",
un->un_f_power_condition_disabled);
return;
}
if (strcasecmp(name, "timeout-releasereservation") == 0) {
if (ddi_strtol(value, &endptr, 0, &val) == 0) {
un->un_reserve_release_time = val;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"reservation release timeout set to %d\n",
un->un_reserve_release_time);
return;
}
if (strcasecmp(name, "reset-lun") == 0) {
if (strcasecmp(value, "true") == 0) {
un->un_f_lun_reset_enabled = TRUE;
} else if (strcasecmp(value, "false") == 0) {
un->un_f_lun_reset_enabled = FALSE;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"lun reset enabled flag set to %d\n",
un->un_f_lun_reset_enabled);
return;
}
if (strcasecmp(name, "retries-busy") == 0) {
if (ddi_strtol(value, &endptr, 0, &val) == 0) {
un->un_busy_retry_count = val;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"busy retry count set to %d\n", un->un_busy_retry_count);
return;
}
if (strcasecmp(name, "retries-timeout") == 0) {
if (ddi_strtol(value, &endptr, 0, &val) == 0) {
un->un_retry_count = val;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"timeout retry count set to %d\n", un->un_retry_count);
return;
}
if (strcasecmp(name, "retries-notready") == 0) {
if (ddi_strtol(value, &endptr, 0, &val) == 0) {
un->un_notready_retry_count = val;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"notready retry count set to %d\n",
un->un_notready_retry_count);
return;
}
if (strcasecmp(name, "retries-reset") == 0) {
if (ddi_strtol(value, &endptr, 0, &val) == 0) {
un->un_reset_retry_count = val;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"reset retry count set to %d\n",
un->un_reset_retry_count);
return;
}
if (strcasecmp(name, "throttle-max") == 0) {
if (ddi_strtol(value, &endptr, 0, &val) == 0) {
un->un_saved_throttle = un->un_throttle = val;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"throttle set to %d\n", un->un_throttle);
}
if (strcasecmp(name, "throttle-min") == 0) {
if (ddi_strtol(value, &endptr, 0, &val) == 0) {
un->un_min_throttle = val;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"min throttle set to %d\n", un->un_min_throttle);
}
if (strcasecmp(name, "rmw-type") == 0) {
if (ddi_strtol(value, &endptr, 0, &val) == 0) {
un->un_f_rmw_type = val;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"RMW type set to %d\n", un->un_f_rmw_type);
}
if (strcasecmp(name, "physical-block-size") == 0) {
if (ddi_strtol(value, &endptr, 0, &val) == 0 &&
ISP2(val) && val >= un->un_tgt_blocksize &&
val >= un->un_sys_blocksize) {
un->un_phy_blocksize = val;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"physical block size set to %d\n", un->un_phy_blocksize);
}
if (strcasecmp(name, "retries-victim") == 0) {
if (ddi_strtol(value, &endptr, 0, &val) == 0) {
un->un_victim_retry_count = val;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"victim retry count set to %d\n",
un->un_victim_retry_count);
return;
}
/*
* Validate the throttle values.
* If any of the numbers are invalid, set everything to defaults.
*/
if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
(un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
(un->un_min_throttle > un->un_throttle)) {
un->un_saved_throttle = un->un_throttle = sd_max_throttle;
un->un_min_throttle = sd_min_throttle;
}
if (strcasecmp(name, "mmc-gesn-polling") == 0) {
if (strcasecmp(value, "true") == 0) {
un->un_f_mmc_gesn_polling = TRUE;
} else if (strcasecmp(value, "false") == 0) {
un->un_f_mmc_gesn_polling = FALSE;
} else {
goto value_invalid;
}
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"mmc-gesn-polling set to %d\n",
un->un_f_mmc_gesn_polling);
}
return;
value_invalid:
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
"value of prop %s is invalid\n", name);
}
/*
* Function: sd_get_tunables_from_conf()
*
*
* This function reads the data list from the sd.conf file and pulls
* the values that can have numeric values as arguments and places
* the values in the appropriate sd_tunables member.
* Since the order of the data list members varies across platforms
* This function reads them from the data list in a platform specific
* order and places them into the correct sd_tunable member that is
* consistent across all platforms.
*/
static void
sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
sd_tunables *values)
{
int i;
int mask;
bzero(values, sizeof (sd_tunables));
for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
mask = 1 << i;
if (mask > flags) {
break;
}
switch (mask & flags) {
case 0: /* This mask bit not set in flags */
continue;
case SD_CONF_BSET_THROTTLE:
values->sdt_throttle = data_list[i];
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_get_tunables_from_conf: throttle = %d\n",
values->sdt_throttle);
break;
case SD_CONF_BSET_CTYPE:
values->sdt_ctype = data_list[i];
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_get_tunables_from_conf: ctype = %d\n",
values->sdt_ctype);
break;
case SD_CONF_BSET_NRR_COUNT:
values->sdt_not_rdy_retries = data_list[i];
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_get_tunables_from_conf: not_rdy_retries = %d\n",
values->sdt_not_rdy_retries);
break;
case SD_CONF_BSET_BSY_RETRY_COUNT:
values->sdt_busy_retries = data_list[i];
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_get_tunables_from_conf: busy_retries = %d\n",
values->sdt_busy_retries);
break;
case SD_CONF_BSET_RST_RETRIES:
values->sdt_reset_retries = data_list[i];
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_get_tunables_from_conf: reset_retries = %d\n",
values->sdt_reset_retries);
break;
case SD_CONF_BSET_RSV_REL_TIME:
values->sdt_reserv_rel_time = data_list[i];
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_get_tunables_from_conf: reserv_rel_time = %d\n",
values->sdt_reserv_rel_time);
break;
case SD_CONF_BSET_MIN_THROTTLE:
values->sdt_min_throttle = data_list[i];
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_get_tunables_from_conf: min_throttle = %d\n",
values->sdt_min_throttle);
break;
case SD_CONF_BSET_DISKSORT_DISABLED:
values->sdt_disk_sort_dis = data_list[i];
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_get_tunables_from_conf: disk_sort_dis = %d\n",
values->sdt_disk_sort_dis);
break;
case SD_CONF_BSET_LUN_RESET_ENABLED:
values->sdt_lun_reset_enable = data_list[i];
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_get_tunables_from_conf: lun_reset_enable = %d"
"\n", values->sdt_lun_reset_enable);
break;
case SD_CONF_BSET_CACHE_IS_NV:
values->sdt_suppress_cache_flush = data_list[i];
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_get_tunables_from_conf: \
suppress_cache_flush = %d"
"\n", values->sdt_suppress_cache_flush);
break;
case SD_CONF_BSET_PC_DISABLED:
values->sdt_disk_sort_dis = data_list[i];
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_get_tunables_from_conf: power_condition_dis = "
"%d\n", values->sdt_power_condition_dis);
break;
}
}
}
/*
* Function: sd_process_sdconf_table
*
* Description: Search the static configuration table for a match on the
* inquiry vid/pid and update the driver soft state structure
* according to the table property values for the device.
*
* The form of a configuration table entry is:
* <vid+pid>,<flags>,<property-data>
* "SEAGATE ST42400N",1,0x40000,
* 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
*
* Arguments: un - driver soft state (unit) structure
*/
static void
sd_process_sdconf_table(struct sd_lun *un)
{
char *id = NULL;
int table_index;
int idlen;
ASSERT(un != NULL);
for (table_index = 0; table_index < sd_disk_table_size;
table_index++) {
id = sd_disk_table[table_index].device_id;
idlen = strlen(id);
/*
* The static configuration table currently does not
* implement version 10 properties. Additionally,
* multiple data-property-name entries are not
* implemented in the static configuration table.
*/
if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_process_sdconf_table: disk %s\n", id);
sd_set_vers1_properties(un,
sd_disk_table[table_index].flags,
sd_disk_table[table_index].properties);
break;
}
}
}
/*
* Function: sd_sdconf_id_match
*
* Description: This local function implements a case sensitive vid/pid
* comparison as well as the boundary cases of wild card and
* multiple blanks.
*
* Note: An implicit assumption made here is that the scsi
* inquiry structure will always keep the vid, pid and
* revision strings in consecutive sequence, so they can be
* read as a single string. If this assumption is not the
* case, a separate string, to be used for the check, needs
* to be built with these strings concatenated.
*
* Arguments: un - driver soft state (unit) structure
* id - table or config file vid/pid
* idlen - length of the vid/pid (bytes)
*
* Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
* SD_FAILURE - Indicates no match with the inquiry vid/pid
*/
static int
sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
{
struct scsi_inquiry *sd_inq;
int rval = SD_SUCCESS;
ASSERT(un != NULL);
sd_inq = un->un_sd->sd_inq;
ASSERT(id != NULL);
/*
* We use the inq_vid as a pointer to a buffer containing the
* vid and pid and use the entire vid/pid length of the table
* entry for the comparison. This works because the inq_pid
* data member follows inq_vid in the scsi_inquiry structure.
*/
if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
/*
* The user id string is compared to the inquiry vid/pid
* using a case insensitive comparison and ignoring
* multiple spaces.
*/
rval = sd_blank_cmp(un, id, idlen);
if (rval != SD_SUCCESS) {
/*
* User id strings that start and end with a "*"
* are a special case. These do not have a
* specific vendor, and the product string can
* appear anywhere in the 16 byte PID portion of
* the inquiry data. This is a simple strstr()
* type search for the user id in the inquiry data.
*/
if ((id[0] == '*') && (id[idlen - 1] == '*')) {
char *pidptr = &id[1];
int i;
int j;
int pidstrlen = idlen - 2;
j = sizeof (SD_INQUIRY(un)->inq_pid) -
pidstrlen;
if (j < 0) {
return (SD_FAILURE);
}
for (i = 0; i < j; i++) {
if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
pidptr, pidstrlen) == 0) {
rval = SD_SUCCESS;
break;
}
}
}
}
}
return (rval);
}
/*
* Function: sd_blank_cmp
*
* Description: If the id string starts and ends with a space, treat
* multiple consecutive spaces as equivalent to a single
* space. For example, this causes a sd_disk_table entry
* of " NEC CDROM " to match a device's id string of
* "NEC CDROM".
*
* Note: The success exit condition for this routine is if
* the pointer to the table entry is '\0' and the cnt of
* the inquiry length is zero. This will happen if the inquiry
* string returned by the device is padded with spaces to be
* exactly 24 bytes in length (8 byte vid + 16 byte pid). The
* SCSI spec states that the inquiry string is to be padded with
* spaces.
*
* Arguments: un - driver soft state (unit) structure
* id - table or config file vid/pid
* idlen - length of the vid/pid (bytes)
*
* Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
* SD_FAILURE - Indicates no match with the inquiry vid/pid
*/
static int
sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
{
char *p1;
char *p2;
int cnt;
cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
sizeof (SD_INQUIRY(un)->inq_pid);
ASSERT(un != NULL);
p2 = un->un_sd->sd_inq->inq_vid;
ASSERT(id != NULL);
p1 = id;
if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
/*
* Note: string p1 is terminated by a NUL but string p2
* isn't. The end of p2 is determined by cnt.
*/
for (;;) {
/* skip over any extra blanks in both strings */
while ((*p1 != '\0') && (*p1 == ' ')) {
p1++;
}
while ((cnt != 0) && (*p2 == ' ')) {
p2++;
cnt--;
}
/* compare the two strings */
if ((cnt == 0) ||
(SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
break;
}
while ((cnt > 0) &&
(SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
p1++;
p2++;
cnt--;
}
}
}
/* return SD_SUCCESS if both strings match */
return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
}
/*
* Function: sd_chk_vers1_data
*
* Description: Verify the version 1 device properties provided by the
* user via the configuration file
*
* Arguments: un - driver soft state (unit) structure
* flags - integer mask indicating properties to be set
* prop_list - integer list of property values
* list_len - number of the elements
*
* Return Code: SD_SUCCESS - Indicates the user provided data is valid
* SD_FAILURE - Indicates the user provided data is invalid
*/
static int
sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
int list_len, char *dataname_ptr)
{
int i;
int mask = 1;
int index = 0;
ASSERT(un != NULL);
/* Check for a NULL property name and list */
if (dataname_ptr == NULL) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sd_chk_vers1_data: NULL data property name.");
return (SD_FAILURE);
}
if (prop_list == NULL) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sd_chk_vers1_data: %s NULL data property list.",
dataname_ptr);
return (SD_FAILURE);
}
/* Display a warning if undefined bits are set in the flags */
if (flags & ~SD_CONF_BIT_MASK) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
"Properties not set.",
(flags & ~SD_CONF_BIT_MASK), dataname_ptr);
return (SD_FAILURE);
}
/*
* Verify the length of the list by identifying the highest bit set
* in the flags and validating that the property list has a length
* up to the index of this bit.
*/
for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
if (flags & mask) {
index++;
}
mask = 1 << i;
}
if (list_len < (index + 2)) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sd_chk_vers1_data: "
"Data property list %s size is incorrect. "
"Properties not set.", dataname_ptr);
scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
"version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
return (SD_FAILURE);
}
return (SD_SUCCESS);
}
/*
* Function: sd_set_vers1_properties
*
* Description: Set version 1 device properties based on a property list
* retrieved from the driver configuration file or static
* configuration table. Version 1 properties have the format:
*
* <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
*
* where the prop0 value will be used to set prop0 if bit0
* is set in the flags
*
* Arguments: un - driver soft state (unit) structure
* flags - integer mask indicating properties to be set
* prop_list - integer list of property values
*/
static void
sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
{
ASSERT(un != NULL);
/*
* Set the flag to indicate cache is to be disabled. An attempt
* to disable the cache via sd_cache_control() will be made
* later during attach once the basic initialization is complete.
*/
if (flags & SD_CONF_BSET_NOCACHE) {
un->un_f_opt_disable_cache = TRUE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: caching disabled flag set\n");
}
/* CD-specific configuration parameters */
if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
un->un_f_cfg_playmsf_bcd = TRUE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: playmsf_bcd set\n");
}
if (flags & SD_CONF_BSET_READSUB_BCD) {
un->un_f_cfg_readsub_bcd = TRUE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: readsub_bcd set\n");
}
if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
un->un_f_cfg_read_toc_trk_bcd = TRUE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: read_toc_trk_bcd set\n");
}
if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
un->un_f_cfg_read_toc_addr_bcd = TRUE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: read_toc_addr_bcd set\n");
}
if (flags & SD_CONF_BSET_NO_READ_HEADER) {
un->un_f_cfg_no_read_header = TRUE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: no_read_header set\n");
}
if (flags & SD_CONF_BSET_READ_CD_XD4) {
un->un_f_cfg_read_cd_xd4 = TRUE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: read_cd_xd4 set\n");
}
/* Support for devices which do not have valid/unique serial numbers */
if (flags & SD_CONF_BSET_FAB_DEVID) {
un->un_f_opt_fab_devid = TRUE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: fab_devid bit set\n");
}
/* Support for user throttle configuration */
if (flags & SD_CONF_BSET_THROTTLE) {
ASSERT(prop_list != NULL);
un->un_saved_throttle = un->un_throttle =
prop_list->sdt_throttle;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: throttle set to %d\n",
prop_list->sdt_throttle);
}
/* Set the per disk retry count according to the conf file or table. */
if (flags & SD_CONF_BSET_NRR_COUNT) {
ASSERT(prop_list != NULL);
if (prop_list->sdt_not_rdy_retries) {
un->un_notready_retry_count =
prop_list->sdt_not_rdy_retries;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: not ready retry count"
" set to %d\n", un->un_notready_retry_count);
}
}
/* The controller type is reported for generic disk driver ioctls */
if (flags & SD_CONF_BSET_CTYPE) {
ASSERT(prop_list != NULL);
switch (prop_list->sdt_ctype) {
case CTYPE_CDROM:
un->un_ctype = prop_list->sdt_ctype;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: ctype set to "
"CTYPE_CDROM\n");
break;
case CTYPE_CCS:
un->un_ctype = prop_list->sdt_ctype;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: ctype set to "
"CTYPE_CCS\n");
break;
case CTYPE_ROD: /* RW optical */
un->un_ctype = prop_list->sdt_ctype;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: ctype set to "
"CTYPE_ROD\n");
break;
default:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sd_set_vers1_properties: Could not set "
"invalid ctype value (%d)",
prop_list->sdt_ctype);
}
}
/* Purple failover timeout */
if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
ASSERT(prop_list != NULL);
un->un_busy_retry_count =
prop_list->sdt_busy_retries;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: "
"busy retry count set to %d\n",
un->un_busy_retry_count);
}
/* Purple reset retry count */
if (flags & SD_CONF_BSET_RST_RETRIES) {
ASSERT(prop_list != NULL);
un->un_reset_retry_count =
prop_list->sdt_reset_retries;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: "
"reset retry count set to %d\n",
un->un_reset_retry_count);
}
/* Purple reservation release timeout */
if (flags & SD_CONF_BSET_RSV_REL_TIME) {
ASSERT(prop_list != NULL);
un->un_reserve_release_time =
prop_list->sdt_reserv_rel_time;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: "
"reservation release timeout set to %d\n",
un->un_reserve_release_time);
}
/*
* Driver flag telling the driver to verify that no commands are pending
* for a device before issuing a Test Unit Ready. This is a workaround
* for a firmware bug in some Seagate eliteI drives.
*/
if (flags & SD_CONF_BSET_TUR_CHECK) {
un->un_f_cfg_tur_check = TRUE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: tur queue check set\n");
}
if (flags & SD_CONF_BSET_MIN_THROTTLE) {
un->un_min_throttle = prop_list->sdt_min_throttle;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: min throttle set to %d\n",
un->un_min_throttle);
}
if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
un->un_f_disksort_disabled =
(prop_list->sdt_disk_sort_dis != 0) ?
TRUE : FALSE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: disksort disabled "
"flag set to %d\n",
prop_list->sdt_disk_sort_dis);
}
if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
un->un_f_lun_reset_enabled =
(prop_list->sdt_lun_reset_enable != 0) ?
TRUE : FALSE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: lun reset enabled "
"flag set to %d\n",
prop_list->sdt_lun_reset_enable);
}
if (flags & SD_CONF_BSET_CACHE_IS_NV) {
un->un_f_suppress_cache_flush =
(prop_list->sdt_suppress_cache_flush != 0) ?
TRUE : FALSE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: suppress_cache_flush "
"flag set to %d\n",
prop_list->sdt_suppress_cache_flush);
}
if (flags & SD_CONF_BSET_PC_DISABLED) {
un->un_f_power_condition_disabled =
(prop_list->sdt_power_condition_dis != 0) ?
TRUE : FALSE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_set_vers1_properties: power_condition_disabled "
"flag set to %d\n",
prop_list->sdt_power_condition_dis);
}
/*
* Validate the throttle values.
* If any of the numbers are invalid, set everything to defaults.
*/
if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
(un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
(un->un_min_throttle > un->un_throttle)) {
un->un_saved_throttle = un->un_throttle = sd_max_throttle;
un->un_min_throttle = sd_min_throttle;
}
}
/*
* Function: sd_get_physical_geometry
*
* Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
* MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
* target, and use this information to initialize the physical
* geometry cache specified by pgeom_p.
*
* MODE SENSE is an optional command, so failure in this case
* does not necessarily denote an error. We want to use the
* MODE SENSE commands to derive the physical geometry of the
* device, but if either command fails, the logical geometry is
* used as the fallback for disk label geometry in cmlb.
*
* This requires that un->un_blockcount and un->un_tgt_blocksize
* have already been initialized for the current target and
* that the current values be passed as args so that we don't
* end up ever trying to use -1 as a valid value. This could
* happen if either value is reset while we're not holding
* the mutex.
*
* Arguments: un - driver soft state (unit) structure
* path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
* the normal command waitq, or SD_PATH_DIRECT_PRIORITY
* to use the USCSI "direct" chain and bypass the normal
* command waitq.
*
* Context: Kernel thread only (can sleep).
*/
static int
sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
diskaddr_t capacity, int lbasize, int path_flag)
{
struct mode_format *page3p;
struct mode_geometry *page4p;
struct mode_header *headerp;
int sector_size;
int nsect;
int nhead;
int ncyl;
int intrlv;
int spc;
diskaddr_t modesense_capacity;
int rpm;
int bd_len;
int mode_header_length;
uchar_t *p3bufp;
uchar_t *p4bufp;
int cdbsize;
int ret = EIO;
sd_ssc_t *ssc;
int status;
ASSERT(un != NULL);
if (lbasize == 0) {
if (ISCD(un)) {
lbasize = 2048;
} else {
lbasize = un->un_sys_blocksize;
}
}
pgeom_p->g_secsize = (unsigned short)lbasize;
/*
* If the unit is a cd/dvd drive MODE SENSE page three
* and MODE SENSE page four are reserved (see SBC spec
* and MMC spec). To prevent soft errors just return
* using the default LBA size.
*
* Since SATA MODE SENSE function (sata_txlt_mode_sense()) does not
* implement support for mode pages 3 and 4 return here to prevent
* illegal requests on SATA drives.
*
* These pages are also reserved in SBC-2 and later. We assume SBC-2
* or later for a direct-attached block device if the SCSI version is
* at least SPC-3.
*/
if (ISCD(un) ||
un->un_interconnect_type == SD_INTERCONNECT_SATA ||
(un->un_ctype == CTYPE_CCS && SD_INQUIRY(un)->inq_ansi >= 5))
return (ret);
cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
/*
* Retrieve MODE SENSE page 3 - Format Device Page
*/
p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
ssc = sd_ssc_init(un);
status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
if (status != 0) {
SD_ERROR(SD_LOG_COMMON, un,
"sd_get_physical_geometry: mode sense page 3 failed\n");
goto page3_exit;
}
/*
* Determine size of Block Descriptors in order to locate the mode
* page data. ATAPI devices return 0, SCSI devices should return
* MODE_BLK_DESC_LENGTH.
*/
headerp = (struct mode_header *)p3bufp;
if (un->un_f_cfg_is_atapi == TRUE) {
struct mode_header_grp2 *mhp =
(struct mode_header_grp2 *)headerp;
mode_header_length = MODE_HEADER_LENGTH_GRP2;
bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
} else {
mode_header_length = MODE_HEADER_LENGTH;
bd_len = ((struct mode_header *)headerp)->bdesc_length;
}
if (bd_len > MODE_BLK_DESC_LENGTH) {
sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
"sd_get_physical_geometry: received unexpected bd_len "
"of %d, page3\n", bd_len);
status = EIO;
goto page3_exit;
}
page3p = (struct mode_format *)
((caddr_t)headerp + mode_header_length + bd_len);
if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
"sd_get_physical_geometry: mode sense pg3 code mismatch "
"%d\n", page3p->mode_page.code);
status = EIO;
goto page3_exit;
}
/*
* Use this physical geometry data only if BOTH MODE SENSE commands
* complete successfully; otherwise, revert to the logical geometry.
* So, we need to save everything in temporary variables.
*/
sector_size = BE_16(page3p->data_bytes_sect);
/*
* 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
*/
if (sector_size == 0) {
sector_size = un->un_sys_blocksize;
} else {
sector_size &= ~(un->un_sys_blocksize - 1);
}
nsect = BE_16(page3p->sect_track);
intrlv = BE_16(page3p->interleave);
SD_INFO(SD_LOG_COMMON, un,
"sd_get_physical_geometry: Format Parameters (page 3)\n");
SD_INFO(SD_LOG_COMMON, un,
" mode page: %d; nsect: %d; sector size: %d;\n",
page3p->mode_page.code, nsect, sector_size);
SD_INFO(SD_LOG_COMMON, un,
" interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
BE_16(page3p->track_skew),
BE_16(page3p->cylinder_skew));
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
/*
* Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
*/
p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
if (status != 0) {
SD_ERROR(SD_LOG_COMMON, un,
"sd_get_physical_geometry: mode sense page 4 failed\n");
goto page4_exit;
}
/*
* Determine size of Block Descriptors in order to locate the mode
* page data. ATAPI devices return 0, SCSI devices should return
* MODE_BLK_DESC_LENGTH.
*/
headerp = (struct mode_header *)p4bufp;
if (un->un_f_cfg_is_atapi == TRUE) {
struct mode_header_grp2 *mhp =
(struct mode_header_grp2 *)headerp;
bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
} else {
bd_len = ((struct mode_header *)headerp)->bdesc_length;
}
if (bd_len > MODE_BLK_DESC_LENGTH) {
sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
"sd_get_physical_geometry: received unexpected bd_len of "
"%d, page4\n", bd_len);
status = EIO;
goto page4_exit;
}
page4p = (struct mode_geometry *)
((caddr_t)headerp + mode_header_length + bd_len);
if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
"sd_get_physical_geometry: mode sense pg4 code mismatch "
"%d\n", page4p->mode_page.code);
status = EIO;
goto page4_exit;
}
/*
* Stash the data now, after we know that both commands completed.
*/
nhead = (int)page4p->heads; /* uchar, so no conversion needed */
spc = nhead * nsect;
ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
rpm = BE_16(page4p->rpm);
modesense_capacity = spc * ncyl;
SD_INFO(SD_LOG_COMMON, un,
"sd_get_physical_geometry: Geometry Parameters (page 4)\n");
SD_INFO(SD_LOG_COMMON, un,
" cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
SD_INFO(SD_LOG_COMMON, un,
" computed capacity(h*s*c): %d;\n", modesense_capacity);
SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n",
(void *)pgeom_p, capacity);
/*
* Compensate if the drive's geometry is not rectangular, i.e.,
* the product of C * H * S returned by MODE SENSE >= that returned
* by read capacity. This is an idiosyncrasy of the original x86
* disk subsystem.
*/
if (modesense_capacity >= capacity) {
SD_INFO(SD_LOG_COMMON, un,
"sd_get_physical_geometry: adjusting acyl; "
"old: %d; new: %d\n", pgeom_p->g_acyl,
(modesense_capacity - capacity + spc - 1) / spc);
if (sector_size != 0) {
/* 1243403: NEC D38x7 drives don't support sec size */
pgeom_p->g_secsize = (unsigned short)sector_size;
}
pgeom_p->g_nsect = (unsigned short)nsect;
pgeom_p->g_nhead = (unsigned short)nhead;
pgeom_p->g_capacity = capacity;
pgeom_p->g_acyl =
(modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl;
}
pgeom_p->g_rpm = (unsigned short)rpm;
pgeom_p->g_intrlv = (unsigned short)intrlv;
ret = 0;
SD_INFO(SD_LOG_COMMON, un,
"sd_get_physical_geometry: mode sense geometry:\n");
SD_INFO(SD_LOG_COMMON, un,
" nsect: %d; sector size: %d; interlv: %d\n",
nsect, sector_size, intrlv);
SD_INFO(SD_LOG_COMMON, un,
" nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
nhead, ncyl, rpm, modesense_capacity);
SD_INFO(SD_LOG_COMMON, un,
"sd_get_physical_geometry: (cached)\n");
SD_INFO(SD_LOG_COMMON, un,
" ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
pgeom_p->g_ncyl, pgeom_p->g_acyl,
pgeom_p->g_nhead, pgeom_p->g_nsect);
SD_INFO(SD_LOG_COMMON, un,
" lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
pgeom_p->g_secsize, pgeom_p->g_capacity,
pgeom_p->g_intrlv, pgeom_p->g_rpm);
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
page4_exit:
kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
page3_exit:
kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
if (status != 0) {
if (status == EIO) {
/*
* Some disks do not support mode sense(6), we
* should ignore this kind of error(sense key is
* 0x5 - illegal request).
*/
uint8_t *sensep;
int senlen;
sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
ssc->ssc_uscsi_cmd->uscsi_rqresid);
if (senlen > 0 &&
scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
sd_ssc_assessment(ssc,
SD_FMT_IGNORE_COMPROMISE);
} else {
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
}
} else {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
}
sd_ssc_fini(ssc);
return (ret);
}
/*
* Function: sd_get_virtual_geometry
*
* Description: Ask the controller to tell us about the target device.
*
* Arguments: un - pointer to softstate
* capacity - disk capacity in #blocks
* lbasize - disk block size in bytes
*
* Context: Kernel thread only
*/
static int
sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
diskaddr_t capacity, int lbasize)
{
uint_t geombuf;
int spc;
ASSERT(un != NULL);
/* Set sector size, and total number of sectors */
(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1);
(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
/* Let the HBA tell us its geometry */
geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
/* A value of -1 indicates an undefined "geometry" property */
if (geombuf == (-1)) {
return (EINVAL);
}
/* Initialize the logical geometry cache. */
lgeom_p->g_nhead = (geombuf >> 16) & 0xffff;
lgeom_p->g_nsect = geombuf & 0xffff;
lgeom_p->g_secsize = un->un_sys_blocksize;
spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
/*
* Note: The driver originally converted the capacity value from
* target blocks to system blocks. However, the capacity value passed
* to this routine is already in terms of system blocks (this scaling
* is done when the READ CAPACITY command is issued and processed).
* This 'error' may have gone undetected because the usage of g_ncyl
* (which is based upon g_capacity) is very limited within the driver
*/
lgeom_p->g_capacity = capacity;
/*
* Set ncyl to zero if the hba returned a zero nhead or nsect value. The
* hba may return zero values if the device has been removed.
*/
if (spc == 0) {
lgeom_p->g_ncyl = 0;
} else {
lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
}
lgeom_p->g_acyl = 0;
SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
return (0);
}
/*
* Function: sd_update_block_info
*
* Description: Calculate a byte count to sector count bitshift value
* from sector size.
*
* Arguments: un: unit struct.
* lbasize: new target sector size
* capacity: new target capacity, ie. block count
*
* Context: Kernel thread context
*/
static void
sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
{
if (lbasize != 0) {
un->un_tgt_blocksize = lbasize;
un->un_f_tgt_blocksize_is_valid = TRUE;
if (!un->un_f_has_removable_media) {
un->un_sys_blocksize = lbasize;
}
}
if (capacity != 0) {
un->un_blockcount = capacity;
un->un_f_blockcount_is_valid = TRUE;
/*
* The capacity has changed so update the errstats.
*/
if (un->un_errstats != NULL) {
struct sd_errstats *stp;
capacity *= un->un_sys_blocksize;
stp = (struct sd_errstats *)un->un_errstats->ks_data;
if (stp->sd_capacity.value.ui64 < capacity)
stp->sd_capacity.value.ui64 = capacity;
}
}
}
/*
* Parses the SCSI Block Limits VPD page (0xB0). It's legal to pass NULL for
* vpd_pg, in which case all the block limits will be reset to the defaults.
*/
static void
sd_parse_blk_limits_vpd(struct sd_lun *un, uchar_t *vpd_pg)
{
sd_blk_limits_t *lim = &un->un_blk_lim;
unsigned pg_len;
if (vpd_pg != NULL)
pg_len = BE_IN16(&vpd_pg[2]);
else
pg_len = 0;
/* Block Limits VPD can be 16 bytes or 64 bytes long - support both */
if (pg_len >= 0x10) {
lim->lim_opt_xfer_len_gran = BE_IN16(&vpd_pg[6]);
lim->lim_max_xfer_len = BE_IN32(&vpd_pg[8]);
lim->lim_opt_xfer_len = BE_IN32(&vpd_pg[12]);
/* Zero means not reported, so use "unlimited" */
if (lim->lim_max_xfer_len == 0)
lim->lim_max_xfer_len = UINT32_MAX;
if (lim->lim_opt_xfer_len == 0)
lim->lim_opt_xfer_len = UINT32_MAX;
} else {
lim->lim_opt_xfer_len_gran = 0;
lim->lim_max_xfer_len = UINT32_MAX;
lim->lim_opt_xfer_len = UINT32_MAX;
}
if (pg_len >= 0x3c) {
lim->lim_max_pfetch_len = BE_IN32(&vpd_pg[16]);
/*
* A zero in either of the following two fields indicates lack
* of UNMAP support.
*/
lim->lim_max_unmap_lba_cnt = BE_IN32(&vpd_pg[20]);
lim->lim_max_unmap_descr_cnt = BE_IN32(&vpd_pg[24]);
lim->lim_opt_unmap_gran = BE_IN32(&vpd_pg[28]);
if ((vpd_pg[32] >> 7) == 1) {
lim->lim_unmap_gran_align =
((vpd_pg[32] & 0x7f) << 24) | (vpd_pg[33] << 16) |
(vpd_pg[34] << 8) | vpd_pg[35];
} else {
lim->lim_unmap_gran_align = 0;
}
lim->lim_max_write_same_len = BE_IN64(&vpd_pg[36]);
} else {
lim->lim_max_pfetch_len = UINT32_MAX;
lim->lim_max_unmap_lba_cnt = UINT32_MAX;
lim->lim_max_unmap_descr_cnt = SD_UNMAP_MAX_DESCR;
lim->lim_opt_unmap_gran = 0;
lim->lim_unmap_gran_align = 0;
lim->lim_max_write_same_len = UINT64_MAX;
}
}
/*
* Collects VPD page B0 data if available (block limits). If the data is
* not available or querying the device failed, we revert to the defaults.
*/
static void
sd_setup_blk_limits(sd_ssc_t *ssc)
{
struct sd_lun *un = ssc->ssc_un;
uchar_t *inqB0 = NULL;
size_t inqB0_resid = 0;
int rval;
if (un->un_vpd_page_mask & SD_VPD_BLK_LIMITS_PG) {
inqB0 = kmem_zalloc(MAX_INQUIRY_SIZE, KM_SLEEP);
rval = sd_send_scsi_INQUIRY(ssc, inqB0, MAX_INQUIRY_SIZE, 0x01,
0xB0, &inqB0_resid);
if (rval != 0) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
kmem_free(inqB0, MAX_INQUIRY_SIZE);
inqB0 = NULL;
}
}
/* passing NULL inqB0 will reset to defaults */
sd_parse_blk_limits_vpd(ssc->ssc_un, inqB0);
if (inqB0)
kmem_free(inqB0, MAX_INQUIRY_SIZE);
}
/*
* Function: sd_register_devid
*
* Description: This routine will obtain the device id information from the
* target, obtain the serial number, and register the device
* id with the ddi framework.
*
* Arguments: devi - the system's dev_info_t for the device.
* un - driver soft state (unit) structure
* reservation_flag - indicates if a reservation conflict
* occurred during attach
*
* Context: Kernel Thread
*/
static void
sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
{
int rval = 0;
uchar_t *inq80 = NULL;
size_t inq80_len = MAX_INQUIRY_SIZE;
size_t inq80_resid = 0;
uchar_t *inq83 = NULL;
size_t inq83_len = MAX_INQUIRY_SIZE;
size_t inq83_resid = 0;
int dlen, len;
char *sn;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT((SD_DEVINFO(un)) == devi);
/*
* We check the availability of the World Wide Name (0x83) and Unit
* Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
* un_vpd_page_mask from them, we decide which way to get the WWN. If
* 0x83 is available, that is the best choice. Our next choice is
* 0x80. If neither are available, we munge the devid from the device
* vid/pid/serial # for Sun qualified disks, or use the ddi framework
* to fabricate a devid for non-Sun qualified disks.
*/
if (sd_check_vpd_page_support(ssc) == 0) {
/* collect page 80 data if available */
if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
mutex_exit(SD_MUTEX(un));
inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
0x01, 0x80, &inq80_resid);
if (rval != 0) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
kmem_free(inq80, inq80_len);
inq80 = NULL;
inq80_len = 0;
} else if (ddi_prop_exists(
DDI_DEV_T_NONE, SD_DEVINFO(un),
DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
INQUIRY_SERIAL_NO) == 0) {
/*
* If we don't already have a serial number
* property, do quick verify of data returned
* and define property.
*/
dlen = inq80_len - inq80_resid;
len = (size_t)inq80[3];
if ((dlen >= 4) && ((len + 4) <= dlen)) {
/*
* Ensure sn termination, skip leading
* blanks, and create property
* 'inquiry-serial-no'.
*/
sn = (char *)&inq80[4];
sn[len] = 0;
while (*sn && (*sn == ' '))
sn++;
if (*sn) {
(void) ddi_prop_update_string(
DDI_DEV_T_NONE,
SD_DEVINFO(un),
INQUIRY_SERIAL_NO, sn);
}
}
}
mutex_enter(SD_MUTEX(un));
}
/* collect page 83 data if available */
if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
mutex_exit(SD_MUTEX(un));
inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
0x01, 0x83, &inq83_resid);
if (rval != 0) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
kmem_free(inq83, inq83_len);
inq83 = NULL;
inq83_len = 0;
}
mutex_enter(SD_MUTEX(un));
}
}
/*
* If transport has already registered a devid for this target
* then that takes precedence over the driver's determination
* of the devid.
*
* NOTE: The reason this check is done here instead of at the beginning
* of the function is to allow the code above to create the
* 'inquiry-serial-no' property.
*/
if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
ASSERT(un->un_devid);
un->un_f_devid_transport_defined = TRUE;
goto cleanup; /* use devid registered by the transport */
}
/*
* This is the case of antiquated Sun disk drives that have the
* FAB_DEVID property set in the disk_table. These drives
* manage the devid's by storing them in last 2 available sectors
* on the drive and have them fabricated by the ddi layer by calling
* ddi_devid_init and passing the DEVID_FAB flag.
*/
if (un->un_f_opt_fab_devid == TRUE) {
/*
* Depending on EINVAL isn't reliable, since a reserved disk
* may result in invalid geometry, so check to make sure a
* reservation conflict did not occur during attach.
*/
if ((sd_get_devid(ssc) == EINVAL) &&
(reservation_flag != SD_TARGET_IS_RESERVED)) {
/*
* The devid is invalid AND there is no reservation
* conflict. Fabricate a new devid.
*/
(void) sd_create_devid(ssc);
}
/* Register the devid if it exists */
if (un->un_devid != NULL) {
(void) ddi_devid_register(SD_DEVINFO(un),
un->un_devid);
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_register_devid: Devid Fabricated\n");
}
goto cleanup;
}
/* encode best devid possible based on data available */
if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
(char *)ddi_driver_name(SD_DEVINFO(un)),
(uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
inq80, inq80_len - inq80_resid, inq83, inq83_len -
inq83_resid, &un->un_devid) == DDI_SUCCESS) {
/* devid successfully encoded, register devid */
(void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
} else {
/*
* Unable to encode a devid based on data available.
* This is not a Sun qualified disk. Older Sun disk
* drives that have the SD_FAB_DEVID property
* set in the disk_table and non Sun qualified
* disks are treated in the same manner. These
* drives manage the devid's by storing them in
* last 2 available sectors on the drive and
* have them fabricated by the ddi layer by
* calling ddi_devid_init and passing the
* DEVID_FAB flag.
* Create a fabricate devid only if there's no
* fabricate devid existed.
*/
if (sd_get_devid(ssc) == EINVAL) {
(void) sd_create_devid(ssc);
}
un->un_f_opt_fab_devid = TRUE;
/* Register the devid if it exists */
if (un->un_devid != NULL) {
(void) ddi_devid_register(SD_DEVINFO(un),
un->un_devid);
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_register_devid: devid fabricated using "
"ddi framework\n");
}
}
cleanup:
/* clean up resources */
if (inq80 != NULL) {
kmem_free(inq80, inq80_len);
}
if (inq83 != NULL) {
kmem_free(inq83, inq83_len);
}
}
/*
* Function: sd_get_devid
*
* Description: This routine will return 0 if a valid device id has been
* obtained from the target and stored in the soft state. If a
* valid device id has not been previously read and stored, a
* read attempt will be made.
*
* Arguments: un - driver soft state (unit) structure
*
* Return Code: 0 if we successfully get the device id
*
* Context: Kernel Thread
*/
static int
sd_get_devid(sd_ssc_t *ssc)
{
struct dk_devid *dkdevid;
ddi_devid_t tmpid;
uint_t *ip;
size_t sz;
diskaddr_t blk;
int status;
int chksum;
int i;
size_t buffer_size;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
un);
if (un->un_devid != NULL) {
return (0);
}
mutex_exit(SD_MUTEX(un));
if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
(void *)SD_PATH_DIRECT) != 0) {
mutex_enter(SD_MUTEX(un));
return (EINVAL);
}
/*
* Read and verify device id, stored in the reserved cylinders at the
* end of the disk. Backup label is on the odd sectors of the last
* track of the last cylinder. Device id will be on track of the next
* to last cylinder.
*/
mutex_enter(SD_MUTEX(un));
buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
mutex_exit(SD_MUTEX(un));
dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
SD_PATH_DIRECT);
if (status != 0) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
goto error;
}
/* Validate the revision */
if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
(dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
status = EINVAL;
goto error;
}
/* Calculate the checksum */
chksum = 0;
ip = (uint_t *)dkdevid;
for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
i++) {
chksum ^= ip[i];
}
/* Compare the checksums */
if (DKD_GETCHKSUM(dkdevid) != chksum) {
status = EINVAL;
goto error;
}
/* Validate the device id */
if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
status = EINVAL;
goto error;
}
/*
* Store the device id in the driver soft state
*/
sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
tmpid = kmem_alloc(sz, KM_SLEEP);
mutex_enter(SD_MUTEX(un));
un->un_devid = tmpid;
bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
kmem_free(dkdevid, buffer_size);
SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
return (status);
error:
mutex_enter(SD_MUTEX(un));
kmem_free(dkdevid, buffer_size);
return (status);
}
/*
* Function: sd_create_devid
*
* Description: This routine will fabricate the device id and write it
* to the disk.
*
* Arguments: un - driver soft state (unit) structure
*
* Return Code: value of the fabricated device id
*
* Context: Kernel Thread
*/
static ddi_devid_t
sd_create_devid(sd_ssc_t *ssc)
{
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
/* Fabricate the devid */
if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
== DDI_FAILURE) {
return (NULL);
}
/* Write the devid to disk */
if (sd_write_deviceid(ssc) != 0) {
ddi_devid_free(un->un_devid);
un->un_devid = NULL;
}
return (un->un_devid);
}
/*
* Function: sd_write_deviceid
*
* Description: This routine will write the device id to the disk
* reserved sector.
*
* Arguments: un - driver soft state (unit) structure
*
* Return Code: EINVAL
* value returned by sd_send_scsi_cmd
*
* Context: Kernel Thread
*/
static int
sd_write_deviceid(sd_ssc_t *ssc)
{
struct dk_devid *dkdevid;
uchar_t *buf;
diskaddr_t blk;
uint_t *ip, chksum;
int status;
int i;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
mutex_exit(SD_MUTEX(un));
if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
(void *)SD_PATH_DIRECT) != 0) {
mutex_enter(SD_MUTEX(un));
return (-1);
}
/* Allocate the buffer */
buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
dkdevid = (struct dk_devid *)buf;
/* Fill in the revision */
dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
/* Copy in the device id */
mutex_enter(SD_MUTEX(un));
bcopy(un->un_devid, &dkdevid->dkd_devid,
ddi_devid_sizeof(un->un_devid));
mutex_exit(SD_MUTEX(un));
/* Calculate the checksum */
chksum = 0;
ip = (uint_t *)dkdevid;
for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
i++) {
chksum ^= ip[i];
}
/* Fill-in checksum */
DKD_FORMCHKSUM(chksum, dkdevid);
/* Write the reserved sector */
status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
SD_PATH_DIRECT);
if (status != 0)
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
kmem_free(buf, un->un_sys_blocksize);
mutex_enter(SD_MUTEX(un));
return (status);
}
/*
* Function: sd_check_vpd_page_support
*
* Description: This routine sends an inquiry command with the EVPD bit set and
* a page code of 0x00 to the device. It is used to determine which
* vital product pages are available to find the devid. We are
* looking for pages 0x83 0x80 or 0xB1. If we return a negative 1,
* the device does not support that command.
*
* Arguments: un - driver soft state (unit) structure
*
* Return Code: 0 - success
* 1 - check condition
*
* Context: This routine can sleep.
*/
static int
sd_check_vpd_page_support(sd_ssc_t *ssc)
{
uchar_t *page_list = NULL;
uchar_t page_length = 0xff; /* Use max possible length */
uchar_t evpd = 0x01; /* Set the EVPD bit */
uchar_t page_code = 0x00; /* Supported VPD Pages */
int rval = 0;
int counter;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
mutex_exit(SD_MUTEX(un));
/*
* We'll set the page length to the maximum to save figuring it out
* with an additional call.
*/
page_list = kmem_zalloc(page_length, KM_SLEEP);
rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
page_code, NULL);
if (rval != 0)
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
mutex_enter(SD_MUTEX(un));
/*
* Now we must validate that the device accepted the command, as some
* drives do not support it. If the drive does support it, we will
* return 0, and the supported pages will be in un_vpd_page_mask. If
* not, we return -1.
*/
if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
/* Loop to find one of the 2 pages we need */
counter = 4; /* Supported pages start at byte 4, with 0x00 */
/*
* Pages are returned in ascending order, and 0x83 is what we
* are hoping for.
*/
while ((page_list[counter] <= 0xB1) &&
(counter <= (page_list[VPD_PAGE_LENGTH] +
VPD_HEAD_OFFSET))) {
/*
* Add 3 because page_list[3] is the number of
* pages minus 3
*/
switch (page_list[counter]) {
case 0x00:
un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
break;
case 0x80:
un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
break;
case 0x81:
un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
break;
case 0x82:
un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
break;
case 0x83:
un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
break;
case 0x86:
un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
break;
case 0xB0:
un->un_vpd_page_mask |= SD_VPD_BLK_LIMITS_PG;
break;
case 0xB1:
un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
break;
}
counter++;
}
} else {
rval = -1;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_check_vpd_page_support: This drive does not implement "
"VPD pages.\n");
}
kmem_free(page_list, page_length);
return (rval);
}
/*
* Function: sd_setup_pm
*
* Description: Initialize Power Management on the device
*
* Context: Kernel Thread
*/
static void
sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
{
uint_t log_page_size;
uchar_t *log_page_data;
int rval = 0;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
/*
* Since we are called from attach, holding a mutex for
* un is unnecessary. Because some of the routines called
* from here require SD_MUTEX to not be held, assert this
* right up front.
*/
ASSERT(!mutex_owned(SD_MUTEX(un)));
/*
* Since the sd device does not have the 'reg' property,
* cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
* The following code is to tell cpr that this device
* DOES need to be suspended and resumed.
*/
(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
"pm-hardware-state", "needs-suspend-resume");
/*
* This complies with the new power management framework
* for certain desktop machines. Create the pm_components
* property as a string array property.
* If un_f_pm_supported is TRUE, that means the disk
* attached HBA has set the "pm-capable" property and
* the value of this property is bigger than 0.
*/
if (un->un_f_pm_supported) {
/*
* not all devices have a motor, try it first.
* some devices may return ILLEGAL REQUEST, some
* will hang
* The following START_STOP_UNIT is used to check if target
* device has a motor.
*/
un->un_f_start_stop_supported = TRUE;
if (un->un_f_power_condition_supported) {
rval = sd_send_scsi_START_STOP_UNIT(ssc,
SD_POWER_CONDITION, SD_TARGET_ACTIVE,
SD_PATH_DIRECT);
if (rval != 0) {
un->un_f_power_condition_supported = FALSE;
}
}
if (!un->un_f_power_condition_supported) {
rval = sd_send_scsi_START_STOP_UNIT(ssc,
SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
}
if (rval != 0) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
un->un_f_start_stop_supported = FALSE;
}
/*
* create pm properties anyways otherwise the parent can't
* go to sleep
*/
un->un_f_pm_is_enabled = TRUE;
(void) sd_create_pm_components(devi, un);
/*
* If it claims that log sense is supported, check it out.
*/
if (un->un_f_log_sense_supported) {
rval = sd_log_page_supported(ssc,
START_STOP_CYCLE_PAGE);
if (rval == 1) {
/* Page found, use it. */
un->un_start_stop_cycle_page =
START_STOP_CYCLE_PAGE;
} else {
/*
* Page not found or log sense is not
* supported.
* Notice we do not check the old style
* START_STOP_CYCLE_VU_PAGE because this
* code path does not apply to old disks.
*/
un->un_f_log_sense_supported = FALSE;
un->un_f_pm_log_sense_smart = FALSE;
}
}
return;
}
/*
* For the disk whose attached HBA has not set the "pm-capable"
* property, check if it supports the power management.
*/
if (!un->un_f_log_sense_supported) {
un->un_power_level = SD_SPINDLE_ON;
un->un_f_pm_is_enabled = FALSE;
return;
}
rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
#ifdef SDDEBUG
if (sd_force_pm_supported) {
/* Force a successful result */
rval = 1;
}
#endif
/*
* If the start-stop cycle counter log page is not supported
* or if the pm-capable property is set to be false (0),
* then we should not create the pm_components property.
*/
if (rval == -1) {
/*
* Error.
* Reading log sense failed, most likely this is
* an older drive that does not support log sense.
* If this fails auto-pm is not supported.
*/
un->un_power_level = SD_SPINDLE_ON;
un->un_f_pm_is_enabled = FALSE;
} else if (rval == 0) {
/*
* Page not found.
* The start stop cycle counter is implemented as page
* START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
* newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
*/
if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
/*
* Page found, use this one.
*/
un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
un->un_f_pm_is_enabled = TRUE;
} else {
/*
* Error or page not found.
* auto-pm is not supported for this device.
*/
un->un_power_level = SD_SPINDLE_ON;
un->un_f_pm_is_enabled = FALSE;
}
} else {
/*
* Page found, use it.
*/
un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
un->un_f_pm_is_enabled = TRUE;
}
if (un->un_f_pm_is_enabled == TRUE) {
log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
log_page_size, un->un_start_stop_cycle_page,
0x01, 0, SD_PATH_DIRECT);
if (rval != 0) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
#ifdef SDDEBUG
if (sd_force_pm_supported) {
/* Force a successful result */
rval = 0;
}
#endif
/*
* If the Log sense for Page( Start/stop cycle counter page)
* succeeds, then power management is supported and we can
* enable auto-pm.
*/
if (rval == 0) {
(void) sd_create_pm_components(devi, un);
} else {
un->un_power_level = SD_SPINDLE_ON;
un->un_f_pm_is_enabled = FALSE;
}
kmem_free(log_page_data, log_page_size);
}
}
/*
* Function: sd_create_pm_components
*
* Description: Initialize PM property.
*
* Context: Kernel thread context
*/
static void
sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
{
ASSERT(!mutex_owned(SD_MUTEX(un)));
if (un->un_f_power_condition_supported) {
if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
"pm-components", sd_pwr_pc.pm_comp, 5)
!= DDI_PROP_SUCCESS) {
un->un_power_level = SD_SPINDLE_ACTIVE;
un->un_f_pm_is_enabled = FALSE;
return;
}
} else {
if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
"pm-components", sd_pwr_ss.pm_comp, 3)
!= DDI_PROP_SUCCESS) {
un->un_power_level = SD_SPINDLE_ON;
un->un_f_pm_is_enabled = FALSE;
return;
}
}
/*
* When components are initially created they are idle,
* power up any non-removables.
* Note: the return value of pm_raise_power can't be used
* for determining if PM should be enabled for this device.
* Even if you check the return values and remove this
* property created above, the PM framework will not honor the
* change after the first call to pm_raise_power. Hence,
* removal of that property does not help if pm_raise_power
* fails. In the case of removable media, the start/stop
* will fail if the media is not present.
*/
if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
mutex_enter(SD_MUTEX(un));
un->un_power_level = SD_PM_STATE_ACTIVE(un);
mutex_enter(&un->un_pm_mutex);
/* Set to on and not busy. */
un->un_pm_count = 0;
} else {
mutex_enter(SD_MUTEX(un));
un->un_power_level = SD_PM_STATE_STOPPED(un);
mutex_enter(&un->un_pm_mutex);
/* Set to off. */
un->un_pm_count = -1;
}
mutex_exit(&un->un_pm_mutex);
mutex_exit(SD_MUTEX(un));
}
/*
* Function: sd_ddi_suspend
*
* Description: Performs system power-down operations. This includes
* setting the drive state to indicate its suspended so
* that no new commands will be accepted. Also, wait for
* all commands that are in transport or queued to a timer
* for retry to complete. All timeout threads are cancelled.
*
* Return Code: DDI_FAILURE or DDI_SUCCESS
*
* Context: Kernel thread context
*/
static int
sd_ddi_suspend(dev_info_t *devi)
{
struct sd_lun *un;
clock_t wait_cmds_complete;
un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
if (un == NULL) {
return (DDI_FAILURE);
}
SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
mutex_enter(SD_MUTEX(un));
/* Return success if the device is already suspended. */
if (un->un_state == SD_STATE_SUSPENDED) {
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
"device already suspended, exiting\n");
return (DDI_SUCCESS);
}
/* Return failure if the device is being used by HA */
if (un->un_resvd_status &
(SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
"device in use by HA, exiting\n");
return (DDI_FAILURE);
}
/*
* Return failure if the device is in a resource wait
* or power changing state.
*/
if ((un->un_state == SD_STATE_RWAIT) ||
(un->un_state == SD_STATE_PM_CHANGING)) {
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
"device in resource wait state, exiting\n");
return (DDI_FAILURE);
}
un->un_save_state = un->un_last_state;
New_state(un, SD_STATE_SUSPENDED);
/*
* Wait for all commands that are in transport or queued to a timer
* for retry to complete.
*
* While waiting, no new commands will be accepted or sent because of
* the new state we set above.
*
* Wait till current operation has completed. If we are in the resource
* wait state (with an intr outstanding) then we need to wait till the
* intr completes and starts the next cmd. We want to wait for
* SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
*/
wait_cmds_complete = ddi_get_lbolt() +
(sd_wait_cmds_complete * drv_usectohz(1000000));
while (un->un_ncmds_in_transport != 0) {
/*
* Fail if commands do not finish in the specified time.
*/
if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
wait_cmds_complete) == -1) {
/*
* Undo the state changes made above. Everything
* must go back to it's original value.
*/
Restore_state(un);
un->un_last_state = un->un_save_state;
/* Wake up any threads that might be waiting. */
cv_broadcast(&un->un_suspend_cv);
mutex_exit(SD_MUTEX(un));
SD_ERROR(SD_LOG_IO_PM, un,
"sd_ddi_suspend: failed due to outstanding cmds\n");
SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
return (DDI_FAILURE);
}
}
/*
* Cancel SCSI watch thread and timeouts, if any are active
*/
if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
opaque_t temp_token = un->un_swr_token;
mutex_exit(SD_MUTEX(un));
scsi_watch_suspend(temp_token);
mutex_enter(SD_MUTEX(un));
}
if (un->un_reset_throttle_timeid != NULL) {
timeout_id_t temp_id = un->un_reset_throttle_timeid;
un->un_reset_throttle_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
if (un->un_dcvb_timeid != NULL) {
timeout_id_t temp_id = un->un_dcvb_timeid;
un->un_dcvb_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
mutex_enter(&un->un_pm_mutex);
if (un->un_pm_timeid != NULL) {
timeout_id_t temp_id = un->un_pm_timeid;
un->un_pm_timeid = NULL;
mutex_exit(&un->un_pm_mutex);
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
} else {
mutex_exit(&un->un_pm_mutex);
}
if (un->un_rmw_msg_timeid != NULL) {
timeout_id_t temp_id = un->un_rmw_msg_timeid;
un->un_rmw_msg_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
if (un->un_retry_timeid != NULL) {
timeout_id_t temp_id = un->un_retry_timeid;
un->un_retry_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
if (un->un_retry_bp != NULL) {
un->un_retry_bp->av_forw = un->un_waitq_headp;
un->un_waitq_headp = un->un_retry_bp;
if (un->un_waitq_tailp == NULL) {
un->un_waitq_tailp = un->un_retry_bp;
}
un->un_retry_bp = NULL;
un->un_retry_statp = NULL;
}
}
if (un->un_direct_priority_timeid != NULL) {
timeout_id_t temp_id = un->un_direct_priority_timeid;
un->un_direct_priority_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
if (un->un_f_is_fibre == TRUE) {
/*
* Remove callbacks for insert and remove events
*/
if (un->un_insert_event != NULL) {
mutex_exit(SD_MUTEX(un));
(void) ddi_remove_event_handler(un->un_insert_cb_id);
mutex_enter(SD_MUTEX(un));
un->un_insert_event = NULL;
}
if (un->un_remove_event != NULL) {
mutex_exit(SD_MUTEX(un));
(void) ddi_remove_event_handler(un->un_remove_cb_id);
mutex_enter(SD_MUTEX(un));
un->un_remove_event = NULL;
}
}
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
return (DDI_SUCCESS);
}
/*
* Function: sd_ddi_resume
*
* Description: Performs system power-up operations..
*
* Return Code: DDI_SUCCESS
* DDI_FAILURE
*
* Context: Kernel thread context
*/
static int
sd_ddi_resume(dev_info_t *devi)
{
struct sd_lun *un;
un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
if (un == NULL) {
return (DDI_FAILURE);
}
SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
mutex_enter(SD_MUTEX(un));
Restore_state(un);
/*
* Restore the state which was saved to give the
* the right state in un_last_state
*/
un->un_last_state = un->un_save_state;
/*
* Note: throttle comes back at full.
* Also note: this MUST be done before calling pm_raise_power
* otherwise the system can get hung in biowait. The scenario where
* this'll happen is under cpr suspend. Writing of the system
* state goes through sddump, which writes 0 to un_throttle. If
* writing the system state then fails, example if the partition is
* too small, then cpr attempts a resume. If throttle isn't restored
* from the saved value until after calling pm_raise_power then
* cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
* in biowait.
*/
un->un_throttle = un->un_saved_throttle;
/*
* The chance of failure is very rare as the only command done in power
* entry point is START command when you transition from 0->1 or
* unknown->1. Put it to SPINDLE ON state irrespective of the state at
* which suspend was done. Ignore the return value as the resume should
* not be failed. In the case of removable media the media need not be
* inserted and hence there is a chance that raise power will fail with
* media not present.
*/
if (un->un_f_attach_spinup) {
mutex_exit(SD_MUTEX(un));
(void) pm_raise_power(SD_DEVINFO(un), 0,
SD_PM_STATE_ACTIVE(un));
mutex_enter(SD_MUTEX(un));
}
/*
* Don't broadcast to the suspend cv and therefore possibly
* start I/O until after power has been restored.
*/
cv_broadcast(&un->un_suspend_cv);
cv_broadcast(&un->un_state_cv);
/* restart thread */
if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
scsi_watch_resume(un->un_swr_token);
}
/*
* Transport any pending commands to the target.
*
* If this is a low-activity device commands in queue will have to wait
* until new commands come in, which may take awhile. Also, we
* specifically don't check un_ncmds_in_transport because we know that
* there really are no commands in progress after the unit was
* suspended and we could have reached the throttle level, been
* suspended, and have no new commands coming in for awhile. Highly
* unlikely, but so is the low-activity disk scenario.
*/
ddi_xbuf_dispatch(un->un_xbuf_attr);
sd_start_cmds(un, NULL);
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
return (DDI_SUCCESS);
}
/*
* Function: sd_pm_state_change
*
* Description: Change the driver power state.
* Someone else is required to actually change the driver
* power level.
*
* Arguments: un - driver soft state (unit) structure
* level - the power level that is changed to
* flag - to decide how to change the power state
*
* Return Code: DDI_SUCCESS
*
* Context: Kernel thread context
*/
static int
sd_pm_state_change(struct sd_lun *un, int level, int flag)
{
ASSERT(un != NULL);
SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n");
ASSERT(!mutex_owned(SD_MUTEX(un)));
mutex_enter(SD_MUTEX(un));
if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) {
un->un_power_level = level;
ASSERT(!mutex_owned(&un->un_pm_mutex));
mutex_enter(&un->un_pm_mutex);
if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
un->un_pm_count++;
ASSERT(un->un_pm_count == 0);
}
mutex_exit(&un->un_pm_mutex);
} else {
/*
* Exit if power management is not enabled for this device,
* or if the device is being used by HA.
*/
if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
(SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_POWER, un,
"sd_pm_state_change: exiting\n");
return (DDI_FAILURE);
}
SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: "
"un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver);
/*
* See if the device is not busy, ie.:
* - we have no commands in the driver for this device
* - not waiting for resources
*/
if ((un->un_ncmds_in_driver == 0) &&
(un->un_state != SD_STATE_RWAIT)) {
/*
* The device is not busy, so it is OK to go to low
* power state. Indicate low power, but rely on someone
* else to actually change it.
*/
mutex_enter(&un->un_pm_mutex);
un->un_pm_count = -1;
mutex_exit(&un->un_pm_mutex);
un->un_power_level = level;
}
}
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n");
return (DDI_SUCCESS);
}
/*
* Function: sd_pm_idletimeout_handler
*
* Description: A timer routine that's active only while a device is busy.
* The purpose is to extend slightly the pm framework's busy
* view of the device to prevent busy/idle thrashing for
* back-to-back commands. Do this by comparing the current time
* to the time at which the last command completed and when the
* difference is greater than sd_pm_idletime, call
* pm_idle_component. In addition to indicating idle to the pm
* framework, update the chain type to again use the internal pm
* layers of the driver.
*
* Arguments: arg - driver soft state (unit) structure
*
* Context: Executes in a timeout(9F) thread context
*/
static void
sd_pm_idletimeout_handler(void *arg)
{
const hrtime_t idletime = sd_pm_idletime * NANOSEC;
struct sd_lun *un = arg;
/*
* Grab both mutexes, in the proper order, since we're accessing
* both PM and softstate variables.
*/
mutex_enter(SD_MUTEX(un));
mutex_enter(&un->un_pm_mutex);
/* if timeout id is NULL, we are being canceled via untimeout */
if (un->un_pm_idle_timeid == NULL) {
mutex_exit(&un->un_pm_mutex);
mutex_exit(SD_MUTEX(un));
return;
}
if (((gethrtime() - un->un_pm_idle_time) > idletime) &&
(un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
/*
* Update the chain types.
* This takes affect on the next new command received.
*/
if (un->un_f_non_devbsize_supported) {
un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
} else {
un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
}
un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
SD_TRACE(SD_LOG_IO_PM, un,
"sd_pm_idletimeout_handler: idling device\n");
(void) pm_idle_component(SD_DEVINFO(un), 0);
un->un_pm_idle_timeid = NULL;
} else {
un->un_pm_idle_timeid =
timeout(sd_pm_idletimeout_handler, un,
(drv_usectohz((clock_t)300000))); /* 300 ms. */
}
mutex_exit(&un->un_pm_mutex);
mutex_exit(SD_MUTEX(un));
}
/*
* Function: sd_pm_timeout_handler
*
* Description: Callback to tell framework we are idle.
*
* Context: timeout(9f) thread context.
*/
static void
sd_pm_timeout_handler(void *arg)
{
struct sd_lun *un = arg;
(void) pm_idle_component(SD_DEVINFO(un), 0);
mutex_enter(&un->un_pm_mutex);
un->un_pm_timeid = NULL;
mutex_exit(&un->un_pm_mutex);
}
/*
* Function: sdpower
*
* Description: PM entry point.
*
* Return Code: DDI_SUCCESS
* DDI_FAILURE
*
* Context: Kernel thread context
*/
static int
sdpower(dev_info_t *devi, int component, int level)
{
struct sd_lun *un;
int instance;
int rval = DDI_SUCCESS;
uint_t i, log_page_size, maxcycles, ncycles;
uchar_t *log_page_data;
int log_sense_page;
int medium_present;
time_t intvlp;
struct pm_trans_data sd_pm_tran_data;
uchar_t save_state = SD_STATE_NORMAL;
int sval;
uchar_t state_before_pm;
sd_ssc_t *ssc;
int last_power_level = SD_SPINDLE_UNINIT;
instance = ddi_get_instance(devi);
if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
!SD_PM_IS_LEVEL_VALID(un, level) || component != 0) {
return (DDI_FAILURE);
}
ssc = sd_ssc_init(un);
SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
mutex_enter(SD_MUTEX(un));
SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
un->un_ncmds_in_driver);
/*
* If un_ncmds_in_driver is non-zero it indicates commands are
* already being processed in the driver.
* At the same time somebody is requesting to go to a lower power
* that can't perform I/O, which can't happen, therefore we need to
* return failure.
*/
if ((!SD_PM_IS_IO_CAPABLE(un, level)) &&
(un->un_ncmds_in_driver != 0)) {
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un,
"sdpower: exit, device has queued cmds.\n");
goto sdpower_failed;
}
/*
* if it is OFFLINE that means the disk is completely dead
* in our case we have to put the disk in on or off by sending commands
* Of course that will fail anyway so return back here.
*
* Power changes to a device that's OFFLINE or SUSPENDED
* are not allowed.
*/
if ((un->un_state == SD_STATE_OFFLINE) ||
(un->un_state == SD_STATE_SUSPENDED)) {
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un,
"sdpower: exit, device is off-line.\n");
goto sdpower_failed;
}
/*
* Change the device's state to indicate it's power level
* is being changed. Do this to prevent a power off in the
* middle of commands, which is especially bad on devices
* that are really powered off instead of just spun down.
*/
state_before_pm = un->un_state;
un->un_state = SD_STATE_PM_CHANGING;
mutex_exit(SD_MUTEX(un));
/*
* If log sense command is not supported, bypass the
* following checking, otherwise, check the log sense
* information for this device.
*/
if (SD_PM_STOP_MOTOR_NEEDED(un, level) &&
un->un_f_log_sense_supported) {
/*
* Get the log sense information to understand whether the
* the powercycle counts have gone beyond the threshhold.
*/
log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
mutex_enter(SD_MUTEX(un));
log_sense_page = un->un_start_stop_cycle_page;
mutex_exit(SD_MUTEX(un));
rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
if (rval != 0) {
if (rval == EIO)
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
else
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
#ifdef SDDEBUG
if (sd_force_pm_supported) {
/* Force a successful result */
rval = 0;
}
#endif
if (rval != 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"Log Sense Failed\n");
kmem_free(log_page_data, log_page_size);
/* Cannot support power management on those drives */
/*
* On exit put the state back to it's original value
* and broadcast to anyone waiting for the power
* change completion.
*/
mutex_enter(SD_MUTEX(un));
un->un_state = state_before_pm;
cv_broadcast(&un->un_suspend_cv);
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un,
"sdpower: exit, Log Sense Failed.\n");
goto sdpower_failed;
}
/*
* From the page data - Convert the essential information to
* pm_trans_data
*/
maxcycles =
(log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
(log_page_data[0x1E] << 8) | log_page_data[0x1F];
ncycles =
(log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
(log_page_data[0x26] << 8) | log_page_data[0x27];
if (un->un_f_pm_log_sense_smart) {
sd_pm_tran_data.un.smart_count.allowed = maxcycles;
sd_pm_tran_data.un.smart_count.consumed = ncycles;
sd_pm_tran_data.un.smart_count.flag = 0;
sd_pm_tran_data.format = DC_SMART_FORMAT;
} else {
sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
log_page_data[8+i];
}
sd_pm_tran_data.un.scsi_cycles.flag = 0;
sd_pm_tran_data.format = DC_SCSI_FORMAT;
}
kmem_free(log_page_data, log_page_size);
/*
* Call pm_trans_check routine to get the Ok from
* the global policy
*/
rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
#ifdef SDDEBUG
if (sd_force_pm_supported) {
/* Force a successful result */
rval = 1;
}
#endif
switch (rval) {
case 0:
/*
* Not Ok to Power cycle or error in parameters passed
* Would have given the advised time to consider power
* cycle. Based on the new intvlp parameter we are
* supposed to pretend we are busy so that pm framework
* will never call our power entry point. Because of
* that install a timeout handler and wait for the
* recommended time to elapse so that power management
* can be effective again.
*
* To effect this behavior, call pm_busy_component to
* indicate to the framework this device is busy.
* By not adjusting un_pm_count the rest of PM in
* the driver will function normally, and independent
* of this but because the framework is told the device
* is busy it won't attempt powering down until it gets
* a matching idle. The timeout handler sends this.
* Note: sd_pm_entry can't be called here to do this
* because sdpower may have been called as a result
* of a call to pm_raise_power from within sd_pm_entry.
*
* If a timeout handler is already active then
* don't install another.
*/
mutex_enter(&un->un_pm_mutex);
if (un->un_pm_timeid == NULL) {
un->un_pm_timeid =
timeout(sd_pm_timeout_handler,
un, intvlp * drv_usectohz(1000000));
mutex_exit(&un->un_pm_mutex);
(void) pm_busy_component(SD_DEVINFO(un), 0);
} else {
mutex_exit(&un->un_pm_mutex);
}
/*
* On exit put the state back to its original value
* and broadcast to anyone waiting for the power
* change completion.
*/
mutex_enter(SD_MUTEX(un));
un->un_state = state_before_pm;
cv_broadcast(&un->un_suspend_cv);
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
"trans check Failed, not ok to power cycle.\n");
goto sdpower_failed;
case -1:
/*
* On exit put the state back to its original value
* and broadcast to anyone waiting for the power
* change completion.
*/
mutex_enter(SD_MUTEX(un));
un->un_state = state_before_pm;
cv_broadcast(&un->un_suspend_cv);
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un,
"sdpower: exit, trans check command Failed.\n");
goto sdpower_failed;
}
}
if (!SD_PM_IS_IO_CAPABLE(un, level)) {
/*
* Save the last state... if the STOP FAILS we need it
* for restoring
*/
mutex_enter(SD_MUTEX(un));
save_state = un->un_last_state;
last_power_level = un->un_power_level;
/*
* There must not be any cmds. getting processed
* in the driver when we get here. Power to the
* device is potentially going off.
*/
ASSERT(un->un_ncmds_in_driver == 0);
mutex_exit(SD_MUTEX(un));
/*
* For now PM suspend the device completely before spindle is
* turned off
*/
if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE))
== DDI_FAILURE) {
/*
* On exit put the state back to its original value
* and broadcast to anyone waiting for the power
* change completion.
*/
mutex_enter(SD_MUTEX(un));
un->un_state = state_before_pm;
un->un_power_level = last_power_level;
cv_broadcast(&un->un_suspend_cv);
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un,
"sdpower: exit, PM suspend Failed.\n");
goto sdpower_failed;
}
}
/*
* The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
* close, or strategy. Dump no long uses this routine, it uses it's
* own code so it can be done in polled mode.
*/
medium_present = TRUE;
/*
* When powering up, issue a TUR in case the device is at unit
* attention. Don't do retries. Bypass the PM layer, otherwise
* a deadlock on un_pm_busy_cv will occur.
*/
if (SD_PM_IS_IO_CAPABLE(un, level)) {
sval = sd_send_scsi_TEST_UNIT_READY(ssc,
SD_DONT_RETRY_TUR | SD_BYPASS_PM);
if (sval != 0)
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
if (un->un_f_power_condition_supported) {
char *pm_condition_name[] = {"STOPPED", "STANDBY",
"IDLE", "ACTIVE"};
SD_TRACE(SD_LOG_IO_PM, un,
"sdpower: sending \'%s\' power condition",
pm_condition_name[level]);
sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
sd_pl2pc[level], SD_PATH_DIRECT);
} else {
SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
((level == SD_SPINDLE_ON) ? "START" : "STOP"));
sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
((level == SD_SPINDLE_ON) ? SD_TARGET_START :
SD_TARGET_STOP), SD_PATH_DIRECT);
}
if (sval != 0) {
if (sval == EIO)
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
else
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
/* Command failed, check for media present. */
if ((sval == ENXIO) && un->un_f_has_removable_media) {
medium_present = FALSE;
}
/*
* The conditions of interest here are:
* if a spindle off with media present fails,
* then restore the state and return an error.
* else if a spindle on fails,
* then return an error (there's no state to restore).
* In all other cases we setup for the new state
* and return success.
*/
if (!SD_PM_IS_IO_CAPABLE(un, level)) {
if ((medium_present == TRUE) && (sval != 0)) {
/* The stop command from above failed */
rval = DDI_FAILURE;
/*
* The stop command failed, and we have media
* present. Put the level back by calling the
* sd_pm_resume() and set the state back to
* it's previous value.
*/
(void) sd_pm_state_change(un, last_power_level,
SD_PM_STATE_ROLLBACK);
mutex_enter(SD_MUTEX(un));
un->un_last_state = save_state;
mutex_exit(SD_MUTEX(un));
} else if (un->un_f_monitor_media_state) {
/*
* The stop command from above succeeded.
* Terminate watch thread in case of removable media
* devices going into low power state. This is as per
* the requirements of pm framework, otherwise commands
* will be generated for the device (through watch
* thread), even when the device is in low power state.
*/
mutex_enter(SD_MUTEX(un));
un->un_f_watcht_stopped = FALSE;
if (un->un_swr_token != NULL) {
opaque_t temp_token = un->un_swr_token;
un->un_f_watcht_stopped = TRUE;
un->un_swr_token = NULL;
mutex_exit(SD_MUTEX(un));
(void) scsi_watch_request_terminate(temp_token,
SCSI_WATCH_TERMINATE_ALL_WAIT);
} else {
mutex_exit(SD_MUTEX(un));
}
}
} else {
/*
* The level requested is I/O capable.
* Legacy behavior: return success on a failed spinup
* if there is no media in the drive.
* Do this by looking at medium_present here.
*/
if ((sval != 0) && medium_present) {
/* The start command from above failed */
rval = DDI_FAILURE;
} else {
/*
* The start command from above succeeded
* PM resume the devices now that we have
* started the disks
*/
(void) sd_pm_state_change(un, level,
SD_PM_STATE_CHANGE);
/*
* Resume the watch thread since it was suspended
* when the device went into low power mode.
*/
if (un->un_f_monitor_media_state) {
mutex_enter(SD_MUTEX(un));
if (un->un_f_watcht_stopped == TRUE) {
opaque_t temp_token;
un->un_f_watcht_stopped = FALSE;
mutex_exit(SD_MUTEX(un));
temp_token =
sd_watch_request_submit(un);
mutex_enter(SD_MUTEX(un));
un->un_swr_token = temp_token;
}
mutex_exit(SD_MUTEX(un));
}
}
}
/*
* On exit put the state back to its original value
* and broadcast to anyone waiting for the power
* change completion.
*/
mutex_enter(SD_MUTEX(un));
un->un_state = state_before_pm;
cv_broadcast(&un->un_suspend_cv);
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
sd_ssc_fini(ssc);
return (rval);
sdpower_failed:
sd_ssc_fini(ssc);
return (DDI_FAILURE);
}
/*
* Function: sdattach
*
* Description: Driver's attach(9e) entry point function.
*
* Arguments: devi - opaque device info handle
* cmd - attach type
*
* Return Code: DDI_SUCCESS
* DDI_FAILURE
*
* Context: Kernel thread context
*/
static int
sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
switch (cmd) {
case DDI_ATTACH:
return (sd_unit_attach(devi));
case DDI_RESUME:
return (sd_ddi_resume(devi));
default:
break;
}
return (DDI_FAILURE);
}
/*
* Function: sddetach
*
* Description: Driver's detach(9E) entry point function.
*
* Arguments: devi - opaque device info handle
* cmd - detach type
*
* Return Code: DDI_SUCCESS
* DDI_FAILURE
*
* Context: Kernel thread context
*/
static int
sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
{
switch (cmd) {
case DDI_DETACH:
return (sd_unit_detach(devi));
case DDI_SUSPEND:
return (sd_ddi_suspend(devi));
default:
break;
}
return (DDI_FAILURE);
}
/*
* Function: sd_sync_with_callback
*
* Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
* state while the callback routine is active.
*
* Arguments: un: softstate structure for the instance
*
* Context: Kernel thread context
*/
static void
sd_sync_with_callback(struct sd_lun *un)
{
ASSERT(un != NULL);
mutex_enter(SD_MUTEX(un));
ASSERT(un->un_in_callback >= 0);
while (un->un_in_callback > 0) {
mutex_exit(SD_MUTEX(un));
delay(2);
mutex_enter(SD_MUTEX(un));
}
mutex_exit(SD_MUTEX(un));
}
/*
* Function: sd_unit_attach
*
* Description: Performs DDI_ATTACH processing for sdattach(). Allocates
* the soft state structure for the device and performs
* all necessary structure and device initializations.
*
* Arguments: devi: the system's dev_info_t for the device.
*
* Return Code: DDI_SUCCESS if attach is successful.
* DDI_FAILURE if any part of the attach fails.
*
* Context: Called at attach(9e) time for the DDI_ATTACH flag.
* Kernel thread context only. Can sleep.
*/
static int
sd_unit_attach(dev_info_t *devi)
{
struct scsi_device *devp;
struct sd_lun *un;
char *variantp;
char name_str[48];
int reservation_flag = SD_TARGET_IS_UNRESERVED;
int instance;
int rval;
int wc_enabled;
int wc_changeable;
int tgt;
uint64_t capacity;
uint_t lbasize = 0;
dev_info_t *pdip = ddi_get_parent(devi);
int offbyone = 0;
int geom_label_valid = 0;
sd_ssc_t *ssc;
int status;
struct sd_fm_internal *sfip = NULL;
int max_xfer_size;
/*
* Retrieve the target driver's private data area. This was set
* up by the HBA.
*/
devp = ddi_get_driver_private(devi);
/*
* Retrieve the target ID of the device.
*/
tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
SCSI_ADDR_PROP_TARGET, -1);
/*
* Since we have no idea what state things were left in by the last
* user of the device, set up some 'default' settings, ie. turn 'em
* off. The scsi_ifsetcap calls force re-negotiations with the drive.
* Do this before the scsi_probe, which sends an inquiry.
* This is a fix for bug (4430280).
* Of special importance is wide-xfer. The drive could have been left
* in wide transfer mode by the last driver to communicate with it,
* this includes us. If that's the case, and if the following is not
* setup properly or we don't re-negotiate with the drive prior to
* transferring data to/from the drive, it causes bus parity errors,
* data overruns, and unexpected interrupts. This first occurred when
* the fix for bug (4378686) was made.
*/
(void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
(void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
(void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
/*
* Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
* on a target. Setting it per lun instance actually sets the
* capability of this target, which affects those luns already
* attached on the same target. So during attach, we can only disable
* this capability only when no other lun has been attached on this
* target. By doing this, we assume a target has the same tagged-qing
* capability for every lun. The condition can be removed when HBA
* is changed to support per lun based tagged-qing capability.
*/
if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
(void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
}
/*
* Use scsi_probe() to issue an INQUIRY command to the device.
* This call will allocate and fill in the scsi_inquiry structure
* and point the sd_inq member of the scsi_device structure to it.
* If the attach succeeds, then this memory will not be de-allocated
* (via scsi_unprobe()) until the instance is detached.
*/
if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
goto probe_failed;
}
/*
* Check the device type as specified in the inquiry data and
* claim it if it is of a type that we support.
*/
switch (devp->sd_inq->inq_dtype) {
case DTYPE_DIRECT:
break;
case DTYPE_RODIRECT:
break;
case DTYPE_OPTICAL:
break;
case DTYPE_NOTPRESENT:
default:
/* Unsupported device type; fail the attach. */
goto probe_failed;
}
/*
* Allocate the soft state structure for this unit.
*
* We rely upon this memory being set to all zeroes by
* ddi_soft_state_zalloc(). We assume that any member of the
* soft state structure that is not explicitly initialized by
* this routine will have a value of zero.
*/
instance = ddi_get_instance(devp->sd_dev);
if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
goto probe_failed;
}
/*
* Retrieve a pointer to the newly-allocated soft state.
*
* This should NEVER fail if the ddi_soft_state_zalloc() call above
* was successful, unless something has gone horribly wrong and the
* ddi's soft state internals are corrupt (in which case it is
* probably better to halt here than just fail the attach....)
*/
if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
panic("sd_unit_attach: NULL soft state on instance:0x%x",
instance);
/*NOTREACHED*/
}
/*
* Link the back ptr of the driver soft state to the scsi_device
* struct for this lun.
* Save a pointer to the softstate in the driver-private area of
* the scsi_device struct.
* Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
* we first set un->un_sd below.
*/
un->un_sd = devp;
devp->sd_private = (opaque_t)un;
/*
* The following must be after devp is stored in the soft state struct.
*/
#ifdef SDDEBUG
SD_TRACE(SD_LOG_ATTACH_DETACH, un,
"%s_unit_attach: un:0x%p instance:%d\n",
ddi_driver_name(devi), un, instance);
#endif
/*
* Set up the device type and node type (for the minor nodes).
* By default we assume that the device can at least support the
* Common Command Set. Call it a CD-ROM if it reports itself
* as a RODIRECT device.
*/
switch (devp->sd_inq->inq_dtype) {
case DTYPE_RODIRECT:
un->un_node_type = DDI_NT_CD_CHAN;
un->un_ctype = CTYPE_CDROM;
break;
case DTYPE_OPTICAL:
un->un_node_type = DDI_NT_BLOCK_CHAN;
un->un_ctype = CTYPE_ROD;
break;
default:
un->un_node_type = DDI_NT_BLOCK_CHAN;
un->un_ctype = CTYPE_CCS;
break;
}
/*
* Try to read the interconnect type from the HBA.
*
* Note: This driver is currently compiled as two binaries, a parallel
* scsi version (sd) and a fibre channel version (ssd). All functional
* differences are determined at compile time. In the future a single
* binary will be provided and the interconnect type will be used to
* differentiate between fibre and parallel scsi behaviors. At that time
* it will be necessary for all fibre channel HBAs to support this
* property.
*
* set un_f_is_fiber to TRUE ( default fiber )
*/
un->un_f_is_fibre = TRUE;
switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
case INTERCONNECT_SSA:
un->un_interconnect_type = SD_INTERCONNECT_SSA;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
break;
case INTERCONNECT_PARALLEL:
un->un_f_is_fibre = FALSE;
un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
break;
case INTERCONNECT_SAS:
un->un_f_is_fibre = FALSE;
un->un_interconnect_type = SD_INTERCONNECT_SAS;
un->un_node_type = DDI_NT_BLOCK_SAS;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un);
break;
case INTERCONNECT_SATA:
un->un_f_is_fibre = FALSE;
un->un_interconnect_type = SD_INTERCONNECT_SATA;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
break;
case INTERCONNECT_FIBRE:
un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
break;
case INTERCONNECT_FABRIC:
un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
un->un_node_type = DDI_NT_BLOCK_FABRIC;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
break;
default:
/*
* The HBA does not support the "interconnect-type" property
* (or did not provide a recognized type).
*
* Note: This will be obsoleted when a single fibre channel
* and parallel scsi driver is delivered. In the meantime the
* interconnect type will be set to the platform default.If that
* type is not parallel SCSI, it means that we should be
* assuming "ssd" semantics. However, here this also means that
* the FC HBA is not supporting the "interconnect-type" property
* like we expect it to, so log this occurrence.
*/
un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
if (!SD_IS_PARALLEL_SCSI(un)) {
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p Assuming "
"INTERCONNECT_FIBRE\n", un);
} else {
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p Assuming "
"INTERCONNECT_PARALLEL\n", un);
un->un_f_is_fibre = FALSE;
}
break;
}
if (un->un_f_is_fibre == TRUE) {
if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
SCSI_VERSION_3) {
switch (un->un_interconnect_type) {
case SD_INTERCONNECT_FIBRE:
case SD_INTERCONNECT_SSA:
un->un_node_type = DDI_NT_BLOCK_WWN;
break;
default:
break;
}
}
}
/*
* Initialize the Request Sense command for the target
*/
if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
goto alloc_rqs_failed;
}
/*
* Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
* with separate binary for sd and ssd.
*
* x86 has 1 binary, un_retry_count is set base on connection type.
* The hardcoded values will go away when Sparc uses 1 binary
* for sd and ssd. This hardcoded values need to match
* SD_RETRY_COUNT in sddef.h
* The value used is base on interconnect type.
* fibre = 3, parallel = 5
*/
un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
/*
* Set the per disk retry count to the default number of retries
* for disks and CDROMs. This value can be overridden by the
* disk property list or an entry in sd.conf.
*/
un->un_notready_retry_count =
ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
: DISK_NOT_READY_RETRY_COUNT(un);
/*
* Set the busy retry count to the default value of un_retry_count.
* This can be overridden by entries in sd.conf or the device
* config table.
*/
un->un_busy_retry_count = un->un_retry_count;
/*
* Init the reset threshold for retries. This number determines
* how many retries must be performed before a reset can be issued
* (for certain error conditions). This can be overridden by entries
* in sd.conf or the device config table.
*/
un->un_reset_retry_count = (un->un_retry_count / 2);
/*
* Set the victim_retry_count to the default un_retry_count
*/
un->un_victim_retry_count = (2 * un->un_retry_count);
/*
* Set the reservation release timeout to the default value of
* 5 seconds. This can be overridden by entries in ssd.conf or the
* device config table.
*/
un->un_reserve_release_time = 5;
/*
* Set up the default maximum transfer size. Note that this may
* get updated later in the attach, when setting up default wide
* operations for disks.
*/
un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
un->un_partial_dma_supported = 1;
/*
* Get "allow bus device reset" property (defaults to "enabled" if
* the property was not defined). This is to disable bus resets for
* certain kinds of error recovery. Note: In the future when a run-time
* fibre check is available the soft state flag should default to
* enabled.
*/
if (un->un_f_is_fibre == TRUE) {
un->un_f_allow_bus_device_reset = TRUE;
} else {
if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
"allow-bus-device-reset", 1) != 0) {
un->un_f_allow_bus_device_reset = TRUE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p Bus device reset "
"enabled\n", un);
} else {
un->un_f_allow_bus_device_reset = FALSE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p Bus device reset "
"disabled\n", un);
}
}
/*
* Check if this is an ATAPI device. ATAPI devices use Group 1
* Read/Write commands and Group 2 Mode Sense/Select commands.
*
* Note: The "obsolete" way of doing this is to check for the "atapi"
* property. The new "variant" property with a value of "atapi" has been
* introduced so that future 'variants' of standard SCSI behavior (like
* atapi) could be specified by the underlying HBA drivers by supplying
* a new value for the "variant" property, instead of having to define a
* new property.
*/
if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
un->un_f_cfg_is_atapi = TRUE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p Atapi device\n", un);
}
if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
&variantp) == DDI_PROP_SUCCESS) {
if (strcmp(variantp, "atapi") == 0) {
un->un_f_cfg_is_atapi = TRUE;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p Atapi device\n", un);
}
ddi_prop_free(variantp);
}
un->un_cmd_timeout = SD_IO_TIME;
un->un_busy_timeout = SD_BSY_TIMEOUT;
/* Info on current states, statuses, etc. (Updated frequently) */
un->un_state = SD_STATE_NORMAL;
un->un_last_state = SD_STATE_NORMAL;
/* Control & status info for command throttling */
un->un_throttle = sd_max_throttle;
un->un_saved_throttle = sd_max_throttle;
un->un_min_throttle = sd_min_throttle;
if (un->un_f_is_fibre == TRUE) {
un->un_f_use_adaptive_throttle = TRUE;
} else {
un->un_f_use_adaptive_throttle = FALSE;
}
/* Removable media support. */
cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
un->un_mediastate = DKIO_NONE;
un->un_specified_mediastate = DKIO_NONE;
/* CVs for suspend/resume (PM or DR) */
cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL);
cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
/* Power management support. */
un->un_power_level = SD_SPINDLE_UNINIT;
cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL);
un->un_f_wcc_inprog = 0;
/*
* The conf file entry and softstate variable is a forceful override,
* meaning a non-zero value must be entered to change the default.
*/
un->un_f_disksort_disabled = FALSE;
un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
un->un_f_enable_rmw = FALSE;
/*
* GET EVENT STATUS NOTIFICATION media polling enabled by default, but
* can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
*/
un->un_f_mmc_gesn_polling = TRUE;
/*
* physical sector size defaults to DEV_BSIZE currently. We can
* override this value via the driver configuration file so we must
* set it before calling sd_read_unit_properties().
*/
un->un_phy_blocksize = DEV_BSIZE;
/*
* Retrieve the properties from the static driver table or the driver
* configuration file (.conf) for this unit and update the soft state
* for the device as needed for the indicated properties.
* Note: the property configuration needs to occur here as some of the
* following routines may have dependencies on soft state flags set
* as part of the driver property configuration.
*/
sd_read_unit_properties(un);
SD_TRACE(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p property configuration complete.\n", un);
/*
* Only if a device has "hotpluggable" property, it is
* treated as hotpluggable device. Otherwise, it is
* regarded as non-hotpluggable one.
*/
if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
-1) != -1) {
un->un_f_is_hotpluggable = TRUE;
}
/*
* set unit's attributes(flags) according to "hotpluggable" and
* RMB bit in INQUIRY data.
*/
sd_set_unit_attributes(un, devi);
/*
* By default, we mark the capacity, lbasize, and geometry
* as invalid. Only if we successfully read a valid capacity
* will we update the un_blockcount and un_tgt_blocksize with the
* valid values (the geometry will be validated later).
*/
un->un_f_blockcount_is_valid = FALSE;
un->un_f_tgt_blocksize_is_valid = FALSE;
/*
* Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
* otherwise.
*/
un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE;
un->un_blockcount = 0;
/*
* Set up the per-instance info needed to determine the correct
* CDBs and other info for issuing commands to the target.
*/
sd_init_cdb_limits(un);
/*
* Set up the IO chains to use, based upon the target type.
*/
if (un->un_f_non_devbsize_supported) {
un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
} else {
un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
}
un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit,
ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
if (ISCD(un)) {
un->un_additional_codes = sd_additional_codes;
} else {
un->un_additional_codes = NULL;
}
/*
* Create the kstats here so they can be available for attach-time
* routines that send commands to the unit (either polled or via
* sd_send_scsi_cmd).
*
* Note: This is a critical sequence that needs to be maintained:
* 1) Instantiate the kstats here, before any routines using the
* iopath (i.e. sd_send_scsi_cmd).
* 2) Instantiate and initialize the partition stats
* (sd_set_pstats).
* 3) Initialize the error stats (sd_set_errstats), following
* sd_validate_geometry(),sd_register_devid(),
* and sd_cache_control().
*/
un->un_stats = kstat_create(sd_label, instance,
NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
if (un->un_stats != NULL) {
un->un_stats->ks_lock = SD_MUTEX(un);
kstat_install(un->un_stats);
}
SD_TRACE(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p un_stats created\n", un);
un->un_unmapstats_ks = kstat_create(sd_label, instance, "unmapstats",
"misc", KSTAT_TYPE_NAMED, sizeof (*un->un_unmapstats) /
sizeof (kstat_named_t), 0);
if (un->un_unmapstats_ks) {
un->un_unmapstats = un->un_unmapstats_ks->ks_data;
kstat_named_init(&un->un_unmapstats->us_cmds,
"commands", KSTAT_DATA_UINT64);
kstat_named_init(&un->un_unmapstats->us_errs,
"errors", KSTAT_DATA_UINT64);
kstat_named_init(&un->un_unmapstats->us_extents,
"extents", KSTAT_DATA_UINT64);
kstat_named_init(&un->un_unmapstats->us_bytes,
"bytes", KSTAT_DATA_UINT64);
kstat_install(un->un_unmapstats_ks);
} else {
cmn_err(CE_NOTE, "!Cannot create unmap kstats for disk %d",
instance);
}
sd_create_errstats(un, instance);
if (un->un_errstats == NULL) {
goto create_errstats_failed;
}
SD_TRACE(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p errstats created\n", un);
/*
* The following if/else code was relocated here from below as part
* of the fix for bug (4430280). However with the default setup added
* on entry to this routine, it's no longer absolutely necessary for
* this to be before the call to sd_spin_up_unit.
*/
if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
(devp->sd_inq->inq_ansi == 5)) &&
devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
/*
* If tagged queueing is supported by the target
* and by the host adapter then we will enable it
*/
un->un_tagflags = 0;
if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
(un->un_f_arq_enabled == TRUE)) {
if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
1, 1) == 1) {
un->un_tagflags = FLAG_STAG;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p tag queueing "
"enabled\n", un);
} else if (scsi_ifgetcap(SD_ADDRESS(un),
"untagged-qing", 0) == 1) {
un->un_f_opt_queueing = TRUE;
un->un_saved_throttle = un->un_throttle =
min(un->un_throttle, 3);
} else {
un->un_f_opt_queueing = FALSE;
un->un_saved_throttle = un->un_throttle = 1;
}
} else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
== 1) && (un->un_f_arq_enabled == TRUE)) {
/* The Host Adapter supports internal queueing. */
un->un_f_opt_queueing = TRUE;
un->un_saved_throttle = un->un_throttle =
min(un->un_throttle, 3);
} else {
un->un_f_opt_queueing = FALSE;
un->un_saved_throttle = un->un_throttle = 1;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p no tag queueing\n", un);
}
/*
* Enable large transfers for SATA/SAS drives
*/
if (SD_IS_SERIAL(un)) {
un->un_max_xfer_size =
ddi_getprop(DDI_DEV_T_ANY, devi, 0,
sd_max_xfer_size, SD_MAX_XFER_SIZE);
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p max transfer "
"size=0x%x\n", un, un->un_max_xfer_size);
}
/* Setup or tear down default wide operations for disks */
/*
* Note: Legacy: it may be possible for both "sd_max_xfer_size"
* and "ssd_max_xfer_size" to exist simultaneously on the same
* system and be set to different values. In the future this
* code may need to be updated when the ssd module is
* obsoleted and removed from the system. (4299588)
*/
if (SD_IS_PARALLEL_SCSI(un) &&
(devp->sd_inq->inq_rdf == RDF_SCSI2) &&
(devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
1, 1) == 1) {
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p Wide Transfer "
"enabled\n", un);
}
/*
* If tagged queuing has also been enabled, then
* enable large xfers
*/
if (un->un_saved_throttle == sd_max_throttle) {
un->un_max_xfer_size =
ddi_getprop(DDI_DEV_T_ANY, devi, 0,
sd_max_xfer_size, SD_MAX_XFER_SIZE);
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p max transfer "
"size=0x%x\n", un, un->un_max_xfer_size);
}
} else {
if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
0, 1) == 1) {
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p "
"Wide Transfer disabled\n", un);
}
}
} else {
un->un_tagflags = FLAG_STAG;
un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
}
/*
* If this target supports LUN reset, try to enable it.
*/
if (un->un_f_lun_reset_enabled) {
if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
"un:0x%p lun_reset capability set\n", un);
} else {
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
"un:0x%p lun-reset capability not set\n", un);
}
}
/*
* Adjust the maximum transfer size. This is to fix
* the problem of partial DMA support on SPARC. Some
* HBA driver, like aac, has very small dma_attr_maxxfer
* size, which requires partial DMA support on SPARC.
* In the future the SPARC pci nexus driver may solve
* the problem instead of this fix.
*/
max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
/* We need DMA partial even on sparc to ensure sddump() works */
un->un_max_xfer_size = max_xfer_size;
if (un->un_partial_dma_supported == 0)
un->un_partial_dma_supported = 1;
}
if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
un->un_max_xfer_size) == 1) {
un->un_buf_breakup_supported = 1;
SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
"un:0x%p Buf breakup enabled\n", un);
}
}
/*
* Set PKT_DMA_PARTIAL flag.
*/
if (un->un_partial_dma_supported == 1) {
un->un_pkt_flags = PKT_DMA_PARTIAL;
} else {
un->un_pkt_flags = 0;
}
/* Initialize sd_ssc_t for internal uscsi commands */
ssc = sd_ssc_init(un);
scsi_fm_init(devp);
/*
* Allocate memory for SCSI FMA stuffs.
*/
un->un_fm_private =
kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
sfip = (struct sd_fm_internal *)un->un_fm_private;
sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
sfip->fm_ssc.ssc_un = un;
if (ISCD(un) ||
un->un_f_has_removable_media ||
devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
/*
* We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
* Their log are unchanged.
*/
sfip->fm_log_level = SD_FM_LOG_NSUP;
} else {
/*
* If enter here, it should be non-CDROM and FM-capable
* device, and it will not keep the old scsi_log as before
* in /var/adm/messages. However, the property
* "fm-scsi-log" will control whether the FM telemetry will
* be logged in /var/adm/messages.
*/
int fm_scsi_log;
fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
if (fm_scsi_log)
sfip->fm_log_level = SD_FM_LOG_EREPORT;
else
sfip->fm_log_level = SD_FM_LOG_SILENT;
}
/*
* At this point in the attach, we have enough info in the
* soft state to be able to issue commands to the target.
*
* All command paths used below MUST issue their commands as
* SD_PATH_DIRECT. This is important as intermediate layers
* are not all initialized yet (such as PM).
*/
/*
* Send a TEST UNIT READY command to the device. This should clear
* any outstanding UNIT ATTENTION that may be present.
*
* Note: Don't check for success, just track if there is a reservation,
* this is a throw away command to clear any unit attentions.
*
* Note: This MUST be the first command issued to the target during
* attach to ensure power on UNIT ATTENTIONS are cleared.
* Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
* with attempts at spinning up a device with no media.
*/
status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
if (status != 0) {
if (status == EACCES)
reservation_flag = SD_TARGET_IS_RESERVED;
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
/*
* If the device is NOT a removable media device, attempt to spin
* it up (using the START_STOP_UNIT command) and read its capacity
* (using the READ CAPACITY command). Note, however, that either
* of these could fail and in some cases we would continue with
* the attach despite the failure (see below).
*/
if (un->un_f_descr_format_supported) {
switch (sd_spin_up_unit(ssc)) {
case 0:
/*
* Spin-up was successful; now try to read the
* capacity. If successful then save the results
* and mark the capacity & lbasize as valid.
*/
SD_TRACE(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p spin-up successful\n", un);
status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
&lbasize, SD_PATH_DIRECT);
switch (status) {
case 0: {
if (capacity > DK_MAX_BLOCKS) {
if ((capacity + 1) >
SD_GROUP1_MAX_ADDRESS) {
/*
* Enable descriptor format
* sense data so that we can
* get 64 bit sense data
* fields.
*/
sd_enable_descr_sense(ssc);
}
}
/*
* Here it's not necessary to check the case:
* the capacity of the device is bigger than
* what the max hba cdb can support. Because
* sd_send_scsi_READ_CAPACITY will retrieve
* the capacity by sending USCSI command, which
* is constrained by the max hba cdb. Actually,
* sd_send_scsi_READ_CAPACITY will return
* EINVAL when using bigger cdb than required
* cdb length. Will handle this case in
* "case EINVAL".
*/
/*
* The following relies on
* sd_send_scsi_READ_CAPACITY never
* returning 0 for capacity and/or lbasize.
*/
sd_update_block_info(un, lbasize, capacity);
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p capacity = %ld "
"blocks; lbasize= %ld.\n", un,
un->un_blockcount, un->un_tgt_blocksize);
break;
}
case EINVAL:
/*
* In the case where the max-cdb-length property
* is smaller than the required CDB length for
* a SCSI device, a target driver can fail to
* attach to that device.
*/
scsi_log(SD_DEVINFO(un),
sd_label, CE_WARN,
"disk capacity is too large "
"for current cdb length");
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
goto spinup_failed;
case EACCES:
/*
* Should never get here if the spin-up
* succeeded, but code it in anyway.
* From here, just continue with the attach...
*/
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p "
"sd_send_scsi_READ_CAPACITY "
"returned reservation conflict\n", un);
reservation_flag = SD_TARGET_IS_RESERVED;
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
break;
default:
/*
* Likewise, should never get here if the
* spin-up succeeded. Just continue with
* the attach...
*/
if (status == EIO)
sd_ssc_assessment(ssc,
SD_FMT_STATUS_CHECK);
else
sd_ssc_assessment(ssc,
SD_FMT_IGNORE);
break;
}
break;
case EACCES:
/*
* Device is reserved by another host. In this case
* we could not spin it up or read the capacity, but
* we continue with the attach anyway.
*/
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p spin-up reservation "
"conflict.\n", un);
reservation_flag = SD_TARGET_IS_RESERVED;
break;
default:
/* Fail the attach if the spin-up failed. */
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p spin-up failed.", un);
goto spinup_failed;
}
}
/*
* Check to see if this is a MMC drive
*/
if (ISCD(un)) {
sd_set_mmc_caps(ssc);
}
/*
* Add a zero-length attribute to tell the world we support
* kernel ioctls (for layered drivers)
*/
(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
DDI_KERNEL_IOCTL, NULL, 0);
/*
* Add a boolean property to tell the world we support
* the B_FAILFAST flag (for layered drivers)
*/
(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
"ddi-failfast-supported", NULL, 0);
/*
* Initialize power management
*/
mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
sd_setup_pm(ssc, devi);
if (un->un_f_pm_is_enabled == FALSE) {
/*
* For performance, point to a jump table that does
* not include pm.
* The direct and priority chains don't change with PM.
*
* Note: this is currently done based on individual device
* capabilities. When an interface for determining system
* power enabled state becomes available, or when additional
* layers are added to the command chain, these values will
* have to be re-evaluated for correctness.
*/
if (un->un_f_non_devbsize_supported) {
un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
} else {
un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
}
un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
}
/*
* This property is set to 0 by HA software to avoid retries
* on a reserved disk. (The preferred property name is
* "retry-on-reservation-conflict") (1189689)
*
* Note: The use of a global here can have unintended consequences. A
* per instance variable is preferable to match the capabilities of
* different underlying hba's (4402600)
*/
sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
sd_retry_on_reservation_conflict);
if (sd_retry_on_reservation_conflict != 0) {
sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
sd_retry_on_reservation_conflict);
}
/* Set up options for QFULL handling. */
if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
"qfull-retries", -1)) != -1) {
(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
rval, 1);
}
if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
"qfull-retry-interval", -1)) != -1) {
(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
rval, 1);
}
/*
* This just prints a message that announces the existence of the
* device. The message is always printed in the system logfile, but
* only appears on the console if the system is booted with the
* -v (verbose) argument.
*/
ddi_report_dev(devi);
un->un_mediastate = DKIO_NONE;
/*
* Check Block Device Characteristics VPD.
*/
sd_check_bdc_vpd(ssc);
/*
* Check whether the drive is in emulation mode.
*/
sd_check_emulation_mode(ssc);
cmlb_alloc_handle(&un->un_cmlbhandle);
#if defined(__x86)
/*
* On x86, compensate for off-by-1 legacy error
*/
if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
(lbasize == un->un_sys_blocksize))
offbyone = CMLB_OFF_BY_ONE;
#endif
if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
VOID2BOOLEAN(un->un_f_has_removable_media != 0),
VOID2BOOLEAN(un->un_f_is_hotpluggable != 0),
un->un_node_type, offbyone, un->un_cmlbhandle,
(void *)SD_PATH_DIRECT) != 0) {
goto cmlb_attach_failed;
}
/*
* Read and validate the device's geometry (ie, disk label)
* A new unformatted drive will not have a valid geometry, but
* the driver needs to successfully attach to this device so
* the drive can be formatted via ioctls.
*/
geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
(void *)SD_PATH_DIRECT) == 0) ? 1: 0;
mutex_enter(SD_MUTEX(un));
/*
* Read and initialize the devid for the unit.
*/
if (un->un_f_devid_supported) {
sd_register_devid(ssc, devi, reservation_flag);
}
mutex_exit(SD_MUTEX(un));
if (un->un_f_opt_disable_cache == TRUE) {
/*
* Disable both read cache and write cache. This is
* the historic behavior of the keywords in the config file.
*/
if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
0) {
SD_ERROR(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p Could not disable "
"caching", un);
goto devid_failed;
}
}
/*
* Check the value of the WCE bit and if it's allowed to be changed,
* set un_f_write_cache_enabled and un_f_cache_mode_changeable
* accordingly.
*/
(void) sd_get_write_cache_enabled(ssc, &wc_enabled);
sd_get_write_cache_changeable(ssc, &wc_changeable);
mutex_enter(SD_MUTEX(un));
un->un_f_write_cache_enabled = (wc_enabled != 0);
un->un_f_cache_mode_changeable = (wc_changeable != 0);
mutex_exit(SD_MUTEX(un));
if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR &&
un->un_tgt_blocksize != DEV_BSIZE) ||
un->un_f_enable_rmw) {
if (!(un->un_wm_cache)) {
(void) snprintf(name_str, sizeof (name_str),
"%s%d_cache",
ddi_driver_name(SD_DEVINFO(un)),
ddi_get_instance(SD_DEVINFO(un)));
un->un_wm_cache = kmem_cache_create(
name_str, sizeof (struct sd_w_map),
8, sd_wm_cache_constructor,
sd_wm_cache_destructor, NULL,
(void *)un, NULL, 0);
if (!(un->un_wm_cache)) {
goto wm_cache_failed;
}
}
}
/*
* Check the value of the NV_SUP bit and set
* un_f_suppress_cache_flush accordingly.
*/
sd_get_nv_sup(ssc);
/*
* Find out what type of reservation this disk supports.
*/
status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL);
switch (status) {
case 0:
/*
* SCSI-3 reservations are supported.
*/
un->un_reservation_type = SD_SCSI3_RESERVATION;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
break;
case ENOTSUP:
/*
* The PERSISTENT RESERVE IN command would not be recognized by
* a SCSI-2 device, so assume the reservation type is SCSI-2.
*/
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
un->un_reservation_type = SD_SCSI2_RESERVATION;
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
break;
default:
/*
* default to SCSI-3 reservations
*/
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
un->un_reservation_type = SD_SCSI3_RESERVATION;
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
break;
}
/*
* Set the pstat and error stat values here, so data obtained during the
* previous attach-time routines is available.
*
* Note: This is a critical sequence that needs to be maintained:
* 1) Instantiate the kstats before any routines using the iopath
* (i.e. sd_send_scsi_cmd).
* 2) Initialize the error stats (sd_set_errstats) and partition
* stats (sd_set_pstats)here, following
* cmlb_validate_geometry(), sd_register_devid(), and
* sd_cache_control().
*/
if (un->un_f_pkstats_enabled && geom_label_valid) {
sd_set_pstats(un);
SD_TRACE(SD_LOG_IO_PARTITION, un,
"sd_unit_attach: un:0x%p pstats created and set\n", un);
}
sd_set_errstats(un);
SD_TRACE(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p errstats set\n", un);
sd_setup_blk_limits(ssc);
/*
* After successfully attaching an instance, we record the information
* of how many luns have been attached on the relative target and
* controller for parallel SCSI. This information is used when sd tries
* to set the tagged queuing capability in HBA.
*/
if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
}
SD_TRACE(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p exit success\n", un);
/* Uninitialize sd_ssc_t pointer */
sd_ssc_fini(ssc);
return (DDI_SUCCESS);
/*
* An error occurred during the attach; clean up & return failure.
*/
wm_cache_failed:
devid_failed:
ddi_remove_minor_node(devi, NULL);
cmlb_attach_failed:
/*
* Cleanup from the scsi_ifsetcap() calls (437868)
*/
(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
/*
* Refer to the comments of setting tagged-qing in the beginning of
* sd_unit_attach. We can only disable tagged queuing when there is
* no lun attached on the target.
*/
if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
}
if (un->un_f_is_fibre == FALSE) {
(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
}
spinup_failed:
/* Uninitialize sd_ssc_t pointer */
sd_ssc_fini(ssc);
mutex_enter(SD_MUTEX(un));
/* Deallocate SCSI FMA memory spaces */
kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
/* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
if (un->un_direct_priority_timeid != NULL) {
timeout_id_t temp_id = un->un_direct_priority_timeid;
un->un_direct_priority_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
/* Cancel any pending start/stop timeouts */
if (un->un_startstop_timeid != NULL) {
timeout_id_t temp_id = un->un_startstop_timeid;
un->un_startstop_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
/* Cancel any pending reset-throttle timeouts */
if (un->un_reset_throttle_timeid != NULL) {
timeout_id_t temp_id = un->un_reset_throttle_timeid;
un->un_reset_throttle_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
/* Cancel rmw warning message timeouts */
if (un->un_rmw_msg_timeid != NULL) {
timeout_id_t temp_id = un->un_rmw_msg_timeid;
un->un_rmw_msg_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
/* Cancel any pending retry timeouts */
if (un->un_retry_timeid != NULL) {
timeout_id_t temp_id = un->un_retry_timeid;
un->un_retry_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
/* Cancel any pending delayed cv broadcast timeouts */
if (un->un_dcvb_timeid != NULL) {
timeout_id_t temp_id = un->un_dcvb_timeid;
un->un_dcvb_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
mutex_exit(SD_MUTEX(un));
/* There should not be any in-progress I/O so ASSERT this check */
ASSERT(un->un_ncmds_in_transport == 0);
ASSERT(un->un_ncmds_in_driver == 0);
/* Do not free the softstate if the callback routine is active */
sd_sync_with_callback(un);
/*
* Partition stats apparently are not used with removables. These would
* not have been created during attach, so no need to clean them up...
*/
if (un->un_errstats != NULL) {
kstat_delete(un->un_errstats);
un->un_errstats = NULL;
}
create_errstats_failed:
if (un->un_stats != NULL) {
kstat_delete(un->un_stats);
un->un_stats = NULL;
}
ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
ddi_xbuf_attr_destroy(un->un_xbuf_attr);
ddi_prop_remove_all(devi);
cv_destroy(&un->un_state_cv);
sd_free_rqs(un);
alloc_rqs_failed:
devp->sd_private = NULL;
bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */
/*
* Note: the man pages are unclear as to whether or not doing a
* ddi_soft_state_free(sd_state, instance) is the right way to
* clean up after the ddi_soft_state_zalloc() if the subsequent
* ddi_get_soft_state() fails. The implication seems to be
* that the get_soft_state cannot fail if the zalloc succeeds.
*/
#ifndef XPV_HVM_DRIVER
ddi_soft_state_free(sd_state, instance);
#endif /* !XPV_HVM_DRIVER */
probe_failed:
scsi_unprobe(devp);
return (DDI_FAILURE);
}
/*
* Function: sd_unit_detach
*
* Description: Performs DDI_DETACH processing for sddetach().
*
* Return Code: DDI_SUCCESS
* DDI_FAILURE
*
* Context: Kernel thread context
*/
static int
sd_unit_detach(dev_info_t *devi)
{
struct scsi_device *devp;
struct sd_lun *un;
int i;
int tgt;
dev_t dev;
dev_info_t *pdip = ddi_get_parent(devi);
int instance = ddi_get_instance(devi);
/*
* Fail the detach for any of the following:
* - Unable to get the sd_lun struct for the instance
* - There is pending I/O
*/
devp = ddi_get_driver_private(devi);
if ((devp == NULL) ||
((un = (struct sd_lun *)devp->sd_private) == NULL) ||
(un->un_ncmds_in_driver != 0)) {
return (DDI_FAILURE);
}
SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
SCSI_ADDR_PROP_TARGET, -1);
dev = sd_make_device(SD_DEVINFO(un));
#ifndef lint
_NOTE(COMPETING_THREADS_NOW);
#endif
mutex_enter(SD_MUTEX(un));
/*
* Fail the detach if there are any outstanding layered
* opens on this device.
*/
for (i = 0; i < NDKMAP; i++) {
if (un->un_ocmap.lyropen[i] != 0) {
goto err_notclosed;
}
}
/*
* Verify there are NO outstanding commands issued to this device.
* ie, un_ncmds_in_transport == 0.
* It's possible to have outstanding commands through the physio
* code path, even though everything's closed.
*/
if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
(un->un_direct_priority_timeid != NULL) ||
(un->un_state == SD_STATE_RWAIT)) {
mutex_exit(SD_MUTEX(un));
SD_ERROR(SD_LOG_ATTACH_DETACH, un,
"sd_dr_detach: Detach failure due to outstanding cmds\n");
goto err_stillbusy;
}
/*
* If we have the device reserved, release the reservation.
*/
if ((un->un_resvd_status & SD_RESERVE) &&
!(un->un_resvd_status & SD_LOST_RESERVE)) {
mutex_exit(SD_MUTEX(un));
/*
* Note: sd_reserve_release sends a command to the device
* via the sd_ioctlcmd() path, and can sleep.
*/
if (sd_reserve_release(dev, SD_RELEASE) != 0) {
SD_ERROR(SD_LOG_ATTACH_DETACH, un,
"sd_dr_detach: Cannot release reservation \n");
}
} else {
mutex_exit(SD_MUTEX(un));
}
/*
* Untimeout any reserve recover, throttle reset, restart unit
* and delayed broadcast timeout threads. Protect the timeout pointer
* from getting nulled by their callback functions.
*/
mutex_enter(SD_MUTEX(un));
if (un->un_resvd_timeid != NULL) {
timeout_id_t temp_id = un->un_resvd_timeid;
un->un_resvd_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
if (un->un_reset_throttle_timeid != NULL) {
timeout_id_t temp_id = un->un_reset_throttle_timeid;
un->un_reset_throttle_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
if (un->un_startstop_timeid != NULL) {
timeout_id_t temp_id = un->un_startstop_timeid;
un->un_startstop_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
if (un->un_rmw_msg_timeid != NULL) {
timeout_id_t temp_id = un->un_rmw_msg_timeid;
un->un_rmw_msg_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
if (un->un_dcvb_timeid != NULL) {
timeout_id_t temp_id = un->un_dcvb_timeid;
un->un_dcvb_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
} else {
mutex_exit(SD_MUTEX(un));
}
/* Remove any pending reservation reclaim requests for this device */
sd_rmv_resv_reclaim_req(dev);
mutex_enter(SD_MUTEX(un));
/* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
if (un->un_direct_priority_timeid != NULL) {
timeout_id_t temp_id = un->un_direct_priority_timeid;
un->un_direct_priority_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
/* Cancel any active multi-host disk watch thread requests */
if (un->un_mhd_token != NULL) {
mutex_exit(SD_MUTEX(un));
_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
if (scsi_watch_request_terminate(un->un_mhd_token,
SCSI_WATCH_TERMINATE_NOWAIT)) {
SD_ERROR(SD_LOG_ATTACH_DETACH, un,
"sd_dr_detach: Cannot cancel mhd watch request\n");
/*
* Note: We are returning here after having removed
* some driver timeouts above. This is consistent with
* the legacy implementation but perhaps the watch
* terminate call should be made with the wait flag set.
*/
goto err_stillbusy;
}
mutex_enter(SD_MUTEX(un));
un->un_mhd_token = NULL;
}
if (un->un_swr_token != NULL) {
mutex_exit(SD_MUTEX(un));
_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
if (scsi_watch_request_terminate(un->un_swr_token,
SCSI_WATCH_TERMINATE_NOWAIT)) {
SD_ERROR(SD_LOG_ATTACH_DETACH, un,
"sd_dr_detach: Cannot cancel swr watch request\n");
/*
* Note: We are returning here after having removed
* some driver timeouts above. This is consistent with
* the legacy implementation but perhaps the watch
* terminate call should be made with the wait flag set.
*/
goto err_stillbusy;
}
mutex_enter(SD_MUTEX(un));
un->un_swr_token = NULL;
}
mutex_exit(SD_MUTEX(un));
/*
* Clear any scsi_reset_notifies. We clear the reset notifies
* if we have not registered one.
* Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
*/
(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
sd_mhd_reset_notify_cb, (caddr_t)un);
/*
* protect the timeout pointers from getting nulled by
* their callback functions during the cancellation process.
* In such a scenario untimeout can be invoked with a null value.
*/
_NOTE(NO_COMPETING_THREADS_NOW);
mutex_enter(&un->un_pm_mutex);
if (un->un_pm_idle_timeid != NULL) {
timeout_id_t temp_id = un->un_pm_idle_timeid;
un->un_pm_idle_timeid = NULL;
mutex_exit(&un->un_pm_mutex);
/*
* Timeout is active; cancel it.
* Note that it'll never be active on a device
* that does not support PM therefore we don't
* have to check before calling pm_idle_component.
*/
(void) untimeout(temp_id);
(void) pm_idle_component(SD_DEVINFO(un), 0);
mutex_enter(&un->un_pm_mutex);
}
/*
* Check whether there is already a timeout scheduled for power
* management. If yes then don't lower the power here, that's.
* the timeout handler's job.
*/
if (un->un_pm_timeid != NULL) {
timeout_id_t temp_id = un->un_pm_timeid;
un->un_pm_timeid = NULL;
mutex_exit(&un->un_pm_mutex);
/*
* Timeout is active; cancel it.
* Note that it'll never be active on a device
* that does not support PM therefore we don't
* have to check before calling pm_idle_component.
*/
(void) untimeout(temp_id);
(void) pm_idle_component(SD_DEVINFO(un), 0);
} else {
mutex_exit(&un->un_pm_mutex);
if ((un->un_f_pm_is_enabled == TRUE) &&
(pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
!= DDI_SUCCESS)) {
SD_ERROR(SD_LOG_ATTACH_DETACH, un,
"sd_dr_detach: Lower power request failed, ignoring.\n");
/*
* Fix for bug: 4297749, item # 13
* The above test now includes a check to see if PM is
* supported by this device before call
* pm_lower_power().
* Note, the following is not dead code. The call to
* pm_lower_power above will generate a call back into
* our sdpower routine which might result in a timeout
* handler getting activated. Therefore the following
* code is valid and necessary.
*/
mutex_enter(&un->un_pm_mutex);
if (un->un_pm_timeid != NULL) {
timeout_id_t temp_id = un->un_pm_timeid;
un->un_pm_timeid = NULL;
mutex_exit(&un->un_pm_mutex);
(void) untimeout(temp_id);
(void) pm_idle_component(SD_DEVINFO(un), 0);
} else {
mutex_exit(&un->un_pm_mutex);
}
}
}
/*
* Cleanup from the scsi_ifsetcap() calls (437868)
* Relocated here from above to be after the call to
* pm_lower_power, which was getting errors.
*/
(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
/*
* Currently, tagged queuing is supported per target based by HBA.
* Setting this per lun instance actually sets the capability of this
* target in HBA, which affects those luns already attached on the
* same target. So during detach, we can only disable this capability
* only when this is the only lun left on this target. By doing
* this, we assume a target has the same tagged queuing capability
* for every lun. The condition can be removed when HBA is changed to
* support per lun based tagged queuing capability.
*/
if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
}
if (un->un_f_is_fibre == FALSE) {
(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
}
/*
* Remove any event callbacks, fibre only
*/
if (un->un_f_is_fibre == TRUE) {
if ((un->un_insert_event != NULL) &&
(ddi_remove_event_handler(un->un_insert_cb_id) !=
DDI_SUCCESS)) {
/*
* Note: We are returning here after having done
* substantial cleanup above. This is consistent
* with the legacy implementation but this may not
* be the right thing to do.
*/
SD_ERROR(SD_LOG_ATTACH_DETACH, un,
"sd_dr_detach: Cannot cancel insert event\n");
goto err_remove_event;
}
un->un_insert_event = NULL;
if ((un->un_remove_event != NULL) &&
(ddi_remove_event_handler(un->un_remove_cb_id) !=
DDI_SUCCESS)) {
/*
* Note: We are returning here after having done
* substantial cleanup above. This is consistent
* with the legacy implementation but this may not
* be the right thing to do.
*/
SD_ERROR(SD_LOG_ATTACH_DETACH, un,
"sd_dr_detach: Cannot cancel remove event\n");
goto err_remove_event;
}
un->un_remove_event = NULL;
}
/* Do not free the softstate if the callback routine is active */
sd_sync_with_callback(un);
cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
cmlb_free_handle(&un->un_cmlbhandle);
/*
* Clean up the soft state struct.
* Cleanup is done in reverse order of allocs/inits.
* At this point there should be no competing threads anymore.
*/
scsi_fm_fini(devp);
/*
* Deallocate memory for SCSI FMA.
*/
kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
/*
* Unregister and free device id if it was not registered
* by the transport.
*/
if (un->un_f_devid_transport_defined == FALSE)
ddi_devid_unregister(devi);
/*
* free the devid structure if allocated before (by ddi_devid_init()
* or ddi_devid_get()).
*/
if (un->un_devid) {
ddi_devid_free(un->un_devid);
un->un_devid = NULL;
}
/*
* Destroy wmap cache if it exists.
*/
if (un->un_wm_cache != NULL) {
kmem_cache_destroy(un->un_wm_cache);
un->un_wm_cache = NULL;
}
/*
* kstat cleanup is done in detach for all device types (4363169).
* We do not want to fail detach if the device kstats are not deleted
* since there is a confusion about the devo_refcnt for the device.
* We just delete the kstats and let detach complete successfully.
*/
if (un->un_stats != NULL) {
kstat_delete(un->un_stats);
un->un_stats = NULL;
}
if (un->un_unmapstats != NULL) {
kstat_delete(un->un_unmapstats_ks);
un->un_unmapstats_ks = NULL;
un->un_unmapstats = NULL;
}
if (un->un_errstats != NULL) {
kstat_delete(un->un_errstats);
un->un_errstats = NULL;
}
/* Remove partition stats */
if (un->un_f_pkstats_enabled) {
for (i = 0; i < NSDMAP; i++) {
if (un->un_pstats[i] != NULL) {
kstat_delete(un->un_pstats[i]);
un->un_pstats[i] = NULL;
}
}
}
/* Remove xbuf registration */
ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
ddi_xbuf_attr_destroy(un->un_xbuf_attr);
/* Remove driver properties */
ddi_prop_remove_all(devi);
mutex_destroy(&un->un_pm_mutex);
cv_destroy(&un->un_pm_busy_cv);
cv_destroy(&un->un_wcc_cv);
/* Removable media condvar. */
cv_destroy(&un->un_state_cv);
/* Suspend/resume condvar. */
cv_destroy(&un->un_suspend_cv);
cv_destroy(&un->un_disk_busy_cv);
sd_free_rqs(un);
/* Free up soft state */
devp->sd_private = NULL;
bzero(un, sizeof (struct sd_lun));
ddi_soft_state_free(sd_state, instance);
/* This frees up the INQUIRY data associated with the device. */
scsi_unprobe(devp);
/*
* After successfully detaching an instance, we update the information
* of how many luns have been attached in the relative target and
* controller for parallel SCSI. This information is used when sd tries
* to set the tagged queuing capability in HBA.
* Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
* check if the device is parallel SCSI. However, we don't need to
* check here because we've already checked during attach. No device
* that is not parallel SCSI is in the chain.
*/
if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
}
return (DDI_SUCCESS);
err_notclosed:
mutex_exit(SD_MUTEX(un));
err_stillbusy:
_NOTE(NO_COMPETING_THREADS_NOW);
err_remove_event:
SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
return (DDI_FAILURE);
}
/*
* Function: sd_create_errstats
*
* Description: This routine instantiates the device error stats.
*
* Note: During attach the stats are instantiated first so they are
* available for attach-time routines that utilize the driver
* iopath to send commands to the device. The stats are initialized
* separately so data obtained during some attach-time routines is
* available. (4362483)
*
* Arguments: un - driver soft state (unit) structure
* instance - driver instance
*
* Context: Kernel thread context
*/
static void
sd_create_errstats(struct sd_lun *un, int instance)
{
struct sd_errstats *stp;
char kstatmodule_err[KSTAT_STRLEN];
char kstatname[KSTAT_STRLEN];
int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
ASSERT(un != NULL);
if (un->un_errstats != NULL) {
return;
}
(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
"%serr", sd_label);
(void) snprintf(kstatname, sizeof (kstatname),
"%s%d,err", sd_label, instance);
un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
"device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
if (un->un_errstats == NULL) {
SD_ERROR(SD_LOG_ATTACH_DETACH, un,
"sd_create_errstats: Failed kstat_create\n");
return;
}
stp = (struct sd_errstats *)un->un_errstats->ks_data;
kstat_named_init(&stp->sd_softerrs, "Soft Errors",
KSTAT_DATA_UINT32);
kstat_named_init(&stp->sd_harderrs, "Hard Errors",
KSTAT_DATA_UINT32);
kstat_named_init(&stp->sd_transerrs, "Transport Errors",
KSTAT_DATA_UINT32);
kstat_named_init(&stp->sd_vid, "Vendor",
KSTAT_DATA_CHAR);
kstat_named_init(&stp->sd_pid, "Product",
KSTAT_DATA_CHAR);
kstat_named_init(&stp->sd_revision, "Revision",
KSTAT_DATA_CHAR);
kstat_named_init(&stp->sd_serial, "Serial No",
KSTAT_DATA_CHAR);
kstat_named_init(&stp->sd_capacity, "Size",
KSTAT_DATA_ULONGLONG);
kstat_named_init(&stp->sd_rq_media_err, "Media Error",
KSTAT_DATA_UINT32);
kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready",
KSTAT_DATA_UINT32);
kstat_named_init(&stp->sd_rq_nodev_err, "No Device",
KSTAT_DATA_UINT32);
kstat_named_init(&stp->sd_rq_recov_err, "Recoverable",
KSTAT_DATA_UINT32);
kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request",
KSTAT_DATA_UINT32);
kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis",
KSTAT_DATA_UINT32);
un->un_errstats->ks_private = un;
un->un_errstats->ks_update = nulldev;
kstat_install(un->un_errstats);
}
/*
* Function: sd_set_errstats
*
* Description: This routine sets the value of the vendor id, product id,
* revision, serial number, and capacity device error stats.
*
* Note: During attach the stats are instantiated first so they are
* available for attach-time routines that utilize the driver
* iopath to send commands to the device. The stats are initialized
* separately so data obtained during some attach-time routines is
* available. (4362483)
*
* Arguments: un - driver soft state (unit) structure
*
* Context: Kernel thread context
*/
static void
sd_set_errstats(struct sd_lun *un)
{
struct sd_errstats *stp;
char *sn;
ASSERT(un != NULL);
ASSERT(un->un_errstats != NULL);
stp = (struct sd_errstats *)un->un_errstats->ks_data;
ASSERT(stp != NULL);
(void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
(void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
(void) strncpy(stp->sd_revision.value.c,
un->un_sd->sd_inq->inq_revision, 4);
/*
* All the errstats are persistent across detach/attach,
* so reset all the errstats here in case of the hot
* replacement of disk drives, except for not changed
* Sun qualified drives.
*/
if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
(bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
stp->sd_softerrs.value.ui32 = 0;
stp->sd_harderrs.value.ui32 = 0;
stp->sd_transerrs.value.ui32 = 0;
stp->sd_rq_media_err.value.ui32 = 0;
stp->sd_rq_ntrdy_err.value.ui32 = 0;
stp->sd_rq_nodev_err.value.ui32 = 0;
stp->sd_rq_recov_err.value.ui32 = 0;
stp->sd_rq_illrq_err.value.ui32 = 0;
stp->sd_rq_pfa_err.value.ui32 = 0;
}
/*
* Set the "Serial No" kstat for Sun qualified drives (indicated by
* "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
* (4376302))
*/
if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
sizeof (SD_INQUIRY(un)->inq_serial));
} else {
/*
* Set the "Serial No" kstat for non-Sun qualified drives
*/
if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un),
DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) {
(void) strlcpy(stp->sd_serial.value.c, sn,
sizeof (stp->sd_serial.value.c));
ddi_prop_free(sn);
}
}
if (un->un_f_blockcount_is_valid != TRUE) {
/*
* Set capacity error stat to 0 for no media. This ensures
* a valid capacity is displayed in response to 'iostat -E'
* when no media is present in the device.
*/
stp->sd_capacity.value.ui64 = 0;
} else {
/*
* Multiply un_blockcount by un->un_sys_blocksize to get
* capacity.
*
* Note: for non-512 blocksize devices "un_blockcount" has been
* "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
* (un_tgt_blocksize / un->un_sys_blocksize).
*/
stp->sd_capacity.value.ui64 = (uint64_t)
((uint64_t)un->un_blockcount * un->un_sys_blocksize);
}
}
/*
* Function: sd_set_pstats
*
* Description: This routine instantiates and initializes the partition
* stats for each partition with more than zero blocks.
* (4363169)
*
* Arguments: un - driver soft state (unit) structure
*
* Context: Kernel thread context
*/
static void
sd_set_pstats(struct sd_lun *un)
{
char kstatname[KSTAT_STRLEN];
int instance;
int i;
diskaddr_t nblks = 0;
char *partname = NULL;
ASSERT(un != NULL);
instance = ddi_get_instance(SD_DEVINFO(un));
/* Note:x86: is this a VTOC8/VTOC16 difference? */
for (i = 0; i < NSDMAP; i++) {
if (cmlb_partinfo(un->un_cmlbhandle, i,
&nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
continue;
mutex_enter(SD_MUTEX(un));
if ((un->un_pstats[i] == NULL) &&
(nblks != 0)) {
(void) snprintf(kstatname, sizeof (kstatname),
"%s%d,%s", sd_label, instance,
partname);
un->un_pstats[i] = kstat_create(sd_label,
instance, kstatname, "partition", KSTAT_TYPE_IO,
1, KSTAT_FLAG_PERSISTENT);
if (un->un_pstats[i] != NULL) {
un->un_pstats[i]->ks_lock = SD_MUTEX(un);
kstat_install(un->un_pstats[i]);
}
}
mutex_exit(SD_MUTEX(un));
}
}
/*
* Values related to caching mode page depending on whether the unit is ATAPI.
*/
#define SDC_CDB_GROUP(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
CDB_GROUP1 : CDB_GROUP0)
#define SDC_HDRLEN(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
MODE_HEADER_LENGTH_GRP2 : MODE_HEADER_LENGTH)
/*
* Use mode_cache_scsi3 to ensure we get all of the mode sense data, otherwise
* the mode select will fail (mode_cache_scsi3 is a superset of mode_caching).
*/
#define SDC_BUFLEN(un) (SDC_HDRLEN(un) + MODE_BLK_DESC_LENGTH + \
sizeof (struct mode_cache_scsi3))
static int
sd_get_caching_mode_page(sd_ssc_t *ssc, uchar_t page_control, uchar_t **header,
int *bdlen)
{
struct sd_lun *un = ssc->ssc_un;
struct mode_caching *mode_caching_page;
size_t buflen = SDC_BUFLEN(un);
int hdrlen = SDC_HDRLEN(un);
int rval;
/*
* Do a test unit ready, otherwise a mode sense may not work if this
* is the first command sent to the device after boot.
*/
if (sd_send_scsi_TEST_UNIT_READY(ssc, 0) != 0)
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
/*
* Allocate memory for the retrieved mode page and its headers. Set
* a pointer to the page itself.
*/
*header = kmem_zalloc(buflen, KM_SLEEP);
/* Get the information from the device */
rval = sd_send_scsi_MODE_SENSE(ssc, SDC_CDB_GROUP(un), *header, buflen,
page_control | MODEPAGE_CACHING, SD_PATH_DIRECT);
if (rval != 0) {
SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, "%s: Mode Sense Failed\n",
__func__);
goto mode_sense_failed;
}
/*
* Determine size of Block Descriptors in order to locate
* the mode page data. ATAPI devices return 0, SCSI devices
* should return MODE_BLK_DESC_LENGTH.
*/
if (un->un_f_cfg_is_atapi == TRUE) {
struct mode_header_grp2 *mhp =
(struct mode_header_grp2 *)(*header);
*bdlen = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
} else {
*bdlen = ((struct mode_header *)(*header))->bdesc_length;
}
if (*bdlen > MODE_BLK_DESC_LENGTH) {
sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
"%s: Mode Sense returned invalid block descriptor length\n",
__func__);
rval = EIO;
goto mode_sense_failed;
}
mode_caching_page = (struct mode_caching *)(*header + hdrlen + *bdlen);
if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
"%s: Mode Sense caching page code mismatch %d\n",
__func__, mode_caching_page->mode_page.code);
rval = EIO;
}
mode_sense_failed:
if (rval != 0) {
kmem_free(*header, buflen);
*header = NULL;
*bdlen = 0;
}
return (rval);
}
/*
* Function: sd_cache_control()
*
* Description: This routine is the driver entry point for setting
* read and write caching by modifying the WCE (write cache
* enable) and RCD (read cache disable) bits of mode
* page 8 (MODEPAGE_CACHING).
*
* Arguments: ssc - ssc contains pointer to driver soft state
* (unit) structure for this target.
* rcd_flag - flag for controlling the read cache
* wce_flag - flag for controlling the write cache
*
* Return Code: EIO
* code returned by sd_send_scsi_MODE_SENSE and
* sd_send_scsi_MODE_SELECT
*
* Context: Kernel Thread
*/
static int
sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag)
{
struct sd_lun *un = ssc->ssc_un;
struct mode_caching *mode_caching_page;
uchar_t *header;
size_t buflen = SDC_BUFLEN(un);
int hdrlen = SDC_HDRLEN(un);
int bdlen;
int rval;
rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
switch (rval) {
case 0:
/* Check the relevant bits on successful mode sense */
mode_caching_page = (struct mode_caching *)(header + hdrlen +
bdlen);
if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
(!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
(mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
(!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
size_t sbuflen;
uchar_t save_pg;
/*
* Construct select buffer length based on the
* length of the sense data returned.
*/
sbuflen = hdrlen + bdlen + sizeof (struct mode_page) +
(int)mode_caching_page->mode_page.length;
/* Set the caching bits as requested */
if (rcd_flag == SD_CACHE_ENABLE)
mode_caching_page->rcd = 0;
else if (rcd_flag == SD_CACHE_DISABLE)
mode_caching_page->rcd = 1;
if (wce_flag == SD_CACHE_ENABLE)
mode_caching_page->wce = 1;
else if (wce_flag == SD_CACHE_DISABLE)
mode_caching_page->wce = 0;
/*
* Save the page if the mode sense says the
* drive supports it.
*/
save_pg = mode_caching_page->mode_page.ps ?
SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
/* Clear reserved bits before mode select */
mode_caching_page->mode_page.ps = 0;
/*
* Clear out mode header for mode select.
* The rest of the retrieved page will be reused.
*/
bzero(header, hdrlen);
if (un->un_f_cfg_is_atapi == TRUE) {
struct mode_header_grp2 *mhp =
(struct mode_header_grp2 *)header;
mhp->bdesc_length_hi = bdlen >> 8;
mhp->bdesc_length_lo = (uchar_t)bdlen & 0xff;
} else {
((struct mode_header *)header)->bdesc_length =
bdlen;
}
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
/* Issue mode select to change the cache settings */
rval = sd_send_scsi_MODE_SELECT(ssc, SDC_CDB_GROUP(un),
header, sbuflen, save_pg, SD_PATH_DIRECT);
}
kmem_free(header, buflen);
break;
case EIO:
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
break;
default:
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
break;
}
return (rval);
}
/*
* Function: sd_get_write_cache_enabled()
*
* Description: This routine is the driver entry point for determining if write
* caching is enabled. It examines the WCE (write cache enable)
* bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
* bits set to MODEPAGE_CURRENT.
*
* Arguments: ssc - ssc contains pointer to driver soft state
* (unit) structure for this target.
* is_enabled - pointer to int where write cache enabled state
* is returned (non-zero -> write cache enabled)
*
* Return Code: EIO
* code returned by sd_send_scsi_MODE_SENSE
*
* Context: Kernel Thread
*
* NOTE: If ioctl is added to disable write cache, this sequence should
* be followed so that no locking is required for accesses to
* un->un_f_write_cache_enabled:
* do mode select to clear wce
* do synchronize cache to flush cache
* set un->un_f_write_cache_enabled = FALSE
*
* Conversely, an ioctl to enable the write cache should be done
* in this order:
* set un->un_f_write_cache_enabled = TRUE
* do mode select to set wce
*/
static int
sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled)
{
struct sd_lun *un = ssc->ssc_un;
struct mode_caching *mode_caching_page;
uchar_t *header;
size_t buflen = SDC_BUFLEN(un);
int hdrlen = SDC_HDRLEN(un);
int bdlen;
int rval;
/* In case of error, flag as enabled */
*is_enabled = TRUE;
rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
switch (rval) {
case 0:
mode_caching_page = (struct mode_caching *)(header + hdrlen +
bdlen);
*is_enabled = mode_caching_page->wce;
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
kmem_free(header, buflen);
break;
case EIO: {
/*
* Some disks do not support Mode Sense(6), we
* should ignore this kind of error (sense key is
* 0x5 - illegal request).
*/
uint8_t *sensep;
int senlen;
sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
ssc->ssc_uscsi_cmd->uscsi_rqresid);
if (senlen > 0 &&
scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
} else {
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
}
break;
}
default:
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
break;
}
return (rval);
}
/*
* Function: sd_get_write_cache_changeable()
*
* Description: This routine is the driver entry point for determining if write
* caching is changeable. It examines the WCE (write cache enable)
* bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
* bits set to MODEPAGE_CHANGEABLE.
*
* Arguments: ssc - ssc contains pointer to driver soft state
* (unit) structure for this target.
* is_changeable - pointer to int where write cache changeable
* state is returned (non-zero -> write cache
* changeable)
*
* Context: Kernel Thread
*/
static void
sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable)
{
struct sd_lun *un = ssc->ssc_un;
struct mode_caching *mode_caching_page;
uchar_t *header;
size_t buflen = SDC_BUFLEN(un);
int hdrlen = SDC_HDRLEN(un);
int bdlen;
int rval;
/* In case of error, flag as enabled */
*is_changeable = TRUE;
rval = sd_get_caching_mode_page(ssc, MODEPAGE_CHANGEABLE, &header,
&bdlen);
switch (rval) {
case 0:
mode_caching_page = (struct mode_caching *)(header + hdrlen +
bdlen);
*is_changeable = mode_caching_page->wce;
kmem_free(header, buflen);
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
break;
case EIO:
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
break;
default:
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
break;
}
}
/*
* Function: sd_get_nv_sup()
*
* Description: This routine is the driver entry point for
* determining whether non-volatile cache is supported. This
* determination process works as follows:
*
* 1. sd first queries sd.conf on whether
* suppress_cache_flush bit is set for this device.
*
* 2. if not there, then queries the internal disk table.
*
* 3. if either sd.conf or internal disk table specifies
* cache flush be suppressed, we don't bother checking
* NV_SUP bit.
*
* If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries
* the optional INQUIRY VPD page 0x86. If the device
* supports VPD page 0x86, sd examines the NV_SUP
* (non-volatile cache support) bit in the INQUIRY VPD page
* 0x86:
* o If NV_SUP bit is set, sd assumes the device has a
* non-volatile cache and set the
* un_f_sync_nv_supported to TRUE.
* o Otherwise cache is not non-volatile,
* un_f_sync_nv_supported is set to FALSE.
*
* Arguments: un - driver soft state (unit) structure
*
* Return Code:
*
* Context: Kernel Thread
*/
static void
sd_get_nv_sup(sd_ssc_t *ssc)
{
int rval = 0;
uchar_t *inq86 = NULL;
size_t inq86_len = MAX_INQUIRY_SIZE;
size_t inq86_resid = 0;
struct dk_callback *dkc;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
mutex_enter(SD_MUTEX(un));
/*
* Be conservative on the device's support of
* SYNC_NV bit: un_f_sync_nv_supported is
* initialized to be false.
*/
un->un_f_sync_nv_supported = FALSE;
/*
* If either sd.conf or internal disk table
* specifies cache flush be suppressed, then
* we don't bother checking NV_SUP bit.
*/
if (un->un_f_suppress_cache_flush == TRUE) {
mutex_exit(SD_MUTEX(un));
return;
}
if (sd_check_vpd_page_support(ssc) == 0 &&
un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
mutex_exit(SD_MUTEX(un));
/* collect page 86 data if available */
inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
0x01, 0x86, &inq86_resid);
if (rval == 0 && (inq86_len - inq86_resid > 6)) {
SD_TRACE(SD_LOG_COMMON, un,
"sd_get_nv_sup: \
successfully get VPD page: %x \
PAGE LENGTH: %x BYTE 6: %x\n",
inq86[1], inq86[3], inq86[6]);
mutex_enter(SD_MUTEX(un));
/*
* check the value of NV_SUP bit: only if the device
* reports NV_SUP bit to be 1, the
* un_f_sync_nv_supported bit will be set to true.
*/
if (inq86[6] & SD_VPD_NV_SUP) {
un->un_f_sync_nv_supported = TRUE;
}
mutex_exit(SD_MUTEX(un));
} else if (rval != 0) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
kmem_free(inq86, inq86_len);
} else {
mutex_exit(SD_MUTEX(un));
}
/*
* Send a SYNC CACHE command to check whether
* SYNC_NV bit is supported. This command should have
* un_f_sync_nv_supported set to correct value.
*/
mutex_enter(SD_MUTEX(un));
if (un->un_f_sync_nv_supported) {
mutex_exit(SD_MUTEX(un));
dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP);
dkc->dkc_flag = FLUSH_VOLATILE;
(void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
/*
* Send a TEST UNIT READY command to the device. This should
* clear any outstanding UNIT ATTENTION that may be present.
*/
rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
if (rval != 0)
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
kmem_free(dkc, sizeof (struct dk_callback));
} else {
mutex_exit(SD_MUTEX(un));
}
SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \
un_f_suppress_cache_flush is set to %d\n",
un->un_f_suppress_cache_flush);
}
/*
* Function: sd_make_device
*
* Description: Utility routine to return the Solaris device number from
* the data in the device's dev_info structure.
*
* Return Code: The Solaris device number
*
* Context: Any
*/
static dev_t
sd_make_device(dev_info_t *devi)
{
return (makedevice(ddi_driver_major(devi),
ddi_get_instance(devi) << SDUNIT_SHIFT));
}
/*
* Function: sd_pm_entry
*
* Description: Called at the start of a new command to manage power
* and busy status of a device. This includes determining whether
* the current power state of the device is sufficient for
* performing the command or whether it must be changed.
* The PM framework is notified appropriately.
* Only with a return status of DDI_SUCCESS will the
* component be busy to the framework.
*
* All callers of sd_pm_entry must check the return status
* and only call sd_pm_exit it it was DDI_SUCCESS. A status
* of DDI_FAILURE indicates the device failed to power up.
* In this case un_pm_count has been adjusted so the result
* on exit is still powered down, ie. count is less than 0.
* Calling sd_pm_exit with this count value hits an ASSERT.
*
* Return Code: DDI_SUCCESS or DDI_FAILURE
*
* Context: Kernel thread context.
*/
static int
sd_pm_entry(struct sd_lun *un)
{
int return_status = DDI_SUCCESS;
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(!mutex_owned(&un->un_pm_mutex));
SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
if (un->un_f_pm_is_enabled == FALSE) {
SD_TRACE(SD_LOG_IO_PM, un,
"sd_pm_entry: exiting, PM not enabled\n");
return (return_status);
}
/*
* Just increment a counter if PM is enabled. On the transition from
* 0 ==> 1, mark the device as busy. The iodone side will decrement
* the count with each IO and mark the device as idle when the count
* hits 0.
*
* If the count is less than 0 the device is powered down. If a powered
* down device is successfully powered up then the count must be
* incremented to reflect the power up. Note that it'll get incremented
* a second time to become busy.
*
* Because the following has the potential to change the device state
* and must release the un_pm_mutex to do so, only one thread can be
* allowed through at a time.
*/
mutex_enter(&un->un_pm_mutex);
while (un->un_pm_busy == TRUE) {
cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
}
un->un_pm_busy = TRUE;
if (un->un_pm_count < 1) {
SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
/*
* Indicate we are now busy so the framework won't attempt to
* power down the device. This call will only fail if either
* we passed a bad component number or the device has no
* components. Neither of these should ever happen.
*/
mutex_exit(&un->un_pm_mutex);
return_status = pm_busy_component(SD_DEVINFO(un), 0);
ASSERT(return_status == DDI_SUCCESS);
mutex_enter(&un->un_pm_mutex);
if (un->un_pm_count < 0) {
mutex_exit(&un->un_pm_mutex);
SD_TRACE(SD_LOG_IO_PM, un,
"sd_pm_entry: power up component\n");
/*
* pm_raise_power will cause sdpower to be called
* which brings the device power level to the
* desired state, If successful, un_pm_count and
* un_power_level will be updated appropriately.
*/
return_status = pm_raise_power(SD_DEVINFO(un), 0,
SD_PM_STATE_ACTIVE(un));
mutex_enter(&un->un_pm_mutex);
if (return_status != DDI_SUCCESS) {
/*
* Power up failed.
* Idle the device and adjust the count
* so the result on exit is that we're
* still powered down, ie. count is less than 0.
*/
SD_TRACE(SD_LOG_IO_PM, un,
"sd_pm_entry: power up failed,"
" idle the component\n");
(void) pm_idle_component(SD_DEVINFO(un), 0);
un->un_pm_count--;
} else {
/*
* Device is powered up, verify the
* count is non-negative.
* This is debug only.
*/
ASSERT(un->un_pm_count == 0);
}
}
if (return_status == DDI_SUCCESS) {
/*
* For performance, now that the device has been tagged
* as busy, and it's known to be powered up, update the
* chain types to use jump tables that do not include
* pm. This significantly lowers the overhead and
* therefore improves performance.
*/
mutex_exit(&un->un_pm_mutex);
mutex_enter(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_PM, un,
"sd_pm_entry: changing uscsi_chain_type from %d\n",
un->un_uscsi_chain_type);
if (un->un_f_non_devbsize_supported) {
un->un_buf_chain_type =
SD_CHAIN_INFO_RMMEDIA_NO_PM;
} else {
un->un_buf_chain_type =
SD_CHAIN_INFO_DISK_NO_PM;
}
un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
SD_TRACE(SD_LOG_IO_PM, un,
" changed uscsi_chain_type to %d\n",
un->un_uscsi_chain_type);
mutex_exit(SD_MUTEX(un));
mutex_enter(&un->un_pm_mutex);
if (un->un_pm_idle_timeid == NULL) {
/* 300 ms. */
un->un_pm_idle_timeid =
timeout(sd_pm_idletimeout_handler, un,
(drv_usectohz((clock_t)300000)));
/*
* Include an extra call to busy which keeps the
* device busy with-respect-to the PM layer
* until the timer fires, at which time it'll
* get the extra idle call.
*/
(void) pm_busy_component(SD_DEVINFO(un), 0);
}
}
}
un->un_pm_busy = FALSE;
/* Next... */
cv_signal(&un->un_pm_busy_cv);
un->un_pm_count++;
SD_TRACE(SD_LOG_IO_PM, un,
"sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
mutex_exit(&un->un_pm_mutex);
return (return_status);
}
/*
* Function: sd_pm_exit
*
* Description: Called at the completion of a command to manage busy
* status for the device. If the device becomes idle the
* PM framework is notified.
*
* Context: Kernel thread context
*/
static void
sd_pm_exit(struct sd_lun *un)
{
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(!mutex_owned(&un->un_pm_mutex));
SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
/*
* After attach the following flag is only read, so don't
* take the penalty of acquiring a mutex for it.
*/
if (un->un_f_pm_is_enabled == TRUE) {
mutex_enter(&un->un_pm_mutex);
un->un_pm_count--;
SD_TRACE(SD_LOG_IO_PM, un,
"sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
ASSERT(un->un_pm_count >= 0);
if (un->un_pm_count == 0) {
mutex_exit(&un->un_pm_mutex);
SD_TRACE(SD_LOG_IO_PM, un,
"sd_pm_exit: idle component\n");
(void) pm_idle_component(SD_DEVINFO(un), 0);
} else {
mutex_exit(&un->un_pm_mutex);
}
}
SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
}
/*
* Function: sdopen
*
* Description: Driver's open(9e) entry point function.
*
* Arguments: dev_i - pointer to device number
* flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
* otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
* cred_p - user credential pointer
*
* Return Code: EINVAL
* ENXIO
* EIO
* EROFS
* EBUSY
*
* Context: Kernel thread context
*/
/* ARGSUSED */
static int
sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
{
struct sd_lun *un;
int nodelay;
int part;
uint64_t partmask;
int instance;
dev_t dev;
int rval = EIO;
diskaddr_t nblks = 0;
diskaddr_t label_cap;
/* Validate the open type */
if (otyp >= OTYPCNT) {
return (EINVAL);
}
dev = *dev_p;
instance = SDUNIT(dev);
/*
* Fail the open if there is no softstate for the instance.
*/
if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
/*
* The probe cache only needs to be cleared when open (9e) fails
* with ENXIO (4238046).
*/
/*
* un-conditionally clearing probe cache is ok with
* separate sd/ssd binaries
* x86 platform can be an issue with both parallel
* and fibre in 1 binary
*/
sd_scsi_clear_probe_cache();
return (ENXIO);
}
nodelay = (flag & (FNDELAY | FNONBLOCK));
part = SDPART(dev);
partmask = 1 << part;
mutex_enter(SD_MUTEX(un));
/*
* All device accesses go thru sdstrategy() where we check
* on suspend status but there could be a scsi_poll command,
* which bypasses sdstrategy(), so we need to check pm
* status.
*/
if (!nodelay) {
while ((un->un_state == SD_STATE_SUSPENDED) ||
(un->un_state == SD_STATE_PM_CHANGING)) {
cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
}
mutex_exit(SD_MUTEX(un));
if (sd_pm_entry(un) != DDI_SUCCESS) {
rval = EIO;
SD_ERROR(SD_LOG_OPEN_CLOSE, un,
"sdopen: sd_pm_entry failed\n");
goto open_failed_with_pm;
}
mutex_enter(SD_MUTEX(un));
}
/* check for previous exclusive open */
SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
SD_TRACE(SD_LOG_OPEN_CLOSE, un,
"sdopen: exclopen=%x, flag=%x, regopen=%x\n",
un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
if (un->un_exclopen & (partmask)) {
goto excl_open_fail;
}
if (flag & FEXCL) {
int i;
if (un->un_ocmap.lyropen[part]) {
goto excl_open_fail;
}
for (i = 0; i < (OTYPCNT - 1); i++) {
if (un->un_ocmap.regopen[i] & (partmask)) {
goto excl_open_fail;
}
}
}
/*
* Check the write permission if this is a removable media device,
* NDELAY has not been set, and writable permission is requested.
*
* Note: If NDELAY was set and this is write-protected media the WRITE
* attempt will fail with EIO as part of the I/O processing. This is a
* more permissive implementation that allows the open to succeed and
* WRITE attempts to fail when appropriate.
*/
if (un->un_f_chk_wp_open) {
if ((flag & FWRITE) && (!nodelay)) {
mutex_exit(SD_MUTEX(un));
/*
* Defer the check for write permission on writable
* DVD drive till sdstrategy and will not fail open even
* if FWRITE is set as the device can be writable
* depending upon the media and the media can change
* after the call to open().
*/
if (un->un_f_dvdram_writable_device == FALSE) {
if (ISCD(un) || sr_check_wp(dev)) {
rval = EROFS;
mutex_enter(SD_MUTEX(un));
SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
"write to cd or write protected media\n");
goto open_fail;
}
}
mutex_enter(SD_MUTEX(un));
}
}
/*
* If opening in NDELAY/NONBLOCK mode, just return.
* Check if disk is ready and has a valid geometry later.
*/
if (!nodelay) {
sd_ssc_t *ssc;
mutex_exit(SD_MUTEX(un));
ssc = sd_ssc_init(un);
rval = sd_ready_and_valid(ssc, part);
sd_ssc_fini(ssc);
mutex_enter(SD_MUTEX(un));
/*
* Fail if device is not ready or if the number of disk
* blocks is zero or negative for non CD devices.
*/
nblks = 0;
if (rval == SD_READY_VALID && (!ISCD(un))) {
/* if cmlb_partinfo fails, nblks remains 0 */
mutex_exit(SD_MUTEX(un));
(void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
mutex_enter(SD_MUTEX(un));
}
if ((rval != SD_READY_VALID) ||
(!ISCD(un) && nblks <= 0)) {
rval = un->un_f_has_removable_media ? ENXIO : EIO;
SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
"device not ready or invalid disk block value\n");
goto open_fail;
}
#if defined(__x86)
} else {
uchar_t *cp;
/*
* x86 requires special nodelay handling, so that p0 is
* always defined and accessible.
* Invalidate geometry only if device is not already open.
*/
cp = &un->un_ocmap.chkd[0];
while (cp < &un->un_ocmap.chkd[OCSIZE]) {
if (*cp != (uchar_t)0) {
break;
}
cp++;
}
if (cp == &un->un_ocmap.chkd[OCSIZE]) {
mutex_exit(SD_MUTEX(un));
cmlb_invalidate(un->un_cmlbhandle,
(void *)SD_PATH_DIRECT);
mutex_enter(SD_MUTEX(un));
}
#endif
}
if (otyp == OTYP_LYR) {
un->un_ocmap.lyropen[part]++;
} else {
un->un_ocmap.regopen[otyp] |= partmask;
}
/* Set up open and exclusive open flags */
if (flag & FEXCL) {
un->un_exclopen |= (partmask);
}
/*
* If the lun is EFI labeled and lun capacity is greater than the
* capacity contained in the label, log a sys-event to notify the
* interested module.
* To avoid an infinite loop of logging sys-event, we only log the
* event when the lun is not opened in NDELAY mode. The event handler
* should open the lun in NDELAY mode.
*/
if (!nodelay) {
mutex_exit(SD_MUTEX(un));
if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
(void*)SD_PATH_DIRECT) == 0) {
mutex_enter(SD_MUTEX(un));
if (un->un_f_blockcount_is_valid &&
un->un_blockcount > label_cap &&
un->un_f_expnevent == B_FALSE) {
un->un_f_expnevent = B_TRUE;
mutex_exit(SD_MUTEX(un));
sd_log_lun_expansion_event(un,
(nodelay ? KM_NOSLEEP : KM_SLEEP));
mutex_enter(SD_MUTEX(un));
}
} else {
mutex_enter(SD_MUTEX(un));
}
}
SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
"open of part %d type %d\n", part, otyp);
mutex_exit(SD_MUTEX(un));
if (!nodelay) {
sd_pm_exit(un);
}
SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
return (DDI_SUCCESS);
excl_open_fail:
SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
rval = EBUSY;
open_fail:
mutex_exit(SD_MUTEX(un));
/*
* On a failed open we must exit the pm management.
*/
if (!nodelay) {
sd_pm_exit(un);
}
open_failed_with_pm:
return (rval);
}
/*
* Function: sdclose
*
* Description: Driver's close(9e) entry point function.
*
* Arguments: dev - device number
* flag - file status flag, informational only
* otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
* cred_p - user credential pointer
*
* Return Code: ENXIO
*
* Context: Kernel thread context
*/
/* ARGSUSED */
static int
sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
{
struct sd_lun *un;
uchar_t *cp;
int part;
int nodelay;
int rval = 0;
/* Validate the open type */
if (otyp >= OTYPCNT) {
return (ENXIO);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
part = SDPART(dev);
nodelay = flag & (FNDELAY | FNONBLOCK);
SD_TRACE(SD_LOG_OPEN_CLOSE, un,
"sdclose: close of part %d type %d\n", part, otyp);
mutex_enter(SD_MUTEX(un));
/* Don't proceed if power is being changed. */
while (un->un_state == SD_STATE_PM_CHANGING) {
cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
}
if (un->un_exclopen & (1 << part)) {
un->un_exclopen &= ~(1 << part);
}
/* Update the open partition map */
if (otyp == OTYP_LYR) {
un->un_ocmap.lyropen[part] -= 1;
} else {
un->un_ocmap.regopen[otyp] &= ~(1 << part);
}
cp = &un->un_ocmap.chkd[0];
while (cp < &un->un_ocmap.chkd[OCSIZE]) {
if (*cp != '\0') {
break;
}
cp++;
}
if (cp == &un->un_ocmap.chkd[OCSIZE]) {
SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
/*
* We avoid persistance upon the last close, and set
* the throttle back to the maximum.
*/
un->un_throttle = un->un_saved_throttle;
if (un->un_state == SD_STATE_OFFLINE) {
if (un->un_f_is_fibre == FALSE) {
scsi_log(SD_DEVINFO(un), sd_label,
CE_WARN, "offline\n");
}
mutex_exit(SD_MUTEX(un));
cmlb_invalidate(un->un_cmlbhandle,
(void *)SD_PATH_DIRECT);
mutex_enter(SD_MUTEX(un));
} else {
/*
* Flush any outstanding writes in NVRAM cache.
* Note: SYNCHRONIZE CACHE is an optional SCSI-2
* cmd, it may not work for non-Pluto devices.
* SYNCHRONIZE CACHE is not required for removables,
* except DVD-RAM drives.
*
* Also note: because SYNCHRONIZE CACHE is currently
* the only command issued here that requires the
* drive be powered up, only do the power up before
* sending the Sync Cache command. If additional
* commands are added which require a powered up
* drive, the following sequence may have to change.
*
* And finally, note that parallel SCSI on SPARC
* only issues a Sync Cache to DVD-RAM, a newly
* supported device.
*/
if ((un->un_f_sync_cache_supported &&
un->un_f_sync_cache_required) ||
un->un_f_dvdram_writable_device == TRUE) {
mutex_exit(SD_MUTEX(un));
if (sd_pm_entry(un) == DDI_SUCCESS) {
rval =
sd_send_scsi_SYNCHRONIZE_CACHE(un,
NULL);
/* ignore error if not supported */
if (rval == ENOTSUP) {
rval = 0;
} else if (rval != 0) {
rval = EIO;
}
sd_pm_exit(un);
} else {
rval = EIO;
}
mutex_enter(SD_MUTEX(un));
}
/*
* For devices which supports DOOR_LOCK, send an ALLOW
* MEDIA REMOVAL command, but don't get upset if it
* fails. We need to raise the power of the drive before
* we can call sd_send_scsi_DOORLOCK()
*/
if (un->un_f_doorlock_supported) {
mutex_exit(SD_MUTEX(un));
if (sd_pm_entry(un) == DDI_SUCCESS) {
sd_ssc_t *ssc;
ssc = sd_ssc_init(un);
rval = sd_send_scsi_DOORLOCK(ssc,
SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
if (rval != 0)
sd_ssc_assessment(ssc,
SD_FMT_IGNORE);
sd_ssc_fini(ssc);
sd_pm_exit(un);
if (ISCD(un) && (rval != 0) &&
(nodelay != 0)) {
rval = ENXIO;
}
} else {
rval = EIO;
}
mutex_enter(SD_MUTEX(un));
}
/*
* If a device has removable media, invalidate all
* parameters related to media, such as geometry,
* blocksize, and blockcount.
*/
if (un->un_f_has_removable_media) {
sr_ejected(un);
}
/*
* Destroy the cache (if it exists) which was
* allocated for the write maps, as long as no
* other outstanding commands for the device exist.
* (If we don't destroy it here, we will do so later
* on detach. More likely we'll just reuse it on
* a future open.)
*/
if ((un->un_wm_cache != NULL) &&
(un->un_ncmds_in_driver == 0)) {
kmem_cache_destroy(un->un_wm_cache);
un->un_wm_cache = NULL;
}
}
}
mutex_exit(SD_MUTEX(un));
return (rval);
}
/*
* Function: sd_ready_and_valid
*
* Description: Test if device is ready and has a valid geometry.
*
* Arguments: ssc - sd_ssc_t will contain un
* un - driver soft state (unit) structure
*
* Return Code: SD_READY_VALID ready and valid label
* SD_NOT_READY_VALID not ready, no label
* SD_RESERVED_BY_OTHERS reservation conflict
*
* Context: Never called at interrupt context.
*/
static int
sd_ready_and_valid(sd_ssc_t *ssc, int part)
{
struct sd_errstats *stp;
uint64_t capacity;
uint_t lbasize;
int rval = SD_READY_VALID;
char name_str[48];
boolean_t is_valid;
struct sd_lun *un;
int status;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
mutex_enter(SD_MUTEX(un));
/*
* If a device has removable media, we must check if media is
* ready when checking if this device is ready and valid.
*/
if (un->un_f_has_removable_media) {
mutex_exit(SD_MUTEX(un));
status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
if (status != 0) {
rval = SD_NOT_READY_VALID;
mutex_enter(SD_MUTEX(un));
/* Ignore all failed status for removalbe media */
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
goto done;
}
is_valid = SD_IS_VALID_LABEL(un);
mutex_enter(SD_MUTEX(un));
if (!is_valid ||
(un->un_f_blockcount_is_valid == FALSE) ||
(un->un_f_tgt_blocksize_is_valid == FALSE)) {
/* capacity has to be read every open. */
mutex_exit(SD_MUTEX(un));
status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
&lbasize, SD_PATH_DIRECT);
if (status != 0) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
cmlb_invalidate(un->un_cmlbhandle,
(void *)SD_PATH_DIRECT);
mutex_enter(SD_MUTEX(un));
rval = SD_NOT_READY_VALID;
goto done;
} else {
mutex_enter(SD_MUTEX(un));
sd_update_block_info(un, lbasize, capacity);
}
}
/*
* Check if the media in the device is writable or not.
*/
if (!is_valid && ISCD(un)) {
sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
}
} else {
/*
* Do a test unit ready to clear any unit attention from non-cd
* devices.
*/
mutex_exit(SD_MUTEX(un));
status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
if (status != 0) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
mutex_enter(SD_MUTEX(un));
}
/*
* If this is a non 512 block device, allocate space for
* the wmap cache. This is being done here since every time
* a media is changed this routine will be called and the
* block size is a function of media rather than device.
*/
if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
un->un_f_non_devbsize_supported) &&
un->un_tgt_blocksize != DEV_BSIZE) ||
un->un_f_enable_rmw) {
if (!(un->un_wm_cache)) {
(void) snprintf(name_str, sizeof (name_str),
"%s%d_cache",
ddi_driver_name(SD_DEVINFO(un)),
ddi_get_instance(SD_DEVINFO(un)));
un->un_wm_cache = kmem_cache_create(
name_str, sizeof (struct sd_w_map),
8, sd_wm_cache_constructor,
sd_wm_cache_destructor, NULL,
(void *)un, NULL, 0);
if (!(un->un_wm_cache)) {
rval = ENOMEM;
goto done;
}
}
}
if (un->un_state == SD_STATE_NORMAL) {
/*
* If the target is not yet ready here (defined by a TUR
* failure), invalidate the geometry and print an 'offline'
* message. This is a legacy message, as the state of the
* target is not actually changed to SD_STATE_OFFLINE.
*
* If the TUR fails for EACCES (Reservation Conflict),
* SD_RESERVED_BY_OTHERS will be returned to indicate
* reservation conflict. If the TUR fails for other
* reasons, SD_NOT_READY_VALID will be returned.
*/
int err;
mutex_exit(SD_MUTEX(un));
err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
mutex_enter(SD_MUTEX(un));
if (err != 0) {
mutex_exit(SD_MUTEX(un));
cmlb_invalidate(un->un_cmlbhandle,
(void *)SD_PATH_DIRECT);
mutex_enter(SD_MUTEX(un));
if (err == EACCES) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"reservation conflict\n");
rval = SD_RESERVED_BY_OTHERS;
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
} else {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"drive offline\n");
rval = SD_NOT_READY_VALID;
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
}
goto done;
}
}
if (un->un_f_format_in_progress == FALSE) {
mutex_exit(SD_MUTEX(un));
(void) cmlb_validate(un->un_cmlbhandle, 0,
(void *)SD_PATH_DIRECT);
if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL,
NULL, (void *) SD_PATH_DIRECT) != 0) {
rval = SD_NOT_READY_VALID;
mutex_enter(SD_MUTEX(un));
goto done;
}
if (un->un_f_pkstats_enabled) {
sd_set_pstats(un);
SD_TRACE(SD_LOG_IO_PARTITION, un,
"sd_ready_and_valid: un:0x%p pstats created and "
"set\n", un);
}
mutex_enter(SD_MUTEX(un));
}
/*
* If this device supports DOOR_LOCK command, try and send
* this command to PREVENT MEDIA REMOVAL, but don't get upset
* if it fails. For a CD, however, it is an error
*/
if (un->un_f_doorlock_supported) {
mutex_exit(SD_MUTEX(un));
status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
SD_PATH_DIRECT);
if ((status != 0) && ISCD(un)) {
rval = SD_NOT_READY_VALID;
mutex_enter(SD_MUTEX(un));
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
goto done;
} else if (status != 0)
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
mutex_enter(SD_MUTEX(un));
}
/* The state has changed, inform the media watch routines */
un->un_mediastate = DKIO_INSERTED;
cv_broadcast(&un->un_state_cv);
rval = SD_READY_VALID;
done:
/*
* Initialize the capacity kstat value, if no media previously
* (capacity kstat is 0) and a media has been inserted
* (un_blockcount > 0).
*/
if (un->un_errstats != NULL) {
stp = (struct sd_errstats *)un->un_errstats->ks_data;
if ((stp->sd_capacity.value.ui64 == 0) &&
(un->un_f_blockcount_is_valid == TRUE)) {
stp->sd_capacity.value.ui64 =
(uint64_t)((uint64_t)un->un_blockcount *
un->un_sys_blocksize);
}
}
mutex_exit(SD_MUTEX(un));
return (rval);
}
/*
* Function: sdmin
*
* Description: Routine to limit the size of a data transfer. Used in
* conjunction with physio(9F).
*
* Arguments: bp - pointer to the indicated buf(9S) struct.
*
* Context: Kernel thread context.
*/
static void
sdmin(struct buf *bp)
{
struct sd_lun *un;
int instance;
instance = SDUNIT(bp->b_edev);
un = ddi_get_soft_state(sd_state, instance);
ASSERT(un != NULL);
/*
* We depend on buf breakup to restrict
* IO size if it is enabled.
*/
if (un->un_buf_breakup_supported) {
return;
}
if (bp->b_bcount > un->un_max_xfer_size) {
bp->b_bcount = un->un_max_xfer_size;
}
}
/*
* Function: sdread
*
* Description: Driver's read(9e) entry point function.
*
* Arguments: dev - device number
* uio - structure pointer describing where data is to be stored
* in user's space
* cred_p - user credential pointer
*
* Return Code: ENXIO
* EIO
* EINVAL
* value returned by physio
*
* Context: Kernel thread context.
*/
/* ARGSUSED */
static int
sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
{
struct sd_lun *un = NULL;
int secmask;
int err = 0;
sd_ssc_t *ssc;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
ASSERT(!mutex_owned(SD_MUTEX(un)));
if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
mutex_enter(SD_MUTEX(un));
/*
* Because the call to sd_ready_and_valid will issue I/O we
* must wait here if either the device is suspended or
* if it's power level is changing.
*/
while ((un->un_state == SD_STATE_SUSPENDED) ||
(un->un_state == SD_STATE_PM_CHANGING)) {
cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
}
un->un_ncmds_in_driver++;
mutex_exit(SD_MUTEX(un));
/* Initialize sd_ssc_t for internal uscsi commands */
ssc = sd_ssc_init(un);
if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
err = EIO;
} else {
err = 0;
}
sd_ssc_fini(ssc);
mutex_enter(SD_MUTEX(un));
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
mutex_exit(SD_MUTEX(un));
if (err != 0)
return (err);
}
/*
* Read requests are restricted to multiples of the system block size.
*/
if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
!un->un_f_enable_rmw)
secmask = un->un_tgt_blocksize - 1;
else
secmask = DEV_BSIZE - 1;
if (uio->uio_loffset & ((offset_t)(secmask))) {
SD_ERROR(SD_LOG_READ_WRITE, un,
"sdread: file offset not modulo %d\n",
secmask + 1);
err = EINVAL;
} else if (uio->uio_iov->iov_len & (secmask)) {
SD_ERROR(SD_LOG_READ_WRITE, un,
"sdread: transfer length not modulo %d\n",
secmask + 1);
err = EINVAL;
} else {
err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
}
return (err);
}
/*
* Function: sdwrite
*
* Description: Driver's write(9e) entry point function.
*
* Arguments: dev - device number
* uio - structure pointer describing where data is stored in
* user's space
* cred_p - user credential pointer
*
* Return Code: ENXIO
* EIO
* EINVAL
* value returned by physio
*
* Context: Kernel thread context.
*/
/* ARGSUSED */
static int
sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
{
struct sd_lun *un = NULL;
int secmask;
int err = 0;
sd_ssc_t *ssc;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
ASSERT(!mutex_owned(SD_MUTEX(un)));
if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
mutex_enter(SD_MUTEX(un));
/*
* Because the call to sd_ready_and_valid will issue I/O we
* must wait here if either the device is suspended or
* if it's power level is changing.
*/
while ((un->un_state == SD_STATE_SUSPENDED) ||
(un->un_state == SD_STATE_PM_CHANGING)) {
cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
}
un->un_ncmds_in_driver++;
mutex_exit(SD_MUTEX(un));
/* Initialize sd_ssc_t for internal uscsi commands */
ssc = sd_ssc_init(un);
if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
err = EIO;
} else {
err = 0;
}
sd_ssc_fini(ssc);
mutex_enter(SD_MUTEX(un));
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
mutex_exit(SD_MUTEX(un));
if (err != 0)
return (err);
}
/*
* Write requests are restricted to multiples of the system block size.
*/
if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
!un->un_f_enable_rmw)
secmask = un->un_tgt_blocksize - 1;
else
secmask = DEV_BSIZE - 1;
if (uio->uio_loffset & ((offset_t)(secmask))) {
SD_ERROR(SD_LOG_READ_WRITE, un,
"sdwrite: file offset not modulo %d\n",
secmask + 1);
err = EINVAL;
} else if (uio->uio_iov->iov_len & (secmask)) {
SD_ERROR(SD_LOG_READ_WRITE, un,
"sdwrite: transfer length not modulo %d\n",
secmask + 1);
err = EINVAL;
} else {
err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
}
return (err);
}
/*
* Function: sdaread
*
* Description: Driver's aread(9e) entry point function.
*
* Arguments: dev - device number
* aio - structure pointer describing where data is to be stored
* cred_p - user credential pointer
*
* Return Code: ENXIO
* EIO
* EINVAL
* value returned by aphysio
*
* Context: Kernel thread context.
*/
/* ARGSUSED */
static int
sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
{
struct sd_lun *un = NULL;
struct uio *uio = aio->aio_uio;
int secmask;
int err = 0;
sd_ssc_t *ssc;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
ASSERT(!mutex_owned(SD_MUTEX(un)));
if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
mutex_enter(SD_MUTEX(un));
/*
* Because the call to sd_ready_and_valid will issue I/O we
* must wait here if either the device is suspended or
* if it's power level is changing.
*/
while ((un->un_state == SD_STATE_SUSPENDED) ||
(un->un_state == SD_STATE_PM_CHANGING)) {
cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
}
un->un_ncmds_in_driver++;
mutex_exit(SD_MUTEX(un));
/* Initialize sd_ssc_t for internal uscsi commands */
ssc = sd_ssc_init(un);
if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
err = EIO;
} else {
err = 0;
}
sd_ssc_fini(ssc);
mutex_enter(SD_MUTEX(un));
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
mutex_exit(SD_MUTEX(un));
if (err != 0)
return (err);
}
/*
* Read requests are restricted to multiples of the system block size.
*/
if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
!un->un_f_enable_rmw)
secmask = un->un_tgt_blocksize - 1;
else
secmask = DEV_BSIZE - 1;
if (uio->uio_loffset & ((offset_t)(secmask))) {
SD_ERROR(SD_LOG_READ_WRITE, un,
"sdaread: file offset not modulo %d\n",
secmask + 1);
err = EINVAL;
} else if (uio->uio_iov->iov_len & (secmask)) {
SD_ERROR(SD_LOG_READ_WRITE, un,
"sdaread: transfer length not modulo %d\n",
secmask + 1);
err = EINVAL;
} else {
err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
}
return (err);
}
/*
* Function: sdawrite
*
* Description: Driver's awrite(9e) entry point function.
*
* Arguments: dev - device number
* aio - structure pointer describing where data is stored
* cred_p - user credential pointer
*
* Return Code: ENXIO
* EIO
* EINVAL
* value returned by aphysio
*
* Context: Kernel thread context.
*/
/* ARGSUSED */
static int
sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
{
struct sd_lun *un = NULL;
struct uio *uio = aio->aio_uio;
int secmask;
int err = 0;
sd_ssc_t *ssc;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
ASSERT(!mutex_owned(SD_MUTEX(un)));
if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
mutex_enter(SD_MUTEX(un));
/*
* Because the call to sd_ready_and_valid will issue I/O we
* must wait here if either the device is suspended or
* if it's power level is changing.
*/
while ((un->un_state == SD_STATE_SUSPENDED) ||
(un->un_state == SD_STATE_PM_CHANGING)) {
cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
}
un->un_ncmds_in_driver++;
mutex_exit(SD_MUTEX(un));
/* Initialize sd_ssc_t for internal uscsi commands */
ssc = sd_ssc_init(un);
if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
err = EIO;
} else {
err = 0;
}
sd_ssc_fini(ssc);
mutex_enter(SD_MUTEX(un));
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
mutex_exit(SD_MUTEX(un));
if (err != 0)
return (err);
}
/*
* Write requests are restricted to multiples of the system block size.
*/
if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
!un->un_f_enable_rmw)
secmask = un->un_tgt_blocksize - 1;
else
secmask = DEV_BSIZE - 1;
if (uio->uio_loffset & ((offset_t)(secmask))) {
SD_ERROR(SD_LOG_READ_WRITE, un,
"sdawrite: file offset not modulo %d\n",
secmask + 1);
err = EINVAL;
} else if (uio->uio_iov->iov_len & (secmask)) {
SD_ERROR(SD_LOG_READ_WRITE, un,
"sdawrite: transfer length not modulo %d\n",
secmask + 1);
err = EINVAL;
} else {
err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
}
return (err);
}
/*
* Driver IO processing follows the following sequence:
*
* sdioctl(9E) sdstrategy(9E) biodone(9F)
* | | ^
* v v |
* sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+
* | | | |
* v | | |
* sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone()
* | | ^ ^
* v v | |
* SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | |
* | | | |
* +---+ | +------------+ +-------+
* | | | |
* | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| |
* | v | |
* | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() |
* | | ^ |
* | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| |
* | v | |
* | sd_mapblocksize_iostart() sd_mapblocksize_iodone() |
* | | ^ |
* | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| |
* | v | |
* | sd_checksum_iostart() sd_checksum_iodone() |
* | | ^ |
* +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+
* | v | |
* | sd_pm_iostart() sd_pm_iodone() |
* | | ^ |
* | | | |
* +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+
* | ^
* v |
* sd_core_iostart() |
* | |
* | +------>(*destroypkt)()
* +-> sd_start_cmds() <-+ | |
* | | | v
* | | | scsi_destroy_pkt(9F)
* | | |
* +->(*initpkt)() +- sdintr()
* | | | |
* | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx()
* | +-> scsi_setup_cdb(9F) |
* | |
* +--> scsi_transport(9F) |
* | |
* +----> SCSA ---->+
*
*
* This code is based upon the following presumptions:
*
* - iostart and iodone functions operate on buf(9S) structures. These
* functions perform the necessary operations on the buf(9S) and pass
* them along to the next function in the chain by using the macros
* SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
* (for iodone side functions).
*
* - The iostart side functions may sleep. The iodone side functions
* are called under interrupt context and may NOT sleep. Therefore
* iodone side functions also may not call iostart side functions.
* (NOTE: iostart side functions should NOT sleep for memory, as
* this could result in deadlock.)
*
* - An iostart side function may call its corresponding iodone side
* function directly (if necessary).
*
* - In the event of an error, an iostart side function can return a buf(9S)
* to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
* b_error in the usual way of course).
*
* - The taskq mechanism may be used by the iodone side functions to dispatch
* requests to the iostart side functions. The iostart side functions in
* this case would be called under the context of a taskq thread, so it's
* OK for them to block/sleep/spin in this case.
*
* - iostart side functions may allocate "shadow" buf(9S) structs and
* pass them along to the next function in the chain. The corresponding
* iodone side functions must coalesce the "shadow" bufs and return
* the "original" buf to the next higher layer.
*
* - The b_private field of the buf(9S) struct holds a pointer to
* an sd_xbuf struct, which contains information needed to
* construct the scsi_pkt for the command.
*
* - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
* layer must acquire & release the SD_MUTEX(un) as needed.
*/
/*
* Create taskq for all targets in the system. This is created at
* _init(9E) and destroyed at _fini(9E).
*
* Note: here we set the minalloc to a reasonably high number to ensure that
* we will have an adequate supply of task entries available at interrupt time.
* This is used in conjunction with the TASKQ_PREPOPULATE flag in
* sd_create_taskq(). Since we do not want to sleep for allocations at
* interrupt time, set maxalloc equal to minalloc. That way we will just fail
* the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
* requests any one instant in time.
*/
#define SD_TASKQ_NUMTHREADS 8
#define SD_TASKQ_MINALLOC 256
#define SD_TASKQ_MAXALLOC 256
static taskq_t *sd_tq = NULL;
_NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
static int sd_taskq_minalloc = SD_TASKQ_MINALLOC;
static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
/*
* The following task queue is being created for the write part of
* read-modify-write of non-512 block size devices.
* Limit the number of threads to 1 for now. This number has been chosen
* considering the fact that it applies only to dvd ram drives/MO drives
* currently. Performance for which is not main criteria at this stage.
* Note: It needs to be explored if we can use a single taskq in future
*/
#define SD_WMR_TASKQ_NUMTHREADS 1
static taskq_t *sd_wmr_tq = NULL;
_NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
/*
* Function: sd_taskq_create
*
* Description: Create taskq thread(s) and preallocate task entries
*
* Return Code: Returns a pointer to the allocated taskq_t.
*
* Context: Can sleep. Requires blockable context.
*
* Notes: - The taskq() facility currently is NOT part of the DDI.
* (definitely NOT recommeded for 3rd-party drivers!) :-)
* - taskq_create() will block for memory, also it will panic
* if it cannot create the requested number of threads.
* - Currently taskq_create() creates threads that cannot be
* swapped.
* - We use TASKQ_PREPOPULATE to ensure we have an adequate
* supply of taskq entries at interrupt time (ie, so that we
* do not have to sleep for memory)
*/
static void
sd_taskq_create(void)
{
char taskq_name[TASKQ_NAMELEN];
ASSERT(sd_tq == NULL);
ASSERT(sd_wmr_tq == NULL);
(void) snprintf(taskq_name, sizeof (taskq_name),
"%s_drv_taskq", sd_label);
sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
(v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
TASKQ_PREPOPULATE));
(void) snprintf(taskq_name, sizeof (taskq_name),
"%s_rmw_taskq", sd_label);
sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
(v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
TASKQ_PREPOPULATE));
}
/*
* Function: sd_taskq_delete
*
* Description: Complementary cleanup routine for sd_taskq_create().
*
* Context: Kernel thread context.
*/
static void
sd_taskq_delete(void)
{
ASSERT(sd_tq != NULL);
ASSERT(sd_wmr_tq != NULL);
taskq_destroy(sd_tq);
taskq_destroy(sd_wmr_tq);
sd_tq = NULL;
sd_wmr_tq = NULL;
}
/*
* Function: sdstrategy
*
* Description: Driver's strategy (9E) entry point function.
*
* Arguments: bp - pointer to buf(9S)
*
* Return Code: Always returns zero
*
* Context: Kernel thread context.
*/
static int
sdstrategy(struct buf *bp)
{
struct sd_lun *un;
un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
if (un == NULL) {
bioerror(bp, EIO);
bp->b_resid = bp->b_bcount;
biodone(bp);
return (0);
}
/* As was done in the past, fail new cmds. if state is dumping. */
if (un->un_state == SD_STATE_DUMPING) {
bioerror(bp, ENXIO);
bp->b_resid = bp->b_bcount;
biodone(bp);
return (0);
}
ASSERT(!mutex_owned(SD_MUTEX(un)));
/*
* Commands may sneak in while we released the mutex in
* DDI_SUSPEND, we should block new commands. However, old
* commands that are still in the driver at this point should
* still be allowed to drain.
*/
mutex_enter(SD_MUTEX(un));
/*
* Must wait here if either the device is suspended or
* if it's power level is changing.
*/
while ((un->un_state == SD_STATE_SUSPENDED) ||
(un->un_state == SD_STATE_PM_CHANGING)) {
cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
}
un->un_ncmds_in_driver++;
/*
* atapi: Since we are running the CD for now in PIO mode we need to
* call bp_mapin here to avoid bp_mapin called interrupt context under
* the HBA's init_pkt routine.
*/
if (un->un_f_cfg_is_atapi == TRUE) {
mutex_exit(SD_MUTEX(un));
bp_mapin(bp);
mutex_enter(SD_MUTEX(un));
}
SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
un->un_ncmds_in_driver);
if (bp->b_flags & B_WRITE)
un->un_f_sync_cache_required = TRUE;
mutex_exit(SD_MUTEX(un));
/*
* This will (eventually) allocate the sd_xbuf area and
* call sd_xbuf_strategy(). We just want to return the
* result of ddi_xbuf_qstrategy so that we have an opt-
* imized tail call which saves us a stack frame.
*/
return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
}
/*
* Function: sd_xbuf_strategy
*
* Description: Function for initiating IO operations via the
* ddi_xbuf_qstrategy() mechanism.
*
* Context: Kernel thread context.
*/
static void
sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
{
struct sd_lun *un = arg;
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
/*
* Initialize the fields in the xbuf and save a pointer to the
* xbuf in bp->b_private.
*/
sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
/* Send the buf down the iostart chain */
SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
}
/*
* Function: sd_xbuf_init
*
* Description: Prepare the given sd_xbuf struct for use.
*
* Arguments: un - ptr to softstate
* bp - ptr to associated buf(9S)
* xp - ptr to associated sd_xbuf
* chain_type - IO chain type to use:
* SD_CHAIN_NULL
* SD_CHAIN_BUFIO
* SD_CHAIN_USCSI
* SD_CHAIN_DIRECT
* SD_CHAIN_DIRECT_PRIORITY
* pktinfop - ptr to private data struct for scsi_pkt(9S)
* initialization; may be NULL if none.
*
* Context: Kernel thread context
*/
static void
sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
uchar_t chain_type, void *pktinfop)
{
int index;
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(xp != NULL);
SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
bp, chain_type);
xp->xb_un = un;
xp->xb_pktp = NULL;
xp->xb_pktinfo = pktinfop;
xp->xb_private = bp->b_private;
xp->xb_blkno = (daddr_t)bp->b_blkno;
/*
* Set up the iostart and iodone chain indexes in the xbuf, based
* upon the specified chain type to use.
*/
switch (chain_type) {
case SD_CHAIN_NULL:
/*
* Fall thru to just use the values for the buf type, even
* tho for the NULL chain these values will never be used.
*/
/* FALLTHRU */
case SD_CHAIN_BUFIO:
index = un->un_buf_chain_type;
if ((!un->un_f_has_removable_media) &&
(un->un_tgt_blocksize != 0) &&
(un->un_tgt_blocksize != DEV_BSIZE ||
un->un_f_enable_rmw)) {
int secmask = 0, blknomask = 0;
if (un->un_f_enable_rmw) {
blknomask =
(un->un_phy_blocksize / DEV_BSIZE) - 1;
secmask = un->un_phy_blocksize - 1;
} else {
blknomask =
(un->un_tgt_blocksize / DEV_BSIZE) - 1;
secmask = un->un_tgt_blocksize - 1;
}
if ((bp->b_lblkno & (blknomask)) ||
(bp->b_bcount & (secmask))) {
if ((un->un_f_rmw_type !=
SD_RMW_TYPE_RETURN_ERROR) ||
un->un_f_enable_rmw) {
if (un->un_f_pm_is_enabled == FALSE)
index =
SD_CHAIN_INFO_MSS_DSK_NO_PM;
else
index =
SD_CHAIN_INFO_MSS_DISK;
}
}
}
break;
case SD_CHAIN_USCSI:
index = un->un_uscsi_chain_type;
break;
case SD_CHAIN_DIRECT:
index = un->un_direct_chain_type;
break;
case SD_CHAIN_DIRECT_PRIORITY:
index = un->un_priority_chain_type;
break;
default:
/* We're really broken if we ever get here... */
panic("sd_xbuf_init: illegal chain type!");
/*NOTREACHED*/
}
xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
/*
* It might be a bit easier to simply bzero the entire xbuf above,
* but it turns out that since we init a fair number of members anyway,
* we save a fair number cycles by doing explicit assignment of zero.
*/
xp->xb_pkt_flags = 0;
xp->xb_dma_resid = 0;
xp->xb_retry_count = 0;
xp->xb_victim_retry_count = 0;
xp->xb_ua_retry_count = 0;
xp->xb_nr_retry_count = 0;
xp->xb_sense_bp = NULL;
xp->xb_sense_status = 0;
xp->xb_sense_state = 0;
xp->xb_sense_resid = 0;
xp->xb_ena = 0;
bp->b_private = xp;
bp->b_flags &= ~(B_DONE | B_ERROR);
bp->b_resid = 0;
bp->av_forw = NULL;
bp->av_back = NULL;
bioerror(bp, 0);
SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
}
/*
* Function: sd_uscsi_strategy
*
* Description: Wrapper for calling into the USCSI chain via physio(9F)
*
* Arguments: bp - buf struct ptr
*
* Return Code: Always returns 0
*
* Context: Kernel thread context
*/
static int
sd_uscsi_strategy(struct buf *bp)
{
struct sd_lun *un;
struct sd_uscsi_info *uip;
struct sd_xbuf *xp;
uchar_t chain_type;
uchar_t cmd;
ASSERT(bp != NULL);
un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
if (un == NULL) {
bioerror(bp, EIO);
bp->b_resid = bp->b_bcount;
biodone(bp);
return (0);
}
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
/*
* A pointer to a struct sd_uscsi_info is expected in bp->b_private
*/
ASSERT(bp->b_private != NULL);
uip = (struct sd_uscsi_info *)bp->b_private;
cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
mutex_enter(SD_MUTEX(un));
/*
* atapi: Since we are running the CD for now in PIO mode we need to
* call bp_mapin here to avoid bp_mapin called interrupt context under
* the HBA's init_pkt routine.
*/
if (un->un_f_cfg_is_atapi == TRUE) {
mutex_exit(SD_MUTEX(un));
bp_mapin(bp);
mutex_enter(SD_MUTEX(un));
}
un->un_ncmds_in_driver++;
SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
un->un_ncmds_in_driver);
if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
(cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
un->un_f_sync_cache_required = TRUE;
mutex_exit(SD_MUTEX(un));
switch (uip->ui_flags) {
case SD_PATH_DIRECT:
chain_type = SD_CHAIN_DIRECT;
break;
case SD_PATH_DIRECT_PRIORITY:
chain_type = SD_CHAIN_DIRECT_PRIORITY;
break;
default:
chain_type = SD_CHAIN_USCSI;
break;
}
/*
* We may allocate extra buf for external USCSI commands. If the
* application asks for bigger than 20-byte sense data via USCSI,
* SCSA layer will allocate 252 bytes sense buf for that command.
*/
if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
SENSE_LENGTH) {
xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH +
MAX_SENSE_LENGTH, KM_SLEEP);
} else {
xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP);
}
sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
/* Use the index obtained within xbuf_init */
SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
return (0);
}
/*
* Function: sd_send_scsi_cmd
*
* Description: Runs a USCSI command for user (when called thru sdioctl),
* or for the driver
*
* Arguments: dev - the dev_t for the device
* incmd - ptr to a valid uscsi_cmd struct
* flag - bit flag, indicating open settings, 32/64 bit type
* dataspace - UIO_USERSPACE or UIO_SYSSPACE
* path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
* the normal command waitq, or SD_PATH_DIRECT_PRIORITY
* to use the USCSI "direct" chain and bypass the normal
* command waitq.
*
* Return Code: 0 - successful completion of the given command
* EIO - scsi_uscsi_handle_command() failed
* ENXIO - soft state not found for specified dev
* EINVAL
* EFAULT - copyin/copyout error
* return code of scsi_uscsi_handle_command():
* EIO
* ENXIO
* EACCES
*
* Context: Waits for command to complete. Can sleep.
*/
static int
sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
enum uio_seg dataspace, int path_flag)
{
struct sd_lun *un;
sd_ssc_t *ssc;
int rval;
un = ddi_get_soft_state(sd_state, SDUNIT(dev));
if (un == NULL) {
return (ENXIO);
}
/*
* Using sd_ssc_send to handle uscsi cmd
*/
ssc = sd_ssc_init(un);
rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
sd_ssc_fini(ssc);
return (rval);
}
/*
* Function: sd_ssc_init
*
* Description: Uscsi end-user call this function to initialize necessary
* fields, such as uscsi_cmd and sd_uscsi_info struct.
*
* The return value of sd_send_scsi_cmd will be treated as a
* fault in various conditions. Even it is not Zero, some
* callers may ignore the return value. That is to say, we can
* not make an accurate assessment in sdintr, since if a
* command is failed in sdintr it does not mean the caller of
* sd_send_scsi_cmd will treat it as a real failure.
*
* To avoid printing too many error logs for a failed uscsi
* packet that the caller may not treat it as a failure, the
* sd will keep silent for handling all uscsi commands.
*
* During detach->attach and attach-open, for some types of
* problems, the driver should be providing information about
* the problem encountered. Device use USCSI_SILENT, which
* suppresses all driver information. The result is that no
* information about the problem is available. Being
* completely silent during this time is inappropriate. The
* driver needs a more selective filter than USCSI_SILENT, so
* that information related to faults is provided.
*
* To make the accurate accessment, the caller of
* sd_send_scsi_USCSI_CMD should take the ownership and
* get necessary information to print error messages.
*
* If we want to print necessary info of uscsi command, we need to
* keep the uscsi_cmd and sd_uscsi_info till we can make the
* assessment. We use sd_ssc_init to alloc necessary
* structs for sending an uscsi command and we are also
* responsible for free the memory by calling
* sd_ssc_fini.
*
* The calling secquences will look like:
* sd_ssc_init->
*
* ...
*
* sd_send_scsi_USCSI_CMD->
* sd_ssc_send-> - - - sdintr
* ...
*
* if we think the return value should be treated as a
* failure, we make the accessment here and print out
* necessary by retrieving uscsi_cmd and sd_uscsi_info'
*
* ...
*
* sd_ssc_fini
*
*
* Arguments: un - pointer to driver soft state (unit) structure for this
* target.
*
* Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains
* uscsi_cmd and sd_uscsi_info.
* NULL - if can not alloc memory for sd_ssc_t struct
*
* Context: Kernel Thread.
*/
static sd_ssc_t *
sd_ssc_init(struct sd_lun *un)
{
sd_ssc_t *ssc;
struct uscsi_cmd *ucmdp;
struct sd_uscsi_info *uip;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
/*
* Allocate sd_ssc_t structure
*/
ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP);
/*
* Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine
*/
ucmdp = scsi_uscsi_alloc();
/*
* Allocate sd_uscsi_info structure
*/
uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
ssc->ssc_uscsi_cmd = ucmdp;
ssc->ssc_uscsi_info = uip;
ssc->ssc_un = un;
return (ssc);
}
/*
* Function: sd_ssc_fini
*
* Description: To free sd_ssc_t and it's hanging off
*
* Arguments: ssc - struct pointer of sd_ssc_t.
*/
static void
sd_ssc_fini(sd_ssc_t *ssc)
{
scsi_uscsi_free(ssc->ssc_uscsi_cmd);
if (ssc->ssc_uscsi_info != NULL) {
kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info));
ssc->ssc_uscsi_info = NULL;
}
kmem_free(ssc, sizeof (sd_ssc_t));
ssc = NULL;
}
/*
* Function: sd_ssc_send
*
* Description: Runs a USCSI command for user when called through sdioctl,
* or for the driver.
*
* Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
* sd_uscsi_info in.
* incmd - ptr to a valid uscsi_cmd struct
* flag - bit flag, indicating open settings, 32/64 bit type
* dataspace - UIO_USERSPACE or UIO_SYSSPACE
* path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
* the normal command waitq, or SD_PATH_DIRECT_PRIORITY
* to use the USCSI "direct" chain and bypass the normal
* command waitq.
*
* Return Code: 0 - successful completion of the given command
* EIO - scsi_uscsi_handle_command() failed
* ENXIO - soft state not found for specified dev
* ECANCELED - command cancelled due to low power
* EINVAL
* EFAULT - copyin/copyout error
* return code of scsi_uscsi_handle_command():
* EIO
* ENXIO
* EACCES
*
* Context: Kernel Thread;
* Waits for command to complete. Can sleep.
*/
static int
sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
enum uio_seg dataspace, int path_flag)
{
struct sd_uscsi_info *uip;
struct uscsi_cmd *uscmd;
struct sd_lun *un;
dev_t dev;
int format = 0;
int rval;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
uscmd = ssc->ssc_uscsi_cmd;
ASSERT(uscmd != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
/*
* If enter here, it indicates that the previous uscsi
* command has not been processed by sd_ssc_assessment.
* This is violating our rules of FMA telemetry processing.
* We should print out this message and the last undisposed
* uscsi command.
*/
if (uscmd->uscsi_cdb != NULL) {
SD_INFO(SD_LOG_SDTEST, un,
"sd_ssc_send is missing the alternative "
"sd_ssc_assessment when running command 0x%x.\n",
uscmd->uscsi_cdb[0]);
}
/*
* Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
* the initial status.
*/
ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
}
/*
* We need to make sure sd_ssc_send will have sd_ssc_assessment
* followed to avoid missing FMA telemetries.
*/
ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
/*
* if USCSI_PMFAILFAST is set and un is in low power, fail the
* command immediately.
*/
mutex_enter(SD_MUTEX(un));
mutex_enter(&un->un_pm_mutex);
if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
SD_DEVICE_IS_IN_LOW_POWER(un)) {
SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
"un:0x%p is in low power\n", un);
mutex_exit(&un->un_pm_mutex);
mutex_exit(SD_MUTEX(un));
return (ECANCELED);
}
mutex_exit(&un->un_pm_mutex);
mutex_exit(SD_MUTEX(un));
#ifdef SDDEBUG
switch (dataspace) {
case UIO_USERSPACE:
SD_TRACE(SD_LOG_IO, un,
"sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
break;
case UIO_SYSSPACE:
SD_TRACE(SD_LOG_IO, un,
"sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
break;
default:
SD_TRACE(SD_LOG_IO, un,
"sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un);
break;
}
#endif
rval = scsi_uscsi_copyin((intptr_t)incmd, flag,
SD_ADDRESS(un), &uscmd);
if (rval != 0) {
SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
"scsi_uscsi_alloc_and_copyin failed\n", un);
return (rval);
}
if ((uscmd->uscsi_cdb != NULL) &&
(uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
mutex_enter(SD_MUTEX(un));
un->un_f_format_in_progress = TRUE;
mutex_exit(SD_MUTEX(un));
format = 1;
}
/*
* Allocate an sd_uscsi_info struct and fill it with the info
* needed by sd_initpkt_for_uscsi(). Then put the pointer into
* b_private in the buf for sd_initpkt_for_uscsi(). Note that
* since we allocate the buf here in this function, we do not
* need to preserve the prior contents of b_private.
* The sd_uscsi_info struct is also used by sd_uscsi_strategy()
*/
uip = ssc->ssc_uscsi_info;
uip->ui_flags = path_flag;
uip->ui_cmdp = uscmd;
/*
* Commands sent with priority are intended for error recovery
* situations, and do not have retries performed.
*/
if (path_flag == SD_PATH_DIRECT_PRIORITY) {
uscmd->uscsi_flags |= USCSI_DIAGNOSE;
}
uscmd->uscsi_flags &= ~USCSI_NOINTR;
dev = SD_GET_DEV(un);
rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
sd_uscsi_strategy, NULL, uip);
/*
* mark ssc_flags right after handle_cmd to make sure
* the uscsi has been sent
*/
ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
#ifdef SDDEBUG
SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
"uscsi_status: 0x%02x uscsi_resid:0x%x\n",
uscmd->uscsi_status, uscmd->uscsi_resid);
if (uscmd->uscsi_bufaddr != NULL) {
SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
"uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n",
uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
if (dataspace == UIO_SYSSPACE) {
SD_DUMP_MEMORY(un, SD_LOG_IO,
"data", (uchar_t *)uscmd->uscsi_bufaddr,
uscmd->uscsi_buflen, SD_LOG_HEX);
}
}
#endif
if (format == 1) {
mutex_enter(SD_MUTEX(un));
un->un_f_format_in_progress = FALSE;
mutex_exit(SD_MUTEX(un));
}
(void) scsi_uscsi_copyout((intptr_t)incmd, uscmd);
return (rval);
}
/*
* Function: sd_ssc_print
*
* Description: Print information available to the console.
*
* Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
* sd_uscsi_info in.
* sd_severity - log level.
* Context: Kernel thread or interrupt context.
*/
static void
sd_ssc_print(sd_ssc_t *ssc, int sd_severity)
{
struct uscsi_cmd *ucmdp;
struct scsi_device *devp;
dev_info_t *devinfo;
uchar_t *sensep;
int senlen;
union scsi_cdb *cdbp;
uchar_t com;
extern struct scsi_key_strings scsi_cmds[];
ASSERT(ssc != NULL);
ASSERT(ssc->ssc_un != NULL);
if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT)
return;
ucmdp = ssc->ssc_uscsi_cmd;
devp = SD_SCSI_DEVP(ssc->ssc_un);
devinfo = SD_DEVINFO(ssc->ssc_un);
ASSERT(ucmdp != NULL);
ASSERT(devp != NULL);
ASSERT(devinfo != NULL);
sensep = (uint8_t *)ucmdp->uscsi_rqbuf;
senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid;
cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb;
/* In certain case (like DOORLOCK), the cdb could be NULL. */
if (cdbp == NULL)
return;
/* We don't print log if no sense data available. */
if (senlen == 0)
sensep = NULL;
com = cdbp->scc_cmd;
scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com,
scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL);
}
/*
* Function: sd_ssc_assessment
*
* Description: We use this function to make an assessment at the point
* where SD driver may encounter a potential error.
*
* Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
* sd_uscsi_info in.
* tp_assess - a hint of strategy for ereport posting.
* Possible values of tp_assess include:
* SD_FMT_IGNORE - we don't post any ereport because we're
* sure that it is ok to ignore the underlying problems.
* SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now
* but it might be not correct to ignore the underlying hardware
* error.
* SD_FMT_STATUS_CHECK - we will post an ereport with the
* payload driver-assessment of value "fail" or
* "fatal"(depending on what information we have here). This
* assessment value is usually set when SD driver think there
* is a potential error occurred(Typically, when return value
* of the SCSI command is EIO).
* SD_FMT_STANDARD - we will post an ereport with the payload
* driver-assessment of value "info". This assessment value is
* set when the SCSI command returned successfully and with
* sense data sent back.
*
* Context: Kernel thread.
*/
static void
sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess)
{
int senlen = 0;
struct uscsi_cmd *ucmdp = NULL;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ucmdp = ssc->ssc_uscsi_cmd;
ASSERT(ucmdp != NULL);
if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT;
} else {
/*
* If enter here, it indicates that we have a wrong
* calling sequence of sd_ssc_send and sd_ssc_assessment,
* both of which should be called in a pair in case of
* loss of FMA telemetries.
*/
if (ucmdp->uscsi_cdb != NULL) {
SD_INFO(SD_LOG_SDTEST, un,
"sd_ssc_assessment is missing the "
"alternative sd_ssc_send when running 0x%x, "
"or there are superfluous sd_ssc_assessment for "
"the same sd_ssc_send.\n",
ucmdp->uscsi_cdb[0]);
}
/*
* Set the ssc_flags to the initial value to avoid passing
* down dirty flags to the following sd_ssc_send function.
*/
ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
return;
}
/*
* Only handle an issued command which is waiting for assessment.
* A command which is not issued will not have
* SSC_FLAGS_INVALID_DATA set, so it'ok we just return here.
*/
if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) {
sd_ssc_print(ssc, SCSI_ERR_INFO);
return;
} else {
/*
* For an issued command, we should clear this flag in
* order to make the sd_ssc_t structure be used off
* multiple uscsi commands.
*/
ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED;
}
/*
* We will not deal with non-retryable(flag USCSI_DIAGNOSE set)
* commands here. And we should clear the ssc_flags before return.
*/
if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) {
ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
return;
}
switch (tp_assess) {
case SD_FMT_IGNORE:
case SD_FMT_IGNORE_COMPROMISE:
break;
case SD_FMT_STATUS_CHECK:
/*
* For a failed command(including the succeeded command
* with invalid data sent back).
*/
sd_ssc_post(ssc, SD_FM_DRV_FATAL);
break;
case SD_FMT_STANDARD:
/*
* Always for the succeeded commands probably with sense
* data sent back.
* Limitation:
* We can only handle a succeeded command with sense
* data sent back when auto-request-sense is enabled.
*/
senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen -
ssc->ssc_uscsi_cmd->uscsi_rqresid;
if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) &&
(un->un_f_arq_enabled == TRUE) &&
senlen > 0 &&
ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) {
sd_ssc_post(ssc, SD_FM_DRV_NOTICE);
}
break;
default:
/*
* Should not have other type of assessment.
*/
scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
"sd_ssc_assessment got wrong "
"sd_type_assessment %d.\n", tp_assess);
break;
}
/*
* Clear up the ssc_flags before return.
*/
ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
}
/*
* Function: sd_ssc_post
*
* Description: 1. read the driver property to get fm-scsi-log flag.
* 2. print log if fm_log_capable is non-zero.
* 3. call sd_ssc_ereport_post to post ereport if possible.
*
* Context: May be called from kernel thread or interrupt context.
*/
static void
sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess)
{
struct sd_lun *un;
int sd_severity;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
/*
* We may enter here from sd_ssc_assessment(for USCSI command) or
* by directly called from sdintr context.
* We don't handle a non-disk drive(CD-ROM, removable media).
* Clear the ssc_flags before return in case we've set
* SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk
* driver.
*/
if (ISCD(un) || un->un_f_has_removable_media) {
ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
return;
}
switch (sd_assess) {
case SD_FM_DRV_FATAL:
sd_severity = SCSI_ERR_FATAL;
break;
case SD_FM_DRV_RECOVERY:
sd_severity = SCSI_ERR_RECOVERED;
break;
case SD_FM_DRV_RETRY:
sd_severity = SCSI_ERR_RETRYABLE;
break;
case SD_FM_DRV_NOTICE:
sd_severity = SCSI_ERR_INFO;
break;
default:
sd_severity = SCSI_ERR_UNKNOWN;
}
/* print log */
sd_ssc_print(ssc, sd_severity);
/* always post ereport */
sd_ssc_ereport_post(ssc, sd_assess);
}
/*
* Function: sd_ssc_set_info
*
* Description: Mark ssc_flags and set ssc_info which would be the
* payload of uderr ereport. This function will cause
* sd_ssc_ereport_post to post uderr ereport only.
* Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI),
* the function will also call SD_ERROR or scsi_log for a
* CDROM/removable-media/DDI_FM_NOT_CAPABLE device.
*
* Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
* sd_uscsi_info in.
* ssc_flags - indicate the sub-category of a uderr.
* comp - this argument is meaningful only when
* ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible
* values include:
* > 0, SD_ERROR is used with comp as the driver logging
* component;
* = 0, scsi-log is used to log error telemetries;
* < 0, no log available for this telemetry.
*
* Context: Kernel thread or interrupt context
*/
static void
sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...)
{
va_list ap;
ASSERT(ssc != NULL);
ASSERT(ssc->ssc_un != NULL);
ssc->ssc_flags |= ssc_flags;
va_start(ap, fmt);
(void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap);
va_end(ap);
/*
* If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command
* with invalid data sent back. For non-uscsi command, the
* following code will be bypassed.
*/
if (ssc_flags & SSC_FLAGS_INVALID_DATA) {
if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) {
/*
* If the error belong to certain component and we
* do not want it to show up on the console, we
* will use SD_ERROR, otherwise scsi_log is
* preferred.
*/
if (comp > 0) {
SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info);
} else if (comp == 0) {
scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label,
CE_WARN, ssc->ssc_info);
}
}
}
}
/*
* Function: sd_buf_iodone
*
* Description: Frees the sd_xbuf & returns the buf to its originator.
*
* Context: May be called from interrupt context.
*/
/* ARGSUSED */
static void
sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
{
struct sd_xbuf *xp;
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
/* xbuf is gone after this */
if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
mutex_enter(SD_MUTEX(un));
/*
* Grab time when the cmd completed.
* This is used for determining if the system has been
* idle long enough to make it idle to the PM framework.
* This is for lowering the overhead, and therefore improving
* performance per I/O operation.
*/
un->un_pm_idle_time = gethrtime();
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
SD_INFO(SD_LOG_IO, un,
"sd_buf_iodone: un_ncmds_in_driver = %ld\n",
un->un_ncmds_in_driver);
mutex_exit(SD_MUTEX(un));
}
biodone(bp); /* bp is gone after this */
SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
}
/*
* Function: sd_uscsi_iodone
*
* Description: Frees the sd_xbuf & returns the buf to its originator.
*
* Context: May be called from interrupt context.
*/
/* ARGSUSED */
static void
sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
{
struct sd_xbuf *xp;
ASSERT(un != NULL);
ASSERT(bp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
bp->b_private = xp->xb_private;
mutex_enter(SD_MUTEX(un));
/*
* Grab time when the cmd completed.
* This is used for determining if the system has been
* idle long enough to make it idle to the PM framework.
* This is for lowering the overhead, and therefore improving
* performance per I/O operation.
*/
un->un_pm_idle_time = gethrtime();
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
un->un_ncmds_in_driver);
mutex_exit(SD_MUTEX(un));
if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
SENSE_LENGTH) {
kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
MAX_SENSE_LENGTH);
} else {
kmem_free(xp, sizeof (struct sd_xbuf));
}
biodone(bp);
SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
}
/*
* Function: sd_mapblockaddr_iostart
*
* Description: Verify request lies within the partition limits for
* the indicated minor device. Issue "overrun" buf if
* request would exceed partition range. Converts
* partition-relative block address to absolute.
*
* Upon exit of this function:
* 1.I/O is aligned
* xp->xb_blkno represents the absolute sector address
* 2.I/O is misaligned
* xp->xb_blkno represents the absolute logical block address
* based on DEV_BSIZE. The logical block address will be
* converted to physical sector address in sd_mapblocksize_\
* iostart.
* 3.I/O is misaligned but is aligned in "overrun" buf
* xp->xb_blkno represents the absolute logical block address
* based on DEV_BSIZE. The logical block address will be
* converted to physical sector address in sd_mapblocksize_\
* iostart. But no RMW will be issued in this case.
*
* Context: Can sleep
*
* Issues: This follows what the old code did, in terms of accessing
* some of the partition info in the unit struct without holding
* the mutext. This is a general issue, if the partition info
* can be altered while IO is in progress... as soon as we send
* a buf, its partitioning can be invalid before it gets to the
* device. Probably the right fix is to move partitioning out
* of the driver entirely.
*/
static void
sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
{
diskaddr_t nblocks; /* #blocks in the given partition */
daddr_t blocknum; /* Block number specified by the buf */
size_t requested_nblocks;
size_t available_nblocks;
int partition;
diskaddr_t partition_offset;
struct sd_xbuf *xp;
int secmask = 0, blknomask = 0;
ushort_t is_aligned = TRUE;
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO_PARTITION, un,
"sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
/*
* If the geometry is not indicated as valid, attempt to access
* the unit & verify the geometry/label. This can be the case for
* removable-media devices, of if the device was opened in
* NDELAY/NONBLOCK mode.
*/
partition = SDPART(bp->b_edev);
if (!SD_IS_VALID_LABEL(un)) {
sd_ssc_t *ssc;
/*
* Initialize sd_ssc_t for internal uscsi commands
* In case of potential porformance issue, we need
* to alloc memory only if there is invalid label
*/
ssc = sd_ssc_init(un);
if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) {
/*
* For removable devices it is possible to start an
* I/O without a media by opening the device in nodelay
* mode. Also for writable CDs there can be many
* scenarios where there is no geometry yet but volume
* manager is trying to issue a read() just because
* it can see TOC on the CD. So do not print a message
* for removables.
*/
if (!un->un_f_has_removable_media) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"i/o to invalid geometry\n");
}
bioerror(bp, EIO);
bp->b_resid = bp->b_bcount;
SD_BEGIN_IODONE(index, un, bp);
sd_ssc_fini(ssc);
return;
}
sd_ssc_fini(ssc);
}
nblocks = 0;
(void) cmlb_partinfo(un->un_cmlbhandle, partition,
&nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT);
if (un->un_f_enable_rmw) {
blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1;
secmask = un->un_phy_blocksize - 1;
} else {
blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
secmask = un->un_tgt_blocksize - 1;
}
if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) {
is_aligned = FALSE;
}
if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) {
/*
* If I/O is aligned, no need to involve RMW(Read Modify Write)
* Convert the logical block number to target's physical sector
* number.
*/
if (is_aligned) {
xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno);
} else {
/*
* There is no RMW if we're just reading, so don't
* warn or error out because of it.
*/
if (bp->b_flags & B_READ) {
/*EMPTY*/
} else if (!un->un_f_enable_rmw &&
un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR) {
bp->b_flags |= B_ERROR;
goto error_exit;
} else if (un->un_f_rmw_type == SD_RMW_TYPE_DEFAULT) {
mutex_enter(SD_MUTEX(un));
if (!un->un_f_enable_rmw &&
un->un_rmw_msg_timeid == NULL) {
scsi_log(SD_DEVINFO(un), sd_label,
CE_WARN, "I/O request is not "
"aligned with %d disk sector size. "
"It is handled through Read Modify "
"Write but the performance is "
"very low.\n",
un->un_tgt_blocksize);
un->un_rmw_msg_timeid =
timeout(sd_rmw_msg_print_handler,
un, SD_RMW_MSG_PRINT_TIMEOUT);
} else {
un->un_rmw_incre_count ++;
}
mutex_exit(SD_MUTEX(un));
}
nblocks = SD_TGT2SYSBLOCK(un, nblocks);
partition_offset = SD_TGT2SYSBLOCK(un,
partition_offset);
}
}
/*
* blocknum is the starting block number of the request. At this
* point it is still relative to the start of the minor device.
*/
blocknum = xp->xb_blkno;
/*
* Legacy: If the starting block number is one past the last block
* in the partition, do not set B_ERROR in the buf.
*/
if (blocknum == nblocks) {
goto error_exit;
}
/*
* Confirm that the first block of the request lies within the
* partition limits. Also the requested number of bytes must be
* a multiple of the system block size.
*/
if ((blocknum < 0) || (blocknum >= nblocks) ||
((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) {
bp->b_flags |= B_ERROR;
goto error_exit;
}
/*
* If the requsted # blocks exceeds the available # blocks, that
* is an overrun of the partition.
*/
if ((!NOT_DEVBSIZE(un)) && is_aligned) {
requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
} else {
requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount);
}
available_nblocks = (size_t)(nblocks - blocknum);
ASSERT(nblocks >= blocknum);
if (requested_nblocks > available_nblocks) {
size_t resid;
/*
* Allocate an "overrun" buf to allow the request to proceed
* for the amount of space available in the partition. The
* amount not transferred will be added into the b_resid
* when the operation is complete. The overrun buf
* replaces the original buf here, and the original buf
* is saved inside the overrun buf, for later use.
*/
if ((!NOT_DEVBSIZE(un)) && is_aligned) {
resid = SD_TGTBLOCKS2BYTES(un,
(offset_t)(requested_nblocks - available_nblocks));
} else {
resid = SD_SYSBLOCKS2BYTES(
(offset_t)(requested_nblocks - available_nblocks));
}
size_t count = bp->b_bcount - resid;
/*
* Note: count is an unsigned entity thus it'll NEVER
* be less than 0 so ASSERT the original values are
* correct.
*/
ASSERT(bp->b_bcount >= resid);
bp = sd_bioclone_alloc(bp, count, blocknum,
(int (*)(struct buf *))(uintptr_t)sd_mapblockaddr_iodone);
xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
ASSERT(xp != NULL);
}
/* At this point there should be no residual for this buf. */
ASSERT(bp->b_resid == 0);
/* Convert the block number to an absolute address. */
xp->xb_blkno += partition_offset;
SD_NEXT_IOSTART(index, un, bp);
SD_TRACE(SD_LOG_IO_PARTITION, un,
"sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
return;
error_exit:
bp->b_resid = bp->b_bcount;
SD_BEGIN_IODONE(index, un, bp);
SD_TRACE(SD_LOG_IO_PARTITION, un,
"sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
}
/*
* Function: sd_mapblockaddr_iodone
*
* Description: Completion-side processing for partition management.
*
* Context: May be called under interrupt context
*/
static void
sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
{
/* int partition; */ /* Not used, see below. */
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO_PARTITION, un,
"sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
if ((uintptr_t)bp->b_iodone == (uintptr_t)sd_mapblockaddr_iodone) {
/*
* We have an "overrun" buf to deal with...
*/
struct sd_xbuf *xp;
struct buf *obp; /* ptr to the original buf */
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
/* Retrieve the pointer to the original buf */
obp = (struct buf *)xp->xb_private;
ASSERT(obp != NULL);
obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
bioerror(obp, bp->b_error);
sd_bioclone_free(bp);
/*
* Get back the original buf.
* Note that since the restoration of xb_blkno below
* was removed, the sd_xbuf is not needed.
*/
bp = obp;
/*
* xp = SD_GET_XBUF(bp);
* ASSERT(xp != NULL);
*/
}
/*
* Convert sd->xb_blkno back to a minor-device relative value.
* Note: this has been commented out, as it is not needed in the
* current implementation of the driver (ie, since this function
* is at the top of the layering chains, so the info will be
* discarded) and it is in the "hot" IO path.
*
* partition = getminor(bp->b_edev) & SDPART_MASK;
* xp->xb_blkno -= un->un_offset[partition];
*/
SD_NEXT_IODONE(index, un, bp);
SD_TRACE(SD_LOG_IO_PARTITION, un,
"sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
}
/*
* Function: sd_mapblocksize_iostart
*
* Description: Convert between system block size (un->un_sys_blocksize)
* and target block size (un->un_tgt_blocksize).
*
* Context: Can sleep to allocate resources.
*
* Assumptions: A higher layer has already performed any partition validation,
* and converted the xp->xb_blkno to an absolute value relative
* to the start of the device.
*
* It is also assumed that the higher layer has implemented
* an "overrun" mechanism for the case where the request would
* read/write beyond the end of a partition. In this case we
* assume (and ASSERT) that bp->b_resid == 0.
*
* Note: The implementation for this routine assumes the target
* block size remains constant between allocation and transport.
*/
static void
sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
{
struct sd_mapblocksize_info *bsp;
struct sd_xbuf *xp;
offset_t first_byte;
daddr_t start_block, end_block;
daddr_t request_bytes;
ushort_t is_aligned = FALSE;
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(bp->b_resid == 0);
SD_TRACE(SD_LOG_IO_RMMEDIA, un,
"sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
/*
* For a non-writable CD, a write request is an error
*/
if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
(un->un_f_mmc_writable_media == FALSE)) {
bioerror(bp, EIO);
bp->b_resid = bp->b_bcount;
SD_BEGIN_IODONE(index, un, bp);
return;
}
/*
* We do not need a shadow buf if the device is using
* un->un_sys_blocksize as its block size or if bcount == 0.
* In this case there is no layer-private data block allocated.
*/
if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
(bp->b_bcount == 0)) {
goto done;
}
#if defined(__x86)
/* We do not support non-block-aligned transfers for ROD devices */
ASSERT(!ISROD(un));
#endif
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
"tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
un->un_tgt_blocksize, DEV_BSIZE);
SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
"request start block:0x%x\n", xp->xb_blkno);
SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
"request len:0x%x\n", bp->b_bcount);
/*
* Allocate the layer-private data area for the mapblocksize layer.
* Layers are allowed to use the xp_private member of the sd_xbuf
* struct to store the pointer to their layer-private data block, but
* each layer also has the responsibility of restoring the prior
* contents of xb_private before returning the buf/xbuf to the
* higher layer that sent it.
*
* Here we save the prior contents of xp->xb_private into the
* bsp->mbs_oprivate field of our layer-private data area. This value
* is restored by sd_mapblocksize_iodone() just prior to freeing up
* the layer-private area and returning the buf/xbuf to the layer
* that sent it.
*
* Note that here we use kmem_zalloc for the allocation as there are
* parts of the mapblocksize code that expect certain fields to be
* zero unless explicitly set to a required value.
*/
bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
bsp->mbs_oprivate = xp->xb_private;
xp->xb_private = bsp;
/*
* This treats the data on the disk (target) as an array of bytes.
* first_byte is the byte offset, from the beginning of the device,
* to the location of the request. This is converted from a
* un->un_sys_blocksize block address to a byte offset, and then back
* to a block address based upon a un->un_tgt_blocksize block size.
*
* xp->xb_blkno should be absolute upon entry into this function,
* but, but it is based upon partitions that use the "system"
* block size. It must be adjusted to reflect the block size of
* the target.
*
* Note that end_block is actually the block that follows the last
* block of the request, but that's what is needed for the computation.
*/
first_byte = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
if (un->un_f_enable_rmw) {
start_block = xp->xb_blkno =
(first_byte / un->un_phy_blocksize) *
(un->un_phy_blocksize / DEV_BSIZE);
end_block = ((first_byte + bp->b_bcount +
un->un_phy_blocksize - 1) / un->un_phy_blocksize) *
(un->un_phy_blocksize / DEV_BSIZE);
} else {
start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
end_block = (first_byte + bp->b_bcount +
un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
}
/* request_bytes is rounded up to a multiple of the target block size */
request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
/*
* See if the starting address of the request and the request
* length are aligned on a un->un_tgt_blocksize boundary. If aligned
* then we do not need to allocate a shadow buf to handle the request.
*/
if (un->un_f_enable_rmw) {
if (((first_byte % un->un_phy_blocksize) == 0) &&
((bp->b_bcount % un->un_phy_blocksize) == 0)) {
is_aligned = TRUE;
}
} else {
if (((first_byte % un->un_tgt_blocksize) == 0) &&
((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
is_aligned = TRUE;
}
}
if ((bp->b_flags & B_READ) == 0) {
/*
* Lock the range for a write operation. An aligned request is
* considered a simple write; otherwise the request must be a
* read-modify-write.
*/
bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
(is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
}
/*
* Alloc a shadow buf if the request is not aligned. Also, this is
* where the READ command is generated for a read-modify-write. (The
* write phase is deferred until after the read completes.)
*/
if (is_aligned == FALSE) {
struct sd_mapblocksize_info *shadow_bsp;
struct sd_xbuf *shadow_xp;
struct buf *shadow_bp;
/*
* Allocate the shadow buf and it associated xbuf. Note that
* after this call the xb_blkno value in both the original
* buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
* same: absolute relative to the start of the device, and
* adjusted for the target block size. The b_blkno in the
* shadow buf will also be set to this value. We should never
* change b_blkno in the original bp however.
*
* Note also that the shadow buf will always need to be a
* READ command, regardless of whether the incoming command
* is a READ or a WRITE.
*/
shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
xp->xb_blkno,
(int (*)(struct buf *))(uintptr_t)sd_mapblocksize_iodone);
shadow_xp = SD_GET_XBUF(shadow_bp);
/*
* Allocate the layer-private data for the shadow buf.
* (No need to preserve xb_private in the shadow xbuf.)
*/
shadow_xp->xb_private = shadow_bsp =
kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
/*
* bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
* to figure out where the start of the user data is (based upon
* the system block size) in the data returned by the READ
* command (which will be based upon the target blocksize). Note
* that this is only really used if the request is unaligned.
*/
if (un->un_f_enable_rmw) {
bsp->mbs_copy_offset = (ssize_t)(first_byte -
((offset_t)xp->xb_blkno * un->un_sys_blocksize));
ASSERT((bsp->mbs_copy_offset >= 0) &&
(bsp->mbs_copy_offset < un->un_phy_blocksize));
} else {
bsp->mbs_copy_offset = (ssize_t)(first_byte -
((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
ASSERT((bsp->mbs_copy_offset >= 0) &&
(bsp->mbs_copy_offset < un->un_tgt_blocksize));
}
shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
/* Transfer the wmap (if any) to the shadow buf */
shadow_bsp->mbs_wmp = bsp->mbs_wmp;
bsp->mbs_wmp = NULL;
/*
* The shadow buf goes on from here in place of the
* original buf.
*/
shadow_bsp->mbs_orig_bp = bp;
bp = shadow_bp;
}
SD_INFO(SD_LOG_IO_RMMEDIA, un,
"sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
SD_INFO(SD_LOG_IO_RMMEDIA, un,
"sd_mapblocksize_iostart: tgt request len:0x%x\n",
request_bytes);
SD_INFO(SD_LOG_IO_RMMEDIA, un,
"sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
done:
SD_NEXT_IOSTART(index, un, bp);
SD_TRACE(SD_LOG_IO_RMMEDIA, un,
"sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
}
/*
* Function: sd_mapblocksize_iodone
*
* Description: Completion side processing for block-size mapping.
*
* Context: May be called under interrupt context
*/
static void
sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
{
struct sd_mapblocksize_info *bsp;
struct sd_xbuf *xp;
struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */
struct buf *orig_bp; /* ptr to the original buf */
offset_t shadow_end;
offset_t request_end;
offset_t shadow_start;
ssize_t copy_offset;
size_t copy_length;
size_t shortfall;
uint_t is_write; /* TRUE if this bp is a WRITE */
uint_t has_wmap; /* TRUE is this bp has a wmap */
ASSERT(un != NULL);
ASSERT(bp != NULL);
SD_TRACE(SD_LOG_IO_RMMEDIA, un,
"sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
/*
* There is no shadow buf or layer-private data if the target is
* using un->un_sys_blocksize as its block size or if bcount == 0.
*/
if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
(bp->b_bcount == 0)) {
goto exit;
}
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
/* Retrieve the pointer to the layer-private data area from the xbuf. */
bsp = xp->xb_private;
is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
if (is_write) {
/*
* For a WRITE request we must free up the block range that
* we have locked up. This holds regardless of whether this is
* an aligned write request or a read-modify-write request.
*/
sd_range_unlock(un, bsp->mbs_wmp);
bsp->mbs_wmp = NULL;
}
if ((uintptr_t)bp->b_iodone != (uintptr_t)sd_mapblocksize_iodone) {
/*
* An aligned read or write command will have no shadow buf;
* there is not much else to do with it.
*/
goto done;
}
orig_bp = bsp->mbs_orig_bp;
ASSERT(orig_bp != NULL);
orig_xp = SD_GET_XBUF(orig_bp);
ASSERT(orig_xp != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
if (!is_write && has_wmap) {
/*
* A READ with a wmap means this is the READ phase of a
* read-modify-write. If an error occurred on the READ then
* we do not proceed with the WRITE phase or copy any data.
* Just release the write maps and return with an error.
*/
if ((bp->b_resid != 0) || (bp->b_error != 0)) {
orig_bp->b_resid = orig_bp->b_bcount;
bioerror(orig_bp, bp->b_error);
sd_range_unlock(un, bsp->mbs_wmp);
goto freebuf_done;
}
}
/*
* Here is where we set up to copy the data from the shadow buf
* into the space associated with the original buf.
*
* To deal with the conversion between block sizes, these
* computations treat the data as an array of bytes, with the
* first byte (byte 0) corresponding to the first byte in the
* first block on the disk.
*/
/*
* shadow_start and shadow_len indicate the location and size of
* the data returned with the shadow IO request.
*/
if (un->un_f_enable_rmw) {
shadow_start = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
} else {
shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
}
shadow_end = shadow_start + bp->b_bcount - bp->b_resid;
/*
* copy_offset gives the offset (in bytes) from the start of the first
* block of the READ request to the beginning of the data. We retrieve
* this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
* there by sd_mapblockize_iostart(). copy_length gives the amount of
* data to be copied (in bytes).
*/
copy_offset = bsp->mbs_copy_offset;
if (un->un_f_enable_rmw) {
ASSERT((copy_offset >= 0) &&
(copy_offset < un->un_phy_blocksize));
} else {
ASSERT((copy_offset >= 0) &&
(copy_offset < un->un_tgt_blocksize));
}
copy_length = orig_bp->b_bcount;
request_end = shadow_start + copy_offset + orig_bp->b_bcount;
/*
* Set up the resid and error fields of orig_bp as appropriate.
*/
if (shadow_end >= request_end) {
/* We got all the requested data; set resid to zero */
orig_bp->b_resid = 0;
} else {
/*
* We failed to get enough data to fully satisfy the original
* request. Just copy back whatever data we got and set
* up the residual and error code as required.
*
* 'shortfall' is the amount by which the data received with the
* shadow buf has "fallen short" of the requested amount.
*/
shortfall = (size_t)(request_end - shadow_end);
if (shortfall > orig_bp->b_bcount) {
/*
* We did not get enough data to even partially
* fulfill the original request. The residual is
* equal to the amount requested.
*/
orig_bp->b_resid = orig_bp->b_bcount;
} else {
/*
* We did not get all the data that we requested
* from the device, but we will try to return what
* portion we did get.
*/
orig_bp->b_resid = shortfall;
}
ASSERT(copy_length >= orig_bp->b_resid);
copy_length -= orig_bp->b_resid;
}
/* Propagate the error code from the shadow buf to the original buf */
bioerror(orig_bp, bp->b_error);
if (is_write) {
goto freebuf_done; /* No data copying for a WRITE */
}
if (has_wmap) {
/*
* This is a READ command from the READ phase of a
* read-modify-write request. We have to copy the data given
* by the user OVER the data returned by the READ command,
* then convert the command from a READ to a WRITE and send
* it back to the target.
*/
bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
copy_length);
bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */
/*
* Dispatch the WRITE command to the taskq thread, which
* will in turn send the command to the target. When the
* WRITE command completes, we (sd_mapblocksize_iodone())
* will get called again as part of the iodone chain
* processing for it. Note that we will still be dealing
* with the shadow buf at that point.
*/
if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
KM_NOSLEEP) != TASKQID_INVALID) {
/*
* Dispatch was successful so we are done. Return
* without going any higher up the iodone chain. Do
* not free up any layer-private data until after the
* WRITE completes.
*/
return;
}
/*
* Dispatch of the WRITE command failed; set up the error
* condition and send this IO back up the iodone chain.
*/
bioerror(orig_bp, EIO);
orig_bp->b_resid = orig_bp->b_bcount;
} else {
/*
* This is a regular READ request (ie, not a RMW). Copy the
* data from the shadow buf into the original buf. The
* copy_offset compensates for any "misalignment" between the
* shadow buf (with its un->un_tgt_blocksize blocks) and the
* original buf (with its un->un_sys_blocksize blocks).
*/
bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
copy_length);
}
freebuf_done:
/*
* At this point we still have both the shadow buf AND the original
* buf to deal with, as well as the layer-private data area in each.
* Local variables are as follows:
*
* bp -- points to shadow buf
* xp -- points to xbuf of shadow buf
* bsp -- points to layer-private data area of shadow buf
* orig_bp -- points to original buf
*
* First free the shadow buf and its associated xbuf, then free the
* layer-private data area from the shadow buf. There is no need to
* restore xb_private in the shadow xbuf.
*/
sd_shadow_buf_free(bp);
kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
/*
* Now update the local variables to point to the original buf, xbuf,
* and layer-private area.
*/
bp = orig_bp;
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
ASSERT(xp == orig_xp);
bsp = xp->xb_private;
ASSERT(bsp != NULL);
done:
/*
* Restore xb_private to whatever it was set to by the next higher
* layer in the chain, then free the layer-private data area.
*/
xp->xb_private = bsp->mbs_oprivate;
kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
exit:
SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
"sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
SD_NEXT_IODONE(index, un, bp);
}
/*
* Function: sd_checksum_iostart
*
* Description: A stub function for a layer that's currently not used.
* For now just a placeholder.
*
* Context: Kernel thread context
*/
static void
sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
{
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_NEXT_IOSTART(index, un, bp);
}
/*
* Function: sd_checksum_iodone
*
* Description: A stub function for a layer that's currently not used.
* For now just a placeholder.
*
* Context: May be called under interrupt context
*/
static void
sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
{
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_NEXT_IODONE(index, un, bp);
}
/*
* Function: sd_checksum_uscsi_iostart
*
* Description: A stub function for a layer that's currently not used.
* For now just a placeholder.
*
* Context: Kernel thread context
*/
static void
sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
{
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_NEXT_IOSTART(index, un, bp);
}
/*
* Function: sd_checksum_uscsi_iodone
*
* Description: A stub function for a layer that's currently not used.
* For now just a placeholder.
*
* Context: May be called under interrupt context
*/
static void
sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
{
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_NEXT_IODONE(index, un, bp);
}
/*
* Function: sd_pm_iostart
*
* Description: iostart-side routine for Power mangement.
*
* Context: Kernel thread context
*/
static void
sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
{
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(!mutex_owned(&un->un_pm_mutex));
SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
if (sd_pm_entry(un) != DDI_SUCCESS) {
/*
* Set up to return the failed buf back up the 'iodone'
* side of the calling chain.
*/
bioerror(bp, EIO);
bp->b_resid = bp->b_bcount;
SD_BEGIN_IODONE(index, un, bp);
SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
return;
}
SD_NEXT_IOSTART(index, un, bp);
SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
}
/*
* Function: sd_pm_iodone
*
* Description: iodone-side routine for power mangement.
*
* Context: may be called from interrupt context
*/
static void
sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
{
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(!mutex_owned(&un->un_pm_mutex));
SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
/*
* After attach the following flag is only read, so don't
* take the penalty of acquiring a mutex for it.
*/
if (un->un_f_pm_is_enabled == TRUE) {
sd_pm_exit(un);
}
SD_NEXT_IODONE(index, un, bp);
SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
}
/*
* Function: sd_core_iostart
*
* Description: Primary driver function for enqueuing buf(9S) structs from
* the system and initiating IO to the target device
*
* Context: Kernel thread context. Can sleep.
*
* Assumptions: - The given xp->xb_blkno is absolute
* (ie, relative to the start of the device).
* - The IO is to be done using the native blocksize of
* the device, as specified in un->un_tgt_blocksize.
*/
/* ARGSUSED */
static void
sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
{
struct sd_xbuf *xp;
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(bp->b_resid == 0);
SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
mutex_enter(SD_MUTEX(un));
/*
* If we are currently in the failfast state, fail any new IO
* that has B_FAILFAST set, then return.
*/
if ((bp->b_flags & B_FAILFAST) &&
(un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
mutex_exit(SD_MUTEX(un));
bioerror(bp, EIO);
bp->b_resid = bp->b_bcount;
SD_BEGIN_IODONE(index, un, bp);
return;
}
if (SD_IS_DIRECT_PRIORITY(xp)) {
/*
* Priority command -- transport it immediately.
*
* Note: We may want to assert that USCSI_DIAGNOSE is set,
* because all direct priority commands should be associated
* with error recovery actions which we don't want to retry.
*/
sd_start_cmds(un, bp);
} else {
/*
* Normal command -- add it to the wait queue, then start
* transporting commands from the wait queue.
*/
sd_add_buf_to_waitq(un, bp);
SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
sd_start_cmds(un, NULL);
}
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
}
/*
* Function: sd_init_cdb_limits
*
* Description: This is to handle scsi_pkt initialization differences
* between the driver platforms.
*
* Legacy behaviors:
*
* If the block number or the sector count exceeds the
* capabilities of a Group 0 command, shift over to a
* Group 1 command. We don't blindly use Group 1
* commands because a) some drives (CDC Wren IVs) get a
* bit confused, and b) there is probably a fair amount
* of speed difference for a target to receive and decode
* a 10 byte command instead of a 6 byte command.
*
* The xfer time difference of 6 vs 10 byte CDBs is
* still significant so this code is still worthwhile.
* 10 byte CDBs are very inefficient with the fas HBA driver
* and older disks. Each CDB byte took 1 usec with some
* popular disks.
*
* Context: Must be called at attach time
*/
static void
sd_init_cdb_limits(struct sd_lun *un)
{
int hba_cdb_limit;
/*
* Use CDB_GROUP1 commands for most devices except for
* parallel SCSI fixed drives in which case we get better
* performance using CDB_GROUP0 commands (where applicable).
*/
un->un_mincdb = SD_CDB_GROUP1;
if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
!un->un_f_has_removable_media) {
un->un_mincdb = SD_CDB_GROUP0;
}
/*
* Try to read the max-cdb-length supported by HBA.
*/
un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
if (0 >= un->un_max_hba_cdb) {
un->un_max_hba_cdb = CDB_GROUP4;
hba_cdb_limit = SD_CDB_GROUP4;
} else if (0 < un->un_max_hba_cdb &&
un->un_max_hba_cdb < CDB_GROUP1) {
hba_cdb_limit = SD_CDB_GROUP0;
} else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
un->un_max_hba_cdb < CDB_GROUP5) {
hba_cdb_limit = SD_CDB_GROUP1;
} else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
un->un_max_hba_cdb < CDB_GROUP4) {
hba_cdb_limit = SD_CDB_GROUP5;
} else {
hba_cdb_limit = SD_CDB_GROUP4;
}
/*
* Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4
* commands for fixed disks.
*/
un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
min(hba_cdb_limit, SD_CDB_GROUP4);
un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
? sizeof (struct scsi_arq_status) : 1);
if (!ISCD(un))
un->un_cmd_timeout = (ushort_t)sd_io_time;
un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
}
/*
* Function: sd_initpkt_for_buf
*
* Description: Allocate and initialize for transport a scsi_pkt struct,
* based upon the info specified in the given buf struct.
*
* Assumes the xb_blkno in the request is absolute (ie,
* relative to the start of the device (NOT partition!).
* Also assumes that the request is using the native block
* size of the device (as returned by the READ CAPACITY
* command).
*
* Return Code: SD_PKT_ALLOC_SUCCESS
* SD_PKT_ALLOC_FAILURE
* SD_PKT_ALLOC_FAILURE_NO_DMA
* SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
*
* Context: Kernel thread and may be called from software interrupt context
* as part of a sdrunout callback. This function may not block or
* call routines that block
*/
static int
sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
{
struct sd_xbuf *xp;
struct scsi_pkt *pktp = NULL;
struct sd_lun *un;
size_t blockcount;
daddr_t startblock;
int rval;
int cmd_flags;
ASSERT(bp != NULL);
ASSERT(pktpp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
un = SD_GET_UN(bp);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp->b_resid == 0);
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
mutex_exit(SD_MUTEX(un));
#if defined(__x86) /* DMAFREE for x86 only */
if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
/*
* Already have a scsi_pkt -- just need DMA resources.
* We must recompute the CDB in case the mapping returns
* a nonzero pkt_resid.
* Note: if this is a portion of a PKT_DMA_PARTIAL transfer
* that is being retried, the unmap/remap of the DMA resouces
* will result in the entire transfer starting over again
* from the very first block.
*/
ASSERT(xp->xb_pktp != NULL);
pktp = xp->xb_pktp;
} else {
pktp = NULL;
}
#endif /* __x86 */
startblock = xp->xb_blkno; /* Absolute block num. */
blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
/*
* sd_setup_rw_pkt will determine the appropriate CDB group to use,
* call scsi_init_pkt, and build the CDB.
*/
rval = sd_setup_rw_pkt(un, &pktp, bp,
cmd_flags, sdrunout, (caddr_t)un,
startblock, blockcount);
if (rval == 0) {
/*
* Success.
*
* If partial DMA is being used and required for this transfer.
* set it up here.
*/
if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
(pktp->pkt_resid != 0)) {
/*
* Save the CDB length and pkt_resid for the
* next xfer
*/
xp->xb_dma_resid = pktp->pkt_resid;
/* rezero resid */
pktp->pkt_resid = 0;
} else {
xp->xb_dma_resid = 0;
}
pktp->pkt_flags = un->un_tagflags;
pktp->pkt_time = un->un_cmd_timeout;
pktp->pkt_comp = sdintr;
pktp->pkt_private = bp;
*pktpp = pktp;
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
#if defined(__x86) /* DMAFREE for x86 only */
xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
#endif
mutex_enter(SD_MUTEX(un));
return (SD_PKT_ALLOC_SUCCESS);
}
/*
* SD_PKT_ALLOC_FAILURE is the only expected failure code
* from sd_setup_rw_pkt.
*/
ASSERT(rval == SD_PKT_ALLOC_FAILURE);
if (rval == SD_PKT_ALLOC_FAILURE) {
*pktpp = NULL;
/*
* Set the driver state to RWAIT to indicate the driver
* is waiting on resource allocations. The driver will not
* suspend, pm_suspend, or detatch while the state is RWAIT.
*/
mutex_enter(SD_MUTEX(un));
New_state(un, SD_STATE_RWAIT);
SD_ERROR(SD_LOG_IO_CORE, un,
"sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
if ((bp->b_flags & B_ERROR) != 0) {
return (SD_PKT_ALLOC_FAILURE_NO_DMA);
}
return (SD_PKT_ALLOC_FAILURE);
} else {
/*
* PKT_ALLOC_FAILURE_CDB_TOO_SMALL
*
* This should never happen. Maybe someone messed with the
* kernel's minphys?
*/
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"Request rejected: too large for CDB: "
"lba:0x%08lx len:0x%08lx\n", startblock, blockcount);
SD_ERROR(SD_LOG_IO_CORE, un,
"sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
mutex_enter(SD_MUTEX(un));
return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
}
}
/*
* Function: sd_destroypkt_for_buf
*
* Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
*
* Context: Kernel thread or interrupt context
*/
static void
sd_destroypkt_for_buf(struct buf *bp)
{
ASSERT(bp != NULL);
ASSERT(SD_GET_UN(bp) != NULL);
SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
"sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
ASSERT(SD_GET_PKTP(bp) != NULL);
scsi_destroy_pkt(SD_GET_PKTP(bp));
SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
"sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
}
/*
* Function: sd_setup_rw_pkt
*
* Description: Determines appropriate CDB group for the requested LBA
* and transfer length, calls scsi_init_pkt, and builds
* the CDB. Do not use for partial DMA transfers except
* for the initial transfer since the CDB size must
* remain constant.
*
* Context: Kernel thread and may be called from software interrupt
* context as part of a sdrunout callback. This function may not
* block or call routines that block
*/
int
sd_setup_rw_pkt(struct sd_lun *un,
struct scsi_pkt **pktpp, struct buf *bp, int flags,
int (*callback)(caddr_t), caddr_t callback_arg,
diskaddr_t lba, uint32_t blockcount)
{
struct scsi_pkt *return_pktp;
union scsi_cdb *cdbp;
struct sd_cdbinfo *cp = NULL;
int i;
/*
* See which size CDB to use, based upon the request.
*/
for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
/*
* Check lba and block count against sd_cdbtab limits.
* In the partial DMA case, we have to use the same size
* CDB for all the transfers. Check lba + blockcount
* against the max LBA so we know that segment of the
* transfer can use the CDB we select.
*/
if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
(blockcount <= sd_cdbtab[i].sc_maxlen)) {
/*
* The command will fit into the CDB type
* specified by sd_cdbtab[i].
*/
cp = sd_cdbtab + i;
/*
* Call scsi_init_pkt so we can fill in the
* CDB.
*/
return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
bp, cp->sc_grpcode, un->un_status_len, 0,
flags, callback, callback_arg);
if (return_pktp != NULL) {
/*
* Return new value of pkt
*/
*pktpp = return_pktp;
/*
* To be safe, zero the CDB insuring there is
* no leftover data from a previous command.
*/
bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
/*
* Handle partial DMA mapping
*/
if (return_pktp->pkt_resid != 0) {
/*
* Not going to xfer as many blocks as
* originally expected
*/
blockcount -=
SD_BYTES2TGTBLOCKS(un,
return_pktp->pkt_resid);
}
cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
/*
* Set command byte based on the CDB
* type we matched.
*/
cdbp->scc_cmd = cp->sc_grpmask |
((bp->b_flags & B_READ) ?
SCMD_READ : SCMD_WRITE);
SD_FILL_SCSI1_LUN(un, return_pktp);
/*
* Fill in LBA and length
*/
ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
(cp->sc_grpcode == CDB_GROUP4) ||
(cp->sc_grpcode == CDB_GROUP0) ||
(cp->sc_grpcode == CDB_GROUP5));
if (cp->sc_grpcode == CDB_GROUP1) {
FORMG1ADDR(cdbp, lba);
FORMG1COUNT(cdbp, blockcount);
return (0);
} else if (cp->sc_grpcode == CDB_GROUP4) {
FORMG4LONGADDR(cdbp, lba);
FORMG4COUNT(cdbp, blockcount);
return (0);
} else if (cp->sc_grpcode == CDB_GROUP0) {
FORMG0ADDR(cdbp, lba);
FORMG0COUNT(cdbp, blockcount);
return (0);
} else if (cp->sc_grpcode == CDB_GROUP5) {
FORMG5ADDR(cdbp, lba);
FORMG5COUNT(cdbp, blockcount);
return (0);
}
/*
* It should be impossible to not match one
* of the CDB types above, so we should never
* reach this point. Set the CDB command byte
* to test-unit-ready to avoid writing
* to somewhere we don't intend.
*/
cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
} else {
/*
* Couldn't get scsi_pkt
*/
return (SD_PKT_ALLOC_FAILURE);
}
}
}
/*
* None of the available CDB types were suitable. This really
* should never happen: on a 64 bit system we support
* READ16/WRITE16 which will hold an entire 64 bit disk address
* and on a 32 bit system we will refuse to bind to a device
* larger than 2TB so addresses will never be larger than 32 bits.
*/
return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
}
/*
* Function: sd_setup_next_rw_pkt
*
* Description: Setup packet for partial DMA transfers, except for the
* initial transfer. sd_setup_rw_pkt should be used for
* the initial transfer.
*
* Context: Kernel thread and may be called from interrupt context.
*/
int
sd_setup_next_rw_pkt(struct sd_lun *un,
struct scsi_pkt *pktp, struct buf *bp,
diskaddr_t lba, uint32_t blockcount)
{
uchar_t com;
union scsi_cdb *cdbp;
uchar_t cdb_group_id;
ASSERT(pktp != NULL);
ASSERT(pktp->pkt_cdbp != NULL);
cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
com = cdbp->scc_cmd;
cdb_group_id = CDB_GROUPID(com);
ASSERT((cdb_group_id == CDB_GROUPID_0) ||
(cdb_group_id == CDB_GROUPID_1) ||
(cdb_group_id == CDB_GROUPID_4) ||
(cdb_group_id == CDB_GROUPID_5));
/*
* Move pkt to the next portion of the xfer.
* func is NULL_FUNC so we do not have to release
* the disk mutex here.
*/
if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
NULL_FUNC, NULL) == pktp) {
/* Success. Handle partial DMA */
if (pktp->pkt_resid != 0) {
blockcount -=
SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
}
cdbp->scc_cmd = com;
SD_FILL_SCSI1_LUN(un, pktp);
if (cdb_group_id == CDB_GROUPID_1) {
FORMG1ADDR(cdbp, lba);
FORMG1COUNT(cdbp, blockcount);
return (0);
} else if (cdb_group_id == CDB_GROUPID_4) {
FORMG4LONGADDR(cdbp, lba);
FORMG4COUNT(cdbp, blockcount);
return (0);
} else if (cdb_group_id == CDB_GROUPID_0) {
FORMG0ADDR(cdbp, lba);
FORMG0COUNT(cdbp, blockcount);
return (0);
} else if (cdb_group_id == CDB_GROUPID_5) {
FORMG5ADDR(cdbp, lba);
FORMG5COUNT(cdbp, blockcount);
return (0);
}
/* Unreachable */
return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
}
/*
* Error setting up next portion of cmd transfer.
* Something is definitely very wrong and this
* should not happen.
*/
return (SD_PKT_ALLOC_FAILURE);
}
/*
* Function: sd_initpkt_for_uscsi
*
* Description: Allocate and initialize for transport a scsi_pkt struct,
* based upon the info specified in the given uscsi_cmd struct.
*
* Return Code: SD_PKT_ALLOC_SUCCESS
* SD_PKT_ALLOC_FAILURE
* SD_PKT_ALLOC_FAILURE_NO_DMA
* SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
*
* Context: Kernel thread and may be called from software interrupt context
* as part of a sdrunout callback. This function may not block or
* call routines that block
*/
static int
sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
{
struct uscsi_cmd *uscmd;
struct sd_xbuf *xp;
struct scsi_pkt *pktp;
struct sd_lun *un;
uint32_t flags = 0;
ASSERT(bp != NULL);
ASSERT(pktpp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
un = SD_GET_UN(bp);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
ASSERT(uscmd != NULL);
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
/*
* Allocate the scsi_pkt for the command.
*
* Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
* during scsi_init_pkt time and will continue to use the
* same path as long as the same scsi_pkt is used without
* intervening scsi_dmafree(). Since uscsi command does
* not call scsi_dmafree() before retry failed command, it
* is necessary to make sure PKT_DMA_PARTIAL flag is NOT
* set such that scsi_vhci can use other available path for
* retry. Besides, ucsci command does not allow DMA breakup,
* so there is no need to set PKT_DMA_PARTIAL flag.
*
* More fundamentally, we can't support breaking up this DMA into
* multiple windows on x86. There is, in general, no guarantee
* that arbitrary SCSI commands are idempotent, which is required
* if we want to use multiple windows for a given command.
*/
if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status)
- sizeof (struct scsi_extended_sense)), 0,
(un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ,
sdrunout, (caddr_t)un);
} else {
pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
sizeof (struct scsi_arq_status), 0,
(un->un_pkt_flags & ~PKT_DMA_PARTIAL),
sdrunout, (caddr_t)un);
}
if (pktp == NULL) {
*pktpp = NULL;
/*
* Set the driver state to RWAIT to indicate the driver
* is waiting on resource allocations. The driver will not
* suspend, pm_suspend, or detatch while the state is RWAIT.
*/
New_state(un, SD_STATE_RWAIT);
SD_ERROR(SD_LOG_IO_CORE, un,
"sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
if ((bp->b_flags & B_ERROR) != 0) {
return (SD_PKT_ALLOC_FAILURE_NO_DMA);
}
return (SD_PKT_ALLOC_FAILURE);
}
/*
* We do not do DMA breakup for USCSI commands, so return failure
* here if all the needed DMA resources were not allocated.
*/
if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
(bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
scsi_destroy_pkt(pktp);
SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
"No partial DMA for USCSI. exit: buf:0x%p\n", bp);
return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
}
/* Init the cdb from the given uscsi struct */
(void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
uscmd->uscsi_cdb[0], 0, 0, 0);
SD_FILL_SCSI1_LUN(un, pktp);
/*
* Set up the optional USCSI flags. See the uscsi(4I) man page
* for listing of the supported flags.
*/
if (uscmd->uscsi_flags & USCSI_SILENT) {
flags |= FLAG_SILENT;
}
if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
flags |= FLAG_DIAGNOSE;
}
if (uscmd->uscsi_flags & USCSI_ISOLATE) {
flags |= FLAG_ISOLATE;
}
if (un->un_f_is_fibre == FALSE) {
if (uscmd->uscsi_flags & USCSI_RENEGOT) {
flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
}
}
/*
* Set the pkt flags here so we save time later.
* Note: These flags are NOT in the uscsi man page!!!
*/
if (uscmd->uscsi_flags & USCSI_HEAD) {
flags |= FLAG_HEAD;
}
if (uscmd->uscsi_flags & USCSI_NOINTR) {
flags |= FLAG_NOINTR;
}
/*
* For tagged queueing, things get a bit complicated.
* Check first for head of queue and last for ordered queue.
* If neither head nor order, use the default driver tag flags.
*/
if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
if (uscmd->uscsi_flags & USCSI_HTAG) {
flags |= FLAG_HTAG;
} else if (uscmd->uscsi_flags & USCSI_OTAG) {
flags |= FLAG_OTAG;
} else {
flags |= un->un_tagflags & FLAG_TAGMASK;
}
}
if (uscmd->uscsi_flags & USCSI_NODISCON) {
flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
}
pktp->pkt_flags = flags;
/* Transfer uscsi information to scsi_pkt */
(void) scsi_uscsi_pktinit(uscmd, pktp);
/* Copy the caller's CDB into the pkt... */
bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
if (uscmd->uscsi_timeout == 0) {
pktp->pkt_time = un->un_uscsi_timeout;
} else {
pktp->pkt_time = uscmd->uscsi_timeout;
}
/* need it later to identify USCSI request in sdintr */
xp->xb_pkt_flags |= SD_XB_USCSICMD;
xp->xb_sense_resid = uscmd->uscsi_rqresid;
pktp->pkt_private = bp;
pktp->pkt_comp = sdintr;
*pktpp = pktp;
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
return (SD_PKT_ALLOC_SUCCESS);
}
/*
* Function: sd_destroypkt_for_uscsi
*
* Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
* IOs.. Also saves relevant info into the associated uscsi_cmd
* struct.
*
* Context: May be called under interrupt context
*/
static void
sd_destroypkt_for_uscsi(struct buf *bp)
{
struct uscsi_cmd *uscmd;
struct sd_xbuf *xp;
struct scsi_pkt *pktp;
struct sd_lun *un;
struct sd_uscsi_info *suip;
ASSERT(bp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
un = SD_GET_UN(bp);
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
pktp = SD_GET_PKTP(bp);
ASSERT(pktp != NULL);
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
ASSERT(uscmd != NULL);
/* Save the status and the residual into the uscsi_cmd struct */
uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
uscmd->uscsi_resid = bp->b_resid;
/* Transfer scsi_pkt information to uscsi */
(void) scsi_uscsi_pktfini(pktp, uscmd);
/*
* If enabled, copy any saved sense data into the area specified
* by the uscsi command.
*/
if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
(uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
/*
* Note: uscmd->uscsi_rqbuf should always point to a buffer
* at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
*/
uscmd->uscsi_rqstatus = xp->xb_sense_status;
uscmd->uscsi_rqresid = xp->xb_sense_resid;
if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
MAX_SENSE_LENGTH);
} else {
bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
SENSE_LENGTH);
}
}
/*
* The following assignments are for SCSI FMA.
*/
ASSERT(xp->xb_private != NULL);
suip = (struct sd_uscsi_info *)xp->xb_private;
suip->ui_pkt_reason = pktp->pkt_reason;
suip->ui_pkt_state = pktp->pkt_state;
suip->ui_pkt_statistics = pktp->pkt_statistics;
suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
/* We are done with the scsi_pkt; free it now */
ASSERT(SD_GET_PKTP(bp) != NULL);
scsi_destroy_pkt(SD_GET_PKTP(bp));
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
}
/*
* Function: sd_bioclone_alloc
*
* Description: Allocate a buf(9S) and init it as per the given buf
* and the various arguments. The associated sd_xbuf
* struct is (nearly) duplicated. The struct buf *bp
* argument is saved in new_xp->xb_private.
*
* Arguments: bp - ptr the the buf(9S) to be "shadowed"
* datalen - size of data area for the shadow bp
* blkno - starting LBA
* func - function pointer for b_iodone in the shadow buf. (May
* be NULL if none.)
*
* Return Code: Pointer to allocates buf(9S) struct
*
* Context: Can sleep.
*/
static struct buf *
sd_bioclone_alloc(struct buf *bp, size_t datalen, daddr_t blkno,
int (*func)(struct buf *))
{
struct sd_lun *un;
struct sd_xbuf *xp;
struct sd_xbuf *new_xp;
struct buf *new_bp;
ASSERT(bp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
un = SD_GET_UN(bp);
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
NULL, KM_SLEEP);
new_bp->b_lblkno = blkno;
/*
* Allocate an xbuf for the shadow bp and copy the contents of the
* original xbuf into it.
*/
new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
bcopy(xp, new_xp, sizeof (struct sd_xbuf));
/*
* The given bp is automatically saved in the xb_private member
* of the new xbuf. Callers are allowed to depend on this.
*/
new_xp->xb_private = bp;
new_bp->b_private = new_xp;
return (new_bp);
}
/*
* Function: sd_shadow_buf_alloc
*
* Description: Allocate a buf(9S) and init it as per the given buf
* and the various arguments. The associated sd_xbuf
* struct is (nearly) duplicated. The struct buf *bp
* argument is saved in new_xp->xb_private.
*
* Arguments: bp - ptr the the buf(9S) to be "shadowed"
* datalen - size of data area for the shadow bp
* bflags - B_READ or B_WRITE (pseudo flag)
* blkno - starting LBA
* func - function pointer for b_iodone in the shadow buf. (May
* be NULL if none.)
*
* Return Code: Pointer to allocates buf(9S) struct
*
* Context: Can sleep.
*/
static struct buf *
sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
daddr_t blkno, int (*func)(struct buf *))
{
struct sd_lun *un;
struct sd_xbuf *xp;
struct sd_xbuf *new_xp;
struct buf *new_bp;
ASSERT(bp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
un = SD_GET_UN(bp);
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
bp_mapin(bp);
}
bflags &= (B_READ | B_WRITE);
#if defined(__x86)
new_bp = getrbuf(KM_SLEEP);
new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
new_bp->b_bcount = datalen;
new_bp->b_flags = bflags |
(bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
#else
new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
datalen, bflags, SLEEP_FUNC, NULL);
#endif
new_bp->av_forw = NULL;
new_bp->av_back = NULL;
new_bp->b_dev = bp->b_dev;
new_bp->b_blkno = blkno;
new_bp->b_iodone = func;
new_bp->b_edev = bp->b_edev;
new_bp->b_resid = 0;
/* We need to preserve the B_FAILFAST flag */
if (bp->b_flags & B_FAILFAST) {
new_bp->b_flags |= B_FAILFAST;
}
/*
* Allocate an xbuf for the shadow bp and copy the contents of the
* original xbuf into it.
*/
new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
bcopy(xp, new_xp, sizeof (struct sd_xbuf));
/* Need later to copy data between the shadow buf & original buf! */
new_xp->xb_pkt_flags |= PKT_CONSISTENT;
/*
* The given bp is automatically saved in the xb_private member
* of the new xbuf. Callers are allowed to depend on this.
*/
new_xp->xb_private = bp;
new_bp->b_private = new_xp;
return (new_bp);
}
/*
* Function: sd_bioclone_free
*
* Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
* in the larger than partition operation.
*
* Context: May be called under interrupt context
*/
static void
sd_bioclone_free(struct buf *bp)
{
struct sd_xbuf *xp;
ASSERT(bp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
/*
* Call bp_mapout() before freeing the buf, in case a lower
* layer or HBA had done a bp_mapin(). we must do this here
* as we are the "originator" of the shadow buf.
*/
bp_mapout(bp);
/*
* Null out b_iodone before freeing the bp, to ensure that the driver
* never gets confused by a stale value in this field. (Just a little
* extra defensiveness here.)
*/
bp->b_iodone = NULL;
freerbuf(bp);
kmem_free(xp, sizeof (struct sd_xbuf));
}
/*
* Function: sd_shadow_buf_free
*
* Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
*
* Context: May be called under interrupt context
*/
static void
sd_shadow_buf_free(struct buf *bp)
{
struct sd_xbuf *xp;
ASSERT(bp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
#if defined(__sparc)
/*
* Call bp_mapout() before freeing the buf, in case a lower
* layer or HBA had done a bp_mapin(). we must do this here
* as we are the "originator" of the shadow buf.
*/
bp_mapout(bp);
#endif
/*
* Null out b_iodone before freeing the bp, to ensure that the driver
* never gets confused by a stale value in this field. (Just a little
* extra defensiveness here.)
*/
bp->b_iodone = NULL;
#if defined(__x86)
kmem_free(bp->b_un.b_addr, bp->b_bcount);
freerbuf(bp);
#else
scsi_free_consistent_buf(bp);
#endif
kmem_free(xp, sizeof (struct sd_xbuf));
}
/*
* Function: sd_print_transport_rejected_message
*
* Description: This implements the ludicrously complex rules for printing
* a "transport rejected" message. This is to address the
* specific problem of having a flood of this error message
* produced when a failover occurs.
*
* Context: Any.
*/
static void
sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
int code)
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(xp != NULL);
/*
* Print the "transport rejected" message under the following
* conditions:
*
* - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
* - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
* - If the error code IS a TRAN_FATAL_ERROR, then the message is
* printed the FIRST time a TRAN_FATAL_ERROR is returned from
* scsi_transport(9F) (which indicates that the target might have
* gone off-line). This uses the un->un_tran_fatal_count
* count, which is incremented whenever a TRAN_FATAL_ERROR is
* received, and reset to zero whenver a TRAN_ACCEPT is returned
* from scsi_transport().
*
* The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
* the preceeding cases in order for the message to be printed.
*/
if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) &&
(SD_FM_LOG(un) == SD_FM_LOG_NSUP)) {
if ((sd_level_mask & SD_LOGMASK_DIAG) ||
(code != TRAN_FATAL_ERROR) ||
(un->un_tran_fatal_count == 1)) {
switch (code) {
case TRAN_BADPKT:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"transport rejected bad packet\n");
break;
case TRAN_FATAL_ERROR:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"transport rejected fatal error\n");
break;
default:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"transport rejected (%d)\n", code);
break;
}
}
}
}
/*
* Function: sd_add_buf_to_waitq
*
* Description: Add the given buf(9S) struct to the wait queue for the
* instance. If sorting is enabled, then the buf is added
* to the queue via an elevator sort algorithm (a la
* disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key.
* If sorting is not enabled, then the buf is just added
* to the end of the wait queue.
*
* Return Code: void
*
* Context: Does not sleep/block, therefore technically can be called
* from any context. However if sorting is enabled then the
* execution time is indeterminate, and may take long if
* the wait queue grows large.
*/
static void
sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
{
struct buf *ap;
ASSERT(bp != NULL);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
/* If the queue is empty, add the buf as the only entry & return. */
if (un->un_waitq_headp == NULL) {
ASSERT(un->un_waitq_tailp == NULL);
un->un_waitq_headp = un->un_waitq_tailp = bp;
bp->av_forw = NULL;
return;
}
ASSERT(un->un_waitq_tailp != NULL);
/*
* If sorting is disabled, just add the buf to the tail end of
* the wait queue and return.
*/
if (un->un_f_disksort_disabled || un->un_f_enable_rmw) {
un->un_waitq_tailp->av_forw = bp;
un->un_waitq_tailp = bp;
bp->av_forw = NULL;
return;
}
/*
* Sort thru the list of requests currently on the wait queue
* and add the new buf request at the appropriate position.
*
* The un->un_waitq_headp is an activity chain pointer on which
* we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
* first queue holds those requests which are positioned after
* the current SD_GET_BLKNO() (in the first request); the second holds
* requests which came in after their SD_GET_BLKNO() number was passed.
* Thus we implement a one way scan, retracting after reaching
* the end of the drive to the first request on the second
* queue, at which time it becomes the first queue.
* A one-way scan is natural because of the way UNIX read-ahead
* blocks are allocated.
*
* If we lie after the first request, then we must locate the
* second request list and add ourselves to it.
*/
ap = un->un_waitq_headp;
if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
while (ap->av_forw != NULL) {
/*
* Look for an "inversion" in the (normally
* ascending) block numbers. This indicates
* the start of the second request list.
*/
if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
/*
* Search the second request list for the
* first request at a larger block number.
* We go before that; however if there is
* no such request, we go at the end.
*/
do {
if (SD_GET_BLKNO(bp) <
SD_GET_BLKNO(ap->av_forw)) {
goto insert;
}
ap = ap->av_forw;
} while (ap->av_forw != NULL);
goto insert; /* after last */
}
ap = ap->av_forw;
}
/*
* No inversions... we will go after the last, and
* be the first request in the second request list.
*/
goto insert;
}
/*
* Request is at/after the current request...
* sort in the first request list.
*/
while (ap->av_forw != NULL) {
/*
* We want to go after the current request (1) if
* there is an inversion after it (i.e. it is the end
* of the first request list), or (2) if the next
* request is a larger block no. than our request.
*/
if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
(SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
goto insert;
}
ap = ap->av_forw;
}
/*
* Neither a second list nor a larger request, therefore
* we go at the end of the first list (which is the same
* as the end of the whole schebang).
*/
insert:
bp->av_forw = ap->av_forw;
ap->av_forw = bp;
/*
* If we inserted onto the tail end of the waitq, make sure the
* tail pointer is updated.
*/
if (ap == un->un_waitq_tailp) {
un->un_waitq_tailp = bp;
}
}
/*
* Function: sd_start_cmds
*
* Description: Remove and transport cmds from the driver queues.
*
* Arguments: un - pointer to the unit (soft state) struct for the target.
*
* immed_bp - ptr to a buf to be transported immediately. Only
* the immed_bp is transported; bufs on the waitq are not
* processed and the un_retry_bp is not checked. If immed_bp is
* NULL, then normal queue processing is performed.
*
* Context: May be called from kernel thread context, interrupt context,
* or runout callback context. This function may not block or
* call routines that block.
*/
static void
sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
{
struct sd_xbuf *xp;
struct buf *bp;
void (*statp)(kstat_io_t *);
#if defined(__x86) /* DMAFREE for x86 only */
void (*saved_statp)(kstat_io_t *);
#endif
int rval;
struct sd_fm_internal *sfip = NULL;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(un->un_ncmds_in_transport >= 0);
ASSERT(un->un_throttle >= 0);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
do {
#if defined(__x86) /* DMAFREE for x86 only */
saved_statp = NULL;
#endif
/*
* If we are syncing or dumping, fail the command to
* avoid recursively calling back into scsi_transport().
* The dump I/O itself uses a separate code path so this
* only prevents non-dump I/O from being sent while dumping.
* File system sync takes place before dumping begins.
* During panic, filesystem I/O is allowed provided
* un_in_callback is <= 1. This is to prevent recursion
* such as sd_start_cmds -> scsi_transport -> sdintr ->
* sd_start_cmds and so on. See panic.c for more information
* about the states the system can be in during panic.
*/
if ((un->un_state == SD_STATE_DUMPING) ||
(ddi_in_panic() && (un->un_in_callback > 1))) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_cmds: panicking\n");
goto exit;
}
if ((bp = immed_bp) != NULL) {
/*
* We have a bp that must be transported immediately.
* It's OK to transport the immed_bp here without doing
* the throttle limit check because the immed_bp is
* always used in a retry/recovery case. This means
* that we know we are not at the throttle limit by
* virtue of the fact that to get here we must have
* already gotten a command back via sdintr(). This also
* relies on (1) the command on un_retry_bp preventing
* further commands from the waitq from being issued;
* and (2) the code in sd_retry_command checking the
* throttle limit before issuing a delayed or immediate
* retry. This holds even if the throttle limit is
* currently ratcheted down from its maximum value.
*/
statp = kstat_runq_enter;
if (bp == un->un_retry_bp) {
ASSERT((un->un_retry_statp == NULL) ||
(un->un_retry_statp == kstat_waitq_enter) ||
(un->un_retry_statp ==
kstat_runq_back_to_waitq));
/*
* If the waitq kstat was incremented when
* sd_set_retry_bp() queued this bp for a retry,
* then we must set up statp so that the waitq
* count will get decremented correctly below.
* Also we must clear un->un_retry_statp to
* ensure that we do not act on a stale value
* in this field.
*/
if ((un->un_retry_statp == kstat_waitq_enter) ||
(un->un_retry_statp ==
kstat_runq_back_to_waitq)) {
statp = kstat_waitq_to_runq;
}
#if defined(__x86) /* DMAFREE for x86 only */
saved_statp = un->un_retry_statp;
#endif
un->un_retry_statp = NULL;
SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
"sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
"un_throttle:%d un_ncmds_in_transport:%d\n",
un, un->un_retry_bp, un->un_throttle,
un->un_ncmds_in_transport);
} else {
SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
"processing priority bp:0x%p\n", bp);
}
} else if ((bp = un->un_waitq_headp) != NULL) {
/*
* A command on the waitq is ready to go, but do not
* send it if:
*
* (1) the throttle limit has been reached, or
* (2) a retry is pending, or
* (3) a START_STOP_UNIT callback pending, or
* (4) a callback for a SD_PATH_DIRECT_PRIORITY
* command is pending.
*
* For all of these conditions, IO processing will
* restart after the condition is cleared.
*/
if (un->un_ncmds_in_transport >= un->un_throttle) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_cmds: exiting, "
"throttle limit reached!\n");
goto exit;
}
if (un->un_retry_bp != NULL) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_cmds: exiting, retry pending!\n");
goto exit;
}
if (un->un_startstop_timeid != NULL) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_cmds: exiting, "
"START_STOP pending!\n");
goto exit;
}
if (un->un_direct_priority_timeid != NULL) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_cmds: exiting, "
"SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
goto exit;
}
/* Dequeue the command */
un->un_waitq_headp = bp->av_forw;
if (un->un_waitq_headp == NULL) {
un->un_waitq_tailp = NULL;
}
bp->av_forw = NULL;
statp = kstat_waitq_to_runq;
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_start_cmds: processing waitq bp:0x%p\n", bp);
} else {
/* No work to do so bail out now */
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_start_cmds: no more work, exiting!\n");
goto exit;
}
/*
* Reset the state to normal. This is the mechanism by which
* the state transitions from either SD_STATE_RWAIT or
* SD_STATE_OFFLINE to SD_STATE_NORMAL.
* If state is SD_STATE_PM_CHANGING then this command is
* part of the device power control and the state must
* not be put back to normal. Doing so would would
* allow new commands to proceed when they shouldn't,
* the device may be going off.
*/
if ((un->un_state != SD_STATE_SUSPENDED) &&
(un->un_state != SD_STATE_PM_CHANGING)) {
New_state(un, SD_STATE_NORMAL);
}
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
#if defined(__x86) /* DMAFREE for x86 only */
/*
* Allocate the scsi_pkt if we need one, or attach DMA
* resources if we have a scsi_pkt that needs them. The
* latter should only occur for commands that are being
* retried.
*/
if ((xp->xb_pktp == NULL) ||
((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
#else
if (xp->xb_pktp == NULL) {
#endif
/*
* There is no scsi_pkt allocated for this buf. Call
* the initpkt function to allocate & init one.
*
* The scsi_init_pkt runout callback functionality is
* implemented as follows:
*
* 1) The initpkt function always calls
* scsi_init_pkt(9F) with sdrunout specified as the
* callback routine.
* 2) A successful packet allocation is initialized and
* the I/O is transported.
* 3) The I/O associated with an allocation resource
* failure is left on its queue to be retried via
* runout or the next I/O.
* 4) The I/O associated with a DMA error is removed
* from the queue and failed with EIO. Processing of
* the transport queues is also halted to be
* restarted via runout or the next I/O.
* 5) The I/O associated with a CDB size or packet
* size error is removed from the queue and failed
* with EIO. Processing of the transport queues is
* continued.
*
* Note: there is no interface for canceling a runout
* callback. To prevent the driver from detaching or
* suspending while a runout is pending the driver
* state is set to SD_STATE_RWAIT
*
* Note: using the scsi_init_pkt callback facility can
* result in an I/O request persisting at the head of
* the list which cannot be satisfied even after
* multiple retries. In the future the driver may
* implement some kind of maximum runout count before
* failing an I/O.
*
* Note: the use of funcp below may seem superfluous,
* but it helps warlock figure out the correct
* initpkt function calls (see [s]sd.wlcmd).
*/
struct scsi_pkt *pktp;
int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
ASSERT(bp != un->un_rqs_bp);
funcp = sd_initpkt_map[xp->xb_chain_iostart];
switch ((*funcp)(bp, &pktp)) {
case SD_PKT_ALLOC_SUCCESS:
xp->xb_pktp = pktp;
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
pktp);
goto got_pkt;
case SD_PKT_ALLOC_FAILURE:
/*
* Temporary (hopefully) resource depletion.
* Since retries and RQS commands always have a
* scsi_pkt allocated, these cases should never
* get here. So the only cases this needs to
* handle is a bp from the waitq (which we put
* back onto the waitq for sdrunout), or a bp
* sent as an immed_bp (which we just fail).
*/
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
#if defined(__x86) /* DMAFREE for x86 only */
if (bp == immed_bp) {
/*
* If SD_XB_DMA_FREED is clear, then
* this is a failure to allocate a
* scsi_pkt, and we must fail the
* command.
*/
if ((xp->xb_pkt_flags &
SD_XB_DMA_FREED) == 0) {
break;
}
/*
* If this immediate command is NOT our
* un_retry_bp, then we must fail it.
*/
if (bp != un->un_retry_bp) {
break;
}
/*
* We get here if this cmd is our
* un_retry_bp that was DMAFREED, but
* scsi_init_pkt() failed to reallocate
* DMA resources when we attempted to
* retry it. This can happen when an
* mpxio failover is in progress, but
* we don't want to just fail the
* command in this case.
*
* Use timeout(9F) to restart it after
* a 100ms delay. We don't want to
* let sdrunout() restart it, because
* sdrunout() is just supposed to start
* commands that are sitting on the
* wait queue. The un_retry_bp stays
* set until the command completes, but
* sdrunout can be called many times
* before that happens. Since sdrunout
* cannot tell if the un_retry_bp is
* already in the transport, it could
* end up calling scsi_transport() for
* the un_retry_bp multiple times.
*
* Also: don't schedule the callback
* if some other callback is already
* pending.
*/
if (un->un_retry_statp == NULL) {
/*
* restore the kstat pointer to
* keep kstat counts coherent
* when we do retry the command.
*/
un->un_retry_statp =
saved_statp;
}
if ((un->un_startstop_timeid == NULL) &&
(un->un_retry_timeid == NULL) &&
(un->un_direct_priority_timeid ==
NULL)) {
un->un_retry_timeid =
timeout(
sd_start_retry_command,
un, SD_RESTART_TIMEOUT);
}
goto exit;
}
#else
if (bp == immed_bp) {
break; /* Just fail the command */
}
#endif
/* Add the buf back to the head of the waitq */
bp->av_forw = un->un_waitq_headp;
un->un_waitq_headp = bp;
if (un->un_waitq_tailp == NULL) {
un->un_waitq_tailp = bp;
}
goto exit;
case SD_PKT_ALLOC_FAILURE_NO_DMA:
/*
* HBA DMA resource failure. Fail the command
* and continue processing of the queues.
*/
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_cmds: "
"SD_PKT_ALLOC_FAILURE_NO_DMA\n");
break;
case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
/*
* Note:x86: Partial DMA mapping not supported
* for USCSI commands, and all the needed DMA
* resources were not allocated.
*/
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_cmds: "
"SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
break;
case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
/*
* Note:x86: Request cannot fit into CDB based
* on lba and len.
*/
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_cmds: "
"SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
break;
default:
/* Should NEVER get here! */
panic("scsi_initpkt error");
/*NOTREACHED*/
}
/*
* Fatal error in allocating a scsi_pkt for this buf.
* Update kstats & return the buf with an error code.
* We must use sd_return_failed_command_no_restart() to
* avoid a recursive call back into sd_start_cmds().
* However this also means that we must keep processing
* the waitq here in order to avoid stalling.
*/
if (statp == kstat_waitq_to_runq) {
SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
}
sd_return_failed_command_no_restart(un, bp, EIO);
if (bp == immed_bp) {
/* immed_bp is gone by now, so clear this */
immed_bp = NULL;
}
continue;
}
got_pkt:
if (bp == immed_bp) {
/* goto the head of the class.... */
xp->xb_pktp->pkt_flags |= FLAG_HEAD;
}
un->un_ncmds_in_transport++;
SD_UPDATE_KSTATS(un, statp, bp);
/*
* Call scsi_transport() to send the command to the target.
* According to SCSA architecture, we must drop the mutex here
* before calling scsi_transport() in order to avoid deadlock.
* Note that the scsi_pkt's completion routine can be executed
* (from interrupt context) even before the call to
* scsi_transport() returns.
*/
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_start_cmds: calling scsi_transport()\n");
DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
mutex_exit(SD_MUTEX(un));
rval = scsi_transport(xp->xb_pktp);
mutex_enter(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_cmds: scsi_transport() returned %d\n", rval);
switch (rval) {
case TRAN_ACCEPT:
/* Clear this with every pkt accepted by the HBA */
un->un_tran_fatal_count = 0;
break; /* Success; try the next cmd (if any) */
case TRAN_BUSY:
un->un_ncmds_in_transport--;
ASSERT(un->un_ncmds_in_transport >= 0);
/*
* Don't retry request sense, the sense data
* is lost when another request is sent.
* Free up the rqs buf and retry
* the original failed cmd. Update kstat.
*/
if (bp == un->un_rqs_bp) {
SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
bp = sd_mark_rqs_idle(un, xp);
sd_retry_command(un, bp, SD_RETRIES_STANDARD,
NULL, NULL, EIO, un->un_busy_timeout / 500,
kstat_waitq_enter);
goto exit;
}
#if defined(__x86) /* DMAFREE for x86 only */
/*
* Free the DMA resources for the scsi_pkt. This will
* allow mpxio to select another path the next time
* we call scsi_transport() with this scsi_pkt.
* See sdintr() for the rationalization behind this.
*/
if ((un->un_f_is_fibre == TRUE) &&
((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
scsi_dmafree(xp->xb_pktp);
xp->xb_pkt_flags |= SD_XB_DMA_FREED;
}
#endif
if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
/*
* Commands that are SD_PATH_DIRECT_PRIORITY
* are for error recovery situations. These do
* not use the normal command waitq, so if they
* get a TRAN_BUSY we cannot put them back onto
* the waitq for later retry. One possible
* problem is that there could already be some
* other command on un_retry_bp that is waiting
* for this one to complete, so we would be
* deadlocked if we put this command back onto
* the waitq for later retry (since un_retry_bp
* must complete before the driver gets back to
* commands on the waitq).
*
* To avoid deadlock we must schedule a callback
* that will restart this command after a set
* interval. This should keep retrying for as
* long as the underlying transport keeps
* returning TRAN_BUSY (just like for other
* commands). Use the same timeout interval as
* for the ordinary TRAN_BUSY retry.
*/
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_cmds: scsi_transport() returned "
"TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
un->un_direct_priority_timeid =
timeout(sd_start_direct_priority_command,
bp, un->un_busy_timeout / 500);
goto exit;
}
/*
* For TRAN_BUSY, we want to reduce the throttle value,
* unless we are retrying a command.
*/
if (bp != un->un_retry_bp) {
sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
}
/*
* Set up the bp to be tried again 10 ms later.
* Note:x86: Is there a timeout value in the sd_lun
* for this condition?
*/
sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
kstat_runq_back_to_waitq);
goto exit;
case TRAN_FATAL_ERROR:
un->un_tran_fatal_count++;
/* FALLTHRU */
case TRAN_BADPKT:
default:
un->un_ncmds_in_transport--;
ASSERT(un->un_ncmds_in_transport >= 0);
/*
* If this is our REQUEST SENSE command with a
* transport error, we must get back the pointers
* to the original buf, and mark the REQUEST
* SENSE command as "available".
*/
if (bp == un->un_rqs_bp) {
bp = sd_mark_rqs_idle(un, xp);
xp = SD_GET_XBUF(bp);
} else {
/*
* Legacy behavior: do not update transport
* error count for request sense commands.
*/
SD_UPDATE_ERRSTATS(un, sd_transerrs);
}
SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
sd_print_transport_rejected_message(un, xp, rval);
/*
* This command will be terminated by SD driver due
* to a fatal transport error. We should post
* ereport.io.scsi.cmd.disk.tran with driver-assessment
* of "fail" for any command to indicate this
* situation.
*/
if (xp->xb_ena > 0) {
ASSERT(un->un_fm_private != NULL);
sfip = un->un_fm_private;
sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT;
sd_ssc_extract_info(&sfip->fm_ssc, un,
xp->xb_pktp, bp, xp);
sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
}
/*
* We must use sd_return_failed_command_no_restart() to
* avoid a recursive call back into sd_start_cmds().
* However this also means that we must keep processing
* the waitq here in order to avoid stalling.
*/
sd_return_failed_command_no_restart(un, bp, EIO);
/*
* Notify any threads waiting in sd_ddi_suspend() that
* a command completion has occurred.
*/
if (un->un_state == SD_STATE_SUSPENDED) {
cv_broadcast(&un->un_disk_busy_cv);
}
if (bp == immed_bp) {
/* immed_bp is gone by now, so clear this */
immed_bp = NULL;
}
break;
}
} while (immed_bp == NULL);
exit:
ASSERT(mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
}
/*
* Function: sd_return_command
*
* Description: Returns a command to its originator (with or without an
* error). Also starts commands waiting to be transported
* to the target.
*
* Context: May be called from interrupt, kernel, or timeout context
*/
static void
sd_return_command(struct sd_lun *un, struct buf *bp)
{
struct sd_xbuf *xp;
struct scsi_pkt *pktp;
struct sd_fm_internal *sfip;
ASSERT(bp != NULL);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != un->un_rqs_bp);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
pktp = SD_GET_PKTP(bp);
sfip = (struct sd_fm_internal *)un->un_fm_private;
ASSERT(sfip != NULL);
SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
/*
* Note: check for the "sdrestart failed" case.
*/
if ((un->un_partial_dma_supported == 1) &&
((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
(geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
(xp->xb_pktp->pkt_resid == 0)) {
if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
/*
* Successfully set up next portion of cmd
* transfer, try sending it
*/
sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
NULL, NULL, 0, (clock_t)0, NULL);
sd_start_cmds(un, NULL);
return; /* Note:x86: need a return here? */
}
}
/*
* If this is the failfast bp, clear it from un_failfast_bp. This
* can happen if upon being re-tried the failfast bp either
* succeeded or encountered another error (possibly even a different
* error than the one that precipitated the failfast state, but in
* that case it would have had to exhaust retries as well). Regardless,
* this should not occur whenever the instance is in the active
* failfast state.
*/
if (bp == un->un_failfast_bp) {
ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
un->un_failfast_bp = NULL;
}
/*
* Clear the failfast state upon successful completion of ANY cmd.
*/
if (bp->b_error == 0) {
un->un_failfast_state = SD_FAILFAST_INACTIVE;
/*
* If this is a successful command, but used to be retried,
* we will take it as a recovered command and post an
* ereport with driver-assessment of "recovered".
*/
if (xp->xb_ena > 0) {
sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY);
}
} else {
/*
* If this is a failed non-USCSI command we will post an
* ereport with driver-assessment set accordingly("fail" or
* "fatal").
*/
if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
}
}
/*
* This is used if the command was retried one or more times. Show that
* we are done with it, and allow processing of the waitq to resume.
*/
if (bp == un->un_retry_bp) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_return_command: un:0x%p: "
"RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
un->un_retry_bp = NULL;
un->un_retry_statp = NULL;
}
SD_UPDATE_RDWR_STATS(un, bp);
SD_UPDATE_PARTITION_STATS(un, bp);
switch (un->un_state) {
case SD_STATE_SUSPENDED:
/*
* Notify any threads waiting in sd_ddi_suspend() that
* a command completion has occurred.
*/
cv_broadcast(&un->un_disk_busy_cv);
break;
default:
sd_start_cmds(un, NULL);
break;
}
/* Return this command up the iodone chain to its originator. */
mutex_exit(SD_MUTEX(un));
(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
xp->xb_pktp = NULL;
SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
ASSERT(!mutex_owned(SD_MUTEX(un)));
mutex_enter(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
}
/*
* Function: sd_return_failed_command
*
* Description: Command completion when an error occurred.
*
* Context: May be called from interrupt context
*/
static void
sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
{
ASSERT(bp != NULL);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_return_failed_command: entry\n");
/*
* b_resid could already be nonzero due to a partial data
* transfer, so do not change it here.
*/
SD_BIOERROR(bp, errcode);
sd_return_command(un, bp);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_return_failed_command: exit\n");
}
/*
* Function: sd_return_failed_command_no_restart
*
* Description: Same as sd_return_failed_command, but ensures that no
* call back into sd_start_cmds will be issued.
*
* Context: May be called from interrupt context
*/
static void
sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
int errcode)
{
struct sd_xbuf *xp;
ASSERT(bp != NULL);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
ASSERT(errcode != 0);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_return_failed_command_no_restart: entry\n");
/*
* b_resid could already be nonzero due to a partial data
* transfer, so do not change it here.
*/
SD_BIOERROR(bp, errcode);
/*
* If this is the failfast bp, clear it. This can happen if the
* failfast bp encounterd a fatal error when we attempted to
* re-try it (such as a scsi_transport(9F) failure). However
* we should NOT be in an active failfast state if the failfast
* bp is not NULL.
*/
if (bp == un->un_failfast_bp) {
ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
un->un_failfast_bp = NULL;
}
if (bp == un->un_retry_bp) {
/*
* This command was retried one or more times. Show that we are
* done with it, and allow processing of the waitq to resume.
*/
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_return_failed_command_no_restart: "
" un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
un->un_retry_bp = NULL;
un->un_retry_statp = NULL;
}
SD_UPDATE_RDWR_STATS(un, bp);
SD_UPDATE_PARTITION_STATS(un, bp);
mutex_exit(SD_MUTEX(un));
if (xp->xb_pktp != NULL) {
(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
xp->xb_pktp = NULL;
}
SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
mutex_enter(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_return_failed_command_no_restart: exit\n");
}
/*
* Function: sd_retry_command
*
* Description: queue up a command for retry, or (optionally) fail it
* if retry counts are exhausted.
*
* Arguments: un - Pointer to the sd_lun struct for the target.
*
* bp - Pointer to the buf for the command to be retried.
*
* retry_check_flag - Flag to see which (if any) of the retry
* counts should be decremented/checked. If the indicated
* retry count is exhausted, then the command will not be
* retried; it will be failed instead. This should use a
* value equal to one of the following:
*
* SD_RETRIES_NOCHECK
* SD_RESD_RETRIES_STANDARD
* SD_RETRIES_VICTIM
*
* Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
* if the check should be made to see of FLAG_ISOLATE is set
* in the pkt. If FLAG_ISOLATE is set, then the command is
* not retried, it is simply failed.
*
* user_funcp - Ptr to function to call before dispatching the
* command. May be NULL if no action needs to be performed.
* (Primarily intended for printing messages.)
*
* user_arg - Optional argument to be passed along to
* the user_funcp call.
*
* failure_code - errno return code to set in the bp if the
* command is going to be failed.
*
* retry_delay - Retry delay interval in (clock_t) units. May
* be zero which indicates that the retry should be retried
* immediately (ie, without an intervening delay).
*
* statp - Ptr to kstat function to be updated if the command
* is queued for a delayed retry. May be NULL if no kstat
* update is desired.
*
* Context: May be called from interrupt context.
*/
static void
sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int code),
void *user_arg, int failure_code, clock_t retry_delay,
void (*statp)(kstat_io_t *))
{
struct sd_xbuf *xp;
struct scsi_pkt *pktp;
struct sd_fm_internal *sfip;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
pktp = SD_GET_PKTP(bp);
ASSERT(pktp != NULL);
sfip = (struct sd_fm_internal *)un->un_fm_private;
ASSERT(sfip != NULL);
SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
"sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
/*
* If we are syncing or dumping, fail the command to avoid
* recursively calling back into scsi_transport().
*/
if (ddi_in_panic()) {
goto fail_command_no_log;
}
/*
* We should never be be retrying a command with FLAG_DIAGNOSE set, so
* log an error and fail the command.
*/
if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
"ERROR, retrying FLAG_DIAGNOSE command.\n");
sd_dump_memory(un, SD_LOG_IO, "CDB",
(uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
sd_dump_memory(un, SD_LOG_IO, "Sense Data",
(uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
goto fail_command;
}
/*
* If we are suspended, then put the command onto head of the
* wait queue since we don't want to start more commands, and
* clear the un_retry_bp. Next time when we are resumed, will
* handle the command in the wait queue.
*/
switch (un->un_state) {
case SD_STATE_SUSPENDED:
case SD_STATE_DUMPING:
bp->av_forw = un->un_waitq_headp;
un->un_waitq_headp = bp;
if (un->un_waitq_tailp == NULL) {
un->un_waitq_tailp = bp;
}
if (bp == un->un_retry_bp) {
un->un_retry_bp = NULL;
un->un_retry_statp = NULL;
}
SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
"exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
return;
default:
break;
}
/*
* If the caller wants us to check FLAG_ISOLATE, then see if that
* is set; if it is then we do not want to retry the command.
* Normally, FLAG_ISOLATE is only used with USCSI cmds.
*/
if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
goto fail_command;
}
}
/*
* If SD_RETRIES_FAILFAST is set, it indicates that either a
* command timeout or a selection timeout has occurred. This means
* that we were unable to establish an kind of communication with
* the target, and subsequent retries and/or commands are likely
* to encounter similar results and take a long time to complete.
*
* If this is a failfast error condition, we need to update the
* failfast state, even if this bp does not have B_FAILFAST set.
*/
if (retry_check_flag & SD_RETRIES_FAILFAST) {
if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
ASSERT(un->un_failfast_bp == NULL);
/*
* If we are already in the active failfast state, and
* another failfast error condition has been detected,
* then fail this command if it has B_FAILFAST set.
* If B_FAILFAST is clear, then maintain the legacy
* behavior of retrying heroically, even tho this will
* take a lot more time to fail the command.
*/
if (bp->b_flags & B_FAILFAST) {
goto fail_command;
}
} else {
/*
* We're not in the active failfast state, but we
* have a failfast error condition, so we must begin
* transition to the next state. We do this regardless
* of whether or not this bp has B_FAILFAST set.
*/
if (un->un_failfast_bp == NULL) {
/*
* This is the first bp to meet a failfast
* condition so save it on un_failfast_bp &
* do normal retry processing. Do not enter
* active failfast state yet. This marks
* entry into the "failfast pending" state.
*/
un->un_failfast_bp = bp;
} else if (un->un_failfast_bp == bp) {
/*
* This is the second time *this* bp has
* encountered a failfast error condition,
* so enter active failfast state & flush
* queues as appropriate.
*/
un->un_failfast_state = SD_FAILFAST_ACTIVE;
un->un_failfast_bp = NULL;
sd_failfast_flushq(un);
/*
* Fail this bp now if B_FAILFAST set;
* otherwise continue with retries. (It would
* be pretty ironic if this bp succeeded on a
* subsequent retry after we just flushed all
* the queues).
*/
if (bp->b_flags & B_FAILFAST) {
goto fail_command;
}
#if !defined(lint) && !defined(__lint)
} else {
/*
* If neither of the preceeding conditionals
* was true, it means that there is some
* *other* bp that has met an inital failfast
* condition and is currently either being
* retried or is waiting to be retried. In
* that case we should perform normal retry
* processing on *this* bp, since there is a
* chance that the current failfast condition
* is transient and recoverable. If that does
* not turn out to be the case, then retries
* will be cleared when the wait queue is
* flushed anyway.
*/
#endif
}
}
} else {
/*
* SD_RETRIES_FAILFAST is clear, which indicates that we
* likely were able to at least establish some level of
* communication with the target and subsequent commands
* and/or retries are likely to get through to the target,
* In this case we want to be aggressive about clearing
* the failfast state. Note that this does not affect
* the "failfast pending" condition.
*/
un->un_failfast_state = SD_FAILFAST_INACTIVE;
}
/*
* Check the specified retry count to see if we can still do
* any retries with this pkt before we should fail it.
*/
switch (retry_check_flag & SD_RETRIES_MASK) {
case SD_RETRIES_VICTIM:
/*
* Check the victim retry count. If exhausted, then fall
* thru & check against the standard retry count.
*/
if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
/* Increment count & proceed with the retry */
xp->xb_victim_retry_count++;
break;
}
/* Victim retries exhausted, fall back to std. retries... */
/* FALLTHRU */
case SD_RETRIES_STANDARD:
if (xp->xb_retry_count >= un->un_retry_count) {
/* Retries exhausted, fail the command */
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_retry_command: retries exhausted!\n");
/*
* update b_resid for failed SCMD_READ & SCMD_WRITE
* commands with nonzero pkt_resid.
*/
if ((pktp->pkt_reason == CMD_CMPLT) &&
(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
(pktp->pkt_resid != 0)) {
uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
SD_UPDATE_B_RESID(bp, pktp);
}
}
goto fail_command;
}
xp->xb_retry_count++;
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_retry_command: retry count:%d\n", xp->xb_retry_count);
break;
case SD_RETRIES_UA:
if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
/* Retries exhausted, fail the command */
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"Unit Attention retries exhausted. "
"Check the target.\n");
goto fail_command;
}
xp->xb_ua_retry_count++;
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_retry_command: retry count:%d\n",
xp->xb_ua_retry_count);
break;
case SD_RETRIES_BUSY:
if (xp->xb_retry_count >= un->un_busy_retry_count) {
/* Retries exhausted, fail the command */
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_retry_command: retries exhausted!\n");
goto fail_command;
}
xp->xb_retry_count++;
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_retry_command: retry count:%d\n", xp->xb_retry_count);
break;
case SD_RETRIES_NOCHECK:
default:
/* No retry count to check. Just proceed with the retry */
break;
}
xp->xb_pktp->pkt_flags |= FLAG_HEAD;
/*
* If this is a non-USCSI command being retried
* during execution last time, we should post an ereport with
* driver-assessment of the value "retry".
* For partial DMA, request sense and STATUS_QFULL, there are no
* hardware errors, we bypass ereport posting.
*/
if (failure_code != 0) {
if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY);
}
}
/*
* If we were given a zero timeout, we must attempt to retry the
* command immediately (ie, without a delay).
*/
if (retry_delay == 0) {
/*
* Check some limiting conditions to see if we can actually
* do the immediate retry. If we cannot, then we must
* fall back to queueing up a delayed retry.
*/
if (un->un_ncmds_in_transport >= un->un_throttle) {
/*
* We are at the throttle limit for the target,
* fall back to delayed retry.
*/
retry_delay = un->un_busy_timeout;
statp = kstat_waitq_enter;
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_retry_command: immed. retry hit "
"throttle!\n");
} else {
/*
* We're clear to proceed with the immediate retry.
* First call the user-provided function (if any)
*/
if (user_funcp != NULL) {
(*user_funcp)(un, bp, user_arg,
SD_IMMEDIATE_RETRY_ISSUED);
#ifdef __lock_lint
sd_print_incomplete_msg(un, bp, user_arg,
SD_IMMEDIATE_RETRY_ISSUED);
sd_print_cmd_incomplete_msg(un, bp, user_arg,
SD_IMMEDIATE_RETRY_ISSUED);
sd_print_sense_failed_msg(un, bp, user_arg,
SD_IMMEDIATE_RETRY_ISSUED);
#endif
}
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_retry_command: issuing immediate retry\n");
/*
* Call sd_start_cmds() to transport the command to
* the target.
*/
sd_start_cmds(un, bp);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_retry_command exit\n");
return;
}
}
/*
* Set up to retry the command after a delay.
* First call the user-provided function (if any)
*/
if (user_funcp != NULL) {
(*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
}
sd_set_retry_bp(un, bp, retry_delay, statp);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
return;
fail_command:
if (user_funcp != NULL) {
(*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
}
fail_command_no_log:
SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_retry_command: returning failed command\n");
sd_return_failed_command(un, bp, failure_code);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
}
/*
* Function: sd_set_retry_bp
*
* Description: Set up the given bp for retry.
*
* Arguments: un - ptr to associated softstate
* bp - ptr to buf(9S) for the command
* retry_delay - time interval before issuing retry (may be 0)
* statp - optional pointer to kstat function
*
* Context: May be called under interrupt context
*/
static void
sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
void (*statp)(kstat_io_t *))
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
"sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
/*
* Indicate that the command is being retried. This will not allow any
* other commands on the wait queue to be transported to the target
* until this command has been completed (success or failure). The
* "retry command" is not transported to the target until the given
* time delay expires, unless the user specified a 0 retry_delay.
*
* Note: the timeout(9F) callback routine is what actually calls
* sd_start_cmds() to transport the command, with the exception of a
* zero retry_delay. The only current implementor of a zero retry delay
* is the case where a START_STOP_UNIT is sent to spin-up a device.
*/
if (un->un_retry_bp == NULL) {
ASSERT(un->un_retry_statp == NULL);
un->un_retry_bp = bp;
/*
* If the user has not specified a delay the command should
* be queued and no timeout should be scheduled.
*/
if (retry_delay == 0) {
/*
* Save the kstat pointer that will be used in the
* call to SD_UPDATE_KSTATS() below, so that
* sd_start_cmds() can correctly decrement the waitq
* count when it is time to transport this command.
*/
un->un_retry_statp = statp;
goto done;
}
}
if (un->un_retry_bp == bp) {
/*
* Save the kstat pointer that will be used in the call to
* SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
* correctly decrement the waitq count when it is time to
* transport this command.
*/
un->un_retry_statp = statp;
/*
* Schedule a timeout if:
* 1) The user has specified a delay.
* 2) There is not a START_STOP_UNIT callback pending.
*
* If no delay has been specified, then it is up to the caller
* to ensure that IO processing continues without stalling.
* Effectively, this means that the caller will issue the
* required call to sd_start_cmds(). The START_STOP_UNIT
* callback does this after the START STOP UNIT command has
* completed. In either of these cases we should not schedule
* a timeout callback here. Also don't schedule the timeout if
* an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
*/
if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
(un->un_direct_priority_timeid == NULL)) {
un->un_retry_timeid =
timeout(sd_start_retry_command, un, retry_delay);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_set_retry_bp: setting timeout: un: 0x%p"
" bp:0x%p un_retry_timeid:0x%p\n",
un, bp, un->un_retry_timeid);
}
} else {
/*
* We only get in here if there is already another command
* waiting to be retried. In this case, we just put the
* given command onto the wait queue, so it can be transported
* after the current retry command has completed.
*
* Also we have to make sure that if the command at the head
* of the wait queue is the un_failfast_bp, that we do not
* put ahead of it any other commands that are to be retried.
*/
if ((un->un_failfast_bp != NULL) &&
(un->un_failfast_bp == un->un_waitq_headp)) {
/*
* Enqueue this command AFTER the first command on
* the wait queue (which is also un_failfast_bp).
*/
bp->av_forw = un->un_waitq_headp->av_forw;
un->un_waitq_headp->av_forw = bp;
if (un->un_waitq_headp == un->un_waitq_tailp) {
un->un_waitq_tailp = bp;
}
} else {
/* Enqueue this command at the head of the waitq. */
bp->av_forw = un->un_waitq_headp;
un->un_waitq_headp = bp;
if (un->un_waitq_tailp == NULL) {
un->un_waitq_tailp = bp;
}
}
if (statp == NULL) {
statp = kstat_waitq_enter;
}
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_set_retry_bp: un:0x%p already delayed retry\n", un);
}
done:
if (statp != NULL) {
SD_UPDATE_KSTATS(un, statp, bp);
}
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_set_retry_bp: exit un:0x%p\n", un);
}
/*
* Function: sd_start_retry_command
*
* Description: Start the command that has been waiting on the target's
* retry queue. Called from timeout(9F) context after the
* retry delay interval has expired.
*
* Arguments: arg - pointer to associated softstate for the device.
*
* Context: timeout(9F) thread context. May not sleep.
*/
static void
sd_start_retry_command(void *arg)
{
struct sd_lun *un = arg;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_retry_command: entry\n");
mutex_enter(SD_MUTEX(un));
un->un_retry_timeid = NULL;
if (un->un_retry_bp != NULL) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
un, un->un_retry_bp);
sd_start_cmds(un, un->un_retry_bp);
}
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_retry_command: exit\n");
}
/*
* Function: sd_rmw_msg_print_handler
*
* Description: If RMW mode is enabled and warning message is triggered
* print I/O count during a fixed interval.
*
* Arguments: arg - pointer to associated softstate for the device.
*
* Context: timeout(9F) thread context. May not sleep.
*/
static void
sd_rmw_msg_print_handler(void *arg)
{
struct sd_lun *un = arg;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_rmw_msg_print_handler: entry\n");
mutex_enter(SD_MUTEX(un));
if (un->un_rmw_incre_count > 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"%"PRIu64" I/O requests are not aligned with %d disk "
"sector size in %ld seconds. They are handled through "
"Read Modify Write but the performance is very low!\n",
un->un_rmw_incre_count, un->un_tgt_blocksize,
drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000);
un->un_rmw_incre_count = 0;
un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler,
un, SD_RMW_MSG_PRINT_TIMEOUT);
} else {
un->un_rmw_msg_timeid = NULL;
}
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_rmw_msg_print_handler: exit\n");
}
/*
* Function: sd_start_direct_priority_command
*
* Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
* received TRAN_BUSY when we called scsi_transport() to send it
* to the underlying HBA. This function is called from timeout(9F)
* context after the delay interval has expired.
*
* Arguments: arg - pointer to associated buf(9S) to be restarted.
*
* Context: timeout(9F) thread context. May not sleep.
*/
static void
sd_start_direct_priority_command(void *arg)
{
struct buf *priority_bp = arg;
struct sd_lun *un;
ASSERT(priority_bp != NULL);
un = SD_GET_UN(priority_bp);
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_direct_priority_command: entry\n");
mutex_enter(SD_MUTEX(un));
un->un_direct_priority_timeid = NULL;
sd_start_cmds(un, priority_bp);
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_direct_priority_command: exit\n");
}
/*
* Function: sd_send_request_sense_command
*
* Description: Sends a REQUEST SENSE command to the target
*
* Context: May be called from interrupt context.
*/
static void
sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
struct scsi_pkt *pktp)
{
ASSERT(bp != NULL);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
"entry: buf:0x%p\n", bp);
/*
* If we are syncing or dumping, then fail the command to avoid a
* recursive callback into scsi_transport(). Also fail the command
* if we are suspended (legacy behavior).
*/
if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
(un->un_state == SD_STATE_DUMPING)) {
sd_return_failed_command(un, bp, EIO);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_send_request_sense_command: syncing/dumping, exit\n");
return;
}
/*
* Retry the failed command and don't issue the request sense if:
* 1) the sense buf is busy
* 2) we have 1 or more outstanding commands on the target
* (the sense data will be cleared or invalidated any way)
*
* Note: There could be an issue with not checking a retry limit here,
* the problem is determining which retry limit to check.
*/
if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
/* Don't retry if the command is flagged as non-retryable */
if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
NULL, NULL, 0, un->un_busy_timeout,
kstat_waitq_enter);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_send_request_sense_command: "
"at full throttle, retrying exit\n");
} else {
sd_return_failed_command(un, bp, EIO);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_send_request_sense_command: "
"at full throttle, non-retryable exit\n");
}
return;
}
sd_mark_rqs_busy(un, bp);
sd_start_cmds(un, un->un_rqs_bp);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_send_request_sense_command: exit\n");
}
/*
* Function: sd_mark_rqs_busy
*
* Description: Indicate that the request sense bp for this instance is
* in use.
*
* Context: May be called under interrupt context
*/
static void
sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
{
struct sd_xbuf *sense_xp;
ASSERT(un != NULL);
ASSERT(bp != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(un->un_sense_isbusy == 0);
SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
"buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
sense_xp = SD_GET_XBUF(un->un_rqs_bp);
ASSERT(sense_xp != NULL);
SD_INFO(SD_LOG_IO, un,
"sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
ASSERT(sense_xp->xb_pktp != NULL);
ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
== (FLAG_SENSING | FLAG_HEAD));
un->un_sense_isbusy = 1;
un->un_rqs_bp->b_resid = 0;
sense_xp->xb_pktp->pkt_resid = 0;
sense_xp->xb_pktp->pkt_reason = 0;
/* So we can get back the bp at interrupt time! */
sense_xp->xb_sense_bp = bp;
bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
/*
* Mark this buf as awaiting sense data. (This is already set in
* the pkt_flags for the RQS packet.)
*/
((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
/* Request sense down same path */
if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) &&
((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance)
sense_xp->xb_pktp->pkt_path_instance =
((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance;
sense_xp->xb_retry_count = 0;
sense_xp->xb_victim_retry_count = 0;
sense_xp->xb_ua_retry_count = 0;
sense_xp->xb_nr_retry_count = 0;
sense_xp->xb_dma_resid = 0;
/* Clean up the fields for auto-request sense */
sense_xp->xb_sense_status = 0;
sense_xp->xb_sense_state = 0;
sense_xp->xb_sense_resid = 0;
bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
}
/*
* Function: sd_mark_rqs_idle
*
* Description: SD_MUTEX must be held continuously through this routine
* to prevent reuse of the rqs struct before the caller can
* complete it's processing.
*
* Return Code: Pointer to the RQS buf
*
* Context: May be called under interrupt context
*/
static struct buf *
sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
{
struct buf *bp;
ASSERT(un != NULL);
ASSERT(sense_xp != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(un->un_sense_isbusy != 0);
un->un_sense_isbusy = 0;
bp = sense_xp->xb_sense_bp;
sense_xp->xb_sense_bp = NULL;
/* This pkt is no longer interested in getting sense data */
((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
return (bp);
}
/*
* Function: sd_alloc_rqs
*
* Description: Set up the unit to receive auto request sense data
*
* Return Code: DDI_SUCCESS or DDI_FAILURE
*
* Context: Called under attach(9E) context
*/
static int
sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
{
struct sd_xbuf *xp;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(un->un_rqs_bp == NULL);
ASSERT(un->un_rqs_pktp == NULL);
/*
* First allocate the required buf and scsi_pkt structs, then set up
* the CDB in the scsi_pkt for a REQUEST SENSE command.
*/
un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
if (un->un_rqs_bp == NULL) {
return (DDI_FAILURE);
}
un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
if (un->un_rqs_pktp == NULL) {
sd_free_rqs(un);
return (DDI_FAILURE);
}
/* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
(void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
/* Set up the other needed members in the ARQ scsi_pkt. */
un->un_rqs_pktp->pkt_comp = sdintr;
un->un_rqs_pktp->pkt_time = sd_io_time;
un->un_rqs_pktp->pkt_flags |=
(FLAG_SENSING | FLAG_HEAD); /* (1222170) */
/*
* Allocate & init the sd_xbuf struct for the RQS command. Do not
* provide any intpkt, destroypkt routines as we take care of
* scsi_pkt allocation/freeing here and in sd_free_rqs().
*/
xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
xp->xb_pktp = un->un_rqs_pktp;
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n",
un, xp, un->un_rqs_pktp, un->un_rqs_bp);
/*
* Save the pointer to the request sense private bp so it can
* be retrieved in sdintr.
*/
un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
ASSERT(un->un_rqs_bp->b_private == xp);
/*
* See if the HBA supports auto-request sense for the specified
* target/lun. If it does, then try to enable it (if not already
* enabled).
*
* Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
* failure, while for other HBAs (pln) scsi_ifsetcap will always
* return success. However, in both of these cases ARQ is always
* enabled and scsi_ifgetcap will always return true. The best approach
* is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
*
* The 3rd case is the HBA (adp) always return enabled on
* scsi_ifgetgetcap even when it's not enable, the best approach
* is issue a scsi_ifsetcap then a scsi_ifgetcap
* Note: this case is to circumvent the Adaptec bug. (x86 only)
*/
if (un->un_f_is_fibre == TRUE) {
un->un_f_arq_enabled = TRUE;
} else {
#if defined(__x86)
/*
* Circumvent the Adaptec bug, remove this code when
* the bug is fixed
*/
(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
#endif
switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
case 0:
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_alloc_rqs: HBA supports ARQ\n");
/*
* ARQ is supported by this HBA but currently is not
* enabled. Attempt to enable it and if successful then
* mark this instance as ARQ enabled.
*/
if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
== 1) {
/* Successfully enabled ARQ in the HBA */
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_alloc_rqs: ARQ enabled\n");
un->un_f_arq_enabled = TRUE;
} else {
/* Could not enable ARQ in the HBA */
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_alloc_rqs: failed ARQ enable\n");
un->un_f_arq_enabled = FALSE;
}
break;
case 1:
/*
* ARQ is supported by this HBA and is already enabled.
* Just mark ARQ as enabled for this instance.
*/
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_alloc_rqs: ARQ already enabled\n");
un->un_f_arq_enabled = TRUE;
break;
default:
/*
* ARQ is not supported by this HBA; disable it for this
* instance.
*/
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_alloc_rqs: HBA does not support ARQ\n");
un->un_f_arq_enabled = FALSE;
break;
}
}
return (DDI_SUCCESS);
}
/*
* Function: sd_free_rqs
*
* Description: Cleanup for the pre-instance RQS command.
*
* Context: Kernel thread context
*/
static void
sd_free_rqs(struct sd_lun *un)
{
ASSERT(un != NULL);
SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
/*
* If consistent memory is bound to a scsi_pkt, the pkt
* has to be destroyed *before* freeing the consistent memory.
* Don't change the sequence of this operations.
* scsi_destroy_pkt() might access memory, which isn't allowed,
* after it was freed in scsi_free_consistent_buf().
*/
if (un->un_rqs_pktp != NULL) {
scsi_destroy_pkt(un->un_rqs_pktp);
un->un_rqs_pktp = NULL;
}
if (un->un_rqs_bp != NULL) {
struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp);
if (xp != NULL) {
kmem_free(xp, sizeof (struct sd_xbuf));
}
scsi_free_consistent_buf(un->un_rqs_bp);
un->un_rqs_bp = NULL;
}
SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
}
/*
* Function: sd_reduce_throttle
*
* Description: Reduces the maximum # of outstanding commands on a
* target to the current number of outstanding commands.
* Queues a tiemout(9F) callback to restore the limit
* after a specified interval has elapsed.
* Typically used when we get a TRAN_BUSY return code
* back from scsi_transport().
*
* Arguments: un - ptr to the sd_lun softstate struct
* throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
*
* Context: May be called from interrupt context
*/
static void
sd_reduce_throttle(struct sd_lun *un, int throttle_type)
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(un->un_ncmds_in_transport >= 0);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
"entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
un, un->un_throttle, un->un_ncmds_in_transport);
if (un->un_throttle > 1) {
if (un->un_f_use_adaptive_throttle == TRUE) {
switch (throttle_type) {
case SD_THROTTLE_TRAN_BUSY:
if (un->un_busy_throttle == 0) {
un->un_busy_throttle = un->un_throttle;
}
break;
case SD_THROTTLE_QFULL:
un->un_busy_throttle = 0;
break;
default:
ASSERT(FALSE);
}
if (un->un_ncmds_in_transport > 0) {
un->un_throttle = un->un_ncmds_in_transport;
}
} else {
if (un->un_ncmds_in_transport == 0) {
un->un_throttle = 1;
} else {
un->un_throttle = un->un_ncmds_in_transport;
}
}
}
/* Reschedule the timeout if none is currently active */
if (un->un_reset_throttle_timeid == NULL) {
un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
un, SD_THROTTLE_RESET_INTERVAL);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_reduce_throttle: timeout scheduled!\n");
}
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
"exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
}
/*
* Function: sd_restore_throttle
*
* Description: Callback function for timeout(9F). Resets the current
* value of un->un_throttle to its default.
*
* Arguments: arg - pointer to associated softstate for the device.
*
* Context: May be called from interrupt context
*/
static void
sd_restore_throttle(void *arg)
{
struct sd_lun *un = arg;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
mutex_enter(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
"entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
un->un_reset_throttle_timeid = NULL;
if (un->un_f_use_adaptive_throttle == TRUE) {
/*
* If un_busy_throttle is nonzero, then it contains the
* value that un_throttle was when we got a TRAN_BUSY back
* from scsi_transport(). We want to revert back to this
* value.
*
* In the QFULL case, the throttle limit will incrementally
* increase until it reaches max throttle.
*/
if (un->un_busy_throttle > 0) {
un->un_throttle = un->un_busy_throttle;
un->un_busy_throttle = 0;
} else {
/*
* increase throttle by 10% open gate slowly, schedule
* another restore if saved throttle has not been
* reached
*/
short throttle;
if (sd_qfull_throttle_enable) {
throttle = un->un_throttle +
max((un->un_throttle / 10), 1);
un->un_throttle =
(throttle < un->un_saved_throttle) ?
throttle : un->un_saved_throttle;
if (un->un_throttle < un->un_saved_throttle) {
un->un_reset_throttle_timeid =
timeout(sd_restore_throttle,
un,
SD_QFULL_THROTTLE_RESET_INTERVAL);
}
}
}
/*
* If un_throttle has fallen below the low-water mark, we
* restore the maximum value here (and allow it to ratchet
* down again if necessary).
*/
if (un->un_throttle < un->un_min_throttle) {
un->un_throttle = un->un_saved_throttle;
}
} else {
SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
"restoring limit from 0x%x to 0x%x\n",
un->un_throttle, un->un_saved_throttle);
un->un_throttle = un->un_saved_throttle;
}
SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
"sd_restore_throttle: calling sd_start_cmds!\n");
sd_start_cmds(un, NULL);
SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
"sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
un, un->un_throttle);
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
}
/*
* Function: sdrunout
*
* Description: Callback routine for scsi_init_pkt when a resource allocation
* fails.
*
* Arguments: arg - a pointer to the sd_lun unit struct for the particular
* soft state instance.
*
* Return Code: The scsi_init_pkt routine allows for the callback function to
* return a 0 indicating the callback should be rescheduled or a 1
* indicating not to reschedule. This routine always returns 1
* because the driver always provides a callback function to
* scsi_init_pkt. This results in a callback always being scheduled
* (via the scsi_init_pkt callback implementation) if a resource
* failure occurs.
*
* Context: This callback function may not block or call routines that block
*
* Note: Using the scsi_init_pkt callback facility can result in an I/O
* request persisting at the head of the list which cannot be
* satisfied even after multiple retries. In the future the driver
* may implement some time of maximum runout count before failing
* an I/O.
*/
static int
sdrunout(caddr_t arg)
{
struct sd_lun *un = (struct sd_lun *)arg;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
mutex_enter(SD_MUTEX(un));
sd_start_cmds(un, NULL);
mutex_exit(SD_MUTEX(un));
/*
* This callback routine always returns 1 (i.e. do not reschedule)
* because we always specify sdrunout as the callback handler for
* scsi_init_pkt inside the call to sd_start_cmds.
*/
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
return (1);
}
/*
* Function: sdintr
*
* Description: Completion callback routine for scsi_pkt(9S) structs
* sent to the HBA driver via scsi_transport(9F).
*
* Context: Interrupt context
*/
static void
sdintr(struct scsi_pkt *pktp)
{
struct buf *bp;
struct sd_xbuf *xp;
struct sd_lun *un;
size_t actual_len;
sd_ssc_t *sscp;
ASSERT(pktp != NULL);
bp = (struct buf *)pktp->pkt_private;
ASSERT(bp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
ASSERT(xp->xb_pktp != NULL);
un = SD_GET_UN(bp);
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
#ifdef SD_FAULT_INJECTION
SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
/* SD FaultInjection */
sd_faultinjection(pktp);
#endif /* SD_FAULT_INJECTION */
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
" xp:0x%p, un:0x%p\n", bp, xp, un);
mutex_enter(SD_MUTEX(un));
ASSERT(un->un_fm_private != NULL);
sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
ASSERT(sscp != NULL);
/* Reduce the count of the #commands currently in transport */
un->un_ncmds_in_transport--;
ASSERT(un->un_ncmds_in_transport >= 0);
/* Increment counter to indicate that the callback routine is active */
un->un_in_callback++;
SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
#ifdef SDDEBUG
if (bp == un->un_retry_bp) {
SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
"un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
un, un->un_retry_bp, un->un_ncmds_in_transport);
}
#endif
/*
* If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
* state if needed.
*/
if (pktp->pkt_reason == CMD_DEV_GONE) {
/* Prevent multiple console messages for the same failure. */
if (un->un_last_pkt_reason != CMD_DEV_GONE) {
un->un_last_pkt_reason = CMD_DEV_GONE;
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"Command failed to complete...Device is gone\n");
}
if (un->un_mediastate != DKIO_DEV_GONE) {
un->un_mediastate = DKIO_DEV_GONE;
cv_broadcast(&un->un_state_cv);
}
/*
* If the command happens to be the REQUEST SENSE command,
* free up the rqs buf and fail the original command.
*/
if (bp == un->un_rqs_bp) {
bp = sd_mark_rqs_idle(un, xp);
}
sd_return_failed_command(un, bp, EIO);
goto exit;
}
if (pktp->pkt_state & STATE_XARQ_DONE) {
SD_TRACE(SD_LOG_COMMON, un,
"sdintr: extra sense data received. pkt=%p\n", pktp);
}
/*
* First see if the pkt has auto-request sense data with it....
* Look at the packet state first so we don't take a performance
* hit looking at the arq enabled flag unless absolutely necessary.
*/
if ((pktp->pkt_state & STATE_ARQ_DONE) &&
(un->un_f_arq_enabled == TRUE)) {
/*
* The HBA did an auto request sense for this command so check
* for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
* driver command that should not be retried.
*/
if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
/*
* Save the relevant sense info into the xp for the
* original cmd.
*/
struct scsi_arq_status *asp;
asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
xp->xb_sense_status =
*((uchar_t *)(&(asp->sts_rqpkt_status)));
xp->xb_sense_state = asp->sts_rqpkt_state;
xp->xb_sense_resid = asp->sts_rqpkt_resid;
if (pktp->pkt_state & STATE_XARQ_DONE) {
actual_len = MAX_SENSE_LENGTH -
xp->xb_sense_resid;
bcopy(&asp->sts_sensedata, xp->xb_sense_data,
MAX_SENSE_LENGTH);
} else {
if (xp->xb_sense_resid > SENSE_LENGTH) {
actual_len = MAX_SENSE_LENGTH -
xp->xb_sense_resid;
} else {
actual_len = SENSE_LENGTH -
xp->xb_sense_resid;
}
if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
if ((((struct uscsi_cmd *)
(xp->xb_pktinfo))->uscsi_rqlen) >
actual_len) {
xp->xb_sense_resid =
(((struct uscsi_cmd *)
(xp->xb_pktinfo))->
uscsi_rqlen) - actual_len;
} else {
xp->xb_sense_resid = 0;
}
}
bcopy(&asp->sts_sensedata, xp->xb_sense_data,
SENSE_LENGTH);
}
/* fail the command */
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: arq done and FLAG_DIAGNOSE set\n");
sd_return_failed_command(un, bp, EIO);
goto exit;
}
#if (defined(__x86)) /* DMAFREE for x86 only */
/*
* We want to either retry or fail this command, so free
* the DMA resources here. If we retry the command then
* the DMA resources will be reallocated in sd_start_cmds().
* Note that when PKT_DMA_PARTIAL is used, this reallocation
* causes the *entire* transfer to start over again from the
* beginning of the request, even for PARTIAL chunks that
* have already transferred successfully.
*/
if ((un->un_f_is_fibre == TRUE) &&
((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
((pktp->pkt_flags & FLAG_SENSING) == 0)) {
scsi_dmafree(pktp);
xp->xb_pkt_flags |= SD_XB_DMA_FREED;
}
#endif
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: arq done, sd_handle_auto_request_sense\n");
sd_handle_auto_request_sense(un, bp, xp, pktp);
goto exit;
}
/* Next see if this is the REQUEST SENSE pkt for the instance */
if (pktp->pkt_flags & FLAG_SENSING) {
/* This pktp is from the unit's REQUEST_SENSE command */
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: sd_handle_request_sense\n");
sd_handle_request_sense(un, bp, xp, pktp);
goto exit;
}
/*
* Check to see if the command successfully completed as requested;
* this is the most common case (and also the hot performance path).
*
* Requirements for successful completion are:
* pkt_reason is CMD_CMPLT and packet status is status good.
* In addition:
* - A residual of zero indicates successful completion no matter what
* the command is.
* - If the residual is not zero and the command is not a read or
* write, then it's still defined as successful completion. In other
* words, if the command is a read or write the residual must be
* zero for successful completion.
* - If the residual is not zero and the command is a read or
* write, and it's a USCSICMD, then it's still defined as
* successful completion.
*/
if ((pktp->pkt_reason == CMD_CMPLT) &&
(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
/*
* Return all USCSI commands on good status
*/
if (pktp->pkt_resid == 0) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: returning command for resid == 0\n");
} else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
SD_UPDATE_B_RESID(bp, pktp);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: returning command for resid != 0\n");
} else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
SD_UPDATE_B_RESID(bp, pktp);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: returning uscsi command\n");
} else {
goto not_successful;
}
sd_return_command(un, bp);
/*
* Decrement counter to indicate that the callback routine
* is done.
*/
un->un_in_callback--;
ASSERT(un->un_in_callback >= 0);
mutex_exit(SD_MUTEX(un));
return;
}
not_successful:
#if (defined(__x86)) /* DMAFREE for x86 only */
/*
* The following is based upon knowledge of the underlying transport
* and its use of DMA resources. This code should be removed when
* PKT_DMA_PARTIAL support is taken out of the disk driver in favor
* of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
* and sd_start_cmds().
*
* Free any DMA resources associated with this command if there
* is a chance it could be retried or enqueued for later retry.
* If we keep the DMA binding then mpxio cannot reissue the
* command on another path whenever a path failure occurs.
*
* Note that when PKT_DMA_PARTIAL is used, free/reallocation
* causes the *entire* transfer to start over again from the
* beginning of the request, even for PARTIAL chunks that
* have already transferred successfully.
*
* This is only done for non-uscsi commands (and also skipped for the
* driver's internal RQS command). Also just do this for Fibre Channel
* devices as these are the only ones that support mpxio.
*/
if ((un->un_f_is_fibre == TRUE) &&
((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
((pktp->pkt_flags & FLAG_SENSING) == 0)) {
scsi_dmafree(pktp);
xp->xb_pkt_flags |= SD_XB_DMA_FREED;
}
#endif
/*
* The command did not successfully complete as requested so check
* for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
* driver command that should not be retried so just return. If
* FLAG_DIAGNOSE is not set the error will be processed below.
*/
if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
/*
* Issue a request sense if a check condition caused the error
* (we handle the auto request sense case above), otherwise
* just fail the command.
*/
if ((pktp->pkt_reason == CMD_CMPLT) &&
(SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
sd_send_request_sense_command(un, bp, pktp);
} else {
sd_return_failed_command(un, bp, EIO);
}
goto exit;
}
/*
* The command did not successfully complete as requested so process
* the error, retry, and/or attempt recovery.
*/
switch (pktp->pkt_reason) {
case CMD_CMPLT:
switch (SD_GET_PKT_STATUS(pktp)) {
case STATUS_GOOD:
/*
* The command completed successfully with a non-zero
* residual
*/
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: STATUS_GOOD \n");
sd_pkt_status_good(un, bp, xp, pktp);
break;
case STATUS_CHECK:
case STATUS_TERMINATED:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
sd_pkt_status_check_condition(un, bp, xp, pktp);
break;
case STATUS_BUSY:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: STATUS_BUSY\n");
sd_pkt_status_busy(un, bp, xp, pktp);
break;
case STATUS_RESERVATION_CONFLICT:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: STATUS_RESERVATION_CONFLICT\n");
sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
break;
case STATUS_QFULL:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: STATUS_QFULL\n");
sd_pkt_status_qfull(un, bp, xp, pktp);
break;
case STATUS_MET:
case STATUS_INTERMEDIATE:
case STATUS_SCSI2:
case STATUS_INTERMEDIATE_MET:
case STATUS_ACA_ACTIVE:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"Unexpected SCSI status received: 0x%x\n",
SD_GET_PKT_STATUS(pktp));
/*
* Mark the ssc_flags when detected invalid status
* code for non-USCSI command.
*/
if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
0, "stat-code");
}
sd_return_failed_command(un, bp, EIO);
break;
default:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"Invalid SCSI status received: 0x%x\n",
SD_GET_PKT_STATUS(pktp));
if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
0, "stat-code");
}
sd_return_failed_command(un, bp, EIO);
break;
}
break;
case CMD_INCOMPLETE:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: CMD_INCOMPLETE\n");
sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
break;
case CMD_TRAN_ERR:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: CMD_TRAN_ERR\n");
sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
break;
case CMD_RESET:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: CMD_RESET \n");
sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
break;
case CMD_ABORTED:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: CMD_ABORTED \n");
sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
break;
case CMD_TIMEOUT:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: CMD_TIMEOUT\n");
sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
break;
case CMD_UNX_BUS_FREE:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: CMD_UNX_BUS_FREE \n");
sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
break;
case CMD_TAG_REJECT:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: CMD_TAG_REJECT\n");
sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
break;
default:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sdintr: default\n");
/*
* Mark the ssc_flags for detecting invliad pkt_reason.
*/
if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
0, "pkt-reason");
}
sd_pkt_reason_default(un, bp, xp, pktp);
break;
}
exit:
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
/* Decrement counter to indicate that the callback routine is done. */
un->un_in_callback--;
ASSERT(un->un_in_callback >= 0);
/*
* At this point, the pkt has been dispatched, ie, it is either
* being re-tried or has been returned to its caller and should
* not be referenced.
*/
mutex_exit(SD_MUTEX(un));
}
/*
* Function: sd_print_incomplete_msg
*
* Description: Prints the error message for a CMD_INCOMPLETE error.
*
* Arguments: un - ptr to associated softstate for the device.
* bp - ptr to the buf(9S) for the command.
* arg - message string ptr
* code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
* or SD_NO_RETRY_ISSUED.
*
* Context: May be called under interrupt context
*/
static void
sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
{
struct scsi_pkt *pktp;
char *msgp;
char *cmdp = arg;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(arg != NULL);
pktp = SD_GET_PKTP(bp);
ASSERT(pktp != NULL);
switch (code) {
case SD_DELAYED_RETRY_ISSUED:
case SD_IMMEDIATE_RETRY_ISSUED:
msgp = "retrying";
break;
case SD_NO_RETRY_ISSUED:
default:
msgp = "giving up";
break;
}
if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"incomplete %s- %s\n", cmdp, msgp);
}
}
/*
* Function: sd_pkt_status_good
*
* Description: Processing for a STATUS_GOOD code in pkt_status.
*
* Context: May be called under interrupt context
*/
static void
sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
char *cmdp;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
ASSERT(pktp->pkt_reason == CMD_CMPLT);
ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
ASSERT(pktp->pkt_resid != 0);
SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
SD_UPDATE_ERRSTATS(un, sd_harderrs);
switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
case SCMD_READ:
cmdp = "read";
break;
case SCMD_WRITE:
cmdp = "write";
break;
default:
SD_UPDATE_B_RESID(bp, pktp);
sd_return_command(un, bp);
SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
return;
}
/*
* See if we can retry the read/write, preferrably immediately.
* If retries are exhaused, then sd_retry_command() will update
* the b_resid count.
*/
sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
cmdp, EIO, (clock_t)0, NULL);
SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
}
/*
* Function: sd_handle_request_sense
*
* Description: Processing for non-auto Request Sense command.
*
* Arguments: un - ptr to associated softstate
* sense_bp - ptr to buf(9S) for the RQS command
* sense_xp - ptr to the sd_xbuf for the RQS command
* sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
*
* Context: May be called under interrupt context
*/
static void
sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
{
struct buf *cmd_bp; /* buf for the original command */
struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */
struct scsi_pkt *cmd_pktp; /* pkt for the original command */
size_t actual_len; /* actual sense data length */
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(sense_bp != NULL);
ASSERT(sense_xp != NULL);
ASSERT(sense_pktp != NULL);
/*
* Note the sense_bp, sense_xp, and sense_pktp here are for the
* RQS command and not the original command.
*/
ASSERT(sense_pktp == un->un_rqs_pktp);
ASSERT(sense_bp == un->un_rqs_bp);
ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
(FLAG_SENSING | FLAG_HEAD));
ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
FLAG_SENSING) == FLAG_SENSING);
/* These are the bp, xp, and pktp for the original command */
cmd_bp = sense_xp->xb_sense_bp;
cmd_xp = SD_GET_XBUF(cmd_bp);
cmd_pktp = SD_GET_PKTP(cmd_bp);
if (sense_pktp->pkt_reason != CMD_CMPLT) {
/*
* The REQUEST SENSE command failed. Release the REQUEST
* SENSE command for re-use, get back the bp for the original
* command, and attempt to re-try the original command if
* FLAG_DIAGNOSE is not set in the original packet.
*/
SD_UPDATE_ERRSTATS(un, sd_harderrs);
if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
cmd_bp = sd_mark_rqs_idle(un, sense_xp);
sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
NULL, NULL, EIO, (clock_t)0, NULL);
return;
}
}
/*
* Save the relevant sense info into the xp for the original cmd.
*
* Note: if the request sense failed the state info will be zero
* as set in sd_mark_rqs_busy()
*/
cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
cmd_xp->xb_sense_state = sense_pktp->pkt_state;
actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
(((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
SENSE_LENGTH)) {
bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
MAX_SENSE_LENGTH);
cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
} else {
bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
SENSE_LENGTH);
if (actual_len < SENSE_LENGTH) {
cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
} else {
cmd_xp->xb_sense_resid = 0;
}
}
/*
* Free up the RQS command....
* NOTE:
* Must do this BEFORE calling sd_validate_sense_data!
* sd_validate_sense_data may return the original command in
* which case the pkt will be freed and the flags can no
* longer be touched.
* SD_MUTEX is held through this process until the command
* is dispatched based upon the sense data, so there are
* no race conditions.
*/
(void) sd_mark_rqs_idle(un, sense_xp);
/*
* For a retryable command see if we have valid sense data, if so then
* turn it over to sd_decode_sense() to figure out the right course of
* action. Just fail a non-retryable command.
*/
if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
SD_SENSE_DATA_IS_VALID) {
sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
}
} else {
SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
(uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
(uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
sd_return_failed_command(un, cmd_bp, EIO);
}
}
/*
* Function: sd_handle_auto_request_sense
*
* Description: Processing for auto-request sense information.
*
* Arguments: un - ptr to associated softstate
* bp - ptr to buf(9S) for the command
* xp - ptr to the sd_xbuf for the command
* pktp - ptr to the scsi_pkt(9S) for the command
*
* Context: May be called under interrupt context
*/
static void
sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
struct scsi_arq_status *asp;
size_t actual_len;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
ASSERT(pktp != un->un_rqs_pktp);
ASSERT(bp != un->un_rqs_bp);
/*
* For auto-request sense, we get a scsi_arq_status back from
* the HBA, with the sense data in the sts_sensedata member.
* The pkt_scbp of the packet points to this scsi_arq_status.
*/
asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
if (asp->sts_rqpkt_reason != CMD_CMPLT) {
/*
* The auto REQUEST SENSE failed; see if we can re-try
* the original command.
*/
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"auto request sense failed (reason=%s)\n",
scsi_rname(asp->sts_rqpkt_reason));
sd_reset_target(un, pktp);
sd_retry_command(un, bp, SD_RETRIES_STANDARD,
NULL, NULL, EIO, (clock_t)0, NULL);
return;
}
/* Save the relevant sense info into the xp for the original cmd. */
xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
xp->xb_sense_state = asp->sts_rqpkt_state;
xp->xb_sense_resid = asp->sts_rqpkt_resid;
if (xp->xb_sense_state & STATE_XARQ_DONE) {
actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
bcopy(&asp->sts_sensedata, xp->xb_sense_data,
MAX_SENSE_LENGTH);
} else {
if (xp->xb_sense_resid > SENSE_LENGTH) {
actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
} else {
actual_len = SENSE_LENGTH - xp->xb_sense_resid;
}
if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
if ((((struct uscsi_cmd *)
(xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
xp->xb_sense_resid = (((struct uscsi_cmd *)
(xp->xb_pktinfo))->uscsi_rqlen) -
actual_len;
} else {
xp->xb_sense_resid = 0;
}
}
bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
}
/*
* See if we have valid sense data, if so then turn it over to
* sd_decode_sense() to figure out the right course of action.
*/
if (sd_validate_sense_data(un, bp, xp, actual_len) ==
SD_SENSE_DATA_IS_VALID) {
sd_decode_sense(un, bp, xp, pktp);
}
}
/*
* Function: sd_print_sense_failed_msg
*
* Description: Print log message when RQS has failed.
*
* Arguments: un - ptr to associated softstate
* bp - ptr to buf(9S) for the command
* arg - generic message string ptr
* code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
* or SD_NO_RETRY_ISSUED
*
* Context: May be called from interrupt context
*/
static void
sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
int code)
{
char *msgp = arg;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
}
}
/*
* Function: sd_validate_sense_data
*
* Description: Check the given sense data for validity.
* If the sense data is not valid, the command will
* be either failed or retried!
*
* Return Code: SD_SENSE_DATA_IS_INVALID
* SD_SENSE_DATA_IS_VALID
*
* Context: May be called from interrupt context
*/
static int
sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
size_t actual_len)
{
struct scsi_extended_sense *esp;
struct scsi_pkt *pktp;
char *msgp = NULL;
sd_ssc_t *sscp;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(bp != un->un_rqs_bp);
ASSERT(xp != NULL);
ASSERT(un->un_fm_private != NULL);
pktp = SD_GET_PKTP(bp);
ASSERT(pktp != NULL);
sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
ASSERT(sscp != NULL);
/*
* Check the status of the RQS command (auto or manual).
*/
switch (xp->xb_sense_status & STATUS_MASK) {
case STATUS_GOOD:
break;
case STATUS_RESERVATION_CONFLICT:
sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
return (SD_SENSE_DATA_IS_INVALID);
case STATUS_BUSY:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"Busy Status on REQUEST SENSE\n");
sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
return (SD_SENSE_DATA_IS_INVALID);
case STATUS_QFULL:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"QFULL Status on REQUEST SENSE\n");
sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
return (SD_SENSE_DATA_IS_INVALID);
case STATUS_CHECK:
case STATUS_TERMINATED:
msgp = "Check Condition on REQUEST SENSE\n";
goto sense_failed;
default:
msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
goto sense_failed;
}
/*
* See if we got the minimum required amount of sense data.
* Note: We are assuming the returned sense data is SENSE_LENGTH bytes
* or less.
*/
if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
(actual_len == 0)) {
msgp = "Request Sense couldn't get sense data\n";
goto sense_failed;
}
if (actual_len < SUN_MIN_SENSE_LENGTH) {
msgp = "Not enough sense information\n";
/* Mark the ssc_flags for detecting invalid sense data */
if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
"sense-data");
}
goto sense_failed;
}
/*
* We require the extended sense data
*/
esp = (struct scsi_extended_sense *)xp->xb_sense_data;
if (esp->es_class != CLASS_EXTENDED_SENSE) {
if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
static char tmp[8];
static char buf[148];
char *p = (char *)(xp->xb_sense_data);
int i;
mutex_enter(&sd_sense_mutex);
(void) strcpy(buf, "undecodable sense information:");
for (i = 0; i < actual_len; i++) {
(void) sprintf(tmp, " 0x%x", *(p++) & 0xff);
(void) strcpy(&buf[strlen(buf)], tmp);
}
i = strlen(buf);
(void) strcpy(&buf[i], "-(assumed fatal)\n");
if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
scsi_log(SD_DEVINFO(un), sd_label,
CE_WARN, buf);
}
mutex_exit(&sd_sense_mutex);
}
/* Mark the ssc_flags for detecting invalid sense data */
if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
"sense-data");
}
/* Note: Legacy behavior, fail the command with no retry */
sd_return_failed_command(un, bp, EIO);
return (SD_SENSE_DATA_IS_INVALID);
}
/*
* Check that es_code is valid (es_class concatenated with es_code
* make up the "response code" field. es_class will always be 7, so
* make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the
* format.
*/
if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
(esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
(esp->es_code != CODE_FMT_DESCR_CURRENT) &&
(esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
(esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
/* Mark the ssc_flags for detecting invalid sense data */
if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
"sense-data");
}
goto sense_failed;
}
return (SD_SENSE_DATA_IS_VALID);
sense_failed:
/*
* If the request sense failed (for whatever reason), attempt
* to retry the original command.
*/
/*
* The SD_RETRY_DELAY value need to be adjusted here
* when SD_RETRY_DELAY change in sddef.h
*/
sd_retry_command(un, bp, SD_RETRIES_STANDARD,
sd_print_sense_failed_msg, msgp, EIO,
un->un_f_is_fibre ? drv_usectohz(100000) : (clock_t)0, NULL);
return (SD_SENSE_DATA_IS_INVALID);
}
/*
* Function: sd_decode_sense
*
* Description: Take recovery action(s) when SCSI Sense Data is received.
*
* Context: Interrupt context.
*/
static void
sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
struct scsi_pkt *pktp)
{
uint8_t sense_key;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(bp != un->un_rqs_bp);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
sense_key = scsi_sense_key(xp->xb_sense_data);
switch (sense_key) {
case KEY_NO_SENSE:
sd_sense_key_no_sense(un, bp, xp, pktp);
break;
case KEY_RECOVERABLE_ERROR:
sd_sense_key_recoverable_error(un, xp->xb_sense_data,
bp, xp, pktp);
break;
case KEY_NOT_READY:
sd_sense_key_not_ready(un, xp->xb_sense_data,
bp, xp, pktp);
break;
case KEY_MEDIUM_ERROR:
case KEY_HARDWARE_ERROR:
sd_sense_key_medium_or_hardware_error(un,
xp->xb_sense_data, bp, xp, pktp);
break;
case KEY_ILLEGAL_REQUEST:
sd_sense_key_illegal_request(un, bp, xp, pktp);
break;
case KEY_UNIT_ATTENTION:
sd_sense_key_unit_attention(un, xp->xb_sense_data,
bp, xp, pktp);
break;
case KEY_WRITE_PROTECT:
case KEY_VOLUME_OVERFLOW:
case KEY_MISCOMPARE:
sd_sense_key_fail_command(un, bp, xp, pktp);
break;
case KEY_BLANK_CHECK:
sd_sense_key_blank_check(un, bp, xp, pktp);
break;
case KEY_ABORTED_COMMAND:
sd_sense_key_aborted_command(un, bp, xp, pktp);
break;
case KEY_VENDOR_UNIQUE:
case KEY_COPY_ABORTED:
case KEY_EQUAL:
case KEY_RESERVED:
default:
sd_sense_key_default(un, xp->xb_sense_data,
bp, xp, pktp);
break;
}
}
/*
* Function: sd_dump_memory
*
* Description: Debug logging routine to print the contents of a user provided
* buffer. The output of the buffer is broken up into 256 byte
* segments due to a size constraint of the scsi_log.
* implementation.
*
* Arguments: un - ptr to softstate
* comp - component mask
* title - "title" string to preceed data when printed
* data - ptr to data block to be printed
* len - size of data block to be printed
* fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
*
* Context: May be called from interrupt context
*/
#define SD_DUMP_MEMORY_BUF_SIZE 256
static char *sd_dump_format_string[] = {
" 0x%02x",
" %c"
};
static void
sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
int len, int fmt)
{
int i, j;
int avail_count;
int start_offset;
int end_offset;
size_t entry_len;
char *bufp;
char *local_buf;
char *format_string;
ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
/*
* In the debug version of the driver, this function is called from a
* number of places which are NOPs in the release driver.
* The debug driver therefore has additional methods of filtering
* debug output.
*/
#ifdef SDDEBUG
/*
* In the debug version of the driver we can reduce the amount of debug
* messages by setting sd_error_level to something other than
* SCSI_ERR_ALL and clearing bits in sd_level_mask and
* sd_component_mask.
*/
if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
(sd_error_level != SCSI_ERR_ALL)) {
return;
}
if (((sd_component_mask & comp) == 0) ||
(sd_error_level != SCSI_ERR_ALL)) {
return;
}
#else
if (sd_error_level != SCSI_ERR_ALL) {
return;
}
#endif
local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
bufp = local_buf;
/*
* Available length is the length of local_buf[], minus the
* length of the title string, minus one for the ":", minus
* one for the newline, minus one for the NULL terminator.
* This gives the #bytes available for holding the printed
* values from the given data buffer.
*/
if (fmt == SD_LOG_HEX) {
format_string = sd_dump_format_string[0];
} else /* SD_LOG_CHAR */ {
format_string = sd_dump_format_string[1];
}
/*
* Available count is the number of elements from the given
* data buffer that we can fit into the available length.
* This is based upon the size of the format string used.
* Make one entry and find it's size.
*/
(void) sprintf(bufp, format_string, data[0]);
entry_len = strlen(bufp);
avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
j = 0;
while (j < len) {
bufp = local_buf;
bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
start_offset = j;
end_offset = start_offset + avail_count;
(void) sprintf(bufp, "%s:", title);
bufp += strlen(bufp);
for (i = start_offset; ((i < end_offset) && (j < len));
i++, j++) {
(void) sprintf(bufp, format_string, data[i]);
bufp += entry_len;
}
(void) sprintf(bufp, "\n");
scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
}
kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
}
/*
* Function: sd_print_sense_msg
*
* Description: Log a message based upon the given sense data.
*
* Arguments: un - ptr to associated softstate
* bp - ptr to buf(9S) for the command
* arg - ptr to associate sd_sense_info struct
* code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
* or SD_NO_RETRY_ISSUED
*
* Context: May be called from interrupt context
*/
static void
sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
{
struct sd_xbuf *xp;
struct scsi_pkt *pktp;
uint8_t *sensep;
daddr_t request_blkno;
diskaddr_t err_blkno;
int severity;
int pfa_flag;
extern struct scsi_key_strings scsi_cmds[];
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
pktp = SD_GET_PKTP(bp);
ASSERT(pktp != NULL);
ASSERT(arg != NULL);
severity = ((struct sd_sense_info *)(arg))->ssi_severity;
pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
if ((code == SD_DELAYED_RETRY_ISSUED) ||
(code == SD_IMMEDIATE_RETRY_ISSUED)) {
severity = SCSI_ERR_RETRYABLE;
}
/* Use absolute block number for the request block number */
request_blkno = xp->xb_blkno;
/*
* Now try to get the error block number from the sense data
*/
sensep = xp->xb_sense_data;
if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
(uint64_t *)&err_blkno)) {
/*
* We retrieved the error block number from the information
* portion of the sense data.
*
* For USCSI commands we are better off using the error
* block no. as the requested block no. (This is the best
* we can estimate.)
*/
if ((SD_IS_BUFIO(xp) == FALSE) &&
((pktp->pkt_flags & FLAG_SILENT) == 0)) {
request_blkno = err_blkno;
}
} else {
/*
* Without the es_valid bit set (for fixed format) or an
* information descriptor (for descriptor format) we cannot
* be certain of the error blkno, so just use the
* request_blkno.
*/
err_blkno = (diskaddr_t)request_blkno;
}
/*
* The following will log the buffer contents for the release driver
* if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
* level is set to verbose.
*/
sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
(uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
sd_dump_memory(un, SD_LOG_IO, "Sense Data",
(uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
if (pfa_flag == FALSE) {
/* This is normally only set for USCSI */
if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
return;
}
if ((SD_IS_BUFIO(xp) == TRUE) &&
(((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
(severity < sd_error_level))) {
return;
}
}
if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
(pktp->pkt_resid == 0))) {
scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
request_blkno, err_blkno, scsi_cmds,
(struct scsi_extended_sense *)sensep,
un->un_additional_codes, NULL);
}
}
/*
* Function: sd_sense_key_no_sense
*
* Description: Recovery action when sense data was not received.
*
* Context: May be called from interrupt context
*/
static void
sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
struct scsi_pkt *pktp)
{
struct sd_sense_info si;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
si.ssi_severity = SCSI_ERR_FATAL;
si.ssi_pfa_flag = FALSE;
SD_UPDATE_ERRSTATS(un, sd_softerrs);
sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
&si, EIO, (clock_t)0, NULL);
}
/*
* Function: sd_sense_key_recoverable_error
*
* Description: Recovery actions for a SCSI "Recovered Error" sense key.
*
* Context: May be called from interrupt context
*/
static void
sd_sense_key_recoverable_error(struct sd_lun *un, uint8_t *sense_datap,
struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
struct sd_sense_info si;
uint8_t asc = scsi_sense_asc(sense_datap);
uint8_t ascq = scsi_sense_ascq(sense_datap);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
/*
* 0x00, 0x1D: ATA PASSTHROUGH INFORMATION AVAILABLE
*/
if (asc == 0x00 && ascq == 0x1D) {
sd_return_command(un, bp);
return;
}
/*
* 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
*/
if ((asc == 0x5D) && (sd_report_pfa != 0)) {
SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
si.ssi_severity = SCSI_ERR_INFO;
si.ssi_pfa_flag = TRUE;
} else {
SD_UPDATE_ERRSTATS(un, sd_softerrs);
SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
si.ssi_severity = SCSI_ERR_RECOVERED;
si.ssi_pfa_flag = FALSE;
}
if (pktp->pkt_resid == 0) {
sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
sd_return_command(un, bp);
return;
}
sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
&si, EIO, (clock_t)0, NULL);
}
/*
* Function: sd_sense_key_not_ready
*
* Description: Recovery actions for a SCSI "Not Ready" sense key.
*
* Context: May be called from interrupt context
*/
static void
sd_sense_key_not_ready(struct sd_lun *un, uint8_t *sense_datap, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
struct sd_sense_info si;
uint8_t asc = scsi_sense_asc(sense_datap);
uint8_t ascq = scsi_sense_ascq(sense_datap);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
si.ssi_severity = SCSI_ERR_FATAL;
si.ssi_pfa_flag = FALSE;
/*
* Update error stats after first NOT READY error. Disks may have
* been powered down and may need to be restarted. For CDROMs,
* report NOT READY errors only if media is present.
*/
if ((ISCD(un) && (asc == 0x3A)) ||
(xp->xb_nr_retry_count > 0)) {
SD_UPDATE_ERRSTATS(un, sd_harderrs);
SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
}
/*
* Just fail if the "not ready" retry limit has been reached.
*/
if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
/* Special check for error message printing for removables. */
if (un->un_f_has_removable_media && (asc == 0x04) &&
(ascq >= 0x04)) {
si.ssi_severity = SCSI_ERR_ALL;
}
goto fail_command;
}
/*
* Check the ASC and ASCQ in the sense data as needed, to determine
* what to do.
*/
switch (asc) {
case 0x04: /* LOGICAL UNIT NOT READY */
/*
* disk drives that don't spin up result in a very long delay
* in format without warning messages. We will log a message
* if the error level is set to verbose.
*/
if (sd_error_level < SCSI_ERR_RETRYABLE) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"logical unit not ready, resetting disk\n");
}
/*
* There are different requirements for CDROMs and disks for
* the number of retries. If a CD-ROM is giving this, it is
* probably reading TOC and is in the process of getting
* ready, so we should keep on trying for a long time to make
* sure that all types of media are taken in account (for
* some media the drive takes a long time to read TOC). For
* disks we do not want to retry this too many times as this
* can cause a long hang in format when the drive refuses to
* spin up (a very common failure).
*/
switch (ascq) {
case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */
/*
* Disk drives frequently refuse to spin up which
* results in a very long hang in format without
* warning messages.
*
* Note: This code preserves the legacy behavior of
* comparing xb_nr_retry_count against zero for fibre
* channel targets instead of comparing against the
* un_reset_retry_count value. The reason for this
* discrepancy has been so utterly lost beneath the
* Sands of Time that even Indiana Jones could not
* find it.
*/
if (un->un_f_is_fibre == TRUE) {
if (((sd_level_mask & SD_LOGMASK_DIAG) ||
(xp->xb_nr_retry_count > 0)) &&
(un->un_startstop_timeid == NULL)) {
scsi_log(SD_DEVINFO(un), sd_label,
CE_WARN, "logical unit not ready, "
"resetting disk\n");
sd_reset_target(un, pktp);
}
} else {
if (((sd_level_mask & SD_LOGMASK_DIAG) ||
(xp->xb_nr_retry_count >
un->un_reset_retry_count)) &&
(un->un_startstop_timeid == NULL)) {
scsi_log(SD_DEVINFO(un), sd_label,
CE_WARN, "logical unit not ready, "
"resetting disk\n");
sd_reset_target(un, pktp);
}
}
break;
case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */
/*
* If the target is in the process of becoming
* ready, just proceed with the retry. This can
* happen with CD-ROMs that take a long time to
* read TOC after a power cycle or reset.
*/
goto do_retry;
case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
break;
case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
/*
* Retries cannot help here so just fail right away.
*/
goto fail_command;
case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */
case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */
case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */
case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */
case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */
default: /* Possible future codes in SCSI spec? */
/*
* For removable-media devices, do not retry if
* ASCQ > 2 as these result mostly from USCSI commands
* on MMC devices issued to check status of an
* operation initiated in immediate mode. Also for
* ASCQ >= 4 do not print console messages as these
* mainly represent a user-initiated operation
* instead of a system failure.
*/
if (un->un_f_has_removable_media) {
si.ssi_severity = SCSI_ERR_ALL;
goto fail_command;
}
break;
}
/*
* As part of our recovery attempt for the NOT READY
* condition, we issue a START STOP UNIT command. However
* we want to wait for a short delay before attempting this
* as there may still be more commands coming back from the
* target with the check condition. To do this we use
* timeout(9F) to call sd_start_stop_unit_callback() after
* the delay interval expires. (sd_start_stop_unit_callback()
* dispatches sd_start_stop_unit_task(), which will issue
* the actual START STOP UNIT command. The delay interval
* is one-half of the delay that we will use to retry the
* command that generated the NOT READY condition.
*
* Note that we could just dispatch sd_start_stop_unit_task()
* from here and allow it to sleep for the delay interval,
* but then we would be tying up the taskq thread
* uncesessarily for the duration of the delay.
*
* Do not issue the START STOP UNIT if the current command
* is already a START STOP UNIT.
*/
if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
break;
}
/*
* Do not schedule the timeout if one is already pending.
*/
if (un->un_startstop_timeid != NULL) {
SD_INFO(SD_LOG_ERROR, un,
"sd_sense_key_not_ready: restart already issued to"
" %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
ddi_get_instance(SD_DEVINFO(un)));
break;
}
/*
* Schedule the START STOP UNIT command, then queue the command
* for a retry.
*
* Note: A timeout is not scheduled for this retry because we
* want the retry to be serial with the START_STOP_UNIT. The
* retry will be started when the START_STOP_UNIT is completed
* in sd_start_stop_unit_task.
*/
un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
un, un->un_busy_timeout / 2);
xp->xb_nr_retry_count++;
sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
return;
case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
if (sd_error_level < SCSI_ERR_RETRYABLE) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"unit does not respond to selection\n");
}
break;
case 0x3A: /* MEDIUM NOT PRESENT */
if (sd_error_level >= SCSI_ERR_FATAL) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"Caddy not inserted in drive\n");
}
sr_ejected(un);
un->un_mediastate = DKIO_EJECTED;
/* The state has changed, inform the media watch routines */
cv_broadcast(&un->un_state_cv);
/* Just fail if no media is present in the drive. */
goto fail_command;
default:
if (sd_error_level < SCSI_ERR_RETRYABLE) {
scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
"Unit not Ready. Additional sense code 0x%x\n",
asc);
}
break;
}
do_retry:
/*
* Retry the command, as some targets may report NOT READY for
* several seconds after being reset.
*/
xp->xb_nr_retry_count++;
si.ssi_severity = SCSI_ERR_RETRYABLE;
sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
&si, EIO, un->un_busy_timeout, NULL);
return;
fail_command:
sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
sd_return_failed_command(un, bp, EIO);
}
/*
* Function: sd_sense_key_medium_or_hardware_error
*
* Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
* sense key.
*
* Context: May be called from interrupt context
*/
static void
sd_sense_key_medium_or_hardware_error(struct sd_lun *un, uint8_t *sense_datap,
struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
struct sd_sense_info si;
uint8_t sense_key = scsi_sense_key(sense_datap);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
si.ssi_severity = SCSI_ERR_FATAL;
si.ssi_pfa_flag = FALSE;
if (sense_key == KEY_MEDIUM_ERROR) {
SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
}
SD_UPDATE_ERRSTATS(un, sd_harderrs);
if ((un->un_reset_retry_count != 0) &&
(xp->xb_retry_count == un->un_reset_retry_count)) {
mutex_exit(SD_MUTEX(un));
/* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
if (un->un_f_allow_bus_device_reset == TRUE) {
int reset_retval = 0;
if (un->un_f_lun_reset_enabled == TRUE) {
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_sense_key_medium_or_hardware_"
"error: issuing RESET_LUN\n");
reset_retval = scsi_reset(SD_ADDRESS(un),
RESET_LUN);
}
if (reset_retval == 0) {
SD_TRACE(SD_LOG_IO_CORE, un,
"sd_sense_key_medium_or_hardware_"
"error: issuing RESET_TARGET\n");
(void) scsi_reset(SD_ADDRESS(un),
RESET_TARGET);
}
}
mutex_enter(SD_MUTEX(un));
}
/*
* This really ought to be a fatal error, but we will retry anyway
* as some drives report this as a spurious error.
*/
sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
&si, EIO, (clock_t)0, NULL);
}
/*
* Function: sd_sense_key_illegal_request
*
* Description: Recovery actions for a SCSI "Illegal Request" sense key.
*
* Context: May be called from interrupt context
*/
static void
sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
struct sd_sense_info si;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
si.ssi_severity = SCSI_ERR_INFO;
si.ssi_pfa_flag = FALSE;
/* Pointless to retry if the target thinks it's an illegal request */
sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
sd_return_failed_command(un, bp, EIO);
}
/*
* Function: sd_sense_key_unit_attention
*
* Description: Recovery actions for a SCSI "Unit Attention" sense key.
*
* Context: May be called from interrupt context
*/
static void
sd_sense_key_unit_attention(struct sd_lun *un, uint8_t *sense_datap,
struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
/*
* For UNIT ATTENTION we allow retries for one minute. Devices
* like Sonoma can return UNIT ATTENTION close to a minute
* under certain conditions.
*/
int retry_check_flag = SD_RETRIES_UA;
boolean_t kstat_updated = B_FALSE;
struct sd_sense_info si;
uint8_t asc = scsi_sense_asc(sense_datap);
uint8_t ascq = scsi_sense_ascq(sense_datap);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
si.ssi_severity = SCSI_ERR_INFO;
si.ssi_pfa_flag = FALSE;
switch (asc) {
case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */
if (sd_report_pfa != 0) {
SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
si.ssi_pfa_flag = TRUE;
retry_check_flag = SD_RETRIES_STANDARD;
goto do_retry;
}
break;
case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
un->un_resvd_status |=
(SD_LOST_RESERVE | SD_WANT_RESERVE);
}
if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
un, KM_NOSLEEP) == TASKQID_INVALID) {
/*
* If we can't dispatch the task we'll just
* live without descriptor sense. We can
* try again on the next "unit attention"
*/
SD_ERROR(SD_LOG_ERROR, un,
"sd_sense_key_unit_attention: "
"Could not dispatch "
"sd_reenable_dsense_task\n");
}
}
/* FALLTHRU */
case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
if (!un->un_f_has_removable_media) {
break;
}
/*
* When we get a unit attention from a removable-media device,
* it may be in a state that will take a long time to recover
* (e.g., from a reset). Since we are executing in interrupt
* context here, we cannot wait around for the device to come
* back. So hand this command off to sd_media_change_task()
* for deferred processing under taskq thread context. (Note
* that the command still may be failed if a problem is
* encountered at a later time.)
*/
if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
KM_NOSLEEP) == TASKQID_INVALID) {
/*
* Cannot dispatch the request so fail the command.
*/
SD_UPDATE_ERRSTATS(un, sd_harderrs);
SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
si.ssi_severity = SCSI_ERR_FATAL;
sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
sd_return_failed_command(un, bp, EIO);
}
/*
* If failed to dispatch sd_media_change_task(), we already
* updated kstat. If succeed to dispatch sd_media_change_task(),
* we should update kstat later if it encounters an error. So,
* we update kstat_updated flag here.
*/
kstat_updated = B_TRUE;
/*
* Either the command has been successfully dispatched to a
* task Q for retrying, or the dispatch failed. In either case
* do NOT retry again by calling sd_retry_command. This sets up
* two retries of the same command and when one completes and
* frees the resources the other will access freed memory,
* a bad thing.
*/
return;
default:
break;
}
/*
* ASC ASCQ
* 2A 09 Capacity data has changed
* 2A 01 Mode parameters changed
* 3F 0E Reported luns data has changed
* Arrays that support logical unit expansion should report
* capacity changes(2Ah/09). Mode parameters changed and
* reported luns data has changed are the approximation.
*/
if (((asc == 0x2a) && (ascq == 0x09)) ||
((asc == 0x2a) && (ascq == 0x01)) ||
((asc == 0x3f) && (ascq == 0x0e))) {
if (taskq_dispatch(sd_tq, sd_target_change_task, un,
KM_NOSLEEP) == TASKQID_INVALID) {
SD_ERROR(SD_LOG_ERROR, un,
"sd_sense_key_unit_attention: "
"Could not dispatch sd_target_change_task\n");
}
}
/*
* Update kstat if we haven't done that.
*/
if (!kstat_updated) {
SD_UPDATE_ERRSTATS(un, sd_harderrs);
SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
}
do_retry:
sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
EIO, SD_UA_RETRY_DELAY, NULL);
}
/*
* Function: sd_sense_key_fail_command
*
* Description: Use to fail a command when we don't like the sense key that
* was returned.
*
* Context: May be called from interrupt context
*/
static void
sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
struct scsi_pkt *pktp)
{
struct sd_sense_info si;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
si.ssi_severity = SCSI_ERR_FATAL;
si.ssi_pfa_flag = FALSE;
sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
sd_return_failed_command(un, bp, EIO);
}
/*
* Function: sd_sense_key_blank_check
*
* Description: Recovery actions for a SCSI "Blank Check" sense key.
* Has no monetary connotation.
*
* Context: May be called from interrupt context
*/
static void
sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
struct scsi_pkt *pktp)
{
struct sd_sense_info si;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
/*
* Blank check is not fatal for removable devices, therefore
* it does not require a console message.
*/
si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
SCSI_ERR_FATAL;
si.ssi_pfa_flag = FALSE;
sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
sd_return_failed_command(un, bp, EIO);
}
/*
* Function: sd_sense_key_aborted_command
*
* Description: Recovery actions for a SCSI "Aborted Command" sense key.
*
* Context: May be called from interrupt context
*/
static void
sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
struct sd_sense_info si;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
si.ssi_severity = SCSI_ERR_FATAL;
si.ssi_pfa_flag = FALSE;
SD_UPDATE_ERRSTATS(un, sd_harderrs);
/*
* This really ought to be a fatal error, but we will retry anyway
* as some drives report this as a spurious error.
*/
sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
&si, EIO, drv_usectohz(100000), NULL);
}
/*
* Function: sd_sense_key_default
*
* Description: Default recovery action for several SCSI sense keys (basically
* attempts a retry).
*
* Context: May be called from interrupt context
*/
static void
sd_sense_key_default(struct sd_lun *un, uint8_t *sense_datap, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
struct sd_sense_info si;
uint8_t sense_key = scsi_sense_key(sense_datap);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
SD_UPDATE_ERRSTATS(un, sd_harderrs);
/*
* Undecoded sense key. Attempt retries and hope that will fix
* the problem. Otherwise, we're dead.
*/
if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
}
si.ssi_severity = SCSI_ERR_FATAL;
si.ssi_pfa_flag = FALSE;
sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
&si, EIO, (clock_t)0, NULL);
}
/*
* Function: sd_print_retry_msg
*
* Description: Print a message indicating the retry action being taken.
*
* Arguments: un - ptr to associated softstate
* bp - ptr to buf(9S) for the command
* arg - not used.
* flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
* or SD_NO_RETRY_ISSUED
*
* Context: May be called from interrupt context
*/
/* ARGSUSED */
static void
sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
{
struct sd_xbuf *xp;
struct scsi_pkt *pktp;
char *reasonp;
char *msgp;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
pktp = SD_GET_PKTP(bp);
ASSERT(pktp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
ASSERT(!mutex_owned(&un->un_pm_mutex));
mutex_enter(&un->un_pm_mutex);
if ((un->un_state == SD_STATE_SUSPENDED) ||
(SD_DEVICE_IS_IN_LOW_POWER(un)) ||
(pktp->pkt_flags & FLAG_SILENT)) {
mutex_exit(&un->un_pm_mutex);
goto update_pkt_reason;
}
mutex_exit(&un->un_pm_mutex);
/*
* Suppress messages if they are all the same pkt_reason; with
* TQ, many (up to 256) are returned with the same pkt_reason.
* If we are in panic, then suppress the retry messages.
*/
switch (flag) {
case SD_NO_RETRY_ISSUED:
msgp = "giving up";
break;
case SD_IMMEDIATE_RETRY_ISSUED:
case SD_DELAYED_RETRY_ISSUED:
if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
((pktp->pkt_reason == un->un_last_pkt_reason) &&
(sd_error_level != SCSI_ERR_ALL))) {
return;
}
msgp = "retrying command";
break;
default:
goto update_pkt_reason;
}
reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
scsi_rname(pktp->pkt_reason));
if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
}
update_pkt_reason:
/*
* Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
* This is to prevent multiple console messages for the same failure
* condition. Note that un->un_last_pkt_reason is NOT restored if &
* when the command is retried successfully because there still may be
* more commands coming back with the same value of pktp->pkt_reason.
*/
if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
un->un_last_pkt_reason = pktp->pkt_reason;
}
}
/*
* Function: sd_print_cmd_incomplete_msg
*
* Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
*
* Arguments: un - ptr to associated softstate
* bp - ptr to buf(9S) for the command
* arg - passed to sd_print_retry_msg()
* code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
* or SD_NO_RETRY_ISSUED
*
* Context: May be called from interrupt context
*/
static void
sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
int code)
{
dev_info_t *dip;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
switch (code) {
case SD_NO_RETRY_ISSUED:
/* Command was failed. Someone turned off this target? */
if (un->un_state != SD_STATE_OFFLINE) {
/*
* Suppress message if we are detaching and
* device has been disconnected
* Note that DEVI_IS_DEVICE_REMOVED is a consolidation
* private interface and not part of the DDI
*/
dip = un->un_sd->sd_dev;
if (!(DEVI_IS_DETACHING(dip) &&
DEVI_IS_DEVICE_REMOVED(dip))) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"disk not responding to selection\n");
}
New_state(un, SD_STATE_OFFLINE);
}
break;
case SD_DELAYED_RETRY_ISSUED:
case SD_IMMEDIATE_RETRY_ISSUED:
default:
/* Command was successfully queued for retry */
sd_print_retry_msg(un, bp, arg, code);
break;
}
}
/*
* Function: sd_pkt_reason_cmd_incomplete
*
* Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
*
* Context: May be called from interrupt context
*/
static void
sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
/* Do not do a reset if selection did not complete */
/* Note: Should this not just check the bit? */
if (pktp->pkt_state != STATE_GOT_BUS) {
SD_UPDATE_ERRSTATS(un, sd_transerrs);
sd_reset_target(un, pktp);
}
/*
* If the target was not successfully selected, then set
* SD_RETRIES_FAILFAST to indicate that we lost communication
* with the target, and further retries and/or commands are
* likely to take a long time.
*/
if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
flag |= SD_RETRIES_FAILFAST;
}
SD_UPDATE_RESERVATION_STATUS(un, pktp);
sd_retry_command(un, bp, flag,
sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
}
/*
* Function: sd_pkt_reason_cmd_tran_err
*
* Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
*
* Context: May be called from interrupt context
*/
static void
sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
/*
* Do not reset if we got a parity error, or if
* selection did not complete.
*/
SD_UPDATE_ERRSTATS(un, sd_harderrs);
/* Note: Should this not just check the bit for pkt_state? */
if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
(pktp->pkt_state != STATE_GOT_BUS)) {
SD_UPDATE_ERRSTATS(un, sd_transerrs);
sd_reset_target(un, pktp);
}
SD_UPDATE_RESERVATION_STATUS(un, pktp);
sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
}
/*
* Function: sd_pkt_reason_cmd_reset
*
* Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
*
* Context: May be called from interrupt context
*/
static void
sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
struct scsi_pkt *pktp)
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
/* The target may still be running the command, so try to reset. */
SD_UPDATE_ERRSTATS(un, sd_transerrs);
sd_reset_target(un, pktp);
SD_UPDATE_RESERVATION_STATUS(un, pktp);
/*
* If pkt_reason is CMD_RESET chances are that this pkt got
* reset because another target on this bus caused it. The target
* that caused it should get CMD_TIMEOUT with pkt_statistics
* of STAT_TIMEOUT/STAT_DEV_RESET.
*/
sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
}
/*
* Function: sd_pkt_reason_cmd_aborted
*
* Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
*
* Context: May be called from interrupt context
*/
static void
sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
struct scsi_pkt *pktp)
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
/* The target may still be running the command, so try to reset. */
SD_UPDATE_ERRSTATS(un, sd_transerrs);
sd_reset_target(un, pktp);
SD_UPDATE_RESERVATION_STATUS(un, pktp);
/*
* If pkt_reason is CMD_ABORTED chances are that this pkt got
* aborted because another target on this bus caused it. The target
* that caused it should get CMD_TIMEOUT with pkt_statistics
* of STAT_TIMEOUT/STAT_DEV_RESET.
*/
sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
}
/*
* Function: sd_pkt_reason_cmd_timeout
*
* Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
*
* Context: May be called from interrupt context
*/
static void
sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
struct scsi_pkt *pktp)
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
SD_UPDATE_ERRSTATS(un, sd_transerrs);
sd_reset_target(un, pktp);
SD_UPDATE_RESERVATION_STATUS(un, pktp);
/*
* A command timeout indicates that we could not establish
* communication with the target, so set SD_RETRIES_FAILFAST
* as further retries/commands are likely to take a long time.
*/
sd_retry_command(un, bp,
(SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
}
/*
* Function: sd_pkt_reason_cmd_unx_bus_free
*
* Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
*
* Context: May be called from interrupt context
*/
static void
sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
SD_UPDATE_ERRSTATS(un, sd_harderrs);
SD_UPDATE_RESERVATION_STATUS(un, pktp);
funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
sd_print_retry_msg : NULL;
sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
}
/*
* Function: sd_pkt_reason_cmd_tag_reject
*
* Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
*
* Context: May be called from interrupt context
*/
static void
sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
SD_UPDATE_ERRSTATS(un, sd_harderrs);
pktp->pkt_flags = 0;
un->un_tagflags = 0;
if (un->un_f_opt_queueing == TRUE) {
un->un_throttle = min(un->un_throttle, 3);
} else {
un->un_throttle = 1;
}
mutex_exit(SD_MUTEX(un));
(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
mutex_enter(SD_MUTEX(un));
SD_UPDATE_RESERVATION_STATUS(un, pktp);
/* Legacy behavior not to check retry counts here. */
sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
}
/*
* Function: sd_pkt_reason_default
*
* Description: Default recovery actions for SCSA pkt_reason values that
* do not have more explicit recovery actions.
*
* Context: May be called from interrupt context
*/
static void
sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
struct scsi_pkt *pktp)
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
SD_UPDATE_ERRSTATS(un, sd_transerrs);
sd_reset_target(un, pktp);
SD_UPDATE_RESERVATION_STATUS(un, pktp);
sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
}
/*
* Function: sd_pkt_status_check_condition
*
* Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
*
* Context: May be called from interrupt context
*/
static void
sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
"entry: buf:0x%p xp:0x%p\n", bp, xp);
/*
* If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
* command will be retried after the request sense). Otherwise, retry
* the command. Note: we are issuing the request sense even though the
* retry limit may have been reached for the failed command.
*/
if (un->un_f_arq_enabled == FALSE) {
SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
"no ARQ, sending request sense command\n");
sd_send_request_sense_command(un, bp, pktp);
} else {
SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
"ARQ,retrying request sense command\n");
/*
* The SD_RETRY_DELAY value need to be adjusted here
* when SD_RETRY_DELAY change in sddef.h
*/
sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
NULL);
}
SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
}
/*
* Function: sd_pkt_status_busy
*
* Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
*
* Context: May be called from interrupt context
*/
static void
sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
struct scsi_pkt *pktp)
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_pkt_status_busy: entry\n");
/* If retries are exhausted, just fail the command. */
if (xp->xb_retry_count >= un->un_busy_retry_count) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"device busy too long\n");
sd_return_failed_command(un, bp, EIO);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_pkt_status_busy: exit\n");
return;
}
xp->xb_retry_count++;
/*
* Try to reset the target. However, we do not want to perform
* more than one reset if the device continues to fail. The reset
* will be performed when the retry count reaches the reset
* threshold. This threshold should be set such that at least
* one retry is issued before the reset is performed.
*/
if (xp->xb_retry_count ==
((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
int rval = 0;
mutex_exit(SD_MUTEX(un));
if (un->un_f_allow_bus_device_reset == TRUE) {
/*
* First try to reset the LUN; if we cannot then
* try to reset the target.
*/
if (un->un_f_lun_reset_enabled == TRUE) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_pkt_status_busy: RESET_LUN\n");
rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
}
if (rval == 0) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_pkt_status_busy: RESET_TARGET\n");
rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
}
}
if (rval == 0) {
/*
* If the RESET_LUN and/or RESET_TARGET failed,
* try RESET_ALL
*/
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_pkt_status_busy: RESET_ALL\n");
rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
}
mutex_enter(SD_MUTEX(un));
if (rval == 0) {
/*
* The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
* At this point we give up & fail the command.
*/
sd_return_failed_command(un, bp, EIO);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_pkt_status_busy: exit (failed cmd)\n");
return;
}
}
/*
* Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
* we have already checked the retry counts above.
*/
sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
EIO, un->un_busy_timeout, NULL);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_pkt_status_busy: exit\n");
}
/*
* Function: sd_pkt_status_reservation_conflict
*
* Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
* command status.
*
* Context: May be called from interrupt context
*/
static void
sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
struct sd_xbuf *xp, struct scsi_pkt *pktp)
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
/*
* If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
* conflict could be due to various reasons like incorrect keys, not
* registered or not reserved etc. So, we return EACCES to the caller.
*/
if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
int cmd = SD_GET_PKT_OPCODE(pktp);
if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
(cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
sd_return_failed_command(un, bp, EACCES);
return;
}
}
un->un_resvd_status |= SD_RESERVATION_CONFLICT;
if ((un->un_resvd_status & SD_FAILFAST) != 0) {
if (sd_failfast_enable != 0) {
/* By definition, we must panic here.... */
sd_panic_for_res_conflict(un);
/*NOTREACHED*/
}
SD_ERROR(SD_LOG_IO, un,
"sd_handle_resv_conflict: Disk Reserved\n");
sd_return_failed_command(un, bp, EACCES);
return;
}
/*
* 1147670: retry only if sd_retry_on_reservation_conflict
* property is set (default is 1). Retries will not succeed
* on a disk reserved by another initiator. HA systems
* may reset this via sd.conf to avoid these retries.
*
* Note: The legacy return code for this failure is EIO, however EACCES
* seems more appropriate for a reservation conflict.
*/
if (sd_retry_on_reservation_conflict == 0) {
SD_ERROR(SD_LOG_IO, un,
"sd_handle_resv_conflict: Device Reserved\n");
sd_return_failed_command(un, bp, EIO);
return;
}
/*
* Retry the command if we can.
*
* Note: The legacy return code for this failure is EIO, however EACCES
* seems more appropriate for a reservation conflict.
*/
sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
(clock_t)2, NULL);
}
/*
* Function: sd_pkt_status_qfull
*
* Description: Handle a QUEUE FULL condition from the target. This can
* occur if the HBA does not handle the queue full condition.
* (Basically this means third-party HBAs as Sun HBAs will
* handle the queue full condition.) Note that if there are
* some commands already in the transport, then the queue full
* has occurred because the queue for this nexus is actually
* full. If there are no commands in the transport, then the
* queue full is resulting from some other initiator or lun
* consuming all the resources at the target.
*
* Context: May be called from interrupt context
*/
static void
sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
struct scsi_pkt *pktp)
{
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(pktp != NULL);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_pkt_status_qfull: entry\n");
/*
* Just lower the QFULL throttle and retry the command. Note that
* we do not limit the number of retries here.
*/
sd_reduce_throttle(un, SD_THROTTLE_QFULL);
sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
SD_RESTART_TIMEOUT, NULL);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_pkt_status_qfull: exit\n");
}
/*
* Function: sd_reset_target
*
* Description: Issue a scsi_reset(9F), with either RESET_LUN,
* RESET_TARGET, or RESET_ALL.
*
* Context: May be called under interrupt context.
*/
static void
sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
{
int rval = 0;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(pktp != NULL);
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
/*
* No need to reset if the transport layer has already done so.
*/
if ((pktp->pkt_statistics &
(STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_reset_target: no reset\n");
return;
}
mutex_exit(SD_MUTEX(un));
if (un->un_f_allow_bus_device_reset == TRUE) {
if (un->un_f_lun_reset_enabled == TRUE) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_reset_target: RESET_LUN\n");
rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
}
if (rval == 0) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_reset_target: RESET_TARGET\n");
rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
}
}
if (rval == 0) {
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_reset_target: RESET_ALL\n");
(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
}
mutex_enter(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
}
/*
* Function: sd_target_change_task
*
* Description: Handle dynamic target change
*
* Context: Executes in a taskq() thread context
*/
static void
sd_target_change_task(void *arg)
{
struct sd_lun *un = arg;
uint64_t capacity;
diskaddr_t label_cap;
uint_t lbasize;
sd_ssc_t *ssc;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
if ((un->un_f_blockcount_is_valid == FALSE) ||
(un->un_f_tgt_blocksize_is_valid == FALSE)) {
return;
}
ssc = sd_ssc_init(un);
if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
&lbasize, SD_PATH_DIRECT) != 0) {
SD_ERROR(SD_LOG_ERROR, un,
"sd_target_change_task: fail to read capacity\n");
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
goto task_exit;
}
mutex_enter(SD_MUTEX(un));
if (capacity <= un->un_blockcount) {
mutex_exit(SD_MUTEX(un));
goto task_exit;
}
sd_update_block_info(un, lbasize, capacity);
mutex_exit(SD_MUTEX(un));
/*
* If lun is EFI labeled and lun capacity is greater than the
* capacity contained in the label, log a sys event.
*/
if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
(void*)SD_PATH_DIRECT) == 0) {
mutex_enter(SD_MUTEX(un));
if (un->un_f_blockcount_is_valid &&
un->un_blockcount > label_cap) {
mutex_exit(SD_MUTEX(un));
sd_log_lun_expansion_event(un, KM_SLEEP);
} else {
mutex_exit(SD_MUTEX(un));
}
}
task_exit:
sd_ssc_fini(ssc);
}
/*
* Function: sd_log_dev_status_event
*
* Description: Log EC_dev_status sysevent
*
* Context: Never called from interrupt context
*/
static void
sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
{
int err;
char *path;
nvlist_t *attr_list;
size_t n;
/* Allocate and build sysevent attribute list */
err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
if (err != 0) {
SD_ERROR(SD_LOG_ERROR, un,
"sd_log_dev_status_event: fail to allocate space\n");
return;
}
path = kmem_alloc(MAXPATHLEN, km_flag);
if (path == NULL) {
nvlist_free(attr_list);
SD_ERROR(SD_LOG_ERROR, un,
"sd_log_dev_status_event: fail to allocate space\n");
return;
}
n = snprintf(path, MAXPATHLEN, "/devices");
(void) ddi_pathname(SD_DEVINFO(un), path + n);
n = strlen(path);
n += snprintf(path + n, MAXPATHLEN - n, ":x");
/*
* On receipt of this event, the ZFS sysevent module will scan
* active zpools for child vdevs matching this physical path.
* In order to catch both whole disk pools and those with an
* EFI boot partition, generate separate sysevents for minor
* node 'a' and 'b'.
*/
for (char c = 'a'; c < 'c'; c++) {
path[n - 1] = c;
err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
if (err != 0) {
SD_ERROR(SD_LOG_ERROR, un,
"sd_log_dev_status_event: fail to add attribute\n");
break;
}
err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR,
EC_DEV_STATUS, esc, attr_list, NULL, km_flag);
if (err != DDI_SUCCESS) {
SD_ERROR(SD_LOG_ERROR, un,
"sd_log_dev_status_event: fail to log sysevent\n");
break;
}
}
nvlist_free(attr_list);
kmem_free(path, MAXPATHLEN);
}
/*
* Function: sd_log_lun_expansion_event
*
* Description: Log lun expansion sys event
*
* Context: Never called from interrupt context
*/
static void
sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
{
sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
}
/*
* Function: sd_log_eject_request_event
*
* Description: Log eject request sysevent
*
* Context: Never called from interrupt context
*/
static void
sd_log_eject_request_event(struct sd_lun *un, int km_flag)
{
sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
}
/*
* Function: sd_media_change_task
*
* Description: Recovery action for CDROM to become available.
*
* Context: Executes in a taskq() thread context
*/
static void
sd_media_change_task(void *arg)
{
struct scsi_pkt *pktp = arg;
struct sd_lun *un;
struct buf *bp;
struct sd_xbuf *xp;
int err = 0;
int retry_count = 0;
int retry_limit = SD_UNIT_ATTENTION_RETRY/10;
struct sd_sense_info si;
ASSERT(pktp != NULL);
bp = (struct buf *)pktp->pkt_private;
ASSERT(bp != NULL);
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
un = SD_GET_UN(bp);
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(un->un_f_monitor_media_state);
si.ssi_severity = SCSI_ERR_INFO;
si.ssi_pfa_flag = FALSE;
/*
* When a reset is issued on a CDROM, it takes a long time to
* recover. First few attempts to read capacity and other things
* related to handling unit attention fail (with a ASC 0x4 and
* ASCQ 0x1). In that case we want to do enough retries and we want
* to limit the retries in other cases of genuine failures like
* no media in drive.
*/
while (retry_count++ < retry_limit) {
if ((err = sd_handle_mchange(un)) == 0) {
break;
}
if (err == EAGAIN) {
retry_limit = SD_UNIT_ATTENTION_RETRY;
}
/* Sleep for 0.5 sec. & try again */
delay(drv_usectohz(500000));
}
/*
* Dispatch (retry or fail) the original command here,
* along with appropriate console messages....
*
* Must grab the mutex before calling sd_retry_command,
* sd_print_sense_msg and sd_return_failed_command.
*/
mutex_enter(SD_MUTEX(un));
if (err != SD_CMD_SUCCESS) {
SD_UPDATE_ERRSTATS(un, sd_harderrs);
SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
si.ssi_severity = SCSI_ERR_FATAL;
sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
sd_return_failed_command(un, bp, EIO);
} else {
sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg,
&si, EIO, (clock_t)0, NULL);
}
mutex_exit(SD_MUTEX(un));
}
/*
* Function: sd_handle_mchange
*
* Description: Perform geometry validation & other recovery when CDROM
* has been removed from drive.
*
* Return Code: 0 for success
* errno-type return code of either sd_send_scsi_DOORLOCK() or
* sd_send_scsi_READ_CAPACITY()
*
* Context: Executes in a taskq() thread context
*/
static int
sd_handle_mchange(struct sd_lun *un)
{
uint64_t capacity;
uint32_t lbasize;
int rval;
sd_ssc_t *ssc;
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(un->un_f_monitor_media_state);
ssc = sd_ssc_init(un);
rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
SD_PATH_DIRECT_PRIORITY);
if (rval != 0)
goto failed;
mutex_enter(SD_MUTEX(un));
sd_update_block_info(un, lbasize, capacity);
if (un->un_errstats != NULL) {
struct sd_errstats *stp =
(struct sd_errstats *)un->un_errstats->ks_data;
stp->sd_capacity.value.ui64 = (uint64_t)
((uint64_t)un->un_blockcount *
(uint64_t)un->un_tgt_blocksize);
}
/*
* Check if the media in the device is writable or not
*/
if (ISCD(un)) {
sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
}
/*
* Note: Maybe let the strategy/partitioning chain worry about getting
* valid geometry.
*/
mutex_exit(SD_MUTEX(un));
cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
if (cmlb_validate(un->un_cmlbhandle, 0,
(void *)SD_PATH_DIRECT_PRIORITY) != 0) {
sd_ssc_fini(ssc);
return (EIO);
} else {
if (un->un_f_pkstats_enabled) {
sd_set_pstats(un);
SD_TRACE(SD_LOG_IO_PARTITION, un,
"sd_handle_mchange: un:0x%p pstats created and "
"set\n", un);
}
}
/*
* Try to lock the door
*/
rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
SD_PATH_DIRECT_PRIORITY);
failed:
if (rval != 0)
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
sd_ssc_fini(ssc);
return (rval);
}
/*
* Function: sd_send_scsi_DOORLOCK
*
* Description: Issue the scsi DOOR LOCK command
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
* flag - SD_REMOVAL_ALLOW
* SD_REMOVAL_PREVENT
* path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
* the normal command waitq, or SD_PATH_DIRECT_PRIORITY
* to use the USCSI "direct" chain and bypass the normal
* command waitq. SD_PATH_DIRECT_PRIORITY is used when this
* command is issued as part of an error recovery action.
*
* Return Code: 0 - Success
* errno return code from sd_ssc_send()
*
* Context: Can sleep.
*/
static int
sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
{
struct scsi_extended_sense sense_buf;
union scsi_cdb cdb;
struct uscsi_cmd ucmd_buf;
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
/* already determined doorlock is not supported, fake success */
if (un->un_f_doorlock_supported == FALSE) {
return (0);
}
/*
* If we are ejecting and see an SD_REMOVAL_PREVENT
* ignore the command so we can complete the eject
* operation.
*/
if (flag == SD_REMOVAL_PREVENT) {
mutex_enter(SD_MUTEX(un));
if (un->un_f_ejecting == TRUE) {
mutex_exit(SD_MUTEX(un));
return (EAGAIN);
}
mutex_exit(SD_MUTEX(un));
}
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
cdb.scc_cmd = SCMD_DOORLOCK;
cdb.cdb_opaque[4] = (uchar_t)flag;
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = CDB_GROUP0;
ucmd_buf.uscsi_bufaddr = NULL;
ucmd_buf.uscsi_buflen = 0;
ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
ucmd_buf.uscsi_timeout = 15;
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, path_flag);
if (status == 0)
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
(ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
(scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
/* fake success and skip subsequent doorlock commands */
un->un_f_doorlock_supported = FALSE;
return (0);
}
return (status);
}
/*
* Function: sd_send_scsi_READ_CAPACITY
*
* Description: This routine uses the scsi READ CAPACITY command to determine
* the device capacity in number of blocks and the device native
* block size. If this function returns a failure, then the
* values in *capp and *lbap are undefined. If the capacity
* returned is 0xffffffff then the lun is too large for a
* normal READ CAPACITY command and the results of a
* READ CAPACITY 16 will be used instead.
*
* Arguments: ssc - ssc contains ptr to soft state struct for the target
* capp - ptr to unsigned 64-bit variable to receive the
* capacity value from the command.
* lbap - ptr to unsigned 32-bit varaible to receive the
* block size value from the command
* path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
* the normal command waitq, or SD_PATH_DIRECT_PRIORITY
* to use the USCSI "direct" chain and bypass the normal
* command waitq. SD_PATH_DIRECT_PRIORITY is used when this
* command is issued as part of an error recovery action.
*
* Return Code: 0 - Success
* EIO - IO error
* EACCES - Reservation conflict detected
* EAGAIN - Device is becoming ready
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Blocks until command completes.
*/
#define SD_CAPACITY_SIZE sizeof (struct scsi_capacity)
static int
sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
int path_flag)
{
struct scsi_extended_sense sense_buf;
struct uscsi_cmd ucmd_buf;
union scsi_cdb cdb;
uint32_t *capacity_buf;
uint64_t capacity;
uint32_t lbasize;
uint32_t pbsize;
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(capp != NULL);
ASSERT(lbap != NULL);
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
/*
* First send a READ_CAPACITY command to the target.
* (This command is mandatory under SCSI-2.)
*
* Set up the CDB for the READ_CAPACITY command. The Partial
* Medium Indicator bit is cleared. The address field must be
* zero if the PMI bit is zero.
*/
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
cdb.scc_cmd = SCMD_READ_CAPACITY;
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = CDB_GROUP1;
ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf;
ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE;
ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
ucmd_buf.uscsi_timeout = 60;
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, path_flag);
switch (status) {
case 0:
/* Return failure if we did not get valid capacity data. */
if (ucmd_buf.uscsi_resid != 0) {
sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
"sd_send_scsi_READ_CAPACITY received invalid "
"capacity data");
kmem_free(capacity_buf, SD_CAPACITY_SIZE);
return (EIO);
}
/*
* Read capacity and block size from the READ CAPACITY 10 data.
* This data may be adjusted later due to device specific
* issues.
*
* According to the SCSI spec, the READ CAPACITY 10
* command returns the following:
*
* bytes 0-3: Maximum logical block address available.
* (MSB in byte:0 & LSB in byte:3)
*
* bytes 4-7: Block length in bytes
* (MSB in byte:4 & LSB in byte:7)
*
*/
capacity = BE_32(capacity_buf[0]);
lbasize = BE_32(capacity_buf[1]);
/*
* Done with capacity_buf
*/
kmem_free(capacity_buf, SD_CAPACITY_SIZE);
/*
* if the reported capacity is set to all 0xf's, then
* this disk is too large and requires SBC-2 commands.
* Reissue the request using READ CAPACITY 16.
*/
if (capacity == 0xffffffff) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
&lbasize, &pbsize, path_flag);
if (status != 0) {
return (status);
} else {
goto rc16_done;
}
}
break; /* Success! */
case EIO:
switch (ucmd_buf.uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
case STATUS_CHECK:
/*
* Check condition; look for ASC/ASCQ of 0x04/0x01
* (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
*/
if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
(scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
(scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
kmem_free(capacity_buf, SD_CAPACITY_SIZE);
return (EAGAIN);
}
break;
default:
break;
}
/* FALLTHRU */
default:
kmem_free(capacity_buf, SD_CAPACITY_SIZE);
return (status);
}
/*
* Some ATAPI CD-ROM drives report inaccurate LBA size values
* (2352 and 0 are common) so for these devices always force the value
* to 2048 as required by the ATAPI specs.
*/
if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
lbasize = 2048;
}
/*
* Get the maximum LBA value from the READ CAPACITY data.
* Here we assume that the Partial Medium Indicator (PMI) bit
* was cleared when issuing the command. This means that the LBA
* returned from the device is the LBA of the last logical block
* on the logical unit. The actual logical block count will be
* this value plus one.
*/
capacity += 1;
/*
* Currently, for removable media, the capacity is saved in terms
* of un->un_sys_blocksize, so scale the capacity value to reflect this.
*/
if (un->un_f_has_removable_media)
capacity *= (lbasize / un->un_sys_blocksize);
rc16_done:
/*
* Copy the values from the READ CAPACITY command into the space
* provided by the caller.
*/
*capp = capacity;
*lbap = lbasize;
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
"capacity:0x%llx lbasize:0x%x\n", capacity, lbasize);
/*
* Both the lbasize and capacity from the device must be nonzero,
* otherwise we assume that the values are not valid and return
* failure to the caller. (4203735)
*/
if ((capacity == 0) || (lbasize == 0)) {
sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
"sd_send_scsi_READ_CAPACITY received invalid value "
"capacity %llu lbasize %d", capacity, lbasize);
return (EIO);
}
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
return (0);
}
/*
* Function: sd_send_scsi_READ_CAPACITY_16
*
* Description: This routine uses the scsi READ CAPACITY 16 command to
* determine the device capacity in number of blocks and the
* device native block size. If this function returns a failure,
* then the values in *capp and *lbap are undefined.
* This routine should be called by sd_send_scsi_READ_CAPACITY
* which will apply any device specific adjustments to capacity
* and lbasize. One exception is it is also called by
* sd_get_media_info_ext. In that function, there is no need to
* adjust the capacity and lbasize.
*
* Arguments: ssc - ssc contains ptr to soft state struct for the target
* capp - ptr to unsigned 64-bit variable to receive the
* capacity value from the command.
* lbap - ptr to unsigned 32-bit varaible to receive the
* block size value from the command
* psp - ptr to unsigned 32-bit variable to receive the
* physical block size value from the command
* path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
* the normal command waitq, or SD_PATH_DIRECT_PRIORITY
* to use the USCSI "direct" chain and bypass the normal
* command waitq. SD_PATH_DIRECT_PRIORITY is used when
* this command is issued as part of an error recovery
* action.
*
* Return Code: 0 - Success
* EIO - IO error
* EACCES - Reservation conflict detected
* EAGAIN - Device is becoming ready
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Blocks until command completes.
*/
#define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16)
static int
sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
uint32_t *psp, int path_flag)
{
struct scsi_extended_sense sense_buf;
struct uscsi_cmd ucmd_buf;
union scsi_cdb cdb;
uint64_t *capacity16_buf;
uint64_t capacity;
uint32_t lbasize;
uint32_t pbsize;
uint32_t lbpb_exp;
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(capp != NULL);
ASSERT(lbap != NULL);
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
/*
* First send a READ_CAPACITY_16 command to the target.
*
* Set up the CDB for the READ_CAPACITY_16 command. The Partial
* Medium Indicator bit is cleared. The address field must be
* zero if the PMI bit is zero.
*/
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = CDB_GROUP4;
ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf;
ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE;
ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
ucmd_buf.uscsi_timeout = 60;
/*
* Read Capacity (16) is a Service Action In command. One
* command byte (0x9E) is overloaded for multiple operations,
* with the second CDB byte specifying the desired operation
*/
cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
/*
* Fill in allocation length field
*/
FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, path_flag);
switch (status) {
case 0:
/* Return failure if we did not get valid capacity data. */
if (ucmd_buf.uscsi_resid > 20) {
sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
"sd_send_scsi_READ_CAPACITY_16 received invalid "
"capacity data");
kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
return (EIO);
}
/*
* Read capacity and block size from the READ CAPACITY 16 data.
* This data may be adjusted later due to device specific
* issues.
*
* According to the SCSI spec, the READ CAPACITY 16
* command returns the following:
*
* bytes 0-7: Maximum logical block address available.
* (MSB in byte:0 & LSB in byte:7)
*
* bytes 8-11: Block length in bytes
* (MSB in byte:8 & LSB in byte:11)
*
* byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
*
* byte 14:
* bit 7: Thin-Provisioning Enabled
* bit 6: Thin-Provisioning Read Zeros
*/
capacity = BE_64(capacity16_buf[0]);
lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
un->un_thin_flags = 0;
if (((uint8_t *)capacity16_buf)[14] & (1 << 7))
un->un_thin_flags |= SD_THIN_PROV_ENABLED;
if (((uint8_t *)capacity16_buf)[14] & (1 << 6))
un->un_thin_flags |= SD_THIN_PROV_READ_ZEROS;
pbsize = lbasize << lbpb_exp;
/*
* Done with capacity16_buf
*/
kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
/*
* if the reported capacity is set to all 0xf's, then
* this disk is too large. This could only happen with
* a device that supports LBAs larger than 64 bits which
* are not defined by any current T10 standards.
*/
if (capacity == 0xffffffffffffffff) {
sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
"disk is too large");
return (EIO);
}
break; /* Success! */
case EIO:
switch (ucmd_buf.uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
case STATUS_CHECK:
/*
* Check condition; look for ASC/ASCQ of 0x04/0x01
* (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
*/
if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
(scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
(scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
return (EAGAIN);
}
break;
default:
break;
}
/* FALLTHRU */
default:
kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
return (status);
}
/*
* Some ATAPI CD-ROM drives report inaccurate LBA size values
* (2352 and 0 are common) so for these devices always force the value
* to 2048 as required by the ATAPI specs.
*/
if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
lbasize = 2048;
}
/*
* Get the maximum LBA value from the READ CAPACITY 16 data.
* Here we assume that the Partial Medium Indicator (PMI) bit
* was cleared when issuing the command. This means that the LBA
* returned from the device is the LBA of the last logical block
* on the logical unit. The actual logical block count will be
* this value plus one.
*/
capacity += 1;
/*
* Currently, for removable media, the capacity is saved in terms
* of un->un_sys_blocksize, so scale the capacity value to reflect this.
*/
if (un->un_f_has_removable_media)
capacity *= (lbasize / un->un_sys_blocksize);
*capp = capacity;
*lbap = lbasize;
*psp = pbsize;
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
"capacity:0x%llx lbasize:0x%x, pbsize: 0x%x\n",
capacity, lbasize, pbsize);
if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) {
sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
"sd_send_scsi_READ_CAPACITY_16 received invalid value "
"capacity %llu lbasize %d pbsize %d", capacity, lbasize);
return (EIO);
}
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
return (0);
}
/*
* Function: sd_send_scsi_START_STOP_UNIT
*
* Description: Issue a scsi START STOP UNIT command to the target.
*
* Arguments: ssc - ssc contatins pointer to driver soft state (unit)
* structure for this target.
* pc_flag - SD_POWER_CONDITION
* SD_START_STOP
* flag - SD_TARGET_START
* SD_TARGET_STOP
* SD_TARGET_EJECT
* SD_TARGET_CLOSE
* path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
* the normal command waitq, or SD_PATH_DIRECT_PRIORITY
* to use the USCSI "direct" chain and bypass the normal
* command waitq. SD_PATH_DIRECT_PRIORITY is used when this
* command is issued as part of an error recovery action.
*
* Return Code: 0 - Success
* EIO - IO error
* EACCES - Reservation conflict detected
* ENXIO - Not Ready, medium not present
* errno return code from sd_ssc_send()
*
* Context: Can sleep.
*/
static int
sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
int path_flag)
{
struct scsi_extended_sense sense_buf;
union scsi_cdb cdb;
struct uscsi_cmd ucmd_buf;
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
if (un->un_f_check_start_stop &&
(pc_flag == SD_START_STOP) &&
((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
(un->un_f_start_stop_supported != TRUE)) {
return (0);
}
/*
* If we are performing an eject operation and
* we receive any command other than SD_TARGET_EJECT
* we should immediately return.
*/
if (flag != SD_TARGET_EJECT) {
mutex_enter(SD_MUTEX(un));
if (un->un_f_ejecting == TRUE) {
mutex_exit(SD_MUTEX(un));
return (EAGAIN);
}
mutex_exit(SD_MUTEX(un));
}
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
bzero(&sense_buf, sizeof (struct scsi_extended_sense));
cdb.scc_cmd = SCMD_START_STOP;
cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
(uchar_t)(flag << 4) : (uchar_t)flag;
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = CDB_GROUP0;
ucmd_buf.uscsi_bufaddr = NULL;
ucmd_buf.uscsi_buflen = 0;
ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
ucmd_buf.uscsi_timeout = 200;
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, path_flag);
switch (status) {
case 0:
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
break; /* Success! */
case EIO:
switch (ucmd_buf.uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
case STATUS_CHECK:
if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
switch (scsi_sense_key(
(uint8_t *)&sense_buf)) {
case KEY_ILLEGAL_REQUEST:
status = ENOTSUP;
break;
case KEY_NOT_READY:
if (scsi_sense_asc(
(uint8_t *)&sense_buf)
== 0x3A) {
status = ENXIO;
}
break;
default:
break;
}
}
break;
default:
break;
}
break;
default:
break;
}
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
return (status);
}
/*
* Function: sd_start_stop_unit_callback
*
* Description: timeout(9F) callback to begin recovery process for a
* device that has spun down.
*
* Arguments: arg - pointer to associated softstate struct.
*
* Context: Executes in a timeout(9F) thread context
*/
static void
sd_start_stop_unit_callback(void *arg)
{
struct sd_lun *un = arg;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
(void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
}
/*
* Function: sd_start_stop_unit_task
*
* Description: Recovery procedure when a drive is spun down.
*
* Arguments: arg - pointer to associated softstate struct.
*
* Context: Executes in a taskq() thread context
*/
static void
sd_start_stop_unit_task(void *arg)
{
struct sd_lun *un = arg;
sd_ssc_t *ssc;
int power_level;
int rval;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
/*
* Some unformatted drives report not ready error, no need to
* restart if format has been initiated.
*/
mutex_enter(SD_MUTEX(un));
if (un->un_f_format_in_progress == TRUE) {
mutex_exit(SD_MUTEX(un));
return;
}
mutex_exit(SD_MUTEX(un));
ssc = sd_ssc_init(un);
/*
* When a START STOP command is issued from here, it is part of a
* failure recovery operation and must be issued before any other
* commands, including any pending retries. Thus it must be sent
* using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
* succeeds or not, we will start I/O after the attempt.
* If power condition is supported and the current power level
* is capable of performing I/O, we should set the power condition
* to that level. Otherwise, set the power condition to ACTIVE.
*/
if (un->un_f_power_condition_supported) {
mutex_enter(SD_MUTEX(un));
ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level));
power_level = sd_pwr_pc.ran_perf[un->un_power_level]
> 0 ? un->un_power_level : SD_SPINDLE_ACTIVE;
mutex_exit(SD_MUTEX(un));
rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY);
} else {
rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
SD_TARGET_START, SD_PATH_DIRECT_PRIORITY);
}
if (rval != 0)
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
sd_ssc_fini(ssc);
/*
* The above call blocks until the START_STOP_UNIT command completes.
* Now that it has completed, we must re-try the original IO that
* received the NOT READY condition in the first place. There are
* three possible conditions here:
*
* (1) The original IO is on un_retry_bp.
* (2) The original IO is on the regular wait queue, and un_retry_bp
* is NULL.
* (3) The original IO is on the regular wait queue, and un_retry_bp
* points to some other, unrelated bp.
*
* For each case, we must call sd_start_cmds() with un_retry_bp
* as the argument. If un_retry_bp is NULL, this will initiate
* processing of the regular wait queue. If un_retry_bp is not NULL,
* then this will process the bp on un_retry_bp. That may or may not
* be the original IO, but that does not matter: the important thing
* is to keep the IO processing going at this point.
*
* Note: This is a very specific error recovery sequence associated
* with a drive that is not spun up. We attempt a START_STOP_UNIT and
* serialize the I/O with completion of the spin-up.
*/
mutex_enter(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
"sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
un, un->un_retry_bp);
un->un_startstop_timeid = NULL; /* Timeout is no longer pending */
sd_start_cmds(un, un->un_retry_bp);
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
}
/*
* Function: sd_send_scsi_INQUIRY
*
* Description: Issue the scsi INQUIRY command.
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
* bufaddr
* buflen
* evpd
* page_code
* page_length
*
* Return Code: 0 - Success
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Does not return until command is completed.
*/
static int
sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen,
uchar_t evpd, uchar_t page_code, size_t *residp)
{
union scsi_cdb cdb;
struct uscsi_cmd ucmd_buf;
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(bufaddr != NULL);
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
bzero(bufaddr, buflen);
cdb.scc_cmd = SCMD_INQUIRY;
cdb.cdb_opaque[1] = evpd;
cdb.cdb_opaque[2] = page_code;
FORMG0COUNT(&cdb, buflen);
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = CDB_GROUP0;
ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
ucmd_buf.uscsi_buflen = buflen;
ucmd_buf.uscsi_rqbuf = NULL;
ucmd_buf.uscsi_rqlen = 0;
ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT;
ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, SD_PATH_DIRECT);
/*
* Only handle status == 0, the upper-level caller
* will put different assessment based on the context.
*/
if (status == 0)
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
if ((status == 0) && (residp != NULL)) {
*residp = ucmd_buf.uscsi_resid;
}
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
return (status);
}
/*
* Function: sd_send_scsi_TEST_UNIT_READY
*
* Description: Issue the scsi TEST UNIT READY command.
* This routine can be told to set the flag USCSI_DIAGNOSE to
* prevent retrying failed commands. Use this when the intent
* is either to check for device readiness, to clear a Unit
* Attention, or to clear any outstanding sense data.
* However under specific conditions the expected behavior
* is for retries to bring a device ready, so use the flag
* with caution.
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
* flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present
* SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
* 0: dont check for media present, do retries on cmd.
*
* Return Code: 0 - Success
* EIO - IO error
* EACCES - Reservation conflict detected
* ENXIO - Not Ready, medium not present
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Does not return until command is completed.
*/
static int
sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag)
{
struct scsi_extended_sense sense_buf;
union scsi_cdb cdb;
struct uscsi_cmd ucmd_buf;
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
/*
* Some Seagate elite1 TQ devices get hung with disconnect/reconnect
* timeouts when they receive a TUR and the queue is not empty. Check
* the configuration flag set during attach (indicating the drive has
* this firmware bug) and un_ncmds_in_transport before issuing the
* TUR. If there are
* pending commands return success, this is a bit arbitrary but is ok
* for non-removables (i.e. the eliteI disks) and non-clustering
* configurations.
*/
if (un->un_f_cfg_tur_check == TRUE) {
mutex_enter(SD_MUTEX(un));
if (un->un_ncmds_in_transport != 0) {
mutex_exit(SD_MUTEX(un));
return (0);
}
mutex_exit(SD_MUTEX(un));
}
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
bzero(&sense_buf, sizeof (struct scsi_extended_sense));
cdb.scc_cmd = SCMD_TEST_UNIT_READY;
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = CDB_GROUP0;
ucmd_buf.uscsi_bufaddr = NULL;
ucmd_buf.uscsi_buflen = 0;
ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
/* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
if ((flag & SD_DONT_RETRY_TUR) != 0) {
ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
}
ucmd_buf.uscsi_timeout = 60;
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
SD_PATH_STANDARD));
switch (status) {
case 0:
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
break; /* Success! */
case EIO:
switch (ucmd_buf.uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
case STATUS_CHECK:
if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
break;
}
if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
(scsi_sense_key((uint8_t *)&sense_buf) ==
KEY_NOT_READY) &&
(scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
status = ENXIO;
}
break;
default:
break;
}
break;
default:
break;
}
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
return (status);
}
/*
* Function: sd_send_scsi_PERSISTENT_RESERVE_IN
*
* Description: Issue the scsi PERSISTENT RESERVE IN command.
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
*
* Return Code: 0 - Success
* EACCES
* ENOTSUP
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Does not return until command is completed.
*/
static int
sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t usr_cmd,
uint16_t data_len, uchar_t *data_bufp)
{
struct scsi_extended_sense sense_buf;
union scsi_cdb cdb;
struct uscsi_cmd ucmd_buf;
int status;
int no_caller_buf = FALSE;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
bzero(&sense_buf, sizeof (struct scsi_extended_sense));
if (data_bufp == NULL) {
/* Allocate a default buf if the caller did not give one */
ASSERT(data_len == 0);
data_len = MHIOC_RESV_KEY_SIZE;
data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
no_caller_buf = TRUE;
}
cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
cdb.cdb_opaque[1] = usr_cmd;
FORMG1COUNT(&cdb, data_len);
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = CDB_GROUP1;
ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp;
ucmd_buf.uscsi_buflen = data_len;
ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
ucmd_buf.uscsi_timeout = 60;
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, SD_PATH_STANDARD);
switch (status) {
case 0:
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
break; /* Success! */
case EIO:
switch (ucmd_buf.uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
case STATUS_CHECK:
if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
(scsi_sense_key((uint8_t *)&sense_buf) ==
KEY_ILLEGAL_REQUEST)) {
status = ENOTSUP;
}
break;
default:
break;
}
break;
default:
break;
}
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
if (no_caller_buf == TRUE) {
kmem_free(data_bufp, data_len);
}
return (status);
}
/*
* Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
*
* Description: This routine is the driver entry point for handling CD-ROM
* multi-host persistent reservation requests (MHIOCGRP_INKEYS,
* MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
* device.
*
* Arguments: ssc - ssc contains un - pointer to soft state struct
* for the target.
* usr_cmd SCSI-3 reservation facility command (one of
* SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
* SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR)
* usr_bufp - user provided pointer register, reserve descriptor or
* preempt and abort structure (mhioc_register_t,
* mhioc_resv_desc_t, mhioc_preemptandabort_t)
*
* Return Code: 0 - Success
* EACCES
* ENOTSUP
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Does not return until command is completed.
*/
static int
sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd,
uchar_t *usr_bufp)
{
struct scsi_extended_sense sense_buf;
union scsi_cdb cdb;
struct uscsi_cmd ucmd_buf;
int status;
uchar_t data_len = sizeof (sd_prout_t);
sd_prout_t *prp;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(data_len == 24); /* required by scsi spec */
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
if (usr_bufp == NULL) {
return (EINVAL);
}
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
bzero(&sense_buf, sizeof (struct scsi_extended_sense));
prp = kmem_zalloc(data_len, KM_SLEEP);
cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
cdb.cdb_opaque[1] = usr_cmd;
FORMG1COUNT(&cdb, data_len);
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = CDB_GROUP1;
ucmd_buf.uscsi_bufaddr = (caddr_t)prp;
ucmd_buf.uscsi_buflen = data_len;
ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
ucmd_buf.uscsi_timeout = 60;
switch (usr_cmd) {
case SD_SCSI3_REGISTER: {
mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
bcopy(ptr->newkey.key, prp->service_key,
MHIOC_RESV_KEY_SIZE);
prp->aptpl = ptr->aptpl;
break;
}
case SD_SCSI3_CLEAR: {
mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
break;
}
case SD_SCSI3_RESERVE:
case SD_SCSI3_RELEASE: {
mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
prp->scope_address = BE_32(ptr->scope_specific_addr);
cdb.cdb_opaque[2] = ptr->type;
break;
}
case SD_SCSI3_PREEMPTANDABORT: {
mhioc_preemptandabort_t *ptr =
(mhioc_preemptandabort_t *)usr_bufp;
bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
bcopy(ptr->victim_key.key, prp->service_key,
MHIOC_RESV_KEY_SIZE);
prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
cdb.cdb_opaque[2] = ptr->resvdesc.type;
ucmd_buf.uscsi_flags |= USCSI_HEAD;
break;
}
case SD_SCSI3_REGISTERANDIGNOREKEY:
{
mhioc_registerandignorekey_t *ptr;
ptr = (mhioc_registerandignorekey_t *)usr_bufp;
bcopy(ptr->newkey.key,
prp->service_key, MHIOC_RESV_KEY_SIZE);
prp->aptpl = ptr->aptpl;
break;
}
default:
ASSERT(FALSE);
break;
}
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, SD_PATH_STANDARD);
switch (status) {
case 0:
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
break; /* Success! */
case EIO:
switch (ucmd_buf.uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
case STATUS_CHECK:
if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
(scsi_sense_key((uint8_t *)&sense_buf) ==
KEY_ILLEGAL_REQUEST)) {
status = ENOTSUP;
}
break;
default:
break;
}
break;
default:
break;
}
kmem_free(prp, data_len);
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
return (status);
}
/*
* Function: sd_send_scsi_SYNCHRONIZE_CACHE
*
* Description: Issues a scsi SYNCHRONIZE CACHE command to the target
*
* Arguments: un - pointer to the target's soft state struct
* dkc - pointer to the callback structure
*
* Return Code: 0 - success
* errno-type error code
*
* Context: kernel thread context only.
*
* _______________________________________________________________
* | dkc_flag & | dkc_callback | DKIOCFLUSHWRITECACHE |
* |FLUSH_VOLATILE| | operation |
* |______________|______________|_________________________________|
* | 0 | NULL | Synchronous flush on both |
* | | | volatile and non-volatile cache |
* |______________|______________|_________________________________|
* | 1 | NULL | Synchronous flush on volatile |
* | | | cache; disk drivers may suppress|
* | | | flush if disk table indicates |
* | | | non-volatile cache |
* |______________|______________|_________________________________|
* | 0 | !NULL | Asynchronous flush on both |
* | | | volatile and non-volatile cache;|
* |______________|______________|_________________________________|
* | 1 | !NULL | Asynchronous flush on volatile |
* | | | cache; disk drivers may suppress|
* | | | flush if disk table indicates |
* | | | non-volatile cache |
* |______________|______________|_________________________________|
*
*/
static int
sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
{
struct sd_uscsi_info *uip;
struct uscsi_cmd *uscmd;
union scsi_cdb *cdb;
struct buf *bp;
int rval = 0;
int is_async;
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
if (dkc == NULL || dkc->dkc_callback == NULL) {
is_async = FALSE;
} else {
is_async = TRUE;
}
mutex_enter(SD_MUTEX(un));
/* check whether cache flush should be suppressed */
if (un->un_f_suppress_cache_flush == TRUE) {
mutex_exit(SD_MUTEX(un));
/*
* suppress the cache flush if the device is told to do
* so by sd.conf or disk table
*/
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \
skip the cache flush since suppress_cache_flush is %d!\n",
un->un_f_suppress_cache_flush);
if (is_async == TRUE) {
/* invoke callback for asynchronous flush */
(*dkc->dkc_callback)(dkc->dkc_cookie, 0);
}
return (rval);
}
mutex_exit(SD_MUTEX(un));
/*
* check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be
* set properly
*/
cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
mutex_enter(SD_MUTEX(un));
if (dkc != NULL && un->un_f_sync_nv_supported &&
(dkc->dkc_flag & FLUSH_VOLATILE)) {
/*
* if the device supports SYNC_NV bit, turn on
* the SYNC_NV bit to only flush volatile cache
*/
cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
}
mutex_exit(SD_MUTEX(un));
/*
* First get some memory for the uscsi_cmd struct and cdb
* and initialize for SYNCHRONIZE_CACHE cmd.
*/
uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
uscmd->uscsi_cdblen = CDB_GROUP1;
uscmd->uscsi_cdb = (caddr_t)cdb;
uscmd->uscsi_bufaddr = NULL;
uscmd->uscsi_buflen = 0;
uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
uscmd->uscsi_rqlen = SENSE_LENGTH;
uscmd->uscsi_rqresid = SENSE_LENGTH;
uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
uscmd->uscsi_timeout = sd_io_time;
/*
* Allocate an sd_uscsi_info struct and fill it with the info
* needed by sd_initpkt_for_uscsi(). Then put the pointer into
* b_private in the buf for sd_initpkt_for_uscsi(). Note that
* since we allocate the buf here in this function, we do not
* need to preserve the prior contents of b_private.
* The sd_uscsi_info struct is also used by sd_uscsi_strategy()
*/
uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
uip->ui_flags = SD_PATH_DIRECT;
uip->ui_cmdp = uscmd;
bp = getrbuf(KM_SLEEP);
bp->b_private = uip;
/*
* Setup buffer to carry uscsi request.
*/
bp->b_flags = B_BUSY;
bp->b_bcount = 0;
bp->b_blkno = 0;
if (is_async == TRUE) {
bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
uip->ui_dkc = *dkc;
}
bp->b_edev = SD_GET_DEV(un);
bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */
/*
* Unset un_f_sync_cache_required flag
*/
mutex_enter(SD_MUTEX(un));
un->un_f_sync_cache_required = FALSE;
mutex_exit(SD_MUTEX(un));
(void) sd_uscsi_strategy(bp);
/*
* If synchronous request, wait for completion
* If async just return and let b_iodone callback
* cleanup.
* NOTE: On return, u_ncmds_in_driver will be decremented,
* but it was also incremented in sd_uscsi_strategy(), so
* we should be ok.
*/
if (is_async == FALSE) {
(void) biowait(bp);
rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
}
return (rval);
}
static int
sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
{
struct sd_uscsi_info *uip;
struct uscsi_cmd *uscmd;
uint8_t *sense_buf;
struct sd_lun *un;
int status;
union scsi_cdb *cdb;
uip = (struct sd_uscsi_info *)(bp->b_private);
ASSERT(uip != NULL);
uscmd = uip->ui_cmdp;
ASSERT(uscmd != NULL);
sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
ASSERT(sense_buf != NULL);
un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
ASSERT(un != NULL);
cdb = (union scsi_cdb *)uscmd->uscsi_cdb;
status = geterror(bp);
switch (status) {
case 0:
break; /* Success! */
case EIO:
switch (uscmd->uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
/* Ignore reservation conflict */
status = 0;
goto done;
case STATUS_CHECK:
if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
(scsi_sense_key(sense_buf) ==
KEY_ILLEGAL_REQUEST)) {
/* Ignore Illegal Request error */
if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) {
mutex_enter(SD_MUTEX(un));
un->un_f_sync_nv_supported = FALSE;
mutex_exit(SD_MUTEX(un));
status = 0;
SD_TRACE(SD_LOG_IO, un,
"un_f_sync_nv_supported \
is set to false.\n");
goto done;
}
mutex_enter(SD_MUTEX(un));
un->un_f_sync_cache_supported = FALSE;
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \
un_f_sync_cache_supported set to false \
with asc = %x, ascq = %x\n",
scsi_sense_asc(sense_buf),
scsi_sense_ascq(sense_buf));
status = ENOTSUP;
goto done;
}
break;
default:
break;
}
/* FALLTHRU */
default:
/*
* Turn on the un_f_sync_cache_required flag
* since the SYNC CACHE command failed
*/
mutex_enter(SD_MUTEX(un));
un->un_f_sync_cache_required = TRUE;
mutex_exit(SD_MUTEX(un));
/*
* Don't log an error message if this device
* has removable media.
*/
if (!un->un_f_has_removable_media) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"SYNCHRONIZE CACHE command failed (%d)\n", status);
}
break;
}
done:
if (uip->ui_dkc.dkc_callback != NULL) {
(*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
}
ASSERT((bp->b_flags & B_REMAPPED) == 0);
freerbuf(bp);
kmem_free(uip, sizeof (struct sd_uscsi_info));
kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
kmem_free(uscmd, sizeof (struct uscsi_cmd));
return (status);
}
/*
* Issues a single SCSI UNMAP command with a prepared UNMAP parameter list.
* Returns zero on success, or the non-zero command error code on failure.
*/
static int
sd_send_scsi_UNMAP_issue_one(sd_ssc_t *ssc, unmap_param_hdr_t *uph,
uint64_t num_descr, uint64_t bytes)
{
struct sd_lun *un = ssc->ssc_un;
struct scsi_extended_sense sense_buf;
union scsi_cdb cdb;
struct uscsi_cmd ucmd_buf;
int status;
const uint64_t param_size = sizeof (unmap_param_hdr_t) +
num_descr * sizeof (unmap_blk_descr_t);
ASSERT3U(param_size - 2, <=, UINT16_MAX);
uph->uph_data_len = BE_16(param_size - 2);
uph->uph_descr_data_len = BE_16(param_size - 8);
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
bzero(&sense_buf, sizeof (struct scsi_extended_sense));
cdb.scc_cmd = SCMD_UNMAP;
FORMG1COUNT(&cdb, param_size);
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = (uchar_t)CDB_GROUP1;
ucmd_buf.uscsi_bufaddr = (caddr_t)uph;
ucmd_buf.uscsi_buflen = param_size;
ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
ucmd_buf.uscsi_flags = USCSI_WRITE | USCSI_RQENABLE | USCSI_SILENT;
ucmd_buf.uscsi_timeout = un->un_cmd_timeout;
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
switch (status) {
case 0:
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
if (un->un_unmapstats) {
atomic_inc_64(&un->un_unmapstats->us_cmds.value.ui64);
atomic_add_64(&un->un_unmapstats->us_extents.value.ui64,
num_descr);
atomic_add_64(&un->un_unmapstats->us_bytes.value.ui64,
bytes);
}
break; /* Success! */
case EIO:
if (un->un_unmapstats)
atomic_inc_64(&un->un_unmapstats->us_errs.value.ui64);
switch (ucmd_buf.uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
default:
break;
}
break;
default:
if (un->un_unmapstats)
atomic_inc_64(&un->un_unmapstats->us_errs.value.ui64);
break;
}
return (status);
}
/*
* Returns a pointer to the i'th block descriptor inside an UNMAP param list.
*/
static inline unmap_blk_descr_t *
UNMAP_blk_descr_i(void *buf, size_t i)
{
return ((unmap_blk_descr_t *)((uintptr_t)buf +
sizeof (unmap_param_hdr_t) + (i * sizeof (unmap_blk_descr_t))));
}
/*
* Takes the list of extents from sd_send_scsi_UNMAP, chops it up, prepares
* UNMAP block descriptors and issues individual SCSI UNMAP commands. While
* doing so we consult the block limits to determine at most how many
* extents and LBAs we can UNMAP in one command.
* If a command fails for whatever, reason, extent list processing is aborted
* and the failed command's status is returned. Otherwise returns 0 on
* success.
*/
static int
sd_send_scsi_UNMAP_issue(dev_t dev, sd_ssc_t *ssc, const dkioc_free_list_t *dfl)
{
struct sd_lun *un = ssc->ssc_un;
unmap_param_hdr_t *uph;
sd_blk_limits_t *lim = &un->un_blk_lim;
int rval = 0;
int partition;
/* partition offset & length in system blocks */
diskaddr_t part_off_sysblks = 0, part_len_sysblks = 0;
uint64_t part_off, part_len;
uint64_t descr_cnt_lim, byte_cnt_lim;
uint64_t descr_issued = 0, bytes_issued = 0;
uph = kmem_zalloc(SD_UNMAP_PARAM_LIST_MAXSZ, KM_SLEEP);
partition = SDPART(dev);
rval = cmlb_partinfo(un->un_cmlbhandle, partition, &part_len_sysblks,
&part_off_sysblks, NULL, NULL, (void *)SD_PATH_DIRECT);
if (rval != 0)
goto out;
part_off = SD_SYSBLOCKS2BYTES(part_off_sysblks);
part_len = SD_SYSBLOCKS2BYTES(part_len_sysblks);
ASSERT(un->un_blk_lim.lim_max_unmap_lba_cnt != 0);
ASSERT(un->un_blk_lim.lim_max_unmap_descr_cnt != 0);
/* Spec says 0xffffffff are special values, so compute maximums. */
byte_cnt_lim = lim->lim_max_unmap_lba_cnt < UINT32_MAX ?
(uint64_t)lim->lim_max_unmap_lba_cnt * un->un_tgt_blocksize :
UINT64_MAX;
descr_cnt_lim = MIN(lim->lim_max_unmap_descr_cnt, SD_UNMAP_MAX_DESCR);
if (dfl->dfl_offset >= part_len) {
rval = SET_ERROR(EINVAL);
goto out;
}
for (size_t i = 0; i < dfl->dfl_num_exts; i++) {
const dkioc_free_list_ext_t *ext = &dfl->dfl_exts[i];
uint64_t ext_start = ext->dfle_start;
uint64_t ext_length = ext->dfle_length;
while (ext_length > 0) {
unmap_blk_descr_t *ubd;
/* Respect device limit on LBA count per command */
uint64_t len = MIN(MIN(ext_length, byte_cnt_lim -
bytes_issued), SD_TGTBLOCKS2BYTES(un, UINT32_MAX));
/* check partition limits */
if (ext_start >= part_len ||
ext_start + len < ext_start ||
dfl->dfl_offset + ext_start + len <
dfl->dfl_offset ||
dfl->dfl_offset + ext_start + len > part_len) {
rval = SET_ERROR(EINVAL);
goto out;
}
ASSERT3U(descr_issued, <, descr_cnt_lim);
ASSERT3U(bytes_issued, <, byte_cnt_lim);
ubd = UNMAP_blk_descr_i(uph, descr_issued);
/* adjust in-partition addresses to be device-global */
ubd->ubd_lba = BE_64(SD_BYTES2TGTBLOCKS(un,
dfl->dfl_offset + ext_start + part_off));
ubd->ubd_lba_cnt = BE_32(SD_BYTES2TGTBLOCKS(un, len));
descr_issued++;
bytes_issued += len;
/* Issue command when device limits reached */
if (descr_issued == descr_cnt_lim ||
bytes_issued == byte_cnt_lim) {
rval = sd_send_scsi_UNMAP_issue_one(ssc, uph,
descr_issued, bytes_issued);
if (rval != 0)
goto out;
descr_issued = 0;
bytes_issued = 0;
}
ext_start += len;
ext_length -= len;
}
}
if (descr_issued > 0) {
/* issue last command */
rval = sd_send_scsi_UNMAP_issue_one(ssc, uph, descr_issued,
bytes_issued);
}
out:
kmem_free(uph, SD_UNMAP_PARAM_LIST_MAXSZ);
return (rval);
}
/*
* Issues one or several UNMAP commands based on a list of extents to be
* unmapped. The internal multi-command processing is hidden, as the exact
* number of commands and extents per command is limited by both SCSI
* command syntax and device limits (as expressed in the SCSI Block Limits
* VPD page and un_blk_lim in struct sd_lun).
* Returns zero on success, or the error code of the first failed SCSI UNMAP
* command.
*/
static int
sd_send_scsi_UNMAP(dev_t dev, sd_ssc_t *ssc, dkioc_free_list_t *dfl, int flag)
{
struct sd_lun *un = ssc->ssc_un;
int rval = 0;
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(dfl != NULL);
/* Per spec, any of these conditions signals lack of UNMAP support. */
if (!(un->un_thin_flags & SD_THIN_PROV_ENABLED) ||
un->un_blk_lim.lim_max_unmap_descr_cnt == 0 ||
un->un_blk_lim.lim_max_unmap_lba_cnt == 0) {
return (SET_ERROR(ENOTSUP));
}
/* For userspace calls we must copy in. */
if (!(flag & FKIOCTL)) {
int err = dfl_copyin(dfl, &dfl, flag, KM_SLEEP);
if (err != 0)
return (err);
} else if (dfl->dfl_num_exts > DFL_COPYIN_MAX_EXTS) {
ASSERT3U(dfl->dfl_num_exts, <=, DFL_COPYIN_MAX_EXTS);
return (SET_ERROR(EINVAL));
}
rval = sd_send_scsi_UNMAP_issue(dev, ssc, dfl);
if (!(flag & FKIOCTL)) {
dfl_free(dfl);
dfl = NULL;
}
return (rval);
}
/*
* Function: sd_send_scsi_GET_CONFIGURATION
*
* Description: Issues the get configuration command to the device.
* Called from sd_check_for_writable_cd & sd_get_media_info
* caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
* Arguments: ssc
* ucmdbuf
* rqbuf
* rqbuflen
* bufaddr
* buflen
* path_flag
*
* Return Code: 0 - Success
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Does not return until command is completed.
*
*/
static int
sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
int path_flag)
{
char cdb[CDB_GROUP1];
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(bufaddr != NULL);
ASSERT(ucmdbuf != NULL);
ASSERT(rqbuf != NULL);
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
bzero(cdb, sizeof (cdb));
bzero(ucmdbuf, sizeof (struct uscsi_cmd));
bzero(rqbuf, rqbuflen);
bzero(bufaddr, buflen);
/*
* Set up cdb field for the get configuration command.
*/
cdb[0] = SCMD_GET_CONFIGURATION;
cdb[1] = 0x02; /* Requested Type */
cdb[8] = SD_PROFILE_HEADER_LEN;
ucmdbuf->uscsi_cdb = cdb;
ucmdbuf->uscsi_cdblen = CDB_GROUP1;
ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
ucmdbuf->uscsi_buflen = buflen;
ucmdbuf->uscsi_timeout = sd_io_time;
ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
ucmdbuf->uscsi_rqlen = rqbuflen;
ucmdbuf->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT | USCSI_READ;
status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
UIO_SYSSPACE, path_flag);
switch (status) {
case 0:
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
break; /* Success! */
case EIO:
switch (ucmdbuf->uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
default:
break;
}
break;
default:
break;
}
if (status == 0) {
SD_DUMP_MEMORY(un, SD_LOG_IO,
"sd_send_scsi_GET_CONFIGURATION: data",
(uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
}
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_GET_CONFIGURATION: exit\n");
return (status);
}
/*
* Function: sd_send_scsi_feature_GET_CONFIGURATION
*
* Description: Issues the get configuration command to the device to
* retrieve a specific feature. Called from
* sd_check_for_writable_cd & sd_set_mmc_caps.
* Arguments: ssc
* ucmdbuf
* rqbuf
* rqbuflen
* bufaddr
* buflen
* feature
*
* Return Code: 0 - Success
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Does not return until command is completed.
*
*/
static int
sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
char feature, int path_flag)
{
char cdb[CDB_GROUP1];
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(bufaddr != NULL);
ASSERT(ucmdbuf != NULL);
ASSERT(rqbuf != NULL);
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
bzero(cdb, sizeof (cdb));
bzero(ucmdbuf, sizeof (struct uscsi_cmd));
bzero(rqbuf, rqbuflen);
bzero(bufaddr, buflen);
/*
* Set up cdb field for the get configuration command.
*/
cdb[0] = SCMD_GET_CONFIGURATION;
cdb[1] = 0x02; /* Requested Type */
cdb[3] = feature;
cdb[8] = buflen;
ucmdbuf->uscsi_cdb = cdb;
ucmdbuf->uscsi_cdblen = CDB_GROUP1;
ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
ucmdbuf->uscsi_buflen = buflen;
ucmdbuf->uscsi_timeout = sd_io_time;
ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
ucmdbuf->uscsi_rqlen = rqbuflen;
ucmdbuf->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT | USCSI_READ;
status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
UIO_SYSSPACE, path_flag);
switch (status) {
case 0:
break; /* Success! */
case EIO:
switch (ucmdbuf->uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
default:
break;
}
break;
default:
break;
}
if (status == 0) {
SD_DUMP_MEMORY(un, SD_LOG_IO,
"sd_send_scsi_feature_GET_CONFIGURATION: data",
(uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
}
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
return (status);
}
/*
* Function: sd_send_scsi_MODE_SENSE
*
* Description: Utility function for issuing a scsi MODE SENSE command.
* Note: This routine uses a consistent implementation for Group0,
* Group1, and Group2 commands across all platforms. ATAPI devices
* use Group 1 Read/Write commands and Group 2 Mode Sense/Select
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
* cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
* CDB_GROUP[1|2] (10 byte).
* bufaddr - buffer for page data retrieved from the target.
* buflen - size of page to be retrieved.
* page_code - page code of data to be retrieved from the target.
* path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
* the normal command waitq, or SD_PATH_DIRECT_PRIORITY
* to use the USCSI "direct" chain and bypass the normal
* command waitq.
*
* Return Code: 0 - Success
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Does not return until command is completed.
*/
static int
sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
size_t buflen, uchar_t page_code, int path_flag)
{
struct scsi_extended_sense sense_buf;
union scsi_cdb cdb;
struct uscsi_cmd ucmd_buf;
int status;
int headlen;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(bufaddr != NULL);
ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
(cdbsize == CDB_GROUP2));
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
bzero(&sense_buf, sizeof (struct scsi_extended_sense));
bzero(bufaddr, buflen);
if (cdbsize == CDB_GROUP0) {
cdb.scc_cmd = SCMD_MODE_SENSE;
cdb.cdb_opaque[2] = page_code;
FORMG0COUNT(&cdb, buflen);
headlen = MODE_HEADER_LENGTH;
} else {
cdb.scc_cmd = SCMD_MODE_SENSE_G1;
cdb.cdb_opaque[2] = page_code;
FORMG1COUNT(&cdb, buflen);
headlen = MODE_HEADER_LENGTH_GRP2;
}
ASSERT(headlen <= buflen);
SD_FILL_SCSI1_LUN_CDB(un, &cdb);
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
ucmd_buf.uscsi_buflen = buflen;
ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
ucmd_buf.uscsi_timeout = 60;
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, path_flag);
switch (status) {
case 0:
/*
* sr_check_wp() uses 0x3f page code and check the header of
* mode page to determine if target device is write-protected.
* But some USB devices return 0 bytes for 0x3f page code. For
* this case, make sure that mode page header is returned at
* least.
*/
if (buflen - ucmd_buf.uscsi_resid < headlen) {
status = EIO;
sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
"mode page header is not returned");
}
break; /* Success! */
case EIO:
switch (ucmd_buf.uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
default:
break;
}
break;
default:
break;
}
if (status == 0) {
SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
(uchar_t *)bufaddr, buflen, SD_LOG_HEX);
}
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
return (status);
}
/*
* Function: sd_send_scsi_MODE_SELECT
*
* Description: Utility function for issuing a scsi MODE SELECT command.
* Note: This routine uses a consistent implementation for Group0,
* Group1, and Group2 commands across all platforms. ATAPI devices
* use Group 1 Read/Write commands and Group 2 Mode Sense/Select
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
* cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
* CDB_GROUP[1|2] (10 byte).
* bufaddr - buffer for page data retrieved from the target.
* buflen - size of page to be retrieved.
* save_page - boolean to determin if SP bit should be set.
* path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
* the normal command waitq, or SD_PATH_DIRECT_PRIORITY
* to use the USCSI "direct" chain and bypass the normal
* command waitq.
*
* Return Code: 0 - Success
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Does not return until command is completed.
*/
static int
sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
size_t buflen, uchar_t save_page, int path_flag)
{
struct scsi_extended_sense sense_buf;
union scsi_cdb cdb;
struct uscsi_cmd ucmd_buf;
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(bufaddr != NULL);
ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
(cdbsize == CDB_GROUP2));
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
bzero(&sense_buf, sizeof (struct scsi_extended_sense));
/* Set the PF bit for many third party drives */
cdb.cdb_opaque[1] = 0x10;
/* Set the savepage(SP) bit if given */
if (save_page == SD_SAVE_PAGE) {
cdb.cdb_opaque[1] |= 0x01;
}
if (cdbsize == CDB_GROUP0) {
cdb.scc_cmd = SCMD_MODE_SELECT;
FORMG0COUNT(&cdb, buflen);
} else {
cdb.scc_cmd = SCMD_MODE_SELECT_G1;
FORMG1COUNT(&cdb, buflen);
}
SD_FILL_SCSI1_LUN_CDB(un, &cdb);
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
ucmd_buf.uscsi_buflen = buflen;
ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
ucmd_buf.uscsi_timeout = 60;
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, path_flag);
switch (status) {
case 0:
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
break; /* Success! */
case EIO:
switch (ucmd_buf.uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
default:
break;
}
break;
default:
break;
}
if (status == 0) {
SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
(uchar_t *)bufaddr, buflen, SD_LOG_HEX);
}
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
return (status);
}
/*
* Function: sd_send_scsi_RDWR
*
* Description: Issue a scsi READ or WRITE command with the given parameters.
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
* cmd: SCMD_READ or SCMD_WRITE
* bufaddr: Address of caller's buffer to receive the RDWR data
* buflen: Length of caller's buffer receive the RDWR data.
* start_block: Block number for the start of the RDWR operation.
* (Assumes target-native block size.)
* residp: Pointer to variable to receive the redisual of the
* RDWR operation (may be NULL of no residual requested).
* path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
* the normal command waitq, or SD_PATH_DIRECT_PRIORITY
* to use the USCSI "direct" chain and bypass the normal
* command waitq.
*
* Return Code: 0 - Success
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Does not return until command is completed.
*/
static int
sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
size_t buflen, daddr_t start_block, int path_flag)
{
struct scsi_extended_sense sense_buf;
union scsi_cdb cdb;
struct uscsi_cmd ucmd_buf;
uint32_t block_count;
int status;
int cdbsize;
uchar_t flag;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(bufaddr != NULL);
ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
if (un->un_f_tgt_blocksize_is_valid != TRUE) {
return (EINVAL);
}
mutex_enter(SD_MUTEX(un));
block_count = SD_BYTES2TGTBLOCKS(un, buflen);
mutex_exit(SD_MUTEX(un));
flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
"bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
bufaddr, buflen, start_block, block_count);
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
bzero(&sense_buf, sizeof (struct scsi_extended_sense));
/* Compute CDB size to use */
if (start_block > 0xffffffff)
cdbsize = CDB_GROUP4;
else if ((start_block & 0xFFE00000) ||
(un->un_f_cfg_is_atapi == TRUE))
cdbsize = CDB_GROUP1;
else
cdbsize = CDB_GROUP0;
switch (cdbsize) {
case CDB_GROUP0: /* 6-byte CDBs */
cdb.scc_cmd = cmd;
FORMG0ADDR(&cdb, start_block);
FORMG0COUNT(&cdb, block_count);
break;
case CDB_GROUP1: /* 10-byte CDBs */
cdb.scc_cmd = cmd | SCMD_GROUP1;
FORMG1ADDR(&cdb, start_block);
FORMG1COUNT(&cdb, block_count);
break;
case CDB_GROUP4: /* 16-byte CDBs */
cdb.scc_cmd = cmd | SCMD_GROUP4;
FORMG4LONGADDR(&cdb, (uint64_t)start_block);
FORMG4COUNT(&cdb, block_count);
break;
case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */
default:
/* All others reserved */
return (EINVAL);
}
/* Set LUN bit(s) in CDB if this is a SCSI-1 device */
SD_FILL_SCSI1_LUN_CDB(un, &cdb);
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
ucmd_buf.uscsi_bufaddr = bufaddr;
ucmd_buf.uscsi_buflen = buflen;
ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT;
ucmd_buf.uscsi_timeout = 60;
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, path_flag);
switch (status) {
case 0:
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
break; /* Success! */
case EIO:
switch (ucmd_buf.uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
default:
break;
}
break;
default:
break;
}
if (status == 0) {
SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
(uchar_t *)bufaddr, buflen, SD_LOG_HEX);
}
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
return (status);
}
/*
* Function: sd_send_scsi_LOG_SENSE
*
* Description: Issue a scsi LOG_SENSE command with the given parameters.
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
*
* Return Code: 0 - Success
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Does not return until command is completed.
*/
static int
sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen,
uchar_t page_code, uchar_t page_control, uint16_t param_ptr, int path_flag)
{
struct scsi_extended_sense sense_buf;
union scsi_cdb cdb;
struct uscsi_cmd ucmd_buf;
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
bzero(&sense_buf, sizeof (struct scsi_extended_sense));
cdb.scc_cmd = SCMD_LOG_SENSE_G1;
cdb.cdb_opaque[2] = (page_control << 6) | page_code;
cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF);
FORMG1COUNT(&cdb, buflen);
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = CDB_GROUP1;
ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
ucmd_buf.uscsi_buflen = buflen;
ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
ucmd_buf.uscsi_timeout = 60;
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, path_flag);
switch (status) {
case 0:
break;
case EIO:
switch (ucmd_buf.uscsi_status) {
case STATUS_RESERVATION_CONFLICT:
status = EACCES;
break;
case STATUS_CHECK:
if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
(scsi_sense_key((uint8_t *)&sense_buf) ==
KEY_ILLEGAL_REQUEST) &&
(scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
/*
* ASC 0x24: INVALID FIELD IN CDB
*/
switch (page_code) {
case START_STOP_CYCLE_PAGE:
/*
* The start stop cycle counter is
* implemented as page 0x31 in earlier
* generation disks. In new generation
* disks the start stop cycle counter is
* implemented as page 0xE. To properly
* handle this case if an attempt for
* log page 0xE is made and fails we
* will try again using page 0x31.
*
* Network storage BU committed to
* maintain the page 0x31 for this
* purpose and will not have any other
* page implemented with page code 0x31
* until all disks transition to the
* standard page.
*/
mutex_enter(SD_MUTEX(un));
un->un_start_stop_cycle_page =
START_STOP_CYCLE_VU_PAGE;
cdb.cdb_opaque[2] =
(char)(page_control << 6) |
un->un_start_stop_cycle_page;
mutex_exit(SD_MUTEX(un));
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
status = sd_ssc_send(
ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, path_flag);
break;
case TEMPERATURE_PAGE:
status = ENOTTY;
break;
default:
break;
}
}
break;
default:
break;
}
break;
default:
break;
}
if (status == 0) {
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
(uchar_t *)bufaddr, buflen, SD_LOG_HEX);
}
SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
return (status);
}
/*
* Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
*
* Description: Issue the scsi GET EVENT STATUS NOTIFICATION command.
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
* bufaddr
* buflen
* class_req
*
* Return Code: 0 - Success
* errno return code from sd_ssc_send()
*
* Context: Can sleep. Does not return until command is completed.
*/
static int
sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr,
size_t buflen, uchar_t class_req)
{
union scsi_cdb cdb;
struct uscsi_cmd ucmd_buf;
int status;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(bufaddr != NULL);
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
bzero(&cdb, sizeof (cdb));
bzero(&ucmd_buf, sizeof (ucmd_buf));
bzero(bufaddr, buflen);
cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
cdb.cdb_opaque[1] = 1; /* polled */
cdb.cdb_opaque[4] = class_req;
FORMG1COUNT(&cdb, buflen);
ucmd_buf.uscsi_cdb = (char *)&cdb;
ucmd_buf.uscsi_cdblen = CDB_GROUP1;
ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
ucmd_buf.uscsi_buflen = buflen;
ucmd_buf.uscsi_rqbuf = NULL;
ucmd_buf.uscsi_rqlen = 0;
ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT;
ucmd_buf.uscsi_timeout = 60;
status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
UIO_SYSSPACE, SD_PATH_DIRECT);
/*
* Only handle status == 0, the upper-level caller
* will put different assessment based on the context.
*/
if (status == 0) {
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
if (ucmd_buf.uscsi_resid != 0) {
status = EIO;
}
}
SD_TRACE(SD_LOG_IO, un,
"sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
return (status);
}
static boolean_t
sd_gesn_media_data_valid(uchar_t *data)
{
uint16_t len;
len = (data[1] << 8) | data[0];
return ((len >= 6) &&
((data[2] & SD_GESN_HEADER_NEA) == 0) &&
((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) &&
((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0));
}
/*
* Function: sdioctl
*
* Description: Driver's ioctl(9e) entry point function.
*
* Arguments: dev - device number
* cmd - ioctl operation to be performed
* arg - user argument, contains data to be set or reference
* parameter for get
* flag - bit flag, indicating open settings, 32/64 bit type
* cred_p - user credential pointer
* rval_p - calling process return value (OPT)
*
* Return Code: EINVAL
* ENOTTY
* ENXIO
* EIO
* EFAULT
* ENOTSUP
* EPERM
*
* Context: Called from the device switch at normal priority.
*/
static int
sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
{
struct sd_lun *un = NULL;
int err = 0;
int i = 0;
cred_t *cr;
int tmprval = EINVAL;
boolean_t is_valid;
sd_ssc_t *ssc;
/*
* All device accesses go thru sdstrategy where we check on suspend
* status
*/
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
ASSERT(!mutex_owned(SD_MUTEX(un)));
/* Initialize sd_ssc_t for internal uscsi commands */
ssc = sd_ssc_init(un);
is_valid = SD_IS_VALID_LABEL(un);
/*
* Moved this wait from sd_uscsi_strategy to here for
* reasons of deadlock prevention. Internal driver commands,
* specifically those to change a devices power level, result
* in a call to sd_uscsi_strategy.
*/
mutex_enter(SD_MUTEX(un));
while ((un->un_state == SD_STATE_SUSPENDED) ||
(un->un_state == SD_STATE_PM_CHANGING)) {
cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
}
/*
* Twiddling the counter here protects commands from now
* through to the top of sd_uscsi_strategy. Without the
* counter inc. a power down, for example, could get in
* after the above check for state is made and before
* execution gets to the top of sd_uscsi_strategy.
* That would cause problems.
*/
un->un_ncmds_in_driver++;
if (!is_valid &&
(flag & (FNDELAY | FNONBLOCK))) {
switch (cmd) {
case DKIOCGGEOM: /* SD_PATH_DIRECT */
case DKIOCGVTOC:
case DKIOCGEXTVTOC:
case DKIOCGAPART:
case DKIOCPARTINFO:
case DKIOCEXTPARTINFO:
case DKIOCSGEOM:
case DKIOCSAPART:
case DKIOCGETEFI:
case DKIOCPARTITION:
case DKIOCSVTOC:
case DKIOCSEXTVTOC:
case DKIOCSETEFI:
case DKIOCGMBOOT:
case DKIOCSMBOOT:
case DKIOCG_PHYGEOM:
case DKIOCG_VIRTGEOM:
#if defined(__x86)
case DKIOCSETEXTPART:
#endif
/* let cmlb handle it */
goto skip_ready_valid;
case CDROMPAUSE:
case CDROMRESUME:
case CDROMPLAYMSF:
case CDROMPLAYTRKIND:
case CDROMREADTOCHDR:
case CDROMREADTOCENTRY:
case CDROMSTOP:
case CDROMSTART:
case CDROMVOLCTRL:
case CDROMSUBCHNL:
case CDROMREADMODE2:
case CDROMREADMODE1:
case CDROMREADOFFSET:
case CDROMSBLKMODE:
case CDROMGBLKMODE:
case CDROMGDRVSPEED:
case CDROMSDRVSPEED:
case CDROMCDDA:
case CDROMCDXA:
case CDROMSUBCODE:
if (!ISCD(un)) {
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
mutex_exit(SD_MUTEX(un));
err = ENOTTY;
goto done_without_assess;
}
break;
case FDEJECT:
case DKIOCEJECT:
case CDROMEJECT:
if (!un->un_f_eject_media_supported) {
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
mutex_exit(SD_MUTEX(un));
err = ENOTTY;
goto done_without_assess;
}
break;
case DKIOCFLUSHWRITECACHE:
mutex_exit(SD_MUTEX(un));
err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
if (err != 0) {
mutex_enter(SD_MUTEX(un));
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
mutex_exit(SD_MUTEX(un));
err = EIO;
goto done_quick_assess;
}
mutex_enter(SD_MUTEX(un));
/* FALLTHROUGH */
case DKIOCREMOVABLE:
case DKIOCHOTPLUGGABLE:
case DKIOCINFO:
case DKIOCGMEDIAINFO:
case DKIOCGMEDIAINFOEXT:
case DKIOCSOLIDSTATE:
case DKIOC_CANFREE:
case MHIOCENFAILFAST:
case MHIOCSTATUS:
case MHIOCTKOWN:
case MHIOCRELEASE:
case MHIOCGRP_INKEYS:
case MHIOCGRP_INRESV:
case MHIOCGRP_REGISTER:
case MHIOCGRP_CLEAR:
case MHIOCGRP_RESERVE:
case MHIOCGRP_PREEMPTANDABORT:
case MHIOCGRP_REGISTERANDIGNOREKEY:
case CDROMCLOSETRAY:
case USCSICMD:
case USCSIMAXXFER:
goto skip_ready_valid;
default:
break;
}
mutex_exit(SD_MUTEX(un));
err = sd_ready_and_valid(ssc, SDPART(dev));
mutex_enter(SD_MUTEX(un));
if (err != SD_READY_VALID) {
switch (cmd) {
case DKIOCSTATE:
case CDROMGDRVSPEED:
case CDROMSDRVSPEED:
case FDEJECT: /* for eject command */
case DKIOCEJECT:
case CDROMEJECT:
case DKIOCREMOVABLE:
case DKIOCHOTPLUGGABLE:
break;
default:
if (un->un_f_has_removable_media) {
err = ENXIO;
} else {
/* Do not map SD_RESERVED_BY_OTHERS to EIO */
if (err == SD_RESERVED_BY_OTHERS) {
err = EACCES;
} else {
err = EIO;
}
}
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
mutex_exit(SD_MUTEX(un));
goto done_without_assess;
}
}
}
skip_ready_valid:
mutex_exit(SD_MUTEX(un));
switch (cmd) {
case DKIOCINFO:
SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
break;
case DKIOCGMEDIAINFO:
SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
err = sd_get_media_info(dev, (caddr_t)arg, flag);
break;
case DKIOCGMEDIAINFOEXT:
SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n");
err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
break;
case DKIOCGGEOM:
case DKIOCGVTOC:
case DKIOCGEXTVTOC:
case DKIOCGAPART:
case DKIOCPARTINFO:
case DKIOCEXTPARTINFO:
case DKIOCSGEOM:
case DKIOCSAPART:
case DKIOCGETEFI:
case DKIOCPARTITION:
case DKIOCSVTOC:
case DKIOCSEXTVTOC:
case DKIOCSETEFI:
case DKIOCGMBOOT:
case DKIOCSMBOOT:
case DKIOCG_PHYGEOM:
case DKIOCG_VIRTGEOM:
#if defined(__x86)
case DKIOCSETEXTPART:
#endif
SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
/* TUR should spin up */
if (un->un_f_has_removable_media)
err = sd_send_scsi_TEST_UNIT_READY(ssc,
SD_CHECK_FOR_MEDIA);
else
err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
if (err != 0)
goto done_with_assess;
err = cmlb_ioctl(un->un_cmlbhandle, dev,
cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
if ((err == 0) &&
((cmd == DKIOCSETEFI) ||
((un->un_f_pkstats_enabled) &&
(cmd == DKIOCSAPART || cmd == DKIOCSVTOC ||
cmd == DKIOCSEXTVTOC)))) {
tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT,
(void *)SD_PATH_DIRECT);
if ((tmprval == 0) && un->un_f_pkstats_enabled) {
sd_set_pstats(un);
SD_TRACE(SD_LOG_IO_PARTITION, un,
"sd_ioctl: un:0x%p pstats created and "
"set\n", un);
}
}
if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) ||
((cmd == DKIOCSETEFI) && (tmprval == 0))) {
mutex_enter(SD_MUTEX(un));
if (un->un_f_devid_supported &&
(un->un_f_opt_fab_devid == TRUE)) {
if (un->un_devid == NULL) {
sd_register_devid(ssc, SD_DEVINFO(un),
SD_TARGET_IS_UNRESERVED);
} else {
/*
* The device id for this disk
* has been fabricated. The
* device id must be preserved
* by writing it back out to
* disk.
*/
if (sd_write_deviceid(ssc) != 0) {
ddi_devid_free(un->un_devid);
un->un_devid = NULL;
}
}
}
mutex_exit(SD_MUTEX(un));
}
break;
case DKIOCLOCK:
SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
SD_PATH_STANDARD);
goto done_with_assess;
case DKIOCUNLOCK:
SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
SD_PATH_STANDARD);
goto done_with_assess;
case DKIOCSTATE: {
enum dkio_state state;
SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
err = EFAULT;
} else {
err = sd_check_media(dev, state);
if (err == 0) {
if (ddi_copyout(&un->un_mediastate, (void *)arg,
sizeof (int), flag) != 0)
err = EFAULT;
}
}
break;
}
case DKIOCREMOVABLE:
SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
i = un->un_f_has_removable_media ? 1 : 0;
if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
err = EFAULT;
} else {
err = 0;
}
break;
case DKIOCSOLIDSTATE:
SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSOLIDSTATE\n");
i = un->un_f_is_solid_state ? 1 : 0;
if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
err = EFAULT;
} else {
err = 0;
}
break;
case DKIOCHOTPLUGGABLE:
SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
i = un->un_f_is_hotpluggable ? 1 : 0;
if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
err = EFAULT;
} else {
err = 0;
}
break;
case DKIOCREADONLY:
SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n");
i = 0;
if ((ISCD(un) && !un->un_f_mmc_writable_media) ||
(sr_check_wp(dev) != 0)) {
i = 1;
}
if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
err = EFAULT;
} else {
err = 0;
}
break;
case DKIOCGTEMPERATURE:
SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
break;
case MHIOCENFAILFAST:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
if ((err = drv_priv(cred_p)) == 0) {
err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
}
break;
case MHIOCTKOWN:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
if ((err = drv_priv(cred_p)) == 0) {
err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
}
break;
case MHIOCRELEASE:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
if ((err = drv_priv(cred_p)) == 0) {
err = sd_mhdioc_release(dev);
}
break;
case MHIOCSTATUS:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
if ((err = drv_priv(cred_p)) == 0) {
switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) {
case 0:
err = 0;
break;
case EACCES:
*rval_p = 1;
err = 0;
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
break;
default:
err = EIO;
goto done_with_assess;
}
}
break;
case MHIOCQRESERVE:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
if ((err = drv_priv(cred_p)) == 0) {
err = sd_reserve_release(dev, SD_RESERVE);
}
break;
case MHIOCREREGISTERDEVID:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
if (drv_priv(cred_p) == EPERM) {
err = EPERM;
} else if (!un->un_f_devid_supported) {
err = ENOTTY;
} else {
err = sd_mhdioc_register_devid(dev);
}
break;
case MHIOCGRP_INKEYS:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
if (((err = drv_priv(cred_p)) != EPERM) &&
arg != (intptr_t)NULL) {
if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
err = ENOTSUP;
} else {
err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
flag);
}
}
break;
case MHIOCGRP_INRESV:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
if (((err = drv_priv(cred_p)) != EPERM) &&
arg != (intptr_t)NULL) {
if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
err = ENOTSUP;
} else {
err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
}
}
break;
case MHIOCGRP_REGISTER:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
if ((err = drv_priv(cred_p)) != EPERM) {
if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
err = ENOTSUP;
} else if (arg != (intptr_t)NULL) {
mhioc_register_t reg;
if (ddi_copyin((void *)arg, ®,
sizeof (mhioc_register_t), flag) != 0) {
err = EFAULT;
} else {
err =
sd_send_scsi_PERSISTENT_RESERVE_OUT(
ssc, SD_SCSI3_REGISTER,
(uchar_t *)®);
if (err != 0)
goto done_with_assess;
}
}
}
break;
case MHIOCGRP_CLEAR:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n");
if ((err = drv_priv(cred_p)) != EPERM) {
if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
err = ENOTSUP;
} else if (arg != (intptr_t)NULL) {
mhioc_register_t reg;
if (ddi_copyin((void *)arg, ®,
sizeof (mhioc_register_t), flag) != 0) {
err = EFAULT;
} else {
err =
sd_send_scsi_PERSISTENT_RESERVE_OUT(
ssc, SD_SCSI3_CLEAR,
(uchar_t *)®);
if (err != 0)
goto done_with_assess;
}
}
}
break;
case MHIOCGRP_RESERVE:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
if ((err = drv_priv(cred_p)) != EPERM) {
if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
err = ENOTSUP;
} else if (arg != (intptr_t)NULL) {
mhioc_resv_desc_t resv_desc;
if (ddi_copyin((void *)arg, &resv_desc,
sizeof (mhioc_resv_desc_t), flag) != 0) {
err = EFAULT;
} else {
err =
sd_send_scsi_PERSISTENT_RESERVE_OUT(
ssc, SD_SCSI3_RESERVE,
(uchar_t *)&resv_desc);
if (err != 0)
goto done_with_assess;
}
}
}
break;
case MHIOCGRP_PREEMPTANDABORT:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
if ((err = drv_priv(cred_p)) != EPERM) {
if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
err = ENOTSUP;
} else if (arg != (intptr_t)NULL) {
mhioc_preemptandabort_t preempt_abort;
if (ddi_copyin((void *)arg, &preempt_abort,
sizeof (mhioc_preemptandabort_t),
flag) != 0) {
err = EFAULT;
} else {
err =
sd_send_scsi_PERSISTENT_RESERVE_OUT(
ssc, SD_SCSI3_PREEMPTANDABORT,
(uchar_t *)&preempt_abort);
if (err != 0)
goto done_with_assess;
}
}
}
break;
case MHIOCGRP_REGISTERANDIGNOREKEY:
SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n");
if ((err = drv_priv(cred_p)) != EPERM) {
if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
err = ENOTSUP;
} else if (arg != (intptr_t)NULL) {
mhioc_registerandignorekey_t r_and_i;
if (ddi_copyin((void *)arg, (void *)&r_and_i,
sizeof (mhioc_registerandignorekey_t),
flag) != 0) {
err = EFAULT;
} else {
err =
sd_send_scsi_PERSISTENT_RESERVE_OUT(
ssc, SD_SCSI3_REGISTERANDIGNOREKEY,
(uchar_t *)&r_and_i);
if (err != 0)
goto done_with_assess;
}
}
}
break;
case USCSICMD:
SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
cr = ddi_get_cred();
if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
err = EPERM;
} else {
enum uio_seg uioseg;
uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
UIO_USERSPACE;
if (un->un_f_format_in_progress == TRUE) {
err = EAGAIN;
break;
}
err = sd_ssc_send(ssc,
(struct uscsi_cmd *)arg,
flag, uioseg, SD_PATH_STANDARD);
if (err != 0)
goto done_with_assess;
else
sd_ssc_assessment(ssc, SD_FMT_STANDARD);
}
break;
case USCSIMAXXFER:
SD_TRACE(SD_LOG_IOCTL, un, "USCSIMAXXFER\n");
cr = ddi_get_cred();
if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
err = EPERM;
} else {
const uscsi_xfer_t xfer = un->un_max_xfer_size;
if (ddi_copyout(&xfer, (void *)arg, sizeof (xfer),
flag) != 0) {
err = EFAULT;
} else {
err = 0;
}
}
break;
case CDROMPAUSE:
case CDROMRESUME:
SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sr_pause_resume(dev, cmd);
}
break;
case CDROMPLAYMSF:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sr_play_msf(dev, (caddr_t)arg, flag);
}
break;
case CDROMPLAYTRKIND:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
#if defined(__x86)
/*
* not supported on ATAPI CD drives, use CDROMPLAYMSF instead
*/
if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
#else
if (!ISCD(un)) {
#endif
err = ENOTTY;
} else {
err = sr_play_trkind(dev, (caddr_t)arg, flag);
}
break;
case CDROMREADTOCHDR:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sr_read_tochdr(dev, (caddr_t)arg, flag);
}
break;
case CDROMREADTOCENTRY:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sr_read_tocentry(dev, (caddr_t)arg, flag);
}
break;
case CDROMSTOP:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
SD_TARGET_STOP, SD_PATH_STANDARD);
goto done_with_assess;
}
break;
case CDROMSTART:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
SD_TARGET_START, SD_PATH_STANDARD);
goto done_with_assess;
}
break;
case CDROMCLOSETRAY:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
SD_TARGET_CLOSE, SD_PATH_STANDARD);
goto done_with_assess;
}
break;
case FDEJECT: /* for eject command */
case DKIOCEJECT:
case CDROMEJECT:
SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
if (!un->un_f_eject_media_supported) {
err = ENOTTY;
} else {
err = sr_eject(dev);
}
break;
case CDROMVOLCTRL:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
}
break;
case CDROMSUBCHNL:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sr_read_subchannel(dev, (caddr_t)arg, flag);
}
break;
case CDROMREADMODE2:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
if (!ISCD(un)) {
err = ENOTTY;
} else if (un->un_f_cfg_is_atapi == TRUE) {
/*
* If the drive supports READ CD, use that instead of
* switching the LBA size via a MODE SELECT
* Block Descriptor
*/
err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
} else {
err = sr_read_mode2(dev, (caddr_t)arg, flag);
}
break;
case CDROMREADMODE1:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sr_read_mode1(dev, (caddr_t)arg, flag);
}
break;
case CDROMREADOFFSET:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sr_read_sony_session_offset(dev, (caddr_t)arg,
flag);
}
break;
case CDROMSBLKMODE:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
/*
* There is no means of changing block size in case of atapi
* drives, thus return ENOTTY if drive type is atapi
*/
if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
err = ENOTTY;
} else if (un->un_f_mmc_cap == TRUE) {
/*
* MMC Devices do not support changing the
* logical block size
*
* Note: EINVAL is being returned instead of ENOTTY to
* maintain consistancy with the original mmc
* driver update.
*/
err = EINVAL;
} else {
mutex_enter(SD_MUTEX(un));
if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
(un->un_ncmds_in_transport > 0)) {
mutex_exit(SD_MUTEX(un));
err = EINVAL;
} else {
mutex_exit(SD_MUTEX(un));
err = sr_change_blkmode(dev, cmd, arg, flag);
}
}
break;
case CDROMGBLKMODE:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
if (!ISCD(un)) {
err = ENOTTY;
} else if ((un->un_f_cfg_is_atapi != FALSE) &&
(un->un_f_blockcount_is_valid != FALSE)) {
/*
* Drive is an ATAPI drive so return target block
* size for ATAPI drives since we cannot change the
* blocksize on ATAPI drives. Used primarily to detect
* if an ATAPI cdrom is present.
*/
if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
sizeof (int), flag) != 0) {
err = EFAULT;
} else {
err = 0;
}
} else {
/*
* Drive supports changing block sizes via a Mode
* Select.
*/
err = sr_change_blkmode(dev, cmd, arg, flag);
}
break;
case CDROMGDRVSPEED:
case CDROMSDRVSPEED:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
if (!ISCD(un)) {
err = ENOTTY;
} else if (un->un_f_mmc_cap == TRUE) {
/*
* Note: In the future the driver implementation
* for getting and
* setting cd speed should entail:
* 1) If non-mmc try the Toshiba mode page
* (sr_change_speed)
* 2) If mmc but no support for Real Time Streaming try
* the SET CD SPEED (0xBB) command
* (sr_atapi_change_speed)
* 3) If mmc and support for Real Time Streaming
* try the GET PERFORMANCE and SET STREAMING
* commands (not yet implemented, 4380808)
*/
/*
* As per recent MMC spec, CD-ROM speed is variable
* and changes with LBA. Since there is no such
* things as drive speed now, fail this ioctl.
*
* Note: EINVAL is returned for consistancy of original
* implementation which included support for getting
* the drive speed of mmc devices but not setting
* the drive speed. Thus EINVAL would be returned
* if a set request was made for an mmc device.
* We no longer support get or set speed for
* mmc but need to remain consistent with regard
* to the error code returned.
*/
err = EINVAL;
} else if (un->un_f_cfg_is_atapi == TRUE) {
err = sr_atapi_change_speed(dev, cmd, arg, flag);
} else {
err = sr_change_speed(dev, cmd, arg, flag);
}
break;
case CDROMCDDA:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sr_read_cdda(dev, (void *)arg, flag);
}
break;
case CDROMCDXA:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sr_read_cdxa(dev, (caddr_t)arg, flag);
}
break;
case CDROMSUBCODE:
SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
if (!ISCD(un)) {
err = ENOTTY;
} else {
err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
}
break;
#ifdef SDDEBUG
/* RESET/ABORTS testing ioctls */
case DKIOCRESET: {
int reset_level;
if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
err = EFAULT;
} else {
SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
"reset_level = 0x%lx\n", reset_level);
if (scsi_reset(SD_ADDRESS(un), reset_level)) {
err = 0;
} else {
err = EIO;
}
}
break;
}
case DKIOCABORT:
SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
if (scsi_abort(SD_ADDRESS(un), NULL)) {
err = 0;
} else {
err = EIO;
}
break;
#endif
#ifdef SD_FAULT_INJECTION
/* SDIOC FaultInjection testing ioctls */
case SDIOCSTART:
case SDIOCSTOP:
case SDIOCINSERTPKT:
case SDIOCINSERTXB:
case SDIOCINSERTUN:
case SDIOCINSERTARQ:
case SDIOCPUSH:
case SDIOCRETRIEVE:
case SDIOCRUN:
SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
"SDIOC detected cmd:0x%X:\n", cmd);
/* call error generator */
sd_faultinjection_ioctl(cmd, arg, un);
err = 0;
break;
#endif /* SD_FAULT_INJECTION */
case DKIOCFLUSHWRITECACHE:
{
struct dk_callback *dkc = (struct dk_callback *)arg;
mutex_enter(SD_MUTEX(un));
if (!un->un_f_sync_cache_supported ||
!un->un_f_write_cache_enabled) {
err = un->un_f_sync_cache_supported ?
0 : ENOTSUP;
mutex_exit(SD_MUTEX(un));
if ((flag & FKIOCTL) && dkc != NULL &&
dkc->dkc_callback != NULL) {
(*dkc->dkc_callback)(dkc->dkc_cookie,
err);
/*
* Did callback and reported error.
* Since we did a callback, ioctl
* should return 0.
*/
err = 0;
}
break;
}
mutex_exit(SD_MUTEX(un));
if ((flag & FKIOCTL) && dkc != NULL &&
dkc->dkc_callback != NULL) {
/* async SYNC CACHE request */
err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
} else {
/* synchronous SYNC CACHE request */
err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
}
}
break;
case DKIOCFREE:
{
dkioc_free_list_t *dfl = (dkioc_free_list_t *)arg;
/* bad ioctls shouldn't panic */
if (dfl == NULL) {
/* check kernel callers strictly in debug */
ASSERT0(flag & FKIOCTL);
err = SET_ERROR(EINVAL);
break;
}
/* synchronous UNMAP request */
err = sd_send_scsi_UNMAP(dev, ssc, dfl, flag);
}
break;
case DKIOC_CANFREE:
SD_TRACE(SD_LOG_IOCTL, un, "DKIOC_CANFREE\n");
i = (un->un_thin_flags & SD_THIN_PROV_ENABLED) ? 1 : 0;
if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
err = EFAULT;
} else {
err = 0;
}
break;
case DKIOCGETWCE: {
int wce;
if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
break;
}
if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
err = EFAULT;
}
break;
}
case DKIOCSETWCE: {
int wce, sync_supported;
int cur_wce = 0;
if (!un->un_f_cache_mode_changeable) {
err = EINVAL;
break;
}
if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
err = EFAULT;
break;
}
/*
* Synchronize multiple threads trying to enable
* or disable the cache via the un_f_wcc_cv
* condition variable.
*/
mutex_enter(SD_MUTEX(un));
/*
* Don't allow the cache to be enabled if the
* config file has it disabled.
*/
if (un->un_f_opt_disable_cache && wce) {
mutex_exit(SD_MUTEX(un));
err = EINVAL;
break;
}
/*
* Wait for write cache change in progress
* bit to be clear before proceeding.
*/
while (un->un_f_wcc_inprog)
cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
un->un_f_wcc_inprog = 1;
mutex_exit(SD_MUTEX(un));
/*
* Get the current write cache state
*/
if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) {
mutex_enter(SD_MUTEX(un));
un->un_f_wcc_inprog = 0;
cv_broadcast(&un->un_wcc_cv);
mutex_exit(SD_MUTEX(un));
break;
}
mutex_enter(SD_MUTEX(un));
un->un_f_write_cache_enabled = (cur_wce != 0);
if (un->un_f_write_cache_enabled && wce == 0) {
/*
* Disable the write cache. Don't clear
* un_f_write_cache_enabled until after
* the mode select and flush are complete.
*/
sync_supported = un->un_f_sync_cache_supported;
/*
* If cache flush is suppressed, we assume that the
* controller firmware will take care of managing the
* write cache for us: no need to explicitly
* disable it.
*/
if (!un->un_f_suppress_cache_flush) {
mutex_exit(SD_MUTEX(un));
if ((err = sd_cache_control(ssc,
SD_CACHE_NOCHANGE,
SD_CACHE_DISABLE)) == 0 &&
sync_supported) {
err = sd_send_scsi_SYNCHRONIZE_CACHE(un,
NULL);
}
} else {
mutex_exit(SD_MUTEX(un));
}
mutex_enter(SD_MUTEX(un));
if (err == 0) {
un->un_f_write_cache_enabled = 0;
}
} else if (!un->un_f_write_cache_enabled && wce != 0) {
/*
* Set un_f_write_cache_enabled first, so there is
* no window where the cache is enabled, but the
* bit says it isn't.
*/
un->un_f_write_cache_enabled = 1;
/*
* If cache flush is suppressed, we assume that the
* controller firmware will take care of managing the
* write cache for us: no need to explicitly
* enable it.
*/
if (!un->un_f_suppress_cache_flush) {
mutex_exit(SD_MUTEX(un));
err = sd_cache_control(ssc, SD_CACHE_NOCHANGE,
SD_CACHE_ENABLE);
} else {
mutex_exit(SD_MUTEX(un));
}
mutex_enter(SD_MUTEX(un));
if (err) {
un->un_f_write_cache_enabled = 0;
}
}
un->un_f_wcc_inprog = 0;
cv_broadcast(&un->un_wcc_cv);
mutex_exit(SD_MUTEX(un));
break;
}
default:
err = ENOTTY;
break;
}
mutex_enter(SD_MUTEX(un));
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
mutex_exit(SD_MUTEX(un));
done_without_assess:
sd_ssc_fini(ssc);
SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
return (err);
done_with_assess:
mutex_enter(SD_MUTEX(un));
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
mutex_exit(SD_MUTEX(un));
done_quick_assess:
if (err != 0)
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
/* Uninitialize sd_ssc_t pointer */
sd_ssc_fini(ssc);
SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
return (err);
}
/*
* Function: sd_dkio_ctrl_info
*
* Description: This routine is the driver entry point for handling controller
* information ioctl requests (DKIOCINFO).
*
* Arguments: dev - the device number
* arg - pointer to user provided dk_cinfo structure
* specifying the controller type and attributes.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: 0
* EFAULT
* ENXIO
*/
static int
sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
{
struct sd_lun *un = NULL;
struct dk_cinfo *info;
dev_info_t *pdip;
int lun, tgt;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
info = (struct dk_cinfo *)
kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
switch (un->un_ctype) {
case CTYPE_CDROM:
info->dki_ctype = DKC_CDROM;
break;
default:
info->dki_ctype = DKC_SCSI_CCS;
break;
}
pdip = ddi_get_parent(SD_DEVINFO(un));
info->dki_cnum = ddi_get_instance(pdip);
if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
(void) strcpy(info->dki_cname, ddi_get_name(pdip));
} else {
(void) strncpy(info->dki_cname, ddi_node_name(pdip),
DK_DEVLEN - 1);
}
lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
/* Unit Information */
info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
info->dki_slave = ((tgt << 3) | lun);
(void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
DK_DEVLEN - 1);
info->dki_flags = DKI_FMTVOL;
info->dki_partition = SDPART(dev);
/* Max Transfer size of this device in blocks */
info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
info->dki_addr = 0;
info->dki_space = 0;
info->dki_prio = 0;
info->dki_vec = 0;
if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
kmem_free(info, sizeof (struct dk_cinfo));
return (EFAULT);
} else {
kmem_free(info, sizeof (struct dk_cinfo));
return (0);
}
}
/*
* Function: sd_get_media_info_com
*
* Description: This routine returns the information required to populate
* the fields for the dk_minfo/dk_minfo_ext structures.
*
* Arguments: dev - the device number
* dki_media_type - media_type
* dki_lbsize - logical block size
* dki_capacity - capacity in blocks
* dki_pbsize - physical block size (if requested)
*
* Return Code: 0
* EACCESS
* EFAULT
* ENXIO
* EIO
*/
static int
sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize,
diskaddr_t *dki_capacity, uint_t *dki_pbsize)
{
struct sd_lun *un = NULL;
struct uscsi_cmd com;
struct scsi_inquiry *sinq;
u_longlong_t media_capacity;
uint64_t capacity;
uint_t lbasize;
uint_t pbsize;
uchar_t *out_data;
uchar_t *rqbuf;
int rval = 0;
int rtn;
sd_ssc_t *ssc;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
(un->un_state == SD_STATE_OFFLINE)) {
return (ENXIO);
}
SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n");
out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
ssc = sd_ssc_init(un);
/* Issue a TUR to determine if the drive is ready with media present */
rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
if (rval == ENXIO) {
goto done;
} else if (rval != 0) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
/* Now get configuration data */
if (ISCD(un)) {
*dki_media_type = DK_CDROM;
/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
if (un->un_f_mmc_cap == TRUE) {
rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
SD_PATH_STANDARD);
if (rtn) {
/*
* We ignore all failures for CD and need to
* put the assessment before processing code
* to avoid missing assessment for FMA.
*/
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
/*
* Failed for other than an illegal request
* or command not supported
*/
if ((com.uscsi_status == STATUS_CHECK) &&
(com.uscsi_rqstatus == STATUS_GOOD)) {
if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
(rqbuf[12] != 0x20)) {
rval = EIO;
goto no_assessment;
}
}
} else {
/*
* The GET CONFIGURATION command succeeded
* so set the media type according to the
* returned data
*/
*dki_media_type = out_data[6];
*dki_media_type <<= 8;
*dki_media_type |= out_data[7];
}
}
} else {
/*
* The profile list is not available, so we attempt to identify
* the media type based on the inquiry data
*/
sinq = un->un_sd->sd_inq;
if ((sinq->inq_dtype == DTYPE_DIRECT) ||
(sinq->inq_dtype == DTYPE_OPTICAL)) {
/* This is a direct access device or optical disk */
*dki_media_type = DK_FIXED_DISK;
if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
(bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
*dki_media_type = DK_ZIP;
} else if (
(bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
*dki_media_type = DK_JAZ;
}
}
} else {
/*
* Not a CD, direct access or optical disk so return
* unknown media
*/
*dki_media_type = DK_UNKNOWN;
}
}
/*
* Now read the capacity so we can provide the lbasize,
* pbsize and capacity.
*/
if (dki_pbsize && un->un_f_descr_format_supported) {
rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
&pbsize, SD_PATH_DIRECT);
/*
* Override the physical blocksize if the instance already
* has a larger value.
*/
pbsize = MAX(pbsize, un->un_phy_blocksize);
}
if (dki_pbsize == NULL || rval != 0 ||
!un->un_f_descr_format_supported) {
rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
SD_PATH_DIRECT);
switch (rval) {
case 0:
if (un->un_f_enable_rmw &&
un->un_phy_blocksize != 0) {
pbsize = un->un_phy_blocksize;
} else {
pbsize = lbasize;
}
media_capacity = capacity;
/*
* sd_send_scsi_READ_CAPACITY() reports capacity in
* un->un_sys_blocksize chunks. So we need to convert
* it into cap.lbsize chunks.
*/
if (un->un_f_has_removable_media) {
media_capacity *= un->un_sys_blocksize;
media_capacity /= lbasize;
}
break;
case EACCES:
rval = EACCES;
goto done;
default:
rval = EIO;
goto done;
}
} else {
if (un->un_f_enable_rmw &&
!ISP2(pbsize % DEV_BSIZE)) {
pbsize = SSD_SECSIZE;
} else if (!ISP2(lbasize % DEV_BSIZE) ||
!ISP2(pbsize % DEV_BSIZE)) {
pbsize = lbasize = DEV_BSIZE;
}
media_capacity = capacity;
}
/*
* If lun is expanded dynamically, update the un structure.
*/
mutex_enter(SD_MUTEX(un));
if ((un->un_f_blockcount_is_valid == TRUE) &&
(un->un_f_tgt_blocksize_is_valid == TRUE) &&
(capacity > un->un_blockcount)) {
un->un_f_expnevent = B_FALSE;
sd_update_block_info(un, lbasize, capacity);
}
mutex_exit(SD_MUTEX(un));
*dki_lbsize = lbasize;
*dki_capacity = media_capacity;
if (dki_pbsize)
*dki_pbsize = pbsize;
done:
if (rval != 0) {
if (rval == EIO)
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
else
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
no_assessment:
sd_ssc_fini(ssc);
kmem_free(out_data, SD_PROFILE_HEADER_LEN);
kmem_free(rqbuf, SENSE_LENGTH);
return (rval);
}
/*
* Function: sd_get_media_info
*
* Description: This routine is the driver entry point for handling ioctl
* requests for the media type or command set profile used by the
* drive to operate on the media (DKIOCGMEDIAINFO).
*
* Arguments: dev - the device number
* arg - pointer to user provided dk_minfo structure
* specifying the media type, logical block size and
* drive capacity.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: returns the value from sd_get_media_info_com
*/
static int
sd_get_media_info(dev_t dev, caddr_t arg, int flag)
{
struct dk_minfo mi;
int rval;
rval = sd_get_media_info_com(dev, &mi.dki_media_type,
&mi.dki_lbsize, &mi.dki_capacity, NULL);
if (rval)
return (rval);
if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag))
rval = EFAULT;
return (rval);
}
/*
* Function: sd_get_media_info_ext
*
* Description: This routine is the driver entry point for handling ioctl
* requests for the media type or command set profile used by the
* drive to operate on the media (DKIOCGMEDIAINFOEXT). The
* difference this ioctl and DKIOCGMEDIAINFO is the return value
* of this ioctl contains both logical block size and physical
* block size.
*
*
* Arguments: dev - the device number
* arg - pointer to user provided dk_minfo_ext structure
* specifying the media type, logical block size,
* physical block size and disk capacity.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: returns the value from sd_get_media_info_com
*/
static int
sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag)
{
struct dk_minfo_ext mie;
int rval = 0;
size_t len;
rval = sd_get_media_info_com(dev, &mie.dki_media_type,
&mie.dki_lbsize, &mie.dki_capacity, &mie.dki_pbsize);
if (rval)
return (rval);
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32:
len = sizeof (struct dk_minfo_ext32);
break;
default:
len = sizeof (struct dk_minfo_ext);
break;
}
if (ddi_copyout(&mie, arg, len, flag))
rval = EFAULT;
return (rval);
}
/*
* Function: sd_watch_request_submit
*
* Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit
* depending on which is supported by device.
*/
static opaque_t
sd_watch_request_submit(struct sd_lun *un)
{
dev_t dev;
/* All submissions are unified to use same device number */
dev = sd_make_device(SD_DEVINFO(un));
if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un),
sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
(caddr_t)dev));
} else {
return (scsi_watch_request_submit(SD_SCSI_DEVP(un),
sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
(caddr_t)dev));
}
}
/*
* Function: sd_check_media
*
* Description: This utility routine implements the functionality for the
* DKIOCSTATE ioctl. This ioctl blocks the user thread until the
* driver state changes from that specified by the user
* (inserted or ejected). For example, if the user specifies
* DKIO_EJECTED and the current media state is inserted this
* routine will immediately return DKIO_INSERTED. However, if the
* current media state is not inserted the user thread will be
* blocked until the drive state changes. If DKIO_NONE is specified
* the user thread will block until a drive state change occurs.
*
* Arguments: dev - the device number
* state - user pointer to a dkio_state, updated with the current
* drive state at return.
*
* Return Code: ENXIO
* EIO
* EAGAIN
* EINTR
*/
static int
sd_check_media(dev_t dev, enum dkio_state state)
{
struct sd_lun *un = NULL;
enum dkio_state prev_state;
opaque_t token = NULL;
int rval = 0;
sd_ssc_t *ssc;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
ssc = sd_ssc_init(un);
mutex_enter(SD_MUTEX(un));
SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
"state=%x, mediastate=%x\n", state, un->un_mediastate);
prev_state = un->un_mediastate;
/* is there anything to do? */
if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
/*
* submit the request to the scsi_watch service;
* scsi_media_watch_cb() does the real work
*/
mutex_exit(SD_MUTEX(un));
/*
* This change handles the case where a scsi watch request is
* added to a device that is powered down. To accomplish this
* we power up the device before adding the scsi watch request,
* since the scsi watch sends a TUR directly to the device
* which the device cannot handle if it is powered down.
*/
if (sd_pm_entry(un) != DDI_SUCCESS) {
mutex_enter(SD_MUTEX(un));
goto done;
}
token = sd_watch_request_submit(un);
sd_pm_exit(un);
mutex_enter(SD_MUTEX(un));
if (token == NULL) {
rval = EAGAIN;
goto done;
}
/*
* This is a special case IOCTL that doesn't return
* until the media state changes. Routine sdpower
* knows about and handles this so don't count it
* as an active cmd in the driver, which would
* keep the device busy to the pm framework.
* If the count isn't decremented the device can't
* be powered down.
*/
un->un_ncmds_in_driver--;
ASSERT(un->un_ncmds_in_driver >= 0);
/*
* if a prior request had been made, this will be the same
* token, as scsi_watch was designed that way.
*/
un->un_swr_token = token;
un->un_specified_mediastate = state;
/*
* now wait for media change
* we will not be signalled unless mediastate == state but it is
* still better to test for this condition, since there is a
* 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
*/
SD_TRACE(SD_LOG_COMMON, un,
"sd_check_media: waiting for media state change\n");
while (un->un_mediastate == state) {
if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
SD_TRACE(SD_LOG_COMMON, un,
"sd_check_media: waiting for media state "
"was interrupted\n");
un->un_ncmds_in_driver++;
rval = EINTR;
goto done;
}
SD_TRACE(SD_LOG_COMMON, un,
"sd_check_media: received signal, state=%x\n",
un->un_mediastate);
}
/*
* Inc the counter to indicate the device once again
* has an active outstanding cmd.
*/
un->un_ncmds_in_driver++;
}
/* invalidate geometry */
if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
sr_ejected(un);
}
if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
uint64_t capacity;
uint_t lbasize;
SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
mutex_exit(SD_MUTEX(un));
/*
* Since the following routines use SD_PATH_DIRECT, we must
* call PM directly before the upcoming disk accesses. This
* may cause the disk to be power/spin up.
*/
if (sd_pm_entry(un) == DDI_SUCCESS) {
rval = sd_send_scsi_READ_CAPACITY(ssc,
&capacity, &lbasize, SD_PATH_DIRECT);
if (rval != 0) {
sd_pm_exit(un);
if (rval == EIO)
sd_ssc_assessment(ssc,
SD_FMT_STATUS_CHECK);
else
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
mutex_enter(SD_MUTEX(un));
goto done;
}
} else {
rval = EIO;
mutex_enter(SD_MUTEX(un));
goto done;
}
mutex_enter(SD_MUTEX(un));
sd_update_block_info(un, lbasize, capacity);
/*
* Check if the media in the device is writable or not
*/
if (ISCD(un)) {
sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
}
mutex_exit(SD_MUTEX(un));
cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
if ((cmlb_validate(un->un_cmlbhandle, 0,
(void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) {
sd_set_pstats(un);
SD_TRACE(SD_LOG_IO_PARTITION, un,
"sd_check_media: un:0x%p pstats created and "
"set\n", un);
}
rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
SD_PATH_DIRECT);
sd_pm_exit(un);
if (rval != 0) {
if (rval == EIO)
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
else
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
mutex_enter(SD_MUTEX(un));
}
done:
sd_ssc_fini(ssc);
un->un_f_watcht_stopped = FALSE;
if (token != NULL && un->un_swr_token != NULL) {
/*
* Use of this local token and the mutex ensures that we avoid
* some race conditions associated with terminating the
* scsi watch.
*/
token = un->un_swr_token;
mutex_exit(SD_MUTEX(un));
(void) scsi_watch_request_terminate(token,
SCSI_WATCH_TERMINATE_WAIT);
if (scsi_watch_get_ref_count(token) == 0) {
mutex_enter(SD_MUTEX(un));
un->un_swr_token = (opaque_t)NULL;
} else {
mutex_enter(SD_MUTEX(un));
}
}
/*
* Update the capacity kstat value, if no media previously
* (capacity kstat is 0) and a media has been inserted
* (un_f_blockcount_is_valid == TRUE)
*/
if (un->un_errstats) {
struct sd_errstats *stp = NULL;
stp = (struct sd_errstats *)un->un_errstats->ks_data;
if ((stp->sd_capacity.value.ui64 == 0) &&
(un->un_f_blockcount_is_valid == TRUE)) {
stp->sd_capacity.value.ui64 =
(uint64_t)((uint64_t)un->un_blockcount *
un->un_sys_blocksize);
}
}
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
return (rval);
}
/*
* Function: sd_delayed_cv_broadcast
*
* Description: Delayed cv_broadcast to allow for target to recover from media
* insertion.
*
* Arguments: arg - driver soft state (unit) structure
*/
static void
sd_delayed_cv_broadcast(void *arg)
{
struct sd_lun *un = arg;
SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
mutex_enter(SD_MUTEX(un));
un->un_dcvb_timeid = NULL;
cv_broadcast(&un->un_state_cv);
mutex_exit(SD_MUTEX(un));
}
/*
* Function: sd_media_watch_cb
*
* Description: Callback routine used for support of the DKIOCSTATE ioctl. This
* routine processes the TUR sense data and updates the driver
* state if a transition has occurred. The user thread
* (sd_check_media) is then signalled.
*
* Arguments: arg - the device 'dev_t' is used for context to discriminate
* among multiple watches that share this callback function
* resultp - scsi watch facility result packet containing scsi
* packet, status byte and sense data
*
* Return Code: 0 for success, -1 for failure
*/
static int
sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
{
struct sd_lun *un;
struct scsi_status *statusp = resultp->statusp;
uint8_t *sensep = (uint8_t *)resultp->sensep;
enum dkio_state state = DKIO_NONE;
dev_t dev = (dev_t)arg;
uchar_t actual_sense_length;
uint8_t skey, asc, ascq;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (-1);
}
actual_sense_length = resultp->actual_sense_length;
mutex_enter(SD_MUTEX(un));
SD_TRACE(SD_LOG_COMMON, un,
"sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
*((char *)statusp), (void *)sensep, actual_sense_length);
if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
un->un_mediastate = DKIO_DEV_GONE;
cv_broadcast(&un->un_state_cv);
mutex_exit(SD_MUTEX(un));
return (0);
}
if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
if (sd_gesn_media_data_valid(resultp->mmc_data)) {
if ((resultp->mmc_data[5] &
SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) {
state = DKIO_INSERTED;
} else {
state = DKIO_EJECTED;
}
if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) ==
SD_GESN_MEDIA_EVENT_EJECTREQUEST) {
sd_log_eject_request_event(un, KM_NOSLEEP);
}
}
} else if (sensep != NULL) {
/*
* If there was a check condition then sensep points to valid
* sense data. If status was not a check condition but a
* reservation or busy status then the new state is DKIO_NONE.
*/
skey = scsi_sense_key(sensep);
asc = scsi_sense_asc(sensep);
ascq = scsi_sense_ascq(sensep);
SD_INFO(SD_LOG_COMMON, un,
"sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
skey, asc, ascq);
/* This routine only uses up to 13 bytes of sense data. */
if (actual_sense_length >= 13) {
if (skey == KEY_UNIT_ATTENTION) {
if (asc == 0x28) {
state = DKIO_INSERTED;
}
} else if (skey == KEY_NOT_READY) {
/*
* Sense data of 02/06/00 means that the
* drive could not read the media (No
* reference position found). In this case
* to prevent a hang on the DKIOCSTATE IOCTL
* we set the media state to DKIO_INSERTED.
*/
if (asc == 0x06 && ascq == 0x00)
state = DKIO_INSERTED;
/*
* if 02/04/02 means that the host
* should send start command. Explicitly
* leave the media state as is
* (inserted) as the media is inserted
* and host has stopped device for PM
* reasons. Upon next true read/write
* to this media will bring the
* device to the right state good for
* media access.
*/
if (asc == 0x3a) {
state = DKIO_EJECTED;
} else {
/*
* If the drive is busy with an
* operation or long write, keep the
* media in an inserted state.
*/
if ((asc == 0x04) &&
((ascq == 0x02) ||
(ascq == 0x07) ||
(ascq == 0x08))) {
state = DKIO_INSERTED;
}
}
} else if (skey == KEY_NO_SENSE) {
if ((asc == 0x00) && (ascq == 0x00)) {
/*
* Sense Data 00/00/00 does not provide
* any information about the state of
* the media. Ignore it.
*/
mutex_exit(SD_MUTEX(un));
return (0);
}
}
}
} else if ((*((char *)statusp) == STATUS_GOOD) &&
(resultp->pkt->pkt_reason == CMD_CMPLT)) {
state = DKIO_INSERTED;
}
SD_TRACE(SD_LOG_COMMON, un,
"sd_media_watch_cb: state=%x, specified=%x\n",
state, un->un_specified_mediastate);
/*
* now signal the waiting thread if this is *not* the specified state;
* delay the signal if the state is DKIO_INSERTED to allow the target
* to recover
*/
if (state != un->un_specified_mediastate) {
un->un_mediastate = state;
if (state == DKIO_INSERTED) {
/*
* delay the signal to give the drive a chance
* to do what it apparently needs to do
*/
SD_TRACE(SD_LOG_COMMON, un,
"sd_media_watch_cb: delayed cv_broadcast\n");
if (un->un_dcvb_timeid == NULL) {
un->un_dcvb_timeid =
timeout(sd_delayed_cv_broadcast, un,
drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
}
} else {
SD_TRACE(SD_LOG_COMMON, un,
"sd_media_watch_cb: immediate cv_broadcast\n");
cv_broadcast(&un->un_state_cv);
}
}
mutex_exit(SD_MUTEX(un));
return (0);
}
/*
* Function: sd_dkio_get_temp
*
* Description: This routine is the driver entry point for handling ioctl
* requests to get the disk temperature.
*
* Arguments: dev - the device number
* arg - pointer to user provided dk_temperature structure.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: 0
* EFAULT
* ENXIO
* EAGAIN
*/
static int
sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
{
struct sd_lun *un = NULL;
struct dk_temperature *dktemp = NULL;
uchar_t *temperature_page;
int rval = 0;
int path_flag = SD_PATH_STANDARD;
sd_ssc_t *ssc;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
ssc = sd_ssc_init(un);
dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
/* copyin the disk temp argument to get the user flags */
if (ddi_copyin((void *)arg, dktemp,
sizeof (struct dk_temperature), flag) != 0) {
rval = EFAULT;
goto done;
}
/* Initialize the temperature to invalid. */
dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
/*
* Note: Investigate removing the "bypass pm" semantic.
* Can we just bypass PM always?
*/
if (dktemp->dkt_flags & DKT_BYPASS_PM) {
path_flag = SD_PATH_DIRECT;
ASSERT(!mutex_owned(&un->un_pm_mutex));
mutex_enter(&un->un_pm_mutex);
if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
/*
* If DKT_BYPASS_PM is set, and the drive happens to be
* in low power mode, we can not wake it up, Need to
* return EAGAIN.
*/
mutex_exit(&un->un_pm_mutex);
rval = EAGAIN;
goto done;
} else {
/*
* Indicate to PM the device is busy. This is required
* to avoid a race - i.e. the ioctl is issuing a
* command and the pm framework brings down the device
* to low power mode (possible power cut-off on some
* platforms).
*/
mutex_exit(&un->un_pm_mutex);
if (sd_pm_entry(un) != DDI_SUCCESS) {
rval = EAGAIN;
goto done;
}
}
}
temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page,
TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag);
if (rval != 0)
goto done2;
/*
* For the current temperature verify that the parameter length is 0x02
* and the parameter code is 0x00
*/
if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
(temperature_page[5] == 0x00)) {
if (temperature_page[9] == 0xFF) {
dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
} else {
dktemp->dkt_cur_temp = (short)(temperature_page[9]);
}
}
/*
* For the reference temperature verify that the parameter
* length is 0x02 and the parameter code is 0x01
*/
if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
(temperature_page[11] == 0x01)) {
if (temperature_page[15] == 0xFF) {
dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
} else {
dktemp->dkt_ref_temp = (short)(temperature_page[15]);
}
}
/* Do the copyout regardless of the temperature commands status. */
if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
flag) != 0) {
rval = EFAULT;
goto done1;
}
done2:
if (rval != 0) {
if (rval == EIO)
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
else
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
done1:
if (path_flag == SD_PATH_DIRECT) {
sd_pm_exit(un);
}
kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
done:
sd_ssc_fini(ssc);
if (dktemp != NULL) {
kmem_free(dktemp, sizeof (struct dk_temperature));
}
return (rval);
}
/*
* Function: sd_log_page_supported
*
* Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
* supported log pages.
*
* Arguments: ssc - ssc contains pointer to driver soft state (unit)
* structure for this target.
* log_page -
*
* Return Code: -1 - on error (log sense is optional and may not be supported).
* 0 - log page not found.
* 1 - log page found.
*/
static int
sd_log_page_supported(sd_ssc_t *ssc, int log_page)
{
uchar_t *log_page_data;
int i;
int match = 0;
int log_size;
int status = 0;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
SD_PATH_DIRECT);
if (status != 0) {
if (status == EIO) {
/*
* Some disks do not support log sense, we
* should ignore this kind of error(sense key is
* 0x5 - illegal request).
*/
uint8_t *sensep;
int senlen;
sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
ssc->ssc_uscsi_cmd->uscsi_rqresid);
if (senlen > 0 &&
scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
sd_ssc_assessment(ssc,
SD_FMT_IGNORE_COMPROMISE);
} else {
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
}
} else {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
SD_ERROR(SD_LOG_COMMON, un,
"sd_log_page_supported: failed log page retrieval\n");
kmem_free(log_page_data, 0xFF);
return (-1);
}
log_size = log_page_data[3];
/*
* The list of supported log pages start from the fourth byte. Check
* until we run out of log pages or a match is found.
*/
for (i = 4; (i < (log_size + 4)) && !match; i++) {
if (log_page_data[i] == log_page) {
match++;
}
}
kmem_free(log_page_data, 0xFF);
return (match);
}
/*
* Function: sd_mhdioc_failfast
*
* Description: This routine is the driver entry point for handling ioctl
* requests to enable/disable the multihost failfast option.
* (MHIOCENFAILFAST)
*
* Arguments: dev - the device number
* arg - user specified probing interval.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: 0
* EFAULT
* ENXIO
*/
static int
sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
{
struct sd_lun *un = NULL;
int mh_time;
int rval = 0;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
return (EFAULT);
if (mh_time) {
mutex_enter(SD_MUTEX(un));
un->un_resvd_status |= SD_FAILFAST;
mutex_exit(SD_MUTEX(un));
/*
* If mh_time is INT_MAX, then this ioctl is being used for
* SCSI-3 PGR purposes, and we don't need to spawn watch thread.
*/
if (mh_time != INT_MAX) {
rval = sd_check_mhd(dev, mh_time);
}
} else {
(void) sd_check_mhd(dev, 0);
mutex_enter(SD_MUTEX(un));
un->un_resvd_status &= ~SD_FAILFAST;
mutex_exit(SD_MUTEX(un));
}
return (rval);
}
/*
* Function: sd_mhdioc_takeown
*
* Description: This routine is the driver entry point for handling ioctl
* requests to forcefully acquire exclusive access rights to the
* multihost disk (MHIOCTKOWN).
*
* Arguments: dev - the device number
* arg - user provided structure specifying the delay
* parameters in milliseconds
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: 0
* EFAULT
* ENXIO
*/
static int
sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
{
struct sd_lun *un = NULL;
struct mhioctkown *tkown = NULL;
int rval = 0;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
if (arg != NULL) {
tkown = (struct mhioctkown *)
kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
if (rval != 0) {
rval = EFAULT;
goto error;
}
}
rval = sd_take_ownership(dev, tkown);
mutex_enter(SD_MUTEX(un));
if (rval == 0) {
un->un_resvd_status |= SD_RESERVE;
if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
sd_reinstate_resv_delay =
tkown->reinstate_resv_delay * 1000;
} else {
sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
}
/*
* Give the scsi_watch routine interval set by
* the MHIOCENFAILFAST ioctl precedence here.
*/
if ((un->un_resvd_status & SD_FAILFAST) == 0) {
mutex_exit(SD_MUTEX(un));
(void) sd_check_mhd(dev,
sd_reinstate_resv_delay / 1000);
SD_TRACE(SD_LOG_IOCTL_MHD, un,
"sd_mhdioc_takeown : %d\n",
sd_reinstate_resv_delay);
} else {
mutex_exit(SD_MUTEX(un));
}
(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
sd_mhd_reset_notify_cb, (caddr_t)un);
} else {
un->un_resvd_status &= ~SD_RESERVE;
mutex_exit(SD_MUTEX(un));
}
error:
if (tkown != NULL) {
kmem_free(tkown, sizeof (struct mhioctkown));
}
return (rval);
}
/*
* Function: sd_mhdioc_release
*
* Description: This routine is the driver entry point for handling ioctl
* requests to release exclusive access rights to the multihost
* disk (MHIOCRELEASE).
*
* Arguments: dev - the device number
*
* Return Code: 0
* ENXIO
*/
static int
sd_mhdioc_release(dev_t dev)
{
struct sd_lun *un = NULL;
timeout_id_t resvd_timeid_save;
int resvd_status_save;
int rval = 0;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
mutex_enter(SD_MUTEX(un));
resvd_status_save = un->un_resvd_status;
un->un_resvd_status &=
~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
if (un->un_resvd_timeid) {
resvd_timeid_save = un->un_resvd_timeid;
un->un_resvd_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(resvd_timeid_save);
} else {
mutex_exit(SD_MUTEX(un));
}
/*
* destroy any pending timeout thread that may be attempting to
* reinstate reservation on this device.
*/
sd_rmv_resv_reclaim_req(dev);
if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
mutex_enter(SD_MUTEX(un));
if ((un->un_mhd_token) &&
((un->un_resvd_status & SD_FAILFAST) == 0)) {
mutex_exit(SD_MUTEX(un));
(void) sd_check_mhd(dev, 0);
} else {
mutex_exit(SD_MUTEX(un));
}
(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
sd_mhd_reset_notify_cb, (caddr_t)un);
} else {
/*
* sd_mhd_watch_cb will restart the resvd recover timeout thread
*/
mutex_enter(SD_MUTEX(un));
un->un_resvd_status = resvd_status_save;
mutex_exit(SD_MUTEX(un));
}
return (rval);
}
/*
* Function: sd_mhdioc_register_devid
*
* Description: This routine is the driver entry point for handling ioctl
* requests to register the device id (MHIOCREREGISTERDEVID).
*
* Note: The implementation for this ioctl has been updated to
* be consistent with the original PSARC case (1999/357)
* (4375899, 4241671, 4220005)
*
* Arguments: dev - the device number
*
* Return Code: 0
* ENXIO
*/
static int
sd_mhdioc_register_devid(dev_t dev)
{
struct sd_lun *un = NULL;
int rval = 0;
sd_ssc_t *ssc;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
ASSERT(!mutex_owned(SD_MUTEX(un)));
mutex_enter(SD_MUTEX(un));
/* If a devid already exists, de-register it */
if (un->un_devid != NULL) {
ddi_devid_unregister(SD_DEVINFO(un));
/*
* After unregister devid, needs to free devid memory
*/
ddi_devid_free(un->un_devid);
un->un_devid = NULL;
}
/* Check for reservation conflict */
mutex_exit(SD_MUTEX(un));
ssc = sd_ssc_init(un);
rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
mutex_enter(SD_MUTEX(un));
switch (rval) {
case 0:
sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
break;
case EACCES:
break;
default:
rval = EIO;
}
mutex_exit(SD_MUTEX(un));
if (rval != 0) {
if (rval == EIO)
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
else
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
sd_ssc_fini(ssc);
return (rval);
}
/*
* Function: sd_mhdioc_inkeys
*
* Description: This routine is the driver entry point for handling ioctl
* requests to issue the SCSI-3 Persistent In Read Keys command
* to the device (MHIOCGRP_INKEYS).
*
* Arguments: dev - the device number
* arg - user provided in_keys structure
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: code returned by sd_persistent_reservation_in_read_keys()
* ENXIO
* EFAULT
*/
static int
sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
{
struct sd_lun *un;
mhioc_inkeys_t inkeys;
int rval = 0;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32: {
struct mhioc_inkeys32 inkeys32;
if (ddi_copyin(arg, &inkeys32,
sizeof (struct mhioc_inkeys32), flag) != 0) {
return (EFAULT);
}
inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
if ((rval = sd_persistent_reservation_in_read_keys(un,
&inkeys, flag)) != 0) {
return (rval);
}
inkeys32.generation = inkeys.generation;
if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
flag) != 0) {
return (EFAULT);
}
break;
}
case DDI_MODEL_NONE:
if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
flag) != 0) {
return (EFAULT);
}
if ((rval = sd_persistent_reservation_in_read_keys(un,
&inkeys, flag)) != 0) {
return (rval);
}
if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
flag) != 0) {
return (EFAULT);
}
break;
}
#else /* ! _MULTI_DATAMODEL */
if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
return (EFAULT);
}
rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
if (rval != 0) {
return (rval);
}
if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
return (EFAULT);
}
#endif /* _MULTI_DATAMODEL */
return (rval);
}
/*
* Function: sd_mhdioc_inresv
*
* Description: This routine is the driver entry point for handling ioctl
* requests to issue the SCSI-3 Persistent In Read Reservations
* command to the device (MHIOCGRP_INKEYS).
*
* Arguments: dev - the device number
* arg - user provided in_resv structure
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: code returned by sd_persistent_reservation_in_read_resv()
* ENXIO
* EFAULT
*/
static int
sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
{
struct sd_lun *un;
mhioc_inresvs_t inresvs;
int rval = 0;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32: {
struct mhioc_inresvs32 inresvs32;
if (ddi_copyin(arg, &inresvs32,
sizeof (struct mhioc_inresvs32), flag) != 0) {
return (EFAULT);
}
inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
if ((rval = sd_persistent_reservation_in_read_resv(un,
&inresvs, flag)) != 0) {
return (rval);
}
inresvs32.generation = inresvs.generation;
if (ddi_copyout(&inresvs32, arg,
sizeof (struct mhioc_inresvs32), flag) != 0) {
return (EFAULT);
}
break;
}
case DDI_MODEL_NONE:
if (ddi_copyin(arg, &inresvs,
sizeof (mhioc_inresvs_t), flag) != 0) {
return (EFAULT);
}
if ((rval = sd_persistent_reservation_in_read_resv(un,
&inresvs, flag)) != 0) {
return (rval);
}
if (ddi_copyout(&inresvs, arg,
sizeof (mhioc_inresvs_t), flag) != 0) {
return (EFAULT);
}
break;
}
#else /* ! _MULTI_DATAMODEL */
if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
return (EFAULT);
}
rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
if (rval != 0) {
return (rval);
}
if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
return (EFAULT);
}
#endif /* ! _MULTI_DATAMODEL */
return (rval);
}
/*
* The following routines support the clustering functionality described below
* and implement lost reservation reclaim functionality.
*
* Clustering
* ----------
* The clustering code uses two different, independent forms of SCSI
* reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
* Persistent Group Reservations. For any particular disk, it will use either
* SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
*
* SCSI-2
* The cluster software takes ownership of a multi-hosted disk by issuing the
* MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
* MHIOCRELEASE ioctl. Closely related is the MHIOCENFAILFAST ioctl -- a
* cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl
* then issues the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the
* driver. The meaning of failfast is that if the driver (on this host) ever
* encounters the scsi error return code RESERVATION_CONFLICT from the device,
* it should immediately panic the host. The motivation for this ioctl is that
* if this host does encounter reservation conflict, the underlying cause is
* that some other host of the cluster has decided that this host is no longer
* in the cluster and has seized control of the disks for itself. Since this
* host is no longer in the cluster, it ought to panic itself. The
* MHIOCENFAILFAST ioctl does two things:
* (a) it sets a flag that will cause any returned RESERVATION_CONFLICT
* error to panic the host
* (b) it sets up a periodic timer to test whether this host still has
* "access" (in that no other host has reserved the device): if the
* periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
* purpose of that periodic timer is to handle scenarios where the host is
* otherwise temporarily quiescent, temporarily doing no real i/o.
* The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
* by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for
* the device itself.
*
* SCSI-3 PGR
* A direct semantic implementation of the SCSI-3 Persistent Reservation
* facility is supported through the shared multihost disk ioctls
* (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
* MHIOCGRP_PREEMPTANDABORT, MHIOCGRP_CLEAR)
*
* Reservation Reclaim:
* --------------------
* To support the lost reservation reclaim operations this driver creates a
* single thread to handle reinstating reservations on all devices that have
* lost reservations sd_resv_reclaim_requests are logged for all devices that
* have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
* and the reservation reclaim thread loops through the requests to regain the
* lost reservations.
*/
/*
* Function: sd_check_mhd()
*
* Description: This function sets up and submits a scsi watch request or
* terminates an existing watch request. This routine is used in
* support of reservation reclaim.
*
* Arguments: dev - the device 'dev_t' is used for context to discriminate
* among multiple watches that share the callback function
* interval - the number of microseconds specifying the watch
* interval for issuing TEST UNIT READY commands. If
* set to 0 the watch should be terminated. If the
* interval is set to 0 and if the device is required
* to hold reservation while disabling failfast, the
* watch is restarted with an interval of
* reinstate_resv_delay.
*
* Return Code: 0 - Successful submit/terminate of scsi watch request
* ENXIO - Indicates an invalid device was specified
* EAGAIN - Unable to submit the scsi watch request
*/
static int
sd_check_mhd(dev_t dev, int interval)
{
struct sd_lun *un;
opaque_t token;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
/* is this a watch termination request? */
if (interval == 0) {
mutex_enter(SD_MUTEX(un));
/* if there is an existing watch task then terminate it */
if (un->un_mhd_token) {
token = un->un_mhd_token;
un->un_mhd_token = NULL;
mutex_exit(SD_MUTEX(un));
(void) scsi_watch_request_terminate(token,
SCSI_WATCH_TERMINATE_ALL_WAIT);
mutex_enter(SD_MUTEX(un));
} else {
mutex_exit(SD_MUTEX(un));
/*
* Note: If we return here we don't check for the
* failfast case. This is the original legacy
* implementation but perhaps we should be checking
* the failfast case.
*/
return (0);
}
/*
* If the device is required to hold reservation while
* disabling failfast, we need to restart the scsi_watch
* routine with an interval of reinstate_resv_delay.
*/
if (un->un_resvd_status & SD_RESERVE) {
interval = sd_reinstate_resv_delay / 1000;
} else {
/* no failfast so bail */
mutex_exit(SD_MUTEX(un));
return (0);
}
mutex_exit(SD_MUTEX(un));
}
/*
* adjust minimum time interval to 1 second,
* and convert from msecs to usecs
*/
if (interval > 0 && interval < 1000) {
interval = 1000;
}
interval *= 1000;
/*
* submit the request to the scsi_watch service
*/
token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
if (token == NULL) {
return (EAGAIN);
}
/*
* save token for termination later on
*/
mutex_enter(SD_MUTEX(un));
un->un_mhd_token = token;
mutex_exit(SD_MUTEX(un));
return (0);
}
/*
* Function: sd_mhd_watch_cb()
*
* Description: This function is the call back function used by the scsi watch
* facility. The scsi watch facility sends the "Test Unit Ready"
* and processes the status. If applicable (i.e. a "Unit Attention"
* status and automatic "Request Sense" not used) the scsi watch
* facility will send a "Request Sense" and retrieve the sense data
* to be passed to this callback function. In either case the
* automatic "Request Sense" or the facility submitting one, this
* callback is passed the status and sense data.
*
* Arguments: arg - the device 'dev_t' is used for context to discriminate
* among multiple watches that share this callback function
* resultp - scsi watch facility result packet containing scsi
* packet, status byte and sense data
*
* Return Code: 0 - continue the watch task
* non-zero - terminate the watch task
*/
static int
sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
{
struct sd_lun *un;
struct scsi_status *statusp;
uint8_t *sensep;
struct scsi_pkt *pkt;
uchar_t actual_sense_length;
dev_t dev = (dev_t)arg;
ASSERT(resultp != NULL);
statusp = resultp->statusp;
sensep = (uint8_t *)resultp->sensep;
pkt = resultp->pkt;
actual_sense_length = resultp->actual_sense_length;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
SD_TRACE(SD_LOG_IOCTL_MHD, un,
"sd_mhd_watch_cb: reason '%s', status '%s'\n",
scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
/* Begin processing of the status and/or sense data */
if (pkt->pkt_reason != CMD_CMPLT) {
/* Handle the incomplete packet */
sd_mhd_watch_incomplete(un, pkt);
return (0);
} else if (*((unsigned char *)statusp) != STATUS_GOOD) {
if (*((unsigned char *)statusp)
== STATUS_RESERVATION_CONFLICT) {
/*
* Handle a reservation conflict by panicking if
* configured for failfast or by logging the conflict
* and updating the reservation status
*/
mutex_enter(SD_MUTEX(un));
if ((un->un_resvd_status & SD_FAILFAST) &&
(sd_failfast_enable)) {
sd_panic_for_res_conflict(un);
/*NOTREACHED*/
}
SD_INFO(SD_LOG_IOCTL_MHD, un,
"sd_mhd_watch_cb: Reservation Conflict\n");
un->un_resvd_status |= SD_RESERVATION_CONFLICT;
mutex_exit(SD_MUTEX(un));
}
}
if (sensep != NULL) {
if (actual_sense_length >= (SENSE_LENGTH - 2)) {
mutex_enter(SD_MUTEX(un));
if ((scsi_sense_asc(sensep) ==
SD_SCSI_RESET_SENSE_CODE) &&
(un->un_resvd_status & SD_RESERVE)) {
/*
* The additional sense code indicates a power
* on or bus device reset has occurred; update
* the reservation status.
*/
un->un_resvd_status |=
(SD_LOST_RESERVE | SD_WANT_RESERVE);
SD_INFO(SD_LOG_IOCTL_MHD, un,
"sd_mhd_watch_cb: Lost Reservation\n");
}
} else {
return (0);
}
} else {
mutex_enter(SD_MUTEX(un));
}
if ((un->un_resvd_status & SD_RESERVE) &&
(un->un_resvd_status & SD_LOST_RESERVE)) {
if (un->un_resvd_status & SD_WANT_RESERVE) {
/*
* A reset occurred in between the last probe and this
* one so if a timeout is pending cancel it.
*/
if (un->un_resvd_timeid) {
timeout_id_t temp_id = un->un_resvd_timeid;
un->un_resvd_timeid = NULL;
mutex_exit(SD_MUTEX(un));
(void) untimeout(temp_id);
mutex_enter(SD_MUTEX(un));
}
un->un_resvd_status &= ~SD_WANT_RESERVE;
}
if (un->un_resvd_timeid == 0) {
/* Schedule a timeout to handle the lost reservation */
un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
(void *)dev,
drv_usectohz(sd_reinstate_resv_delay));
}
}
mutex_exit(SD_MUTEX(un));
return (0);
}
/*
* Function: sd_mhd_watch_incomplete()
*
* Description: This function is used to find out why a scsi pkt sent by the
* scsi watch facility was not completed. Under some scenarios this
* routine will return. Otherwise it will send a bus reset to see
* if the drive is still online.
*
* Arguments: un - driver soft state (unit) structure
* pkt - incomplete scsi pkt
*/
static void
sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
{
int be_chatty;
int perr;
ASSERT(pkt != NULL);
ASSERT(un != NULL);
be_chatty = (!(pkt->pkt_flags & FLAG_SILENT));
perr = (pkt->pkt_statistics & STAT_PERR);
mutex_enter(SD_MUTEX(un));
if (un->un_state == SD_STATE_DUMPING) {
mutex_exit(SD_MUTEX(un));
return;
}
switch (pkt->pkt_reason) {
case CMD_UNX_BUS_FREE:
/*
* If we had a parity error that caused the target to drop BSY*,
* don't be chatty about it.
*/
if (perr && be_chatty) {
be_chatty = 0;
}
break;
case CMD_TAG_REJECT:
/*
* The SCSI-2 spec states that a tag reject will be sent by the
* target if tagged queuing is not supported. A tag reject may
* also be sent during certain initialization periods or to
* control internal resources. For the latter case the target
* may also return Queue Full.
*
* If this driver receives a tag reject from a target that is
* going through an init period or controlling internal
* resources tagged queuing will be disabled. This is a less
* than optimal behavior but the driver is unable to determine
* the target state and assumes tagged queueing is not supported
*/
pkt->pkt_flags = 0;
un->un_tagflags = 0;
if (un->un_f_opt_queueing == TRUE) {
un->un_throttle = min(un->un_throttle, 3);
} else {
un->un_throttle = 1;
}
mutex_exit(SD_MUTEX(un));
(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
mutex_enter(SD_MUTEX(un));
break;
case CMD_INCOMPLETE:
/*
* The transport stopped with an abnormal state, fallthrough and
* reset the target and/or bus unless selection did not complete
* (indicated by STATE_GOT_BUS) in which case we don't want to
* go through a target/bus reset
*/
if (pkt->pkt_state == STATE_GOT_BUS) {
break;
}
/*FALLTHROUGH*/
case CMD_TIMEOUT:
default:
/*
* The lun may still be running the command, so a lun reset
* should be attempted. If the lun reset fails or cannot be
* issued, than try a target reset. Lastly try a bus reset.
*/
if ((pkt->pkt_statistics &
(STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) == 0) {
int reset_retval = 0;
mutex_exit(SD_MUTEX(un));
if (un->un_f_allow_bus_device_reset == TRUE) {
if (un->un_f_lun_reset_enabled == TRUE) {
reset_retval =
scsi_reset(SD_ADDRESS(un),
RESET_LUN);
}
if (reset_retval == 0) {
reset_retval =
scsi_reset(SD_ADDRESS(un),
RESET_TARGET);
}
}
if (reset_retval == 0) {
(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
}
mutex_enter(SD_MUTEX(un));
}
break;
}
/* A device/bus reset has occurred; update the reservation status. */
if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
(STAT_BUS_RESET | STAT_DEV_RESET))) {
if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
un->un_resvd_status |=
(SD_LOST_RESERVE | SD_WANT_RESERVE);
SD_INFO(SD_LOG_IOCTL_MHD, un,
"sd_mhd_watch_incomplete: Lost Reservation\n");
}
}
/*
* The disk has been turned off; Update the device state.
*
* Note: Should we be offlining the disk here?
*/
if (pkt->pkt_state == STATE_GOT_BUS) {
SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
"Disk not responding to selection\n");
if (un->un_state != SD_STATE_OFFLINE) {
New_state(un, SD_STATE_OFFLINE);
}
} else if (be_chatty) {
/*
* suppress messages if they are all the same pkt reason;
* with TQ, many (up to 256) are returned with the same
* pkt_reason
*/
if (pkt->pkt_reason != un->un_last_pkt_reason) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_mhd_watch_incomplete: "
"SCSI transport failed: reason '%s'\n",
scsi_rname(pkt->pkt_reason));
}
}
un->un_last_pkt_reason = pkt->pkt_reason;
mutex_exit(SD_MUTEX(un));
}
/*
* Function: sd_sname()
*
* Description: This is a simple little routine to return a string containing
* a printable description of command status byte for use in
* logging.
*
* Arguments: status - pointer to a status byte
*
* Return Code: char * - string containing status description.
*/
static char *
sd_sname(uchar_t status)
{
switch (status & STATUS_MASK) {
case STATUS_GOOD:
return ("good status");
case STATUS_CHECK:
return ("check condition");
case STATUS_MET:
return ("condition met");
case STATUS_BUSY:
return ("busy");
case STATUS_INTERMEDIATE:
return ("intermediate");
case STATUS_INTERMEDIATE_MET:
return ("intermediate - condition met");
case STATUS_RESERVATION_CONFLICT:
return ("reservation_conflict");
case STATUS_TERMINATED:
return ("command terminated");
case STATUS_QFULL:
return ("queue full");
default:
return ("<unknown status>");
}
}
/*
* Function: sd_mhd_resvd_recover()
*
* Description: This function adds a reservation entry to the
* sd_resv_reclaim_request list and signals the reservation
* reclaim thread that there is work pending. If the reservation
* reclaim thread has not been previously created this function
* will kick it off.
*
* Arguments: arg - the device 'dev_t' is used for context to discriminate
* among multiple watches that share this callback function
*
* Context: This routine is called by timeout() and is run in interrupt
* context. It must not sleep or call other functions which may
* sleep.
*/
static void
sd_mhd_resvd_recover(void *arg)
{
dev_t dev = (dev_t)arg;
struct sd_lun *un;
struct sd_thr_request *sd_treq = NULL;
struct sd_thr_request *sd_cur = NULL;
struct sd_thr_request *sd_prev = NULL;
int already_there = 0;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return;
}
mutex_enter(SD_MUTEX(un));
un->un_resvd_timeid = NULL;
if (un->un_resvd_status & SD_WANT_RESERVE) {
/*
* There was a reset so don't issue the reserve, allow the
* sd_mhd_watch_cb callback function to notice this and
* reschedule the timeout for reservation.
*/
mutex_exit(SD_MUTEX(un));
return;
}
mutex_exit(SD_MUTEX(un));
/*
* Add this device to the sd_resv_reclaim_request list and the
* sd_resv_reclaim_thread should take care of the rest.
*
* Note: We can't sleep in this context so if the memory allocation
* fails allow the sd_mhd_watch_cb callback function to notice this and
* reschedule the timeout for reservation. (4378460)
*/
sd_treq = (struct sd_thr_request *)
kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
if (sd_treq == NULL) {
return;
}
sd_treq->sd_thr_req_next = NULL;
sd_treq->dev = dev;
mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
if (sd_tr.srq_thr_req_head == NULL) {
sd_tr.srq_thr_req_head = sd_treq;
} else {
sd_cur = sd_prev = sd_tr.srq_thr_req_head;
for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
if (sd_cur->dev == dev) {
/*
* already in Queue so don't log
* another request for the device
*/
already_there = 1;
break;
}
sd_prev = sd_cur;
}
if (!already_there) {
SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
"logging request for %lx\n", dev);
sd_prev->sd_thr_req_next = sd_treq;
} else {
kmem_free(sd_treq, sizeof (struct sd_thr_request));
}
}
/*
* Create a kernel thread to do the reservation reclaim and free up this
* thread. We cannot block this thread while we go away to do the
* reservation reclaim
*/
if (sd_tr.srq_resv_reclaim_thread == NULL)
sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
sd_resv_reclaim_thread, NULL,
0, &p0, TS_RUN, v.v_maxsyspri - 2);
/* Tell the reservation reclaim thread that it has work to do */
cv_signal(&sd_tr.srq_resv_reclaim_cv);
mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
}
/*
* Function: sd_resv_reclaim_thread()
*
* Description: This function implements the reservation reclaim operations
*
* Arguments: arg - the device 'dev_t' is used for context to discriminate
* among multiple watches that share this callback function
*/
static void
sd_resv_reclaim_thread()
{
struct sd_lun *un;
struct sd_thr_request *sd_mhreq;
/* Wait for work */
mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
if (sd_tr.srq_thr_req_head == NULL) {
cv_wait(&sd_tr.srq_resv_reclaim_cv,
&sd_tr.srq_resv_reclaim_mutex);
}
/* Loop while we have work */
while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
un = ddi_get_soft_state(sd_state,
SDUNIT(sd_tr.srq_thr_cur_req->dev));
if (un == NULL) {
/*
* softstate structure is NULL so just
* dequeue the request and continue
*/
sd_tr.srq_thr_req_head =
sd_tr.srq_thr_cur_req->sd_thr_req_next;
kmem_free(sd_tr.srq_thr_cur_req,
sizeof (struct sd_thr_request));
continue;
}
/* dequeue the request */
sd_mhreq = sd_tr.srq_thr_cur_req;
sd_tr.srq_thr_req_head =
sd_tr.srq_thr_cur_req->sd_thr_req_next;
mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
/*
* Reclaim reservation only if SD_RESERVE is still set. There
* may have been a call to MHIOCRELEASE before we got here.
*/
mutex_enter(SD_MUTEX(un));
if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
/*
* Note: The SD_LOST_RESERVE flag is cleared before
* reclaiming the reservation. If this is done after the
* call to sd_reserve_release a reservation loss in the
* window between pkt completion of reserve cmd and
* mutex_enter below may not be recognized
*/
un->un_resvd_status &= ~SD_LOST_RESERVE;
mutex_exit(SD_MUTEX(un));
if (sd_reserve_release(sd_mhreq->dev,
SD_RESERVE) == 0) {
mutex_enter(SD_MUTEX(un));
un->un_resvd_status |= SD_RESERVE;
mutex_exit(SD_MUTEX(un));
SD_INFO(SD_LOG_IOCTL_MHD, un,
"sd_resv_reclaim_thread: "
"Reservation Recovered\n");
} else {
mutex_enter(SD_MUTEX(un));
un->un_resvd_status |= SD_LOST_RESERVE;
mutex_exit(SD_MUTEX(un));
SD_INFO(SD_LOG_IOCTL_MHD, un,
"sd_resv_reclaim_thread: Failed "
"Reservation Recovery\n");
}
} else {
mutex_exit(SD_MUTEX(un));
}
mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
/*
* wakeup the destroy thread if anyone is waiting on
* us to complete.
*/
cv_signal(&sd_tr.srq_inprocess_cv);
SD_TRACE(SD_LOG_IOCTL_MHD, un,
"sd_resv_reclaim_thread: cv_signalling current request \n");
}
/*
* cleanup the sd_tr structure now that this thread will not exist
*/
ASSERT(sd_tr.srq_thr_req_head == NULL);
ASSERT(sd_tr.srq_thr_cur_req == NULL);
sd_tr.srq_resv_reclaim_thread = NULL;
mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
thread_exit();
}
/*
* Function: sd_rmv_resv_reclaim_req()
*
* Description: This function removes any pending reservation reclaim requests
* for the specified device.
*
* Arguments: dev - the device 'dev_t'
*/
static void
sd_rmv_resv_reclaim_req(dev_t dev)
{
struct sd_thr_request *sd_mhreq;
struct sd_thr_request *sd_prev;
/* Remove a reservation reclaim request from the list */
mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
/*
* We are attempting to reinstate reservation for
* this device. We wait for sd_reserve_release()
* to return before we return.
*/
cv_wait(&sd_tr.srq_inprocess_cv,
&sd_tr.srq_resv_reclaim_mutex);
} else {
sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
if (sd_mhreq && sd_mhreq->dev == dev) {
sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
return;
}
for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
if (sd_mhreq && sd_mhreq->dev == dev) {
break;
}
sd_prev = sd_mhreq;
}
if (sd_mhreq != NULL) {
sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
}
}
mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
}
/*
* Function: sd_mhd_reset_notify_cb()
*
* Description: This is a call back function for scsi_reset_notify. This
* function updates the softstate reserved status and logs the
* reset. The driver scsi watch facility callback function
* (sd_mhd_watch_cb) and reservation reclaim thread functionality
* will reclaim the reservation.
*
* Arguments: arg - driver soft state (unit) structure
*/
static void
sd_mhd_reset_notify_cb(caddr_t arg)
{
struct sd_lun *un = (struct sd_lun *)arg;
mutex_enter(SD_MUTEX(un));
if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
SD_INFO(SD_LOG_IOCTL_MHD, un,
"sd_mhd_reset_notify_cb: Lost Reservation\n");
}
mutex_exit(SD_MUTEX(un));
}
/*
* Function: sd_take_ownership()
*
* Description: This routine implements an algorithm to achieve a stable
* reservation on disks which don't implement priority reserve,
* and makes sure that other host lose re-reservation attempts.
* This algorithm contains of a loop that keeps issuing the RESERVE
* for some period of time (min_ownership_delay, default 6 seconds)
* During that loop, it looks to see if there has been a bus device
* reset or bus reset (both of which cause an existing reservation
* to be lost). If the reservation is lost issue RESERVE until a
* period of min_ownership_delay with no resets has gone by, or
* until max_ownership_delay has expired. This loop ensures that
* the host really did manage to reserve the device, in spite of
* resets. The looping for min_ownership_delay (default six
* seconds) is important to early generation clustering products,
* Solstice HA 1.x and Sun Cluster 2.x. Those products use an
* MHIOCENFAILFAST periodic timer of two seconds. By having
* MHIOCTKOWN issue Reserves in a loop for six seconds, and having
* MHIOCENFAILFAST poll every two seconds, the idea is that by the
* time the MHIOCTKOWN ioctl returns, the other host (if any) will
* have already noticed, via the MHIOCENFAILFAST polling, that it
* no longer "owns" the disk and will have panicked itself. Thus,
* the host issuing the MHIOCTKOWN is assured (with timing
* dependencies) that by the time it actually starts to use the
* disk for real work, the old owner is no longer accessing it.
*
* min_ownership_delay is the minimum amount of time for which the
* disk must be reserved continuously devoid of resets before the
* MHIOCTKOWN ioctl will return success.
*
* max_ownership_delay indicates the amount of time by which the
* take ownership should succeed or timeout with an error.
*
* Arguments: dev - the device 'dev_t'
* *p - struct containing timing info.
*
* Return Code: 0 for success or error code
*/
static int
sd_take_ownership(dev_t dev, struct mhioctkown *p)
{
struct sd_lun *un;
int rval;
int err;
int reservation_count = 0;
int min_ownership_delay = 6000000; /* in usec */
int max_ownership_delay = 30000000; /* in usec */
clock_t start_time; /* starting time of this algorithm */
clock_t end_time; /* time limit for giving up */
clock_t ownership_time; /* time limit for stable ownership */
clock_t current_time;
clock_t previous_current_time;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
/*
* Attempt a device reservation. A priority reservation is requested.
*/
if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
!= SD_SUCCESS) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_take_ownership: return(1)=%d\n", rval);
return (rval);
}
/* Update the softstate reserved status to indicate the reservation */
mutex_enter(SD_MUTEX(un));
un->un_resvd_status |= SD_RESERVE;
un->un_resvd_status &=
~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
mutex_exit(SD_MUTEX(un));
if (p != NULL) {
if (p->min_ownership_delay != 0) {
min_ownership_delay = p->min_ownership_delay * 1000;
}
if (p->max_ownership_delay != 0) {
max_ownership_delay = p->max_ownership_delay * 1000;
}
}
SD_INFO(SD_LOG_IOCTL_MHD, un,
"sd_take_ownership: min, max delays: %d, %d\n",
min_ownership_delay, max_ownership_delay);
start_time = ddi_get_lbolt();
current_time = start_time;
ownership_time = current_time + drv_usectohz(min_ownership_delay);
end_time = start_time + drv_usectohz(max_ownership_delay);
while (current_time - end_time < 0) {
delay(drv_usectohz(500000));
if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
mutex_enter(SD_MUTEX(un));
rval = (un->un_resvd_status &
SD_RESERVATION_CONFLICT) ? EACCES : EIO;
mutex_exit(SD_MUTEX(un));
break;
}
}
previous_current_time = current_time;
current_time = ddi_get_lbolt();
mutex_enter(SD_MUTEX(un));
if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
ownership_time = ddi_get_lbolt() +
drv_usectohz(min_ownership_delay);
reservation_count = 0;
} else {
reservation_count++;
}
un->un_resvd_status |= SD_RESERVE;
un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
mutex_exit(SD_MUTEX(un));
SD_INFO(SD_LOG_IOCTL_MHD, un,
"sd_take_ownership: ticks for loop iteration=%ld, "
"reservation=%s\n", (current_time - previous_current_time),
reservation_count ? "ok" : "reclaimed");
if (current_time - ownership_time >= 0 &&
reservation_count >= 4) {
rval = 0; /* Achieved a stable ownership */
break;
}
if (current_time - end_time >= 0) {
rval = EACCES; /* No ownership in max possible time */
break;
}
}
SD_TRACE(SD_LOG_IOCTL_MHD, un,
"sd_take_ownership: return(2)=%d\n", rval);
return (rval);
}
/*
* Function: sd_reserve_release()
*
* Description: This function builds and sends scsi RESERVE, RELEASE, and
* PRIORITY RESERVE commands based on a user specified command type
*
* Arguments: dev - the device 'dev_t'
* cmd - user specified command type; one of SD_PRIORITY_RESERVE,
* SD_RESERVE, SD_RELEASE
*
* Return Code: 0 or Error Code
*/
static int
sd_reserve_release(dev_t dev, int cmd)
{
struct uscsi_cmd *com = NULL;
struct sd_lun *un = NULL;
char cdb[CDB_GROUP0];
int rval;
ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
(cmd == SD_PRIORITY_RESERVE));
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
/* instantiate and initialize the command and cdb */
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
bzero(cdb, CDB_GROUP0);
com->uscsi_flags = USCSI_SILENT;
com->uscsi_timeout = un->un_reserve_release_time;
com->uscsi_cdblen = CDB_GROUP0;
com->uscsi_cdb = cdb;
if (cmd == SD_RELEASE) {
cdb[0] = SCMD_RELEASE;
} else {
cdb[0] = SCMD_RESERVE;
}
/* Send the command. */
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
/*
* "break" a reservation that is held by another host, by issuing a
* reset if priority reserve is desired, and we could not get the
* device.
*/
if ((cmd == SD_PRIORITY_RESERVE) &&
(rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
/*
* First try to reset the LUN. If we cannot, then try a target
* reset, followed by a bus reset if the target reset fails.
*/
int reset_retval = 0;
if (un->un_f_lun_reset_enabled == TRUE) {
reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
}
if (reset_retval == 0) {
/* The LUN reset either failed or was not issued */
reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
}
if ((reset_retval == 0) &&
(scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
rval = EIO;
kmem_free(com, sizeof (*com));
return (rval);
}
bzero(com, sizeof (struct uscsi_cmd));
com->uscsi_flags = USCSI_SILENT;
com->uscsi_cdb = cdb;
com->uscsi_cdblen = CDB_GROUP0;
com->uscsi_timeout = 5;
/*
* Reissue the last reserve command, this time without request
* sense. Assume that it is just a regular reserve command.
*/
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
}
/* Return an error if still getting a reservation conflict. */
if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
rval = EACCES;
}
kmem_free(com, sizeof (*com));
return (rval);
}
#define SD_NDUMP_RETRIES 12
/*
* System Crash Dump routine
*/
static int
sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
{
int instance;
int partition;
int i;
int err;
struct sd_lun *un;
struct scsi_pkt *wr_pktp;
struct buf *wr_bp;
struct buf wr_buf;
daddr_t tgt_byte_offset; /* rmw - byte offset for target */
daddr_t tgt_blkno; /* rmw - blkno for target */
size_t tgt_byte_count; /* rmw - # of bytes to xfer */
size_t tgt_nblk; /* rmw - # of tgt blks to xfer */
size_t io_start_offset;
int doing_rmw = FALSE;
int rval;
ssize_t dma_resid;
daddr_t oblkno;
diskaddr_t nblks = 0;
diskaddr_t start_block;
instance = SDUNIT(dev);
if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
!SD_IS_VALID_LABEL(un) || ISCD(un)) {
return (ENXIO);
}
_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
partition = SDPART(dev);
SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
if (!(NOT_DEVBSIZE(un))) {
int secmask = 0;
int blknomask = 0;
blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
secmask = un->un_tgt_blocksize - 1;
if (blkno & blknomask) {
SD_TRACE(SD_LOG_DUMP, un,
"sddump: dump start block not modulo %d\n",
un->un_tgt_blocksize);
return (EINVAL);
}
if ((nblk * DEV_BSIZE) & secmask) {
SD_TRACE(SD_LOG_DUMP, un,
"sddump: dump length not modulo %d\n",
un->un_tgt_blocksize);
return (EINVAL);
}
}
/* Validate blocks to dump at against partition size. */
(void) cmlb_partinfo(un->un_cmlbhandle, partition,
&nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT);
if (NOT_DEVBSIZE(un)) {
if ((blkno + nblk) > nblks) {
SD_TRACE(SD_LOG_DUMP, un,
"sddump: dump range larger than partition: "
"blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
blkno, nblk, nblks);
return (EINVAL);
}
} else {
if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) +
(nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) {
SD_TRACE(SD_LOG_DUMP, un,
"sddump: dump range larger than partition: "
"blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
blkno, nblk, nblks);
return (EINVAL);
}
}
mutex_enter(&un->un_pm_mutex);
if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
struct scsi_pkt *start_pktp;
mutex_exit(&un->un_pm_mutex);
/*
* use pm framework to power on HBA 1st
*/
(void) pm_raise_power(SD_DEVINFO(un), 0,
SD_PM_STATE_ACTIVE(un));
/*
* Dump no long uses sdpower to power on a device, it's
* in-line here so it can be done in polled mode.
*/
SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
if (start_pktp == NULL) {
/* We were not given a SCSI packet, fail. */
return (EIO);
}
bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
start_pktp->pkt_cdbp[4] = SD_TARGET_START;
start_pktp->pkt_flags = FLAG_NOINTR;
mutex_enter(SD_MUTEX(un));
SD_FILL_SCSI1_LUN(un, start_pktp);
mutex_exit(SD_MUTEX(un));
/*
* Scsi_poll returns 0 (success) if the command completes and
* the status block is STATUS_GOOD.
*/
if (sd_scsi_poll(un, start_pktp) != 0) {
scsi_destroy_pkt(start_pktp);
return (EIO);
}
scsi_destroy_pkt(start_pktp);
(void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un),
SD_PM_STATE_CHANGE);
} else {
mutex_exit(&un->un_pm_mutex);
}
mutex_enter(SD_MUTEX(un));
un->un_throttle = 0;
/*
* The first time through, reset the specific target device.
* However, when cpr calls sddump we know that sd is in a
* a good state so no bus reset is required.
* Clear sense data via Request Sense cmd.
* In sddump we don't care about allow_bus_device_reset anymore
*/
if ((un->un_state != SD_STATE_SUSPENDED) &&
(un->un_state != SD_STATE_DUMPING)) {
New_state(un, SD_STATE_DUMPING);
if (un->un_f_is_fibre == FALSE) {
mutex_exit(SD_MUTEX(un));
/*
* Attempt a bus reset for parallel scsi.
*
* Note: A bus reset is required because on some host
* systems (i.e. E420R) a bus device reset is
* insufficient to reset the state of the target.
*
* Note: Don't issue the reset for fibre-channel,
* because this tends to hang the bus (loop) for
* too long while everyone is logging out and in
* and the deadman timer for dumping will fire
* before the dump is complete.
*/
if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
mutex_enter(SD_MUTEX(un));
Restore_state(un);
mutex_exit(SD_MUTEX(un));
return (EIO);
}
/* Delay to give the device some recovery time. */
drv_usecwait(10000);
if (sd_send_polled_RQS(un) == SD_FAILURE) {
SD_INFO(SD_LOG_DUMP, un,
"sddump: sd_send_polled_RQS failed\n");
}
mutex_enter(SD_MUTEX(un));
}
}
/*
* Convert the partition-relative block number to a
* disk physical block number.
*/
if (NOT_DEVBSIZE(un)) {
blkno += start_block;
} else {
blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE);
blkno += start_block;
}
SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
/*
* Check if the device has a non-512 block size.
*/
wr_bp = NULL;
if (NOT_DEVBSIZE(un)) {
tgt_byte_offset = blkno * un->un_sys_blocksize;
tgt_byte_count = nblk * un->un_sys_blocksize;
if ((tgt_byte_offset % un->un_tgt_blocksize) ||
(tgt_byte_count % un->un_tgt_blocksize)) {
doing_rmw = TRUE;
/*
* Calculate the block number and number of block
* in terms of the media block size.
*/
tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
tgt_nblk =
((tgt_byte_offset + tgt_byte_count +
(un->un_tgt_blocksize - 1)) /
un->un_tgt_blocksize) - tgt_blkno;
/*
* Invoke the routine which is going to do read part
* of read-modify-write.
* Note that this routine returns a pointer to
* a valid bp in wr_bp.
*/
err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
&wr_bp);
if (err) {
mutex_exit(SD_MUTEX(un));
return (err);
}
/*
* Offset is being calculated as -
* (original block # * system block size) -
* (new block # * target block size)
*/
io_start_offset =
((uint64_t)(blkno * un->un_sys_blocksize)) -
((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
ASSERT(io_start_offset < un->un_tgt_blocksize);
/*
* Do the modify portion of read modify write.
*/
bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
(size_t)nblk * un->un_sys_blocksize);
} else {
doing_rmw = FALSE;
tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
}
/* Convert blkno and nblk to target blocks */
blkno = tgt_blkno;
nblk = tgt_nblk;
} else {
wr_bp = &wr_buf;
bzero(wr_bp, sizeof (struct buf));
wr_bp->b_flags = B_BUSY;
wr_bp->b_un.b_addr = addr;
wr_bp->b_bcount = nblk << DEV_BSHIFT;
wr_bp->b_resid = 0;
}
mutex_exit(SD_MUTEX(un));
/*
* Obtain a SCSI packet for the write command.
* It should be safe to call the allocator here without
* worrying about being locked for DVMA mapping because
* the address we're passed is already a DVMA mapping
*
* We are also not going to worry about semaphore ownership
* in the dump buffer. Dumping is single threaded at present.
*/
wr_pktp = NULL;
dma_resid = wr_bp->b_bcount;
oblkno = blkno;
if (!(NOT_DEVBSIZE(un))) {
nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE);
}
while (dma_resid != 0) {
for (i = 0; i < SD_NDUMP_RETRIES; i++) {
wr_bp->b_flags &= ~B_ERROR;
if (un->un_partial_dma_supported == 1) {
blkno = oblkno +
((wr_bp->b_bcount - dma_resid) /
un->un_tgt_blocksize);
nblk = dma_resid / un->un_tgt_blocksize;
if (wr_pktp) {
/*
* Partial DMA transfers after initial transfer
*/
rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
blkno, nblk);
} else {
/* Initial transfer */
rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
un->un_pkt_flags, NULL_FUNC, NULL,
blkno, nblk);
}
} else {
rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
0, NULL_FUNC, NULL, blkno, nblk);
}
if (rval == 0) {
/* We were given a SCSI packet, continue. */
break;
}
if (i == 0) {
if (wr_bp->b_flags & B_ERROR) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"no resources for dumping; "
"error code: 0x%x, retrying",
geterror(wr_bp));
} else {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"no resources for dumping; retrying");
}
} else if (i != (SD_NDUMP_RETRIES - 1)) {
if (wr_bp->b_flags & B_ERROR) {
scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
"no resources for dumping; error code: "
"0x%x, retrying\n", geterror(wr_bp));
}
} else {
if (wr_bp->b_flags & B_ERROR) {
scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
"no resources for dumping; "
"error code: 0x%x, retries failed, "
"giving up.\n", geterror(wr_bp));
} else {
scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
"no resources for dumping; "
"retries failed, giving up.\n");
}
mutex_enter(SD_MUTEX(un));
Restore_state(un);
if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
mutex_exit(SD_MUTEX(un));
scsi_free_consistent_buf(wr_bp);
} else {
mutex_exit(SD_MUTEX(un));
}
return (EIO);
}
drv_usecwait(10000);
}
if (un->un_partial_dma_supported == 1) {
/*
* save the resid from PARTIAL_DMA
*/
dma_resid = wr_pktp->pkt_resid;
if (dma_resid != 0)
nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
wr_pktp->pkt_resid = 0;
} else {
dma_resid = 0;
}
/* SunBug 1222170 */
wr_pktp->pkt_flags = FLAG_NOINTR;
err = EIO;
for (i = 0; i < SD_NDUMP_RETRIES; i++) {
/*
* Scsi_poll returns 0 (success) if the command completes and
* the status block is STATUS_GOOD. We should only check
* errors if this condition is not true. Even then we should
* send our own request sense packet only if we have a check
* condition and auto request sense has not been performed by
* the hba.
*/
SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
if ((sd_scsi_poll(un, wr_pktp) == 0) &&
(wr_pktp->pkt_resid == 0)) {
err = SD_SUCCESS;
break;
}
/*
* Check CMD_DEV_GONE 1st, give up if device is gone.
*/
if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"Error while dumping state...Device is gone\n");
break;
}
if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
SD_INFO(SD_LOG_DUMP, un,
"sddump: write failed with CHECK, try # %d\n", i);
if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
(void) sd_send_polled_RQS(un);
}
continue;
}
if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
int reset_retval = 0;
SD_INFO(SD_LOG_DUMP, un,
"sddump: write failed with BUSY, try # %d\n", i);
if (un->un_f_lun_reset_enabled == TRUE) {
reset_retval = scsi_reset(SD_ADDRESS(un),
RESET_LUN);
}
if (reset_retval == 0) {
(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
}
(void) sd_send_polled_RQS(un);
} else {
SD_INFO(SD_LOG_DUMP, un,
"sddump: write failed with 0x%x, try # %d\n",
SD_GET_PKT_STATUS(wr_pktp), i);
mutex_enter(SD_MUTEX(un));
sd_reset_target(un, wr_pktp);
mutex_exit(SD_MUTEX(un));
}
/*
* If we are not getting anywhere with lun/target resets,
* let's reset the bus.
*/
if (i == SD_NDUMP_RETRIES / 2) {
(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
(void) sd_send_polled_RQS(un);
}
}
}
scsi_destroy_pkt(wr_pktp);
mutex_enter(SD_MUTEX(un));
if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
mutex_exit(SD_MUTEX(un));
scsi_free_consistent_buf(wr_bp);
} else {
mutex_exit(SD_MUTEX(un));
}
SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
return (err);
}
/*
* Function: sd_scsi_poll()
*
* Description: This is a wrapper for the scsi_poll call.
*
* Arguments: sd_lun - The unit structure
* scsi_pkt - The scsi packet being sent to the device.
*
* Return Code: 0 - Command completed successfully with good status
* -1 - Command failed. This could indicate a check condition
* or other status value requiring recovery action.
*
* NOTE: This code is only called off sddump().
*/
static int
sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
{
int status;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ASSERT(pktp != NULL);
status = SD_SUCCESS;
if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
pktp->pkt_flags |= un->un_tagflags;
pktp->pkt_flags &= ~FLAG_NODISCON;
}
status = sd_ddi_scsi_poll(pktp);
/*
* Scsi_poll returns 0 (success) if the command completes and the
* status block is STATUS_GOOD. We should only check errors if this
* condition is not true. Even then we should send our own request
* sense packet only if we have a check condition and auto
* request sense has not been performed by the hba.
* Don't get RQS data if pkt_reason is CMD_DEV_GONE.
*/
if ((status != SD_SUCCESS) &&
(SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
(pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
(pktp->pkt_reason != CMD_DEV_GONE))
(void) sd_send_polled_RQS(un);
return (status);
}
/*
* Function: sd_send_polled_RQS()
*
* Description: This sends the request sense command to a device.
*
* Arguments: sd_lun - The unit structure
*
* Return Code: 0 - Command completed successfully with good status
* -1 - Command failed.
*
*/
static int
sd_send_polled_RQS(struct sd_lun *un)
{
int ret_val;
struct scsi_pkt *rqs_pktp;
struct buf *rqs_bp;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
ret_val = SD_SUCCESS;
rqs_pktp = un->un_rqs_pktp;
rqs_bp = un->un_rqs_bp;
mutex_enter(SD_MUTEX(un));
if (un->un_sense_isbusy) {
ret_val = SD_FAILURE;
mutex_exit(SD_MUTEX(un));
return (ret_val);
}
/*
* If the request sense buffer (and packet) is not in use,
* let's set the un_sense_isbusy and send our packet
*/
un->un_sense_isbusy = 1;
rqs_pktp->pkt_resid = 0;
rqs_pktp->pkt_reason = 0;
rqs_pktp->pkt_flags |= FLAG_NOINTR;
bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
mutex_exit(SD_MUTEX(un));
SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
" 0x%p\n", rqs_bp->b_un.b_addr);
/*
* Can't send this to sd_scsi_poll, we wrap ourselves around the
* axle - it has a call into us!
*/
if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
SD_INFO(SD_LOG_COMMON, un,
"sd_send_polled_RQS: RQS failed\n");
}
SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
(uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
mutex_enter(SD_MUTEX(un));
un->un_sense_isbusy = 0;
mutex_exit(SD_MUTEX(un));
return (ret_val);
}
/*
* Defines needed for localized version of the scsi_poll routine.
*/
#define CSEC 10000 /* usecs */
#define SEC_TO_CSEC (1000000 / CSEC)
/*
* Function: sd_ddi_scsi_poll()
*
* Description: Localized version of the scsi_poll routine. The purpose is to
* send a scsi_pkt to a device as a polled command. This version
* is to ensure more robust handling of transport errors.
* Specifically this routine cures not ready, coming ready
* transition for power up and reset.
*
* Arguments: scsi_pkt - The scsi_pkt being sent to a device
*
* Return Code: 0 - Command completed successfully with good status
* -1 - Command failed.
*
* NOTE: This code is almost identical to scsi_poll, however before 6668774 can
* be fixed (removing this code), we need to determine how to handle the
* KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump().
*
* NOTE: This code is only called off sddump().
*/
static int
sd_ddi_scsi_poll(struct scsi_pkt *pkt)
{
int rval = -1;
int savef;
long savet;
void (*savec)();
int timeout;
int busy_count;
int poll_delay;
int rc;
uint8_t *sensep;
struct scsi_arq_status *arqstat;
extern int do_polled_io;
ASSERT(pkt->pkt_scbp);
/*
* save old flags..
*/
savef = pkt->pkt_flags;
savec = pkt->pkt_comp;
savet = pkt->pkt_time;
pkt->pkt_flags |= FLAG_NOINTR;
/*
* XXX there is nothing in the SCSA spec that states that we should not
* do a callback for polled cmds; however, removing this will break sd
* and probably other target drivers
*/
pkt->pkt_comp = NULL;
/*
* we don't like a polled command without timeout.
* 60 seconds seems long enough.
*/
if (pkt->pkt_time == 0)
pkt->pkt_time = SCSI_POLL_TIMEOUT;
/*
* Send polled cmd.
*
* We do some error recovery for various errors. Tran_busy,
* queue full, and non-dispatched commands are retried every 10 msec.
* as they are typically transient failures. Busy status and Not
* Ready are retried every second as this status takes a while to
* change.
*/
timeout = pkt->pkt_time * SEC_TO_CSEC;
for (busy_count = 0; busy_count < timeout; busy_count++) {
/*
* Initialize pkt status variables.
*/
*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
if (rc != TRAN_BUSY) {
/* Transport failed - give up. */
break;
} else {
/* Transport busy - try again. */
poll_delay = 1 * CSEC; /* 10 msec. */
}
} else {
/*
* Transport accepted - check pkt status.
*/
rc = (*pkt->pkt_scbp) & STATUS_MASK;
if ((pkt->pkt_reason == CMD_CMPLT) &&
(rc == STATUS_CHECK) &&
(pkt->pkt_state & STATE_ARQ_DONE)) {
arqstat =
(struct scsi_arq_status *)(pkt->pkt_scbp);
sensep = (uint8_t *)&arqstat->sts_sensedata;
} else {
sensep = NULL;
}
if ((pkt->pkt_reason == CMD_CMPLT) &&
(rc == STATUS_GOOD)) {
/* No error - we're done */
rval = 0;
break;
} else if (pkt->pkt_reason == CMD_DEV_GONE) {
/* Lost connection - give up */
break;
} else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
(pkt->pkt_state == 0)) {
/* Pkt not dispatched - try again. */
poll_delay = 1 * CSEC; /* 10 msec. */
} else if ((pkt->pkt_reason == CMD_CMPLT) &&
(rc == STATUS_QFULL)) {
/* Queue full - try again. */
poll_delay = 1 * CSEC; /* 10 msec. */
} else if ((pkt->pkt_reason == CMD_CMPLT) &&
(rc == STATUS_BUSY)) {
/* Busy - try again. */
poll_delay = 100 * CSEC; /* 1 sec. */
busy_count += (SEC_TO_CSEC - 1);
} else if ((sensep != NULL) &&
(scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) {
/*
* Unit Attention - try again.
* Pretend it took 1 sec.
* NOTE: 'continue' avoids poll_delay
*/
busy_count += (SEC_TO_CSEC - 1);
continue;
} else if ((sensep != NULL) &&
(scsi_sense_key(sensep) == KEY_NOT_READY) &&
(scsi_sense_asc(sensep) == 0x04) &&
(scsi_sense_ascq(sensep) == 0x01)) {
/*
* Not ready -> ready - try again.
* 04h/01h: LUN IS IN PROCESS OF BECOMING READY
* ...same as STATUS_BUSY
*/
poll_delay = 100 * CSEC; /* 1 sec. */
busy_count += (SEC_TO_CSEC - 1);
} else {
/* BAD status - give up. */
break;
}
}
if (((curthread->t_flag & T_INTR_THREAD) == 0) &&
!do_polled_io) {
delay(drv_usectohz(poll_delay));
} else {
/* we busy wait during cpr_dump or interrupt threads */
drv_usecwait(poll_delay);
}
}
pkt->pkt_flags = savef;
pkt->pkt_comp = savec;
pkt->pkt_time = savet;
/* return on error */
if (rval)
return (rval);
/*
* This is not a performance critical code path.
*
* As an accommodation for scsi_poll callers, to avoid ddi_dma_sync()
* issues associated with looking at DMA memory prior to
* scsi_pkt_destroy(), we scsi_sync_pkt() prior to return.
*/
scsi_sync_pkt(pkt);
return (0);
}
/*
* Function: sd_persistent_reservation_in_read_keys
*
* Description: This routine is the driver entry point for handling CD-ROM
* multi-host persistent reservation requests (MHIOCGRP_INKEYS)
* by sending the SCSI-3 PRIN commands to the device.
* Processes the read keys command response by copying the
* reservation key information into the user provided buffer.
* Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
*
* Arguments: un - Pointer to soft state struct for the target.
* usrp - user provided pointer to multihost Persistent In Read
* Keys structure (mhioc_inkeys_t)
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: 0 - Success
* EACCES
* ENOTSUP
* errno return code from sd_send_scsi_cmd()
*
* Context: Can sleep. Does not return until command is completed.
*/
static int
sd_persistent_reservation_in_read_keys(struct sd_lun *un,
mhioc_inkeys_t *usrp, int flag)
{
#ifdef _MULTI_DATAMODEL
struct mhioc_key_list32 li32;
#endif
sd_prin_readkeys_t *in;
mhioc_inkeys_t *ptr;
mhioc_key_list_t li;
uchar_t *data_bufp = NULL;
int data_len = 0;
int rval = 0;
size_t copysz = 0;
sd_ssc_t *ssc;
if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
return (EINVAL);
}
bzero(&li, sizeof (mhioc_key_list_t));
ssc = sd_ssc_init(un);
/*
* Get the listsize from user
*/
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32:
copysz = sizeof (struct mhioc_key_list32);
if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_keys: "
"failed ddi_copyin: mhioc_key_list32_t\n");
rval = EFAULT;
goto done;
}
li.listsize = li32.listsize;
li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
break;
case DDI_MODEL_NONE:
copysz = sizeof (mhioc_key_list_t);
if (ddi_copyin(ptr->li, &li, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_keys: "
"failed ddi_copyin: mhioc_key_list_t\n");
rval = EFAULT;
goto done;
}
break;
}
#else /* ! _MULTI_DATAMODEL */
copysz = sizeof (mhioc_key_list_t);
if (ddi_copyin(ptr->li, &li, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_keys: "
"failed ddi_copyin: mhioc_key_list_t\n");
rval = EFAULT;
goto done;
}
#endif
data_len = li.listsize * MHIOC_RESV_KEY_SIZE;
data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
data_bufp = kmem_zalloc(data_len, KM_SLEEP);
rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS,
data_len, data_bufp);
if (rval != 0) {
if (rval == EIO)
sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
else
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
goto done;
}
in = (sd_prin_readkeys_t *)data_bufp;
ptr->generation = BE_32(in->generation);
li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
/*
* Return the min(listsize, listlen) keys
*/
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32:
li32.listlen = li.listlen;
if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_keys: "
"failed ddi_copyout: mhioc_key_list32_t\n");
rval = EFAULT;
goto done;
}
break;
case DDI_MODEL_NONE:
if (ddi_copyout(&li, ptr->li, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_keys: "
"failed ddi_copyout: mhioc_key_list_t\n");
rval = EFAULT;
goto done;
}
break;
}
#else /* ! _MULTI_DATAMODEL */
if (ddi_copyout(&li, ptr->li, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_keys: "
"failed ddi_copyout: mhioc_key_list_t\n");
rval = EFAULT;
goto done;
}
#endif /* _MULTI_DATAMODEL */
copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
li.listsize * MHIOC_RESV_KEY_SIZE);
if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_keys: "
"failed ddi_copyout: keylist\n");
rval = EFAULT;
}
done:
sd_ssc_fini(ssc);
kmem_free(data_bufp, data_len);
return (rval);
}
/*
* Function: sd_persistent_reservation_in_read_resv
*
* Description: This routine is the driver entry point for handling CD-ROM
* multi-host persistent reservation requests (MHIOCGRP_INRESV)
* by sending the SCSI-3 PRIN commands to the device.
* Process the read persistent reservations command response by
* copying the reservation information into the user provided
* buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
*
* Arguments: un - Pointer to soft state struct for the target.
* usrp - user provided pointer to multihost Persistent In Read
* Keys structure (mhioc_inkeys_t)
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: 0 - Success
* EACCES
* ENOTSUP
* errno return code from sd_send_scsi_cmd()
*
* Context: Can sleep. Does not return until command is completed.
*/
static int
sd_persistent_reservation_in_read_resv(struct sd_lun *un,
mhioc_inresvs_t *usrp, int flag)
{
#ifdef _MULTI_DATAMODEL
struct mhioc_resv_desc_list32 resvlist32;
#endif
sd_prin_readresv_t *in;
mhioc_inresvs_t *ptr;
sd_readresv_desc_t *readresv_ptr;
mhioc_resv_desc_list_t resvlist;
mhioc_resv_desc_t resvdesc;
uchar_t *data_bufp = NULL;
int data_len;
int rval = 0;
int i;
size_t copysz = 0;
mhioc_resv_desc_t *bufp;
sd_ssc_t *ssc;
if ((ptr = usrp) == NULL) {
return (EINVAL);
}
ssc = sd_ssc_init(un);
/*
* Get the listsize from user
*/
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32:
copysz = sizeof (struct mhioc_resv_desc_list32);
if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_resv: "
"failed ddi_copyin: mhioc_resv_desc_list_t\n");
rval = EFAULT;
goto done;
}
resvlist.listsize = resvlist32.listsize;
resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
break;
case DDI_MODEL_NONE:
copysz = sizeof (mhioc_resv_desc_list_t);
if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_resv: "
"failed ddi_copyin: mhioc_resv_desc_list_t\n");
rval = EFAULT;
goto done;
}
break;
}
#else /* ! _MULTI_DATAMODEL */
copysz = sizeof (mhioc_resv_desc_list_t);
if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_resv: "
"failed ddi_copyin: mhioc_resv_desc_list_t\n");
rval = EFAULT;
goto done;
}
#endif /* ! _MULTI_DATAMODEL */
data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN;
data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
data_bufp = kmem_zalloc(data_len, KM_SLEEP);
rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV,
data_len, data_bufp);
if (rval != 0) {
if (rval == EIO)
sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
else
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
goto done;
}
in = (sd_prin_readresv_t *)data_bufp;
ptr->generation = BE_32(in->generation);
resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
/*
* Return the min(listsize, listlen( keys
*/
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32:
resvlist32.listlen = resvlist.listlen;
if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_resv: "
"failed ddi_copyout: mhioc_resv_desc_list_t\n");
rval = EFAULT;
goto done;
}
break;
case DDI_MODEL_NONE:
if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_resv: "
"failed ddi_copyout: mhioc_resv_desc_list_t\n");
rval = EFAULT;
goto done;
}
break;
}
#else /* ! _MULTI_DATAMODEL */
if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_resv: "
"failed ddi_copyout: mhioc_resv_desc_list_t\n");
rval = EFAULT;
goto done;
}
#endif /* ! _MULTI_DATAMODEL */
readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
bufp = resvlist.list;
copysz = sizeof (mhioc_resv_desc_t);
for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
i++, readresv_ptr++, bufp++) {
bcopy(&readresv_ptr->resvkey, &resvdesc.key,
MHIOC_RESV_KEY_SIZE);
resvdesc.type = readresv_ptr->type;
resvdesc.scope = readresv_ptr->scope;
resvdesc.scope_specific_addr =
BE_32(readresv_ptr->scope_specific_addr);
if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
SD_ERROR(SD_LOG_IOCTL_MHD, un,
"sd_persistent_reservation_in_read_resv: "
"failed ddi_copyout: resvlist\n");
rval = EFAULT;
goto done;
}
}
done:
sd_ssc_fini(ssc);
/* only if data_bufp is allocated, we need to free it */
if (data_bufp) {
kmem_free(data_bufp, data_len);
}
return (rval);
}
/*
* Function: sr_change_blkmode()
*
* Description: This routine is the driver entry point for handling CD-ROM
* block mode ioctl requests. Support for returning and changing
* the current block size in use by the device is implemented. The
* LBA size is changed via a MODE SELECT Block Descriptor.
*
* This routine issues a mode sense with an allocation length of
* 12 bytes for the mode page header and a single block descriptor.
*
* Arguments: dev - the device 'dev_t'
* cmd - the request type; one of CDROMGBLKMODE (get) or
* CDROMSBLKMODE (set)
* data - current block size or requested block size
* flag - this argument is a pass through to ddi_copyxxx() directly
* from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EINVAL if invalid arguments are provided
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EIO if invalid mode sense block descriptor length
*
*/
static int
sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
{
struct sd_lun *un = NULL;
struct mode_header *sense_mhp, *select_mhp;
struct block_descriptor *sense_desc, *select_desc;
int current_bsize;
int rval = EINVAL;
uchar_t *sense = NULL;
uchar_t *select = NULL;
sd_ssc_t *ssc;
ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
/*
* The block length is changed via the Mode Select block descriptor, the
* "Read/Write Error Recovery" mode page (0x1) contents are not actually
* required as part of this routine. Therefore the mode sense allocation
* length is specified to be the length of a mode page header and a
* block descriptor.
*/
sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
ssc = sd_ssc_init(un);
rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (rval != 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_change_blkmode: Mode Sense Failed\n");
kmem_free(sense, BUFLEN_CHG_BLK_MODE);
return (rval);
}
/* Check the block descriptor len to handle only 1 block descriptor */
sense_mhp = (struct mode_header *)sense;
if ((sense_mhp->bdesc_length == 0) ||
(sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_change_blkmode: Mode Sense returned invalid block"
" descriptor length\n");
kmem_free(sense, BUFLEN_CHG_BLK_MODE);
return (EIO);
}
sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
current_bsize = ((sense_desc->blksize_hi << 16) |
(sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
/* Process command */
switch (cmd) {
case CDROMGBLKMODE:
/* Return the block size obtained during the mode sense */
if (ddi_copyout(¤t_bsize, (void *)data,
sizeof (int), flag) != 0)
rval = EFAULT;
break;
case CDROMSBLKMODE:
/* Validate the requested block size */
switch (data) {
case CDROM_BLK_512:
case CDROM_BLK_1024:
case CDROM_BLK_2048:
case CDROM_BLK_2056:
case CDROM_BLK_2336:
case CDROM_BLK_2340:
case CDROM_BLK_2352:
case CDROM_BLK_2368:
case CDROM_BLK_2448:
case CDROM_BLK_2646:
case CDROM_BLK_2647:
break;
default:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_change_blkmode: "
"Block Size '%ld' Not Supported\n", data);
kmem_free(sense, BUFLEN_CHG_BLK_MODE);
return (EINVAL);
}
/*
* The current block size matches the requested block size so
* there is no need to send the mode select to change the size
*/
if (current_bsize == data) {
break;
}
/* Build the select data for the requested block size */
select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
select_mhp = (struct mode_header *)select;
select_desc =
(struct block_descriptor *)(select + MODE_HEADER_LENGTH);
/*
* The LBA size is changed via the block descriptor, so the
* descriptor is built according to the user data
*/
select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16);
select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
select_desc->blksize_lo = (char)((data) & 0x000000ff);
/* Send the mode select for the requested block size */
ssc = sd_ssc_init(un);
rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (rval != 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_change_blkmode: Mode Select Failed\n");
/*
* The mode select failed for the requested block size,
* so reset the data for the original block size and
* send it to the target. The error is indicated by the
* return value for the failed mode select.
*/
select_desc->blksize_hi = sense_desc->blksize_hi;
select_desc->blksize_mid = sense_desc->blksize_mid;
select_desc->blksize_lo = sense_desc->blksize_lo;
ssc = sd_ssc_init(un);
(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
SD_PATH_STANDARD);
sd_ssc_fini(ssc);
} else {
ASSERT(!mutex_owned(SD_MUTEX(un)));
mutex_enter(SD_MUTEX(un));
sd_update_block_info(un, (uint32_t)data, 0);
mutex_exit(SD_MUTEX(un));
}
break;
default:
/* should not reach here, but check anyway */
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_change_blkmode: Command '%x' Not Supported\n", cmd);
rval = EINVAL;
break;
}
if (select) {
kmem_free(select, BUFLEN_CHG_BLK_MODE);
}
if (sense) {
kmem_free(sense, BUFLEN_CHG_BLK_MODE);
}
return (rval);
}
/*
* Note: The following sr_change_speed() and sr_atapi_change_speed() routines
* implement driver support for getting and setting the CD speed. The command
* set used will be based on the device type. If the device has not been
* identified as MMC the Toshiba vendor specific mode page will be used. If
* the device is MMC but does not support the Real Time Streaming feature
* the SET CD SPEED command will be used to set speed and mode page 0x2A will
* be used to read the speed.
*/
/*
* Function: sr_change_speed()
*
* Description: This routine is the driver entry point for handling CD-ROM
* drive speed ioctl requests for devices supporting the Toshiba
* vendor specific drive speed mode page. Support for returning
* and changing the current drive speed in use by the device is
* implemented.
*
* Arguments: dev - the device 'dev_t'
* cmd - the request type; one of CDROMGDRVSPEED (get) or
* CDROMSDRVSPEED (set)
* data - current drive speed or requested drive speed
* flag - this argument is a pass through to ddi_copyxxx() directly
* from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EINVAL if invalid arguments are provided
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EIO if invalid mode sense block descriptor length
*/
static int
sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
{
struct sd_lun *un = NULL;
struct mode_header *sense_mhp, *select_mhp;
struct mode_speed *sense_page, *select_page;
int current_speed;
int rval = EINVAL;
int bd_len;
uchar_t *sense = NULL;
uchar_t *select = NULL;
sd_ssc_t *ssc;
ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
/*
* Note: The drive speed is being modified here according to a Toshiba
* vendor specific mode page (0x31).
*/
sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
ssc = sd_ssc_init(un);
rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (rval != 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_change_speed: Mode Sense Failed\n");
kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
return (rval);
}
sense_mhp = (struct mode_header *)sense;
/* Check the block descriptor len to handle only 1 block descriptor */
bd_len = sense_mhp->bdesc_length;
if (bd_len > MODE_BLK_DESC_LENGTH) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_change_speed: Mode Sense returned invalid block "
"descriptor length\n");
kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
return (EIO);
}
sense_page = (struct mode_speed *)
(sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
current_speed = sense_page->speed;
/* Process command */
switch (cmd) {
case CDROMGDRVSPEED:
/* Return the drive speed obtained during the mode sense */
if (current_speed == 0x2) {
current_speed = CDROM_TWELVE_SPEED;
}
if (ddi_copyout(¤t_speed, (void *)data,
sizeof (int), flag) != 0) {
rval = EFAULT;
}
break;
case CDROMSDRVSPEED:
/* Validate the requested drive speed */
switch ((uchar_t)data) {
case CDROM_TWELVE_SPEED:
data = 0x2;
/*FALLTHROUGH*/
case CDROM_NORMAL_SPEED:
case CDROM_DOUBLE_SPEED:
case CDROM_QUAD_SPEED:
case CDROM_MAXIMUM_SPEED:
break;
default:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_change_speed: "
"Drive Speed '%d' Not Supported\n", (uchar_t)data);
kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
return (EINVAL);
}
/*
* The current drive speed matches the requested drive speed so
* there is no need to send the mode select to change the speed
*/
if (current_speed == data) {
break;
}
/* Build the select data for the requested drive speed */
select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
select_mhp = (struct mode_header *)select;
select_mhp->bdesc_length = 0;
select_page =
(struct mode_speed *)(select + MODE_HEADER_LENGTH);
select_page =
(struct mode_speed *)(select + MODE_HEADER_LENGTH);
select_page->mode_page.code = CDROM_MODE_SPEED;
select_page->mode_page.length = 2;
select_page->speed = (uchar_t)data;
/* Send the mode select for the requested block size */
ssc = sd_ssc_init(un);
rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (rval != 0) {
/*
* The mode select failed for the requested drive speed,
* so reset the data for the original drive speed and
* send it to the target. The error is indicated by the
* return value for the failed mode select.
*/
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_drive_speed: Mode Select Failed\n");
select_page->speed = sense_page->speed;
ssc = sd_ssc_init(un);
(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
sd_ssc_fini(ssc);
}
break;
default:
/* should not reach here, but check anyway */
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_change_speed: Command '%x' Not Supported\n", cmd);
rval = EINVAL;
break;
}
if (select) {
kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
}
if (sense) {
kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
}
return (rval);
}
/*
* Function: sr_atapi_change_speed()
*
* Description: This routine is the driver entry point for handling CD-ROM
* drive speed ioctl requests for MMC devices that do not support
* the Real Time Streaming feature (0x107).
*
* Note: This routine will use the SET SPEED command which may not
* be supported by all devices.
*
* Arguments: dev- the device 'dev_t'
* cmd- the request type; one of CDROMGDRVSPEED (get) or
* CDROMSDRVSPEED (set)
* data- current drive speed or requested drive speed
* flag- this argument is a pass through to ddi_copyxxx() directly
* from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EINVAL if invalid arguments are provided
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EIO if invalid mode sense block descriptor length
*/
static int
sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
{
struct sd_lun *un;
struct uscsi_cmd *com = NULL;
struct mode_header_grp2 *sense_mhp;
uchar_t *sense_page;
uchar_t *sense = NULL;
char cdb[CDB_GROUP5];
int bd_len;
int current_speed = 0;
int max_speed = 0;
int rval;
sd_ssc_t *ssc;
ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
ssc = sd_ssc_init(un);
rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (rval != 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_atapi_change_speed: Mode Sense Failed\n");
kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
return (rval);
}
/* Check the block descriptor len to handle only 1 block descriptor */
sense_mhp = (struct mode_header_grp2 *)sense;
bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
if (bd_len > MODE_BLK_DESC_LENGTH) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_atapi_change_speed: Mode Sense returned invalid "
"block descriptor length\n");
kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
return (EIO);
}
/* Calculate the current and maximum drive speeds */
sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
current_speed = (sense_page[14] << 8) | sense_page[15];
max_speed = (sense_page[8] << 8) | sense_page[9];
/* Process the command */
switch (cmd) {
case CDROMGDRVSPEED:
current_speed /= SD_SPEED_1X;
if (ddi_copyout(¤t_speed, (void *)data,
sizeof (int), flag) != 0)
rval = EFAULT;
break;
case CDROMSDRVSPEED:
/* Convert the speed code to KB/sec */
switch ((uchar_t)data) {
case CDROM_NORMAL_SPEED:
current_speed = SD_SPEED_1X;
break;
case CDROM_DOUBLE_SPEED:
current_speed = 2 * SD_SPEED_1X;
break;
case CDROM_QUAD_SPEED:
current_speed = 4 * SD_SPEED_1X;
break;
case CDROM_TWELVE_SPEED:
current_speed = 12 * SD_SPEED_1X;
break;
case CDROM_MAXIMUM_SPEED:
current_speed = 0xffff;
break;
default:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_atapi_change_speed: invalid drive speed %d\n",
(uchar_t)data);
kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
return (EINVAL);
}
/* Check the request against the drive's max speed. */
if (current_speed != 0xffff) {
if (current_speed > max_speed) {
kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
return (EINVAL);
}
}
/*
* Build and send the SET SPEED command
*
* Note: The SET SPEED (0xBB) command used in this routine is
* obsolete per the SCSI MMC spec but still supported in the
* MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
* therefore the command is still implemented in this routine.
*/
bzero(cdb, sizeof (cdb));
cdb[0] = (char)SCMD_SET_CDROM_SPEED;
cdb[2] = (uchar_t)(current_speed >> 8);
cdb[3] = (uchar_t)current_speed;
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
com->uscsi_cdb = (caddr_t)cdb;
com->uscsi_cdblen = CDB_GROUP5;
com->uscsi_bufaddr = NULL;
com->uscsi_buflen = 0;
com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD);
break;
default:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
rval = EINVAL;
}
if (sense) {
kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
}
if (com) {
kmem_free(com, sizeof (*com));
}
return (rval);
}
/*
* Function: sr_pause_resume()
*
* Description: This routine is the driver entry point for handling CD-ROM
* pause/resume ioctl requests. This only affects the audio play
* operation.
*
* Arguments: dev - the device 'dev_t'
* cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
* for setting the resume bit of the cdb.
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EINVAL if invalid mode specified
*
*/
static int
sr_pause_resume(dev_t dev, int cmd)
{
struct sd_lun *un;
struct uscsi_cmd *com;
char cdb[CDB_GROUP1];
int rval;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
bzero(cdb, CDB_GROUP1);
cdb[0] = SCMD_PAUSE_RESUME;
switch (cmd) {
case CDROMRESUME:
cdb[8] = 1;
break;
case CDROMPAUSE:
cdb[8] = 0;
break;
default:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
" Command '%x' Not Supported\n", cmd);
rval = EINVAL;
goto done;
}
com->uscsi_cdb = cdb;
com->uscsi_cdblen = CDB_GROUP1;
com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
done:
kmem_free(com, sizeof (*com));
return (rval);
}
/*
* Function: sr_play_msf()
*
* Description: This routine is the driver entry point for handling CD-ROM
* ioctl requests to output the audio signals at the specified
* starting address and continue the audio play until the specified
* ending address (CDROMPLAYMSF) The address is in Minute Second
* Frame (MSF) format.
*
* Arguments: dev - the device 'dev_t'
* data - pointer to user provided audio msf structure,
* specifying start/end addresses.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
*/
static int
sr_play_msf(dev_t dev, caddr_t data, int flag)
{
struct sd_lun *un;
struct uscsi_cmd *com;
struct cdrom_msf msf_struct;
struct cdrom_msf *msf = &msf_struct;
char cdb[CDB_GROUP1];
int rval;
if (data == NULL) {
return (EINVAL);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
return (EFAULT);
}
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
bzero(cdb, CDB_GROUP1);
cdb[0] = SCMD_PLAYAUDIO_MSF;
if (un->un_f_cfg_playmsf_bcd == TRUE) {
cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
} else {
cdb[3] = msf->cdmsf_min0;
cdb[4] = msf->cdmsf_sec0;
cdb[5] = msf->cdmsf_frame0;
cdb[6] = msf->cdmsf_min1;
cdb[7] = msf->cdmsf_sec1;
cdb[8] = msf->cdmsf_frame1;
}
com->uscsi_cdb = cdb;
com->uscsi_cdblen = CDB_GROUP1;
com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
kmem_free(com, sizeof (*com));
return (rval);
}
/*
* Function: sr_play_trkind()
*
* Description: This routine is the driver entry point for handling CD-ROM
* ioctl requests to output the audio signals at the specified
* starting address and continue the audio play until the specified
* ending address (CDROMPLAYTRKIND). The address is in Track Index
* format.
*
* Arguments: dev - the device 'dev_t'
* data - pointer to user provided audio track/index structure,
* specifying start/end addresses.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
*/
static int
sr_play_trkind(dev_t dev, caddr_t data, int flag)
{
struct cdrom_ti ti_struct;
struct cdrom_ti *ti = &ti_struct;
struct uscsi_cmd *com = NULL;
char cdb[CDB_GROUP1];
int rval;
if (data == NULL) {
return (EINVAL);
}
if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
return (EFAULT);
}
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
bzero(cdb, CDB_GROUP1);
cdb[0] = SCMD_PLAYAUDIO_TI;
cdb[4] = ti->cdti_trk0;
cdb[5] = ti->cdti_ind0;
cdb[7] = ti->cdti_trk1;
cdb[8] = ti->cdti_ind1;
com->uscsi_cdb = cdb;
com->uscsi_cdblen = CDB_GROUP1;
com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
kmem_free(com, sizeof (*com));
return (rval);
}
/*
* Function: sr_read_all_subcodes()
*
* Description: This routine is the driver entry point for handling CD-ROM
* ioctl requests to return raw subcode data while the target is
* playing audio (CDROMSUBCODE).
*
* Arguments: dev - the device 'dev_t'
* data - pointer to user provided cdrom subcode structure,
* specifying the transfer length and address.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
*/
static int
sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
{
struct sd_lun *un = NULL;
struct uscsi_cmd *com = NULL;
struct cdrom_subcode *subcode = NULL;
int rval;
size_t buflen;
char cdb[CDB_GROUP5];
#ifdef _MULTI_DATAMODEL
/* To support ILP32 applications in an LP64 world */
struct cdrom_subcode32 cdrom_subcode32;
struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32;
#endif
if (data == NULL) {
return (EINVAL);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32:
if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_read_all_subcodes: ddi_copyin Failed\n");
kmem_free(subcode, sizeof (struct cdrom_subcode));
return (EFAULT);
}
/* Convert the ILP32 uscsi data from the application to LP64 */
cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
break;
case DDI_MODEL_NONE:
if (ddi_copyin(data, subcode,
sizeof (struct cdrom_subcode), flag)) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_read_all_subcodes: ddi_copyin Failed\n");
kmem_free(subcode, sizeof (struct cdrom_subcode));
return (EFAULT);
}
break;
}
#else /* ! _MULTI_DATAMODEL */
if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_read_all_subcodes: ddi_copyin Failed\n");
kmem_free(subcode, sizeof (struct cdrom_subcode));
return (EFAULT);
}
#endif /* _MULTI_DATAMODEL */
/*
* Since MMC-2 expects max 3 bytes for length, check if the
* length input is greater than 3 bytes
*/
if ((subcode->cdsc_length & 0xFF000000) != 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_read_all_subcodes: "
"cdrom transfer length too large: %d (limit %d)\n",
subcode->cdsc_length, 0xFFFFFF);
kmem_free(subcode, sizeof (struct cdrom_subcode));
return (EINVAL);
}
buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
bzero(cdb, CDB_GROUP5);
if (un->un_f_mmc_cap == TRUE) {
cdb[0] = (char)SCMD_READ_CD;
cdb[2] = (char)0xff;
cdb[3] = (char)0xff;
cdb[4] = (char)0xff;
cdb[5] = (char)0xff;
cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
cdb[10] = 1;
} else {
/*
* Note: A vendor specific command (0xDF) is being used here to
* request a read of all subcodes.
*/
cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
}
com->uscsi_cdb = cdb;
com->uscsi_cdblen = CDB_GROUP5;
com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
com->uscsi_buflen = buflen;
com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
SD_PATH_STANDARD);
kmem_free(subcode, sizeof (struct cdrom_subcode));
kmem_free(com, sizeof (*com));
return (rval);
}
/*
* Function: sr_read_subchannel()
*
* Description: This routine is the driver entry point for handling CD-ROM
* ioctl requests to return the Q sub-channel data of the CD
* current position block. (CDROMSUBCHNL) The data includes the
* track number, index number, absolute CD-ROM address (LBA or MSF
* format per the user) , track relative CD-ROM address (LBA or MSF
* format per the user), control data and audio status.
*
* Arguments: dev - the device 'dev_t'
* data - pointer to user provided cdrom sub-channel structure
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
*/
static int
sr_read_subchannel(dev_t dev, caddr_t data, int flag)
{
struct sd_lun *un;
struct uscsi_cmd *com;
struct cdrom_subchnl subchanel;
struct cdrom_subchnl *subchnl = &subchanel;
char cdb[CDB_GROUP1];
caddr_t buffer;
int rval;
if (data == NULL) {
return (EINVAL);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
(un->un_state == SD_STATE_OFFLINE)) {
return (ENXIO);
}
if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
return (EFAULT);
}
buffer = kmem_zalloc((size_t)16, KM_SLEEP);
bzero(cdb, CDB_GROUP1);
cdb[0] = SCMD_READ_SUBCHANNEL;
/* Set the MSF bit based on the user requested address format */
cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
/*
* Set the Q bit in byte 2 to indicate that Q sub-channel data be
* returned
*/
cdb[2] = 0x40;
/*
* Set byte 3 to specify the return data format. A value of 0x01
* indicates that the CD-ROM current position should be returned.
*/
cdb[3] = 0x01;
cdb[8] = 0x10;
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
com->uscsi_cdb = cdb;
com->uscsi_cdblen = CDB_GROUP1;
com->uscsi_bufaddr = buffer;
com->uscsi_buflen = 16;
com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
if (rval != 0) {
kmem_free(buffer, 16);
kmem_free(com, sizeof (*com));
return (rval);
}
/* Process the returned Q sub-channel data */
subchnl->cdsc_audiostatus = buffer[1];
subchnl->cdsc_adr = (buffer[5] & 0xF0) >> 4;
subchnl->cdsc_ctrl = (buffer[5] & 0x0F);
subchnl->cdsc_trk = buffer[6];
subchnl->cdsc_ind = buffer[7];
if (subchnl->cdsc_format & CDROM_LBA) {
subchnl->cdsc_absaddr.lba =
((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
subchnl->cdsc_reladdr.lba =
((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
} else if (un->un_f_cfg_readsub_bcd == TRUE) {
subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]);
subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]);
} else {
subchnl->cdsc_absaddr.msf.minute = buffer[9];
subchnl->cdsc_absaddr.msf.second = buffer[10];
subchnl->cdsc_absaddr.msf.frame = buffer[11];
subchnl->cdsc_reladdr.msf.minute = buffer[13];
subchnl->cdsc_reladdr.msf.second = buffer[14];
subchnl->cdsc_reladdr.msf.frame = buffer[15];
}
kmem_free(buffer, 16);
kmem_free(com, sizeof (*com));
if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
!= 0) {
return (EFAULT);
}
return (rval);
}
/*
* Function: sr_read_tocentry()
*
* Description: This routine is the driver entry point for handling CD-ROM
* ioctl requests to read from the Table of Contents (TOC)
* (CDROMREADTOCENTRY). This routine provides the ADR and CTRL
* fields, the starting address (LBA or MSF format per the user)
* and the data mode if the user specified track is a data track.
*
* Note: The READ HEADER (0x44) command used in this routine is
* obsolete per the SCSI MMC spec but still supported in the
* MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
* therefore the command is still implemented in this routine.
*
* Arguments: dev - the device 'dev_t'
* data - pointer to user provided toc entry structure,
* specifying the track # and the address format
* (LBA or MSF).
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
*/
static int
sr_read_tocentry(dev_t dev, caddr_t data, int flag)
{
struct sd_lun *un = NULL;
struct uscsi_cmd *com;
struct cdrom_tocentry toc_entry;
struct cdrom_tocentry *entry = &toc_entry;
caddr_t buffer;
int rval;
char cdb[CDB_GROUP1];
if (data == NULL) {
return (EINVAL);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
(un->un_state == SD_STATE_OFFLINE)) {
return (ENXIO);
}
if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
return (EFAULT);
}
/* Validate the requested track and address format */
if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
return (EINVAL);
}
if (entry->cdte_track == 0) {
return (EINVAL);
}
buffer = kmem_zalloc((size_t)12, KM_SLEEP);
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
bzero(cdb, CDB_GROUP1);
cdb[0] = SCMD_READ_TOC;
/* Set the MSF bit based on the user requested address format */
cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
cdb[6] = BYTE_TO_BCD(entry->cdte_track);
} else {
cdb[6] = entry->cdte_track;
}
/*
* Bytes 7 & 8 are the 12 byte allocation length for a single entry.
* (4 byte TOC response header + 8 byte track descriptor)
*/
cdb[8] = 12;
com->uscsi_cdb = cdb;
com->uscsi_cdblen = CDB_GROUP1;
com->uscsi_bufaddr = buffer;
com->uscsi_buflen = 0x0C;
com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
if (rval != 0) {
kmem_free(buffer, 12);
kmem_free(com, sizeof (*com));
return (rval);
}
/* Process the toc entry */
entry->cdte_adr = (buffer[5] & 0xF0) >> 4;
entry->cdte_ctrl = (buffer[5] & 0x0F);
if (entry->cdte_format & CDROM_LBA) {
entry->cdte_addr.lba =
((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
} else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]);
entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]);
entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]);
/*
* Send a READ TOC command using the LBA address format to get
* the LBA for the track requested so it can be used in the
* READ HEADER request
*
* Note: The MSF bit of the READ HEADER command specifies the
* output format. The block address specified in that command
* must be in LBA format.
*/
cdb[1] = 0;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
if (rval != 0) {
kmem_free(buffer, 12);
kmem_free(com, sizeof (*com));
return (rval);
}
} else {
entry->cdte_addr.msf.minute = buffer[9];
entry->cdte_addr.msf.second = buffer[10];
entry->cdte_addr.msf.frame = buffer[11];
/*
* Send a READ TOC command using the LBA address format to get
* the LBA for the track requested so it can be used in the
* READ HEADER request
*
* Note: The MSF bit of the READ HEADER command specifies the
* output format. The block address specified in that command
* must be in LBA format.
*/
cdb[1] = 0;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
if (rval != 0) {
kmem_free(buffer, 12);
kmem_free(com, sizeof (*com));
return (rval);
}
}
/*
* Build and send the READ HEADER command to determine the data mode of
* the user specified track.
*/
if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
(entry->cdte_track != CDROM_LEADOUT)) {
bzero(cdb, CDB_GROUP1);
cdb[0] = SCMD_READ_HEADER;
cdb[2] = buffer[8];
cdb[3] = buffer[9];
cdb[4] = buffer[10];
cdb[5] = buffer[11];
cdb[8] = 0x08;
com->uscsi_buflen = 0x08;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
if (rval == 0) {
entry->cdte_datamode = buffer[0];
} else {
/*
* READ HEADER command failed, since this is
* obsoleted in one spec, its better to return
* -1 for an invlid track so that we can still
* receive the rest of the TOC data.
*/
entry->cdte_datamode = (uchar_t)-1;
}
} else {
entry->cdte_datamode = (uchar_t)-1;
}
kmem_free(buffer, 12);
kmem_free(com, sizeof (*com));
if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
return (EFAULT);
return (rval);
}
/*
* Function: sr_read_tochdr()
*
* Description: This routine is the driver entry point for handling CD-ROM
* ioctl requests to read the Table of Contents (TOC) header
* (CDROMREADTOHDR). The TOC header consists of the disk starting
* and ending track numbers
*
* Arguments: dev - the device 'dev_t'
* data - pointer to user provided toc header structure,
* specifying the starting and ending track numbers.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
*/
static int
sr_read_tochdr(dev_t dev, caddr_t data, int flag)
{
struct sd_lun *un;
struct uscsi_cmd *com;
struct cdrom_tochdr toc_header;
struct cdrom_tochdr *hdr = &toc_header;
char cdb[CDB_GROUP1];
int rval;
caddr_t buffer;
if (data == NULL) {
return (EINVAL);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
(un->un_state == SD_STATE_OFFLINE)) {
return (ENXIO);
}
buffer = kmem_zalloc(4, KM_SLEEP);
bzero(cdb, CDB_GROUP1);
cdb[0] = SCMD_READ_TOC;
/*
* Specifying a track number of 0x00 in the READ TOC command indicates
* that the TOC header should be returned
*/
cdb[6] = 0x00;
/*
* Bytes 7 & 8 are the 4 byte allocation length for TOC header.
* (2 byte data len + 1 byte starting track # + 1 byte ending track #)
*/
cdb[8] = 0x04;
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
com->uscsi_cdb = cdb;
com->uscsi_cdblen = CDB_GROUP1;
com->uscsi_bufaddr = buffer;
com->uscsi_buflen = 0x04;
com->uscsi_timeout = 300;
com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
} else {
hdr->cdth_trk0 = buffer[2];
hdr->cdth_trk1 = buffer[3];
}
kmem_free(buffer, 4);
kmem_free(com, sizeof (*com));
if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
return (EFAULT);
}
return (rval);
}
/*
* Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
* sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
* handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
* digital audio and extended architecture digital audio. These modes are
* defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
* MMC specs.
*
* In addition to support for the various data formats these routines also
* include support for devices that implement only the direct access READ
* commands (0x08, 0x28), devices that implement the READ_CD commands
* (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
* READ CDXA commands (0xD8, 0xDB)
*/
/*
* Function: sr_read_mode1()
*
* Description: This routine is the driver entry point for handling CD-ROM
* ioctl read mode1 requests (CDROMREADMODE1).
*
* Arguments: dev - the device 'dev_t'
* data - pointer to user provided cd read structure specifying
* the lba buffer address and length.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
*/
static int
sr_read_mode1(dev_t dev, caddr_t data, int flag)
{
struct sd_lun *un;
struct cdrom_read mode1_struct;
struct cdrom_read *mode1 = &mode1_struct;
int rval;
sd_ssc_t *ssc;
#ifdef _MULTI_DATAMODEL
/* To support ILP32 applications in an LP64 world */
struct cdrom_read32 cdrom_read32;
struct cdrom_read32 *cdrd32 = &cdrom_read32;
#endif /* _MULTI_DATAMODEL */
if (data == NULL) {
return (EINVAL);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
(un->un_state == SD_STATE_OFFLINE)) {
return (ENXIO);
}
SD_TRACE(SD_LOG_ATTACH_DETACH, un,
"sd_read_mode1: entry: un:0x%p\n", un);
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32:
if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
return (EFAULT);
}
/* Convert the ILP32 uscsi data from the application to LP64 */
cdrom_read32tocdrom_read(cdrd32, mode1);
break;
case DDI_MODEL_NONE:
if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
return (EFAULT);
}
}
#else /* ! _MULTI_DATAMODEL */
if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
return (EFAULT);
}
#endif /* _MULTI_DATAMODEL */
ssc = sd_ssc_init(un);
rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr,
mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
sd_ssc_fini(ssc);
SD_TRACE(SD_LOG_ATTACH_DETACH, un,
"sd_read_mode1: exit: un:0x%p\n", un);
return (rval);
}
/*
* Function: sr_read_cd_mode2()
*
* Description: This routine is the driver entry point for handling CD-ROM
* ioctl read mode2 requests (CDROMREADMODE2) for devices that
* support the READ CD (0xBE) command or the 1st generation
* READ CD (0xD4) command.
*
* Arguments: dev - the device 'dev_t'
* data - pointer to user provided cd read structure specifying
* the lba buffer address and length.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
*/
static int
sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
{
struct sd_lun *un;
struct uscsi_cmd *com;
struct cdrom_read mode2_struct;
struct cdrom_read *mode2 = &mode2_struct;
uchar_t cdb[CDB_GROUP5];
int nblocks;
int rval;
#ifdef _MULTI_DATAMODEL
/* To support ILP32 applications in an LP64 world */
struct cdrom_read32 cdrom_read32;
struct cdrom_read32 *cdrd32 = &cdrom_read32;
#endif /* _MULTI_DATAMODEL */
if (data == NULL) {
return (EINVAL);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
(un->un_state == SD_STATE_OFFLINE)) {
return (ENXIO);
}
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32:
if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
return (EFAULT);
}
/* Convert the ILP32 uscsi data from the application to LP64 */
cdrom_read32tocdrom_read(cdrd32, mode2);
break;
case DDI_MODEL_NONE:
if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
return (EFAULT);
}
break;
}
#else /* ! _MULTI_DATAMODEL */
if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
return (EFAULT);
}
#endif /* _MULTI_DATAMODEL */
bzero(cdb, sizeof (cdb));
if (un->un_f_cfg_read_cd_xd4 == TRUE) {
/* Read command supported by 1st generation atapi drives */
cdb[0] = SCMD_READ_CDD4;
} else {
/* Universal CD Access Command */
cdb[0] = SCMD_READ_CD;
}
/*
* Set expected sector type to: 2336s byte, Mode 2 Yellow Book
*/
cdb[1] = CDROM_SECTOR_TYPE_MODE2;
/* set the start address */
cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
/* set the transfer length */
nblocks = mode2->cdread_buflen / 2336;
cdb[6] = (uchar_t)(nblocks >> 16);
cdb[7] = (uchar_t)(nblocks >> 8);
cdb[8] = (uchar_t)nblocks;
/* set the filter bits */
cdb[9] = CDROM_READ_CD_USERDATA;
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
com->uscsi_cdb = (caddr_t)cdb;
com->uscsi_cdblen = sizeof (cdb);
com->uscsi_bufaddr = mode2->cdread_bufaddr;
com->uscsi_buflen = mode2->cdread_buflen;
com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
SD_PATH_STANDARD);
kmem_free(com, sizeof (*com));
return (rval);
}
/*
* Function: sr_read_mode2()
*
* Description: This routine is the driver entry point for handling CD-ROM
* ioctl read mode2 requests (CDROMREADMODE2) for devices that
* do not support the READ CD (0xBE) command.
*
* Arguments: dev - the device 'dev_t'
* data - pointer to user provided cd read structure specifying
* the lba buffer address and length.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
* EIO if fail to reset block size
* EAGAIN if commands are in progress in the driver
*/
static int
sr_read_mode2(dev_t dev, caddr_t data, int flag)
{
struct sd_lun *un;
struct cdrom_read mode2_struct;
struct cdrom_read *mode2 = &mode2_struct;
int rval;
uint32_t restore_blksize;
struct uscsi_cmd *com;
uchar_t cdb[CDB_GROUP0];
int nblocks;
#ifdef _MULTI_DATAMODEL
/* To support ILP32 applications in an LP64 world */
struct cdrom_read32 cdrom_read32;
struct cdrom_read32 *cdrd32 = &cdrom_read32;
#endif /* _MULTI_DATAMODEL */
if (data == NULL) {
return (EINVAL);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
(un->un_state == SD_STATE_OFFLINE)) {
return (ENXIO);
}
/*
* Because this routine will update the device and driver block size
* being used we want to make sure there are no commands in progress.
* If commands are in progress the user will have to try again.
*
* We check for 1 instead of 0 because we increment un_ncmds_in_driver
* in sdioctl to protect commands from sdioctl through to the top of
* sd_uscsi_strategy. See sdioctl for details.
*/
mutex_enter(SD_MUTEX(un));
if (un->un_ncmds_in_driver != 1) {
mutex_exit(SD_MUTEX(un));
return (EAGAIN);
}
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_ATTACH_DETACH, un,
"sd_read_mode2: entry: un:0x%p\n", un);
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32:
if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
return (EFAULT);
}
/* Convert the ILP32 uscsi data from the application to LP64 */
cdrom_read32tocdrom_read(cdrd32, mode2);
break;
case DDI_MODEL_NONE:
if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
return (EFAULT);
}
break;
}
#else /* ! _MULTI_DATAMODEL */
if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
return (EFAULT);
}
#endif /* _MULTI_DATAMODEL */
/* Store the current target block size for restoration later */
restore_blksize = un->un_tgt_blocksize;
/* Change the device and soft state target block size to 2336 */
if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
rval = EIO;
goto done;
}
bzero(cdb, sizeof (cdb));
/* set READ operation */
cdb[0] = SCMD_READ;
/* adjust lba for 2kbyte blocks from 512 byte blocks */
mode2->cdread_lba >>= 2;
/* set the start address */
cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
/* set the transfer length */
nblocks = mode2->cdread_buflen / 2336;
cdb[4] = (uchar_t)nblocks & 0xFF;
/* build command */
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
com->uscsi_cdb = (caddr_t)cdb;
com->uscsi_cdblen = sizeof (cdb);
com->uscsi_bufaddr = mode2->cdread_bufaddr;
com->uscsi_buflen = mode2->cdread_buflen;
com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ;
/*
* Issue SCSI command with user space address for read buffer.
*
* This sends the command through main channel in the driver.
*
* Since this is accessed via an IOCTL call, we go through the
* standard path, so that if the device was powered down, then
* it would be 'awakened' to handle the command.
*/
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
SD_PATH_STANDARD);
kmem_free(com, sizeof (*com));
/* Restore the device and soft state target block size */
if (sr_sector_mode(dev, restore_blksize) != 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"can't do switch back to mode 1\n");
/*
* If sd_send_scsi_READ succeeded we still need to report
* an error because we failed to reset the block size
*/
if (rval == 0) {
rval = EIO;
}
}
done:
SD_TRACE(SD_LOG_ATTACH_DETACH, un,
"sd_read_mode2: exit: un:0x%p\n", un);
return (rval);
}
/*
* Function: sr_sector_mode()
*
* Description: This utility function is used by sr_read_mode2 to set the target
* block size based on the user specified size. This is a legacy
* implementation based upon a vendor specific mode page
*
* Arguments: dev - the device 'dev_t'
* data - flag indicating if block size is being set to 2336 or
* 512.
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
*/
static int
sr_sector_mode(dev_t dev, uint32_t blksize)
{
struct sd_lun *un;
uchar_t *sense;
uchar_t *select;
int rval;
sd_ssc_t *ssc;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
(un->un_state == SD_STATE_OFFLINE)) {
return (ENXIO);
}
sense = kmem_zalloc(20, KM_SLEEP);
/* Note: This is a vendor specific mode page (0x81) */
ssc = sd_ssc_init(un);
rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81,
SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (rval != 0) {
SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
"sr_sector_mode: Mode Sense failed\n");
kmem_free(sense, 20);
return (rval);
}
select = kmem_zalloc(20, KM_SLEEP);
select[3] = 0x08;
select[10] = ((blksize >> 8) & 0xff);
select[11] = (blksize & 0xff);
select[12] = 0x01;
select[13] = 0x06;
select[14] = sense[14];
select[15] = sense[15];
if (blksize == SD_MODE2_BLKSIZE) {
select[14] |= 0x01;
}
ssc = sd_ssc_init(un);
rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20,
SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (rval != 0) {
SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
"sr_sector_mode: Mode Select failed\n");
} else {
/*
* Only update the softstate block size if we successfully
* changed the device block mode.
*/
mutex_enter(SD_MUTEX(un));
sd_update_block_info(un, blksize, 0);
mutex_exit(SD_MUTEX(un));
}
kmem_free(sense, 20);
kmem_free(select, 20);
return (rval);
}
/*
* Function: sr_read_cdda()
*
* Description: This routine is the driver entry point for handling CD-ROM
* ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
* the target supports CDDA these requests are handled via a vendor
* specific command (0xD8) If the target does not support CDDA
* these requests are handled via the READ CD command (0xBE).
*
* Arguments: dev - the device 'dev_t'
* data - pointer to user provided CD-DA structure specifying
* the track starting address, transfer length, and
* subcode options.
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if invalid arguments are provided
* ENOTTY
*/
static int
sr_read_cdda(dev_t dev, caddr_t data, int flag)
{
struct sd_lun *un;
struct uscsi_cmd *com;
struct cdrom_cdda *cdda;
int rval;
size_t buflen;
char cdb[CDB_GROUP5];
#ifdef _MULTI_DATAMODEL
/* To support ILP32 applications in an LP64 world */
struct cdrom_cdda32 cdrom_cdda32;
struct cdrom_cdda32 *cdda32 = &cdrom_cdda32;
#endif /* _MULTI_DATAMODEL */
if (data == NULL) {
return (EINVAL);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32:
if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_read_cdda: ddi_copyin Failed\n");
kmem_free(cdda, sizeof (struct cdrom_cdda));
return (EFAULT);
}
/* Convert the ILP32 uscsi data from the application to LP64 */
cdrom_cdda32tocdrom_cdda(cdda32, cdda);
break;
case DDI_MODEL_NONE:
if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_read_cdda: ddi_copyin Failed\n");
kmem_free(cdda, sizeof (struct cdrom_cdda));
return (EFAULT);
}
break;
}
#else /* ! _MULTI_DATAMODEL */
if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_read_cdda: ddi_copyin Failed\n");
kmem_free(cdda, sizeof (struct cdrom_cdda));
return (EFAULT);
}
#endif /* _MULTI_DATAMODEL */
/*
* Since MMC-2 expects max 3 bytes for length, check if the
* length input is greater than 3 bytes
*/
if ((cdda->cdda_length & 0xFF000000) != 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
"cdrom transfer length too large: %d (limit %d)\n",
cdda->cdda_length, 0xFFFFFF);
kmem_free(cdda, sizeof (struct cdrom_cdda));
return (EINVAL);
}
switch (cdda->cdda_subcode) {
case CDROM_DA_NO_SUBCODE:
buflen = CDROM_BLK_2352 * cdda->cdda_length;
break;
case CDROM_DA_SUBQ:
buflen = CDROM_BLK_2368 * cdda->cdda_length;
break;
case CDROM_DA_ALL_SUBCODE:
buflen = CDROM_BLK_2448 * cdda->cdda_length;
break;
case CDROM_DA_SUBCODE_ONLY:
buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
break;
default:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_read_cdda: Subcode '0x%x' Not Supported\n",
cdda->cdda_subcode);
kmem_free(cdda, sizeof (struct cdrom_cdda));
return (EINVAL);
}
/* Build and send the command */
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
bzero(cdb, CDB_GROUP5);
if (un->un_f_cfg_cdda == TRUE) {
cdb[0] = (char)SCMD_READ_CD;
cdb[1] = 0x04;
cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
cdb[8] = ((cdda->cdda_length) & 0x000000ff);
cdb[9] = 0x10;
switch (cdda->cdda_subcode) {
case CDROM_DA_NO_SUBCODE :
cdb[10] = 0x0;
break;
case CDROM_DA_SUBQ :
cdb[10] = 0x2;
break;
case CDROM_DA_ALL_SUBCODE :
cdb[10] = 0x1;
break;
case CDROM_DA_SUBCODE_ONLY :
/* FALLTHROUGH */
default :
kmem_free(cdda, sizeof (struct cdrom_cdda));
kmem_free(com, sizeof (*com));
return (ENOTTY);
}
} else {
cdb[0] = (char)SCMD_READ_CDDA;
cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
cdb[9] = ((cdda->cdda_length) & 0x000000ff);
cdb[10] = cdda->cdda_subcode;
}
com->uscsi_cdb = cdb;
com->uscsi_cdblen = CDB_GROUP5;
com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
com->uscsi_buflen = buflen;
com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
SD_PATH_STANDARD);
kmem_free(cdda, sizeof (struct cdrom_cdda));
kmem_free(com, sizeof (*com));
return (rval);
}
/*
* Function: sr_read_cdxa()
*
* Description: This routine is the driver entry point for handling CD-ROM
* ioctl requests to return CD-XA (Extended Architecture) data.
* (CDROMCDXA).
*
* Arguments: dev - the device 'dev_t'
* data - pointer to user provided CD-XA structure specifying
* the data starting address, transfer length, and format
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
*/
static int
sr_read_cdxa(dev_t dev, caddr_t data, int flag)
{
struct sd_lun *un;
struct uscsi_cmd *com;
struct cdrom_cdxa *cdxa;
int rval;
size_t buflen;
char cdb[CDB_GROUP5];
uchar_t read_flags;
#ifdef _MULTI_DATAMODEL
/* To support ILP32 applications in an LP64 world */
struct cdrom_cdxa32 cdrom_cdxa32;
struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32;
#endif /* _MULTI_DATAMODEL */
if (data == NULL) {
return (EINVAL);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (ENXIO);
}
cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32:
if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
kmem_free(cdxa, sizeof (struct cdrom_cdxa));
return (EFAULT);
}
/*
* Convert the ILP32 uscsi data from the
* application to LP64 for internal use.
*/
cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
break;
case DDI_MODEL_NONE:
if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
kmem_free(cdxa, sizeof (struct cdrom_cdxa));
return (EFAULT);
}
break;
}
#else /* ! _MULTI_DATAMODEL */
if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
kmem_free(cdxa, sizeof (struct cdrom_cdxa));
return (EFAULT);
}
#endif /* _MULTI_DATAMODEL */
/*
* Since MMC-2 expects max 3 bytes for length, check if the
* length input is greater than 3 bytes
*/
if ((cdxa->cdxa_length & 0xFF000000) != 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
"cdrom transfer length too large: %d (limit %d)\n",
cdxa->cdxa_length, 0xFFFFFF);
kmem_free(cdxa, sizeof (struct cdrom_cdxa));
return (EINVAL);
}
switch (cdxa->cdxa_format) {
case CDROM_XA_DATA:
buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
read_flags = 0x10;
break;
case CDROM_XA_SECTOR_DATA:
buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
read_flags = 0xf8;
break;
case CDROM_XA_DATA_W_ERROR:
buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
read_flags = 0xfc;
break;
default:
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_read_cdxa: Format '0x%x' Not Supported\n",
cdxa->cdxa_format);
kmem_free(cdxa, sizeof (struct cdrom_cdxa));
return (EINVAL);
}
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
bzero(cdb, CDB_GROUP5);
if (un->un_f_mmc_cap == TRUE) {
cdb[0] = (char)SCMD_READ_CD;
cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
cdb[9] = (char)read_flags;
} else {
/*
* Note: A vendor specific command (0xDB) is being used her to
* request a read of all subcodes.
*/
cdb[0] = (char)SCMD_READ_CDXA;
cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
cdb[10] = cdxa->cdxa_format;
}
com->uscsi_cdb = cdb;
com->uscsi_cdblen = CDB_GROUP5;
com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
com->uscsi_buflen = buflen;
com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
SD_PATH_STANDARD);
kmem_free(cdxa, sizeof (struct cdrom_cdxa));
kmem_free(com, sizeof (*com));
return (rval);
}
/*
* Function: sr_eject()
*
* Description: This routine is the driver entry point for handling CD-ROM
* eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
*
* Arguments: dev - the device 'dev_t'
*
* Return Code: the code returned by sd_send_scsi_cmd()
*/
static int
sr_eject(dev_t dev)
{
struct sd_lun *un;
int rval;
sd_ssc_t *ssc;
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
(un->un_state == SD_STATE_OFFLINE)) {
return (ENXIO);
}
/*
* To prevent race conditions with the eject
* command, keep track of an eject command as
* it progresses. If we are already handling
* an eject command in the driver for the given
* unit and another request to eject is received
* immediately return EAGAIN so we don't lose
* the command if the current eject command fails.
*/
mutex_enter(SD_MUTEX(un));
if (un->un_f_ejecting == TRUE) {
mutex_exit(SD_MUTEX(un));
return (EAGAIN);
}
un->un_f_ejecting = TRUE;
mutex_exit(SD_MUTEX(un));
ssc = sd_ssc_init(un);
rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (rval != 0) {
mutex_enter(SD_MUTEX(un));
un->un_f_ejecting = FALSE;
mutex_exit(SD_MUTEX(un));
return (rval);
}
ssc = sd_ssc_init(un);
rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
SD_TARGET_EJECT, SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (rval == 0) {
mutex_enter(SD_MUTEX(un));
sr_ejected(un);
un->un_mediastate = DKIO_EJECTED;
un->un_f_ejecting = FALSE;
cv_broadcast(&un->un_state_cv);
mutex_exit(SD_MUTEX(un));
} else {
mutex_enter(SD_MUTEX(un));
un->un_f_ejecting = FALSE;
mutex_exit(SD_MUTEX(un));
}
return (rval);
}
/*
* Function: sr_ejected()
*
* Description: This routine updates the soft state structure to invalidate the
* geometry information after the media has been ejected or a
* media eject has been detected.
*
* Arguments: un - driver soft state (unit) structure
*/
static void
sr_ejected(struct sd_lun *un)
{
struct sd_errstats *stp;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
un->un_f_blockcount_is_valid = FALSE;
un->un_f_tgt_blocksize_is_valid = FALSE;
mutex_exit(SD_MUTEX(un));
cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
mutex_enter(SD_MUTEX(un));
if (un->un_errstats != NULL) {
stp = (struct sd_errstats *)un->un_errstats->ks_data;
stp->sd_capacity.value.ui64 = 0;
}
}
/*
* Function: sr_check_wp()
*
* Description: This routine checks the write protection of a removable
* media disk and hotpluggable devices via the write protect bit of
* the Mode Page Header device specific field. Some devices choke
* on unsupported mode page. In order to workaround this issue,
* this routine has been implemented to use 0x3f mode page(request
* for all pages) for all device types.
*
* Arguments: dev - the device 'dev_t'
*
* Return Code: int indicating if the device is write protected (1) or not (0)
*
* Context: Kernel thread.
*
*/
static int
sr_check_wp(dev_t dev)
{
struct sd_lun *un;
uchar_t device_specific;
uchar_t *sense;
int hdrlen;
int rval = FALSE;
int status;
sd_ssc_t *ssc;
/*
* Note: The return codes for this routine should be reworked to
* properly handle the case of a NULL softstate.
*/
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
return (FALSE);
}
if (un->un_f_cfg_is_atapi == TRUE) {
/*
* The mode page contents are not required; set the allocation
* length for the mode page header only
*/
hdrlen = MODE_HEADER_LENGTH_GRP2;
sense = kmem_zalloc(hdrlen, KM_SLEEP);
ssc = sd_ssc_init(un);
status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen,
MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (status != 0)
goto err_exit;
device_specific =
((struct mode_header_grp2 *)sense)->device_specific;
} else {
hdrlen = MODE_HEADER_LENGTH;
sense = kmem_zalloc(hdrlen, KM_SLEEP);
ssc = sd_ssc_init(un);
status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen,
MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (status != 0)
goto err_exit;
device_specific =
((struct mode_header *)sense)->device_specific;
}
/*
* Write protect mode sense failed; not all disks
* understand this query. Return FALSE assuming that
* these devices are not writable.
*/
if (device_specific & WRITE_PROTECT) {
rval = TRUE;
}
err_exit:
kmem_free(sense, hdrlen);
return (rval);
}
/*
* Function: sr_volume_ctrl()
*
* Description: This routine is the driver entry point for handling CD-ROM
* audio output volume ioctl requests. (CDROMVOLCTRL)
*
* Arguments: dev - the device 'dev_t'
* data - pointer to user audio volume control structure
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
*
*/
static int
sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
{
struct sd_lun *un;
struct cdrom_volctrl volume;
struct cdrom_volctrl *vol = &volume;
uchar_t *sense_page;
uchar_t *select_page;
uchar_t *sense;
uchar_t *select;
int sense_buflen;
int select_buflen;
int rval;
sd_ssc_t *ssc;
if (data == NULL) {
return (EINVAL);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
(un->un_state == SD_STATE_OFFLINE)) {
return (ENXIO);
}
if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
return (EFAULT);
}
if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
struct mode_header_grp2 *sense_mhp;
struct mode_header_grp2 *select_mhp;
int bd_len;
sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
select_buflen = MODE_HEADER_LENGTH_GRP2 +
MODEPAGE_AUDIO_CTRL_LEN;
sense = kmem_zalloc(sense_buflen, KM_SLEEP);
select = kmem_zalloc(select_buflen, KM_SLEEP);
ssc = sd_ssc_init(un);
rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
sense_buflen, MODEPAGE_AUDIO_CTRL,
SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (rval != 0) {
SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
"sr_volume_ctrl: Mode Sense Failed\n");
kmem_free(sense, sense_buflen);
kmem_free(select, select_buflen);
return (rval);
}
sense_mhp = (struct mode_header_grp2 *)sense;
select_mhp = (struct mode_header_grp2 *)select;
bd_len = (sense_mhp->bdesc_length_hi << 8) |
sense_mhp->bdesc_length_lo;
if (bd_len > MODE_BLK_DESC_LENGTH) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_volume_ctrl: Mode Sense returned invalid "
"block descriptor length\n");
kmem_free(sense, sense_buflen);
kmem_free(select, select_buflen);
return (EIO);
}
sense_page = (uchar_t *)
(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
select_mhp->length_msb = 0;
select_mhp->length_lsb = 0;
select_mhp->bdesc_length_hi = 0;
select_mhp->bdesc_length_lo = 0;
} else {
struct mode_header *sense_mhp, *select_mhp;
sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
sense = kmem_zalloc(sense_buflen, KM_SLEEP);
select = kmem_zalloc(select_buflen, KM_SLEEP);
ssc = sd_ssc_init(un);
rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
sense_buflen, MODEPAGE_AUDIO_CTRL,
SD_PATH_STANDARD);
sd_ssc_fini(ssc);
if (rval != 0) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_volume_ctrl: Mode Sense Failed\n");
kmem_free(sense, sense_buflen);
kmem_free(select, select_buflen);
return (rval);
}
sense_mhp = (struct mode_header *)sense;
select_mhp = (struct mode_header *)select;
if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sr_volume_ctrl: Mode Sense returned invalid "
"block descriptor length\n");
kmem_free(sense, sense_buflen);
kmem_free(select, select_buflen);
return (EIO);
}
sense_page = (uchar_t *)
(sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
select_mhp->length = 0;
select_mhp->bdesc_length = 0;
}
/*
* Note: An audio control data structure could be created and overlayed
* on the following in place of the array indexing method implemented.
*/
/* Build the select data for the user volume data */
select_page[0] = MODEPAGE_AUDIO_CTRL;
select_page[1] = 0xE;
/* Set the immediate bit */
select_page[2] = 0x04;
/* Zero out reserved fields */
select_page[3] = 0x00;
select_page[4] = 0x00;
/* Return sense data for fields not to be modified */
select_page[5] = sense_page[5];
select_page[6] = sense_page[6];
select_page[7] = sense_page[7];
/* Set the user specified volume levels for channel 0 and 1 */
select_page[8] = 0x01;
select_page[9] = vol->channel0;
select_page[10] = 0x02;
select_page[11] = vol->channel1;
/* Channel 2 and 3 are currently unsupported so return the sense data */
select_page[12] = sense_page[12];
select_page[13] = sense_page[13];
select_page[14] = sense_page[14];
select_page[15] = sense_page[15];
ssc = sd_ssc_init(un);
if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select,
select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
} else {
rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
}
sd_ssc_fini(ssc);
kmem_free(sense, sense_buflen);
kmem_free(select, select_buflen);
return (rval);
}
/*
* Function: sr_read_sony_session_offset()
*
* Description: This routine is the driver entry point for handling CD-ROM
* ioctl requests for session offset information. (CDROMREADOFFSET)
* The address of the first track in the last session of a
* multi-session CD-ROM is returned
*
* Note: This routine uses a vendor specific key value in the
* command control field without implementing any vendor check here
* or in the ioctl routine.
*
* Arguments: dev - the device 'dev_t'
* data - pointer to an int to hold the requested address
* flag - this argument is a pass through to ddi_copyxxx()
* directly from the mode argument of ioctl().
*
* Return Code: the code returned by sd_send_scsi_cmd()
* EFAULT if ddi_copyxxx() fails
* ENXIO if fail ddi_get_soft_state
* EINVAL if data pointer is NULL
*/
static int
sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
{
struct sd_lun *un;
struct uscsi_cmd *com;
caddr_t buffer;
char cdb[CDB_GROUP1];
int session_offset = 0;
int rval;
if (data == NULL) {
return (EINVAL);
}
if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
(un->un_state == SD_STATE_OFFLINE)) {
return (ENXIO);
}
buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
bzero(cdb, CDB_GROUP1);
cdb[0] = SCMD_READ_TOC;
/*
* Bytes 7 & 8 are the 12 byte allocation length for a single entry.
* (4 byte TOC response header + 8 byte response data)
*/
cdb[8] = SONY_SESSION_OFFSET_LEN;
/* Byte 9 is the control byte. A vendor specific value is used */
cdb[9] = SONY_SESSION_OFFSET_KEY;
com = kmem_zalloc(sizeof (*com), KM_SLEEP);
com->uscsi_cdb = cdb;
com->uscsi_cdblen = CDB_GROUP1;
com->uscsi_bufaddr = buffer;
com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ;
rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
SD_PATH_STANDARD);
if (rval != 0) {
kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
kmem_free(com, sizeof (*com));
return (rval);
}
if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
session_offset =
((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
/*
* Offset returned offset in current lbasize block's. Convert to
* 2k block's to return to the user
*/
if (un->un_tgt_blocksize == CDROM_BLK_512) {
session_offset >>= 2;
} else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
session_offset >>= 1;
}
}
if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
rval = EFAULT;
}
kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
kmem_free(com, sizeof (*com));
return (rval);
}
/*
* Function: sd_wm_cache_constructor()
*
* Description: Cache Constructor for the wmap cache for the read/modify/write
* devices.
*
* Arguments: wm - A pointer to the sd_w_map to be initialized.
* un - sd_lun structure for the device.
* flag - the km flags passed to constructor
*
* Return Code: 0 on success.
* -1 on failure.
*/
/*ARGSUSED*/
static int
sd_wm_cache_constructor(void *wm, void *un, int flags)
{
bzero(wm, sizeof (struct sd_w_map));
cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
return (0);
}
/*
* Function: sd_wm_cache_destructor()
*
* Description: Cache destructor for the wmap cache for the read/modify/write
* devices.
*
* Arguments: wm - A pointer to the sd_w_map to be initialized.
* un - sd_lun structure for the device.
*/
/*ARGSUSED*/
static void
sd_wm_cache_destructor(void *wm, void *un)
{
cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
}
/*
* Function: sd_range_lock()
*
* Description: Lock the range of blocks specified as parameter to ensure
* that read, modify write is atomic and no other i/o writes
* to the same location. The range is specified in terms
* of start and end blocks. Block numbers are the actual
* media block numbers and not system.
*
* Arguments: un - sd_lun structure for the device.
* startb - The starting block number
* endb - The end block number
* typ - type of i/o - simple/read_modify_write
*
* Return Code: wm - pointer to the wmap structure.
*
* Context: This routine can sleep.
*/
static struct sd_w_map *
sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
{
struct sd_w_map *wmp = NULL;
struct sd_w_map *sl_wmp = NULL;
struct sd_w_map *tmp_wmp;
wm_state state = SD_WM_CHK_LIST;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
mutex_enter(SD_MUTEX(un));
while (state != SD_WM_DONE) {
switch (state) {
case SD_WM_CHK_LIST:
/*
* This is the starting state. Check the wmap list
* to see if the range is currently available.
*/
if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
/*
* If this is a simple write and no rmw
* i/o is pending then try to lock the
* range as the range should be available.
*/
state = SD_WM_LOCK_RANGE;
} else {
tmp_wmp = sd_get_range(un, startb, endb);
if (tmp_wmp != NULL) {
if ((wmp != NULL) && ONLIST(un, wmp)) {
/*
* Should not keep onlist wmps
* while waiting this macro
* will also do wmp = NULL;
*/
FREE_ONLIST_WMAP(un, wmp);
}
/*
* sl_wmp is the wmap on which wait
* is done, since the tmp_wmp points
* to the inuse wmap, set sl_wmp to
* tmp_wmp and change the state to sleep
*/
sl_wmp = tmp_wmp;
state = SD_WM_WAIT_MAP;
} else {
state = SD_WM_LOCK_RANGE;
}
}
break;
case SD_WM_LOCK_RANGE:
ASSERT(un->un_wm_cache);
/*
* The range need to be locked, try to get a wmap.
* First attempt it with NO_SLEEP, want to avoid a sleep
* if possible as we will have to release the sd mutex
* if we have to sleep.
*/
if (wmp == NULL)
wmp = kmem_cache_alloc(un->un_wm_cache,
KM_NOSLEEP);
if (wmp == NULL) {
mutex_exit(SD_MUTEX(un));
_NOTE(DATA_READABLE_WITHOUT_LOCK
(sd_lun::un_wm_cache))
wmp = kmem_cache_alloc(un->un_wm_cache,
KM_SLEEP);
mutex_enter(SD_MUTEX(un));
/*
* we released the mutex so recheck and go to
* check list state.
*/
state = SD_WM_CHK_LIST;
} else {
/*
* We exit out of state machine since we
* have the wmap. Do the housekeeping first.
* place the wmap on the wmap list if it is not
* on it already and then set the state to done.
*/
wmp->wm_start = startb;
wmp->wm_end = endb;
wmp->wm_flags = typ | SD_WM_BUSY;
if (typ & SD_WTYPE_RMW) {
un->un_rmw_count++;
}
/*
* If not already on the list then link
*/
if (!ONLIST(un, wmp)) {
wmp->wm_next = un->un_wm;
wmp->wm_prev = NULL;
if (wmp->wm_next)
wmp->wm_next->wm_prev = wmp;
un->un_wm = wmp;
}
state = SD_WM_DONE;
}
break;
case SD_WM_WAIT_MAP:
ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
/*
* Wait is done on sl_wmp, which is set in the
* check_list state.
*/
sl_wmp->wm_wanted_count++;
cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
sl_wmp->wm_wanted_count--;
/*
* We can reuse the memory from the completed sl_wmp
* lock range for our new lock, but only if noone is
* waiting for it.
*/
ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
if (sl_wmp->wm_wanted_count == 0) {
if (wmp != NULL) {
CHK_N_FREEWMP(un, wmp);
}
wmp = sl_wmp;
}
sl_wmp = NULL;
/*
* After waking up, need to recheck for availability of
* range.
*/
state = SD_WM_CHK_LIST;
break;
default:
panic("sd_range_lock: "
"Unknown state %d in sd_range_lock", state);
/*NOTREACHED*/
} /* switch(state) */
} /* while(state != SD_WM_DONE) */
mutex_exit(SD_MUTEX(un));
ASSERT(wmp != NULL);
return (wmp);
}
/*
* Function: sd_get_range()
*
* Description: Find if there any overlapping I/O to this one
* Returns the write-map of 1st such I/O, NULL otherwise.
*
* Arguments: un - sd_lun structure for the device.
* startb - The starting block number
* endb - The end block number
*
* Return Code: wm - pointer to the wmap structure.
*/
static struct sd_w_map *
sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
{
struct sd_w_map *wmp;
ASSERT(un != NULL);
for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
if (!(wmp->wm_flags & SD_WM_BUSY)) {
continue;
}
if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
break;
}
if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
break;
}
}
return (wmp);
}
/*
* Function: sd_free_inlist_wmap()
*
* Description: Unlink and free a write map struct.
*
* Arguments: un - sd_lun structure for the device.
* wmp - sd_w_map which needs to be unlinked.
*/
static void
sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
{
ASSERT(un != NULL);
if (un->un_wm == wmp) {
un->un_wm = wmp->wm_next;
} else {
wmp->wm_prev->wm_next = wmp->wm_next;
}
if (wmp->wm_next) {
wmp->wm_next->wm_prev = wmp->wm_prev;
}
wmp->wm_next = wmp->wm_prev = NULL;
kmem_cache_free(un->un_wm_cache, wmp);
}
/*
* Function: sd_range_unlock()
*
* Description: Unlock the range locked by wm.
* Free write map if nobody else is waiting on it.
*
* Arguments: un - sd_lun structure for the device.
* wmp - sd_w_map which needs to be unlinked.
*/
static void
sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
{
ASSERT(un != NULL);
ASSERT(wm != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
mutex_enter(SD_MUTEX(un));
if (wm->wm_flags & SD_WTYPE_RMW) {
un->un_rmw_count--;
}
if (wm->wm_wanted_count) {
wm->wm_flags = 0;
/*
* Broadcast that the wmap is available now.
*/
cv_broadcast(&wm->wm_avail);
} else {
/*
* If no one is waiting on the map, it should be free'ed.
*/
sd_free_inlist_wmap(un, wm);
}
mutex_exit(SD_MUTEX(un));
}
/*
* Function: sd_read_modify_write_task
*
* Description: Called from a taskq thread to initiate the write phase of
* a read-modify-write request. This is used for targets where
* un->un_sys_blocksize != un->un_tgt_blocksize.
*
* Arguments: arg - a pointer to the buf(9S) struct for the write command.
*
* Context: Called under taskq thread context.
*/
static void
sd_read_modify_write_task(void *arg)
{
struct sd_mapblocksize_info *bsp;
struct buf *bp;
struct sd_xbuf *xp;
struct sd_lun *un;
bp = arg; /* The bp is given in arg */
ASSERT(bp != NULL);
/* Get the pointer to the layer-private data struct */
xp = SD_GET_XBUF(bp);
ASSERT(xp != NULL);
bsp = xp->xb_private;
ASSERT(bsp != NULL);
un = SD_GET_UN(bp);
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
SD_TRACE(SD_LOG_IO_RMMEDIA, un,
"sd_read_modify_write_task: entry: buf:0x%p\n", bp);
/*
* This is the write phase of a read-modify-write request, called
* under the context of a taskq thread in response to the completion
* of the read portion of the rmw request completing under interrupt
* context. The write request must be sent from here down the iostart
* chain as if it were being sent from sd_mapblocksize_iostart(), so
* we use the layer index saved in the layer-private data area.
*/
SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
SD_TRACE(SD_LOG_IO_RMMEDIA, un,
"sd_read_modify_write_task: exit: buf:0x%p\n", bp);
}
/*
* Function: sddump_do_read_of_rmw()
*
* Description: This routine will be called from sddump, If sddump is called
* with an I/O which not aligned on device blocksize boundary
* then the write has to be converted to read-modify-write.
* Do the read part here in order to keep sddump simple.
* Note - That the sd_mutex is held across the call to this
* routine.
*
* Arguments: un - sd_lun
* blkno - block number in terms of media block size.
* nblk - number of blocks.
* bpp - pointer to pointer to the buf structure. On return
* from this function, *bpp points to the valid buffer
* to which the write has to be done.
*
* Return Code: 0 for success or errno-type return code
*/
static int
sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
struct buf **bpp)
{
int err;
int i;
int rval;
struct buf *bp;
struct scsi_pkt *pkt = NULL;
uint32_t target_blocksize;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
target_blocksize = un->un_tgt_blocksize;
mutex_exit(SD_MUTEX(un));
bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
(size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
if (bp == NULL) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"no resources for dumping; giving up");
err = ENOMEM;
goto done;
}
rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
blkno, nblk);
if (rval != 0) {
scsi_free_consistent_buf(bp);
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"no resources for dumping; giving up");
err = ENOMEM;
goto done;
}
pkt->pkt_flags |= FLAG_NOINTR;
err = EIO;
for (i = 0; i < SD_NDUMP_RETRIES; i++) {
/*
* Scsi_poll returns 0 (success) if the command completes and
* the status block is STATUS_GOOD. We should only check
* errors if this condition is not true. Even then we should
* send our own request sense packet only if we have a check
* condition and auto request sense has not been performed by
* the hba.
*/
SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
err = 0;
break;
}
/*
* Check CMD_DEV_GONE 1st, give up if device is gone,
* no need to read RQS data.
*/
if (pkt->pkt_reason == CMD_DEV_GONE) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"Error while dumping state with rmw..."
"Device is gone\n");
break;
}
if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
SD_INFO(SD_LOG_DUMP, un,
"sddump: read failed with CHECK, try # %d\n", i);
if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
(void) sd_send_polled_RQS(un);
}
continue;
}
if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
int reset_retval = 0;
SD_INFO(SD_LOG_DUMP, un,
"sddump: read failed with BUSY, try # %d\n", i);
if (un->un_f_lun_reset_enabled == TRUE) {
reset_retval = scsi_reset(SD_ADDRESS(un),
RESET_LUN);
}
if (reset_retval == 0) {
(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
}
(void) sd_send_polled_RQS(un);
} else {
SD_INFO(SD_LOG_DUMP, un,
"sddump: read failed with 0x%x, try # %d\n",
SD_GET_PKT_STATUS(pkt), i);
mutex_enter(SD_MUTEX(un));
sd_reset_target(un, pkt);
mutex_exit(SD_MUTEX(un));
}
/*
* If we are not getting anywhere with lun/target resets,
* let's reset the bus.
*/
if (i > SD_NDUMP_RETRIES / 2) {
(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
(void) sd_send_polled_RQS(un);
}
}
scsi_destroy_pkt(pkt);
if (err != 0) {
scsi_free_consistent_buf(bp);
*bpp = NULL;
} else {
*bpp = bp;
}
done:
mutex_enter(SD_MUTEX(un));
return (err);
}
/*
* Function: sd_failfast_flushq
*
* Description: Take all bp's on the wait queue that have B_FAILFAST set
* in b_flags and move them onto the failfast queue, then kick
* off a thread to return all bp's on the failfast queue to
* their owners with an error set.
*
* Arguments: un - pointer to the soft state struct for the instance.
*
* Context: may execute in interrupt context.
*/
static void
sd_failfast_flushq(struct sd_lun *un)
{
struct buf *bp;
struct buf *next_waitq_bp;
struct buf *prev_waitq_bp = NULL;
ASSERT(un != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
ASSERT(un->un_failfast_bp == NULL);
SD_TRACE(SD_LOG_IO_FAILFAST, un,
"sd_failfast_flushq: entry: un:0x%p\n", un);
/*
* Check if we should flush all bufs when entering failfast state, or
* just those with B_FAILFAST set.
*/
if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
/*
* Move *all* bp's on the wait queue to the failfast flush
* queue, including those that do NOT have B_FAILFAST set.
*/
if (un->un_failfast_headp == NULL) {
ASSERT(un->un_failfast_tailp == NULL);
un->un_failfast_headp = un->un_waitq_headp;
} else {
ASSERT(un->un_failfast_tailp != NULL);
un->un_failfast_tailp->av_forw = un->un_waitq_headp;
}
un->un_failfast_tailp = un->un_waitq_tailp;
/* update kstat for each bp moved out of the waitq */
for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
}
/* empty the waitq */
un->un_waitq_headp = un->un_waitq_tailp = NULL;
} else {
/*
* Go thru the wait queue, pick off all entries with
* B_FAILFAST set, and move these onto the failfast queue.
*/
for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
/*
* Save the pointer to the next bp on the wait queue,
* so we get to it on the next iteration of this loop.
*/
next_waitq_bp = bp->av_forw;
/*
* If this bp from the wait queue does NOT have
* B_FAILFAST set, just move on to the next element
* in the wait queue. Note, this is the only place
* where it is correct to set prev_waitq_bp.
*/
if ((bp->b_flags & B_FAILFAST) == 0) {
prev_waitq_bp = bp;
continue;
}
/*
* Remove the bp from the wait queue.
*/
if (bp == un->un_waitq_headp) {
/* The bp is the first element of the waitq. */
un->un_waitq_headp = next_waitq_bp;
if (un->un_waitq_headp == NULL) {
/* The wait queue is now empty */
un->un_waitq_tailp = NULL;
}
} else {
/*
* The bp is either somewhere in the middle
* or at the end of the wait queue.
*/
ASSERT(un->un_waitq_headp != NULL);
ASSERT(prev_waitq_bp != NULL);
ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
== 0);
if (bp == un->un_waitq_tailp) {
/* bp is the last entry on the waitq. */
ASSERT(next_waitq_bp == NULL);
un->un_waitq_tailp = prev_waitq_bp;
}
prev_waitq_bp->av_forw = next_waitq_bp;
}
bp->av_forw = NULL;
/*
* update kstat since the bp is moved out of
* the waitq
*/
SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
/*
* Now put the bp onto the failfast queue.
*/
if (un->un_failfast_headp == NULL) {
/* failfast queue is currently empty */
ASSERT(un->un_failfast_tailp == NULL);
un->un_failfast_headp =
un->un_failfast_tailp = bp;
} else {
/* Add the bp to the end of the failfast q */
ASSERT(un->un_failfast_tailp != NULL);
ASSERT(un->un_failfast_tailp->b_flags &
B_FAILFAST);
un->un_failfast_tailp->av_forw = bp;
un->un_failfast_tailp = bp;
}
}
}
/*
* Now return all bp's on the failfast queue to their owners.
*/
while ((bp = un->un_failfast_headp) != NULL) {
un->un_failfast_headp = bp->av_forw;
if (un->un_failfast_headp == NULL) {
un->un_failfast_tailp = NULL;
}
/*
* We want to return the bp with a failure error code, but
* we do not want a call to sd_start_cmds() to occur here,
* so use sd_return_failed_command_no_restart() instead of
* sd_return_failed_command().
*/
sd_return_failed_command_no_restart(un, bp, EIO);
}
/* Flush the xbuf queues if required. */
if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
}
SD_TRACE(SD_LOG_IO_FAILFAST, un,
"sd_failfast_flushq: exit: un:0x%p\n", un);
}
/*
* Function: sd_failfast_flushq_callback
*
* Description: Return TRUE if the given bp meets the criteria for failfast
* flushing. Used with ddi_xbuf_flushq(9F).
*
* Arguments: bp - ptr to buf struct to be examined.
*
* Context: Any
*/
static int
sd_failfast_flushq_callback(struct buf *bp)
{
/*
* Return TRUE if (1) we want to flush ALL bufs when the failfast
* state is entered; OR (2) the given bp has B_FAILFAST set.
*/
return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
(bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
}
/*
* Function: sd_setup_next_xfer
*
* Description: Prepare next I/O operation using DMA_PARTIAL
*
*/
static int
sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
struct scsi_pkt *pkt, struct sd_xbuf *xp)
{
ssize_t num_blks_not_xfered;
daddr_t strt_blk_num;
ssize_t bytes_not_xfered;
int rval;
ASSERT(pkt->pkt_resid == 0);
/*
* Calculate next block number and amount to be transferred.
*
* How much data NOT transfered to the HBA yet.
*/
bytes_not_xfered = xp->xb_dma_resid;
/*
* figure how many blocks NOT transfered to the HBA yet.
*/
num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
/*
* set starting block number to the end of what WAS transfered.
*/
strt_blk_num = xp->xb_blkno +
SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
/*
* Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt
* will call scsi_initpkt with NULL_FUNC so we do not have to release
* the disk mutex here.
*/
rval = sd_setup_next_rw_pkt(un, pkt, bp,
strt_blk_num, num_blks_not_xfered);
if (rval == 0) {
/*
* Success.
*
* Adjust things if there are still more blocks to be
* transfered.
*/
xp->xb_dma_resid = pkt->pkt_resid;
pkt->pkt_resid = 0;
return (1);
}
/*
* There's really only one possible return value from
* sd_setup_next_rw_pkt which occurs when scsi_init_pkt
* returns NULL.
*/
ASSERT(rval == SD_PKT_ALLOC_FAILURE);
bp->b_resid = bp->b_bcount;
bp->b_flags |= B_ERROR;
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"Error setting up next portion of DMA transfer\n");
return (0);
}
/*
* Function: sd_panic_for_res_conflict
*
* Description: Call panic with a string formatted with "Reservation Conflict"
* and a human readable identifier indicating the SD instance
* that experienced the reservation conflict.
*
* Arguments: un - pointer to the soft state struct for the instance.
*
* Context: may execute in interrupt context.
*/
#define SD_RESV_CONFLICT_FMT_LEN 40
void
sd_panic_for_res_conflict(struct sd_lun *un)
{
char panic_str[SD_RESV_CONFLICT_FMT_LEN + MAXPATHLEN];
char path_str[MAXPATHLEN];
(void) snprintf(panic_str, sizeof (panic_str),
"Reservation Conflict\nDisk: %s",
ddi_pathname(SD_DEVINFO(un), path_str));
panic(panic_str);
}
/*
* Note: The following sd_faultinjection_ioctl( ) routines implement
* driver support for handling fault injection for error analysis
* causing faults in multiple layers of the driver.
*
*/
#ifdef SD_FAULT_INJECTION
static uint_t sd_fault_injection_on = 0;
/*
* Function: sd_faultinjection_ioctl()
*
* Description: This routine is the driver entry point for handling
* faultinjection ioctls to inject errors into the
* layer model
*
* Arguments: cmd - the ioctl cmd received
* arg - the arguments from user and returns
*/
static void
sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un)
{
uint_t i = 0;
uint_t rval;
SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
mutex_enter(SD_MUTEX(un));
switch (cmd) {
case SDIOCRUN:
/* Allow pushed faults to be injected */
SD_INFO(SD_LOG_SDTEST, un,
"sd_faultinjection_ioctl: Injecting Fault Run\n");
sd_fault_injection_on = 1;
SD_INFO(SD_LOG_IOERR, un,
"sd_faultinjection_ioctl: run finished\n");
break;
case SDIOCSTART:
/* Start Injection Session */
SD_INFO(SD_LOG_SDTEST, un,
"sd_faultinjection_ioctl: Injecting Fault Start\n");
sd_fault_injection_on = 0;
un->sd_injection_mask = 0xFFFFFFFF;
for (i = 0; i < SD_FI_MAX_ERROR; i++) {
un->sd_fi_fifo_pkt[i] = NULL;
un->sd_fi_fifo_xb[i] = NULL;
un->sd_fi_fifo_un[i] = NULL;
un->sd_fi_fifo_arq[i] = NULL;
}
un->sd_fi_fifo_start = 0;
un->sd_fi_fifo_end = 0;
mutex_enter(&(un->un_fi_mutex));
un->sd_fi_log[0] = '\0';
un->sd_fi_buf_len = 0;
mutex_exit(&(un->un_fi_mutex));
SD_INFO(SD_LOG_IOERR, un,
"sd_faultinjection_ioctl: start finished\n");
break;
case SDIOCSTOP:
/* Stop Injection Session */
SD_INFO(SD_LOG_SDTEST, un,
"sd_faultinjection_ioctl: Injecting Fault Stop\n");
sd_fault_injection_on = 0;
un->sd_injection_mask = 0x0;
/* Empty stray or unuseds structs from fifo */
for (i = 0; i < SD_FI_MAX_ERROR; i++) {
if (un->sd_fi_fifo_pkt[i] != NULL) {
kmem_free(un->sd_fi_fifo_pkt[i],
sizeof (struct sd_fi_pkt));
}
if (un->sd_fi_fifo_xb[i] != NULL) {
kmem_free(un->sd_fi_fifo_xb[i],
sizeof (struct sd_fi_xb));
}
if (un->sd_fi_fifo_un[i] != NULL) {
kmem_free(un->sd_fi_fifo_un[i],
sizeof (struct sd_fi_un));
}
if (un->sd_fi_fifo_arq[i] != NULL) {
kmem_free(un->sd_fi_fifo_arq[i],
sizeof (struct sd_fi_arq));
}
un->sd_fi_fifo_pkt[i] = NULL;
un->sd_fi_fifo_un[i] = NULL;
un->sd_fi_fifo_xb[i] = NULL;
un->sd_fi_fifo_arq[i] = NULL;
}
un->sd_fi_fifo_start = 0;
un->sd_fi_fifo_end = 0;
SD_INFO(SD_LOG_IOERR, un,
"sd_faultinjection_ioctl: stop finished\n");
break;
case SDIOCINSERTPKT:
/* Store a packet struct to be pushed onto fifo */
SD_INFO(SD_LOG_SDTEST, un,
"sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
sd_fault_injection_on = 0;
/* No more that SD_FI_MAX_ERROR allowed in Queue */
if (un->sd_fi_fifo_pkt[i] != NULL) {
kmem_free(un->sd_fi_fifo_pkt[i],
sizeof (struct sd_fi_pkt));
}
if (arg != (uintptr_t)NULL) {
un->sd_fi_fifo_pkt[i] =
kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
if (un->sd_fi_fifo_pkt[i] == NULL) {
/* Alloc failed don't store anything */
break;
}
rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
sizeof (struct sd_fi_pkt), 0);
if (rval == -1) {
kmem_free(un->sd_fi_fifo_pkt[i],
sizeof (struct sd_fi_pkt));
un->sd_fi_fifo_pkt[i] = NULL;
}
} else {
SD_INFO(SD_LOG_IOERR, un,
"sd_faultinjection_ioctl: pkt null\n");
}
break;
case SDIOCINSERTXB:
/* Store a xb struct to be pushed onto fifo */
SD_INFO(SD_LOG_SDTEST, un,
"sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
sd_fault_injection_on = 0;
if (un->sd_fi_fifo_xb[i] != NULL) {
kmem_free(un->sd_fi_fifo_xb[i],
sizeof (struct sd_fi_xb));
un->sd_fi_fifo_xb[i] = NULL;
}
if (arg != (uintptr_t)NULL) {
un->sd_fi_fifo_xb[i] =
kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
if (un->sd_fi_fifo_xb[i] == NULL) {
/* Alloc failed don't store anything */
break;
}
rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
sizeof (struct sd_fi_xb), 0);
if (rval == -1) {
kmem_free(un->sd_fi_fifo_xb[i],
sizeof (struct sd_fi_xb));
un->sd_fi_fifo_xb[i] = NULL;
}
} else {
SD_INFO(SD_LOG_IOERR, un,
"sd_faultinjection_ioctl: xb null\n");
}
break;
case SDIOCINSERTUN:
/* Store a un struct to be pushed onto fifo */
SD_INFO(SD_LOG_SDTEST, un,
"sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
sd_fault_injection_on = 0;
if (un->sd_fi_fifo_un[i] != NULL) {
kmem_free(un->sd_fi_fifo_un[i],
sizeof (struct sd_fi_un));
un->sd_fi_fifo_un[i] = NULL;
}
if (arg != (uintptr_t)NULL) {
un->sd_fi_fifo_un[i] =
kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
if (un->sd_fi_fifo_un[i] == NULL) {
/* Alloc failed don't store anything */
break;
}
rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
sizeof (struct sd_fi_un), 0);
if (rval == -1) {
kmem_free(un->sd_fi_fifo_un[i],
sizeof (struct sd_fi_un));
un->sd_fi_fifo_un[i] = NULL;
}
} else {
SD_INFO(SD_LOG_IOERR, un,
"sd_faultinjection_ioctl: un null\n");
}
break;
case SDIOCINSERTARQ:
/* Store a arq struct to be pushed onto fifo */
SD_INFO(SD_LOG_SDTEST, un,
"sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
sd_fault_injection_on = 0;
if (un->sd_fi_fifo_arq[i] != NULL) {
kmem_free(un->sd_fi_fifo_arq[i],
sizeof (struct sd_fi_arq));
un->sd_fi_fifo_arq[i] = NULL;
}
if (arg != (uintptr_t)NULL) {
un->sd_fi_fifo_arq[i] =
kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
if (un->sd_fi_fifo_arq[i] == NULL) {
/* Alloc failed don't store anything */
break;
}
rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
sizeof (struct sd_fi_arq), 0);
if (rval == -1) {
kmem_free(un->sd_fi_fifo_arq[i],
sizeof (struct sd_fi_arq));
un->sd_fi_fifo_arq[i] = NULL;
}
} else {
SD_INFO(SD_LOG_IOERR, un,
"sd_faultinjection_ioctl: arq null\n");
}
break;
case SDIOCPUSH:
/* Push stored xb, pkt, un, and arq onto fifo */
sd_fault_injection_on = 0;
if (arg != (uintptr_t)NULL) {
rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
if (rval != -1 &&
un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
un->sd_fi_fifo_end += i;
}
} else {
SD_INFO(SD_LOG_IOERR, un,
"sd_faultinjection_ioctl: push arg null\n");
if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
un->sd_fi_fifo_end++;
}
}
SD_INFO(SD_LOG_IOERR, un,
"sd_faultinjection_ioctl: push to end=%d\n",
un->sd_fi_fifo_end);
break;
case SDIOCRETRIEVE:
/* Return buffer of log from Injection session */
SD_INFO(SD_LOG_SDTEST, un,
"sd_faultinjection_ioctl: Injecting Fault Retreive");
sd_fault_injection_on = 0;
mutex_enter(&(un->un_fi_mutex));
rval = ddi_copyout(un->sd_fi_log, (void *)arg,
un->sd_fi_buf_len+1, 0);
mutex_exit(&(un->un_fi_mutex));
if (rval == -1) {
/*
* arg is possibly invalid setting
* it to NULL for return
*/
arg = (uintptr_t)NULL;
}
break;
}
mutex_exit(SD_MUTEX(un));
SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: exit\n");
}
/*
* Function: sd_injection_log()
*
* Description: This routine adds buff to the already existing injection log
* for retrieval via faultinjection_ioctl for use in fault
* detection and recovery
*
* Arguments: buf - the string to add to the log
*/
static void
sd_injection_log(char *buf, struct sd_lun *un)
{
uint_t len;
ASSERT(un != NULL);
ASSERT(buf != NULL);
mutex_enter(&(un->un_fi_mutex));
len = min(strlen(buf), 255);
/* Add logged value to Injection log to be returned later */
if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
uint_t offset = strlen((char *)un->sd_fi_log);
char *destp = (char *)un->sd_fi_log + offset;
int i;
for (i = 0; i < len; i++) {
*destp++ = *buf++;
}
un->sd_fi_buf_len += len;
un->sd_fi_log[un->sd_fi_buf_len] = '\0';
}
mutex_exit(&(un->un_fi_mutex));
}
/*
* Function: sd_faultinjection()
*
* Description: This routine takes the pkt and changes its
* content based on error injection scenerio.
*
* Arguments: pktp - packet to be changed
*/
static void
sd_faultinjection(struct scsi_pkt *pktp)
{
uint_t i;
struct sd_fi_pkt *fi_pkt;
struct sd_fi_xb *fi_xb;
struct sd_fi_un *fi_un;
struct sd_fi_arq *fi_arq;
struct buf *bp;
struct sd_xbuf *xb;
struct sd_lun *un;
ASSERT(pktp != NULL);
/* pull bp xb and un from pktp */
bp = (struct buf *)pktp->pkt_private;
xb = SD_GET_XBUF(bp);
un = SD_GET_UN(bp);
ASSERT(un != NULL);
mutex_enter(SD_MUTEX(un));
SD_TRACE(SD_LOG_SDTEST, un,
"sd_faultinjection: entry Injection from sdintr\n");
/* if injection is off return */
if (sd_fault_injection_on == 0 ||
un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
mutex_exit(SD_MUTEX(un));
return;
}
SD_INFO(SD_LOG_SDTEST, un,
"sd_faultinjection: is working for copying\n");
/* take next set off fifo */
i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
fi_pkt = un->sd_fi_fifo_pkt[i];
fi_xb = un->sd_fi_fifo_xb[i];
fi_un = un->sd_fi_fifo_un[i];
fi_arq = un->sd_fi_fifo_arq[i];
/* set variables accordingly */
/* set pkt if it was on fifo */
if (fi_pkt != NULL) {
SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
if (fi_pkt->pkt_cdbp != 0xff)
SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
}
/* set xb if it was on fifo */
if (fi_xb != NULL) {
SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
if (fi_xb->xb_retry_count != 0)
SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
SD_CONDSET(xb, xb, xb_victim_retry_count,
"xb_victim_retry_count");
SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
/* copy in block data from sense */
/*
* if (fi_xb->xb_sense_data[0] != -1) {
* bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
* SENSE_LENGTH);
* }
*/
bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH);
/* copy in extended sense codes */
SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
xb, es_code, "es_code");
SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
xb, es_key, "es_key");
SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
xb, es_add_code, "es_add_code");
SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
xb, es_qual_code, "es_qual_code");
struct scsi_extended_sense *esp;
esp = (struct scsi_extended_sense *)xb->xb_sense_data;
esp->es_class = CLASS_EXTENDED_SENSE;
}
/* set un if it was on fifo */
if (fi_un != NULL) {
SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
SD_CONDSET(un, un, un_ctype, "un_ctype");
SD_CONDSET(un, un, un_reset_retry_count,
"un_reset_retry_count");
SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
SD_CONDSET(un, un, un_f_allow_bus_device_reset,
"un_f_allow_bus_device_reset");
SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
}
/* copy in auto request sense if it was on fifo */
if (fi_arq != NULL) {
bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
}
/* free structs */
if (un->sd_fi_fifo_pkt[i] != NULL) {
kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
}
if (un->sd_fi_fifo_xb[i] != NULL) {
kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
}
if (un->sd_fi_fifo_un[i] != NULL) {
kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
}
if (un->sd_fi_fifo_arq[i] != NULL) {
kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
}
/*
* kmem_free does not gurantee to set to NULL
* since we uses these to determine if we set
* values or not lets confirm they are always
* NULL after free
*/
un->sd_fi_fifo_pkt[i] = NULL;
un->sd_fi_fifo_un[i] = NULL;
un->sd_fi_fifo_xb[i] = NULL;
un->sd_fi_fifo_arq[i] = NULL;
un->sd_fi_fifo_start++;
mutex_exit(SD_MUTEX(un));
SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
}
#endif /* SD_FAULT_INJECTION */
/*
* This routine is invoked in sd_unit_attach(). Before calling it, the
* properties in conf file should be processed already, and "hotpluggable"
* property was processed also.
*
* The sd driver distinguishes 3 different type of devices: removable media,
* non-removable media, and hotpluggable. Below the differences are defined:
*
* 1. Device ID
*
* The device ID of a device is used to identify this device. Refer to
* ddi_devid_register(9F).
*
* For a non-removable media disk device which can provide 0x80 or 0x83
* VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
* device ID is created to identify this device. For other non-removable
* media devices, a default device ID is created only if this device has
* at least 2 alter cylinders. Otherwise, this device has no devid.
*
* -------------------------------------------------------
* removable media hotpluggable | Can Have Device ID
* -------------------------------------------------------
* false false | Yes
* false true | Yes
* true x | No
* ------------------------------------------------------
*
*
* 2. SCSI group 4 commands
*
* In SCSI specs, only some commands in group 4 command set can use
* 8-byte addresses that can be used to access >2TB storage spaces.
* Other commands have no such capability. Without supporting group4,
* it is impossible to make full use of storage spaces of a disk with
* capacity larger than 2TB.
*
* -----------------------------------------------
* removable media hotpluggable LP64 | Group
* -----------------------------------------------
* false false false | 1
* false false true | 4
* false true false | 1
* false true true | 4
* true x x | 5
* -----------------------------------------------
*
*
* 3. Check for VTOC Label
*
* If a direct-access disk has no EFI label, sd will check if it has a
* valid VTOC label. Now, sd also does that check for removable media
* and hotpluggable devices.
*
* --------------------------------------------------------------
* Direct-Access removable media hotpluggable | Check Label
* -------------------------------------------------------------
* false false false | No
* false false true | No
* false true false | Yes
* false true true | Yes
* true x x | Yes
* --------------------------------------------------------------
*
*
* 4. Building default VTOC label
*
* As section 3 says, sd checks if some kinds of devices have VTOC label.
* If those devices have no valid VTOC label, sd(4D) will attempt to
* create default VTOC for them. Currently sd creates default VTOC label
* for all devices on x86 platform (VTOC_16), but only for removable
* media devices on SPARC (VTOC_8).
*
* -----------------------------------------------------------
* removable media hotpluggable platform | Default Label
* -----------------------------------------------------------
* false false sparc | No
* false true x86 | Yes
* false true sparc | Yes
* true x x | Yes
* ----------------------------------------------------------
*
*
* 5. Supported blocksizes of target devices
*
* Sd supports non-512-byte blocksize for removable media devices only.
* For other devices, only 512-byte blocksize is supported. This may be
* changed in near future because some RAID devices require non-512-byte
* blocksize
*
* -----------------------------------------------------------
* removable media hotpluggable | non-512-byte blocksize
* -----------------------------------------------------------
* false false | No
* false true | No
* true x | Yes
* -----------------------------------------------------------
*
*
* 6. Automatic mount & unmount
*
* sd(4D) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
* if a device is removable media device. It return 1 for removable media
* devices, and 0 for others.
*
* The automatic mounting subsystem should distinguish between the types
* of devices and apply automounting policies to each.
*
*
* 7. fdisk partition management
*
* Fdisk is traditional partition method on x86 platform. sd(4D) driver
* just supports fdisk partitions on x86 platform. On sparc platform, sd
* doesn't support fdisk partitions at all. Note: pcfs(4FS) can recognize
* fdisk partitions on both x86 and SPARC platform.
*
* -----------------------------------------------------------
* platform removable media USB/1394 | fdisk supported
* -----------------------------------------------------------
* x86 X X | true
* ------------------------------------------------------------
* sparc X X | false
* ------------------------------------------------------------
*
*
* 8. MBOOT/MBR
*
* Although sd(4D) doesn't support fdisk on SPARC platform, it does support
* read/write mboot for removable media devices on sparc platform.
*
* -----------------------------------------------------------
* platform removable media USB/1394 | mboot supported
* -----------------------------------------------------------
* x86 X X | true
* ------------------------------------------------------------
* sparc false false | false
* sparc false true | true
* sparc true false | true
* sparc true true | true
* ------------------------------------------------------------
*
*
* 9. error handling during opening device
*
* If failed to open a disk device, an errno is returned. For some kinds
* of errors, different errno is returned depending on if this device is
* a removable media device. This brings USB/1394 hard disks in line with
* expected hard disk behavior. It is not expected that this breaks any
* application.
*
* ------------------------------------------------------
* removable media hotpluggable | errno
* ------------------------------------------------------
* false false | EIO
* false true | EIO
* true x | ENXIO
* ------------------------------------------------------
*
*
* 11. ioctls: DKIOCEJECT, CDROMEJECT
*
* These IOCTLs are applicable only to removable media devices.
*
* -----------------------------------------------------------
* removable media hotpluggable |DKIOCEJECT, CDROMEJECT
* -----------------------------------------------------------
* false false | No
* false true | No
* true x | Yes
* -----------------------------------------------------------
*
*
* 12. Kstats for partitions
*
* sd creates partition kstat for non-removable media devices. USB and
* Firewire hard disks now have partition kstats
*
* ------------------------------------------------------
* removable media hotpluggable | kstat
* ------------------------------------------------------
* false false | Yes
* false true | Yes
* true x | No
* ------------------------------------------------------
*
*
* 13. Removable media & hotpluggable properties
*
* Sd driver creates a "removable-media" property for removable media
* devices. Parent nexus drivers create a "hotpluggable" property if
* it supports hotplugging.
*
* ---------------------------------------------------------------------
* removable media hotpluggable | "removable-media" " hotpluggable"
* ---------------------------------------------------------------------
* false false | No No
* false true | No Yes
* true false | Yes No
* true true | Yes Yes
* ---------------------------------------------------------------------
*
*
* 14. Power Management
*
* sd only power manages removable media devices or devices that support
* LOG_SENSE or have a "pm-capable" property (PSARC/2002/250)
*
* A parent nexus that supports hotplugging can also set "pm-capable"
* if the disk can be power managed.
*
* ------------------------------------------------------------
* removable media hotpluggable pm-capable | power manage
* ------------------------------------------------------------
* false false false | No
* false false true | Yes
* false true false | No
* false true true | Yes
* true x x | Yes
* ------------------------------------------------------------
*
* USB and firewire hard disks can now be power managed independently
* of the framebuffer
*
*
* 15. Support for USB disks with capacity larger than 1TB
*
* Currently, sd doesn't permit a fixed disk device with capacity
* larger than 1TB to be used in a 32-bit operating system environment.
* However, sd doesn't do that for removable media devices. Instead, it
* assumes that removable media devices cannot have a capacity larger
* than 1TB. Therefore, using those devices on 32-bit system is partially
* supported, which can cause some unexpected results.
*
* ---------------------------------------------------------------------
* removable media USB/1394 | Capacity > 1TB | Used in 32-bit env
* ---------------------------------------------------------------------
* false false | true | no
* false true | true | no
* true false | true | Yes
* true true | true | Yes
* ---------------------------------------------------------------------
*
*
* 16. Check write-protection at open time
*
* When a removable media device is being opened for writing without NDELAY
* flag, sd will check if this device is writable. If attempting to open
* without NDELAY flag a write-protected device, this operation will abort.
*
* ------------------------------------------------------------
* removable media USB/1394 | WP Check
* ------------------------------------------------------------
* false false | No
* false true | No
* true false | Yes
* true true | Yes
* ------------------------------------------------------------
*
*
* 17. syslog when corrupted VTOC is encountered
*
* Currently, if an invalid VTOC is encountered, sd only print syslog
* for fixed SCSI disks.
* ------------------------------------------------------------
* removable media USB/1394 | print syslog
* ------------------------------------------------------------
* false false | Yes
* false true | No
* true false | No
* true true | No
* ------------------------------------------------------------
*/
static void
sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
{
int pm_cap;
ASSERT(un->un_sd);
ASSERT(un->un_sd->sd_inq);
/*
* Enable SYNC CACHE support for all devices.
*/
un->un_f_sync_cache_supported = TRUE;
/*
* Set the sync cache required flag to false.
* This would ensure that there is no SYNC CACHE
* sent when there are no writes
*/
un->un_f_sync_cache_required = FALSE;
if (un->un_sd->sd_inq->inq_rmb) {
/*
* The media of this device is removable. And for this kind
* of devices, it is possible to change medium after opening
* devices. Thus we should support this operation.
*/
un->un_f_has_removable_media = TRUE;
/*
* support non-512-byte blocksize of removable media devices
*/
un->un_f_non_devbsize_supported = TRUE;
/*
* Assume that all removable media devices support DOOR_LOCK
*/
un->un_f_doorlock_supported = TRUE;
/*
* For a removable media device, it is possible to be opened
* with NDELAY flag when there is no media in drive, in this
* case we don't care if device is writable. But if without
* NDELAY flag, we need to check if media is write-protected.
*/
un->un_f_chk_wp_open = TRUE;
/*
* need to start a SCSI watch thread to monitor media state,
* when media is being inserted or ejected, notify syseventd.
*/
un->un_f_monitor_media_state = TRUE;
/*
* Some devices don't support START_STOP_UNIT command.
* Therefore, we'd better check if a device supports it
* before sending it.
*/
un->un_f_check_start_stop = TRUE;
/*
* support eject media ioctl:
* FDEJECT, DKIOCEJECT, CDROMEJECT
*/
un->un_f_eject_media_supported = TRUE;
/*
* Because many removable-media devices don't support
* LOG_SENSE, we couldn't use this command to check if
* a removable media device support power-management.
* We assume that they support power-management via
* START_STOP_UNIT command and can be spun up and down
* without limitations.
*/
un->un_f_pm_supported = TRUE;
/*
* Need to create a zero length (Boolean) property
* removable-media for the removable media devices.
* Note that the return value of the property is not being
* checked, since if unable to create the property
* then do not want the attach to fail altogether. Consistent
* with other property creation in attach.
*/
(void) ddi_prop_create(DDI_DEV_T_NONE, devi,
DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
} else {
/*
* create device ID for device
*/
un->un_f_devid_supported = TRUE;
/*
* Spin up non-removable-media devices once it is attached
*/
un->un_f_attach_spinup = TRUE;
/*
* According to SCSI specification, Sense data has two kinds of
* format: fixed format, and descriptor format. At present, we
* don't support descriptor format sense data for removable
* media.
*/
if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
un->un_f_descr_format_supported = TRUE;
}
/*
* kstats are created only for non-removable media devices.
*
* Set this in sd.conf to 0 in order to disable kstats. The
* default is 1, so they are enabled by default.
*/
un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
SD_DEVINFO(un), DDI_PROP_DONTPASS,
"enable-partition-kstats", 1));
/*
* Check if HBA has set the "pm-capable" property.
* If "pm-capable" exists and is non-zero then we can
* power manage the device without checking the start/stop
* cycle count log sense page.
*
* If "pm-capable" exists and is set to be false (0),
* then we should not power manage the device.
*
* If "pm-capable" doesn't exist then pm_cap will
* be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case,
* sd will check the start/stop cycle count log sense page
* and power manage the device if the cycle count limit has
* not been exceeded.
*/
pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) {
un->un_f_log_sense_supported = TRUE;
if (!un->un_f_power_condition_disabled &&
SD_INQUIRY(un)->inq_ansi == 6) {
un->un_f_power_condition_supported = TRUE;
}
} else {
/*
* pm-capable property exists.
*
* Convert "TRUE" values for pm_cap to
* SD_PM_CAPABLE_IS_TRUE to make it easier to check
* later. "TRUE" values are any values defined in
* inquiry.h.
*/
if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) {
un->un_f_log_sense_supported = FALSE;
} else {
/* SD_PM_CAPABLE_IS_TRUE case */
un->un_f_pm_supported = TRUE;
if (!un->un_f_power_condition_disabled &&
SD_PM_CAPABLE_IS_SPC_4(pm_cap)) {
un->un_f_power_condition_supported =
TRUE;
}
if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) {
un->un_f_log_sense_supported = TRUE;
un->un_f_pm_log_sense_smart =
SD_PM_CAP_SMART_LOG(pm_cap);
}
}
SD_INFO(SD_LOG_ATTACH_DETACH, un,
"sd_unit_attach: un:0x%p pm-capable "
"property set to %d.\n", un, un->un_f_pm_supported);
}
}
if (un->un_f_is_hotpluggable) {
/*
* Have to watch hotpluggable devices as well, since
* that's the only way for userland applications to
* detect hot removal while device is busy/mounted.
*/
un->un_f_monitor_media_state = TRUE;
un->un_f_check_start_stop = TRUE;
}
}
/*
* sd_tg_rdwr:
* Provides rdwr access for cmlb via sd_tgops. The start_block is
* in sys block size, req_length in bytes.
*
*/
static int
sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
diskaddr_t start_block, size_t reqlength, void *tg_cookie)
{
struct sd_lun *un;
int path_flag = (int)(uintptr_t)tg_cookie;
char *dkl = NULL;
diskaddr_t real_addr = start_block;
diskaddr_t first_byte, end_block;
size_t buffer_size = reqlength;
int rval = 0;
diskaddr_t cap;
uint32_t lbasize;
sd_ssc_t *ssc;
un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
if (un == NULL)
return (ENXIO);
if (cmd != TG_READ && cmd != TG_WRITE)
return (EINVAL);
ssc = sd_ssc_init(un);
mutex_enter(SD_MUTEX(un));
if (un->un_f_tgt_blocksize_is_valid == FALSE) {
mutex_exit(SD_MUTEX(un));
rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
&lbasize, path_flag);
if (rval != 0)
goto done1;
mutex_enter(SD_MUTEX(un));
sd_update_block_info(un, lbasize, cap);
if ((un->un_f_tgt_blocksize_is_valid == FALSE)) {
mutex_exit(SD_MUTEX(un));
rval = EIO;
goto done;
}
}
if (NOT_DEVBSIZE(un)) {
/*
* sys_blocksize != tgt_blocksize, need to re-adjust
* blkno and save the index to beginning of dk_label
*/
first_byte = SD_SYSBLOCKS2BYTES(start_block);
real_addr = first_byte / un->un_tgt_blocksize;
end_block = (first_byte + reqlength +
un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
/* round up buffer size to multiple of target block size */
buffer_size = (end_block - real_addr) * un->un_tgt_blocksize;
SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr",
"label_addr: 0x%x allocation size: 0x%x\n",
real_addr, buffer_size);
if (((first_byte % un->un_tgt_blocksize) != 0) ||
(reqlength % un->un_tgt_blocksize) != 0)
/* the request is not aligned */
dkl = kmem_zalloc(buffer_size, KM_SLEEP);
}
/*
* The MMC standard allows READ CAPACITY to be
* inaccurate by a bounded amount (in the interest of
* response latency). As a result, failed READs are
* commonplace (due to the reading of metadata and not
* data). Depending on the per-Vendor/drive Sense data,
* the failed READ can cause many (unnecessary) retries.
*/
if (ISCD(un) && (cmd == TG_READ) &&
(un->un_f_blockcount_is_valid == TRUE) &&
((start_block == (un->un_blockcount - 1)) ||
(start_block == (un->un_blockcount - 2)))) {
path_flag = SD_PATH_DIRECT_PRIORITY;
}
mutex_exit(SD_MUTEX(un));
if (cmd == TG_READ) {
rval = sd_send_scsi_READ(ssc, (dkl != NULL) ? dkl : bufaddr,
buffer_size, real_addr, path_flag);
if (dkl != NULL)
bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block,
real_addr), bufaddr, reqlength);
} else {
if (dkl) {
rval = sd_send_scsi_READ(ssc, dkl, buffer_size,
real_addr, path_flag);
if (rval) {
goto done1;
}
bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block,
real_addr), reqlength);
}
rval = sd_send_scsi_WRITE(ssc, (dkl != NULL) ? dkl : bufaddr,
buffer_size, real_addr, path_flag);
}
done1:
if (dkl != NULL)
kmem_free(dkl, buffer_size);
if (rval != 0) {
if (rval == EIO)
sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
else
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
done:
sd_ssc_fini(ssc);
return (rval);
}
static int
sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie)
{
struct sd_lun *un;
diskaddr_t cap;
uint32_t lbasize;
int path_flag = (int)(uintptr_t)tg_cookie;
int ret = 0;
un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
if (un == NULL)
return (ENXIO);
switch (cmd) {
case TG_GETPHYGEOM:
case TG_GETVIRTGEOM:
case TG_GETCAPACITY:
case TG_GETBLOCKSIZE:
mutex_enter(SD_MUTEX(un));
if ((un->un_f_blockcount_is_valid == TRUE) &&
(un->un_f_tgt_blocksize_is_valid == TRUE)) {
cap = un->un_blockcount;
lbasize = un->un_tgt_blocksize;
mutex_exit(SD_MUTEX(un));
} else {
sd_ssc_t *ssc;
mutex_exit(SD_MUTEX(un));
ssc = sd_ssc_init(un);
ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
&lbasize, path_flag);
if (ret != 0) {
if (ret == EIO)
sd_ssc_assessment(ssc,
SD_FMT_STATUS_CHECK);
else
sd_ssc_assessment(ssc,
SD_FMT_IGNORE);
sd_ssc_fini(ssc);
return (ret);
}
sd_ssc_fini(ssc);
mutex_enter(SD_MUTEX(un));
sd_update_block_info(un, lbasize, cap);
if ((un->un_f_blockcount_is_valid == FALSE) ||
(un->un_f_tgt_blocksize_is_valid == FALSE)) {
mutex_exit(SD_MUTEX(un));
return (EIO);
}
mutex_exit(SD_MUTEX(un));
}
if (cmd == TG_GETCAPACITY) {
*(diskaddr_t *)arg = cap;
return (0);
}
if (cmd == TG_GETBLOCKSIZE) {
*(uint32_t *)arg = lbasize;
return (0);
}
if (cmd == TG_GETPHYGEOM)
ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg,
cap, lbasize, path_flag);
else
/* TG_GETVIRTGEOM */
ret = sd_get_virtual_geometry(un,
(cmlb_geom_t *)arg, cap, lbasize);
return (ret);
case TG_GETATTR:
mutex_enter(SD_MUTEX(un));
((tg_attribute_t *)arg)->media_is_writable =
un->un_f_mmc_writable_media;
((tg_attribute_t *)arg)->media_is_solid_state =
un->un_f_is_solid_state;
((tg_attribute_t *)arg)->media_is_rotational =
un->un_f_is_rotational;
mutex_exit(SD_MUTEX(un));
return (0);
default:
return (ENOTTY);
}
}
/*
* Function: sd_ssc_ereport_post
*
* Description: Will be called when SD driver need to post an ereport.
*
* Context: Kernel thread or interrupt context.
*/
#define DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown"
static void
sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
{
int uscsi_path_instance = 0;
uchar_t uscsi_pkt_reason;
uint32_t uscsi_pkt_state;
uint32_t uscsi_pkt_statistics;
uint64_t uscsi_ena;
uchar_t op_code;
uint8_t *sensep;
union scsi_cdb *cdbp;
uint_t cdblen = 0;
uint_t senlen = 0;
struct sd_lun *un;
dev_info_t *dip;
char *devid;
int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
SSC_FLAGS_INVALID_STATUS |
SSC_FLAGS_INVALID_SENSE |
SSC_FLAGS_INVALID_DATA;
char assessment[16];
ASSERT(ssc != NULL);
ASSERT(ssc->ssc_uscsi_cmd != NULL);
ASSERT(ssc->ssc_uscsi_info != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
dip = un->un_sd->sd_dev;
/*
* Get the devid:
* devid will only be passed to non-transport error reports.
*/
devid = DEVI(dip)->devi_devid_str;
/*
* If we are syncing or dumping, the command will not be executed
* so we bypass this situation.
*/
if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
(un->un_state == SD_STATE_DUMPING))
return;
uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance;
uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
/* In rare cases, EG:DOORLOCK, the cdb could be NULL */
if (cdbp == NULL) {
scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
"sd_ssc_ereport_post meet empty cdb\n");
return;
}
op_code = cdbp->scc_cmd;
cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
ssc->ssc_uscsi_cmd->uscsi_rqresid);
if (senlen > 0)
ASSERT(sensep != NULL);
/*
* Initialize drv_assess to corresponding values.
* SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending
* on the sense-key returned back.
*/
switch (drv_assess) {
case SD_FM_DRV_RECOVERY:
(void) sprintf(assessment, "%s", "recovered");
break;
case SD_FM_DRV_RETRY:
(void) sprintf(assessment, "%s", "retry");
break;
case SD_FM_DRV_NOTICE:
(void) sprintf(assessment, "%s", "info");
break;
case SD_FM_DRV_FATAL:
default:
(void) sprintf(assessment, "%s", "unknown");
}
/*
* If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
* command, we will post ereport.io.scsi.cmd.disk.recovered.
* driver-assessment will always be "recovered" here.
*/
if (drv_assess == SD_FM_DRV_RECOVERY) {
scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
"cmd.disk.recovered", uscsi_ena, devid, NULL,
DDI_NOSLEEP, NULL,
FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
DEVID_IF_KNOWN(devid),
"driver-assessment", DATA_TYPE_STRING, assessment,
"op-code", DATA_TYPE_UINT8, op_code,
"cdb", DATA_TYPE_UINT8_ARRAY,
cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
"pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
"pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
"pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
NULL);
return;
}
/*
* If there is un-expected/un-decodable data, we should post
* ereport.io.scsi.cmd.disk.dev.uderr.
* driver-assessment will be set based on parameter drv_assess.
* SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
* SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
* SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
* SSC_FLAGS_INVALID_DATA - invalid data sent back.
*/
if (ssc->ssc_flags & ssc_invalid_flags) {
if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
NULL, "cmd.disk.dev.uderr", uscsi_ena, devid,
NULL, DDI_NOSLEEP, NULL,
FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
DEVID_IF_KNOWN(devid),
"driver-assessment", DATA_TYPE_STRING,
drv_assess == SD_FM_DRV_FATAL ?
"fail" : assessment,
"op-code", DATA_TYPE_UINT8, op_code,
"cdb", DATA_TYPE_UINT8_ARRAY,
cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
"pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
"pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
"pkt-stats", DATA_TYPE_UINT32,
uscsi_pkt_statistics,
"stat-code", DATA_TYPE_UINT8,
ssc->ssc_uscsi_cmd->uscsi_status,
"un-decode-info", DATA_TYPE_STRING,
ssc->ssc_info,
"un-decode-value", DATA_TYPE_UINT8_ARRAY,
senlen, sensep,
NULL);
} else {
/*
* For other type of invalid data, the
* un-decode-value field would be empty because the
* un-decodable content could be seen from upper
* level payload or inside un-decode-info.
*/
scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
NULL,
"cmd.disk.dev.uderr", uscsi_ena, devid,
NULL, DDI_NOSLEEP, NULL,
FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
DEVID_IF_KNOWN(devid),
"driver-assessment", DATA_TYPE_STRING,
drv_assess == SD_FM_DRV_FATAL ?
"fail" : assessment,
"op-code", DATA_TYPE_UINT8, op_code,
"cdb", DATA_TYPE_UINT8_ARRAY,
cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
"pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
"pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
"pkt-stats", DATA_TYPE_UINT32,
uscsi_pkt_statistics,
"stat-code", DATA_TYPE_UINT8,
ssc->ssc_uscsi_cmd->uscsi_status,
"un-decode-info", DATA_TYPE_STRING,
ssc->ssc_info,
"un-decode-value", DATA_TYPE_UINT8_ARRAY,
0, NULL,
NULL);
}
ssc->ssc_flags &= ~ssc_invalid_flags;
return;
}
if (uscsi_pkt_reason != CMD_CMPLT ||
(ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
/*
* pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
* set inside sd_start_cmds due to errors(bad packet or
* fatal transport error), we should take it as a
* transport error, so we post ereport.io.scsi.cmd.disk.tran.
* driver-assessment will be set based on drv_assess.
* We will set devid to NULL because it is a transport
* error.
*/
if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
"cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL,
FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
DEVID_IF_KNOWN(devid),
"driver-assessment", DATA_TYPE_STRING,
drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
"op-code", DATA_TYPE_UINT8, op_code,
"cdb", DATA_TYPE_UINT8_ARRAY,
cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
"pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
"pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
"pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
NULL);
} else {
/*
* If we got here, we have a completed command, and we need
* to further investigate the sense data to see what kind
* of ereport we should post.
* No ereport is needed if sense-key is KEY_RECOVERABLE_ERROR
* and asc/ascq is "ATA PASS-THROUGH INFORMATION AVAILABLE".
* Post ereport.io.scsi.cmd.disk.dev.rqs.merr if sense-key is
* KEY_MEDIUM_ERROR.
* Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise.
* driver-assessment will be set based on the parameter
* drv_assess.
*/
if (senlen > 0) {
/*
* Here we have sense data available.
*/
uint8_t sense_key = scsi_sense_key(sensep);
uint8_t sense_asc = scsi_sense_asc(sensep);
uint8_t sense_ascq = scsi_sense_ascq(sensep);
if (sense_key == KEY_RECOVERABLE_ERROR &&
sense_asc == 0x00 && sense_ascq == 0x1d)
return;
if (sense_key == KEY_MEDIUM_ERROR) {
/*
* driver-assessment should be "fatal" if
* drv_assess is SD_FM_DRV_FATAL.
*/
scsi_fm_ereport_post(un->un_sd,
uscsi_path_instance, NULL,
"cmd.disk.dev.rqs.merr",
uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL,
FM_VERSION, DATA_TYPE_UINT8,
FM_EREPORT_VERS0,
DEVID_IF_KNOWN(devid),
"driver-assessment",
DATA_TYPE_STRING,
drv_assess == SD_FM_DRV_FATAL ?
"fatal" : assessment,
"op-code",
DATA_TYPE_UINT8, op_code,
"cdb",
DATA_TYPE_UINT8_ARRAY, cdblen,
ssc->ssc_uscsi_cmd->uscsi_cdb,
"pkt-reason",
DATA_TYPE_UINT8, uscsi_pkt_reason,
"pkt-state",
DATA_TYPE_UINT8, uscsi_pkt_state,
"pkt-stats",
DATA_TYPE_UINT32,
uscsi_pkt_statistics,
"stat-code",
DATA_TYPE_UINT8,
ssc->ssc_uscsi_cmd->uscsi_status,
"key",
DATA_TYPE_UINT8,
scsi_sense_key(sensep),
"asc",
DATA_TYPE_UINT8,
scsi_sense_asc(sensep),
"ascq",
DATA_TYPE_UINT8,
scsi_sense_ascq(sensep),
"sense-data",
DATA_TYPE_UINT8_ARRAY,
senlen, sensep,
"lba",
DATA_TYPE_UINT64,
ssc->ssc_uscsi_info->ui_lba,
NULL);
} else {
/*
* if sense-key == 0x4(hardware
* error), driver-assessment should
* be "fatal" if drv_assess is
* SD_FM_DRV_FATAL.
*/
scsi_fm_ereport_post(un->un_sd,
uscsi_path_instance, NULL,
"cmd.disk.dev.rqs.derr",
uscsi_ena, devid,
NULL, DDI_NOSLEEP, NULL,
FM_VERSION,
DATA_TYPE_UINT8, FM_EREPORT_VERS0,
DEVID_IF_KNOWN(devid),
"driver-assessment",
DATA_TYPE_STRING,
drv_assess == SD_FM_DRV_FATAL ?
(sense_key == 0x4 ?
"fatal" : "fail") : assessment,
"op-code",
DATA_TYPE_UINT8, op_code,
"cdb",
DATA_TYPE_UINT8_ARRAY, cdblen,
ssc->ssc_uscsi_cmd->uscsi_cdb,
"pkt-reason",
DATA_TYPE_UINT8, uscsi_pkt_reason,
"pkt-state",
DATA_TYPE_UINT8, uscsi_pkt_state,
"pkt-stats",
DATA_TYPE_UINT32,
uscsi_pkt_statistics,
"stat-code",
DATA_TYPE_UINT8,
ssc->ssc_uscsi_cmd->uscsi_status,
"key",
DATA_TYPE_UINT8,
scsi_sense_key(sensep),
"asc",
DATA_TYPE_UINT8,
scsi_sense_asc(sensep),
"ascq",
DATA_TYPE_UINT8,
scsi_sense_ascq(sensep),
"sense-data",
DATA_TYPE_UINT8_ARRAY,
senlen, sensep,
NULL);
}
} else {
/*
* For stat_code == STATUS_GOOD, this is not a
* hardware error.
*/
if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
return;
/*
* Post ereport.io.scsi.cmd.disk.dev.serr if we got the
* stat-code but with sense data unavailable.
* driver-assessment will be set based on parameter
* drv_assess.
*/
scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
NULL,
"cmd.disk.dev.serr", uscsi_ena,
devid, NULL, DDI_NOSLEEP, NULL,
FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
DEVID_IF_KNOWN(devid),
"driver-assessment", DATA_TYPE_STRING,
drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
"op-code", DATA_TYPE_UINT8, op_code,
"cdb",
DATA_TYPE_UINT8_ARRAY,
cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
"pkt-reason",
DATA_TYPE_UINT8, uscsi_pkt_reason,
"pkt-state",
DATA_TYPE_UINT8, uscsi_pkt_state,
"pkt-stats",
DATA_TYPE_UINT32, uscsi_pkt_statistics,
"stat-code",
DATA_TYPE_UINT8,
ssc->ssc_uscsi_cmd->uscsi_status,
NULL);
}
}
}
/*
* Function: sd_ssc_extract_info
*
* Description: Extract information available to help generate ereport.
*
* Context: Kernel thread or interrupt context.
*/
static void
sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp,
struct buf *bp, struct sd_xbuf *xp)
{
size_t senlen = 0;
union scsi_cdb *cdbp;
int path_instance;
/*
* Need scsi_cdb_size array to determine the cdb length.
*/
extern uchar_t scsi_cdb_size[];
ASSERT(un != NULL);
ASSERT(pktp != NULL);
ASSERT(bp != NULL);
ASSERT(xp != NULL);
ASSERT(ssc != NULL);
ASSERT(mutex_owned(SD_MUTEX(un)));
/*
* Transfer the cdb buffer pointer here.
*/
cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)];
ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp;
/*
* Transfer the sense data buffer pointer if sense data is available,
* calculate the sense data length first.
*/
if ((xp->xb_sense_state & STATE_XARQ_DONE) ||
(xp->xb_sense_state & STATE_ARQ_DONE)) {
/*
* For arq case, we will enter here.
*/
if (xp->xb_sense_state & STATE_XARQ_DONE) {
senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid;
} else {
senlen = SENSE_LENGTH;
}
} else {
/*
* For non-arq case, we will enter this branch.
*/
if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK &&
(xp->xb_sense_state & STATE_XFERRED_DATA)) {
senlen = SENSE_LENGTH - xp->xb_sense_resid;
}
}
ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff);
ssc->ssc_uscsi_cmd->uscsi_rqresid = 0;
ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data;
ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
/*
* Only transfer path_instance when scsi_pkt was properly allocated.
*/
path_instance = pktp->pkt_path_instance;
if (scsi_pkt_allocated_correctly(pktp) && path_instance)
ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance;
else
ssc->ssc_uscsi_cmd->uscsi_path_instance = 0;
/*
* Copy in the other fields we may need when posting ereport.
*/
ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason;
ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state;
ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics;
ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
/*
* For partially read/write command, we will not create ena
* in case of a successful command be reconized as recovered.
*/
if ((pktp->pkt_reason == CMD_CMPLT) &&
(ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) &&
(senlen == 0)) {
return;
}
/*
* To associate ereports of a single command execution flow, we
* need a shared ena for a specific command.
*/
if (xp->xb_ena == 0)
xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1);
ssc->ssc_uscsi_info->ui_ena = xp->xb_ena;
}
/*
* Function: sd_check_bdc_vpd
*
* Description: Query the optional INQUIRY VPD page 0xb1. If the device
* supports VPD page 0xb1, sd examines the MEDIUM ROTATION
* RATE.
*
* Set the following based on RPM value:
* = 0 device is not solid state, non-rotational
* = 1 device is solid state, non-rotational
* > 1 device is not solid state, rotational
*
* Context: Kernel thread or interrupt context.
*/
static void
sd_check_bdc_vpd(sd_ssc_t *ssc)
{
int rval = 0;
uchar_t *inqb1 = NULL;
size_t inqb1_len = MAX_INQUIRY_SIZE;
size_t inqb1_resid = 0;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
mutex_enter(SD_MUTEX(un));
un->un_f_is_rotational = TRUE;
un->un_f_is_solid_state = FALSE;
if (ISCD(un)) {
mutex_exit(SD_MUTEX(un));
return;
}
if (sd_check_vpd_page_support(ssc) == 0 &&
un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) {
mutex_exit(SD_MUTEX(un));
/* collect page b1 data */
inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP);
rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len,
0x01, 0xB1, &inqb1_resid);
if (rval == 0 && (inqb1_len - inqb1_resid > 5)) {
SD_TRACE(SD_LOG_COMMON, un,
"sd_check_bdc_vpd: \
successfully get VPD page: %x \
PAGE LENGTH: %x BYTE 4: %x \
BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4],
inqb1[5]);
mutex_enter(SD_MUTEX(un));
/*
* Check the MEDIUM ROTATION RATE.
*/
if (inqb1[4] == 0) {
if (inqb1[5] == 0) {
un->un_f_is_rotational = FALSE;
} else if (inqb1[5] == 1) {
un->un_f_is_rotational = FALSE;
un->un_f_is_solid_state = TRUE;
/*
* Solid state drives don't need
* disksort.
*/
un->un_f_disksort_disabled = TRUE;
}
}
mutex_exit(SD_MUTEX(un));
} else if (rval != 0) {
sd_ssc_assessment(ssc, SD_FMT_IGNORE);
}
kmem_free(inqb1, inqb1_len);
} else {
mutex_exit(SD_MUTEX(un));
}
}
/*
* Function: sd_check_emulation_mode
*
* Description: Check whether the SSD is at emulation mode
* by issuing READ_CAPACITY_16 to see whether
* we can get physical block size of the drive.
*
* Context: Kernel thread or interrupt context.
*/
static void
sd_check_emulation_mode(sd_ssc_t *ssc)
{
int rval = 0;
uint64_t capacity;
uint_t lbasize;
uint_t pbsize;
int i;
int devid_len;
struct sd_lun *un;
ASSERT(ssc != NULL);
un = ssc->ssc_un;
ASSERT(un != NULL);
ASSERT(!mutex_owned(SD_MUTEX(un)));
mutex_enter(SD_MUTEX(un));
if (ISCD(un)) {
mutex_exit(SD_MUTEX(un));
return;
}
if (un->un_f_descr_format_supported) {
mutex_exit(SD_MUTEX(un));
rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
&pbsize, SD_PATH_DIRECT);
mutex_enter(SD_MUTEX(un));
if (rval != 0) {
un->un_phy_blocksize = DEV_BSIZE;
} else {
if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) {
un->un_phy_blocksize = DEV_BSIZE;
} else if (pbsize > un->un_phy_blocksize) {
/*
* Don't reset the physical blocksize
* unless we've detected a larger value.
*/
un->un_phy_blocksize = pbsize;
}
}
}
for (i = 0; i < sd_flash_dev_table_size; i++) {
devid_len = (int)strlen(sd_flash_dev_table[i]);
if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len)
== SD_SUCCESS) {
un->un_phy_blocksize = SSD_SECSIZE;
if (un->un_f_is_solid_state &&
un->un_phy_blocksize != un->un_tgt_blocksize)
un->un_f_enable_rmw = TRUE;
}
}
mutex_exit(SD_MUTEX(un));
}
|