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