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