Unify the input handling of the low-level network stack by introducing
[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.12 2004/07/23 07:16:27 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_handle_init(&sc->pcn_stat_ch);
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         callout_handle_init(&sc->pcn_stat_ch);
647
648 fail:
649         splx(s);
650         return(error);
651 }
652
653 static int pcn_detach(dev)
654         device_t                dev;
655 {
656         struct pcn_softc        *sc;
657         struct ifnet            *ifp;
658         int                     s;
659
660         s = splimp();
661
662         sc = device_get_softc(dev);
663         ifp = &sc->arpcom.ac_if;
664
665         pcn_reset(sc);
666         pcn_stop(sc);
667         ether_ifdetach(ifp);
668
669         if (sc->pcn_miibus != NULL) {
670                 bus_generic_detach(dev);
671                 device_delete_child(dev, sc->pcn_miibus);
672         }
673
674         bus_teardown_intr(dev, sc->pcn_irq, sc->pcn_intrhand);
675         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_irq);
676         bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
677
678         contigfree(sc->pcn_ldata, sizeof(struct pcn_list_data), M_DEVBUF);
679
680         splx(s);
681
682         return(0);
683 }
684
685 /*
686  * Initialize the transmit descriptors.
687  */
688 static int pcn_list_tx_init(sc)
689         struct pcn_softc        *sc;
690 {
691         struct pcn_list_data    *ld;
692         struct pcn_ring_data    *cd;
693         int                     i;
694
695         cd = &sc->pcn_cdata;
696         ld = sc->pcn_ldata;
697
698         for (i = 0; i < PCN_TX_LIST_CNT; i++) {
699                 cd->pcn_tx_chain[i] = NULL;
700                 ld->pcn_tx_list[i].pcn_tbaddr = 0;
701                 ld->pcn_tx_list[i].pcn_txctl = 0;
702                 ld->pcn_tx_list[i].pcn_txstat = 0;
703         }
704
705         cd->pcn_tx_prod = cd->pcn_tx_cons = cd->pcn_tx_cnt = 0;
706
707         return(0);
708 }
709
710
711 /*
712  * Initialize the RX descriptors and allocate mbufs for them.
713  */
714 static int pcn_list_rx_init(sc)
715         struct pcn_softc        *sc;
716 {
717         struct pcn_list_data    *ld;
718         struct pcn_ring_data    *cd;
719         int                     i;
720
721         ld = sc->pcn_ldata;
722         cd = &sc->pcn_cdata;
723
724         for (i = 0; i < PCN_RX_LIST_CNT; i++) {
725                 if (pcn_newbuf(sc, i, NULL) == ENOBUFS)
726                         return(ENOBUFS);
727         }
728
729         cd->pcn_rx_prod = 0;
730
731         return(0);
732 }
733
734 /*
735  * Initialize an RX descriptor and attach an MBUF cluster.
736  */
737 static int pcn_newbuf(sc, idx, m)
738         struct pcn_softc        *sc;
739         int                     idx;
740         struct mbuf             *m;
741 {
742         struct mbuf             *m_new = NULL;
743         struct pcn_rx_desc      *c;
744
745         c = &sc->pcn_ldata->pcn_rx_list[idx];
746
747         if (m == NULL) {
748                 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
749                 if (m_new == NULL)
750                         return(ENOBUFS);
751
752                 MCLGET(m_new, MB_DONTWAIT);
753                 if (!(m_new->m_flags & M_EXT)) {
754                         m_freem(m_new);
755                         return(ENOBUFS);
756                 }
757                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
758         } else {
759                 m_new = m;
760                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
761                 m_new->m_data = m_new->m_ext.ext_buf;
762         }
763
764         m_adj(m_new, ETHER_ALIGN);
765
766         sc->pcn_cdata.pcn_rx_chain[idx] = m_new;
767         c->pcn_rbaddr = vtophys(mtod(m_new, caddr_t));
768         c->pcn_bufsz = (~(PCN_RXLEN) + 1) & PCN_RXLEN_BUFSZ;
769         c->pcn_bufsz |= PCN_RXLEN_MBO;
770         c->pcn_rxstat = PCN_RXSTAT_STP|PCN_RXSTAT_ENP|PCN_RXSTAT_OWN;
771
772         return(0);
773 }
774
775 /*
776  * A frame has been uploaded: pass the resulting mbuf chain up to
777  * the higher level protocols.
778  */
779 static void pcn_rxeof(sc)
780         struct pcn_softc        *sc;
781 {
782         struct mbuf             *m;
783         struct ifnet            *ifp;
784         struct pcn_rx_desc      *cur_rx;
785         int                     i;
786
787         ifp = &sc->arpcom.ac_if;
788         i = sc->pcn_cdata.pcn_rx_prod;
789
790         while(PCN_OWN_RXDESC(&sc->pcn_ldata->pcn_rx_list[i])) {
791                 cur_rx = &sc->pcn_ldata->pcn_rx_list[i];
792                 m = sc->pcn_cdata.pcn_rx_chain[i];
793                 sc->pcn_cdata.pcn_rx_chain[i] = NULL;
794
795                 /*
796                  * If an error occurs, update stats, clear the
797                  * status word and leave the mbuf cluster in place:
798                  * it should simply get re-used next time this descriptor
799                  * comes up in the ring.
800                  */
801                 if (cur_rx->pcn_rxstat & PCN_RXSTAT_ERR) {
802                         ifp->if_ierrors++;
803                         pcn_newbuf(sc, i, m);
804                         PCN_INC(i, PCN_RX_LIST_CNT);
805                         continue;
806                 }
807
808                 if (pcn_newbuf(sc, i, NULL)) {
809                         /* Ran out of mbufs; recycle this one. */
810                         pcn_newbuf(sc, i, m);
811                         ifp->if_ierrors++;
812                         PCN_INC(i, PCN_RX_LIST_CNT);
813                         continue;
814                 }
815
816                 PCN_INC(i, PCN_RX_LIST_CNT);
817
818                 /* No errors; receive the packet. */
819                 ifp->if_ipackets++;
820                 m->m_len = m->m_pkthdr.len =
821                     cur_rx->pcn_rxlen - ETHER_CRC_LEN;
822                 m->m_pkthdr.rcvif = ifp;
823
824                 (*ifp->if_input)(ifp, m);
825         }
826
827         sc->pcn_cdata.pcn_rx_prod = i;
828
829         return;
830 }
831
832 /*
833  * A frame was downloaded to the chip. It's safe for us to clean up
834  * the list buffers.
835  */
836
837 static void pcn_txeof(sc)
838         struct pcn_softc        *sc;
839 {
840         struct pcn_tx_desc      *cur_tx = NULL;
841         struct ifnet            *ifp;
842         u_int32_t               idx;
843
844         ifp = &sc->arpcom.ac_if;
845
846         /*
847          * Go through our tx list and free mbufs for those
848          * frames that have been transmitted.
849          */
850         idx = sc->pcn_cdata.pcn_tx_cons;
851         while (idx != sc->pcn_cdata.pcn_tx_prod) {
852                 cur_tx = &sc->pcn_ldata->pcn_tx_list[idx];
853
854                 if (!PCN_OWN_TXDESC(cur_tx))
855                         break;
856
857                 if (!(cur_tx->pcn_txctl & PCN_TXCTL_ENP)) {
858                         sc->pcn_cdata.pcn_tx_cnt--;
859                         PCN_INC(idx, PCN_TX_LIST_CNT);
860                         continue;
861                 }
862
863                 if (cur_tx->pcn_txctl & PCN_TXCTL_ERR) {
864                         ifp->if_oerrors++;
865                         if (cur_tx->pcn_txstat & PCN_TXSTAT_EXDEF)
866                                 ifp->if_collisions++;
867                         if (cur_tx->pcn_txstat & PCN_TXSTAT_RTRY)
868                                 ifp->if_collisions++;
869                 }
870
871                 ifp->if_collisions +=
872                     cur_tx->pcn_txstat & PCN_TXSTAT_TRC;
873
874                 ifp->if_opackets++;
875                 if (sc->pcn_cdata.pcn_tx_chain[idx] != NULL) {
876                         m_freem(sc->pcn_cdata.pcn_tx_chain[idx]);
877                         sc->pcn_cdata.pcn_tx_chain[idx] = NULL;
878                 }
879
880                 sc->pcn_cdata.pcn_tx_cnt--;
881                 PCN_INC(idx, PCN_TX_LIST_CNT);
882         }
883
884         if (idx != sc->pcn_cdata.pcn_tx_cons) {
885                 /* Some buffers have been freed. */
886                 sc->pcn_cdata.pcn_tx_cons = idx;
887                 ifp->if_flags &= ~IFF_OACTIVE;
888         }
889         ifp->if_timer = (sc->pcn_cdata.pcn_tx_cnt == 0) ? 0 : 5;
890
891         return;
892 }
893
894 static void pcn_tick(xsc)
895         void                    *xsc;
896 {
897         struct pcn_softc        *sc;
898         struct mii_data         *mii;
899         struct ifnet            *ifp;
900         int                     s;
901
902         s = splimp();
903
904         sc = xsc;
905         ifp = &sc->arpcom.ac_if;
906
907         mii = device_get_softc(sc->pcn_miibus);
908         mii_tick(mii);
909
910         if (sc->pcn_link & !(mii->mii_media_status & IFM_ACTIVE))
911                 sc->pcn_link = 0;
912
913         if (!sc->pcn_link) {
914                 mii_pollstat(mii);
915                 if (mii->mii_media_status & IFM_ACTIVE &&
916                     IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
917                         sc->pcn_link++;
918                         if (ifp->if_snd.ifq_head != NULL)
919                                 pcn_start(ifp);
920         }
921
922         sc->pcn_stat_ch = timeout(pcn_tick, sc, hz);
923
924         splx(s);
925
926         return;
927 }
928
929 static void pcn_intr(arg)
930         void                    *arg;
931 {
932         struct pcn_softc        *sc;
933         struct ifnet            *ifp;
934         u_int32_t               status;
935
936         sc = arg;
937         ifp = &sc->arpcom.ac_if;
938
939         /* Supress unwanted interrupts */
940         if (!(ifp->if_flags & IFF_UP)) {
941                 pcn_stop(sc);
942                 return;
943         }
944
945         CSR_WRITE_4(sc, PCN_IO32_RAP, PCN_CSR_CSR);
946
947         while ((status = CSR_READ_4(sc, PCN_IO32_RDP)) & PCN_CSR_INTR) {
948                 CSR_WRITE_4(sc, PCN_IO32_RDP, status);
949
950                 if (status & PCN_CSR_RINT)
951                         pcn_rxeof(sc);
952
953                 if (status & PCN_CSR_TINT)
954                         pcn_txeof(sc);
955
956                 if (status & PCN_CSR_ERR) {
957                         pcn_init(sc);
958                         break;
959                 }
960         }
961
962         if (ifp->if_snd.ifq_head != NULL)
963                 pcn_start(ifp);
964
965         return;
966 }
967
968 /*
969  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
970  * pointers to the fragment pointers.
971  */
972 static int pcn_encap(sc, m_head, txidx)
973         struct pcn_softc        *sc;
974         struct mbuf             *m_head;
975         u_int32_t               *txidx;
976 {
977         struct pcn_tx_desc      *f = NULL;
978         struct mbuf             *m;
979         int                     frag, cur, cnt = 0;
980
981         /*
982          * Start packing the mbufs in this chain into
983          * the fragment pointers. Stop when we run out
984          * of fragments or hit the end of the mbuf chain.
985          */
986         m = m_head;
987         cur = frag = *txidx;
988
989         for (m = m_head; m != NULL; m = m->m_next) {
990                 if (m->m_len != 0) {
991                         if ((PCN_TX_LIST_CNT -
992                             (sc->pcn_cdata.pcn_tx_cnt + cnt)) < 2)
993                                 return(ENOBUFS);
994                         f = &sc->pcn_ldata->pcn_tx_list[frag];
995                         f->pcn_txctl = (~(m->m_len) + 1) & PCN_TXCTL_BUFSZ;
996                         f->pcn_txctl |= PCN_TXCTL_MBO;
997                         f->pcn_tbaddr = vtophys(mtod(m, vm_offset_t));
998                         if (cnt == 0)
999                                 f->pcn_txctl |= PCN_TXCTL_STP;
1000                         else
1001                                 f->pcn_txctl |= PCN_TXCTL_OWN;
1002                         cur = frag;
1003                         PCN_INC(frag, PCN_TX_LIST_CNT);
1004                         cnt++;
1005                 }
1006         }
1007
1008         if (m != NULL)
1009                 return(ENOBUFS);
1010
1011         sc->pcn_cdata.pcn_tx_chain[cur] = m_head;
1012         sc->pcn_ldata->pcn_tx_list[cur].pcn_txctl |=
1013             PCN_TXCTL_ENP|PCN_TXCTL_ADD_FCS|PCN_TXCTL_MORE_LTINT;
1014         sc->pcn_ldata->pcn_tx_list[*txidx].pcn_txctl |= PCN_TXCTL_OWN;
1015         sc->pcn_cdata.pcn_tx_cnt += cnt;
1016         *txidx = frag;
1017
1018         return(0);
1019 }
1020
1021 /*
1022  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1023  * to the mbuf data regions directly in the transmit lists. We also save a
1024  * copy of the pointers since the transmit list fragment pointers are
1025  * physical addresses.
1026  */
1027 static void pcn_start(ifp)
1028         struct ifnet            *ifp;
1029 {
1030         struct pcn_softc        *sc;
1031         struct mbuf             *m_head = NULL;
1032         u_int32_t               idx;
1033
1034         sc = ifp->if_softc;
1035
1036         if (!sc->pcn_link)
1037                 return;
1038
1039         idx = sc->pcn_cdata.pcn_tx_prod;
1040
1041         if (ifp->if_flags & IFF_OACTIVE)
1042                 return;
1043
1044         while(sc->pcn_cdata.pcn_tx_chain[idx] == NULL) {
1045                 IF_DEQUEUE(&ifp->if_snd, m_head);
1046                 if (m_head == NULL)
1047                         break;
1048
1049                 if (pcn_encap(sc, m_head, &idx)) {
1050                         IF_PREPEND(&ifp->if_snd, m_head);
1051                         ifp->if_flags |= IFF_OACTIVE;
1052                         break;
1053                 }
1054
1055                 /*
1056                  * If there's a BPF listener, bounce a copy of this frame
1057                  * to him.
1058                  */
1059                 if (ifp->if_bpf)
1060                         bpf_mtap(ifp, m_head);
1061
1062         }
1063
1064         /* Transmit */
1065         sc->pcn_cdata.pcn_tx_prod = idx;
1066         pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_TX|PCN_CSR_INTEN);
1067
1068         /*
1069          * Set a timeout in case the chip goes out to lunch.
1070          */
1071         ifp->if_timer = 5;
1072
1073         return;
1074 }
1075
1076 void pcn_setfilt(ifp)
1077         struct ifnet            *ifp;
1078 {
1079         struct pcn_softc        *sc;
1080
1081         sc = ifp->if_softc;
1082
1083         /* If we want promiscuous mode, set the allframes bit. */
1084         if (ifp->if_flags & IFF_PROMISC) {
1085                 PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC);
1086         } else {
1087                 PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC);
1088         }
1089
1090         /* Set the capture broadcast bit to capture broadcast frames. */
1091         if (ifp->if_flags & IFF_BROADCAST) {
1092                 PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD);
1093         } else {
1094                 PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD);
1095         }
1096
1097         return;
1098 }
1099
1100 static void pcn_init(xsc)
1101         void                    *xsc;
1102 {
1103         struct pcn_softc        *sc = xsc;
1104         struct ifnet            *ifp = &sc->arpcom.ac_if;
1105         struct mii_data         *mii = NULL;
1106         int                     s;
1107
1108         s = splimp();
1109
1110         /*
1111          * Cancel pending I/O and free all RX/TX buffers.
1112          */
1113         pcn_stop(sc);
1114         pcn_reset(sc);
1115
1116         mii = device_get_softc(sc->pcn_miibus);
1117
1118         /* Set MAC address */
1119         pcn_csr_write(sc, PCN_CSR_PAR0,
1120             ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1121         pcn_csr_write(sc, PCN_CSR_PAR1,
1122             ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1123         pcn_csr_write(sc, PCN_CSR_PAR2,
1124             ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
1125
1126         /* Init circular RX list. */
1127         if (pcn_list_rx_init(sc) == ENOBUFS) {
1128                 printf("pcn%d: initialization failed: no "
1129                     "memory for rx buffers\n", sc->pcn_unit);
1130                 pcn_stop(sc);
1131                 (void)splx(s);
1132                 return;
1133         }
1134
1135         /* Set up RX filter. */
1136         pcn_setfilt(ifp);
1137
1138         /*
1139          * Init tx descriptors.
1140          */
1141         pcn_list_tx_init(sc);
1142
1143         /* Set up the mode register. */
1144         pcn_csr_write(sc, PCN_CSR_MODE, PCN_PORT_MII);
1145
1146         /*
1147          * Load the multicast filter.
1148          */
1149         pcn_setmulti(sc);
1150
1151         /*
1152          * Load the addresses of the RX and TX lists.
1153          */
1154         pcn_csr_write(sc, PCN_CSR_RXADDR0,
1155             vtophys(&sc->pcn_ldata->pcn_rx_list[0]) & 0xFFFF);
1156         pcn_csr_write(sc, PCN_CSR_RXADDR1,
1157             (vtophys(&sc->pcn_ldata->pcn_rx_list[0]) >> 16) & 0xFFFF);
1158         pcn_csr_write(sc, PCN_CSR_TXADDR0,
1159             vtophys(&sc->pcn_ldata->pcn_tx_list[0]) & 0xFFFF);
1160         pcn_csr_write(sc, PCN_CSR_TXADDR1,
1161             (vtophys(&sc->pcn_ldata->pcn_tx_list[0]) >> 16) & 0xFFFF);
1162
1163         /* Set the RX and TX ring sizes. */
1164         pcn_csr_write(sc, PCN_CSR_RXRINGLEN, (~PCN_RX_LIST_CNT) + 1);
1165         pcn_csr_write(sc, PCN_CSR_TXRINGLEN, (~PCN_TX_LIST_CNT) + 1);
1166
1167         /* We're not using the initialization block. */
1168         pcn_csr_write(sc, PCN_CSR_IAB1, 0);
1169
1170         /* Enable fast suspend mode. */
1171         PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL2, PCN_EXTCTL2_FASTSPNDE);
1172
1173         /*
1174          * Enable burst read and write. Also set the no underflow
1175          * bit. This will avoid transmit underruns in certain
1176          * conditions while still providing decent performance.
1177          */
1178         PCN_BCR_SETBIT(sc, PCN_BCR_BUSCTL, PCN_BUSCTL_NOUFLOW|
1179             PCN_BUSCTL_BREAD|PCN_BUSCTL_BWRITE);
1180
1181         /* Enable graceful recovery from underflow. */
1182         PCN_CSR_SETBIT(sc, PCN_CSR_IMR, PCN_IMR_DXSUFLO);
1183
1184         /* Enable auto-padding of short TX frames. */
1185         PCN_CSR_SETBIT(sc, PCN_CSR_TFEAT, PCN_TFEAT_PAD_TX);
1186
1187         /* Disable MII autoneg (we handle this ourselves). */
1188         PCN_BCR_SETBIT(sc, PCN_BCR_MIICTL, PCN_MIICTL_DANAS);
1189
1190         if (sc->pcn_type == Am79C978)
1191                 pcn_bcr_write(sc, PCN_BCR_PHYSEL,
1192                     PCN_PHYSEL_PCNET|PCN_PHY_HOMEPNA);
1193
1194         /* Enable interrupts and start the controller running. */
1195         pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_INTEN|PCN_CSR_START);
1196
1197         mii_mediachg(mii);
1198
1199         ifp->if_flags |= IFF_RUNNING;
1200         ifp->if_flags &= ~IFF_OACTIVE;
1201
1202         (void)splx(s);
1203         sc->pcn_stat_ch = timeout(pcn_tick, sc, hz);
1204
1205         return;
1206 }
1207
1208 /*
1209  * Set media options.
1210  */
1211 static int pcn_ifmedia_upd(ifp)
1212         struct ifnet            *ifp;
1213 {
1214         struct pcn_softc        *sc;
1215         struct mii_data         *mii;
1216
1217         sc = ifp->if_softc;
1218         mii = device_get_softc(sc->pcn_miibus);
1219
1220         sc->pcn_link = 0;
1221         if (mii->mii_instance) {
1222                 struct mii_softc        *miisc;
1223                 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1224                     miisc = LIST_NEXT(miisc, mii_list))
1225                         mii_phy_reset(miisc);
1226         }
1227         mii_mediachg(mii);
1228
1229         return(0);
1230 }
1231
1232 /*
1233  * Report current media status.
1234  */
1235 static void pcn_ifmedia_sts(ifp, ifmr)
1236         struct ifnet            *ifp;
1237         struct ifmediareq       *ifmr;
1238 {
1239         struct pcn_softc        *sc;
1240         struct mii_data         *mii;
1241
1242         sc = ifp->if_softc;
1243
1244         mii = device_get_softc(sc->pcn_miibus);
1245         mii_pollstat(mii);
1246         ifmr->ifm_active = mii->mii_media_active;
1247         ifmr->ifm_status = mii->mii_media_status;
1248
1249         return;
1250 }
1251
1252 static int pcn_ioctl(ifp, command, data, cr)
1253         struct ifnet            *ifp;
1254         u_long                  command;
1255         caddr_t                 data;
1256         struct ucred            *cr;
1257 {
1258         struct pcn_softc        *sc = ifp->if_softc;
1259         struct ifreq            *ifr = (struct ifreq *) data;
1260         struct mii_data         *mii = NULL;
1261         int                     s, error = 0;
1262
1263         s = splimp();
1264
1265         switch(command) {
1266         case SIOCSIFADDR:
1267         case SIOCGIFADDR:
1268         case SIOCSIFMTU:
1269                 error = ether_ioctl(ifp, command, data);
1270                 break;
1271         case SIOCSIFFLAGS:
1272                 if (ifp->if_flags & IFF_UP) {
1273                         if (ifp->if_flags & IFF_RUNNING &&
1274                             ifp->if_flags & IFF_PROMISC &&
1275                             !(sc->pcn_if_flags & IFF_PROMISC)) {
1276                                 PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1,
1277                                     PCN_EXTCTL1_SPND);
1278                                 pcn_setfilt(ifp);
1279                                 PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1,
1280                                     PCN_EXTCTL1_SPND);
1281                                 pcn_csr_write(sc, PCN_CSR_CSR,
1282                                     PCN_CSR_INTEN|PCN_CSR_START);
1283                         } else if (ifp->if_flags & IFF_RUNNING &&
1284                             !(ifp->if_flags & IFF_PROMISC) &&
1285                                 sc->pcn_if_flags & IFF_PROMISC) {
1286                                 PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1,
1287                                     PCN_EXTCTL1_SPND);
1288                                 pcn_setfilt(ifp);
1289                                 PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1,
1290                                     PCN_EXTCTL1_SPND);
1291                                 pcn_csr_write(sc, PCN_CSR_CSR,
1292                                     PCN_CSR_INTEN|PCN_CSR_START);
1293                         } else if (!(ifp->if_flags & IFF_RUNNING))
1294                                 pcn_init(sc);
1295                 } else {
1296                         if (ifp->if_flags & IFF_RUNNING)
1297                                 pcn_stop(sc);
1298                 }
1299                 sc->pcn_if_flags = ifp->if_flags;
1300                 error = 0;
1301                 break;
1302         case SIOCADDMULTI:
1303         case SIOCDELMULTI:
1304                 pcn_setmulti(sc);
1305                 error = 0;
1306                 break;
1307         case SIOCGIFMEDIA:
1308         case SIOCSIFMEDIA:
1309                 mii = device_get_softc(sc->pcn_miibus);
1310                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1311                 break;
1312         default:
1313                 error = EINVAL;
1314                 break;
1315         }
1316
1317         (void)splx(s);
1318
1319         return(error);
1320 }
1321
1322 static void pcn_watchdog(ifp)
1323         struct ifnet            *ifp;
1324 {
1325         struct pcn_softc        *sc;
1326
1327         sc = ifp->if_softc;
1328
1329         ifp->if_oerrors++;
1330         printf("pcn%d: watchdog timeout\n", sc->pcn_unit);
1331
1332         pcn_stop(sc);
1333         pcn_reset(sc);
1334         pcn_init(sc);
1335
1336         if (ifp->if_snd.ifq_head != NULL)
1337                 pcn_start(ifp);
1338
1339         return;
1340 }
1341
1342 /*
1343  * Stop the adapter and free any mbufs allocated to the
1344  * RX and TX lists.
1345  */
1346 static void pcn_stop(sc)
1347         struct pcn_softc        *sc;
1348 {
1349         int             i;
1350         struct ifnet            *ifp;
1351
1352         ifp = &sc->arpcom.ac_if;
1353         ifp->if_timer = 0;
1354
1355         untimeout(pcn_tick, sc, sc->pcn_stat_ch);
1356         PCN_CSR_SETBIT(sc, PCN_CSR_CSR, PCN_CSR_STOP);
1357         sc->pcn_link = 0;
1358
1359         /*
1360          * Free data in the RX lists.
1361          */
1362         for (i = 0; i < PCN_RX_LIST_CNT; i++) {
1363                 if (sc->pcn_cdata.pcn_rx_chain[i] != NULL) {
1364                         m_freem(sc->pcn_cdata.pcn_rx_chain[i]);
1365                         sc->pcn_cdata.pcn_rx_chain[i] = NULL;
1366                 }
1367         }
1368         bzero((char *)&sc->pcn_ldata->pcn_rx_list,
1369                 sizeof(sc->pcn_ldata->pcn_rx_list));
1370
1371         /*
1372          * Free the TX list buffers.
1373          */
1374         for (i = 0; i < PCN_TX_LIST_CNT; i++) {
1375                 if (sc->pcn_cdata.pcn_tx_chain[i] != NULL) {
1376                         m_freem(sc->pcn_cdata.pcn_tx_chain[i]);
1377                         sc->pcn_cdata.pcn_tx_chain[i] = NULL;
1378                 }
1379         }
1380
1381         bzero((char *)&sc->pcn_ldata->pcn_tx_list,
1382                 sizeof(sc->pcn_ldata->pcn_tx_list));
1383
1384         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1385
1386         return;
1387 }
1388
1389 /*
1390  * Stop all chip I/O so that the kernel's probe routines don't
1391  * get confused by errant DMAs when rebooting.
1392  */
1393 static void pcn_shutdown(dev)
1394         device_t                dev;
1395 {
1396         struct pcn_softc        *sc;
1397
1398         sc = device_get_softc(dev);
1399
1400         pcn_reset(sc);
1401         pcn_stop(sc);
1402
1403         return;
1404 }