]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/adm5120/if_admsw.c
4478 dtrace_dof_maxsize is far too small
[FreeBSD/FreeBSD.git] / sys / mips / adm5120 / if_admsw.c
1 /* $NetBSD: if_admsw.c,v 1.3 2007/04/22 19:26:25 dyoung Exp $ */
2
3 /*-
4  * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or
8  * without modification, are permitted provided that the following
9  * conditions are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above
13  *    copyright notice, this list of conditions and the following
14  *    disclaimer in the documentation and/or other materials provided
15  *    with the distribution.
16  * 3. The names of the authors may not be used to endorse or promote
17  *    products derived from this software without specific prior
18  *    written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
27  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  */
33 /*
34  * Copyright (c) 2001 Wasabi Systems, Inc.
35  * All rights reserved.
36  *
37  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *      This product includes software developed for the NetBSD Project by
50  *      Wasabi Systems, Inc.
51  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
52  *    or promote products derived from this software without specific prior
53  *    written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65  * POSSIBILITY OF SUCH DAMAGE.
66  */
67
68 /*
69  * Device driver for Alchemy Semiconductor Au1x00 Ethernet Media
70  * Access Controller.
71  *
72  * TODO:
73  *
74  *      Better Rx buffer management; we want to get new Rx buffers
75  *      to the chip more quickly than we currently do.
76  */
77
78 #include <sys/cdefs.h>
79 __FBSDID("$FreeBSD$");
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/bus.h>
84 #include <sys/kernel.h>
85 #include <sys/mbuf.h>
86 #include <sys/malloc.h>
87 #include <sys/module.h>
88 #include <sys/rman.h>
89 #include <sys/socket.h>
90 #include <sys/sockio.h>
91 #include <sys/sysctl.h>
92 #include <machine/bus.h>
93
94 #include <net/ethernet.h>
95 #include <net/if.h>
96 #include <net/if_arp.h>
97 #include <net/if_dl.h>
98 #include <net/if_media.h>
99 #include <net/if_mib.h>
100 #include <net/if_types.h>
101 #include <net/if_var.h>
102
103 #ifdef INET
104 #include <netinet/in.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/in_var.h>
107 #include <netinet/ip.h>
108 #endif
109
110 #include <net/bpf.h>
111 #include <net/bpfdesc.h>
112
113 #include <mips/adm5120/adm5120reg.h>
114 #include <mips/adm5120/if_admswreg.h>
115 #include <mips/adm5120/if_admswvar.h>
116
117 /* TODO: add locking */
118 #define ADMSW_LOCK(sc) do {} while(0);
119 #define ADMSW_UNLOCK(sc) do {} while(0);
120
121 static uint8_t vlan_matrix[SW_DEVS] = {
122         (1 << 6) | (1 << 0),            /* CPU + port0 */
123         (1 << 6) | (1 << 1),            /* CPU + port1 */
124         (1 << 6) | (1 << 2),            /* CPU + port2 */
125         (1 << 6) | (1 << 3),            /* CPU + port3 */
126         (1 << 6) | (1 << 4),            /* CPU + port4 */
127         (1 << 6) | (1 << 5),            /* CPU + port5 */
128 };
129
130 /* ifnet entry points */
131 static void     admsw_start(struct ifnet *);
132 static void     admsw_watchdog(void *);
133 static int      admsw_ioctl(struct ifnet *, u_long, caddr_t);
134 static void     admsw_init(void *);
135 static void     admsw_stop(struct ifnet *, int);
136
137 static void     admsw_reset(struct admsw_softc *);
138 static void     admsw_set_filter(struct admsw_softc *);
139
140 static void     admsw_txintr(struct admsw_softc *, int);
141 static void     admsw_rxintr(struct admsw_softc *, int);
142 static int      admsw_add_rxbuf(struct admsw_softc *, int, int);
143 #define admsw_add_rxhbuf(sc, idx)       admsw_add_rxbuf(sc, idx, 1)
144 #define admsw_add_rxlbuf(sc, idx)       admsw_add_rxbuf(sc, idx, 0)
145
146 static int      admsw_mediachange(struct ifnet *);
147 static void     admsw_mediastatus(struct ifnet *, struct ifmediareq *);
148
149 static int      admsw_intr(void *);
150
151 /* bus entry points */
152 static int      admsw_probe(device_t dev);
153 static int      admsw_attach(device_t dev);
154 static int      admsw_detach(device_t dev);
155 static int      admsw_shutdown(device_t dev);
156
157 static void
158 admsw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
159 {
160         uint32_t *addr;
161
162         if (error)
163                 return;
164
165         KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
166         addr = arg;
167         *addr = segs->ds_addr;
168 }
169
170 static void
171 admsw_rxbuf_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
172 {
173         struct admsw_descsoft *ds;
174
175         if (error)
176                 return;
177
178         KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
179
180         ds = arg;
181         ds->ds_nsegs = nseg;
182         ds->ds_addr[0] = segs[0].ds_addr;
183         ds->ds_len[0] = segs[0].ds_len;
184
185 }
186
187 static void
188 admsw_mbuf_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, 
189     bus_size_t mapsize, int error)
190 {
191         struct admsw_descsoft *ds;
192
193         if (error)
194                 return;
195
196         ds = arg;
197
198         if((nseg != 1) && (nseg != 2))
199                 panic("%s: nseg == %d\n", __func__, nseg);
200
201         ds->ds_nsegs = nseg;
202         ds->ds_addr[0] = segs[0].ds_addr;
203         ds->ds_len[0] = segs[0].ds_len;
204
205         if(nseg > 1) {
206                 ds->ds_addr[1] = segs[1].ds_addr;
207                 ds->ds_len[1] = segs[1].ds_len;
208         }
209 }
210
211
212
213 static int
214 admsw_probe(device_t dev)
215 {
216
217         device_set_desc(dev, "ADM5120 Switch Engine");
218         return (0);
219 }
220
221 #define REG_READ(o)     bus_read_4((sc)->mem_res, (o))
222 #define REG_WRITE(o,v)  bus_write_4((sc)->mem_res, (o),(v))
223
224 static void
225 admsw_init_bufs(struct admsw_softc *sc)
226 {
227         int i;
228         struct admsw_desc *desc;
229
230         for (i = 0; i < ADMSW_NTXHDESC; i++) {
231                 if (sc->sc_txhsoft[i].ds_mbuf != NULL) {
232                         m_freem(sc->sc_txhsoft[i].ds_mbuf);
233                         sc->sc_txhsoft[i].ds_mbuf = NULL;
234                 }
235                 desc = &sc->sc_txhdescs[i];
236                 desc->data = 0;
237                 desc->cntl = 0;
238                 desc->len = MAC_BUFLEN;
239                 desc->status = 0;
240                 ADMSW_CDTXHSYNC(sc, i,
241                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
242         }
243         sc->sc_txhdescs[ADMSW_NTXHDESC - 1].data |= ADM5120_DMA_RINGEND;
244         ADMSW_CDTXHSYNC(sc, ADMSW_NTXHDESC - 1,
245             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
246
247         for (i = 0; i < ADMSW_NRXHDESC; i++) {
248                 if (sc->sc_rxhsoft[i].ds_mbuf == NULL) {
249                         if (admsw_add_rxhbuf(sc, i) != 0)
250                                 panic("admsw_init_bufs\n");
251                 } else
252                         ADMSW_INIT_RXHDESC(sc, i);
253         }
254
255         for (i = 0; i < ADMSW_NTXLDESC; i++) {
256                 if (sc->sc_txlsoft[i].ds_mbuf != NULL) {
257                         m_freem(sc->sc_txlsoft[i].ds_mbuf);
258                         sc->sc_txlsoft[i].ds_mbuf = NULL;
259                 }
260                 desc = &sc->sc_txldescs[i];
261                 desc->data = 0;
262                 desc->cntl = 0;
263                 desc->len = MAC_BUFLEN;
264                 desc->status = 0;
265                 ADMSW_CDTXLSYNC(sc, i,
266                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
267         }
268         sc->sc_txldescs[ADMSW_NTXLDESC - 1].data |= ADM5120_DMA_RINGEND;
269         ADMSW_CDTXLSYNC(sc, ADMSW_NTXLDESC - 1,
270             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
271
272         for (i = 0; i < ADMSW_NRXLDESC; i++) {
273                 if (sc->sc_rxlsoft[i].ds_mbuf == NULL) {
274                         if (admsw_add_rxlbuf(sc, i) != 0)
275                                 panic("admsw_init_bufs\n");
276                 } else
277                         ADMSW_INIT_RXLDESC(sc, i);
278         }
279
280         REG_WRITE(SEND_HBADDR_REG, ADMSW_CDTXHADDR(sc, 0));
281         REG_WRITE(SEND_LBADDR_REG, ADMSW_CDTXLADDR(sc, 0));
282         REG_WRITE(RECV_HBADDR_REG, ADMSW_CDRXHADDR(sc, 0));
283         REG_WRITE(RECV_LBADDR_REG, ADMSW_CDRXLADDR(sc, 0));
284
285         sc->sc_txfree = ADMSW_NTXLDESC;
286         sc->sc_txnext = 0;
287         sc->sc_txdirty = 0;
288         sc->sc_rxptr = 0;
289 }
290
291 static void
292 admsw_setvlan(struct admsw_softc *sc, char matrix[6])
293 {
294         uint32_t i;
295
296         i = matrix[0] + (matrix[1] << 8) + (matrix[2] << 16) + (matrix[3] << 24);
297         REG_WRITE(VLAN_G1_REG, i);
298         i = matrix[4] + (matrix[5] << 8);
299         REG_WRITE(VLAN_G2_REG, i);
300 }
301
302 static void
303 admsw_reset(struct admsw_softc *sc)
304 {
305         uint32_t wdog1;
306         int i;
307
308         REG_WRITE(PORT_CONF0_REG,
309             REG_READ(PORT_CONF0_REG) | PORT_CONF0_DP_MASK);
310         REG_WRITE(CPUP_CONF_REG,
311             REG_READ(CPUP_CONF_REG) | CPUP_CONF_DCPUP);
312
313         /* Wait for DMA to complete.  Overkill.  In 3ms, we can
314          * send at least two entire 1500-byte packets at 10 Mb/s.
315          */
316         DELAY(3000);
317
318         /* The datasheet recommends that we move all PHYs to reset
319          * state prior to software reset.
320          */
321         REG_WRITE(PHY_CNTL2_REG,
322             REG_READ(PHY_CNTL2_REG) & ~PHY_CNTL2_PHYR_MASK);
323
324         /* Reset the switch. */
325         REG_WRITE(ADMSW_SW_RES, 0x1);
326
327         DELAY(100 * 1000);
328
329         REG_WRITE(ADMSW_BOOT_DONE, ADMSW_BOOT_DONE_BO);
330
331         /* begin old code */
332         REG_WRITE(CPUP_CONF_REG,
333             CPUP_CONF_DCPUP | CPUP_CONF_CRCP | CPUP_CONF_DUNP_MASK |
334             CPUP_CONF_DMCP_MASK);
335
336         REG_WRITE(PORT_CONF0_REG, PORT_CONF0_EMCP_MASK | PORT_CONF0_EMBP_MASK);
337
338         REG_WRITE(PHY_CNTL2_REG,
339             REG_READ(PHY_CNTL2_REG) | PHY_CNTL2_ANE_MASK | PHY_CNTL2_PHYR_MASK |
340             PHY_CNTL2_AMDIX_MASK);
341
342         REG_WRITE(PHY_CNTL3_REG, REG_READ(PHY_CNTL3_REG) | PHY_CNTL3_RNT);
343
344         REG_WRITE(ADMSW_INT_MASK, INT_MASK);
345         REG_WRITE(ADMSW_INT_ST, INT_MASK);
346
347         /*
348          * While in DDB, we stop servicing interrupts, RX ring
349          * fills up and when free block counter falls behind FC
350          * threshold, the switch starts to emit 802.3x PAUSE
351          * frames.  This can upset peer switches.
352          *
353          * Stop this from happening by disabling FC and D2
354          * thresholds.
355          */
356         REG_WRITE(FC_TH_REG,
357             REG_READ(FC_TH_REG) & ~(FC_TH_FCS_MASK | FC_TH_D2S_MASK));
358
359         admsw_setvlan(sc, vlan_matrix);
360
361         for (i = 0; i < SW_DEVS; i++) {
362                 REG_WRITE(MAC_WT1_REG,
363                     sc->sc_enaddr[2] |
364                     (sc->sc_enaddr[3]<<8) |
365                     (sc->sc_enaddr[4]<<16) |
366                     ((sc->sc_enaddr[5]+i)<<24));
367                 REG_WRITE(MAC_WT0_REG, (i<<MAC_WT0_VLANID_SHIFT) |
368                     (sc->sc_enaddr[0]<<16) | (sc->sc_enaddr[1]<<24) |
369                     MAC_WT0_WRITE | MAC_WT0_VLANID_EN);
370
371                 while (!(REG_READ(MAC_WT0_REG) & MAC_WT0_WRITE_DONE));
372         }
373
374         wdog1 = REG_READ(ADM5120_WDOG1);
375         REG_WRITE(ADM5120_WDOG1, wdog1 & ~ADM5120_WDOG1_WDE);
376 }
377
378 static int
379 admsw_attach(device_t dev)
380 {
381         uint8_t enaddr[ETHER_ADDR_LEN];
382         struct admsw_softc *sc = (struct admsw_softc *) device_get_softc(dev);
383         struct ifnet *ifp;
384         int error, i, rid;
385
386         sc->sc_dev = dev;
387         device_printf(dev, "ADM5120 Switch Engine, %d ports\n", SW_DEVS);
388         sc->ndevs = 0;
389
390         /* XXXMIPS: fix it */
391         enaddr[0] = 0x00;
392         enaddr[1] = 0x0C;
393         enaddr[2] = 0x42;
394         enaddr[3] = 0x07;
395         enaddr[4] = 0xB2;
396         enaddr[5] = 0x4E;
397
398         memcpy(sc->sc_enaddr, enaddr, sizeof(sc->sc_enaddr));
399
400         device_printf(sc->sc_dev, "base Ethernet address %s\n",
401             ether_sprintf(enaddr));
402         callout_init(&sc->sc_watchdog, 1);
403
404         rid = 0;
405         if ((sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 
406             RF_ACTIVE)) == NULL) {
407                 device_printf(dev, "unable to allocate memory resource\n");
408                 return (ENXIO);
409         }
410
411         /* Hook up the interrupt handler. */
412         rid = 0;
413         if ((sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 
414             RF_SHAREABLE | RF_ACTIVE)) == NULL) {
415                 device_printf(dev, "unable to allocate IRQ resource\n");
416                 return (ENXIO);
417         }
418
419         if ((error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, 
420             admsw_intr, NULL, sc, &sc->sc_ih)) != 0) {
421                 device_printf(dev, 
422                     "WARNING: unable to register interrupt handler\n");
423                 return (error);
424         }
425
426         /*
427          * Allocate the control data structures, and create and load the
428          * DMA map for it.
429          */
430         if ((error = bus_dma_tag_create(NULL, 4, 0, 
431             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
432             NULL, NULL, sizeof(struct admsw_control_data), 1,
433             sizeof(struct admsw_control_data), 0, NULL, NULL, 
434             &sc->sc_control_dmat)) != 0) {
435                 device_printf(sc->sc_dev, 
436                     "unable to create control data DMA map, error = %d\n", 
437                     error);
438                 return (error);
439         }
440
441         if ((error = bus_dmamem_alloc(sc->sc_control_dmat,
442             (void **)&sc->sc_control_data, BUS_DMA_NOWAIT, 
443             &sc->sc_cddmamap)) != 0) {
444                 device_printf(sc->sc_dev, 
445                     "unable to allocate control data, error = %d\n", error);
446                 return (error);
447         }
448
449         if ((error = bus_dmamap_load(sc->sc_control_dmat, sc->sc_cddmamap,
450             sc->sc_control_data, sizeof(struct admsw_control_data), 
451             admsw_dma_map_addr, &sc->sc_cddma, 0)) != 0) {
452                 device_printf(sc->sc_dev, 
453                     "unable to load control data DMA map, error = %d\n", error);
454                 return (error);
455         }
456
457         /*
458          * Create the transmit buffer DMA maps.
459          */
460         if ((error = bus_dma_tag_create(NULL, 1, 0, 
461             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
462             NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, 
463             &sc->sc_bufs_dmat)) != 0) {
464                 device_printf(sc->sc_dev, 
465                     "unable to create control data DMA map, error = %d\n", 
466                     error);
467                 return (error);
468         }
469
470         for (i = 0; i < ADMSW_NTXHDESC; i++) {
471                 if ((error = bus_dmamap_create(sc->sc_bufs_dmat, 0,
472                     &sc->sc_txhsoft[i].ds_dmamap)) != 0) {
473                         device_printf(sc->sc_dev, 
474                             "unable to create txh DMA map %d, error = %d\n", 
475                             i, error);
476                         return (error);
477                 }
478                 sc->sc_txhsoft[i].ds_mbuf = NULL;
479         }
480
481         for (i = 0; i < ADMSW_NTXLDESC; i++) {
482                 if ((error = bus_dmamap_create(sc->sc_bufs_dmat, 0,
483                     &sc->sc_txlsoft[i].ds_dmamap)) != 0) {
484                         device_printf(sc->sc_dev, 
485                             "unable to create txl DMA map %d, error = %d\n", 
486                             i, error);
487                         return (error);
488                 }
489                 sc->sc_txlsoft[i].ds_mbuf = NULL;
490         }
491
492         /*
493          * Create the receive buffer DMA maps.
494          */
495         for (i = 0; i < ADMSW_NRXHDESC; i++) {
496                 if ((error = bus_dmamap_create(sc->sc_bufs_dmat, 0, 
497                      &sc->sc_rxhsoft[i].ds_dmamap)) != 0) {
498                         device_printf(sc->sc_dev, 
499                             "unable to create rxh DMA map %d, error = %d\n", 
500                             i, error);
501                         return (error);
502                 }
503                 sc->sc_rxhsoft[i].ds_mbuf = NULL;
504         }
505
506         for (i = 0; i < ADMSW_NRXLDESC; i++) {
507                 if ((error = bus_dmamap_create(sc->sc_bufs_dmat, 0,
508                     &sc->sc_rxlsoft[i].ds_dmamap)) != 0) {
509                         device_printf(sc->sc_dev, 
510                             "unable to create rxl DMA map %d, error = %d\n",
511                             i, error);
512                         return (error);
513                 }
514                 sc->sc_rxlsoft[i].ds_mbuf = NULL;
515         }
516
517         admsw_init_bufs(sc);
518         admsw_reset(sc);
519
520         for (i = 0; i < SW_DEVS; i++) {
521                 ifmedia_init(&sc->sc_ifmedia[i], 0, admsw_mediachange, 
522                     admsw_mediastatus);
523                 ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_10_T, 0, NULL);
524                 ifmedia_add(&sc->sc_ifmedia[i], 
525                     IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
526                 ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_100_TX, 0, NULL);
527                 ifmedia_add(&sc->sc_ifmedia[i], 
528                     IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
529                 ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_AUTO, 0, NULL);
530                 ifmedia_set(&sc->sc_ifmedia[i], IFM_ETHER|IFM_AUTO);
531
532                 ifp = sc->sc_ifnet[i] = if_alloc(IFT_ETHER);
533
534                 /* Setup interface parameters */
535                 ifp->if_softc = sc;
536                 if_initname(ifp, device_get_name(dev), i);
537                 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
538                 ifp->if_ioctl = admsw_ioctl;
539                 ifp->if_output = ether_output;
540                 ifp->if_start = admsw_start;
541                 ifp->if_init = admsw_init;
542                 ifp->if_mtu = ETHERMTU;
543                 ifp->if_baudrate = IF_Mbps(100);
544                 IFQ_SET_MAXLEN(&ifp->if_snd, max(ADMSW_NTXLDESC, ifqmaxlen));
545                 ifp->if_snd.ifq_drv_maxlen = max(ADMSW_NTXLDESC, ifqmaxlen);
546                 IFQ_SET_READY(&ifp->if_snd);
547                 ifp->if_capabilities |= IFCAP_VLAN_MTU;
548
549                 /* Attach the interface. */
550                 ether_ifattach(ifp, enaddr);
551                 enaddr[5]++;
552         }
553
554         /* XXX: admwdog_attach(sc); */
555
556         /* leave interrupts and cpu port disabled */
557         return (0);
558 }
559
560 static int
561 admsw_detach(device_t dev)
562 {
563
564         printf("TODO: DETACH\n");
565         return (0);
566 }
567
568 /*
569  * admsw_shutdown:
570  *
571  *      Make sure the interface is stopped at reboot time.
572  */
573 static int
574 admsw_shutdown(device_t dev)
575 {
576         struct admsw_softc *sc;
577         int i;
578
579         sc = device_get_softc(dev);
580         for (i = 0; i < SW_DEVS; i++)
581                 admsw_stop(sc->sc_ifnet[i], 1);
582
583         return (0);
584 }
585
586 /*
587  * admsw_start:         [ifnet interface function]
588  *
589  *      Start packet transmission on the interface.
590  */
591 static void
592 admsw_start(struct ifnet *ifp)
593 {
594         struct admsw_softc *sc = ifp->if_softc;
595         struct mbuf *m0, *m;
596         struct admsw_descsoft *ds;
597         struct admsw_desc *desc;
598         bus_dmamap_t dmamap;
599         struct ether_header *eh;
600         int error, nexttx, len, i;
601         static int vlan = 0;
602
603         /*
604          * Loop through the send queues, setting up transmit descriptors
605          * unitl we drain the queues, or use up all available transmit
606          * descriptors.
607          */
608         for (;;) {
609                 vlan++;
610                 if (vlan == SW_DEVS)
611                         vlan = 0;
612                 i = vlan;
613                 for (;;) {
614                         ifp = sc->sc_ifnet[i];
615                         if ((ifp->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) 
616                             == IFF_DRV_RUNNING) {
617                                 /* Grab a packet off the queue. */
618                                 IF_DEQUEUE(&ifp->if_snd, m0);
619                                 if (m0 != NULL)
620                                         break;
621                         }
622                         i++;
623                         if (i == SW_DEVS)
624                                 i = 0;
625                         if (i == vlan)
626                                 return;
627                 }
628                 vlan = i;
629                 m = NULL;
630
631                 /* Get a spare descriptor. */
632                 if (sc->sc_txfree == 0) {
633                         /* No more slots left; notify upper layer. */
634                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
635                         break;
636                 }
637                 nexttx = sc->sc_txnext;
638                 desc = &sc->sc_txldescs[nexttx];
639                 ds = &sc->sc_txlsoft[nexttx];
640                 dmamap = ds->ds_dmamap;
641
642                 /*
643                  * Load the DMA map.  If this fails, the packet either
644                  * didn't fit in the alloted number of segments, or we
645                  * were short on resources.  In this case, we'll copy
646                  * and try again.
647                  */
648                 if (m0->m_pkthdr.len < ETHER_MIN_LEN ||
649                     bus_dmamap_load_mbuf(sc->sc_bufs_dmat, dmamap, m0,
650                     admsw_mbuf_map_addr, ds, BUS_DMA_NOWAIT) != 0) {
651                         MGETHDR(m, M_NOWAIT, MT_DATA);
652                         if (m == NULL) {
653                                 device_printf(sc->sc_dev, 
654                                     "unable to allocate Tx mbuf\n");
655                                 break;
656                         }
657                         if (m0->m_pkthdr.len > MHLEN) {
658                                 MCLGET(m, M_NOWAIT);
659                                 if ((m->m_flags & M_EXT) == 0) {
660                                         device_printf(sc->sc_dev, 
661                                             "unable to allocate Tx cluster\n");
662                                         m_freem(m);
663                                         break;
664                                 }
665                         }
666                         m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
667                         m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
668                         m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
669                         if (m->m_pkthdr.len < ETHER_MIN_LEN) {
670                                 if (M_TRAILINGSPACE(m) < ETHER_MIN_LEN - m->m_pkthdr.len)
671                                         panic("admsw_start: M_TRAILINGSPACE\n");
672                                 memset(mtod(m, uint8_t *) + m->m_pkthdr.len, 0,
673                                     ETHER_MIN_LEN - ETHER_CRC_LEN - m->m_pkthdr.len);
674                                 m->m_pkthdr.len = m->m_len = ETHER_MIN_LEN;
675                         }
676                         error = bus_dmamap_load_mbuf(sc->sc_bufs_dmat, 
677                             dmamap, m, admsw_mbuf_map_addr, ds, BUS_DMA_NOWAIT);
678                         if (error) {
679                                 device_printf(sc->sc_dev, 
680                                     "unable to load Tx buffer, error = %d\n", 
681                                     error);
682                                 break;
683                         }
684                 }
685
686                 if (m != NULL) {
687                         m_freem(m0);
688                         m0 = m;
689                 }
690
691                 /*
692                  * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
693                  */
694
695                 /* Sync the DMA map. */
696                 bus_dmamap_sync(sc->sc_bufs_dmat, dmamap, BUS_DMASYNC_PREWRITE);
697
698                 if (ds->ds_nsegs != 1 && ds->ds_nsegs != 2)
699                         panic("admsw_start: nsegs == %d\n", ds->ds_nsegs);
700                 desc->data = ds->ds_addr[0];
701                 desc->len = len = ds->ds_len[0];
702                 if (ds->ds_nsegs > 1) {
703                         len += ds->ds_len[1];
704                         desc->cntl = ds->ds_addr[1] | ADM5120_DMA_BUF2ENABLE;
705                 } else
706                         desc->cntl = 0;
707                 desc->status = (len << ADM5120_DMA_LENSHIFT) | (1 << vlan);
708                 eh = mtod(m0, struct ether_header *);
709                 if (ntohs(eh->ether_type) == ETHERTYPE_IP &&
710                     m0->m_pkthdr.csum_flags & CSUM_IP)
711                         desc->status |= ADM5120_DMA_CSUM;
712                 if (nexttx == ADMSW_NTXLDESC - 1)
713                         desc->data |= ADM5120_DMA_RINGEND;
714                 desc->data |= ADM5120_DMA_OWN;
715
716                 /* Sync the descriptor. */
717                 ADMSW_CDTXLSYNC(sc, nexttx,
718                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
719
720                 REG_WRITE(SEND_TRIG_REG, 1);
721                 /* printf("send slot %d\n",nexttx); */
722
723                 /*
724                  * Store a pointer to the packet so we can free it later.
725                  */
726                 ds->ds_mbuf = m0;
727
728                 /* Advance the Tx pointer. */
729                 sc->sc_txfree--;
730                 sc->sc_txnext = ADMSW_NEXTTXL(nexttx);
731
732                 /* Pass the packet to any BPF listeners. */
733                 BPF_MTAP(ifp, m0);
734
735                 /* Set a watchdog timer in case the chip flakes out. */
736                 sc->sc_timer = 5;
737         }
738 }
739
740 /*
741  * admsw_watchdog:      [ifnet interface function]
742  *
743  *      Watchdog timer handler.
744  */
745 static void
746 admsw_watchdog(void *arg)
747 {
748         struct admsw_softc *sc = arg;
749         struct ifnet *ifp;
750         int vlan;
751
752         callout_reset(&sc->sc_watchdog, hz, admsw_watchdog, sc);
753         if (sc->sc_timer == 0 || --sc->sc_timer > 0)
754                 return;
755
756         /* Check if an interrupt was lost. */
757         if (sc->sc_txfree == ADMSW_NTXLDESC) {
758                 device_printf(sc->sc_dev, "watchdog false alarm\n");
759                 return;
760         }
761         if (sc->sc_timer != 0)
762                 device_printf(sc->sc_dev, "watchdog timer is %d!\n",  
763                     sc->sc_timer);
764         admsw_txintr(sc, 0);
765         if (sc->sc_txfree == ADMSW_NTXLDESC) {
766                 device_printf(sc->sc_dev, "tx IRQ lost (queue empty)\n");
767                 return;
768         }
769         if (sc->sc_timer != 0) {
770                 device_printf(sc->sc_dev, "tx IRQ lost (timer recharged)\n");
771                 return;
772         }
773
774         device_printf(sc->sc_dev, "device timeout, txfree = %d\n",  
775             sc->sc_txfree);
776         for (vlan = 0; vlan < SW_DEVS; vlan++)
777                 admsw_stop(sc->sc_ifnet[vlan], 0);
778         admsw_init(sc);
779
780         ifp = sc->sc_ifnet[0];
781
782         /* Try to get more packets going. */
783         admsw_start(ifp);
784 }
785
786 /*
787  * admsw_ioctl:         [ifnet interface function]
788  *
789  *      Handle control requests from the operator.
790  */
791 static int
792 admsw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
793 {
794         struct admsw_softc *sc = ifp->if_softc;
795         struct ifdrv *ifd;
796         int error, port;
797
798         ADMSW_LOCK(sc);
799
800         switch (cmd) {
801         case SIOCSIFMEDIA:
802         case SIOCGIFMEDIA:
803                 port = 0;
804                 while(port < SW_DEVS)
805                         if(ifp == sc->sc_ifnet[port])
806                                  break;
807                         else 
808                                 port++;
809                 if (port >= SW_DEVS)
810                         error = EOPNOTSUPP;
811                 else
812                         error = ifmedia_ioctl(ifp, (struct ifreq *)data,
813                             &sc->sc_ifmedia[port], cmd);
814                 break;
815
816         case SIOCGDRVSPEC:
817         case SIOCSDRVSPEC:
818                 ifd = (struct ifdrv *) data;
819                 if (ifd->ifd_cmd != 0 || ifd->ifd_len != sizeof(vlan_matrix)) {
820                         error = EINVAL;
821                         break;
822                 }
823                 if (cmd == SIOCGDRVSPEC) {
824                         error = copyout(vlan_matrix, ifd->ifd_data,
825                             sizeof(vlan_matrix));
826                 } else {
827                         error = copyin(ifd->ifd_data, vlan_matrix,
828                             sizeof(vlan_matrix));
829                         admsw_setvlan(sc, vlan_matrix);
830                 }
831                 break;
832
833         default:
834                 error = ether_ioctl(ifp, cmd, data);
835                 if (error == ENETRESET) {
836                         /*
837                          * Multicast list has changed; set the hardware filter
838                          * accordingly.
839                          */
840                         admsw_set_filter(sc);
841                         error = 0;
842                 }
843                 break;
844         }
845
846         /* Try to get more packets going. */
847         admsw_start(ifp);
848
849         ADMSW_UNLOCK(sc);
850         return (error);
851 }
852
853
854 /*
855  * admsw_intr:
856  *
857  *      Interrupt service routine.
858  */
859 static int
860 admsw_intr(void *arg)
861 {
862         struct admsw_softc *sc = arg;
863         uint32_t pending;
864
865         pending = REG_READ(ADMSW_INT_ST);
866         REG_WRITE(ADMSW_INT_ST, pending);
867
868         if (sc->ndevs == 0)
869                 return (FILTER_STRAY);
870
871         if ((pending & ADMSW_INTR_RHD) != 0)
872                 admsw_rxintr(sc, 1);
873
874         if ((pending & ADMSW_INTR_RLD) != 0)
875                 admsw_rxintr(sc, 0);
876
877         if ((pending & ADMSW_INTR_SHD) != 0)
878                 admsw_txintr(sc, 1);
879
880         if ((pending & ADMSW_INTR_SLD) != 0)
881                 admsw_txintr(sc, 0);
882
883         return (FILTER_HANDLED);
884 }
885
886 /*
887  * admsw_txintr:
888  *
889  *      Helper; handle transmit interrupts.
890  */
891 static void
892 admsw_txintr(struct admsw_softc *sc, int prio)
893 {
894         struct ifnet *ifp;
895         struct admsw_desc *desc;
896         struct admsw_descsoft *ds;
897         int i, vlan;
898         int gotone = 0;
899
900         /* printf("txintr: txdirty: %d, txfree: %d\n",sc->sc_txdirty, sc->sc_txfree); */
901         for (i = sc->sc_txdirty; sc->sc_txfree != ADMSW_NTXLDESC;
902             i = ADMSW_NEXTTXL(i)) {
903
904                 ADMSW_CDTXLSYNC(sc, i,
905                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
906
907                 desc = &sc->sc_txldescs[i];
908                 ds = &sc->sc_txlsoft[i];
909                 if (desc->data & ADM5120_DMA_OWN) {
910                         ADMSW_CDTXLSYNC(sc, i,
911                             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
912                         break;
913                 }
914
915                 bus_dmamap_sync(sc->sc_bufs_dmat, ds->ds_dmamap, 
916                     BUS_DMASYNC_POSTWRITE);
917                 bus_dmamap_unload(sc->sc_bufs_dmat, ds->ds_dmamap);
918                 m_freem(ds->ds_mbuf);
919                 ds->ds_mbuf = NULL;
920
921                 vlan = ffs(desc->status & 0x3f) - 1;
922                 if (vlan < 0 || vlan >= SW_DEVS)
923                         panic("admsw_txintr: bad vlan\n");
924                 ifp = sc->sc_ifnet[vlan];
925                 gotone = 1;
926                 /* printf("clear tx slot %d\n",i); */
927
928                 ifp->if_opackets++;
929
930                 sc->sc_txfree++;
931         }
932
933         if (gotone) {
934                 sc->sc_txdirty = i;
935                 for (vlan = 0; vlan < SW_DEVS; vlan++)
936                         sc->sc_ifnet[vlan]->if_drv_flags &= ~IFF_DRV_OACTIVE;
937
938                 ifp = sc->sc_ifnet[0];
939
940                 /* Try to queue more packets. */
941                 admsw_start(ifp);
942
943                 /*
944                  * If there are no more pending transmissions,
945                  * cancel the watchdog timer.
946                  */
947                 if (sc->sc_txfree == ADMSW_NTXLDESC)
948                         sc->sc_timer = 0;
949
950         }
951
952         /* printf("txintr end: txdirty: %d, txfree: %d\n",sc->sc_txdirty, sc->sc_txfree); */
953 }
954
955 /*
956  * admsw_rxintr:
957  *
958  *      Helper; handle receive interrupts.
959  */
960 static void
961 admsw_rxintr(struct admsw_softc *sc, int high)
962 {
963         struct ifnet *ifp;
964         struct admsw_descsoft *ds;
965         struct mbuf *m;
966         uint32_t stat;
967         int i, len, port, vlan;
968
969         /* printf("rxintr\n"); */
970
971         if (high)
972                 panic("admsw_rxintr: high priority packet\n");
973
974 #if 1
975         ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, 
976             BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
977         if ((sc->sc_rxldescs[sc->sc_rxptr].data & ADM5120_DMA_OWN) == 0)
978                 ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, 
979                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
980         else {
981                 i = sc->sc_rxptr;
982                 do {
983                         ADMSW_CDRXLSYNC(sc, i, 
984                             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
985                         i = ADMSW_NEXTRXL(i);
986                         /* the ring is empty, just return. */
987                         if (i == sc->sc_rxptr)
988                                 return;
989                         ADMSW_CDRXLSYNC(sc, i, 
990                             BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
991                 } while (sc->sc_rxldescs[i].data & ADM5120_DMA_OWN);
992
993                 ADMSW_CDRXLSYNC(sc, i, 
994                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
995
996                 ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, 
997                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
998
999                 if ((sc->sc_rxldescs[sc->sc_rxptr].data & ADM5120_DMA_OWN) == 0)
1000                         ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, 
1001                             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1002                 else {
1003                         ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, 
1004                             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1005                         /* We've fallen behind the chip: catch it. */
1006 #if 0
1007                         device_printf(sc->sc_dev, 
1008                            "RX ring resync, base=%x, work=%x, %d -> %d\n",
1009                             REG_READ(RECV_LBADDR_REG),
1010                             REG_READ(RECV_LWADDR_REG), sc->sc_rxptr, i);
1011 #endif
1012                         sc->sc_rxptr = i;
1013                         /* ADMSW_EVCNT_INCR(&sc->sc_ev_rxsync); */
1014                 }
1015         }
1016 #endif
1017         for (i = sc->sc_rxptr;; i = ADMSW_NEXTRXL(i)) {
1018                 ds = &sc->sc_rxlsoft[i];
1019
1020                 ADMSW_CDRXLSYNC(sc, i, 
1021                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1022
1023                 if (sc->sc_rxldescs[i].data & ADM5120_DMA_OWN) {
1024                         ADMSW_CDRXLSYNC(sc, i, 
1025                             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1026                         break;
1027                 }
1028
1029                 /* printf("process slot %d\n",i); */
1030
1031                 bus_dmamap_sync(sc->sc_bufs_dmat, ds->ds_dmamap,
1032                     BUS_DMASYNC_POSTREAD);
1033
1034                 stat = sc->sc_rxldescs[i].status;
1035                 len = (stat & ADM5120_DMA_LEN) >> ADM5120_DMA_LENSHIFT;
1036                 len -= ETHER_CRC_LEN;
1037                 port = (stat & ADM5120_DMA_PORTID) >> ADM5120_DMA_PORTSHIFT;
1038
1039                 for (vlan = 0; vlan < SW_DEVS; vlan++)
1040                         if ((1 << port) & vlan_matrix[vlan])
1041                                 break;
1042
1043                 if (vlan == SW_DEVS)
1044                         vlan = 0;
1045
1046                 ifp = sc->sc_ifnet[vlan];
1047
1048                 m = ds->ds_mbuf;
1049                 if (admsw_add_rxlbuf(sc, i) != 0) {
1050                         ifp->if_ierrors++;
1051                         ADMSW_INIT_RXLDESC(sc, i);
1052                         bus_dmamap_sync(sc->sc_bufs_dmat, ds->ds_dmamap,
1053                             BUS_DMASYNC_PREREAD);
1054                         continue;
1055                 }
1056
1057                 m->m_pkthdr.rcvif = ifp;
1058                 m->m_pkthdr.len = m->m_len = len;
1059                 if ((stat & ADM5120_DMA_TYPE) == ADM5120_DMA_TYPE_IP) {
1060                         m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1061                         if (!(stat & ADM5120_DMA_CSUMFAIL))
1062                                 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1063                 }
1064
1065                 BPF_MTAP(ifp, m);
1066
1067                 /* Pass it on. */
1068                 (*ifp->if_input)(ifp, m);
1069                 ifp->if_ipackets++;
1070         }
1071
1072         /* Update the receive pointer. */
1073         sc->sc_rxptr = i;
1074 }
1075
1076 /*
1077  * admsw_init:          [ifnet interface function]
1078  *
1079  *      Initialize the interface.
1080  */
1081 static void
1082 admsw_init(void *xsc)
1083 {
1084         struct admsw_softc *sc = xsc;
1085         struct ifnet *ifp;
1086         int i;
1087
1088         for (i = 0; i < SW_DEVS; i++) {
1089                 ifp = sc->sc_ifnet[i];
1090                 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1091                         if (sc->ndevs == 0) {
1092                                 admsw_init_bufs(sc);
1093                                 admsw_reset(sc);
1094                                 REG_WRITE(CPUP_CONF_REG,
1095                                     CPUP_CONF_CRCP | CPUP_CONF_DUNP_MASK |
1096                                     CPUP_CONF_DMCP_MASK);
1097                                 /* clear all pending interrupts */
1098                                 REG_WRITE(ADMSW_INT_ST, INT_MASK);
1099
1100                                 /* enable needed interrupts */
1101                                 REG_WRITE(ADMSW_INT_MASK, 
1102                                     REG_READ(ADMSW_INT_MASK) & 
1103                                     ~(ADMSW_INTR_SHD | ADMSW_INTR_SLD | 
1104                                         ADMSW_INTR_RHD | ADMSW_INTR_RLD | 
1105                                         ADMSW_INTR_HDF | ADMSW_INTR_LDF));
1106
1107                                 callout_reset(&sc->sc_watchdog, hz,
1108                                     admsw_watchdog, sc);
1109                         }
1110                         sc->ndevs++;
1111                 }
1112
1113
1114                 /* mark iface as running */
1115                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1116                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1117         }
1118
1119         /* Set the receive filter. */
1120         admsw_set_filter(sc);
1121 }
1122
1123 /*
1124  * admsw_stop:          [ifnet interface function]
1125  *
1126  *      Stop transmission on the interface.
1127  */
1128 static void
1129 admsw_stop(struct ifnet *ifp, int disable)
1130 {
1131         struct admsw_softc *sc = ifp->if_softc;
1132
1133         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1134                 return;
1135
1136         if (--sc->ndevs == 0) {
1137                 /* printf("debug: de-initializing hardware\n"); */
1138
1139                 /* disable cpu port */
1140                 REG_WRITE(CPUP_CONF_REG,
1141                                 CPUP_CONF_DCPUP | CPUP_CONF_CRCP |
1142                                 CPUP_CONF_DUNP_MASK | CPUP_CONF_DMCP_MASK);
1143
1144                 /* XXX We should disable, then clear? --dyoung */
1145                 /* clear all pending interrupts */
1146                 REG_WRITE(ADMSW_INT_ST, INT_MASK);
1147
1148                 /* disable interrupts */
1149                 REG_WRITE(ADMSW_INT_MASK, INT_MASK);
1150
1151                 /* Cancel the watchdog timer. */
1152                 sc->sc_timer = 0;
1153                 callout_stop(&sc->sc_watchdog);
1154         }
1155
1156         /* Mark the interface as down. */
1157         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1158
1159         return;
1160 }
1161
1162 /*
1163  * admsw_set_filter:
1164  *
1165  *      Set up the receive filter.
1166  */
1167 static void
1168 admsw_set_filter(struct admsw_softc *sc)
1169 {
1170         int i;
1171         uint32_t allmc, anymc, conf, promisc;
1172         struct ifnet *ifp;
1173         struct ifmultiaddr *ifma;
1174
1175         /* Find which ports should be operated in promisc mode. */
1176         allmc = anymc = promisc = 0;
1177         for (i = 0; i < SW_DEVS; i++) {
1178                 ifp = sc->sc_ifnet[i];
1179                 if (ifp->if_flags & IFF_PROMISC)
1180                         promisc |= vlan_matrix[i];
1181
1182                 ifp->if_flags &= ~IFF_ALLMULTI;
1183
1184                 if_maddr_rlock(ifp);
1185                 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1186                 {
1187                         if (ifma->ifma_addr->sa_family != AF_LINK)
1188                                 continue;
1189
1190                         anymc |= vlan_matrix[i];
1191                 }
1192                 if_maddr_runlock(ifp);
1193         }
1194
1195         conf = REG_READ(CPUP_CONF_REG);
1196         /* 1 Disable forwarding of unknown & multicast packets to
1197          *   CPU on all ports.
1198          * 2 Enable forwarding of unknown & multicast packets to
1199          *   CPU on ports where IFF_PROMISC or IFF_ALLMULTI is set.
1200          */
1201         conf |= CPUP_CONF_DUNP_MASK | CPUP_CONF_DMCP_MASK;
1202         /* Enable forwarding of unknown packets to CPU on selected ports. */
1203         conf ^= ((promisc << CPUP_CONF_DUNP_SHIFT) & CPUP_CONF_DUNP_MASK);
1204         conf ^= ((allmc << CPUP_CONF_DMCP_SHIFT) & CPUP_CONF_DMCP_MASK);
1205         conf ^= ((anymc << CPUP_CONF_DMCP_SHIFT) & CPUP_CONF_DMCP_MASK);
1206         REG_WRITE(CPUP_CONF_REG, conf);
1207 }
1208
1209 /*
1210  * admsw_add_rxbuf:
1211  *
1212  *      Add a receive buffer to the indicated descriptor.
1213  */
1214 int
1215 admsw_add_rxbuf(struct admsw_softc *sc, int idx, int high)
1216 {
1217         struct admsw_descsoft *ds;
1218         struct mbuf *m;
1219         int error;
1220
1221         if (high)
1222                 ds = &sc->sc_rxhsoft[idx];
1223         else
1224                 ds = &sc->sc_rxlsoft[idx];
1225
1226         MGETHDR(m, M_NOWAIT, MT_DATA);
1227         if (m == NULL)
1228                 return (ENOBUFS);
1229
1230         MCLGET(m, M_NOWAIT);
1231         if ((m->m_flags & M_EXT) == 0) {
1232                 m_freem(m);
1233                 return (ENOBUFS);
1234         }
1235
1236         if (ds->ds_mbuf != NULL)
1237                 bus_dmamap_unload(sc->sc_bufs_dmat, ds->ds_dmamap);
1238
1239         ds->ds_mbuf = m;
1240
1241         error = bus_dmamap_load(sc->sc_bufs_dmat, ds->ds_dmamap,
1242             m->m_ext.ext_buf, m->m_ext.ext_size, admsw_rxbuf_map_addr, 
1243             ds, BUS_DMA_NOWAIT);
1244         if (error) {
1245                 device_printf(sc->sc_dev, 
1246                     "can't load rx DMA map %d, error = %d\n", idx, error);
1247                 panic("admsw_add_rxbuf");       /* XXX */
1248         }
1249
1250         bus_dmamap_sync(sc->sc_bufs_dmat, ds->ds_dmamap, BUS_DMASYNC_PREREAD);
1251
1252         if (high)
1253                 ADMSW_INIT_RXHDESC(sc, idx);
1254         else
1255                 ADMSW_INIT_RXLDESC(sc, idx);
1256
1257         return (0);
1258 }
1259
1260 int
1261 admsw_mediachange(struct ifnet *ifp)
1262 {
1263         struct admsw_softc *sc = ifp->if_softc;
1264         int port = 0;
1265         struct ifmedia *ifm;
1266         int old, new, val;
1267
1268         while(port < SW_DEVS) {
1269                 if(ifp == sc->sc_ifnet[port])
1270                         break;
1271                 else
1272                         port++;
1273         }
1274
1275         ifm = &sc->sc_ifmedia[port];
1276
1277         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1278                 return (EINVAL);
1279
1280         if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
1281                 val = PHY_CNTL2_AUTONEG|PHY_CNTL2_100M|PHY_CNTL2_FDX;
1282         } else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
1283                 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1284                         val = PHY_CNTL2_100M|PHY_CNTL2_FDX;
1285                 else
1286                         val = PHY_CNTL2_100M;
1287         } else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_T) {
1288                 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1289                         val = PHY_CNTL2_FDX;
1290                 else
1291                         val = 0;
1292         } else
1293                 return (EINVAL);
1294
1295         old = REG_READ(PHY_CNTL2_REG);
1296         new = old & ~((PHY_CNTL2_AUTONEG|PHY_CNTL2_100M|PHY_CNTL2_FDX) << port);
1297         new |= (val << port);
1298
1299         if (new != old)
1300                 REG_WRITE(PHY_CNTL2_REG, new);
1301
1302         return (0);
1303 }
1304
1305 void
1306 admsw_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1307 {
1308         struct admsw_softc *sc = ifp->if_softc;
1309         int port = 0;
1310         int status;
1311
1312         while(port < SW_DEVS) {
1313                 if(ifp == sc->sc_ifnet[port])
1314                         break;
1315                 else
1316                         port++;
1317         }
1318
1319         ifmr->ifm_status = IFM_AVALID;
1320         ifmr->ifm_active = IFM_ETHER;
1321
1322         status = REG_READ(PHY_ST_REG) >> port;
1323
1324         if ((status & PHY_ST_LINKUP) == 0) {
1325                 ifmr->ifm_active |= IFM_NONE;
1326                 return;
1327         }
1328
1329         ifmr->ifm_status |= IFM_ACTIVE;
1330         ifmr->ifm_active |= (status & PHY_ST_100M) ? IFM_100_TX : IFM_10_T;
1331         if (status & PHY_ST_FDX)
1332                 ifmr->ifm_active |= IFM_FDX;
1333 }
1334
1335 static device_method_t admsw_methods[] = {
1336         /* Device interface */
1337         DEVMETHOD(device_probe,         admsw_probe),
1338         DEVMETHOD(device_attach,        admsw_attach),
1339         DEVMETHOD(device_detach,        admsw_detach),
1340         DEVMETHOD(device_shutdown,      admsw_shutdown),
1341
1342         { 0, 0 }
1343 };
1344
1345 static devclass_t admsw_devclass;
1346
1347 static driver_t admsw_driver = {
1348         "admsw",
1349         admsw_methods,
1350         sizeof(struct admsw_softc),
1351 };
1352
1353 DRIVER_MODULE(admsw, obio, admsw_driver, admsw_devclass, 0, 0);
1354 MODULE_DEPEND(admsw, ether, 1, 1, 1);