xref: /openbmc/linux/drivers/atm/eni.c (revision a61bbcf2)
11da177e4SLinus Torvalds /* drivers/atm/eni.c - Efficient Networks ENI155P device driver */
21da177e4SLinus Torvalds 
31da177e4SLinus Torvalds /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/module.h>
71da177e4SLinus Torvalds #include <linux/config.h>
81da177e4SLinus Torvalds #include <linux/kernel.h>
91da177e4SLinus Torvalds #include <linux/mm.h>
101da177e4SLinus Torvalds #include <linux/pci.h>
111da177e4SLinus Torvalds #include <linux/errno.h>
121da177e4SLinus Torvalds #include <linux/atm.h>
131da177e4SLinus Torvalds #include <linux/atmdev.h>
141da177e4SLinus Torvalds #include <linux/sonet.h>
151da177e4SLinus Torvalds #include <linux/skbuff.h>
161da177e4SLinus Torvalds #include <linux/time.h>
171da177e4SLinus Torvalds #include <linux/delay.h>
181da177e4SLinus Torvalds #include <linux/uio.h>
191da177e4SLinus Torvalds #include <linux/init.h>
201da177e4SLinus Torvalds #include <linux/atm_eni.h>
211da177e4SLinus Torvalds #include <linux/bitops.h>
221da177e4SLinus Torvalds #include <asm/system.h>
231da177e4SLinus Torvalds #include <asm/io.h>
241da177e4SLinus Torvalds #include <asm/atomic.h>
251da177e4SLinus Torvalds #include <asm/uaccess.h>
261da177e4SLinus Torvalds #include <asm/string.h>
271da177e4SLinus Torvalds #include <asm/byteorder.h>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include "tonga.h"
301da177e4SLinus Torvalds #include "midway.h"
311da177e4SLinus Torvalds #include "suni.h"
321da177e4SLinus Torvalds #include "eni.h"
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #if !defined(__i386__) && !defined(__x86_64__)
351da177e4SLinus Torvalds #ifndef ioremap_nocache
361da177e4SLinus Torvalds #define ioremap_nocache(X,Y) ioremap(X,Y)
371da177e4SLinus Torvalds #endif
381da177e4SLinus Torvalds #endif
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds /*
411da177e4SLinus Torvalds  * TODO:
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  * Show stoppers
441da177e4SLinus Torvalds  *  none
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * Minor
471da177e4SLinus Torvalds  *  - OAM support
481da177e4SLinus Torvalds  *  - fix bugs listed below
491da177e4SLinus Torvalds  */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds /*
521da177e4SLinus Torvalds  * KNOWN BUGS:
531da177e4SLinus Torvalds  *
541da177e4SLinus Torvalds  * - may run into JK-JK bug and deadlock
551da177e4SLinus Torvalds  * - should allocate UBR channel first
561da177e4SLinus Torvalds  * - buffer space allocation algorithm is stupid
571da177e4SLinus Torvalds  *   (RX: should be maxSDU+maxdelay*rate
581da177e4SLinus Torvalds  *    TX: should be maxSDU+min(maxSDU,maxdelay*rate) )
591da177e4SLinus Torvalds  * - doesn't support OAM cells
601da177e4SLinus Torvalds  * - eni_put_free may hang if not putting memory fragments that _complete_
611da177e4SLinus Torvalds  *   2^n block (never happens in real life, though)
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #if 0
661da177e4SLinus Torvalds #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
671da177e4SLinus Torvalds #else
681da177e4SLinus Torvalds #define DPRINTK(format,args...)
691da177e4SLinus Torvalds #endif
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #ifndef CONFIG_ATM_ENI_TUNE_BURST
731da177e4SLinus Torvalds #define CONFIG_ATM_ENI_BURST_TX_8W
741da177e4SLinus Torvalds #define CONFIG_ATM_ENI_BURST_RX_4W
751da177e4SLinus Torvalds #endif
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #ifndef CONFIG_ATM_ENI_DEBUG
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds #define NULLCHECK(x)
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds #define EVENT(s,a,b)
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static void event_dump(void)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #else
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /*
951da177e4SLinus Torvalds  * NULL pointer checking
961da177e4SLinus Torvalds  */
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds #define NULLCHECK(x) \
991da177e4SLinus Torvalds 	if ((unsigned long) (x) < 0x30) \
1001da177e4SLinus Torvalds 		printk(KERN_CRIT #x "==0x%lx\n",(unsigned long) (x))
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /*
1031da177e4SLinus Torvalds  * Very extensive activity logging. Greatly improves bug detection speed but
1041da177e4SLinus Torvalds  * costs a few Mbps if enabled.
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds #define EV 64
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds static const char *ev[EV];
1101da177e4SLinus Torvalds static unsigned long ev_a[EV],ev_b[EV];
1111da177e4SLinus Torvalds static int ec = 0;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds static void EVENT(const char *s,unsigned long a,unsigned long b)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	ev[ec] = s;
1171da177e4SLinus Torvalds 	ev_a[ec] = a;
1181da177e4SLinus Torvalds 	ev_b[ec] = b;
1191da177e4SLinus Torvalds 	ec = (ec+1) % EV;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds static void event_dump(void)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	int n,i;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	for (n = 0; n < EV; n++) {
1281da177e4SLinus Torvalds 		i = (ec+n) % EV;
1291da177e4SLinus Torvalds 		printk(KERN_NOTICE);
1301da177e4SLinus Torvalds 		printk(ev[i] ? ev[i] : "(null)",ev_a[i],ev_b[i]);
1311da177e4SLinus Torvalds 	}
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds #endif /* CONFIG_ATM_ENI_DEBUG */
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /*
1391da177e4SLinus Torvalds  * NExx   must not be equal at end
1401da177e4SLinus Torvalds  * EExx   may be equal at end
1411da177e4SLinus Torvalds  * xxPJOK verify validity of pointer jumps
1421da177e4SLinus Torvalds  * xxPMOK operating on a circular buffer of "c" words
1431da177e4SLinus Torvalds  */
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds #define NEPJOK(a0,a1,b) \
1461da177e4SLinus Torvalds     ((a0) < (a1) ? (b) <= (a0) || (b) > (a1) : (b) <= (a0) && (b) > (a1))
1471da177e4SLinus Torvalds #define EEPJOK(a0,a1,b) \
1481da177e4SLinus Torvalds     ((a0) < (a1) ? (b) < (a0) || (b) >= (a1) : (b) < (a0) && (b) >= (a1))
1491da177e4SLinus Torvalds #define NEPMOK(a0,d,b,c) NEPJOK(a0,(a0+d) & (c-1),b)
1501da177e4SLinus Torvalds #define EEPMOK(a0,d,b,c) EEPJOK(a0,(a0+d) & (c-1),b)
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds static int tx_complete = 0,dma_complete = 0,queued = 0,requeued = 0,
1541da177e4SLinus Torvalds   backlogged = 0,rx_enqueued = 0,rx_dequeued = 0,pushed = 0,submitted = 0,
1551da177e4SLinus Torvalds   putting = 0;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds static struct atm_dev *eni_boards = NULL;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds static u32 *cpu_zeroes = NULL; /* aligned "magic" zeroes */
1601da177e4SLinus Torvalds static dma_addr_t zeroes;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds /* Read/write registers on card */
1631da177e4SLinus Torvalds #define eni_in(r)	readl(eni_dev->reg+(r)*4)
1641da177e4SLinus Torvalds #define eni_out(v,r)	writel((v),eni_dev->reg+(r)*4)
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /*-------------------------------- utilities --------------------------------*/
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds static void dump_mem(struct eni_dev *eni_dev)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	int i;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	for (i = 0; i < eni_dev->free_len; i++)
1751da177e4SLinus Torvalds 		printk(KERN_DEBUG "  %d: %p %d\n",i,
1761da177e4SLinus Torvalds 		    eni_dev->free_list[i].start,
1771da177e4SLinus Torvalds 		    1 << eni_dev->free_list[i].order);
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds static void dump(struct atm_dev *dev)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	int i;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
1881da177e4SLinus Torvalds 	printk(KERN_NOTICE "Free memory\n");
1891da177e4SLinus Torvalds 	dump_mem(eni_dev);
1901da177e4SLinus Torvalds 	printk(KERN_NOTICE "TX buffers\n");
1911da177e4SLinus Torvalds 	for (i = 0; i < NR_CHAN; i++)
1921da177e4SLinus Torvalds 		if (eni_dev->tx[i].send)
1931da177e4SLinus Torvalds 			printk(KERN_NOTICE "  TX %d @ %p: %ld\n",i,
1941da177e4SLinus Torvalds 			    eni_dev->tx[i].send,eni_dev->tx[i].words*4);
1951da177e4SLinus Torvalds 	printk(KERN_NOTICE "RX buffers\n");
1961da177e4SLinus Torvalds 	for (i = 0; i < 1024; i++)
1971da177e4SLinus Torvalds 		if (eni_dev->rx_map[i] && ENI_VCC(eni_dev->rx_map[i])->rx)
1981da177e4SLinus Torvalds 			printk(KERN_NOTICE "  RX %d @ %p: %ld\n",i,
1991da177e4SLinus Torvalds 			    ENI_VCC(eni_dev->rx_map[i])->recv,
2001da177e4SLinus Torvalds 			    ENI_VCC(eni_dev->rx_map[i])->words*4);
2011da177e4SLinus Torvalds 	printk(KERN_NOTICE "----\n");
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds static void eni_put_free(struct eni_dev *eni_dev, void __iomem *start,
2061da177e4SLinus Torvalds     unsigned long size)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	struct eni_free *list;
2091da177e4SLinus Torvalds 	int len,order;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	DPRINTK("init 0x%lx+%ld(0x%lx)\n",start,size,size);
2121da177e4SLinus Torvalds 	start += eni_dev->base_diff;
2131da177e4SLinus Torvalds 	list = eni_dev->free_list;
2141da177e4SLinus Torvalds 	len = eni_dev->free_len;
2151da177e4SLinus Torvalds 	while (size) {
2161da177e4SLinus Torvalds 		if (len >= eni_dev->free_list_size) {
2171da177e4SLinus Torvalds 			printk(KERN_CRIT "eni_put_free overflow (%p,%ld)\n",
2181da177e4SLinus Torvalds 			    start,size);
2191da177e4SLinus Torvalds 			break;
2201da177e4SLinus Torvalds 		}
2211da177e4SLinus Torvalds 		for (order = 0; !(((unsigned long)start | size) & (1 << order)); order++);
2221da177e4SLinus Torvalds 		if (MID_MIN_BUF_SIZE > (1 << order)) {
2231da177e4SLinus Torvalds 			printk(KERN_CRIT "eni_put_free: order %d too small\n",
2241da177e4SLinus Torvalds 			    order);
2251da177e4SLinus Torvalds 			break;
2261da177e4SLinus Torvalds 		}
2271da177e4SLinus Torvalds 		list[len].start = (void __iomem *) start;
2281da177e4SLinus Torvalds 		list[len].order = order;
2291da177e4SLinus Torvalds 		len++;
2301da177e4SLinus Torvalds 		start += 1 << order;
2311da177e4SLinus Torvalds 		size -= 1 << order;
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 	eni_dev->free_len = len;
2341da177e4SLinus Torvalds 	/*dump_mem(eni_dev);*/
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds static void __iomem *eni_alloc_mem(struct eni_dev *eni_dev, unsigned long *size)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	struct eni_free *list;
2411da177e4SLinus Torvalds 	void __iomem *start;
2421da177e4SLinus Torvalds 	int len,i,order,best_order,index;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	list = eni_dev->free_list;
2451da177e4SLinus Torvalds 	len = eni_dev->free_len;
2461da177e4SLinus Torvalds 	if (*size < MID_MIN_BUF_SIZE) *size = MID_MIN_BUF_SIZE;
2471da177e4SLinus Torvalds 	if (*size > MID_MAX_BUF_SIZE) return NULL;
2481da177e4SLinus Torvalds 	for (order = 0; (1 << order) < *size; order++);
2491da177e4SLinus Torvalds 	DPRINTK("trying: %ld->%d\n",*size,order);
2501da177e4SLinus Torvalds 	best_order = 65; /* we don't have more than 2^64 of anything ... */
2511da177e4SLinus Torvalds 	index = 0; /* silence GCC */
2521da177e4SLinus Torvalds 	for (i = 0; i < len; i++)
2531da177e4SLinus Torvalds 		if (list[i].order == order) {
2541da177e4SLinus Torvalds 			best_order = order;
2551da177e4SLinus Torvalds 			index = i;
2561da177e4SLinus Torvalds 			break;
2571da177e4SLinus Torvalds 		}
2581da177e4SLinus Torvalds 		else if (best_order > list[i].order && list[i].order > order) {
2591da177e4SLinus Torvalds 				best_order = list[i].order;
2601da177e4SLinus Torvalds 				index = i;
2611da177e4SLinus Torvalds 			}
2621da177e4SLinus Torvalds 	if (best_order == 65) return NULL;
2631da177e4SLinus Torvalds 	start = list[index].start-eni_dev->base_diff;
2641da177e4SLinus Torvalds 	list[index] = list[--len];
2651da177e4SLinus Torvalds 	eni_dev->free_len = len;
2661da177e4SLinus Torvalds 	*size = 1 << order;
2671da177e4SLinus Torvalds 	eni_put_free(eni_dev,start+*size,(1 << best_order)-*size);
2681da177e4SLinus Torvalds 	DPRINTK("%ld bytes (order %d) at 0x%lx\n",*size,order,start);
2691da177e4SLinus Torvalds 	memset_io(start,0,*size);       /* never leak data */
2701da177e4SLinus Torvalds 	/*dump_mem(eni_dev);*/
2711da177e4SLinus Torvalds 	return start;
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds static void eni_free_mem(struct eni_dev *eni_dev, void __iomem *start,
2761da177e4SLinus Torvalds     unsigned long size)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	struct eni_free *list;
2791da177e4SLinus Torvalds 	int len,i,order;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	start += eni_dev->base_diff;
2821da177e4SLinus Torvalds 	list = eni_dev->free_list;
2831da177e4SLinus Torvalds 	len = eni_dev->free_len;
2841da177e4SLinus Torvalds 	for (order = -1; size; order++) size >>= 1;
2851da177e4SLinus Torvalds 	DPRINTK("eni_free_mem: %p+0x%lx (order %d)\n",start,size,order);
2861da177e4SLinus Torvalds 	for (i = 0; i < len; i++)
2871da177e4SLinus Torvalds 		if (((unsigned long) list[i].start) == ((unsigned long)start^(1 << order)) &&
2881da177e4SLinus Torvalds 		    list[i].order == order) {
2891da177e4SLinus Torvalds 			DPRINTK("match[%d]: 0x%lx/0x%lx(0x%x), %d/%d\n",i,
2901da177e4SLinus Torvalds 			    list[i].start,start,1 << order,list[i].order,order);
2911da177e4SLinus Torvalds 			list[i] = list[--len];
2921da177e4SLinus Torvalds 			start = (void __iomem *) ((unsigned long) start & ~(unsigned long) (1 << order));
2931da177e4SLinus Torvalds 			order++;
2941da177e4SLinus Torvalds 			i = -1;
2951da177e4SLinus Torvalds 			continue;
2961da177e4SLinus Torvalds 		}
2971da177e4SLinus Torvalds 	if (len >= eni_dev->free_list_size) {
2981da177e4SLinus Torvalds 		printk(KERN_ALERT "eni_free_mem overflow (%p,%d)\n",start,
2991da177e4SLinus Torvalds 		    order);
3001da177e4SLinus Torvalds 		return;
3011da177e4SLinus Torvalds 	}
3021da177e4SLinus Torvalds 	list[len].start = start;
3031da177e4SLinus Torvalds 	list[len].order = order;
3041da177e4SLinus Torvalds 	eni_dev->free_len = len+1;
3051da177e4SLinus Torvalds 	/*dump_mem(eni_dev);*/
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds /*----------------------------------- RX ------------------------------------*/
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds #define ENI_VCC_NOS ((struct atm_vcc *) 1)
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds static void rx_ident_err(struct atm_vcc *vcc)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	struct atm_dev *dev;
3181da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
3191da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	dev = vcc->dev;
3221da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
3231da177e4SLinus Torvalds 	/* immediately halt adapter */
3241da177e4SLinus Torvalds 	eni_out(eni_in(MID_MC_S) &
3251da177e4SLinus Torvalds 	    ~(MID_DMA_ENABLE | MID_TX_ENABLE | MID_RX_ENABLE),MID_MC_S);
3261da177e4SLinus Torvalds 	/* dump useful information */
3271da177e4SLinus Torvalds 	eni_vcc = ENI_VCC(vcc);
3281da177e4SLinus Torvalds 	printk(KERN_ALERT DEV_LABEL "(itf %d): driver error - RX ident "
3291da177e4SLinus Torvalds 	    "mismatch\n",dev->number);
3301da177e4SLinus Torvalds 	printk(KERN_ALERT "  VCI %d, rxing %d, words %ld\n",vcc->vci,
3311da177e4SLinus Torvalds 	    eni_vcc->rxing,eni_vcc->words);
3321da177e4SLinus Torvalds 	printk(KERN_ALERT "  host descr 0x%lx, rx pos 0x%lx, descr value "
3331da177e4SLinus Torvalds 	    "0x%x\n",eni_vcc->descr,eni_vcc->rx_pos,
3341da177e4SLinus Torvalds 	    (unsigned) readl(eni_vcc->recv+eni_vcc->descr*4));
3351da177e4SLinus Torvalds 	printk(KERN_ALERT "  last %p, servicing %d\n",eni_vcc->last,
3361da177e4SLinus Torvalds 	    eni_vcc->servicing);
3371da177e4SLinus Torvalds 	EVENT("---dump ends here---\n",0,0);
3381da177e4SLinus Torvalds 	printk(KERN_NOTICE "---recent events---\n");
3391da177e4SLinus Torvalds 	event_dump();
3401da177e4SLinus Torvalds 	ENI_DEV(dev)->fast = NULL; /* really stop it */
3411da177e4SLinus Torvalds 	ENI_DEV(dev)->slow = NULL;
3421da177e4SLinus Torvalds 	skb_queue_head_init(&ENI_DEV(dev)->rx_queue);
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds static int do_rx_dma(struct atm_vcc *vcc,struct sk_buff *skb,
3471da177e4SLinus Torvalds     unsigned long skip,unsigned long size,unsigned long eff)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
3501da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
3511da177e4SLinus Torvalds 	u32 dma_rd,dma_wr;
3521da177e4SLinus Torvalds 	u32 dma[RX_DMA_BUF*2];
3531da177e4SLinus Torvalds 	dma_addr_t paddr;
3541da177e4SLinus Torvalds 	unsigned long here;
3551da177e4SLinus Torvalds 	int i,j;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	eni_dev = ENI_DEV(vcc->dev);
3581da177e4SLinus Torvalds 	eni_vcc = ENI_VCC(vcc);
3591da177e4SLinus Torvalds 	paddr = 0; /* GCC, shut up */
3601da177e4SLinus Torvalds 	if (skb) {
3611da177e4SLinus Torvalds 		paddr = pci_map_single(eni_dev->pci_dev,skb->data,skb->len,
3621da177e4SLinus Torvalds 		    PCI_DMA_FROMDEVICE);
3631da177e4SLinus Torvalds 		ENI_PRV_PADDR(skb) = paddr;
3641da177e4SLinus Torvalds 		if (paddr & 3)
3651da177e4SLinus Torvalds 			printk(KERN_CRIT DEV_LABEL "(itf %d): VCI %d has "
3661da177e4SLinus Torvalds 			    "mis-aligned RX data (0x%lx)\n",vcc->dev->number,
3671da177e4SLinus Torvalds 			    vcc->vci,(unsigned long) paddr);
3681da177e4SLinus Torvalds 		ENI_PRV_SIZE(skb) = size+skip;
3691da177e4SLinus Torvalds 		    /* PDU plus descriptor */
3701da177e4SLinus Torvalds 		ATM_SKB(skb)->vcc = vcc;
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds 	j = 0;
3731da177e4SLinus Torvalds 	if ((eff && skip) || 1) { /* @@@ actually, skip is always == 1 ... */
3741da177e4SLinus Torvalds 		here = (eni_vcc->descr+skip) & (eni_vcc->words-1);
3751da177e4SLinus Torvalds 		dma[j++] = (here << MID_DMA_COUNT_SHIFT) | (vcc->vci
3761da177e4SLinus Torvalds 		    << MID_DMA_VCI_SHIFT) | MID_DT_JK;
3771da177e4SLinus Torvalds 		j++;
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 	here = (eni_vcc->descr+size+skip) & (eni_vcc->words-1);
3801da177e4SLinus Torvalds 	if (!eff) size += skip;
3811da177e4SLinus Torvalds 	else {
3821da177e4SLinus Torvalds 		unsigned long words;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 		if (!size) {
3851da177e4SLinus Torvalds 			DPRINTK("strange things happen ...\n");
3861da177e4SLinus Torvalds 			EVENT("strange things happen ... (skip=%ld,eff=%ld)\n",
3871da177e4SLinus Torvalds 			    size,eff);
3881da177e4SLinus Torvalds 		}
3891da177e4SLinus Torvalds 		words = eff;
3901da177e4SLinus Torvalds 		if (paddr & 15) {
3911da177e4SLinus Torvalds 			unsigned long init;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 			init = 4-((paddr & 15) >> 2);
3941da177e4SLinus Torvalds 			if (init > words) init = words;
3951da177e4SLinus Torvalds 			dma[j++] = MID_DT_WORD | (init << MID_DMA_COUNT_SHIFT) |
3961da177e4SLinus Torvalds 			    (vcc->vci << MID_DMA_VCI_SHIFT);
3971da177e4SLinus Torvalds 			dma[j++] = paddr;
3981da177e4SLinus Torvalds 			paddr += init << 2;
3991da177e4SLinus Torvalds 			words -= init;
4001da177e4SLinus Torvalds 		}
4011da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_RX_16W /* may work with some PCI chipsets ... */
4021da177e4SLinus Torvalds 		if (words & ~15) {
4031da177e4SLinus Torvalds 			dma[j++] = MID_DT_16W | ((words >> 4) <<
4041da177e4SLinus Torvalds 			    MID_DMA_COUNT_SHIFT) | (vcc->vci <<
4051da177e4SLinus Torvalds 			    MID_DMA_VCI_SHIFT);
4061da177e4SLinus Torvalds 			dma[j++] = paddr;
4071da177e4SLinus Torvalds 			paddr += (words & ~15) << 2;
4081da177e4SLinus Torvalds 			words &= 15;
4091da177e4SLinus Torvalds 		}
4101da177e4SLinus Torvalds #endif
4111da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_RX_8W  /* works only with *some* PCI chipsets ... */
4121da177e4SLinus Torvalds 		if (words & ~7) {
4131da177e4SLinus Torvalds 			dma[j++] = MID_DT_8W | ((words >> 3) <<
4141da177e4SLinus Torvalds 			    MID_DMA_COUNT_SHIFT) | (vcc->vci <<
4151da177e4SLinus Torvalds 			    MID_DMA_VCI_SHIFT);
4161da177e4SLinus Torvalds 			dma[j++] = paddr;
4171da177e4SLinus Torvalds 			paddr += (words & ~7) << 2;
4181da177e4SLinus Torvalds 			words &= 7;
4191da177e4SLinus Torvalds 		}
4201da177e4SLinus Torvalds #endif
4211da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_RX_4W /* recommended */
4221da177e4SLinus Torvalds 		if (words & ~3) {
4231da177e4SLinus Torvalds 			dma[j++] = MID_DT_4W | ((words >> 2) <<
4241da177e4SLinus Torvalds 			    MID_DMA_COUNT_SHIFT) | (vcc->vci <<
4251da177e4SLinus Torvalds 			    MID_DMA_VCI_SHIFT);
4261da177e4SLinus Torvalds 			dma[j++] = paddr;
4271da177e4SLinus Torvalds 			paddr += (words & ~3) << 2;
4281da177e4SLinus Torvalds 			words &= 3;
4291da177e4SLinus Torvalds 		}
4301da177e4SLinus Torvalds #endif
4311da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_RX_2W /* probably useless if RX_4W, RX_8W, ... */
4321da177e4SLinus Torvalds 		if (words & ~1) {
4331da177e4SLinus Torvalds 			dma[j++] = MID_DT_2W | ((words >> 1) <<
4341da177e4SLinus Torvalds 			    MID_DMA_COUNT_SHIFT) | (vcc->vci <<
4351da177e4SLinus Torvalds 			    MID_DMA_VCI_SHIFT);
4361da177e4SLinus Torvalds 			dma[j++] = paddr;
4371da177e4SLinus Torvalds 			paddr += (words & ~1) << 2;
4381da177e4SLinus Torvalds 			words &= 1;
4391da177e4SLinus Torvalds 		}
4401da177e4SLinus Torvalds #endif
4411da177e4SLinus Torvalds 		if (words) {
4421da177e4SLinus Torvalds 			dma[j++] = MID_DT_WORD | (words << MID_DMA_COUNT_SHIFT)
4431da177e4SLinus Torvalds 			    | (vcc->vci << MID_DMA_VCI_SHIFT);
4441da177e4SLinus Torvalds 			dma[j++] = paddr;
4451da177e4SLinus Torvalds 		}
4461da177e4SLinus Torvalds 	}
4471da177e4SLinus Torvalds 	if (size != eff) {
4481da177e4SLinus Torvalds 		dma[j++] = (here << MID_DMA_COUNT_SHIFT) |
4491da177e4SLinus Torvalds 		    (vcc->vci << MID_DMA_VCI_SHIFT) | MID_DT_JK;
4501da177e4SLinus Torvalds 		j++;
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 	if (!j || j > 2*RX_DMA_BUF) {
4531da177e4SLinus Torvalds 		printk(KERN_CRIT DEV_LABEL "!j or j too big!!!\n");
4541da177e4SLinus Torvalds 		goto trouble;
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds 	dma[j-2] |= MID_DMA_END;
4571da177e4SLinus Torvalds 	j = j >> 1;
4581da177e4SLinus Torvalds 	dma_wr = eni_in(MID_DMA_WR_RX);
4591da177e4SLinus Torvalds 	dma_rd = eni_in(MID_DMA_RD_RX);
4601da177e4SLinus Torvalds 	/*
4611da177e4SLinus Torvalds 	 * Can I move the dma_wr pointer by 2j+1 positions without overwriting
4621da177e4SLinus Torvalds 	 * data that hasn't been read (position of dma_rd) yet ?
4631da177e4SLinus Torvalds 	 */
4641da177e4SLinus Torvalds 	if (!NEPMOK(dma_wr,j+j+1,dma_rd,NR_DMA_RX)) { /* @@@ +1 is ugly */
4651da177e4SLinus Torvalds 		printk(KERN_WARNING DEV_LABEL "(itf %d): RX DMA full\n",
4661da177e4SLinus Torvalds 		    vcc->dev->number);
4671da177e4SLinus Torvalds 		goto trouble;
4681da177e4SLinus Torvalds 	}
4691da177e4SLinus Torvalds         for (i = 0; i < j; i++) {
4701da177e4SLinus Torvalds 		writel(dma[i*2],eni_dev->rx_dma+dma_wr*8);
4711da177e4SLinus Torvalds 		writel(dma[i*2+1],eni_dev->rx_dma+dma_wr*8+4);
4721da177e4SLinus Torvalds 		dma_wr = (dma_wr+1) & (NR_DMA_RX-1);
4731da177e4SLinus Torvalds         }
4741da177e4SLinus Torvalds 	if (skb) {
4751da177e4SLinus Torvalds 		ENI_PRV_POS(skb) = eni_vcc->descr+size+1;
4761da177e4SLinus Torvalds 		skb_queue_tail(&eni_dev->rx_queue,skb);
4771da177e4SLinus Torvalds 		eni_vcc->last = skb;
4781da177e4SLinus Torvalds rx_enqueued++;
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds 	eni_vcc->descr = here;
4811da177e4SLinus Torvalds 	eni_out(dma_wr,MID_DMA_WR_RX);
4821da177e4SLinus Torvalds 	return 0;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds trouble:
4851da177e4SLinus Torvalds 	if (paddr)
4861da177e4SLinus Torvalds 		pci_unmap_single(eni_dev->pci_dev,paddr,skb->len,
4871da177e4SLinus Torvalds 		    PCI_DMA_FROMDEVICE);
4881da177e4SLinus Torvalds 	if (skb) dev_kfree_skb_irq(skb);
4891da177e4SLinus Torvalds 	return -1;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds static void discard(struct atm_vcc *vcc,unsigned long size)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	eni_vcc = ENI_VCC(vcc);
4981da177e4SLinus Torvalds 	EVENT("discard (size=%ld)\n",size,0);
4991da177e4SLinus Torvalds 	while (do_rx_dma(vcc,NULL,1,size,0)) EVENT("BUSY LOOP",0,0);
5001da177e4SLinus Torvalds 	    /* could do a full fallback, but that might be more expensive */
5011da177e4SLinus Torvalds 	if (eni_vcc->rxing) ENI_PRV_POS(eni_vcc->last) += size+1;
5021da177e4SLinus Torvalds 	else eni_vcc->rx_pos = (eni_vcc->rx_pos+size+1) & (eni_vcc->words-1);
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds /*
5071da177e4SLinus Torvalds  * TODO: should check whether direct copies (without DMA setup, dequeuing on
5081da177e4SLinus Torvalds  * interrupt, etc.) aren't much faster for AAL0
5091da177e4SLinus Torvalds  */
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds static int rx_aal0(struct atm_vcc *vcc)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
5141da177e4SLinus Torvalds 	unsigned long descr;
5151da177e4SLinus Torvalds 	unsigned long length;
5161da177e4SLinus Torvalds 	struct sk_buff *skb;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	DPRINTK(">rx_aal0\n");
5191da177e4SLinus Torvalds 	eni_vcc = ENI_VCC(vcc);
5201da177e4SLinus Torvalds 	descr = readl(eni_vcc->recv+eni_vcc->descr*4);
5211da177e4SLinus Torvalds 	if ((descr & MID_RED_IDEN) != (MID_RED_RX_ID << MID_RED_SHIFT)) {
5221da177e4SLinus Torvalds 		rx_ident_err(vcc);
5231da177e4SLinus Torvalds 		return 1;
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds 	if (descr & MID_RED_T) {
5261da177e4SLinus Torvalds 		DPRINTK(DEV_LABEL "(itf %d): trashing empty cell\n",
5271da177e4SLinus Torvalds 		    vcc->dev->number);
5281da177e4SLinus Torvalds 		length = 0;
5291da177e4SLinus Torvalds 		atomic_inc(&vcc->stats->rx_err);
5301da177e4SLinus Torvalds 	}
5311da177e4SLinus Torvalds 	else {
5321da177e4SLinus Torvalds 		length = ATM_CELL_SIZE-1; /* no HEC */
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 	skb = length ? atm_alloc_charge(vcc,length,GFP_ATOMIC) : NULL;
5351da177e4SLinus Torvalds 	if (!skb) {
5361da177e4SLinus Torvalds 		discard(vcc,length >> 2);
5371da177e4SLinus Torvalds 		return 0;
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds 	skb_put(skb,length);
540a61bbcf2SPatrick McHardy 	skb_set_timestamp(skb, &eni_vcc->timestamp);
5411da177e4SLinus Torvalds 	DPRINTK("got len %ld\n",length);
5421da177e4SLinus Torvalds 	if (do_rx_dma(vcc,skb,1,length >> 2,length >> 2)) return 1;
5431da177e4SLinus Torvalds 	eni_vcc->rxing++;
5441da177e4SLinus Torvalds 	return 0;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds static int rx_aal5(struct atm_vcc *vcc)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
5511da177e4SLinus Torvalds 	unsigned long descr;
5521da177e4SLinus Torvalds 	unsigned long size,eff,length;
5531da177e4SLinus Torvalds 	struct sk_buff *skb;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	EVENT("rx_aal5\n",0,0);
5561da177e4SLinus Torvalds 	DPRINTK(">rx_aal5\n");
5571da177e4SLinus Torvalds 	eni_vcc = ENI_VCC(vcc);
5581da177e4SLinus Torvalds 	descr = readl(eni_vcc->recv+eni_vcc->descr*4);
5591da177e4SLinus Torvalds 	if ((descr & MID_RED_IDEN) != (MID_RED_RX_ID << MID_RED_SHIFT)) {
5601da177e4SLinus Torvalds 		rx_ident_err(vcc);
5611da177e4SLinus Torvalds 		return 1;
5621da177e4SLinus Torvalds 	}
5631da177e4SLinus Torvalds 	if (descr & (MID_RED_T | MID_RED_CRC_ERR)) {
5641da177e4SLinus Torvalds 		if (descr & MID_RED_T) {
5651da177e4SLinus Torvalds 			EVENT("empty cell (descr=0x%lx)\n",descr,0);
5661da177e4SLinus Torvalds 			DPRINTK(DEV_LABEL "(itf %d): trashing empty cell\n",
5671da177e4SLinus Torvalds 			    vcc->dev->number);
5681da177e4SLinus Torvalds 			size = 0;
5691da177e4SLinus Torvalds 		}
5701da177e4SLinus Torvalds 		else {
5711da177e4SLinus Torvalds 			static unsigned long silence = 0;
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 			if (time_after(jiffies, silence) || silence == 0) {
5741da177e4SLinus Torvalds 				printk(KERN_WARNING DEV_LABEL "(itf %d): "
5751da177e4SLinus Torvalds 				    "discarding PDU(s) with CRC error\n",
5761da177e4SLinus Torvalds 				    vcc->dev->number);
5771da177e4SLinus Torvalds 				silence = (jiffies+2*HZ)|1;
5781da177e4SLinus Torvalds 			}
5791da177e4SLinus Torvalds 			size = (descr & MID_RED_COUNT)*(ATM_CELL_PAYLOAD >> 2);
5801da177e4SLinus Torvalds 			EVENT("CRC error (descr=0x%lx,size=%ld)\n",descr,
5811da177e4SLinus Torvalds 			    size);
5821da177e4SLinus Torvalds 		}
5831da177e4SLinus Torvalds 		eff = length = 0;
5841da177e4SLinus Torvalds 		atomic_inc(&vcc->stats->rx_err);
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 	else {
5871da177e4SLinus Torvalds 		size = (descr & MID_RED_COUNT)*(ATM_CELL_PAYLOAD >> 2);
5881da177e4SLinus Torvalds 		DPRINTK("size=%ld\n",size);
5891da177e4SLinus Torvalds 		length = readl(eni_vcc->recv+(((eni_vcc->descr+size-1) &
5901da177e4SLinus Torvalds 		    (eni_vcc->words-1)))*4) & 0xffff;
5911da177e4SLinus Torvalds 				/* -trailer(2)+header(1) */
5921da177e4SLinus Torvalds 		if (length && length <= (size << 2)-8 && length <=
5931da177e4SLinus Torvalds 		  ATM_MAX_AAL5_PDU) eff = (length+3) >> 2;
5941da177e4SLinus Torvalds 		else {				 /* ^ trailer length (8) */
5951da177e4SLinus Torvalds 			EVENT("bad PDU (descr=0x08%lx,length=%ld)\n",descr,
5961da177e4SLinus Torvalds 			    length);
5971da177e4SLinus Torvalds 			printk(KERN_ERR DEV_LABEL "(itf %d): bad AAL5 PDU "
5981da177e4SLinus Torvalds 			    "(VCI=%d,length=%ld,size=%ld (descr 0x%lx))\n",
5991da177e4SLinus Torvalds 			    vcc->dev->number,vcc->vci,length,size << 2,descr);
6001da177e4SLinus Torvalds 			length = eff = 0;
6011da177e4SLinus Torvalds 			atomic_inc(&vcc->stats->rx_err);
6021da177e4SLinus Torvalds 		}
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 	skb = eff ? atm_alloc_charge(vcc,eff << 2,GFP_ATOMIC) : NULL;
6051da177e4SLinus Torvalds 	if (!skb) {
6061da177e4SLinus Torvalds 		discard(vcc,size);
6071da177e4SLinus Torvalds 		return 0;
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds 	skb_put(skb,length);
6101da177e4SLinus Torvalds 	DPRINTK("got len %ld\n",length);
6111da177e4SLinus Torvalds 	if (do_rx_dma(vcc,skb,1,size,eff)) return 1;
6121da177e4SLinus Torvalds 	eni_vcc->rxing++;
6131da177e4SLinus Torvalds 	return 0;
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds static inline int rx_vcc(struct atm_vcc *vcc)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	void __iomem *vci_dsc;
6201da177e4SLinus Torvalds 	unsigned long tmp;
6211da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	eni_vcc = ENI_VCC(vcc);
6241da177e4SLinus Torvalds 	vci_dsc = ENI_DEV(vcc->dev)->vci+vcc->vci*16;
6251da177e4SLinus Torvalds 	EVENT("rx_vcc(1)\n",0,0);
6261da177e4SLinus Torvalds 	while (eni_vcc->descr != (tmp = (readl(vci_dsc+4) & MID_VCI_DESCR) >>
6271da177e4SLinus Torvalds 	    MID_VCI_DESCR_SHIFT)) {
6281da177e4SLinus Torvalds 		EVENT("rx_vcc(2: host dsc=0x%lx, nic dsc=0x%lx)\n",
6291da177e4SLinus Torvalds 		    eni_vcc->descr,tmp);
6301da177e4SLinus Torvalds 		DPRINTK("CB_DESCR %ld REG_DESCR %d\n",ENI_VCC(vcc)->descr,
6311da177e4SLinus Torvalds 		    (((unsigned) readl(vci_dsc+4) & MID_VCI_DESCR) >>
6321da177e4SLinus Torvalds 		    MID_VCI_DESCR_SHIFT));
6331da177e4SLinus Torvalds 		if (ENI_VCC(vcc)->rx(vcc)) return 1;
6341da177e4SLinus Torvalds 	}
6351da177e4SLinus Torvalds 	/* clear IN_SERVICE flag */
6361da177e4SLinus Torvalds 	writel(readl(vci_dsc) & ~MID_VCI_IN_SERVICE,vci_dsc);
6371da177e4SLinus Torvalds 	/*
6381da177e4SLinus Torvalds 	 * If new data has arrived between evaluating the while condition and
6391da177e4SLinus Torvalds 	 * clearing IN_SERVICE, we wouldn't be notified until additional data
6401da177e4SLinus Torvalds 	 * follows. So we have to loop again to be sure.
6411da177e4SLinus Torvalds 	 */
6421da177e4SLinus Torvalds 	EVENT("rx_vcc(3)\n",0,0);
6431da177e4SLinus Torvalds 	while (ENI_VCC(vcc)->descr != (tmp = (readl(vci_dsc+4) & MID_VCI_DESCR)
6441da177e4SLinus Torvalds 	    >> MID_VCI_DESCR_SHIFT)) {
6451da177e4SLinus Torvalds 		EVENT("rx_vcc(4: host dsc=0x%lx, nic dsc=0x%lx)\n",
6461da177e4SLinus Torvalds 		    eni_vcc->descr,tmp);
6471da177e4SLinus Torvalds 		DPRINTK("CB_DESCR %ld REG_DESCR %d\n",ENI_VCC(vcc)->descr,
6481da177e4SLinus Torvalds 		    (((unsigned) readl(vci_dsc+4) & MID_VCI_DESCR) >>
6491da177e4SLinus Torvalds 		    MID_VCI_DESCR_SHIFT));
6501da177e4SLinus Torvalds 		if (ENI_VCC(vcc)->rx(vcc)) return 1;
6511da177e4SLinus Torvalds 	}
6521da177e4SLinus Torvalds 	return 0;
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds static void poll_rx(struct atm_dev *dev)
6571da177e4SLinus Torvalds {
6581da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
6591da177e4SLinus Torvalds 	struct atm_vcc *curr;
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
6621da177e4SLinus Torvalds 	while ((curr = eni_dev->fast)) {
6631da177e4SLinus Torvalds 		EVENT("poll_rx.fast\n",0,0);
6641da177e4SLinus Torvalds 		if (rx_vcc(curr)) return;
6651da177e4SLinus Torvalds 		eni_dev->fast = ENI_VCC(curr)->next;
6661da177e4SLinus Torvalds 		ENI_VCC(curr)->next = ENI_VCC_NOS;
6671da177e4SLinus Torvalds 		barrier();
6681da177e4SLinus Torvalds 		ENI_VCC(curr)->servicing--;
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 	while ((curr = eni_dev->slow)) {
6711da177e4SLinus Torvalds 		EVENT("poll_rx.slow\n",0,0);
6721da177e4SLinus Torvalds 		if (rx_vcc(curr)) return;
6731da177e4SLinus Torvalds 		eni_dev->slow = ENI_VCC(curr)->next;
6741da177e4SLinus Torvalds 		ENI_VCC(curr)->next = ENI_VCC_NOS;
6751da177e4SLinus Torvalds 		barrier();
6761da177e4SLinus Torvalds 		ENI_VCC(curr)->servicing--;
6771da177e4SLinus Torvalds 	}
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds static void get_service(struct atm_dev *dev)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
6841da177e4SLinus Torvalds 	struct atm_vcc *vcc;
6851da177e4SLinus Torvalds 	unsigned long vci;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	DPRINTK(">get_service\n");
6881da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
6891da177e4SLinus Torvalds 	while (eni_in(MID_SERV_WRITE) != eni_dev->serv_read) {
6901da177e4SLinus Torvalds 		vci = readl(eni_dev->service+eni_dev->serv_read*4);
6911da177e4SLinus Torvalds 		eni_dev->serv_read = (eni_dev->serv_read+1) & (NR_SERVICE-1);
6921da177e4SLinus Torvalds 		vcc = eni_dev->rx_map[vci & 1023];
6931da177e4SLinus Torvalds 		if (!vcc) {
6941da177e4SLinus Torvalds 			printk(KERN_CRIT DEV_LABEL "(itf %d): VCI %ld not "
6951da177e4SLinus Torvalds 			    "found\n",dev->number,vci);
6961da177e4SLinus Torvalds 			continue; /* nasty but we try to go on anyway */
6971da177e4SLinus Torvalds 			/* @@@ nope, doesn't work */
6981da177e4SLinus Torvalds 		}
6991da177e4SLinus Torvalds 		EVENT("getting from service\n",0,0);
7001da177e4SLinus Torvalds 		if (ENI_VCC(vcc)->next != ENI_VCC_NOS) {
7011da177e4SLinus Torvalds 			EVENT("double service\n",0,0);
7021da177e4SLinus Torvalds 			DPRINTK("Grr, servicing VCC %ld twice\n",vci);
7031da177e4SLinus Torvalds 			continue;
7041da177e4SLinus Torvalds 		}
7051da177e4SLinus Torvalds 		do_gettimeofday(&ENI_VCC(vcc)->timestamp);
7061da177e4SLinus Torvalds 		ENI_VCC(vcc)->next = NULL;
7071da177e4SLinus Torvalds 		if (vcc->qos.rxtp.traffic_class == ATM_CBR) {
7081da177e4SLinus Torvalds 			if (eni_dev->fast)
7091da177e4SLinus Torvalds 				ENI_VCC(eni_dev->last_fast)->next = vcc;
7101da177e4SLinus Torvalds 			else eni_dev->fast = vcc;
7111da177e4SLinus Torvalds 			eni_dev->last_fast = vcc;
7121da177e4SLinus Torvalds 		}
7131da177e4SLinus Torvalds 		else {
7141da177e4SLinus Torvalds 			if (eni_dev->slow)
7151da177e4SLinus Torvalds 				ENI_VCC(eni_dev->last_slow)->next = vcc;
7161da177e4SLinus Torvalds 			else eni_dev->slow = vcc;
7171da177e4SLinus Torvalds 			eni_dev->last_slow = vcc;
7181da177e4SLinus Torvalds 		}
7191da177e4SLinus Torvalds putting++;
7201da177e4SLinus Torvalds 		ENI_VCC(vcc)->servicing++;
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds static void dequeue_rx(struct atm_dev *dev)
7261da177e4SLinus Torvalds {
7271da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
7281da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
7291da177e4SLinus Torvalds 	struct atm_vcc *vcc;
7301da177e4SLinus Torvalds 	struct sk_buff *skb;
7311da177e4SLinus Torvalds 	void __iomem *vci_dsc;
7321da177e4SLinus Torvalds 	int first;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
7351da177e4SLinus Torvalds 	first = 1;
7361da177e4SLinus Torvalds 	while (1) {
7371da177e4SLinus Torvalds 		skb = skb_dequeue(&eni_dev->rx_queue);
7381da177e4SLinus Torvalds 		if (!skb) {
7391da177e4SLinus Torvalds 			if (first) {
7401da177e4SLinus Torvalds 				DPRINTK(DEV_LABEL "(itf %d): RX but not "
7411da177e4SLinus Torvalds 				    "rxing\n",dev->number);
7421da177e4SLinus Torvalds 				EVENT("nothing to dequeue\n",0,0);
7431da177e4SLinus Torvalds 			}
7441da177e4SLinus Torvalds 			break;
7451da177e4SLinus Torvalds 		}
7461da177e4SLinus Torvalds 		EVENT("dequeued (size=%ld,pos=0x%lx)\n",ENI_PRV_SIZE(skb),
7471da177e4SLinus Torvalds 		    ENI_PRV_POS(skb));
7481da177e4SLinus Torvalds rx_dequeued++;
7491da177e4SLinus Torvalds 		vcc = ATM_SKB(skb)->vcc;
7501da177e4SLinus Torvalds 		eni_vcc = ENI_VCC(vcc);
7511da177e4SLinus Torvalds 		first = 0;
7521da177e4SLinus Torvalds 		vci_dsc = eni_dev->vci+vcc->vci*16;
7531da177e4SLinus Torvalds 		if (!EEPMOK(eni_vcc->rx_pos,ENI_PRV_SIZE(skb),
7541da177e4SLinus Torvalds 		    (readl(vci_dsc+4) & MID_VCI_READ) >> MID_VCI_READ_SHIFT,
7551da177e4SLinus Torvalds 		    eni_vcc->words)) {
7561da177e4SLinus Torvalds 			EVENT("requeuing\n",0,0);
7571da177e4SLinus Torvalds 			skb_queue_head(&eni_dev->rx_queue,skb);
7581da177e4SLinus Torvalds 			break;
7591da177e4SLinus Torvalds 		}
7601da177e4SLinus Torvalds 		eni_vcc->rxing--;
7611da177e4SLinus Torvalds 		eni_vcc->rx_pos = ENI_PRV_POS(skb) & (eni_vcc->words-1);
7621da177e4SLinus Torvalds 		pci_unmap_single(eni_dev->pci_dev,ENI_PRV_PADDR(skb),skb->len,
7631da177e4SLinus Torvalds 		    PCI_DMA_TODEVICE);
7641da177e4SLinus Torvalds 		if (!skb->len) dev_kfree_skb_irq(skb);
7651da177e4SLinus Torvalds 		else {
7661da177e4SLinus Torvalds 			EVENT("pushing (len=%ld)\n",skb->len,0);
7671da177e4SLinus Torvalds 			if (vcc->qos.aal == ATM_AAL0)
7681da177e4SLinus Torvalds 				*(unsigned long *) skb->data =
7691da177e4SLinus Torvalds 				    ntohl(*(unsigned long *) skb->data);
7701da177e4SLinus Torvalds 			memset(skb->cb,0,sizeof(struct eni_skb_prv));
7711da177e4SLinus Torvalds 			vcc->push(vcc,skb);
7721da177e4SLinus Torvalds 			pushed++;
7731da177e4SLinus Torvalds 		}
7741da177e4SLinus Torvalds 		atomic_inc(&vcc->stats->rx);
7751da177e4SLinus Torvalds 	}
7761da177e4SLinus Torvalds 	wake_up(&eni_dev->rx_wait);
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds static int open_rx_first(struct atm_vcc *vcc)
7811da177e4SLinus Torvalds {
7821da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
7831da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
7841da177e4SLinus Torvalds 	unsigned long size;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	DPRINTK("open_rx_first\n");
7871da177e4SLinus Torvalds 	eni_dev = ENI_DEV(vcc->dev);
7881da177e4SLinus Torvalds 	eni_vcc = ENI_VCC(vcc);
7891da177e4SLinus Torvalds 	eni_vcc->rx = NULL;
7901da177e4SLinus Torvalds 	if (vcc->qos.rxtp.traffic_class == ATM_NONE) return 0;
7911da177e4SLinus Torvalds 	size = vcc->qos.rxtp.max_sdu*eni_dev->rx_mult/100;
7921da177e4SLinus Torvalds 	if (size > MID_MAX_BUF_SIZE && vcc->qos.rxtp.max_sdu <=
7931da177e4SLinus Torvalds 	    MID_MAX_BUF_SIZE)
7941da177e4SLinus Torvalds 		size = MID_MAX_BUF_SIZE;
7951da177e4SLinus Torvalds 	eni_vcc->recv = eni_alloc_mem(eni_dev,&size);
7961da177e4SLinus Torvalds 	DPRINTK("rx at 0x%lx\n",eni_vcc->recv);
7971da177e4SLinus Torvalds 	eni_vcc->words = size >> 2;
7981da177e4SLinus Torvalds 	if (!eni_vcc->recv) return -ENOBUFS;
7991da177e4SLinus Torvalds 	eni_vcc->rx = vcc->qos.aal == ATM_AAL5 ? rx_aal5 : rx_aal0;
8001da177e4SLinus Torvalds 	eni_vcc->descr = 0;
8011da177e4SLinus Torvalds 	eni_vcc->rx_pos = 0;
8021da177e4SLinus Torvalds 	eni_vcc->rxing = 0;
8031da177e4SLinus Torvalds 	eni_vcc->servicing = 0;
8041da177e4SLinus Torvalds 	eni_vcc->next = ENI_VCC_NOS;
8051da177e4SLinus Torvalds 	return 0;
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds static int open_rx_second(struct atm_vcc *vcc)
8101da177e4SLinus Torvalds {
8111da177e4SLinus Torvalds 	void __iomem *here;
8121da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
8131da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
8141da177e4SLinus Torvalds 	unsigned long size;
8151da177e4SLinus Torvalds 	int order;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	DPRINTK("open_rx_second\n");
8181da177e4SLinus Torvalds 	eni_dev = ENI_DEV(vcc->dev);
8191da177e4SLinus Torvalds 	eni_vcc = ENI_VCC(vcc);
8201da177e4SLinus Torvalds 	if (!eni_vcc->rx) return 0;
8211da177e4SLinus Torvalds 	/* set up VCI descriptor */
8221da177e4SLinus Torvalds 	here = eni_dev->vci+vcc->vci*16;
8231da177e4SLinus Torvalds 	DPRINTK("loc 0x%x\n",(unsigned) (eni_vcc->recv-eni_dev->ram)/4);
8241da177e4SLinus Torvalds 	size = eni_vcc->words >> 8;
8251da177e4SLinus Torvalds 	for (order = -1; size; order++) size >>= 1;
8261da177e4SLinus Torvalds 	writel(0,here+4); /* descr, read = 0 */
8271da177e4SLinus Torvalds 	writel(0,here+8); /* write, state, count = 0 */
8281da177e4SLinus Torvalds 	if (eni_dev->rx_map[vcc->vci])
8291da177e4SLinus Torvalds 		printk(KERN_CRIT DEV_LABEL "(itf %d): BUG - VCI %d already "
8301da177e4SLinus Torvalds 		    "in use\n",vcc->dev->number,vcc->vci);
8311da177e4SLinus Torvalds 	eni_dev->rx_map[vcc->vci] = vcc; /* now it counts */
8321da177e4SLinus Torvalds 	writel(((vcc->qos.aal != ATM_AAL5 ? MID_MODE_RAW : MID_MODE_AAL5) <<
8331da177e4SLinus Torvalds 	    MID_VCI_MODE_SHIFT) | MID_VCI_PTI_MODE |
8341da177e4SLinus Torvalds 	    (((eni_vcc->recv-eni_dev->ram) >> (MID_LOC_SKIP+2)) <<
8351da177e4SLinus Torvalds 	    MID_VCI_LOCATION_SHIFT) | (order << MID_VCI_SIZE_SHIFT),here);
8361da177e4SLinus Torvalds 	return 0;
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds static void close_rx(struct atm_vcc *vcc)
8411da177e4SLinus Torvalds {
8421da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait,current);
8431da177e4SLinus Torvalds 	void __iomem *here;
8441da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
8451da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	eni_vcc = ENI_VCC(vcc);
8481da177e4SLinus Torvalds 	if (!eni_vcc->rx) return;
8491da177e4SLinus Torvalds 	eni_dev = ENI_DEV(vcc->dev);
8501da177e4SLinus Torvalds 	if (vcc->vpi != ATM_VPI_UNSPEC && vcc->vci != ATM_VCI_UNSPEC) {
8511da177e4SLinus Torvalds 		here = eni_dev->vci+vcc->vci*16;
8521da177e4SLinus Torvalds 		/* block receiver */
8531da177e4SLinus Torvalds 		writel((readl(here) & ~MID_VCI_MODE) | (MID_MODE_TRASH <<
8541da177e4SLinus Torvalds 		    MID_VCI_MODE_SHIFT),here);
8551da177e4SLinus Torvalds 		/* wait for receiver to become idle */
8561da177e4SLinus Torvalds 		udelay(27);
8571da177e4SLinus Torvalds 		/* discard pending cell */
8581da177e4SLinus Torvalds 		writel(readl(here) & ~MID_VCI_IN_SERVICE,here);
8591da177e4SLinus Torvalds 		/* don't accept any new ones */
8601da177e4SLinus Torvalds 		eni_dev->rx_map[vcc->vci] = NULL;
8611da177e4SLinus Torvalds 		/* wait for RX queue to drain */
8621da177e4SLinus Torvalds 		DPRINTK("eni_close: waiting for RX ...\n");
8631da177e4SLinus Torvalds 		EVENT("RX closing\n",0,0);
8641da177e4SLinus Torvalds 		add_wait_queue(&eni_dev->rx_wait,&wait);
8651da177e4SLinus Torvalds 		set_current_state(TASK_UNINTERRUPTIBLE);
8661da177e4SLinus Torvalds 		barrier();
8671da177e4SLinus Torvalds 		for (;;) {
8681da177e4SLinus Torvalds 			/* transition service->rx: rxing++, servicing-- */
8691da177e4SLinus Torvalds 			if (!eni_vcc->servicing) {
8701da177e4SLinus Torvalds 				barrier();
8711da177e4SLinus Torvalds 				if (!eni_vcc->rxing) break;
8721da177e4SLinus Torvalds 			}
8731da177e4SLinus Torvalds 			EVENT("drain PDUs (rx %ld, serv %ld)\n",eni_vcc->rxing,
8741da177e4SLinus Torvalds 			    eni_vcc->servicing);
8751da177e4SLinus Torvalds 			printk(KERN_INFO "%d+%d RX left\n",eni_vcc->servicing,
8761da177e4SLinus Torvalds 			    eni_vcc->rxing);
8771da177e4SLinus Torvalds 			schedule();
8781da177e4SLinus Torvalds 			set_current_state(TASK_UNINTERRUPTIBLE);
8791da177e4SLinus Torvalds 		}
8801da177e4SLinus Torvalds 		for (;;) {
8811da177e4SLinus Torvalds 			int at_end;
8821da177e4SLinus Torvalds 			u32 tmp;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 			tasklet_disable(&eni_dev->task);
8851da177e4SLinus Torvalds 			tmp = readl(eni_dev->vci+vcc->vci*16+4) & MID_VCI_READ;
8861da177e4SLinus Torvalds 			at_end = eni_vcc->rx_pos == tmp >> MID_VCI_READ_SHIFT;
8871da177e4SLinus Torvalds 			tasklet_enable(&eni_dev->task);
8881da177e4SLinus Torvalds 			if (at_end) break;
8891da177e4SLinus Torvalds 			EVENT("drain discard (host 0x%lx, nic 0x%lx)\n",
8901da177e4SLinus Torvalds 			    eni_vcc->rx_pos,tmp);
8911da177e4SLinus Torvalds 			printk(KERN_INFO "draining RX: host 0x%lx, nic 0x%x\n",
8921da177e4SLinus Torvalds 			    eni_vcc->rx_pos,tmp);
8931da177e4SLinus Torvalds 			schedule();
8941da177e4SLinus Torvalds 			set_current_state(TASK_UNINTERRUPTIBLE);
8951da177e4SLinus Torvalds 		}
8961da177e4SLinus Torvalds 		set_current_state(TASK_RUNNING);
8971da177e4SLinus Torvalds 		remove_wait_queue(&eni_dev->rx_wait,&wait);
8981da177e4SLinus Torvalds 	}
8991da177e4SLinus Torvalds 	eni_free_mem(eni_dev,eni_vcc->recv,eni_vcc->words << 2);
9001da177e4SLinus Torvalds 	eni_vcc->rx = NULL;
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds static int start_rx(struct atm_dev *dev)
9051da177e4SLinus Torvalds {
9061da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
9091da177e4SLinus Torvalds 	eni_dev->rx_map = (struct atm_vcc **) get_zeroed_page(GFP_KERNEL);
9101da177e4SLinus Torvalds 	if (!eni_dev->rx_map) {
9111da177e4SLinus Torvalds 		printk(KERN_ERR DEV_LABEL "(itf %d): couldn't get free page\n",
9121da177e4SLinus Torvalds 		    dev->number);
9131da177e4SLinus Torvalds 		free_page((unsigned long) eni_dev->free_list);
9141da177e4SLinus Torvalds 		return -ENOMEM;
9151da177e4SLinus Torvalds 	}
9161da177e4SLinus Torvalds 	memset(eni_dev->rx_map,0,PAGE_SIZE);
9171da177e4SLinus Torvalds 	eni_dev->rx_mult = DEFAULT_RX_MULT;
9181da177e4SLinus Torvalds 	eni_dev->fast = eni_dev->last_fast = NULL;
9191da177e4SLinus Torvalds 	eni_dev->slow = eni_dev->last_slow = NULL;
9201da177e4SLinus Torvalds 	init_waitqueue_head(&eni_dev->rx_wait);
9211da177e4SLinus Torvalds 	skb_queue_head_init(&eni_dev->rx_queue);
9221da177e4SLinus Torvalds 	eni_dev->serv_read = eni_in(MID_SERV_WRITE);
9231da177e4SLinus Torvalds 	eni_out(0,MID_DMA_WR_RX);
9241da177e4SLinus Torvalds 	return 0;
9251da177e4SLinus Torvalds }
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds /*----------------------------------- TX ------------------------------------*/
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds enum enq_res { enq_ok,enq_next,enq_jam };
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds static inline void put_dma(int chan,u32 *dma,int *j,dma_addr_t paddr,
9351da177e4SLinus Torvalds     u32 size)
9361da177e4SLinus Torvalds {
9371da177e4SLinus Torvalds 	u32 init,words;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	DPRINTK("put_dma: 0x%lx+0x%x\n",(unsigned long) paddr,size);
9401da177e4SLinus Torvalds 	EVENT("put_dma: 0x%lx+0x%lx\n",(unsigned long) paddr,size);
9411da177e4SLinus Torvalds #if 0 /* don't complain anymore */
9421da177e4SLinus Torvalds 	if (paddr & 3)
9431da177e4SLinus Torvalds 		printk(KERN_ERR "put_dma: unaligned addr (0x%lx)\n",paddr);
9441da177e4SLinus Torvalds 	if (size & 3)
9451da177e4SLinus Torvalds 		printk(KERN_ERR "put_dma: unaligned size (0x%lx)\n",size);
9461da177e4SLinus Torvalds #endif
9471da177e4SLinus Torvalds 	if (paddr & 3) {
9481da177e4SLinus Torvalds 		init = 4-(paddr & 3);
9491da177e4SLinus Torvalds 		if (init > size || size < 7) init = size;
9501da177e4SLinus Torvalds 		DPRINTK("put_dma: %lx DMA: %d/%d bytes\n",
9511da177e4SLinus Torvalds 		    (unsigned long) paddr,init,size);
9521da177e4SLinus Torvalds 		dma[(*j)++] = MID_DT_BYTE | (init << MID_DMA_COUNT_SHIFT) |
9531da177e4SLinus Torvalds 		    (chan << MID_DMA_CHAN_SHIFT);
9541da177e4SLinus Torvalds 		dma[(*j)++] = paddr;
9551da177e4SLinus Torvalds 		paddr += init;
9561da177e4SLinus Torvalds 		size -= init;
9571da177e4SLinus Torvalds 	}
9581da177e4SLinus Torvalds 	words = size >> 2;
9591da177e4SLinus Torvalds 	size &= 3;
9601da177e4SLinus Torvalds 	if (words && (paddr & 31)) {
9611da177e4SLinus Torvalds 		init = 8-((paddr & 31) >> 2);
9621da177e4SLinus Torvalds 		if (init > words) init = words;
9631da177e4SLinus Torvalds 		DPRINTK("put_dma: %lx DMA: %d/%d words\n",
9641da177e4SLinus Torvalds 		    (unsigned long) paddr,init,words);
9651da177e4SLinus Torvalds 		dma[(*j)++] = MID_DT_WORD | (init << MID_DMA_COUNT_SHIFT) |
9661da177e4SLinus Torvalds 		    (chan << MID_DMA_CHAN_SHIFT);
9671da177e4SLinus Torvalds 		dma[(*j)++] = paddr;
9681da177e4SLinus Torvalds 		paddr += init << 2;
9691da177e4SLinus Torvalds 		words -= init;
9701da177e4SLinus Torvalds 	}
9711da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_TX_16W /* may work with some PCI chipsets ... */
9721da177e4SLinus Torvalds 	if (words & ~15) {
9731da177e4SLinus Torvalds 		DPRINTK("put_dma: %lx DMA: %d*16/%d words\n",
9741da177e4SLinus Torvalds 		    (unsigned long) paddr,words >> 4,words);
9751da177e4SLinus Torvalds 		dma[(*j)++] = MID_DT_16W | ((words >> 4) << MID_DMA_COUNT_SHIFT)
9761da177e4SLinus Torvalds 		    | (chan << MID_DMA_CHAN_SHIFT);
9771da177e4SLinus Torvalds 		dma[(*j)++] = paddr;
9781da177e4SLinus Torvalds 		paddr += (words & ~15) << 2;
9791da177e4SLinus Torvalds 		words &= 15;
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds #endif
9821da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_TX_8W /* recommended */
9831da177e4SLinus Torvalds 	if (words & ~7) {
9841da177e4SLinus Torvalds 		DPRINTK("put_dma: %lx DMA: %d*8/%d words\n",
9851da177e4SLinus Torvalds 		    (unsigned long) paddr,words >> 3,words);
9861da177e4SLinus Torvalds 		dma[(*j)++] = MID_DT_8W | ((words >> 3) << MID_DMA_COUNT_SHIFT)
9871da177e4SLinus Torvalds 		    | (chan << MID_DMA_CHAN_SHIFT);
9881da177e4SLinus Torvalds 		dma[(*j)++] = paddr;
9891da177e4SLinus Torvalds 		paddr += (words & ~7) << 2;
9901da177e4SLinus Torvalds 		words &= 7;
9911da177e4SLinus Torvalds 	}
9921da177e4SLinus Torvalds #endif
9931da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_TX_4W /* probably useless if TX_8W or TX_16W */
9941da177e4SLinus Torvalds 	if (words & ~3) {
9951da177e4SLinus Torvalds 		DPRINTK("put_dma: %lx DMA: %d*4/%d words\n",
9961da177e4SLinus Torvalds 		    (unsigned long) paddr,words >> 2,words);
9971da177e4SLinus Torvalds 		dma[(*j)++] = MID_DT_4W | ((words >> 2) << MID_DMA_COUNT_SHIFT)
9981da177e4SLinus Torvalds 		    | (chan << MID_DMA_CHAN_SHIFT);
9991da177e4SLinus Torvalds 		dma[(*j)++] = paddr;
10001da177e4SLinus Torvalds 		paddr += (words & ~3) << 2;
10011da177e4SLinus Torvalds 		words &= 3;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds #endif
10041da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_TX_2W /* probably useless if TX_4W, TX_8W, ... */
10051da177e4SLinus Torvalds 	if (words & ~1) {
10061da177e4SLinus Torvalds 		DPRINTK("put_dma: %lx DMA: %d*2/%d words\n",
10071da177e4SLinus Torvalds 		    (unsigned long) paddr,words >> 1,words);
10081da177e4SLinus Torvalds 		dma[(*j)++] = MID_DT_2W | ((words >> 1) << MID_DMA_COUNT_SHIFT)
10091da177e4SLinus Torvalds 		    | (chan << MID_DMA_CHAN_SHIFT);
10101da177e4SLinus Torvalds 		dma[(*j)++] = paddr;
10111da177e4SLinus Torvalds 		paddr += (words & ~1) << 2;
10121da177e4SLinus Torvalds 		words &= 1;
10131da177e4SLinus Torvalds 	}
10141da177e4SLinus Torvalds #endif
10151da177e4SLinus Torvalds 	if (words) {
10161da177e4SLinus Torvalds 		DPRINTK("put_dma: %lx DMA: %d words\n",(unsigned long) paddr,
10171da177e4SLinus Torvalds 		    words);
10181da177e4SLinus Torvalds 		dma[(*j)++] = MID_DT_WORD | (words << MID_DMA_COUNT_SHIFT) |
10191da177e4SLinus Torvalds 		    (chan << MID_DMA_CHAN_SHIFT);
10201da177e4SLinus Torvalds 		dma[(*j)++] = paddr;
10211da177e4SLinus Torvalds 		paddr += words << 2;
10221da177e4SLinus Torvalds 	}
10231da177e4SLinus Torvalds 	if (size) {
10241da177e4SLinus Torvalds 		DPRINTK("put_dma: %lx DMA: %d bytes\n",(unsigned long) paddr,
10251da177e4SLinus Torvalds 		    size);
10261da177e4SLinus Torvalds 		dma[(*j)++] = MID_DT_BYTE | (size << MID_DMA_COUNT_SHIFT) |
10271da177e4SLinus Torvalds 		    (chan << MID_DMA_CHAN_SHIFT);
10281da177e4SLinus Torvalds 		dma[(*j)++] = paddr;
10291da177e4SLinus Torvalds 	}
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds static enum enq_res do_tx(struct sk_buff *skb)
10341da177e4SLinus Torvalds {
10351da177e4SLinus Torvalds 	struct atm_vcc *vcc;
10361da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
10371da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
10381da177e4SLinus Torvalds 	struct eni_tx *tx;
10391da177e4SLinus Torvalds 	dma_addr_t paddr;
10401da177e4SLinus Torvalds 	u32 dma_rd,dma_wr;
10411da177e4SLinus Torvalds 	u32 size; /* in words */
10421da177e4SLinus Torvalds 	int aal5,dma_size,i,j;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	DPRINTK(">do_tx\n");
10451da177e4SLinus Torvalds 	NULLCHECK(skb);
10461da177e4SLinus Torvalds 	EVENT("do_tx: skb=0x%lx, %ld bytes\n",(unsigned long) skb,skb->len);
10471da177e4SLinus Torvalds 	vcc = ATM_SKB(skb)->vcc;
10481da177e4SLinus Torvalds 	NULLCHECK(vcc);
10491da177e4SLinus Torvalds 	eni_dev = ENI_DEV(vcc->dev);
10501da177e4SLinus Torvalds 	NULLCHECK(eni_dev);
10511da177e4SLinus Torvalds 	eni_vcc = ENI_VCC(vcc);
10521da177e4SLinus Torvalds 	tx = eni_vcc->tx;
10531da177e4SLinus Torvalds 	NULLCHECK(tx);
10541da177e4SLinus Torvalds #if 0 /* Enable this for testing with the "align" program */
10551da177e4SLinus Torvalds 	{
10561da177e4SLinus Torvalds 		unsigned int hack = *((char *) skb->data)-'0';
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 		if (hack < 8) {
10591da177e4SLinus Torvalds 			skb->data += hack;
10601da177e4SLinus Torvalds 			skb->len -= hack;
10611da177e4SLinus Torvalds 		}
10621da177e4SLinus Torvalds 	}
10631da177e4SLinus Torvalds #endif
10641da177e4SLinus Torvalds #if 0 /* should work now */
10651da177e4SLinus Torvalds 	if ((unsigned long) skb->data & 3)
10661da177e4SLinus Torvalds 		printk(KERN_ERR DEV_LABEL "(itf %d): VCI %d has mis-aligned "
10671da177e4SLinus Torvalds 		    "TX data\n",vcc->dev->number,vcc->vci);
10681da177e4SLinus Torvalds #endif
10691da177e4SLinus Torvalds 	/*
10701da177e4SLinus Torvalds 	 * Potential future IP speedup: make hard_header big enough to put
10711da177e4SLinus Torvalds 	 * segmentation descriptor directly into PDU. Saves: 4 slave writes,
10721da177e4SLinus Torvalds 	 * 1 DMA xfer & 2 DMA'ed bytes (protocol layering is for wimps :-)
10731da177e4SLinus Torvalds 	 */
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	aal5 = vcc->qos.aal == ATM_AAL5;
10761da177e4SLinus Torvalds 	/* check space in buffer */
10771da177e4SLinus Torvalds 	if (!aal5)
10781da177e4SLinus Torvalds 		size = (ATM_CELL_PAYLOAD >> 2)+TX_DESCR_SIZE;
10791da177e4SLinus Torvalds 			/* cell without HEC plus segmentation header (includes
10801da177e4SLinus Torvalds 			   four-byte cell header) */
10811da177e4SLinus Torvalds 	else {
10821da177e4SLinus Torvalds 		size = skb->len+4*AAL5_TRAILER+ATM_CELL_PAYLOAD-1;
10831da177e4SLinus Torvalds 			/* add AAL5 trailer */
10841da177e4SLinus Torvalds 		size = ((size-(size % ATM_CELL_PAYLOAD)) >> 2)+TX_DESCR_SIZE;
10851da177e4SLinus Torvalds 						/* add segmentation header */
10861da177e4SLinus Torvalds 	}
10871da177e4SLinus Torvalds 	/*
10881da177e4SLinus Torvalds 	 * Can I move tx_pos by size bytes without getting closer than TX_GAP
10891da177e4SLinus Torvalds 	 * to the read pointer ? TX_GAP means to leave some space for what
10901da177e4SLinus Torvalds 	 * the manual calls "too close".
10911da177e4SLinus Torvalds 	 */
10921da177e4SLinus Torvalds 	if (!NEPMOK(tx->tx_pos,size+TX_GAP,
10931da177e4SLinus Torvalds 	    eni_in(MID_TX_RDPTR(tx->index)),tx->words)) {
10941da177e4SLinus Torvalds 		DPRINTK(DEV_LABEL "(itf %d): TX full (size %d)\n",
10951da177e4SLinus Torvalds 		    vcc->dev->number,size);
10961da177e4SLinus Torvalds 		return enq_next;
10971da177e4SLinus Torvalds 	}
10981da177e4SLinus Torvalds 	/* check DMA */
10991da177e4SLinus Torvalds 	dma_wr = eni_in(MID_DMA_WR_TX);
11001da177e4SLinus Torvalds 	dma_rd = eni_in(MID_DMA_RD_TX);
11011da177e4SLinus Torvalds 	dma_size = 3; /* JK for descriptor and final fill, plus final size
11021da177e4SLinus Torvalds 			 mis-alignment fix */
11031da177e4SLinus Torvalds DPRINTK("iovcnt = %d\n",skb_shinfo(skb)->nr_frags);
11041da177e4SLinus Torvalds 	if (!skb_shinfo(skb)->nr_frags) dma_size += 5;
11051da177e4SLinus Torvalds 	else dma_size += 5*(skb_shinfo(skb)->nr_frags+1);
11061da177e4SLinus Torvalds 	if (dma_size > TX_DMA_BUF) {
11071da177e4SLinus Torvalds 		printk(KERN_CRIT DEV_LABEL "(itf %d): needs %d DMA entries "
11081da177e4SLinus Torvalds 		    "(got only %d)\n",vcc->dev->number,dma_size,TX_DMA_BUF);
11091da177e4SLinus Torvalds 	}
11101da177e4SLinus Torvalds 	DPRINTK("dma_wr is %d, tx_pos is %ld\n",dma_wr,tx->tx_pos);
11111da177e4SLinus Torvalds 	if (dma_wr != dma_rd && ((dma_rd+NR_DMA_TX-dma_wr) & (NR_DMA_TX-1)) <
11121da177e4SLinus Torvalds 	     dma_size) {
11131da177e4SLinus Torvalds 		printk(KERN_WARNING DEV_LABEL "(itf %d): TX DMA full\n",
11141da177e4SLinus Torvalds 		    vcc->dev->number);
11151da177e4SLinus Torvalds 		return enq_jam;
11161da177e4SLinus Torvalds 	}
11171da177e4SLinus Torvalds 	paddr = pci_map_single(eni_dev->pci_dev,skb->data,skb->len,
11181da177e4SLinus Torvalds 	    PCI_DMA_TODEVICE);
11191da177e4SLinus Torvalds 	ENI_PRV_PADDR(skb) = paddr;
11201da177e4SLinus Torvalds 	/* prepare DMA queue entries */
11211da177e4SLinus Torvalds 	j = 0;
11221da177e4SLinus Torvalds 	eni_dev->dma[j++] = (((tx->tx_pos+TX_DESCR_SIZE) & (tx->words-1)) <<
11231da177e4SLinus Torvalds 	     MID_DMA_COUNT_SHIFT) | (tx->index << MID_DMA_CHAN_SHIFT) |
11241da177e4SLinus Torvalds 	     MID_DT_JK;
11251da177e4SLinus Torvalds 	j++;
11261da177e4SLinus Torvalds 	if (!skb_shinfo(skb)->nr_frags)
11271da177e4SLinus Torvalds 		if (aal5) put_dma(tx->index,eni_dev->dma,&j,paddr,skb->len);
11281da177e4SLinus Torvalds 		else put_dma(tx->index,eni_dev->dma,&j,paddr+4,skb->len-4);
11291da177e4SLinus Torvalds 	else {
11301da177e4SLinus Torvalds DPRINTK("doing direct send\n"); /* @@@ well, this doesn't work anyway */
11311da177e4SLinus Torvalds 		for (i = -1; i < skb_shinfo(skb)->nr_frags; i++)
11321da177e4SLinus Torvalds 			if (i == -1)
11331da177e4SLinus Torvalds 				put_dma(tx->index,eni_dev->dma,&j,(unsigned long)
11341da177e4SLinus Torvalds 				    skb->data,
11351da177e4SLinus Torvalds 				    skb->len - skb->data_len);
11361da177e4SLinus Torvalds 			else
11371da177e4SLinus Torvalds 				put_dma(tx->index,eni_dev->dma,&j,(unsigned long)
11381da177e4SLinus Torvalds 				    skb_shinfo(skb)->frags[i].page + skb_shinfo(skb)->frags[i].page_offset,
11391da177e4SLinus Torvalds 				    skb_shinfo(skb)->frags[i].size);
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 	if (skb->len & 3)
11421da177e4SLinus Torvalds 		put_dma(tx->index,eni_dev->dma,&j,zeroes,4-(skb->len & 3));
11431da177e4SLinus Torvalds 	/* JK for AAL5 trailer - AAL0 doesn't need it, but who cares ... */
11441da177e4SLinus Torvalds 	eni_dev->dma[j++] = (((tx->tx_pos+size) & (tx->words-1)) <<
11451da177e4SLinus Torvalds 	     MID_DMA_COUNT_SHIFT) | (tx->index << MID_DMA_CHAN_SHIFT) |
11461da177e4SLinus Torvalds 	     MID_DMA_END | MID_DT_JK;
11471da177e4SLinus Torvalds 	j++;
11481da177e4SLinus Torvalds 	DPRINTK("DMA at end: %d\n",j);
11491da177e4SLinus Torvalds 	/* store frame */
11501da177e4SLinus Torvalds 	writel((MID_SEG_TX_ID << MID_SEG_ID_SHIFT) |
11511da177e4SLinus Torvalds 	    (aal5 ? MID_SEG_AAL5 : 0) | (tx->prescaler << MID_SEG_PR_SHIFT) |
11521da177e4SLinus Torvalds 	    (tx->resolution << MID_SEG_RATE_SHIFT) |
11531da177e4SLinus Torvalds 	    (size/(ATM_CELL_PAYLOAD/4)),tx->send+tx->tx_pos*4);
11541da177e4SLinus Torvalds /*printk("dsc = 0x%08lx\n",(unsigned long) readl(tx->send+tx->tx_pos*4));*/
11551da177e4SLinus Torvalds 	writel((vcc->vci << MID_SEG_VCI_SHIFT) |
11561da177e4SLinus Torvalds             (aal5 ? 0 : (skb->data[3] & 0xf)) |
11571da177e4SLinus Torvalds 	    (ATM_SKB(skb)->atm_options & ATM_ATMOPT_CLP ? MID_SEG_CLP : 0),
11581da177e4SLinus Torvalds 	    tx->send+((tx->tx_pos+1) & (tx->words-1))*4);
11591da177e4SLinus Torvalds 	DPRINTK("size: %d, len:%d\n",size,skb->len);
11601da177e4SLinus Torvalds 	if (aal5)
11611da177e4SLinus Torvalds 		writel(skb->len,tx->send+
11621da177e4SLinus Torvalds                     ((tx->tx_pos+size-AAL5_TRAILER) & (tx->words-1))*4);
11631da177e4SLinus Torvalds 	j = j >> 1;
11641da177e4SLinus Torvalds 	for (i = 0; i < j; i++) {
11651da177e4SLinus Torvalds 		writel(eni_dev->dma[i*2],eni_dev->tx_dma+dma_wr*8);
11661da177e4SLinus Torvalds 		writel(eni_dev->dma[i*2+1],eni_dev->tx_dma+dma_wr*8+4);
11671da177e4SLinus Torvalds 		dma_wr = (dma_wr+1) & (NR_DMA_TX-1);
11681da177e4SLinus Torvalds 	}
11691da177e4SLinus Torvalds 	ENI_PRV_POS(skb) = tx->tx_pos;
11701da177e4SLinus Torvalds 	ENI_PRV_SIZE(skb) = size;
11711da177e4SLinus Torvalds 	ENI_VCC(vcc)->txing += size;
11721da177e4SLinus Torvalds 	tx->tx_pos = (tx->tx_pos+size) & (tx->words-1);
11731da177e4SLinus Torvalds 	DPRINTK("dma_wr set to %d, tx_pos is now %ld\n",dma_wr,tx->tx_pos);
11741da177e4SLinus Torvalds 	eni_out(dma_wr,MID_DMA_WR_TX);
11751da177e4SLinus Torvalds 	skb_queue_tail(&eni_dev->tx_queue,skb);
11761da177e4SLinus Torvalds queued++;
11771da177e4SLinus Torvalds 	return enq_ok;
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds static void poll_tx(struct atm_dev *dev)
11821da177e4SLinus Torvalds {
11831da177e4SLinus Torvalds 	struct eni_tx *tx;
11841da177e4SLinus Torvalds 	struct sk_buff *skb;
11851da177e4SLinus Torvalds 	enum enq_res res;
11861da177e4SLinus Torvalds 	int i;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	DPRINTK(">poll_tx\n");
11891da177e4SLinus Torvalds 	for (i = NR_CHAN-1; i >= 0; i--) {
11901da177e4SLinus Torvalds 		tx = &ENI_DEV(dev)->tx[i];
11911da177e4SLinus Torvalds 		if (tx->send)
11921da177e4SLinus Torvalds 			while ((skb = skb_dequeue(&tx->backlog))) {
11931da177e4SLinus Torvalds 				res = do_tx(skb);
11941da177e4SLinus Torvalds 				if (res == enq_ok) continue;
11951da177e4SLinus Torvalds 				DPRINTK("re-queuing TX PDU\n");
11961da177e4SLinus Torvalds 				skb_queue_head(&tx->backlog,skb);
11971da177e4SLinus Torvalds requeued++;
11981da177e4SLinus Torvalds 				if (res == enq_jam) return;
11991da177e4SLinus Torvalds 				break;
12001da177e4SLinus Torvalds 			}
12011da177e4SLinus Torvalds 	}
12021da177e4SLinus Torvalds }
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds static void dequeue_tx(struct atm_dev *dev)
12061da177e4SLinus Torvalds {
12071da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
12081da177e4SLinus Torvalds 	struct atm_vcc *vcc;
12091da177e4SLinus Torvalds 	struct sk_buff *skb;
12101da177e4SLinus Torvalds 	struct eni_tx *tx;
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	NULLCHECK(dev);
12131da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
12141da177e4SLinus Torvalds 	NULLCHECK(eni_dev);
12151da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&eni_dev->tx_queue))) {
12161da177e4SLinus Torvalds 		vcc = ATM_SKB(skb)->vcc;
12171da177e4SLinus Torvalds 		NULLCHECK(vcc);
12181da177e4SLinus Torvalds 		tx = ENI_VCC(vcc)->tx;
12191da177e4SLinus Torvalds 		NULLCHECK(ENI_VCC(vcc)->tx);
12201da177e4SLinus Torvalds 		DPRINTK("dequeue_tx: next 0x%lx curr 0x%x\n",ENI_PRV_POS(skb),
12211da177e4SLinus Torvalds 		    (unsigned) eni_in(MID_TX_DESCRSTART(tx->index)));
12221da177e4SLinus Torvalds 		if (ENI_VCC(vcc)->txing < tx->words && ENI_PRV_POS(skb) ==
12231da177e4SLinus Torvalds 		    eni_in(MID_TX_DESCRSTART(tx->index))) {
12241da177e4SLinus Torvalds 			skb_queue_head(&eni_dev->tx_queue,skb);
12251da177e4SLinus Torvalds 			break;
12261da177e4SLinus Torvalds 		}
12271da177e4SLinus Torvalds 		ENI_VCC(vcc)->txing -= ENI_PRV_SIZE(skb);
12281da177e4SLinus Torvalds 		pci_unmap_single(eni_dev->pci_dev,ENI_PRV_PADDR(skb),skb->len,
12291da177e4SLinus Torvalds 		    PCI_DMA_TODEVICE);
12301da177e4SLinus Torvalds 		if (vcc->pop) vcc->pop(vcc,skb);
12311da177e4SLinus Torvalds 		else dev_kfree_skb_irq(skb);
12321da177e4SLinus Torvalds 		atomic_inc(&vcc->stats->tx);
12331da177e4SLinus Torvalds 		wake_up(&eni_dev->tx_wait);
12341da177e4SLinus Torvalds dma_complete++;
12351da177e4SLinus Torvalds 	}
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds static struct eni_tx *alloc_tx(struct eni_dev *eni_dev,int ubr)
12401da177e4SLinus Torvalds {
12411da177e4SLinus Torvalds 	int i;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	for (i = !ubr; i < NR_CHAN; i++)
12441da177e4SLinus Torvalds 		if (!eni_dev->tx[i].send) return eni_dev->tx+i;
12451da177e4SLinus Torvalds 	return NULL;
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds static int comp_tx(struct eni_dev *eni_dev,int *pcr,int reserved,int *pre,
12501da177e4SLinus Torvalds     int *res,int unlimited)
12511da177e4SLinus Torvalds {
12521da177e4SLinus Torvalds 	static const int pre_div[] = { 4,16,128,2048 };
12531da177e4SLinus Torvalds 	    /* 2^(((x+2)^2-(x+2))/2+1) */
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	if (unlimited) *pre = *res = 0;
12561da177e4SLinus Torvalds 	else {
12571da177e4SLinus Torvalds 		if (*pcr > 0) {
12581da177e4SLinus Torvalds 			int div;
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 			for (*pre = 0; *pre < 3; (*pre)++)
12611da177e4SLinus Torvalds 				if (TS_CLOCK/pre_div[*pre]/64 <= *pcr) break;
12621da177e4SLinus Torvalds 			div = pre_div[*pre]**pcr;
12631da177e4SLinus Torvalds 			DPRINTK("min div %d\n",div);
12641da177e4SLinus Torvalds 			*res = TS_CLOCK/div-1;
12651da177e4SLinus Torvalds 		}
12661da177e4SLinus Torvalds 		else {
12671da177e4SLinus Torvalds 			int div;
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 			if (!*pcr) *pcr = eni_dev->tx_bw+reserved;
12701da177e4SLinus Torvalds 			for (*pre = 3; *pre >= 0; (*pre)--)
12711da177e4SLinus Torvalds 				if (TS_CLOCK/pre_div[*pre]/64 > -*pcr) break;
12721da177e4SLinus Torvalds 			if (*pre < 3) (*pre)++; /* else fail later */
12731da177e4SLinus Torvalds 			div = pre_div[*pre]*-*pcr;
12741da177e4SLinus Torvalds 			DPRINTK("max div %d\n",div);
12751da177e4SLinus Torvalds 			*res = (TS_CLOCK+div-1)/div-1;
12761da177e4SLinus Torvalds 		}
12771da177e4SLinus Torvalds 		if (*res < 0) *res = 0;
12781da177e4SLinus Torvalds 		if (*res > MID_SEG_MAX_RATE) *res = MID_SEG_MAX_RATE;
12791da177e4SLinus Torvalds 	}
12801da177e4SLinus Torvalds 	*pcr = TS_CLOCK/pre_div[*pre]/(*res+1);
12811da177e4SLinus Torvalds 	DPRINTK("out pcr: %d (%d:%d)\n",*pcr,*pre,*res);
12821da177e4SLinus Torvalds 	return 0;
12831da177e4SLinus Torvalds }
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds static int reserve_or_set_tx(struct atm_vcc *vcc,struct atm_trafprm *txtp,
12871da177e4SLinus Torvalds     int set_rsv,int set_shp)
12881da177e4SLinus Torvalds {
12891da177e4SLinus Torvalds 	struct eni_dev *eni_dev = ENI_DEV(vcc->dev);
12901da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc = ENI_VCC(vcc);
12911da177e4SLinus Torvalds 	struct eni_tx *tx;
12921da177e4SLinus Torvalds 	unsigned long size;
12931da177e4SLinus Torvalds 	void __iomem *mem;
12941da177e4SLinus Torvalds 	int rate,ubr,unlimited,new_tx;
12951da177e4SLinus Torvalds 	int pre,res,order;
12961da177e4SLinus Torvalds 	int error;
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 	rate = atm_pcr_goal(txtp);
12991da177e4SLinus Torvalds 	ubr = txtp->traffic_class == ATM_UBR;
13001da177e4SLinus Torvalds 	unlimited = ubr && (!rate || rate <= -ATM_OC3_PCR ||
13011da177e4SLinus Torvalds 	    rate >= ATM_OC3_PCR);
13021da177e4SLinus Torvalds 	if (!unlimited) {
13031da177e4SLinus Torvalds 		size = txtp->max_sdu*eni_dev->tx_mult/100;
13041da177e4SLinus Torvalds 		if (size > MID_MAX_BUF_SIZE && txtp->max_sdu <=
13051da177e4SLinus Torvalds 		    MID_MAX_BUF_SIZE)
13061da177e4SLinus Torvalds 			size = MID_MAX_BUF_SIZE;
13071da177e4SLinus Torvalds 	}
13081da177e4SLinus Torvalds 	else {
13091da177e4SLinus Torvalds 		if (eni_dev->ubr) {
13101da177e4SLinus Torvalds 			eni_vcc->tx = eni_dev->ubr;
13111da177e4SLinus Torvalds 			txtp->pcr = ATM_OC3_PCR;
13121da177e4SLinus Torvalds 			return 0;
13131da177e4SLinus Torvalds 		}
13141da177e4SLinus Torvalds 		size = UBR_BUFFER;
13151da177e4SLinus Torvalds 	}
13161da177e4SLinus Torvalds 	new_tx = !eni_vcc->tx;
13171da177e4SLinus Torvalds 	mem = NULL; /* for gcc */
13181da177e4SLinus Torvalds 	if (!new_tx) tx = eni_vcc->tx;
13191da177e4SLinus Torvalds 	else {
13201da177e4SLinus Torvalds 		mem = eni_alloc_mem(eni_dev,&size);
13211da177e4SLinus Torvalds 		if (!mem) return -ENOBUFS;
13221da177e4SLinus Torvalds 		tx = alloc_tx(eni_dev,unlimited);
13231da177e4SLinus Torvalds 		if (!tx) {
13241da177e4SLinus Torvalds 			eni_free_mem(eni_dev,mem,size);
13251da177e4SLinus Torvalds 			return -EBUSY;
13261da177e4SLinus Torvalds 		}
13271da177e4SLinus Torvalds 		DPRINTK("got chan %d\n",tx->index);
13281da177e4SLinus Torvalds 		tx->reserved = tx->shaping = 0;
13291da177e4SLinus Torvalds 		tx->send = mem;
13301da177e4SLinus Torvalds 		tx->words = size >> 2;
13311da177e4SLinus Torvalds 		skb_queue_head_init(&tx->backlog);
13321da177e4SLinus Torvalds 		for (order = 0; size > (1 << (order+10)); order++);
13331da177e4SLinus Torvalds 		eni_out((order << MID_SIZE_SHIFT) |
13341da177e4SLinus Torvalds 		    ((tx->send-eni_dev->ram) >> (MID_LOC_SKIP+2)),
13351da177e4SLinus Torvalds 		    MID_TX_PLACE(tx->index));
13361da177e4SLinus Torvalds 		tx->tx_pos = eni_in(MID_TX_DESCRSTART(tx->index)) &
13371da177e4SLinus Torvalds 		    MID_DESCR_START;
13381da177e4SLinus Torvalds 	}
13391da177e4SLinus Torvalds 	error = comp_tx(eni_dev,&rate,tx->reserved,&pre,&res,unlimited);
13401da177e4SLinus Torvalds 	if (!error  && txtp->min_pcr > rate) error = -EINVAL;
13411da177e4SLinus Torvalds 	if (!error && txtp->max_pcr && txtp->max_pcr != ATM_MAX_PCR &&
13421da177e4SLinus Torvalds 	    txtp->max_pcr < rate) error = -EINVAL;
13431da177e4SLinus Torvalds 	if (!error && !ubr && rate > eni_dev->tx_bw+tx->reserved)
13441da177e4SLinus Torvalds 		error = -EINVAL;
13451da177e4SLinus Torvalds 	if (!error && set_rsv && !set_shp && rate < tx->shaping)
13461da177e4SLinus Torvalds 		error = -EINVAL;
13471da177e4SLinus Torvalds 	if (!error && !set_rsv && rate > tx->reserved && !ubr)
13481da177e4SLinus Torvalds 		error = -EINVAL;
13491da177e4SLinus Torvalds 	if (error) {
13501da177e4SLinus Torvalds 		if (new_tx) {
13511da177e4SLinus Torvalds 			tx->send = NULL;
13521da177e4SLinus Torvalds 			eni_free_mem(eni_dev,mem,size);
13531da177e4SLinus Torvalds 		}
13541da177e4SLinus Torvalds 		return error;
13551da177e4SLinus Torvalds 	}
13561da177e4SLinus Torvalds 	txtp->pcr = rate;
13571da177e4SLinus Torvalds 	if (set_rsv && !ubr) {
13581da177e4SLinus Torvalds 		eni_dev->tx_bw += tx->reserved;
13591da177e4SLinus Torvalds 		tx->reserved = rate;
13601da177e4SLinus Torvalds 		eni_dev->tx_bw -= rate;
13611da177e4SLinus Torvalds 	}
13621da177e4SLinus Torvalds 	if (set_shp || (unlimited && new_tx)) {
13631da177e4SLinus Torvalds 		if (unlimited && new_tx) eni_dev->ubr = tx;
13641da177e4SLinus Torvalds 		tx->prescaler = pre;
13651da177e4SLinus Torvalds 		tx->resolution = res;
13661da177e4SLinus Torvalds 		tx->shaping = rate;
13671da177e4SLinus Torvalds 	}
13681da177e4SLinus Torvalds 	if (set_shp) eni_vcc->tx = tx;
13691da177e4SLinus Torvalds 	DPRINTK("rsv %d shp %d\n",tx->reserved,tx->shaping);
13701da177e4SLinus Torvalds 	return 0;
13711da177e4SLinus Torvalds }
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds static int open_tx_first(struct atm_vcc *vcc)
13751da177e4SLinus Torvalds {
13761da177e4SLinus Torvalds 	ENI_VCC(vcc)->tx = NULL;
13771da177e4SLinus Torvalds 	if (vcc->qos.txtp.traffic_class == ATM_NONE) return 0;
13781da177e4SLinus Torvalds 	ENI_VCC(vcc)->txing = 0;
13791da177e4SLinus Torvalds 	return reserve_or_set_tx(vcc,&vcc->qos.txtp,1,1);
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds static int open_tx_second(struct atm_vcc *vcc)
13841da177e4SLinus Torvalds {
13851da177e4SLinus Torvalds 	return 0; /* nothing to do */
13861da177e4SLinus Torvalds }
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds static void close_tx(struct atm_vcc *vcc)
13901da177e4SLinus Torvalds {
13911da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait,current);
13921da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
13931da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 	eni_vcc = ENI_VCC(vcc);
13961da177e4SLinus Torvalds 	if (!eni_vcc->tx) return;
13971da177e4SLinus Torvalds 	eni_dev = ENI_DEV(vcc->dev);
13981da177e4SLinus Torvalds 	/* wait for TX queue to drain */
13991da177e4SLinus Torvalds 	DPRINTK("eni_close: waiting for TX ...\n");
14001da177e4SLinus Torvalds 	add_wait_queue(&eni_dev->tx_wait,&wait);
14011da177e4SLinus Torvalds 	set_current_state(TASK_UNINTERRUPTIBLE);
14021da177e4SLinus Torvalds 	for (;;) {
14031da177e4SLinus Torvalds 		int txing;
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 		tasklet_disable(&eni_dev->task);
14061da177e4SLinus Torvalds 		txing = skb_peek(&eni_vcc->tx->backlog) || eni_vcc->txing;
14071da177e4SLinus Torvalds 		tasklet_enable(&eni_dev->task);
14081da177e4SLinus Torvalds 		if (!txing) break;
14091da177e4SLinus Torvalds 		DPRINTK("%d TX left\n",eni_vcc->txing);
14101da177e4SLinus Torvalds 		schedule();
14111da177e4SLinus Torvalds 		set_current_state(TASK_UNINTERRUPTIBLE);
14121da177e4SLinus Torvalds 	}
14131da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
14141da177e4SLinus Torvalds 	remove_wait_queue(&eni_dev->tx_wait,&wait);
14151da177e4SLinus Torvalds 	if (eni_vcc->tx != eni_dev->ubr) {
14161da177e4SLinus Torvalds 		/*
14171da177e4SLinus Torvalds 		 * Looping a few times in here is probably far cheaper than
14181da177e4SLinus Torvalds 		 * keeping track of TX completions all the time, so let's poll
14191da177e4SLinus Torvalds 		 * a bit ...
14201da177e4SLinus Torvalds 		 */
14211da177e4SLinus Torvalds 		while (eni_in(MID_TX_RDPTR(eni_vcc->tx->index)) !=
14221da177e4SLinus Torvalds 		    eni_in(MID_TX_DESCRSTART(eni_vcc->tx->index)))
14231da177e4SLinus Torvalds 			schedule();
14241da177e4SLinus Torvalds 		eni_free_mem(eni_dev,eni_vcc->tx->send,eni_vcc->tx->words << 2);
14251da177e4SLinus Torvalds 		eni_vcc->tx->send = NULL;
14261da177e4SLinus Torvalds 		eni_dev->tx_bw += eni_vcc->tx->reserved;
14271da177e4SLinus Torvalds 	}
14281da177e4SLinus Torvalds 	eni_vcc->tx = NULL;
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds static int start_tx(struct atm_dev *dev)
14331da177e4SLinus Torvalds {
14341da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
14351da177e4SLinus Torvalds 	int i;
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
14381da177e4SLinus Torvalds 	eni_dev->lost = 0;
14391da177e4SLinus Torvalds 	eni_dev->tx_bw = ATM_OC3_PCR;
14401da177e4SLinus Torvalds 	eni_dev->tx_mult = DEFAULT_TX_MULT;
14411da177e4SLinus Torvalds 	init_waitqueue_head(&eni_dev->tx_wait);
14421da177e4SLinus Torvalds 	eni_dev->ubr = NULL;
14431da177e4SLinus Torvalds 	skb_queue_head_init(&eni_dev->tx_queue);
14441da177e4SLinus Torvalds 	eni_out(0,MID_DMA_WR_TX);
14451da177e4SLinus Torvalds 	for (i = 0; i < NR_CHAN; i++) {
14461da177e4SLinus Torvalds 		eni_dev->tx[i].send = NULL;
14471da177e4SLinus Torvalds 		eni_dev->tx[i].index = i;
14481da177e4SLinus Torvalds 	}
14491da177e4SLinus Torvalds 	return 0;
14501da177e4SLinus Torvalds }
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds /*--------------------------------- common ----------------------------------*/
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds #if 0 /* may become useful again when tuning things */
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds static void foo(void)
14591da177e4SLinus Torvalds {
14601da177e4SLinus Torvalds printk(KERN_INFO
14611da177e4SLinus Torvalds   "tx_complete=%d,dma_complete=%d,queued=%d,requeued=%d,sub=%d,\n"
14621da177e4SLinus Torvalds   "backlogged=%d,rx_enqueued=%d,rx_dequeued=%d,putting=%d,pushed=%d\n",
14631da177e4SLinus Torvalds   tx_complete,dma_complete,queued,requeued,submitted,backlogged,
14641da177e4SLinus Torvalds   rx_enqueued,rx_dequeued,putting,pushed);
14651da177e4SLinus Torvalds if (eni_boards) printk(KERN_INFO "loss: %ld\n",ENI_DEV(eni_boards)->lost);
14661da177e4SLinus Torvalds }
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds #endif
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds static void bug_int(struct atm_dev *dev,unsigned long reason)
14721da177e4SLinus Torvalds {
14731da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 	DPRINTK(">bug_int\n");
14761da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
14771da177e4SLinus Torvalds 	if (reason & MID_DMA_ERR_ACK)
14781da177e4SLinus Torvalds 		printk(KERN_CRIT DEV_LABEL "(itf %d): driver error - DMA "
14791da177e4SLinus Torvalds 		    "error\n",dev->number);
14801da177e4SLinus Torvalds 	if (reason & MID_TX_IDENT_MISM)
14811da177e4SLinus Torvalds 		printk(KERN_CRIT DEV_LABEL "(itf %d): driver error - ident "
14821da177e4SLinus Torvalds 		    "mismatch\n",dev->number);
14831da177e4SLinus Torvalds 	if (reason & MID_TX_DMA_OVFL)
14841da177e4SLinus Torvalds 		printk(KERN_CRIT DEV_LABEL "(itf %d): driver error - DMA "
14851da177e4SLinus Torvalds 		    "overflow\n",dev->number);
14861da177e4SLinus Torvalds 	EVENT("---dump ends here---\n",0,0);
14871da177e4SLinus Torvalds 	printk(KERN_NOTICE "---recent events---\n");
14881da177e4SLinus Torvalds 	event_dump();
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds static irqreturn_t eni_int(int irq,void *dev_id,struct pt_regs *regs)
14931da177e4SLinus Torvalds {
14941da177e4SLinus Torvalds 	struct atm_dev *dev;
14951da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
14961da177e4SLinus Torvalds 	u32 reason;
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	DPRINTK(">eni_int\n");
14991da177e4SLinus Torvalds 	dev = dev_id;
15001da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
15011da177e4SLinus Torvalds 	reason = eni_in(MID_ISA);
15021da177e4SLinus Torvalds 	DPRINTK(DEV_LABEL ": int 0x%lx\n",(unsigned long) reason);
15031da177e4SLinus Torvalds 	/*
15041da177e4SLinus Torvalds 	 * Must handle these two right now, because reading ISA doesn't clear
15051da177e4SLinus Torvalds 	 * them, so they re-occur and we never make it to the tasklet. Since
15061da177e4SLinus Torvalds 	 * they're rare, we don't mind the occasional invocation of eni_tasklet
15071da177e4SLinus Torvalds 	 * with eni_dev->events == 0.
15081da177e4SLinus Torvalds 	 */
15091da177e4SLinus Torvalds 	if (reason & MID_STAT_OVFL) {
15101da177e4SLinus Torvalds 		EVENT("stat overflow\n",0,0);
15111da177e4SLinus Torvalds 		eni_dev->lost += eni_in(MID_STAT) & MID_OVFL_TRASH;
15121da177e4SLinus Torvalds 	}
15131da177e4SLinus Torvalds 	if (reason & MID_SUNI_INT) {
15141da177e4SLinus Torvalds 		EVENT("SUNI int\n",0,0);
15151da177e4SLinus Torvalds 		dev->phy->interrupt(dev);
15161da177e4SLinus Torvalds #if 0
15171da177e4SLinus Torvalds 		foo();
15181da177e4SLinus Torvalds #endif
15191da177e4SLinus Torvalds 	}
15201da177e4SLinus Torvalds 	spin_lock(&eni_dev->lock);
15211da177e4SLinus Torvalds 	eni_dev->events |= reason;
15221da177e4SLinus Torvalds 	spin_unlock(&eni_dev->lock);
15231da177e4SLinus Torvalds 	tasklet_schedule(&eni_dev->task);
15241da177e4SLinus Torvalds 	return IRQ_HANDLED;
15251da177e4SLinus Torvalds }
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds static void eni_tasklet(unsigned long data)
15291da177e4SLinus Torvalds {
15301da177e4SLinus Torvalds 	struct atm_dev *dev = (struct atm_dev *) data;
15311da177e4SLinus Torvalds 	struct eni_dev *eni_dev = ENI_DEV(dev);
15321da177e4SLinus Torvalds 	unsigned long flags;
15331da177e4SLinus Torvalds 	u32 events;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 	DPRINTK("eni_tasklet (dev %p)\n",dev);
15361da177e4SLinus Torvalds 	spin_lock_irqsave(&eni_dev->lock,flags);
15371da177e4SLinus Torvalds 	events = xchg(&eni_dev->events,0);
15381da177e4SLinus Torvalds 	spin_unlock_irqrestore(&eni_dev->lock,flags);
15391da177e4SLinus Torvalds 	if (events & MID_RX_DMA_COMPLETE) {
15401da177e4SLinus Torvalds 		EVENT("INT: RX DMA complete, starting dequeue_rx\n",0,0);
15411da177e4SLinus Torvalds 		dequeue_rx(dev);
15421da177e4SLinus Torvalds 		EVENT("dequeue_rx done, starting poll_rx\n",0,0);
15431da177e4SLinus Torvalds 		poll_rx(dev);
15441da177e4SLinus Torvalds 		EVENT("poll_rx done\n",0,0);
15451da177e4SLinus Torvalds 		/* poll_tx ? */
15461da177e4SLinus Torvalds 	}
15471da177e4SLinus Torvalds 	if (events & MID_SERVICE) {
15481da177e4SLinus Torvalds 		EVENT("INT: service, starting get_service\n",0,0);
15491da177e4SLinus Torvalds 		get_service(dev);
15501da177e4SLinus Torvalds 		EVENT("get_service done, starting poll_rx\n",0,0);
15511da177e4SLinus Torvalds 		poll_rx(dev);
15521da177e4SLinus Torvalds 		EVENT("poll_rx done\n",0,0);
15531da177e4SLinus Torvalds 	}
15541da177e4SLinus Torvalds  	if (events & MID_TX_DMA_COMPLETE) {
15551da177e4SLinus Torvalds 		EVENT("INT: TX DMA COMPLETE\n",0,0);
15561da177e4SLinus Torvalds 		dequeue_tx(dev);
15571da177e4SLinus Torvalds 	}
15581da177e4SLinus Torvalds 	if (events & MID_TX_COMPLETE) {
15591da177e4SLinus Torvalds 		EVENT("INT: TX COMPLETE\n",0,0);
15601da177e4SLinus Torvalds tx_complete++;
15611da177e4SLinus Torvalds 		wake_up(&eni_dev->tx_wait);
15621da177e4SLinus Torvalds 		/* poll_rx ? */
15631da177e4SLinus Torvalds 	}
15641da177e4SLinus Torvalds 	if (events & (MID_DMA_ERR_ACK | MID_TX_IDENT_MISM | MID_TX_DMA_OVFL)) {
15651da177e4SLinus Torvalds 		EVENT("bug interrupt\n",0,0);
15661da177e4SLinus Torvalds 		bug_int(dev,events);
15671da177e4SLinus Torvalds 	}
15681da177e4SLinus Torvalds 	poll_tx(dev);
15691da177e4SLinus Torvalds }
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds /*--------------------------------- entries ---------------------------------*/
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds static const char *media_name[] __devinitdata = {
15761da177e4SLinus Torvalds     "MMF", "SMF", "MMF", "03?", /*  0- 3 */
15771da177e4SLinus Torvalds     "UTP", "05?", "06?", "07?", /*  4- 7 */
15781da177e4SLinus Torvalds     "TAXI","09?", "10?", "11?", /*  8-11 */
15791da177e4SLinus Torvalds     "12?", "13?", "14?", "15?", /* 12-15 */
15801da177e4SLinus Torvalds     "MMF", "SMF", "18?", "19?", /* 16-19 */
15811da177e4SLinus Torvalds     "UTP", "21?", "22?", "23?", /* 20-23 */
15821da177e4SLinus Torvalds     "24?", "25?", "26?", "27?", /* 24-27 */
15831da177e4SLinus Torvalds     "28?", "29?", "30?", "31?"  /* 28-31 */
15841da177e4SLinus Torvalds };
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds #define SET_SEPROM \
15881da177e4SLinus Torvalds   ({ if (!error && !pci_error) { \
15891da177e4SLinus Torvalds     pci_error = pci_write_config_byte(eni_dev->pci_dev,PCI_TONGA_CTRL,tonga); \
15901da177e4SLinus Torvalds     udelay(10); /* 10 usecs */ \
15911da177e4SLinus Torvalds   } })
15921da177e4SLinus Torvalds #define GET_SEPROM \
15931da177e4SLinus Torvalds   ({ if (!error && !pci_error) { \
15941da177e4SLinus Torvalds     pci_error = pci_read_config_byte(eni_dev->pci_dev,PCI_TONGA_CTRL,&tonga); \
15951da177e4SLinus Torvalds     udelay(10); /* 10 usecs */ \
15961da177e4SLinus Torvalds   } })
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds static int __devinit get_esi_asic(struct atm_dev *dev)
16001da177e4SLinus Torvalds {
16011da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
16021da177e4SLinus Torvalds 	unsigned char tonga;
16031da177e4SLinus Torvalds 	int error,failed,pci_error;
16041da177e4SLinus Torvalds 	int address,i,j;
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
16071da177e4SLinus Torvalds 	error = pci_error = 0;
16081da177e4SLinus Torvalds 	tonga = SEPROM_MAGIC | SEPROM_DATA | SEPROM_CLK;
16091da177e4SLinus Torvalds 	SET_SEPROM;
16101da177e4SLinus Torvalds 	for (i = 0; i < ESI_LEN && !error && !pci_error; i++) {
16111da177e4SLinus Torvalds 		/* start operation */
16121da177e4SLinus Torvalds 		tonga |= SEPROM_DATA;
16131da177e4SLinus Torvalds 		SET_SEPROM;
16141da177e4SLinus Torvalds 		tonga |= SEPROM_CLK;
16151da177e4SLinus Torvalds 		SET_SEPROM;
16161da177e4SLinus Torvalds 		tonga &= ~SEPROM_DATA;
16171da177e4SLinus Torvalds 		SET_SEPROM;
16181da177e4SLinus Torvalds 		tonga &= ~SEPROM_CLK;
16191da177e4SLinus Torvalds 		SET_SEPROM;
16201da177e4SLinus Torvalds 		/* send address */
16211da177e4SLinus Torvalds 		address = ((i+SEPROM_ESI_BASE) << 1)+1;
16221da177e4SLinus Torvalds 		for (j = 7; j >= 0; j--) {
16231da177e4SLinus Torvalds 			tonga = (address >> j) & 1 ? tonga | SEPROM_DATA :
16241da177e4SLinus Torvalds 			    tonga & ~SEPROM_DATA;
16251da177e4SLinus Torvalds 			SET_SEPROM;
16261da177e4SLinus Torvalds 			tonga |= SEPROM_CLK;
16271da177e4SLinus Torvalds 			SET_SEPROM;
16281da177e4SLinus Torvalds 			tonga &= ~SEPROM_CLK;
16291da177e4SLinus Torvalds 			SET_SEPROM;
16301da177e4SLinus Torvalds 		}
16311da177e4SLinus Torvalds 		/* get ack */
16321da177e4SLinus Torvalds 		tonga |= SEPROM_DATA;
16331da177e4SLinus Torvalds 		SET_SEPROM;
16341da177e4SLinus Torvalds 		tonga |= SEPROM_CLK;
16351da177e4SLinus Torvalds 		SET_SEPROM;
16361da177e4SLinus Torvalds 		GET_SEPROM;
16371da177e4SLinus Torvalds 		failed = tonga & SEPROM_DATA;
16381da177e4SLinus Torvalds 		tonga &= ~SEPROM_CLK;
16391da177e4SLinus Torvalds 		SET_SEPROM;
16401da177e4SLinus Torvalds 		tonga |= SEPROM_DATA;
16411da177e4SLinus Torvalds 		SET_SEPROM;
16421da177e4SLinus Torvalds 		if (failed) error = -EIO;
16431da177e4SLinus Torvalds 		else {
16441da177e4SLinus Torvalds 			dev->esi[i] = 0;
16451da177e4SLinus Torvalds 			for (j = 7; j >= 0; j--) {
16461da177e4SLinus Torvalds 				dev->esi[i] <<= 1;
16471da177e4SLinus Torvalds 				tonga |= SEPROM_DATA;
16481da177e4SLinus Torvalds 				SET_SEPROM;
16491da177e4SLinus Torvalds 				tonga |= SEPROM_CLK;
16501da177e4SLinus Torvalds 				SET_SEPROM;
16511da177e4SLinus Torvalds 				GET_SEPROM;
16521da177e4SLinus Torvalds 				if (tonga & SEPROM_DATA) dev->esi[i] |= 1;
16531da177e4SLinus Torvalds 				tonga &= ~SEPROM_CLK;
16541da177e4SLinus Torvalds 				SET_SEPROM;
16551da177e4SLinus Torvalds 				tonga |= SEPROM_DATA;
16561da177e4SLinus Torvalds 				SET_SEPROM;
16571da177e4SLinus Torvalds 			}
16581da177e4SLinus Torvalds 			/* get ack */
16591da177e4SLinus Torvalds 			tonga |= SEPROM_DATA;
16601da177e4SLinus Torvalds 			SET_SEPROM;
16611da177e4SLinus Torvalds 			tonga |= SEPROM_CLK;
16621da177e4SLinus Torvalds 			SET_SEPROM;
16631da177e4SLinus Torvalds 			GET_SEPROM;
16641da177e4SLinus Torvalds 			if (!(tonga & SEPROM_DATA)) error = -EIO;
16651da177e4SLinus Torvalds 			tonga &= ~SEPROM_CLK;
16661da177e4SLinus Torvalds 			SET_SEPROM;
16671da177e4SLinus Torvalds 			tonga |= SEPROM_DATA;
16681da177e4SLinus Torvalds 			SET_SEPROM;
16691da177e4SLinus Torvalds 		}
16701da177e4SLinus Torvalds 		/* stop operation */
16711da177e4SLinus Torvalds 		tonga &= ~SEPROM_DATA;
16721da177e4SLinus Torvalds 		SET_SEPROM;
16731da177e4SLinus Torvalds 		tonga |= SEPROM_CLK;
16741da177e4SLinus Torvalds 		SET_SEPROM;
16751da177e4SLinus Torvalds 		tonga |= SEPROM_DATA;
16761da177e4SLinus Torvalds 		SET_SEPROM;
16771da177e4SLinus Torvalds 	}
16781da177e4SLinus Torvalds 	if (pci_error) {
16791da177e4SLinus Torvalds 		printk(KERN_ERR DEV_LABEL "(itf %d): error reading ESI "
16801da177e4SLinus Torvalds 		    "(0x%02x)\n",dev->number,pci_error);
16811da177e4SLinus Torvalds 		error = -EIO;
16821da177e4SLinus Torvalds 	}
16831da177e4SLinus Torvalds 	return error;
16841da177e4SLinus Torvalds }
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds #undef SET_SEPROM
16881da177e4SLinus Torvalds #undef GET_SEPROM
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds static int __devinit get_esi_fpga(struct atm_dev *dev, void __iomem *base)
16921da177e4SLinus Torvalds {
16931da177e4SLinus Torvalds 	void __iomem *mac_base;
16941da177e4SLinus Torvalds 	int i;
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 	mac_base = base+EPROM_SIZE-sizeof(struct midway_eprom);
16971da177e4SLinus Torvalds 	for (i = 0; i < ESI_LEN; i++) dev->esi[i] = readb(mac_base+(i^3));
16981da177e4SLinus Torvalds 	return 0;
16991da177e4SLinus Torvalds }
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds static int __devinit eni_do_init(struct atm_dev *dev)
17031da177e4SLinus Torvalds {
17041da177e4SLinus Torvalds 	struct midway_eprom __iomem *eprom;
17051da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
17061da177e4SLinus Torvalds 	struct pci_dev *pci_dev;
17071da177e4SLinus Torvalds 	unsigned long real_base;
17081da177e4SLinus Torvalds 	void __iomem *base;
17091da177e4SLinus Torvalds 	unsigned char revision;
17101da177e4SLinus Torvalds 	int error,i,last;
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	DPRINTK(">eni_init\n");
17131da177e4SLinus Torvalds 	dev->ci_range.vpi_bits = 0;
17141da177e4SLinus Torvalds 	dev->ci_range.vci_bits = NR_VCI_LD;
17151da177e4SLinus Torvalds 	dev->link_rate = ATM_OC3_PCR;
17161da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
17171da177e4SLinus Torvalds 	pci_dev = eni_dev->pci_dev;
17181da177e4SLinus Torvalds 	real_base = pci_resource_start(pci_dev, 0);
17191da177e4SLinus Torvalds 	eni_dev->irq = pci_dev->irq;
17201da177e4SLinus Torvalds 	error = pci_read_config_byte(pci_dev,PCI_REVISION_ID,&revision);
17211da177e4SLinus Torvalds 	if (error) {
17221da177e4SLinus Torvalds 		printk(KERN_ERR DEV_LABEL "(itf %d): init error 0x%02x\n",
17231da177e4SLinus Torvalds 		    dev->number,error);
17241da177e4SLinus Torvalds 		return -EINVAL;
17251da177e4SLinus Torvalds 	}
17261da177e4SLinus Torvalds 	if ((error = pci_write_config_word(pci_dev,PCI_COMMAND,
17271da177e4SLinus Torvalds 	    PCI_COMMAND_MEMORY |
17281da177e4SLinus Torvalds 	    (eni_dev->asic ? PCI_COMMAND_PARITY | PCI_COMMAND_SERR : 0)))) {
17291da177e4SLinus Torvalds 		printk(KERN_ERR DEV_LABEL "(itf %d): can't enable memory "
17301da177e4SLinus Torvalds 		    "(0x%02x)\n",dev->number,error);
17311da177e4SLinus Torvalds 		return -EIO;
17321da177e4SLinus Torvalds 	}
17331da177e4SLinus Torvalds 	printk(KERN_NOTICE DEV_LABEL "(itf %d): rev.%d,base=0x%lx,irq=%d,",
17341da177e4SLinus Torvalds 	    dev->number,revision,real_base,eni_dev->irq);
17351da177e4SLinus Torvalds 	if (!(base = ioremap_nocache(real_base,MAP_MAX_SIZE))) {
17361da177e4SLinus Torvalds 		printk("\n");
17371da177e4SLinus Torvalds 		printk(KERN_ERR DEV_LABEL "(itf %d): can't set up page "
17381da177e4SLinus Torvalds 		    "mapping\n",dev->number);
17391da177e4SLinus Torvalds 		return error;
17401da177e4SLinus Torvalds 	}
17411da177e4SLinus Torvalds 	eni_dev->base_diff = real_base - (unsigned long) base;
17421da177e4SLinus Torvalds 	/* id may not be present in ASIC Tonga boards - check this @@@ */
17431da177e4SLinus Torvalds 	if (!eni_dev->asic) {
17441da177e4SLinus Torvalds 		eprom = (base+EPROM_SIZE-sizeof(struct midway_eprom));
17451da177e4SLinus Torvalds 		if (readl(&eprom->magic) != ENI155_MAGIC) {
17461da177e4SLinus Torvalds 			printk("\n");
17471da177e4SLinus Torvalds 			printk(KERN_ERR KERN_ERR DEV_LABEL "(itf %d): bad "
17481da177e4SLinus Torvalds 			    "magic - expected 0x%x, got 0x%x\n",dev->number,
17491da177e4SLinus Torvalds 			    ENI155_MAGIC,(unsigned) readl(&eprom->magic));
17501da177e4SLinus Torvalds 			return -EINVAL;
17511da177e4SLinus Torvalds 		}
17521da177e4SLinus Torvalds 	}
17531da177e4SLinus Torvalds 	eni_dev->phy = base+PHY_BASE;
17541da177e4SLinus Torvalds 	eni_dev->reg = base+REG_BASE;
17551da177e4SLinus Torvalds 	eni_dev->ram = base+RAM_BASE;
17561da177e4SLinus Torvalds 	last = MAP_MAX_SIZE-RAM_BASE;
17571da177e4SLinus Torvalds 	for (i = last-RAM_INCREMENT; i >= 0; i -= RAM_INCREMENT) {
17581da177e4SLinus Torvalds 		writel(0x55555555,eni_dev->ram+i);
17591da177e4SLinus Torvalds 		if (readl(eni_dev->ram+i) != 0x55555555) last = i;
17601da177e4SLinus Torvalds 		else {
17611da177e4SLinus Torvalds 			writel(0xAAAAAAAA,eni_dev->ram+i);
17621da177e4SLinus Torvalds 			if (readl(eni_dev->ram+i) != 0xAAAAAAAA) last = i;
17631da177e4SLinus Torvalds 			else writel(i,eni_dev->ram+i);
17641da177e4SLinus Torvalds 		}
17651da177e4SLinus Torvalds 	}
17661da177e4SLinus Torvalds 	for (i = 0; i < last; i += RAM_INCREMENT)
17671da177e4SLinus Torvalds 		if (readl(eni_dev->ram+i) != i) break;
17681da177e4SLinus Torvalds 	eni_dev->mem = i;
17691da177e4SLinus Torvalds 	memset_io(eni_dev->ram,0,eni_dev->mem);
17701da177e4SLinus Torvalds 	/* TODO: should shrink allocation now */
17711da177e4SLinus Torvalds 	printk("mem=%dkB (",eni_dev->mem >> 10);
17721da177e4SLinus Torvalds 	/* TODO: check for non-SUNI, check for TAXI ? */
17731da177e4SLinus Torvalds 	if (!(eni_in(MID_RES_ID_MCON) & 0x200) != !eni_dev->asic) {
17741da177e4SLinus Torvalds 		printk(")\n");
17751da177e4SLinus Torvalds 		printk(KERN_ERR DEV_LABEL "(itf %d): ERROR - wrong id 0x%x\n",
17761da177e4SLinus Torvalds 		    dev->number,(unsigned) eni_in(MID_RES_ID_MCON));
17771da177e4SLinus Torvalds 		return -EINVAL;
17781da177e4SLinus Torvalds 	}
17791da177e4SLinus Torvalds 	error = eni_dev->asic ? get_esi_asic(dev) : get_esi_fpga(dev,base);
17801da177e4SLinus Torvalds 	if (error) return error;
17811da177e4SLinus Torvalds 	for (i = 0; i < ESI_LEN; i++)
17821da177e4SLinus Torvalds 		printk("%s%02X",i ? "-" : "",dev->esi[i]);
17831da177e4SLinus Torvalds 	printk(")\n");
17841da177e4SLinus Torvalds 	printk(KERN_NOTICE DEV_LABEL "(itf %d): %s,%s\n",dev->number,
17851da177e4SLinus Torvalds 	    eni_in(MID_RES_ID_MCON) & 0x200 ? "ASIC" : "FPGA",
17861da177e4SLinus Torvalds 	    media_name[eni_in(MID_RES_ID_MCON) & DAUGTHER_ID]);
17871da177e4SLinus Torvalds 	return suni_init(dev);
17881da177e4SLinus Torvalds }
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds static int __devinit eni_start(struct atm_dev *dev)
17921da177e4SLinus Torvalds {
17931da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	void __iomem *buf;
17961da177e4SLinus Torvalds 	unsigned long buffer_mem;
17971da177e4SLinus Torvalds 	int error;
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 	DPRINTK(">eni_start\n");
18001da177e4SLinus Torvalds 	eni_dev = ENI_DEV(dev);
18011da177e4SLinus Torvalds 	if (request_irq(eni_dev->irq,&eni_int,SA_SHIRQ,DEV_LABEL,dev)) {
18021da177e4SLinus Torvalds 		printk(KERN_ERR DEV_LABEL "(itf %d): IRQ%d is already in use\n",
18031da177e4SLinus Torvalds 		    dev->number,eni_dev->irq);
1804758d11bfSAndrew Morton 		error = -EAGAIN;
1805758d11bfSAndrew Morton 		goto out;
18061da177e4SLinus Torvalds 	}
18071da177e4SLinus Torvalds 	pci_set_master(eni_dev->pci_dev);
18081da177e4SLinus Torvalds 	if ((error = pci_write_config_word(eni_dev->pci_dev,PCI_COMMAND,
18091da177e4SLinus Torvalds 	    PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
18101da177e4SLinus Torvalds 	    (eni_dev->asic ? PCI_COMMAND_PARITY | PCI_COMMAND_SERR : 0)))) {
18111da177e4SLinus Torvalds 		printk(KERN_ERR DEV_LABEL "(itf %d): can't enable memory+"
18121da177e4SLinus Torvalds 		    "master (0x%02x)\n",dev->number,error);
1813758d11bfSAndrew Morton 		goto free_irq;
18141da177e4SLinus Torvalds 	}
18151da177e4SLinus Torvalds 	if ((error = pci_write_config_byte(eni_dev->pci_dev,PCI_TONGA_CTRL,
18161da177e4SLinus Torvalds 	    END_SWAP_DMA))) {
18171da177e4SLinus Torvalds 		printk(KERN_ERR DEV_LABEL "(itf %d): can't set endian swap "
18181da177e4SLinus Torvalds 		    "(0x%02x)\n",dev->number,error);
1819758d11bfSAndrew Morton 		goto free_irq;
18201da177e4SLinus Torvalds 	}
18211da177e4SLinus Torvalds 	/* determine addresses of internal tables */
18221da177e4SLinus Torvalds 	eni_dev->vci = eni_dev->ram;
18231da177e4SLinus Torvalds 	eni_dev->rx_dma = eni_dev->ram+NR_VCI*16;
18241da177e4SLinus Torvalds 	eni_dev->tx_dma = eni_dev->rx_dma+NR_DMA_RX*8;
18251da177e4SLinus Torvalds 	eni_dev->service = eni_dev->tx_dma+NR_DMA_TX*8;
18261da177e4SLinus Torvalds 	buf = eni_dev->service+NR_SERVICE*4;
18271da177e4SLinus Torvalds 	DPRINTK("vci 0x%lx,rx 0x%lx, tx 0x%lx,srv 0x%lx,buf 0x%lx\n",
18281da177e4SLinus Torvalds 	     eni_dev->vci,eni_dev->rx_dma,eni_dev->tx_dma,
18291da177e4SLinus Torvalds 	     eni_dev->service,buf);
18301da177e4SLinus Torvalds 	spin_lock_init(&eni_dev->lock);
18311da177e4SLinus Torvalds 	tasklet_init(&eni_dev->task,eni_tasklet,(unsigned long) dev);
18321da177e4SLinus Torvalds 	eni_dev->events = 0;
18331da177e4SLinus Torvalds 	/* initialize memory management */
18341da177e4SLinus Torvalds 	buffer_mem = eni_dev->mem - (buf - eni_dev->ram);
18351da177e4SLinus Torvalds 	eni_dev->free_list_size = buffer_mem/MID_MIN_BUF_SIZE/2;
18361da177e4SLinus Torvalds 	eni_dev->free_list = (struct eni_free *) kmalloc(
18371da177e4SLinus Torvalds 	    sizeof(struct eni_free)*(eni_dev->free_list_size+1),GFP_KERNEL);
18381da177e4SLinus Torvalds 	if (!eni_dev->free_list) {
18391da177e4SLinus Torvalds 		printk(KERN_ERR DEV_LABEL "(itf %d): couldn't get free page\n",
18401da177e4SLinus Torvalds 		    dev->number);
1841758d11bfSAndrew Morton 		error = -ENOMEM;
1842758d11bfSAndrew Morton 		goto free_irq;
18431da177e4SLinus Torvalds 	}
18441da177e4SLinus Torvalds 	eni_dev->free_len = 0;
18451da177e4SLinus Torvalds 	eni_put_free(eni_dev,buf,buffer_mem);
18461da177e4SLinus Torvalds 	memset_io(eni_dev->vci,0,16*NR_VCI); /* clear VCI table */
18471da177e4SLinus Torvalds 	/*
18481da177e4SLinus Torvalds 	 * byte_addr  free (k)
18491da177e4SLinus Torvalds 	 * 0x00000000     512  VCI table
18501da177e4SLinus Torvalds 	 * 0x00004000	  496  RX DMA
18511da177e4SLinus Torvalds 	 * 0x00005000	  492  TX DMA
18521da177e4SLinus Torvalds 	 * 0x00006000	  488  service list
18531da177e4SLinus Torvalds 	 * 0x00007000	  484  buffers
18541da177e4SLinus Torvalds 	 * 0x00080000	    0  end (512kB)
18551da177e4SLinus Torvalds 	 */
18561da177e4SLinus Torvalds 	eni_out(0xffffffff,MID_IE);
18571da177e4SLinus Torvalds 	error = start_tx(dev);
1858758d11bfSAndrew Morton 	if (error) goto free_list;
18591da177e4SLinus Torvalds 	error = start_rx(dev);
1860758d11bfSAndrew Morton 	if (error) goto free_list;
18611da177e4SLinus Torvalds 	error = dev->phy->start(dev);
1862758d11bfSAndrew Morton 	if (error) goto free_list;
18631da177e4SLinus Torvalds 	eni_out(eni_in(MID_MC_S) | (1 << MID_INT_SEL_SHIFT) |
18641da177e4SLinus Torvalds 	    MID_TX_LOCK_MODE | MID_DMA_ENABLE | MID_TX_ENABLE | MID_RX_ENABLE,
18651da177e4SLinus Torvalds 	    MID_MC_S);
18661da177e4SLinus Torvalds 	    /* Tonga uses SBus INTReq1 */
18671da177e4SLinus Torvalds 	(void) eni_in(MID_ISA); /* clear Midway interrupts */
18681da177e4SLinus Torvalds 	return 0;
1869758d11bfSAndrew Morton 
1870758d11bfSAndrew Morton free_list:
1871758d11bfSAndrew Morton 	kfree(eni_dev->free_list);
1872758d11bfSAndrew Morton 
1873758d11bfSAndrew Morton free_irq:
1874758d11bfSAndrew Morton 	free_irq(eni_dev->irq, eni_dev);
1875758d11bfSAndrew Morton 
1876758d11bfSAndrew Morton out:
1877758d11bfSAndrew Morton 	return error;
18781da177e4SLinus Torvalds }
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds static void eni_close(struct atm_vcc *vcc)
18821da177e4SLinus Torvalds {
18831da177e4SLinus Torvalds 	DPRINTK(">eni_close\n");
18841da177e4SLinus Torvalds 	if (!ENI_VCC(vcc)) return;
18851da177e4SLinus Torvalds 	clear_bit(ATM_VF_READY,&vcc->flags);
18861da177e4SLinus Torvalds 	close_rx(vcc);
18871da177e4SLinus Torvalds 	close_tx(vcc);
18881da177e4SLinus Torvalds 	DPRINTK("eni_close: done waiting\n");
18891da177e4SLinus Torvalds 	/* deallocate memory */
18901da177e4SLinus Torvalds 	kfree(ENI_VCC(vcc));
18911da177e4SLinus Torvalds 	vcc->dev_data = NULL;
18921da177e4SLinus Torvalds 	clear_bit(ATM_VF_ADDR,&vcc->flags);
18931da177e4SLinus Torvalds 	/*foo();*/
18941da177e4SLinus Torvalds }
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 
18971da177e4SLinus Torvalds static int eni_open(struct atm_vcc *vcc)
18981da177e4SLinus Torvalds {
18991da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
19001da177e4SLinus Torvalds 	struct eni_vcc *eni_vcc;
19011da177e4SLinus Torvalds 	int error;
19021da177e4SLinus Torvalds 	short vpi = vcc->vpi;
19031da177e4SLinus Torvalds 	int vci = vcc->vci;
19041da177e4SLinus Torvalds 
19051da177e4SLinus Torvalds 	DPRINTK(">eni_open\n");
19061da177e4SLinus Torvalds 	EVENT("eni_open\n",0,0);
19071da177e4SLinus Torvalds 	if (!test_bit(ATM_VF_PARTIAL,&vcc->flags))
19081da177e4SLinus Torvalds 		vcc->dev_data = NULL;
19091da177e4SLinus Torvalds 	eni_dev = ENI_DEV(vcc->dev);
19101da177e4SLinus Torvalds 	if (vci != ATM_VPI_UNSPEC && vpi != ATM_VCI_UNSPEC)
19111da177e4SLinus Torvalds 		set_bit(ATM_VF_ADDR,&vcc->flags);
19121da177e4SLinus Torvalds 	if (vcc->qos.aal != ATM_AAL0 && vcc->qos.aal != ATM_AAL5)
19131da177e4SLinus Torvalds 		return -EINVAL;
19141da177e4SLinus Torvalds 	DPRINTK(DEV_LABEL "(itf %d): open %d.%d\n",vcc->dev->number,vcc->vpi,
19151da177e4SLinus Torvalds 	    vcc->vci);
19161da177e4SLinus Torvalds 	if (!test_bit(ATM_VF_PARTIAL,&vcc->flags)) {
19171da177e4SLinus Torvalds 		eni_vcc = kmalloc(sizeof(struct eni_vcc),GFP_KERNEL);
19181da177e4SLinus Torvalds 		if (!eni_vcc) return -ENOMEM;
19191da177e4SLinus Torvalds 		vcc->dev_data = eni_vcc;
19201da177e4SLinus Torvalds 		eni_vcc->tx = NULL; /* for eni_close after open_rx */
19211da177e4SLinus Torvalds 		if ((error = open_rx_first(vcc))) {
19221da177e4SLinus Torvalds 			eni_close(vcc);
19231da177e4SLinus Torvalds 			return error;
19241da177e4SLinus Torvalds 		}
19251da177e4SLinus Torvalds 		if ((error = open_tx_first(vcc))) {
19261da177e4SLinus Torvalds 			eni_close(vcc);
19271da177e4SLinus Torvalds 			return error;
19281da177e4SLinus Torvalds 		}
19291da177e4SLinus Torvalds 	}
19301da177e4SLinus Torvalds 	if (vci == ATM_VPI_UNSPEC || vpi == ATM_VCI_UNSPEC) return 0;
19311da177e4SLinus Torvalds 	if ((error = open_rx_second(vcc))) {
19321da177e4SLinus Torvalds 		eni_close(vcc);
19331da177e4SLinus Torvalds 		return error;
19341da177e4SLinus Torvalds 	}
19351da177e4SLinus Torvalds 	if ((error = open_tx_second(vcc))) {
19361da177e4SLinus Torvalds 		eni_close(vcc);
19371da177e4SLinus Torvalds 		return error;
19381da177e4SLinus Torvalds 	}
19391da177e4SLinus Torvalds 	set_bit(ATM_VF_READY,&vcc->flags);
19401da177e4SLinus Torvalds 	/* should power down SUNI while !ref_count @@@ */
19411da177e4SLinus Torvalds 	return 0;
19421da177e4SLinus Torvalds }
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 
19451da177e4SLinus Torvalds static int eni_change_qos(struct atm_vcc *vcc,struct atm_qos *qos,int flgs)
19461da177e4SLinus Torvalds {
19471da177e4SLinus Torvalds 	struct eni_dev *eni_dev = ENI_DEV(vcc->dev);
19481da177e4SLinus Torvalds 	struct eni_tx *tx = ENI_VCC(vcc)->tx;
19491da177e4SLinus Torvalds 	struct sk_buff *skb;
19501da177e4SLinus Torvalds 	int error,rate,rsv,shp;
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	if (qos->txtp.traffic_class == ATM_NONE) return 0;
19531da177e4SLinus Torvalds 	if (tx == eni_dev->ubr) return -EBADFD;
19541da177e4SLinus Torvalds 	rate = atm_pcr_goal(&qos->txtp);
19551da177e4SLinus Torvalds 	if (rate < 0) rate = -rate;
19561da177e4SLinus Torvalds 	rsv = shp = 0;
19571da177e4SLinus Torvalds 	if ((flgs & ATM_MF_DEC_RSV) && rate && rate < tx->reserved) rsv = 1;
19581da177e4SLinus Torvalds 	if ((flgs & ATM_MF_INC_RSV) && (!rate || rate > tx->reserved)) rsv = 1;
19591da177e4SLinus Torvalds 	if ((flgs & ATM_MF_DEC_SHP) && rate && rate < tx->shaping) shp = 1;
19601da177e4SLinus Torvalds 	if ((flgs & ATM_MF_INC_SHP) && (!rate || rate > tx->shaping)) shp = 1;
19611da177e4SLinus Torvalds 	if (!rsv && !shp) return 0;
19621da177e4SLinus Torvalds 	error = reserve_or_set_tx(vcc,&qos->txtp,rsv,shp);
19631da177e4SLinus Torvalds 	if (error) return error;
19641da177e4SLinus Torvalds 	if (shp && !(flgs & ATM_MF_IMMED)) return 0;
19651da177e4SLinus Torvalds 	/*
19661da177e4SLinus Torvalds 	 * Walk through the send buffer and patch the rate information in all
19671da177e4SLinus Torvalds 	 * segmentation buffer descriptors of this VCC.
19681da177e4SLinus Torvalds 	 */
19691da177e4SLinus Torvalds 	tasklet_disable(&eni_dev->task);
19701da177e4SLinus Torvalds 	skb_queue_walk(&eni_dev->tx_queue, skb) {
19711da177e4SLinus Torvalds 		void __iomem *dsc;
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 		if (ATM_SKB(skb)->vcc != vcc) continue;
19741da177e4SLinus Torvalds 		dsc = tx->send+ENI_PRV_POS(skb)*4;
19751da177e4SLinus Torvalds 		writel((readl(dsc) & ~(MID_SEG_RATE | MID_SEG_PR)) |
19761da177e4SLinus Torvalds 		    (tx->prescaler << MID_SEG_PR_SHIFT) |
19771da177e4SLinus Torvalds 		    (tx->resolution << MID_SEG_RATE_SHIFT), dsc);
19781da177e4SLinus Torvalds 	}
19791da177e4SLinus Torvalds 	tasklet_enable(&eni_dev->task);
19801da177e4SLinus Torvalds 	return 0;
19811da177e4SLinus Torvalds }
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds static int eni_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
19851da177e4SLinus Torvalds {
19861da177e4SLinus Torvalds 	struct eni_dev *eni_dev = ENI_DEV(dev);
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 	if (cmd == ENI_MEMDUMP) {
19891da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN)) return -EPERM;
19901da177e4SLinus Torvalds 		printk(KERN_WARNING "Please use /proc/atm/" DEV_LABEL ":%d "
19911da177e4SLinus Torvalds 		    "instead of obsolete ioctl ENI_MEMDUMP\n",dev->number);
19921da177e4SLinus Torvalds 		dump(dev);
19931da177e4SLinus Torvalds 		return 0;
19941da177e4SLinus Torvalds 	}
19951da177e4SLinus Torvalds 	if (cmd == ENI_SETMULT) {
19961da177e4SLinus Torvalds 		struct eni_multipliers mult;
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN)) return -EPERM;
19991da177e4SLinus Torvalds 		if (copy_from_user(&mult, arg,
20001da177e4SLinus Torvalds 		    sizeof(struct eni_multipliers)))
20011da177e4SLinus Torvalds 			return -EFAULT;
20021da177e4SLinus Torvalds 		if ((mult.tx && mult.tx <= 100) || (mult.rx &&mult.rx <= 100) ||
20031da177e4SLinus Torvalds 		    mult.tx > 65536 || mult.rx > 65536)
20041da177e4SLinus Torvalds 			return -EINVAL;
20051da177e4SLinus Torvalds 		if (mult.tx) eni_dev->tx_mult = mult.tx;
20061da177e4SLinus Torvalds 		if (mult.rx) eni_dev->rx_mult = mult.rx;
20071da177e4SLinus Torvalds 		return 0;
20081da177e4SLinus Torvalds 	}
20091da177e4SLinus Torvalds 	if (cmd == ATM_SETCIRANGE) {
20101da177e4SLinus Torvalds 		struct atm_cirange ci;
20111da177e4SLinus Torvalds 
20121da177e4SLinus Torvalds 		if (copy_from_user(&ci, arg,sizeof(struct atm_cirange)))
20131da177e4SLinus Torvalds 			return -EFAULT;
20141da177e4SLinus Torvalds 		if ((ci.vpi_bits == 0 || ci.vpi_bits == ATM_CI_MAX) &&
20151da177e4SLinus Torvalds 		    (ci.vci_bits == NR_VCI_LD || ci.vpi_bits == ATM_CI_MAX))
20161da177e4SLinus Torvalds 		    return 0;
20171da177e4SLinus Torvalds 		return -EINVAL;
20181da177e4SLinus Torvalds 	}
20191da177e4SLinus Torvalds 	if (!dev->phy->ioctl) return -ENOIOCTLCMD;
20201da177e4SLinus Torvalds 	return dev->phy->ioctl(dev,cmd,arg);
20211da177e4SLinus Torvalds }
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds static int eni_getsockopt(struct atm_vcc *vcc,int level,int optname,
20251da177e4SLinus Torvalds     void __user *optval,int optlen)
20261da177e4SLinus Torvalds {
20271da177e4SLinus Torvalds 	return -EINVAL;
20281da177e4SLinus Torvalds }
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds static int eni_setsockopt(struct atm_vcc *vcc,int level,int optname,
20321da177e4SLinus Torvalds     void __user *optval,int optlen)
20331da177e4SLinus Torvalds {
20341da177e4SLinus Torvalds 	return -EINVAL;
20351da177e4SLinus Torvalds }
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds static int eni_send(struct atm_vcc *vcc,struct sk_buff *skb)
20391da177e4SLinus Torvalds {
20401da177e4SLinus Torvalds 	enum enq_res res;
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 	DPRINTK(">eni_send\n");
20431da177e4SLinus Torvalds 	if (!ENI_VCC(vcc)->tx) {
20441da177e4SLinus Torvalds 		if (vcc->pop) vcc->pop(vcc,skb);
20451da177e4SLinus Torvalds 		else dev_kfree_skb(skb);
20461da177e4SLinus Torvalds 		return -EINVAL;
20471da177e4SLinus Torvalds 	}
20481da177e4SLinus Torvalds 	if (!skb) {
20491da177e4SLinus Torvalds 		printk(KERN_CRIT "!skb in eni_send ?\n");
20501da177e4SLinus Torvalds 		if (vcc->pop) vcc->pop(vcc,skb);
20511da177e4SLinus Torvalds 		return -EINVAL;
20521da177e4SLinus Torvalds 	}
20531da177e4SLinus Torvalds 	if (vcc->qos.aal == ATM_AAL0) {
20541da177e4SLinus Torvalds 		if (skb->len != ATM_CELL_SIZE-1) {
20551da177e4SLinus Torvalds 			if (vcc->pop) vcc->pop(vcc,skb);
20561da177e4SLinus Torvalds 			else dev_kfree_skb(skb);
20571da177e4SLinus Torvalds 			return -EINVAL;
20581da177e4SLinus Torvalds 		}
20591da177e4SLinus Torvalds 		*(u32 *) skb->data = htonl(*(u32 *) skb->data);
20601da177e4SLinus Torvalds 	}
20611da177e4SLinus Torvalds submitted++;
20621da177e4SLinus Torvalds 	ATM_SKB(skb)->vcc = vcc;
20631da177e4SLinus Torvalds 	tasklet_disable(&ENI_DEV(vcc->dev)->task);
20641da177e4SLinus Torvalds 	res = do_tx(skb);
20651da177e4SLinus Torvalds 	tasklet_enable(&ENI_DEV(vcc->dev)->task);
20661da177e4SLinus Torvalds 	if (res == enq_ok) return 0;
20671da177e4SLinus Torvalds 	skb_queue_tail(&ENI_VCC(vcc)->tx->backlog,skb);
20681da177e4SLinus Torvalds backlogged++;
20691da177e4SLinus Torvalds 	tasklet_schedule(&ENI_DEV(vcc->dev)->task);
20701da177e4SLinus Torvalds 	return 0;
20711da177e4SLinus Torvalds }
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds static void eni_phy_put(struct atm_dev *dev,unsigned char value,
20741da177e4SLinus Torvalds     unsigned long addr)
20751da177e4SLinus Torvalds {
20761da177e4SLinus Torvalds 	writel(value,ENI_DEV(dev)->phy+addr*4);
20771da177e4SLinus Torvalds }
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds static unsigned char eni_phy_get(struct atm_dev *dev,unsigned long addr)
20821da177e4SLinus Torvalds {
20831da177e4SLinus Torvalds 	return readl(ENI_DEV(dev)->phy+addr*4);
20841da177e4SLinus Torvalds }
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds static int eni_proc_read(struct atm_dev *dev,loff_t *pos,char *page)
20881da177e4SLinus Torvalds {
20891da177e4SLinus Torvalds 	struct hlist_node *node;
20901da177e4SLinus Torvalds 	struct sock *s;
20911da177e4SLinus Torvalds 	static const char *signal[] = { "LOST","unknown","okay" };
20921da177e4SLinus Torvalds 	struct eni_dev *eni_dev = ENI_DEV(dev);
20931da177e4SLinus Torvalds 	struct atm_vcc *vcc;
20941da177e4SLinus Torvalds 	int left,i;
20951da177e4SLinus Torvalds 
20961da177e4SLinus Torvalds 	left = *pos;
20971da177e4SLinus Torvalds 	if (!left)
20981da177e4SLinus Torvalds 		return sprintf(page,DEV_LABEL "(itf %d) signal %s, %dkB, "
20991da177e4SLinus Torvalds 		    "%d cps remaining\n",dev->number,signal[(int) dev->signal],
21001da177e4SLinus Torvalds 		    eni_dev->mem >> 10,eni_dev->tx_bw);
21011da177e4SLinus Torvalds 	if (!--left)
21021da177e4SLinus Torvalds 		return sprintf(page,"%4sBursts: TX"
21031da177e4SLinus Torvalds #if !defined(CONFIG_ATM_ENI_BURST_TX_16W) && \
21041da177e4SLinus Torvalds     !defined(CONFIG_ATM_ENI_BURST_TX_8W) && \
21051da177e4SLinus Torvalds     !defined(CONFIG_ATM_ENI_BURST_TX_4W) && \
21061da177e4SLinus Torvalds     !defined(CONFIG_ATM_ENI_BURST_TX_2W)
21071da177e4SLinus Torvalds 		    " none"
21081da177e4SLinus Torvalds #endif
21091da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_TX_16W
21101da177e4SLinus Torvalds 		    " 16W"
21111da177e4SLinus Torvalds #endif
21121da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_TX_8W
21131da177e4SLinus Torvalds 		    " 8W"
21141da177e4SLinus Torvalds #endif
21151da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_TX_4W
21161da177e4SLinus Torvalds 		    " 4W"
21171da177e4SLinus Torvalds #endif
21181da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_TX_2W
21191da177e4SLinus Torvalds 		    " 2W"
21201da177e4SLinus Torvalds #endif
21211da177e4SLinus Torvalds 		    ", RX"
21221da177e4SLinus Torvalds #if !defined(CONFIG_ATM_ENI_BURST_RX_16W) && \
21231da177e4SLinus Torvalds     !defined(CONFIG_ATM_ENI_BURST_RX_8W) && \
21241da177e4SLinus Torvalds     !defined(CONFIG_ATM_ENI_BURST_RX_4W) && \
21251da177e4SLinus Torvalds     !defined(CONFIG_ATM_ENI_BURST_RX_2W)
21261da177e4SLinus Torvalds 		    " none"
21271da177e4SLinus Torvalds #endif
21281da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_RX_16W
21291da177e4SLinus Torvalds 		    " 16W"
21301da177e4SLinus Torvalds #endif
21311da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_RX_8W
21321da177e4SLinus Torvalds 		    " 8W"
21331da177e4SLinus Torvalds #endif
21341da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_RX_4W
21351da177e4SLinus Torvalds 		    " 4W"
21361da177e4SLinus Torvalds #endif
21371da177e4SLinus Torvalds #ifdef CONFIG_ATM_ENI_BURST_RX_2W
21381da177e4SLinus Torvalds 		    " 2W"
21391da177e4SLinus Torvalds #endif
21401da177e4SLinus Torvalds #ifndef CONFIG_ATM_ENI_TUNE_BURST
21411da177e4SLinus Torvalds 		    " (default)"
21421da177e4SLinus Torvalds #endif
21431da177e4SLinus Torvalds 		    "\n","");
21441da177e4SLinus Torvalds 	if (!--left)
21451da177e4SLinus Torvalds 		return sprintf(page,"%4sBuffer multipliers: tx %d%%, rx %d%%\n",
21461da177e4SLinus Torvalds 		    "",eni_dev->tx_mult,eni_dev->rx_mult);
21471da177e4SLinus Torvalds 	for (i = 0; i < NR_CHAN; i++) {
21481da177e4SLinus Torvalds 		struct eni_tx *tx = eni_dev->tx+i;
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds 		if (!tx->send) continue;
21511da177e4SLinus Torvalds 		if (!--left) {
21521da177e4SLinus Torvalds 			return sprintf(page,"tx[%d]:    0x%ld-0x%ld "
21531da177e4SLinus Torvalds 			    "(%6ld bytes), rsv %d cps, shp %d cps%s\n",i,
21541da177e4SLinus Torvalds 			    (unsigned long) (tx->send - eni_dev->ram),
21551da177e4SLinus Torvalds 			    tx->send-eni_dev->ram+tx->words*4-1,tx->words*4,
21561da177e4SLinus Torvalds 			    tx->reserved,tx->shaping,
21571da177e4SLinus Torvalds 			    tx == eni_dev->ubr ? " (UBR)" : "");
21581da177e4SLinus Torvalds 		}
21591da177e4SLinus Torvalds 		if (--left) continue;
21601da177e4SLinus Torvalds 		return sprintf(page,"%10sbacklog %u packets\n","",
21611da177e4SLinus Torvalds 		    skb_queue_len(&tx->backlog));
21621da177e4SLinus Torvalds 	}
21631da177e4SLinus Torvalds 	read_lock(&vcc_sklist_lock);
21641da177e4SLinus Torvalds 	for(i = 0; i < VCC_HTABLE_SIZE; ++i) {
21651da177e4SLinus Torvalds 		struct hlist_head *head = &vcc_hash[i];
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 		sk_for_each(s, node, head) {
21681da177e4SLinus Torvalds 			struct eni_vcc *eni_vcc;
21691da177e4SLinus Torvalds 			int length;
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds 			vcc = atm_sk(s);
21721da177e4SLinus Torvalds 			if (vcc->dev != dev)
21731da177e4SLinus Torvalds 				continue;
21741da177e4SLinus Torvalds 			eni_vcc = ENI_VCC(vcc);
21751da177e4SLinus Torvalds 			if (--left) continue;
21761da177e4SLinus Torvalds 			length = sprintf(page,"vcc %4d: ",vcc->vci);
21771da177e4SLinus Torvalds 			if (eni_vcc->rx) {
21781da177e4SLinus Torvalds 				length += sprintf(page+length,"0x%ld-0x%ld "
21791da177e4SLinus Torvalds 				    "(%6ld bytes)",
21801da177e4SLinus Torvalds 				    (unsigned long) (eni_vcc->recv - eni_dev->ram),
21811da177e4SLinus Torvalds 				    eni_vcc->recv-eni_dev->ram+eni_vcc->words*4-1,
21821da177e4SLinus Torvalds 				    eni_vcc->words*4);
21831da177e4SLinus Torvalds 				if (eni_vcc->tx) length += sprintf(page+length,", ");
21841da177e4SLinus Torvalds 			}
21851da177e4SLinus Torvalds 			if (eni_vcc->tx)
21861da177e4SLinus Torvalds 				length += sprintf(page+length,"tx[%d], txing %d bytes",
21871da177e4SLinus Torvalds 				    eni_vcc->tx->index,eni_vcc->txing);
21881da177e4SLinus Torvalds 			page[length] = '\n';
21891da177e4SLinus Torvalds 			read_unlock(&vcc_sklist_lock);
21901da177e4SLinus Torvalds 			return length+1;
21911da177e4SLinus Torvalds 		}
21921da177e4SLinus Torvalds 	}
21931da177e4SLinus Torvalds 	read_unlock(&vcc_sklist_lock);
21941da177e4SLinus Torvalds 	for (i = 0; i < eni_dev->free_len; i++) {
21951da177e4SLinus Torvalds 		struct eni_free *fe = eni_dev->free_list+i;
21961da177e4SLinus Torvalds 		unsigned long offset;
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds 		if (--left) continue;
21991da177e4SLinus Torvalds 		offset = (unsigned long) eni_dev->ram+eni_dev->base_diff;
22001da177e4SLinus Torvalds 		return sprintf(page,"free      %p-%p (%6d bytes)\n",
22011da177e4SLinus Torvalds 		    fe->start-offset,fe->start-offset+(1 << fe->order)-1,
22021da177e4SLinus Torvalds 		    1 << fe->order);
22031da177e4SLinus Torvalds 	}
22041da177e4SLinus Torvalds 	return 0;
22051da177e4SLinus Torvalds }
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds static const struct atmdev_ops ops = {
22091da177e4SLinus Torvalds 	.open		= eni_open,
22101da177e4SLinus Torvalds 	.close		= eni_close,
22111da177e4SLinus Torvalds 	.ioctl		= eni_ioctl,
22121da177e4SLinus Torvalds 	.getsockopt	= eni_getsockopt,
22131da177e4SLinus Torvalds 	.setsockopt	= eni_setsockopt,
22141da177e4SLinus Torvalds 	.send		= eni_send,
22151da177e4SLinus Torvalds 	.phy_put	= eni_phy_put,
22161da177e4SLinus Torvalds 	.phy_get	= eni_phy_get,
22171da177e4SLinus Torvalds 	.change_qos	= eni_change_qos,
22181da177e4SLinus Torvalds 	.proc_read	= eni_proc_read
22191da177e4SLinus Torvalds };
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds static int __devinit eni_init_one(struct pci_dev *pci_dev,
22231da177e4SLinus Torvalds     const struct pci_device_id *ent)
22241da177e4SLinus Torvalds {
22251da177e4SLinus Torvalds 	struct atm_dev *dev;
22261da177e4SLinus Torvalds 	struct eni_dev *eni_dev;
22271da177e4SLinus Torvalds 	int error = -ENOMEM;
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds 	DPRINTK("eni_init_one\n");
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds 	if (pci_enable_device(pci_dev)) {
22321da177e4SLinus Torvalds 		error = -EIO;
22331da177e4SLinus Torvalds 		goto out0;
22341da177e4SLinus Torvalds 	}
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 	eni_dev = (struct eni_dev *) kmalloc(sizeof(struct eni_dev),GFP_KERNEL);
22371da177e4SLinus Torvalds 	if (!eni_dev) goto out0;
22381da177e4SLinus Torvalds 	if (!cpu_zeroes) {
22391da177e4SLinus Torvalds 		cpu_zeroes = pci_alloc_consistent(pci_dev,ENI_ZEROES_SIZE,
22401da177e4SLinus Torvalds 		    &zeroes);
22411da177e4SLinus Torvalds 		if (!cpu_zeroes) goto out1;
22421da177e4SLinus Torvalds 	}
22431da177e4SLinus Torvalds 	dev = atm_dev_register(DEV_LABEL,&ops,-1,NULL);
22441da177e4SLinus Torvalds 	if (!dev) goto out2;
22451da177e4SLinus Torvalds 	pci_set_drvdata(pci_dev, dev);
22461da177e4SLinus Torvalds 	eni_dev->pci_dev = pci_dev;
22471da177e4SLinus Torvalds 	dev->dev_data = eni_dev;
22481da177e4SLinus Torvalds 	eni_dev->asic = ent->driver_data;
22491da177e4SLinus Torvalds 	error = eni_do_init(dev);
22501da177e4SLinus Torvalds 	if (error) goto out3;
22511da177e4SLinus Torvalds 	error = eni_start(dev);
22521da177e4SLinus Torvalds 	if (error) goto out3;
22531da177e4SLinus Torvalds 	eni_dev->more = eni_boards;
22541da177e4SLinus Torvalds 	eni_boards = dev;
22551da177e4SLinus Torvalds 	return 0;
22561da177e4SLinus Torvalds out3:
22571da177e4SLinus Torvalds 	atm_dev_deregister(dev);
22581da177e4SLinus Torvalds out2:
22591da177e4SLinus Torvalds 	pci_free_consistent(eni_dev->pci_dev,ENI_ZEROES_SIZE,cpu_zeroes,zeroes);
22601da177e4SLinus Torvalds 	cpu_zeroes = NULL;
22611da177e4SLinus Torvalds out1:
22621da177e4SLinus Torvalds 	kfree(eni_dev);
22631da177e4SLinus Torvalds out0:
22641da177e4SLinus Torvalds 	return error;
22651da177e4SLinus Torvalds }
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds static struct pci_device_id eni_pci_tbl[] = {
22691da177e4SLinus Torvalds 	{ PCI_VENDOR_ID_EF, PCI_DEVICE_ID_EF_ATM_FPGA, PCI_ANY_ID, PCI_ANY_ID,
22701da177e4SLinus Torvalds 	  0, 0, 0 /* FPGA */ },
22711da177e4SLinus Torvalds 	{ PCI_VENDOR_ID_EF, PCI_DEVICE_ID_EF_ATM_ASIC, PCI_ANY_ID, PCI_ANY_ID,
22721da177e4SLinus Torvalds 	  0, 0, 1 /* ASIC */ },
22731da177e4SLinus Torvalds 	{ 0, }
22741da177e4SLinus Torvalds };
22751da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci,eni_pci_tbl);
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds static void __devexit eni_remove_one(struct pci_dev *pci_dev)
22791da177e4SLinus Torvalds {
22801da177e4SLinus Torvalds 	/* grrr */
22811da177e4SLinus Torvalds }
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds static struct pci_driver eni_driver = {
22851da177e4SLinus Torvalds 	.name		= DEV_LABEL,
22861da177e4SLinus Torvalds 	.id_table	= eni_pci_tbl,
22871da177e4SLinus Torvalds 	.probe		= eni_init_one,
22881da177e4SLinus Torvalds 	.remove		= __devexit_p(eni_remove_one),
22891da177e4SLinus Torvalds };
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds static int __init eni_init(void)
22931da177e4SLinus Torvalds {
22941da177e4SLinus Torvalds 	struct sk_buff *skb; /* dummy for sizeof */
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds 	if (sizeof(skb->cb) < sizeof(struct eni_skb_prv)) {
22971da177e4SLinus Torvalds 		printk(KERN_ERR "eni_detect: skb->cb is too small (%Zd < %Zd)\n",
22981da177e4SLinus Torvalds 		    sizeof(skb->cb),sizeof(struct eni_skb_prv));
22991da177e4SLinus Torvalds 		return -EIO;
23001da177e4SLinus Torvalds 	}
23011da177e4SLinus Torvalds 	return pci_register_driver(&eni_driver);
23021da177e4SLinus Torvalds }
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds module_init(eni_init);
23061da177e4SLinus Torvalds /* @@@ since exit routine not defined, this module can not be unloaded */
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2309