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