* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / wan / sdla_fr.c
blob6b2201a81b4e1e15ea93a79046660d513fa016d5
1 /*****************************************************************************
2 * sdla_fr.c WANPIPE(tm) Multiprotocol WAN Link Driver. Frame relay module.
4 * Author(s): Gene Kozin
5 * Jaspreet Singh <jaspreet@sangoma.com>
7 * Copyright: (c) 1995-1997 Sangoma Technologies Inc.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 * ============================================================================
14 * Nov 26, 1997 Jaspreet Singh o Improved load sharing with multiple boards
15 * o Added Cli() to protect enabling of interrupts
16 * while polling is called.
17 * Nov 24, 1997 Jaspreet Singh o Added counters to avoid enabling of interrupts
18 * when they have been disabled by another
19 * interface or routine (eg. wpf_poll).
20 * Nov 06, 1997 Jaspreet Singh o Added INTR_TEST_MODE to avoid polling
21 * routine disable interrupts during interrupt
22 * testing.
23 * Oct 20, 1997 Jaspreet Singh o Added hooks in for Router UP time.
24 * Oct 16, 1997 Jaspreet Singh o The critical flag is used to maintain flow
25 * control by avoiding RACE conditions. The
26 * cli() and restore_flags() are taken out.
27 * The fr_channel structure is appended for
28 * Driver Statistics.
29 * Oct 15, 1997 Farhan Thawar o updated if_send() and receive for IPX
30 * Aug 29, 1997 Farhan Thawar o Removed most of the cli() and sti()
31 * o Abstracted the UDP management stuff
32 * o Now use tbusy and critical more intelligently
33 * Jul 21, 1997 Jaspreet Singh o Can configure T391, T392, N391, N392 & N393
34 * through router.conf.
35 * o Protected calls to sdla_peek() by adDing
36 * save_flags(), cli() and restore_flags().
37 * o Added error message for Inactive DLCIs in
38 * fr_event() and update_chan_state().
39 * o Fixed freeing up of buffers using kfree()
40 * when packets are received.
41 * Jul 07, 1997 Jaspreet Singh o Added configurable TTL for UDP packets
42 * o Added ability to discard multicast and
43 * broadcast source addressed packets
44 * Jun 27, 1997 Jaspreet Singh o Added FT1 monitor capabilities
45 * New case (0x44) statement in if_send routine * Added a global variable rCount to keep track
46 * of FT1 status enabled on the board.
47 * May 29, 1997 Jaspreet Singh o Fixed major Flow Control Problem
48 * With multiple boards a problem was seen where
49 * the second board always stopped transmitting
50 * packet after running for a while. The code
51 * got into a stage where the interrupts were
52 * disabled and dev->tbusy was set to 1.
53 * This caused the If_send() routine to get into* the if clause for it(0,dev->tbusy)
54 * forever.
55 * The code got into this stage due to an
56 * interrupt occurring within the if clause for
57 * set_bit(0,dev->tbusy). Since an interrupt
58 * disables furhter transmit interrupt and
59 * makes dev->tbusy = 0, this effect was undone * by making dev->tbusy = 1 in the if clause.
60 * The Fix checks to see if Transmit interrupts
61 * are disabled then do not make dev->tbusy = 1
62 * Introduced a global variable: int_occur and
63 * added tx_int_enabled in the wan_device
64 * structure.
65 * May 21, 1997 Jaspreet Singh o Fixed UDP Management for multiple
66 * boards.
68 * Apr 25, 1997 Farhan Thawar o added UDP Management stuff
69 * o fixed bug in if_send() and tx_intr() to
70 * sleep and wakeup all devices
71 * Mar 11, 1997 Farhan Thawar Version 3.1.1
72 * o fixed (+1) bug in fr508_rx_intr()
73 * o changed if_send() to return 0 if
74 * wandev.critical() is true
75 * o free socket buffer in if_send() if
76 * returning 0
77 * o added tx_intr() routine
78 * Jan 30, 1997 Gene Kozin Version 3.1.0
79 * o implemented exec() entry point
80 * o fixed a bug causing driver configured as
81 * a FR switch to be stuck in WAN_
82 * mode
83 * Jan 02, 1997 Gene Kozin Initial version.
84 *****************************************************************************/
86 #include <linux/kernel.h> /* printk(), and other useful stuff */
87 #include <linux/stddef.h> /* offsetof(), etc. */
88 #include <linux/errno.h> /* return codes */
89 #include <linux/string.h> /* inline memset(), etc. */
90 #include <linux/malloc.h> /* kmalloc(), kfree() */
91 #include <linux/wanrouter.h> /* WAN router definitions */
92 #include <linux/wanpipe.h> /* WANPIPE common user API definitions */
93 #include <linux/if_arp.h> /* ARPHRD_* defines */
94 #include <asm/byteorder.h> /* htons(), etc. */
95 #include <asm/io.h> /* for inb(), outb(), etc. */
96 #include <linux/time.h> /* for do_gettimeofday */
97 #define _GNUC_
98 #include <linux/sdla_fr.h> /* frame relay firmware API definitions */
99 #include <asm/uaccess.h>
101 /****** Defines & Macros ****************************************************/
103 #define MAX_CMD_RETRY 10 /* max number of firmware retries */
104 #define FR_HEADER_LEN 8 /* max encapsulation header size */
105 #define FR_CHANNEL_MTU 1500 /* unfragmented logical channel MTU */
107 /* Q.922 frame types */
109 #define Q922_UI 0x03 /* Unnumbered Info frame */
110 #define Q922_XID 0xAF /* ??? */
112 /* DLCI configured or not */
114 #define DLCI_NOT_CONFIGURED 0x00
115 #define DLCI_CONFIG_PENDING 0x01
116 #define DLCI_CONFIGURED 0x02
118 /* CIR enabled or not */
120 #define CIR_ENABLED 0x00
121 #define CIR_DISABLED 0x01
123 /* Interrupt mode for DLCI = 0 */
125 #define BUFFER_INTR_MODE 0x00
126 #define DLCI_LIST_INTR_MODE 0x01
128 /* Transmit Interrupt Status */
130 #define DISABLED 0x00
131 #define WAITING_TO_BE_ENABLED 0x01
133 /* For handle_IPXWAN() */
135 #define CVHexToAscii(b) (((unsigned char)(b) > (unsigned char)9) ? ((unsigned char)'A' + ((unsigned char)(b) - (unsigned char)10)) : ((unsigned char)'0' + (unsigned char)(b)))
137 /****** Data Structures *****************************************************/
139 /* This is an extention of the 'struct net_device' we create for each network
140 * interface to keep the rest of channel-specific data.
142 typedef struct fr_channel {
143 char name[WAN_IFNAME_SZ + 1]; /* interface name, ASCIIZ */
144 unsigned dlci_configured; /* check whether configured or not */
145 unsigned cir_status; /* check whether CIR enabled or not */
146 unsigned dlci; /* logical channel number */
147 unsigned cir; /* committed information rate */
148 unsigned bc; /* committed burst size */
149 unsigned be; /* excess burst size */
150 unsigned mc; /* multicast support on or off */
151 unsigned tx_int_status; /* Transmit Interrupt Status */
152 unsigned short pkt_length; /* Packet Length */
153 unsigned long router_start_time; /* Router start time in seconds */
154 unsigned long tick_counter; /* counter for transmit time out */
155 char dev_pending_devtint; /* interface pending dev_tint() */
156 char state; /* channel state */
157 void *dlci_int_interface; /* pointer to the DLCI Interface */
158 unsigned long IB_addr; /* physical address of Interface Byte */
159 unsigned long state_tick; /* time of the last state change */
160 sdla_t *card; /* -> owner */
161 struct net_device_stats ifstats; /* interface statistics */
162 unsigned long if_send_entry;
163 unsigned long if_send_skb_null;
164 unsigned long if_send_broadcast;
165 unsigned long if_send_multicast;
166 unsigned long if_send_critical_ISR;
167 unsigned long if_send_critical_non_ISR;
168 unsigned long if_send_busy;
169 unsigned long if_send_busy_timeout;
170 unsigned long if_send_FPIPE_request;
171 unsigned long if_send_DRVSTATS_request;
172 unsigned long if_send_wan_disconnected;
173 unsigned long if_send_dlci_disconnected;
174 unsigned long if_send_no_bfrs;
175 unsigned long if_send_adptr_bfrs_full;
176 unsigned long if_send_bfrs_passed_to_adptr;
177 unsigned long rx_intr_no_socket;
178 unsigned long rx_intr_dev_not_started;
179 unsigned long rx_intr_FPIPE_request;
180 unsigned long rx_intr_DRVSTATS_request;
181 unsigned long rx_intr_bfr_not_passed_to_stack;
182 unsigned long rx_intr_bfr_passed_to_stack;
183 unsigned long UDP_FPIPE_mgmt_kmalloc_err;
184 unsigned long UDP_FPIPE_mgmt_direction_err;
185 unsigned long UDP_FPIPE_mgmt_adptr_type_err;
186 unsigned long UDP_FPIPE_mgmt_adptr_cmnd_OK;
187 unsigned long UDP_FPIPE_mgmt_adptr_cmnd_timeout;
188 unsigned long UDP_FPIPE_mgmt_adptr_send_passed;
189 unsigned long UDP_FPIPE_mgmt_adptr_send_failed;
190 unsigned long UDP_FPIPE_mgmt_not_passed_to_stack;
191 unsigned long UDP_FPIPE_mgmt_passed_to_stack;
192 unsigned long UDP_FPIPE_mgmt_no_socket;
193 unsigned long UDP_DRVSTATS_mgmt_kmalloc_err;
194 unsigned long UDP_DRVSTATS_mgmt_adptr_cmnd_OK;
195 unsigned long UDP_DRVSTATS_mgmt_adptr_cmnd_timeout;
196 unsigned long UDP_DRVSTATS_mgmt_adptr_send_passed;
197 unsigned long UDP_DRVSTATS_mgmt_adptr_send_failed;
198 unsigned long UDP_DRVSTATS_mgmt_not_passed_to_stack;
199 unsigned long UDP_DRVSTATS_mgmt_passed_to_stack;
200 unsigned long UDP_DRVSTATS_mgmt_no_socket;
201 unsigned long router_up_time;
202 } fr_channel_t;
204 typedef struct dlci_status {
205 unsigned short dlci PACKED;
206 unsigned char state PACKED;
207 } dlci_status_t;
209 typedef struct dlci_IB_mapping {
210 unsigned short dlci PACKED;
211 unsigned long addr_value PACKED;
212 } dlci_IB_mapping_t;
214 /* This structure is used for DLCI list Tx interrupt mode. It is used to
215 enable interrupt bit and set the packet length for transmission
218 typedef struct fr_dlci_interface {
219 unsigned char gen_interrupt PACKED;
220 unsigned short packet_length PACKED;
221 unsigned char reserved PACKED;
222 } fr_dlci_interface_t;
224 static unsigned short num_frames;
225 static unsigned long curr_trace_addr;
226 static unsigned long start_trace_addr;
227 static unsigned short available_buffer_space;
228 static char TracingEnabled; /* variable for keeping track of enabling/disabling FT1 monitor status */
229 static int rCount = 0;
230 extern void disable_irq(unsigned int);
231 extern void enable_irq(unsigned int);
233 /* variable for keeping track of number of interrupts generated during
234 * interrupt test routine
236 static int Intr_test_counter;
238 /****** Function Prototypes *************************************************/
240 /* WAN link driver entry points. These are called by the WAN router module. */
241 static int update(wan_device_t * wandev);
242 static int new_if(wan_device_t * wandev, struct net_device *dev,
243 wanif_conf_t * conf);
244 static int del_if(wan_device_t * wandev, struct net_device *dev);
245 /* WANPIPE-specific entry points */
246 static int wpf_exec(struct sdla *card, void *u_cmd, void *u_data);
247 /* Network device interface */
248 static int if_init(struct net_device *dev);
249 static int if_open(struct net_device *dev);
250 static int if_close(struct net_device *dev);
251 static int if_header(struct sk_buff *skb, struct net_device *dev,
252 unsigned short type, void *daddr, void *saddr, unsigned len);
253 static int if_rebuild_hdr(struct sk_buff *skb);
254 static int if_send(struct sk_buff *skb, struct net_device *dev);
255 static struct net_device_stats *if_stats(struct net_device *dev);
256 /* Interrupt handlers */
257 static void fr502_isr(sdla_t * card);
258 static void fr508_isr(sdla_t * card);
259 static void fr502_rx_intr(sdla_t * card);
260 static void fr508_rx_intr(sdla_t * card);
261 static void tx_intr(sdla_t * card);
262 static void spur_intr(sdla_t * card);
263 /* Background polling routines */
264 static void wpf_poll(sdla_t * card);
265 /* Frame relay firmware interface functions */
266 static int fr_read_version(sdla_t * card, char *str);
267 static int fr_configure(sdla_t * card, fr_conf_t * conf);
268 static int fr_dlci_configure(sdla_t * card, fr_dlc_conf_t * conf, unsigned dlci);
269 static int fr_set_intr_mode(sdla_t * card, unsigned mode, unsigned mtu);
270 static int fr_comm_enable(sdla_t * card);
271 static int fr_comm_disable(sdla_t * card);
272 static int fr_get_err_stats(sdla_t * card);
273 static int fr_get_stats(sdla_t * card);
274 static int fr_add_dlci(sdla_t * card, int dlci, int num);
275 static int fr_activate_dlci(sdla_t * card, int dlci, int num);
276 static int fr_issue_isf(sdla_t * card, int isf);
277 static int fr502_send(sdla_t * card, int dlci, int attr, int len, void *buf);
278 static int fr508_send(sdla_t * card, int dlci, int attr, int len, void *buf);
279 /* Firmware asynchronous event handlers */
280 static int fr_event(sdla_t * card, int event, fr_mbox_t * mbox);
281 static int fr_modem_failure(sdla_t * card, fr_mbox_t * mbox);
282 static int fr_dlci_change(sdla_t * card, fr_mbox_t * mbox);
283 /* Miscellaneous functions */
284 static int update_chan_state(struct net_device *dev);
285 static void set_chan_state(struct net_device *dev, int state);
286 static struct net_device *find_channel(sdla_t * card, unsigned dlci);
287 static int is_tx_ready(sdla_t * card, fr_channel_t * chan);
288 static unsigned int dec_to_uint(unsigned char *str, int len);
289 static int reply_udp(unsigned char *data, unsigned int mbox_len);
290 static int intr_test(sdla_t * card);
291 static void init_chan_statistics(fr_channel_t * chan);
292 static void init_global_statistics(sdla_t * card);
293 static void read_DLCI_IB_mapping(sdla_t * card, fr_channel_t * chan);
294 /* Udp management functions */
295 static int process_udp_mgmt_pkt(char udp_pkt_src, sdla_t * card, struct sk_buff *skb, struct net_device *dev, int dlci, fr_channel_t * chan);
296 static int process_udp_driver_call(char udp_pkt_src, sdla_t * card, struct sk_buff *skb, struct net_device *dev, int dlci, fr_channel_t * chan);
297 static int udp_pkt_type(struct sk_buff *skb, sdla_t * card);
298 /* IPX functions */
299 static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming);
300 static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_IPX, unsigned long network_number);
302 /****** Public Functions ****************************************************/
304 /*============================================================================
305 * Frame relay protocol initialization routine.
307 * This routine is called by the main WANPIPE module during setup. At this
308 * point adapter is completely initialized and firmware is running.
309 * o read firmware version (to make sure it's alive)
310 * o configure adapter
311 * o initialize protocol-specific fields of the adapter data space.
313 * Return: 0 o.k.
314 * < 0 failure.
316 int wpf_init(sdla_t * card, wandev_conf_t * conf)
318 union {
319 char str[80];
320 fr_conf_t cfg;
321 } u;
322 int i;
323 /* Verify configuration ID */
324 if (conf->config_id != WANCONFIG_FR)
326 printk(KERN_INFO "%s: invalid configuration ID %u!\n",
327 card->devname, conf->config_id);
328 return -EINVAL;
330 /* Initialize protocol-specific fields of adapter data space */
331 switch (card->hw.fwid)
333 case SFID_FR502:
334 card->mbox = (void *) (card->hw.dpmbase + FR502_MBOX_OFFS);
335 card->rxmb = (void *) (card->hw.dpmbase + FR502_RXMB_OFFS);
336 card->flags = (void *) (card->hw.dpmbase + FR502_FLAG_OFFS);
337 card->isr = &fr502_isr;
338 break;
339 case SFID_FR508:
340 card->mbox = (void *) (card->hw.dpmbase + FR508_MBOX_OFFS);
341 card->flags = (void *) (card->hw.dpmbase + FR508_FLAG_OFFS);
342 card->isr = &fr508_isr;
343 break;
344 default:
345 return -EINVAL;
347 /* Read firmware version. Note that when adapter initializes, it
348 * clears the mailbox, so it may appear that the first command was
349 * executed successfully when in fact it was merely erased. To work
350 * around this, we execute the first command twice.
352 if (fr_read_version(card, NULL) || fr_read_version(card, u.str))
353 return -EIO;
354 printk(KERN_INFO "%s: running frame relay firmware v%s\n",
355 card->devname, u.str);
356 /* Adjust configuration */
357 conf->mtu = max(min(conf->mtu, 4080), FR_CHANNEL_MTU + FR_HEADER_LEN);
358 conf->bps = min(conf->bps, 2048000);
359 /* Configure adapter firmware */
360 memset(&u.cfg, 0, sizeof(u.cfg));
361 u.cfg.mtu = conf->mtu;
362 u.cfg.kbps = conf->bps / 1000;
363 u.cfg.cir_fwd = u.cfg.cir_bwd = 16;
364 u.cfg.bc_fwd = u.cfg.bc_bwd = 16;
365 if (conf->station == WANOPT_CPE)
367 u.cfg.options = 0x0080;
368 printk(KERN_INFO "%s: Global CIR enabled by Default\n", card->devname);
370 else
372 u.cfg.options = 0x0081;
374 switch (conf->u.fr.signalling)
376 case WANOPT_FR_Q933:
377 u.cfg.options |= 0x0200;
378 break;
379 case WANOPT_FR_LMI:
380 u.cfg.options |= 0x0400;
381 break;
383 if (conf->station == WANOPT_CPE)
385 u.cfg.options |= 0x8000; /* auto config DLCI */
386 card->u.f.dlci_num = 0;
388 else
390 u.cfg.station = 1; /* switch emulation mode */
391 /* For switch emulation we have to create a list of dlci(s)
392 * that will be sent to be global SET_DLCI_CONFIGURATION
393 * command in fr_configure() routine.
395 card->u.f.dlci_num = min(max(conf->u.fr.dlci_num, 1), 100);
396 for (i = 0; i < card->u.f.dlci_num; i++)
398 card->u.f.node_dlci[i] = (unsigned short)
399 conf->u.fr.dlci[i] ? conf->u.fr.dlci[i] : 16;
402 if (conf->clocking == WANOPT_INTERNAL)
403 u.cfg.port |= 0x0001;
404 if (conf->interface == WANOPT_RS232)
405 u.cfg.port |= 0x0002;
406 if (conf->u.fr.t391)
407 u.cfg.t391 = min(conf->u.fr.t391, 30);
408 else
409 u.cfg.t391 = 5;
410 if (conf->u.fr.t392)
411 u.cfg.t392 = min(conf->u.fr.t392, 30);
412 else
413 u.cfg.t392 = 15;
414 if (conf->u.fr.n391)
415 u.cfg.n391 = min(conf->u.fr.n391, 255);
416 else
417 u.cfg.n391 = 2;
418 if (conf->u.fr.n392)
419 u.cfg.n392 = min(conf->u.fr.n392, 10);
420 else
421 u.cfg.n392 = 3;
422 if (conf->u.fr.n393)
423 u.cfg.n393 = min(conf->u.fr.n393, 10);
424 else
425 u.cfg.n393 = 4;
426 if (fr_configure(card, &u.cfg))
427 return -EIO;
428 if (card->hw.fwid == SFID_FR508)
430 fr_buf_info_t *buf_info =
431 (void *) (card->hw.dpmbase + FR508_RXBC_OFFS);
432 card->rxmb = (void *) (buf_info->rse_next - FR_MB_VECTOR + card->hw.dpmbase);
433 card->u.f.rxmb_base = (void *) (buf_info->rse_base - FR_MB_VECTOR + card->hw.dpmbase);
434 card->u.f.rxmb_last = (void *) (buf_info->rse_base + (buf_info->rse_num - 1) *
435 sizeof(fr_buf_ctl_t) - FR_MB_VECTOR + card->hw.dpmbase);
436 card->u.f.rx_base = buf_info->buf_base;
437 card->u.f.rx_top = buf_info->buf_top;
439 card->wandev.mtu = conf->mtu;
440 card->wandev.bps = conf->bps;
441 card->wandev.interface = conf->interface;
442 card->wandev.clocking = conf->clocking;
443 card->wandev.station = conf->station;
444 card->poll = &wpf_poll;
445 card->exec = &wpf_exec;
446 card->wandev.update = &update;
447 card->wandev.new_if = &new_if;
448 card->wandev.del_if = &del_if;
449 card->wandev.state = WAN_DISCONNECTED;
450 card->wandev.ttl = conf->ttl;
451 card->wandev.udp_port = conf->udp_port;
452 card->wandev.enable_tx_int = 0;
453 card->irq_dis_if_send_count = 0;
454 card->irq_dis_poll_count = 0;
455 card->wandev.enable_IPX = conf->enable_IPX;
456 if (conf->network_number)
457 card->wandev.network_number = conf->network_number;
458 else
459 card->wandev.network_number = 0xDEADBEEF;
460 /* Intialize global statistics for a card */
461 init_global_statistics(card);
462 TracingEnabled = 0;
463 return 0;
466 /******* WAN Device Driver Entry Points *************************************/
468 /*============================================================================
469 * Update device status & statistics.
472 static int update(wan_device_t * wandev)
474 sdla_t *card;
475 /* sanity checks */
476 if ((wandev == NULL) || (wandev->private == NULL))
477 return -EFAULT;
478 if (wandev->state == WAN_UNCONFIGURED)
479 return -ENODEV;
480 if (test_and_set_bit(0, (void *) &wandev->critical))
481 return -EAGAIN;
482 card = wandev->private;
483 fr_get_err_stats(card);
484 fr_get_stats(card);
485 wandev->critical = 0;
486 return 0;
489 /*============================================================================
490 * Create new logical channel.
491 * This routine is called by the router when ROUTER_IFNEW IOCTL is being
492 * handled.
493 * o parse media- and hardware-specific configuration
494 * o make sure that a new channel can be created
495 * o allocate resources, if necessary
496 * o prepare network device structure for registaration.
498 * Return: 0 o.k.
499 * < 0 failure (channel will not be created)
502 static int new_if(wan_device_t * wandev, struct net_device *dev, wanif_conf_t * conf)
504 sdla_t *card = wandev->private;
505 fr_channel_t *chan;
506 int err = 0;
507 if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ))
509 printk(KERN_INFO "%s: invalid interface name!\n",
510 card->devname);
511 return -EINVAL;
513 /* allocate and initialize private data */
514 chan = kmalloc(sizeof(fr_channel_t), GFP_KERNEL);
515 if (chan == NULL)
516 return -ENOMEM;
517 memset(chan, 0, sizeof(fr_channel_t));
518 strcpy(chan->name, conf->name);
519 chan->card = card;
520 /* verify media address */
521 if (is_digit(conf->addr[0]))
523 int dlci = dec_to_uint(conf->addr, 0);
524 if (dlci && (dlci <= 4095))
526 chan->dlci = dlci;
528 else
530 printk(KERN_ERR "%s: invalid DLCI %u on interface %s!\n",
531 wandev->name, dlci, chan->name);
532 err = -EINVAL;
535 else
537 printk(KERN_ERR "%s: invalid media address on interface %s!\n",
538 wandev->name, chan->name);
539 err = -EINVAL;
541 if (err)
543 kfree(chan);
544 return err;
546 /* place cir,be,bc and other channel specific information into the
547 * chan structure
549 if (conf->cir)
551 chan->cir = max(1, min(conf->cir, 512));
552 chan->cir_status = CIR_ENABLED;
553 if (conf->bc)
554 chan->bc = max(1, min(conf->bc, 512));
555 if (conf->be)
556 chan->be = max(0, min(conf->be, 511));
558 else
559 chan->cir_status = CIR_DISABLED;
560 chan->mc = conf->mc;
561 chan->dlci_configured = DLCI_NOT_CONFIGURED;
562 chan->tx_int_status = DISABLED;
563 init_chan_statistics(chan);
564 /* prepare network device data space for registration */
565 dev->name = chan->name;
566 dev->init = &if_init;
567 dev->priv = chan;
568 return 0;
570 /*============================================================================
571 * Delete logical channel.
573 static int del_if(wan_device_t * wandev, struct net_device *dev)
575 if (dev->priv)
577 kfree(dev->priv);
578 dev->priv = NULL;
580 return 0;
583 /****** WANPIPE-specific entry points ***************************************/
585 /*============================================================================
586 * Execute adapter interface command.
588 static int wpf_exec(struct sdla *card, void *u_cmd, void *u_data)
590 fr_mbox_t *mbox = card->mbox;
591 int retry = MAX_CMD_RETRY;
592 int err, len;
593 fr_cmd_t cmd;
594 if(copy_from_user((void *) &cmd, u_cmd, sizeof(cmd)))
595 return -EFAULT;
596 /* execute command */
599 memcpy(&mbox->cmd, &cmd, sizeof(cmd));
600 if (cmd.length)
602 if(copy_from_user((void *) &mbox->data, u_data, cmd.length))
603 return -EFAULT;
605 if (sdla_exec(mbox))
606 err = mbox->cmd.result;
607 else
608 return -EIO;
610 while (err && retry-- && fr_event(card, err, mbox));
612 /* return result */
614 if(copy_to_user(u_cmd, (void *) &mbox->cmd, sizeof(fr_cmd_t)))
615 return -EFAULT;
616 len = mbox->cmd.length;
617 if (len && u_data && copy_to_user(u_data, (void *) &mbox->data, len))
618 return -EFAULT;
619 return 0;
622 /****** Network Device Interface ********************************************/
623 /*============================================================================
624 * Initialize Linux network interface.
626 * This routine is called only once for each interface, during Linux network
627 * interface registration. Returning anything but zero will fail interface
628 * registration.
630 static int if_init(struct net_device *dev)
632 fr_channel_t *chan = dev->priv;
633 sdla_t *card = chan->card;
634 wan_device_t *wandev = &card->wandev;
636 /* Initialize device driver entry points */
637 dev->open = &if_open;
638 dev->stop = &if_close;
639 dev->hard_header = &if_header;
640 dev->rebuild_header = &if_rebuild_hdr;
641 dev->hard_start_xmit = &if_send;
642 dev->get_stats = &if_stats;
643 /* Initialize media-specific parameters */
644 dev->type = ARPHRD_DLCI; /* ARP h/w type */
645 dev->mtu = FR_CHANNEL_MTU;
646 dev->hard_header_len = FR_HEADER_LEN; /* media header length */
647 dev->addr_len = 2; /* hardware address length */
648 *(unsigned short *) dev->dev_addr = htons(chan->dlci);
649 /* Initialize hardware parameters (just for reference) */
650 dev->irq = wandev->irq;
651 dev->dma = wandev->dma;
652 dev->base_addr = wandev->ioport;
653 dev->mem_start = (unsigned long)wandev->maddr;
654 dev->mem_end = dev->mem_start + wandev->msize - 1;
655 /* Set transmit buffer queue length */
656 dev->tx_queue_len = 10;
657 /* Initialize socket buffers */
658 dev_init_buffers(dev);
659 set_chan_state(dev, WAN_DISCONNECTED);
660 return 0;
663 /*============================================================================
664 * Open network interface.
665 * o if this is the first open, then enable communications and interrupts.
666 * o prevent module from unloading by incrementing use count
668 * Return 0 if O.k. or errno.
671 static int if_open(struct net_device *dev)
673 fr_channel_t *chan = dev->priv;
674 sdla_t *card = chan->card;
675 struct net_device *dev2;
676 int err = 0;
677 fr508_flags_t *flags = card->flags;
678 struct timeval tv;
679 if (dev->start)
680 return -EBUSY; /* only one open is allowed */
681 if (test_and_set_bit(0, (void *) &card->wandev.critical))
682 return -EAGAIN;
683 if (!card->open_cnt)
685 Intr_test_counter = 0;
686 card->intr_mode = INTR_TEST_MODE;
687 err = intr_test(card);
688 if ((err) || (Intr_test_counter != (MAX_INTR_TEST_COUNTER + 1))) {
689 printk(KERN_INFO
690 "%s: Interrupt Test Failed, Counter: %i\n",
691 card->devname, Intr_test_counter);
692 err = -EIO;
693 card->wandev.critical = 0;
694 return err;
696 printk(KERN_INFO "%s: Interrupt Test Passed, Counter: %i\n"
697 ,card->devname, Intr_test_counter);
698 /* The following allocates and intializes a circular
699 * link list of interfaces per card.
701 card->devs_struct = kmalloc(sizeof(load_sharing_t), GFP_KERNEL);
702 if (card->devs_struct == NULL)
703 return -ENOMEM;
704 card->dev_to_devtint_next = card->devs_struct;
705 for (dev2 = card->wandev.dev; dev2; dev2 = dev2->slave) {
706 (card->devs_struct)->dev_ptr = dev2;
707 if (dev2->slave == NULL)
708 (card->devs_struct)->next = card->dev_to_devtint_next;
709 else {
710 (card->devs_struct)->next = kmalloc(
711 sizeof(load_sharing_t), GFP_KERNEL);
712 if ((card->devs_struct)->next == NULL)
713 return -ENOMEM;
714 card->devs_struct = (card->devs_struct)->next;
717 card->devs_struct = card->dev_to_devtint_next;
718 card->intr_mode = BUFFER_INTR_MODE;
720 check all the interfaces for the device to see if CIR has
721 been enabled for any DLCI(s). If so then use the DLCI list
722 Interrupt mode for fr_set_intr_mode(), otherwise use the default global interrupt mode
724 for (dev2 = card->wandev.dev; dev2; dev2 = dev2->slave) {
725 if (((fr_channel_t *) dev2->priv)->cir_status
726 == CIR_ENABLED) {
727 card->intr_mode = DLCI_LIST_INTR_MODE;
728 break;
732 If you enable comms and then set ints, you get a Tx int as you
733 perform the SET_INT_TRIGGERS command. So, we only set int
734 triggers and then adjust the interrupt mask (to disable Tx ints) before enabling comms.
736 if (card->intr_mode == BUFFER_INTR_MODE) {
737 if (fr_set_intr_mode(card, 0x03, card->wandev.mtu)) {
738 err = -EIO;
739 card->wandev.critical = 0;
740 return err;
742 printk(KERN_INFO
743 "%s: Global Buffering Tx Interrupt Mode\n"
744 ,card->devname);
745 } else if (card->intr_mode == DLCI_LIST_INTR_MODE) {
746 if (fr_set_intr_mode(card, 0x83, card->wandev.mtu)) {
747 err = -EIO;
748 card->wandev.critical = 0;
749 return err;
751 printk(KERN_INFO
752 "%s: DLCI list Tx Interrupt Mode\n",
753 card->devname);
755 flags->imask &= ~0x02;
756 if (fr_comm_enable(card)) {
757 err = -EIO;
758 card->wandev.critical = 0;
759 return err;
761 wanpipe_set_state(card, WAN_CONNECTED);
762 if (card->wandev.station == WANOPT_CPE) {
763 /* CPE: issue full status enquiry */
764 fr_issue_isf(card, FR_ISF_FSE);
765 } else { /* FR switch: activate DLCI(s) */
766 /* For Switch emulation we have to ADD and ACTIVATE
767 * the DLCI(s) that were configured with the SET_DLCI_
768 * CONFIGURATION command. Add and Activate will fail if
769 * DLCI specified is not included in the list.
771 * Also If_open is called once for each interface. But
772 * it does not get in here for all the interface. So
773 * we have to pass the entire list of DLCI(s) to add
774 * activate routines.
776 fr_add_dlci(card,
777 card->u.f.node_dlci[0], card->u.f.dlci_num);
778 fr_activate_dlci(card,
779 card->u.f.node_dlci[0], card->u.f.dlci_num);
782 dev->mtu = min(dev->mtu, card->wandev.mtu - FR_HEADER_LEN);
783 dev->interrupt = 0;
784 dev->tbusy = 0;
785 dev->start = 1;
786 wanpipe_open(card);
787 update_chan_state(dev);
788 do_gettimeofday(&tv);
789 chan->router_start_time = tv.tv_sec;
790 card->wandev.critical = 0;
791 return err;
794 /*============================================================================
795 * Close network interface.
796 * o if this is the last open, then disable communications and interrupts.
797 * o reset flags.
800 static int if_close(struct net_device *dev)
802 fr_channel_t *chan = dev->priv;
803 sdla_t *card = chan->card;
804 if (test_and_set_bit(0, (void *) &card->wandev.critical))
805 return -EAGAIN;
806 dev->start = 0;
807 wanpipe_close(card);
808 if (!card->open_cnt)
810 wanpipe_set_state(card, WAN_DISCONNECTED);
811 fr_set_intr_mode(card, 0, 0);
812 fr_comm_disable(card);
814 card->wandev.critical = 0;
815 return 0;
818 /*============================================================================
819 * Build media header.
820 * o encapsulate packet according to encapsulation type.
822 * The trick here is to put packet type (Ethertype) into 'protocol' field of
823 * the socket buffer, so that we don't forget it. If encapsulation fails,
824 * set skb->protocol to 0 and discard packet later.
826 * Return: media header length.
829 static int if_header(struct sk_buff *skb, struct net_device *dev,
830 unsigned short type, void *daddr, void *saddr, unsigned len)
832 int hdr_len = 0;
833 skb->protocol = type;
834 hdr_len = wanrouter_encapsulate(skb, dev);
835 if (hdr_len < 0)
837 hdr_len = 0;
838 skb->protocol = 0;
840 skb_push(skb, 1);
841 skb->data[0] = Q922_UI;
842 ++hdr_len;
843 return hdr_len;
846 /*============================================================================
847 * Re-build media header.
849 * Return: 1 physical address resolved.
850 * 0 physical address not resolved
853 static int if_rebuild_hdr(struct sk_buff *skb)
855 struct net_device *dev=skb->dev;
856 fr_channel_t *chan = dev->priv;
857 sdla_t *card = chan->card;
858 printk(KERN_INFO "%s: rebuild_header() called for interface %s!\n",
859 card->devname, dev->name);
860 return 1;
863 /*============================================================================
864 * Send a packet on a network interface.
865 * o set tbusy flag (marks start of the transmission) to block a timer-based
866 * transmit from overlapping.
867 * o check link state. If link is not up, then drop the packet.
868 * o check channel status. If it's down then initiate a call.
869 * o pass a packet to corresponding WAN device.
870 * o free socket buffer
872 * Return: 0 complete (socket buffer must be freed)
873 * non-0 packet may be re-transmitted (tbusy must be set)
875 * Notes:
876 * 1. This routine is called either by the protocol stack or by the "net
877 * bottom half" (with interrupts enabled).
878 * 2. Setting tbusy flag will inhibit further transmit requests from the
879 * protocol stack and can be used for flow control with protocol layer.
882 static int if_send(struct sk_buff *skb, struct net_device *dev)
884 fr_channel_t *chan = dev->priv;
885 sdla_t *card = chan->card;
886 int retry = 0, err;
887 unsigned char *sendpacket;
888 struct net_device *dev2;
889 unsigned long check_braddr, check_mcaddr;
890 fr508_flags_t *adptr_flags = card->flags;
891 int udp_type, send_data;
892 fr_dlci_interface_t *dlci_interface = chan->dlci_int_interface;
893 unsigned long host_cpu_flags;
894 ++chan->if_send_entry;
896 if (dev->tbusy)
898 /* If our device stays busy for at least 5 seconds then we will
899 * kick start the device by making dev->tbusy = 0. We expect
900 * that our device never stays busy more than 5 seconds. So this * is only used as a last resort.
902 ++chan->if_send_busy;
903 ++chan->ifstats.collisions;
904 if ((jiffies - chan->tick_counter) < (5 * HZ))
905 return 1;
907 printk(KERN_INFO "%s: Transmit timed out\n", chan->name);
908 ++chan->if_send_busy_timeout;
909 /* unbusy all the interfaces on the card */
910 for (dev2 = card->wandev.dev; dev2; dev2 = dev2->slave)
911 dev2->tbusy = 0;
913 sendpacket = skb->data;
914 udp_type = udp_pkt_type(skb, card);
915 if (udp_type == UDP_DRVSTATS_TYPE)
917 ++chan->if_send_DRVSTATS_request;
918 process_udp_driver_call(UDP_PKT_FRM_STACK, card, skb, dev, 0,
919 chan);
920 dev_kfree_skb(skb);
921 return 0;
923 else if (udp_type == UDP_FPIPE_TYPE)
924 ++chan->if_send_FPIPE_request;
925 /* retreive source address in two forms: broadcast & multicast */
926 check_braddr = sendpacket[17];
927 check_mcaddr = sendpacket[14];
928 check_braddr = check_braddr << 8;
929 check_mcaddr = check_mcaddr << 8;
930 check_braddr |= sendpacket[16];
931 check_mcaddr |= sendpacket[15];
932 check_braddr = check_braddr << 8;
933 check_mcaddr = check_mcaddr << 8;
934 check_braddr |= sendpacket[15];
935 check_mcaddr |= sendpacket[16];
936 check_braddr = check_braddr << 8;
937 check_mcaddr = check_mcaddr << 8;
938 check_braddr |= sendpacket[14];
939 check_mcaddr |= sendpacket[17];
940 /* if the Source Address is a Multicast address */
941 if ((chan->mc == WANOPT_NO) && (check_mcaddr >= 0xE0000001) &&
942 (check_mcaddr <= 0xFFFFFFFE))
944 printk(KERN_INFO "%s: Multicast Src. Addr. silently discarded\n"
945 ,card->devname);
946 dev_kfree_skb(skb);
947 ++chan->ifstats.tx_dropped;
948 ++chan->if_send_multicast;
949 return 0;
951 disable_irq(card->hw.irq);
952 ++card->irq_dis_if_send_count;
953 if (test_and_set_bit(0, (void *) &card->wandev.critical))
955 if (card->wandev.critical == CRITICAL_IN_ISR)
957 ++chan->if_send_critical_ISR;
958 if (card->intr_mode == DLCI_LIST_INTR_MODE)
960 /* The enable_tx_int flag is set here so that if
961 * the critical flag is set due to an interrupt
962 * then we want to enable transmit interrupts
963 * again.
965 card->wandev.enable_tx_int = 1;
966 /* Setting this flag to WAITING_TO_BE_ENABLED
967 * specifies that interrupt bit has to be
968 * enabled for that particular interface.
969 * (delayed interrupt)
971 chan->tx_int_status = WAITING_TO_BE_ENABLED;
972 /* This is used for enabling dynamic calculation
973 * of CIRs relative to the packet length.
975 chan->pkt_length = skb->len;
976 dev->tbusy = 1;
977 chan->tick_counter = jiffies;
979 else
981 card->wandev.enable_tx_int = 1;
982 dev->tbusy = 1;
983 chan->tick_counter = jiffies;
985 save_flags(host_cpu_flags);
986 cli();
987 if ((!(--card->irq_dis_if_send_count)) &&
988 (!card->irq_dis_poll_count))
989 enable_irq(card->hw.irq);
990 restore_flags(host_cpu_flags);
991 return 1;
993 ++chan->if_send_critical_non_ISR;
994 ++chan->ifstats.tx_dropped;
995 dev_kfree_skb(skb);
996 save_flags(host_cpu_flags);
997 cli();
998 if ((!(--card->irq_dis_if_send_count)) &&
999 (!card->irq_dis_poll_count))
1000 enable_irq(card->hw.irq);
1001 restore_flags(host_cpu_flags);
1002 return 0;
1004 card->wandev.critical = 0x21;
1005 if (udp_type == UDP_FPIPE_TYPE)
1007 err = process_udp_mgmt_pkt(UDP_PKT_FRM_STACK, card, skb,
1008 dev, 0, chan);
1010 else if (card->wandev.state != WAN_CONNECTED)
1012 ++chan->if_send_wan_disconnected;
1013 ++chan->ifstats.tx_dropped;
1014 ++card->wandev.stats.tx_dropped;
1016 else if (chan->state != WAN_CONNECTED)
1018 ++chan->if_send_dlci_disconnected;
1019 update_chan_state(dev);
1020 ++chan->ifstats.tx_dropped;
1021 ++card->wandev.stats.tx_dropped;
1023 else if (!is_tx_ready(card, chan))
1025 if (card->intr_mode == DLCI_LIST_INTR_MODE)
1027 dlci_interface->gen_interrupt |= 0x40;
1028 dlci_interface->packet_length = skb->len;
1030 dev->tbusy = 1;
1031 chan->tick_counter = jiffies;
1032 adptr_flags->imask |= 0x02;
1033 ++chan->if_send_no_bfrs;
1034 retry = 1;
1036 else
1038 send_data = 1;
1039 /* If it's an IPX packet */
1040 if (sendpacket[1] == 0x00 &&
1041 sendpacket[2] == 0x80 &&
1042 sendpacket[6] == 0x81 &&
1043 sendpacket[7] == 0x37)
1045 if (card->wandev.enable_IPX)
1047 switch_net_numbers(sendpacket,
1048 card->wandev.network_number, 0);
1050 else
1052 /* increment some statistic here! */
1053 send_data = 0;
1056 if (send_data)
1058 err = (card->hw.fwid == SFID_FR508) ?
1059 fr508_send(card, chan->dlci, 0, skb->len, skb->data) :
1060 fr502_send(card, chan->dlci, 0, skb->len, skb->data);
1061 if (err)
1063 if (card->intr_mode == DLCI_LIST_INTR_MODE)
1065 dlci_interface->gen_interrupt |= 0x40;
1066 dlci_interface->packet_length = skb->len;
1068 dev->tbusy = 1;
1069 chan->tick_counter = jiffies;
1070 adptr_flags->imask |= 0x02;
1071 retry = 1;
1072 ++chan->if_send_adptr_bfrs_full;
1073 ++chan->ifstats.tx_errors;
1074 ++card->wandev.stats.tx_errors;
1076 else
1078 ++chan->if_send_bfrs_passed_to_adptr;
1079 ++chan->ifstats.tx_packets;
1080 ++card->wandev.stats.tx_packets;
1081 chan->ifstats.tx_bytes += skb->len;
1082 card->wandev.stats.tx_bytes += skb->len;
1086 if (!retry)
1087 dev_kfree_skb(skb);
1089 card->wandev.critical = 0;
1090 save_flags(host_cpu_flags);
1091 cli();
1092 if ((!(--card->irq_dis_if_send_count)) && (!card->irq_dis_poll_count))
1093 enable_irq(card->hw.irq);
1094 restore_flags(host_cpu_flags);
1095 return retry;
1098 /*============================================================================
1099 * Reply to UDP Management system.
1100 * Return nothing.
1103 static int reply_udp(unsigned char *data, unsigned int mbox_len)
1105 unsigned short len, udp_length, temp, i, ip_length;
1106 unsigned long sum;
1107 /* Set length of packet */
1108 len = mbox_len + 62;
1109 /* fill in UDP reply */
1110 data[38] = 0x02;
1111 /* fill in UDP length */
1112 udp_length = mbox_len + 40;
1113 /* put it on an even boundary */
1114 if (udp_length & 0x0001)
1116 udp_length += 1;
1117 len += 1;
1119 temp = (udp_length << 8) | (udp_length >> 8);
1120 memcpy(&data[26], &temp, 2);
1121 /* swap UDP ports */
1122 memcpy(&temp, &data[22], 2);
1123 memcpy(&data[22], &data[24], 2);
1124 memcpy(&data[24], &temp, 2);
1125 /* add UDP pseudo header */
1126 temp = 0x1100;
1127 memcpy(&data[udp_length + 22], &temp, 2);
1128 temp = (udp_length << 8) | (udp_length >> 8);
1129 memcpy(&data[udp_length + 24], &temp, 2);
1130 /* calculate UDP checksum */
1131 data[28] = data[29] = 0;
1132 sum = 0;
1133 for (i = 0; i < udp_length + 12; i += 2)
1135 memcpy(&temp, &data[14 + i], 2);
1136 sum += (unsigned long) temp;
1138 while (sum >> 16)
1139 sum = (sum & 0xffffUL) + (sum >> 16);
1141 temp = (unsigned short) sum;
1142 temp = ~temp;
1143 if (temp == 0)
1144 temp = 0xffff;
1145 memcpy(&data[28], &temp, 2);
1146 /* fill in IP length */
1147 ip_length = udp_length + 20;
1148 temp = (ip_length << 8) | (ip_length >> 8);
1149 memcpy(&data[4], &temp, 2);
1150 /* swap IP addresses */
1151 memcpy(&temp, &data[14], 2);
1152 memcpy(&data[14], &data[18], 2);
1153 memcpy(&data[18], &temp, 2);
1154 memcpy(&temp, &data[16], 2);
1155 memcpy(&data[16], &data[20], 2);
1156 memcpy(&data[20], &temp, 2);
1157 /* fill in IP checksum */
1158 data[12] = data[13] = 0;
1159 sum = 0;
1160 for (i = 0; i < 20; i += 2)
1162 memcpy(&temp, &data[2 + i], 2);
1163 sum += (unsigned long) temp;
1165 while (sum >> 16)
1166 sum = (sum & 0xffffUL) + (sum >> 16);
1167 temp = (unsigned short) sum;
1168 temp = ~temp;
1169 if (temp == 0)
1170 temp = 0xffff;
1171 memcpy(&data[12], &temp, 2);
1172 return len;
1173 } /* reply_udp */
1175 If incoming is 0 (outgoing)- if the net numbers is ours make it 0
1176 if incoming is 1 - if the net number is 0 make it ours
1179 static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming)
1181 unsigned long pnetwork_number;
1182 pnetwork_number = (unsigned long) ((sendpacket[14] << 24) +
1183 (sendpacket[15] << 16) + (sendpacket[16] << 8) +
1184 sendpacket[17]);
1185 if (!incoming) {
1186 /* If the destination network number is ours, make it 0 */
1187 if (pnetwork_number == network_number) {
1188 sendpacket[14] = sendpacket[15] = sendpacket[16] =
1189 sendpacket[17] = 0x00;
1191 } else {
1192 /* If the incoming network is 0, make it ours */
1193 if (pnetwork_number == 0)
1195 sendpacket[14] = (unsigned char) (network_number >> 24);
1196 sendpacket[15] = (unsigned char) ((network_number &
1197 0x00FF0000) >> 16);
1198 sendpacket[16] = (unsigned char) ((network_number &
1199 0x0000FF00) >> 8);
1200 sendpacket[17] = (unsigned char) (network_number &
1201 0x000000FF);
1204 pnetwork_number = (unsigned long) ((sendpacket[26] << 24) +
1205 (sendpacket[27] << 16) + (sendpacket[28] << 8) +
1206 sendpacket[29]);
1207 if (!incoming) {
1208 /* If the source network is ours, make it 0 */
1209 if (pnetwork_number == network_number)
1211 sendpacket[26] = sendpacket[27] = sendpacket[28] =
1212 sendpacket[29] = 0x00;
1214 } else {
1215 /* If the source network is 0, make it ours */
1216 if (pnetwork_number == 0) {
1217 sendpacket[26] = (unsigned char) (network_number >> 24);
1218 sendpacket[27] = (unsigned char) ((network_number &
1219 0x00FF0000) >> 16);
1220 sendpacket[28] = (unsigned char) ((network_number &
1221 0x0000FF00) >> 8);
1222 sendpacket[29] = (unsigned char) (network_number &
1223 0x000000FF);
1226 } /* switch_net_numbers */
1228 /*============================================================================
1229 * Get Ethernet-style interface statistics.
1230 * Return a pointer to struct net_device_stats.
1233 static struct net_device_stats *if_stats(struct net_device *dev)
1235 fr_channel_t *chan = dev->priv;
1236 if(chan==NULL)
1237 return NULL;
1239 return &chan->ifstats;
1242 /****** Interrupt Handlers **************************************************/
1243 /*============================================================================
1244 * S502 frame relay interrupt service routine.
1247 static void fr502_isr(sdla_t * card)
1249 fr502_flags_t *flags = card->flags;
1250 switch (flags->iflag)
1252 case 0x01: /* receive interrupt */
1253 fr502_rx_intr(card);
1254 break;
1255 case 0x02: /* transmit interrupt */
1256 flags->imask &= ~0x02;
1257 tx_intr(card);
1258 break;
1259 default:
1260 spur_intr(card);
1262 flags->iflag = 0;
1264 /*============================================================================
1265 * S508 frame relay interrupt service routine.
1268 static void fr508_isr(sdla_t * card)
1270 fr508_flags_t *flags = card->flags;
1271 fr_buf_ctl_t *bctl;
1272 char *ptr = &flags->iflag;
1273 struct net_device *dev = card->wandev.dev;
1274 struct net_device *dev2;
1275 int i;
1276 unsigned long host_cpu_flags;
1277 unsigned disable_tx_intr = 1;
1278 fr_channel_t *chan;
1279 fr_dlci_interface_t *dlci_interface;
1280 /* This flag prevents nesting of interrupts. See sdla_isr() routine
1281 * in sdlamain.c.
1283 card->in_isr = 1;
1284 ++card->statistics.isr_entry;
1285 if (test_and_set_bit(0, (void *) &card->wandev.critical))
1287 printk(KERN_INFO "fr508_isr: %s, wandev.critical set to 0x%02X, int type = 0x%02X\n", card->devname, card->wandev.critical, flags->iflag);
1288 ++card->statistics.isr_already_critical;
1289 card->in_isr = 0;
1290 return;
1292 /* For all interrupts set the critical flag to CRITICAL_RX_INTR.
1293 * If the if_send routine is called with this flag set it will set
1294 * the enable transmit flag to 1. (for a delayed interrupt)
1296 card->wandev.critical = CRITICAL_IN_ISR;
1297 card->dlci_int_mode_unbusy = 0;
1298 card->buff_int_mode_unbusy = 0;
1299 switch (flags->iflag)
1301 case 0x01: /* receive interrupt */
1302 ++card->statistics.isr_rx;
1303 fr508_rx_intr(card);
1304 break;
1305 case 0x02: /* transmit interrupt */
1306 ++card->statistics.isr_tx;
1307 bctl = (void *) (flags->tse_offs - FR_MB_VECTOR +
1308 card->hw.dpmbase);
1309 bctl->flag = 0xA0;
1310 if (card->intr_mode == DLCI_LIST_INTR_MODE)
1312 /* Find the structure and make it unbusy */
1313 dev = find_channel(card, flags->dlci);
1314 dev->tbusy = 0;
1315 /* This is used to perform devtint at the
1316 * end of the isr
1318 card->dlci_int_mode_unbusy = 1;
1319 /* check to see if any other interfaces are
1320 * busy. If so then do not disable the tx
1321 * interrupts
1323 for (dev2 = card->wandev.dev; dev2;
1324 dev2 = dev2->slave)
1326 if (dev2->tbusy == 1)
1328 disable_tx_intr = 0;
1329 break;
1332 if (disable_tx_intr)
1333 flags->imask &= ~0x02;
1335 else if (card->intr_mode == BUFFER_INTR_MODE)
1337 for (dev2 = card->wandev.dev; dev2;
1338 dev2 = dev2->slave)
1340 if (!dev2 || !dev2->start)
1342 ++card->statistics.tx_intr_dev_not_started;
1343 continue;
1345 if (dev2->tbusy)
1347 card->buff_int_mode_unbusy = 1;
1348 ((fr_channel_t *) dev2->priv)->dev_pending_devtint = 1;
1349 dev2->tbusy = 0;
1351 else
1352 ((fr_channel_t *) dev2->priv)->dev_pending_devtint = 0;
1354 flags->imask &= ~0x02;
1356 break;
1357 case 0x08:
1358 Intr_test_counter++;
1359 ++card->statistics.isr_intr_test;
1360 break;
1361 default:
1362 ++card->statistics.isr_spurious;
1363 spur_intr(card);
1364 printk(KERN_INFO "%s: Interrupt Type 0x%02X!\n",
1365 card->devname, flags->iflag);
1366 printk(KERN_INFO "%s: ID Bytes = ", card->devname);
1367 for (i = 0; i < 8; i++)
1368 printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
1369 printk(KERN_INFO "\n");
1370 break;
1372 card->wandev.critical = CRITICAL_INTR_HANDLED;
1373 if (card->wandev.enable_tx_int)
1375 if (card->intr_mode == DLCI_LIST_INTR_MODE)
1377 for (dev2 = card->wandev.dev; dev2; dev2 = dev2->slave)
1379 chan = dev2->priv;
1380 if (chan->tx_int_status == WAITING_TO_BE_ENABLED)
1382 dlci_interface = chan->dlci_int_interface;
1383 dlci_interface->gen_interrupt |= 0x40;
1384 dlci_interface->packet_length = chan->pkt_length;
1385 chan->tx_int_status = DISABLED;
1389 card->wandev.enable_tx_int = 0;
1390 flags->imask |= 0x02;
1391 ++card->statistics.isr_enable_tx_int;
1393 save_flags(host_cpu_flags);
1394 cli();
1395 card->in_isr = 0;
1396 card->wandev.critical = 0xD1;
1397 flags->iflag = 0;
1398 card->wandev.critical = 0;
1399 restore_flags(host_cpu_flags);
1400 /* Device is now ready to send. The instant this is executed the If_Send
1401 routine is called. That is why this is put at the bottom of the ISR
1402 to prevent a endless loop condition caused by repeated Interrupts and
1403 enable_tx_int flag.
1405 if (card->dlci_int_mode_unbusy)
1406 mark_bh(NET_BH);
1407 if (card->buff_int_mode_unbusy)
1409 for (;;)
1411 if (((fr_channel_t *) ((card->devs_struct)->dev_ptr)->priv)->dev_pending_devtint == 1)
1413 ((fr_channel_t *) ((card->devs_struct)->dev_ptr)->priv)->dev_pending_devtint = 0;
1414 mark_bh(NET_BH);
1416 if ((card->devs_struct)->next == card->dev_to_devtint_next)
1417 break;
1418 card->devs_struct = (card->devs_struct)->next;
1420 card->devs_struct = (card->dev_to_devtint_next)->next;
1421 card->dev_to_devtint_next = card->devs_struct;
1424 /*============================================================================
1425 * Receive interrupt handler.
1428 static void fr502_rx_intr(sdla_t * card)
1430 fr_mbox_t *mbox = card->rxmb;
1431 struct sk_buff *skb;
1432 struct net_device *dev;
1433 fr_channel_t *chan;
1434 unsigned dlci, len;
1435 void *buf;
1436 unsigned char *sendpacket;
1437 unsigned char buf2[3];
1438 int udp_type;
1439 sdla_mapmem(&card->hw, FR502_RX_VECTOR);
1440 dlci = mbox->cmd.dlci;
1441 len = mbox->cmd.length;
1442 /* Find network interface for this packet */
1443 dev = find_channel(card, dlci);
1444 if (dev == NULL)
1446 /* Invalid channel, discard packet */
1447 printk(KERN_INFO "%s: receiving on orphaned DLCI %d!\n",
1448 card->devname, dlci);
1449 sdla_mapmem(&card->hw, FR_MB_VECTOR);
1451 chan = dev->priv;
1452 if (!dev->start)
1454 ++chan->ifstats.rx_dropped;
1455 sdla_mapmem(&card->hw, FR_MB_VECTOR);
1457 /* Allocate socket buffer */
1458 skb = dev_alloc_skb(len);
1459 if (skb == NULL)
1461 printk(KERN_INFO "%s: no socket buffers available!\n",
1462 card->devname);
1463 ++chan->ifstats.rx_dropped;
1464 sdla_mapmem(&card->hw, FR_MB_VECTOR);
1466 /* Copy data to the socket buffer */
1467 buf = skb_put(skb, len);
1468 memcpy(buf, mbox->data, len);
1469 sdla_mapmem(&card->hw, FR_MB_VECTOR);
1470 /* Check if it's a UDP management packet */
1471 sendpacket = skb->data;
1472 memcpy(&buf2, &card->wandev.udp_port, 2);
1473 udp_type = udp_pkt_type(skb, card);
1474 if ((udp_type == UDP_FPIPE_TYPE) || (udp_type == UDP_DRVSTATS_TYPE))
1476 if (udp_type == UDP_DRVSTATS_TYPE)
1478 ++chan->rx_intr_DRVSTATS_request;
1479 process_udp_driver_call(UDP_PKT_FRM_NETWORK, card, skb,
1480 dev, dlci, chan);
1482 else
1484 ++chan->rx_intr_FPIPE_request;
1485 process_udp_mgmt_pkt(UDP_PKT_FRM_NETWORK, card, skb,
1486 dev, dlci, chan);
1489 else
1491 /* Decapsulate packet and pass it up the protocol stack */
1492 skb->dev = dev;
1493 buf = skb_pull(skb, 1); /* remove hardware header */
1494 if (!wanrouter_type_trans(skb, dev))
1496 /* can't decapsulate packet */
1497 dev_kfree_skb(skb);
1498 ++chan->ifstats.rx_errors;
1499 ++card->wandev.stats.rx_errors;
1501 else
1503 netif_rx(skb);
1504 ++chan->ifstats.rx_packets;
1505 ++card->wandev.stats.rx_packets;
1506 chan->ifstats.rx_bytes += skb->len;
1507 card->wandev.stats.rx_bytes += skb->len;
1510 sdla_mapmem(&card->hw, FR_MB_VECTOR);
1512 /*============================================================================
1513 * Receive interrupt handler.
1516 static void fr508_rx_intr(sdla_t * card)
1518 fr_buf_ctl_t *frbuf = card->rxmb;
1519 struct sk_buff *skb;
1520 struct net_device *dev;
1521 fr_channel_t *chan;
1522 unsigned dlci, len, offs;
1523 void *buf;
1524 unsigned rx_count = 0;
1525 fr508_flags_t *flags = card->flags;
1526 char *ptr = &flags->iflag;
1527 int i, err, udp_type;
1528 if (frbuf->flag != 0x01)
1530 printk(KERN_INFO
1531 "%s: corrupted Rx buffer @ 0x%X, flag = 0x%02X!\n",
1532 card->devname, (unsigned) frbuf, frbuf->flag);
1533 printk(KERN_INFO "%s: ID Bytes = ", card->devname);
1534 for (i = 0; i < 8; i++)
1535 printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
1536 printk(KERN_INFO "\n");
1537 ++card->statistics.rx_intr_corrupt_rx_bfr;
1538 return;
1543 len = frbuf->length;
1544 dlci = frbuf->dlci;
1545 offs = frbuf->offset;
1546 /* Find network interface for this packet */
1547 dev = find_channel(card, dlci);
1548 chan = dev->priv;
1549 if (dev == NULL)
1551 /* Invalid channel, discard packet */
1552 printk(KERN_INFO "%s: receiving on orphaned DLCI %d!\n"
1553 ,card->devname, dlci);
1554 ++card->statistics.rx_intr_on_orphaned_DLCI;
1556 else
1558 skb = dev_alloc_skb(len);
1559 if (!dev->start || (skb == NULL))
1561 ++chan->ifstats.rx_dropped;
1562 if (dev->start)
1564 printk(KERN_INFO
1565 "%s: no socket buffers available!\n",
1566 card->devname);
1567 ++chan->rx_intr_no_socket;
1568 } else
1569 ++chan->rx_intr_dev_not_started;
1571 else
1573 /* Copy data to the socket buffer */
1574 if ((offs + len) > card->u.f.rx_top + 1)
1576 unsigned tmp = card->u.f.rx_top - offs + 1;
1577 buf = skb_put(skb, tmp);
1578 sdla_peek(&card->hw, offs, buf, tmp);
1579 offs = card->u.f.rx_base;
1580 len -= tmp;
1582 buf = skb_put(skb, len);
1583 sdla_peek(&card->hw, offs, buf, len);
1584 udp_type = udp_pkt_type(skb, card);
1585 if (udp_type == UDP_DRVSTATS_TYPE)
1587 ++chan->rx_intr_DRVSTATS_request;
1588 process_udp_driver_call(
1589 UDP_PKT_FRM_NETWORK, card, skb,
1590 dev, dlci, chan);
1592 else if (udp_type == UDP_FPIPE_TYPE)
1594 ++chan->rx_intr_FPIPE_request;
1595 err = process_udp_mgmt_pkt(
1596 UDP_PKT_FRM_NETWORK, card,
1597 skb, dev, dlci, chan);
1599 else if (handle_IPXWAN(skb->data, card->devname, card->wandev.enable_IPX, card->wandev.network_number))
1601 if (card->wandev.enable_IPX)
1602 fr508_send(card, dlci, 0, skb->len, skb->data);
1604 else
1606 /* Decapsulate packet and pass it up the
1607 protocol stack */
1608 skb->dev = dev;
1609 /* remove hardware header */
1610 buf = skb_pull(skb, 1);
1611 if (!wanrouter_type_trans(skb, dev))
1613 /* can't decapsulate packet */
1614 dev_kfree_skb(skb);
1615 ++chan->
1616 rx_intr_bfr_not_passed_to_stack;
1617 ++chan->
1618 ifstats.rx_errors;
1619 ++card->
1620 wandev.stats.rx_errors;
1622 else
1624 netif_rx(skb);
1625 ++chan->rx_intr_bfr_passed_to_stack;
1626 ++chan->ifstats.rx_packets;
1627 ++card->wandev.stats.rx_packets;
1628 chan->ifstats.rx_bytes += skb->len;
1629 card->wandev.stats.rx_bytes += skb->len;
1634 /* Release buffer element and calculate a pointer to the next
1635 one */
1636 frbuf->flag = 0;
1637 card->rxmb = ++frbuf;
1638 if ((void *) frbuf > card->u.f.rxmb_last)
1639 card->rxmb = card->u.f.rxmb_base;
1640 /* The loop put in is temporary, that is why the break is
1641 * placed here. (?????)
1643 break;
1644 frbuf = card->rxmb;
1646 while (frbuf->flag && ((++rx_count) < 4));
1648 /*============================================================================
1649 * Transmit interrupt handler.
1650 * o print a warning
1651 * o
1652 * If number of spurious interrupts exceeded some limit, then ???
1654 static void tx_intr(sdla_t * card)
1656 struct net_device *dev = card->wandev.dev;
1657 if (card->intr_mode == BUFFER_INTR_MODE)
1659 for (; dev; dev = dev->slave)
1661 if (!dev || !dev->start)
1663 ++card->statistics.tx_intr_dev_not_started;
1664 continue;
1666 dev->tbusy = 0;
1667 mark_bh(NET_BH);
1670 else
1672 dev->tbusy = 0;
1673 mark_bh(NET_BH);
1677 /*============================================================================
1678 * Spurious interrupt handler.
1679 * o print a warning
1680 * o
1681 * If number of spurious interrupts exceeded some limit, then ???
1684 static void spur_intr(sdla_t * card)
1686 printk(KERN_INFO "%s: spurious interrupt!\n", card->devname);
1690 Return 0 for non-IPXWAN packet
1691 1 for IPXWAN packet or IPX is not enabled!
1694 static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_IPX, unsigned long network_number)
1696 int i;
1697 if (sendpacket[1] == 0x00 &&
1698 sendpacket[2] == 0x80 &&
1699 sendpacket[6] == 0x81 &&
1700 sendpacket[7] == 0x37)
1702 /* It's an IPX packet */
1703 if (!enable_IPX) {
1704 /* Return 1 so we don't pass it up the stack. */
1705 return 1;
1708 else
1710 /* It's not IPX so return and pass it up the stack. */
1711 return 0;
1713 if (sendpacket[24] == 0x90 &&
1714 sendpacket[25] == 0x04)
1716 /* It's IPXWAN */
1717 if (sendpacket[10] == 0x02 &&
1718 sendpacket[42] == 0x00)
1720 /* It's a timer request packet */
1721 printk(KERN_INFO "%s: Received IPXWAN Timer Request packet\n", devname);
1722 /* Go through the routing options and answer no to every */
1723 /* option except Unnumbered RIP/SAP */
1724 for (i = 49; sendpacket[i] == 0x00; i += 5)
1726 /* 0x02 is the option for Unnumbered RIP/SAP */
1727 if (sendpacket[i + 4] != 0x02)
1729 sendpacket[i + 1] = 0;
1732 /* Skip over the extended Node ID option */
1733 if (sendpacket[i] == 0x04)
1734 i += 8;
1735 /* We also want to turn off all header compression opt. */
1736 for (; sendpacket[i] == 0x80;)
1738 sendpacket[i + 1] = 0;
1739 i += (sendpacket[i + 2] << 8) + (sendpacket[i + 3]) + 4;
1741 /* Set the packet type to timer response */
1742 sendpacket[42] = 0x01;
1743 printk(KERN_INFO "%s: Sending IPXWAN Timer Response\n", devname);
1745 else if (sendpacket[42] == 0x02)
1747 /* This is an information request packet */
1748 printk(KERN_INFO "%s: Received IPXWAN Information Request packet\n", devname);
1749 /* Set the packet type to information response */
1750 sendpacket[42] = 0x03;
1751 /* Set the router name */
1752 sendpacket[59] = 'F';
1753 sendpacket[60] = 'P';
1754 sendpacket[61] = 'I';
1755 sendpacket[62] = 'P';
1756 sendpacket[63] = 'E';
1757 sendpacket[64] = '-';
1758 sendpacket[65] = CVHexToAscii(network_number >> 28);
1759 sendpacket[66] = CVHexToAscii((network_number & 0x0F000000) >> 24);
1760 sendpacket[67] = CVHexToAscii((network_number & 0x00F00000) >> 20);
1761 sendpacket[68] = CVHexToAscii((network_number & 0x000F0000) >> 16);
1762 sendpacket[69] = CVHexToAscii((network_number & 0x0000F000) >> 12);
1763 sendpacket[70] = CVHexToAscii((network_number & 0x00000F00) >> 8);
1764 sendpacket[71] = CVHexToAscii((network_number & 0x000000F0) >> 4);
1765 sendpacket[72] = CVHexToAscii(network_number & 0x0000000F);
1766 for (i = 73; i < 107; i += 1)
1767 sendpacket[i] = 0;
1768 printk(KERN_INFO "%s: Sending IPXWAN Information Response packet\n", devname);
1770 else
1772 printk(KERN_INFO "%s: Unknown IPXWAN packet!\n", devname);
1773 return 0;
1775 /* Set the WNodeID to our network address */
1776 sendpacket[43] = (unsigned char) (network_number >> 24);
1777 sendpacket[44] = (unsigned char) ((network_number & 0x00FF0000) >> 16);
1778 sendpacket[45] = (unsigned char) ((network_number & 0x0000FF00) >> 8);
1779 sendpacket[46] = (unsigned char) (network_number & 0x000000FF);
1780 return 1;
1782 /* If we get here, its an IPX-data packet so it'll get passed up the stack. */
1783 /* switch the network numbers */
1784 switch_net_numbers(sendpacket, network_number, 1);
1785 return 0;
1788 /****** Background Polling Routines ****************************************/
1790 /*============================================================================
1791 * Main polling routine.
1792 * This routine is repeatedly called by the WANPIPE 'thead' to allow for
1793 * time-dependent housekeeping work.
1795 * o fetch asynchronous network events.
1797 * Notes:
1798 * 1. This routine may be called on interrupt context with all interrupts
1799 * enabled. Beware!
1802 static void wpf_poll(sdla_t * card)
1804 /* struct net_device* dev = card->wandev.dev; */
1805 fr508_flags_t *flags = card->flags;
1806 unsigned long host_cpu_flags;
1807 ++card->statistics.poll_entry;
1808 if (((jiffies - card->state_tick) < HZ) ||
1809 (card->intr_mode == INTR_TEST_MODE))
1810 return;
1811 disable_irq(card->hw.irq);
1812 ++card->irq_dis_poll_count;
1813 if (test_and_set_bit(0, (void *) &card->wandev.critical))
1815 ++card->statistics.poll_already_critical;
1816 save_flags(host_cpu_flags);
1817 cli();
1818 if ((!card->irq_dis_if_send_count) &&
1819 (!(--card->irq_dis_poll_count)))
1820 enable_irq(card->hw.irq);
1821 restore_flags(host_cpu_flags);
1822 return;
1824 card->wandev.critical = 0x11;
1825 ++card->statistics.poll_processed;
1826 /* This is to be changed later ??? */
1828 if( dev && dev->tbusy && !(flags->imask & 0x02) ) {
1829 printk(KERN_INFO "%s: Wpf_Poll: tbusy = 0x01, imask = 0x%02X\n", card->devname, flags->imask);
1832 if (flags->event)
1834 fr_mbox_t *mbox = card->mbox;
1835 int err;
1836 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
1837 mbox->cmd.command = FR_READ_STATUS;
1838 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1839 if (err)
1840 fr_event(card, err, mbox);
1842 card->wandev.critical = 0;
1843 save_flags(host_cpu_flags);
1844 cli();
1845 if ((!card->irq_dis_if_send_count) && (!(--card->irq_dis_poll_count)))
1846 enable_irq(card->hw.irq);
1847 restore_flags(host_cpu_flags);
1848 card->state_tick = jiffies;
1851 /****** Frame Relay Firmware-Specific Functions *****************************/
1853 /*============================================================================
1854 * Read firmware code version.
1855 * o fill string str with firmware version info.
1858 static int fr_read_version(sdla_t * card, char *str)
1860 fr_mbox_t *mbox = card->mbox;
1861 int retry = MAX_CMD_RETRY;
1862 int err;
1865 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
1866 mbox->cmd.command = FR_READ_CODE_VERSION;
1867 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1868 } while (err && retry-- && fr_event(card, err, mbox));
1870 if (!err && str)
1872 int len = mbox->cmd.length;
1873 memcpy(str, mbox->data, len);
1874 str[len] = '\0';
1876 return err;
1878 /*============================================================================
1879 * Set global configuration.
1882 static int fr_configure(sdla_t * card, fr_conf_t * conf)
1884 fr_mbox_t *mbox = card->mbox;
1885 int retry = MAX_CMD_RETRY;
1886 int dlci_num = card->u.f.dlci_num;
1887 int err, i;
1890 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
1891 memcpy(mbox->data, conf, sizeof(fr_conf_t));
1892 if (dlci_num)
1893 for (i = 0; i < dlci_num; ++i)
1894 ((fr_conf_t *) mbox->data)->dlci[i] =
1895 card->u.f.node_dlci[i];
1896 mbox->cmd.command = FR_SET_CONFIG;
1897 mbox->cmd.length =
1898 sizeof(fr_conf_t) + dlci_num * sizeof(short);
1899 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1901 while (err && retry-- && fr_event(card, err, mbox));
1903 return err;
1905 /*============================================================================
1906 * Set DLCI configuration.
1908 static int fr_dlci_configure(sdla_t * card, fr_dlc_conf_t * conf, unsigned dlci)
1910 fr_mbox_t *mbox = card->mbox;
1911 int retry = MAX_CMD_RETRY;
1912 int err;
1915 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
1916 memcpy(mbox->data, conf, sizeof(fr_dlc_conf_t));
1917 mbox->cmd.dlci = (unsigned short) dlci;
1918 mbox->cmd.command = FR_SET_CONFIG;
1919 mbox->cmd.length = 0x0E;
1920 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1922 while (err && retry--);
1923 return err;
1925 /*============================================================================
1926 * Set interrupt mode.
1928 static int fr_set_intr_mode(sdla_t * card, unsigned mode, unsigned mtu)
1930 fr_mbox_t *mbox = card->mbox;
1931 int retry = MAX_CMD_RETRY;
1932 int err;
1935 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
1936 if (card->hw.fwid == SFID_FR502)
1938 fr502_intr_ctl_t *ictl = (void *) mbox->data;
1939 memset(ictl, 0, sizeof(fr502_intr_ctl_t));
1940 ictl->mode = mode;
1941 ictl->tx_len = mtu;
1942 mbox->cmd.length = sizeof(fr502_intr_ctl_t);
1944 else
1946 fr508_intr_ctl_t *ictl = (void *) mbox->data;
1947 memset(ictl, 0, sizeof(fr508_intr_ctl_t));
1948 ictl->mode = mode;
1949 ictl->tx_len = mtu;
1950 ictl->irq = card->hw.irq;
1951 mbox->cmd.length = sizeof(fr508_intr_ctl_t);
1953 mbox->cmd.command = FR_SET_INTR_MODE;
1954 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1956 while (err && retry-- && fr_event(card, err, mbox));
1958 return err;
1960 /*============================================================================
1961 * Enable communications.
1963 static int fr_comm_enable(sdla_t * card)
1965 fr_mbox_t *mbox = card->mbox;
1966 int retry = MAX_CMD_RETRY;
1967 int err;
1970 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
1971 mbox->cmd.command = FR_COMM_ENABLE;
1972 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1974 while (err && retry-- && fr_event(card, err, mbox));
1976 return err;
1978 /*============================================================================
1979 * Disable communications.
1981 static int fr_comm_disable(sdla_t * card)
1983 fr_mbox_t *mbox = card->mbox;
1984 int retry = MAX_CMD_RETRY;
1985 int err;
1988 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
1989 mbox->cmd.command = FR_COMM_DISABLE;
1990 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1992 while (err && retry-- && fr_event(card, err, mbox));
1994 return err;
1996 /*============================================================================
1997 * Get communications error statistics.
1999 static int fr_get_err_stats(sdla_t * card)
2001 fr_mbox_t *mbox = card->mbox;
2002 int retry = MAX_CMD_RETRY;
2003 int err;
2007 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
2008 mbox->cmd.command = FR_READ_ERROR_STATS;
2009 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2011 while (err && retry-- && fr_event(card, err, mbox));
2013 if (!err)
2015 fr_comm_stat_t *stats = (void *) mbox->data;
2016 card->wandev.stats.rx_over_errors = stats->rx_overruns;
2017 card->wandev.stats.rx_crc_errors = stats->rx_bad_crc;
2018 card->wandev.stats.rx_missed_errors = stats->rx_aborts;
2019 card->wandev.stats.rx_length_errors = stats->rx_too_long;
2020 card->wandev.stats.tx_aborted_errors = stats->tx_aborts;
2022 return err;
2024 /*============================================================================
2025 * Get statistics.
2027 static int fr_get_stats(sdla_t * card)
2029 fr_mbox_t *mbox = card->mbox;
2030 int retry = MAX_CMD_RETRY;
2031 int err;
2034 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
2035 mbox->cmd.command = FR_READ_STATISTICS;
2036 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2038 while (err && retry-- && fr_event(card, err, mbox));
2040 if (!err)
2042 fr_link_stat_t *stats = (void *) mbox->data;
2043 card->wandev.stats.rx_frame_errors = stats->rx_bad_format;
2044 card->wandev.stats.rx_dropped = stats->rx_dropped + stats->rx_dropped2;
2046 return err;
2048 /*============================================================================
2049 * Add DLCI(s) (Access Node only!).
2050 * This routine will perform the ADD_DLCIs command for the specified DLCI.
2052 static int fr_add_dlci(sdla_t * card, int dlci, int num)
2054 fr_mbox_t *mbox = card->mbox;
2055 int retry = MAX_CMD_RETRY;
2056 int err, i;
2059 unsigned short *dlci_list = (void *) mbox->data;
2060 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
2061 for (i = 0; i < num; ++i)
2062 dlci_list[i] = card->u.f.node_dlci[i];
2063 mbox->cmd.length = num * sizeof(short);
2064 mbox->cmd.command = FR_ADD_DLCI;
2065 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2067 while (err && retry-- && fr_event(card, err, mbox));
2069 return err;
2071 /*============================================================================
2072 * Activate DLCI(s) (Access Node only!).
2073 * This routine will perform the ACTIVATE_DLCIs command with a list of DLCIs.
2075 static int fr_activate_dlci(sdla_t * card, int dlci, int num)
2077 fr_mbox_t *mbox = card->mbox;
2078 int retry = MAX_CMD_RETRY;
2079 int err, i;
2082 unsigned short *dlci_list = (void *) mbox->data;
2083 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
2084 for (i = 0; i < num; ++i)
2085 dlci_list[i] = card->u.f.node_dlci[i];
2086 mbox->cmd.length = num * sizeof(short);
2087 mbox->cmd.command = FR_ACTIVATE_DLCI;
2088 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2090 while (err && retry-- && fr_event(card, err, mbox));
2092 return err;
2094 /*============================================================================
2095 * Issue in-channel signalling frame.
2097 static int fr_issue_isf(sdla_t * card, int isf)
2099 fr_mbox_t *mbox = card->mbox;
2100 int retry = MAX_CMD_RETRY;
2101 int err;
2104 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
2105 mbox->data[0] = isf;
2106 mbox->cmd.length = 1;
2107 mbox->cmd.command = FR_ISSUE_IS_FRAME;
2108 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2110 while (err && retry-- && fr_event(card, err, mbox));
2112 return err;
2114 /*============================================================================
2115 * Send a frame (S502 version).
2117 static int fr502_send(sdla_t * card, int dlci, int attr, int len, void *buf)
2119 fr_mbox_t *mbox = card->mbox;
2120 int retry = MAX_CMD_RETRY;
2121 int err;
2125 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
2126 memcpy(mbox->data, buf, len);
2127 mbox->cmd.dlci = dlci;
2128 mbox->cmd.attr = attr;
2129 mbox->cmd.length = len;
2130 mbox->cmd.command = FR_WRITE;
2131 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2133 while (err && retry-- && fr_event(card, err, mbox));
2135 return err;
2137 /*============================================================================
2138 * Send a frame (S508 version).
2140 static int fr508_send(sdla_t * card, int dlci, int attr, int len, void *buf)
2142 fr_mbox_t *mbox = card->mbox;
2143 int retry = MAX_CMD_RETRY;
2144 int err;
2148 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
2149 mbox->cmd.dlci = dlci;
2150 mbox->cmd.attr = attr;
2151 mbox->cmd.length = len;
2152 mbox->cmd.command = FR_WRITE;
2153 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2155 while (err && retry-- && fr_event(card, err, mbox));
2157 if (!err)
2159 fr_buf_ctl_t *frbuf = (void *) (*(unsigned long *) mbox->data -
2160 FR_MB_VECTOR + card->hw.dpmbase);
2161 sdla_poke(&card->hw, frbuf->offset, buf, len);
2162 frbuf->flag = 0x01;
2164 return err;
2167 /****** Firmware Asynchronous Event Handlers ********************************/
2169 /*============================================================================
2170 * Main asynchronous event/error handler.
2171 * This routine is called whenever firmware command returns non-zero
2172 * return code.
2174 * Return zero if previous command has to be cancelled.
2177 static int fr_event(sdla_t * card, int event, fr_mbox_t * mbox)
2179 fr508_flags_t *flags = card->flags;
2180 char *ptr = &flags->iflag;
2181 int i;
2182 switch (event)
2184 case FRRES_MODEM_FAILURE:
2185 return fr_modem_failure(card, mbox);
2186 case FRRES_CHANNEL_DOWN:
2187 wanpipe_set_state(card, WAN_DISCONNECTED);
2188 return 1;
2189 case FRRES_CHANNEL_UP:
2190 wanpipe_set_state(card, WAN_CONNECTED);
2191 return 1;
2192 case FRRES_DLCI_CHANGE:
2193 return fr_dlci_change(card, mbox);
2194 case FRRES_DLCI_MISMATCH:
2195 printk(KERN_INFO "%s: DLCI list mismatch!\n",
2196 card->devname);
2197 return 1;
2198 case CMD_TIMEOUT:
2199 printk(KERN_ERR "%s: command 0x%02X timed out!\n",
2200 card->devname, mbox->cmd.command);
2201 printk(KERN_INFO "%s: ID Bytes = ", card->devname);
2202 for (i = 0; i < 8; i++)
2203 printk(KERN_INFO "0x%02X ", *(ptr + 0x28 + i));
2204 printk(KERN_INFO "\n");
2205 break;
2206 case FRRES_DLCI_INACTIVE:
2207 printk(KERN_ERR "%s: DLCI %u is inactive!\n",
2208 card->devname, mbox->cmd.dlci);
2209 break;
2210 case FRRES_CIR_OVERFLOW:
2211 break;
2212 case FRRES_BUFFER_OVERFLOW:
2213 break;
2214 default:
2215 printk(KERN_INFO "%s: command 0x%02X returned 0x%02X!\n"
2216 ,card->devname, mbox->cmd.command, event);
2218 return 0;
2221 /*============================================================================
2222 * Handle modem error.
2224 * Return zero if previous command has to be cancelled.
2226 static int fr_modem_failure(sdla_t * card, fr_mbox_t * mbox)
2228 printk(KERN_INFO "%s: physical link down! (modem error 0x%02X)\n",
2229 card->devname, mbox->data[0]);
2230 switch (mbox->cmd.command)
2232 case FR_WRITE:
2233 case FR_READ:
2234 return 0;
2236 return 1;
2238 /*============================================================================
2239 * Handle DLCI status change.
2241 * Return zero if previous command has to be cancelled.
2243 static int fr_dlci_change(sdla_t * card, fr_mbox_t * mbox)
2245 dlci_status_t *status = (void *) mbox->data;
2246 int cnt = mbox->cmd.length / sizeof(dlci_status_t);
2247 fr_dlc_conf_t cfg;
2248 fr_channel_t *chan;
2249 struct net_device *dev2;
2250 for (; cnt; --cnt, ++status)
2252 unsigned short dlci = status->dlci;
2253 struct net_device *dev = find_channel(card, dlci);
2254 if (dev == NULL)
2256 printk(KERN_INFO
2257 "%s: CPE contains unconfigured DLCI= %d\n",
2258 card->devname, dlci);
2260 else
2262 if (status->state & 0x01)
2264 printk(KERN_INFO
2265 "%s: DLCI %u has been deleted!\n",
2266 card->devname, dlci);
2267 if (dev && dev->start)
2268 set_chan_state(dev, WAN_DISCONNECTED);
2270 else if (status->state & 0x02)
2272 printk(KERN_INFO
2273 "%s: DLCI %u becomes active!\n",
2274 card->devname, dlci);
2275 chan = dev->priv;
2276 /* This flag is used for configuring specific
2277 DLCI(s) when they become active.
2279 chan->dlci_configured = DLCI_CONFIG_PENDING;
2280 if (dev && dev->start)
2281 set_chan_state(dev, WAN_CONNECTED);
2285 for (dev2 = card->wandev.dev; dev2; dev2 = dev2->slave)
2287 chan = dev2->priv;
2288 if (chan->dlci_configured == DLCI_CONFIG_PENDING)
2290 memset(&cfg, 0, sizeof(cfg));
2291 if (chan->cir_status == CIR_DISABLED)
2293 cfg.cir_fwd = cfg.cir_bwd = 16;
2294 cfg.bc_fwd = cfg.bc_bwd = 16;
2295 cfg.conf_flags = 0x0001;
2296 printk(KERN_INFO "%s: CIR Disabled for %s\n",
2297 card->devname, chan->name);
2298 } else if (chan->cir_status == CIR_ENABLED) {
2299 cfg.cir_fwd = cfg.cir_bwd = chan->cir;
2300 cfg.bc_fwd = cfg.bc_bwd = chan->bc;
2301 cfg.be_fwd = cfg.be_bwd = chan->be;
2302 cfg.conf_flags = 0x0000;
2303 printk(KERN_INFO "%s: CIR Enabled for %s\n",
2304 card->devname, chan->name);
2306 if (fr_dlci_configure(card, &cfg, chan->dlci))
2308 printk(KERN_INFO
2309 "%s: DLCI Configure failed for %d\n",
2310 card->devname, chan->dlci);
2311 return 1;
2313 chan->dlci_configured = DLCI_CONFIGURED;
2314 /* Read the interface byte mapping into the channel
2315 structure.
2317 if (card->intr_mode == DLCI_LIST_INTR_MODE)
2318 read_DLCI_IB_mapping(card, chan);
2321 return 1;
2323 /******* Miscellaneous ******************************************************/
2325 /*============================================================================
2326 * Update channel state.
2328 static int update_chan_state(struct net_device *dev)
2330 fr_channel_t *chan = dev->priv;
2331 sdla_t *card = chan->card;
2332 fr_mbox_t *mbox = card->mbox;
2333 int retry = MAX_CMD_RETRY;
2334 int err;
2335 int dlci_found = 0;
2339 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
2340 mbox->cmd.command = FR_LIST_ACTIVE_DLCI;
2341 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2343 while (err && retry-- && fr_event(card, err, mbox));
2345 if (!err)
2347 unsigned short *list = (void *) mbox->data;
2348 int cnt = mbox->cmd.length / sizeof(short);
2349 for (; cnt; --cnt, ++list)
2351 if (*list == chan->dlci)
2353 dlci_found = 1;
2354 set_chan_state(dev, WAN_CONNECTED);
2355 break;
2358 if (!dlci_found)
2359 printk(KERN_INFO "%s: DLCI %u is inactive\n",
2360 card->devname, chan->dlci);
2363 return err;
2365 /*============================================================================
2366 * Set channel state.
2368 static void set_chan_state(struct net_device *dev, int state)
2370 fr_channel_t *chan = dev->priv;
2371 sdla_t *card = chan->card;
2372 unsigned long flags;
2374 save_flags(flags);
2375 cli();
2377 if (chan->state != state)
2379 switch (state)
2381 case WAN_CONNECTED:
2382 printk(KERN_INFO "%s: interface %s connected!\n"
2383 ,card->devname, dev->name);
2384 break;
2385 case WAN_CONNECTING:
2386 printk(KERN_INFO
2387 "%s: interface %s connecting...\n",
2388 card->devname, dev->name);
2389 break;
2390 case WAN_DISCONNECTED:
2391 printk(KERN_INFO
2392 "%s: interface %s disconnected!\n",
2393 card->devname, dev->name);
2394 break;
2396 chan->state = state;
2398 chan->state_tick = jiffies;
2399 restore_flags(flags);
2402 /*============================================================================
2403 * Find network device by its channel number.
2405 static struct net_device *find_channel(sdla_t * card, unsigned dlci)
2407 struct net_device *dev;
2408 for (dev = card->wandev.dev; dev; dev = dev->slave)
2409 if (((fr_channel_t *) dev->priv)->dlci == dlci)
2410 break;
2411 return dev;
2413 /*============================================================================
2414 * Check to see if a frame can be sent. If no transmit buffers available,
2415 * enable transmit interrupts.
2417 * Return: 1 - Tx buffer(s) available
2418 * 0 - no buffers available
2421 static int is_tx_ready(sdla_t * card, fr_channel_t * chan)
2423 if (card->hw.fwid == SFID_FR508)
2425 unsigned char sb = inb(card->hw.port);
2426 if (sb & 0x02)
2427 return 1;
2429 else
2431 fr502_flags_t *flags = card->flags;
2432 if (flags->tx_ready)
2433 return 1;
2434 flags->imask |= 0x02;
2436 return 0;
2439 /*============================================================================
2440 * Convert decimal string to unsigned integer.
2441 * If len != 0 then only 'len' characters of the string are converted.
2443 static unsigned int dec_to_uint(unsigned char *str, int len)
2445 unsigned val;
2446 if (!len)
2447 len = strlen(str);
2448 for (val = 0; len && is_digit(*str); ++str, --len)
2449 val = (val * 10) + (*str - (unsigned) '0');
2450 return val;
2453 /*==============================================================================
2454 * Process UDP call of type FPIPE8ND
2457 static int process_udp_mgmt_pkt(char udp_pkt_src, sdla_t * card, struct sk_buff *skb, struct net_device *dev, int dlci, fr_channel_t * chan)
2459 int c_retry = MAX_CMD_RETRY;
2460 unsigned char *data;
2461 unsigned char *buf;
2462 unsigned char buf2[5];
2463 unsigned int loops, frames, len;
2464 unsigned long data_ptr;
2465 unsigned short real_len, buffer_length;
2466 struct sk_buff *new_skb;
2467 unsigned char *sendpacket;
2468 fr_mbox_t *mbox = card->mbox;
2469 int err;
2470 struct timeval tv;
2471 int udp_mgmt_req_valid = 1;
2472 sendpacket = skb->data;
2473 memcpy(&buf2, &card->wandev.udp_port, 2);
2474 if ((data = kmalloc(2000, GFP_ATOMIC)) == NULL)
2476 printk(KERN_INFO
2477 "%s: Error allocating memory for UDP management cmnd 0x%02X",
2478 card->devname, data[47]);
2479 ++chan->UDP_FPIPE_mgmt_kmalloc_err;
2480 return 1;
2482 memcpy(data, sendpacket, skb->len);
2483 switch (data[47])
2485 /* FPIPE_ENABLE_TRACE */
2486 case 0x41:
2487 /* FPIPE_DISABLE_TRACE */
2488 case 0x42:
2489 /* FPIPE_GET_TRACE_INFO */
2490 case 0x43:
2491 /* SET FT1 MODE */
2492 case 0x81:
2493 if (udp_pkt_src == UDP_PKT_FRM_NETWORK)
2495 ++chan->UDP_FPIPE_mgmt_direction_err;
2496 udp_mgmt_req_valid = 0;
2497 break;
2499 /* FPIPE_FT1_READ_STATUS */
2500 case 0x44:
2501 /* FT1 MONITOR STATUS */
2502 case 0x80:
2503 if (card->hw.fwid != SFID_FR508)
2505 ++chan->UDP_FPIPE_mgmt_adptr_type_err;
2506 udp_mgmt_req_valid = 0;
2508 break;
2509 default:
2510 break;
2512 if (!udp_mgmt_req_valid)
2514 /* set length to 0 */
2515 data[48] = data[49] = 0;
2516 /* set return code */
2517 data[50] = (card->hw.fwid != SFID_FR508) ? 0x1F : 0xCD;
2519 else
2521 switch (data[47])
2523 /* FPIPE_ENABLE_TRACE */
2524 case 0x41:
2525 if (!TracingEnabled)
2529 /* SET_TRACE_CONFIGURATION */
2530 mbox->cmd.command = 0x60;
2531 mbox->cmd.length = 1;
2532 mbox->cmd.dlci = 0x00;
2533 mbox->data[0] = 0x37;
2534 err = sdla_exec(mbox) ?
2535 mbox->cmd.result : CMD_TIMEOUT;
2537 while (err && c_retry-- && fr_event(card, err, mbox));
2539 if (err)
2541 TracingEnabled = 0;
2542 /* set the return code */
2543 data[50] = mbox->cmd.result;
2544 mbox->cmd.length = 0;
2545 break;
2547 /* get num_frames */
2548 sdla_peek(&card->hw, 0x9000, &num_frames, 2);
2549 sdla_peek(&card->hw, 0x9002, &curr_trace_addr,4);
2550 start_trace_addr = curr_trace_addr;
2551 /* MAX_SEND_BUFFER_SIZE -
2552 * sizeof(UDP_MGMT_PACKET) - 41 */
2553 available_buffer_space = 1926;
2554 /* set return code */
2555 data[50] = 0;
2557 else
2559 /* set return code to line trace already
2560 enabled */
2561 data[50] = 1;
2563 mbox->cmd.length = 0;
2564 TracingEnabled = 1;
2565 break;
2566 /* FPIPE_DISABLE_TRACE */
2567 case 0x42:
2568 if (TracingEnabled)
2572 /* SET_TRACE_CONFIGURATION */
2573 mbox->cmd.command = 0x60;
2574 mbox->cmd.length = 1;
2575 mbox->cmd.dlci = 0x00;
2576 mbox->data[0] = 0x36;
2577 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2579 while (err && c_retry-- && fr_event(card, err, mbox));
2581 /* set return code */
2582 data[50] = 0;
2583 mbox->cmd.length = 0;
2584 TracingEnabled = 0;
2585 break;
2586 /* FPIPE_GET_TRACE_INFO */
2587 case 0x43:
2588 /* Line trace cannot be performed on the 502 */
2589 if (!TracingEnabled)
2591 /* set return code */
2592 data[50] = 1;
2593 mbox->cmd.length = 0;
2594 break;
2596 buffer_length = 0;
2597 loops = (num_frames < 20) ? num_frames : 20;
2598 for (frames = 0; frames < loops; frames += 1)
2600 sdla_peek(&card->hw, curr_trace_addr, &buf2, 1);
2601 /* no data on board so exit */
2602 if (buf2[0] == 0x00)
2603 break;
2604 /* 1+sizeof(FRAME_DATA) = 9 */
2605 if ((available_buffer_space - buffer_length) < 9)
2607 /* indicate we have more frames on board
2608 and exit */
2609 data[62] |= 0x02;
2610 break;
2612 /* get frame status */
2613 sdla_peek(&card->hw, curr_trace_addr + 0x05, &data[62 + buffer_length], 1);
2614 /* get time stamp */
2615 sdla_peek(&card->hw, curr_trace_addr + 0x06, &data[66 + buffer_length], 2);
2616 /* get frame length */
2617 sdla_peek(&card->hw, curr_trace_addr + 0x01, &data[64 + buffer_length], 2);
2618 /* get pointer to real data */
2619 sdla_peek(&card->hw, curr_trace_addr + 0x0C,&data_ptr, 4);
2620 /* see if we can fit the frame into the user buffer */
2621 memcpy(&real_len, &data[64 + buffer_length], 2);
2622 if (data_ptr == 0 || real_len + 8 > available_buffer_space)
2624 data[63 + buffer_length] = 0x00;
2626 else
2628 /* we can take it next time */
2629 if (available_buffer_space - buffer_length < real_len + 8)
2631 data[62] |= 0x02;
2632 break;
2634 /* ok, get the frame */
2635 data[63 + buffer_length] = 0x01;
2636 /* get the data */
2637 sdla_peek(&card->hw, data_ptr, &data[68 + buffer_length], real_len);
2638 /* zero the opp flag to show we got the frame */
2639 buf2[0] = 0x00;
2640 sdla_poke(&card->hw, curr_trace_addr, &buf2, 1);
2641 /* now move onto the next frame */
2642 curr_trace_addr += 16;
2643 /* check if we passed the last address */
2644 if (curr_trace_addr >= (start_trace_addr + num_frames * 16))
2645 curr_trace_addr = start_trace_addr;
2646 /* update buffer length and make sure
2647 its even */
2648 if (data[63 + buffer_length] == 0x01)
2649 buffer_length += real_len - 1;
2650 /* for the header */
2651 buffer_length += 8;
2652 if (buffer_length & 0x0001)
2653 buffer_length += 1;
2656 /* ok now set the total number of frames passed in the
2657 high 5 bits */
2658 data[62] = (frames << 3) | data[62];
2659 /* set the data length */
2660 mbox->cmd.length = buffer_length;
2661 memcpy(&data[48], &buffer_length, 2);
2662 data[50] = 0;
2663 break;
2664 /* FPIPE_FT1_READ_STATUS */
2665 case 0x44:
2666 sdla_peek(&card->hw, 0xF020, &data[62], 2);
2667 data[48] = 2;
2668 data[49] = 0;
2669 data[50] = 0;
2670 mbox->cmd.length = 2;
2671 break;
2672 /* FPIPE_FLUSH_DRIVER_STATS */
2673 case 0x48:
2674 init_chan_statistics(chan);
2675 init_global_statistics(card);
2676 mbox->cmd.length = 0;
2677 break;
2678 case 0x49:
2679 do_gettimeofday(&tv);
2680 chan->router_up_time = tv.tv_sec - chan->router_start_time;
2681 *(unsigned long *) &data[62] = chan->router_up_time;
2682 mbox->cmd.length = 4;
2683 break;
2684 /* FPIPE_KILL_BOARD */
2685 case 0x50:
2686 break;
2687 /* FT1 MONITOR STATUS */
2688 case 0x80:
2689 if (data[62] == 1)
2691 if (rCount++ != 0)
2693 data[50] = 0;
2694 mbox->cmd.length = 1;
2695 break;
2698 /* Disable FT1 MONITOR STATUS */
2699 if (data[62] == 0)
2701 if (--rCount != 0)
2703 data[50] = 0;
2704 mbox->cmd.length = 1;
2705 break;
2708 default:
2711 memcpy(&mbox->cmd, &sendpacket[47], sizeof(fr_cmd_t));
2712 if (mbox->cmd.length)
2713 memcpy(&mbox->data, &sendpacket[62],mbox->cmd.length);
2714 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2716 while (err && c_retry-- && fr_event(card, err, mbox));
2718 if (!err)
2720 ++chan->UDP_FPIPE_mgmt_adptr_cmnd_OK;
2721 memcpy(data, sendpacket, skb->len);
2722 memcpy(&data[47], &mbox->cmd, sizeof(fr_cmd_t));
2723 if (mbox->cmd.length)
2725 memcpy(&data[62], &mbox->data,mbox->cmd.length);
2728 else
2730 ++chan->UDP_FPIPE_mgmt_adptr_cmnd_timeout;
2734 /* Fill UDP TTL */
2735 data[10] = card->wandev.ttl;
2736 len = reply_udp(data, mbox->cmd.length);
2737 if (udp_pkt_src == UDP_PKT_FRM_NETWORK)
2739 err = fr508_send(card, dlci, 0, len, data);
2740 if (err)
2741 ++chan->UDP_FPIPE_mgmt_adptr_send_passed;
2742 else
2743 ++chan->UDP_FPIPE_mgmt_adptr_send_failed;
2744 dev_kfree_skb(skb);
2746 else
2748 /* Allocate socket buffer */
2749 if ((new_skb = dev_alloc_skb(len)) != NULL)
2751 /* copy data into new_skb */
2752 buf = skb_put(new_skb, len);
2753 memcpy(buf, data, len);
2754 /* Decapsulate packet and pass it up the protocol
2755 stack */
2756 new_skb->dev = dev;
2757 buf = skb_pull(new_skb, 1); /* remove hardware header */
2758 if (!wanrouter_type_trans(new_skb, dev))
2760 ++chan->UDP_FPIPE_mgmt_not_passed_to_stack;
2761 /* can't decapsulate packet */
2762 dev_kfree_skb(new_skb);
2764 else
2766 ++chan->UDP_FPIPE_mgmt_passed_to_stack;
2767 netif_rx(new_skb);
2770 else
2772 ++chan->UDP_FPIPE_mgmt_no_socket;
2773 printk(KERN_INFO
2774 "%s: UDP mgmt cmnd, no socket buffers available!\n",
2775 card->devname);
2778 kfree(data);
2779 return 0;
2781 /*==============================================================================
2782 * Perform the Interrupt Test by running the READ_CODE_VERSION command MAX_INTR_
2783 * TEST_COUNTER times.
2786 static int intr_test(sdla_t * card)
2788 fr_mbox_t *mb = card->mbox;
2789 int err, i;
2790 /* The critical flag is unset here because we want to get into the
2791 ISR without the flag already set. The If_open sets the flag.
2793 card->wandev.critical = 0;
2794 err = fr_set_intr_mode(card, 0x08, card->wandev.mtu);
2795 if (err == CMD_OK)
2797 for (i = 0; i < MAX_INTR_TEST_COUNTER; i++)
2799 /* Run command READ_CODE_VERSION */
2800 memset(&mb->cmd, 0, sizeof(fr_cmd_t));
2801 mb->cmd.length = 0;
2802 mb->cmd.command = 0x40;
2803 err = sdla_exec(mb) ? mb->cmd.result : CMD_TIMEOUT;
2804 if (err != CMD_OK)
2805 fr_event(card, err, mb);
2808 else
2810 return err;
2812 err = fr_set_intr_mode(card, 0, card->wandev.mtu);
2813 if (err != CMD_OK)
2814 return err;
2815 card->wandev.critical = 1;
2816 return 0;
2818 /*============================================================================
2819 * Process UDP call of type DRVSTATS.
2821 static int process_udp_driver_call(char udp_pkt_src, sdla_t * card, struct sk_buff *skb, struct net_device *dev, int dlci, fr_channel_t * chan)
2823 int c_retry = MAX_CMD_RETRY;
2824 unsigned char *sendpacket;
2825 unsigned char buf2[5];
2826 unsigned char *data;
2827 unsigned char *buf;
2828 unsigned int len;
2829 fr_mbox_t *mbox = card->mbox;
2830 struct sk_buff *new_skb;
2831 int err;
2832 sendpacket = skb->data;
2833 memcpy(&buf2, &card->wandev.udp_port, 2);
2834 if ((data = kmalloc(2000, GFP_ATOMIC)) == NULL)
2836 printk(KERN_INFO
2837 "%s: Error allocating memory for UDP DRIVER STATS cmnd0x%02X"
2838 ,card->devname, data[45]);
2839 ++chan->UDP_DRVSTATS_mgmt_kmalloc_err;
2840 return 1;
2842 memcpy(data, sendpacket, skb->len);
2843 switch (data[47])
2845 case 0x45:
2846 *(unsigned long *) &data[62] = chan->if_send_entry;
2847 *(unsigned long *) &data[66] = chan->if_send_skb_null;
2848 *(unsigned long *) &data[70] = chan->if_send_broadcast;
2849 *(unsigned long *) &data[74] = chan->if_send_multicast;
2850 *(unsigned long *) &data[78] = chan->if_send_critical_ISR;
2851 *(unsigned long *) &data[82] = chan->if_send_critical_non_ISR;
2852 *(unsigned long *) &data[86] = chan->if_send_busy;
2853 *(unsigned long *) &data[90] = chan->if_send_busy_timeout;
2854 *(unsigned long *) &data[94] = chan->if_send_DRVSTATS_request;
2855 *(unsigned long *) &data[98] = chan->if_send_FPIPE_request;
2856 *(unsigned long *) &data[102] = chan->if_send_wan_disconnected;
2857 *(unsigned long *) &data[106] = chan->if_send_dlci_disconnected;
2858 *(unsigned long *) &data[110] = chan->if_send_no_bfrs;
2859 *(unsigned long *) &data[114] = chan->if_send_adptr_bfrs_full;
2860 *(unsigned long *) &data[118] = chan->if_send_bfrs_passed_to_adptr;
2861 *(unsigned long *) &data[120] = card->irq_dis_if_send_count;
2862 mbox->cmd.length = 62;
2863 break;
2864 case 0x46:
2865 *(unsigned long *) &data[62] = card->statistics.isr_entry;
2866 *(unsigned long *) &data[66] = card->statistics.isr_already_critical;
2867 *(unsigned long *) &data[70] = card->statistics.isr_rx;
2868 *(unsigned long *) &data[74] = card->statistics.isr_tx;
2869 *(unsigned long *) &data[78] = card->statistics.isr_intr_test;
2870 *(unsigned long *) &data[82] = card->statistics.isr_spurious;
2871 *(unsigned long *) &data[86] = card->statistics.isr_enable_tx_int;
2872 *(unsigned long *) &data[90] = card->statistics.tx_intr_dev_not_started;
2873 *(unsigned long *) &data[94] = card->statistics.rx_intr_corrupt_rx_bfr;
2874 *(unsigned long *) &data[98] = card->statistics.rx_intr_on_orphaned_DLCI;
2875 *(unsigned long *) &data[102] = chan->rx_intr_no_socket;
2876 *(unsigned long *) &data[106] = chan->rx_intr_dev_not_started;
2877 *(unsigned long *) &data[110] = chan->rx_intr_DRVSTATS_request;
2878 *(unsigned long *) &data[114] = chan->rx_intr_FPIPE_request;
2879 *(unsigned long *) &data[118] = chan->rx_intr_bfr_not_passed_to_stack;
2880 *(unsigned long *) &data[122] = chan->rx_intr_bfr_passed_to_stack;
2881 mbox->cmd.length = 64;
2882 break;
2883 case 0x47:
2884 *(unsigned long *) &data[62] = chan->UDP_FPIPE_mgmt_kmalloc_err;
2885 *(unsigned long *) &data[66] = chan->UDP_FPIPE_mgmt_adptr_type_err;
2886 *(unsigned long *) &data[70] = chan->UDP_FPIPE_mgmt_direction_err;
2887 *(unsigned long *) &data[74] = chan->UDP_FPIPE_mgmt_adptr_cmnd_timeout;
2888 *(unsigned long *) &data[78] = chan->UDP_FPIPE_mgmt_adptr_cmnd_OK;
2889 *(unsigned long *) &data[82] = chan->UDP_FPIPE_mgmt_adptr_send_passed;
2890 *(unsigned long *) &data[86] = chan->UDP_FPIPE_mgmt_adptr_send_failed;
2891 *(unsigned long *) &data[90] = chan->UDP_FPIPE_mgmt_no_socket;
2892 *(unsigned long *) &data[94] = chan->UDP_FPIPE_mgmt_not_passed_to_stack;
2893 *(unsigned long *) &data[98] = chan->UDP_FPIPE_mgmt_passed_to_stack;
2894 *(unsigned long *) &data[102] = chan->UDP_DRVSTATS_mgmt_kmalloc_err;
2895 *(unsigned long *) &data[106] = chan->UDP_DRVSTATS_mgmt_adptr_cmnd_timeout;
2896 *(unsigned long *) &data[110] = chan->UDP_DRVSTATS_mgmt_adptr_cmnd_OK;
2897 *(unsigned long *) &data[114] = chan->UDP_DRVSTATS_mgmt_adptr_send_passed;
2898 *(unsigned long *) &data[118] = chan->UDP_DRVSTATS_mgmt_adptr_send_failed;
2899 *(unsigned long *) &data[122] = chan->UDP_DRVSTATS_mgmt_no_socket;
2900 *(unsigned long *) &data[126] = chan->UDP_DRVSTATS_mgmt_not_passed_to_stack;
2901 *(unsigned long *) &data[130] = chan->UDP_DRVSTATS_mgmt_passed_to_stack;
2902 *(unsigned long *) &data[134] = card->statistics.poll_entry;
2903 *(unsigned long *) &data[138] = card->statistics.poll_already_critical;
2904 *(unsigned long *) &data[142] = card->statistics.poll_processed;
2905 *(unsigned long *) &data[144] = card->irq_dis_poll_count;
2906 mbox->cmd.length = 86;
2907 break;
2908 default:
2911 memcpy(&mbox->cmd, &sendpacket[47], sizeof(fr_cmd_t));
2912 if (mbox->cmd.length)
2913 memcpy(&mbox->data, &sendpacket[62], mbox->cmd.length);
2914 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
2916 while (err && c_retry-- && fr_event(card, err, mbox));
2918 if (!err)
2920 ++chan->UDP_DRVSTATS_mgmt_adptr_cmnd_OK;
2921 memcpy(data, sendpacket, skb->len);
2922 memcpy(&data[47], &mbox->cmd, sizeof(fr_cmd_t));
2923 if (mbox->cmd.length)
2924 memcpy(&data[62], &mbox->data, mbox->cmd.length);
2926 else
2928 ++chan->UDP_DRVSTATS_mgmt_adptr_cmnd_timeout;
2931 /* Fill UDP TTL */
2932 data[10] = card->wandev.ttl;
2933 len = reply_udp(data, mbox->cmd.length);
2934 if (udp_pkt_src == UDP_PKT_FRM_NETWORK)
2936 err = fr508_send(card, dlci, 0, len, data);
2937 if (err)
2938 ++chan->UDP_DRVSTATS_mgmt_adptr_send_failed;
2939 else
2940 ++chan->UDP_DRVSTATS_mgmt_adptr_send_passed;
2941 dev_kfree_skb(skb);
2943 else
2945 /* Allocate socket buffer */
2946 if ((new_skb = dev_alloc_skb(len)) != NULL)
2948 /* copy data into new_skb */
2949 buf = skb_put(new_skb, len);
2950 memcpy(buf, data, len);
2951 /* Decapsulate packet and pass it up the
2952 protocol stack */
2953 new_skb->dev = dev;
2954 /* remove hardware header */
2955 buf = skb_pull(new_skb, 1);
2956 if (!wanrouter_type_trans(new_skb, dev))
2958 /* can't decapsulate packet */
2959 ++chan->UDP_DRVSTATS_mgmt_not_passed_to_stack;
2960 dev_kfree_skb(new_skb);
2962 else
2964 ++chan->UDP_DRVSTATS_mgmt_passed_to_stack;
2965 netif_rx(new_skb);
2968 else
2970 ++chan->UDP_DRVSTATS_mgmt_no_socket;
2971 printk(KERN_INFO "%s: UDP mgmt cmnd, no socket buffers available!\n", card->devname);
2974 kfree(data);
2975 return 0;
2978 /*==============================================================================
2979 * Determine what type of UDP call it is. DRVSTATS or FPIPE8ND ?
2982 static int udp_pkt_type(struct sk_buff *skb, sdla_t * card)
2984 unsigned char *sendpacket;
2985 unsigned char buf2[5];
2986 sendpacket = skb->data;
2987 memcpy(&buf2, &card->wandev.udp_port, 2);
2988 if (sendpacket[2] == 0x45 && /* IP packet */
2989 sendpacket[11] == 0x11 && /* UDP packet */
2990 sendpacket[24] == buf2[1] && /* UDP Port */
2991 sendpacket[25] == buf2[0] &&
2992 sendpacket[38] == 0x01)
2994 if (sendpacket[30] == 0x46 && /* FPIPE8ND: Signature */
2995 sendpacket[31] == 0x50 &&
2996 sendpacket[32] == 0x49 &&
2997 sendpacket[33] == 0x50 &&
2998 sendpacket[34] == 0x45 &&
2999 sendpacket[35] == 0x38 &&
3000 sendpacket[36] == 0x4E &&
3001 sendpacket[37] == 0x44)
3003 return UDP_FPIPE_TYPE;
3004 } else if (sendpacket[30] == 0x44 && /* DRVSTATS: Signature */
3005 sendpacket[31] == 0x52 &&
3006 sendpacket[32] == 0x56 &&
3007 sendpacket[33] == 0x53 &&
3008 sendpacket[34] == 0x54 &&
3009 sendpacket[35] == 0x41 &&
3010 sendpacket[36] == 0x54 &&
3011 sendpacket[37] == 0x53)
3013 return UDP_DRVSTATS_TYPE;
3015 else
3016 return UDP_INVALID_TYPE;
3018 else
3019 return UDP_INVALID_TYPE;
3021 /*==============================================================================
3022 * Initializes the Statistics values in the fr_channel structure.
3025 void init_chan_statistics(fr_channel_t * chan)
3027 chan->if_send_entry = 0;
3028 chan->if_send_skb_null = 0;
3029 chan->if_send_broadcast = 0;
3030 chan->if_send_multicast = 0;
3031 chan->if_send_critical_ISR = 0;
3032 chan->if_send_critical_non_ISR = 0;
3033 chan->if_send_busy = 0;
3034 chan->if_send_busy_timeout = 0;
3035 chan->if_send_FPIPE_request = 0;
3036 chan->if_send_DRVSTATS_request = 0;
3037 chan->if_send_wan_disconnected = 0;
3038 chan->if_send_dlci_disconnected = 0;
3039 chan->if_send_no_bfrs = 0;
3040 chan->if_send_adptr_bfrs_full = 0;
3041 chan->if_send_bfrs_passed_to_adptr = 0;
3042 chan->rx_intr_no_socket = 0;
3043 chan->rx_intr_dev_not_started = 0;
3044 chan->rx_intr_FPIPE_request = 0;
3045 chan->rx_intr_DRVSTATS_request = 0;
3046 chan->rx_intr_bfr_not_passed_to_stack = 0;
3047 chan->rx_intr_bfr_passed_to_stack = 0;
3048 chan->UDP_FPIPE_mgmt_kmalloc_err = 0;
3049 chan->UDP_FPIPE_mgmt_direction_err = 0;
3050 chan->UDP_FPIPE_mgmt_adptr_type_err = 0;
3051 chan->UDP_FPIPE_mgmt_adptr_cmnd_OK = 0;
3052 chan->UDP_FPIPE_mgmt_adptr_cmnd_timeout = 0;
3053 chan->UDP_FPIPE_mgmt_adptr_send_passed = 0;
3054 chan->UDP_FPIPE_mgmt_adptr_send_failed = 0;
3055 chan->UDP_FPIPE_mgmt_not_passed_to_stack = 0;
3056 chan->UDP_FPIPE_mgmt_passed_to_stack = 0;
3057 chan->UDP_FPIPE_mgmt_no_socket = 0;
3058 chan->UDP_DRVSTATS_mgmt_kmalloc_err = 0;
3059 chan->UDP_DRVSTATS_mgmt_adptr_cmnd_OK = 0;
3060 chan->UDP_DRVSTATS_mgmt_adptr_cmnd_timeout = 0;
3061 chan->UDP_DRVSTATS_mgmt_adptr_send_passed = 0;
3062 chan->UDP_DRVSTATS_mgmt_adptr_send_failed = 0;
3063 chan->UDP_DRVSTATS_mgmt_not_passed_to_stack = 0;
3064 chan->UDP_DRVSTATS_mgmt_passed_to_stack = 0;
3065 chan->UDP_DRVSTATS_mgmt_no_socket = 0;
3067 /*==============================================================================
3068 * Initializes the Statistics values in the Sdla_t structure.
3071 void init_global_statistics(sdla_t * card)
3073 /* Intialize global statistics for a card */
3074 card->statistics.isr_entry = 0;
3075 card->statistics.isr_already_critical = 0;
3076 card->statistics.isr_rx = 0;
3077 card->statistics.isr_tx = 0;
3078 card->statistics.isr_intr_test = 0;
3079 card->statistics.isr_spurious = 0;
3080 card->statistics.isr_enable_tx_int = 0;
3081 card->statistics.rx_intr_corrupt_rx_bfr = 0;
3082 card->statistics.rx_intr_on_orphaned_DLCI = 0;
3083 card->statistics.tx_intr_dev_not_started = 0;
3084 card->statistics.poll_entry = 0;
3085 card->statistics.poll_already_critical = 0;
3086 card->statistics.poll_processed = 0;
3089 static void read_DLCI_IB_mapping(sdla_t * card, fr_channel_t * chan)
3091 fr_mbox_t *mbox = card->mbox;
3092 int retry = MAX_CMD_RETRY;
3093 dlci_IB_mapping_t *result;
3094 int err, counter, found;
3097 memset(&mbox->cmd, 0, sizeof(fr_cmd_t));
3098 mbox->cmd.command = FR_READ_DLCI_IB_MAPPING;
3099 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
3101 while (err && retry-- && fr_event(card, err, mbox));
3103 if (mbox->cmd.result != 0)
3104 printk(KERN_INFO "%s: Read DLCI IB Mapping failed\n", chan->name);
3106 counter = mbox->cmd.length / sizeof(dlci_IB_mapping_t);
3107 result = (void *) mbox->data;
3108 found = 0;
3109 for (; counter; --counter, ++result)
3111 if (result->dlci == chan->dlci)
3113 printk(KERN_INFO "%s: DLCI= %d, IB addr = %lx for %s\n"
3114 ,card->devname, result->dlci, result->addr_value ,chan->name);
3115 chan->IB_addr = result->addr_value;
3116 chan->dlci_int_interface = (void *) (card->hw.dpmbase +
3117 (chan->IB_addr & 0x00001FFF));
3118 found = 1;
3119 break;
3122 if (!found)
3123 printk(KERN_INFO "%s: DLCI %d not found by IB MAPPING cmd\n",
3124 card->devname, chan->dlci);
3127 /****** End *****************************************************************/