1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* This file contains the implementation of the mboxsc module, a mailbox layer
* built upon the Starcat IOSRAM driver.
*/
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/modctl.h>
#include <sys/errno.h>
#include <sys/ksynch.h>
#include <sys/kmem.h>
#include <sys/varargs.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/sysmacros.h>
#include <sys/iosramreg.h>
#include <sys/iosramio.h>
#include <sys/mboxsc.h>
#include <sys/mboxsc_impl.h>
/*
* Debugging facility
*/
#define DBGACT_NONE (0x00000000)
#define DBGACT_BREAK (0x00000001)
#define DBGACT_SHOWPOS (0x00000002)
#define DBGACT_DEFAULT DBGACT_NONE
#define DBG_DEV (0x00000001)
#define DBG_CALLS (0x00000002)
#define DBG_RETS (0x00000004)
#define DBG_ARGS (0x00000008)
#define DBG_KMEM (0x00000010)
#define DBG_ALL (0xFFFFFFFF)
#ifdef DEBUG
static uint32_t mboxsc_debug_mask = 0x00000000;
#define DPRINTF0(class, action, fmt) \
mboxsc_dprintf(__FILE__, __LINE__, (class), (action), (fmt))
#define DPRINTF1(class, action, fmt, arg1) \
mboxsc_dprintf(__FILE__, __LINE__, (class), (action), (fmt),\
(arg1))
#define DPRINTF2(class, action, fmt, arg1, arg2) \
mboxsc_dprintf(__FILE__, __LINE__, (class), (action), (fmt),\
(arg1), (arg2))
#define DPRINTF3(class, action, fmt, arg1, arg2, arg3) \
mboxsc_dprintf(__FILE__, __LINE__, (class), (action), (fmt),\
(arg1), (arg2), (arg3))
#define DPRINTF4(class, action, fmt, arg1, arg2, arg3, arg4) \
mboxsc_dprintf(__FILE__, __LINE__, (class), (action), (fmt),\
(arg1), (arg2), (arg3), (arg4))
#define DPRINTF5(class, action, fmt, arg1, arg2, arg3, arg4, arg5) \
mboxsc_dprintf(__FILE__, __LINE__, (class), (action), (fmt),\
(arg1), (arg2), (arg3), (arg4), (arg5))
#else /* DEBUG */
#define DPRINTF0(class, action, fmt)
#define DPRINTF1(class, action, fmt, arg1)
#define DPRINTF2(class, action, fmt, arg1, arg2)
#define DPRINTF3(class, action, fmt, arg1, arg2, arg3)
#define DPRINTF4(class, action, fmt, arg1, arg2, arg3, arg4)
#define DPRINTF5(class, action, fmt, arg1, arg2, arg3, arg4, arg5)
#endif /* DEBUG */
/*
* Basic constants
*/
#ifndef TRUE
#define TRUE (1)
#endif /* TRUE */
#ifndef FALSE
#define FALSE (0)
#endif /* FALSE */
/*
* Whenever mboxsc_init is called to create a new mailbox, an instance of
* mboxsc_mbox_t is created and inserted into a hash table to maintain
* various information about the mailbox. The mbox_state, mbox_refcount, and
* mbox_wait fields are all protected by the global mboxsc_lock mutex.
* If lock contention between mailboxes becomes an issue, each mailbox will
* need to be given its own mutex to protect the mbox_wait, mbox_state,
* and mbox_update_wait fields. The mbox_refcount field will probably need to
* remain under global protection, however, since it is used to keep track of
* the number of threads sleeping inside the mailbox's various synchronization
* mechanisms and would consequently be difficult to protect using those same
* mechanisms.
*/
typedef struct mboxsc_mbox {
uint32_t mbox_key;
int mbox_direction;
void (*mbox_callback)(void);
uint32_t mbox_length;
uint16_t mbox_refcount;
uint16_t mbox_state;
kcondvar_t mbox_wait;
mboxsc_msghdr_t mbox_header;
struct mboxsc_mbox *mbox_hash_next;
} mboxsc_mbox_t;
/*
* Various state flags that can be set on a mailbox. Multiple states may
* be active at the same time.
*/
#define STATE_IDLE (0x0000)
#define STATE_WRITING (0x0001)
#define STATE_READING (0x0002)
#define STATE_HDRVALID (0x0004)
/*
* Timeout periods for mboxsc_putmsg and mboxsc_getmsg, converted to ticks
* from the microsecond values found in mboxsc_impl.h.
*/
#define EAGAIN_POLL (drv_usectohz(MBOXSC_EAGAIN_POLL_USECS))
#define PUTMSG_POLL (drv_usectohz(MBOXSC_PUTMSG_POLL_USECS))
#define HWLOCK_POLL (drv_usectohz(MBOXSC_HWLOCK_POLL_USECS))
#define LOOP_WARN_INTERVAL (drv_usectohz(MBOXSC_USECS_PER_SECOND * 15))
/*
* Various tests that are performed on message header fields.
*/
#define IS_UNSOLICITED_TYPE(type) ((type) != MBOXSC_MSG_REPLY)
#define MSG_TYPE_MATCHES(type, msgp) \
(((type) == 0) || ((type) & (msgp)->msg_type))
#define MSG_CMD_MATCHES(cmd, msgp) \
(((cmd) == 0) || ((cmd) == (msgp)->msg_cmd))
#define MSG_TRANSID_MATCHES(tid, msgp) \
(((tid) == 0) || ((tid) == (msgp)->msg_transid))
/*
* This macro can be used to determine the size of any field in the message
* header (or any other struct, for that matter).
*/
#define FIELD_SIZE(type, field) (sizeof (((type *)0)->field))
/*
* Mask used when generating unique transaction ID values.
* This arbitrarily chosen value will be OR'd together with
* a counter for each successive internally-generated transaction ID.
*/
#define TRANSID_GEN_MASK (0xFFC0000000000000)
/*
* All existing mailboxes are stored in a hash table with HASHTBL_SIZE
* entries so they can be rapidly accessed by their key values.
*/
#define HASHTBL_SIZE (32)
#define HASH_KEY(key) ((((key) >> 24) ^ ((key) >> 16) ^ ((key) >> 9) ^\
(key)) & (HASHTBL_SIZE - 1));
/*
* Unfortunately, it is necessary to calculate checksums on data split up
* amongst different buffers in some cases. Consequently, mboxsc_checksum
* accepts a "seed" value as one of its parameters. When first starting a
* checksum calculation, the seed should be 0.
*/
#define CHKSUM_INIT (0)
/*
* local variables
*/
static kmutex_t mboxsc_lock;
static mboxsc_mbox_t *mboxsc_hash_table[HASHTBL_SIZE];
static uint32_t mboxsc_flaglock_count;
static uint32_t mboxsc_active_version = MBOXSC_PROTOCOL_VERSION;
static kcondvar_t mboxsc_dereference_cv;
/*
* Structures from modctl.h used for loadable module support.
* The mboxsc API is a "miscellaneous" module.
*/
extern struct mod_ops mod_miscops;
static struct modlmisc modlmisc = {
&mod_miscops,
"IOSRAM Mailbox API 'mboxsc'",
};
static struct modlinkage modlinkage = {
MODREV_1,
(void *)&modlmisc,
NULL
};
/*
* Prototypes for local functions
*/
static void mboxsc_iosram_callback(void *arg);
static void mboxsc_hdrchange_callback(void);
static int mboxsc_add_mailbox(mboxsc_mbox_t *mailboxp);
static void mboxsc_close_mailbox(mboxsc_mbox_t *mailboxp);
static void mboxsc_hashinsert_mailbox(mboxsc_mbox_t *mailboxp);
static mboxsc_mbox_t *mboxsc_hashfind_mailbox_by_key(uint32_t key);
static mboxsc_mbox_t *mboxsc_hashremove_mailbox_by_key(uint32_t key);
static mboxsc_chksum_t mboxsc_checksum(mboxsc_chksum_t seed, uint8_t *buf,
uint32_t length);
static int mboxsc_lock_flags(uint8_t mandatory, clock_t deadline);
static int mboxsc_unlock_flags(uint8_t mandatory);
static int mboxsc_timed_read(clock_t deadline, uint32_t key,
uint32_t off, uint32_t len, caddr_t dptr);
static int mboxsc_timed_write(clock_t deadline, uint32_t key,
uint32_t off, uint32_t len, caddr_t dptr);
static int mboxsc_timed_get_flag(clock_t deadline, uint32_t key,
uint8_t *data_validp, uint8_t *int_pendingp);
static int mboxsc_timed_set_flag(clock_t deadline, uint32_t key,
uint8_t data_valid, uint8_t int_pending);
static int mboxsc_timed_send_intr(clock_t deadline);
static int mboxsc_expire_message(uint32_t key, int *resultp);
static uint64_t mboxsc_generate_transid(uint64_t prev_transid);
static void mboxsc_reference_mailbox(mboxsc_mbox_t *mailboxp);
static void mboxsc_dereference_mailbox(mboxsc_mbox_t *mailboxp);
#ifdef DEBUG
/*PRINTFLIKE5*/
static void mboxsc_dprintf(const char *file, int line,
uint32_t class, uint32_t action, const char *fmt, ...);
int mboxsc_debug(int cmd, void *arg);
#endif /* DEBUG */
/*
* _init
*
* Loadable module support routine. Initializes global lock and hash table.
*/
int
_init(void)
{
int i;
uint32_t sms_version;
int error = 0;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "_init called\n");
/*
* Initialize all module resources.
*/
mutex_init(&mboxsc_lock, NULL, MUTEX_DRIVER, NULL);
cv_init(&mboxsc_dereference_cv, NULL, CV_DRIVER, NULL);
for (i = 0; i < HASHTBL_SIZE; i++) {
mboxsc_hash_table[i] = NULL;
}
mboxsc_flaglock_count = 0;
if (mod_install(&modlinkage) != 0) {
goto failed;
}
/*
* Set the os_mbox_version field in the IOSRAM header to indicate the
* highest Mailbox Protocol version we support
*/
error = iosram_hdr_ctrl(IOSRAM_HDRCMD_SET_OS_MBOX_VER,
(void *)MBOXSC_PROTOCOL_VERSION);
if (error != 0) {
goto failed;
}
/*
* Read the sms_mbox_version field in the IOSRAM header to determine
* what the greatest commonly supported version is.
*/
error = iosram_hdr_ctrl(IOSRAM_HDRCMD_GET_SMS_MBOX_VER,
(void *)&sms_version);
if (error != 0) {
goto failed;
}
mboxsc_active_version = MIN(MBOXSC_PROTOCOL_VERSION, sms_version);
DPRINTF2(DBG_DEV, DBGACT_DEFAULT,
"sms version: %d, active version: %d\n", sms_version,
mboxsc_active_version);
/*
* Register a callback with the IOSRAM driver to receive notification of
* changes to the IOSRAM header, in case the sms_mbox_version field
* changes.
*/
error = iosram_hdr_ctrl(IOSRAM_HDRCMD_REG_CALLBACK,
(void *)mboxsc_hdrchange_callback);
if (error != 0) {
goto failed;
}
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "_init ret: 0x%08x\n", error);
return (0);
/*
* If initialization fails, uninitialize resources.
*/
failed:
mutex_destroy(&mboxsc_lock);
cv_destroy(&mboxsc_dereference_cv);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "_init ret: 0x%08x\n", error);
return (error);
}
/*
* _fini
*
* Loadable module support routine. Closes all mailboxes and releases all
* resources.
*/
int
_fini(void)
{
int i;
int error = 0;
mboxsc_mbox_t *mailboxp;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "_fini called\n");
/*
* Attempt to remove the module. If successful, close all mailboxes
* and deallocate the global lock.
*/
error = mod_remove(&modlinkage);
if (error == 0) {
mutex_enter(&mboxsc_lock);
(void) iosram_hdr_ctrl(IOSRAM_HDRCMD_REG_CALLBACK, NULL);
for (i = 0; i < HASHTBL_SIZE; i++) {
while (mboxsc_hash_table[i] != NULL) {
mailboxp = mboxsc_hash_table[i];
mboxsc_close_mailbox(mailboxp);
}
}
mutex_exit(&mboxsc_lock);
mutex_destroy(&mboxsc_lock);
cv_destroy(&mboxsc_dereference_cv);
}
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "_fini ret: 0x%08x\n", error);
return (error);
}
/*
* _info
*
* Loadable module support routine.
*/
int
_info(struct modinfo *modinfop)
{
int error = 0;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "_info called\n");
error = mod_info(&modlinkage, modinfop);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "_info ret: 0x%08x\n", error);
return (error);
}
/*
* mboxsc_init
*
* Attempts to create a new mailbox.
*/
int
mboxsc_init(uint32_t key, int direction, void (*event_handler)(void))
{
int error = 0;
mboxsc_mbox_t *mailboxp;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_init called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%x\n", key);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "direction = %d\n", direction);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "event_handlerp = %p\n",
(void *)event_handler);
/*
* Check for valid direction and callback specification.
*/
if (((direction != MBOXSC_MBOX_IN) && (direction != MBOXSC_MBOX_OUT)) ||
((event_handler != NULL) && (direction != MBOXSC_MBOX_IN))) {
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "mboxsc_init ret: 0x%08x\n",
EINVAL);
return (EINVAL);
}
/*
* Allocate memory for the mailbox structure and initialize all
* caller-provided fields.
*/
mailboxp = (mboxsc_mbox_t *)kmem_zalloc(sizeof (mboxsc_mbox_t),
KM_SLEEP);
DPRINTF2(DBG_KMEM, DBGACT_DEFAULT, "kmem_zalloc(%lu) = %p\n",
sizeof (mboxsc_mbox_t), (void *)mailboxp);
mailboxp->mbox_key = key;
mailboxp->mbox_direction = direction;
mailboxp->mbox_callback = event_handler;
/*
* Attempt to add the mailbox. If unsuccessful, free the allocated
* memory.
*/
mutex_enter(&mboxsc_lock);
error = mboxsc_add_mailbox(mailboxp);
mutex_exit(&mboxsc_lock);
if (error != 0) {
DPRINTF2(DBG_KMEM, DBGACT_DEFAULT, "kmem_free(%p, %lu)\n",
(void *)mailboxp, sizeof (mboxsc_mbox_t));
kmem_free(mailboxp, sizeof (mboxsc_mbox_t));
}
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "mboxsc_init ret: 0x%08x\n", error);
return (error);
}
/*
* mboxsc_fini
*
* Closes the mailbox with the indicated key, if it exists.
*/
int
mboxsc_fini(uint32_t key)
{
int error = 0;
mboxsc_mbox_t *mailboxp;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_fini called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%x\n", key);
/*
* Attempt to close the mailbox.
*/
mutex_enter(&mboxsc_lock);
mailboxp = mboxsc_hashfind_mailbox_by_key(key);
if (mailboxp == NULL) {
error = EBADF;
} else {
while (mailboxp->mbox_refcount != 0) {
cv_wait(&mboxsc_dereference_cv, &mboxsc_lock);
}
mboxsc_close_mailbox(mailboxp);
}
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "mboxsc_fini ret: 0x%08x\n", error);
return (error);
}
/*
* mboxsc_putmsg
*
* Attempt to place a message into an outbound mailbox and signal the
* recipient. A successful return (0) indicates that the message was
* successfully delivered.
*/
int
mboxsc_putmsg(uint32_t key, uint32_t type, uint32_t cmd, uint64_t *transidp,
uint32_t length, void *datap, clock_t timeout)
{
int i;
int error = 0;
int result;
int lock_held = 0;
int unlock_err;
uint8_t data_valid;
clock_t deadline;
clock_t remainder;
mboxsc_chksum_t checksum;
mboxsc_mbox_t *mailboxp;
mboxsc_msghdr_t header;
#ifdef DEBUG /* because lint whines about if stmts without consequents */
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_putmsg called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%x\n", key);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "type = 0x%x\n", type);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "cmd = 0x%x\n", cmd);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "transidp = %p\n", (void *)transidp);
if (transidp != NULL) {
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "*transidp = 0x%016lx\n",
*transidp);
}
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "length = 0x%x\n", length);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "datap = %p\n", datap);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "timeout = %ld\n", timeout);
#endif /* DEBUG */
/*
* Perform some basic sanity checks on the message.
*/
for (i = 0; i < MBOXSC_NUM_MSG_TYPES; i++) {
if (type == (1 << i)) {
break;
}
}
if ((i == MBOXSC_NUM_MSG_TYPES) || (cmd == 0) ||
((datap == NULL) && (length != 0)) ||
(timeout < MBOXSC_PUTMSG_MIN_TIMEOUT_MSECS) ||
(timeout > MBOXSC_PUTMSG_MAX_TIMEOUT_MSECS)) {
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_putmsg ret: 0x%08x\n", EINVAL);
return (EINVAL);
}
/*
* Initialize the header structure with values provided by the caller.
*/
header.msg_version = mboxsc_active_version;
header.msg_type = type;
header.msg_cmd = cmd;
header.msg_length = MBOXSC_MSGHDR_SIZE + length;
if (transidp != NULL) {
header.msg_transid = *transidp;
} else {
header.msg_transid = 0;
}
/*
* Perform additional sanity checks on the mailbox and message.
* Make sure that the specified mailbox really exists, that the
* given message will fit in it, and that the current message's
* transaction ID isn't the same as the last message's transaction
* ID unless both messages are replies (it's okay, necessary even,
* to reuse a transaction ID when resending a failed reply message,
* but that is the only case in which it is permissible).
*/
mutex_enter(&mboxsc_lock);
mailboxp = mboxsc_hashfind_mailbox_by_key(key);
if (mailboxp == NULL) {
error = EBADF;
} else if ((mailboxp->mbox_direction != MBOXSC_MBOX_OUT) ||
(length + MBOXSC_PROTOCOL_SIZE > mailboxp->mbox_length) ||
((header.msg_transid == mailboxp->mbox_header.msg_transid) &&
((type & mailboxp->mbox_header.msg_type) != MBOXSC_MSG_REPLY) &&
(header.msg_transid != 0))) {
error = EINVAL;
}
if (error != 0) {
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_putmsg ret: 0x%08x\n", error);
return (error);
}
/*
* If the message's transaction ID is set to 0, generate a unique
* transaction ID and copy it into the message header. If the message
* is successfully delivered and transidp != NULL, we'll copy this new
* transid into *transidp later.
*/
if (header.msg_transid == 0) {
header.msg_transid =
mboxsc_generate_transid(mailboxp->mbox_header.msg_transid);
}
/*
* Don't allow mboxsc_putmsg to attempt to place a message for
* longer than the caller's timeout.
*/
deadline = ddi_get_lbolt() +
drv_usectohz(timeout * MBOXSC_USECS_PER_MSEC);
/*
* Increment the reference count on the mailbox to keep it from being
* closed, and wait for it to become available.
*/
mboxsc_reference_mailbox(mailboxp);
remainder = 1;
while ((mailboxp->mbox_state & STATE_WRITING) &&
(remainder > 0)) {
remainder = cv_timedwait_sig(&(mailboxp->mbox_wait),
&mboxsc_lock, deadline);
}
/*
* Check to see whether or not the mailbox became available. If it
* did not, decrement its reference count and return an error to the
* caller.
*/
if (remainder == -1) {
error = ENOSPC;
} else if (remainder == 0) {
error = EINTR;
}
if (error != 0) {
mboxsc_dereference_mailbox(mailboxp);
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_putmsg ret: 0x%08x\n", error);
return (error);
}
/*
* Since the message is valid and we're going to try to write it to
* IOSRAM, record its header for future reference (e.g. to make sure the
* next message doesn't incorrectly use the same transID).
*/
bcopy(&header, &(mailboxp->mbox_header), MBOXSC_MSGHDR_SIZE);
/*
* Flag the mailbox as being in use and release the global lock.
*/
mailboxp->mbox_state |= STATE_WRITING;
mutex_exit(&mboxsc_lock);
/*
* Calculate the message checksum using the header and the data.
*/
checksum = mboxsc_checksum(CHKSUM_INIT, (uint8_t *)&header,
MBOXSC_MSGHDR_SIZE);
checksum = mboxsc_checksum(checksum, (uint8_t *)datap, length);
/*
* Attempt to write the message and checksum to IOSRAM until successful,
* or as long as time remains and no errors other than EAGAIN are
* returned from any call to the IOSRAM driver in case there is a tunnel
* switch in progress.
*/
error = mboxsc_timed_write(deadline, key, MBOXSC_MSGHDR_OFFSET,
MBOXSC_MSGHDR_SIZE, (caddr_t)&header);
if (error == 0) {
error = mboxsc_timed_write(deadline, key, MBOXSC_DATA_OFFSET,
length, (caddr_t)datap);
}
if (error == 0) {
error = mboxsc_timed_write(deadline, key, header.msg_length,
MBOXSC_CHKSUM_SIZE, (caddr_t)&checksum);
}
/*
* Lock the flags before setting data_valid. This isn't strictly
* necessary for correct protocol operation, but it gives us a chance to
* verify that the flags lock is functional before we commit to sending
* the message.
*/
if (error == 0) {
error = mboxsc_lock_flags(FALSE, deadline);
if (error == 0) {
lock_held = 1;
} else if (error == EBUSY) {
error = EAGAIN;
}
}
if (error == 0) {
error = mboxsc_timed_set_flag(deadline, key, IOSRAM_DATA_VALID,
IOSRAM_INT_TO_SSC);
}
/*
* Unlock the flags. If an error is encountered, only return it if
* another error hasn't been encountered previously.
*/
if (lock_held) {
unlock_err = mboxsc_unlock_flags(TRUE);
if ((unlock_err != 0) && ((error == 0) || (error == EAGAIN))) {
error = unlock_err;
}
}
/*
* If time ran out or an IOSRAM call failed, notify other callers that
* the mailbox is available, decrement its reference count, and return
* an error.
*/
if (error != 0) {
ASSERT((error != EINVAL) && (error != EMSGSIZE));
mutex_enter(&mboxsc_lock);
mailboxp->mbox_state &= ~STATE_WRITING;
cv_broadcast(&(mailboxp->mbox_wait));
mboxsc_dereference_mailbox(mailboxp);
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_putmsg ret: 0x%08x\n", error);
return (error);
}
/*
* Send an interrupt to the remote mailbox interface to announce the
* presence of a new, valid message.
*/
error = mboxsc_timed_send_intr(deadline);
/*
* Wait until either the data_valid flag is set INVALID by the
* remote client or time runs out. Since we're calling delay as
* a part of polling the flag anyway, we don't really need to do
* the usual continuous retry if iosram_get_flag returns EAGAIN.
*/
data_valid = IOSRAM_DATA_VALID;
if (error == DDI_SUCCESS) {
do {
delay(MIN(PUTMSG_POLL, deadline - ddi_get_lbolt()));
error = iosram_get_flag(key, &data_valid, NULL);
} while ((data_valid == IOSRAM_DATA_VALID) &&
((error == EAGAIN) || (error == 0)) &&
(deadline - ddi_get_lbolt() >= 0));
}
/*
* If the data_valid flag was set to INVALID by the other side, the
* message was successfully transmitted. If it wasn't, but there
* weren't any IOSRAM errors, the operation timed out. If there was a
* problem with the IOSRAM, pass that info back to the caller.
*/
if (data_valid == IOSRAM_DATA_INVALID) {
result = 0;
} else if ((error == 0) || (error == DDI_FAILURE)) {
result = ETIMEDOUT;
} else {
ASSERT(error != EINVAL);
result = error;
}
/*
* If the message has not been picked up, expire it. Note that this may
* actually result in detecting successful message delivery if the SC
* picks it up at the last moment. If expiration fails due to an error,
* return an error to the user even if the message appears to have
* been successfully delivered.
*/
if (data_valid == IOSRAM_DATA_VALID) {
error = mboxsc_expire_message(key, &result);
if ((error != 0) && ((result == 0) || (result == ETIMEDOUT))) {
result = error;
}
}
/*
* If the message was successfully delivered, and we generated a
* transaction ID for the caller, and the caller wants to know what it
* was, give it to them.
*/
if ((result == 0) && (transidp != NULL) && (*transidp == 0)) {
*transidp = header.msg_transid;
}
/*
* Regardless of whether the message was successfully transmitted or
* not, notify other callers that the mailbox is available and decrement
* its reference count.
*/
mutex_enter(&mboxsc_lock);
mailboxp->mbox_state &= ~STATE_WRITING;
cv_broadcast(&(mailboxp->mbox_wait));
mboxsc_dereference_mailbox(mailboxp);
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "mboxsc_putmsg ret: 0x%08x\n",
result);
return (result);
}
/*
* mboxsc_getmsg
*
* Attempt to retrieve a message from the mailbox with the given key that
* matches values provided in msgp. A successful return (0) indicates that
* a message matching the caller's request was successfully received within
* timeout milliseconds. If a message matching the caller's request is
* detected, but can't be successfully read, an error will be returned even
* if the caller's timeout hasn't expired.
*/
int
mboxsc_getmsg(uint32_t key, uint32_t *typep, uint32_t *cmdp, uint64_t *transidp,
uint32_t *lengthp, void *datap, clock_t timeout)
{
int error = 0;
uint32_t datalen;
uint8_t data_valid;
uint8_t lock_held;
mboxsc_chksum_t read_checksum;
mboxsc_chksum_t calc_checksum;
uint64_t read_transid;
clock_t deadline;
clock_t remainder;
mboxsc_mbox_t *mailboxp;
mboxsc_msghdr_t header;
#ifdef DEBUG /* because lint whines about if stmts without consequents */
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_getmsg called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%x\n", key);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "typep = %p\n", (void *)typep);
if (typep != NULL) {
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "*typep = 0x%x\n", *typep);
}
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "cmdp = %p\n", (void *)cmdp);
if (cmdp != NULL) {
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "*cmdp = 0x%x\n", *cmdp);
}
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "transidp = %p\n", (void *)transidp);
if (transidp != NULL) {
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "*transidp = 0x%lx\n",
*transidp);
}
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "lengthp = %p\n", (void *)lengthp);
if (lengthp != NULL) {
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "*lengthp = 0x%x\n",
*lengthp);
}
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "datap = %p\n", datap);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "timeout = %ld\n", timeout);
#endif /* DEBUG */
/*
* Perform basic sanity checks on the caller's request.
*/
if ((typep == NULL) || (*typep >= (1 << MBOXSC_NUM_MSG_TYPES)) ||
(cmdp == NULL) || (transidp == NULL) || (lengthp == NULL) ||
((datap == NULL) && (*lengthp != 0)) ||
(timeout < MBOXSC_GETMSG_MIN_TIMEOUT_MSECS) ||
(timeout > MBOXSC_GETMSG_MAX_TIMEOUT_MSECS)) {
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_getmsg ret: 0x%08x\n", EINVAL);
return (EINVAL);
}
/*
* Don't allow mboxsc_getmsg to attempt to receive a message for
* longer than the caller's timeout.
*/
deadline = ddi_get_lbolt() +
drv_usectohz(timeout * MBOXSC_USECS_PER_MSEC);
/*
* Perform additional sanity checks on the client's request and the
* associated mailbox.
*/
mutex_enter(&mboxsc_lock);
mailboxp = mboxsc_hashfind_mailbox_by_key(key);
if (mailboxp == NULL) {
error = EBADF;
} else if (mailboxp->mbox_direction != MBOXSC_MBOX_IN) {
error = EINVAL;
}
if (error != 0) {
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_getmsg ret: 0x%08x\n", error);
return (error);
}
/*
* The request is okay, so reference the mailbox (to keep it from being
* closed), and proceed with the real work.
*/
mboxsc_reference_mailbox(mailboxp);
/*
* Certain failures that may occur late in the process of getting a
* message (e.g. checksum error, cancellation by the sender) are
* supposed to leave the recipient waiting for the next message to
* arrive rather than returning an error. To facilitate restarting
* the message acquisition process, the following label is provided
* as a target for a very few judiciously-placed "goto"s.
*
* The mboxsc_lock mutex MUST be held when jumping to this point.
*/
mboxsc_getmsg_retry:
;
/*
* If there is a valid message in the mailbox right now, check to
* see if it matches the caller's request. If not, or if another
* caller is already reading it, wait for either the arrival of the
* next message or the expiration of the caller's specified timeout.
*/
error = 0;
while (!(mailboxp->mbox_state & STATE_HDRVALID) ||
(mailboxp->mbox_state & STATE_READING) ||
!MSG_TYPE_MATCHES(*typep, &(mailboxp->mbox_header)) ||
!MSG_CMD_MATCHES(*cmdp, &(mailboxp->mbox_header)) ||
!MSG_TRANSID_MATCHES(*transidp, &(mailboxp->mbox_header))) {
remainder = cv_timedwait_sig(&(mailboxp->mbox_wait),
&mboxsc_lock, deadline);
if (remainder == -1) {
error = ETIMEDOUT;
} else if (remainder == 0) {
error = EINTR;
}
if (error != 0) {
mboxsc_dereference_mailbox(mailboxp);
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_getmsg ret: 0x%08x\n", error);
return (error);
}
}
/*
* If somebody sends us a message using a Mailbox Protocol version
* greater than the highest one we understand, invalidate the message,
* because we can't safely interpret anything beyond the version field.
*/
if (mailboxp->mbox_header.msg_version > MBOXSC_PROTOCOL_VERSION) {
DPRINTF1(DBG_DEV, DBGACT_DEFAULT,
"incoming message with unsupported version %d\n",
mailboxp->mbox_header.msg_version);
mailboxp->mbox_state &= ~STATE_HDRVALID;
goto mboxsc_getmsg_retry;
}
/*
* At this point, there is a stored message header that matches the
* caller's request, but the actual message may no longer be valid
* in IOSRAM. Check the data_valid flag to see whether or not
* this is the case. If the message has expired, go start over.
*
* The global mutex is held while reading flag data from IOSRAM to
* avoid certain race conditions. One race condition is still
* possible (i.e. SC-side has just set the data_valid flag for a
* new message, but the stored message header hasn't been updated
* yet), but it won't cause incorrect behavior (just some wasted work).
*/
error = iosram_get_flag(key, &data_valid, NULL);
ASSERT(error != EINVAL);
if (error == 0) {
if (data_valid != IOSRAM_DATA_VALID) {
mailboxp->mbox_state &= ~STATE_HDRVALID;
goto mboxsc_getmsg_retry;
}
} else if ((error == EAGAIN) && (deadline - ddi_get_lbolt() >= 0)) {
mutex_exit(&mboxsc_lock);
delay(MIN(EAGAIN_POLL, deadline - ddi_get_lbolt()));
mutex_enter(&mboxsc_lock);
goto mboxsc_getmsg_retry;
}
/*
* If the message is larger than the caller's buffer, provide the caller
* with the length of the message and return an error.
*/
datalen = mailboxp->mbox_header.msg_length - MBOXSC_MSGHDR_SIZE;
if ((error == 0) && (datalen > *lengthp)) {
*lengthp = datalen;
error = EMSGSIZE;
}
/*
* Note that there's no need to check STATE_HDRVALID before broadcasting
* here because the header is guaranteed to be valid at this point.
*/
if (error != 0) {
cv_broadcast(&(mailboxp->mbox_wait));
mboxsc_dereference_mailbox(mailboxp);
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_getmsg ret: 0x%08x\n", error);
return (error);
}
/*
* Store a copy of the current message header, flag the mailbox to
* indicate that it is being read and attempt to read the message data
* and checksum.
*/
bcopy(&(mailboxp->mbox_header), &header, MBOXSC_MSGHDR_SIZE);
mailboxp->mbox_state |= STATE_READING;
mutex_exit(&mboxsc_lock);
if (datalen > 0) {
error = mboxsc_timed_read(deadline, key, MBOXSC_DATA_OFFSET,
datalen, (caddr_t)datap);
}
if (error == 0) {
error = mboxsc_timed_read(deadline, key, header.msg_length,
MBOXSC_CHKSUM_SIZE, (caddr_t)&read_checksum);
}
/*
* Check for errors that may have occurred while accessing IOSRAM.
*/
if (error != 0) {
ASSERT((error != EINVAL) && (error != EMSGSIZE));
mutex_enter(&mboxsc_lock);
mailboxp->mbox_state &= ~STATE_READING;
if (mailboxp->mbox_state & STATE_HDRVALID) {
cv_broadcast(&(mailboxp->mbox_wait));
}
mboxsc_dereference_mailbox(mailboxp);
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_getmsg ret: 0x%08x\n", error);
return (error);
}
/*
* Calculate the checksum for the header and data that was read from
* IOSRAM.
*/
calc_checksum = mboxsc_checksum(CHKSUM_INIT, (uint8_t *)&header,
MBOXSC_MSGHDR_SIZE);
calc_checksum = mboxsc_checksum(calc_checksum, (uint8_t *)datap,
datalen);
/*
* If the message header has been invalidated, note the change.
* If a the checksum verification fails, invalidate the message
* header. In either case, go back to the beginning and wait
* for a new message.
*/
mutex_enter(&mboxsc_lock);
if (!(mailboxp->mbox_state & STATE_HDRVALID)) {
error = -1;
DPRINTF0(DBG_DEV, DBGACT_DEFAULT,
"mboxsc_getmsg - message invalidated while reading\n");
} else if (read_checksum != calc_checksum) {
error = -1;
mailboxp->mbox_state &= ~STATE_HDRVALID;
DPRINTF0(DBG_DEV, DBGACT_DEFAULT,
"mboxsc_getmsg - message failed checksum\n");
cmn_err(CE_NOTE,
"mboxsc_getmsg - message failed checksum\n");
}
if (error == -1) {
mailboxp->mbox_state &= ~STATE_READING;
goto mboxsc_getmsg_retry;
}
/*
* Acquire the hardware lock used for synchronization of data_valid flag
* access to avoid race conditions. If it is acquired, try to check the
* current data_valid flag and transaction ID to verify that the message
* is still valid.
*/
mutex_exit(&mboxsc_lock);
if ((error = mboxsc_lock_flags(FALSE, deadline)) != 0) {
lock_held = FALSE;
/*
* We don't "do" EBUSY here, so treat it as EAGAIN.
*/
if (error == EBUSY) {
error = EAGAIN;
}
} else {
lock_held = TRUE;
}
if (error == 0) {
error = mboxsc_timed_get_flag(deadline, key, &data_valid, NULL);
}
if ((error == 0) && (data_valid == IOSRAM_DATA_VALID)) {
error = mboxsc_timed_read(deadline, key,
offsetof(mboxsc_msghdr_t, msg_transid),
FIELD_SIZE(mboxsc_msghdr_t, msg_transid),
(caddr_t)&read_transid);
}
/*
* If something failed along the way, either the error is unrecoverable
* or we're just plain out of time, so unlock the flags if they were
* locked, release the mailbox, wake up other potential readers if
* there's still a message around, and return.
*/
if (error != 0) {
ASSERT((error != EINVAL) && (error != EMSGSIZE));
if (lock_held) {
(void) mboxsc_unlock_flags(TRUE);
}
mutex_enter(&mboxsc_lock);
mailboxp->mbox_state &= ~STATE_READING;
if (mailboxp->mbox_state & STATE_HDRVALID) {
cv_broadcast(&(mailboxp->mbox_wait));
}
mboxsc_dereference_mailbox(mailboxp);
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_getmsg ret: 0x%08x\n", error);
return (error);
}
/*
* If the data_valid flag isn't set to IOSRAM_DATA_VALID, or the
* message transaction ID in IOSRAM has changed, the message being
* read was timed out by its sender. Since the data_valid flag can't
* change as long as we have the flags locked, we can safely mark the
* stored message header invalid if either the data_valid flag isn't set
* or the stored transaction ID doesn't match the one we read. (If
* data_valid is set, the transaction ID shouldn't be changing
* underneath us.) On the other hand, if there may still be a valid
* message, wake up any pending readers.
*/
if ((data_valid != IOSRAM_DATA_VALID) ||
(read_transid != header.msg_transid)) {
mutex_enter(&mboxsc_lock);
mailboxp->mbox_state &= ~STATE_READING;
if ((data_valid != IOSRAM_DATA_VALID) ||
(mailboxp->mbox_header.msg_transid != read_transid)) {
mailboxp->mbox_state &= ~STATE_HDRVALID;
} else if (mailboxp->mbox_state & STATE_HDRVALID) {
cv_broadcast(&(mailboxp->mbox_wait));
}
/*
* Unfortunately, we can't be holding mboxsc_lock when we unlock
* the flags. However, we have to hold the flags until here to
* make sure the SC doesn't change the message's state while
* we're checking to see if we should invalidate our stored
* header.
*/
mutex_exit(&mboxsc_lock);
error = mboxsc_unlock_flags(TRUE);
mutex_enter(&mboxsc_lock);
DPRINTF0(DBG_DEV, DBGACT_DEFAULT,
"mboxsc_getmsg() - message invalidated by sender\n");
goto mboxsc_getmsg_retry;
}
/*
* If everything has worked up to this point, all that remains is
* to set the data_valid flag to IOSRAM_DATA_INVALID, tidy up, and
* return the message. If the flag can't be set, the message can't
* be received, so keep trying as long as there is time.
*/
error = mboxsc_timed_set_flag(deadline, key, IOSRAM_DATA_INVALID,
IOSRAM_INT_NONE);
(void) mboxsc_unlock_flags(TRUE);
mutex_enter(&mboxsc_lock);
if (error != 0) {
ASSERT(error != EINVAL);
mboxsc_dereference_mailbox(mailboxp);
mailboxp->mbox_state &= ~STATE_READING;
if (mailboxp->mbox_state & STATE_HDRVALID) {
cv_broadcast(&(mailboxp->mbox_wait));
}
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_getmsg ret: 0x%08x\n", error);
return (error);
}
/*
* If the message was read 100% successfully and the stored message
* header for the mailbox still matches the message that was read,
* invalidate it to prevent other readers from trying to read it.
*/
if (bcmp(&(mailboxp->mbox_header), &header, MBOXSC_MSGHDR_SIZE) == 0) {
mailboxp->mbox_state &= ~STATE_HDRVALID;
} else if (mailboxp->mbox_state & STATE_HDRVALID) {
cv_broadcast(&(mailboxp->mbox_wait));
}
mboxsc_dereference_mailbox(mailboxp);
mailboxp->mbox_state &= ~STATE_READING;
mutex_exit(&mboxsc_lock);
/*
* Since we're successfully returning a message, we need to provide the
* caller with all of the interesting header information.
*/
*typep = header.msg_type;
*cmdp = header.msg_cmd;
*transidp = header.msg_transid;
*lengthp = datalen;
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "mboxsc_getmsg ret: 0x%08x\n", 0);
return (0);
}
/*
* mboxsc_ctrl
*
* This routine provides access to a variety of services not available through
* the basic API.
*/
int
mboxsc_ctrl(uint32_t key, uint32_t cmd, void *arg)
{
int error = 0;
mboxsc_mbox_t *mailboxp;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_ctrl called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%x\n", key);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "cmd = 0x%x\n", cmd);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "arg = %p\n", arg);
mutex_enter(&mboxsc_lock);
mailboxp = mboxsc_hashfind_mailbox_by_key(key);
if (mailboxp == NULL) {
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "mboxsc_ctrl ret: 0x%08x\n",
EBADF);
return (EBADF);
}
switch (cmd) {
case MBOXSC_CMD_VERSION:
/*
* Return the Protocol version currently in use. Since
* there is only one version that exists right now, we
* can't be using anything else.
*/
if (arg == NULL) {
error = EINVAL;
break;
}
*(uint32_t *)arg = MBOXSC_PROTOCOL_VERSION;
break;
case MBOXSC_CMD_MAXVERSION:
/*
* Return the highest Protocol version that we support.
*/
if (arg == NULL) {
error = EINVAL;
break;
}
*(uint32_t *)arg = MBOXSC_PROTOCOL_VERSION;
break;
case MBOXSC_CMD_MAXDATALEN:
/*
* Return the amount of space available for client data
* in the indicated mailbox.
*/
if (arg == NULL) {
error = EINVAL;
break;
}
*(uint32_t *)arg = mailboxp->mbox_length -
MBOXSC_PROTOCOL_SIZE;
break;
case MBOXSC_CMD_PUTMSG_TIMEOUT_RANGE:
{
mboxsc_timeout_range_t *rangep;
/*
* Return the range of acceptable timeout values for
* mboxsc_putmsg, expressed in milliseconds.
*/
if (arg == NULL) {
error = EINVAL;
break;
}
rangep = (mboxsc_timeout_range_t *)arg;
rangep->min_timeout = MBOXSC_PUTMSG_MIN_TIMEOUT_MSECS;
rangep->max_timeout = MBOXSC_PUTMSG_MAX_TIMEOUT_MSECS;
break;
}
case MBOXSC_CMD_GETMSG_TIMEOUT_RANGE:
{
mboxsc_timeout_range_t *rangep;
/*
* Return the range of acceptable timeout values for
* mboxsc_getmsg, expressed in milliseconds.
*/
if (arg == NULL) {
error = EINVAL;
break;
}
rangep = (mboxsc_timeout_range_t *)arg;
rangep->min_timeout = MBOXSC_GETMSG_MIN_TIMEOUT_MSECS;
rangep->max_timeout = MBOXSC_GETMSG_MAX_TIMEOUT_MSECS;
break;
}
default:
error = ENOTSUP;
break;
}
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "mboxsc_ctrl ret: 0x%08x\n", error);
return (error);
}
/*
* mboxsc_putmsg_def_timeout
*
* This routine returns the default mboxsc_putmsg timeout provided for the
* convenience of clients.
*/
clock_t
mboxsc_putmsg_def_timeout(void)
{
return (MBOXSC_PUTMSG_DEF_TIMEOUT_MSECS);
}
/*
* mboxsc_iosram_callback
*
* This routine is registered with the IOSRAM driver for all inbound mailboxes,
* and performs preliminary processing of all new messages.
*/
static void
mboxsc_iosram_callback(void *arg)
{
int error = 0;
uint8_t data_valid;
uint32_t key = (uint32_t)(uintptr_t)arg;
mboxsc_mbox_t *mailboxp;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_iosram_callback called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "arg = 0x%x\n", key);
mutex_enter(&mboxsc_lock);
mailboxp = mboxsc_hashfind_mailbox_by_key(key);
/*
* We shouldn't ever receive a callback for a mailbox that doesn't
* exist or for an output mailbox.
*/
ASSERT(mailboxp != NULL);
ASSERT(mailboxp->mbox_direction == MBOXSC_MBOX_IN);
/*
* Attempt to read the header of the mailbox. If the IOSRAM returns
* EAGAIN, indicating a tunnel switch is in progress, do not retry
* the operation.
*/
mailboxp->mbox_state &= ~STATE_HDRVALID;
error = iosram_rd(key, MBOXSC_MSGHDR_OFFSET, MBOXSC_MSGHDR_SIZE,
(caddr_t)&(mailboxp->mbox_header));
/*
* If somebody sends us a message using a Mailbox Protocol version
* greater than the highest one we understand, ignore the message,
* because we can't safely interpret anything beyond the version field.
*/
if (mailboxp->mbox_header.msg_version > MBOXSC_PROTOCOL_VERSION) {
error = -1;
DPRINTF1(DBG_DEV, DBGACT_DEFAULT,
"incoming message with unsupported version %d\n",
mailboxp->mbox_header.msg_version);
}
/*
* If this message is a repeat of a previous message (which should
* only happen with reply messages), it is conceivable that a client
* already executing in mboxsc_getmsg for the previous message could
* end up receiving the new message before this callback gets a chance
* to execute. If that happens, the data_valid flag will already have
* been cleared. Call iosram_get_flag to see if that is the case, and
* do not process the message if it is.
*/
if (error == 0) {
error = iosram_get_flag(key, &data_valid, NULL);
if ((error == 0) && (data_valid != IOSRAM_DATA_VALID)) {
error = -1;
}
}
/*
* If the iosram_rd call failed, return.
*/
if (error != 0) {
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_iosram_callback ret (0x%08x)\n", error);
return;
}
/*
* If the message read from IOSRAM was unsolicited, invoke
* its callback. Otherwise, wake all threads that are waiting
* in mboxsc_getmsg.
*/
mailboxp->mbox_state |= STATE_HDRVALID;
if (IS_UNSOLICITED_TYPE(mailboxp->mbox_header.msg_type) &&
(mailboxp->mbox_callback != NULL)) {
mboxsc_reference_mailbox(mailboxp);
mutex_exit(&mboxsc_lock);
(*(mailboxp->mbox_callback))();
mutex_enter(&mboxsc_lock);
mboxsc_dereference_mailbox(mailboxp);
} else {
cv_broadcast(&(mailboxp->mbox_wait));
}
mutex_exit(&mboxsc_lock);
DPRINTF0(DBG_RETS, DBGACT_DEFAULT, "mboxsc_iosram_callback ret\n");
}
/*
* mboxsc_hdrchange_callback
*
* This routine is registered with the IOSRAM driver to react to any changes SMS
* makes to the IOSRAM header.
*/
static void
mboxsc_hdrchange_callback(void)
{
int error;
uint32_t sms_version;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT,
"mboxsc_hdrchange_callback called\n");
error = iosram_hdr_ctrl(IOSRAM_HDRCMD_GET_SMS_MBOX_VER,
(void *)&sms_version);
if (error == 0) {
DPRINTF1(DBG_DEV, DBGACT_DEFAULT,
"sms mailbox version = %d\n", sms_version);
mboxsc_active_version = MIN(MBOXSC_PROTOCOL_VERSION,
sms_version);
}
DPRINTF0(DBG_RETS, DBGACT_DEFAULT, "mboxsc_hdrchange_callback ret\n");
}
/*
* mboxsc_add_mailbox
*
* If no other mailbox exists with the same key as this mailbox, attempt to
* retrieve its length from the IOSRAM driver and register the mboxsc callback
* for the associated IOSRAM chunk. If successful, initialize the
* non-client-supplied mailbox fields and insert it into the hash table.
* NOTE: The caller MUST hold mboxsc_lock to avoid corrupting the hash table.
*/
static int
mboxsc_add_mailbox(mboxsc_mbox_t *mailboxp)
{
int error = 0;
uint32_t key = mailboxp->mbox_key;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_add_mailbox called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "mailboxp = %p\n", (void *)mailboxp);
/*
* The global lock must be held by the caller.
*/
ASSERT(mutex_owned(&mboxsc_lock));
/*
* Don't create the mailbox if it already exists.
*/
if (mboxsc_hashfind_mailbox_by_key(key) != NULL) {
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_add_mailbox ret: 0x%08x\n", EEXIST);
return (EEXIST);
}
/*
* Obtain the mailbox length and register the mboxsc callback with the
* IOSRAM driver. If either call to the IOSRAM driver fails, or the
* chunk is too small to be used as a mailbox, return an error to the
* caller.
*/
error = iosram_ctrl(key, IOSRAM_CMD_CHUNKLEN, &(mailboxp->mbox_length));
if ((error == 0) && (mailboxp->mbox_length < MBOXSC_PROTOCOL_SIZE)) {
error = EFAULT;
}
if ((error == 0) && (mailboxp->mbox_direction == MBOXSC_MBOX_IN)) {
error = iosram_register(key, mboxsc_iosram_callback,
(void *)(uintptr_t)(key));
if (error == EBUSY) {
error = EFAULT;
}
}
if (error != 0) {
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_add_mailbox ret: 0x%08x\n", error);
return (error);
}
/*
* Initialize remaining mailbox fields and insert mailbox into
* hash table.
*/
mailboxp->mbox_state = STATE_IDLE;
mailboxp->mbox_refcount = 0;
cv_init(&(mailboxp->mbox_wait), NULL, CV_DRIVER, NULL);
mboxsc_hashinsert_mailbox(mailboxp);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "mboxsc_add_mailbox ret: 0x%08x\n",
0);
return (0);
}
/*
* mboxsc_close_mailbox
*
* Remove a mailbox from the hash table, unregister its IOSRAM callback, and
* deallocate its resources.
* NOTE: The caller MUST hold mboxsc_lock to avoid corrupting the hash table.
*/
static void
mboxsc_close_mailbox(mboxsc_mbox_t *mailboxp)
{
int error = 0;
uint32_t key = mailboxp->mbox_key;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_close_mailbox called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "mailboxp = %p\n", (void *)mailboxp);
/*
* The global lock must be held by the caller.
*/
ASSERT(mutex_owned(&mboxsc_lock));
/*
* Unregister the mboxsc callback for this particular mailbox.
*/
if (mailboxp->mbox_direction == MBOXSC_MBOX_IN) {
error = iosram_unregister(key);
if (error == EINVAL) {
DPRINTF1(DBG_DEV, DBGACT_DEFAULT, "invalid key (0x%08x)"
" reported in mboxsc_close_mailbox.\n", key);
error = 0;
}
}
/*
* Remove the mailbox from the hash table and deallocate its resources.
*/
(void) mboxsc_hashremove_mailbox_by_key(key);
cv_destroy(&(mailboxp->mbox_wait));
DPRINTF2(DBG_KMEM, DBGACT_DEFAULT, "kmem_free(%p, %lu)\n",
(void *)mailboxp, sizeof (mboxsc_mbox_t));
kmem_free(mailboxp, sizeof (mboxsc_mbox_t));
DPRINTF0(DBG_RETS, DBGACT_DEFAULT, "mboxsc_close_mailbox ret\n");
}
/*
* mboxsc_hashinsert_mailbox
*
* Insert a fully initialized mailbox into the hash table. No duplicate
* checking is performed at this point, so the caller is responsible for
* duplicate prevention if it is desired.
* NOTE: The caller MUST hold mboxsc_lock to avoid corrupting the hash table.
*/
static void
mboxsc_hashinsert_mailbox(mboxsc_mbox_t *mailboxp)
{
uint32_t hash;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT,
"mboxsc_hashinsert_mailbox called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "mailboxp = %p\n", (void *)mailboxp);
/*
* The global lock must be held by the caller.
*/
ASSERT(mutex_owned(&mboxsc_lock));
hash = HASH_KEY(mailboxp->mbox_key);
mailboxp->mbox_hash_next = mboxsc_hash_table[hash];
mboxsc_hash_table[hash] = mailboxp;
DPRINTF0(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_hashinsert_mailbox ret\n");
}
/*
* mboxsc_hashfind_mailbox_by_key
*
* Locate a mailbox with the given key in the hash table. Return a pointer
* to the mailbox if it exists, or NULL if no matching mailbox is found.
* NOTE: The caller MUST hold mboxsc_lock to avoid corrupting the hash table.
*/
static mboxsc_mbox_t *
mboxsc_hashfind_mailbox_by_key(uint32_t key)
{
uint32_t hash;
mboxsc_mbox_t *mailboxp;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT,
"mboxsc_hashfind_mailbox_by_key called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%x\n", key);
/*
* The global lock must be held by the caller.
*/
ASSERT(mutex_owned(&mboxsc_lock));
hash = HASH_KEY(key);
mailboxp = mboxsc_hash_table[hash];
while (mailboxp != NULL) {
if (mailboxp->mbox_key == key) {
break;
}
mailboxp = mailboxp->mbox_hash_next;
}
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_hashfind_mailbox_by_key ret: %p\n", (void *)mailboxp);
return (mailboxp);
}
/*
* mboxsc_hashremove_mailbox_by_key
*
* Locate a mailbox with the given key in the hash table. If it exists,
* remove it from the hash table and return a pointer to it. Otherwise,
* return NULL.
* NOTE: The caller MUST hold mboxsc_lock to avoid corrupting the hash table.
*/
static mboxsc_mbox_t *
mboxsc_hashremove_mailbox_by_key(uint32_t key)
{
uint32_t hash;
mboxsc_mbox_t *mailboxp;
mboxsc_mbox_t *last;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT,
"mboxsc_hashremove_mailbox_by_key called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%x\n", key);
/*
* The global lock must be held by the caller.
*/
ASSERT(mutex_owned(&mboxsc_lock));
hash = HASH_KEY(key);
mailboxp = mboxsc_hash_table[hash];
last = NULL;
while (mailboxp != NULL) {
if (mailboxp->mbox_key == key) {
break;
}
last = mailboxp;
mailboxp = mailboxp->mbox_hash_next;
}
/*
* If a mailbox was found, remove it from the hash table.
*/
if (mailboxp != NULL) {
if (last == NULL) {
mboxsc_hash_table[hash] = mailboxp->mbox_hash_next;
} else {
last->mbox_hash_next = mailboxp->mbox_hash_next;
}
mailboxp->mbox_hash_next = NULL;
}
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_hashremove_mailbox_by_key ret: %p\n", (void *)mailboxp);
return (mailboxp);
}
/*
* mboxsc_checksum
*
* Given a pointer to a data buffer and its length, calculate the checksum of
* the data contained therein.
*/
static mboxsc_chksum_t
mboxsc_checksum(mboxsc_chksum_t seed, uint8_t *buf, uint32_t length)
{
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_checksum called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "seed = 0x%x\n", seed);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "buf = %p\n", (void *)buf);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "length = 0x%x\n", length);
while (length-- > 0) {
seed += *(buf++);
}
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "mboxsc_checksum ret: 0x%08x\n",
seed);
return (seed);
}
/*
* mboxsc_lock_flags
*
* Acquire the hardware lock used for data_valid flag synchronization. If the
* lock is currently held by SMS and acquisition is mandatory, just keep on
* trying until it is acquired. If acquisition is not mandatory, keep trying
* until the given deadline has been reached. To avoid loading the system
* unreasonably on EBUSY or EAGAIN, sleep for an appropriate amount of time
* before retrying. If a hardware error is encountered return it to the caller.
*
* If the lock is held, but not by SMS, clear it and acquire it. Nobody
* else should be grabbing that lock.
*/
static int
mboxsc_lock_flags(uint8_t mandatory, clock_t deadline)
{
int error;
int warned = 0;
uint32_t sema;
clock_t pause;
clock_t warning_time = ddi_get_lbolt() + LOOP_WARN_INTERVAL;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_lock_flags called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "mandatory = 0x%x\n", mandatory);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "deadline = 0x%lx\n", deadline);
/*
* Keep trying to acquire the lock until successful or (if acquisition
* is not mandatory) time runs out. If EBUSY (lock is already held) or
* EAGAIN (tunnel switch in progress) is encountered, sleep for an
* appropriate amount of time before retrying. Any other error is
* unrecoverable.
*/
do {
pause = 0;
/*
* Since multiple threads could conceivably want the flag lock
* at the same time, we place the lock under a mutex and keep a
* counter indicating how many threads have the flags locked at
* the moment.
*/
mutex_enter(&mboxsc_lock);
if ((mboxsc_flaglock_count > 0) ||
((error = iosram_sema_acquire(&sema)) == 0)) {
mboxsc_flaglock_count++;
mutex_exit(&mboxsc_lock);
if (warned) {
cmn_err(CE_WARN, "Flags locked");
}
DPRINTF0(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_lock_flags ret: 0\n");
return (0);
}
/*
* If iosram_sema_acquire returned EBUSY (lock already held),
* make sure the lock is held by SMS, since nobody else should
* ever be holding it. If EBUSY or EAGAIN (tunnel switch in
* progress) was returned, determine the appropriate amount of
* time to sleep before trying again.
*/
if (error == EBUSY) {
if (IOSRAM_SEMA_GET_IDX(sema) != IOSRAM_SEMA_SMS_IDX) {
(void) iosram_sema_release();
cmn_err(CE_WARN,
"Incorrect flag lock value read (0x%08x)",
sema);
} else {
pause = (mandatory ? HWLOCK_POLL :
MIN(HWLOCK_POLL, deadline -
ddi_get_lbolt()));
}
} else if (error == EAGAIN) {
pause = (mandatory ? EAGAIN_POLL : MIN(EAGAIN_POLL,
deadline - ddi_get_lbolt()));
}
/*
* We had to hold the lock until now to protect the potential
* iosram_sema_release call above.
*/
mutex_exit(&mboxsc_lock);
/*
* If EAGAIN or EBUSY was encountered, we're looping.
*/
if ((error == EAGAIN) || (error == EBUSY)) {
/*
* If we've been looping here for a while, something is
* probably wrong, so we should generated a warning.
*/
if (warning_time - ddi_get_lbolt() <= 0) {
if (!warned) {
warned = 1;
cmn_err(CE_WARN,
"Unable to lock flags (0x%08x)",
error);
} else {
cmn_err(CE_WARN,
"Still unable to lock flags");
}
warning_time = ddi_get_lbolt() +
LOOP_WARN_INTERVAL;
}
/*
* Sleep a while before trying again.
*/
delay(pause);
}
} while (((error == EAGAIN) || (error == EBUSY)) &&
(mandatory || (deadline - ddi_get_lbolt() >= 0)));
/*
* If something really bad has happened, generate a warning.
*/
if ((error != EAGAIN) && (error != EBUSY)) {
cmn_err(CE_WARN, "Flag locking failed! (%d)", error);
}
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "mboxsc_lock_flags ret: 0x%08x\n",
error);
return (error);
}
/*
* mboxsc_unlock_flags
*
* Release the hardware lock used for data_valid flag synchronization.
* If a hardware error is encountered, return it to the caller. If the
* mandatory flag is set, loop and retry if EAGAIN is encountered.
*/
static int
mboxsc_unlock_flags(uint8_t mandatory)
{
int error;
int warned = 0;
clock_t warning_time = ddi_get_lbolt() + LOOP_WARN_INTERVAL;
ASSERT(mboxsc_flaglock_count != 0);
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_unlock_flags called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "mandatory = 0x%x\n", mandatory);
do {
/*
* Since multiple threads could conceivably want the flag lock
* at the same time, we place the lock under a mutex and keep a
* counter indicating how many threads have the flags locked at
* the moment.
*/
mutex_enter(&mboxsc_lock);
if ((mboxsc_flaglock_count > 1) ||
((error = iosram_sema_release()) == 0)) {
mboxsc_flaglock_count--;
mutex_exit(&mboxsc_lock);
if (warned) {
cmn_err(CE_WARN, "Flags unlocked");
}
DPRINTF0(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_unlock_flags ret: 0\n");
return (0);
}
mutex_exit(&mboxsc_lock);
/*
* If iosram_sema_release returned EAGAIN (tunnel switch in
* progress) and unlocking the flags is mandatory, sleep before
* trying again. If we've been trying for a while, display a
* warning message too.
*/
if ((error == EAGAIN) && mandatory) {
if (warning_time - ddi_get_lbolt() <= 0) {
if (!warned) {
warned = 1;
cmn_err(CE_WARN, "Unable to unlock "
"flags (iosram EAGAIN)");
} else {
cmn_err(CE_WARN,
"Still unable to unlock flags");
}
warning_time = ddi_get_lbolt() +
LOOP_WARN_INTERVAL;
}
delay(EAGAIN_POLL);
}
} while ((error == EAGAIN) && mandatory);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "mboxsc_unlock_flags ret: 0x%08x\n",
error);
return (error);
}
/*
* mboxsc_timed_read
*
* This function is just a wrapper around iosram_rd that will keep sleeping
* and retrying, up to a given deadline, if iosram_rd returns EAGAIN
* (presumably due to a tunnel switch).
*/
static int
mboxsc_timed_read(clock_t deadline, uint32_t key, uint32_t off, uint32_t len,
caddr_t dptr)
{
int error;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_timed_read called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "deadline = 0x%lx\n", deadline);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%x\n", key);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "off = 0x%x\n", off);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "len = 0x%x\n", len);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "dptr = %p\n", (void *)dptr);
do {
error = iosram_rd(key, off, len, dptr);
if (error == EAGAIN) {
delay(MIN(EAGAIN_POLL, deadline - ddi_get_lbolt()));
}
} while ((error == EAGAIN) && (deadline - ddi_get_lbolt() >= 0));
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_timed_read ret: 0x%08x\n", error);
return (error);
}
/*
* mboxsc_timed_write
*
* This function is just a wrapper around iosram_wr that will keep sleeping
* and retrying, up to a given deadline, if iosram_wr returns EAGAIN
* (presumably due to a tunnel switch).
*/
static int
mboxsc_timed_write(clock_t deadline, uint32_t key, uint32_t off, uint32_t len,
caddr_t dptr)
{
int error;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_timed_write called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "deadline = 0x%lx\n", deadline);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%x\n", key);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "off = 0x%x\n", off);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "len = 0x%x\n", len);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "dptr = %p\n", (void *)dptr);
do {
error = iosram_wr(key, off, len, dptr);
if (error == EAGAIN) {
delay(MIN(EAGAIN_POLL, deadline - ddi_get_lbolt()));
}
} while ((error == EAGAIN) && (deadline - ddi_get_lbolt() >= 0));
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_timed_write ret: 0x%08x\n", error);
return (error);
}
/*
* mboxsc_timed_get_flag
*
* This function is just a wrapper around iosram_get_flag that will keep
* sleeping and retrying, up to a given deadline, if iosram_get_flag returns
* EAGAIN (presumably due to a tunnel switch).
*/
static int
mboxsc_timed_get_flag(clock_t deadline, uint32_t key, uint8_t *data_validp,
uint8_t *int_pendingp)
{
int error;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_timed_get_flag called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "deadline = 0x%lx\n", deadline);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%x\n", key);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "data_validp = %p\n",
(void *)data_validp);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "int_pendingp = %p\n",
(void *)int_pendingp);
do {
error = iosram_get_flag(key, data_validp, int_pendingp);
if (error == EAGAIN) {
delay(MIN(EAGAIN_POLL, deadline - ddi_get_lbolt()));
}
} while ((error == EAGAIN) && (deadline - ddi_get_lbolt() >= 0));
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_timed_get_flag ret: 0x%08x\n", error);
return (error);
}
/*
* mboxsc_timed_set_flag
*
* This function is just a wrapper around iosram_set_flag that will keep
* sleeping and retrying, up to a given deadline, if iosram_set_flag returns
* EAGAIN (presumably due to a tunnel switch).
*/
static int
mboxsc_timed_set_flag(clock_t deadline, uint32_t key, uint8_t data_valid,
uint8_t int_pending)
{
int error;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_timed_set_flag called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "deadline = 0x%lx\n", deadline);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%x\n", key);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "data_valid = %d\n", data_valid);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "int_pending = %d\n", int_pending);
do {
error = iosram_set_flag(key, data_valid, int_pending);
if (error == EAGAIN) {
delay(MIN(EAGAIN_POLL, deadline - ddi_get_lbolt()));
}
} while ((error == EAGAIN) && (deadline - ddi_get_lbolt() >= 0));
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_timed_set_flag ret: 0x%08x\n", error);
return (error);
}
/*
* mboxsc_timed_send_intr
*
* This function is just a wrapper around iosram_send_intr that will keep
* sleeping and retrying, up to a given deadline, if iosram_send_intr returns
* EAGAIN (presumably due to a tunnel switch).
*/
static int
mboxsc_timed_send_intr(clock_t deadline)
{
int error;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_timed_send_intr called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "deadline = 0x%lx\n", deadline);
do {
error = iosram_send_intr();
if (error == DDI_FAILURE) {
delay(MIN(EAGAIN_POLL, deadline - ddi_get_lbolt()));
}
} while ((error == DDI_FAILURE) && (deadline - ddi_get_lbolt() >= 0));
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_timed_send_intr ret: 0x%08x\n", error);
return (error);
}
/*
* mboxsc_expire_message
*
* This function is called by mboxsc_putmsg to handle expiration of messages
* that weren't picked up before they timed out. It will not return until the
* message has been picked up (which isn't expected), the message has been
* successfully expired, or a serious error has been encountered. If the
* message is finally picked up, it will set the value pointed to by "resultp"
* to 0. Unlike other sections of code, this function will never time out on
* EAGAIN from the iosram driver, since it is important that both sides of the
* IOSRAM agree on whether or not a message was delivered successfully.
*/
static int
mboxsc_expire_message(uint32_t key, int *resultp)
{
int error = 0;
int lock_held = 0;
int warned = 0;
uint8_t data_valid;
clock_t warning_time = ddi_get_lbolt() + LOOP_WARN_INTERVAL;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_expire_message called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%x\n", key);
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "resultp = %p\n", (void *)resultp);
do {
error = 0;
/*
* Lock the flags if they aren't locked already.
*/
if (!lock_held) {
error = mboxsc_lock_flags(TRUE, 0);
if (error == 0) {
lock_held = 1;
}
}
/*
* If the flags were locked successfully, reread the data-valid
* flag.
*/
if (error == 0) {
error = iosram_get_flag(key, &data_valid, NULL);
}
/*
* If the data-valid flag was read successfully, see if it has
* been cleared or not, as the other side may have finally read
* the message.
*/
if (error == 0) {
if (data_valid == IOSRAM_DATA_INVALID) {
/*
* Surprise! The SC finally picked up the
* message, so delivery succeeded after all.
*/
if (*resultp == ETIMEDOUT) {
*resultp = 0;
}
} else {
/*
* The message still hasn't been read, so try to
* clear the data-valid flag.
*/
error = iosram_set_flag(key,
IOSRAM_DATA_INVALID, IOSRAM_INT_NONE);
}
}
/*
* If the flags were locked, unlock them, no matter what else
* has or has not succeeded. Don't overwrite the existing value
* of "error" unless no errors other than EAGAIN have been
* encountered previously. If we hit EAGAIN at some point,
* unlocking the flags here is optional. In all other cases, it
* is mandatory.
*/
if (lock_held) {
int unlock_err;
if (error == EAGAIN) {
unlock_err = mboxsc_unlock_flags(FALSE);
} else {
unlock_err = mboxsc_unlock_flags(TRUE);
}
if (unlock_err == 0) {
lock_held = 0;
} else if ((error == 0) || (error == EAGAIN)) {
error = unlock_err;
}
}
/*
* Did we hit a tunnel switch? (iosram driver returns EAGAIN)
* If so, sleep for a while before trying the whole process
* again.
*/
if (error == EAGAIN) {
/*
* If we've been stuck in this loop for a while,
* something is probably wrong, and we should display a
* warning.
*/
if (warning_time - ddi_get_lbolt() <= 0) {
if (!warned) {
warned = 1;
cmn_err(CE_WARN, "Unable to clear flag "
"(iosram EAGAIN)");
} else {
cmn_err(CE_WARN,
"Still unable to clear flag");
}
warning_time = ddi_get_lbolt() +
LOOP_WARN_INTERVAL;
}
delay(EAGAIN_POLL);
}
} while (error == EAGAIN);
/*
* If the data-valid flag was not successfully cleared due to some sort
* of problem, report it. Otherwise, if we looped for a while on EAGAIN
* and generated a warning about it, indicate that everything is okay
* now.
*/
if (error != 0) {
cmn_err(CE_WARN, "Message expiration failure! (%d)", error);
} else if (warned) {
cmn_err(CE_WARN, "Flag cleared");
}
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_expire_message ret: 0x%08x\n", error);
return (error);
}
/*
* mboxsc_generate_transid
*
* This function generates unique transaction IDs using an incrementing counter.
* The value generated is guaranteed not to be the same as the prev_transid
* value passed in by the caller.
*/
static uint64_t
mboxsc_generate_transid(uint64_t prev_transid)
{
uint64_t new_transid;
static uint64_t transid_counter = 0;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_generate_transid called");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "prev_transid = 0x%016lx\n",
prev_transid);
do {
new_transid = TRANSID_GEN_MASK | transid_counter++;
if (transid_counter & TRANSID_GEN_MASK) {
transid_counter = 0;
}
} while (new_transid == prev_transid);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"mboxsc_generate_transid ret: 0x%016lx", new_transid);
return (new_transid);
}
/*
* mboxsc_reference_mailbox
*
* Increment the mailbox's reference count to prevent it from being closed.
* This really doesn't deserve to be a function, but since a dereference
* function is needed, having a corresponding reference function makes the code
* clearer.
*/
static void
mboxsc_reference_mailbox(mboxsc_mbox_t *mailboxp)
{
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_reference_mailbox called");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "mailboxp = 0x%p\n",
(void *)mailboxp);
ASSERT(mutex_owned(&mboxsc_lock));
mailboxp->mbox_refcount++;
DPRINTF0(DBG_RETS, DBGACT_DEFAULT, "mboxsc_reference_mailbox ret");
}
/*
* mboxsc_dereference_mailbox
*
* Decrement the mailbox's reference count, and if the count has gone to zero,
* signal any threads waiting for mailboxes to be completely dereferenced.
*/
static void
mboxsc_dereference_mailbox(mboxsc_mbox_t *mailboxp)
{
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT,
"mboxsc_dereference_mailbox called");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "mailboxp = 0x%p\n",
(void *)mailboxp);
ASSERT(mutex_owned(&mboxsc_lock));
mailboxp->mbox_refcount--;
if (mailboxp->mbox_refcount == 0) {
cv_broadcast(&mboxsc_dereference_cv);
}
DPRINTF0(DBG_RETS, DBGACT_DEFAULT, "mboxsc_dereference_mailbox ret");
}
#ifndef DEBUG
/* ARGSUSED */
int
mboxsc_debug(int cmd, void *arg)
{
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_debug called");
DPRINTF0(DBG_RETS, DBGACT_DEFAULT, "mboxsc_debug ret");
return (ENOTSUP);
}
#else /* DEBUG */
static void print_hash_table(void);
static int print_mailbox_by_key(uint32_t key);
static void print_mailbox(mboxsc_mbox_t *mailboxp);
int
mboxsc_debug(int cmd, void *arg)
{
int error = 0;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "mboxsc_debug called\n");
switch (cmd) {
case MBOXSC_PRNMBOX:
error = print_mailbox_by_key((uint32_t)(uintptr_t)arg);
break;
case MBOXSC_PRNHASHTBL:
print_hash_table();
break;
case MBOXSC_SETDBGMASK:
mboxsc_debug_mask = (uint32_t)(uintptr_t)arg;
break;
default:
DPRINTF1(DBG_DEV, DBGACT_DEFAULT,
"Error: unknown mboxsc debug cmd (%d)\n", cmd);
error = ENOTTY;
break;
}
DPRINTF1(DBG_RETS, DBGACT_DEFAULT, "mboxsc_debug ret: 0x%08x\n", error);
return (error);
}
/*PRINTFLIKE5*/
static void
mboxsc_dprintf(
const char *file,
int line,
uint32_t class,
uint32_t action,
const char *fmt,
...)
{
int i;
char indent_buf[64];
char msg_buf[256];
va_list adx;
static uint32_t indent = 0;
if (action & DBGACT_SHOWPOS) {
cmn_err(CE_CONT, "%s at line %d:\n", file, line);
}
if (class & DBG_RETS) {
indent--;
}
if (class & mboxsc_debug_mask) {
indent_buf[0] = '\0';
for (i = 0; i < indent; i++) {
(void) strcat(indent_buf, " ");
}
va_start(adx, fmt);
(void) vsprintf(msg_buf, fmt, adx);
va_end(adx);
cmn_err(CE_CONT, "%s%s", indent_buf, msg_buf);
}
if (class & DBG_CALLS) {
indent++;
}
if (action & DBGACT_BREAK) {
debug_enter("");
}
}
static void
print_hash_table(void)
{
int i;
mboxsc_mbox_t *mailboxp;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "print_hash_table called\n");
mutex_enter(&mboxsc_lock);
for (i = 0; i < HASHTBL_SIZE; i++) {
DPRINTF1(DBG_DEV, DBGACT_DEFAULT, "hash[%02d]:\n", i);
for (mailboxp = mboxsc_hash_table[i]; mailboxp != NULL;
mailboxp = mailboxp->mbox_hash_next) {
DPRINTF2(DBG_DEV, DBGACT_DEFAULT,
" key: 0x%08x, dir: %d\n", mailboxp->mbox_key,
mailboxp->mbox_direction);
}
}
mutex_exit(&mboxsc_lock);
DPRINTF0(DBG_RETS, DBGACT_DEFAULT, "print_hash_table ret\n");
}
static int
print_mailbox_by_key(uint32_t key)
{
int error = 0;
mboxsc_mbox_t *mailboxp;
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "print_mailbox_by_key called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "key = 0x%08x\n", key);
mutex_enter(&mboxsc_lock);
mailboxp = mboxsc_hashfind_mailbox_by_key(key);
if (mailboxp != NULL) {
print_mailbox(mailboxp);
error = 0;
} else {
DPRINTF1(DBG_DEV, DBGACT_DEFAULT,
"print_mailbox_by_key: no such mbox 0x%08x\n", key);
error = EBADF;
}
mutex_exit(&mboxsc_lock);
DPRINTF1(DBG_RETS, DBGACT_DEFAULT,
"print_mailbox_by_key ret: 0x%08x\n", error);
return (error);
}
/* ARGSUSED */
static void
print_mailbox(mboxsc_mbox_t *mailboxp)
{
DPRINTF0(DBG_CALLS, DBGACT_DEFAULT, "print_mailbox called\n");
DPRINTF1(DBG_ARGS, DBGACT_DEFAULT, "mailboxp = %p\n",
(void *)mailboxp);
if (mailboxp->mbox_direction == MBOXSC_MBOX_IN) {
DPRINTF3(DBG_DEV, DBGACT_DEFAULT,
"key = 0x%08x, dir = %d, callback = %p\n",
mailboxp->mbox_key, mailboxp->mbox_direction,
(void *)mailboxp->mbox_callback);
} else {
DPRINTF2(DBG_DEV, DBGACT_DEFAULT, "key = 0x%08x, dir = %d\n",
(int)mailboxp->mbox_key, mailboxp->mbox_direction);
}
DPRINTF3(DBG_DEV, DBGACT_DEFAULT,
"length = %d, refcount = %d, state = %d\n",
mailboxp->mbox_length, mailboxp->mbox_refcount,
mailboxp->mbox_state);
/* LINTED E_BAD_FORMAT_ARG_TYPE2 */
DPRINTF2(DBG_DEV, DBGACT_DEFAULT, "waitcv = %p, hashnext = %p\n",
(void *)&mailboxp->mbox_wait, (void *)mailboxp->mbox_hash_next);
if (mailboxp->mbox_direction == MBOXSC_MBOX_IN) {
DPRINTF3(DBG_DEV, DBGACT_DEFAULT,
"hdr.type = 0x%x, hdr.cmd = 0x%x, hdr.len = 0x%x\n",
mailboxp->mbox_header.msg_type,
mailboxp->mbox_header.msg_cmd,
mailboxp->mbox_header.msg_length);
DPRINTF1(DBG_DEV, DBGACT_DEFAULT, "hdr.tid = 0x%016lx\n",
mailboxp->mbox_header.msg_transid);
}
DPRINTF0(DBG_RETS, DBGACT_DEFAULT, "print_mailbox ret\n");
}
#endif /* DEBUG */
|