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
|
/*-
* Low level routines for the Advanced Systems Inc. SCSI controllers chips
*
* Copyright (c) 1996-1997, 1999-2000 Justin Gibbs.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification, immediately at the beginning of the file.
* 2. 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.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*-
* Ported from:
* advansys.c - Linux Host Driver for AdvanSys SCSI Adapters
*
* Copyright (c) 1995-1996 Advanced System Products, Inc.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that redistributions of source
* code retain the above copyright notice and this comment without
* modification.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/lock.h>
#include <sys/kernel.h>
#include <sys/mutex.h>
#include <sys/systm.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/cam_sim.h>
#include <cam/cam_xpt_sim.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_message.h>
#include <cam/scsi/scsi_da.h>
#include <cam/scsi/scsi_cd.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/pmap.h>
#include <dev/advansys/advansys.h>
#include <dev/advansys/advmcode.h>
struct adv_quirk_entry {
struct scsi_inquiry_pattern inq_pat;
u_int8_t quirks;
#define ADV_QUIRK_FIX_ASYN_XFER_ALWAYS 0x01
#define ADV_QUIRK_FIX_ASYN_XFER 0x02
};
static struct adv_quirk_entry adv_quirk_table[] =
{
{
{ T_CDROM, SIP_MEDIA_REMOVABLE, "HP", "*", "*" },
ADV_QUIRK_FIX_ASYN_XFER_ALWAYS|ADV_QUIRK_FIX_ASYN_XFER
},
{
{ T_CDROM, SIP_MEDIA_REMOVABLE, "NEC", "CD-ROM DRIVE", "*" },
0
},
{
{
T_SEQUENTIAL, SIP_MEDIA_REMOVABLE,
"TANDBERG", " TDC 36", "*"
},
0
},
{
{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK", "*", "*" },
0
},
{
{
T_PROCESSOR, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
"*", "*", "*"
},
0
},
{
{
T_SCANNER, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
"*", "*", "*"
},
0
},
{
/* Default quirk entry */
{
T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
/*vendor*/"*", /*product*/"*", /*revision*/"*"
},
ADV_QUIRK_FIX_ASYN_XFER,
}
};
/*
* Allowable periods in ns
*/
static u_int8_t adv_sdtr_period_tbl[] =
{
25,
30,
35,
40,
50,
60,
70,
85
};
static u_int8_t adv_sdtr_period_tbl_ultra[] =
{
12,
19,
25,
32,
38,
44,
50,
57,
63,
69,
75,
82,
88,
94,
100,
107
};
struct ext_msg {
u_int8_t msg_type;
u_int8_t msg_len;
u_int8_t msg_req;
union {
struct {
u_int8_t sdtr_xfer_period;
u_int8_t sdtr_req_ack_offset;
} sdtr;
struct {
u_int8_t wdtr_width;
} wdtr;
struct {
u_int8_t mdp[4];
} mdp;
} u_ext_msg;
u_int8_t res;
};
#define xfer_period u_ext_msg.sdtr.sdtr_xfer_period
#define req_ack_offset u_ext_msg.sdtr.sdtr_req_ack_offset
#define wdtr_width u_ext_msg.wdtr.wdtr_width
#define mdp_b3 u_ext_msg.mdp_b3
#define mdp_b2 u_ext_msg.mdp_b2
#define mdp_b1 u_ext_msg.mdp_b1
#define mdp_b0 u_ext_msg.mdp_b0
/*
* Some of the early PCI adapters have problems with
* async transfers. Instead use an offset of 1.
*/
#define ASYN_SDTR_DATA_FIX_PCI_REV_AB 0x41
/* LRAM routines */
static void adv_read_lram_16_multi(struct adv_softc *adv, u_int16_t s_addr,
u_int16_t *buffer, int count);
static void adv_write_lram_16_multi(struct adv_softc *adv,
u_int16_t s_addr, u_int16_t *buffer,
int count);
static void adv_mset_lram_16(struct adv_softc *adv, u_int16_t s_addr,
u_int16_t set_value, int count);
static u_int32_t adv_msum_lram_16(struct adv_softc *adv, u_int16_t s_addr,
int count);
static int adv_write_and_verify_lram_16(struct adv_softc *adv,
u_int16_t addr, u_int16_t value);
static u_int32_t adv_read_lram_32(struct adv_softc *adv, u_int16_t addr);
static void adv_write_lram_32(struct adv_softc *adv, u_int16_t addr,
u_int32_t value);
static void adv_write_lram_32_multi(struct adv_softc *adv,
u_int16_t s_addr, u_int32_t *buffer,
int count);
/* EEPROM routines */
static u_int16_t adv_read_eeprom_16(struct adv_softc *adv, u_int8_t addr);
static u_int16_t adv_write_eeprom_16(struct adv_softc *adv, u_int8_t addr,
u_int16_t value);
static int adv_write_eeprom_cmd_reg(struct adv_softc *adv,
u_int8_t cmd_reg);
static int adv_set_eeprom_config_once(struct adv_softc *adv,
struct adv_eeprom_config *eeconfig);
/* Initialization */
static u_int32_t adv_load_microcode(struct adv_softc *adv, u_int16_t s_addr,
u_int16_t *mcode_buf, u_int16_t mcode_size);
static void adv_reinit_lram(struct adv_softc *adv);
static void adv_init_lram(struct adv_softc *adv);
static int adv_init_microcode_var(struct adv_softc *adv);
static void adv_init_qlink_var(struct adv_softc *adv);
/* Interrupts */
static void adv_disable_interrupt(struct adv_softc *adv);
static void adv_enable_interrupt(struct adv_softc *adv);
static void adv_toggle_irq_act(struct adv_softc *adv);
/* Chip Control */
static int adv_host_req_chip_halt(struct adv_softc *adv);
static void adv_set_chip_ih(struct adv_softc *adv, u_int16_t ins_code);
#if 0
static u_int8_t adv_get_chip_scsi_ctrl(struct adv_softc *adv);
#endif
/* Queue handling and execution */
static __inline int
adv_sgcount_to_qcount(int sgcount);
static __inline int
adv_sgcount_to_qcount(int sgcount)
{
int n_sg_list_qs;
n_sg_list_qs = ((sgcount - 1) / ADV_SG_LIST_PER_Q);
if (((sgcount - 1) % ADV_SG_LIST_PER_Q) != 0)
n_sg_list_qs++;
return (n_sg_list_qs + 1);
}
#if BYTE_ORDER == BIG_ENDIAN
static void adv_adj_endian_qdone_info(struct adv_q_done_info *);
static void adv_adj_scsiq_endian(struct adv_scsi_q *);
#endif
static void adv_get_q_info(struct adv_softc *adv, u_int16_t s_addr,
u_int16_t *inbuf, int words);
static u_int adv_get_num_free_queues(struct adv_softc *adv, u_int8_t n_qs);
static u_int8_t adv_alloc_free_queues(struct adv_softc *adv,
u_int8_t free_q_head, u_int8_t n_free_q);
static u_int8_t adv_alloc_free_queue(struct adv_softc *adv,
u_int8_t free_q_head);
static int adv_send_scsi_queue(struct adv_softc *adv,
struct adv_scsi_q *scsiq,
u_int8_t n_q_required);
static void adv_put_ready_sg_list_queue(struct adv_softc *adv,
struct adv_scsi_q *scsiq,
u_int q_no);
static void adv_put_ready_queue(struct adv_softc *adv,
struct adv_scsi_q *scsiq, u_int q_no);
static void adv_put_scsiq(struct adv_softc *adv, u_int16_t s_addr,
u_int16_t *buffer, int words);
/* Messages */
static void adv_handle_extmsg_in(struct adv_softc *adv,
u_int16_t halt_q_addr, u_int8_t q_cntl,
target_bit_vector target_id,
int tid);
static void adv_msgout_sdtr(struct adv_softc *adv, u_int8_t sdtr_period,
u_int8_t sdtr_offset);
static void adv_set_sdtr_reg_at_id(struct adv_softc *adv, int id,
u_int8_t sdtr_data);
/* Exported functions first */
void
advasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
{
struct adv_softc *adv;
adv = (struct adv_softc *)callback_arg;
mtx_assert(&adv->lock, MA_OWNED);
switch (code) {
case AC_FOUND_DEVICE:
{
struct ccb_getdev *cgd;
target_bit_vector target_mask;
int num_entries;
caddr_t match;
struct adv_quirk_entry *entry;
struct adv_target_transinfo* tinfo;
cgd = (struct ccb_getdev *)arg;
target_mask = ADV_TID_TO_TARGET_MASK(cgd->ccb_h.target_id);
num_entries = sizeof(adv_quirk_table)/sizeof(*adv_quirk_table);
match = cam_quirkmatch((caddr_t)&cgd->inq_data,
(caddr_t)adv_quirk_table,
num_entries, sizeof(*adv_quirk_table),
scsi_inquiry_match);
if (match == NULL)
panic("advasync: device didn't match wildcard entry!!");
entry = (struct adv_quirk_entry *)match;
if (adv->bug_fix_control & ADV_BUG_FIX_ASYN_USE_SYN) {
if ((entry->quirks & ADV_QUIRK_FIX_ASYN_XFER_ALWAYS)!=0)
adv->fix_asyn_xfer_always |= target_mask;
else
adv->fix_asyn_xfer_always &= ~target_mask;
/*
* We start out life with all bits set and clear them
* after we've determined that the fix isn't necessary.
* It may well be that we've already cleared a target
* before the full inquiry session completes, so don't
* gratuitously set a target bit even if it has this
* quirk. But, if the quirk exonerates a device, clear
* the bit now.
*/
if ((entry->quirks & ADV_QUIRK_FIX_ASYN_XFER) == 0)
adv->fix_asyn_xfer &= ~target_mask;
}
/*
* Reset our sync settings now that we've determined
* what quirks are in effect for the device.
*/
tinfo = &adv->tinfo[cgd->ccb_h.target_id];
adv_set_syncrate(adv, cgd->ccb_h.path,
cgd->ccb_h.target_id,
tinfo->current.period,
tinfo->current.offset,
ADV_TRANS_CUR);
break;
}
case AC_LOST_DEVICE:
{
u_int target_mask;
if (adv->bug_fix_control & ADV_BUG_FIX_ASYN_USE_SYN) {
target_mask = 0x01 << xpt_path_target_id(path);
adv->fix_asyn_xfer |= target_mask;
}
/*
* Revert to async transfers
* for the next device.
*/
adv_set_syncrate(adv, /*path*/NULL,
xpt_path_target_id(path),
/*period*/0,
/*offset*/0,
ADV_TRANS_GOAL|ADV_TRANS_CUR);
}
default:
break;
}
}
void
adv_set_bank(struct adv_softc *adv, u_int8_t bank)
{
u_int8_t control;
/*
* Start out with the bank reset to 0
*/
control = ADV_INB(adv, ADV_CHIP_CTRL)
& (~(ADV_CC_SINGLE_STEP | ADV_CC_TEST
| ADV_CC_DIAG | ADV_CC_SCSI_RESET
| ADV_CC_CHIP_RESET | ADV_CC_BANK_ONE));
if (bank == 1) {
control |= ADV_CC_BANK_ONE;
} else if (bank == 2) {
control |= ADV_CC_DIAG | ADV_CC_BANK_ONE;
}
ADV_OUTB(adv, ADV_CHIP_CTRL, control);
}
u_int8_t
adv_read_lram_8(struct adv_softc *adv, u_int16_t addr)
{
u_int8_t byte_data;
u_int16_t word_data;
/*
* LRAM is accessed on 16bit boundaries.
*/
ADV_OUTW(adv, ADV_LRAM_ADDR, addr & 0xFFFE);
word_data = ADV_INW(adv, ADV_LRAM_DATA);
if (addr & 1) {
#if BYTE_ORDER == BIG_ENDIAN
byte_data = (u_int8_t)(word_data & 0xFF);
#else
byte_data = (u_int8_t)((word_data >> 8) & 0xFF);
#endif
} else {
#if BYTE_ORDER == BIG_ENDIAN
byte_data = (u_int8_t)((word_data >> 8) & 0xFF);
#else
byte_data = (u_int8_t)(word_data & 0xFF);
#endif
}
return (byte_data);
}
void
adv_write_lram_8(struct adv_softc *adv, u_int16_t addr, u_int8_t value)
{
u_int16_t word_data;
word_data = adv_read_lram_16(adv, addr & 0xFFFE);
if (addr & 1) {
word_data &= 0x00FF;
word_data |= (((u_int8_t)value << 8) & 0xFF00);
} else {
word_data &= 0xFF00;
word_data |= ((u_int8_t)value & 0x00FF);
}
adv_write_lram_16(adv, addr & 0xFFFE, word_data);
}
u_int16_t
adv_read_lram_16(struct adv_softc *adv, u_int16_t addr)
{
ADV_OUTW(adv, ADV_LRAM_ADDR, addr);
return (ADV_INW(adv, ADV_LRAM_DATA));
}
void
adv_write_lram_16(struct adv_softc *adv, u_int16_t addr, u_int16_t value)
{
ADV_OUTW(adv, ADV_LRAM_ADDR, addr);
ADV_OUTW(adv, ADV_LRAM_DATA, value);
}
/*
* Determine if there is a board at "iobase" by looking
* for the AdvanSys signatures. Return 1 if a board is
* found, 0 otherwise.
*/
int
adv_find_signature(struct resource *res)
{
u_int16_t signature;
if (bus_read_1(res, ADV_SIGNATURE_BYTE) == ADV_1000_ID1B) {
signature = bus_read_2(res, ADV_SIGNATURE_WORD);
if ((signature == ADV_1000_ID0W)
|| (signature == ADV_1000_ID0W_FIX))
return (1);
}
return (0);
}
void
adv_lib_init(struct adv_softc *adv)
{
if ((adv->type & ADV_ULTRA) != 0) {
adv->sdtr_period_tbl = adv_sdtr_period_tbl_ultra;
adv->sdtr_period_tbl_size = sizeof(adv_sdtr_period_tbl_ultra);
} else {
adv->sdtr_period_tbl = adv_sdtr_period_tbl;
adv->sdtr_period_tbl_size = sizeof(adv_sdtr_period_tbl);
}
}
u_int16_t
adv_get_eeprom_config(struct adv_softc *adv, struct
adv_eeprom_config *eeprom_config)
{
u_int16_t sum;
u_int16_t *wbuf;
u_int8_t cfg_beg;
u_int8_t cfg_end;
u_int8_t s_addr;
wbuf = (u_int16_t *)eeprom_config;
sum = 0;
for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
*wbuf = adv_read_eeprom_16(adv, s_addr);
sum += *wbuf;
}
if (adv->type & ADV_VL) {
cfg_beg = ADV_EEPROM_CFG_BEG_VL;
cfg_end = ADV_EEPROM_MAX_ADDR_VL;
} else {
cfg_beg = ADV_EEPROM_CFG_BEG;
cfg_end = ADV_EEPROM_MAX_ADDR;
}
for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
*wbuf = adv_read_eeprom_16(adv, s_addr);
sum += *wbuf;
#ifdef ADV_DEBUG_EEPROM
printf("Addr 0x%x: 0x%04x\n", s_addr, *wbuf);
#endif
}
*wbuf = adv_read_eeprom_16(adv, s_addr);
return (sum);
}
int
adv_set_eeprom_config(struct adv_softc *adv,
struct adv_eeprom_config *eeprom_config)
{
int retry;
retry = 0;
while (1) {
if (adv_set_eeprom_config_once(adv, eeprom_config) == 0) {
break;
}
if (++retry > ADV_EEPROM_MAX_RETRY) {
break;
}
}
return (retry > ADV_EEPROM_MAX_RETRY);
}
int
adv_reset_chip(struct adv_softc *adv, int reset_bus)
{
adv_stop_chip(adv);
ADV_OUTB(adv, ADV_CHIP_CTRL, ADV_CC_CHIP_RESET | ADV_CC_HALT
| (reset_bus ? ADV_CC_SCSI_RESET : 0));
DELAY(60);
adv_set_chip_ih(adv, ADV_INS_RFLAG_WTM);
adv_set_chip_ih(adv, ADV_INS_HALT);
if (reset_bus)
ADV_OUTB(adv, ADV_CHIP_CTRL, ADV_CC_CHIP_RESET | ADV_CC_HALT);
ADV_OUTB(adv, ADV_CHIP_CTRL, ADV_CC_HALT);
if (reset_bus)
DELAY(200 * 1000);
ADV_OUTW(adv, ADV_CHIP_STATUS, ADV_CIW_CLR_SCSI_RESET_INT);
ADV_OUTW(adv, ADV_CHIP_STATUS, 0);
return (adv_is_chip_halted(adv));
}
int
adv_test_external_lram(struct adv_softc* adv)
{
u_int16_t q_addr;
u_int16_t saved_value;
int success;
success = 0;
q_addr = ADV_QNO_TO_QADDR(241);
saved_value = adv_read_lram_16(adv, q_addr);
if (adv_write_and_verify_lram_16(adv, q_addr, 0x55AA) == 0) {
success = 1;
adv_write_lram_16(adv, q_addr, saved_value);
}
return (success);
}
int
adv_init_lram_and_mcode(struct adv_softc *adv)
{
u_int32_t retval;
adv_disable_interrupt(adv);
adv_init_lram(adv);
retval = adv_load_microcode(adv, 0, (u_int16_t *)adv_mcode,
adv_mcode_size);
if (retval != adv_mcode_chksum) {
device_printf(adv->dev,
"Microcode download failed checksum!\n");
return (1);
}
if (adv_init_microcode_var(adv) != 0)
return (1);
adv_enable_interrupt(adv);
return (0);
}
u_int8_t
adv_get_chip_irq(struct adv_softc *adv)
{
u_int16_t cfg_lsw;
u_int8_t chip_irq;
cfg_lsw = ADV_INW(adv, ADV_CONFIG_LSW);
if ((adv->type & ADV_VL) != 0) {
chip_irq = (u_int8_t)(((cfg_lsw >> 2) & 0x07));
if ((chip_irq == 0) ||
(chip_irq == 4) ||
(chip_irq == 7)) {
return (0);
}
return (chip_irq + (ADV_MIN_IRQ_NO - 1));
}
chip_irq = (u_int8_t)(((cfg_lsw >> 2) & 0x03));
if (chip_irq == 3)
chip_irq += 2;
return (chip_irq + ADV_MIN_IRQ_NO);
}
u_int8_t
adv_set_chip_irq(struct adv_softc *adv, u_int8_t irq_no)
{
u_int16_t cfg_lsw;
if ((adv->type & ADV_VL) != 0) {
if (irq_no != 0) {
if ((irq_no < ADV_MIN_IRQ_NO)
|| (irq_no > ADV_MAX_IRQ_NO)) {
irq_no = 0;
} else {
irq_no -= ADV_MIN_IRQ_NO - 1;
}
}
cfg_lsw = ADV_INW(adv, ADV_CONFIG_LSW) & 0xFFE3;
cfg_lsw |= 0x0010;
ADV_OUTW(adv, ADV_CONFIG_LSW, cfg_lsw);
adv_toggle_irq_act(adv);
cfg_lsw = ADV_INW(adv, ADV_CONFIG_LSW) & 0xFFE0;
cfg_lsw |= (irq_no & 0x07) << 2;
ADV_OUTW(adv, ADV_CONFIG_LSW, cfg_lsw);
adv_toggle_irq_act(adv);
} else if ((adv->type & ADV_ISA) != 0) {
if (irq_no == 15)
irq_no -= 2;
irq_no -= ADV_MIN_IRQ_NO;
cfg_lsw = ADV_INW(adv, ADV_CONFIG_LSW) & 0xFFF3;
cfg_lsw |= (irq_no & 0x03) << 2;
ADV_OUTW(adv, ADV_CONFIG_LSW, cfg_lsw);
}
return (adv_get_chip_irq(adv));
}
void
adv_set_chip_scsiid(struct adv_softc *adv, int new_id)
{
u_int16_t cfg_lsw;
cfg_lsw = ADV_INW(adv, ADV_CONFIG_LSW);
if (ADV_CONFIG_SCSIID(cfg_lsw) == new_id)
return;
cfg_lsw &= ~ADV_CFG_LSW_SCSIID;
cfg_lsw |= (new_id & ADV_MAX_TID) << ADV_CFG_LSW_SCSIID_SHIFT;
ADV_OUTW(adv, ADV_CONFIG_LSW, cfg_lsw);
}
int
adv_execute_scsi_queue(struct adv_softc *adv, struct adv_scsi_q *scsiq,
u_int32_t datalen)
{
struct adv_target_transinfo* tinfo;
u_int32_t *p_data_addr;
u_int32_t *p_data_bcount;
int disable_syn_offset_one_fix;
int retval;
u_int n_q_required;
u_int32_t addr;
u_int8_t sg_entry_cnt;
u_int8_t target_ix;
u_int8_t sg_entry_cnt_minus_one;
u_int8_t tid_no;
if (!dumping)
mtx_assert(&adv->lock, MA_OWNED);
scsiq->q1.q_no = 0;
retval = 1; /* Default to error case */
target_ix = scsiq->q2.target_ix;
tid_no = ADV_TIX_TO_TID(target_ix);
tinfo = &adv->tinfo[tid_no];
if (scsiq->cdbptr[0] == REQUEST_SENSE) {
/* Renegotiate if appropriate. */
adv_set_syncrate(adv, /*struct cam_path */NULL,
tid_no, /*period*/0, /*offset*/0,
ADV_TRANS_CUR);
if (tinfo->current.period != tinfo->goal.period) {
adv_msgout_sdtr(adv, tinfo->goal.period,
tinfo->goal.offset);
scsiq->q1.cntl |= (QC_MSG_OUT | QC_URGENT);
}
}
if ((scsiq->q1.cntl & QC_SG_HEAD) != 0) {
sg_entry_cnt = scsiq->sg_head->entry_cnt;
sg_entry_cnt_minus_one = sg_entry_cnt - 1;
#ifdef DIAGNOSTIC
if (sg_entry_cnt <= 1)
panic("adv_execute_scsi_queue: Queue "
"with QC_SG_HEAD set but %d segs.", sg_entry_cnt);
if (sg_entry_cnt > ADV_MAX_SG_LIST)
panic("adv_execute_scsi_queue: "
"Queue with too many segs.");
if ((adv->type & (ADV_ISA | ADV_VL | ADV_EISA)) != 0) {
int i;
for (i = 0; i < sg_entry_cnt_minus_one; i++) {
addr = scsiq->sg_head->sg_list[i].addr +
scsiq->sg_head->sg_list[i].bytes;
if ((addr & 0x0003) != 0)
panic("adv_execute_scsi_queue: SG "
"with odd address or byte count");
}
}
#endif
p_data_addr =
&scsiq->sg_head->sg_list[sg_entry_cnt_minus_one].addr;
p_data_bcount =
&scsiq->sg_head->sg_list[sg_entry_cnt_minus_one].bytes;
n_q_required = adv_sgcount_to_qcount(sg_entry_cnt);
scsiq->sg_head->queue_cnt = n_q_required - 1;
} else {
p_data_addr = &scsiq->q1.data_addr;
p_data_bcount = &scsiq->q1.data_cnt;
n_q_required = 1;
}
disable_syn_offset_one_fix = FALSE;
if ((adv->fix_asyn_xfer & scsiq->q1.target_id) != 0
&& (adv->fix_asyn_xfer_always & scsiq->q1.target_id) == 0) {
if (datalen != 0) {
if (datalen < 512) {
disable_syn_offset_one_fix = TRUE;
} else {
if (scsiq->cdbptr[0] == INQUIRY
|| scsiq->cdbptr[0] == REQUEST_SENSE
|| scsiq->cdbptr[0] == READ_CAPACITY
|| scsiq->cdbptr[0] == MODE_SELECT_6
|| scsiq->cdbptr[0] == MODE_SENSE_6
|| scsiq->cdbptr[0] == MODE_SENSE_10
|| scsiq->cdbptr[0] == MODE_SELECT_10
|| scsiq->cdbptr[0] == READ_TOC) {
disable_syn_offset_one_fix = TRUE;
}
}
}
}
if (disable_syn_offset_one_fix) {
scsiq->q2.tag_code &=
~(MSG_SIMPLE_Q_TAG|MSG_HEAD_OF_Q_TAG|MSG_ORDERED_Q_TAG);
scsiq->q2.tag_code |= (ADV_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX
| ADV_TAG_FLAG_DISABLE_DISCONNECT);
}
if ((adv->bug_fix_control & ADV_BUG_FIX_IF_NOT_DWB) != 0
&& (scsiq->cdbptr[0] == READ_10 || scsiq->cdbptr[0] == READ_6)) {
u_int8_t extra_bytes;
addr = *p_data_addr + *p_data_bcount;
extra_bytes = addr & 0x0003;
if (extra_bytes != 0
&& ((scsiq->q1.cntl & QC_SG_HEAD) != 0
|| (scsiq->q1.data_cnt & 0x01FF) == 0)) {
scsiq->q2.tag_code |= ADV_TAG_FLAG_EXTRA_BYTES;
scsiq->q1.extra_bytes = extra_bytes;
*p_data_bcount -= extra_bytes;
}
}
if ((adv_get_num_free_queues(adv, n_q_required) >= n_q_required)
|| ((scsiq->q1.cntl & QC_URGENT) != 0))
retval = adv_send_scsi_queue(adv, scsiq, n_q_required);
return (retval);
}
u_int8_t
adv_copy_lram_doneq(struct adv_softc *adv, u_int16_t q_addr,
struct adv_q_done_info *scsiq, u_int32_t max_dma_count)
{
u_int16_t val;
u_int8_t sg_queue_cnt;
adv_get_q_info(adv, q_addr + ADV_SCSIQ_DONE_INFO_BEG,
(u_int16_t *)scsiq,
(sizeof(scsiq->d2) + sizeof(scsiq->d3)) / 2);
#if BYTE_ORDER == BIG_ENDIAN
adv_adj_endian_qdone_info(scsiq);
#endif
val = adv_read_lram_16(adv, q_addr + ADV_SCSIQ_B_STATUS);
scsiq->q_status = val & 0xFF;
scsiq->q_no = (val >> 8) & 0XFF;
val = adv_read_lram_16(adv, q_addr + ADV_SCSIQ_B_CNTL);
scsiq->cntl = val & 0xFF;
sg_queue_cnt = (val >> 8) & 0xFF;
val = adv_read_lram_16(adv,q_addr + ADV_SCSIQ_B_SENSE_LEN);
scsiq->sense_len = val & 0xFF;
scsiq->extra_bytes = (val >> 8) & 0xFF;
/*
* Due to a bug in accessing LRAM on the 940UA, the residual
* is split into separate high and low 16bit quantities.
*/
scsiq->remain_bytes =
adv_read_lram_16(adv, q_addr + ADV_SCSIQ_DW_REMAIN_XFER_CNT);
scsiq->remain_bytes |=
adv_read_lram_16(adv, q_addr + ADV_SCSIQ_W_ALT_DC1) << 16;
/*
* XXX Is this just a safeguard or will the counter really
* have bogus upper bits?
*/
scsiq->remain_bytes &= max_dma_count;
return (sg_queue_cnt);
}
int
adv_start_chip(struct adv_softc *adv)
{
ADV_OUTB(adv, ADV_CHIP_CTRL, 0);
if ((ADV_INW(adv, ADV_CHIP_STATUS) & ADV_CSW_HALTED) != 0)
return (0);
return (1);
}
int
adv_stop_execution(struct adv_softc *adv)
{
int count;
count = 0;
if (adv_read_lram_8(adv, ADV_STOP_CODE_B) == 0) {
adv_write_lram_8(adv, ADV_STOP_CODE_B,
ADV_STOP_REQ_RISC_STOP);
do {
if (adv_read_lram_8(adv, ADV_STOP_CODE_B) &
ADV_STOP_ACK_RISC_STOP) {
return (1);
}
DELAY(1000);
} while (count++ < 20);
}
return (0);
}
int
adv_is_chip_halted(struct adv_softc *adv)
{
if ((ADV_INW(adv, ADV_CHIP_STATUS) & ADV_CSW_HALTED) != 0) {
if ((ADV_INB(adv, ADV_CHIP_CTRL) & ADV_CC_HALT) != 0) {
return (1);
}
}
return (0);
}
/*
* XXX The numeric constants and the loops in this routine
* need to be documented.
*/
void
adv_ack_interrupt(struct adv_softc *adv)
{
u_int8_t host_flag;
u_int8_t risc_flag;
int loop;
loop = 0;
do {
risc_flag = adv_read_lram_8(adv, ADVV_RISC_FLAG_B);
if (loop++ > 0x7FFF) {
break;
}
} while ((risc_flag & ADV_RISC_FLAG_GEN_INT) != 0);
host_flag = adv_read_lram_8(adv, ADVV_HOST_FLAG_B);
adv_write_lram_8(adv, ADVV_HOST_FLAG_B,
host_flag | ADV_HOST_FLAG_ACK_INT);
ADV_OUTW(adv, ADV_CHIP_STATUS, ADV_CIW_INT_ACK);
loop = 0;
while (ADV_INW(adv, ADV_CHIP_STATUS) & ADV_CSW_INT_PENDING) {
ADV_OUTW(adv, ADV_CHIP_STATUS, ADV_CIW_INT_ACK);
if (loop++ > 3) {
break;
}
}
adv_write_lram_8(adv, ADVV_HOST_FLAG_B, host_flag);
}
/*
* Handle all conditions that may halt the chip waiting
* for us to intervene.
*/
void
adv_isr_chip_halted(struct adv_softc *adv)
{
u_int16_t int_halt_code;
u_int16_t halt_q_addr;
target_bit_vector target_mask;
target_bit_vector scsi_busy;
u_int8_t halt_qp;
u_int8_t target_ix;
u_int8_t q_cntl;
u_int8_t tid_no;
if (!dumping)
mtx_assert(&adv->lock, MA_OWNED);
int_halt_code = adv_read_lram_16(adv, ADVV_HALTCODE_W);
halt_qp = adv_read_lram_8(adv, ADVV_CURCDB_B);
halt_q_addr = ADV_QNO_TO_QADDR(halt_qp);
target_ix = adv_read_lram_8(adv, halt_q_addr + ADV_SCSIQ_B_TARGET_IX);
q_cntl = adv_read_lram_8(adv, halt_q_addr + ADV_SCSIQ_B_CNTL);
tid_no = ADV_TIX_TO_TID(target_ix);
target_mask = ADV_TID_TO_TARGET_MASK(tid_no);
if (int_halt_code == ADV_HALT_DISABLE_ASYN_USE_SYN_FIX) {
/*
* Temporarily disable the async fix by removing
* this target from the list of affected targets,
* setting our async rate, and then putting us
* back into the mask.
*/
adv->fix_asyn_xfer &= ~target_mask;
adv_set_syncrate(adv, /*struct cam_path */NULL,
tid_no, /*period*/0, /*offset*/0,
ADV_TRANS_ACTIVE);
adv->fix_asyn_xfer |= target_mask;
} else if (int_halt_code == ADV_HALT_ENABLE_ASYN_USE_SYN_FIX) {
adv_set_syncrate(adv, /*struct cam_path */NULL,
tid_no, /*period*/0, /*offset*/0,
ADV_TRANS_ACTIVE);
} else if (int_halt_code == ADV_HALT_EXTMSG_IN) {
adv_handle_extmsg_in(adv, halt_q_addr, q_cntl,
target_mask, tid_no);
} else if (int_halt_code == ADV_HALT_CHK_CONDITION) {
struct adv_target_transinfo* tinfo;
struct adv_ccb_info *cinfo;
union ccb *ccb;
u_int32_t cinfo_index;
u_int8_t tag_code;
u_int8_t q_status;
tinfo = &adv->tinfo[tid_no];
q_cntl |= QC_REQ_SENSE;
/* Renegotiate if appropriate. */
adv_set_syncrate(adv, /*struct cam_path */NULL,
tid_no, /*period*/0, /*offset*/0,
ADV_TRANS_CUR);
if (tinfo->current.period != tinfo->goal.period) {
adv_msgout_sdtr(adv, tinfo->goal.period,
tinfo->goal.offset);
q_cntl |= QC_MSG_OUT;
}
adv_write_lram_8(adv, halt_q_addr + ADV_SCSIQ_B_CNTL, q_cntl);
/* Don't tag request sense commands */
tag_code = adv_read_lram_8(adv,
halt_q_addr + ADV_SCSIQ_B_TAG_CODE);
tag_code &=
~(MSG_SIMPLE_Q_TAG|MSG_HEAD_OF_Q_TAG|MSG_ORDERED_Q_TAG);
if ((adv->fix_asyn_xfer & target_mask) != 0
&& (adv->fix_asyn_xfer_always & target_mask) == 0) {
tag_code |= (ADV_TAG_FLAG_DISABLE_DISCONNECT
| ADV_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX);
}
adv_write_lram_8(adv, halt_q_addr + ADV_SCSIQ_B_TAG_CODE,
tag_code);
q_status = adv_read_lram_8(adv,
halt_q_addr + ADV_SCSIQ_B_STATUS);
q_status |= (QS_READY | QS_BUSY);
adv_write_lram_8(adv, halt_q_addr + ADV_SCSIQ_B_STATUS,
q_status);
/*
* Freeze the devq until we can handle the sense condition.
*/
cinfo_index =
adv_read_lram_32(adv, halt_q_addr + ADV_SCSIQ_D_CINFO_IDX);
cinfo = &adv->ccb_infos[cinfo_index];
ccb = adv->ccb_infos[cinfo_index].ccb;
xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
ccb->ccb_h.status |= CAM_DEV_QFRZN;
adv_abort_ccb(adv, tid_no, ADV_TIX_TO_LUN(target_ix),
/*ccb*/NULL, CAM_REQUEUE_REQ,
/*queued_only*/TRUE);
scsi_busy = adv_read_lram_8(adv, ADVV_SCSIBUSY_B);
scsi_busy &= ~target_mask;
adv_write_lram_8(adv, ADVV_SCSIBUSY_B, scsi_busy);
/*
* Ensure we have enough time to actually
* retrieve the sense.
*/
callout_reset(&cinfo->timer, 5 * hz, adv_timeout, ccb);
} else if (int_halt_code == ADV_HALT_SDTR_REJECTED) {
struct ext_msg out_msg;
adv_read_lram_16_multi(adv, ADVV_MSGOUT_BEG,
(u_int16_t *) &out_msg,
sizeof(out_msg)/2);
if ((out_msg.msg_type == MSG_EXTENDED)
&& (out_msg.msg_len == MSG_EXT_SDTR_LEN)
&& (out_msg.msg_req == MSG_EXT_SDTR)) {
/* Revert to Async */
adv_set_syncrate(adv, /*struct cam_path */NULL,
tid_no, /*period*/0, /*offset*/0,
ADV_TRANS_GOAL|ADV_TRANS_ACTIVE);
}
q_cntl &= ~QC_MSG_OUT;
adv_write_lram_8(adv, halt_q_addr + ADV_SCSIQ_B_CNTL, q_cntl);
} else if (int_halt_code == ADV_HALT_SS_QUEUE_FULL) {
union ccb *ccb;
u_int32_t cinfo_index;
adv_read_lram_8(adv, halt_q_addr + ADV_SCSIQ_SCSI_STATUS);
cinfo_index =
adv_read_lram_32(adv, halt_q_addr + ADV_SCSIQ_D_CINFO_IDX);
ccb = adv->ccb_infos[cinfo_index].ccb;
xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
ccb->ccb_h.status |= CAM_DEV_QFRZN|CAM_SCSI_STATUS_ERROR;
ccb->csio.scsi_status = SCSI_STATUS_QUEUE_FULL;
adv_abort_ccb(adv, tid_no, ADV_TIX_TO_LUN(target_ix),
/*ccb*/NULL, CAM_REQUEUE_REQ,
/*queued_only*/TRUE);
scsi_busy = adv_read_lram_8(adv, ADVV_SCSIBUSY_B);
scsi_busy &= ~target_mask;
adv_write_lram_8(adv, ADVV_SCSIBUSY_B, scsi_busy);
} else {
printf("Unhandled Halt Code %x\n", int_halt_code);
}
adv_write_lram_16(adv, ADVV_HALTCODE_W, 0);
}
void
adv_sdtr_to_period_offset(struct adv_softc *adv,
u_int8_t sync_data, u_int8_t *period,
u_int8_t *offset, int tid)
{
if (adv->fix_asyn_xfer & ADV_TID_TO_TARGET_MASK(tid)
&& (sync_data == ASYN_SDTR_DATA_FIX_PCI_REV_AB)) {
*period = *offset = 0;
} else {
*period = adv->sdtr_period_tbl[((sync_data >> 4) & 0xF)];
*offset = sync_data & 0xF;
}
}
void
adv_set_syncrate(struct adv_softc *adv, struct cam_path *path,
u_int tid, u_int period, u_int offset, u_int type)
{
struct adv_target_transinfo* tinfo;
u_int old_period;
u_int old_offset;
u_int8_t sdtr_data;
mtx_assert(&adv->lock, MA_OWNED);
tinfo = &adv->tinfo[tid];
/* Filter our input */
sdtr_data = adv_period_offset_to_sdtr(adv, &period,
&offset, tid);
old_period = tinfo->current.period;
old_offset = tinfo->current.offset;
if ((type & ADV_TRANS_CUR) != 0
&& ((old_period != period || old_offset != offset)
|| period == 0 || offset == 0) /*Changes in asyn fix settings*/) {
int halted;
halted = adv_is_chip_halted(adv);
if (halted == 0)
/* Must halt the chip first */
adv_host_req_chip_halt(adv);
/* Update current hardware settings */
adv_set_sdtr_reg_at_id(adv, tid, sdtr_data);
/*
* If a target can run in sync mode, we don't need
* to check it for sync problems.
*/
if (offset != 0)
adv->fix_asyn_xfer &= ~ADV_TID_TO_TARGET_MASK(tid);
if (halted == 0)
/* Start the chip again */
adv_start_chip(adv);
tinfo->current.period = period;
tinfo->current.offset = offset;
if (path != NULL) {
/*
* Tell the SCSI layer about the
* new transfer parameters.
*/
struct ccb_trans_settings neg;
memset(&neg, 0, sizeof (neg));
struct ccb_trans_settings_spi *spi =
&neg.xport_specific.spi;
neg.protocol = PROTO_SCSI;
neg.protocol_version = SCSI_REV_2;
neg.transport = XPORT_SPI;
neg.transport_version = 2;
spi->sync_offset = offset;
spi->sync_period = period;
spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
spi->valid |= CTS_SPI_VALID_SYNC_RATE;
xpt_setup_ccb(&neg.ccb_h, path, /*priority*/1);
xpt_async(AC_TRANSFER_NEG, path, &neg);
}
}
if ((type & ADV_TRANS_GOAL) != 0) {
tinfo->goal.period = period;
tinfo->goal.offset = offset;
}
if ((type & ADV_TRANS_USER) != 0) {
tinfo->user.period = period;
tinfo->user.offset = offset;
}
}
u_int8_t
adv_period_offset_to_sdtr(struct adv_softc *adv, u_int *period,
u_int *offset, int tid)
{
u_int i;
u_int dummy_offset;
u_int dummy_period;
if (offset == NULL) {
dummy_offset = 0;
offset = &dummy_offset;
}
if (period == NULL) {
dummy_period = 0;
period = &dummy_period;
}
*offset = MIN(ADV_SYN_MAX_OFFSET, *offset);
if (*period != 0 && *offset != 0) {
for (i = 0; i < adv->sdtr_period_tbl_size; i++) {
if (*period <= adv->sdtr_period_tbl[i]) {
/*
* When responding to a target that requests
* sync, the requested rate may fall between
* two rates that we can output, but still be
* a rate that we can receive. Because of this,
* we want to respond to the target with
* the same rate that it sent to us even
* if the period we use to send data to it
* is lower. Only lower the response period
* if we must.
*/
if (i == 0 /* Our maximum rate */)
*period = adv->sdtr_period_tbl[0];
return ((i << 4) | *offset);
}
}
}
/* Must go async */
*period = 0;
*offset = 0;
if (adv->fix_asyn_xfer & ADV_TID_TO_TARGET_MASK(tid))
return (ASYN_SDTR_DATA_FIX_PCI_REV_AB);
return (0);
}
/* Internal Routines */
static void
adv_read_lram_16_multi(struct adv_softc *adv, u_int16_t s_addr,
u_int16_t *buffer, int count)
{
ADV_OUTW(adv, ADV_LRAM_ADDR, s_addr);
ADV_INSW(adv, ADV_LRAM_DATA, buffer, count);
}
static void
adv_write_lram_16_multi(struct adv_softc *adv, u_int16_t s_addr,
u_int16_t *buffer, int count)
{
ADV_OUTW(adv, ADV_LRAM_ADDR, s_addr);
ADV_OUTSW(adv, ADV_LRAM_DATA, buffer, count);
}
static void
adv_mset_lram_16(struct adv_softc *adv, u_int16_t s_addr,
u_int16_t set_value, int count)
{
ADV_OUTW(adv, ADV_LRAM_ADDR, s_addr);
bus_set_multi_2(adv->res, adv->reg_off + ADV_LRAM_DATA,
set_value, count);
}
static u_int32_t
adv_msum_lram_16(struct adv_softc *adv, u_int16_t s_addr, int count)
{
u_int32_t sum;
int i;
sum = 0;
ADV_OUTW(adv, ADV_LRAM_ADDR, s_addr);
for (i = 0; i < count; i++)
sum += ADV_INW(adv, ADV_LRAM_DATA);
return (sum);
}
static int
adv_write_and_verify_lram_16(struct adv_softc *adv, u_int16_t addr,
u_int16_t value)
{
int retval;
retval = 0;
ADV_OUTW(adv, ADV_LRAM_ADDR, addr);
ADV_OUTW(adv, ADV_LRAM_DATA, value);
DELAY(10000);
ADV_OUTW(adv, ADV_LRAM_ADDR, addr);
if (value != ADV_INW(adv, ADV_LRAM_DATA))
retval = 1;
return (retval);
}
static u_int32_t
adv_read_lram_32(struct adv_softc *adv, u_int16_t addr)
{
u_int16_t val_low, val_high;
ADV_OUTW(adv, ADV_LRAM_ADDR, addr);
#if BYTE_ORDER == BIG_ENDIAN
val_high = ADV_INW(adv, ADV_LRAM_DATA);
val_low = ADV_INW(adv, ADV_LRAM_DATA);
#else
val_low = ADV_INW(adv, ADV_LRAM_DATA);
val_high = ADV_INW(adv, ADV_LRAM_DATA);
#endif
return (((u_int32_t)val_high << 16) | (u_int32_t)val_low);
}
static void
adv_write_lram_32(struct adv_softc *adv, u_int16_t addr, u_int32_t value)
{
ADV_OUTW(adv, ADV_LRAM_ADDR, addr);
#if BYTE_ORDER == BIG_ENDIAN
ADV_OUTW(adv, ADV_LRAM_DATA, (u_int16_t)((value >> 16) & 0xFFFF));
ADV_OUTW(adv, ADV_LRAM_DATA, (u_int16_t)(value & 0xFFFF));
#else
ADV_OUTW(adv, ADV_LRAM_DATA, (u_int16_t)(value & 0xFFFF));
ADV_OUTW(adv, ADV_LRAM_DATA, (u_int16_t)((value >> 16) & 0xFFFF));
#endif
}
static void
adv_write_lram_32_multi(struct adv_softc *adv, u_int16_t s_addr,
u_int32_t *buffer, int count)
{
ADV_OUTW(adv, ADV_LRAM_ADDR, s_addr);
ADV_OUTSW(adv, ADV_LRAM_DATA, (u_int16_t *)buffer, count * 2);
}
static u_int16_t
adv_read_eeprom_16(struct adv_softc *adv, u_int8_t addr)
{
u_int16_t read_wval;
u_int8_t cmd_reg;
adv_write_eeprom_cmd_reg(adv, ADV_EEPROM_CMD_WRITE_DISABLE);
DELAY(1000);
cmd_reg = addr | ADV_EEPROM_CMD_READ;
adv_write_eeprom_cmd_reg(adv, cmd_reg);
DELAY(1000);
read_wval = ADV_INW(adv, ADV_EEPROM_DATA);
DELAY(1000);
return (read_wval);
}
static u_int16_t
adv_write_eeprom_16(struct adv_softc *adv, u_int8_t addr, u_int16_t value)
{
u_int16_t read_value;
read_value = adv_read_eeprom_16(adv, addr);
if (read_value != value) {
adv_write_eeprom_cmd_reg(adv, ADV_EEPROM_CMD_WRITE_ENABLE);
DELAY(1000);
ADV_OUTW(adv, ADV_EEPROM_DATA, value);
DELAY(1000);
adv_write_eeprom_cmd_reg(adv, ADV_EEPROM_CMD_WRITE | addr);
DELAY(20 * 1000);
adv_write_eeprom_cmd_reg(adv, ADV_EEPROM_CMD_WRITE_DISABLE);
DELAY(1000);
read_value = adv_read_eeprom_16(adv, addr);
}
return (read_value);
}
static int
adv_write_eeprom_cmd_reg(struct adv_softc *adv, u_int8_t cmd_reg)
{
u_int8_t read_back;
int retry;
retry = 0;
while (1) {
ADV_OUTB(adv, ADV_EEPROM_CMD, cmd_reg);
DELAY(1000);
read_back = ADV_INB(adv, ADV_EEPROM_CMD);
if (read_back == cmd_reg) {
return (1);
}
if (retry++ > ADV_EEPROM_MAX_RETRY) {
return (0);
}
}
}
static int
adv_set_eeprom_config_once(struct adv_softc *adv,
struct adv_eeprom_config *eeprom_config)
{
int n_error;
u_int16_t *wbuf;
u_int16_t sum;
u_int8_t s_addr;
u_int8_t cfg_beg;
u_int8_t cfg_end;
wbuf = (u_int16_t *)eeprom_config;
n_error = 0;
sum = 0;
for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
sum += *wbuf;
if (*wbuf != adv_write_eeprom_16(adv, s_addr, *wbuf)) {
n_error++;
}
}
if (adv->type & ADV_VL) {
cfg_beg = ADV_EEPROM_CFG_BEG_VL;
cfg_end = ADV_EEPROM_MAX_ADDR_VL;
} else {
cfg_beg = ADV_EEPROM_CFG_BEG;
cfg_end = ADV_EEPROM_MAX_ADDR;
}
for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
sum += *wbuf;
if (*wbuf != adv_write_eeprom_16(adv, s_addr, *wbuf)) {
n_error++;
}
}
*wbuf = sum;
if (sum != adv_write_eeprom_16(adv, s_addr, sum)) {
n_error++;
}
wbuf = (u_int16_t *)eeprom_config;
for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
if (*wbuf != adv_read_eeprom_16(adv, s_addr)) {
n_error++;
}
}
for (s_addr = cfg_beg; s_addr <= cfg_end; s_addr++, wbuf++) {
if (*wbuf != adv_read_eeprom_16(adv, s_addr)) {
n_error++;
}
}
return (n_error);
}
static u_int32_t
adv_load_microcode(struct adv_softc *adv, u_int16_t s_addr,
u_int16_t *mcode_buf, u_int16_t mcode_size)
{
u_int32_t chksum;
u_int16_t mcode_lram_size;
u_int16_t mcode_chksum;
mcode_lram_size = mcode_size >> 1;
/* XXX Why zero the memory just before you write the whole thing?? */
adv_mset_lram_16(adv, s_addr, 0, mcode_lram_size);
adv_write_lram_16_multi(adv, s_addr, mcode_buf, mcode_lram_size);
chksum = adv_msum_lram_16(adv, s_addr, mcode_lram_size);
mcode_chksum = (u_int16_t)adv_msum_lram_16(adv, ADV_CODE_SEC_BEG,
((mcode_size - s_addr
- ADV_CODE_SEC_BEG) >> 1));
adv_write_lram_16(adv, ADVV_MCODE_CHKSUM_W, mcode_chksum);
adv_write_lram_16(adv, ADVV_MCODE_SIZE_W, mcode_size);
return (chksum);
}
static void
adv_reinit_lram(struct adv_softc *adv) {
adv_init_lram(adv);
adv_init_qlink_var(adv);
}
static void
adv_init_lram(struct adv_softc *adv)
{
u_int8_t i;
u_int16_t s_addr;
adv_mset_lram_16(adv, ADV_QADR_BEG, 0,
(((adv->max_openings + 2 + 1) * 64) >> 1));
i = ADV_MIN_ACTIVE_QNO;
s_addr = ADV_QADR_BEG + ADV_QBLK_SIZE;
adv_write_lram_8(adv, s_addr + ADV_SCSIQ_B_FWD, i + 1);
adv_write_lram_8(adv, s_addr + ADV_SCSIQ_B_BWD, adv->max_openings);
adv_write_lram_8(adv, s_addr + ADV_SCSIQ_B_QNO, i);
i++;
s_addr += ADV_QBLK_SIZE;
for (; i < adv->max_openings; i++, s_addr += ADV_QBLK_SIZE) {
adv_write_lram_8(adv, s_addr + ADV_SCSIQ_B_FWD, i + 1);
adv_write_lram_8(adv, s_addr + ADV_SCSIQ_B_BWD, i - 1);
adv_write_lram_8(adv, s_addr + ADV_SCSIQ_B_QNO, i);
}
adv_write_lram_8(adv, s_addr + ADV_SCSIQ_B_FWD, ADV_QLINK_END);
adv_write_lram_8(adv, s_addr + ADV_SCSIQ_B_BWD, adv->max_openings - 1);
adv_write_lram_8(adv, s_addr + ADV_SCSIQ_B_QNO, adv->max_openings);
i++;
s_addr += ADV_QBLK_SIZE;
for (; i <= adv->max_openings + 3; i++, s_addr += ADV_QBLK_SIZE) {
adv_write_lram_8(adv, s_addr + ADV_SCSIQ_B_FWD, i);
adv_write_lram_8(adv, s_addr + ADV_SCSIQ_B_BWD, i);
adv_write_lram_8(adv, s_addr + ADV_SCSIQ_B_QNO, i);
}
}
static int
adv_init_microcode_var(struct adv_softc *adv)
{
int i;
for (i = 0; i <= ADV_MAX_TID; i++) {
/* Start out async all around */
adv_set_syncrate(adv, /*path*/NULL,
i, 0, 0,
ADV_TRANS_GOAL|ADV_TRANS_CUR);
}
adv_init_qlink_var(adv);
adv_write_lram_8(adv, ADVV_DISC_ENABLE_B, adv->disc_enable);
adv_write_lram_8(adv, ADVV_HOSTSCSI_ID_B, 0x01 << adv->scsi_id);
adv_write_lram_32(adv, ADVV_OVERRUN_PADDR_D, adv->overrun_physbase);
adv_write_lram_32(adv, ADVV_OVERRUN_BSIZE_D, ADV_OVERRUN_BSIZE);
ADV_OUTW(adv, ADV_REG_PROG_COUNTER, ADV_MCODE_START_ADDR);
if (ADV_INW(adv, ADV_REG_PROG_COUNTER) != ADV_MCODE_START_ADDR) {
device_printf(adv->dev,
"Unable to set program counter. Aborting.\n");
return (1);
}
return (0);
}
static void
adv_init_qlink_var(struct adv_softc *adv)
{
int i;
u_int16_t lram_addr;
adv_write_lram_8(adv, ADVV_NEXTRDY_B, 1);
adv_write_lram_8(adv, ADVV_DONENEXT_B, adv->max_openings);
adv_write_lram_16(adv, ADVV_FREE_Q_HEAD_W, 1);
adv_write_lram_16(adv, ADVV_DONE_Q_TAIL_W, adv->max_openings);
adv_write_lram_8(adv, ADVV_BUSY_QHEAD_B,
(u_int8_t)((int) adv->max_openings + 1));
adv_write_lram_8(adv, ADVV_DISC1_QHEAD_B,
(u_int8_t)((int) adv->max_openings + 2));
adv_write_lram_8(adv, ADVV_TOTAL_READY_Q_B, adv->max_openings);
adv_write_lram_16(adv, ADVV_ASCDVC_ERR_CODE_W, 0);
adv_write_lram_16(adv, ADVV_HALTCODE_W, 0);
adv_write_lram_8(adv, ADVV_STOP_CODE_B, 0);
adv_write_lram_8(adv, ADVV_SCSIBUSY_B, 0);
adv_write_lram_8(adv, ADVV_WTM_FLAG_B, 0);
adv_write_lram_8(adv, ADVV_Q_DONE_IN_PROGRESS_B, 0);
lram_addr = ADV_QADR_BEG;
for (i = 0; i < 32; i++, lram_addr += 2)
adv_write_lram_16(adv, lram_addr, 0);
}
static void
adv_disable_interrupt(struct adv_softc *adv)
{
u_int16_t cfg;
cfg = ADV_INW(adv, ADV_CONFIG_LSW);
ADV_OUTW(adv, ADV_CONFIG_LSW, cfg & ~ADV_CFG_LSW_HOST_INT_ON);
}
static void
adv_enable_interrupt(struct adv_softc *adv)
{
u_int16_t cfg;
cfg = ADV_INW(adv, ADV_CONFIG_LSW);
ADV_OUTW(adv, ADV_CONFIG_LSW, cfg | ADV_CFG_LSW_HOST_INT_ON);
}
static void
adv_toggle_irq_act(struct adv_softc *adv)
{
ADV_OUTW(adv, ADV_CHIP_STATUS, ADV_CIW_IRQ_ACT);
ADV_OUTW(adv, ADV_CHIP_STATUS, 0);
}
void
adv_start_execution(struct adv_softc *adv)
{
if (adv_read_lram_8(adv, ADV_STOP_CODE_B) != 0) {
adv_write_lram_8(adv, ADV_STOP_CODE_B, 0);
}
}
int
adv_stop_chip(struct adv_softc *adv)
{
u_int8_t cc_val;
cc_val = ADV_INB(adv, ADV_CHIP_CTRL)
& (~(ADV_CC_SINGLE_STEP | ADV_CC_TEST | ADV_CC_DIAG));
ADV_OUTB(adv, ADV_CHIP_CTRL, cc_val | ADV_CC_HALT);
adv_set_chip_ih(adv, ADV_INS_HALT);
adv_set_chip_ih(adv, ADV_INS_RFLAG_WTM);
if ((ADV_INW(adv, ADV_CHIP_STATUS) & ADV_CSW_HALTED) == 0) {
return (0);
}
return (1);
}
static int
adv_host_req_chip_halt(struct adv_softc *adv)
{
int count;
u_int8_t saved_stop_code;
if (adv_is_chip_halted(adv))
return (1);
count = 0;
saved_stop_code = adv_read_lram_8(adv, ADVV_STOP_CODE_B);
adv_write_lram_8(adv, ADVV_STOP_CODE_B,
ADV_STOP_HOST_REQ_RISC_HALT | ADV_STOP_REQ_RISC_STOP);
while (adv_is_chip_halted(adv) == 0
&& count++ < 2000)
;
adv_write_lram_8(adv, ADVV_STOP_CODE_B, saved_stop_code);
return (count < 2000);
}
static void
adv_set_chip_ih(struct adv_softc *adv, u_int16_t ins_code)
{
adv_set_bank(adv, 1);
ADV_OUTW(adv, ADV_REG_IH, ins_code);
adv_set_bank(adv, 0);
}
#if 0
static u_int8_t
adv_get_chip_scsi_ctrl(struct adv_softc *adv)
{
u_int8_t scsi_ctrl;
adv_set_bank(adv, 1);
scsi_ctrl = ADV_INB(adv, ADV_REG_SC);
adv_set_bank(adv, 0);
return (scsi_ctrl);
}
#endif
/*
* XXX Looks like more padding issues in this routine as well.
* There has to be a way to turn this into an insw.
*/
static void
adv_get_q_info(struct adv_softc *adv, u_int16_t s_addr,
u_int16_t *inbuf, int words)
{
int i;
ADV_OUTW(adv, ADV_LRAM_ADDR, s_addr);
for (i = 0; i < words; i++, inbuf++) {
if (i == 5) {
continue;
}
*inbuf = ADV_INW(adv, ADV_LRAM_DATA);
}
}
static u_int
adv_get_num_free_queues(struct adv_softc *adv, u_int8_t n_qs)
{
u_int cur_used_qs;
u_int cur_free_qs;
cur_used_qs = adv->cur_active + ADV_MIN_FREE_Q;
if ((cur_used_qs + n_qs) <= adv->max_openings) {
cur_free_qs = adv->max_openings - cur_used_qs;
return (cur_free_qs);
}
adv->openings_needed = n_qs;
return (0);
}
static u_int8_t
adv_alloc_free_queues(struct adv_softc *adv, u_int8_t free_q_head,
u_int8_t n_free_q)
{
int i;
for (i = 0; i < n_free_q; i++) {
free_q_head = adv_alloc_free_queue(adv, free_q_head);
if (free_q_head == ADV_QLINK_END)
break;
}
return (free_q_head);
}
static u_int8_t
adv_alloc_free_queue(struct adv_softc *adv, u_int8_t free_q_head)
{
u_int16_t q_addr;
u_int8_t next_qp;
u_int8_t q_status;
next_qp = ADV_QLINK_END;
q_addr = ADV_QNO_TO_QADDR(free_q_head);
q_status = adv_read_lram_8(adv, q_addr + ADV_SCSIQ_B_STATUS);
if ((q_status & QS_READY) == 0)
next_qp = adv_read_lram_8(adv, q_addr + ADV_SCSIQ_B_FWD);
return (next_qp);
}
static int
adv_send_scsi_queue(struct adv_softc *adv, struct adv_scsi_q *scsiq,
u_int8_t n_q_required)
{
u_int8_t free_q_head;
u_int8_t next_qp;
int retval;
retval = 1;
free_q_head = adv_read_lram_16(adv, ADVV_FREE_Q_HEAD_W) & 0xFF;
if ((next_qp = adv_alloc_free_queues(adv, free_q_head, n_q_required))
!= ADV_QLINK_END) {
scsiq->q1.q_no = free_q_head;
/*
* Now that we know our Q number, point our sense
* buffer pointer to a bus dma mapped area where
* we can dma the data to.
*/
scsiq->q1.sense_addr = adv->sense_physbase
+ ((free_q_head - 1) * sizeof(struct scsi_sense_data));
adv_put_ready_sg_list_queue(adv, scsiq, free_q_head);
adv_write_lram_16(adv, ADVV_FREE_Q_HEAD_W, next_qp);
adv->cur_active += n_q_required;
retval = 0;
}
return (retval);
}
static void
adv_put_ready_sg_list_queue(struct adv_softc *adv, struct adv_scsi_q *scsiq,
u_int q_no)
{
u_int8_t sg_list_dwords;
u_int8_t sg_index, i;
u_int8_t sg_entry_cnt;
u_int8_t next_qp;
u_int16_t q_addr;
struct adv_sg_head *sg_head;
struct adv_sg_list_q scsi_sg_q;
sg_head = scsiq->sg_head;
if (sg_head) {
sg_entry_cnt = sg_head->entry_cnt - 1;
#ifdef DIAGNOSTIC
if (sg_entry_cnt == 0)
panic("adv_put_ready_sg_list_queue: ScsiQ with "
"a SG list but only one element");
if ((scsiq->q1.cntl & QC_SG_HEAD) == 0)
panic("adv_put_ready_sg_list_queue: ScsiQ with "
"a SG list but QC_SG_HEAD not set");
#endif
q_addr = ADV_QNO_TO_QADDR(q_no);
sg_index = 1;
scsiq->q1.sg_queue_cnt = sg_head->queue_cnt;
scsi_sg_q.sg_head_qp = q_no;
scsi_sg_q.cntl = QCSG_SG_XFER_LIST;
for (i = 0; i < sg_head->queue_cnt; i++) {
u_int8_t segs_this_q;
if (sg_entry_cnt > ADV_SG_LIST_PER_Q)
segs_this_q = ADV_SG_LIST_PER_Q;
else {
/* This will be the last segment then */
segs_this_q = sg_entry_cnt;
scsi_sg_q.cntl |= QCSG_SG_XFER_END;
}
scsi_sg_q.seq_no = i + 1;
sg_list_dwords = segs_this_q << 1;
if (i == 0) {
scsi_sg_q.sg_list_cnt = segs_this_q;
scsi_sg_q.sg_cur_list_cnt = segs_this_q;
} else {
scsi_sg_q.sg_list_cnt = segs_this_q - 1;
scsi_sg_q.sg_cur_list_cnt = segs_this_q - 1;
}
next_qp = adv_read_lram_8(adv, q_addr + ADV_SCSIQ_B_FWD);
scsi_sg_q.q_no = next_qp;
q_addr = ADV_QNO_TO_QADDR(next_qp);
adv_write_lram_16_multi(adv,
q_addr + ADV_SCSIQ_SGHD_CPY_BEG,
(u_int16_t *)&scsi_sg_q,
sizeof(scsi_sg_q) >> 1);
adv_write_lram_32_multi(adv, q_addr + ADV_SGQ_LIST_BEG,
(u_int32_t *)&sg_head->sg_list[sg_index],
sg_list_dwords);
sg_entry_cnt -= segs_this_q;
sg_index += ADV_SG_LIST_PER_Q;
}
}
adv_put_ready_queue(adv, scsiq, q_no);
}
static void
adv_put_ready_queue(struct adv_softc *adv, struct adv_scsi_q *scsiq,
u_int q_no)
{
struct adv_target_transinfo* tinfo;
u_int q_addr;
u_int tid_no;
tid_no = ADV_TIX_TO_TID(scsiq->q2.target_ix);
tinfo = &adv->tinfo[tid_no];
if ((tinfo->current.period != tinfo->goal.period)
|| (tinfo->current.offset != tinfo->goal.offset)) {
adv_msgout_sdtr(adv, tinfo->goal.period, tinfo->goal.offset);
scsiq->q1.cntl |= QC_MSG_OUT;
}
q_addr = ADV_QNO_TO_QADDR(q_no);
scsiq->q1.status = QS_FREE;
adv_write_lram_16_multi(adv, q_addr + ADV_SCSIQ_CDB_BEG,
(u_int16_t *)scsiq->cdbptr,
scsiq->q2.cdb_len >> 1);
#if BYTE_ORDER == BIG_ENDIAN
adv_adj_scsiq_endian(scsiq);
#endif
adv_put_scsiq(adv, q_addr + ADV_SCSIQ_CPY_BEG,
(u_int16_t *) &scsiq->q1.cntl,
((sizeof(scsiq->q1) + sizeof(scsiq->q2)) / 2) - 1);
#ifdef CC_WRITE_IO_COUNT
adv_write_lram_16(adv, q_addr + ADV_SCSIQ_W_REQ_COUNT,
adv->req_count);
#endif
#ifdef CC_CLEAR_DMA_REMAIN
adv_write_lram_32(adv, q_addr + ADV_SCSIQ_DW_REMAIN_XFER_ADDR, 0);
adv_write_lram_32(adv, q_addr + ADV_SCSIQ_DW_REMAIN_XFER_CNT, 0);
#endif
adv_write_lram_16(adv, q_addr + ADV_SCSIQ_B_STATUS,
(scsiq->q1.q_no << 8) | QS_READY);
}
static void
adv_put_scsiq(struct adv_softc *adv, u_int16_t s_addr,
u_int16_t *buffer, int words)
{
int i;
/*
* XXX This routine makes *gross* assumptions
* about padding in the data structures.
* Either the data structures should have explicit
* padding members added, or they should have padding
* turned off via compiler attributes depending on
* which yields better overall performance. My hunch
* would be that turning off padding would be the
* faster approach as an outsw is much faster than
* this crude loop and accessing un-aligned data
* members isn't *that* expensive. The other choice
* would be to modify the ASC script so that the
* the adv_scsiq_1 structure can be re-arranged so
* padding isn't required.
*/
ADV_OUTW(adv, ADV_LRAM_ADDR, s_addr);
for (i = 0; i < words; i++, buffer++) {
if (i == 2 || i == 10) {
continue;
}
ADV_OUTW(adv, ADV_LRAM_DATA, *buffer);
}
}
#if BYTE_ORDER == BIG_ENDIAN
void
adv_adj_endian_qdone_info(struct adv_q_done_info *scsiq)
{
panic("adv(4) not supported on big-endian machines.\n");
}
void
adv_adj_scsiq_endian(struct adv_scsi_q *scsiq)
{
panic("adv(4) not supported on big-endian machines.\n");
}
#endif
static void
adv_handle_extmsg_in(struct adv_softc *adv, u_int16_t halt_q_addr,
u_int8_t q_cntl, target_bit_vector target_mask,
int tid_no)
{
struct ext_msg ext_msg;
adv_read_lram_16_multi(adv, ADVV_MSGIN_BEG, (u_int16_t *) &ext_msg,
sizeof(ext_msg) >> 1);
if ((ext_msg.msg_type == MSG_EXTENDED)
&& (ext_msg.msg_req == MSG_EXT_SDTR)
&& (ext_msg.msg_len == MSG_EXT_SDTR_LEN)) {
union ccb *ccb;
struct adv_target_transinfo* tinfo;
u_int32_t cinfo_index;
u_int period;
u_int offset;
int sdtr_accept;
u_int8_t orig_offset;
cinfo_index =
adv_read_lram_32(adv, halt_q_addr + ADV_SCSIQ_D_CINFO_IDX);
ccb = adv->ccb_infos[cinfo_index].ccb;
tinfo = &adv->tinfo[tid_no];
sdtr_accept = TRUE;
orig_offset = ext_msg.req_ack_offset;
if (ext_msg.xfer_period < tinfo->goal.period) {
sdtr_accept = FALSE;
ext_msg.xfer_period = tinfo->goal.period;
}
/* Perform range checking */
period = ext_msg.xfer_period;
offset = ext_msg.req_ack_offset;
adv_period_offset_to_sdtr(adv, &period, &offset, tid_no);
ext_msg.xfer_period = period;
ext_msg.req_ack_offset = offset;
/* Record our current sync settings */
adv_set_syncrate(adv, ccb->ccb_h.path,
tid_no, ext_msg.xfer_period,
ext_msg.req_ack_offset,
ADV_TRANS_GOAL|ADV_TRANS_ACTIVE);
/* Offset too high or large period forced async */
if (orig_offset != ext_msg.req_ack_offset)
sdtr_accept = FALSE;
if (sdtr_accept && (q_cntl & QC_MSG_OUT)) {
/* Valid response to our requested negotiation */
q_cntl &= ~QC_MSG_OUT;
} else {
/* Must Respond */
q_cntl |= QC_MSG_OUT;
adv_msgout_sdtr(adv, ext_msg.xfer_period,
ext_msg.req_ack_offset);
}
} else if (ext_msg.msg_type == MSG_EXTENDED
&& ext_msg.msg_req == MSG_EXT_WDTR
&& ext_msg.msg_len == MSG_EXT_WDTR_LEN) {
ext_msg.wdtr_width = 0;
adv_write_lram_16_multi(adv, ADVV_MSGOUT_BEG,
(u_int16_t *)&ext_msg,
sizeof(ext_msg) >> 1);
q_cntl |= QC_MSG_OUT;
} else {
ext_msg.msg_type = MSG_MESSAGE_REJECT;
adv_write_lram_16_multi(adv, ADVV_MSGOUT_BEG,
(u_int16_t *)&ext_msg,
sizeof(ext_msg) >> 1);
q_cntl |= QC_MSG_OUT;
}
adv_write_lram_8(adv, halt_q_addr + ADV_SCSIQ_B_CNTL, q_cntl);
}
static void
adv_msgout_sdtr(struct adv_softc *adv, u_int8_t sdtr_period,
u_int8_t sdtr_offset)
{
struct ext_msg sdtr_buf;
sdtr_buf.msg_type = MSG_EXTENDED;
sdtr_buf.msg_len = MSG_EXT_SDTR_LEN;
sdtr_buf.msg_req = MSG_EXT_SDTR;
sdtr_buf.xfer_period = sdtr_period;
sdtr_offset &= ADV_SYN_MAX_OFFSET;
sdtr_buf.req_ack_offset = sdtr_offset;
adv_write_lram_16_multi(adv, ADVV_MSGOUT_BEG,
(u_int16_t *) &sdtr_buf,
sizeof(sdtr_buf) / 2);
}
int
adv_abort_ccb(struct adv_softc *adv, int target, int lun, union ccb *ccb,
u_int32_t status, int queued_only)
{
u_int16_t q_addr;
u_int8_t q_no;
struct adv_q_done_info scsiq_buf;
struct adv_q_done_info *scsiq;
u_int8_t target_ix;
int count;
if (!dumping)
mtx_assert(&adv->lock, MA_OWNED);
scsiq = &scsiq_buf;
target_ix = ADV_TIDLUN_TO_IX(target, lun);
count = 0;
for (q_no = ADV_MIN_ACTIVE_QNO; q_no <= adv->max_openings; q_no++) {
struct adv_ccb_info *ccb_info;
q_addr = ADV_QNO_TO_QADDR(q_no);
adv_copy_lram_doneq(adv, q_addr, scsiq, adv->max_dma_count);
ccb_info = &adv->ccb_infos[scsiq->d2.ccb_index];
if (((scsiq->q_status & QS_READY) != 0)
&& ((scsiq->q_status & QS_ABORTED) == 0)
&& ((scsiq->cntl & QCSG_SG_XFER_LIST) == 0)
&& (scsiq->d2.target_ix == target_ix)
&& (queued_only == 0
|| !(scsiq->q_status & (QS_DISC1|QS_DISC2|QS_BUSY|QS_DONE)))
&& (ccb == NULL || (ccb == ccb_info->ccb))) {
union ccb *aborted_ccb;
struct adv_ccb_info *cinfo;
scsiq->q_status |= QS_ABORTED;
adv_write_lram_8(adv, q_addr + ADV_SCSIQ_B_STATUS,
scsiq->q_status);
aborted_ccb = ccb_info->ccb;
/* Don't clobber earlier error codes */
if ((aborted_ccb->ccb_h.status & CAM_STATUS_MASK)
== CAM_REQ_INPROG)
aborted_ccb->ccb_h.status |= status;
cinfo = (struct adv_ccb_info *)
aborted_ccb->ccb_h.ccb_cinfo_ptr;
cinfo->state |= ACCB_ABORT_QUEUED;
count++;
}
}
return (count);
}
int
adv_reset_bus(struct adv_softc *adv, int initiate_bus_reset)
{
int count;
int i;
union ccb *ccb;
if (!dumping)
mtx_assert(&adv->lock, MA_OWNED);
i = 200;
while ((ADV_INW(adv, ADV_CHIP_STATUS) & ADV_CSW_SCSI_RESET_ACTIVE) != 0
&& i--)
DELAY(1000);
adv_reset_chip(adv, initiate_bus_reset);
adv_reinit_lram(adv);
for (i = 0; i <= ADV_MAX_TID; i++)
adv_set_syncrate(adv, NULL, i, /*period*/0,
/*offset*/0, ADV_TRANS_CUR);
ADV_OUTW(adv, ADV_REG_PROG_COUNTER, ADV_MCODE_START_ADDR);
/* Tell the XPT layer that a bus reset occured */
if (adv->path != NULL)
xpt_async(AC_BUS_RESET, adv->path, NULL);
count = 0;
while ((ccb = (union ccb *)LIST_FIRST(&adv->pending_ccbs)) != NULL) {
if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
ccb->ccb_h.status |= CAM_SCSI_BUS_RESET;
adv_done(adv, ccb, QD_ABORTED_BY_HOST, 0, 0, 0);
count++;
}
adv_start_chip(adv);
return (count);
}
static void
adv_set_sdtr_reg_at_id(struct adv_softc *adv, int tid, u_int8_t sdtr_data)
{
int orig_id;
adv_set_bank(adv, 1);
orig_id = ffs(ADV_INB(adv, ADV_HOST_SCSIID)) - 1;
ADV_OUTB(adv, ADV_HOST_SCSIID, tid);
if (ADV_INB(adv, ADV_HOST_SCSIID) == (0x01 << tid)) {
adv_set_bank(adv, 0);
ADV_OUTB(adv, ADV_SYN_OFFSET, sdtr_data);
}
adv_set_bank(adv, 1);
ADV_OUTB(adv, ADV_HOST_SCSIID, orig_id);
adv_set_bank(adv, 0);
}
|