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
|
/*
* cycx_x25.c Cyclom 2X WAN Link Driver. X.25 module.
*
* Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
*
* Copyright: (c) 1998-2003 Arnaldo Carvalho de Melo
*
* Based on sdla_x25.c by Gene Kozin <genek@compuserve.com>
*
* 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.
* ============================================================================
* 2001/01/12 acme use dev_kfree_skb_irq on interrupt context
* 2000/04/02 acme dprintk, cycx_debug
* fixed the bug introduced in get_dev_by_lcn and
* get_dev_by_dte_addr by the anonymous hacker
* that converted this driver to softnet
* 2000/01/08 acme cleanup
* 1999/10/27 acme use ARPHRD_HWX25 so that the X.25 stack know
* that we have a X.25 stack implemented in
* firmware onboard
* 1999/10/18 acme support for X.25 sockets in if_send,
* beware: socket(AF_X25...) IS WORK IN PROGRESS,
* TCP/IP over X.25 via wanrouter not affected,
* working.
* 1999/10/09 acme chan_disc renamed to chan_disconnect,
* began adding support for X.25 sockets:
* conf->protocol in new_if
* 1999/10/05 acme fixed return E... to return -E...
* 1999/08/10 acme serialized access to the card thru a spinlock
* in x25_exec
* 1999/08/09 acme removed per channel spinlocks
* removed references to enable_tx_int
* 1999/05/28 acme fixed nibble_to_byte, ackvc now properly treated
* if_send simplified
* 1999/05/25 acme fixed t1, t2, t21 & t23 configuration
* use spinlocks instead of cli/sti in some points
* 1999/05/24 acme finished the x25_get_stat function
* 1999/05/23 acme dev->type = ARPHRD_X25 (tcpdump only works,
* AFAIT, with ARPHRD_ETHER). This seems to be
* needed to use socket(AF_X25)...
* Now the config file must specify a peer media
* address for svc channels over a crossover cable.
* Removed hold_timeout from x25_channel_t,
* not used.
* A little enhancement in the DEBUG processing
* 1999/05/22 acme go to DISCONNECTED in disconnect_confirm_intr,
* instead of chan_disc.
* 1999/05/16 marcelo fixed timer initialization in SVCs
* 1999/01/05 acme x25_configure now get (most of) all
* parameters...
* 1999/01/05 acme pktlen now (correctly) uses log2 (value
* configured)
* 1999/01/03 acme judicious use of data types (u8, u16, u32, etc)
* 1999/01/03 acme cyx_isr: reset dpmbase to acknowledge
* indication (interrupt from cyclom 2x)
* 1999/01/02 acme cyx_isr: first hackings...
* 1999/01/0203 acme when initializing an array don't give less
* elements than declared...
* example: char send_cmd[6] = "?\xFF\x10";
* you'll gonna lose a couple hours, 'cause your
* brain won't admit that there's an error in the
* above declaration... the side effect is that
* memset is put into the unresolved symbols
* instead of using the inline memset functions...
* 1999/01/02 acme began chan_connect, chan_send, x25_send
* 1998/12/31 acme x25_configure
* this code can be compiled as non module
* 1998/12/27 acme code cleanup
* IPX code wiped out! let's decrease code
* complexity for now, remember: I'm learning! :)
* bps_to_speed_code OK
* 1998/12/26 acme Minimal debug code cleanup
* 1998/08/08 acme Initial version.
*/
#define CYCLOMX_X25_DEBUG 1
#include <linux/ctype.h> /* isdigit() */
#include <linux/errno.h> /* return codes */
#include <linux/if_arp.h> /* ARPHRD_HWX25 */
#include <linux/kernel.h> /* printk(), and other useful stuff */
#include <linux/module.h>
#include <linux/string.h> /* inline memset(), etc. */
#include <linux/slab.h> /* kmalloc(), kfree() */
#include <linux/stddef.h> /* offsetof(), etc. */
#include <linux/wanrouter.h> /* WAN router definitions */
#include <asm/byteorder.h> /* htons(), etc. */
#include <linux/cyclomx.h> /* Cyclom 2X common user API definitions */
#include <linux/cycx_x25.h> /* X.25 firmware API definitions */
#include <net/x25device.h>
/* Defines & Macros */
#define CYCX_X25_MAX_CMD_RETRY 5
#define CYCX_X25_CHAN_MTU 2048 /* unfragmented logical channel MTU */
/* Data Structures */
/* This is an extension of the 'struct net_device' we create for each network
interface to keep the rest of X.25 channel-specific data. */
struct cycx_x25_channel {
/* This member must be first. */
struct net_device *slave; /* WAN slave */
char name[WAN_IFNAME_SZ+1]; /* interface name, ASCIIZ */
char addr[WAN_ADDRESS_SZ+1]; /* media address, ASCIIZ */
char *local_addr; /* local media address, ASCIIZ -
svc thru crossover cable */
s16 lcn; /* logical channel number/conn.req.key*/
u8 link;
struct timer_list timer; /* timer used for svc channel disc. */
u16 protocol; /* ethertype, 0 - multiplexed */
u8 svc; /* 0 - permanent, 1 - switched */
u8 state; /* channel state */
u8 drop_sequence; /* mark sequence for dropping */
u32 idle_tmout; /* sec, before disconnecting */
struct sk_buff *rx_skb; /* receive socket buffer */
struct cycx_device *card; /* -> owner */
struct net_device_stats ifstats;/* interface statistics */
};
/* Function Prototypes */
/* WAN link driver entry points. These are called by the WAN router module. */
static int cycx_wan_update(struct wan_device *wandev),
cycx_wan_new_if(struct wan_device *wandev, struct net_device *dev,
wanif_conf_t *conf),
cycx_wan_del_if(struct wan_device *wandev, struct net_device *dev);
/* Network device interface */
static int cycx_netdevice_init(struct net_device *dev);
static int cycx_netdevice_open(struct net_device *dev);
static int cycx_netdevice_stop(struct net_device *dev);
static int cycx_netdevice_hard_header(struct sk_buff *skb,
struct net_device *dev, u16 type,
const void *daddr, const void *saddr,
unsigned len);
static int cycx_netdevice_rebuild_header(struct sk_buff *skb);
static int cycx_netdevice_hard_start_xmit(struct sk_buff *skb,
struct net_device *dev);
static struct net_device_stats *
cycx_netdevice_get_stats(struct net_device *dev);
/* Interrupt handlers */
static void cycx_x25_irq_handler(struct cycx_device *card),
cycx_x25_irq_tx(struct cycx_device *card, struct cycx_x25_cmd *cmd),
cycx_x25_irq_rx(struct cycx_device *card, struct cycx_x25_cmd *cmd),
cycx_x25_irq_log(struct cycx_device *card,
struct cycx_x25_cmd *cmd),
cycx_x25_irq_stat(struct cycx_device *card,
struct cycx_x25_cmd *cmd),
cycx_x25_irq_connect_confirm(struct cycx_device *card,
struct cycx_x25_cmd *cmd),
cycx_x25_irq_disconnect_confirm(struct cycx_device *card,
struct cycx_x25_cmd *cmd),
cycx_x25_irq_connect(struct cycx_device *card,
struct cycx_x25_cmd *cmd),
cycx_x25_irq_disconnect(struct cycx_device *card,
struct cycx_x25_cmd *cmd),
cycx_x25_irq_spurious(struct cycx_device *card,
struct cycx_x25_cmd *cmd);
/* X.25 firmware interface functions */
static int cycx_x25_configure(struct cycx_device *card,
struct cycx_x25_config *conf),
cycx_x25_get_stats(struct cycx_device *card),
cycx_x25_send(struct cycx_device *card, u8 link, u8 lcn, u8 bitm,
int len, void *buf),
cycx_x25_connect_response(struct cycx_device *card,
struct cycx_x25_channel *chan),
cycx_x25_disconnect_response(struct cycx_device *card, u8 link,
u8 lcn);
/* channel functions */
static int cycx_x25_chan_connect(struct net_device *dev),
cycx_x25_chan_send(struct net_device *dev, struct sk_buff *skb);
static void cycx_x25_chan_disconnect(struct net_device *dev),
cycx_x25_chan_send_event(struct net_device *dev, u8 event);
/* Miscellaneous functions */
static void cycx_x25_set_chan_state(struct net_device *dev, u8 state),
cycx_x25_chan_timer(unsigned long d);
static void nibble_to_byte(u8 *s, u8 *d, u8 len, u8 nibble),
reset_timer(struct net_device *dev);
static u8 bps_to_speed_code(u32 bps);
static u8 cycx_log2(u32 n);
static unsigned dec_to_uint(u8 *str, int len);
static struct net_device *cycx_x25_get_dev_by_lcn(struct wan_device *wandev,
s16 lcn);
static struct net_device *
cycx_x25_get_dev_by_dte_addr(struct wan_device *wandev, char *dte);
#ifdef CYCLOMX_X25_DEBUG
static void hex_dump(char *msg, unsigned char *p, int len);
static void cycx_x25_dump_config(struct cycx_x25_config *conf);
static void cycx_x25_dump_stats(struct cycx_x25_stats *stats);
static void cycx_x25_dump_devs(struct wan_device *wandev);
#else
#define hex_dump(msg, p, len)
#define cycx_x25_dump_config(conf)
#define cycx_x25_dump_stats(stats)
#define cycx_x25_dump_devs(wandev)
#endif
/* Public Functions */
/* X.25 Protocol Initialization routine.
*
* This routine is called by the main Cyclom 2X module during setup. At this
* point adapter is completely initialized and X.25 firmware is running.
* o configure adapter
* o initialize protocol-specific fields of the adapter data space.
*
* Return: 0 o.k.
* < 0 failure. */
int cycx_x25_wan_init(struct cycx_device *card, wandev_conf_t *conf)
{
struct cycx_x25_config cfg;
/* 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 = card->hw.dpmbase + X25_MBOX_OFFS;
card->u.x.connection_keys = 0;
spin_lock_init(&card->u.x.lock);
/* Configure adapter. Here we set reasonable 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(&cfg, 0, sizeof(cfg));
cfg.link = 0;
cfg.clock = conf->clocking == WANOPT_EXTERNAL ? 8 : 55;
cfg.speed = bps_to_speed_code(conf->bps);
cfg.n3win = 7;
cfg.n2win = 2;
cfg.n2 = 5;
cfg.nvc = 1;
cfg.npvc = 1;
cfg.flags = 0x02; /* default = V35 */
cfg.t1 = 10; /* line carrier timeout */
cfg.t2 = 29; /* tx timeout */
cfg.t21 = 180; /* CALL timeout */
cfg.t23 = 180; /* CLEAR timeout */
/* adjust MTU */
if (!conf->mtu || 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;
cfg.pktlen = cycx_log2(card->wandev.mtu);
if (conf->station == WANOPT_DTE) {
cfg.locaddr = 3; /* DTE */
cfg.remaddr = 1; /* DCE */
} else {
cfg.locaddr = 1; /* DCE */
cfg.remaddr = 3; /* DTE */
}
if (conf->interface == WANOPT_RS232)
cfg.flags = 0; /* FIXME just reset the 2nd bit */
if (conf->u.x25.hi_pvc) {
card->u.x.hi_pvc = min_t(unsigned int, conf->u.x25.hi_pvc, 4095);
card->u.x.lo_pvc = min_t(unsigned int, conf->u.x25.lo_pvc, card->u.x.hi_pvc);
}
if (conf->u.x25.hi_svc) {
card->u.x.hi_svc = min_t(unsigned int, conf->u.x25.hi_svc, 4095);
card->u.x.lo_svc = min_t(unsigned int, conf->u.x25.lo_svc, card->u.x.hi_svc);
}
if (card->u.x.lo_pvc == 255)
cfg.npvc = 0;
else
cfg.npvc = card->u.x.hi_pvc - card->u.x.lo_pvc + 1;
cfg.nvc = card->u.x.hi_svc - card->u.x.lo_svc + 1 + cfg.npvc;
if (conf->u.x25.hdlc_window)
cfg.n2win = min_t(unsigned int, conf->u.x25.hdlc_window, 7);
if (conf->u.x25.pkt_window)
cfg.n3win = min_t(unsigned int, conf->u.x25.pkt_window, 7);
if (conf->u.x25.t1)
cfg.t1 = min_t(unsigned int, conf->u.x25.t1, 30);
if (conf->u.x25.t2)
cfg.t2 = min_t(unsigned int, conf->u.x25.t2, 30);
if (conf->u.x25.t11_t21)
cfg.t21 = min_t(unsigned int, conf->u.x25.t11_t21, 30);
if (conf->u.x25.t13_t23)
cfg.t23 = min_t(unsigned int, conf->u.x25.t13_t23, 30);
if (conf->u.x25.n2)
cfg.n2 = min_t(unsigned int, conf->u.x25.n2, 30);
/* initialize adapter */
if (cycx_x25_configure(card, &cfg))
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 = cycx_x25_irq_handler;
card->exec = NULL;
card->wandev.update = cycx_wan_update;
card->wandev.new_if = cycx_wan_new_if;
card->wandev.del_if = cycx_wan_del_if;
card->wandev.state = WAN_DISCONNECTED;
return 0;
}
/* WAN Device Driver Entry Points */
/* Update device status & statistics. */
static int cycx_wan_update(struct wan_device *wandev)
{
/* sanity checks */
if (!wandev || !wandev->private)
return -EFAULT;
if (wandev->state == WAN_UNCONFIGURED)
return -ENODEV;
cycx_x25_get_stats(wandev->private);
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 registration.
*
* Return: 0 o.k.
* < 0 failure (channel will not be created) */
static int cycx_wan_new_if(struct wan_device *wandev, struct net_device *dev,
wanif_conf_t *conf)
{
struct cycx_device *card = wandev->private;
struct cycx_x25_channel *chan;
int err = 0;
if (!conf->name[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 = kzalloc(sizeof(struct cycx_x25_channel), GFP_KERNEL);
if (!chan)
return -ENOMEM;
strcpy(chan->name, conf->name);
chan->card = card;
chan->link = conf->port;
chan->protocol = conf->protocol ? ETH_P_X25 : ETH_P_IP;
chan->rx_skb = NULL;
/* only used in svc connected thru crossover cable */
chan->local_addr = NULL;
if (conf->addr[0] == '@') { /* SVC */
int len = strlen(conf->local_addr);
if (len) {
if (len > WAN_ADDRESS_SZ) {
printk(KERN_ERR "%s: %s local addr too long!\n",
wandev->name, chan->name);
kfree(chan);
return -EINVAL;
} else {
chan->local_addr = kmalloc(len + 1, GFP_KERNEL);
if (!chan->local_addr) {
kfree(chan);
return -ENOMEM;
}
}
strncpy(chan->local_addr, conf->local_addr,
WAN_ADDRESS_SZ);
}
chan->svc = 1;
strncpy(chan->addr, &conf->addr[1], WAN_ADDRESS_SZ);
init_timer(&chan->timer);
chan->timer.function = cycx_x25_chan_timer;
chan->timer.data = (unsigned long)dev;
/* Set channel timeouts (default if not specified) */
chan->idle_tmout = conf->idle_timeout ? conf->idle_timeout : 90;
} else if (isdigit(conf->addr[0])) { /* PVC */
s16 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->local_addr);
kfree(chan);
return err;
}
/* prepare network device data space for registration */
strcpy(dev->name, chan->name);
dev->init = cycx_netdevice_init;
dev->priv = chan;
return 0;
}
/* Delete logical channel. */
static int cycx_wan_del_if(struct wan_device *wandev, struct net_device *dev)
{
if (dev->priv) {
struct cycx_x25_channel *chan = dev->priv;
if (chan->svc) {
kfree(chan->local_addr);
if (chan->state == WAN_CONNECTED)
del_timer(&chan->timer);
}
kfree(chan);
dev->priv = NULL;
}
return 0;
}
/* Network Device Interface */
static const struct header_ops cycx_header_ops = {
.create = cycx_netdevice_hard_header,
.rebuild = cycx_netdevice_rebuild_header,
};
/* 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 cycx_netdevice_init(struct net_device *dev)
{
struct cycx_x25_channel *chan = dev->priv;
struct cycx_device *card = chan->card;
struct wan_device *wandev = &card->wandev;
/* Initialize device driver entry points */
dev->open = cycx_netdevice_open;
dev->stop = cycx_netdevice_stop;
dev->header_ops = &cycx_header_ops;
dev->hard_start_xmit = cycx_netdevice_hard_start_xmit;
dev->get_stats = cycx_netdevice_get_stats;
/* Initialize media-specific parameters */
dev->mtu = CYCX_X25_CHAN_MTU;
dev->type = ARPHRD_HWX25; /* ARP h/w type */
dev->hard_header_len = 0; /* media header length */
dev->addr_len = 0; /* hardware address length */
if (!chan->svc)
*(__be16*)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 = (unsigned long)(wandev->maddr +
wandev->msize - 1);
dev->flags |= IFF_NOARP;
/* Set transmit buffer queue length */
dev->tx_queue_len = 10;
/* Initialize socket buffers */
cycx_x25_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 cycx_netdevice_open(struct net_device *dev)
{
if (netif_running(dev))
return -EBUSY; /* only one open is allowed */
netif_start_queue(dev);
return 0;
}
/* Close network interface.
* o reset flags.
* o if there's no more open channels then disconnect physical link. */
static int cycx_netdevice_stop(struct net_device *dev)
{
struct cycx_x25_channel *chan = dev->priv;
netif_stop_queue(dev);
if (chan->state == WAN_CONNECTED || chan->state == WAN_CONNECTING)
cycx_x25_chan_disconnect(dev);
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 cycx_netdevice_hard_header(struct sk_buff *skb,
struct net_device *dev, u16 type,
const void *daddr, const void *saddr,
unsigned len)
{
skb->protocol = htons(type);
return dev->hard_header_len;
}
/* * Re-build media header.
* Return: 1 physical address resolved.
* 0 physical address not resolved */
static int cycx_netdevice_rebuild_header(struct sk_buff *skb)
{
return 1;
}
/* Send a packet on a network interface.
* o set busy 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 cycx_netdevice_hard_start_xmit(struct sk_buff *skb,
struct net_device *dev)
{
struct cycx_x25_channel *chan = dev->priv;
struct cycx_device *card = chan->card;
if (!chan->svc)
chan->protocol = ntohs(skb->protocol);
if (card->wandev.state != WAN_CONNECTED)
++chan->ifstats.tx_dropped;
else if (chan->svc && chan->protocol &&
chan->protocol != ntohs(skb->protocol)) {
printk(KERN_INFO
"%s: unsupported Ethertype 0x%04X on interface %s!\n",
card->devname, ntohs(skb->protocol), dev->name);
++chan->ifstats.tx_errors;
} else if (chan->protocol == ETH_P_IP) {
switch (chan->state) {
case WAN_DISCONNECTED:
if (cycx_x25_chan_connect(dev)) {
netif_stop_queue(dev);
return -EBUSY;
}
/* fall thru */
case WAN_CONNECTED:
reset_timer(dev);
dev->trans_start = jiffies;
netif_stop_queue(dev);
if (cycx_x25_chan_send(dev, skb))
return -EBUSY;
break;
default:
++chan->ifstats.tx_dropped;
++card->wandev.stats.tx_dropped;
}
} else { /* chan->protocol == ETH_P_X25 */
switch (skb->data[0]) {
case 0: break;
case 1: /* Connect request */
cycx_x25_chan_connect(dev);
goto free_packet;
case 2: /* Disconnect request */
cycx_x25_chan_disconnect(dev);
goto free_packet;
default:
printk(KERN_INFO
"%s: unknown %d x25-iface request on %s!\n",
card->devname, skb->data[0], dev->name);
++chan->ifstats.tx_errors;
goto free_packet;
}
skb_pull(skb, 1); /* Remove control byte */
reset_timer(dev);
dev->trans_start = jiffies;
netif_stop_queue(dev);
if (cycx_x25_chan_send(dev, skb)) {
/* prepare for future retransmissions */
skb_push(skb, 1);
return -EBUSY;
}
}
free_packet:
dev_kfree_skb(skb);
return 0;
}
/* Get Ethernet-style interface statistics.
* Return a pointer to struct net_device_stats */
static struct net_device_stats *cycx_netdevice_get_stats(struct net_device *dev)
{
struct cycx_x25_channel *chan = dev->priv;
return chan ? &chan->ifstats : NULL;
}
/* Interrupt Handlers */
/* X.25 Interrupt Service Routine. */
static void cycx_x25_irq_handler(struct cycx_device *card)
{
struct cycx_x25_cmd cmd;
u16 z = 0;
card->in_isr = 1;
card->buff_int_mode_unbusy = 0;
cycx_peek(&card->hw, X25_RXMBOX_OFFS, &cmd, sizeof(cmd));
switch (cmd.command) {
case X25_DATA_INDICATION:
cycx_x25_irq_rx(card, &cmd);
break;
case X25_ACK_FROM_VC:
cycx_x25_irq_tx(card, &cmd);
break;
case X25_LOG:
cycx_x25_irq_log(card, &cmd);
break;
case X25_STATISTIC:
cycx_x25_irq_stat(card, &cmd);
break;
case X25_CONNECT_CONFIRM:
cycx_x25_irq_connect_confirm(card, &cmd);
break;
case X25_CONNECT_INDICATION:
cycx_x25_irq_connect(card, &cmd);
break;
case X25_DISCONNECT_INDICATION:
cycx_x25_irq_disconnect(card, &cmd);
break;
case X25_DISCONNECT_CONFIRM:
cycx_x25_irq_disconnect_confirm(card, &cmd);
break;
case X25_LINE_ON:
cycx_set_state(card, WAN_CONNECTED);
break;
case X25_LINE_OFF:
cycx_set_state(card, WAN_DISCONNECTED);
break;
default:
cycx_x25_irq_spurious(card, &cmd);
break;
}
cycx_poke(&card->hw, 0, &z, sizeof(z));
cycx_poke(&card->hw, X25_RXMBOX_OFFS, &z, sizeof(z));
card->in_isr = 0;
}
/* Transmit interrupt handler.
* o Release socket buffer
* o Clear 'tbusy' flag */
static void cycx_x25_irq_tx(struct cycx_device *card, struct cycx_x25_cmd *cmd)
{
struct net_device *dev;
struct wan_device *wandev = &card->wandev;
u8 lcn;
cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
/* unbusy device and then dev_tint(); */
dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
if (dev) {
card->buff_int_mode_unbusy = 1;
netif_wake_queue(dev);
} else
printk(KERN_ERR "%s:ackvc for inexistent lcn %d\n",
card->devname, lcn);
}
/* Receive interrupt handler.
* This routine handles fragmented IP packets using M-bit according to the
* RFC1356.
* o map logical 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
* coming 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 cycx_x25_irq_rx(struct cycx_device *card, struct cycx_x25_cmd *cmd)
{
struct wan_device *wandev = &card->wandev;
struct net_device *dev;
struct cycx_x25_channel *chan;
struct sk_buff *skb;
u8 bitm, lcn;
int pktlen = cmd->len - 5;
cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
cycx_peek(&card->hw, cmd->buf + 4, &bitm, sizeof(bitm));
bitm &= 0x10;
dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
if (!dev) {
/* Invalid channel, discard packet */
printk(KERN_INFO "%s: receiving on orphaned LCN %d!\n",
card->devname, lcn);
return;
}
chan = dev->priv;
reset_timer(dev);
if (chan->drop_sequence) {
if (!bitm)
chan->drop_sequence = 0;
else
return;
}
if ((skb = chan->rx_skb) == NULL) {
/* Allocate new socket buffer */
int bufsize = bitm ? dev->mtu : pktlen;
if ((skb = dev_alloc_skb((chan->protocol == ETH_P_X25 ? 1 : 0) +
bufsize +
dev->hard_header_len)) == NULL) {
printk(KERN_INFO "%s: no socket buffers available!\n",
card->devname);
chan->drop_sequence = 1;
++chan->ifstats.rx_dropped;
return;
}
if (chan->protocol == ETH_P_X25) /* X.25 socket layer control */
/* 0 = data packet (dev_alloc_skb zeroed skb->data) */
skb_put(skb, 1);
skb->dev = dev;
skb->protocol = htons(chan->protocol);
chan->rx_skb = skb;
}
if (skb_tailroom(skb) < pktlen) {
/* No room for the packet. Call off the whole thing! */
dev_kfree_skb_irq(skb);
chan->rx_skb = NULL;
if (bitm)
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 */
cycx_peek(&card->hw, cmd->buf + 5, skb_put(skb, pktlen), pktlen);
if (bitm)
return; /* more data is coming */
chan->rx_skb = NULL; /* dequeue packet */
++chan->ifstats.rx_packets;
chan->ifstats.rx_bytes += pktlen;
skb_reset_mac_header(skb);
netif_rx(skb);
}
/* Connect interrupt handler. */
static void cycx_x25_irq_connect(struct cycx_device *card,
struct cycx_x25_cmd *cmd)
{
struct wan_device *wandev = &card->wandev;
struct net_device *dev = NULL;
struct cycx_x25_channel *chan;
u8 d[32],
loc[24],
rem[24];
u8 lcn, sizeloc, sizerem;
cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
cycx_peek(&card->hw, cmd->buf + 5, &sizeloc, sizeof(sizeloc));
cycx_peek(&card->hw, cmd->buf + 6, d, cmd->len - 6);
sizerem = sizeloc >> 4;
sizeloc &= 0x0F;
loc[0] = rem[0] = '\0';
if (sizeloc)
nibble_to_byte(d, loc, sizeloc, 0);
if (sizerem)
nibble_to_byte(d + (sizeloc >> 1), rem, sizerem, sizeloc & 1);
dprintk(1, KERN_INFO "%s:lcn=%d, local=%s, remote=%s\n",
__func__, lcn, loc, rem);
dev = cycx_x25_get_dev_by_dte_addr(wandev, rem);
if (!dev) {
/* Invalid channel, discard packet */
printk(KERN_INFO "%s: connect not expected: remote %s!\n",
card->devname, rem);
return;
}
chan = dev->priv;
chan->lcn = lcn;
cycx_x25_connect_response(card, chan);
cycx_x25_set_chan_state(dev, WAN_CONNECTED);
}
/* Connect confirm interrupt handler. */
static void cycx_x25_irq_connect_confirm(struct cycx_device *card,
struct cycx_x25_cmd *cmd)
{
struct wan_device *wandev = &card->wandev;
struct net_device *dev;
struct cycx_x25_channel *chan;
u8 lcn, key;
cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
cycx_peek(&card->hw, cmd->buf + 1, &key, sizeof(key));
dprintk(1, KERN_INFO "%s: %s:lcn=%d, key=%d\n",
card->devname, __func__, lcn, key);
dev = cycx_x25_get_dev_by_lcn(wandev, -key);
if (!dev) {
/* Invalid channel, discard packet */
clear_bit(--key, (void*)&card->u.x.connection_keys);
printk(KERN_INFO "%s: connect confirm not expected: lcn %d, "
"key=%d!\n", card->devname, lcn, key);
return;
}
clear_bit(--key, (void*)&card->u.x.connection_keys);
chan = dev->priv;
chan->lcn = lcn;
cycx_x25_set_chan_state(dev, WAN_CONNECTED);
}
/* Disconnect confirm interrupt handler. */
static void cycx_x25_irq_disconnect_confirm(struct cycx_device *card,
struct cycx_x25_cmd *cmd)
{
struct wan_device *wandev = &card->wandev;
struct net_device *dev;
u8 lcn;
cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
dprintk(1, KERN_INFO "%s: %s:lcn=%d\n",
card->devname, __func__, lcn);
dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
if (!dev) {
/* Invalid channel, discard packet */
printk(KERN_INFO "%s:disconnect confirm not expected!:lcn %d\n",
card->devname, lcn);
return;
}
cycx_x25_set_chan_state(dev, WAN_DISCONNECTED);
}
/* disconnect interrupt handler. */
static void cycx_x25_irq_disconnect(struct cycx_device *card,
struct cycx_x25_cmd *cmd)
{
struct wan_device *wandev = &card->wandev;
struct net_device *dev;
u8 lcn;
cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
dprintk(1, KERN_INFO "%s:lcn=%d\n", __func__, lcn);
dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
if (dev) {
struct cycx_x25_channel *chan = dev->priv;
cycx_x25_disconnect_response(card, chan->link, lcn);
cycx_x25_set_chan_state(dev, WAN_DISCONNECTED);
} else
cycx_x25_disconnect_response(card, 0, lcn);
}
/* LOG interrupt handler. */
static void cycx_x25_irq_log(struct cycx_device *card, struct cycx_x25_cmd *cmd)
{
#if CYCLOMX_X25_DEBUG
char bf[20];
u16 size, toread, link, msg_code;
u8 code, routine;
cycx_peek(&card->hw, cmd->buf, &msg_code, sizeof(msg_code));
cycx_peek(&card->hw, cmd->buf + 2, &link, sizeof(link));
cycx_peek(&card->hw, cmd->buf + 4, &size, sizeof(size));
/* at most 20 bytes are available... thanks to Daniela :) */
toread = size < 20 ? size : 20;
cycx_peek(&card->hw, cmd->buf + 10, &bf, toread);
cycx_peek(&card->hw, cmd->buf + 10 + toread, &code, 1);
cycx_peek(&card->hw, cmd->buf + 10 + toread + 1, &routine, 1);
printk(KERN_INFO "cycx_x25_irq_handler: X25_LOG (0x4500) indic.:\n");
printk(KERN_INFO "cmd->buf=0x%X\n", cmd->buf);
printk(KERN_INFO "Log message code=0x%X\n", msg_code);
printk(KERN_INFO "Link=%d\n", link);
printk(KERN_INFO "log code=0x%X\n", code);
printk(KERN_INFO "log routine=0x%X\n", routine);
printk(KERN_INFO "Message size=%d\n", size);
hex_dump("Message", bf, toread);
#endif
}
/* STATISTIC interrupt handler. */
static void cycx_x25_irq_stat(struct cycx_device *card,
struct cycx_x25_cmd *cmd)
{
cycx_peek(&card->hw, cmd->buf, &card->u.x.stats,
sizeof(card->u.x.stats));
hex_dump("cycx_x25_irq_stat", (unsigned char*)&card->u.x.stats,
sizeof(card->u.x.stats));
cycx_x25_dump_stats(&card->u.x.stats);
wake_up_interruptible(&card->wait_stats);
}
/* Spurious interrupt handler.
* o print a warning
* If number of spurious interrupts exceeded some limit, then ??? */
static void cycx_x25_irq_spurious(struct cycx_device *card,
struct cycx_x25_cmd *cmd)
{
printk(KERN_INFO "%s: spurious interrupt (0x%X)!\n",
card->devname, cmd->command);
}
#ifdef CYCLOMX_X25_DEBUG
static void hex_dump(char *msg, unsigned char *p, int len)
{
unsigned char hex[1024],
* phex = hex;
if (len >= (sizeof(hex) / 2))
len = (sizeof(hex) / 2) - 1;
while (len--) {
sprintf(phex, "%02x", *p++);
phex += 2;
}
printk(KERN_INFO "%s: %s\n", msg, hex);
}
#endif
/* Cyclom 2X Firmware-Specific Functions */
/* Exec X.25 command. */
static int x25_exec(struct cycx_device *card, int command, int link,
void *d1, int len1, void *d2, int len2)
{
struct cycx_x25_cmd c;
unsigned long flags;
u32 addr = 0x1200 + 0x2E0 * link + 0x1E2;
u8 retry = CYCX_X25_MAX_CMD_RETRY;
int err = 0;
c.command = command;
c.link = link;
c.len = len1 + len2;
spin_lock_irqsave(&card->u.x.lock, flags);
/* write command */
cycx_poke(&card->hw, X25_MBOX_OFFS, &c, sizeof(c) - sizeof(c.buf));
/* write X.25 data */
if (d1) {
cycx_poke(&card->hw, addr, d1, len1);
if (d2) {
if (len2 > 254) {
u32 addr1 = 0xA00 + 0x400 * link;
cycx_poke(&card->hw, addr + len1, d2, 249);
cycx_poke(&card->hw, addr1, ((u8*)d2) + 249,
len2 - 249);
} else
cycx_poke(&card->hw, addr + len1, d2, len2);
}
}
/* generate interruption, executing command */
cycx_intr(&card->hw);
/* wait till card->mbox == 0 */
do {
err = cycx_exec(card->mbox);
} while (retry-- && err);
spin_unlock_irqrestore(&card->u.x.lock, flags);
return err;
}
/* Configure adapter. */
static int cycx_x25_configure(struct cycx_device *card,
struct cycx_x25_config *conf)
{
struct {
u16 nlinks;
struct cycx_x25_config conf[2];
} x25_cmd_conf;
memset(&x25_cmd_conf, 0, sizeof(x25_cmd_conf));
x25_cmd_conf.nlinks = 2;
x25_cmd_conf.conf[0] = *conf;
/* FIXME: we need to find a way in the wanrouter framework
to configure the second link, for now lets use it
with the same config from the first link, fixing
the interface type to RS232, the speed in 38400 and
the clock to external */
x25_cmd_conf.conf[1] = *conf;
x25_cmd_conf.conf[1].link = 1;
x25_cmd_conf.conf[1].speed = 5; /* 38400 */
x25_cmd_conf.conf[1].clock = 8;
x25_cmd_conf.conf[1].flags = 0; /* default = RS232 */
cycx_x25_dump_config(&x25_cmd_conf.conf[0]);
cycx_x25_dump_config(&x25_cmd_conf.conf[1]);
return x25_exec(card, X25_CONFIG, 0,
&x25_cmd_conf, sizeof(x25_cmd_conf), NULL, 0);
}
/* Get protocol statistics. */
static int cycx_x25_get_stats(struct cycx_device *card)
{
/* the firmware expects 20 in the size field!!!
thanks to Daniela */
int err = x25_exec(card, X25_STATISTIC, 0, NULL, 20, NULL, 0);
if (err)
return err;
interruptible_sleep_on(&card->wait_stats);
if (signal_pending(current))
return -EINTR;
card->wandev.stats.rx_packets = card->u.x.stats.n2_rx_frames;
card->wandev.stats.rx_over_errors = card->u.x.stats.rx_over_errors;
card->wandev.stats.rx_crc_errors = card->u.x.stats.rx_crc_errors;
card->wandev.stats.rx_length_errors = 0; /* not available from fw */
card->wandev.stats.rx_frame_errors = 0; /* not available from fw */
card->wandev.stats.rx_missed_errors = card->u.x.stats.rx_aborts;
card->wandev.stats.rx_dropped = 0; /* not available from fw */
card->wandev.stats.rx_errors = 0; /* not available from fw */
card->wandev.stats.tx_packets = card->u.x.stats.n2_tx_frames;
card->wandev.stats.tx_aborted_errors = card->u.x.stats.tx_aborts;
card->wandev.stats.tx_dropped = 0; /* not available from fw */
card->wandev.stats.collisions = 0; /* not available from fw */
card->wandev.stats.tx_errors = 0; /* not available from fw */
cycx_x25_dump_devs(&card->wandev);
return 0;
}
/* return the number of nibbles */
static int byte_to_nibble(u8 *s, u8 *d, char *nibble)
{
int i = 0;
if (*nibble && *s) {
d[i] |= *s++ - '0';
*nibble = 0;
++i;
}
while (*s) {
d[i] = (*s - '0') << 4;
if (*(s + 1))
d[i] |= *(s + 1) - '0';
else {
*nibble = 1;
break;
}
++i;
s += 2;
}
return i;
}
static void nibble_to_byte(u8 *s, u8 *d, u8 len, u8 nibble)
{
if (nibble) {
*d++ = '0' + (*s++ & 0x0F);
--len;
}
while (len) {
*d++ = '0' + (*s >> 4);
if (--len) {
*d++ = '0' + (*s & 0x0F);
--len;
} else break;
++s;
}
*d = '\0';
}
/* Place X.25 call. */
static int x25_place_call(struct cycx_device *card,
struct cycx_x25_channel *chan)
{
int err = 0,
len;
char d[64],
nibble = 0,
mylen = chan->local_addr ? strlen(chan->local_addr) : 0,
remotelen = strlen(chan->addr);
u8 key;
if (card->u.x.connection_keys == ~0U) {
printk(KERN_INFO "%s: too many simultaneous connection "
"requests!\n", card->devname);
return -EAGAIN;
}
key = ffz(card->u.x.connection_keys);
set_bit(key, (void*)&card->u.x.connection_keys);
++key;
dprintk(1, KERN_INFO "%s:x25_place_call:key=%d\n", card->devname, key);
memset(d, 0, sizeof(d));
d[1] = key; /* user key */
d[2] = 0x10;
d[4] = 0x0B;
len = byte_to_nibble(chan->addr, d + 6, &nibble);
if (chan->local_addr)
len += byte_to_nibble(chan->local_addr, d + 6 + len, &nibble);
if (nibble)
++len;
d[5] = mylen << 4 | remotelen;
d[6 + len + 1] = 0xCC; /* TCP/IP over X.25, thanks to Daniela :) */
if ((err = x25_exec(card, X25_CONNECT_REQUEST, chan->link,
&d, 7 + len + 1, NULL, 0)) != 0)
clear_bit(--key, (void*)&card->u.x.connection_keys);
else
chan->lcn = -key;
return err;
}
/* Place X.25 CONNECT RESPONSE. */
static int cycx_x25_connect_response(struct cycx_device *card,
struct cycx_x25_channel *chan)
{
u8 d[8];
memset(d, 0, sizeof(d));
d[0] = d[3] = chan->lcn;
d[2] = 0x10;
d[4] = 0x0F;
d[7] = 0xCC; /* TCP/IP over X.25, thanks Daniela */
return x25_exec(card, X25_CONNECT_RESPONSE, chan->link, &d, 8, NULL, 0);
}
/* Place X.25 DISCONNECT RESPONSE. */
static int cycx_x25_disconnect_response(struct cycx_device *card, u8 link,
u8 lcn)
{
char d[5];
memset(d, 0, sizeof(d));
d[0] = d[3] = lcn;
d[2] = 0x10;
d[4] = 0x17;
return x25_exec(card, X25_DISCONNECT_RESPONSE, link, &d, 5, NULL, 0);
}
/* Clear X.25 call. */
static int x25_clear_call(struct cycx_device *card, u8 link, u8 lcn, u8 cause,
u8 diagn)
{
u8 d[7];
memset(d, 0, sizeof(d));
d[0] = d[3] = lcn;
d[2] = 0x10;
d[4] = 0x13;
d[5] = cause;
d[6] = diagn;
return x25_exec(card, X25_DISCONNECT_REQUEST, link, d, 7, NULL, 0);
}
/* Send X.25 data packet. */
static int cycx_x25_send(struct cycx_device *card, u8 link, u8 lcn, u8 bitm,
int len, void *buf)
{
u8 d[] = "?\xFF\x10??";
d[0] = d[3] = lcn;
d[4] = bitm;
return x25_exec(card, X25_DATA_REQUEST, link, &d, 5, buf, len);
}
/* Miscellaneous */
/* Find network device by its channel number. */
static struct net_device *cycx_x25_get_dev_by_lcn(struct wan_device *wandev,
s16 lcn)
{
struct net_device *dev = wandev->dev;
struct cycx_x25_channel *chan;
while (dev) {
chan = (struct cycx_x25_channel*)dev->priv;
if (chan->lcn == lcn)
break;
dev = chan->slave;
}
return dev;
}
/* Find network device by its remote dte address. */
static struct net_device *
cycx_x25_get_dev_by_dte_addr(struct wan_device *wandev, char *dte)
{
struct net_device *dev = wandev->dev;
struct cycx_x25_channel *chan;
while (dev) {
chan = (struct cycx_x25_channel*)dev->priv;
if (!strcmp(chan->addr, dte))
break;
dev = chan->slave;
}
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 cycx_x25_chan_connect(struct net_device *dev)
{
struct cycx_x25_channel *chan = dev->priv;
struct cycx_device *card = chan->card;
if (chan->svc) {
if (!chan->addr[0])
return -EINVAL; /* no destination address */
dprintk(1, KERN_INFO "%s: placing X.25 call to %s...\n",
card->devname, chan->addr);
if (x25_place_call(card, chan))
return -EIO;
cycx_x25_set_chan_state(dev, WAN_CONNECTING);
return 1;
} else
cycx_x25_set_chan_state(dev, WAN_CONNECTED);
return 0;
}
/* Disconnect logical channel.
* o if SVC then clear X.25 call */
static void cycx_x25_chan_disconnect(struct net_device *dev)
{
struct cycx_x25_channel *chan = dev->priv;
if (chan->svc) {
x25_clear_call(chan->card, chan->link, chan->lcn, 0, 0);
cycx_x25_set_chan_state(dev, WAN_DISCONNECTING);
} else
cycx_x25_set_chan_state(dev, WAN_DISCONNECTED);
}
/* Called by kernel timer */
static void cycx_x25_chan_timer(unsigned long d)
{
struct net_device *dev = (struct net_device *)d;
struct cycx_x25_channel *chan = dev->priv;
if (chan->state == WAN_CONNECTED)
cycx_x25_chan_disconnect(dev);
else
printk(KERN_ERR "%s: %s for svc (%s) not connected!\n",
chan->card->devname, __func__, dev->name);
}
/* Set logical channel state. */
static void cycx_x25_set_chan_state(struct net_device *dev, u8 state)
{
struct cycx_x25_channel *chan = dev->priv;
struct cycx_device *card = chan->card;
unsigned long flags;
char *string_state = NULL;
spin_lock_irqsave(&card->lock, flags);
if (chan->state != state) {
if (chan->svc && chan->state == WAN_CONNECTED)
del_timer(&chan->timer);
switch (state) {
case WAN_CONNECTED:
string_state = "connected!";
*(__be16*)dev->dev_addr = htons(chan->lcn);
netif_wake_queue(dev);
reset_timer(dev);
if (chan->protocol == ETH_P_X25)
cycx_x25_chan_send_event(dev, 1);
break;
case WAN_CONNECTING:
string_state = "connecting...";
break;
case WAN_DISCONNECTING:
string_state = "disconnecting...";
break;
case WAN_DISCONNECTED:
string_state = "disconnected!";
if (chan->svc) {
*(unsigned short*)dev->dev_addr = 0;
chan->lcn = 0;
}
if (chan->protocol == ETH_P_X25)
cycx_x25_chan_send_event(dev, 2);
netif_wake_queue(dev);
break;
}
printk(KERN_INFO "%s: interface %s %s\n", card->devname,
dev->name, string_state);
chan->state = state;
}
spin_unlock_irqrestore(&card->lock, 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 cycx_x25_chan_send(struct net_device *dev, struct sk_buff *skb)
{
struct cycx_x25_channel *chan = dev->priv;
struct cycx_device *card = chan->card;
int bitm = 0; /* final packet */
unsigned len = skb->len;
if (skb->len > card->wandev.mtu) {
len = card->wandev.mtu;
bitm = 0x10; /* set M-bit (more data) */
}
if (cycx_x25_send(card, chan->link, chan->lcn, bitm, len, skb->data))
return 1;
if (bitm) {
skb_pull(skb, len);
return 1;
}
++chan->ifstats.tx_packets;
chan->ifstats.tx_bytes += len;
return 0;
}
/* Send event (connection, disconnection, etc) to X.25 socket layer */
static void cycx_x25_chan_send_event(struct net_device *dev, u8 event)
{
struct sk_buff *skb;
unsigned char *ptr;
if ((skb = dev_alloc_skb(1)) == NULL) {
printk(KERN_ERR "%s: out of memory\n", __func__);
return;
}
ptr = skb_put(skb, 1);
*ptr = event;
skb->protocol = x25_type_trans(skb, dev);
netif_rx(skb);
}
/* Convert line speed in bps to a number used by cyclom 2x code. */
static u8 bps_to_speed_code(u32 bps)
{
u8 number = 0; /* defaults to the lowest (1200) speed ;> */
if (bps >= 512000) number = 8;
else if (bps >= 256000) number = 7;
else if (bps >= 64000) number = 6;
else if (bps >= 38400) number = 5;
else if (bps >= 19200) number = 4;
else if (bps >= 9600) number = 3;
else if (bps >= 4800) number = 2;
else if (bps >= 2400) number = 1;
return number;
}
/* log base 2 */
static u8 cycx_log2(u32 n)
{
u8 log = 0;
if (!n)
return 0;
while (n > 1) {
n >>= 1;
++log;
}
return log;
}
/* Convert decimal string to unsigned integer.
* If len != 0 then only 'len' characters of the string are converted. */
static unsigned dec_to_uint(u8 *str, int len)
{
unsigned val = 0;
if (!len)
len = strlen(str);
for (; len && isdigit(*str); ++str, --len)
val = (val * 10) + (*str - (unsigned) '0');
return val;
}
static void reset_timer(struct net_device *dev)
{
struct cycx_x25_channel *chan = dev->priv;
if (chan->svc)
mod_timer(&chan->timer, jiffies+chan->idle_tmout*HZ);
}
#ifdef CYCLOMX_X25_DEBUG
static void cycx_x25_dump_config(struct cycx_x25_config *conf)
{
printk(KERN_INFO "X.25 configuration\n");
printk(KERN_INFO "-----------------\n");
printk(KERN_INFO "link number=%d\n", conf->link);
printk(KERN_INFO "line speed=%d\n", conf->speed);
printk(KERN_INFO "clock=%sternal\n", conf->clock == 8 ? "Ex" : "In");
printk(KERN_INFO "# level 2 retransm.=%d\n", conf->n2);
printk(KERN_INFO "level 2 window=%d\n", conf->n2win);
printk(KERN_INFO "level 3 window=%d\n", conf->n3win);
printk(KERN_INFO "# logical channels=%d\n", conf->nvc);
printk(KERN_INFO "level 3 pkt len=%d\n", conf->pktlen);
printk(KERN_INFO "my address=%d\n", conf->locaddr);
printk(KERN_INFO "remote address=%d\n", conf->remaddr);
printk(KERN_INFO "t1=%d seconds\n", conf->t1);
printk(KERN_INFO "t2=%d seconds\n", conf->t2);
printk(KERN_INFO "t21=%d seconds\n", conf->t21);
printk(KERN_INFO "# PVCs=%d\n", conf->npvc);
printk(KERN_INFO "t23=%d seconds\n", conf->t23);
printk(KERN_INFO "flags=0x%x\n", conf->flags);
}
static void cycx_x25_dump_stats(struct cycx_x25_stats *stats)
{
printk(KERN_INFO "X.25 statistics\n");
printk(KERN_INFO "--------------\n");
printk(KERN_INFO "rx_crc_errors=%d\n", stats->rx_crc_errors);
printk(KERN_INFO "rx_over_errors=%d\n", stats->rx_over_errors);
printk(KERN_INFO "n2_tx_frames=%d\n", stats->n2_tx_frames);
printk(KERN_INFO "n2_rx_frames=%d\n", stats->n2_rx_frames);
printk(KERN_INFO "tx_timeouts=%d\n", stats->tx_timeouts);
printk(KERN_INFO "rx_timeouts=%d\n", stats->rx_timeouts);
printk(KERN_INFO "n3_tx_packets=%d\n", stats->n3_tx_packets);
printk(KERN_INFO "n3_rx_packets=%d\n", stats->n3_rx_packets);
printk(KERN_INFO "tx_aborts=%d\n", stats->tx_aborts);
printk(KERN_INFO "rx_aborts=%d\n", stats->rx_aborts);
}
static void cycx_x25_dump_devs(struct wan_device *wandev)
{
struct net_device *dev = wandev->dev;
printk(KERN_INFO "X.25 dev states\n");
printk(KERN_INFO "name: addr: txoff: protocol:\n");
printk(KERN_INFO "---------------------------------------\n");
while(dev) {
struct cycx_x25_channel *chan = dev->priv;
printk(KERN_INFO "%-5.5s %-15.15s %d ETH_P_%s\n",
chan->name, chan->addr, netif_queue_stopped(dev),
chan->protocol == ETH_P_IP ? "IP" : "X25");
dev = chan->slave;
}
}
#endif /* CYCLOMX_X25_DEBUG */
/* End */
|