]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ex/if_ex.c
Convert atse(4) driver for Altera Triple-Speed Ethernet MegaCore to use
[FreeBSD/FreeBSD.git] / sys / dev / ex / if_ex.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1996, Javier Martín Rueda (jmrueda@diatel.upm.es)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *
30  * MAINTAINER: Matthew N. Dodd <winter@jurai.net>
31  *                             <mdodd@FreeBSD.org>
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 /*
38  * Intel EtherExpress Pro/10, Pro/10+ Ethernet driver
39  *
40  * Revision history:
41  *
42  * dd-mmm-yyyy: Multicast support ported from NetBSD's if_iy driver.
43  * 30-Oct-1996: first beta version. Inet and BPF supported, but no multicast.
44  */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/sockio.h>
50 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52
53 #include <sys/module.h>
54 #include <sys/bus.h>
55
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58 #include <sys/rman.h>
59
60 #include <net/if.h>
61 #include <net/if_var.h>
62 #include <net/if_arp.h>
63 #include <net/if_dl.h>
64 #include <net/if_media.h> 
65 #include <net/if_types.h> 
66 #include <net/ethernet.h>
67 #include <net/bpf.h>
68
69 #include <netinet/in.h>
70 #include <netinet/if_ether.h>
71
72
73 #include <isa/isavar.h>
74 #include <isa/pnpvar.h>
75
76 #include <dev/ex/if_exreg.h>
77 #include <dev/ex/if_exvar.h>
78
79 #ifdef EXDEBUG
80 # define Start_End 1
81 # define Rcvd_Pkts 2
82 # define Sent_Pkts 4
83 # define Status    8
84 static int debug_mask = 0;
85 # define DODEBUG(level, action) if (level & debug_mask) action
86 #else
87 # define DODEBUG(level, action)
88 #endif
89
90 devclass_t ex_devclass;
91
92 char irq2eemap[] =
93         { -1, -1, 0, 1, -1, 2, -1, -1, -1, 0, 3, 4, -1, -1, -1, -1 };
94 u_char ee2irqmap[] =
95         { 9, 3, 5, 10, 11, 0, 0, 0 };
96                 
97 char plus_irq2eemap[] =
98         { -1, -1, -1, 0, 1, 2, -1, 3, -1, 4, 5, 6, 7, -1, -1, -1 };
99 u_char plus_ee2irqmap[] =
100         { 3, 4, 5, 7, 9, 10, 11, 12 };
101
102 /* Network Interface Functions */
103 static void     ex_init(void *);
104 static void     ex_init_locked(struct ex_softc *);
105 static void     ex_start(struct ifnet *);
106 static void     ex_start_locked(struct ifnet *);
107 static int      ex_ioctl(struct ifnet *, u_long, caddr_t);
108 static void     ex_watchdog(void *);
109
110 /* ifmedia Functions    */
111 static int      ex_ifmedia_upd(struct ifnet *);
112 static void     ex_ifmedia_sts(struct ifnet *, struct ifmediareq *);
113
114 static int      ex_get_media(struct ex_softc *);
115
116 static void     ex_reset(struct ex_softc *);
117 static void     ex_setmulti(struct ex_softc *);
118
119 static void     ex_tx_intr(struct ex_softc *);
120 static void     ex_rx_intr(struct ex_softc *);
121
122 void
123 ex_get_address(struct ex_softc *sc, u_char *enaddr)
124 {
125         uint16_t        eaddr_tmp;
126
127         eaddr_tmp = ex_eeprom_read(sc, EE_Eth_Addr_Lo);
128         enaddr[5] = eaddr_tmp & 0xff;
129         enaddr[4] = eaddr_tmp >> 8;
130         eaddr_tmp = ex_eeprom_read(sc, EE_Eth_Addr_Mid);
131         enaddr[3] = eaddr_tmp & 0xff;
132         enaddr[2] = eaddr_tmp >> 8;
133         eaddr_tmp = ex_eeprom_read(sc, EE_Eth_Addr_Hi);
134         enaddr[1] = eaddr_tmp & 0xff;
135         enaddr[0] = eaddr_tmp >> 8;
136         
137         return;
138 }
139
140 int
141 ex_card_type(u_char *enaddr)
142 {
143         if ((enaddr[0] == 0x00) && (enaddr[1] == 0xA0) && (enaddr[2] == 0xC9))
144                 return (CARD_TYPE_EX_10_PLUS);
145
146         return (CARD_TYPE_EX_10);
147 }
148
149 /*
150  * Caller is responsible for eventually calling
151  * ex_release_resources() on failure.
152  */
153 int
154 ex_alloc_resources(device_t dev)
155 {
156         struct ex_softc *       sc = device_get_softc(dev);
157         int                     error = 0;
158
159         sc->ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
160                                             &sc->ioport_rid, RF_ACTIVE);
161         if (!sc->ioport) {
162                 device_printf(dev, "No I/O space?!\n");
163                 error = ENOMEM;
164                 goto bad;
165         }
166
167         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
168                                         RF_ACTIVE);
169
170         if (!sc->irq) {
171                 device_printf(dev, "No IRQ?!\n");
172                 error = ENOMEM;
173                 goto bad;
174         }
175
176 bad:
177         return (error);
178 }
179
180 void
181 ex_release_resources(device_t dev)
182 {
183         struct ex_softc *       sc = device_get_softc(dev);
184
185         if (sc->ih) {
186                 bus_teardown_intr(dev, sc->irq, sc->ih);
187                 sc->ih = NULL;
188         }
189
190         if (sc->ioport) {
191                 bus_release_resource(dev, SYS_RES_IOPORT,
192                                         sc->ioport_rid, sc->ioport);
193                 sc->ioport = NULL;
194         }
195
196         if (sc->irq) {
197                 bus_release_resource(dev, SYS_RES_IRQ,
198                                         sc->irq_rid, sc->irq);
199                 sc->irq = NULL;
200         }
201
202         if (sc->ifp)
203                 if_free(sc->ifp);
204
205         return;
206 }
207
208 int
209 ex_attach(device_t dev)
210 {
211         struct ex_softc *       sc = device_get_softc(dev);
212         struct ifnet *          ifp;
213         struct ifmedia *        ifm;
214         int                     error;
215         uint16_t                temp;
216
217         ifp = sc->ifp = if_alloc(IFT_ETHER);
218         if (ifp == NULL) {
219                 device_printf(dev, "can not if_alloc()\n");
220                 return (ENOSPC);
221         }
222         /* work out which set of irq <-> internal tables to use */
223         if (ex_card_type(sc->enaddr) == CARD_TYPE_EX_10_PLUS) {
224                 sc->irq2ee = plus_irq2eemap;
225                 sc->ee2irq = plus_ee2irqmap;
226         } else {
227                 sc->irq2ee = irq2eemap;
228                 sc->ee2irq = ee2irqmap;
229         }
230
231         sc->mem_size = CARD_RAM_SIZE;   /* XXX This should be read from the card itself. */
232
233         /*
234          * Initialize the ifnet structure.
235          */
236         ifp->if_softc = sc;
237         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
238         ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
239         ifp->if_start = ex_start;
240         ifp->if_ioctl = ex_ioctl;
241         ifp->if_init = ex_init;
242         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
243
244         ifmedia_init(&sc->ifmedia, 0, ex_ifmedia_upd, ex_ifmedia_sts);
245         mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
246             MTX_DEF);
247         callout_init_mtx(&sc->timer, &sc->lock, 0);
248
249         temp = ex_eeprom_read(sc, 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_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
258         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
259         ifmedia_set(&sc->ifmedia, ex_get_media(sc));
260
261         ifm = &sc->ifmedia;
262         ifm->ifm_media = ifm->ifm_cur->ifm_media;       
263         ex_ifmedia_upd(ifp);
264
265         /*
266          * Attach the interface.
267          */
268         ether_ifattach(ifp, sc->enaddr);
269
270         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
271                                 NULL, ex_intr, (void *)sc, &sc->ih);
272         if (error) {
273                 device_printf(dev, "bus_setup_intr() failed!\n");
274                 ether_ifdetach(ifp);
275                 mtx_destroy(&sc->lock);
276                 return (error);
277         }
278
279         return(0);
280 }
281
282 int
283 ex_detach(device_t dev)
284 {
285         struct ex_softc *sc;
286         struct ifnet    *ifp;
287
288         sc = device_get_softc(dev);
289         ifp = sc->ifp;
290
291         EX_LOCK(sc);
292         ex_stop(sc);
293         EX_UNLOCK(sc);
294
295         ether_ifdetach(ifp);
296         callout_drain(&sc->timer);
297
298         ex_release_resources(dev);
299         mtx_destroy(&sc->lock);
300
301         return (0);
302 }
303
304 static void
305 ex_init(void *xsc)
306 {
307         struct ex_softc *       sc = (struct ex_softc *) xsc;
308
309         EX_LOCK(sc);
310         ex_init_locked(sc);
311         EX_UNLOCK(sc);
312 }
313
314 static void
315 ex_init_locked(struct ex_softc *sc)
316 {
317         struct ifnet *          ifp = sc->ifp;
318         int                     i;
319         unsigned short          temp_reg;
320
321         DODEBUG(Start_End, printf("%s: ex_init: start\n", ifp->if_xname););
322
323         sc->tx_timeout = 0;
324
325         /*
326          * Load the ethernet address into the card.
327          */
328         CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
329         temp_reg = CSR_READ_1(sc, EEPROM_REG);
330         if (temp_reg & Trnoff_Enable)
331                 CSR_WRITE_1(sc, EEPROM_REG, temp_reg & ~Trnoff_Enable);
332         for (i = 0; i < ETHER_ADDR_LEN; i++)
333                 CSR_WRITE_1(sc, I_ADDR_REG0 + i, IF_LLADDR(sc->ifp)[i]);
334
335         /*
336          * - Setup transmit chaining and discard bad received frames.
337          * - Match broadcast.
338          * - Clear test mode.
339          * - Set receiving mode.
340          */
341         CSR_WRITE_1(sc, REG1, CSR_READ_1(sc, REG1) | Tx_Chn_Int_Md | Tx_Chn_ErStp | Disc_Bad_Fr);
342         CSR_WRITE_1(sc, REG2, CSR_READ_1(sc, REG2) | No_SA_Ins | RX_CRC_InMem);
343         CSR_WRITE_1(sc, REG3, CSR_READ_1(sc, REG3) & 0x3f /* XXX constants. */ );
344         /*
345          * - Set IRQ number, if this part has it.  ISA devices have this,
346          * while PC Card devices don't seem to.  Either way, we have to
347          * switch to Bank1 as the rest of this code relies on that.
348          */
349         CSR_WRITE_1(sc, CMD_REG, Bank1_Sel);
350         if (sc->flags & HAS_INT_NO_REG)
351                 CSR_WRITE_1(sc, INT_NO_REG,
352                     (CSR_READ_1(sc, INT_NO_REG) & 0xf8) |
353                     sc->irq2ee[sc->irq_no]);
354
355         /*
356          * Divide the available memory in the card into rcv and xmt buffers.
357          * By default, I use the first 3/4 of the memory for the rcv buffer,
358          * and the remaining 1/4 of the memory for the xmt buffer.
359          */
360         sc->rx_mem_size = sc->mem_size * 3 / 4;
361         sc->tx_mem_size = sc->mem_size - sc->rx_mem_size;
362         sc->rx_lower_limit = 0x0000;
363         sc->rx_upper_limit = sc->rx_mem_size - 2;
364         sc->tx_lower_limit = sc->rx_mem_size;
365         sc->tx_upper_limit = sc->mem_size - 2;
366         CSR_WRITE_1(sc, RCV_LOWER_LIMIT_REG, sc->rx_lower_limit >> 8);
367         CSR_WRITE_1(sc, RCV_UPPER_LIMIT_REG, sc->rx_upper_limit >> 8);
368         CSR_WRITE_1(sc, XMT_LOWER_LIMIT_REG, sc->tx_lower_limit >> 8);
369         CSR_WRITE_1(sc, XMT_UPPER_LIMIT_REG, sc->tx_upper_limit >> 8);
370         
371         /*
372          * Enable receive and transmit interrupts, and clear any pending int.
373          */
374         CSR_WRITE_1(sc, REG1, CSR_READ_1(sc, REG1) | TriST_INT);
375         CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
376         CSR_WRITE_1(sc, MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
377         CSR_WRITE_1(sc, STATUS_REG, All_Int);
378
379         /*
380          * Initialize receive and transmit ring buffers.
381          */
382         CSR_WRITE_2(sc, RCV_BAR, sc->rx_lower_limit);
383         sc->rx_head = sc->rx_lower_limit;
384         CSR_WRITE_2(sc, RCV_STOP_REG, sc->rx_upper_limit | 0xfe);
385         CSR_WRITE_2(sc, XMT_BAR, sc->tx_lower_limit);
386         sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
387
388         ifp->if_drv_flags |= IFF_DRV_RUNNING;
389         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
390         DODEBUG(Status, printf("OIDLE init\n"););
391         callout_reset(&sc->timer, hz, ex_watchdog, sc);
392         
393         ex_setmulti(sc);
394         
395         /*
396          * Final reset of the board, and enable operation.
397          */
398         CSR_WRITE_1(sc, CMD_REG, Sel_Reset_CMD);
399         DELAY(2);
400         CSR_WRITE_1(sc, CMD_REG, Rcv_Enable_CMD);
401
402         ex_start_locked(ifp);
403
404         DODEBUG(Start_End, printf("%s: ex_init: finish\n", ifp->if_xname););
405 }
406
407 static void
408 ex_start(struct ifnet *ifp)
409 {
410         struct ex_softc *       sc = ifp->if_softc;
411
412         EX_LOCK(sc);
413         ex_start_locked(ifp);
414         EX_UNLOCK(sc);
415 }
416
417 static void
418 ex_start_locked(struct ifnet *ifp)
419 {
420         struct ex_softc *       sc = ifp->if_softc;
421         int                     i, len, data_len, avail, dest, next;
422         unsigned char           tmp16[2];
423         struct mbuf *           opkt;
424         struct mbuf *           m;
425
426         DODEBUG(Start_End, printf("ex_start%d: start\n", unit););
427
428         /*
429          * Main loop: send outgoing packets to network card until there are no
430          * more packets left, or the card cannot accept any more yet.
431          */
432         while (((opkt = ifp->if_snd.ifq_head) != NULL) &&
433                !(ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
434
435                 /*
436                  * Ensure there is enough free transmit buffer space for
437                  * this packet, including its header. Note: the header
438                  * cannot wrap around the end of the transmit buffer and
439                  * must be kept together, so we allow space for twice the
440                  * length of the header, just in case.
441                  */
442
443                 for (len = 0, m = opkt; m != NULL; m = m->m_next) {
444                         len += m->m_len;
445                 }
446
447                 data_len = len;
448
449                 DODEBUG(Sent_Pkts, printf("1. Sending packet with %d data bytes. ", data_len););
450
451                 if (len & 1) {
452                         len += XMT_HEADER_LEN + 1;
453                 } else {
454                         len += XMT_HEADER_LEN;
455                 }
456
457                 if ((i = sc->tx_tail - sc->tx_head) >= 0) {
458                         avail = sc->tx_mem_size - i;
459                 } else {
460                         avail = -i;
461                 }
462
463                 DODEBUG(Sent_Pkts, printf("i=%d, avail=%d\n", i, avail););
464
465                 if (avail >= len + XMT_HEADER_LEN) {
466                         IF_DEQUEUE(&ifp->if_snd, opkt);
467
468 #ifdef EX_PSA_INTR      
469                         /*
470                          * Disable rx and tx interrupts, to avoid corruption
471                          * of the host address register by interrupt service
472                          * routines.
473                          * XXX Is this necessary with splimp() enabled?
474                          */
475                         CSR_WRITE_1(sc, MASK_REG, All_Int);
476 #endif
477
478                         /*
479                          * Compute the start and end addresses of this
480                          * frame in the tx buffer.
481                          */
482                         dest = sc->tx_tail;
483                         next = dest + len;
484
485                         if (next > sc->tx_upper_limit) {
486                                 if ((sc->tx_upper_limit + 2 - sc->tx_tail) <=
487                                     XMT_HEADER_LEN) {
488                                         dest = sc->tx_lower_limit;
489                                         next = dest + len;
490                                 } else {
491                                         next = sc->tx_lower_limit +
492                                                 next - sc->tx_upper_limit - 2;
493                                 }
494                         }
495
496                         /*
497                          * Build the packet frame in the card's ring buffer.
498                          */
499                         DODEBUG(Sent_Pkts, printf("2. dest=%d, next=%d. ", dest, next););
500
501                         CSR_WRITE_2(sc, HOST_ADDR_REG, dest);
502                         CSR_WRITE_2(sc, IO_PORT_REG, Transmit_CMD);
503                         CSR_WRITE_2(sc, IO_PORT_REG, 0);
504                         CSR_WRITE_2(sc, IO_PORT_REG, next);
505                         CSR_WRITE_2(sc, IO_PORT_REG, data_len);
506
507                         /*
508                          * Output the packet data to the card. Ensure all
509                          * transfers are 16-bit wide, even if individual
510                          * mbufs have odd length.
511                          */
512                         for (m = opkt, i = 0; m != NULL; m = m->m_next) {
513                                 DODEBUG(Sent_Pkts, printf("[%d]", m->m_len););
514                                 if (i) {
515                                         tmp16[1] = *(mtod(m, caddr_t));
516                                         CSR_WRITE_MULTI_2(sc, IO_PORT_REG,
517                                             (uint16_t *) tmp16, 1);
518                                 }
519                                 CSR_WRITE_MULTI_2(sc, IO_PORT_REG,
520                                     (uint16_t *) (mtod(m, caddr_t) + i),
521                                     (m->m_len - i) / 2);
522                                 if ((i = (m->m_len - i) & 1) != 0) {
523                                         tmp16[0] = *(mtod(m, caddr_t) +
524                                                    m->m_len - 1);
525                                 }
526                         }
527                         if (i)
528                                 CSR_WRITE_MULTI_2(sc, IO_PORT_REG, 
529                                     (uint16_t *) tmp16, 1);
530                         /*
531                          * If there were other frames chained, update the
532                          * chain in the last one.
533                          */
534                         if (sc->tx_head != sc->tx_tail) {
535                                 if (sc->tx_tail != dest) {
536                                         CSR_WRITE_2(sc, HOST_ADDR_REG,
537                                              sc->tx_last + XMT_Chain_Point);
538                                         CSR_WRITE_2(sc, IO_PORT_REG, dest);
539                                 }
540                                 CSR_WRITE_2(sc, HOST_ADDR_REG,
541                                      sc->tx_last + XMT_Byte_Count);
542                                 i = CSR_READ_2(sc, IO_PORT_REG);
543                                 CSR_WRITE_2(sc, HOST_ADDR_REG,
544                                      sc->tx_last + XMT_Byte_Count);
545                                 CSR_WRITE_2(sc, IO_PORT_REG, i | Ch_bit);
546                         }
547         
548                         /*
549                          * Resume normal operation of the card:
550                          * - Make a dummy read to flush the DRAM write
551                          *   pipeline.
552                          * - Enable receive and transmit interrupts.
553                          * - Send Transmit or Resume_XMT command, as
554                          *   appropriate.
555                          */
556                         CSR_READ_2(sc, IO_PORT_REG);
557 #ifdef EX_PSA_INTR
558                         CSR_WRITE_1(sc, MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
559 #endif
560                         if (sc->tx_head == sc->tx_tail) {
561                                 CSR_WRITE_2(sc, XMT_BAR, dest);
562                                 CSR_WRITE_1(sc, CMD_REG, Transmit_CMD);
563                                 sc->tx_head = dest;
564                                 DODEBUG(Sent_Pkts, printf("Transmit\n"););
565                         } else {
566                                 CSR_WRITE_1(sc, CMD_REG, Resume_XMT_List_CMD);
567                                 DODEBUG(Sent_Pkts, printf("Resume\n"););
568                         }
569         
570                         sc->tx_last = dest;
571                         sc->tx_tail = next;
572          
573                         BPF_MTAP(ifp, opkt);
574
575                         sc->tx_timeout = 2;
576                         if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
577                         m_freem(opkt);
578                 } else {
579                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
580                         DODEBUG(Status, printf("OACTIVE start\n"););
581                 }
582         }
583
584         DODEBUG(Start_End, printf("ex_start%d: finish\n", unit););
585 }
586
587 void
588 ex_stop(struct ex_softc *sc)
589 {
590         
591         DODEBUG(Start_End, printf("ex_stop%d: start\n", unit););
592
593         EX_ASSERT_LOCKED(sc);
594         /*
595          * Disable card operation:
596          * - Disable the interrupt line.
597          * - Flush transmission and disable reception.
598          * - Mask and clear all interrupts.
599          * - Reset the 82595.
600          */
601         CSR_WRITE_1(sc, CMD_REG, Bank1_Sel);
602         CSR_WRITE_1(sc, REG1, CSR_READ_1(sc, REG1) & ~TriST_INT);
603         CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
604         CSR_WRITE_1(sc, CMD_REG, Rcv_Stop);
605         sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
606         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. */
607         CSR_WRITE_1(sc, MASK_REG, All_Int);
608         CSR_WRITE_1(sc, STATUS_REG, All_Int);
609         CSR_WRITE_1(sc, CMD_REG, Reset_CMD);
610         DELAY(200);
611         sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
612         sc->tx_timeout = 0;
613         callout_stop(&sc->timer);
614
615         DODEBUG(Start_End, printf("ex_stop%d: finish\n", unit););
616
617         return;
618 }
619
620 void
621 ex_intr(void *arg)
622 {
623         struct ex_softc *sc = (struct ex_softc *)arg;
624         struct ifnet    *ifp = sc->ifp;
625         int             int_status, send_pkts;
626         int             loops = 100;
627
628         DODEBUG(Start_End, printf("ex_intr%d: start\n", unit););
629
630         EX_LOCK(sc);
631         send_pkts = 0;
632         while (loops-- > 0 &&
633             (int_status = CSR_READ_1(sc, STATUS_REG)) & (Tx_Int | Rx_Int)) {
634                 /* don't loop forever */
635                 if (int_status == 0xff)
636                         break;
637                 if (int_status & Rx_Int) {
638                         CSR_WRITE_1(sc, STATUS_REG, Rx_Int);
639                         ex_rx_intr(sc);
640                 } else if (int_status & Tx_Int) {
641                         CSR_WRITE_1(sc, STATUS_REG, Tx_Int);
642                         ex_tx_intr(sc);
643                         send_pkts = 1;
644                 }
645         }
646         if (loops == 0)
647                 printf("100 loops are not enough\n");
648
649         /*
650          * If any packet has been transmitted, and there are queued packets to
651          * be sent, attempt to send more packets to the network card.
652          */
653         if (send_pkts && (ifp->if_snd.ifq_head != NULL))
654                 ex_start_locked(ifp);
655         EX_UNLOCK(sc);
656
657         DODEBUG(Start_End, printf("ex_intr%d: finish\n", unit););
658
659         return;
660 }
661
662 static void
663 ex_tx_intr(struct ex_softc *sc)
664 {
665         struct ifnet *  ifp = sc->ifp;
666         int             tx_status;
667
668         DODEBUG(Start_End, printf("ex_tx_intr%d: start\n", unit););
669
670         /*
671          * - Cancel the watchdog.
672          * For all packets transmitted since last transmit interrupt:
673          * - Advance chain pointer to next queued packet.
674          * - Update statistics.
675          */
676
677         sc->tx_timeout = 0;
678
679         while (sc->tx_head != sc->tx_tail) {
680                 CSR_WRITE_2(sc, HOST_ADDR_REG, sc->tx_head);
681
682                 if (!(CSR_READ_2(sc, IO_PORT_REG) & Done_bit))
683                         break;
684
685                 tx_status = CSR_READ_2(sc, IO_PORT_REG);
686                 sc->tx_head = CSR_READ_2(sc, IO_PORT_REG);
687
688                 if (tx_status & TX_OK_bit) {
689                         if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
690                 } else {
691                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
692                 }
693
694                 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, tx_status & No_Collisions_bits);
695         }
696
697         /*
698          * The card should be ready to accept more packets now.
699          */
700
701         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
702
703         DODEBUG(Status, printf("OIDLE tx_intr\n"););
704         DODEBUG(Start_End, printf("ex_tx_intr%d: finish\n", unit););
705
706         return;
707 }
708
709 static void
710 ex_rx_intr(struct ex_softc *sc)
711 {
712         struct ifnet *          ifp = sc->ifp;
713         int                     rx_status;
714         int                     pkt_len;
715         int                     QQQ;
716         struct mbuf *           m;
717         struct mbuf *           ipkt;
718         struct ether_header *   eh;
719
720         DODEBUG(Start_End, printf("ex_rx_intr%d: start\n", unit););
721
722         /*
723          * For all packets received since last receive interrupt:
724          * - If packet ok, read it into a new mbuf and queue it to interface,
725          *   updating statistics.
726          * - If packet bad, just discard it, and update statistics.
727          * Finally, advance receive stop limit in card's memory to new location.
728          */
729
730         CSR_WRITE_2(sc, HOST_ADDR_REG, sc->rx_head);
731
732         while (CSR_READ_2(sc, IO_PORT_REG) == RCV_Done) {
733
734                 rx_status = CSR_READ_2(sc, IO_PORT_REG);
735                 sc->rx_head = CSR_READ_2(sc, IO_PORT_REG);
736                 QQQ = pkt_len = CSR_READ_2(sc, IO_PORT_REG);
737
738                 if (rx_status & RCV_OK_bit) {
739                         MGETHDR(m, M_NOWAIT, MT_DATA);
740                         ipkt = m;
741                         if (ipkt == NULL) {
742                                 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
743                         } else {
744                                 ipkt->m_pkthdr.rcvif = ifp;
745                                 ipkt->m_pkthdr.len = pkt_len;
746                                 ipkt->m_len = MHLEN;
747
748                                 while (pkt_len > 0) {
749                                         if (pkt_len >= MINCLSIZE) {
750                                                 if (MCLGET(m, M_NOWAIT)) {
751                                                         m->m_len = MCLBYTES;
752                                                 } else {
753                                                         m_freem(ipkt);
754                                                         if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
755                                                         goto rx_another;
756                                                 }
757                                         }
758                                         m->m_len = min(m->m_len, pkt_len);
759
760           /*
761            * NOTE: I'm assuming that all mbufs allocated are of even length,
762            * except for the last one in an odd-length packet.
763            */
764
765                                         CSR_READ_MULTI_2(sc, IO_PORT_REG,
766                                             mtod(m, uint16_t *), m->m_len / 2);
767
768                                         if (m->m_len & 1) {
769                                                 *(mtod(m, caddr_t) + m->m_len - 1) = CSR_READ_1(sc, IO_PORT_REG);
770                                         }
771                                         pkt_len -= m->m_len;
772
773                                         if (pkt_len > 0) {
774                                                 MGET(m->m_next, M_NOWAIT, MT_DATA);
775                                                 if (m->m_next == NULL) {
776                                                         m_freem(ipkt);
777                                                         if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
778                                                         goto rx_another;
779                                                 }
780                                                 m = m->m_next;
781                                                 m->m_len = MLEN;
782                                         }
783                                 }
784                                 eh = mtod(ipkt, struct ether_header *);
785 #ifdef EXDEBUG
786         if (debug_mask & Rcvd_Pkts) {
787                 if ((eh->ether_dhost[5] != 0xff) || (eh->ether_dhost[0] != 0xff)) {
788                         printf("Receive packet with %d data bytes: %6D -> ", QQQ, eh->ether_shost, ":");
789                         printf("%6D\n", eh->ether_dhost, ":");
790                 } /* QQQ */
791         }
792 #endif
793                                 EX_UNLOCK(sc);
794                                 (*ifp->if_input)(ifp, ipkt);
795                                 EX_LOCK(sc);
796                                 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
797                         }
798                 } else {
799                         if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
800                 }
801                 CSR_WRITE_2(sc, HOST_ADDR_REG, sc->rx_head);
802 rx_another: ;
803         }
804
805         if (sc->rx_head < sc->rx_lower_limit + 2)
806                 CSR_WRITE_2(sc, RCV_STOP_REG, sc->rx_upper_limit);
807         else
808                 CSR_WRITE_2(sc, RCV_STOP_REG, sc->rx_head - 2);
809
810         DODEBUG(Start_End, printf("ex_rx_intr%d: finish\n", unit););
811
812         return;
813 }
814
815
816 static int
817 ex_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
818 {
819         struct ex_softc *       sc = ifp->if_softc;
820         struct ifreq *          ifr = (struct ifreq *)data;
821         int                     error = 0;
822
823         DODEBUG(Start_End, printf("%s: ex_ioctl: start ", ifp->if_xname););
824
825         switch(cmd) {
826                 case SIOCSIFFLAGS:
827                         DODEBUG(Start_End, printf("SIOCSIFFLAGS"););
828                         EX_LOCK(sc);
829                         if ((ifp->if_flags & IFF_UP) == 0 &&
830                             (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
831                                 ex_stop(sc);
832                         } else {
833                                 ex_init_locked(sc);
834                         }
835                         EX_UNLOCK(sc);
836                         break;
837                 case SIOCADDMULTI:
838                 case SIOCDELMULTI:
839                         ex_init(sc);
840                         error = 0;
841                         break;
842                 case SIOCSIFMEDIA:
843                 case SIOCGIFMEDIA:
844                         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, cmd);
845                         break;
846                 default:
847                         error = ether_ioctl(ifp, cmd, data);
848                         break;
849         }
850
851         DODEBUG(Start_End, printf("\n%s: ex_ioctl: finish\n", ifp->if_xname););
852
853         return(error);
854 }
855
856 static void
857 ex_setmulti(struct ex_softc *sc)
858 {
859         struct ifnet *ifp;
860         struct ifmultiaddr *maddr;
861         uint16_t *addr;
862         int count;
863         int timeout, status;
864         
865         ifp = sc->ifp;
866
867         count = 0;
868         if_maddr_rlock(ifp);
869         TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
870                 if (maddr->ifma_addr->sa_family != AF_LINK)
871                         continue;
872                 count++;
873         }
874         if_maddr_runlock(ifp);
875
876         if ((ifp->if_flags & IFF_PROMISC) || (ifp->if_flags & IFF_ALLMULTI)
877                         || count > 63) {
878                 /* Interface is in promiscuous mode or there are too many
879                  * multicast addresses for the card to handle */
880                 CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
881                 CSR_WRITE_1(sc, REG2, CSR_READ_1(sc, REG2) | Promisc_Mode);
882                 CSR_WRITE_1(sc, REG3, CSR_READ_1(sc, REG3));
883                 CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
884         }
885         else if ((ifp->if_flags & IFF_MULTICAST) && (count > 0)) {
886                 /* Program multicast addresses plus our MAC address
887                  * into the filter */
888                 CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
889                 CSR_WRITE_1(sc, REG2, CSR_READ_1(sc, REG2) | Multi_IA);
890                 CSR_WRITE_1(sc, REG3, CSR_READ_1(sc, REG3));
891                 CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
892
893                 /* Borrow space from TX buffer; this should be safe
894                  * as this is only called from ex_init */
895                 
896                 CSR_WRITE_2(sc, HOST_ADDR_REG, sc->tx_lower_limit);
897                 CSR_WRITE_2(sc, IO_PORT_REG, MC_Setup_CMD);
898                 CSR_WRITE_2(sc, IO_PORT_REG, 0);
899                 CSR_WRITE_2(sc, IO_PORT_REG, 0);
900                 CSR_WRITE_2(sc, IO_PORT_REG, (count + 1) * 6);
901
902                 if_maddr_rlock(ifp);
903                 TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
904                         if (maddr->ifma_addr->sa_family != AF_LINK)
905                                 continue;
906
907                         addr = (uint16_t*)LLADDR((struct sockaddr_dl *)
908                                         maddr->ifma_addr);
909                         CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
910                         CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
911                         CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
912                 }
913                 if_maddr_runlock(ifp);
914
915                 /* Program our MAC address as well */
916                 /* XXX: Is this necessary?  The Linux driver does this
917                  * but the NetBSD driver does not */
918                 addr = (uint16_t*)IF_LLADDR(sc->ifp);
919                 CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
920                 CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
921                 CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
922
923                 CSR_READ_2(sc, IO_PORT_REG);
924                 CSR_WRITE_2(sc, XMT_BAR, sc->tx_lower_limit);
925                 CSR_WRITE_1(sc, CMD_REG, MC_Setup_CMD);
926
927                 sc->tx_head = sc->tx_lower_limit;
928                 sc->tx_tail = sc->tx_head + XMT_HEADER_LEN + (count + 1) * 6;
929
930                 for (timeout=0; timeout<100; timeout++) {
931                         DELAY(2);
932                         if ((CSR_READ_1(sc, STATUS_REG) & Exec_Int) == 0)
933                                 continue;
934
935                         status = CSR_READ_1(sc, CMD_REG);
936                         CSR_WRITE_1(sc, STATUS_REG, Exec_Int);
937                         break;
938                 }
939
940                 sc->tx_head = sc->tx_tail;
941         }
942         else
943         {
944                 /* No multicast or promiscuous mode */
945                 CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
946                 CSR_WRITE_1(sc, REG2, CSR_READ_1(sc, REG2) & 0xDE);
947                         /* ~(Multi_IA | Promisc_Mode) */
948                 CSR_WRITE_1(sc, REG3, CSR_READ_1(sc, REG3));
949                 CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
950         }
951 }
952
953 static void
954 ex_reset(struct ex_softc *sc)
955 {
956
957         DODEBUG(Start_End, printf("ex_reset%d: start\n", unit););
958
959         EX_ASSERT_LOCKED(sc);
960         ex_stop(sc);
961         ex_init_locked(sc);
962
963         DODEBUG(Start_End, printf("ex_reset%d: finish\n", unit););
964
965         return;
966 }
967
968 static void
969 ex_watchdog(void *arg)
970 {
971         struct ex_softc *       sc = arg;
972         struct ifnet *ifp = sc->ifp;
973
974         if (sc->tx_timeout && --sc->tx_timeout == 0) {
975                 DODEBUG(Start_End, if_printf(ifp, "ex_watchdog: start\n"););
976
977                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
978
979                 DODEBUG(Status, printf("OIDLE watchdog\n"););
980
981                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
982                 ex_reset(sc);
983                 ex_start_locked(ifp);
984
985                 DODEBUG(Start_End, if_printf(ifp, "ex_watchdog: finish\n"););
986         }
987
988         callout_reset(&sc->timer, hz, ex_watchdog, sc);
989 }
990
991 static int
992 ex_get_media(struct ex_softc *sc)
993 {
994         int     current;
995         int     media;
996
997         media = ex_eeprom_read(sc, EE_W5);
998
999         CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
1000         current = CSR_READ_1(sc, REG3);
1001         CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
1002
1003         if ((current & TPE_bit) && (media & EE_W5_PORT_TPE))
1004                 return(IFM_ETHER|IFM_10_T);
1005         if ((current & BNC_bit) && (media & EE_W5_PORT_BNC))
1006                 return(IFM_ETHER|IFM_10_2);
1007
1008         if (media & EE_W5_PORT_AUI)
1009                 return (IFM_ETHER|IFM_10_5);
1010
1011         return (IFM_ETHER|IFM_AUTO);
1012 }
1013
1014 static int
1015 ex_ifmedia_upd(ifp)
1016         struct ifnet *          ifp;
1017 {
1018         struct ex_softc *       sc = ifp->if_softc;
1019
1020         if (IFM_TYPE(sc->ifmedia.ifm_media) != IFM_ETHER)
1021                 return EINVAL;
1022
1023         return (0);
1024 }
1025
1026 static void
1027 ex_ifmedia_sts(ifp, ifmr)
1028         struct ifnet *          ifp;
1029         struct ifmediareq *     ifmr;
1030 {
1031         struct ex_softc *       sc = ifp->if_softc;
1032
1033         EX_LOCK(sc);
1034         ifmr->ifm_active = ex_get_media(sc);
1035         ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
1036         EX_UNLOCK(sc);
1037
1038         return;
1039 }
1040
1041 u_short
1042 ex_eeprom_read(struct ex_softc *sc, int location)
1043 {
1044         int i;
1045         u_short data = 0;
1046         int read_cmd = location | EE_READ_CMD;
1047         short ctrl_val = EECS;
1048
1049         CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
1050         CSR_WRITE_1(sc, EEPROM_REG, EECS);
1051         for (i = 8; i >= 0; i--) {
1052                 short outval = (read_cmd & (1 << i)) ? ctrl_val | EEDI : ctrl_val;
1053                 CSR_WRITE_1(sc, EEPROM_REG, outval);
1054                 CSR_WRITE_1(sc, EEPROM_REG, outval | EESK);
1055                 DELAY(3);
1056                 CSR_WRITE_1(sc, EEPROM_REG, outval);
1057                 DELAY(2);
1058         }
1059         CSR_WRITE_1(sc, EEPROM_REG, ctrl_val);
1060
1061         for (i = 16; i > 0; i--) {
1062                 CSR_WRITE_1(sc, EEPROM_REG, ctrl_val | EESK);
1063                 DELAY(3);
1064                 data = (data << 1) | 
1065                     ((CSR_READ_1(sc, EEPROM_REG) & EEDO) ? 1 : 0);
1066                 CSR_WRITE_1(sc, EEPROM_REG, ctrl_val);
1067                 DELAY(2);
1068         }
1069
1070         ctrl_val &= ~EECS;
1071         CSR_WRITE_1(sc, EEPROM_REG, ctrl_val | EESK);
1072         DELAY(3);
1073         CSR_WRITE_1(sc, EEPROM_REG, ctrl_val);
1074         DELAY(2);
1075         CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
1076         return(data);
1077 }