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