Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / atm / fore200e.c
blob196b336446275d4816f0228c29a25bf595d9a11c
1 /*
2 $Id: fore200e.c,v 1.5 2000/04/14 10:10:34 davem Exp $
4 A FORE Systems 200E-series driver for ATM on Linux.
5 Christophe Lizzi (lizzi@cnam.fr), October 1999-March 2003.
7 Based on the PCA-200E driver from Uwe Dannowski (Uwe.Dannowski@inf.tu-dresden.de).
9 This driver simultaneously supports PCA-200E and SBA-200E adapters
10 on i386, alpha (untested), powerpc, sparc and sparc64 architectures.
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.
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.
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
28 #include <linux/config.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/capability.h>
33 #include <linux/sched.h>
34 #include <linux/interrupt.h>
35 #include <linux/bitops.h>
36 #include <linux/pci.h>
37 #include <linux/module.h>
38 #include <linux/atmdev.h>
39 #include <linux/sonet.h>
40 #include <linux/atm_suni.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/delay.h>
43 #include <asm/io.h>
44 #include <asm/string.h>
45 #include <asm/page.h>
46 #include <asm/irq.h>
47 #include <asm/dma.h>
48 #include <asm/byteorder.h>
49 #include <asm/uaccess.h>
50 #include <asm/atomic.h>
52 #ifdef CONFIG_ATM_FORE200E_SBA
53 #include <asm/idprom.h>
54 #include <asm/sbus.h>
55 #include <asm/openprom.h>
56 #include <asm/oplib.h>
57 #include <asm/pgtable.h>
58 #endif
60 #if defined(CONFIG_ATM_FORE200E_USE_TASKLET) /* defer interrupt work to a tasklet */
61 #define FORE200E_USE_TASKLET
62 #endif
64 #if 0 /* enable the debugging code of the buffer supply queues */
65 #define FORE200E_BSQ_DEBUG
66 #endif
68 #if 1 /* ensure correct handling of 52-byte AAL0 SDUs expected by atmdump-like apps */
69 #define FORE200E_52BYTE_AAL0_SDU
70 #endif
72 #include "fore200e.h"
73 #include "suni.h"
75 #define FORE200E_VERSION "0.3e"
77 #define FORE200E "fore200e: "
79 #if 0 /* override .config */
80 #define CONFIG_ATM_FORE200E_DEBUG 1
81 #endif
82 #if defined(CONFIG_ATM_FORE200E_DEBUG) && (CONFIG_ATM_FORE200E_DEBUG > 0)
83 #define DPRINTK(level, format, args...) do { if (CONFIG_ATM_FORE200E_DEBUG >= (level)) \
84 printk(FORE200E format, ##args); } while (0)
85 #else
86 #define DPRINTK(level, format, args...) do {} while (0)
87 #endif
90 #define FORE200E_ALIGN(addr, alignment) \
91 ((((unsigned long)(addr) + (alignment - 1)) & ~(alignment - 1)) - (unsigned long)(addr))
93 #define FORE200E_DMA_INDEX(dma_addr, type, index) ((dma_addr) + (index) * sizeof(type))
95 #define FORE200E_INDEX(virt_addr, type, index) (&((type *)(virt_addr))[ index ])
97 #define FORE200E_NEXT_ENTRY(index, modulo) (index = ++(index) % (modulo))
99 #if 1
100 #define ASSERT(expr) if (!(expr)) { \
101 printk(FORE200E "assertion failed! %s[%d]: %s\n", \
102 __FUNCTION__, __LINE__, #expr); \
103 panic(FORE200E "%s", __FUNCTION__); \
105 #else
106 #define ASSERT(expr) do {} while (0)
107 #endif
110 static const struct atmdev_ops fore200e_ops;
111 static const struct fore200e_bus fore200e_bus[];
113 static LIST_HEAD(fore200e_boards);
116 MODULE_AUTHOR("Christophe Lizzi - credits to Uwe Dannowski and Heikki Vatiainen");
117 MODULE_DESCRIPTION("FORE Systems 200E-series ATM driver - version " FORE200E_VERSION);
118 MODULE_SUPPORTED_DEVICE("PCA-200E, SBA-200E");
121 static const int fore200e_rx_buf_nbr[ BUFFER_SCHEME_NBR ][ BUFFER_MAGN_NBR ] = {
122 { BUFFER_S1_NBR, BUFFER_L1_NBR },
123 { BUFFER_S2_NBR, BUFFER_L2_NBR }
126 static const int fore200e_rx_buf_size[ BUFFER_SCHEME_NBR ][ BUFFER_MAGN_NBR ] = {
127 { BUFFER_S1_SIZE, BUFFER_L1_SIZE },
128 { BUFFER_S2_SIZE, BUFFER_L2_SIZE }
132 #if defined(CONFIG_ATM_FORE200E_DEBUG) && (CONFIG_ATM_FORE200E_DEBUG > 0)
133 static const char* fore200e_traffic_class[] = { "NONE", "UBR", "CBR", "VBR", "ABR", "ANY" };
134 #endif
137 #if 0 /* currently unused */
138 static int
139 fore200e_fore2atm_aal(enum fore200e_aal aal)
141 switch(aal) {
142 case FORE200E_AAL0: return ATM_AAL0;
143 case FORE200E_AAL34: return ATM_AAL34;
144 case FORE200E_AAL5: return ATM_AAL5;
147 return -EINVAL;
149 #endif
152 static enum fore200e_aal
153 fore200e_atm2fore_aal(int aal)
155 switch(aal) {
156 case ATM_AAL0: return FORE200E_AAL0;
157 case ATM_AAL34: return FORE200E_AAL34;
158 case ATM_AAL1:
159 case ATM_AAL2:
160 case ATM_AAL5: return FORE200E_AAL5;
163 return -EINVAL;
167 static char*
168 fore200e_irq_itoa(int irq)
170 #if defined(__sparc_v9__)
171 return __irq_itoa(irq);
172 #else
173 static char str[8];
174 sprintf(str, "%d", irq);
175 return str;
176 #endif
180 static void*
181 fore200e_kmalloc(int size, int flags)
183 void* chunk = kmalloc(size, flags);
185 if (chunk)
186 memset(chunk, 0x00, size);
187 else
188 printk(FORE200E "kmalloc() failed, requested size = %d, flags = 0x%x\n", size, flags);
190 return chunk;
194 static void
195 fore200e_kfree(void* chunk)
197 kfree(chunk);
201 /* allocate and align a chunk of memory intended to hold the data behing exchanged
202 between the driver and the adapter (using streaming DVMA) */
204 static int
205 fore200e_chunk_alloc(struct fore200e* fore200e, struct chunk* chunk, int size, int alignment, int direction)
207 unsigned long offset = 0;
209 if (alignment <= sizeof(int))
210 alignment = 0;
212 chunk->alloc_size = size + alignment;
213 chunk->align_size = size;
214 chunk->direction = direction;
216 chunk->alloc_addr = fore200e_kmalloc(chunk->alloc_size, GFP_KERNEL | GFP_DMA);
217 if (chunk->alloc_addr == NULL)
218 return -ENOMEM;
220 if (alignment > 0)
221 offset = FORE200E_ALIGN(chunk->alloc_addr, alignment);
223 chunk->align_addr = chunk->alloc_addr + offset;
225 chunk->dma_addr = fore200e->bus->dma_map(fore200e, chunk->align_addr, chunk->align_size, direction);
227 return 0;
231 /* free a chunk of memory */
233 static void
234 fore200e_chunk_free(struct fore200e* fore200e, struct chunk* chunk)
236 fore200e->bus->dma_unmap(fore200e, chunk->dma_addr, chunk->dma_size, chunk->direction);
238 fore200e_kfree(chunk->alloc_addr);
242 static void
243 fore200e_spin(int msecs)
245 unsigned long timeout = jiffies + msecs_to_jiffies(msecs);
246 while (time_before(jiffies, timeout));
250 static int
251 fore200e_poll(struct fore200e* fore200e, volatile u32* addr, u32 val, int msecs)
253 unsigned long timeout = jiffies + msecs_to_jiffies(msecs);
254 int ok;
256 mb();
257 do {
258 if ((ok = (*addr == val)) || (*addr & STATUS_ERROR))
259 break;
261 } while (time_before(jiffies, timeout));
263 #if 1
264 if (!ok) {
265 printk(FORE200E "cmd polling failed, got status 0x%08x, expected 0x%08x\n",
266 *addr, val);
268 #endif
270 return ok;
274 static int
275 fore200e_io_poll(struct fore200e* fore200e, volatile u32 __iomem *addr, u32 val, int msecs)
277 unsigned long timeout = jiffies + msecs_to_jiffies(msecs);
278 int ok;
280 do {
281 if ((ok = (fore200e->bus->read(addr) == val)))
282 break;
284 } while (time_before(jiffies, timeout));
286 #if 1
287 if (!ok) {
288 printk(FORE200E "I/O polling failed, got status 0x%08x, expected 0x%08x\n",
289 fore200e->bus->read(addr), val);
291 #endif
293 return ok;
297 static void
298 fore200e_free_rx_buf(struct fore200e* fore200e)
300 int scheme, magn, nbr;
301 struct buffer* buffer;
303 for (scheme = 0; scheme < BUFFER_SCHEME_NBR; scheme++) {
304 for (magn = 0; magn < BUFFER_MAGN_NBR; magn++) {
306 if ((buffer = fore200e->host_bsq[ scheme ][ magn ].buffer) != NULL) {
308 for (nbr = 0; nbr < fore200e_rx_buf_nbr[ scheme ][ magn ]; nbr++) {
310 struct chunk* data = &buffer[ nbr ].data;
312 if (data->alloc_addr != NULL)
313 fore200e_chunk_free(fore200e, data);
321 static void
322 fore200e_uninit_bs_queue(struct fore200e* fore200e)
324 int scheme, magn;
326 for (scheme = 0; scheme < BUFFER_SCHEME_NBR; scheme++) {
327 for (magn = 0; magn < BUFFER_MAGN_NBR; magn++) {
329 struct chunk* status = &fore200e->host_bsq[ scheme ][ magn ].status;
330 struct chunk* rbd_block = &fore200e->host_bsq[ scheme ][ magn ].rbd_block;
332 if (status->alloc_addr)
333 fore200e->bus->dma_chunk_free(fore200e, status);
335 if (rbd_block->alloc_addr)
336 fore200e->bus->dma_chunk_free(fore200e, rbd_block);
342 static int
343 fore200e_reset(struct fore200e* fore200e, int diag)
345 int ok;
347 fore200e->cp_monitor = fore200e->virt_base + FORE200E_CP_MONITOR_OFFSET;
349 fore200e->bus->write(BSTAT_COLD_START, &fore200e->cp_monitor->bstat);
351 fore200e->bus->reset(fore200e);
353 if (diag) {
354 ok = fore200e_io_poll(fore200e, &fore200e->cp_monitor->bstat, BSTAT_SELFTEST_OK, 1000);
355 if (ok == 0) {
357 printk(FORE200E "device %s self-test failed\n", fore200e->name);
358 return -ENODEV;
361 printk(FORE200E "device %s self-test passed\n", fore200e->name);
363 fore200e->state = FORE200E_STATE_RESET;
366 return 0;
370 static void
371 fore200e_shutdown(struct fore200e* fore200e)
373 printk(FORE200E "removing device %s at 0x%lx, IRQ %s\n",
374 fore200e->name, fore200e->phys_base,
375 fore200e_irq_itoa(fore200e->irq));
377 if (fore200e->state > FORE200E_STATE_RESET) {
378 /* first, reset the board to prevent further interrupts or data transfers */
379 fore200e_reset(fore200e, 0);
382 /* then, release all allocated resources */
383 switch(fore200e->state) {
385 case FORE200E_STATE_COMPLETE:
386 if (fore200e->stats)
387 kfree(fore200e->stats);
389 case FORE200E_STATE_IRQ:
390 free_irq(fore200e->irq, fore200e->atm_dev);
392 case FORE200E_STATE_ALLOC_BUF:
393 fore200e_free_rx_buf(fore200e);
395 case FORE200E_STATE_INIT_BSQ:
396 fore200e_uninit_bs_queue(fore200e);
398 case FORE200E_STATE_INIT_RXQ:
399 fore200e->bus->dma_chunk_free(fore200e, &fore200e->host_rxq.status);
400 fore200e->bus->dma_chunk_free(fore200e, &fore200e->host_rxq.rpd);
402 case FORE200E_STATE_INIT_TXQ:
403 fore200e->bus->dma_chunk_free(fore200e, &fore200e->host_txq.status);
404 fore200e->bus->dma_chunk_free(fore200e, &fore200e->host_txq.tpd);
406 case FORE200E_STATE_INIT_CMDQ:
407 fore200e->bus->dma_chunk_free(fore200e, &fore200e->host_cmdq.status);
409 case FORE200E_STATE_INITIALIZE:
410 /* nothing to do for that state */
412 case FORE200E_STATE_START_FW:
413 /* nothing to do for that state */
415 case FORE200E_STATE_LOAD_FW:
416 /* nothing to do for that state */
418 case FORE200E_STATE_RESET:
419 /* nothing to do for that state */
421 case FORE200E_STATE_MAP:
422 fore200e->bus->unmap(fore200e);
424 case FORE200E_STATE_CONFIGURE:
425 /* nothing to do for that state */
427 case FORE200E_STATE_REGISTER:
428 /* XXX shouldn't we *start* by deregistering the device? */
429 atm_dev_deregister(fore200e->atm_dev);
431 case FORE200E_STATE_BLANK:
432 /* nothing to do for that state */
433 break;
438 #ifdef CONFIG_ATM_FORE200E_PCA
440 static u32 fore200e_pca_read(volatile u32 __iomem *addr)
442 /* on big-endian hosts, the board is configured to convert
443 the endianess of slave RAM accesses */
444 return le32_to_cpu(readl(addr));
448 static void fore200e_pca_write(u32 val, volatile u32 __iomem *addr)
450 /* on big-endian hosts, the board is configured to convert
451 the endianess of slave RAM accesses */
452 writel(cpu_to_le32(val), addr);
456 static u32
457 fore200e_pca_dma_map(struct fore200e* fore200e, void* virt_addr, int size, int direction)
459 u32 dma_addr = pci_map_single((struct pci_dev*)fore200e->bus_dev, virt_addr, size, direction);
461 DPRINTK(3, "PCI DVMA mapping: virt_addr = 0x%p, size = %d, direction = %d, --> dma_addr = 0x%08x\n",
462 virt_addr, size, direction, dma_addr);
464 return dma_addr;
468 static void
469 fore200e_pca_dma_unmap(struct fore200e* fore200e, u32 dma_addr, int size, int direction)
471 DPRINTK(3, "PCI DVMA unmapping: dma_addr = 0x%08x, size = %d, direction = %d\n",
472 dma_addr, size, direction);
474 pci_unmap_single((struct pci_dev*)fore200e->bus_dev, dma_addr, size, direction);
478 static void
479 fore200e_pca_dma_sync_for_cpu(struct fore200e* fore200e, u32 dma_addr, int size, int direction)
481 DPRINTK(3, "PCI DVMA sync: dma_addr = 0x%08x, size = %d, direction = %d\n", dma_addr, size, direction);
483 pci_dma_sync_single_for_cpu((struct pci_dev*)fore200e->bus_dev, dma_addr, size, direction);
486 static void
487 fore200e_pca_dma_sync_for_device(struct fore200e* fore200e, u32 dma_addr, int size, int direction)
489 DPRINTK(3, "PCI DVMA sync: dma_addr = 0x%08x, size = %d, direction = %d\n", dma_addr, size, direction);
491 pci_dma_sync_single_for_device((struct pci_dev*)fore200e->bus_dev, dma_addr, size, direction);
495 /* allocate a DMA consistent chunk of memory intended to act as a communication mechanism
496 (to hold descriptors, status, queues, etc.) shared by the driver and the adapter */
498 static int
499 fore200e_pca_dma_chunk_alloc(struct fore200e* fore200e, struct chunk* chunk,
500 int size, int nbr, int alignment)
502 /* returned chunks are page-aligned */
503 chunk->alloc_size = size * nbr;
504 chunk->alloc_addr = pci_alloc_consistent((struct pci_dev*)fore200e->bus_dev,
505 chunk->alloc_size,
506 &chunk->dma_addr);
508 if ((chunk->alloc_addr == NULL) || (chunk->dma_addr == 0))
509 return -ENOMEM;
511 chunk->align_addr = chunk->alloc_addr;
513 return 0;
517 /* free a DMA consistent chunk of memory */
519 static void
520 fore200e_pca_dma_chunk_free(struct fore200e* fore200e, struct chunk* chunk)
522 pci_free_consistent((struct pci_dev*)fore200e->bus_dev,
523 chunk->alloc_size,
524 chunk->alloc_addr,
525 chunk->dma_addr);
529 static int
530 fore200e_pca_irq_check(struct fore200e* fore200e)
532 /* this is a 1 bit register */
533 int irq_posted = readl(fore200e->regs.pca.psr);
535 #if defined(CONFIG_ATM_FORE200E_DEBUG) && (CONFIG_ATM_FORE200E_DEBUG == 2)
536 if (irq_posted && (readl(fore200e->regs.pca.hcr) & PCA200E_HCR_OUTFULL)) {
537 DPRINTK(2,"FIFO OUT full, device %d\n", fore200e->atm_dev->number);
539 #endif
541 return irq_posted;
545 static void
546 fore200e_pca_irq_ack(struct fore200e* fore200e)
548 writel(PCA200E_HCR_CLRINTR, fore200e->regs.pca.hcr);
552 static void
553 fore200e_pca_reset(struct fore200e* fore200e)
555 writel(PCA200E_HCR_RESET, fore200e->regs.pca.hcr);
556 fore200e_spin(10);
557 writel(0, fore200e->regs.pca.hcr);
561 static int __init
562 fore200e_pca_map(struct fore200e* fore200e)
564 DPRINTK(2, "device %s being mapped in memory\n", fore200e->name);
566 fore200e->virt_base = ioremap(fore200e->phys_base, PCA200E_IOSPACE_LENGTH);
568 if (fore200e->virt_base == NULL) {
569 printk(FORE200E "can't map device %s\n", fore200e->name);
570 return -EFAULT;
573 DPRINTK(1, "device %s mapped to 0x%p\n", fore200e->name, fore200e->virt_base);
575 /* gain access to the PCA specific registers */
576 fore200e->regs.pca.hcr = fore200e->virt_base + PCA200E_HCR_OFFSET;
577 fore200e->regs.pca.imr = fore200e->virt_base + PCA200E_IMR_OFFSET;
578 fore200e->regs.pca.psr = fore200e->virt_base + PCA200E_PSR_OFFSET;
580 fore200e->state = FORE200E_STATE_MAP;
581 return 0;
585 static void
586 fore200e_pca_unmap(struct fore200e* fore200e)
588 DPRINTK(2, "device %s being unmapped from memory\n", fore200e->name);
590 if (fore200e->virt_base != NULL)
591 iounmap(fore200e->virt_base);
595 static int __init
596 fore200e_pca_configure(struct fore200e* fore200e)
598 struct pci_dev* pci_dev = (struct pci_dev*)fore200e->bus_dev;
599 u8 master_ctrl, latency;
601 DPRINTK(2, "device %s being configured\n", fore200e->name);
603 if ((pci_dev->irq == 0) || (pci_dev->irq == 0xFF)) {
604 printk(FORE200E "incorrect IRQ setting - misconfigured PCI-PCI bridge?\n");
605 return -EIO;
608 pci_read_config_byte(pci_dev, PCA200E_PCI_MASTER_CTRL, &master_ctrl);
610 master_ctrl = master_ctrl
611 #if defined(__BIG_ENDIAN)
612 /* request the PCA board to convert the endianess of slave RAM accesses */
613 | PCA200E_CTRL_CONVERT_ENDIAN
614 #endif
615 #if 0
616 | PCA200E_CTRL_DIS_CACHE_RD
617 | PCA200E_CTRL_DIS_WRT_INVAL
618 | PCA200E_CTRL_ENA_CONT_REQ_MODE
619 | PCA200E_CTRL_2_CACHE_WRT_INVAL
620 #endif
621 | PCA200E_CTRL_LARGE_PCI_BURSTS;
623 pci_write_config_byte(pci_dev, PCA200E_PCI_MASTER_CTRL, master_ctrl);
625 /* raise latency from 32 (default) to 192, as this seems to prevent NIC
626 lockups (under heavy rx loads) due to continuous 'FIFO OUT full' condition.
627 this may impact the performances of other PCI devices on the same bus, though */
628 latency = 192;
629 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
631 fore200e->state = FORE200E_STATE_CONFIGURE;
632 return 0;
636 static int __init
637 fore200e_pca_prom_read(struct fore200e* fore200e, struct prom_data* prom)
639 struct host_cmdq* cmdq = &fore200e->host_cmdq;
640 struct host_cmdq_entry* entry = &cmdq->host_entry[ cmdq->head ];
641 struct prom_opcode opcode;
642 int ok;
643 u32 prom_dma;
645 FORE200E_NEXT_ENTRY(cmdq->head, QUEUE_SIZE_CMD);
647 opcode.opcode = OPCODE_GET_PROM;
648 opcode.pad = 0;
650 prom_dma = fore200e->bus->dma_map(fore200e, prom, sizeof(struct prom_data), DMA_FROM_DEVICE);
652 fore200e->bus->write(prom_dma, &entry->cp_entry->cmd.prom_block.prom_haddr);
654 *entry->status = STATUS_PENDING;
656 fore200e->bus->write(*(u32*)&opcode, (u32 __iomem *)&entry->cp_entry->cmd.prom_block.opcode);
658 ok = fore200e_poll(fore200e, entry->status, STATUS_COMPLETE, 400);
660 *entry->status = STATUS_FREE;
662 fore200e->bus->dma_unmap(fore200e, prom_dma, sizeof(struct prom_data), DMA_FROM_DEVICE);
664 if (ok == 0) {
665 printk(FORE200E "unable to get PROM data from device %s\n", fore200e->name);
666 return -EIO;
669 #if defined(__BIG_ENDIAN)
671 #define swap_here(addr) (*((u32*)(addr)) = swab32( *((u32*)(addr)) ))
673 /* MAC address is stored as little-endian */
674 swap_here(&prom->mac_addr[0]);
675 swap_here(&prom->mac_addr[4]);
676 #endif
678 return 0;
682 static int
683 fore200e_pca_proc_read(struct fore200e* fore200e, char *page)
685 struct pci_dev* pci_dev = (struct pci_dev*)fore200e->bus_dev;
687 return sprintf(page, " PCI bus/slot/function:\t%d/%d/%d\n",
688 pci_dev->bus->number, PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn));
691 #endif /* CONFIG_ATM_FORE200E_PCA */
694 #ifdef CONFIG_ATM_FORE200E_SBA
696 static u32
697 fore200e_sba_read(volatile u32 __iomem *addr)
699 return sbus_readl(addr);
703 static void
704 fore200e_sba_write(u32 val, volatile u32 __iomem *addr)
706 sbus_writel(val, addr);
710 static u32
711 fore200e_sba_dma_map(struct fore200e* fore200e, void* virt_addr, int size, int direction)
713 u32 dma_addr = sbus_map_single((struct sbus_dev*)fore200e->bus_dev, virt_addr, size, direction);
715 DPRINTK(3, "SBUS DVMA mapping: virt_addr = 0x%p, size = %d, direction = %d --> dma_addr = 0x%08x\n",
716 virt_addr, size, direction, dma_addr);
718 return dma_addr;
722 static void
723 fore200e_sba_dma_unmap(struct fore200e* fore200e, u32 dma_addr, int size, int direction)
725 DPRINTK(3, "SBUS DVMA unmapping: dma_addr = 0x%08x, size = %d, direction = %d,\n",
726 dma_addr, size, direction);
728 sbus_unmap_single((struct sbus_dev*)fore200e->bus_dev, dma_addr, size, direction);
732 static void
733 fore200e_sba_dma_sync_for_cpu(struct fore200e* fore200e, u32 dma_addr, int size, int direction)
735 DPRINTK(3, "SBUS DVMA sync: dma_addr = 0x%08x, size = %d, direction = %d\n", dma_addr, size, direction);
737 sbus_dma_sync_single_for_cpu((struct sbus_dev*)fore200e->bus_dev, dma_addr, size, direction);
740 static void
741 fore200e_sba_dma_sync_for_device(struct fore200e* fore200e, u32 dma_addr, int size, int direction)
743 DPRINTK(3, "SBUS DVMA sync: dma_addr = 0x%08x, size = %d, direction = %d\n", dma_addr, size, direction);
745 sbus_dma_sync_single_for_device((struct sbus_dev*)fore200e->bus_dev, dma_addr, size, direction);
749 /* allocate a DVMA consistent chunk of memory intended to act as a communication mechanism
750 (to hold descriptors, status, queues, etc.) shared by the driver and the adapter */
752 static int
753 fore200e_sba_dma_chunk_alloc(struct fore200e* fore200e, struct chunk* chunk,
754 int size, int nbr, int alignment)
756 chunk->alloc_size = chunk->align_size = size * nbr;
758 /* returned chunks are page-aligned */
759 chunk->alloc_addr = sbus_alloc_consistent((struct sbus_dev*)fore200e->bus_dev,
760 chunk->alloc_size,
761 &chunk->dma_addr);
763 if ((chunk->alloc_addr == NULL) || (chunk->dma_addr == 0))
764 return -ENOMEM;
766 chunk->align_addr = chunk->alloc_addr;
768 return 0;
772 /* free a DVMA consistent chunk of memory */
774 static void
775 fore200e_sba_dma_chunk_free(struct fore200e* fore200e, struct chunk* chunk)
777 sbus_free_consistent((struct sbus_dev*)fore200e->bus_dev,
778 chunk->alloc_size,
779 chunk->alloc_addr,
780 chunk->dma_addr);
784 static void
785 fore200e_sba_irq_enable(struct fore200e* fore200e)
787 u32 hcr = fore200e->bus->read(fore200e->regs.sba.hcr) & SBA200E_HCR_STICKY;
788 fore200e->bus->write(hcr | SBA200E_HCR_INTR_ENA, fore200e->regs.sba.hcr);
792 static int
793 fore200e_sba_irq_check(struct fore200e* fore200e)
795 return fore200e->bus->read(fore200e->regs.sba.hcr) & SBA200E_HCR_INTR_REQ;
799 static void
800 fore200e_sba_irq_ack(struct fore200e* fore200e)
802 u32 hcr = fore200e->bus->read(fore200e->regs.sba.hcr) & SBA200E_HCR_STICKY;
803 fore200e->bus->write(hcr | SBA200E_HCR_INTR_CLR, fore200e->regs.sba.hcr);
807 static void
808 fore200e_sba_reset(struct fore200e* fore200e)
810 fore200e->bus->write(SBA200E_HCR_RESET, fore200e->regs.sba.hcr);
811 fore200e_spin(10);
812 fore200e->bus->write(0, fore200e->regs.sba.hcr);
816 static int __init
817 fore200e_sba_map(struct fore200e* fore200e)
819 struct sbus_dev* sbus_dev = (struct sbus_dev*)fore200e->bus_dev;
820 unsigned int bursts;
822 /* gain access to the SBA specific registers */
823 fore200e->regs.sba.hcr = sbus_ioremap(&sbus_dev->resource[0], 0, SBA200E_HCR_LENGTH, "SBA HCR");
824 fore200e->regs.sba.bsr = sbus_ioremap(&sbus_dev->resource[1], 0, SBA200E_BSR_LENGTH, "SBA BSR");
825 fore200e->regs.sba.isr = sbus_ioremap(&sbus_dev->resource[2], 0, SBA200E_ISR_LENGTH, "SBA ISR");
826 fore200e->virt_base = sbus_ioremap(&sbus_dev->resource[3], 0, SBA200E_RAM_LENGTH, "SBA RAM");
828 if (fore200e->virt_base == NULL) {
829 printk(FORE200E "unable to map RAM of device %s\n", fore200e->name);
830 return -EFAULT;
833 DPRINTK(1, "device %s mapped to 0x%p\n", fore200e->name, fore200e->virt_base);
835 fore200e->bus->write(0x02, fore200e->regs.sba.isr); /* XXX hardwired interrupt level */
837 /* get the supported DVMA burst sizes */
838 bursts = prom_getintdefault(sbus_dev->bus->prom_node, "burst-sizes", 0x00);
840 if (sbus_can_dma_64bit(sbus_dev))
841 sbus_set_sbus64(sbus_dev, bursts);
843 fore200e->state = FORE200E_STATE_MAP;
844 return 0;
848 static void
849 fore200e_sba_unmap(struct fore200e* fore200e)
851 sbus_iounmap(fore200e->regs.sba.hcr, SBA200E_HCR_LENGTH);
852 sbus_iounmap(fore200e->regs.sba.bsr, SBA200E_BSR_LENGTH);
853 sbus_iounmap(fore200e->regs.sba.isr, SBA200E_ISR_LENGTH);
854 sbus_iounmap(fore200e->virt_base, SBA200E_RAM_LENGTH);
858 static int __init
859 fore200e_sba_configure(struct fore200e* fore200e)
861 fore200e->state = FORE200E_STATE_CONFIGURE;
862 return 0;
866 static struct fore200e* __init
867 fore200e_sba_detect(const struct fore200e_bus* bus, int index)
869 struct fore200e* fore200e;
870 struct sbus_bus* sbus_bus;
871 struct sbus_dev* sbus_dev = NULL;
873 unsigned int count = 0;
875 for_each_sbus (sbus_bus) {
876 for_each_sbusdev (sbus_dev, sbus_bus) {
877 if (strcmp(sbus_dev->prom_name, SBA200E_PROM_NAME) == 0) {
878 if (count >= index)
879 goto found;
880 count++;
884 return NULL;
886 found:
887 if (sbus_dev->num_registers != 4) {
888 printk(FORE200E "this %s device has %d instead of 4 registers\n",
889 bus->model_name, sbus_dev->num_registers);
890 return NULL;
893 fore200e = fore200e_kmalloc(sizeof(struct fore200e), GFP_KERNEL);
894 if (fore200e == NULL)
895 return NULL;
897 fore200e->bus = bus;
898 fore200e->bus_dev = sbus_dev;
899 fore200e->irq = sbus_dev->irqs[ 0 ];
901 fore200e->phys_base = (unsigned long)sbus_dev;
903 sprintf(fore200e->name, "%s-%d", bus->model_name, index - 1);
905 return fore200e;
909 static int __init
910 fore200e_sba_prom_read(struct fore200e* fore200e, struct prom_data* prom)
912 struct sbus_dev* sbus_dev = (struct sbus_dev*) fore200e->bus_dev;
913 int len;
915 len = prom_getproperty(sbus_dev->prom_node, "macaddrlo2", &prom->mac_addr[ 4 ], 4);
916 if (len < 0)
917 return -EBUSY;
919 len = prom_getproperty(sbus_dev->prom_node, "macaddrhi4", &prom->mac_addr[ 2 ], 4);
920 if (len < 0)
921 return -EBUSY;
923 prom_getproperty(sbus_dev->prom_node, "serialnumber",
924 (char*)&prom->serial_number, sizeof(prom->serial_number));
926 prom_getproperty(sbus_dev->prom_node, "promversion",
927 (char*)&prom->hw_revision, sizeof(prom->hw_revision));
929 return 0;
933 static int
934 fore200e_sba_proc_read(struct fore200e* fore200e, char *page)
936 struct sbus_dev* sbus_dev = (struct sbus_dev*)fore200e->bus_dev;
938 return sprintf(page, " SBUS slot/device:\t\t%d/'%s'\n", sbus_dev->slot, sbus_dev->prom_name);
940 #endif /* CONFIG_ATM_FORE200E_SBA */
943 static void
944 fore200e_tx_irq(struct fore200e* fore200e)
946 struct host_txq* txq = &fore200e->host_txq;
947 struct host_txq_entry* entry;
948 struct atm_vcc* vcc;
949 struct fore200e_vc_map* vc_map;
951 if (fore200e->host_txq.txing == 0)
952 return;
954 for (;;) {
956 entry = &txq->host_entry[ txq->tail ];
958 if ((*entry->status & STATUS_COMPLETE) == 0) {
959 break;
962 DPRINTK(3, "TX COMPLETED: entry = %p [tail = %d], vc_map = %p, skb = %p\n",
963 entry, txq->tail, entry->vc_map, entry->skb);
965 /* free copy of misaligned data */
966 if (entry->data)
967 kfree(entry->data);
969 /* remove DMA mapping */
970 fore200e->bus->dma_unmap(fore200e, entry->tpd->tsd[ 0 ].buffer, entry->tpd->tsd[ 0 ].length,
971 DMA_TO_DEVICE);
973 vc_map = entry->vc_map;
975 /* vcc closed since the time the entry was submitted for tx? */
976 if ((vc_map->vcc == NULL) ||
977 (test_bit(ATM_VF_READY, &vc_map->vcc->flags) == 0)) {
979 DPRINTK(1, "no ready vcc found for PDU sent on device %d\n",
980 fore200e->atm_dev->number);
982 dev_kfree_skb_any(entry->skb);
984 else {
985 ASSERT(vc_map->vcc);
987 /* vcc closed then immediately re-opened? */
988 if (vc_map->incarn != entry->incarn) {
990 /* when a vcc is closed, some PDUs may be still pending in the tx queue.
991 if the same vcc is immediately re-opened, those pending PDUs must
992 not be popped after the completion of their emission, as they refer
993 to the prior incarnation of that vcc. otherwise, sk_atm(vcc)->sk_wmem_alloc
994 would be decremented by the size of the (unrelated) skb, possibly
995 leading to a negative sk->sk_wmem_alloc count, ultimately freezing the vcc.
996 we thus bind the tx entry to the current incarnation of the vcc
997 when the entry is submitted for tx. When the tx later completes,
998 if the incarnation number of the tx entry does not match the one
999 of the vcc, then this implies that the vcc has been closed then re-opened.
1000 we thus just drop the skb here. */
1002 DPRINTK(1, "vcc closed-then-re-opened; dropping PDU sent on device %d\n",
1003 fore200e->atm_dev->number);
1005 dev_kfree_skb_any(entry->skb);
1007 else {
1008 vcc = vc_map->vcc;
1009 ASSERT(vcc);
1011 /* notify tx completion */
1012 if (vcc->pop) {
1013 vcc->pop(vcc, entry->skb);
1015 else {
1016 dev_kfree_skb_any(entry->skb);
1018 #if 1
1019 /* race fixed by the above incarnation mechanism, but... */
1020 if (atomic_read(&sk_atm(vcc)->sk_wmem_alloc) < 0) {
1021 atomic_set(&sk_atm(vcc)->sk_wmem_alloc, 0);
1023 #endif
1024 /* check error condition */
1025 if (*entry->status & STATUS_ERROR)
1026 atomic_inc(&vcc->stats->tx_err);
1027 else
1028 atomic_inc(&vcc->stats->tx);
1032 *entry->status = STATUS_FREE;
1034 fore200e->host_txq.txing--;
1036 FORE200E_NEXT_ENTRY(txq->tail, QUEUE_SIZE_TX);
1041 #ifdef FORE200E_BSQ_DEBUG
1042 int bsq_audit(int where, struct host_bsq* bsq, int scheme, int magn)
1044 struct buffer* buffer;
1045 int count = 0;
1047 buffer = bsq->freebuf;
1048 while (buffer) {
1050 if (buffer->supplied) {
1051 printk(FORE200E "bsq_audit(%d): queue %d.%d, buffer %ld supplied but in free list!\n",
1052 where, scheme, magn, buffer->index);
1055 if (buffer->magn != magn) {
1056 printk(FORE200E "bsq_audit(%d): queue %d.%d, buffer %ld, unexpected magn = %d\n",
1057 where, scheme, magn, buffer->index, buffer->magn);
1060 if (buffer->scheme != scheme) {
1061 printk(FORE200E "bsq_audit(%d): queue %d.%d, buffer %ld, unexpected scheme = %d\n",
1062 where, scheme, magn, buffer->index, buffer->scheme);
1065 if ((buffer->index < 0) || (buffer->index >= fore200e_rx_buf_nbr[ scheme ][ magn ])) {
1066 printk(FORE200E "bsq_audit(%d): queue %d.%d, out of range buffer index = %ld !\n",
1067 where, scheme, magn, buffer->index);
1070 count++;
1071 buffer = buffer->next;
1074 if (count != bsq->freebuf_count) {
1075 printk(FORE200E "bsq_audit(%d): queue %d.%d, %d bufs in free list, but freebuf_count = %d\n",
1076 where, scheme, magn, count, bsq->freebuf_count);
1078 return 0;
1080 #endif
1083 static void
1084 fore200e_supply(struct fore200e* fore200e)
1086 int scheme, magn, i;
1088 struct host_bsq* bsq;
1089 struct host_bsq_entry* entry;
1090 struct buffer* buffer;
1092 for (scheme = 0; scheme < BUFFER_SCHEME_NBR; scheme++) {
1093 for (magn = 0; magn < BUFFER_MAGN_NBR; magn++) {
1095 bsq = &fore200e->host_bsq[ scheme ][ magn ];
1097 #ifdef FORE200E_BSQ_DEBUG
1098 bsq_audit(1, bsq, scheme, magn);
1099 #endif
1100 while (bsq->freebuf_count >= RBD_BLK_SIZE) {
1102 DPRINTK(2, "supplying %d rx buffers to queue %d / %d, freebuf_count = %d\n",
1103 RBD_BLK_SIZE, scheme, magn, bsq->freebuf_count);
1105 entry = &bsq->host_entry[ bsq->head ];
1107 for (i = 0; i < RBD_BLK_SIZE; i++) {
1109 /* take the first buffer in the free buffer list */
1110 buffer = bsq->freebuf;
1111 if (!buffer) {
1112 printk(FORE200E "no more free bufs in queue %d.%d, but freebuf_count = %d\n",
1113 scheme, magn, bsq->freebuf_count);
1114 return;
1116 bsq->freebuf = buffer->next;
1118 #ifdef FORE200E_BSQ_DEBUG
1119 if (buffer->supplied)
1120 printk(FORE200E "queue %d.%d, buffer %lu already supplied\n",
1121 scheme, magn, buffer->index);
1122 buffer->supplied = 1;
1123 #endif
1124 entry->rbd_block->rbd[ i ].buffer_haddr = buffer->data.dma_addr;
1125 entry->rbd_block->rbd[ i ].handle = FORE200E_BUF2HDL(buffer);
1128 FORE200E_NEXT_ENTRY(bsq->head, QUEUE_SIZE_BS);
1130 /* decrease accordingly the number of free rx buffers */
1131 bsq->freebuf_count -= RBD_BLK_SIZE;
1133 *entry->status = STATUS_PENDING;
1134 fore200e->bus->write(entry->rbd_block_dma, &entry->cp_entry->rbd_block_haddr);
1141 static int
1142 fore200e_push_rpd(struct fore200e* fore200e, struct atm_vcc* vcc, struct rpd* rpd)
1144 struct sk_buff* skb;
1145 struct buffer* buffer;
1146 struct fore200e_vcc* fore200e_vcc;
1147 int i, pdu_len = 0;
1148 #ifdef FORE200E_52BYTE_AAL0_SDU
1149 u32 cell_header = 0;
1150 #endif
1152 ASSERT(vcc);
1154 fore200e_vcc = FORE200E_VCC(vcc);
1155 ASSERT(fore200e_vcc);
1157 #ifdef FORE200E_52BYTE_AAL0_SDU
1158 if ((vcc->qos.aal == ATM_AAL0) && (vcc->qos.rxtp.max_sdu == ATM_AAL0_SDU)) {
1160 cell_header = (rpd->atm_header.gfc << ATM_HDR_GFC_SHIFT) |
1161 (rpd->atm_header.vpi << ATM_HDR_VPI_SHIFT) |
1162 (rpd->atm_header.vci << ATM_HDR_VCI_SHIFT) |
1163 (rpd->atm_header.plt << ATM_HDR_PTI_SHIFT) |
1164 rpd->atm_header.clp;
1165 pdu_len = 4;
1167 #endif
1169 /* compute total PDU length */
1170 for (i = 0; i < rpd->nseg; i++)
1171 pdu_len += rpd->rsd[ i ].length;
1173 skb = alloc_skb(pdu_len, GFP_ATOMIC);
1174 if (skb == NULL) {
1175 DPRINTK(2, "unable to alloc new skb, rx PDU length = %d\n", pdu_len);
1177 atomic_inc(&vcc->stats->rx_drop);
1178 return -ENOMEM;
1181 do_gettimeofday(&skb->stamp);
1183 #ifdef FORE200E_52BYTE_AAL0_SDU
1184 if (cell_header) {
1185 *((u32*)skb_put(skb, 4)) = cell_header;
1187 #endif
1189 /* reassemble segments */
1190 for (i = 0; i < rpd->nseg; i++) {
1192 /* rebuild rx buffer address from rsd handle */
1193 buffer = FORE200E_HDL2BUF(rpd->rsd[ i ].handle);
1195 /* Make device DMA transfer visible to CPU. */
1196 fore200e->bus->dma_sync_for_cpu(fore200e, buffer->data.dma_addr, rpd->rsd[ i ].length, DMA_FROM_DEVICE);
1198 memcpy(skb_put(skb, rpd->rsd[ i ].length), buffer->data.align_addr, rpd->rsd[ i ].length);
1200 /* Now let the device get at it again. */
1201 fore200e->bus->dma_sync_for_device(fore200e, buffer->data.dma_addr, rpd->rsd[ i ].length, DMA_FROM_DEVICE);
1204 DPRINTK(3, "rx skb: len = %d, truesize = %d\n", skb->len, skb->truesize);
1206 if (pdu_len < fore200e_vcc->rx_min_pdu)
1207 fore200e_vcc->rx_min_pdu = pdu_len;
1208 if (pdu_len > fore200e_vcc->rx_max_pdu)
1209 fore200e_vcc->rx_max_pdu = pdu_len;
1210 fore200e_vcc->rx_pdu++;
1212 /* push PDU */
1213 if (atm_charge(vcc, skb->truesize) == 0) {
1215 DPRINTK(2, "receive buffers saturated for %d.%d.%d - PDU dropped\n",
1216 vcc->itf, vcc->vpi, vcc->vci);
1218 dev_kfree_skb_any(skb);
1220 atomic_inc(&vcc->stats->rx_drop);
1221 return -ENOMEM;
1224 ASSERT(atomic_read(&sk_atm(vcc)->sk_wmem_alloc) >= 0);
1226 vcc->push(vcc, skb);
1227 atomic_inc(&vcc->stats->rx);
1229 ASSERT(atomic_read(&sk_atm(vcc)->sk_wmem_alloc) >= 0);
1231 return 0;
1235 static void
1236 fore200e_collect_rpd(struct fore200e* fore200e, struct rpd* rpd)
1238 struct host_bsq* bsq;
1239 struct buffer* buffer;
1240 int i;
1242 for (i = 0; i < rpd->nseg; i++) {
1244 /* rebuild rx buffer address from rsd handle */
1245 buffer = FORE200E_HDL2BUF(rpd->rsd[ i ].handle);
1247 bsq = &fore200e->host_bsq[ buffer->scheme ][ buffer->magn ];
1249 #ifdef FORE200E_BSQ_DEBUG
1250 bsq_audit(2, bsq, buffer->scheme, buffer->magn);
1252 if (buffer->supplied == 0)
1253 printk(FORE200E "queue %d.%d, buffer %ld was not supplied\n",
1254 buffer->scheme, buffer->magn, buffer->index);
1255 buffer->supplied = 0;
1256 #endif
1258 /* re-insert the buffer into the free buffer list */
1259 buffer->next = bsq->freebuf;
1260 bsq->freebuf = buffer;
1262 /* then increment the number of free rx buffers */
1263 bsq->freebuf_count++;
1268 static void
1269 fore200e_rx_irq(struct fore200e* fore200e)
1271 struct host_rxq* rxq = &fore200e->host_rxq;
1272 struct host_rxq_entry* entry;
1273 struct atm_vcc* vcc;
1274 struct fore200e_vc_map* vc_map;
1276 for (;;) {
1278 entry = &rxq->host_entry[ rxq->head ];
1280 /* no more received PDUs */
1281 if ((*entry->status & STATUS_COMPLETE) == 0)
1282 break;
1284 vc_map = FORE200E_VC_MAP(fore200e, entry->rpd->atm_header.vpi, entry->rpd->atm_header.vci);
1286 if ((vc_map->vcc == NULL) ||
1287 (test_bit(ATM_VF_READY, &vc_map->vcc->flags) == 0)) {
1289 DPRINTK(1, "no ready VC found for PDU received on %d.%d.%d\n",
1290 fore200e->atm_dev->number,
1291 entry->rpd->atm_header.vpi, entry->rpd->atm_header.vci);
1293 else {
1294 vcc = vc_map->vcc;
1295 ASSERT(vcc);
1297 if ((*entry->status & STATUS_ERROR) == 0) {
1299 fore200e_push_rpd(fore200e, vcc, entry->rpd);
1301 else {
1302 DPRINTK(2, "damaged PDU on %d.%d.%d\n",
1303 fore200e->atm_dev->number,
1304 entry->rpd->atm_header.vpi, entry->rpd->atm_header.vci);
1305 atomic_inc(&vcc->stats->rx_err);
1309 FORE200E_NEXT_ENTRY(rxq->head, QUEUE_SIZE_RX);
1311 fore200e_collect_rpd(fore200e, entry->rpd);
1313 /* rewrite the rpd address to ack the received PDU */
1314 fore200e->bus->write(entry->rpd_dma, &entry->cp_entry->rpd_haddr);
1315 *entry->status = STATUS_FREE;
1317 fore200e_supply(fore200e);
1322 #ifndef FORE200E_USE_TASKLET
1323 static void
1324 fore200e_irq(struct fore200e* fore200e)
1326 unsigned long flags;
1328 spin_lock_irqsave(&fore200e->q_lock, flags);
1329 fore200e_rx_irq(fore200e);
1330 spin_unlock_irqrestore(&fore200e->q_lock, flags);
1332 spin_lock_irqsave(&fore200e->q_lock, flags);
1333 fore200e_tx_irq(fore200e);
1334 spin_unlock_irqrestore(&fore200e->q_lock, flags);
1336 #endif
1339 static irqreturn_t
1340 fore200e_interrupt(int irq, void* dev, struct pt_regs* regs)
1342 struct fore200e* fore200e = FORE200E_DEV((struct atm_dev*)dev);
1344 if (fore200e->bus->irq_check(fore200e) == 0) {
1346 DPRINTK(3, "interrupt NOT triggered by device %d\n", fore200e->atm_dev->number);
1347 return IRQ_NONE;
1349 DPRINTK(3, "interrupt triggered by device %d\n", fore200e->atm_dev->number);
1351 #ifdef FORE200E_USE_TASKLET
1352 tasklet_schedule(&fore200e->tx_tasklet);
1353 tasklet_schedule(&fore200e->rx_tasklet);
1354 #else
1355 fore200e_irq(fore200e);
1356 #endif
1358 fore200e->bus->irq_ack(fore200e);
1359 return IRQ_HANDLED;
1363 #ifdef FORE200E_USE_TASKLET
1364 static void
1365 fore200e_tx_tasklet(unsigned long data)
1367 struct fore200e* fore200e = (struct fore200e*) data;
1368 unsigned long flags;
1370 DPRINTK(3, "tx tasklet scheduled for device %d\n", fore200e->atm_dev->number);
1372 spin_lock_irqsave(&fore200e->q_lock, flags);
1373 fore200e_tx_irq(fore200e);
1374 spin_unlock_irqrestore(&fore200e->q_lock, flags);
1378 static void
1379 fore200e_rx_tasklet(unsigned long data)
1381 struct fore200e* fore200e = (struct fore200e*) data;
1382 unsigned long flags;
1384 DPRINTK(3, "rx tasklet scheduled for device %d\n", fore200e->atm_dev->number);
1386 spin_lock_irqsave(&fore200e->q_lock, flags);
1387 fore200e_rx_irq((struct fore200e*) data);
1388 spin_unlock_irqrestore(&fore200e->q_lock, flags);
1390 #endif
1393 static int
1394 fore200e_select_scheme(struct atm_vcc* vcc)
1396 /* fairly balance the VCs over (identical) buffer schemes */
1397 int scheme = vcc->vci % 2 ? BUFFER_SCHEME_ONE : BUFFER_SCHEME_TWO;
1399 DPRINTK(1, "VC %d.%d.%d uses buffer scheme %d\n",
1400 vcc->itf, vcc->vpi, vcc->vci, scheme);
1402 return scheme;
1406 static int
1407 fore200e_activate_vcin(struct fore200e* fore200e, int activate, struct atm_vcc* vcc, int mtu)
1409 struct host_cmdq* cmdq = &fore200e->host_cmdq;
1410 struct host_cmdq_entry* entry = &cmdq->host_entry[ cmdq->head ];
1411 struct activate_opcode activ_opcode;
1412 struct deactivate_opcode deactiv_opcode;
1413 struct vpvc vpvc;
1414 int ok;
1415 enum fore200e_aal aal = fore200e_atm2fore_aal(vcc->qos.aal);
1417 FORE200E_NEXT_ENTRY(cmdq->head, QUEUE_SIZE_CMD);
1419 if (activate) {
1420 FORE200E_VCC(vcc)->scheme = fore200e_select_scheme(vcc);
1422 activ_opcode.opcode = OPCODE_ACTIVATE_VCIN;
1423 activ_opcode.aal = aal;
1424 activ_opcode.scheme = FORE200E_VCC(vcc)->scheme;
1425 activ_opcode.pad = 0;
1427 else {
1428 deactiv_opcode.opcode = OPCODE_DEACTIVATE_VCIN;
1429 deactiv_opcode.pad = 0;
1432 vpvc.vci = vcc->vci;
1433 vpvc.vpi = vcc->vpi;
1435 *entry->status = STATUS_PENDING;
1437 if (activate) {
1439 #ifdef FORE200E_52BYTE_AAL0_SDU
1440 mtu = 48;
1441 #endif
1442 /* the MTU is not used by the cp, except in the case of AAL0 */
1443 fore200e->bus->write(mtu, &entry->cp_entry->cmd.activate_block.mtu);
1444 fore200e->bus->write(*(u32*)&vpvc, (u32 __iomem *)&entry->cp_entry->cmd.activate_block.vpvc);
1445 fore200e->bus->write(*(u32*)&activ_opcode, (u32 __iomem *)&entry->cp_entry->cmd.activate_block.opcode);
1447 else {
1448 fore200e->bus->write(*(u32*)&vpvc, (u32 __iomem *)&entry->cp_entry->cmd.deactivate_block.vpvc);
1449 fore200e->bus->write(*(u32*)&deactiv_opcode, (u32 __iomem *)&entry->cp_entry->cmd.deactivate_block.opcode);
1452 ok = fore200e_poll(fore200e, entry->status, STATUS_COMPLETE, 400);
1454 *entry->status = STATUS_FREE;
1456 if (ok == 0) {
1457 printk(FORE200E "unable to %s VC %d.%d.%d\n",
1458 activate ? "open" : "close", vcc->itf, vcc->vpi, vcc->vci);
1459 return -EIO;
1462 DPRINTK(1, "VC %d.%d.%d %sed\n", vcc->itf, vcc->vpi, vcc->vci,
1463 activate ? "open" : "clos");
1465 return 0;
1469 #define FORE200E_MAX_BACK2BACK_CELLS 255 /* XXX depends on CDVT */
1471 static void
1472 fore200e_rate_ctrl(struct atm_qos* qos, struct tpd_rate* rate)
1474 if (qos->txtp.max_pcr < ATM_OC3_PCR) {
1476 /* compute the data cells to idle cells ratio from the tx PCR */
1477 rate->data_cells = qos->txtp.max_pcr * FORE200E_MAX_BACK2BACK_CELLS / ATM_OC3_PCR;
1478 rate->idle_cells = FORE200E_MAX_BACK2BACK_CELLS - rate->data_cells;
1480 else {
1481 /* disable rate control */
1482 rate->data_cells = rate->idle_cells = 0;
1487 static int
1488 fore200e_open(struct atm_vcc *vcc)
1490 struct fore200e* fore200e = FORE200E_DEV(vcc->dev);
1491 struct fore200e_vcc* fore200e_vcc;
1492 struct fore200e_vc_map* vc_map;
1493 unsigned long flags;
1494 int vci = vcc->vci;
1495 short vpi = vcc->vpi;
1497 ASSERT((vpi >= 0) && (vpi < 1<<FORE200E_VPI_BITS));
1498 ASSERT((vci >= 0) && (vci < 1<<FORE200E_VCI_BITS));
1500 spin_lock_irqsave(&fore200e->q_lock, flags);
1502 vc_map = FORE200E_VC_MAP(fore200e, vpi, vci);
1503 if (vc_map->vcc) {
1505 spin_unlock_irqrestore(&fore200e->q_lock, flags);
1507 printk(FORE200E "VC %d.%d.%d already in use\n",
1508 fore200e->atm_dev->number, vpi, vci);
1510 return -EINVAL;
1513 vc_map->vcc = vcc;
1515 spin_unlock_irqrestore(&fore200e->q_lock, flags);
1517 fore200e_vcc = fore200e_kmalloc(sizeof(struct fore200e_vcc), GFP_ATOMIC);
1518 if (fore200e_vcc == NULL) {
1519 vc_map->vcc = NULL;
1520 return -ENOMEM;
1523 DPRINTK(2, "opening %d.%d.%d:%d QoS = (tx: cl=%s, pcr=%d-%d, cdv=%d, max_sdu=%d; "
1524 "rx: cl=%s, pcr=%d-%d, cdv=%d, max_sdu=%d)\n",
1525 vcc->itf, vcc->vpi, vcc->vci, fore200e_atm2fore_aal(vcc->qos.aal),
1526 fore200e_traffic_class[ vcc->qos.txtp.traffic_class ],
1527 vcc->qos.txtp.min_pcr, vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_cdv, vcc->qos.txtp.max_sdu,
1528 fore200e_traffic_class[ vcc->qos.rxtp.traffic_class ],
1529 vcc->qos.rxtp.min_pcr, vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_cdv, vcc->qos.rxtp.max_sdu);
1531 /* pseudo-CBR bandwidth requested? */
1532 if ((vcc->qos.txtp.traffic_class == ATM_CBR) && (vcc->qos.txtp.max_pcr > 0)) {
1534 down(&fore200e->rate_sf);
1535 if (fore200e->available_cell_rate < vcc->qos.txtp.max_pcr) {
1536 up(&fore200e->rate_sf);
1538 fore200e_kfree(fore200e_vcc);
1539 vc_map->vcc = NULL;
1540 return -EAGAIN;
1543 /* reserve bandwidth */
1544 fore200e->available_cell_rate -= vcc->qos.txtp.max_pcr;
1545 up(&fore200e->rate_sf);
1548 vcc->itf = vcc->dev->number;
1550 set_bit(ATM_VF_PARTIAL,&vcc->flags);
1551 set_bit(ATM_VF_ADDR, &vcc->flags);
1553 vcc->dev_data = fore200e_vcc;
1555 if (fore200e_activate_vcin(fore200e, 1, vcc, vcc->qos.rxtp.max_sdu) < 0) {
1557 vc_map->vcc = NULL;
1559 clear_bit(ATM_VF_ADDR, &vcc->flags);
1560 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1562 vcc->dev_data = NULL;
1564 fore200e->available_cell_rate += vcc->qos.txtp.max_pcr;
1566 fore200e_kfree(fore200e_vcc);
1567 return -EINVAL;
1570 /* compute rate control parameters */
1571 if ((vcc->qos.txtp.traffic_class == ATM_CBR) && (vcc->qos.txtp.max_pcr > 0)) {
1573 fore200e_rate_ctrl(&vcc->qos, &fore200e_vcc->rate);
1574 set_bit(ATM_VF_HASQOS, &vcc->flags);
1576 DPRINTK(3, "tx on %d.%d.%d:%d, tx PCR = %d, rx PCR = %d, data_cells = %u, idle_cells = %u\n",
1577 vcc->itf, vcc->vpi, vcc->vci, fore200e_atm2fore_aal(vcc->qos.aal),
1578 vcc->qos.txtp.max_pcr, vcc->qos.rxtp.max_pcr,
1579 fore200e_vcc->rate.data_cells, fore200e_vcc->rate.idle_cells);
1582 fore200e_vcc->tx_min_pdu = fore200e_vcc->rx_min_pdu = MAX_PDU_SIZE + 1;
1583 fore200e_vcc->tx_max_pdu = fore200e_vcc->rx_max_pdu = 0;
1584 fore200e_vcc->tx_pdu = fore200e_vcc->rx_pdu = 0;
1586 /* new incarnation of the vcc */
1587 vc_map->incarn = ++fore200e->incarn_count;
1589 /* VC unusable before this flag is set */
1590 set_bit(ATM_VF_READY, &vcc->flags);
1592 return 0;
1596 static void
1597 fore200e_close(struct atm_vcc* vcc)
1599 struct fore200e* fore200e = FORE200E_DEV(vcc->dev);
1600 struct fore200e_vcc* fore200e_vcc;
1601 struct fore200e_vc_map* vc_map;
1602 unsigned long flags;
1604 ASSERT(vcc);
1605 ASSERT((vcc->vpi >= 0) && (vcc->vpi < 1<<FORE200E_VPI_BITS));
1606 ASSERT((vcc->vci >= 0) && (vcc->vci < 1<<FORE200E_VCI_BITS));
1608 DPRINTK(2, "closing %d.%d.%d:%d\n", vcc->itf, vcc->vpi, vcc->vci, fore200e_atm2fore_aal(vcc->qos.aal));
1610 clear_bit(ATM_VF_READY, &vcc->flags);
1612 fore200e_activate_vcin(fore200e, 0, vcc, 0);
1614 spin_lock_irqsave(&fore200e->q_lock, flags);
1616 vc_map = FORE200E_VC_MAP(fore200e, vcc->vpi, vcc->vci);
1618 /* the vc is no longer considered as "in use" by fore200e_open() */
1619 vc_map->vcc = NULL;
1621 vcc->itf = vcc->vci = vcc->vpi = 0;
1623 fore200e_vcc = FORE200E_VCC(vcc);
1624 vcc->dev_data = NULL;
1626 spin_unlock_irqrestore(&fore200e->q_lock, flags);
1628 /* release reserved bandwidth, if any */
1629 if ((vcc->qos.txtp.traffic_class == ATM_CBR) && (vcc->qos.txtp.max_pcr > 0)) {
1631 down(&fore200e->rate_sf);
1632 fore200e->available_cell_rate += vcc->qos.txtp.max_pcr;
1633 up(&fore200e->rate_sf);
1635 clear_bit(ATM_VF_HASQOS, &vcc->flags);
1638 clear_bit(ATM_VF_ADDR, &vcc->flags);
1639 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1641 ASSERT(fore200e_vcc);
1642 fore200e_kfree(fore200e_vcc);
1646 static int
1647 fore200e_send(struct atm_vcc *vcc, struct sk_buff *skb)
1649 struct fore200e* fore200e = FORE200E_DEV(vcc->dev);
1650 struct fore200e_vcc* fore200e_vcc = FORE200E_VCC(vcc);
1651 struct fore200e_vc_map* vc_map;
1652 struct host_txq* txq = &fore200e->host_txq;
1653 struct host_txq_entry* entry;
1654 struct tpd* tpd;
1655 struct tpd_haddr tpd_haddr;
1656 int retry = CONFIG_ATM_FORE200E_TX_RETRY;
1657 int tx_copy = 0;
1658 int tx_len = skb->len;
1659 u32* cell_header = NULL;
1660 unsigned char* skb_data;
1661 int skb_len;
1662 unsigned char* data;
1663 unsigned long flags;
1665 ASSERT(vcc);
1666 ASSERT(atomic_read(&sk_atm(vcc)->sk_wmem_alloc) >= 0);
1667 ASSERT(fore200e);
1668 ASSERT(fore200e_vcc);
1670 if (!test_bit(ATM_VF_READY, &vcc->flags)) {
1671 DPRINTK(1, "VC %d.%d.%d not ready for tx\n", vcc->itf, vcc->vpi, vcc->vpi);
1672 dev_kfree_skb_any(skb);
1673 return -EINVAL;
1676 #ifdef FORE200E_52BYTE_AAL0_SDU
1677 if ((vcc->qos.aal == ATM_AAL0) && (vcc->qos.txtp.max_sdu == ATM_AAL0_SDU)) {
1678 cell_header = (u32*) skb->data;
1679 skb_data = skb->data + 4; /* skip 4-byte cell header */
1680 skb_len = tx_len = skb->len - 4;
1682 DPRINTK(3, "user-supplied cell header = 0x%08x\n", *cell_header);
1684 else
1685 #endif
1687 skb_data = skb->data;
1688 skb_len = skb->len;
1691 if (((unsigned long)skb_data) & 0x3) {
1693 DPRINTK(2, "misaligned tx PDU on device %s\n", fore200e->name);
1694 tx_copy = 1;
1695 tx_len = skb_len;
1698 if ((vcc->qos.aal == ATM_AAL0) && (skb_len % ATM_CELL_PAYLOAD)) {
1700 /* this simply NUKES the PCA board */
1701 DPRINTK(2, "incomplete tx AAL0 PDU on device %s\n", fore200e->name);
1702 tx_copy = 1;
1703 tx_len = ((skb_len / ATM_CELL_PAYLOAD) + 1) * ATM_CELL_PAYLOAD;
1706 if (tx_copy) {
1707 data = kmalloc(tx_len, GFP_ATOMIC | GFP_DMA);
1708 if (data == NULL) {
1709 if (vcc->pop) {
1710 vcc->pop(vcc, skb);
1712 else {
1713 dev_kfree_skb_any(skb);
1715 return -ENOMEM;
1718 memcpy(data, skb_data, skb_len);
1719 if (skb_len < tx_len)
1720 memset(data + skb_len, 0x00, tx_len - skb_len);
1722 else {
1723 data = skb_data;
1726 vc_map = FORE200E_VC_MAP(fore200e, vcc->vpi, vcc->vci);
1727 ASSERT(vc_map->vcc == vcc);
1729 retry_here:
1731 spin_lock_irqsave(&fore200e->q_lock, flags);
1733 entry = &txq->host_entry[ txq->head ];
1735 if ((*entry->status != STATUS_FREE) || (txq->txing >= QUEUE_SIZE_TX - 2)) {
1737 /* try to free completed tx queue entries */
1738 fore200e_tx_irq(fore200e);
1740 if (*entry->status != STATUS_FREE) {
1742 spin_unlock_irqrestore(&fore200e->q_lock, flags);
1744 /* retry once again? */
1745 if (--retry > 0) {
1746 udelay(50);
1747 goto retry_here;
1750 atomic_inc(&vcc->stats->tx_err);
1752 fore200e->tx_sat++;
1753 DPRINTK(2, "tx queue of device %s is saturated, PDU dropped - heartbeat is %08x\n",
1754 fore200e->name, fore200e->cp_queues->heartbeat);
1755 if (vcc->pop) {
1756 vcc->pop(vcc, skb);
1758 else {
1759 dev_kfree_skb_any(skb);
1762 if (tx_copy)
1763 kfree(data);
1765 return -ENOBUFS;
1769 entry->incarn = vc_map->incarn;
1770 entry->vc_map = vc_map;
1771 entry->skb = skb;
1772 entry->data = tx_copy ? data : NULL;
1774 tpd = entry->tpd;
1775 tpd->tsd[ 0 ].buffer = fore200e->bus->dma_map(fore200e, data, tx_len, DMA_TO_DEVICE);
1776 tpd->tsd[ 0 ].length = tx_len;
1778 FORE200E_NEXT_ENTRY(txq->head, QUEUE_SIZE_TX);
1779 txq->txing++;
1781 /* The dma_map call above implies a dma_sync so the device can use it,
1782 * thus no explicit dma_sync call is necessary here.
1785 DPRINTK(3, "tx on %d.%d.%d:%d, len = %u (%u)\n",
1786 vcc->itf, vcc->vpi, vcc->vci, fore200e_atm2fore_aal(vcc->qos.aal),
1787 tpd->tsd[0].length, skb_len);
1789 if (skb_len < fore200e_vcc->tx_min_pdu)
1790 fore200e_vcc->tx_min_pdu = skb_len;
1791 if (skb_len > fore200e_vcc->tx_max_pdu)
1792 fore200e_vcc->tx_max_pdu = skb_len;
1793 fore200e_vcc->tx_pdu++;
1795 /* set tx rate control information */
1796 tpd->rate.data_cells = fore200e_vcc->rate.data_cells;
1797 tpd->rate.idle_cells = fore200e_vcc->rate.idle_cells;
1799 if (cell_header) {
1800 tpd->atm_header.clp = (*cell_header & ATM_HDR_CLP);
1801 tpd->atm_header.plt = (*cell_header & ATM_HDR_PTI_MASK) >> ATM_HDR_PTI_SHIFT;
1802 tpd->atm_header.vci = (*cell_header & ATM_HDR_VCI_MASK) >> ATM_HDR_VCI_SHIFT;
1803 tpd->atm_header.vpi = (*cell_header & ATM_HDR_VPI_MASK) >> ATM_HDR_VPI_SHIFT;
1804 tpd->atm_header.gfc = (*cell_header & ATM_HDR_GFC_MASK) >> ATM_HDR_GFC_SHIFT;
1806 else {
1807 /* set the ATM header, common to all cells conveying the PDU */
1808 tpd->atm_header.clp = 0;
1809 tpd->atm_header.plt = 0;
1810 tpd->atm_header.vci = vcc->vci;
1811 tpd->atm_header.vpi = vcc->vpi;
1812 tpd->atm_header.gfc = 0;
1815 tpd->spec.length = tx_len;
1816 tpd->spec.nseg = 1;
1817 tpd->spec.aal = fore200e_atm2fore_aal(vcc->qos.aal);
1818 tpd->spec.intr = 1;
1820 tpd_haddr.size = sizeof(struct tpd) / (1<<TPD_HADDR_SHIFT); /* size is expressed in 32 byte blocks */
1821 tpd_haddr.pad = 0;
1822 tpd_haddr.haddr = entry->tpd_dma >> TPD_HADDR_SHIFT; /* shift the address, as we are in a bitfield */
1824 *entry->status = STATUS_PENDING;
1825 fore200e->bus->write(*(u32*)&tpd_haddr, (u32 __iomem *)&entry->cp_entry->tpd_haddr);
1827 spin_unlock_irqrestore(&fore200e->q_lock, flags);
1829 return 0;
1833 static int
1834 fore200e_getstats(struct fore200e* fore200e)
1836 struct host_cmdq* cmdq = &fore200e->host_cmdq;
1837 struct host_cmdq_entry* entry = &cmdq->host_entry[ cmdq->head ];
1838 struct stats_opcode opcode;
1839 int ok;
1840 u32 stats_dma_addr;
1842 if (fore200e->stats == NULL) {
1843 fore200e->stats = fore200e_kmalloc(sizeof(struct stats), GFP_KERNEL | GFP_DMA);
1844 if (fore200e->stats == NULL)
1845 return -ENOMEM;
1848 stats_dma_addr = fore200e->bus->dma_map(fore200e, fore200e->stats,
1849 sizeof(struct stats), DMA_FROM_DEVICE);
1851 FORE200E_NEXT_ENTRY(cmdq->head, QUEUE_SIZE_CMD);
1853 opcode.opcode = OPCODE_GET_STATS;
1854 opcode.pad = 0;
1856 fore200e->bus->write(stats_dma_addr, &entry->cp_entry->cmd.stats_block.stats_haddr);
1858 *entry->status = STATUS_PENDING;
1860 fore200e->bus->write(*(u32*)&opcode, (u32 __iomem *)&entry->cp_entry->cmd.stats_block.opcode);
1862 ok = fore200e_poll(fore200e, entry->status, STATUS_COMPLETE, 400);
1864 *entry->status = STATUS_FREE;
1866 fore200e->bus->dma_unmap(fore200e, stats_dma_addr, sizeof(struct stats), DMA_FROM_DEVICE);
1868 if (ok == 0) {
1869 printk(FORE200E "unable to get statistics from device %s\n", fore200e->name);
1870 return -EIO;
1873 return 0;
1877 static int
1878 fore200e_getsockopt(struct atm_vcc* vcc, int level, int optname, void __user *optval, int optlen)
1880 /* struct fore200e* fore200e = FORE200E_DEV(vcc->dev); */
1882 DPRINTK(2, "getsockopt %d.%d.%d, level = %d, optname = 0x%x, optval = 0x%p, optlen = %d\n",
1883 vcc->itf, vcc->vpi, vcc->vci, level, optname, optval, optlen);
1885 return -EINVAL;
1889 static int
1890 fore200e_setsockopt(struct atm_vcc* vcc, int level, int optname, void __user *optval, int optlen)
1892 /* struct fore200e* fore200e = FORE200E_DEV(vcc->dev); */
1894 DPRINTK(2, "setsockopt %d.%d.%d, level = %d, optname = 0x%x, optval = 0x%p, optlen = %d\n",
1895 vcc->itf, vcc->vpi, vcc->vci, level, optname, optval, optlen);
1897 return -EINVAL;
1901 #if 0 /* currently unused */
1902 static int
1903 fore200e_get_oc3(struct fore200e* fore200e, struct oc3_regs* regs)
1905 struct host_cmdq* cmdq = &fore200e->host_cmdq;
1906 struct host_cmdq_entry* entry = &cmdq->host_entry[ cmdq->head ];
1907 struct oc3_opcode opcode;
1908 int ok;
1909 u32 oc3_regs_dma_addr;
1911 oc3_regs_dma_addr = fore200e->bus->dma_map(fore200e, regs, sizeof(struct oc3_regs), DMA_FROM_DEVICE);
1913 FORE200E_NEXT_ENTRY(cmdq->head, QUEUE_SIZE_CMD);
1915 opcode.opcode = OPCODE_GET_OC3;
1916 opcode.reg = 0;
1917 opcode.value = 0;
1918 opcode.mask = 0;
1920 fore200e->bus->write(oc3_regs_dma_addr, &entry->cp_entry->cmd.oc3_block.regs_haddr);
1922 *entry->status = STATUS_PENDING;
1924 fore200e->bus->write(*(u32*)&opcode, (u32*)&entry->cp_entry->cmd.oc3_block.opcode);
1926 ok = fore200e_poll(fore200e, entry->status, STATUS_COMPLETE, 400);
1928 *entry->status = STATUS_FREE;
1930 fore200e->bus->dma_unmap(fore200e, oc3_regs_dma_addr, sizeof(struct oc3_regs), DMA_FROM_DEVICE);
1932 if (ok == 0) {
1933 printk(FORE200E "unable to get OC-3 regs of device %s\n", fore200e->name);
1934 return -EIO;
1937 return 0;
1939 #endif
1942 static int
1943 fore200e_set_oc3(struct fore200e* fore200e, u32 reg, u32 value, u32 mask)
1945 struct host_cmdq* cmdq = &fore200e->host_cmdq;
1946 struct host_cmdq_entry* entry = &cmdq->host_entry[ cmdq->head ];
1947 struct oc3_opcode opcode;
1948 int ok;
1950 DPRINTK(2, "set OC-3 reg = 0x%02x, value = 0x%02x, mask = 0x%02x\n", reg, value, mask);
1952 FORE200E_NEXT_ENTRY(cmdq->head, QUEUE_SIZE_CMD);
1954 opcode.opcode = OPCODE_SET_OC3;
1955 opcode.reg = reg;
1956 opcode.value = value;
1957 opcode.mask = mask;
1959 fore200e->bus->write(0, &entry->cp_entry->cmd.oc3_block.regs_haddr);
1961 *entry->status = STATUS_PENDING;
1963 fore200e->bus->write(*(u32*)&opcode, (u32 __iomem *)&entry->cp_entry->cmd.oc3_block.opcode);
1965 ok = fore200e_poll(fore200e, entry->status, STATUS_COMPLETE, 400);
1967 *entry->status = STATUS_FREE;
1969 if (ok == 0) {
1970 printk(FORE200E "unable to set OC-3 reg 0x%02x of device %s\n", reg, fore200e->name);
1971 return -EIO;
1974 return 0;
1978 static int
1979 fore200e_setloop(struct fore200e* fore200e, int loop_mode)
1981 u32 mct_value, mct_mask;
1982 int error;
1984 if (!capable(CAP_NET_ADMIN))
1985 return -EPERM;
1987 switch (loop_mode) {
1989 case ATM_LM_NONE:
1990 mct_value = 0;
1991 mct_mask = SUNI_MCT_DLE | SUNI_MCT_LLE;
1992 break;
1994 case ATM_LM_LOC_PHY:
1995 mct_value = mct_mask = SUNI_MCT_DLE;
1996 break;
1998 case ATM_LM_RMT_PHY:
1999 mct_value = mct_mask = SUNI_MCT_LLE;
2000 break;
2002 default:
2003 return -EINVAL;
2006 error = fore200e_set_oc3(fore200e, SUNI_MCT, mct_value, mct_mask);
2007 if (error == 0)
2008 fore200e->loop_mode = loop_mode;
2010 return error;
2014 static inline unsigned int
2015 fore200e_swap(unsigned int in)
2017 #if defined(__LITTLE_ENDIAN)
2018 return swab32(in);
2019 #else
2020 return in;
2021 #endif
2025 static int
2026 fore200e_fetch_stats(struct fore200e* fore200e, struct sonet_stats __user *arg)
2028 struct sonet_stats tmp;
2030 if (fore200e_getstats(fore200e) < 0)
2031 return -EIO;
2033 tmp.section_bip = fore200e_swap(fore200e->stats->oc3.section_bip8_errors);
2034 tmp.line_bip = fore200e_swap(fore200e->stats->oc3.line_bip24_errors);
2035 tmp.path_bip = fore200e_swap(fore200e->stats->oc3.path_bip8_errors);
2036 tmp.line_febe = fore200e_swap(fore200e->stats->oc3.line_febe_errors);
2037 tmp.path_febe = fore200e_swap(fore200e->stats->oc3.path_febe_errors);
2038 tmp.corr_hcs = fore200e_swap(fore200e->stats->oc3.corr_hcs_errors);
2039 tmp.uncorr_hcs = fore200e_swap(fore200e->stats->oc3.ucorr_hcs_errors);
2040 tmp.tx_cells = fore200e_swap(fore200e->stats->aal0.cells_transmitted) +
2041 fore200e_swap(fore200e->stats->aal34.cells_transmitted) +
2042 fore200e_swap(fore200e->stats->aal5.cells_transmitted);
2043 tmp.rx_cells = fore200e_swap(fore200e->stats->aal0.cells_received) +
2044 fore200e_swap(fore200e->stats->aal34.cells_received) +
2045 fore200e_swap(fore200e->stats->aal5.cells_received);
2047 if (arg)
2048 return copy_to_user(arg, &tmp, sizeof(struct sonet_stats)) ? -EFAULT : 0;
2050 return 0;
2054 static int
2055 fore200e_ioctl(struct atm_dev* dev, unsigned int cmd, void __user * arg)
2057 struct fore200e* fore200e = FORE200E_DEV(dev);
2059 DPRINTK(2, "ioctl cmd = 0x%x (%u), arg = 0x%p (%lu)\n", cmd, cmd, arg, (unsigned long)arg);
2061 switch (cmd) {
2063 case SONET_GETSTAT:
2064 return fore200e_fetch_stats(fore200e, (struct sonet_stats __user *)arg);
2066 case SONET_GETDIAG:
2067 return put_user(0, (int __user *)arg) ? -EFAULT : 0;
2069 case ATM_SETLOOP:
2070 return fore200e_setloop(fore200e, (int)(unsigned long)arg);
2072 case ATM_GETLOOP:
2073 return put_user(fore200e->loop_mode, (int __user *)arg) ? -EFAULT : 0;
2075 case ATM_QUERYLOOP:
2076 return put_user(ATM_LM_LOC_PHY | ATM_LM_RMT_PHY, (int __user *)arg) ? -EFAULT : 0;
2079 return -ENOSYS; /* not implemented */
2083 static int
2084 fore200e_change_qos(struct atm_vcc* vcc,struct atm_qos* qos, int flags)
2086 struct fore200e_vcc* fore200e_vcc = FORE200E_VCC(vcc);
2087 struct fore200e* fore200e = FORE200E_DEV(vcc->dev);
2089 if (!test_bit(ATM_VF_READY, &vcc->flags)) {
2090 DPRINTK(1, "VC %d.%d.%d not ready for QoS change\n", vcc->itf, vcc->vpi, vcc->vpi);
2091 return -EINVAL;
2094 DPRINTK(2, "change_qos %d.%d.%d, "
2095 "(tx: cl=%s, pcr=%d-%d, cdv=%d, max_sdu=%d; "
2096 "rx: cl=%s, pcr=%d-%d, cdv=%d, max_sdu=%d), flags = 0x%x\n"
2097 "available_cell_rate = %u",
2098 vcc->itf, vcc->vpi, vcc->vci,
2099 fore200e_traffic_class[ qos->txtp.traffic_class ],
2100 qos->txtp.min_pcr, qos->txtp.max_pcr, qos->txtp.max_cdv, qos->txtp.max_sdu,
2101 fore200e_traffic_class[ qos->rxtp.traffic_class ],
2102 qos->rxtp.min_pcr, qos->rxtp.max_pcr, qos->rxtp.max_cdv, qos->rxtp.max_sdu,
2103 flags, fore200e->available_cell_rate);
2105 if ((qos->txtp.traffic_class == ATM_CBR) && (qos->txtp.max_pcr > 0)) {
2107 down(&fore200e->rate_sf);
2108 if (fore200e->available_cell_rate + vcc->qos.txtp.max_pcr < qos->txtp.max_pcr) {
2109 up(&fore200e->rate_sf);
2110 return -EAGAIN;
2113 fore200e->available_cell_rate += vcc->qos.txtp.max_pcr;
2114 fore200e->available_cell_rate -= qos->txtp.max_pcr;
2116 up(&fore200e->rate_sf);
2118 memcpy(&vcc->qos, qos, sizeof(struct atm_qos));
2120 /* update rate control parameters */
2121 fore200e_rate_ctrl(qos, &fore200e_vcc->rate);
2123 set_bit(ATM_VF_HASQOS, &vcc->flags);
2125 return 0;
2128 return -EINVAL;
2132 static int __init
2133 fore200e_irq_request(struct fore200e* fore200e)
2135 if (request_irq(fore200e->irq, fore200e_interrupt, SA_SHIRQ, fore200e->name, fore200e->atm_dev) < 0) {
2137 printk(FORE200E "unable to reserve IRQ %s for device %s\n",
2138 fore200e_irq_itoa(fore200e->irq), fore200e->name);
2139 return -EBUSY;
2142 printk(FORE200E "IRQ %s reserved for device %s\n",
2143 fore200e_irq_itoa(fore200e->irq), fore200e->name);
2145 #ifdef FORE200E_USE_TASKLET
2146 tasklet_init(&fore200e->tx_tasklet, fore200e_tx_tasklet, (unsigned long)fore200e);
2147 tasklet_init(&fore200e->rx_tasklet, fore200e_rx_tasklet, (unsigned long)fore200e);
2148 #endif
2150 fore200e->state = FORE200E_STATE_IRQ;
2151 return 0;
2155 static int __init
2156 fore200e_get_esi(struct fore200e* fore200e)
2158 struct prom_data* prom = fore200e_kmalloc(sizeof(struct prom_data), GFP_KERNEL | GFP_DMA);
2159 int ok, i;
2161 if (!prom)
2162 return -ENOMEM;
2164 ok = fore200e->bus->prom_read(fore200e, prom);
2165 if (ok < 0) {
2166 fore200e_kfree(prom);
2167 return -EBUSY;
2170 printk(FORE200E "device %s, rev. %c, S/N: %d, ESI: %02x:%02x:%02x:%02x:%02x:%02x\n",
2171 fore200e->name,
2172 (prom->hw_revision & 0xFF) + '@', /* probably meaningless with SBA boards */
2173 prom->serial_number & 0xFFFF,
2174 prom->mac_addr[ 2 ], prom->mac_addr[ 3 ], prom->mac_addr[ 4 ],
2175 prom->mac_addr[ 5 ], prom->mac_addr[ 6 ], prom->mac_addr[ 7 ]);
2177 for (i = 0; i < ESI_LEN; i++) {
2178 fore200e->esi[ i ] = fore200e->atm_dev->esi[ i ] = prom->mac_addr[ i + 2 ];
2181 fore200e_kfree(prom);
2183 return 0;
2187 static int __init
2188 fore200e_alloc_rx_buf(struct fore200e* fore200e)
2190 int scheme, magn, nbr, size, i;
2192 struct host_bsq* bsq;
2193 struct buffer* buffer;
2195 for (scheme = 0; scheme < BUFFER_SCHEME_NBR; scheme++) {
2196 for (magn = 0; magn < BUFFER_MAGN_NBR; magn++) {
2198 bsq = &fore200e->host_bsq[ scheme ][ magn ];
2200 nbr = fore200e_rx_buf_nbr[ scheme ][ magn ];
2201 size = fore200e_rx_buf_size[ scheme ][ magn ];
2203 DPRINTK(2, "rx buffers %d / %d are being allocated\n", scheme, magn);
2205 /* allocate the array of receive buffers */
2206 buffer = bsq->buffer = fore200e_kmalloc(nbr * sizeof(struct buffer), GFP_KERNEL);
2208 if (buffer == NULL)
2209 return -ENOMEM;
2211 bsq->freebuf = NULL;
2213 for (i = 0; i < nbr; i++) {
2215 buffer[ i ].scheme = scheme;
2216 buffer[ i ].magn = magn;
2217 #ifdef FORE200E_BSQ_DEBUG
2218 buffer[ i ].index = i;
2219 buffer[ i ].supplied = 0;
2220 #endif
2222 /* allocate the receive buffer body */
2223 if (fore200e_chunk_alloc(fore200e,
2224 &buffer[ i ].data, size, fore200e->bus->buffer_alignment,
2225 DMA_FROM_DEVICE) < 0) {
2227 while (i > 0)
2228 fore200e_chunk_free(fore200e, &buffer[ --i ].data);
2229 fore200e_kfree(buffer);
2231 return -ENOMEM;
2234 /* insert the buffer into the free buffer list */
2235 buffer[ i ].next = bsq->freebuf;
2236 bsq->freebuf = &buffer[ i ];
2238 /* all the buffers are free, initially */
2239 bsq->freebuf_count = nbr;
2241 #ifdef FORE200E_BSQ_DEBUG
2242 bsq_audit(3, bsq, scheme, magn);
2243 #endif
2247 fore200e->state = FORE200E_STATE_ALLOC_BUF;
2248 return 0;
2252 static int __init
2253 fore200e_init_bs_queue(struct fore200e* fore200e)
2255 int scheme, magn, i;
2257 struct host_bsq* bsq;
2258 struct cp_bsq_entry __iomem * cp_entry;
2260 for (scheme = 0; scheme < BUFFER_SCHEME_NBR; scheme++) {
2261 for (magn = 0; magn < BUFFER_MAGN_NBR; magn++) {
2263 DPRINTK(2, "buffer supply queue %d / %d is being initialized\n", scheme, magn);
2265 bsq = &fore200e->host_bsq[ scheme ][ magn ];
2267 /* allocate and align the array of status words */
2268 if (fore200e->bus->dma_chunk_alloc(fore200e,
2269 &bsq->status,
2270 sizeof(enum status),
2271 QUEUE_SIZE_BS,
2272 fore200e->bus->status_alignment) < 0) {
2273 return -ENOMEM;
2276 /* allocate and align the array of receive buffer descriptors */
2277 if (fore200e->bus->dma_chunk_alloc(fore200e,
2278 &bsq->rbd_block,
2279 sizeof(struct rbd_block),
2280 QUEUE_SIZE_BS,
2281 fore200e->bus->descr_alignment) < 0) {
2283 fore200e->bus->dma_chunk_free(fore200e, &bsq->status);
2284 return -ENOMEM;
2287 /* get the base address of the cp resident buffer supply queue entries */
2288 cp_entry = fore200e->virt_base +
2289 fore200e->bus->read(&fore200e->cp_queues->cp_bsq[ scheme ][ magn ]);
2291 /* fill the host resident and cp resident buffer supply queue entries */
2292 for (i = 0; i < QUEUE_SIZE_BS; i++) {
2294 bsq->host_entry[ i ].status =
2295 FORE200E_INDEX(bsq->status.align_addr, enum status, i);
2296 bsq->host_entry[ i ].rbd_block =
2297 FORE200E_INDEX(bsq->rbd_block.align_addr, struct rbd_block, i);
2298 bsq->host_entry[ i ].rbd_block_dma =
2299 FORE200E_DMA_INDEX(bsq->rbd_block.dma_addr, struct rbd_block, i);
2300 bsq->host_entry[ i ].cp_entry = &cp_entry[ i ];
2302 *bsq->host_entry[ i ].status = STATUS_FREE;
2304 fore200e->bus->write(FORE200E_DMA_INDEX(bsq->status.dma_addr, enum status, i),
2305 &cp_entry[ i ].status_haddr);
2310 fore200e->state = FORE200E_STATE_INIT_BSQ;
2311 return 0;
2315 static int __init
2316 fore200e_init_rx_queue(struct fore200e* fore200e)
2318 struct host_rxq* rxq = &fore200e->host_rxq;
2319 struct cp_rxq_entry __iomem * cp_entry;
2320 int i;
2322 DPRINTK(2, "receive queue is being initialized\n");
2324 /* allocate and align the array of status words */
2325 if (fore200e->bus->dma_chunk_alloc(fore200e,
2326 &rxq->status,
2327 sizeof(enum status),
2328 QUEUE_SIZE_RX,
2329 fore200e->bus->status_alignment) < 0) {
2330 return -ENOMEM;
2333 /* allocate and align the array of receive PDU descriptors */
2334 if (fore200e->bus->dma_chunk_alloc(fore200e,
2335 &rxq->rpd,
2336 sizeof(struct rpd),
2337 QUEUE_SIZE_RX,
2338 fore200e->bus->descr_alignment) < 0) {
2340 fore200e->bus->dma_chunk_free(fore200e, &rxq->status);
2341 return -ENOMEM;
2344 /* get the base address of the cp resident rx queue entries */
2345 cp_entry = fore200e->virt_base + fore200e->bus->read(&fore200e->cp_queues->cp_rxq);
2347 /* fill the host resident and cp resident rx entries */
2348 for (i=0; i < QUEUE_SIZE_RX; i++) {
2350 rxq->host_entry[ i ].status =
2351 FORE200E_INDEX(rxq->status.align_addr, enum status, i);
2352 rxq->host_entry[ i ].rpd =
2353 FORE200E_INDEX(rxq->rpd.align_addr, struct rpd, i);
2354 rxq->host_entry[ i ].rpd_dma =
2355 FORE200E_DMA_INDEX(rxq->rpd.dma_addr, struct rpd, i);
2356 rxq->host_entry[ i ].cp_entry = &cp_entry[ i ];
2358 *rxq->host_entry[ i ].status = STATUS_FREE;
2360 fore200e->bus->write(FORE200E_DMA_INDEX(rxq->status.dma_addr, enum status, i),
2361 &cp_entry[ i ].status_haddr);
2363 fore200e->bus->write(FORE200E_DMA_INDEX(rxq->rpd.dma_addr, struct rpd, i),
2364 &cp_entry[ i ].rpd_haddr);
2367 /* set the head entry of the queue */
2368 rxq->head = 0;
2370 fore200e->state = FORE200E_STATE_INIT_RXQ;
2371 return 0;
2375 static int __init
2376 fore200e_init_tx_queue(struct fore200e* fore200e)
2378 struct host_txq* txq = &fore200e->host_txq;
2379 struct cp_txq_entry __iomem * cp_entry;
2380 int i;
2382 DPRINTK(2, "transmit queue is being initialized\n");
2384 /* allocate and align the array of status words */
2385 if (fore200e->bus->dma_chunk_alloc(fore200e,
2386 &txq->status,
2387 sizeof(enum status),
2388 QUEUE_SIZE_TX,
2389 fore200e->bus->status_alignment) < 0) {
2390 return -ENOMEM;
2393 /* allocate and align the array of transmit PDU descriptors */
2394 if (fore200e->bus->dma_chunk_alloc(fore200e,
2395 &txq->tpd,
2396 sizeof(struct tpd),
2397 QUEUE_SIZE_TX,
2398 fore200e->bus->descr_alignment) < 0) {
2400 fore200e->bus->dma_chunk_free(fore200e, &txq->status);
2401 return -ENOMEM;
2404 /* get the base address of the cp resident tx queue entries */
2405 cp_entry = fore200e->virt_base + fore200e->bus->read(&fore200e->cp_queues->cp_txq);
2407 /* fill the host resident and cp resident tx entries */
2408 for (i=0; i < QUEUE_SIZE_TX; i++) {
2410 txq->host_entry[ i ].status =
2411 FORE200E_INDEX(txq->status.align_addr, enum status, i);
2412 txq->host_entry[ i ].tpd =
2413 FORE200E_INDEX(txq->tpd.align_addr, struct tpd, i);
2414 txq->host_entry[ i ].tpd_dma =
2415 FORE200E_DMA_INDEX(txq->tpd.dma_addr, struct tpd, i);
2416 txq->host_entry[ i ].cp_entry = &cp_entry[ i ];
2418 *txq->host_entry[ i ].status = STATUS_FREE;
2420 fore200e->bus->write(FORE200E_DMA_INDEX(txq->status.dma_addr, enum status, i),
2421 &cp_entry[ i ].status_haddr);
2423 /* although there is a one-to-one mapping of tx queue entries and tpds,
2424 we do not write here the DMA (physical) base address of each tpd into
2425 the related cp resident entry, because the cp relies on this write
2426 operation to detect that a new pdu has been submitted for tx */
2429 /* set the head and tail entries of the queue */
2430 txq->head = 0;
2431 txq->tail = 0;
2433 fore200e->state = FORE200E_STATE_INIT_TXQ;
2434 return 0;
2438 static int __init
2439 fore200e_init_cmd_queue(struct fore200e* fore200e)
2441 struct host_cmdq* cmdq = &fore200e->host_cmdq;
2442 struct cp_cmdq_entry __iomem * cp_entry;
2443 int i;
2445 DPRINTK(2, "command queue is being initialized\n");
2447 /* allocate and align the array of status words */
2448 if (fore200e->bus->dma_chunk_alloc(fore200e,
2449 &cmdq->status,
2450 sizeof(enum status),
2451 QUEUE_SIZE_CMD,
2452 fore200e->bus->status_alignment) < 0) {
2453 return -ENOMEM;
2456 /* get the base address of the cp resident cmd queue entries */
2457 cp_entry = fore200e->virt_base + fore200e->bus->read(&fore200e->cp_queues->cp_cmdq);
2459 /* fill the host resident and cp resident cmd entries */
2460 for (i=0; i < QUEUE_SIZE_CMD; i++) {
2462 cmdq->host_entry[ i ].status =
2463 FORE200E_INDEX(cmdq->status.align_addr, enum status, i);
2464 cmdq->host_entry[ i ].cp_entry = &cp_entry[ i ];
2466 *cmdq->host_entry[ i ].status = STATUS_FREE;
2468 fore200e->bus->write(FORE200E_DMA_INDEX(cmdq->status.dma_addr, enum status, i),
2469 &cp_entry[ i ].status_haddr);
2472 /* set the head entry of the queue */
2473 cmdq->head = 0;
2475 fore200e->state = FORE200E_STATE_INIT_CMDQ;
2476 return 0;
2480 static void __init
2481 fore200e_param_bs_queue(struct fore200e* fore200e,
2482 enum buffer_scheme scheme, enum buffer_magn magn,
2483 int queue_length, int pool_size, int supply_blksize)
2485 struct bs_spec __iomem * bs_spec = &fore200e->cp_queues->init.bs_spec[ scheme ][ magn ];
2487 fore200e->bus->write(queue_length, &bs_spec->queue_length);
2488 fore200e->bus->write(fore200e_rx_buf_size[ scheme ][ magn ], &bs_spec->buffer_size);
2489 fore200e->bus->write(pool_size, &bs_spec->pool_size);
2490 fore200e->bus->write(supply_blksize, &bs_spec->supply_blksize);
2494 static int __init
2495 fore200e_initialize(struct fore200e* fore200e)
2497 struct cp_queues __iomem * cpq;
2498 int ok, scheme, magn;
2500 DPRINTK(2, "device %s being initialized\n", fore200e->name);
2502 init_MUTEX(&fore200e->rate_sf);
2503 spin_lock_init(&fore200e->q_lock);
2505 cpq = fore200e->cp_queues = fore200e->virt_base + FORE200E_CP_QUEUES_OFFSET;
2507 /* enable cp to host interrupts */
2508 fore200e->bus->write(1, &cpq->imask);
2510 if (fore200e->bus->irq_enable)
2511 fore200e->bus->irq_enable(fore200e);
2513 fore200e->bus->write(NBR_CONNECT, &cpq->init.num_connect);
2515 fore200e->bus->write(QUEUE_SIZE_CMD, &cpq->init.cmd_queue_len);
2516 fore200e->bus->write(QUEUE_SIZE_RX, &cpq->init.rx_queue_len);
2517 fore200e->bus->write(QUEUE_SIZE_TX, &cpq->init.tx_queue_len);
2519 fore200e->bus->write(RSD_EXTENSION, &cpq->init.rsd_extension);
2520 fore200e->bus->write(TSD_EXTENSION, &cpq->init.tsd_extension);
2522 for (scheme = 0; scheme < BUFFER_SCHEME_NBR; scheme++)
2523 for (magn = 0; magn < BUFFER_MAGN_NBR; magn++)
2524 fore200e_param_bs_queue(fore200e, scheme, magn,
2525 QUEUE_SIZE_BS,
2526 fore200e_rx_buf_nbr[ scheme ][ magn ],
2527 RBD_BLK_SIZE);
2529 /* issue the initialize command */
2530 fore200e->bus->write(STATUS_PENDING, &cpq->init.status);
2531 fore200e->bus->write(OPCODE_INITIALIZE, &cpq->init.opcode);
2533 ok = fore200e_io_poll(fore200e, &cpq->init.status, STATUS_COMPLETE, 3000);
2534 if (ok == 0) {
2535 printk(FORE200E "device %s initialization failed\n", fore200e->name);
2536 return -ENODEV;
2539 printk(FORE200E "device %s initialized\n", fore200e->name);
2541 fore200e->state = FORE200E_STATE_INITIALIZE;
2542 return 0;
2546 static void __init
2547 fore200e_monitor_putc(struct fore200e* fore200e, char c)
2549 struct cp_monitor __iomem * monitor = fore200e->cp_monitor;
2551 #if 0
2552 printk("%c", c);
2553 #endif
2554 fore200e->bus->write(((u32) c) | FORE200E_CP_MONITOR_UART_AVAIL, &monitor->soft_uart.send);
2558 static int __init
2559 fore200e_monitor_getc(struct fore200e* fore200e)
2561 struct cp_monitor __iomem * monitor = fore200e->cp_monitor;
2562 unsigned long timeout = jiffies + msecs_to_jiffies(50);
2563 int c;
2565 while (time_before(jiffies, timeout)) {
2567 c = (int) fore200e->bus->read(&monitor->soft_uart.recv);
2569 if (c & FORE200E_CP_MONITOR_UART_AVAIL) {
2571 fore200e->bus->write(FORE200E_CP_MONITOR_UART_FREE, &monitor->soft_uart.recv);
2572 #if 0
2573 printk("%c", c & 0xFF);
2574 #endif
2575 return c & 0xFF;
2579 return -1;
2583 static void __init
2584 fore200e_monitor_puts(struct fore200e* fore200e, char* str)
2586 while (*str) {
2588 /* the i960 monitor doesn't accept any new character if it has something to say */
2589 while (fore200e_monitor_getc(fore200e) >= 0);
2591 fore200e_monitor_putc(fore200e, *str++);
2594 while (fore200e_monitor_getc(fore200e) >= 0);
2598 static int __init
2599 fore200e_start_fw(struct fore200e* fore200e)
2601 int ok;
2602 char cmd[ 48 ];
2603 struct fw_header* fw_header = (struct fw_header*) fore200e->bus->fw_data;
2605 DPRINTK(2, "device %s firmware being started\n", fore200e->name);
2607 #if defined(__sparc_v9__)
2608 /* reported to be required by SBA cards on some sparc64 hosts */
2609 fore200e_spin(100);
2610 #endif
2612 sprintf(cmd, "\rgo %x\r", le32_to_cpu(fw_header->start_offset));
2614 fore200e_monitor_puts(fore200e, cmd);
2616 ok = fore200e_io_poll(fore200e, &fore200e->cp_monitor->bstat, BSTAT_CP_RUNNING, 1000);
2617 if (ok == 0) {
2618 printk(FORE200E "device %s firmware didn't start\n", fore200e->name);
2619 return -ENODEV;
2622 printk(FORE200E "device %s firmware started\n", fore200e->name);
2624 fore200e->state = FORE200E_STATE_START_FW;
2625 return 0;
2629 static int __init
2630 fore200e_load_fw(struct fore200e* fore200e)
2632 u32* fw_data = (u32*) fore200e->bus->fw_data;
2633 u32 fw_size = (u32) *fore200e->bus->fw_size / sizeof(u32);
2635 struct fw_header* fw_header = (struct fw_header*) fw_data;
2637 u32 __iomem *load_addr = fore200e->virt_base + le32_to_cpu(fw_header->load_offset);
2639 DPRINTK(2, "device %s firmware being loaded at 0x%p (%d words)\n",
2640 fore200e->name, load_addr, fw_size);
2642 if (le32_to_cpu(fw_header->magic) != FW_HEADER_MAGIC) {
2643 printk(FORE200E "corrupted %s firmware image\n", fore200e->bus->model_name);
2644 return -ENODEV;
2647 for (; fw_size--; fw_data++, load_addr++)
2648 fore200e->bus->write(le32_to_cpu(*fw_data), load_addr);
2650 fore200e->state = FORE200E_STATE_LOAD_FW;
2651 return 0;
2655 static int __init
2656 fore200e_register(struct fore200e* fore200e)
2658 struct atm_dev* atm_dev;
2660 DPRINTK(2, "device %s being registered\n", fore200e->name);
2662 atm_dev = atm_dev_register(fore200e->bus->proc_name, &fore200e_ops, -1,
2663 NULL);
2664 if (atm_dev == NULL) {
2665 printk(FORE200E "unable to register device %s\n", fore200e->name);
2666 return -ENODEV;
2669 atm_dev->dev_data = fore200e;
2670 fore200e->atm_dev = atm_dev;
2672 atm_dev->ci_range.vpi_bits = FORE200E_VPI_BITS;
2673 atm_dev->ci_range.vci_bits = FORE200E_VCI_BITS;
2675 fore200e->available_cell_rate = ATM_OC3_PCR;
2677 fore200e->state = FORE200E_STATE_REGISTER;
2678 return 0;
2682 static int __init
2683 fore200e_init(struct fore200e* fore200e)
2685 if (fore200e_register(fore200e) < 0)
2686 return -ENODEV;
2688 if (fore200e->bus->configure(fore200e) < 0)
2689 return -ENODEV;
2691 if (fore200e->bus->map(fore200e) < 0)
2692 return -ENODEV;
2694 if (fore200e_reset(fore200e, 1) < 0)
2695 return -ENODEV;
2697 if (fore200e_load_fw(fore200e) < 0)
2698 return -ENODEV;
2700 if (fore200e_start_fw(fore200e) < 0)
2701 return -ENODEV;
2703 if (fore200e_initialize(fore200e) < 0)
2704 return -ENODEV;
2706 if (fore200e_init_cmd_queue(fore200e) < 0)
2707 return -ENOMEM;
2709 if (fore200e_init_tx_queue(fore200e) < 0)
2710 return -ENOMEM;
2712 if (fore200e_init_rx_queue(fore200e) < 0)
2713 return -ENOMEM;
2715 if (fore200e_init_bs_queue(fore200e) < 0)
2716 return -ENOMEM;
2718 if (fore200e_alloc_rx_buf(fore200e) < 0)
2719 return -ENOMEM;
2721 if (fore200e_get_esi(fore200e) < 0)
2722 return -EIO;
2724 if (fore200e_irq_request(fore200e) < 0)
2725 return -EBUSY;
2727 fore200e_supply(fore200e);
2729 /* all done, board initialization is now complete */
2730 fore200e->state = FORE200E_STATE_COMPLETE;
2731 return 0;
2735 static int __devinit
2736 fore200e_pca_detect(struct pci_dev *pci_dev, const struct pci_device_id *pci_ent)
2738 const struct fore200e_bus* bus = (struct fore200e_bus*) pci_ent->driver_data;
2739 struct fore200e* fore200e;
2740 int err = 0;
2741 static int index = 0;
2743 if (pci_enable_device(pci_dev)) {
2744 err = -EINVAL;
2745 goto out;
2748 fore200e = fore200e_kmalloc(sizeof(struct fore200e), GFP_KERNEL);
2749 if (fore200e == NULL) {
2750 err = -ENOMEM;
2751 goto out_disable;
2754 fore200e->bus = bus;
2755 fore200e->bus_dev = pci_dev;
2756 fore200e->irq = pci_dev->irq;
2757 fore200e->phys_base = pci_resource_start(pci_dev, 0);
2759 sprintf(fore200e->name, "%s-%d", bus->model_name, index - 1);
2761 pci_set_master(pci_dev);
2763 printk(FORE200E "device %s found at 0x%lx, IRQ %s\n",
2764 fore200e->bus->model_name,
2765 fore200e->phys_base, fore200e_irq_itoa(fore200e->irq));
2767 sprintf(fore200e->name, "%s-%d", bus->model_name, index);
2769 err = fore200e_init(fore200e);
2770 if (err < 0) {
2771 fore200e_shutdown(fore200e);
2772 goto out_free;
2775 ++index;
2776 pci_set_drvdata(pci_dev, fore200e);
2778 out:
2779 return err;
2781 out_free:
2782 kfree(fore200e);
2783 out_disable:
2784 pci_disable_device(pci_dev);
2785 goto out;
2789 static void __devexit fore200e_pca_remove_one(struct pci_dev *pci_dev)
2791 struct fore200e *fore200e;
2793 fore200e = pci_get_drvdata(pci_dev);
2795 list_del(&fore200e->entry);
2797 fore200e_shutdown(fore200e);
2798 kfree(fore200e);
2799 pci_disable_device(pci_dev);
2803 #ifdef CONFIG_ATM_FORE200E_PCA
2804 static struct pci_device_id fore200e_pca_tbl[] = {
2805 { PCI_VENDOR_ID_FORE, PCI_DEVICE_ID_FORE_PCA200E, PCI_ANY_ID, PCI_ANY_ID,
2806 0, 0, (unsigned long) &fore200e_bus[0] },
2807 { 0, }
2810 MODULE_DEVICE_TABLE(pci, fore200e_pca_tbl);
2812 static struct pci_driver fore200e_pca_driver = {
2813 .name = "fore_200e",
2814 .probe = fore200e_pca_detect,
2815 .remove = __devexit_p(fore200e_pca_remove_one),
2816 .id_table = fore200e_pca_tbl,
2818 #endif
2821 static int __init
2822 fore200e_module_init(void)
2824 const struct fore200e_bus* bus;
2825 struct fore200e* fore200e;
2826 int index;
2828 printk(FORE200E "FORE Systems 200E-series ATM driver - version " FORE200E_VERSION "\n");
2830 /* for each configured bus interface */
2831 for (bus = fore200e_bus; bus->model_name; bus++) {
2833 /* detect all boards present on that bus */
2834 for (index = 0; bus->detect && (fore200e = bus->detect(bus, index)); index++) {
2836 printk(FORE200E "device %s found at 0x%lx, IRQ %s\n",
2837 fore200e->bus->model_name,
2838 fore200e->phys_base, fore200e_irq_itoa(fore200e->irq));
2840 sprintf(fore200e->name, "%s-%d", bus->model_name, index);
2842 if (fore200e_init(fore200e) < 0) {
2844 fore200e_shutdown(fore200e);
2845 break;
2848 list_add(&fore200e->entry, &fore200e_boards);
2852 #ifdef CONFIG_ATM_FORE200E_PCA
2853 if (!pci_module_init(&fore200e_pca_driver))
2854 return 0;
2855 #endif
2857 if (!list_empty(&fore200e_boards))
2858 return 0;
2860 return -ENODEV;
2864 static void __exit
2865 fore200e_module_cleanup(void)
2867 struct fore200e *fore200e, *next;
2869 #ifdef CONFIG_ATM_FORE200E_PCA
2870 pci_unregister_driver(&fore200e_pca_driver);
2871 #endif
2873 list_for_each_entry_safe(fore200e, next, &fore200e_boards, entry) {
2874 fore200e_shutdown(fore200e);
2875 kfree(fore200e);
2877 DPRINTK(1, "module being removed\n");
2881 static int
2882 fore200e_proc_read(struct atm_dev *dev, loff_t* pos, char* page)
2884 struct fore200e* fore200e = FORE200E_DEV(dev);
2885 struct fore200e_vcc* fore200e_vcc;
2886 struct atm_vcc* vcc;
2887 int i, len, left = *pos;
2888 unsigned long flags;
2890 if (!left--) {
2892 if (fore200e_getstats(fore200e) < 0)
2893 return -EIO;
2895 len = sprintf(page,"\n"
2896 " device:\n"
2897 " internal name:\t\t%s\n", fore200e->name);
2899 /* print bus-specific information */
2900 if (fore200e->bus->proc_read)
2901 len += fore200e->bus->proc_read(fore200e, page + len);
2903 len += sprintf(page + len,
2904 " interrupt line:\t\t%s\n"
2905 " physical base address:\t0x%p\n"
2906 " virtual base address:\t0x%p\n"
2907 " factory address (ESI):\t%02x:%02x:%02x:%02x:%02x:%02x\n"
2908 " board serial number:\t\t%d\n\n",
2909 fore200e_irq_itoa(fore200e->irq),
2910 (void*)fore200e->phys_base,
2911 fore200e->virt_base,
2912 fore200e->esi[0], fore200e->esi[1], fore200e->esi[2],
2913 fore200e->esi[3], fore200e->esi[4], fore200e->esi[5],
2914 fore200e->esi[4] * 256 + fore200e->esi[5]);
2916 return len;
2919 if (!left--)
2920 return sprintf(page,
2921 " free small bufs, scheme 1:\t%d\n"
2922 " free large bufs, scheme 1:\t%d\n"
2923 " free small bufs, scheme 2:\t%d\n"
2924 " free large bufs, scheme 2:\t%d\n",
2925 fore200e->host_bsq[ BUFFER_SCHEME_ONE ][ BUFFER_MAGN_SMALL ].freebuf_count,
2926 fore200e->host_bsq[ BUFFER_SCHEME_ONE ][ BUFFER_MAGN_LARGE ].freebuf_count,
2927 fore200e->host_bsq[ BUFFER_SCHEME_TWO ][ BUFFER_MAGN_SMALL ].freebuf_count,
2928 fore200e->host_bsq[ BUFFER_SCHEME_TWO ][ BUFFER_MAGN_LARGE ].freebuf_count);
2930 if (!left--) {
2931 u32 hb = fore200e->bus->read(&fore200e->cp_queues->heartbeat);
2933 len = sprintf(page,"\n\n"
2934 " cell processor:\n"
2935 " heartbeat state:\t\t");
2937 if (hb >> 16 != 0xDEAD)
2938 len += sprintf(page + len, "0x%08x\n", hb);
2939 else
2940 len += sprintf(page + len, "*** FATAL ERROR %04x ***\n", hb & 0xFFFF);
2942 return len;
2945 if (!left--) {
2946 static const char* media_name[] = {
2947 "unshielded twisted pair",
2948 "multimode optical fiber ST",
2949 "multimode optical fiber SC",
2950 "single-mode optical fiber ST",
2951 "single-mode optical fiber SC",
2952 "unknown"
2955 static const char* oc3_mode[] = {
2956 "normal operation",
2957 "diagnostic loopback",
2958 "line loopback",
2959 "unknown"
2962 u32 fw_release = fore200e->bus->read(&fore200e->cp_queues->fw_release);
2963 u32 mon960_release = fore200e->bus->read(&fore200e->cp_queues->mon960_release);
2964 u32 oc3_revision = fore200e->bus->read(&fore200e->cp_queues->oc3_revision);
2965 u32 media_index = FORE200E_MEDIA_INDEX(fore200e->bus->read(&fore200e->cp_queues->media_type));
2966 u32 oc3_index;
2968 if ((media_index < 0) || (media_index > 4))
2969 media_index = 5;
2971 switch (fore200e->loop_mode) {
2972 case ATM_LM_NONE: oc3_index = 0;
2973 break;
2974 case ATM_LM_LOC_PHY: oc3_index = 1;
2975 break;
2976 case ATM_LM_RMT_PHY: oc3_index = 2;
2977 break;
2978 default: oc3_index = 3;
2981 return sprintf(page,
2982 " firmware release:\t\t%d.%d.%d\n"
2983 " monitor release:\t\t%d.%d\n"
2984 " media type:\t\t\t%s\n"
2985 " OC-3 revision:\t\t0x%x\n"
2986 " OC-3 mode:\t\t\t%s",
2987 fw_release >> 16, fw_release << 16 >> 24, fw_release << 24 >> 24,
2988 mon960_release >> 16, mon960_release << 16 >> 16,
2989 media_name[ media_index ],
2990 oc3_revision,
2991 oc3_mode[ oc3_index ]);
2994 if (!left--) {
2995 struct cp_monitor __iomem * cp_monitor = fore200e->cp_monitor;
2997 return sprintf(page,
2998 "\n\n"
2999 " monitor:\n"
3000 " version number:\t\t%d\n"
3001 " boot status word:\t\t0x%08x\n",
3002 fore200e->bus->read(&cp_monitor->mon_version),
3003 fore200e->bus->read(&cp_monitor->bstat));
3006 if (!left--)
3007 return sprintf(page,
3008 "\n"
3009 " device statistics:\n"
3010 " 4b5b:\n"
3011 " crc_header_errors:\t\t%10u\n"
3012 " framing_errors:\t\t%10u\n",
3013 fore200e_swap(fore200e->stats->phy.crc_header_errors),
3014 fore200e_swap(fore200e->stats->phy.framing_errors));
3016 if (!left--)
3017 return sprintf(page, "\n"
3018 " OC-3:\n"
3019 " section_bip8_errors:\t%10u\n"
3020 " path_bip8_errors:\t\t%10u\n"
3021 " line_bip24_errors:\t\t%10u\n"
3022 " line_febe_errors:\t\t%10u\n"
3023 " path_febe_errors:\t\t%10u\n"
3024 " corr_hcs_errors:\t\t%10u\n"
3025 " ucorr_hcs_errors:\t\t%10u\n",
3026 fore200e_swap(fore200e->stats->oc3.section_bip8_errors),
3027 fore200e_swap(fore200e->stats->oc3.path_bip8_errors),
3028 fore200e_swap(fore200e->stats->oc3.line_bip24_errors),
3029 fore200e_swap(fore200e->stats->oc3.line_febe_errors),
3030 fore200e_swap(fore200e->stats->oc3.path_febe_errors),
3031 fore200e_swap(fore200e->stats->oc3.corr_hcs_errors),
3032 fore200e_swap(fore200e->stats->oc3.ucorr_hcs_errors));
3034 if (!left--)
3035 return sprintf(page,"\n"
3036 " ATM:\t\t\t\t cells\n"
3037 " TX:\t\t\t%10u\n"
3038 " RX:\t\t\t%10u\n"
3039 " vpi out of range:\t\t%10u\n"
3040 " vpi no conn:\t\t%10u\n"
3041 " vci out of range:\t\t%10u\n"
3042 " vci no conn:\t\t%10u\n",
3043 fore200e_swap(fore200e->stats->atm.cells_transmitted),
3044 fore200e_swap(fore200e->stats->atm.cells_received),
3045 fore200e_swap(fore200e->stats->atm.vpi_bad_range),
3046 fore200e_swap(fore200e->stats->atm.vpi_no_conn),
3047 fore200e_swap(fore200e->stats->atm.vci_bad_range),
3048 fore200e_swap(fore200e->stats->atm.vci_no_conn));
3050 if (!left--)
3051 return sprintf(page,"\n"
3052 " AAL0:\t\t\t cells\n"
3053 " TX:\t\t\t%10u\n"
3054 " RX:\t\t\t%10u\n"
3055 " dropped:\t\t\t%10u\n",
3056 fore200e_swap(fore200e->stats->aal0.cells_transmitted),
3057 fore200e_swap(fore200e->stats->aal0.cells_received),
3058 fore200e_swap(fore200e->stats->aal0.cells_dropped));
3060 if (!left--)
3061 return sprintf(page,"\n"
3062 " AAL3/4:\n"
3063 " SAR sublayer:\t\t cells\n"
3064 " TX:\t\t\t%10u\n"
3065 " RX:\t\t\t%10u\n"
3066 " dropped:\t\t\t%10u\n"
3067 " CRC errors:\t\t%10u\n"
3068 " protocol errors:\t\t%10u\n\n"
3069 " CS sublayer:\t\t PDUs\n"
3070 " TX:\t\t\t%10u\n"
3071 " RX:\t\t\t%10u\n"
3072 " dropped:\t\t\t%10u\n"
3073 " protocol errors:\t\t%10u\n",
3074 fore200e_swap(fore200e->stats->aal34.cells_transmitted),
3075 fore200e_swap(fore200e->stats->aal34.cells_received),
3076 fore200e_swap(fore200e->stats->aal34.cells_dropped),
3077 fore200e_swap(fore200e->stats->aal34.cells_crc_errors),
3078 fore200e_swap(fore200e->stats->aal34.cells_protocol_errors),
3079 fore200e_swap(fore200e->stats->aal34.cspdus_transmitted),
3080 fore200e_swap(fore200e->stats->aal34.cspdus_received),
3081 fore200e_swap(fore200e->stats->aal34.cspdus_dropped),
3082 fore200e_swap(fore200e->stats->aal34.cspdus_protocol_errors));
3084 if (!left--)
3085 return sprintf(page,"\n"
3086 " AAL5:\n"
3087 " SAR sublayer:\t\t cells\n"
3088 " TX:\t\t\t%10u\n"
3089 " RX:\t\t\t%10u\n"
3090 " dropped:\t\t\t%10u\n"
3091 " congestions:\t\t%10u\n\n"
3092 " CS sublayer:\t\t PDUs\n"
3093 " TX:\t\t\t%10u\n"
3094 " RX:\t\t\t%10u\n"
3095 " dropped:\t\t\t%10u\n"
3096 " CRC errors:\t\t%10u\n"
3097 " protocol errors:\t\t%10u\n",
3098 fore200e_swap(fore200e->stats->aal5.cells_transmitted),
3099 fore200e_swap(fore200e->stats->aal5.cells_received),
3100 fore200e_swap(fore200e->stats->aal5.cells_dropped),
3101 fore200e_swap(fore200e->stats->aal5.congestion_experienced),
3102 fore200e_swap(fore200e->stats->aal5.cspdus_transmitted),
3103 fore200e_swap(fore200e->stats->aal5.cspdus_received),
3104 fore200e_swap(fore200e->stats->aal5.cspdus_dropped),
3105 fore200e_swap(fore200e->stats->aal5.cspdus_crc_errors),
3106 fore200e_swap(fore200e->stats->aal5.cspdus_protocol_errors));
3108 if (!left--)
3109 return sprintf(page,"\n"
3110 " AUX:\t\t allocation failures\n"
3111 " small b1:\t\t\t%10u\n"
3112 " large b1:\t\t\t%10u\n"
3113 " small b2:\t\t\t%10u\n"
3114 " large b2:\t\t\t%10u\n"
3115 " RX PDUs:\t\t\t%10u\n"
3116 " TX PDUs:\t\t\t%10lu\n",
3117 fore200e_swap(fore200e->stats->aux.small_b1_failed),
3118 fore200e_swap(fore200e->stats->aux.large_b1_failed),
3119 fore200e_swap(fore200e->stats->aux.small_b2_failed),
3120 fore200e_swap(fore200e->stats->aux.large_b2_failed),
3121 fore200e_swap(fore200e->stats->aux.rpd_alloc_failed),
3122 fore200e->tx_sat);
3124 if (!left--)
3125 return sprintf(page,"\n"
3126 " receive carrier:\t\t\t%s\n",
3127 fore200e->stats->aux.receive_carrier ? "ON" : "OFF!");
3129 if (!left--) {
3130 return sprintf(page,"\n"
3131 " VCCs:\n address VPI VCI AAL "
3132 "TX PDUs TX min/max size RX PDUs RX min/max size\n");
3135 for (i = 0; i < NBR_CONNECT; i++) {
3137 vcc = fore200e->vc_map[i].vcc;
3139 if (vcc == NULL)
3140 continue;
3142 spin_lock_irqsave(&fore200e->q_lock, flags);
3144 if (vcc && test_bit(ATM_VF_READY, &vcc->flags) && !left--) {
3146 fore200e_vcc = FORE200E_VCC(vcc);
3147 ASSERT(fore200e_vcc);
3149 len = sprintf(page,
3150 " %08x %03d %05d %1d %09lu %05d/%05d %09lu %05d/%05d\n",
3151 (u32)(unsigned long)vcc,
3152 vcc->vpi, vcc->vci, fore200e_atm2fore_aal(vcc->qos.aal),
3153 fore200e_vcc->tx_pdu,
3154 fore200e_vcc->tx_min_pdu > 0xFFFF ? 0 : fore200e_vcc->tx_min_pdu,
3155 fore200e_vcc->tx_max_pdu,
3156 fore200e_vcc->rx_pdu,
3157 fore200e_vcc->rx_min_pdu > 0xFFFF ? 0 : fore200e_vcc->rx_min_pdu,
3158 fore200e_vcc->rx_max_pdu);
3160 spin_unlock_irqrestore(&fore200e->q_lock, flags);
3161 return len;
3164 spin_unlock_irqrestore(&fore200e->q_lock, flags);
3167 return 0;
3170 module_init(fore200e_module_init);
3171 module_exit(fore200e_module_cleanup);
3174 static const struct atmdev_ops fore200e_ops =
3176 .open = fore200e_open,
3177 .close = fore200e_close,
3178 .ioctl = fore200e_ioctl,
3179 .getsockopt = fore200e_getsockopt,
3180 .setsockopt = fore200e_setsockopt,
3181 .send = fore200e_send,
3182 .change_qos = fore200e_change_qos,
3183 .proc_read = fore200e_proc_read,
3184 .owner = THIS_MODULE
3188 #ifdef CONFIG_ATM_FORE200E_PCA
3189 extern const unsigned char _fore200e_pca_fw_data[];
3190 extern const unsigned int _fore200e_pca_fw_size;
3191 #endif
3192 #ifdef CONFIG_ATM_FORE200E_SBA
3193 extern const unsigned char _fore200e_sba_fw_data[];
3194 extern const unsigned int _fore200e_sba_fw_size;
3195 #endif
3197 static const struct fore200e_bus fore200e_bus[] = {
3198 #ifdef CONFIG_ATM_FORE200E_PCA
3199 { "PCA-200E", "pca200e", 32, 4, 32,
3200 _fore200e_pca_fw_data, &_fore200e_pca_fw_size,
3201 fore200e_pca_read,
3202 fore200e_pca_write,
3203 fore200e_pca_dma_map,
3204 fore200e_pca_dma_unmap,
3205 fore200e_pca_dma_sync_for_cpu,
3206 fore200e_pca_dma_sync_for_device,
3207 fore200e_pca_dma_chunk_alloc,
3208 fore200e_pca_dma_chunk_free,
3209 NULL,
3210 fore200e_pca_configure,
3211 fore200e_pca_map,
3212 fore200e_pca_reset,
3213 fore200e_pca_prom_read,
3214 fore200e_pca_unmap,
3215 NULL,
3216 fore200e_pca_irq_check,
3217 fore200e_pca_irq_ack,
3218 fore200e_pca_proc_read,
3220 #endif
3221 #ifdef CONFIG_ATM_FORE200E_SBA
3222 { "SBA-200E", "sba200e", 32, 64, 32,
3223 _fore200e_sba_fw_data, &_fore200e_sba_fw_size,
3224 fore200e_sba_read,
3225 fore200e_sba_write,
3226 fore200e_sba_dma_map,
3227 fore200e_sba_dma_unmap,
3228 fore200e_sba_dma_sync_for_cpu,
3229 fore200e_sba_dma_sync_for_device,
3230 fore200e_sba_dma_chunk_alloc,
3231 fore200e_sba_dma_chunk_free,
3232 fore200e_sba_detect,
3233 fore200e_sba_configure,
3234 fore200e_sba_map,
3235 fore200e_sba_reset,
3236 fore200e_sba_prom_read,
3237 fore200e_sba_unmap,
3238 fore200e_sba_irq_enable,
3239 fore200e_sba_irq_check,
3240 fore200e_sba_irq_ack,
3241 fore200e_sba_proc_read,
3243 #endif
3247 #ifdef MODULE_LICENSE
3248 MODULE_LICENSE("GPL");
3249 #endif