1 /* drivers/atm/zatm.c - ZeitNet ZN122x device driver */
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
6 #include <linux/config.h>
7 #include <linux/module.h>
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
11 #include <linux/pci.h>
12 #include <linux/errno.h>
13 #include <linux/atm.h>
14 #include <linux/atmdev.h>
15 #include <linux/sonet.h>
16 #include <linux/skbuff.h>
17 #include <linux/netdevice.h>
18 #include <linux/delay.h>
19 #include <linux/ioport.h> /* for request_region */
20 #include <linux/uio.h>
21 #include <linux/init.h>
22 #include <linux/atm_zatm.h>
23 #include <linux/capability.h>
24 #include <linux/bitops.h>
25 #include <asm/byteorder.h>
26 #include <asm/system.h>
27 #include <asm/string.h>
29 #include <asm/atomic.h>
30 #include <asm/uaccess.h>
42 * - support 64 kB SDUs (will have to use multibuffer batches then :-( )
43 * - proper use of CDV, credit = max(1,CDVT*PCR)
45 * - better receive timestamps
50 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
52 #define DPRINTK(format,args...)
56 #ifndef CONFIG_ATM_ZATM_DEBUG
64 static void event_dump(void)
73 * NULL pointer checking
76 #define NULLCHECK(x) \
77 if ((unsigned long) (x) < 0x30) printk(KERN_CRIT #x "==0x%x\n", (int) (x))
80 * Very extensive activity logging. Greatly improves bug detection speed but
81 * costs a few Mbps if enabled.
86 static const char *ev
[EV
];
87 static unsigned long ev_a
[EV
],ev_b
[EV
];
91 static void EVENT(const char *s
,unsigned long a
,unsigned long b
)
100 static void event_dump(void)
104 printk(KERN_NOTICE
"----- event dump follows -----\n");
105 for (n
= 0; n
< EV
; n
++) {
108 printk(ev
[i
] ? ev
[i
] : "(null)",ev_a
[i
],ev_b
[i
]);
110 printk(KERN_NOTICE
"----- event dump ends here -----\n");
114 #endif /* CONFIG_ATM_ZATM_DEBUG */
117 #define RING_BUSY 1 /* indication from do_tx that PDU has to be
120 static struct atm_dev
*zatm_boards
= NULL
;
121 static unsigned long dummy
[2] = {0,0};
124 #define zin_n(r) inl(zatm_dev->base+r*4)
125 #define zin(r) inl(zatm_dev->base+uPD98401_##r*4)
126 #define zout(v,r) outl(v,zatm_dev->base+uPD98401_##r*4)
127 #define zwait while (zin(CMR) & uPD98401_BUSY)
129 /* RX0, RX1, TX0, TX1 */
130 static const int mbx_entries
[NR_MBX
] = { 1024,1024,1024,1024 };
131 static const int mbx_esize
[NR_MBX
] = { 16,16,4,4 }; /* entry size in bytes */
133 #define MBX_SIZE(i) (mbx_entries[i]*mbx_esize[i])
136 /*-------------------------------- utilities --------------------------------*/
139 static void zpokel(struct zatm_dev
*zatm_dev
,u32 value
,u32 addr
)
143 zout(uPD98401_IND_ACC
| uPD98401_IA_BALL
|
144 (uPD98401_IA_TGT_CM
<< uPD98401_IA_TGT_SHIFT
) | addr
,CMR
);
148 static u32
zpeekl(struct zatm_dev
*zatm_dev
,u32 addr
)
151 zout(uPD98401_IND_ACC
| uPD98401_IA_BALL
| uPD98401_IA_RW
|
152 (uPD98401_IA_TGT_CM
<< uPD98401_IA_TGT_SHIFT
) | addr
,CMR
);
158 /*------------------------------- free lists --------------------------------*/
162 * Free buffer head structure:
163 * [0] pointer to buffer (for SAR)
164 * [1] buffer descr link pointer (for SAR)
165 * [2] back pointer to skb (for poll_rx)
170 struct rx_buffer_head
{
171 u32 buffer
; /* pointer to buffer (for SAR) */
172 u32 link
; /* buffer descriptor link pointer (for SAR) */
173 struct sk_buff
*skb
; /* back pointer to skb (for poll_rx) */
177 static void refill_pool(struct atm_dev
*dev
,int pool
)
179 struct zatm_dev
*zatm_dev
;
181 struct rx_buffer_head
*first
;
183 int align
,offset
,free
,count
,size
;
185 EVENT("refill_pool\n",0,0);
186 zatm_dev
= ZATM_DEV(dev
);
187 size
= (64 << (pool
<= ZATM_AAL5_POOL_BASE
? 0 :
188 pool
-ZATM_AAL5_POOL_BASE
))+sizeof(struct rx_buffer_head
);
189 if (size
< PAGE_SIZE
) {
190 align
= 32; /* for 32 byte alignment */
191 offset
= sizeof(struct rx_buffer_head
);
195 offset
= zatm_dev
->pool_info
[pool
].offset
+
196 sizeof(struct rx_buffer_head
);
201 free
= zpeekl(zatm_dev
,zatm_dev
->pool_base
+2*pool
) &
202 uPD98401_RXFP_REMAIN
;
203 restore_flags(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]);
211 while (free
< zatm_dev
->pool_info
[pool
].high_water
) {
212 struct rx_buffer_head
*head
;
214 skb
= alloc_skb(size
,GFP_ATOMIC
);
216 printk(KERN_WARNING DEV_LABEL
"(Itf %d): got no new "
217 "skb (%d) with %d free\n",dev
->number
,size
,free
);
220 skb_reserve(skb
,(unsigned char *) ((((unsigned long) skb
->data
+
221 align
+offset
-1) & ~(unsigned long) (align
-1))-offset
)-
223 head
= (struct rx_buffer_head
*) skb
->data
;
224 skb_reserve(skb
,sizeof(struct rx_buffer_head
));
225 if (!first
) first
= head
;
227 head
->buffer
= virt_to_bus(skb
->data
);
230 EVENT("enq skb 0x%08lx/0x%08lx\n",(unsigned long) skb
,
231 (unsigned long) head
);
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 restore_flags(flags
);
244 zout(virt_to_bus(first
),CER
);
245 zout(uPD98401_ADD_BAT
| (pool
<< uPD98401_POOL_SHIFT
) | count
,
247 restore_flags(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]);
256 static void drain_free(struct atm_dev
*dev
,int pool
)
260 while ((skb
= skb_dequeue(&ZATM_DEV(dev
)->pool
[pool
]))) kfree_skb(skb
);
264 static int pool_index(int max_pdu
)
268 if (max_pdu
% ATM_CELL_PAYLOAD
)
269 printk(KERN_ERR DEV_LABEL
": driver error in pool_index: "
270 "max_pdu is %d\n",max_pdu
);
271 if (max_pdu
> 65536) return -1;
272 for (i
= 0; (64 << i
) < max_pdu
; i
++);
273 return i
+ZATM_AAL5_POOL_BASE
;
277 /* use_pool isn't reentrant */
280 static void use_pool(struct atm_dev
*dev
,int pool
)
282 struct zatm_dev
*zatm_dev
;
286 zatm_dev
= ZATM_DEV(dev
);
287 if (!(zatm_dev
->pool_info
[pool
].ref_count
++)) {
288 skb_queue_head_init(&zatm_dev
->pool
[pool
]);
289 size
= pool
-ZATM_AAL5_POOL_BASE
;
290 if (size
< 0) size
= 0; /* 64B... */
291 else if (size
> 10) size
= 10; /* ... 64kB */
294 zpokel(zatm_dev
,((zatm_dev
->pool_info
[pool
].low_water
/4) <<
295 uPD98401_RXFP_ALERT_SHIFT
) |
296 (1 << uPD98401_RXFP_BTSZ_SHIFT
) |
297 (size
<< uPD98401_RXFP_BFSZ_SHIFT
),
298 zatm_dev
->pool_base
+pool
*2);
299 zpokel(zatm_dev
,(unsigned long) dummy
,zatm_dev
->pool_base
+
301 restore_flags(flags
);
302 zatm_dev
->last_free
[pool
] = NULL
;
303 refill_pool(dev
,pool
);
305 DPRINTK("pool %d: %d\n",pool
,zatm_dev
->pool_info
[pool
].ref_count
);
309 static void unuse_pool(struct atm_dev
*dev
,int pool
)
311 if (!(--ZATM_DEV(dev
)->pool_info
[pool
].ref_count
))
312 drain_free(dev
,pool
);
316 static void zatm_feedback(struct atm_vcc
*vcc
,struct sk_buff
*skb
,
317 unsigned long start
,unsigned long dest
,int len
)
319 struct zatm_pool_info
*pool
;
320 unsigned long offset
,flags
;
322 DPRINTK("start 0x%08lx dest 0x%08lx len %d\n",start
,dest
,len
);
323 if (len
< PAGE_SIZE
) return;
324 pool
= &ZATM_DEV(vcc
->dev
)->pool_info
[ZATM_VCC(vcc
)->pool
];
325 offset
= (dest
-start
) & (PAGE_SIZE
-1);
328 if (!offset
|| pool
->offset
== offset
) {
330 restore_flags(flags
);
333 if (offset
!= pool
->next_off
) {
334 pool
->next_off
= offset
;
336 restore_flags(flags
);
339 if (++pool
->next_cnt
>= pool
->next_thres
) {
340 pool
->offset
= pool
->next_off
;
343 restore_flags(flags
);
347 /*----------------------- high-precision timestamps -------------------------*/
350 #ifdef CONFIG_ATM_ZATM_EXACT_TS
352 static struct timer_list sync_timer
;
356 * Note: the exact time is not normalized, i.e. tv_usec can be > 1000000.
357 * This must be handled by higher layers.
360 static inline struct timeval
exact_time(struct zatm_dev
*zatm_dev
,u32 ticks
)
364 tmp
= zatm_dev
->last_time
;
365 tmp
.tv_usec
+= ((s64
) (ticks
-zatm_dev
->last_clk
)*
366 (s64
) zatm_dev
->factor
) >> TIMER_SHIFT
;
371 static void zatm_clock_sync(unsigned long dummy
)
373 struct atm_dev
*atm_dev
;
374 struct zatm_dev
*zatm_dev
;
376 for (atm_dev
= zatm_boards
; atm_dev
; atm_dev
= zatm_dev
->more
) {
377 unsigned long flags
,interval
;
379 struct timeval now
,expected
;
382 zatm_dev
= ZATM_DEV(atm_dev
);
385 ticks
= zpeekl(zatm_dev
,uPD98401_TSR
);
386 do_gettimeofday(&now
);
387 restore_flags(flags
);
388 expected
= exact_time(zatm_dev
,ticks
);
389 diff
= 1000000*(expected
.tv_sec
-now
.tv_sec
)+
390 (expected
.tv_usec
-now
.tv_usec
);
391 zatm_dev
->timer_history
[zatm_dev
->th_curr
].real
= now
;
392 zatm_dev
->timer_history
[zatm_dev
->th_curr
].expected
= expected
;
393 zatm_dev
->th_curr
= (zatm_dev
->th_curr
+1) &
394 (ZATM_TIMER_HISTORY_SIZE
-1);
395 interval
= 1000000*(now
.tv_sec
-zatm_dev
->last_real_time
.tv_sec
)
396 +(now
.tv_usec
-zatm_dev
->last_real_time
.tv_usec
);
397 if (diff
>= -ADJ_REP_THRES
&& diff
<= ADJ_REP_THRES
)
398 zatm_dev
->timer_diffs
= 0;
400 #ifndef AGGRESSIVE_DEBUGGING
401 if (++zatm_dev
->timer_diffs
>= ADJ_MSG_THRES
)
404 zatm_dev
->timer_diffs
= 0;
405 printk(KERN_INFO DEV_LABEL
": TSR update after %ld us:"
406 " calculation differed by %d us\n",interval
,diff
);
407 #ifdef AGGRESSIVE_DEBUGGING
408 printk(KERN_DEBUG
" %d.%08d -> %d.%08d (%lu)\n",
409 zatm_dev
->last_real_time
.tv_sec
,
410 zatm_dev
->last_real_time
.tv_usec
,
411 now
.tv_sec
,now
.tv_usec
,interval
);
412 printk(KERN_DEBUG
" %u -> %u (%d)\n",
413 zatm_dev
->last_clk
,ticks
,ticks
-zatm_dev
->last_clk
);
414 printk(KERN_DEBUG
" factor %u\n",zatm_dev
->factor
);
417 if (diff
< -ADJ_IGN_THRES
|| diff
> ADJ_IGN_THRES
) {
418 /* filter out any major changes (e.g. time zone setup and
420 zatm_dev
->last_time
= now
;
422 (1000 << TIMER_SHIFT
)/(zatm_dev
->khz
+1);
425 zatm_dev
->last_time
= expected
;
427 * Is the accuracy of udelay really only about 1:300 on
428 * a 90 MHz Pentium ? Well, the following line avoids
429 * the problem, but ...
431 * What it does is simply:
433 * zatm_dev->factor = (interval << TIMER_SHIFT)/
434 * (ticks-zatm_dev->last_clk);
436 #define S(x) #x /* "stringification" ... */
438 asm("movl %2,%%ebx\n\t"
440 "xorl %%edx,%%edx\n\t"
441 "shldl $" SX(TIMER_SHIFT
) ",%1,%%edx\n\t"
442 "shl $" SX(TIMER_SHIFT
) ",%1\n\t"
444 : "=eax" (zatm_dev
->factor
)
445 : "eax" (interval
-diff
),"g" (ticks
),
446 "g" (zatm_dev
->last_clk
)
450 #ifdef AGGRESSIVE_DEBUGGING
451 printk(KERN_DEBUG
" (%ld << %d)/(%u-%u) = %u\n",
452 interval
,TIMER_SHIFT
,ticks
,zatm_dev
->last_clk
,
456 zatm_dev
->last_real_time
= now
;
457 zatm_dev
->last_clk
= ticks
;
459 mod_timer(&sync_timer
,sync_timer
.expires
+POLL_INTERVAL
*HZ
);
463 static void __init
zatm_clock_init(struct zatm_dev
*zatm_dev
)
465 static int start_timer
= 1;
468 zatm_dev
->factor
= (1000 << TIMER_SHIFT
)/(zatm_dev
->khz
+1);
469 zatm_dev
->timer_diffs
= 0;
470 memset(zatm_dev
->timer_history
,0,sizeof(zatm_dev
->timer_history
));
471 zatm_dev
->th_curr
= 0;
474 do_gettimeofday(&zatm_dev
->last_time
);
475 zatm_dev
->last_clk
= zpeekl(zatm_dev
,uPD98401_TSR
);
478 init_timer(&sync_timer
);
479 sync_timer
.expires
= jiffies
+POLL_INTERVAL
*HZ
;
480 sync_timer
.function
= zatm_clock_sync
;
481 add_timer(&sync_timer
);
483 restore_flags(flags
);
490 /*----------------------------------- RX ------------------------------------*/
494 static void exception(struct atm_vcc
*vcc
)
496 static int count
= 0;
497 struct zatm_dev
*zatm_dev
= ZATM_DEV(vcc
->dev
);
498 struct zatm_vcc
*zatm_vcc
= ZATM_VCC(vcc
);
502 if (count
++ > 2) return;
503 for (i
= 0; i
< 8; i
++)
504 printk("TX%d: 0x%08lx\n",i
,
505 zpeekl(zatm_dev
,zatm_vcc
->tx_chan
*VC_SIZE
/4+i
));
506 for (i
= 0; i
< 5; i
++)
507 printk("SH%d: 0x%08lx\n",i
,
508 zpeekl(zatm_dev
,uPD98401_IM(zatm_vcc
->shaper
)+16*i
));
509 qrp
= (unsigned long *) zpeekl(zatm_dev
,zatm_vcc
->tx_chan
*VC_SIZE
/4+
511 printk("qrp=0x%08lx\n",(unsigned long) qrp
);
512 for (i
= 0; i
< 4; i
++) printk("QRP[%d]: 0x%08lx",i
,qrp
[i
]);
517 static const char *err_txt
[] = {
521 "Maximum len violation",
537 static void poll_rx(struct atm_dev
*dev
,int mbx
)
539 struct zatm_dev
*zatm_dev
;
544 EVENT("poll_rx\n",0,0);
545 zatm_dev
= ZATM_DEV(dev
);
546 pos
= (zatm_dev
->mbx_start
[mbx
] & ~0xffffUL
) | zin(MTA(mbx
));
547 while (x
= zin(MWA(mbx
)), (pos
& 0xffff) != x
) {
553 EVENT("MBX: host 0x%lx, nic 0x%x\n",pos
,x
);
555 if (((pos
+= 16) & 0xffff) == zatm_dev
->mbx_end
[mbx
])
556 pos
= zatm_dev
->mbx_start
[mbx
];
557 cells
= here
[0] & uPD98401_AAL5_SIZE
;
559 printk("RX IND: 0x%x, 0x%x, 0x%x, 0x%x\n",here
[0],here
[1],here
[2],here
[3]);
562 printk("POOL: 0x%08x, 0x%08x\n",zpeekl(zatm_dev
,
563 zatm_dev
->pool_base
),
564 zpeekl(zatm_dev
,zatm_dev
->pool_base
+1));
565 x
= (unsigned long *) here
[2];
566 printk("[0..3] = 0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx\n",
567 x
[0],x
[1],x
[2],x
[3]);
571 if (here
[3] & uPD98401_AAL5_ERR
) {
572 error
= (here
[3] & uPD98401_AAL5_ES
) >>
573 uPD98401_AAL5_ES_SHIFT
;
574 if (error
== uPD98401_AAL5_ES_DEACT
||
575 error
== uPD98401_AAL5_ES_FREE
) continue;
577 EVENT("error code 0x%x/0x%x\n",(here
[3] & uPD98401_AAL5_ES
) >>
578 uPD98401_AAL5_ES_SHIFT
,error
);
579 skb
= ((struct rx_buffer_head
*) bus_to_virt(here
[2]))->skb
;
580 #ifdef CONFIG_ATM_ZATM_EXACT_TS
581 skb
->stamp
= exact_time(zatm_dev
,here
[1]);
586 printk("[-3..0] 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",((unsigned *) skb
->data
)[-3],
587 ((unsigned *) skb
->data
)[-2],((unsigned *) skb
->data
)[-1],
588 ((unsigned *) skb
->data
)[0]);
590 EVENT("skb 0x%lx, here 0x%lx\n",(unsigned long) skb
,
591 (unsigned long) here
);
593 printk("dummy: 0x%08lx, 0x%08lx\n",dummy
[0],dummy
[1]);
595 size
= error
? 0 : ntohs(((u16
*) skb
->data
)[cells
*
596 ATM_CELL_PAYLOAD
/sizeof(u16
)-3]);
597 EVENT("got skb 0x%lx, size %d\n",(unsigned long) skb
,size
);
598 chan
= (here
[3] & uPD98401_AAL5_CHAN
) >>
599 uPD98401_AAL5_CHAN_SHIFT
;
600 if (chan
< zatm_dev
->chans
&& zatm_dev
->rx_map
[chan
]) {
601 vcc
= zatm_dev
->rx_map
[chan
];
602 if (skb
== zatm_dev
->last_free
[ZATM_VCC(vcc
)->pool
])
603 zatm_dev
->last_free
[ZATM_VCC(vcc
)->pool
] = NULL
;
607 printk(KERN_ERR DEV_LABEL
"(itf %d): RX indication "
608 "for non-existing channel\n",dev
->number
);
614 static unsigned long silence
= 0;
615 static int last_error
= 0;
617 if (error
!= last_error
||
618 time_after(jiffies
, silence
) || silence
== 0){
619 printk(KERN_WARNING DEV_LABEL
"(itf %d): "
620 "chan %d error %s\n",dev
->number
,chan
,
623 silence
= (jiffies
+2*HZ
)|1;
627 if (size
&& (size
> cells
*ATM_CELL_PAYLOAD
-ATM_AAL5_TRAILER
||
628 size
<= (cells
-1)*ATM_CELL_PAYLOAD
-ATM_AAL5_TRAILER
)) {
629 printk(KERN_ERR DEV_LABEL
"(itf %d): size %d with %d "
630 "cells\n",dev
->number
,size
,cells
);
634 if (size
> ATM_MAX_AAL5_PDU
) {
635 printk(KERN_ERR DEV_LABEL
"(itf %d): size too big "
636 "(%d)\n",dev
->number
,size
);
641 dev_kfree_skb_irq(skb
);
642 if (vcc
) atomic_inc(&vcc
->stats
->rx_err
);
645 if (!atm_charge(vcc
,skb
->truesize
)) {
646 dev_kfree_skb_irq(skb
);
650 ATM_SKB(skb
)->vcc
= vcc
;
652 atomic_inc(&vcc
->stats
->rx
);
654 zout(pos
& 0xffff,MTA(mbx
));
655 #if 0 /* probably a stupid idea */
656 refill_pool(dev
,zatm_vcc
->pool
);
657 /* maybe this saves us a few interrupts */
662 static int open_rx_first(struct atm_vcc
*vcc
)
664 struct zatm_dev
*zatm_dev
;
665 struct zatm_vcc
*zatm_vcc
;
670 DPRINTK("open_rx_first (0x%x)\n",inb_p(0xc053));
671 zatm_dev
= ZATM_DEV(vcc
->dev
);
672 zatm_vcc
= ZATM_VCC(vcc
);
673 zatm_vcc
->rx_chan
= 0;
674 if (vcc
->qos
.rxtp
.traffic_class
== ATM_NONE
) return 0;
675 if (vcc
->qos
.aal
== ATM_AAL5
) {
676 if (vcc
->qos
.rxtp
.max_sdu
> 65464)
677 vcc
->qos
.rxtp
.max_sdu
= 65464;
678 /* fix this - we may want to receive 64kB SDUs
680 cells
= (vcc
->qos
.rxtp
.max_sdu
+ATM_AAL5_TRAILER
+
681 ATM_CELL_PAYLOAD
-1)/ATM_CELL_PAYLOAD
;
682 zatm_vcc
->pool
= pool_index(cells
*ATM_CELL_PAYLOAD
);
686 zatm_vcc
->pool
= ZATM_AAL0_POOL
;
688 if (zatm_vcc
->pool
< 0) return -EMSGSIZE
;
692 zout(uPD98401_OPEN_CHAN
,CMR
);
694 DPRINTK("0x%x 0x%x\n",zin(CMR
),zin(CER
));
695 chan
= (zin(CMR
) & uPD98401_CHAN_ADDR
) >> uPD98401_CHAN_ADDR_SHIFT
;
696 restore_flags(flags
);
697 DPRINTK("chan is %d\n",chan
);
698 if (!chan
) return -EAGAIN
;
699 use_pool(vcc
->dev
,zatm_vcc
->pool
);
700 DPRINTK("pool %d\n",zatm_vcc
->pool
);
701 /* set up VC descriptor */
703 zpokel(zatm_dev
,zatm_vcc
->pool
<< uPD98401_RXVC_POOL_SHIFT
,
705 zpokel(zatm_dev
,uPD98401_RXVC_OD
| (vcc
->qos
.aal
== ATM_AAL5
?
706 uPD98401_RXVC_AR
: 0) | cells
,chan
*VC_SIZE
/4+1);
707 zpokel(zatm_dev
,0,chan
*VC_SIZE
/4+2);
708 zatm_vcc
->rx_chan
= chan
;
709 zatm_dev
->rx_map
[chan
] = vcc
;
710 restore_flags(flags
);
715 static int open_rx_second(struct atm_vcc
*vcc
)
717 struct zatm_dev
*zatm_dev
;
718 struct zatm_vcc
*zatm_vcc
;
722 DPRINTK("open_rx_second (0x%x)\n",inb_p(0xc053));
723 zatm_dev
= ZATM_DEV(vcc
->dev
);
724 zatm_vcc
= ZATM_VCC(vcc
);
725 if (!zatm_vcc
->rx_chan
) return 0;
728 /* should also handle VPI @@@ */
730 shift
= (1-(vcc
->vci
& 1)) << 4;
731 zpokel(zatm_dev
,(zpeekl(zatm_dev
,pos
) & ~(0xffff << shift
)) |
732 ((zatm_vcc
->rx_chan
| uPD98401_RXLT_ENBL
) << shift
),pos
);
733 restore_flags(flags
);
738 static void close_rx(struct atm_vcc
*vcc
)
740 struct zatm_dev
*zatm_dev
;
741 struct zatm_vcc
*zatm_vcc
;
745 zatm_vcc
= ZATM_VCC(vcc
);
746 zatm_dev
= ZATM_DEV(vcc
->dev
);
747 if (!zatm_vcc
->rx_chan
) return;
748 DPRINTK("close_rx\n");
749 /* disable receiver */
751 if (vcc
->vpi
!= ATM_VPI_UNSPEC
&& vcc
->vci
!= ATM_VCI_UNSPEC
) {
754 shift
= (1-(vcc
->vci
& 1)) << 4;
755 zpokel(zatm_dev
,zpeekl(zatm_dev
,pos
) & ~(0xffff << shift
),pos
);
757 zout(uPD98401_NOP
,CMR
);
759 zout(uPD98401_NOP
,CMR
);
760 restore_flags(flags
);
764 zout(uPD98401_DEACT_CHAN
| uPD98401_CHAN_RT
| (zatm_vcc
->rx_chan
<<
765 uPD98401_CHAN_ADDR_SHIFT
),CMR
);
767 udelay(10); /* why oh why ... ? */
768 zout(uPD98401_CLOSE_CHAN
| uPD98401_CHAN_RT
| (zatm_vcc
->rx_chan
<<
769 uPD98401_CHAN_ADDR_SHIFT
),CMR
);
771 if (!(zin(CMR
) & uPD98401_CHAN_ADDR
))
772 printk(KERN_CRIT DEV_LABEL
"(itf %d): can't close RX channel "
773 "%d\n",vcc
->dev
->number
,zatm_vcc
->rx_chan
);
774 restore_flags(flags
);
775 zatm_dev
->rx_map
[zatm_vcc
->rx_chan
] = NULL
;
776 zatm_vcc
->rx_chan
= 0;
777 unuse_pool(vcc
->dev
,zatm_vcc
->pool
);
781 static int start_rx(struct atm_dev
*dev
)
783 struct zatm_dev
*zatm_dev
;
786 DPRINTK("start_rx\n");
787 zatm_dev
= ZATM_DEV(dev
);
788 size
= sizeof(struct atm_vcc
*)*zatm_dev
->chans
;
789 zatm_dev
->rx_map
= (struct atm_vcc
**) kmalloc(size
,GFP_KERNEL
);
790 if (!zatm_dev
->rx_map
) return -ENOMEM
;
791 memset(zatm_dev
->rx_map
,0,size
);
792 /* set VPI/VCI split (use all VCIs and give what's left to VPIs) */
793 zpokel(zatm_dev
,(1 << dev
->ci_range
.vci_bits
)-1,uPD98401_VRR
);
794 /* prepare free buffer pools */
795 for (i
= 0; i
<= ZATM_LAST_POOL
; i
++) {
796 zatm_dev
->pool_info
[i
].ref_count
= 0;
797 zatm_dev
->pool_info
[i
].rqa_count
= 0;
798 zatm_dev
->pool_info
[i
].rqu_count
= 0;
799 zatm_dev
->pool_info
[i
].low_water
= LOW_MARK
;
800 zatm_dev
->pool_info
[i
].high_water
= HIGH_MARK
;
801 zatm_dev
->pool_info
[i
].offset
= 0;
802 zatm_dev
->pool_info
[i
].next_off
= 0;
803 zatm_dev
->pool_info
[i
].next_cnt
= 0;
804 zatm_dev
->pool_info
[i
].next_thres
= OFF_CNG_THRES
;
810 /*----------------------------------- TX ------------------------------------*/
813 static int do_tx(struct sk_buff
*skb
)
816 struct zatm_dev
*zatm_dev
;
817 struct zatm_vcc
*zatm_vcc
;
821 EVENT("do_tx\n",0,0);
822 DPRINTK("sending skb %p\n",skb
);
823 vcc
= ATM_SKB(skb
)->vcc
;
824 zatm_dev
= ZATM_DEV(vcc
->dev
);
825 zatm_vcc
= ZATM_VCC(vcc
);
826 EVENT("iovcnt=%d\n",ATM_SKB(skb
)->iovcnt
,0);
829 if (!ATM_SKB(skb
)->iovcnt
) {
830 if (zatm_vcc
->txing
== RING_ENTRIES
-1) {
831 restore_flags(flags
);
835 dsc
= zatm_vcc
->ring
+zatm_vcc
->ring_curr
;
836 zatm_vcc
->ring_curr
= (zatm_vcc
->ring_curr
+RING_WORDS
) &
837 (RING_ENTRIES
*RING_WORDS
-1);
840 dsc
[3] = virt_to_bus(skb
->data
);
842 dsc
[0] = uPD98401_TXPD_V
| uPD98401_TXPD_DP
| uPD98401_TXPD_SM
843 | (vcc
->qos
.aal
== ATM_AAL5
? uPD98401_TXPD_AAL5
: 0 |
844 (ATM_SKB(skb
)->atm_options
& ATM_ATMOPT_CLP
?
845 uPD98401_CLPM_1
: uPD98401_CLPM_0
));
846 EVENT("dsc (0x%lx)\n",(unsigned long) dsc
,0);
849 printk("NONONONOO!!!!\n");
855 dsc
= (u32
*) kmalloc(uPD98401_TXPD_SIZE
*2+
856 uPD98401_TXBD_SIZE
*ATM_SKB(skb
)->iovcnt
,GFP_ATOMIC
);
858 if (vcc
->pop
) vcc
->pop(vcc
,skb
);
859 else dev_kfree_skb_irq(skb
);
862 /* @@@ should check alignment */
864 dsc
[0] = uPD98401_TXPD_V
| uPD98401_TXPD_DP
|
865 (vcc
->aal
== ATM_AAL5
? uPD98401_TXPD_AAL5
: 0 |
866 (ATM_SKB(skb
)->atm_options
& ATM_ATMOPT_CLP
?
867 uPD98401_CLPM_1
: uPD98401_CLPM_0
));
869 dsc
[2] = ATM_SKB(skb
)->iovcnt
*uPD98401_TXBD_SIZE
;
870 dsc
[3] = virt_to_bus(put
);
871 for (i
= 0; i
< ATM_SKB(skb
)->iovcnt
; i
++) {
872 *put
++ = ((struct iovec
*) skb
->data
)[i
].iov_len
;
873 *put
++ = virt_to_bus(((struct iovec
*)
874 skb
->data
)[i
].iov_base
);
876 put
[-2] |= uPD98401_TXBD_LAST
;
879 ZATM_PRV_DSC(skb
) = dsc
;
880 skb_queue_tail(&zatm_vcc
->tx_queue
,skb
);
881 DPRINTK("QRP=0x%08lx\n",zpeekl(zatm_dev
,zatm_vcc
->tx_chan
*VC_SIZE
/4+
884 zout(uPD98401_TX_READY
| (zatm_vcc
->tx_chan
<<
885 uPD98401_CHAN_ADDR_SHIFT
),CMR
);
886 restore_flags(flags
);
892 static inline void dequeue_tx(struct atm_vcc
*vcc
)
894 struct zatm_vcc
*zatm_vcc
;
897 EVENT("dequeue_tx\n",0,0);
898 zatm_vcc
= ZATM_VCC(vcc
);
899 skb
= skb_dequeue(&zatm_vcc
->tx_queue
);
901 printk(KERN_CRIT DEV_LABEL
"(itf %d): dequeue_tx but not "
902 "txing\n",vcc
->dev
->number
);
905 #if 0 /* @@@ would fail on CLP */
906 if (*ZATM_PRV_DSC(skb
) != (uPD98401_TXPD_V
| uPD98401_TXPD_DP
|
907 uPD98401_TXPD_SM
| uPD98401_TXPD_AAL5
)) printk("@#*$!!!! (%08x)\n",
910 *ZATM_PRV_DSC(skb
) = 0; /* mark as invalid */
912 if (vcc
->pop
) vcc
->pop(vcc
,skb
);
913 else dev_kfree_skb_irq(skb
);
914 while ((skb
= skb_dequeue(&zatm_vcc
->backlog
)))
915 if (do_tx(skb
) == RING_BUSY
) {
916 skb_queue_head(&zatm_vcc
->backlog
,skb
);
919 atomic_inc(&vcc
->stats
->tx
);
920 wake_up(&zatm_vcc
->tx_wait
);
924 static void poll_tx(struct atm_dev
*dev
,int mbx
)
926 struct zatm_dev
*zatm_dev
;
930 EVENT("poll_tx\n",0,0);
931 zatm_dev
= ZATM_DEV(dev
);
932 pos
= (zatm_dev
->mbx_start
[mbx
] & ~0xffffUL
) | zin(MTA(mbx
));
933 while (x
= zin(MWA(mbx
)), (pos
& 0xffff) != x
) {
939 EVENT("MBX: host 0x%lx, nic 0x%x\n",pos
,x
);
942 chan
= (data
& uPD98401_TXI_CONN
) >> uPD98401_TXI_CONN_SHIFT
;
943 EVENT("addr = 0x%lx, data = 0x%08x,",(unsigned long) addr
,
945 EVENT("chan = %d\n",chan
,0);
948 chan
= (zatm_dev
->mbx_start
[mbx
][pos
>> 2] & uPD98401_TXI_CONN
)
949 >> uPD98401_TXI_CONN_SHIFT
;
951 if (chan
< zatm_dev
->chans
&& zatm_dev
->tx_map
[chan
])
952 dequeue_tx(zatm_dev
->tx_map
[chan
]);
954 printk(KERN_CRIT DEV_LABEL
"(itf %d): TX indication "
955 "for non-existing channel %d\n",dev
->number
,chan
);
958 if (((pos
+= 4) & 0xffff) == zatm_dev
->mbx_end
[mbx
])
959 pos
= zatm_dev
->mbx_start
[mbx
];
961 zout(pos
& 0xffff,MTA(mbx
));
966 * BUG BUG BUG: Doesn't handle "new-style" rate specification yet.
969 static int alloc_shaper(struct atm_dev
*dev
,int *pcr
,int min
,int max
,int ubr
)
971 struct zatm_dev
*zatm_dev
;
976 DPRINTK("alloc_shaper (min = %d, max = %d)\n",min
,max
);
977 zatm_dev
= ZATM_DEV(dev
);
978 if (!zatm_dev
->free_shapers
) return -EAGAIN
;
979 for (shaper
= 0; !((zatm_dev
->free_shapers
>> shaper
) & 1); shaper
++);
980 zatm_dev
->free_shapers
&= ~1 << shaper
;
984 zatm_dev
->ubr_ref_cnt
++;
985 zatm_dev
->ubr
= shaper
;
995 m
= ATM_OC3_PCR
*255/min
;
999 if (max
> zatm_dev
->tx_bw
) max
= zatm_dev
->tx_bw
;
1006 m
= (ATM_OC3_PCR
*255+max
-1)/max
;
1010 printk(KERN_CRIT DEV_LABEL
"shaper algorithm botched "
1011 "[%d,%d] -> i=%ld,m=%ld\n",min
,max
,i
,m
);
1014 *pcr
= i
*ATM_OC3_PCR
/m
;
1015 c
= 20; /* @@@ should use max_cdv ! */
1016 if ((min
&& *pcr
< min
) || (max
&& *pcr
> max
)) return -EINVAL
;
1017 if (zatm_dev
->tx_bw
< *pcr
) return -EAGAIN
;
1018 zatm_dev
->tx_bw
-= *pcr
;
1022 DPRINTK("i = %d, m = %d, PCR = %d\n",i
,m
,*pcr
);
1023 zpokel(zatm_dev
,(i
<< uPD98401_IM_I_SHIFT
) | m
,uPD98401_IM(shaper
));
1024 zpokel(zatm_dev
,c
<< uPD98401_PC_C_SHIFT
,uPD98401_PC(shaper
));
1025 zpokel(zatm_dev
,0,uPD98401_X(shaper
));
1026 zpokel(zatm_dev
,0,uPD98401_Y(shaper
));
1027 zpokel(zatm_dev
,uPD98401_PS_E
,uPD98401_PS(shaper
));
1028 restore_flags(flags
);
1033 static void dealloc_shaper(struct atm_dev
*dev
,int shaper
)
1035 struct zatm_dev
*zatm_dev
;
1036 unsigned long flags
;
1038 zatm_dev
= ZATM_DEV(dev
);
1039 if (shaper
== zatm_dev
->ubr
) {
1040 if (--zatm_dev
->ubr_ref_cnt
) return;
1045 zpokel(zatm_dev
,zpeekl(zatm_dev
,uPD98401_PS(shaper
)) & ~uPD98401_PS_E
,
1046 uPD98401_PS(shaper
));
1047 restore_flags(flags
);
1048 zatm_dev
->free_shapers
|= 1 << shaper
;
1052 static void close_tx(struct atm_vcc
*vcc
)
1054 struct zatm_dev
*zatm_dev
;
1055 struct zatm_vcc
*zatm_vcc
;
1056 unsigned long flags
;
1058 struct sk_buff
*skb
;
1061 zatm_vcc
= ZATM_VCC(vcc
);
1062 zatm_dev
= ZATM_DEV(vcc
->dev
);
1063 chan
= zatm_vcc
->tx_chan
;
1065 DPRINTK("close_tx\n");
1068 while (skb_peek(&zatm_vcc
->backlog
)) {
1070 printk("waiting for backlog to drain ...\n");
1074 sleep_on(&zatm_vcc
->tx_wait
);
1077 while ((skb
= skb_peek(&zatm_vcc
->tx_queue
))) {
1079 printk("waiting for TX queue to drain ... %p\n",skb
);
1083 DPRINTK("waiting for TX queue to drain ... %p\n",skb
);
1084 sleep_on(&zatm_vcc
->tx_wait
);
1088 zout(uPD98401_DEACT_CHAN
| (chan
<< uPD98401_CHAN_ADDR_SHIFT
),CMR
);
1091 zout(uPD98401_CLOSE_CHAN
| (chan
<< uPD98401_CHAN_ADDR_SHIFT
),CMR
);
1093 if (!(zin(CMR
) & uPD98401_CHAN_ADDR
))
1094 printk(KERN_CRIT DEV_LABEL
"(itf %d): can't close TX channel "
1095 "%d\n",vcc
->dev
->number
,chan
);
1096 restore_flags(flags
);
1097 zatm_vcc
->tx_chan
= 0;
1098 zatm_dev
->tx_map
[chan
] = NULL
;
1099 if (zatm_vcc
->shaper
!= zatm_dev
->ubr
) {
1100 zatm_dev
->tx_bw
+= vcc
->qos
.txtp
.min_pcr
;
1101 dealloc_shaper(vcc
->dev
,zatm_vcc
->shaper
);
1103 if (zatm_vcc
->ring
) kfree(zatm_vcc
->ring
);
1107 static int open_tx_first(struct atm_vcc
*vcc
)
1109 struct zatm_dev
*zatm_dev
;
1110 struct zatm_vcc
*zatm_vcc
;
1111 unsigned long flags
;
1113 unsigned short chan
;
1116 DPRINTK("open_tx_first\n");
1117 zatm_dev
= ZATM_DEV(vcc
->dev
);
1118 zatm_vcc
= ZATM_VCC(vcc
);
1119 zatm_vcc
->tx_chan
= 0;
1120 if (vcc
->qos
.txtp
.traffic_class
== ATM_NONE
) return 0;
1124 zout(uPD98401_OPEN_CHAN
,CMR
);
1126 DPRINTK("0x%x 0x%x\n",zin(CMR
),zin(CER
));
1127 chan
= (zin(CMR
) & uPD98401_CHAN_ADDR
) >> uPD98401_CHAN_ADDR_SHIFT
;
1128 restore_flags(flags
);
1129 DPRINTK("chan is %d\n",chan
);
1130 if (!chan
) return -EAGAIN
;
1131 unlimited
= vcc
->qos
.txtp
.traffic_class
== ATM_UBR
&&
1132 (!vcc
->qos
.txtp
.max_pcr
|| vcc
->qos
.txtp
.max_pcr
== ATM_MAX_PCR
||
1133 vcc
->qos
.txtp
.max_pcr
>= ATM_OC3_PCR
);
1134 if (unlimited
&& zatm_dev
->ubr
!= -1) zatm_vcc
->shaper
= zatm_dev
->ubr
;
1136 if (unlimited
) vcc
->qos
.txtp
.max_sdu
= ATM_MAX_AAL5_PDU
;
1137 if ((zatm_vcc
->shaper
= alloc_shaper(vcc
->dev
,&pcr
,
1138 vcc
->qos
.txtp
.min_pcr
,vcc
->qos
.txtp
.max_pcr
,unlimited
))
1141 return zatm_vcc
->shaper
;
1143 if (pcr
> ATM_OC3_PCR
) pcr
= ATM_OC3_PCR
;
1144 vcc
->qos
.txtp
.min_pcr
= vcc
->qos
.txtp
.max_pcr
= pcr
;
1146 zatm_vcc
->tx_chan
= chan
;
1147 skb_queue_head_init(&zatm_vcc
->tx_queue
);
1148 init_waitqueue_head(&zatm_vcc
->tx_wait
);
1149 /* initialize ring */
1150 zatm_vcc
->ring
= kmalloc(RING_SIZE
,GFP_KERNEL
);
1151 if (!zatm_vcc
->ring
) return -ENOMEM
;
1152 memset(zatm_vcc
->ring
,0,RING_SIZE
);
1153 loop
= zatm_vcc
->ring
+RING_ENTRIES
*RING_WORDS
;
1154 loop
[0] = uPD98401_TXPD_V
;
1155 loop
[1] = loop
[2] = 0;
1156 loop
[3] = virt_to_bus(zatm_vcc
->ring
);
1157 zatm_vcc
->ring_curr
= 0;
1158 zatm_vcc
->txing
= 0;
1159 skb_queue_head_init(&zatm_vcc
->backlog
);
1160 zpokel(zatm_dev
,virt_to_bus(zatm_vcc
->ring
),
1161 chan
*VC_SIZE
/4+uPD98401_TXVC_QRP
);
1166 static int open_tx_second(struct atm_vcc
*vcc
)
1168 struct zatm_dev
*zatm_dev
;
1169 struct zatm_vcc
*zatm_vcc
;
1170 unsigned long flags
;
1172 DPRINTK("open_tx_second\n");
1173 zatm_dev
= ZATM_DEV(vcc
->dev
);
1174 zatm_vcc
= ZATM_VCC(vcc
);
1175 if (!zatm_vcc
->tx_chan
) return 0;
1177 /* set up VC descriptor */
1179 zpokel(zatm_dev
,0,zatm_vcc
->tx_chan
*VC_SIZE
/4);
1180 zpokel(zatm_dev
,uPD98401_TXVC_L
| (zatm_vcc
->shaper
<<
1181 uPD98401_TXVC_SHP_SHIFT
) | (vcc
->vpi
<< uPD98401_TXVC_VPI_SHIFT
) |
1182 vcc
->vci
,zatm_vcc
->tx_chan
*VC_SIZE
/4+1);
1183 zpokel(zatm_dev
,0,zatm_vcc
->tx_chan
*VC_SIZE
/4+2);
1184 restore_flags(flags
);
1185 zatm_dev
->tx_map
[zatm_vcc
->tx_chan
] = vcc
;
1190 static int start_tx(struct atm_dev
*dev
)
1192 struct zatm_dev
*zatm_dev
;
1195 DPRINTK("start_tx\n");
1196 zatm_dev
= ZATM_DEV(dev
);
1197 zatm_dev
->tx_map
= (struct atm_vcc
**) kmalloc(sizeof(struct atm_vcc
*)*
1198 zatm_dev
->chans
,GFP_KERNEL
);
1199 if (!zatm_dev
->tx_map
) return -ENOMEM
;
1200 zatm_dev
->tx_bw
= ATM_OC3_PCR
;
1201 zatm_dev
->free_shapers
= (1 << NR_SHAPERS
)-1;
1203 zatm_dev
->ubr_ref_cnt
= 0;
1204 /* initialize shapers */
1205 for (i
= 0; i
< NR_SHAPERS
; i
++) zpokel(zatm_dev
,0,uPD98401_PS(i
));
1210 /*------------------------------- interrupts --------------------------------*/
1213 static void zatm_int(int irq
,void *dev_id
,struct pt_regs
*regs
)
1215 struct atm_dev
*dev
;
1216 struct zatm_dev
*zatm_dev
;
1220 zatm_dev
= ZATM_DEV(dev
);
1221 while ((reason
= zin(GSR
))) {
1222 EVENT("reason 0x%x\n",reason
,0);
1223 if (reason
& uPD98401_INT_PI
) {
1224 EVENT("PHY int\n",0,0);
1225 dev
->phy
->interrupt(dev
);
1227 if (reason
& uPD98401_INT_RQA
) {
1228 unsigned long pools
;
1232 EVENT("RQA (0x%08x)\n",pools
,0);
1233 for (i
= 0; pools
; i
++) {
1236 zatm_dev
->pool_info
[i
].rqa_count
++;
1241 if (reason
& uPD98401_INT_RQU
) {
1242 unsigned long pools
;
1245 printk(KERN_WARNING DEV_LABEL
"(itf %d): RQU 0x%08lx\n",
1248 for (i
= 0; pools
; i
++) {
1251 zatm_dev
->pool_info
[i
].rqu_count
++;
1256 /* don't handle RD */
1257 if (reason
& uPD98401_INT_SPE
)
1258 printk(KERN_ALERT DEV_LABEL
"(itf %d): system parity "
1259 "error at 0x%08x\n",dev
->number
,zin(ADDR
));
1260 if (reason
& uPD98401_INT_CPE
)
1261 printk(KERN_ALERT DEV_LABEL
"(itf %d): control memory "
1262 "parity error at 0x%08x\n",dev
->number
,zin(ADDR
));
1263 if (reason
& uPD98401_INT_SBE
) {
1264 printk(KERN_ALERT DEV_LABEL
"(itf %d): system bus "
1265 "error at 0x%08x\n",dev
->number
,zin(ADDR
));
1268 /* don't handle IND */
1269 if (reason
& uPD98401_INT_MF
) {
1270 printk(KERN_CRIT DEV_LABEL
"(itf %d): mailbox full "
1271 "(0x%x)\n",dev
->number
,(reason
& uPD98401_INT_MF
)
1272 >> uPD98401_INT_MF_SHIFT
);
1274 /* @@@ should try to recover */
1276 if (reason
& uPD98401_INT_MM
) {
1277 if (reason
& 1) poll_rx(dev
,0);
1278 if (reason
& 2) poll_rx(dev
,1);
1279 if (reason
& 4) poll_tx(dev
,2);
1280 if (reason
& 8) poll_tx(dev
,3);
1282 /* @@@ handle RCRn */
1287 /*----------------------------- (E)EPROM access -----------------------------*/
1290 static void __init
eprom_set(struct zatm_dev
*zatm_dev
,unsigned long value
,
1295 if ((error
= pci_write_config_dword(zatm_dev
->pci_dev
,cmd
,value
)))
1296 printk(KERN_ERR DEV_LABEL
": PCI write failed (0x%02x)\n",
1301 static unsigned long __init
eprom_get(struct zatm_dev
*zatm_dev
,
1307 if ((error
= pci_read_config_dword(zatm_dev
->pci_dev
,cmd
,&value
)))
1308 printk(KERN_ERR DEV_LABEL
": PCI read failed (0x%02x)\n",
1314 static void __init
eprom_put_bits(struct zatm_dev
*zatm_dev
,
1315 unsigned long data
,int bits
,unsigned short cmd
)
1317 unsigned long value
;
1320 for (i
= bits
-1; i
>= 0; i
--) {
1321 value
= ZEPROM_CS
| (((data
>> i
) & 1) ? ZEPROM_DI
: 0);
1322 eprom_set(zatm_dev
,value
,cmd
);
1323 eprom_set(zatm_dev
,value
| ZEPROM_SK
,cmd
);
1324 eprom_set(zatm_dev
,value
,cmd
);
1329 static void __init
eprom_get_byte(struct zatm_dev
*zatm_dev
,
1330 unsigned char *byte
,unsigned short cmd
)
1335 for (i
= 8; i
; i
--) {
1336 eprom_set(zatm_dev
,ZEPROM_CS
,cmd
);
1337 eprom_set(zatm_dev
,ZEPROM_CS
| ZEPROM_SK
,cmd
);
1339 if (eprom_get(zatm_dev
,cmd
) & ZEPROM_DO
) *byte
|= 1;
1340 eprom_set(zatm_dev
,ZEPROM_CS
,cmd
);
1345 static unsigned char __init
eprom_try_esi(struct atm_dev
*dev
,
1346 unsigned short cmd
,int offset
,int swap
)
1348 unsigned char buf
[ZEPROM_SIZE
];
1349 struct zatm_dev
*zatm_dev
;
1352 zatm_dev
= ZATM_DEV(dev
);
1353 for (i
= 0; i
< ZEPROM_SIZE
; i
+= 2) {
1354 eprom_set(zatm_dev
,ZEPROM_CS
,cmd
); /* select EPROM */
1355 eprom_put_bits(zatm_dev
,ZEPROM_CMD_READ
,ZEPROM_CMD_LEN
,cmd
);
1356 eprom_put_bits(zatm_dev
,i
>> 1,ZEPROM_ADDR_LEN
,cmd
);
1357 eprom_get_byte(zatm_dev
,buf
+i
+swap
,cmd
);
1358 eprom_get_byte(zatm_dev
,buf
+i
+1-swap
,cmd
);
1359 eprom_set(zatm_dev
,0,cmd
); /* deselect EPROM */
1361 memcpy(dev
->esi
,buf
+offset
,ESI_LEN
);
1362 return memcmp(dev
->esi
,"\0\0\0\0\0",ESI_LEN
); /* assumes ESI_LEN == 6 */
1366 static void __init
eprom_get_esi(struct atm_dev
*dev
)
1368 if (eprom_try_esi(dev
,ZEPROM_V1_REG
,ZEPROM_V1_ESI_OFF
,1)) return;
1369 (void) eprom_try_esi(dev
,ZEPROM_V2_REG
,ZEPROM_V2_ESI_OFF
,0);
1373 /*--------------------------------- entries ---------------------------------*/
1376 static int __init
zatm_init(struct atm_dev
*dev
)
1378 struct zatm_dev
*zatm_dev
;
1379 struct pci_dev
*pci_dev
;
1380 unsigned short command
;
1381 unsigned char revision
;
1383 unsigned long t0
,t1
,t2
;
1385 DPRINTK(">zatm_init\n");
1386 zatm_dev
= ZATM_DEV(dev
);
1387 pci_dev
= zatm_dev
->pci_dev
;
1388 zatm_dev
->base
= pci_resource_start(pci_dev
, 0);
1389 zatm_dev
->irq
= pci_dev
->irq
;
1390 if ((error
= pci_read_config_word(pci_dev
,PCI_COMMAND
,&command
)) ||
1391 (error
= pci_read_config_byte(pci_dev
,PCI_REVISION_ID
,&revision
))) {
1392 printk(KERN_ERR DEV_LABEL
"(itf %d): init error 0x%02x\n",
1396 if ((error
= pci_write_config_word(pci_dev
,PCI_COMMAND
,
1397 command
| PCI_COMMAND_IO
| PCI_COMMAND_MASTER
))) {
1398 printk(KERN_ERR DEV_LABEL
"(itf %d): can't enable IO (0x%02x)"
1399 "\n",dev
->number
,error
);
1403 printk(KERN_NOTICE DEV_LABEL
"(itf %d): rev.%d,base=0x%x,irq=%d,",
1404 dev
->number
,revision
,zatm_dev
->base
,zatm_dev
->irq
);
1405 /* reset uPD98401 */
1407 while (!(zin(GSR
) & uPD98401_INT_IND
));
1408 zout(uPD98401_GMR_ONE
/*uPD98401_BURST4*/,GMR
);
1409 last
= MAX_CRAM_SIZE
;
1410 for (i
= last
-RAM_INCREMENT
; i
>= 0; i
-= RAM_INCREMENT
) {
1411 zpokel(zatm_dev
,0x55555555,i
);
1412 if (zpeekl(zatm_dev
,i
) != 0x55555555) last
= i
;
1414 zpokel(zatm_dev
,0xAAAAAAAA,i
);
1415 if (zpeekl(zatm_dev
,i
) != 0xAAAAAAAA) last
= i
;
1416 else zpokel(zatm_dev
,i
,i
);
1419 for (i
= 0; i
< last
; i
+= RAM_INCREMENT
)
1420 if (zpeekl(zatm_dev
,i
) != i
) break;
1421 zatm_dev
->mem
= i
<< 2;
1422 while (i
) zpokel(zatm_dev
,0,--i
);
1423 /* reset again to rebuild memory pointers */
1425 while (!(zin(GSR
) & uPD98401_INT_IND
));
1426 zout(uPD98401_GMR_ONE
| uPD98401_BURST8
| uPD98401_BURST4
|
1427 uPD98401_BURST2
| uPD98401_GMR_PM
| uPD98401_GMR_DR
,GMR
);
1428 /* TODO: should shrink allocation now */
1429 printk("mem=%dkB,%s (",zatm_dev
->mem
>> 10,zatm_dev
->copper
? "UTP" :
1431 for (i
= 0; i
< ESI_LEN
; i
++)
1432 printk("%02X%s",dev
->esi
[i
],i
== ESI_LEN
-1 ? ")\n" : "-");
1434 unsigned long flags
;
1438 t0
= zpeekl(zatm_dev
,uPD98401_TSR
);
1440 t1
= zpeekl(zatm_dev
,uPD98401_TSR
);
1442 t2
= zpeekl(zatm_dev
,uPD98401_TSR
);
1443 restore_flags(flags
);
1445 while (t0
> t1
|| t1
> t2
); /* loop if wrapping ... */
1446 zatm_dev
->khz
= t2
-2*t1
+t0
;
1447 printk(KERN_NOTICE DEV_LABEL
"(itf %d): uPD98401 %d.%d at %d.%03d "
1448 "MHz\n",dev
->number
,
1449 (zin(VER
) & uPD98401_MAJOR
) >> uPD98401_MAJOR_SHIFT
,
1450 zin(VER
) & uPD98401_MINOR
,zatm_dev
->khz
/1000,zatm_dev
->khz
% 1000);
1451 #ifdef CONFIG_ATM_ZATM_EXACT_TS
1452 zatm_clock_init(zatm_dev
);
1454 return uPD98402_init(dev
);
1458 static int __init
zatm_start(struct atm_dev
*dev
)
1460 struct zatm_dev
*zatm_dev
;
1465 DPRINTK("zatm_start\n");
1466 zatm_dev
= ZATM_DEV(dev
);
1467 if (request_irq(zatm_dev
->irq
,&zatm_int
,SA_SHIRQ
,DEV_LABEL
,dev
)) {
1468 printk(KERN_ERR DEV_LABEL
"(itf %d): IRQ%d is already in use\n",
1469 dev
->number
,zatm_dev
->irq
);
1472 request_region(zatm_dev
->base
,uPD98401_PORTS
,DEV_LABEL
);
1473 /* define memory regions */
1475 if (NR_SHAPERS
*SHAPER_SIZE
> pools
*POOL_SIZE
)
1476 pools
= NR_SHAPERS
*SHAPER_SIZE
/POOL_SIZE
;
1477 vccs
= (zatm_dev
->mem
-NR_SHAPERS
*SHAPER_SIZE
-pools
*POOL_SIZE
)/
1478 (2*VC_SIZE
+RX_SIZE
);
1480 for (rx
= 1; rx
< vccs
; rx
<<= 1) ld
++;
1481 dev
->ci_range
.vpi_bits
= 0; /* @@@ no VPI for now */
1482 dev
->ci_range
.vci_bits
= ld
;
1483 dev
->link_rate
= ATM_OC3_PCR
;
1484 zatm_dev
->chans
= vccs
; /* ??? */
1485 curr
= rx
*RX_SIZE
/4;
1486 DPRINTK("RX pool 0x%08lx\n",curr
);
1487 zpokel(zatm_dev
,curr
,uPD98401_PMA
); /* receive pool */
1488 zatm_dev
->pool_base
= curr
;
1489 curr
+= pools
*POOL_SIZE
/4;
1490 DPRINTK("Shapers 0x%08lx\n",curr
);
1491 zpokel(zatm_dev
,curr
,uPD98401_SMA
); /* shapers */
1492 curr
+= NR_SHAPERS
*SHAPER_SIZE
/4;
1493 DPRINTK("Free 0x%08lx\n",curr
);
1494 zpokel(zatm_dev
,curr
,uPD98401_TOS
); /* free pool */
1495 printk(KERN_INFO DEV_LABEL
"(itf %d): %d shapers, %d pools, %d RX, "
1496 "%ld VCs\n",dev
->number
,NR_SHAPERS
,pools
,rx
,
1497 (zatm_dev
->mem
-curr
*4)/VC_SIZE
);
1498 /* create mailboxes */
1499 for (i
= 0; i
< NR_MBX
; i
++) zatm_dev
->mbx_start
[i
] = 0;
1500 for (i
= 0; i
< NR_MBX
; i
++)
1501 if (mbx_entries
[i
]) {
1504 here
= (unsigned long) kmalloc(2*MBX_SIZE(i
),
1506 if (!here
) return -ENOMEM
;
1507 if ((here
^(here
+MBX_SIZE(i
))) & ~0xffffUL
)/* paranoia */
1508 here
= (here
& ~0xffffUL
)+0x10000;
1509 if ((here
^virt_to_bus((void *) here
)) & 0xffff) {
1510 printk(KERN_ERR DEV_LABEL
"(itf %d): system "
1511 "bus incompatible with driver\n",
1513 kfree((void *) here
);
1516 DPRINTK("mbx@0x%08lx-0x%08lx\n",here
,here
+MBX_SIZE(i
));
1517 zatm_dev
->mbx_start
[i
] = here
;
1518 zatm_dev
->mbx_end
[i
] = (here
+MBX_SIZE(i
)) & 0xffff;
1519 zout(virt_to_bus((void *) here
) >> 16,MSH(i
));
1520 zout(virt_to_bus((void *) here
),MSL(i
));
1521 zout((here
+MBX_SIZE(i
)) & 0xffff,MBA(i
));
1522 zout(here
& 0xffff,MTA(i
));
1523 zout(here
& 0xffff,MWA(i
));
1525 error
= start_tx(dev
);
1526 if (error
) return error
;
1527 error
= start_rx(dev
);
1528 if (error
) return error
;
1529 error
= dev
->phy
->start(dev
);
1530 if (error
) return error
;
1531 zout(0xffffffff,IMR
); /* enable interrupts */
1532 /* enable TX & RX */
1533 zout(zin(GMR
) | uPD98401_GMR_SE
| uPD98401_GMR_RE
,GMR
);
1538 static void zatm_close(struct atm_vcc
*vcc
)
1540 DPRINTK(">zatm_close\n");
1541 if (!ZATM_VCC(vcc
)) return;
1542 clear_bit(ATM_VF_READY
,&vcc
->flags
);
1544 EVENT("close_tx\n",0,0);
1546 DPRINTK("zatm_close: done waiting\n");
1547 /* deallocate memory */
1548 kfree(ZATM_VCC(vcc
));
1549 ZATM_VCC(vcc
) = NULL
;
1550 clear_bit(ATM_VF_ADDR
,&vcc
->flags
);
1554 static int zatm_open(struct atm_vcc
*vcc
,short vpi
,int vci
)
1556 struct zatm_dev
*zatm_dev
;
1557 struct zatm_vcc
*zatm_vcc
;
1560 DPRINTK(">zatm_open\n");
1561 zatm_dev
= ZATM_DEV(vcc
->dev
);
1562 if (!test_bit(ATM_VF_PARTIAL
,&vcc
->flags
)) ZATM_VCC(vcc
) = NULL
;
1563 error
= atm_find_ci(vcc
,&vpi
,&vci
);
1564 if (error
) return error
;
1567 if (vci
!= ATM_VPI_UNSPEC
&& vpi
!= ATM_VCI_UNSPEC
)
1568 set_bit(ATM_VF_ADDR
,&vcc
->flags
);
1569 if (vcc
->qos
.aal
!= ATM_AAL5
) return -EINVAL
; /* @@@ AAL0 */
1570 DPRINTK(DEV_LABEL
"(itf %d): open %d.%d\n",vcc
->dev
->number
,vcc
->vpi
,
1572 if (!test_bit(ATM_VF_PARTIAL
,&vcc
->flags
)) {
1573 zatm_vcc
= kmalloc(sizeof(struct zatm_vcc
),GFP_KERNEL
);
1575 clear_bit(ATM_VF_ADDR
,&vcc
->flags
);
1578 ZATM_VCC(vcc
) = zatm_vcc
;
1579 ZATM_VCC(vcc
)->tx_chan
= 0; /* for zatm_close after open_rx */
1580 if ((error
= open_rx_first(vcc
))) {
1584 if ((error
= open_tx_first(vcc
))) {
1589 if (vci
== ATM_VPI_UNSPEC
|| vpi
== ATM_VCI_UNSPEC
) return 0;
1590 if ((error
= open_rx_second(vcc
))) {
1594 if ((error
= open_tx_second(vcc
))) {
1598 set_bit(ATM_VF_READY
,&vcc
->flags
);
1603 static int zatm_change_qos(struct atm_vcc
*vcc
,struct atm_qos
*qos
,int flags
)
1605 printk("Not yet implemented\n");
1611 static int zatm_ioctl(struct atm_dev
*dev
,unsigned int cmd
,void *arg
)
1613 struct zatm_dev
*zatm_dev
;
1614 unsigned long flags
;
1616 zatm_dev
= ZATM_DEV(dev
);
1619 if (!capable(CAP_NET_ADMIN
)) return -EPERM
;
1623 struct zatm_pool_info info
;
1627 &((struct zatm_pool_req
*) arg
)->pool_num
))
1629 if (pool
< 0 || pool
> ZATM_LAST_POOL
)
1633 info
= zatm_dev
->pool_info
[pool
];
1634 if (cmd
== ZATM_GETPOOLZ
) {
1635 zatm_dev
->pool_info
[pool
].rqa_count
= 0;
1636 zatm_dev
->pool_info
[pool
].rqu_count
= 0;
1638 restore_flags(flags
);
1639 return copy_to_user(
1640 &((struct zatm_pool_req
*) arg
)->info
,
1641 &info
,sizeof(info
)) ? -EFAULT
: 0;
1645 struct zatm_pool_info info
;
1648 if (!capable(CAP_NET_ADMIN
)) return -EPERM
;
1650 &((struct zatm_pool_req
*) arg
)->pool_num
))
1652 if (pool
< 0 || pool
> ZATM_LAST_POOL
)
1654 if (copy_from_user(&info
,
1655 &((struct zatm_pool_req
*) arg
)->info
,
1656 sizeof(info
))) return -EFAULT
;
1657 if (!info
.low_water
)
1658 info
.low_water
= zatm_dev
->
1659 pool_info
[pool
].low_water
;
1660 if (!info
.high_water
)
1661 info
.high_water
= zatm_dev
->
1662 pool_info
[pool
].high_water
;
1663 if (!info
.next_thres
)
1664 info
.next_thres
= zatm_dev
->
1665 pool_info
[pool
].next_thres
;
1666 if (info
.low_water
>= info
.high_water
||
1671 zatm_dev
->pool_info
[pool
].low_water
=
1673 zatm_dev
->pool_info
[pool
].high_water
=
1675 zatm_dev
->pool_info
[pool
].next_thres
=
1677 restore_flags(flags
);
1680 #ifdef CONFIG_ATM_ZATM_EXACT_TS
1687 for (i
= 0; i
< ZATM_TIMER_HISTORY_SIZE
; i
++) {
1689 (struct zatm_t_hist
*) arg
+i
,
1690 &zatm_dev
->timer_history
[
1691 (zatm_dev
->th_curr
+i
) &
1692 (ZATM_TIMER_HISTORY_SIZE
-1)],
1693 sizeof(struct zatm_t_hist
)))
1695 restore_flags(flags
);
1698 restore_flags(flags
);
1703 if (!dev
->phy
->ioctl
) return -ENOIOCTLCMD
;
1704 return dev
->phy
->ioctl(dev
,cmd
,arg
);
1709 static int zatm_getsockopt(struct atm_vcc
*vcc
,int level
,int optname
,
1710 void *optval
,int optlen
)
1716 static int zatm_setsockopt(struct atm_vcc
*vcc
,int level
,int optname
,
1717 void *optval
,int optlen
)
1724 static int zatm_sg_send(struct atm_vcc
*vcc
,unsigned long start
,
1727 return vcc
->aal
== ATM_AAL5
;
1728 /* @@@ should check size and maybe alignment*/
1733 static int zatm_send(struct atm_vcc
*vcc
,struct sk_buff
*skb
)
1737 EVENT(">zatm_send 0x%lx\n",(unsigned long) skb
,0);
1738 if (!ZATM_VCC(vcc
)->tx_chan
|| !test_bit(ATM_VF_READY
,&vcc
->flags
)) {
1739 if (vcc
->pop
) vcc
->pop(vcc
,skb
);
1740 else dev_kfree_skb(skb
);
1744 printk(KERN_CRIT
"!skb in zatm_send ?\n");
1745 if (vcc
->pop
) vcc
->pop(vcc
,skb
);
1748 ATM_SKB(skb
)->vcc
= vcc
;
1750 if (error
!= RING_BUSY
) return error
;
1751 skb_queue_tail(&ZATM_VCC(vcc
)->backlog
,skb
);
1756 static void zatm_phy_put(struct atm_dev
*dev
,unsigned char value
,
1759 struct zatm_dev
*zatm_dev
;
1761 zatm_dev
= ZATM_DEV(dev
);
1764 zout(uPD98401_IND_ACC
| uPD98401_IA_B0
|
1765 (uPD98401_IA_TGT_PHY
<< uPD98401_IA_TGT_SHIFT
) | addr
,CMR
);
1769 static unsigned char zatm_phy_get(struct atm_dev
*dev
,unsigned long addr
)
1771 struct zatm_dev
*zatm_dev
;
1773 zatm_dev
= ZATM_DEV(dev
);
1775 zout(uPD98401_IND_ACC
| uPD98401_IA_B0
| uPD98401_IA_RW
|
1776 (uPD98401_IA_TGT_PHY
<< uPD98401_IA_TGT_SHIFT
) | addr
,CMR
);
1778 return zin(CER
) & 0xff;
1782 static const struct atmdev_ops ops
= {
1786 getsockopt
: zatm_getsockopt
,
1787 setsockopt
: zatm_setsockopt
,
1790 phy_put
: zatm_phy_put
,
1791 phy_get
: zatm_phy_get
,
1792 feedback
: zatm_feedback
,
1793 change_qos
: zatm_change_qos
,
1797 int __init
zatm_detect(void)
1799 struct atm_dev
*dev
;
1800 struct zatm_dev
*zatm_dev
;
1803 zatm_dev
= (struct zatm_dev
*) kmalloc(sizeof(struct zatm_dev
),
1805 if (!zatm_dev
) return -ENOMEM
;
1807 for (type
= 0; type
< 2; type
++) {
1808 struct pci_dev
*pci_dev
;
1811 while ((pci_dev
= pci_find_device(PCI_VENDOR_ID_ZEITNET
,type
?
1812 PCI_DEVICE_ID_ZEITNET_1225
: PCI_DEVICE_ID_ZEITNET_1221
,
1814 if (pci_enable_device(pci_dev
)) break;
1815 dev
= atm_dev_register(DEV_LABEL
,&ops
,-1,NULL
);
1817 zatm_dev
->pci_dev
= pci_dev
;
1818 ZATM_DEV(dev
) = zatm_dev
;
1819 zatm_dev
->copper
= type
;
1820 if (zatm_init(dev
) || zatm_start(dev
)) {
1821 atm_dev_deregister(dev
);
1824 zatm_dev
->more
= zatm_boards
;
1827 zatm_dev
= (struct zatm_dev
*) kmalloc(sizeof(struct
1828 zatm_dev
),GFP_KERNEL
);
1829 if (!zatm_dev
) break;
1838 int init_module(void)
1840 if (!zatm_detect()) {
1841 printk(KERN_ERR DEV_LABEL
": no adapter found\n");
1849 void cleanup_module(void)
1852 * Well, there's no way to get rid of the driver yet, so we don't
1853 * have to clean up, right ? :-)