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

/*
 * t1394.c
 *    1394 Target Driver Interface
 *    This file contains all of the 1394 Software Framework routines called
 *    by target drivers
 */

#include <sys/sysmacros.h>
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/types.h>
#include <sys/kmem.h>
#include <sys/disp.h>
#include <sys/1394/t1394.h>
#include <sys/1394/s1394.h>
#include <sys/1394/h1394.h>
#include <sys/1394/ieee1394.h>

static int s1394_allow_detach = 0;

/*
 * Function:    t1394_attach()
 * Input(s):    dip			The dip given to the target driver
 *					    in it's attach() routine
 *		version			The version of the target driver -
 *					    T1394_VERSION_V1
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	attachinfo		Used to pass info back to target,
 *					    including bus generation, local
 *					    node ID, dma attribute, etc.
 *		t1394_hdl		The target "handle" to be used for
 *					    all subsequent calls into the
 *					    1394 Software Framework
 *
 * Description:	t1394_attach() registers the target (based on its dip) with
 *		the 1394 Software Framework.  It returns the bus_generation,
 *		local_nodeID, iblock_cookie and other useful information to
 *		the target, as well as a handle (t1394_hdl) that will be used
 *		in all subsequent calls into this framework.
 */
/* ARGSUSED */
int
t1394_attach(dev_info_t *dip, int version, uint_t flags,
    t1394_attachinfo_t *attachinfo, t1394_handle_t *t1394_hdl)
{
	s1394_hal_t	*hal;
	s1394_target_t	*target;
	uint_t		dev;
	uint_t		curr;
	uint_t		unit_dir;
	int		hp_node = 0;

	ASSERT(t1394_hdl != NULL);
	ASSERT(attachinfo != NULL);

	*t1394_hdl = NULL;

	if (version != T1394_VERSION_V1) {
		return (DDI_FAILURE);
	}

	hal = s1394_dip_to_hal(ddi_get_parent(dip));
	if (hal == NULL) {
		return (DDI_FAILURE);
	}

	ASSERT(MUTEX_NOT_HELD(&hal->topology_tree_mutex));

	hp_node = ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
	    "hp-node");

	/* Allocate space for s1394_target_t */
	target = kmem_zalloc(sizeof (s1394_target_t), KM_SLEEP);

	mutex_enter(&hal->topology_tree_mutex);

	target->target_version = version;

	/* Copy in the params */
	target->target_dip = dip;
	target->on_hal	   = hal;

	/* Place the target on the appropriate node */
	target->on_node	= NULL;

	rw_enter(&target->on_hal->target_list_rwlock, RW_WRITER);
	if (hp_node != 0) {
		s1394_add_target_to_node(target);
		/*
		 * on_node can be NULL if the node got unplugged
		 * while the target driver is in its attach routine.
		 */
		if (target->on_node == NULL) {
			s1394_remove_target_from_node(target);
			rw_exit(&target->on_hal->target_list_rwlock);
			mutex_exit(&hal->topology_tree_mutex);
			kmem_free(target, sizeof (s1394_target_t));
			return (DDI_FAILURE);
		}

		target->target_state = S1394_TARG_HP_NODE;
		if (S1394_NODE_BUS_PWR_CONSUMER(target->on_node) == B_TRUE)
			target->target_state |= S1394_TARG_BUS_PWR_CONSUMER;
	}

	/* Return the current generation */
	attachinfo->localinfo.bus_generation = target->on_hal->generation_count;

	/* Fill in hal node id */
	attachinfo->localinfo.local_nodeID = target->on_hal->node_id;

	/* Give the target driver the iblock_cookie */
	attachinfo->iblock_cookie = target->on_hal->halinfo.hw_interrupt;

	/* Give the target driver the attributes */
	attachinfo->acc_attr	= target->on_hal->halinfo.acc_attr;
	attachinfo->dma_attr	= target->on_hal->halinfo.dma_attr;

	unit_dir = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
		DDI_PROP_DONTPASS, "unit-dir-offset", 0);
	target->unit_dir = unit_dir;

	/* By default, disable all physical AR requests */
	target->physical_arreq_enabled = 0;


	/* Get dev_max_payload & current_max_payload */
	s1394_get_maxpayload(target, &dev, &curr);
	target->dev_max_payload		= dev;
	target->current_max_payload	= curr;

	/* Add into linked list */
	if ((target->on_hal->target_head == NULL) &&
	    (target->on_hal->target_tail == NULL)) {
		target->on_hal->target_head = target;
		target->on_hal->target_tail = target;
	} else {
		target->on_hal->target_tail->target_next = target;
		target->target_prev = target->on_hal->target_tail;
		target->on_hal->target_tail = target;
	}
	rw_exit(&target->on_hal->target_list_rwlock);

	/* Fill in services layer private info */
	*t1394_hdl = (t1394_handle_t)target;

	mutex_exit(&hal->topology_tree_mutex);

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_detach()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	DDI_SUCCESS		Target successfully detached
 *		DDI_FAILURE		Target failed to detach
 *
 * Description:	t1394_detach() unregisters the target from the 1394 Software
 *		Framework.  t1394_detach() can fail if the target has any
 *		allocated commands that haven't been freed.
 */
/* ARGSUSED */
int
t1394_detach(t1394_handle_t *t1394_hdl, uint_t flags)
{
	s1394_target_t	*target;
	uint_t		num_cmds;

	ASSERT(t1394_hdl != NULL);

	target = (s1394_target_t *)(*t1394_hdl);

	ASSERT(target->on_hal);

	mutex_enter(&target->on_hal->topology_tree_mutex);
	rw_enter(&target->on_hal->target_list_rwlock, RW_WRITER);

	/* How many cmds has this target allocated? */
	num_cmds = target->target_num_cmds;

	if (num_cmds != 0) {
		rw_exit(&target->on_hal->target_list_rwlock);
		mutex_exit(&target->on_hal->topology_tree_mutex);
		return (DDI_FAILURE);
	}

	/*
	 * Remove from linked lists. Topology tree is already locked
	 * so that the node won't go away while we are looking at it.
	 */
	if ((target->on_hal->target_head == target) &&
	    (target->on_hal->target_tail == target)) {
		target->on_hal->target_head = NULL;
		target->on_hal->target_tail = NULL;
	} else {
		if (target->target_prev)
			target->target_prev->target_next = target->target_next;
		if (target->target_next)
			target->target_next->target_prev = target->target_prev;
		if (target->on_hal->target_head == target)
			target->on_hal->target_head = target->target_next;
		if (target->on_hal->target_tail == target)
			target->on_hal->target_tail = target->target_prev;
	}

	s1394_remove_target_from_node(target);
	rw_exit(&target->on_hal->target_list_rwlock);

	mutex_exit(&target->on_hal->topology_tree_mutex);

	/* Free memory */
	kmem_free(target, sizeof (s1394_target_t));

	*t1394_hdl = NULL;

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_alloc_cmd()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		flags			The flags parameter is described below
 *
 * Output(s):	cmdp			Pointer to the newly allocated command
 *
 * Description:	t1394_alloc_cmd() allocates a command for use with the
 *		t1394_read(), t1394_write(), or t1394_lock() interfaces
 *		of the 1394 Software Framework.  By default, t1394_alloc_cmd()
 *		may sleep while allocating memory for the command structure.
 *		If this is undesirable, the target may set the
 *		T1394_ALLOC_CMD_NOSLEEP bit in the flags parameter.  Also,
 *		this call may fail because a target driver has already
 *		allocated MAX_NUMBER_ALLOC_CMDS commands.
 */
int
t1394_alloc_cmd(t1394_handle_t t1394_hdl, uint_t flags, cmd1394_cmd_t **cmdp)
{
	s1394_hal_t	 *hal;
	s1394_target_t	 *target;
	s1394_cmd_priv_t *s_priv;
	uint_t		 num_cmds;

	ASSERT(t1394_hdl != NULL);

	target = (s1394_target_t *)t1394_hdl;

	/* Find the HAL this target resides on */
	hal = target->on_hal;

	rw_enter(&hal->target_list_rwlock, RW_WRITER);

	/* How many cmds has this target allocated? */
	num_cmds = target->target_num_cmds;

	if (num_cmds >= MAX_NUMBER_ALLOC_CMDS) {
		rw_exit(&hal->target_list_rwlock);
		/* kstats - cmd alloc failures */
		hal->hal_kstats->cmd_alloc_fail++;
		return (DDI_FAILURE);
	}

	/* Increment the number of cmds this target has allocated? */
	target->target_num_cmds = num_cmds + 1;

	if (s1394_alloc_cmd(hal, flags, cmdp) != DDI_SUCCESS) {
		target->target_num_cmds = num_cmds;	/* Undo increment */
		rw_exit(&hal->target_list_rwlock);
		/* kstats - cmd alloc failures */
		hal->hal_kstats->cmd_alloc_fail++;
		return (DDI_FAILURE);
	}

	rw_exit(&hal->target_list_rwlock);

	/* Get the Services Layer private area */
	s_priv = S1394_GET_CMD_PRIV(*cmdp);

	/* Initialize the command's blocking mutex */
	mutex_init(&s_priv->blocking_mutex, NULL, MUTEX_DRIVER,
	    hal->halinfo.hw_interrupt);

	/* Initialize the command's blocking condition variable */
	cv_init(&s_priv->blocking_cv, NULL, CV_DRIVER, NULL);

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_free_cmd()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		flags			The flags parameter is unused (for now)
 *		cmdp			Pointer to the command to be freed
 *
 * Output(s):	DDI_SUCCESS		Target successfully freed command
 *		DDI_FAILURE		Target failed to free command
 *
 * Description:	t1394_free_cmd() attempts to free a command that has previously
 *		been allocated by the target driver.  It is possible for
 *		t1394_free_cmd() to fail because the command is currently
 *		in-use by the 1394 Software Framework.
 */
/* ARGSUSED */
int
t1394_free_cmd(t1394_handle_t t1394_hdl, uint_t flags, cmd1394_cmd_t **cmdp)
{
	s1394_hal_t	 *hal;
	s1394_target_t	 *target;
	s1394_cmd_priv_t *s_priv;
	uint_t		 num_cmds;

	ASSERT(t1394_hdl != NULL);

	target = (s1394_target_t *)t1394_hdl;

	/* Find the HAL this target resides on */
	hal = target->on_hal;

	rw_enter(&hal->target_list_rwlock, RW_WRITER);

	/* How many cmds has this target allocated? */
	num_cmds = target->target_num_cmds;

	if (num_cmds == 0) {
		rw_exit(&hal->target_list_rwlock);
		ASSERT(num_cmds != 0);
		return (DDI_FAILURE);
	}

	/* Get the Services Layer private area */
	s_priv = S1394_GET_CMD_PRIV(*cmdp);

	/* Check that command isn't in use */
	if (s_priv->cmd_in_use == B_TRUE) {
		rw_exit(&hal->target_list_rwlock);
		ASSERT(s_priv->cmd_in_use == B_FALSE);
		return (DDI_FAILURE);
	}

	/* Decrement the number of cmds this target has allocated */
	target->target_num_cmds--;

	rw_exit(&hal->target_list_rwlock);

	/* Destroy the command's blocking condition variable */
	cv_destroy(&s_priv->blocking_cv);

	/* Destroy the command's blocking mutex */
	mutex_destroy(&s_priv->blocking_mutex);

	kmem_cache_free(hal->hal_kmem_cachep, *cmdp);

	/* Command pointer is set to NULL before returning */
	*cmdp = NULL;

	/* kstats - number of cmd frees */
	hal->hal_kstats->cmd_free++;

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_read()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		cmd			Pointer to the command to send
 *
 * Output(s):	DDI_SUCCESS		Target successful sent the command
 *		DDI_FAILURE		Target failed to send command
 *
 * Description:	t1394_read() attempts to send an asynchronous read request
 *		onto the 1394 bus.
 */
int
t1394_read(t1394_handle_t t1394_hdl, cmd1394_cmd_t *cmd)
{
	s1394_hal_t	  *to_hal;
	s1394_target_t	  *target;
	s1394_cmd_priv_t  *s_priv;
	s1394_hal_state_t state;
	int		  ret;
	int		  err;

	ASSERT(t1394_hdl != NULL);
	ASSERT(cmd != NULL);

	/* Get the Services Layer private area */
	s_priv = S1394_GET_CMD_PRIV(cmd);

	/* Is this command currently in use? */
	if (s_priv->cmd_in_use == B_TRUE) {
		ASSERT(s_priv->cmd_in_use == B_FALSE);
		return (DDI_FAILURE);
	}

	target = (s1394_target_t *)t1394_hdl;

	/* Set-up the destination of the command */
	to_hal = target->on_hal;

	/* No status (default) */
	cmd->cmd_result = CMD1394_NOSTATUS;

	/* Check for proper command type */
	if ((cmd->cmd_type != CMD1394_ASYNCH_RD_QUAD) &&
	    (cmd->cmd_type != CMD1394_ASYNCH_RD_BLOCK)) {
		cmd->cmd_result = CMD1394_EINVALID_COMMAND;
		return (DDI_FAILURE);
	}

	/* Is this a blocking command on interrupt stack? */
	if ((cmd->cmd_options & CMD1394_BLOCKING) &&
	    (servicing_interrupt())) {
		cmd->cmd_result = CMD1394_EINVALID_CONTEXT;
		return (DDI_FAILURE);
	}

	mutex_enter(&to_hal->topology_tree_mutex);
	state = to_hal->hal_state;
	if (state != S1394_HAL_NORMAL) {
		ret = s1394_HAL_asynch_error(to_hal, cmd, state);
		if (ret != CMD1394_CMDSUCCESS) {
			cmd->cmd_result = ret;
			mutex_exit(&to_hal->topology_tree_mutex);
			return (DDI_FAILURE);
		}
	}

	ret = s1394_setup_asynch_command(to_hal, target, cmd,
	    S1394_CMD_READ, &err);

	/* Command has now been put onto the queue! */
	if (ret != DDI_SUCCESS) {
		/* Copy error code into result */
		cmd->cmd_result = err;
		mutex_exit(&to_hal->topology_tree_mutex);
		return (DDI_FAILURE);
	}

	/*
	 * If this command was sent during a bus reset,
	 * then put it onto the pending Q.
	 */
	if (state == S1394_HAL_RESET) {
		/* Remove cmd from outstanding request Q */
		s1394_remove_q_asynch_cmd(to_hal, cmd);
		/* Are we on the bus reset event stack? */
		if (s1394_on_br_thread(to_hal) == B_TRUE) {
			/* Blocking commands are not allowed */
			if (cmd->cmd_options & CMD1394_BLOCKING) {
				mutex_exit(&to_hal->topology_tree_mutex);
				s_priv->cmd_in_use = B_FALSE;
				cmd->cmd_result	   = CMD1394_EINVALID_CONTEXT;
				return (DDI_FAILURE);
			}
		}

		s1394_pending_q_insert(to_hal, cmd, S1394_PENDING_Q_FRONT);
		mutex_exit(&to_hal->topology_tree_mutex);

		/* Block (if necessary) */
		goto block_on_asynch_cmd;
	}
	mutex_exit(&to_hal->topology_tree_mutex);

	/* Send the command out */
	ret = s1394_xfer_asynch_command(to_hal, cmd, &err);

	if (ret != DDI_SUCCESS) {
		if (err == CMD1394_ESTALE_GENERATION) {
			/* Remove cmd from outstanding request Q */
			s1394_remove_q_asynch_cmd(to_hal, cmd);
			s1394_pending_q_insert(to_hal, cmd,
			    S1394_PENDING_Q_FRONT);

			/* Block (if necessary) */
			goto block_on_asynch_cmd;

		} else {
			/* Remove cmd from outstanding request Q */
			s1394_remove_q_asynch_cmd(to_hal, cmd);

			s_priv->cmd_in_use = B_FALSE;

			/* Copy error code into result */
			cmd->cmd_result    = err;

			return (DDI_FAILURE);
		}
	} else {
		/* Block (if necessary) */
		goto block_on_asynch_cmd;
	}

block_on_asynch_cmd:
	s1394_block_on_asynch_cmd(cmd);

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_write()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		cmd			Pointer to the command to send
 *
 * Output(s):	DDI_SUCCESS		Target successful sent the command
 *		DDI_FAILURE		Target failed to send command
 *
 * Description:	t1394_write() attempts to send an asynchronous write request
 *		onto the 1394 bus.
 */
int
t1394_write(t1394_handle_t t1394_hdl, cmd1394_cmd_t *cmd)
{
	s1394_hal_t	  *to_hal;
	s1394_target_t	  *target;
	s1394_cmd_priv_t  *s_priv;
	s1394_hal_state_t state;
	int		  ret;
	int		  err;

	ASSERT(t1394_hdl != NULL);
	ASSERT(cmd != NULL);

	/* Get the Services Layer private area */
	s_priv = S1394_GET_CMD_PRIV(cmd);

	/* Is this command currently in use? */
	if (s_priv->cmd_in_use == B_TRUE) {
		ASSERT(s_priv->cmd_in_use == B_FALSE);
		return (DDI_FAILURE);
	}

	target = (s1394_target_t *)t1394_hdl;

	/* Set-up the destination of the command */
	to_hal = target->on_hal;

	/* Is this an FA request? */
	if (s_priv->cmd_ext_type == S1394_CMD_EXT_FA) {
		if (S1394_IS_CMD_FCP(s_priv) &&
		    (s1394_fcp_write_check_cmd(cmd) != DDI_SUCCESS)) {
			return (DDI_FAILURE);
		}
		s1394_fa_convert_cmd(to_hal, cmd);
	}

	/* No status (default) */
	cmd->cmd_result = CMD1394_NOSTATUS;

	/* Check for proper command type */
	if ((cmd->cmd_type != CMD1394_ASYNCH_WR_QUAD) &&
	    (cmd->cmd_type != CMD1394_ASYNCH_WR_BLOCK)) {
		cmd->cmd_result = CMD1394_EINVALID_COMMAND;
		s1394_fa_check_restore_cmd(to_hal, cmd);
		return (DDI_FAILURE);
	}

	/* Is this a blocking command on interrupt stack? */
	if ((cmd->cmd_options & CMD1394_BLOCKING) &&
	    (servicing_interrupt())) {
		cmd->cmd_result = CMD1394_EINVALID_CONTEXT;
		s1394_fa_check_restore_cmd(to_hal, cmd);
		return (DDI_FAILURE);
	}

	mutex_enter(&to_hal->topology_tree_mutex);
	state = to_hal->hal_state;
	if (state != S1394_HAL_NORMAL) {
		ret = s1394_HAL_asynch_error(to_hal, cmd, state);
		if (ret != CMD1394_CMDSUCCESS) {
			cmd->cmd_result = ret;
			mutex_exit(&to_hal->topology_tree_mutex);
			s1394_fa_check_restore_cmd(to_hal, cmd);
			return (DDI_FAILURE);
		}
	}

	ret = s1394_setup_asynch_command(to_hal, target, cmd,
	    S1394_CMD_WRITE, &err);

	/* Command has now been put onto the queue! */
	if (ret != DDI_SUCCESS) {
		/* Copy error code into result */
		cmd->cmd_result = err;
		mutex_exit(&to_hal->topology_tree_mutex);
		s1394_fa_check_restore_cmd(to_hal, cmd);
		return (DDI_FAILURE);
	}

	/*
	 * If this command was sent during a bus reset,
	 * then put it onto the pending Q.
	 */
	if (state == S1394_HAL_RESET) {
		/* Remove cmd from outstanding request Q */
		s1394_remove_q_asynch_cmd(to_hal, cmd);
		/* Are we on the bus reset event stack? */
		if (s1394_on_br_thread(to_hal) == B_TRUE) {
			/* Blocking commands are not allowed */
			if (cmd->cmd_options & CMD1394_BLOCKING) {
				mutex_exit(&to_hal->topology_tree_mutex);
				s_priv->cmd_in_use = B_FALSE;
				cmd->cmd_result    = CMD1394_EINVALID_CONTEXT;
				s1394_fa_check_restore_cmd(to_hal, cmd);
				return (DDI_FAILURE);
			}
		}

		s1394_pending_q_insert(to_hal, cmd, S1394_PENDING_Q_FRONT);
		mutex_exit(&to_hal->topology_tree_mutex);

		/* Block (if necessary) */
		s1394_block_on_asynch_cmd(cmd);

		return (DDI_SUCCESS);
	}
	mutex_exit(&to_hal->topology_tree_mutex);

	/* Send the command out */
	ret = s1394_xfer_asynch_command(to_hal, cmd, &err);

	if (ret != DDI_SUCCESS) {
		if (err == CMD1394_ESTALE_GENERATION) {
			/* Remove cmd from outstanding request Q */
			s1394_remove_q_asynch_cmd(to_hal, cmd);
			s1394_pending_q_insert(to_hal, cmd,
			    S1394_PENDING_Q_FRONT);

			/* Block (if necessary) */
			s1394_block_on_asynch_cmd(cmd);

			return (DDI_SUCCESS);
		} else {
			/* Remove cmd from outstanding request Q */
			s1394_remove_q_asynch_cmd(to_hal, cmd);

			s_priv->cmd_in_use = B_FALSE;

			/* Copy error code into result */
			cmd->cmd_result = err;

			s1394_fa_check_restore_cmd(to_hal, cmd);
			return (DDI_FAILURE);
		}
	} else {
		/* Block (if necessary) */
		s1394_block_on_asynch_cmd(cmd);

		return (DDI_SUCCESS);
	}
}

/*
 * Function:    t1394_lock()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		cmd			Pointer to the command to send
 *
 * Output(s):	DDI_SUCCESS		Target successful sent the command
 *		DDI_FAILURE		Target failed to send command
 *
 * Description:	t1394_lock() attempts to send an asynchronous lock request
 *		onto the 1394 bus.
 */
int
t1394_lock(t1394_handle_t t1394_hdl, cmd1394_cmd_t *cmd)
{
	s1394_hal_t	    *to_hal;
	s1394_target_t	    *target;
	s1394_cmd_priv_t    *s_priv;
	s1394_hal_state_t   state;
	cmd1394_lock_type_t lock_type;
	uint_t		    num_retries;
	int		    ret;

	ASSERT(t1394_hdl != NULL);
	ASSERT(cmd != NULL);

	/* Get the Services Layer private area */
	s_priv = S1394_GET_CMD_PRIV(cmd);

	/* Is this command currently in use? */
	if (s_priv->cmd_in_use == B_TRUE) {
		ASSERT(s_priv->cmd_in_use == B_FALSE);
		return (DDI_FAILURE);
	}

	target = (s1394_target_t *)t1394_hdl;

	/* Set-up the destination of the command */
	to_hal = target->on_hal;

	mutex_enter(&to_hal->topology_tree_mutex);
	state = to_hal->hal_state;
	if (state != S1394_HAL_NORMAL) {
		ret = s1394_HAL_asynch_error(to_hal, cmd, state);
		if (ret != CMD1394_CMDSUCCESS) {
			cmd->cmd_result = ret;
			mutex_exit(&to_hal->topology_tree_mutex);
			return (DDI_FAILURE);
		}
	}
	mutex_exit(&to_hal->topology_tree_mutex);

	/* Check for proper command type */
	if ((cmd->cmd_type != CMD1394_ASYNCH_LOCK_32) &&
	    (cmd->cmd_type != CMD1394_ASYNCH_LOCK_64)) {
		cmd->cmd_result = CMD1394_EINVALID_COMMAND;
		return (DDI_FAILURE);
	}

	/* No status (default) */
	cmd->cmd_result = CMD1394_NOSTATUS;

	/* Is this a blocking command on interrupt stack? */
	if ((cmd->cmd_options & CMD1394_BLOCKING) &&
	    (servicing_interrupt())) {
		cmd->cmd_result = CMD1394_EINVALID_CONTEXT;
		return (DDI_FAILURE);
	}

	if (cmd->cmd_type == CMD1394_ASYNCH_LOCK_32) {
		lock_type	= cmd->cmd_u.l32.lock_type;
		num_retries	= cmd->cmd_u.l32.num_retries;
	} else {	/* (cmd->cmd_type == CMD1394_ASYNCH_LOCK_64) */
		lock_type	= cmd->cmd_u.l64.lock_type;
		num_retries	= cmd->cmd_u.l64.num_retries;
	}

	/* Make sure num_retries is reasonable */
	ASSERT(num_retries <= MAX_NUMBER_OF_LOCK_RETRIES);

	switch (lock_type) {
	case CMD1394_LOCK_MASK_SWAP:
	case CMD1394_LOCK_FETCH_ADD:
	case CMD1394_LOCK_LITTLE_ADD:
	case CMD1394_LOCK_BOUNDED_ADD:
	case CMD1394_LOCK_WRAP_ADD:
	case CMD1394_LOCK_COMPARE_SWAP:
		ret = s1394_compare_swap(to_hal, target, cmd);
		break;

	case CMD1394_LOCK_BIT_AND:
	case CMD1394_LOCK_BIT_OR:
	case CMD1394_LOCK_BIT_XOR:
	case CMD1394_LOCK_INCREMENT:
	case CMD1394_LOCK_DECREMENT:
	case CMD1394_LOCK_ADD:
	case CMD1394_LOCK_SUBTRACT:
	case CMD1394_LOCK_THRESH_ADD:
	case CMD1394_LOCK_THRESH_SUBTRACT:
	case CMD1394_LOCK_CLIP_ADD:
	case CMD1394_LOCK_CLIP_SUBTRACT:
		ret = s1394_split_lock_req(to_hal, target, cmd);
		break;

	default:
		cmd->cmd_result = CMD1394_EINVALID_COMMAND;
		ret = DDI_FAILURE;
		break;
	}

	return (ret);
}

/*
 * Function:    t1394_alloc_addr()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		addr_allocp		The structure used to specify the type,
 *					    size, permissions, and callbacks
 *					    (if any) for the requested block
 *					    of 1394 address space
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	result			Used to pass more specific info back
 *					    to target
 *
 * Description:	t1394_alloc_addr() requests that part of the 1394 Address Space
 *		on the local node be set aside for this target driver, and
 *		associated with this address space should be some permissions
 *		and callbacks.  If the request is unable to be fulfilled,
 *		t1394_alloc_addr() will return DDI_FAILURE and result will
 *		indicate the reason.  T1394_EINVALID_PARAM indicates that the
 *		combination of flags given is invalid, and T1394_EALLOC_ADDR
 *		indicates that the requested type of address space is
 *		unavailable.
 */
/* ARGSUSED */
int
t1394_alloc_addr(t1394_handle_t t1394_hdl, t1394_alloc_addr_t *addr_allocp,
    uint_t flags, int *result)
{
	s1394_hal_t	*hal;
	s1394_target_t	*target;
	uint64_t	addr_lo;
	uint64_t	addr_hi;
	int		err;

	ASSERT(t1394_hdl != NULL);
	ASSERT(addr_allocp != NULL);

	target = (s1394_target_t *)t1394_hdl;

	/* Find the HAL this target resides on */
	hal = target->on_hal;

	/* Get the bounds of the request */
	addr_lo = addr_allocp->aa_address;
	addr_hi = addr_lo + addr_allocp->aa_length;

	/* Check combination of flags */
	if ((addr_allocp->aa_enable & T1394_ADDR_RDENBL) &&
	    (addr_allocp->aa_evts.recv_read_request == NULL) &&
	    (addr_allocp->aa_kmem_bufp == NULL)) {
		if ((addr_allocp->aa_type != T1394_ADDR_FIXED)	||
		    (addr_lo < hal->physical_addr_lo)		||
		    (addr_hi > hal->physical_addr_hi)) {

			/*
			 * Reads are enabled, but target doesn't want to
			 * be notified and hasn't given backing store
			 */
			*result = T1394_EINVALID_PARAM;

			/* kstats - addr alloc failures */
			hal->hal_kstats->addr_alloc_fail++;
			return (DDI_FAILURE);
		} else {
			addr_allocp->aa_enable &= ~T1394_ADDR_RDENBL;
		}
	}

	if ((addr_allocp->aa_enable & T1394_ADDR_WRENBL) &&
	    (addr_allocp->aa_evts.recv_write_request == NULL) &&
	    (addr_allocp->aa_kmem_bufp == NULL)) {
		if ((addr_allocp->aa_type != T1394_ADDR_FIXED)	||
		    (addr_lo < hal->physical_addr_lo)		||
		    (addr_hi > hal->physical_addr_hi)) {

			/*
			 * Writes are enabled, but target doesn't want to
			 * be notified and hasn't given backing store
			 */
			*result = T1394_EINVALID_PARAM;

			/* kstats - addr alloc failures */
			hal->hal_kstats->addr_alloc_fail++;
			return (DDI_FAILURE);
		} else {
			addr_allocp->aa_enable &= ~T1394_ADDR_WRENBL;
		}
	}

	if ((addr_allocp->aa_enable & T1394_ADDR_LKENBL) &&
	    (addr_allocp->aa_evts.recv_lock_request == NULL) &&
	    (addr_allocp->aa_kmem_bufp == NULL)) {
		if ((addr_allocp->aa_type != T1394_ADDR_FIXED)	||
		    (addr_lo < hal->physical_addr_lo)		||
		    (addr_hi > hal->physical_addr_hi)) {

			/*
			 * Locks are enabled, but target doesn't want to
			 * be notified and hasn't given backing store
			 */
			*result = T1394_EINVALID_PARAM;

			/* kstats - addr alloc failures */
			hal->hal_kstats->addr_alloc_fail++;
			return (DDI_FAILURE);
		} else {
			addr_allocp->aa_enable &= ~T1394_ADDR_LKENBL;
		}
	}

	/* If not T1394_ADDR_FIXED, then allocate a block */
	if (addr_allocp->aa_type != T1394_ADDR_FIXED) {
		err = s1394_request_addr_blk((s1394_hal_t *)target->on_hal,
					addr_allocp);
		if (err != DDI_SUCCESS) {
			*result = T1394_EALLOC_ADDR;
			/* kstats - addr alloc failures */
			hal->hal_kstats->addr_alloc_fail++;
		} else {
			*result = T1394_NOERROR;
		}
		return (err);
	} else {
		err = s1394_claim_addr_blk((s1394_hal_t *)target->on_hal,
					addr_allocp);
		if (err != DDI_SUCCESS) {
			*result = T1394_EALLOC_ADDR;
			/* kstats - addr alloc failures */
			hal->hal_kstats->addr_alloc_fail++;
		} else {
			*result = T1394_NOERROR;
			/* If physical, update the AR request counter */
			if ((addr_lo >= hal->physical_addr_lo) &&
			    (addr_hi <= hal->physical_addr_hi)) {
				rw_enter(&hal->target_list_rwlock, RW_WRITER);
				target->physical_arreq_enabled++;
				rw_exit(&hal->target_list_rwlock);

				s1394_physical_arreq_set_one(target);
			}
		}
		return (err);
	}
}

/*
 * Function:    t1394_free_addr()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		addr_hdl		The address "handle" returned by the
 *					   the t1394_alloc_addr() routine
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	DDI_SUCCESS		Target successfully freed memory
 *		DDI_FAILURE		Target failed to free the memory block
 *
 * Description:	t1394_free_addr() attempts to free up memory that has been
 *		allocated by the target using t1394_alloc_addr().
 */
/* ARGSUSED */
int
t1394_free_addr(t1394_handle_t t1394_hdl, t1394_addr_handle_t *addr_hdl,
    uint_t flags)
{
	s1394_addr_space_blk_t	*curr_blk;
	s1394_hal_t		*hal;
	s1394_target_t		*target;

	ASSERT(t1394_hdl != NULL);
	ASSERT(addr_hdl != NULL);

	target = (s1394_target_t *)t1394_hdl;

	/* Find the HAL this target resides on */
	hal = target->on_hal;

	curr_blk = (s1394_addr_space_blk_t *)(*addr_hdl);

	if (s1394_free_addr_blk(hal, curr_blk) != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	/* If physical, update the AR request counter */
	if (curr_blk->addr_type == T1394_ADDR_FIXED) {
		target->physical_arreq_enabled--;
		s1394_physical_arreq_clear_one(target);
	}

	*addr_hdl = NULL;

	/* kstats - number of addr frees */
	hal->hal_kstats->addr_space_free++;

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_recv_request_done()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		resp			Pointer to the command which the
 *					    target received in it's callback
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	DDI_SUCCESS		Target successfully returned command
 *					    to the 1394 Software Framework,
 *					    and, if necessary, sent response
 *		DDI_FAILURE		Target failed to return the command to
 *					    the 1394 Software Framework
 *
 * Description:	t1394_recv_request_done() takes the command that is given and
 *		determines whether that command requires a response to be
 *		sent on the 1394 bus.  If it is necessary and it's response
 *		code (cmd_result) has been set appropriately, then a response
 *		will be sent.  If no response is necessary (broadcast or
 *		posted write), then the command resources are reclaimed.
 */
/* ARGSUSED */
int
t1394_recv_request_done(t1394_handle_t t1394_hdl, cmd1394_cmd_t *resp,
    uint_t flags)
{
	s1394_hal_t	 *hal;
	s1394_cmd_priv_t *s_priv;
	h1394_cmd_priv_t *h_priv;
	mblk_t		 *curr_blk;
	size_t		 msgb_len;
	size_t		 size;
	int		 ret;
	boolean_t	 response = B_TRUE;
	boolean_t	 posted_write = B_FALSE;
	boolean_t	 write_cmd = B_FALSE;
	boolean_t	 mblk_too_small;

	ASSERT(t1394_hdl != NULL);
	ASSERT(resp != NULL);

	/* Find the HAL this target resides on */
	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	/* Get the Services Layer private area */
	s_priv = S1394_GET_CMD_PRIV(resp);

	/* Get a pointer to the HAL private struct */
	h_priv = (h1394_cmd_priv_t *)&s_priv->hal_cmd_private;

	/* Is this an FA request? */
	if (s_priv->cmd_ext_type == S1394_CMD_EXT_FA) {
		s1394_fa_convert_cmd(hal, resp);
	}

	/* Is this a write request? */
	if ((resp->cmd_type == CMD1394_ASYNCH_WR_QUAD) ||
	    (resp->cmd_type == CMD1394_ASYNCH_WR_BLOCK)) {
		write_cmd = B_TRUE;
		/* Is this a posted write request? */
		posted_write = s_priv->posted_write;
	}

	/* If broadcast or posted write cmd, don't send response */
	if ((resp->broadcast == 1) ||
	    ((write_cmd == B_TRUE) && (posted_write == B_TRUE)))
		response = B_FALSE;

	if (response == B_FALSE) {
		if ((write_cmd == B_TRUE) && (posted_write == B_TRUE)) {
			/* kstats - Posted Write error */
			hal->hal_kstats->arreq_posted_write_error++;
		}

		/* Free the command - Pass it back to the HAL */
		HAL_CALL(hal).response_complete(hal->halinfo.hal_private, resp,
		    h_priv);
		return (DDI_SUCCESS);
	}

	ASSERT(response == B_TRUE);

	/* Verify valid response code */
	switch (resp->cmd_result) {
	case IEEE1394_RESP_COMPLETE:
		/* Is the mblk_t too small? */
		if (resp->cmd_type == CMD1394_ASYNCH_RD_BLOCK) {
			curr_blk = resp->cmd_u.b.data_block;
			size	 = resp->cmd_u.b.blk_length;
			msgb_len = 0;
			mblk_too_small = B_TRUE;

			if (curr_blk == NULL) {
				/*
				 * Free the command - Pass it back
				 * to the HAL
				 */
				HAL_CALL(hal).response_complete(
				    hal->halinfo.hal_private, resp, h_priv);
				ASSERT(curr_blk != NULL);
				return (DDI_FAILURE);
			}

			while (curr_blk != NULL) {
				msgb_len +=
				    (curr_blk->b_wptr - curr_blk->b_rptr);

				if (msgb_len >= size) {
					mblk_too_small = B_FALSE;
					break;
				}
				curr_blk = curr_blk->b_cont;
			}

			if (mblk_too_small == B_TRUE) {
				/*
				 * Free the command - Pass it back
				 * to the HAL
				 */
				HAL_CALL(hal).response_complete(
				    hal->halinfo.hal_private, resp, h_priv);
				ASSERT(mblk_too_small != B_TRUE);
				return (DDI_FAILURE);
			}
		}
		/* FALLTHROUGH */
	case IEEE1394_RESP_CONFLICT_ERROR:
	case IEEE1394_RESP_DATA_ERROR:
	case IEEE1394_RESP_TYPE_ERROR:
	case IEEE1394_RESP_ADDRESS_ERROR:
		ret = s1394_send_response(hal, resp);
		return (ret);

	default:
		return (DDI_FAILURE);
	}
}


/*
 * Function:    t1394_fcp_register_controller()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		evts			The structure in which the target
 *					    specifies its callback routines
 *
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	DDI_SUCCESS		Successfully registered.
 *
 *		DDI_FAILURE		Not registered due to failure.
 *
 * Description:	Used to register the target within the Framework as an FCP
 *		controller.
 */
/* ARGSUSED */
int
t1394_fcp_register_controller(t1394_handle_t t1394_hdl, t1394_fcp_evts_t *evts,
    uint_t flags)
{
	int		result;

	ASSERT(t1394_hdl != NULL);

	result = s1394_fcp_register_ctl((s1394_target_t *)t1394_hdl, evts);

	return (result);
}

/*
 * Function:    t1394_fcp_unregister_controller()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *
 * Output(s):	DDI_SUCCESS		Successfully unregistered.
 *
 *		DDI_FAILURE		Not unregistered due to failure.
 *
 * Description:	Used to unregister the target within the Framework as an FCP
 *		controller.
 */
int
t1394_fcp_unregister_controller(t1394_handle_t t1394_hdl)
{
	int		result;

	ASSERT(t1394_hdl != NULL);

	result = s1394_fcp_unregister_ctl((s1394_target_t *)t1394_hdl);

	return (result);
}

/*
 * Function:    t1394_fcp_register_target()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		evts			The structure in which the target
 *					    specifies its callback routines
 *
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	DDI_SUCCESS		Successfully registered.
 *
 *		DDI_FAILURE		Not registered due to failure.
 *
 * Description:	Used to register the target within the Framework as an FCP
 *		target.
 */
/* ARGSUSED */
int
t1394_fcp_register_target(t1394_handle_t t1394_hdl, t1394_fcp_evts_t *evts,
    uint_t flags)
{
	int		result;

	ASSERT(t1394_hdl != NULL);

	result = s1394_fcp_register_tgt((s1394_target_t *)t1394_hdl, evts);

	return (result);
}

/*
 * Function:    t1394_fcp_unregister_target()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *
 * Output(s):	DDI_SUCCESS		Successfully unregistered.
 *
 *		DDI_FAILURE		Not unregistered due to failure.
 *
 * Description:	Used to unregister the target within the Framework as an FCP
 *		target.
 */
int
t1394_fcp_unregister_target(t1394_handle_t t1394_hdl)
{
	int		result;

	ASSERT(t1394_hdl != NULL);

	result = s1394_fcp_unregister_tgt((s1394_target_t *)t1394_hdl);

	return (result);
}

/*
 * Function:    t1394_cmp_register()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		evts			The structure in which the target
 *					    specifies its callback routines
 *
 * Output(s):	DDI_SUCCESS		Successfully registered.
 *
 *		DDI_FAILURE		Not registered due to failure.
 *
 * Description:	Used to register the target within the Framework as a CMP
 *		device.
 */
/* ARGSUSED */
int
t1394_cmp_register(t1394_handle_t t1394_hdl, t1394_cmp_evts_t *evts,
    uint_t flags)
{
	int		result;

	ASSERT(t1394_hdl != NULL);

	result = s1394_cmp_register((s1394_target_t *)t1394_hdl, evts);

	return (result);
}

/*
 * Function:    t1394_cmp_unregister()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		evts			The structure in which the target
 *					    specifies its callback routines
 *
 * Output(s):	DDI_SUCCESS		Successfully registered.
 *
 *		DDI_FAILURE		Not registered due to failure.
 *
 * Description:	Used to unregister the target within the Framework as a CMP
 *		device.
 */
int
t1394_cmp_unregister(t1394_handle_t t1394_hdl)
{
	int		result;

	ASSERT(t1394_hdl != NULL);

	result = s1394_cmp_unregister((s1394_target_t *)t1394_hdl);

	return (result);
}

/*
 * Function:    t1394_cmp_read()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		reg			Register type.
 *		valp			Returned register value.
 *
 * Output(s):	DDI_SUCCESS		Successfully registered.
 *
 *		DDI_FAILURE		Not registered due to failure.
 *
 * Description:	Used to read a CMP register value.
 */
int
t1394_cmp_read(t1394_handle_t t1394_hdl, t1394_cmp_reg_t reg, uint32_t *valp)
{
	int		result;

	ASSERT(t1394_hdl != NULL);

	result = s1394_cmp_read((s1394_target_t *)t1394_hdl, reg, valp);

	return (result);
}

/*
 * Function:    t1394_cmp_cas()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		reg			Register type.
 *		arg_val			Compare argument.
 *		new_val			New register value.
 *		old_valp		Returned original register value.
 *
 * Output(s):	DDI_SUCCESS		Successfully registered.
 *
 *		DDI_FAILURE		Not registered due to failure.
 *
 * Description:	Used to compare-swap a CMP register value.
 */
int
t1394_cmp_cas(t1394_handle_t t1394_hdl, t1394_cmp_reg_t reg, uint32_t arg_val,
    uint32_t new_val, uint32_t *old_valp)
{
	int		result;

	ASSERT(t1394_hdl != NULL);

	result = s1394_cmp_cas((s1394_target_t *)t1394_hdl, reg, arg_val,
				new_val, old_valp);

	return (result);
}

/*
 * Function:    t1394_alloc_isoch_single()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		sii			The structure used to set up the
 *					    overall characteristics of the
 *					    isochronous stream
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	setup_args		Contains the channel number that was
 *					    allocated
 *		t1394_single_hdl	This in the isoch "handle" used in
 *					    t1394_free_isoch_single()
 *		result			Used to pass more specific info back
 *					    to target
 *
 * Description:	t1394_alloc_isoch_single() is used to direct the 1394 Software
 *		Framework to allocate an isochronous channel and bandwidth
 *		from the Isochronous Resource Manager (IRM).  If a bus reset
 *		occurs, the 1394 Software Framework attempts to reallocate the
 *		same resources, calling the rsrc_fail_target() callback if
 *		it is unsuccessful.
 */
/* ARGSUSED */
int
t1394_alloc_isoch_single(t1394_handle_t t1394_hdl,
    t1394_isoch_singleinfo_t *sii, uint_t flags,
    t1394_isoch_single_out_t *output_args,
    t1394_isoch_single_handle_t	*t1394_single_hdl, int *result)
{
	s1394_hal_t		*hal;
	s1394_isoch_cec_t	*cec_new;
	t1394_join_isochinfo_t	jii;
	int			ret;
	int			err;

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_single_hdl != NULL);
	ASSERT(sii != NULL);

	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	/* Check for invalid channel_mask */
	if (sii->si_channel_mask == 0) {
		return (DDI_FAILURE);
	}

	/* Check for invalid bandwidth */
	if ((sii->si_bandwidth <= IEEE1394_BANDWIDTH_MIN) ||
	    (sii->si_bandwidth > IEEE1394_BANDWIDTH_MAX)) {
		return (DDI_FAILURE);
	}

	/* Verify that rsrc_fail_target() callback is non-NULL */
	if (sii->rsrc_fail_target == NULL) {
		return (DDI_FAILURE);
	}

	/*
	 * Allocate an Isoch CEC of type S1394_SINGLE
	 */

	/* Allocate the Isoch CEC structure */
	cec_new = kmem_zalloc(sizeof (s1394_isoch_cec_t), KM_SLEEP);

	/* Initialize the structure type */
	cec_new->cec_type = S1394_SINGLE;

	/* Create the mutex and "in_callbacks" cv */
	mutex_init(&cec_new->isoch_cec_mutex, NULL, MUTEX_DRIVER,
	    hal->halinfo.hw_interrupt);
	cv_init(&cec_new->in_callbacks_cv, NULL, CV_DRIVER,
	    hal->halinfo.hw_interrupt);

	/* Initialize the Isoch CEC's member list */
	cec_new->cec_member_list_head = NULL;
	cec_new->cec_member_list_tail = NULL;

	/* Initialize the filters */
	cec_new->filter_min_speed	= sii->si_speed;
	cec_new->filter_max_speed	= sii->si_speed;
	cec_new->filter_current_speed	= cec_new->filter_max_speed;
	cec_new->filter_channel_mask	= sii->si_channel_mask;
	cec_new->bandwidth		= sii->si_bandwidth;
	cec_new->state_transitions	= ISOCH_CEC_FREE | ISOCH_CEC_JOIN |
					    ISOCH_CEC_SETUP;

	mutex_enter(&hal->isoch_cec_list_mutex);

	/* Insert Isoch CEC into the HAL's list */
	s1394_isoch_cec_list_insert(hal, cec_new);

	mutex_exit(&hal->isoch_cec_list_mutex);

	/*
	 * Join the newly created Isoch CEC
	 */
	jii.req_channel_mask	= sii->si_channel_mask;
	jii.req_max_speed	= sii->si_speed;
	jii.jii_options		= T1394_TALKER;
	jii.isoch_cec_evts_arg	= sii->single_evt_arg;

	/* All events are NULL except rsrc_fail_target() */
	jii.isoch_cec_evts.setup_target	    = NULL;
	jii.isoch_cec_evts.start_target	    = NULL;
	jii.isoch_cec_evts.stop_target	    = NULL;
	jii.isoch_cec_evts.stop_target	    = NULL;
	jii.isoch_cec_evts.teardown_target  = NULL;
	jii.isoch_cec_evts.rsrc_fail_target = sii->rsrc_fail_target;

	ret = t1394_join_isoch_cec(t1394_hdl,
	    (t1394_isoch_cec_handle_t)cec_new, 0, &jii);

	if (ret != DDI_SUCCESS) {
		ret = t1394_free_isoch_cec(t1394_hdl, flags,
		    (t1394_isoch_cec_handle_t *)&cec_new);
		if (ret != DDI_SUCCESS) {
			/* Unable to free the Isoch CEC */
			ASSERT(0);
		}

		/* Handle is nulled out before returning */
		*t1394_single_hdl = NULL;

		return (DDI_FAILURE);
	}

	/*
	 * Setup the isoch resources, etc.
	 */
	ret = t1394_setup_isoch_cec(t1394_hdl,
	    (t1394_isoch_cec_handle_t)cec_new, 0, &err);

	if (ret != DDI_SUCCESS) {
		*result = err;

		/* Leave the Isoch CEC */
		ret = t1394_leave_isoch_cec(t1394_hdl,
		    (t1394_isoch_cec_handle_t)cec_new, 0);
		if (ret != DDI_SUCCESS) {
			/* Unable to leave the Isoch CEC */
			ASSERT(0);
		}

		/* Free up the Isoch CEC */
		ret = t1394_free_isoch_cec(t1394_hdl, flags,
		    (t1394_isoch_cec_handle_t *)&cec_new);
		if (ret != DDI_SUCCESS) {
			/* Unable to free the Isoch CEC */
			ASSERT(0);
		}

		/* Handle is nulled out before returning */
		*t1394_single_hdl = NULL;

		return (DDI_FAILURE);
	}

	/* Return the setup_args - channel num and speed */
	mutex_enter(&cec_new->isoch_cec_mutex);
	output_args->channel_num  = cec_new->realloc_chnl_num;
	mutex_exit(&cec_new->isoch_cec_mutex);

	/* Update the handle */
	*t1394_single_hdl = (t1394_isoch_single_handle_t)cec_new;

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_free_isoch_single()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		t1394_single_hdl	The isoch "handle" return by
 *					    t1394_alloc_isoch_single()
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	None
 *
 * Description:	t1394_free_isoch_single() frees the isochronous resources
 *		and the handle that were allocated during the call to
 *		t1394_alloc_isoch_single().
 */
/* ARGSUSED */
void
t1394_free_isoch_single(t1394_handle_t t1394_hdl,
    t1394_isoch_single_handle_t *t1394_single_hdl, uint_t flags)
{
	s1394_isoch_cec_t *cec_curr;
	int		  ret;

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_single_hdl != NULL);

	/* Convert the handle to an Isoch CEC pointer */
	cec_curr = (s1394_isoch_cec_t *)(*t1394_single_hdl);

	/*
	 * Teardown the isoch resources, etc.
	 */
	ret = t1394_teardown_isoch_cec(t1394_hdl,
	    (t1394_isoch_cec_handle_t)cec_curr, 0);
	if (ret != DDI_SUCCESS) {
		/* Unable to teardown the Isoch CEC */
		ASSERT(0);
	}

	/*
	 * Leave the Isoch CEC
	 */
	ret = t1394_leave_isoch_cec(t1394_hdl,
	    (t1394_isoch_cec_handle_t)cec_curr, 0);
	if (ret != DDI_SUCCESS) {
		/* Unable to leave the Isoch CEC */
		ASSERT(0);
	}

	/*
	 * Free the Isoch CEC
	 */
	ret = t1394_free_isoch_cec(t1394_hdl, flags,
	    (t1394_isoch_cec_handle_t *)&cec_curr);
	if (ret != DDI_SUCCESS) {
		/* Unable to free the Isoch CEC */
		ASSERT(0);
	}

	/* Handle is nulled out before returning */
	*t1394_single_hdl = NULL;
}

/*
 * Function:    t1394_alloc_isoch_cec()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		props			The structure used to set up the
 *					    overall characteristics of for
 *					    the Isoch CEC.
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	t1394_isoch_cec_hdl	The Isoch CEC "handle" used in all
 *					    subsequent isoch_cec() calls
 *
 * Description:	t1394_alloc_isoch_cec() allocates and initializes an
 *		isochronous channel event coordinator (Isoch CEC) for use
 *		in managing and coordinating activity for an isoch channel
 */
/* ARGSUSED */
int
t1394_alloc_isoch_cec(t1394_handle_t t1394_hdl, t1394_isoch_cec_props_t *props,
    uint_t flags, t1394_isoch_cec_handle_t *t1394_isoch_cec_hdl)
{
	s1394_hal_t	  *hal;
	s1394_isoch_cec_t *cec_new;
	uint64_t	  temp;

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_isoch_cec_hdl != NULL);
	ASSERT(props != NULL);

	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	/* Check for invalid channel_mask */
	if (props->cec_channel_mask == 0) {
		return (DDI_FAILURE);
	}

	/* Test conditions specific to T1394_NO_IRM_ALLOC */
	temp = props->cec_channel_mask;
	if (props->cec_options & T1394_NO_IRM_ALLOC) {
		/* If T1394_NO_IRM_ALLOC, then only one bit should be set */
		if (!ISP2(temp)) {
			return (DDI_FAILURE);
		}

		/* If T1394_NO_IRM_ALLOC, then speeds should be equal */
		if (props->cec_min_speed != props->cec_max_speed) {
			return (DDI_FAILURE);
		}
	}

	/* Check for invalid bandwidth */
	if ((props->cec_bandwidth <= IEEE1394_BANDWIDTH_MIN) ||
	    (props->cec_bandwidth > IEEE1394_BANDWIDTH_MAX)) {
		return (DDI_FAILURE);
	}

	/* Allocate the Isoch CEC structure */
	cec_new = kmem_zalloc(sizeof (s1394_isoch_cec_t), KM_SLEEP);

	/* Initialize the structure type */
	cec_new->cec_type = S1394_PEER_TO_PEER;

	/* Create the mutex and "in_callbacks" cv */
	mutex_init(&cec_new->isoch_cec_mutex, NULL, MUTEX_DRIVER,
	    hal->halinfo.hw_interrupt);
	cv_init(&cec_new->in_callbacks_cv, NULL, CV_DRIVER,
	    hal->halinfo.hw_interrupt);

	/* Initialize the Isoch CEC's member list */
	cec_new->cec_member_list_head	= NULL;
	cec_new->cec_member_list_tail	= NULL;

	/* Initialize the filters */
	cec_new->filter_min_speed	= props->cec_min_speed;
	cec_new->filter_max_speed	= props->cec_max_speed;
	cec_new->filter_current_speed	= cec_new->filter_max_speed;
	cec_new->filter_channel_mask	= props->cec_channel_mask;
	cec_new->bandwidth		= props->cec_bandwidth;
	cec_new->cec_options		= props->cec_options;
	cec_new->state_transitions	= ISOCH_CEC_FREE | ISOCH_CEC_JOIN |
					    ISOCH_CEC_SETUP;

	mutex_enter(&hal->isoch_cec_list_mutex);

	/* Insert Isoch CEC into the HAL's list */
	s1394_isoch_cec_list_insert(hal, cec_new);

	mutex_exit(&hal->isoch_cec_list_mutex);

	/* Update the handle and return */
	*t1394_isoch_cec_hdl = (t1394_isoch_cec_handle_t)cec_new;

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_free_isoch_cec()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		flags			The flags parameter is unused (for now)
 *		t1394_isoch_cec_hdl	The Isoch CEC "handle" returned by
 *					    t1394_alloc_isoch_cec()
 *
 * Output(s):	DDI_SUCCESS		Target successfully freed the Isoch CEC
 *		DDI_FAILURE		Target failed to free the Isoch CEC
 *
 * Description:	t1394_free_isoch_cec() attempts to free the Isoch CEC
 *		structure.  It will fail (DDI_FAILURE) if there are any
 *		remaining members who have not yet left.
 */
/* ARGSUSED */
int
t1394_free_isoch_cec(t1394_handle_t t1394_hdl, uint_t flags,
    t1394_isoch_cec_handle_t *t1394_isoch_cec_hdl)
{
	s1394_hal_t	  *hal;
	s1394_isoch_cec_t *cec_curr;

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_isoch_cec_hdl != NULL);

	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	/* Convert the handle to an Isoch CEC pointer */
	cec_curr = (s1394_isoch_cec_t *)(*t1394_isoch_cec_hdl);

	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

	/* Are we in any callbacks? */
	if (CEC_IN_ANY_CALLBACKS(cec_curr)) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* Is "free" a legal state transition? */
	if (CEC_TRANSITION_LEGAL(cec_curr, ISOCH_CEC_FREE) == 0) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}
	mutex_exit(&cec_curr->isoch_cec_mutex);

	mutex_enter(&hal->isoch_cec_list_mutex);

	/* Remove Isoch CEC from HAL's list */
	s1394_isoch_cec_list_remove(hal, cec_curr);

	mutex_exit(&hal->isoch_cec_list_mutex);

	/* Destroy the Isoch CEC's mutex and cv */
	cv_destroy(&cec_curr->in_callbacks_cv);
	mutex_destroy(&cec_curr->isoch_cec_mutex);

	/* Free up the memory for the Isoch CEC struct */
	kmem_free(cec_curr, sizeof (s1394_isoch_cec_t));

	/* Update the handle and return */
	*t1394_isoch_cec_hdl = NULL;

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_join_isoch_cec()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		t1394_isoch_cec_hdl	The Isoch CEC "handle" returned by
 *					    t1394_alloc_isoch_cec()
 *		flags			The flags parameter is unused (for now)
 *		join_isoch_info		This structure provides infomation
 *					    about a target that wishes to join
 *					    the given Isoch CEC.  It gives
 *					    max_speed, channel_mask, etc.
 *
 * Output(s):	DDI_SUCCESS		Target successfully joined the
 *					    Isoch CEC
 *		DDI_FAILURE		Target failed to join the Isoch CEC
 *
 * Description:	t1394_join_isoch_cec() determines, based on the information
 *		given in the join_isoch_info structure, if the target may
 *		join the Isoch CEC.  If it is determined that the target may
 *		join, the specified callback routines are stored away for
 *		later use in the coordination tasks.
 */
/* ARGSUSED */
int
t1394_join_isoch_cec(t1394_handle_t t1394_hdl,
    t1394_isoch_cec_handle_t t1394_isoch_cec_hdl, uint_t flags,
    t1394_join_isochinfo_t *join_isoch_info)
{
	s1394_hal_t		 *hal;
	s1394_isoch_cec_t	 *cec_curr;
	s1394_isoch_cec_member_t *member_new;
	uint64_t		 check_mask;
	uint_t			 curr_max_speed;

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_isoch_cec_hdl != NULL);

	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	/* Convert the handle to an Isoch CEC pointer */
	cec_curr = (s1394_isoch_cec_t *)t1394_isoch_cec_hdl;

	/* Allocate a new Isoch CEC member structure */
	member_new = kmem_zalloc(sizeof (s1394_isoch_cec_member_t), KM_SLEEP);

	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

	/* Are we in any callbacks? (Wait for them to finish) */
	while (CEC_IN_ANY_CALLBACKS(cec_curr)) {
		cec_curr->cec_want_wakeup = B_TRUE;
		cv_wait(&cec_curr->in_callbacks_cv,
		    &cec_curr->isoch_cec_mutex);
	}

	/* Is "join" a legal state transition? */
	if (CEC_TRANSITION_LEGAL(cec_curr, ISOCH_CEC_JOIN) == 0) {
		kmem_free(member_new, sizeof (s1394_isoch_cec_member_t));
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* Check the channel mask for consistency */
	check_mask = join_isoch_info->req_channel_mask &
	    cec_curr->filter_channel_mask;
	if (check_mask == 0) {
		kmem_free(member_new, sizeof (s1394_isoch_cec_member_t));
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* Check for consistent speeds */
	if (join_isoch_info->req_max_speed < cec_curr->filter_min_speed) {
		kmem_free(member_new, sizeof (s1394_isoch_cec_member_t));
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	} else if (join_isoch_info->req_max_speed <
	    cec_curr->filter_current_speed) {
		curr_max_speed = join_isoch_info->req_max_speed;
	} else {
		curr_max_speed = cec_curr->filter_current_speed;
	}

	/* Check for no more than one talker */
	if ((join_isoch_info->jii_options & T1394_TALKER) &&
	    (cec_curr->cec_member_talker != NULL)) {
		kmem_free(member_new, sizeof (s1394_isoch_cec_member_t));
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* Verify that all callbacks are non-NULL (for PEER_TO_PEER) */
	if ((cec_curr->cec_type == S1394_PEER_TO_PEER) &&
	    ((join_isoch_info->isoch_cec_evts.setup_target	== NULL) ||
	    (join_isoch_info->isoch_cec_evts.start_target	== NULL) ||
	    (join_isoch_info->isoch_cec_evts.stop_target	== NULL) ||
	    (join_isoch_info->isoch_cec_evts.rsrc_fail_target	== NULL) ||
	    (join_isoch_info->isoch_cec_evts.teardown_target	== NULL))) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* Copy the events information into the struct */
	member_new->isoch_cec_evts	= join_isoch_info->isoch_cec_evts;
	member_new->isoch_cec_evts_arg	= join_isoch_info->isoch_cec_evts_arg;
	member_new->cec_mem_options	= join_isoch_info->jii_options;
	member_new->cec_mem_target	= (s1394_target_t *)t1394_hdl;

	/* Insert new member into Isoch CEC's member list */
	s1394_isoch_cec_member_list_insert(hal, cec_curr, member_new);

	/* Update the channel mask filter */
	cec_curr->filter_channel_mask	= check_mask;

	/* Update the speed filter */
	cec_curr->filter_current_speed	= curr_max_speed;

	/* Update the talker pointer (if necessary) */
	if (join_isoch_info->jii_options & T1394_TALKER)
		cec_curr->cec_member_talker = cec_curr->cec_member_list_head;

	/*
	 * Now "leave" is a legal state transition
	 * and "free" is an illegal state transition
	 */
	CEC_SET_LEGAL(cec_curr, ISOCH_CEC_LEAVE);
	CEC_SET_ILLEGAL(cec_curr, ISOCH_CEC_FREE);

	/* Unlock the Isoch CEC member list */
	mutex_exit(&cec_curr->isoch_cec_mutex);

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_leave_isoch_cec()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		t1394_isoch_cec_hdl	The Isoch CEC "handle" returned by
 *					    t1394_alloc_isoch_cec()
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	DDI_SUCCESS		Target successfully left the
 *					    Isoch CEC
 *		DDI_FAILURE		Target failed to leave the Isoch CEC
 *
 * Description:	t1394_leave_isoch_cec() is used by a target driver to remove
 *		itself from the Isoch CEC's member list.  It is possible
 *		for this call to fail because the target is not found in
 *		the current member list, or because it is not an appropriate
 *		time for a target to leave.
 */
/* ARGSUSED */
int
t1394_leave_isoch_cec(t1394_handle_t t1394_hdl,
    t1394_isoch_cec_handle_t t1394_isoch_cec_hdl, uint_t flags)
{
	s1394_hal_t		 *hal;
	s1394_isoch_cec_t	 *cec_curr;
	s1394_isoch_cec_member_t *member_curr;
	s1394_isoch_cec_member_t *member_temp;
	boolean_t		 found;
	uint64_t		 temp_channel_mask;
	uint_t			 temp_max_speed;

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_isoch_cec_hdl != NULL);

	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	/* Convert the handle to an Isoch CEC pointer */
	cec_curr = (s1394_isoch_cec_t *)t1394_isoch_cec_hdl;

	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

	/* Are we in any callbacks? (Wait for them to finish) */
	while (CEC_IN_ANY_CALLBACKS(cec_curr)) {
		cec_curr->cec_want_wakeup = B_TRUE;
		cv_wait(&cec_curr->in_callbacks_cv,
		    &cec_curr->isoch_cec_mutex);
	}

	/* Is "leave" a legal state transition? */
	if (CEC_TRANSITION_LEGAL(cec_curr, ISOCH_CEC_LEAVE) == 0) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* Find the Target on the CEC's member list */
	found = B_FALSE;
	temp_channel_mask = cec_curr->cec_alloc_props.cec_channel_mask;
	temp_max_speed	  = cec_curr->cec_alloc_props.cec_max_speed;
	member_curr	  = cec_curr->cec_member_list_head;
	while (member_curr != NULL) {
		if (member_curr->cec_mem_target ==
		    (s1394_target_t *)t1394_hdl) {
			member_temp = member_curr;
			found	    = B_TRUE;
		} else {
			/* Keep track of channel mask and max speed info */
			temp_channel_mask &= member_curr->req_channel_mask;
			if (member_curr->req_max_speed < temp_max_speed)
				temp_max_speed = member_curr->req_max_speed;
		}
		member_curr = member_curr->cec_mem_next;
	}

	/* Target not found on this Isoch CEC */
	if (found == B_FALSE) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	} else {
		/* This member's departure may change filter constraints */
		cec_curr->filter_current_speed  = temp_max_speed;
		cec_curr->filter_channel_mask   = temp_channel_mask;
	}

	/* Remove member from Isoch CEC's member list */
	s1394_isoch_cec_member_list_remove(hal, cec_curr, member_temp);

	/* If we are removing the talker, then update the pointer */
	if (cec_curr->cec_member_talker == member_temp)
		cec_curr->cec_member_talker = NULL;

	/* Is the Isoch CEC's member list empty? */
	if ((cec_curr->cec_member_list_head == NULL) &&
	    (cec_curr->cec_member_list_tail == NULL)) {
		/*
		 * Now "free" _might_ be a legal state transition
		 * if we aren't in setup or start phases and "leave"
		 * is definitely an illegal state transition
		 */
		if (CEC_TRANSITION_LEGAL(cec_curr, ISOCH_CEC_JOIN) != 0)
			CEC_SET_LEGAL(cec_curr, ISOCH_CEC_FREE);
		CEC_SET_ILLEGAL(cec_curr, ISOCH_CEC_LEAVE);
	}

	/* Unlock the Isoch CEC member list */
	mutex_exit(&cec_curr->isoch_cec_mutex);

	/* Free the Isoch CEC member structure */
	kmem_free(member_temp, sizeof (s1394_isoch_cec_member_t));

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_setup_isoch_cec()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		t1394_isoch_cec_hdl	The Isoch CEC "handle" returned by
 *					    t1394_alloc_isoch_cec()
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	result			Used to pass more specific info back
 *					    to target
 *
 * Description:	t1394_setup_isoch_cec() directs the 1394 Software Framework
 *		to allocate isochronous resources and invoke the setup_target()
 *		callback for each member of the Isoch CEC.  This call may
 *		fail because bandwidth was unavailable (T1394_ENO_BANDWIDTH),
 *		channels were unavailable (T1394_ENO_CHANNEL), or one of the
 *		member targets returned failure from its setup_target()
 *		callback.
 */
/* ARGSUSED */
int
t1394_setup_isoch_cec(t1394_handle_t t1394_hdl,
    t1394_isoch_cec_handle_t t1394_isoch_cec_hdl, uint_t flags, int *result)
{
	s1394_hal_t			*hal;
	s1394_isoch_cec_t		*cec_curr;
	s1394_isoch_cec_member_t	*member_curr;
	t1394_setup_target_args_t	target_args;
	uint64_t			temp_chnl_mask;
	uint32_t			old_chnl;
	uint32_t			try_chnl;
	uint_t				bw_alloc_units;
	uint_t				generation;
	int				chnl_num;
	int				err;
	int				ret;
	int				j;
	int	(*setup_callback)(t1394_isoch_cec_handle_t, opaque_t,
			    t1394_setup_target_args_t *);

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_isoch_cec_hdl != NULL);

	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	/* Convert the handle to an Isoch CEC pointer */
	cec_curr = (s1394_isoch_cec_t *)t1394_isoch_cec_hdl;

	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

	/* Are we in any callbacks? */
	if (CEC_IN_ANY_CALLBACKS(cec_curr)) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* Is "setup" a legal state transition? */
	if (CEC_TRANSITION_LEGAL(cec_curr, ISOCH_CEC_SETUP) == 0) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* If T1394_NO_IRM_ALLOC is set then don't allocate... do callbacks */
	if (cec_curr->cec_options & T1394_NO_IRM_ALLOC) {
		goto setup_do_callbacks;
	}

	/* Allocate bandwidth and channels */
	for (j = 0; j < S1394_ISOCH_ALLOC_RETRIES; j++) {
		/*
		 * Get the current generation number - don't
		 * need the lock because we are read only here
		 */
		generation = hal->generation_count;

		/* Compute how much bandwidth is needed */
		bw_alloc_units = s1394_compute_bw_alloc_units(hal,
		    cec_curr->bandwidth, cec_curr->filter_current_speed);

		/* Check that the generation has not changed - */
		/* don't need the lock (read only) */
		if (generation != hal->generation_count)
			continue;

		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);

		/* Try to allocate the bandwidth */
		ret = s1394_bandwidth_alloc(hal, bw_alloc_units, generation,
		    &err);

		/* Lock the Isoch CEC member list */
		mutex_enter(&cec_curr->isoch_cec_mutex);

		/* If there was a bus reset, start over */
		if (ret == DDI_FAILURE) {
			if (err == CMD1394_EBUSRESET) {
				continue; /* start over and try again */
			} else {
				*result = T1394_ENO_BANDWIDTH;
				/* Unlock the Isoch CEC member list */
				mutex_exit(&cec_curr->isoch_cec_mutex);
				return (DDI_FAILURE);
			}
		}

		/* Check that the generation has not changed - */
		/* don't need the lock (read only) */
		if (generation != hal->generation_count)
			continue;

		/*
		 * Allocate a channel
		 *    From IEEE 1394-1995, Section 8.3.2.3.8: "Bits
		 *    allocated in the CHANNELS_AVAILABLE_HI field of
		 *    this register shall start at bit zero (channel
		 *    number zero), and additional channel numbers shall
		 *    be represented in a monotonically increasing sequence
		 *    of bit numbers up to a maximum of bit 31 (channel
		 *    number 31).  Bits allocated in the CHANNELS_AVAILABLE_LO
		 *    field of this register shall start at bit zero
		 *    (channel number 32), and additional channel numbers
		 *    shall be represented in a monotonically increasing
		 *    sequence of bit numbers up to a maximum of bit 31
		 *    (channel number 63).
		 */
		temp_chnl_mask = cec_curr->filter_channel_mask;
		for (chnl_num = 63; chnl_num >= 0; chnl_num--) {
			if ((temp_chnl_mask & 1) == 1) {
				try_chnl = (1 << ((63 - chnl_num) % 32));

				/* Unlock the Isoch CEC member list */
				mutex_exit(&cec_curr->isoch_cec_mutex);
				if (chnl_num < 32) {
					ret = s1394_channel_alloc(hal,
					    try_chnl, generation,
					    S1394_CHANNEL_ALLOC_HI, &old_chnl,
					    &err);
				} else {
					ret = s1394_channel_alloc(hal,
					    try_chnl, generation,
					    S1394_CHANNEL_ALLOC_LO, &old_chnl,
					    &err);
				}
				/* Lock the Isoch CEC member list */
				mutex_enter(&cec_curr->isoch_cec_mutex);

				/* Did we get a channel? (or a bus reset) */
				if ((ret == DDI_SUCCESS) ||
				    (err == CMD1394_EBUSRESET))
					break;
			}
			temp_chnl_mask = temp_chnl_mask >> 1;
		}

		/* If we've tried all the possible channels, then fail */
		if (chnl_num == 0) {
			*result = T1394_ENO_CHANNEL;
			/*
			 * If we successfully allocate bandwidth, and
			 * then fail getting a channel, we need to
			 * free up the bandwidth
			 */

			/* Check that the generation has not changed */
			/* lock not needed here (read only) */
			if (generation != hal->generation_count)
				continue;

			/* Unlock the Isoch CEC member list */
			mutex_exit(&cec_curr->isoch_cec_mutex);

			/* Try to free up the bandwidth */
			ret = s1394_bandwidth_free(hal, bw_alloc_units,
			    generation, &err);

			/* Lock the Isoch CEC member list */
			mutex_enter(&cec_curr->isoch_cec_mutex);

			if (ret == DDI_FAILURE) {
				if (err == CMD1394_EBUSRESET) {
					continue;
				}
			}

			/* Unlock the Isoch CEC member list */
			mutex_exit(&cec_curr->isoch_cec_mutex);
			return (DDI_FAILURE);
		}

		/* If we got a channel, we're done (else start over) */
		if (ret == DDI_SUCCESS)
			break;
		else if (err == CMD1394_EBUSRESET)
			continue;
	}

	/* Have we gotten too many bus resets? */
	if (j == S1394_ISOCH_ALLOC_RETRIES) {
		*result = T1394_ENO_BANDWIDTH;
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	cec_curr->realloc_valid	    = B_TRUE;
	cec_curr->realloc_chnl_num  = chnl_num;
	cec_curr->realloc_bandwidth = cec_curr->bandwidth;
	cec_curr->realloc_speed	    = cec_curr->filter_current_speed;

setup_do_callbacks:
	/* Call all of the setup_target() callbacks */
	target_args.channel_num	    = chnl_num;
	target_args.channel_speed   = cec_curr->filter_current_speed;

	/* Now we are going into the callbacks */
	cec_curr->in_callbacks	    = B_TRUE;

	/* Unlock the Isoch CEC member list */
	mutex_exit(&cec_curr->isoch_cec_mutex);

	member_curr = cec_curr->cec_member_list_head;
	*result = 0;
	while (member_curr != NULL) {
		if (member_curr->isoch_cec_evts.setup_target != NULL) {
			setup_callback =
			    member_curr->isoch_cec_evts.setup_target;
			ret = setup_callback(t1394_isoch_cec_hdl,
			    member_curr->isoch_cec_evts_arg, &target_args);
			if (ret != DDI_SUCCESS)
				*result = T1394_ETARGET;
		}
		member_curr = member_curr->cec_mem_next;
	}

	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

	/* We are finished with the callbacks */
	cec_curr->in_callbacks = B_FALSE;
	if (cec_curr->cec_want_wakeup == B_TRUE) {
		cec_curr->cec_want_wakeup = B_FALSE;
		cv_broadcast(&cec_curr->in_callbacks_cv);
	}

	/*
	 * Now "start" and "teardown" are legal state transitions
	 * and "join", "free", and "setup" are illegal state transitions
	 */
	CEC_SET_LEGAL(cec_curr, (ISOCH_CEC_START | ISOCH_CEC_TEARDOWN));
	CEC_SET_ILLEGAL(cec_curr, (ISOCH_CEC_JOIN | ISOCH_CEC_FREE |
	    ISOCH_CEC_SETUP));

	/* Unlock the Isoch CEC member list */
	mutex_exit(&cec_curr->isoch_cec_mutex);

	/* Return DDI_FAILURE if any targets failed setup */
	if (*result != 0) {
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_start_isoch_cec()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		t1394_isoch_cec_hdl	The Isoch CEC "handle" returned by
 *					    t1394_alloc_isoch_cec()
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	DDI_SUCCESS		All start_target() callbacks returned
 *					    successfully
 *		DDI_FAILURE		One or more start_target() callbacks
 *					    returned failure
 *
 * Description:	t1394_start_isoch_cec() directs the 1394 Software Framework
 *		to invoke each of the start_target() callbacks, first for
 *		each listener, then for the talker.
 */
/* ARGSUSED */
int
t1394_start_isoch_cec(t1394_handle_t t1394_hdl,
    t1394_isoch_cec_handle_t t1394_isoch_cec_hdl, uint_t flags)
{
	s1394_isoch_cec_t	 *cec_curr;
	s1394_isoch_cec_member_t *member_curr;
	int			 ret;
	boolean_t		 err;
	int	(*start_callback)(t1394_isoch_cec_handle_t, opaque_t);

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_isoch_cec_hdl != NULL);

	/* Convert the handle to an Isoch CEC pointer */
	cec_curr = (s1394_isoch_cec_t *)t1394_isoch_cec_hdl;

	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

	/* Are we in any callbacks? */
	if (CEC_IN_ANY_CALLBACKS(cec_curr)) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* Is "start" a legal state transition? */
	if (CEC_TRANSITION_LEGAL(cec_curr, ISOCH_CEC_START) == 0) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* Now we are going into the callbacks */
	cec_curr->in_callbacks = B_TRUE;

	/* Unlock the Isoch CEC member list */
	mutex_exit(&cec_curr->isoch_cec_mutex);

	/*
	 * Call all of the start_target() callbacks
	 * Start at the tail (listeners first) and
	 * go toward the head (talker last)
	 */
	member_curr = cec_curr->cec_member_list_tail;
	err = B_FALSE;
	while (member_curr != NULL) {
		if (member_curr->isoch_cec_evts.start_target != NULL) {
			start_callback =
			    member_curr->isoch_cec_evts.start_target;
			ret = start_callback(t1394_isoch_cec_hdl,
			    member_curr->isoch_cec_evts_arg);
		if (ret != DDI_SUCCESS)
			err = B_TRUE;
		}
		member_curr = member_curr->cec_mem_prev;
	}

	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

	/* We are finished with the callbacks */
	cec_curr->in_callbacks = B_FALSE;
	if (cec_curr->cec_want_wakeup == B_TRUE) {
		cec_curr->cec_want_wakeup = B_FALSE;
		cv_broadcast(&cec_curr->in_callbacks_cv);
	}

	/*
	 * Now "stop" is a legal state transitions
	 * and "start" and "teardown" are illegal state transitions
	 */
	CEC_SET_LEGAL(cec_curr, ISOCH_CEC_STOP);
	CEC_SET_ILLEGAL(cec_curr, (ISOCH_CEC_START | ISOCH_CEC_TEARDOWN));

	/* Unlock the Isoch CEC member list */
	mutex_exit(&cec_curr->isoch_cec_mutex);

	/* Return DDI_FAILURE if any targets failed start */
	if (err == B_TRUE) {
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_stop_isoch_cec()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		t1394_isoch_cec_hdl	The Isoch CEC "handle" returned by
 *					    t1394_alloc_isoch_cec()
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	DDI_SUCCESS		Target successfully stopped the
 *					    Isoch CEC
 *		DDI_FAILURE		Target failed to stop the Isoch CEC
 *
 * Description:	t1394_stop_isoch_cec() directs the 1394 Software Framework
 *		to invoke each of the stop_target() callbacks, first for
 *		the talker, then for each listener.
 *		(This call will fail if it is called at an
 *		inappropriate time, i.e. before the t1394_start_isoch_cec()
 *		call, etc.)
 */
/* ARGSUSED */
int
t1394_stop_isoch_cec(t1394_handle_t t1394_hdl,
    t1394_isoch_cec_handle_t t1394_isoch_cec_hdl, uint_t flags)
{
	s1394_isoch_cec_t	 *cec_curr;
	s1394_isoch_cec_member_t *member_curr;
	void	(*stop_callback)(t1394_isoch_cec_handle_t, opaque_t);

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_isoch_cec_hdl != NULL);

	/* Convert the handle to an Isoch CEC pointer */
	cec_curr = (s1394_isoch_cec_t *)t1394_isoch_cec_hdl;

	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

	/* Are we in any callbacks? */
	if (CEC_IN_ANY_CALLBACKS(cec_curr)) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* Is "stop" a legal state transition? */
	if (CEC_TRANSITION_LEGAL(cec_curr, ISOCH_CEC_STOP) == 0) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* Now we are going into the callbacks */
	cec_curr->in_callbacks = B_TRUE;

	/* Unlock the Isoch CEC member list */
	mutex_exit(&cec_curr->isoch_cec_mutex);

	/*
	 * Call all of the stop_target() callbacks
	 * Start at the head (talker first) and
	 * go toward the tail (listeners last)
	 */
	member_curr = cec_curr->cec_member_list_head;
	while (member_curr != NULL) {
		if (member_curr->isoch_cec_evts.stop_target != NULL) {
			stop_callback =
			    member_curr->isoch_cec_evts.stop_target;
			stop_callback(t1394_isoch_cec_hdl,
			    member_curr->isoch_cec_evts_arg);
		}
		member_curr = member_curr->cec_mem_next;
	}

	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

	/* We are finished with the callbacks */
	cec_curr->in_callbacks = B_FALSE;
	if (cec_curr->cec_want_wakeup == B_TRUE) {
		cec_curr->cec_want_wakeup = B_FALSE;
		cv_broadcast(&cec_curr->in_callbacks_cv);
	}

	/*
	 * Now "start" and "teardown" are legal state transitions
	 * and "stop" is an illegal state transitions
	 */
	CEC_SET_LEGAL(cec_curr, (ISOCH_CEC_START | ISOCH_CEC_TEARDOWN));
	CEC_SET_ILLEGAL(cec_curr, ISOCH_CEC_STOP);

	/* Unlock the Isoch CEC member list */
	mutex_exit(&cec_curr->isoch_cec_mutex);

	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_teardown_isoch_cec()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		t1394_isoch_cec_hdl	The Isoch CEC "handle" returned by
 *					    t1394_alloc_isoch_cec()
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	DDI_SUCCESS		Target successfully tore down the
 *					    Isoch CEC
 *		DDI_FAILURE		Target failed to tear down the
 *					    Isoch CEC
 *
 * Description:	t1394_teardown_isoch_cec() directs the 1394 Software Framework
 *		to free up any isochronous resources we might be holding and
 *		call all of the teardown_target() callbacks.
 *		(This call will fail if it is called at an
 *		inappropriate time, i.e. before the t1394_start_isoch_cec()
 *		call, before the t1394_stop_isoch_cec, etc.
 */
/* ARGSUSED */
int
t1394_teardown_isoch_cec(t1394_handle_t t1394_hdl,
    t1394_isoch_cec_handle_t t1394_isoch_cec_hdl, uint_t flags)
{
	s1394_hal_t		 *hal;
	s1394_isoch_cec_t	 *cec_curr;
	s1394_isoch_cec_member_t *member_curr;
	uint32_t		 chnl_mask;
	uint32_t		 old_chnl_mask;
	uint_t			 bw_alloc_units;
	uint_t			 generation;
	int			 ret;
	int			 err;
	void	(*teardown_callback)(t1394_isoch_cec_handle_t, opaque_t);

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_isoch_cec_hdl != NULL);

	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	/* Convert the handle to an Isoch CEC pointer */
	cec_curr = (s1394_isoch_cec_t *)t1394_isoch_cec_hdl;

	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

	/* Are we in any callbacks? */
	if (CEC_IN_ANY_CALLBACKS(cec_curr)) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* Is "teardown" a legal state transition? */
	if (CEC_TRANSITION_LEGAL(cec_curr, ISOCH_CEC_TEARDOWN) == 0) {
		/* Unlock the Isoch CEC member list */
		mutex_exit(&cec_curr->isoch_cec_mutex);
		return (DDI_FAILURE);
	}

	/* If T1394_NO_IRM_ALLOC is set then don't free... do callbacks */
	if (cec_curr->cec_options & T1394_NO_IRM_ALLOC) {
		goto teardown_do_callbacks;
	}

	/* If nothing has been allocated or we failed to */
	/* reallocate, then we are done... call the callbacks */
	if ((cec_curr->realloc_valid == B_FALSE) ||
	    (cec_curr->realloc_failed == B_TRUE)) {
		goto teardown_do_callbacks;
	}

	/*
	 * Get the current generation number - don't need the
	 * topology tree mutex here because it is read-only, and
	 * there is a race condition with or without it.
	 */
	generation = hal->generation_count;

	/* Compute the amount bandwidth to free */
	bw_alloc_units = s1394_compute_bw_alloc_units(hal,
	    cec_curr->bandwidth, cec_curr->realloc_speed);

	/* Check that the generation has not changed - */
	/* don't need the lock (read only) */
	if (generation != hal->generation_count)
		goto teardown_do_callbacks;

	/* Unlock the Isoch CEC member list */
	mutex_exit(&cec_curr->isoch_cec_mutex);

	/* Try to free up the bandwidth */
	ret = s1394_bandwidth_free(hal, bw_alloc_units, generation, &err);

	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

	if (ret == DDI_FAILURE) {
		if (err == CMD1394_EBUSRESET) {
			goto teardown_do_callbacks;
		}
	}

	/* Free the allocated channel */
	chnl_mask = (1 << ((63 - cec_curr->realloc_chnl_num) % 32));

	/* Unlock the Isoch CEC member list */
	mutex_exit(&cec_curr->isoch_cec_mutex);
	if (cec_curr->realloc_chnl_num < 32) {
		ret = s1394_channel_free(hal, chnl_mask, generation,
		    S1394_CHANNEL_ALLOC_HI, &old_chnl_mask, &err);
	} else {
		ret = s1394_channel_free(hal, chnl_mask, generation,
		    S1394_CHANNEL_ALLOC_LO, &old_chnl_mask, &err);
	}
	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

teardown_do_callbacks:
	/* From here on reallocation is unnecessary */
	cec_curr->realloc_valid	    = B_FALSE;
	cec_curr->realloc_chnl_num  = 0;
	cec_curr->realloc_bandwidth = 0;

	/* Now we are going into the callbacks */
	cec_curr->in_callbacks	    = B_TRUE;

	/* Unlock the Isoch CEC member list */
	mutex_exit(&cec_curr->isoch_cec_mutex);

	/* Call all of the teardown_target() callbacks */
	member_curr = cec_curr->cec_member_list_head;
	while (member_curr != NULL) {
		if (member_curr->isoch_cec_evts.teardown_target != NULL) {
			teardown_callback =
			    member_curr->isoch_cec_evts.teardown_target;
			teardown_callback(t1394_isoch_cec_hdl,
			    member_curr->isoch_cec_evts_arg);
		}
		member_curr = member_curr->cec_mem_next;
	}

	/* Lock the Isoch CEC member list */
	mutex_enter(&cec_curr->isoch_cec_mutex);

	/* We are finished with the callbacks */
	cec_curr->in_callbacks = B_FALSE;
	if (cec_curr->cec_want_wakeup == B_TRUE) {
		cec_curr->cec_want_wakeup = B_FALSE;
		cv_broadcast(&cec_curr->in_callbacks_cv);
	}

	/*
	 * Now "join" and "setup" are legal state transitions
	 * and "start" and "teardown" are illegal state transitions
	 */
	CEC_SET_LEGAL(cec_curr, (ISOCH_CEC_JOIN | ISOCH_CEC_SETUP));
	CEC_SET_ILLEGAL(cec_curr, (ISOCH_CEC_START | ISOCH_CEC_TEARDOWN));

	/* And if the member list is empty, then "free" is legal too */
	if ((cec_curr->cec_member_list_head == NULL) &&
	    (cec_curr->cec_member_list_tail == NULL)) {
		CEC_SET_LEGAL(cec_curr, ISOCH_CEC_FREE);
	}

	/* Unlock the Isoch CEC member list */
	mutex_exit(&cec_curr->isoch_cec_mutex);
	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_alloc_isoch_dma()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		idi			This structure contains information
 *					    for configuring the data flow for
 *					    isochronous DMA
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	t1394_idma_hdl		The IDMA "handle" used in all
 *					    subsequent isoch_dma() calls
 *		result			Used to pass more specific info back
 *					    to target
 *
 * Description:	t1394_alloc_isoch_dma() allocates and initializes an
 *		isochronous DMA resource for transmitting or receiving
 *		isochronous data.  If it fails, result may hold
 *		T1394_EIDMA_NO_RESRCS, indicating that no isoch DMA resource
 *		are available.
 */
/* ARGSUSED */
int
t1394_alloc_isoch_dma(t1394_handle_t t1394_hdl,
    id1394_isoch_dmainfo_t *idi, uint_t flags,
    t1394_isoch_dma_handle_t *t1394_idma_hdl, int *result)
{
	s1394_hal_t	*hal;
	int		ret;

	ASSERT(t1394_hdl != NULL);
	ASSERT(idi != NULL);
	ASSERT(t1394_idma_hdl != NULL);

	/* Find the HAL this target resides on */
	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	/* Sanity check dma options.  If talk enabled, listen should be off */
	if ((idi->idma_options & ID1394_TALK) &&
	    (idi->idma_options != ID1394_TALK)) {
		*result = T1394_EIDMA_CONFLICT;
		return (DDI_FAILURE);
	}

	/* Only one listen mode allowed */
	if ((idi->idma_options & ID1394_LISTEN_PKT_MODE) &&
	    (idi->idma_options & ID1394_LISTEN_BUF_MODE)) {
		*result = T1394_EIDMA_CONFLICT;
		return (DDI_FAILURE);
	}

	/* Have HAL alloc a resource and compile ixl */
	ret = HAL_CALL(hal).alloc_isoch_dma(hal->halinfo.hal_private, idi,
	    (void **)t1394_idma_hdl, result);

	if (ret != DDI_SUCCESS) {
		if (*result == IXL1394_ENO_DMA_RESRCS) {
			*result = T1394_EIDMA_NO_RESRCS;
		}
	}

	return (ret);
}

/*
 * Function:    t1394_free_isoch_dma()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		flags			The flags parameter is unused (for now)
 *		t1394_idma_hdl		The IDMA "handle" returned by
 *					    t1394_alloc_isoch_dma()
 *
 * Output(s):	None
 *
 * Description:	t1394_free_isoch_dma() is used to free all DMA resources
 *		allocated for the isoch stream associated with t1394_idma_hdl.
 */
/* ARGSUSED */
void
t1394_free_isoch_dma(t1394_handle_t t1394_hdl, uint_t flags,
    t1394_isoch_dma_handle_t *t1394_idma_hdl)
{
	s1394_hal_t	*hal;

	ASSERT(t1394_hdl != NULL);
	ASSERT(*t1394_idma_hdl != NULL);

	/* Find the HAL this target resides on */
	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	/* Tell HAL to release local isoch dma resources */
	HAL_CALL(hal).free_isoch_dma(hal->halinfo.hal_private, *t1394_idma_hdl);

	/* Null out isoch handle */
	*t1394_idma_hdl = NULL;
}

/*
 * Function:    t1394_start_isoch_dma()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		t1394_idma_hdl		The IDMA "handle" returned by
 *					    t1394_alloc_isoch_dma()
 *		idma_ctrlinfo		This structure contains control args
 *					    used when starting isoch DMA for
 *					    the allocated resource
 *		flags			One flag defined - ID1394_START_ON_CYCLE
 *
 * Output(s):	result			Used to pass more specific info back
 *					    to target
 *
 * Description:	t1394_start_isoch_dma() is used to start DMA for the isoch
 *		stream associated with t1394_idma_hdl.
 */
/* ARGSUSED */
int
t1394_start_isoch_dma(t1394_handle_t t1394_hdl,
    t1394_isoch_dma_handle_t t1394_idma_hdl,
    id1394_isoch_dma_ctrlinfo_t *idma_ctrlinfo, uint_t flags,
    int *result)
{
	s1394_hal_t	*hal;
	int		ret;

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_idma_hdl != NULL);
	ASSERT(idma_ctrlinfo != NULL);

	/* Find the HAL this target resides on */
	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	ret = HAL_CALL(hal).start_isoch_dma(hal->halinfo.hal_private,
	    (void *)t1394_idma_hdl, idma_ctrlinfo, flags, result);

	return (ret);
}

/*
 * Function:    t1394_stop_isoch_dma()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		t1394_idma_hdl		The IDMA "handle" returned by
 *					    t1394_alloc_isoch_dma()
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	None
 *
 * Description:	t1394_stop_isoch_dma() is used to stop DMA for the isoch
 *		stream associated with t1394_idma_hdl.
 */
/* ARGSUSED */
void
t1394_stop_isoch_dma(t1394_handle_t t1394_hdl,
    t1394_isoch_dma_handle_t t1394_idma_hdl, uint_t flags)
{
	s1394_hal_t	*hal;
	int		result;

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_idma_hdl != NULL);

	/* Find the HAL this target resides on */
	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	HAL_CALL(hal).stop_isoch_dma(hal->halinfo.hal_private,
	    (void *)t1394_idma_hdl, &result);
}

/*
 * Function:    t1394_update_isoch_dma()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		t1394_idma_hdl		The IDMA "handle" returned by
 *					    t1394_alloc_isoch_dma()
 *		idma_updateinfo		This structure contains ixl command args
 *					    used when updating args in an
 *					    existing list of ixl commands with
 *					    args in a new list of ixl commands.
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	result			Used to pass more specific info back
 *					    to target
 *
 * Description:	t1394_update_isoch_dma() is used to alter an IXL program that
 *		has already been built (compiled) by t1394_alloc_isoch_dma().
 */
/* ARGSUSED */
int
t1394_update_isoch_dma(t1394_handle_t t1394_hdl,
    t1394_isoch_dma_handle_t t1394_idma_hdl,
    id1394_isoch_dma_updateinfo_t *idma_updateinfo, uint_t flags,
    int *result)
{
	s1394_hal_t	*hal;
	int		ret;

	ASSERT(t1394_hdl != NULL);
	ASSERT(t1394_idma_hdl != NULL);
	ASSERT(idma_updateinfo != NULL);

	/* Find the HAL this target resides on */
	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	ret = HAL_CALL(hal).update_isoch_dma(hal->halinfo.hal_private,
	    (void *)t1394_idma_hdl, idma_updateinfo, flags, result);

	return (ret);
}

/*
 * Function:    t1394_initiate_bus_reset()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	None
 *
 * Description:	t1394_initiate_bus_reset() determines whether the local
 *		device has a P1394A PHY and will support the arbitrated
 *		short bus reset. If not, it will initiate a normal bus reset.
 */
/* ARGSUSED */
void
t1394_initiate_bus_reset(t1394_handle_t t1394_hdl, uint_t flags)
{
	s1394_hal_t	*hal;

	ASSERT(t1394_hdl != NULL);

	/* Find the HAL this target resides on */
	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	/* Reset the bus */
	if (hal->halinfo.phy == H1394_PHY_1394A) {
		(void) HAL_CALL(hal).short_bus_reset(hal->halinfo.hal_private);
	} else {
		(void) HAL_CALL(hal).bus_reset(hal->halinfo.hal_private);
	}
}

/*
 * Function:    t1394_get_topology_map()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		bus_generation		The current generation
 *		tm_length		The size of the tm_buffer given
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	tm_buffer		Filled in by the 1394 Software Framework
 *					    with the contents of the local
 *					    TOPOLOGY_MAP
 *
 * Description:	t1394_get_topology_map() returns the 1394 TOPLOGY_MAP.  See
 *		IEEE 1394-1995 Section 8.2.3.4.1 for format information.  This
 *		call can fail if there is a generation mismatch or the
 *		tm_buffer is too small to hold the TOPOLOGY_MAP.
 */
/* ARGSUSED */
int
t1394_get_topology_map(t1394_handle_t t1394_hdl, uint_t bus_generation,
    size_t tm_length, uint_t flags, uint32_t *tm_buffer)
{
	s1394_hal_t	*hal;
	uint32_t	*tm_ptr;
	uint_t		length;

	ASSERT(t1394_hdl != NULL);

	/* Find the HAL this target resides on */
	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	/* Lock the topology tree */
	mutex_enter(&hal->topology_tree_mutex);

	/* Check the bus_generation for the Topology Map */
	if (bus_generation != hal->generation_count) {
		/* Unlock the topology tree */
		mutex_exit(&hal->topology_tree_mutex);
		return (DDI_FAILURE);
	}

	tm_ptr	= (uint32_t *)hal->CSR_topology_map;
	length	= tm_ptr[0] >> 16;
	length  = length * 4;	/* Bytes instead of quadlets   */
	length  = length + 4;   /* don't forget the first quad */

	/* Check that the buffer is big enough */
	if (length > (uint_t)tm_length) {
		/* Unlock the topology tree */
		mutex_exit(&hal->topology_tree_mutex);
		return (DDI_FAILURE);
	}

	/* Do the copy */
	bcopy(tm_ptr, tm_buffer, length);

	/* Unlock the topology tree */
	mutex_exit(&hal->topology_tree_mutex);
	return (DDI_SUCCESS);
}

/*
 * Function:    t1394_CRC16()
 * Input(s):    d			The data to compute the CRC-16 for
 *		crc_length		The length into the data to compute for
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	CRC			The CRC-16 computed for the length
 *					    of data specified
 *
 * Description:	t1394_CRC16() implements ISO/IEC 13213:1994, ANSI/IEEE Std
 *		1212, 1994 - 8.1.5.
 */
/* ARGSUSED */
uint_t
t1394_CRC16(uint32_t *d, size_t crc_length, uint_t flags)
{
	/* Implements ISO/IEC 13213:1994,	*/
	/* ANSI/IEEE Std 1212, 1994 - 8.1.5	*/
	uint_t	ret;

	ret = s1394_CRC16((uint_t *)d, (uint_t)crc_length);

	return (ret);
}

/*
 * Function:    t1394_add_cfgrom_entry()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		cfgrom_entryinfo	This structure holds the cfgrom key,
 *					    buffer, and size
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	t1394_cfgrom_hdl	The ConfigROM "handle" used in
 *					    t1394_rem_cfgrom_entry()
 *		result			Used to pass more specific info back
 *					    to target
 *
 * Description:	t1394_add_cfgrom_entry() adds an entry to the local Config ROM,
 *		updating the directory entries as necessary.  This call could
 *		fail because there is no room for the new entry in Config ROM
 *		(T1394_ECFGROM_FULL), the key is invalid (T1394_EINVALID_PARAM),
 *		or it was called in interrupt context (T1394_EINVALID_CONTEXT).
 */
/* ARGSUSED */
int
t1394_add_cfgrom_entry(t1394_handle_t t1394_hdl,
    t1394_cfgrom_entryinfo_t *cfgrom_entryinfo, uint_t flags,
    t1394_cfgrom_handle_t *t1394_cfgrom_hdl, int *result)
{
	s1394_hal_t	*hal;
	s1394_target_t	*target;
	int		ret;
	uint_t		key;
	uint_t		size;
	uint32_t	*buffer;

	ASSERT(t1394_hdl != NULL);

	target = (s1394_target_t *)t1394_hdl;

	key = cfgrom_entryinfo->ce_key;
	buffer = cfgrom_entryinfo->ce_buffer;
	size = (uint_t)cfgrom_entryinfo->ce_size;

	/* Check for a valid size */
	if (size == 0) {
		*result = T1394_EINVALID_PARAM;
		return (DDI_FAILURE);
	}

	/* Check for a valid key type */
	if (((key << IEEE1212_KEY_VALUE_SHIFT) & IEEE1212_KEY_TYPE_MASK) == 0) {
		*result = T1394_EINVALID_PARAM;
		return (DDI_FAILURE);
	}

	/* Find the HAL this target resides on */
	hal = target->on_hal;

	/* Is this on the interrupt stack? */
	if (servicing_interrupt()) {
		*result = T1394_EINVALID_CONTEXT;
		return (DDI_FAILURE);
	}

	/* Lock the Config ROM buffer */
	mutex_enter(&hal->local_config_rom_mutex);

	ret = s1394_add_config_rom_entry(hal, key, buffer, size,
	    (void **)t1394_cfgrom_hdl, result);
	if (ret != DDI_SUCCESS) {
		if (*result == CMD1394_ERSRC_CONFLICT)
			*result = T1394_ECFGROM_FULL;
		mutex_exit(&hal->local_config_rom_mutex);

		return (ret);
	}

	/* Setup the timeout function */
	if (hal->config_rom_timer_set == B_FALSE) {
		hal->config_rom_timer_set = B_TRUE;
		mutex_exit(&hal->local_config_rom_mutex);
		hal->config_rom_timer =
		    timeout(s1394_update_config_rom_callback, hal,
			drv_usectohz(CONFIG_ROM_UPDATE_DELAY * 1000));
	} else {
		mutex_exit(&hal->local_config_rom_mutex);
	}

	return (ret);
}

/*
 * Function:    t1394_rem_cfgrom_entry()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		flags			The flags parameter is unused (for now)
 *		t1394_cfgrom_hdl	The ConfigROM "handle" returned by
 *					    t1394_add_cfgrom_entry()
 *
 * Output(s):	result			Used to pass more specific info back
 *					    to target
 *
 * Description:	t1394_rem_cfgrom_entry() is used to remove a previously added
 *		Config ROM entry (indicated by t1394_cfgrom_hdl).
 */
/* ARGSUSED */
int
t1394_rem_cfgrom_entry(t1394_handle_t t1394_hdl, uint_t flags,
    t1394_cfgrom_handle_t *t1394_cfgrom_hdl, int *result)
{
	s1394_hal_t	*hal;
	s1394_target_t	*target;
	int		ret;

	ASSERT(t1394_hdl != NULL);

	target = (s1394_target_t *)t1394_hdl;

	/* Find the HAL this target resides on */
	hal = target->on_hal;

	/* Is this on the interrupt stack? */
	if (servicing_interrupt()) {
		*result = T1394_EINVALID_CONTEXT;
		return (DDI_FAILURE);
	}

	/* Lock the Config ROM buffer */
	mutex_enter(&hal->local_config_rom_mutex);

	ret = s1394_remove_config_rom_entry(hal, (void **)t1394_cfgrom_hdl,
	    result);
	if (ret != DDI_SUCCESS) {
		mutex_exit(&hal->local_config_rom_mutex);
		return (ret);
	}

	/* Setup the timeout function */
	if (hal->config_rom_timer_set == B_FALSE) {
		hal->config_rom_timer_set = B_TRUE;
		mutex_exit(&hal->local_config_rom_mutex);
		hal->config_rom_timer =
		    timeout(s1394_update_config_rom_callback, hal,
			drv_usectohz(CONFIG_ROM_UPDATE_DELAY * 1000));
	} else {
		mutex_exit(&hal->local_config_rom_mutex);
	}

	return (ret);
}

/*
 * Function:    t1394_get_targetinfo()
 * Input(s):    t1394_hdl		The target "handle" returned by
 *					    t1394_attach()
 *		bus_generation		The current generation
 *		flags			The flags parameter is unused (for now)
 *
 * Output(s):	targetinfo		Structure containing max_payload,
 *					    max_speed, and target node ID.
 *
 * Description:	t1394_get_targetinfo() is used to retrieve information specific
 *		to a target device.  It will fail if the generation given
 *		does not match the current generation.
 */
/* ARGSUSED */
int
t1394_get_targetinfo(t1394_handle_t t1394_hdl, uint_t bus_generation,
    uint_t flags, t1394_targetinfo_t *targetinfo)
{
	s1394_hal_t	*hal;
	s1394_target_t	*target;
	uint_t		dev;
	uint_t		curr;
	uint_t		from_node;
	uint_t		to_node;

	ASSERT(t1394_hdl != NULL);

	/* Find the HAL this target resides on */
	hal = ((s1394_target_t *)t1394_hdl)->on_hal;

	target = (s1394_target_t *)t1394_hdl;

	/* Lock the topology tree */
	mutex_enter(&hal->topology_tree_mutex);

	/* Check the bus_generation */
	if (bus_generation != hal->generation_count) {
		/* Unlock the topology tree */
		mutex_exit(&hal->topology_tree_mutex);
		return (DDI_FAILURE);
	}

	rw_enter(&hal->target_list_rwlock, RW_READER);
	/*
	 * If there is no node, report T1394_INVALID_NODEID for target_nodeID;
	 * current_max_speed and current_max_payload are undefined for this
	 * case.
	 */
	if (((target->target_state & S1394_TARG_GONE) != 0) ||
	    (target->on_node == NULL)) {
		targetinfo->target_nodeID = T1394_INVALID_NODEID;
	} else {
		targetinfo->target_nodeID =
		    (target->on_hal->node_id & IEEE1394_BUS_NUM_MASK) |
		    target->on_node->node_num;

		from_node = (target->on_hal->node_id) & IEEE1394_NODE_NUM_MASK;
		to_node = target->on_node->node_num;

		targetinfo->current_max_speed = (uint_t)s1394_speed_map_get(
		    hal, from_node, to_node);

		/* Get current_max_payload */
		s1394_get_maxpayload(target, &dev, &curr);
		targetinfo->current_max_payload	= curr;
	}

	rw_exit(&hal->target_list_rwlock);
	/* Unlock the topology tree */
	mutex_exit(&hal->topology_tree_mutex);
	return (DDI_SUCCESS);
}