vserver 1.9.3
[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 void dscc4_rx_irq(struct dscc4_pci_priv *, struct dscc4_dev_priv *);
355 static 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 #ifdef DSCC4_POLLING
370 static int dscc4_tx_poll(struct dscc4_dev_priv *, struct net_device *);
371 #endif
372
373 static inline struct dscc4_dev_priv *dscc4_priv(struct net_device *dev)
374 {
375         return dev_to_hdlc(dev)->priv;
376 }
377
378 static inline struct net_device *dscc4_to_dev(struct dscc4_dev_priv *p)
379 {
380         return p->dev;
381 }
382
383 static void scc_patchl(u32 mask, u32 value, struct dscc4_dev_priv *dpriv,
384                         struct net_device *dev, int offset)
385 {
386         u32 state;
387
388         /* Cf scc_writel for concern regarding thread-safety */
389         state = dpriv->scc_regs[offset >> 2];
390         state &= ~mask;
391         state |= value;
392         dpriv->scc_regs[offset >> 2] = state;
393         writel(state, dev->base_addr + SCC_REG_START(dpriv) + offset);
394 }
395
396 static void scc_writel(u32 bits, struct dscc4_dev_priv *dpriv,
397                        struct net_device *dev, int offset)
398 {
399         /*
400          * Thread-UNsafe.
401          * As of 2002/02/16, there are no thread racing for access.
402          */
403         dpriv->scc_regs[offset >> 2] = bits;
404         writel(bits, dev->base_addr + SCC_REG_START(dpriv) + offset);
405 }
406
407 static inline u32 scc_readl(struct dscc4_dev_priv *dpriv, int offset)
408 {
409         return dpriv->scc_regs[offset >> 2];
410 }
411
412 static u32 scc_readl_star(struct dscc4_dev_priv *dpriv, struct net_device *dev)
413 {
414         /* Cf errata DS5 p.4 */
415         readl(dev->base_addr + SCC_REG_START(dpriv) + STAR);
416         return readl(dev->base_addr + SCC_REG_START(dpriv) + STAR);
417 }
418
419 static inline void dscc4_do_tx(struct dscc4_dev_priv *dpriv,
420                                struct net_device *dev)
421 {
422         dpriv->ltda = dpriv->tx_fd_dma +
423                       ((dpriv->tx_current-1)%TX_RING_SIZE)*sizeof(struct TxFD);
424         writel(dpriv->ltda, dev->base_addr + CH0LTDA + dpriv->dev_id*4);
425         /* Flush posted writes *NOW* */
426         readl(dev->base_addr + CH0LTDA + dpriv->dev_id*4);
427 }
428
429 static inline void dscc4_rx_update(struct dscc4_dev_priv *dpriv,
430                                    struct net_device *dev)
431 {
432         dpriv->lrda = dpriv->rx_fd_dma +
433                       ((dpriv->rx_dirty - 1)%RX_RING_SIZE)*sizeof(struct RxFD);
434         writel(dpriv->lrda, dev->base_addr + CH0LRDA + dpriv->dev_id*4);
435 }
436
437 static inline unsigned int dscc4_tx_done(struct dscc4_dev_priv *dpriv)
438 {
439         return dpriv->tx_current == dpriv->tx_dirty;
440 }
441
442 static inline unsigned int dscc4_tx_quiescent(struct dscc4_dev_priv *dpriv,
443                                               struct net_device *dev)
444 {
445         return readl(dev->base_addr + CH0FTDA + dpriv->dev_id*4) == dpriv->ltda;
446 }
447
448 int state_check(u32 state, struct dscc4_dev_priv *dpriv, struct net_device *dev,
449                 const char *msg)
450 {
451         int ret = 0;
452
453         if (debug > 1) {
454         if (SOURCE_ID(state) != dpriv->dev_id) {
455                 printk(KERN_DEBUG "%s (%s): Source Id=%d, state=%08x\n",
456                        dev->name, msg, SOURCE_ID(state), state );
457                         ret = -1;
458         }
459         if (state & 0x0df80c00) {
460                 printk(KERN_DEBUG "%s (%s): state=%08x (UFO alert)\n",
461                        dev->name, msg, state);
462                         ret = -1;
463         }
464         }
465         return ret;
466 }
467
468 void dscc4_tx_print(struct net_device *dev, struct dscc4_dev_priv *dpriv,
469                     char *msg)
470 {
471         printk(KERN_DEBUG "%s: tx_current=%02d tx_dirty=%02d (%s)\n",
472                dev->name, dpriv->tx_current, dpriv->tx_dirty, msg);
473 }
474
475 static void dscc4_release_ring(struct dscc4_dev_priv *dpriv)
476 {
477         struct pci_dev *pdev = dpriv->pci_priv->pdev;
478         struct TxFD *tx_fd = dpriv->tx_fd;
479         struct RxFD *rx_fd = dpriv->rx_fd;
480         struct sk_buff **skbuff;
481         int i;
482
483         pci_free_consistent(pdev, TX_TOTAL_SIZE, tx_fd, dpriv->tx_fd_dma);
484         pci_free_consistent(pdev, RX_TOTAL_SIZE, rx_fd, dpriv->rx_fd_dma);
485
486         skbuff = dpriv->tx_skbuff;
487         for (i = 0; i < TX_RING_SIZE; i++) {
488                 if (*skbuff) {
489                         pci_unmap_single(pdev, tx_fd->data, (*skbuff)->len,
490                                 PCI_DMA_TODEVICE);
491                         dev_kfree_skb(*skbuff);
492                 }
493                 skbuff++;
494                 tx_fd++;
495         }
496
497         skbuff = dpriv->rx_skbuff;
498         for (i = 0; i < RX_RING_SIZE; i++) {
499                 if (*skbuff) {
500                         pci_unmap_single(pdev, rx_fd->data,
501                                 RX_MAX(HDLC_MAX_MRU), PCI_DMA_FROMDEVICE);
502                         dev_kfree_skb(*skbuff);
503                 }
504                 skbuff++;
505                 rx_fd++;
506         }
507 }
508
509 inline int try_get_rx_skb(struct dscc4_dev_priv *dpriv, struct net_device *dev)
510 {
511         unsigned int dirty = dpriv->rx_dirty%RX_RING_SIZE;
512         struct RxFD *rx_fd = dpriv->rx_fd + dirty;
513         const int len = RX_MAX(HDLC_MAX_MRU);
514         struct sk_buff *skb;
515         int ret = 0;
516
517         skb = dev_alloc_skb(len);
518         dpriv->rx_skbuff[dirty] = skb;
519         if (skb) {
520                 skb->dev = dev;
521                 skb->protocol = hdlc_type_trans(skb, dev);
522                 skb->mac.raw = skb->data;
523                 rx_fd->data = pci_map_single(dpriv->pci_priv->pdev, skb->data,
524                                              len, PCI_DMA_FROMDEVICE);
525         } else {
526                 rx_fd->data = (u32) NULL;
527                 ret = -1;
528         }
529         return ret;
530 }
531
532 /*
533  * IRQ/thread/whatever safe
534  */
535 static int dscc4_wait_ack_cec(struct dscc4_dev_priv *dpriv,
536                               struct net_device *dev, char *msg)
537 {
538         s8 i = 0;
539
540         do {
541                 if (!(scc_readl_star(dpriv, dev) & SccBusy)) {
542                         printk(KERN_DEBUG "%s: %s ack (%d try)\n", dev->name,
543                                msg, i);
544                         goto done;
545                 }
546                 set_current_state(TASK_UNINTERRUPTIBLE);
547                 schedule_timeout(10);
548                 rmb();
549         } while (++i > 0);
550         printk(KERN_ERR "%s: %s timeout\n", dev->name, msg);
551 done:
552         return (i >= 0) ? i : -EAGAIN;
553 }
554
555 static int dscc4_do_action(struct net_device *dev, char *msg)
556 {
557         unsigned long ioaddr = dev->base_addr;
558         s16 i = 0;
559
560         writel(Action, ioaddr + GCMDR);
561         ioaddr += GSTAR;
562         do {
563                 u32 state = readl(ioaddr);
564
565                 if (state & ArAck) {
566                         printk(KERN_DEBUG "%s: %s ack\n", dev->name, msg);
567                         writel(ArAck, ioaddr);
568                         goto done;
569                 } else if (state & Arf) {
570                         printk(KERN_ERR "%s: %s failed\n", dev->name, msg);
571                         writel(Arf, ioaddr);
572                         i = -1;
573                         goto done;
574         }
575                 rmb();
576         } while (++i > 0);
577         printk(KERN_ERR "%s: %s timeout\n", dev->name, msg);
578 done:
579         return i;
580 }
581
582 static inline int dscc4_xpr_ack(struct dscc4_dev_priv *dpriv)
583 {
584         int cur = dpriv->iqtx_current%IRQ_RING_SIZE;
585         s8 i = 0;
586
587         do {
588                 if (!(dpriv->flags & (NeedIDR | NeedIDT)) ||
589                     (dpriv->iqtx[cur] & Xpr))
590                         break;
591                 smp_rmb();
592                 set_current_state(TASK_UNINTERRUPTIBLE);
593                 schedule_timeout(10);
594         } while (++i > 0);
595
596         return (i >= 0 ) ? i : -EAGAIN;
597 }
598
599 #if 0 /* dscc4_{rx/tx}_reset are both unreliable - more tweak needed */
600 static void dscc4_rx_reset(struct dscc4_dev_priv *dpriv, struct net_device *dev)
601 {
602         unsigned long flags;
603
604         spin_lock_irqsave(&dpriv->pci_priv->lock, flags);
605         /* Cf errata DS5 p.6 */
606         writel(0x00000000, dev->base_addr + CH0LRDA + dpriv->dev_id*4);
607         scc_patchl(PowerUp, 0, dpriv, dev, CCR0);
608         readl(dev->base_addr + CH0LRDA + dpriv->dev_id*4);
609         writel(MTFi|Rdr, dev->base_addr + dpriv->dev_id*0x0c + CH0CFG);
610         writel(Action, dev->base_addr + GCMDR);
611         spin_unlock_irqrestore(&dpriv->pci_priv->lock, flags);
612 }
613
614 #endif
615
616 #if 0
617 static void dscc4_tx_reset(struct dscc4_dev_priv *dpriv, struct net_device *dev)
618 {
619         u16 i = 0;
620
621         /* Cf errata DS5 p.7 */
622         scc_patchl(PowerUp, 0, dpriv, dev, CCR0);
623         scc_writel(0x00050000, dpriv, dev, CCR2);
624         /*
625          * Must be longer than the time required to fill the fifo.
626          */
627         while (!dscc4_tx_quiescent(dpriv, dev) && ++i) {
628                 udelay(1);
629                 wmb();
630         }
631
632         writel(MTFi|Rdt, dev->base_addr + dpriv->dev_id*0x0c + CH0CFG);
633         if (dscc4_do_action(dev, "Rdt") < 0)
634                 printk(KERN_ERR "%s: Tx reset failed\n", dev->name);
635 }
636 #endif
637
638 /* TODO: (ab)use this function to refill a completely depleted RX ring. */
639 static inline void dscc4_rx_skb(struct dscc4_dev_priv *dpriv,
640                                 struct net_device *dev)
641 {
642         struct RxFD *rx_fd = dpriv->rx_fd + dpriv->rx_current%RX_RING_SIZE;
643         struct net_device_stats *stats = hdlc_stats(dev);
644         struct pci_dev *pdev = dpriv->pci_priv->pdev;
645         struct sk_buff *skb;
646         int pkt_len;
647
648         skb = dpriv->rx_skbuff[dpriv->rx_current++%RX_RING_SIZE];
649         if (!skb) {
650                 printk(KERN_DEBUG "%s: skb=0 (%s)\n", dev->name, __FUNCTION__);
651                 goto refill;
652         }
653         pkt_len = TO_SIZE(rx_fd->state2);
654         pci_unmap_single(pdev, rx_fd->data, RX_MAX(HDLC_MAX_MRU), PCI_DMA_FROMDEVICE);
655         if ((skb->data[--pkt_len] & FrameOk) == FrameOk) {
656                 stats->rx_packets++;
657                 stats->rx_bytes += pkt_len;
658                 skb_put(skb, pkt_len);
659                 if (netif_running(dev))
660                         skb->protocol = hdlc_type_trans(skb, dev);
661                 skb->dev->last_rx = jiffies;
662                 netif_rx(skb);
663         } else {
664                 if (skb->data[pkt_len] & FrameRdo)
665                         stats->rx_fifo_errors++;
666                 else if (!(skb->data[pkt_len] | ~FrameCrc))
667                         stats->rx_crc_errors++;
668                 else if (!(skb->data[pkt_len] | ~(FrameVfr | FrameRab)))
669                         stats->rx_length_errors++;
670                 else
671                         stats->rx_errors++;
672                 dev_kfree_skb_irq(skb);
673         }
674 refill:
675         while ((dpriv->rx_dirty - dpriv->rx_current) % RX_RING_SIZE) {
676                 if (try_get_rx_skb(dpriv, dev) < 0)
677                         break;
678                 dpriv->rx_dirty++;
679         }
680         dscc4_rx_update(dpriv, dev);
681         rx_fd->state2 = 0x00000000;
682         rx_fd->end = 0xbabeface;
683 }
684
685 static void dscc4_free1(struct pci_dev *pdev)
686 {
687         struct dscc4_pci_priv *ppriv;
688         struct dscc4_dev_priv *root;
689         int i;
690
691         ppriv = pci_get_drvdata(pdev);
692         root = ppriv->root;
693
694         for (i = 0; i < dev_per_card; i++)
695                 unregister_hdlc_device(dscc4_to_dev(&root[i]));
696
697         pci_set_drvdata(pdev, NULL);
698
699         for (i = 0; i < dev_per_card; i++)
700                 free_netdev(root[i].dev);
701         kfree(root);
702         kfree(ppriv);
703 }
704
705 static int __devinit dscc4_init_one(struct pci_dev *pdev,
706                                   const struct pci_device_id *ent)
707 {
708         struct dscc4_pci_priv *priv;
709         struct dscc4_dev_priv *dpriv;
710         static int cards_found = 0;
711         unsigned long ioaddr;
712         int i;
713
714         printk(KERN_DEBUG "%s", version);
715
716         if (pci_enable_device(pdev))
717                 goto err_out;
718         if (!request_mem_region(pci_resource_start(pdev, 0),
719                                 pci_resource_len(pdev, 0), "registers")) {
720                 printk(KERN_ERR "%s: can't reserve MMIO region (regs)\n",
721                         DRV_NAME);
722                 goto err_out;
723         }
724         if (!request_mem_region(pci_resource_start(pdev, 1),
725                                 pci_resource_len(pdev, 1), "LBI interface")) {
726                 printk(KERN_ERR "%s: can't reserve MMIO region (lbi)\n",
727                         DRV_NAME);
728                 goto err_out_free_mmio_region0;
729         }
730         ioaddr = (unsigned long)ioremap(pci_resource_start(pdev, 0),
731                                         pci_resource_len(pdev, 0));
732         if (!ioaddr) {
733                 printk(KERN_ERR "%s: cannot remap MMIO region %lx @ %lx\n",
734                         DRV_NAME, pci_resource_len(pdev, 0),
735                         pci_resource_start(pdev, 0));
736                 goto err_out_free_mmio_region;
737         }
738         printk(KERN_DEBUG "Siemens DSCC4, MMIO at %#lx (regs), %#lx (lbi), IRQ %d\n",
739                 pci_resource_start(pdev, 0),
740                 pci_resource_start(pdev, 1), pdev->irq);
741
742         /* Cf errata DS5 p.2 */
743         pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xf8);
744         pci_set_master(pdev);
745
746         if (dscc4_found1(pdev, ioaddr))
747                 goto err_out_iounmap;
748
749         priv = (struct dscc4_pci_priv *)pci_get_drvdata(pdev);
750
751         if (request_irq(pdev->irq, &dscc4_irq, SA_SHIRQ, DRV_NAME, priv->root)){
752                 printk(KERN_WARNING "%s: IRQ %d busy\n", DRV_NAME, pdev->irq);
753                 goto err_out_free1;
754         }
755
756         /* power up/little endian/dma core controlled via lrda/ltda */
757         writel(0x00000001, ioaddr + GMODE);
758         /* Shared interrupt queue */
759         {
760                 u32 bits;
761
762                 bits = (IRQ_RING_SIZE >> 5) - 1;
763                 bits |= bits << 4;
764                 bits |= bits << 8;
765                 bits |= bits << 16;
766                 writel(bits, ioaddr + IQLENR0);
767         }
768         /* Global interrupt queue */
769         writel((u32)(((IRQ_RING_SIZE >> 5) - 1) << 20), ioaddr + IQLENR1);
770         priv->iqcfg = (u32 *) pci_alloc_consistent(pdev,
771                 IRQ_RING_SIZE*sizeof(u32), &priv->iqcfg_dma);
772         if (!priv->iqcfg)
773                 goto err_out_free_irq;
774         writel(priv->iqcfg_dma, ioaddr + IQCFG);
775
776         /*
777          * SCC 0-3 private rx/tx irq structures
778          * IQRX/TXi needs to be set soon. Learned it the hard way...
779          */
780         for (i = 0; i < dev_per_card; i++) {
781                 dpriv = priv->root + i;
782                 dpriv->iqtx = (u32 *) pci_alloc_consistent(pdev,
783                         IRQ_RING_SIZE*sizeof(u32), &dpriv->iqtx_dma);
784                 if (!dpriv->iqtx)
785                         goto err_out_free_iqtx;
786                 writel(dpriv->iqtx_dma, ioaddr + IQTX0 + i*4);
787         }
788         for (i = 0; i < dev_per_card; i++) {
789                 dpriv = priv->root + i;
790                 dpriv->iqrx = (u32 *) pci_alloc_consistent(pdev,
791                         IRQ_RING_SIZE*sizeof(u32), &dpriv->iqrx_dma);
792                 if (!dpriv->iqrx)
793                         goto err_out_free_iqrx;
794                 writel(dpriv->iqrx_dma, ioaddr + IQRX0 + i*4);
795         }
796
797         /* Cf application hint. Beware of hard-lock condition on threshold. */
798         writel(0x42104000, ioaddr + FIFOCR1);
799         //writel(0x9ce69800, ioaddr + FIFOCR2);
800         writel(0xdef6d800, ioaddr + FIFOCR2);
801         //writel(0x11111111, ioaddr + FIFOCR4);
802         writel(0x18181818, ioaddr + FIFOCR4);
803         // FIXME: should depend on the chipset revision
804         writel(0x0000000e, ioaddr + FIFOCR3);
805
806         writel(0xff200001, ioaddr + GCMDR);
807
808         cards_found++;
809         return 0;
810
811 err_out_free_iqrx:
812         while (--i >= 0) {
813                 dpriv = priv->root + i;
814                 pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
815                                     dpriv->iqrx, dpriv->iqrx_dma);
816         }
817         i = dev_per_card;
818 err_out_free_iqtx:
819         while (--i >= 0) {
820                 dpriv = priv->root + i;
821                 pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
822                                     dpriv->iqtx, dpriv->iqtx_dma);
823         }
824         pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32), priv->iqcfg,
825                             priv->iqcfg_dma);
826 err_out_free_irq:
827         free_irq(pdev->irq, priv->root);
828 err_out_free1:
829         dscc4_free1(pdev);
830 err_out_iounmap:
831         iounmap ((void *)ioaddr);
832 err_out_free_mmio_region:
833         release_mem_region(pci_resource_start(pdev, 1),
834                            pci_resource_len(pdev, 1));
835 err_out_free_mmio_region0:
836         release_mem_region(pci_resource_start(pdev, 0),
837                            pci_resource_len(pdev, 0));
838 err_out:
839         return -ENODEV;
840 };
841
842 /*
843  * Let's hope the default values are decent enough to protect my
844  * feet from the user's gun - Ueimor
845  */
846 static void dscc4_init_registers(struct dscc4_dev_priv *dpriv,
847                                  struct net_device *dev)
848 {
849         /* No interrupts, SCC core disabled. Let's relax */
850         scc_writel(0x00000000, dpriv, dev, CCR0);
851
852         scc_writel(LengthCheck | (HDLC_MAX_MRU >> 5), dpriv, dev, RLCR);
853
854         /*
855          * No address recognition/crc-CCITT/cts enabled
856          * Shared flags transmission disabled - cf errata DS5 p.11
857          * Carrier detect disabled - cf errata p.14
858          * FIXME: carrier detection/polarity may be handled more gracefully.
859          */
860         scc_writel(0x02408000, dpriv, dev, CCR1);
861
862         /* crc not forwarded - Cf errata DS5 p.11 */
863         scc_writel(0x00050008 & ~RxActivate, dpriv, dev, CCR2);
864         // crc forwarded
865         //scc_writel(0x00250008 & ~RxActivate, dpriv, dev, CCR2);
866 }
867
868 static inline int dscc4_set_quartz(struct dscc4_dev_priv *dpriv, int hz)
869 {
870         int ret = 0;
871
872         if ((hz < 0) || (hz > DSCC4_HZ_MAX))
873                 ret = -EOPNOTSUPP;
874         else
875                 dpriv->pci_priv->xtal_hz = hz;
876
877         return ret;
878 }
879
880 static int dscc4_found1(struct pci_dev *pdev, unsigned long ioaddr)
881 {
882         struct dscc4_pci_priv *ppriv;
883         struct dscc4_dev_priv *root;
884         int i, ret = -ENOMEM;
885
886         root = (struct dscc4_dev_priv *)
887                 kmalloc(dev_per_card*sizeof(*root), GFP_KERNEL);
888         if (!root) {
889                 printk(KERN_ERR "%s: can't allocate data\n", DRV_NAME);
890                 goto err_out;
891         }
892         memset(root, 0, dev_per_card*sizeof(*root));
893
894         for (i = 0; i < dev_per_card; i++) {
895                 root[i].dev = alloc_hdlcdev(root + i);
896                 if (!root[i].dev) {
897                         while (i--)
898                                 free_netdev(root[i].dev);
899                         goto err_free_dev;
900                 }
901         }
902
903         ppriv = (struct dscc4_pci_priv *) kmalloc(sizeof(*ppriv), GFP_KERNEL);
904         if (!ppriv) {
905                 printk(KERN_ERR "%s: can't allocate private data\n", DRV_NAME);
906                 goto err_free_dev2;
907         }
908         memset(ppriv, 0, sizeof(struct dscc4_pci_priv));
909         ret = dscc4_set_quartz(root, quartz);
910         if (ret < 0)
911                 goto err_free_priv;
912         ppriv->root = root;
913         spin_lock_init(&ppriv->lock);
914
915         for (i = 0; i < dev_per_card; i++) {
916                 struct dscc4_dev_priv *dpriv = root + i;
917                 struct net_device *d = dscc4_to_dev(dpriv);
918                 hdlc_device *hdlc = dev_to_hdlc(d);
919
920                 d->base_addr = ioaddr;
921                 d->init = NULL;
922                 d->irq = pdev->irq;
923                 d->open = dscc4_open;
924                 d->stop = dscc4_close;
925                 d->set_multicast_list = NULL;
926                 d->do_ioctl = dscc4_ioctl;
927                 d->tx_timeout = dscc4_tx_timeout;
928                 d->watchdog_timeo = TX_TIMEOUT;
929                 SET_MODULE_OWNER(d);
930                 SET_NETDEV_DEV(d, &pdev->dev);
931
932                 dpriv->dev_id = i;
933                 dpriv->pci_priv = ppriv;
934                 spin_lock_init(&dpriv->lock);
935
936                 hdlc->xmit = dscc4_start_xmit;
937                 hdlc->attach = dscc4_hdlc_attach;
938
939                 dscc4_init_registers(dpriv, d);
940                 dpriv->parity = PARITY_CRC16_PR0_CCITT;
941                 dpriv->encoding = ENCODING_NRZ;
942         
943                 ret = dscc4_init_ring(d);
944                 if (ret < 0)
945                         goto err_unregister;
946
947                 ret = register_hdlc_device(d);
948                 if (ret < 0) {
949                         printk(KERN_ERR "%s: unable to register\n", DRV_NAME);
950                         dscc4_release_ring(dpriv);
951                         goto err_unregister;
952                 }
953         }
954         pci_set_drvdata(pdev, ppriv);
955         return ret;
956
957 err_unregister:
958         while (--i >= 0) {
959                 dscc4_release_ring(root + i);
960                 unregister_hdlc_device(dscc4_to_dev(&root[i]));
961         }
962 err_free_priv:
963         kfree(ppriv);
964 err_free_dev2:
965         for (i = 0; i < dev_per_card; i++)
966                 free_netdev(root[i].dev);
967 err_free_dev:
968         kfree(root);
969 err_out:
970         return ret;
971 };
972
973 /* FIXME: get rid of the unneeded code */
974 static void dscc4_timer(unsigned long data)
975 {
976         struct net_device *dev = (struct net_device *)data;
977         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
978 //      struct dscc4_pci_priv *ppriv;
979
980         goto done;
981 done:
982         dpriv->timer.expires = jiffies + TX_TIMEOUT;
983         add_timer(&dpriv->timer);
984 }
985
986 static void dscc4_tx_timeout(struct net_device *dev)
987 {
988         /* FIXME: something is missing there */
989 }
990
991 static int dscc4_loopback_check(struct dscc4_dev_priv *dpriv)
992 {
993         sync_serial_settings *settings = &dpriv->settings;
994
995         if (settings->loopback && (settings->clock_type != CLOCK_INT)) {
996                 struct net_device *dev = dscc4_to_dev(dpriv);
997
998                 printk(KERN_INFO "%s: loopback requires clock\n", dev->name);
999                 return -1;
1000         }
1001         return 0;
1002 }
1003
1004 #ifdef CONFIG_DSCC4_PCI_RST
1005 /*
1006  * Some DSCC4-based cards wires the GPIO port and the PCI #RST pin together
1007  * so as to provide a safe way to reset the asic while not the whole machine
1008  * rebooting.
1009  *
1010  * This code doesn't need to be efficient. Keep It Simple
1011  */
1012 static void dscc4_pci_reset(struct pci_dev *pdev, unsigned long ioaddr)
1013 {
1014         int i;
1015
1016         down(&dscc4_sem);
1017         for (i = 0; i < 16; i++)
1018                 pci_read_config_dword(pdev, i << 2, dscc4_pci_config_store + i);
1019
1020         /* Maximal LBI clock divider (who cares ?) and whole GPIO range. */
1021         writel(0x001c0000, ioaddr + GMODE);
1022         /* Configure GPIO port as output */
1023         writel(0x0000ffff, ioaddr + GPDIR);
1024         /* Disable interruption */
1025         writel(0x0000ffff, ioaddr + GPIM);
1026
1027         writel(0x0000ffff, ioaddr + GPDATA);
1028         writel(0x00000000, ioaddr + GPDATA);
1029
1030         /* Flush posted writes */
1031         readl(ioaddr + GSTAR);
1032
1033         set_current_state(TASK_UNINTERRUPTIBLE);
1034         schedule_timeout(10);
1035
1036         for (i = 0; i < 16; i++)
1037                 pci_write_config_dword(pdev, i << 2, dscc4_pci_config_store[i]);
1038         up(&dscc4_sem);
1039 }
1040 #else
1041 #define dscc4_pci_reset(pdev,ioaddr)    do {} while (0)
1042 #endif /* CONFIG_DSCC4_PCI_RST */
1043
1044 static int dscc4_open(struct net_device *dev)
1045 {
1046         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
1047         struct dscc4_pci_priv *ppriv;
1048         int ret = -EAGAIN;
1049
1050         if ((dscc4_loopback_check(dpriv) < 0) || !dev->hard_start_xmit)
1051                 goto err;
1052
1053         if ((ret = hdlc_open(dev)))
1054                 goto err;
1055
1056         ppriv = dpriv->pci_priv;
1057
1058         /*
1059          * Due to various bugs, there is no way to reliably reset a
1060          * specific port (manufacturer's dependant special PCI #RST wiring
1061          * apart: it affects all ports). Thus the device goes in the best
1062          * silent mode possible at dscc4_close() time and simply claims to
1063          * be up if it's opened again. It still isn't possible to change
1064          * the HDLC configuration without rebooting but at least the ports
1065          * can be up/down ifconfig'ed without killing the host.
1066          */
1067         if (dpriv->flags & FakeReset) {
1068                 dpriv->flags &= ~FakeReset;
1069                 scc_patchl(0, PowerUp, dpriv, dev, CCR0);
1070                 scc_patchl(0, 0x00050000, dpriv, dev, CCR2);
1071                 scc_writel(EventsMask, dpriv, dev, IMR);
1072                 printk(KERN_INFO "%s: up again.\n", dev->name);
1073                 goto done;
1074         }
1075
1076         /* IDT+IDR during XPR */
1077         dpriv->flags = NeedIDR | NeedIDT;
1078
1079         scc_patchl(0, PowerUp | Vis, dpriv, dev, CCR0);
1080
1081         /*
1082          * The following is a bit paranoid...
1083          *
1084          * NB: the datasheet "...CEC will stay active if the SCC is in
1085          * power-down mode or..." and CCR2.RAC = 1 are two different
1086          * situations.
1087          */
1088         if (scc_readl_star(dpriv, dev) & SccBusy) {
1089                 printk(KERN_ERR "%s busy. Try later\n", dev->name);
1090                 ret = -EAGAIN;
1091                 goto err_out;
1092         } else
1093                 printk(KERN_INFO "%s: available. Good\n", dev->name);
1094
1095         scc_writel(EventsMask, dpriv, dev, IMR);
1096
1097         /* Posted write is flushed in the wait_ack loop */
1098         scc_writel(TxSccRes | RxSccRes, dpriv, dev, CMDR);
1099
1100         if ((ret = dscc4_wait_ack_cec(dpriv, dev, "Cec")) < 0)
1101                 goto err_disable_scc_events;
1102
1103         /*
1104          * I would expect XPR near CE completion (before ? after ?).
1105          * At worst, this code won't see a late XPR and people
1106          * will have to re-issue an ifconfig (this is harmless).
1107          * WARNING, a really missing XPR usually means a hardware
1108          * reset is needed. Suggestions anyone ?
1109          */
1110         if ((ret = dscc4_xpr_ack(dpriv)) < 0) {
1111                 printk(KERN_ERR "%s: %s timeout\n", DRV_NAME, "XPR");
1112                 goto err_disable_scc_events;
1113         }
1114         
1115         if (debug > 2)
1116                 dscc4_tx_print(dev, dpriv, "Open");
1117
1118 done:
1119         netif_start_queue(dev);
1120
1121         init_timer(&dpriv->timer);
1122         dpriv->timer.expires = jiffies + 10*HZ;
1123         dpriv->timer.data = (unsigned long)dev;
1124         dpriv->timer.function = &dscc4_timer;
1125         add_timer(&dpriv->timer);
1126         netif_carrier_on(dev);
1127
1128         return 0;
1129
1130 err_disable_scc_events:
1131         scc_writel(0xffffffff, dpriv, dev, IMR);
1132         scc_patchl(PowerUp | Vis, 0, dpriv, dev, CCR0);
1133 err_out:
1134         hdlc_close(dev);
1135 err:
1136         return ret;
1137 }
1138
1139 #ifdef DSCC4_POLLING
1140 static int dscc4_tx_poll(struct dscc4_dev_priv *dpriv, struct net_device *dev)
1141 {
1142         /* FIXME: it's gonna be easy (TM), for sure */
1143 }
1144 #endif /* DSCC4_POLLING */
1145
1146 static int dscc4_start_xmit(struct sk_buff *skb, struct net_device *dev)
1147 {
1148         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
1149         struct dscc4_pci_priv *ppriv = dpriv->pci_priv;
1150         struct TxFD *tx_fd;
1151         int next;
1152
1153         next = dpriv->tx_current%TX_RING_SIZE;
1154         dpriv->tx_skbuff[next] = skb;
1155         tx_fd = dpriv->tx_fd + next;
1156         tx_fd->state = FrameEnd | TO_STATE_TX(skb->len);
1157         tx_fd->data = pci_map_single(ppriv->pdev, skb->data, skb->len,
1158                                      PCI_DMA_TODEVICE);
1159         tx_fd->complete = 0x00000000;
1160         tx_fd->jiffies = jiffies;
1161         mb();
1162
1163 #ifdef DSCC4_POLLING
1164         spin_lock(&dpriv->lock);
1165         while (dscc4_tx_poll(dpriv, dev));
1166         spin_unlock(&dpriv->lock);
1167 #endif
1168
1169         dev->trans_start = jiffies;
1170
1171         if (debug > 2)
1172                 dscc4_tx_print(dev, dpriv, "Xmit");
1173         /* To be cleaned(unsigned int)/optimized. Later, ok ? */
1174         if (!((++dpriv->tx_current - dpriv->tx_dirty)%TX_RING_SIZE))
1175                 netif_stop_queue(dev);
1176
1177         if (dscc4_tx_quiescent(dpriv, dev))
1178                 dscc4_do_tx(dpriv, dev);
1179
1180         return 0;
1181 }
1182
1183 static int dscc4_close(struct net_device *dev)
1184 {
1185         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
1186
1187         del_timer_sync(&dpriv->timer);
1188         netif_stop_queue(dev);
1189
1190         scc_patchl(PowerUp | Vis, 0, dpriv, dev, CCR0);
1191         scc_patchl(0x00050000, 0, dpriv, dev, CCR2);
1192         scc_writel(0xffffffff, dpriv, dev, IMR);
1193
1194         dpriv->flags |= FakeReset;
1195
1196         hdlc_close(dev);
1197
1198         return 0;
1199 }
1200
1201 static inline int dscc4_check_clock_ability(int port)
1202 {
1203         int ret = 0;
1204
1205 #ifdef CONFIG_DSCC4_PCISYNC
1206         if (port >= 2)
1207                 ret = -1;
1208 #endif
1209         return ret;
1210 }
1211
1212 /*
1213  * DS1 p.137: "There are a total of 13 different clocking modes..."
1214  *                                  ^^
1215  * Design choices:
1216  * - by default, assume a clock is provided on pin RxClk/TxClk (clock mode 0a).
1217  *   Clock mode 3b _should_ work but the testing seems to make this point
1218  *   dubious (DIY testing requires setting CCR0 at 0x00000033).
1219  *   This is supposed to provide least surprise "DTE like" behavior.
1220  * - if line rate is specified, clocks are assumed to be locally generated.
1221  *   A quartz must be available (on pin XTAL1). Modes 6b/7b are used. Choosing
1222  *   between these it automagically done according on the required frequency
1223  *   scaling. Of course some rounding may take place.
1224  * - no high speed mode (40Mb/s). May be trivial to do but I don't have an
1225  *   appropriate external clocking device for testing.
1226  * - no time-slot/clock mode 5: shameless lazyness.
1227  *
1228  * The clock signals wiring can be (is ?) manufacturer dependant. Good luck.
1229  *
1230  * BIG FAT WARNING: if the device isn't provided enough clocking signal, it
1231  * won't pass the init sequence. For example, straight back-to-back DTE without
1232  * external clock will fail when dscc4_open() (<- 'ifconfig hdlcx xxx') is
1233  * called.
1234  *
1235  * Typos lurk in datasheet (missing divier in clock mode 7a figure 51 p.153
1236  * DS0 for example)
1237  *
1238  * Clock mode related bits of CCR0:
1239  *     +------------ TOE: output TxClk (0b/2b/3a/3b/6b/7a/7b only)
1240  *     | +---------- SSEL: sub-mode select 0 -> a, 1 -> b
1241  *     | | +-------- High Speed: say 0
1242  *     | | | +-+-+-- Clock Mode: 0..7
1243  *     | | | | | |
1244  * -+-+-+-+-+-+-+-+
1245  * x|x|5|4|3|2|1|0| lower bits
1246  *
1247  * Division factor of BRR: k = (N+1)x2^M (total divider = 16xk in mode 6b)
1248  *            +-+-+-+------------------ M (0..15)
1249  *            | | | |     +-+-+-+-+-+-- N (0..63)
1250  *    0 0 0 0 | | | | 0 0 | | | | | |
1251  * ...-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1252  *    f|e|d|c|b|a|9|8|7|6|5|4|3|2|1|0| lower bits
1253  *
1254  */
1255 static int dscc4_set_clock(struct net_device *dev, u32 *bps, u32 *state)
1256 {
1257         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
1258         int ret = -1;
1259         u32 brr;
1260
1261         *state &= ~Ccr0ClockMask;
1262         if (*bps) { /* Clock generated - required for DCE */
1263                 u32 n = 0, m = 0, divider;
1264                 int xtal;
1265
1266                 xtal = dpriv->pci_priv->xtal_hz;
1267                 if (!xtal)
1268                         goto done;
1269                 if (dscc4_check_clock_ability(dpriv->dev_id) < 0)
1270                         goto done;
1271                 divider = xtal / *bps;
1272                 if (divider > BRR_DIVIDER_MAX) {
1273                         divider >>= 4;
1274                         *state |= 0x00000036; /* Clock mode 6b (BRG/16) */
1275                 } else
1276                         *state |= 0x00000037; /* Clock mode 7b (BRG) */
1277                 if (divider >> 22) {
1278                         n = 63;
1279                         m = 15;
1280                 } else if (divider) {
1281                         /* Extraction of the 6 highest weighted bits */
1282                         m = 0;
1283                         while (0xffffffc0 & divider) {
1284                                 m++;
1285                                 divider >>= 1;
1286                         }
1287                         n = divider;
1288                 }
1289                 brr = (m << 8) | n;
1290                 divider = n << m;
1291                 if (!(*state & 0x00000001)) /* ?b mode mask => clock mode 6b */
1292                         divider <<= 4;
1293                 *bps = xtal / divider;
1294         } else {
1295                 /*
1296                  * External clock - DTE
1297                  * "state" already reflects Clock mode 0a (CCR0 = 0xzzzzzz00).
1298                  * Nothing more to be done
1299                  */
1300                 brr = 0;
1301         }
1302         scc_writel(brr, dpriv, dev, BRR);
1303         ret = 0;
1304 done:
1305         return ret;
1306 }
1307
1308 static int dscc4_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1309 {
1310         sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
1311         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
1312         const size_t size = sizeof(dpriv->settings);
1313         int ret = 0;
1314
1315         if (dev->flags & IFF_UP)
1316                 return -EBUSY;
1317
1318         if (cmd != SIOCWANDEV)
1319                 return -EOPNOTSUPP;
1320
1321         switch(ifr->ifr_settings.type) {
1322         case IF_GET_IFACE:
1323                 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1324                 if (ifr->ifr_settings.size < size) {
1325                         ifr->ifr_settings.size = size; /* data size wanted */
1326                         return -ENOBUFS;
1327                 }
1328                 if (copy_to_user(line, &dpriv->settings, size))
1329                         return -EFAULT;
1330                 break;
1331
1332         case IF_IFACE_SYNC_SERIAL:
1333                 if (!capable(CAP_NET_ADMIN))
1334                         return -EPERM;
1335
1336                 if (dpriv->flags & FakeReset) {
1337                         printk(KERN_INFO "%s: please reset the device"
1338                                " before this command\n", dev->name);
1339                         return -EPERM;
1340                 }
1341                 if (copy_from_user(&dpriv->settings, line, size))
1342                         return -EFAULT;
1343                 ret = dscc4_set_iface(dpriv, dev);
1344                 break;
1345
1346         default:
1347                 ret = hdlc_ioctl(dev, ifr, cmd);
1348                 break;
1349         }
1350
1351         return ret;
1352 }
1353
1354 static int dscc4_match(struct thingie *p, int value)
1355 {
1356         int i;
1357
1358         for (i = 0; p[i].define != -1; i++) {
1359                 if (value == p[i].define)
1360                         break;
1361         }
1362         if (p[i].define == -1)
1363                 return -1;
1364         else
1365                 return i;
1366 }
1367
1368 static int dscc4_clock_setting(struct dscc4_dev_priv *dpriv,
1369                                struct net_device *dev)
1370 {
1371         sync_serial_settings *settings = &dpriv->settings;
1372         int ret = -EOPNOTSUPP;
1373         u32 bps, state;
1374
1375         bps = settings->clock_rate;
1376         state = scc_readl(dpriv, CCR0);
1377         if (dscc4_set_clock(dev, &bps, &state) < 0)
1378                 goto done;
1379         if (bps) { /* DCE */
1380                 printk(KERN_DEBUG "%s: generated RxClk (DCE)\n", dev->name);
1381                 if (settings->clock_rate != bps) {
1382                         printk(KERN_DEBUG "%s: clock adjusted (%08d -> %08d)\n",
1383                                 dev->name, settings->clock_rate, bps);
1384                         settings->clock_rate = bps;
1385                 }
1386         } else { /* DTE */
1387                 state |= PowerUp | Vis;
1388                 printk(KERN_DEBUG "%s: external RxClk (DTE)\n", dev->name);
1389         }
1390         scc_writel(state, dpriv, dev, CCR0);
1391         ret = 0;
1392 done:
1393         return ret;
1394 }
1395
1396 static int dscc4_encoding_setting(struct dscc4_dev_priv *dpriv,
1397                                   struct net_device *dev)
1398 {
1399         struct thingie encoding[] = {
1400                 { ENCODING_NRZ,         0x00000000 },
1401                 { ENCODING_NRZI,        0x00200000 },
1402                 { ENCODING_FM_MARK,     0x00400000 },
1403                 { ENCODING_FM_SPACE,    0x00500000 },
1404                 { ENCODING_MANCHESTER,  0x00600000 },
1405                 { -1,                   0}
1406         };
1407         int i, ret = 0;
1408
1409         i = dscc4_match(encoding, dpriv->encoding);
1410         if (i >= 0)
1411                 scc_patchl(EncodingMask, encoding[i].bits, dpriv, dev, CCR0);
1412         else
1413                 ret = -EOPNOTSUPP;
1414         return ret;
1415 }
1416
1417 static int dscc4_loopback_setting(struct dscc4_dev_priv *dpriv,
1418                                   struct net_device *dev)
1419 {
1420         sync_serial_settings *settings = &dpriv->settings;
1421         u32 state;
1422
1423         state = scc_readl(dpriv, CCR1);
1424         if (settings->loopback) {
1425                 printk(KERN_DEBUG "%s: loopback\n", dev->name);
1426                 state |= 0x00000100;
1427         } else {
1428                 printk(KERN_DEBUG "%s: normal\n", dev->name);
1429                 state &= ~0x00000100;
1430         }
1431         scc_writel(state, dpriv, dev, CCR1);
1432         return 0;
1433 }
1434
1435 static int dscc4_crc_setting(struct dscc4_dev_priv *dpriv,
1436                              struct net_device *dev)
1437 {
1438         struct thingie crc[] = {
1439                 { PARITY_CRC16_PR0_CCITT,       0x00000010 },
1440                 { PARITY_CRC16_PR1_CCITT,       0x00000000 },
1441                 { PARITY_CRC32_PR0_CCITT,       0x00000011 },
1442                 { PARITY_CRC32_PR1_CCITT,       0x00000001 }
1443         };
1444         int i, ret = 0;
1445
1446         i = dscc4_match(crc, dpriv->parity);
1447         if (i >= 0)
1448                 scc_patchl(CrcMask, crc[i].bits, dpriv, dev, CCR1);
1449         else
1450                 ret = -EOPNOTSUPP;
1451         return ret;
1452 }
1453
1454 static int dscc4_set_iface(struct dscc4_dev_priv *dpriv, struct net_device *dev)
1455 {
1456         struct {
1457                 int (*action)(struct dscc4_dev_priv *, struct net_device *);
1458         } *p, do_setting[] = {
1459                 { dscc4_encoding_setting },
1460                 { dscc4_clock_setting },
1461                 { dscc4_loopback_setting },
1462                 { dscc4_crc_setting },
1463                 { NULL }
1464         };
1465         int ret = 0;
1466
1467         for (p = do_setting; p->action; p++) {
1468                 if ((ret = p->action(dpriv, dev)) < 0)
1469                         break;
1470         }
1471         return ret;
1472 }
1473
1474 static irqreturn_t dscc4_irq(int irq, void *token, struct pt_regs *ptregs)
1475 {
1476         struct dscc4_dev_priv *root = token;
1477         struct dscc4_pci_priv *priv;
1478         struct net_device *dev;
1479         unsigned long ioaddr;
1480         u32 state;
1481         unsigned long flags;
1482         int i, handled = 1;
1483
1484         priv = root->pci_priv;
1485         dev = dscc4_to_dev(root);
1486
1487         spin_lock_irqsave(&priv->lock, flags);
1488
1489         ioaddr = dev->base_addr;
1490
1491         state = readl(ioaddr + GSTAR);
1492         if (!state) {
1493                 handled = 0;
1494                 goto out;
1495         }
1496         if (debug > 3)
1497                 printk(KERN_DEBUG "%s: GSTAR = 0x%08x\n", DRV_NAME, state);
1498         writel(state, ioaddr + GSTAR);
1499
1500         if (state & Arf) {
1501                 printk(KERN_ERR "%s: failure (Arf). Harass the maintener\n",
1502                        dev->name);
1503                 goto out;
1504         }
1505         state &= ~ArAck;
1506         if (state & Cfg) {
1507                 if (debug > 0)
1508                         printk(KERN_DEBUG "%s: CfgIV\n", DRV_NAME);
1509                 if (priv->iqcfg[priv->cfg_cur++%IRQ_RING_SIZE] & Arf)
1510                         printk(KERN_ERR "%s: %s failed\n", dev->name, "CFG");
1511                 if (!(state &= ~Cfg))
1512                         goto out;
1513         }
1514         if (state & RxEvt) {
1515                 i = dev_per_card - 1;
1516                 do {
1517                         dscc4_rx_irq(priv, root + i);
1518                 } while (--i >= 0);
1519                 state &= ~RxEvt;
1520         }
1521         if (state & TxEvt) {
1522                 i = dev_per_card - 1;
1523                 do {
1524                         dscc4_tx_irq(priv, root + i);
1525                 } while (--i >= 0);
1526                 state &= ~TxEvt;
1527         }
1528 out:
1529         spin_unlock_irqrestore(&priv->lock, flags);
1530         return IRQ_RETVAL(handled);
1531 }
1532
1533 static void dscc4_tx_irq(struct dscc4_pci_priv *ppriv,
1534                                 struct dscc4_dev_priv *dpriv)
1535 {
1536         struct net_device *dev = dscc4_to_dev(dpriv);
1537         u32 state;
1538         int cur, loop = 0;
1539
1540 try:
1541         cur = dpriv->iqtx_current%IRQ_RING_SIZE;
1542         state = dpriv->iqtx[cur];
1543         if (!state) {
1544                 if (debug > 4)
1545                         printk(KERN_DEBUG "%s: Tx ISR = 0x%08x\n", dev->name,
1546                                state);
1547                 if ((debug > 1) && (loop > 1))
1548                         printk(KERN_DEBUG "%s: Tx irq loop=%d\n", dev->name, loop);
1549                 if (loop && netif_queue_stopped(dev))
1550                         if ((dpriv->tx_current - dpriv->tx_dirty)%TX_RING_SIZE)
1551                                 netif_wake_queue(dev);
1552
1553                 if (netif_running(dev) && dscc4_tx_quiescent(dpriv, dev) &&
1554                     !dscc4_tx_done(dpriv))
1555                                 dscc4_do_tx(dpriv, dev);
1556                 return;
1557         }
1558         loop++;
1559         dpriv->iqtx[cur] = 0;
1560         dpriv->iqtx_current++;
1561
1562         if (state_check(state, dpriv, dev, "Tx") < 0)
1563                 return;
1564
1565         if (state & SccEvt) {
1566                 if (state & Alls) {
1567                         struct net_device_stats *stats = hdlc_stats(dev);
1568                         struct sk_buff *skb;
1569                         struct TxFD *tx_fd;
1570
1571                         if (debug > 2)
1572                                 dscc4_tx_print(dev, dpriv, "Alls");
1573                         /*
1574                          * DataComplete can't be trusted for Tx completion.
1575                          * Cf errata DS5 p.8
1576                          */
1577                         cur = dpriv->tx_dirty%TX_RING_SIZE;
1578                         tx_fd = dpriv->tx_fd + cur;
1579                         skb = dpriv->tx_skbuff[cur];
1580                         if (skb) {
1581                                 pci_unmap_single(ppriv->pdev, tx_fd->data,
1582                                                  skb->len, PCI_DMA_TODEVICE);
1583                                 if (tx_fd->state & FrameEnd) {
1584                                         stats->tx_packets++;
1585                                         stats->tx_bytes += skb->len;
1586                                 }
1587                                 dev_kfree_skb_irq(skb);
1588                                 dpriv->tx_skbuff[cur] = NULL;
1589                                 ++dpriv->tx_dirty;
1590                         } else {
1591                                 if (debug > 1)
1592                                         printk(KERN_ERR "%s Tx: NULL skb %d\n",
1593                                                 dev->name, cur);
1594                         }
1595                         /*
1596                          * If the driver ends sending crap on the wire, it
1597                          * will be way easier to diagnose than the (not so)
1598                          * random freeze induced by null sized tx frames.
1599                          */
1600                         tx_fd->data = tx_fd->next;
1601                         tx_fd->state = FrameEnd | TO_STATE_TX(2*DUMMY_SKB_SIZE);
1602                         tx_fd->complete = 0x00000000;
1603                         tx_fd->jiffies = 0;
1604
1605                         if (!(state &= ~Alls))
1606                                 goto try;
1607                 }
1608                 /*
1609                  * Transmit Data Underrun
1610                  */
1611                 if (state & Xdu) {
1612                         printk(KERN_ERR "%s: XDU. Ask maintainer\n", DRV_NAME);
1613                         dpriv->flags = NeedIDT;
1614                         /* Tx reset */
1615                         writel(MTFi | Rdt,
1616                                dev->base_addr + 0x0c*dpriv->dev_id + CH0CFG);
1617                         writel(Action, dev->base_addr + GCMDR);
1618                         return;
1619                 }
1620                 if (state & Cts) {
1621                         printk(KERN_INFO "%s: CTS transition\n", dev->name);
1622                         if (!(state &= ~Cts)) /* DEBUG */
1623                                 goto try;
1624                 }
1625                 if (state & Xmr) {
1626                         /* Frame needs to be sent again - FIXME */
1627                         printk(KERN_ERR "%s: Xmr. Ask maintainer\n", DRV_NAME);
1628                         if (!(state &= ~Xmr)) /* DEBUG */
1629                                 goto try;
1630                 }
1631                 if (state & Xpr) {
1632                         unsigned long scc_addr, ring;
1633                         int i;
1634
1635                         /*
1636                          * - the busy condition happens (sometimes);
1637                          * - it doesn't seem to make the handler unreliable.
1638                          */
1639                         for (i = 1; i; i <<= 1) {
1640                                 if (!(scc_readl_star(dpriv, dev) & SccBusy))
1641                                         break;
1642                         }
1643                         if (!i)
1644                                 printk(KERN_INFO "%s busy in irq\n", dev->name);
1645
1646                         scc_addr = dev->base_addr + 0x0c*dpriv->dev_id;
1647                         /* Keep this order: IDT before IDR */
1648                         if (dpriv->flags & NeedIDT) {
1649                                 if (debug > 2)
1650                                         dscc4_tx_print(dev, dpriv, "Xpr");
1651                                 ring = dpriv->tx_fd_dma +
1652                                        (dpriv->tx_dirty%TX_RING_SIZE)*
1653                                        sizeof(struct TxFD);
1654                                 writel(ring, scc_addr + CH0BTDA);
1655                                 dscc4_do_tx(dpriv, dev);
1656                                 writel(MTFi | Idt, scc_addr + CH0CFG);
1657                                 if (dscc4_do_action(dev, "IDT") < 0)
1658                                         goto err_xpr;
1659                                 dpriv->flags &= ~NeedIDT;
1660                         }
1661                         if (dpriv->flags & NeedIDR) {
1662                                 ring = dpriv->rx_fd_dma +
1663                                        (dpriv->rx_current%RX_RING_SIZE)*
1664                                        sizeof(struct RxFD);
1665                                 writel(ring, scc_addr + CH0BRDA);
1666                                 dscc4_rx_update(dpriv, dev);
1667                                 writel(MTFi | Idr, scc_addr + CH0CFG);
1668                                 if (dscc4_do_action(dev, "IDR") < 0)
1669                                         goto err_xpr;
1670                                 dpriv->flags &= ~NeedIDR;
1671                                 smp_wmb();
1672                                 /* Activate receiver and misc */
1673                                 scc_writel(0x08050008, dpriv, dev, CCR2);
1674                         }
1675                 err_xpr:
1676                         if (!(state &= ~Xpr))
1677                                 goto try;
1678                 }
1679                 if (state & Cd) {
1680                         if (debug > 0)
1681                                 printk(KERN_INFO "%s: CD transition\n", dev->name);
1682                         if (!(state &= ~Cd)) /* DEBUG */
1683                                 goto try;
1684                 }
1685         } else { /* ! SccEvt */
1686                 if (state & Hi) {
1687 #ifdef DSCC4_POLLING
1688                         while (!dscc4_tx_poll(dpriv, dev));
1689 #endif
1690                         printk(KERN_INFO "%s: Tx Hi\n", dev->name);
1691                         state &= ~Hi;
1692                 }
1693                 if (state & Err) {
1694                         printk(KERN_INFO "%s: Tx ERR\n", dev->name);
1695                         hdlc_stats(dev)->tx_errors++;
1696                         state &= ~Err;
1697                 }
1698         }
1699         goto try;
1700 }
1701
1702 static void dscc4_rx_irq(struct dscc4_pci_priv *priv,
1703                                     struct dscc4_dev_priv *dpriv)
1704 {
1705         struct net_device *dev = dscc4_to_dev(dpriv);
1706         u32 state;
1707         int cur;
1708
1709 try:
1710         cur = dpriv->iqrx_current%IRQ_RING_SIZE;
1711         state = dpriv->iqrx[cur];
1712         if (!state)
1713                 return;
1714         dpriv->iqrx[cur] = 0;
1715         dpriv->iqrx_current++;
1716
1717         if (state_check(state, dpriv, dev, "Rx") < 0)
1718                 return;
1719
1720         if (!(state & SccEvt)){
1721                 struct RxFD *rx_fd;
1722
1723                 if (debug > 4)
1724                         printk(KERN_DEBUG "%s: Rx ISR = 0x%08x\n", dev->name,
1725                                state);
1726                 state &= 0x00ffffff;
1727                 if (state & Err) { /* Hold or reset */
1728                         printk(KERN_DEBUG "%s: Rx ERR\n", dev->name);
1729                         cur = dpriv->rx_current%RX_RING_SIZE;
1730                         rx_fd = dpriv->rx_fd + cur;
1731                         /*
1732                          * Presume we're not facing a DMAC receiver reset.
1733                          * As We use the rx size-filtering feature of the
1734                          * DSCC4, the beginning of a new frame is waiting in
1735                          * the rx fifo. I bet a Receive Data Overflow will
1736                          * happen most of time but let's try and avoid it.
1737                          * Btw (as for RDO) if one experiences ERR whereas
1738                          * the system looks rather idle, there may be a
1739                          * problem with latency. In this case, increasing
1740                          * RX_RING_SIZE may help.
1741                          */
1742                         //while (dpriv->rx_needs_refill) {
1743                                 while (!(rx_fd->state1 & Hold)) {
1744                                         rx_fd++;
1745                                         cur++;
1746                                         if (!(cur = cur%RX_RING_SIZE))
1747                                                 rx_fd = dpriv->rx_fd;
1748                                 }
1749                                 //dpriv->rx_needs_refill--;
1750                                 try_get_rx_skb(dpriv, dev);
1751                                 if (!rx_fd->data)
1752                                         goto try;
1753                                 rx_fd->state1 &= ~Hold;
1754                                 rx_fd->state2 = 0x00000000;
1755                                 rx_fd->end = 0xbabeface;
1756                         //}
1757                         goto try;
1758                 }
1759                 if (state & Fi) {
1760                         dscc4_rx_skb(dpriv, dev);
1761                         goto try;
1762                 }
1763                 if (state & Hi ) { /* HI bit */
1764                         printk(KERN_INFO "%s: Rx Hi\n", dev->name);
1765                         state &= ~Hi;
1766                         goto try;
1767                 }
1768         } else { /* SccEvt */
1769                 if (debug > 1) {
1770                         //FIXME: verifier la presence de tous les evenements
1771                 static struct {
1772                         u32 mask;
1773                         const char *irq_name;
1774                 } evts[] = {
1775                         { 0x00008000, "TIN"},
1776                         { 0x00000020, "RSC"},
1777                         { 0x00000010, "PCE"},
1778                         { 0x00000008, "PLLA"},
1779                         { 0, NULL}
1780                 }, *evt;
1781
1782                 for (evt = evts; evt->irq_name; evt++) {
1783                         if (state & evt->mask) {
1784                                         printk(KERN_DEBUG "%s: %s\n",
1785                                                 dev->name, evt->irq_name);
1786                                 if (!(state &= ~evt->mask))
1787                                         goto try;
1788                         }
1789                 }
1790                 } else {
1791                         if (!(state &= ~0x0000c03c))
1792                                 goto try;
1793                 }
1794                 if (state & Cts) {
1795                         printk(KERN_INFO "%s: CTS transition\n", dev->name);
1796                         if (!(state &= ~Cts)) /* DEBUG */
1797                                 goto try;
1798                 }
1799                 /*
1800                  * Receive Data Overflow (FIXME: fscked)
1801                  */
1802                 if (state & Rdo) {
1803                         struct RxFD *rx_fd;
1804                         u32 scc_addr;
1805                         int cur;
1806
1807                         //if (debug)
1808                         //      dscc4_rx_dump(dpriv);
1809                         scc_addr = dev->base_addr + 0x0c*dpriv->dev_id;
1810
1811                         scc_patchl(RxActivate, 0, dpriv, dev, CCR2);
1812                         /*
1813                          * This has no effect. Why ?
1814                          * ORed with TxSccRes, one sees the CFG ack (for
1815                          * the TX part only).
1816                          */
1817                         scc_writel(RxSccRes, dpriv, dev, CMDR);
1818                         dpriv->flags |= RdoSet;
1819
1820                         /*
1821                          * Let's try and save something in the received data.
1822                          * rx_current must be incremented at least once to
1823                          * avoid HOLD in the BRDA-to-be-pointed desc.
1824                          */
1825                         do {
1826                                 cur = dpriv->rx_current++%RX_RING_SIZE;
1827                                 rx_fd = dpriv->rx_fd + cur;
1828                                 if (!(rx_fd->state2 & DataComplete))
1829                                         break;
1830                                 if (rx_fd->state2 & FrameAborted) {
1831                                         hdlc_stats(dev)->rx_over_errors++;
1832                                         rx_fd->state1 |= Hold;
1833                                         rx_fd->state2 = 0x00000000;
1834                                         rx_fd->end = 0xbabeface;
1835                                 } else
1836                                         dscc4_rx_skb(dpriv, dev);
1837                         } while (1);
1838
1839                         if (debug > 0) {
1840                                 if (dpriv->flags & RdoSet)
1841                                         printk(KERN_DEBUG
1842                                                "%s: no RDO in Rx data\n", DRV_NAME);
1843                         }
1844 #ifdef DSCC4_RDO_EXPERIMENTAL_RECOVERY
1845                         /*
1846                          * FIXME: must the reset be this violent ?
1847                          */
1848 #warning "FIXME: CH0BRDA"
1849                         writel(dpriv->rx_fd_dma +
1850                                (dpriv->rx_current%RX_RING_SIZE)*
1851                                sizeof(struct RxFD), scc_addr + CH0BRDA);
1852                         writel(MTFi|Rdr|Idr, scc_addr + CH0CFG);
1853                         if (dscc4_do_action(dev, "RDR") < 0) {
1854                                 printk(KERN_ERR "%s: RDO recovery failed(%s)\n",
1855                                        dev->name, "RDR");
1856                                 goto rdo_end;
1857                         }
1858                         writel(MTFi|Idr, scc_addr + CH0CFG);
1859                         if (dscc4_do_action(dev, "IDR") < 0) {
1860                                 printk(KERN_ERR "%s: RDO recovery failed(%s)\n",
1861                                        dev->name, "IDR");
1862                                 goto rdo_end;
1863                         }
1864                 rdo_end:
1865 #endif
1866                         scc_patchl(0, RxActivate, dpriv, dev, CCR2);
1867                         goto try;
1868                 }
1869                 if (state & Cd) {
1870                         printk(KERN_INFO "%s: CD transition\n", dev->name);
1871                         if (!(state &= ~Cd)) /* DEBUG */
1872                                 goto try;
1873                 }
1874                 if (state & Flex) {
1875                         printk(KERN_DEBUG "%s: Flex. Ttttt...\n", DRV_NAME);
1876                         if (!(state &= ~Flex))
1877                                 goto try;
1878                 }
1879         }
1880 }
1881
1882 /*
1883  * I had expected the following to work for the first descriptor
1884  * (tx_fd->state = 0xc0000000)
1885  * - Hold=1 (don't try and branch to the next descripto);
1886  * - No=0 (I want an empty data section, i.e. size=0);
1887  * - Fe=1 (required by No=0 or we got an Err irq and must reset).
1888  * It failed and locked solid. Thus the introduction of a dummy skb.
1889  * Problem is acknowledged in errata sheet DS5. Joy :o/
1890  */
1891 struct sk_buff *dscc4_init_dummy_skb(struct dscc4_dev_priv *dpriv)
1892 {
1893         struct sk_buff *skb;
1894
1895         skb = dev_alloc_skb(DUMMY_SKB_SIZE);
1896         if (skb) {
1897                 int last = dpriv->tx_dirty%TX_RING_SIZE;
1898                 struct TxFD *tx_fd = dpriv->tx_fd + last;
1899
1900                 skb->len = DUMMY_SKB_SIZE;
1901                 memcpy(skb->data, version, strlen(version)%DUMMY_SKB_SIZE);
1902                 tx_fd->state = FrameEnd | TO_STATE_TX(DUMMY_SKB_SIZE);
1903                 tx_fd->data = pci_map_single(dpriv->pci_priv->pdev, skb->data,
1904                                              DUMMY_SKB_SIZE, PCI_DMA_TODEVICE);
1905                 dpriv->tx_skbuff[last] = skb;
1906         }
1907         return skb;
1908 }
1909
1910 static int dscc4_init_ring(struct net_device *dev)
1911 {
1912         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
1913         struct pci_dev *pdev = dpriv->pci_priv->pdev;
1914         struct TxFD *tx_fd;
1915         struct RxFD *rx_fd;
1916         void *ring;
1917         int i;
1918
1919         ring = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &dpriv->rx_fd_dma);
1920         if (!ring)
1921                 goto err_out;
1922         dpriv->rx_fd = rx_fd = (struct RxFD *) ring;
1923
1924         ring = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &dpriv->tx_fd_dma);
1925         if (!ring)
1926                 goto err_free_dma_rx;
1927         dpriv->tx_fd = tx_fd = (struct TxFD *) ring;
1928
1929         memset(dpriv->tx_skbuff, 0, sizeof(struct sk_buff *)*TX_RING_SIZE);
1930         dpriv->tx_dirty = 0xffffffff;
1931         i = dpriv->tx_current = 0;
1932         do {
1933                 tx_fd->state = FrameEnd | TO_STATE_TX(2*DUMMY_SKB_SIZE);
1934                 tx_fd->complete = 0x00000000;
1935                 /* FIXME: NULL should be ok - to be tried */
1936                 tx_fd->data = dpriv->tx_fd_dma;
1937                 (tx_fd++)->next = (u32)(dpriv->tx_fd_dma +
1938                                         (++i%TX_RING_SIZE)*sizeof(*tx_fd));
1939         } while (i < TX_RING_SIZE);
1940
1941         if (dscc4_init_dummy_skb(dpriv) < 0)
1942                 goto err_free_dma_tx;
1943
1944         memset(dpriv->rx_skbuff, 0, sizeof(struct sk_buff *)*RX_RING_SIZE);
1945         i = dpriv->rx_dirty = dpriv->rx_current = 0;
1946         do {
1947                 /* size set by the host. Multiple of 4 bytes please */
1948                 rx_fd->state1 = HiDesc;
1949                 rx_fd->state2 = 0x00000000;
1950                 rx_fd->end = 0xbabeface;
1951                 rx_fd->state1 |= TO_STATE_RX(HDLC_MAX_MRU);
1952                 // FIXME: return value verifiee mais traitement suspect
1953                 if (try_get_rx_skb(dpriv, dev) >= 0)
1954                         dpriv->rx_dirty++;
1955                 (rx_fd++)->next = (u32)(dpriv->rx_fd_dma +
1956                                         (++i%RX_RING_SIZE)*sizeof(*rx_fd));
1957         } while (i < RX_RING_SIZE);
1958
1959         return 0;
1960
1961 err_free_dma_tx:
1962         pci_free_consistent(pdev, TX_TOTAL_SIZE, ring, dpriv->tx_fd_dma);
1963 err_free_dma_rx:
1964         pci_free_consistent(pdev, RX_TOTAL_SIZE, rx_fd, dpriv->rx_fd_dma);
1965 err_out:
1966         return -ENOMEM;
1967 }
1968
1969 static void __devexit dscc4_remove_one(struct pci_dev *pdev)
1970 {
1971         struct dscc4_pci_priv *ppriv;
1972         struct dscc4_dev_priv *root;
1973         unsigned long ioaddr;
1974         int i;
1975
1976         ppriv = pci_get_drvdata(pdev);
1977         root = ppriv->root;
1978
1979         ioaddr = dscc4_to_dev(root)->base_addr;
1980
1981         dscc4_pci_reset(pdev, ioaddr);
1982
1983         free_irq(pdev->irq, root);
1984         pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32), ppriv->iqcfg,
1985                             ppriv->iqcfg_dma);
1986         for (i = 0; i < dev_per_card; i++) {
1987                 struct dscc4_dev_priv *dpriv = root + i;
1988
1989                 dscc4_release_ring(dpriv);
1990                 pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
1991                                     dpriv->iqrx, dpriv->iqrx_dma);
1992                 pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
1993                                     dpriv->iqtx, dpriv->iqtx_dma);
1994         }
1995
1996         dscc4_free1(pdev);
1997
1998         iounmap((void *)ioaddr);
1999
2000         release_mem_region(pci_resource_start(pdev, 1),
2001                            pci_resource_len(pdev, 1));
2002         release_mem_region(pci_resource_start(pdev, 0),
2003                            pci_resource_len(pdev, 0));
2004 }
2005
2006 static int dscc4_hdlc_attach(struct net_device *dev, unsigned short encoding,
2007         unsigned short parity)
2008 {
2009         struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
2010
2011         if (encoding != ENCODING_NRZ &&
2012             encoding != ENCODING_NRZI &&
2013             encoding != ENCODING_FM_MARK &&
2014             encoding != ENCODING_FM_SPACE &&
2015             encoding != ENCODING_MANCHESTER)
2016                 return -EINVAL;
2017
2018         if (parity != PARITY_NONE &&
2019             parity != PARITY_CRC16_PR0_CCITT &&
2020             parity != PARITY_CRC16_PR1_CCITT &&
2021             parity != PARITY_CRC32_PR0_CCITT &&
2022             parity != PARITY_CRC32_PR1_CCITT)
2023                 return -EINVAL;
2024
2025         dpriv->encoding = encoding;
2026         dpriv->parity = parity;
2027         return 0;
2028 }
2029
2030 #ifndef MODULE
2031 static int __init dscc4_setup(char *str)
2032 {
2033         int *args[] = { &debug, &quartz, NULL }, **p = args;
2034
2035         while (*p && (get_option(&str, *p) == 2))
2036                 p++;
2037         return 1;
2038 }
2039
2040 __setup("dscc4.setup=", dscc4_setup);
2041 #endif
2042
2043 static struct pci_device_id dscc4_pci_tbl[] = {
2044         { PCI_VENDOR_ID_SIEMENS, PCI_DEVICE_ID_SIEMENS_DSCC4,
2045                 PCI_ANY_ID, PCI_ANY_ID, },
2046         { 0,}
2047 };
2048 MODULE_DEVICE_TABLE(pci, dscc4_pci_tbl);
2049
2050 static struct pci_driver dscc4_driver = {
2051         .name           = DRV_NAME,
2052         .id_table       = dscc4_pci_tbl,
2053         .probe          = dscc4_init_one,
2054         .remove         = __devexit_p(dscc4_remove_one),
2055 };
2056
2057 static int __init dscc4_init_module(void)
2058 {
2059         return pci_module_init(&dscc4_driver);
2060 }
2061
2062 static void __exit dscc4_cleanup_module(void)
2063 {
2064         pci_unregister_driver(&dscc4_driver);
2065 }
2066
2067 module_init(dscc4_init_module);
2068 module_exit(dscc4_cleanup_module);