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