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