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