include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / net / wan / cycx_x25.c
blob06f3f6309e4b30056bcb566ef767c2c1c581c5a9
1 /*
2 * cycx_x25.c Cyclom 2X WAN Link Driver. X.25 module.
4 * Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6 * Copyright: (c) 1998-2003 Arnaldo Carvalho de Melo
8 * Based on sdla_x25.c by Gene Kozin <genek@compuserve.com>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 * ============================================================================
15 * 2001/01/12 acme use dev_kfree_skb_irq on interrupt context
16 * 2000/04/02 acme dprintk, cycx_debug
17 * fixed the bug introduced in get_dev_by_lcn and
18 * get_dev_by_dte_addr by the anonymous hacker
19 * that converted this driver to softnet
20 * 2000/01/08 acme cleanup
21 * 1999/10/27 acme use ARPHRD_HWX25 so that the X.25 stack know
22 * that we have a X.25 stack implemented in
23 * firmware onboard
24 * 1999/10/18 acme support for X.25 sockets in if_send,
25 * beware: socket(AF_X25...) IS WORK IN PROGRESS,
26 * TCP/IP over X.25 via wanrouter not affected,
27 * working.
28 * 1999/10/09 acme chan_disc renamed to chan_disconnect,
29 * began adding support for X.25 sockets:
30 * conf->protocol in new_if
31 * 1999/10/05 acme fixed return E... to return -E...
32 * 1999/08/10 acme serialized access to the card thru a spinlock
33 * in x25_exec
34 * 1999/08/09 acme removed per channel spinlocks
35 * removed references to enable_tx_int
36 * 1999/05/28 acme fixed nibble_to_byte, ackvc now properly treated
37 * if_send simplified
38 * 1999/05/25 acme fixed t1, t2, t21 & t23 configuration
39 * use spinlocks instead of cli/sti in some points
40 * 1999/05/24 acme finished the x25_get_stat function
41 * 1999/05/23 acme dev->type = ARPHRD_X25 (tcpdump only works,
42 * AFAIT, with ARPHRD_ETHER). This seems to be
43 * needed to use socket(AF_X25)...
44 * Now the config file must specify a peer media
45 * address for svc channels over a crossover cable.
46 * Removed hold_timeout from x25_channel_t,
47 * not used.
48 * A little enhancement in the DEBUG processing
49 * 1999/05/22 acme go to DISCONNECTED in disconnect_confirm_intr,
50 * instead of chan_disc.
51 * 1999/05/16 marcelo fixed timer initialization in SVCs
52 * 1999/01/05 acme x25_configure now get (most of) all
53 * parameters...
54 * 1999/01/05 acme pktlen now (correctly) uses log2 (value
55 * configured)
56 * 1999/01/03 acme judicious use of data types (u8, u16, u32, etc)
57 * 1999/01/03 acme cyx_isr: reset dpmbase to acknowledge
58 * indication (interrupt from cyclom 2x)
59 * 1999/01/02 acme cyx_isr: first hackings...
60 * 1999/01/0203 acme when initializing an array don't give less
61 * elements than declared...
62 * example: char send_cmd[6] = "?\xFF\x10";
63 * you'll gonna lose a couple hours, 'cause your
64 * brain won't admit that there's an error in the
65 * above declaration... the side effect is that
66 * memset is put into the unresolved symbols
67 * instead of using the inline memset functions...
68 * 1999/01/02 acme began chan_connect, chan_send, x25_send
69 * 1998/12/31 acme x25_configure
70 * this code can be compiled as non module
71 * 1998/12/27 acme code cleanup
72 * IPX code wiped out! let's decrease code
73 * complexity for now, remember: I'm learning! :)
74 * bps_to_speed_code OK
75 * 1998/12/26 acme Minimal debug code cleanup
76 * 1998/08/08 acme Initial version.
79 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
81 #define CYCLOMX_X25_DEBUG 1
83 #include <linux/ctype.h> /* isdigit() */
84 #include <linux/errno.h> /* return codes */
85 #include <linux/if_arp.h> /* ARPHRD_HWX25 */
86 #include <linux/kernel.h> /* printk(), and other useful stuff */
87 #include <linux/module.h>
88 #include <linux/string.h> /* inline memset(), etc. */
89 #include <linux/sched.h>
90 #include <linux/slab.h> /* kmalloc(), kfree() */
91 #include <linux/stddef.h> /* offsetof(), etc. */
92 #include <linux/wanrouter.h> /* WAN router definitions */
94 #include <asm/byteorder.h> /* htons(), etc. */
96 #include <linux/cyclomx.h> /* Cyclom 2X common user API definitions */
97 #include <linux/cycx_x25.h> /* X.25 firmware API definitions */
99 #include <net/x25device.h>
101 /* Defines & Macros */
102 #define CYCX_X25_MAX_CMD_RETRY 5
103 #define CYCX_X25_CHAN_MTU 2048 /* unfragmented logical channel MTU */
105 /* Data Structures */
106 /* This is an extension of the 'struct net_device' we create for each network
107 interface to keep the rest of X.25 channel-specific data. */
108 struct cycx_x25_channel {
109 /* This member must be first. */
110 struct net_device *slave; /* WAN slave */
112 char name[WAN_IFNAME_SZ+1]; /* interface name, ASCIIZ */
113 char addr[WAN_ADDRESS_SZ+1]; /* media address, ASCIIZ */
114 char *local_addr; /* local media address, ASCIIZ -
115 svc thru crossover cable */
116 s16 lcn; /* logical channel number/conn.req.key*/
117 u8 link;
118 struct timer_list timer; /* timer used for svc channel disc. */
119 u16 protocol; /* ethertype, 0 - multiplexed */
120 u8 svc; /* 0 - permanent, 1 - switched */
121 u8 state; /* channel state */
122 u8 drop_sequence; /* mark sequence for dropping */
123 u32 idle_tmout; /* sec, before disconnecting */
124 struct sk_buff *rx_skb; /* receive socket buffer */
125 struct cycx_device *card; /* -> owner */
126 struct net_device_stats ifstats;/* interface statistics */
129 /* Function Prototypes */
130 /* WAN link driver entry points. These are called by the WAN router module. */
131 static int cycx_wan_update(struct wan_device *wandev),
132 cycx_wan_new_if(struct wan_device *wandev, struct net_device *dev,
133 wanif_conf_t *conf),
134 cycx_wan_del_if(struct wan_device *wandev, struct net_device *dev);
136 /* Network device interface */
137 static int cycx_netdevice_init(struct net_device *dev);
138 static int cycx_netdevice_open(struct net_device *dev);
139 static int cycx_netdevice_stop(struct net_device *dev);
140 static int cycx_netdevice_hard_header(struct sk_buff *skb,
141 struct net_device *dev, u16 type,
142 const void *daddr, const void *saddr,
143 unsigned len);
144 static int cycx_netdevice_rebuild_header(struct sk_buff *skb);
145 static netdev_tx_t cycx_netdevice_hard_start_xmit(struct sk_buff *skb,
146 struct net_device *dev);
148 static struct net_device_stats *
149 cycx_netdevice_get_stats(struct net_device *dev);
151 /* Interrupt handlers */
152 static void cycx_x25_irq_handler(struct cycx_device *card),
153 cycx_x25_irq_tx(struct cycx_device *card, struct cycx_x25_cmd *cmd),
154 cycx_x25_irq_rx(struct cycx_device *card, struct cycx_x25_cmd *cmd),
155 cycx_x25_irq_log(struct cycx_device *card,
156 struct cycx_x25_cmd *cmd),
157 cycx_x25_irq_stat(struct cycx_device *card,
158 struct cycx_x25_cmd *cmd),
159 cycx_x25_irq_connect_confirm(struct cycx_device *card,
160 struct cycx_x25_cmd *cmd),
161 cycx_x25_irq_disconnect_confirm(struct cycx_device *card,
162 struct cycx_x25_cmd *cmd),
163 cycx_x25_irq_connect(struct cycx_device *card,
164 struct cycx_x25_cmd *cmd),
165 cycx_x25_irq_disconnect(struct cycx_device *card,
166 struct cycx_x25_cmd *cmd),
167 cycx_x25_irq_spurious(struct cycx_device *card,
168 struct cycx_x25_cmd *cmd);
170 /* X.25 firmware interface functions */
171 static int cycx_x25_configure(struct cycx_device *card,
172 struct cycx_x25_config *conf),
173 cycx_x25_get_stats(struct cycx_device *card),
174 cycx_x25_send(struct cycx_device *card, u8 link, u8 lcn, u8 bitm,
175 int len, void *buf),
176 cycx_x25_connect_response(struct cycx_device *card,
177 struct cycx_x25_channel *chan),
178 cycx_x25_disconnect_response(struct cycx_device *card, u8 link,
179 u8 lcn);
181 /* channel functions */
182 static int cycx_x25_chan_connect(struct net_device *dev),
183 cycx_x25_chan_send(struct net_device *dev, struct sk_buff *skb);
185 static void cycx_x25_chan_disconnect(struct net_device *dev),
186 cycx_x25_chan_send_event(struct net_device *dev, u8 event);
188 /* Miscellaneous functions */
189 static void cycx_x25_set_chan_state(struct net_device *dev, u8 state),
190 cycx_x25_chan_timer(unsigned long d);
192 static void nibble_to_byte(u8 *s, u8 *d, u8 len, u8 nibble),
193 reset_timer(struct net_device *dev);
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);
200 static struct net_device *cycx_x25_get_dev_by_lcn(struct wan_device *wandev,
201 s16 lcn);
202 static struct net_device *
203 cycx_x25_get_dev_by_dte_addr(struct wan_device *wandev, char *dte);
205 static void cycx_x25_chan_setup(struct net_device *dev);
207 #ifdef CYCLOMX_X25_DEBUG
208 static void hex_dump(char *msg, unsigned char *p, int len);
209 static void cycx_x25_dump_config(struct cycx_x25_config *conf);
210 static void cycx_x25_dump_stats(struct cycx_x25_stats *stats);
211 static void cycx_x25_dump_devs(struct wan_device *wandev);
212 #else
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)
217 #endif
218 /* Public Functions */
220 /* X.25 Protocol Initialization routine.
222 * This routine is called by the main Cyclom 2X module during setup. At this
223 * point adapter is completely initialized and X.25 firmware is running.
224 * o configure adapter
225 * o initialize protocol-specific fields of the adapter data space.
227 * Return: 0 o.k.
228 * < 0 failure. */
229 int cycx_x25_wan_init(struct cycx_device *card, wandev_conf_t *conf)
231 struct cycx_x25_config cfg;
233 /* Verify configuration ID */
234 if (conf->config_id != WANCONFIG_X25) {
235 pr_info("%s: invalid configuration ID %u!\n",
236 card->devname, conf->config_id);
237 return -EINVAL;
240 /* Initialize protocol-specific fields */
241 card->mbox = card->hw.dpmbase + X25_MBOX_OFFS;
242 card->u.x.connection_keys = 0;
243 spin_lock_init(&card->u.x.lock);
245 /* Configure adapter. Here we set reasonable defaults, then parse
246 * device configuration structure and set configuration options.
247 * Most configuration options are verified and corrected (if
248 * necessary) since we can't rely on the adapter to do so and don't
249 * want it to fail either. */
250 memset(&cfg, 0, sizeof(cfg));
251 cfg.link = 0;
252 cfg.clock = conf->clocking == WANOPT_EXTERNAL ? 8 : 55;
253 cfg.speed = bps_to_speed_code(conf->bps);
254 cfg.n3win = 7;
255 cfg.n2win = 2;
256 cfg.n2 = 5;
257 cfg.nvc = 1;
258 cfg.npvc = 1;
259 cfg.flags = 0x02; /* default = V35 */
260 cfg.t1 = 10; /* line carrier timeout */
261 cfg.t2 = 29; /* tx timeout */
262 cfg.t21 = 180; /* CALL timeout */
263 cfg.t23 = 180; /* CLEAR timeout */
265 /* adjust MTU */
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;
272 else
273 card->wandev.mtu = 64;
275 cfg.pktlen = cycx_log2(card->wandev.mtu);
277 if (conf->station == WANOPT_DTE) {
278 cfg.locaddr = 3; /* DTE */
279 cfg.remaddr = 1; /* DCE */
280 } else {
281 cfg.locaddr = 1; /* DCE */
282 cfg.remaddr = 3; /* DTE */
285 if (conf->interface == WANOPT_RS232)
286 cfg.flags = 0; /* FIXME just reset the 2nd bit */
288 if (conf->u.x25.hi_pvc) {
289 card->u.x.hi_pvc = min_t(unsigned int, conf->u.x25.hi_pvc, 4095);
290 card->u.x.lo_pvc = min_t(unsigned int, conf->u.x25.lo_pvc, card->u.x.hi_pvc);
293 if (conf->u.x25.hi_svc) {
294 card->u.x.hi_svc = min_t(unsigned int, conf->u.x25.hi_svc, 4095);
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)
299 cfg.npvc = 0;
300 else
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;
305 if (conf->u.x25.hdlc_window)
306 cfg.n2win = min_t(unsigned int, conf->u.x25.hdlc_window, 7);
308 if (conf->u.x25.pkt_window)
309 cfg.n3win = min_t(unsigned int, conf->u.x25.pkt_window, 7);
311 if (conf->u.x25.t1)
312 cfg.t1 = min_t(unsigned int, conf->u.x25.t1, 30);
314 if (conf->u.x25.t2)
315 cfg.t2 = min_t(unsigned int, conf->u.x25.t2, 30);
317 if (conf->u.x25.t11_t21)
318 cfg.t21 = min_t(unsigned int, conf->u.x25.t11_t21, 30);
320 if (conf->u.x25.t13_t23)
321 cfg.t23 = min_t(unsigned int, conf->u.x25.t13_t23, 30);
323 if (conf->u.x25.n2)
324 cfg.n2 = min_t(unsigned int, conf->u.x25.n2, 30);
326 /* initialize adapter */
327 if (cycx_x25_configure(card, &cfg))
328 return -EIO;
330 /* Initialize protocol-specific fields of adapter data space */
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;
336 card->exec = NULL;
337 card->wandev.update = cycx_wan_update;
338 card->wandev.new_if = cycx_wan_new_if;
339 card->wandev.del_if = cycx_wan_del_if;
340 card->wandev.state = WAN_DISCONNECTED;
342 return 0;
345 /* WAN Device Driver Entry Points */
346 /* Update device status & statistics. */
347 static int cycx_wan_update(struct wan_device *wandev)
349 /* sanity checks */
350 if (!wandev || !wandev->private)
351 return -EFAULT;
353 if (wandev->state == WAN_UNCONFIGURED)
354 return -ENODEV;
356 cycx_x25_get_stats(wandev->private);
358 return 0;
361 /* Create new logical channel.
362 * This routine is called by the router when ROUTER_IFNEW IOCTL is being
363 * handled.
364 * o parse media- and hardware-specific configuration
365 * o make sure that a new channel can be created
366 * o allocate resources, if necessary
367 * o prepare network device structure for registration.
369 * Return: 0 o.k.
370 * < 0 failure (channel will not be created) */
371 static int cycx_wan_new_if(struct wan_device *wandev, struct net_device *dev,
372 wanif_conf_t *conf)
374 struct cycx_device *card = wandev->private;
375 struct cycx_x25_channel *chan;
376 int err = 0;
378 if (!conf->name[0] || strlen(conf->name) > WAN_IFNAME_SZ) {
379 pr_info("%s: invalid interface name!\n", card->devname);
380 return -EINVAL;
383 dev = alloc_netdev(sizeof(struct cycx_x25_channel), conf->name,
384 cycx_x25_chan_setup);
385 if (!dev)
386 return -ENOMEM;
388 chan = netdev_priv(dev);
389 strcpy(chan->name, conf->name);
390 chan->card = card;
391 chan->link = conf->port;
392 chan->protocol = conf->protocol ? ETH_P_X25 : ETH_P_IP;
393 chan->rx_skb = NULL;
394 /* only used in svc connected thru crossover cable */
395 chan->local_addr = NULL;
397 if (conf->addr[0] == '@') { /* SVC */
398 int len = strlen(conf->local_addr);
400 if (len) {
401 if (len > WAN_ADDRESS_SZ) {
402 pr_err("%s: %s local addr too long!\n",
403 wandev->name, chan->name);
404 err = -EINVAL;
405 goto error;
406 } else {
407 chan->local_addr = kmalloc(len + 1, GFP_KERNEL);
409 if (!chan->local_addr) {
410 err = -ENOMEM;
411 goto error;
415 strncpy(chan->local_addr, conf->local_addr,
416 WAN_ADDRESS_SZ);
419 chan->svc = 1;
420 strncpy(chan->addr, &conf->addr[1], WAN_ADDRESS_SZ);
421 init_timer(&chan->timer);
422 chan->timer.function = cycx_x25_chan_timer;
423 chan->timer.data = (unsigned long)dev;
425 /* Set channel timeouts (default if not specified) */
426 chan->idle_tmout = conf->idle_timeout ? conf->idle_timeout : 90;
427 } else if (isdigit(conf->addr[0])) { /* PVC */
428 s16 lcn = dec_to_uint(conf->addr, 0);
430 if (lcn >= card->u.x.lo_pvc && lcn <= card->u.x.hi_pvc)
431 chan->lcn = lcn;
432 else {
433 pr_err("%s: PVC %u is out of range on interface %s!\n",
434 wandev->name, lcn, chan->name);
435 err = -EINVAL;
436 goto error;
438 } else {
439 pr_err("%s: invalid media address on interface %s!\n",
440 wandev->name, chan->name);
441 err = -EINVAL;
442 goto error;
445 return 0;
447 error:
448 free_netdev(dev);
449 return err;
452 /* Delete logical channel. */
453 static int cycx_wan_del_if(struct wan_device *wandev, struct net_device *dev)
455 struct cycx_x25_channel *chan = netdev_priv(dev);
457 if (chan->svc) {
458 kfree(chan->local_addr);
459 if (chan->state == WAN_CONNECTED)
460 del_timer(&chan->timer);
463 return 0;
467 /* Network Device Interface */
469 static const struct header_ops cycx_header_ops = {
470 .create = cycx_netdevice_hard_header,
471 .rebuild = cycx_netdevice_rebuild_header,
474 static const struct net_device_ops cycx_netdev_ops = {
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,
482 static void cycx_x25_chan_setup(struct net_device *dev)
484 /* Initialize device driver entry points */
485 dev->netdev_ops = &cycx_netdev_ops;
486 dev->header_ops = &cycx_header_ops;
488 /* Initialize media-specific parameters */
489 dev->mtu = CYCX_X25_CHAN_MTU;
490 dev->type = ARPHRD_HWX25; /* ARP h/w type */
491 dev->hard_header_len = 0; /* media header length */
492 dev->addr_len = 0; /* hardware address length */
495 /* Initialize Linux network interface.
497 * This routine is called only once for each interface, during Linux network
498 * interface registration. Returning anything but zero will fail interface
499 * registration. */
500 static int cycx_netdevice_init(struct net_device *dev)
502 struct cycx_x25_channel *chan = netdev_priv(dev);
503 struct cycx_device *card = chan->card;
504 struct wan_device *wandev = &card->wandev;
506 if (!chan->svc)
507 *(__be16*)dev->dev_addr = htons(chan->lcn);
509 /* Initialize hardware parameters (just for reference) */
510 dev->irq = wandev->irq;
511 dev->dma = wandev->dma;
512 dev->base_addr = wandev->ioport;
513 dev->mem_start = (unsigned long)wandev->maddr;
514 dev->mem_end = (unsigned long)(wandev->maddr +
515 wandev->msize - 1);
516 dev->flags |= IFF_NOARP;
518 /* Set transmit buffer queue length */
519 dev->tx_queue_len = 10;
521 /* Initialize socket buffers */
522 cycx_x25_set_chan_state(dev, WAN_DISCONNECTED);
524 return 0;
527 /* Open network interface.
528 * o prevent module from unloading by incrementing use count
529 * o if link is disconnected then initiate connection
531 * Return 0 if O.k. or errno. */
532 static int cycx_netdevice_open(struct net_device *dev)
534 if (netif_running(dev))
535 return -EBUSY; /* only one open is allowed */
537 netif_start_queue(dev);
538 return 0;
541 /* Close network interface.
542 * o reset flags.
543 * o if there's no more open channels then disconnect physical link. */
544 static int cycx_netdevice_stop(struct net_device *dev)
546 struct cycx_x25_channel *chan = netdev_priv(dev);
548 netif_stop_queue(dev);
550 if (chan->state == WAN_CONNECTED || chan->state == WAN_CONNECTING)
551 cycx_x25_chan_disconnect(dev);
553 return 0;
556 /* Build media header.
557 * o encapsulate packet according to encapsulation type.
559 * The trick here is to put packet type (Ethertype) into 'protocol' field of
560 * the socket buffer, so that we don't forget it. If encapsulation fails,
561 * set skb->protocol to 0 and discard packet later.
563 * Return: media header length. */
564 static int cycx_netdevice_hard_header(struct sk_buff *skb,
565 struct net_device *dev, u16 type,
566 const void *daddr, const void *saddr,
567 unsigned len)
569 skb->protocol = htons(type);
571 return dev->hard_header_len;
574 /* * Re-build media header.
575 * Return: 1 physical address resolved.
576 * 0 physical address not resolved */
577 static int cycx_netdevice_rebuild_header(struct sk_buff *skb)
579 return 1;
582 /* Send a packet on a network interface.
583 * o set busy flag (marks start of the transmission).
584 * o check link state. If link is not up, then drop the packet.
585 * o check channel status. If it's down then initiate a call.
586 * o pass a packet to corresponding WAN device.
587 * o free socket buffer
589 * Return: 0 complete (socket buffer must be freed)
590 * non-0 packet may be re-transmitted (tbusy must be set)
592 * Notes:
593 * 1. This routine is called either by the protocol stack or by the "net
594 * bottom half" (with interrupts enabled).
595 * 2. Setting tbusy flag will inhibit further transmit requests from the
596 * protocol stack and can be used for flow control with protocol layer. */
597 static netdev_tx_t cycx_netdevice_hard_start_xmit(struct sk_buff *skb,
598 struct net_device *dev)
600 struct cycx_x25_channel *chan = netdev_priv(dev);
601 struct cycx_device *card = chan->card;
603 if (!chan->svc)
604 chan->protocol = ntohs(skb->protocol);
606 if (card->wandev.state != WAN_CONNECTED)
607 ++chan->ifstats.tx_dropped;
608 else if (chan->svc && chan->protocol &&
609 chan->protocol != ntohs(skb->protocol)) {
610 pr_info("%s: unsupported Ethertype 0x%04X on interface %s!\n",
611 card->devname, ntohs(skb->protocol), dev->name);
612 ++chan->ifstats.tx_errors;
613 } else if (chan->protocol == ETH_P_IP) {
614 switch (chan->state) {
615 case WAN_DISCONNECTED:
616 if (cycx_x25_chan_connect(dev)) {
617 netif_stop_queue(dev);
618 return NETDEV_TX_BUSY;
620 /* fall thru */
621 case WAN_CONNECTED:
622 reset_timer(dev);
623 dev->trans_start = jiffies;
624 netif_stop_queue(dev);
626 if (cycx_x25_chan_send(dev, skb))
627 return NETDEV_TX_BUSY;
629 break;
630 default:
631 ++chan->ifstats.tx_dropped;
632 ++card->wandev.stats.tx_dropped;
634 } else { /* chan->protocol == ETH_P_X25 */
635 switch (skb->data[0]) {
636 case X25_IFACE_DATA:
637 break;
638 case X25_IFACE_CONNECT:
639 cycx_x25_chan_connect(dev);
640 goto free_packet;
641 case X25_IFACE_DISCONNECT:
642 cycx_x25_chan_disconnect(dev);
643 goto free_packet;
644 default:
645 pr_info("%s: unknown %d x25-iface request on %s!\n",
646 card->devname, skb->data[0], dev->name);
647 ++chan->ifstats.tx_errors;
648 goto free_packet;
651 skb_pull(skb, 1); /* Remove control byte */
652 reset_timer(dev);
653 dev->trans_start = jiffies;
654 netif_stop_queue(dev);
656 if (cycx_x25_chan_send(dev, skb)) {
657 /* prepare for future retransmissions */
658 skb_push(skb, 1);
659 return NETDEV_TX_BUSY;
663 free_packet:
664 dev_kfree_skb(skb);
666 return NETDEV_TX_OK;
669 /* Get Ethernet-style interface statistics.
670 * Return a pointer to struct net_device_stats */
671 static struct net_device_stats *cycx_netdevice_get_stats(struct net_device *dev)
673 struct cycx_x25_channel *chan = netdev_priv(dev);
675 return chan ? &chan->ifstats : NULL;
678 /* Interrupt Handlers */
679 /* X.25 Interrupt Service Routine. */
680 static void cycx_x25_irq_handler(struct cycx_device *card)
682 struct cycx_x25_cmd cmd;
683 u16 z = 0;
685 card->in_isr = 1;
686 card->buff_int_mode_unbusy = 0;
687 cycx_peek(&card->hw, X25_RXMBOX_OFFS, &cmd, sizeof(cmd));
689 switch (cmd.command) {
690 case X25_DATA_INDICATION:
691 cycx_x25_irq_rx(card, &cmd);
692 break;
693 case X25_ACK_FROM_VC:
694 cycx_x25_irq_tx(card, &cmd);
695 break;
696 case X25_LOG:
697 cycx_x25_irq_log(card, &cmd);
698 break;
699 case X25_STATISTIC:
700 cycx_x25_irq_stat(card, &cmd);
701 break;
702 case X25_CONNECT_CONFIRM:
703 cycx_x25_irq_connect_confirm(card, &cmd);
704 break;
705 case X25_CONNECT_INDICATION:
706 cycx_x25_irq_connect(card, &cmd);
707 break;
708 case X25_DISCONNECT_INDICATION:
709 cycx_x25_irq_disconnect(card, &cmd);
710 break;
711 case X25_DISCONNECT_CONFIRM:
712 cycx_x25_irq_disconnect_confirm(card, &cmd);
713 break;
714 case X25_LINE_ON:
715 cycx_set_state(card, WAN_CONNECTED);
716 break;
717 case X25_LINE_OFF:
718 cycx_set_state(card, WAN_DISCONNECTED);
719 break;
720 default:
721 cycx_x25_irq_spurious(card, &cmd);
722 break;
725 cycx_poke(&card->hw, 0, &z, sizeof(z));
726 cycx_poke(&card->hw, X25_RXMBOX_OFFS, &z, sizeof(z));
727 card->in_isr = 0;
730 /* Transmit interrupt handler.
731 * o Release socket buffer
732 * o Clear 'tbusy' flag */
733 static void cycx_x25_irq_tx(struct cycx_device *card, struct cycx_x25_cmd *cmd)
735 struct net_device *dev;
736 struct wan_device *wandev = &card->wandev;
737 u8 lcn;
739 cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
741 /* unbusy device and then dev_tint(); */
742 dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
743 if (dev) {
744 card->buff_int_mode_unbusy = 1;
745 netif_wake_queue(dev);
746 } else
747 pr_err("%s:ackvc for inexistent lcn %d\n", card->devname, lcn);
750 /* Receive interrupt handler.
751 * This routine handles fragmented IP packets using M-bit according to the
752 * RFC1356.
753 * o map logical channel number to network interface.
754 * o allocate socket buffer or append received packet to the existing one.
755 * o if M-bit is reset (i.e. it's the last packet in a sequence) then
756 * decapsulate packet and pass socket buffer to the protocol stack.
758 * Notes:
759 * 1. When allocating a socket buffer, if M-bit is set then more data is
760 * coming and we have to allocate buffer for the maximum IP packet size
761 * expected on this channel.
762 * 2. If something goes wrong and X.25 packet has to be dropped (e.g. no
763 * socket buffers available) the whole packet sequence must be discarded. */
764 static void cycx_x25_irq_rx(struct cycx_device *card, struct cycx_x25_cmd *cmd)
766 struct wan_device *wandev = &card->wandev;
767 struct net_device *dev;
768 struct cycx_x25_channel *chan;
769 struct sk_buff *skb;
770 u8 bitm, lcn;
771 int pktlen = cmd->len - 5;
773 cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
774 cycx_peek(&card->hw, cmd->buf + 4, &bitm, sizeof(bitm));
775 bitm &= 0x10;
777 dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
778 if (!dev) {
779 /* Invalid channel, discard packet */
780 pr_info("%s: receiving on orphaned LCN %d!\n",
781 card->devname, lcn);
782 return;
785 chan = netdev_priv(dev);
786 reset_timer(dev);
788 if (chan->drop_sequence) {
789 if (!bitm)
790 chan->drop_sequence = 0;
791 else
792 return;
795 if ((skb = chan->rx_skb) == NULL) {
796 /* Allocate new socket buffer */
797 int bufsize = bitm ? dev->mtu : pktlen;
799 if ((skb = dev_alloc_skb((chan->protocol == ETH_P_X25 ? 1 : 0) +
800 bufsize +
801 dev->hard_header_len)) == NULL) {
802 pr_info("%s: no socket buffers available!\n",
803 card->devname);
804 chan->drop_sequence = 1;
805 ++chan->ifstats.rx_dropped;
806 return;
809 if (chan->protocol == ETH_P_X25) /* X.25 socket layer control */
810 /* 0 = data packet (dev_alloc_skb zeroed skb->data) */
811 skb_put(skb, 1);
813 skb->dev = dev;
814 skb->protocol = htons(chan->protocol);
815 chan->rx_skb = skb;
818 if (skb_tailroom(skb) < pktlen) {
819 /* No room for the packet. Call off the whole thing! */
820 dev_kfree_skb_irq(skb);
821 chan->rx_skb = NULL;
823 if (bitm)
824 chan->drop_sequence = 1;
826 pr_info("%s: unexpectedly long packet sequence on interface %s!\n",
827 card->devname, dev->name);
828 ++chan->ifstats.rx_length_errors;
829 return;
832 /* Append packet to the socket buffer */
833 cycx_peek(&card->hw, cmd->buf + 5, skb_put(skb, pktlen), pktlen);
835 if (bitm)
836 return; /* more data is coming */
838 chan->rx_skb = NULL; /* dequeue packet */
840 ++chan->ifstats.rx_packets;
841 chan->ifstats.rx_bytes += pktlen;
843 skb_reset_mac_header(skb);
844 netif_rx(skb);
847 /* Connect interrupt handler. */
848 static void cycx_x25_irq_connect(struct cycx_device *card,
849 struct cycx_x25_cmd *cmd)
851 struct wan_device *wandev = &card->wandev;
852 struct net_device *dev = NULL;
853 struct cycx_x25_channel *chan;
854 u8 d[32],
855 loc[24],
856 rem[24];
857 u8 lcn, sizeloc, sizerem;
859 cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
860 cycx_peek(&card->hw, cmd->buf + 5, &sizeloc, sizeof(sizeloc));
861 cycx_peek(&card->hw, cmd->buf + 6, d, cmd->len - 6);
863 sizerem = sizeloc >> 4;
864 sizeloc &= 0x0F;
866 loc[0] = rem[0] = '\0';
868 if (sizeloc)
869 nibble_to_byte(d, loc, sizeloc, 0);
871 if (sizerem)
872 nibble_to_byte(d + (sizeloc >> 1), rem, sizerem, sizeloc & 1);
874 dprintk(1, KERN_INFO "%s:lcn=%d, local=%s, remote=%s\n",
875 __func__, lcn, loc, rem);
877 dev = cycx_x25_get_dev_by_dte_addr(wandev, rem);
878 if (!dev) {
879 /* Invalid channel, discard packet */
880 pr_info("%s: connect not expected: remote %s!\n",
881 card->devname, rem);
882 return;
885 chan = netdev_priv(dev);
886 chan->lcn = lcn;
887 cycx_x25_connect_response(card, chan);
888 cycx_x25_set_chan_state(dev, WAN_CONNECTED);
891 /* Connect confirm interrupt handler. */
892 static void cycx_x25_irq_connect_confirm(struct cycx_device *card,
893 struct cycx_x25_cmd *cmd)
895 struct wan_device *wandev = &card->wandev;
896 struct net_device *dev;
897 struct cycx_x25_channel *chan;
898 u8 lcn, key;
900 cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
901 cycx_peek(&card->hw, cmd->buf + 1, &key, sizeof(key));
902 dprintk(1, KERN_INFO "%s: %s:lcn=%d, key=%d\n",
903 card->devname, __func__, lcn, key);
905 dev = cycx_x25_get_dev_by_lcn(wandev, -key);
906 if (!dev) {
907 /* Invalid channel, discard packet */
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);
911 return;
914 clear_bit(--key, (void*)&card->u.x.connection_keys);
915 chan = netdev_priv(dev);
916 chan->lcn = lcn;
917 cycx_x25_set_chan_state(dev, WAN_CONNECTED);
920 /* Disconnect confirm interrupt handler. */
921 static void cycx_x25_irq_disconnect_confirm(struct cycx_device *card,
922 struct cycx_x25_cmd *cmd)
924 struct wan_device *wandev = &card->wandev;
925 struct net_device *dev;
926 u8 lcn;
928 cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
929 dprintk(1, KERN_INFO "%s: %s:lcn=%d\n",
930 card->devname, __func__, lcn);
931 dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
932 if (!dev) {
933 /* Invalid channel, discard packet */
934 pr_info("%s:disconnect confirm not expected!:lcn %d\n",
935 card->devname, lcn);
936 return;
939 cycx_x25_set_chan_state(dev, WAN_DISCONNECTED);
942 /* disconnect interrupt handler. */
943 static void cycx_x25_irq_disconnect(struct cycx_device *card,
944 struct cycx_x25_cmd *cmd)
946 struct wan_device *wandev = &card->wandev;
947 struct net_device *dev;
948 u8 lcn;
950 cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
951 dprintk(1, KERN_INFO "%s:lcn=%d\n", __func__, lcn);
953 dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
954 if (dev) {
955 struct cycx_x25_channel *chan = netdev_priv(dev);
957 cycx_x25_disconnect_response(card, chan->link, lcn);
958 cycx_x25_set_chan_state(dev, WAN_DISCONNECTED);
959 } else
960 cycx_x25_disconnect_response(card, 0, lcn);
963 /* LOG interrupt handler. */
964 static void cycx_x25_irq_log(struct cycx_device *card, struct cycx_x25_cmd *cmd)
966 #if CYCLOMX_X25_DEBUG
967 char bf[20];
968 u16 size, toread, link, msg_code;
969 u8 code, routine;
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));
974 /* at most 20 bytes are available... thanks to Daniela :) */
975 toread = size < 20 ? size : 20;
976 cycx_peek(&card->hw, cmd->buf + 10, &bf, toread);
977 cycx_peek(&card->hw, cmd->buf + 10 + toread, &code, 1);
978 cycx_peek(&card->hw, cmd->buf + 10 + toread + 1, &routine, 1);
980 pr_info("cycx_x25_irq_handler: X25_LOG (0x4500) indic.:\n");
981 pr_info("cmd->buf=0x%X\n", cmd->buf);
982 pr_info("Log message code=0x%X\n", msg_code);
983 pr_info("Link=%d\n", link);
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);
988 #endif
991 /* STATISTIC interrupt handler. */
992 static void cycx_x25_irq_stat(struct cycx_device *card,
993 struct cycx_x25_cmd *cmd)
995 cycx_peek(&card->hw, cmd->buf, &card->u.x.stats,
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);
1000 wake_up_interruptible(&card->wait_stats);
1003 /* Spurious interrupt handler.
1004 * o print a warning
1005 * If number of spurious interrupts exceeded some limit, then ??? */
1006 static void cycx_x25_irq_spurious(struct cycx_device *card,
1007 struct cycx_x25_cmd *cmd)
1009 pr_info("%s: spurious interrupt (0x%X)!\n",
1010 card->devname, cmd->command);
1012 #ifdef CYCLOMX_X25_DEBUG
1013 static void hex_dump(char *msg, unsigned char *p, int len)
1015 print_hex_dump(KERN_INFO, msg, DUMP_PREFIX_OFFSET, 16, 1,
1016 p, len, true);
1018 #endif
1020 /* Cyclom 2X Firmware-Specific Functions */
1021 /* Exec X.25 command. */
1022 static int x25_exec(struct cycx_device *card, int command, int link,
1023 void *d1, int len1, void *d2, int len2)
1025 struct cycx_x25_cmd c;
1026 unsigned long flags;
1027 u32 addr = 0x1200 + 0x2E0 * link + 0x1E2;
1028 u8 retry = CYCX_X25_MAX_CMD_RETRY;
1029 int err = 0;
1031 c.command = command;
1032 c.link = link;
1033 c.len = len1 + len2;
1035 spin_lock_irqsave(&card->u.x.lock, flags);
1037 /* write command */
1038 cycx_poke(&card->hw, X25_MBOX_OFFS, &c, sizeof(c) - sizeof(c.buf));
1040 /* write X.25 data */
1041 if (d1) {
1042 cycx_poke(&card->hw, addr, d1, len1);
1044 if (d2) {
1045 if (len2 > 254) {
1046 u32 addr1 = 0xA00 + 0x400 * link;
1048 cycx_poke(&card->hw, addr + len1, d2, 249);
1049 cycx_poke(&card->hw, addr1, ((u8*)d2) + 249,
1050 len2 - 249);
1051 } else
1052 cycx_poke(&card->hw, addr + len1, d2, len2);
1056 /* generate interruption, executing command */
1057 cycx_intr(&card->hw);
1059 /* wait till card->mbox == 0 */
1060 do {
1061 err = cycx_exec(card->mbox);
1062 } while (retry-- && err);
1064 spin_unlock_irqrestore(&card->u.x.lock, flags);
1066 return err;
1069 /* Configure adapter. */
1070 static int cycx_x25_configure(struct cycx_device *card,
1071 struct cycx_x25_config *conf)
1073 struct {
1074 u16 nlinks;
1075 struct cycx_x25_config conf[2];
1076 } x25_cmd_conf;
1078 memset(&x25_cmd_conf, 0, sizeof(x25_cmd_conf));
1079 x25_cmd_conf.nlinks = 2;
1080 x25_cmd_conf.conf[0] = *conf;
1081 /* FIXME: we need to find a way in the wanrouter framework
1082 to configure the second link, for now lets use it
1083 with the same config from the first link, fixing
1084 the interface type to RS232, the speed in 38400 and
1085 the clock to external */
1086 x25_cmd_conf.conf[1] = *conf;
1087 x25_cmd_conf.conf[1].link = 1;
1088 x25_cmd_conf.conf[1].speed = 5; /* 38400 */
1089 x25_cmd_conf.conf[1].clock = 8;
1090 x25_cmd_conf.conf[1].flags = 0; /* default = RS232 */
1092 cycx_x25_dump_config(&x25_cmd_conf.conf[0]);
1093 cycx_x25_dump_config(&x25_cmd_conf.conf[1]);
1095 return x25_exec(card, X25_CONFIG, 0,
1096 &x25_cmd_conf, sizeof(x25_cmd_conf), NULL, 0);
1099 /* Get protocol statistics. */
1100 static int cycx_x25_get_stats(struct cycx_device *card)
1102 /* the firmware expects 20 in the size field!!!
1103 thanks to Daniela */
1104 int err = x25_exec(card, X25_STATISTIC, 0, NULL, 20, NULL, 0);
1106 if (err)
1107 return err;
1109 interruptible_sleep_on(&card->wait_stats);
1111 if (signal_pending(current))
1112 return -EINTR;
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; /* not available from fw */
1118 card->wandev.stats.rx_frame_errors = 0; /* not available from fw */
1119 card->wandev.stats.rx_missed_errors = card->u.x.stats.rx_aborts;
1120 card->wandev.stats.rx_dropped = 0; /* not available from fw */
1121 card->wandev.stats.rx_errors = 0; /* not available from fw */
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; /* not available from fw */
1125 card->wandev.stats.collisions = 0; /* not available from fw */
1126 card->wandev.stats.tx_errors = 0; /* not available from fw */
1128 cycx_x25_dump_devs(&card->wandev);
1130 return 0;
1133 /* return the number of nibbles */
1134 static int byte_to_nibble(u8 *s, u8 *d, char *nibble)
1136 int i = 0;
1138 if (*nibble && *s) {
1139 d[i] |= *s++ - '0';
1140 *nibble = 0;
1141 ++i;
1144 while (*s) {
1145 d[i] = (*s - '0') << 4;
1146 if (*(s + 1))
1147 d[i] |= *(s + 1) - '0';
1148 else {
1149 *nibble = 1;
1150 break;
1152 ++i;
1153 s += 2;
1156 return i;
1159 static void nibble_to_byte(u8 *s, u8 *d, u8 len, u8 nibble)
1161 if (nibble) {
1162 *d++ = '0' + (*s++ & 0x0F);
1163 --len;
1166 while (len) {
1167 *d++ = '0' + (*s >> 4);
1169 if (--len) {
1170 *d++ = '0' + (*s & 0x0F);
1171 --len;
1172 } else break;
1174 ++s;
1177 *d = '\0';
1180 /* Place X.25 call. */
1181 static int x25_place_call(struct cycx_device *card,
1182 struct cycx_x25_channel *chan)
1184 int err = 0,
1185 len;
1186 char d[64],
1187 nibble = 0,
1188 mylen = chan->local_addr ? strlen(chan->local_addr) : 0,
1189 remotelen = strlen(chan->addr);
1190 u8 key;
1192 if (card->u.x.connection_keys == ~0U) {
1193 pr_info("%s: too many simultaneous connection requests!\n",
1194 card->devname);
1195 return -EAGAIN;
1198 key = ffz(card->u.x.connection_keys);
1199 set_bit(key, (void*)&card->u.x.connection_keys);
1200 ++key;
1201 dprintk(1, KERN_INFO "%s:x25_place_call:key=%d\n", card->devname, key);
1202 memset(d, 0, sizeof(d));
1203 d[1] = key; /* user key */
1204 d[2] = 0x10;
1205 d[4] = 0x0B;
1207 len = byte_to_nibble(chan->addr, d + 6, &nibble);
1209 if (chan->local_addr)
1210 len += byte_to_nibble(chan->local_addr, d + 6 + len, &nibble);
1212 if (nibble)
1213 ++len;
1215 d[5] = mylen << 4 | remotelen;
1216 d[6 + len + 1] = 0xCC; /* TCP/IP over X.25, thanks to Daniela :) */
1218 if ((err = x25_exec(card, X25_CONNECT_REQUEST, chan->link,
1219 &d, 7 + len + 1, NULL, 0)) != 0)
1220 clear_bit(--key, (void*)&card->u.x.connection_keys);
1221 else
1222 chan->lcn = -key;
1224 return err;
1227 /* Place X.25 CONNECT RESPONSE. */
1228 static int cycx_x25_connect_response(struct cycx_device *card,
1229 struct cycx_x25_channel *chan)
1231 u8 d[8];
1233 memset(d, 0, sizeof(d));
1234 d[0] = d[3] = chan->lcn;
1235 d[2] = 0x10;
1236 d[4] = 0x0F;
1237 d[7] = 0xCC; /* TCP/IP over X.25, thanks Daniela */
1239 return x25_exec(card, X25_CONNECT_RESPONSE, chan->link, &d, 8, NULL, 0);
1242 /* Place X.25 DISCONNECT RESPONSE. */
1243 static int cycx_x25_disconnect_response(struct cycx_device *card, u8 link,
1244 u8 lcn)
1246 char d[5];
1248 memset(d, 0, sizeof(d));
1249 d[0] = d[3] = lcn;
1250 d[2] = 0x10;
1251 d[4] = 0x17;
1253 return x25_exec(card, X25_DISCONNECT_RESPONSE, link, &d, 5, NULL, 0);
1256 /* Clear X.25 call. */
1257 static int x25_clear_call(struct cycx_device *card, u8 link, u8 lcn, u8 cause,
1258 u8 diagn)
1260 u8 d[7];
1262 memset(d, 0, sizeof(d));
1263 d[0] = d[3] = lcn;
1264 d[2] = 0x10;
1265 d[4] = 0x13;
1266 d[5] = cause;
1267 d[6] = diagn;
1269 return x25_exec(card, X25_DISCONNECT_REQUEST, link, d, 7, NULL, 0);
1272 /* Send X.25 data packet. */
1273 static int cycx_x25_send(struct cycx_device *card, u8 link, u8 lcn, u8 bitm,
1274 int len, void *buf)
1276 u8 d[] = "?\xFF\x10??";
1278 d[0] = d[3] = lcn;
1279 d[4] = bitm;
1281 return x25_exec(card, X25_DATA_REQUEST, link, &d, 5, buf, len);
1284 /* Miscellaneous */
1285 /* Find network device by its channel number. */
1286 static struct net_device *cycx_x25_get_dev_by_lcn(struct wan_device *wandev,
1287 s16 lcn)
1289 struct net_device *dev = wandev->dev;
1290 struct cycx_x25_channel *chan;
1292 while (dev) {
1293 chan = netdev_priv(dev);
1295 if (chan->lcn == lcn)
1296 break;
1297 dev = chan->slave;
1299 return dev;
1302 /* Find network device by its remote dte address. */
1303 static struct net_device *
1304 cycx_x25_get_dev_by_dte_addr(struct wan_device *wandev, char *dte)
1306 struct net_device *dev = wandev->dev;
1307 struct cycx_x25_channel *chan;
1309 while (dev) {
1310 chan = netdev_priv(dev);
1312 if (!strcmp(chan->addr, dte))
1313 break;
1314 dev = chan->slave;
1316 return dev;
1319 /* Initiate connection on the logical channel.
1320 * o for PVC we just get channel configuration
1321 * o for SVCs place an X.25 call
1323 * Return: 0 connected
1324 * >0 connection in progress
1325 * <0 failure */
1326 static int cycx_x25_chan_connect(struct net_device *dev)
1328 struct cycx_x25_channel *chan = netdev_priv(dev);
1329 struct cycx_device *card = chan->card;
1331 if (chan->svc) {
1332 if (!chan->addr[0])
1333 return -EINVAL; /* no destination address */
1335 dprintk(1, KERN_INFO "%s: placing X.25 call to %s...\n",
1336 card->devname, chan->addr);
1338 if (x25_place_call(card, chan))
1339 return -EIO;
1341 cycx_x25_set_chan_state(dev, WAN_CONNECTING);
1342 return 1;
1343 } else
1344 cycx_x25_set_chan_state(dev, WAN_CONNECTED);
1346 return 0;
1349 /* Disconnect logical channel.
1350 * o if SVC then clear X.25 call */
1351 static void cycx_x25_chan_disconnect(struct net_device *dev)
1353 struct cycx_x25_channel *chan = netdev_priv(dev);
1355 if (chan->svc) {
1356 x25_clear_call(chan->card, chan->link, chan->lcn, 0, 0);
1357 cycx_x25_set_chan_state(dev, WAN_DISCONNECTING);
1358 } else
1359 cycx_x25_set_chan_state(dev, WAN_DISCONNECTED);
1362 /* Called by kernel timer */
1363 static void cycx_x25_chan_timer(unsigned long d)
1365 struct net_device *dev = (struct net_device *)d;
1366 struct cycx_x25_channel *chan = netdev_priv(dev);
1368 if (chan->state == WAN_CONNECTED)
1369 cycx_x25_chan_disconnect(dev);
1370 else
1371 pr_err("%s: %s for svc (%s) not connected!\n",
1372 chan->card->devname, __func__, dev->name);
1375 /* Set logical channel state. */
1376 static void cycx_x25_set_chan_state(struct net_device *dev, u8 state)
1378 struct cycx_x25_channel *chan = netdev_priv(dev);
1379 struct cycx_device *card = chan->card;
1380 unsigned long flags;
1381 char *string_state = NULL;
1383 spin_lock_irqsave(&card->lock, flags);
1385 if (chan->state != state) {
1386 if (chan->svc && chan->state == WAN_CONNECTED)
1387 del_timer(&chan->timer);
1389 switch (state) {
1390 case WAN_CONNECTED:
1391 string_state = "connected!";
1392 *(__be16*)dev->dev_addr = htons(chan->lcn);
1393 netif_wake_queue(dev);
1394 reset_timer(dev);
1396 if (chan->protocol == ETH_P_X25)
1397 cycx_x25_chan_send_event(dev,
1398 X25_IFACE_CONNECT);
1400 break;
1401 case WAN_CONNECTING:
1402 string_state = "connecting...";
1403 break;
1404 case WAN_DISCONNECTING:
1405 string_state = "disconnecting...";
1406 break;
1407 case WAN_DISCONNECTED:
1408 string_state = "disconnected!";
1410 if (chan->svc) {
1411 *(unsigned short*)dev->dev_addr = 0;
1412 chan->lcn = 0;
1415 if (chan->protocol == ETH_P_X25)
1416 cycx_x25_chan_send_event(dev,
1417 X25_IFACE_DISCONNECT);
1419 netif_wake_queue(dev);
1420 break;
1423 pr_info("%s: interface %s %s\n",
1424 card->devname, dev->name, string_state);
1425 chan->state = state;
1428 spin_unlock_irqrestore(&card->lock, flags);
1431 /* Send packet on a logical channel.
1432 * When this function is called, tx_skb field of the channel data space
1433 * points to the transmit socket buffer. When transmission is complete,
1434 * release socket buffer and reset 'tbusy' flag.
1436 * Return: 0 - transmission complete
1437 * 1 - busy
1439 * Notes:
1440 * 1. If packet length is greater than MTU for this channel, we'll fragment
1441 * the packet into 'complete sequence' using M-bit.
1442 * 2. When transmission is complete, an event notification should be issued
1443 * to the router. */
1444 static int cycx_x25_chan_send(struct net_device *dev, struct sk_buff *skb)
1446 struct cycx_x25_channel *chan = netdev_priv(dev);
1447 struct cycx_device *card = chan->card;
1448 int bitm = 0; /* final packet */
1449 unsigned len = skb->len;
1451 if (skb->len > card->wandev.mtu) {
1452 len = card->wandev.mtu;
1453 bitm = 0x10; /* set M-bit (more data) */
1456 if (cycx_x25_send(card, chan->link, chan->lcn, bitm, len, skb->data))
1457 return 1;
1459 if (bitm) {
1460 skb_pull(skb, len);
1461 return 1;
1464 ++chan->ifstats.tx_packets;
1465 chan->ifstats.tx_bytes += len;
1467 return 0;
1470 /* Send event (connection, disconnection, etc) to X.25 socket layer */
1472 static void cycx_x25_chan_send_event(struct net_device *dev, u8 event)
1474 struct sk_buff *skb;
1475 unsigned char *ptr;
1477 if ((skb = dev_alloc_skb(1)) == NULL) {
1478 pr_err("%s: out of memory\n", __func__);
1479 return;
1482 ptr = skb_put(skb, 1);
1483 *ptr = event;
1485 skb->protocol = x25_type_trans(skb, dev);
1486 netif_rx(skb);
1489 /* Convert line speed in bps to a number used by cyclom 2x code. */
1490 static u8 bps_to_speed_code(u32 bps)
1492 u8 number = 0; /* defaults to the lowest (1200) speed ;> */
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;
1503 return number;
1506 /* log base 2 */
1507 static u8 cycx_log2(u32 n)
1509 u8 log = 0;
1511 if (!n)
1512 return 0;
1514 while (n > 1) {
1515 n >>= 1;
1516 ++log;
1519 return log;
1522 /* Convert decimal string to unsigned integer.
1523 * If len != 0 then only 'len' characters of the string are converted. */
1524 static unsigned dec_to_uint(u8 *str, int len)
1526 unsigned val = 0;
1528 if (!len)
1529 len = strlen(str);
1531 for (; len && isdigit(*str); ++str, --len)
1532 val = (val * 10) + (*str - (unsigned) '0');
1534 return val;
1537 static void reset_timer(struct net_device *dev)
1539 struct cycx_x25_channel *chan = netdev_priv(dev);
1541 if (chan->svc)
1542 mod_timer(&chan->timer, jiffies+chan->idle_tmout*HZ);
1544 #ifdef CYCLOMX_X25_DEBUG
1545 static void cycx_x25_dump_config(struct cycx_x25_config *conf)
1547 pr_info("X.25 configuration\n");
1548 pr_info("-----------------\n");
1549 pr_info("link number=%d\n", conf->link);
1550 pr_info("line speed=%d\n", conf->speed);
1551 pr_info("clock=%sternal\n", conf->clock == 8 ? "Ex" : "In");
1552 pr_info("# level 2 retransm.=%d\n", conf->n2);
1553 pr_info("level 2 window=%d\n", conf->n2win);
1554 pr_info("level 3 window=%d\n", conf->n3win);
1555 pr_info("# logical channels=%d\n", conf->nvc);
1556 pr_info("level 3 pkt len=%d\n", conf->pktlen);
1557 pr_info("my address=%d\n", conf->locaddr);
1558 pr_info("remote address=%d\n", conf->remaddr);
1559 pr_info("t1=%d seconds\n", conf->t1);
1560 pr_info("t2=%d seconds\n", conf->t2);
1561 pr_info("t21=%d seconds\n", conf->t21);
1562 pr_info("# PVCs=%d\n", conf->npvc);
1563 pr_info("t23=%d seconds\n", conf->t23);
1564 pr_info("flags=0x%x\n", conf->flags);
1567 static void cycx_x25_dump_stats(struct cycx_x25_stats *stats)
1569 pr_info("X.25 statistics\n");
1570 pr_info("--------------\n");
1571 pr_info("rx_crc_errors=%d\n", stats->rx_crc_errors);
1572 pr_info("rx_over_errors=%d\n", stats->rx_over_errors);
1573 pr_info("n2_tx_frames=%d\n", stats->n2_tx_frames);
1574 pr_info("n2_rx_frames=%d\n", stats->n2_rx_frames);
1575 pr_info("tx_timeouts=%d\n", stats->tx_timeouts);
1576 pr_info("rx_timeouts=%d\n", stats->rx_timeouts);
1577 pr_info("n3_tx_packets=%d\n", stats->n3_tx_packets);
1578 pr_info("n3_rx_packets=%d\n", stats->n3_rx_packets);
1579 pr_info("tx_aborts=%d\n", stats->tx_aborts);
1580 pr_info("rx_aborts=%d\n", stats->rx_aborts);
1583 static void cycx_x25_dump_devs(struct wan_device *wandev)
1585 struct net_device *dev = wandev->dev;
1587 pr_info("X.25 dev states\n");
1588 pr_info("name: addr: txoff: protocol:\n");
1589 pr_info("---------------------------------------\n");
1591 while(dev) {
1592 struct cycx_x25_channel *chan = netdev_priv(dev);
1594 pr_info("%-5.5s %-15.15s %d ETH_P_%s\n",
1595 chan->name, chan->addr, netif_queue_stopped(dev),
1596 chan->protocol == ETH_P_IP ? "IP" : "X25");
1597 dev = chan->slave;
1601 #endif /* CYCLOMX_X25_DEBUG */
1602 /* End */