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