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