ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / wan / dscc4.c
1 /*
2  * drivers/net/wan/dscc4/dscc4.c: a DSCC4 HDLC driver for Linux
3  *
4  * This software may be used and distributed according to the terms of the
5  * GNU General Public License.
6  *
7  * The author may be reached as romieu@cogenit.fr.
8  * Specific bug reports/asian food will be welcome.
9  *
10  * Special thanks to the nice people at CS-Telecom for the hardware and the
11  * access to the test/measure tools.
12  *
13  *
14  *                             Theory of Operation
15  *
16  * I. Board Compatibility
17  *
18  * This device driver is designed for the Siemens PEB20534 4 ports serial
19  * controller as found on Etinc PCISYNC cards. The documentation for the
20  * chipset is available at http://www.infineon.com:
21  * - Data Sheet "DSCC4, DMA Supported Serial Communication Controller with
22  * 4 Channels, PEB 20534 Version 2.1, PEF 20534 Version 2.1";
23  * - Application Hint "Management of DSCC4 on-chip FIFO resources".
24  * - Errata sheet DS5 (courtesy of Michael Skerritt).
25  * Jens David has built an adapter based on the same chipset. Take a look
26  * at http://www.afthd.tu-darmstadt.de/~dg1kjd/pciscc4 for a specific
27  * driver.
28  * Sample code (2 revisions) is available at Infineon.
29  *
30  * II. Board-specific settings
31  *
32  * Pcisync can transmit some clock signal to the outside world on the
33  * *first two* ports provided you put a quartz and a line driver on it and
34  * remove the jumpers. The operation is described on Etinc web site. If you
35  * go DCE on these ports, don't forget to use an adequate cable.
36  *
37  * Sharing of the PCI interrupt line for this board is possible.
38  *
39  * III. Driver operation
40  *
41  * The rx/tx operations are based on a linked list of descriptors. The driver
42  * doesn't use HOLD mode any more. HOLD mode is definitely buggy and the more
43  * I tried to fix it, the more it started to look like (convoluted) software
44  * mutation of LxDA method. Errata sheet DS5 suggests to use LxDA: consider
45  * this a rfc2119 MUST.
46  *
47  * Tx direction
48  * When the tx ring is full, the xmit routine issues a call to netdev_stop.
49  * The device is supposed to be enabled again during an ALLS irq (we could
50  * use HI but as it's easy to lose events, it's fscked).
51  *
52  * Rx direction
53  * The received frames aren't supposed to span over multiple receiving areas.
54  * I may implement it some day but it isn't the highest ranked item.
55  *
56  * IV. Notes
57  * The current error (XDU, RFO) recovery code is untested.
58  * So far, RDO takes his RX channel down and the right sequence to enable it
59  * again is still a mistery. If RDO happens, plan a reboot. More details
60  * in the code (NB: as this happens, TX still works).
61  * Don't mess the cables during operation, especially on DTE ports. I don't
62  * suggest it for DCE either but at least one can get some messages instead
63  * of a complete instant freeze.
64  * Tests are done on Rev. 20 of the silicium. The RDO handling changes with
65  * the documentation/chipset releases.
66  *
67  * TODO:
68  * - test X25.
69  * - use polling at high irq/s,
70  * - performance analysis,
71  * - endianness.
72  *
73  * 2001/12/10   Daniela Squassoni  <daniela@cyclades.com>
74  * - Contribution to support the new generic HDLC layer.
75  *
76  * 2002/01      Ueimor
77  * - old style interface removal
78  * - dscc4_release_ring fix (related to DMA mapping)
79  * - hard_start_xmit fix (hint: TxSizeMax)
80  * - misc crapectomy.
81  */
82
83 #include <linux/module.h>
84 #include <linux/types.h>
85 #include <linux/errno.h>
86 #include <linux/list.h>
87 #include <linux/ioport.h>
88 #include <linux/pci.h>
89 #include <linux/kernel.h>
90 #include <linux/mm.h>
91
92 #include <asm/system.h>
93 #include <asm/cache.h>
94 #include <asm/byteorder.h>
95 #include <asm/uaccess.h>
96 #include <asm/io.h>
97 #include <asm/irq.h>
98
99 #include <linux/init.h>
100 #include <linux/string.h>
101
102 #include <linux/if_arp.h>
103 #include <linux/netdevice.h>
104 #include <linux/skbuff.h>
105 #include <linux/delay.h>
106 #include <net/syncppp.h>
107 #include <linux/hdlc.h>
108
109 /* Version */
110 static const char version[] = "$Id: dscc4.c,v 1.173 2003/09/20 23:55:34 romieu Exp $ for Linux\n";
111 static int debug;
112 static int quartz;
113
114 #ifdef CONFIG_DSCC4_PCI_RST
115 static DECLARE_MUTEX(dscc4_sem);
116 static u32 dscc4_pci_config_store[16];
117 #endif
118
119 #define DRV_NAME        "dscc4"
120
121 #undef DSCC4_POLLING
122
123 /* Module parameters */
124
125 MODULE_AUTHOR("Maintainer: Francois Romieu <romieu@cogenit.fr>");
126 MODULE_DESCRIPTION("Siemens PEB20534 PCI Controler");
127 MODULE_LICENSE("GPL");
128 MODULE_PARM(debug,"i");
129 MODULE_PARM_DESC(debug,"Enable/disable extra messages");
130 MODULE_PARM(quartz,"i");
131 MODULE_PARM_DESC(quartz,"If present, on-board quartz frequency (Hz)");
132
133 /* Structures */
134
135 struct thingie {
136         int define;
137         u32 bits;
138 };
139
140 struct TxFD {
141         u32 state;
142         u32 next;
143         u32 data;
144         u32 complete;
145         u32 jiffies; /* Allows sizeof(TxFD) == sizeof(RxFD) + extra hack */
146 };
147
148 struct RxFD {
149         u32 state1;
150         u32 next;
151         u32 data;
152         u32 state2;
153         u32 end;
154 };
155
156 #define DUMMY_SKB_SIZE          64
157 #define TX_LOW                  8
158 #define TX_RING_SIZE            32
159 #define RX_RING_SIZE            32
160 #define TX_TOTAL_SIZE           TX_RING_SIZE*sizeof(struct TxFD)
161 #define RX_TOTAL_SIZE           RX_RING_SIZE*sizeof(struct RxFD)
162 #define IRQ_RING_SIZE           64              /* Keep it a multiple of 32 */
163 #define TX_TIMEOUT              (HZ/10)
164 #define DSCC4_HZ_MAX            33000000
165 #define BRR_DIVIDER_MAX         64*0x00004000   /* Cf errata DS5 p.10 */
166 #define dev_per_card            4
167 #define SCC_REGISTERS_MAX       23              /* Cf errata DS5 p.4 */
168
169 #define SOURCE_ID(flags)        (((flags) >> 28) & 0x03)
170 #define TO_SIZE(state)          (((state) >> 16) & 0x1fff)
171
172 /*
173  * Given the operating range of Linux HDLC, the 2 defines below could be
174  * made simpler. However they are a fine reminder for the limitations of
175  * the driver: it's better to stay < TxSizeMax and < RxSizeMax.
176  */
177 #define TO_STATE_TX(len)        cpu_to_le32(((len) & TxSizeMax) << 16)
178 #define TO_STATE_RX(len)        cpu_to_le32((RX_MAX(len) % RxSizeMax) << 16)
179 #define RX_MAX(len)             ((((len) >> 5) + 1) << 5)       /* Cf RLCR */
180 #define SCC_REG_START(dpriv)    (SCC_START+(dpriv->dev_id)*SCC_OFFSET)
181
182 struct dscc4_pci_priv {
183         u32 *iqcfg;
184         int cfg_cur;
185         spinlock_t lock;
186         struct pci_dev *pdev;
187
188         struct dscc4_dev_priv *root;
189         dma_addr_t iqcfg_dma;
190         u32 xtal_hz;
191 };
192
193 struct dscc4_dev_priv {
194         struct sk_buff *rx_skbuff[RX_RING_SIZE];
195         struct sk_buff *tx_skbuff[TX_RING_SIZE];
196
197         struct RxFD *rx_fd;
198         struct TxFD *tx_fd;
199         u32 *iqrx;
200         u32 *iqtx;
201
202         /* FIXME: check all the volatile are required */
203         volatile u32 tx_current;
204         u32 rx_current;
205         u32 iqtx_current;
206         u32 iqrx_current;
207
208         volatile u32 tx_dirty;
209         volatile u32 ltda;
210         u32 rx_dirty;
211         u32 lrda;
212
213         dma_addr_t tx_fd_dma;
214         dma_addr_t rx_fd_dma;
215         dma_addr_t iqtx_dma;
216         dma_addr_t iqrx_dma;
217
218         u32 scc_regs[SCC_REGISTERS_MAX]; /* Cf errata DS5 p.4 */
219
220         struct timer_list timer;
221
222         struct dscc4_pci_priv *pci_priv;
223         spinlock_t lock;
224
225         int dev_id;
226         volatile u32 flags;
227         u32 timer_help;
228
229         unsigned short encoding;
230         unsigned short parity;
231         struct net_device *dev;
232         sync_serial_settings settings;
233         u32 __pad __attribute__ ((aligned (4)));
234 };
235
236 /* GLOBAL registers definitions */
237 #define GCMDR   0x00
238 #define GSTAR   0x04
239 #define GMODE   0x08
240 #define IQLENR0 0x0C
241 #define IQLENR1 0x10
242 #define IQRX0   0x14
243 #define IQTX0   0x24
244 #define IQCFG   0x3c
245 #define FIFOCR1 0x44
246 #define FIFOCR2 0x48
247 #define FIFOCR3 0x4c
248 #define FIFOCR4 0x34
249 #define CH0CFG  0x50
250 #define CH0BRDA 0x54
251 #define CH0BTDA 0x58
252 #define CH0FRDA 0x98
253 #define CH0FTDA 0xb0
254 #define CH0LRDA 0xc8
255 #define CH0LTDA 0xe0
256
257 /* SCC registers definitions */
258 #define SCC_START       0x0100
259 #define SCC_OFFSET      0x80
260 #define CMDR    0x00
261 #define STAR    0x04
262 #define CCR0    0x08
263 #define CCR1    0x0c
264 #define CCR2    0x10
265 #define BRR     0x2C
266 #define RLCR    0x40
267 #define IMR     0x54
268 #define ISR     0x58
269
270 #define GPDIR   0x0400
271 #define GPDATA  0x0404
272 #define GPIM    0x0408
273
274 /* Bit masks */
275 #define EncodingMask    0x00700000
276 #define CrcMask         0x00000003
277
278 #define IntRxScc0       0x10000000
279 #define IntTxScc0       0x01000000
280
281 #define TxPollCmd       0x00000400
282 #define RxActivate      0x08000000
283 #define MTFi            0x04000000
284 #define Rdr             0x00400000
285 #define Rdt             0x00200000
286 #define Idr             0x00100000
287 #define Idt             0x00080000
288 #define TxSccRes        0x01000000
289 #define RxSccRes        0x00010000
290 #define TxSizeMax       0x1fff          /* Datasheet DS1 - 11.1.1.1 */
291 #define RxSizeMax       0x1ffc          /* Datasheet DS1 - 11.1.2.1 */
292
293 #define Ccr0ClockMask   0x0000003f
294 #define Ccr1LoopMask    0x00000200
295 #define IsrMask         0x000fffff
296 #define BrrExpMask      0x00000f00
297 #define BrrMultMask     0x0000003f
298 #define EncodingMask    0x00700000
299 #define Hold            0x40000000
300 #define SccBusy         0x10000000
301 #define PowerUp         0x80000000
302 #define Vis             0x00001000
303 #define FrameOk         (FrameVfr | FrameCrc)
304 #define FrameVfr        0x80
305 #define FrameRdo        0x40
306 #define FrameCrc        0x20
307 #define FrameRab        0x10
308 #define FrameAborted    0x00000200
309 #define FrameEnd        0x80000000
310 #define DataComplete    0x40000000
311 #define LengthCheck     0x00008000
312 #define SccEvt          0x02000000
313 #define NoAck           0x00000200
314 #define Action          0x00000001
315 #define HiDesc          0x20000000
316
317 /* SCC events */
318 #define RxEvt           0xf0000000
319 #define TxEvt           0x0f000000
320 #define Alls            0x00040000
321 #define Xdu             0x00010000
322 #define Cts             0x00004000
323 #define Xmr             0x00002000
324 #define Xpr             0x00001000
325 #define Rdo             0x00000080
326 #define Rfs             0x00000040
327 #define Cd              0x00000004
328 #define Rfo             0x00000002
329 #define Flex            0x00000001
330
331 /* DMA core events */
332 #define Cfg             0x00200000
333 #define Hi              0x00040000
334 #define Fi              0x00020000
335 #define Err             0x00010000
336 #define Arf             0x00000002
337 #define ArAck           0x00000001
338
339 /* State flags */
340 #define Ready           0x00000000
341 #define NeedIDR         0x00000001
342 #define NeedIDT         0x00000002
343 #define RdoSet          0x00000004
344 #define FakeReset       0x00000008
345
346 /* Don't mask RDO. Ever. */
347 #ifdef DSCC4_POLLING
348 #define EventsMask      0xfffeef7f
349 #else
350 #define EventsMask      0xfffa8f7a
351 #endif
352
353 /* Functions prototypes */
354 static inline void dscc4_rx_irq(struct dscc4_pci_priv *, struct dscc4_dev_priv *);
355 static inline void dscc4_tx_irq(struct dscc4_pci_priv *, struct dscc4_dev_priv *);
356 static int dscc4_found1(struct pci_dev *, unsigned long ioaddr);
357 static int dscc4_init_one(struct pci_dev *, const struct pci_device_id *ent);
358 static int dscc4_open(struct net_device *);
359 static int dscc4_start_xmit(struct sk_buff *, struct net_device *);
360 static int dscc4_close(struct net_device *);
361 static int dscc4_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
362 static int dscc4_init_ring(struct net_device *);
363 static void dscc4_release_ring(struct dscc4_dev_priv *);
364 static void dscc4_timer(unsigned long);
365 static void dscc4_tx_timeout(struct net_device *);
366 static irqreturn_t dscc4_irq(int irq, void *dev_id, struct pt_regs *ptregs);
367 static int dscc4_hdlc_attach(struct net_device *, unsigned short, unsigned short);
368 static int dscc4_set_iface(struct dscc4_dev_priv *, struct net_device *);
369 static inline int dscc4_set_quartz(struct dscc4_dev_priv *, int);
370 #ifdef DSCC4_POLLING
371 static int dscc4_tx_poll(struct dscc4_dev_priv *, struct net_device *);
372 #endif
373
374 static inline struct dscc4_dev_priv *dscc4_priv(struct net_device *dev)
375 {
376         return dev_to_hdlc(dev)->priv;
377 }
378
379 static inline struct net_device *dscc4_to_dev(struct dscc4_dev_priv *p)
380 {
381         return p->dev;
382 }
383
384 static void scc_patchl(u32 mask, u32 value, struct dscc4_dev_priv *dpriv,
385                         struct net_device *dev, int offset)
386 {
387         u32 state;
388
389         /* Cf scc_writel for concern regarding thread-safety */
390         state = dpriv->scc_regs[offset >> 2];
391         state &= ~mask;
392         state |= value;
393         dpriv->scc_regs[offset >> 2] = state;
394         writel(state, dev->base_addr + SCC_REG_START(dpriv) + offset);
395 }
396
397 static void scc_writel(u32 bits, struct dscc4_dev_priv *dpriv,
398                        struct net_device *dev, int offset)
399 {
400         /*
401          * Thread-UNsafe.
402          * As of 2002/02/16, there are no thread racing for access.
403          */
404         dpriv->scc_regs[offset >> 2] = bits;
405         writel(bits, dev->base_addr + SCC_REG_START(dpriv) + offset);
406 }
407
408 static inline u32 scc_readl(struct dscc4_dev_priv *dpriv, int offset)
409 {
410         return dpriv->scc_regs[offset >> 2];
411 }
412
413 static u32 scc_readl_star(struct dscc4_dev_priv *dpriv, struct net_device *dev)
414 {
415         /* Cf errata DS5 p.4 */
416         readl(dev->base_addr + SCC_REG_START(dpriv) + STAR);
417         return readl(dev->base_addr + SCC_REG_START(dpriv) + STAR);
418 }
419
420 static inline void dscc4_do_tx(struct dscc4_dev_priv *dpriv,
421                                struct net_device *dev)
422 {
423         dpriv->ltda = dpriv->tx_fd_dma +
424                       ((dpriv->tx_current-1)%TX_RING_SIZE)*sizeof(struct TxFD);
425         writel(dpriv->ltda, dev->base_addr + CH0LTDA + dpriv->dev_id*4);
426         /* Flush posted writes *NOW* */
427         readl(dev->base_addr + CH0LTDA + dpriv->dev_id*4);
428 }
429
430 static inline void dscc4_rx_update(struct dscc4_dev_priv *dpriv,
431                                    struct net_device *dev)
432 {
433         dpriv->lrda = dpriv->rx_fd_dma +
434                       ((dpriv->rx_dirty - 1)%RX_RING_SIZE)*sizeof(struct RxFD);
435         writel(dpriv->lrda, dev->base_addr + CH0LRDA + dpriv->dev_id*4);
436 }
437
438 static inline unsigned int dscc4_tx_done(struct dscc4_dev_priv *dpriv)
439 {
440         return dpriv->tx_current == dpriv->tx_dirty;
441 }
442
443 static inline unsigned int dscc4_tx_quiescent(struct dscc4_dev_priv *dpriv,
444                                               struct net_device *dev)
445 {
446         return readl(dev->base_addr + CH0FTDA + dpriv->dev_id*4) == dpriv->ltda;
447 }
448
449 int state_check(u32 state, struct dscc4_dev_priv *dpriv, struct net_device *dev,
450                 const char *msg)
451 {
452         int ret = 0;
453
454         if (debug > 1) {
455         if (SOURCE_ID(state) != dpriv->dev_id) {
456                 printk(KERN_DEBUG "%s (%s): Source Id=%d, state=%08x\n",
457                        dev->name, msg, SOURCE_ID(state), state );
458                         ret = -1;
459         }
460         if (state & 0x0df80c00) {
461                 printk(KERN_DEBUG "%s (%s): state=%08x (UFO alert)\n",
462                        dev->name, msg, state);
463                         ret = -1;
464         }
465         }
466         return ret;
467 }
468
469 void dscc4_tx_print(struct net_device *dev, struct dscc4_dev_priv *dpriv,
470                     char *msg)
471 {
472         printk(KERN_DEBUG "%s: tx_current=%02d tx_dirty=%02d (%s)\n",
473                dev->name, dpriv->tx_current, dpriv->tx_dirty, msg);
474 }
475
476 static void dscc4_release_ring(struct dscc4_dev_priv *dpriv)
477 {
478         struct pci_dev *pdev = dpriv->pci_priv->pdev;
479         struct TxFD *tx_fd = dpriv->tx_fd;
480         struct RxFD *rx_fd = dpriv->rx_fd;
481         struct sk_buff **skbuff;
482         int i;
483
484         pci_free_consistent(pdev, TX_TOTAL_SIZE, tx_fd, dpriv->tx_fd_dma);
485         pci_free_consistent(pdev, RX_TOTAL_SIZE, rx_fd, dpriv->rx_fd_dma);
486
487         skbuff = dpriv->tx_skbuff;
488         for (i = 0; i < TX_RING_SIZE; i++) {
489                 if (*skbuff) {
490                         pci_unmap_single(pdev, tx_fd->data, (*skbuff)->len,
491                                 PCI_DMA_TODEVICE);
492                         dev_kfree_skb(*skbuff);
493                 }
494                 skbuff++;
495                 tx_fd++;
496         }
497
498         skbuff = dpriv->rx_skbuff;
499         for (i = 0; i < RX_RING_SIZE; i++) {
500                 if (*skbuff) {
501                         pci_unmap_single(pdev, rx_fd->data,
502                                 RX_MAX(HDLC_MAX_MRU), PCI_DMA_FROMDEVICE);
503                         dev_kfree_skb(*skbuff);
504                 }
505                 skbuff++;
506                 rx_fd++;
507         }
508 }
509
510 inline int try_get_rx_skb(struct dscc4_dev_priv *dpriv, struct net_device *dev)
511 {
512         unsigned int dirty = dpriv->rx_dirty%RX_RING_SIZE;
513         struct RxFD *rx_fd = dpriv->rx_fd + dirty;
514         const int len = RX_MAX(HDLC_MAX_MRU);
515         struct sk_buff *skb;
516         int ret = 0;
517
518         skb = dev_alloc_skb(len);
519         dpriv->rx_skbuff[dirty] = skb;
520         if (skb) {
521                 skb->dev = dev;
522                 skb->protocol = hdlc_type_trans(skb, dev);
523                 skb->mac.raw = skb->data;
524                 rx_fd->data = pci_map_single(dpriv->pci_priv->pdev, skb->data,
525                                              len, PCI_DMA_FROMDEVICE);
526         } else {
527                 rx_fd->data = (u32) NULL;
528                 ret = -1;
529         }
530         return ret;
531 }
532
533 /*
534  * IRQ/thread/whatever safe
535  */
536 static int dscc4_wait_ack_cec(struct dscc4_dev_priv *dpriv,
537                               struct net_device *dev, char *msg)
538 {
539         s8 i = 0;
540
541         do {
542                 if (!(scc_readl_star(dpriv, dev) & SccBusy)) {
543                         printk(KERN_DEBUG "%s: %s ack (%d try)\n", dev->name,
544                                msg, i);
545                         goto done;
546                 }
547                 set_current_state(TASK_UNINTERRUPTIBLE);
548                 schedule_timeout(10);
549                 rmb();
550         } while (++i > 0);
551         printk(KERN_ERR "%s: %s timeout\n", dev->name, msg);
552 done:
553         return (i >= 0) ? i : -EAGAIN;
554 }
555
556 static int dscc4_do_action(struct net_device *dev, char *msg)
557 {
558         unsigned long ioaddr = dev->base_addr;
559         s16 i = 0;
560
561         writel(Action, ioaddr + GCMDR);
562         ioaddr += GSTAR;
563         do {
564                 u32 state = readl(ioaddr);
565
566                 if (state & ArAck) {
567                         printk(KERN_DEBUG "%s: %s ack\n", dev->name, msg);
568                         writel(ArAck, ioaddr);
569                         goto done;
570                 } else if (state & Arf) {
571                         printk(KERN_ERR "%s: %s failed\n", dev->name, msg);
572                         writel(Arf, ioaddr);
573                         i = -1;
574                         goto done;
575         }
576                 rmb();
577         } while (++i > 0);
578         printk(KERN_ERR "%s: %s timeout\n", dev->name, msg);
579 done:
580         return i;
581 }
582
583 static inline int dscc4_xpr_ack(struct dscc4_dev_priv *dpriv)
584 {
585         int cur = dpriv->iqtx_current%IRQ_RING_SIZE;
586         s8 i = 0;
587
588         do {
589                 if (!(dpriv->flags & (NeedIDR | NeedIDT)) ||
590                     (dpriv->iqtx[cur] & Xpr))
591                         break;
592                 smp_rmb();
593                 set_current_state(TASK_UNINTERRUPTIBLE);
594                 schedule_timeout(10);
595         } while (++i > 0);
596
597         return (i >= 0 ) ? i : -EAGAIN;
598 }
599
600 #if 0 /* dscc4_{rx/tx}_reset are both unreliable - more tweak needed */
601 static void dscc4_rx_reset(struct dscc4_dev_priv *dpriv, struct net_device *dev)
602 {
603         unsigned long flags;
604
605         spin_lock_irqsave(&dpriv->pci_priv->lock, flags);
606         /* Cf errata DS5 p.6 */
607         writel(0x00000000, dev->base_addr + CH0LRDA + dpriv->dev_id*4);
608         scc_patchl(PowerUp, 0, dpriv, dev, CCR0);
609         readl(dev->base_addr + CH0LRDA + dpriv->dev_id*4);
610         writel(MTFi|Rdr, dev->base_addr + dpriv->dev_id*0x0c + CH0CFG);
611         writel(Action, dev->base_addr + GCMDR);
612         spin_unlock_irqrestore(&dpriv->pci_priv->lock, flags);
613 }
614
615 #endif
616
617 #if 0
618 static void dscc4_tx_reset(struct dscc4_dev_priv *dpriv, struct net_device *dev)
619 {
620         u16 i = 0;
621
622         /* Cf errata DS5 p.7 */
623         scc_patchl(PowerUp, 0, dpriv, dev, CCR0);
624         scc_writel(0x00050000, dpriv, dev, CCR2);
625         /*
626          * Must be longer than the time required to fill the fifo.
627          */
628         while (!dscc4_tx_quiescent(dpriv, dev) && ++i) {
629                 udelay(1);
630                 wmb();
631         }
632
633         writel(MTFi|Rdt, dev->base_addr + dpriv->dev_id*0x0c + CH0CFG);
634         if (dscc4_do_action(dev, "Rdt") < 0)
635                 printk(KERN_ERR "%s: Tx reset failed\n", dev->name);
636 }
637 #endif
638
639 /* TODO: (ab)use this function to refill a completely depleted RX ring. */
640 static inline void dscc4_rx_skb(struct dscc4_dev_priv *dpriv,
641                                 struct net_device *dev)
642 {
643         struct RxFD *rx_fd = dpriv->rx_fd + dpriv->rx_current%RX_RING_SIZE;
644         struct net_device_stats *stats = hdlc_stats(dev);
645         struct pci_dev *pdev = dpriv->pci_priv->pdev;
646         struct sk_buff *skb;
647         int pkt_len;
648
649         skb = dpriv->rx_skbuff[dpriv->rx_current++%RX_RING_SIZE];
650         if (!skb) {
651                 printk(KERN_DEBUG "%s: skb=0 (%s)\n", dev->name, __FUNCTION__);
652                 goto refill;
653         }
654         pkt_len = TO_SIZE(rx_fd->state2);
655         pci_unmap_single(pdev, rx_fd->data, RX_MAX(HDLC_MAX_MRU), PCI_DMA_FROMDEVICE);
656         if ((skb->data[--pkt_len] & FrameOk) == FrameOk) {
657                 stats->rx_packets++;
658                 stats->rx_bytes += pkt_len;
659                 skb_put(skb, pkt_len);
660                 if (netif_running(dev))
661                         skb->protocol = hdlc_type_trans(skb, dev);
662                 skb->dev->last_rx = jiffies;
663                 netif_rx(skb);
664         } else {
665                 if (skb->data[pkt_len] & FrameRdo)
666                         stats->rx_fifo_errors++;
667                 else if (!(skb->data[pkt_len] | ~FrameCrc))
668                         stats->rx_crc_errors++;
669                 else if (!(skb->data[pkt_len] | ~(FrameVfr | FrameRab)))
670                         stats->rx_length_errors++;
671                 else
672                         stats->rx_errors++;
673                 dev_kfree_skb_irq(skb);
674         }
675 refill:
676         while ((dpriv->rx_dirty - dpriv->rx_current) % RX_RING_SIZE) {
677                 if (try_get_rx_skb(dpriv, dev) < 0)
678                         break;
679                 dpriv->rx_dirty++;
680         }
681         dscc4_rx_update(dpriv, dev);
682         rx_fd->state2 = 0x00000000;
683         rx_fd->end = 0xbabeface;
684 }
685
686 static void dscc4_free1(struct pci_dev *pdev)
687 {
688         struct dscc4_pci_priv *ppriv;
689         struct dscc4_dev_priv *root;
690         int i;
691
692         ppriv = pci_get_drvdata(pdev);
693         root = ppriv->root;
694
695         for (i = 0; i < dev_per_card; i++)
696                 unregister_hdlc_device(dscc4_to_dev(&root[i]));
697
698         pci_set_drvdata(pdev, NULL);
699
700         for (i = 0; i < dev_per_card; i++)
701                 free_netdev(root[i].dev);
702         kfree(root);
703         kfree(ppriv);
704 }
705
706 static int __devinit dscc4_init_one(struct pci_dev *pdev,
707                                   const struct pci_device_id *ent)
708 {
709         struct dscc4_pci_priv *priv;
710         struct dscc4_dev_priv *dpriv;
711         static int cards_found = 0;
712         unsigned long ioaddr;
713         int i;
714
715         printk(KERN_DEBUG "%s", version);
716
717         if (pci_enable_device(pdev))
718                 goto err_out;
719         if (!request_mem_region(pci_resource_start(pdev, 0),
720                                 pci_resource_len(pdev, 0), "registers")) {
721                 printk(KERN_ERR "%s: can't reserve MMIO region (regs)\n",
722                         DRV_NAME);
723                 goto err_out;
724         }
725         if (!request_mem_region(pci_resource_start(pdev, 1),
726                                 pci_resource_len(pdev, 1), "LBI interface")) {
727                 printk(KERN_ERR "%s: can't reserve MMIO region (lbi)\n",
728                         DRV_NAME);
729                 goto err_out_free_mmio_region0;
730         }
731         ioaddr = (unsigned long)ioremap(pci_resource_start(pdev, 0),
732                                         pci_resource_len(pdev, 0));
733         if (!ioaddr) {
734                 printk(KERN_ERR "%s: cannot remap MMIO region %lx @ %lx\n",
735                         DRV_NAME, pci_resource_len(pdev, 0),
736                         pci_resource_start(pdev, 0));
737                 goto err_out_free_mmio_region;
738         }
739         printk(KERN_DEBUG "Siemens DSCC4, MMIO at %#lx (regs), %#lx (lbi), IRQ %d\n",
740                 pci_resource_start(pdev, 0),
741                 pci_resource_start(pdev, 1), pdev->irq);
742
743         /* Cf errata DS5 p.2 */
744         pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xf8);
745         pci_set_master(pdev);
746
747         if (dscc4_found1(pdev, ioaddr))
748                 goto err_out_iounmap;
749
750         priv = (struct dscc4_pci_priv *)pci_get_drvdata(pdev);
751
752         if (request_irq(pdev->irq, &dscc4_irq, SA_SHIRQ, DRV_NAME, priv->root)){
753                 printk(KERN_WARNING "%s: IRQ %d busy\n", DRV_NAME, pdev->irq);
754                 goto err_out_free1;
755         }
756
757         /* power up/little endian/dma core controlled via lrda/ltda */
758         writel(0x00000001, ioaddr + GMODE);
759         /* Shared interrupt queue */
760         {
761                 u32 bits;
762
763                 bits = (IRQ_RING_SIZE >> 5) - 1;
764                 bits |= bits << 4;
765                 bits |= bits << 8;
766                 bits |= bits << 16;
767                 writel(bits, ioaddr + IQLENR0);
768         }
769         /* Global interrupt queue */
770         writel((u32)(((IRQ_RING_SIZE >> 5) - 1) << 20), ioaddr + IQLENR1);
771         priv->iqcfg = (u32 *) pci_alloc_consistent(pdev,
772                 IRQ_RING_SIZE*sizeof(u32), &priv->iqcfg_dma);
773         if (!priv->iqcfg)
774                 goto err_out_free_irq;
775         writel(priv->iqcfg_dma, ioaddr + IQCFG);
776
777         /*
778          * SCC 0-3 private rx/tx irq structures
779          * IQRX/TXi needs to be set soon. Learned it the hard way...
780          */
781         for (i = 0; i < dev_per_card; i++) {
782                 dpriv = priv->root + i;
783                 dpriv->iqtx = (u32 *) pci_alloc_consistent(pdev,
784                         IRQ_RING_SIZE*sizeof(u32), &dpriv->iqtx_dma);
785                 if (!dpriv->iqtx)
786                         goto err_out_free_iqtx;
787                 writel(dpriv->iqtx_dma, ioaddr + IQTX0 + i*4);
788         }
789         for (i = 0; i < dev_per_card; i++) {
790                 dpriv = priv->root + i;
791                 dpriv->iqrx = (u32 *) pci_alloc_consistent(pdev,
792                         IRQ_RING_SIZE*sizeof(u32), &dpriv->iqrx_dma);
793                 if (!dpriv->iqrx)
794                         goto err_out_free_iqrx;
795                 writel(dpriv->iqrx_dma, ioaddr + IQRX0 + i*4);
796         }
797
798         /* Cf application hint. Beware of hard-lock condition on threshold. */
799         writel(0x42104000, ioaddr + FIFOCR1);
800         //writel(0x9ce69800, ioaddr + FIFOCR2);
801         writel(0xdef6d800, ioaddr + FIFOCR2);
802         //writel(0x11111111, ioaddr + FIFOCR4);
803         writel(0x18181818, ioaddr + FIFOCR4);
804         // FIXME: should depend on the chipset revision
805         writel(0x0000000e, ioaddr + FIFOCR3);
806
807         writel(0xff200001, ioaddr + GCMDR);
808
809         cards_found++;
810         return 0;
811
812 err_out_free_iqrx:
813         while (--i >= 0) {
814                 dpriv = priv->root + i;
815                 pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
816                                     dpriv->iqrx, dpriv->iqrx_dma);
817         }
818         i = dev_per_card;
819 err_out_free_iqtx:
820         while (--i >= 0) {
821                 dpriv = priv->root + i;
822                 pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
823                                     dpriv->iqtx, dpriv->iqtx_dma);
824         }
825         pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32), priv->iqcfg,
826                             priv->iqcfg_dma);
827 err_out_free_irq:
828         free_irq(pdev->irq, priv->root);
829 err_out_free1:
830         dscc4_free1(pdev);
831 err_out_iounmap:
832         iounmap ((void *)ioaddr);
833 err_out_free_mmio_region:
834         release_mem_region(pci_resource_start(pdev, 1),
835                            pci_resource_len(pdev, 1));
836 err_out_free_mmio_region0:
837         release_mem_region(pci_resource_start(pdev, 0),
838                            pci_resource_len(pdev, 0));
839 err_out:
840         return -ENODEV;
841 };
842
843 /*
844  * Let's hope the default values are decent enough to protect my
845  * feet from the user's gun - Ueimor
846  */
847 static void dscc4_init_registers(struct dscc4_dev_priv *dpriv,
848                                  struct net_device *dev)
849 {
850         /* No interrupts, SCC core disabled. Let's relax */
851         scc_writel(0x00000000, dpriv, dev, CCR0);
852
853         scc_writel(LengthCheck | (HDLC_MAX_MRU >> 5), dpriv, dev, RLCR);
854
855         /*
856          * No address recognition/crc-CCITT/cts enabled
857          * Shared flags transmission disabled - cf errata DS5 p.11
858          * Carrier detect disabled - cf errata p.14
859          * FIXME: carrier detection/polarity may be handled more gracefully.
860          */
861         scc_writel(0x02408000, dpriv, dev, CCR1);
862
863         /* crc not forwarded - Cf errata DS5 p.11 */
864         scc_writel(0x00050008 & ~RxActivate, dpriv, dev, CCR2);
865         // crc forwarded
866         //scc_writel(0x00250008 & ~RxActivate, dpriv, dev, CCR2);
867 }
868
869 static int dscc4_found1(struct pci_dev *pdev, unsigned long ioaddr)
870 {
871         struct dscc4_pci_priv *ppriv;
872         struct dscc4_dev_priv *root;
873         int i, ret = -ENOMEM;
874
875         root = (struct dscc4_dev_priv *)
876                 kmalloc(dev_per_card*sizeof(*root), GFP_KERNEL);
877         if (!root) {
878                 printk(KERN_ERR "%s: can't allocate data\n", DRV_NAME);
879                 goto err_out;
880         }
881         memset(root, 0, dev_per_card*sizeof(*root));
882
883         for (i = 0; i < dev_per_card; i++) {
884                 root[i].dev = alloc_hdlcdev(root + i);
885                 if (!root[i].dev) {
886                         while (i--)
887                                 free_netdev(root[i].dev);
888                         goto err_free_dev;
889                 }
890         }
891
892         ppriv = (struct dscc4_pci_priv *) kmalloc(sizeof(*ppriv), GFP_KERNEL);
893         if (!ppriv) {
894                 printk(KERN_ERR "%s: can't allocate private data\n", DRV_NAME);
895                 goto err_free_dev2;
896         }
897         memset(ppriv, 0, sizeof(struct dscc4_pci_priv));
898         ret = dscc4_set_quartz(root, quartz);
899         if (ret < 0)
900                 goto err_free_priv;
901         ppriv->root = root;
902         spin_lock_init(&ppriv->lock);
903
904         for (i = 0; i < dev_per_card; i++) {
905                 struct dscc4_dev_priv *dpriv = root + i;
906                 struct net_device *d = dscc4_to_dev(dpriv);
907                 hdlc_device *hdlc = dev_to_hdlc(d);
908
909                 d->base_addr = ioaddr;
910                 d->init = NULL;
911                 d->irq = pdev->irq;
912                 d->open = dscc4_open;
913                 d->stop = dscc4_close;
914                 d->set_multicast_list = NULL;
915                 d->do_ioctl = dscc4_ioctl;
916                 d->tx_timeout = dscc4_tx_timeout;
917                 d->watchdog_timeo = TX_TIMEOUT;
918                 SET_MODULE_OWNER(d);
919                 SET_NETDEV_DEV(d, &pdev->dev);
920
921                 dpriv->dev_id = i;
922                 dpriv->pci_priv = ppriv;
923                 spin_lock_init(&dpriv->lock);
924
925                 hdlc->xmit = dscc4_start_xmit;
926                 hdlc->attach = dscc4_hdlc_attach;
927
928                 dscc4_init_registers(dpriv, d);
929                 dpriv->parity = PARITY_CRC16_PR0_CCITT;
930                 dpriv->encoding = ENCODING_NRZ;
931         
932                 ret = dscc4_init_ring(d);
933                 if (ret < 0)
934                         goto err_unregister;
935
936                 ret = register_hdlc_device(d);
937                 if (ret < 0) {
938                         printk(KERN_ERR "%s: unable to register\n", DRV_NAME);
939                         dscc4_release_ring(dpriv);
940                         goto err_unregister;
941                 }
942         }
943         pci_set_drvdata(pdev, ppriv);
944         return ret;
945
946 err_unregister:
947         while (--i >= 0) {
948                 dscc4_release_ring(root + i);
949                 unregister_hdlc_device(dscc4_to_dev(&root[i]));
950         }
951 err_free_priv:
952         kfree(ppriv);
953 err_free_dev2:
954         for (i = 0; i < dev_per_card; i++)
955                 free_netdev(root[i].dev);
956 err_free_dev:
957         kfree(root);
958 err_out:
959         return ret;
960 };
961
962 /* FIXME: get rid of the unneeded code */
963 static void dscc4_timer(unsigned long data)
964 {
965         struct net_device *dev = (struct net_device *)data;
966         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
967 //      struct dscc4_pci_priv *ppriv;
968
969         goto done;
970 done:
971         dpriv->timer.expires = jiffies + TX_TIMEOUT;
972         add_timer(&dpriv->timer);
973 }
974
975 static void dscc4_tx_timeout(struct net_device *dev)
976 {
977         /* FIXME: something is missing there */
978 }
979
980 static int dscc4_loopback_check(struct dscc4_dev_priv *dpriv)
981 {
982         sync_serial_settings *settings = &dpriv->settings;
983
984         if (settings->loopback && (settings->clock_type != CLOCK_INT)) {
985                 struct net_device *dev = dscc4_to_dev(dpriv);
986
987                 printk(KERN_INFO "%s: loopback requires clock\n", dev->name);
988                 return -1;
989         }
990         return 0;
991 }
992
993 #ifdef CONFIG_DSCC4_PCI_RST
994 /*
995  * Some DSCC4-based cards wires the GPIO port and the PCI #RST pin together
996  * so as to provide a safe way to reset the asic while not the whole machine
997  * rebooting.
998  *
999  * This code doesn't need to be efficient. Keep It Simple
1000  */
1001 static void dscc4_pci_reset(struct pci_dev *pdev, unsigned long ioaddr)
1002 {
1003         int i;
1004
1005         down(&dscc4_sem);
1006         for (i = 0; i < 16; i++)
1007                 pci_read_config_dword(pdev, i << 2, dscc4_pci_config_store + i);
1008
1009         /* Maximal LBI clock divider (who cares ?) and whole GPIO range. */
1010         writel(0x001c0000, ioaddr + GMODE);
1011         /* Configure GPIO port as output */
1012         writel(0x0000ffff, ioaddr + GPDIR);
1013         /* Disable interruption */
1014         writel(0x0000ffff, ioaddr + GPIM);
1015
1016         writel(0x0000ffff, ioaddr + GPDATA);
1017         writel(0x00000000, ioaddr + GPDATA);
1018
1019         /* Flush posted writes */
1020         readl(ioaddr + GSTAR);
1021
1022         set_current_state(TASK_UNINTERRUPTIBLE);
1023         schedule_timeout(10);
1024
1025         for (i = 0; i < 16; i++)
1026                 pci_write_config_dword(pdev, i << 2, dscc4_pci_config_store[i]);
1027         up(&dscc4_sem);
1028 }
1029 #else
1030 #define dscc4_pci_reset(pdev,ioaddr)    do {} while (0)
1031 #endif /* CONFIG_DSCC4_PCI_RST */
1032
1033 static int dscc4_open(struct net_device *dev)
1034 {
1035         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
1036         struct dscc4_pci_priv *ppriv;
1037         int ret = -EAGAIN;
1038
1039         if ((dscc4_loopback_check(dpriv) < 0) || !dev->hard_start_xmit)
1040                 goto err;
1041
1042         if ((ret = hdlc_open(dev)))
1043                 goto err;
1044
1045         ppriv = dpriv->pci_priv;
1046
1047         /*
1048          * Due to various bugs, there is no way to reliably reset a
1049          * specific port (manufacturer's dependant special PCI #RST wiring
1050          * apart: it affects all ports). Thus the device goes in the best
1051          * silent mode possible at dscc4_close() time and simply claims to
1052          * be up if it's opened again. It still isn't possible to change
1053          * the HDLC configuration without rebooting but at least the ports
1054          * can be up/down ifconfig'ed without killing the host.
1055          */
1056         if (dpriv->flags & FakeReset) {
1057                 dpriv->flags &= ~FakeReset;
1058                 scc_patchl(0, PowerUp, dpriv, dev, CCR0);
1059                 scc_patchl(0, 0x00050000, dpriv, dev, CCR2);
1060                 scc_writel(EventsMask, dpriv, dev, IMR);
1061                 printk(KERN_INFO "%s: up again.\n", dev->name);
1062                 goto done;
1063         }
1064
1065         /* IDT+IDR during XPR */
1066         dpriv->flags = NeedIDR | NeedIDT;
1067
1068         scc_patchl(0, PowerUp | Vis, dpriv, dev, CCR0);
1069
1070         /*
1071          * The following is a bit paranoid...
1072          *
1073          * NB: the datasheet "...CEC will stay active if the SCC is in
1074          * power-down mode or..." and CCR2.RAC = 1 are two different
1075          * situations.
1076          */
1077         if (scc_readl_star(dpriv, dev) & SccBusy) {
1078                 printk(KERN_ERR "%s busy. Try later\n", dev->name);
1079                 ret = -EAGAIN;
1080                 goto err_out;
1081         } else
1082                 printk(KERN_INFO "%s: available. Good\n", dev->name);
1083
1084         scc_writel(EventsMask, dpriv, dev, IMR);
1085
1086         /* Posted write is flushed in the wait_ack loop */
1087         scc_writel(TxSccRes | RxSccRes, dpriv, dev, CMDR);
1088
1089         if ((ret = dscc4_wait_ack_cec(dpriv, dev, "Cec")) < 0)
1090                 goto err_disable_scc_events;
1091
1092         /*
1093          * I would expect XPR near CE completion (before ? after ?).
1094          * At worst, this code won't see a late XPR and people
1095          * will have to re-issue an ifconfig (this is harmless).
1096          * WARNING, a really missing XPR usually means a hardware
1097          * reset is needed. Suggestions anyone ?
1098          */
1099         if ((ret = dscc4_xpr_ack(dpriv)) < 0) {
1100                 printk(KERN_ERR "%s: %s timeout\n", DRV_NAME, "XPR");
1101                 goto err_disable_scc_events;
1102         }
1103         
1104         if (debug > 2)
1105                 dscc4_tx_print(dev, dpriv, "Open");
1106
1107 done:
1108         netif_start_queue(dev);
1109
1110         init_timer(&dpriv->timer);
1111         dpriv->timer.expires = jiffies + 10*HZ;
1112         dpriv->timer.data = (unsigned long)dev;
1113         dpriv->timer.function = &dscc4_timer;
1114         add_timer(&dpriv->timer);
1115         netif_carrier_on(dev);
1116
1117         return 0;
1118
1119 err_disable_scc_events:
1120         scc_writel(0xffffffff, dpriv, dev, IMR);
1121         scc_patchl(PowerUp | Vis, 0, dpriv, dev, CCR0);
1122 err_out:
1123         hdlc_close(dev);
1124 err:
1125         return ret;
1126 }
1127
1128 #ifdef DSCC4_POLLING
1129 static int dscc4_tx_poll(struct dscc4_dev_priv *dpriv, struct net_device *dev)
1130 {
1131         /* FIXME: it's gonna be easy (TM), for sure */
1132 }
1133 #endif /* DSCC4_POLLING */
1134
1135 static int dscc4_start_xmit(struct sk_buff *skb, struct net_device *dev)
1136 {
1137         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
1138         struct dscc4_pci_priv *ppriv = dpriv->pci_priv;
1139         struct TxFD *tx_fd;
1140         int next;
1141
1142         next = dpriv->tx_current%TX_RING_SIZE;
1143         dpriv->tx_skbuff[next] = skb;
1144         tx_fd = dpriv->tx_fd + next;
1145         tx_fd->state = FrameEnd | TO_STATE_TX(skb->len);
1146         tx_fd->data = pci_map_single(ppriv->pdev, skb->data, skb->len,
1147                                      PCI_DMA_TODEVICE);
1148         tx_fd->complete = 0x00000000;
1149         tx_fd->jiffies = jiffies;
1150         mb();
1151
1152 #ifdef DSCC4_POLLING
1153         spin_lock(&dpriv->lock);
1154         while (dscc4_tx_poll(dpriv, dev));
1155         spin_unlock(&dpriv->lock);
1156 #endif
1157
1158         dev->trans_start = jiffies;
1159
1160         if (debug > 2)
1161                 dscc4_tx_print(dev, dpriv, "Xmit");
1162         /* To be cleaned(unsigned int)/optimized. Later, ok ? */
1163         if (!((++dpriv->tx_current - dpriv->tx_dirty)%TX_RING_SIZE))
1164                 netif_stop_queue(dev);
1165
1166         if (dscc4_tx_quiescent(dpriv, dev))
1167                 dscc4_do_tx(dpriv, dev);
1168
1169         return 0;
1170 }
1171
1172 static int dscc4_close(struct net_device *dev)
1173 {
1174         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
1175
1176         del_timer_sync(&dpriv->timer);
1177         netif_stop_queue(dev);
1178
1179         scc_patchl(PowerUp | Vis, 0, dpriv, dev, CCR0);
1180         scc_patchl(0x00050000, 0, dpriv, dev, CCR2);
1181         scc_writel(0xffffffff, dpriv, dev, IMR);
1182
1183         dpriv->flags |= FakeReset;
1184
1185         hdlc_close(dev);
1186
1187         return 0;
1188 }
1189
1190 static inline int dscc4_check_clock_ability(int port)
1191 {
1192         int ret = 0;
1193
1194 #ifdef CONFIG_DSCC4_PCISYNC
1195         if (port >= 2)
1196                 ret = -1;
1197 #endif
1198         return ret;
1199 }
1200
1201 /*
1202  * DS1 p.137: "There are a total of 13 different clocking modes..."
1203  *                                  ^^
1204  * Design choices:
1205  * - by default, assume a clock is provided on pin RxClk/TxClk (clock mode 0a).
1206  *   Clock mode 3b _should_ work but the testing seems to make this point
1207  *   dubious (DIY testing requires setting CCR0 at 0x00000033).
1208  *   This is supposed to provide least surprise "DTE like" behavior.
1209  * - if line rate is specified, clocks are assumed to be locally generated.
1210  *   A quartz must be available (on pin XTAL1). Modes 6b/7b are used. Choosing
1211  *   between these it automagically done according on the required frequency
1212  *   scaling. Of course some rounding may take place.
1213  * - no high speed mode (40Mb/s). May be trivial to do but I don't have an
1214  *   appropriate external clocking device for testing.
1215  * - no time-slot/clock mode 5: shameless lazyness.
1216  *
1217  * The clock signals wiring can be (is ?) manufacturer dependant. Good luck.
1218  *
1219  * BIG FAT WARNING: if the device isn't provided enough clocking signal, it
1220  * won't pass the init sequence. For example, straight back-to-back DTE without
1221  * external clock will fail when dscc4_open() (<- 'ifconfig hdlcx xxx') is
1222  * called.
1223  *
1224  * Typos lurk in datasheet (missing divier in clock mode 7a figure 51 p.153
1225  * DS0 for example)
1226  *
1227  * Clock mode related bits of CCR0:
1228  *     +------------ TOE: output TxClk (0b/2b/3a/3b/6b/7a/7b only)
1229  *     | +---------- SSEL: sub-mode select 0 -> a, 1 -> b
1230  *     | | +-------- High Speed: say 0
1231  *     | | | +-+-+-- Clock Mode: 0..7
1232  *     | | | | | |
1233  * -+-+-+-+-+-+-+-+
1234  * x|x|5|4|3|2|1|0| lower bits
1235  *
1236  * Division factor of BRR: k = (N+1)x2^M (total divider = 16xk in mode 6b)
1237  *            +-+-+-+------------------ M (0..15)
1238  *            | | | |     +-+-+-+-+-+-- N (0..63)
1239  *    0 0 0 0 | | | | 0 0 | | | | | |
1240  * ...-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1241  *    f|e|d|c|b|a|9|8|7|6|5|4|3|2|1|0| lower bits
1242  *
1243  */
1244 static int dscc4_set_clock(struct net_device *dev, u32 *bps, u32 *state)
1245 {
1246         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
1247         int ret = -1;
1248         u32 brr;
1249
1250         *state &= ~Ccr0ClockMask;
1251         if (*bps) { /* Clock generated - required for DCE */
1252                 u32 n = 0, m = 0, divider;
1253                 int xtal;
1254
1255                 xtal = dpriv->pci_priv->xtal_hz;
1256                 if (!xtal)
1257                         goto done;
1258                 if (dscc4_check_clock_ability(dpriv->dev_id) < 0)
1259                         goto done;
1260                 divider = xtal / *bps;
1261                 if (divider > BRR_DIVIDER_MAX) {
1262                         divider >>= 4;
1263                         *state |= 0x00000036; /* Clock mode 6b (BRG/16) */
1264                 } else
1265                         *state |= 0x00000037; /* Clock mode 7b (BRG) */
1266                 if (divider >> 22) {
1267                         n = 63;
1268                         m = 15;
1269                 } else if (divider) {
1270                         /* Extraction of the 6 highest weighted bits */
1271                         m = 0;
1272                         while (0xffffffc0 & divider) {
1273                                 m++;
1274                                 divider >>= 1;
1275                         }
1276                         n = divider;
1277                 }
1278                 brr = (m << 8) | n;
1279                 divider = n << m;
1280                 if (!(*state & 0x00000001)) /* ?b mode mask => clock mode 6b */
1281                         divider <<= 4;
1282                 *bps = xtal / divider;
1283         } else {
1284                 /*
1285                  * External clock - DTE
1286                  * "state" already reflects Clock mode 0a (CCR0 = 0xzzzzzz00).
1287                  * Nothing more to be done
1288                  */
1289                 brr = 0;
1290         }
1291         scc_writel(brr, dpriv, dev, BRR);
1292         ret = 0;
1293 done:
1294         return ret;
1295 }
1296
1297 static int dscc4_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1298 {
1299         sync_serial_settings *line = ifr->ifr_settings.ifs_ifsu.sync;
1300         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
1301         const size_t size = sizeof(dpriv->settings);
1302         int ret = 0;
1303
1304         if (dev->flags & IFF_UP)
1305                 return -EBUSY;
1306
1307         if (cmd != SIOCWANDEV)
1308                 return -EOPNOTSUPP;
1309
1310         switch(ifr->ifr_settings.type) {
1311         case IF_GET_IFACE:
1312                 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1313                 if (ifr->ifr_settings.size < size) {
1314                         ifr->ifr_settings.size = size; /* data size wanted */
1315                         return -ENOBUFS;
1316                 }
1317                 if (copy_to_user(line, &dpriv->settings, size))
1318                         return -EFAULT;
1319                 break;
1320
1321         case IF_IFACE_SYNC_SERIAL:
1322                 if (!capable(CAP_NET_ADMIN))
1323                         return -EPERM;
1324
1325                 if (dpriv->flags & FakeReset) {
1326                         printk(KERN_INFO "%s: please reset the device"
1327                                " before this command\n", dev->name);
1328                         return -EPERM;
1329                 }
1330                 if (copy_from_user(&dpriv->settings, line, size))
1331                         return -EFAULT;
1332                 ret = dscc4_set_iface(dpriv, dev);
1333                 break;
1334
1335         default:
1336                 ret = hdlc_ioctl(dev, ifr, cmd);
1337                 break;
1338         }
1339
1340         return ret;
1341 }
1342
1343 static inline int dscc4_set_quartz(struct dscc4_dev_priv *dpriv, int hz)
1344 {
1345         int ret = 0;
1346
1347         if ((hz < 0) || (hz > DSCC4_HZ_MAX))
1348                 ret = -EOPNOTSUPP;
1349         else
1350                 dpriv->pci_priv->xtal_hz = hz;
1351
1352         return ret;
1353 }
1354
1355 static int dscc4_match(struct thingie *p, int value)
1356 {
1357         int i;
1358
1359         for (i = 0; p[i].define != -1; i++) {
1360                 if (value == p[i].define)
1361                         break;
1362         }
1363         if (p[i].define == -1)
1364                 return -1;
1365         else
1366                 return i;
1367 }
1368
1369 static int dscc4_clock_setting(struct dscc4_dev_priv *dpriv,
1370                                struct net_device *dev)
1371 {
1372         sync_serial_settings *settings = &dpriv->settings;
1373         int ret = -EOPNOTSUPP;
1374         u32 bps, state;
1375
1376         bps = settings->clock_rate;
1377         state = scc_readl(dpriv, CCR0);
1378         if (dscc4_set_clock(dev, &bps, &state) < 0)
1379                 goto done;
1380         if (bps) { /* DCE */
1381                 printk(KERN_DEBUG "%s: generated RxClk (DCE)\n", dev->name);
1382                 if (settings->clock_rate != bps) {
1383                         printk(KERN_DEBUG "%s: clock adjusted (%08d -> %08d)\n",
1384                                 dev->name, settings->clock_rate, bps);
1385                         settings->clock_rate = bps;
1386                 }
1387         } else { /* DTE */
1388                 state |= PowerUp | Vis;
1389                 printk(KERN_DEBUG "%s: external RxClk (DTE)\n", dev->name);
1390         }
1391         scc_writel(state, dpriv, dev, CCR0);
1392         ret = 0;
1393 done:
1394         return ret;
1395 }
1396
1397 static int dscc4_encoding_setting(struct dscc4_dev_priv *dpriv,
1398                                   struct net_device *dev)
1399 {
1400         struct thingie encoding[] = {
1401                 { ENCODING_NRZ,         0x00000000 },
1402                 { ENCODING_NRZI,        0x00200000 },
1403                 { ENCODING_FM_MARK,     0x00400000 },
1404                 { ENCODING_FM_SPACE,    0x00500000 },
1405                 { ENCODING_MANCHESTER,  0x00600000 },
1406                 { -1,                   0}
1407         };
1408         int i, ret = 0;
1409
1410         i = dscc4_match(encoding, dpriv->encoding);
1411         if (i >= 0)
1412                 scc_patchl(EncodingMask, encoding[i].bits, dpriv, dev, CCR0);
1413         else
1414                 ret = -EOPNOTSUPP;
1415         return ret;
1416 }
1417
1418 static int dscc4_loopback_setting(struct dscc4_dev_priv *dpriv,
1419                                   struct net_device *dev)
1420 {
1421         sync_serial_settings *settings = &dpriv->settings;
1422         u32 state;
1423
1424         state = scc_readl(dpriv, CCR1);
1425         if (settings->loopback) {
1426                 printk(KERN_DEBUG "%s: loopback\n", dev->name);
1427                 state |= 0x00000100;
1428         } else {
1429                 printk(KERN_DEBUG "%s: normal\n", dev->name);
1430                 state &= ~0x00000100;
1431         }
1432         scc_writel(state, dpriv, dev, CCR1);
1433         return 0;
1434 }
1435
1436 static int dscc4_crc_setting(struct dscc4_dev_priv *dpriv,
1437                              struct net_device *dev)
1438 {
1439         struct thingie crc[] = {
1440                 { PARITY_CRC16_PR0_CCITT,       0x00000010 },
1441                 { PARITY_CRC16_PR1_CCITT,       0x00000000 },
1442                 { PARITY_CRC32_PR0_CCITT,       0x00000011 },
1443                 { PARITY_CRC32_PR1_CCITT,       0x00000001 }
1444         };
1445         int i, ret = 0;
1446
1447         i = dscc4_match(crc, dpriv->parity);
1448         if (i >= 0)
1449                 scc_patchl(CrcMask, crc[i].bits, dpriv, dev, CCR1);
1450         else
1451                 ret = -EOPNOTSUPP;
1452         return ret;
1453 }
1454
1455 static int dscc4_set_iface(struct dscc4_dev_priv *dpriv, struct net_device *dev)
1456 {
1457         struct {
1458                 int (*action)(struct dscc4_dev_priv *, struct net_device *);
1459         } *p, do_setting[] = {
1460                 { dscc4_encoding_setting },
1461                 { dscc4_clock_setting },
1462                 { dscc4_loopback_setting },
1463                 { dscc4_crc_setting },
1464                 { NULL }
1465         };
1466         int ret = 0;
1467
1468         for (p = do_setting; p->action; p++) {
1469                 if ((ret = p->action(dpriv, dev)) < 0)
1470                         break;
1471         }
1472         return ret;
1473 }
1474
1475 static irqreturn_t dscc4_irq(int irq, void *token, struct pt_regs *ptregs)
1476 {
1477         struct dscc4_dev_priv *root = token;
1478         struct dscc4_pci_priv *priv;
1479         struct net_device *dev;
1480         unsigned long ioaddr;
1481         u32 state;
1482         unsigned long flags;
1483         int i, handled = 1;
1484
1485         priv = root->pci_priv;
1486         dev = dscc4_to_dev(root);
1487
1488         spin_lock_irqsave(&priv->lock, flags);
1489
1490         ioaddr = dev->base_addr;
1491
1492         state = readl(ioaddr + GSTAR);
1493         if (!state) {
1494                 handled = 0;
1495                 goto out;
1496         }
1497         if (debug > 3)
1498                 printk(KERN_DEBUG "%s: GSTAR = 0x%08x\n", DRV_NAME, state);
1499         writel(state, ioaddr + GSTAR);
1500
1501         if (state & Arf) {
1502                 printk(KERN_ERR "%s: failure (Arf). Harass the maintener\n",
1503                        dev->name);
1504                 goto out;
1505         }
1506         state &= ~ArAck;
1507         if (state & Cfg) {
1508                 if (debug > 0)
1509                         printk(KERN_DEBUG "%s: CfgIV\n", DRV_NAME);
1510                 if (priv->iqcfg[priv->cfg_cur++%IRQ_RING_SIZE] & Arf)
1511                         printk(KERN_ERR "%s: %s failed\n", dev->name, "CFG");
1512                 if (!(state &= ~Cfg))
1513                         goto out;
1514         }
1515         if (state & RxEvt) {
1516                 i = dev_per_card - 1;
1517                 do {
1518                         dscc4_rx_irq(priv, root + i);
1519                 } while (--i >= 0);
1520                 state &= ~RxEvt;
1521         }
1522         if (state & TxEvt) {
1523                 i = dev_per_card - 1;
1524                 do {
1525                         dscc4_tx_irq(priv, root + i);
1526                 } while (--i >= 0);
1527                 state &= ~TxEvt;
1528         }
1529 out:
1530         spin_unlock_irqrestore(&priv->lock, flags);
1531         return IRQ_RETVAL(handled);
1532 }
1533
1534 static inline void dscc4_tx_irq(struct dscc4_pci_priv *ppriv,
1535                                 struct dscc4_dev_priv *dpriv)
1536 {
1537         struct net_device *dev = dscc4_to_dev(dpriv);
1538         u32 state;
1539         int cur, loop = 0;
1540
1541 try:
1542         cur = dpriv->iqtx_current%IRQ_RING_SIZE;
1543         state = dpriv->iqtx[cur];
1544         if (!state) {
1545                 if (debug > 4)
1546                         printk(KERN_DEBUG "%s: Tx ISR = 0x%08x\n", dev->name,
1547                                state);
1548                 if ((debug > 1) && (loop > 1))
1549                         printk(KERN_DEBUG "%s: Tx irq loop=%d\n", dev->name, loop);
1550                 if (loop && netif_queue_stopped(dev))
1551                         if ((dpriv->tx_current - dpriv->tx_dirty)%TX_RING_SIZE)
1552                                 netif_wake_queue(dev);
1553
1554                 if (netif_running(dev) && dscc4_tx_quiescent(dpriv, dev) &&
1555                     !dscc4_tx_done(dpriv))
1556                                 dscc4_do_tx(dpriv, dev);
1557                 return;
1558         }
1559         loop++;
1560         dpriv->iqtx[cur] = 0;
1561         dpriv->iqtx_current++;
1562
1563         if (state_check(state, dpriv, dev, "Tx") < 0)
1564                 return;
1565
1566         if (state & SccEvt) {
1567                 if (state & Alls) {
1568                         struct net_device_stats *stats = hdlc_stats(dev);
1569                         struct sk_buff *skb;
1570                         struct TxFD *tx_fd;
1571
1572                         if (debug > 2)
1573                                 dscc4_tx_print(dev, dpriv, "Alls");
1574                         /*
1575                          * DataComplete can't be trusted for Tx completion.
1576                          * Cf errata DS5 p.8
1577                          */
1578                         cur = dpriv->tx_dirty%TX_RING_SIZE;
1579                         tx_fd = dpriv->tx_fd + cur;
1580                         skb = dpriv->tx_skbuff[cur];
1581                         if (skb) {
1582                                 pci_unmap_single(ppriv->pdev, tx_fd->data,
1583                                                  skb->len, PCI_DMA_TODEVICE);
1584                                 if (tx_fd->state & FrameEnd) {
1585                                         stats->tx_packets++;
1586                                         stats->tx_bytes += skb->len;
1587                                 }
1588                                 dev_kfree_skb_irq(skb);
1589                                 dpriv->tx_skbuff[cur] = NULL;
1590                                 ++dpriv->tx_dirty;
1591                         } else {
1592                                 if (debug > 1)
1593                                         printk(KERN_ERR "%s Tx: NULL skb %d\n",
1594                                                 dev->name, cur);
1595                         }
1596                         /*
1597                          * If the driver ends sending crap on the wire, it
1598                          * will be way easier to diagnose than the (not so)
1599                          * random freeze induced by null sized tx frames.
1600                          */
1601                         tx_fd->data = tx_fd->next;
1602                         tx_fd->state = FrameEnd | TO_STATE_TX(2*DUMMY_SKB_SIZE);
1603                         tx_fd->complete = 0x00000000;
1604                         tx_fd->jiffies = 0;
1605
1606                         if (!(state &= ~Alls))
1607                                 goto try;
1608                 }
1609                 /*
1610                  * Transmit Data Underrun
1611                  */
1612                 if (state & Xdu) {
1613                         printk(KERN_ERR "%s: XDU. Ask maintainer\n", DRV_NAME);
1614                         dpriv->flags = NeedIDT;
1615                         /* Tx reset */
1616                         writel(MTFi | Rdt,
1617                                dev->base_addr + 0x0c*dpriv->dev_id + CH0CFG);
1618                         writel(Action, dev->base_addr + GCMDR);
1619                         return;
1620                 }
1621                 if (state & Cts) {
1622                         printk(KERN_INFO "%s: CTS transition\n", dev->name);
1623                         if (!(state &= ~Cts)) /* DEBUG */
1624                                 goto try;
1625                 }
1626                 if (state & Xmr) {
1627                         /* Frame needs to be sent again - FIXME */
1628                         printk(KERN_ERR "%s: Xmr. Ask maintainer\n", DRV_NAME);
1629                         if (!(state &= ~Xmr)) /* DEBUG */
1630                                 goto try;
1631                 }
1632                 if (state & Xpr) {
1633                         unsigned long scc_addr, ring;
1634                         int i;
1635
1636                         /*
1637                          * - the busy condition happens (sometimes);
1638                          * - it doesn't seem to make the handler unreliable.
1639                          */
1640                         for (i = 1; i; i <<= 1) {
1641                                 if (!(scc_readl_star(dpriv, dev) & SccBusy))
1642                                         break;
1643                         }
1644                         if (!i)
1645                                 printk(KERN_INFO "%s busy in irq\n", dev->name);
1646
1647                         scc_addr = dev->base_addr + 0x0c*dpriv->dev_id;
1648                         /* Keep this order: IDT before IDR */
1649                         if (dpriv->flags & NeedIDT) {
1650                                 if (debug > 2)
1651                                         dscc4_tx_print(dev, dpriv, "Xpr");
1652                                 ring = dpriv->tx_fd_dma +
1653                                        (dpriv->tx_dirty%TX_RING_SIZE)*
1654                                        sizeof(struct TxFD);
1655                                 writel(ring, scc_addr + CH0BTDA);
1656                                 dscc4_do_tx(dpriv, dev);
1657                                 writel(MTFi | Idt, scc_addr + CH0CFG);
1658                                 if (dscc4_do_action(dev, "IDT") < 0)
1659                                         goto err_xpr;
1660                                 dpriv->flags &= ~NeedIDT;
1661                         }
1662                         if (dpriv->flags & NeedIDR) {
1663                                 ring = dpriv->rx_fd_dma +
1664                                        (dpriv->rx_current%RX_RING_SIZE)*
1665                                        sizeof(struct RxFD);
1666                                 writel(ring, scc_addr + CH0BRDA);
1667                                 dscc4_rx_update(dpriv, dev);
1668                                 writel(MTFi | Idr, scc_addr + CH0CFG);
1669                                 if (dscc4_do_action(dev, "IDR") < 0)
1670                                         goto err_xpr;
1671                                 dpriv->flags &= ~NeedIDR;
1672                                 smp_wmb();
1673                                 /* Activate receiver and misc */
1674                                 scc_writel(0x08050008, dpriv, dev, CCR2);
1675                         }
1676                 err_xpr:
1677                         if (!(state &= ~Xpr))
1678                                 goto try;
1679                 }
1680                 if (state & Cd) {
1681                         if (debug > 0)
1682                                 printk(KERN_INFO "%s: CD transition\n", dev->name);
1683                         if (!(state &= ~Cd)) /* DEBUG */
1684                                 goto try;
1685                 }
1686         } else { /* ! SccEvt */
1687                 if (state & Hi) {
1688 #ifdef DSCC4_POLLING
1689                         while (!dscc4_tx_poll(dpriv, dev));
1690 #endif
1691                         printk(KERN_INFO "%s: Tx Hi\n", dev->name);
1692                         state &= ~Hi;
1693                 }
1694                 if (state & Err) {
1695                         printk(KERN_INFO "%s: Tx ERR\n", dev->name);
1696                         hdlc_stats(dev)->tx_errors++;
1697                         state &= ~Err;
1698                 }
1699         }
1700         goto try;
1701 }
1702
1703 static inline void dscc4_rx_irq(struct dscc4_pci_priv *priv,
1704                                     struct dscc4_dev_priv *dpriv)
1705 {
1706         struct net_device *dev = dscc4_to_dev(dpriv);
1707         u32 state;
1708         int cur;
1709
1710 try:
1711         cur = dpriv->iqrx_current%IRQ_RING_SIZE;
1712         state = dpriv->iqrx[cur];
1713         if (!state)
1714                 return;
1715         dpriv->iqrx[cur] = 0;
1716         dpriv->iqrx_current++;
1717
1718         if (state_check(state, dpriv, dev, "Rx") < 0)
1719                 return;
1720
1721         if (!(state & SccEvt)){
1722                 struct RxFD *rx_fd;
1723
1724                 if (debug > 4)
1725                         printk(KERN_DEBUG "%s: Rx ISR = 0x%08x\n", dev->name,
1726                                state);
1727                 state &= 0x00ffffff;
1728                 if (state & Err) { /* Hold or reset */
1729                         printk(KERN_DEBUG "%s: Rx ERR\n", dev->name);
1730                         cur = dpriv->rx_current%RX_RING_SIZE;
1731                         rx_fd = dpriv->rx_fd + cur;
1732                         /*
1733                          * Presume we're not facing a DMAC receiver reset.
1734                          * As We use the rx size-filtering feature of the
1735                          * DSCC4, the beginning of a new frame is waiting in
1736                          * the rx fifo. I bet a Receive Data Overflow will
1737                          * happen most of time but let's try and avoid it.
1738                          * Btw (as for RDO) if one experiences ERR whereas
1739                          * the system looks rather idle, there may be a
1740                          * problem with latency. In this case, increasing
1741                          * RX_RING_SIZE may help.
1742                          */
1743                         //while (dpriv->rx_needs_refill) {
1744                                 while (!(rx_fd->state1 & Hold)) {
1745                                         rx_fd++;
1746                                         cur++;
1747                                         if (!(cur = cur%RX_RING_SIZE))
1748                                                 rx_fd = dpriv->rx_fd;
1749                                 }
1750                                 //dpriv->rx_needs_refill--;
1751                                 try_get_rx_skb(dpriv, dev);
1752                                 if (!rx_fd->data)
1753                                         goto try;
1754                                 rx_fd->state1 &= ~Hold;
1755                                 rx_fd->state2 = 0x00000000;
1756                                 rx_fd->end = 0xbabeface;
1757                         //}
1758                         goto try;
1759                 }
1760                 if (state & Fi) {
1761                         dscc4_rx_skb(dpriv, dev);
1762                         goto try;
1763                 }
1764                 if (state & Hi ) { /* HI bit */
1765                         printk(KERN_INFO "%s: Rx Hi\n", dev->name);
1766                         state &= ~Hi;
1767                         goto try;
1768                 }
1769         } else { /* SccEvt */
1770                 if (debug > 1) {
1771                         //FIXME: verifier la presence de tous les evenements
1772                 static struct {
1773                         u32 mask;
1774                         const char *irq_name;
1775                 } evts[] = {
1776                         { 0x00008000, "TIN"},
1777                         { 0x00000020, "RSC"},
1778                         { 0x00000010, "PCE"},
1779                         { 0x00000008, "PLLA"},
1780                         { 0, NULL}
1781                 }, *evt;
1782
1783                 for (evt = evts; evt->irq_name; evt++) {
1784                         if (state & evt->mask) {
1785                                         printk(KERN_DEBUG "%s: %s\n",
1786                                                 dev->name, evt->irq_name);
1787                                 if (!(state &= ~evt->mask))
1788                                         goto try;
1789                         }
1790                 }
1791                 } else {
1792                         if (!(state &= ~0x0000c03c))
1793                                 goto try;
1794                 }
1795                 if (state & Cts) {
1796                         printk(KERN_INFO "%s: CTS transition\n", dev->name);
1797                         if (!(state &= ~Cts)) /* DEBUG */
1798                                 goto try;
1799                 }
1800                 /*
1801                  * Receive Data Overflow (FIXME: fscked)
1802                  */
1803                 if (state & Rdo) {
1804                         struct RxFD *rx_fd;
1805                         u32 scc_addr;
1806                         int cur;
1807
1808                         //if (debug)
1809                         //      dscc4_rx_dump(dpriv);
1810                         scc_addr = dev->base_addr + 0x0c*dpriv->dev_id;
1811
1812                         scc_patchl(RxActivate, 0, dpriv, dev, CCR2);
1813                         /*
1814                          * This has no effect. Why ?
1815                          * ORed with TxSccRes, one sees the CFG ack (for
1816                          * the TX part only).
1817                          */
1818                         scc_writel(RxSccRes, dpriv, dev, CMDR);
1819                         dpriv->flags |= RdoSet;
1820
1821                         /*
1822                          * Let's try and save something in the received data.
1823                          * rx_current must be incremented at least once to
1824                          * avoid HOLD in the BRDA-to-be-pointed desc.
1825                          */
1826                         do {
1827                                 cur = dpriv->rx_current++%RX_RING_SIZE;
1828                                 rx_fd = dpriv->rx_fd + cur;
1829                                 if (!(rx_fd->state2 & DataComplete))
1830                                         break;
1831                                 if (rx_fd->state2 & FrameAborted) {
1832                                         hdlc_stats(dev)->rx_over_errors++;
1833                                         rx_fd->state1 |= Hold;
1834                                         rx_fd->state2 = 0x00000000;
1835                                         rx_fd->end = 0xbabeface;
1836                                 } else
1837                                         dscc4_rx_skb(dpriv, dev);
1838                         } while (1);
1839
1840                         if (debug > 0) {
1841                                 if (dpriv->flags & RdoSet)
1842                                         printk(KERN_DEBUG
1843                                                "%s: no RDO in Rx data\n", DRV_NAME);
1844                         }
1845 #ifdef DSCC4_RDO_EXPERIMENTAL_RECOVERY
1846                         /*
1847                          * FIXME: must the reset be this violent ?
1848                          */
1849 #warning "FIXME: CH0BRDA"
1850                         writel(dpriv->rx_fd_dma +
1851                                (dpriv->rx_current%RX_RING_SIZE)*
1852                                sizeof(struct RxFD), scc_addr + CH0BRDA);
1853                         writel(MTFi|Rdr|Idr, scc_addr + CH0CFG);
1854                         if (dscc4_do_action(dev, "RDR") < 0) {
1855                                 printk(KERN_ERR "%s: RDO recovery failed(%s)\n",
1856                                        dev->name, "RDR");
1857                                 goto rdo_end;
1858                         }
1859                         writel(MTFi|Idr, scc_addr + CH0CFG);
1860                         if (dscc4_do_action(dev, "IDR") < 0) {
1861                                 printk(KERN_ERR "%s: RDO recovery failed(%s)\n",
1862                                        dev->name, "IDR");
1863                                 goto rdo_end;
1864                         }
1865                 rdo_end:
1866 #endif
1867                         scc_patchl(0, RxActivate, dpriv, dev, CCR2);
1868                         goto try;
1869                 }
1870                 if (state & Cd) {
1871                         printk(KERN_INFO "%s: CD transition\n", dev->name);
1872                         if (!(state &= ~Cd)) /* DEBUG */
1873                                 goto try;
1874                 }
1875                 if (state & Flex) {
1876                         printk(KERN_DEBUG "%s: Flex. Ttttt...\n", DRV_NAME);
1877                         if (!(state &= ~Flex))
1878                                 goto try;
1879                 }
1880         }
1881 }
1882
1883 /*
1884  * I had expected the following to work for the first descriptor
1885  * (tx_fd->state = 0xc0000000)
1886  * - Hold=1 (don't try and branch to the next descripto);
1887  * - No=0 (I want an empty data section, i.e. size=0);
1888  * - Fe=1 (required by No=0 or we got an Err irq and must reset).
1889  * It failed and locked solid. Thus the introduction of a dummy skb.
1890  * Problem is acknowledged in errata sheet DS5. Joy :o/
1891  */
1892 struct sk_buff *dscc4_init_dummy_skb(struct dscc4_dev_priv *dpriv)
1893 {
1894         struct sk_buff *skb;
1895
1896         skb = dev_alloc_skb(DUMMY_SKB_SIZE);
1897         if (skb) {
1898                 int last = dpriv->tx_dirty%TX_RING_SIZE;
1899                 struct TxFD *tx_fd = dpriv->tx_fd + last;
1900
1901                 skb->len = DUMMY_SKB_SIZE;
1902                 memcpy(skb->data, version, strlen(version)%DUMMY_SKB_SIZE);
1903                 tx_fd->state = FrameEnd | TO_STATE_TX(DUMMY_SKB_SIZE);
1904                 tx_fd->data = pci_map_single(dpriv->pci_priv->pdev, skb->data,
1905                                              DUMMY_SKB_SIZE, PCI_DMA_TODEVICE);
1906                 dpriv->tx_skbuff[last] = skb;
1907         }
1908         return skb;
1909 }
1910
1911 static int dscc4_init_ring(struct net_device *dev)
1912 {
1913         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
1914         struct pci_dev *pdev = dpriv->pci_priv->pdev;
1915         struct TxFD *tx_fd;
1916         struct RxFD *rx_fd;
1917         void *ring;
1918         int i;
1919
1920         ring = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &dpriv->rx_fd_dma);
1921         if (!ring)
1922                 goto err_out;
1923         dpriv->rx_fd = rx_fd = (struct RxFD *) ring;
1924
1925         ring = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &dpriv->tx_fd_dma);
1926         if (!ring)
1927                 goto err_free_dma_rx;
1928         dpriv->tx_fd = tx_fd = (struct TxFD *) ring;
1929
1930         memset(dpriv->tx_skbuff, 0, sizeof(struct sk_buff *)*TX_RING_SIZE);
1931         dpriv->tx_dirty = 0xffffffff;
1932         i = dpriv->tx_current = 0;
1933         do {
1934                 tx_fd->state = FrameEnd | TO_STATE_TX(2*DUMMY_SKB_SIZE);
1935                 tx_fd->complete = 0x00000000;
1936                 /* FIXME: NULL should be ok - to be tried */
1937                 tx_fd->data = dpriv->tx_fd_dma;
1938                 (tx_fd++)->next = (u32)(dpriv->tx_fd_dma +
1939                                         (++i%TX_RING_SIZE)*sizeof(*tx_fd));
1940         } while (i < TX_RING_SIZE);
1941
1942         if (dscc4_init_dummy_skb(dpriv) < 0)
1943                 goto err_free_dma_tx;
1944
1945         memset(dpriv->rx_skbuff, 0, sizeof(struct sk_buff *)*RX_RING_SIZE);
1946         i = dpriv->rx_dirty = dpriv->rx_current = 0;
1947         do {
1948                 /* size set by the host. Multiple of 4 bytes please */
1949                 rx_fd->state1 = HiDesc;
1950                 rx_fd->state2 = 0x00000000;
1951                 rx_fd->end = 0xbabeface;
1952                 rx_fd->state1 |= TO_STATE_RX(HDLC_MAX_MRU);
1953                 // FIXME: return value verifiee mais traitement suspect
1954                 if (try_get_rx_skb(dpriv, dev) >= 0)
1955                         dpriv->rx_dirty++;
1956                 (rx_fd++)->next = (u32)(dpriv->rx_fd_dma +
1957                                         (++i%RX_RING_SIZE)*sizeof(*rx_fd));
1958         } while (i < RX_RING_SIZE);
1959
1960         return 0;
1961
1962 err_free_dma_tx:
1963         pci_free_consistent(pdev, TX_TOTAL_SIZE, ring, dpriv->tx_fd_dma);
1964 err_free_dma_rx:
1965         pci_free_consistent(pdev, RX_TOTAL_SIZE, rx_fd, dpriv->rx_fd_dma);
1966 err_out:
1967         return -ENOMEM;
1968 }
1969
1970 static void __devexit dscc4_remove_one(struct pci_dev *pdev)
1971 {
1972         struct dscc4_pci_priv *ppriv;
1973         struct dscc4_dev_priv *root;
1974         unsigned long ioaddr;
1975         int i;
1976
1977         ppriv = pci_get_drvdata(pdev);
1978         root = ppriv->root;
1979
1980         ioaddr = dscc4_to_dev(root)->base_addr;
1981
1982         dscc4_pci_reset(pdev, ioaddr);
1983
1984         free_irq(pdev->irq, root);
1985         pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32), ppriv->iqcfg,
1986                             ppriv->iqcfg_dma);
1987         for (i = 0; i < dev_per_card; i++) {
1988                 struct dscc4_dev_priv *dpriv = root + i;
1989
1990                 dscc4_release_ring(dpriv);
1991                 pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
1992                                     dpriv->iqrx, dpriv->iqrx_dma);
1993                 pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
1994                                     dpriv->iqtx, dpriv->iqtx_dma);
1995         }
1996
1997         dscc4_free1(pdev);
1998
1999         iounmap((void *)ioaddr);
2000
2001         release_mem_region(pci_resource_start(pdev, 1),
2002                            pci_resource_len(pdev, 1));
2003         release_mem_region(pci_resource_start(pdev, 0),
2004                            pci_resource_len(pdev, 0));
2005 }
2006
2007 static int dscc4_hdlc_attach(struct net_device *dev, unsigned short encoding,
2008         unsigned short parity)
2009 {
2010         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
2011
2012         if (encoding != ENCODING_NRZ &&
2013             encoding != ENCODING_NRZI &&
2014             encoding != ENCODING_FM_MARK &&
2015             encoding != ENCODING_FM_SPACE &&
2016             encoding != ENCODING_MANCHESTER)
2017                 return -EINVAL;
2018
2019         if (parity != PARITY_NONE &&
2020             parity != PARITY_CRC16_PR0_CCITT &&
2021             parity != PARITY_CRC16_PR1_CCITT &&
2022             parity != PARITY_CRC32_PR0_CCITT &&
2023             parity != PARITY_CRC32_PR1_CCITT)
2024                 return -EINVAL;
2025
2026         dpriv->encoding = encoding;
2027         dpriv->parity = parity;
2028         return 0;
2029 }
2030
2031 #ifndef MODULE
2032 static int __init dscc4_setup(char *str)
2033 {
2034         int *args[] = { &debug, &quartz, NULL }, **p = args;
2035
2036         while (*p && (get_option(&str, *p) == 2))
2037                 p++;
2038         return 1;
2039 }
2040
2041 __setup("dscc4.setup=", dscc4_setup);
2042 #endif
2043
2044 static struct pci_device_id dscc4_pci_tbl[] = {
2045         { PCI_VENDOR_ID_SIEMENS, PCI_DEVICE_ID_SIEMENS_DSCC4,
2046                 PCI_ANY_ID, PCI_ANY_ID, },
2047         { 0,}
2048 };
2049 MODULE_DEVICE_TABLE(pci, dscc4_pci_tbl);
2050
2051 static struct pci_driver dscc4_driver = {
2052         .name           = DRV_NAME,
2053         .id_table       = dscc4_pci_tbl,
2054         .probe          = dscc4_init_one,
2055         .remove         = __devexit_p(dscc4_remove_one),
2056 };
2057
2058 static int __init dscc4_init_module(void)
2059 {
2060         return pci_module_init(&dscc4_driver);
2061 }
2062
2063 static void __exit dscc4_cleanup_module(void)
2064 {
2065         pci_unregister_driver(&dscc4_driver);
2066 }
2067
2068 module_init(dscc4_init_module);
2069 module_exit(dscc4_cleanup_module);