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