ia64/linux-2.6.18-xen.hg

view drivers/atm/eni.c @ 897:329ea0ccb344

balloon: try harder to balloon up under memory pressure.

Currently if the balloon driver is unable to increase the guest's
reservation it assumes the failure was due to reaching its full
allocation, gives up on the ballooning operation and records the limit
it reached as the "hard limit". The driver will not try again until
the target is set again (even to the same value).

However it is possible that ballooning has in fact failed due to
memory pressure in the host and therefore it is desirable to keep
attempting to reach the target in case memory becomes available. The
most likely scenario is that some guests are ballooning down while
others are ballooning up and therefore there is temporary memory
pressure while things stabilise. You would not expect a well behaved
toolstack to ask a domain to balloon to more than its allocation nor
would you expect it to deliberately over-commit memory by setting
balloon targets which exceed the total host memory.

This patch drops the concept of a hard limit and causes the balloon
driver to retry increasing the reservation on a timer in the same
manner as when decreasing the reservation.

Also if we partially succeed in increasing the reservation
(i.e. receive less pages than we asked for) then we may as well keep
those pages rather than returning them to Xen.

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