]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/dev/ed/if_ed.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / sys / dev / ed / if_ed.c
1 /*-
2  * Copyright (c) 1995, David Greenman
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /*
32  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
33  *   adapters. By David Greenman, 29-April-1993
34  *
35  * Currently supports the Western Digital/SMC 8003 and 8013 series,
36  *   the SMC Elite Ultra (8216), the 3Com 3c503, the NE1000 and NE2000,
37  *   and a variety of similar clones.
38  *
39  */
40
41 #include "opt_ed.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sockio.h>
46 #include <sys/mbuf.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/syslog.h>
50
51 #include <sys/bus.h>
52
53 #include <machine/bus.h>
54 #include <sys/rman.h>
55 #include <machine/resource.h>
56
57 #include <net/ethernet.h>
58 #include <net/if.h>
59 #include <net/if_arp.h>
60 #include <net/if_dl.h>
61 #include <net/if_mib.h>
62 #include <net/if_media.h>
63 #include <net/if_types.h>
64
65 #include <net/bpf.h>
66
67 #include <dev/ed/if_edreg.h>
68 #include <dev/ed/if_edvar.h>
69 #include <sys/kdb.h>
70
71 devclass_t ed_devclass;
72
73 static void     ed_init(void *);
74 static void     ed_init_locked(struct ed_softc *);
75 static int      ed_ioctl(struct ifnet *, u_long, caddr_t);
76 static void     ed_start(struct ifnet *);
77 static void     ed_start_locked(struct ifnet *);
78 static void     ed_reset(struct ifnet *);
79 static void     ed_watchdog(struct ifnet *);
80
81 static void     ed_ds_getmcaf(struct ed_softc *, uint32_t *);
82
83 static void     ed_get_packet(struct ed_softc *, bus_size_t, u_short);
84 static void     ed_stop_hw(struct ed_softc *sc);
85
86 static __inline void ed_rint(struct ed_softc *);
87 static __inline void ed_xmit(struct ed_softc *);
88 static __inline void ed_ring_copy(struct ed_softc *, bus_size_t, char *,
89     u_short);
90 static u_short  ed_pio_write_mbufs(struct ed_softc *, struct mbuf *,
91     bus_size_t);
92
93 static void     ed_setrcr(struct ed_softc *);
94
95 /*
96  * Generic probe routine for testing for the existance of a DS8390.
97  *      Must be called after the NIC has just been reset. This routine
98  *      works by looking at certain register values that are guaranteed
99  *      to be initialized a certain way after power-up or reset. Seems
100  *      not to currently work on the 83C690.
101  *
102  * Specifically:
103  *
104  *      Register                        reset bits      set bits
105  *      Command Register (CR)           TXP, STA        RD2, STP
106  *      Interrupt Status (ISR)                          RST
107  *      Interrupt Mask (IMR)            All bits
108  *      Data Control (DCR)                              LAS
109  *      Transmit Config. (TCR)          LB1, LB0
110  *
111  * We only look at the CR and ISR registers, however, because looking at
112  *      the others would require changing register pages (which would be
113  *      intrusive if this isn't an 8390).
114  *
115  * Return 1 if 8390 was found, 0 if not.
116  */
117
118 int
119 ed_probe_generic8390(struct ed_softc *sc)
120 {
121         if ((ed_nic_inb(sc, ED_P0_CR) &
122              (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
123             (ED_CR_RD2 | ED_CR_STP))
124                 return (0);
125         if ((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RST) != ED_ISR_RST)
126                 return (0);
127
128         return (1);
129 }
130
131 void
132 ed_disable_16bit_access(struct ed_softc *sc)
133 {
134         /*
135          * Disable 16 bit access to shared memory
136          */
137         if (sc->isa16bit && sc->vendor == ED_VENDOR_WD_SMC) {
138                 if (sc->chip_type == ED_CHIP_TYPE_WD790)
139                         ed_asic_outb(sc, ED_WD_MSR, 0x00);
140                 ed_asic_outb(sc, ED_WD_LAAR,
141                     sc->wd_laar_proto & ~ED_WD_LAAR_M16EN);
142         }
143 }
144
145 void
146 ed_enable_16bit_access(struct ed_softc *sc)
147 {
148         if (sc->isa16bit && sc->vendor == ED_VENDOR_WD_SMC) {
149                 ed_asic_outb(sc, ED_WD_LAAR,
150                      sc->wd_laar_proto | ED_WD_LAAR_M16EN);
151                 if (sc->chip_type == ED_CHIP_TYPE_WD790)
152                         ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_MENB);
153         }
154 }
155
156 /*
157  * Allocate a port resource with the given resource id.
158  */
159 int
160 ed_alloc_port(device_t dev, int rid, int size)
161 {
162         struct ed_softc *sc = device_get_softc(dev);
163         struct resource *res;
164
165         res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
166             0ul, ~0ul, size, RF_ACTIVE);
167         if (res) {
168                 sc->port_rid = rid;
169                 sc->port_res = res;
170                 sc->port_used = size;
171                 sc->port_bst = rman_get_bustag(res);
172                 sc->port_bsh = rman_get_bushandle(res);
173                 return (0);
174         }
175         return (ENOENT);
176 }
177
178 /*
179  * Allocate a memory resource with the given resource id.
180  */
181 int
182 ed_alloc_memory(device_t dev, int rid, int size)
183 {
184         struct ed_softc *sc = device_get_softc(dev);
185         struct resource *res;
186
187         res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
188             0ul, ~0ul, size, RF_ACTIVE);
189         if (res) {
190                 sc->mem_rid = rid;
191                 sc->mem_res = res;
192                 sc->mem_used = size;
193                 sc->mem_bst = rman_get_bustag(res);
194                 sc->mem_bsh = rman_get_bushandle(res);
195                 return (0);
196         }
197         return (ENOENT);
198 }
199
200 /*
201  * Allocate an irq resource with the given resource id.
202  */
203 int
204 ed_alloc_irq(device_t dev, int rid, int flags)
205 {
206         struct ed_softc *sc = device_get_softc(dev);
207         struct resource *res;
208
209         res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE | flags);
210         if (res) {
211                 sc->irq_rid = rid;
212                 sc->irq_res = res;
213                 return (0);
214         }
215         return (ENOENT);
216 }
217
218 /*
219  * Release all resources
220  */
221 void
222 ed_release_resources(device_t dev)
223 {
224         struct ed_softc *sc = device_get_softc(dev);
225
226         if (sc->port_res) {
227                 bus_release_resource(dev, SYS_RES_IOPORT,
228                     sc->port_rid, sc->port_res);
229                 sc->port_res = 0;
230         }
231         if (sc->mem_res) {
232                 bus_release_resource(dev, SYS_RES_MEMORY,
233                     sc->mem_rid, sc->mem_res);
234                 sc->mem_res = 0;
235         }
236         if (sc->irq_res) {
237                 bus_release_resource(dev, SYS_RES_IRQ,
238                     sc->irq_rid, sc->irq_res);
239                 sc->irq_res = 0;
240         }
241         if (sc->ifp)
242                 if_free(sc->ifp);
243 }
244
245 /*
246  * Install interface into kernel networking data structures
247  */
248 int
249 ed_attach(device_t dev)
250 {
251         struct ed_softc *sc = device_get_softc(dev);
252         struct ifnet *ifp;
253
254         sc->dev = dev;
255         ED_LOCK_INIT(sc);
256         ifp = sc->ifp = if_alloc(IFT_ETHER);
257         if (ifp == NULL) {
258                 device_printf(dev, "can not if_alloc()\n");
259                 ED_LOCK_DESTROY(sc);
260                 return (ENOSPC);
261         }
262
263         if (sc->readmem == NULL) {
264                 if (sc->mem_shared) {
265                         if (sc->isa16bit)
266                                 sc->readmem = ed_shmem_readmem16;
267                         else
268                                 sc->readmem = ed_shmem_readmem8;
269                 } else {
270                         sc->readmem = ed_pio_readmem;
271                 }
272         }
273         
274         callout_init_mtx(&sc->tick_ch, ED_MUTEX(sc), 0);
275         /*
276          * Set interface to stopped condition (reset)
277          */
278         ed_stop_hw(sc);
279
280         /*
281          * Initialize ifnet structure
282          */
283         ifp->if_softc = sc;
284         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
285         ifp->if_start = ed_start;
286         ifp->if_ioctl = ed_ioctl;
287         ifp->if_watchdog = ed_watchdog;
288         ifp->if_init = ed_init;
289         IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
290         ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
291         IFQ_SET_READY(&ifp->if_snd);
292         ifp->if_linkmib = &sc->mibdata;
293         ifp->if_linkmiblen = sizeof sc->mibdata;
294         /*
295          * XXX - should do a better job.
296          */
297         if (sc->chip_type == ED_CHIP_TYPE_WD790)
298                 sc->mibdata.dot3StatsEtherChipSet =
299                         DOT3CHIPSET(dot3VendorWesternDigital,
300                                     dot3ChipSetWesternDigital83C790);
301         else
302                 sc->mibdata.dot3StatsEtherChipSet =
303                         DOT3CHIPSET(dot3VendorNational, 
304                                     dot3ChipSetNational8390);
305         sc->mibdata.dot3Compliance = DOT3COMPLIANCE_COLLS;
306
307         /*
308          * Set default state for ALTPHYS flag (used to disable the 
309          * tranceiver for AUI operation), based on config option.
310          */
311         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
312         if (device_get_flags(dev) & ED_FLAGS_DISABLE_TRANCEIVER)
313                 ifp->if_flags |= IFF_ALTPHYS;
314
315         /*
316          * Attach the interface
317          */
318         ether_ifattach(ifp, sc->enaddr);
319         /* device attach does transition from UNCONFIGURED to IDLE state */
320
321         if (bootverbose || 1) {
322                 if (sc->type_str && (*sc->type_str != 0))
323                         device_printf(dev, "type %s ", sc->type_str);
324                 else
325                         device_printf(dev, "type unknown (0x%x) ", sc->type);
326
327 #ifdef ED_HPP
328                 if (sc->vendor == ED_VENDOR_HP)
329                         printf("(%s %s IO)",
330                             (sc->hpp_id & ED_HPP_ID_16_BIT_ACCESS) ?
331                             "16-bit" : "32-bit",
332                             sc->hpp_mem_start ? "memory mapped" : "regular");
333                 else
334 #endif
335                         printf("%s ", sc->isa16bit ? "(16 bit)" : "(8 bit)");
336
337 #if defined(ED_HPP) || defined(ED_3C503)
338                 printf("%s", (((sc->vendor == ED_VENDOR_3COM) ||
339                                     (sc->vendor == ED_VENDOR_HP)) &&
340                            (ifp->if_flags & IFF_ALTPHYS)) ?
341                     " tranceiver disabled" : "");
342 #endif
343                 printf("\n");
344         }
345         return (0);
346 }
347
348 /*
349  * Detach the driver from the hardware and other systems in the kernel.
350  */
351 int
352 ed_detach(device_t dev)
353 {
354         struct ed_softc *sc = device_get_softc(dev);
355         struct ifnet *ifp = sc->ifp;
356
357         ED_ASSERT_UNLOCKED(sc);
358         ED_LOCK(sc);
359         if (bus_child_present(dev))
360                 ed_stop(sc);
361         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
362         ED_UNLOCK(sc);
363         callout_drain(&sc->tick_ch);
364         ether_ifdetach(ifp);
365         bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
366         ed_release_resources(dev);
367         ED_LOCK_DESTROY(sc);
368         bus_generic_detach(dev);
369         return (0);
370 }
371
372 /*
373  * Reset interface.
374  */
375 static void
376 ed_reset(struct ifnet *ifp)
377 {
378         struct ed_softc *sc = ifp->if_softc;
379
380         ED_ASSERT_LOCKED(sc);
381         /*
382          * Stop interface and re-initialize.
383          */
384         ed_stop(sc);
385         ed_init_locked(sc);
386 }
387
388 static void
389 ed_stop_hw(struct ed_softc *sc)
390 {
391         int     n = 5000;
392
393         /*
394          * Stop everything on the interface, and select page 0 registers.
395          */
396         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
397
398         /*
399          * Wait for interface to enter stopped state, but limit # of checks to
400          * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but
401          * just in case it's an old one.
402          */
403         if (sc->chip_type != ED_CHIP_TYPE_AX88190)
404                 while (((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RST) == 0) && --n)
405                         continue;
406 }
407
408 /*
409  * Take interface offline.
410  */
411 void
412 ed_stop(struct ed_softc *sc)
413 {
414         ED_ASSERT_LOCKED(sc);
415         if (sc->sc_tick)
416                 callout_stop(&sc->tick_ch);
417         ed_stop_hw(sc);
418 }
419
420 /*
421  * Device timeout/watchdog routine. Entered if the device neglects to
422  *      generate an interrupt after a transmit has been started on it.
423  */
424 static void
425 ed_watchdog(struct ifnet *ifp)
426 {
427         struct ed_softc *sc = ifp->if_softc;
428
429         log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
430         ifp->if_oerrors++;
431
432         ED_LOCK(sc);
433         ed_reset(ifp);
434         ED_UNLOCK(sc);
435 }
436
437 /*
438  * Initialize device.
439  */
440 static void
441 ed_init(void *xsc)
442 {
443         struct ed_softc *sc = xsc;
444
445         ED_ASSERT_UNLOCKED(sc);
446         ED_LOCK(sc);
447         ed_init_locked(sc);
448         ED_UNLOCK(sc);
449 }
450
451 static void
452 ed_init_locked(struct ed_softc *sc)
453 {
454         struct ifnet *ifp = sc->ifp;
455         int     i;
456
457         ED_ASSERT_LOCKED(sc);
458
459         /*
460          * Initialize the NIC in the exact order outlined in the NS manual.
461          * This init procedure is "mandatory"...don't change what or when
462          * things happen.
463          */
464
465         /* reset transmitter flags */
466         sc->xmit_busy = 0;
467         ifp->if_timer = 0;
468
469         sc->txb_inuse = 0;
470         sc->txb_new = 0;
471         sc->txb_next_tx = 0;
472
473         /* This variable is used below - don't move this assignment */
474         sc->next_packet = sc->rec_page_start + 1;
475
476         /*
477          * Set interface for page 0, Remote DMA complete, Stopped
478          */
479         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
480
481         if (sc->isa16bit)
482                 /*
483                  * Set FIFO threshold to 8, No auto-init Remote DMA, byte
484                  * order=80x86, word-wide DMA xfers,
485                  */
486                 ed_nic_outb(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
487         else
488                 /*
489                  * Same as above, but byte-wide DMA xfers
490                  */
491                 ed_nic_outb(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
492
493         /*
494          * Clear Remote Byte Count Registers
495          */
496         ed_nic_outb(sc, ED_P0_RBCR0, 0);
497         ed_nic_outb(sc, ED_P0_RBCR1, 0);
498
499         /*
500          * For the moment, don't store incoming packets in memory.
501          */
502         ed_nic_outb(sc, ED_P0_RCR, ED_RCR_MON);
503
504         /*
505          * Place NIC in internal loopback mode
506          */
507         ed_nic_outb(sc, ED_P0_TCR, ED_TCR_LB0);
508
509         /*
510          * Initialize transmit/receive (ring-buffer) Page Start
511          */
512         ed_nic_outb(sc, ED_P0_TPSR, sc->tx_page_start);
513         ed_nic_outb(sc, ED_P0_PSTART, sc->rec_page_start);
514         /* Set lower bits of byte addressable framing to 0 */
515         if (sc->chip_type == ED_CHIP_TYPE_WD790)
516                 ed_nic_outb(sc, 0x09, 0);
517
518         /*
519          * Initialize Receiver (ring-buffer) Page Stop and Boundry
520          */
521         ed_nic_outb(sc, ED_P0_PSTOP, sc->rec_page_stop);
522         ed_nic_outb(sc, ED_P0_BNRY, sc->rec_page_start);
523
524         /*
525          * Clear all interrupts. A '1' in each bit position clears the
526          * corresponding flag.
527          */
528         ed_nic_outb(sc, ED_P0_ISR, 0xff);
529
530         /*
531          * Enable the following interrupts: receive/transmit complete,
532          * receive/transmit error, and Receiver OverWrite.
533          *
534          * Counter overflow and Remote DMA complete are *not* enabled.
535          */
536         ed_nic_outb(sc, ED_P0_IMR,
537         ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE | ED_IMR_OVWE);
538
539         /*
540          * Program Command Register for page 1
541          */
542         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
543
544         /*
545          * Copy out our station address
546          */
547         for (i = 0; i < ETHER_ADDR_LEN; ++i)
548                 ed_nic_outb(sc, ED_P1_PAR(i), IFP2ENADDR(sc->ifp)[i]);
549
550         /*
551          * Set Current Page pointer to next_packet (initialized above)
552          */
553         ed_nic_outb(sc, ED_P1_CURR, sc->next_packet);
554
555         /*
556          * Program Receiver Configuration Register and multicast filter. CR is
557          * set to page 0 on return.
558          */
559         ed_setrcr(sc);
560
561         /*
562          * Take interface out of loopback
563          */
564         ed_nic_outb(sc, ED_P0_TCR, 0);
565
566 #ifdef ED_3C503
567         /*
568          * If this is a 3Com board, the tranceiver must be software enabled
569          * (there is no settable hardware default).
570          */
571         if (sc->vendor == ED_VENDOR_3COM) {
572                 if (ifp->if_flags & IFF_ALTPHYS)
573                         ed_asic_outb(sc, ED_3COM_CR, 0);
574                 else
575                         ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_XSEL);
576         }
577 #endif
578         if (sc->sc_mediachg)
579             sc->sc_mediachg(sc);
580
581         /*
582          * Set 'running' flag, and clear output active flag.
583          */
584         ifp->if_drv_flags |= IFF_DRV_RUNNING;
585         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
586
587         /*
588          * ...and attempt to start output
589          */
590         ed_start_locked(ifp);
591
592         if (sc->sc_tick)
593                 callout_reset(&sc->tick_ch, hz, sc->sc_tick, sc);
594 }
595
596 /*
597  * This routine actually starts the transmission on the interface
598  */
599 static __inline void
600 ed_xmit(struct ed_softc *sc)
601 {
602         struct ifnet *ifp = sc->ifp;
603         unsigned short len;
604
605         len = sc->txb_len[sc->txb_next_tx];
606
607         /*
608          * Set NIC for page 0 register access
609          */
610         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
611
612         /*
613          * Set TX buffer start page
614          */
615         ed_nic_outb(sc, ED_P0_TPSR, sc->tx_page_start +
616                     sc->txb_next_tx * ED_TXBUF_SIZE);
617
618         /*
619          * Set TX length
620          */
621         ed_nic_outb(sc, ED_P0_TBCR0, len);
622         ed_nic_outb(sc, ED_P0_TBCR1, len >> 8);
623
624         /*
625          * Set page 0, Remote DMA complete, Transmit Packet, and *Start*
626          */
627         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_TXP | ED_CR_STA);
628         sc->xmit_busy = 1;
629
630         /*
631          * Point to next transmit buffer slot and wrap if necessary.
632          */
633         sc->txb_next_tx++;
634         if (sc->txb_next_tx == sc->txb_cnt)
635                 sc->txb_next_tx = 0;
636
637         /*
638          * Set a timer just in case we never hear from the board again
639          */
640         ifp->if_timer = 2;
641 }
642
643 /*
644  * Start output on interface.
645  * We make two assumptions here:
646  *  1) that the current priority is set to splimp _before_ this code
647  *     is called *and* is returned to the appropriate priority after
648  *     return
649  *  2) that the IFF_DRV_OACTIVE flag is checked before this code is called
650  *     (i.e. that the output part of the interface is idle)
651  */
652 static void
653 ed_start(struct ifnet *ifp)
654 {
655         struct ed_softc *sc = ifp->if_softc;
656
657         ED_ASSERT_UNLOCKED(sc);
658         ED_LOCK(sc);
659         ed_start_locked(ifp);
660         ED_UNLOCK(sc);
661 }
662
663 static void
664 ed_start_locked(struct ifnet *ifp)
665 {
666         struct ed_softc *sc = ifp->if_softc;
667         struct mbuf *m0, *m;
668         bus_size_t buffer;
669         int     len;
670
671         ED_ASSERT_LOCKED(sc);
672 outloop:
673
674         /*
675          * First, see if there are buffered packets and an idle transmitter -
676          * should never happen at this point.
677          */
678         if (sc->txb_inuse && (sc->xmit_busy == 0)) {
679                 printf("ed: packets buffered, but transmitter idle\n");
680                 ed_xmit(sc);
681         }
682
683         /*
684          * See if there is room to put another packet in the buffer.
685          */
686         if (sc->txb_inuse == sc->txb_cnt) {
687
688                 /*
689                  * No room. Indicate this to the outside world and exit.
690                  */
691                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
692                 return;
693         }
694         IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
695         if (m == 0) {
696
697                 /*
698                  * We are using the !OACTIVE flag to indicate to the outside
699                  * world that we can accept an additional packet rather than
700                  * that the transmitter is _actually_ active. Indeed, the
701                  * transmitter may be active, but if we haven't filled all the
702                  * buffers with data then we still want to accept more.
703                  */
704                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
705                 return;
706         }
707
708         /*
709          * Copy the mbuf chain into the transmit buffer
710          */
711         m0 = m;
712
713         /* txb_new points to next open buffer slot */
714         buffer = sc->mem_start + (sc->txb_new * ED_TXBUF_SIZE * ED_PAGE_SIZE);
715
716         if (sc->mem_shared) {
717                 /*
718                  * Special case setup for 16 bit boards...
719                  */
720                 if (sc->isa16bit) {
721                         switch (sc->vendor) {
722 #ifdef ED_3C503
723                                 /*
724                                  * For 16bit 3Com boards (which have 16k of
725                                  * memory), we have the xmit buffers in a
726                                  * different page of memory ('page 0') - so
727                                  * change pages.
728                                  */
729                         case ED_VENDOR_3COM:
730                                 ed_asic_outb(sc, ED_3COM_GACFR,
731                                              ED_3COM_GACFR_RSEL);
732                                 break;
733 #endif
734                                 /*
735                                  * Enable 16bit access to shared memory on
736                                  * WD/SMC boards.
737                                  *
738                                  * XXX - same as ed_enable_16bit_access()
739                                  */
740                         case ED_VENDOR_WD_SMC:
741                                 ed_asic_outb(sc, ED_WD_LAAR,
742                                     sc->wd_laar_proto | ED_WD_LAAR_M16EN);
743                                 if (sc->chip_type == ED_CHIP_TYPE_WD790)
744                                         ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_MENB);
745                                 break;
746                         }
747                 }
748                 for (len = 0; m != 0; m = m->m_next) {
749                         if (sc->isa16bit)
750                                 bus_space_write_region_2(sc->mem_bst,
751                                     sc->mem_bsh, buffer,
752                                     mtod(m, uint16_t *), (m->m_len + 1)/ 2);
753                         else
754                                 bus_space_write_region_1(sc->mem_bst,
755                                     sc->mem_bsh, buffer,
756                                     mtod(m, uint8_t *), m->m_len);
757                         buffer += m->m_len;
758                         len += m->m_len;
759                 }
760
761                 /*
762                  * Restore previous shared memory access
763                  */
764                 if (sc->isa16bit) {
765                         switch (sc->vendor) {
766 #ifdef ED_3C503
767                         case ED_VENDOR_3COM:
768                                 ed_asic_outb(sc, ED_3COM_GACFR,
769                                     ED_3COM_GACFR_RSEL | ED_3COM_GACFR_MBS0);
770                                 break;
771 #endif
772                         case ED_VENDOR_WD_SMC:
773                                 /* XXX - same as ed_disable_16bit_access() */
774                                 if (sc->chip_type == ED_CHIP_TYPE_WD790)
775                                         ed_asic_outb(sc, ED_WD_MSR, 0x00);
776                                 ed_asic_outb(sc, ED_WD_LAAR,
777                                     sc->wd_laar_proto & ~ED_WD_LAAR_M16EN);
778                                 break;
779                         }
780                 }
781         } else {
782                 len = ed_pio_write_mbufs(sc, m, buffer);
783                 if (len == 0) {
784                         m_freem(m0);
785                         goto outloop;
786                 }
787         }
788
789         sc->txb_len[sc->txb_new] = max(len, (ETHER_MIN_LEN-ETHER_CRC_LEN));
790
791         sc->txb_inuse++;
792
793         /*
794          * Point to next buffer slot and wrap if necessary.
795          */
796         sc->txb_new++;
797         if (sc->txb_new == sc->txb_cnt)
798                 sc->txb_new = 0;
799
800         if (sc->xmit_busy == 0)
801                 ed_xmit(sc);
802
803         /*
804          * Tap off here if there is a bpf listener.
805          */
806         BPF_MTAP(ifp, m0);
807
808         m_freem(m0);
809
810         /*
811          * Loop back to the top to possibly buffer more packets
812          */
813         goto outloop;
814 }
815
816 /*
817  * Ethernet interface receiver interrupt.
818  */
819 static __inline void
820 ed_rint(struct ed_softc *sc)
821 {
822         struct ifnet *ifp = sc->ifp;
823         u_char  boundry;
824         u_short len;
825         struct ed_ring packet_hdr;
826         bus_size_t packet_ptr;
827
828         ED_ASSERT_LOCKED(sc);
829
830         /*
831          * Set NIC to page 1 registers to get 'current' pointer
832          */
833         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
834
835         /*
836          * 'sc->next_packet' is the logical beginning of the ring-buffer -
837          * i.e. it points to where new data has been buffered. The 'CURR'
838          * (current) register points to the logical end of the ring-buffer -
839          * i.e. it points to where additional new data will be added. We loop
840          * here until the logical beginning equals the logical end (or in
841          * other words, until the ring-buffer is empty).
842          */
843         while (sc->next_packet != ed_nic_inb(sc, ED_P1_CURR)) {
844
845                 /* get pointer to this buffer's header structure */
846                 packet_ptr = sc->mem_ring +
847                     (sc->next_packet - sc->rec_page_start) * ED_PAGE_SIZE;
848         
849                 /*
850                  * The byte count includes a 4 byte header that was added by
851                  * the NIC.
852                  */
853                 sc->readmem(sc, packet_ptr, (char *) &packet_hdr,
854                     sizeof(packet_hdr));
855                 len = packet_hdr.count;
856                 if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN + sizeof(struct ed_ring)) ||
857                     len < (ETHER_MIN_LEN - ETHER_CRC_LEN + sizeof(struct ed_ring))) {
858                         /*
859                          * Length is a wild value. There's a good chance that
860                          * this was caused by the NIC being old and buggy.
861                          * The bug is that the length low byte is duplicated in
862                          * the high byte. Try to recalculate the length based on
863                          * the pointer to the next packet.
864                          */
865                         /*
866                          * NOTE: sc->next_packet is pointing at the current packet.
867                          */
868                         len &= ED_PAGE_SIZE - 1;        /* preserve offset into page */
869                         if (packet_hdr.next_packet >= sc->next_packet)
870                                 len += (packet_hdr.next_packet -
871                                     sc->next_packet) * ED_PAGE_SIZE;
872                         else
873                                 len += 
874                                     ((packet_hdr.next_packet - sc->rec_page_start) +
875                                     (sc->rec_page_stop - sc->next_packet)) * ED_PAGE_SIZE;
876                         /*
877                          * because buffers are aligned on 256-byte boundary,
878                          * the length computed above is off by 256 in almost
879                          * all cases. Fix it...
880                          */
881                         if (len & 0xff)
882                                 len -= 256;
883                         if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN 
884                             + sizeof(struct ed_ring)))
885                                 sc->mibdata.dot3StatsFrameTooLongs++;
886                 }
887
888                 /*
889                  * Be fairly liberal about what we allow as a "reasonable" length
890                  * so that a [crufty] packet will make it to BPF (and can thus
891                  * be analyzed). Note that all that is really important is that
892                  * we have a length that will fit into one mbuf cluster or less;
893                  * the upper layer protocols can then figure out the length from
894                  * their own length field(s).
895                  * But make sure that we have at least a full ethernet header
896                  * or we would be unable to call ether_input() later.
897                  */
898                 if ((len >= sizeof(struct ed_ring) + ETHER_HDR_LEN) &&
899                     (len <= MCLBYTES) &&
900                     (packet_hdr.next_packet >= sc->rec_page_start) &&
901                     (packet_hdr.next_packet < sc->rec_page_stop)) {
902                         /*
903                          * Go get packet.
904                          */
905                         ed_get_packet(sc, packet_ptr + sizeof(struct ed_ring),
906                                       len - sizeof(struct ed_ring));
907                         ifp->if_ipackets++;
908                 } else {
909                         /*
910                          * Really BAD. The ring pointers are corrupted.
911                          */
912                         log(LOG_ERR,
913                             "%s: NIC memory corrupt - invalid packet length %d\n",
914                             ifp->if_xname, len);
915                         ifp->if_ierrors++;
916                         ed_reset(ifp);
917                         return;
918                 }
919
920                 /*
921                  * Update next packet pointer
922                  */
923                 sc->next_packet = packet_hdr.next_packet;
924
925                 /*
926                  * Update NIC boundry pointer - being careful to keep it one
927                  * buffer behind. (as recommended by NS databook)
928                  */
929                 boundry = sc->next_packet - 1;
930                 if (boundry < sc->rec_page_start)
931                         boundry = sc->rec_page_stop - 1;
932
933                 /*
934                  * Set NIC to page 0 registers to update boundry register
935                  */
936                 ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
937                 ed_nic_outb(sc, ED_P0_BNRY, boundry);
938
939                 /*
940                  * Set NIC to page 1 registers before looping to top (prepare
941                  * to get 'CURR' current pointer)
942                  */
943                 ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
944         }
945 }
946
947 /*
948  * Ethernet interface interrupt processor
949  */
950 void
951 edintr(void *arg)
952 {
953         struct ed_softc *sc = (struct ed_softc*) arg;
954         struct ifnet *ifp = sc->ifp;
955         u_char  isr;
956         int     count;
957
958         ED_LOCK(sc);
959         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
960                 ED_UNLOCK(sc);
961                 return;
962         }
963         /*
964          * Set NIC to page 0 registers
965          */
966         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
967
968         /*
969          * loop until there are no more new interrupts.  When the card
970          * goes away, the hardware will read back 0xff.  Looking at
971          * the interrupts, it would appear that 0xff is impossible,
972          * or at least extremely unlikely.
973          */
974         while ((isr = ed_nic_inb(sc, ED_P0_ISR)) != 0 && isr != 0xff) {
975
976                 /*
977                  * reset all the bits that we are 'acknowledging' by writing a
978                  * '1' to each bit position that was set (writing a '1'
979                  * *clears* the bit)
980                  */
981                 ed_nic_outb(sc, ED_P0_ISR, isr);
982
983                 /* 
984                  * XXX workaround for AX88190
985                  * We limit this to 5000 iterations.  At 1us per inb/outb,
986                  * this translates to about 15ms, which should be plenty
987                  * of time, and also gives protection in the card eject
988                  * case.
989                  */
990                 if (sc->chip_type == ED_CHIP_TYPE_AX88190) {
991                         count = 5000;           /* 15ms */
992                         while (count-- && (ed_nic_inb(sc, ED_P0_ISR) & isr)) {
993                                 ed_nic_outb(sc, ED_P0_ISR,0);
994                                 ed_nic_outb(sc, ED_P0_ISR,isr);
995                         }
996                         if (count == 0)
997                                 break;
998                 }
999
1000                 /*
1001                  * Handle transmitter interrupts. Handle these first because
1002                  * the receiver will reset the board under some conditions.
1003                  */
1004                 if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
1005                         u_char  collisions = ed_nic_inb(sc, ED_P0_NCR) & 0x0f;
1006
1007                         /*
1008                          * Check for transmit error. If a TX completed with an
1009                          * error, we end up throwing the packet away. Really
1010                          * the only error that is possible is excessive
1011                          * collisions, and in this case it is best to allow
1012                          * the automatic mechanisms of TCP to backoff the
1013                          * flow. Of course, with UDP we're screwed, but this
1014                          * is expected when a network is heavily loaded.
1015                          */
1016                         (void) ed_nic_inb(sc, ED_P0_TSR);
1017                         if (isr & ED_ISR_TXE) {
1018                                 u_char tsr;
1019
1020                                 /*
1021                                  * Excessive collisions (16)
1022                                  */
1023                                 tsr = ed_nic_inb(sc, ED_P0_TSR);
1024                                 if ((tsr & ED_TSR_ABT)  
1025                                     && (collisions == 0)) {
1026
1027                                         /*
1028                                          * When collisions total 16, the
1029                                          * P0_NCR will indicate 0, and the
1030                                          * TSR_ABT is set.
1031                                          */
1032                                         collisions = 16;
1033                                         sc->mibdata.dot3StatsExcessiveCollisions++;
1034                                         sc->mibdata.dot3StatsCollFrequencies[15]++;
1035                                 }
1036                                 if (tsr & ED_TSR_OWC)
1037                                         sc->mibdata.dot3StatsLateCollisions++;
1038                                 if (tsr & ED_TSR_CDH)
1039                                         sc->mibdata.dot3StatsSQETestErrors++;
1040                                 if (tsr & ED_TSR_CRS)
1041                                         sc->mibdata.dot3StatsCarrierSenseErrors++;
1042                                 if (tsr & ED_TSR_FU)
1043                                         sc->mibdata.dot3StatsInternalMacTransmitErrors++;
1044
1045                                 /*
1046                                  * update output errors counter
1047                                  */
1048                                 ifp->if_oerrors++;
1049                         } else {
1050
1051                                 /*
1052                                  * Update total number of successfully
1053                                  * transmitted packets.
1054                                  */
1055                                 ifp->if_opackets++;
1056                         }
1057
1058                         /*
1059                          * reset tx busy and output active flags
1060                          */
1061                         sc->xmit_busy = 0;
1062                         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1063
1064                         /*
1065                          * clear watchdog timer
1066                          */
1067                         ifp->if_timer = 0;
1068
1069                         /*
1070                          * Add in total number of collisions on last
1071                          * transmission.
1072                          */
1073                         ifp->if_collisions += collisions;
1074                         switch(collisions) {
1075                         case 0:
1076                         case 16:
1077                                 break;
1078                         case 1:
1079                                 sc->mibdata.dot3StatsSingleCollisionFrames++;
1080                                 sc->mibdata.dot3StatsCollFrequencies[0]++;
1081                                 break;
1082                         default:
1083                                 sc->mibdata.dot3StatsMultipleCollisionFrames++;
1084                                 sc->mibdata.
1085                                         dot3StatsCollFrequencies[collisions-1]
1086                                                 ++;
1087                                 break;
1088                         }
1089
1090                         /*
1091                          * Decrement buffer in-use count if not zero (can only
1092                          * be zero if a transmitter interrupt occured while
1093                          * not actually transmitting). If data is ready to
1094                          * transmit, start it transmitting, otherwise defer
1095                          * until after handling receiver
1096                          */
1097                         if (sc->txb_inuse && --sc->txb_inuse)
1098                                 ed_xmit(sc);
1099                 }
1100
1101                 /*
1102                  * Handle receiver interrupts
1103                  */
1104                 if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
1105
1106                         /*
1107                          * Overwrite warning. In order to make sure that a
1108                          * lockup of the local DMA hasn't occurred, we reset
1109                          * and re-init the NIC. The NSC manual suggests only a
1110                          * partial reset/re-init is necessary - but some chips
1111                          * seem to want more. The DMA lockup has been seen
1112                          * only with early rev chips - Methinks this bug was
1113                          * fixed in later revs. -DG
1114                          */
1115                         if (isr & ED_ISR_OVW) {
1116                                 ifp->if_ierrors++;
1117 #ifdef DIAGNOSTIC
1118                                 log(LOG_WARNING,
1119                                     "%s: warning - receiver ring buffer overrun\n",
1120                                     ifp->if_xname);
1121 #endif
1122
1123                                 /*
1124                                  * Stop/reset/re-init NIC
1125                                  */
1126                                 ed_reset(ifp);
1127                         } else {
1128
1129                                 /*
1130                                  * Receiver Error. One or more of: CRC error,
1131                                  * frame alignment error FIFO overrun, or
1132                                  * missed packet.
1133                                  */
1134                                 if (isr & ED_ISR_RXE) {
1135                                         u_char rsr;
1136                                         rsr = ed_nic_inb(sc, ED_P0_RSR);
1137                                         if (rsr & ED_RSR_CRC)
1138                                                 sc->mibdata.dot3StatsFCSErrors++;
1139                                         if (rsr & ED_RSR_FAE)
1140                                                 sc->mibdata.dot3StatsAlignmentErrors++;
1141                                         if (rsr & ED_RSR_FO)
1142                                                 sc->mibdata.dot3StatsInternalMacReceiveErrors++;
1143                                         ifp->if_ierrors++;
1144 #ifdef ED_DEBUG
1145                                         if_printf(ifp, "receive error %x\n",
1146                                                ed_nic_inb(sc, ED_P0_RSR));
1147 #endif
1148                                 }
1149
1150                                 /*
1151                                  * Go get the packet(s) XXX - Doing this on an
1152                                  * error is dubious because there shouldn't be
1153                                  * any data to get (we've configured the
1154                                  * interface to not accept packets with
1155                                  * errors).
1156                                  */
1157
1158                                 /*
1159                                  * Enable 16bit access to shared memory first
1160                                  * on WD/SMC boards.
1161                                  */
1162                                 ed_enable_16bit_access(sc);
1163                                 ed_rint(sc);
1164                                 ed_disable_16bit_access(sc);
1165                         }
1166                 }
1167
1168                 /*
1169                  * If it looks like the transmitter can take more data,
1170                  * attempt to start output on the interface. This is done
1171                  * after handling the receiver to give the receiver priority.
1172                  */
1173                 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
1174                         ed_start_locked(ifp);
1175
1176                 /*
1177                  * return NIC CR to standard state: page 0, remote DMA
1178                  * complete, start (toggling the TXP bit off, even if was just
1179                  * set in the transmit routine, is *okay* - it is 'edge'
1180                  * triggered from low to high)
1181                  */
1182                 ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
1183
1184                 /*
1185                  * If the Network Talley Counters overflow, read them to reset
1186                  * them. It appears that old 8390's won't clear the ISR flag
1187                  * otherwise - resulting in an infinite loop.
1188                  */
1189                 if (isr & ED_ISR_CNT) {
1190                         (void) ed_nic_inb(sc, ED_P0_CNTR0);
1191                         (void) ed_nic_inb(sc, ED_P0_CNTR1);
1192                         (void) ed_nic_inb(sc, ED_P0_CNTR2);
1193                 }
1194         }
1195         ED_UNLOCK(sc);
1196 }
1197
1198 /*
1199  * Process an ioctl request.
1200  */
1201 static int
1202 ed_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1203 {
1204         struct ed_softc *sc = ifp->if_softc;
1205         struct ifreq *ifr = (struct ifreq *)data;
1206         int     error = 0;
1207
1208         /*
1209          * XXX really needed?
1210          */
1211         if (sc == NULL) {
1212                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1213                 return (ENXIO);
1214         }
1215
1216         switch (command) {
1217         case SIOCSIFFLAGS:
1218                 /*
1219                  * If the interface is marked up and stopped, then start it.
1220                  * If it is marked down and running, then stop it.
1221                  */
1222                 ED_LOCK(sc);
1223                 if (ifp->if_flags & IFF_UP) {
1224                         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1225                                 ed_init_locked(sc);
1226                 } else {
1227                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1228                                 ed_stop(sc);
1229                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1230                         }
1231                 }
1232
1233                 /*
1234                  * Promiscuous flag may have changed, so reprogram the RCR.
1235                  */
1236                 ed_setrcr(sc);
1237
1238                 /*
1239                  * An unfortunate hack to provide the (required) software
1240                  * control of the tranceiver for 3Com/HP boards.
1241                  * The ALTPHYS flag disables the tranceiver if set.
1242                  */
1243 #ifdef ED_3C503
1244                 if (sc->vendor == ED_VENDOR_3COM) {
1245                         if (ifp->if_flags & IFF_ALTPHYS)
1246                                 ed_asic_outb(sc, ED_3COM_CR, 0);
1247                         else
1248                                 ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_XSEL);
1249                 }
1250 #endif
1251 #ifdef ED_HPP
1252                 if (sc->vendor == ED_VENDOR_HP) 
1253                         ed_hpp_set_physical_link(sc);
1254 #endif
1255                 ED_UNLOCK(sc);
1256                 break;
1257
1258         case SIOCADDMULTI:
1259         case SIOCDELMULTI:
1260                 /*
1261                  * Multicast list has changed; set the hardware filter
1262                  * accordingly.
1263                  */
1264                 ED_LOCK(sc);
1265                 ed_setrcr(sc);
1266                 ED_UNLOCK(sc);
1267                 error = 0;
1268                 break;
1269
1270         case SIOCGIFMEDIA:
1271         case SIOCSIFMEDIA:
1272                 if (sc->sc_media_ioctl == NULL) {
1273                         error = EINVAL;
1274                         break;
1275                 }
1276                 sc->sc_media_ioctl(sc, ifr, command);
1277                 break;
1278
1279         default:
1280                 error = ether_ioctl(ifp, command, data);
1281                 break;
1282         }
1283         return (error);
1284 }
1285
1286 /*
1287  * Given a source and destination address, copy 'amount' of a packet from
1288  *      the ring buffer into a linear destination buffer. Takes into account
1289  *      ring-wrap.
1290  */
1291 static __inline void
1292 ed_ring_copy(struct ed_softc *sc, bus_size_t src, char *dst, u_short amount)
1293 {
1294         u_short tmp_amount;
1295
1296         /* does copy wrap to lower addr in ring buffer? */
1297         if (src + amount > sc->mem_end) {
1298                 tmp_amount = sc->mem_end - src;
1299                 /* copy amount up to end of NIC memory */
1300                 sc->readmem(sc, src, dst, tmp_amount);
1301                 amount -= tmp_amount;
1302                 src = sc->mem_ring;
1303                 dst += tmp_amount;
1304         }
1305         sc->readmem(sc, src, dst, amount);
1306 }
1307
1308 /*
1309  * Retreive packet from shared memory and send to the next level up via
1310  * ether_input().
1311  */
1312 static void
1313 ed_get_packet(struct ed_softc *sc, bus_size_t buf, u_short len)
1314 {
1315         struct ifnet *ifp = sc->ifp;
1316         struct ether_header *eh;
1317         struct mbuf *m;
1318
1319         /* Allocate a header mbuf */
1320         MGETHDR(m, M_DONTWAIT, MT_DATA);
1321         if (m == NULL)
1322                 return;
1323         m->m_pkthdr.rcvif = ifp;
1324         m->m_pkthdr.len = m->m_len = len;
1325
1326         /*
1327          * We always put the received packet in a single buffer -
1328          * either with just an mbuf header or in a cluster attached
1329          * to the header. The +2 is to compensate for the alignment
1330          * fixup below.
1331          */
1332         if ((len + 2) > MHLEN) {
1333                 /* Attach an mbuf cluster */
1334                 MCLGET(m, M_DONTWAIT);
1335
1336                 /* Insist on getting a cluster */
1337                 if ((m->m_flags & M_EXT) == 0) {
1338                         m_freem(m);
1339                         return;
1340                 }
1341         }
1342
1343         /*
1344          * The +2 is to longword align the start of the real packet.
1345          * This is important for NFS.
1346          */
1347         m->m_data += 2;
1348         eh = mtod(m, struct ether_header *);
1349
1350         /*
1351          * Get packet, including link layer address, from interface.
1352          */
1353         ed_ring_copy(sc, buf, (char *)eh, len);
1354
1355         m->m_pkthdr.len = m->m_len = len;
1356
1357         ED_UNLOCK(sc);
1358         (*ifp->if_input)(ifp, m);
1359         ED_LOCK(sc);
1360 }
1361
1362 /*
1363  * Supporting routines
1364  */
1365
1366 /*
1367  * Given a NIC memory source address and a host memory destination
1368  *      address, copy 'amount' from NIC to host using shared memory.
1369  *      The 'amount' is rounded up to a word - okay as long as mbufs
1370  *              are word sized.  That's what the +1 is below.
1371  * This routine accesses things as 16 bit quantities.
1372  */
1373 void
1374 ed_shmem_readmem16(struct ed_softc *sc, bus_size_t src, uint8_t *dst,
1375     uint16_t amount)
1376 {
1377         bus_space_read_region_2(sc->mem_bst, sc->mem_bsh, src, (uint16_t *)dst,
1378             amount + 1 / 2);
1379 }
1380
1381 /*
1382  * Given a NIC memory source address and a host memory destination
1383  *      address, copy 'amount' from NIC to host using shared memory.
1384  * This routine accesses things as 8 bit quantities.
1385  */
1386 void
1387 ed_shmem_readmem8(struct ed_softc *sc, bus_size_t src, uint8_t *dst,
1388     uint16_t amount)
1389 {
1390         bus_space_read_region_1(sc->mem_bst, sc->mem_bsh, src, dst, amount);
1391 }
1392
1393 /*
1394  * Given a NIC memory source address and a host memory destination
1395  *      address, copy 'amount' from NIC to host using Programmed I/O.
1396  *      The 'amount' is rounded up to a word - okay as long as mbufs
1397  *              are word sized.
1398  *      This routine is currently Novell-specific.
1399  */
1400 void
1401 ed_pio_readmem(struct ed_softc *sc, bus_size_t src, uint8_t *dst,
1402     uint16_t amount)
1403 {
1404         /* Regular Novell cards */
1405         /* select page 0 registers */
1406         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STA);
1407
1408         /* round up to a word */
1409         if (amount & 1)
1410                 ++amount;
1411
1412         /* set up DMA byte count */
1413         ed_nic_outb(sc, ED_P0_RBCR0, amount);
1414         ed_nic_outb(sc, ED_P0_RBCR1, amount >> 8);
1415
1416         /* set up source address in NIC mem */
1417         ed_nic_outb(sc, ED_P0_RSAR0, src);
1418         ed_nic_outb(sc, ED_P0_RSAR1, src >> 8);
1419
1420         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD0 | ED_CR_STA);
1421
1422         if (sc->isa16bit)
1423                 ed_asic_insw(sc, ED_NOVELL_DATA, dst, amount / 2);
1424         else
1425                 ed_asic_insb(sc, ED_NOVELL_DATA, dst, amount);
1426 }
1427
1428 /*
1429  * Stripped down routine for writing a linear buffer to NIC memory.
1430  *      Only used in the probe routine to test the memory. 'len' must
1431  *      be even.
1432  */
1433 void
1434 ed_pio_writemem(struct ed_softc *sc, uint8_t *src, uint16_t dst, uint16_t len)
1435 {
1436         int     maxwait = 200;  /* about 240us */
1437
1438         /* select page 0 registers */
1439         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STA);
1440
1441         /* reset remote DMA complete flag */
1442         ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RDC);
1443
1444         /* set up DMA byte count */
1445         ed_nic_outb(sc, ED_P0_RBCR0, len);
1446         ed_nic_outb(sc, ED_P0_RBCR1, len >> 8);
1447
1448         /* set up destination address in NIC mem */
1449         ed_nic_outb(sc, ED_P0_RSAR0, dst);
1450         ed_nic_outb(sc, ED_P0_RSAR1, dst >> 8);
1451
1452         /* set remote DMA write */
1453         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD1 | ED_CR_STA);
1454
1455         if (sc->isa16bit)
1456                 ed_asic_outsw(sc, ED_NOVELL_DATA, src, len / 2);
1457         else
1458                 ed_asic_outsb(sc, ED_NOVELL_DATA, src, len);
1459
1460         /*
1461          * Wait for remote DMA complete. This is necessary because on the
1462          * transmit side, data is handled internally by the NIC in bursts and
1463          * we can't start another remote DMA until this one completes. Not
1464          * waiting causes really bad things to happen - like the NIC
1465          * irrecoverably jamming the ISA bus.
1466          */
1467         while (((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) &&
1468             --maxwait)
1469                 continue;
1470 }
1471
1472 /*
1473  * Write an mbuf chain to the destination NIC memory address using
1474  *      programmed I/O.
1475  */
1476 static u_short
1477 ed_pio_write_mbufs(struct ed_softc *sc, struct mbuf *m, bus_size_t dst)
1478 {
1479         struct ifnet *ifp = sc->ifp;
1480         unsigned short total_len, dma_len;
1481         struct mbuf *mp;
1482         int     maxwait = 200;  /* about 240us */
1483
1484         ED_ASSERT_LOCKED(sc);
1485
1486 #ifdef ED_HPP
1487         /* HP PC Lan+ cards need special handling */
1488         if (sc->vendor == ED_VENDOR_HP && sc->type == ED_TYPE_HP_PCLANPLUS)
1489                 return ed_hpp_write_mbufs(sc, m, dst);
1490 #endif
1491
1492         /* Regular Novell cards */
1493         /* First, count up the total number of bytes to copy */
1494         for (total_len = 0, mp = m; mp; mp = mp->m_next)
1495                 total_len += mp->m_len;
1496
1497         dma_len = total_len;
1498         if (sc->isa16bit && (dma_len & 1))
1499                 dma_len++;
1500
1501         /* select page 0 registers */
1502         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STA);
1503
1504         /* reset remote DMA complete flag */
1505         ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RDC);
1506
1507         /* set up DMA byte count */
1508         ed_nic_outb(sc, ED_P0_RBCR0, dma_len);
1509         ed_nic_outb(sc, ED_P0_RBCR1, dma_len >> 8);
1510
1511         /* set up destination address in NIC mem */
1512         ed_nic_outb(sc, ED_P0_RSAR0, dst);
1513         ed_nic_outb(sc, ED_P0_RSAR1, dst >> 8);
1514
1515         /* set remote DMA write */
1516         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD1 | ED_CR_STA);
1517
1518   /*
1519    * Transfer the mbuf chain to the NIC memory.
1520    * 16-bit cards require that data be transferred as words, and only words.
1521    * So that case requires some extra code to patch over odd-length mbufs.
1522    */
1523
1524         if (!sc->isa16bit) {
1525                 /* NE1000s are easy */
1526                 while (m) {
1527                         if (m->m_len)
1528                                 ed_asic_outsb(sc, ED_NOVELL_DATA, 
1529                                     m->m_data, m->m_len);
1530                         m = m->m_next;
1531                 }
1532         } else {
1533                 /* NE2000s are a pain */
1534                 unsigned char *data;
1535                 int len, wantbyte;
1536                 unsigned char savebyte[2];
1537
1538                 wantbyte = 0;
1539
1540                 while (m) {
1541                         len = m->m_len;
1542                         if (len) {
1543                                 data = mtod(m, caddr_t);
1544                                 /* finish the last word */
1545                                 if (wantbyte) {
1546                                         savebyte[1] = *data;
1547                                         ed_asic_outw(sc, ED_NOVELL_DATA,
1548                                                      *(u_short *)savebyte);
1549                                         data++;
1550                                         len--;
1551                                         wantbyte = 0;
1552                                 }
1553                                 /* output contiguous words */
1554                                 if (len > 1) {
1555                                         ed_asic_outsw(sc, ED_NOVELL_DATA,
1556                                                       data, len >> 1);
1557                                         data += len & ~1;
1558                                         len &= 1;
1559                                 }
1560                                 /* save last byte, if necessary */
1561                                 if (len == 1) {
1562                                         savebyte[0] = *data;
1563                                         wantbyte = 1;
1564                                 }
1565                         }
1566                         m = m->m_next;
1567                 }
1568                 /* spit last byte */
1569                 if (wantbyte)
1570                         ed_asic_outw(sc, ED_NOVELL_DATA, *(u_short *)savebyte);
1571         }
1572
1573         /*
1574          * Wait for remote DMA complete. This is necessary because on the
1575          * transmit side, data is handled internally by the NIC in bursts and
1576          * we can't start another remote DMA until this one completes. Not
1577          * waiting causes really bad things to happen - like the NIC
1578          * irrecoverably jamming the ISA bus.
1579          */
1580         while (((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) &&
1581             --maxwait)
1582                 continue;
1583
1584         if (!maxwait) {
1585                 log(LOG_WARNING, "%s: remote transmit DMA failed to complete\n",
1586                     ifp->if_xname);
1587                 ed_reset(ifp);
1588                 return(0);
1589         }
1590         return (total_len);
1591 }
1592
1593 static void
1594 ed_setrcr(struct ed_softc *sc)
1595 {
1596         struct ifnet *ifp = sc->ifp;
1597         int     i;
1598         u_char  reg1;
1599
1600         ED_ASSERT_LOCKED(sc);
1601
1602         /* Bit 6 in AX88190 RCR register must be set. */
1603         if (sc->chip_type == ED_CHIP_TYPE_AX88190)
1604                 reg1 = ED_RCR_INTT;
1605         else
1606                 reg1 = 0x00;
1607
1608         /* set page 1 registers */
1609         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
1610
1611         if (ifp->if_flags & IFF_PROMISC) {
1612
1613                 /*
1614                  * Reconfigure the multicast filter.
1615                  */
1616                 for (i = 0; i < 8; i++)
1617                         ed_nic_outb(sc, ED_P1_MAR(i), 0xff);
1618
1619                 /*
1620                  * And turn on promiscuous mode. Also enable reception of
1621                  * runts and packets with CRC & alignment errors.
1622                  */
1623                 /* Set page 0 registers */
1624                 ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
1625
1626                 ed_nic_outb(sc, ED_P0_RCR, ED_RCR_PRO | ED_RCR_AM |
1627                             ED_RCR_AB | ED_RCR_AR | ED_RCR_SEP | reg1);
1628         } else {
1629                 /* set up multicast addresses and filter modes */
1630                 if (ifp->if_flags & IFF_MULTICAST) {
1631                         uint32_t  mcaf[2];
1632
1633                         if (ifp->if_flags & IFF_ALLMULTI) {
1634                                 mcaf[0] = 0xffffffff;
1635                                 mcaf[1] = 0xffffffff;
1636                         } else
1637                                 ed_ds_getmcaf(sc, mcaf);
1638
1639                         /*
1640                          * Set multicast filter on chip.
1641                          */
1642                         for (i = 0; i < 8; i++)
1643                                 ed_nic_outb(sc, ED_P1_MAR(i), ((u_char *) mcaf)[i]);
1644
1645                         /* Set page 0 registers */
1646                         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
1647
1648                         ed_nic_outb(sc, ED_P0_RCR, ED_RCR_AM | ED_RCR_AB | reg1);
1649                 } else {
1650
1651                         /*
1652                          * Initialize multicast address hashing registers to
1653                          * not accept multicasts.
1654                          */
1655                         for (i = 0; i < 8; ++i)
1656                                 ed_nic_outb(sc, ED_P1_MAR(i), 0x00);
1657
1658                         /* Set page 0 registers */
1659                         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
1660
1661                         ed_nic_outb(sc, ED_P0_RCR, ED_RCR_AB | reg1);
1662                 }
1663         }
1664
1665         /*
1666          * Start interface.
1667          */
1668         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
1669 }
1670
1671 /*
1672  * Compute the multicast address filter from the
1673  * list of multicast addresses we need to listen to.
1674  */
1675 static void
1676 ed_ds_getmcaf(struct ed_softc *sc, uint32_t *mcaf)
1677 {
1678         uint32_t index;
1679         u_char *af = (u_char *) mcaf;
1680         struct ifmultiaddr *ifma;
1681
1682         mcaf[0] = 0;
1683         mcaf[1] = 0;
1684
1685         IF_ADDR_LOCK(sc->ifp);
1686         TAILQ_FOREACH(ifma, &sc->ifp->if_multiaddrs, ifma_link) {
1687                 if (ifma->ifma_addr->sa_family != AF_LINK)
1688                         continue;
1689                 index = ether_crc32_be(LLADDR((struct sockaddr_dl *)
1690                     ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
1691                 af[index >> 3] |= 1 << (index & 7);
1692         }
1693         IF_ADDR_UNLOCK(sc->ifp);
1694 }
1695
1696 int
1697 ed_isa_mem_ok(device_t dev, u_long pmem, u_int memsize)
1698 {
1699         if (pmem < 0xa0000 || pmem + memsize > 0x1000000) {
1700                 device_printf(dev, "Invalid ISA memory address range "
1701                     "configured: 0x%lx - 0x%lx\n", pmem, pmem + memsize);
1702                 return (ENXIO);
1703         }
1704         return (0);
1705 }
1706
1707 int
1708 ed_clear_memory(device_t dev)
1709 {
1710         struct ed_softc *sc = device_get_softc(dev);
1711         bus_size_t i;
1712
1713         bus_space_set_region_1(sc->mem_bst, sc->mem_bsh, sc->mem_start,
1714             0, sc->mem_size);
1715
1716         for (i = 0; i < sc->mem_size; i++) {
1717                 if (bus_space_read_1(sc->mem_bst, sc->mem_bsh,
1718                     sc->mem_start + i)) {
1719                         device_printf(dev, "failed to clear shared memory at "
1720                           "0x%jx - check configuration\n",
1721                             (uintmax_t)rman_get_start(sc->mem_res) + i);
1722                         return (ENXIO);
1723                 }
1724         }
1725         return (0);
1726 }