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