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