1 /* drivers/atm/zatm.c - ZeitNet ZN122x device driver */
2 
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4 
5 
6 #include <linux/config.h>
7 #include <linux/module.h>
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
10 #include <linux/mm.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>
28 #include <asm/io.h>
29 #include <asm/atomic.h>
30 #include <asm/uaccess.h>
31 
32 #include "uPD98401.h"
33 #include "uPD98402.h"
34 #include "zeprom.h"
35 #include "zatm.h"
36 
37 
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  */
48 
49 #if 0
50 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
51 #else
52 #define DPRINTK(format,args...)
53 #endif
54 
55 #ifndef __i386__
56 #ifdef CONFIG_ATM_ZATM_EXACT_TS
57 #warning Precise timestamping only available on i386 platform
58 #undef CONFIG_ATM_ZATM_EXACT_TS
59 #endif
60 #endif
61 
62 #ifndef CONFIG_ATM_ZATM_DEBUG
63 
64 
65 #define NULLCHECK(x)
66 
67 #define EVENT(s,a,b)
68 
69 
event_dump(void)70 static void event_dump(void)
71 {
72 }
73 
74 
75 #else
76 
77 
78 /*
79  * NULL pointer checking
80  */
81 
82 #define NULLCHECK(x) \
83   if ((unsigned long) (x) < 0x30) printk(KERN_CRIT #x "==0x%x\n", (int) (x))
84 
85 /*
86  * Very extensive activity logging. Greatly improves bug detection speed but
87  * costs a few Mbps if enabled.
88  */
89 
90 #define EV 64
91 
92 static const char *ev[EV];
93 static unsigned long ev_a[EV],ev_b[EV];
94 static int ec = 0;
95 
96 
EVENT(const char * s,unsigned long a,unsigned long b)97 static void EVENT(const char *s,unsigned long a,unsigned long b)
98 {
99 	ev[ec] = s;
100 	ev_a[ec] = a;
101 	ev_b[ec] = b;
102 	ec = (ec+1) % EV;
103 }
104 
105 
event_dump(void)106 static void event_dump(void)
107 {
108 	int n,i;
109 
110 	printk(KERN_NOTICE "----- event dump follows -----\n");
111 	for (n = 0; n < EV; n++) {
112 		i = (ec+n) % EV;
113 		printk(KERN_NOTICE);
114 		printk(ev[i] ? ev[i] : "(null)",ev_a[i],ev_b[i]);
115 	}
116 	printk(KERN_NOTICE "----- event dump ends here -----\n");
117 }
118 
119 
120 #endif /* CONFIG_ATM_ZATM_DEBUG */
121 
122 
123 #define RING_BUSY	1	/* indication from do_tx that PDU has to be
124 				   backlogged */
125 
126 static struct atm_dev *zatm_boards = NULL;
127 static unsigned long dummy[2] = {0,0};
128 
129 
130 #define zin_n(r) inl(zatm_dev->base+r*4)
131 #define zin(r) inl(zatm_dev->base+uPD98401_##r*4)
132 #define zout(v,r) outl(v,zatm_dev->base+uPD98401_##r*4)
133 #define zwait while (zin(CMR) & uPD98401_BUSY)
134 
135 /* RX0, RX1, TX0, TX1 */
136 static const int mbx_entries[NR_MBX] = { 1024,1024,1024,1024 };
137 static const int mbx_esize[NR_MBX] = { 16,16,4,4 }; /* entry size in bytes */
138 
139 #define MBX_SIZE(i) (mbx_entries[i]*mbx_esize[i])
140 
141 
142 /*-------------------------------- utilities --------------------------------*/
143 
144 
zpokel(struct zatm_dev * zatm_dev,u32 value,u32 addr)145 static void zpokel(struct zatm_dev *zatm_dev,u32 value,u32 addr)
146 {
147 	zwait;
148 	zout(value,CER);
149 	zout(uPD98401_IND_ACC | uPD98401_IA_BALL |
150 	    (uPD98401_IA_TGT_CM << uPD98401_IA_TGT_SHIFT) | addr,CMR);
151 }
152 
153 
zpeekl(struct zatm_dev * zatm_dev,u32 addr)154 static u32 zpeekl(struct zatm_dev *zatm_dev,u32 addr)
155 {
156 	zwait;
157 	zout(uPD98401_IND_ACC | uPD98401_IA_BALL | uPD98401_IA_RW |
158 	  (uPD98401_IA_TGT_CM << uPD98401_IA_TGT_SHIFT) | addr,CMR);
159 	zwait;
160 	return zin(CER);
161 }
162 
163 
164 /*------------------------------- free lists --------------------------------*/
165 
166 
167 /*
168  * Free buffer head structure:
169  *   [0] pointer to buffer (for SAR)
170  *   [1] buffer descr link pointer (for SAR)
171  *   [2] back pointer to skb (for poll_rx)
172  *   [3] data
173  *   ...
174  */
175 
176 struct rx_buffer_head {
177 	u32		buffer;	/* pointer to buffer (for SAR) */
178 	u32		link;	/* buffer descriptor link pointer (for SAR) */
179 	struct sk_buff	*skb;	/* back pointer to skb (for poll_rx) */
180 };
181 
182 
refill_pool(struct atm_dev * dev,int pool)183 static void refill_pool(struct atm_dev *dev,int pool)
184 {
185 	struct zatm_dev *zatm_dev;
186 	struct sk_buff *skb;
187 	struct rx_buffer_head *first;
188 	unsigned long flags;
189 	int align,offset,free,count,size;
190 
191 	EVENT("refill_pool\n",0,0);
192 	zatm_dev = ZATM_DEV(dev);
193 	size = (64 << (pool <= ZATM_AAL5_POOL_BASE ? 0 :
194 	    pool-ZATM_AAL5_POOL_BASE))+sizeof(struct rx_buffer_head);
195 	if (size < PAGE_SIZE) {
196 		align = 32; /* for 32 byte alignment */
197 		offset = sizeof(struct rx_buffer_head);
198 	}
199 	else {
200 		align = 4096;
201 		offset = zatm_dev->pool_info[pool].offset+
202 		    sizeof(struct rx_buffer_head);
203 	}
204 	size += align;
205 	save_flags(flags);
206 	cli();
207 	free = zpeekl(zatm_dev,zatm_dev->pool_base+2*pool) &
208 	    uPD98401_RXFP_REMAIN;
209 	restore_flags(flags);
210 	if (free >= zatm_dev->pool_info[pool].low_water) return;
211 	EVENT("starting ... POOL: 0x%x, 0x%x\n",
212 	    zpeekl(zatm_dev,zatm_dev->pool_base+2*pool),
213 	    zpeekl(zatm_dev,zatm_dev->pool_base+2*pool+1));
214 	EVENT("dummy: 0x%08lx, 0x%08lx\n",dummy[0],dummy[1]);
215 	count = 0;
216 	first = NULL;
217 	while (free < zatm_dev->pool_info[pool].high_water) {
218 		struct rx_buffer_head *head;
219 
220 		skb = alloc_skb(size,GFP_ATOMIC);
221 		if (!skb) {
222 			printk(KERN_WARNING DEV_LABEL "(Itf %d): got no new "
223 			    "skb (%d) with %d free\n",dev->number,size,free);
224 			break;
225 		}
226 		skb_reserve(skb,(unsigned char *) ((((unsigned long) skb->data+
227 		    align+offset-1) & ~(unsigned long) (align-1))-offset)-
228 		    skb->data);
229 		head = (struct rx_buffer_head *) skb->data;
230 		skb_reserve(skb,sizeof(struct rx_buffer_head));
231 		if (!first) first = head;
232 		count++;
233 		head->buffer = virt_to_bus(skb->data);
234 		head->link = 0;
235 		head->skb = skb;
236 		EVENT("enq skb 0x%08lx/0x%08lx\n",(unsigned long) skb,
237 		    (unsigned long) head);
238 		cli();
239 		if (zatm_dev->last_free[pool])
240 			((struct rx_buffer_head *) (zatm_dev->last_free[pool]->
241 			    data))[-1].link = virt_to_bus(head);
242 		zatm_dev->last_free[pool] = skb;
243 		skb_queue_tail(&zatm_dev->pool[pool],skb);
244 		restore_flags(flags);
245 		free++;
246 	}
247 	if (first) {
248 		cli();
249 		zwait;
250 		zout(virt_to_bus(first),CER);
251 		zout(uPD98401_ADD_BAT | (pool << uPD98401_POOL_SHIFT) | count,
252 		    CMR);
253 		restore_flags(flags);
254 		EVENT ("POOL: 0x%x, 0x%x\n",
255 		    zpeekl(zatm_dev,zatm_dev->pool_base+2*pool),
256 		    zpeekl(zatm_dev,zatm_dev->pool_base+2*pool+1));
257 		EVENT("dummy: 0x%08lx, 0x%08lx\n",dummy[0],dummy[1]);
258 	}
259 }
260 
261 
drain_free(struct atm_dev * dev,int pool)262 static void drain_free(struct atm_dev *dev,int pool)
263 {
264 	skb_queue_purge(&ZATM_DEV(dev)->pool[pool]);
265 }
266 
267 
pool_index(int max_pdu)268 static int pool_index(int max_pdu)
269 {
270 	int i;
271 
272 	if (max_pdu % ATM_CELL_PAYLOAD)
273 		printk(KERN_ERR DEV_LABEL ": driver error in pool_index: "
274 		    "max_pdu is %d\n",max_pdu);
275 	if (max_pdu > 65536) return -1;
276 	for (i = 0; (64 << i) < max_pdu; i++);
277 	return i+ZATM_AAL5_POOL_BASE;
278 }
279 
280 
281 /* use_pool isn't reentrant */
282 
283 
use_pool(struct atm_dev * dev,int pool)284 static void use_pool(struct atm_dev *dev,int pool)
285 {
286 	struct zatm_dev *zatm_dev;
287 	unsigned long flags;
288 	int size;
289 
290 	zatm_dev = ZATM_DEV(dev);
291 	if (!(zatm_dev->pool_info[pool].ref_count++)) {
292 		skb_queue_head_init(&zatm_dev->pool[pool]);
293 		size = pool-ZATM_AAL5_POOL_BASE;
294 		if (size < 0) size = 0; /* 64B... */
295 		else if (size > 10) size = 10; /* ... 64kB */
296 		save_flags(flags);
297 		cli();
298 		zpokel(zatm_dev,((zatm_dev->pool_info[pool].low_water/4) <<
299 		    uPD98401_RXFP_ALERT_SHIFT) |
300 		    (1 << uPD98401_RXFP_BTSZ_SHIFT) |
301 		    (size << uPD98401_RXFP_BFSZ_SHIFT),
302 		    zatm_dev->pool_base+pool*2);
303 		zpokel(zatm_dev,(unsigned long) dummy,zatm_dev->pool_base+
304 		    pool*2+1);
305 		restore_flags(flags);
306 		zatm_dev->last_free[pool] = NULL;
307 		refill_pool(dev,pool);
308 	}
309 	DPRINTK("pool %d: %d\n",pool,zatm_dev->pool_info[pool].ref_count);
310 }
311 
312 
unuse_pool(struct atm_dev * dev,int pool)313 static void unuse_pool(struct atm_dev *dev,int pool)
314 {
315 	if (!(--ZATM_DEV(dev)->pool_info[pool].ref_count))
316 		drain_free(dev,pool);
317 }
318 
319 
zatm_feedback(struct atm_vcc * vcc,struct sk_buff * skb,unsigned long start,unsigned long dest,int len)320 static void zatm_feedback(struct atm_vcc *vcc,struct sk_buff *skb,
321     unsigned long start,unsigned long dest,int len)
322 {
323 	struct zatm_pool_info *pool;
324 	unsigned long offset,flags;
325 
326 	DPRINTK("start 0x%08lx dest 0x%08lx len %d\n",start,dest,len);
327 	if (len < PAGE_SIZE) return;
328 	pool = &ZATM_DEV(vcc->dev)->pool_info[ZATM_VCC(vcc)->pool];
329 	offset = (dest-start) & (PAGE_SIZE-1);
330 	save_flags(flags);
331 	cli();
332 	if (!offset || pool->offset == offset) {
333 		pool->next_cnt = 0;
334 		restore_flags(flags);
335 		return;
336 	}
337 	if (offset != pool->next_off) {
338 		pool->next_off = offset;
339 		pool->next_cnt = 0;
340 		restore_flags(flags);
341 		return;
342 	}
343 	if (++pool->next_cnt >= pool->next_thres) {
344 		pool->offset = pool->next_off;
345 		pool->next_cnt = 0;
346 	}
347 	restore_flags(flags);
348 }
349 
350 
351 /*----------------------- high-precision timestamps -------------------------*/
352 
353 
354 #ifdef CONFIG_ATM_ZATM_EXACT_TS
355 
356 static struct timer_list sync_timer;
357 
358 
359 /*
360  * Note: the exact time is not normalized, i.e. tv_usec can be > 1000000.
361  * This must be handled by higher layers.
362  */
363 
exact_time(struct zatm_dev * zatm_dev,u32 ticks)364 static inline struct timeval exact_time(struct zatm_dev *zatm_dev,u32 ticks)
365 {
366 	struct timeval tmp;
367 
368 	tmp = zatm_dev->last_time;
369 	tmp.tv_usec += ((s64) (ticks-zatm_dev->last_clk)*
370 	    (s64) zatm_dev->factor) >> TIMER_SHIFT;
371 	return tmp;
372 }
373 
374 
zatm_clock_sync(unsigned long dummy)375 static void zatm_clock_sync(unsigned long dummy)
376 {
377 	struct atm_dev *atm_dev;
378 	struct zatm_dev *zatm_dev;
379 
380 	for (atm_dev = zatm_boards; atm_dev; atm_dev = zatm_dev->more) {
381 		unsigned long flags,interval;
382 		int diff;
383 		struct timeval now,expected;
384 		u32 ticks;
385 
386 		zatm_dev = ZATM_DEV(atm_dev);
387 		save_flags(flags);
388 		cli();
389 		ticks = zpeekl(zatm_dev,uPD98401_TSR);
390 		do_gettimeofday(&now);
391 		restore_flags(flags);
392 		expected = exact_time(zatm_dev,ticks);
393 		diff = 1000000*(expected.tv_sec-now.tv_sec)+
394 		    (expected.tv_usec-now.tv_usec);
395 		zatm_dev->timer_history[zatm_dev->th_curr].real = now;
396 		zatm_dev->timer_history[zatm_dev->th_curr].expected = expected;
397 		zatm_dev->th_curr = (zatm_dev->th_curr+1) &
398 		    (ZATM_TIMER_HISTORY_SIZE-1);
399 		interval = 1000000*(now.tv_sec-zatm_dev->last_real_time.tv_sec)
400 		    +(now.tv_usec-zatm_dev->last_real_time.tv_usec);
401 		if (diff >= -ADJ_REP_THRES && diff <= ADJ_REP_THRES)
402 			zatm_dev->timer_diffs = 0;
403 		else
404 #ifndef AGGRESSIVE_DEBUGGING
405 			if (++zatm_dev->timer_diffs >= ADJ_MSG_THRES)
406 #endif
407 			{
408 			zatm_dev->timer_diffs = 0;
409 			printk(KERN_INFO DEV_LABEL ": TSR update after %ld us:"
410 			    " calculation differed by %d us\n",interval,diff);
411 #ifdef AGGRESSIVE_DEBUGGING
412 			printk(KERN_DEBUG "  %d.%08d -> %d.%08d (%lu)\n",
413 			    zatm_dev->last_real_time.tv_sec,
414 			    zatm_dev->last_real_time.tv_usec,
415 			    now.tv_sec,now.tv_usec,interval);
416 			printk(KERN_DEBUG "  %u -> %u (%d)\n",
417 			    zatm_dev->last_clk,ticks,ticks-zatm_dev->last_clk);
418 			printk(KERN_DEBUG "  factor %u\n",zatm_dev->factor);
419 #endif
420 		}
421 		if (diff < -ADJ_IGN_THRES || diff > ADJ_IGN_THRES) {
422 		    /* filter out any major changes (e.g. time zone setup and
423 		       such) */
424 			zatm_dev->last_time = now;
425 			zatm_dev->factor =
426 			    (1000 << TIMER_SHIFT)/(zatm_dev->khz+1);
427 		}
428 		else {
429 			zatm_dev->last_time = expected;
430 			/*
431 			 * Is the accuracy of udelay really only about 1:300 on
432 			 * a 90 MHz Pentium ? Well, the following line avoids
433 			 * the problem, but ...
434 			 *
435 			 * What it does is simply:
436 			 *
437 			 * zatm_dev->factor = (interval << TIMER_SHIFT)/
438 			 *     (ticks-zatm_dev->last_clk);
439 			 */
440 #define S(x) #x		/* "stringification" ... */
441 #define SX(x) S(x)
442 			asm("movl %2,%%ebx\n\t"
443 			    "subl %3,%%ebx\n\t"
444 			    "xorl %%edx,%%edx\n\t"
445 			    "shldl $" SX(TIMER_SHIFT) ",%1,%%edx\n\t"
446 			    "shl $" SX(TIMER_SHIFT) ",%1\n\t"
447 			    "divl %%ebx\n\t"
448 			    : "=a" (zatm_dev->factor)
449 			    : "0" (interval-diff),"g" (ticks),
450 			      "g" (zatm_dev->last_clk)
451 			    : "ebx","edx","cc");
452 #undef S
453 #undef SX
454 #ifdef AGGRESSIVE_DEBUGGING
455 			printk(KERN_DEBUG "  (%ld << %d)/(%u-%u) = %u\n",
456 			    interval,TIMER_SHIFT,ticks,zatm_dev->last_clk,
457 			    zatm_dev->factor);
458 #endif
459 		}
460 		zatm_dev->last_real_time = now;
461 		zatm_dev->last_clk = ticks;
462 	}
463 	mod_timer(&sync_timer,sync_timer.expires+POLL_INTERVAL*HZ);
464 }
465 
466 
zatm_clock_init(struct zatm_dev * zatm_dev)467 static void __init zatm_clock_init(struct zatm_dev *zatm_dev)
468 {
469 	static int start_timer = 1;
470 	unsigned long flags;
471 
472 	zatm_dev->factor = (1000 << TIMER_SHIFT)/(zatm_dev->khz+1);
473 	zatm_dev->timer_diffs = 0;
474 	memset(zatm_dev->timer_history,0,sizeof(zatm_dev->timer_history));
475 	zatm_dev->th_curr = 0;
476 	save_flags(flags);
477 	cli();
478 	do_gettimeofday(&zatm_dev->last_time);
479 	zatm_dev->last_clk = zpeekl(zatm_dev,uPD98401_TSR);
480 	if (start_timer) {
481 		start_timer = 0;
482 		init_timer(&sync_timer);
483 		sync_timer.expires = jiffies+POLL_INTERVAL*HZ;
484 		sync_timer.function = zatm_clock_sync;
485 		add_timer(&sync_timer);
486 	}
487 	restore_flags(flags);
488 }
489 
490 
491 #endif
492 
493 
494 /*----------------------------------- RX ------------------------------------*/
495 
496 
497 #if 0
498 static void exception(struct atm_vcc *vcc)
499 {
500    static int count = 0;
501    struct zatm_dev *zatm_dev = ZATM_DEV(vcc->dev);
502    struct zatm_vcc *zatm_vcc = ZATM_VCC(vcc);
503    unsigned long *qrp;
504    int i;
505 
506    if (count++ > 2) return;
507    for (i = 0; i < 8; i++)
508 	printk("TX%d: 0x%08lx\n",i,
509 	  zpeekl(zatm_dev,zatm_vcc->tx_chan*VC_SIZE/4+i));
510    for (i = 0; i < 5; i++)
511 	printk("SH%d: 0x%08lx\n",i,
512 	  zpeekl(zatm_dev,uPD98401_IM(zatm_vcc->shaper)+16*i));
513    qrp = (unsigned long *) zpeekl(zatm_dev,zatm_vcc->tx_chan*VC_SIZE/4+
514      uPD98401_TXVC_QRP);
515    printk("qrp=0x%08lx\n",(unsigned long) qrp);
516    for (i = 0; i < 4; i++) printk("QRP[%d]: 0x%08lx",i,qrp[i]);
517 }
518 #endif
519 
520 
521 static const char *err_txt[] = {
522 	"No error",
523 	"RX buf underflow",
524 	"RX FIFO overrun",
525 	"Maximum len violation",
526 	"CRC error",
527 	"User abort",
528 	"Length violation",
529 	"T1 error",
530 	"Deactivated",
531 	"???",
532 	"???",
533 	"???",
534 	"???",
535 	"???",
536 	"???",
537 	"???"
538 };
539 
540 
poll_rx(struct atm_dev * dev,int mbx)541 static void poll_rx(struct atm_dev *dev,int mbx)
542 {
543 	struct zatm_dev *zatm_dev;
544 	unsigned long pos;
545 	u32 x;
546 	int error;
547 
548 	EVENT("poll_rx\n",0,0);
549 	zatm_dev = ZATM_DEV(dev);
550 	pos = (zatm_dev->mbx_start[mbx] & ~0xffffUL) | zin(MTA(mbx));
551 	while (x = zin(MWA(mbx)), (pos & 0xffff) != x) {
552 		u32 *here;
553 		struct sk_buff *skb;
554 		struct atm_vcc *vcc;
555 		int cells,size,chan;
556 
557 		EVENT("MBX: host 0x%lx, nic 0x%x\n",pos,x);
558 		here = (u32 *) pos;
559 		if (((pos += 16) & 0xffff) == zatm_dev->mbx_end[mbx])
560 			pos = zatm_dev->mbx_start[mbx];
561 		cells = here[0] & uPD98401_AAL5_SIZE;
562 #if 0
563 printk("RX IND: 0x%x, 0x%x, 0x%x, 0x%x\n",here[0],here[1],here[2],here[3]);
564 {
565 unsigned long *x;
566 		printk("POOL: 0x%08x, 0x%08x\n",zpeekl(zatm_dev,
567 		      zatm_dev->pool_base),
568 		      zpeekl(zatm_dev,zatm_dev->pool_base+1));
569 		x = (unsigned long *) here[2];
570 		printk("[0..3] = 0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx\n",
571 		    x[0],x[1],x[2],x[3]);
572 }
573 #endif
574 		error = 0;
575 		if (here[3] & uPD98401_AAL5_ERR) {
576 			error = (here[3] & uPD98401_AAL5_ES) >>
577 			    uPD98401_AAL5_ES_SHIFT;
578 			if (error == uPD98401_AAL5_ES_DEACT ||
579 			    error == uPD98401_AAL5_ES_FREE) continue;
580 		}
581 EVENT("error code 0x%x/0x%x\n",(here[3] & uPD98401_AAL5_ES) >>
582   uPD98401_AAL5_ES_SHIFT,error);
583 		skb = ((struct rx_buffer_head *) bus_to_virt(here[2]))->skb;
584 #ifdef CONFIG_ATM_ZATM_EXACT_TS
585 		skb->stamp = exact_time(zatm_dev,here[1]);
586 #else
587 		skb->stamp = xtime;
588 #endif
589 #if 0
590 printk("[-3..0] 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",((unsigned *) skb->data)[-3],
591   ((unsigned *) skb->data)[-2],((unsigned *) skb->data)[-1],
592   ((unsigned *) skb->data)[0]);
593 #endif
594 		EVENT("skb 0x%lx, here 0x%lx\n",(unsigned long) skb,
595 		    (unsigned long) here);
596 #if 0
597 printk("dummy: 0x%08lx, 0x%08lx\n",dummy[0],dummy[1]);
598 #endif
599 		size = error ? 0 : ntohs(((u16 *) skb->data)[cells*
600 		    ATM_CELL_PAYLOAD/sizeof(u16)-3]);
601 		EVENT("got skb 0x%lx, size %d\n",(unsigned long) skb,size);
602 		chan = (here[3] & uPD98401_AAL5_CHAN) >>
603 		    uPD98401_AAL5_CHAN_SHIFT;
604 		if (chan < zatm_dev->chans && zatm_dev->rx_map[chan]) {
605 			vcc = zatm_dev->rx_map[chan];
606 			if (skb == zatm_dev->last_free[ZATM_VCC(vcc)->pool])
607 				zatm_dev->last_free[ZATM_VCC(vcc)->pool] = NULL;
608 			skb_unlink(skb);
609 		}
610 		else {
611 			printk(KERN_ERR DEV_LABEL "(itf %d): RX indication "
612 			    "for non-existing channel\n",dev->number);
613 			size = 0;
614 			vcc = NULL;
615 			event_dump();
616 		}
617 		if (error) {
618 			static unsigned long silence = 0;
619 			static int last_error = 0;
620 
621 			if (error != last_error ||
622 			    time_after(jiffies, silence)  || silence == 0){
623 				printk(KERN_WARNING DEV_LABEL "(itf %d): "
624 				    "chan %d error %s\n",dev->number,chan,
625 				    err_txt[error]);
626 				last_error = error;
627 				silence = (jiffies+2*HZ)|1;
628 			}
629 			size = 0;
630 		}
631 		if (size && (size > cells*ATM_CELL_PAYLOAD-ATM_AAL5_TRAILER ||
632 		    size <= (cells-1)*ATM_CELL_PAYLOAD-ATM_AAL5_TRAILER)) {
633 			printk(KERN_ERR DEV_LABEL "(itf %d): size %d with %d "
634 			    "cells\n",dev->number,size,cells);
635 			size = 0;
636 			event_dump();
637 		}
638 		if (size > ATM_MAX_AAL5_PDU) {
639 			printk(KERN_ERR DEV_LABEL "(itf %d): size too big "
640 			    "(%d)\n",dev->number,size);
641 			size = 0;
642 			event_dump();
643 		}
644 		if (!size) {
645 			dev_kfree_skb_irq(skb);
646 			if (vcc) atomic_inc(&vcc->stats->rx_err);
647 			continue;
648 		}
649 		if (!atm_charge(vcc,skb->truesize)) {
650 			dev_kfree_skb_irq(skb);
651 			continue;
652 		}
653 		skb->len = size;
654 		ATM_SKB(skb)->vcc = vcc;
655 		vcc->push(vcc,skb);
656 		atomic_inc(&vcc->stats->rx);
657 	}
658 	zout(pos & 0xffff,MTA(mbx));
659 #if 0 /* probably a stupid idea */
660 	refill_pool(dev,zatm_vcc->pool);
661 		/* maybe this saves us a few interrupts */
662 #endif
663 }
664 
665 
open_rx_first(struct atm_vcc * vcc)666 static int open_rx_first(struct atm_vcc *vcc)
667 {
668 	struct zatm_dev *zatm_dev;
669 	struct zatm_vcc *zatm_vcc;
670 	unsigned long flags;
671 	unsigned short chan;
672 	int cells;
673 
674 	DPRINTK("open_rx_first (0x%x)\n",inb_p(0xc053));
675 	zatm_dev = ZATM_DEV(vcc->dev);
676 	zatm_vcc = ZATM_VCC(vcc);
677 	zatm_vcc->rx_chan = 0;
678 	if (vcc->qos.rxtp.traffic_class == ATM_NONE) return 0;
679 	if (vcc->qos.aal == ATM_AAL5) {
680 		if (vcc->qos.rxtp.max_sdu > 65464)
681 			vcc->qos.rxtp.max_sdu = 65464;
682 			/* fix this - we may want to receive 64kB SDUs
683 			   later */
684 		cells = (vcc->qos.rxtp.max_sdu+ATM_AAL5_TRAILER+
685 		    ATM_CELL_PAYLOAD-1)/ATM_CELL_PAYLOAD;
686 		zatm_vcc->pool = pool_index(cells*ATM_CELL_PAYLOAD);
687 	}
688 	else {
689 		cells = 1;
690 		zatm_vcc->pool = ZATM_AAL0_POOL;
691 	}
692 	if (zatm_vcc->pool < 0) return -EMSGSIZE;
693 	save_flags(flags);
694 	cli();
695 	zwait;
696 	zout(uPD98401_OPEN_CHAN,CMR);
697 	zwait;
698 	DPRINTK("0x%x 0x%x\n",zin(CMR),zin(CER));
699 	chan = (zin(CMR) & uPD98401_CHAN_ADDR) >> uPD98401_CHAN_ADDR_SHIFT;
700 	restore_flags(flags);
701 	DPRINTK("chan is %d\n",chan);
702 	if (!chan) return -EAGAIN;
703 	use_pool(vcc->dev,zatm_vcc->pool);
704 	DPRINTK("pool %d\n",zatm_vcc->pool);
705 	/* set up VC descriptor */
706 	cli();
707 	zpokel(zatm_dev,zatm_vcc->pool << uPD98401_RXVC_POOL_SHIFT,
708 	    chan*VC_SIZE/4);
709 	zpokel(zatm_dev,uPD98401_RXVC_OD | (vcc->qos.aal == ATM_AAL5 ?
710 	    uPD98401_RXVC_AR : 0) | cells,chan*VC_SIZE/4+1);
711 	zpokel(zatm_dev,0,chan*VC_SIZE/4+2);
712 	zatm_vcc->rx_chan = chan;
713 	zatm_dev->rx_map[chan] = vcc;
714 	restore_flags(flags);
715 	return 0;
716 }
717 
718 
open_rx_second(struct atm_vcc * vcc)719 static int open_rx_second(struct atm_vcc *vcc)
720 {
721 	struct zatm_dev *zatm_dev;
722 	struct zatm_vcc *zatm_vcc;
723 	unsigned long flags;
724 	int pos,shift;
725 
726 	DPRINTK("open_rx_second (0x%x)\n",inb_p(0xc053));
727 	zatm_dev = ZATM_DEV(vcc->dev);
728 	zatm_vcc = ZATM_VCC(vcc);
729 	if (!zatm_vcc->rx_chan) return 0;
730 	save_flags(flags);
731 	cli();
732 	/* should also handle VPI @@@ */
733 	pos = vcc->vci >> 1;
734 	shift = (1-(vcc->vci & 1)) << 4;
735 	zpokel(zatm_dev,(zpeekl(zatm_dev,pos) & ~(0xffff << shift)) |
736 	    ((zatm_vcc->rx_chan | uPD98401_RXLT_ENBL) << shift),pos);
737 	restore_flags(flags);
738 	return 0;
739 }
740 
741 
close_rx(struct atm_vcc * vcc)742 static void close_rx(struct atm_vcc *vcc)
743 {
744 	struct zatm_dev *zatm_dev;
745 	struct zatm_vcc *zatm_vcc;
746 	unsigned long flags;
747 	int pos,shift;
748 
749 	zatm_vcc = ZATM_VCC(vcc);
750 	zatm_dev = ZATM_DEV(vcc->dev);
751 	if (!zatm_vcc->rx_chan) return;
752 	DPRINTK("close_rx\n");
753 	/* disable receiver */
754 	save_flags(flags);
755 	if (vcc->vpi != ATM_VPI_UNSPEC && vcc->vci != ATM_VCI_UNSPEC) {
756 		cli();
757 		pos = vcc->vci >> 1;
758 		shift = (1-(vcc->vci & 1)) << 4;
759 		zpokel(zatm_dev,zpeekl(zatm_dev,pos) & ~(0xffff << shift),pos);
760 		zwait;
761 		zout(uPD98401_NOP,CMR);
762 		zwait;
763 		zout(uPD98401_NOP,CMR);
764 		restore_flags(flags);
765 	}
766 	cli();
767 	zwait;
768 	zout(uPD98401_DEACT_CHAN | uPD98401_CHAN_RT | (zatm_vcc->rx_chan <<
769 	    uPD98401_CHAN_ADDR_SHIFT),CMR);
770 	zwait;
771 	udelay(10); /* why oh why ... ? */
772 	zout(uPD98401_CLOSE_CHAN | uPD98401_CHAN_RT | (zatm_vcc->rx_chan <<
773 	    uPD98401_CHAN_ADDR_SHIFT),CMR);
774 	zwait;
775 	if (!(zin(CMR) & uPD98401_CHAN_ADDR))
776 		printk(KERN_CRIT DEV_LABEL "(itf %d): can't close RX channel "
777 		    "%d\n",vcc->dev->number,zatm_vcc->rx_chan);
778 	restore_flags(flags);
779 	zatm_dev->rx_map[zatm_vcc->rx_chan] = NULL;
780 	zatm_vcc->rx_chan = 0;
781 	unuse_pool(vcc->dev,zatm_vcc->pool);
782 }
783 
784 
start_rx(struct atm_dev * dev)785 static int start_rx(struct atm_dev *dev)
786 {
787 	struct zatm_dev *zatm_dev;
788 	int size,i;
789 
790 DPRINTK("start_rx\n");
791 	zatm_dev = ZATM_DEV(dev);
792 	size = sizeof(struct atm_vcc *)*zatm_dev->chans;
793 	zatm_dev->rx_map = (struct atm_vcc **) kmalloc(size,GFP_KERNEL);
794 	if (!zatm_dev->rx_map) return -ENOMEM;
795 	memset(zatm_dev->rx_map,0,size);
796 	/* set VPI/VCI split (use all VCIs and give what's left to VPIs) */
797 	zpokel(zatm_dev,(1 << dev->ci_range.vci_bits)-1,uPD98401_VRR);
798 	/* prepare free buffer pools */
799 	for (i = 0; i <= ZATM_LAST_POOL; i++) {
800 		zatm_dev->pool_info[i].ref_count = 0;
801 		zatm_dev->pool_info[i].rqa_count = 0;
802 		zatm_dev->pool_info[i].rqu_count = 0;
803 		zatm_dev->pool_info[i].low_water = LOW_MARK;
804 		zatm_dev->pool_info[i].high_water = HIGH_MARK;
805 		zatm_dev->pool_info[i].offset = 0;
806 		zatm_dev->pool_info[i].next_off = 0;
807 		zatm_dev->pool_info[i].next_cnt = 0;
808 		zatm_dev->pool_info[i].next_thres = OFF_CNG_THRES;
809 	}
810 	return 0;
811 }
812 
813 
814 /*----------------------------------- TX ------------------------------------*/
815 
816 
do_tx(struct sk_buff * skb)817 static int do_tx(struct sk_buff *skb)
818 {
819 	struct atm_vcc *vcc;
820 	struct zatm_dev *zatm_dev;
821 	struct zatm_vcc *zatm_vcc;
822 	u32 *dsc;
823 	unsigned long flags;
824 
825 	EVENT("do_tx\n",0,0);
826 	DPRINTK("sending skb %p\n",skb);
827 	vcc = ATM_SKB(skb)->vcc;
828 	zatm_dev = ZATM_DEV(vcc->dev);
829 	zatm_vcc = ZATM_VCC(vcc);
830 	EVENT("iovcnt=%d\n",skb_shinfo(skb)->nr_frags,0);
831 	save_flags(flags);
832 	cli();
833 	if (!skb_shinfo(skb)->nr_frags) {
834 		if (zatm_vcc->txing == RING_ENTRIES-1) {
835 			restore_flags(flags);
836 			return RING_BUSY;
837 		}
838 		zatm_vcc->txing++;
839 		dsc = zatm_vcc->ring+zatm_vcc->ring_curr;
840 		zatm_vcc->ring_curr = (zatm_vcc->ring_curr+RING_WORDS) &
841 		    (RING_ENTRIES*RING_WORDS-1);
842 		dsc[1] = 0;
843 		dsc[2] = skb->len;
844 		dsc[3] = virt_to_bus(skb->data);
845 		mb();
846 		dsc[0] = uPD98401_TXPD_V | uPD98401_TXPD_DP | uPD98401_TXPD_SM
847 		    | (vcc->qos.aal == ATM_AAL5 ? uPD98401_TXPD_AAL5 : 0 |
848 		    (ATM_SKB(skb)->atm_options & ATM_ATMOPT_CLP ?
849 		    uPD98401_CLPM_1 : uPD98401_CLPM_0));
850 		EVENT("dsc (0x%lx)\n",(unsigned long) dsc,0);
851 	}
852 	else {
853 printk("NONONONOO!!!!\n");
854 		dsc = NULL;
855 #if 0
856 		u32 *put;
857 		int i;
858 
859 		dsc = (u32 *) kmalloc(uPD98401_TXPD_SIZE*2+
860 		    uPD98401_TXBD_SIZE*ATM_SKB(skb)->iovcnt,GFP_ATOMIC);
861 		if (!dsc) {
862 			if (vcc->pop) vcc->pop(vcc,skb);
863 			else dev_kfree_skb_irq(skb);
864 			return -EAGAIN;
865 		}
866 		/* @@@ should check alignment */
867 		put = dsc+8;
868 		dsc[0] = uPD98401_TXPD_V | uPD98401_TXPD_DP |
869 		    (vcc->aal == ATM_AAL5 ? uPD98401_TXPD_AAL5 : 0 |
870 		    (ATM_SKB(skb)->atm_options & ATM_ATMOPT_CLP ?
871 		    uPD98401_CLPM_1 : uPD98401_CLPM_0));
872 		dsc[1] = 0;
873 		dsc[2] = ATM_SKB(skb)->iovcnt*uPD98401_TXBD_SIZE;
874 		dsc[3] = virt_to_bus(put);
875 		for (i = 0; i < ATM_SKB(skb)->iovcnt; i++) {
876 			*put++ = ((struct iovec *) skb->data)[i].iov_len;
877 			*put++ = virt_to_bus(((struct iovec *)
878 			    skb->data)[i].iov_base);
879 		}
880 		put[-2] |= uPD98401_TXBD_LAST;
881 #endif
882 	}
883 	ZATM_PRV_DSC(skb) = dsc;
884 	skb_queue_tail(&zatm_vcc->tx_queue,skb);
885 	DPRINTK("QRP=0x%08lx\n",zpeekl(zatm_dev,zatm_vcc->tx_chan*VC_SIZE/4+
886 	  uPD98401_TXVC_QRP));
887 	zwait;
888 	zout(uPD98401_TX_READY | (zatm_vcc->tx_chan <<
889 	    uPD98401_CHAN_ADDR_SHIFT),CMR);
890 	restore_flags(flags);
891 	EVENT("done\n",0,0);
892 	return 0;
893 }
894 
895 
dequeue_tx(struct atm_vcc * vcc)896 static inline void dequeue_tx(struct atm_vcc *vcc)
897 {
898 	struct zatm_vcc *zatm_vcc;
899 	struct sk_buff *skb;
900 
901 	EVENT("dequeue_tx\n",0,0);
902 	zatm_vcc = ZATM_VCC(vcc);
903 	skb = skb_dequeue(&zatm_vcc->tx_queue);
904 	if (!skb) {
905 		printk(KERN_CRIT DEV_LABEL "(itf %d): dequeue_tx but not "
906 		    "txing\n",vcc->dev->number);
907 		return;
908 	}
909 #if 0 /* @@@ would fail on CLP */
910 if (*ZATM_PRV_DSC(skb) != (uPD98401_TXPD_V | uPD98401_TXPD_DP |
911   uPD98401_TXPD_SM | uPD98401_TXPD_AAL5)) printk("@#*$!!!!  (%08x)\n",
912   *ZATM_PRV_DSC(skb));
913 #endif
914 	*ZATM_PRV_DSC(skb) = 0; /* mark as invalid */
915 	zatm_vcc->txing--;
916 	if (vcc->pop) vcc->pop(vcc,skb);
917 	else dev_kfree_skb_irq(skb);
918 	while ((skb = skb_dequeue(&zatm_vcc->backlog)))
919 		if (do_tx(skb) == RING_BUSY) {
920 			skb_queue_head(&zatm_vcc->backlog,skb);
921 			break;
922 		}
923 	atomic_inc(&vcc->stats->tx);
924 	wake_up(&zatm_vcc->tx_wait);
925 }
926 
927 
poll_tx(struct atm_dev * dev,int mbx)928 static void poll_tx(struct atm_dev *dev,int mbx)
929 {
930 	struct zatm_dev *zatm_dev;
931 	unsigned long pos;
932 	u32 x;
933 
934 	EVENT("poll_tx\n",0,0);
935 	zatm_dev = ZATM_DEV(dev);
936 	pos = (zatm_dev->mbx_start[mbx] & ~0xffffUL) | zin(MTA(mbx));
937 	while (x = zin(MWA(mbx)), (pos & 0xffff) != x) {
938 		int chan;
939 
940 #if 1
941 		u32 data,*addr;
942 
943 		EVENT("MBX: host 0x%lx, nic 0x%x\n",pos,x);
944 		addr = (u32 *) pos;
945 		data = *addr;
946 		chan = (data & uPD98401_TXI_CONN) >> uPD98401_TXI_CONN_SHIFT;
947 		EVENT("addr = 0x%lx, data = 0x%08x,",(unsigned long) addr,
948 		    data);
949 		EVENT("chan = %d\n",chan,0);
950 #else
951 NO !
952 		chan = (zatm_dev->mbx_start[mbx][pos >> 2] & uPD98401_TXI_CONN)
953 		>> uPD98401_TXI_CONN_SHIFT;
954 #endif
955 		if (chan < zatm_dev->chans && zatm_dev->tx_map[chan])
956 			dequeue_tx(zatm_dev->tx_map[chan]);
957 		else {
958 			printk(KERN_CRIT DEV_LABEL "(itf %d): TX indication "
959 			    "for non-existing channel %d\n",dev->number,chan);
960 			event_dump();
961 		}
962 		if (((pos += 4) & 0xffff) == zatm_dev->mbx_end[mbx])
963 			pos = zatm_dev->mbx_start[mbx];
964 	}
965 	zout(pos & 0xffff,MTA(mbx));
966 }
967 
968 
969 /*
970  * BUG BUG BUG: Doesn't handle "new-style" rate specification yet.
971  */
972 
alloc_shaper(struct atm_dev * dev,int * pcr,int min,int max,int ubr)973 static int alloc_shaper(struct atm_dev *dev,int *pcr,int min,int max,int ubr)
974 {
975 	struct zatm_dev *zatm_dev;
976 	unsigned long flags;
977 	unsigned long i,m,c;
978 	int shaper;
979 
980 	DPRINTK("alloc_shaper (min = %d, max = %d)\n",min,max);
981 	zatm_dev = ZATM_DEV(dev);
982 	if (!zatm_dev->free_shapers) return -EAGAIN;
983 	for (shaper = 0; !((zatm_dev->free_shapers >> shaper) & 1); shaper++);
984 	zatm_dev->free_shapers &= ~1 << shaper;
985 	if (ubr) {
986 		c = 5;
987 		i = m = 1;
988 		zatm_dev->ubr_ref_cnt++;
989 		zatm_dev->ubr = shaper;
990 	}
991 	else {
992 		if (min) {
993 			if (min <= 255) {
994 				i = min;
995 				m = ATM_OC3_PCR;
996 			}
997 			else {
998 				i = 255;
999 				m = ATM_OC3_PCR*255/min;
1000 			}
1001 		}
1002 		else {
1003 			if (max > zatm_dev->tx_bw) max = zatm_dev->tx_bw;
1004 			if (max <= 255) {
1005 				i = max;
1006 				m = ATM_OC3_PCR;
1007 			}
1008 			else {
1009 				i = 255;
1010 				m = (ATM_OC3_PCR*255+max-1)/max;
1011 			}
1012 		}
1013 		if (i > m) {
1014 			printk(KERN_CRIT DEV_LABEL "shaper algorithm botched "
1015 			    "[%d,%d] -> i=%ld,m=%ld\n",min,max,i,m);
1016 			m = i;
1017 		}
1018 		*pcr = i*ATM_OC3_PCR/m;
1019 		c = 20; /* @@@ should use max_cdv ! */
1020 		if ((min && *pcr < min) || (max && *pcr > max)) return -EINVAL;
1021 		if (zatm_dev->tx_bw < *pcr) return -EAGAIN;
1022 		zatm_dev->tx_bw -= *pcr;
1023 	}
1024 	save_flags(flags);
1025 	cli();
1026 	DPRINTK("i = %d, m = %d, PCR = %d\n",i,m,*pcr);
1027 	zpokel(zatm_dev,(i << uPD98401_IM_I_SHIFT) | m,uPD98401_IM(shaper));
1028 	zpokel(zatm_dev,c << uPD98401_PC_C_SHIFT,uPD98401_PC(shaper));
1029 	zpokel(zatm_dev,0,uPD98401_X(shaper));
1030 	zpokel(zatm_dev,0,uPD98401_Y(shaper));
1031 	zpokel(zatm_dev,uPD98401_PS_E,uPD98401_PS(shaper));
1032 	restore_flags(flags);
1033 	return shaper;
1034 }
1035 
1036 
dealloc_shaper(struct atm_dev * dev,int shaper)1037 static void dealloc_shaper(struct atm_dev *dev,int shaper)
1038 {
1039 	struct zatm_dev *zatm_dev;
1040 	unsigned long flags;
1041 
1042 	zatm_dev = ZATM_DEV(dev);
1043 	if (shaper == zatm_dev->ubr) {
1044 		if (--zatm_dev->ubr_ref_cnt) return;
1045 		zatm_dev->ubr = -1;
1046 	}
1047 	save_flags(flags);
1048 	cli();
1049 	zpokel(zatm_dev,zpeekl(zatm_dev,uPD98401_PS(shaper)) & ~uPD98401_PS_E,
1050 	    uPD98401_PS(shaper));
1051 	restore_flags(flags);
1052 	zatm_dev->free_shapers |= 1 << shaper;
1053 }
1054 
1055 
close_tx(struct atm_vcc * vcc)1056 static void close_tx(struct atm_vcc *vcc)
1057 {
1058 	struct zatm_dev *zatm_dev;
1059 	struct zatm_vcc *zatm_vcc;
1060 	unsigned long flags;
1061 	int chan;
1062 struct sk_buff *skb;
1063 int once = 1;
1064 
1065 	zatm_vcc = ZATM_VCC(vcc);
1066 	zatm_dev = ZATM_DEV(vcc->dev);
1067 	chan = zatm_vcc->tx_chan;
1068 	if (!chan) return;
1069 	DPRINTK("close_tx\n");
1070 	save_flags(flags);
1071 	cli();
1072 	while (skb_peek(&zatm_vcc->backlog)) {
1073 if (once) {
1074 printk("waiting for backlog to drain ...\n");
1075 event_dump();
1076 once = 0;
1077 }
1078 		sleep_on(&zatm_vcc->tx_wait);
1079 	}
1080 once = 1;
1081 	while ((skb = skb_peek(&zatm_vcc->tx_queue))) {
1082 if (once) {
1083 printk("waiting for TX queue to drain ... %p\n",skb);
1084 event_dump();
1085 once = 0;
1086 }
1087 		DPRINTK("waiting for TX queue to drain ... %p\n",skb);
1088 		sleep_on(&zatm_vcc->tx_wait);
1089 	}
1090 #if 0
1091 	zwait;
1092 	zout(uPD98401_DEACT_CHAN | (chan << uPD98401_CHAN_ADDR_SHIFT),CMR);
1093 #endif
1094 	zwait;
1095 	zout(uPD98401_CLOSE_CHAN | (chan << uPD98401_CHAN_ADDR_SHIFT),CMR);
1096 	zwait;
1097 	if (!(zin(CMR) & uPD98401_CHAN_ADDR))
1098 		printk(KERN_CRIT DEV_LABEL "(itf %d): can't close TX channel "
1099 		    "%d\n",vcc->dev->number,chan);
1100 	restore_flags(flags);
1101 	zatm_vcc->tx_chan = 0;
1102 	zatm_dev->tx_map[chan] = NULL;
1103 	if (zatm_vcc->shaper != zatm_dev->ubr) {
1104 		zatm_dev->tx_bw += vcc->qos.txtp.min_pcr;
1105 		dealloc_shaper(vcc->dev,zatm_vcc->shaper);
1106 	}
1107 	if (zatm_vcc->ring) kfree(zatm_vcc->ring);
1108 }
1109 
1110 
open_tx_first(struct atm_vcc * vcc)1111 static int open_tx_first(struct atm_vcc *vcc)
1112 {
1113 	struct zatm_dev *zatm_dev;
1114 	struct zatm_vcc *zatm_vcc;
1115 	unsigned long flags;
1116 	u32 *loop;
1117 	unsigned short chan;
1118 	int pcr,unlimited;
1119 
1120 	DPRINTK("open_tx_first\n");
1121 	zatm_dev = ZATM_DEV(vcc->dev);
1122 	zatm_vcc = ZATM_VCC(vcc);
1123 	zatm_vcc->tx_chan = 0;
1124 	if (vcc->qos.txtp.traffic_class == ATM_NONE) return 0;
1125 	save_flags(flags);
1126 	cli();
1127 	zwait;
1128 	zout(uPD98401_OPEN_CHAN,CMR);
1129 	zwait;
1130 	DPRINTK("0x%x 0x%x\n",zin(CMR),zin(CER));
1131 	chan = (zin(CMR) & uPD98401_CHAN_ADDR) >> uPD98401_CHAN_ADDR_SHIFT;
1132 	restore_flags(flags);
1133 	DPRINTK("chan is %d\n",chan);
1134 	if (!chan) return -EAGAIN;
1135 	unlimited = vcc->qos.txtp.traffic_class == ATM_UBR &&
1136 	    (!vcc->qos.txtp.max_pcr || vcc->qos.txtp.max_pcr == ATM_MAX_PCR ||
1137 	    vcc->qos.txtp.max_pcr >= ATM_OC3_PCR);
1138 	if (unlimited && zatm_dev->ubr != -1) zatm_vcc->shaper = zatm_dev->ubr;
1139 	else {
1140 		if (unlimited) vcc->qos.txtp.max_sdu = ATM_MAX_AAL5_PDU;
1141 		if ((zatm_vcc->shaper = alloc_shaper(vcc->dev,&pcr,
1142 		    vcc->qos.txtp.min_pcr,vcc->qos.txtp.max_pcr,unlimited))
1143 		    < 0) {
1144 			close_tx(vcc);
1145 			return zatm_vcc->shaper;
1146 		}
1147 		if (pcr > ATM_OC3_PCR) pcr = ATM_OC3_PCR;
1148 		vcc->qos.txtp.min_pcr = vcc->qos.txtp.max_pcr = pcr;
1149 	}
1150 	zatm_vcc->tx_chan = chan;
1151 	skb_queue_head_init(&zatm_vcc->tx_queue);
1152 	init_waitqueue_head(&zatm_vcc->tx_wait);
1153 	/* initialize ring */
1154 	zatm_vcc->ring = kmalloc(RING_SIZE,GFP_KERNEL);
1155 	if (!zatm_vcc->ring) return -ENOMEM;
1156 	memset(zatm_vcc->ring,0,RING_SIZE);
1157 	loop = zatm_vcc->ring+RING_ENTRIES*RING_WORDS;
1158 	loop[0] = uPD98401_TXPD_V;
1159 	loop[1] = loop[2] = 0;
1160 	loop[3] = virt_to_bus(zatm_vcc->ring);
1161 	zatm_vcc->ring_curr = 0;
1162 	zatm_vcc->txing = 0;
1163 	skb_queue_head_init(&zatm_vcc->backlog);
1164 	zpokel(zatm_dev,virt_to_bus(zatm_vcc->ring),
1165 	    chan*VC_SIZE/4+uPD98401_TXVC_QRP);
1166 	return 0;
1167 }
1168 
1169 
open_tx_second(struct atm_vcc * vcc)1170 static int open_tx_second(struct atm_vcc *vcc)
1171 {
1172 	struct zatm_dev *zatm_dev;
1173 	struct zatm_vcc *zatm_vcc;
1174 	unsigned long flags;
1175 
1176 	DPRINTK("open_tx_second\n");
1177 	zatm_dev = ZATM_DEV(vcc->dev);
1178 	zatm_vcc = ZATM_VCC(vcc);
1179 	if (!zatm_vcc->tx_chan) return 0;
1180 	save_flags(flags);
1181 	/* set up VC descriptor */
1182 	cli();
1183 	zpokel(zatm_dev,0,zatm_vcc->tx_chan*VC_SIZE/4);
1184 	zpokel(zatm_dev,uPD98401_TXVC_L | (zatm_vcc->shaper <<
1185 	    uPD98401_TXVC_SHP_SHIFT) | (vcc->vpi << uPD98401_TXVC_VPI_SHIFT) |
1186 	    vcc->vci,zatm_vcc->tx_chan*VC_SIZE/4+1);
1187 	zpokel(zatm_dev,0,zatm_vcc->tx_chan*VC_SIZE/4+2);
1188 	restore_flags(flags);
1189 	zatm_dev->tx_map[zatm_vcc->tx_chan] = vcc;
1190 	return 0;
1191 }
1192 
1193 
start_tx(struct atm_dev * dev)1194 static int start_tx(struct atm_dev *dev)
1195 {
1196 	struct zatm_dev *zatm_dev;
1197 	int i;
1198 
1199 	DPRINTK("start_tx\n");
1200 	zatm_dev = ZATM_DEV(dev);
1201 	zatm_dev->tx_map = (struct atm_vcc **) kmalloc(sizeof(struct atm_vcc *)*
1202 	    zatm_dev->chans,GFP_KERNEL);
1203 	if (!zatm_dev->tx_map) return -ENOMEM;
1204 	zatm_dev->tx_bw = ATM_OC3_PCR;
1205 	zatm_dev->free_shapers = (1 << NR_SHAPERS)-1;
1206 	zatm_dev->ubr = -1;
1207 	zatm_dev->ubr_ref_cnt = 0;
1208 	/* initialize shapers */
1209 	for (i = 0; i < NR_SHAPERS; i++) zpokel(zatm_dev,0,uPD98401_PS(i));
1210 	return 0;
1211 }
1212 
1213 
1214 /*------------------------------- interrupts --------------------------------*/
1215 
1216 
zatm_int(int irq,void * dev_id,struct pt_regs * regs)1217 static void zatm_int(int irq,void *dev_id,struct pt_regs *regs)
1218 {
1219 	struct atm_dev *dev;
1220 	struct zatm_dev *zatm_dev;
1221 	u32 reason;
1222 
1223 	dev = dev_id;
1224 	zatm_dev = ZATM_DEV(dev);
1225 	while ((reason = zin(GSR))) {
1226 		EVENT("reason 0x%x\n",reason,0);
1227 		if (reason & uPD98401_INT_PI) {
1228 			EVENT("PHY int\n",0,0);
1229 			dev->phy->interrupt(dev);
1230 		}
1231 		if (reason & uPD98401_INT_RQA) {
1232 			unsigned long pools;
1233 			int i;
1234 
1235 			pools = zin(RQA);
1236 			EVENT("RQA (0x%08x)\n",pools,0);
1237 			for (i = 0; pools; i++) {
1238 				if (pools & 1) {
1239 					refill_pool(dev,i);
1240 					zatm_dev->pool_info[i].rqa_count++;
1241 				}
1242 				pools >>= 1;
1243 			}
1244 		}
1245 		if (reason & uPD98401_INT_RQU) {
1246 			unsigned long pools;
1247 			int i;
1248 			pools = zin(RQU);
1249 			printk(KERN_WARNING DEV_LABEL "(itf %d): RQU 0x%08lx\n",
1250 			    dev->number,pools);
1251 			event_dump();
1252 			for (i = 0; pools; i++) {
1253 				if (pools & 1) {
1254 					refill_pool(dev,i);
1255 					zatm_dev->pool_info[i].rqu_count++;
1256 				}
1257 				pools >>= 1;
1258 			}
1259 		}
1260 		/* don't handle RD */
1261 		if (reason & uPD98401_INT_SPE)
1262 			printk(KERN_ALERT DEV_LABEL "(itf %d): system parity "
1263 			    "error at 0x%08x\n",dev->number,zin(ADDR));
1264 		if (reason & uPD98401_INT_CPE)
1265 			printk(KERN_ALERT DEV_LABEL "(itf %d): control memory "
1266 			    "parity error at 0x%08x\n",dev->number,zin(ADDR));
1267 		if (reason & uPD98401_INT_SBE) {
1268 			printk(KERN_ALERT DEV_LABEL "(itf %d): system bus "
1269 			    "error at 0x%08x\n",dev->number,zin(ADDR));
1270 			event_dump();
1271 		}
1272 		/* don't handle IND */
1273 		if (reason & uPD98401_INT_MF) {
1274 			printk(KERN_CRIT DEV_LABEL "(itf %d): mailbox full "
1275 			    "(0x%x)\n",dev->number,(reason & uPD98401_INT_MF)
1276 			    >> uPD98401_INT_MF_SHIFT);
1277 			event_dump();
1278 			    /* @@@ should try to recover */
1279 		}
1280 		if (reason & uPD98401_INT_MM) {
1281 			if (reason & 1) poll_rx(dev,0);
1282 			if (reason & 2) poll_rx(dev,1);
1283 			if (reason & 4) poll_tx(dev,2);
1284 			if (reason & 8) poll_tx(dev,3);
1285 		}
1286 		/* @@@ handle RCRn */
1287 	}
1288 }
1289 
1290 
1291 /*----------------------------- (E)EPROM access -----------------------------*/
1292 
1293 
eprom_set(struct zatm_dev * zatm_dev,unsigned long value,unsigned short cmd)1294 static void __init eprom_set(struct zatm_dev *zatm_dev,unsigned long value,
1295     unsigned short cmd)
1296 {
1297 	int error;
1298 
1299 	if ((error = pci_write_config_dword(zatm_dev->pci_dev,cmd,value)))
1300 		printk(KERN_ERR DEV_LABEL ": PCI write failed (0x%02x)\n",
1301 		    error);
1302 }
1303 
1304 
eprom_get(struct zatm_dev * zatm_dev,unsigned short cmd)1305 static unsigned long __init eprom_get(struct zatm_dev *zatm_dev,
1306     unsigned short cmd)
1307 {
1308 	unsigned int value;
1309 	int error;
1310 
1311 	if ((error = pci_read_config_dword(zatm_dev->pci_dev,cmd,&value)))
1312 		printk(KERN_ERR DEV_LABEL ": PCI read failed (0x%02x)\n",
1313 		    error);
1314 	return value;
1315 }
1316 
1317 
eprom_put_bits(struct zatm_dev * zatm_dev,unsigned long data,int bits,unsigned short cmd)1318 static void __init eprom_put_bits(struct zatm_dev *zatm_dev,
1319     unsigned long data,int bits,unsigned short cmd)
1320 {
1321 	unsigned long value;
1322 	int i;
1323 
1324 	for (i = bits-1; i >= 0; i--) {
1325 		value = ZEPROM_CS | (((data >> i) & 1) ? ZEPROM_DI : 0);
1326 		eprom_set(zatm_dev,value,cmd);
1327 		eprom_set(zatm_dev,value | ZEPROM_SK,cmd);
1328 		eprom_set(zatm_dev,value,cmd);
1329 	}
1330 }
1331 
1332 
eprom_get_byte(struct zatm_dev * zatm_dev,unsigned char * byte,unsigned short cmd)1333 static void __init eprom_get_byte(struct zatm_dev *zatm_dev,
1334     unsigned char *byte,unsigned short cmd)
1335 {
1336 	int i;
1337 
1338 	*byte = 0;
1339 	for (i = 8; i; i--) {
1340 		eprom_set(zatm_dev,ZEPROM_CS,cmd);
1341 		eprom_set(zatm_dev,ZEPROM_CS | ZEPROM_SK,cmd);
1342 		*byte <<= 1;
1343 		if (eprom_get(zatm_dev,cmd) & ZEPROM_DO) *byte |= 1;
1344 		eprom_set(zatm_dev,ZEPROM_CS,cmd);
1345 	}
1346 }
1347 
1348 
eprom_try_esi(struct atm_dev * dev,unsigned short cmd,int offset,int swap)1349 static unsigned char __init eprom_try_esi(struct atm_dev *dev,
1350     unsigned short cmd,int offset,int swap)
1351 {
1352 	unsigned char buf[ZEPROM_SIZE];
1353 	struct zatm_dev *zatm_dev;
1354 	int i;
1355 
1356 	zatm_dev = ZATM_DEV(dev);
1357 	for (i = 0; i < ZEPROM_SIZE; i += 2) {
1358 		eprom_set(zatm_dev,ZEPROM_CS,cmd); /* select EPROM */
1359 		eprom_put_bits(zatm_dev,ZEPROM_CMD_READ,ZEPROM_CMD_LEN,cmd);
1360 		eprom_put_bits(zatm_dev,i >> 1,ZEPROM_ADDR_LEN,cmd);
1361 		eprom_get_byte(zatm_dev,buf+i+swap,cmd);
1362 		eprom_get_byte(zatm_dev,buf+i+1-swap,cmd);
1363 		eprom_set(zatm_dev,0,cmd); /* deselect EPROM */
1364 	}
1365 	memcpy(dev->esi,buf+offset,ESI_LEN);
1366 	return memcmp(dev->esi,"\0\0\0\0\0",ESI_LEN); /* assumes ESI_LEN == 6 */
1367 }
1368 
1369 
eprom_get_esi(struct atm_dev * dev)1370 static void __init eprom_get_esi(struct atm_dev *dev)
1371 {
1372 	if (eprom_try_esi(dev,ZEPROM_V1_REG,ZEPROM_V1_ESI_OFF,1)) return;
1373 	(void) eprom_try_esi(dev,ZEPROM_V2_REG,ZEPROM_V2_ESI_OFF,0);
1374 }
1375 
1376 
1377 /*--------------------------------- entries ---------------------------------*/
1378 
1379 
zatm_init(struct atm_dev * dev)1380 static int __init zatm_init(struct atm_dev *dev)
1381 {
1382 	struct zatm_dev *zatm_dev;
1383 	struct pci_dev *pci_dev;
1384 	unsigned short command;
1385 	unsigned char revision;
1386 	int error,i,last;
1387 	unsigned long t0,t1,t2;
1388 
1389 	DPRINTK(">zatm_init\n");
1390 	zatm_dev = ZATM_DEV(dev);
1391 	pci_dev = zatm_dev->pci_dev;
1392 	zatm_dev->base = pci_resource_start(pci_dev, 0);
1393 	zatm_dev->irq = pci_dev->irq;
1394 	if ((error = pci_read_config_word(pci_dev,PCI_COMMAND,&command)) ||
1395 	    (error = pci_read_config_byte(pci_dev,PCI_REVISION_ID,&revision))) {
1396 		printk(KERN_ERR DEV_LABEL "(itf %d): init error 0x%02x\n",
1397 		    dev->number,error);
1398 		return -EINVAL;
1399 	}
1400 	if ((error = pci_write_config_word(pci_dev,PCI_COMMAND,
1401 	    command | PCI_COMMAND_IO | PCI_COMMAND_MASTER))) {
1402 		printk(KERN_ERR DEV_LABEL "(itf %d): can't enable IO (0x%02x)"
1403 		    "\n",dev->number,error);
1404 		return -EIO;
1405 	}
1406 	eprom_get_esi(dev);
1407 	printk(KERN_NOTICE DEV_LABEL "(itf %d): rev.%d,base=0x%x,irq=%d,",
1408 	    dev->number,revision,zatm_dev->base,zatm_dev->irq);
1409 	/* reset uPD98401 */
1410 	zout(0,SWR);
1411 	while (!(zin(GSR) & uPD98401_INT_IND));
1412 	zout(uPD98401_GMR_ONE /*uPD98401_BURST4*/,GMR);
1413 	last = MAX_CRAM_SIZE;
1414 	for (i = last-RAM_INCREMENT; i >= 0; i -= RAM_INCREMENT) {
1415 		zpokel(zatm_dev,0x55555555,i);
1416 		if (zpeekl(zatm_dev,i) != 0x55555555) last = i;
1417 		else {
1418 			zpokel(zatm_dev,0xAAAAAAAA,i);
1419 			if (zpeekl(zatm_dev,i) != 0xAAAAAAAA) last = i;
1420 			else zpokel(zatm_dev,i,i);
1421 		}
1422 	}
1423 	for (i = 0; i < last; i += RAM_INCREMENT)
1424 		if (zpeekl(zatm_dev,i) != i) break;
1425 	zatm_dev->mem = i << 2;
1426 	while (i) zpokel(zatm_dev,0,--i);
1427 	/* reset again to rebuild memory pointers */
1428 	zout(0,SWR);
1429 	while (!(zin(GSR) & uPD98401_INT_IND));
1430 	zout(uPD98401_GMR_ONE | uPD98401_BURST8 | uPD98401_BURST4 |
1431 	    uPD98401_BURST2 | uPD98401_GMR_PM | uPD98401_GMR_DR,GMR);
1432 	/* TODO: should shrink allocation now */
1433 	printk("mem=%dkB,%s (",zatm_dev->mem >> 10,zatm_dev->copper ? "UTP" :
1434 	    "MMF");
1435 	for (i = 0; i < ESI_LEN; i++)
1436 		printk("%02X%s",dev->esi[i],i == ESI_LEN-1 ? ")\n" : "-");
1437 	do {
1438 		unsigned long flags;
1439 
1440 		save_flags(flags);
1441 		cli();
1442 		t0 = zpeekl(zatm_dev,uPD98401_TSR);
1443 		udelay(10);
1444 		t1 = zpeekl(zatm_dev,uPD98401_TSR);
1445 		udelay(1010);
1446 		t2 = zpeekl(zatm_dev,uPD98401_TSR);
1447 		restore_flags(flags);
1448 	}
1449 	while (t0 > t1 || t1 > t2); /* loop if wrapping ... */
1450 	zatm_dev->khz = t2-2*t1+t0;
1451 	printk(KERN_NOTICE DEV_LABEL "(itf %d): uPD98401 %d.%d at %d.%03d "
1452 	    "MHz\n",dev->number,
1453 	    (zin(VER) & uPD98401_MAJOR) >> uPD98401_MAJOR_SHIFT,
1454             zin(VER) & uPD98401_MINOR,zatm_dev->khz/1000,zatm_dev->khz % 1000);
1455 #ifdef CONFIG_ATM_ZATM_EXACT_TS
1456 	zatm_clock_init(zatm_dev);
1457 #endif
1458 	return uPD98402_init(dev);
1459 }
1460 
1461 
zatm_start(struct atm_dev * dev)1462 static int __init zatm_start(struct atm_dev *dev)
1463 {
1464 	struct zatm_dev *zatm_dev;
1465 	unsigned long curr;
1466 	int pools,vccs,rx;
1467 	int error,i,ld;
1468 
1469 	DPRINTK("zatm_start\n");
1470 	zatm_dev = ZATM_DEV(dev);
1471 	zatm_dev->rx_map = zatm_dev->tx_map = NULL;
1472 	for (i = 0; i < NR_MBX; i++)
1473 		zatm_dev->mbx_start[i] = 0;
1474 	if (request_irq(zatm_dev->irq,&zatm_int,SA_SHIRQ,DEV_LABEL,dev)) {
1475 		printk(KERN_ERR DEV_LABEL "(itf %d): IRQ%d is already in use\n",
1476 		    dev->number,zatm_dev->irq);
1477 		return -EAGAIN;
1478 	}
1479 	request_region(zatm_dev->base,uPD98401_PORTS,DEV_LABEL);
1480 	/* define memory regions */
1481 	pools = NR_POOLS;
1482 	if (NR_SHAPERS*SHAPER_SIZE > pools*POOL_SIZE)
1483 		pools = NR_SHAPERS*SHAPER_SIZE/POOL_SIZE;
1484 	vccs = (zatm_dev->mem-NR_SHAPERS*SHAPER_SIZE-pools*POOL_SIZE)/
1485 	    (2*VC_SIZE+RX_SIZE);
1486 	ld = -1;
1487 	for (rx = 1; rx < vccs; rx <<= 1) ld++;
1488 	dev->ci_range.vpi_bits = 0; /* @@@ no VPI for now */
1489 	dev->ci_range.vci_bits = ld;
1490 	dev->link_rate = ATM_OC3_PCR;
1491 	zatm_dev->chans = vccs; /* ??? */
1492 	curr = rx*RX_SIZE/4;
1493 	DPRINTK("RX pool 0x%08lx\n",curr);
1494 	zpokel(zatm_dev,curr,uPD98401_PMA); /* receive pool */
1495 	zatm_dev->pool_base = curr;
1496 	curr += pools*POOL_SIZE/4;
1497 	DPRINTK("Shapers 0x%08lx\n",curr);
1498 	zpokel(zatm_dev,curr,uPD98401_SMA); /* shapers */
1499 	curr += NR_SHAPERS*SHAPER_SIZE/4;
1500 	DPRINTK("Free    0x%08lx\n",curr);
1501 	zpokel(zatm_dev,curr,uPD98401_TOS); /* free pool */
1502 	printk(KERN_INFO DEV_LABEL "(itf %d): %d shapers, %d pools, %d RX, "
1503 	    "%ld VCs\n",dev->number,NR_SHAPERS,pools,rx,
1504 	    (zatm_dev->mem-curr*4)/VC_SIZE);
1505 	/* create mailboxes */
1506 	for (i = 0; i < NR_MBX; i++)
1507 		if (mbx_entries[i]) {
1508 			unsigned long here;
1509 
1510 			here = (unsigned long) kmalloc(2*MBX_SIZE(i),
1511 			    GFP_KERNEL);
1512 			if (!here) {
1513 				error = -ENOMEM;
1514 				goto out;
1515 			}
1516 			if ((here^(here+MBX_SIZE(i))) & ~0xffffUL)/* paranoia */
1517 				here = (here & ~0xffffUL)+0x10000;
1518 			zatm_dev->mbx_start[i] = here;
1519 			if ((here^virt_to_bus((void *) here)) & 0xffff) {
1520 				printk(KERN_ERR DEV_LABEL "(itf %d): system "
1521 				    "bus incompatible with driver\n",
1522 				    dev->number);
1523 				error = -ENODEV;
1524 				goto out;
1525 			}
1526 			DPRINTK("mbx@0x%08lx-0x%08lx\n",here,here+MBX_SIZE(i));
1527 			zatm_dev->mbx_end[i] = (here+MBX_SIZE(i)) & 0xffff;
1528 			zout(virt_to_bus((void *) here) >> 16,MSH(i));
1529 			zout(virt_to_bus((void *) here),MSL(i));
1530 			zout((here+MBX_SIZE(i)) & 0xffff,MBA(i));
1531 			zout(here & 0xffff,MTA(i));
1532 			zout(here & 0xffff,MWA(i));
1533 		}
1534 	error = start_tx(dev);
1535 	if (error) goto out;
1536 	error = start_rx(dev);
1537 	if (error) goto out;
1538 	error = dev->phy->start(dev);
1539 	if (error) goto out;
1540 	zout(0xffffffff,IMR); /* enable interrupts */
1541 	/* enable TX & RX */
1542 	zout(zin(GMR) | uPD98401_GMR_SE | uPD98401_GMR_RE,GMR);
1543 	return 0;
1544     out:
1545 	for (i = 0; i < NR_MBX; i++)
1546 		if (zatm_dev->mbx_start[i] != 0)
1547 			kfree((void *) zatm_dev->mbx_start[i]);
1548 	if (zatm_dev->rx_map != NULL)
1549 		kfree(zatm_dev->rx_map);
1550 	if (zatm_dev->tx_map != NULL)
1551 		kfree(zatm_dev->tx_map);
1552 	free_irq(zatm_dev->irq, dev);
1553 	return error;
1554 }
1555 
1556 
zatm_close(struct atm_vcc * vcc)1557 static void zatm_close(struct atm_vcc *vcc)
1558 {
1559         DPRINTK(">zatm_close\n");
1560         if (!ZATM_VCC(vcc)) return;
1561 	clear_bit(ATM_VF_READY,&vcc->flags);
1562         close_rx(vcc);
1563 	EVENT("close_tx\n",0,0);
1564         close_tx(vcc);
1565         DPRINTK("zatm_close: done waiting\n");
1566         /* deallocate memory */
1567         kfree(ZATM_VCC(vcc));
1568 	vcc->dev_data = NULL;
1569 	clear_bit(ATM_VF_ADDR,&vcc->flags);
1570 }
1571 
1572 
zatm_open(struct atm_vcc * vcc,short vpi,int vci)1573 static int zatm_open(struct atm_vcc *vcc,short vpi,int vci)
1574 {
1575 	struct zatm_dev *zatm_dev;
1576 	struct zatm_vcc *zatm_vcc;
1577 	int error;
1578 
1579 	DPRINTK(">zatm_open\n");
1580 	zatm_dev = ZATM_DEV(vcc->dev);
1581 	if (!test_bit(ATM_VF_PARTIAL,&vcc->flags)) vcc->dev_data = NULL;
1582 	error = atm_find_ci(vcc,&vpi,&vci);
1583 	if (error) return error;
1584 	vcc->vpi = vpi;
1585 	vcc->vci = vci;
1586 	if (vci != ATM_VPI_UNSPEC && vpi != ATM_VCI_UNSPEC)
1587 		set_bit(ATM_VF_ADDR,&vcc->flags);
1588 	if (vcc->qos.aal != ATM_AAL5) return -EINVAL; /* @@@ AAL0 */
1589 	DPRINTK(DEV_LABEL "(itf %d): open %d.%d\n",vcc->dev->number,vcc->vpi,
1590 	    vcc->vci);
1591 	if (!test_bit(ATM_VF_PARTIAL,&vcc->flags)) {
1592 		zatm_vcc = kmalloc(sizeof(struct zatm_vcc),GFP_KERNEL);
1593 		if (!zatm_vcc) {
1594 			clear_bit(ATM_VF_ADDR,&vcc->flags);
1595 			return -ENOMEM;
1596 		}
1597 		vcc->dev_data = zatm_vcc;
1598 		ZATM_VCC(vcc)->tx_chan = 0; /* for zatm_close after open_rx */
1599 		if ((error = open_rx_first(vcc))) {
1600 	                zatm_close(vcc);
1601 	                return error;
1602 	        }
1603 		if ((error = open_tx_first(vcc))) {
1604 			zatm_close(vcc);
1605 			return error;
1606 	        }
1607 	}
1608 	if (vci == ATM_VPI_UNSPEC || vpi == ATM_VCI_UNSPEC) return 0;
1609 	if ((error = open_rx_second(vcc))) {
1610 		zatm_close(vcc);
1611 		return error;
1612         }
1613 	if ((error = open_tx_second(vcc))) {
1614 		zatm_close(vcc);
1615 		return error;
1616         }
1617 	set_bit(ATM_VF_READY,&vcc->flags);
1618         return 0;
1619 }
1620 
1621 
zatm_change_qos(struct atm_vcc * vcc,struct atm_qos * qos,int flags)1622 static int zatm_change_qos(struct atm_vcc *vcc,struct atm_qos *qos,int flags)
1623 {
1624 	printk("Not yet implemented\n");
1625 	return -ENOSYS;
1626 	/* @@@ */
1627 }
1628 
1629 
zatm_ioctl(struct atm_dev * dev,unsigned int cmd,void * arg)1630 static int zatm_ioctl(struct atm_dev *dev,unsigned int cmd,void *arg)
1631 {
1632 	struct zatm_dev *zatm_dev;
1633 	unsigned long flags;
1634 
1635 	zatm_dev = ZATM_DEV(dev);
1636 	switch (cmd) {
1637 		case ZATM_GETPOOLZ:
1638 			if (!capable(CAP_NET_ADMIN)) return -EPERM;
1639 			/* fall through */
1640 		case ZATM_GETPOOL:
1641 			{
1642 				struct zatm_pool_info info;
1643 				int pool;
1644 
1645 				if (get_user(pool,
1646 				    &((struct zatm_pool_req *) arg)->pool_num))
1647 					return -EFAULT;
1648 				if (pool < 0 || pool > ZATM_LAST_POOL)
1649 					return -EINVAL;
1650 				save_flags(flags);
1651 				cli();
1652 				info = zatm_dev->pool_info[pool];
1653 				if (cmd == ZATM_GETPOOLZ) {
1654 					zatm_dev->pool_info[pool].rqa_count = 0;
1655 					zatm_dev->pool_info[pool].rqu_count = 0;
1656 				}
1657 				restore_flags(flags);
1658 				return copy_to_user(
1659 				    &((struct zatm_pool_req *) arg)->info,
1660 				    &info,sizeof(info)) ? -EFAULT : 0;
1661 			}
1662 		case ZATM_SETPOOL:
1663 			{
1664 				struct zatm_pool_info info;
1665 				int pool;
1666 
1667 				if (!capable(CAP_NET_ADMIN)) return -EPERM;
1668 				if (get_user(pool,
1669 				    &((struct zatm_pool_req *) arg)->pool_num))
1670 					return -EFAULT;
1671 				if (pool < 0 || pool > ZATM_LAST_POOL)
1672 					return -EINVAL;
1673 				if (copy_from_user(&info,
1674 				    &((struct zatm_pool_req *) arg)->info,
1675 				    sizeof(info))) return -EFAULT;
1676 				if (!info.low_water)
1677 					info.low_water = zatm_dev->
1678 					    pool_info[pool].low_water;
1679 				if (!info.high_water)
1680 					info.high_water = zatm_dev->
1681 					    pool_info[pool].high_water;
1682 				if (!info.next_thres)
1683 					info.next_thres = zatm_dev->
1684 					    pool_info[pool].next_thres;
1685 				if (info.low_water >= info.high_water ||
1686 				    info.low_water < 0)
1687 					return -EINVAL;
1688 				save_flags(flags);
1689 				cli();
1690 				zatm_dev->pool_info[pool].low_water =
1691 				    info.low_water;
1692 				zatm_dev->pool_info[pool].high_water =
1693 				    info.high_water;
1694 				zatm_dev->pool_info[pool].next_thres =
1695 				    info.next_thres;
1696 				restore_flags(flags);
1697 				return 0;
1698 			}
1699 #ifdef CONFIG_ATM_ZATM_EXACT_TS
1700 		case ZATM_GETTHIST:
1701 			{
1702 				int i;
1703 				struct zatm_t_hist hs[ZATM_TIMER_HISTORY_SIZE];
1704 				save_flags(flags);
1705 				cli();
1706 				for (i = 0; i < ZATM_TIMER_HISTORY_SIZE; i++)
1707 					hs[i] = zatm_dev->timer_history[
1708 					    (zatm_dev->th_curr+i) &
1709 					    (ZATM_TIMER_HISTORY_SIZE-1)];
1710 				restore_flags(flags);
1711 				return copy_to_user((struct zatm_t_hist *) arg,
1712 				    hs, sizeof(hs)) ? -EFAULT : 0;
1713 			}
1714 #endif
1715 		default:
1716         		if (!dev->phy->ioctl) return -ENOIOCTLCMD;
1717 		        return dev->phy->ioctl(dev,cmd,arg);
1718 	}
1719 }
1720 
1721 
zatm_getsockopt(struct atm_vcc * vcc,int level,int optname,void * optval,int optlen)1722 static int zatm_getsockopt(struct atm_vcc *vcc,int level,int optname,
1723     void *optval,int optlen)
1724 {
1725 	return -EINVAL;
1726 }
1727 
1728 
zatm_setsockopt(struct atm_vcc * vcc,int level,int optname,void * optval,int optlen)1729 static int zatm_setsockopt(struct atm_vcc *vcc,int level,int optname,
1730     void *optval,int optlen)
1731 {
1732 	return -EINVAL;
1733 }
1734 
1735 
1736 #if 0
1737 static int zatm_sg_send(struct atm_vcc *vcc,unsigned long start,
1738     unsigned long size)
1739 {
1740 	return vcc->aal == ATM_AAL5;
1741 	   /* @@@ should check size and maybe alignment*/
1742 }
1743 #endif
1744 
1745 
zatm_send(struct atm_vcc * vcc,struct sk_buff * skb)1746 static int zatm_send(struct atm_vcc *vcc,struct sk_buff *skb)
1747 {
1748 	int error;
1749 
1750 	EVENT(">zatm_send 0x%lx\n",(unsigned long) skb,0);
1751 	if (!ZATM_VCC(vcc)->tx_chan || !test_bit(ATM_VF_READY,&vcc->flags)) {
1752 		if (vcc->pop) vcc->pop(vcc,skb);
1753 		else dev_kfree_skb(skb);
1754 		return -EINVAL;
1755 	}
1756 	if (!skb) {
1757 		printk(KERN_CRIT "!skb in zatm_send ?\n");
1758 		if (vcc->pop) vcc->pop(vcc,skb);
1759 		return -EINVAL;
1760 	}
1761 	ATM_SKB(skb)->vcc = vcc;
1762 	error = do_tx(skb);
1763 	if (error != RING_BUSY) return error;
1764 	skb_queue_tail(&ZATM_VCC(vcc)->backlog,skb);
1765 	return 0;
1766 }
1767 
1768 
zatm_phy_put(struct atm_dev * dev,unsigned char value,unsigned long addr)1769 static void zatm_phy_put(struct atm_dev *dev,unsigned char value,
1770     unsigned long addr)
1771 {
1772 	struct zatm_dev *zatm_dev;
1773 
1774 	zatm_dev = ZATM_DEV(dev);
1775 	zwait;
1776 	zout(value,CER);
1777 	zout(uPD98401_IND_ACC | uPD98401_IA_B0 |
1778 	    (uPD98401_IA_TGT_PHY << uPD98401_IA_TGT_SHIFT) | addr,CMR);
1779 }
1780 
1781 
zatm_phy_get(struct atm_dev * dev,unsigned long addr)1782 static unsigned char zatm_phy_get(struct atm_dev *dev,unsigned long addr)
1783 {
1784 	struct zatm_dev *zatm_dev;
1785 
1786 	zatm_dev = ZATM_DEV(dev);
1787 	zwait;
1788 	zout(uPD98401_IND_ACC | uPD98401_IA_B0 | uPD98401_IA_RW |
1789 	  (uPD98401_IA_TGT_PHY << uPD98401_IA_TGT_SHIFT) | addr,CMR);
1790 	zwait;
1791 	return zin(CER) & 0xff;
1792 }
1793 
1794 
1795 static const struct atmdev_ops ops = {
1796 	open:		zatm_open,
1797 	close:		zatm_close,
1798 	ioctl:		zatm_ioctl,
1799 	getsockopt:	zatm_getsockopt,
1800 	setsockopt:	zatm_setsockopt,
1801 	send:		zatm_send,
1802 	/*zatm_sg_send*/
1803 	phy_put:	zatm_phy_put,
1804 	phy_get:	zatm_phy_get,
1805 	feedback:	zatm_feedback,
1806 	change_qos:	zatm_change_qos,
1807 };
1808 
1809 
zatm_detect(void)1810 int __init zatm_detect(void)
1811 {
1812 	struct atm_dev *dev;
1813 	struct zatm_dev *zatm_dev;
1814 	int devs,type;
1815 
1816 	zatm_dev = (struct zatm_dev *) kmalloc(sizeof(struct zatm_dev),
1817 	    GFP_KERNEL);
1818 	if (!zatm_dev) return -ENOMEM;
1819 	devs = 0;
1820 	for (type = 0; type < 2; type++) {
1821 		struct pci_dev *pci_dev;
1822 
1823 		pci_dev = NULL;
1824 		while ((pci_dev = pci_find_device(PCI_VENDOR_ID_ZEITNET,type ?
1825 		    PCI_DEVICE_ID_ZEITNET_1225 : PCI_DEVICE_ID_ZEITNET_1221,
1826 		    pci_dev))) {
1827 			if (pci_enable_device(pci_dev)) break;
1828 			dev = atm_dev_register(DEV_LABEL,&ops,-1,NULL);
1829 			if (!dev) break;
1830 			zatm_dev->pci_dev = pci_dev;
1831 			dev->dev_data = zatm_dev;
1832 			zatm_dev->copper = type;
1833 			if (zatm_init(dev) || zatm_start(dev)) {
1834 				atm_dev_deregister(dev);
1835 				break;
1836 			}
1837 			zatm_dev->more = zatm_boards;
1838 			zatm_boards = dev;
1839 			devs++;
1840 			zatm_dev = (struct zatm_dev *) kmalloc(sizeof(struct
1841 			    zatm_dev),GFP_KERNEL);
1842 			if (!zatm_dev) {
1843 				printk(KERN_EMERG "zatm.c: memory shortage\n");
1844 				return devs;
1845 			}
1846 		}
1847 	}
1848 	kfree(zatm_dev);
1849 	return devs;
1850 }
1851 
1852 
1853 #ifdef MODULE
1854 
1855 MODULE_LICENSE("GPL");
1856 
init_module(void)1857 int init_module(void)
1858 {
1859 	if (!zatm_detect()) {
1860 		printk(KERN_ERR DEV_LABEL ": no adapter found\n");
1861 		return -ENXIO;
1862 	}
1863 	MOD_INC_USE_COUNT;
1864 	return 0;
1865 }
1866 
1867 
cleanup_module(void)1868 void cleanup_module(void)
1869 {
1870 	/*
1871 	 * Well, there's no way to get rid of the driver yet, so we don't
1872 	 * have to clean up, right ? :-)
1873 	 */
1874 }
1875 
1876 #endif
1877