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