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