v2.4.9.9 -> v2.4.9.10
[opensuse:kernel.git] / drivers / net / wan / cycx_x25.c
1 /*
2 * cycx_x25.c    Cyclom 2X WAN Link Driver.  X.25 module.
3 *
4 * Author:       Arnaldo Carvalho de Melo <acme@conectiva.com.br>
5 *
6 * Copyright:    (c) 1998-2001 Arnaldo Carvalho de Melo
7 *
8 * Based on sdla_x25.c by Gene Kozin <genek@compuserve.com>
9 *
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.
77 */
78
79 #define CYCLOMX_X25_DEBUG 1
80
81 #include <linux/version.h>
82 #include <linux/kernel.h>       /* printk(), and other useful stuff */
83 #include <linux/stddef.h>       /* offsetof(), etc. */
84 #include <linux/errno.h>        /* return codes */
85 #include <linux/string.h>       /* inline memset(), etc. */
86 #include <linux/slab.h> /* kmalloc(), kfree() */
87 #include <linux/wanrouter.h>    /* WAN router definitions */
88 #include <asm/byteorder.h>      /* htons(), etc. */
89 #include <linux/if_arp.h>       /* ARPHRD_HWX25 */
90 #include <linux/cyclomx.h>      /* Cyclom 2X common user API definitions */
91 #include <linux/cycx_x25.h>     /* X.25 firmware API definitions */
92
93 /* Defines & Macros */
94 #define MAX_CMD_RETRY   5
95 #define X25_CHAN_MTU    2048    /* unfragmented logical channel MTU */
96
97 /* Data Structures */
98 /* This is an extension of the 'struct net_device' we create for each network
99    interface to keep the rest of X.25 channel-specific data. */
100 typedef struct x25_channel {
101         /* This member must be first. */
102         struct net_device *slave;       /* WAN slave */
103
104         char name[WAN_IFNAME_SZ+1];     /* interface name, ASCIIZ */
105         char addr[WAN_ADDRESS_SZ+1];    /* media address, ASCIIZ */
106         char *local_addr;               /* local media address, ASCIIZ -
107                                            svc thru crossover cable */
108         s16 lcn;                        /* logical channel number/conn.req.key*/
109         u8 link;
110         struct timer_list timer;        /* timer used for svc channel disc. */
111         u16 protocol;                   /* ethertype, 0 - multiplexed */
112         u8 svc;                         /* 0 - permanent, 1 - switched */
113         u8 state;                       /* channel state */
114         u8 drop_sequence;               /* mark sequence for dropping */
115         u32 idle_tmout;                 /* sec, before disconnecting */
116         struct sk_buff *rx_skb;         /* receive socket buffer */
117         cycx_t *card;                   /* -> owner */
118         struct net_device_stats ifstats;/* interface statistics */
119 } x25_channel_t;
120
121 /* Function Prototypes */
122 /* WAN link driver entry points. These are called by the WAN router module. */
123 static int update (wan_device_t *wandev),
124            new_if (wan_device_t *wandev, struct net_device *dev,
125                    wanif_conf_t *conf),
126            del_if (wan_device_t *wandev, struct net_device *dev);
127
128 /* Network device interface */
129 static int if_init (struct net_device *dev),
130            if_open (struct net_device *dev),
131            if_close (struct net_device *dev),
132            if_header (struct sk_buff *skb, struct net_device *dev,
133                       u16 type, void *daddr, void *saddr, unsigned len),
134            if_rebuild_hdr (struct sk_buff *skb),
135            if_send (struct sk_buff *skb, struct net_device *dev);
136
137 static struct net_device_stats * if_stats (struct net_device *dev);
138
139 /* Interrupt handlers */
140 static void cyx_isr (cycx_t *card),
141             tx_intr (cycx_t *card, TX25Cmd *cmd),
142             rx_intr (cycx_t *card, TX25Cmd *cmd),
143             log_intr (cycx_t *card, TX25Cmd *cmd),
144             stat_intr (cycx_t *card, TX25Cmd *cmd),
145             connect_confirm_intr (cycx_t *card, TX25Cmd *cmd),
146             disconnect_confirm_intr (cycx_t *card, TX25Cmd *cmd),
147             connect_intr (cycx_t *card, TX25Cmd *cmd),
148             disconnect_intr (cycx_t *card, TX25Cmd *cmd),
149             spur_intr (cycx_t *card, TX25Cmd *cmd);
150
151 /* X.25 firmware interface functions */
152 static int x25_configure (cycx_t *card, TX25Config *conf),
153            x25_get_stats (cycx_t *card),
154            x25_send (cycx_t *card, u8 link, u8 lcn, u8 bitm, int len,
155                      void *buf),
156            x25_connect_response (cycx_t *card, x25_channel_t *chan),
157            x25_disconnect_response (cycx_t *card, u8 link, u8 lcn);
158
159 /* channel functions */
160 static int chan_connect (struct net_device *dev),
161            chan_send (struct net_device *dev, struct sk_buff *skb);
162
163 static void chan_disconnect (struct net_device *dev),
164             chan_x25_send_event(struct net_device *dev, u8 event);
165
166 /* Miscellaneous functions */
167 static void set_chan_state (struct net_device *dev, u8 state),
168             chan_timer (unsigned long d);
169
170 static void nibble_to_byte (u8 *s, u8 *d, u8 len, u8 nibble),
171             reset_timer (struct net_device *dev);
172
173 static u8 bps_to_speed_code (u32 bps);
174 static u8 log2 (u32 n);
175
176 static unsigned dec_to_uint (u8 *str, int len);
177
178 static struct net_device *get_dev_by_lcn (wan_device_t *wandev, s16 lcn);
179 static struct net_device *get_dev_by_dte_addr (wan_device_t *wandev, char *dte);
180
181 #ifdef CYCLOMX_X25_DEBUG
182 static void hex_dump(char *msg, unsigned char *p, int len);
183 static void x25_dump_config(TX25Config *conf);
184 static void x25_dump_stats(TX25Stats *stats);
185 static void x25_dump_devs(wan_device_t *wandev);
186 #else
187 #define hex_dump(msg, p, len)
188 #define x25_dump_config(conf)
189 #define x25_dump_stats(stats)
190 #define x25_dump_devs(wandev)
191 #endif
192 /* Public Functions */
193
194 /* X.25 Protocol Initialization routine.
195  *
196  * This routine is called by the main Cyclom 2X module during setup.  At this
197  * point adapter is completely initialized and X.25 firmware is running.
198  *  o configure adapter
199  *  o initialize protocol-specific fields of the adapter data space.
200  *
201  * Return:      0       o.k.
202  *              < 0     failure.  */
203 int cyx_init (cycx_t *card, wandev_conf_t *conf)
204 {
205         TX25Config cfg;
206
207         /* Verify configuration ID */
208         if (conf->config_id != WANCONFIG_X25) {
209                 printk(KERN_INFO "%s: invalid configuration ID %u!\n",
210                                  card->devname, conf->config_id);
211                 return -EINVAL;
212         }
213
214         /* Initialize protocol-specific fields */
215         card->mbox  = card->hw.dpmbase + X25_MBOX_OFFS;
216         card->u.x.connection_keys = 0;
217         card->u.x.lock = SPIN_LOCK_UNLOCKED;
218
219         /* Configure adapter. Here we set reasonable defaults, then parse
220          * device configuration structure and set configuration options.
221          * Most configuration options are verified and corrected (if
222          * necessary) since we can't rely on the adapter to do so and don't
223          * want it to fail either. */
224         memset(&cfg, 0, sizeof(cfg));
225         cfg.link = 0;
226         cfg.clock = conf->clocking == WANOPT_EXTERNAL ? 8 : 55;
227         cfg.speed = bps_to_speed_code(conf->bps);
228         cfg.n3win = 7;
229         cfg.n2win = 2;
230         cfg.n2 = 5;
231         cfg.nvc = 1;
232         cfg.npvc = 1;
233         cfg.flags = 0x02; /* default = V35 */
234         cfg.t1 = 10;   /* line carrier timeout */
235         cfg.t2 = 29;   /* tx timeout */
236         cfg.t21 = 180; /* CALL timeout */
237         cfg.t23 = 180; /* CLEAR timeout */
238
239         /* adjust MTU */
240         if (!conf->mtu || conf->mtu >= 512)
241                 card->wandev.mtu = 512;
242         else if (conf->mtu >= 256)
243                 card->wandev.mtu = 256;
244         else if (conf->mtu >= 128)
245                 card->wandev.mtu = 128;
246         else
247                 card->wandev.mtu = 64;
248
249         cfg.pktlen = log2(card->wandev.mtu);
250
251         if (conf->station == WANOPT_DTE) {
252                 cfg.locaddr = 3; /* DTE */
253                 cfg.remaddr = 1; /* DCE */
254         } else {
255                 cfg.locaddr = 1; /* DCE */
256                 cfg.remaddr = 3; /* DTE */
257         }
258
259         if (conf->interface == WANOPT_RS232)
260                 cfg.flags = 0;      /* FIXME just reset the 2nd bit */
261
262         if (conf->u.x25.hi_pvc) {
263                 card->u.x.hi_pvc = min_t(unsigned int, conf->u.x25.hi_pvc, 4095);
264                 card->u.x.lo_pvc = min_t(unsigned int, conf->u.x25.lo_pvc, card->u.x.hi_pvc);
265         }
266
267         if (conf->u.x25.hi_svc) {
268                 card->u.x.hi_svc = min_t(unsigned int, conf->u.x25.hi_svc, 4095);
269                 card->u.x.lo_svc = min_t(unsigned int, conf->u.x25.lo_svc, card->u.x.hi_svc);
270         }
271
272         if (card->u.x.lo_pvc == 255)
273                 cfg.npvc = 0;
274         else
275                 cfg.npvc = card->u.x.hi_pvc - card->u.x.lo_pvc + 1;
276
277         cfg.nvc = card->u.x.hi_svc - card->u.x.lo_svc + 1 + cfg.npvc;
278
279         if (conf->u.x25.hdlc_window)
280                 cfg.n2win = min_t(unsigned int, conf->u.x25.hdlc_window, 7);
281
282         if (conf->u.x25.pkt_window)
283                 cfg.n3win = min_t(unsigned int, conf->u.x25.pkt_window, 7);
284
285         if (conf->u.x25.t1)
286                 cfg.t1 = min_t(unsigned int, conf->u.x25.t1, 30);
287
288         if (conf->u.x25.t2)
289                 cfg.t2 = min_t(unsigned int, conf->u.x25.t2, 30);
290
291         if (conf->u.x25.t11_t21)
292                 cfg.t21 = min_t(unsigned int, conf->u.x25.t11_t21, 30);
293
294         if (conf->u.x25.t13_t23)
295                 cfg.t23 = min_t(unsigned int, conf->u.x25.t13_t23, 30);
296
297         if (conf->u.x25.n2)
298                 cfg.n2 = min_t(unsigned int, conf->u.x25.n2, 30);
299
300         /* initialize adapter */
301         if (x25_configure(card, &cfg))
302                 return -EIO;
303
304         /* Initialize protocol-specific fields of adapter data space */
305         card->wandev.bps        = conf->bps;
306         card->wandev.interface  = conf->interface;
307         card->wandev.clocking   = conf->clocking;
308         card->wandev.station    = conf->station;
309         card->isr               = cyx_isr;
310         card->exec              = NULL;
311         card->wandev.update     = update;
312         card->wandev.new_if     = new_if;
313         card->wandev.del_if     = del_if;
314         card->wandev.state      = WAN_DISCONNECTED;
315
316         return 0;
317 }
318
319 /* WAN Device Driver Entry Points */
320 /* Update device status & statistics. */
321 static int update (wan_device_t *wandev)
322 {
323         /* sanity checks */
324         if (!wandev || !wandev->private)
325                 return -EFAULT;
326
327         if (wandev->state == WAN_UNCONFIGURED)
328                 return -ENODEV;
329
330         x25_get_stats(wandev->private);
331
332         return 0;
333 }
334
335 /* Create new logical channel.
336  * This routine is called by the router when ROUTER_IFNEW IOCTL is being
337  * handled.
338  * o parse media- and hardware-specific configuration
339  * o make sure that a new channel can be created
340  * o allocate resources, if necessary
341  * o prepare network device structure for registration.
342  *
343  * Return:      0       o.k.
344  *              < 0     failure (channel will not be created) */
345 static int new_if (wan_device_t *wandev, struct net_device *dev,
346                    wanif_conf_t *conf)
347 {
348         cycx_t *card = wandev->private;
349         x25_channel_t *chan;
350         int err = 0;
351
352         if (!conf->name[0] || strlen(conf->name) > WAN_IFNAME_SZ) {
353                 printk(KERN_INFO "%s: invalid interface name!\n",card->devname);
354                 return -EINVAL;
355         }
356
357         /* allocate and initialize private data */
358         if ((chan = kmalloc(sizeof(x25_channel_t), GFP_KERNEL)) == NULL)
359                 return -ENOMEM;
360
361         memset(chan, 0, sizeof(x25_channel_t));
362         strcpy(chan->name, conf->name);
363         chan->card = card;
364         chan->link = conf->port;
365         chan->protocol = conf->protocol ? ETH_P_X25 : ETH_P_IP;
366         chan->rx_skb = NULL;
367         /* only used in svc connected thru crossover cable */
368         chan->local_addr = NULL;
369
370         if (conf->addr[0] == '@') {     /* SVC */
371                 int len = strlen(conf->local_addr);
372
373                 if (len) {
374                         if (len > WAN_ADDRESS_SZ) {
375                                 printk(KERN_ERR "%s: %s local addr too long!\n",
376                                                 wandev->name, chan->name);
377                                 kfree(chan);
378                                 return -EINVAL;
379                         } else {
380                                 chan->local_addr = kmalloc(len + 1, GFP_KERNEL);
381
382                                 if (!chan->local_addr) {
383                                         kfree(chan);
384                                         return -ENOMEM;
385                                 }
386                         }
387
388                         strncpy(chan->local_addr, conf->local_addr,
389                                 WAN_ADDRESS_SZ);
390                 }
391
392                 chan->svc = 1;
393                 strncpy(chan->addr, &conf->addr[1], WAN_ADDRESS_SZ);
394                 init_timer(&chan->timer);
395                 chan->timer.function = chan_timer;
396                 chan->timer.data = (unsigned long)dev;
397
398                 /* Set channel timeouts (default if not specified) */
399                 chan->idle_tmout = conf->idle_timeout ? conf->idle_timeout : 90;
400         } else if (is_digit(conf->addr[0])) {   /* PVC */
401                 s16 lcn = dec_to_uint(conf->addr, 0);
402
403                 if (lcn >= card->u.x.lo_pvc && lcn <= card->u.x.hi_pvc)
404                         chan->lcn = lcn;
405                 else {
406                         printk(KERN_ERR
407                                 "%s: PVC %u is out of range on interface %s!\n",
408                                 wandev->name, lcn, chan->name);
409                         err = -EINVAL;
410                 }
411         } else {
412                 printk(KERN_ERR "%s: invalid media address on interface %s!\n",
413                                 wandev->name, chan->name);
414                 err = -EINVAL;
415         }
416
417         if (err) {
418                 if (chan->local_addr)
419                         kfree(chan->local_addr);
420
421                 kfree(chan);
422                 return err;
423         }
424
425         /* prepare network device data space for registration */
426         strcpy(dev->name, chan->name);
427         dev->init = if_init;
428         dev->priv = chan;
429
430         return 0;
431 }
432
433 /* Delete logical channel. */
434 static int del_if (wan_device_t *wandev, struct net_device *dev)
435 {
436         if (dev->priv) {
437                 x25_channel_t *chan = dev->priv;
438
439                 if (chan->svc) {
440                         if (chan->local_addr)
441                                 kfree(chan->local_addr);
442
443                         if (chan->state == WAN_CONNECTED)
444                                 del_timer(&chan->timer);
445                 }
446
447                 kfree(chan);
448                 dev->priv = NULL;
449         }
450
451         return 0;
452 }
453
454 /* Network Device Interface */
455 /* Initialize Linux network interface.
456  *
457  * This routine is called only once for each interface, during Linux network
458  * interface registration.  Returning anything but zero will fail interface
459  * registration. */
460 static int if_init (struct net_device *dev)
461 {
462         x25_channel_t *chan = dev->priv;
463         cycx_t *card = chan->card;
464         wan_device_t *wandev = &card->wandev;
465
466         /* Initialize device driver entry points */
467         dev->open = if_open;
468         dev->stop = if_close;
469         dev->hard_header = if_header;
470         dev->rebuild_header = if_rebuild_hdr;
471         dev->hard_start_xmit = if_send;
472         dev->get_stats = if_stats;
473
474         /* Initialize media-specific parameters */
475         dev->mtu = X25_CHAN_MTU;
476         dev->type = ARPHRD_HWX25;       /* ARP h/w type */
477         dev->hard_header_len = 0;       /* media header length */
478         dev->addr_len = 0;              /* hardware address length */
479
480         if (!chan->svc)
481                 *(u16*)dev->dev_addr = htons(chan->lcn);
482
483         /* Initialize hardware parameters (just for reference) */
484         dev->irq = wandev->irq;
485         dev->dma = wandev->dma;
486         dev->base_addr = wandev->ioport;
487         dev->mem_start = (unsigned long)wandev->maddr;
488         dev->mem_end = (unsigned long)(wandev->maddr + wandev->msize - 1);
489         dev->flags |= IFF_NOARP;
490
491         /* Set transmit buffer queue length */
492         dev->tx_queue_len = 10;
493
494         /* Initialize socket buffers */
495         set_chan_state(dev, WAN_DISCONNECTED);
496
497         return 0;
498 }
499
500 /* Open network interface.
501  * o prevent module from unloading by incrementing use count
502  * o if link is disconnected then initiate connection
503  *
504  * Return 0 if O.k. or errno.  */
505 static int if_open (struct net_device *dev)
506 {
507         x25_channel_t *chan = dev->priv;
508         cycx_t *card = chan->card;
509
510         if (netif_running(dev))
511                 return -EBUSY; /* only one open is allowed */ 
512
513         netif_start_queue(dev);
514         cyclomx_mod_inc_use_count(card);
515
516         return 0;
517 }
518
519 /* Close network interface.
520  * o reset flags.
521  * o if there's no more open channels then disconnect physical link. */
522 static int if_close (struct net_device *dev)
523 {
524         x25_channel_t *chan = dev->priv;
525         cycx_t *card = chan->card;
526
527         netif_stop_queue(dev);
528         
529         if (chan->state == WAN_CONNECTED || chan->state == WAN_CONNECTING)
530                 chan_disconnect(dev);
531                 
532         cyclomx_mod_dec_use_count(card);
533
534         return 0;
535 }
536
537 /* Build media header.
538  * o encapsulate packet according to encapsulation type.
539  *
540  * The trick here is to put packet type (Ethertype) into 'protocol' field of
541  * the socket buffer, so that we don't forget it.  If encapsulation fails,
542  * set skb->protocol to 0 and discard packet later.
543  *
544  * Return:      media header length. */
545 static int if_header (struct sk_buff *skb, struct net_device *dev,
546                       u16 type, void *daddr, void *saddr, unsigned len)
547 {
548         skb->protocol = type;
549
550         return dev->hard_header_len;
551 }
552
553 /* * Re-build media header.
554  * Return:      1       physical address resolved.
555  *              0       physical address not resolved */
556 static int if_rebuild_hdr (struct sk_buff *skb)
557 {
558         return 1;
559 }
560
561 /* Send a packet on a network interface.
562  * o set busy flag (marks start of the transmission).
563  * o check link state. If link is not up, then drop the packet.
564  * o check channel status. If it's down then initiate a call.
565  * o pass a packet to corresponding WAN device.
566  * o free socket buffer
567  *
568  * Return:      0       complete (socket buffer must be freed)
569  *              non-0   packet may be re-transmitted (tbusy must be set)
570  *
571  * Notes:
572  * 1. This routine is called either by the protocol stack or by the "net
573  *    bottom half" (with interrupts enabled).
574  * 2. Setting tbusy flag will inhibit further transmit requests from the
575  *    protocol stack and can be used for flow control with protocol layer. */
576 static int if_send (struct sk_buff *skb, struct net_device *dev)
577 {
578         x25_channel_t *chan = dev->priv;
579         cycx_t *card = chan->card;
580
581         if (!chan->svc)
582                 chan->protocol = skb->protocol;
583
584         if (card->wandev.state != WAN_CONNECTED)
585                 ++chan->ifstats.tx_dropped;
586         else if (chan->svc && chan->protocol &&
587                  chan->protocol != skb->protocol) {
588                 printk(KERN_INFO
589                         "%s: unsupported Ethertype 0x%04X on interface %s!\n",
590                         card->devname, skb->protocol, dev->name);
591                 ++chan->ifstats.tx_errors;
592         } else if (chan->protocol == ETH_P_IP) {
593                 switch (chan->state) {
594                         case WAN_DISCONNECTED:
595                                 if (chan_connect(dev)) {
596                                         netif_stop_queue(dev);
597                                         return -EBUSY;
598                                 }
599                                 /* fall thru */
600                         case WAN_CONNECTED:
601                                 reset_timer(dev);
602                                 dev->trans_start = jiffies;
603                                 netif_stop_queue(dev);
604
605                                 if (chan_send(dev, skb))
606                                         return -EBUSY;
607
608                                 break;
609                         default:
610                                 ++chan->ifstats.tx_dropped;     
611                                 ++card->wandev.stats.tx_dropped;
612                 } 
613         } else { /* chan->protocol == ETH_P_X25 */
614                 switch (skb->data[0]) {
615                         case 0: break;
616                         case 1: /* Connect request */
617                                 chan_connect(dev);
618                                 goto free_packet;
619                         case 2: /* Disconnect request */
620                                 chan_disconnect(dev);
621                                 goto free_packet;
622                         default:
623                                 printk(KERN_INFO
624                                        "%s: unknown %d x25-iface request on %s!\n",
625                                         card->devname, skb->data[0], dev->name);
626                                 ++chan->ifstats.tx_errors;
627                                 goto free_packet;
628                 }
629
630                 skb_pull(skb, 1); /* Remove control byte */
631                 reset_timer(dev);
632                 dev->trans_start = jiffies;
633                 netif_stop_queue(dev);
634                 
635                 if (chan_send(dev, skb)) {
636                         /* prepare for future retransmissions */
637                         skb_push(skb, 1);
638                         return -EBUSY;
639                 }
640         }
641
642 free_packet:
643         dev_kfree_skb(skb);
644
645         return 0;
646 }
647
648 /* Get Ethernet-style interface statistics.
649  * Return a pointer to struct net_device_stats */
650 static struct net_device_stats *if_stats (struct net_device *dev)
651 {
652         x25_channel_t *chan = dev->priv;
653
654         return chan ? &chan->ifstats : NULL;
655 }
656
657 /* Interrupt Handlers */
658 /* X.25 Interrupt Service Routine. */
659 static void cyx_isr (cycx_t *card)
660 {
661         TX25Cmd cmd;
662         u16 z = 0;
663
664         card->in_isr = 1;
665         card->buff_int_mode_unbusy = 0;
666         cycx_peek(&card->hw, X25_RXMBOX_OFFS, &cmd, sizeof(cmd));
667
668         switch (cmd.command) {
669                 case X25_DATA_INDICATION:
670                         rx_intr(card, &cmd);
671                         break;
672                 case X25_ACK_FROM_VC:
673                         tx_intr(card, &cmd);
674                         break;
675                 case X25_LOG: 
676                         log_intr(card, &cmd);
677                         break;
678                 case X25_STATISTIC: 
679                         stat_intr(card, &cmd);
680                         break;
681                 case X25_CONNECT_CONFIRM:
682                         connect_confirm_intr(card, &cmd);
683                         break;
684                 case X25_CONNECT_INDICATION:
685                         connect_intr(card, &cmd);
686                         break;
687                 case X25_DISCONNECT_INDICATION:
688                         disconnect_intr(card, &cmd);
689                         break;
690                 case X25_DISCONNECT_CONFIRM:
691                         disconnect_confirm_intr(card, &cmd);
692                         break;
693                 case X25_LINE_ON:
694                         cyclomx_set_state(card, WAN_CONNECTED);
695                         break;
696                 case X25_LINE_OFF:
697                         cyclomx_set_state(card, WAN_DISCONNECTED);
698                         break;
699                 default: 
700                         spur_intr(card, &cmd); /* unwanted interrupt */
701         }
702
703         cycx_poke(&card->hw, 0, &z, sizeof(z));
704         cycx_poke(&card->hw, X25_RXMBOX_OFFS, &z, sizeof(z));
705         card->in_isr = 0;
706 }
707
708 /* Transmit interrupt handler.
709  *      o Release socket buffer
710  *      o Clear 'tbusy' flag */
711 static void tx_intr (cycx_t *card, TX25Cmd *cmd)
712 {
713         struct net_device *dev;
714         wan_device_t *wandev = &card->wandev;
715         u8 lcn;
716
717         cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
718
719         /* unbusy device and then dev_tint(); */
720         if ((dev = get_dev_by_lcn(wandev, lcn)) != NULL) {
721                 card->buff_int_mode_unbusy = 1;
722                 netif_wake_queue(dev);
723         } else
724                 printk(KERN_ERR "%s:ackvc for inexistent lcn %d\n",
725                                  card->devname, lcn);
726 }
727
728 /* Receive interrupt handler.
729  * This routine handles fragmented IP packets using M-bit according to the
730  * RFC1356.
731  * o map logical channel number to network interface.
732  * o allocate socket buffer or append received packet to the existing one.
733  * o if M-bit is reset (i.e. it's the last packet in a sequence) then 
734  *   decapsulate packet and pass socket buffer to the protocol stack.
735  *
736  * Notes:
737  * 1. When allocating a socket buffer, if M-bit is set then more data is
738  *    coming and we have to allocate buffer for the maximum IP packet size
739  *    expected on this channel.
740  * 2. If something goes wrong and X.25 packet has to be dropped (e.g. no
741  *    socket buffers available) the whole packet sequence must be discarded. */
742 static void rx_intr (cycx_t *card, TX25Cmd *cmd)
743 {
744         wan_device_t *wandev = &card->wandev;
745         struct net_device *dev;
746         x25_channel_t *chan;
747         struct sk_buff *skb;
748         u8 bitm, lcn;
749         int pktlen = cmd->len - 5;
750
751         cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
752         cycx_peek(&card->hw, cmd->buf + 4, &bitm, sizeof(bitm));
753         bitm &= 0x10;
754
755         if ((dev = get_dev_by_lcn(wandev, lcn)) == NULL) {
756                 /* Invalid channel, discard packet */
757                 printk(KERN_INFO "%s: receiving on orphaned LCN %d!\n",
758                                  card->devname, lcn);
759                 return;
760         }
761
762         chan = dev->priv;
763         reset_timer(dev);
764
765         if (chan->drop_sequence) {
766                 if (!bitm)
767                         chan->drop_sequence = 0;
768                 else
769                         return;
770         }
771
772         if ((skb = chan->rx_skb) == NULL) {
773                 /* Allocate new socket buffer */
774                 int bufsize = bitm ? dev->mtu : pktlen;
775
776                 if ((skb = dev_alloc_skb((chan->protocol == ETH_P_X25 ? 1 : 0) +
777                                          bufsize +
778                                          dev->hard_header_len)) == NULL) {
779                         printk(KERN_INFO "%s: no socket buffers available!\n",
780                                          card->devname);
781                         chan->drop_sequence = 1;
782                         ++chan->ifstats.rx_dropped;
783                         return;
784                 }
785
786                 if (chan->protocol == ETH_P_X25) /* X.25 socket layer control */
787                         /* 0 = data packet (dev_alloc_skb zeroed skb->data) */
788                         skb_put(skb, 1); 
789
790                 skb->dev = dev;
791                 skb->protocol = htons(chan->protocol);
792                 chan->rx_skb = skb;
793         }
794
795         if (skb_tailroom(skb) < pktlen) {
796                 /* No room for the packet. Call off the whole thing! */
797                 dev_kfree_skb_irq(skb);
798                 chan->rx_skb = NULL;
799
800                 if (bitm)
801                         chan->drop_sequence = 1;
802
803                 printk(KERN_INFO "%s: unexpectedly long packet sequence "
804                         "on interface %s!\n", card->devname, dev->name);
805                 ++chan->ifstats.rx_length_errors;
806                 return;
807         }
808
809         /* Append packet to the socket buffer  */
810         cycx_peek(&card->hw, cmd->buf + 5, skb_put(skb, pktlen), pktlen);
811
812         if (bitm)
813                 return; /* more data is coming */
814
815         chan->rx_skb = NULL;            /* dequeue packet */
816
817         ++chan->ifstats.rx_packets;
818         chan->ifstats.rx_bytes += pktlen;
819
820         skb->mac.raw = skb->data;
821         netif_rx(skb);
822         dev->last_rx = jiffies;         /* timestamp */
823 }
824
825 /* Connect interrupt handler. */
826 static void connect_intr (cycx_t *card, TX25Cmd *cmd)
827 {
828         wan_device_t *wandev = &card->wandev;
829         struct net_device *dev = NULL;
830         x25_channel_t *chan;
831         u8 d[32],
832            loc[24],
833            rem[24];
834         u8 lcn, sizeloc, sizerem;
835
836         cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
837         cycx_peek(&card->hw, cmd->buf + 5, &sizeloc, sizeof(sizeloc));
838         cycx_peek(&card->hw, cmd->buf + 6, d, cmd->len - 6);
839
840         sizerem = sizeloc >> 4;
841         sizeloc &= 0x0F;
842
843         loc[0] = rem[0] = '\0';
844
845         if (sizeloc)
846                 nibble_to_byte(d, loc, sizeloc, 0);
847
848         if (sizerem)
849                 nibble_to_byte(d + (sizeloc >> 1), rem, sizerem, sizeloc & 1);
850
851         dprintk(1, KERN_INFO "connect_intr:lcn=%d, local=%s, remote=%s\n",
852                           lcn, loc, rem);
853
854         if ((dev = get_dev_by_dte_addr(wandev, rem)) == NULL) {
855                 /* Invalid channel, discard packet */
856                 printk(KERN_INFO "%s: connect not expected: remote %s!\n",
857                                  card->devname, rem);
858                 return;
859         }
860
861         chan = dev->priv;
862         chan->lcn = lcn;
863         x25_connect_response(card, chan);
864         set_chan_state(dev, WAN_CONNECTED);
865 }
866
867 /* Connect confirm interrupt handler. */
868 static void connect_confirm_intr (cycx_t *card, TX25Cmd *cmd)
869 {
870         wan_device_t *wandev = &card->wandev;
871         struct net_device *dev;
872         x25_channel_t *chan;
873         u8 lcn, key;
874
875         cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
876         cycx_peek(&card->hw, cmd->buf + 1, &key, sizeof(key));
877         dprintk(1, KERN_INFO "%s: connect_confirm_intr:lcn=%d, key=%d\n",
878                           card->devname, lcn, key);
879
880         if ((dev = get_dev_by_lcn(wandev, -key)) == NULL) {
881                 /* Invalid channel, discard packet */
882                 clear_bit(--key, (void*)&card->u.x.connection_keys);
883                 printk(KERN_INFO "%s: connect confirm not expected: lcn %d, "
884                                  "key=%d!\n", card->devname, lcn, key);
885                 return;
886         }
887
888         clear_bit(--key, (void*)&card->u.x.connection_keys);
889         chan = dev->priv;
890         chan->lcn = lcn;
891         set_chan_state(dev, WAN_CONNECTED);
892 }
893
894 /* Disconnect confirm interrupt handler. */
895 static void disconnect_confirm_intr (cycx_t *card, TX25Cmd *cmd)
896 {
897         wan_device_t *wandev = &card->wandev;
898         struct net_device *dev;
899         u8 lcn;
900
901         cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
902         dprintk(1, KERN_INFO "%s: disconnect_confirm_intr:lcn=%d\n",
903                           card->devname, lcn);
904         if ((dev = get_dev_by_lcn(wandev, lcn)) == NULL) {
905                 /* Invalid channel, discard packet */
906                 printk(KERN_INFO "%s:disconnect confirm not expected!:lcn %d\n",
907                                  card->devname, lcn);
908                 return;
909         }
910
911         set_chan_state(dev, WAN_DISCONNECTED);
912 }
913
914 /* disconnect interrupt handler. */
915 static void disconnect_intr (cycx_t *card, TX25Cmd *cmd)
916 {
917         wan_device_t *wandev = &card->wandev;
918         struct net_device *dev;
919         u8 lcn;
920
921         cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
922         dprintk(1, KERN_INFO "disconnect_intr:lcn=%d\n", lcn);
923
924         if ((dev = get_dev_by_lcn(wandev, lcn)) != NULL) {
925                 x25_channel_t *chan = dev->priv;
926
927                 x25_disconnect_response(card, chan->link, lcn);
928                 set_chan_state(dev, WAN_DISCONNECTED);
929         } else
930                 x25_disconnect_response(card, 0, lcn);
931 }
932
933 /* LOG interrupt handler. */
934 static void log_intr (cycx_t *card, TX25Cmd *cmd)
935 {
936 #if CYCLOMX_X25_DEBUG
937         char bf[20];
938         u16 size, toread, link, msg_code;
939         u8 code, routine;
940
941         cycx_peek(&card->hw, cmd->buf, &msg_code, sizeof(msg_code));
942         cycx_peek(&card->hw, cmd->buf + 2, &link, sizeof(link));
943         cycx_peek(&card->hw, cmd->buf + 4, &size, sizeof(size));
944         /* at most 20 bytes are available... thanks to Daniela :) */
945         toread = size < 20 ? size : 20;
946         cycx_peek(&card->hw, cmd->buf + 10, &bf, toread);
947         cycx_peek(&card->hw, cmd->buf + 10 + toread, &code, 1);
948         cycx_peek(&card->hw, cmd->buf + 10 + toread + 1, &routine, 1);
949
950         printk(KERN_INFO "cyx_isr: X25_LOG (0x4500) indic.:\n");
951         printk(KERN_INFO "cmd->buf=0x%X\n", cmd->buf);
952         printk(KERN_INFO "Log message code=0x%X\n", msg_code);
953         printk(KERN_INFO "Link=%d\n", link);
954         printk(KERN_INFO "log code=0x%X\n", code);
955         printk(KERN_INFO "log routine=0x%X\n", routine);
956         printk(KERN_INFO "Message size=%d\n", size);
957         hex_dump("Message", bf, toread);
958 #endif
959 }
960
961 /* STATISTIC interrupt handler. */
962 static void stat_intr (cycx_t *card, TX25Cmd *cmd)
963 {
964         cycx_peek(&card->hw, cmd->buf, &card->u.x.stats,
965                   sizeof(card->u.x.stats));
966         hex_dump("stat_intr", (unsigned char*)&card->u.x.stats,
967                  sizeof(card->u.x.stats));
968         x25_dump_stats(&card->u.x.stats);
969         wake_up_interruptible(&card->wait_stats);
970 }
971
972 /* Spurious interrupt handler.
973  * o print a warning
974  * If number of spurious interrupts exceeded some limit, then ??? */
975 static void spur_intr (cycx_t *card, TX25Cmd *cmd)
976 {
977         printk(KERN_INFO "%s: spurious interrupt (0x%X)!\n",
978                          card->devname, cmd->command);
979 }
980 #ifdef CYCLOMX_X25_DEBUG
981 static void hex_dump(char *msg, unsigned char *p, int len)
982 {
983         unsigned char hex[1024],
984                 * phex = hex;
985
986         if (len >= (sizeof(hex) / 2))
987                 len = (sizeof(hex) / 2) - 1;
988
989         while (len--) {
990                 sprintf(phex, "%02x", *p++);
991                 phex += 2;
992         }
993
994         printk(KERN_INFO "%s: %s\n", msg, hex);
995 }
996 #endif
997
998 /* Cyclom 2X Firmware-Specific Functions */
999 /* Exec X.25 command. */
1000 static int x25_exec (cycx_t *card, int command, int link,
1001                      void *d1, int len1, void *d2, int len2)
1002 {
1003         TX25Cmd c;
1004         unsigned long flags;
1005         u32 addr = 0x1200 + 0x2E0 * link + 0x1E2;
1006         u8 retry = MAX_CMD_RETRY;
1007         int err = 0;
1008
1009         c.command = command;
1010         c.link = link;
1011         c.len = len1 + len2;
1012
1013         spin_lock_irqsave(&card->u.x.lock, flags);
1014
1015         /* write command */
1016         cycx_poke(&card->hw, X25_MBOX_OFFS, &c, sizeof(c) - sizeof(c.buf));
1017
1018         /* write X.25 data */
1019         if (d1) {
1020                 cycx_poke(&card->hw, addr, d1, len1);
1021
1022                 if (d2) {
1023                         if (len2 > 254) {
1024                                 u32 addr1 = 0xA00 + 0x400 * link;
1025
1026                                 cycx_poke(&card->hw, addr + len1, d2, 249);
1027                                 cycx_poke(&card->hw, addr1, ((u8*)d2) + 249,
1028                                           len2 - 249);
1029                         } else
1030                                 cycx_poke(&card->hw, addr + len1, d2, len2);
1031                 }
1032         }
1033
1034         /* generate interruption, executing command */
1035         cycx_intr(&card->hw);
1036
1037         /* wait till card->mbox == 0 */
1038         do {
1039                 err = cycx_exec(card->mbox);
1040         } while (retry-- && err);
1041
1042         spin_unlock_irqrestore(&card->u.x.lock, flags);
1043
1044         return err;
1045 }
1046
1047 /* Configure adapter. */
1048 static int x25_configure (cycx_t *card, TX25Config *conf)
1049 {
1050         struct {
1051                 u16 nlinks;
1052                 TX25Config conf[2];
1053         } x25_cmd_conf;
1054
1055         memset (&x25_cmd_conf, 0, sizeof(x25_cmd_conf));
1056         x25_cmd_conf.nlinks = 2;
1057         x25_cmd_conf.conf[0] = *conf;
1058         /* FIXME: we need to find a way in the wanrouter framework
1059                   to configure the second link, for now lets use it
1060                   with the same config from the first link, fixing
1061                   the interface type to RS232, the speed in 38400 and
1062                   the clock to external */
1063         x25_cmd_conf.conf[1] = *conf;
1064         x25_cmd_conf.conf[1].link = 1;
1065         x25_cmd_conf.conf[1].speed = 5; /* 38400 */
1066         x25_cmd_conf.conf[1].clock = 8;
1067         x25_cmd_conf.conf[1].flags = 0; /* default = RS232 */
1068
1069         x25_dump_config(&x25_cmd_conf.conf[0]);
1070         x25_dump_config(&x25_cmd_conf.conf[1]);
1071
1072         return x25_exec(card, X25_CONFIG, 0,
1073                         &x25_cmd_conf, sizeof(x25_cmd_conf), NULL, 0);
1074 }
1075
1076 /* Get protocol statistics. */
1077 static int x25_get_stats (cycx_t *card)
1078 {
1079         /* the firmware expects 20 in the size field!!!
1080            thanks to Daniela */
1081         int err = x25_exec(card, X25_STATISTIC, 0, NULL, 20, NULL, 0);
1082
1083         if (err)
1084                 return err;
1085
1086         interruptible_sleep_on(&card->wait_stats);
1087
1088         if (signal_pending(current))
1089                 return -EINTR;
1090
1091         card->wandev.stats.rx_packets = card->u.x.stats.n2_rx_frames;
1092         card->wandev.stats.rx_over_errors = card->u.x.stats.rx_over_errors;
1093         card->wandev.stats.rx_crc_errors = card->u.x.stats.rx_crc_errors;
1094         card->wandev.stats.rx_length_errors = 0; /* not available from fw */
1095         card->wandev.stats.rx_frame_errors = 0; /* not available from fw */
1096         card->wandev.stats.rx_missed_errors = card->u.x.stats.rx_aborts;
1097         card->wandev.stats.rx_dropped = 0; /* not available from fw */
1098         card->wandev.stats.rx_errors = 0; /* not available from fw */
1099         card->wandev.stats.tx_packets = card->u.x.stats.n2_tx_frames;
1100         card->wandev.stats.tx_aborted_errors = card->u.x.stats.tx_aborts;
1101         card->wandev.stats.tx_dropped = 0; /* not available from fw */
1102         card->wandev.stats.collisions = 0; /* not available from fw */
1103         card->wandev.stats.tx_errors = 0; /* not available from fw */
1104
1105         x25_dump_devs(&card->wandev);
1106
1107         return 0;
1108 }
1109
1110 /* return the number of nibbles */
1111 static int byte_to_nibble(u8 *s, u8 *d, char *nibble)
1112 {
1113         int i = 0;
1114
1115         if (*nibble && *s) {
1116                 d[i] |= *s++ - '0';
1117                 *nibble = 0;
1118                 ++i;
1119         }
1120
1121         while (*s) {
1122                 d[i] = (*s - '0') << 4;
1123                 if (*(s + 1))
1124                         d[i] |= *(s + 1) - '0';
1125                 else {
1126                         *nibble = 1;
1127                         break;
1128                 }
1129                 ++i;
1130                 s += 2;
1131         }
1132
1133         return i;
1134 }
1135
1136 static void nibble_to_byte(u8 *s, u8 *d, u8 len, u8 nibble)
1137 {
1138         if (nibble) {
1139                 *d++ = '0' + (*s++ & 0x0F);
1140                 --len;
1141         }
1142
1143         while (len) {
1144                 *d++ = '0' + (*s >> 4);
1145
1146                 if (--len) {
1147                         *d++ = '0' + (*s & 0x0F);
1148                         --len;
1149                 } else break;
1150                 
1151                 ++s;
1152         }
1153
1154         *d = '\0';
1155 }
1156
1157 /* Place X.25 call. */
1158 static int x25_place_call (cycx_t *card, x25_channel_t *chan)
1159 {
1160         int err = 0,
1161             len;
1162         char d[64],
1163              nibble = 0,
1164              mylen = chan->local_addr ? strlen(chan->local_addr) : 0,
1165              remotelen = strlen(chan->addr);
1166         u8 key;
1167
1168         if (card->u.x.connection_keys == ~0UL) {
1169                 printk(KERN_INFO "%s: too many simultaneous connection "
1170                                  "requests!\n", card->devname);
1171                 return -EAGAIN;
1172         }
1173
1174         key = ffz(card->u.x.connection_keys);
1175         set_bit(key, (void*)&card->u.x.connection_keys);
1176         ++key;
1177         dprintk(1, KERN_INFO "%s:x25_place_call:key=%d\n", card->devname, key);
1178         memset(d, 0, sizeof(d));
1179         d[1] = key; /* user key */
1180         d[2] = 0x10;
1181         d[4] = 0x0B;
1182
1183         len = byte_to_nibble(chan->addr, d + 6, &nibble);
1184
1185         if (chan->local_addr)
1186                 len += byte_to_nibble(chan->local_addr, d + 6 + len, &nibble);
1187
1188         if (nibble)
1189                 ++len;
1190
1191         d[5] = mylen << 4 | remotelen;
1192         d[6 + len + 1] = 0xCC; /* TCP/IP over X.25, thanks to Daniela :) */
1193         
1194         if ((err = x25_exec(card, X25_CONNECT_REQUEST, chan->link,
1195                             &d, 7 + len + 1, NULL, 0)) != 0)
1196                 clear_bit(--key, (void*)&card->u.x.connection_keys);
1197         else
1198                 chan->lcn = -key;
1199
1200         return err;
1201 }
1202
1203 /* Place X.25 CONNECT RESPONSE. */
1204 static int x25_connect_response (cycx_t *card, x25_channel_t *chan)
1205 {
1206         u8 d[8];
1207
1208         memset(d, 0, sizeof(d));
1209         d[0] = d[3] = chan->lcn;
1210         d[2] = 0x10;
1211         d[4] = 0x0F;
1212         d[7] = 0xCC; /* TCP/IP over X.25, thanks Daniela */
1213
1214         return x25_exec(card, X25_CONNECT_RESPONSE, chan->link, &d, 8, NULL, 0);
1215 }
1216
1217 /* Place X.25 DISCONNECT RESPONSE.  */
1218 static int x25_disconnect_response (cycx_t *card, u8 link, u8 lcn)
1219 {
1220         char d[5];
1221
1222         memset(d, 0, sizeof(d));
1223         d[0] = d[3] = lcn;
1224         d[2] = 0x10;
1225         d[4] = 0x17;
1226
1227         return x25_exec(card, X25_DISCONNECT_RESPONSE, link, &d, 5, NULL, 0);
1228 }
1229
1230 /* Clear X.25 call.  */
1231 static int x25_clear_call (cycx_t *card, u8 link, u8 lcn, u8 cause, u8 diagn)
1232 {
1233         u8 d[7];
1234
1235         memset(d, 0, sizeof(d));
1236         d[0] = d[3] = lcn;
1237         d[2] = 0x10;
1238         d[4] = 0x13;
1239         d[5] = cause;
1240         d[6] = diagn;
1241
1242         return x25_exec(card, X25_DISCONNECT_REQUEST, link, d, 7, NULL, 0);
1243 }
1244
1245 /* Send X.25 data packet. */
1246 static int x25_send (cycx_t *card, u8 link, u8 lcn, u8 bitm, int len, void *buf)
1247 {
1248         u8 d[] = "?\xFF\x10??"; 
1249
1250         d[0] = d[3] = lcn;
1251         d[4] = bitm;
1252
1253         return x25_exec(card, X25_DATA_REQUEST, link, &d, 5, buf, len);
1254 }
1255
1256 /* Miscellaneous */
1257 /* Find network device by its channel number.  */
1258 static struct net_device *get_dev_by_lcn (wan_device_t *wandev, s16 lcn)
1259 {
1260         struct net_device *dev = wandev->dev;
1261         x25_channel_t *chan;
1262
1263         while (dev) {
1264                 chan = (x25_channel_t*)dev->priv;
1265
1266                 if (chan->lcn == lcn)
1267                         break;
1268                 dev = chan->slave;
1269         }       
1270         return dev;
1271 }
1272
1273 /* Find network device by its remote dte address. */
1274 static struct net_device *get_dev_by_dte_addr (wan_device_t *wandev, char *dte)
1275 {
1276         struct net_device *dev = wandev->dev;
1277         x25_channel_t *chan;
1278
1279         while (dev) {
1280                 chan = (x25_channel_t*)dev->priv;
1281
1282                 if (!strcmp(chan->addr, dte))
1283                         break;
1284                 dev = chan->slave;
1285         }
1286         return dev;
1287 }
1288
1289 /* Initiate connection on the logical channel.
1290  * o for PVC we just get channel configuration
1291  * o for SVCs place an X.25 call
1292  *
1293  * Return:      0       connected
1294  *              >0      connection in progress
1295  *              <0      failure */
1296 static int chan_connect (struct net_device *dev)
1297 {
1298         x25_channel_t *chan = dev->priv;
1299         cycx_t *card = chan->card;
1300
1301         if (chan->svc) {
1302                 if (!chan->addr[0])
1303                         return -EINVAL; /* no destination address */
1304
1305                 dprintk(1, KERN_INFO "%s: placing X.25 call to %s...\n",
1306                                   card->devname, chan->addr);
1307
1308                 if (x25_place_call(card, chan))
1309                         return -EIO;
1310
1311                 set_chan_state(dev, WAN_CONNECTING);
1312                 return 1;
1313         } else 
1314                 set_chan_state(dev, WAN_CONNECTED);
1315
1316         return 0;
1317 }
1318
1319 /* Disconnect logical channel.
1320  * o if SVC then clear X.25 call */
1321 static void chan_disconnect (struct net_device *dev)
1322 {
1323         x25_channel_t *chan = dev->priv;
1324
1325         if (chan->svc) {
1326                 x25_clear_call(chan->card, chan->link, chan->lcn, 0, 0);
1327                 set_chan_state(dev, WAN_DISCONNECTING);
1328         } else
1329                 set_chan_state(dev, WAN_DISCONNECTED);
1330 }
1331
1332 /* Called by kernel timer */
1333 static void chan_timer (unsigned long d)
1334 {
1335         struct net_device *dev = (struct net_device *)d;
1336         x25_channel_t *chan = dev->priv;
1337         
1338         if (chan->state == WAN_CONNECTED)
1339                 chan_disconnect(dev);
1340         else
1341                 printk(KERN_ERR "%s: chan_timer for svc (%s) not connected!\n",
1342                                 chan->card->devname, dev->name);
1343 }
1344
1345 /* Set logical channel state. */
1346 static void set_chan_state (struct net_device *dev, u8 state)
1347 {
1348         x25_channel_t *chan = dev->priv;
1349         cycx_t *card = chan->card;
1350         long flags;
1351         char *string_state = NULL;
1352
1353         spin_lock_irqsave(&card->lock, flags);
1354
1355         if (chan->state != state) {
1356                 if (chan->svc && chan->state == WAN_CONNECTED)
1357                         del_timer(&chan->timer);
1358         
1359                 switch (state) {
1360                         case WAN_CONNECTED:
1361                                 string_state = "connected!";
1362                                 *(u16*)dev->dev_addr = htons(chan->lcn);
1363                                 netif_wake_queue(dev);
1364                                 reset_timer(dev);
1365
1366                                 if (chan->protocol == ETH_P_X25)
1367                                         chan_x25_send_event(dev, 1);
1368
1369                                 break;
1370
1371                         case WAN_CONNECTING:
1372                                 string_state = "connecting...";
1373                                 break;
1374
1375                         case WAN_DISCONNECTING:
1376                                 string_state = "disconnecting...";
1377                                 break;
1378
1379                         case WAN_DISCONNECTED:
1380                                 string_state = "disconnected!";
1381                                 
1382                                 if (chan->svc) {
1383                                         *(unsigned short*)dev->dev_addr = 0;
1384                                         chan->lcn = 0;
1385                                 }
1386
1387                                 if (chan->protocol == ETH_P_X25)
1388                                         chan_x25_send_event(dev, 2);
1389
1390                                 netif_wake_queue(dev);
1391                                 break;
1392                 }
1393
1394                 printk (KERN_INFO "%s: interface %s %s\n", card->devname,
1395                                   dev->name, string_state);
1396                 chan->state = state;
1397         }
1398
1399         spin_unlock_irqrestore(&card->lock, flags);
1400 }
1401
1402 /* Send packet on a logical channel.
1403  *      When this function is called, tx_skb field of the channel data space
1404  *      points to the transmit socket buffer.  When transmission is complete,
1405  *      release socket buffer and reset 'tbusy' flag.
1406  *
1407  * Return:      0       - transmission complete
1408  *              1       - busy
1409  *
1410  * Notes:
1411  * 1. If packet length is greater than MTU for this channel, we'll fragment
1412  *    the packet into 'complete sequence' using M-bit.
1413  * 2. When transmission is complete, an event notification should be issued
1414  *    to the router.  */
1415 static int chan_send (struct net_device *dev, struct sk_buff *skb)
1416 {
1417         x25_channel_t *chan = dev->priv;
1418         cycx_t *card = chan->card;
1419         int bitm = 0;           /* final packet */
1420         unsigned len = skb->len;
1421
1422         if (skb->len > card->wandev.mtu) {
1423                 len = card->wandev.mtu;
1424                 bitm = 0x10;            /* set M-bit (more data) */
1425         }
1426
1427         if (x25_send(card, chan->link, chan->lcn, bitm, len, skb->data))
1428                 return 1;
1429                 
1430         if (bitm) {
1431                 skb_pull(skb, len);
1432                 return 1;
1433         }
1434
1435         ++chan->ifstats.tx_packets;
1436         chan->ifstats.tx_bytes += len;
1437
1438         return 0;
1439 }
1440
1441 /* Send event (connection, disconnection, etc) to X.25 socket layer */
1442
1443 static void chan_x25_send_event(struct net_device *dev, u8 event)
1444 {
1445         struct sk_buff *skb;
1446         unsigned char *ptr;
1447
1448         if ((skb = dev_alloc_skb(1)) == NULL) {
1449                 printk(KERN_ERR __FUNCTION__ ": out of memory\n");
1450                 return;
1451         }
1452
1453         ptr  = skb_put(skb, 1);
1454         *ptr = event;
1455
1456         skb->dev = dev;
1457         skb->protocol = htons(ETH_P_X25);
1458         skb->mac.raw = skb->data;
1459         skb->pkt_type = PACKET_HOST;
1460
1461         netif_rx(skb);
1462         dev->last_rx = jiffies;         /* timestamp */
1463 }
1464
1465 /* Convert line speed in bps to a number used by cyclom 2x code. */
1466 static u8 bps_to_speed_code (u32 bps)
1467 {
1468         u8 number = 0; /* defaults to the lowest (1200) speed ;> */
1469
1470              if (bps >= 512000) number = 8;
1471         else if (bps >= 256000) number = 7;
1472         else if (bps >= 64000)  number = 6;
1473         else if (bps >= 38400)  number = 5;
1474         else if (bps >= 19200)  number = 4;
1475         else if (bps >= 9600)   number = 3;
1476         else if (bps >= 4800)   number = 2;
1477         else if (bps >= 2400)   number = 1;
1478
1479         return number;
1480 }
1481
1482 /* log base 2 */
1483 static u8 log2 (u32 n)
1484 {
1485         u8 log = 0;
1486
1487         if (!n)
1488                 return 0;
1489
1490         while (n > 1) {
1491                 n >>= 1;
1492                 ++log;
1493         }
1494
1495         return log;
1496 }
1497
1498 /* Convert decimal string to unsigned integer.
1499  * If len != 0 then only 'len' characters of the string are converted. */
1500 static unsigned dec_to_uint (u8 *str, int len)
1501 {
1502         unsigned val = 0;
1503
1504         if (!len)
1505                 len = strlen(str);
1506
1507         for (; len && is_digit(*str); ++str, --len)
1508                 val = (val * 10) + (*str - (unsigned) '0');
1509
1510         return val;
1511 }
1512
1513 static void reset_timer(struct net_device *dev)
1514 {
1515         x25_channel_t *chan = dev->priv;
1516
1517         if (chan->svc)
1518                 mod_timer(&chan->timer, jiffies+chan->idle_tmout*HZ);
1519 }
1520 #ifdef CYCLOMX_X25_DEBUG
1521 static void x25_dump_config(TX25Config *conf)
1522 {
1523         printk(KERN_INFO "X.25 configuration\n");
1524         printk(KERN_INFO "-----------------\n");
1525         printk(KERN_INFO "link number=%d\n", conf->link);
1526         printk(KERN_INFO "line speed=%d\n", conf->speed);
1527         printk(KERN_INFO "clock=%sternal\n", conf->clock == 8 ? "Ex" : "In");
1528         printk(KERN_INFO "# level 2 retransm.=%d\n", conf->n2);
1529         printk(KERN_INFO "level 2 window=%d\n", conf->n2win);
1530         printk(KERN_INFO "level 3 window=%d\n", conf->n3win);
1531         printk(KERN_INFO "# logical channels=%d\n", conf->nvc);
1532         printk(KERN_INFO "level 3 pkt len=%d\n", conf->pktlen);
1533         printk(KERN_INFO "my address=%d\n", conf->locaddr);
1534         printk(KERN_INFO "remote address=%d\n", conf->remaddr);
1535         printk(KERN_INFO "t1=%d seconds\n", conf->t1);
1536         printk(KERN_INFO "t2=%d seconds\n", conf->t2);
1537         printk(KERN_INFO "t21=%d seconds\n", conf->t21);
1538         printk(KERN_INFO "# PVCs=%d\n", conf->npvc);
1539         printk(KERN_INFO "t23=%d seconds\n", conf->t23);
1540         printk(KERN_INFO "flags=0x%x\n", conf->flags);
1541 }
1542
1543 static void x25_dump_stats(TX25Stats *stats)
1544 {
1545         printk(KERN_INFO "X.25 statistics\n");
1546         printk(KERN_INFO "--------------\n");
1547         printk(KERN_INFO "rx_crc_errors=%d\n", stats->rx_crc_errors);
1548         printk(KERN_INFO "rx_over_errors=%d\n", stats->rx_over_errors);
1549         printk(KERN_INFO "n2_tx_frames=%d\n", stats->n2_tx_frames);
1550         printk(KERN_INFO "n2_rx_frames=%d\n", stats->n2_rx_frames);
1551         printk(KERN_INFO "tx_timeouts=%d\n", stats->tx_timeouts);
1552         printk(KERN_INFO "rx_timeouts=%d\n", stats->rx_timeouts);
1553         printk(KERN_INFO "n3_tx_packets=%d\n", stats->n3_tx_packets);
1554         printk(KERN_INFO "n3_rx_packets=%d\n", stats->n3_rx_packets);
1555         printk(KERN_INFO "tx_aborts=%d\n", stats->tx_aborts);
1556         printk(KERN_INFO "rx_aborts=%d\n", stats->rx_aborts);
1557 }
1558
1559 static void x25_dump_devs(wan_device_t *wandev)
1560 {
1561         struct net_device *dev = wandev->dev;
1562
1563         printk(KERN_INFO "X.25 dev states\n");
1564         printk(KERN_INFO "name: addr:           txoff:  protocol:\n");
1565         printk(KERN_INFO "---------------------------------------\n");
1566
1567         while(dev) {
1568                 x25_channel_t *chan = dev->priv;
1569
1570                 printk(KERN_INFO "%-5.5s %-15.15s   %d     ETH_P_%s\n",
1571                                  chan->name, chan->addr, netif_queue_stopped(dev),
1572                                  chan->protocol == ETH_P_IP ? "IP" : "X25");
1573                 dev = chan->slave;
1574         }
1575 }
1576
1577 #endif /* CYCLOMX_X25_DEBUG */
1578 /* End */