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