patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / net / wan / farsync.c
1 /*
2  *      FarSync WAN driver for Linux (2.6.x kernel version)
3  *
4  *      Actually sync driver for X.21, V.35 and V.24 on FarSync T-series cards
5  *
6  *      Copyright (C) 2001-2004 FarSite Communications Ltd.
7  *      www.farsite.co.uk
8  *
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  *      Author:      R.J.Dunlop    <bob.dunlop@farsite.co.uk>
15  *      Maintainer:  Kevin Curtis  <kevin.curtis@farsite.co.uk>
16  */
17
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/version.h>
21 #include <linux/pci.h>
22 #include <linux/ioport.h>
23 #include <linux/init.h>
24 #include <linux/if.h>
25 #include <linux/hdlc.h>
26 #include <asm/io.h>
27 #include <asm/uaccess.h>
28
29 #include "farsync.h"
30
31 /*
32  *      Module info
33  */
34 MODULE_AUTHOR("R.J.Dunlop <bob.dunlop@farsite.co.uk>");
35 MODULE_DESCRIPTION("FarSync T-Series WAN driver. FarSite Communications Ltd.");
36 MODULE_PARM(fst_txq_low, "i");
37 MODULE_PARM(fst_txq_high, "i");
38 MODULE_PARM(fst_max_reads, "i");
39 MODULE_PARM(fst_excluded_cards, "i");
40 MODULE_PARM(fst_excluded_list, "0-32i");
41 MODULE_LICENSE("GPL");
42
43 /*      Driver configuration and global parameters
44  *      ==========================================
45  */
46
47 /*      Number of ports (per card) and cards supported
48  */
49 #define FST_MAX_PORTS           4
50 #define FST_MAX_CARDS           32
51
52 /*      Default parameters for the link
53  */
54 #define FST_TX_QUEUE_LEN        100     /* At 8Mbps a longer queue length is
55                                          * useful, the syncppp module forces
56                                          * this down assuming a slower line I
57                                          * guess.
58                                          */
59 #define FST_TXQ_DEPTH           16      /* This one is for the buffering
60                                          * of frames on the way down to the card
61                                          * so that we can keep the card busy
62                                          * and maximise throughput
63                                          */
64 #define FST_HIGH_WATER_MARK     12      /* Point at which we flow control
65                                          * network layer */
66 #define FST_LOW_WATER_MARK      8       /* Point at which we remove flow
67                                          * control from network layer */
68 #define FST_MAX_MTU             8000    /* Huge but possible */
69 #define FST_DEF_MTU             1500    /* Common sane value */
70
71 #define FST_TX_TIMEOUT          (2*HZ)
72
73 #ifdef ARPHRD_RAWHDLC
74 #define ARPHRD_MYTYPE   ARPHRD_RAWHDLC  /* Raw frames */
75 #else
76 #define ARPHRD_MYTYPE   ARPHRD_HDLC     /* Cisco-HDLC (keepalives etc) */
77 #endif
78
79 /*
80  * Modules parameters and associated varaibles
81  */
82 int fst_txq_low = FST_LOW_WATER_MARK;
83 int fst_txq_high = FST_HIGH_WATER_MARK;
84 int fst_max_reads = 7;
85 int fst_excluded_cards = 0;
86 int fst_excluded_list[FST_MAX_CARDS];
87
88 /*      Card shared memory layout
89  *      =========================
90  */
91 #pragma pack(1)
92
93 /*      This information is derived in part from the FarSite FarSync Smc.h
94  *      file. Unfortunately various name clashes and the non-portability of the
95  *      bit field declarations in that file have meant that I have chosen to
96  *      recreate the information here.
97  *
98  *      The SMC (Shared Memory Configuration) has a version number that is
99  *      incremented every time there is a significant change. This number can
100  *      be used to check that we have not got out of step with the firmware
101  *      contained in the .CDE files.
102  */
103 #define SMC_VERSION 24
104
105 #define FST_MEMSIZE 0x100000    /* Size of card memory (1Mb) */
106
107 #define SMC_BASE 0x00002000L    /* Base offset of the shared memory window main
108                                  * configuration structure */
109 #define BFM_BASE 0x00010000L    /* Base offset of the shared memory window DMA
110                                  * buffers */
111
112 #define LEN_TX_BUFFER 8192      /* Size of packet buffers */
113 #define LEN_RX_BUFFER 8192
114
115 #define LEN_SMALL_TX_BUFFER 256 /* Size of obsolete buffs used for DOS diags */
116 #define LEN_SMALL_RX_BUFFER 256
117
118 #define NUM_TX_BUFFER 2         /* Must be power of 2. Fixed by firmware */
119 #define NUM_RX_BUFFER 8
120
121 /* Interrupt retry time in milliseconds */
122 #define INT_RETRY_TIME 2
123
124 /*      The Am186CH/CC processors support a SmartDMA mode using circular pools
125  *      of buffer descriptors. The structure is almost identical to that used
126  *      in the LANCE Ethernet controllers. Details available as PDF from the
127  *      AMD web site: http://www.amd.com/products/epd/processors/\
128  *                    2.16bitcont/3.am186cxfa/a21914/21914.pdf
129  */
130 struct txdesc {                 /* Transmit descriptor */
131         volatile u16 ladr;      /* Low order address of packet. This is a
132                                  * linear address in the Am186 memory space
133                                  */
134         volatile u8 hadr;       /* High order address. Low 4 bits only, high 4
135                                  * bits must be zero
136                                  */
137         volatile u8 bits;       /* Status and config */
138         volatile u16 bcnt;      /* 2s complement of packet size in low 15 bits.
139                                  * Transmit terminal count interrupt enable in
140                                  * top bit.
141                                  */
142         u16 unused;             /* Not used in Tx */
143 };
144
145 struct rxdesc {                 /* Receive descriptor */
146         volatile u16 ladr;      /* Low order address of packet */
147         volatile u8 hadr;       /* High order address */
148         volatile u8 bits;       /* Status and config */
149         volatile u16 bcnt;      /* 2s complement of buffer size in low 15 bits.
150                                  * Receive terminal count interrupt enable in
151                                  * top bit.
152                                  */
153         volatile u16 mcnt;      /* Message byte count (15 bits) */
154 };
155
156 /* Convert a length into the 15 bit 2's complement */
157 /* #define cnv_bcnt(len)   (( ~(len) + 1 ) & 0x7FFF ) */
158 /* Since we need to set the high bit to enable the completion interrupt this
159  * can be made a lot simpler
160  */
161 #define cnv_bcnt(len)   (-(len))
162
163 /* Status and config bits for the above */
164 #define DMA_OWN         0x80    /* SmartDMA owns the descriptor */
165 #define TX_STP          0x02    /* Tx: start of packet */
166 #define TX_ENP          0x01    /* Tx: end of packet */
167 #define RX_ERR          0x40    /* Rx: error (OR of next 4 bits) */
168 #define RX_FRAM         0x20    /* Rx: framing error */
169 #define RX_OFLO         0x10    /* Rx: overflow error */
170 #define RX_CRC          0x08    /* Rx: CRC error */
171 #define RX_HBUF         0x04    /* Rx: buffer error */
172 #define RX_STP          0x02    /* Rx: start of packet */
173 #define RX_ENP          0x01    /* Rx: end of packet */
174
175 /* Interrupts from the card are caused by various events which are presented
176  * in a circular buffer as several events may be processed on one physical int
177  */
178 #define MAX_CIRBUFF     32
179
180 struct cirbuff {
181         u8 rdindex;             /* read, then increment and wrap */
182         u8 wrindex;             /* write, then increment and wrap */
183         u8 evntbuff[MAX_CIRBUFF];
184 };
185
186 /* Interrupt event codes.
187  * Where appropriate the two low order bits indicate the port number
188  */
189 #define CTLA_CHG        0x18    /* Control signal changed */
190 #define CTLB_CHG        0x19
191 #define CTLC_CHG        0x1A
192 #define CTLD_CHG        0x1B
193
194 #define INIT_CPLT       0x20    /* Initialisation complete */
195 #define INIT_FAIL       0x21    /* Initialisation failed */
196
197 #define ABTA_SENT       0x24    /* Abort sent */
198 #define ABTB_SENT       0x25
199 #define ABTC_SENT       0x26
200 #define ABTD_SENT       0x27
201
202 #define TXA_UNDF        0x28    /* Transmission underflow */
203 #define TXB_UNDF        0x29
204 #define TXC_UNDF        0x2A
205 #define TXD_UNDF        0x2B
206
207 #define F56_INT         0x2C
208 #define M32_INT         0x2D
209
210 #define TE1_ALMA        0x30
211
212 /* Port physical configuration. See farsync.h for field values */
213 struct port_cfg {
214         u16 lineInterface;      /* Physical interface type */
215         u8 x25op;               /* Unused at present */
216         u8 internalClock;       /* 1 => internal clock, 0 => external */
217         u8 transparentMode;     /* 1 => on, 0 => off */
218         u8 invertClock;         /* 0 => normal, 1 => inverted */
219         u8 padBytes[6];         /* Padding */
220         u32 lineSpeed;          /* Speed in bps */
221 };
222
223 /* TE1 port physical configuration */
224 struct su_config {
225         u32 dataRate;
226         u8 clocking;
227         u8 framing;
228         u8 structure;
229         u8 interface;
230         u8 coding;
231         u8 lineBuildOut;
232         u8 equalizer;
233         u8 transparentMode;
234         u8 loopMode;
235         u8 range;
236         u8 txBufferMode;
237         u8 rxBufferMode;
238         u8 startingSlot;
239         u8 losThreshold;
240         u8 enableIdleCode;
241         u8 idleCode;
242         u8 spare[44];
243 };
244
245 /* TE1 Status */
246 struct su_status {
247         u32 receiveBufferDelay;
248         u32 framingErrorCount;
249         u32 codeViolationCount;
250         u32 crcErrorCount;
251         u32 lineAttenuation;
252         u8 portStarted;
253         u8 lossOfSignal;
254         u8 receiveRemoteAlarm;
255         u8 alarmIndicationSignal;
256         u8 spare[40];
257 };
258
259 /* Finally sling all the above together into the shared memory structure.
260  * Sorry it's a hodge podge of arrays, structures and unused bits, it's been
261  * evolving under NT for some time so I guess we're stuck with it.
262  * The structure starts at offset SMC_BASE.
263  * See farsync.h for some field values.
264  */
265 struct fst_shared {
266         /* DMA descriptor rings */
267         struct rxdesc rxDescrRing[FST_MAX_PORTS][NUM_RX_BUFFER];
268         struct txdesc txDescrRing[FST_MAX_PORTS][NUM_TX_BUFFER];
269
270         /* Obsolete small buffers */
271         u8 smallRxBuffer[FST_MAX_PORTS][NUM_RX_BUFFER][LEN_SMALL_RX_BUFFER];
272         u8 smallTxBuffer[FST_MAX_PORTS][NUM_TX_BUFFER][LEN_SMALL_TX_BUFFER];
273
274         u8 taskStatus;          /* 0x00 => initialising, 0x01 => running,
275                                  * 0xFF => halted
276                                  */
277
278         u8 interruptHandshake;  /* Set to 0x01 by adapter to signal interrupt,
279                                  * set to 0xEE by host to acknowledge interrupt
280                                  */
281
282         u16 smcVersion;         /* Must match SMC_VERSION */
283
284         u32 smcFirmwareVersion; /* 0xIIVVRRBB where II = product ID, VV = major
285                                  * version, RR = revision and BB = build
286                                  */
287
288         u16 txa_done;           /* Obsolete completion flags */
289         u16 rxa_done;
290         u16 txb_done;
291         u16 rxb_done;
292         u16 txc_done;
293         u16 rxc_done;
294         u16 txd_done;
295         u16 rxd_done;
296
297         u16 mailbox[4];         /* Diagnostics mailbox. Not used */
298
299         struct cirbuff interruptEvent;  /* interrupt causes */
300
301         u32 v24IpSts[FST_MAX_PORTS];    /* V.24 control input status */
302         u32 v24OpSts[FST_MAX_PORTS];    /* V.24 control output status */
303
304         struct port_cfg portConfig[FST_MAX_PORTS];
305
306         u16 clockStatus[FST_MAX_PORTS]; /* lsb: 0=> present, 1=> absent */
307
308         u16 cableStatus;        /* lsb: 0=> present, 1=> absent */
309
310         u16 txDescrIndex[FST_MAX_PORTS];        /* transmit descriptor ring index */
311         u16 rxDescrIndex[FST_MAX_PORTS];        /* receive descriptor ring index */
312
313         u16 portMailbox[FST_MAX_PORTS][2];      /* command, modifier */
314         u16 cardMailbox[4];     /* Not used */
315
316         /* Number of times the card thinks the host has
317          * missed an interrupt by not acknowledging
318          * within 2mS (I guess NT has problems)
319          */
320         u32 interruptRetryCount;
321
322         /* Driver private data used as an ID. We'll not
323          * use this as I'd rather keep such things
324          * in main memory rather than on the PCI bus
325          */
326         u32 portHandle[FST_MAX_PORTS];
327
328         /* Count of Tx underflows for stats */
329         u32 transmitBufferUnderflow[FST_MAX_PORTS];
330
331         /* Debounced V.24 control input status */
332         u32 v24DebouncedSts[FST_MAX_PORTS];
333
334         /* Adapter debounce timers. Don't touch */
335         u32 ctsTimer[FST_MAX_PORTS];
336         u32 ctsTimerRun[FST_MAX_PORTS];
337         u32 dcdTimer[FST_MAX_PORTS];
338         u32 dcdTimerRun[FST_MAX_PORTS];
339
340         u32 numberOfPorts;      /* Number of ports detected at startup */
341
342         u16 _reserved[64];
343
344         u16 cardMode;           /* Bit-mask to enable features:
345                                  * Bit 0: 1 enables LED identify mode
346                                  */
347
348         u16 portScheduleOffset;
349
350         struct su_config suConfig;      /* TE1 Bits */
351         struct su_status suStatus;
352
353         u32 endOfSmcSignature;  /* endOfSmcSignature MUST be the last member of
354                                  * the structure and marks the end of shared
355                                  * memory. Adapter code initializes it as
356                                  * END_SIG.
357                                  */
358 };
359
360 /* endOfSmcSignature value */
361 #define END_SIG                 0x12345678
362
363 /* Mailbox values. (portMailbox) */
364 #define NOP             0       /* No operation */
365 #define ACK             1       /* Positive acknowledgement to PC driver */
366 #define NAK             2       /* Negative acknowledgement to PC driver */
367 #define STARTPORT       3       /* Start an HDLC port */
368 #define STOPPORT        4       /* Stop an HDLC port */
369 #define ABORTTX         5       /* Abort the transmitter for a port */
370 #define SETV24O         6       /* Set V24 outputs */
371
372 /* PLX Chip Register Offsets */
373 #define CNTRL_9052      0x50    /* Control Register */
374 #define CNTRL_9054      0x6c    /* Control Register */
375
376 #define INTCSR_9052     0x4c    /* Interrupt control/status register */
377 #define INTCSR_9054     0x68    /* Interrupt control/status register */
378
379 /* 9054 DMA Registers */
380 /*
381  * Note that we will be using DMA Channel 0 for copying rx data
382  * and Channel 1 for copying tx data
383  */
384 #define DMAMODE0        0x80
385 #define DMAPADR0        0x84
386 #define DMALADR0        0x88
387 #define DMASIZ0         0x8c
388 #define DMADPR0         0x90
389 #define DMAMODE1        0x94
390 #define DMAPADR1        0x98
391 #define DMALADR1        0x9c
392 #define DMASIZ1         0xa0
393 #define DMADPR1         0xa4
394 #define DMACSR0         0xa8
395 #define DMACSR1         0xa9
396 #define DMAARB          0xac
397 #define DMATHR          0xb0
398 #define DMADAC0         0xb4
399 #define DMADAC1         0xb8
400 #define DMAMARBR        0xac
401
402 #define FST_MIN_DMA_LEN 64
403 #define FST_RX_DMA_INT  0x01
404 #define FST_TX_DMA_INT  0x02
405 #define FST_CARD_INT    0x04
406
407 /* Larger buffers are positioned in memory at offset BFM_BASE */
408 struct buf_window {
409         u8 txBuffer[FST_MAX_PORTS][NUM_TX_BUFFER][LEN_TX_BUFFER];
410         u8 rxBuffer[FST_MAX_PORTS][NUM_RX_BUFFER][LEN_RX_BUFFER];
411 };
412
413 /* Calculate offset of a buffer object within the shared memory window */
414 #define BUF_OFFSET(X)   ((unsigned int)&(((struct buf_window *)BFM_BASE)->X))
415
416 #pragma pack()
417
418 /*      Device driver private information
419  *      =================================
420  */
421 /*      Per port (line or channel) information
422  */
423 struct fst_port_info {
424         struct net_device *dev; /* Device struct - must be first */
425         struct fst_card_info *card;     /* Card we're associated with */
426         int index;              /* Port index on the card */
427         int hwif;               /* Line hardware (lineInterface copy) */
428         int run;                /* Port is running */
429         int mode;               /* Normal or FarSync raw */
430         int rxpos;              /* Next Rx buffer to use */
431         int txpos;              /* Next Tx buffer to use */
432         int txipos;             /* Next Tx buffer to check for free */
433         int start;              /* Indication of start/stop to network */
434         /*
435          * A sixteen entry transmit queue
436          */
437         int txqs;               /* index to get next buffer to tx */
438         int txqe;               /* index to queue next packet */
439         struct sk_buff *txq[FST_TXQ_DEPTH];     /* The queue */
440         int rxqdepth;
441 };
442
443 /*      Per card information
444  */
445 struct fst_card_info {
446         char *mem;              /* Card memory mapped to kernel space */
447         char *ctlmem;           /* Control memory for PCI cards */
448         unsigned int phys_mem;  /* Physical memory window address */
449         unsigned int phys_ctlmem;       /* Physical control memory address */
450         unsigned int irq;       /* Interrupt request line number */
451         unsigned int nports;    /* Number of serial ports */
452         unsigned int type;      /* Type index of card */
453         unsigned int state;     /* State of card */
454         spinlock_t card_lock;   /* Lock for SMP access */
455         unsigned short pci_conf;        /* PCI card config in I/O space */
456         /* Per port info */
457         struct fst_port_info ports[FST_MAX_PORTS];
458         struct pci_dev *device; /* Information about the pci device */
459         int card_no;            /* Inst of the card on the system */
460         int family;             /* TxP or TxU */
461         int dmarx_in_progress;
462         int dmatx_in_progress;
463         unsigned long int_count;
464         unsigned long int_time_ave;
465         void *rx_dma_handle_host;
466         dma_addr_t rx_dma_handle_card;
467         void *tx_dma_handle_host;
468         dma_addr_t tx_dma_handle_card;
469         struct sk_buff *dma_skb_rx;
470         struct fst_port_info *dma_port_rx;
471         struct fst_port_info *dma_port_tx;
472         int dma_len_rx;
473         int dma_len_tx;
474         int dma_txpos;
475         int dma_rxpos;
476 };
477
478 /* Convert an HDLC device pointer into a port info pointer and similar */
479 #define dev_to_port(D)  (dev_to_hdlc(D)->priv)
480 #define port_to_dev(P)  ((P)->dev)
481
482
483 /*
484  *      Shared memory window access macros
485  *
486  *      We have a nice memory based structure above, which could be directly
487  *      mapped on i386 but might not work on other architectures unless we use
488  *      the readb,w,l and writeb,w,l macros. Unfortunately these macros take
489  *      physical offsets so we have to convert. The only saving grace is that
490  *      this should all collapse back to a simple indirection eventually.
491  */
492 #define WIN_OFFSET(X)   ((long)&(((struct fst_shared *)SMC_BASE)->X))
493
494 #define FST_RDB(C,E)    readb ((C)->mem + WIN_OFFSET(E))
495 #define FST_RDW(C,E)    readw ((C)->mem + WIN_OFFSET(E))
496 #define FST_RDL(C,E)    readl ((C)->mem + WIN_OFFSET(E))
497
498 #define FST_WRB(C,E,B)  writeb ((B), (C)->mem + WIN_OFFSET(E))
499 #define FST_WRW(C,E,W)  writew ((W), (C)->mem + WIN_OFFSET(E))
500 #define FST_WRL(C,E,L)  writel ((L), (C)->mem + WIN_OFFSET(E))
501
502 /*
503  *      Debug support
504  */
505 #if FST_DEBUG
506
507 static int fst_debug_mask = { FST_DEBUG };
508
509 /* Most common debug activity is to print something if the corresponding bit
510  * is set in the debug mask. Note: this uses a non-ANSI extension in GCC to
511  * support variable numbers of macro parameters. The inverted if prevents us
512  * eating someone else's else clause.
513  */
514 #define dbg(F,fmt,A...) if ( ! ( fst_debug_mask & (F))) \
515                                 ; \
516                         else \
517                                 printk ( KERN_DEBUG FST_NAME ": " fmt, ## A )
518
519 #else
520 #define dbg(X...)               /* NOP */
521 #endif
522
523 /*      Printing short cuts
524  */
525 #define printk_err(fmt,A...)    printk ( KERN_ERR     FST_NAME ": " fmt, ## A )
526 #define printk_warn(fmt,A...)   printk ( KERN_WARNING FST_NAME ": " fmt, ## A )
527 #define printk_info(fmt,A...)   printk ( KERN_INFO    FST_NAME ": " fmt, ## A )
528
529 /*
530  *      PCI ID lookup table
531  */
532 static struct pci_device_id fst_pci_dev_id[] __devinitdata = {
533         {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_T2P, PCI_ANY_ID, 
534          PCI_ANY_ID, 0, 0, FST_TYPE_T2P},
535
536         {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_T4P, PCI_ANY_ID, 
537          PCI_ANY_ID, 0, 0, FST_TYPE_T4P},
538
539         {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_T1U, PCI_ANY_ID, 
540          PCI_ANY_ID, 0, 0, FST_TYPE_T1U},
541
542         {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_T2U, PCI_ANY_ID, 
543          PCI_ANY_ID, 0, 0, FST_TYPE_T2U},
544
545         {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_T4U, PCI_ANY_ID, 
546          PCI_ANY_ID, 0, 0, FST_TYPE_T4U},
547
548         {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_TE1, PCI_ANY_ID, 
549          PCI_ANY_ID, 0, 0, FST_TYPE_TE1},
550
551         {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_TE1C, PCI_ANY_ID, 
552          PCI_ANY_ID, 0, 0, FST_TYPE_TE1},
553         {0,}                    /* End */
554 };
555
556 MODULE_DEVICE_TABLE(pci, fst_pci_dev_id);
557
558 /*
559  *      Device Driver Work Queues
560  *
561  *      So that we don't spend too much time processing events in the 
562  *      Interrupt Service routine, we will declare a work queue per Card 
563  *      and make the ISR schedule a task in the queue for later execution.
564  *      In the 2.4 Kernel we used to use the immediate queue for BH's
565  *      Now that they are gone, tasklets seem to be much better than work 
566  *      queues.
567  */
568
569 static void do_bottom_half_tx(struct fst_card_info *card);
570 static void do_bottom_half_rx(struct fst_card_info *card);
571 static void fst_process_tx_work_q(unsigned long work_q);
572 static void fst_process_int_work_q(unsigned long work_q);
573
574 DECLARE_TASKLET(fst_tx_task, fst_process_tx_work_q, 0);
575 DECLARE_TASKLET(fst_int_task, fst_process_int_work_q, 0);
576
577 struct fst_card_info *fst_card_array[FST_MAX_CARDS];
578 spinlock_t fst_work_q_lock;
579 u64 fst_work_txq;
580 u64 fst_work_intq;
581
582 static void
583 fst_q_work_item(u64 * queue, int card_index)
584 {
585         unsigned long flags;
586         u64 mask;
587
588         /*
589          * Grab the queue exclusively
590          */
591         spin_lock_irqsave(&fst_work_q_lock, flags);
592
593         /*
594          * Making an entry in the queue is simply a matter of setting
595          * a bit for the card indicating that there is work to do in the
596          * bottom half for the card.  Note the limitation of 64 cards.
597          * That ought to be enough
598          */
599         mask = 1 << card_index;
600         *queue |= mask;
601         spin_unlock_irqrestore(&fst_work_q_lock, flags);
602 }
603
604 static void
605 fst_process_tx_work_q(unsigned long /*void **/work_q)
606 {
607         unsigned long flags;
608         u64 work_txq;
609         int i;
610
611         /*
612          * Grab the queue exclusively
613          */
614         dbg(DBG_TX, "fst_process_tx_work_q\n");
615         spin_lock_irqsave(&fst_work_q_lock, flags);
616         work_txq = fst_work_txq;
617         fst_work_txq = 0;
618         spin_unlock_irqrestore(&fst_work_q_lock, flags);
619
620         /*
621          * Call the bottom half for each card with work waiting
622          */
623         for (i = 0; i < FST_MAX_CARDS; i++) {
624                 if (work_txq & 0x01) {
625                         if (fst_card_array[i] != NULL) {
626                                 dbg(DBG_TX, "Calling tx bh for card %d\n", i);
627                                 do_bottom_half_tx(fst_card_array[i]);
628                         }
629                 }
630                 work_txq = work_txq >> 1;
631         }
632 }
633
634 static void
635 fst_process_int_work_q(unsigned long /*void **/work_q)
636 {
637         unsigned long flags;
638         u64 work_intq;
639         int i;
640
641         /*
642          * Grab the queue exclusively
643          */
644         dbg(DBG_INTR, "fst_process_int_work_q\n");
645         spin_lock_irqsave(&fst_work_q_lock, flags);
646         work_intq = fst_work_intq;
647         fst_work_intq = 0;
648         spin_unlock_irqrestore(&fst_work_q_lock, flags);
649
650         /*
651          * Call the bottom half for each card with work waiting
652          */
653         for (i = 0; i < FST_MAX_CARDS; i++) {
654                 if (work_intq & 0x01) {
655                         if (fst_card_array[i] != NULL) {
656                                 dbg(DBG_INTR,
657                                     "Calling rx & tx bh for card %d\n", i);
658                                 do_bottom_half_rx(fst_card_array[i]);
659                                 do_bottom_half_tx(fst_card_array[i]);
660                         }
661                 }
662                 work_intq = work_intq >> 1;
663         }
664 }
665
666 /*      Card control functions
667  *      ======================
668  */
669 /*      Place the processor in reset state
670  *
671  * Used to be a simple write to card control space but a glitch in the latest
672  * AMD Am186CH processor means that we now have to do it by asserting and de-
673  * asserting the PLX chip PCI Adapter Software Reset. Bit 30 in CNTRL register
674  * at offset 9052_CNTRL.  Note the updates for the TXU.
675  */
676 static inline void
677 fst_cpureset(struct fst_card_info *card)
678 {
679         unsigned char interrupt_line_register;
680         unsigned long j = jiffies + 1;
681         unsigned int regval;
682
683         if (card->family == FST_FAMILY_TXU) {
684                 if (pci_read_config_byte
685                     (card->device, PCI_INTERRUPT_LINE, &interrupt_line_register)) {
686                         dbg(DBG_ASS,
687                             "Error in reading interrupt line register\n");
688                 }
689                 /*
690                  * Assert PLX software reset and Am186 hardware reset
691                  * and then deassert the PLX software reset but 186 still in reset
692                  */
693                 outw(0x440f, card->pci_conf + CNTRL_9054 + 2);
694                 outw(0x040f, card->pci_conf + CNTRL_9054 + 2);
695                 /*
696                  * We are delaying here to allow the 9054 to reset itself
697                  */
698                 j = jiffies + 1;
699                 while (jiffies < j)
700                         /* Do nothing */ ;
701                 outw(0x240f, card->pci_conf + CNTRL_9054 + 2);
702                 /*
703                  * We are delaying here to allow the 9054 to reload its eeprom
704                  */
705                 j = jiffies + 1;
706                 while (jiffies < j)
707                         /* Do nothing */ ;
708                 outw(0x040f, card->pci_conf + CNTRL_9054 + 2);
709
710                 if (pci_write_config_byte
711                     (card->device, PCI_INTERRUPT_LINE, interrupt_line_register)) {
712                         dbg(DBG_ASS,
713                             "Error in writing interrupt line register\n");
714                 }
715
716         } else {
717                 regval = inl(card->pci_conf + CNTRL_9052);
718
719                 outl(regval | 0x40000000, card->pci_conf + CNTRL_9052);
720                 outl(regval & ~0x40000000, card->pci_conf + CNTRL_9052);
721         }
722 }
723
724 /*      Release the processor from reset
725  */
726 static inline void
727 fst_cpurelease(struct fst_card_info *card)
728 {
729         if (card->family == FST_FAMILY_TXU) {
730                 /*
731                  * Force posted writes to complete
732                  */
733                 (void) readb(card->mem);
734
735                 /*
736                  * Release LRESET DO = 1
737                  * Then release Local Hold, DO = 1
738                  */
739                 outw(0x040e, card->pci_conf + CNTRL_9054 + 2);
740                 outw(0x040f, card->pci_conf + CNTRL_9054 + 2);
741         } else {
742                 (void) readb(card->ctlmem);
743         }
744 }
745
746 /*      Clear the cards interrupt flag
747  */
748 static inline void
749 fst_clear_intr(struct fst_card_info *card)
750 {
751         if (card->family == FST_FAMILY_TXU) {
752                 (void) readb(card->ctlmem);
753         } else {
754                 /* Poke the appropriate PLX chip register (same as enabling interrupts)
755                  */
756                 outw(0x0543, card->pci_conf + INTCSR_9052);
757         }
758 }
759
760 /*      Enable card interrupts
761  */
762 static inline void
763 fst_enable_intr(struct fst_card_info *card)
764 {
765         if (card->family == FST_FAMILY_TXU) {
766                 outl(0x0f0c0900, card->pci_conf + INTCSR_9054);
767         } else {
768                 outw(0x0543, card->pci_conf + INTCSR_9052);
769         }
770 }
771
772 /*      Disable card interrupts
773  */
774 static inline void
775 fst_disable_intr(struct fst_card_info *card)
776 {
777         if (card->family == FST_FAMILY_TXU) {
778                 outl(0x00000000, card->pci_conf + INTCSR_9054);
779         } else {
780                 outw(0x0000, card->pci_conf + INTCSR_9052);
781         }
782 }
783
784 /*      Process the result of trying to pass a recieved frame up the stack
785  */
786 static void
787 fst_process_rx_status(int rx_status, char *name)
788 {
789         switch (rx_status) {
790         case NET_RX_SUCCESS:
791                 {
792                         /*
793                          * Nothing to do here
794                          */
795                         break;
796                 }
797
798         case NET_RX_CN_LOW:
799                 {
800                         dbg(DBG_ASS, "%s: Receive Low Congestion\n", name);
801                         break;
802                 }
803
804         case NET_RX_CN_MOD:
805                 {
806                         dbg(DBG_ASS, "%s: Receive Moderate Congestion\n", name);
807                         break;
808                 }
809
810         case NET_RX_CN_HIGH:
811                 {
812                         dbg(DBG_ASS, "%s: Receive High Congestion\n", name);
813                         break;
814                 }
815
816         case NET_RX_DROP:
817                 {
818                         dbg(DBG_ASS, "%s: Received packet dropped\n", name);
819                         break;
820                 }
821         }
822 }
823
824 /*      Initilaise DMA for PLX 9054
825  */
826 static inline void
827 fst_init_dma(struct fst_card_info *card)
828 {
829         /*
830          * This is only required for the PLX 9054
831          */
832         if (card->family == FST_FAMILY_TXU) {
833                 pci_set_master(card->device);
834                 outl(0x00020441, card->pci_conf + DMAMODE0);
835                 outl(0x00020441, card->pci_conf + DMAMODE1);
836                 outl(0x0, card->pci_conf + DMATHR);
837         }
838 }
839
840 /*      Tx dma complete interrupt
841  */
842 static void
843 fst_tx_dma_complete(struct fst_card_info *card, struct fst_port_info *port,
844                     int len, int txpos)
845 {
846         struct net_device *dev = port_to_dev(port);
847         struct net_device_stats *stats = hdlc_stats(dev);
848
849         /*
850          * Everything is now set, just tell the card to go
851          */
852         dbg(DBG_TX, "fst_tx_dma_complete\n");
853         FST_WRB(card, txDescrRing[port->index][txpos].bits,
854                 DMA_OWN | TX_STP | TX_ENP);
855         stats->tx_packets++;
856         stats->tx_bytes += len;
857         dev->trans_start = jiffies;
858 }
859
860 /*      Rx dma complete interrupt
861  */
862 static void
863 fst_rx_dma_complete(struct fst_card_info *card, struct fst_port_info *port,
864                     int len, struct sk_buff *skb, int rxp)
865 {
866         struct net_device *dev = port_to_dev(port);
867         struct net_device_stats *stats = hdlc_stats(dev);
868         int pi;
869         int rx_status;
870
871         dbg(DBG_TX, "fst_rx_dma_complete\n");
872         pi = port->index;
873         memcpy(skb_put(skb, len), card->rx_dma_handle_host, len);
874
875         /* Reset buffer descriptor */
876         FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
877
878         /* Update stats */
879         stats->rx_packets++;
880         stats->rx_bytes += len;
881
882         /* Push upstream */
883         dbg(DBG_RX, "Pushing the frame up the stack\n");
884         skb->mac.raw = skb->data;
885         skb->dev = dev;
886         if (port->mode == FST_RAW) {
887                 /*
888                  * Mark it for our own raw sockets interface
889                  */
890                 skb->protocol = htons(ETH_P_CUST);
891                 skb->pkt_type = PACKET_HOST;
892         } else {
893                 skb->protocol = hdlc_type_trans(skb, skb->dev);
894         }
895         rx_status = netif_rx(skb);
896         fst_process_rx_status(rx_status, port_to_dev(port)->name);
897         if (rx_status == NET_RX_DROP)
898                 stats->rx_dropped++;
899         dev->last_rx = jiffies;
900 }
901
902 /*
903  *      Receive a frame through the DMA
904  */
905 static inline void
906 fst_rx_dma(struct fst_card_info *card, unsigned char *skb,
907            unsigned char *mem, int len)
908 {
909         /*
910          * This routine will setup the DMA and start it
911          */
912
913         dbg(DBG_RX, "In fst_rx_dma %p %p %d\n", skb, mem, len);
914         if (card->dmarx_in_progress) {
915                 dbg(DBG_ASS, "In fst_rx_dma while dma in progress\n");
916         }
917
918         outl((unsigned long) skb, card->pci_conf + DMAPADR0);   /* Copy to here */
919         outl((unsigned long) mem, card->pci_conf + DMALADR0);   /* from here */
920         outl(len, card->pci_conf + DMASIZ0);    /* for this length */
921         outl(0x00000000c, card->pci_conf + DMADPR0);    /* In this direction */
922
923         /*
924          * We use the dmarx_in_progress flag to flag the channel as busy
925          */
926         card->dmarx_in_progress = 1;
927         outb(0x03, card->pci_conf + DMACSR0);   /* Start the transfer */
928 }
929
930 /*
931  *      Send a frame through the DMA
932  */
933 static inline void
934 fst_tx_dma(struct fst_card_info *card, unsigned char *skb,
935            unsigned char *mem, int len)
936 {
937         /*
938          * This routine will setup the DMA and start it.
939          */
940
941         dbg(DBG_TX, "In fst_tx_dma %p %p %d\n", skb, mem, len);
942         if (card->dmatx_in_progress) {
943                 dbg(DBG_ASS, "In fst_tx_dma while dma in progress\n");
944         }
945
946         outl((unsigned long) skb, card->pci_conf + DMAPADR1);   /* Copy from here */
947         outl((unsigned long) mem, card->pci_conf + DMALADR1);   /* to here */
948         outl(len, card->pci_conf + DMASIZ1);    /* for this length */
949         outl(0x000000004, card->pci_conf + DMADPR1);    /* In this direction */
950
951         /*
952          * We use the dmatx_in_progress to flag the channel as busy
953          */
954         card->dmatx_in_progress = 1;
955         outb(0x03, card->pci_conf + DMACSR1);   /* Start the transfer */
956 }
957
958 /*      Issue a Mailbox command for a port.
959  *      Note we issue them on a fire and forget basis, not expecting to see an
960  *      error and not waiting for completion.
961  */
962 static void
963 fst_issue_cmd(struct fst_port_info *port, unsigned short cmd)
964 {
965         struct fst_card_info *card;
966         unsigned short mbval;
967         unsigned long flags;
968         int safety;
969
970         card = port->card;
971         spin_lock_irqsave(&card->card_lock, flags);
972         mbval = FST_RDW(card, portMailbox[port->index][0]);
973
974         safety = 0;
975         /* Wait for any previous command to complete */
976         while (mbval > NAK) {
977                 spin_unlock_irqrestore(&card->card_lock, flags);
978                 schedule_timeout(1);
979                 spin_lock_irqsave(&card->card_lock, flags);
980
981                 if (++safety > 2000) {
982                         printk_err("Mailbox safety timeout\n");
983                         break;
984                 }
985
986                 mbval = FST_RDW(card, portMailbox[port->index][0]);
987         }
988         if (safety > 0) {
989                 dbg(DBG_CMD, "Mailbox clear after %d jiffies\n", safety);
990         }
991         if (mbval == NAK) {
992                 dbg(DBG_CMD, "issue_cmd: previous command was NAK'd\n");
993         }
994
995         FST_WRW(card, portMailbox[port->index][0], cmd);
996
997         if (cmd == ABORTTX || cmd == STARTPORT) {
998                 port->txpos = 0;
999                 port->txipos = 0;
1000                 port->start = 0;
1001         }
1002
1003         spin_unlock_irqrestore(&card->card_lock, flags);
1004 }
1005
1006 /*      Port output signals control
1007  */
1008 static inline void
1009 fst_op_raise(struct fst_port_info *port, unsigned int outputs)
1010 {
1011         outputs |= FST_RDL(port->card, v24OpSts[port->index]);
1012         FST_WRL(port->card, v24OpSts[port->index], outputs);
1013
1014         if (port->run)
1015                 fst_issue_cmd(port, SETV24O);
1016 }
1017
1018 static inline void
1019 fst_op_lower(struct fst_port_info *port, unsigned int outputs)
1020 {
1021         outputs = ~outputs & FST_RDL(port->card, v24OpSts[port->index]);
1022         FST_WRL(port->card, v24OpSts[port->index], outputs);
1023
1024         if (port->run)
1025                 fst_issue_cmd(port, SETV24O);
1026 }
1027
1028 /*
1029  *      Setup port Rx buffers
1030  */
1031 static void
1032 fst_rx_config(struct fst_port_info *port)
1033 {
1034         int i;
1035         int pi;
1036         unsigned int offset;
1037         unsigned long flags;
1038         struct fst_card_info *card;
1039
1040         pi = port->index;
1041         card = port->card;
1042         spin_lock_irqsave(&card->card_lock, flags);
1043         for (i = 0; i < NUM_RX_BUFFER; i++) {
1044                 offset = BUF_OFFSET(rxBuffer[pi][i][0]);
1045
1046                 FST_WRW(card, rxDescrRing[pi][i].ladr, (u16) offset);
1047                 FST_WRB(card, rxDescrRing[pi][i].hadr, (u8) (offset >> 16));
1048                 FST_WRW(card, rxDescrRing[pi][i].bcnt, cnv_bcnt(LEN_RX_BUFFER));
1049                 FST_WRW(card, rxDescrRing[pi][i].mcnt, LEN_RX_BUFFER);
1050                 FST_WRB(card, rxDescrRing[pi][i].bits, DMA_OWN);
1051         }
1052         port->rxpos = 0;
1053         spin_unlock_irqrestore(&card->card_lock, flags);
1054 }
1055
1056 /*
1057  *      Setup port Tx buffers
1058  */
1059 static void
1060 fst_tx_config(struct fst_port_info *port)
1061 {
1062         int i;
1063         int pi;
1064         unsigned int offset;
1065         unsigned long flags;
1066         struct fst_card_info *card;
1067
1068         pi = port->index;
1069         card = port->card;
1070         spin_lock_irqsave(&card->card_lock, flags);
1071         for (i = 0; i < NUM_TX_BUFFER; i++) {
1072                 offset = BUF_OFFSET(txBuffer[pi][i][0]);
1073
1074                 FST_WRW(card, txDescrRing[pi][i].ladr, (u16) offset);
1075                 FST_WRB(card, txDescrRing[pi][i].hadr, (u8) (offset >> 16));
1076                 FST_WRW(card, txDescrRing[pi][i].bcnt, 0);
1077                 FST_WRB(card, txDescrRing[pi][i].bits, 0);
1078         }
1079         port->txpos = 0;
1080         port->txipos = 0;
1081         port->start = 0;
1082         spin_unlock_irqrestore(&card->card_lock, flags);
1083 }
1084
1085 /*      TE1 Alarm change interrupt event
1086  */
1087 static void
1088 fst_intr_te1_alarm(struct fst_card_info *card, struct fst_port_info *port)
1089 {
1090         u8 los;
1091         u8 rra;
1092         u8 ais;
1093
1094         los = FST_RDB(card, suStatus.lossOfSignal);
1095         rra = FST_RDB(card, suStatus.receiveRemoteAlarm);
1096         ais = FST_RDB(card, suStatus.alarmIndicationSignal);
1097
1098         if (los) {
1099                 /*
1100                  * Lost the link
1101                  */
1102                 if (netif_carrier_ok(port_to_dev(port))) {
1103                         dbg(DBG_INTR, "Net carrier off\n");
1104                         netif_carrier_off(port_to_dev(port));
1105                 }
1106         } else {
1107                 /*
1108                  * Link available
1109                  */
1110                 if (!netif_carrier_ok(port_to_dev(port))) {
1111                         dbg(DBG_INTR, "Net carrier on\n");
1112                         netif_carrier_on(port_to_dev(port));
1113                 }
1114         }
1115
1116         if (los)
1117                 dbg(DBG_INTR, "Assert LOS Alarm\n");
1118         else
1119                 dbg(DBG_INTR, "De-assert LOS Alarm\n");
1120         if (rra)
1121                 dbg(DBG_INTR, "Assert RRA Alarm\n");
1122         else
1123                 dbg(DBG_INTR, "De-assert RRA Alarm\n");
1124
1125         if (ais)
1126                 dbg(DBG_INTR, "Assert AIS Alarm\n");
1127         else
1128                 dbg(DBG_INTR, "De-assert AIS Alarm\n");
1129 }
1130
1131 /*      Control signal change interrupt event
1132  */
1133 static void
1134 fst_intr_ctlchg(struct fst_card_info *card, struct fst_port_info *port)
1135 {
1136         int signals;
1137
1138         signals = FST_RDL(card, v24DebouncedSts[port->index]);
1139
1140         if (signals & (((port->hwif == X21) || (port->hwif == X21D))
1141                        ? IPSTS_INDICATE : IPSTS_DCD)) {
1142                 if (!netif_carrier_ok(port_to_dev(port))) {
1143                         dbg(DBG_INTR, "DCD active\n");
1144                         netif_carrier_on(port_to_dev(port));
1145                 }
1146         } else {
1147                 if (netif_carrier_ok(port_to_dev(port))) {
1148                         dbg(DBG_INTR, "DCD lost\n");
1149                         netif_carrier_off(port_to_dev(port));
1150                 }
1151         }
1152 }
1153
1154 /*      Log Rx Errors
1155  */
1156 static void
1157 fst_log_rx_error(struct fst_card_info *card, struct fst_port_info *port,
1158                  unsigned char dmabits, int rxp, unsigned short len)
1159 {
1160         struct net_device *dev = port_to_dev(port);
1161         struct net_device_stats *stats = hdlc_stats(dev);
1162
1163         /* 
1164          * Increment the appropriate error counter
1165          */
1166         stats->rx_errors++;
1167         if (dmabits & RX_OFLO) {
1168                 stats->rx_fifo_errors++;
1169                 dbg(DBG_ASS, "Rx fifo error on card %d port %d buffer %d\n",
1170                     card->card_no, port->index, rxp);
1171         }
1172         if (dmabits & RX_CRC) {
1173                 stats->rx_crc_errors++;
1174                 dbg(DBG_ASS, "Rx crc error on card %d port %d\n",
1175                     card->card_no, port->index);
1176         }
1177         if (dmabits & RX_FRAM) {
1178                 stats->rx_frame_errors++;
1179                 dbg(DBG_ASS, "Rx frame error on card %d port %d\n",
1180                     card->card_no, port->index);
1181         }
1182         if (dmabits == (RX_STP | RX_ENP)) {
1183                 stats->rx_length_errors++;
1184                 dbg(DBG_ASS, "Rx length error (%d) on card %d port %d\n",
1185                     len, card->card_no, port->index);
1186         }
1187 }
1188
1189 /*      Rx Error Recovery
1190  */
1191 static void
1192 fst_recover_rx_error(struct fst_card_info *card, struct fst_port_info *port,
1193                      unsigned char dmabits, int rxp, unsigned short len)
1194 {
1195         int i;
1196         int pi;
1197
1198         pi = port->index;
1199         /* 
1200          * Discard buffer descriptors until we see the start of the
1201          * next frame.  Note that for long frames this could be in
1202          * a subsequent interrupt. 
1203          */
1204         i = 0;
1205         while ((dmabits & (DMA_OWN | RX_STP)) == 0) {
1206                 FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
1207                 rxp = (rxp+1) % NUM_RX_BUFFER;
1208                 if (++i > NUM_RX_BUFFER) {
1209                         dbg(DBG_ASS, "intr_rx: Discarding more bufs"
1210                             " than we have\n");
1211                         break;
1212                 }
1213                 dmabits = FST_RDB(card, rxDescrRing[pi][rxp].bits);
1214                 dbg(DBG_ASS, "DMA Bits of next buffer was %x\n", dmabits);
1215         }
1216         dbg(DBG_ASS, "There were %d subsequent buffers in error\n", i);
1217
1218         /* Discard the terminal buffer */
1219         if (!(dmabits & DMA_OWN)) {
1220                 FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
1221                 rxp = (rxp+1) % NUM_RX_BUFFER;
1222         }
1223         port->rxpos = rxp;
1224         return;
1225
1226 }
1227
1228 /*      Rx complete interrupt
1229  */
1230 static void
1231 fst_intr_rx(struct fst_card_info *card, struct fst_port_info *port)
1232 {
1233         unsigned char dmabits;
1234         int pi;
1235         int rxp;
1236         int rx_status;
1237         unsigned short len;
1238         struct sk_buff *skb;
1239         struct net_device *dev = port_to_dev(port);
1240         struct net_device_stats *stats = hdlc_stats(dev);
1241
1242         /* Check we have a buffer to process */
1243         pi = port->index;
1244         rxp = port->rxpos;
1245         dmabits = FST_RDB(card, rxDescrRing[pi][rxp].bits);
1246         if (dmabits & DMA_OWN) {
1247                 dbg(DBG_RX | DBG_INTR, "intr_rx: No buffer port %d pos %d\n",
1248                     pi, rxp);
1249                 return;
1250         }
1251         if (card->dmarx_in_progress) {
1252                 return;
1253         }
1254
1255         /* Get buffer length */
1256         len = FST_RDW(card, rxDescrRing[pi][rxp].mcnt);
1257         /* Discard the CRC */
1258         len -= 2;
1259         if (len == 0) {
1260                 /*
1261                  * This seems to happen on the TE1 interface sometimes
1262                  * so throw the frame away and log the event.
1263                  */
1264                 printk_err("Frame received with 0 length. Card %d Port %d\n",
1265                            card->card_no, port->index);
1266                 /* Return descriptor to card */
1267                 FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
1268
1269                 rxp = (rxp+1) % NUM_RX_BUFFER;
1270                 port->rxpos = rxp;
1271                 return;
1272         }
1273
1274         /* Check buffer length and for other errors. We insist on one packet
1275          * in one buffer. This simplifies things greatly and since we've
1276          * allocated 8K it shouldn't be a real world limitation
1277          */
1278         dbg(DBG_RX, "intr_rx: %d,%d: flags %x len %d\n", pi, rxp, dmabits, len);
1279         if (dmabits != (RX_STP | RX_ENP) || len > LEN_RX_BUFFER - 2) {
1280                 fst_log_rx_error(card, port, dmabits, rxp, len);
1281                 fst_recover_rx_error(card, port, dmabits, rxp, len);
1282                 return;
1283         }
1284
1285         /* Allocate SKB */
1286         if ((skb = dev_alloc_skb(len)) == NULL) {
1287                 dbg(DBG_RX, "intr_rx: can't allocate buffer\n");
1288
1289                 stats->rx_dropped++;
1290
1291                 /* Return descriptor to card */
1292                 FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
1293
1294                 rxp = (rxp+1) % NUM_RX_BUFFER;
1295                 port->rxpos = rxp;
1296                 return;
1297         }
1298
1299         /*
1300          * We know the length we need to receive, len.
1301          * It's not worth using the DMA for reads of less than
1302          * FST_MIN_DMA_LEN
1303          */
1304
1305         if ((len < FST_MIN_DMA_LEN) || (card->family == FST_FAMILY_TXP)) {
1306                 memcpy_fromio(skb_put(skb, len),
1307                               card->mem + BUF_OFFSET(rxBuffer[pi][rxp][0]),
1308                               len);
1309
1310                 /* Reset buffer descriptor */
1311                 FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
1312
1313                 /* Update stats */
1314                 stats->rx_packets++;
1315                 stats->rx_bytes += len;
1316
1317                 /* Push upstream */
1318                 dbg(DBG_RX, "Pushing frame up the stack\n");
1319                 skb->mac.raw = skb->data;
1320                 skb->dev = dev;
1321                 if (port->mode == FST_RAW) {
1322                         /*
1323                          * Mark it for our own raw sockets interface
1324                          */
1325                         skb->protocol = htons(ETH_P_CUST);
1326                         skb->pkt_type = PACKET_HOST;
1327                 } else {
1328                         skb->protocol = hdlc_type_trans(skb, skb->dev);
1329                 }
1330                 rx_status = netif_rx(skb);
1331                 fst_process_rx_status(rx_status, port_to_dev(port)->name);
1332                 if (rx_status == NET_RX_DROP) {
1333                         stats->rx_dropped++;
1334                 }
1335                 dev->last_rx = jiffies;
1336         } else {
1337                 card->dma_skb_rx = skb;
1338                 card->dma_port_rx = port;
1339                 card->dma_len_rx = len;
1340                 card->dma_rxpos = rxp;
1341                 fst_rx_dma(card, (char *) card->rx_dma_handle_card,
1342                            (char *) BUF_OFFSET(rxBuffer[pi][rxp][0]), len);
1343         }
1344         if (rxp != port->rxpos) {
1345                 dbg(DBG_ASS, "About to increment rxpos by more than 1\n");
1346                 dbg(DBG_ASS, "rxp = %d rxpos = %d\n", rxp, port->rxpos);
1347         }
1348         rxp = (rxp+1) % NUM_RX_BUFFER;
1349         port->rxpos = rxp;
1350 }
1351
1352 /*
1353  *      The bottom halfs to the ISR
1354  *
1355  */
1356
1357 static void
1358 do_bottom_half_tx(struct fst_card_info *card)
1359 {
1360         struct fst_port_info *port;
1361         int pi;
1362         int txq_length;
1363         struct sk_buff *skb;
1364         unsigned long flags;
1365         struct net_device *dev;
1366         struct net_device_stats *stats;
1367
1368         /*
1369          *  Find a free buffer for the transmit
1370          *  Step through each port on this card
1371          */
1372
1373         dbg(DBG_TX, "do_bottom_half_tx\n");
1374         for (pi = 0, port = card->ports; pi < card->nports; pi++, port++) {
1375                 if (!port->run)
1376                         continue;
1377
1378                 dev = port_to_dev(port);
1379                 stats = hdlc_stats(dev);
1380                 while (!
1381                        (FST_RDB(card, txDescrRing[pi][port->txpos].bits) &
1382                         DMA_OWN)
1383                        && !(card->dmatx_in_progress)) {
1384                         /*
1385                          * There doesn't seem to be a txdone event per-se
1386                          * We seem to have to deduce it, by checking the DMA_OWN
1387                          * bit on the next buffer we think we can use
1388                          */
1389                         spin_lock_irqsave(&card->card_lock, flags);
1390                         if ((txq_length = port->txqe - port->txqs) < 0) {
1391                                 /*
1392                                  * This is the case where one has wrapped and the
1393                                  * maths gives us a negative number
1394                                  */
1395                                 txq_length = txq_length + FST_TXQ_DEPTH;
1396                         }
1397                         spin_unlock_irqrestore(&card->card_lock, flags);
1398                         if (txq_length > 0) {
1399                                 /*
1400                                  * There is something to send
1401                                  */
1402                                 spin_lock_irqsave(&card->card_lock, flags);
1403                                 skb = port->txq[port->txqs];
1404                                 port->txqs++;
1405                                 if (port->txqs == FST_TXQ_DEPTH) {
1406                                         port->txqs = 0;
1407                                 }
1408                                 spin_unlock_irqrestore(&card->card_lock, flags);
1409                                 /*
1410                                  * copy the data and set the required indicators on the
1411                                  * card.
1412                                  */
1413                                 FST_WRW(card, txDescrRing[pi][port->txpos].bcnt,
1414                                         cnv_bcnt(skb->len));
1415                                 if ((skb->len < FST_MIN_DMA_LEN)
1416                                     || (card->family == FST_FAMILY_TXP)) {
1417                                         /* Enqueue the packet with normal io */
1418                                         memcpy_toio(card->mem +
1419                                                     BUF_OFFSET(txBuffer[pi]
1420                                                                [port->
1421                                                                 txpos][0]),
1422                                                     skb->data, skb->len);
1423                                         FST_WRB(card,
1424                                                 txDescrRing[pi][port->txpos].
1425                                                 bits,
1426                                                 DMA_OWN | TX_STP | TX_ENP);
1427                                         stats->tx_packets++;
1428                                         stats->tx_bytes += skb->len;
1429                                         dev->trans_start = jiffies;
1430                                 } else {
1431                                         /* Or do it through dma */
1432                                         memcpy(card->tx_dma_handle_host,
1433                                                skb->data, skb->len);
1434                                         card->dma_port_tx = port;
1435                                         card->dma_len_tx = skb->len;
1436                                         card->dma_txpos = port->txpos;
1437                                         fst_tx_dma(card,
1438                                                    (char *) card->
1439                                                    tx_dma_handle_card,
1440                                                    (char *)
1441                                                    BUF_OFFSET(txBuffer[pi]
1442                                                               [port->txpos][0]),
1443                                                    skb->len);
1444                                 }
1445                                 if (++port->txpos >= NUM_TX_BUFFER)
1446                                         port->txpos = 0;
1447                                 /*
1448                                  * If we have flow control on, can we now release it?
1449                                  */
1450                                 if (port->start) {
1451                                         if (txq_length < fst_txq_low) {
1452                                                 netif_wake_queue(port_to_dev
1453                                                                  (port));
1454                                                 port->start = 0;
1455                                         }
1456                                 }
1457                                 dev_kfree_skb(skb);
1458                         } else {
1459                                 /*
1460                                  * Nothing to send so break out of the while loop
1461                                  */
1462                                 break;
1463                         }
1464                 }
1465         }
1466 }
1467
1468 static void
1469 do_bottom_half_rx(struct fst_card_info *card)
1470 {
1471         struct fst_port_info *port;
1472         int pi;
1473         int rx_count = 0;
1474
1475         /* Check for rx completions on all ports on this card */
1476         dbg(DBG_RX, "do_bottom_half_rx\n");
1477         for (pi = 0, port = card->ports; pi < card->nports; pi++, port++) {
1478                 if (!port->run)
1479                         continue;
1480
1481                 while (!(FST_RDB(card, rxDescrRing[pi][port->rxpos].bits)
1482                          & DMA_OWN) && !(card->dmarx_in_progress)) {
1483                         if (rx_count > fst_max_reads) {
1484                                 /*
1485                                  * Don't spend forever in receive processing
1486                                  * Schedule another event
1487                                  */
1488                                 fst_q_work_item(&fst_work_intq, card->card_no);
1489                                 tasklet_schedule(&fst_int_task);
1490                                 break;  /* Leave the loop */
1491                         }
1492                         fst_intr_rx(card, port);
1493                         rx_count++;
1494                 }
1495         }
1496 }
1497
1498 /*
1499  *      The interrupt service routine
1500  *      Dev_id is our fst_card_info pointer
1501  */
1502 irqreturn_t
1503 fst_intr(int irq, void *dev_id, struct pt_regs *regs)
1504 {
1505         struct fst_card_info *card;
1506         struct fst_port_info *port;
1507         int rdidx;              /* Event buffer indices */
1508         int wridx;
1509         int event;              /* Actual event for processing */
1510         unsigned int dma_intcsr = 0;
1511         unsigned int do_card_interrupt;
1512         unsigned int int_retry_count;
1513
1514         if ((card = dev_id) == NULL) {
1515                 dbg(DBG_INTR, "intr: spurious %d\n", irq);
1516                 return IRQ_NONE;
1517         }
1518
1519         /*
1520          * Check to see if the interrupt was for this card
1521          * return if not
1522          * Note that the call to clear the interrupt is important
1523          */
1524         dbg(DBG_INTR, "intr: %d %p\n", irq, card);
1525         if (card->state != FST_RUNNING) {
1526                 printk_err
1527                     ("Interrupt received for card %d in a non running state (%d)\n",
1528                      card->card_no, card->state);
1529
1530                 /* 
1531                  * It is possible to really be running, i.e. we have re-loaded
1532                  * a running card
1533                  * Clear and reprime the interrupt source 
1534                  */
1535                 fst_clear_intr(card);
1536                 return IRQ_HANDLED;
1537         }
1538
1539         /* Clear and reprime the interrupt source */
1540         fst_clear_intr(card);
1541
1542         /*
1543          * Is the interrupt for this card (handshake == 1)
1544          */
1545         do_card_interrupt = 0;
1546         if (FST_RDB(card, interruptHandshake) == 1) {
1547                 do_card_interrupt += FST_CARD_INT;
1548                 /* Set the software acknowledge */
1549                 FST_WRB(card, interruptHandshake, 0xEE);
1550         }
1551         if (card->family == FST_FAMILY_TXU) {
1552                 /*
1553                  * Is it a DMA Interrupt
1554                  */
1555                 dma_intcsr = inl(card->pci_conf + INTCSR_9054);
1556                 if (dma_intcsr & 0x00200000) {
1557                         /*
1558                          * DMA Channel 0 (Rx transfer complete)
1559                          */
1560                         dbg(DBG_RX, "DMA Rx xfer complete\n");
1561                         outb(0x8, card->pci_conf + DMACSR0);
1562                         fst_rx_dma_complete(card, card->dma_port_rx,
1563                                             card->dma_len_rx, card->dma_skb_rx,
1564                                             card->dma_rxpos);
1565                         card->dmarx_in_progress = 0;
1566                         do_card_interrupt += FST_RX_DMA_INT;
1567                 }
1568                 if (dma_intcsr & 0x00400000) {
1569                         /*
1570                          * DMA Channel 1 (Tx transfer complete)
1571                          */
1572                         dbg(DBG_TX, "DMA Tx xfer complete\n");
1573                         outb(0x8, card->pci_conf + DMACSR1);
1574                         fst_tx_dma_complete(card, card->dma_port_tx,
1575                                             card->dma_len_tx, card->dma_txpos);
1576                         card->dmatx_in_progress = 0;
1577                         do_card_interrupt += FST_TX_DMA_INT;
1578                 }
1579         }
1580
1581         /*
1582          * Have we been missing Interrupts
1583          */
1584         int_retry_count = FST_RDL(card, interruptRetryCount);
1585         if (int_retry_count) {
1586                 dbg(DBG_ASS, "Card %d int_retry_count is  %d\n",
1587                     card->card_no, int_retry_count);
1588                 FST_WRL(card, interruptRetryCount, 0);
1589         }
1590
1591         if (!do_card_interrupt) {
1592                 return IRQ_HANDLED;
1593         }
1594
1595         /* Scehdule the bottom half of the ISR */
1596         fst_q_work_item(&fst_work_intq, card->card_no);
1597         tasklet_schedule(&fst_int_task);
1598
1599         /* Drain the event queue */
1600         rdidx = FST_RDB(card, interruptEvent.rdindex) & 0x1f;
1601         wridx = FST_RDB(card, interruptEvent.wrindex) & 0x1f;
1602         while (rdidx != wridx) {
1603                 event = FST_RDB(card, interruptEvent.evntbuff[rdidx]);
1604                 port = &card->ports[event & 0x03];
1605
1606                 dbg(DBG_INTR, "Processing Interrupt event: %x\n", event);
1607
1608                 switch (event) {
1609                 case TE1_ALMA:
1610                         dbg(DBG_INTR, "TE1 Alarm intr\n");
1611                         if (port->run)
1612                                 fst_intr_te1_alarm(card, port);
1613                         break;
1614
1615                 case CTLA_CHG:
1616                 case CTLB_CHG:
1617                 case CTLC_CHG:
1618                 case CTLD_CHG:
1619                         if (port->run)
1620                                 fst_intr_ctlchg(card, port);
1621                         break;
1622
1623                 case ABTA_SENT:
1624                 case ABTB_SENT:
1625                 case ABTC_SENT:
1626                 case ABTD_SENT:
1627                         dbg(DBG_TX, "Abort complete port %d\n", port->index);
1628                         break;
1629
1630                 case TXA_UNDF:
1631                 case TXB_UNDF:
1632                 case TXC_UNDF:
1633                 case TXD_UNDF:
1634                         /* Difficult to see how we'd get this given that we
1635                          * always load up the entire packet for DMA.
1636                          */
1637                         dbg(DBG_TX, "Tx underflow port %d\n", port->index);
1638                         hdlc_stats(port_to_dev(port))->tx_errors++;
1639                         hdlc_stats(port_to_dev(port))->tx_fifo_errors;
1640                         dbg(DBG_ASS, "Tx underflow on card %d port %d\n",
1641                             card->card_no, port->index);
1642                         break;
1643
1644                 case INIT_CPLT:
1645                         dbg(DBG_INIT, "Card init OK intr\n");
1646                         break;
1647
1648                 case INIT_FAIL:
1649                         dbg(DBG_INIT, "Card init FAILED intr\n");
1650                         card->state = FST_IFAILED;
1651                         break;
1652
1653                 default:
1654                         printk_err("intr: unknown card event %d. ignored\n",
1655                                    event);
1656                         break;
1657                 }
1658
1659                 /* Bump and wrap the index */
1660                 if (++rdidx >= MAX_CIRBUFF)
1661                         rdidx = 0;
1662         }
1663         FST_WRB(card, interruptEvent.rdindex, rdidx);
1664         return IRQ_HANDLED;
1665 }
1666
1667 /*      Check that the shared memory configuration is one that we can handle
1668  *      and that some basic parameters are correct
1669  */
1670 static void
1671 check_started_ok(struct fst_card_info *card)
1672 {
1673         int i;
1674
1675         /* Check structure version and end marker */
1676         if (FST_RDW(card, smcVersion) != SMC_VERSION) {
1677                 printk_err("Bad shared memory version %d expected %d\n",
1678                            FST_RDW(card, smcVersion), SMC_VERSION);
1679                 card->state = FST_BADVERSION;
1680                 return;
1681         }
1682         if (FST_RDL(card, endOfSmcSignature) != END_SIG) {
1683                 printk_err("Missing shared memory signature\n");
1684                 card->state = FST_BADVERSION;
1685                 return;
1686         }
1687         /* Firmware status flag, 0x00 = initialising, 0x01 = OK, 0xFF = fail */
1688         if ((i = FST_RDB(card, taskStatus)) == 0x01) {
1689                 card->state = FST_RUNNING;
1690         } else if (i == 0xFF) {
1691                 printk_err("Firmware initialisation failed. Card halted\n");
1692                 card->state = FST_HALTED;
1693                 return;
1694         } else if (i != 0x00) {
1695                 printk_err("Unknown firmware status 0x%x\n", i);
1696                 card->state = FST_HALTED;
1697                 return;
1698         }
1699
1700         /* Finally check the number of ports reported by firmware against the
1701          * number we assumed at card detection. Should never happen with
1702          * existing firmware etc so we just report it for the moment.
1703          */
1704         if (FST_RDL(card, numberOfPorts) != card->nports) {
1705                 printk_warn("Port count mismatch on card %d."
1706                             " Firmware thinks %d we say %d\n", card->card_no,
1707                             FST_RDL(card, numberOfPorts), card->nports);
1708         }
1709 }
1710
1711 static int
1712 set_conf_from_info(struct fst_card_info *card, struct fst_port_info *port,
1713                    struct fstioc_info *info)
1714 {
1715         int err;
1716         unsigned char my_framing;
1717
1718         /* Set things according to the user set valid flags 
1719          * Several of the old options have been invalidated/replaced by the 
1720          * generic hdlc package.
1721          */
1722         err = 0;
1723         if (info->valid & FSTVAL_PROTO) {
1724                 if (info->proto == FST_RAW)
1725                         port->mode = FST_RAW;
1726                 else
1727                         port->mode = FST_GEN_HDLC;
1728         }
1729
1730         if (info->valid & FSTVAL_CABLE)
1731                 err = -EINVAL;
1732
1733         if (info->valid & FSTVAL_SPEED)
1734                 err = -EINVAL;
1735
1736         if (info->valid & FSTVAL_PHASE)
1737                 FST_WRB(card, portConfig[port->index].invertClock,
1738                         info->invertClock);
1739         if (info->valid & FSTVAL_MODE)
1740                 FST_WRW(card, cardMode, info->cardMode);
1741         if (info->valid & FSTVAL_TE1) {
1742                 FST_WRL(card, suConfig.dataRate, info->lineSpeed);
1743                 FST_WRB(card, suConfig.clocking, info->clockSource);
1744                 my_framing = FRAMING_E1;
1745                 if (info->framing == E1)
1746                         my_framing = FRAMING_E1;
1747                 if (info->framing == T1)
1748                         my_framing = FRAMING_T1;
1749                 if (info->framing == J1)
1750                         my_framing = FRAMING_J1;
1751                 FST_WRB(card, suConfig.framing, my_framing);
1752                 FST_WRB(card, suConfig.structure, info->structure);
1753                 FST_WRB(card, suConfig.interface, info->interface);
1754                 FST_WRB(card, suConfig.coding, info->coding);
1755                 FST_WRB(card, suConfig.lineBuildOut, info->lineBuildOut);
1756                 FST_WRB(card, suConfig.equalizer, info->equalizer);
1757                 FST_WRB(card, suConfig.transparentMode, info->transparentMode);
1758                 FST_WRB(card, suConfig.loopMode, info->loopMode);
1759                 FST_WRB(card, suConfig.range, info->range);
1760                 FST_WRB(card, suConfig.txBufferMode, info->txBufferMode);
1761                 FST_WRB(card, suConfig.rxBufferMode, info->rxBufferMode);
1762                 FST_WRB(card, suConfig.startingSlot, info->startingSlot);
1763                 FST_WRB(card, suConfig.losThreshold, info->losThreshold);
1764                 if (info->idleCode)
1765                         FST_WRB(card, suConfig.enableIdleCode, 1);
1766                 else
1767                         FST_WRB(card, suConfig.enableIdleCode, 0);
1768                 FST_WRB(card, suConfig.idleCode, info->idleCode);
1769 #if FST_DEBUG
1770                 if (info->valid & FSTVAL_TE1) {
1771                         printk("Setting TE1 data\n");
1772                         printk("Line Speed = %d\n", info->lineSpeed);
1773                         printk("Start slot = %d\n", info->startingSlot);
1774                         printk("Clock source = %d\n", info->clockSource);
1775                         printk("Framing = %d\n", my_framing);
1776                         printk("Structure = %d\n", info->structure);
1777                         printk("interface = %d\n", info->interface);
1778                         printk("Coding = %d\n", info->coding);
1779                         printk("Line build out = %d\n", info->lineBuildOut);
1780                         printk("Equaliser = %d\n", info->equalizer);
1781                         printk("Transparent mode = %d\n",
1782                                info->transparentMode);
1783                         printk("Loop mode = %d\n", info->loopMode);
1784                         printk("Range = %d\n", info->range);
1785                         printk("Tx Buffer mode = %d\n", info->txBufferMode);
1786                         printk("Rx Buffer mode = %d\n", info->rxBufferMode);
1787                         printk("LOS Threshold = %d\n", info->losThreshold);
1788                         printk("Idle Code = %d\n", info->idleCode);
1789                 }
1790 #endif
1791         }
1792 #if FST_DEBUG
1793         if (info->valid & FSTVAL_DEBUG) {
1794                 fst_debug_mask = info->debug;
1795         }
1796 #endif
1797
1798         return err;
1799 }
1800
1801 static void
1802 gather_conf_info(struct fst_card_info *card, struct fst_port_info *port,
1803                  struct fstioc_info *info)
1804 {
1805         int i;
1806
1807         memset(info, 0, sizeof (struct fstioc_info));
1808
1809         i = port->index;
1810         info->kernelVersion = LINUX_VERSION_CODE;
1811         info->nports = card->nports;
1812         info->type = card->type;
1813         info->state = card->state;
1814         info->proto = FST_GEN_HDLC;
1815         info->index = i;
1816 #if FST_DEBUG
1817         info->debug = fst_debug_mask;
1818 #endif
1819
1820         /* Only mark information as valid if card is running.
1821          * Copy the data anyway in case it is useful for diagnostics
1822          */
1823         info->valid = ((card->state == FST_RUNNING) ? FSTVAL_ALL : FSTVAL_CARD)
1824 #if FST_DEBUG
1825             | FSTVAL_DEBUG
1826 #endif
1827             ;
1828
1829         info->lineInterface = FST_RDW(card, portConfig[i].lineInterface);
1830         info->internalClock = FST_RDB(card, portConfig[i].internalClock);
1831         info->lineSpeed = FST_RDL(card, portConfig[i].lineSpeed);
1832         info->invertClock = FST_RDB(card, portConfig[i].invertClock);
1833         info->v24IpSts = FST_RDL(card, v24IpSts[i]);
1834         info->v24OpSts = FST_RDL(card, v24OpSts[i]);
1835         info->clockStatus = FST_RDW(card, clockStatus[i]);
1836         info->cableStatus = FST_RDW(card, cableStatus);
1837         info->cardMode = FST_RDW(card, cardMode);
1838         info->smcFirmwareVersion = FST_RDL(card, smcFirmwareVersion);
1839
1840         /*
1841          * The T2U can report cable presence for both A or B
1842          * in bits 0 and 1 of cableStatus.  See which port we are and 
1843          * do the mapping.
1844          */
1845         if (card->family == FST_FAMILY_TXU) {
1846                 if (port->index == 0) {
1847                         /*
1848                          * Port A
1849                          */
1850                         info->cableStatus = info->cableStatus & 1;
1851                 } else {
1852                         /*
1853                          * Port B
1854                          */
1855                         info->cableStatus = info->cableStatus >> 1;
1856                         info->cableStatus = info->cableStatus & 1;
1857                 }
1858         }
1859         /*
1860          * Some additional bits if we are TE1
1861          */
1862         if (card->type == FST_TYPE_TE1) {
1863                 info->lineSpeed = FST_RDL(card, suConfig.dataRate);
1864                 info->clockSource = FST_RDB(card, suConfig.clocking);
1865                 info->framing = FST_RDB(card, suConfig.framing);
1866                 info->structure = FST_RDB(card, suConfig.structure);
1867                 info->interface = FST_RDB(card, suConfig.interface);
1868                 info->coding = FST_RDB(card, suConfig.coding);
1869                 info->lineBuildOut = FST_RDB(card, suConfig.lineBuildOut);
1870                 info->equalizer = FST_RDB(card, suConfig.equalizer);
1871                 info->loopMode = FST_RDB(card, suConfig.loopMode);
1872                 info->range = FST_RDB(card, suConfig.range);
1873                 info->txBufferMode = FST_RDB(card, suConfig.txBufferMode);
1874                 info->rxBufferMode = FST_RDB(card, suConfig.rxBufferMode);
1875                 info->startingSlot = FST_RDB(card, suConfig.startingSlot);
1876                 info->losThreshold = FST_RDB(card, suConfig.losThreshold);
1877                 if (FST_RDB(card, suConfig.enableIdleCode))
1878                         info->idleCode = FST_RDB(card, suConfig.idleCode);
1879                 else
1880                         info->idleCode = 0;
1881                 info->receiveBufferDelay =
1882                     FST_RDL(card, suStatus.receiveBufferDelay);
1883                 info->framingErrorCount =
1884                     FST_RDL(card, suStatus.framingErrorCount);
1885                 info->codeViolationCount =
1886                     FST_RDL(card, suStatus.codeViolationCount);
1887                 info->crcErrorCount = FST_RDL(card, suStatus.crcErrorCount);
1888                 info->lineAttenuation = FST_RDL(card, suStatus.lineAttenuation);
1889                 info->lossOfSignal = FST_RDB(card, suStatus.lossOfSignal);
1890                 info->receiveRemoteAlarm =
1891                     FST_RDB(card, suStatus.receiveRemoteAlarm);
1892                 info->alarmIndicationSignal =
1893                     FST_RDB(card, suStatus.alarmIndicationSignal);
1894         }
1895 }
1896
1897 static int
1898 fst_set_iface(struct fst_card_info *card, struct fst_port_info *port,
1899               struct ifreq *ifr)
1900 {
1901         sync_serial_settings sync;
1902         int i;
1903
1904         if (ifr->ifr_settings.size != sizeof (sync)) {
1905                 return -ENOMEM;
1906         }
1907
1908         if (copy_from_user
1909             (&sync, ifr->ifr_settings.ifs_ifsu.sync, sizeof (sync))) {
1910                 return -EFAULT;
1911         }
1912
1913         if (sync.loopback)
1914                 return -EINVAL;
1915
1916         i = port->index;
1917
1918         switch (ifr->ifr_settings.type) {
1919         case IF_IFACE_V35:
1920                 FST_WRW(card, portConfig[i].lineInterface, V35);
1921                 port->hwif = V35;
1922                 break;
1923
1924         case IF_IFACE_V24:
1925                 FST_WRW(card, portConfig[i].lineInterface, V24);
1926                 port->hwif = V24;
1927                 break;
1928
1929         case IF_IFACE_X21:
1930                 FST_WRW(card, portConfig[i].lineInterface, X21);
1931                 port->hwif = X21;
1932                 break;
1933
1934         case IF_IFACE_X21D:
1935                 FST_WRW(card, portConfig[i].lineInterface, X21D);
1936                 port->hwif = X21D;
1937                 break;
1938
1939         case IF_IFACE_T1:
1940                 FST_WRW(card, portConfig[i].lineInterface, T1);
1941                 port->hwif = T1;
1942                 break;
1943
1944         case IF_IFACE_E1:
1945                 FST_WRW(card, portConfig[i].lineInterface, E1);
1946                 port->hwif = E1;
1947                 break;
1948
1949         case IF_IFACE_SYNC_SERIAL:
1950                 break;
1951
1952         default:
1953                 return -EINVAL;
1954         }
1955
1956         switch (sync.clock_type) {
1957         case CLOCK_EXT:
1958                 FST_WRB(card, portConfig[i].internalClock, EXTCLK);
1959                 break;
1960
1961         case CLOCK_INT:
1962                 FST_WRB(card, portConfig[i].internalClock, INTCLK);
1963                 break;
1964
1965         default:
1966                 return -EINVAL;
1967         }
1968         FST_WRL(card, portConfig[i].lineSpeed, sync.clock_rate);
1969         return 0;
1970 }
1971
1972 static int
1973 fst_get_iface(struct fst_card_info *card, struct fst_port_info *port,
1974               struct ifreq *ifr)
1975 {
1976         sync_serial_settings sync;
1977         int i;
1978
1979         /* First check what line type is set, we'll default to reporting X.21
1980          * if nothing is set as IF_IFACE_SYNC_SERIAL implies it can't be
1981          * changed
1982          */
1983         switch (port->hwif) {
1984         case E1:
1985                 ifr->ifr_settings.type = IF_IFACE_E1;
1986                 break;
1987         case T1:
1988                 ifr->ifr_settings.type = IF_IFACE_T1;
1989                 break;
1990         case V35:
1991                 ifr->ifr_settings.type = IF_IFACE_V35;
1992                 break;
1993         case V24:
1994                 ifr->ifr_settings.type = IF_IFACE_V24;
1995                 break;
1996         case X21D:
1997                 ifr->ifr_settings.type = IF_IFACE_X21D;
1998                 break;
1999         case X21:
2000         default:
2001                 ifr->ifr_settings.type = IF_IFACE_X21;
2002                 break;
2003         }
2004         if (ifr->ifr_settings.size == 0) {
2005                 return 0;       /* only type requested */
2006         }
2007         if (ifr->ifr_settings.size < sizeof (sync)) {
2008                 return -ENOMEM;
2009         }
2010
2011         i = port->index;
2012         sync.clock_rate = FST_RDL(card, portConfig[i].lineSpeed);
2013         /* Lucky card and linux use same encoding here */
2014         sync.clock_type = FST_RDB(card, portConfig[i].internalClock) ==
2015             INTCLK ? CLOCK_INT : CLOCK_EXT;
2016         sync.loopback = 0;
2017
2018         if (copy_to_user(ifr->ifr_settings.ifs_ifsu.sync, &sync, sizeof (sync))) {
2019                 return -EFAULT;
2020         }
2021
2022         ifr->ifr_settings.size = sizeof (sync);
2023         return 0;
2024 }
2025
2026 static int
2027 fst_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2028 {
2029         struct fst_card_info *card;
2030         struct fst_port_info *port;
2031         struct fstioc_write wrthdr;
2032         struct fstioc_info info;
2033         unsigned long flags;
2034
2035         dbg(DBG_IOCTL, "ioctl: %x, %p\n", cmd, ifr->ifr_data);
2036
2037         port = dev_to_port(dev);
2038         card = port->card;
2039
2040         if (!capable(CAP_NET_ADMIN))
2041                 return -EPERM;
2042
2043         switch (cmd) {
2044         case FSTCPURESET:
2045                 fst_cpureset(card);
2046                 card->state = FST_RESET;
2047                 return 0;
2048
2049         case FSTCPURELEASE:
2050                 fst_cpurelease(card);
2051                 card->state = FST_STARTING;
2052                 return 0;
2053
2054         case FSTWRITE:          /* Code write (download) */
2055
2056                 /* First copy in the header with the length and offset of data
2057                  * to write
2058                  */
2059                 if (ifr->ifr_data == NULL) {
2060                         return -EINVAL;
2061                 }
2062                 if (copy_from_user(&wrthdr, ifr->ifr_data,
2063                                    sizeof (struct fstioc_write))) {
2064                         return -EFAULT;
2065                 }
2066
2067                 /* Sanity check the parameters. We don't support partial writes
2068                  * when going over the top
2069                  */
2070                 if (wrthdr.size > FST_MEMSIZE || wrthdr.offset > FST_MEMSIZE
2071                     || wrthdr.size + wrthdr.offset > FST_MEMSIZE) {
2072                         return -ENXIO;
2073                 }
2074
2075                 /* Now copy the data to the card.
2076                  * This will probably break on some architectures.
2077                  * I'll fix it when I have something to test on.
2078                  */
2079                 if (copy_from_user(card->mem + wrthdr.offset,
2080                                    ifr->ifr_data + sizeof (struct fstioc_write),
2081                                    wrthdr.size)) {
2082                         return -EFAULT;
2083                 }
2084
2085                 /* Writes to the memory of a card in the reset state constitute
2086                  * a download
2087                  */
2088                 if (card->state == FST_RESET) {
2089                         card->state = FST_DOWNLOAD;
2090                 }
2091                 return 0;
2092
2093         case FSTGETCONF:
2094
2095                 /* If card has just been started check the shared memory config
2096                  * version and marker
2097                  */
2098                 if (card->state == FST_STARTING) {
2099                         check_started_ok(card);
2100
2101                         /* If everything checked out enable card interrupts */
2102                         if (card->state == FST_RUNNING) {
2103                                 spin_lock_irqsave(&card->card_lock, flags);
2104                                 fst_enable_intr(card);
2105                                 FST_WRB(card, interruptHandshake, 0xEE);
2106                                 spin_unlock_irqrestore(&card->card_lock, flags);
2107                         }
2108                 }
2109
2110                 if (ifr->ifr_data == NULL) {
2111                         return -EINVAL;
2112                 }
2113
2114                 gather_conf_info(card, port, &info);
2115
2116                 if (copy_to_user(ifr->ifr_data, &info, sizeof (info))) {
2117                         return -EFAULT;
2118                 }
2119                 return 0;
2120
2121         case FSTSETCONF:
2122
2123                 /*
2124                  * Most of the settings have been moved to the generic ioctls
2125                  * this just covers debug and board ident now
2126                  */
2127
2128                 if (card->state != FST_RUNNING) {
2129                         printk_err
2130                             ("Attempt to configure card %d in non-running state (%d)\n",
2131                              card->card_no, card->state);
2132                         return -EIO;
2133                 }
2134                 if (copy_from_user(&info, ifr->ifr_data, sizeof (info))) {
2135                         return -EFAULT;
2136                 }
2137
2138                 return set_conf_from_info(card, port, &info);
2139
2140         case SIOCWANDEV:
2141                 switch (ifr->ifr_settings.type) {
2142                 case IF_GET_IFACE:
2143                         return fst_get_iface(card, port, ifr);
2144
2145                 case IF_IFACE_SYNC_SERIAL:
2146                 case IF_IFACE_V35:
2147                 case IF_IFACE_V24:
2148                 case IF_IFACE_X21:
2149                 case IF_IFACE_X21D:
2150                 case IF_IFACE_T1:
2151                 case IF_IFACE_E1:
2152                         return fst_set_iface(card, port, ifr);
2153
2154                 case IF_PROTO_RAW:
2155                         port->mode = FST_RAW;
2156                         return 0;
2157
2158                 case IF_GET_PROTO:
2159                         if (port->mode == FST_RAW) {
2160                                 ifr->ifr_settings.type = IF_PROTO_RAW;
2161                                 return 0;
2162                         }
2163                         return hdlc_ioctl(dev, ifr, cmd);
2164
2165                 default:
2166                         port->mode = FST_GEN_HDLC;
2167                         dbg(DBG_IOCTL, "Passing this type to hdlc %x\n",
2168                             ifr->ifr_settings.type);
2169                         return hdlc_ioctl(dev, ifr, cmd);
2170                 }
2171
2172         default:
2173                 /* Not one of ours. Pass through to HDLC package */
2174                 return hdlc_ioctl(dev, ifr, cmd);
2175         }
2176 }
2177
2178 static void
2179 fst_openport(struct fst_port_info *port)
2180 {
2181         int signals;
2182         int txq_length;
2183
2184         /* Only init things if card is actually running. This allows open to
2185          * succeed for downloads etc.
2186          */
2187         if (port->card->state == FST_RUNNING) {
2188                 if (port->run) {
2189                         dbg(DBG_OPEN, "open: found port already running\n");
2190
2191                         fst_issue_cmd(port, STOPPORT);
2192                         port->run = 0;
2193                 }
2194
2195                 fst_rx_config(port);
2196                 fst_tx_config(port);
2197                 fst_op_raise(port, OPSTS_RTS | OPSTS_DTR);
2198
2199                 fst_issue_cmd(port, STARTPORT);
2200                 port->run = 1;
2201
2202                 signals = FST_RDL(port->card, v24DebouncedSts[port->index]);
2203                 if (signals & (((port->hwif == X21) || (port->hwif == X21D))
2204                                ? IPSTS_INDICATE : IPSTS_DCD))
2205                         netif_carrier_on(port_to_dev(port));
2206                 else
2207                         netif_carrier_off(port_to_dev(port));
2208
2209                 txq_length = port->txqe - port->txqs;
2210                 port->txqe = 0;
2211                 port->txqs = 0;
2212         }
2213
2214 }
2215
2216 static void
2217 fst_closeport(struct fst_port_info *port)
2218 {
2219         if (port->card->state == FST_RUNNING) {
2220                 if (port->run) {
2221                         port->run = 0;
2222                         fst_op_lower(port, OPSTS_RTS | OPSTS_DTR);
2223
2224                         fst_issue_cmd(port, STOPPORT);
2225                 } else {
2226                         dbg(DBG_OPEN, "close: port not running\n");
2227                 }
2228         }
2229 }
2230
2231 static int
2232 fst_open(struct net_device *dev)
2233 {
2234         int err;
2235         struct fst_port_info *port;
2236
2237         port = dev_to_port(dev);
2238         if (!try_module_get(THIS_MODULE))
2239           return -EBUSY;
2240
2241         if (port->mode != FST_RAW) {
2242                 err = hdlc_open(dev);
2243                 if (err)
2244                         return err;
2245         }
2246
2247         fst_openport(port);
2248         netif_wake_queue(dev);
2249         return 0;
2250 }
2251
2252 static int
2253 fst_close(struct net_device *dev)
2254 {
2255         struct fst_port_info *port;
2256         struct fst_card_info *card;
2257         unsigned char tx_dma_done;
2258         unsigned char rx_dma_done;
2259
2260         port = dev_to_port(dev);
2261         card = port->card;
2262
2263         tx_dma_done = inb(card->pci_conf + DMACSR1);
2264         rx_dma_done = inb(card->pci_conf + DMACSR0);
2265         dbg(DBG_OPEN,
2266             "Port Close: tx_dma_in_progress = %d (%x) rx_dma_in_progress = %d (%x)\n",
2267             card->dmatx_in_progress, tx_dma_done, card->dmarx_in_progress,
2268             rx_dma_done);
2269
2270         netif_stop_queue(dev);
2271         fst_closeport(dev_to_port(dev));
2272         if (port->mode != FST_RAW) {
2273                 hdlc_close(dev);
2274         }
2275         module_put(THIS_MODULE);
2276         return 0;
2277 }
2278
2279 static int
2280 fst_attach(struct net_device *dev, unsigned short encoding, unsigned short parity)
2281 {
2282         /*
2283          * Setting currently fixed in FarSync card so we check and forget
2284          */
2285         if (encoding != ENCODING_NRZ || parity != PARITY_CRC16_PR1_CCITT)
2286                 return -EINVAL;
2287         return 0;
2288 }
2289
2290 static void
2291 fst_tx_timeout(struct net_device *dev)
2292 {
2293         struct fst_port_info *port;
2294         struct fst_card_info *card;
2295         struct net_device_stats *stats = hdlc_stats(dev);
2296
2297         port = dev_to_port(dev);
2298         card = port->card;
2299         stats->tx_errors++;
2300         stats->tx_aborted_errors++;
2301         dbg(DBG_ASS, "Tx timeout card %d port %d\n",
2302             card->card_no, port->index);
2303         fst_issue_cmd(port, ABORTTX);
2304
2305         dev->trans_start = jiffies;
2306         netif_wake_queue(dev);
2307         port->start = 0;
2308 }
2309
2310 static int
2311 fst_start_xmit(struct sk_buff *skb, struct net_device *dev)
2312 {
2313         struct fst_card_info *card;
2314         struct fst_port_info *port;
2315         struct net_device_stats *stats = hdlc_stats(dev);
2316         unsigned long flags;
2317         int txq_length;
2318
2319         port = dev_to_port(dev);
2320         card = port->card;
2321         dbg(DBG_TX, "fst_start_xmit: length = %d\n", skb->len);
2322
2323         /* Drop packet with error if we don't have carrier */
2324         if (!netif_carrier_ok(dev)) {
2325                 dev_kfree_skb(skb);
2326                 stats->tx_errors++;
2327                 stats->tx_carrier_errors++;
2328                 dbg(DBG_ASS,
2329                     "Tried to transmit but no carrier on card %d port %d\n",
2330                     card->card_no, port->index);
2331                 return 0;
2332         }
2333
2334         /* Drop it if it's too big! MTU failure ? */
2335         if (skb->len > LEN_TX_BUFFER) {
2336                 dbg(DBG_ASS, "Packet too large %d vs %d\n", skb->len,
2337                     LEN_TX_BUFFER);
2338                 dev_kfree_skb(skb);
2339                 stats->tx_errors++;
2340                 return 0;
2341         }
2342
2343         /*
2344          * We are always going to queue the packet
2345          * so that the bottom half is the only place we tx from
2346          * Check there is room in the port txq
2347          */
2348         spin_lock_irqsave(&card->card_lock, flags);
2349         if ((txq_length = port->txqe - port->txqs) < 0) {
2350                 /*
2351                  * This is the case where the next free has wrapped but the
2352                  * last used hasn't
2353                  */
2354                 txq_length = txq_length + FST_TXQ_DEPTH;
2355         }
2356         spin_unlock_irqrestore(&card->card_lock, flags);
2357         if (txq_length > fst_txq_high) {
2358                 /*
2359                  * We have got enough buffers in the pipeline.  Ask the network
2360                  * layer to stop sending frames down
2361                  */
2362                 netif_stop_queue(dev);
2363                 port->start = 1;        /* I'm using this to signal stop sent up */
2364         }
2365
2366         if (txq_length == FST_TXQ_DEPTH - 1) {
2367                 /*
2368                  * This shouldn't have happened but such is life
2369                  */
2370                 dev_kfree_skb(skb);
2371                 stats->tx_errors++;
2372                 dbg(DBG_ASS, "Tx queue overflow card %d port %d\n",
2373                     card->card_no, port->index);
2374                 return 0;
2375         }
2376
2377         /*
2378          * queue the buffer
2379          */
2380         spin_lock_irqsave(&card->card_lock, flags);
2381         port->txq[port->txqe] = skb;
2382         port->txqe++;
2383         if (port->txqe == FST_TXQ_DEPTH)
2384                 port->txqe = 0;
2385         spin_unlock_irqrestore(&card->card_lock, flags);
2386
2387         /* Scehdule the bottom half which now does transmit processing */
2388         fst_q_work_item(&fst_work_txq, card->card_no);
2389         tasklet_schedule(&fst_tx_task);
2390
2391         return 0;
2392 }
2393
2394 /*
2395  *      Card setup having checked hardware resources.
2396  *      Should be pretty bizarre if we get an error here (kernel memory
2397  *      exhaustion is one possibility). If we do see a problem we report it
2398  *      via a printk and leave the corresponding interface and all that follow
2399  *      disabled.
2400  */
2401 static char *type_strings[] __devinitdata = {
2402         "no hardware",          /* Should never be seen */
2403         "FarSync T2P",
2404         "FarSync T4P",
2405         "FarSync T1U",
2406         "FarSync T2U",
2407         "FarSync T4U",
2408         "FarSync TE1"
2409 };
2410
2411 static void __devinit
2412 fst_init_card(struct fst_card_info *card)
2413 {
2414         int i;
2415         int err;
2416
2417         /* We're working on a number of ports based on the card ID. If the
2418          * firmware detects something different later (should never happen)
2419          * we'll have to revise it in some way then.
2420          */
2421         for (i = 0; i < card->nports; i++) {
2422                 err = register_hdlc_device(card->ports[i].dev);
2423                 if (err < 0) {
2424                         int j;
2425                         printk_err ("Cannot register HDLC device for port %d"
2426                                     " (errno %d)\n", i, -err );
2427                         for (j = i; j < card->nports; j++) {
2428                                 free_netdev(card->ports[j].dev);
2429                                 card->ports[j].dev = NULL;
2430                         }
2431                         card->nports = i;
2432                         break;
2433                 }
2434         }
2435
2436         printk_info("%s-%s: %s IRQ%d, %d ports\n",
2437                port_to_dev(&card->ports[0])->name,
2438                port_to_dev(&card->ports[card->nports - 1])->name,
2439                type_strings[card->type], card->irq, card->nports);
2440 }
2441
2442 /*
2443  *      Initialise card when detected.
2444  *      Returns 0 to indicate success, or errno otherwise.
2445  */
2446 static int __devinit
2447 fst_add_one(struct pci_dev *pdev, const struct pci_device_id *ent)
2448 {
2449         static int firsttime_done = 0;
2450         static int no_of_cards_added = 0;
2451         struct fst_card_info *card;
2452         int err = 0;
2453         int i;
2454
2455         if (!firsttime_done) {
2456                 printk_info("FarSync WAN driver " FST_USER_VERSION
2457                        " (c) 2001-2004 FarSite Communications Ltd.\n");
2458                 firsttime_done = 1;
2459                 dbg(DBG_ASS, "The value of debug mask is %x\n", fst_debug_mask);
2460         }
2461
2462         /*
2463          * We are going to be clever and allow certain cards not to be
2464          * configured.  An exclude list can be provided in /etc/modules.conf
2465          */
2466         if (fst_excluded_cards != 0) {
2467                 /*
2468                  * There are cards to exclude
2469                  *
2470                  */
2471                 for (i = 0; i < fst_excluded_cards; i++) {
2472                         if ((pdev->devfn) >> 3 == fst_excluded_list[i]) {
2473                                 printk_info("FarSync PCI device %d not assigned\n",
2474                                        (pdev->devfn) >> 3);
2475                                 return -EBUSY;
2476                         }
2477                 }
2478         }
2479
2480         /* Allocate driver private data */
2481         card = kmalloc(sizeof (struct fst_card_info), GFP_KERNEL);
2482         if (card == NULL) {
2483                 printk_err("FarSync card found but insufficient memory for"
2484                            " driver storage\n");
2485                 return -ENOMEM;
2486         }
2487         memset(card, 0, sizeof (struct fst_card_info));
2488
2489         /* Try to enable the device */
2490         if ((err = pci_enable_device(pdev)) != 0) {
2491                 printk_err("Failed to enable card. Err %d\n", -err);
2492                 kfree(card);
2493                 return err;
2494         }
2495
2496         if ((err = pci_request_regions(pdev, "FarSync")) !=0) {
2497                 printk_err("Failed to allocate regions. Err %d\n", -err);
2498                 pci_disable_device(pdev);
2499                 kfree(card);
2500                 return err;
2501         }
2502
2503         /* Get virtual addresses of memory regions */
2504         card->pci_conf = pci_resource_start(pdev, 1);
2505         card->phys_mem = pci_resource_start(pdev, 2);
2506         card->phys_ctlmem = pci_resource_start(pdev, 3);
2507         if ((card->mem = ioremap(card->phys_mem, FST_MEMSIZE)) == NULL) {
2508                 printk_err("Physical memory remap failed\n");
2509                 pci_release_regions(pdev);
2510                 pci_disable_device(pdev);
2511                 kfree(card);
2512                 return -ENODEV;
2513         }
2514         if ((card->ctlmem = ioremap(card->phys_ctlmem, 0x10)) == NULL) {
2515                 printk_err("Control memory remap failed\n");
2516                 pci_release_regions(pdev);
2517                 pci_disable_device(pdev);
2518                 kfree(card);
2519                 return -ENODEV;
2520         }
2521         dbg(DBG_PCI, "kernel mem %p, ctlmem %p\n", card->mem, card->ctlmem);
2522
2523         /* Register the interrupt handler */
2524         if (request_irq(pdev->irq, fst_intr, SA_SHIRQ, FST_DEV_NAME, card)) {
2525                 printk_err("Unable to register interrupt %d\n", card->irq);
2526                 pci_release_regions(pdev);
2527                 pci_disable_device(pdev);
2528                 iounmap(card->ctlmem);
2529                 iounmap(card->mem);
2530                 kfree(card);
2531                 return -ENODEV;
2532         }
2533
2534         /* Record info we need */
2535         card->irq = pdev->irq;
2536         card->type = ent->driver_data;
2537         card->family = ((ent->driver_data == FST_TYPE_T2P) ||
2538                         (ent->driver_data == FST_TYPE_T4P))
2539             ? FST_FAMILY_TXP : FST_FAMILY_TXU;
2540         if ((ent->driver_data == FST_TYPE_T1U) ||
2541             (ent->driver_data == FST_TYPE_TE1))
2542                 card->nports = 1;
2543         else
2544                 card->nports = ((ent->driver_data == FST_TYPE_T2P) ||
2545                                 (ent->driver_data == FST_TYPE_T2U)) ? 2 : 4;
2546
2547         card->state = FST_UNINIT;
2548         spin_lock_init ( &card->card_lock );
2549
2550         for ( i = 0 ; i < card->nports ; i++ ) {
2551                 struct net_device *dev = alloc_hdlcdev(&card->ports[i]);
2552                 hdlc_device *hdlc;
2553                 if (!dev) {
2554                         while (i--)
2555                                 free_netdev(card->ports[i].dev);
2556                         printk_err ("FarSync: out of memory\n");
2557                         free_irq(card->irq, card);
2558                         pci_release_regions(pdev);
2559                         pci_disable_device(pdev);
2560                         iounmap(card->ctlmem);
2561                         iounmap(card->mem);
2562                         kfree(card);
2563                         return -ENODEV;
2564                 }
2565                 card->ports[i].dev    = dev;
2566                 card->ports[i].card   = card;
2567                 card->ports[i].index  = i;
2568                 card->ports[i].run    = 0;
2569
2570                 hdlc = dev_to_hdlc(dev);
2571
2572                 /* Fill in the net device info */
2573                 /* Since this is a PCI setup this is purely
2574                  * informational. Give them the buffer addresses
2575                  * and basic card I/O.
2576                  */
2577                 dev->mem_start   = card->phys_mem
2578                                  + BUF_OFFSET ( txBuffer[i][0][0]);
2579                 dev->mem_end     = card->phys_mem
2580                                  + BUF_OFFSET ( txBuffer[i][NUM_TX_BUFFER][0]);
2581                 dev->base_addr   = card->pci_conf;
2582                 dev->irq         = card->irq;
2583
2584                 dev->tx_queue_len          = FST_TX_QUEUE_LEN;
2585                 dev->open                  = fst_open;
2586                 dev->stop                  = fst_close;
2587                 dev->do_ioctl              = fst_ioctl;
2588                 dev->watchdog_timeo        = FST_TX_TIMEOUT;
2589                 dev->tx_timeout            = fst_tx_timeout;
2590                 hdlc->attach = fst_attach;
2591                 hdlc->xmit   = fst_start_xmit;
2592         }
2593
2594         card->device = pdev;
2595
2596         dbg(DBG_PCI, "type %d nports %d irq %d\n", card->type,
2597             card->nports, card->irq);
2598         dbg(DBG_PCI, "conf %04x mem %08x ctlmem %08x\n",
2599             card->pci_conf, card->phys_mem, card->phys_ctlmem);
2600
2601         /* Reset the card's processor */
2602         fst_cpureset(card);
2603         card->state = FST_RESET;
2604
2605         /* Initialise DMA (if required) */
2606         fst_init_dma(card);
2607
2608         /* Record driver data for later use */
2609         pci_set_drvdata(pdev, card);
2610
2611         /* Remainder of card setup */
2612         fst_card_array[no_of_cards_added] = card;
2613         card->card_no = no_of_cards_added++;    /* Record instance and bump it */
2614         fst_init_card(card);
2615         if (card->family == FST_FAMILY_TXU) {
2616                 /*
2617                  * Allocate a dma buffer for transmit and receives
2618                  */
2619                 card->rx_dma_handle_host =
2620                     pci_alloc_consistent(card->device, FST_MAX_MTU,
2621                                          &card->rx_dma_handle_card);
2622                 if (card->rx_dma_handle_host == NULL) {
2623                         printk_err("Could not allocate rx dma buffer\n");
2624                         fst_disable_intr(card);
2625                         pci_release_regions(pdev);
2626                         pci_disable_device(pdev);
2627                         iounmap(card->ctlmem);
2628                         iounmap(card->mem);
2629                         kfree(card);
2630                         return -ENOMEM;
2631                 }
2632                 card->tx_dma_handle_host =
2633                     pci_alloc_consistent(card->device, FST_MAX_MTU,
2634                                          &card->tx_dma_handle_card);
2635                 if (card->tx_dma_handle_host == NULL) {
2636                         printk_err("Could not allocate tx dma buffer\n");
2637                         fst_disable_intr(card);
2638                         pci_release_regions(pdev);
2639                         pci_disable_device(pdev);
2640                         iounmap(card->ctlmem);
2641                         iounmap(card->mem);
2642                         kfree(card);
2643                         return -ENOMEM;
2644                 }
2645         }
2646         return 0;               /* Success */
2647 }
2648
2649 /*
2650  *      Cleanup and close down a card
2651  */
2652 static void __devexit
2653 fst_remove_one(struct pci_dev *pdev)
2654 {
2655         struct fst_card_info *card;
2656         int i;
2657
2658         card = pci_get_drvdata(pdev);
2659
2660         for (i = 0; i < card->nports; i++) {
2661                 struct net_device *dev = port_to_dev(&card->ports[i]);
2662                 unregister_hdlc_device(dev);
2663         }
2664
2665         fst_disable_intr(card);
2666         free_irq(card->irq, card);
2667
2668         iounmap(card->ctlmem);
2669         iounmap(card->mem);
2670         pci_release_regions(pdev);
2671         if (card->family == FST_FAMILY_TXU) {
2672                 /*
2673                  * Free dma buffers
2674                  */
2675                 pci_free_consistent(card->device, FST_MAX_MTU,
2676                                     card->rx_dma_handle_host,
2677                                     card->rx_dma_handle_card);
2678                 pci_free_consistent(card->device, FST_MAX_MTU,
2679                                     card->tx_dma_handle_host,
2680                                     card->tx_dma_handle_card);
2681         }
2682         fst_card_array[card->card_no] = NULL;
2683 }
2684
2685 static struct pci_driver fst_driver = {
2686         .name           = FST_NAME,
2687         .id_table       = fst_pci_dev_id,
2688         .probe          = fst_add_one,
2689         .remove = __devexit_p(fst_remove_one),
2690         .suspend        = NULL,
2691         .resume = NULL,
2692 };
2693
2694 static int __init
2695 fst_init(void)
2696 {
2697         int i;
2698
2699         for (i = 0; i < FST_MAX_CARDS; i++)
2700                 fst_card_array[i] = NULL;
2701         spin_lock_init(&fst_work_q_lock);
2702         return pci_module_init(&fst_driver);
2703 }
2704
2705 static void __exit
2706 fst_cleanup_module(void)
2707 {
2708         printk_info("FarSync WAN driver unloading\n");
2709         pci_unregister_driver(&fst_driver);
2710 }
2711
2712 module_init(fst_init);
2713 module_exit(fst_cleanup_module);