6 #include <linux/module.h>
7 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/atm.h>
12 #include <linux/atmdev.h>
13 #include <linux/sonet.h>
15 #include <linux/netdevice.h>
17 #include <linux/uio.h>
22 #include <linux/capability.h>
23 #include <linux/bitops.h>
24 #include <linux/wait.h>
25 #include <linux/slab.h>
26 #include <asm/byteorder.h>
27 #include <asm/string.h>
30 #include <asm/uaccess.h>
52 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
54 #define DPRINTK(format,args...)
57 #ifndef CONFIG_ATM_ZATM_DEBUG
65 static void event_dump(
void)
77 #define NULLCHECK(x) \
78 if ((unsigned long) (x) < 0x30) printk(KERN_CRIT #x "==0x%x\n", (int) (x))
87 static const char *ev[EV];
88 static unsigned long ev_a[EV],ev_b[EV];
92 static void EVENT(
const char *
s,
unsigned long a,
unsigned long b)
101 static void event_dump(
void)
106 for (n = 0; n < EV; n++) {
109 printk(ev[i] ? ev[i] :
"(null)",ev_a[i],ev_b[i]);
122 static unsigned long dummy[2] = {0,0};
125 #define zin_n(r) inl(zatm_dev->base+r*4)
126 #define zin(r) inl(zatm_dev->base+uPD98401_##r*4)
127 #define zout(v,r) outl(v,zatm_dev->base+uPD98401_##r*4)
128 #define zwait while (zin(CMR) & uPD98401_BUSY)
131 static const int mbx_entries[
NR_MBX] = { 1024,1024,1024,1024 };
132 static const int mbx_esize[
NR_MBX] = { 16,16,4,4 };
134 #define MBX_SIZE(i) (mbx_entries[i]*mbx_esize[i])
180 struct zatm_dev *zatm_dev;
186 EVENT(
"refill_pool\n",0,0);
196 offset = zatm_dev->
pool_info[pool].offset+
201 free = zpeekl(zatm_dev,zatm_dev->
pool_base+2*pool) &
203 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
204 if (free >= zatm_dev->
pool_info[pool].low_water)
return;
205 EVENT(
"starting ... POOL: 0x%x, 0x%x\n",
206 zpeekl(zatm_dev,zatm_dev->
pool_base+2*pool),
207 zpeekl(zatm_dev,zatm_dev->
pool_base+2*pool+1));
208 EVENT(
"dummy: 0x%08lx, 0x%08lx\n",dummy[0],dummy[1]);
211 while (free < zatm_dev->
pool_info[pool].high_water) {
217 "skb (%d) with %d free\n",dev->
number,size,free);
220 skb_reserve(skb,(
unsigned char *) ((((
unsigned long) skb->
data+
221 align+offset-1) & ~(
unsigned long) (align-1))-offset)-
225 if (!first) first =
head;
230 EVENT(
"enq skb 0x%08lx/0x%08lx\n",(
unsigned long) skb,
231 (
unsigned long) head);
238 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
247 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
248 EVENT (
"POOL: 0x%x, 0x%x\n",
249 zpeekl(zatm_dev,zatm_dev->
pool_base+2*pool),
250 zpeekl(zatm_dev,zatm_dev->
pool_base+2*pool+1));
251 EVENT(
"dummy: 0x%08lx, 0x%08lx\n",dummy[0],dummy[1]);
256 static void drain_free(
struct atm_dev *dev,
int pool)
268 "max_pdu is %d\n",max_pdu);
269 if (max_pdu > 65536)
return -1;
270 for (i = 0; (64 <<
i) < max_pdu; i++);
278 static void use_pool(
struct atm_dev *dev,
int pool)
280 struct zatm_dev *zatm_dev;
285 if (!(zatm_dev->
pool_info[pool].ref_count++)) {
286 skb_queue_head_init(&zatm_dev->
pool[pool]);
288 if (size < 0) size = 0;
289 else if (size > 10) size = 10;
291 zpokel(zatm_dev,((zatm_dev->
pool_info[pool].low_water/4) <<
296 zpokel(zatm_dev,(
unsigned long) dummy,zatm_dev->
pool_base+
298 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
300 refill_pool(dev,pool);
306 static void unuse_pool(
struct atm_dev *dev,
int pool)
309 drain_free(dev,pool);
318 static int count = 0;
324 if (count++ > 2)
return;
325 for (i = 0; i < 8; i++)
326 printk(
"TX%d: 0x%08lx\n",i,
328 for (i = 0; i < 5; i++)
329 printk(
"SH%d: 0x%08lx\n",i,
331 qrp = (
unsigned long *) zpeekl(zatm_dev,zatm_vcc->
tx_chan*
VC_SIZE/4+
333 printk(
"qrp=0x%08lx\n",(
unsigned long) qrp);
334 for (i = 0; i < 4; i++)
printk(
"QRP[%d]: 0x%08lx",i,qrp[i]);
339 static const char *err_txt[] = {
343 "Maximum len violation",
359 static void poll_rx(
struct atm_dev *dev,
int mbx)
361 struct zatm_dev *zatm_dev;
366 EVENT(
"poll_rx\n",0,0);
369 while (x =
zin(MWA(mbx)), (pos & 0xffff) != x) {
375 EVENT(
"MBX: host 0x%lx, nic 0x%x\n",pos,x);
377 if (((pos += 16) & 0xffff) == zatm_dev->
mbx_end[mbx])
381 printk(
"RX IND: 0x%x, 0x%x, 0x%x, 0x%x\n",here[0],here[1],here[2],here[3]);
384 printk(
"POOL: 0x%08x, 0x%08x\n",zpeekl(zatm_dev,
387 x = (
unsigned long *) here[2];
388 printk(
"[0..3] = 0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx\n",
389 x[0],x[1],x[2],x[3]);
402 __net_timestamp(skb);
404 printk(
"[-3..0] 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",((
unsigned *) skb->
data)[-3],
405 ((
unsigned *) skb->
data)[-2],((
unsigned *) skb->
data)[-1],
406 ((
unsigned *) skb->
data)[0]);
408 EVENT(
"skb 0x%lx, here 0x%lx\n",(
unsigned long) skb,
409 (
unsigned long) here);
411 printk(
"dummy: 0x%08lx, 0x%08lx\n",dummy[0],dummy[1]);
413 size = error ? 0 :
ntohs(((
__be16 *) skb->data)[cells*
414 ATM_CELL_PAYLOAD/
sizeof(
u16)-3]);
415 EVENT(
"got skb 0x%lx, size %d\n",(
unsigned long) skb,size);
420 vcc = zatm_dev->
rx_map[chan];
428 "for non-existing channel\n",dev->
number);
434 static unsigned long silence = 0;
435 static int last_error = 0;
437 if (error != last_error ||
438 time_after(jiffies, silence) || silence == 0){
440 "chan %d error %s\n",dev->
number,chan,
450 "cells\n",dev->
number,size,cells);
456 "(%d)\n",dev->
number,size);
474 zout(pos & 0xffff,MTA(mbx));
476 refill_pool(dev,zatm_vcc->
pool);
482 static int open_rx_first(
struct atm_vcc *vcc)
484 struct zatm_dev *zatm_dev;
485 struct zatm_vcc *zatm_vcc;
494 if (vcc->
qos.rxtp.traffic_class ==
ATM_NONE)
return 0;
496 if (vcc->
qos.rxtp.max_sdu > 65464)
497 vcc->
qos.rxtp.max_sdu = 65464;
515 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
517 if (!chan)
return -
EAGAIN;
518 use_pool(vcc->
dev,zatm_vcc->
pool);
526 zpokel(zatm_dev,0,chan*
VC_SIZE/4+2);
529 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
534 static int open_rx_second(
struct atm_vcc *vcc)
536 struct zatm_dev *zatm_dev;
537 struct zatm_vcc *zatm_vcc;
544 if (!zatm_vcc->
rx_chan)
return 0;
548 shift = (1-(vcc->
vci & 1)) << 4;
549 zpokel(zatm_dev,(zpeekl(zatm_dev,pos) & ~(0xffff << shift)) |
551 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
556 static void close_rx(
struct atm_vcc *vcc)
558 struct zatm_dev *zatm_dev;
559 struct zatm_vcc *zatm_vcc;
565 if (!zatm_vcc->
rx_chan)
return;
571 shift = (1-(vcc->
vci & 1)) << 4;
572 zpokel(zatm_dev,zpeekl(zatm_dev,pos) & ~(0xffff << shift),pos);
577 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
591 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
594 unuse_pool(vcc->
dev,zatm_vcc->
pool);
598 static int start_rx(
struct atm_dev *dev)
600 struct zatm_dev *zatm_dev;
629 static int do_tx(
struct sk_buff *skb)
632 struct zatm_dev *zatm_dev;
633 struct zatm_vcc *zatm_vcc;
637 EVENT(
"do_tx\n",0,0);
638 DPRINTK(
"sending skb %p\n",skb);
642 EVENT(
"iovcnt=%d\n",skb_shinfo(skb)->nr_frags,0);
644 if (!skb_shinfo(skb)->nr_frags) {
646 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
661 EVENT(
"dsc (0x%lx)\n",(
unsigned long) dsc,0);
664 printk(
"NONONONOO!!!!\n");
688 for (i = 0; i <
ATM_SKB(skb)->iovcnt; i++) {
689 *put++ = ((
struct iovec *) skb->
data)[
i].iov_len;
691 skb->
data)[i].iov_base);
703 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
709 static inline void dequeue_tx(
struct atm_vcc *vcc)
711 struct zatm_vcc *zatm_vcc;
714 EVENT(
"dequeue_tx\n",0,0);
719 "txing\n",vcc->
dev->number);
729 if (vcc->
pop) vcc->
pop(vcc,skb);
741 static void poll_tx(
struct atm_dev *dev,
int mbx)
743 struct zatm_dev *zatm_dev;
747 EVENT(
"poll_tx\n",0,0);
750 while (x =
zin(MWA(mbx)), (pos & 0xffff) != x) {
756 EVENT(
"MBX: host 0x%lx, nic 0x%x\n",pos,x);
760 EVENT(
"addr = 0x%lx, data = 0x%08x,",(
unsigned long) addr,
762 EVENT(
"chan = %d\n",chan,0);
769 dequeue_tx(zatm_dev->
tx_map[chan]);
772 "for non-existing channel %d\n",dev->
number,chan);
775 if (((pos += 4) & 0xffff) == zatm_dev->
mbx_end[mbx])
778 zout(pos & 0xffff,MTA(mbx));
786 static int alloc_shaper(
struct atm_dev *dev,
int *pcr,
int min,
int max,
int ubr)
788 struct zatm_dev *zatm_dev;
793 DPRINTK(
"alloc_shaper (min = %d, max = %d)\n",min,max);
796 for (shaper = 0; !((zatm_dev->
free_shapers >> shaper) & 1); shaper++);
802 zatm_dev->
ubr = shaper;
817 if (max > zatm_dev->
tx_bw) max = zatm_dev->
tx_bw;
829 "[%d,%d] -> i=%ld,m=%ld\n",min,max,i,m);
834 if ((min && *pcr < min) || (max && *pcr > max))
return -
EINVAL;
836 zatm_dev->
tx_bw -= *pcr;
839 DPRINTK(
"i = %d, m = %d, PCR = %d\n",i,m,*pcr);
845 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
850 static void dealloc_shaper(
struct atm_dev *dev,
int shaper)
852 struct zatm_dev *zatm_dev;
856 if (shaper == zatm_dev->
ubr) {
863 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
868 static void close_tx(
struct atm_vcc *vcc)
870 struct zatm_dev *zatm_dev;
871 struct zatm_vcc *zatm_vcc;
880 if (skb_peek(&zatm_vcc->
backlog)) {
881 printk(
"waiting for backlog to drain ...\n");
885 if (skb_peek(&zatm_vcc->
tx_queue)) {
886 printk(
"waiting for TX queue to drain ...\n");
900 "%d\n",vcc->
dev->number,chan);
901 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
905 zatm_dev->
tx_bw += vcc->
qos.txtp.min_pcr;
906 dealloc_shaper(vcc->
dev,zatm_vcc->
shaper);
912 static int open_tx_first(
struct atm_vcc *vcc)
914 struct zatm_dev *zatm_dev;
915 struct zatm_vcc *zatm_vcc;
925 if (vcc->
qos.txtp.traffic_class ==
ATM_NONE)
return 0;
932 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
934 if (!chan)
return -
EAGAIN;
935 unlimited = vcc->
qos.txtp.traffic_class ==
ATM_UBR &&
938 if (unlimited && zatm_dev->
ubr != -1) zatm_vcc->
shaper = zatm_dev->
ubr;
943 if ((zatm_vcc->
shaper = alloc_shaper(vcc->
dev,&pcr,
944 vcc->
qos.txtp.min_pcr,vcc->
qos.txtp.max_pcr,unlimited))
950 vcc->
qos.txtp.min_pcr = vcc->
qos.txtp.max_pcr = pcr;
953 skb_queue_head_init(&zatm_vcc->
tx_queue);
960 loop[1] = loop[2] = 0;
964 skb_queue_head_init(&zatm_vcc->
backlog);
971 static int open_tx_second(
struct atm_vcc *vcc)
973 struct zatm_dev *zatm_dev;
974 struct zatm_vcc *zatm_vcc;
980 if (!zatm_vcc->
tx_chan)
return 0;
988 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
994 static int start_tx(
struct atm_dev *dev)
996 struct zatm_dev *zatm_dev;
1020 struct zatm_dev *zatm_dev;
1026 while ((reason =
zin(
GSR))) {
1028 EVENT(
"reason 0x%x\n",reason,0);
1030 EVENT(
"PHY int\n",0,0);
1031 dev->
phy->interrupt(dev);
1034 unsigned long pools;
1038 EVENT(
"RQA (0x%08x)\n",pools,0);
1039 for (i = 0; pools; i++) {
1048 unsigned long pools;
1054 for (i = 0; pools; i++) {
1077 "(0x%x)\n",dev->
number,(reason & uPD98401_INT_MF)
1083 if (reason & 1) poll_rx(dev,0);
1084 if (reason & 2) poll_rx(dev,1);
1085 if (reason & 4) poll_tx(dev,2);
1086 if (reason & 8) poll_tx(dev,3);
1097 static void __devinit eprom_set(
struct zatm_dev *zatm_dev,
unsigned long value,
1102 if ((error = pci_write_config_dword(zatm_dev->
pci_dev,cmd,value)))
1108 static unsigned long __devinit eprom_get(
struct zatm_dev *zatm_dev,
1114 if ((error = pci_read_config_dword(zatm_dev->
pci_dev,cmd,&value)))
1121 static void __devinit eprom_put_bits(
struct zatm_dev *zatm_dev,
1122 unsigned long data,
int bits,
unsigned short cmd)
1124 unsigned long value;
1127 for (i = bits-1; i >= 0; i--) {
1129 eprom_set(zatm_dev,value,cmd);
1130 eprom_set(zatm_dev,value |
ZEPROM_SK,cmd);
1131 eprom_set(zatm_dev,value,cmd);
1136 static void __devinit eprom_get_byte(
struct zatm_dev *zatm_dev,
1137 unsigned char *
byte,
unsigned short cmd)
1142 for (i = 8;
i; i--) {
1146 if (eprom_get(zatm_dev,cmd) &
ZEPROM_DO) *byte |= 1;
1153 unsigned short cmd,
int offset,
int swap)
1156 struct zatm_dev *zatm_dev;
1164 eprom_get_byte(zatm_dev,buf+i+swap,cmd);
1165 eprom_get_byte(zatm_dev,buf+i+1-swap,cmd);
1166 eprom_set(zatm_dev,0,cmd);
1185 struct zatm_dev *zatm_dev;
1196 zatm_dev->
irq = pci_dev->
irq;
1197 if ((error = pci_read_config_word(pci_dev,
PCI_COMMAND,&command))) {
1202 if ((error = pci_write_config_word(pci_dev,
PCI_COMMAND,
1217 zpokel(zatm_dev,0x55555555,i);
1218 if (zpeekl(zatm_dev,i) != 0x55555555) last =
i;
1220 zpokel(zatm_dev,0xAAAAAAAA,i);
1221 if (zpeekl(zatm_dev,i) != 0xAAAAAAAA) last =
i;
1222 else zpokel(zatm_dev,i,i);
1226 if (zpeekl(zatm_dev,i) != i)
break;
1227 zatm_dev->
mem = i << 2;
1228 while (i) zpokel(zatm_dev,0,--i);
1231 while (!(
zin(
GSR) & uPD98401_INT_IND));
1238 printk(
"%02X%s",dev->
esi[i],i == ESI_LEN-1 ?
")\n" :
"-");
1240 unsigned long flags;
1248 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
1250 while (t0 > t1 || t1 > t2);
1251 zatm_dev->
khz = t2-2*t1+
t0;
1262 struct zatm_dev *zatm_dev =
ZATM_DEV(dev);
1270 for (i = 0; i <
NR_MBX; i++)
1285 for (rx = 1; rx < vccs; rx <<= 1) ld++;
1289 zatm_dev->
chans = vccs;
1291 DPRINTK(
"RX pool 0x%08lx\n",curr);
1294 curr += pools*POOL_SIZE/4;
1295 DPRINTK(
"Shapers 0x%08lx\n",curr);
1298 DPRINTK(
"Free 0x%08lx\n",curr);
1301 "%ld VCs\n",dev->
number,NR_SHAPERS,pools,rx,
1304 for (i = 0; i <
NR_MBX; i++) {
1308 if (!mbx_entries[i])
1319 if (((
unsigned long)mbx ^ mbx_dma) & 0xffff) {
1321 "bus incompatible with driver\n", dev->
number);
1331 zout(mbx_dma >> 16, MSH(i));
1332 zout(mbx_dma, MSL(i));
1334 zout((
unsigned long)mbx & 0xffff, MTA(i));
1335 zout((
unsigned long)mbx & 0xffff, MWA(i));
1337 error = start_tx(dev);
1340 error = start_rx(dev);
1343 error = dev->
phy->start(dev);
1367 static void zatm_close(
struct atm_vcc *vcc)
1373 EVENT(
"close_tx\n",0,0);
1375 DPRINTK(
"zatm_close: done waiting\n");
1383 static int zatm_open(
struct atm_vcc *vcc)
1385 struct zatm_dev *zatm_dev;
1386 struct zatm_vcc *zatm_vcc;
1387 short vpi = vcc->
vpi;
1398 DPRINTK(DEV_LABEL
"(itf %d): open %d.%d\n",vcc->
dev->number,vcc->
vpi,
1408 if ((error = open_rx_first(vcc))) {
1412 if ((error = open_tx_first(vcc))) {
1418 if ((error = open_rx_second(vcc))) {
1422 if ((error = open_tx_second(vcc))) {
1431 static int zatm_change_qos(
struct atm_vcc *vcc,
struct atm_qos *qos,
int flags)
1433 printk(
"Not yet implemented\n");
1439 static int zatm_ioctl(
struct atm_dev *dev,
unsigned int cmd,
void __user *
arg)
1441 struct zatm_dev *zatm_dev;
1442 unsigned long flags;
1462 zatm_dev->
pool_info[pool].rqa_count = 0;
1463 zatm_dev->
pool_info[pool].rqu_count = 0;
1465 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
1484 if (!
info.low_water)
1485 info.low_water = zatm_dev->
1487 if (!
info.high_water)
1488 info.high_water = zatm_dev->
1490 if (!
info.next_thres)
1491 info.next_thres = zatm_dev->
1493 if (
info.low_water >=
info.high_water ||
1503 spin_unlock_irqrestore(&zatm_dev->
lock, flags);
1508 return dev->
phy->ioctl(dev,cmd,arg);
1513 static int zatm_getsockopt(
struct atm_vcc *vcc,
int level,
int optname,
1514 void __user *optval,
int optlen)
1520 static int zatm_setsockopt(
struct atm_vcc *vcc,
int level,
int optname,
1521 void __user *optval,
unsigned int optlen)
1530 EVENT(
">zatm_send 0x%lx\n",(
unsigned long) skb,0);
1532 if (vcc->
pop) vcc->
pop(vcc,skb);
1533 else dev_kfree_skb(skb);
1538 if (vcc->
pop) vcc->
pop(vcc,skb);
1549 static void zatm_phy_put(
struct atm_dev *dev,
unsigned char value,
1552 struct zatm_dev *zatm_dev;
1562 static unsigned char zatm_phy_get(
struct atm_dev *dev,
unsigned long addr)
1564 struct zatm_dev *zatm_dev;
1577 .close = zatm_close,
1578 .ioctl = zatm_ioctl,
1579 .getsockopt = zatm_getsockopt,
1580 .setsockopt = zatm_setsockopt,
1582 .phy_put = zatm_phy_put,
1583 .phy_get = zatm_phy_get,
1584 .change_qos = zatm_change_qos,
1587 static int __devinit zatm_init_one(
struct pci_dev *pci_dev,
1591 struct zatm_dev *zatm_dev;
1606 goto out_deregister;
1615 if ((ret = zatm_init(dev)) || (ret = zatm_start(dev)))
1618 pci_set_drvdata(pci_dev, dev);
1619 zatm_dev->
more = zatm_boards;
1648 .id_table = zatm_pci_tbl,
1649 .probe = zatm_init_one,
1652 static int __init zatm_init_module(
void)
1654 return pci_register_driver(&zatm_driver);