Loading...
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 | /***************************************************************************** * sdla_x25.c WANPIPE(tm) Multiprotocol WAN Link Driver. X.25 module. * * Author: Gene Kozin <genek@compuserve.com> * * Copyright: (c) 1995-1997 Sangoma Technologies Inc. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * ============================================================================ * Mar 15, 1998 Alan Cox o 2.1.x porting * Nov 27, 1997 Jaspreet Singh o Added protection against enabling of irqs * when they are disabled. * Nov 17, 1997 Farhan Thawar o Added IPX support * o Changed if_send() to now buffer packets when * the board is busy * o Removed queueing of packets via the polling * routing * o Changed if_send() critical flags to properly * handle race conditions * Nov 06, 1997 Farhan Thawar o Added support for SVC timeouts * o Changed PVC encapsulation to ETH_P_IP * Jul 21, 1997 Jaspreet Singh o Fixed freeing up of buffers using kfree() * when packets are received. * Mar 11, 1997 Farhan Thawar Version 3.1.1 * o added support for V35 * o changed if_send() to return 0 if * wandev.critical() is true * o free socket buffer in if_send() if * returning 0 * o added support for single '@' address to * accept all incoming calls * o fixed bug in set_chan_state() to disconnect * Jan 15, 1997 Gene Kozin Version 3.1.0 * o implemented exec() entry point * Jan 07, 1997 Gene Kozin Initial version. *****************************************************************************/ #include <linux/kernel.h> /* printk(), and other useful stuff */ #include <linux/stddef.h> /* offsetof(), etc. */ #include <linux/errno.h> /* return codes */ #include <linux/string.h> /* inline memset(), etc. */ #include <linux/malloc.h> /* kmalloc(), kfree() */ #include <linux/wanrouter.h> /* WAN router definitions */ #include <linux/wanpipe.h> /* WANPIPE common user API definitions */ #include <asm/byteorder.h> /* htons(), etc. */ #include <asm/uaccess.h> #define _GNUC_ #include <linux/sdla_x25.h> /* X.25 firmware API definitions */ /****** Defines & Macros ****************************************************/ #define CMD_OK 0 /* normal firmware return code */ #define CMD_TIMEOUT 0xFF /* firmware command timed out */ #define MAX_CMD_RETRY 10 /* max number of firmware retries */ #define X25_CHAN_MTU 4096 /* unfragmented logical channel MTU */ #define X25_HRDHDR_SZ 7 /* max encapsulation header size */ #define X25_CONCT_TMOUT (90*HZ) /* link connection timeout */ #define X25_RECON_TMOUT (10*HZ) /* link connection timeout */ #define CONNECT_TIMEOUT (90*HZ) /* link connection timeout */ #define HOLD_DOWN_TIME (30*HZ) /* link hold down time */ /* For IPXWAN */ #define CVHexToAscii(b) (((unsigned char)(b) > (unsigned char)9) ? ((unsigned char)'A' + ((unsigned char)(b) - (unsigned char)10)) : ((unsigned char)'0' + (unsigned char)(b))) /****** Data Structures *****************************************************/ /* This is an extention of the 'struct device' we create for each network * interface to keep the rest of X.25 channel-specific data. */ typedef struct x25_channel { char name[WAN_IFNAME_SZ+1]; /* interface name, ASCIIZ */ char addr[WAN_ADDRESS_SZ+1]; /* media address, ASCIIZ */ unsigned lcn; /* logical channel number */ unsigned tx_pkt_size; unsigned short protocol; /* ethertype, 0 - multiplexed */ char svc; /* 0 - permanent, 1 - switched */ char state; /* channel state */ char drop_sequence; /* mark sequence for dropping */ unsigned long state_tick; /* time of the last state change */ unsigned idle_timeout; /* sec, before disconnecting */ unsigned long i_timeout_sofar; /* # of sec's we've been idle */ unsigned hold_timeout; /* sec, before re-connecting */ unsigned long tick_counter; /* counter for transmit time out */ char devtint; /* Weather we should dev_tint() */ struct sk_buff* rx_skb; /* receive socket buffer */ struct sk_buff* tx_skb; /* transmit socket buffer */ sdla_t* card; /* -> owner */ int ch_idx; struct net_device_stats ifstats; /* interface statistics */ } x25_channel_t; typedef struct x25_call_info { char dest[17]; /* ASCIIZ destination address */ char src[17]; /* ASCIIZ source address */ char nuser; /* number of user data bytes */ unsigned char user[127]; /* user data */ char nfacil; /* number of facilities */ struct { unsigned char code; unsigned char parm; } facil[64]; /* facilities */ } x25_call_info_t; /****** Function Prototypes *************************************************/ /* WAN link driver entry points. These are called by the WAN router module. */ static int update (wan_device_t* wandev); static int new_if (wan_device_t* wandev, struct device* dev, wanif_conf_t* conf); static int del_if (wan_device_t* wandev, struct device* dev); /* WANPIPE-specific entry points */ static int wpx_exec (struct sdla* card, void* u_cmd, void* u_data); /* Network device interface */ static int if_init (struct device* dev); static int if_open (struct device* dev); static int if_close (struct device* dev); static int if_header (struct sk_buff* skb, struct device* dev, unsigned short type, void* daddr, void* saddr, unsigned len); static int if_rebuild_hdr (struct sk_buff* skb); static int if_send (struct sk_buff* skb, struct device* dev); static struct net_device_stats * if_stats (struct device* dev); /* Interrupt handlers */ static void wpx_isr (sdla_t* card); static void rx_intr (sdla_t* card); static void tx_intr (sdla_t* card); static void status_intr (sdla_t* card); static void event_intr (sdla_t* card); static void spur_intr (sdla_t* card); /* Background polling routines */ static void wpx_poll (sdla_t* card); static void poll_disconnected (sdla_t* card); static void poll_connecting (sdla_t* card); static void poll_active (sdla_t* card); /* X.25 firmware interface functions */ static int x25_get_version (sdla_t* card, char* str); static int x25_configure (sdla_t* card, TX25Config* conf); static int x25_get_err_stats (sdla_t* card); static int x25_get_stats (sdla_t* card); static int x25_set_intr_mode (sdla_t* card, int mode); static int x25_close_hdlc (sdla_t* card); static int x25_open_hdlc (sdla_t* card); static int x25_setup_hdlc (sdla_t* card); static int x25_set_dtr (sdla_t* card, int dtr); static int x25_get_chan_conf (sdla_t* card, x25_channel_t* chan); static int x25_place_call (sdla_t* card, x25_channel_t* chan); static int x25_accept_call (sdla_t* card, int lcn, int qdm); static int x25_clear_call (sdla_t* card, int lcn, int cause, int diagn); static int x25_send (sdla_t* card, int lcn, int qdm, int len, void* buf); static int x25_fetch_events (sdla_t* card); static int x25_error (sdla_t* card, int err, int cmd, int lcn); /* X.25 asynchronous event handlers */ static int incoming_call (sdla_t* card, int cmd, int lcn, TX25Mbox* mb); static int call_accepted (sdla_t* card, int cmd, int lcn, TX25Mbox* mb); static int call_cleared (sdla_t* card, int cmd, int lcn, TX25Mbox* mb); static int timeout_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb); static int restart_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb); /* Miscellaneous functions */ static int connect (sdla_t* card); static int disconnect (sdla_t* card); static struct device* get_dev_by_lcn(wan_device_t* wandev, unsigned lcn); static int chan_connect (struct device* dev); static int chan_disc (struct device* dev); static void set_chan_state (struct device* dev, int state); static int chan_send (struct device* dev, struct sk_buff* skb); static unsigned char bps_to_speed_code (unsigned long bps); static unsigned int dec_to_uint (unsigned char* str, int len); static unsigned int hex_to_uint (unsigned char* str, int len); static void parse_call_info (unsigned char* str, x25_call_info_t* info); /* IPX functions */ static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming); static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_IPX, unsigned long network_number, unsigned short proto); extern void disable_irq(unsigned int); extern void enable_irq(unsigned int); /****** Global Data ********************************************************** * Note: All data must be explicitly initialized!!! */ /****** Public Functions ****************************************************/ /*============================================================================ * X.25 Protocol Initialization routine. * * This routine is called by the main WANPIPE module during setup. At this * point adapter is completely initialized and X.25 firmware is running. * o read firmware version (to make sure it's alive) * o configure adapter * o initialize protocol-specific fields of the adapter data space. * * Return: 0 o.k. * < 0 failure. */ int wpx_init (sdla_t* card, wandev_conf_t* conf) { union { char str[80]; TX25Config cfg; } u; /* Verify configuration ID */ if (conf->config_id != WANCONFIG_X25) { printk(KERN_INFO "%s: invalid configuration ID %u!\n", card->devname, conf->config_id) ; return -EINVAL; } /* Initialize protocol-specific fields */ card->mbox = (void*)(card->hw.dpmbase + X25_MBOX_OFFS); card->rxmb = (void*)(card->hw.dpmbase + X25_RXMBOX_OFFS); card->flags = (void*)(card->hw.dpmbase + X25_STATUS_OFFS); /* Read firmware version. Note that when adapter initializes, it * clears the mailbox, so it may appear that the first command was * executed successfully when in fact it was merely erased. To work * around this, we execute the first command twice. */ if (x25_get_version(card, NULL) || x25_get_version(card, u.str)) return -EIO ; printk(KERN_INFO "%s: running X.25 firmware v%s\n", card->devname, u.str) ; /* Configure adapter. Here we set resonable defaults, then parse * device configuration structure and set configuration options. * Most configuration options are verified and corrected (if * necessary) since we can't rely on the adapter to do so and don't * want it to fail either. */ memset(&u.cfg, 0, sizeof(u.cfg)); u.cfg.t1 = 3; u.cfg.n2 = 10; u.cfg.autoHdlc = 1; /* automatic HDLC connection */ u.cfg.hdlcWindow = 7; u.cfg.pktWindow = 2; u.cfg.station = 1; /* DTE */ u.cfg.options = 0x00B0; /* disable D-bit pragmatics */ u.cfg.ccittCompat = 1988; u.cfg.t10t20 = 30; u.cfg.t11t21 = 30; u.cfg.t12t22 = 30; u.cfg.t13t23 = 30; u.cfg.t16t26 = 30; u.cfg.t28 = 30; u.cfg.r10r20 = 5; u.cfg.r12r22 = 5; u.cfg.r13r23 = 5; u.cfg.responseOpt = 1; /* RR's after every packet */ if (conf->clocking != WANOPT_EXTERNAL) u.cfg.baudRate = bps_to_speed_code(conf->bps) ; if (conf->station != WANOPT_DTE) { u.cfg.station = 0; /* DCE mode */ } if (conf->interface != WANOPT_RS232 ) { u.cfg.hdlcOptions |= 0x80; /* V35 mode */ } /* adjust MTU */ if (!conf->mtu || (conf->mtu >= 1024)) card->wandev.mtu = 1024 ; else if (conf->mtu >= 512) card->wandev.mtu = 512 ; else if (conf->mtu >= 256) card->wandev.mtu = 256 ; else if (conf->mtu >= 128) card->wandev.mtu = 128 ; else card->wandev.mtu = 64; u.cfg.defPktSize = u.cfg.pktMTU = card->wandev.mtu; if (conf->u.x25.hi_pvc) { card->u.x.hi_pvc = min(conf->u.x25.hi_pvc, 4095); card->u.x.lo_pvc = min(conf->u.x25.lo_pvc, card->u.x.hi_pvc); } if (conf->u.x25.hi_svc) { card->u.x.hi_svc = min(conf->u.x25.hi_svc, 4095); card->u.x.lo_svc = min(conf->u.x25.lo_svc, card->u.x.hi_svc); } u.cfg.loPVC = card->u.x.lo_pvc; u.cfg.hiPVC = card->u.x.hi_pvc; u.cfg.loTwoWaySVC = card->u.x.lo_svc; u.cfg.hiTwoWaySVC = card->u.x.hi_svc; if (conf->u.x25.hdlc_window) u.cfg.hdlcWindow = min(conf->u.x25.hdlc_window, 7) ; if (conf->u.x25.pkt_window) u.cfg.pktWindow = min(conf->u.x25.pkt_window, 7) ; if (conf->u.x25.t1) u.cfg.t1 = min(conf->u.x25.t1, 30) ; u.cfg.t2 = min(conf->u.x25.t2, 29); u.cfg.t4 = min(conf->u.x25.t4, 240); if (conf->u.x25.n2) u.cfg.n2 = min(conf->u.x25.n2, 30) ; if (conf->u.x25.ccitt_compat) u.cfg.ccittCompat = conf->u.x25.ccitt_compat ; /* initialize adapter */ if ((x25_configure(card, &u.cfg) != CMD_OK) || (x25_close_hdlc(card) != CMD_OK) || /* close HDLC link */ (x25_set_dtr(card, 0) != CMD_OK)) /* drop DTR */ return -EIO ; /* Initialize protocol-specific fields of adapter data space */ card->wandev.bps = conf->bps; card->wandev.interface = conf->interface; card->wandev.clocking = conf->clocking; card->wandev.station = conf->station; card->isr = &wpx_isr; card->poll = &wpx_poll; card->exec = &wpx_exec; card->wandev.update = &update; card->wandev.new_if = &new_if; card->wandev.del_if = &del_if; card->wandev.state = WAN_DISCONNECTED; card->wandev.enable_tx_int = 0; card->irq_dis_if_send_count = 0; card->irq_dis_poll_count = 0; card->wandev.enable_IPX = conf->enable_IPX; if (conf->network_number) card->wandev.network_number = conf->network_number; else card->wandev.network_number = 0xDEADBEEF; return 0; } /******* WAN Device Driver Entry Points *************************************/ /*============================================================================ * Update device status & statistics. */ static int update (wan_device_t* wandev) { sdla_t* card; /* sanity checks */ if ((wandev == NULL) || (wandev->private == NULL)) return -EFAULT; if (wandev->state == WAN_UNCONFIGURED) return -ENODEV; if (test_and_set_bit(0, (void*)&wandev->critical)) return -EAGAIN; card = wandev->private; x25_get_err_stats(card); x25_get_stats(card); wandev->critical = 0; return 0; } /*============================================================================ * Create new logical channel. * This routine is called by the router when ROUTER_IFNEW IOCTL is being * handled. * o parse media- and hardware-specific configuration * o make sure that a new channel can be created * o allocate resources, if necessary * o prepare network device structure for registaration. * * Return: 0 o.k. * < 0 failure (channel will not be created) */ static int new_if (wan_device_t* wandev, struct device* dev, wanif_conf_t* conf) { sdla_t* card = wandev->private; x25_channel_t* chan; int err = 0; if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ)) { printk(KERN_INFO "%s: invalid interface name!\n", card->devname) ; return -EINVAL; } /* allocate and initialize private data */ chan = kmalloc(sizeof(x25_channel_t), GFP_KERNEL); if (chan == NULL) return -ENOMEM ; memset(chan, 0, sizeof(x25_channel_t)); strcpy(chan->name, conf->name); chan->card = card; chan->protocol = ETH_P_IP; chan->tx_skb = chan->rx_skb = NULL; /* verify media address */ if (conf->addr[0] == '@') /* SVC */ { chan->svc = 1; strncpy(chan->addr, &conf->addr[1], WAN_ADDRESS_SZ); /* Set channel timeouts (default if not specified) */ chan->idle_timeout = (conf->idle_timeout) ? conf->idle_timeout : 90; chan->hold_timeout = (conf->hold_timeout) ? conf->hold_timeout : 10; } else if (is_digit(conf->addr[0])) /* PVC */ { int lcn = dec_to_uint(conf->addr, 0); if ((lcn >= card->u.x.lo_pvc) && (lcn <= card->u.x.hi_pvc)) { chan->lcn = lcn; } else { printk(KERN_ERR "%s: PVC %u is out of range on interface %s!\n", wandev->name, lcn, chan->name) ; err = -EINVAL; } } else { printk(KERN_ERR "%s: invalid media address on interface %s!\n", wandev->name, chan->name) ; err = -EINVAL; } if (err) { kfree(chan); return err; } /* prepare network device data space for registration */ dev->name = chan->name; dev->init = &if_init; dev->priv = chan; return 0; } /*============================================================================ * Delete logical channel. */ static int del_if (wan_device_t* wandev, struct device* dev) { if (dev->priv) { kfree(dev->priv); dev->priv = NULL; } return 0; } /****** WANPIPE-specific entry points ***************************************/ /*============================================================================ * Execute adapter interface command. */ static int wpx_exec (struct sdla* card, void* u_cmd, void* u_data) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err, len; TX25Cmd cmd; if(copy_from_user((void*)&cmd, u_cmd, sizeof(cmd))) return -EFAULT; /* execute command */ do { memcpy(&mbox->cmd, &cmd, sizeof(cmd)); if (cmd.length) { if(copy_from_user((void*)&mbox->data, u_data, cmd.length)) return-EFAULT; } if (sdla_exec(mbox)) err = mbox->cmd.result ; else return -EIO; } while (err && retry-- && x25_error(card, err, cmd.command, cmd.lcn)); /* return result */ if(copy_to_user(u_cmd, (void*)&mbox->cmd, sizeof(TX25Cmd))) return -EFAULT; len = mbox->cmd.length; if (len && u_data && copy_to_user(u_data, (void*)&mbox->data, len)) return -EFAULT; return 0; } /****** Network Device Interface ********************************************/ /*============================================================================ * Initialize Linux network interface. * * This routine is called only once for each interface, during Linux network * interface registration. Returning anything but zero will fail interface * registration. */ static int if_init (struct device* dev) { x25_channel_t* chan = dev->priv; sdla_t* card = chan->card; wan_device_t* wandev = &card->wandev; /* Initialize device driver entry points */ dev->open = &if_open; dev->stop = &if_close; dev->hard_header = &if_header; dev->rebuild_header = &if_rebuild_hdr; dev->hard_start_xmit = &if_send; dev->get_stats = &if_stats; /* Initialize media-specific parameters */ dev->type = 30; /* ARP h/w type */ dev->mtu = X25_CHAN_MTU; dev->hard_header_len = X25_HRDHDR_SZ; /* media header length */ dev->addr_len = 2; /* hardware address length */ if (!chan->svc) *(unsigned short*)dev->dev_addr = htons(chan->lcn); /* Initialize hardware parameters (just for reference) */ dev->irq = wandev->irq; dev->dma = wandev->dma; dev->base_addr = wandev->ioport; dev->mem_start = (unsigned long)wandev->maddr; dev->mem_end = dev->mem_end + wandev->msize - 1; /* Set transmit buffer queue length */ dev->tx_queue_len = 10; /* Initialize socket buffers */ dev_init_buffers(dev); set_chan_state(dev, WAN_DISCONNECTED); return 0; } /*============================================================================ * Open network interface. * o prevent module from unloading by incrementing use count * o if link is disconnected then initiate connection * * Return 0 if O.k. or errno. */ static int if_open (struct device* dev) { x25_channel_t* chan = dev->priv; sdla_t* card = chan->card; if (dev->start) return -EBUSY; /* only one open is allowed */ if (test_and_set_bit(0, (void*)&card->wandev.critical)) return -EAGAIN; dev->interrupt = 0; dev->tbusy = 0; dev->start = 1; wanpipe_open(card); /* If this is the first open, initiate physical connection */ if (card->open_cnt == 1) connect(card); card->wandev.critical = 0; return 0; } /*============================================================================ * Close network interface. * o reset flags. * o if there's no more open channels then disconnect physical link. */ static int if_close (struct device* dev) { x25_channel_t* chan = dev->priv; sdla_t* card = chan->card; if (test_and_set_bit(0, (void*)&card->wandev.critical)) return -EAGAIN; dev->start = 0; if ((chan->state == WAN_CONNECTED) || (chan->state == WAN_CONNECTING)) chan_disc(dev); wanpipe_close(card); /* If this is the last close, disconnect physical link */ if (!card->open_cnt) disconnect(card); card->wandev.critical = 0; return 0; } /*============================================================================ * Build media header. * o encapsulate packet according to encapsulation type. * * The trick here is to put packet type (Ethertype) into 'protocol' field of * the socket buffer, so that we don't forget it. If encapsulation fails, * set skb->protocol to 0 and discard packet later. * * Return: media header length. */ static int if_header (struct sk_buff* skb, struct device* dev, unsigned short type, void* daddr, void* saddr, unsigned len) { x25_channel_t* chan = dev->priv; int hdr_len = dev->hard_header_len; skb->protocol = type; if (!chan->protocol) { hdr_len = wanrouter_encapsulate(skb, dev); if (hdr_len < 0) { hdr_len = 0; skb->protocol = 0; } } return hdr_len; } /*============================================================================ * Re-build media header. * * Return: 1 physical address resolved. * 0 physical address not resolved */ static int if_rebuild_hdr (struct sk_buff* skb) { struct device *dev=skb->dev; x25_channel_t* chan = dev->priv; sdla_t* card = chan->card; printk(KERN_INFO "%s: rebuild_header() called for interface %s!\n", card->devname, dev->name); return 1; } /*============================================================================ * Send a packet on a network interface. * o set tbusy flag (marks start of the transmission). * o check link state. If link is not up, then drop the packet. * o check channel status. If it's down then initiate a call. * o pass a packet to corresponding WAN device. * o free socket buffer * * Return: 0 complete (socket buffer must be freed) * non-0 packet may be re-transmitted (tbusy must be set) * * Notes: * 1. This routine is called either by the protocol stack or by the "net * bottom half" (with interrupts enabled). * 2. Setting tbusy flag will inhibit further transmit requests from the * protocol stack and can be used for flow control with protocol layer. */ static int if_send (struct sk_buff* skb, struct device* dev) { x25_channel_t* chan = dev->priv; sdla_t* card = chan->card; struct device *dev2; TX25Status* status = card->flags; unsigned long host_cpu_flags; if (dev->tbusy) { ++chan->ifstats.rx_dropped; if ((jiffies - chan->tick_counter) < (5*HZ)) { return dev->tbusy; } printk(KERN_INFO "%s: Transmit time out %s!\n", card->devname, dev->name) ; for( dev2 = card->wandev.dev; dev2; dev2 = dev2->slave) { dev2->tbusy = 0; } } chan->tick_counter = jiffies; disable_irq(card->hw.irq); ++card->irq_dis_if_send_count; if (test_and_set_bit(0, (void*)&card->wandev.critical)) { printk(KERN_INFO "Hit critical in if_send()!\n"); if (card->wandev.critical == CRITICAL_IN_ISR) { card->wandev.enable_tx_int = 1; dev->tbusy = 1; save_flags(host_cpu_flags); cli(); if ((!(--card->irq_dis_if_send_count)) && (!card->irq_dis_poll_count)) enable_irq(card->hw.irq); restore_flags(host_cpu_flags); return dev->tbusy; } dev_kfree_skb(skb); save_flags(host_cpu_flags); cli(); if ((!(--card->irq_dis_if_send_count)) && (!card->irq_dis_poll_count)) enable_irq(card->hw.irq); restore_flags(host_cpu_flags); return dev->tbusy; } /* Below is only until we have per-channel IPX going.... */ if(!(chan->svc)) chan->protocol = skb->protocol; if (card->wandev.state != WAN_CONNECTED) ++chan->ifstats.tx_dropped; /* Below is only until we have per-channel IPX going.... */ else if ( (chan->svc) && (chan->protocol && (chan->protocol != skb->protocol))) { printk(KERN_INFO "%s: unsupported Ethertype 0x%04X on interface %s!\n", card->devname, skb->protocol, dev->name); ++chan->ifstats.tx_errors; } else switch (chan->state) { case WAN_DISCONNECTED: /* Try to establish connection. If succeded, then start * transmission, else drop a packet. */ if (chan_connect(dev) != 0) { ++chan->ifstats.tx_dropped; ++card->wandev.stats.tx_dropped; break; } /* fall through */ case WAN_CONNECTED: if( skb->protocol == ETH_P_IPX ) { if(card->wandev.enable_IPX) { switch_net_numbers( skb->data, card->wandev.network_number, 0); } else { ++card->wandev.stats.tx_dropped; ++chan->ifstats.tx_dropped; goto tx_done; } } dev->trans_start = jiffies; if(chan_send(dev, skb)) { dev->tbusy = 1; status->imask |= 0x2; } break; default: ++chan->ifstats.tx_dropped; ++card->wandev.stats.tx_dropped; } tx_done: if (!dev->tbusy) dev_kfree_skb(skb); card->wandev.critical = 0; save_flags(host_cpu_flags); cli(); if ((!(--card->irq_dis_if_send_count)) && (!card->irq_dis_poll_count)) enable_irq(card->hw.irq); restore_flags(host_cpu_flags); return dev->tbusy; } /*============================================================================ * Get Ethernet-style interface statistics. * Return a pointer to struct net_device_stats */ static struct net_device_stats* if_stats (struct device* dev) { x25_channel_t* chan = dev->priv; if(chan==NULL) return NULL; return &chan->ifstats; } /****** Interrupt Handlers **************************************************/ /*============================================================================ * X.25 Interrupt Service Routine. */ static void wpx_isr (sdla_t* card) { TX25Status* status = card->flags; struct device *dev; unsigned long host_cpu_flags; card->in_isr = 1; card->buff_int_mode_unbusy = 0; if (test_and_set_bit(0, (void*)&card->wandev.critical)) { printk(KERN_INFO "wpx_isr: %s, wandev.critical set to 0x%02X, int type = 0x%02X\n", card->devname, card->wandev.critical, status->iflags); card->in_isr = 0; return; } /* For all interrupts set the critical flag to CRITICAL_RX_INTR. * If the if_send routine is called with this flag set it will set * the enable transmit flag to 1. (for a delayed interrupt) */ card->wandev.critical = CRITICAL_IN_ISR; switch (status->iflags) { case 0x01: /* receive interrupt */ rx_intr(card); break; case 0x02: /* transmit interrupt */ tx_intr(card); card->buff_int_mode_unbusy = 1; status->imask &= ~0x2; break; case 0x04: /* modem status interrupt */ status_intr(card); break; case 0x10: /* network event interrupt */ event_intr(card); break; default: /* unwanted interrupt */ spur_intr(card); } card->wandev.critical = CRITICAL_INTR_HANDLED; if( card->wandev.enable_tx_int) { card->wandev.enable_tx_int = 0; status->imask |= 0x2; } save_flags(host_cpu_flags); cli(); card->in_isr = 0; status->iflags = 0; /* clear interrupt condition */ card->wandev.critical = 0; restore_flags(host_cpu_flags); if(card->buff_int_mode_unbusy) { for(dev = card->wandev.dev; dev; dev = dev->slave) { if(((x25_channel_t*)dev->priv)->devtint) { mark_bh(NET_BH); return; } } } } /*============================================================================ * Receive interrupt handler. * This routine handles fragmented IP packets using M-bit according to the * RFC1356. * o map ligical channel number to network interface. * o allocate socket buffer or append received packet to the existing one. * o if M-bit is reset (i.e. it's the last packet in a sequence) then * decapsulate packet and pass socket buffer to the protocol stack. * * Notes: * 1. When allocating a socket buffer, if M-bit is set then more data is * comming and we have to allocate buffer for the maximum IP packet size * expected on this channel. * 2. If something goes wrong and X.25 packet has to be dropped (e.g. no * socket buffers available) the whole packet sequence must be discarded. */ static void rx_intr (sdla_t* card) { TX25Mbox* rxmb = card->rxmb; unsigned lcn = rxmb->cmd.lcn; /* logical channel number */ unsigned len = rxmb->cmd.length; /* packet length */ unsigned qdm = rxmb->cmd.qdm; /* Q,D and M bits */ wan_device_t* wandev = &card->wandev; struct device* dev = get_dev_by_lcn(wandev, lcn); x25_channel_t* chan; struct sk_buff* skb; void* bufptr; if (dev == NULL) { /* Invalid channel, discard packet */ printk(KERN_INFO "%s: receiving on orphaned LCN %d!\n", card->devname, lcn); return; } chan = dev->priv; chan->i_timeout_sofar = jiffies; if (chan->drop_sequence) { if (!(qdm & 0x01)) chan->drop_sequence = 0; return; } skb = chan->rx_skb; if (skb == NULL) { /* Allocate new socket buffer */ int bufsize = (qdm & 0x01) ? dev->mtu : len; skb = dev_alloc_skb(bufsize + dev->hard_header_len); if (skb == NULL) { printk(KERN_INFO "%s: no socket buffers available!\n", card->devname); chan->drop_sequence = 1; /* set flag */ ++chan->ifstats.rx_dropped; return; } skb->dev = dev; skb->protocol = htons(chan->protocol); chan->rx_skb = skb; } if (skb_tailroom(skb) < len) { /* No room for the packet. Call off the whole thing! */ dev_kfree_skb(skb); chan->rx_skb = NULL; if (qdm & 0x01) chan->drop_sequence = 1; printk(KERN_INFO "%s: unexpectedly long packet sequence " "on interface %s!\n", card->devname, dev->name); ++chan->ifstats.rx_length_errors; return; } /* Append packet to the socket buffer */ bufptr = skb_put(skb, len); memcpy(bufptr, rxmb->data, len); if (qdm & 0x01) return; /* more data is comming */ dev->last_rx = jiffies; /* timestamp */ chan->rx_skb = NULL; /* dequeue packet */ /* Decapsulate packet, if necessary */ if (!skb->protocol && !wanrouter_type_trans(skb, dev)) { /* can't decapsulate packet */ dev_kfree_skb(skb); ++chan->ifstats.rx_errors; } else { if( handle_IPXWAN(skb->data, card->devname, card->wandev.enable_IPX, card->wandev.network_number, skb->protocol)) { if( card->wandev.enable_IPX ) { if(chan_send(dev, skb)) { chan->tx_skb = skb; } else { dev_kfree_skb(skb); } } else { /* FIXME: increment IPX packet dropped statistic */ } } else { netif_rx(skb); ++chan->ifstats.rx_packets; chan->ifstats.rx_bytes += skb->len; } } } /*============================================================================ * Transmit interrupt handler. * o Release socket buffer * o Clear 'tbusy' flag */ static void tx_intr (sdla_t* card) { struct device *dev; /* unbusy all devices and then dev_tint(); */ for(dev = card->wandev.dev; dev; dev = dev->slave) { ((x25_channel_t*)dev->priv)->devtint = dev->tbusy; dev->tbusy = 0; } } /*============================================================================ * Modem status interrupt handler. */ static void status_intr (sdla_t* card) { } /*============================================================================ * Network event interrupt handler. */ static void event_intr (sdla_t* card) { } /*============================================================================ * Spurious interrupt handler. * o print a warning * o * If number of spurious interrupts exceeded some limit, then ??? */ static void spur_intr (sdla_t* card) { printk(KERN_INFO "%s: spurious interrupt!\n", card->devname); } /****** Background Polling Routines ****************************************/ /*============================================================================ * Main polling routine. * This routine is repeatedly called by the WANPIPE 'thread' to allow for * time-dependent housekeeping work. * * Notes: * 1. This routine may be called on interrupt context with all interrupts * enabled. Beware! */ static void wpx_poll (sdla_t* card) { unsigned long host_cpu_flags; disable_irq(card->hw.irq); ++card->irq_dis_poll_count; if (test_and_set_bit(0, (void*)&card->wandev.critical)) { printk(KERN_INFO "%s: critical in polling!\n",card->devname); save_flags(host_cpu_flags); cli(); if ((!card->irq_dis_if_send_count) && (!(--card->irq_dis_poll_count))) enable_irq(card->hw.irq); restore_flags(host_cpu_flags); return; } switch(card->wandev.state) { case WAN_CONNECTED: poll_active(card); break; case WAN_CONNECTING: poll_connecting(card); break; case WAN_DISCONNECTED: poll_disconnected(card); } card->wandev.critical = 0; save_flags(host_cpu_flags); cli(); if ((!card->irq_dis_if_send_count) && (!(--card->irq_dis_poll_count))) enable_irq(card->hw.irq); restore_flags(host_cpu_flags); } /*============================================================================ * Handle physical link establishment phase. * o if connection timed out, disconnect the link. */ static void poll_connecting (sdla_t* card) { TX25Status* status = card->flags; if (status->gflags & X25_HDLC_ABM) { wanpipe_set_state(card, WAN_CONNECTED); x25_set_intr_mode(card, 0x83); /* enable Rx interrupts */ status->imask &= ~0x2; /* mask Tx interupts */ } else if ((jiffies - card->state_tick) > CONNECT_TIMEOUT) disconnect(card); } /*============================================================================ * Handle physical link disconnected phase. * o if hold-down timeout has expired and there are open interfaces, connect * link. */ static void poll_disconnected (sdla_t* card) { if (card->open_cnt && ((jiffies - card->state_tick) > HOLD_DOWN_TIME)) connect(card); } /*============================================================================ * Handle active link phase. * o fetch X.25 asynchronous events. * o kick off transmission on all interfaces. */ static void poll_active (sdla_t* card) { struct device* dev; /* Fetch X.25 asynchronous events */ x25_fetch_events(card); for (dev = card->wandev.dev; dev; dev = dev->slave) { x25_channel_t* chan = dev->priv; struct sk_buff* skb = chan->tx_skb; /* If there is a packet queued for transmission then kick * the channel's send routine. When transmission is complete * or if error has occurred, release socket buffer and reset * 'tbusy' flag. */ if (skb && (chan_send(dev, skb) == 0)) { chan->tx_skb = NULL; dev->tbusy = 0; dev_kfree_skb(skb); } /* If SVC has been idle long enough, close virtual circuit */ if(( chan->svc )&&( chan->state == WAN_CONNECTED )) { if( (jiffies - chan->i_timeout_sofar) / HZ > chan->idle_timeout ) { /* Close svc */ printk(KERN_INFO "%s: Closing down Idle link %s on LCN %d\n",card->devname,chan->name,chan->lcn); chan->i_timeout_sofar = jiffies; chan_disc(dev); } } } } /****** SDLA Firmware-Specific Functions ************************************* * Almost all X.25 commands can unexpetedly fail due to so called 'X.25 * asynchronous events' such as restart, interrupt, incoming call request, * call clear request, etc. They can't be ignored and have to be dealt with * immediately. To tackle with this problem we execute each interface command * in a loop until good return code is received or maximum number of retries * is reached. Each interface command returns non-zero return code, an * asynchronous event/error handler x25_error() is called. */ /*============================================================================ * Read X.25 firmware version. * Put code version as ASCII string in str. */ static int x25_get_version (sdla_t* card, char* str) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); mbox->cmd.command = X25_READ_CODE_VERSION; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_READ_CODE_VERSION, 0)); if (!err && str) { int len = mbox->cmd.length; memcpy(str, mbox->data, len); str[len] = '\0'; } return err; } /*============================================================================ * Configure adapter. */ static int x25_configure (sdla_t* card, TX25Config* conf) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); memcpy(mbox->data, (void*)conf, sizeof(TX25Config)); mbox->cmd.length = sizeof(TX25Config); mbox->cmd.command = X25_SET_CONFIGURATION; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_SET_CONFIGURATION, 0)); return err; } /*============================================================================ * Get communications error statistics. */ static int x25_get_err_stats (sdla_t* card) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); mbox->cmd.command = X25_HDLC_READ_COMM_ERR; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_HDLC_READ_COMM_ERR, 0)); if (!err) { THdlcCommErr* stats = (void*)mbox->data; card->wandev.stats.rx_over_errors = stats->rxOverrun; card->wandev.stats.rx_crc_errors = stats->rxBadCrc; card->wandev.stats.rx_missed_errors = stats->rxAborted; card->wandev.stats.tx_aborted_errors = stats->txAborted; } return err; } /*============================================================================ * Get protocol statistics. */ static int x25_get_stats (sdla_t* card) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); mbox->cmd.command = X25_READ_STATISTICS; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_READ_STATISTICS, 0)); if (!err) { TX25Stats* stats = (void*)mbox->data; card->wandev.stats.rx_packets = stats->rxData; card->wandev.stats.tx_packets = stats->rxData; } return err; } /*============================================================================ * Close HDLC link. */ static int x25_close_hdlc (sdla_t* card) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); mbox->cmd.command = X25_HDLC_LINK_CLOSE; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_CLOSE, 0)); return err; } /*============================================================================ * Open HDLC link. */ static int x25_open_hdlc (sdla_t* card) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); mbox->cmd.command = X25_HDLC_LINK_OPEN; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_OPEN, 0)); return err; } /*============================================================================ * Setup HDLC link. */ static int x25_setup_hdlc (sdla_t* card) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); mbox->cmd.command = X25_HDLC_LINK_SETUP; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_SETUP, 0)); return err; } /*============================================================================ * Set (raise/drop) DTR. */ static int x25_set_dtr (sdla_t* card, int dtr) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); mbox->data[0] = 0; mbox->data[2] = 0; mbox->data[1] = dtr ? 0x02 : 0x01; mbox->cmd.length = 3; mbox->cmd.command = X25_SET_GLOBAL_VARS; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_SET_GLOBAL_VARS, 0)); return err; } /*============================================================================ * Set interrupt mode. */ static int x25_set_intr_mode (sdla_t* card, int mode) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); mbox->data[0] = mode; if (card->hw.fwid == SFID_X25_508) { mbox->data[1] = card->hw.irq; mbox->cmd.length = 2; } else mbox->cmd.length = 1; mbox->cmd.command = X25_SET_INTERRUPT_MODE; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_SET_INTERRUPT_MODE, 0)) ; return err; } /*============================================================================ * Read X.25 channel configuration. */ static int x25_get_chan_conf (sdla_t* card, x25_channel_t* chan) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int lcn = chan->lcn; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); mbox->cmd.lcn = lcn; mbox->cmd.command = X25_READ_CHANNEL_CONFIG; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_READ_CHANNEL_CONFIG, lcn)); if (!err) { TX25Status* status = card->flags; /* calculate an offset into the array of status bytes */ if (card->u.x.hi_svc <= 255) chan->ch_idx = lcn - 1; else { int offset; switch (mbox->data[0] && 0x1F) { case 0x01: offset = status->pvc_map; break; case 0x03: offset = status->icc_map; break; case 0x07: offset = status->twc_map; break; case 0x0B: offset = status->ogc_map; break; default: offset = 0; } chan->ch_idx = lcn - 1 - offset; } /* get actual transmit packet size on this channel */ switch(mbox->data[1] & 0x38) { case 0x00: chan->tx_pkt_size = 16; break; case 0x08: chan->tx_pkt_size = 32; break; case 0x10: chan->tx_pkt_size = 64; break; case 0x18: chan->tx_pkt_size = 128; break; case 0x20: chan->tx_pkt_size = 256; break; case 0x28: chan->tx_pkt_size = 512; break; case 0x30: chan->tx_pkt_size = 1024; break; } printk(KERN_INFO "%s: X.25 packet size on LCN %d is %d.\n", card->devname, lcn, chan->tx_pkt_size); } return err; } /*============================================================================ * Place X.25 call. */ static int x25_place_call (sdla_t* card, x25_channel_t* chan) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; char str[64]; sprintf(str, "-d%s -uCC", chan->addr); do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); strcpy(mbox->data, str); mbox->cmd.length = strlen(str); mbox->cmd.command = X25_PLACE_CALL; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_PLACE_CALL, 0)); if (!err) { chan->lcn = mbox->cmd.lcn; chan->protocol = ETH_P_IP; } return err; } /*============================================================================ * Accept X.25 call. */ static int x25_accept_call (sdla_t* card, int lcn, int qdm) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); mbox->cmd.lcn = lcn; mbox->cmd.qdm = qdm; mbox->cmd.command = X25_ACCEPT_CALL; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_ACCEPT_CALL, lcn)); return err; } /*============================================================================ * Clear X.25 call. */ static int x25_clear_call (sdla_t* card, int lcn, int cause, int diagn) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); mbox->cmd.lcn = lcn; mbox->cmd.cause = cause; mbox->cmd.diagn = diagn; mbox->cmd.command = X25_CLEAR_CALL; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_CLEAR_CALL, lcn)); return err; } /*============================================================================ * Send X.25 data packet. */ static int x25_send (sdla_t* card, int lcn, int qdm, int len, void* buf) { TX25Mbox* mbox = card->mbox; int retry = MAX_CMD_RETRY; int err; do { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); memcpy(mbox->data, buf, len); mbox->cmd.length = len; mbox->cmd.lcn = lcn; mbox->cmd.qdm = qdm; mbox->cmd.command = X25_WRITE; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; } while (err && retry-- && x25_error(card, err, X25_WRITE, lcn)); return err; } /*============================================================================ * Fetch X.25 asynchronous events. */ static int x25_fetch_events (sdla_t* card) { TX25Status* status = card->flags; TX25Mbox* mbox = card->mbox; int err = 0; if (status->gflags & 0x20) { memset(&mbox->cmd, 0, sizeof(TX25Cmd)); mbox->cmd.command = X25_IS_DATA_AVAILABLE; err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT; if (err) x25_error(card, err, X25_IS_DATA_AVAILABLE, 0); } return err; } /*============================================================================ * X.25 asynchronous event/error handler. * This routine is called each time interface command returns non-zero * return code to handle X.25 asynchronous events and common errors. * Return non-zero to repeat command or zero to cancel it. * * Notes: * 1. This function may be called recursively, as handling some of the * asynchronous events (e.g. call request) requires execution of the * interface command(s) that, in turn, may also return asynchronous * events. To avoid re-entrancy problems we copy mailbox to dynamically * allocated memory before processing events. */ static int x25_error (sdla_t* card, int err, int cmd, int lcn) { int retry = 1; unsigned dlen = ((TX25Mbox*)card->mbox)->cmd.length; TX25Mbox* mb; mb = kmalloc(sizeof(TX25Mbox) + dlen, GFP_ATOMIC); if (mb == NULL) { printk(KERN_ERR "%s: x25_error() out of memory!\n", card->devname); return 0; } memcpy(mb, card->mbox, sizeof(TX25Mbox) + dlen); switch (err) { case 0x40: /* X.25 asynchronous packet was received */ mb->data[dlen] = '\0'; switch (mb->cmd.pktType & 0x7F) { case 0x30: /* incoming call */ retry = incoming_call(card, cmd, lcn, mb); break; case 0x31: /* connected */ retry = call_accepted(card, cmd, lcn, mb); break; case 0x02: /* call clear request */ retry = call_cleared(card, cmd, lcn, mb); break; case 0x04: /* reset request */ printk(KERN_INFO "%s: X.25 reset request on LCN %d! " "Cause:0x%02X Diagn:0x%02X\n", card->devname, mb->cmd.lcn, mb->cmd.cause, mb->cmd.diagn); break; case 0x08: /* restart request */ retry = restart_event(card, cmd, lcn, mb); break; default: printk(KERN_INFO "%s: X.25 event 0x%02X on LCN %d! " "Cause:0x%02X Diagn:0x%02X\n", card->devname, mb->cmd.pktType, mb->cmd.lcn, mb->cmd.cause, mb->cmd.diagn); } break; case 0x41: /* X.25 protocol violation indication */ printk(KERN_INFO "%s: X.25 protocol violation on LCN %d! " "Packet:0x%02X Cause:0x%02X Diagn:0x%02X\n", card->devname, mb->cmd.lcn, mb->cmd.pktType & 0x7F, mb->cmd.cause, mb->cmd.diagn); break; case 0x42: /* X.25 timeout */ retry = timeout_event(card, cmd, lcn, mb); break; case 0x43: /* X.25 retry limit exceeded */ printk(KERN_INFO "%s: exceeded X.25 retry limit on LCN %d! " "Packet:0x%02X Diagn:0x%02X\n", card->devname, mb->cmd.lcn, mb->cmd.pktType, mb->cmd.diagn); break; case 0x08: /* modem failure */ printk(KERN_INFO "%s: modem failure!\n", card->devname); break; case 0x09: /* N2 retry limit */ printk(KERN_INFO "%s: exceeded HDLC retry limit!\n", card->devname); break; case 0x06: /* unnumbered frame was received while in ABM */ printk(KERN_INFO "%s: received Unnumbered frame 0x%02X!\n", card->devname, mb->data[0]); break; case CMD_TIMEOUT: printk(KERN_ERR "%s: command 0x%02X timed out!\n", card->devname, cmd); retry = 0; /* abort command */ break; default: printk(KERN_INFO "%s: command 0x%02X returned 0x%02X!\n", card->devname, cmd, err); retry = 0; /* abort command */ } kfree(mb); return retry; } /****** X.25 Asynchronous Event Handlers ************************************* * These functions are called by the x25_error() and should return 0, if * the command resulting in the asynchronous event must be aborted. */ /*============================================================================ * Handle X.25 incoming call request. * RFC 1356 establishes the following rules: * 1. The first octet in the Call User Data (CUD) field of the call * request packet contains NLPID identifying protocol encapsulation. * 2. Calls MUST NOT be accepted unless router supports requested * protocol encapsulation. * 3. A diagnostic code 249 defined by ISO/IEC 8208 may be used when * clearing a call because protocol encapsulation is not supported. * 4. If an incoming call is received while a call request is pending * (i.e. call collision has occurred), the incoming call shall be * rejected and call request shall be retried. */ static int incoming_call (sdla_t* card, int cmd, int lcn, TX25Mbox* mb) { wan_device_t* wandev = &card->wandev; int new_lcn = mb->cmd.lcn; struct device* dev = get_dev_by_lcn(wandev, new_lcn); x25_channel_t* chan = NULL; int accept = 0; /* set to '1' if o.k. to accept call */ x25_call_info_t* info; /* Make sure there is no call collision */ if (dev != NULL) { printk(KERN_INFO "%s: X.25 incoming call collision on LCN %d!\n", card->devname, new_lcn); x25_clear_call(card, new_lcn, 0, 0); return 1; } /* Make sure D bit is not set in call request */ if (mb->cmd.qdm & 0x02) { printk(KERN_INFO "%s: X.25 incoming call on LCN %d with D-bit set!\n", card->devname, new_lcn); x25_clear_call(card, new_lcn, 0, 0); return 1; } /* Parse call request data */ info = kmalloc(sizeof(x25_call_info_t), GFP_ATOMIC); if (info == NULL) { printk(KERN_ERR "%s: not enough memory to parse X.25 incoming call " "on LCN %d!\n", card->devname, new_lcn); x25_clear_call(card, new_lcn, 0, 0); return 1; } parse_call_info(mb->data, info); printk(KERN_INFO "%s: X.25 incoming call on LCN %d! Call data: %s\n", card->devname, new_lcn, mb->data); /* Find available channel */ for (dev = wandev->dev; dev; dev = dev->slave) { chan = dev->priv; if (!chan->svc || (chan->state != WAN_DISCONNECTED)) continue; if (strcmp(info->src, chan->addr) == 0) break; /* If just an '@' is specified, accept all incoming calls */ if (strcmp(chan->addr, "") == 0) break; } if (dev == NULL) { printk(KERN_INFO "%s: no channels available!\n", card->devname); x25_clear_call(card, new_lcn, 0, 0); } /* Check requested protocol encapsulation */ else if (info->nuser == 0) { printk(KERN_INFO "%s: no user data in incoming call on LCN %d!\n", card->devname, new_lcn); x25_clear_call(card, new_lcn, 0, 0); } else switch (info->user[0]) { case 0: /* multiplexed */ chan->protocol = 0; accept = 1; break; case NLPID_IP: /* IP datagrams */ chan->protocol = ETH_P_IP; accept = 1; break; case NLPID_SNAP: /* IPX datagrams */ chan->protocol = ETH_P_IPX; accept = 1; break; default: printk(KERN_INFO "%s: unsupported NLPID 0x%02X in incoming call " "on LCN %d!\n", card->devname, info->user[0], new_lcn); x25_clear_call(card, new_lcn, 0, 249); } if (accept && (x25_accept_call(card, new_lcn, 0) == CMD_OK)) { chan->lcn = new_lcn; if (x25_get_chan_conf(card, chan) == CMD_OK) set_chan_state(dev, WAN_CONNECTED); else x25_clear_call(card, new_lcn, 0, 0); } kfree(info); return 1; } /*============================================================================ * Handle accepted call. */ static int call_accepted (sdla_t* card, int cmd, int lcn, TX25Mbox* mb) { unsigned new_lcn = mb->cmd.lcn; struct device* dev = get_dev_by_lcn(&card->wandev, new_lcn); x25_channel_t* chan; printk(KERN_INFO "%s: X.25 call accepted on LCN %d!\n", card->devname, new_lcn); if (dev == NULL) { printk(KERN_INFO "%s: clearing orphaned connection on LCN %d!\n", card->devname, new_lcn); x25_clear_call(card, new_lcn, 0, 0); return 1; } /* Get channel configuration and notify router */ chan = dev->priv; if (x25_get_chan_conf(card, chan) != CMD_OK) { x25_clear_call(card, new_lcn, 0, 0); return 1; } set_chan_state(dev, WAN_CONNECTED); return 1; } /*============================================================================ * Handle cleared call. */ static int call_cleared (sdla_t* card, int cmd, int lcn, TX25Mbox* mb) { unsigned new_lcn = mb->cmd.lcn; struct device* dev = get_dev_by_lcn(&card->wandev, new_lcn); printk(KERN_INFO "%s: X.25 clear request on LCN %d! Cause:0x%02X " "Diagn:0x%02X\n", card->devname, new_lcn, mb->cmd.cause, mb->cmd.diagn); if (dev == NULL) return 1; set_chan_state(dev, WAN_DISCONNECTED); return ((cmd == X25_WRITE) && (lcn == new_lcn)) ? 0 : 1; } /*============================================================================ * Handle X.25 restart event. */ static int restart_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb) { wan_device_t* wandev = &card->wandev; struct device* dev; printk(KERN_INFO "%s: X.25 restart request! Cause:0x%02X Diagn:0x%02X\n", card->devname, mb->cmd.cause, mb->cmd.diagn); /* down all logical channels */ for (dev = wandev->dev; dev; dev = dev->slave) set_chan_state(dev, WAN_DISCONNECTED); return (cmd == X25_WRITE) ? 0 : 1; } /*============================================================================ * Handle timeout event. */ static int timeout_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb) { unsigned new_lcn = mb->cmd.lcn; if (mb->cmd.pktType == 0x05) /* call request time out */ { struct device* dev = get_dev_by_lcn(&card->wandev, new_lcn); printk(KERN_INFO "%s: X.25 call timed timeout on LCN %d!\n", card->devname, new_lcn); if (dev) set_chan_state(dev, WAN_DISCONNECTED); } else printk(KERN_INFO "%s: X.25 packet 0x%02X timeout on LCN %d!\n", card->devname, mb->cmd.pktType, new_lcn); return 1; } /******* Miscellaneous ******************************************************/ /*============================================================================ * Establish physical connection. * o open HDLC and raise DTR * * Return: 0 connection established * 1 connection is in progress * <0 error */ static int connect (sdla_t* card) { if (x25_open_hdlc(card) || x25_setup_hdlc(card)) return -EIO; wanpipe_set_state(card, WAN_CONNECTING); return 1; } /*============================================================================ * Tear down physical connection. * o close HDLC link * o drop DTR * * Return: 0 * <0 error */ static int disconnect (sdla_t* card) { wanpipe_set_state(card, WAN_DISCONNECTED); x25_set_intr_mode(card, 0); /* disable interrupt generation */ x25_close_hdlc(card); /* close HDLC link */ x25_set_dtr(card, 0); /* drop DTR */ return 0; } /*============================================================================ * Find network device by its channel number. */ static struct device* get_dev_by_lcn (wan_device_t* wandev, unsigned lcn) { struct device* dev; for (dev = wandev->dev; dev; dev = dev->slave) if (((x25_channel_t*)dev->priv)->lcn == lcn) break; return dev; } /*============================================================================ * Initiate connection on the logical channel. * o for PVC we just get channel configuration * o for SVCs place an X.25 call * * Return: 0 connected * >0 connection in progress * <0 failure */ static int chan_connect (struct device* dev) { x25_channel_t* chan = dev->priv; sdla_t* card = chan->card; if (chan->svc) { if (!chan->addr[0]) return -EINVAL; /* no destination address */ printk(KERN_INFO "%s: placing X.25 call to %s ...\n", card->devname, chan->addr); if (x25_place_call(card, chan) != CMD_OK) return -EIO; set_chan_state(dev, WAN_CONNECTING); return 1; } else { if (x25_get_chan_conf(card, chan) != CMD_OK) return -EIO; set_chan_state(dev, WAN_CONNECTED); } return 0; } /*============================================================================ * Disconnect logical channel. * o if SVC then clear X.25 call */ static int chan_disc (struct device* dev) { x25_channel_t* chan = dev->priv; if (chan->svc) x25_clear_call(chan->card, chan->lcn, 0, 0); set_chan_state(dev, WAN_DISCONNECTED); return 0; } /*============================================================================ * Set logical channel state. */ static void set_chan_state (struct device* dev, int state) { x25_channel_t* chan = dev->priv; sdla_t* card = chan->card; unsigned long flags; save_flags(flags); cli(); if (chan->state != state) { switch (state) { case WAN_CONNECTED: printk (KERN_INFO "%s: interface %s connected!\n", card->devname, dev->name); *(unsigned short*)dev->dev_addr = htons(chan->lcn); chan->i_timeout_sofar = jiffies; break; case WAN_CONNECTING: printk (KERN_INFO "%s: interface %s connecting...\n", card->devname, dev->name); break; case WAN_DISCONNECTED: printk (KERN_INFO "%s: interface %s disconnected!\n", card->devname, dev->name); if (chan->svc) { *(unsigned short*)dev->dev_addr = 0; chan->lcn = 0; } break; } chan->state = state; } chan->state_tick = jiffies; restore_flags(flags); } /*============================================================================ * Send packet on a logical channel. * When this function is called, tx_skb field of the channel data space * points to the transmit socket buffer. When transmission is complete, * release socket buffer and reset 'tbusy' flag. * * Return: 0 - transmission complete * 1 - busy * * Notes: * 1. If packet length is greater than MTU for this channel, we'll fragment * the packet into 'complete sequence' using M-bit. * 2. When transmission is complete, an event notification should be issued * to the router. */ static int chan_send (struct device* dev, struct sk_buff* skb) { x25_channel_t* chan = dev->priv; sdla_t* card = chan->card; TX25Status* status = card->flags; unsigned len, qdm; /* Check to see if channel is ready */ if (!(status->cflags[chan->ch_idx] & 0x40)) return 1; if (skb->len > chan->tx_pkt_size) { len = chan->tx_pkt_size; qdm = 0x01; /* set M-bit (more data) */ } else /* final packet */ { len = skb->len; qdm = 0; } switch(x25_send(card, chan->lcn, qdm, len, skb->data)) { case 0x00: /* success */ chan->i_timeout_sofar = jiffies; if (qdm) { skb_pull(skb, len); return 1; } ++chan->ifstats.tx_packets; chan->ifstats.tx_bytes += skb->len; break; case 0x33: /* Tx busy */ return 1; default: /* failure */ ++chan->ifstats.tx_errors; /* return 1; */ } return 0; } /*============================================================================ * Parse X.25 call request data and fill x25_call_info_t structure. */ static void parse_call_info (unsigned char* str, x25_call_info_t* info) { memset(info, 0, sizeof(x25_call_info_t)); for (; *str; ++str) { int i; unsigned ch; if (*str == '-') switch (str[1]) { case 'd': /* destination address */ for (i = 0; i < 16; ++i) { ch = str[2+i]; if (!is_digit(ch)) break; info->dest[i] = ch; } break; case 's': /* source address */ for (i = 0; i < 16; ++i) { ch = str[2+i]; if (!is_digit(ch)) break; info->src[i] = ch; } break; case 'u': /* user data */ for (i = 0; i < 127; ++i) { ch = str[2+2*i]; if (!is_hex_digit(ch)) break; info->user[i] = hex_to_uint(&str[2+2*i], 2); } info->nuser = i; break; case 'f': /* facilities */ for (i = 0; i < 64; ++i) { ch = str[2+4*i]; if (!is_hex_digit(ch)) break; info->facil[i].code = hex_to_uint(&str[2+4*i], 2); ch = str[4+4*i]; if (!is_hex_digit(ch)) break; info->facil[i].parm = hex_to_uint(&str[4+4*i], 2); } info->nfacil = i; break; } } } /*============================================================================ * Convert line speed in bps to a number used by S502 code. */ static unsigned char bps_to_speed_code (unsigned long bps) { unsigned char number; if (bps <= 1200) number = 0x01 ; else if (bps <= 2400) number = 0x02; else if (bps <= 4800) number = 0x03; else if (bps <= 9600) number = 0x04; else if (bps <= 19200) number = 0x05; else if (bps <= 38400) number = 0x06; else if (bps <= 45000) number = 0x07; else if (bps <= 56000) number = 0x08; else if (bps <= 64000) number = 0x09; else if (bps <= 74000) number = 0x0A; else if (bps <= 112000) number = 0x0B; else if (bps <= 128000) number = 0x0C; else number = 0x0D; return number; } /*============================================================================ * Convert decimal string to unsigned integer. * If len != 0 then only 'len' characters of the string are converted. */ static unsigned int dec_to_uint (unsigned char* str, int len) { unsigned val; if (!len) len = strlen(str); for (val = 0; len && is_digit(*str); ++str, --len) val = (val * 10) + (*str - (unsigned)'0'); return val; } /*============================================================================ * Convert hex string to unsigned integer. * If len != 0 then only 'len' characters of the string are conferted. */ static unsigned int hex_to_uint (unsigned char* str, int len) { unsigned val, ch; if (!len) len = strlen(str); for (val = 0; len; ++str, --len) { ch = *str; if (is_digit(ch)) val = (val << 4) + (ch - (unsigned)'0'); else if (is_hex_digit(ch)) val = (val << 4) + ((ch & 0xDF) - (unsigned)'A' + 10); else break; } return val; } static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_IPX, unsigned long network_number, unsigned short proto) { int i; if( proto == htons(ETH_P_IPX) ) { /* It's an IPX packet */ if(!enable_IPX) { /* Return 1 so we don't pass it up the stack. */ return 1; } } else { /* It's not IPX so pass it up the stack. */ return 0; } if( sendpacket[16] == 0x90 && sendpacket[17] == 0x04) { /* It's IPXWAN */ if( sendpacket[2] == 0x02 && sendpacket[34] == 0x00) { /* It's a timer request packet */ printk(KERN_INFO "%s: Received IPXWAN Timer Request packet\n",devname); /* Go through the routing options and answer no to every * option except Unnumbered RIP/SAP */ for(i = 41; sendpacket[i] == 0x00; i += 5) { /* 0x02 is the option for Unnumbered RIP/SAP */ if( sendpacket[i + 4] != 0x02) sendpacket[i + 1] = 0; } /* Skip over the extended Node ID option */ if( sendpacket[i] == 0x04 ) i += 8; /* We also want to turn off all header compression opt. */ for(; sendpacket[i] == 0x80 ;) { sendpacket[i + 1] = 0; i += (sendpacket[i + 2] << 8) + (sendpacket[i + 3]) + 4; } /* Set the packet type to timer response */ sendpacket[34] = 0x01; printk(KERN_INFO "%s: Sending IPXWAN Timer Response\n",devname); } else if( sendpacket[34] == 0x02 ) { /* This is an information request packet */ printk(KERN_INFO "%s: Received IPXWAN Information Request packet\n",devname); /* Set the packet type to information response */ sendpacket[34] = 0x03; /* Set the router name */ sendpacket[51] = 'X'; sendpacket[52] = 'T'; sendpacket[53] = 'P'; sendpacket[54] = 'I'; sendpacket[55] = 'P'; sendpacket[56] = 'E'; sendpacket[57] = '-'; sendpacket[58] = CVHexToAscii(network_number >> 28); sendpacket[59] = CVHexToAscii((network_number & 0x0F000000)>> 24); sendpacket[60] = CVHexToAscii((network_number & 0x00F00000)>> 20); sendpacket[61] = CVHexToAscii((network_number & 0x000F0000)>> 16); sendpacket[62] = CVHexToAscii((network_number & 0x0000F000)>> 12); sendpacket[63] = CVHexToAscii((network_number & 0x00000F00)>> 8); sendpacket[64] = CVHexToAscii((network_number & 0x000000F0)>> 4); sendpacket[65] = CVHexToAscii(network_number & 0x0000000F); for(i = 66; i < 99; i+= 1) sendpacket[i] = 0; printk(KERN_INFO "%s: Sending IPXWAN Information Response packet\n",devname); } else { printk(KERN_INFO "%s: Unknown IPXWAN packet!\n",devname); return 0; } /* Set the WNodeID to our network address */ sendpacket[35] = (unsigned char)(network_number >> 24); sendpacket[36] = (unsigned char)((network_number & 0x00FF0000) >> 16); sendpacket[37] = (unsigned char)((network_number & 0x0000FF00) >> 8); sendpacket[38] = (unsigned char)(network_number & 0x000000FF); return 1; } else { /* If we get here its an IPX-data packet, so it'll get passed up the stack. switch the network numbers */ switch_net_numbers(sendpacket, network_number, 1); return 0; } } /* If incoming is 0 (outgoing)- if the net numbers is ours make it 0 if incoming is 1 - if the net number is 0 make it ours */ static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming) { unsigned long pnetwork_number; pnetwork_number = (unsigned long)((sendpacket[6] << 24) + (sendpacket[7] << 16) + (sendpacket[8] << 8) + sendpacket[9]); if (!incoming) { /* If the destination network number is ours, make it 0 */ if( pnetwork_number == network_number) { sendpacket[6] = sendpacket[7] = sendpacket[8] = sendpacket[9] = 0x00; } } else { /* If the incoming network is 0, make it ours */ if( pnetwork_number == 0) { sendpacket[6] = (unsigned char)(network_number >> 24); sendpacket[7] = (unsigned char)((network_number & 0x00FF0000) >> 16); sendpacket[8] = (unsigned char)((network_number & 0x0000FF00) >> 8); sendpacket[9] = (unsigned char)(network_number & 0x000000FF); } } pnetwork_number = (unsigned long)((sendpacket[18] << 24) + (sendpacket[19] << 16) + (sendpacket[20] << 8) + sendpacket[21]); if( !incoming ) { /* If the source network is ours, make it 0 */ if( pnetwork_number == network_number) { sendpacket[18] = sendpacket[19] = sendpacket[20] = sendpacket[21] = 0x00; } } else { /* If the source network is 0, make it ours */ if( pnetwork_number == 0 ) { sendpacket[18] = (unsigned char)(network_number >> 24); sendpacket[19] = (unsigned char)((network_number & 0x00FF0000) >> 16); sendpacket[20] = (unsigned char)((network_number & 0x0000FF00) >> 8); sendpacket[21] = (unsigned char)(network_number & 0x000000FF); } } } /* switch_net_numbers */ /****** End *****************************************************************/ |