ia64/linux-2.6.18-xen.hg

view drivers/atm/zatm.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/zatm.c - ZeitNet ZN122x device driver */
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
6 #include <linux/module.h>
7 #include <linux/sched.h>
8 #include <linux/kernel.h>
9 #include <linux/mm.h>
10 #include <linux/pci.h>
11 #include <linux/errno.h>
12 #include <linux/atm.h>
13 #include <linux/atmdev.h>
14 #include <linux/sonet.h>
15 #include <linux/skbuff.h>
16 #include <linux/netdevice.h>
17 #include <linux/delay.h>
18 #include <linux/uio.h>
19 #include <linux/init.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/atm_zatm.h>
22 #include <linux/capability.h>
23 #include <linux/bitops.h>
24 #include <linux/wait.h>
25 #include <asm/byteorder.h>
26 #include <asm/system.h>
27 #include <asm/string.h>
28 #include <asm/io.h>
29 #include <asm/atomic.h>
30 #include <asm/uaccess.h>
32 #include "uPD98401.h"
33 #include "uPD98402.h"
34 #include "zeprom.h"
35 #include "zatm.h"
38 /*
39 * TODO:
40 *
41 * Minor features
42 * - support 64 kB SDUs (will have to use multibuffer batches then :-( )
43 * - proper use of CDV, credit = max(1,CDVT*PCR)
44 * - AAL0
45 * - better receive timestamps
46 * - OAM
47 */
49 #define ZATM_COPPER 1
51 #if 0
52 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
53 #else
54 #define DPRINTK(format,args...)
55 #endif
57 #ifndef CONFIG_ATM_ZATM_DEBUG
60 #define NULLCHECK(x)
62 #define EVENT(s,a,b)
65 static void event_dump(void)
66 {
67 }
70 #else
73 /*
74 * NULL pointer checking
75 */
77 #define NULLCHECK(x) \
78 if ((unsigned long) (x) < 0x30) printk(KERN_CRIT #x "==0x%x\n", (int) (x))
80 /*
81 * Very extensive activity logging. Greatly improves bug detection speed but
82 * costs a few Mbps if enabled.
83 */
85 #define EV 64
87 static const char *ev[EV];
88 static unsigned long ev_a[EV],ev_b[EV];
89 static int ec = 0;
92 static void EVENT(const char *s,unsigned long a,unsigned long b)
93 {
94 ev[ec] = s;
95 ev_a[ec] = a;
96 ev_b[ec] = b;
97 ec = (ec+1) % EV;
98 }
101 static void event_dump(void)
102 {
103 int n,i;
105 printk(KERN_NOTICE "----- event dump follows -----\n");
106 for (n = 0; n < EV; n++) {
107 i = (ec+n) % EV;
108 printk(KERN_NOTICE);
109 printk(ev[i] ? ev[i] : "(null)",ev_a[i],ev_b[i]);
110 }
111 printk(KERN_NOTICE "----- event dump ends here -----\n");
112 }
115 #endif /* CONFIG_ATM_ZATM_DEBUG */
118 #define RING_BUSY 1 /* indication from do_tx that PDU has to be
119 backlogged */
121 static struct atm_dev *zatm_boards = NULL;
122 static unsigned long dummy[2] = {0,0};
125 #define zin_n(r) inl(zatm_dev->base+r*4)
126 #define zin(r) inl(zatm_dev->base+uPD98401_##r*4)
127 #define zout(v,r) outl(v,zatm_dev->base+uPD98401_##r*4)
128 #define zwait while (zin(CMR) & uPD98401_BUSY)
130 /* RX0, RX1, TX0, TX1 */
131 static const int mbx_entries[NR_MBX] = { 1024,1024,1024,1024 };
132 static const int mbx_esize[NR_MBX] = { 16,16,4,4 }; /* entry size in bytes */
134 #define MBX_SIZE(i) (mbx_entries[i]*mbx_esize[i])
137 /*-------------------------------- utilities --------------------------------*/
140 static void zpokel(struct zatm_dev *zatm_dev,u32 value,u32 addr)
141 {
142 zwait;
143 zout(value,CER);
144 zout(uPD98401_IND_ACC | uPD98401_IA_BALL |
145 (uPD98401_IA_TGT_CM << uPD98401_IA_TGT_SHIFT) | addr,CMR);
146 }
149 static u32 zpeekl(struct zatm_dev *zatm_dev,u32 addr)
150 {
151 zwait;
152 zout(uPD98401_IND_ACC | uPD98401_IA_BALL | uPD98401_IA_RW |
153 (uPD98401_IA_TGT_CM << uPD98401_IA_TGT_SHIFT) | addr,CMR);
154 zwait;
155 return zin(CER);
156 }
159 /*------------------------------- free lists --------------------------------*/
162 /*
163 * Free buffer head structure:
164 * [0] pointer to buffer (for SAR)
165 * [1] buffer descr link pointer (for SAR)
166 * [2] back pointer to skb (for poll_rx)
167 * [3] data
168 * ...
169 */
171 struct rx_buffer_head {
172 u32 buffer; /* pointer to buffer (for SAR) */
173 u32 link; /* buffer descriptor link pointer (for SAR) */
174 struct sk_buff *skb; /* back pointer to skb (for poll_rx) */
175 };
178 static void refill_pool(struct atm_dev *dev,int pool)
179 {
180 struct zatm_dev *zatm_dev;
181 struct sk_buff *skb;
182 struct rx_buffer_head *first;
183 unsigned long flags;
184 int align,offset,free,count,size;
186 EVENT("refill_pool\n",0,0);
187 zatm_dev = ZATM_DEV(dev);
188 size = (64 << (pool <= ZATM_AAL5_POOL_BASE ? 0 :
189 pool-ZATM_AAL5_POOL_BASE))+sizeof(struct rx_buffer_head);
190 if (size < PAGE_SIZE) {
191 align = 32; /* for 32 byte alignment */
192 offset = sizeof(struct rx_buffer_head);
193 }
194 else {
195 align = 4096;
196 offset = zatm_dev->pool_info[pool].offset+
197 sizeof(struct rx_buffer_head);
198 }
199 size += align;
200 spin_lock_irqsave(&zatm_dev->lock, flags);
201 free = zpeekl(zatm_dev,zatm_dev->pool_base+2*pool) &
202 uPD98401_RXFP_REMAIN;
203 spin_unlock_irqrestore(&zatm_dev->lock, flags);
204 if (free >= zatm_dev->pool_info[pool].low_water) return;
205 EVENT("starting ... POOL: 0x%x, 0x%x\n",
206 zpeekl(zatm_dev,zatm_dev->pool_base+2*pool),
207 zpeekl(zatm_dev,zatm_dev->pool_base+2*pool+1));
208 EVENT("dummy: 0x%08lx, 0x%08lx\n",dummy[0],dummy[1]);
209 count = 0;
210 first = NULL;
211 while (free < zatm_dev->pool_info[pool].high_water) {
212 struct rx_buffer_head *head;
214 skb = alloc_skb(size,GFP_ATOMIC);
215 if (!skb) {
216 printk(KERN_WARNING DEV_LABEL "(Itf %d): got no new "
217 "skb (%d) with %d free\n",dev->number,size,free);
218 break;
219 }
220 skb_reserve(skb,(unsigned char *) ((((unsigned long) skb->data+
221 align+offset-1) & ~(unsigned long) (align-1))-offset)-
222 skb->data);
223 head = (struct rx_buffer_head *) skb->data;
224 skb_reserve(skb,sizeof(struct rx_buffer_head));
225 if (!first) first = head;
226 count++;
227 head->buffer = virt_to_bus(skb->data);
228 head->link = 0;
229 head->skb = skb;
230 EVENT("enq skb 0x%08lx/0x%08lx\n",(unsigned long) skb,
231 (unsigned long) head);
232 spin_lock_irqsave(&zatm_dev->lock, flags);
233 if (zatm_dev->last_free[pool])
234 ((struct rx_buffer_head *) (zatm_dev->last_free[pool]->
235 data))[-1].link = virt_to_bus(head);
236 zatm_dev->last_free[pool] = skb;
237 skb_queue_tail(&zatm_dev->pool[pool],skb);
238 spin_unlock_irqrestore(&zatm_dev->lock, flags);
239 free++;
240 }
241 if (first) {
242 spin_lock_irqsave(&zatm_dev->lock, flags);
243 zwait;
244 zout(virt_to_bus(first),CER);
245 zout(uPD98401_ADD_BAT | (pool << uPD98401_POOL_SHIFT) | count,
246 CMR);
247 spin_unlock_irqrestore(&zatm_dev->lock, flags);
248 EVENT ("POOL: 0x%x, 0x%x\n",
249 zpeekl(zatm_dev,zatm_dev->pool_base+2*pool),
250 zpeekl(zatm_dev,zatm_dev->pool_base+2*pool+1));
251 EVENT("dummy: 0x%08lx, 0x%08lx\n",dummy[0],dummy[1]);
252 }
253 }
256 static void drain_free(struct atm_dev *dev,int pool)
257 {
258 skb_queue_purge(&ZATM_DEV(dev)->pool[pool]);
259 }
262 static int pool_index(int max_pdu)
263 {
264 int i;
266 if (max_pdu % ATM_CELL_PAYLOAD)
267 printk(KERN_ERR DEV_LABEL ": driver error in pool_index: "
268 "max_pdu is %d\n",max_pdu);
269 if (max_pdu > 65536) return -1;
270 for (i = 0; (64 << i) < max_pdu; i++);
271 return i+ZATM_AAL5_POOL_BASE;
272 }
275 /* use_pool isn't reentrant */
278 static void use_pool(struct atm_dev *dev,int pool)
279 {
280 struct zatm_dev *zatm_dev;
281 unsigned long flags;
282 int size;
284 zatm_dev = ZATM_DEV(dev);
285 if (!(zatm_dev->pool_info[pool].ref_count++)) {
286 skb_queue_head_init(&zatm_dev->pool[pool]);
287 size = pool-ZATM_AAL5_POOL_BASE;
288 if (size < 0) size = 0; /* 64B... */
289 else if (size > 10) size = 10; /* ... 64kB */
290 spin_lock_irqsave(&zatm_dev->lock, flags);
291 zpokel(zatm_dev,((zatm_dev->pool_info[pool].low_water/4) <<
292 uPD98401_RXFP_ALERT_SHIFT) |
293 (1 << uPD98401_RXFP_BTSZ_SHIFT) |
294 (size << uPD98401_RXFP_BFSZ_SHIFT),
295 zatm_dev->pool_base+pool*2);
296 zpokel(zatm_dev,(unsigned long) dummy,zatm_dev->pool_base+
297 pool*2+1);
298 spin_unlock_irqrestore(&zatm_dev->lock, flags);
299 zatm_dev->last_free[pool] = NULL;
300 refill_pool(dev,pool);
301 }
302 DPRINTK("pool %d: %d\n",pool,zatm_dev->pool_info[pool].ref_count);
303 }
306 static void unuse_pool(struct atm_dev *dev,int pool)
307 {
308 if (!(--ZATM_DEV(dev)->pool_info[pool].ref_count))
309 drain_free(dev,pool);
310 }
312 /*----------------------------------- RX ------------------------------------*/
315 #if 0
316 static void exception(struct atm_vcc *vcc)
317 {
318 static int count = 0;
319 struct zatm_dev *zatm_dev = ZATM_DEV(vcc->dev);
320 struct zatm_vcc *zatm_vcc = ZATM_VCC(vcc);
321 unsigned long *qrp;
322 int i;
324 if (count++ > 2) return;
325 for (i = 0; i < 8; i++)
326 printk("TX%d: 0x%08lx\n",i,
327 zpeekl(zatm_dev,zatm_vcc->tx_chan*VC_SIZE/4+i));
328 for (i = 0; i < 5; i++)
329 printk("SH%d: 0x%08lx\n",i,
330 zpeekl(zatm_dev,uPD98401_IM(zatm_vcc->shaper)+16*i));
331 qrp = (unsigned long *) zpeekl(zatm_dev,zatm_vcc->tx_chan*VC_SIZE/4+
332 uPD98401_TXVC_QRP);
333 printk("qrp=0x%08lx\n",(unsigned long) qrp);
334 for (i = 0; i < 4; i++) printk("QRP[%d]: 0x%08lx",i,qrp[i]);
335 }
336 #endif
339 static const char *err_txt[] = {
340 "No error",
341 "RX buf underflow",
342 "RX FIFO overrun",
343 "Maximum len violation",
344 "CRC error",
345 "User abort",
346 "Length violation",
347 "T1 error",
348 "Deactivated",
349 "???",
350 "???",
351 "???",
352 "???",
353 "???",
354 "???",
355 "???"
356 };
359 static void poll_rx(struct atm_dev *dev,int mbx)
360 {
361 struct zatm_dev *zatm_dev;
362 unsigned long pos;
363 u32 x;
364 int error;
366 EVENT("poll_rx\n",0,0);
367 zatm_dev = ZATM_DEV(dev);
368 pos = (zatm_dev->mbx_start[mbx] & ~0xffffUL) | zin(MTA(mbx));
369 while (x = zin(MWA(mbx)), (pos & 0xffff) != x) {
370 u32 *here;
371 struct sk_buff *skb;
372 struct atm_vcc *vcc;
373 int cells,size,chan;
375 EVENT("MBX: host 0x%lx, nic 0x%x\n",pos,x);
376 here = (u32 *) pos;
377 if (((pos += 16) & 0xffff) == zatm_dev->mbx_end[mbx])
378 pos = zatm_dev->mbx_start[mbx];
379 cells = here[0] & uPD98401_AAL5_SIZE;
380 #if 0
381 printk("RX IND: 0x%x, 0x%x, 0x%x, 0x%x\n",here[0],here[1],here[2],here[3]);
382 {
383 unsigned long *x;
384 printk("POOL: 0x%08x, 0x%08x\n",zpeekl(zatm_dev,
385 zatm_dev->pool_base),
386 zpeekl(zatm_dev,zatm_dev->pool_base+1));
387 x = (unsigned long *) here[2];
388 printk("[0..3] = 0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx\n",
389 x[0],x[1],x[2],x[3]);
390 }
391 #endif
392 error = 0;
393 if (here[3] & uPD98401_AAL5_ERR) {
394 error = (here[3] & uPD98401_AAL5_ES) >>
395 uPD98401_AAL5_ES_SHIFT;
396 if (error == uPD98401_AAL5_ES_DEACT ||
397 error == uPD98401_AAL5_ES_FREE) continue;
398 }
399 EVENT("error code 0x%x/0x%x\n",(here[3] & uPD98401_AAL5_ES) >>
400 uPD98401_AAL5_ES_SHIFT,error);
401 skb = ((struct rx_buffer_head *) bus_to_virt(here[2]))->skb;
402 __net_timestamp(skb);
403 #if 0
404 printk("[-3..0] 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",((unsigned *) skb->data)[-3],
405 ((unsigned *) skb->data)[-2],((unsigned *) skb->data)[-1],
406 ((unsigned *) skb->data)[0]);
407 #endif
408 EVENT("skb 0x%lx, here 0x%lx\n",(unsigned long) skb,
409 (unsigned long) here);
410 #if 0
411 printk("dummy: 0x%08lx, 0x%08lx\n",dummy[0],dummy[1]);
412 #endif
413 size = error ? 0 : ntohs(((__be16 *) skb->data)[cells*
414 ATM_CELL_PAYLOAD/sizeof(u16)-3]);
415 EVENT("got skb 0x%lx, size %d\n",(unsigned long) skb,size);
416 chan = (here[3] & uPD98401_AAL5_CHAN) >>
417 uPD98401_AAL5_CHAN_SHIFT;
418 if (chan < zatm_dev->chans && zatm_dev->rx_map[chan]) {
419 int pos;
420 vcc = zatm_dev->rx_map[chan];
421 pos = ZATM_VCC(vcc)->pool;
422 if (skb == zatm_dev->last_free[pos])
423 zatm_dev->last_free[pos] = NULL;
424 skb_unlink(skb, zatm_dev->pool + pos);
425 }
426 else {
427 printk(KERN_ERR DEV_LABEL "(itf %d): RX indication "
428 "for non-existing channel\n",dev->number);
429 size = 0;
430 vcc = NULL;
431 event_dump();
432 }
433 if (error) {
434 static unsigned long silence = 0;
435 static int last_error = 0;
437 if (error != last_error ||
438 time_after(jiffies, silence) || silence == 0){
439 printk(KERN_WARNING DEV_LABEL "(itf %d): "
440 "chan %d error %s\n",dev->number,chan,
441 err_txt[error]);
442 last_error = error;
443 silence = (jiffies+2*HZ)|1;
444 }
445 size = 0;
446 }
447 if (size && (size > cells*ATM_CELL_PAYLOAD-ATM_AAL5_TRAILER ||
448 size <= (cells-1)*ATM_CELL_PAYLOAD-ATM_AAL5_TRAILER)) {
449 printk(KERN_ERR DEV_LABEL "(itf %d): size %d with %d "
450 "cells\n",dev->number,size,cells);
451 size = 0;
452 event_dump();
453 }
454 if (size > ATM_MAX_AAL5_PDU) {
455 printk(KERN_ERR DEV_LABEL "(itf %d): size too big "
456 "(%d)\n",dev->number,size);
457 size = 0;
458 event_dump();
459 }
460 if (!size) {
461 dev_kfree_skb_irq(skb);
462 if (vcc) atomic_inc(&vcc->stats->rx_err);
463 continue;
464 }
465 if (!atm_charge(vcc,skb->truesize)) {
466 dev_kfree_skb_irq(skb);
467 continue;
468 }
469 skb->len = size;
470 ATM_SKB(skb)->vcc = vcc;
471 vcc->push(vcc,skb);
472 atomic_inc(&vcc->stats->rx);
473 }
474 zout(pos & 0xffff,MTA(mbx));
475 #if 0 /* probably a stupid idea */
476 refill_pool(dev,zatm_vcc->pool);
477 /* maybe this saves us a few interrupts */
478 #endif
479 }
482 static int open_rx_first(struct atm_vcc *vcc)
483 {
484 struct zatm_dev *zatm_dev;
485 struct zatm_vcc *zatm_vcc;
486 unsigned long flags;
487 unsigned short chan;
488 int cells;
490 DPRINTK("open_rx_first (0x%x)\n",inb_p(0xc053));
491 zatm_dev = ZATM_DEV(vcc->dev);
492 zatm_vcc = ZATM_VCC(vcc);
493 zatm_vcc->rx_chan = 0;
494 if (vcc->qos.rxtp.traffic_class == ATM_NONE) return 0;
495 if (vcc->qos.aal == ATM_AAL5) {
496 if (vcc->qos.rxtp.max_sdu > 65464)
497 vcc->qos.rxtp.max_sdu = 65464;
498 /* fix this - we may want to receive 64kB SDUs
499 later */
500 cells = (vcc->qos.rxtp.max_sdu+ATM_AAL5_TRAILER+
501 ATM_CELL_PAYLOAD-1)/ATM_CELL_PAYLOAD;
502 zatm_vcc->pool = pool_index(cells*ATM_CELL_PAYLOAD);
503 }
504 else {
505 cells = 1;
506 zatm_vcc->pool = ZATM_AAL0_POOL;
507 }
508 if (zatm_vcc->pool < 0) return -EMSGSIZE;
509 spin_lock_irqsave(&zatm_dev->lock, flags);
510 zwait;
511 zout(uPD98401_OPEN_CHAN,CMR);
512 zwait;
513 DPRINTK("0x%x 0x%x\n",zin(CMR),zin(CER));
514 chan = (zin(CMR) & uPD98401_CHAN_ADDR) >> uPD98401_CHAN_ADDR_SHIFT;
515 spin_unlock_irqrestore(&zatm_dev->lock, flags);
516 DPRINTK("chan is %d\n",chan);
517 if (!chan) return -EAGAIN;
518 use_pool(vcc->dev,zatm_vcc->pool);
519 DPRINTK("pool %d\n",zatm_vcc->pool);
520 /* set up VC descriptor */
521 spin_lock_irqsave(&zatm_dev->lock, flags);
522 zpokel(zatm_dev,zatm_vcc->pool << uPD98401_RXVC_POOL_SHIFT,
523 chan*VC_SIZE/4);
524 zpokel(zatm_dev,uPD98401_RXVC_OD | (vcc->qos.aal == ATM_AAL5 ?
525 uPD98401_RXVC_AR : 0) | cells,chan*VC_SIZE/4+1);
526 zpokel(zatm_dev,0,chan*VC_SIZE/4+2);
527 zatm_vcc->rx_chan = chan;
528 zatm_dev->rx_map[chan] = vcc;
529 spin_unlock_irqrestore(&zatm_dev->lock, flags);
530 return 0;
531 }
534 static int open_rx_second(struct atm_vcc *vcc)
535 {
536 struct zatm_dev *zatm_dev;
537 struct zatm_vcc *zatm_vcc;
538 unsigned long flags;
539 int pos,shift;
541 DPRINTK("open_rx_second (0x%x)\n",inb_p(0xc053));
542 zatm_dev = ZATM_DEV(vcc->dev);
543 zatm_vcc = ZATM_VCC(vcc);
544 if (!zatm_vcc->rx_chan) return 0;
545 spin_lock_irqsave(&zatm_dev->lock, flags);
546 /* should also handle VPI @@@ */
547 pos = vcc->vci >> 1;
548 shift = (1-(vcc->vci & 1)) << 4;
549 zpokel(zatm_dev,(zpeekl(zatm_dev,pos) & ~(0xffff << shift)) |
550 ((zatm_vcc->rx_chan | uPD98401_RXLT_ENBL) << shift),pos);
551 spin_unlock_irqrestore(&zatm_dev->lock, flags);
552 return 0;
553 }
556 static void close_rx(struct atm_vcc *vcc)
557 {
558 struct zatm_dev *zatm_dev;
559 struct zatm_vcc *zatm_vcc;
560 unsigned long flags;
561 int pos,shift;
563 zatm_vcc = ZATM_VCC(vcc);
564 zatm_dev = ZATM_DEV(vcc->dev);
565 if (!zatm_vcc->rx_chan) return;
566 DPRINTK("close_rx\n");
567 /* disable receiver */
568 if (vcc->vpi != ATM_VPI_UNSPEC && vcc->vci != ATM_VCI_UNSPEC) {
569 spin_lock_irqsave(&zatm_dev->lock, flags);
570 pos = vcc->vci >> 1;
571 shift = (1-(vcc->vci & 1)) << 4;
572 zpokel(zatm_dev,zpeekl(zatm_dev,pos) & ~(0xffff << shift),pos);
573 zwait;
574 zout(uPD98401_NOP,CMR);
575 zwait;
576 zout(uPD98401_NOP,CMR);
577 spin_unlock_irqrestore(&zatm_dev->lock, flags);
578 }
579 spin_lock_irqsave(&zatm_dev->lock, flags);
580 zwait;
581 zout(uPD98401_DEACT_CHAN | uPD98401_CHAN_RT | (zatm_vcc->rx_chan <<
582 uPD98401_CHAN_ADDR_SHIFT),CMR);
583 zwait;
584 udelay(10); /* why oh why ... ? */
585 zout(uPD98401_CLOSE_CHAN | uPD98401_CHAN_RT | (zatm_vcc->rx_chan <<
586 uPD98401_CHAN_ADDR_SHIFT),CMR);
587 zwait;
588 if (!(zin(CMR) & uPD98401_CHAN_ADDR))
589 printk(KERN_CRIT DEV_LABEL "(itf %d): can't close RX channel "
590 "%d\n",vcc->dev->number,zatm_vcc->rx_chan);
591 spin_unlock_irqrestore(&zatm_dev->lock, flags);
592 zatm_dev->rx_map[zatm_vcc->rx_chan] = NULL;
593 zatm_vcc->rx_chan = 0;
594 unuse_pool(vcc->dev,zatm_vcc->pool);
595 }
598 static int start_rx(struct atm_dev *dev)
599 {
600 struct zatm_dev *zatm_dev;
601 int size,i;
603 DPRINTK("start_rx\n");
604 zatm_dev = ZATM_DEV(dev);
605 size = sizeof(struct atm_vcc *)*zatm_dev->chans;
606 zatm_dev->rx_map = (struct atm_vcc **) kmalloc(size,GFP_KERNEL);
607 if (!zatm_dev->rx_map) return -ENOMEM;
608 memset(zatm_dev->rx_map,0,size);
609 /* set VPI/VCI split (use all VCIs and give what's left to VPIs) */
610 zpokel(zatm_dev,(1 << dev->ci_range.vci_bits)-1,uPD98401_VRR);
611 /* prepare free buffer pools */
612 for (i = 0; i <= ZATM_LAST_POOL; i++) {
613 zatm_dev->pool_info[i].ref_count = 0;
614 zatm_dev->pool_info[i].rqa_count = 0;
615 zatm_dev->pool_info[i].rqu_count = 0;
616 zatm_dev->pool_info[i].low_water = LOW_MARK;
617 zatm_dev->pool_info[i].high_water = HIGH_MARK;
618 zatm_dev->pool_info[i].offset = 0;
619 zatm_dev->pool_info[i].next_off = 0;
620 zatm_dev->pool_info[i].next_cnt = 0;
621 zatm_dev->pool_info[i].next_thres = OFF_CNG_THRES;
622 }
623 return 0;
624 }
627 /*----------------------------------- TX ------------------------------------*/
630 static int do_tx(struct sk_buff *skb)
631 {
632 struct atm_vcc *vcc;
633 struct zatm_dev *zatm_dev;
634 struct zatm_vcc *zatm_vcc;
635 u32 *dsc;
636 unsigned long flags;
638 EVENT("do_tx\n",0,0);
639 DPRINTK("sending skb %p\n",skb);
640 vcc = ATM_SKB(skb)->vcc;
641 zatm_dev = ZATM_DEV(vcc->dev);
642 zatm_vcc = ZATM_VCC(vcc);
643 EVENT("iovcnt=%d\n",skb_shinfo(skb)->nr_frags,0);
644 spin_lock_irqsave(&zatm_dev->lock, flags);
645 if (!skb_shinfo(skb)->nr_frags) {
646 if (zatm_vcc->txing == RING_ENTRIES-1) {
647 spin_unlock_irqrestore(&zatm_dev->lock, flags);
648 return RING_BUSY;
649 }
650 zatm_vcc->txing++;
651 dsc = zatm_vcc->ring+zatm_vcc->ring_curr;
652 zatm_vcc->ring_curr = (zatm_vcc->ring_curr+RING_WORDS) &
653 (RING_ENTRIES*RING_WORDS-1);
654 dsc[1] = 0;
655 dsc[2] = skb->len;
656 dsc[3] = virt_to_bus(skb->data);
657 mb();
658 dsc[0] = uPD98401_TXPD_V | uPD98401_TXPD_DP | uPD98401_TXPD_SM
659 | (vcc->qos.aal == ATM_AAL5 ? uPD98401_TXPD_AAL5 : 0 |
660 (ATM_SKB(skb)->atm_options & ATM_ATMOPT_CLP ?
661 uPD98401_CLPM_1 : uPD98401_CLPM_0));
662 EVENT("dsc (0x%lx)\n",(unsigned long) dsc,0);
663 }
664 else {
665 printk("NONONONOO!!!!\n");
666 dsc = NULL;
667 #if 0
668 u32 *put;
669 int i;
671 dsc = kmalloc(uPD98401_TXPD_SIZE * 2 +
672 uPD98401_TXBD_SIZE * ATM_SKB(skb)->iovcnt, GFP_ATOMIC);
673 if (!dsc) {
674 if (vcc->pop)
675 vcc->pop(vcc, skb);
676 else
677 dev_kfree_skb_irq(skb);
678 return -EAGAIN;
679 }
680 /* @@@ should check alignment */
681 put = dsc+8;
682 dsc[0] = uPD98401_TXPD_V | uPD98401_TXPD_DP |
683 (vcc->aal == ATM_AAL5 ? uPD98401_TXPD_AAL5 : 0 |
684 (ATM_SKB(skb)->atm_options & ATM_ATMOPT_CLP ?
685 uPD98401_CLPM_1 : uPD98401_CLPM_0));
686 dsc[1] = 0;
687 dsc[2] = ATM_SKB(skb)->iovcnt * uPD98401_TXBD_SIZE;
688 dsc[3] = virt_to_bus(put);
689 for (i = 0; i < ATM_SKB(skb)->iovcnt; i++) {
690 *put++ = ((struct iovec *) skb->data)[i].iov_len;
691 *put++ = virt_to_bus(((struct iovec *)
692 skb->data)[i].iov_base);
693 }
694 put[-2] |= uPD98401_TXBD_LAST;
695 #endif
696 }
697 ZATM_PRV_DSC(skb) = dsc;
698 skb_queue_tail(&zatm_vcc->tx_queue,skb);
699 DPRINTK("QRP=0x%08lx\n",zpeekl(zatm_dev,zatm_vcc->tx_chan*VC_SIZE/4+
700 uPD98401_TXVC_QRP));
701 zwait;
702 zout(uPD98401_TX_READY | (zatm_vcc->tx_chan <<
703 uPD98401_CHAN_ADDR_SHIFT),CMR);
704 spin_unlock_irqrestore(&zatm_dev->lock, flags);
705 EVENT("done\n",0,0);
706 return 0;
707 }
710 static inline void dequeue_tx(struct atm_vcc *vcc)
711 {
712 struct zatm_vcc *zatm_vcc;
713 struct sk_buff *skb;
715 EVENT("dequeue_tx\n",0,0);
716 zatm_vcc = ZATM_VCC(vcc);
717 skb = skb_dequeue(&zatm_vcc->tx_queue);
718 if (!skb) {
719 printk(KERN_CRIT DEV_LABEL "(itf %d): dequeue_tx but not "
720 "txing\n",vcc->dev->number);
721 return;
722 }
723 #if 0 /* @@@ would fail on CLP */
724 if (*ZATM_PRV_DSC(skb) != (uPD98401_TXPD_V | uPD98401_TXPD_DP |
725 uPD98401_TXPD_SM | uPD98401_TXPD_AAL5)) printk("@#*$!!!! (%08x)\n",
726 *ZATM_PRV_DSC(skb));
727 #endif
728 *ZATM_PRV_DSC(skb) = 0; /* mark as invalid */
729 zatm_vcc->txing--;
730 if (vcc->pop) vcc->pop(vcc,skb);
731 else dev_kfree_skb_irq(skb);
732 while ((skb = skb_dequeue(&zatm_vcc->backlog)))
733 if (do_tx(skb) == RING_BUSY) {
734 skb_queue_head(&zatm_vcc->backlog,skb);
735 break;
736 }
737 atomic_inc(&vcc->stats->tx);
738 wake_up(&zatm_vcc->tx_wait);
739 }
742 static void poll_tx(struct atm_dev *dev,int mbx)
743 {
744 struct zatm_dev *zatm_dev;
745 unsigned long pos;
746 u32 x;
748 EVENT("poll_tx\n",0,0);
749 zatm_dev = ZATM_DEV(dev);
750 pos = (zatm_dev->mbx_start[mbx] & ~0xffffUL) | zin(MTA(mbx));
751 while (x = zin(MWA(mbx)), (pos & 0xffff) != x) {
752 int chan;
754 #if 1
755 u32 data,*addr;
757 EVENT("MBX: host 0x%lx, nic 0x%x\n",pos,x);
758 addr = (u32 *) pos;
759 data = *addr;
760 chan = (data & uPD98401_TXI_CONN) >> uPD98401_TXI_CONN_SHIFT;
761 EVENT("addr = 0x%lx, data = 0x%08x,",(unsigned long) addr,
762 data);
763 EVENT("chan = %d\n",chan,0);
764 #else
765 NO !
766 chan = (zatm_dev->mbx_start[mbx][pos >> 2] & uPD98401_TXI_CONN)
767 >> uPD98401_TXI_CONN_SHIFT;
768 #endif
769 if (chan < zatm_dev->chans && zatm_dev->tx_map[chan])
770 dequeue_tx(zatm_dev->tx_map[chan]);
771 else {
772 printk(KERN_CRIT DEV_LABEL "(itf %d): TX indication "
773 "for non-existing channel %d\n",dev->number,chan);
774 event_dump();
775 }
776 if (((pos += 4) & 0xffff) == zatm_dev->mbx_end[mbx])
777 pos = zatm_dev->mbx_start[mbx];
778 }
779 zout(pos & 0xffff,MTA(mbx));
780 }
783 /*
784 * BUG BUG BUG: Doesn't handle "new-style" rate specification yet.
785 */
787 static int alloc_shaper(struct atm_dev *dev,int *pcr,int min,int max,int ubr)
788 {
789 struct zatm_dev *zatm_dev;
790 unsigned long flags;
791 unsigned long i,m,c;
792 int shaper;
794 DPRINTK("alloc_shaper (min = %d, max = %d)\n",min,max);
795 zatm_dev = ZATM_DEV(dev);
796 if (!zatm_dev->free_shapers) return -EAGAIN;
797 for (shaper = 0; !((zatm_dev->free_shapers >> shaper) & 1); shaper++);
798 zatm_dev->free_shapers &= ~1 << shaper;
799 if (ubr) {
800 c = 5;
801 i = m = 1;
802 zatm_dev->ubr_ref_cnt++;
803 zatm_dev->ubr = shaper;
804 }
805 else {
806 if (min) {
807 if (min <= 255) {
808 i = min;
809 m = ATM_OC3_PCR;
810 }
811 else {
812 i = 255;
813 m = ATM_OC3_PCR*255/min;
814 }
815 }
816 else {
817 if (max > zatm_dev->tx_bw) max = zatm_dev->tx_bw;
818 if (max <= 255) {
819 i = max;
820 m = ATM_OC3_PCR;
821 }
822 else {
823 i = 255;
824 m = (ATM_OC3_PCR*255+max-1)/max;
825 }
826 }
827 if (i > m) {
828 printk(KERN_CRIT DEV_LABEL "shaper algorithm botched "
829 "[%d,%d] -> i=%ld,m=%ld\n",min,max,i,m);
830 m = i;
831 }
832 *pcr = i*ATM_OC3_PCR/m;
833 c = 20; /* @@@ should use max_cdv ! */
834 if ((min && *pcr < min) || (max && *pcr > max)) return -EINVAL;
835 if (zatm_dev->tx_bw < *pcr) return -EAGAIN;
836 zatm_dev->tx_bw -= *pcr;
837 }
838 spin_lock_irqsave(&zatm_dev->lock, flags);
839 DPRINTK("i = %d, m = %d, PCR = %d\n",i,m,*pcr);
840 zpokel(zatm_dev,(i << uPD98401_IM_I_SHIFT) | m,uPD98401_IM(shaper));
841 zpokel(zatm_dev,c << uPD98401_PC_C_SHIFT,uPD98401_PC(shaper));
842 zpokel(zatm_dev,0,uPD98401_X(shaper));
843 zpokel(zatm_dev,0,uPD98401_Y(shaper));
844 zpokel(zatm_dev,uPD98401_PS_E,uPD98401_PS(shaper));
845 spin_unlock_irqrestore(&zatm_dev->lock, flags);
846 return shaper;
847 }
850 static void dealloc_shaper(struct atm_dev *dev,int shaper)
851 {
852 struct zatm_dev *zatm_dev;
853 unsigned long flags;
855 zatm_dev = ZATM_DEV(dev);
856 if (shaper == zatm_dev->ubr) {
857 if (--zatm_dev->ubr_ref_cnt) return;
858 zatm_dev->ubr = -1;
859 }
860 spin_lock_irqsave(&zatm_dev->lock, flags);
861 zpokel(zatm_dev,zpeekl(zatm_dev,uPD98401_PS(shaper)) & ~uPD98401_PS_E,
862 uPD98401_PS(shaper));
863 spin_unlock_irqrestore(&zatm_dev->lock, flags);
864 zatm_dev->free_shapers |= 1 << shaper;
865 }
868 static void close_tx(struct atm_vcc *vcc)
869 {
870 struct zatm_dev *zatm_dev;
871 struct zatm_vcc *zatm_vcc;
872 unsigned long flags;
873 int chan;
875 zatm_vcc = ZATM_VCC(vcc);
876 zatm_dev = ZATM_DEV(vcc->dev);
877 chan = zatm_vcc->tx_chan;
878 if (!chan) return;
879 DPRINTK("close_tx\n");
880 if (skb_peek(&zatm_vcc->backlog)) {
881 printk("waiting for backlog to drain ...\n");
882 event_dump();
883 wait_event(zatm_vcc->tx_wait, !skb_peek(&zatm_vcc->backlog));
884 }
885 if (skb_peek(&zatm_vcc->tx_queue)) {
886 printk("waiting for TX queue to drain ...\n");
887 event_dump();
888 wait_event(zatm_vcc->tx_wait, !skb_peek(&zatm_vcc->tx_queue));
889 }
890 spin_lock_irqsave(&zatm_dev->lock, flags);
891 #if 0
892 zwait;
893 zout(uPD98401_DEACT_CHAN | (chan << uPD98401_CHAN_ADDR_SHIFT),CMR);
894 #endif
895 zwait;
896 zout(uPD98401_CLOSE_CHAN | (chan << uPD98401_CHAN_ADDR_SHIFT),CMR);
897 zwait;
898 if (!(zin(CMR) & uPD98401_CHAN_ADDR))
899 printk(KERN_CRIT DEV_LABEL "(itf %d): can't close TX channel "
900 "%d\n",vcc->dev->number,chan);
901 spin_unlock_irqrestore(&zatm_dev->lock, flags);
902 zatm_vcc->tx_chan = 0;
903 zatm_dev->tx_map[chan] = NULL;
904 if (zatm_vcc->shaper != zatm_dev->ubr) {
905 zatm_dev->tx_bw += vcc->qos.txtp.min_pcr;
906 dealloc_shaper(vcc->dev,zatm_vcc->shaper);
907 }
908 kfree(zatm_vcc->ring);
909 }
912 static int open_tx_first(struct atm_vcc *vcc)
913 {
914 struct zatm_dev *zatm_dev;
915 struct zatm_vcc *zatm_vcc;
916 unsigned long flags;
917 u32 *loop;
918 unsigned short chan;
919 int pcr,unlimited;
921 DPRINTK("open_tx_first\n");
922 zatm_dev = ZATM_DEV(vcc->dev);
923 zatm_vcc = ZATM_VCC(vcc);
924 zatm_vcc->tx_chan = 0;
925 if (vcc->qos.txtp.traffic_class == ATM_NONE) return 0;
926 spin_lock_irqsave(&zatm_dev->lock, flags);
927 zwait;
928 zout(uPD98401_OPEN_CHAN,CMR);
929 zwait;
930 DPRINTK("0x%x 0x%x\n",zin(CMR),zin(CER));
931 chan = (zin(CMR) & uPD98401_CHAN_ADDR) >> uPD98401_CHAN_ADDR_SHIFT;
932 spin_unlock_irqrestore(&zatm_dev->lock, flags);
933 DPRINTK("chan is %d\n",chan);
934 if (!chan) return -EAGAIN;
935 unlimited = vcc->qos.txtp.traffic_class == ATM_UBR &&
936 (!vcc->qos.txtp.max_pcr || vcc->qos.txtp.max_pcr == ATM_MAX_PCR ||
937 vcc->qos.txtp.max_pcr >= ATM_OC3_PCR);
938 if (unlimited && zatm_dev->ubr != -1) zatm_vcc->shaper = zatm_dev->ubr;
939 else {
940 if (unlimited) vcc->qos.txtp.max_sdu = ATM_MAX_AAL5_PDU;
941 if ((zatm_vcc->shaper = alloc_shaper(vcc->dev,&pcr,
942 vcc->qos.txtp.min_pcr,vcc->qos.txtp.max_pcr,unlimited))
943 < 0) {
944 close_tx(vcc);
945 return zatm_vcc->shaper;
946 }
947 if (pcr > ATM_OC3_PCR) pcr = ATM_OC3_PCR;
948 vcc->qos.txtp.min_pcr = vcc->qos.txtp.max_pcr = pcr;
949 }
950 zatm_vcc->tx_chan = chan;
951 skb_queue_head_init(&zatm_vcc->tx_queue);
952 init_waitqueue_head(&zatm_vcc->tx_wait);
953 /* initialize ring */
954 zatm_vcc->ring = kmalloc(RING_SIZE,GFP_KERNEL);
955 if (!zatm_vcc->ring) return -ENOMEM;
956 memset(zatm_vcc->ring,0,RING_SIZE);
957 loop = zatm_vcc->ring+RING_ENTRIES*RING_WORDS;
958 loop[0] = uPD98401_TXPD_V;
959 loop[1] = loop[2] = 0;
960 loop[3] = virt_to_bus(zatm_vcc->ring);
961 zatm_vcc->ring_curr = 0;
962 zatm_vcc->txing = 0;
963 skb_queue_head_init(&zatm_vcc->backlog);
964 zpokel(zatm_dev,virt_to_bus(zatm_vcc->ring),
965 chan*VC_SIZE/4+uPD98401_TXVC_QRP);
966 return 0;
967 }
970 static int open_tx_second(struct atm_vcc *vcc)
971 {
972 struct zatm_dev *zatm_dev;
973 struct zatm_vcc *zatm_vcc;
974 unsigned long flags;
976 DPRINTK("open_tx_second\n");
977 zatm_dev = ZATM_DEV(vcc->dev);
978 zatm_vcc = ZATM_VCC(vcc);
979 if (!zatm_vcc->tx_chan) return 0;
980 /* set up VC descriptor */
981 spin_lock_irqsave(&zatm_dev->lock, flags);
982 zpokel(zatm_dev,0,zatm_vcc->tx_chan*VC_SIZE/4);
983 zpokel(zatm_dev,uPD98401_TXVC_L | (zatm_vcc->shaper <<
984 uPD98401_TXVC_SHP_SHIFT) | (vcc->vpi << uPD98401_TXVC_VPI_SHIFT) |
985 vcc->vci,zatm_vcc->tx_chan*VC_SIZE/4+1);
986 zpokel(zatm_dev,0,zatm_vcc->tx_chan*VC_SIZE/4+2);
987 spin_unlock_irqrestore(&zatm_dev->lock, flags);
988 zatm_dev->tx_map[zatm_vcc->tx_chan] = vcc;
989 return 0;
990 }
993 static int start_tx(struct atm_dev *dev)
994 {
995 struct zatm_dev *zatm_dev;
996 int i;
998 DPRINTK("start_tx\n");
999 zatm_dev = ZATM_DEV(dev);
1000 zatm_dev->tx_map = (struct atm_vcc **) kmalloc(sizeof(struct atm_vcc *)*
1001 zatm_dev->chans,GFP_KERNEL);
1002 if (!zatm_dev->tx_map) return -ENOMEM;
1003 zatm_dev->tx_bw = ATM_OC3_PCR;
1004 zatm_dev->free_shapers = (1 << NR_SHAPERS)-1;
1005 zatm_dev->ubr = -1;
1006 zatm_dev->ubr_ref_cnt = 0;
1007 /* initialize shapers */
1008 for (i = 0; i < NR_SHAPERS; i++) zpokel(zatm_dev,0,uPD98401_PS(i));
1009 return 0;
1013 /*------------------------------- interrupts --------------------------------*/
1016 static irqreturn_t zatm_int(int irq,void *dev_id,struct pt_regs *regs)
1018 struct atm_dev *dev;
1019 struct zatm_dev *zatm_dev;
1020 u32 reason;
1021 int handled = 0;
1023 dev = dev_id;
1024 zatm_dev = ZATM_DEV(dev);
1025 while ((reason = zin(GSR))) {
1026 handled = 1;
1027 EVENT("reason 0x%x\n",reason,0);
1028 if (reason & uPD98401_INT_PI) {
1029 EVENT("PHY int\n",0,0);
1030 dev->phy->interrupt(dev);
1032 if (reason & uPD98401_INT_RQA) {
1033 unsigned long pools;
1034 int i;
1036 pools = zin(RQA);
1037 EVENT("RQA (0x%08x)\n",pools,0);
1038 for (i = 0; pools; i++) {
1039 if (pools & 1) {
1040 refill_pool(dev,i);
1041 zatm_dev->pool_info[i].rqa_count++;
1043 pools >>= 1;
1046 if (reason & uPD98401_INT_RQU) {
1047 unsigned long pools;
1048 int i;
1049 pools = zin(RQU);
1050 printk(KERN_WARNING DEV_LABEL "(itf %d): RQU 0x%08lx\n",
1051 dev->number,pools);
1052 event_dump();
1053 for (i = 0; pools; i++) {
1054 if (pools & 1) {
1055 refill_pool(dev,i);
1056 zatm_dev->pool_info[i].rqu_count++;
1058 pools >>= 1;
1061 /* don't handle RD */
1062 if (reason & uPD98401_INT_SPE)
1063 printk(KERN_ALERT DEV_LABEL "(itf %d): system parity "
1064 "error at 0x%08x\n",dev->number,zin(ADDR));
1065 if (reason & uPD98401_INT_CPE)
1066 printk(KERN_ALERT DEV_LABEL "(itf %d): control memory "
1067 "parity error at 0x%08x\n",dev->number,zin(ADDR));
1068 if (reason & uPD98401_INT_SBE) {
1069 printk(KERN_ALERT DEV_LABEL "(itf %d): system bus "
1070 "error at 0x%08x\n",dev->number,zin(ADDR));
1071 event_dump();
1073 /* don't handle IND */
1074 if (reason & uPD98401_INT_MF) {
1075 printk(KERN_CRIT DEV_LABEL "(itf %d): mailbox full "
1076 "(0x%x)\n",dev->number,(reason & uPD98401_INT_MF)
1077 >> uPD98401_INT_MF_SHIFT);
1078 event_dump();
1079 /* @@@ should try to recover */
1081 if (reason & uPD98401_INT_MM) {
1082 if (reason & 1) poll_rx(dev,0);
1083 if (reason & 2) poll_rx(dev,1);
1084 if (reason & 4) poll_tx(dev,2);
1085 if (reason & 8) poll_tx(dev,3);
1087 /* @@@ handle RCRn */
1089 return IRQ_RETVAL(handled);
1093 /*----------------------------- (E)EPROM access -----------------------------*/
1096 static void __devinit eprom_set(struct zatm_dev *zatm_dev,unsigned long value,
1097 unsigned short cmd)
1099 int error;
1101 if ((error = pci_write_config_dword(zatm_dev->pci_dev,cmd,value)))
1102 printk(KERN_ERR DEV_LABEL ": PCI write failed (0x%02x)\n",
1103 error);
1107 static unsigned long __devinit eprom_get(struct zatm_dev *zatm_dev,
1108 unsigned short cmd)
1110 unsigned int value;
1111 int error;
1113 if ((error = pci_read_config_dword(zatm_dev->pci_dev,cmd,&value)))
1114 printk(KERN_ERR DEV_LABEL ": PCI read failed (0x%02x)\n",
1115 error);
1116 return value;
1120 static void __devinit eprom_put_bits(struct zatm_dev *zatm_dev,
1121 unsigned long data,int bits,unsigned short cmd)
1123 unsigned long value;
1124 int i;
1126 for (i = bits-1; i >= 0; i--) {
1127 value = ZEPROM_CS | (((data >> i) & 1) ? ZEPROM_DI : 0);
1128 eprom_set(zatm_dev,value,cmd);
1129 eprom_set(zatm_dev,value | ZEPROM_SK,cmd);
1130 eprom_set(zatm_dev,value,cmd);
1135 static void __devinit eprom_get_byte(struct zatm_dev *zatm_dev,
1136 unsigned char *byte,unsigned short cmd)
1138 int i;
1140 *byte = 0;
1141 for (i = 8; i; i--) {
1142 eprom_set(zatm_dev,ZEPROM_CS,cmd);
1143 eprom_set(zatm_dev,ZEPROM_CS | ZEPROM_SK,cmd);
1144 *byte <<= 1;
1145 if (eprom_get(zatm_dev,cmd) & ZEPROM_DO) *byte |= 1;
1146 eprom_set(zatm_dev,ZEPROM_CS,cmd);
1151 static unsigned char __devinit eprom_try_esi(struct atm_dev *dev,
1152 unsigned short cmd,int offset,int swap)
1154 unsigned char buf[ZEPROM_SIZE];
1155 struct zatm_dev *zatm_dev;
1156 int i;
1158 zatm_dev = ZATM_DEV(dev);
1159 for (i = 0; i < ZEPROM_SIZE; i += 2) {
1160 eprom_set(zatm_dev,ZEPROM_CS,cmd); /* select EPROM */
1161 eprom_put_bits(zatm_dev,ZEPROM_CMD_READ,ZEPROM_CMD_LEN,cmd);
1162 eprom_put_bits(zatm_dev,i >> 1,ZEPROM_ADDR_LEN,cmd);
1163 eprom_get_byte(zatm_dev,buf+i+swap,cmd);
1164 eprom_get_byte(zatm_dev,buf+i+1-swap,cmd);
1165 eprom_set(zatm_dev,0,cmd); /* deselect EPROM */
1167 memcpy(dev->esi,buf+offset,ESI_LEN);
1168 return memcmp(dev->esi,"\0\0\0\0\0",ESI_LEN); /* assumes ESI_LEN == 6 */
1172 static void __devinit eprom_get_esi(struct atm_dev *dev)
1174 if (eprom_try_esi(dev,ZEPROM_V1_REG,ZEPROM_V1_ESI_OFF,1)) return;
1175 (void) eprom_try_esi(dev,ZEPROM_V2_REG,ZEPROM_V2_ESI_OFF,0);
1179 /*--------------------------------- entries ---------------------------------*/
1182 static int __init zatm_init(struct atm_dev *dev)
1184 struct zatm_dev *zatm_dev;
1185 struct pci_dev *pci_dev;
1186 unsigned short command;
1187 unsigned char revision;
1188 int error,i,last;
1189 unsigned long t0,t1,t2;
1191 DPRINTK(">zatm_init\n");
1192 zatm_dev = ZATM_DEV(dev);
1193 spin_lock_init(&zatm_dev->lock);
1194 pci_dev = zatm_dev->pci_dev;
1195 zatm_dev->base = pci_resource_start(pci_dev, 0);
1196 zatm_dev->irq = pci_dev->irq;
1197 if ((error = pci_read_config_word(pci_dev,PCI_COMMAND,&command)) ||
1198 (error = pci_read_config_byte(pci_dev,PCI_REVISION_ID,&revision))) {
1199 printk(KERN_ERR DEV_LABEL "(itf %d): init error 0x%02x\n",
1200 dev->number,error);
1201 return -EINVAL;
1203 if ((error = pci_write_config_word(pci_dev,PCI_COMMAND,
1204 command | PCI_COMMAND_IO | PCI_COMMAND_MASTER))) {
1205 printk(KERN_ERR DEV_LABEL "(itf %d): can't enable IO (0x%02x)"
1206 "\n",dev->number,error);
1207 return -EIO;
1209 eprom_get_esi(dev);
1210 printk(KERN_NOTICE DEV_LABEL "(itf %d): rev.%d,base=0x%x,irq=%d,",
1211 dev->number,revision,zatm_dev->base,zatm_dev->irq);
1212 /* reset uPD98401 */
1213 zout(0,SWR);
1214 while (!(zin(GSR) & uPD98401_INT_IND));
1215 zout(uPD98401_GMR_ONE /*uPD98401_BURST4*/,GMR);
1216 last = MAX_CRAM_SIZE;
1217 for (i = last-RAM_INCREMENT; i >= 0; i -= RAM_INCREMENT) {
1218 zpokel(zatm_dev,0x55555555,i);
1219 if (zpeekl(zatm_dev,i) != 0x55555555) last = i;
1220 else {
1221 zpokel(zatm_dev,0xAAAAAAAA,i);
1222 if (zpeekl(zatm_dev,i) != 0xAAAAAAAA) last = i;
1223 else zpokel(zatm_dev,i,i);
1226 for (i = 0; i < last; i += RAM_INCREMENT)
1227 if (zpeekl(zatm_dev,i) != i) break;
1228 zatm_dev->mem = i << 2;
1229 while (i) zpokel(zatm_dev,0,--i);
1230 /* reset again to rebuild memory pointers */
1231 zout(0,SWR);
1232 while (!(zin(GSR) & uPD98401_INT_IND));
1233 zout(uPD98401_GMR_ONE | uPD98401_BURST8 | uPD98401_BURST4 |
1234 uPD98401_BURST2 | uPD98401_GMR_PM | uPD98401_GMR_DR,GMR);
1235 /* TODO: should shrink allocation now */
1236 printk("mem=%dkB,%s (",zatm_dev->mem >> 10,zatm_dev->copper ? "UTP" :
1237 "MMF");
1238 for (i = 0; i < ESI_LEN; i++)
1239 printk("%02X%s",dev->esi[i],i == ESI_LEN-1 ? ")\n" : "-");
1240 do {
1241 unsigned long flags;
1243 spin_lock_irqsave(&zatm_dev->lock, flags);
1244 t0 = zpeekl(zatm_dev,uPD98401_TSR);
1245 udelay(10);
1246 t1 = zpeekl(zatm_dev,uPD98401_TSR);
1247 udelay(1010);
1248 t2 = zpeekl(zatm_dev,uPD98401_TSR);
1249 spin_unlock_irqrestore(&zatm_dev->lock, flags);
1251 while (t0 > t1 || t1 > t2); /* loop if wrapping ... */
1252 zatm_dev->khz = t2-2*t1+t0;
1253 printk(KERN_NOTICE DEV_LABEL "(itf %d): uPD98401 %d.%d at %d.%03d "
1254 "MHz\n",dev->number,
1255 (zin(VER) & uPD98401_MAJOR) >> uPD98401_MAJOR_SHIFT,
1256 zin(VER) & uPD98401_MINOR,zatm_dev->khz/1000,zatm_dev->khz % 1000);
1257 return uPD98402_init(dev);
1261 static int __init zatm_start(struct atm_dev *dev)
1263 struct zatm_dev *zatm_dev = ZATM_DEV(dev);
1264 struct pci_dev *pdev = zatm_dev->pci_dev;
1265 unsigned long curr;
1266 int pools,vccs,rx;
1267 int error, i, ld;
1269 DPRINTK("zatm_start\n");
1270 zatm_dev->rx_map = zatm_dev->tx_map = NULL;
1271 for (i = 0; i < NR_MBX; i++)
1272 zatm_dev->mbx_start[i] = 0;
1273 error = request_irq(zatm_dev->irq, zatm_int, IRQF_SHARED, DEV_LABEL, dev);
1274 if (error < 0) {
1275 printk(KERN_ERR DEV_LABEL "(itf %d): IRQ%d is already in use\n",
1276 dev->number,zatm_dev->irq);
1277 goto done;
1279 /* define memory regions */
1280 pools = NR_POOLS;
1281 if (NR_SHAPERS*SHAPER_SIZE > pools*POOL_SIZE)
1282 pools = NR_SHAPERS*SHAPER_SIZE/POOL_SIZE;
1283 vccs = (zatm_dev->mem-NR_SHAPERS*SHAPER_SIZE-pools*POOL_SIZE)/
1284 (2*VC_SIZE+RX_SIZE);
1285 ld = -1;
1286 for (rx = 1; rx < vccs; rx <<= 1) ld++;
1287 dev->ci_range.vpi_bits = 0; /* @@@ no VPI for now */
1288 dev->ci_range.vci_bits = ld;
1289 dev->link_rate = ATM_OC3_PCR;
1290 zatm_dev->chans = vccs; /* ??? */
1291 curr = rx*RX_SIZE/4;
1292 DPRINTK("RX pool 0x%08lx\n",curr);
1293 zpokel(zatm_dev,curr,uPD98401_PMA); /* receive pool */
1294 zatm_dev->pool_base = curr;
1295 curr += pools*POOL_SIZE/4;
1296 DPRINTK("Shapers 0x%08lx\n",curr);
1297 zpokel(zatm_dev,curr,uPD98401_SMA); /* shapers */
1298 curr += NR_SHAPERS*SHAPER_SIZE/4;
1299 DPRINTK("Free 0x%08lx\n",curr);
1300 zpokel(zatm_dev,curr,uPD98401_TOS); /* free pool */
1301 printk(KERN_INFO DEV_LABEL "(itf %d): %d shapers, %d pools, %d RX, "
1302 "%ld VCs\n",dev->number,NR_SHAPERS,pools,rx,
1303 (zatm_dev->mem-curr*4)/VC_SIZE);
1304 /* create mailboxes */
1305 for (i = 0; i < NR_MBX; i++) {
1306 void *mbx;
1307 dma_addr_t mbx_dma;
1309 if (!mbx_entries[i])
1310 continue;
1311 mbx = pci_alloc_consistent(pdev, 2*MBX_SIZE(i), &mbx_dma);
1312 if (!mbx) {
1313 error = -ENOMEM;
1314 goto out;
1316 /*
1317 * Alignment provided by pci_alloc_consistent() isn't enough
1318 * for this device.
1319 */
1320 if (((unsigned long)mbx ^ mbx_dma) & 0xffff) {
1321 printk(KERN_ERR DEV_LABEL "(itf %d): system "
1322 "bus incompatible with driver\n", dev->number);
1323 pci_free_consistent(pdev, 2*MBX_SIZE(i), mbx, mbx_dma);
1324 error = -ENODEV;
1325 goto out;
1327 DPRINTK("mbx@0x%08lx-0x%08lx\n", mbx, mbx + MBX_SIZE(i));
1328 zatm_dev->mbx_start[i] = (unsigned long)mbx;
1329 zatm_dev->mbx_dma[i] = mbx_dma;
1330 zatm_dev->mbx_end[i] = (zatm_dev->mbx_start[i] + MBX_SIZE(i)) &
1331 0xffff;
1332 zout(mbx_dma >> 16, MSH(i));
1333 zout(mbx_dma, MSL(i));
1334 zout(zatm_dev->mbx_end[i], MBA(i));
1335 zout((unsigned long)mbx & 0xffff, MTA(i));
1336 zout((unsigned long)mbx & 0xffff, MWA(i));
1338 error = start_tx(dev);
1339 if (error)
1340 goto out;
1341 error = start_rx(dev);
1342 if (error)
1343 goto out_tx;
1344 error = dev->phy->start(dev);
1345 if (error)
1346 goto out_rx;
1347 zout(0xffffffff,IMR); /* enable interrupts */
1348 /* enable TX & RX */
1349 zout(zin(GMR) | uPD98401_GMR_SE | uPD98401_GMR_RE,GMR);
1350 done:
1351 return error;
1353 out_rx:
1354 kfree(zatm_dev->rx_map);
1355 out_tx:
1356 kfree(zatm_dev->tx_map);
1357 out:
1358 while (i-- > 0) {
1359 pci_free_consistent(pdev, 2*MBX_SIZE(i),
1360 (void *)zatm_dev->mbx_start[i],
1361 zatm_dev->mbx_dma[i]);
1363 free_irq(zatm_dev->irq, dev);
1364 goto done;
1368 static void zatm_close(struct atm_vcc *vcc)
1370 DPRINTK(">zatm_close\n");
1371 if (!ZATM_VCC(vcc)) return;
1372 clear_bit(ATM_VF_READY,&vcc->flags);
1373 close_rx(vcc);
1374 EVENT("close_tx\n",0,0);
1375 close_tx(vcc);
1376 DPRINTK("zatm_close: done waiting\n");
1377 /* deallocate memory */
1378 kfree(ZATM_VCC(vcc));
1379 vcc->dev_data = NULL;
1380 clear_bit(ATM_VF_ADDR,&vcc->flags);
1384 static int zatm_open(struct atm_vcc *vcc)
1386 struct zatm_dev *zatm_dev;
1387 struct zatm_vcc *zatm_vcc;
1388 short vpi = vcc->vpi;
1389 int vci = vcc->vci;
1390 int error;
1392 DPRINTK(">zatm_open\n");
1393 zatm_dev = ZATM_DEV(vcc->dev);
1394 if (!test_bit(ATM_VF_PARTIAL,&vcc->flags))
1395 vcc->dev_data = NULL;
1396 if (vci != ATM_VPI_UNSPEC && vpi != ATM_VCI_UNSPEC)
1397 set_bit(ATM_VF_ADDR,&vcc->flags);
1398 if (vcc->qos.aal != ATM_AAL5) return -EINVAL; /* @@@ AAL0 */
1399 DPRINTK(DEV_LABEL "(itf %d): open %d.%d\n",vcc->dev->number,vcc->vpi,
1400 vcc->vci);
1401 if (!test_bit(ATM_VF_PARTIAL,&vcc->flags)) {
1402 zatm_vcc = kmalloc(sizeof(struct zatm_vcc),GFP_KERNEL);
1403 if (!zatm_vcc) {
1404 clear_bit(ATM_VF_ADDR,&vcc->flags);
1405 return -ENOMEM;
1407 vcc->dev_data = zatm_vcc;
1408 ZATM_VCC(vcc)->tx_chan = 0; /* for zatm_close after open_rx */
1409 if ((error = open_rx_first(vcc))) {
1410 zatm_close(vcc);
1411 return error;
1413 if ((error = open_tx_first(vcc))) {
1414 zatm_close(vcc);
1415 return error;
1418 if (vci == ATM_VPI_UNSPEC || vpi == ATM_VCI_UNSPEC) return 0;
1419 if ((error = open_rx_second(vcc))) {
1420 zatm_close(vcc);
1421 return error;
1423 if ((error = open_tx_second(vcc))) {
1424 zatm_close(vcc);
1425 return error;
1427 set_bit(ATM_VF_READY,&vcc->flags);
1428 return 0;
1432 static int zatm_change_qos(struct atm_vcc *vcc,struct atm_qos *qos,int flags)
1434 printk("Not yet implemented\n");
1435 return -ENOSYS;
1436 /* @@@ */
1440 static int zatm_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
1442 struct zatm_dev *zatm_dev;
1443 unsigned long flags;
1445 zatm_dev = ZATM_DEV(dev);
1446 switch (cmd) {
1447 case ZATM_GETPOOLZ:
1448 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1449 /* fall through */
1450 case ZATM_GETPOOL:
1452 struct zatm_pool_info info;
1453 int pool;
1455 if (get_user(pool,
1456 &((struct zatm_pool_req __user *) arg)->pool_num))
1457 return -EFAULT;
1458 if (pool < 0 || pool > ZATM_LAST_POOL)
1459 return -EINVAL;
1460 spin_lock_irqsave(&zatm_dev->lock, flags);
1461 info = zatm_dev->pool_info[pool];
1462 if (cmd == ZATM_GETPOOLZ) {
1463 zatm_dev->pool_info[pool].rqa_count = 0;
1464 zatm_dev->pool_info[pool].rqu_count = 0;
1466 spin_unlock_irqrestore(&zatm_dev->lock, flags);
1467 return copy_to_user(
1468 &((struct zatm_pool_req __user *) arg)->info,
1469 &info,sizeof(info)) ? -EFAULT : 0;
1471 case ZATM_SETPOOL:
1473 struct zatm_pool_info info;
1474 int pool;
1476 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1477 if (get_user(pool,
1478 &((struct zatm_pool_req __user *) arg)->pool_num))
1479 return -EFAULT;
1480 if (pool < 0 || pool > ZATM_LAST_POOL)
1481 return -EINVAL;
1482 if (copy_from_user(&info,
1483 &((struct zatm_pool_req __user *) arg)->info,
1484 sizeof(info))) return -EFAULT;
1485 if (!info.low_water)
1486 info.low_water = zatm_dev->
1487 pool_info[pool].low_water;
1488 if (!info.high_water)
1489 info.high_water = zatm_dev->
1490 pool_info[pool].high_water;
1491 if (!info.next_thres)
1492 info.next_thres = zatm_dev->
1493 pool_info[pool].next_thres;
1494 if (info.low_water >= info.high_water ||
1495 info.low_water < 0)
1496 return -EINVAL;
1497 spin_lock_irqsave(&zatm_dev->lock, flags);
1498 zatm_dev->pool_info[pool].low_water =
1499 info.low_water;
1500 zatm_dev->pool_info[pool].high_water =
1501 info.high_water;
1502 zatm_dev->pool_info[pool].next_thres =
1503 info.next_thres;
1504 spin_unlock_irqrestore(&zatm_dev->lock, flags);
1505 return 0;
1507 default:
1508 if (!dev->phy->ioctl) return -ENOIOCTLCMD;
1509 return dev->phy->ioctl(dev,cmd,arg);
1514 static int zatm_getsockopt(struct atm_vcc *vcc,int level,int optname,
1515 void __user *optval,int optlen)
1517 return -EINVAL;
1521 static int zatm_setsockopt(struct atm_vcc *vcc,int level,int optname,
1522 void __user *optval,int optlen)
1524 return -EINVAL;
1527 static int zatm_send(struct atm_vcc *vcc,struct sk_buff *skb)
1529 int error;
1531 EVENT(">zatm_send 0x%lx\n",(unsigned long) skb,0);
1532 if (!ZATM_VCC(vcc)->tx_chan || !test_bit(ATM_VF_READY,&vcc->flags)) {
1533 if (vcc->pop) vcc->pop(vcc,skb);
1534 else dev_kfree_skb(skb);
1535 return -EINVAL;
1537 if (!skb) {
1538 printk(KERN_CRIT "!skb in zatm_send ?\n");
1539 if (vcc->pop) vcc->pop(vcc,skb);
1540 return -EINVAL;
1542 ATM_SKB(skb)->vcc = vcc;
1543 error = do_tx(skb);
1544 if (error != RING_BUSY) return error;
1545 skb_queue_tail(&ZATM_VCC(vcc)->backlog,skb);
1546 return 0;
1550 static void zatm_phy_put(struct atm_dev *dev,unsigned char value,
1551 unsigned long addr)
1553 struct zatm_dev *zatm_dev;
1555 zatm_dev = ZATM_DEV(dev);
1556 zwait;
1557 zout(value,CER);
1558 zout(uPD98401_IND_ACC | uPD98401_IA_B0 |
1559 (uPD98401_IA_TGT_PHY << uPD98401_IA_TGT_SHIFT) | addr,CMR);
1563 static unsigned char zatm_phy_get(struct atm_dev *dev,unsigned long addr)
1565 struct zatm_dev *zatm_dev;
1567 zatm_dev = ZATM_DEV(dev);
1568 zwait;
1569 zout(uPD98401_IND_ACC | uPD98401_IA_B0 | uPD98401_IA_RW |
1570 (uPD98401_IA_TGT_PHY << uPD98401_IA_TGT_SHIFT) | addr,CMR);
1571 zwait;
1572 return zin(CER) & 0xff;
1576 static const struct atmdev_ops ops = {
1577 .open = zatm_open,
1578 .close = zatm_close,
1579 .ioctl = zatm_ioctl,
1580 .getsockopt = zatm_getsockopt,
1581 .setsockopt = zatm_setsockopt,
1582 .send = zatm_send,
1583 .phy_put = zatm_phy_put,
1584 .phy_get = zatm_phy_get,
1585 .change_qos = zatm_change_qos,
1586 };
1588 static int __devinit zatm_init_one(struct pci_dev *pci_dev,
1589 const struct pci_device_id *ent)
1591 struct atm_dev *dev;
1592 struct zatm_dev *zatm_dev;
1593 int ret = -ENOMEM;
1595 zatm_dev = (struct zatm_dev *) kmalloc(sizeof(*zatm_dev), GFP_KERNEL);
1596 if (!zatm_dev) {
1597 printk(KERN_EMERG "%s: memory shortage\n", DEV_LABEL);
1598 goto out;
1601 dev = atm_dev_register(DEV_LABEL, &ops, -1, NULL);
1602 if (!dev)
1603 goto out_free;
1605 ret = pci_enable_device(pci_dev);
1606 if (ret < 0)
1607 goto out_deregister;
1609 ret = pci_request_regions(pci_dev, DEV_LABEL);
1610 if (ret < 0)
1611 goto out_disable;
1613 zatm_dev->pci_dev = pci_dev;
1614 dev->dev_data = zatm_dev;
1615 zatm_dev->copper = (int)ent->driver_data;
1616 if ((ret = zatm_init(dev)) || (ret = zatm_start(dev)))
1617 goto out_release;
1619 pci_set_drvdata(pci_dev, dev);
1620 zatm_dev->more = zatm_boards;
1621 zatm_boards = dev;
1622 ret = 0;
1623 out:
1624 return ret;
1626 out_release:
1627 pci_release_regions(pci_dev);
1628 out_disable:
1629 pci_disable_device(pci_dev);
1630 out_deregister:
1631 atm_dev_deregister(dev);
1632 out_free:
1633 kfree(zatm_dev);
1634 goto out;
1638 MODULE_LICENSE("GPL");
1640 static struct pci_device_id zatm_pci_tbl[] __devinitdata = {
1641 { PCI_VENDOR_ID_ZEITNET, PCI_DEVICE_ID_ZEITNET_1221,
1642 PCI_ANY_ID, PCI_ANY_ID, 0, 0, ZATM_COPPER },
1643 { PCI_VENDOR_ID_ZEITNET, PCI_DEVICE_ID_ZEITNET_1225,
1644 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1645 { 0, }
1646 };
1647 MODULE_DEVICE_TABLE(pci, zatm_pci_tbl);
1649 static struct pci_driver zatm_driver = {
1650 .name = DEV_LABEL,
1651 .id_table = zatm_pci_tbl,
1652 .probe = zatm_init_one,
1653 };
1655 static int __init zatm_init_module(void)
1657 return pci_register_driver(&zatm_driver);
1660 module_init(zatm_init_module);
1661 /* module_exit not defined so not unloadable */