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