ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / atm / fore200e.c
1 /*
2   $Id: fore200e.c,v 1.5 2000/04/14 10:10:34 davem Exp $
3
4   A FORE Systems 200E-series driver for ATM on Linux.
5   Christophe Lizzi (lizzi@cnam.fr), October 1999-March 2003.
6
7   Based on the PCA-200E driver from Uwe Dannowski (Uwe.Dannowski@inf.tu-dresden.de).
8
9   This driver simultaneously supports PCA-200E and SBA-200E adapters
10   on i386, alpha (untested), powerpc, sparc and sparc64 architectures.
11
12   This program is free software; you can redistribute it and/or modify
13   it under the terms of the GNU General Public License as published by
14   the Free Software Foundation; either version 2 of the License, or
15   (at your option) any later version.
16
17   This program is distributed in the hope that it will be useful,
18   but WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   GNU General Public License for more details.
21
22   You should have received a copy of the GNU General Public License
23   along with this program; if not, write to the Free Software
24   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 */
26
27
28 #include <linux/config.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/capability.h>
33 #include <linux/sched.h>
34 #include <linux/interrupt.h>
35 #include <linux/bitops.h>
36 #include <linux/pci.h>
37 #include <linux/module.h>
38 #include <linux/atmdev.h>
39 #include <linux/sonet.h>
40 #include <linux/atm_suni.h>
41 #include <asm/io.h>
42 #include <asm/string.h>
43 #include <asm/page.h>
44 #include <asm/irq.h>
45 #include <asm/dma.h>
46 #include <asm/byteorder.h>
47 #include <asm/uaccess.h>
48 #include <asm/atomic.h>
49
50 #ifdef CONFIG_ATM_FORE200E_SBA
51 #include <asm/idprom.h>
52 #include <asm/sbus.h>
53 #include <asm/openprom.h>
54 #include <asm/oplib.h>
55 #include <asm/pgtable.h>
56 #endif
57
58 #if defined(CONFIG_ATM_FORE200E_USE_TASKLET) /* defer interrupt work to a tasklet */
59 #define FORE200E_USE_TASKLET
60 #endif
61
62 #if 0 /* enable the debugging code of the buffer supply queues */
63 #define FORE200E_BSQ_DEBUG
64 #endif
65
66 #if 1 /* ensure correct handling of 52-byte AAL0 SDUs expected by atmdump-like apps */
67 #define FORE200E_52BYTE_AAL0_SDU
68 #endif
69
70 #include "fore200e.h"
71 #include "suni.h"
72
73 #define FORE200E_VERSION "0.3e"
74
75 #define FORE200E         "fore200e: "
76
77 #if 0 /* override .config */
78 #define CONFIG_ATM_FORE200E_DEBUG 1
79 #endif
80 #if defined(CONFIG_ATM_FORE200E_DEBUG) && (CONFIG_ATM_FORE200E_DEBUG > 0)
81 #define DPRINTK(level, format, args...)  do { if (CONFIG_ATM_FORE200E_DEBUG >= (level)) \
82                                                   printk(FORE200E format, ##args); } while (0)
83 #else
84 #define DPRINTK(level, format, args...)  do {} while (0)
85 #endif
86
87
88 #define FORE200E_ALIGN(addr, alignment) \
89         ((((unsigned long)(addr) + (alignment - 1)) & ~(alignment - 1)) - (unsigned long)(addr))
90
91 #define FORE200E_DMA_INDEX(dma_addr, type, index)  ((dma_addr) + (index) * sizeof(type))
92
93 #define FORE200E_INDEX(virt_addr, type, index)     (&((type *)(virt_addr))[ index ])
94
95 #define FORE200E_NEXT_ENTRY(index, modulo)         (index = ++(index) % (modulo))
96
97
98 #define MSECS(ms)  (((ms)*HZ/1000)+1)
99
100
101 #if 1
102 #define ASSERT(expr)     if (!(expr)) { \
103                              printk(FORE200E "assertion failed! %s[%d]: %s\n", \
104                                     __FUNCTION__, __LINE__, #expr); \
105                              panic(FORE200E "%s", __FUNCTION__); \
106                          }
107 #else
108 #define ASSERT(expr)     do {} while (0)
109 #endif
110
111
112 extern const struct atmdev_ops   fore200e_ops;
113 extern const struct fore200e_bus fore200e_bus[];
114
115 static struct fore200e* fore200e_boards = NULL;
116
117
118 MODULE_AUTHOR("Christophe Lizzi - credits to Uwe Dannowski and Heikki Vatiainen");
119 MODULE_DESCRIPTION("FORE Systems 200E-series ATM driver - version " FORE200E_VERSION);
120 MODULE_SUPPORTED_DEVICE("PCA-200E, SBA-200E");
121
122
123 static const int fore200e_rx_buf_nbr[ BUFFER_SCHEME_NBR ][ BUFFER_MAGN_NBR ] = {
124     { BUFFER_S1_NBR, BUFFER_L1_NBR },
125     { BUFFER_S2_NBR, BUFFER_L2_NBR }
126 };
127
128 static const int fore200e_rx_buf_size[ BUFFER_SCHEME_NBR ][ BUFFER_MAGN_NBR ] = {
129     { BUFFER_S1_SIZE, BUFFER_L1_SIZE },
130     { BUFFER_S2_SIZE, BUFFER_L2_SIZE }
131 };
132
133
134 #if defined(CONFIG_ATM_FORE200E_DEBUG) && (CONFIG_ATM_FORE200E_DEBUG > 0)
135 static const char* fore200e_traffic_class[] = { "NONE", "UBR", "CBR", "VBR", "ABR", "ANY" };
136 #endif
137
138
139 #if 0 /* currently unused */
140 static int 
141 fore200e_fore2atm_aal(enum fore200e_aal aal)
142 {
143     switch(aal) {
144     case FORE200E_AAL0:  return ATM_AAL0;
145     case FORE200E_AAL34: return ATM_AAL34;
146     case FORE200E_AAL5:  return ATM_AAL5;
147     }
148
149     return -EINVAL;
150 }
151 #endif
152
153
154 static enum fore200e_aal
155 fore200e_atm2fore_aal(int aal)
156 {
157     switch(aal) {
158     case ATM_AAL0:  return FORE200E_AAL0;
159     case ATM_AAL34: return FORE200E_AAL34;
160     case ATM_AAL1:
161     case ATM_AAL2:
162     case ATM_AAL5:  return FORE200E_AAL5;
163     }
164
165     return -EINVAL;
166 }
167
168
169 static char*
170 fore200e_irq_itoa(int irq)
171 {
172 #if defined(__sparc_v9__)
173     return __irq_itoa(irq);
174 #else
175     static char str[8];
176     sprintf(str, "%d", irq);
177     return str;
178 #endif
179 }
180
181
182 static void*
183 fore200e_kmalloc(int size, int flags)
184 {
185     void* chunk = kmalloc(size, flags);
186
187     if (chunk)
188         memset(chunk, 0x00, size);
189     else
190         printk(FORE200E "kmalloc() failed, requested size = %d, flags = 0x%x\n", size, flags);
191     
192     return chunk;
193 }
194
195
196 static void
197 fore200e_kfree(void* chunk)
198 {
199     kfree(chunk);
200 }
201
202
203 /* allocate and align a chunk of memory intended to hold the data behing exchanged
204    between the driver and the adapter (using streaming DVMA) */
205
206 static int
207 fore200e_chunk_alloc(struct fore200e* fore200e, struct chunk* chunk, int size, int alignment, int direction)
208 {
209     unsigned long offset = 0;
210
211     if (alignment <= sizeof(int))
212         alignment = 0;
213
214     chunk->alloc_size = size + alignment;
215     chunk->align_size = size;
216     chunk->direction  = direction;
217
218     chunk->alloc_addr = fore200e_kmalloc(chunk->alloc_size, GFP_KERNEL | GFP_DMA);
219     if (chunk->alloc_addr == NULL)
220         return -ENOMEM;
221
222     if (alignment > 0)
223         offset = FORE200E_ALIGN(chunk->alloc_addr, alignment); 
224     
225     chunk->align_addr = chunk->alloc_addr + offset;
226
227     chunk->dma_addr = fore200e->bus->dma_map(fore200e, chunk->align_addr, chunk->align_size, direction);
228     
229     return 0;
230 }
231
232
233 /* free a chunk of memory */
234
235 static void
236 fore200e_chunk_free(struct fore200e* fore200e, struct chunk* chunk)
237 {
238     fore200e->bus->dma_unmap(fore200e, chunk->dma_addr, chunk->dma_size, chunk->direction);
239
240     fore200e_kfree(chunk->alloc_addr);
241 }
242
243
244 static void
245 fore200e_spin(int msecs)
246 {
247     unsigned long timeout = jiffies + MSECS(msecs);
248     while (time_before(jiffies, timeout));
249 }
250
251
252 static int
253 fore200e_poll(struct fore200e* fore200e, volatile u32* addr, u32 val, int msecs)
254 {
255     unsigned long timeout = jiffies + MSECS(msecs);
256     int           ok;
257
258     mb();
259     do {
260         if ((ok = (*addr == val)) || (*addr & STATUS_ERROR))
261             break;
262
263     } while (time_before(jiffies, timeout));
264
265 #if 1
266     if (!ok) {
267         printk(FORE200E "cmd polling failed, got status 0x%08x, expected 0x%08x\n",
268                *addr, val);
269     }
270 #endif
271
272     return ok;
273 }
274
275
276 static int
277 fore200e_io_poll(struct fore200e* fore200e, volatile u32* addr, u32 val, int msecs)
278 {
279     unsigned long timeout = jiffies + MSECS(msecs);
280     int           ok;
281
282     do {
283         if ((ok = (fore200e->bus->read(addr) == val)))
284             break;
285
286     } while (time_before(jiffies, timeout));
287
288 #if 1
289     if (!ok) {
290         printk(FORE200E "I/O polling failed, got status 0x%08x, expected 0x%08x\n",
291                fore200e->bus->read(addr), val);
292     }
293 #endif
294
295     return ok;
296 }
297
298
299 static void
300 fore200e_free_rx_buf(struct fore200e* fore200e)
301 {
302     int scheme, magn, nbr;
303     struct buffer* buffer;
304
305     for (scheme = 0; scheme < BUFFER_SCHEME_NBR; scheme++) {
306         for (magn = 0; magn < BUFFER_MAGN_NBR; magn++) {
307
308             if ((buffer = fore200e->host_bsq[ scheme ][ magn ].buffer) != NULL) {
309
310                 for (nbr = 0; nbr < fore200e_rx_buf_nbr[ scheme ][ magn ]; nbr++) {
311
312                     struct chunk* data = &buffer[ nbr ].data;
313
314                     if (data->alloc_addr != NULL)
315                         fore200e_chunk_free(fore200e, data);
316                 }
317             }
318         }
319     }
320 }
321
322
323 static void
324 fore200e_uninit_bs_queue(struct fore200e* fore200e)
325 {
326     int scheme, magn;
327     
328     for (scheme = 0; scheme < BUFFER_SCHEME_NBR; scheme++) {
329         for (magn = 0; magn < BUFFER_MAGN_NBR; magn++) {
330
331             struct chunk* status    = &fore200e->host_bsq[ scheme ][ magn ].status;
332             struct chunk* rbd_block = &fore200e->host_bsq[ scheme ][ magn ].rbd_block;
333             
334             if (status->alloc_addr)
335                 fore200e->bus->dma_chunk_free(fore200e, status);
336             
337             if (rbd_block->alloc_addr)
338                 fore200e->bus->dma_chunk_free(fore200e, rbd_block);
339         }
340     }
341 }
342
343
344 static int
345 fore200e_reset(struct fore200e* fore200e, int diag)
346 {
347     int ok;
348
349     fore200e->cp_monitor = (struct cp_monitor*)(fore200e->virt_base + FORE200E_CP_MONITOR_OFFSET);
350     
351     fore200e->bus->write(BSTAT_COLD_START, &fore200e->cp_monitor->bstat);
352
353     fore200e->bus->reset(fore200e);
354
355     if (diag) {
356         ok = fore200e_io_poll(fore200e, &fore200e->cp_monitor->bstat, BSTAT_SELFTEST_OK, 1000);
357         if (ok == 0) {
358             
359             printk(FORE200E "device %s self-test failed\n", fore200e->name);
360             return -ENODEV;
361         }
362
363         printk(FORE200E "device %s self-test passed\n", fore200e->name);
364         
365         fore200e->state = FORE200E_STATE_RESET;
366     }
367
368     return 0;
369 }
370
371
372 static void
373 fore200e_shutdown(struct fore200e* fore200e)
374 {
375     printk(FORE200E "removing device %s at 0x%lx, IRQ %s\n",
376            fore200e->name, fore200e->phys_base, 
377            fore200e_irq_itoa(fore200e->irq));
378     
379     if (fore200e->state > FORE200E_STATE_RESET) {
380         /* first, reset the board to prevent further interrupts or data transfers */
381         fore200e_reset(fore200e, 0);
382     }
383     
384     /* then, release all allocated resources */
385     switch(fore200e->state) {
386
387     case FORE200E_STATE_COMPLETE:
388         if (fore200e->stats)
389             kfree(fore200e->stats);
390
391     case FORE200E_STATE_IRQ:
392         free_irq(fore200e->irq, fore200e->atm_dev);
393
394     case FORE200E_STATE_ALLOC_BUF:
395         fore200e_free_rx_buf(fore200e);
396
397     case FORE200E_STATE_INIT_BSQ:
398         fore200e_uninit_bs_queue(fore200e);
399
400     case FORE200E_STATE_INIT_RXQ:
401         fore200e->bus->dma_chunk_free(fore200e, &fore200e->host_rxq.status);
402         fore200e->bus->dma_chunk_free(fore200e, &fore200e->host_rxq.rpd);
403
404     case FORE200E_STATE_INIT_TXQ:
405         fore200e->bus->dma_chunk_free(fore200e, &fore200e->host_txq.status);
406         fore200e->bus->dma_chunk_free(fore200e, &fore200e->host_txq.tpd);
407
408     case FORE200E_STATE_INIT_CMDQ:
409         fore200e->bus->dma_chunk_free(fore200e, &fore200e->host_cmdq.status);
410
411     case FORE200E_STATE_INITIALIZE:
412         /* nothing to do for that state */
413
414     case FORE200E_STATE_START_FW:
415         /* nothing to do for that state */
416
417     case FORE200E_STATE_LOAD_FW:
418         /* nothing to do for that state */
419
420     case FORE200E_STATE_RESET:
421         /* nothing to do for that state */
422
423     case FORE200E_STATE_MAP:
424         fore200e->bus->unmap(fore200e);
425
426     case FORE200E_STATE_CONFIGURE:
427         /* nothing to do for that state */
428
429     case FORE200E_STATE_REGISTER:
430         /* XXX shouldn't we *start* by deregistering the device? */
431         atm_dev_deregister(fore200e->atm_dev);
432
433     case FORE200E_STATE_BLANK:
434         /* nothing to do for that state */
435         break;
436     }
437 }
438
439
440 #ifdef CONFIG_ATM_FORE200E_PCA
441
442 static u32 fore200e_pca_read(volatile u32* addr)
443 {
444     /* on big-endian hosts, the board is configured to convert
445        the endianess of slave RAM accesses  */
446     return le32_to_cpu(readl(addr));
447 }
448
449
450 static void fore200e_pca_write(u32 val, volatile u32* addr)
451 {
452     /* on big-endian hosts, the board is configured to convert
453        the endianess of slave RAM accesses  */
454     writel(cpu_to_le32(val), addr);
455 }
456
457
458 static u32
459 fore200e_pca_dma_map(struct fore200e* fore200e, void* virt_addr, int size, int direction)
460 {
461     u32 dma_addr = pci_map_single((struct pci_dev*)fore200e->bus_dev, virt_addr, size, direction);
462
463     DPRINTK(3, "PCI DVMA mapping: virt_addr = 0x%p, size = %d, direction = %d,  --> dma_addr = 0x%08x\n",
464             virt_addr, size, direction, dma_addr);
465     
466     return dma_addr;
467 }
468
469
470 static void
471 fore200e_pca_dma_unmap(struct fore200e* fore200e, u32 dma_addr, int size, int direction)
472 {
473     DPRINTK(3, "PCI DVMA unmapping: dma_addr = 0x%08x, size = %d, direction = %d\n",
474             dma_addr, size, direction);
475
476     pci_unmap_single((struct pci_dev*)fore200e->bus_dev, dma_addr, size, direction);
477 }
478
479
480 static void
481 fore200e_pca_dma_sync_for_cpu(struct fore200e* fore200e, u32 dma_addr, int size, int direction)
482 {
483     DPRINTK(3, "PCI DVMA sync: dma_addr = 0x%08x, size = %d, direction = %d\n", dma_addr, size, direction);
484
485     pci_dma_sync_single_for_cpu((struct pci_dev*)fore200e->bus_dev, dma_addr, size, direction);
486 }
487
488 static void
489 fore200e_pca_dma_sync_for_device(struct fore200e* fore200e, u32 dma_addr, int size, int direction)
490 {
491     DPRINTK(3, "PCI DVMA sync: dma_addr = 0x%08x, size = %d, direction = %d\n", dma_addr, size, direction);
492
493     pci_dma_sync_single_for_device((struct pci_dev*)fore200e->bus_dev, dma_addr, size, direction);
494 }
495
496
497 /* allocate a DMA consistent chunk of memory intended to act as a communication mechanism
498    (to hold descriptors, status, queues, etc.) shared by the driver and the adapter */
499
500 static int
501 fore200e_pca_dma_chunk_alloc(struct fore200e* fore200e, struct chunk* chunk,
502                              int size, int nbr, int alignment)
503 {
504     /* returned chunks are page-aligned */
505     chunk->alloc_size = size * nbr;
506     chunk->alloc_addr = pci_alloc_consistent((struct pci_dev*)fore200e->bus_dev,
507                                              chunk->alloc_size,
508                                              &chunk->dma_addr);
509     
510     if ((chunk->alloc_addr == NULL) || (chunk->dma_addr == 0))
511         return -ENOMEM;
512
513     chunk->align_addr = chunk->alloc_addr;
514     
515     return 0;
516 }
517
518
519 /* free a DMA consistent chunk of memory */
520
521 static void
522 fore200e_pca_dma_chunk_free(struct fore200e* fore200e, struct chunk* chunk)
523 {
524     pci_free_consistent((struct pci_dev*)fore200e->bus_dev,
525                         chunk->alloc_size,
526                         chunk->alloc_addr,
527                         chunk->dma_addr);
528 }
529
530
531 static int
532 fore200e_pca_irq_check(struct fore200e* fore200e)
533 {
534     /* this is a 1 bit register */
535     int irq_posted = readl(fore200e->regs.pca.psr);
536
537 #if defined(CONFIG_ATM_FORE200E_DEBUG) && (CONFIG_ATM_FORE200E_DEBUG == 2)
538     if (irq_posted && (readl(fore200e->regs.pca.hcr) & PCA200E_HCR_OUTFULL)) {
539         DPRINTK(2,"FIFO OUT full, device %d\n", fore200e->atm_dev->number);
540     }
541 #endif
542
543     return irq_posted;
544 }
545
546
547 static void
548 fore200e_pca_irq_ack(struct fore200e* fore200e)
549 {
550     writel(PCA200E_HCR_CLRINTR, fore200e->regs.pca.hcr);
551 }
552
553
554 static void
555 fore200e_pca_reset(struct fore200e* fore200e)
556 {
557     writel(PCA200E_HCR_RESET, fore200e->regs.pca.hcr);
558     fore200e_spin(10);
559     writel(0, fore200e->regs.pca.hcr);
560 }
561
562
563 static int __init
564 fore200e_pca_map(struct fore200e* fore200e)
565 {
566     DPRINTK(2, "device %s being mapped in memory\n", fore200e->name);
567
568     fore200e->virt_base = ioremap(fore200e->phys_base, PCA200E_IOSPACE_LENGTH);
569     
570     if (fore200e->virt_base == NULL) {
571         printk(FORE200E "can't map device %s\n", fore200e->name);
572         return -EFAULT;
573     }
574
575     DPRINTK(1, "device %s mapped to 0x%p\n", fore200e->name, fore200e->virt_base);
576
577     /* gain access to the PCA specific registers  */
578     fore200e->regs.pca.hcr = (u32*)(fore200e->virt_base + PCA200E_HCR_OFFSET);
579     fore200e->regs.pca.imr = (u32*)(fore200e->virt_base + PCA200E_IMR_OFFSET);
580     fore200e->regs.pca.psr = (u32*)(fore200e->virt_base + PCA200E_PSR_OFFSET);
581
582     fore200e->state = FORE200E_STATE_MAP;
583     return 0;
584 }
585
586
587 static void
588 fore200e_pca_unmap(struct fore200e* fore200e)
589 {
590     DPRINTK(2, "device %s being unmapped from memory\n", fore200e->name);
591
592     if (fore200e->virt_base != NULL)
593         iounmap(fore200e->virt_base);
594 }
595
596
597 static int __init
598 fore200e_pca_configure(struct fore200e* fore200e)
599 {
600     struct pci_dev* pci_dev = (struct pci_dev*)fore200e->bus_dev;
601     u8              master_ctrl, latency;
602
603     DPRINTK(2, "device %s being configured\n", fore200e->name);
604
605     if ((pci_dev->irq == 0) || (pci_dev->irq == 0xFF)) {
606         printk(FORE200E "incorrect IRQ setting - misconfigured PCI-PCI bridge?\n");
607         return -EIO;
608     }
609
610     pci_read_config_byte(pci_dev, PCA200E_PCI_MASTER_CTRL, &master_ctrl);
611
612     master_ctrl = master_ctrl
613 #if defined(__BIG_ENDIAN)
614         /* request the PCA board to convert the endianess of slave RAM accesses */
615         | PCA200E_CTRL_CONVERT_ENDIAN
616 #endif
617 #if 0
618         | PCA200E_CTRL_DIS_CACHE_RD
619         | PCA200E_CTRL_DIS_WRT_INVAL
620         | PCA200E_CTRL_ENA_CONT_REQ_MODE
621         | PCA200E_CTRL_2_CACHE_WRT_INVAL
622 #endif
623         | PCA200E_CTRL_LARGE_PCI_BURSTS;
624     
625     pci_write_config_byte(pci_dev, PCA200E_PCI_MASTER_CTRL, master_ctrl);
626
627     /* raise latency from 32 (default) to 192, as this seems to prevent NIC
628        lockups (under heavy rx loads) due to continuous 'FIFO OUT full' condition.
629        this may impact the performances of other PCI devices on the same bus, though */
630     latency = 192;
631     pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
632
633     fore200e->state = FORE200E_STATE_CONFIGURE;
634     return 0;
635 }
636
637
638 static struct fore200e* __init
639 fore200e_pca_detect(const struct fore200e_bus* bus, int index)
640 {
641     struct fore200e* fore200e;
642     struct pci_dev*  pci_dev = NULL;
643     int              count = index;
644     
645     do {
646         pci_dev = pci_find_device(PCI_VENDOR_ID_FORE, PCI_DEVICE_ID_FORE_PCA200E, pci_dev);
647         if (pci_dev == NULL)
648             return NULL;
649     } while (count--);
650
651     if (pci_enable_device(pci_dev))
652         return NULL;
653     
654     fore200e = fore200e_kmalloc(sizeof(struct fore200e), GFP_KERNEL);
655     if (fore200e == NULL)
656         return NULL;
657
658     fore200e->bus       = bus;
659     fore200e->bus_dev   = pci_dev;    
660     fore200e->irq       = pci_dev->irq;
661     fore200e->phys_base = pci_resource_start(pci_dev, 0);
662
663     sprintf(fore200e->name, "%s-%d", bus->model_name, index - 1);
664
665     pci_set_master(pci_dev);
666
667     return fore200e;
668 }
669
670
671 static int __init
672 fore200e_pca_prom_read(struct fore200e* fore200e, struct prom_data* prom)
673 {
674     struct host_cmdq*       cmdq  = &fore200e->host_cmdq;
675     struct host_cmdq_entry* entry = &cmdq->host_entry[ cmdq->head ];
676     struct prom_opcode      opcode;
677     int                     ok;
678     u32                     prom_dma;
679
680     FORE200E_NEXT_ENTRY(cmdq->head, QUEUE_SIZE_CMD);
681
682     opcode.opcode = OPCODE_GET_PROM;
683     opcode.pad    = 0;
684
685     prom_dma = fore200e->bus->dma_map(fore200e, prom, sizeof(struct prom_data), FORE200E_DMA_FROMDEVICE);
686
687     fore200e->bus->write(prom_dma, &entry->cp_entry->cmd.prom_block.prom_haddr);
688     
689     *entry->status = STATUS_PENDING;
690
691     fore200e->bus->write(*(u32*)&opcode, (u32*)&entry->cp_entry->cmd.prom_block.opcode);
692
693     ok = fore200e_poll(fore200e, entry->status, STATUS_COMPLETE, 400);
694
695     *entry->status = STATUS_FREE;
696
697     fore200e->bus->dma_unmap(fore200e, prom_dma, sizeof(struct prom_data), FORE200E_DMA_FROMDEVICE);
698
699     if (ok == 0) {
700         printk(FORE200E "unable to get PROM data from device %s\n", fore200e->name);
701         return -EIO;
702     }
703
704 #if defined(__BIG_ENDIAN)
705     
706 #define swap_here(addr) (*((u32*)(addr)) = swab32( *((u32*)(addr)) ))
707
708     /* MAC address is stored as little-endian */
709     swap_here(&prom->mac_addr[0]);
710     swap_here(&prom->mac_addr[4]);
711 #endif
712     
713     return 0;
714 }
715
716
717 static int
718 fore200e_pca_proc_read(struct fore200e* fore200e, char *page)
719 {
720     struct pci_dev* pci_dev = (struct pci_dev*)fore200e->bus_dev;
721
722     return sprintf(page, "   PCI bus/slot/function:\t%d/%d/%d\n",
723                    pci_dev->bus->number, PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn));
724 }
725
726 #endif /* CONFIG_ATM_FORE200E_PCA */
727
728
729 #ifdef CONFIG_ATM_FORE200E_SBA
730
731 static u32
732 fore200e_sba_read(volatile u32* addr)
733 {
734     return sbus_readl(addr);
735 }
736
737
738 static void
739 fore200e_sba_write(u32 val, volatile u32* addr)
740 {
741     sbus_writel(val, addr);
742 }
743
744
745 static u32
746 fore200e_sba_dma_map(struct fore200e* fore200e, void* virt_addr, int size, int direction)
747 {
748     u32 dma_addr = sbus_map_single((struct sbus_dev*)fore200e->bus_dev, virt_addr, size, direction);
749
750     DPRINTK(3, "SBUS DVMA mapping: virt_addr = 0x%p, size = %d, direction = %d --> dma_addr = 0x%08x\n",
751             virt_addr, size, direction, dma_addr);
752     
753     return dma_addr;
754 }
755
756
757 static void
758 fore200e_sba_dma_unmap(struct fore200e* fore200e, u32 dma_addr, int size, int direction)
759 {
760     DPRINTK(3, "SBUS DVMA unmapping: dma_addr = 0x%08x, size = %d, direction = %d,\n",
761             dma_addr, size, direction);
762
763     sbus_unmap_single((struct sbus_dev*)fore200e->bus_dev, dma_addr, size, direction);
764 }
765
766
767 static void
768 fore200e_sba_dma_sync_for_cpu(struct fore200e* fore200e, u32 dma_addr, int size, int direction)
769 {
770     DPRINTK(3, "SBUS DVMA sync: dma_addr = 0x%08x, size = %d, direction = %d\n", dma_addr, size, direction);
771     
772     sbus_dma_sync_single_for_cpu((struct sbus_dev*)fore200e->bus_dev, dma_addr, size, direction);
773 }
774
775 static void
776 fore200e_sba_dma_sync_for_device(struct fore200e* fore200e, u32 dma_addr, int size, int direction)
777 {
778     DPRINTK(3, "SBUS DVMA sync: dma_addr = 0x%08x, size = %d, direction = %d\n", dma_addr, size, direction);
779
780     sbus_dma_sync_single_for_device((struct sbus_dev*)fore200e->bus_dev, dma_addr, size, direction);
781 }
782
783
784 /* allocate a DVMA consistent chunk of memory intended to act as a communication mechanism
785    (to hold descriptors, status, queues, etc.) shared by the driver and the adapter */
786
787 static int
788 fore200e_sba_dma_chunk_alloc(struct fore200e* fore200e, struct chunk* chunk,
789                              int size, int nbr, int alignment)
790 {
791     chunk->alloc_size = chunk->align_size = size * nbr;
792
793     /* returned chunks are page-aligned */
794     chunk->alloc_addr = sbus_alloc_consistent((struct sbus_dev*)fore200e->bus_dev,
795                                               chunk->alloc_size,
796                                               &chunk->dma_addr);
797
798     if ((chunk->alloc_addr == NULL) || (chunk->dma_addr == 0))
799         return -ENOMEM;
800
801     chunk->align_addr = chunk->alloc_addr;
802     
803     return 0;
804 }
805
806
807 /* free a DVMA consistent chunk of memory */
808
809 static void
810 fore200e_sba_dma_chunk_free(struct fore200e* fore200e, struct chunk* chunk)
811 {
812     sbus_free_consistent((struct sbus_dev*)fore200e->bus_dev,
813                          chunk->alloc_size,
814                          chunk->alloc_addr,
815                          chunk->dma_addr);
816 }
817
818
819 static void
820 fore200e_sba_irq_enable(struct fore200e* fore200e)
821 {
822     u32 hcr = fore200e->bus->read(fore200e->regs.sba.hcr) & SBA200E_HCR_STICKY;
823     fore200e->bus->write(hcr | SBA200E_HCR_INTR_ENA, fore200e->regs.sba.hcr);
824 }
825
826
827 static int
828 fore200e_sba_irq_check(struct fore200e* fore200e)
829 {
830     return fore200e->bus->read(fore200e->regs.sba.hcr) & SBA200E_HCR_INTR_REQ;
831 }
832
833
834 static void
835 fore200e_sba_irq_ack(struct fore200e* fore200e)
836 {
837     u32 hcr = fore200e->bus->read(fore200e->regs.sba.hcr) & SBA200E_HCR_STICKY;
838     fore200e->bus->write(hcr | SBA200E_HCR_INTR_CLR, fore200e->regs.sba.hcr);
839 }
840
841
842 static void
843 fore200e_sba_reset(struct fore200e* fore200e)
844 {
845     fore200e->bus->write(SBA200E_HCR_RESET, fore200e->regs.sba.hcr);
846     fore200e_spin(10);
847     fore200e->bus->write(0, fore200e->regs.sba.hcr);
848 }
849
850
851 static int __init
852 fore200e_sba_map(struct fore200e* fore200e)
853 {
854     struct sbus_dev* sbus_dev = (struct sbus_dev*)fore200e->bus_dev;
855     unsigned int bursts;
856
857     /* gain access to the SBA specific registers  */
858     fore200e->regs.sba.hcr = (u32*)sbus_ioremap(&sbus_dev->resource[0], 0, SBA200E_HCR_LENGTH, "SBA HCR");
859     fore200e->regs.sba.bsr = (u32*)sbus_ioremap(&sbus_dev->resource[1], 0, SBA200E_BSR_LENGTH, "SBA BSR");
860     fore200e->regs.sba.isr = (u32*)sbus_ioremap(&sbus_dev->resource[2], 0, SBA200E_ISR_LENGTH, "SBA ISR");
861     fore200e->virt_base    = (u32*)sbus_ioremap(&sbus_dev->resource[3], 0, SBA200E_RAM_LENGTH, "SBA RAM");
862
863     if (fore200e->virt_base == NULL) {
864         printk(FORE200E "unable to map RAM of device %s\n", fore200e->name);
865         return -EFAULT;
866     }
867
868     DPRINTK(1, "device %s mapped to 0x%p\n", fore200e->name, fore200e->virt_base);
869     
870     fore200e->bus->write(0x02, fore200e->regs.sba.isr); /* XXX hardwired interrupt level */
871
872     /* get the supported DVMA burst sizes */
873     bursts = prom_getintdefault(sbus_dev->bus->prom_node, "burst-sizes", 0x00);
874
875     if (sbus_can_dma_64bit(sbus_dev))
876         sbus_set_sbus64(sbus_dev, bursts);
877
878     fore200e->state = FORE200E_STATE_MAP;
879     return 0;
880 }
881
882
883 static void
884 fore200e_sba_unmap(struct fore200e* fore200e)
885 {
886     sbus_iounmap((ulong)fore200e->regs.sba.hcr, SBA200E_HCR_LENGTH);
887     sbus_iounmap((ulong)fore200e->regs.sba.bsr, SBA200E_BSR_LENGTH);
888     sbus_iounmap((ulong)fore200e->regs.sba.isr, SBA200E_ISR_LENGTH);
889     sbus_iounmap((ulong)fore200e->virt_base,    SBA200E_RAM_LENGTH);
890 }
891
892
893 static int __init
894 fore200e_sba_configure(struct fore200e* fore200e)
895 {
896     fore200e->state = FORE200E_STATE_CONFIGURE;
897     return 0;
898 }
899
900
901 static struct fore200e* __init
902 fore200e_sba_detect(const struct fore200e_bus* bus, int index)
903 {
904     struct fore200e*          fore200e;
905     struct sbus_bus* sbus_bus;
906     struct sbus_dev* sbus_dev = NULL;
907     
908     unsigned int     count = 0;
909     
910     for_each_sbus (sbus_bus) {
911         for_each_sbusdev (sbus_dev, sbus_bus) {
912             if (strcmp(sbus_dev->prom_name, SBA200E_PROM_NAME) == 0) {
913                 if (count >= index)
914                     goto found;
915                 count++;
916             }
917         }
918     }
919     return NULL;
920     
921   found:
922     if (sbus_dev->num_registers != 4) {
923         printk(FORE200E "this %s device has %d instead of 4 registers\n",
924                bus->model_name, sbus_dev->num_registers);
925         return NULL;
926     }
927
928     fore200e = fore200e_kmalloc(sizeof(struct fore200e), GFP_KERNEL);
929     if (fore200e == NULL)
930         return NULL;
931
932     fore200e->bus     = bus;
933     fore200e->bus_dev = sbus_dev;
934     fore200e->irq     = sbus_dev->irqs[ 0 ];
935
936     fore200e->phys_base = (unsigned long)sbus_dev;
937
938     sprintf(fore200e->name, "%s-%d", bus->model_name, index - 1);
939     
940     return fore200e;
941 }
942
943
944 static int __init
945 fore200e_sba_prom_read(struct fore200e* fore200e, struct prom_data* prom)
946 {
947     struct sbus_dev* sbus_dev = (struct sbus_dev*) fore200e->bus_dev;
948     int                       len;
949
950     len = prom_getproperty(sbus_dev->prom_node, "macaddrlo2", &prom->mac_addr[ 4 ], 4);
951     if (len < 0)
952         return -EBUSY;
953
954     len = prom_getproperty(sbus_dev->prom_node, "macaddrhi4", &prom->mac_addr[ 2 ], 4);
955     if (len < 0)
956         return -EBUSY;
957     
958     prom_getproperty(sbus_dev->prom_node, "serialnumber",
959                      (char*)&prom->serial_number, sizeof(prom->serial_number));
960     
961     prom_getproperty(sbus_dev->prom_node, "promversion",
962                      (char*)&prom->hw_revision, sizeof(prom->hw_revision));
963     
964     return 0;
965 }
966
967
968 static int
969 fore200e_sba_proc_read(struct fore200e* fore200e, char *page)
970 {
971     struct sbus_dev* sbus_dev = (struct sbus_dev*)fore200e->bus_dev;
972
973     return sprintf(page, "   SBUS slot/device:\t\t%d/'%s'\n", sbus_dev->slot, sbus_dev->prom_name);
974 }
975 #endif /* CONFIG_ATM_FORE200E_SBA */
976
977
978 static void
979 fore200e_tx_irq(struct fore200e* fore200e)
980 {
981     struct host_txq*        txq = &fore200e->host_txq;
982     struct host_txq_entry*  entry;
983     struct atm_vcc*         vcc;
984     struct fore200e_vc_map* vc_map;
985
986     if (fore200e->host_txq.txing == 0)
987         return;
988
989     for (;;) {
990         
991         entry = &txq->host_entry[ txq->tail ];
992
993         if ((*entry->status & STATUS_COMPLETE) == 0) {
994             break;
995         }
996
997         DPRINTK(3, "TX COMPLETED: entry = %p [tail = %d], vc_map = %p, skb = %p\n", 
998                 entry, txq->tail, entry->vc_map, entry->skb);
999
1000         /* free copy of misaligned data */
1001         if (entry->data)
1002             kfree(entry->data);
1003         
1004         /* remove DMA mapping */
1005         fore200e->bus->dma_unmap(fore200e, entry->tpd->tsd[ 0 ].buffer, entry->tpd->tsd[ 0 ].length,
1006                                  FORE200E_DMA_TODEVICE);
1007
1008         vc_map = entry->vc_map;
1009
1010         /* vcc closed since the time the entry was submitted for tx? */
1011         if ((vc_map->vcc == NULL) ||
1012             (test_bit(ATM_VF_READY, &vc_map->vcc->flags) == 0)) {
1013
1014             DPRINTK(1, "no ready vcc found for PDU sent on device %d\n",
1015                     fore200e->atm_dev->number);
1016
1017             dev_kfree_skb_any(entry->skb);
1018         }
1019         else {
1020             ASSERT(vc_map->vcc);
1021
1022             /* vcc closed then immediately re-opened? */
1023             if (vc_map->incarn != entry->incarn) {
1024
1025                 /* when a vcc is closed, some PDUs may be still pending in the tx queue.
1026                    if the same vcc is immediately re-opened, those pending PDUs must
1027                    not be popped after the completion of their emission, as they refer
1028                    to the prior incarnation of that vcc. otherwise, vcc->sk->sk_wmem_alloc
1029                    would be decremented by the size of the (unrelated) skb, possibly
1030                    leading to a negative sk->sk_wmem_alloc count, ultimately freezing the vcc.
1031                    we thus bind the tx entry to the current incarnation of the vcc
1032                    when the entry is submitted for tx. When the tx later completes,
1033                    if the incarnation number of the tx entry does not match the one
1034                    of the vcc, then this implies that the vcc has been closed then re-opened.
1035                    we thus just drop the skb here. */
1036
1037                 DPRINTK(1, "vcc closed-then-re-opened; dropping PDU sent on device %d\n",
1038                         fore200e->atm_dev->number);
1039
1040                 dev_kfree_skb_any(entry->skb);
1041             }
1042             else {
1043                 vcc = vc_map->vcc;
1044                 ASSERT(vcc);
1045
1046                 /* notify tx completion */
1047                 if (vcc->pop) {
1048                     vcc->pop(vcc, entry->skb);
1049                 }
1050                 else {
1051                     dev_kfree_skb_any(entry->skb);
1052                 }
1053 #if 1
1054                 /* race fixed by the above incarnation mechanism, but... */
1055                 if (atomic_read(&vcc->sk->sk_wmem_alloc) < 0) {
1056                     atomic_set(&vcc->sk->sk_wmem_alloc, 0);
1057                 }
1058 #endif
1059                 /* check error condition */
1060                 if (*entry->status & STATUS_ERROR)
1061                     atomic_inc(&vcc->stats->tx_err);
1062                 else
1063                     atomic_inc(&vcc->stats->tx);
1064             }
1065         }
1066
1067         *entry->status = STATUS_FREE;
1068
1069         fore200e->host_txq.txing--;
1070
1071         FORE200E_NEXT_ENTRY(txq->tail, QUEUE_SIZE_TX);
1072     }
1073 }
1074
1075
1076 #ifdef FORE200E_BSQ_DEBUG
1077 int bsq_audit(int where, struct host_bsq* bsq, int scheme, int magn)
1078 {
1079     struct buffer* buffer;
1080     int count = 0;
1081
1082     buffer = bsq->freebuf;
1083     while (buffer) {
1084
1085         if (buffer->supplied) {
1086             printk(FORE200E "bsq_audit(%d): queue %d.%d, buffer %ld supplied but in free list!\n",
1087                    where, scheme, magn, buffer->index);
1088         }
1089
1090         if (buffer->magn != magn) {
1091             printk(FORE200E "bsq_audit(%d): queue %d.%d, buffer %ld, unexpected magn = %d\n",
1092                    where, scheme, magn, buffer->index, buffer->magn);
1093         }
1094
1095         if (buffer->scheme != scheme) {
1096             printk(FORE200E "bsq_audit(%d): queue %d.%d, buffer %ld, unexpected scheme = %d\n",
1097                    where, scheme, magn, buffer->index, buffer->scheme);
1098         }
1099
1100         if ((buffer->index < 0) || (buffer->index >= fore200e_rx_buf_nbr[ scheme ][ magn ])) {
1101             printk(FORE200E "bsq_audit(%d): queue %d.%d, out of range buffer index = %ld !\n",
1102                    where, scheme, magn, buffer->index);
1103         }
1104
1105         count++;
1106         buffer = buffer->next;
1107     }
1108
1109     if (count != bsq->freebuf_count) {
1110         printk(FORE200E "bsq_audit(%d): queue %d.%d, %d bufs in free list, but freebuf_count = %d\n",
1111                where, scheme, magn, count, bsq->freebuf_count);
1112     }
1113     return 0;
1114 }
1115 #endif
1116
1117
1118 static void
1119 fore200e_supply(struct fore200e* fore200e)
1120 {
1121     int  scheme, magn, i;
1122
1123     struct host_bsq*       bsq;
1124     struct host_bsq_entry* entry;
1125     struct buffer*         buffer;
1126
1127     for (scheme = 0; scheme < BUFFER_SCHEME_NBR; scheme++) {
1128         for (magn = 0; magn < BUFFER_MAGN_NBR; magn++) {
1129
1130             bsq = &fore200e->host_bsq[ scheme ][ magn ];
1131
1132 #ifdef FORE200E_BSQ_DEBUG
1133             bsq_audit(1, bsq, scheme, magn);
1134 #endif
1135             while (bsq->freebuf_count >= RBD_BLK_SIZE) {
1136
1137                 DPRINTK(2, "supplying %d rx buffers to queue %d / %d, freebuf_count = %d\n",
1138                         RBD_BLK_SIZE, scheme, magn, bsq->freebuf_count);
1139
1140                 entry = &bsq->host_entry[ bsq->head ];
1141
1142                 for (i = 0; i < RBD_BLK_SIZE; i++) {
1143
1144                     /* take the first buffer in the free buffer list */
1145                     buffer = bsq->freebuf;
1146                     if (!buffer) {
1147                         printk(FORE200E "no more free bufs in queue %d.%d, but freebuf_count = %d\n",
1148                                scheme, magn, bsq->freebuf_count);
1149                         return;
1150                     }
1151                     bsq->freebuf = buffer->next;
1152                     
1153 #ifdef FORE200E_BSQ_DEBUG
1154                     if (buffer->supplied)
1155                         printk(FORE200E "queue %d.%d, buffer %lu already supplied\n",
1156                                scheme, magn, buffer->index);
1157                     buffer->supplied = 1;
1158 #endif
1159                     entry->rbd_block->rbd[ i ].buffer_haddr = buffer->data.dma_addr;
1160                     entry->rbd_block->rbd[ i ].handle       = FORE200E_BUF2HDL(buffer);
1161                 }
1162
1163                 FORE200E_NEXT_ENTRY(bsq->head, QUEUE_SIZE_BS);
1164
1165                 /* decrease accordingly the number of free rx buffers */
1166                 bsq->freebuf_count -= RBD_BLK_SIZE;
1167
1168                 *entry->status = STATUS_PENDING;
1169                 fore200e->bus->write(entry->rbd_block_dma, &entry->cp_entry->rbd_block_haddr);
1170             }
1171         }
1172     }
1173 }
1174
1175
1176 static int
1177 fore200e_push_rpd(struct fore200e* fore200e, struct atm_vcc* vcc, struct rpd* rpd)
1178 {
1179     struct sk_buff*      skb;
1180     struct buffer*       buffer;
1181     struct fore200e_vcc* fore200e_vcc;
1182     int                  i, pdu_len = 0;
1183 #ifdef FORE200E_52BYTE_AAL0_SDU
1184     u32                  cell_header = 0;
1185 #endif
1186
1187     ASSERT(vcc);
1188     
1189     fore200e_vcc = FORE200E_VCC(vcc);
1190     ASSERT(fore200e_vcc);
1191
1192 #ifdef FORE200E_52BYTE_AAL0_SDU
1193     if ((vcc->qos.aal == ATM_AAL0) && (vcc->qos.rxtp.max_sdu == ATM_AAL0_SDU)) {
1194
1195         cell_header = (rpd->atm_header.gfc << ATM_HDR_GFC_SHIFT) |
1196                       (rpd->atm_header.vpi << ATM_HDR_VPI_SHIFT) |
1197                       (rpd->atm_header.vci << ATM_HDR_VCI_SHIFT) |
1198                       (rpd->atm_header.plt << ATM_HDR_PTI_SHIFT) | 
1199                        rpd->atm_header.clp;
1200         pdu_len = 4;
1201     }
1202 #endif
1203     
1204     /* compute total PDU length */
1205     for (i = 0; i < rpd->nseg; i++)
1206         pdu_len += rpd->rsd[ i ].length;
1207     
1208     skb = alloc_skb(pdu_len, GFP_ATOMIC);
1209     if (skb == NULL) {
1210         DPRINTK(2, "unable to alloc new skb, rx PDU length = %d\n", pdu_len);
1211
1212         atomic_inc(&vcc->stats->rx_drop);
1213         return -ENOMEM;
1214     } 
1215
1216     do_gettimeofday(&skb->stamp);
1217     
1218 #ifdef FORE200E_52BYTE_AAL0_SDU
1219     if (cell_header) {
1220         *((u32*)skb_put(skb, 4)) = cell_header;
1221     }
1222 #endif
1223
1224     /* reassemble segments */
1225     for (i = 0; i < rpd->nseg; i++) {
1226         
1227         /* rebuild rx buffer address from rsd handle */
1228         buffer = FORE200E_HDL2BUF(rpd->rsd[ i ].handle);
1229         
1230         /* Make device DMA transfer visible to CPU.  */
1231         fore200e->bus->dma_sync_for_cpu(fore200e, buffer->data.dma_addr, rpd->rsd[ i ].length, FORE200E_DMA_FROMDEVICE);
1232         
1233         memcpy(skb_put(skb, rpd->rsd[ i ].length), buffer->data.align_addr, rpd->rsd[ i ].length);
1234
1235         /* Now let the device get at it again.  */
1236         fore200e->bus->dma_sync_for_device(fore200e, buffer->data.dma_addr, rpd->rsd[ i ].length, FORE200E_DMA_FROMDEVICE);
1237     }
1238
1239     DPRINTK(3, "rx skb: len = %d, truesize = %d\n", skb->len, skb->truesize);
1240     
1241     if (pdu_len < fore200e_vcc->rx_min_pdu)
1242         fore200e_vcc->rx_min_pdu = pdu_len;
1243     if (pdu_len > fore200e_vcc->rx_max_pdu)
1244         fore200e_vcc->rx_max_pdu = pdu_len;
1245     fore200e_vcc->rx_pdu++;
1246
1247     /* push PDU */
1248     if (atm_charge(vcc, skb->truesize) == 0) {
1249
1250         DPRINTK(2, "receive buffers saturated for %d.%d.%d - PDU dropped\n",
1251                 vcc->itf, vcc->vpi, vcc->vci);
1252
1253         dev_kfree_skb_any(skb);
1254
1255         atomic_inc(&vcc->stats->rx_drop);
1256         return -ENOMEM;
1257     }
1258
1259     ASSERT(atomic_read(&vcc->sk->sk_wmem_alloc) >= 0);
1260
1261     vcc->push(vcc, skb);
1262     atomic_inc(&vcc->stats->rx);
1263
1264     ASSERT(atomic_read(&vcc->sk->sk_wmem_alloc) >= 0);
1265
1266     return 0;
1267 }
1268
1269
1270 static void
1271 fore200e_collect_rpd(struct fore200e* fore200e, struct rpd* rpd)
1272 {
1273     struct host_bsq* bsq;
1274     struct buffer*   buffer;
1275     int              i;
1276     
1277     for (i = 0; i < rpd->nseg; i++) {
1278
1279         /* rebuild rx buffer address from rsd handle */
1280         buffer = FORE200E_HDL2BUF(rpd->rsd[ i ].handle);
1281
1282         bsq = &fore200e->host_bsq[ buffer->scheme ][ buffer->magn ];
1283
1284 #ifdef FORE200E_BSQ_DEBUG
1285         bsq_audit(2, bsq, buffer->scheme, buffer->magn);
1286
1287         if (buffer->supplied == 0)
1288             printk(FORE200E "queue %d.%d, buffer %ld was not supplied\n",
1289                    buffer->scheme, buffer->magn, buffer->index);
1290         buffer->supplied = 0;
1291 #endif
1292
1293         /* re-insert the buffer into the free buffer list */
1294         buffer->next = bsq->freebuf;
1295         bsq->freebuf = buffer;
1296
1297         /* then increment the number of free rx buffers */
1298         bsq->freebuf_count++;
1299     }
1300 }
1301
1302
1303 static void
1304 fore200e_rx_irq(struct fore200e* fore200e)
1305 {
1306     struct host_rxq*        rxq = &fore200e->host_rxq;
1307     struct host_rxq_entry*  entry;
1308     struct atm_vcc*         vcc;
1309     struct fore200e_vc_map* vc_map;
1310
1311     for (;;) {
1312         
1313         entry = &rxq->host_entry[ rxq->head ];
1314
1315         /* no more received PDUs */
1316         if ((*entry->status & STATUS_COMPLETE) == 0)
1317             break;
1318
1319         vc_map = FORE200E_VC_MAP(fore200e, entry->rpd->atm_header.vpi, entry->rpd->atm_header.vci);
1320
1321         if ((vc_map->vcc == NULL) ||
1322             (test_bit(ATM_VF_READY, &vc_map->vcc->flags) == 0)) {
1323
1324             DPRINTK(1, "no ready VC found for PDU received on %d.%d.%d\n",
1325                     fore200e->atm_dev->number,
1326                     entry->rpd->atm_header.vpi, entry->rpd->atm_header.vci);
1327         }
1328         else {
1329             vcc = vc_map->vcc;
1330             ASSERT(vcc);
1331
1332             if ((*entry->status & STATUS_ERROR) == 0) {
1333
1334                 fore200e_push_rpd(fore200e, vcc, entry->rpd);
1335             }
1336             else {
1337                 DPRINTK(2, "damaged PDU on %d.%d.%d\n",
1338                         fore200e->atm_dev->number,
1339                         entry->rpd->atm_header.vpi, entry->rpd->atm_header.vci);
1340                 atomic_inc(&vcc->stats->rx_err);
1341             }
1342         }
1343
1344         FORE200E_NEXT_ENTRY(rxq->head, QUEUE_SIZE_RX);
1345
1346         fore200e_collect_rpd(fore200e, entry->rpd);
1347
1348         /* rewrite the rpd address to ack the received PDU */
1349         fore200e->bus->write(entry->rpd_dma, &entry->cp_entry->rpd_haddr);
1350         *entry->status = STATUS_FREE;
1351
1352         fore200e_supply(fore200e);
1353     }
1354 }
1355
1356
1357 #ifndef FORE200E_USE_TASKLET
1358 static void
1359 fore200e_irq(struct fore200e* fore200e)
1360 {
1361     unsigned long flags;
1362
1363     spin_lock_irqsave(&fore200e->q_lock, flags);
1364     fore200e_rx_irq(fore200e);
1365     spin_unlock_irqrestore(&fore200e->q_lock, flags);
1366
1367     spin_lock_irqsave(&fore200e->q_lock, flags);
1368     fore200e_tx_irq(fore200e);
1369     spin_unlock_irqrestore(&fore200e->q_lock, flags);
1370 }
1371 #endif
1372
1373
1374 static irqreturn_t
1375 fore200e_interrupt(int irq, void* dev, struct pt_regs* regs)
1376 {
1377     struct fore200e* fore200e = FORE200E_DEV((struct atm_dev*)dev);
1378
1379     if (fore200e->bus->irq_check(fore200e) == 0) {
1380         
1381         DPRINTK(3, "interrupt NOT triggered by device %d\n", fore200e->atm_dev->number);
1382         return IRQ_NONE;
1383     }
1384     DPRINTK(3, "interrupt triggered by device %d\n", fore200e->atm_dev->number);
1385
1386 #ifdef FORE200E_USE_TASKLET
1387     tasklet_schedule(&fore200e->tx_tasklet);
1388     tasklet_schedule(&fore200e->rx_tasklet);
1389 #else
1390     fore200e_irq(fore200e);
1391 #endif
1392     
1393     fore200e->bus->irq_ack(fore200e);
1394     return IRQ_HANDLED;
1395 }
1396
1397
1398 #ifdef FORE200E_USE_TASKLET
1399 static void
1400 fore200e_tx_tasklet(unsigned long data)
1401 {
1402     struct fore200e* fore200e = (struct fore200e*) data;
1403     unsigned long flags;
1404
1405     DPRINTK(3, "tx tasklet scheduled for device %d\n", fore200e->atm_dev->number);
1406
1407     spin_lock_irqsave(&fore200e->q_lock, flags);
1408     fore200e_tx_irq(fore200e);
1409     spin_unlock_irqrestore(&fore200e->q_lock, flags);
1410 }
1411
1412
1413 static void
1414 fore200e_rx_tasklet(unsigned long data)
1415 {
1416     struct fore200e* fore200e = (struct fore200e*) data;
1417     unsigned long    flags;
1418
1419     DPRINTK(3, "rx tasklet scheduled for device %d\n", fore200e->atm_dev->number);
1420
1421     spin_lock_irqsave(&fore200e->q_lock, flags);
1422     fore200e_rx_irq((struct fore200e*) data);
1423     spin_unlock_irqrestore(&fore200e->q_lock, flags);
1424 }
1425 #endif
1426
1427
1428 static int
1429 fore200e_select_scheme(struct atm_vcc* vcc)
1430 {
1431     /* fairly balance the VCs over (identical) buffer schemes */
1432     int scheme = vcc->vci % 2 ? BUFFER_SCHEME_ONE : BUFFER_SCHEME_TWO;
1433
1434     DPRINTK(1, "VC %d.%d.%d uses buffer scheme %d\n",
1435             vcc->itf, vcc->vpi, vcc->vci, scheme);
1436
1437     return scheme;
1438 }
1439
1440
1441 static int 
1442 fore200e_activate_vcin(struct fore200e* fore200e, int activate, struct atm_vcc* vcc, int mtu)
1443 {
1444     struct host_cmdq*        cmdq  = &fore200e->host_cmdq;
1445     struct host_cmdq_entry*  entry = &cmdq->host_entry[ cmdq->head ];
1446     struct activate_opcode   activ_opcode;
1447     struct deactivate_opcode deactiv_opcode;
1448     struct vpvc              vpvc;
1449     int                      ok;
1450     enum fore200e_aal        aal = fore200e_atm2fore_aal(vcc->qos.aal);
1451
1452     FORE200E_NEXT_ENTRY(cmdq->head, QUEUE_SIZE_CMD);
1453     
1454     if (activate) {
1455         FORE200E_VCC(vcc)->scheme = fore200e_select_scheme(vcc);
1456         
1457         activ_opcode.opcode = OPCODE_ACTIVATE_VCIN;
1458         activ_opcode.aal    = aal;
1459         activ_opcode.scheme = FORE200E_VCC(vcc)->scheme;
1460         activ_opcode.pad    = 0;
1461     }
1462     else {
1463         deactiv_opcode.opcode = OPCODE_DEACTIVATE_VCIN;
1464         deactiv_opcode.pad    = 0;
1465     }
1466
1467     vpvc.vci = vcc->vci;
1468     vpvc.vpi = vcc->vpi;
1469
1470     *entry->status = STATUS_PENDING;
1471
1472     if (activate) {
1473
1474 #ifdef FORE200E_52BYTE_AAL0_SDU
1475         mtu = 48;
1476 #endif
1477         /* the MTU is not used by the cp, except in the case of AAL0 */
1478         fore200e->bus->write(mtu,                        &entry->cp_entry->cmd.activate_block.mtu);
1479         fore200e->bus->write(*(u32*)&vpvc,         (u32*)&entry->cp_entry->cmd.activate_block.vpvc);
1480         fore200e->bus->write(*(u32*)&activ_opcode, (u32*)&entry->cp_entry->cmd.activate_block.opcode);
1481     }
1482     else {
1483         fore200e->bus->write(*(u32*)&vpvc,           (u32*)&entry->cp_entry->cmd.deactivate_block.vpvc);
1484         fore200e->bus->write(*(u32*)&deactiv_opcode, (u32*)&entry->cp_entry->cmd.deactivate_block.opcode);
1485     }
1486
1487     ok = fore200e_poll(fore200e, entry->status, STATUS_COMPLETE, 400);
1488
1489     *entry->status = STATUS_FREE;
1490
1491     if (ok == 0) {
1492         printk(FORE200E "unable to %s VC %d.%d.%d\n",
1493                activate ? "open" : "close", vcc->itf, vcc->vpi, vcc->vci);
1494         return -EIO;
1495     }
1496
1497     DPRINTK(1, "VC %d.%d.%d %sed\n", vcc->itf, vcc->vpi, vcc->vci, 
1498             activate ? "open" : "clos");
1499
1500     return 0;
1501 }
1502
1503
1504 #define FORE200E_MAX_BACK2BACK_CELLS 255    /* XXX depends on CDVT */
1505
1506 static void
1507 fore200e_rate_ctrl(struct atm_qos* qos, struct tpd_rate* rate)
1508 {
1509     if (qos->txtp.max_pcr < ATM_OC3_PCR) {
1510     
1511         /* compute the data cells to idle cells ratio from the tx PCR */
1512         rate->data_cells = qos->txtp.max_pcr * FORE200E_MAX_BACK2BACK_CELLS / ATM_OC3_PCR;
1513         rate->idle_cells = FORE200E_MAX_BACK2BACK_CELLS - rate->data_cells;
1514     }
1515     else {
1516         /* disable rate control */
1517         rate->data_cells = rate->idle_cells = 0;
1518     }
1519 }
1520
1521
1522 static int
1523 fore200e_open(struct atm_vcc *vcc)
1524 {
1525     struct fore200e*        fore200e = FORE200E_DEV(vcc->dev);
1526     struct fore200e_vcc*    fore200e_vcc;
1527     struct fore200e_vc_map* vc_map;
1528     unsigned long           flags;
1529     int                     vci = vcc->vci;
1530     short                   vpi = vcc->vpi;
1531
1532     ASSERT((vpi >= 0) && (vpi < 1<<FORE200E_VPI_BITS));
1533     ASSERT((vci >= 0) && (vci < 1<<FORE200E_VCI_BITS));
1534
1535     spin_lock_irqsave(&fore200e->q_lock, flags);
1536
1537     vc_map = FORE200E_VC_MAP(fore200e, vpi, vci);
1538     if (vc_map->vcc) {
1539
1540         spin_unlock_irqrestore(&fore200e->q_lock, flags);
1541
1542         printk(FORE200E "VC %d.%d.%d already in use\n",
1543                fore200e->atm_dev->number, vpi, vci);
1544
1545         return -EINVAL;
1546     }
1547
1548     vc_map->vcc = vcc;
1549
1550     spin_unlock_irqrestore(&fore200e->q_lock, flags);
1551
1552     fore200e_vcc = fore200e_kmalloc(sizeof(struct fore200e_vcc), GFP_ATOMIC);
1553     if (fore200e_vcc == NULL) {
1554         vc_map->vcc = NULL;
1555         return -ENOMEM;
1556     }
1557
1558     DPRINTK(2, "opening %d.%d.%d:%d QoS = (tx: cl=%s, pcr=%d-%d, cdv=%d, max_sdu=%d; "
1559             "rx: cl=%s, pcr=%d-%d, cdv=%d, max_sdu=%d)\n",
1560             vcc->itf, vcc->vpi, vcc->vci, fore200e_atm2fore_aal(vcc->qos.aal),
1561             fore200e_traffic_class[ vcc->qos.txtp.traffic_class ],
1562             vcc->qos.txtp.min_pcr, vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_cdv, vcc->qos.txtp.max_sdu,
1563             fore200e_traffic_class[ vcc->qos.rxtp.traffic_class ],
1564             vcc->qos.rxtp.min_pcr, vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_cdv, vcc->qos.rxtp.max_sdu);
1565     
1566     /* pseudo-CBR bandwidth requested? */
1567     if ((vcc->qos.txtp.traffic_class == ATM_CBR) && (vcc->qos.txtp.max_pcr > 0)) {
1568         
1569         down(&fore200e->rate_sf);
1570         if (fore200e->available_cell_rate < vcc->qos.txtp.max_pcr) {
1571             up(&fore200e->rate_sf);
1572
1573             fore200e_kfree(fore200e_vcc);
1574             vc_map->vcc = NULL;
1575             return -EAGAIN;
1576         }
1577
1578         /* reserve bandwidth */
1579         fore200e->available_cell_rate -= vcc->qos.txtp.max_pcr;
1580         up(&fore200e->rate_sf);
1581     }
1582     
1583     vcc->itf = vcc->dev->number;
1584
1585     set_bit(ATM_VF_PARTIAL,&vcc->flags);
1586     set_bit(ATM_VF_ADDR, &vcc->flags);
1587
1588     vcc->dev_data = fore200e_vcc;
1589     
1590     if (fore200e_activate_vcin(fore200e, 1, vcc, vcc->qos.rxtp.max_sdu) < 0) {
1591
1592         vc_map->vcc = NULL;
1593
1594         clear_bit(ATM_VF_ADDR, &vcc->flags);
1595         clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1596
1597         vcc->dev_data = NULL;
1598
1599         fore200e->available_cell_rate += vcc->qos.txtp.max_pcr;
1600
1601         fore200e_kfree(fore200e_vcc);
1602         return -EINVAL;
1603     }
1604     
1605     /* compute rate control parameters */
1606     if ((vcc->qos.txtp.traffic_class == ATM_CBR) && (vcc->qos.txtp.max_pcr > 0)) {
1607         
1608         fore200e_rate_ctrl(&vcc->qos, &fore200e_vcc->rate);
1609         set_bit(ATM_VF_HASQOS, &vcc->flags);
1610
1611         DPRINTK(3, "tx on %d.%d.%d:%d, tx PCR = %d, rx PCR = %d, data_cells = %u, idle_cells = %u\n",
1612                 vcc->itf, vcc->vpi, vcc->vci, fore200e_atm2fore_aal(vcc->qos.aal),
1613                 vcc->qos.txtp.max_pcr, vcc->qos.rxtp.max_pcr, 
1614                 fore200e_vcc->rate.data_cells, fore200e_vcc->rate.idle_cells);
1615     }
1616     
1617     fore200e_vcc->tx_min_pdu = fore200e_vcc->rx_min_pdu = MAX_PDU_SIZE + 1;
1618     fore200e_vcc->tx_max_pdu = fore200e_vcc->rx_max_pdu = 0;
1619     fore200e_vcc->tx_pdu     = fore200e_vcc->rx_pdu     = 0;
1620
1621     /* new incarnation of the vcc */
1622     vc_map->incarn = ++fore200e->incarn_count;
1623
1624     /* VC unusable before this flag is set */
1625     set_bit(ATM_VF_READY, &vcc->flags);
1626
1627     return 0;
1628 }
1629
1630
1631 static void
1632 fore200e_close(struct atm_vcc* vcc)
1633 {
1634     struct fore200e*        fore200e = FORE200E_DEV(vcc->dev);
1635     struct fore200e_vcc*    fore200e_vcc;
1636     struct fore200e_vc_map* vc_map;
1637     unsigned long           flags;
1638
1639     ASSERT(vcc);
1640     ASSERT((vcc->vpi >= 0) && (vcc->vpi < 1<<FORE200E_VPI_BITS));
1641     ASSERT((vcc->vci >= 0) && (vcc->vci < 1<<FORE200E_VCI_BITS));
1642
1643     DPRINTK(2, "closing %d.%d.%d:%d\n", vcc->itf, vcc->vpi, vcc->vci, fore200e_atm2fore_aal(vcc->qos.aal));
1644
1645     clear_bit(ATM_VF_READY, &vcc->flags);
1646
1647     fore200e_activate_vcin(fore200e, 0, vcc, 0);
1648
1649     spin_lock_irqsave(&fore200e->q_lock, flags);
1650
1651     vc_map = FORE200E_VC_MAP(fore200e, vcc->vpi, vcc->vci);
1652
1653     /* the vc is no longer considered as "in use" by fore200e_open() */
1654     vc_map->vcc = NULL;
1655
1656     vcc->itf = vcc->vci = vcc->vpi = 0;
1657
1658     fore200e_vcc = FORE200E_VCC(vcc);
1659     FORE200E_VCC(vcc) = NULL;
1660
1661     spin_unlock_irqrestore(&fore200e->q_lock, flags);
1662
1663     /* release reserved bandwidth, if any */
1664     if ((vcc->qos.txtp.traffic_class == ATM_CBR) && (vcc->qos.txtp.max_pcr > 0)) {
1665
1666         down(&fore200e->rate_sf);
1667         fore200e->available_cell_rate += vcc->qos.txtp.max_pcr;
1668         up(&fore200e->rate_sf);
1669
1670         clear_bit(ATM_VF_HASQOS, &vcc->flags);
1671     }
1672
1673     clear_bit(ATM_VF_ADDR, &vcc->flags);
1674     clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1675
1676     ASSERT(fore200e_vcc);
1677     fore200e_kfree(fore200e_vcc);
1678 }
1679
1680
1681 static int
1682 fore200e_send(struct atm_vcc *vcc, struct sk_buff *skb)
1683 {
1684     struct fore200e*        fore200e     = FORE200E_DEV(vcc->dev);
1685     struct fore200e_vcc*    fore200e_vcc = FORE200E_VCC(vcc);
1686     struct fore200e_vc_map* vc_map;
1687     struct host_txq*        txq          = &fore200e->host_txq;
1688     struct host_txq_entry*  entry;
1689     struct tpd*             tpd;
1690     struct tpd_haddr        tpd_haddr;
1691     int                     retry        = CONFIG_ATM_FORE200E_TX_RETRY;
1692     int                     tx_copy      = 0;
1693     int                     tx_len       = skb->len;
1694     u32*                    cell_header  = NULL;
1695     unsigned char*          skb_data;
1696     int                     skb_len;
1697     unsigned char*          data;
1698     unsigned long           flags;
1699
1700     ASSERT(vcc);
1701     ASSERT(atomic_read(&vcc->sk->sk_wmem_alloc) >= 0);
1702     ASSERT(fore200e);
1703     ASSERT(fore200e_vcc);
1704
1705     if (!test_bit(ATM_VF_READY, &vcc->flags)) {
1706         DPRINTK(1, "VC %d.%d.%d not ready for tx\n", vcc->itf, vcc->vpi, vcc->vpi);
1707         dev_kfree_skb_any(skb);
1708         return -EINVAL;
1709     }
1710
1711 #ifdef FORE200E_52BYTE_AAL0_SDU
1712     if ((vcc->qos.aal == ATM_AAL0) && (vcc->qos.txtp.max_sdu == ATM_AAL0_SDU)) {
1713         cell_header = (u32*) skb->data;
1714         skb_data    = skb->data + 4;    /* skip 4-byte cell header */
1715         skb_len     = tx_len = skb->len  - 4;
1716
1717         DPRINTK(3, "user-supplied cell header = 0x%08x\n", *cell_header);
1718     }
1719     else 
1720 #endif
1721     {
1722         skb_data = skb->data;
1723         skb_len  = skb->len;
1724     }
1725     
1726     if (((unsigned long)skb_data) & 0x3) {
1727
1728         DPRINTK(2, "misaligned tx PDU on device %s\n", fore200e->name);
1729         tx_copy = 1;
1730         tx_len  = skb_len;
1731     }
1732
1733     if ((vcc->qos.aal == ATM_AAL0) && (skb_len % ATM_CELL_PAYLOAD)) {
1734
1735         /* this simply NUKES the PCA board */
1736         DPRINTK(2, "incomplete tx AAL0 PDU on device %s\n", fore200e->name);
1737         tx_copy = 1;
1738         tx_len  = ((skb_len / ATM_CELL_PAYLOAD) + 1) * ATM_CELL_PAYLOAD;
1739     }
1740     
1741     if (tx_copy) {
1742         data = kmalloc(tx_len, GFP_ATOMIC | GFP_DMA);
1743         if (data == NULL) {
1744             if (vcc->pop) {
1745                 vcc->pop(vcc, skb);
1746             }
1747             else {
1748                 dev_kfree_skb_any(skb);
1749             }
1750             return -ENOMEM;
1751         }
1752
1753         memcpy(data, skb_data, skb_len);
1754         if (skb_len < tx_len)
1755             memset(data + skb_len, 0x00, tx_len - skb_len);
1756     }
1757     else {
1758         data = skb_data;
1759     }
1760
1761     vc_map = FORE200E_VC_MAP(fore200e, vcc->vpi, vcc->vci);
1762     ASSERT(vc_map->vcc == vcc);
1763
1764   retry_here:
1765
1766     spin_lock_irqsave(&fore200e->q_lock, flags);
1767
1768     entry = &txq->host_entry[ txq->head ];
1769
1770     if ((*entry->status != STATUS_FREE) || (txq->txing >= QUEUE_SIZE_TX - 2)) {
1771
1772         /* try to free completed tx queue entries */
1773         fore200e_tx_irq(fore200e);
1774
1775         if (*entry->status != STATUS_FREE) {
1776
1777             spin_unlock_irqrestore(&fore200e->q_lock, flags);
1778
1779             /* retry once again? */
1780             if (--retry > 0) {
1781                 schedule();
1782                 goto retry_here;
1783             }
1784
1785             atomic_inc(&vcc->stats->tx_err);
1786
1787             fore200e->tx_sat++;
1788             DPRINTK(2, "tx queue of device %s is saturated, PDU dropped - heartbeat is %08x\n",
1789                     fore200e->name, fore200e->cp_queues->heartbeat);
1790             if (vcc->pop) {
1791                 vcc->pop(vcc, skb);
1792             }
1793             else {
1794                 dev_kfree_skb_any(skb);
1795             }
1796
1797             if (tx_copy)
1798                 kfree(data);
1799
1800             return -ENOBUFS;
1801         }
1802     }
1803
1804     entry->incarn = vc_map->incarn;
1805     entry->vc_map = vc_map;
1806     entry->skb    = skb;
1807     entry->data   = tx_copy ? data : NULL;
1808
1809     tpd = entry->tpd;
1810     tpd->tsd[ 0 ].buffer = fore200e->bus->dma_map(fore200e, data, tx_len, FORE200E_DMA_TODEVICE);
1811     tpd->tsd[ 0 ].length = tx_len;
1812
1813     FORE200E_NEXT_ENTRY(txq->head, QUEUE_SIZE_TX);
1814     txq->txing++;
1815
1816     /* The dma_map call above implies a dma_sync so the device can use it,
1817      * thus no explicit dma_sync call is necessary here.
1818      */
1819     
1820     DPRINTK(3, "tx on %d.%d.%d:%d, len = %u (%u)\n", 
1821             vcc->itf, vcc->vpi, vcc->vci, fore200e_atm2fore_aal(vcc->qos.aal),
1822             tpd->tsd[0].length, skb_len);
1823
1824     if (skb_len < fore200e_vcc->tx_min_pdu)
1825         fore200e_vcc->tx_min_pdu = skb_len;
1826     if (skb_len > fore200e_vcc->tx_max_pdu)
1827         fore200e_vcc->tx_max_pdu = skb_len;
1828     fore200e_vcc->tx_pdu++;
1829
1830     /* set tx rate control information */
1831     tpd->rate.data_cells = fore200e_vcc->rate.data_cells;
1832     tpd->rate.idle_cells = fore200e_vcc->rate.idle_cells;
1833
1834     if (cell_header) {
1835         tpd->atm_header.clp = (*cell_header & ATM_HDR_CLP);
1836         tpd->atm_header.plt = (*cell_header & ATM_HDR_PTI_MASK) >> ATM_HDR_PTI_SHIFT;
1837         tpd->atm_header.vci = (*cell_header & ATM_HDR_VCI_MASK) >> ATM_HDR_VCI_SHIFT;
1838         tpd->atm_header.vpi = (*cell_header & ATM_HDR_VPI_MASK) >> ATM_HDR_VPI_SHIFT;
1839         tpd->atm_header.gfc = (*cell_header & ATM_HDR_GFC_MASK) >> ATM_HDR_GFC_SHIFT;
1840     }
1841     else {
1842         /* set the ATM header, common to all cells conveying the PDU */
1843         tpd->atm_header.clp = 0;
1844         tpd->atm_header.plt = 0;
1845         tpd->atm_header.vci = vcc->vci;
1846         tpd->atm_header.vpi = vcc->vpi;
1847         tpd->atm_header.gfc = 0;
1848     }
1849
1850     tpd->spec.length = tx_len;
1851     tpd->spec.nseg   = 1;
1852     tpd->spec.aal    = fore200e_atm2fore_aal(vcc->qos.aal);
1853     tpd->spec.intr   = 1;
1854
1855     tpd_haddr.size  = sizeof(struct tpd) / (1<<TPD_HADDR_SHIFT);  /* size is expressed in 32 byte blocks */
1856     tpd_haddr.pad   = 0;
1857     tpd_haddr.haddr = entry->tpd_dma >> TPD_HADDR_SHIFT;          /* shift the address, as we are in a bitfield */
1858
1859     *entry->status = STATUS_PENDING;
1860     fore200e->bus->write(*(u32*)&tpd_haddr, (u32*)&entry->cp_entry->tpd_haddr);
1861
1862     spin_unlock_irqrestore(&fore200e->q_lock, flags);
1863
1864     return 0;
1865 }
1866
1867
1868 static int
1869 fore200e_getstats(struct fore200e* fore200e)
1870 {
1871     struct host_cmdq*       cmdq  = &fore200e->host_cmdq;
1872     struct host_cmdq_entry* entry = &cmdq->host_entry[ cmdq->head ];
1873     struct stats_opcode     opcode;
1874     int                     ok;
1875     u32                     stats_dma_addr;
1876
1877     if (fore200e->stats == NULL) {
1878         fore200e->stats = fore200e_kmalloc(sizeof(struct stats), GFP_KERNEL | GFP_DMA);
1879         if (fore200e->stats == NULL)
1880             return -ENOMEM;
1881     }
1882     
1883     stats_dma_addr = fore200e->bus->dma_map(fore200e, fore200e->stats,
1884                                             sizeof(struct stats), FORE200E_DMA_FROMDEVICE);
1885     
1886     FORE200E_NEXT_ENTRY(cmdq->head, QUEUE_SIZE_CMD);
1887
1888     opcode.opcode = OPCODE_GET_STATS;
1889     opcode.pad    = 0;
1890
1891     fore200e->bus->write(stats_dma_addr, &entry->cp_entry->cmd.stats_block.stats_haddr);
1892     
1893     *entry->status = STATUS_PENDING;
1894
1895     fore200e->bus->write(*(u32*)&opcode, (u32*)&entry->cp_entry->cmd.stats_block.opcode);
1896
1897     ok = fore200e_poll(fore200e, entry->status, STATUS_COMPLETE, 400);
1898
1899     *entry->status = STATUS_FREE;
1900
1901     fore200e->bus->dma_unmap(fore200e, stats_dma_addr, sizeof(struct stats), FORE200E_DMA_FROMDEVICE);
1902     
1903     if (ok == 0) {
1904         printk(FORE200E "unable to get statistics from device %s\n", fore200e->name);
1905         return -EIO;
1906     }
1907
1908     return 0;
1909 }
1910
1911
1912 static int
1913 fore200e_getsockopt(struct atm_vcc* vcc, int level, int optname, void* optval, int optlen)
1914 {
1915     /* struct fore200e* fore200e = FORE200E_DEV(vcc->dev); */
1916
1917     DPRINTK(2, "getsockopt %d.%d.%d, level = %d, optname = 0x%x, optval = 0x%p, optlen = %d\n",
1918             vcc->itf, vcc->vpi, vcc->vci, level, optname, optval, optlen);
1919
1920     return -EINVAL;
1921 }
1922
1923
1924 static int
1925 fore200e_setsockopt(struct atm_vcc* vcc, int level, int optname, void* optval, int optlen)
1926 {
1927     /* struct fore200e* fore200e = FORE200E_DEV(vcc->dev); */
1928     
1929     DPRINTK(2, "setsockopt %d.%d.%d, level = %d, optname = 0x%x, optval = 0x%p, optlen = %d\n",
1930             vcc->itf, vcc->vpi, vcc->vci, level, optname, optval, optlen);
1931     
1932     return -EINVAL;
1933 }
1934
1935
1936 #if 0 /* currently unused */
1937 static int
1938 fore200e_get_oc3(struct fore200e* fore200e, struct oc3_regs* regs)
1939 {
1940     struct host_cmdq*       cmdq  = &fore200e->host_cmdq;
1941     struct host_cmdq_entry* entry = &cmdq->host_entry[ cmdq->head ];
1942     struct oc3_opcode       opcode;
1943     int                     ok;
1944     u32                     oc3_regs_dma_addr;
1945
1946     oc3_regs_dma_addr = fore200e->bus->dma_map(fore200e, regs, sizeof(struct oc3_regs), FORE200E_DMA_FROMDEVICE);
1947
1948     FORE200E_NEXT_ENTRY(cmdq->head, QUEUE_SIZE_CMD);
1949
1950     opcode.opcode = OPCODE_GET_OC3;
1951     opcode.reg    = 0;
1952     opcode.value  = 0;
1953     opcode.mask   = 0;
1954
1955     fore200e->bus->write(oc3_regs_dma_addr, &entry->cp_entry->cmd.oc3_block.regs_haddr);
1956     
1957     *entry->status = STATUS_PENDING;
1958
1959     fore200e->bus->write(*(u32*)&opcode, (u32*)&entry->cp_entry->cmd.oc3_block.opcode);
1960
1961     ok = fore200e_poll(fore200e, entry->status, STATUS_COMPLETE, 400);
1962
1963     *entry->status = STATUS_FREE;
1964
1965     fore200e->bus->dma_unmap(fore200e, oc3_regs_dma_addr, sizeof(struct oc3_regs), FORE200E_DMA_FROMDEVICE);
1966     
1967     if (ok == 0) {
1968         printk(FORE200E "unable to get OC-3 regs of device %s\n", fore200e->name);
1969         return -EIO;
1970     }
1971
1972     return 0;
1973 }
1974 #endif
1975
1976
1977 static int
1978 fore200e_set_oc3(struct fore200e* fore200e, u32 reg, u32 value, u32 mask)
1979 {
1980     struct host_cmdq*       cmdq  = &fore200e->host_cmdq;
1981     struct host_cmdq_entry* entry = &cmdq->host_entry[ cmdq->head ];
1982     struct oc3_opcode       opcode;
1983     int                     ok;
1984
1985     DPRINTK(2, "set OC-3 reg = 0x%02x, value = 0x%02x, mask = 0x%02x\n", reg, value, mask);
1986
1987     FORE200E_NEXT_ENTRY(cmdq->head, QUEUE_SIZE_CMD);
1988
1989     opcode.opcode = OPCODE_SET_OC3;
1990     opcode.reg    = reg;
1991     opcode.value  = value;
1992     opcode.mask   = mask;
1993
1994     fore200e->bus->write(0, &entry->cp_entry->cmd.oc3_block.regs_haddr);
1995     
1996     *entry->status = STATUS_PENDING;
1997
1998     fore200e->bus->write(*(u32*)&opcode, (u32*)&entry->cp_entry->cmd.oc3_block.opcode);
1999
2000     ok = fore200e_poll(fore200e, entry->status, STATUS_COMPLETE, 400);
2001
2002     *entry->status = STATUS_FREE;
2003
2004     if (ok == 0) {
2005         printk(FORE200E "unable to set OC-3 reg 0x%02x of device %s\n", reg, fore200e->name);
2006         return -EIO;
2007     }
2008
2009     return 0;
2010 }
2011
2012
2013 static int
2014 fore200e_setloop(struct fore200e* fore200e, int loop_mode)
2015 {
2016     u32 mct_value, mct_mask;
2017     int error;
2018
2019     if (!capable(CAP_NET_ADMIN))
2020         return -EPERM;
2021     
2022     switch (loop_mode) {
2023
2024     case ATM_LM_NONE:
2025         mct_value = 0; 
2026         mct_mask  = SUNI_MCT_DLE | SUNI_MCT_LLE;
2027         break;
2028         
2029     case ATM_LM_LOC_PHY:
2030         mct_value = mct_mask = SUNI_MCT_DLE;
2031         break;
2032
2033     case ATM_LM_RMT_PHY:
2034         mct_value = mct_mask = SUNI_MCT_LLE;
2035         break;
2036
2037     default:
2038         return -EINVAL;
2039     }
2040
2041     error = fore200e_set_oc3(fore200e, SUNI_MCT, mct_value, mct_mask);
2042     if (error == 0)
2043         fore200e->loop_mode = loop_mode;
2044
2045     return error;
2046 }
2047
2048
2049 static inline unsigned int
2050 fore200e_swap(unsigned int in)
2051 {
2052 #if defined(__LITTLE_ENDIAN)
2053     return swab32(in);
2054 #else
2055     return in;
2056 #endif
2057 }
2058
2059
2060 static int
2061 fore200e_fetch_stats(struct fore200e* fore200e, struct sonet_stats* arg)
2062 {
2063     struct sonet_stats tmp;
2064
2065     if (fore200e_getstats(fore200e) < 0)
2066         return -EIO;
2067
2068     tmp.section_bip = fore200e_swap(fore200e->stats->oc3.section_bip8_errors);
2069     tmp.line_bip    = fore200e_swap(fore200e->stats->oc3.line_bip24_errors);
2070     tmp.path_bip    = fore200e_swap(fore200e->stats->oc3.path_bip8_errors);
2071     tmp.line_febe   = fore200e_swap(fore200e->stats->oc3.line_febe_errors);
2072     tmp.path_febe   = fore200e_swap(fore200e->stats->oc3.path_febe_errors);
2073     tmp.corr_hcs    = fore200e_swap(fore200e->stats->oc3.corr_hcs_errors);
2074     tmp.uncorr_hcs  = fore200e_swap(fore200e->stats->oc3.ucorr_hcs_errors);
2075     tmp.tx_cells    = fore200e_swap(fore200e->stats->aal0.cells_transmitted)  +
2076                       fore200e_swap(fore200e->stats->aal34.cells_transmitted) +
2077                       fore200e_swap(fore200e->stats->aal5.cells_transmitted);
2078     tmp.rx_cells    = fore200e_swap(fore200e->stats->aal0.cells_received)     +
2079                       fore200e_swap(fore200e->stats->aal34.cells_received)    +
2080                       fore200e_swap(fore200e->stats->aal5.cells_received);
2081
2082     if (arg)
2083         return copy_to_user(arg, &tmp, sizeof(struct sonet_stats)) ? -EFAULT : 0;       
2084     
2085     return 0;
2086 }
2087
2088
2089 static int
2090 fore200e_ioctl(struct atm_dev* dev, unsigned int cmd, void* arg)
2091 {
2092     struct fore200e* fore200e = FORE200E_DEV(dev);
2093     
2094     DPRINTK(2, "ioctl cmd = 0x%x (%u), arg = 0x%p (%lu)\n", cmd, cmd, arg, (unsigned long)arg);
2095
2096     switch (cmd) {
2097
2098     case SONET_GETSTAT:
2099         return fore200e_fetch_stats(fore200e, (struct sonet_stats*)arg);
2100
2101     case SONET_GETDIAG:
2102         return put_user(0, (int*)arg) ? -EFAULT : 0;
2103
2104     case ATM_SETLOOP:
2105         return fore200e_setloop(fore200e, (int)(unsigned long)arg);
2106
2107     case ATM_GETLOOP:
2108         return put_user(fore200e->loop_mode, (int*)arg) ? -EFAULT : 0;
2109
2110     case ATM_QUERYLOOP:
2111         return put_user(ATM_LM_LOC_PHY | ATM_LM_RMT_PHY, (int*)arg) ? -EFAULT : 0;
2112     }
2113
2114     return -ENOSYS; /* not implemented */
2115 }
2116
2117
2118 static int
2119 fore200e_change_qos(struct atm_vcc* vcc,struct atm_qos* qos, int flags)
2120 {
2121     struct fore200e_vcc* fore200e_vcc = FORE200E_VCC(vcc);
2122     struct fore200e*     fore200e     = FORE200E_DEV(vcc->dev);
2123
2124     if (!test_bit(ATM_VF_READY, &vcc->flags)) {
2125         DPRINTK(1, "VC %d.%d.%d not ready for QoS change\n", vcc->itf, vcc->vpi, vcc->vpi);
2126         return -EINVAL;
2127     }
2128
2129     DPRINTK(2, "change_qos %d.%d.%d, "
2130             "(tx: cl=%s, pcr=%d-%d, cdv=%d, max_sdu=%d; "
2131             "rx: cl=%s, pcr=%d-%d, cdv=%d, max_sdu=%d), flags = 0x%x\n"
2132             "available_cell_rate = %u",
2133             vcc->itf, vcc->vpi, vcc->vci,
2134             fore200e_traffic_class[ qos->txtp.traffic_class ],
2135             qos->txtp.min_pcr, qos->txtp.max_pcr, qos->txtp.max_cdv, qos->txtp.max_sdu,
2136             fore200e_traffic_class[ qos->rxtp.traffic_class ],
2137             qos->rxtp.min_pcr, qos->rxtp.max_pcr, qos->rxtp.max_cdv, qos->rxtp.max_sdu,
2138             flags, fore200e->available_cell_rate);
2139
2140     if ((qos->txtp.traffic_class == ATM_CBR) && (qos->txtp.max_pcr > 0)) {
2141
2142         down(&fore200e->rate_sf);
2143         if (fore200e->available_cell_rate + vcc->qos.txtp.max_pcr < qos->txtp.max_pcr) {
2144             up(&fore200e->rate_sf);
2145             return -EAGAIN;
2146         }
2147
2148         fore200e->available_cell_rate += vcc->qos.txtp.max_pcr;
2149         fore200e->available_cell_rate -= qos->txtp.max_pcr;
2150
2151         up(&fore200e->rate_sf);
2152         
2153         memcpy(&vcc->qos, qos, sizeof(struct atm_qos));
2154         
2155         /* update rate control parameters */
2156         fore200e_rate_ctrl(qos, &fore200e_vcc->rate);
2157
2158         set_bit(ATM_VF_HASQOS, &vcc->flags);
2159
2160         return 0;
2161     }
2162     
2163     return -EINVAL;
2164 }
2165     
2166
2167 static int __init
2168 fore200e_irq_request(struct fore200e* fore200e)
2169 {
2170     if (request_irq(fore200e->irq, fore200e_interrupt, SA_SHIRQ, fore200e->name, fore200e->atm_dev) < 0) {
2171
2172         printk(FORE200E "unable to reserve IRQ %s for device %s\n",
2173                fore200e_irq_itoa(fore200e->irq), fore200e->name);
2174         return -EBUSY;
2175     }
2176
2177     printk(FORE200E "IRQ %s reserved for device %s\n",
2178            fore200e_irq_itoa(fore200e->irq), fore200e->name);
2179
2180 #ifdef FORE200E_USE_TASKLET
2181     tasklet_init(&fore200e->tx_tasklet, fore200e_tx_tasklet, (unsigned long)fore200e);
2182     tasklet_init(&fore200e->rx_tasklet, fore200e_rx_tasklet, (unsigned long)fore200e);
2183 #endif
2184
2185     fore200e->state = FORE200E_STATE_IRQ;
2186     return 0;
2187 }
2188
2189
2190 static int __init
2191 fore200e_get_esi(struct fore200e* fore200e)
2192 {
2193     struct prom_data* prom = fore200e_kmalloc(sizeof(struct prom_data), GFP_KERNEL | GFP_DMA);
2194     int ok, i;
2195
2196     if (!prom)
2197         return -ENOMEM;
2198
2199     ok = fore200e->bus->prom_read(fore200e, prom);
2200     if (ok < 0) {
2201         fore200e_kfree(prom);
2202         return -EBUSY;
2203     }
2204         
2205     printk(FORE200E "device %s, rev. %c, S/N: %d, ESI: %02x:%02x:%02x:%02x:%02x:%02x\n", 
2206            fore200e->name, 
2207            (prom->hw_revision & 0xFF) + '@',    /* probably meaningless with SBA boards */
2208            prom->serial_number & 0xFFFF,
2209            prom->mac_addr[ 2 ], prom->mac_addr[ 3 ], prom->mac_addr[ 4 ],
2210            prom->mac_addr[ 5 ], prom->mac_addr[ 6 ], prom->mac_addr[ 7 ]);
2211         
2212     for (i = 0; i < ESI_LEN; i++) {
2213         fore200e->esi[ i ] = fore200e->atm_dev->esi[ i ] = prom->mac_addr[ i + 2 ];
2214     }
2215     
2216     fore200e_kfree(prom);
2217
2218     return 0;
2219 }
2220
2221
2222 static int __init
2223 fore200e_alloc_rx_buf(struct fore200e* fore200e)
2224 {
2225     int scheme, magn, nbr, size, i;
2226
2227     struct host_bsq* bsq;
2228     struct buffer*   buffer;
2229
2230     for (scheme = 0; scheme < BUFFER_SCHEME_NBR; scheme++) {
2231         for (magn = 0; magn < BUFFER_MAGN_NBR; magn++) {
2232
2233             bsq = &fore200e->host_bsq[ scheme ][ magn ];
2234
2235             nbr  = fore200e_rx_buf_nbr[ scheme ][ magn ];
2236             size = fore200e_rx_buf_size[ scheme ][ magn ];
2237
2238             DPRINTK(2, "rx buffers %d / %d are being allocated\n", scheme, magn);
2239
2240             /* allocate the array of receive buffers */
2241             buffer = bsq->buffer = fore200e_kmalloc(nbr * sizeof(struct buffer), GFP_KERNEL);
2242
2243             if (buffer == NULL)
2244                 return -ENOMEM;
2245
2246             bsq->freebuf = NULL;
2247
2248             for (i = 0; i < nbr; i++) {
2249
2250                 buffer[ i ].scheme = scheme;
2251                 buffer[ i ].magn   = magn;
2252 #ifdef FORE200E_BSQ_DEBUG
2253                 buffer[ i ].index  = i;
2254                 buffer[ i ].supplied = 0;
2255 #endif
2256
2257                 /* allocate the receive buffer body */
2258                 if (fore200e_chunk_alloc(fore200e,
2259                                          &buffer[ i ].data, size, fore200e->bus->buffer_alignment,
2260                                          FORE200E_DMA_FROMDEVICE) < 0) {
2261                     
2262                     while (i > 0)
2263                         fore200e_chunk_free(fore200e, &buffer[ --i ].data);
2264                     fore200e_kfree(buffer);
2265                     
2266                     return -ENOMEM;
2267                 }
2268
2269                 /* insert the buffer into the free buffer list */
2270                 buffer[ i ].next = bsq->freebuf;
2271                 bsq->freebuf = &buffer[ i ];
2272             }
2273             /* all the buffers are free, initially */
2274             bsq->freebuf_count = nbr;
2275
2276 #ifdef FORE200E_BSQ_DEBUG
2277             bsq_audit(3, bsq, scheme, magn);
2278 #endif
2279         }
2280     }
2281
2282     fore200e->state = FORE200E_STATE_ALLOC_BUF;
2283     return 0;
2284 }
2285
2286
2287 static int __init
2288 fore200e_init_bs_queue(struct fore200e* fore200e)
2289 {
2290     int scheme, magn, i;
2291
2292     struct host_bsq*     bsq;
2293     struct cp_bsq_entry* cp_entry;
2294
2295     for (scheme = 0; scheme < BUFFER_SCHEME_NBR; scheme++) {
2296         for (magn = 0; magn < BUFFER_MAGN_NBR; magn++) {
2297
2298             DPRINTK(2, "buffer supply queue %d / %d is being initialized\n", scheme, magn);
2299
2300             bsq = &fore200e->host_bsq[ scheme ][ magn ];
2301
2302             /* allocate and align the array of status words */
2303             if (fore200e->bus->dma_chunk_alloc(fore200e,
2304                                                &bsq->status,
2305                                                sizeof(enum status), 
2306                                                QUEUE_SIZE_BS,
2307                                                fore200e->bus->status_alignment) < 0) {
2308                 return -ENOMEM;
2309             }
2310
2311             /* allocate and align the array of receive buffer descriptors */
2312             if (fore200e->bus->dma_chunk_alloc(fore200e,
2313                                                &bsq->rbd_block,
2314                                                sizeof(struct rbd_block),
2315                                                QUEUE_SIZE_BS,
2316                                                fore200e->bus->descr_alignment) < 0) {
2317                 
2318                 fore200e->bus->dma_chunk_free(fore200e, &bsq->status);
2319                 return -ENOMEM;
2320             }
2321             
2322             /* get the base address of the cp resident buffer supply queue entries */
2323             cp_entry = (struct cp_bsq_entry*)(fore200e->virt_base + 
2324                        fore200e->bus->read(&fore200e->cp_queues->cp_bsq[ scheme ][ magn ]));
2325             
2326             /* fill the host resident and cp resident buffer supply queue entries */
2327             for (i = 0; i < QUEUE_SIZE_BS; i++) {
2328                 
2329                 bsq->host_entry[ i ].status = 
2330                                      FORE200E_INDEX(bsq->status.align_addr, enum status, i);
2331                 bsq->host_entry[ i ].rbd_block =
2332                                      FORE200E_INDEX(bsq->rbd_block.align_addr, struct rbd_block, i);
2333                 bsq->host_entry[ i ].rbd_block_dma =
2334                                      FORE200E_DMA_INDEX(bsq->rbd_block.dma_addr, struct rbd_block, i);
2335                 bsq->host_entry[ i ].cp_entry = &cp_entry[ i ];
2336                 
2337                 *bsq->host_entry[ i ].status = STATUS_FREE;
2338                 
2339                 fore200e->bus->write(FORE200E_DMA_INDEX(bsq->status.dma_addr, enum status, i), 
2340                                      &cp_entry[ i ].status_haddr);
2341             }
2342         }
2343     }
2344
2345     fore200e->state = FORE200E_STATE_INIT_BSQ;
2346     return 0;
2347 }
2348
2349
2350 static int __init
2351 fore200e_init_rx_queue(struct fore200e* fore200e)
2352 {
2353     struct host_rxq*     rxq =  &fore200e->host_rxq;
2354     struct cp_rxq_entry* cp_entry;
2355     int i;
2356
2357     DPRINTK(2, "receive queue is being initialized\n");
2358
2359     /* allocate and align the array of status words */
2360     if (fore200e->bus->dma_chunk_alloc(fore200e,
2361                                        &rxq->status,
2362                                        sizeof(enum status), 
2363                                        QUEUE_SIZE_RX,
2364                                        fore200e->bus->status_alignment) < 0) {
2365         return -ENOMEM;
2366     }
2367
2368     /* allocate and align the array of receive PDU descriptors */
2369     if (fore200e->bus->dma_chunk_alloc(fore200e,
2370                                        &rxq->rpd,
2371                                        sizeof(struct rpd), 
2372                                        QUEUE_SIZE_RX,
2373                                        fore200e->bus->descr_alignment) < 0) {
2374         
2375         fore200e->bus->dma_chunk_free(fore200e, &rxq->status);
2376         return -ENOMEM;
2377     }
2378
2379     /* get the base address of the cp resident rx queue entries */
2380     cp_entry = (struct cp_rxq_entry*)(fore200e->virt_base + 
2381                                       fore200e->bus->read(&fore200e->cp_queues->cp_rxq));
2382
2383     /* fill the host resident and cp resident rx entries */
2384     for (i=0; i < QUEUE_SIZE_RX; i++) {
2385         
2386         rxq->host_entry[ i ].status = 
2387                              FORE200E_INDEX(rxq->status.align_addr, enum status, i);
2388         rxq->host_entry[ i ].rpd = 
2389                              FORE200E_INDEX(rxq->rpd.align_addr, struct rpd, i);
2390         rxq->host_entry[ i ].rpd_dma = 
2391                              FORE200E_DMA_INDEX(rxq->rpd.dma_addr, struct rpd, i);
2392         rxq->host_entry[ i ].cp_entry = &cp_entry[ i ];
2393
2394         *rxq->host_entry[ i ].status = STATUS_FREE;
2395
2396         fore200e->bus->write(FORE200E_DMA_INDEX(rxq->status.dma_addr, enum status, i), 
2397                              &cp_entry[ i ].status_haddr);
2398
2399         fore200e->bus->write(FORE200E_DMA_INDEX(rxq->rpd.dma_addr, struct rpd, i),
2400                              &cp_entry[ i ].rpd_haddr);
2401     }
2402
2403     /* set the head entry of the queue */
2404     rxq->head = 0;
2405
2406     fore200e->state = FORE200E_STATE_INIT_RXQ;
2407     return 0;
2408 }
2409
2410
2411 static int __init
2412 fore200e_init_tx_queue(struct fore200e* fore200e)
2413 {
2414     struct host_txq*     txq =  &fore200e->host_txq;
2415     struct cp_txq_entry* cp_entry;
2416     int i;
2417
2418     DPRINTK(2, "transmit queue is being initialized\n");
2419
2420     /* allocate and align the array of status words */
2421     if (fore200e->bus->dma_chunk_alloc(fore200e,
2422                                        &txq->status,
2423                                        sizeof(enum status), 
2424                                        QUEUE_SIZE_TX,
2425                                        fore200e->bus->status_alignment) < 0) {
2426         return -ENOMEM;
2427     }
2428
2429     /* allocate and align the array of transmit PDU descriptors */
2430     if (fore200e->bus->dma_chunk_alloc(fore200e,
2431                                        &txq->tpd,
2432                                        sizeof(struct tpd), 
2433                                        QUEUE_SIZE_TX,
2434                                        fore200e->bus->descr_alignment) < 0) {
2435         
2436         fore200e->bus->dma_chunk_free(fore200e, &txq->status);
2437         return -ENOMEM;
2438     }
2439
2440     /* get the base address of the cp resident tx queue entries */
2441     cp_entry = (struct cp_txq_entry*)(fore200e->virt_base + 
2442                                       fore200e->bus->read(&fore200e->cp_queues->cp_txq));
2443
2444     /* fill the host resident and cp resident tx entries */
2445     for (i=0; i < QUEUE_SIZE_TX; i++) {
2446         
2447         txq->host_entry[ i ].status = 
2448                              FORE200E_INDEX(txq->status.align_addr, enum status, i);
2449         txq->host_entry[ i ].tpd = 
2450                              FORE200E_INDEX(txq->tpd.align_addr, struct tpd, i);
2451         txq->host_entry[ i ].tpd_dma  = 
2452                              FORE200E_DMA_INDEX(txq->tpd.dma_addr, struct tpd, i);
2453         txq->host_entry[ i ].cp_entry = &cp_entry[ i ];
2454
2455         *txq->host_entry[ i ].status = STATUS_FREE;
2456         
2457         fore200e->bus->write(FORE200E_DMA_INDEX(txq->status.dma_addr, enum status, i), 
2458                              &cp_entry[ i ].status_haddr);
2459         
2460         /* although there is a one-to-one mapping of tx queue entries and tpds,
2461            we do not write here the DMA (physical) base address of each tpd into
2462            the related cp resident entry, because the cp relies on this write
2463            operation to detect that a new pdu has been submitted for tx */
2464     }
2465
2466     /* set the head and tail entries of the queue */
2467     txq->head = 0;
2468     txq->tail = 0;
2469
2470     fore200e->state = FORE200E_STATE_INIT_TXQ;
2471     return 0;
2472 }
2473
2474
2475 static int __init
2476 fore200e_init_cmd_queue(struct fore200e* fore200e)
2477 {
2478     struct host_cmdq*     cmdq =  &fore200e->host_cmdq;
2479     struct cp_cmdq_entry* cp_entry;
2480     int i;
2481
2482     DPRINTK(2, "command queue is being initialized\n");
2483
2484     /* allocate and align the array of status words */
2485     if (fore200e->bus->dma_chunk_alloc(fore200e,
2486                                        &cmdq->status,
2487                                        sizeof(enum status), 
2488                                        QUEUE_SIZE_CMD,
2489                                        fore200e->bus->status_alignment) < 0) {
2490         return -ENOMEM;
2491     }
2492     
2493     /* get the base address of the cp resident cmd queue entries */
2494     cp_entry = (struct cp_cmdq_entry*)(fore200e->virt_base + 
2495                                        fore200e->bus->read(&fore200e->cp_queues->cp_cmdq));
2496
2497     /* fill the host resident and cp resident cmd entries */
2498     for (i=0; i < QUEUE_SIZE_CMD; i++) {
2499         
2500         cmdq->host_entry[ i ].status   = 
2501                               FORE200E_INDEX(cmdq->status.align_addr, enum status, i);
2502         cmdq->host_entry[ i ].cp_entry = &cp_entry[ i ];
2503
2504         *cmdq->host_entry[ i ].status = STATUS_FREE;
2505
2506         fore200e->bus->write(FORE200E_DMA_INDEX(cmdq->status.dma_addr, enum status, i), 
2507                              &cp_entry[ i ].status_haddr);
2508     }
2509
2510     /* set the head entry of the queue */
2511     cmdq->head = 0;
2512
2513     fore200e->state = FORE200E_STATE_INIT_CMDQ;
2514     return 0;
2515 }
2516
2517
2518 static void __init
2519 fore200e_param_bs_queue(struct fore200e* fore200e,
2520                         enum buffer_scheme scheme, enum buffer_magn magn,
2521                         int queue_length, int pool_size, int supply_blksize)
2522 {
2523     struct bs_spec* bs_spec = &fore200e->cp_queues->init.bs_spec[ scheme ][ magn ];
2524
2525     fore200e->bus->write(queue_length,                           &bs_spec->queue_length);
2526     fore200e->bus->write(fore200e_rx_buf_size[ scheme ][ magn ], &bs_spec->buffer_size);
2527     fore200e->bus->write(pool_size,                              &bs_spec->pool_size);
2528     fore200e->bus->write(supply_blksize,                         &bs_spec->supply_blksize);
2529 }
2530
2531
2532 static int __init
2533 fore200e_initialize(struct fore200e* fore200e)
2534 {
2535     struct cp_queues* cpq;
2536     int               ok, scheme, magn;
2537
2538     DPRINTK(2, "device %s being initialized\n", fore200e->name);
2539
2540     init_MUTEX(&fore200e->rate_sf);
2541     spin_lock_init(&fore200e->q_lock);
2542
2543     cpq = fore200e->cp_queues = (struct cp_queues*) (fore200e->virt_base + FORE200E_CP_QUEUES_OFFSET);
2544
2545     /* enable cp to host interrupts */
2546     fore200e->bus->write(1, &cpq->imask);
2547
2548     if (fore200e->bus->irq_enable)
2549         fore200e->bus->irq_enable(fore200e);
2550     
2551     fore200e->bus->write(NBR_CONNECT, &cpq->init.num_connect);
2552
2553     fore200e->bus->write(QUEUE_SIZE_CMD, &cpq->init.cmd_queue_len);
2554     fore200e->bus->write(QUEUE_SIZE_RX,  &cpq->init.rx_queue_len);
2555     fore200e->bus->write(QUEUE_SIZE_TX,  &cpq->init.tx_queue_len);
2556
2557     fore200e->bus->write(RSD_EXTENSION,  &cpq->init.rsd_extension);
2558     fore200e->bus->write(TSD_EXTENSION,  &cpq->init.tsd_extension);
2559
2560     for (scheme = 0; scheme < BUFFER_SCHEME_NBR; scheme++)
2561         for (magn = 0; magn < BUFFER_MAGN_NBR; magn++)
2562             fore200e_param_bs_queue(fore200e, scheme, magn,
2563                                     QUEUE_SIZE_BS, 
2564                                     fore200e_rx_buf_nbr[ scheme ][ magn ],
2565                                     RBD_BLK_SIZE);
2566
2567     /* issue the initialize command */
2568     fore200e->bus->write(STATUS_PENDING,    &cpq->init.status);
2569     fore200e->bus->write(OPCODE_INITIALIZE, &cpq->init.opcode);
2570
2571     ok = fore200e_io_poll(fore200e, &cpq->init.status, STATUS_COMPLETE, 3000);
2572     if (ok == 0) {
2573         printk(FORE200E "device %s initialization failed\n", fore200e->name);
2574         return -ENODEV;
2575     }
2576
2577     printk(FORE200E "device %s initialized\n", fore200e->name);
2578
2579     fore200e->state = FORE200E_STATE_INITIALIZE;
2580     return 0;
2581 }
2582
2583
2584 static void __init
2585 fore200e_monitor_putc(struct fore200e* fore200e, char c)
2586 {
2587     struct cp_monitor* monitor = fore200e->cp_monitor;
2588
2589 #if 0
2590     printk("%c", c);
2591 #endif
2592     fore200e->bus->write(((u32) c) | FORE200E_CP_MONITOR_UART_AVAIL, &monitor->soft_uart.send);
2593 }
2594
2595
2596 static int __init
2597 fore200e_monitor_getc(struct fore200e* fore200e)
2598 {
2599     struct cp_monitor* monitor = fore200e->cp_monitor;
2600     unsigned long      timeout = jiffies + MSECS(50);
2601     int                c;
2602
2603     while (time_before(jiffies, timeout)) {
2604
2605         c = (int) fore200e->bus->read(&monitor->soft_uart.recv);
2606
2607         if (c & FORE200E_CP_MONITOR_UART_AVAIL) {
2608
2609             fore200e->bus->write(FORE200E_CP_MONITOR_UART_FREE, &monitor->soft_uart.recv);
2610 #if 0
2611             printk("%c", c & 0xFF);
2612 #endif
2613             return c & 0xFF;
2614         }
2615     }
2616
2617     return -1;
2618 }
2619
2620
2621 static void __init
2622 fore200e_monitor_puts(struct fore200e* fore200e, char* str)
2623 {
2624     while (*str) {
2625
2626         /* the i960 monitor doesn't accept any new character if it has something to say */
2627         while (fore200e_monitor_getc(fore200e) >= 0);
2628         
2629         fore200e_monitor_putc(fore200e, *str++);
2630     }
2631
2632     while (fore200e_monitor_getc(fore200e) >= 0);
2633 }
2634
2635
2636 static int __init
2637 fore200e_start_fw(struct fore200e* fore200e)
2638 {
2639     int               ok;
2640     char              cmd[ 48 ];
2641     struct fw_header* fw_header = (struct fw_header*) fore200e->bus->fw_data;
2642
2643     DPRINTK(2, "device %s firmware being started\n", fore200e->name);
2644
2645 #if defined(__sparc_v9__)
2646     /* reported to be required by SBA cards on some sparc64 hosts */
2647     fore200e_spin(100);
2648 #endif
2649
2650     sprintf(cmd, "\rgo %x\r", le32_to_cpu(fw_header->start_offset));
2651
2652     fore200e_monitor_puts(fore200e, cmd);
2653
2654     ok = fore200e_io_poll(fore200e, &fore200e->cp_monitor->bstat, BSTAT_CP_RUNNING, 1000);
2655     if (ok == 0) {
2656         printk(FORE200E "device %s firmware didn't start\n", fore200e->name);
2657         return -ENODEV;
2658     }
2659
2660     printk(FORE200E "device %s firmware started\n", fore200e->name);
2661
2662     fore200e->state = FORE200E_STATE_START_FW;
2663     return 0;
2664 }
2665
2666
2667 static int __init
2668 fore200e_load_fw(struct fore200e* fore200e)
2669 {
2670     u32* fw_data = (u32*) fore200e->bus->fw_data;
2671     u32  fw_size = (u32) *fore200e->bus->fw_size / sizeof(u32);
2672
2673     struct fw_header* fw_header = (struct fw_header*) fw_data;
2674
2675     u32* load_addr = fore200e->virt_base + le32_to_cpu(fw_header->load_offset);
2676
2677     DPRINTK(2, "device %s firmware being loaded at 0x%p (%d words)\n", 
2678             fore200e->name, load_addr, fw_size);
2679
2680     if (le32_to_cpu(fw_header->magic) != FW_HEADER_MAGIC) {
2681         printk(FORE200E "corrupted %s firmware image\n", fore200e->bus->model_name);
2682         return -ENODEV;
2683     }
2684
2685     for (; fw_size--; fw_data++, load_addr++)
2686         fore200e->bus->write(le32_to_cpu(*fw_data), load_addr);
2687
2688     fore200e->state = FORE200E_STATE_LOAD_FW;
2689     return 0;
2690 }
2691
2692
2693 static int __init
2694 fore200e_register(struct fore200e* fore200e)
2695 {
2696     struct atm_dev* atm_dev;
2697
2698     DPRINTK(2, "device %s being registered\n", fore200e->name);
2699
2700     atm_dev = atm_dev_register(fore200e->bus->proc_name, &fore200e_ops, -1,
2701       NULL); 
2702     if (atm_dev == NULL) {
2703         printk(FORE200E "unable to register device %s\n", fore200e->name);
2704         return -ENODEV;
2705     }
2706
2707     atm_dev->dev_data = fore200e;
2708     fore200e->atm_dev = atm_dev;
2709
2710     atm_dev->ci_range.vpi_bits = FORE200E_VPI_BITS;
2711     atm_dev->ci_range.vci_bits = FORE200E_VCI_BITS;
2712
2713     fore200e->available_cell_rate = ATM_OC3_PCR;
2714
2715     fore200e->state = FORE200E_STATE_REGISTER;
2716     return 0;
2717 }
2718
2719
2720 static int __init
2721 fore200e_init(struct fore200e* fore200e)
2722 {
2723     if (fore200e_register(fore200e) < 0)
2724         return -ENODEV;
2725     
2726     if (fore200e->bus->configure(fore200e) < 0)
2727         return -ENODEV;
2728
2729     if (fore200e->bus->map(fore200e) < 0)
2730         return -ENODEV;
2731
2732     if (fore200e_reset(fore200e, 1) < 0)
2733         return -ENODEV;
2734
2735     if (fore200e_load_fw(fore200e) < 0)
2736         return -ENODEV;
2737
2738     if (fore200e_start_fw(fore200e) < 0)
2739         return -ENODEV;
2740
2741     if (fore200e_initialize(fore200e) < 0)
2742         return -ENODEV;
2743
2744     if (fore200e_init_cmd_queue(fore200e) < 0)
2745         return -ENOMEM;
2746
2747     if (fore200e_init_tx_queue(fore200e) < 0)
2748         return -ENOMEM;
2749
2750     if (fore200e_init_rx_queue(fore200e) < 0)
2751         return -ENOMEM;
2752
2753     if (fore200e_init_bs_queue(fore200e) < 0)
2754         return -ENOMEM;
2755
2756     if (fore200e_alloc_rx_buf(fore200e) < 0)
2757         return -ENOMEM;
2758
2759     if (fore200e_get_esi(fore200e) < 0)
2760         return -EIO;
2761
2762     if (fore200e_irq_request(fore200e) < 0)
2763         return -EBUSY;
2764
2765     fore200e_supply(fore200e);
2766     
2767     /* all done, board initialization is now complete */
2768     fore200e->state = FORE200E_STATE_COMPLETE;
2769     return 0;
2770 }
2771
2772 static int __init
2773 fore200e_module_init(void)
2774 {
2775     const struct fore200e_bus* bus;
2776     struct       fore200e*     fore200e;
2777     int                        index, link;
2778
2779     printk(FORE200E "FORE Systems 200E-series ATM driver - version " FORE200E_VERSION "\n");
2780
2781     /* for each configured bus interface */
2782     for (link = 0, bus = fore200e_bus; bus->model_name; bus++) {
2783
2784         /* detect all boards present on that bus */
2785         for (index = 0; (fore200e = bus->detect(bus, index)); index++) {
2786             
2787             printk(FORE200E "device %s found at 0x%lx, IRQ %s\n",
2788                    fore200e->bus->model_name, 
2789                    fore200e->phys_base, fore200e_irq_itoa(fore200e->irq));
2790
2791             sprintf(fore200e->name, "%s-%d", bus->model_name, index);
2792
2793             if (fore200e_init(fore200e) < 0) {
2794
2795                 fore200e_shutdown(fore200e);
2796                 break;
2797             }
2798
2799             link++;
2800
2801             fore200e->next  = fore200e_boards;
2802             fore200e_boards = fore200e;
2803         }
2804     }
2805
2806     if (link)
2807         return 0;
2808     return -ENODEV;
2809 }
2810
2811
2812 static void __exit
2813 fore200e_module_cleanup(void)
2814 {
2815     while (fore200e_boards) {
2816         struct fore200e* fore200e = fore200e_boards;
2817
2818         fore200e_shutdown(fore200e);
2819         fore200e_boards = fore200e->next;
2820         kfree(fore200e);
2821     }
2822     DPRINTK(1, "module being removed\n");
2823 }
2824
2825
2826 static int
2827 fore200e_proc_read(struct atm_dev *dev, loff_t* pos, char* page)
2828 {
2829     struct fore200e*     fore200e  = FORE200E_DEV(dev);
2830     struct fore200e_vcc* fore200e_vcc;
2831     struct atm_vcc*      vcc;
2832     int                  i, len, left = *pos;
2833     unsigned long        flags;
2834
2835     if (!left--) {
2836
2837         if (fore200e_getstats(fore200e) < 0)
2838             return -EIO;
2839
2840         len = sprintf(page,"\n"
2841                        " device:\n"
2842                        "   internal name:\t\t%s\n", fore200e->name);
2843
2844         /* print bus-specific information */
2845         if (fore200e->bus->proc_read)
2846             len += fore200e->bus->proc_read(fore200e, page + len);
2847         
2848         len += sprintf(page + len,
2849                 "   interrupt line:\t\t%s\n"
2850                 "   physical base address:\t0x%p\n"
2851                 "   virtual base address:\t0x%p\n"
2852                 "   factory address (ESI):\t%02x:%02x:%02x:%02x:%02x:%02x\n"
2853                 "   board serial number:\t\t%d\n\n",
2854                 fore200e_irq_itoa(fore200e->irq),
2855                 (void*)fore200e->phys_base,
2856                 (void*)fore200e->virt_base,
2857                 fore200e->esi[0], fore200e->esi[1], fore200e->esi[2],
2858                 fore200e->esi[3], fore200e->esi[4], fore200e->esi[5],
2859                 fore200e->esi[4] * 256 + fore200e->esi[5]);
2860
2861         return len;
2862     }
2863
2864     if (!left--)
2865         return sprintf(page,
2866                        "   free small bufs, scheme 1:\t%d\n"
2867                        "   free large bufs, scheme 1:\t%d\n"
2868                        "   free small bufs, scheme 2:\t%d\n"
2869                        "   free large bufs, scheme 2:\t%d\n",
2870                        fore200e->host_bsq[ BUFFER_SCHEME_ONE ][ BUFFER_MAGN_SMALL ].freebuf_count,
2871                        fore200e->host_bsq[ BUFFER_SCHEME_ONE ][ BUFFER_MAGN_LARGE ].freebuf_count,
2872                        fore200e->host_bsq[ BUFFER_SCHEME_TWO ][ BUFFER_MAGN_SMALL ].freebuf_count,
2873                        fore200e->host_bsq[ BUFFER_SCHEME_TWO ][ BUFFER_MAGN_LARGE ].freebuf_count);
2874
2875     if (!left--) {
2876         u32 hb = fore200e->bus->read(&fore200e->cp_queues->heartbeat);
2877
2878         len = sprintf(page,"\n\n"
2879                       " cell processor:\n"
2880                       "   heartbeat state:\t\t");
2881         
2882         if (hb >> 16 != 0xDEAD)
2883             len += sprintf(page + len, "0x%08x\n", hb);
2884         else
2885             len += sprintf(page + len, "*** FATAL ERROR %04x ***\n", hb & 0xFFFF);
2886
2887         return len;
2888     }
2889
2890     if (!left--) {
2891         static const char* media_name[] = {
2892             "unshielded twisted pair",
2893             "multimode optical fiber ST",
2894             "multimode optical fiber SC",
2895             "single-mode optical fiber ST",
2896             "single-mode optical fiber SC",
2897             "unknown"
2898         };
2899
2900         static const char* oc3_mode[] = {
2901             "normal operation",
2902             "diagnostic loopback",
2903             "line loopback",
2904             "unknown"
2905         };
2906
2907         u32 fw_release     = fore200e->bus->read(&fore200e->cp_queues->fw_release);
2908         u32 mon960_release = fore200e->bus->read(&fore200e->cp_queues->mon960_release);
2909         u32 oc3_revision   = fore200e->bus->read(&fore200e->cp_queues->oc3_revision);
2910         u32 media_index    = FORE200E_MEDIA_INDEX(fore200e->bus->read(&fore200e->cp_queues->media_type));
2911         u32 oc3_index;
2912
2913         if ((media_index < 0) || (media_index > 4))
2914             media_index = 5;
2915         
2916         switch (fore200e->loop_mode) {
2917             case ATM_LM_NONE:    oc3_index = 0;
2918                                  break;
2919             case ATM_LM_LOC_PHY: oc3_index = 1;
2920                                  break;
2921             case ATM_LM_RMT_PHY: oc3_index = 2;
2922                                  break;
2923             default:             oc3_index = 3;
2924         }
2925
2926         return sprintf(page,
2927                        "   firmware release:\t\t%d.%d.%d\n"
2928                        "   monitor release:\t\t%d.%d\n"
2929                        "   media type:\t\t\t%s\n"
2930                        "   OC-3 revision:\t\t0x%x\n"
2931                        "   OC-3 mode:\t\t\t%s",
2932                        fw_release >> 16, fw_release << 16 >> 24,  fw_release << 24 >> 24,
2933                        mon960_release >> 16, mon960_release << 16 >> 16,
2934                        media_name[ media_index ],
2935                        oc3_revision,
2936                        oc3_mode[ oc3_index ]);
2937     }
2938
2939     if (!left--) {
2940         struct cp_monitor* cp_monitor = fore200e->cp_monitor;
2941
2942         return sprintf(page,
2943                        "\n\n"
2944                        " monitor:\n"
2945                        "   version number:\t\t%d\n"
2946                        "   boot status word:\t\t0x%08x\n",
2947                        fore200e->bus->read(&cp_monitor->mon_version),
2948                        fore200e->bus->read(&cp_monitor->bstat));
2949     }
2950
2951     if (!left--)
2952         return sprintf(page,
2953                        "\n"
2954                        " device statistics:\n"
2955                        "  4b5b:\n"
2956                        "     crc_header_errors:\t\t%10u\n"
2957                        "     framing_errors:\t\t%10u\n",
2958                        fore200e_swap(fore200e->stats->phy.crc_header_errors),
2959                        fore200e_swap(fore200e->stats->phy.framing_errors));
2960     
2961     if (!left--)
2962         return sprintf(page, "\n"
2963                        "  OC-3:\n"
2964                        "     section_bip8_errors:\t%10u\n"
2965                        "     path_bip8_errors:\t\t%10u\n"
2966                        "     line_bip24_errors:\t\t%10u\n"
2967                        "     line_febe_errors:\t\t%10u\n"
2968                        "     path_febe_errors:\t\t%10u\n"
2969                        "     corr_hcs_errors:\t\t%10u\n"
2970                        "     ucorr_hcs_errors:\t\t%10u\n",
2971                        fore200e_swap(fore200e->stats->oc3.section_bip8_errors),
2972                        fore200e_swap(fore200e->stats->oc3.path_bip8_errors),
2973                        fore200e_swap(fore200e->stats->oc3.line_bip24_errors),
2974                        fore200e_swap(fore200e->stats->oc3.line_febe_errors),
2975                        fore200e_swap(fore200e->stats->oc3.path_febe_errors),
2976                        fore200e_swap(fore200e->stats->oc3.corr_hcs_errors),
2977                        fore200e_swap(fore200e->stats->oc3.ucorr_hcs_errors));
2978
2979     if (!left--)
2980         return sprintf(page,"\n"
2981                        "   ATM:\t\t\t\t     cells\n"
2982                        "     TX:\t\t\t%10u\n"
2983                        "     RX:\t\t\t%10u\n"
2984                        "     vpi out of range:\t\t%10u\n"
2985                        "     vpi no conn:\t\t%10u\n"
2986                        "     vci out of range:\t\t%10u\n"
2987                        "     vci no conn:\t\t%10u\n",
2988                        fore200e_swap(fore200e->stats->atm.cells_transmitted),
2989                        fore200e_swap(fore200e->stats->atm.cells_received),
2990                        fore200e_swap(fore200e->stats->atm.vpi_bad_range),
2991                        fore200e_swap(fore200e->stats->atm.vpi_no_conn),
2992                        fore200e_swap(fore200e->stats->atm.vci_bad_range),
2993                        fore200e_swap(fore200e->stats->atm.vci_no_conn));
2994     
2995     if (!left--)
2996         return sprintf(page,"\n"
2997                        "   AAL0:\t\t\t     cells\n"
2998                        "     TX:\t\t\t%10u\n"
2999                        "     RX:\t\t\t%10u\n"
3000                        "     dropped:\t\t\t%10u\n",
3001                        fore200e_swap(fore200e->stats->aal0.cells_transmitted),
3002                        fore200e_swap(fore200e->stats->aal0.cells_received),
3003                        fore200e_swap(fore200e->stats->aal0.cells_dropped));
3004     
3005     if (!left--)
3006         return sprintf(page,"\n"
3007                        "   AAL3/4:\n"
3008                        "     SAR sublayer:\t\t     cells\n"
3009                        "       TX:\t\t\t%10u\n"
3010                        "       RX:\t\t\t%10u\n"
3011                        "       dropped:\t\t\t%10u\n"
3012                        "       CRC errors:\t\t%10u\n"
3013                        "       protocol errors:\t\t%10u\n\n"
3014                        "     CS  sublayer:\t\t      PDUs\n"
3015                        "       TX:\t\t\t%10u\n"
3016                        "       RX:\t\t\t%10u\n"
3017                        "       dropped:\t\t\t%10u\n"
3018                        "       protocol errors:\t\t%10u\n",
3019                        fore200e_swap(fore200e->stats->aal34.cells_transmitted),
3020                        fore200e_swap(fore200e->stats->aal34.cells_received),
3021                        fore200e_swap(fore200e->stats->aal34.cells_dropped),
3022                        fore200e_swap(fore200e->stats->aal34.cells_crc_errors),
3023                        fore200e_swap(fore200e->stats->aal34.cells_protocol_errors),
3024                        fore200e_swap(fore200e->stats->aal34.cspdus_transmitted),
3025                        fore200e_swap(fore200e->stats->aal34.cspdus_received),
3026                        fore200e_swap(fore200e->stats->aal34.cspdus_dropped),
3027                        fore200e_swap(fore200e->stats->aal34.cspdus_protocol_errors));
3028     
3029     if (!left--)
3030         return sprintf(page,"\n"
3031                        "   AAL5:\n"
3032                        "     SAR sublayer:\t\t     cells\n"
3033                        "       TX:\t\t\t%10u\n"
3034                        "       RX:\t\t\t%10u\n"
3035                        "       dropped:\t\t\t%10u\n"
3036                        "       congestions:\t\t%10u\n\n"
3037                        "     CS  sublayer:\t\t      PDUs\n"
3038                        "       TX:\t\t\t%10u\n"
3039                        "       RX:\t\t\t%10u\n"
3040                        "       dropped:\t\t\t%10u\n"
3041                        "       CRC errors:\t\t%10u\n"
3042                        "       protocol errors:\t\t%10u\n",
3043                        fore200e_swap(fore200e->stats->aal5.cells_transmitted),
3044                        fore200e_swap(fore200e->stats->aal5.cells_received),
3045                        fore200e_swap(fore200e->stats->aal5.cells_dropped),
3046                        fore200e_swap(fore200e->stats->aal5.congestion_experienced),
3047                        fore200e_swap(fore200e->stats->aal5.cspdus_transmitted),
3048                        fore200e_swap(fore200e->stats->aal5.cspdus_received),
3049                        fore200e_swap(fore200e->stats->aal5.cspdus_dropped),
3050                        fore200e_swap(fore200e->stats->aal5.cspdus_crc_errors),
3051                        fore200e_swap(fore200e->stats->aal5.cspdus_protocol_errors));
3052     
3053     if (!left--)
3054         return sprintf(page,"\n"
3055                        "   AUX:\t\t       allocation failures\n"
3056                        "     small b1:\t\t\t%10u\n"
3057                        "     large b1:\t\t\t%10u\n"
3058                        "     small b2:\t\t\t%10u\n"
3059                        "     large b2:\t\t\t%10u\n"
3060                        "     RX PDUs:\t\t\t%10u\n"
3061                        "     TX PDUs:\t\t\t%10lu\n",
3062                        fore200e_swap(fore200e->stats->aux.small_b1_failed),
3063                        fore200e_swap(fore200e->stats->aux.large_b1_failed),
3064                        fore200e_swap(fore200e->stats->aux.small_b2_failed),
3065                        fore200e_swap(fore200e->stats->aux.large_b2_failed),
3066                        fore200e_swap(fore200e->stats->aux.rpd_alloc_failed),
3067                        fore200e->tx_sat);
3068     
3069     if (!left--)
3070         return sprintf(page,"\n"
3071                        " receive carrier:\t\t\t%s\n",
3072                        fore200e->stats->aux.receive_carrier ? "ON" : "OFF!");
3073     
3074     if (!left--) {
3075         return sprintf(page,"\n"
3076                        " VCCs:\n  address   VPI VCI   AAL "
3077                        "TX PDUs   TX min/max size  RX PDUs   RX min/max size\n");
3078     }
3079
3080     for (i = 0; i < NBR_CONNECT; i++) {
3081
3082         vcc = fore200e->vc_map[i].vcc;
3083
3084         if (vcc == NULL)
3085             continue;
3086
3087         spin_lock_irqsave(&fore200e->q_lock, flags);
3088
3089         if (vcc && test_bit(ATM_VF_READY, &vcc->flags) && !left--) {
3090
3091             fore200e_vcc = FORE200E_VCC(vcc);
3092             ASSERT(fore200e_vcc);
3093
3094             len = sprintf(page,
3095                           "  %08x  %03d %05d %1d   %09lu %05d/%05d      %09lu %05d/%05d\n",
3096                           (u32)(unsigned long)vcc,
3097                           vcc->vpi, vcc->vci, fore200e_atm2fore_aal(vcc->qos.aal),
3098                           fore200e_vcc->tx_pdu,
3099                           fore200e_vcc->tx_min_pdu > 0xFFFF ? 0 : fore200e_vcc->tx_min_pdu,
3100                           fore200e_vcc->tx_max_pdu,
3101                           fore200e_vcc->rx_pdu,
3102                           fore200e_vcc->rx_min_pdu > 0xFFFF ? 0 : fore200e_vcc->rx_min_pdu,
3103                           fore200e_vcc->rx_max_pdu);
3104
3105             spin_unlock_irqrestore(&fore200e->q_lock, flags);
3106             return len;
3107         }
3108
3109         spin_unlock_irqrestore(&fore200e->q_lock, flags);
3110     }
3111     
3112     return 0;
3113 }
3114
3115 module_init(fore200e_module_init);
3116 module_exit(fore200e_module_cleanup);
3117
3118
3119 static const struct atmdev_ops fore200e_ops =
3120 {
3121         .open       = fore200e_open,
3122         .close      = fore200e_close,
3123         .ioctl      = fore200e_ioctl,
3124         .getsockopt = fore200e_getsockopt,
3125         .setsockopt = fore200e_setsockopt,
3126         .send       = fore200e_send,
3127         .change_qos = fore200e_change_qos,
3128         .proc_read  = fore200e_proc_read,
3129         .owner      = THIS_MODULE
3130 };
3131
3132
3133 #ifdef CONFIG_ATM_FORE200E_PCA
3134 extern const unsigned char _fore200e_pca_fw_data[];
3135 extern const unsigned int  _fore200e_pca_fw_size;
3136 #endif
3137 #ifdef CONFIG_ATM_FORE200E_SBA
3138 extern const unsigned char _fore200e_sba_fw_data[];
3139 extern const unsigned int  _fore200e_sba_fw_size;
3140 #endif
3141
3142 static const struct fore200e_bus fore200e_bus[] = {
3143 #ifdef CONFIG_ATM_FORE200E_PCA
3144     { "PCA-200E", "pca200e", 32, 4, 32, 
3145       _fore200e_pca_fw_data, &_fore200e_pca_fw_size,
3146       fore200e_pca_read,
3147       fore200e_pca_write,
3148       fore200e_pca_dma_map,
3149       fore200e_pca_dma_unmap,
3150       fore200e_pca_dma_sync_for_cpu,
3151       fore200e_pca_dma_sync_for_device,
3152       fore200e_pca_dma_chunk_alloc,
3153       fore200e_pca_dma_chunk_free,
3154       fore200e_pca_detect,
3155       fore200e_pca_configure,
3156       fore200e_pca_map,
3157       fore200e_pca_reset,
3158       fore200e_pca_prom_read,
3159       fore200e_pca_unmap,
3160       NULL,
3161       fore200e_pca_irq_check,
3162       fore200e_pca_irq_ack,
3163       fore200e_pca_proc_read,
3164     },
3165 #endif
3166 #ifdef CONFIG_ATM_FORE200E_SBA
3167     { "SBA-200E", "sba200e", 32, 64, 32,
3168       _fore200e_sba_fw_data, &_fore200e_sba_fw_size,
3169       fore200e_sba_read,
3170       fore200e_sba_write,
3171       fore200e_sba_dma_map,
3172       fore200e_sba_dma_unmap,
3173       fore200e_sba_dma_sync_for_cpu,
3174       fore200e_sba_dma_sync_for_device,
3175       fore200e_sba_dma_chunk_alloc,
3176       fore200e_sba_dma_chunk_free,
3177       fore200e_sba_detect, 
3178       fore200e_sba_configure,
3179       fore200e_sba_map,
3180       fore200e_sba_reset,
3181       fore200e_sba_prom_read,
3182       fore200e_sba_unmap,
3183       fore200e_sba_irq_enable,
3184       fore200e_sba_irq_check,
3185       fore200e_sba_irq_ack,
3186       fore200e_sba_proc_read,
3187     },
3188 #endif
3189     {}
3190 };
3191
3192 #ifdef MODULE_LICENSE
3193 MODULE_LICENSE("GPL");
3194 #endif