Merge from vendor branch BINUTILS:
[dragonfly.git] / sys / dev / netif / ex / if_ex.c
1 /*
2  * Copyright (c) 1996, Javier Martín Rueda (jmrueda@diatel.upm.es)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/ex/if_ex.c,v 1.26.2.3 2001/03/05 05:33:20 imp Exp $
28  * $DragonFly: src/sys/dev/netif/ex/if_ex.c,v 1.19 2005/06/14 15:23:18 joerg Exp $
29  *
30  * MAINTAINER: Matthew N. Dodd <winter@jurai.net>
31  *                             <mdodd@FreeBSD.org>
32  */
33
34 /*
35  * Intel EtherExpress Pro/10, Pro/10+ Ethernet driver
36  *
37  * Revision history:
38  *
39  * 30-Oct-1996: first beta version. Inet and BPF supported, but no multicast.
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/sockio.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/thread2.h>
49
50 #include <sys/module.h>
51 #include <sys/bus.h>
52
53 #include <machine/bus.h>
54 #include <machine/resource.h>
55 #include <sys/rman.h>
56
57 #include <net/if.h>
58 #include <net/ifq_var.h>
59 #include <net/if.h>
60 #include <net/if_arp.h>
61 #include <net/if_media.h> 
62 #include <net/ethernet.h>
63 #include <net/bpf.h>
64
65 #include <netinet/in.h>
66 #include <netinet/if_ether.h>
67 #include <machine/clock.h>
68
69
70 #include <bus/isa/isavar.h>
71 #include <bus/isa/pnpvar.h>
72
73 #include "if_exreg.h"
74 #include "if_exvar.h"
75
76 DECLARE_DUMMY_MODULE(if_ex);
77
78 #ifdef EXDEBUG
79 # define Start_End 1
80 # define Rcvd_Pkts 2
81 # define Sent_Pkts 4
82 # define Status    8
83 static int debug_mask = 0;
84 static int exintr_count = 0;
85 # define DODEBUG(level, action) if (level & debug_mask) action
86 #else
87 # define DODEBUG(level, action)
88 #endif
89
90 char irq2eemap[] =
91         { -1, -1, 0, 1, -1, 2, -1, -1, -1, 0, 3, 4, -1, -1, -1, -1 };
92 u_char ee2irqmap[] =
93         { 9, 3, 5, 10, 11, 0, 0, 0 };
94                 
95 char plus_irq2eemap[] =
96         { -1, -1, -1, 0, 1, 2, -1, 3, -1, 4, 5, 6, 7, -1, -1, -1 };
97 u_char plus_ee2irqmap[] =
98         { 3, 4, 5, 7, 9, 10, 11, 12 };
99
100 /* Network Interface Functions */
101 static void     ex_init         (void *);
102 static void     ex_start        (struct ifnet *);
103 static int      ex_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
104 static void     ex_watchdog     (struct ifnet *);
105
106 /* ifmedia Functions    */
107 static int      ex_ifmedia_upd  (struct ifnet *);
108 static void     ex_ifmedia_sts  (struct ifnet *, struct ifmediareq *);
109
110 static int      ex_get_media    (u_int32_t iobase);
111
112 static void     ex_reset        (struct ex_softc *);
113
114 static void     ex_tx_intr      (struct ex_softc *);
115 static void     ex_rx_intr      (struct ex_softc *);
116
117 int
118 look_for_card (u_int32_t iobase)
119 {
120         int count1, count2;
121
122         /*
123          * Check for the i82595 signature, and check that the round robin
124          * counter actually advances.
125          */
126         if (((count1 = inb(iobase + ID_REG)) & Id_Mask) != Id_Sig)
127                 return(0);
128         count2 = inb(iobase + ID_REG);
129         count2 = inb(iobase + ID_REG);
130         count2 = inb(iobase + ID_REG);
131
132         return((count2 & Counter_bits) == ((count1 + 0xc0) & Counter_bits));
133 }
134
135 void
136 ex_get_address (u_int32_t iobase, u_char *enaddr)
137 {
138         u_int16_t       eaddr_tmp;
139
140         eaddr_tmp = eeprom_read(iobase, EE_Eth_Addr_Lo);
141         enaddr[5] = eaddr_tmp & 0xff;
142         enaddr[4] = eaddr_tmp >> 8;
143         eaddr_tmp = eeprom_read(iobase, EE_Eth_Addr_Mid);
144         enaddr[3] = eaddr_tmp & 0xff;
145         enaddr[2] = eaddr_tmp >> 8;
146         eaddr_tmp = eeprom_read(iobase, EE_Eth_Addr_Hi);
147         enaddr[1] = eaddr_tmp & 0xff;
148         enaddr[0] = eaddr_tmp >> 8;
149         
150         return;
151 }
152
153 int
154 ex_card_type (u_char *enaddr)
155 {
156         if ((enaddr[0] == 0x00) && (enaddr[1] == 0xA0) && (enaddr[2] == 0xC9))
157                 return (CARD_TYPE_EX_10_PLUS);
158
159         return (CARD_TYPE_EX_10);
160 }
161
162 /*
163  * Caller is responsible for eventually calling
164  * ex_release_resources() on failure.
165  */
166 int
167 ex_alloc_resources (device_t dev)
168 {
169         struct ex_softc *       sc = device_get_softc(dev);
170         int                     error = 0;
171
172         sc->ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &sc->ioport_rid,
173             RF_ACTIVE);
174         if (!sc->ioport) {
175                 device_printf(dev, "No I/O space?!\n");
176                 error = ENOMEM;
177                 goto bad;
178         }
179
180         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
181             RF_ACTIVE);
182
183         if (!sc->irq) {
184                 device_printf(dev, "No IRQ?!\n");
185                 error = ENOMEM;
186                 goto bad;
187         }
188
189 bad:
190         return (error);
191 }
192
193 void
194 ex_release_resources (device_t dev)
195 {
196         struct ex_softc *       sc = device_get_softc(dev);
197
198         if (sc->ih) {
199                 bus_teardown_intr(dev, sc->irq, sc->ih);
200                 sc->ih = NULL;
201         }
202
203         if (sc->ioport) {
204                 bus_release_resource(dev, SYS_RES_IOPORT,
205                                         sc->ioport_rid, sc->ioport);
206                 sc->ioport = NULL;
207         }
208
209         if (sc->irq) {
210                 bus_release_resource(dev, SYS_RES_IRQ,
211                                         sc->irq_rid, sc->irq);
212                 sc->irq = NULL;
213         }
214
215         return;
216 }
217
218 int
219 ex_attach(device_t dev)
220 {
221         struct ex_softc *       sc = device_get_softc(dev);
222         struct ifnet *          ifp = &sc->arpcom.ac_if;
223         struct ifmedia *        ifm;
224         int                     unit = device_get_unit(dev);
225         u_int16_t               temp;
226
227         /* work out which set of irq <-> internal tables to use */
228         if (ex_card_type(sc->arpcom.ac_enaddr) == CARD_TYPE_EX_10_PLUS) {
229                 sc->irq2ee = plus_irq2eemap;
230                 sc->ee2irq = plus_ee2irqmap;
231         } else {
232                 sc->irq2ee = irq2eemap;
233                 sc->ee2irq = ee2irqmap;
234         }
235
236         sc->mem_size = CARD_RAM_SIZE;   /* XXX This should be read from the card itself. */
237
238         /*
239          * Initialize the ifnet structure.
240          */
241         ifp->if_softc = sc;
242         if_initname(ifp, "ex", unit);
243         ifp->if_mtu = ETHERMTU;
244         ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST /* XXX not done yet. | IFF_MULTICAST */;
245         ifp->if_start = ex_start;
246         ifp->if_ioctl = ex_ioctl;
247         ifp->if_watchdog = ex_watchdog;
248         ifp->if_init = ex_init;
249         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
250         ifq_set_ready(&ifp->if_snd);
251
252         ifmedia_init(&sc->ifmedia, 0, ex_ifmedia_upd, ex_ifmedia_sts);
253
254         temp = eeprom_read(sc->iobase, EE_W5);
255         if (temp & EE_W5_PORT_TPE)
256                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
257         if (temp & EE_W5_PORT_BNC)
258                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_2, 0, NULL);
259         if (temp & EE_W5_PORT_AUI)
260                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_5, 0, NULL);
261
262         ifmedia_set(&sc->ifmedia, ex_get_media(sc->iobase));
263
264         ifm = &sc->ifmedia;
265         ifm->ifm_media = ifm->ifm_cur->ifm_media;
266         ex_ifmedia_upd(ifp);
267
268         /*
269          * Attach the interface.
270          */
271         ether_ifattach(ifp, sc->arpcom.ac_enaddr);
272
273         return(0);
274 }
275
276 static void
277 ex_init(void *xsc)
278 {
279         struct ex_softc *       sc = (struct ex_softc *) xsc;
280         struct ifnet *          ifp = &sc->arpcom.ac_if;
281         int                     i;
282         int                     iobase = sc->iobase;
283         unsigned short          temp_reg;
284
285         DODEBUG(Start_End, printf("ex_init%d: start\n", ifp->if_dunit););
286
287         crit_enter();
288
289         ifp->if_timer = 0;
290
291         /*
292          * Load the ethernet address into the card.
293          */
294         outb(iobase + CMD_REG, Bank2_Sel);
295         temp_reg = inb(iobase + EEPROM_REG);
296         if (temp_reg & Trnoff_Enable) {
297                 outb(iobase + EEPROM_REG, temp_reg & ~Trnoff_Enable);
298         }
299         for (i = 0; i < ETHER_ADDR_LEN; i++) {
300                 outb(iobase + I_ADDR_REG0 + i, sc->arpcom.ac_enaddr[i]);
301         }
302         /*
303          * - Setup transmit chaining and discard bad received frames.
304          * - Match broadcast.
305          * - Clear test mode.
306          * - Set receiving mode.
307          * - Set IRQ number.
308          */
309         outb(iobase + REG1, inb(iobase + REG1) | Tx_Chn_Int_Md | Tx_Chn_ErStp | Disc_Bad_Fr);
310         outb(iobase + REG2, inb(iobase + REG2) | No_SA_Ins | RX_CRC_InMem);
311         outb(iobase + REG3, inb(iobase + REG3) & 0x3f /* XXX constants. */ );
312         outb(iobase + CMD_REG, Bank1_Sel);
313         outb(iobase + INT_NO_REG, (inb(iobase + INT_NO_REG) & 0xf8) | sc->irq2ee[sc->irq_no]);
314
315         /*
316          * Divide the available memory in the card into rcv and xmt buffers.
317          * By default, I use the first 3/4 of the memory for the rcv buffer,
318          * and the remaining 1/4 of the memory for the xmt buffer.
319          */
320         sc->rx_mem_size = sc->mem_size * 3 / 4;
321         sc->tx_mem_size = sc->mem_size - sc->rx_mem_size;
322         sc->rx_lower_limit = 0x0000;
323         sc->rx_upper_limit = sc->rx_mem_size - 2;
324         sc->tx_lower_limit = sc->rx_mem_size;
325         sc->tx_upper_limit = sc->mem_size - 2;
326         outb(iobase + RCV_LOWER_LIMIT_REG, sc->rx_lower_limit >> 8);
327         outb(iobase + RCV_UPPER_LIMIT_REG, sc->rx_upper_limit >> 8);
328         outb(iobase + XMT_LOWER_LIMIT_REG, sc->tx_lower_limit >> 8);
329         outb(iobase + XMT_UPPER_LIMIT_REG, sc->tx_upper_limit >> 8);
330         
331         /*
332          * Enable receive and transmit interrupts, and clear any pending int.
333          */
334         outb(iobase + REG1, inb(iobase + REG1) | TriST_INT);
335         outb(iobase + CMD_REG, Bank0_Sel);
336         outb(iobase + MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
337         outb(iobase + STATUS_REG, All_Int);
338
339         /*
340          * Initialize receive and transmit ring buffers.
341          */
342         outw(iobase + RCV_BAR, sc->rx_lower_limit);
343         sc->rx_head = sc->rx_lower_limit;
344         outw(iobase + RCV_STOP_REG, sc->rx_upper_limit | 0xfe);
345         outw(iobase + XMT_BAR, sc->tx_lower_limit);
346         sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
347
348         ifp->if_flags |= IFF_RUNNING;
349         ifp->if_flags &= ~IFF_OACTIVE;
350         DODEBUG(Status, printf("OIDLE init\n"););
351         
352         /*
353          * Final reset of the board, and enable operation.
354          */
355         outb(iobase + CMD_REG, Sel_Reset_CMD);
356         DELAY(2);
357         outb(iobase + CMD_REG, Rcv_Enable_CMD);
358
359         ex_start(ifp);
360
361         crit_exit();
362
363         DODEBUG(Start_End, printf("ex_init%d: finish\n", ifp->if_dunit););
364 }
365
366
367 static void
368 ex_start(struct ifnet *ifp)
369 {
370         struct ex_softc *       sc = ifp->if_softc;
371         int                     iobase = sc->iobase;
372         int                     i, len, data_len, avail, dest, next;
373         unsigned char           tmp16[2];
374         struct mbuf *           opkt;
375         struct mbuf *           m;
376
377         DODEBUG(Start_End, printf("ex_start%d: start\n", unit););
378
379         crit_enter();
380
381         /*
382          * Main loop: send outgoing packets to network card until there are no
383          * more packets left, or the card cannot accept any more yet.
384          */
385         while ((ifp->if_flags & IFF_OACTIVE) == 0) {
386                 opkt = ifq_poll(&ifp->if_snd);
387                 if (opkt == NULL)
388                         break;
389
390                 /*
391                  * Ensure there is enough free transmit buffer space for
392                  * this packet, including its header. Note: the header
393                  * cannot wrap around the end of the transmit buffer and
394                  * must be kept together, so we allow space for twice the
395                  * length of the header, just in case.
396                  */
397
398                 for (len = 0, m = opkt; m != NULL; m = m->m_next) {
399                         len += m->m_len;
400                 }
401
402                 data_len = len;
403
404                 DODEBUG(Sent_Pkts, printf("1. Sending packet with %d data bytes. ", data_len););
405
406                 if (len & 1) {
407                         len += XMT_HEADER_LEN + 1;
408                 } else {
409                         len += XMT_HEADER_LEN;
410                 }
411
412                 if ((i = sc->tx_tail - sc->tx_head) >= 0) {
413                         avail = sc->tx_mem_size - i;
414                 } else {
415                         avail = -i;
416                 }
417
418                 DODEBUG(Sent_Pkts, printf("i=%d, avail=%d\n", i, avail););
419
420                 if (avail >= len + XMT_HEADER_LEN) {
421                         opkt = ifq_dequeue(&ifp->if_snd);
422
423 #ifdef EX_PSA_INTR      
424                         /*
425                          * Disable rx and tx interrupts, to avoid corruption
426                          * of the host address register by interrupt service
427                          * routines.
428                          * XXX Is this necessary with splimp() enabled?
429                          */
430                         outb(iobase + MASK_REG, All_Int);
431 #endif
432
433                         /*
434                          * Compute the start and end addresses of this
435                          * frame in the tx buffer.
436                          */
437                         dest = sc->tx_tail;
438                         next = dest + len;
439
440                         if (next > sc->tx_upper_limit) {
441                                 if ((sc->tx_upper_limit + 2 - sc->tx_tail) <=
442                                     XMT_HEADER_LEN) {
443                                         dest = sc->tx_lower_limit;
444                                         next = dest + len;
445                                 } else {
446                                         next = sc->tx_lower_limit +
447                                                 next - sc->tx_upper_limit - 2;
448                                 }
449                         }
450
451                         /*
452                          * Build the packet frame in the card's ring buffer.
453                          */
454                         DODEBUG(Sent_Pkts, printf("2. dest=%d, next=%d. ", dest, next););
455
456                         outw(iobase + HOST_ADDR_REG, dest);
457                         outw(iobase + IO_PORT_REG, Transmit_CMD);
458                         outw(iobase + IO_PORT_REG, 0);
459                         outw(iobase + IO_PORT_REG, next);
460                         outw(iobase + IO_PORT_REG, data_len);
461
462                         /*
463                          * Output the packet data to the card. Ensure all
464                          * transfers are 16-bit wide, even if individual
465                          * mbufs have odd length.
466                          */
467
468                         for (m = opkt, i = 0; m != NULL; m = m->m_next) {
469                                 DODEBUG(Sent_Pkts, printf("[%d]", m->m_len););
470                                 if (i) {
471                                         tmp16[1] = *(mtod(m, caddr_t));
472                                         outsw(iobase + IO_PORT_REG, tmp16, 1);
473                                 }
474                                 outsw(iobase + IO_PORT_REG,
475                                       mtod(m, caddr_t) + i, (m->m_len - i) / 2);
476
477                                 if ((i = (m->m_len - i) & 1) != 0) {
478                                         tmp16[0] = *(mtod(m, caddr_t) +
479                                                    m->m_len - 1);
480                                 }
481                         }
482                         if (i) {
483                                 outsw(iobase + IO_PORT_REG, tmp16, 1);
484                         }
485         
486                         /*
487                          * If there were other frames chained, update the
488                          * chain in the last one.
489                          */
490                         if (sc->tx_head != sc->tx_tail) {
491                                 if (sc->tx_tail != dest) {
492                                         outw(iobase + HOST_ADDR_REG,
493                                              sc->tx_last + XMT_Chain_Point);
494                                         outw(iobase + IO_PORT_REG, dest);
495                                 }
496                                 outw(iobase + HOST_ADDR_REG,
497                                      sc->tx_last + XMT_Byte_Count);
498                                 i = inw(iobase + IO_PORT_REG);
499                                 outw(iobase + HOST_ADDR_REG,
500                                      sc->tx_last + XMT_Byte_Count);
501                                 outw(iobase + IO_PORT_REG, i | Ch_bit);
502                         }
503         
504                         /*
505                          * Resume normal operation of the card:
506                          * - Make a dummy read to flush the DRAM write
507                          *   pipeline.
508                          * - Enable receive and transmit interrupts.
509                          * - Send Transmit or Resume_XMT command, as
510                          *   appropriate.
511                          */
512                         inw(iobase + IO_PORT_REG);
513 #ifdef EX_PSA_INTR
514                         outb(iobase + MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
515 #endif
516                         if (sc->tx_head == sc->tx_tail) {
517                                 outw(iobase + XMT_BAR, dest);
518                                 outb(iobase + CMD_REG, Transmit_CMD);
519                                 sc->tx_head = dest;
520                                 DODEBUG(Sent_Pkts, printf("Transmit\n"););
521                         } else {
522                                 outb(iobase + CMD_REG, Resume_XMT_List_CMD);
523                                 DODEBUG(Sent_Pkts, printf("Resume\n"););
524                         }
525         
526                         sc->tx_last = dest;
527                         sc->tx_tail = next;
528          
529                         BPF_MTAP(ifp, opkt);
530
531                         ifp->if_timer = 2;
532                         ifp->if_opackets++;
533                         m_freem(opkt);
534                 } else {
535                         ifp->if_flags |= IFF_OACTIVE;
536                         DODEBUG(Status, printf("OACTIVE start\n"););
537                 }
538         }
539
540         crit_exit();
541
542         DODEBUG(Start_End, printf("ex_start%d: finish\n", unit););
543 }
544
545 void
546 ex_stop(struct ex_softc *sc)
547 {
548         int iobase = sc->iobase;
549
550         DODEBUG(Start_End, printf("ex_stop%d: start\n", unit););
551
552         /*
553          * Disable card operation:
554          * - Disable the interrupt line.
555          * - Flush transmission and disable reception.
556          * - Mask and clear all interrupts.
557          * - Reset the 82595.
558          */
559         outb(iobase + CMD_REG, Bank1_Sel);
560         outb(iobase + REG1, inb(iobase + REG1) & ~TriST_INT);
561         outb(iobase + CMD_REG, Bank0_Sel);
562         outb(iobase + CMD_REG, Rcv_Stop);
563         sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
564         sc->tx_last = 0; /* XXX I think these two lines are not necessary, because ex_init will always be called again to reinit the interface. */
565         outb(iobase + MASK_REG, All_Int);
566         outb(iobase + STATUS_REG, All_Int);
567         outb(iobase + CMD_REG, Reset_CMD);
568         DELAY(200);
569
570         DODEBUG(Start_End, printf("ex_stop%d: finish\n", unit););
571
572         return;
573 }
574
575 void
576 ex_intr(void *arg)
577 {
578         struct ex_softc *       sc = (struct ex_softc *)arg;
579         struct ifnet *  ifp = &sc->arpcom.ac_if;
580         int                     iobase = sc->iobase;
581         int                     int_status, send_pkts;
582
583         DODEBUG(Start_End, printf("ex_intr%d: start\n", unit););
584
585 #ifdef EXDEBUG
586         if (++exintr_count != 1)
587                 printf("WARNING: nested interrupt (%d). Mail the author.\n", exintr_count);
588 #endif
589
590         send_pkts = 0;
591         while ((int_status = inb(iobase + STATUS_REG)) & (Tx_Int | Rx_Int)) {
592                 if (int_status & Rx_Int) {
593                         outb(iobase + STATUS_REG, Rx_Int);
594
595                         ex_rx_intr(sc);
596                 } else if (int_status & Tx_Int) {
597                         outb(iobase + STATUS_REG, Tx_Int);
598
599                         ex_tx_intr(sc);
600                         send_pkts = 1;
601                 }
602         }
603
604         /*
605          * If any packet has been transmitted, and there are queued packets to
606          * be sent, attempt to send more packets to the network card.
607          */
608
609         if (send_pkts && !ifq_is_empty(&ifp->if_snd))
610                 ex_start(ifp);
611
612 #ifdef EXDEBUG
613         exintr_count--;
614 #endif
615
616         DODEBUG(Start_End, printf("ex_intr%d: finish\n", unit););
617
618         return;
619 }
620
621 static void
622 ex_tx_intr(struct ex_softc *sc)
623 {
624         struct ifnet *  ifp = &sc->arpcom.ac_if;
625         int             iobase = sc->iobase;
626         int             tx_status;
627
628         DODEBUG(Start_End, printf("ex_tx_intr%d: start\n", unit););
629
630         /*
631          * - Cancel the watchdog.
632          * For all packets transmitted since last transmit interrupt:
633          * - Advance chain pointer to next queued packet.
634          * - Update statistics.
635          */
636
637         ifp->if_timer = 0;
638
639         while (sc->tx_head != sc->tx_tail) {
640                 outw(iobase + HOST_ADDR_REG, sc->tx_head);
641
642                 if (! inw(iobase + IO_PORT_REG) & Done_bit)
643                         break;
644
645                 tx_status = inw(iobase + IO_PORT_REG);
646                 sc->tx_head = inw(iobase + IO_PORT_REG);
647
648                 if (tx_status & TX_OK_bit) {
649                         ifp->if_opackets++;
650                 } else {
651                         ifp->if_oerrors++;
652                 }
653
654                 ifp->if_collisions += tx_status & No_Collisions_bits;
655         }
656
657         /*
658          * The card should be ready to accept more packets now.
659          */
660
661         ifp->if_flags &= ~IFF_OACTIVE;
662
663         DODEBUG(Status, printf("OIDLE tx_intr\n"););
664         DODEBUG(Start_End, printf("ex_tx_intr%d: finish\n", unit););
665
666         return;
667 }
668
669 static void
670 ex_rx_intr(struct ex_softc *sc)
671 {
672         struct ifnet *          ifp = &sc->arpcom.ac_if;
673         int                     iobase = sc->iobase;
674         int                     rx_status;
675         int                     pkt_len;
676         int                     QQQ;
677         struct mbuf *           m;
678         struct mbuf *           ipkt;
679
680         DODEBUG(Start_End, printf("ex_rx_intr%d: start\n", unit););
681
682         /*
683          * For all packets received since last receive interrupt:
684          * - If packet ok, read it into a new mbuf and queue it to interface,
685          *   updating statistics.
686          * - If packet bad, just discard it, and update statistics.
687          * Finally, advance receive stop limit in card's memory to new location.
688          */
689
690         outw(iobase + HOST_ADDR_REG, sc->rx_head);
691
692         while (inw(iobase + IO_PORT_REG) == RCV_Done) {
693
694                 rx_status = inw(iobase + IO_PORT_REG);
695                 sc->rx_head = inw(iobase + IO_PORT_REG);
696                 QQQ = pkt_len = inw(iobase + IO_PORT_REG);
697
698                 if (rx_status & RCV_OK_bit) {
699                         MGETHDR(m, MB_DONTWAIT, MT_DATA);
700                         ipkt = m;
701                         if (ipkt == NULL) {
702                                 ifp->if_iqdrops++;
703                         } else {
704                                 ipkt->m_pkthdr.rcvif = ifp;
705                                 ipkt->m_pkthdr.len = pkt_len;
706                                 ipkt->m_len = MHLEN;
707
708                                 while (pkt_len > 0) {
709                                         if (pkt_len > MINCLSIZE) {
710                                                 MCLGET(m, MB_DONTWAIT);
711                                                 if (m->m_flags & M_EXT) {
712                                                         m->m_len = MCLBYTES;
713                                                 } else {
714                                                         m_freem(ipkt);
715                                                         ifp->if_iqdrops++;
716                                                         goto rx_another;
717                                                 }
718                                         }
719                                         m->m_len = min(m->m_len, pkt_len);
720
721           /*
722            * NOTE: I'm assuming that all mbufs allocated are of even length,
723            * except for the last one in an odd-length packet.
724            */
725
726                                         insw(iobase + IO_PORT_REG,
727                                              mtod(m, caddr_t), m->m_len / 2);
728
729                                         if (m->m_len & 1) {
730                                                 *(mtod(m, caddr_t) + m->m_len - 1) = inb(iobase + IO_PORT_REG);
731                                         }
732                                         pkt_len -= m->m_len;
733
734                                         if (pkt_len > 0) {
735                                                 MGET(m->m_next, MB_DONTWAIT, MT_DATA);
736                                                 if (m->m_next == NULL) {
737                                                         m_freem(ipkt);
738                                                         ifp->if_iqdrops++;
739                                                         goto rx_another;
740                                                 }
741                                                 m = m->m_next;
742                                                 m->m_len = MLEN;
743                                         }
744                                 }
745                                 (*ifp->if_input)(ifp, ipkt);
746                                 ifp->if_ipackets++;
747                         }
748                 } else {
749                         ifp->if_ierrors++;
750                 }
751                 outw(iobase + HOST_ADDR_REG, sc->rx_head);
752 rx_another: ;
753         }
754
755         if (sc->rx_head < sc->rx_lower_limit + 2)
756                 outw(iobase + RCV_STOP_REG, sc->rx_upper_limit);
757         else
758                 outw(iobase + RCV_STOP_REG, sc->rx_head - 2);
759
760         DODEBUG(Start_End, printf("ex_rx_intr%d: finish\n", unit););
761
762         return;
763 }
764
765
766 static int
767 ex_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
768 {
769         struct ex_softc *       sc = ifp->if_softc;
770         struct ifreq *          ifr = (struct ifreq *)data;
771         int                     error = 0;
772
773         DODEBUG(Start_End, printf("ex_ioctl%d: start ", ifp->if_dunit););
774
775         crit_enter();
776
777         switch(cmd) {
778                 case SIOCSIFFLAGS:
779                         DODEBUG(Start_End, printf("SIOCSIFFLAGS"););
780                         if ((ifp->if_flags & IFF_UP) == 0 &&
781                             (ifp->if_flags & IFF_RUNNING)) {
782
783                                 ifp->if_flags &= ~IFF_RUNNING;
784                                 ex_stop(sc);
785                         } else {
786                                 ex_init(sc);
787                         }
788                         break;
789 #ifdef NODEF
790                 case SIOCGHWADDR:
791                         DODEBUG(Start_End, printf("SIOCGHWADDR"););
792                         bcopy((caddr_t)sc->sc_addr, (caddr_t)&ifr->ifr_data,
793                               sizeof(sc->sc_addr));
794                         break;
795 #endif
796                 case SIOCADDMULTI:
797                         DODEBUG(Start_End, printf("SIOCADDMULTI"););
798                 case SIOCDELMULTI:
799                         DODEBUG(Start_End, printf("SIOCDELMULTI"););
800                         /* XXX Support not done yet. */
801                         error = EINVAL;
802                         break;
803                 case SIOCSIFMEDIA:
804                 case SIOCGIFMEDIA:
805                         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, cmd);
806                         break;
807                 default:
808                         DODEBUG(Start_End, printf("unknown"););
809                         error = ether_ioctl(ifp, cmd, data);
810                         break;
811         }
812
813         crit_exit();
814
815         DODEBUG(Start_End, printf("\nex_ioctl%d: finish\n", ifp->if_dunit););
816
817         return(error);
818 }
819
820
821 static void
822 ex_reset(struct ex_softc *sc)
823 {
824         DODEBUG(Start_End, printf("ex_reset%d: start\n", unit););
825   
826         crit_enter();
827
828         ex_stop(sc);
829         ex_init(sc);
830
831         crit_exit();
832
833         DODEBUG(Start_End, printf("ex_reset%d: finish\n", unit););
834 }
835
836 static void
837 ex_watchdog(struct ifnet *ifp)
838 {
839         struct ex_softc *       sc = ifp->if_softc;
840
841         DODEBUG(Start_End, printf("ex_watchdog%d: start\n", ifp->if_dunit););
842
843         ifp->if_flags &= ~IFF_OACTIVE;
844
845         DODEBUG(Status, printf("OIDLE watchdog\n"););
846
847         ifp->if_oerrors++;
848         ex_reset(sc);
849         ex_start(ifp);
850
851         DODEBUG(Start_End, printf("ex_watchdog%d: finish\n", ifp->if_dunit););
852
853         return;
854 }
855
856 static int
857 ex_get_media (u_int32_t iobase)
858 {
859         int     tmp;
860
861         outb(iobase + CMD_REG, Bank2_Sel);
862         tmp = inb(iobase + REG3);
863         outb(iobase + CMD_REG, Bank0_Sel);
864
865         if (tmp & TPE_bit)
866                 return(IFM_ETHER|IFM_10_T);
867         if (tmp & BNC_bit)
868                 return(IFM_ETHER|IFM_10_2);
869
870         return (IFM_ETHER|IFM_10_5);
871 }
872
873 static int
874 ex_ifmedia_upd (ifp)
875         struct ifnet *          ifp;
876 {
877
878         return (0);
879 }
880
881 static void
882 ex_ifmedia_sts(ifp, ifmr)
883         struct ifnet *          ifp;
884         struct ifmediareq *     ifmr;
885 {
886         struct ex_softc *       sc = ifp->if_softc;
887
888         ifmr->ifm_active = ex_get_media(sc->iobase);
889
890         return;
891 }
892
893 u_short
894 eeprom_read(u_int32_t iobase, int location)
895 {
896         int i;
897         u_short data = 0;
898         int ee_addr;
899         int read_cmd = location | EE_READ_CMD;
900         short ctrl_val = EECS;
901
902         ee_addr = iobase + EEPROM_REG;
903         outb(iobase + CMD_REG, Bank2_Sel);
904         outb(ee_addr, EECS);
905         for (i = 8; i >= 0; i--) {
906                 short outval = (read_cmd & (1 << i)) ? ctrl_val | EEDI : ctrl_val;
907                 outb(ee_addr, outval);
908                 outb(ee_addr, outval | EESK);
909                 DELAY(3);
910                 outb(ee_addr, outval);
911                 DELAY(2);
912         }
913         outb(ee_addr, ctrl_val);
914
915         for (i = 16; i > 0; i--) {
916                 outb(ee_addr, ctrl_val | EESK);
917                 DELAY(3);
918                 data = (data << 1) | ((inb(ee_addr) & EEDO) ? 1 : 0);
919                 outb(ee_addr, ctrl_val);
920                 DELAY(2);
921         }
922
923         ctrl_val &= ~EECS;
924         outb(ee_addr, ctrl_val | EESK);
925         DELAY(3);
926         outb(ee_addr, ctrl_val);
927         DELAY(2);
928         outb(iobase + CMD_REG, Bank0_Sel);
929         return(data);
930 }