summaryrefslogtreecommitdiffstats
path: root/sys/dev/twa/twa.c
blob: d14a968fac9ba591c2bbab25cccb0373d0115036 (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
/*-
 * Copyright (c) 2003-04 3ware, Inc.
 * Copyright (c) 2000 Michael Smith
 * Copyright (c) 2000 BSDi
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$FreeBSD$
 */

/*
 * 3ware driver for 9000 series storage controllers.
 *
 * Author: Vinod Kashyap
 */


#include <dev/twa/twa_includes.h>

#ifdef TWA_FLASH_FIRMWARE
static int	twa_flash_firmware(struct twa_softc *sc);
static int	twa_hard_reset(struct twa_softc *sc);
#endif /* TWA_FLASH_FIRMWARE */

static int	twa_init_ctlr(struct twa_softc *sc);
static void	*twa_get_param(struct twa_softc *sc, int table_id,
					int parameter_id, size_t size,
					void (* callback)(struct twa_request *tr));
static int	twa_set_param(struct twa_softc *sc, int table_id, int param_id,
					int param_size, void *data,
					void (* callback)(struct twa_request *tr));
static int	twa_init_connection(struct twa_softc *sc, u_int16_t message_credits,
				u_int32_t set_features, u_int16_t current_fw_srl,
				u_int16_t current_fw_arch_id, u_int16_t current_fw_branch,
				u_int16_t current_fw_build, u_int16_t *fw_on_ctlr_srl,
				u_int16_t *fw_on_ctlr_arch_id, u_int16_t *fw_on_ctlr_branch,
				u_int16_t *fw_on_ctlr_build, u_int32_t *init_connect_result);

static int	twa_wait_request(struct twa_request *req, u_int32_t timeout);
static int	twa_immediate_request(struct twa_request *req, u_int32_t timeout);

static int	twa_done(struct twa_softc *sc);
static int	twa_drain_pending_queue(struct twa_softc *sc);
static void	twa_drain_complete_queue(struct twa_softc *sc);
static int	twa_wait_status(struct twa_softc *sc, u_int32_t status, u_int32_t timeout);
static int	twa_drain_response_queue(struct twa_softc *sc);
static int	twa_check_ctlr_state(struct twa_softc *sc, u_int32_t status_reg);
static int	twa_soft_reset(struct twa_softc *sc);

static void	twa_host_intr(struct twa_softc *sc);
static void	twa_attention_intr(struct twa_softc *sc);
static void	twa_command_intr(struct twa_softc *sc);

static int	twa_fetch_aen(struct twa_softc *sc);
static void	twa_aen_callback(struct twa_request *tr);
static unsigned short	twa_enqueue_aen(struct twa_softc *sc,
				struct twa_command_header *cmd_hdr);
static int	twa_drain_aen_queue(struct twa_softc *sc);
static int	twa_find_aen(struct twa_softc *sc, u_int16_t aen_code);

static void	twa_panic(struct twa_softc *sc, int8_t *reason);

/*
 * Function name:	twa_setup
 * Description:		Initializes driver data structures for the controller.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
int
twa_setup(struct twa_softc *sc)
{
	struct twa_event_packet	*aen_queue;
	int			error = 0;
	int			i;

	twa_dbg_dprint_enter(3, sc);

	/* Initialize request queues. */
	twa_initq_free(sc);
	twa_initq_busy(sc);
	twa_initq_pending(sc);
	twa_initq_complete(sc);

	if (twa_alloc_req_pkts(sc, TWA_Q_LENGTH)) {
		twa_printf(sc, "Failed to allocate request packets.\n");
		return(ENOMEM);
	}

	/* Allocate memory for the AEN queue. */
	if ((aen_queue = malloc(sizeof(struct twa_event_packet) * TWA_Q_LENGTH,
					M_DEVBUF, M_WAITOK)) == NULL) {
		/* 
		 * This should not cause us to return error.  We will only be
		 * unable to support AEN's.  But then, we will have to check
		 * time and again to see if we can support AEN's, if we
		 * continue.  So, we will just return error.
		 */
		twa_printf(sc, "Could not allocate memory for AEN queue.\n");
		return(ENOMEM); /* any unfreed memory will be freed by twa_free */
	}
	/* Initialize the aen queue. */
	bzero(aen_queue, sizeof(struct twa_event_packet) * TWA_Q_LENGTH);
	for (i = 0; i < TWA_Q_LENGTH; i++)
		sc->twa_aen_queue[i] = &(aen_queue[i]);

	/* Disable interrupts. */
	twa_disable_interrupts(sc);

	/* Initialize the controller. */
	if ((error = twa_init_ctlr(sc))) {
		/* Soft reset the controller, and try one more time. */
		twa_printf(sc, "Controller initialization failed. Retrying...\n");
		if ((error = twa_soft_reset(sc)))
			twa_printf(sc, "Controller soft reset failed.\n");
		else
			error = twa_init_ctlr(sc);
	}
	return(error);
}

#ifdef TWA_FLASH_FIRMWARE
/*
 * Function name:	twa_flash_firmware
 * Description:		Flashes bundled firmware image onto controller.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_flash_firmware(struct twa_softc *sc)
{
	struct twa_request			*tr;
	struct twa_command_header		*cmd_hdr;
	struct twa_command_download_firmware	*cmd;
	u_int32_t				fw_img_chunk_size;
	u_int32_t				this_chunk_size = 0;
	u_int32_t				remaining_img_size = 0;
	u_int8_t				*error_str;
	int					error;
	int					i;

	if ((tr = twa_get_request(sc)) == NULL) {
		/* No free request packets available.  Can't proceed. */
		error = EIO;
		goto out;
	}
	tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
	/* Allocate sufficient memory to hold a chunk of the firmware image. */
	fw_img_chunk_size = ((twa_fw_img_size/NUM_FW_IMAGE_CHUNKS) + 511) & ~511;
	if ((tr->tr_data = malloc(fw_img_chunk_size, M_DEVBUF, M_WAITOK)) == NULL) {
		twa_printf (sc, "Could not allocate memory for firmware image.\n"); 
		error = ENOMEM;
		goto out;
	}
	remaining_img_size = twa_fw_img_size;
	cmd_hdr = &(tr->tr_command->cmd_hdr);
	cmd = &(tr->tr_command->command.cmd_pkt_7k.download_fw);

	for (i = 0; i < NUM_FW_IMAGE_CHUNKS; i++) {
		/* Build a cmd pkt for downloading firmware. */
		bzero(tr->tr_command, sizeof(struct twa_command_packet));

		cmd_hdr->header_desc.size_header = 128;
	
		cmd->opcode = TWA_OP_DOWNLOAD_FIRMWARE;
		cmd->sgl_offset = 2;/* offset in dwords, to the beginning of sg list */
		cmd->size = 2;	/* this field will be updated at data map time */
		cmd->request_id = tr->tr_request_id;
		cmd->unit = 0;
		cmd->status = 0;
		cmd->flags = 0;
		cmd->param = 8;	/* prom image */

		if (i != (NUM_FW_IMAGE_CHUNKS - 1))
			this_chunk_size = fw_img_chunk_size;
		else	 /* last chunk */
			this_chunk_size = remaining_img_size;
	
		remaining_img_size -= this_chunk_size;
		bcopy(twa_fw_img + (i * fw_img_chunk_size),
					tr->tr_data, this_chunk_size);

		/*
		 * The next line will effect only the last chunk.
		 */
		tr->tr_length = (this_chunk_size + 511) & ~511;

		tr->tr_flags |= TWA_CMD_DATA_OUT;

		error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
		if (error) {
			twa_printf(sc, "Firmware flash request could not be posted. error = 0x%x\n",
									error);
			if (error == ETIMEDOUT)
				return(error); /* clean-up done by twa_immediate_request */
			break;
		}
		error = cmd->status;
		if (i != (NUM_FW_IMAGE_CHUNKS - 1)) {
			if ((error = cmd_hdr->status_block.error) != TWA_ERROR_MORE_DATA) {
				error_str = 
				&(cmd_hdr->err_desc[strlen(cmd_hdr->err_desc) + 1]);
				if (error_str[0] == '\0')
					error_str = twa_find_msg_string(twa_error_table, error);

				twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
					cmd->opcode,
					TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
					error,
					error_str,
					cmd_hdr->err_desc);
				twa_printf(sc, "Firmware flash request failed. Intermediate error = 0x%x, i = %x\n",
							cmd->status, i);
				/* Hard reset the controller, so that it doesn't wait for the remaining chunks. */
				twa_hard_reset(sc);
				break;
			}
		} else	 /* last chunk */
			if (error) {
				error_str =
				&(cmd_hdr->err_desc[strlen(cmd_hdr->err_desc) + 1]);
				if (error_str[0] == '\0')
					error_str = twa_find_msg_string(twa_error_table,
						cmd_hdr->status_block.error);

				twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
					cmd->opcode,
					TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
					cmd_hdr->status_block.error,
					error_str,
					cmd_hdr->err_desc);
				twa_printf(sc, "Firmware flash request failed. error = 0x%x\n", error);
				/* Hard reset the controller, so that it doesn't wait for more chunks. */
				twa_hard_reset(sc);
			}
	} /* for */

	if (tr->tr_data)
		free(tr->tr_data, M_DEVBUF);
out:
	if (tr)
		twa_release_request(tr);
	return(error);
}


/*
 * Function name:	twa_hard_reset
 * Description:		Hard reset the controller.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_hard_reset(struct twa_softc *sc)
{
	struct twa_request			*tr;
	struct twa_command_header		*cmd_hdr;
	struct twa_command_reset_firmware	*cmd;
	int					error;

	if ((tr = twa_get_request(sc)) == NULL)
		return(EIO);
	tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
	/* Build a cmd pkt for sending down the hard reset command. */
	cmd_hdr = &(tr->tr_command->cmd_hdr);
	cmd_hdr->header_desc.size_header = 128;
	
	cmd = &(tr->tr_command->command.cmd_pkt_7k.reset_fw);
	cmd->opcode = TWA_OP_RESET_FIRMWARE;
	cmd->size = 2;	/* this field will be updated at data map time */
	cmd->request_id = tr->tr_request_id;
	cmd->unit = 0;
	cmd->status = 0;
	cmd->flags = 0;
	cmd->param = 0;	/* don't reload FPGA logic */

	tr->tr_data = NULL;
	tr->tr_length = 0;

	error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
	if (error) {
		twa_printf(sc, "Hard reset request could not be posted. error = 0x%x\n",
								error);
		if (error == ETIMEDOUT)
			return(error); /* clean-up done by twa_immediate_request */
		goto out;
	}
	if ((error = cmd->status)) {
		u_int8_t *error_str =
		&(cmd_hdr->err_desc[strlen(cmd_hdr->err_desc) + 1]);

		if (error_str[0] == '\0')
			error_str = twa_find_msg_string(twa_error_table,
					cmd_hdr->status_block.error);

		twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
					cmd->opcode,
					TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
					cmd_hdr->status_block.error,
					error_str,
					cmd_hdr->err_desc);
		twa_printf(sc, "Hard reset request failed. error = 0x%x\n", error);
	}

out:
	if (tr)
		twa_release_request(tr);
	return(error);
}

#endif /* TWA_FLASH_FIRMWARE */

/*
 * Function name:	twa_init_ctlr
 * Description:		Establishes a logical connection with the controller.
 *			If bundled with firmware, determines whether or not
 *			to flash firmware, based on arch_id, fw SRL (Spec.
 *			Revision Level), branch & build #'s.  Also determines
 *			whether or not the driver is compatible with the
 *			firmware on the controller, before proceeding to work
 *			with it.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_init_ctlr(struct twa_softc *sc)
{
	u_int16_t	fw_on_ctlr_srl = 0;
	u_int16_t	fw_on_ctlr_arch_id = 0;
	u_int16_t	fw_on_ctlr_branch = 0;
	u_int16_t	fw_on_ctlr_build = 0;
	u_int32_t	init_connect_result = 0;
	int		error = 0;
#ifdef TWA_FLASH_FIRMWARE
	int8_t		fw_flashed = FALSE;
	int8_t		fw_flash_failed = FALSE;
#endif /* TWA_FLASH_FIRMWARE */

	twa_dbg_dprint_enter(3, sc);

	/* Wait for the controller to become ready. */
	if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY,
					TWA_REQUEST_TIMEOUT_PERIOD)) {
		twa_printf(sc, "Microcontroller not ready.\n");
		return(ENXIO);
	}
	/* Drain the response queue. */
	if (twa_drain_response_queue(sc)) {
		twa_printf(sc, "Can't drain response queue.\n");
		return(1);
	}
	/* Establish a logical connection with the controller. */
	if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
			TWA_EXTENDED_INIT_CONNECT, TWA_CURRENT_FW_SRL,
			TWA_9000_ARCH_ID, TWA_CURRENT_FW_BRANCH,
			TWA_CURRENT_FW_BUILD, &fw_on_ctlr_srl,
			&fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
			&fw_on_ctlr_build, &init_connect_result))) {
		twa_printf(sc, "Can't initialize connection in current mode.\n");
		return(error);
	}

#ifdef TWA_FLASH_FIRMWARE

	if ((init_connect_result & TWA_BUNDLED_FW_SAFE_TO_FLASH) &&
		(init_connect_result & TWA_CTLR_FW_RECOMMENDS_FLASH)) {
		/*
		 * The bundled firmware is safe to flash, and the firmware
		 * on the controller recommends a flash.  So, flash!
		 */
		twa_printf(sc, "Flashing bundled firmware...\n");
		if ((error = twa_flash_firmware(sc))) {
			fw_flash_failed = TRUE;
			twa_printf(sc, "Unable to flash bundled firmware.\n");
			twa_printf(sc, "Will see if possible to work with firmware on controller...\n");
		} else {
			twa_printf(sc, "Successfully flashed bundled firmware.\n");
			fw_flashed = TRUE;
		}
	}

	if (fw_flashed) {
		/* The firmware was flashed.  Have the new image loaded */
		error = twa_hard_reset(sc);
		if (error)
			twa_printf(sc, "Could not reset controller after flash!\n");
		else	/* Go through initialization again. */
			error = twa_init_ctlr(sc);
		/*
		 * If hard reset of controller failed, we need to return.
		 * Otherwise, the above recursive call to twa_init_ctlr will
		 * have completed the rest of the initialization (starting
		 * from twa_drain_aen_queue below).  Don't do it again.
		 * Just return.
		 */
		return(error);
	} else
#endif /* TWA_FLASH_FIRMWARE */
	{
		/*
		 * Either we are not bundled with a firmware image, or
		 * the bundled firmware is not safe to flash,
		 * or flash failed for some reason.  See if we can at
		 * least work with the firmware on the controller in the
		 * current mode.
		 */
		if (init_connect_result & TWA_CTLR_FW_COMPATIBLE) {
			/* Yes, we can.  Make note of the operating mode. */
			sc->working_srl = TWA_CURRENT_FW_SRL;
			sc->working_branch = TWA_CURRENT_FW_BRANCH;
			sc->working_build = TWA_CURRENT_FW_BUILD;
		} else {
			/*
			 * No, we can't.  See if we can at least work with
			 * it in the base mode.  We should never come here
			 * if firmware has just been flashed.
			 */
			twa_printf(sc, "Driver/Firmware mismatch.  Negotiating for base level...\n");
			if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
					TWA_EXTENDED_INIT_CONNECT, TWA_BASE_FW_SRL,
					TWA_9000_ARCH_ID, TWA_BASE_FW_BRANCH,
					TWA_BASE_FW_BUILD, &fw_on_ctlr_srl,
					&fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
					&fw_on_ctlr_build, &init_connect_result))) {
				twa_printf(sc, "Can't initialize connection in base mode.\n");
				return(error);
			}
			if (!(init_connect_result & TWA_CTLR_FW_COMPATIBLE)) {
				/*
				 * The firmware on the controller is not even
				 * compatible with our base mode.  We cannot
				 * work with it.  Bail...
				 */
				twa_printf(sc, "Incompatible firmware on controller\n");
#ifdef TWA_FLASH_FIRMWARE
				if (fw_flash_failed)
					twa_printf(sc, "...and could not flash bundled firmware.\n");
				else
					twa_printf(sc, "...and bundled firmware not safe to flash.\n");
#endif /* TWA_FLASH_FIRMWARE */
				return(1);
			}
			/* We can work with this firmware, but only in base mode. */
			sc->working_srl = TWA_BASE_FW_SRL;
			sc->working_branch = TWA_BASE_FW_BRANCH;
			sc->working_build = TWA_BASE_FW_BUILD;
			sc->twa_operating_mode = TWA_BASE_MODE;
		}
	}

	/* Drain the AEN queue */
	if (twa_drain_aen_queue(sc)) {
		/* 
		 * We will just print that we couldn't drain the AEN queue.
		 * There's no need to bail out.
		 */
		twa_printf(sc, "Can't drain AEN queue.\n");
	}

	/* Set controller state to initialized. */
	sc->twa_state &= ~TWA_STATE_SHUTDOWN;

	twa_enable_interrupts(sc);
	twa_dbg_dprint_exit(3, sc);
	return(0);
}


/*
 * Function name:	twa_deinit_ctlr
 * Description:		Close logical connection with the controller.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
int
twa_deinit_ctlr(struct twa_softc *sc)
{
	/*
	 * Mark the controller as shutting down,
	 * and disable any further interrupts.
	 */
	sc->twa_state |= TWA_STATE_SHUTDOWN;
	twa_disable_interrupts(sc);

	/* Let the controller know that we are going down. */
	return(twa_init_connection(sc, TWA_SHUTDOWN_MESSAGE_CREDITS,
					0, 0, 0, 0, 0,
					NULL, NULL, NULL, NULL, NULL));
}


/*
 * Function name:	twa_interrupt
 * Description:		Interrupt handler.  Determines the kind of interrupt,
 *			and calls the appropriate handler.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	None
 */
void
twa_interrupt(struct twa_softc *sc)
{
	u_int32_t	status_reg;
	int		s;

	s = splcam();
	twa_dbg_dprint_enter(5, sc);

	/* Collect current interrupt status. */
	status_reg = TWA_READ_STATUS_REGISTER(sc);
	if (twa_check_ctlr_state(sc, status_reg))
		return;

	/* Dispatch based on the kind of interrupt. */
	if (status_reg & TWA_STATUS_HOST_INTERRUPT)
		twa_host_intr(sc);
	if (status_reg & TWA_STATUS_ATTENTION_INTERRUPT)
		twa_attention_intr(sc);
	if (status_reg & TWA_STATUS_COMMAND_INTERRUPT)
		twa_command_intr(sc);
	if (status_reg & TWA_STATUS_RESPONSE_INTERRUPT)
		twa_done(sc);
	splx(s);
}


/*
 * Function name:	twa_ioctl
 * Description:		ioctl handler.
 *
 * Input:		sc	-- ptr to per ctlr structure
 *			cmd	-- ioctl cmd
 *			buf	-- ptr to buffer in kernel memory, which is
 *				   a copy of the input buffer in user-space
 * Output:		buf	-- ptr to buffer in kernel memory, which will
 *				   be copied of the output buffer in user-space
 * Return value:	0	-- success
 *			non-zero-- failure
 */
int
twa_ioctl(struct twa_softc *sc, int cmd, void *buf)
{
	struct twa_ioctl_9k	*user_buf = (struct twa_ioctl_9k *)buf;
	struct twa_event_packet	event_buf;
	int32_t			event_index;
	int32_t			start_index;
	int			s;
	int			error = 0;
		
	switch (cmd) {
	case TWA_IOCTL_FIRMWARE_PASS_THROUGH:
	{
		struct twa_command_packet	*cmdpkt;
		struct twa_request 		*tr;
		u_int32_t			data_buf_size_adjusted;


		twa_dbg_dprint(2, sc, "Firmware PassThru");

		/* Get a request packet */
		while ((tr = twa_get_request(sc)) == NULL)
			/*
			 * No free request packets available.  Sleep until
			 * one becomes available.
			 */
			tsleep(&(sc->twa_wait_timeout), PPAUSE, "twioctl", hz);

		/*
		 * Make sure that the data buffer sent to firmware is a 
		 * 512 byte multiple in size.
		 */
  		data_buf_size_adjusted = (user_buf->twa_drvr_pkt.buffer_length + 511) & ~511;
		if ((tr->tr_length = data_buf_size_adjusted)) {
			if ((tr->tr_data = malloc(data_buf_size_adjusted, M_DEVBUF, M_WAITOK)) == NULL) {
				twa_printf(sc, "Could not alloc mem for fw_passthru data_buf.\n");
				error = ENOMEM;
				goto fw_passthru_done;
			}
			/* Copy the payload. */
			if ((error = copyin((void *) (user_buf->pdata), 
					(void *) (tr->tr_data),
					user_buf->twa_drvr_pkt.buffer_length)) != 0) {
				twa_printf (sc, "Could not copyin fw_passthru data_buf.\n"); 
				goto fw_passthru_done;
			}
			tr->tr_flags |= TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
		}
		tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_IOCTL;
		cmdpkt = tr->tr_command;

		/* Copy the command packet. */
		bcopy(&(user_buf->twa_cmd_pkt), cmdpkt,
					sizeof(struct twa_command_packet));
		cmdpkt->command.cmd_pkt_7k.generic.request_id = tr->tr_request_id;

		twa_dbg_dprint(3, sc, "cmd_pkt_7k = %x %x %x %x %x %x %x",
					cmdpkt->command.cmd_pkt_7k.generic.opcode,
					cmdpkt->command.cmd_pkt_7k.generic.sgl_offset,
					cmdpkt->command.cmd_pkt_7k.generic.size,
					cmdpkt->command.cmd_pkt_7k.generic.request_id,
					cmdpkt->command.cmd_pkt_7k.generic.unit,
					cmdpkt->command.cmd_pkt_7k.generic.status,
					cmdpkt->command.cmd_pkt_7k.generic.flags);

		/* Send down the request, and wait for it to complete. */
		if ((error = twa_wait_request(tr, TWA_REQUEST_TIMEOUT_PERIOD))) {
			twa_printf(sc, "fw_passthru request failed. error = 0x%x\n", error);
			if (error == ETIMEDOUT)
				break; /* clean-up done by twa_wait_request */
			goto fw_passthru_done;
		}

		/* Copy the command packet back into user space. */
		bcopy(cmdpkt, &(user_buf->twa_cmd_pkt),
					sizeof(struct twa_command_packet));
	
		/* If there was a payload, copy it back too. */
		if (tr->tr_length)
			error = copyout(tr->tr_data, user_buf->pdata,
					user_buf->twa_drvr_pkt.buffer_length);

fw_passthru_done:
		/* Free resources. */
		if (tr->tr_data)
			free(tr->tr_data, M_DEVBUF);
		if (tr)
			twa_release_request(tr);
		break;
	}


	case TWA_IOCTL_SCAN_BUS:
		/* Request CAM for a bus scan. */
		twa_request_bus_scan(sc);
		break;


	case TWA_IOCTL_GET_FIRST_EVENT:
		twa_dbg_dprint(3, sc, "Get First Event");

		if (sc->twa_aen_queue_wrapped) {
			if (sc->twa_aen_queue_overflow) {
				/*
				 * The aen queue has wrapped, even before some
				 * events have been retrieved.  Let the caller
				 * know that he missed out on some AEN's.
				 */
				user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
				sc->twa_aen_queue_overflow = FALSE;
			} else
				user_buf->twa_drvr_pkt.status = 0;
			event_index = sc->twa_aen_head;
		} else {
			if (sc->twa_aen_head == sc->twa_aen_tail) {
				user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
				break;
			}
			user_buf->twa_drvr_pkt.status = 0;
			event_index = sc->twa_aen_tail;	/* = 0 */
		}
		if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
					sizeof(struct twa_event_packet))) != 0)
			twa_printf(sc, "get_first: Could not copyout to event_buf. error = %x\n", error);
		(sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
		break;


	case TWA_IOCTL_GET_LAST_EVENT:
		twa_dbg_dprint(3, sc, "Get Last Event");

		if (sc->twa_aen_queue_wrapped) {
			if (sc->twa_aen_queue_overflow) {
				/*
				 * The aen queue has wrapped, even before some
				 * events have been retrieved.  Let the caller
				 * know that he missed out on some AEN's.
				 */
				user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
				sc->twa_aen_queue_overflow = FALSE;
			} else
				user_buf->twa_drvr_pkt.status = 0;
		} else {
			if (sc->twa_aen_head == sc->twa_aen_tail) {
				user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
				break;
			}
			user_buf->twa_drvr_pkt.status = 0;
		}
		event_index = (sc->twa_aen_head - 1 + TWA_Q_LENGTH) % TWA_Q_LENGTH;
		if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
					sizeof(struct twa_event_packet))) != 0)
			twa_printf(sc, "get_last: Could not copyout to event_buf. error = %x\n", error);
		(sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
		break;


	case TWA_IOCTL_GET_NEXT_EVENT:
		twa_dbg_dprint(3, sc, "Get Next Event");

		user_buf->twa_drvr_pkt.status = 0;
		if (sc->twa_aen_queue_wrapped) {
			twa_dbg_dprint(3, sc, "Get Next Event: wrapped");
			if (sc->twa_aen_queue_overflow) {
				/*
				 * The aen queue has wrapped, even before some
				 * events have been retrieved.  Let the caller
				 * know that he missed out on some AEN's.
				 */
				twa_dbg_dprint(2, sc, "Get Next Event: overflow");
				user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
				sc->twa_aen_queue_overflow = FALSE;
			}
			start_index = sc->twa_aen_head;
		} else {
			if (sc->twa_aen_head == sc->twa_aen_tail) {
				twa_dbg_dprint(3, sc, "Get Next Event: empty queue");
				user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
				break;
			}
			start_index = sc->twa_aen_tail;	/* = 0 */
		}
		if ((error = copyin(user_buf->pdata, &event_buf,
				sizeof(struct twa_event_packet))) != 0)
			twa_printf(sc, "get_next: Could not copyin event_buf.\n");

		event_index = (start_index + event_buf.sequence_id -
				(sc->twa_aen_queue[start_index])->sequence_id + 1)
				% TWA_Q_LENGTH;

		twa_dbg_dprint(3, sc, "Get Next Event: si = %x, ei = %x, ebsi = %x, sisi = %x, eisi = %x",
				start_index, event_index, event_buf.sequence_id,
				(sc->twa_aen_queue[start_index])->sequence_id,
				(sc->twa_aen_queue[event_index])->sequence_id);

		if (! ((sc->twa_aen_queue[event_index])->sequence_id >
						event_buf.sequence_id)) {
			if (user_buf->twa_drvr_pkt.status == TWA_ERROR_AEN_OVERFLOW)
				sc->twa_aen_queue_overflow = TRUE; /* so we report the overflow next time */
			user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
			break;
		}
		if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata, 
					sizeof(struct twa_event_packet))) != 0)
			twa_printf(sc, "get_next: Could not copyout to event_buf. error = %x\n", error);

		(sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
		break;


	case TWA_IOCTL_GET_PREVIOUS_EVENT:
		twa_dbg_dprint(3, sc, "Get Previous Event");

		user_buf->twa_drvr_pkt.status = 0;
		if (sc->twa_aen_queue_wrapped) {
			if (sc->twa_aen_queue_overflow) {
				/*
				 * The aen queue has wrapped, even before some
				 * events have been retrieved.  Let the caller
				 * know that he missed out on some AEN's.
				 */
				user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
				sc->twa_aen_queue_overflow = FALSE;
			}
			start_index = sc->twa_aen_head;
		} else {
			if (sc->twa_aen_head == sc->twa_aen_tail) {
				user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
				break;
			}
			start_index = sc->twa_aen_tail;	/* = 0 */
		}
		if ((error = copyin(user_buf->pdata, &event_buf,
				sizeof(struct twa_event_packet))) != 0)
			twa_printf(sc, "get_previous: Could not copyin event_buf.\n");

		event_index = (start_index + event_buf.sequence_id -
			(sc->twa_aen_queue[start_index])->sequence_id - 1) % TWA_Q_LENGTH;
		if (! ((sc->twa_aen_queue[event_index])->sequence_id < event_buf.sequence_id)) {
			if (user_buf->twa_drvr_pkt.status == TWA_ERROR_AEN_OVERFLOW)
				sc->twa_aen_queue_overflow = TRUE; /* so we report the overflow next time */
			user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
			break;
		}
		if ((error = copyout(sc->twa_aen_queue [event_index], user_buf->pdata,
					sizeof(struct twa_event_packet))) != 0)
			twa_printf(sc, "get_previous: Could not copyout to event_buf. error = %x\n", error);

		(sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
		break;


	case TWA_IOCTL_GET_LOCK:
	{
		struct twa_lock_packet	twa_lock;
		u_int32_t		cur_time;

		cur_time = time_second - (tz_minuteswest * 60) - 
					(wall_cmos_clock ? adjkerntz : 0);
		copyin(user_buf->pdata, &twa_lock,
				sizeof(struct twa_lock_packet));
		s = splcam();
		if ((sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) ||
				(twa_lock.force_flag) ||
				(cur_time >= sc->twa_ioctl_lock.timeout)) {
			twa_dbg_dprint(3, sc, "GET_LOCK: Getting lock!");
			sc->twa_ioctl_lock.lock = TWA_LOCK_HELD;
			sc->twa_ioctl_lock.timeout = cur_time + (twa_lock.timeout_msec / 1000);
			twa_lock.time_remaining_msec = twa_lock.timeout_msec;
			user_buf->twa_drvr_pkt.status = 0;
		} else {
			twa_dbg_dprint(2, sc, "GET_LOCK: Lock already held!");
			twa_lock.time_remaining_msec =
				(sc->twa_ioctl_lock.timeout - cur_time) * 1000;
			user_buf->twa_drvr_pkt.status =
					TWA_ERROR_IOCTL_LOCK_ALREADY_HELD;
		}
		splx(s);
		copyout(&twa_lock, user_buf->pdata,
				sizeof(struct twa_lock_packet));
		break;
	}


	case TWA_IOCTL_RELEASE_LOCK:
		s = splcam();
		if (sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) {
			twa_dbg_dprint(2, sc, "twa_ioctl: RELEASE_LOCK: Lock not held!");
			user_buf->twa_drvr_pkt.status = TWA_ERROR_IOCTL_LOCK_NOT_HELD;
		} else {
			twa_dbg_dprint(3, sc, "RELEASE_LOCK: Releasing lock!");
			sc->twa_ioctl_lock.lock = TWA_LOCK_FREE;
			user_buf->twa_drvr_pkt.status = 0;
		}
		splx(s);
		break;


	case TWA_IOCTL_GET_COMPATIBILITY_INFO:
	{
		struct twa_compatibility_packet	comp_pkt;

		bcopy(TWA_DRIVER_VERSION_STRING, comp_pkt.driver_version,
					sizeof(TWA_DRIVER_VERSION_STRING));
		comp_pkt.working_srl = sc->working_srl;
		comp_pkt.working_branch = sc->working_branch;
		comp_pkt.working_build = sc->working_build;
		user_buf->twa_drvr_pkt.status = 0;

		/* Copy compatibility information to user space. */
		copyout(&comp_pkt, user_buf->pdata,
				min(sizeof(struct twa_compatibility_packet),
					user_buf->twa_drvr_pkt.buffer_length));
		break;
	}

	default:	
		/* Unknown opcode. */
		error = ENOTTY;
	}

	return(error);
}


/*
 * Function name:	twa_enable_interrupts
 * Description:		Enables interrupts on the controller
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	None
 */
void
twa_enable_interrupts(struct twa_softc *sc)
{
	sc->twa_state |= TWA_STATE_INTR_ENABLED;
	TWA_WRITE_CONTROL_REGISTER(sc,
		TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
		TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
		TWA_CONTROL_ENABLE_INTERRUPTS);
}


/*
 * Function name:	twa_setup
 * Description:		Disables interrupts on the controller
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	None
 */
void
twa_disable_interrupts(struct twa_softc *sc)
{
	TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_DISABLE_INTERRUPTS);
	sc->twa_state &= ~TWA_STATE_INTR_ENABLED;
}



/*
 * Function name:	twa_get_param
 * Description:		Get a firmware parameter.
 *
 * Input:		sc		-- ptr to per ctlr structure
 *			table_id	-- parameter table #
 *			param_id	-- index of the parameter in the table
 *			param_size	-- size of the parameter in bytes
 *			callback	-- ptr to function, if any, to be called
 *					back on completion; NULL if no callback.
 * Output:		None
 * Return value:	ptr to param structure	-- success
 *			NULL			-- failure
 */
static void *
twa_get_param(struct twa_softc *sc, int table_id, int param_id,
		size_t param_size, void (* callback)(struct twa_request *tr))
{
	struct twa_request		*tr;
	struct twa_command_header	*cmd_hdr;
	union twa_command_7k		*cmd;
	struct twa_param_9k		*param = NULL;
	int				error = ENOMEM;

	twa_dbg_dprint_enter(4, sc);

	/* Get a request packet. */
	if ((tr = twa_get_request(sc)) == NULL)
		goto out;
	tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;

	/* Allocate memory to read data into. */
	if ((param = (struct twa_param_9k *)
			malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
		goto out;
	bzero(param, sizeof(struct twa_param_9k) - 1 + param_size);
	tr->tr_data = param;
	tr->tr_length = TWA_SECTOR_SIZE;
	tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;

	/* Build the cmd pkt. */
	cmd_hdr = &(tr->tr_command->cmd_hdr);
	cmd_hdr->header_desc.size_header = 128;
	
	cmd = &(tr->tr_command->command.cmd_pkt_7k);
	cmd->param.opcode = TWA_OP_GET_PARAM;
	cmd->param.sgl_offset = 2;
	cmd->param.size = 2;
	cmd->param.request_id = tr->tr_request_id;
	cmd->param.unit = 0;
	cmd->param.param_count = 1;

	/* Specify which parameter we need. */
	param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
	param->parameter_id = param_id;
	param->parameter_size_bytes = param_size;

	/* Submit the command. */
	if (callback == NULL) {
		/* There's no call back; wait till the command completes. */
		error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
		if (error == ETIMEDOUT)
			return(NULL); /* clean-up done by twa_immediate_request */
		if (error)
			goto out;
		if ((error = cmd->param.status)) {
			u_int8_t *error_str =
			&(cmd_hdr->err_desc[strlen(cmd_hdr->err_desc) + 1]);

			if (error_str[0] == '\0')
				error_str = twa_find_msg_string(twa_error_table,
						cmd_hdr->status_block.error);

			twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
					cmd->param.opcode,
					TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
					cmd_hdr->status_block.error,
					error_str,
					cmd_hdr->err_desc);
			goto out; /* twa_drain_complete_queue will have done the unmapping */
		}
		twa_release_request(tr);
		return(param);
	} else {
		/* There's a call back.  Simply submit the command. */
		tr->tr_callback = callback;
		if ((error = twa_map_request(tr))) {
			twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
						__func__, error);
			goto out;
		}
		return(callback);
	}

out:
	twa_printf(sc, "get_param failed. error = 0x%x\n", error);
	if (param)
		free(param, M_DEVBUF);
	if (tr)
		twa_release_request(tr);
	return(NULL);
}


/*
 * Function name:	twa_set_param
 * Description:		Set a firmware parameter.
 *
 * Input:		sc		-- ptr to per ctlr structure
 *			table_id	-- parameter table #
 *			param_id	-- index of the parameter in the table
 *			param_size	-- size of the parameter in bytes
 *			callback	-- ptr to function, if any, to be called
 *					back on completion; NULL if no callback.
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_set_param(struct twa_softc *sc, int table_id,
			int param_id, int param_size, void *data,
			void (* callback)(struct twa_request *tr))
{
	struct twa_request		*tr;
	struct twa_command_header	*cmd_hdr;
	union twa_command_7k		*cmd;
	struct twa_param_9k		*param = NULL;
	int				error = ENOMEM;

	twa_dbg_dprint_enter(4, sc);

	/* Get a request packet. */
	if ((tr = twa_get_request(sc)) == NULL)
		goto out;
	tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;

	/* Allocate memory to send data using. */
	if ((param = (struct twa_param_9k *)
			malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
		goto out;
	bzero(param, sizeof(struct twa_param_9k) - 1 + param_size);
	tr->tr_data = param;
	tr->tr_length = TWA_SECTOR_SIZE;
	tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;

	/* Build the cmd pkt. */
	cmd_hdr = &(tr->tr_command->cmd_hdr);
	cmd_hdr->header_desc.size_header = 128;

	cmd = &(tr->tr_command->command.cmd_pkt_7k);
	cmd->param.opcode = TWA_OP_SET_PARAM;
	cmd->param.sgl_offset = 2;
	cmd->param.size = 2;
	cmd->param.request_id = tr->tr_request_id;
	cmd->param.unit = 0;
	cmd->param.param_count = 1;

	/* Specify which parameter we want to set. */
	param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
	param->parameter_id = param_id;
	param->parameter_size_bytes = param_size;
	bcopy(data, param->data, param_size);

	/* Submit the command. */
	if (callback == NULL) {
		/* There's no call back;  wait till the command completes. */
		error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
		if (error == ETIMEDOUT)
			return(error); /* clean-up done by twa_immediate_request */
		if (error)
			goto out;
		if ((error = cmd->param.status)) {
			u_int8_t *error_str =
			&(cmd_hdr->err_desc[strlen(cmd_hdr->err_desc) + 1]);

			if (error_str[0] == '\0')
				error_str = twa_find_msg_string(twa_error_table,
						cmd_hdr->status_block.error);
			twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
					cmd->param.opcode,
					TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
					cmd_hdr->status_block.error,
					error_str,
					cmd_hdr->err_desc);
			goto out; /* twa_drain_complete_queue will have done the unmapping */
		}
		free(param, M_DEVBUF);
		twa_release_request(tr);
		return(error);
	} else {
		/* There's a call back.  Simply submit the command. */
		tr->tr_callback = callback;
		if ((error = twa_map_request(tr))) {
			twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
						__func__, error);
			goto out;
		}
		return(0);
	}

out:
	twa_printf(sc, "set_param failed. error = 0x%x\n", error);
	if (param)
		free(param, M_DEVBUF);
	if (tr)
		twa_release_request(tr);
	return(error);
}


/*
 * Function name:	twa_init_connection
 * Description:		Send init_connection cmd to firmware
 *
 * Input:		sc		-- ptr to per ctlr structure
 *			message_credits	-- max # of requests that we might send
 *					 down simultaneously.  This will be
 *					 typically set to 256 at init-time or
 *					after a reset, and to 1 at shutdown-time
 *			set_features	-- indicates if we intend to use 64-bit
 *					sg, also indicates if we want to do a
 *					basic or an extended init_connection;
 *
 * Note: The following input/output parameters are valid, only in case of an
 *		extended init_connection:
 *
 *			current_fw_srl		-- srl of fw we are bundled
 *						with, if any; 0 otherwise
 *			current_fw_arch_id	-- arch_id of fw we are bundled
 *						with, if any; 0 otherwise
 *			current_fw_branch	-- branch # of fw we are bundled
 *						with, if any; 0 otherwise
 *			current_fw_build	-- build # of fw we are bundled
 *						with, if any; 0 otherwise
 * Output:		fw_on_ctlr_srl		-- srl of fw on ctlr
 *			fw_on_ctlr_arch_id	-- arch_id of fw on ctlr
 *			fw_on_ctlr_branch	-- branch # of fw on ctlr
 *			fw_on_ctlr_build	-- build # of fw on ctlr
 *			init_connect_result	-- result bitmap of fw response
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_init_connection(struct twa_softc *sc, u_int16_t message_credits,
			u_int32_t set_features, u_int16_t current_fw_srl,
			u_int16_t current_fw_arch_id, u_int16_t current_fw_branch,
			u_int16_t current_fw_build, u_int16_t *fw_on_ctlr_srl,
			u_int16_t *fw_on_ctlr_arch_id, u_int16_t *fw_on_ctlr_branch,
			u_int16_t *fw_on_ctlr_build, u_int32_t *init_connect_result)
{
	struct twa_request		*tr;
	struct twa_command_header	*cmd_hdr;
	struct twa_command_init_connect	*init_connect;
	int				error = 1;
    
	twa_dbg_dprint_enter(3, sc);

	/* Get a request packet. */
	if ((tr = twa_get_request(sc)) == NULL)
		goto out;
	tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
	/* Build the cmd pkt. */
	cmd_hdr = &(tr->tr_command->cmd_hdr);
	cmd_hdr->header_desc.size_header = 128;

	init_connect = &(tr->tr_command->command.cmd_pkt_7k.init_connect);
	init_connect->opcode = TWA_OP_INIT_CONNECTION;
   	init_connect->request_id = tr->tr_request_id;
	init_connect->message_credits = message_credits;
	init_connect->features = set_features;
	if (TWA_64BIT_ADDRESSES)
		init_connect->features |= TWA_64BIT_SG_ADDRESSES;
	if (set_features & TWA_EXTENDED_INIT_CONNECT) {
		/* Fill in the extra fields needed for an extended init_connect. */
		init_connect->size = 6;
		init_connect->fw_srl = current_fw_srl;
		init_connect->fw_arch_id = current_fw_arch_id;
		init_connect->fw_branch = current_fw_branch;
		init_connect->fw_build = current_fw_build;
	} else
		init_connect->size = 3;

	/* Submit the command, and wait for it to complete. */
	error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
	if (error == ETIMEDOUT)
		return(error); /* clean-up done by twa_immediate_request */
	if (error)
		goto out;
	if ((error = init_connect->status)) {
		u_int8_t *error_str =
		&(cmd_hdr->err_desc[strlen(cmd_hdr->err_desc) + 1]);

		if (error_str[0] == '\0')
			error_str = twa_find_msg_string(twa_error_table,
					cmd_hdr->status_block.error);
		twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
					init_connect->opcode,
					TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
					cmd_hdr->status_block.error,
					error_str,
					cmd_hdr->err_desc);
		goto out; /* twa_drain_complete_queue will have done the unmapping */
	}
	if (set_features & TWA_EXTENDED_INIT_CONNECT) {
		*fw_on_ctlr_srl = init_connect->fw_srl;
		*fw_on_ctlr_arch_id = init_connect->fw_arch_id;
		*fw_on_ctlr_branch = init_connect->fw_branch;
		*fw_on_ctlr_build = init_connect->fw_build;
		*init_connect_result = init_connect->result;
	}
	twa_release_request(tr);
	return(error);

out:
	twa_printf(sc, "init_connection failed. error = 0x%x\n", error);
	if (tr)
		twa_release_request(tr);
	return(error);
}



/*
 * Function name:	twa_wait_request
 * Description:		Sends down a firmware cmd, and waits for the completion,
 *			but NOT in a tight loop.
 *
 * Input:		tr	-- ptr to request pkt
 *			timeout -- max # of seconds to wait before giving up
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_wait_request(struct twa_request *tr, u_int32_t timeout)
{
	time_t	end_time;
	int	s;
	int	error;

	twa_dbg_dprint_enter(4, tr->tr_sc);

	tr->tr_flags |= TWA_CMD_SLEEP_ON_REQUEST;
	tr->tr_status = TWA_CMD_BUSY;
	if ((error = twa_map_request(tr))) {
		twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
						__func__, error);
		return(error);
	}

	s = splcam();
	end_time = time_second + timeout;
	while (tr->tr_status != TWA_CMD_COMPLETE) {
		if ((error = tr->tr_error))
			goto err;
		if ((error = tsleep(tr, PRIBIO, "twawait", timeout * hz)) == 0) {
			if ((error = tr->tr_error)) /* possible reset */
				goto err;
			error = (tr->tr_status != TWA_CMD_COMPLETE);
			break;
		}
		tr->tr_flags &= ~TWA_CMD_SLEEP_ON_REQUEST;
		if (error == EWOULDBLOCK) {
			/* Time out! */
			twa_printf(tr->tr_sc, "%s: Request %p timed out.\n",
								__func__, tr);
			/*
			 * We will reset the controller only if the request has
			 * already been submitted, so as to not lose the
			 * request packet.  If a busy request timed out, the
			 * reset will take care of freeing resources.  If a
			 * pending request timed out, we will free resources
			 * for that request, right here.  So, the caller is
			 * expected to NOT cleanup when ETIMEDOUT is returned.
			 */
			if (tr->tr_status != TWA_CMD_PENDING)
				twa_reset(tr->tr_sc);
			else {
				/* Request was never submitted.  Clean up. */
				twa_remove_pending(tr);
				twa_unmap_request(tr);
			}
			if (tr->tr_data)
				free(tr->tr_data, M_DEVBUF);
			twa_release_request(tr);
			splx(s);
			return(ETIMEDOUT);
		}
		/* 
		 * Either the request got completed, or we were woken up by a
		 * signal.  Calculate the new timeout, in case it was the latter.
		 */
		timeout = (end_time - time_second);
	}
	twa_unmap_request(tr);

err:
	splx(s);
	return(error);
}



/*
 * Function name:	twa_immediate_request
 * Description:		Sends down a firmware cmd, and waits for the completion
 *			in a tight loop.
 *
 * Input:		tr	-- ptr to request pkt
 *			timeout -- max # of seconds to wait before giving up
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_immediate_request(struct twa_request *tr, u_int32_t timeout)
{
	time_t	end_time;
	int	error = 0;

	twa_dbg_dprint_enter(4, tr->tr_sc);

	if ((error = twa_map_request(tr))) {
		twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
						__func__, error);
		return(error);
	}

	end_time = time_second + timeout;
	do {
		if ((error = tr->tr_error))
			return(error);
		twa_done(tr->tr_sc);
		if ((tr->tr_status != TWA_CMD_BUSY) &&
			(tr->tr_status != TWA_CMD_PENDING)) {
			twa_unmap_request(tr);
			return(tr->tr_status != TWA_CMD_COMPLETE);
		}
	} while (time_second <= end_time);

	/* Time out! */
	twa_printf(tr->tr_sc, "%s: Request %p timed out.\n", __func__, tr);
	/*
	 * We will reset the controller only if the request has
	 * already been submitted, so as to not lose the
	 * request packet.  If a busy request timed out, the
	 * reset will take care of freeing resources.  If a
	 * pending request timed out, we will free resources
	 * for that request, right here.  So, the caller is
	 * expected to NOT cleanup when ETIMEDOUT is returned.
	 */
	if (tr->tr_status != TWA_CMD_PENDING)
		twa_reset(tr->tr_sc);
	else {
		/* Request was never submitted.  Clean up. */
		twa_remove_pending(tr);
		twa_unmap_request(tr);
		if (tr->tr_data)
			free(tr->tr_data, M_DEVBUF);
		twa_release_request(tr);
	}
	return(ETIMEDOUT);
}



/*
 * Function name:	twa_complete_io
 * Description:		Callback on scsi requests to fw.
 *
 * Input:		tr	-- ptr to request pkt
 * Output:		None
 * Return value:	None
 */
void
twa_complete_io(struct twa_request *tr)
{
	struct twa_softc	*sc = tr->tr_sc;

	twa_dbg_dprint_enter(8, sc);

	if (tr->tr_status != TWA_CMD_COMPLETE)
		twa_panic(sc, "twa_complete_io on incomplete command");
	if (tr->tr_private) /* This is a scsi cmd.  Complete it. */
		twa_scsi_complete(tr);
	twa_release_request(tr);
}


/*
 * Function name:	twa_reset
 * Description:		Soft resets and then initializes the controller;
 *			drains any incomplete requests.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
int
twa_reset(struct twa_softc *sc)
{
	int	s;
	int	error = 0;

	twa_dbg_dprint_enter(2, sc);

	/*
	 * Disable interrupts from the controller, and mask any
	 * accidental entry into our interrupt handler.
	 */
	twa_disable_interrupts(sc);
	s = splcam();
	
	/*
	 * Complete all requests in the complete queue; error back all requests
	 * in the busy queue.  Any internal requests will be simply freed.
	 * Re-submit any requests in the pending queue.
	 */
	twa_drain_complete_queue(sc);
	twa_drain_busy_queue(sc);

	/* Soft reset the controller. */
	if ((error = twa_soft_reset(sc))) {
		twa_printf (sc, "Controller reset failed.\n");
		goto out;
	}

	/* Re-establish logical connection with the controller. */
	if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
					0, 0, 0, 0, 0,
					NULL, NULL, NULL, NULL, NULL))) {
		twa_printf(sc, "Can't initialize connection after reset.\n");
		goto out;
	}

	twa_printf(sc, "Controller reset done!\n");

out:
	splx(s);
	/*
	 * Enable interrupts, and also clear attention and response interrupts.
	 */
	twa_enable_interrupts(sc);
	return(error);
}



/*
 * Function name:	twa_soft_reset
 * Description:		Does the actual soft reset.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_soft_reset(struct twa_softc *sc)
{
	u_int32_t	status_reg;

	twa_dbg_dprint_enter(1, sc);

	twa_printf(sc, "Resetting controller...\n");
	TWA_SOFT_RESET(sc);

	if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY |
				TWA_STATUS_ATTENTION_INTERRUPT, 30)) {
		twa_printf(sc, "Micro-ctlr not ready/No attn intr after reset.\n");
		return(1);
	}
	TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
	if (twa_drain_response_queue(sc)) {
		twa_printf(sc, "Can't drain response queue.\n");
		return(1);
	}
	if (twa_drain_aen_queue(sc)) {
		twa_printf(sc, "Can't drain AEN queue.\n");
		return(1);
	}
	if (twa_find_aen(sc, TWA_AEN_SOFT_RESET)) {
		twa_printf(sc, "Reset not reported by controller.\n");
		return(1);
	}
	status_reg = TWA_READ_STATUS_REGISTER(sc);
	if (TWA_STATUS_ERRORS(status_reg) ||
				twa_check_ctlr_state(sc, status_reg)) {
		twa_printf(sc, "Controller errors detected.\n");
		return(1);
	}
	return(0);
}



/*
 * Function name:	twa_submit_io
 * Description:		Wrapper to twa_start.
 *
 * Input:		tr	-- ptr to request pkt
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
int
twa_submit_io(struct twa_request *tr)
{
	int	error;

	if ((error = twa_start(tr))) {
		if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL) {
			if (error == EBUSY)
				/*
				 * Cmd queue is full.  Freeze the simq to
				 * maintain ccb ordering.  The next ccb that
				 * gets completed will unfreeze the simq.
				 */
				twa_disallow_new_requests(tr->tr_sc);
			else
				/* It's a controller error. */
				twa_printf(tr->tr_sc, "SCSI cmd = 0x%x: ERROR: (0x%02X: 0x%04X)\n",
					tr->tr_command->command.cmd_pkt_9k.cdb[0],
					TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
					error);
			
			tr->tr_error = error;
			twa_scsi_complete(tr);
		} else {
			if (error == EBUSY)
				error = 0; /* the request will be in the pending queue */
			else {
				twa_printf(tr->tr_sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X)\n",
						(tr->tr_cmd_pkt_type == TWA_CMD_PKT_TYPE_9K) ?
						(tr->tr_command->command.cmd_pkt_9k.command.opcode) :
						(tr->tr_command->command.cmd_pkt_7k.generic.opcode),
						TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
						tr->tr_error);
				tr->tr_error = error;
			}
		}
	}
	return(error);
}



/*
 * Function name:	twa_start
 * Description:		Posts a cmd to firmware.
 *
 * Input:		tr	-- ptr to request pkt
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
int
twa_start(struct twa_request *tr)
{
	struct twa_softc	*sc = tr->tr_sc;
	u_int32_t		status_reg;
	int			s;
	int			error;

	twa_dbg_dprint_enter(10, sc);

	s = splcam();
	/* Check to see if we can post a command. */
	status_reg = TWA_READ_STATUS_REGISTER(sc);
	if ((error = twa_check_ctlr_state(sc, status_reg)))
		goto out;

	if (status_reg & TWA_STATUS_COMMAND_QUEUE_FULL) {
		if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ||
			(tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_IOCTL)) {
			if (tr->tr_status != TWA_CMD_PENDING) {
				twa_dbg_dprint(2, sc, "pending internal/ioctl request");
				tr->tr_status = TWA_CMD_PENDING;
				twa_enqueue_pending(tr);
			}
			TWA_WRITE_CONTROL_REGISTER(sc,
					TWA_CONTROL_UNMASK_COMMAND_INTERRUPT);
		}
		error = EBUSY;
	} else {
		/* Mark the request as currently being processed. */
		tr->tr_status = TWA_CMD_BUSY;
		/* Move the request into the busy queue. */
		twa_enqueue_busy(tr);
		/* Cmd queue is not full.  Post the command. */
		TWA_WRITE_COMMAND_QUEUE(sc,
			tr->tr_cmd_phys + sizeof(struct twa_command_header));
	}

out:
	splx(s);
	return(error);
}



/*
 * Function name:	twa_done
 * Description:		Looks for cmd completions from fw; queues cmds completed
 *			by fw into complete queue.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	0	-- no ctlr error
 *			non-zero-- ctlr error
 */
static int
twa_done(struct twa_softc *sc)
{
	union twa_response_queue	rq;
	struct twa_request		*tr;
	int				s;
	int				error = 0;
	u_int32_t			status_reg;
    
	twa_dbg_dprint_enter(10, sc);

	s = splcam();
	for (;;) {
		status_reg = TWA_READ_STATUS_REGISTER(sc);
		if ((error = twa_check_ctlr_state(sc, status_reg)))
			break;
		if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
			break;
		/* Response queue is not empty. */
		rq = TWA_READ_RESPONSE_QUEUE(sc);
		tr = sc->twa_lookup[rq.u.response_id];	/* lookup the request */
		if (tr->tr_status != TWA_CMD_BUSY)
			twa_printf(sc, "ERROR: Unposted command completed!! req = %p; status = %d\n",
					tr, tr->tr_status);
		tr->tr_status = TWA_CMD_COMPLETE;
		/* Enqueue request in the complete queue. */
		twa_remove_busy(tr);
		twa_enqueue_complete(tr);
	}
	splx(s);

	/* Complete this, and other requests in the complete queue. */
	twa_drain_complete_queue(sc);
	return(error);
}



/*
 * Function name:	twa_drain_pending_queue
 * Description:		Kick starts any requests in the pending queue.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	0	-- all pending requests drained
 *			non-zero-- otherwise
 */
static int
twa_drain_pending_queue(struct twa_softc *sc)
{
	struct twa_request	*tr;
	int			error = 0;
    
	twa_dbg_dprint_enter(10, sc);
	
	/*
	 * Pull requests off the pending queue, and submit them.
	 */
	while ((tr = twa_dequeue_pending(sc)) != NULL) {
		if ((error = twa_start(tr))) {
			if (error == EBUSY) {
				twa_dbg_dprint(2, sc, "Requeueing pending request");
				tr->tr_status = TWA_CMD_PENDING;
				twa_requeue_pending(tr);/* queue at the head */
				break;
			} else {
				twa_printf(sc, "%s: twa_start returned 0x%x\n",
							__func__, error);
				tr->tr_error = error;
				if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST)
					wakeup_one(tr);/* let the caller know it failed */
				error = 0;
			}
		}
	}
	return(error);
}



/*
 * Function name:	twa_drain_complete_queue
 * Description:		Does unmapping for each request completed by fw,
 *			and lets the request originators know of the completion.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	None
 */
static void
twa_drain_complete_queue(struct twa_softc *sc)
{
	struct twa_request	*tr;
    
	twa_dbg_dprint_enter(10, sc);

	/*
	 * Pull commands off the completed list, dispatch them appropriately.
	 */
	while ((tr = twa_dequeue_complete(sc)) != NULL) {
		/* Unmap the command packet, and any associated data buffer. */
		twa_unmap_request(tr);

		/* Call the callback, if there's one. */
		if (tr->tr_callback)
			tr->tr_callback(tr);
		else
			if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST) {
				/* Wake up the sleeping command originator. */
				twa_dbg_dprint(7, sc, "Waking up originator of request %p", tr);
				wakeup_one(tr);
			}
	}
}



/*
 * Function name:	twa_wait_status
 * Description:		Wait for a given status to show up in the fw status register.
 *
 * Input:		sc	-- ptr to per ctlr structure
 *			status	-- status to look for
 *			timeout -- max # of seconds to wait before giving up
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_wait_status(struct twa_softc *sc, u_int32_t status, u_int32_t timeout)
{
	time_t		end_time;
	u_int32_t	status_reg;

	twa_dbg_dprint_enter(4, sc);

	end_time = time_second + timeout;
	do {
		status_reg = TWA_READ_STATUS_REGISTER(sc);
		if ((status_reg & status) == status)/* got the required bit(s)? */
			return(0);
		DELAY(1000);
	} while (time_second <= end_time);

	return(1);
}



/*
 * Function name:	twa_drain_response_queue
 * Description:		Drain the response queue.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_drain_response_queue(struct twa_softc *sc)
{
	union twa_response_queue	rq;
	u_int32_t			status_reg;

	twa_dbg_dprint_enter(4, sc);

	for (;;) {
		status_reg = TWA_READ_STATUS_REGISTER(sc);
		if (twa_check_ctlr_state(sc, status_reg))
			return(1);
		if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
			return(0); /* no more response queue entries */
		rq = TWA_READ_RESPONSE_QUEUE(sc);
	}
}



/*
 * Function name:	twa_host_intr
 * Description:		This function gets called if we triggered an interrupt.
 *			We don't use it as of now.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	None
 */
static void
twa_host_intr(struct twa_softc *sc)
{
	twa_dbg_dprint_enter(6, sc);

	TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_HOST_INTERRUPT);
}



/*
 * Function name:	twa_attention_intr
 * Description:		This function gets called if the fw posted an AEN
 *			(Asynchronous Event Notification).  It fetches
 *			all the AEN's that the fw might have posted.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	None
 */
static void
twa_attention_intr(struct twa_softc *sc)
{
	int	error;

	twa_dbg_dprint_enter(6, sc);

	TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
	if ((error = twa_fetch_aen(sc)))
		twa_printf(sc, "Fetch AEN failed. error = 0x%x\n", error);
}



/*
 * Function name:	twa_command_intr
 * Description:		This function gets called if we hit a queue full
 *			condition earlier, and the fw is now ready for
 *			new cmds.  Submits any pending requests.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	None
 */
static void
twa_command_intr(struct twa_softc *sc)
{
	twa_dbg_dprint_enter(6, sc);

	TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_MASK_COMMAND_INTERRUPT);
	/*
	 * Start any requests that might be in the pending queue.
	 * If all requests could not be started because of a queue_full
	 * condition, twa_start will have unmasked the command interrupt.
	 */
	twa_drain_pending_queue(sc);
}



/*
 * Function name:	twa_fetch_aen
 * Description:		Send down a Request Sense cmd to fw to fetch an AEN.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_fetch_aen(struct twa_softc *sc)
{
	struct twa_request	*tr;
	int			error = 0;

	twa_dbg_dprint_enter(4, sc);

	if ((tr = twa_get_request(sc)) == NULL)
		return(EIO);
	tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
	tr->tr_callback = twa_aen_callback;
	if ((error = twa_send_scsi_cmd(tr, 0x03 /* REQUEST_SENSE */))) {
		if (tr->tr_data)
			free(tr->tr_data, M_DEVBUF);
		twa_release_request(tr);
	}
	return(error);
}



/*
 * Function name:	twa_aen_callback
 * Description:		Callback for requests to fetch AEN's.
 *
 * Input:		tr	-- ptr to completed request pkt
 * Output:		None
 * Return value:	None
 */
static void
twa_aen_callback(struct twa_request *tr)
{
	struct twa_softc		*sc = tr->tr_sc;
	struct twa_command_header	*cmd_hdr;
	struct twa_command_9k		*cmd = &(tr->tr_command->command.cmd_pkt_9k);
	u_int8_t			*error_str;
	int				fetch_more_aens = 0;
	int				error;
	int				i;

	twa_dbg_dprint_enter(4, sc);

	twa_dbg_dprint(4, sc, "req_id = 0x%x, status = 0x%x",
				cmd->request_id,
				cmd->status);

	if (tr->tr_error)
		goto out;

	if (! cmd->status) {
		cmd_hdr = (struct twa_command_header *)(tr->tr_data);
		if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) &&
			(cmd->cdb[0] == 0x3 /* REQUEST_SENSE */))
			if (twa_enqueue_aen(sc, cmd_hdr) != TWA_AEN_QUEUE_EMPTY)
				fetch_more_aens = 1;
	} else {
		cmd_hdr = &(tr->tr_command->cmd_hdr);
		error_str = &(cmd_hdr->err_desc[strlen(cmd_hdr->err_desc) + 1]);

		if (error_str[0] == '\0')
			error_str = twa_find_msg_string(twa_error_table,
						cmd_hdr->status_block.error);
		twa_printf(sc, "%s: cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
				__func__, cmd->command.opcode,
				TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
				cmd_hdr->status_block.error,
				error_str,
				cmd_hdr->err_desc);
		twa_dbg_print(2, "sense info: ");
		for (i = 0; i < 18; i++)
			twa_dbg_print(2, "%x\t", tr->tr_command->cmd_hdr.sense_data[i]);
		twa_dbg_print(2, ""); /* print new line */
		for (i = 0; i < 128; i++)
			twa_dbg_print(7, "%x\t", ((int8_t *)(tr->tr_data))[i]);
	}

out:
	if (tr->tr_data)
		free(tr->tr_data, M_DEVBUF);
	twa_release_request(tr);
	if (fetch_more_aens)
		if ((error = twa_fetch_aen(sc)))
			twa_printf(sc, "%s: Fetch AEN failed. error = 0x%x\n",
					__func__, error);
}



/*
 * Function name:	twa_drain_aen_queue
 * Description:		Fetches all un-retrieved AEN's posted by fw.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_drain_aen_queue(struct twa_softc *sc)
{
	struct twa_request		*tr;
	struct twa_command_header	*cmd_hdr;
	time_t				end_time;
	int				error = 0;

	for (;;) {
		if ((tr = twa_get_request(sc)) == NULL) {
			error = EIO;
			break;
		}
		tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
		tr->tr_callback = NULL;
		if ((error = twa_send_scsi_cmd(tr, 0x03 /* REQUEST_SENSE */))) {
			twa_dbg_dprint(1, sc, "Cannot send command to fetch aen");
			break;
		}

		end_time = time_second + TWA_REQUEST_TIMEOUT_PERIOD;
		do {
			twa_done(tr->tr_sc);
			if (tr->tr_status != TWA_CMD_BUSY)
				break;
		} while (time_second <= end_time);

		if (tr->tr_status != TWA_CMD_COMPLETE) {
			error = ETIMEDOUT;
			break;
		}

		if ((error = tr->tr_command->command.cmd_pkt_9k.status))
			break;

		cmd_hdr = (struct twa_command_header *)(tr->tr_data);
		if (twa_enqueue_aen(sc, cmd_hdr) == TWA_AEN_QUEUE_EMPTY)
			break;

		free(tr->tr_data, M_DEVBUF);
		twa_release_request(tr);
	}

	if (tr) {
		if (tr->tr_data)
			free(tr->tr_data, M_DEVBUF);
		twa_release_request(tr);
	}
	return(error);
}



/*
 * Function name:	twa_enqueue_aen
 * Description:		Queues AEN's to be supplied to user-space tools on request.
 *
 * Input:		sc	-- ptr to per ctlr structure
 *			cmd_hdr	-- ptr to hdr of fw cmd pkt, from where the AEN
 *				   details can be retrieved.
 * Output:		None
 * Return value:	AEN code
 */
static unsigned short
twa_enqueue_aen(struct twa_softc *sc, struct twa_command_header *cmd_hdr)
{
	struct twa_event_packet	*event;
	unsigned short		aen_code;
	unsigned long		local_time;
	unsigned long		sync_time;
	u_int8_t		*aen_str;
	int			s;

	twa_dbg_dprint_enter(4, sc);
	s = splcam();
	aen_code = cmd_hdr->status_block.error;

	switch (aen_code) {
	case TWA_AEN_SYNC_TIME_WITH_HOST:
		twa_dbg_dprint(4, sc, "Received AEN_SYNC_TIME");
		/* Calculate time (in seconds) since last Sunday 12.00 AM. */
		local_time = time_second - (tz_minuteswest * 60) -
					(wall_cmos_clock ? adjkerntz : 0);
		sync_time = (local_time - (3 * 86400)) % 604800;
		if (twa_set_param(sc, TWA_PARAM_TIME_TABLE,
					TWA_PARAM_TIME_SchedulerTime, 4,
					&sync_time, twa_aen_callback))
			twa_printf(sc, "Unable to sync time with ctlr!\n");
		break;

	case TWA_AEN_QUEUE_EMPTY:
		twa_dbg_dprint(4, sc, "AEN queue empty");
		break;

	default:
		/* Queue the event. */
		event = sc->twa_aen_queue[sc->twa_aen_head];
		if (event->retrieved == TWA_AEN_NOT_RETRIEVED)
			sc->twa_aen_queue_overflow = TRUE;
		event->severity = cmd_hdr->status_block.substatus_block.severity;
		local_time = time_second - (tz_minuteswest * 60) -
					(wall_cmos_clock ? adjkerntz : 0);
		event->time_stamp_sec = local_time;
		event->aen_code = aen_code;
		event->retrieved = TWA_AEN_NOT_RETRIEVED;
		event->sequence_id = ++(sc->twa_current_sequence_id);

		aen_str = &(cmd_hdr->err_desc[strlen(cmd_hdr->err_desc) + 1]);
		if (aen_str[0] == '\0')
			aen_str = twa_find_msg_string(twa_aen_table, aen_code);


		event->parameter_len = strlen(cmd_hdr->err_desc) +
					strlen(aen_str) + 2;
		bcopy(cmd_hdr->err_desc, event->parameter_data,
					event->parameter_len);

		twa_dbg_dprint(4, sc, "event = %x %x %x %x %x %x %x\n %s %s",
				event->sequence_id,
				event->time_stamp_sec,
				event->aen_code,
				event->severity,
				event->retrieved,
				event->repeat_count,
				event->parameter_len,
				&(event->parameter_data[strlen(cmd_hdr->err_desc) + 1]),
				event->parameter_data);

		twa_dbg_dprint(4, sc, "cmd_hdr = %x %lx %x %x %x %x %zx\n %s %s",
				sc->twa_current_sequence_id,
				local_time,
				cmd_hdr->status_block.error,
				cmd_hdr->status_block.substatus_block.severity,
				TWA_AEN_NOT_RETRIEVED,
				0,
				strlen(cmd_hdr->err_desc),
				&(cmd_hdr->err_desc[strlen(cmd_hdr->err_desc) + 1]),
				cmd_hdr->err_desc);

		/* Print the event. */
		if (event->severity < TWA_AEN_SEVERITY_DEBUG)
			twa_printf(sc,  "%s: (0x%02X: 0x%04X): %s: %s\n",
					twa_aen_severity_table[event->severity],
					TWA_MESSAGE_SOURCE_CONTROLLER_EVENT,
					aen_code,
					aen_str,
					event->parameter_data);

		if ((sc->twa_aen_head + 1) == TWA_Q_LENGTH)
			sc->twa_aen_queue_wrapped = TRUE;
		sc->twa_aen_head = (sc->twa_aen_head + 1) % TWA_Q_LENGTH;
		break;
	} /* switch */
	splx(s);
	return(aen_code);
}



/*
 * Function name:	twa_find_aen
 * Description:		Reports whether a given AEN ever occurred.
 *
 * Input:		sc	-- ptr to per ctlr structure
 *			aen_code-- AEN to look for
 * Output:		None
 * Return value:	0	-- success
 *			non-zero-- failure
 */
static int
twa_find_aen(struct twa_softc *sc, u_int16_t aen_code)
{
	u_int32_t	last_index;
	int		s;
	int		i;

	s = splcam();

	if (sc->twa_aen_queue_wrapped)
		last_index = sc->twa_aen_head;
	else
		last_index = 0;

	i = sc->twa_aen_head;
	do {
		i = (i + TWA_Q_LENGTH - 1) % TWA_Q_LENGTH;
		if ((sc->twa_aen_queue[i])->aen_code == aen_code) {
			splx(s);
			return(0);
		}
	} while (i != last_index);

	splx(s);
	return(1);
}



/*
 * Function name:	twa_find_msg_string
 * Description:		Looks up a given table, and returns the message string
 *			corresponding to a given code (error code or AEN code).
 *
 * Input:		sc	-- ptr to per ctlr structure
 *			code	-- code, the message string corresponding to
 *				   which is to be returned.
 * Output:		None
 * Return value:	ptr to corresponding msg string	-- success
 *			NULL				-- failure
 */
char *
twa_find_msg_string(struct twa_message *table, u_int16_t code)
{
	int	i;

	for (i = 0; table[i].message != NULL; i++)
		if (table[i].code == code)
			return(table[i].message);

	return(table[i].message);
}



/*
 * Function name:	twa_get_request
 * Description:		Gets a request pkt from the free queue.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	ptr to request pkt	-- success
 *			NULL			-- failure
 */
struct twa_request *
twa_get_request(struct twa_softc *sc)
{
	struct twa_request	*tr;

	twa_dbg_dprint_enter(4, sc);

	/* Get a free request packet. */
	tr = twa_dequeue_free(sc);

	/* Initialize some fields to their defaults. */
	if (tr) {
		tr->tr_data = NULL;
		tr->tr_real_data = NULL;
		tr->tr_length = 0;
		tr->tr_real_length = 0;
		tr->tr_status = TWA_CMD_SETUP;/* command is in setup phase */
		tr->tr_flags = 0;
		tr->tr_error = 0;
		tr->tr_private = NULL;
		tr->tr_callback = NULL;
		tr->tr_cmd_pkt_type = 0;

		/*
		 * Look at the status field in the command packet to see how
		 * it completed the last time it was used, and zero out only
		 * the portions that might have changed.  Note that we don't
		 * care to zero out the sglist.
		 */
		if (tr->tr_command->command.cmd_pkt_9k.status)
			bzero(tr->tr_command,
				sizeof(struct twa_command_header) + 28 /* max bytes before sglist */);
		else
			bzero(&(tr->tr_command->command), 28 /* max bytes before sglist */);
	}
	return(tr);
}



/*
 * Function name:	twa_release_request
 * Description:		Puts a request pkt into the free queue.
 *
 * Input:		tr	-- ptr to request pkt to be freed
 * Output:		None
 * Return value:	None
 */
void
twa_release_request(struct twa_request *tr)
{
	twa_dbg_dprint_enter(4, tr->tr_sc);

	twa_enqueue_free(tr);
}



/*
 * Function name:	twa_describe_controller
 * Description:		Describes the controller, in terms of its fw version,
 *			BIOS version etc.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	None
 */
void
twa_describe_controller(struct twa_softc *sc)
{
	struct twa_param_9k	*p[6];
	u_int8_t		num_ports = 0;

	twa_dbg_dprint_enter(2, sc);

	/* Get the port count. */
	p[0] = twa_get_param(sc, TWA_PARAM_CONTROLLER_TABLE,
				TWA_PARAM_CONTROLLER_PORT_COUNT, 1, NULL);
	if (p[0]) {
		num_ports = *(u_int8_t *)(p[0]->data);
		free(p[0], M_DEVBUF);
	}

	/* Get the firmware and BIOS versions. */
	p[0] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
				TWA_PARAM_VERSION_FW, 16, NULL);
	p[1] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
				TWA_PARAM_VERSION_BIOS, 16, NULL);

	twa_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n",
			num_ports, p[0]?(p[0]->data):NULL, p[1]?(p[1]->data):NULL);
	if (bootverbose) {
		/* Get more versions. */
		p[2] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
					TWA_PARAM_VERSION_MONITOR, 16, NULL);
		p[3] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
					TWA_PARAM_VERSION_PCBA, 8, NULL);
		p[4] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
					TWA_PARAM_VERSION_ATA, 8, NULL);
		p[5] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
					TWA_PARAM_VERSION_PCI, 8, NULL);

		twa_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
				p[2]?(p[2]->data):NULL, p[3]?(p[3]->data):NULL,
				p[4]?(p[4]->data):NULL, p[5]?(p[5]->data):NULL);

		if (p[2])
			free(p[2], M_DEVBUF);
		if (p[3])
			free(p[3], M_DEVBUF);
		if (p[4])
			free(p[4], M_DEVBUF);
		if (p[5])
			free(p[5], M_DEVBUF);
	}
	if (p[0])
		free(p[0], M_DEVBUF);
	if (p[1])
		free(p[1], M_DEVBUF);
}



/*
 * Function name:	twa_check_ctlr_state
 * Description:		Makes sure that the fw status register reports a
 *			proper status.
 *
 * Input:		sc		-- ptr to per ctlr structure
 *			status_reg	-- value in the status register
 * Output:		None
 * Return value:	0	-- no errors
 *			non-zero-- errors
 */
static int
twa_check_ctlr_state(struct twa_softc *sc, u_int32_t status_reg)
{
	int		result = 0;
	static time_t	last_warning[2] = {0, 0};

	/* Check if the 'micro-controller ready' bit is not set. */
	if ((status_reg & TWA_STATUS_EXPECTED_BITS) !=
				TWA_STATUS_EXPECTED_BITS) {
		if (time_second > (last_warning[0] + 5)) {
			twa_printf(sc, "Missing expected status bit(s) %b\n",
					~status_reg & TWA_STATUS_EXPECTED_BITS,
					TWA_STATUS_BITS_DESCRIPTION);
			last_warning[0] = time_second;
		}
		result = 1;
	}

	/* Check if any error bits are set. */
	if ((status_reg & TWA_STATUS_UNEXPECTED_BITS) != 0) {
		if (time_second > (last_warning[1] + 5)) {
			twa_printf(sc, "Unexpected status bit(s) %b\n",
					status_reg & TWA_STATUS_UNEXPECTED_BITS,
					TWA_STATUS_BITS_DESCRIPTION);
			last_warning[1] = time_second;
		}
		if (status_reg & TWA_STATUS_PCI_PARITY_ERROR_INTERRUPT) {
			twa_printf(sc, "PCI parity error: clearing... Re-seat/move/replace card.\n");
			TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PARITY_ERROR);
			twa_write_pci_config(sc, TWA_PCI_CONFIG_CLEAR_PARITY_ERROR, 2);
		}
		if (status_reg & TWA_STATUS_PCI_ABORT_INTERRUPT) {
			twa_printf(sc, "PCI abort: clearing...\n");
			TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PCI_ABORT);
			twa_write_pci_config(sc, TWA_PCI_CONFIG_CLEAR_PCI_ABORT, 2);
		}
		if (status_reg & TWA_STATUS_QUEUE_ERROR_INTERRUPT) {
			twa_printf(sc, "Controller queue error: clearing...\n");
			TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PCI_ABORT);
		}
		if (status_reg & TWA_STATUS_SBUF_WRITE_ERROR) {
			twa_printf(sc, "SBUF write error: clearing...\n");
			TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_SBUF_WRITE_ERROR);
		}
		if (status_reg & TWA_STATUS_MICROCONTROLLER_ERROR) {
			twa_printf(sc, "Micro-controller error!\n");
			result = 1;
		}
	}
	return(result);
}	



/*
 * Function name:	twa_print_controller
 * Description:		Prints the current status of the controller.
 *
 * Input:		sc	-- ptr to per ctlr structure
 * Output:		None
 * Return value:	None
 */
void
twa_print_controller(struct twa_softc *sc)
{
	u_int32_t	status_reg;

	/* Print current controller details. */
	status_reg = TWA_READ_STATUS_REGISTER(sc);
	twa_printf(sc, "status   %b\n", status_reg, TWA_STATUS_BITS_DESCRIPTION);
#ifdef TWA_DEBUG
	twa_printf(sc, "q type    current  max\n");
	twa_printf(sc, "free      %04d     %04d\n",
		sc->twa_qstats[TWAQ_FREE].q_length, sc->twa_qstats[TWAQ_FREE].q_max);
	twa_printf(sc, "busy      %04d     %04d\n",
		sc->twa_qstats[TWAQ_BUSY].q_length, sc->twa_qstats[TWAQ_BUSY].q_max);
	twa_printf(sc, "pending   %04d     %04d\n",
		sc->twa_qstats[TWAQ_PENDING].q_length, sc->twa_qstats[TWAQ_PENDING].q_max);
	twa_printf(sc, "complete  %04d     %04d\n",
		sc->twa_qstats[TWAQ_COMPLETE].q_length, sc->twa_qstats[TWAQ_COMPLETE].q_max);
#endif /* TWA_DEBUG */
	twa_printf(sc, "AEN queue head %d  tail %d\n",
			sc->twa_aen_head, sc->twa_aen_tail);
}	



/*
 * Function name:	twa_panic
 * Description:		Called when something is seriously wrong with the ctlr.
 *			Hits the debugger if the debugger is turned on, else
 *			resets the ctlr.
 *
 * Input:		sc	-- ptr to per ctlr structure
 *			reason	-- string describing what went wrong
 * Output:		None
 * Return value:	None
 */
static void
twa_panic(struct twa_softc *sc, int8_t *reason)
{
	twa_print_controller(sc);
#ifdef TWA_DEBUG
	panic(reason);
#else
	twa_printf(sc, "twa_panic: RESETTING CONTROLLER...\n");
	twa_reset(sc);
#endif
}

OpenPOWER on IntegriCloud