1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
|
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 2014, Joyent, Inc. All rights reserved.
*/
#include <sys/types.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/uio.h>
#include <sys/open.h>
#include <sys/cred.h>
#include <sys/kmem.h>
#include <sys/conf.h>
#include <sys/cmn_err.h>
#include <sys/ksynch.h>
#include <sys/modctl.h>
#include <sys/stat.h> /* needed for S_IFBLK and S_IFCHR */
#include <sys/debug.h>
#include <sys/promif.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/cyclic.h>
#include <sys/termio.h>
#include <sys/intr.h>
#include <sys/ivintr.h>
#include <sys/note.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/sysmacros.h>
#include <sys/ldc.h>
#include <sys/mdeg.h>
#include <sys/vcc_impl.h>
#define VCC_LDC_RETRIES 5
#define VCC_LDC_DELAY 1000 /* usec */
/*
* Function prototypes.
*/
/* DDI entrypoints */
static int vcc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
static int vcc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
static int vcc_open(dev_t *devp, int flag, int otyp, cred_t *cred);
static int vcc_close(dev_t dev, int flag, int otyp, cred_t *cred);
static int vcc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
cred_t *credp, int *rvalp);
static int vcc_read(dev_t dev, struct uio *uiop, cred_t *credp);
static int vcc_write(dev_t dev, struct uio *uiop, cred_t *credp);
static int vcc_chpoll(dev_t dev, short events, int anyyet,
short *reventsp, struct pollhead **phpp);
static int vcc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
void *arg, void **resultp);
/* callback functions */
static uint_t vcc_ldc_cb(uint64_t event, caddr_t arg);
static int vcc_mdeg_cb(void *cb_argp, mdeg_result_t *resp);
/* Internal functions */
static int i_vcc_ldc_init(vcc_t *vccp, vcc_port_t *vport);
static int i_vcc_add_port(vcc_t *vccp, char *group_name, uint64_t tcp_port,
uint_t portno, char *domain_name);
static int i_vcc_config_port(vcc_t *vccp, uint_t portno, uint64_t ldc_id);
static int i_vcc_reset_events(vcc_t *vccp);
static int i_vcc_cons_tbl(vcc_t *vccp, uint_t num_ports,
caddr_t buf, int mode);
static int i_vcc_del_cons_ok(vcc_t *vccp, caddr_t buf, int mode);
static int i_vcc_close_port(vcc_port_t *vport);
static int i_vcc_write_ldc(vcc_port_t *vport, vcc_msg_t *buf);
static int i_vcc_read_ldc(vcc_port_t *vport, char *data_buf, size_t *sz);
static void *vcc_ssp;
static struct cb_ops vcc_cb_ops = {
vcc_open, /* open */
vcc_close, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
vcc_read, /* read */
vcc_write, /* write */
vcc_ioctl, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
ddi_segmap, /* segmap */
vcc_chpoll, /* chpoll */
ddi_prop_op, /* prop_op */
NULL, /* stream */
D_NEW | D_MP /* flags */
};
static struct dev_ops vcc_ops = {
DEVO_REV, /* rev */
0, /* ref count */
vcc_getinfo, /* getinfo */
nulldev, /* identify */
nulldev, /* probe */
vcc_attach, /* attach */
vcc_detach, /* detach */
nodev, /* reset */
&vcc_cb_ops, /* cb_ops */
(struct bus_ops *)NULL, /* bus_ops */
NULL, /* power */
ddi_quiesce_not_needed, /* quiesce */
};
extern struct mod_ops mod_driverops;
#define VCC_CHANNEL_ENDPOINT "channel-endpoint"
#define VCC_ID_PROP "id"
/*
* This is the string displayed by modinfo(8).
*/
static char vcc_ident[] = "sun4v Virtual Console Concentrator Driver";
static struct modldrv md = {
&mod_driverops, /* Type - it is a driver */
vcc_ident, /* Name of the module */
&vcc_ops, /* driver specfic opts */
};
static struct modlinkage ml = {
MODREV_1,
&md,
NULL
};
/*
* Matching criteria passed to the MDEG to register interest
* in changes to 'virtual-device-port' nodes identified by their
* 'id' property.
*/
static md_prop_match_t vcc_port_prop_match[] = {
{ MDET_PROP_VAL, "id" },
{ MDET_LIST_END, NULL }
};
static mdeg_node_match_t vcc_port_match = {"virtual-device-port",
vcc_port_prop_match};
/*
* Specification of an MD node passed to the MDEG to filter any
* 'virtual-device-port' nodes that do not belong to the specified node.
* This template is copied for each vldc instance and filled in with
* the appropriate 'cfg-handle' value before being passed to the MDEG.
*/
static mdeg_prop_spec_t vcc_prop_template[] = {
{ MDET_PROP_STR, "name", "virtual-console-concentrator" },
{ MDET_PROP_VAL, "cfg-handle", NULL },
{ MDET_LIST_END, NULL, NULL }
};
#define VCC_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
#ifdef DEBUG
/*
* Print debug messages
*
* set vldcdbg to 0xf to enable all messages
*
* 0x8 - Errors
* 0x4 - Warnings
* 0x2 - All debug messages (most verbose)
* 0x1 - Minimal debug messages
*/
int vccdbg = 0x8;
static void
vccdebug(const char *fmt, ...)
{
char buf[512];
va_list ap;
va_start(ap, fmt);
(void) vsprintf(buf, fmt, ap);
va_end(ap);
cmn_err(CE_CONT, "%s\n", buf);
}
#define D1 \
if (vccdbg & 0x01) \
vccdebug
#define D2 \
if (vccdbg & 0x02) \
vccdebug
#define DWARN \
if (vccdbg & 0x04) \
vccdebug
#else
#define D1
#define D2
#define DWARN
#endif
/* _init(9E): initialize the loadable module */
int
_init(void)
{
int error;
/* init the soft state structure */
error = ddi_soft_state_init(&vcc_ssp, sizeof (vcc_t), 1);
if (error != 0) {
return (error);
}
/* Link the driver into the system */
error = mod_install(&ml);
return (error);
}
/* _info(9E): return information about the loadable module */
int
_info(struct modinfo *modinfop)
{
/* Report status of the dynamically loadable driver module */
return (mod_info(&ml, modinfop));
}
/* _fini(9E): prepare the module for unloading. */
int
_fini(void)
{
int error;
/* Unlink the driver module from the system */
if ((error = mod_remove(&ml)) == 0) {
/*
* We have successfully "removed" the driver.
* destroy soft state
*/
ddi_soft_state_fini(&vcc_ssp);
}
return (error);
}
/* getinfo(9E) */
static int
vcc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
{
_NOTE(ARGUNUSED(dip))
int instance = VCCINST(getminor((dev_t)arg));
vcc_t *vccp = NULL;
switch (cmd) {
case DDI_INFO_DEVT2DEVINFO:
if ((vccp = ddi_get_soft_state(vcc_ssp, instance)) == NULL) {
*resultp = NULL;
return (DDI_FAILURE);
}
*resultp = vccp->dip;
return (DDI_SUCCESS);
case DDI_INFO_DEVT2INSTANCE:
*resultp = (void *)(uintptr_t)instance;
return (DDI_SUCCESS);
default:
*resultp = NULL;
return (DDI_FAILURE);
}
}
/*
* There are two cases that need special blocking. One of them is to block
* a minor node without a port and another is to block application other
* than vntsd.
*
* A minor node can exist in the file system without associated with a port
* because when a port is deleted, ddi_remove_minor does not unlink it.
* Clients might try to open a minor node even after the corresponding port
* node has been removed. To identify and block these calls,
* we need to validate the association between a port and its minor node.
*
* An application other than vntsd can access a console port as long
* as vntsd is not using the port. A port opened by an application other
* than vntsd will be closed when vntsd wants to use the port.
* However, other application could use same file descriptor
* access vcc cb_ops. So we need to identify and block caller other
* than vntsd, when vntsd is using the port.
*/
static int
i_vcc_can_use_port(vcc_minor_t *minorp, vcc_port_t *vport)
{
if (vport->minorp != minorp) {
/* port config changed */
return (ENXIO);
}
if (vport->valid_pid == VCC_NO_PID_BLOCKING) {
/* no blocking needed */
return (0);
}
if (vport->valid_pid != ddi_get_pid()) {
return (EIO);
}
return (0);
}
/* Syncronization between thread using cv_wait */
static int
i_vcc_wait_port_status(vcc_port_t *vport, kcondvar_t *cv, uint32_t status)
{
int rv;
ASSERT(mutex_owned(&vport->lock));
for (; ; ) {
if ((vport->status & VCC_PORT_AVAIL) == 0) {
/* port has been deleted */
D1("i_vcc_wait_port_status: port%d deleted\n",
vport->number);
return (EIO);
}
if ((vport->status & VCC_PORT_OPEN) == 0) {
D1("i_vcc_wait_port_status: port%d is closed \n",
vport->number);
return (EIO);
}
if (vport->status & VCC_PORT_LDC_LINK_DOWN) {
return (EIO);
}
if ((vport->valid_pid != VCC_NO_PID_BLOCKING) &&
(vport->valid_pid != ddi_get_pid())) {
return (EIO);
}
if ((vport->status & status) == status) {
return (0);
}
if (!ddi_can_receive_sig()) {
return (EIO);
}
rv = cv_wait_sig(cv, &vport->lock);
if (rv == 0) {
D1("i_vcc_wait_port_status: port%d get intr \n",
vport->number);
/* got signal */
return (EINTR);
}
}
}
/* Syncronization between threads, signal state change */
static void
i_vcc_set_port_status(vcc_port_t *vport, kcondvar_t *cv, uint32_t status)
{
mutex_enter(&vport->lock);
vport->status |= status;
cv_broadcast(cv);
mutex_exit(&vport->lock);
}
/* initialize a ldc channel */
static int
i_vcc_ldc_init(vcc_t *vccp, vcc_port_t *vport)
{
ldc_attr_t attr;
int rv = EIO;
ASSERT(mutex_owned(&vport->lock));
ASSERT(vport->ldc_id != VCC_INVALID_CHANNEL);
/* initialize the channel */
attr.devclass = LDC_DEV_SERIAL;
attr.instance = ddi_get_instance(vccp->dip);
attr.mtu = VCC_MTU_SZ;
attr.mode = LDC_MODE_RAW;
if ((rv = ldc_init(vport->ldc_id, &attr, &(vport->ldc_handle))) != 0) {
cmn_err(CE_CONT, "i_vcc_ldc_init: port %d ldc channel %ld"
" failed ldc_init %d \n", vport->number, vport->ldc_id, rv);
vport->ldc_id = VCC_INVALID_CHANNEL;
return (rv);
}
/* register it */
if ((rv = ldc_reg_callback(vport->ldc_handle, vcc_ldc_cb,
(caddr_t)vport)) != 0) {
cmn_err(CE_CONT, "i_vcc_ldc_init: port@%d ldc_register_cb"
"failed\n", vport->number);
(void) ldc_fini(vport->ldc_handle);
vport->ldc_id = VCC_INVALID_CHANNEL;
return (rv);
}
/* open and bring channel up */
if ((rv = ldc_open(vport->ldc_handle)) != 0) {
cmn_err(CE_CONT, "i_vcc_ldc_init: port@%d inv channel 0x%lx\n",
vport->number, vport->ldc_id);
(void) ldc_unreg_callback(vport->ldc_handle);
(void) ldc_fini(vport->ldc_handle);
vport->ldc_id = VCC_INVALID_CHANNEL;
return (rv);
}
/* init the channel status */
if ((rv = ldc_status(vport->ldc_handle, &vport->ldc_status)) != 0) {
cmn_err(CE_CONT, "i_vcc_ldc_init: port@%d ldc_status failed\n",
vport->number);
(void) ldc_close(vport->ldc_handle);
(void) ldc_unreg_callback(vport->ldc_handle);
(void) ldc_fini(vport->ldc_handle);
vport->ldc_id = VCC_INVALID_CHANNEL;
return (rv);
}
return (0);
}
/* release a ldc channel */
static void
i_vcc_ldc_fini(vcc_port_t *vport)
{
int rv = EIO;
vcc_msg_t buf;
size_t sz;
int retry = 0;
D1("i_vcc_ldc_fini: port@%lld, ldc_id%%llx\n", vport->number,
vport->ldc_id);
ASSERT(mutex_owned(&vport->lock));
/* wait for write available */
rv = i_vcc_wait_port_status(vport, &vport->write_cv,
VCC_PORT_USE_WRITE_LDC);
if (rv == 0) {
vport->status &= ~VCC_PORT_USE_WRITE_LDC;
/* send a HUP message */
buf.type = LDC_CONSOLE_CTRL;
buf.ctrl_msg = LDC_CONSOLE_HUP;
buf.size = 0;
/*
* ignore write error since we still want to clean up
* ldc channel.
*/
(void) i_vcc_write_ldc(vport, &buf);
mutex_exit(&vport->lock);
i_vcc_set_port_status(vport, &vport->write_cv,
VCC_PORT_USE_WRITE_LDC);
mutex_enter(&vport->lock);
}
/* flush ldc channel */
rv = i_vcc_wait_port_status(vport, &vport->read_cv,
VCC_PORT_USE_READ_LDC);
if (rv == 0) {
vport->status &= ~VCC_PORT_USE_READ_LDC;
do {
sz = sizeof (buf);
rv = i_vcc_read_ldc(vport, (char *)&buf, &sz);
} while (rv == 0 && sz > 0);
vport->status |= VCC_PORT_USE_READ_LDC;
}
/*
* ignore read error since we still want to clean up
* ldc channel.
*/
(void) ldc_set_cb_mode(vport->ldc_handle, LDC_CB_DISABLE);
/* close LDC channel - retry on EAGAIN */
while ((rv = ldc_close(vport->ldc_handle)) == EAGAIN) {
if (++retry > VCC_LDC_RETRIES) {
cmn_err(CE_CONT, "i_vcc_ldc_fini: cannot close channel"
" %ld\n", vport->ldc_id);
break;
}
drv_usecwait(VCC_LDC_DELAY);
}
if (rv == 0) {
(void) ldc_unreg_callback(vport->ldc_handle);
(void) ldc_fini(vport->ldc_handle);
} else {
/*
* Closing the LDC channel has failed. Ideally we should
* fail here but there is no Zeus level infrastructure
* to handle this. The MD has already been changed and
* we have to do the close. So we try to do as much
* clean up as we can.
*/
while (ldc_unreg_callback(vport->ldc_handle) == EAGAIN)
drv_usecwait(VCC_LDC_DELAY);
}
}
/* read data from ldc channel */
static int
i_vcc_read_ldc(vcc_port_t *vport, char *data_buf, size_t *sz)
{
int rv;
size_t size;
size_t space_left = *sz;
vcc_msg_t buf;
int i;
/* make sure holding read lock */
ASSERT((vport->status & VCC_PORT_USE_READ_LDC) == 0);
ASSERT(space_left >= VCC_MTU_SZ);
*sz = 0;
while (space_left >= VCC_MTU_SZ) {
size = sizeof (buf);
rv = ldc_read(vport->ldc_handle, (caddr_t)&buf, &size);
if (rv) {
return (rv);
}
/*
* FIXME: ldc_read should not reaturn 0 with
* either size == 0, buf.size == 0 or size < VCC_HDR_SZ
*/
if (size == 0) {
if (*sz > 0) {
return (0);
}
return (EAGAIN);
}
if (size < VCC_HDR_SZ) {
return (EIO);
}
/*
* only data is expected from console - otherwise
* return error
*/
if (buf.type != LDC_CONSOLE_DATA) {
return (EIO);
}
if (buf.size == 0) {
if (*sz > 0) {
return (0);
}
return (EAGAIN);
}
/* copy data */
for (i = 0; i < buf.size; i++, (*sz)++) {
data_buf[*sz] = buf.data[i];
}
space_left -= buf.size;
}
return (0);
}
/* callback from ldc */
static uint_t
vcc_ldc_cb(uint64_t event, caddr_t arg)
{
vcc_port_t *vport = (vcc_port_t *)arg;
boolean_t hasdata;
/*
* do not need to hold lock because if ldc calls back, the
* ldc_handle must be valid.
*/
D2("vcc_ldc_cb: callback invoked port=%d events=%llx\n",
vport->number, event);
/* check event from ldc */
if (event & LDC_EVT_WRITE) {
/* channel has space for write */
i_vcc_set_port_status(vport, &vport->write_cv,
VCC_PORT_LDC_WRITE_READY);
return (LDC_SUCCESS);
}
if (event & LDC_EVT_READ) {
/* channel has data for read */
(void) ldc_chkq(vport->ldc_handle, &hasdata);
if (!hasdata) {
/* data already read */
return (LDC_SUCCESS);
}
i_vcc_set_port_status(vport, &vport->read_cv,
VCC_PORT_LDC_DATA_READY);
return (LDC_SUCCESS);
}
if (event & LDC_EVT_DOWN) {
/* channel is down */
i_vcc_set_port_status(vport, &vport->write_cv,
VCC_PORT_LDC_LINK_DOWN);
cv_broadcast(&vport->read_cv);
}
return (LDC_SUCCESS);
}
/* configure a vcc port with ldc channel */
static int
i_vcc_config_port(vcc_t *vccp, uint_t portno, uint64_t ldc_id)
{
int rv = EIO;
vcc_port_t *vport;
if ((portno >= VCC_MAX_PORTS) || (portno == VCC_CONTROL_PORT)) {
cmn_err(CE_CONT, "i_vcc_config_port: invalid port number %d\n",
portno);
return (EINVAL);
}
vport = &(vccp->port[portno]);
if ((vport->status & VCC_PORT_AVAIL) == 0) {
cmn_err(CE_CONT, "i_vcc_config_port: port@%d does not exist\n",
portno);
return (EINVAL);
}
if (vport->ldc_id != VCC_INVALID_CHANNEL) {
cmn_err(CE_CONT, "i_vcc_config_port: port@%d channel already"
"configured\n", portno);
return (EINVAL);
}
mutex_enter(&vport->lock);
/* store the ldc ID */
vport->ldc_id = ldc_id;
/* check if someone has already opened this port */
if (vport->status & VCC_PORT_OPEN) {
if ((rv = i_vcc_ldc_init(vccp, vport)) != 0) {
mutex_exit(&vport->lock);
return (rv);
}
/* mark port as ready */
vport->status |= VCC_PORT_LDC_CHANNEL_READY;
cv_broadcast(&vport->read_cv);
cv_broadcast(&vport->write_cv);
}
mutex_exit(&vport->lock);
D1("i_vcc_config_port: port@%d ldc=%d, domain=%s",
vport->number, vport->ldc_id, vport->minorp->domain_name);
return (0);
}
/* add a vcc console port */
static int
i_vcc_add_port(vcc_t *vccp, char *group_name, uint64_t tcp_port,
uint_t portno, char *domain_name)
{
int instance;
int rv = MDEG_FAILURE;
minor_t minor;
vcc_port_t *vport;
uint_t minor_idx;
char name[MAXPATHLEN];
if ((portno >= VCC_MAX_PORTS) || (portno == VCC_CONTROL_PORT)) {
DWARN("i_vcc_add_port: invalid port number %d\n", portno);
return (MDEG_FAILURE);
}
vport = &(vccp->port[portno]);
if (vport->status & VCC_PORT_AVAIL) {
/* this port already exists */
cmn_err(CE_CONT, "i_vcc_add_port: invalid port - port@%d "
"exists\n", portno);
return (MDEG_FAILURE);
}
vport->number = portno;
vport->ldc_id = VCC_INVALID_CHANNEL;
if (domain_name == NULL) {
cmn_err(CE_CONT, "i_vcc_add_port: invalid domain name\n");
return (MDEG_FAILURE);
}
if (group_name == NULL) {
cmn_err(CE_CONT, "i_vcc_add_port: invalid group name\n");
return (MDEG_FAILURE);
}
/* look up minor number */
for (minor_idx = 0; minor_idx < vccp->minors_assigned; minor_idx++) {
if (strcmp(vccp->minor_tbl[minor_idx].domain_name,
domain_name) == 0) {
/* found previous assigned minor number */
break;
}
}
if (minor_idx == vccp->minors_assigned) {
/* end of lookup - assign new minor number */
if (minor_idx == VCC_MAX_PORTS) {
cmn_err(CE_CONT, "i_vcc_add_port:"
"too many minornodes (%d)\n",
minor_idx);
return (MDEG_FAILURE);
}
(void) strlcpy(vccp->minor_tbl[minor_idx].domain_name,
domain_name, MAXPATHLEN);
vccp->minors_assigned++;
}
vport->minorp = &vccp->minor_tbl[minor_idx];
vccp->minor_tbl[minor_idx].portno = portno;
(void) strlcpy(vport->group_name, group_name, MAXPATHLEN);
vport->tcp_port = tcp_port;
D1("i_vcc_add_port:@%d domain=%s, group=%s, tcp=%lld",
vport->number, vport->minorp->domain_name,
vport->group_name, vport->tcp_port);
/*
* Create a minor node. The minor number is
* (instance << VCC_INST_SHIFT) | minor_idx
*/
instance = ddi_get_instance(vccp->dip);
minor = (instance << VCC_INST_SHIFT) | (minor_idx);
(void) snprintf(name, MAXPATHLEN - 1, "%s%s", VCC_MINOR_NAME_PREFIX,
domain_name);
rv = ddi_create_minor_node(vccp->dip, name, S_IFCHR, minor,
DDI_NT_SERIAL, 0);
if (rv != DDI_SUCCESS) {
vccp->minors_assigned--;
return (MDEG_FAILURE);
}
mutex_enter(&vport->lock);
vport->status = VCC_PORT_AVAIL | VCC_PORT_ADDED;
mutex_exit(&vport->lock);
return (MDEG_SUCCESS);
}
/* delete a port */
static int
i_vcc_delete_port(vcc_t *vccp, vcc_port_t *vport)
{
char name[MAXPATHLEN];
int rv;
ASSERT(mutex_owned(&vport->lock));
if ((vport->status & VCC_PORT_AVAIL) == 0) {
D1("vcc_del_port port already deleted \n");
return (0);
}
if (vport->status & VCC_PORT_OPEN) {
/* do not block mdeg callback */
vport->valid_pid = VCC_NO_PID_BLOCKING;
rv = i_vcc_close_port(vport);
}
/* remove minor node */
(void) snprintf(name, MAXPATHLEN-1, "%s%s", VCC_MINOR_NAME_PREFIX,
vport->minorp->domain_name);
ddi_remove_minor_node(vccp->dip, name);
/* let read and write thread know */
cv_broadcast(&vport->read_cv);
cv_broadcast(&vport->write_cv);
vport->status = 0;
return (rv);
}
/* register callback to MDEG */
static int
i_vcc_mdeg_register(vcc_t *vccp, int instance)
{
mdeg_prop_spec_t *pspecp;
mdeg_node_spec_t *ispecp;
mdeg_handle_t mdeg_hdl;
int sz;
int rv;
/*
* Allocate and initialize a per-instance copy
* of the global property spec array that will
* uniquely identify this vcc instance.
*/
sz = sizeof (vcc_prop_template);
pspecp = kmem_alloc(sz, KM_SLEEP);
bcopy(vcc_prop_template, pspecp, sz);
VCC_SET_MDEG_PROP_INST(pspecp, instance);
/* initialize the complete prop spec structure */
ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
ispecp->namep = "virtual-device";
ispecp->specp = pspecp;
/* perform the registration */
rv = mdeg_register(ispecp, &vcc_port_match, vcc_mdeg_cb,
vccp, &mdeg_hdl);
if (rv != MDEG_SUCCESS) {
cmn_err(CE_CONT, "i_vcc_mdeg_register:"
"mdeg_register failed (%d)\n", rv);
kmem_free(ispecp, sizeof (mdeg_node_spec_t));
kmem_free(pspecp, sz);
return (DDI_FAILURE);
}
/* save off data that will be needed later */
vccp->md_ispecp = (void *)ispecp;
vccp->mdeg_hdl = mdeg_hdl;
return (0);
}
/* destroy all mutex from port table */
static void
i_vcc_cleanup_port_table(vcc_t *vccp)
{
int i;
vcc_port_t *vport;
for (i = 0; i < VCC_MAX_PORTS; i++) {
vport = &(vccp->port[i]);
mutex_destroy(&vport->lock);
cv_destroy(&vport->read_cv);
cv_destroy(&vport->write_cv);
}
}
/*
* attach(9E): attach a device to the system.
* called once for each instance of the device on the system.
*/
static int
vcc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int i, instance, inst;
int rv = DDI_FAILURE;
vcc_t *vccp;
minor_t minor;
vcc_port_t *vport;
switch (cmd) {
case DDI_ATTACH:
instance = ddi_get_instance(dip);
if (ddi_soft_state_zalloc(vcc_ssp, instance) != DDI_SUCCESS)
return (DDI_FAILURE);
vccp = ddi_get_soft_state(vcc_ssp, instance);
if (vccp == NULL) {
ddi_soft_state_free(vccp, instance);
return (ENXIO);
}
D1("vcc_attach: DDI_ATTACH instance=%d\n", instance);
/* initialize the mutex */
mutex_init(&vccp->lock, NULL, MUTEX_DRIVER, NULL);
mutex_enter(&vccp->lock);
vccp->dip = dip;
for (i = 0; i < VCC_MAX_PORTS; i++) {
vport = &(vccp->port[i]);
mutex_init(&vport->lock, NULL, MUTEX_DRIVER, NULL);
cv_init(&vport->read_cv, NULL, CV_DRIVER, NULL);
cv_init(&vport->write_cv, NULL, CV_DRIVER, NULL);
vport->valid_pid = VCC_NO_PID_BLOCKING;
}
vport = &vccp->port[VCC_CONTROL_PORT];
mutex_enter(&vport->lock);
vport->minorp = &vccp->minor_tbl[VCC_CONTROL_MINOR_IDX];
vport->status |= VCC_PORT_AVAIL;
/* create a minor node for vcc control */
minor = (instance << VCC_INST_SHIFT) | VCC_CONTROL_MINOR_IDX;
vccp->minor_tbl[VCC_CONTROL_PORT].portno =
VCC_CONTROL_MINOR_IDX;
rv = ddi_create_minor_node(vccp->dip, "ctl", S_IFCHR, minor,
DDI_NT_SERIAL, 0);
mutex_exit(&vport->lock);
if (rv != DDI_SUCCESS) {
cmn_err(CE_CONT, "vcc_attach: error"
"creating control minor node\n");
i_vcc_cleanup_port_table(vccp);
mutex_exit(&vccp->lock);
/* clean up soft state */
ddi_soft_state_free(vccp, instance);
return (DDI_FAILURE);
}
/* get the instance number by reading 'reg' property */
inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"reg", -1);
if (inst == -1) {
cmn_err(CE_CONT, "vcc_attach: vcc%d has no "
"'reg' property\n",
ddi_get_instance(dip));
i_vcc_cleanup_port_table(vccp);
/* remove minor */
ddi_remove_minor_node(vccp->dip, NULL);
/* clean up soft state */
mutex_exit(&vccp->lock);
ddi_soft_state_free(vccp, instance);
return (DDI_FAILURE);
}
/*
* Mdeg might invoke callback in the same call sequence
* if there is a domain port at the time of registration.
* Since the callback also grabs vcc->lock mutex, to avoid
* mutex reentry error, release the lock before registration
*/
mutex_exit(&vccp->lock);
/* register for notifications from Zeus */
rv = i_vcc_mdeg_register(vccp, inst);
if (rv != MDEG_SUCCESS) {
cmn_err(CE_CONT, "vcc_attach: error register to MD\n");
i_vcc_cleanup_port_table(vccp);
/* remove minor */
ddi_remove_minor_node(vccp->dip, NULL);
/* clean up soft state */
ddi_soft_state_free(vccp, instance);
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
case DDI_RESUME:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/*
* detach(9E): detach a device from the system.
*/
static int
vcc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
int i, instance;
vcc_t *vccp;
mdeg_node_spec_t *ispecp;
vcc_port_t *vport;
switch (cmd) {
case DDI_DETACH:
instance = ddi_get_instance(dip);
vccp = ddi_get_soft_state(vcc_ssp, instance);
if (vccp == NULL)
return (ENXIO);
D1("vcc_detach: DDI_DETACH instance=%d\n", instance);
mutex_enter(&vccp->lock);
/* unregister from MD event generator */
ASSERT(vccp->mdeg_hdl);
(void) mdeg_unregister(vccp->mdeg_hdl);
ispecp = (mdeg_node_spec_t *)vccp->md_ispecp;
ASSERT(ispecp);
kmem_free(ispecp->specp, sizeof (vcc_prop_template));
kmem_free(ispecp, sizeof (mdeg_node_spec_t));
/* remove minor nodes */
ddi_remove_minor_node(vccp->dip, NULL);
mutex_exit(&vccp->lock);
for (i = 0; i < VCC_MAX_PORTS; i++) {
vport = &vccp->port[i];
mutex_enter(&vport->lock);
if (i == VCC_CONTROL_PORT) {
if (vport->status & VCC_PORT_OPEN) {
(void) i_vcc_close_port(vport);
}
}
if ((vccp->port[i].status & VCC_PORT_AVAIL) &&
(i != VCC_CONTROL_PORT)) {
D1("vcc_detach: removing port port@%d\n", i);
(void) i_vcc_delete_port(vccp, vport);
}
mutex_exit(&vport->lock);
cv_destroy(&vport->read_cv);
cv_destroy(&vport->write_cv);
mutex_destroy(&vport->lock);
}
/* destroy mutex and free the soft state */
mutex_destroy(&vccp->lock);
ddi_soft_state_free(vcc_ssp, instance);
return (DDI_SUCCESS);
case DDI_SUSPEND:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/* cb_open */
static int
vcc_open(dev_t *devp, int flag, int otyp, cred_t *cred)
{
_NOTE(ARGUNUSED(otyp, cred))
int instance;
int rv = EIO;
minor_t minor;
uint_t portno;
vcc_t *vccp;
vcc_port_t *vport;
minor = getminor(*devp);
instance = VCCINST(minor);
vccp = ddi_get_soft_state(vcc_ssp, instance);
if (vccp == NULL) {
return (ENXIO);
}
portno = VCCPORT(vccp, minor);
vport = &(vccp->port[portno]);
mutex_enter(&vport->lock);
if ((vport->status & VCC_PORT_AVAIL) == 0) {
/* port may be removed */
mutex_exit(&vport->lock);
return (ENXIO);
}
if (vport->status & VCC_PORT_OPEN) {
/* only one open per port */
cmn_err(CE_CONT, "vcc_open: virtual-console-concentrator@%d:%d "
"is already open\n", instance, portno);
mutex_exit(&vport->lock);
return (EAGAIN);
}
/* check minor no and pid */
if ((rv = i_vcc_can_use_port(VCCMINORP(vccp, minor),
vport)) != 0) {
mutex_exit(&vport->lock);
return (rv);
}
if (portno == VCC_CONTROL_PORT) {
vport->status |= VCC_PORT_OPEN;
mutex_exit(&vport->lock);
return (0);
}
/*
* the port may just be added by mdeg callback and may
* not be configured yet.
*/
if (vport->ldc_id == VCC_INVALID_CHANNEL) {
mutex_exit(&vport->lock);
return (ENXIO);
}
/* check if channel has been initialized */
if ((vport->status & VCC_PORT_LDC_CHANNEL_READY) == 0) {
rv = i_vcc_ldc_init(vccp, vport);
if (rv) {
mutex_exit(&vport->lock);
return (EIO);
}
/* mark port as ready */
vport->status |= VCC_PORT_LDC_CHANNEL_READY;
}
vport->status |= VCC_PORT_USE_READ_LDC | VCC_PORT_USE_WRITE_LDC|
VCC_PORT_TERM_RD|VCC_PORT_TERM_WR|VCC_PORT_OPEN;
if ((flag & O_NONBLOCK) || (flag & O_NDELAY)) {
vport->status |= VCC_PORT_NONBLOCK;
}
mutex_exit(&vport->lock);
return (0);
}
/* close port */
static int
i_vcc_close_port(vcc_port_t *vport)
{
if ((vport->status & VCC_PORT_OPEN) == 0) {
return (0);
}
ASSERT(mutex_owned(&vport->lock));
if (vport->status & VCC_PORT_LDC_CHANNEL_READY) {
/* clean up ldc channel */
i_vcc_ldc_fini(vport);
vport->status &= ~VCC_PORT_LDC_CHANNEL_READY;
}
/* reset rd/wr suspends */
vport->status |= VCC_PORT_TERM_RD | VCC_PORT_TERM_WR;
vport->status &= ~VCC_PORT_NONBLOCK;
vport->status &= ~VCC_PORT_OPEN;
vport->valid_pid = VCC_NO_PID_BLOCKING;
/* signal any blocked read and write thread */
cv_broadcast(&vport->read_cv);
cv_broadcast(&vport->write_cv);
return (0);
}
/* cb_close */
static int
vcc_close(dev_t dev, int flag, int otyp, cred_t *cred)
{
_NOTE(ARGUNUSED(flag, otyp, cred))
int instance;
minor_t minor;
int rv = EIO;
uint_t portno;
vcc_t *vccp;
vcc_port_t *vport;
minor = getminor(dev);
instance = VCCINST(minor);
vccp = ddi_get_soft_state(vcc_ssp, instance);
if (vccp == NULL) {
return (ENXIO);
}
portno = VCCPORT(vccp, minor);
D1("vcc_close: closing virtual-console-concentrator@%d:%d\n",
instance, portno);
vport = &(vccp->port[portno]);
/*
* needs lock to provent i_vcc_delete_port, which is called by
* the mdeg callback, from closing port.
*/
mutex_enter(&vport->lock);
if ((vport->status & VCC_PORT_OPEN) == 0) {
mutex_exit(&vport->lock);
return (0);
}
if (portno == VCC_CONTROL_PORT) {
/*
* vntsd closes control port before it exits. There
* could be events still pending for vntsd.
*/
mutex_exit(&vport->lock);
rv = i_vcc_reset_events(vccp);
return (0);
}
/* check minor no and pid */
if ((rv = i_vcc_can_use_port(VCCMINORP(vccp, minor),
vport)) != 0) {
mutex_exit(&vport->lock);
return (rv);
}
rv = i_vcc_close_port(vport);
mutex_exit(&vport->lock);
return (rv);
}
/*
* ioctl VCC_CONS_TBL - vntsd allocates buffer according to return of
* VCC_NUM_PORTS. However, when vntsd requests for the console table, console
* ports could be deleted or added. parameter num_ports is number of structures
* that vntsd allocated for the table. If there are more ports than
* num_ports, set up to wakeup vntsd to add ports.
* If there less ports than num_ports, fill (-1) for cons_no to tell vntsd.
*/
static int
i_vcc_cons_tbl(vcc_t *vccp, uint_t num_ports, caddr_t buf, int mode)
{
vcc_console_t cons;
int i;
vcc_port_t *vport;
boolean_t notify_vntsd = B_FALSE;
char pathname[MAXPATHLEN];
(void) ddi_pathname(vccp->dip, pathname);
for (i = 0; i < VCC_MAX_PORTS; i++) {
vport = &vccp->port[i];
if (i == VCC_CONTROL_PORT) {
continue;
}
if ((vport->status & VCC_PORT_AVAIL) == 0) {
continue;
}
/* a port exists before vntsd becomes online */
mutex_enter(&vport->lock);
if (num_ports == 0) {
/* more ports than vntsd's buffer can hold */
vport->status |= VCC_PORT_ADDED;
notify_vntsd = B_TRUE;
mutex_exit(&vport->lock);
continue;
}
bzero(&cons, sizeof (vcc_console_t));
/* construct console buffer */
cons.cons_no = vport->number;
cons.tcp_port = vport->tcp_port;
(void) memcpy(cons.domain_name,
vport->minorp->domain_name, MAXPATHLEN);
(void) memcpy(cons.group_name, vport->group_name,
MAXPATHLEN);
vport->status &= ~VCC_PORT_ADDED;
mutex_exit(&vport->lock);
(void) snprintf(cons.dev_name, MAXPATHLEN-1, "%s:%s%s",
pathname, VCC_MINOR_NAME_PREFIX, cons.domain_name);
/* copy out data */
if (ddi_copyout(&cons, (void *)buf,
sizeof (vcc_console_t), mode)) {
mutex_exit(&vport->lock);
return (EFAULT);
}
buf += sizeof (vcc_console_t);
num_ports--;
}
if (num_ports == 0) {
/* vntsd's buffer is full */
if (notify_vntsd) {
/* more ports need to notify vntsd */
vport = &vccp->port[VCC_CONTROL_PORT];
mutex_enter(&vport->lock);
vport->pollevent |= VCC_POLL_ADD_PORT;
mutex_exit(&vport->lock);
}
return (0);
}
/* less ports than vntsd expected */
bzero(&cons, sizeof (vcc_console_t));
cons.cons_no = -1;
while (num_ports > 0) {
/* fill vntsd buffer with no console */
if (ddi_copyout(&cons, (void *)buf,
sizeof (vcc_console_t), mode) != 0) {
mutex_exit(&vport->lock);
return (EFAULT);
}
D1("i_vcc_cons_tbl: a port is deleted\n");
buf += sizeof (vcc_console_t) +MAXPATHLEN;
num_ports--;
}
return (0);
}
/* turn off event flag if there is no more change */
static void
i_vcc_turn_off_event(vcc_t *vccp, uint32_t port_status, uint32_t event)
{
vcc_port_t *vport;
int i;
for (i = 0; i < VCC_MAX_PORTS; i++) {
vport = &(vccp->port[i]);
if ((vport->status & VCC_PORT_AVAIL) == 0) {
continue;
}
if (vport->status & port_status) {
/* more port changes status */
return;
}
}
/* no more changed port */
vport = &vccp->port[VCC_CONTROL_PORT];
/* turn off event */
mutex_enter(&vport->lock);
vport->pollevent &= ~event;
mutex_exit(&vport->lock);
}
/* ioctl VCC_CONS_INFO */
static int
i_vcc_cons_info(vcc_t *vccp, caddr_t buf, int mode)
{
vcc_console_t cons;
uint_t portno;
vcc_port_t *vport;
char pathname[MAXPATHLEN];
/* read in portno */
if (ddi_copyin((void*)buf, &portno, sizeof (uint_t), mode)) {
return (EFAULT);
}
D1("i_vcc_cons_info@%d:\n", portno);
if ((portno >= VCC_MAX_PORTS) || (portno == VCC_CONTROL_PORT)) {
return (EINVAL);
}
vport = &vccp->port[portno];
if ((vport->status & VCC_PORT_AVAIL) == 0) {
return (EINVAL);
}
mutex_enter(&vport->lock);
vport->status &= ~VCC_PORT_ADDED;
/* construct configruation data */
bzero(&cons, sizeof (vcc_console_t));
cons.cons_no = vport->number;
cons.tcp_port = vport->tcp_port;
(void) memcpy(cons.domain_name, vport->minorp->domain_name, MAXPATHLEN);
(void) memcpy(cons.group_name, vport->group_name, MAXPATHLEN);
mutex_exit(&vport->lock);
(void) ddi_pathname(vccp->dip, pathname),
/* copy device name */
(void) snprintf(cons.dev_name, MAXPATHLEN-1, "%s:%s%s",
pathname, VCC_MINOR_NAME_PREFIX, cons.domain_name);
/* copy data */
if (ddi_copyout(&cons, (void *)buf,
sizeof (vcc_console_t), mode) != 0) {
mutex_exit(&vport->lock);
return (EFAULT);
}
D1("i_vcc_cons_info@%d:domain:%s serv:%s tcp@%lld %s\n",
cons.cons_no, cons.domain_name,
cons.group_name, cons.tcp_port, cons.dev_name);
i_vcc_turn_off_event(vccp, VCC_PORT_ADDED, VCC_POLL_ADD_PORT);
return (0);
}
/* response to vntsd inquiry ioctl call */
static int
i_vcc_inquiry(vcc_t *vccp, caddr_t buf, int mode)
{
vcc_port_t *vport;
uint_t i;
vcc_response_t msg;
vport = &(vccp->port[VCC_CONTROL_PORT]);
if ((vport->pollevent & VCC_POLL_ADD_PORT) == 0) {
return (EINVAL);
}
/* an added port */
D1("i_vcc_inquiry\n");
for (i = 0; i < VCC_MAX_PORTS; i++) {
if ((vccp->port[i].status & VCC_PORT_AVAIL) == 0) {
continue;
}
if (vccp->port[i].status & VCC_PORT_ADDED) {
/* port added */
msg.reason = VCC_CONS_ADDED;
msg.cons_no = i;
if (ddi_copyout((void *)&msg, (void *)buf,
sizeof (msg), mode) == -1) {
cmn_err(CE_CONT, "i_vcc_find_changed_port:"
"ddi_copyout"
" failed\n");
return (EFAULT);
}
return (0);
}
}
/* the added port was deleted before vntsd wakes up */
msg.reason = VCC_CONS_MISS_ADDED;
if (ddi_copyout((void *)&msg, (void *)buf,
sizeof (msg), mode) == -1) {
cmn_err(CE_CONT, "i_vcc_find_changed_port: ddi_copyout"
" failed\n");
return (EFAULT);
}
return (0);
}
/* clean up events after vntsd exits */
static int
i_vcc_reset_events(vcc_t *vccp)
{
uint_t i;
vcc_port_t *vport;
for (i = 0; i < VCC_MAX_PORTS; i++) {
vport = &(vccp->port[i]);
if ((vport->status & VCC_PORT_AVAIL) == 0) {
continue;
}
ASSERT(!mutex_owned(&vport->lock));
if (i == VCC_CONTROL_PORT) {
/* close control port */
mutex_enter(&vport->lock);
vport->status &= ~VCC_PORT_OPEN;
/* clean up poll events */
vport->pollevent = 0;
vport->pollflag = 0;
mutex_exit(&vport->lock);
continue;
}
if (vport->status & VCC_PORT_ADDED) {
/* pending added port event to vntsd */
mutex_enter(&vport->lock);
vport->status &= ~VCC_PORT_ADDED;
mutex_exit(&vport->lock);
}
}
vport = &vccp->port[VCC_CONTROL_PORT];
return (0);
}
/* ioctl VCC_FORCE_CLOSE */
static int
i_vcc_force_close(vcc_t *vccp, caddr_t buf, int mode)
{
uint_t portno;
vcc_port_t *vport;
int rv;
/* read in portno */
if (ddi_copyin((void*)buf, &portno, sizeof (uint_t), mode)) {
return (EFAULT);
}
D1("i_vcc_force_close@%d:\n", portno);
if ((portno >= VCC_MAX_PORTS) || (portno == VCC_CONTROL_PORT)) {
return (EINVAL);
}
vport = &vccp->port[portno];
if ((vport->status & VCC_PORT_AVAIL) == 0) {
return (EINVAL);
}
mutex_enter(&vport->lock);
rv = i_vcc_close_port(vport);
/* block callers other than vntsd */
vport->valid_pid = ddi_get_pid();
mutex_exit(&vport->lock);
return (rv);
}
/* ioctl VCC_CONS_STATUS */
static int
i_vcc_cons_status(vcc_t *vccp, caddr_t buf, int mode)
{
vcc_console_t console;
vcc_port_t *vport;
/* read in portno */
if (ddi_copyin((void*)buf, &console, sizeof (console), mode)) {
return (EFAULT);
}
D1("i_vcc_cons_status@%d:\n", console.cons_no);
if ((console.cons_no >= VCC_MAX_PORTS) ||
(console.cons_no == VCC_CONTROL_PORT)) {
return (EINVAL);
}
vport = &vccp->port[console.cons_no];
if ((vport->status & VCC_PORT_AVAIL) == 0) {
console.cons_no = -1;
} else if (strncmp(console.domain_name, vport->minorp->domain_name,
MAXPATHLEN)) {
console.cons_no = -1;
} else if (strncmp(console.group_name, vport->group_name,
MAXPATHLEN)) {
console.cons_no = -1;
} else if (console.tcp_port != vport->tcp_port) {
console.cons_no = -1;
} else if (vport->ldc_id == VCC_INVALID_CHANNEL) {
console.cons_no = -1;
}
D1("i_vcc_cons_status@%d: %s %s %llx\n", console.cons_no,
console.group_name, console.domain_name, console.tcp_port);
if (ddi_copyout(&console, (void *)buf, sizeof (console), mode) == -1) {
cmn_err(CE_CONT, "i_vcc_cons_status ddi_copyout failed\n");
return (EFAULT);
}
return (0);
}
/* cb_ioctl handler for vcc control port */
static int
i_vcc_ctrl_ioctl(vcc_t *vccp, int cmd, void* arg, int mode)
{
static uint_t num_ports;
switch (cmd) {
case VCC_NUM_CONSOLE:
mutex_enter(&vccp->lock);
num_ports = vccp->num_ports;
mutex_exit(&vccp->lock);
/* number of consoles */
return (ddi_copyout((void *)&num_ports, arg,
sizeof (int), mode));
case VCC_CONS_TBL:
/* console config table */
return (i_vcc_cons_tbl(vccp, num_ports, (caddr_t)arg, mode));
case VCC_INQUIRY:
/* reason for wakeup */
return (i_vcc_inquiry(vccp, (caddr_t)arg, mode));
case VCC_CONS_INFO:
/* a console config */
return (i_vcc_cons_info(vccp, (caddr_t)arg, mode));
case VCC_FORCE_CLOSE:
/* force to close a console */
return (i_vcc_force_close(vccp, (caddr_t)arg, mode));
case VCC_CONS_STATUS:
/* console status */
return (i_vcc_cons_status(vccp, (caddr_t)arg, mode));
default:
/* unknown command */
return (ENODEV);
}
}
/* write data to ldc. may block if channel has no space for write */
static int
i_vcc_write_ldc(vcc_port_t *vport, vcc_msg_t *buf)
{
int rv = EIO;
size_t size;
ASSERT(mutex_owned(&vport->lock));
ASSERT((vport->status & VCC_PORT_USE_WRITE_LDC) == 0);
for (; ; ) {
size = VCC_HDR_SZ + buf->size;
rv = ldc_write(vport->ldc_handle, (caddr_t)buf, &size);
D1("i_vcc_write_ldc: port@%d: err=%d %d bytes\n",
vport->number, rv, size);
if (rv == 0) {
return (rv);
}
if (rv != EWOULDBLOCK) {
return (EIO);
}
if (vport->status & VCC_PORT_NONBLOCK) {
return (EAGAIN);
}
/* block util ldc has more space */
rv = i_vcc_wait_port_status(vport, &vport->write_cv,
VCC_PORT_LDC_WRITE_READY);
if (rv) {
return (rv);
}
vport->status &= ~VCC_PORT_LDC_WRITE_READY;
}
}
/* cb_ioctl handler for port ioctl */
static int
i_vcc_port_ioctl(vcc_t *vccp, minor_t minor, int portno, int cmd, void *arg,
int mode)
{
vcc_port_t *vport;
struct termios term;
vcc_msg_t buf;
int rv;
D1("i_vcc_port_ioctl@%d cmd %d\n", portno, cmd);
vport = &(vccp->port[portno]);
if ((vport->status & VCC_PORT_AVAIL) == 0) {
return (EIO);
}
switch (cmd) {
/* terminal support */
case TCGETA:
case TCGETS:
mutex_enter(&vport->lock);
/* check minor no and pid */
if ((rv = i_vcc_can_use_port(VCCMINORP(vccp, minor),
vport)) != 0) {
mutex_exit(&vport->lock);
return (rv);
}
(void) memcpy(&term, &vport->term, sizeof (term));
mutex_exit(&vport->lock);
return (ddi_copyout(&term, arg, sizeof (term), mode));
case TCSETS:
case TCSETA:
case TCSETAW:
case TCSETAF:
if (ddi_copyin(arg, &term, sizeof (term), mode) != 0) {
return (EFAULT);
}
mutex_enter(&vport->lock);
/* check minor no and pid */
if ((rv = i_vcc_can_use_port(VCCMINORP(vccp, minor),
vport)) != 0) {
mutex_exit(&vport->lock);
return (rv);
}
(void) memcpy(&vport->term, &term, sizeof (term));
mutex_exit(&vport->lock);
return (0);
case TCSBRK:
/* send break to console */
mutex_enter(&vport->lock);
/* check minor no and pid */
if ((rv = i_vcc_can_use_port(VCCMINORP(vccp, minor),
vport)) != 0) {
mutex_exit(&vport->lock);
return (rv);
}
/* wait for write available */
rv = i_vcc_wait_port_status(vport, &vport->write_cv,
VCC_PORT_LDC_CHANNEL_READY| VCC_PORT_USE_WRITE_LDC);
if (rv) {
mutex_exit(&vport->lock);
return (rv);
}
vport->status &= ~VCC_PORT_USE_WRITE_LDC;
buf.type = LDC_CONSOLE_CTRL;
buf.ctrl_msg = LDC_CONSOLE_BREAK;
buf.size = 0;
rv = i_vcc_write_ldc(vport, &buf);
mutex_exit(&vport->lock);
i_vcc_set_port_status(vport, &vport->write_cv,
VCC_PORT_USE_WRITE_LDC);
return (0);
case TCXONC:
/* suspend read or write */
if (ddi_copyin(arg, &cmd, sizeof (int), mode) != 0) {
return (EFAULT);
}
mutex_enter(&vport->lock);
/* check minor no and pid */
if ((rv = i_vcc_can_use_port(VCCMINORP(vccp, minor),
vport)) != 0) {
mutex_exit(&vport->lock);
return (rv);
}
switch (cmd) {
case 0:
/* suspend read */
vport->status &= ~VCC_PORT_TERM_RD;
break;
case 1:
/* resume read */
vport->status |= VCC_PORT_TERM_RD;
cv_broadcast(&vport->read_cv);
break;
case 2:
/* suspend write */
vport->status &= ~VCC_PORT_TERM_WR;
break;
case 3:
/* resume write */
vport->status |= VCC_PORT_TERM_WR;
cv_broadcast(&vport->write_cv);
break;
default:
mutex_exit(&vport->lock);
return (EINVAL);
}
mutex_exit(&vport->lock);
return (0);
case TCFLSH:
return (0);
default:
return (EINVAL);
}
}
/* cb_ioctl */
static int
vcc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
cred_t *credp, int *rvalp)
{
_NOTE(ARGUNUSED(credp, rvalp))
int instance;
minor_t minor;
int portno;
vcc_t *vccp;
minor = getminor(dev);
instance = VCCINST(minor);
vccp = ddi_get_soft_state(vcc_ssp, instance);
if (vccp == NULL) {
return (ENXIO);
}
portno = VCCPORT(vccp, minor);
D1("vcc_ioctl: virtual-console-concentrator@%d:%d\n", instance, portno);
if (portno >= VCC_MAX_PORTS) {
cmn_err(CE_CONT, "vcc_ioctl:virtual-console-concentrator@%d"
" invalid portno\n", portno);
return (EINVAL);
}
D1("vcc_ioctl: virtual-console-concentrator@%d:%d ioctl cmd=%d\n",
instance, portno, cmd);
if (portno == VCC_CONTROL_PORT) {
/* control ioctl */
return (i_vcc_ctrl_ioctl(vccp, cmd, (void *)arg, mode));
}
/* data port ioctl */
return (i_vcc_port_ioctl(vccp, minor, portno, cmd, (void *)arg, mode));
}
/* cb_read */
static int
vcc_read(dev_t dev, struct uio *uiop, cred_t *credp)
{
_NOTE(ARGUNUSED(credp))
int instance;
minor_t minor;
uint_t portno;
vcc_t *vccp;
vcc_port_t *vport;
int rv = EIO; /* by default fail ! */
char *buf;
size_t uio_size;
size_t size;
minor = getminor(dev);
instance = VCCINST(minor);
vccp = ddi_get_soft_state(vcc_ssp, instance);
if (vccp == NULL) {
return (ENXIO);
}
portno = VCCPORT(vccp, minor);
/* no read for control port */
if (portno == VCC_CONTROL_PORT) {
return (EIO);
}
/* temp buf to hold ldc data */
uio_size = uiop->uio_resid;
if (uio_size < VCC_MTU_SZ) {
return (EINVAL);
}
vport = &(vccp->port[portno]);
mutex_enter(&vport->lock);
/* check minor no and pid */
if ((rv = i_vcc_can_use_port(VCCMINORP(vccp, minor),
vport)) != 0) {
mutex_exit(&vport->lock);
return (rv);
}
rv = i_vcc_wait_port_status(vport, &vport->read_cv,
VCC_PORT_TERM_RD|VCC_PORT_LDC_CHANNEL_READY|
VCC_PORT_USE_READ_LDC);
if (rv) {
mutex_exit(&vport->lock);
return (rv);
}
buf = kmem_alloc(uio_size, KM_SLEEP);
vport->status &= ~VCC_PORT_USE_READ_LDC;
for (; ; ) {
size = uio_size;
rv = i_vcc_read_ldc(vport, buf, &size);
if (rv == EAGAIN) {
/* should block? */
if (vport->status & VCC_PORT_NONBLOCK) {
break;
}
} else if (rv) {
/* error */
break;
}
if (size > 0) {
/* got data */
break;
}
/* wait for data from ldc */
vport->status &= ~VCC_PORT_LDC_DATA_READY;
mutex_exit(&vport->lock);
i_vcc_set_port_status(vport, &vport->read_cv,
VCC_PORT_USE_READ_LDC);
mutex_enter(&vport->lock);
rv = i_vcc_wait_port_status(vport, &vport->read_cv,
VCC_PORT_TERM_RD|VCC_PORT_LDC_CHANNEL_READY|
VCC_PORT_USE_READ_LDC| VCC_PORT_LDC_DATA_READY);
if (rv) {
break;
}
vport->status &= ~VCC_PORT_USE_READ_LDC;
}
mutex_exit(&vport->lock);
if ((rv == 0) && (size > 0)) {
/* data is in buf */
rv = uiomove(buf, size, UIO_READ, uiop);
}
kmem_free(buf, uio_size);
i_vcc_set_port_status(vport, &vport->read_cv, VCC_PORT_USE_READ_LDC);
return (rv);
}
/* cb_write */
static int
vcc_write(dev_t dev, struct uio *uiop, cred_t *credp)
{
_NOTE(ARGUNUSED(credp))
int instance;
minor_t minor;
size_t size;
size_t bytes;
uint_t portno;
vcc_t *vccp;
vcc_port_t *vport;
int rv = EIO;
vcc_msg_t buf;
minor = getminor(dev);
instance = VCCINST(minor);
vccp = ddi_get_soft_state(vcc_ssp, instance);
if (vccp == NULL) {
return (ENXIO);
}
portno = VCCPORT(vccp, minor);
/* no write for control port */
if (portno == VCC_CONTROL_PORT) {
return (EIO);
}
vport = &(vccp->port[portno]);
/*
* check if the channel has been configured,
* if write has been suspend and grab write lock.
*/
mutex_enter(&vport->lock);
/* check minor no and pid */
if ((rv = i_vcc_can_use_port(VCCMINORP(vccp, minor),
vport)) != 0) {
mutex_exit(&vport->lock);
return (rv);
}
rv = i_vcc_wait_port_status(vport, &vport->write_cv,
VCC_PORT_TERM_WR|VCC_PORT_LDC_CHANNEL_READY|
VCC_PORT_USE_WRITE_LDC);
if (rv) {
mutex_exit(&vport->lock);
return (rv);
}
vport->status &= ~VCC_PORT_USE_WRITE_LDC;
mutex_exit(&vport->lock);
size = uiop->uio_resid;
D2("vcc_write: virtual-console-concentrator@%d:%d writing %d bytes\n",
instance, portno, size);
buf.type = LDC_CONSOLE_DATA;
while (size) {
bytes = MIN(size, VCC_MTU_SZ);
/* move data */
rv = uiomove(&(buf.data), bytes, UIO_WRITE, uiop);
if (rv) {
break;
}
/* write to ldc */
buf.size = bytes;
mutex_enter(&vport->lock);
/* check minor no and pid */
if ((rv = i_vcc_can_use_port(VCCMINORP(vccp, minor),
vport)) != 0) {
mutex_exit(&vport->lock);
return (rv);
}
rv = i_vcc_write_ldc(vport, &buf);
mutex_exit(&vport->lock);
if (rv) {
break;
}
size -= bytes;
}
i_vcc_set_port_status(vport, &vport->write_cv, VCC_PORT_USE_WRITE_LDC);
return (rv);
}
/* mdeg callback for a removed port */
static int
i_vcc_md_remove_port(md_t *mdp, mde_cookie_t mdep, vcc_t *vccp)
{
uint64_t portno; /* md requires 64bit for port number */
int rv = MDEG_FAILURE;
vcc_port_t *vport;
if (md_get_prop_val(mdp, mdep, "id", &portno)) {
cmn_err(CE_CONT, "vcc_mdeg_cb: port has no 'id' property\n");
return (MDEG_FAILURE);
}
if ((portno >= VCC_MAX_PORTS) || (portno < 0)) {
cmn_err(CE_CONT, "i_vcc_md_remove_port@%ld invalid port no\n",
portno);
return (MDEG_FAILURE);
}
if (portno == VCC_CONTROL_PORT) {
cmn_err(CE_CONT, "i_vcc_md_remove_port@%ld can not remove"
"control port\n",
portno);
return (MDEG_FAILURE);
}
vport = &(vccp->port[portno]);
/* delete the port */
mutex_enter(&vport->lock);
rv = i_vcc_delete_port(vccp, vport);
mutex_exit(&vport->lock);
mutex_enter(&vccp->lock);
vccp->num_ports--;
mutex_exit(&vccp->lock);
return (rv ? MDEG_FAILURE : MDEG_SUCCESS);
}
static int
i_vcc_get_ldc_id(md_t *md, mde_cookie_t mdep, uint64_t *ldc_id)
{
int num_nodes;
size_t size;
mde_cookie_t *channel;
int num_channels;
if ((num_nodes = md_node_count(md)) <= 0) {
cmn_err(CE_CONT, "i_vcc_get_ldc_channel_id:"
" Invalid node count in Machine Description subtree");
return (-1);
}
size = num_nodes*(sizeof (*channel));
channel = kmem_zalloc(size, KM_SLEEP);
ASSERT(channel != NULL); /* because KM_SLEEP */
/* Look for channel endpoint child(ren) of the vdisk MD node */
if ((num_channels = md_scan_dag(md, mdep,
md_find_name(md, "channel-endpoint"),
md_find_name(md, "fwd"), channel)) <= 0) {
cmn_err(CE_CONT, "i_vcc_get_ldc_id: No 'channel-endpoint'"
" found for vcc");
kmem_free(channel, size);
return (-1);
}
/* Get the "id" value for the first channel endpoint node */
if (md_get_prop_val(md, channel[0], "id", ldc_id) != 0) {
cmn_err(CE_CONT, "i_vcc_get_ldc: No id property found "
"for channel-endpoint of vcc");
kmem_free(channel, size);
return (-1);
}
if (num_channels > 1) {
cmn_err(CE_CONT, "i_vcc_get_ldc: Warning: Using ID of first"
" of multiple channels for this vcc");
}
kmem_free(channel, size);
return (0);
}
/* mdeg callback for an added port */
static int
i_vcc_md_add_port(md_t *mdp, mde_cookie_t mdep, vcc_t *vccp)
{
uint64_t portno; /* md requires 64 bit */
char *domain_name;
char *group_name;
uint64_t ldc_id;
uint64_t tcp_port;
vcc_port_t *vport;
/* read in the port's reg property */
if (md_get_prop_val(mdp, mdep, "id", &portno)) {
cmn_err(CE_CONT, "i_vcc_md_add_port_: port has no 'id' "
"property\n");
return (MDEG_FAILURE);
}
/* read in the port's "vcc-doman-name" property */
if (md_get_prop_str(mdp, mdep, "vcc-domain-name", &domain_name)) {
cmn_err(CE_CONT, "i_vcc_md_add_port: port%ld has "
"no 'vcc-domain-name' property\n", portno);
return (MDEG_FAILURE);
}
/* read in the port's "vcc-group-name" property */
if (md_get_prop_str(mdp, mdep, "vcc-group-name", &group_name)) {
cmn_err(CE_CONT, "i_vcc_md_add_port: port%ld has no "
"'vcc-group-name'property\n", portno);
return (MDEG_FAILURE);
}
/* read in the port's "vcc-tcp-port" property */
if (md_get_prop_val(mdp, mdep, "vcc-tcp-port", &tcp_port)) {
cmn_err(CE_CONT, "i_vcc_md_add_port: port%ld has no"
"'vcc-tcp-port' property\n", portno);
return (MDEG_FAILURE);
}
D1("i_vcc_md_add_port: port@%d domain-name=%s group-name=%s"
" tcp-port=%lld\n", portno, domain_name, group_name, tcp_port);
/* add the port */
if (i_vcc_add_port(vccp, group_name, tcp_port, portno, domain_name)) {
return (MDEG_FAILURE);
}
vport = &vccp->port[portno];
if (i_vcc_get_ldc_id(mdp, mdep, &ldc_id)) {
mutex_enter(&vport->lock);
(void) i_vcc_delete_port(vccp, vport);
mutex_exit(&vport->lock);
return (MDEG_FAILURE);
}
/* configure the port */
if (i_vcc_config_port(vccp, portno, ldc_id)) {
mutex_enter(&vport->lock);
(void) i_vcc_delete_port(vccp, vport);
mutex_exit(&vport->lock);
return (MDEG_FAILURE);
}
mutex_enter(&vccp->lock);
vccp->num_ports++;
mutex_exit(&vccp->lock);
vport = &vccp->port[VCC_CONTROL_PORT];
if (vport->pollflag & VCC_POLL_CONFIG) {
/* wakeup vntsd */
mutex_enter(&vport->lock);
vport->pollevent |= VCC_POLL_ADD_PORT;
mutex_exit(&vport->lock);
pollwakeup(&vport->poll, POLLIN);
}
return (MDEG_SUCCESS);
}
/* mdeg callback */
static int
vcc_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
{
int idx;
vcc_t *vccp;
int rv;
vccp = (vcc_t *)cb_argp;
ASSERT(vccp);
if (resp == NULL) {
return (MDEG_FAILURE);
}
/* added port */
D1("vcc_mdeg_cb: added %d port(s)\n", resp->added.nelem);
for (idx = 0; idx < resp->added.nelem; idx++) {
rv = i_vcc_md_add_port(resp->added.mdp,
resp->added.mdep[idx], vccp);
if (rv != MDEG_SUCCESS) {
return (rv);
}
}
/* removed port */
D1("vcc_mdeg_cb: removed %d port(s)\n", resp->removed.nelem);
for (idx = 0; idx < resp->removed.nelem; idx++) {
rv = i_vcc_md_remove_port(resp->removed.mdp,
resp->removed.mdep[idx], vccp);
if (rv != MDEG_SUCCESS) {
return (rv);
}
}
/*
* XXX - Currently no support for updating already active
* ports. So, ignore the match_curr and match_prev arrays
* for now.
*/
return (MDEG_SUCCESS);
}
/* cb_chpoll */
static int
vcc_chpoll(dev_t dev, short events, int anyyet, short *reventsp,
struct pollhead **phpp)
{
int instance;
minor_t minor;
uint_t portno;
vcc_t *vccp;
vcc_port_t *vport;
minor = getminor(dev);
instance = VCCINST(minor);
vccp = ddi_get_soft_state(vcc_ssp, instance);
if (vccp == NULL) {
return (ENXIO);
}
portno = VCCPORT(vccp, minor);
vport = &(vccp->port[portno]);
D1("vcc_chpoll: virtual-console-concentrator@%d events 0x%x\n",
portno, events);
*reventsp = 0;
if (portno != VCC_CONTROL_PORT) {
return (ENXIO);
}
/* poll for config change */
if (vport->pollevent) {
*reventsp |= (events & POLLIN);
}
if ((((*reventsp) == 0) && (!anyyet)) || (events & POLLET)) {
*phpp = &vport->poll;
if (events & POLLIN) {
mutex_enter(&vport->lock);
vport->pollflag |= VCC_POLL_CONFIG;
mutex_exit(&vport->lock);
} else {
return (ENXIO);
}
}
D1("vcc_chpoll: virtual-console-concentrator@%d:%d ev=0x%x, "
"rev=0x%x pev=0x%x, flag=0x%x\n",
instance, portno, events, (*reventsp),
vport->pollevent, vport->pollflag);
return (0);
}
|