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