This source file includes following definitions.
- __get_order
- dma_mem_alloc
- inb_status
- inb_control
- inb_command
- outb_control
- outb_command
- inw_data
- outw_data
- backlog_next
- get_status
- set_hsf
- adapter_reset
- check_dma
- send_pcb_slow
- send_pcb_fast
- prime_rx
- send_pcb
- receive_pcb
- start_receive
- receive_packet
- elp_interrupt
- elp_open
- send_packet
- elp_start_xmit
- elp_get_stats
- elp_close
- elp_set_mc_list
- elp_init
- elp_sense
- elp_autodetect
- elplus_probe
- init_module
- cleanup_module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
78
79
80
81
82
83
84
85
86 #include <linux/module.h>
87
88 #include <linux/kernel.h>
89 #include <linux/sched.h>
90 #include <linux/string.h>
91 #include <linux/interrupt.h>
92 #include <linux/ptrace.h>
93 #include <linux/errno.h>
94 #include <linux/in.h>
95 #include <linux/malloc.h>
96 #include <linux/ioport.h>
97 #include <asm/bitops.h>
98 #include <asm/io.h>
99 #include <asm/dma.h>
100
101 #include <linux/netdevice.h>
102 #include <linux/etherdevice.h>
103 #include <linux/skbuff.h>
104
105 #include "3c505.h"
106
107 #define ELP_DMA 6
108 #define ELP_RX_PCBS 4
109
110
111
112
113
114
115
116 static const char *filename = __FILE__;
117
118 static const char *timeout_msg = "*** timeout at %s:%s (line %d) ***\n";
119 #define TIMEOUT_MSG(lineno) \
120 printk(timeout_msg, filename,__FUNCTION__,(lineno))
121
122 static const char *invalid_pcb_msg =
123 "*** invalid pcb length %d at %s:%s (line %d) ***\n";
124 #define INVALID_PCB_MSG(len) \
125 printk(invalid_pcb_msg, (len),filename,__FUNCTION__,__LINE__)
126
127 static const char *search_msg = "%s: Looking for 3c505 adapter at address %#x...";
128
129 static const char *stilllooking_msg = "still looking...";
130
131 static const char *found_msg = "found.\n";
132
133 static const char *notfound_msg = "not found (reason = %d)\n";
134
135 static const char *couldnot_msg = "%s: 3c505 not found\n";
136
137
138
139
140
141
142
143 #ifdef ELP_DEBUG
144 static const int elp_debug = ELP_DEBUG;
145 #else
146 static const int elp_debug = 0;
147 #endif
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162 #ifndef TRUE
163 #define TRUE 1
164 #endif
165
166 #ifndef FALSE
167 #define FALSE 0
168 #endif
169
170
171
172
173
174
175
176
177 const int addr_list[] =
178 {0x300, 0x280, 0x310, 0};
179
180
181
182
183 static inline int __get_order(unsigned long size)
184 {
185 int order;
186
187 size = (size - 1) >> (PAGE_SHIFT - 1);
188 order = -1;
189 do {
190 size >>= 1;
191 order++;
192 } while (size);
193 return order;
194 }
195
196 static unsigned long dma_mem_alloc(int size)
197 {
198 int order = __get_order(size);
199
200 return __get_dma_pages(GFP_KERNEL, order);
201 }
202
203
204
205
206
207
208
209
210 static inline unsigned char inb_status(unsigned int base_addr)
211 {
212 return inb(base_addr + PORT_STATUS);
213 }
214
215 static inline unsigned char inb_control(unsigned int base_addr)
216 {
217 return inb(base_addr + PORT_CONTROL);
218 }
219
220 static inline int inb_command(unsigned int base_addr)
221 {
222 return inb(base_addr + PORT_COMMAND);
223 }
224
225 static inline void outb_control(unsigned char val, unsigned int base_addr)
226 {
227 outb(val, base_addr + PORT_CONTROL);
228 }
229
230 static inline void outb_command(unsigned char val, unsigned int base_addr)
231 {
232 outb(val, base_addr + PORT_COMMAND);
233 }
234
235 static inline unsigned int inw_data(unsigned int base_addr)
236 {
237 return inw(base_addr + PORT_DATA);
238 }
239
240 static inline void outw_data(unsigned int val, unsigned int base_addr)
241 {
242 outw(val, base_addr + PORT_DATA);
243 }
244
245
246
247
248
249
250
251
252
253
254
255
256 #define DMA_BUFFER_SIZE 1600
257 #define BACKLOG_SIZE 4
258
259 typedef struct {
260 volatile short got[NUM_TRANSMIT_CMDS];
261 pcb_struct tx_pcb;
262 pcb_struct rx_pcb;
263 pcb_struct itx_pcb;
264 pcb_struct irx_pcb;
265 struct enet_statistics stats;
266
267 void *dma_buffer;
268
269 struct {
270 unsigned int length[BACKLOG_SIZE];
271 unsigned int in;
272 unsigned int out;
273 } rx_backlog;
274
275 struct {
276 unsigned int direction;
277 unsigned int length;
278 unsigned int copy_flag;
279 struct sk_buff *skb;
280 long int start_time;
281 } current_dma;
282
283
284 unsigned long send_pcb_semaphore;
285 unsigned int dmaing;
286 unsigned long busy;
287
288 unsigned int rx_active;
289 } elp_device;
290
291 static inline unsigned int backlog_next(unsigned int n)
292 {
293 return (n + 1) % BACKLOG_SIZE;
294 }
295
296
297
298
299
300
301
302
303
304
305
306
307
308 #define GET_ASF(addr) \
309 (get_status(addr)&ASF_PCB_MASK)
310
311 static inline int get_status(unsigned int base_addr)
312 {
313 int timeout = jiffies + 10;
314 register int stat1;
315 do {
316 stat1 = inb_status(base_addr);
317 } while (stat1 != inb_status(base_addr) && jiffies < timeout);
318 if (jiffies >= timeout)
319 TIMEOUT_MSG(__LINE__);
320 return stat1;
321 }
322
323 static inline void set_hsf(unsigned int base_addr, int hsf)
324 {
325 cli();
326 outb_control((inb_control(base_addr) & ~HSF_PCB_MASK) | hsf, base_addr);
327 sti();
328 }
329
330 static int start_receive(struct device *, pcb_struct *);
331
332 inline static void adapter_reset(struct device *dev)
333 {
334 int timeout;
335 unsigned char orig_hcr = inb_control(dev->base_addr);
336
337 elp_device *adapter = dev->priv;
338
339 outb_control(0, dev->base_addr);
340
341 if (inb_status(dev->base_addr) & ACRF) {
342 do {
343 inb_command(dev->base_addr);
344 timeout = jiffies + 2;
345 while ((jiffies <= timeout) && !(inb_status(dev->base_addr) & ACRF));
346 } while (inb_status(dev->base_addr) & ACRF);
347 set_hsf(dev->base_addr, HSF_PCB_NAK);
348 }
349 outb_control(inb_control(dev->base_addr) | ATTN | DIR, dev->base_addr);
350 timeout = jiffies + 1;
351 while (jiffies <= timeout);
352 outb_control(inb_control(dev->base_addr) & ~ATTN, dev->base_addr);
353 timeout = jiffies + 1;
354 while (jiffies <= timeout);
355 outb_control(inb_control(dev->base_addr) | FLSH, dev->base_addr);
356 timeout = jiffies + 1;
357 while (jiffies <= timeout);
358 outb_control(inb_control(dev->base_addr) & ~FLSH, dev->base_addr);
359 timeout = jiffies + 1;
360 while (jiffies <= timeout);
361
362 outb_control(orig_hcr, dev->base_addr);
363 if (!start_receive(dev, &adapter->tx_pcb))
364 printk("%s: start receive command failed \n", dev->name);
365 }
366
367 static inline void check_dma(struct device *dev)
368 {
369 elp_device *adapter = dev->priv;
370 if (adapter->dmaing && (jiffies > (adapter->current_dma.start_time + 10))) {
371 unsigned long flags;
372 printk("%s: DMA %s timed out, %d bytes left\n", dev->name, adapter->current_dma.direction ? "download" : "upload", get_dma_residue(dev->dma));
373 save_flags(flags);
374 cli();
375 adapter->dmaing = 0;
376 adapter->busy = 0;
377 disable_dma(dev->dma);
378 if (adapter->rx_active)
379 adapter->rx_active--;
380 outb_control(inb_control(dev->base_addr) & ~(DMAE | TCEN | DIR), dev->base_addr);
381 restore_flags(flags);
382 }
383 }
384
385
386 static inline unsigned int send_pcb_slow(unsigned int base_addr, unsigned char byte)
387 {
388 unsigned int timeout;
389 outb_command(byte, base_addr);
390 for (timeout = jiffies + 5; jiffies < timeout;) {
391 if (inb_status(base_addr) & HCRE)
392 return FALSE;
393 }
394 printk("3c505: send_pcb_slow timed out\n");
395 return TRUE;
396 }
397
398 static inline unsigned int send_pcb_fast(unsigned int base_addr, unsigned char byte)
399 {
400 unsigned int timeout;
401 outb_command(byte, base_addr);
402 for (timeout = 0; timeout < 40000; timeout++) {
403 if (inb_status(base_addr) & HCRE)
404 return FALSE;
405 }
406 printk("3c505: send_pcb_fast timed out\n");
407 return TRUE;
408 }
409
410 static int
411 start_receive(struct device *dev, pcb_struct * tx_pcb);
412
413
414 static inline void prime_rx(struct device *dev)
415 {
416 elp_device *adapter = dev->priv;
417 while (adapter->rx_active < ELP_RX_PCBS && dev->start) {
418 if (!start_receive(dev, &adapter->itx_pcb))
419 break;
420 }
421 }
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447 static int send_pcb(struct device *dev, pcb_struct * pcb)
448 {
449 int i;
450 int timeout;
451 elp_device *adapter = dev->priv;
452
453 check_dma(dev);
454
455 if (adapter->dmaing && adapter->current_dma.direction == 0)
456 return FALSE;
457
458
459 if (set_bit(1, &adapter->send_pcb_semaphore)) {
460 if (elp_debug >= 3) {
461 printk("%s: send_pcb entered while threaded\n", dev->name);
462 }
463 return FALSE;
464 }
465
466
467
468
469
470 set_hsf(dev->base_addr, 0);
471
472 if (send_pcb_slow(dev->base_addr, pcb->command))
473 goto abort;
474
475 cli();
476
477 if (send_pcb_fast(dev->base_addr, pcb->length))
478 goto sti_abort;
479
480 for (i = 0; i < pcb->length; i++) {
481 if (send_pcb_fast(dev->base_addr, pcb->data.raw[i]))
482 goto sti_abort;
483 }
484
485 outb_control(inb_control(dev->base_addr) | 3, dev->base_addr);
486 outb_command(2 + pcb->length, dev->base_addr);
487
488
489 sti();
490
491 for (timeout = jiffies + 5; jiffies < timeout;) {
492 switch (GET_ASF(dev->base_addr)) {
493 case ASF_PCB_ACK:
494 adapter->send_pcb_semaphore = 0;
495 return TRUE;
496 break;
497 case ASF_PCB_NAK:
498 cli();
499 printk("%s: send_pcb got NAK\n", dev->name);
500 goto abort;
501 break;
502 }
503 }
504
505 if (elp_debug >= 1)
506 printk("%s: timeout waiting for PCB acknowledge (status %02x)\n", dev->name, inb_status(dev->base_addr));
507
508 sti_abort:
509 sti();
510 abort:
511 adapter->send_pcb_semaphore = 0;
512 return FALSE;
513 }
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529 static int receive_pcb(struct device *dev, pcb_struct * pcb)
530 {
531 int i, j;
532 int total_length;
533 int stat;
534 int timeout;
535
536 elp_device *adapter = dev->priv;
537
538 set_hsf(dev->base_addr, 0);
539
540
541 timeout = jiffies + 2;
542 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && jiffies < timeout);
543 if (jiffies >= timeout) {
544 TIMEOUT_MSG(__LINE__);
545 return FALSE;
546 }
547 pcb->command = inb_command(dev->base_addr);
548
549
550 timeout = jiffies + 3;
551 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && jiffies < timeout);
552 if (jiffies >= timeout) {
553 TIMEOUT_MSG(__LINE__);
554 printk("%s: status %02x\n", dev->name, stat);
555 return FALSE;
556 }
557 pcb->length = inb_command(dev->base_addr);
558
559 if (pcb->length > MAX_PCB_DATA) {
560 INVALID_PCB_MSG(pcb->length);
561 adapter_reset(dev);
562 return FALSE;
563 }
564
565 cli();
566 i = 0;
567 do {
568 j = 0;
569 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
570 pcb->data.raw[i++] = inb_command(dev->base_addr);
571 if (i > MAX_PCB_DATA)
572 INVALID_PCB_MSG(i);
573 } while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
574 sti();
575 if (j >= 20000) {
576 TIMEOUT_MSG(__LINE__);
577 return FALSE;
578 }
579
580 total_length = pcb->data.raw[--i];
581
582
583 if (total_length != (pcb->length + 2)) {
584 if (elp_debug >= 2)
585 printk("%s: mangled PCB received\n", dev->name);
586 set_hsf(dev->base_addr, HSF_PCB_NAK);
587 return FALSE;
588 }
589
590 if (pcb->command == CMD_RECEIVE_PACKET_COMPLETE) {
591 if (set_bit(0, (void *) &adapter->busy)) {
592 if (backlog_next(adapter->rx_backlog.in) == adapter->rx_backlog.out) {
593 set_hsf(dev->base_addr, HSF_PCB_NAK);
594 printk("%s: PCB rejected, transfer in progress and backlog full\n", dev->name);
595 pcb->command = 0;
596 return TRUE;
597 } else {
598 pcb->command = 0xff;
599 }
600 }
601 }
602 set_hsf(dev->base_addr, HSF_PCB_ACK);
603 return TRUE;
604 }
605
606
607
608
609
610
611
612
613 static int start_receive(struct device *dev, pcb_struct * tx_pcb)
614 {
615 int status;
616 elp_device *adapter = dev->priv;
617
618 if (elp_debug >= 3)
619 printk("%s: restarting receiver\n", dev->name);
620 tx_pcb->command = CMD_RECEIVE_PACKET;
621 tx_pcb->length = sizeof(struct Rcv_pkt);
622 tx_pcb->data.rcv_pkt.buf_seg
623 = tx_pcb->data.rcv_pkt.buf_ofs = 0;
624 tx_pcb->data.rcv_pkt.buf_len = 1600;
625 tx_pcb->data.rcv_pkt.timeout = 0;
626 status = send_pcb(dev, tx_pcb);
627 if (status)
628 adapter->rx_active++;
629 return status;
630 }
631
632
633
634
635
636
637
638
639
640
641 static void receive_packet(struct device *dev, int len)
642 {
643 int rlen;
644 elp_device *adapter = dev->priv;
645 unsigned long target;
646 struct sk_buff *skb;
647
648 rlen = (len + 1) & ~1;
649 skb = dev_alloc_skb(rlen + 2);
650
651 adapter->current_dma.copy_flag = 0;
652
653 if (!skb) {
654 printk("%s: memory squeeze, dropping packet\n", dev->name);
655 target = virt_to_bus(adapter->dma_buffer);
656 } else {
657 skb_reserve(skb, 2);
658 target = virt_to_bus(skb_put(skb, rlen));
659 if ((target + rlen) >= MAX_DMA_ADDRESS) {
660 target = virt_to_bus(adapter->dma_buffer);
661 adapter->current_dma.copy_flag = 1;
662 }
663 }
664
665 if (set_bit(0, (void *) &adapter->dmaing))
666 printk("%s: rx blocked, DMA in progress, dir %d\n", dev->name, adapter->current_dma.direction);
667
668 adapter->current_dma.direction = 0;
669 adapter->current_dma.length = rlen;
670 adapter->current_dma.skb = skb;
671 adapter->current_dma.start_time = jiffies;
672
673 outb_control(inb_control(dev->base_addr) | DIR | TCEN | DMAE, dev->base_addr);
674
675 disable_dma(dev->dma);
676 clear_dma_ff(dev->dma);
677 set_dma_mode(dev->dma, 0x04);
678 set_dma_addr(dev->dma, target);
679 set_dma_count(dev->dma, rlen);
680 enable_dma(dev->dma);
681
682 if (elp_debug >= 3) {
683 printk("%s: rx DMA transfer started\n", dev->name);
684 }
685 if (adapter->rx_active)
686 adapter->rx_active--;
687
688 if (!adapter->busy)
689 printk("%s: receive_packet called, busy not set.\n", dev->name);
690 }
691
692
693
694
695
696
697
698 static void elp_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr)
699 {
700 int len;
701 int dlen;
702 int icount = 0;
703 struct device *dev;
704 elp_device *adapter;
705 int timeout;
706
707 if (irq < 0 || irq > 15) {
708 printk("elp_interrupt(): illegal IRQ number found in interrupt routine (%i)\n", irq);
709 return;
710 }
711 dev = irq2dev_map[irq];
712
713 if (dev == NULL) {
714 printk("elp_interrupt(): irq %d for unknown device.\n", irq);
715 return;
716 }
717 adapter = (elp_device *) dev->priv;
718
719 if (dev->interrupt) {
720 printk("%s: re-entering the interrupt handler!\n", dev->name);
721 return;
722 }
723 dev->interrupt = 1;
724
725 do {
726
727
728
729 if (inb_status(dev->base_addr) & DONE) {
730 if (!adapter->dmaing) {
731 printk("%s: phantom DMA completed\n", dev->name);
732 }
733 if (elp_debug >= 3) {
734 printk("%s: %s DMA complete, status %02x\n", dev->name, adapter->current_dma.direction ? "tx" : "rx", inb_status(dev->base_addr));
735 }
736
737 outb_control(inb_control(dev->base_addr) & ~(DMAE | TCEN | DIR), dev->base_addr);
738 if (adapter->current_dma.direction) {
739 dev_kfree_skb(adapter->current_dma.skb, FREE_WRITE);
740 } else {
741 struct sk_buff *skb = adapter->current_dma.skb;
742 if (skb) {
743 skb->dev = dev;
744 if (adapter->current_dma.copy_flag) {
745 memcpy(skb_put(skb, adapter->current_dma.length), adapter->dma_buffer, adapter->current_dma.length);
746 }
747 skb->protocol = eth_type_trans(skb,dev);
748 netif_rx(skb);
749 }
750 }
751 adapter->dmaing = 0;
752 if (adapter->rx_backlog.in != adapter->rx_backlog.out) {
753 int t = adapter->rx_backlog.length[adapter->rx_backlog.out];
754 adapter->rx_backlog.out = backlog_next(adapter->rx_backlog.out);
755 if (elp_debug >= 2)
756 printk("%s: receiving backlogged packet (%d)\n", dev->name, t);
757 receive_packet(dev, t);
758 } else {
759 adapter->busy = 0;
760 }
761 } else {
762
763 check_dma(dev);
764 }
765
766 sti();
767
768
769
770
771 timeout = jiffies + 3;
772 while ((inb_status(dev->base_addr) & ACRF) != 0 && jiffies < timeout) {
773 if (receive_pcb(dev, &adapter->irx_pcb)) {
774 switch (adapter->irx_pcb.command) {
775 case 0:
776 break;
777
778
779
780 case 0xff:
781 case CMD_RECEIVE_PACKET_COMPLETE:
782
783 if (dev->start == 0)
784 break;
785 cli();
786 len = adapter->irx_pcb.data.rcv_resp.pkt_len;
787 dlen = adapter->irx_pcb.data.rcv_resp.buf_len;
788 if (adapter->irx_pcb.data.rcv_resp.timeout != 0) {
789 printk("%s: interrupt - packet not received correctly\n", dev->name);
790 sti();
791 } else {
792 if (elp_debug >= 3) {
793 sti();
794 printk("%s: interrupt - packet received of length %i (%i)\n", dev->name, len, dlen);
795 cli();
796 }
797 if (adapter->irx_pcb.command == 0xff) {
798 if (elp_debug >= 2)
799 printk("%s: adding packet to backlog (len = %d)\n", dev->name, dlen);
800 adapter->rx_backlog.length[adapter->rx_backlog.in] = dlen;
801 adapter->rx_backlog.in = backlog_next(adapter->rx_backlog.in);
802 } else {
803 receive_packet(dev, dlen);
804 }
805 sti();
806 if (elp_debug >= 3)
807 printk("%s: packet received\n", dev->name);
808 }
809 break;
810
811
812
813
814 case CMD_CONFIGURE_82586_RESPONSE:
815 adapter->got[CMD_CONFIGURE_82586] = 1;
816 if (elp_debug >= 3)
817 printk("%s: interrupt - configure response received\n", dev->name);
818 break;
819
820
821
822
823 case CMD_CONFIGURE_ADAPTER_RESPONSE:
824 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 1;
825 if (elp_debug >= 3)
826 printk("%s: Adapter memory configuration %s.\n", dev->name,
827 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
828 break;
829
830
831
832
833 case CMD_LOAD_MULTICAST_RESPONSE:
834 adapter->got[CMD_LOAD_MULTICAST_LIST] = 1;
835 if (elp_debug >= 3)
836 printk("%s: Multicast address list loading %s.\n", dev->name,
837 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
838 break;
839
840
841
842
843 case CMD_SET_ADDRESS_RESPONSE:
844 adapter->got[CMD_SET_STATION_ADDRESS] = 1;
845 if (elp_debug >= 3)
846 printk("%s: Ethernet address setting %s.\n", dev->name,
847 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
848 break;
849
850
851
852
853
854 case CMD_NETWORK_STATISTICS_RESPONSE:
855 adapter->stats.rx_packets += adapter->irx_pcb.data.netstat.tot_recv;
856 adapter->stats.tx_packets += adapter->irx_pcb.data.netstat.tot_xmit;
857 adapter->stats.rx_crc_errors += adapter->irx_pcb.data.netstat.err_CRC;
858 adapter->stats.rx_frame_errors += adapter->irx_pcb.data.netstat.err_align;
859 adapter->stats.rx_fifo_errors += adapter->irx_pcb.data.netstat.err_ovrrun;
860 adapter->stats.rx_over_errors += adapter->irx_pcb.data.netstat.err_res;
861 adapter->got[CMD_NETWORK_STATISTICS] = 1;
862 if (elp_debug >= 3)
863 printk("%s: interrupt - statistics response received\n", dev->name);
864 break;
865
866
867
868
869 case CMD_TRANSMIT_PACKET_COMPLETE:
870 if (elp_debug >= 3)
871 printk("%s: interrupt - packet sent\n", dev->name);
872 if (dev->start == 0)
873 break;
874 if (adapter->irx_pcb.data.xmit_resp.c_stat != 0) {
875 if (elp_debug >= 0)
876 printk("%s: interrupt - error sending packet %4.4x\n",
877 dev->name, adapter->irx_pcb.data.xmit_resp.c_stat);
878 switch (adapter->irx_pcb.data.xmit_resp.c_stat) {
879 case 0xffff:
880 adapter->stats.tx_aborted_errors++;
881 break;
882 case 0xfffe:
883 adapter->stats.tx_fifo_errors++;
884 break;
885 }
886 }
887 dev->tbusy = 0;
888 mark_bh(NET_BH);
889 break;
890
891
892
893
894 default:
895 printk("%s: unknown PCB received - %2.2x\n", dev->name, adapter->irx_pcb.command);
896 break;
897 }
898 } else {
899 printk("%s: failed to read PCB on interrupt\n", dev->name);
900 adapter_reset(dev);
901 }
902 }
903
904 } while (icount++ < 5 && (inb_status(dev->base_addr) & (ACRF | DONE)));
905
906 prime_rx(dev);
907
908
909
910
911 dev->interrupt = 0;
912 }
913
914
915
916
917
918
919
920
921 static int elp_open(struct device *dev)
922 {
923 elp_device *adapter;
924
925 adapter = dev->priv;
926
927 if (elp_debug >= 3)
928 printk("%s: request to open device\n", dev->name);
929
930
931
932
933 if (adapter == NULL) {
934 printk("%s: Opening a non-existent physical device\n", dev->name);
935 return -EAGAIN;
936 }
937
938
939
940 outb_control(0x00, dev->base_addr);
941
942
943
944
945 inb_command(dev->base_addr);
946 adapter_reset(dev);
947
948
949
950
951 dev->interrupt = 0;
952
953
954
955
956 dev->tbusy = 0;
957
958
959
960
961 adapter->rx_active = 0;
962
963 adapter->busy = 0;
964 adapter->send_pcb_semaphore = 0;
965 adapter->rx_backlog.in = 0;
966 adapter->rx_backlog.out = 0;
967
968
969
970
971 irq2dev_map[dev->irq] = dev;
972
973
974
975
976 if (request_irq(dev->irq, &elp_interrupt, 0, "3c505", NULL)) {
977 irq2dev_map[dev->irq] = NULL;
978 return -EAGAIN;
979 }
980 if (request_dma(dev->dma, "3c505")) {
981 printk("%s: could not allocate DMA channel\n", dev->name);
982 return -EAGAIN;
983 }
984 adapter->dma_buffer = (void *) dma_mem_alloc(DMA_BUFFER_SIZE);
985 if (!adapter->dma_buffer) {
986 printk("Could not allocate DMA buffer\n");
987 }
988 adapter->dmaing = 0;
989
990
991
992
993 outb_control(CMDE, dev->base_addr);
994
995
996
997
998 dev->start = 1;
999
1000
1001
1002
1003 if (elp_debug >= 3)
1004 printk("%s: sending 3c505 memory configuration command\n", dev->name);
1005 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
1006 adapter->tx_pcb.data.memconf.cmd_q = 10;
1007 adapter->tx_pcb.data.memconf.rcv_q = 20;
1008 adapter->tx_pcb.data.memconf.mcast = 10;
1009 adapter->tx_pcb.data.memconf.frame = 20;
1010 adapter->tx_pcb.data.memconf.rcv_b = 20;
1011 adapter->tx_pcb.data.memconf.progs = 0;
1012 adapter->tx_pcb.length = sizeof(struct Memconf);
1013 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 0;
1014 if (!send_pcb(dev, &adapter->tx_pcb))
1015 printk("%s: couldn't send memory configuration command\n", dev->name);
1016 else {
1017 int timeout = jiffies + TIMEOUT;
1018 while (adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] == 0 && jiffies < timeout);
1019 if (jiffies >= timeout)
1020 TIMEOUT_MSG(__LINE__);
1021 }
1022
1023
1024
1025
1026
1027 if (elp_debug >= 3)
1028 printk("%s: sending 82586 configure command\n", dev->name);
1029 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1030 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1031 adapter->tx_pcb.length = 2;
1032 adapter->got[CMD_CONFIGURE_82586] = 0;
1033 if (!send_pcb(dev, &adapter->tx_pcb))
1034 printk("%s: couldn't send 82586 configure command\n", dev->name);
1035 else {
1036 int timeout = jiffies + TIMEOUT;
1037 while (adapter->got[CMD_CONFIGURE_82586] == 0 && jiffies < timeout);
1038 if (jiffies >= timeout)
1039 TIMEOUT_MSG(__LINE__);
1040 }
1041
1042
1043 outb(0x1, dev->base_addr + PORT_AUXDMA);
1044
1045
1046
1047
1048 prime_rx(dev);
1049 if (elp_debug >= 3)
1050 printk("%s: %d receive PCBs active\n", dev->name, adapter->rx_active);
1051
1052 MOD_INC_USE_COUNT;
1053
1054 return 0;
1055 }
1056
1057
1058
1059
1060
1061
1062
1063
1064 static int send_packet(struct device *dev, struct sk_buff *skb)
1065 {
1066 elp_device *adapter = dev->priv;
1067 unsigned long target;
1068
1069
1070
1071
1072 unsigned int nlen = (((skb->len < 60) ? 60 : skb->len) + 1) & (~1);
1073
1074 if (set_bit(0, (void *) &adapter->busy)) {
1075 if (elp_debug >= 2)
1076 printk("%s: transmit blocked\n", dev->name);
1077 return FALSE;
1078 }
1079 adapter = dev->priv;
1080
1081
1082
1083
1084
1085 adapter->tx_pcb.command = CMD_TRANSMIT_PACKET;
1086 adapter->tx_pcb.length = sizeof(struct Xmit_pkt);
1087 adapter->tx_pcb.data.xmit_pkt.buf_ofs
1088 = adapter->tx_pcb.data.xmit_pkt.buf_seg = 0;
1089 adapter->tx_pcb.data.xmit_pkt.pkt_len = nlen;
1090
1091 if (!send_pcb(dev, &adapter->tx_pcb)) {
1092 adapter->busy = 0;
1093 return FALSE;
1094 }
1095
1096 if (set_bit(0, (void *) &adapter->dmaing))
1097 printk("%s: tx: DMA %d in progress\n", dev->name, adapter->current_dma.direction);
1098
1099 adapter->current_dma.direction = 1;
1100 adapter->current_dma.start_time = jiffies;
1101
1102 target = virt_to_bus(skb->data);
1103 if ((target + nlen) >= MAX_DMA_ADDRESS) {
1104 memcpy(adapter->dma_buffer, skb->data, nlen);
1105 target = virt_to_bus(adapter->dma_buffer);
1106 }
1107 adapter->current_dma.skb = skb;
1108 cli();
1109 disable_dma(dev->dma);
1110 clear_dma_ff(dev->dma);
1111 set_dma_mode(dev->dma, 0x08);
1112 set_dma_addr(dev->dma, target);
1113 set_dma_count(dev->dma, nlen);
1114 enable_dma(dev->dma);
1115 outb_control(inb_control(dev->base_addr) | DMAE | TCEN, dev->base_addr);
1116 if (elp_debug >= 3)
1117 printk("%s: DMA transfer started\n", dev->name);
1118
1119 return TRUE;
1120 }
1121
1122
1123
1124
1125
1126
1127
1128
1129 static int elp_start_xmit(struct sk_buff *skb, struct device *dev)
1130 {
1131 if (dev->interrupt) {
1132 printk("%s: start_xmit aborted (in irq)\n", dev->name);
1133 return 1;
1134 }
1135
1136 check_dma(dev);
1137
1138
1139
1140
1141 if (dev->tbusy) {
1142 elp_device *adapter = dev->priv;
1143 int tickssofar = jiffies - dev->trans_start;
1144 int stat;
1145
1146 if (tickssofar < 1000)
1147 return 1;
1148
1149 stat = inb_status(dev->base_addr);
1150 printk("%s: transmit timed out, %s?\n", dev->name, (stat & ACRF) ? "IRQ conflict" : "network cable problem");
1151 if (elp_debug >= 1)
1152 printk("%s: status %#02x\n", dev->name, stat);
1153 dev->trans_start = jiffies;
1154 dev->tbusy = 0;
1155 adapter->stats.tx_dropped++;
1156 }
1157
1158
1159 if (skb == NULL) {
1160 dev_tint(dev);
1161 return 0;
1162 }
1163
1164 if (skb->len <= 0)
1165 return 0;
1166
1167 if (elp_debug >= 3)
1168 printk("%s: request to send packet of length %d\n", dev->name, (int) skb->len);
1169
1170 if (set_bit(0, (void *) &dev->tbusy)) {
1171 printk("%s: transmitter access conflict\n", dev->name);
1172 return 1;
1173 }
1174
1175
1176
1177 if (!send_packet(dev, skb)) {
1178 if (elp_debug >= 2) {
1179 printk("%s: failed to transmit packet\n", dev->name);
1180 }
1181 dev->tbusy = 0;
1182 return 1;
1183 }
1184 if (elp_debug >= 3)
1185 printk("%s: packet of length %d sent\n", dev->name, (int) skb->len);
1186
1187
1188
1189
1190 dev->trans_start = jiffies;
1191
1192 prime_rx(dev);
1193
1194 return 0;
1195 }
1196
1197
1198
1199
1200
1201
1202
1203 static struct enet_statistics *
1204 elp_get_stats(struct device *dev)
1205 {
1206 elp_device *adapter = (elp_device *) dev->priv;
1207
1208 if (elp_debug >= 3)
1209 printk("%s: request for stats\n", dev->name);
1210
1211
1212
1213 if (!dev->start)
1214 return &adapter->stats;
1215
1216
1217 adapter->tx_pcb.command = CMD_NETWORK_STATISTICS;
1218 adapter->tx_pcb.length = 0;
1219 adapter->got[CMD_NETWORK_STATISTICS] = 0;
1220 if (!send_pcb(dev, &adapter->tx_pcb))
1221 printk("%s: couldn't send get statistics command\n", dev->name);
1222 else {
1223 int timeout = jiffies + TIMEOUT;
1224 while (adapter->got[CMD_NETWORK_STATISTICS] == 0 && jiffies < timeout);
1225 if (jiffies >= timeout) {
1226 TIMEOUT_MSG(__LINE__);
1227 return &adapter->stats;
1228 }
1229 }
1230
1231
1232 return &adapter->stats;
1233 }
1234
1235
1236
1237
1238
1239
1240
1241 static int elp_close(struct device *dev)
1242 {
1243 elp_device *adapter;
1244
1245 adapter = dev->priv;
1246
1247 if (elp_debug >= 3)
1248 printk("%s: request to close device\n", dev->name);
1249
1250
1251
1252
1253
1254 (void) elp_get_stats(dev);
1255
1256
1257
1258
1259 outb_control(0x00, dev->base_addr);
1260
1261
1262
1263
1264 dev->tbusy = 1;
1265
1266
1267
1268
1269 dev->start = 0;
1270
1271
1272
1273
1274 free_irq(dev->irq, NULL);
1275
1276
1277
1278
1279 irq2dev_map[dev->irq] = 0;
1280
1281 free_dma(dev->dma);
1282 free_pages((unsigned long) adapter->dma_buffer, __get_order(DMA_BUFFER_SIZE));
1283
1284 MOD_DEC_USE_COUNT;
1285
1286 return 0;
1287 }
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299 static void elp_set_mc_list(struct device *dev)
1300 {
1301 elp_device *adapter = (elp_device *) dev->priv;
1302 struct dev_mc_list *dmi = dev->mc_list;
1303 int i;
1304
1305 if (elp_debug >= 3)
1306 printk("%s: request to set multicast list\n", dev->name);
1307
1308 if (!(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1309
1310
1311 adapter->tx_pcb.command = CMD_LOAD_MULTICAST_LIST;
1312 adapter->tx_pcb.length = 6 * dev->mc_count;
1313 for (i = 0; i < dev->mc_count; i++) {
1314 memcpy(adapter->tx_pcb.data.multicast[i], dmi->dmi_addr, 6);
1315 dmi = dmi->next;
1316 }
1317 adapter->got[CMD_LOAD_MULTICAST_LIST] = 0;
1318 if (!send_pcb(dev, &adapter->tx_pcb))
1319 printk("%s: couldn't send set_multicast command\n", dev->name);
1320 else {
1321 int timeout = jiffies + TIMEOUT;
1322 while (adapter->got[CMD_LOAD_MULTICAST_LIST] == 0 && jiffies < timeout);
1323 if (jiffies >= timeout) {
1324 TIMEOUT_MSG(__LINE__);
1325 }
1326 }
1327 if (dev->mc_count)
1328 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD | RECV_MULTI;
1329 else
1330 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1331 } else
1332 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_PROMISC;
1333
1334
1335
1336
1337 if (elp_debug >= 3)
1338 printk("%s: sending 82586 configure command\n", dev->name);
1339 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1340 adapter->tx_pcb.length = 2;
1341 adapter->got[CMD_CONFIGURE_82586] = 0;
1342 if (!send_pcb(dev, &adapter->tx_pcb))
1343 printk("%s: couldn't send 82586 configure command\n", dev->name);
1344 else {
1345 int timeout = jiffies + TIMEOUT;
1346 while (adapter->got[CMD_CONFIGURE_82586] == 0 && jiffies < timeout);
1347 if (jiffies >= timeout)
1348 TIMEOUT_MSG(__LINE__);
1349 }
1350 }
1351
1352
1353
1354
1355
1356
1357
1358 static void elp_init(struct device *dev)
1359 {
1360 elp_device *adapter = dev->priv;
1361
1362
1363
1364
1365 dev->open = elp_open;
1366 dev->stop = elp_close;
1367 dev->get_stats = elp_get_stats;
1368 dev->hard_start_xmit = elp_start_xmit;
1369 dev->set_multicast_list = elp_set_mc_list;
1370
1371
1372 ether_setup(dev);
1373
1374
1375
1376
1377 memset(&(adapter->stats), 0, sizeof(struct enet_statistics));
1378
1379
1380
1381
1382 dev->mem_start = dev->mem_end = dev->rmem_end = dev->rmem_start = 0;
1383 }
1384
1385
1386
1387
1388
1389
1390
1391 static int elp_sense(struct device *dev)
1392 {
1393 int timeout;
1394 int addr = dev->base_addr;
1395 const char *name = dev->name;
1396 long flags;
1397 byte orig_HCR, orig_HSR;
1398
1399 if (check_region(addr, 0xf))
1400 return -1;
1401
1402 orig_HCR = inb_control(addr);
1403 orig_HSR = inb_status(addr);
1404
1405 if (elp_debug > 0)
1406 printk(search_msg, name, addr);
1407
1408 if (((orig_HCR == 0xff) && (orig_HSR == 0xff)) ||
1409 ((orig_HCR & DIR) != (orig_HSR & DIR))) {
1410 if (elp_debug > 0)
1411 printk(notfound_msg, 1);
1412 return -1;
1413 }
1414
1415 save_flags(flags);
1416 sti();
1417
1418
1419 if (elp_debug > 0)
1420 printk(stilllooking_msg);
1421 if (orig_HCR & DIR) {
1422
1423 outb_control(orig_HCR & ~DIR, addr);
1424 timeout = jiffies + 30;
1425 while (jiffies < timeout);
1426 restore_flags(flags);
1427 if (inb_status(addr) & DIR) {
1428 outb_control(orig_HCR, addr);
1429 if (elp_debug > 0)
1430 printk(notfound_msg, 2);
1431 return -1;
1432 }
1433 } else {
1434
1435 outb_control(orig_HCR | DIR, addr);
1436 timeout = jiffies + 30;
1437 while (jiffies < timeout);
1438 restore_flags(flags);
1439 if (!(inb_status(addr) & DIR)) {
1440 outb_control(orig_HCR, addr);
1441 if (elp_debug > 0)
1442 printk(notfound_msg, 3);
1443 return -1;
1444 }
1445 }
1446
1447
1448
1449
1450 if (elp_debug > 0)
1451 printk(found_msg);
1452
1453 return 0;
1454 }
1455
1456
1457
1458
1459
1460
1461
1462 static int elp_autodetect(struct device *dev)
1463 {
1464 int idx = 0;
1465
1466
1467
1468 if (dev->base_addr != 0) {
1469 if (elp_sense(dev) == 0)
1470 return dev->base_addr;
1471 } else
1472 while ((dev->base_addr = addr_list[idx++])) {
1473 if (elp_sense(dev) == 0)
1474 return dev->base_addr;
1475 }
1476
1477
1478 if (elp_debug > 0)
1479 printk(couldnot_msg, dev->name);
1480
1481 return 0;
1482 }
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506 int elplus_probe(struct device *dev)
1507 {
1508 elp_device *adapter;
1509 int i, tries, tries1, timeout, okay;
1510
1511
1512
1513
1514
1515 dev->base_addr = elp_autodetect(dev);
1516 if (!(dev->base_addr))
1517 return -ENODEV;
1518
1519
1520
1521
1522 adapter = (elp_device *) (dev->priv = kmalloc(sizeof(elp_device), GFP_KERNEL));
1523 if (adapter == NULL) {
1524 printk("%s: out of memory\n", dev->name);
1525 return -ENODEV;
1526 }
1527
1528 for (tries1 = 0; tries1 < 3; tries1++) {
1529 outb_control((inb_control(dev->base_addr) | CMDE) & ~DIR, dev->base_addr);
1530
1531
1532
1533 timeout = jiffies + 5;
1534 okay = 0;
1535 while (jiffies < timeout && !(inb_status(dev->base_addr) & HCRE));
1536 if ((inb_status(dev->base_addr) & HCRE)) {
1537 outb_command(0, dev->base_addr);
1538 timeout = jiffies + 5;
1539 while (jiffies < timeout && !(inb_status(dev->base_addr) & HCRE));
1540 if (inb_status(dev->base_addr) & HCRE)
1541 okay = 1;
1542 }
1543 if (!okay) {
1544
1545
1546
1547 printk("%s: command register wouldn't drain, assuming ", dev->name);
1548 if ((inb_status(dev->base_addr) & 7) == 3) {
1549
1550
1551
1552 printk("3c505 still starting\n");
1553 timeout = jiffies + 10 * HZ;
1554 while (jiffies < timeout && (inb_status(dev->base_addr) & 7));
1555 if (inb_status(dev->base_addr) & 7) {
1556 printk("%s: 3c505 failed to start\n", dev->name);
1557 continue;
1558 }
1559 } else {
1560
1561
1562
1563 printk("3c505 dead\n");
1564 continue;
1565 }
1566 }
1567 for (tries = 0; tries < 5; tries++) {
1568
1569
1570
1571
1572
1573 adapter->tx_pcb.command = CMD_STATION_ADDRESS;
1574 adapter->tx_pcb.length = 0;
1575 autoirq_setup(0);
1576 if (!send_pcb(dev, &adapter->tx_pcb)) {
1577 printk("%s: could not send first PCB\n", dev->name);
1578 autoirq_report(0);
1579 continue;
1580 }
1581 if (!receive_pcb(dev, &adapter->rx_pcb)) {
1582 printk("%s: could not read first PCB\n", dev->name);
1583 autoirq_report(0);
1584 continue;
1585 }
1586 if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
1587 (adapter->rx_pcb.length != 6)) {
1588 printk("%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
1589 autoirq_report(0);
1590 continue;
1591 }
1592 goto okay;
1593 }
1594
1595
1596
1597 printk(KERN_INFO "%s: resetting adapter\n", dev->name);
1598 outb_control(inb_control(dev->base_addr) | FLSH | ATTN, dev->base_addr);
1599 outb_control(inb_control(dev->base_addr) & ~(FLSH | ATTN), dev->base_addr);
1600 }
1601 printk("%s: 3c505 is sulking, giving up\n", dev->name);
1602 return -ENODEV;
1603
1604 okay:
1605 if (dev->irq) {
1606 int rpt = autoirq_report(0);
1607 if (dev->irq != rpt) {
1608 printk("%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
1609 return -ENODEV;
1610 }
1611
1612 } else
1613 dev->irq = autoirq_report(0);
1614 switch (dev->irq) {
1615 case 0:
1616 printk("%s: No IRQ reported by autoirq_report().\n", dev->name);
1617 printk("%s: Check the jumpers of your 3c505 board.\n", dev->name);
1618 return -ENODEV;
1619 case 1:
1620 case 6:
1621 case 8:
1622 case 13:
1623 printk("%s: Impossible IRQ %d reported by autoirq_report().\n",
1624 dev->name, dev->irq);
1625 return -ENODEV;
1626 }
1627
1628
1629
1630
1631 outb_control(inb_control(dev->base_addr) & ~CMDE, dev->base_addr);
1632
1633
1634
1635
1636 for (i = 0; i < 6; i++)
1637 dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
1638
1639
1640 dev->dma = ELP_DMA;
1641
1642
1643
1644
1645 printk("%s: 3c505 at %#lx, irq %d, dma %d, ",
1646 dev->name, dev->base_addr, dev->irq, dev->dma);
1647 printk("addr %02x:%02x:%02x:%02x:%02x:%02x, ",
1648 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1649 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1650
1651
1652
1653
1654
1655 adapter->tx_pcb.command = CMD_ADAPTER_INFO;
1656 adapter->tx_pcb.length = 0;
1657 if (!send_pcb(dev, &adapter->tx_pcb) ||
1658 !receive_pcb(dev, &adapter->rx_pcb) ||
1659 (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
1660 (adapter->rx_pcb.length != 10)) {
1661 printk("%s: not responding to second PCB\n", dev->name);
1662 }
1663 printk("rev %d.%d, %dk\n", adapter->rx_pcb.data.info.major_vers, adapter->rx_pcb.data.info.minor_vers, adapter->rx_pcb.data.info.RAM_sz);
1664
1665
1666
1667
1668 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
1669 adapter->tx_pcb.length = 12;
1670 adapter->tx_pcb.data.memconf.cmd_q = 8;
1671 adapter->tx_pcb.data.memconf.rcv_q = 8;
1672 adapter->tx_pcb.data.memconf.mcast = 10;
1673 adapter->tx_pcb.data.memconf.frame = 10;
1674 adapter->tx_pcb.data.memconf.rcv_b = 10;
1675 adapter->tx_pcb.data.memconf.progs = 0;
1676 if (!send_pcb(dev, &adapter->tx_pcb) ||
1677 !receive_pcb(dev, &adapter->rx_pcb) ||
1678 (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
1679 (adapter->rx_pcb.length != 2)) {
1680 printk("%s: could not configure adapter memory\n", dev->name);
1681 }
1682 if (adapter->rx_pcb.data.configure) {
1683 printk("%s: adapter configuration failed\n", dev->name);
1684 }
1685
1686
1687
1688 request_region(dev->base_addr, ELP_IO_EXTENT, "3c505");
1689
1690
1691
1692
1693 elp_init(dev);
1694
1695 return 0;
1696 }
1697
1698 #ifdef MODULE
1699 static char devicename[9] =
1700 {0,};
1701 static struct device dev_3c505 =
1702 {
1703 devicename,
1704 0, 0, 0, 0,
1705 0, 0,
1706 0, 0, 0, NULL, elplus_probe};
1707
1708 int io = 0x300;
1709 int irq = 0;
1710
1711 int init_module(void)
1712 {
1713 if (io == 0)
1714 printk("3c505: You should not use auto-probing with insmod!\n");
1715 dev_3c505.base_addr = io;
1716 dev_3c505.irq = irq;
1717 if (register_netdev(&dev_3c505) != 0) {
1718 printk("3c505: register_netdev() returned non-zero.\n");
1719 return -EIO;
1720 }
1721 return 0;
1722 }
1723
1724 void cleanup_module(void)
1725 {
1726 unregister_netdev(&dev_3c505);
1727 kfree(dev_3c505.priv);
1728 dev_3c505.priv = NULL;
1729
1730
1731 release_region(dev_3c505.base_addr, ELP_IO_EXTENT);
1732 }
1733
1734 #endif
1735
1736
1737
1738
1739
1740
1741
1742
1743