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
|
/*-
* Copyright (c) 2000 Michael Smith
* Copyright (c) 2003 Paul Saab
* Copyright (c) 2003 Vinod Kashyap
* 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$
*/
/*
* Driver for the 3ware Escalade family of IDE RAID controllers.
*/
#include <dev/twe/twe_compat.h>
#include <dev/twe/twereg.h>
#include <dev/twe/tweio.h>
#include <dev/twe/twevar.h>
#define TWE_DEFINE_TABLES
#include <dev/twe/twe_tables.h>
/*
* Command submission.
*/
static int twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result);
static int twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result);
static int twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result);
static void *twe_get_param(struct twe_softc *sc, int table_id, int parameter_id, size_t size,
void (* func)(struct twe_request *tr));
#ifdef TWE_SHUTDOWN_NOTIFICATION
static int twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value);
#endif
#if 0
static int twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value);
static int twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value);
#endif
static int twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size,
void *data);
static int twe_init_connection(struct twe_softc *sc, int mode);
static int twe_wait_request(struct twe_request *tr);
static int twe_immediate_request(struct twe_request *tr, int usetmp);
static void twe_completeio(struct twe_request *tr);
static void twe_reset(struct twe_softc *sc);
static int twe_add_unit(struct twe_softc *sc, int unit);
static int twe_del_unit(struct twe_softc *sc, int unit);
/*
* Command I/O to controller.
*/
static void twe_done(struct twe_softc *sc);
static void twe_complete(struct twe_softc *sc);
static int twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout);
static int twe_drain_response_queue(struct twe_softc *sc);
static int twe_check_bits(struct twe_softc *sc, u_int32_t status_reg);
static int twe_soft_reset(struct twe_softc *sc);
/*
* Interrupt handling.
*/
static void twe_host_intr(struct twe_softc *sc);
static void twe_attention_intr(struct twe_softc *sc);
static void twe_command_intr(struct twe_softc *sc);
/*
* Asynchronous event handling.
*/
static int twe_fetch_aen(struct twe_softc *sc);
static void twe_handle_aen(struct twe_request *tr);
static void twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen);
static u_int16_t twe_dequeue_aen(struct twe_softc *sc);
static int twe_drain_aen_queue(struct twe_softc *sc);
static int twe_find_aen(struct twe_softc *sc, u_int16_t aen);
/*
* Command buffer management.
*/
static int twe_get_request(struct twe_softc *sc, struct twe_request **tr);
static void twe_release_request(struct twe_request *tr);
/*
* Debugging.
*/
static char *twe_format_aen(struct twe_softc *sc, u_int16_t aen);
static int twe_report_request(struct twe_request *tr);
static void twe_panic(struct twe_softc *sc, char *reason);
/********************************************************************************
********************************************************************************
Public Interfaces
********************************************************************************
********************************************************************************/
/********************************************************************************
* Initialise the controller, set up driver data structures.
*/
int
twe_setup(struct twe_softc *sc)
{
struct twe_request *tr;
TWE_Command *cmd;
u_int32_t status_reg;
int i;
debug_called(4);
/*
* Initialise request queues.
*/
twe_initq_free(sc);
twe_initq_bio(sc);
twe_initq_ready(sc);
twe_initq_busy(sc);
twe_initq_complete(sc);
sc->twe_wait_aen = -1;
/*
* Allocate request structures up front.
*/
for (i = 0; i < TWE_Q_LENGTH; i++) {
if ((tr = twe_allocate_request(sc, i)) == NULL)
return(ENOMEM);
/*
* Set global defaults that won't change.
*/
cmd = TWE_FIND_COMMAND(tr);
cmd->generic.host_id = sc->twe_host_id; /* controller-assigned host ID */
cmd->generic.request_id = i; /* our index number */
sc->twe_lookup[i] = tr;
/*
* Put command onto the freelist.
*/
twe_release_request(tr);
}
/*
* Check status register for errors, clear them.
*/
status_reg = TWE_STATUS(sc);
twe_check_bits(sc, status_reg);
/*
* Wait for the controller to come ready.
*/
if (twe_wait_status(sc, TWE_STATUS_MICROCONTROLLER_READY, 60)) {
twe_printf(sc, "microcontroller not ready\n");
return(ENXIO);
}
/*
* Disable interrupts from the card.
*/
twe_disable_interrupts(sc);
/*
* Soft reset the controller, look for the AEN acknowledging the reset,
* check for errors, drain the response queue.
*/
for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
if (i > 0)
twe_printf(sc, "reset %d failed, trying again\n", i);
if (!twe_soft_reset(sc))
break; /* reset process complete */
}
/* did we give up? */
if (i >= TWE_MAX_RESET_TRIES) {
twe_printf(sc, "can't initialise controller, giving up\n");
return(ENXIO);
}
return(0);
}
static int
twe_add_unit(struct twe_softc *sc, int unit)
{
struct twe_drive *dr;
int table, error = 0;
u_int16_t dsize;
TWE_Param *drives = NULL, *param = NULL;
TWE_Unit_Descriptor *ud;
if (unit < 0 || unit > TWE_MAX_UNITS)
return (EINVAL);
/*
* The controller is in a safe state, so try to find drives attached to it.
*/
if ((drives = twe_get_param(sc, TWE_PARAM_UNITSUMMARY, TWE_PARAM_UNITSUMMARY_Status,
TWE_MAX_UNITS, NULL)) == NULL) {
twe_printf(sc, "can't detect attached units\n");
return (EIO);
}
dr = &sc->twe_drive[unit];
/* check that the drive is online */
if (!(drives->data[unit] & TWE_PARAM_UNITSTATUS_Online)) {
error = ENXIO;
goto out;
}
table = TWE_PARAM_UNITINFO + unit;
if (twe_get_param_4(sc, table, TWE_PARAM_UNITINFO_Capacity, &dr->td_size)) {
twe_printf(sc, "error fetching capacity for unit %d\n", unit);
error = EIO;
goto out;
}
if (twe_get_param_1(sc, table, TWE_PARAM_UNITINFO_Status, &dr->td_state)) {
twe_printf(sc, "error fetching state for unit %d\n", unit);
error = EIO;
goto out;
}
if (twe_get_param_2(sc, table, TWE_PARAM_UNITINFO_DescriptorSize, &dsize)) {
twe_printf(sc, "error fetching descriptor size for unit %d\n", unit);
error = EIO;
goto out;
}
if ((param = twe_get_param(sc, table, TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL)) == NULL) {
twe_printf(sc, "error fetching descriptor for unit %d\n", unit);
error = EIO;
goto out;
}
ud = (TWE_Unit_Descriptor *)param->data;
dr->td_type = ud->configuration;
/* build synthetic geometry as per controller internal rules */
if (dr->td_size > 0x200000) {
dr->td_heads = 255;
dr->td_sectors = 63;
} else {
dr->td_heads = 64;
dr->td_sectors = 32;
}
dr->td_cylinders = dr->td_size / (dr->td_heads * dr->td_sectors);
dr->td_twe_unit = unit;
error = twe_attach_drive(sc, dr);
out:
if (param != NULL)
free(param, M_DEVBUF);
if (drives != NULL)
free(drives, M_DEVBUF);
return (error);
}
static int
twe_del_unit(struct twe_softc *sc, int unit)
{
int error;
if (unit < 0 || unit >= TWE_MAX_UNITS)
return (ENXIO);
if (sc->twe_drive[unit].td_disk == NULL)
return (ENXIO);
error = twe_detach_drive(sc, unit);
return (error);
}
/********************************************************************************
* Locate disk devices and attach children to them.
*/
void
twe_init(struct twe_softc *sc)
{
int i;
/*
* Scan for drives
*/
for (i = 0; i < TWE_MAX_UNITS; i++)
twe_add_unit(sc, i);
/*
* Initialise connection with controller.
*/
twe_init_connection(sc, TWE_INIT_MESSAGE_CREDITS);
#ifdef TWE_SHUTDOWN_NOTIFICATION
/*
* Tell the controller we support shutdown notification.
*/
twe_set_param_1(sc, TWE_PARAM_FEATURES, TWE_PARAM_FEATURES_DriverShutdown, 1);
#endif
/*
* Mark controller up and ready to run.
*/
sc->twe_state &= ~TWE_STATE_SHUTDOWN;
/*
* Finally enable interrupts.
*/
twe_enable_interrupts(sc);
}
/********************************************************************************
* Stop the controller
*/
void
twe_deinit(struct twe_softc *sc)
{
/*
* Mark the controller as shutting down, and disable any further interrupts.
*/
sc->twe_state |= TWE_STATE_SHUTDOWN;
twe_disable_interrupts(sc);
#ifdef TWE_SHUTDOWN_NOTIFICATION
/*
* Disconnect from the controller
*/
twe_init_connection(sc, TWE_SHUTDOWN_MESSAGE_CREDITS);
#endif
}
/*******************************************************************************
* Take an interrupt, or be poked by other code to look for interrupt-worthy
* status.
*/
void
twe_intr(struct twe_softc *sc)
{
u_int32_t status_reg;
debug_called(4);
/*
* Collect current interrupt status.
*/
status_reg = TWE_STATUS(sc);
twe_check_bits(sc, status_reg);
/*
* Dispatch based on interrupt status
*/
if (status_reg & TWE_STATUS_HOST_INTERRUPT)
twe_host_intr(sc);
if (status_reg & TWE_STATUS_ATTENTION_INTERRUPT)
twe_attention_intr(sc);
if (status_reg & TWE_STATUS_COMMAND_INTERRUPT)
twe_command_intr(sc);
if (status_reg & TWE_STATUS_RESPONSE_INTERRUPT)
twe_done(sc);
};
/********************************************************************************
* Pull as much work off the softc's work queue as possible and give it to the
* controller.
*/
void
twe_startio(struct twe_softc *sc)
{
struct twe_request *tr;
TWE_Command *cmd;
twe_bio *bp;
int error;
debug_called(4);
if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN))
return;
/* spin until something prevents us from doing any work */
for (;;) {
/* try to get a command that's already ready to go */
tr = twe_dequeue_ready(sc);
/* build a command from an outstanding bio */
if (tr == NULL) {
/* get a command to handle the bio with */
if (twe_get_request(sc, &tr))
break;
/* see if there's work to be done */
if ((bp = twe_dequeue_bio(sc)) == NULL) {
twe_release_request(tr);
break;
}
/* connect the bio to the command */
tr->tr_complete = twe_completeio;
tr->tr_private = bp;
tr->tr_data = TWE_BIO_DATA(bp);
tr->tr_length = TWE_BIO_LENGTH(bp);
cmd = TWE_FIND_COMMAND(tr);
if (TWE_BIO_IS_READ(bp)) {
tr->tr_flags |= TWE_CMD_DATAIN;
cmd->io.opcode = TWE_OP_READ;
} else {
tr->tr_flags |= TWE_CMD_DATAOUT;
cmd->io.opcode = TWE_OP_WRITE;
}
/* build a suitable I/O command (assumes 512-byte rounded transfers) */
cmd->io.size = 3;
cmd->io.unit = TWE_BIO_UNIT(bp);
cmd->io.block_count = (tr->tr_length + TWE_BLOCK_SIZE - 1) / TWE_BLOCK_SIZE;
cmd->io.lba = TWE_BIO_LBA(bp);
}
/* did we find something to do? */
if (tr == NULL)
break;
/* try to map and submit the command to controller */
error = twe_map_request(tr);
if (error != 0) {
if (error == EBUSY)
break;
tr->tr_status = TWE_CMD_ERROR;
if (tr->tr_private != NULL) {
bp = (twe_bio *)(tr->tr_private);
TWE_BIO_SET_ERROR(bp, error);
tr->tr_private = NULL;
twed_intr(bp);
twe_release_request(tr);
} else if (tr->tr_flags & TWE_CMD_SLEEPER)
wakeup_one(tr); /* wakeup the sleeping owner */
}
}
}
/********************************************************************************
* Write blocks from memory to disk, for system crash dumps.
*/
int
twe_dump_blocks(struct twe_softc *sc, int unit, u_int32_t lba, void *data, int nblks)
{
struct twe_request *tr;
TWE_Command *cmd;
int error;
if (twe_get_request(sc, &tr))
return(ENOMEM);
tr->tr_data = data;
tr->tr_status = TWE_CMD_SETUP;
tr->tr_length = nblks * TWE_BLOCK_SIZE;
tr->tr_flags = TWE_CMD_DATAOUT;
cmd = TWE_FIND_COMMAND(tr);
cmd->io.opcode = TWE_OP_WRITE;
cmd->io.size = 3;
cmd->io.unit = unit;
cmd->io.block_count = nblks;
cmd->io.lba = lba;
error = twe_immediate_request(tr, 0);
if (error == 0)
if (twe_report_request(tr))
error = EIO;
twe_release_request(tr);
return(error);
}
/********************************************************************************
* Handle controller-specific control operations.
*/
int
twe_ioctl(struct twe_softc *sc, int ioctlcmd, void *addr)
{
struct twe_usercommand *tu = (struct twe_usercommand *)addr;
struct twe_paramcommand *tp = (struct twe_paramcommand *)addr;
struct twe_drivecommand *td = (struct twe_drivecommand *)addr;
union twe_statrequest *ts = (union twe_statrequest *)addr;
TWE_Param *param;
TWE_Command *cmd;
void *data;
u_int16_t *aen_code = (u_int16_t *)addr;
struct twe_request *tr;
u_int8_t srid;
int s, error;
error = 0;
switch(ioctlcmd) {
/* handle a command from userspace */
case TWEIO_COMMAND:
/* get a request */
while (twe_get_request(sc, &tr))
tsleep(sc, PPAUSE, "twioctl", hz);
/*
* Save the command's request ID, copy the user-supplied command in,
* restore the request ID.
*/
cmd = TWE_FIND_COMMAND(tr);
srid = cmd->generic.request_id;
bcopy(&tu->tu_command, cmd, sizeof(TWE_Command));
cmd->generic.request_id = srid;
/*
* if there's a data buffer, allocate and copy it in.
* Must be in multipled of 512 bytes.
*/
tr->tr_length = (tu->tu_size + 511) & ~511;
if (tr->tr_length > 0) {
if ((tr->tr_data = malloc(tr->tr_length, M_DEVBUF, M_WAITOK)) == NULL) {
error = ENOMEM;
goto cmd_done;
}
if ((error = copyin(tu->tu_data, tr->tr_data, tu->tu_size)) != 0)
goto cmd_done;
tr->tr_flags |= TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
}
/* run the command */
error = twe_wait_request(tr);
if (error)
goto cmd_done;
/* copy the command out again */
bcopy(cmd, &tu->tu_command, sizeof(TWE_Command));
/* if there was a data buffer, copy it out */
if (tr->tr_length > 0)
error = copyout(tr->tr_data, tu->tu_data, tu->tu_size);
cmd_done:
/* free resources */
if (tr->tr_data != NULL)
free(tr->tr_data, M_DEVBUF);
if (tr != NULL)
twe_release_request(tr);
break;
/* fetch statistics counter */
case TWEIO_STATS:
switch (ts->ts_item) {
#ifdef TWE_PERFORMANCE_MONITOR
case TWEQ_FREE:
case TWEQ_BIO:
case TWEQ_READY:
case TWEQ_BUSY:
case TWEQ_COMPLETE:
bcopy(&sc->twe_qstat[ts->ts_item], &ts->ts_qstat, sizeof(struct twe_qstat));
break;
#endif
default:
error = ENOENT;
break;
}
break;
/* poll for an AEN */
case TWEIO_AEN_POLL:
*aen_code = twe_dequeue_aen(sc);
break;
/* wait for another AEN to show up */
case TWEIO_AEN_WAIT:
s = splbio();
while ((*aen_code = twe_dequeue_aen(sc)) == TWE_AEN_QUEUE_EMPTY) {
error = tsleep(&sc->twe_aen_queue, PRIBIO | PCATCH, "tweaen", 0);
if (error == EINTR)
break;
}
splx(s);
break;
case TWEIO_GET_PARAM:
if ((param = twe_get_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, NULL)) == NULL) {
twe_printf(sc, "TWEIO_GET_PARAM failed for 0x%x/0x%x/%d\n",
tp->tp_table_id, tp->tp_param_id, tp->tp_size);
error = EINVAL;
} else {
if (param->parameter_size_bytes > tp->tp_size) {
twe_printf(sc, "TWEIO_GET_PARAM parameter too large (%d > %d)\n",
param->parameter_size_bytes, tp->tp_size);
error = EFAULT;
} else {
error = copyout(param->data, tp->tp_data, param->parameter_size_bytes);
}
free(param, M_DEVBUF);
}
break;
case TWEIO_SET_PARAM:
if ((data = malloc(tp->tp_size, M_DEVBUF, M_WAITOK)) == NULL) {
error = ENOMEM;
} else {
error = copyin(tp->tp_data, data, tp->tp_size);
if (error == 0)
error = twe_set_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, data);
free(data, M_DEVBUF);
}
break;
case TWEIO_RESET:
twe_reset(sc);
break;
case TWEIO_ADD_UNIT:
error = twe_add_unit(sc, td->td_unit);
break;
case TWEIO_DEL_UNIT:
error = twe_del_unit(sc, td->td_unit);
break;
/* XXX implement ATA PASSTHROUGH */
/* nothing we understand */
default:
error = ENOTTY;
}
return(error);
}
/********************************************************************************
* Enable the useful interrupts from the controller.
*/
void
twe_enable_interrupts(struct twe_softc *sc)
{
sc->twe_state |= TWE_STATE_INTEN;
TWE_CONTROL(sc,
TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT |
TWE_CONTROL_UNMASK_RESPONSE_INTERRUPT |
TWE_CONTROL_ENABLE_INTERRUPTS);
}
/********************************************************************************
* Disable interrupts from the controller.
*/
void
twe_disable_interrupts(struct twe_softc *sc)
{
TWE_CONTROL(sc, TWE_CONTROL_DISABLE_INTERRUPTS);
sc->twe_state &= ~TWE_STATE_INTEN;
}
/********************************************************************************
********************************************************************************
Command Submission
********************************************************************************
********************************************************************************/
/********************************************************************************
* Read integer parameter table entries.
*/
static int
twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result)
{
TWE_Param *param;
if ((param = twe_get_param(sc, table_id, param_id, 1, NULL)) == NULL)
return(ENOENT);
*result = *(u_int8_t *)param->data;
free(param, M_DEVBUF);
return(0);
}
static int
twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result)
{
TWE_Param *param;
if ((param = twe_get_param(sc, table_id, param_id, 2, NULL)) == NULL)
return(ENOENT);
*result = *(u_int16_t *)param->data;
free(param, M_DEVBUF);
return(0);
}
static int
twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result)
{
TWE_Param *param;
if ((param = twe_get_param(sc, table_id, param_id, 4, NULL)) == NULL)
return(ENOENT);
*result = *(u_int32_t *)param->data;
free(param, M_DEVBUF);
return(0);
}
/********************************************************************************
* Perform a TWE_OP_GET_PARAM command. If a callback function is provided, it
* will be called with the command when it's completed. If no callback is
* provided, we will wait for the command to complete and then return just the data.
* The caller is responsible for freeing the data when done with it.
*/
static void *
twe_get_param(struct twe_softc *sc, int table_id, int param_id, size_t param_size,
void (* func)(struct twe_request *tr))
{
struct twe_request *tr;
TWE_Command *cmd;
TWE_Param *param;
int error;
debug_called(4);
tr = NULL;
param = NULL;
/* get a command */
if (twe_get_request(sc, &tr))
goto err;
/* get a buffer */
if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
goto err;
tr->tr_data = param;
tr->tr_length = TWE_SECTOR_SIZE;
tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
/* build the command for the controller */
cmd = TWE_FIND_COMMAND(tr);
cmd->param.opcode = TWE_OP_GET_PARAM;
cmd->param.size = 2;
cmd->param.unit = 0;
cmd->param.param_count = 1;
/* fill in the outbound parameter data */
param->table_id = table_id;
param->parameter_id = param_id;
param->parameter_size_bytes = param_size;
/* submit the command and either wait or let the callback handle it */
if (func == NULL) {
/* XXX could use twe_wait_request here if interrupts were enabled? */
error = twe_immediate_request(tr, 1 /* usetmp */);
if (error == 0) {
if (twe_report_request(tr))
goto err;
} else {
goto err;
}
twe_release_request(tr);
return(param);
} else {
tr->tr_complete = func;
error = twe_map_request(tr);
if ((error == 0) || (error == EBUSY))
return(func);
}
/* something failed */
err:
debug(1, "failed");
if (tr != NULL)
twe_release_request(tr);
if (param != NULL)
free(param, M_DEVBUF);
return(NULL);
}
/********************************************************************************
* Set integer parameter table entries.
*/
#ifdef TWE_SHUTDOWN_NOTIFICATION
static int
twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value)
{
return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
}
#endif
#if 0
static int
twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value)
{
return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
}
static int
twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value)
{
return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
}
#endif
/********************************************************************************
* Perform a TWE_OP_SET_PARAM command, returns nonzero on error.
*/
static int
twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size, void *data)
{
struct twe_request *tr;
TWE_Command *cmd;
TWE_Param *param;
int error;
debug_called(4);
tr = NULL;
param = NULL;
error = ENOMEM;
/* get a command */
if (twe_get_request(sc, &tr))
goto out;
/* get a buffer */
if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
goto out;
tr->tr_data = param;
tr->tr_length = TWE_SECTOR_SIZE;
tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
/* build the command for the controller */
cmd = TWE_FIND_COMMAND(tr);
cmd->param.opcode = TWE_OP_SET_PARAM;
cmd->param.size = 2;
cmd->param.unit = 0;
cmd->param.param_count = 1;
/* fill in the outbound parameter data */
param->table_id = table_id;
param->parameter_id = param_id;
param->parameter_size_bytes = param_size;
bcopy(data, param->data, param_size);
/* XXX could use twe_wait_request here if interrupts were enabled? */
error = twe_immediate_request(tr, 1 /* usetmp */);
if (error == 0) {
if (twe_report_request(tr))
error = EIO;
}
out:
if (tr != NULL)
twe_release_request(tr);
if (param != NULL)
free(param, M_DEVBUF);
return(error);
}
/********************************************************************************
* Perform a TWE_OP_INIT_CONNECTION command, returns nonzero on error.
*
* Typically called with interrupts disabled.
*/
static int
twe_init_connection(struct twe_softc *sc, int mode)
{
struct twe_request *tr;
TWE_Command *cmd;
int error;
debug_called(4);
/* get a command */
if (twe_get_request(sc, &tr))
return(0);
/* build the command */
cmd = TWE_FIND_COMMAND(tr);
cmd->initconnection.opcode = TWE_OP_INIT_CONNECTION;
cmd->initconnection.size = 3;
cmd->initconnection.host_id = 0;
cmd->initconnection.message_credits = mode;
cmd->initconnection.response_queue_pointer = 0;
/* submit the command */
error = twe_immediate_request(tr, 0 /* usetmp */);
twe_release_request(tr);
if (mode == TWE_INIT_MESSAGE_CREDITS)
sc->twe_host_id = cmd->initconnection.host_id;
return(error);
}
/********************************************************************************
* Start the command (tr) and sleep waiting for it to complete.
*
* Successfully completed commands are dequeued.
*/
static int
twe_wait_request(struct twe_request *tr)
{
int s;
debug_called(4);
tr->tr_flags |= TWE_CMD_SLEEPER;
tr->tr_status = TWE_CMD_BUSY;
twe_enqueue_ready(tr);
twe_startio(tr->tr_sc);
s = splbio();
while (tr->tr_status == TWE_CMD_BUSY)
tsleep(tr, PRIBIO, "twewait", 0);
splx(s);
return(tr->tr_status != TWE_CMD_COMPLETE);
}
/********************************************************************************
* Start the command (tr) and busy-wait for it to complete.
* This should only be used when interrupts are actually disabled (although it
* will work if they are not).
*/
static int
twe_immediate_request(struct twe_request *tr, int usetmp)
{
struct twe_softc *sc;
int error;
int count = 0;
debug_called(4);
sc = tr->tr_sc;
if (usetmp && (tr->tr_data != NULL)) {
tr->tr_flags |= TWE_CMD_IMMEDIATE;
if (tr->tr_length > MAXBSIZE)
return (EINVAL);
bcopy(tr->tr_data, sc->twe_immediate, tr->tr_length);
}
tr->tr_status = TWE_CMD_BUSY;
if ((error = twe_map_request(tr)) != 0)
if (error != EBUSY)
return(error);
/* Wait up to 5 seconds for the command to complete */
while ((count++ < 5000) && (tr->tr_status == TWE_CMD_BUSY)){
DELAY(1000);
twe_done(sc);
}
if (usetmp && (tr->tr_data != NULL))
bcopy(sc->twe_immediate, tr->tr_data, tr->tr_length);
return(tr->tr_status != TWE_CMD_COMPLETE);
}
/********************************************************************************
* Handle completion of an I/O command.
*/
static void
twe_completeio(struct twe_request *tr)
{
TWE_Command *cmd = TWE_FIND_COMMAND(tr);
struct twe_softc *sc = tr->tr_sc;
twe_bio *bp = (twe_bio *)tr->tr_private;
debug_called(4);
if (tr->tr_status == TWE_CMD_COMPLETE) {
if (cmd->generic.status)
if (twe_report_request(tr))
TWE_BIO_SET_ERROR(bp, EIO);
} else {
twe_panic(sc, "twe_completeio on incomplete command");
}
tr->tr_private = NULL;
twed_intr(bp);
twe_release_request(tr);
}
/********************************************************************************
* Reset the controller and pull all the active commands back onto the ready
* queue. Used to restart a controller that's exhibiting bad behaviour.
*/
static void
twe_reset(struct twe_softc *sc)
{
struct twe_request *tr;
int i, s;
/*
* Sleep for a short period to allow AENs to be signalled.
*/
tsleep(sc, PRIBIO, "twereset", hz);
/*
* Disable interrupts from the controller, and mask any accidental entry
* into our interrupt handler.
*/
twe_printf(sc, "controller reset in progress...\n");
twe_disable_interrupts(sc);
s = splbio();
/*
* Try to soft-reset the controller.
*/
for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
if (i > 0)
twe_printf(sc, "reset %d failed, trying again\n", i);
if (!twe_soft_reset(sc))
break; /* reset process complete */
}
/* did we give up? */
if (i >= TWE_MAX_RESET_TRIES) {
twe_printf(sc, "can't reset controller, giving up\n");
goto out;
}
/*
* Move all of the commands that were busy back to the ready queue.
*/
i = 0;
while ((tr = twe_dequeue_busy(sc)) != NULL) {
twe_enqueue_ready(tr);
i++;
}
/*
* Kick the controller to start things going again, then re-enable interrupts.
*/
twe_startio(sc);
twe_enable_interrupts(sc);
twe_printf(sc, "controller reset done, %d commands restarted\n", i);
out:
splx(s);
twe_enable_interrupts(sc);
}
/********************************************************************************
********************************************************************************
Command I/O to Controller
********************************************************************************
********************************************************************************/
/********************************************************************************
* Try to deliver (tr) to the controller.
*
* Can be called at any interrupt level, with or without interrupts enabled.
*/
int
twe_start(struct twe_request *tr)
{
struct twe_softc *sc = tr->tr_sc;
TWE_Command *cmd;
int i, s, done;
u_int32_t status_reg;
debug_called(4);
/* mark the command as currently being processed */
tr->tr_status = TWE_CMD_BUSY;
cmd = TWE_FIND_COMMAND(tr);
/*
* Spin briefly waiting for the controller to come ready
*
* XXX it might be more efficient to return EBUSY immediately
* and let the command be rescheduled.
*/
for (i = 100000, done = 0; (i > 0) && !done; i--) {
s = splbio();
/* check to see if we can post a command */
status_reg = TWE_STATUS(sc);
twe_check_bits(sc, status_reg);
if (!(status_reg & TWE_STATUS_COMMAND_QUEUE_FULL)) {
twe_enqueue_busy(tr);
TWE_COMMAND_QUEUE(sc, TWE_FIND_COMMANDPHYS(tr));
done = 1;
/* move command to work queue */
#ifdef TWE_DEBUG
if (tr->tr_complete != NULL) {
debug(3, "queued request %d with callback %p", cmd->generic.request_id, tr->tr_complete);
} else if (tr->tr_flags & TWE_CMD_SLEEPER) {
debug(3, "queued request %d with wait channel %p", cmd->generic.request_id, tr);
} else {
debug(3, "queued request %d for polling caller", cmd->generic.request_id);
}
#endif
}
splx(s); /* drop spl to allow completion interrupts */
}
/* command is enqueued */
if (done)
return(0);
/*
* We couldn't get the controller to take the command; try submitting it again later.
* This should only happen if something is wrong with the controller, or if we have
* overestimated the number of commands it can accept. (Should we actually reject
* the command at this point?)
*/
return(EBUSY);
}
/********************************************************************************
* Poll the controller (sc) for completed commands.
*
* Can be called at any interrupt level, with or without interrupts enabled.
*/
static void
twe_done(struct twe_softc *sc)
{
TWE_Response_Queue rq;
TWE_Command *cmd;
struct twe_request *tr;
int s, found;
u_int32_t status_reg;
debug_called(5);
/* loop collecting completed commands */
found = 0;
s = splbio();
for (;;) {
status_reg = TWE_STATUS(sc);
twe_check_bits(sc, status_reg); /* XXX should this fail? */
if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)) {
found = 1;
rq = TWE_RESPONSE_QUEUE(sc);
tr = sc->twe_lookup[rq.u.response_id]; /* find command */
cmd = TWE_FIND_COMMAND(tr);
if (tr->tr_status != TWE_CMD_BUSY)
twe_printf(sc, "completion event for nonbusy command\n");
tr->tr_status = TWE_CMD_COMPLETE;
debug(3, "completed request id %d with status %d",
cmd->generic.request_id, cmd->generic.status);
/* move to completed queue */
twe_remove_busy(tr);
twe_enqueue_complete(tr);
sc->twe_state &= ~TWE_STATE_CTLR_BUSY;
} else {
break; /* no response ready */
}
}
splx(s);
/* if we've completed any commands, try posting some more */
if (found)
twe_startio(sc);
/* handle completion and timeouts */
twe_complete(sc); /* XXX use deferred completion? */
}
/********************************************************************************
* Perform post-completion processing for commands on (sc).
*
* This is split from twe_done as it can be safely deferred and run at a lower
* priority level should facilities for such a thing become available.
*/
static void
twe_complete(struct twe_softc *sc)
{
struct twe_request *tr;
debug_called(5);
/*
* Pull commands off the completed list, dispatch them appropriately
*/
while ((tr = twe_dequeue_complete(sc)) != NULL) {
/* unmap the command's data buffer */
twe_unmap_request(tr);
/* dispatch to suit command originator */
if (tr->tr_complete != NULL) { /* completion callback */
debug(2, "call completion handler %p", tr->tr_complete);
tr->tr_complete(tr);
} else if (tr->tr_flags & TWE_CMD_SLEEPER) { /* caller is asleep waiting */
debug(2, "wake up command owner on %p", tr);
wakeup_one(tr);
} else { /* caller is polling command */
debug(2, "command left for owner");
}
}
}
/********************************************************************************
* Wait for (status) to be set in the controller status register for up to
* (timeout) seconds. Returns 0 if found, nonzero if we time out.
*
* Note: this busy-waits, rather than sleeping, since we may be called with
* eg. clock interrupts masked.
*/
static int
twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout)
{
time_t expiry;
u_int32_t status_reg;
debug_called(4);
expiry = time_second + timeout;
do {
status_reg = TWE_STATUS(sc);
if (status_reg & status) /* got the required bit(s)? */
return(0);
DELAY(100000);
} while (time_second <= expiry);
return(1);
}
/********************************************************************************
* Drain the response queue, which may contain responses to commands we know
* nothing about.
*/
static int
twe_drain_response_queue(struct twe_softc *sc)
{
TWE_Response_Queue rq;
u_int32_t status_reg;
debug_called(4);
for (;;) { /* XXX give up eventually? */
status_reg = TWE_STATUS(sc);
if (twe_check_bits(sc, status_reg))
return(1);
if (status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)
return(0);
rq = TWE_RESPONSE_QUEUE(sc);
}
}
/********************************************************************************
* Soft-reset the controller
*/
static int
twe_soft_reset(struct twe_softc *sc)
{
u_int32_t status_reg;
debug_called(2);
TWE_SOFT_RESET(sc);
if (twe_wait_status(sc, TWE_STATUS_ATTENTION_INTERRUPT, 30)) {
twe_printf(sc, "no attention interrupt\n");
return(1);
}
TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
if (twe_drain_aen_queue(sc)) {
twe_printf(sc, "can't drain AEN queue\n");
return(1);
}
if (twe_find_aen(sc, TWE_AEN_SOFT_RESET)) {
twe_printf(sc, "reset not reported\n");
return(1);
}
status_reg = TWE_STATUS(sc);
if (TWE_STATUS_ERRORS(status_reg) || twe_check_bits(sc, status_reg)) {
twe_printf(sc, "controller errors detected\n");
return(1);
}
if (twe_drain_response_queue(sc)) {
twe_printf(sc, "can't drain response queue\n");
return(1);
}
return(0);
}
/********************************************************************************
********************************************************************************
Interrupt Handling
********************************************************************************
********************************************************************************/
/********************************************************************************
* Host interrupt.
*
* XXX what does this mean?
*/
static void
twe_host_intr(struct twe_softc *sc)
{
debug_called(4);
twe_printf(sc, "host interrupt\n");
TWE_CONTROL(sc, TWE_CONTROL_CLEAR_HOST_INTERRUPT);
}
/********************************************************************************
* Attention interrupt.
*
* Signalled when the controller has one or more AENs for us.
*/
static void
twe_attention_intr(struct twe_softc *sc)
{
debug_called(4);
/* instigate a poll for AENs */
if (twe_fetch_aen(sc)) {
twe_printf(sc, "error polling for signalled AEN\n");
} else {
TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
}
}
/********************************************************************************
* Command interrupt.
*
* Signalled when the controller can handle more commands.
*/
static void
twe_command_intr(struct twe_softc *sc)
{
debug_called(4);
/*
* We don't use this, rather we try to submit commands when we receive
* them, and when other commands have completed. Mask it so we don't get
* another one.
*/
TWE_CONTROL(sc, TWE_CONTROL_MASK_COMMAND_INTERRUPT);
}
/********************************************************************************
********************************************************************************
Asynchronous Event Handling
********************************************************************************
********************************************************************************/
/********************************************************************************
* Request an AEN from the controller.
*/
static int
twe_fetch_aen(struct twe_softc *sc)
{
debug_called(4);
if ((twe_get_param(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2, twe_handle_aen)) == NULL)
return(EIO);
return(0);
}
/********************************************************************************
* Handle an AEN returned by the controller.
*/
static void
twe_handle_aen(struct twe_request *tr)
{
struct twe_softc *sc = tr->tr_sc;
TWE_Param *param;
u_int16_t aen;
debug_called(4);
/* XXX check for command success somehow? */
param = (TWE_Param *)tr->tr_data;
aen = *(u_int16_t *)(param->data);
free(tr->tr_data, M_DEVBUF);
twe_release_request(tr);
twe_enqueue_aen(sc, aen);
/* XXX poll for more AENs? */
}
/********************************************************************************
* Pull AENs out of the controller and park them in the queue, in a context where
* interrupts aren't active. Return nonzero if we encounter any errors in the
* process of obtaining all the available AENs.
*/
static int
twe_drain_aen_queue(struct twe_softc *sc)
{
u_int16_t aen;
for (;;) {
if (twe_get_param_2(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, &aen))
return(1);
if (aen == TWE_AEN_QUEUE_EMPTY)
return(0);
twe_enqueue_aen(sc, aen);
}
}
/********************************************************************************
* Push an AEN that we've received onto the queue.
*
* Note that we have to lock this against reentrance, since it may be called
* from both interrupt and non-interrupt context.
*
* If someone is waiting for the AEN we have, wake them up.
*/
static void
twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen)
{
char *msg;
int s, next, nextnext;
debug_called(4);
if ((msg = twe_format_aen(sc, aen)) != NULL)
twe_printf(sc, "AEN: <%s>\n", msg);
s = splbio();
/* enqueue the AEN */
next = ((sc->twe_aen_head + 1) % TWE_Q_LENGTH);
nextnext = ((sc->twe_aen_head + 2) % TWE_Q_LENGTH);
/* check to see if this is the last free slot, and subvert the AEN if it is */
if (nextnext == sc->twe_aen_tail)
aen = TWE_AEN_QUEUE_FULL;
/* look to see if there's room for this AEN */
if (next != sc->twe_aen_tail) {
sc->twe_aen_queue[sc->twe_aen_head] = aen;
sc->twe_aen_head = next;
}
/* wake up anyone asleep on the queue */
wakeup(&sc->twe_aen_queue);
/* anyone looking for this AEN? */
if (sc->twe_wait_aen == aen) {
sc->twe_wait_aen = -1;
wakeup(&sc->twe_wait_aen);
}
splx(s);
}
/********************************************************************************
* Pop an AEN off the queue, or return -1 if there are none left.
*
* We are more or less interrupt-safe, so don't block interrupts.
*/
static u_int16_t
twe_dequeue_aen(struct twe_softc *sc)
{
u_int16_t result;
debug_called(4);
if (sc->twe_aen_tail == sc->twe_aen_head) {
result = TWE_AEN_QUEUE_EMPTY;
} else {
result = sc->twe_aen_queue[sc->twe_aen_tail];
sc->twe_aen_tail = ((sc->twe_aen_tail + 1) % TWE_Q_LENGTH);
}
return(result);
}
/********************************************************************************
* Check to see if the requested AEN is in the queue.
*
* XXX we could probably avoid masking interrupts here
*/
static int
twe_find_aen(struct twe_softc *sc, u_int16_t aen)
{
int i, s, missing;
missing = 1;
s = splbio();
for (i = sc->twe_aen_tail; (i != sc->twe_aen_head) && missing; i = (i + 1) % TWE_Q_LENGTH) {
if (sc->twe_aen_queue[i] == aen)
missing = 0;
}
splx(s);
return(missing);
}
#if 0 /* currently unused */
/********************************************************************************
* Sleep waiting for at least (timeout) seconds until we see (aen) as
* requested. Returns nonzero on timeout or failure.
*
* XXX: this should not be used in cases where there may be more than one sleeper
* without a mechanism for registering multiple sleepers.
*/
static int
twe_wait_aen(struct twe_softc *sc, int aen, int timeout)
{
time_t expiry;
int found, s;
debug_called(4);
expiry = time_second + timeout;
found = 0;
s = splbio();
sc->twe_wait_aen = aen;
do {
twe_fetch_aen(sc);
tsleep(&sc->twe_wait_aen, PZERO, "twewaen", hz);
if (sc->twe_wait_aen == -1)
found = 1;
} while ((time_second <= expiry) && !found);
splx(s);
return(!found);
}
#endif
/********************************************************************************
********************************************************************************
Command Buffer Management
********************************************************************************
********************************************************************************/
/********************************************************************************
* Get a new command buffer.
*
* This will return NULL if all command buffers are in use.
*/
static int
twe_get_request(struct twe_softc *sc, struct twe_request **tr)
{
TWE_Command *cmd;
debug_called(4);
/* try to reuse an old buffer */
*tr = twe_dequeue_free(sc);
/* initialise some fields to their defaults */
if (*tr != NULL) {
cmd = TWE_FIND_COMMAND(*tr);
(*tr)->tr_data = NULL;
(*tr)->tr_private = NULL;
(*tr)->tr_status = TWE_CMD_SETUP; /* command is in setup phase */
(*tr)->tr_flags = 0;
(*tr)->tr_complete = NULL;
cmd->generic.status = 0; /* before submission to controller */
cmd->generic.flags = 0; /* not used */
}
return(*tr == NULL);
}
/********************************************************************************
* Release a command buffer for reuse.
*
*/
static void
twe_release_request(struct twe_request *tr)
{
debug_called(4);
if (tr->tr_private != NULL)
twe_panic(tr->tr_sc, "tr_private != NULL");
twe_enqueue_free(tr);
}
/********************************************************************************
********************************************************************************
Debugging
********************************************************************************
********************************************************************************/
/********************************************************************************
* Print some information about the controller
*/
void
twe_describe_controller(struct twe_softc *sc)
{
TWE_Param *p[6];
u_int8_t ports;
u_int32_t size;
int i;
debug_called(2);
/* get the port count */
twe_get_param_1(sc, TWE_PARAM_CONTROLLER, TWE_PARAM_CONTROLLER_PortCount, &ports);
/* get version strings */
p[0] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW, 16, NULL);
p[1] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS, 16, NULL);
if (p[0] && p[1])
twe_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n", ports, p[0]->data, p[1]->data);
if (bootverbose) {
p[2] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon, 16, NULL);
p[3] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB, 8, NULL);
p[4] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA, 8, NULL);
p[5] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI, 8, NULL);
if (p[2] && p[3] && p[4] && p[5])
twe_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n", p[2]->data, p[3]->data,
p[4]->data, p[5]->data);
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);
/* print attached drives */
if (bootverbose) {
p[0] = twe_get_param(sc, TWE_PARAM_DRIVESUMMARY, TWE_PARAM_DRIVESUMMARY_Status, 16, NULL);
for (i = 0; i < ports; i++) {
if (p[0]->data[i] != TWE_PARAM_DRIVESTATUS_Present)
continue;
twe_get_param_4(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Size, &size);
p[1] = twe_get_param(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Model, 40, NULL);
if (p[1] != NULL) {
twe_printf(sc, "port %d: %.40s %dMB\n", i, p[1]->data, size / 2048);
free(p[1], M_DEVBUF);
} else {
twe_printf(sc, "port %d, drive status unavailable\n", i);
}
}
if (p[0])
free(p[0], M_DEVBUF);
}
}
/********************************************************************************
* Look up a text description of a numeric code and return a pointer to same.
*/
char *
twe_describe_code(struct twe_code_lookup *table, u_int32_t code)
{
int i;
for (i = 0; table[i].string != NULL; i++)
if (table[i].code == code)
return(table[i].string);
return(table[i+1].string);
}
/********************************************************************************
* Complain if the status bits aren't what we're expecting.
*
* Rate-limit the complaints to at most one of each every five seconds, but
* always return the correct status.
*/
static int
twe_check_bits(struct twe_softc *sc, u_int32_t status_reg)
{
int result;
static time_t lastwarn[2] = {0, 0};
/*
* This can be a little problematic, as twe_panic may call twe_reset if
* TWE_DEBUG is not set, which will call us again as part of the soft reset.
*/
if ((status_reg & TWE_STATUS_PANIC_BITS) != 0) {
twe_printf(sc, "FATAL STATUS BIT(S) %b\n", status_reg & TWE_STATUS_PANIC_BITS,
TWE_STATUS_BITS_DESCRIPTION);
twe_panic(sc, "fatal status bits");
}
result = 0;
if ((status_reg & TWE_STATUS_EXPECTED_BITS) != TWE_STATUS_EXPECTED_BITS) {
if (time_second > (lastwarn[0] + 5)) {
twe_printf(sc, "missing expected status bit(s) %b\n", ~status_reg & TWE_STATUS_EXPECTED_BITS,
TWE_STATUS_BITS_DESCRIPTION);
lastwarn[0] = time_second;
}
result = 1;
}
if ((status_reg & TWE_STATUS_UNEXPECTED_BITS) != 0) {
if (time_second > (lastwarn[1] + 5)) {
twe_printf(sc, "unexpected status bit(s) %b\n", status_reg & TWE_STATUS_UNEXPECTED_BITS,
TWE_STATUS_BITS_DESCRIPTION);
lastwarn[1] = time_second;
}
result = 1;
if (status_reg & TWE_STATUS_PCI_PARITY_ERROR) {
twe_printf(sc, "PCI parity error: Reseat card, move card or buggy device present.");
twe_clear_pci_parity_error(sc);
}
if (status_reg & TWE_STATUS_PCI_ABORT) {
twe_printf(sc, "PCI abort, clearing.\n");
twe_clear_pci_abort(sc);
}
}
return(result);
}
/********************************************************************************
* Return a string describing (aen).
*
* The low 8 bits of the aen are the code, the high 8 bits give the unit number
* where an AEN is specific to a unit.
*
* Note that we could expand this routine to handle eg. up/downgrading the status
* of a drive if we had some idea of what the drive's initial status was.
*/
static char *
twe_format_aen(struct twe_softc *sc, u_int16_t aen)
{
static char buf[80];
device_t child;
char *code, *msg;
code = twe_describe_code(twe_table_aen, TWE_AEN_CODE(aen));
msg = code + 2;
switch (*code) {
case 'q':
if (!bootverbose)
return(NULL);
/* FALLTHROUGH */
case 'a':
return(msg);
case 'c':
if ((child = sc->twe_drive[TWE_AEN_UNIT(aen)].td_disk) != NULL) {
sprintf(buf, "twed%d: %s", device_get_unit(child), msg);
} else {
sprintf(buf, "twe%d: %s for unknown unit %d", device_get_unit(sc->twe_dev),
msg, TWE_AEN_UNIT(aen));
}
return(buf);
case 'p':
sprintf(buf, "twe%d: port %d: %s", device_get_unit(sc->twe_dev), TWE_AEN_UNIT(aen),
msg);
return(buf);
case 'x':
default:
break;
}
sprintf(buf, "unknown AEN 0x%x", aen);
return(buf);
}
/********************************************************************************
* Print a diagnostic if the status of the command warrants it, and return
* either zero (command was ok) or nonzero (command failed).
*/
static int
twe_report_request(struct twe_request *tr)
{
struct twe_softc *sc = tr->tr_sc;
TWE_Command *cmd = TWE_FIND_COMMAND(tr);
int result = 0;
/*
* Check the command status value and handle accordingly.
*/
if (cmd->generic.status == TWE_STATUS_RESET) {
/*
* The status code 0xff requests a controller reset.
*/
twe_printf(sc, "command returned with controller reset request\n");
twe_reset(sc);
result = 1;
} else if (cmd->generic.status > TWE_STATUS_FATAL) {
/*
* Fatal errors that don't require controller reset.
*
* We know a few special flags values.
*/
switch (cmd->generic.flags) {
case 0x1b:
device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
"drive timeout");
break;
case 0x51:
device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
"unrecoverable drive error");
break;
default:
device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
"controller error - %s (flags = 0x%x)\n",
twe_describe_code(twe_table_status, cmd->generic.status),
cmd->generic.flags);
result = 1;
}
} else if (cmd->generic.status > TWE_STATUS_WARNING) {
/*
* Warning level status.
*/
device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
"warning - %s (flags = 0x%x)\n",
twe_describe_code(twe_table_status, cmd->generic.status),
cmd->generic.flags);
} else if (cmd->generic.status > 0x40) {
/*
* Info level status.
*/
device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
"attention - %s (flags = 0x%x)\n",
twe_describe_code(twe_table_status, cmd->generic.status),
cmd->generic.flags);
}
return(result);
}
/********************************************************************************
* Print some controller state to aid in debugging error/panic conditions
*/
void
twe_print_controller(struct twe_softc *sc)
{
u_int32_t status_reg;
status_reg = TWE_STATUS(sc);
twe_printf(sc, "status %b\n", status_reg, TWE_STATUS_BITS_DESCRIPTION);
twe_printf(sc, " current max min\n");
twe_printf(sc, "free %04d %04d %04d\n",
sc->twe_qstat[TWEQ_FREE].q_length, sc->twe_qstat[TWEQ_FREE].q_max, sc->twe_qstat[TWEQ_FREE].q_min);
twe_printf(sc, "ready %04d %04d %04d\n",
sc->twe_qstat[TWEQ_READY].q_length, sc->twe_qstat[TWEQ_READY].q_max, sc->twe_qstat[TWEQ_READY].q_min);
twe_printf(sc, "busy %04d %04d %04d\n",
sc->twe_qstat[TWEQ_BUSY].q_length, sc->twe_qstat[TWEQ_BUSY].q_max, sc->twe_qstat[TWEQ_BUSY].q_min);
twe_printf(sc, "complete %04d %04d %04d\n",
sc->twe_qstat[TWEQ_COMPLETE].q_length, sc->twe_qstat[TWEQ_COMPLETE].q_max, sc->twe_qstat[TWEQ_COMPLETE].q_min);
twe_printf(sc, "bioq %04d %04d %04d\n",
sc->twe_qstat[TWEQ_BIO].q_length, sc->twe_qstat[TWEQ_BIO].q_max, sc->twe_qstat[TWEQ_BIO].q_min);
twe_printf(sc, "AEN queue head %d tail %d\n", sc->twe_aen_head, sc->twe_aen_tail);
}
static void
twe_panic(struct twe_softc *sc, char *reason)
{
twe_print_controller(sc);
#ifdef TWE_DEBUG
panic(reason);
#else
twe_reset(sc);
#endif
}
#if 0
/********************************************************************************
* Print a request/command in human-readable format.
*/
static void
twe_print_request(struct twe_request *tr)
{
struct twe_softc *sc = tr->tr_sc;
TWE_Command *cmd = TWE_FIND_COMMAND(tr);
int i;
twe_printf(sc, "CMD: request_id %d opcode <%s> size %d unit %d host_id %d\n",
cmd->generic.request_id, twe_describe_code(twe_table_opcode, cmd->generic.opcode), cmd->generic.size,
cmd->generic.unit, cmd->generic.host_id);
twe_printf(sc, " status %d flags 0x%x count %d sgl_offset %d\n",
cmd->generic.status, cmd->generic.flags, cmd->generic.count, cmd->generic.sgl_offset);
switch(cmd->generic.opcode) { /* XXX add more opcodes? */
case TWE_OP_READ:
case TWE_OP_WRITE:
twe_printf(sc, " lba %d\n", cmd->io.lba);
for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->io.sgl[i].length != 0); i++)
twe_printf(sc, " %d: 0x%x/%d\n",
i, cmd->io.sgl[i].address, cmd->io.sgl[i].length);
break;
case TWE_OP_GET_PARAM:
case TWE_OP_SET_PARAM:
for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->param.sgl[i].length != 0); i++)
twe_printf(sc, " %d: 0x%x/%d\n",
i, cmd->param.sgl[i].address, cmd->param.sgl[i].length);
break;
case TWE_OP_INIT_CONNECTION:
twe_printf(sc, " response queue pointer 0x%x\n",
cmd->initconnection.response_queue_pointer);
break;
default:
break;
}
twe_printf(sc, " tr_command %p/0x%x tr_data %p/0x%x,%d\n",
tr, TWE_FIND_COMMANDPHYS(tr), tr->tr_data, tr->tr_dataphys, tr->tr_length);
twe_printf(sc, " tr_status %d tr_flags 0x%x tr_complete %p tr_private %p\n",
tr->tr_status, tr->tr_flags, tr->tr_complete, tr->tr_private);
}
#endif
|