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