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