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