• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/atm/
1/* drivers/atm/eni.c - Efficient Networks ENI155P device driver */
2
3/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
5
6#include <linux/module.h>
7#include <linux/kernel.h>
8#include <linux/mm.h>
9#include <linux/pci.h>
10#include <linux/errno.h>
11#include <linux/atm.h>
12#include <linux/atmdev.h>
13#include <linux/sonet.h>
14#include <linux/skbuff.h>
15#include <linux/time.h>
16#include <linux/delay.h>
17#include <linux/uio.h>
18#include <linux/init.h>
19#include <linux/atm_eni.h>
20#include <linux/bitops.h>
21#include <linux/slab.h>
22#include <asm/system.h>
23#include <asm/io.h>
24#include <asm/atomic.h>
25#include <asm/uaccess.h>
26#include <asm/string.h>
27#include <asm/byteorder.h>
28
29#include "tonga.h"
30#include "midway.h"
31#include "suni.h"
32#include "eni.h"
33
34#if !defined(__i386__) && !defined(__x86_64__)
35#ifndef ioremap_nocache
36#define ioremap_nocache(X,Y) ioremap(X,Y)
37#endif
38#endif
39
40/*
41 * TODO:
42 *
43 * Show stoppers
44 *  none
45 *
46 * Minor
47 *  - OAM support
48 *  - fix bugs listed below
49 */
50
51/*
52 * KNOWN BUGS:
53 *
54 * - may run into JK-JK bug and deadlock
55 * - should allocate UBR channel first
56 * - buffer space allocation algorithm is stupid
57 *   (RX: should be maxSDU+maxdelay*rate
58 *    TX: should be maxSDU+min(maxSDU,maxdelay*rate) )
59 * - doesn't support OAM cells
60 * - eni_put_free may hang if not putting memory fragments that _complete_
61 *   2^n block (never happens in real life, though)
62 */
63
64
65#define DPRINTK(format,args...)
66
67
68#ifndef CONFIG_ATM_ENI_TUNE_BURST
69#define CONFIG_ATM_ENI_BURST_TX_8W
70#define CONFIG_ATM_ENI_BURST_RX_4W
71#endif
72
73
74#ifndef CONFIG_ATM_ENI_DEBUG
75
76
77#define NULLCHECK(x)
78
79#define EVENT(s,a,b)
80
81
82static void event_dump(void)
83{
84}
85
86
87#else
88
89
90/*
91 * NULL pointer checking
92 */
93
94#define NULLCHECK(x) \
95	if ((unsigned long) (x) < 0x30) \
96		printk(KERN_CRIT #x "==0x%lx\n",(unsigned long) (x))
97
98/*
99 * Very extensive activity logging. Greatly improves bug detection speed but
100 * costs a few Mbps if enabled.
101 */
102
103#define EV 64
104
105static const char *ev[EV];
106static unsigned long ev_a[EV],ev_b[EV];
107static int ec = 0;
108
109
110static void EVENT(const char *s,unsigned long a,unsigned long b)
111{
112	ev[ec] = s;
113	ev_a[ec] = a;
114	ev_b[ec] = b;
115	ec = (ec+1) % EV;
116}
117
118
119static void event_dump(void)
120{
121	int n,i;
122
123	for (n = 0; n < EV; n++) {
124		i = (ec+n) % EV;
125		printk(KERN_NOTICE);
126		printk(ev[i] ? ev[i] : "(null)",ev_a[i],ev_b[i]);
127	}
128}
129
130
131#endif /* CONFIG_ATM_ENI_DEBUG */
132
133
134/*
135 * NExx   must not be equal at end
136 * EExx   may be equal at end
137 * xxPJOK verify validity of pointer jumps
138 * xxPMOK operating on a circular buffer of "c" words
139 */
140
141#define NEPJOK(a0,a1,b) \
142    ((a0) < (a1) ? (b) <= (a0) || (b) > (a1) : (b) <= (a0) && (b) > (a1))
143#define EEPJOK(a0,a1,b) \
144    ((a0) < (a1) ? (b) < (a0) || (b) >= (a1) : (b) < (a0) && (b) >= (a1))
145#define NEPMOK(a0,d,b,c) NEPJOK(a0,(a0+d) & (c-1),b)
146#define EEPMOK(a0,d,b,c) EEPJOK(a0,(a0+d) & (c-1),b)
147
148
149static int tx_complete = 0,dma_complete = 0,queued = 0,requeued = 0,
150  backlogged = 0,rx_enqueued = 0,rx_dequeued = 0,pushed = 0,submitted = 0,
151  putting = 0;
152
153static struct atm_dev *eni_boards = NULL;
154
155static u32 *cpu_zeroes = NULL; /* aligned "magic" zeroes */
156static dma_addr_t zeroes;
157
158/* Read/write registers on card */
159#define eni_in(r)	readl(eni_dev->reg+(r)*4)
160#define eni_out(v,r)	writel((v),eni_dev->reg+(r)*4)
161
162
163/*-------------------------------- utilities --------------------------------*/
164
165
166static void dump_mem(struct eni_dev *eni_dev)
167{
168	int i;
169
170	for (i = 0; i < eni_dev->free_len; i++)
171		printk(KERN_DEBUG "  %d: %p %d\n",i,
172		    eni_dev->free_list[i].start,
173		    1 << eni_dev->free_list[i].order);
174}
175
176
177static void dump(struct atm_dev *dev)
178{
179	struct eni_dev *eni_dev;
180
181	int i;
182
183	eni_dev = ENI_DEV(dev);
184	printk(KERN_NOTICE "Free memory\n");
185	dump_mem(eni_dev);
186	printk(KERN_NOTICE "TX buffers\n");
187	for (i = 0; i < NR_CHAN; i++)
188		if (eni_dev->tx[i].send)
189			printk(KERN_NOTICE "  TX %d @ %p: %ld\n",i,
190			    eni_dev->tx[i].send,eni_dev->tx[i].words*4);
191	printk(KERN_NOTICE "RX buffers\n");
192	for (i = 0; i < 1024; i++)
193		if (eni_dev->rx_map[i] && ENI_VCC(eni_dev->rx_map[i])->rx)
194			printk(KERN_NOTICE "  RX %d @ %p: %ld\n",i,
195			    ENI_VCC(eni_dev->rx_map[i])->recv,
196			    ENI_VCC(eni_dev->rx_map[i])->words*4);
197	printk(KERN_NOTICE "----\n");
198}
199
200
201static void eni_put_free(struct eni_dev *eni_dev, void __iomem *start,
202    unsigned long size)
203{
204	struct eni_free *list;
205	int len,order;
206
207	DPRINTK("init 0x%lx+%ld(0x%lx)\n",start,size,size);
208	start += eni_dev->base_diff;
209	list = eni_dev->free_list;
210	len = eni_dev->free_len;
211	while (size) {
212		if (len >= eni_dev->free_list_size) {
213			printk(KERN_CRIT "eni_put_free overflow (%p,%ld)\n",
214			    start,size);
215			break;
216		}
217		for (order = 0; !(((unsigned long)start | size) & (1 << order)); order++);
218		if (MID_MIN_BUF_SIZE > (1 << order)) {
219			printk(KERN_CRIT "eni_put_free: order %d too small\n",
220			    order);
221			break;
222		}
223		list[len].start = (void __iomem *) start;
224		list[len].order = order;
225		len++;
226		start += 1 << order;
227		size -= 1 << order;
228	}
229	eni_dev->free_len = len;
230	/*dump_mem(eni_dev);*/
231}
232
233
234static void __iomem *eni_alloc_mem(struct eni_dev *eni_dev, unsigned long *size)
235{
236	struct eni_free *list;
237	void __iomem *start;
238	int len,i,order,best_order,index;
239
240	list = eni_dev->free_list;
241	len = eni_dev->free_len;
242	if (*size < MID_MIN_BUF_SIZE) *size = MID_MIN_BUF_SIZE;
243	if (*size > MID_MAX_BUF_SIZE) return NULL;
244	for (order = 0; (1 << order) < *size; order++);
245	DPRINTK("trying: %ld->%d\n",*size,order);
246	best_order = 65; /* we don't have more than 2^64 of anything ... */
247	index = 0; /* silence GCC */
248	for (i = 0; i < len; i++)
249		if (list[i].order == order) {
250			best_order = order;
251			index = i;
252			break;
253		}
254		else if (best_order > list[i].order && list[i].order > order) {
255				best_order = list[i].order;
256				index = i;
257			}
258	if (best_order == 65) return NULL;
259	start = list[index].start-eni_dev->base_diff;
260	list[index] = list[--len];
261	eni_dev->free_len = len;
262	*size = 1 << order;
263	eni_put_free(eni_dev,start+*size,(1 << best_order)-*size);
264	DPRINTK("%ld bytes (order %d) at 0x%lx\n",*size,order,start);
265	memset_io(start,0,*size);       /* never leak data */
266	/*dump_mem(eni_dev);*/
267	return start;
268}
269
270
271static void eni_free_mem(struct eni_dev *eni_dev, void __iomem *start,
272    unsigned long size)
273{
274	struct eni_free *list;
275	int len,i,order;
276
277	start += eni_dev->base_diff;
278	list = eni_dev->free_list;
279	len = eni_dev->free_len;
280	for (order = -1; size; order++) size >>= 1;
281	DPRINTK("eni_free_mem: %p+0x%lx (order %d)\n",start,size,order);
282	for (i = 0; i < len; i++)
283		if (((unsigned long) list[i].start) == ((unsigned long)start^(1 << order)) &&
284		    list[i].order == order) {
285			DPRINTK("match[%d]: 0x%lx/0x%lx(0x%x), %d/%d\n",i,
286			    list[i].start,start,1 << order,list[i].order,order);
287			list[i] = list[--len];
288			start = (void __iomem *) ((unsigned long) start & ~(unsigned long) (1 << order));
289			order++;
290			i = -1;
291			continue;
292		}
293	if (len >= eni_dev->free_list_size) {
294		printk(KERN_ALERT "eni_free_mem overflow (%p,%d)\n",start,
295		    order);
296		return;
297	}
298	list[len].start = start;
299	list[len].order = order;
300	eni_dev->free_len = len+1;
301	/*dump_mem(eni_dev);*/
302}
303
304
305/*----------------------------------- RX ------------------------------------*/
306
307
308#define ENI_VCC_NOS ((struct atm_vcc *) 1)
309
310
311static void rx_ident_err(struct atm_vcc *vcc)
312{
313	struct atm_dev *dev;
314	struct eni_dev *eni_dev;
315	struct eni_vcc *eni_vcc;
316
317	dev = vcc->dev;
318	eni_dev = ENI_DEV(dev);
319	/* immediately halt adapter */
320	eni_out(eni_in(MID_MC_S) &
321	    ~(MID_DMA_ENABLE | MID_TX_ENABLE | MID_RX_ENABLE),MID_MC_S);
322	/* dump useful information */
323	eni_vcc = ENI_VCC(vcc);
324	printk(KERN_ALERT DEV_LABEL "(itf %d): driver error - RX ident "
325	    "mismatch\n",dev->number);
326	printk(KERN_ALERT "  VCI %d, rxing %d, words %ld\n",vcc->vci,
327	    eni_vcc->rxing,eni_vcc->words);
328	printk(KERN_ALERT "  host descr 0x%lx, rx pos 0x%lx, descr value "
329	    "0x%x\n",eni_vcc->descr,eni_vcc->rx_pos,
330	    (unsigned) readl(eni_vcc->recv+eni_vcc->descr*4));
331	printk(KERN_ALERT "  last %p, servicing %d\n",eni_vcc->last,
332	    eni_vcc->servicing);
333	EVENT("---dump ends here---\n",0,0);
334	printk(KERN_NOTICE "---recent events---\n");
335	event_dump();
336	ENI_DEV(dev)->fast = NULL; /* really stop it */
337	ENI_DEV(dev)->slow = NULL;
338	skb_queue_head_init(&ENI_DEV(dev)->rx_queue);
339}
340
341
342static int do_rx_dma(struct atm_vcc *vcc,struct sk_buff *skb,
343    unsigned long skip,unsigned long size,unsigned long eff)
344{
345	struct eni_dev *eni_dev;
346	struct eni_vcc *eni_vcc;
347	u32 dma_rd,dma_wr;
348	u32 dma[RX_DMA_BUF*2];
349	dma_addr_t paddr;
350	unsigned long here;
351	int i,j;
352
353	eni_dev = ENI_DEV(vcc->dev);
354	eni_vcc = ENI_VCC(vcc);
355	paddr = 0; /* GCC, shut up */
356	if (skb) {
357		paddr = pci_map_single(eni_dev->pci_dev,skb->data,skb->len,
358		    PCI_DMA_FROMDEVICE);
359		ENI_PRV_PADDR(skb) = paddr;
360		if (paddr & 3)
361			printk(KERN_CRIT DEV_LABEL "(itf %d): VCI %d has "
362			    "mis-aligned RX data (0x%lx)\n",vcc->dev->number,
363			    vcc->vci,(unsigned long) paddr);
364		ENI_PRV_SIZE(skb) = size+skip;
365		    /* PDU plus descriptor */
366		ATM_SKB(skb)->vcc = vcc;
367	}
368	j = 0;
369	if ((eff && skip) || 1) { /* @@@ actually, skip is always == 1 ... */
370		here = (eni_vcc->descr+skip) & (eni_vcc->words-1);
371		dma[j++] = (here << MID_DMA_COUNT_SHIFT) | (vcc->vci
372		    << MID_DMA_VCI_SHIFT) | MID_DT_JK;
373		j++;
374	}
375	here = (eni_vcc->descr+size+skip) & (eni_vcc->words-1);
376	if (!eff) size += skip;
377	else {
378		unsigned long words;
379
380		if (!size) {
381			DPRINTK("strange things happen ...\n");
382			EVENT("strange things happen ... (skip=%ld,eff=%ld)\n",
383			    size,eff);
384		}
385		words = eff;
386		if (paddr & 15) {
387			unsigned long init;
388
389			init = 4-((paddr & 15) >> 2);
390			if (init > words) init = words;
391			dma[j++] = MID_DT_WORD | (init << MID_DMA_COUNT_SHIFT) |
392			    (vcc->vci << MID_DMA_VCI_SHIFT);
393			dma[j++] = paddr;
394			paddr += init << 2;
395			words -= init;
396		}
397#ifdef CONFIG_ATM_ENI_BURST_RX_16W /* may work with some PCI chipsets ... */
398		if (words & ~15) {
399			dma[j++] = MID_DT_16W | ((words >> 4) <<
400			    MID_DMA_COUNT_SHIFT) | (vcc->vci <<
401			    MID_DMA_VCI_SHIFT);
402			dma[j++] = paddr;
403			paddr += (words & ~15) << 2;
404			words &= 15;
405		}
406#endif
407#ifdef CONFIG_ATM_ENI_BURST_RX_8W  /* works only with *some* PCI chipsets ... */
408		if (words & ~7) {
409			dma[j++] = MID_DT_8W | ((words >> 3) <<
410			    MID_DMA_COUNT_SHIFT) | (vcc->vci <<
411			    MID_DMA_VCI_SHIFT);
412			dma[j++] = paddr;
413			paddr += (words & ~7) << 2;
414			words &= 7;
415		}
416#endif
417#ifdef CONFIG_ATM_ENI_BURST_RX_4W /* recommended */
418		if (words & ~3) {
419			dma[j++] = MID_DT_4W | ((words >> 2) <<
420			    MID_DMA_COUNT_SHIFT) | (vcc->vci <<
421			    MID_DMA_VCI_SHIFT);
422			dma[j++] = paddr;
423			paddr += (words & ~3) << 2;
424			words &= 3;
425		}
426#endif
427#ifdef CONFIG_ATM_ENI_BURST_RX_2W /* probably useless if RX_4W, RX_8W, ... */
428		if (words & ~1) {
429			dma[j++] = MID_DT_2W | ((words >> 1) <<
430			    MID_DMA_COUNT_SHIFT) | (vcc->vci <<
431			    MID_DMA_VCI_SHIFT);
432			dma[j++] = paddr;
433			paddr += (words & ~1) << 2;
434			words &= 1;
435		}
436#endif
437		if (words) {
438			dma[j++] = MID_DT_WORD | (words << MID_DMA_COUNT_SHIFT)
439			    | (vcc->vci << MID_DMA_VCI_SHIFT);
440			dma[j++] = paddr;
441		}
442	}
443	if (size != eff) {
444		dma[j++] = (here << MID_DMA_COUNT_SHIFT) |
445		    (vcc->vci << MID_DMA_VCI_SHIFT) | MID_DT_JK;
446		j++;
447	}
448	if (!j || j > 2*RX_DMA_BUF) {
449		printk(KERN_CRIT DEV_LABEL "!j or j too big!!!\n");
450		goto trouble;
451	}
452	dma[j-2] |= MID_DMA_END;
453	j = j >> 1;
454	dma_wr = eni_in(MID_DMA_WR_RX);
455	dma_rd = eni_in(MID_DMA_RD_RX);
456	/*
457	 * Can I move the dma_wr pointer by 2j+1 positions without overwriting
458	 * data that hasn't been read (position of dma_rd) yet ?
459	 */
460	if (!NEPMOK(dma_wr,j+j+1,dma_rd,NR_DMA_RX)) { /* @@@ +1 is ugly */
461		printk(KERN_WARNING DEV_LABEL "(itf %d): RX DMA full\n",
462		    vcc->dev->number);
463		goto trouble;
464	}
465        for (i = 0; i < j; i++) {
466		writel(dma[i*2],eni_dev->rx_dma+dma_wr*8);
467		writel(dma[i*2+1],eni_dev->rx_dma+dma_wr*8+4);
468		dma_wr = (dma_wr+1) & (NR_DMA_RX-1);
469        }
470	if (skb) {
471		ENI_PRV_POS(skb) = eni_vcc->descr+size+1;
472		skb_queue_tail(&eni_dev->rx_queue,skb);
473		eni_vcc->last = skb;
474rx_enqueued++;
475	}
476	eni_vcc->descr = here;
477	eni_out(dma_wr,MID_DMA_WR_RX);
478	return 0;
479
480trouble:
481	if (paddr)
482		pci_unmap_single(eni_dev->pci_dev,paddr,skb->len,
483		    PCI_DMA_FROMDEVICE);
484	if (skb) dev_kfree_skb_irq(skb);
485	return -1;
486}
487
488
489static void discard(struct atm_vcc *vcc,unsigned long size)
490{
491	struct eni_vcc *eni_vcc;
492
493	eni_vcc = ENI_VCC(vcc);
494	EVENT("discard (size=%ld)\n",size,0);
495	while (do_rx_dma(vcc,NULL,1,size,0)) EVENT("BUSY LOOP",0,0);
496	    /* could do a full fallback, but that might be more expensive */
497	if (eni_vcc->rxing) ENI_PRV_POS(eni_vcc->last) += size+1;
498	else eni_vcc->rx_pos = (eni_vcc->rx_pos+size+1) & (eni_vcc->words-1);
499}
500
501
502/*
503 * TODO: should check whether direct copies (without DMA setup, dequeuing on
504 * interrupt, etc.) aren't much faster for AAL0
505 */
506
507static int rx_aal0(struct atm_vcc *vcc)
508{
509	struct eni_vcc *eni_vcc;
510	unsigned long descr;
511	unsigned long length;
512	struct sk_buff *skb;
513
514	DPRINTK(">rx_aal0\n");
515	eni_vcc = ENI_VCC(vcc);
516	descr = readl(eni_vcc->recv+eni_vcc->descr*4);
517	if ((descr & MID_RED_IDEN) != (MID_RED_RX_ID << MID_RED_SHIFT)) {
518		rx_ident_err(vcc);
519		return 1;
520	}
521	if (descr & MID_RED_T) {
522		DPRINTK(DEV_LABEL "(itf %d): trashing empty cell\n",
523		    vcc->dev->number);
524		length = 0;
525		atomic_inc(&vcc->stats->rx_err);
526	}
527	else {
528		length = ATM_CELL_SIZE-1; /* no HEC */
529	}
530	skb = length ? atm_alloc_charge(vcc,length,GFP_ATOMIC) : NULL;
531	if (!skb) {
532		discard(vcc,length >> 2);
533		return 0;
534	}
535	skb_put(skb,length);
536	skb->tstamp = eni_vcc->timestamp;
537	DPRINTK("got len %ld\n",length);
538	if (do_rx_dma(vcc,skb,1,length >> 2,length >> 2)) return 1;
539	eni_vcc->rxing++;
540	return 0;
541}
542
543
544static int rx_aal5(struct atm_vcc *vcc)
545{
546	struct eni_vcc *eni_vcc;
547	unsigned long descr;
548	unsigned long size,eff,length;
549	struct sk_buff *skb;
550
551	EVENT("rx_aal5\n",0,0);
552	DPRINTK(">rx_aal5\n");
553	eni_vcc = ENI_VCC(vcc);
554	descr = readl(eni_vcc->recv+eni_vcc->descr*4);
555	if ((descr & MID_RED_IDEN) != (MID_RED_RX_ID << MID_RED_SHIFT)) {
556		rx_ident_err(vcc);
557		return 1;
558	}
559	if (descr & (MID_RED_T | MID_RED_CRC_ERR)) {
560		if (descr & MID_RED_T) {
561			EVENT("empty cell (descr=0x%lx)\n",descr,0);
562			DPRINTK(DEV_LABEL "(itf %d): trashing empty cell\n",
563			    vcc->dev->number);
564			size = 0;
565		}
566		else {
567			static unsigned long silence = 0;
568
569			if (time_after(jiffies, silence) || silence == 0) {
570				printk(KERN_WARNING DEV_LABEL "(itf %d): "
571				    "discarding PDU(s) with CRC error\n",
572				    vcc->dev->number);
573				silence = (jiffies+2*HZ)|1;
574			}
575			size = (descr & MID_RED_COUNT)*(ATM_CELL_PAYLOAD >> 2);
576			EVENT("CRC error (descr=0x%lx,size=%ld)\n",descr,
577			    size);
578		}
579		eff = length = 0;
580		atomic_inc(&vcc->stats->rx_err);
581	}
582	else {
583		size = (descr & MID_RED_COUNT)*(ATM_CELL_PAYLOAD >> 2);
584		DPRINTK("size=%ld\n",size);
585		length = readl(eni_vcc->recv+(((eni_vcc->descr+size-1) &
586		    (eni_vcc->words-1)))*4) & 0xffff;
587				/* -trailer(2)+header(1) */
588		if (length && length <= (size << 2)-8 && length <=
589		  ATM_MAX_AAL5_PDU) eff = (length+3) >> 2;
590		else {				 /* ^ trailer length (8) */
591			EVENT("bad PDU (descr=0x08%lx,length=%ld)\n",descr,
592			    length);
593			printk(KERN_ERR DEV_LABEL "(itf %d): bad AAL5 PDU "
594			    "(VCI=%d,length=%ld,size=%ld (descr 0x%lx))\n",
595			    vcc->dev->number,vcc->vci,length,size << 2,descr);
596			length = eff = 0;
597			atomic_inc(&vcc->stats->rx_err);
598		}
599	}
600	skb = eff ? atm_alloc_charge(vcc,eff << 2,GFP_ATOMIC) : NULL;
601	if (!skb) {
602		discard(vcc,size);
603		return 0;
604	}
605	skb_put(skb,length);
606	DPRINTK("got len %ld\n",length);
607	if (do_rx_dma(vcc,skb,1,size,eff)) return 1;
608	eni_vcc->rxing++;
609	return 0;
610}
611
612
613static inline int rx_vcc(struct atm_vcc *vcc)
614{
615	void __iomem *vci_dsc;
616	unsigned long tmp;
617	struct eni_vcc *eni_vcc;
618
619	eni_vcc = ENI_VCC(vcc);
620	vci_dsc = ENI_DEV(vcc->dev)->vci+vcc->vci*16;
621	EVENT("rx_vcc(1)\n",0,0);
622	while (eni_vcc->descr != (tmp = (readl(vci_dsc+4) & MID_VCI_DESCR) >>
623	    MID_VCI_DESCR_SHIFT)) {
624		EVENT("rx_vcc(2: host dsc=0x%lx, nic dsc=0x%lx)\n",
625		    eni_vcc->descr,tmp);
626		DPRINTK("CB_DESCR %ld REG_DESCR %d\n",ENI_VCC(vcc)->descr,
627		    (((unsigned) readl(vci_dsc+4) & MID_VCI_DESCR) >>
628		    MID_VCI_DESCR_SHIFT));
629		if (ENI_VCC(vcc)->rx(vcc)) return 1;
630	}
631	/* clear IN_SERVICE flag */
632	writel(readl(vci_dsc) & ~MID_VCI_IN_SERVICE,vci_dsc);
633	/*
634	 * If new data has arrived between evaluating the while condition and
635	 * clearing IN_SERVICE, we wouldn't be notified until additional data
636	 * follows. So we have to loop again to be sure.
637	 */
638	EVENT("rx_vcc(3)\n",0,0);
639	while (ENI_VCC(vcc)->descr != (tmp = (readl(vci_dsc+4) & MID_VCI_DESCR)
640	    >> MID_VCI_DESCR_SHIFT)) {
641		EVENT("rx_vcc(4: host dsc=0x%lx, nic dsc=0x%lx)\n",
642		    eni_vcc->descr,tmp);
643		DPRINTK("CB_DESCR %ld REG_DESCR %d\n",ENI_VCC(vcc)->descr,
644		    (((unsigned) readl(vci_dsc+4) & MID_VCI_DESCR) >>
645		    MID_VCI_DESCR_SHIFT));
646		if (ENI_VCC(vcc)->rx(vcc)) return 1;
647	}
648	return 0;
649}
650
651
652static void poll_rx(struct atm_dev *dev)
653{
654	struct eni_dev *eni_dev;
655	struct atm_vcc *curr;
656
657	eni_dev = ENI_DEV(dev);
658	while ((curr = eni_dev->fast)) {
659		EVENT("poll_rx.fast\n",0,0);
660		if (rx_vcc(curr)) return;
661		eni_dev->fast = ENI_VCC(curr)->next;
662		ENI_VCC(curr)->next = ENI_VCC_NOS;
663		barrier();
664		ENI_VCC(curr)->servicing--;
665	}
666	while ((curr = eni_dev->slow)) {
667		EVENT("poll_rx.slow\n",0,0);
668		if (rx_vcc(curr)) return;
669		eni_dev->slow = ENI_VCC(curr)->next;
670		ENI_VCC(curr)->next = ENI_VCC_NOS;
671		barrier();
672		ENI_VCC(curr)->servicing--;
673	}
674}
675
676
677static void get_service(struct atm_dev *dev)
678{
679	struct eni_dev *eni_dev;
680	struct atm_vcc *vcc;
681	unsigned long vci;
682
683	DPRINTK(">get_service\n");
684	eni_dev = ENI_DEV(dev);
685	while (eni_in(MID_SERV_WRITE) != eni_dev->serv_read) {
686		vci = readl(eni_dev->service+eni_dev->serv_read*4);
687		eni_dev->serv_read = (eni_dev->serv_read+1) & (NR_SERVICE-1);
688		vcc = eni_dev->rx_map[vci & 1023];
689		if (!vcc) {
690			printk(KERN_CRIT DEV_LABEL "(itf %d): VCI %ld not "
691			    "found\n",dev->number,vci);
692			continue; /* nasty but we try to go on anyway */
693			/* @@@ nope, doesn't work */
694		}
695		EVENT("getting from service\n",0,0);
696		if (ENI_VCC(vcc)->next != ENI_VCC_NOS) {
697			EVENT("double service\n",0,0);
698			DPRINTK("Grr, servicing VCC %ld twice\n",vci);
699			continue;
700		}
701		ENI_VCC(vcc)->timestamp = ktime_get_real();
702		ENI_VCC(vcc)->next = NULL;
703		if (vcc->qos.rxtp.traffic_class == ATM_CBR) {
704			if (eni_dev->fast)
705				ENI_VCC(eni_dev->last_fast)->next = vcc;
706			else eni_dev->fast = vcc;
707			eni_dev->last_fast = vcc;
708		}
709		else {
710			if (eni_dev->slow)
711				ENI_VCC(eni_dev->last_slow)->next = vcc;
712			else eni_dev->slow = vcc;
713			eni_dev->last_slow = vcc;
714		}
715putting++;
716		ENI_VCC(vcc)->servicing++;
717	}
718}
719
720
721static void dequeue_rx(struct atm_dev *dev)
722{
723	struct eni_dev *eni_dev;
724	struct eni_vcc *eni_vcc;
725	struct atm_vcc *vcc;
726	struct sk_buff *skb;
727	void __iomem *vci_dsc;
728	int first;
729
730	eni_dev = ENI_DEV(dev);
731	first = 1;
732	while (1) {
733		skb = skb_dequeue(&eni_dev->rx_queue);
734		if (!skb) {
735			if (first) {
736				DPRINTK(DEV_LABEL "(itf %d): RX but not "
737				    "rxing\n",dev->number);
738				EVENT("nothing to dequeue\n",0,0);
739			}
740			break;
741		}
742		EVENT("dequeued (size=%ld,pos=0x%lx)\n",ENI_PRV_SIZE(skb),
743		    ENI_PRV_POS(skb));
744rx_dequeued++;
745		vcc = ATM_SKB(skb)->vcc;
746		eni_vcc = ENI_VCC(vcc);
747		first = 0;
748		vci_dsc = eni_dev->vci+vcc->vci*16;
749		if (!EEPMOK(eni_vcc->rx_pos,ENI_PRV_SIZE(skb),
750		    (readl(vci_dsc+4) & MID_VCI_READ) >> MID_VCI_READ_SHIFT,
751		    eni_vcc->words)) {
752			EVENT("requeuing\n",0,0);
753			skb_queue_head(&eni_dev->rx_queue,skb);
754			break;
755		}
756		eni_vcc->rxing--;
757		eni_vcc->rx_pos = ENI_PRV_POS(skb) & (eni_vcc->words-1);
758		pci_unmap_single(eni_dev->pci_dev,ENI_PRV_PADDR(skb),skb->len,
759		    PCI_DMA_TODEVICE);
760		if (!skb->len) dev_kfree_skb_irq(skb);
761		else {
762			EVENT("pushing (len=%ld)\n",skb->len,0);
763			if (vcc->qos.aal == ATM_AAL0)
764				*(unsigned long *) skb->data =
765				    ntohl(*(unsigned long *) skb->data);
766			memset(skb->cb,0,sizeof(struct eni_skb_prv));
767			vcc->push(vcc,skb);
768			pushed++;
769		}
770		atomic_inc(&vcc->stats->rx);
771	}
772	wake_up(&eni_dev->rx_wait);
773}
774
775
776static int open_rx_first(struct atm_vcc *vcc)
777{
778	struct eni_dev *eni_dev;
779	struct eni_vcc *eni_vcc;
780	unsigned long size;
781
782	DPRINTK("open_rx_first\n");
783	eni_dev = ENI_DEV(vcc->dev);
784	eni_vcc = ENI_VCC(vcc);
785	eni_vcc->rx = NULL;
786	if (vcc->qos.rxtp.traffic_class == ATM_NONE) return 0;
787	size = vcc->qos.rxtp.max_sdu*eni_dev->rx_mult/100;
788	if (size > MID_MAX_BUF_SIZE && vcc->qos.rxtp.max_sdu <=
789	    MID_MAX_BUF_SIZE)
790		size = MID_MAX_BUF_SIZE;
791	eni_vcc->recv = eni_alloc_mem(eni_dev,&size);
792	DPRINTK("rx at 0x%lx\n",eni_vcc->recv);
793	eni_vcc->words = size >> 2;
794	if (!eni_vcc->recv) return -ENOBUFS;
795	eni_vcc->rx = vcc->qos.aal == ATM_AAL5 ? rx_aal5 : rx_aal0;
796	eni_vcc->descr = 0;
797	eni_vcc->rx_pos = 0;
798	eni_vcc->rxing = 0;
799	eni_vcc->servicing = 0;
800	eni_vcc->next = ENI_VCC_NOS;
801	return 0;
802}
803
804
805static int open_rx_second(struct atm_vcc *vcc)
806{
807	void __iomem *here;
808	struct eni_dev *eni_dev;
809	struct eni_vcc *eni_vcc;
810	unsigned long size;
811	int order;
812
813	DPRINTK("open_rx_second\n");
814	eni_dev = ENI_DEV(vcc->dev);
815	eni_vcc = ENI_VCC(vcc);
816	if (!eni_vcc->rx) return 0;
817	/* set up VCI descriptor */
818	here = eni_dev->vci+vcc->vci*16;
819	DPRINTK("loc 0x%x\n",(unsigned) (eni_vcc->recv-eni_dev->ram)/4);
820	size = eni_vcc->words >> 8;
821	for (order = -1; size; order++) size >>= 1;
822	writel(0,here+4); /* descr, read = 0 */
823	writel(0,here+8); /* write, state, count = 0 */
824	if (eni_dev->rx_map[vcc->vci])
825		printk(KERN_CRIT DEV_LABEL "(itf %d): BUG - VCI %d already "
826		    "in use\n",vcc->dev->number,vcc->vci);
827	eni_dev->rx_map[vcc->vci] = vcc; /* now it counts */
828	writel(((vcc->qos.aal != ATM_AAL5 ? MID_MODE_RAW : MID_MODE_AAL5) <<
829	    MID_VCI_MODE_SHIFT) | MID_VCI_PTI_MODE |
830	    (((eni_vcc->recv-eni_dev->ram) >> (MID_LOC_SKIP+2)) <<
831	    MID_VCI_LOCATION_SHIFT) | (order << MID_VCI_SIZE_SHIFT),here);
832	return 0;
833}
834
835
836static void close_rx(struct atm_vcc *vcc)
837{
838	DECLARE_WAITQUEUE(wait,current);
839	void __iomem *here;
840	struct eni_dev *eni_dev;
841	struct eni_vcc *eni_vcc;
842
843	eni_vcc = ENI_VCC(vcc);
844	if (!eni_vcc->rx) return;
845	eni_dev = ENI_DEV(vcc->dev);
846	if (vcc->vpi != ATM_VPI_UNSPEC && vcc->vci != ATM_VCI_UNSPEC) {
847		here = eni_dev->vci+vcc->vci*16;
848		/* block receiver */
849		writel((readl(here) & ~MID_VCI_MODE) | (MID_MODE_TRASH <<
850		    MID_VCI_MODE_SHIFT),here);
851		/* wait for receiver to become idle */
852		udelay(27);
853		/* discard pending cell */
854		writel(readl(here) & ~MID_VCI_IN_SERVICE,here);
855		/* don't accept any new ones */
856		eni_dev->rx_map[vcc->vci] = NULL;
857		/* wait for RX queue to drain */
858		DPRINTK("eni_close: waiting for RX ...\n");
859		EVENT("RX closing\n",0,0);
860		add_wait_queue(&eni_dev->rx_wait,&wait);
861		set_current_state(TASK_UNINTERRUPTIBLE);
862		barrier();
863		for (;;) {
864			/* transition service->rx: rxing++, servicing-- */
865			if (!eni_vcc->servicing) {
866				barrier();
867				if (!eni_vcc->rxing) break;
868			}
869			EVENT("drain PDUs (rx %ld, serv %ld)\n",eni_vcc->rxing,
870			    eni_vcc->servicing);
871			printk(KERN_INFO "%d+%d RX left\n",eni_vcc->servicing,
872			    eni_vcc->rxing);
873			schedule();
874			set_current_state(TASK_UNINTERRUPTIBLE);
875		}
876		for (;;) {
877			int at_end;
878			u32 tmp;
879
880			tasklet_disable(&eni_dev->task);
881			tmp = readl(eni_dev->vci+vcc->vci*16+4) & MID_VCI_READ;
882			at_end = eni_vcc->rx_pos == tmp >> MID_VCI_READ_SHIFT;
883			tasklet_enable(&eni_dev->task);
884			if (at_end) break;
885			EVENT("drain discard (host 0x%lx, nic 0x%lx)\n",
886			    eni_vcc->rx_pos,tmp);
887			printk(KERN_INFO "draining RX: host 0x%lx, nic 0x%x\n",
888			    eni_vcc->rx_pos,tmp);
889			schedule();
890			set_current_state(TASK_UNINTERRUPTIBLE);
891		}
892		set_current_state(TASK_RUNNING);
893		remove_wait_queue(&eni_dev->rx_wait,&wait);
894	}
895	eni_free_mem(eni_dev,eni_vcc->recv,eni_vcc->words << 2);
896	eni_vcc->rx = NULL;
897}
898
899
900static int start_rx(struct atm_dev *dev)
901{
902	struct eni_dev *eni_dev;
903
904	eni_dev = ENI_DEV(dev);
905	eni_dev->rx_map = (struct atm_vcc **) get_zeroed_page(GFP_KERNEL);
906	if (!eni_dev->rx_map) {
907		printk(KERN_ERR DEV_LABEL "(itf %d): couldn't get free page\n",
908		    dev->number);
909		free_page((unsigned long) eni_dev->free_list);
910		return -ENOMEM;
911	}
912	eni_dev->rx_mult = DEFAULT_RX_MULT;
913	eni_dev->fast = eni_dev->last_fast = NULL;
914	eni_dev->slow = eni_dev->last_slow = NULL;
915	init_waitqueue_head(&eni_dev->rx_wait);
916	skb_queue_head_init(&eni_dev->rx_queue);
917	eni_dev->serv_read = eni_in(MID_SERV_WRITE);
918	eni_out(0,MID_DMA_WR_RX);
919	return 0;
920}
921
922
923/*----------------------------------- TX ------------------------------------*/
924
925
926enum enq_res { enq_ok,enq_next,enq_jam };
927
928
929static inline void put_dma(int chan,u32 *dma,int *j,dma_addr_t paddr,
930    u32 size)
931{
932	u32 init,words;
933
934	DPRINTK("put_dma: 0x%lx+0x%x\n",(unsigned long) paddr,size);
935	EVENT("put_dma: 0x%lx+0x%lx\n",(unsigned long) paddr,size);
936	if (paddr & 3) {
937		init = 4-(paddr & 3);
938		if (init > size || size < 7) init = size;
939		DPRINTK("put_dma: %lx DMA: %d/%d bytes\n",
940		    (unsigned long) paddr,init,size);
941		dma[(*j)++] = MID_DT_BYTE | (init << MID_DMA_COUNT_SHIFT) |
942		    (chan << MID_DMA_CHAN_SHIFT);
943		dma[(*j)++] = paddr;
944		paddr += init;
945		size -= init;
946	}
947	words = size >> 2;
948	size &= 3;
949	if (words && (paddr & 31)) {
950		init = 8-((paddr & 31) >> 2);
951		if (init > words) init = words;
952		DPRINTK("put_dma: %lx DMA: %d/%d words\n",
953		    (unsigned long) paddr,init,words);
954		dma[(*j)++] = MID_DT_WORD | (init << MID_DMA_COUNT_SHIFT) |
955		    (chan << MID_DMA_CHAN_SHIFT);
956		dma[(*j)++] = paddr;
957		paddr += init << 2;
958		words -= init;
959	}
960#ifdef CONFIG_ATM_ENI_BURST_TX_16W /* may work with some PCI chipsets ... */
961	if (words & ~15) {
962		DPRINTK("put_dma: %lx DMA: %d*16/%d words\n",
963		    (unsigned long) paddr,words >> 4,words);
964		dma[(*j)++] = MID_DT_16W | ((words >> 4) << MID_DMA_COUNT_SHIFT)
965		    | (chan << MID_DMA_CHAN_SHIFT);
966		dma[(*j)++] = paddr;
967		paddr += (words & ~15) << 2;
968		words &= 15;
969	}
970#endif
971#ifdef CONFIG_ATM_ENI_BURST_TX_8W /* recommended */
972	if (words & ~7) {
973		DPRINTK("put_dma: %lx DMA: %d*8/%d words\n",
974		    (unsigned long) paddr,words >> 3,words);
975		dma[(*j)++] = MID_DT_8W | ((words >> 3) << MID_DMA_COUNT_SHIFT)
976		    | (chan << MID_DMA_CHAN_SHIFT);
977		dma[(*j)++] = paddr;
978		paddr += (words & ~7) << 2;
979		words &= 7;
980	}
981#endif
982#ifdef CONFIG_ATM_ENI_BURST_TX_4W /* probably useless if TX_8W or TX_16W */
983	if (words & ~3) {
984		DPRINTK("put_dma: %lx DMA: %d*4/%d words\n",
985		    (unsigned long) paddr,words >> 2,words);
986		dma[(*j)++] = MID_DT_4W | ((words >> 2) << MID_DMA_COUNT_SHIFT)
987		    | (chan << MID_DMA_CHAN_SHIFT);
988		dma[(*j)++] = paddr;
989		paddr += (words & ~3) << 2;
990		words &= 3;
991	}
992#endif
993#ifdef CONFIG_ATM_ENI_BURST_TX_2W /* probably useless if TX_4W, TX_8W, ... */
994	if (words & ~1) {
995		DPRINTK("put_dma: %lx DMA: %d*2/%d words\n",
996		    (unsigned long) paddr,words >> 1,words);
997		dma[(*j)++] = MID_DT_2W | ((words >> 1) << MID_DMA_COUNT_SHIFT)
998		    | (chan << MID_DMA_CHAN_SHIFT);
999		dma[(*j)++] = paddr;
1000		paddr += (words & ~1) << 2;
1001		words &= 1;
1002	}
1003#endif
1004	if (words) {
1005		DPRINTK("put_dma: %lx DMA: %d words\n",(unsigned long) paddr,
1006		    words);
1007		dma[(*j)++] = MID_DT_WORD | (words << MID_DMA_COUNT_SHIFT) |
1008		    (chan << MID_DMA_CHAN_SHIFT);
1009		dma[(*j)++] = paddr;
1010		paddr += words << 2;
1011	}
1012	if (size) {
1013		DPRINTK("put_dma: %lx DMA: %d bytes\n",(unsigned long) paddr,
1014		    size);
1015		dma[(*j)++] = MID_DT_BYTE | (size << MID_DMA_COUNT_SHIFT) |
1016		    (chan << MID_DMA_CHAN_SHIFT);
1017		dma[(*j)++] = paddr;
1018	}
1019}
1020
1021
1022static enum enq_res do_tx(struct sk_buff *skb)
1023{
1024	struct atm_vcc *vcc;
1025	struct eni_dev *eni_dev;
1026	struct eni_vcc *eni_vcc;
1027	struct eni_tx *tx;
1028	dma_addr_t paddr;
1029	u32 dma_rd,dma_wr;
1030	u32 size; /* in words */
1031	int aal5,dma_size,i,j;
1032
1033	DPRINTK(">do_tx\n");
1034	NULLCHECK(skb);
1035	EVENT("do_tx: skb=0x%lx, %ld bytes\n",(unsigned long) skb,skb->len);
1036	vcc = ATM_SKB(skb)->vcc;
1037	NULLCHECK(vcc);
1038	eni_dev = ENI_DEV(vcc->dev);
1039	NULLCHECK(eni_dev);
1040	eni_vcc = ENI_VCC(vcc);
1041	tx = eni_vcc->tx;
1042	NULLCHECK(tx);
1043	/*
1044	 * Potential future IP speedup: make hard_header big enough to put
1045	 * segmentation descriptor directly into PDU. Saves: 4 slave writes,
1046	 * 1 DMA xfer & 2 DMA'ed bytes (protocol layering is for wimps :-)
1047	 */
1048
1049	aal5 = vcc->qos.aal == ATM_AAL5;
1050	/* check space in buffer */
1051	if (!aal5)
1052		size = (ATM_CELL_PAYLOAD >> 2)+TX_DESCR_SIZE;
1053			/* cell without HEC plus segmentation header (includes
1054			   four-byte cell header) */
1055	else {
1056		size = skb->len+4*AAL5_TRAILER+ATM_CELL_PAYLOAD-1;
1057			/* add AAL5 trailer */
1058		size = ((size-(size % ATM_CELL_PAYLOAD)) >> 2)+TX_DESCR_SIZE;
1059						/* add segmentation header */
1060	}
1061	/*
1062	 * Can I move tx_pos by size bytes without getting closer than TX_GAP
1063	 * to the read pointer ? TX_GAP means to leave some space for what
1064	 * the manual calls "too close".
1065	 */
1066	if (!NEPMOK(tx->tx_pos,size+TX_GAP,
1067	    eni_in(MID_TX_RDPTR(tx->index)),tx->words)) {
1068		DPRINTK(DEV_LABEL "(itf %d): TX full (size %d)\n",
1069		    vcc->dev->number,size);
1070		return enq_next;
1071	}
1072	/* check DMA */
1073	dma_wr = eni_in(MID_DMA_WR_TX);
1074	dma_rd = eni_in(MID_DMA_RD_TX);
1075	dma_size = 3; /* JK for descriptor and final fill, plus final size
1076			 mis-alignment fix */
1077DPRINTK("iovcnt = %d\n",skb_shinfo(skb)->nr_frags);
1078	if (!skb_shinfo(skb)->nr_frags) dma_size += 5;
1079	else dma_size += 5*(skb_shinfo(skb)->nr_frags+1);
1080	if (dma_size > TX_DMA_BUF) {
1081		printk(KERN_CRIT DEV_LABEL "(itf %d): needs %d DMA entries "
1082		    "(got only %d)\n",vcc->dev->number,dma_size,TX_DMA_BUF);
1083	}
1084	DPRINTK("dma_wr is %d, tx_pos is %ld\n",dma_wr,tx->tx_pos);
1085	if (dma_wr != dma_rd && ((dma_rd+NR_DMA_TX-dma_wr) & (NR_DMA_TX-1)) <
1086	     dma_size) {
1087		printk(KERN_WARNING DEV_LABEL "(itf %d): TX DMA full\n",
1088		    vcc->dev->number);
1089		return enq_jam;
1090	}
1091	paddr = pci_map_single(eni_dev->pci_dev,skb->data,skb->len,
1092	    PCI_DMA_TODEVICE);
1093	ENI_PRV_PADDR(skb) = paddr;
1094	/* prepare DMA queue entries */
1095	j = 0;
1096	eni_dev->dma[j++] = (((tx->tx_pos+TX_DESCR_SIZE) & (tx->words-1)) <<
1097	     MID_DMA_COUNT_SHIFT) | (tx->index << MID_DMA_CHAN_SHIFT) |
1098	     MID_DT_JK;
1099	j++;
1100	if (!skb_shinfo(skb)->nr_frags)
1101		if (aal5) put_dma(tx->index,eni_dev->dma,&j,paddr,skb->len);
1102		else put_dma(tx->index,eni_dev->dma,&j,paddr+4,skb->len-4);
1103	else {
1104DPRINTK("doing direct send\n"); /* @@@ well, this doesn't work anyway */
1105		for (i = -1; i < skb_shinfo(skb)->nr_frags; i++)
1106			if (i == -1)
1107				put_dma(tx->index,eni_dev->dma,&j,(unsigned long)
1108				    skb->data,
1109				    skb_headlen(skb));
1110			else
1111				put_dma(tx->index,eni_dev->dma,&j,(unsigned long)
1112				    skb_shinfo(skb)->frags[i].page + skb_shinfo(skb)->frags[i].page_offset,
1113				    skb_shinfo(skb)->frags[i].size);
1114	}
1115	if (skb->len & 3)
1116		put_dma(tx->index,eni_dev->dma,&j,zeroes,4-(skb->len & 3));
1117	/* JK for AAL5 trailer - AAL0 doesn't need it, but who cares ... */
1118	eni_dev->dma[j++] = (((tx->tx_pos+size) & (tx->words-1)) <<
1119	     MID_DMA_COUNT_SHIFT) | (tx->index << MID_DMA_CHAN_SHIFT) |
1120	     MID_DMA_END | MID_DT_JK;
1121	j++;
1122	DPRINTK("DMA at end: %d\n",j);
1123	/* store frame */
1124	writel((MID_SEG_TX_ID << MID_SEG_ID_SHIFT) |
1125	    (aal5 ? MID_SEG_AAL5 : 0) | (tx->prescaler << MID_SEG_PR_SHIFT) |
1126	    (tx->resolution << MID_SEG_RATE_SHIFT) |
1127	    (size/(ATM_CELL_PAYLOAD/4)),tx->send+tx->tx_pos*4);
1128/*printk("dsc = 0x%08lx\n",(unsigned long) readl(tx->send+tx->tx_pos*4));*/
1129	writel((vcc->vci << MID_SEG_VCI_SHIFT) |
1130            (aal5 ? 0 : (skb->data[3] & 0xf)) |
1131	    (ATM_SKB(skb)->atm_options & ATM_ATMOPT_CLP ? MID_SEG_CLP : 0),
1132	    tx->send+((tx->tx_pos+1) & (tx->words-1))*4);
1133	DPRINTK("size: %d, len:%d\n",size,skb->len);
1134	if (aal5)
1135		writel(skb->len,tx->send+
1136                    ((tx->tx_pos+size-AAL5_TRAILER) & (tx->words-1))*4);
1137	j = j >> 1;
1138	for (i = 0; i < j; i++) {
1139		writel(eni_dev->dma[i*2],eni_dev->tx_dma+dma_wr*8);
1140		writel(eni_dev->dma[i*2+1],eni_dev->tx_dma+dma_wr*8+4);
1141		dma_wr = (dma_wr+1) & (NR_DMA_TX-1);
1142	}
1143	ENI_PRV_POS(skb) = tx->tx_pos;
1144	ENI_PRV_SIZE(skb) = size;
1145	ENI_VCC(vcc)->txing += size;
1146	tx->tx_pos = (tx->tx_pos+size) & (tx->words-1);
1147	DPRINTK("dma_wr set to %d, tx_pos is now %ld\n",dma_wr,tx->tx_pos);
1148	eni_out(dma_wr,MID_DMA_WR_TX);
1149	skb_queue_tail(&eni_dev->tx_queue,skb);
1150queued++;
1151	return enq_ok;
1152}
1153
1154
1155static void poll_tx(struct atm_dev *dev)
1156{
1157	struct eni_tx *tx;
1158	struct sk_buff *skb;
1159	enum enq_res res;
1160	int i;
1161
1162	DPRINTK(">poll_tx\n");
1163	for (i = NR_CHAN-1; i >= 0; i--) {
1164		tx = &ENI_DEV(dev)->tx[i];
1165		if (tx->send)
1166			while ((skb = skb_dequeue(&tx->backlog))) {
1167				res = do_tx(skb);
1168				if (res == enq_ok) continue;
1169				DPRINTK("re-queuing TX PDU\n");
1170				skb_queue_head(&tx->backlog,skb);
1171requeued++;
1172				if (res == enq_jam) return;
1173				break;
1174			}
1175	}
1176}
1177
1178
1179static void dequeue_tx(struct atm_dev *dev)
1180{
1181	struct eni_dev *eni_dev;
1182	struct atm_vcc *vcc;
1183	struct sk_buff *skb;
1184	struct eni_tx *tx;
1185
1186	NULLCHECK(dev);
1187	eni_dev = ENI_DEV(dev);
1188	NULLCHECK(eni_dev);
1189	while ((skb = skb_dequeue(&eni_dev->tx_queue))) {
1190		vcc = ATM_SKB(skb)->vcc;
1191		NULLCHECK(vcc);
1192		tx = ENI_VCC(vcc)->tx;
1193		NULLCHECK(ENI_VCC(vcc)->tx);
1194		DPRINTK("dequeue_tx: next 0x%lx curr 0x%x\n",ENI_PRV_POS(skb),
1195		    (unsigned) eni_in(MID_TX_DESCRSTART(tx->index)));
1196		if (ENI_VCC(vcc)->txing < tx->words && ENI_PRV_POS(skb) ==
1197		    eni_in(MID_TX_DESCRSTART(tx->index))) {
1198			skb_queue_head(&eni_dev->tx_queue,skb);
1199			break;
1200		}
1201		ENI_VCC(vcc)->txing -= ENI_PRV_SIZE(skb);
1202		pci_unmap_single(eni_dev->pci_dev,ENI_PRV_PADDR(skb),skb->len,
1203		    PCI_DMA_TODEVICE);
1204		if (vcc->pop) vcc->pop(vcc,skb);
1205		else dev_kfree_skb_irq(skb);
1206		atomic_inc(&vcc->stats->tx);
1207		wake_up(&eni_dev->tx_wait);
1208dma_complete++;
1209	}
1210}
1211
1212
1213static struct eni_tx *alloc_tx(struct eni_dev *eni_dev,int ubr)
1214{
1215	int i;
1216
1217	for (i = !ubr; i < NR_CHAN; i++)
1218		if (!eni_dev->tx[i].send) return eni_dev->tx+i;
1219	return NULL;
1220}
1221
1222
1223static int comp_tx(struct eni_dev *eni_dev,int *pcr,int reserved,int *pre,
1224    int *res,int unlimited)
1225{
1226	static const int pre_div[] = { 4,16,128,2048 };
1227	    /* 2^(((x+2)^2-(x+2))/2+1) */
1228
1229	if (unlimited) *pre = *res = 0;
1230	else {
1231		if (*pcr > 0) {
1232			int div;
1233
1234			for (*pre = 0; *pre < 3; (*pre)++)
1235				if (TS_CLOCK/pre_div[*pre]/64 <= *pcr) break;
1236			div = pre_div[*pre]**pcr;
1237			DPRINTK("min div %d\n",div);
1238			*res = TS_CLOCK/div-1;
1239		}
1240		else {
1241			int div;
1242
1243			if (!*pcr) *pcr = eni_dev->tx_bw+reserved;
1244			for (*pre = 3; *pre >= 0; (*pre)--)
1245				if (TS_CLOCK/pre_div[*pre]/64 > -*pcr) break;
1246			if (*pre < 3) (*pre)++; /* else fail later */
1247			div = pre_div[*pre]*-*pcr;
1248			DPRINTK("max div %d\n",div);
1249			*res = DIV_ROUND_UP(TS_CLOCK, div)-1;
1250		}
1251		if (*res < 0) *res = 0;
1252		if (*res > MID_SEG_MAX_RATE) *res = MID_SEG_MAX_RATE;
1253	}
1254	*pcr = TS_CLOCK/pre_div[*pre]/(*res+1);
1255	DPRINTK("out pcr: %d (%d:%d)\n",*pcr,*pre,*res);
1256	return 0;
1257}
1258
1259
1260static int reserve_or_set_tx(struct atm_vcc *vcc,struct atm_trafprm *txtp,
1261    int set_rsv,int set_shp)
1262{
1263	struct eni_dev *eni_dev = ENI_DEV(vcc->dev);
1264	struct eni_vcc *eni_vcc = ENI_VCC(vcc);
1265	struct eni_tx *tx;
1266	unsigned long size;
1267	void __iomem *mem;
1268	int rate,ubr,unlimited,new_tx;
1269	int pre,res,order;
1270	int error;
1271
1272	rate = atm_pcr_goal(txtp);
1273	ubr = txtp->traffic_class == ATM_UBR;
1274	unlimited = ubr && (!rate || rate <= -ATM_OC3_PCR ||
1275	    rate >= ATM_OC3_PCR);
1276	if (!unlimited) {
1277		size = txtp->max_sdu*eni_dev->tx_mult/100;
1278		if (size > MID_MAX_BUF_SIZE && txtp->max_sdu <=
1279		    MID_MAX_BUF_SIZE)
1280			size = MID_MAX_BUF_SIZE;
1281	}
1282	else {
1283		if (eni_dev->ubr) {
1284			eni_vcc->tx = eni_dev->ubr;
1285			txtp->pcr = ATM_OC3_PCR;
1286			return 0;
1287		}
1288		size = UBR_BUFFER;
1289	}
1290	new_tx = !eni_vcc->tx;
1291	mem = NULL; /* for gcc */
1292	if (!new_tx) tx = eni_vcc->tx;
1293	else {
1294		mem = eni_alloc_mem(eni_dev,&size);
1295		if (!mem) return -ENOBUFS;
1296		tx = alloc_tx(eni_dev,unlimited);
1297		if (!tx) {
1298			eni_free_mem(eni_dev,mem,size);
1299			return -EBUSY;
1300		}
1301		DPRINTK("got chan %d\n",tx->index);
1302		tx->reserved = tx->shaping = 0;
1303		tx->send = mem;
1304		tx->words = size >> 2;
1305		skb_queue_head_init(&tx->backlog);
1306		for (order = 0; size > (1 << (order+10)); order++);
1307		eni_out((order << MID_SIZE_SHIFT) |
1308		    ((tx->send-eni_dev->ram) >> (MID_LOC_SKIP+2)),
1309		    MID_TX_PLACE(tx->index));
1310		tx->tx_pos = eni_in(MID_TX_DESCRSTART(tx->index)) &
1311		    MID_DESCR_START;
1312	}
1313	error = comp_tx(eni_dev,&rate,tx->reserved,&pre,&res,unlimited);
1314	if (!error  && txtp->min_pcr > rate) error = -EINVAL;
1315	if (!error && txtp->max_pcr && txtp->max_pcr != ATM_MAX_PCR &&
1316	    txtp->max_pcr < rate) error = -EINVAL;
1317	if (!error && !ubr && rate > eni_dev->tx_bw+tx->reserved)
1318		error = -EINVAL;
1319	if (!error && set_rsv && !set_shp && rate < tx->shaping)
1320		error = -EINVAL;
1321	if (!error && !set_rsv && rate > tx->reserved && !ubr)
1322		error = -EINVAL;
1323	if (error) {
1324		if (new_tx) {
1325			tx->send = NULL;
1326			eni_free_mem(eni_dev,mem,size);
1327		}
1328		return error;
1329	}
1330	txtp->pcr = rate;
1331	if (set_rsv && !ubr) {
1332		eni_dev->tx_bw += tx->reserved;
1333		tx->reserved = rate;
1334		eni_dev->tx_bw -= rate;
1335	}
1336	if (set_shp || (unlimited && new_tx)) {
1337		if (unlimited && new_tx) eni_dev->ubr = tx;
1338		tx->prescaler = pre;
1339		tx->resolution = res;
1340		tx->shaping = rate;
1341	}
1342	if (set_shp) eni_vcc->tx = tx;
1343	DPRINTK("rsv %d shp %d\n",tx->reserved,tx->shaping);
1344	return 0;
1345}
1346
1347
1348static int open_tx_first(struct atm_vcc *vcc)
1349{
1350	ENI_VCC(vcc)->tx = NULL;
1351	if (vcc->qos.txtp.traffic_class == ATM_NONE) return 0;
1352	ENI_VCC(vcc)->txing = 0;
1353	return reserve_or_set_tx(vcc,&vcc->qos.txtp,1,1);
1354}
1355
1356
1357static int open_tx_second(struct atm_vcc *vcc)
1358{
1359	return 0; /* nothing to do */
1360}
1361
1362
1363static void close_tx(struct atm_vcc *vcc)
1364{
1365	DECLARE_WAITQUEUE(wait,current);
1366	struct eni_dev *eni_dev;
1367	struct eni_vcc *eni_vcc;
1368
1369	eni_vcc = ENI_VCC(vcc);
1370	if (!eni_vcc->tx) return;
1371	eni_dev = ENI_DEV(vcc->dev);
1372	/* wait for TX queue to drain */
1373	DPRINTK("eni_close: waiting for TX ...\n");
1374	add_wait_queue(&eni_dev->tx_wait,&wait);
1375	set_current_state(TASK_UNINTERRUPTIBLE);
1376	for (;;) {
1377		int txing;
1378
1379		tasklet_disable(&eni_dev->task);
1380		txing = skb_peek(&eni_vcc->tx->backlog) || eni_vcc->txing;
1381		tasklet_enable(&eni_dev->task);
1382		if (!txing) break;
1383		DPRINTK("%d TX left\n",eni_vcc->txing);
1384		schedule();
1385		set_current_state(TASK_UNINTERRUPTIBLE);
1386	}
1387	set_current_state(TASK_RUNNING);
1388	remove_wait_queue(&eni_dev->tx_wait,&wait);
1389	if (eni_vcc->tx != eni_dev->ubr) {
1390		/*
1391		 * Looping a few times in here is probably far cheaper than
1392		 * keeping track of TX completions all the time, so let's poll
1393		 * a bit ...
1394		 */
1395		while (eni_in(MID_TX_RDPTR(eni_vcc->tx->index)) !=
1396		    eni_in(MID_TX_DESCRSTART(eni_vcc->tx->index)))
1397			schedule();
1398		eni_free_mem(eni_dev,eni_vcc->tx->send,eni_vcc->tx->words << 2);
1399		eni_vcc->tx->send = NULL;
1400		eni_dev->tx_bw += eni_vcc->tx->reserved;
1401	}
1402	eni_vcc->tx = NULL;
1403}
1404
1405
1406static int start_tx(struct atm_dev *dev)
1407{
1408	struct eni_dev *eni_dev;
1409	int i;
1410
1411	eni_dev = ENI_DEV(dev);
1412	eni_dev->lost = 0;
1413	eni_dev->tx_bw = ATM_OC3_PCR;
1414	eni_dev->tx_mult = DEFAULT_TX_MULT;
1415	init_waitqueue_head(&eni_dev->tx_wait);
1416	eni_dev->ubr = NULL;
1417	skb_queue_head_init(&eni_dev->tx_queue);
1418	eni_out(0,MID_DMA_WR_TX);
1419	for (i = 0; i < NR_CHAN; i++) {
1420		eni_dev->tx[i].send = NULL;
1421		eni_dev->tx[i].index = i;
1422	}
1423	return 0;
1424}
1425
1426
1427/*--------------------------------- common ----------------------------------*/
1428
1429
1430
1431
1432static void bug_int(struct atm_dev *dev,unsigned long reason)
1433{
1434	struct eni_dev *eni_dev;
1435
1436	DPRINTK(">bug_int\n");
1437	eni_dev = ENI_DEV(dev);
1438	if (reason & MID_DMA_ERR_ACK)
1439		printk(KERN_CRIT DEV_LABEL "(itf %d): driver error - DMA "
1440		    "error\n",dev->number);
1441	if (reason & MID_TX_IDENT_MISM)
1442		printk(KERN_CRIT DEV_LABEL "(itf %d): driver error - ident "
1443		    "mismatch\n",dev->number);
1444	if (reason & MID_TX_DMA_OVFL)
1445		printk(KERN_CRIT DEV_LABEL "(itf %d): driver error - DMA "
1446		    "overflow\n",dev->number);
1447	EVENT("---dump ends here---\n",0,0);
1448	printk(KERN_NOTICE "---recent events---\n");
1449	event_dump();
1450}
1451
1452
1453static irqreturn_t eni_int(int irq,void *dev_id)
1454{
1455	struct atm_dev *dev;
1456	struct eni_dev *eni_dev;
1457	u32 reason;
1458
1459	DPRINTK(">eni_int\n");
1460	dev = dev_id;
1461	eni_dev = ENI_DEV(dev);
1462	reason = eni_in(MID_ISA);
1463	DPRINTK(DEV_LABEL ": int 0x%lx\n",(unsigned long) reason);
1464	/*
1465	 * Must handle these two right now, because reading ISA doesn't clear
1466	 * them, so they re-occur and we never make it to the tasklet. Since
1467	 * they're rare, we don't mind the occasional invocation of eni_tasklet
1468	 * with eni_dev->events == 0.
1469	 */
1470	if (reason & MID_STAT_OVFL) {
1471		EVENT("stat overflow\n",0,0);
1472		eni_dev->lost += eni_in(MID_STAT) & MID_OVFL_TRASH;
1473	}
1474	if (reason & MID_SUNI_INT) {
1475		EVENT("SUNI int\n",0,0);
1476		dev->phy->interrupt(dev);
1477	}
1478	spin_lock(&eni_dev->lock);
1479	eni_dev->events |= reason;
1480	spin_unlock(&eni_dev->lock);
1481	tasklet_schedule(&eni_dev->task);
1482	return IRQ_HANDLED;
1483}
1484
1485
1486static void eni_tasklet(unsigned long data)
1487{
1488	struct atm_dev *dev = (struct atm_dev *) data;
1489	struct eni_dev *eni_dev = ENI_DEV(dev);
1490	unsigned long flags;
1491	u32 events;
1492
1493	DPRINTK("eni_tasklet (dev %p)\n",dev);
1494	spin_lock_irqsave(&eni_dev->lock,flags);
1495	events = xchg(&eni_dev->events,0);
1496	spin_unlock_irqrestore(&eni_dev->lock,flags);
1497	if (events & MID_RX_DMA_COMPLETE) {
1498		EVENT("INT: RX DMA complete, starting dequeue_rx\n",0,0);
1499		dequeue_rx(dev);
1500		EVENT("dequeue_rx done, starting poll_rx\n",0,0);
1501		poll_rx(dev);
1502		EVENT("poll_rx done\n",0,0);
1503		/* poll_tx ? */
1504	}
1505	if (events & MID_SERVICE) {
1506		EVENT("INT: service, starting get_service\n",0,0);
1507		get_service(dev);
1508		EVENT("get_service done, starting poll_rx\n",0,0);
1509		poll_rx(dev);
1510		EVENT("poll_rx done\n",0,0);
1511	}
1512 	if (events & MID_TX_DMA_COMPLETE) {
1513		EVENT("INT: TX DMA COMPLETE\n",0,0);
1514		dequeue_tx(dev);
1515	}
1516	if (events & MID_TX_COMPLETE) {
1517		EVENT("INT: TX COMPLETE\n",0,0);
1518tx_complete++;
1519		wake_up(&eni_dev->tx_wait);
1520		/* poll_rx ? */
1521	}
1522	if (events & (MID_DMA_ERR_ACK | MID_TX_IDENT_MISM | MID_TX_DMA_OVFL)) {
1523		EVENT("bug interrupt\n",0,0);
1524		bug_int(dev,events);
1525	}
1526	poll_tx(dev);
1527}
1528
1529
1530/*--------------------------------- entries ---------------------------------*/
1531
1532
1533static const char *media_name[] __devinitdata = {
1534    "MMF", "SMF", "MMF", "03?", /*  0- 3 */
1535    "UTP", "05?", "06?", "07?", /*  4- 7 */
1536    "TAXI","09?", "10?", "11?", /*  8-11 */
1537    "12?", "13?", "14?", "15?", /* 12-15 */
1538    "MMF", "SMF", "18?", "19?", /* 16-19 */
1539    "UTP", "21?", "22?", "23?", /* 20-23 */
1540    "24?", "25?", "26?", "27?", /* 24-27 */
1541    "28?", "29?", "30?", "31?"  /* 28-31 */
1542};
1543
1544
1545#define SET_SEPROM \
1546  ({ if (!error && !pci_error) { \
1547    pci_error = pci_write_config_byte(eni_dev->pci_dev,PCI_TONGA_CTRL,tonga); \
1548    udelay(10); /* 10 usecs */ \
1549  } })
1550#define GET_SEPROM \
1551  ({ if (!error && !pci_error) { \
1552    pci_error = pci_read_config_byte(eni_dev->pci_dev,PCI_TONGA_CTRL,&tonga); \
1553    udelay(10); /* 10 usecs */ \
1554  } })
1555
1556
1557static int __devinit get_esi_asic(struct atm_dev *dev)
1558{
1559	struct eni_dev *eni_dev;
1560	unsigned char tonga;
1561	int error,failed,pci_error;
1562	int address,i,j;
1563
1564	eni_dev = ENI_DEV(dev);
1565	error = pci_error = 0;
1566	tonga = SEPROM_MAGIC | SEPROM_DATA | SEPROM_CLK;
1567	SET_SEPROM;
1568	for (i = 0; i < ESI_LEN && !error && !pci_error; i++) {
1569		/* start operation */
1570		tonga |= SEPROM_DATA;
1571		SET_SEPROM;
1572		tonga |= SEPROM_CLK;
1573		SET_SEPROM;
1574		tonga &= ~SEPROM_DATA;
1575		SET_SEPROM;
1576		tonga &= ~SEPROM_CLK;
1577		SET_SEPROM;
1578		/* send address */
1579		address = ((i+SEPROM_ESI_BASE) << 1)+1;
1580		for (j = 7; j >= 0; j--) {
1581			tonga = (address >> j) & 1 ? tonga | SEPROM_DATA :
1582			    tonga & ~SEPROM_DATA;
1583			SET_SEPROM;
1584			tonga |= SEPROM_CLK;
1585			SET_SEPROM;
1586			tonga &= ~SEPROM_CLK;
1587			SET_SEPROM;
1588		}
1589		/* get ack */
1590		tonga |= SEPROM_DATA;
1591		SET_SEPROM;
1592		tonga |= SEPROM_CLK;
1593		SET_SEPROM;
1594		GET_SEPROM;
1595		failed = tonga & SEPROM_DATA;
1596		tonga &= ~SEPROM_CLK;
1597		SET_SEPROM;
1598		tonga |= SEPROM_DATA;
1599		SET_SEPROM;
1600		if (failed) error = -EIO;
1601		else {
1602			dev->esi[i] = 0;
1603			for (j = 7; j >= 0; j--) {
1604				dev->esi[i] <<= 1;
1605				tonga |= SEPROM_DATA;
1606				SET_SEPROM;
1607				tonga |= SEPROM_CLK;
1608				SET_SEPROM;
1609				GET_SEPROM;
1610				if (tonga & SEPROM_DATA) dev->esi[i] |= 1;
1611				tonga &= ~SEPROM_CLK;
1612				SET_SEPROM;
1613				tonga |= SEPROM_DATA;
1614				SET_SEPROM;
1615			}
1616			/* get ack */
1617			tonga |= SEPROM_DATA;
1618			SET_SEPROM;
1619			tonga |= SEPROM_CLK;
1620			SET_SEPROM;
1621			GET_SEPROM;
1622			if (!(tonga & SEPROM_DATA)) error = -EIO;
1623			tonga &= ~SEPROM_CLK;
1624			SET_SEPROM;
1625			tonga |= SEPROM_DATA;
1626			SET_SEPROM;
1627		}
1628		/* stop operation */
1629		tonga &= ~SEPROM_DATA;
1630		SET_SEPROM;
1631		tonga |= SEPROM_CLK;
1632		SET_SEPROM;
1633		tonga |= SEPROM_DATA;
1634		SET_SEPROM;
1635	}
1636	if (pci_error) {
1637		printk(KERN_ERR DEV_LABEL "(itf %d): error reading ESI "
1638		    "(0x%02x)\n",dev->number,pci_error);
1639		error = -EIO;
1640	}
1641	return error;
1642}
1643
1644
1645#undef SET_SEPROM
1646#undef GET_SEPROM
1647
1648
1649static int __devinit get_esi_fpga(struct atm_dev *dev, void __iomem *base)
1650{
1651	void __iomem *mac_base;
1652	int i;
1653
1654	mac_base = base+EPROM_SIZE-sizeof(struct midway_eprom);
1655	for (i = 0; i < ESI_LEN; i++) dev->esi[i] = readb(mac_base+(i^3));
1656	return 0;
1657}
1658
1659
1660static int __devinit eni_do_init(struct atm_dev *dev)
1661{
1662	struct midway_eprom __iomem *eprom;
1663	struct eni_dev *eni_dev;
1664	struct pci_dev *pci_dev;
1665	unsigned long real_base;
1666	void __iomem *base;
1667	int error,i,last;
1668
1669	DPRINTK(">eni_init\n");
1670	dev->ci_range.vpi_bits = 0;
1671	dev->ci_range.vci_bits = NR_VCI_LD;
1672	dev->link_rate = ATM_OC3_PCR;
1673	eni_dev = ENI_DEV(dev);
1674	pci_dev = eni_dev->pci_dev;
1675	real_base = pci_resource_start(pci_dev, 0);
1676	eni_dev->irq = pci_dev->irq;
1677	if ((error = pci_write_config_word(pci_dev,PCI_COMMAND,
1678	    PCI_COMMAND_MEMORY |
1679	    (eni_dev->asic ? PCI_COMMAND_PARITY | PCI_COMMAND_SERR : 0)))) {
1680		printk(KERN_ERR DEV_LABEL "(itf %d): can't enable memory "
1681		    "(0x%02x)\n",dev->number,error);
1682		return -EIO;
1683	}
1684	printk(KERN_NOTICE DEV_LABEL "(itf %d): rev.%d,base=0x%lx,irq=%d,",
1685	    dev->number,pci_dev->revision,real_base,eni_dev->irq);
1686	if (!(base = ioremap_nocache(real_base,MAP_MAX_SIZE))) {
1687		printk("\n");
1688		printk(KERN_ERR DEV_LABEL "(itf %d): can't set up page "
1689		    "mapping\n",dev->number);
1690		return error;
1691	}
1692	eni_dev->base_diff = real_base - (unsigned long) base;
1693	/* id may not be present in ASIC Tonga boards - check this @@@ */
1694	if (!eni_dev->asic) {
1695		eprom = (base+EPROM_SIZE-sizeof(struct midway_eprom));
1696		if (readl(&eprom->magic) != ENI155_MAGIC) {
1697			printk("\n");
1698			printk(KERN_ERR KERN_ERR DEV_LABEL "(itf %d): bad "
1699			    "magic - expected 0x%x, got 0x%x\n",dev->number,
1700			    ENI155_MAGIC,(unsigned) readl(&eprom->magic));
1701			error = -EINVAL;
1702			goto unmap;
1703		}
1704	}
1705	eni_dev->phy = base+PHY_BASE;
1706	eni_dev->reg = base+REG_BASE;
1707	eni_dev->ram = base+RAM_BASE;
1708	last = MAP_MAX_SIZE-RAM_BASE;
1709	for (i = last-RAM_INCREMENT; i >= 0; i -= RAM_INCREMENT) {
1710		writel(0x55555555,eni_dev->ram+i);
1711		if (readl(eni_dev->ram+i) != 0x55555555) last = i;
1712		else {
1713			writel(0xAAAAAAAA,eni_dev->ram+i);
1714			if (readl(eni_dev->ram+i) != 0xAAAAAAAA) last = i;
1715			else writel(i,eni_dev->ram+i);
1716		}
1717	}
1718	for (i = 0; i < last; i += RAM_INCREMENT)
1719		if (readl(eni_dev->ram+i) != i) break;
1720	eni_dev->mem = i;
1721	memset_io(eni_dev->ram,0,eni_dev->mem);
1722	/* TODO: should shrink allocation now */
1723	printk("mem=%dkB (",eni_dev->mem >> 10);
1724	/* TODO: check for non-SUNI, check for TAXI ? */
1725	if (!(eni_in(MID_RES_ID_MCON) & 0x200) != !eni_dev->asic) {
1726		printk(")\n");
1727		printk(KERN_ERR DEV_LABEL "(itf %d): ERROR - wrong id 0x%x\n",
1728		    dev->number,(unsigned) eni_in(MID_RES_ID_MCON));
1729		error = -EINVAL;
1730		goto unmap;
1731	}
1732	error = eni_dev->asic ? get_esi_asic(dev) : get_esi_fpga(dev,base);
1733	if (error)
1734		goto unmap;
1735	for (i = 0; i < ESI_LEN; i++)
1736		printk("%s%02X",i ? "-" : "",dev->esi[i]);
1737	printk(")\n");
1738	printk(KERN_NOTICE DEV_LABEL "(itf %d): %s,%s\n",dev->number,
1739	    eni_in(MID_RES_ID_MCON) & 0x200 ? "ASIC" : "FPGA",
1740	    media_name[eni_in(MID_RES_ID_MCON) & DAUGTHER_ID]);
1741
1742	error = suni_init(dev);
1743	if (error)
1744		goto unmap;
1745out:
1746	return error;
1747unmap:
1748	iounmap(base);
1749	goto out;
1750}
1751
1752
1753static int __devinit eni_start(struct atm_dev *dev)
1754{
1755	struct eni_dev *eni_dev;
1756
1757	void __iomem *buf;
1758	unsigned long buffer_mem;
1759	int error;
1760
1761	DPRINTK(">eni_start\n");
1762	eni_dev = ENI_DEV(dev);
1763	if (request_irq(eni_dev->irq,&eni_int,IRQF_SHARED,DEV_LABEL,dev)) {
1764		printk(KERN_ERR DEV_LABEL "(itf %d): IRQ%d is already in use\n",
1765		    dev->number,eni_dev->irq);
1766		error = -EAGAIN;
1767		goto out;
1768	}
1769	pci_set_master(eni_dev->pci_dev);
1770	if ((error = pci_write_config_word(eni_dev->pci_dev,PCI_COMMAND,
1771	    PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
1772	    (eni_dev->asic ? PCI_COMMAND_PARITY | PCI_COMMAND_SERR : 0)))) {
1773		printk(KERN_ERR DEV_LABEL "(itf %d): can't enable memory+"
1774		    "master (0x%02x)\n",dev->number,error);
1775		goto free_irq;
1776	}
1777	if ((error = pci_write_config_byte(eni_dev->pci_dev,PCI_TONGA_CTRL,
1778	    END_SWAP_DMA))) {
1779		printk(KERN_ERR DEV_LABEL "(itf %d): can't set endian swap "
1780		    "(0x%02x)\n",dev->number,error);
1781		goto free_irq;
1782	}
1783	/* determine addresses of internal tables */
1784	eni_dev->vci = eni_dev->ram;
1785	eni_dev->rx_dma = eni_dev->ram+NR_VCI*16;
1786	eni_dev->tx_dma = eni_dev->rx_dma+NR_DMA_RX*8;
1787	eni_dev->service = eni_dev->tx_dma+NR_DMA_TX*8;
1788	buf = eni_dev->service+NR_SERVICE*4;
1789	DPRINTK("vci 0x%lx,rx 0x%lx, tx 0x%lx,srv 0x%lx,buf 0x%lx\n",
1790	     eni_dev->vci,eni_dev->rx_dma,eni_dev->tx_dma,
1791	     eni_dev->service,buf);
1792	spin_lock_init(&eni_dev->lock);
1793	tasklet_init(&eni_dev->task,eni_tasklet,(unsigned long) dev);
1794	eni_dev->events = 0;
1795	/* initialize memory management */
1796	buffer_mem = eni_dev->mem - (buf - eni_dev->ram);
1797	eni_dev->free_list_size = buffer_mem/MID_MIN_BUF_SIZE/2;
1798	eni_dev->free_list = kmalloc(
1799	    sizeof(struct eni_free)*(eni_dev->free_list_size+1),GFP_KERNEL);
1800	if (!eni_dev->free_list) {
1801		printk(KERN_ERR DEV_LABEL "(itf %d): couldn't get free page\n",
1802		    dev->number);
1803		error = -ENOMEM;
1804		goto free_irq;
1805	}
1806	eni_dev->free_len = 0;
1807	eni_put_free(eni_dev,buf,buffer_mem);
1808	memset_io(eni_dev->vci,0,16*NR_VCI); /* clear VCI table */
1809	/*
1810	 * byte_addr  free (k)
1811	 * 0x00000000     512  VCI table
1812	 * 0x00004000	  496  RX DMA
1813	 * 0x00005000	  492  TX DMA
1814	 * 0x00006000	  488  service list
1815	 * 0x00007000	  484  buffers
1816	 * 0x00080000	    0  end (512kB)
1817	 */
1818	eni_out(0xffffffff,MID_IE);
1819	error = start_tx(dev);
1820	if (error) goto free_list;
1821	error = start_rx(dev);
1822	if (error) goto free_list;
1823	error = dev->phy->start(dev);
1824	if (error) goto free_list;
1825	eni_out(eni_in(MID_MC_S) | (1 << MID_INT_SEL_SHIFT) |
1826	    MID_TX_LOCK_MODE | MID_DMA_ENABLE | MID_TX_ENABLE | MID_RX_ENABLE,
1827	    MID_MC_S);
1828	    /* Tonga uses SBus INTReq1 */
1829	(void) eni_in(MID_ISA); /* clear Midway interrupts */
1830	return 0;
1831
1832free_list:
1833	kfree(eni_dev->free_list);
1834
1835free_irq:
1836	free_irq(eni_dev->irq, eni_dev);
1837
1838out:
1839	return error;
1840}
1841
1842
1843static void eni_close(struct atm_vcc *vcc)
1844{
1845	DPRINTK(">eni_close\n");
1846	if (!ENI_VCC(vcc)) return;
1847	clear_bit(ATM_VF_READY,&vcc->flags);
1848	close_rx(vcc);
1849	close_tx(vcc);
1850	DPRINTK("eni_close: done waiting\n");
1851	/* deallocate memory */
1852	kfree(ENI_VCC(vcc));
1853	vcc->dev_data = NULL;
1854	clear_bit(ATM_VF_ADDR,&vcc->flags);
1855	/*foo();*/
1856}
1857
1858
1859static int eni_open(struct atm_vcc *vcc)
1860{
1861	struct eni_dev *eni_dev;
1862	struct eni_vcc *eni_vcc;
1863	int error;
1864	short vpi = vcc->vpi;
1865	int vci = vcc->vci;
1866
1867	DPRINTK(">eni_open\n");
1868	EVENT("eni_open\n",0,0);
1869	if (!test_bit(ATM_VF_PARTIAL,&vcc->flags))
1870		vcc->dev_data = NULL;
1871	eni_dev = ENI_DEV(vcc->dev);
1872	if (vci != ATM_VPI_UNSPEC && vpi != ATM_VCI_UNSPEC)
1873		set_bit(ATM_VF_ADDR,&vcc->flags);
1874	if (vcc->qos.aal != ATM_AAL0 && vcc->qos.aal != ATM_AAL5)
1875		return -EINVAL;
1876	DPRINTK(DEV_LABEL "(itf %d): open %d.%d\n",vcc->dev->number,vcc->vpi,
1877	    vcc->vci);
1878	if (!test_bit(ATM_VF_PARTIAL,&vcc->flags)) {
1879		eni_vcc = kmalloc(sizeof(struct eni_vcc),GFP_KERNEL);
1880		if (!eni_vcc) return -ENOMEM;
1881		vcc->dev_data = eni_vcc;
1882		eni_vcc->tx = NULL; /* for eni_close after open_rx */
1883		if ((error = open_rx_first(vcc))) {
1884			eni_close(vcc);
1885			return error;
1886		}
1887		if ((error = open_tx_first(vcc))) {
1888			eni_close(vcc);
1889			return error;
1890		}
1891	}
1892	if (vci == ATM_VPI_UNSPEC || vpi == ATM_VCI_UNSPEC) return 0;
1893	if ((error = open_rx_second(vcc))) {
1894		eni_close(vcc);
1895		return error;
1896	}
1897	if ((error = open_tx_second(vcc))) {
1898		eni_close(vcc);
1899		return error;
1900	}
1901	set_bit(ATM_VF_READY,&vcc->flags);
1902	/* should power down SUNI while !ref_count @@@ */
1903	return 0;
1904}
1905
1906
1907static int eni_change_qos(struct atm_vcc *vcc,struct atm_qos *qos,int flgs)
1908{
1909	struct eni_dev *eni_dev = ENI_DEV(vcc->dev);
1910	struct eni_tx *tx = ENI_VCC(vcc)->tx;
1911	struct sk_buff *skb;
1912	int error,rate,rsv,shp;
1913
1914	if (qos->txtp.traffic_class == ATM_NONE) return 0;
1915	if (tx == eni_dev->ubr) return -EBADFD;
1916	rate = atm_pcr_goal(&qos->txtp);
1917	if (rate < 0) rate = -rate;
1918	rsv = shp = 0;
1919	if ((flgs & ATM_MF_DEC_RSV) && rate && rate < tx->reserved) rsv = 1;
1920	if ((flgs & ATM_MF_INC_RSV) && (!rate || rate > tx->reserved)) rsv = 1;
1921	if ((flgs & ATM_MF_DEC_SHP) && rate && rate < tx->shaping) shp = 1;
1922	if ((flgs & ATM_MF_INC_SHP) && (!rate || rate > tx->shaping)) shp = 1;
1923	if (!rsv && !shp) return 0;
1924	error = reserve_or_set_tx(vcc,&qos->txtp,rsv,shp);
1925	if (error) return error;
1926	if (shp && !(flgs & ATM_MF_IMMED)) return 0;
1927	/*
1928	 * Walk through the send buffer and patch the rate information in all
1929	 * segmentation buffer descriptors of this VCC.
1930	 */
1931	tasklet_disable(&eni_dev->task);
1932	skb_queue_walk(&eni_dev->tx_queue, skb) {
1933		void __iomem *dsc;
1934
1935		if (ATM_SKB(skb)->vcc != vcc) continue;
1936		dsc = tx->send+ENI_PRV_POS(skb)*4;
1937		writel((readl(dsc) & ~(MID_SEG_RATE | MID_SEG_PR)) |
1938		    (tx->prescaler << MID_SEG_PR_SHIFT) |
1939		    (tx->resolution << MID_SEG_RATE_SHIFT), dsc);
1940	}
1941	tasklet_enable(&eni_dev->task);
1942	return 0;
1943}
1944
1945
1946static int eni_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
1947{
1948	struct eni_dev *eni_dev = ENI_DEV(dev);
1949
1950	if (cmd == ENI_MEMDUMP) {
1951		if (!capable(CAP_NET_ADMIN)) return -EPERM;
1952		printk(KERN_WARNING "Please use /proc/atm/" DEV_LABEL ":%d "
1953		    "instead of obsolete ioctl ENI_MEMDUMP\n",dev->number);
1954		dump(dev);
1955		return 0;
1956	}
1957	if (cmd == ENI_SETMULT) {
1958		struct eni_multipliers mult;
1959
1960		if (!capable(CAP_NET_ADMIN)) return -EPERM;
1961		if (copy_from_user(&mult, arg,
1962		    sizeof(struct eni_multipliers)))
1963			return -EFAULT;
1964		if ((mult.tx && mult.tx <= 100) || (mult.rx &&mult.rx <= 100) ||
1965		    mult.tx > 65536 || mult.rx > 65536)
1966			return -EINVAL;
1967		if (mult.tx) eni_dev->tx_mult = mult.tx;
1968		if (mult.rx) eni_dev->rx_mult = mult.rx;
1969		return 0;
1970	}
1971	if (cmd == ATM_SETCIRANGE) {
1972		struct atm_cirange ci;
1973
1974		if (copy_from_user(&ci, arg,sizeof(struct atm_cirange)))
1975			return -EFAULT;
1976		if ((ci.vpi_bits == 0 || ci.vpi_bits == ATM_CI_MAX) &&
1977		    (ci.vci_bits == NR_VCI_LD || ci.vpi_bits == ATM_CI_MAX))
1978		    return 0;
1979		return -EINVAL;
1980	}
1981	if (!dev->phy->ioctl) return -ENOIOCTLCMD;
1982	return dev->phy->ioctl(dev,cmd,arg);
1983}
1984
1985
1986static int eni_getsockopt(struct atm_vcc *vcc,int level,int optname,
1987    void __user *optval,int optlen)
1988{
1989	return -EINVAL;
1990}
1991
1992
1993static int eni_setsockopt(struct atm_vcc *vcc,int level,int optname,
1994    void __user *optval,unsigned int optlen)
1995{
1996	return -EINVAL;
1997}
1998
1999
2000static int eni_send(struct atm_vcc *vcc,struct sk_buff *skb)
2001{
2002	enum enq_res res;
2003
2004	DPRINTK(">eni_send\n");
2005	if (!ENI_VCC(vcc)->tx) {
2006		if (vcc->pop) vcc->pop(vcc,skb);
2007		else dev_kfree_skb(skb);
2008		return -EINVAL;
2009	}
2010	if (!skb) {
2011		printk(KERN_CRIT "!skb in eni_send ?\n");
2012		if (vcc->pop) vcc->pop(vcc,skb);
2013		return -EINVAL;
2014	}
2015	if (vcc->qos.aal == ATM_AAL0) {
2016		if (skb->len != ATM_CELL_SIZE-1) {
2017			if (vcc->pop) vcc->pop(vcc,skb);
2018			else dev_kfree_skb(skb);
2019			return -EINVAL;
2020		}
2021		*(u32 *) skb->data = htonl(*(u32 *) skb->data);
2022	}
2023submitted++;
2024	ATM_SKB(skb)->vcc = vcc;
2025	tasklet_disable(&ENI_DEV(vcc->dev)->task);
2026	res = do_tx(skb);
2027	tasklet_enable(&ENI_DEV(vcc->dev)->task);
2028	if (res == enq_ok) return 0;
2029	skb_queue_tail(&ENI_VCC(vcc)->tx->backlog,skb);
2030backlogged++;
2031	tasklet_schedule(&ENI_DEV(vcc->dev)->task);
2032	return 0;
2033}
2034
2035static void eni_phy_put(struct atm_dev *dev,unsigned char value,
2036    unsigned long addr)
2037{
2038	writel(value,ENI_DEV(dev)->phy+addr*4);
2039}
2040
2041
2042
2043static unsigned char eni_phy_get(struct atm_dev *dev,unsigned long addr)
2044{
2045	return readl(ENI_DEV(dev)->phy+addr*4);
2046}
2047
2048
2049static int eni_proc_read(struct atm_dev *dev,loff_t *pos,char *page)
2050{
2051	struct hlist_node *node;
2052	struct sock *s;
2053	static const char *signal[] = { "LOST","unknown","okay" };
2054	struct eni_dev *eni_dev = ENI_DEV(dev);
2055	struct atm_vcc *vcc;
2056	int left,i;
2057
2058	left = *pos;
2059	if (!left)
2060		return sprintf(page,DEV_LABEL "(itf %d) signal %s, %dkB, "
2061		    "%d cps remaining\n",dev->number,signal[(int) dev->signal],
2062		    eni_dev->mem >> 10,eni_dev->tx_bw);
2063	if (!--left)
2064		return sprintf(page,"%4sBursts: TX"
2065#if !defined(CONFIG_ATM_ENI_BURST_TX_16W) && !defined(CONFIG_ATM_ENI_BURST_TX_8W) && \
2066	!defined(CONFIG_ATM_ENI_BURST_TX_4W) && !defined(CONFIG_ATM_ENI_BURST_TX_2W)
2067		    " none"
2068#endif
2069#ifdef CONFIG_ATM_ENI_BURST_TX_16W
2070		    " 16W"
2071#endif
2072#ifdef CONFIG_ATM_ENI_BURST_TX_8W
2073		    " 8W"
2074#endif
2075#ifdef CONFIG_ATM_ENI_BURST_TX_4W
2076		    " 4W"
2077#endif
2078#ifdef CONFIG_ATM_ENI_BURST_TX_2W
2079		    " 2W"
2080#endif
2081		    ", RX"
2082#if !defined(CONFIG_ATM_ENI_BURST_RX_16W) && !defined(CONFIG_ATM_ENI_BURST_RX_8W) && \
2083	!defined(CONFIG_ATM_ENI_BURST_RX_4W) && !defined(CONFIG_ATM_ENI_BURST_RX_2W)
2084		    " none"
2085#endif
2086#ifdef CONFIG_ATM_ENI_BURST_RX_16W
2087		    " 16W"
2088#endif
2089#ifdef CONFIG_ATM_ENI_BURST_RX_8W
2090		    " 8W"
2091#endif
2092#ifdef CONFIG_ATM_ENI_BURST_RX_4W
2093		    " 4W"
2094#endif
2095#ifdef CONFIG_ATM_ENI_BURST_RX_2W
2096		    " 2W"
2097#endif
2098#ifndef CONFIG_ATM_ENI_TUNE_BURST
2099		    " (default)"
2100#endif
2101		    "\n","");
2102	if (!--left)
2103		return sprintf(page,"%4sBuffer multipliers: tx %d%%, rx %d%%\n",
2104		    "",eni_dev->tx_mult,eni_dev->rx_mult);
2105	for (i = 0; i < NR_CHAN; i++) {
2106		struct eni_tx *tx = eni_dev->tx+i;
2107
2108		if (!tx->send) continue;
2109		if (!--left) {
2110			return sprintf(page,"tx[%d]:    0x%ld-0x%ld "
2111			    "(%6ld bytes), rsv %d cps, shp %d cps%s\n",i,
2112			    (unsigned long) (tx->send - eni_dev->ram),
2113			    tx->send-eni_dev->ram+tx->words*4-1,tx->words*4,
2114			    tx->reserved,tx->shaping,
2115			    tx == eni_dev->ubr ? " (UBR)" : "");
2116		}
2117		if (--left) continue;
2118		return sprintf(page,"%10sbacklog %u packets\n","",
2119		    skb_queue_len(&tx->backlog));
2120	}
2121	read_lock(&vcc_sklist_lock);
2122	for(i = 0; i < VCC_HTABLE_SIZE; ++i) {
2123		struct hlist_head *head = &vcc_hash[i];
2124
2125		sk_for_each(s, node, head) {
2126			struct eni_vcc *eni_vcc;
2127			int length;
2128
2129			vcc = atm_sk(s);
2130			if (vcc->dev != dev)
2131				continue;
2132			eni_vcc = ENI_VCC(vcc);
2133			if (--left) continue;
2134			length = sprintf(page,"vcc %4d: ",vcc->vci);
2135			if (eni_vcc->rx) {
2136				length += sprintf(page+length,"0x%ld-0x%ld "
2137				    "(%6ld bytes)",
2138				    (unsigned long) (eni_vcc->recv - eni_dev->ram),
2139				    eni_vcc->recv-eni_dev->ram+eni_vcc->words*4-1,
2140				    eni_vcc->words*4);
2141				if (eni_vcc->tx) length += sprintf(page+length,", ");
2142			}
2143			if (eni_vcc->tx)
2144				length += sprintf(page+length,"tx[%d], txing %d bytes",
2145				    eni_vcc->tx->index,eni_vcc->txing);
2146			page[length] = '\n';
2147			read_unlock(&vcc_sklist_lock);
2148			return length+1;
2149		}
2150	}
2151	read_unlock(&vcc_sklist_lock);
2152	for (i = 0; i < eni_dev->free_len; i++) {
2153		struct eni_free *fe = eni_dev->free_list+i;
2154		unsigned long offset;
2155
2156		if (--left) continue;
2157		offset = (unsigned long) eni_dev->ram+eni_dev->base_diff;
2158		return sprintf(page,"free      %p-%p (%6d bytes)\n",
2159		    fe->start-offset,fe->start-offset+(1 << fe->order)-1,
2160		    1 << fe->order);
2161	}
2162	return 0;
2163}
2164
2165
2166static const struct atmdev_ops ops = {
2167	.open		= eni_open,
2168	.close		= eni_close,
2169	.ioctl		= eni_ioctl,
2170	.getsockopt	= eni_getsockopt,
2171	.setsockopt	= eni_setsockopt,
2172	.send		= eni_send,
2173	.phy_put	= eni_phy_put,
2174	.phy_get	= eni_phy_get,
2175	.change_qos	= eni_change_qos,
2176	.proc_read	= eni_proc_read
2177};
2178
2179
2180static int __devinit eni_init_one(struct pci_dev *pci_dev,
2181    const struct pci_device_id *ent)
2182{
2183	struct atm_dev *dev;
2184	struct eni_dev *eni_dev;
2185	int error = -ENOMEM;
2186
2187	DPRINTK("eni_init_one\n");
2188
2189	if (pci_enable_device(pci_dev)) {
2190		error = -EIO;
2191		goto out0;
2192	}
2193
2194	eni_dev = kmalloc(sizeof(struct eni_dev),GFP_KERNEL);
2195	if (!eni_dev) goto out0;
2196	if (!cpu_zeroes) {
2197		cpu_zeroes = pci_alloc_consistent(pci_dev,ENI_ZEROES_SIZE,
2198		    &zeroes);
2199		if (!cpu_zeroes) goto out1;
2200	}
2201	dev = atm_dev_register(DEV_LABEL,&ops,-1,NULL);
2202	if (!dev) goto out2;
2203	pci_set_drvdata(pci_dev, dev);
2204	eni_dev->pci_dev = pci_dev;
2205	dev->dev_data = eni_dev;
2206	eni_dev->asic = ent->driver_data;
2207	error = eni_do_init(dev);
2208	if (error) goto out3;
2209	error = eni_start(dev);
2210	if (error) goto out3;
2211	eni_dev->more = eni_boards;
2212	eni_boards = dev;
2213	return 0;
2214out3:
2215	atm_dev_deregister(dev);
2216out2:
2217	pci_free_consistent(eni_dev->pci_dev,ENI_ZEROES_SIZE,cpu_zeroes,zeroes);
2218	cpu_zeroes = NULL;
2219out1:
2220	kfree(eni_dev);
2221out0:
2222	return error;
2223}
2224
2225
2226static struct pci_device_id eni_pci_tbl[] = {
2227	{ PCI_VDEVICE(EF, PCI_DEVICE_ID_EF_ATM_FPGA), 0 /* FPGA */ },
2228	{ PCI_VDEVICE(EF, PCI_DEVICE_ID_EF_ATM_ASIC), 1 /* ASIC */ },
2229	{ 0, }
2230};
2231MODULE_DEVICE_TABLE(pci,eni_pci_tbl);
2232
2233
2234static void __devexit eni_remove_one(struct pci_dev *pci_dev)
2235{
2236	/* grrr */
2237}
2238
2239
2240static struct pci_driver eni_driver = {
2241	.name		= DEV_LABEL,
2242	.id_table	= eni_pci_tbl,
2243	.probe		= eni_init_one,
2244	.remove		= __devexit_p(eni_remove_one),
2245};
2246
2247
2248static int __init eni_init(void)
2249{
2250	struct sk_buff *skb; /* dummy for sizeof */
2251
2252	if (sizeof(skb->cb) < sizeof(struct eni_skb_prv)) {
2253		printk(KERN_ERR "eni_detect: skb->cb is too small (%Zd < %Zd)\n",
2254		    sizeof(skb->cb),sizeof(struct eni_skb_prv));
2255		return -EIO;
2256	}
2257	return pci_register_driver(&eni_driver);
2258}
2259
2260
2261module_init(eni_init);
2262/* @@@ since exit routine not defined, this module can not be unloaded */
2263
2264MODULE_LICENSE("GPL");
2265