This source file includes following definitions.
- atp_init
- atp_probe1
- init_dev
- get_node_ID
- eeprom_op
- net_open
- hardware_init
- trigger_send
- write_packet
- net_send_packet
- net_interrupt
- net_rx
- read_block
- net_close
- net_get_stats
- set_multicast_list
1
2
3
4
5
6
7
8
9
10
11
12
13
14 static char *version =
15 "atp.c:v0.04 2/25/94 Donald Becker (becker@super.org)\n";
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 #include <linux/config.h>
78 #include <linux/kernel.h>
79 #include <linux/sched.h>
80 #include <linux/types.h>
81 #include <linux/fcntl.h>
82 #include <linux/interrupt.h>
83 #include <linux/ptrace.h>
84 #include <linux/ioport.h>
85 #include <linux/in.h>
86 #include <linux/malloc.h>
87 #include <linux/string.h>
88 #include <asm/system.h>
89 #include <asm/bitops.h>
90 #include <asm/io.h>
91 #include <asm/dma.h>
92 #include <errno.h>
93
94 #include "dev.h"
95 #include "eth.h"
96 #include "skbuff.h"
97 #include "arp.h"
98
99 #include "atp.h"
100
101
102 #ifndef HAVE_AUTOIRQ
103
104 extern void autoirq_setup(int waittime);
105 extern int autoirq_report(int waittime);
106
107
108 extern struct device *irq2dev_map[16];
109 #endif
110
111 #ifndef HAVE_ALLOC_SKB
112 #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
113 #define kfree_skbmem(addr, size) kfree_s(addr,size);
114 #endif
115
116 #ifndef HAVE_PORTRESERVE
117 #define check_region(ioaddr, size) 0
118 #define snarf_region(ioaddr, size); do ; while (0)
119 #endif
120
121
122 #ifndef NET_DEBUG
123 #define NET_DEBUG 4
124 #endif
125 static unsigned int net_debug = NET_DEBUG;
126
127
128 #define ETHERCARD_TOTAL_SIZE 3
129
130
131
132 extern int atp_probe(struct device *dev);
133
134 static int atp_probe1(struct device *dev, short ioaddr);
135 static void init_dev(struct device *dev);
136 static void get_node_ID(struct device *dev);
137 static unsigned short eeprom_op(short ioaddr, unsigned int cmd);
138 static int net_open(struct device *dev);
139 static void hardware_init(struct device *dev);
140 static void write_packet(short ioaddr, int length, unsigned char *packet, int mode);
141 static void trigger_send(short ioaddr, int length);
142 static int net_send_packet(struct sk_buff *skb, struct device *dev);
143 static void net_interrupt(int reg_ptr);
144 static void net_rx(struct device *dev);
145 static void read_block(short ioaddr, int length, unsigned char *buffer, int data_mode);
146 static int net_close(struct device *dev);
147 static struct enet_statistics *net_get_stats(struct device *dev);
148 #ifdef HAVE_MULTICAST
149 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
150 #endif
151
152
153
154
155
156
157
158
159 int
160 atp_init(struct device *dev)
161 {
162 int *port, ports[] = {0x378, 0x278, 0x3bc, 0};
163 int base_addr = dev->base_addr;
164
165 if (base_addr > 0x1ff)
166 return atp_probe1(dev, base_addr);
167 else if (base_addr == 1)
168 return ENXIO;
169
170 for (port = ports; *port; port++) {
171 int ioaddr = *port;
172 outb(0x57, ioaddr + PAR_DATA);
173 if (inb(ioaddr + PAR_DATA) != 0x57)
174 continue;
175 if (atp_probe1(dev, ioaddr) == 0)
176 return 0;
177 }
178
179 return ENODEV;
180 }
181
182 static int atp_probe1(struct device *dev, short ioaddr)
183 {
184 int saved_ctrl_reg, status;
185
186 outb(0xff, ioaddr + PAR_DATA);
187
188
189 saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
190
191 outb(0x04, ioaddr + PAR_CONTROL);
192 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
193 eeprom_delay(2048);
194 status = read_nibble(ioaddr, CMR1);
195
196 if ((status & 0x78) != 0x08) {
197
198 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
199 return 1;
200 }
201 status = read_nibble(ioaddr, CMR2_h);
202 if ((status & 0x78) != 0x10) {
203 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
204 return 1;
205 }
206
207 write_reg_byte(ioaddr, CMR2, 0x01);
208 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
209
210
211 if (ioaddr == 0x378)
212 dev->irq = 7;
213 else
214 dev->irq = 5;
215 write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF);
216 write_reg(ioaddr, CMR2, CMR2_NULL);
217
218 dev->base_addr = ioaddr;
219
220
221 get_node_ID(dev);
222
223 printk("%s: Pocket adaptor found at %#3x, IRQ %d, SAPROM "
224 "%02X:%02X:%02X:%02X:%02X:%02X.\n", dev->name, dev->base_addr,
225 dev->irq, dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
226 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
227
228
229 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
230
231 if (net_debug)
232 printk(version);
233
234
235 init_dev(dev);
236 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
237 memset(dev->priv, 0, sizeof(struct net_local));
238
239
240 {
241 struct net_local *lp = (struct net_local *)dev->priv;
242 lp->addr_mode = CMR2h_Normal;
243 }
244
245
246 dev->if_port = (dev->mem_start & 0xf) ? dev->mem_start & 0x7 : 4;
247 if (dev->mem_end & 0xf)
248 net_debug = dev->mem_end & 7;
249
250 dev->open = net_open;
251 dev->stop = net_close;
252 dev->hard_start_xmit = net_send_packet;
253 dev->get_stats = net_get_stats;
254 #ifdef HAVE_MULTICAST
255 dev->set_multicast_list = &set_multicast_list;
256 #endif
257
258 return 0;
259 }
260
261
262
263 static void init_dev(struct device *dev)
264 {
265 int i;
266
267 for (i = 0; i < DEV_NUMBUFFS; i++)
268 dev->buffs[i] = NULL;
269
270 dev->hard_header = eth_header;
271 dev->add_arp = eth_add_arp;
272 dev->queue_xmit = dev_queue_xmit;
273 dev->rebuild_header = eth_rebuild_header;
274 dev->type_trans = eth_type_trans;
275
276 dev->type = ARPHRD_ETHER;
277 dev->hard_header_len = ETH_HLEN;
278 dev->mtu = 1500;
279 dev->addr_len = ETH_ALEN;
280 for (i = 0; i < ETH_ALEN; i++) {
281 dev->broadcast[i]=0xff;
282 }
283
284
285 dev->flags = IFF_BROADCAST;
286 dev->family = AF_INET;
287 dev->pa_addr = 0;
288 dev->pa_brdaddr = 0;
289 dev->pa_mask = 0;
290 dev->pa_alen = sizeof(unsigned long);
291 }
292
293
294 static void get_node_ID(struct device *dev)
295 {
296 short ioaddr = dev->base_addr;
297 int sa_offset = 0;
298 int i;
299
300 write_reg(ioaddr, CMR2, CMR2_EEPROM);
301
302
303
304 if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
305 sa_offset = 15;
306
307 for (i = 0; i < 3; i++)
308 ((unsigned short *)dev->dev_addr)[i] =
309 ntohs(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
310
311 write_reg(ioaddr, CMR2, CMR2_NULL);
312 }
313
314
315
316
317
318
319
320
321
322
323
324
325
326 static unsigned short eeprom_op(short ioaddr, unsigned int cmd)
327 {
328 unsigned eedata_out = 0;
329 int num_bits = EE_CMD_SIZE;
330
331 while (--num_bits >= 0) {
332 char outval = test_bit(num_bits, &cmd) ? EE_DATA_WRITE : 0;
333 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
334 eeprom_delay(5);
335 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
336 eedata_out <<= 1;
337 if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
338 eedata_out++;
339 eeprom_delay(5);
340 }
341 write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
342 return eedata_out;
343 }
344
345
346
347
348
349
350
351
352
353
354
355
356 static int net_open(struct device *dev)
357 {
358
359
360
361
362 if (irq2dev_map[dev->irq] != 0
363 || (irq2dev_map[dev->irq] = dev) == 0
364 || request_irq(dev->irq, &net_interrupt)) {
365 return -EAGAIN;
366 }
367
368 hardware_init(dev);
369 dev->start = 1;
370 return 0;
371 }
372
373
374
375 static void hardware_init(struct device *dev)
376 {
377 struct net_local *lp = (struct net_local *)dev->priv;
378 int ioaddr = dev->base_addr;
379 int i;
380
381 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
382
383 for (i = 0; i < 6; i++)
384 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
385
386 write_reg_high(ioaddr, CMR2, lp->addr_mode);
387
388 if (net_debug > 2) {
389 printk("%s: Reset: current Rx mode %d.\n", dev->name,
390 (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
391 }
392
393 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
394 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
395
396
397 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
398
399
400 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
401 write_reg_high(ioaddr, IMR, ISRh_RxErr);
402
403 lp->tx_unit_busy = 0;
404 lp->pac_cnt_in_tx_buf = 0;
405 lp->saved_tx_size = 0;
406
407 dev->tbusy = 0;
408 dev->interrupt = 0;
409 }
410
411 static void trigger_send(short ioaddr, int length)
412 {
413 write_reg_byte(ioaddr, TxCNT0, length & 0xff);
414 write_reg(ioaddr, TxCNT1, length >> 8);
415 write_reg(ioaddr, CMR1, CMR1_Xmit);
416 }
417
418 static void write_packet(short ioaddr, int length, unsigned char *packet, int data_mode)
419 {
420 length = (length + 1) & ~1;
421 outb(EOC+MAR, ioaddr + PAR_DATA);
422 if ((data_mode & 1) == 0) {
423
424 outb(WrAddr+MAR, ioaddr + PAR_DATA);
425 do {
426 write_byte_mode0(ioaddr, *packet++);
427 } while (--length > 0) ;
428 } else {
429
430 unsigned char outbyte = *packet++;
431
432 outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
433 outb(WrAddr+MAR, ioaddr + PAR_DATA);
434
435 outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
436 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
437 outbyte >>= 4;
438 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
439 outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
440 while (--length > 0)
441 write_byte_mode1(ioaddr, *packet++);
442 }
443
444 outb(0xff, ioaddr + PAR_DATA);
445 outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
446 }
447
448 static int
449 net_send_packet(struct sk_buff *skb, struct device *dev)
450 {
451 struct net_local *lp = (struct net_local *)dev->priv;
452 int ioaddr = dev->base_addr;
453
454 if (dev->tbusy) {
455
456
457 int tickssofar = jiffies - dev->trans_start;
458 if (tickssofar < 5)
459 return 1;
460 printk("%s: transmit timed out, %s?\n", dev->name,
461 inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
462 : "IRQ conflict");
463 lp->stats.tx_errors++;
464
465 hardware_init(dev);
466 dev->tbusy=0;
467 dev->trans_start = jiffies;
468 }
469
470
471
472
473 if (skb == NULL) {
474 dev_tint(dev);
475 return 0;
476 }
477
478
479
480 if (!skb->arp && dev->rebuild_header(skb->data, dev)) {
481 skb->dev = dev;
482 arp_queue (skb);
483 return 0;
484 }
485 skb->arp=1;
486
487
488
489 if (set_bit(0, (void*)&dev->tbusy) != 0)
490 printk("%s: Transmitter access conflict.\n", dev->name);
491 else {
492 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
493 unsigned char *buf = skb->data;
494 int flags;
495
496
497
498 save_flags(flags);
499 cli();
500 write_reg(ioaddr, IMR, 0);
501 write_reg_high(ioaddr, IMR, 0);
502 restore_flags(flags);
503
504 write_packet(ioaddr, length, buf, dev->if_port);
505
506 lp->pac_cnt_in_tx_buf++;
507 if (lp->tx_unit_busy == 0) {
508 trigger_send(ioaddr, length);
509 lp->saved_tx_size = 0;
510 lp->re_tx = 0;
511 lp->tx_unit_busy = 1;
512 } else
513 lp->saved_tx_size = length;
514
515 dev->trans_start = jiffies;
516
517 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
518 write_reg_high(ioaddr, IMR, ISRh_RxErr);
519 }
520
521 if (skb->free)
522 kfree_skb (skb, FREE_WRITE);
523
524 return 0;
525 }
526
527
528
529 static void
530 net_interrupt(int reg_ptr)
531 {
532 int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
533 struct device *dev = (struct device *)(irq2dev_map[irq]);
534 struct net_local *lp;
535 int ioaddr, status, boguscount = 20;
536 static int num_tx_since_rx = 0;
537
538 if (dev == NULL) {
539 printk ("ATP_interrupt(): irq %d for unknown device.\n", irq);
540 return;
541 }
542 dev->interrupt = 1;
543
544 ioaddr = dev->base_addr;
545 lp = (struct net_local *)dev->priv;
546
547
548 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
549
550
551 write_reg(ioaddr, CMR2, CMR2_NULL);
552 write_reg(ioaddr, IMR, 0);
553
554 if (net_debug > 5) printk("%s: In interrupt ", dev->name);
555 while (--boguscount > 0) {
556 status = read_nibble(ioaddr, ISR);
557 if (net_debug > 5) printk("loop status %02x..", status);
558
559 if (status & (ISR_RxOK<<3)) {
560 write_reg(ioaddr, ISR, ISR_RxOK);
561 do {
562 int read_status = read_nibble(ioaddr, CMR1);
563 if (net_debug > 6)
564 printk("handling Rx packet %02x..", read_status);
565
566
567 if (read_status & (CMR1_IRQ << 3)) {
568 lp->stats.rx_over_errors++;
569
570 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
571 net_rx(dev);
572
573 write_reg_high(ioaddr, ISR, ISRh_RxErr);
574 write_reg_high(ioaddr, CMR2, lp->addr_mode);
575 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
576 net_rx(dev);
577 dev->last_rx = jiffies;
578 num_tx_since_rx = 0;
579 } else
580 break;
581 } while (--boguscount > 0);
582 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
583 if (net_debug > 6) printk("handling Tx done..");
584
585
586 write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
587 if (status & (ISR_TxErr<<3)) {
588 lp->stats.collisions++;
589 if (++lp->re_tx > 15) {
590 lp->stats.tx_aborted_errors++;
591 hardware_init(dev);
592 break;
593 }
594
595 if (net_debug > 6) printk("attempting to ReTx");
596 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
597 } else {
598
599 lp->stats.tx_packets++;
600 lp->pac_cnt_in_tx_buf--;
601 if ( lp->saved_tx_size) {
602 trigger_send(ioaddr, lp->saved_tx_size);
603 lp->saved_tx_size = 0;
604 lp->re_tx = 0;
605 } else
606 lp->tx_unit_busy = 0;
607 dev->tbusy = 0;
608 mark_bh(INET_BH);
609 }
610 num_tx_since_rx++;
611 } else if (num_tx_since_rx > 8
612 && jiffies > dev->last_rx + 100) {
613 if (net_debug > 2)
614 printk("%s: Missed packet? No Rx after %d Tx and %ld jiffies"
615 " status %02x CMR1 %02x.\n", dev->name,
616 num_tx_since_rx, jiffies - dev->last_rx, status,
617 (read_nibble(ioaddr, CMR1) >> 3) & 15);
618 lp->stats.rx_missed_errors++;
619 hardware_init(dev);
620 num_tx_since_rx = 0;
621 break;
622 } else
623 break;
624 }
625
626
627
628 {
629 int i;
630 for (i = 0; i < 6; i++)
631 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
632 }
633
634
635 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
636
637 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
638
639 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
640 write_reg_high(ioaddr, IMR, ISRh_RxErr);
641
642 if (net_debug > 5) printk("exiting interrupt.\n");
643
644 dev->interrupt = 0;
645
646 return;
647 }
648
649
650 static void net_rx(struct device *dev)
651 {
652 struct net_local *lp = (struct net_local *)dev->priv;
653 int ioaddr = dev->base_addr;
654 #ifdef notdef
655 ushort header[4];
656 #else
657 struct rx_header rx_head;
658 #endif
659
660
661 outb(EOC+MAR, ioaddr + PAR_DATA);
662 read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
663 if (net_debug > 5)
664 printk(" rx_count %04x %04x %04x %04x..", rx_head.pad,
665 rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
666 if ((rx_head.rx_status & 0x77) != 0x01) {
667 lp->stats.rx_errors++;
668
669
670 if (net_debug > 3) printk("%s: Unknown ATP Rx error %04x.\n",
671 dev->name, rx_head.rx_status);
672 hardware_init(dev);
673 return;
674 } else {
675
676 int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
677 int sksize = sizeof(struct sk_buff) + pkt_len;
678 struct sk_buff *skb;
679
680 skb = alloc_skb(sksize, GFP_ATOMIC);
681 if (skb == NULL) {
682 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
683 lp->stats.rx_dropped++;
684 goto done;
685 }
686 skb->mem_len = sksize;
687 skb->mem_addr = skb;
688 skb->len = pkt_len;
689 skb->dev = dev;
690
691 read_block(ioaddr, pkt_len, skb->data, dev->if_port);
692
693 if (net_debug > 6) {
694 unsigned char *data = skb->data;
695 printk(" data %02x%02x%02x %02x%02x%02x %02x%02x%02x"
696 "%02x%02x%02x %02x%02x..",
697 data[0], data[1], data[2], data[3], data[4], data[5],
698 data[6], data[7], data[8], data[9], data[10], data[11],
699 data[12], data[13]);
700 }
701
702 #ifdef HAVE_NETIF_RX
703 netif_rx(skb);
704 #else
705 skb->lock = 0;
706 if (dev_rint((unsigned char*)skb, pkt_len, IN_SKBUFF, dev) != 0) {
707 kfree_s(skb, sksize);
708 lp->stats.rx_dropped++;
709 break;
710 }
711 #endif
712 lp->stats.rx_packets++;
713 }
714 done:
715 write_reg(ioaddr, CMR1, CMR1_NextPkt);
716 return;
717 }
718
719 static void read_block(short ioaddr, int length, unsigned char *p, int data_mode)
720 {
721
722 if (data_mode <= 3) {
723 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
724 outb(length == 8 ? RdAddr | HNib | MAR : RdAddr | MAR,
725 ioaddr + PAR_DATA);
726 if (data_mode <= 1) {
727 do *p++ = read_byte_mode0(ioaddr); while (--length > 0);
728 } else
729 do *p++ = read_byte_mode2(ioaddr); while (--length > 0);
730 } else if (data_mode <= 5)
731 do *p++ = read_byte_mode4(ioaddr); while (--length > 0);
732 else
733 do *p++ = read_byte_mode6(ioaddr); while (--length > 0);
734
735 outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
736 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
737 }
738
739
740 static int
741 net_close(struct device *dev)
742 {
743 struct net_local *lp = (struct net_local *)dev->priv;
744 int ioaddr = dev->base_addr;
745
746 dev->tbusy = 1;
747 dev->start = 0;
748
749
750 lp->addr_mode = CMR2h_OFF;
751 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
752
753
754 outb(0x00, ioaddr + PAR_CONTROL);
755 free_irq(dev->irq);
756 irq2dev_map[dev->irq] = 0;
757
758
759 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
760
761 return 0;
762 }
763
764
765
766 static struct enet_statistics *
767 net_get_stats(struct device *dev)
768 {
769 struct net_local *lp = (struct net_local *)dev->priv;
770 return &lp->stats;
771 }
772
773 #ifdef HAVE_MULTICAST
774
775
776
777
778
779
780 static void
781 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
782 {
783 struct net_local *lp = (struct net_local *)dev->priv;
784 short ioaddr = dev->base_addr;
785 lp->addr_mode = num_addrs ? CMR2h_PROMISC : CMR2h_Normal;
786 write_reg_high(ioaddr, CMR2, lp->addr_mode);
787 }
788 #endif
789
790
791
792
793
794
795
796
797