OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [net/] [wan/] [farsync.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *      FarSync X21 driver for Linux (generic HDLC version)
3
 *
4
 *      Actually sync driver for X.21, V.35 and V.24 on FarSync T-series cards
5
 *
6
 *      Copyright (C) 2001 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
 */
16
 
17
#include <linux/module.h>
18
#include <linux/kernel.h>
19
#include <linux/pci.h>
20
#include <linux/ioport.h>
21
#include <linux/init.h>
22
#include <asm/uaccess.h>
23
#include <linux/if.h>
24
#include <linux/hdlc.h>
25
 
26
#include "farsync.h"
27
 
28
 
29
/*
30
 *      Module info
31
 */
32
MODULE_AUTHOR("R.J.Dunlop <bob.dunlop@farsite.co.uk>");
33
MODULE_DESCRIPTION("FarSync T-Series X21 driver. FarSite Communications Ltd.");
34
MODULE_LICENSE("GPL");
35
 
36
EXPORT_NO_SYMBOLS;
37
 
38
 
39
/*      Driver configuration and global parameters
40
 *      ==========================================
41
 */
42
 
43
/*      Number of ports (per card) supported
44
 */
45
#define FST_MAX_PORTS           4
46
 
47
 
48
/*      PCI vendor and device IDs
49
 */
50
#define FSC_PCI_VENDOR_ID       0x1619  /* FarSite Communications Ltd */
51
#define T2P_PCI_DEVICE_ID       0x0400  /* T2P X21 2 port card */
52
#define T4P_PCI_DEVICE_ID       0x0440  /* T4P X21 4 port card */
53
 
54
 
55
/*      Default parameters for the link
56
 */
57
#define FST_TX_QUEUE_LEN        100     /* At 8Mbps a longer queue length is
58
                                         * useful, the syncppp module forces
59
                                         * this down assuming a slower line I
60
                                         * guess.
61
                                         */
62
#define FST_MAX_MTU             8000    /* Huge but possible */
63
#define FST_DEF_MTU             1500    /* Common sane value */
64
 
65
#define FST_TX_TIMEOUT          (2*HZ)
66
 
67
 
68
#ifdef ARPHRD_RAWHDLC
69
#define ARPHRD_MYTYPE   ARPHRD_RAWHDLC  /* Raw frames */
70
#else
71
#define ARPHRD_MYTYPE   ARPHRD_HDLC     /* Cisco-HDLC (keepalives etc) */
72
#endif
73
 
74
 
75
/*      Card shared memory layout
76
 *      =========================
77
 */
78
#pragma pack(1)
79
 
80
/*      This information is derived in part from the FarSite FarSync Smc.h
81
 *      file. Unfortunately various name clashes and the non-portability of the
82
 *      bit field declarations in that file have meant that I have chosen to
83
 *      recreate the information here.
84
 *
85
 *      The SMC (Shared Memory Configuration) has a version number that is
86
 *      incremented every time there is a significant change. This number can
87
 *      be used to check that we have not got out of step with the firmware
88
 *      contained in the .CDE files.
89
 */
90
#define SMC_VERSION 11
91
 
92
#define FST_MEMSIZE 0x100000    /* Size of card memory (1Mb) */
93
 
94
#define SMC_BASE 0x00002000L    /* Base offset of the shared memory window main
95
                                 * configuration structure */
96
#define BFM_BASE 0x00010000L    /* Base offset of the shared memory window DMA
97
                                 * buffers */
98
 
99
#define LEN_TX_BUFFER 8192      /* Size of packet buffers */
100
#define LEN_RX_BUFFER 8192
101
 
102
#define LEN_SMALL_TX_BUFFER 256 /* Size of obsolete buffs used for DOS diags */
103
#define LEN_SMALL_RX_BUFFER 256
104
 
105
#define NUM_TX_BUFFER 2         /* Must be power of 2. Fixed by firmware */
106
#define NUM_RX_BUFFER 8
107
 
108
/* Interrupt retry time in milliseconds */
109
#define INT_RETRY_TIME 2
110
 
111
 
112
/*      The Am186CH/CC processors support a SmartDMA mode using circular pools
113
 *      of buffer descriptors. The structure is almost identical to that used
114
 *      in the LANCE Ethernet controllers. Details available as PDF from the
115
 *      AMD web site: http://www.amd.com/products/epd/processors/\
116
 *                    2.16bitcont/3.am186cxfa/a21914/21914.pdf
117
 */
118
struct txdesc {                 /* Transmit descriptor */
119
        volatile u16 ladr;      /* Low order address of packet. This is a
120
                                 * linear address in the Am186 memory space
121
                                 */
122
        volatile u8  hadr;      /* High order address. Low 4 bits only, high 4
123
                                 * bits must be zero
124
                                 */
125
        volatile u8  bits;      /* Status and config */
126
        volatile u16 bcnt;      /* 2s complement of packet size in low 15 bits.
127
                                 * Transmit terminal count interrupt enable in
128
                                 * top bit.
129
                                 */
130
                 u16 unused;    /* Not used in Tx */
131
};
132
 
133
struct rxdesc {                 /* Receive descriptor */
134
        volatile u16 ladr;      /* Low order address of packet */
135
        volatile u8  hadr;      /* High order address */
136
        volatile u8  bits;      /* Status and config */
137
        volatile u16 bcnt;      /* 2s complement of buffer size in low 15 bits.
138
                                 * Receive terminal count interrupt enable in
139
                                 * top bit.
140
                                 */
141
        volatile u16 mcnt;      /* Message byte count (15 bits) */
142
};
143
 
144
/* Convert a length into the 15 bit 2's complement */
145
/* #define cnv_bcnt(len)   (( ~(len) + 1 ) & 0x7FFF ) */
146
/* Since we need to set the high bit to enable the completion interrupt this
147
 * can be made a lot simpler
148
 */
149
#define cnv_bcnt(len)   (-(len))
150
 
151
/* Status and config bits for the above */
152
#define DMA_OWN         0x80            /* SmartDMA owns the descriptor */
153
#define TX_STP          0x02            /* Tx: start of packet */
154
#define TX_ENP          0x01            /* Tx: end of packet */
155
#define RX_ERR          0x40            /* Rx: error (OR of next 4 bits) */
156
#define RX_FRAM         0x20            /* Rx: framing error */
157
#define RX_OFLO         0x10            /* Rx: overflow error */
158
#define RX_CRC          0x08            /* Rx: CRC error */
159
#define RX_HBUF         0x04            /* Rx: buffer error */
160
#define RX_STP          0x02            /* Rx: start of packet */
161
#define RX_ENP          0x01            /* Rx: end of packet */
162
 
163
 
164
/* Interrupts from the card are caused by various events and these are presented
165
 * in a circular buffer as several events may be processed on one physical int
166
 */
167
#define MAX_CIRBUFF     32
168
 
169
struct cirbuff {
170
        u8 rdindex;             /* read, then increment and wrap */
171
        u8 wrindex;             /* write, then increment and wrap */
172
        u8 evntbuff[MAX_CIRBUFF];
173
};
174
 
175
/* Interrupt event codes.
176
 * Where appropriate the two low order bits indicate the port number
177
 */
178
#define CTLA_CHG        0x18    /* Control signal changed */
179
#define CTLB_CHG        0x19
180
#define CTLC_CHG        0x1A
181
#define CTLD_CHG        0x1B
182
 
183
#define INIT_CPLT       0x20    /* Initialisation complete */
184
#define INIT_FAIL       0x21    /* Initialisation failed */
185
 
186
#define ABTA_SENT       0x24    /* Abort sent */
187
#define ABTB_SENT       0x25
188
#define ABTC_SENT       0x26
189
#define ABTD_SENT       0x27
190
 
191
#define TXA_UNDF        0x28    /* Transmission underflow */
192
#define TXB_UNDF        0x29
193
#define TXC_UNDF        0x2A
194
#define TXD_UNDF        0x2B
195
 
196
 
197
/* Port physical configuration. See farsync.h for field values */
198
struct port_cfg {
199
        u16  lineInterface;     /* Physical interface type */
200
        u8   x25op;             /* Unused at present */
201
        u8   internalClock;     /* 1 => internal clock, 0 => external */
202
        u32  lineSpeed;         /* Speed in bps */
203
};
204
 
205
/* Finally sling all the above together into the shared memory structure.
206
 * Sorry it's a hodge podge of arrays, structures and unused bits, it's been
207
 * evolving under NT for some time so I guess we're stuck with it.
208
 * The structure starts at offset SMC_BASE.
209
 * See farsync.h for some field values.
210
 */
211
struct fst_shared {
212
                                /* DMA descriptor rings */
213
        struct rxdesc rxDescrRing[FST_MAX_PORTS][NUM_RX_BUFFER];
214
        struct txdesc txDescrRing[FST_MAX_PORTS][NUM_TX_BUFFER];
215
 
216
                                /* Obsolete small buffers */
217
        u8  smallRxBuffer[FST_MAX_PORTS][NUM_RX_BUFFER][LEN_SMALL_RX_BUFFER];
218
        u8  smallTxBuffer[FST_MAX_PORTS][NUM_TX_BUFFER][LEN_SMALL_TX_BUFFER];
219
 
220
        u8  taskStatus;         /* 0x00 => initialising, 0x01 => running,
221
                                 * 0xFF => halted
222
                                 */
223
 
224
        u8  interruptHandshake; /* Set to 0x01 by adapter to signal interrupt,
225
                                 * set to 0xEE by host to acknowledge interrupt
226
                                 */
227
 
228
        u16 smcVersion;         /* Must match SMC_VERSION */
229
 
230
        u32 smcFirmwareVersion; /* 0xIIVVRRBB where II = product ID, VV = major
231
                                 * version, RR = revision and BB = build
232
                                 */
233
 
234
        u16 txa_done;           /* Obsolete completion flags */
235
        u16 rxa_done;
236
        u16 txb_done;
237
        u16 rxb_done;
238
        u16 txc_done;
239
        u16 rxc_done;
240
        u16 txd_done;
241
        u16 rxd_done;
242
 
243
        u16 mailbox[4];         /* Diagnostics mailbox. Not used */
244
 
245
        struct cirbuff interruptEvent;  /* interrupt causes */
246
 
247
        u32 v24IpSts[FST_MAX_PORTS];    /* V.24 control input status */
248
        u32 v24OpSts[FST_MAX_PORTS];    /* V.24 control output status */
249
 
250
        struct port_cfg portConfig[FST_MAX_PORTS];
251
 
252
        u16 clockStatus[FST_MAX_PORTS]; /* lsb: 0=> present, 1=> absent */
253
 
254
        u16 cableStatus;                /* lsb: 0=> present, 1=> absent */
255
 
256
        u16 txDescrIndex[FST_MAX_PORTS]; /* transmit descriptor ring index */
257
        u16 rxDescrIndex[FST_MAX_PORTS]; /* receive descriptor ring index */
258
 
259
        u16 portMailbox[FST_MAX_PORTS][2];      /* command, modifier */
260
        u16 cardMailbox[4];                     /* Not used */
261
 
262
                                /* Number of times that card thinks the host has
263
                                 * missed an interrupt by not acknowledging
264
                                 * within 2mS (I guess NT has problems)
265
                                 */
266
        u32 interruptRetryCount;
267
 
268
                                /* Driver private data used as an ID. We'll not
269
                                 * use this on Linux I'd rather keep such things
270
                                 * in main memory rather than on the PCI bus
271
                                 */
272
        u32 portHandle[FST_MAX_PORTS];
273
 
274
                                /* Count of Tx underflows for stats */
275
        u32 transmitBufferUnderflow[FST_MAX_PORTS];
276
 
277
                                /* Debounced V.24 control input status */
278
        u32 v24DebouncedSts[FST_MAX_PORTS];
279
 
280
                                /* Adapter debounce timers. Don't touch */
281
        u32 ctsTimer[FST_MAX_PORTS];
282
        u32 ctsTimerRun[FST_MAX_PORTS];
283
        u32 dcdTimer[FST_MAX_PORTS];
284
        u32 dcdTimerRun[FST_MAX_PORTS];
285
 
286
        u32 numberOfPorts;      /* Number of ports detected at startup */
287
 
288
        u16 _reserved[64];
289
 
290
        u16 cardMode;           /* Bit-mask to enable features:
291
                                 * Bit 0: 1 enables LED identify mode
292
                                 */
293
 
294
        u16 portScheduleOffset;
295
 
296
        u32 endOfSmcSignature;  /* endOfSmcSignature MUST be the last member of
297
                                 * the structure and marks the end of the shared
298
                                 * memory. Adapter code initializes its value as
299
                                 * END_SIG.
300
                                 */
301
};
302
 
303
/* endOfSmcSignature value */
304
#define END_SIG                 0x12345678
305
 
306
/* Mailbox values. (portMailbox) */
307
#define NOP             0       /* No operation */
308
#define ACK             1       /* Positive acknowledgement to PC driver */
309
#define NAK             2       /* Negative acknowledgement to PC driver */
310
#define STARTPORT       3       /* Start an HDLC port */
311
#define STOPPORT        4       /* Stop an HDLC port */
312
#define ABORTTX         5       /* Abort the transmitter for a port */
313
#define SETV24O         6       /* Set V24 outputs */
314
 
315
 
316
/* Larger buffers are positioned in memory at offset BFM_BASE */
317
struct buf_window {
318
        u8 txBuffer[FST_MAX_PORTS][NUM_TX_BUFFER][LEN_TX_BUFFER];
319
        u8 rxBuffer[FST_MAX_PORTS][NUM_RX_BUFFER][LEN_RX_BUFFER];
320
};
321
 
322
/* Calculate offset of a buffer object within the shared memory window */
323
#define BUF_OFFSET(X)   ((unsigned int)&(((struct buf_window *)BFM_BASE)->X))
324
 
325
#pragma pack()
326
 
327
 
328
/*      Device driver private information
329
 *      =================================
330
 */
331
/*      Per port (line or channel) information
332
 */
333
struct fst_port_info {
334
        hdlc_device             hdlc;   /* HDLC device struct - must be first */
335
        struct fst_card_info   *card;   /* Card we're associated with */
336
        int                     index;  /* Port index on the card */
337
        int                     hwif;   /* Line hardware (lineInterface copy) */
338
        int                     run;    /* Port is running */
339
        int                     rxpos;  /* Next Rx buffer to use */
340
        int                     txpos;  /* Next Tx buffer to use */
341
        int                     txipos; /* Next Tx buffer to check for free */
342
        int                     txcnt;  /* Count of Tx buffers in use */
343
};
344
 
345
/*      Per card information
346
 */
347
struct fst_card_info {
348
        char          *mem;             /* Card memory mapped to kernel space */
349
        char          *ctlmem;          /* Control memory for PCI cards */
350
        unsigned int   phys_mem;        /* Physical memory window address */
351
        unsigned int   phys_ctlmem;     /* Physical control memory address */
352
        unsigned int   irq;             /* Interrupt request line number */
353
        unsigned int   nports;          /* Number of serial ports */
354
        unsigned int   type;            /* Type index of card */
355
        unsigned int   state;           /* State of card */
356
        spinlock_t     card_lock;       /* Lock for SMP access */
357
        unsigned short pci_conf;        /* PCI card config in I/O space */
358
                                        /* Per port info */
359
        struct fst_port_info ports[ FST_MAX_PORTS ];
360
};
361
 
362
/* Convert an HDLC device pointer into a port info pointer and similar */
363
#define hdlc_to_port(H) ((struct fst_port_info *)(H))
364
#define dev_to_port(D)  hdlc_to_port(dev_to_hdlc(D))
365
#define port_to_dev(P)  hdlc_to_dev(&(P)->hdlc)
366
 
367
 
368
/*
369
 *      Shared memory window access macros
370
 *
371
 *      We have a nice memory based structure above, which could be directly
372
 *      mapped on i386 but might not work on other architectures unless we use
373
 *      the readb,w,l and writeb,w,l macros. Unfortunately these macros take
374
 *      physical offsets so we have to convert. The only saving grace is that
375
 *      this should all collapse back to a simple indirection eventually.
376
 */
377
#define WIN_OFFSET(X)   ((long)&(((struct fst_shared *)SMC_BASE)->X))
378
 
379
#define FST_RDB(C,E)    readb ((C)->mem + WIN_OFFSET(E))
380
#define FST_RDW(C,E)    readw ((C)->mem + WIN_OFFSET(E))
381
#define FST_RDL(C,E)    readl ((C)->mem + WIN_OFFSET(E))
382
 
383
#define FST_WRB(C,E,B)  writeb ((B), (C)->mem + WIN_OFFSET(E))
384
#define FST_WRW(C,E,W)  writew ((W), (C)->mem + WIN_OFFSET(E))
385
#define FST_WRL(C,E,L)  writel ((L), (C)->mem + WIN_OFFSET(E))
386
 
387
 
388
/*
389
 *      Debug support
390
 */
391
#if FST_DEBUG
392
 
393
static int fst_debug_mask = { FST_DEBUG };
394
 
395
/* Most common debug activity is to print something if the corresponding bit
396
 * is set in the debug mask. Note: this uses a non-ANSI extension in GCC to
397
 * support variable numbers of macro parameters. The inverted if prevents us
398
 * eating someone else's else clause.
399
 */
400
#define dbg(F,fmt,A...) if ( ! ( fst_debug_mask & (F))) \
401
                                ; \
402
                        else \
403
                                printk ( KERN_DEBUG FST_NAME ": " fmt, ## A )
404
 
405
#else
406
# define dbg(X...)      /* NOP */
407
#endif
408
 
409
 
410
/*      Printing short cuts
411
 */
412
#define printk_err(fmt,A...)    printk ( KERN_ERR     FST_NAME ": " fmt, ## A )
413
#define printk_warn(fmt,A...)   printk ( KERN_WARNING FST_NAME ": " fmt, ## A )
414
#define printk_info(fmt,A...)   printk ( KERN_INFO    FST_NAME ": " fmt, ## A )
415
 
416
 
417
/*
418
 *      PCI ID lookup table
419
 */
420
static struct pci_device_id fst_pci_dev_id[] __devinitdata = {
421
        { FSC_PCI_VENDOR_ID, T2P_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
422
                                        FST_TYPE_T2P },
423
        { FSC_PCI_VENDOR_ID, T4P_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
424
                                        FST_TYPE_T4P },
425
        { 0, }                          /* End */
426
};
427
 
428
MODULE_DEVICE_TABLE ( pci, fst_pci_dev_id );
429
 
430
 
431
/*      Card control functions
432
 *      ======================
433
 */
434
/*      Place the processor in reset state
435
 *
436
 * Used to be a simple write to card control space but a glitch in the latest
437
 * AMD Am186CH processor means that we now have to do it by asserting and de-
438
 * asserting the PLX chip PCI Adapter Software Reset. Bit 30 in CNTRL register
439
 * at offset 0x50.
440
 */
441
static inline void
442
fst_cpureset ( struct fst_card_info *card )
443
{
444
        unsigned int regval;
445
 
446
        regval = inl ( card->pci_conf + 0x50 );
447
 
448
        outl ( regval |  0x40000000, card->pci_conf + 0x50 );
449
        outl ( regval & ~0x40000000, card->pci_conf + 0x50 );
450
}
451
 
452
/*      Release the processor from reset
453
 */
454
static inline void
455
fst_cpurelease ( struct fst_card_info *card )
456
{
457
        (void) readb ( card->ctlmem );
458
}
459
 
460
/*      Clear the cards interrupt flag
461
 */
462
static inline void
463
fst_clear_intr ( struct fst_card_info *card )
464
{
465
        /* Poke the appropriate PLX chip register (same as enabling interrupts)
466
         */
467
        outw ( 0x0543, card->pci_conf + 0x4C );
468
}
469
 
470
/*      Disable card interrupts
471
 */
472
static inline void
473
fst_disable_intr ( struct fst_card_info *card )
474
{
475
        outw ( 0x0000, card->pci_conf + 0x4C );
476
}
477
 
478
 
479
/*      Issue a Mailbox command for a port.
480
 *      Note we issue them on a fire and forget basis, not expecting to see an
481
 *      error and not waiting for completion.
482
 */
483
static void
484
fst_issue_cmd ( struct fst_port_info *port, unsigned short cmd )
485
{
486
        struct fst_card_info *card;
487
        unsigned short mbval;
488
        unsigned long flags;
489
        int safety;
490
 
491
        card = port->card;
492
        spin_lock_irqsave ( &card->card_lock, flags );
493
        mbval = FST_RDW ( card, portMailbox[port->index][0]);
494
 
495
        safety = 0;
496
        /* Wait for any previous command to complete */
497
        while ( mbval > NAK )
498
        {
499
                spin_unlock_irqrestore ( &card->card_lock, flags );
500
                schedule_timeout ( 1 );
501
                spin_lock_irqsave ( &card->card_lock, flags );
502
 
503
                if ( ++safety > 1000 )
504
                {
505
                        printk_err ("Mailbox safety timeout\n");
506
                        break;
507
                }
508
 
509
                mbval = FST_RDW ( card, portMailbox[port->index][0]);
510
        }
511
        if ( safety > 0 )
512
        {
513
                dbg ( DBG_CMD,"Mailbox clear after %d jiffies\n", safety );
514
        }
515
        if ( mbval == NAK )
516
        {
517
                dbg ( DBG_CMD,"issue_cmd: previous command was NAK'd\n");
518
        }
519
 
520
        FST_WRW ( card, portMailbox[port->index][0], cmd );
521
 
522
        if ( cmd == ABORTTX || cmd == STARTPORT )
523
        {
524
                port->txpos  = 0;
525
                port->txipos = 0;
526
                port->txcnt  = 0;
527
        }
528
 
529
        spin_unlock_irqrestore ( &card->card_lock, flags );
530
}
531
 
532
 
533
/*      Port output signals control
534
 */
535
static inline void
536
fst_op_raise ( struct fst_port_info *port, unsigned int outputs )
537
{
538
        outputs |= FST_RDL ( port->card, v24OpSts[port->index]);
539
        FST_WRL ( port->card, v24OpSts[port->index], outputs );
540
 
541
        if ( port->run )
542
                fst_issue_cmd ( port, SETV24O );
543
}
544
 
545
static inline void
546
fst_op_lower ( struct fst_port_info *port, unsigned int outputs )
547
{
548
        outputs = ~outputs & FST_RDL ( port->card, v24OpSts[port->index]);
549
        FST_WRL ( port->card, v24OpSts[port->index], outputs );
550
 
551
        if ( port->run )
552
                fst_issue_cmd ( port, SETV24O );
553
}
554
 
555
 
556
/*
557
 *      Setup port Rx buffers
558
 */
559
static void
560
fst_rx_config ( struct fst_port_info *port )
561
{
562
        int i;
563
        int pi;
564
        unsigned int offset;
565
        unsigned long flags;
566
        struct fst_card_info *card;
567
 
568
        pi   = port->index;
569
        card = port->card;
570
        spin_lock_irqsave ( &card->card_lock, flags );
571
        for ( i = 0 ; i < NUM_RX_BUFFER ; i++ )
572
        {
573
                offset = BUF_OFFSET ( rxBuffer[pi][i][0]);
574
 
575
                FST_WRW ( card, rxDescrRing[pi][i].ladr, (u16) offset );
576
                FST_WRB ( card, rxDescrRing[pi][i].hadr, (u8)( offset >> 16 ));
577
                FST_WRW ( card, rxDescrRing[pi][i].bcnt,
578
                                        cnv_bcnt ( LEN_RX_BUFFER ));
579
                FST_WRW ( card, rxDescrRing[pi][i].mcnt, 0 );
580
                FST_WRB ( card, rxDescrRing[pi][i].bits, DMA_OWN );
581
        }
582
        port->rxpos  = 0;
583
        spin_unlock_irqrestore ( &card->card_lock, flags );
584
}
585
 
586
 
587
/*
588
 *      Setup port Tx buffers
589
 */
590
static void
591
fst_tx_config ( struct fst_port_info *port )
592
{
593
        int i;
594
        int pi;
595
        unsigned int offset;
596
        unsigned long flags;
597
        struct fst_card_info *card;
598
 
599
        pi   = port->index;
600
        card = port->card;
601
        spin_lock_irqsave ( &card->card_lock, flags );
602
        for ( i = 0 ; i < NUM_TX_BUFFER ; i++ )
603
        {
604
                offset = BUF_OFFSET ( txBuffer[pi][i][0]);
605
 
606
                FST_WRW ( card, txDescrRing[pi][i].ladr, (u16) offset );
607
                FST_WRB ( card, txDescrRing[pi][i].hadr, (u8)( offset >> 16 ));
608
                FST_WRW ( card, txDescrRing[pi][i].bcnt, 0 );
609
                FST_WRB ( card, txDescrRing[pi][i].bits, 0 );
610
        }
611
        port->txpos  = 0;
612
        port->txipos = 0;
613
        port->txcnt  = 0;
614
        spin_unlock_irqrestore ( &card->card_lock, flags );
615
}
616
 
617
 
618
/*      Control signal change interrupt event
619
 */
620
static void
621
fst_intr_ctlchg ( struct fst_card_info *card, struct fst_port_info *port )
622
{
623
        int signals;
624
 
625
        signals = FST_RDL ( card, v24DebouncedSts[port->index]);
626
 
627
        if ( signals & (( port->hwif == X21 ) ? IPSTS_INDICATE : IPSTS_DCD ))
628
        {
629
                if ( ! netif_carrier_ok ( port_to_dev ( port )))
630
                {
631
                        dbg ( DBG_INTR,"DCD active\n");
632
                        netif_carrier_on ( port_to_dev ( port ));
633
                }
634
        }
635
        else
636
        {
637
                if ( netif_carrier_ok ( port_to_dev ( port )))
638
                {
639
                        dbg ( DBG_INTR,"DCD lost\n");
640
                        netif_carrier_off ( port_to_dev ( port ));
641
                }
642
        }
643
}
644
 
645
 
646
/*      Rx complete interrupt
647
 */
648
static void
649
fst_intr_rx ( struct fst_card_info *card, struct fst_port_info *port )
650
{
651
        unsigned char dmabits;
652
        int pi;
653
        int rxp;
654
        unsigned short len;
655
        struct sk_buff *skb;
656
        int i;
657
 
658
 
659
        /* Check we have a buffer to process */
660
        pi  = port->index;
661
        rxp = port->rxpos;
662
        dmabits = FST_RDB ( card, rxDescrRing[pi][rxp].bits );
663
        if ( dmabits & DMA_OWN )
664
        {
665
                dbg ( DBG_RX | DBG_INTR,"intr_rx: No buffer port %d pos %d\n",
666
                                        pi, rxp );
667
                return;
668
        }
669
 
670
        /* Get buffer length */
671
        len = FST_RDW ( card, rxDescrRing[pi][rxp].mcnt );
672
        /* Discard the CRC */
673
        len -= 2;
674
 
675
        /* Check buffer length and for other errors. We insist on one packet
676
         * in one buffer. This simplifies things greatly and since we've
677
         * allocated 8K it shouldn't be a real world limitation
678
         */
679
        dbg ( DBG_RX,"intr_rx: %d,%d: flags %x len %d\n", pi, rxp, dmabits,
680
                                        len );
681
        if ( dmabits != ( RX_STP | RX_ENP ) || len > LEN_RX_BUFFER - 2 )
682
        {
683
                port->hdlc.stats.rx_errors++;
684
 
685
                /* Update error stats and discard buffer */
686
                if ( dmabits & RX_OFLO )
687
                {
688
                        port->hdlc.stats.rx_fifo_errors++;
689
                }
690
                if ( dmabits & RX_CRC )
691
                {
692
                        port->hdlc.stats.rx_crc_errors++;
693
                }
694
                if ( dmabits & RX_FRAM )
695
                {
696
                        port->hdlc.stats.rx_frame_errors++;
697
                }
698
                if ( dmabits == ( RX_STP | RX_ENP ))
699
                {
700
                        port->hdlc.stats.rx_length_errors++;
701
                }
702
 
703
                /* Discard buffer descriptors until we see the end of packet
704
                 * marker
705
                 */
706
                i = 0;
707
                while (( dmabits & ( DMA_OWN | RX_ENP )) == 0 )
708
                {
709
                        FST_WRB ( card, rxDescrRing[pi][rxp].bits, DMA_OWN );
710
                        if ( ++rxp >= NUM_RX_BUFFER )
711
                                rxp = 0;
712
                        if ( ++i > NUM_RX_BUFFER )
713
                        {
714
                                dbg ( DBG_ASS,"intr_rx: Discarding more bufs"
715
                                                " than we have\n");
716
                                break;
717
                        }
718
                        dmabits = FST_RDB ( card, rxDescrRing[pi][rxp].bits );
719
                }
720
 
721
                /* Discard the terminal buffer */
722
                if ( ! ( dmabits & DMA_OWN ))
723
                {
724
                        FST_WRB ( card, rxDescrRing[pi][rxp].bits, DMA_OWN );
725
                        if ( ++rxp >= NUM_RX_BUFFER )
726
                                rxp = 0;
727
                }
728
                port->rxpos = rxp;
729
                return;
730
        }
731
 
732
        /* Allocate SKB */
733
        if (( skb = dev_alloc_skb ( len )) == NULL )
734
        {
735
                dbg ( DBG_RX,"intr_rx: can't allocate buffer\n");
736
 
737
                port->hdlc.stats.rx_dropped++;
738
 
739
                /* Return descriptor to card */
740
                FST_WRB ( card, rxDescrRing[pi][rxp].bits, DMA_OWN );
741
 
742
                if ( ++rxp >= NUM_RX_BUFFER )
743
                        port->rxpos = 0;
744
                else
745
                        port->rxpos = rxp;
746
                return;
747
        }
748
 
749
        memcpy_fromio ( skb_put ( skb, len ),
750
                                card->mem + BUF_OFFSET ( rxBuffer[pi][rxp][0]),
751
                                len );
752
 
753
        /* Reset buffer descriptor */
754
        FST_WRB ( card, rxDescrRing[pi][rxp].bits, DMA_OWN );
755
        if ( ++rxp >= NUM_RX_BUFFER )
756
                port->rxpos = 0;
757
        else
758
                port->rxpos = rxp;
759
 
760
        /* Update stats */
761
        port->hdlc.stats.rx_packets++;
762
        port->hdlc.stats.rx_bytes += len;
763
 
764
        /* Push upstream */
765
        skb->mac.raw = skb->data;
766
        skb->dev = hdlc_to_dev ( &port->hdlc );
767
        skb->protocol = hdlc_type_trans(skb, skb->dev);
768
        netif_rx ( skb );
769
 
770
        port_to_dev ( port )->last_rx = jiffies;
771
}
772
 
773
 
774
/*
775
 *      The interrupt service routine
776
 *      Dev_id is our fst_card_info pointer
777
 */
778
static void
779
fst_intr ( int irq, void *dev_id, struct pt_regs *regs )
780
{
781
        struct fst_card_info *card;
782
        struct fst_port_info *port;
783
        int rdidx;                      /* Event buffer indices */
784
        int wridx;
785
        int event;                      /* Actual event for processing */
786
        int pi;
787
 
788
        if (( card = dev_id ) == NULL )
789
        {
790
                dbg ( DBG_INTR,"intr: spurious %d\n", irq );
791
                return;
792
        }
793
 
794
        dbg ( DBG_INTR,"intr: %d %p\n", irq, card );
795
 
796
        spin_lock ( &card->card_lock );
797
 
798
        /* Clear and reprime the interrupt source */
799
        fst_clear_intr ( card );
800
 
801
        /* Set the software acknowledge */
802
        FST_WRB ( card, interruptHandshake, 0xEE );
803
 
804
        /* Drain the event queue */
805
        rdidx = FST_RDB ( card, interruptEvent.rdindex );
806
        wridx = FST_RDB ( card, interruptEvent.wrindex );
807
        while ( rdidx != wridx )
808
        {
809
                event = FST_RDB ( card, interruptEvent.evntbuff[rdidx]);
810
 
811
                port = &card->ports[event & 0x03];
812
 
813
                dbg ( DBG_INTR,"intr: %x\n", event );
814
 
815
                switch ( event )
816
                {
817
                case CTLA_CHG:
818
                case CTLB_CHG:
819
                case CTLC_CHG:
820
                case CTLD_CHG:
821
                        if ( port->run )
822
                                fst_intr_ctlchg ( card, port );
823
                        break;
824
 
825
                case ABTA_SENT:
826
                case ABTB_SENT:
827
                case ABTC_SENT:
828
                case ABTD_SENT:
829
                        dbg ( DBG_TX,"Abort complete port %d\n", event & 0x03 );
830
                        break;
831
 
832
                case TXA_UNDF:
833
                case TXB_UNDF:
834
                case TXC_UNDF:
835
                case TXD_UNDF:
836
                        /* Difficult to see how we'd get this given that we
837
                         * always load up the entire packet for DMA.
838
                         */
839
                        dbg ( DBG_TX,"Tx underflow port %d\n", event & 0x03 );
840
                        port->hdlc.stats.tx_errors++;
841
                        port->hdlc.stats.tx_fifo_errors++;
842
                        break;
843
 
844
                case INIT_CPLT:
845
                        dbg ( DBG_INIT,"Card init OK intr\n");
846
                        break;
847
 
848
                case INIT_FAIL:
849
                        dbg ( DBG_INIT,"Card init FAILED intr\n");
850
                        card->state = FST_IFAILED;
851
                        break;
852
 
853
                default:
854
                        printk_err ("intr: unknown card event code. ignored\n");
855
                        break;
856
                }
857
 
858
                /* Bump and wrap the index */
859
                if ( ++rdidx >= MAX_CIRBUFF )
860
                        rdidx = 0;
861
        }
862
        FST_WRB ( card, interruptEvent.rdindex, rdidx );
863
 
864
        for ( pi = 0, port = card->ports ; pi < card->nports ; pi++, port++ )
865
        {
866
                if ( ! port->run )
867
                        continue;
868
 
869
                /* Check for rx completions */
870
                while ( ! ( FST_RDB ( card, rxDescrRing[pi][port->rxpos].bits )
871
                                                                & DMA_OWN ))
872
                {
873
                        fst_intr_rx ( card, port );
874
                }
875
 
876
                /* Check for Tx completions */
877
                while ( port->txcnt > 0 && ! ( FST_RDB ( card,
878
                        txDescrRing[pi][port->txipos].bits ) & DMA_OWN ))
879
                {
880
                        --port->txcnt;
881
                        if ( ++port->txipos >= NUM_TX_BUFFER )
882
                                port->txipos = 0;
883
                        netif_wake_queue ( port_to_dev ( port ));
884
                }
885
        }
886
 
887
        spin_unlock ( &card->card_lock );
888
}
889
 
890
 
891
/*      Check that the shared memory configuration is one that we can handle
892
 *      and that some basic parameters are correct
893
 */
894
static void
895
check_started_ok ( struct fst_card_info *card )
896
{
897
        int i;
898
 
899
        /* Check structure version and end marker */
900
        if ( FST_RDW ( card, smcVersion ) != SMC_VERSION )
901
        {
902
                printk_err ("Bad shared memory version %d expected %d\n",
903
                                FST_RDW ( card, smcVersion ), SMC_VERSION );
904
                card->state = FST_BADVERSION;
905
                return;
906
        }
907
        if ( FST_RDL ( card, endOfSmcSignature ) != END_SIG )
908
        {
909
                printk_err ("Missing shared memory signature\n");
910
                card->state = FST_BADVERSION;
911
                return;
912
        }
913
        /* Firmware status flag, 0x00 = initialising, 0x01 = OK, 0xFF = fail */
914
        if (( i = FST_RDB ( card, taskStatus )) == 0x01 )
915
        {
916
                card->state = FST_RUNNING;
917
        }
918
        else if ( i == 0xFF )
919
        {
920
                printk_err ("Firmware initialisation failed. Card halted\n");
921
                card->state = FST_HALTED;
922
                return;
923
        }
924
        else if ( i != 0x00 )
925
        {
926
                printk_err ("Unknown firmware status 0x%x\n", i );
927
                card->state = FST_HALTED;
928
                return;
929
        }
930
 
931
        /* Finally check the number of ports reported by firmware against the
932
         * number we assumed at card detection. Should never happen with
933
         * existing firmware etc so we just report it for the moment.
934
         */
935
        if ( FST_RDL ( card, numberOfPorts ) != card->nports )
936
        {
937
                printk_warn ("Port count mismatch."
938
                                " Firmware thinks %d we say %d\n",
939
                                FST_RDL ( card, numberOfPorts ), card->nports );
940
        }
941
}
942
 
943
 
944
static int
945
set_conf_from_info ( struct fst_card_info *card, struct fst_port_info *port,
946
                struct fstioc_info *info )
947
{
948
        int err;
949
 
950
        /* Set things according to the user set valid flags.
951
         * Several of the old options have been invalidated/replaced by the
952
         * generic HDLC package.
953
         */
954
        err = 0;
955
        if ( info->valid & FSTVAL_PROTO )
956
                err = -EINVAL;
957
        if ( info->valid & FSTVAL_CABLE )
958
                err = -EINVAL;
959
        if ( info->valid & FSTVAL_SPEED )
960
                err = -EINVAL;
961
 
962
        if ( info->valid & FSTVAL_MODE )
963
                FST_WRW ( card, cardMode, info->cardMode );
964
#if FST_DEBUG
965
        if ( info->valid & FSTVAL_DEBUG )
966
                fst_debug_mask = info->debug;
967
#endif
968
 
969
        return err;
970
}
971
 
972
static void
973
gather_conf_info ( struct fst_card_info *card, struct fst_port_info *port,
974
                struct fstioc_info *info )
975
{
976
        int i;
977
 
978
        memset ( info, 0, sizeof ( struct fstioc_info ));
979
 
980
        i = port->index;
981
        info->nports = card->nports;
982
        info->type   = card->type;
983
        info->state  = card->state;
984
        info->proto  = FST_GEN_HDLC;
985
        info->index  = i;
986
#if FST_DEBUG
987
        info->debug  = fst_debug_mask;
988
#endif
989
 
990
        /* Only mark information as valid if card is running.
991
         * Copy the data anyway in case it is useful for diagnostics
992
         */
993
        info->valid
994
                = (( card->state == FST_RUNNING ) ? FSTVAL_ALL : FSTVAL_CARD )
995
#if FST_DEBUG
996
                | FSTVAL_DEBUG
997
#endif
998
                ;
999
 
1000
        info->lineInterface = FST_RDW ( card, portConfig[i].lineInterface );
1001
        info->internalClock = FST_RDB ( card, portConfig[i].internalClock );
1002
        info->lineSpeed     = FST_RDL ( card, portConfig[i].lineSpeed );
1003
        info->v24IpSts      = FST_RDL ( card, v24IpSts[i] );
1004
        info->v24OpSts      = FST_RDL ( card, v24OpSts[i] );
1005
        info->clockStatus   = FST_RDW ( card, clockStatus[i] );
1006
        info->cableStatus   = FST_RDW ( card, cableStatus );
1007
        info->cardMode      = FST_RDW ( card, cardMode );
1008
        info->smcFirmwareVersion  = FST_RDL ( card, smcFirmwareVersion );
1009
}
1010
 
1011
 
1012
static int
1013
fst_set_iface ( struct fst_card_info *card, struct fst_port_info *port,
1014
                struct ifreq *ifr )
1015
{
1016
        sync_serial_settings sync;
1017
        int i;
1018
 
1019
        if (copy_from_user (&sync, ifr->ifr_settings.ifs_ifsu.sync,
1020
                            sizeof (sync)))
1021
                return -EFAULT;
1022
 
1023
        if ( sync.loopback )
1024
                return -EINVAL;
1025
 
1026
        i = port->index;
1027
 
1028
        switch (ifr->ifr_settings.type)
1029
        {
1030
        case IF_IFACE_V35:
1031
                FST_WRW ( card, portConfig[i].lineInterface, V35 );
1032
                port->hwif = V35;
1033
                break;
1034
 
1035
        case IF_IFACE_V24:
1036
                FST_WRW ( card, portConfig[i].lineInterface, V24 );
1037
                port->hwif = V24;
1038
                break;
1039
 
1040
        case IF_IFACE_X21:
1041
                FST_WRW ( card, portConfig[i].lineInterface, X21 );
1042
                port->hwif = X21;
1043
                break;
1044
 
1045
        case IF_IFACE_SYNC_SERIAL:
1046
                break;
1047
 
1048
        default:
1049
                return -EINVAL;
1050
        }
1051
 
1052
        switch ( sync.clock_type )
1053
        {
1054
        case CLOCK_EXT:
1055
                FST_WRB ( card, portConfig[i].internalClock, EXTCLK );
1056
                break;
1057
 
1058
        case CLOCK_INT:
1059
                FST_WRB ( card, portConfig[i].internalClock, INTCLK );
1060
                break;
1061
 
1062
        default:
1063
                return -EINVAL;
1064
        }
1065
        FST_WRL ( card, portConfig[i].lineSpeed, sync.clock_rate );
1066
        return 0;
1067
}
1068
 
1069
static int
1070
fst_get_iface ( struct fst_card_info *card, struct fst_port_info *port,
1071
                struct ifreq *ifr )
1072
{
1073
        sync_serial_settings sync;
1074
        int i;
1075
 
1076
        /* First check what line type is set, we'll default to reporting X.21
1077
         * if nothing is set as IF_IFACE_SYNC_SERIAL implies it can't be
1078
         * changed
1079
         */
1080
        switch ( port->hwif )
1081
        {
1082
        case V35:
1083
                ifr->ifr_settings.type = IF_IFACE_V35;
1084
                break;
1085
        case V24:
1086
                ifr->ifr_settings.type = IF_IFACE_V24;
1087
                break;
1088
        case X21:
1089
        default:
1090
                ifr->ifr_settings.type = IF_IFACE_X21;
1091
                break;
1092
        }
1093
 
1094
        if (ifr->ifr_settings.size < sizeof(sync)) {
1095
                ifr->ifr_settings.size = sizeof(sync); /* data size wanted */
1096
                return -ENOBUFS;
1097
        }
1098
 
1099
        i = port->index;
1100
        sync.clock_rate = FST_RDL ( card, portConfig[i].lineSpeed );
1101
        /* Lucky card and linux use same encoding here */
1102
        sync.clock_type = FST_RDB ( card, portConfig[i].internalClock );
1103
        sync.loopback = 0;
1104
 
1105
        if (copy_to_user (ifr->ifr_settings.ifs_ifsu.sync, &sync,
1106
                          sizeof(sync)))
1107
                return -EFAULT;
1108
 
1109
        return 0;
1110
}
1111
 
1112
 
1113
static int
1114
fst_ioctl ( struct net_device *dev, struct ifreq *ifr, int cmd )
1115
{
1116
        struct fst_card_info *card;
1117
        struct fst_port_info *port;
1118
        struct fstioc_write wrthdr;
1119
        struct fstioc_info info;
1120
        unsigned long flags;
1121
 
1122
        dbg ( DBG_IOCTL,"ioctl: %x, %p\n", cmd, ifr->ifr_data );
1123
 
1124
        port = dev_to_port ( dev );
1125
        card = port->card;
1126
 
1127
        if ( !capable ( CAP_NET_ADMIN ))
1128
                return -EPERM;
1129
 
1130
        switch ( cmd )
1131
        {
1132
        case FSTCPURESET:
1133
                fst_cpureset ( card );
1134
                card->state = FST_RESET;
1135
                return 0;
1136
 
1137
        case FSTCPURELEASE:
1138
                fst_cpurelease ( card );
1139
                card->state = FST_STARTING;
1140
                return 0;
1141
 
1142
        case FSTWRITE:                  /* Code write (download) */
1143
 
1144
                /* First copy in the header with the length and offset of data
1145
                 * to write
1146
                 */
1147
                if ( ifr->ifr_data == NULL )
1148
                {
1149
                        return -EINVAL;
1150
                }
1151
                if ( copy_from_user ( &wrthdr, ifr->ifr_data,
1152
                                        sizeof ( struct fstioc_write )))
1153
                {
1154
                        return -EFAULT;
1155
                }
1156
 
1157
                /* Sanity check the parameters. We don't support partial writes
1158
                 * when going over the top
1159
                 */
1160
                if ( wrthdr.size > FST_MEMSIZE || wrthdr.offset > FST_MEMSIZE
1161
                                || wrthdr.size + wrthdr.offset > FST_MEMSIZE )
1162
                {
1163
                        return -ENXIO;
1164
                }
1165
 
1166
                /* Now copy the data to the card.
1167
                 * This will probably break on some architectures.
1168
                 * I'll fix it when I have something to test on.
1169
                 */
1170
                if ( copy_from_user ( card->mem + wrthdr.offset,
1171
                                ifr->ifr_data + sizeof ( struct fstioc_write ),
1172
                                wrthdr.size ))
1173
                {
1174
                        return -EFAULT;
1175
                }
1176
 
1177
                /* Writes to the memory of a card in the reset state constitute
1178
                 * a download
1179
                 */
1180
                if ( card->state == FST_RESET )
1181
                {
1182
                        card->state = FST_DOWNLOAD;
1183
                }
1184
                return 0;
1185
 
1186
        case FSTGETCONF:
1187
 
1188
                /* If card has just been started check the shared memory config
1189
                 * version and marker
1190
                 */
1191
                if ( card->state == FST_STARTING )
1192
                {
1193
                        check_started_ok ( card );
1194
 
1195
                        /* If everything checked out enable card interrupts */
1196
                        if ( card->state == FST_RUNNING )
1197
                        {
1198
                                spin_lock_irqsave ( &card->card_lock, flags );
1199
                                fst_clear_intr ( card );
1200
                                FST_WRB ( card, interruptHandshake, 0xEE );
1201
                                spin_unlock_irqrestore ( &card->card_lock,
1202
                                                                flags );
1203
                        }
1204
                }
1205
 
1206
                if ( ifr->ifr_data == NULL )
1207
                {
1208
                        return -EINVAL;
1209
                }
1210
 
1211
                gather_conf_info ( card, port, &info );
1212
 
1213
                if ( copy_to_user ( ifr->ifr_data, &info, sizeof ( info )))
1214
                {
1215
                        return -EFAULT;
1216
                }
1217
                return 0;
1218
 
1219
        case FSTSETCONF:
1220
 
1221
                /* Most of the setting have been moved to the generic ioctls
1222
                 * this just covers debug and board ident mode now
1223
                 */
1224
                if ( copy_from_user ( &info,  ifr->ifr_data, sizeof ( info )))
1225
                {
1226
                        return -EFAULT;
1227
                }
1228
 
1229
                return set_conf_from_info ( card, port, &info );
1230
 
1231
        case SIOCWANDEV:
1232
                switch (ifr->ifr_settings.type)
1233
                {
1234
                case IF_GET_IFACE:
1235
                        return fst_get_iface ( card, port, ifr );
1236
 
1237
                case IF_IFACE_SYNC_SERIAL:
1238
                case IF_IFACE_V35:
1239
                case IF_IFACE_V24:
1240
                case IF_IFACE_X21:
1241
                        return fst_set_iface ( card, port, ifr );
1242
 
1243
                default:
1244
                        return hdlc_ioctl ( dev, ifr, cmd );
1245
                }
1246
 
1247
        default:
1248
                /* Not one of ours. Pass through to HDLC package */
1249
                return hdlc_ioctl ( dev, ifr, cmd );
1250
        }
1251
}
1252
 
1253
 
1254
static void
1255
fst_openport ( struct fst_port_info *port )
1256
{
1257
        int signals;
1258
 
1259
        /* Only init things if card is actually running. This allows open to
1260
         * succeed for downloads etc.
1261
         */
1262
        if ( port->card->state == FST_RUNNING )
1263
        {
1264
                if ( port->run )
1265
                {
1266
                        dbg ( DBG_OPEN,"open: found port already running\n");
1267
 
1268
                        fst_issue_cmd ( port, STOPPORT );
1269
                        port->run = 0;
1270
                }
1271
 
1272
                fst_rx_config ( port );
1273
                fst_tx_config ( port );
1274
                fst_op_raise ( port, OPSTS_RTS | OPSTS_DTR );
1275
 
1276
                fst_issue_cmd ( port, STARTPORT );
1277
                port->run = 1;
1278
 
1279
                signals = FST_RDL ( port->card, v24DebouncedSts[port->index]);
1280
                if ( signals & (( port->hwif == X21 ) ? IPSTS_INDICATE
1281
                                                      : IPSTS_DCD ))
1282
                        netif_carrier_on ( port_to_dev ( port ));
1283
                else
1284
                        netif_carrier_off ( port_to_dev ( port ));
1285
        }
1286
}
1287
 
1288
static void
1289
fst_closeport ( struct fst_port_info *port )
1290
{
1291
        if ( port->card->state == FST_RUNNING )
1292
        {
1293
                if ( port->run )
1294
                {
1295
                        port->run = 0;
1296
                        fst_op_lower ( port, OPSTS_RTS | OPSTS_DTR );
1297
 
1298
                        fst_issue_cmd ( port, STOPPORT );
1299
                }
1300
                else
1301
                {
1302
                        dbg ( DBG_OPEN,"close: port not running\n");
1303
                }
1304
        }
1305
}
1306
 
1307
 
1308
static int
1309
fst_open ( struct net_device *dev )
1310
{
1311
        int err;
1312
 
1313
        err = hdlc_open ( dev_to_hdlc ( dev ));
1314
        if ( err )
1315
                return err;
1316
 
1317
        MOD_INC_USE_COUNT;
1318
 
1319
        fst_openport ( dev_to_port ( dev ));
1320
        netif_wake_queue ( dev );
1321
        return 0;
1322
}
1323
 
1324
static int
1325
fst_close ( struct net_device *dev )
1326
{
1327
        netif_stop_queue ( dev );
1328
        fst_closeport ( dev_to_port ( dev ));
1329
        hdlc_close ( dev_to_hdlc  ( dev ));
1330
        MOD_DEC_USE_COUNT;
1331
        return 0;
1332
}
1333
 
1334
static int
1335
fst_attach ( hdlc_device *hdlc, unsigned short encoding, unsigned short parity )
1336
{
1337
        /* Setting currently fixed in FarSync card so we check and forget */
1338
        if ( encoding != ENCODING_NRZ || parity != PARITY_CRC16_PR1_CCITT )
1339
                return -EINVAL;
1340
        return 0;
1341
}
1342
 
1343
 
1344
static void
1345
fst_tx_timeout ( struct net_device *dev )
1346
{
1347
        struct fst_port_info *port;
1348
 
1349
        dbg ( DBG_INTR | DBG_TX,"tx_timeout\n");
1350
 
1351
        port = dev_to_port ( dev );
1352
 
1353
        port->hdlc.stats.tx_errors++;
1354
        port->hdlc.stats.tx_aborted_errors++;
1355
 
1356
        if ( port->txcnt > 0 )
1357
                fst_issue_cmd ( port, ABORTTX );
1358
 
1359
        dev->trans_start = jiffies;
1360
        netif_wake_queue ( dev );
1361
}
1362
 
1363
 
1364
static int
1365
fst_start_xmit ( struct sk_buff *skb, struct net_device *dev )
1366
{
1367
        struct fst_card_info *card;
1368
        struct fst_port_info *port;
1369
        unsigned char dmabits;
1370
        unsigned long flags;
1371
        int pi;
1372
        int txp;
1373
 
1374
        port = dev_to_port ( dev );
1375
        card = port->card;
1376
 
1377
        /* Drop packet with error if we don't have carrier */
1378
        if ( ! netif_carrier_ok ( dev ))
1379
        {
1380
                dev_kfree_skb ( skb );
1381
                port->hdlc.stats.tx_errors++;
1382
                port->hdlc.stats.tx_carrier_errors++;
1383
                return 0;
1384
        }
1385
 
1386
        /* Drop it if it's too big! MTU failure ? */
1387
        if ( skb->len > LEN_TX_BUFFER )
1388
        {
1389
                dbg ( DBG_TX,"Packet too large %d vs %d\n", skb->len,
1390
                                                LEN_TX_BUFFER );
1391
                dev_kfree_skb ( skb );
1392
                port->hdlc.stats.tx_errors++;
1393
                return 0;
1394
        }
1395
 
1396
        /* Check we have a buffer */
1397
        pi = port->index;
1398
        spin_lock_irqsave ( &card->card_lock, flags );
1399
        txp = port->txpos;
1400
        dmabits = FST_RDB ( card, txDescrRing[pi][txp].bits );
1401
        if ( dmabits & DMA_OWN )
1402
        {
1403
                spin_unlock_irqrestore ( &card->card_lock, flags );
1404
                dbg ( DBG_TX,"Out of Tx buffers\n");
1405
                dev_kfree_skb ( skb );
1406
                port->hdlc.stats.tx_errors++;
1407
                return 0;
1408
        }
1409
        if ( ++port->txpos >= NUM_TX_BUFFER )
1410
                port->txpos = 0;
1411
 
1412
        if ( ++port->txcnt >= NUM_TX_BUFFER )
1413
                netif_stop_queue ( dev );
1414
 
1415
        /* Release the card lock before we copy the data as we now have
1416
         * exclusive access to the buffer.
1417
         */
1418
        spin_unlock_irqrestore ( &card->card_lock, flags );
1419
 
1420
        /* Enqueue the packet */
1421
        memcpy_toio ( card->mem + BUF_OFFSET ( txBuffer[pi][txp][0]),
1422
                                                skb->data, skb->len );
1423
        FST_WRW ( card, txDescrRing[pi][txp].bcnt, cnv_bcnt ( skb->len ));
1424
        FST_WRB ( card, txDescrRing[pi][txp].bits, DMA_OWN | TX_STP | TX_ENP );
1425
 
1426
        port->hdlc.stats.tx_packets++;
1427
        port->hdlc.stats.tx_bytes += skb->len;
1428
 
1429
        dev_kfree_skb ( skb );
1430
 
1431
        dev->trans_start = jiffies;
1432
        return 0;
1433
}
1434
 
1435
 
1436
/*
1437
 *      Card setup having checked hardware resources.
1438
 *      Should be pretty bizarre if we get an error here (kernel memory
1439
 *      exhaustion is one possibility). If we do see a problem we report it
1440
 *      via a printk and leave the corresponding interface and all that follow
1441
 *      disabled.
1442
 */
1443
static char *type_strings[] __devinitdata = {
1444
        "no hardware",                  /* Should never be seen */
1445
        "FarSync T2P",
1446
        "FarSync T4P"
1447
};
1448
 
1449
static void __devinit
1450
fst_init_card ( struct fst_card_info *card )
1451
{
1452
        int i;
1453
        int err;
1454
        struct net_device *dev;
1455
 
1456
        /* We're working on a number of ports based on the card ID. If the
1457
         * firmware detects something different later (should never happen)
1458
         * we'll have to revise it in some way then.
1459
         */
1460
        for ( i = 0 ; i < card->nports ; i++ )
1461
        {
1462
                card->ports[i].card   = card;
1463
                card->ports[i].index  = i;
1464
                card->ports[i].run    = 0;
1465
 
1466
                dev = hdlc_to_dev ( &card->ports[i].hdlc );
1467
 
1468
                /* Fill in the net device info */
1469
                                /* Since this is a PCI setup this is purely
1470
                                 * informational. Give them the buffer addresses
1471
                                 * and basic card I/O.
1472
                                 */
1473
                dev->mem_start   = card->phys_mem
1474
                                 + BUF_OFFSET ( txBuffer[i][0][0]);
1475
                dev->mem_end     = card->phys_mem
1476
                                 + BUF_OFFSET ( txBuffer[i][NUM_TX_BUFFER][0]);
1477
                dev->rmem_start  = card->phys_mem
1478
                                 + BUF_OFFSET ( rxBuffer[i][0][0]);
1479
                dev->rmem_end    = card->phys_mem
1480
                                 + BUF_OFFSET ( rxBuffer[i][NUM_RX_BUFFER][0]);
1481
                dev->base_addr   = card->pci_conf;
1482
                dev->irq         = card->irq;
1483
 
1484
                dev->tx_queue_len          = FST_TX_QUEUE_LEN;
1485
                dev->open                  = fst_open;
1486
                dev->stop                  = fst_close;
1487
                dev->do_ioctl              = fst_ioctl;
1488
                dev->watchdog_timeo        = FST_TX_TIMEOUT;
1489
                dev->tx_timeout            = fst_tx_timeout;
1490
                card->ports[i].hdlc.attach = fst_attach;
1491
                card->ports[i].hdlc.xmit   = fst_start_xmit;
1492
 
1493
                if (( err = register_hdlc_device ( &card->ports[i].hdlc )) < 0 )
1494
                {
1495
                        printk_err ("Cannot register HDLC device for port %d"
1496
                                    " (errno %d)\n", i, -err );
1497
                        card->nports = i;
1498
                        break;
1499
                }
1500
        }
1501
 
1502
        spin_lock_init ( &card->card_lock );
1503
 
1504
        printk ( KERN_INFO "%s-%s: %s IRQ%d, %d ports\n",
1505
                        hdlc_to_dev(&card->ports[0].hdlc)->name,
1506
                        hdlc_to_dev(&card->ports[card->nports-1].hdlc)->name,
1507
                        type_strings[card->type], card->irq, card->nports );
1508
}
1509
 
1510
 
1511
/*
1512
 *      Initialise card when detected.
1513
 *      Returns 0 to indicate success, or errno otherwise.
1514
 */
1515
static int __devinit
1516
fst_add_one ( struct pci_dev *pdev, const struct pci_device_id *ent )
1517
{
1518
        static int firsttime_done = 0;
1519
        struct fst_card_info *card;
1520
        int err = 0;
1521
 
1522
        if ( ! firsttime_done )
1523
        {
1524
                printk ( KERN_INFO "FarSync X21 driver " FST_USER_VERSION
1525
                                " (c) 2001 FarSite Communications Ltd.\n");
1526
                firsttime_done = 1;
1527
        }
1528
 
1529
        /* Allocate driver private data */
1530
        card = kmalloc ( sizeof ( struct fst_card_info ),  GFP_KERNEL);
1531
        if (card == NULL)
1532
        {
1533
                printk_err ("FarSync card found but insufficient memory for"
1534
                                " driver storage\n");
1535
                return -ENOMEM;
1536
        }
1537
        memset ( card, 0, sizeof ( struct fst_card_info ));
1538
 
1539
        /* Record info we need*/
1540
        card->irq         = pdev->irq;
1541
        card->pci_conf    = pci_resource_start ( pdev, 1 );
1542
        card->phys_mem    = pci_resource_start ( pdev, 2 );
1543
        card->phys_ctlmem = pci_resource_start ( pdev, 3 );
1544
 
1545
        card->type        = ent->driver_data;
1546
        card->nports      = ( ent->driver_data == FST_TYPE_T2P ) ? 2 : 4;
1547
 
1548
        card->state       = FST_UNINIT;
1549
 
1550
        dbg ( DBG_PCI,"type %d nports %d irq %d\n", card->type,
1551
                        card->nports, card->irq );
1552
        dbg ( DBG_PCI,"conf %04x mem %08x ctlmem %08x\n",
1553
                        card->pci_conf, card->phys_mem, card->phys_ctlmem );
1554
 
1555
        /* Check we can get access to the memory and I/O regions */
1556
        if ( ! request_region ( card->pci_conf, 0x80,"PLX config regs"))
1557
        {
1558
                printk_err ("Unable to get config I/O @ 0x%04X\n",
1559
                                                card->pci_conf );
1560
                err = -ENODEV;
1561
                goto error_free_card;
1562
        }
1563
        if ( ! request_mem_region ( card->phys_mem, FST_MEMSIZE,"Shared RAM"))
1564
        {
1565
                printk_err ("Unable to get main memory @ 0x%08X\n",
1566
                                                card->phys_mem );
1567
                err = -ENODEV;
1568
                goto error_release_io;
1569
        }
1570
        if ( ! request_mem_region ( card->phys_ctlmem, 0x10,"Control memory"))
1571
        {
1572
                printk_err ("Unable to get control memory @ 0x%08X\n",
1573
                                                card->phys_ctlmem );
1574
                err = -ENODEV;
1575
                goto error_release_mem;
1576
        }
1577
 
1578
        /* Try to enable the device */
1579
        if (( err = pci_enable_device ( pdev )) != 0 )
1580
        {
1581
                printk_err ("Failed to enable card. Err %d\n", -err );
1582
                goto error_release_ctlmem;
1583
        }
1584
 
1585
        /* Get virtual addresses of memory regions */
1586
        if (( card->mem = ioremap ( card->phys_mem, FST_MEMSIZE )) == NULL )
1587
        {
1588
                printk_err ("Physical memory remap failed\n");
1589
                err = -ENODEV;
1590
                goto error_release_ctlmem;
1591
        }
1592
        if (( card->ctlmem = ioremap ( card->phys_ctlmem, 0x10 )) == NULL )
1593
        {
1594
                printk_err ("Control memory remap failed\n");
1595
                err = -ENODEV;
1596
                goto error_unmap_mem;
1597
        }
1598
        dbg ( DBG_PCI,"kernel mem %p, ctlmem %p\n", card->mem, card->ctlmem);
1599
 
1600
        /* Reset the card's processor */
1601
        fst_cpureset ( card );
1602
        card->state = FST_RESET;
1603
 
1604
        /* Register the interrupt handler */
1605
        if ( request_irq ( card->irq, fst_intr, SA_SHIRQ, FST_DEV_NAME, card ))
1606
        {
1607
 
1608
                printk_err ("Unable to register interrupt %d\n", card->irq );
1609
                err = -ENODEV;
1610
                goto error_unmap_ctlmem;
1611
        }
1612
 
1613
        /* Record driver data for later use */
1614
        pci_set_drvdata(pdev, card);
1615
 
1616
        /* Remainder of card setup */
1617
        fst_init_card ( card );
1618
 
1619
        return 0;                       /* Success */
1620
 
1621
 
1622
                                        /* Failure. Release resources */
1623
error_unmap_ctlmem:
1624
        iounmap ( card->ctlmem );
1625
 
1626
error_unmap_mem:
1627
        iounmap ( card->mem );
1628
 
1629
error_release_ctlmem:
1630
        release_mem_region ( card->phys_ctlmem, 0x10 );
1631
 
1632
error_release_mem:
1633
        release_mem_region ( card->phys_mem, FST_MEMSIZE );
1634
 
1635
error_release_io:
1636
        release_region ( card->pci_conf, 0x80 );
1637
 
1638
error_free_card:
1639
        kfree ( card );
1640
        return err;
1641
}
1642
 
1643
 
1644
/*
1645
 *      Cleanup and close down a card
1646
 */
1647
static void __devexit
1648
fst_remove_one ( struct pci_dev *pdev )
1649
{
1650
        struct fst_card_info *card;
1651
        int i;
1652
 
1653
        card = pci_get_drvdata(pdev);
1654
 
1655
        for ( i = 0 ; i < card->nports ; i++ )
1656
        {
1657
                unregister_hdlc_device ( &card->ports[i].hdlc );
1658
        }
1659
 
1660
        fst_disable_intr ( card );
1661
        free_irq ( card->irq, card );
1662
 
1663
        iounmap ( card->ctlmem );
1664
        iounmap ( card->mem );
1665
 
1666
        release_mem_region ( card->phys_ctlmem, 0x10 );
1667
        release_mem_region ( card->phys_mem, FST_MEMSIZE );
1668
        release_region ( card->pci_conf, 0x80 );
1669
 
1670
        kfree ( card );
1671
}
1672
 
1673
static struct pci_driver fst_driver = {
1674
        name:           FST_NAME,
1675
        id_table:       fst_pci_dev_id,
1676
        probe:          fst_add_one,
1677
        remove:         __devexit_p(fst_remove_one),
1678
        suspend:        NULL,
1679
        resume:         NULL,
1680
};
1681
 
1682
static int __init
1683
fst_init(void)
1684
{
1685
        return pci_module_init ( &fst_driver );
1686
}
1687
 
1688
static void __exit
1689
fst_cleanup_module(void)
1690
{
1691
        pci_unregister_driver ( &fst_driver );
1692
}
1693
 
1694
module_init ( fst_init );
1695
module_exit ( fst_cleanup_module );
1696
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.