Merge from vendor branch GDB:
[dragonfly.git] / sys / dev / netif / pcn / if_pcn.c
1 /*
2  * Copyright (c) 2000 Berkeley Software Design, Inc.
3  * Copyright (c) 1997, 1998, 1999, 2000
4  *      Bill Paul <wpaul@osd.bsdi.com>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following 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  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/pci/if_pcn.c,v 1.5.2.10 2003/03/05 18:42:33 njl Exp $
34  * $DragonFly: src/sys/dev/netif/pcn/if_pcn.c,v 1.13 2004/09/15 00:24:30 joerg Exp $
35  *
36  * $FreeBSD: src/sys/pci/if_pcn.c,v 1.5.2.10 2003/03/05 18:42:33 njl Exp $
37  */
38
39 /*
40  * AMD Am79c972 fast ethernet PCI NIC driver. Datatheets are available
41  * from http://www.amd.com.
42  *
43  * Written by Bill Paul <wpaul@osd.bsdi.com>
44  */
45
46 /*
47  * The AMD PCnet/PCI controllers are more advanced and functional
48  * versions of the venerable 7990 LANCE. The PCnet/PCI chips retain
49  * backwards compatibility with the LANCE and thus can be made
50  * to work with older LANCE drivers. This is in fact how the
51  * PCnet/PCI chips were supported in FreeBSD originally. The trouble
52  * is that the PCnet/PCI devices offer several performance enhancements
53  * which can't be exploited in LANCE compatibility mode. Chief among
54  * these enhancements is the ability to perform PCI DMA operations
55  * using 32-bit addressing (which eliminates the need for ISA
56  * bounce-buffering), and special receive buffer alignment (which
57  * allows the receive handler to pass packets to the upper protocol
58  * layers without copying on both the x86 and alpha platforms).
59  */
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/sockio.h>
64 #include <sys/mbuf.h>
65 #include <sys/malloc.h>
66 #include <sys/kernel.h>
67 #include <sys/socket.h>
68
69 #include <net/if.h>
70 #include <net/if_arp.h>
71 #include <net/ethernet.h>
72 #include <net/if_dl.h>
73 #include <net/if_media.h>
74
75 #include <net/bpf.h>
76
77 #include <vm/vm.h>              /* for vtophys */
78 #include <vm/pmap.h>            /* for vtophys */
79 #include <machine/clock.h>      /* for DELAY */
80 #include <machine/bus_pio.h>
81 #include <machine/bus_memio.h>
82 #include <machine/bus.h>
83 #include <machine/resource.h>
84 #include <sys/bus.h>
85 #include <sys/rman.h>
86
87 #include "../mii_layer/mii.h"
88 #include "../mii_layer/miivar.h"
89
90 #include <bus/pci/pcireg.h>
91 #include <bus/pci/pcivar.h>
92
93 #define PCN_USEIOSPACE
94
95 #include "if_pcnreg.h"
96
97 /* "controller miibus0" required.  See GENERIC if you get errors here. */
98 #include "miibus_if.h"
99
100 /*
101  * Various supported device vendors/types and their names.
102  */
103 static struct pcn_type pcn_devs[] = {
104         { PCN_VENDORID, PCN_DEVICEID_PCNET, "AMD PCnet/PCI 10/100BaseTX" },
105         { PCN_VENDORID, PCN_DEVICEID_HOME, "AMD PCnet/Home HomePNA" },
106         { 0, 0, NULL }
107 };
108
109 static u_int32_t pcn_csr_read   (struct pcn_softc *, int);
110 static u_int16_t pcn_csr_read16 (struct pcn_softc *, int);
111 static u_int16_t pcn_bcr_read16 (struct pcn_softc *, int);
112 static void pcn_csr_write       (struct pcn_softc *, int, int);
113 static u_int32_t pcn_bcr_read   (struct pcn_softc *, int);
114 static void pcn_bcr_write       (struct pcn_softc *, int, int);
115
116 static int pcn_probe            (device_t);
117 static int pcn_attach           (device_t);
118 static int pcn_detach           (device_t);
119
120 static int pcn_newbuf           (struct pcn_softc *, int, struct mbuf *);
121 static int pcn_encap            (struct pcn_softc *,
122                                         struct mbuf *, u_int32_t *);
123 static void pcn_rxeof           (struct pcn_softc *);
124 static void pcn_txeof           (struct pcn_softc *);
125 static void pcn_intr            (void *);
126 static void pcn_tick            (void *);
127 static void pcn_start           (struct ifnet *);
128 static int pcn_ioctl            (struct ifnet *, u_long, caddr_t,
129                                         struct ucred *);
130 static void pcn_init            (void *);
131 static void pcn_stop            (struct pcn_softc *);
132 static void pcn_watchdog                (struct ifnet *);
133 static void pcn_shutdown                (device_t);
134 static int pcn_ifmedia_upd      (struct ifnet *);
135 static void pcn_ifmedia_sts     (struct ifnet *, struct ifmediareq *);
136
137 static int pcn_miibus_readreg   (device_t, int, int);
138 static int pcn_miibus_writereg  (device_t, int, int, int);
139 static void pcn_miibus_statchg  (device_t);
140
141 static void pcn_setfilt         (struct ifnet *);
142 static void pcn_setmulti        (struct pcn_softc *);
143 static u_int32_t pcn_crc        (caddr_t);
144 static void pcn_reset           (struct pcn_softc *);
145 static int pcn_list_rx_init     (struct pcn_softc *);
146 static int pcn_list_tx_init     (struct pcn_softc *);
147
148 #ifdef PCN_USEIOSPACE
149 #define PCN_RES                 SYS_RES_IOPORT
150 #define PCN_RID                 PCN_PCI_LOIO
151 #else
152 #define PCN_RES                 SYS_RES_MEMORY
153 #define PCN_RID                 PCN_PCI_LOMEM
154 #endif
155
156 static device_method_t pcn_methods[] = {
157         /* Device interface */
158         DEVMETHOD(device_probe,         pcn_probe),
159         DEVMETHOD(device_attach,        pcn_attach),
160         DEVMETHOD(device_detach,        pcn_detach),
161         DEVMETHOD(device_shutdown,      pcn_shutdown),
162
163         /* bus interface */
164         DEVMETHOD(bus_print_child,      bus_generic_print_child),
165         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
166
167         /* MII interface */
168         DEVMETHOD(miibus_readreg,       pcn_miibus_readreg),
169         DEVMETHOD(miibus_writereg,      pcn_miibus_writereg),
170         DEVMETHOD(miibus_statchg,       pcn_miibus_statchg),
171
172         { 0, 0 }
173 };
174
175 static driver_t pcn_driver = {
176         "pcn",
177         pcn_methods,
178         sizeof(struct pcn_softc)
179 };
180
181 static devclass_t pcn_devclass;
182
183 DECLARE_DUMMY_MODULE(if_pcn);
184 DRIVER_MODULE(if_pcn, pci, pcn_driver, pcn_devclass, 0, 0);
185 DRIVER_MODULE(miibus, pcn, miibus_driver, miibus_devclass, 0, 0);
186
187 #define PCN_CSR_SETBIT(sc, reg, x)                      \
188         pcn_csr_write(sc, reg, pcn_csr_read(sc, reg) | (x))
189
190 #define PCN_CSR_CLRBIT(sc, reg, x)                      \
191         pcn_csr_write(sc, reg, pcn_csr_read(sc, reg) & ~(x))
192
193 #define PCN_BCR_SETBIT(sc, reg, x)                      \
194         pcn_bcr_write(sc, reg, pcn_bcr_read(sc, reg) | (x))
195
196 #define PCN_BCR_CLRBIT(sc, reg, x)                      \
197         pcn_bcr_write(sc, reg, pcn_bcr_read(sc, reg) & ~(x))
198
199 static u_int32_t pcn_csr_read(sc, reg)
200         struct pcn_softc        *sc;
201         int                     reg;
202 {
203         CSR_WRITE_4(sc, PCN_IO32_RAP, reg);
204         return(CSR_READ_4(sc, PCN_IO32_RDP));
205 }
206
207 static u_int16_t pcn_csr_read16(sc, reg)
208         struct pcn_softc        *sc;
209         int                     reg;
210 {
211         CSR_WRITE_2(sc, PCN_IO16_RAP, reg);
212         return(CSR_READ_2(sc, PCN_IO16_RDP));
213 }
214
215 static void pcn_csr_write(sc, reg, val)
216         struct pcn_softc        *sc;
217         int                     reg;
218 {
219         CSR_WRITE_4(sc, PCN_IO32_RAP, reg);
220         CSR_WRITE_4(sc, PCN_IO32_RDP, val);
221         return;
222 }
223
224 static u_int32_t pcn_bcr_read(sc, reg)
225         struct pcn_softc        *sc;
226         int                     reg;
227 {
228         CSR_WRITE_4(sc, PCN_IO32_RAP, reg);
229         return(CSR_READ_4(sc, PCN_IO32_BDP));
230 }
231
232 static u_int16_t pcn_bcr_read16(sc, reg)
233         struct pcn_softc        *sc;
234         int                     reg;
235 {
236         CSR_WRITE_2(sc, PCN_IO16_RAP, reg);
237         return(CSR_READ_2(sc, PCN_IO16_BDP));
238 }
239
240 static void pcn_bcr_write(sc, reg, val)
241         struct pcn_softc        *sc;
242         int                     reg;
243 {
244         CSR_WRITE_4(sc, PCN_IO32_RAP, reg);
245         CSR_WRITE_4(sc, PCN_IO32_BDP, val);
246         return;
247 }
248
249 static int pcn_miibus_readreg(dev, phy, reg)
250         device_t                dev;
251         int                     phy, reg;
252 {
253         struct pcn_softc        *sc;
254         int                     val;
255
256         sc = device_get_softc(dev);
257
258         if (sc->pcn_phyaddr && phy > sc->pcn_phyaddr)
259                 return(0);
260
261         pcn_bcr_write(sc, PCN_BCR_MIIADDR, reg | (phy << 5));
262         val = pcn_bcr_read(sc, PCN_BCR_MIIDATA) & 0xFFFF;
263         if (val == 0xFFFF)
264                 return(0);
265
266         sc->pcn_phyaddr = phy;
267
268         return(val);
269 }
270
271 static int pcn_miibus_writereg(dev, phy, reg, data)
272         device_t                dev;
273         int                     phy, reg, data;
274 {
275         struct pcn_softc        *sc;
276
277         sc = device_get_softc(dev);
278
279         pcn_bcr_write(sc, PCN_BCR_MIIADDR, reg | (phy << 5));
280         pcn_bcr_write(sc, PCN_BCR_MIIDATA, data);
281
282         return(0);
283 }
284
285 static void pcn_miibus_statchg(dev)
286         device_t                dev;
287 {
288         struct pcn_softc        *sc;
289         struct mii_data         *mii;
290
291         sc = device_get_softc(dev);
292         mii = device_get_softc(sc->pcn_miibus);
293
294         if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
295                 PCN_BCR_SETBIT(sc, PCN_BCR_DUPLEX, PCN_DUPLEX_FDEN);
296         } else {
297                 PCN_BCR_CLRBIT(sc, PCN_BCR_DUPLEX, PCN_DUPLEX_FDEN);
298         }
299
300         return;
301 }
302
303 #define DC_POLY         0xEDB88320
304
305 static u_int32_t pcn_crc(addr)
306         caddr_t                 addr;
307 {
308         u_int32_t               idx, bit, data, crc;
309
310         /* Compute CRC for the address value. */
311         crc = 0xFFFFFFFF; /* initial value */
312
313         for (idx = 0; idx < 6; idx++) {
314                 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
315                         crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0);
316         }
317
318         return ((crc >> 26) & 0x3F);
319 }
320
321 static void pcn_setmulti(sc)
322         struct pcn_softc        *sc;
323 {
324         struct ifnet            *ifp;
325         struct ifmultiaddr      *ifma;
326         u_int32_t               h, i;
327         u_int16_t               hashes[4] = { 0, 0, 0, 0 };
328
329         ifp = &sc->arpcom.ac_if;
330
331         PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND);
332
333         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
334                 for (i = 0; i < 4; i++)
335                         pcn_csr_write(sc, PCN_CSR_MAR0 + i, 0xFFFF);
336                 PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND);
337                 return;
338         }
339
340         /* first, zot all the existing hash bits */
341         for (i = 0; i < 4; i++)
342                 pcn_csr_write(sc, PCN_CSR_MAR0 + i, 0);
343
344         /* now program new ones */
345         for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
346             ifma = ifma->ifma_link.le_next) {
347                 if (ifma->ifma_addr->sa_family != AF_LINK)
348                         continue;
349                 h = pcn_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
350                 hashes[h >> 4] |= 1 << (h & 0xF);
351         }
352
353         for (i = 0; i < 4; i++)
354                 pcn_csr_write(sc, PCN_CSR_MAR0 + i, hashes[i]);
355
356         PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND);
357
358         return;
359 }
360
361 static void pcn_reset(sc)
362         struct pcn_softc        *sc;
363 {
364         /*
365          * Issue a reset by reading from the RESET register.
366          * Note that we don't know if the chip is operating in
367          * 16-bit or 32-bit mode at this point, so we attempt
368          * to reset the chip both ways. If one fails, the other
369          * will succeed.
370          */
371         CSR_READ_2(sc, PCN_IO16_RESET);
372         CSR_READ_4(sc, PCN_IO32_RESET);
373
374         /* Wait a little while for the chip to get its brains in order. */
375         DELAY(1000);
376
377         /* Select 32-bit (DWIO) mode */
378         CSR_WRITE_4(sc, PCN_IO32_RDP, 0);
379
380         /* Select software style 3. */
381         pcn_bcr_write(sc, PCN_BCR_SSTYLE, PCN_SWSTYLE_PCNETPCI_BURST);
382
383         return;
384 }
385
386 /*
387  * Probe for an AMD chip. Check the PCI vendor and device
388  * IDs against our list and return a device name if we find a match.
389  */
390 static int pcn_probe(dev)
391         device_t                dev;
392 {
393         struct pcn_type         *t;
394         struct pcn_softc        *sc;
395         int                     rid;
396         u_int32_t               chip_id;
397
398         t = pcn_devs;
399         sc = device_get_softc(dev);
400
401         while(t->pcn_name != NULL) {
402                 if ((pci_get_vendor(dev) == t->pcn_vid) &&
403                     (pci_get_device(dev) == t->pcn_did)) {
404                         /*
405                          * Temporarily map the I/O space
406                          * so we can read the chip ID register.
407                          */
408                         rid = PCN_RID;
409                         sc->pcn_res = bus_alloc_resource(dev, PCN_RES, &rid,
410                             0, ~0, 1, RF_ACTIVE);
411                         if (sc->pcn_res == NULL) {
412                                 device_printf(dev,
413                                     "couldn't map ports/memory\n");
414                                 return(ENXIO);
415                         }
416                         sc->pcn_btag = rman_get_bustag(sc->pcn_res);
417                         sc->pcn_bhandle = rman_get_bushandle(sc->pcn_res);
418                         /*
419                          * Note: we can *NOT* put the chip into
420                          * 32-bit mode yet. The lnc driver will only
421                          * work in 16-bit mode, and once the chip
422                          * goes into 32-bit mode, the only way to
423                          * get it out again is with a hardware reset.
424                          * So if pcn_probe() is called before the
425                          * lnc driver's probe routine, the chip will
426                          * be locked into 32-bit operation and the lnc
427                          * driver will be unable to attach to it.
428                          * Note II: if the chip happens to already
429                          * be in 32-bit mode, we still need to check
430                          * the chip ID, but first we have to detect
431                          * 32-bit mode using only 16-bit operations.
432                          * The safest way to do this is to read the
433                          * PCI subsystem ID from BCR23/24 and compare
434                          * that with the value read from PCI config
435                          * space.   
436                          */
437                         chip_id = pcn_bcr_read16(sc, PCN_BCR_PCISUBSYSID);
438                         chip_id <<= 16;
439                         chip_id |= pcn_bcr_read16(sc, PCN_BCR_PCISUBVENID);
440                         /*
441                          * Note III: the test for 0x10001000 is a hack to
442                          * pacify VMware, who's pseudo-PCnet interface is
443                          * broken. Reading the subsystem register from PCI
444                          * config space yeilds 0x00000000 while reading the
445                          * same value from I/O space yeilds 0x10001000. It's
446                          * not supposed to be that way.
447                          */
448                         if (chip_id == pci_read_config(dev,
449                             PCIR_SUBVEND_0, 4) || chip_id == 0x10001000) {
450                                 /* We're in 16-bit mode. */
451                                 chip_id = pcn_csr_read16(sc, PCN_CSR_CHIPID1);
452                                 chip_id <<= 16;
453                                 chip_id |= pcn_csr_read16(sc, PCN_CSR_CHIPID0);
454                         } else {
455                                 /* We're in 32-bit mode. */
456                                 chip_id = pcn_csr_read(sc, PCN_CSR_CHIPID1);
457                                 chip_id <<= 16;
458                                 chip_id |= pcn_csr_read(sc, PCN_CSR_CHIPID0);
459                         }
460                         bus_release_resource(dev, PCN_RES,
461                             PCN_RID, sc->pcn_res);
462                         chip_id >>= 12;
463                         sc->pcn_type = chip_id & PART_MASK;
464                         switch(sc->pcn_type) {
465                         case Am79C971:
466                         case Am79C972:
467                         case Am79C973:
468                         case Am79C975:
469                         case Am79C976:
470                         case Am79C978:
471                                 break;
472                         default:
473                                 return(ENXIO);
474                                 break;
475                         }
476                         device_set_desc(dev, t->pcn_name);
477                         return(0);
478                 }
479                 t++;
480         }
481
482         return(ENXIO);
483 }
484
485 /*
486  * Attach the interface. Allocate softc structures, do ifmedia
487  * setup and ethernet/BPF attach.
488  */
489 static int pcn_attach(dev)
490         device_t                dev;
491 {
492         int                     s;
493         u_int32_t               eaddr[2];
494         u_int32_t               command;
495         struct pcn_softc        *sc;
496         struct ifnet            *ifp;
497         int                     unit, error = 0, rid;
498
499         s = splimp();
500
501         sc = device_get_softc(dev);
502         unit = device_get_unit(dev);
503
504         /*
505          * Handle power management nonsense.
506          */
507
508         command = pci_read_config(dev, PCN_PCI_CAPID, 4) & 0x000000FF;
509         if (command == 0x01) {
510
511                 command = pci_read_config(dev, PCN_PCI_PWRMGMTCTRL, 4);
512                 if (command & PCN_PSTATE_MASK) {
513                         u_int32_t               iobase, membase, irq;
514
515                         /* Save important PCI config data. */
516                         iobase = pci_read_config(dev, PCN_PCI_LOIO, 4);
517                         membase = pci_read_config(dev, PCN_PCI_LOMEM, 4);
518                         irq = pci_read_config(dev, PCN_PCI_INTLINE, 4);
519
520                         /* Reset the power state. */
521                         printf("pcn%d: chip is in D%d power mode "
522                         "-- setting to D0\n", unit, command & PCN_PSTATE_MASK);
523                         command &= 0xFFFFFFFC;
524                         pci_write_config(dev, PCN_PCI_PWRMGMTCTRL, command, 4);
525
526                         /* Restore PCI config data. */
527                         pci_write_config(dev, PCN_PCI_LOIO, iobase, 4);
528                         pci_write_config(dev, PCN_PCI_LOMEM, membase, 4);
529                         pci_write_config(dev, PCN_PCI_INTLINE, irq, 4);
530                 }
531         }
532
533         /*
534          * Map control/status registers.
535          */
536         command = pci_read_config(dev, PCIR_COMMAND, 4);
537         command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
538         pci_write_config(dev, PCIR_COMMAND, command, 4);
539         command = pci_read_config(dev, PCIR_COMMAND, 4);
540
541 #ifdef PCN_USEIOSPACE
542         if (!(command & PCIM_CMD_PORTEN)) {
543                 printf("pcn%d: failed to enable I/O ports!\n", unit);
544                 error = ENXIO;;
545                 goto fail;
546         }
547 #else
548         if (!(command & PCIM_CMD_MEMEN)) {
549                 printf("pcn%d: failed to enable memory mapping!\n", unit);
550                 error = ENXIO;;
551                 goto fail;
552         }
553 #endif
554
555         rid = PCN_RID;
556         sc->pcn_res = bus_alloc_resource(dev, PCN_RES, &rid,
557             0, ~0, 1, RF_ACTIVE);
558
559         if (sc->pcn_res == NULL) {
560                 printf("pcn%d: couldn't map ports/memory\n", unit);
561                 error = ENXIO;
562                 goto fail;
563         }
564
565         sc->pcn_btag = rman_get_bustag(sc->pcn_res);
566         sc->pcn_bhandle = rman_get_bushandle(sc->pcn_res);
567
568         /* Allocate interrupt */
569         rid = 0;
570         sc->pcn_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
571             RF_SHAREABLE | RF_ACTIVE);
572
573         if (sc->pcn_irq == NULL) {
574                 printf("pcn%d: couldn't map interrupt\n", unit);
575                 bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
576                 error = ENXIO;
577                 goto fail;
578         }
579
580         error = bus_setup_intr(dev, sc->pcn_irq, INTR_TYPE_NET,
581             pcn_intr, sc, &sc->pcn_intrhand);
582
583         if (error) {
584                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_res);
585                 bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
586                 printf("pcn%d: couldn't set up irq\n", unit);
587                 goto fail;
588         }
589
590         /* Reset the adapter. */
591         pcn_reset(sc);
592
593         /*
594          * Get station address from the EEPROM.
595          */
596         eaddr[0] = CSR_READ_4(sc, PCN_IO32_APROM00);
597         eaddr[1] = CSR_READ_4(sc, PCN_IO32_APROM01);
598
599         sc->pcn_unit = unit;
600         callout_init(&sc->pcn_stat_timer);
601
602         sc->pcn_ldata = contigmalloc(sizeof(struct pcn_list_data), M_DEVBUF,
603             M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
604
605         if (sc->pcn_ldata == NULL) {
606                 printf("pcn%d: no memory for list buffers!\n", unit);
607                 bus_teardown_intr(dev, sc->pcn_irq, sc->pcn_intrhand);
608                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_irq);
609                 bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
610                 error = ENXIO;
611                 goto fail;
612         }
613         bzero(sc->pcn_ldata, sizeof(struct pcn_list_data));
614
615         ifp = &sc->arpcom.ac_if;
616         ifp->if_softc = sc;
617         if_initname(ifp, "pcn", unit);
618         ifp->if_mtu = ETHERMTU;
619         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
620         ifp->if_ioctl = pcn_ioctl;
621         ifp->if_start = pcn_start;
622         ifp->if_watchdog = pcn_watchdog;
623         ifp->if_init = pcn_init;
624         ifp->if_baudrate = 10000000;
625         ifp->if_snd.ifq_maxlen = PCN_TX_LIST_CNT - 1;
626
627         /*
628          * Do MII setup.
629          */
630         if (mii_phy_probe(dev, &sc->pcn_miibus,
631             pcn_ifmedia_upd, pcn_ifmedia_sts)) {
632                 printf("pcn%d: MII without any PHY!\n", sc->pcn_unit);
633                 contigfree(sc->pcn_ldata, sizeof(struct pcn_list_data),
634                     M_DEVBUF);
635                 bus_teardown_intr(dev, sc->pcn_irq, sc->pcn_intrhand);
636                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_irq);
637                 bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
638                 error = ENXIO;
639                 goto fail;
640         }
641
642         /*
643          * Call MI attach routine.
644          */
645         ether_ifattach(ifp, eaddr);
646
647 fail:
648         splx(s);
649         return(error);
650 }
651
652 static int pcn_detach(dev)
653         device_t                dev;
654 {
655         struct pcn_softc        *sc;
656         struct ifnet            *ifp;
657         int                     s;
658
659         s = splimp();
660
661         sc = device_get_softc(dev);
662         ifp = &sc->arpcom.ac_if;
663
664         pcn_reset(sc);
665         pcn_stop(sc);
666         ether_ifdetach(ifp);
667
668         if (sc->pcn_miibus != NULL) {
669                 bus_generic_detach(dev);
670                 device_delete_child(dev, sc->pcn_miibus);
671         }
672
673         bus_teardown_intr(dev, sc->pcn_irq, sc->pcn_intrhand);
674         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_irq);
675         bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
676
677         contigfree(sc->pcn_ldata, sizeof(struct pcn_list_data), M_DEVBUF);
678
679         splx(s);
680
681         return(0);
682 }
683
684 /*
685  * Initialize the transmit descriptors.
686  */
687 static int pcn_list_tx_init(sc)
688         struct pcn_softc        *sc;
689 {
690         struct pcn_list_data    *ld;
691         struct pcn_ring_data    *cd;
692         int                     i;
693
694         cd = &sc->pcn_cdata;
695         ld = sc->pcn_ldata;
696
697         for (i = 0; i < PCN_TX_LIST_CNT; i++) {
698                 cd->pcn_tx_chain[i] = NULL;
699                 ld->pcn_tx_list[i].pcn_tbaddr = 0;
700                 ld->pcn_tx_list[i].pcn_txctl = 0;
701                 ld->pcn_tx_list[i].pcn_txstat = 0;
702         }
703
704         cd->pcn_tx_prod = cd->pcn_tx_cons = cd->pcn_tx_cnt = 0;
705
706         return(0);
707 }
708
709
710 /*
711  * Initialize the RX descriptors and allocate mbufs for them.
712  */
713 static int pcn_list_rx_init(sc)
714         struct pcn_softc        *sc;
715 {
716         struct pcn_list_data    *ld;
717         struct pcn_ring_data    *cd;
718         int                     i;
719
720         ld = sc->pcn_ldata;
721         cd = &sc->pcn_cdata;
722
723         for (i = 0; i < PCN_RX_LIST_CNT; i++) {
724                 if (pcn_newbuf(sc, i, NULL) == ENOBUFS)
725                         return(ENOBUFS);
726         }
727
728         cd->pcn_rx_prod = 0;
729
730         return(0);
731 }
732
733 /*
734  * Initialize an RX descriptor and attach an MBUF cluster.
735  */
736 static int pcn_newbuf(sc, idx, m)
737         struct pcn_softc        *sc;
738         int                     idx;
739         struct mbuf             *m;
740 {
741         struct mbuf             *m_new = NULL;
742         struct pcn_rx_desc      *c;
743
744         c = &sc->pcn_ldata->pcn_rx_list[idx];
745
746         if (m == NULL) {
747                 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
748                 if (m_new == NULL)
749                         return(ENOBUFS);
750
751                 MCLGET(m_new, MB_DONTWAIT);
752                 if (!(m_new->m_flags & M_EXT)) {
753                         m_freem(m_new);
754                         return(ENOBUFS);
755                 }
756                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
757         } else {
758                 m_new = m;
759                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
760                 m_new->m_data = m_new->m_ext.ext_buf;
761         }
762
763         m_adj(m_new, ETHER_ALIGN);
764
765         sc->pcn_cdata.pcn_rx_chain[idx] = m_new;
766         c->pcn_rbaddr = vtophys(mtod(m_new, caddr_t));
767         c->pcn_bufsz = (~(PCN_RXLEN) + 1) & PCN_RXLEN_BUFSZ;
768         c->pcn_bufsz |= PCN_RXLEN_MBO;
769         c->pcn_rxstat = PCN_RXSTAT_STP|PCN_RXSTAT_ENP|PCN_RXSTAT_OWN;
770
771         return(0);
772 }
773
774 /*
775  * A frame has been uploaded: pass the resulting mbuf chain up to
776  * the higher level protocols.
777  */
778 static void pcn_rxeof(sc)
779         struct pcn_softc        *sc;
780 {
781         struct mbuf             *m;
782         struct ifnet            *ifp;
783         struct pcn_rx_desc      *cur_rx;
784         int                     i;
785
786         ifp = &sc->arpcom.ac_if;
787         i = sc->pcn_cdata.pcn_rx_prod;
788
789         while(PCN_OWN_RXDESC(&sc->pcn_ldata->pcn_rx_list[i])) {
790                 cur_rx = &sc->pcn_ldata->pcn_rx_list[i];
791                 m = sc->pcn_cdata.pcn_rx_chain[i];
792                 sc->pcn_cdata.pcn_rx_chain[i] = NULL;
793
794                 /*
795                  * If an error occurs, update stats, clear the
796                  * status word and leave the mbuf cluster in place:
797                  * it should simply get re-used next time this descriptor
798                  * comes up in the ring.
799                  */
800                 if (cur_rx->pcn_rxstat & PCN_RXSTAT_ERR) {
801                         ifp->if_ierrors++;
802                         pcn_newbuf(sc, i, m);
803                         PCN_INC(i, PCN_RX_LIST_CNT);
804                         continue;
805                 }
806
807                 if (pcn_newbuf(sc, i, NULL)) {
808                         /* Ran out of mbufs; recycle this one. */
809                         pcn_newbuf(sc, i, m);
810                         ifp->if_ierrors++;
811                         PCN_INC(i, PCN_RX_LIST_CNT);
812                         continue;
813                 }
814
815                 PCN_INC(i, PCN_RX_LIST_CNT);
816
817                 /* No errors; receive the packet. */
818                 ifp->if_ipackets++;
819                 m->m_len = m->m_pkthdr.len =
820                     cur_rx->pcn_rxlen - ETHER_CRC_LEN;
821                 m->m_pkthdr.rcvif = ifp;
822
823                 (*ifp->if_input)(ifp, m);
824         }
825
826         sc->pcn_cdata.pcn_rx_prod = i;
827
828         return;
829 }
830
831 /*
832  * A frame was downloaded to the chip. It's safe for us to clean up
833  * the list buffers.
834  */
835
836 static void pcn_txeof(sc)
837         struct pcn_softc        *sc;
838 {
839         struct pcn_tx_desc      *cur_tx = NULL;
840         struct ifnet            *ifp;
841         u_int32_t               idx;
842
843         ifp = &sc->arpcom.ac_if;
844
845         /*
846          * Go through our tx list and free mbufs for those
847          * frames that have been transmitted.
848          */
849         idx = sc->pcn_cdata.pcn_tx_cons;
850         while (idx != sc->pcn_cdata.pcn_tx_prod) {
851                 cur_tx = &sc->pcn_ldata->pcn_tx_list[idx];
852
853                 if (!PCN_OWN_TXDESC(cur_tx))
854                         break;
855
856                 if (!(cur_tx->pcn_txctl & PCN_TXCTL_ENP)) {
857                         sc->pcn_cdata.pcn_tx_cnt--;
858                         PCN_INC(idx, PCN_TX_LIST_CNT);
859                         continue;
860                 }
861
862                 if (cur_tx->pcn_txctl & PCN_TXCTL_ERR) {
863                         ifp->if_oerrors++;
864                         if (cur_tx->pcn_txstat & PCN_TXSTAT_EXDEF)
865                                 ifp->if_collisions++;
866                         if (cur_tx->pcn_txstat & PCN_TXSTAT_RTRY)
867                                 ifp->if_collisions++;
868                 }
869
870                 ifp->if_collisions +=
871                     cur_tx->pcn_txstat & PCN_TXSTAT_TRC;
872
873                 ifp->if_opackets++;
874                 if (sc->pcn_cdata.pcn_tx_chain[idx] != NULL) {
875                         m_freem(sc->pcn_cdata.pcn_tx_chain[idx]);
876                         sc->pcn_cdata.pcn_tx_chain[idx] = NULL;
877                 }
878
879                 sc->pcn_cdata.pcn_tx_cnt--;
880                 PCN_INC(idx, PCN_TX_LIST_CNT);
881         }
882
883         if (idx != sc->pcn_cdata.pcn_tx_cons) {
884                 /* Some buffers have been freed. */
885                 sc->pcn_cdata.pcn_tx_cons = idx;
886                 ifp->if_flags &= ~IFF_OACTIVE;
887         }
888         ifp->if_timer = (sc->pcn_cdata.pcn_tx_cnt == 0) ? 0 : 5;
889
890         return;
891 }
892
893 static void pcn_tick(xsc)
894         void                    *xsc;
895 {
896         struct pcn_softc        *sc;
897         struct mii_data         *mii;
898         struct ifnet            *ifp;
899         int                     s;
900
901         s = splimp();
902
903         sc = xsc;
904         ifp = &sc->arpcom.ac_if;
905
906         mii = device_get_softc(sc->pcn_miibus);
907         mii_tick(mii);
908
909         if (sc->pcn_link & !(mii->mii_media_status & IFM_ACTIVE))
910                 sc->pcn_link = 0;
911
912         if (!sc->pcn_link) {
913                 mii_pollstat(mii);
914                 if (mii->mii_media_status & IFM_ACTIVE &&
915                     IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
916                         sc->pcn_link++;
917                         if (ifp->if_snd.ifq_head != NULL)
918                                 pcn_start(ifp);
919         }
920
921         callout_reset(&sc->pcn_stat_timer, hz, pcn_tick, sc);
922
923         splx(s);
924
925         return;
926 }
927
928 static void pcn_intr(arg)
929         void                    *arg;
930 {
931         struct pcn_softc        *sc;
932         struct ifnet            *ifp;
933         u_int32_t               status;
934
935         sc = arg;
936         ifp = &sc->arpcom.ac_if;
937
938         /* Supress unwanted interrupts */
939         if (!(ifp->if_flags & IFF_UP)) {
940                 pcn_stop(sc);
941                 return;
942         }
943
944         CSR_WRITE_4(sc, PCN_IO32_RAP, PCN_CSR_CSR);
945
946         while ((status = CSR_READ_4(sc, PCN_IO32_RDP)) & PCN_CSR_INTR) {
947                 CSR_WRITE_4(sc, PCN_IO32_RDP, status);
948
949                 if (status & PCN_CSR_RINT)
950                         pcn_rxeof(sc);
951
952                 if (status & PCN_CSR_TINT)
953                         pcn_txeof(sc);
954
955                 if (status & PCN_CSR_ERR) {
956                         pcn_init(sc);
957                         break;
958                 }
959         }
960
961         if (ifp->if_snd.ifq_head != NULL)
962                 pcn_start(ifp);
963
964         return;
965 }
966
967 /*
968  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
969  * pointers to the fragment pointers.
970  */
971 static int pcn_encap(sc, m_head, txidx)
972         struct pcn_softc        *sc;
973         struct mbuf             *m_head;
974         u_int32_t               *txidx;
975 {
976         struct pcn_tx_desc      *f = NULL;
977         struct mbuf             *m;
978         int                     frag, cur, cnt = 0;
979
980         /*
981          * Start packing the mbufs in this chain into
982          * the fragment pointers. Stop when we run out
983          * of fragments or hit the end of the mbuf chain.
984          */
985         m = m_head;
986         cur = frag = *txidx;
987
988         for (m = m_head; m != NULL; m = m->m_next) {
989                 if (m->m_len != 0) {
990                         if ((PCN_TX_LIST_CNT -
991                             (sc->pcn_cdata.pcn_tx_cnt + cnt)) < 2)
992                                 return(ENOBUFS);
993                         f = &sc->pcn_ldata->pcn_tx_list[frag];
994                         f->pcn_txctl = (~(m->m_len) + 1) & PCN_TXCTL_BUFSZ;
995                         f->pcn_txctl |= PCN_TXCTL_MBO;
996                         f->pcn_tbaddr = vtophys(mtod(m, vm_offset_t));
997                         if (cnt == 0)
998                                 f->pcn_txctl |= PCN_TXCTL_STP;
999                         else
1000                                 f->pcn_txctl |= PCN_TXCTL_OWN;
1001                         cur = frag;
1002                         PCN_INC(frag, PCN_TX_LIST_CNT);
1003                         cnt++;
1004                 }
1005         }
1006
1007         if (m != NULL)
1008                 return(ENOBUFS);
1009
1010         sc->pcn_cdata.pcn_tx_chain[cur] = m_head;
1011         sc->pcn_ldata->pcn_tx_list[cur].pcn_txctl |=
1012             PCN_TXCTL_ENP|PCN_TXCTL_ADD_FCS|PCN_TXCTL_MORE_LTINT;
1013         sc->pcn_ldata->pcn_tx_list[*txidx].pcn_txctl |= PCN_TXCTL_OWN;
1014         sc->pcn_cdata.pcn_tx_cnt += cnt;
1015         *txidx = frag;
1016
1017         return(0);
1018 }
1019
1020 /*
1021  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1022  * to the mbuf data regions directly in the transmit lists. We also save a
1023  * copy of the pointers since the transmit list fragment pointers are
1024  * physical addresses.
1025  */
1026 static void pcn_start(ifp)
1027         struct ifnet            *ifp;
1028 {
1029         struct pcn_softc        *sc;
1030         struct mbuf             *m_head = NULL;
1031         u_int32_t               idx;
1032
1033         sc = ifp->if_softc;
1034
1035         if (!sc->pcn_link)
1036                 return;
1037
1038         idx = sc->pcn_cdata.pcn_tx_prod;
1039
1040         if (ifp->if_flags & IFF_OACTIVE)
1041                 return;
1042
1043         while(sc->pcn_cdata.pcn_tx_chain[idx] == NULL) {
1044                 IF_DEQUEUE(&ifp->if_snd, m_head);
1045                 if (m_head == NULL)
1046                         break;
1047
1048                 if (pcn_encap(sc, m_head, &idx)) {
1049                         IF_PREPEND(&ifp->if_snd, m_head);
1050                         ifp->if_flags |= IFF_OACTIVE;
1051                         break;
1052                 }
1053
1054                 /*
1055                  * If there's a BPF listener, bounce a copy of this frame
1056                  * to him.
1057                  */
1058                 if (ifp->if_bpf)
1059                         bpf_mtap(ifp, m_head);
1060
1061         }
1062
1063         /* Transmit */
1064         sc->pcn_cdata.pcn_tx_prod = idx;
1065         pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_TX|PCN_CSR_INTEN);
1066
1067         /*
1068          * Set a timeout in case the chip goes out to lunch.
1069          */
1070         ifp->if_timer = 5;
1071
1072         return;
1073 }
1074
1075 void pcn_setfilt(ifp)
1076         struct ifnet            *ifp;
1077 {
1078         struct pcn_softc        *sc;
1079
1080         sc = ifp->if_softc;
1081
1082         /* If we want promiscuous mode, set the allframes bit. */
1083         if (ifp->if_flags & IFF_PROMISC) {
1084                 PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC);
1085         } else {
1086                 PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC);
1087         }
1088
1089         /* Set the capture broadcast bit to capture broadcast frames. */
1090         if (ifp->if_flags & IFF_BROADCAST) {
1091                 PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD);
1092         } else {
1093                 PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD);
1094         }
1095
1096         return;
1097 }
1098
1099 static void pcn_init(xsc)
1100         void                    *xsc;
1101 {
1102         struct pcn_softc        *sc = xsc;
1103         struct ifnet            *ifp = &sc->arpcom.ac_if;
1104         struct mii_data         *mii = NULL;
1105         int                     s;
1106
1107         s = splimp();
1108
1109         /*
1110          * Cancel pending I/O and free all RX/TX buffers.
1111          */
1112         pcn_stop(sc);
1113         pcn_reset(sc);
1114
1115         mii = device_get_softc(sc->pcn_miibus);
1116
1117         /* Set MAC address */
1118         pcn_csr_write(sc, PCN_CSR_PAR0,
1119             ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1120         pcn_csr_write(sc, PCN_CSR_PAR1,
1121             ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1122         pcn_csr_write(sc, PCN_CSR_PAR2,
1123             ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
1124
1125         /* Init circular RX list. */
1126         if (pcn_list_rx_init(sc) == ENOBUFS) {
1127                 printf("pcn%d: initialization failed: no "
1128                     "memory for rx buffers\n", sc->pcn_unit);
1129                 pcn_stop(sc);
1130                 (void)splx(s);
1131                 return;
1132         }
1133
1134         /* Set up RX filter. */
1135         pcn_setfilt(ifp);
1136
1137         /*
1138          * Init tx descriptors.
1139          */
1140         pcn_list_tx_init(sc);
1141
1142         /* Set up the mode register. */
1143         pcn_csr_write(sc, PCN_CSR_MODE, PCN_PORT_MII);
1144
1145         /*
1146          * Load the multicast filter.
1147          */
1148         pcn_setmulti(sc);
1149
1150         /*
1151          * Load the addresses of the RX and TX lists.
1152          */
1153         pcn_csr_write(sc, PCN_CSR_RXADDR0,
1154             vtophys(&sc->pcn_ldata->pcn_rx_list[0]) & 0xFFFF);
1155         pcn_csr_write(sc, PCN_CSR_RXADDR1,
1156             (vtophys(&sc->pcn_ldata->pcn_rx_list[0]) >> 16) & 0xFFFF);
1157         pcn_csr_write(sc, PCN_CSR_TXADDR0,
1158             vtophys(&sc->pcn_ldata->pcn_tx_list[0]) & 0xFFFF);
1159         pcn_csr_write(sc, PCN_CSR_TXADDR1,
1160             (vtophys(&sc->pcn_ldata->pcn_tx_list[0]) >> 16) & 0xFFFF);
1161
1162         /* Set the RX and TX ring sizes. */
1163         pcn_csr_write(sc, PCN_CSR_RXRINGLEN, (~PCN_RX_LIST_CNT) + 1);
1164         pcn_csr_write(sc, PCN_CSR_TXRINGLEN, (~PCN_TX_LIST_CNT) + 1);
1165
1166         /* We're not using the initialization block. */
1167         pcn_csr_write(sc, PCN_CSR_IAB1, 0);
1168
1169         /* Enable fast suspend mode. */
1170         PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL2, PCN_EXTCTL2_FASTSPNDE);
1171
1172         /*
1173          * Enable burst read and write. Also set the no underflow
1174          * bit. This will avoid transmit underruns in certain
1175          * conditions while still providing decent performance.
1176          */
1177         PCN_BCR_SETBIT(sc, PCN_BCR_BUSCTL, PCN_BUSCTL_NOUFLOW|
1178             PCN_BUSCTL_BREAD|PCN_BUSCTL_BWRITE);
1179
1180         /* Enable graceful recovery from underflow. */
1181         PCN_CSR_SETBIT(sc, PCN_CSR_IMR, PCN_IMR_DXSUFLO);
1182
1183         /* Enable auto-padding of short TX frames. */
1184         PCN_CSR_SETBIT(sc, PCN_CSR_TFEAT, PCN_TFEAT_PAD_TX);
1185
1186         /* Disable MII autoneg (we handle this ourselves). */
1187         PCN_BCR_SETBIT(sc, PCN_BCR_MIICTL, PCN_MIICTL_DANAS);
1188
1189         if (sc->pcn_type == Am79C978)
1190                 pcn_bcr_write(sc, PCN_BCR_PHYSEL,
1191                     PCN_PHYSEL_PCNET|PCN_PHY_HOMEPNA);
1192
1193         /* Enable interrupts and start the controller running. */
1194         pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_INTEN|PCN_CSR_START);
1195
1196         mii_mediachg(mii);
1197
1198         ifp->if_flags |= IFF_RUNNING;
1199         ifp->if_flags &= ~IFF_OACTIVE;
1200
1201         (void)splx(s);
1202         callout_reset(&sc->pcn_stat_timer, hz, pcn_tick, sc);
1203 }
1204
1205 /*
1206  * Set media options.
1207  */
1208 static int pcn_ifmedia_upd(ifp)
1209         struct ifnet            *ifp;
1210 {
1211         struct pcn_softc        *sc;
1212         struct mii_data         *mii;
1213
1214         sc = ifp->if_softc;
1215         mii = device_get_softc(sc->pcn_miibus);
1216
1217         sc->pcn_link = 0;
1218         if (mii->mii_instance) {
1219                 struct mii_softc        *miisc;
1220                 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1221                     miisc = LIST_NEXT(miisc, mii_list))
1222                         mii_phy_reset(miisc);
1223         }
1224         mii_mediachg(mii);
1225
1226         return(0);
1227 }
1228
1229 /*
1230  * Report current media status.
1231  */
1232 static void pcn_ifmedia_sts(ifp, ifmr)
1233         struct ifnet            *ifp;
1234         struct ifmediareq       *ifmr;
1235 {
1236         struct pcn_softc        *sc;
1237         struct mii_data         *mii;
1238
1239         sc = ifp->if_softc;
1240
1241         mii = device_get_softc(sc->pcn_miibus);
1242         mii_pollstat(mii);
1243         ifmr->ifm_active = mii->mii_media_active;
1244         ifmr->ifm_status = mii->mii_media_status;
1245
1246         return;
1247 }
1248
1249 static int pcn_ioctl(ifp, command, data, cr)
1250         struct ifnet            *ifp;
1251         u_long                  command;
1252         caddr_t                 data;
1253         struct ucred            *cr;
1254 {
1255         struct pcn_softc        *sc = ifp->if_softc;
1256         struct ifreq            *ifr = (struct ifreq *) data;
1257         struct mii_data         *mii = NULL;
1258         int                     s, error = 0;
1259
1260         s = splimp();
1261
1262         switch(command) {
1263         case SIOCSIFADDR:
1264         case SIOCGIFADDR:
1265         case SIOCSIFMTU:
1266                 error = ether_ioctl(ifp, command, data);
1267                 break;
1268         case SIOCSIFFLAGS:
1269                 if (ifp->if_flags & IFF_UP) {
1270                         if (ifp->if_flags & IFF_RUNNING &&
1271                             ifp->if_flags & IFF_PROMISC &&
1272                             !(sc->pcn_if_flags & IFF_PROMISC)) {
1273                                 PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1,
1274                                     PCN_EXTCTL1_SPND);
1275                                 pcn_setfilt(ifp);
1276                                 PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1,
1277                                     PCN_EXTCTL1_SPND);
1278                                 pcn_csr_write(sc, PCN_CSR_CSR,
1279                                     PCN_CSR_INTEN|PCN_CSR_START);
1280                         } else if (ifp->if_flags & IFF_RUNNING &&
1281                             !(ifp->if_flags & IFF_PROMISC) &&
1282                                 sc->pcn_if_flags & IFF_PROMISC) {
1283                                 PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1,
1284                                     PCN_EXTCTL1_SPND);
1285                                 pcn_setfilt(ifp);
1286                                 PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1,
1287                                     PCN_EXTCTL1_SPND);
1288                                 pcn_csr_write(sc, PCN_CSR_CSR,
1289                                     PCN_CSR_INTEN|PCN_CSR_START);
1290                         } else if (!(ifp->if_flags & IFF_RUNNING))
1291                                 pcn_init(sc);
1292                 } else {
1293                         if (ifp->if_flags & IFF_RUNNING)
1294                                 pcn_stop(sc);
1295                 }
1296                 sc->pcn_if_flags = ifp->if_flags;
1297                 error = 0;
1298                 break;
1299         case SIOCADDMULTI:
1300         case SIOCDELMULTI:
1301                 pcn_setmulti(sc);
1302                 error = 0;
1303                 break;
1304         case SIOCGIFMEDIA:
1305         case SIOCSIFMEDIA:
1306                 mii = device_get_softc(sc->pcn_miibus);
1307                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1308                 break;
1309         default:
1310                 error = EINVAL;
1311                 break;
1312         }
1313
1314         (void)splx(s);
1315
1316         return(error);
1317 }
1318
1319 static void pcn_watchdog(ifp)
1320         struct ifnet            *ifp;
1321 {
1322         struct pcn_softc        *sc;
1323
1324         sc = ifp->if_softc;
1325
1326         ifp->if_oerrors++;
1327         printf("pcn%d: watchdog timeout\n", sc->pcn_unit);
1328
1329         pcn_stop(sc);
1330         pcn_reset(sc);
1331         pcn_init(sc);
1332
1333         if (ifp->if_snd.ifq_head != NULL)
1334                 pcn_start(ifp);
1335
1336         return;
1337 }
1338
1339 /*
1340  * Stop the adapter and free any mbufs allocated to the
1341  * RX and TX lists.
1342  */
1343 static void pcn_stop(sc)
1344         struct pcn_softc        *sc;
1345 {
1346         int             i;
1347         struct ifnet            *ifp;
1348
1349         ifp = &sc->arpcom.ac_if;
1350         ifp->if_timer = 0;
1351
1352         callout_stop(&sc->pcn_stat_timer);
1353         PCN_CSR_SETBIT(sc, PCN_CSR_CSR, PCN_CSR_STOP);
1354         sc->pcn_link = 0;
1355
1356         /*
1357          * Free data in the RX lists.
1358          */
1359         for (i = 0; i < PCN_RX_LIST_CNT; i++) {
1360                 if (sc->pcn_cdata.pcn_rx_chain[i] != NULL) {
1361                         m_freem(sc->pcn_cdata.pcn_rx_chain[i]);
1362                         sc->pcn_cdata.pcn_rx_chain[i] = NULL;
1363                 }
1364         }
1365         bzero((char *)&sc->pcn_ldata->pcn_rx_list,
1366                 sizeof(sc->pcn_ldata->pcn_rx_list));
1367
1368         /*
1369          * Free the TX list buffers.
1370          */
1371         for (i = 0; i < PCN_TX_LIST_CNT; i++) {
1372                 if (sc->pcn_cdata.pcn_tx_chain[i] != NULL) {
1373                         m_freem(sc->pcn_cdata.pcn_tx_chain[i]);
1374                         sc->pcn_cdata.pcn_tx_chain[i] = NULL;
1375                 }
1376         }
1377
1378         bzero((char *)&sc->pcn_ldata->pcn_tx_list,
1379                 sizeof(sc->pcn_ldata->pcn_tx_list));
1380
1381         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1382
1383         return;
1384 }
1385
1386 /*
1387  * Stop all chip I/O so that the kernel's probe routines don't
1388  * get confused by errant DMAs when rebooting.
1389  */
1390 static void pcn_shutdown(dev)
1391         device_t                dev;
1392 {
1393         struct pcn_softc        *sc;
1394
1395         sc = device_get_softc(dev);
1396
1397         pcn_reset(sc);
1398         pcn_stop(sc);
1399
1400         return;
1401 }