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