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