79 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
81 #define CYCLOMX_X25_DEBUG 1
83 #include <linux/ctype.h>
84 #include <linux/errno.h>
85 #include <linux/if_arp.h>
86 #include <linux/kernel.h>
87 #include <linux/module.h>
88 #include <linux/string.h>
89 #include <linux/sched.h>
90 #include <linux/slab.h>
91 #include <linux/stddef.h>
92 #include <linux/wanrouter.h>
94 #include <asm/byteorder.h>
102 #define CYCX_X25_MAX_CMD_RETRY 5
103 #define CYCX_X25_CHAN_MTU 2048
131 static int cycx_wan_update(
struct wan_device *wandev),
140 static int cycx_netdevice_hard_header(
struct sk_buff *
skb,
144 static int cycx_netdevice_rebuild_header(
struct sk_buff *
skb);
152 static void cycx_x25_irq_handler(
struct cycx_device *
card),
171 static int cycx_x25_configure(
struct cycx_device *
card,
185 static void cycx_x25_chan_disconnect(
struct net_device *
dev),
192 static void nibble_to_byte(
u8 *
s,
u8 *
d,
u8 len,
u8 nibble),
195 static u8 bps_to_speed_code(
u32 bps);
196 static u8 cycx_log2(
u32 n);
198 static unsigned dec_to_uint(
u8 *
str,
int len);
203 cycx_x25_get_dev_by_dte_addr(
struct wan_device *wandev,
char *dte);
207 #ifdef CYCLOMX_X25_DEBUG
208 static void hex_dump(
char *
msg,
unsigned char *
p,
int len);
211 static void cycx_x25_dump_devs(
struct wan_device *wandev);
213 #define hex_dump(msg, p, len)
214 #define cycx_x25_dump_config(conf)
215 #define cycx_x25_dump_stats(stats)
216 #define cycx_x25_dump_devs(wandev)
235 pr_info(
"%s: invalid configuration ID %u!\n",
242 card->u.x.connection_keys = 0;
250 memset(&cfg, 0,
sizeof(cfg));
253 cfg.
speed = bps_to_speed_code(conf->
bps);
266 if (!conf->
mtu || conf->
mtu >= 512)
267 card->wandev.mtu = 512;
268 else if (conf->
mtu >= 256)
269 card->wandev.mtu = 256;
270 else if (conf->
mtu >= 128)
271 card->wandev.mtu = 128;
273 card->wandev.mtu = 64;
275 cfg.
pktlen = cycx_log2(card->wandev.mtu);
290 card->u.x.lo_pvc =
min_t(
unsigned int, conf->
u.
x25.
lo_pvc, card->u.x.hi_pvc);
295 card->u.x.lo_svc =
min_t(
unsigned int, conf->
u.
x25.
lo_svc, card->u.x.hi_svc);
298 if (card->u.x.lo_pvc == 255)
301 cfg.
npvc = card->u.x.hi_pvc - card->u.x.lo_pvc + 1;
303 cfg.
nvc = card->u.x.hi_svc - card->u.x.lo_svc + 1 + cfg.
npvc;
327 if (cycx_x25_configure(card, &cfg))
331 card->wandev.bps = conf->
bps;
332 card->wandev.interface = conf->
interface;
333 card->wandev.clocking = conf->
clocking;
334 card->wandev.station = conf->
station;
335 card->isr = cycx_x25_irq_handler;
337 card->wandev.update = cycx_wan_update;
347 static int cycx_wan_update(
struct wan_device *wandev)
350 if (!wandev || !wandev->
private)
379 pr_info(
"%s: invalid interface name!\n", card->devname);
384 cycx_x25_chan_setup);
388 chan = netdev_priv(dev);
397 if (conf->
addr[0] ==
'@') {
402 pr_err(
"%s: %s local addr too long!\n",
430 if (lcn >= card->u.x.lo_pvc && lcn <= card->
u.x.hi_pvc)
433 pr_err(
"%s: PVC %u is out of range on interface %s!\n",
439 pr_err(
"%s: invalid media address on interface %s!\n",
469 static const struct header_ops cycx_header_ops = {
470 .create = cycx_netdevice_hard_header,
471 .rebuild = cycx_netdevice_rebuild_header,
475 .ndo_init = cycx_netdevice_init,
476 .ndo_open = cycx_netdevice_open,
477 .ndo_stop = cycx_netdevice_stop,
478 .ndo_start_xmit = cycx_netdevice_hard_start_xmit,
479 .ndo_get_stats = cycx_netdevice_get_stats,
503 struct cycx_device *
card = chan->
card;
532 static int cycx_netdevice_open(
struct net_device *dev)
534 if (netif_running(dev))
537 netif_start_queue(dev);
544 static int cycx_netdevice_stop(
struct net_device *dev)
548 netif_stop_queue(dev);
551 cycx_x25_chan_disconnect(dev);
564 static int cycx_netdevice_hard_header(
struct sk_buff *
skb,
577 static int cycx_netdevice_rebuild_header(
struct sk_buff *
skb)
601 struct cycx_device *card = chan->
card;
610 pr_info(
"%s: unsupported Ethertype 0x%04X on interface %s!\n",
614 switch (chan->
state) {
616 if (cycx_x25_chan_connect(dev)) {
617 netif_stop_queue(dev);
624 netif_stop_queue(dev);
632 ++card->wandev.stats.tx_dropped;
635 switch (skb->
data[0]) {
639 cycx_x25_chan_connect(dev);
642 cycx_x25_chan_disconnect(dev);
645 pr_info(
"%s: unknown %d x25-iface request on %s!\n",
646 card->devname, skb->
data[0], dev->
name);
654 netif_stop_queue(dev);
680 static void cycx_x25_irq_handler(
struct cycx_device *card)
686 card->buff_int_mode_unbusy = 0;
689 switch (
cmd.command) {
742 dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
744 card->buff_int_mode_unbusy = 1;
745 netif_wake_queue(dev);
747 pr_err(
"%s:ackvc for inexistent lcn %d\n", card->devname, lcn);
774 cycx_peek(&card->hw, cmd->
buf + 4, &bitm,
sizeof(bitm));
777 dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
780 pr_info(
"%s: receiving on orphaned LCN %d!\n",
785 chan = netdev_priv(dev);
802 pr_info(
"%s: no socket buffers available!\n",
818 if (skb_tailroom(skb) < pktlen) {
826 pr_info(
"%s: unexpectedly long packet sequence on interface %s!\n",
827 card->devname, dev->
name);
828 ++chan->
ifstats.rx_length_errors;
843 skb_reset_mac_header(skb);
857 u8 lcn, sizeloc, sizerem;
860 cycx_peek(&card->hw, cmd->
buf + 5, &sizeloc,
sizeof(sizeloc));
863 sizerem = sizeloc >> 4;
866 loc[0] = rem[0] =
'\0';
869 nibble_to_byte(d, loc, sizeloc, 0);
872 nibble_to_byte(d + (sizeloc >> 1), rem, sizerem, sizeloc & 1);
875 __func__, lcn, loc, rem);
877 dev = cycx_x25_get_dev_by_dte_addr(wandev, rem);
880 pr_info(
"%s: connect not expected: remote %s!\n",
885 chan = netdev_priv(dev);
903 card->devname, __func__, lcn, key);
905 dev = cycx_x25_get_dev_by_lcn(wandev, -key);
908 clear_bit(--key, (
void*)&card->u.x.connection_keys);
909 pr_info(
"%s: connect confirm not expected: lcn %d, key=%d!\n",
910 card->devname, lcn, key);
914 clear_bit(--key, (
void*)&card->u.x.connection_keys);
915 chan = netdev_priv(dev);
930 card->devname, __func__, lcn);
931 dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
934 pr_info(
"%s:disconnect confirm not expected!:lcn %d\n",
953 dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
966 #if CYCLOMX_X25_DEBUG
971 cycx_peek(&card->hw, cmd->
buf, &msg_code,
sizeof(msg_code));
972 cycx_peek(&card->hw, cmd->
buf + 2, &link,
sizeof(link));
973 cycx_peek(&card->hw, cmd->
buf + 4, &size,
sizeof(size));
975 toread = size < 20 ? size : 20;
978 cycx_peek(&card->hw, cmd->
buf + 10 + toread + 1, &routine, 1);
980 pr_info(
"cycx_x25_irq_handler: X25_LOG (0x4500) indic.:\n");
982 pr_info(
"Log message code=0x%X\n", msg_code);
984 pr_info(
"log code=0x%X\n", code);
985 pr_info(
"log routine=0x%X\n", routine);
986 pr_info(
"Message size=%d\n", size);
987 hex_dump(
"Message", bf, toread);
996 sizeof(card->u.x.stats));
997 hex_dump(
"cycx_x25_irq_stat", (
unsigned char*)&card->u.x.stats,
998 sizeof(card->u.x.stats));
999 cycx_x25_dump_stats(&card->u.x.stats);
1009 pr_info(
"%s: spurious interrupt (0x%X)!\n",
1012 #ifdef CYCLOMX_X25_DEBUG
1013 static void hex_dump(
char *
msg,
unsigned char *
p,
int len)
1022 static int x25_exec(
struct cycx_device *card,
int command,
int link,
1023 void *
d1,
int len1,
void *
d2,
int len2)
1026 unsigned long flags;
1027 u32 addr = 0x1200 + 0x2E0 * link + 0x1E2;
1033 c.len = len1 + len2;
1048 cycx_poke(&card->hw, addr + len1, d2, 249);
1052 cycx_poke(&card->hw, addr + len1, d2, len2);
1062 }
while (retry-- && err);
1064 spin_unlock_irqrestore(&card->u.x.lock, flags);
1070 static int cycx_x25_configure(
struct cycx_device *card,
1078 memset(&x25_cmd_conf, 0,
sizeof(x25_cmd_conf));
1079 x25_cmd_conf.nlinks = 2;
1080 x25_cmd_conf.conf[0] = *conf;
1086 x25_cmd_conf.conf[1] = *conf;
1087 x25_cmd_conf.conf[1].
link = 1;
1088 x25_cmd_conf.conf[1].speed = 5;
1089 x25_cmd_conf.conf[1].clock = 8;
1090 x25_cmd_conf.conf[1].flags = 0;
1092 cycx_x25_dump_config(&x25_cmd_conf.conf[0]);
1093 cycx_x25_dump_config(&x25_cmd_conf.conf[1]);
1096 &x25_cmd_conf,
sizeof(x25_cmd_conf),
NULL, 0);
1114 card->wandev.stats.rx_packets = card->u.x.stats.n2_rx_frames;
1115 card->wandev.stats.rx_over_errors = card->u.x.stats.rx_over_errors;
1116 card->wandev.stats.rx_crc_errors = card->u.x.stats.rx_crc_errors;
1117 card->wandev.stats.rx_length_errors = 0;
1118 card->wandev.stats.rx_frame_errors = 0;
1119 card->wandev.stats.rx_missed_errors = card->u.x.stats.rx_aborts;
1120 card->wandev.stats.rx_dropped = 0;
1121 card->wandev.stats.rx_errors = 0;
1122 card->wandev.stats.tx_packets = card->u.x.stats.n2_tx_frames;
1123 card->wandev.stats.tx_aborted_errors = card->u.x.stats.tx_aborts;
1124 card->wandev.stats.tx_dropped = 0;
1125 card->wandev.stats.collisions = 0;
1126 card->wandev.stats.tx_errors = 0;
1128 cycx_x25_dump_devs(&card->wandev);
1134 static int byte_to_nibble(
u8 *
s,
u8 *
d,
char *nibble)
1138 if (*nibble && *s) {
1145 d[
i] = (*s -
'0') << 4;
1147 d[i] |= *(s + 1) -
'0';
1159 static void nibble_to_byte(
u8 *s,
u8 *d,
u8 len,
u8 nibble)
1162 *d++ =
'0' + (*s++ & 0x0F);
1167 *d++ =
'0' + (*s >> 4);
1170 *d++ =
'0' + (*s & 0x0F);
1181 static int x25_place_call(
struct cycx_device *card,
1189 remotelen =
strlen(chan->addr);
1192 if (card->u.x.connection_keys == ~0
U) {
1193 pr_info(
"%s: too many simultaneous connection requests!\n",
1198 key =
ffz(card->u.x.connection_keys);
1199 set_bit(key, (
void*)&card->u.x.connection_keys);
1207 len = byte_to_nibble(chan->
addr, d + 6, &nibble);
1210 len += byte_to_nibble(chan->
local_addr, d + 6 + len, &nibble);
1215 d[5] = mylen << 4 | remotelen;
1216 d[6 + len + 1] = 0xCC;
1219 &d, 7 + len + 1,
NULL, 0)) != 0)
1220 clear_bit(--key, (
void*)&card->u.x.connection_keys);
1234 d[0] = d[3] = chan->
lcn;
1257 static int x25_clear_call(
struct cycx_device *card,
u8 link,
u8 lcn,
u8 cause,
1276 u8 d[] =
"?\xFF\x10??";
1293 chan = netdev_priv(dev);
1295 if (chan->
lcn == lcn)
1304 cycx_x25_get_dev_by_dte_addr(
struct wan_device *wandev,
char *dte)
1310 chan = netdev_priv(dev);
1326 static int cycx_x25_chan_connect(
struct net_device *dev)
1329 struct cycx_device *card = chan->
card;
1336 card->devname, chan->
addr);
1338 if (x25_place_call(card, chan))
1351 static void cycx_x25_chan_disconnect(
struct net_device *dev)
1356 x25_clear_call(chan->
card, chan->
link, chan->
lcn, 0, 0);
1369 cycx_x25_chan_disconnect(dev);
1371 pr_err(
"%s: %s for svc (%s) not connected!\n",
1372 chan->
card->devname, __func__, dev->
name);
1379 struct cycx_device *card = chan->
card;
1380 unsigned long flags;
1381 char *string_state =
NULL;
1385 if (chan->
state != state) {
1391 string_state =
"connected!";
1393 netif_wake_queue(dev);
1402 string_state =
"connecting...";
1405 string_state =
"disconnecting...";
1408 string_state =
"disconnected!";
1411 *(
unsigned short*)dev->
dev_addr = 0;
1419 netif_wake_queue(dev);
1423 pr_info(
"%s: interface %s %s\n",
1424 card->devname, dev->
name, string_state);
1428 spin_unlock_irqrestore(&card->lock, flags);
1447 struct cycx_device *card = chan->
card;
1449 unsigned len = skb->
len;
1451 if (skb->
len > card->wandev.mtu) {
1452 len = card->wandev.mtu;
1465 chan->
ifstats.tx_bytes += len;
1477 if ((skb = dev_alloc_skb(1)) ==
NULL) {
1478 pr_err(
"%s: out of memory\n", __func__);
1485 skb->
protocol = x25_type_trans(skb, dev);
1490 static u8 bps_to_speed_code(
u32 bps)
1494 if (bps >= 512000) number = 8;
1495 else if (bps >= 256000) number = 7;
1496 else if (bps >= 64000) number = 6;
1497 else if (bps >= 38400) number = 5;
1498 else if (bps >= 19200) number = 4;
1499 else if (bps >= 9600) number = 3;
1500 else if (bps >= 4800) number = 2;
1501 else if (bps >= 2400) number = 1;
1507 static u8 cycx_log2(
u32 n)
1524 static unsigned dec_to_uint(
u8 *
str,
int len)
1532 val = (val * 10) + (*str - (unsigned)
'0');
1544 #ifdef CYCLOMX_X25_DEBUG
1547 pr_info(
"X.25 configuration\n");
1548 pr_info(
"-----------------\n");
1551 pr_info(
"clock=%sternal\n", conf->
clock == 8 ?
"Ex" :
"In");
1552 pr_info(
"# level 2 retransm.=%d\n", conf->
n2);
1555 pr_info(
"# logical channels=%d\n", conf->
nvc);
1583 static void cycx_x25_dump_devs(
struct wan_device *wandev)
1588 pr_info(
"name: addr: txoff: protocol:\n");
1589 pr_info(
"---------------------------------------\n");
1594 pr_info(
"%-5.5s %-15.15s %d ETH_P_%s\n",
1595 chan->
name, chan->
addr, netif_queue_stopped(dev),