if_mec.c revision 1.26 1 1.26 tsutsui /* $NetBSD: if_mec.c,v 1.26 2008/08/10 18:49:47 tsutsui Exp $ */
2 1.1 tsutsui
3 1.20 tsutsui /*-
4 1.20 tsutsui * Copyright (c) 2004 Izumi Tsutsui. All rights reserved.
5 1.1 tsutsui *
6 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
7 1.1 tsutsui * modification, are permitted provided that the following conditions
8 1.1 tsutsui * are met:
9 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
10 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
11 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
13 1.1 tsutsui * documentation and/or other materials provided with the distribution.
14 1.1 tsutsui *
15 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 tsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 tsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 tsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 tsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 tsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 tsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 tsutsui * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 tsutsui * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 tsutsui * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 tsutsui */
26 1.1 tsutsui
27 1.1 tsutsui /*
28 1.1 tsutsui * Copyright (c) 2003 Christopher SEKIYA
29 1.1 tsutsui * All rights reserved.
30 1.1 tsutsui *
31 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
32 1.1 tsutsui * modification, are permitted provided that the following conditions
33 1.1 tsutsui * are met:
34 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
35 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
36 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
37 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
38 1.1 tsutsui * documentation and/or other materials provided with the distribution.
39 1.1 tsutsui * 3. All advertising materials mentioning features or use of this software
40 1.1 tsutsui * must display the following acknowledgement:
41 1.1 tsutsui * This product includes software developed for the
42 1.1 tsutsui * NetBSD Project. See http://www.NetBSD.org/ for
43 1.1 tsutsui * information about NetBSD.
44 1.1 tsutsui * 4. The name of the author may not be used to endorse or promote products
45 1.1 tsutsui * derived from this software without specific prior written permission.
46 1.1 tsutsui *
47 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 1.1 tsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 1.1 tsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 1.1 tsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 1.1 tsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 1.1 tsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 1.1 tsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 1.1 tsutsui * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 1.1 tsutsui * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 1.1 tsutsui * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 1.1 tsutsui */
58 1.1 tsutsui
59 1.1 tsutsui /*
60 1.14 tsutsui * MACE MAC-110 Ethernet driver
61 1.1 tsutsui */
62 1.1 tsutsui
63 1.1 tsutsui #include <sys/cdefs.h>
64 1.26 tsutsui __KERNEL_RCSID(0, "$NetBSD: if_mec.c,v 1.26 2008/08/10 18:49:47 tsutsui Exp $");
65 1.1 tsutsui
66 1.1 tsutsui #include "opt_ddb.h"
67 1.1 tsutsui #include "bpfilter.h"
68 1.1 tsutsui #include "rnd.h"
69 1.1 tsutsui
70 1.1 tsutsui #include <sys/param.h>
71 1.1 tsutsui #include <sys/systm.h>
72 1.1 tsutsui #include <sys/device.h>
73 1.1 tsutsui #include <sys/callout.h>
74 1.1 tsutsui #include <sys/mbuf.h>
75 1.1 tsutsui #include <sys/malloc.h>
76 1.1 tsutsui #include <sys/kernel.h>
77 1.1 tsutsui #include <sys/socket.h>
78 1.1 tsutsui #include <sys/ioctl.h>
79 1.1 tsutsui #include <sys/errno.h>
80 1.1 tsutsui
81 1.1 tsutsui #if NRND > 0
82 1.1 tsutsui #include <sys/rnd.h>
83 1.1 tsutsui #endif
84 1.1 tsutsui
85 1.1 tsutsui #include <net/if.h>
86 1.1 tsutsui #include <net/if_dl.h>
87 1.1 tsutsui #include <net/if_media.h>
88 1.1 tsutsui #include <net/if_ether.h>
89 1.1 tsutsui
90 1.1 tsutsui #if NBPFILTER > 0
91 1.1 tsutsui #include <net/bpf.h>
92 1.1 tsutsui #endif
93 1.1 tsutsui
94 1.1 tsutsui #include <machine/bus.h>
95 1.1 tsutsui #include <machine/intr.h>
96 1.1 tsutsui #include <machine/machtype.h>
97 1.1 tsutsui
98 1.1 tsutsui #include <dev/mii/mii.h>
99 1.1 tsutsui #include <dev/mii/miivar.h>
100 1.1 tsutsui
101 1.1 tsutsui #include <sgimips/mace/macevar.h>
102 1.1 tsutsui #include <sgimips/mace/if_mecreg.h>
103 1.1 tsutsui
104 1.1 tsutsui #include <dev/arcbios/arcbios.h>
105 1.1 tsutsui #include <dev/arcbios/arcbiosvar.h>
106 1.1 tsutsui
107 1.1 tsutsui /* #define MEC_DEBUG */
108 1.1 tsutsui
109 1.1 tsutsui #ifdef MEC_DEBUG
110 1.1 tsutsui #define MEC_DEBUG_RESET 0x01
111 1.1 tsutsui #define MEC_DEBUG_START 0x02
112 1.1 tsutsui #define MEC_DEBUG_STOP 0x04
113 1.1 tsutsui #define MEC_DEBUG_INTR 0x08
114 1.1 tsutsui #define MEC_DEBUG_RXINTR 0x10
115 1.1 tsutsui #define MEC_DEBUG_TXINTR 0x20
116 1.1 tsutsui uint32_t mec_debug = 0;
117 1.1 tsutsui #define DPRINTF(x, y) if (mec_debug & (x)) printf y
118 1.1 tsutsui #else
119 1.1 tsutsui #define DPRINTF(x, y) /* nothing */
120 1.1 tsutsui #endif
121 1.1 tsutsui
122 1.1 tsutsui /*
123 1.1 tsutsui * Transmit descriptor list size
124 1.1 tsutsui */
125 1.1 tsutsui #define MEC_NTXDESC 64
126 1.1 tsutsui #define MEC_NTXDESC_MASK (MEC_NTXDESC - 1)
127 1.1 tsutsui #define MEC_NEXTTX(x) (((x) + 1) & MEC_NTXDESC_MASK)
128 1.21 tsutsui #define MEC_NTXDESC_RSVD 4
129 1.1 tsutsui
130 1.1 tsutsui /*
131 1.1 tsutsui * software state for TX
132 1.1 tsutsui */
133 1.1 tsutsui struct mec_txsoft {
134 1.1 tsutsui struct mbuf *txs_mbuf; /* head of our mbuf chain */
135 1.1 tsutsui bus_dmamap_t txs_dmamap; /* our DMA map */
136 1.1 tsutsui uint32_t txs_flags;
137 1.1 tsutsui #define MEC_TXS_BUFLEN_MASK 0x0000007f /* data len in txd_buf */
138 1.1 tsutsui #define MEC_TXS_TXDBUF 0x00000080 /* txd_buf is used */
139 1.1 tsutsui #define MEC_TXS_TXDPTR1 0x00000100 /* txd_ptr[0] is used */
140 1.1 tsutsui };
141 1.1 tsutsui
142 1.1 tsutsui /*
143 1.1 tsutsui * Transmit buffer descriptor
144 1.1 tsutsui */
145 1.1 tsutsui #define MEC_TXDESCSIZE 128
146 1.1 tsutsui #define MEC_NTXPTR 3
147 1.1 tsutsui #define MEC_TXD_BUFOFFSET \
148 1.1 tsutsui (sizeof(uint64_t) + MEC_NTXPTR * sizeof(uint64_t))
149 1.1 tsutsui #define MEC_TXD_BUFSIZE (MEC_TXDESCSIZE - MEC_TXD_BUFOFFSET)
150 1.1 tsutsui #define MEC_TXD_BUFSTART(len) (MEC_TXD_BUFSIZE - (len))
151 1.1 tsutsui #define MEC_TXD_ALIGN 8
152 1.1 tsutsui #define MEC_TXD_ROUNDUP(addr) \
153 1.1 tsutsui (((addr) + (MEC_TXD_ALIGN - 1)) & ~((uint64_t)MEC_TXD_ALIGN - 1))
154 1.1 tsutsui
155 1.1 tsutsui struct mec_txdesc {
156 1.1 tsutsui volatile uint64_t txd_cmd;
157 1.1 tsutsui #define MEC_TXCMD_DATALEN 0x000000000000ffff /* data length */
158 1.1 tsutsui #define MEC_TXCMD_BUFSTART 0x00000000007f0000 /* start byte offset */
159 1.1 tsutsui #define TXCMD_BUFSTART(x) ((x) << 16)
160 1.1 tsutsui #define MEC_TXCMD_TERMDMA 0x0000000000800000 /* stop DMA on abort */
161 1.1 tsutsui #define MEC_TXCMD_TXINT 0x0000000001000000 /* INT after TX done */
162 1.1 tsutsui #define MEC_TXCMD_PTR1 0x0000000002000000 /* valid 1st txd_ptr */
163 1.1 tsutsui #define MEC_TXCMD_PTR2 0x0000000004000000 /* valid 2nd txd_ptr */
164 1.1 tsutsui #define MEC_TXCMD_PTR3 0x0000000008000000 /* valid 3rd txd_ptr */
165 1.1 tsutsui #define MEC_TXCMD_UNUSED 0xfffffffff0000000ULL /* should be zero */
166 1.1 tsutsui
167 1.1 tsutsui #define txd_stat txd_cmd
168 1.1 tsutsui #define MEC_TXSTAT_LEN 0x000000000000ffff /* TX length */
169 1.1 tsutsui #define MEC_TXSTAT_COLCNT 0x00000000000f0000 /* collision count */
170 1.1 tsutsui #define MEC_TXSTAT_COLCNT_SHIFT 16
171 1.1 tsutsui #define MEC_TXSTAT_LATE_COL 0x0000000000100000 /* late collision */
172 1.1 tsutsui #define MEC_TXSTAT_CRCERROR 0x0000000000200000 /* */
173 1.1 tsutsui #define MEC_TXSTAT_DEFERRED 0x0000000000400000 /* */
174 1.1 tsutsui #define MEC_TXSTAT_SUCCESS 0x0000000000800000 /* TX complete */
175 1.1 tsutsui #define MEC_TXSTAT_TOOBIG 0x0000000001000000 /* */
176 1.1 tsutsui #define MEC_TXSTAT_UNDERRUN 0x0000000002000000 /* */
177 1.1 tsutsui #define MEC_TXSTAT_COLLISIONS 0x0000000004000000 /* */
178 1.1 tsutsui #define MEC_TXSTAT_EXDEFERRAL 0x0000000008000000 /* */
179 1.1 tsutsui #define MEC_TXSTAT_COLLIDED 0x0000000010000000 /* */
180 1.1 tsutsui #define MEC_TXSTAT_UNUSED 0x7fffffffe0000000ULL /* should be zero */
181 1.1 tsutsui #define MEC_TXSTAT_SENT 0x8000000000000000ULL /* packet sent */
182 1.1 tsutsui
183 1.1 tsutsui uint64_t txd_ptr[MEC_NTXPTR];
184 1.1 tsutsui #define MEC_TXPTR_UNUSED2 0x0000000000000007 /* should be zero */
185 1.1 tsutsui #define MEC_TXPTR_DMAADDR 0x00000000fffffff8 /* TX DMA address */
186 1.1 tsutsui #define MEC_TXPTR_LEN 0x0000ffff00000000ULL /* buffer length */
187 1.1 tsutsui #define TXPTR_LEN(x) ((uint64_t)(x) << 32)
188 1.1 tsutsui #define MEC_TXPTR_UNUSED1 0xffff000000000000ULL /* should be zero */
189 1.1 tsutsui
190 1.1 tsutsui uint8_t txd_buf[MEC_TXD_BUFSIZE];
191 1.1 tsutsui };
192 1.1 tsutsui
193 1.1 tsutsui /*
194 1.1 tsutsui * Receive buffer size
195 1.1 tsutsui */
196 1.1 tsutsui #define MEC_NRXDESC 16
197 1.1 tsutsui #define MEC_NRXDESC_MASK (MEC_NRXDESC - 1)
198 1.1 tsutsui #define MEC_NEXTRX(x) (((x) + 1) & MEC_NRXDESC_MASK)
199 1.1 tsutsui
200 1.1 tsutsui /*
201 1.1 tsutsui * Receive buffer description
202 1.1 tsutsui */
203 1.1 tsutsui #define MEC_RXDESCSIZE 4096 /* umm, should be 4kbyte aligned */
204 1.1 tsutsui #define MEC_RXD_NRXPAD 3
205 1.1 tsutsui #define MEC_RXD_DMAOFFSET (1 + MEC_RXD_NRXPAD)
206 1.1 tsutsui #define MEC_RXD_BUFOFFSET (MEC_RXD_DMAOFFSET * sizeof(uint64_t))
207 1.1 tsutsui #define MEC_RXD_BUFSIZE (MEC_RXDESCSIZE - MEC_RXD_BUFOFFSET)
208 1.1 tsutsui
209 1.1 tsutsui struct mec_rxdesc {
210 1.1 tsutsui volatile uint64_t rxd_stat;
211 1.1 tsutsui #define MEC_RXSTAT_LEN 0x000000000000ffff /* data length */
212 1.1 tsutsui #define MEC_RXSTAT_VIOLATION 0x0000000000010000 /* code violation (?) */
213 1.1 tsutsui #define MEC_RXSTAT_UNUSED2 0x0000000000020000 /* unknown (?) */
214 1.1 tsutsui #define MEC_RXSTAT_CRCERROR 0x0000000000040000 /* CRC error */
215 1.1 tsutsui #define MEC_RXSTAT_MULTICAST 0x0000000000080000 /* multicast packet */
216 1.1 tsutsui #define MEC_RXSTAT_BROADCAST 0x0000000000100000 /* broadcast packet */
217 1.1 tsutsui #define MEC_RXSTAT_INVALID 0x0000000000200000 /* invalid preamble */
218 1.1 tsutsui #define MEC_RXSTAT_LONGEVENT 0x0000000000400000 /* long packet */
219 1.1 tsutsui #define MEC_RXSTAT_BADPACKET 0x0000000000800000 /* bad packet */
220 1.1 tsutsui #define MEC_RXSTAT_CAREVENT 0x0000000001000000 /* carrier event */
221 1.1 tsutsui #define MEC_RXSTAT_MATCHMCAST 0x0000000002000000 /* match multicast */
222 1.1 tsutsui #define MEC_RXSTAT_MATCHMAC 0x0000000004000000 /* match MAC */
223 1.1 tsutsui #define MEC_RXSTAT_SEQNUM 0x00000000f8000000 /* sequence number */
224 1.1 tsutsui #define MEC_RXSTAT_CKSUM 0x0000ffff00000000ULL /* IP checksum */
225 1.1 tsutsui #define MEC_RXSTAT_UNUSED1 0x7fff000000000000ULL /* should be zero */
226 1.1 tsutsui #define MEC_RXSTAT_RECEIVED 0x8000000000000000ULL /* set to 1 on RX */
227 1.1 tsutsui uint64_t rxd_pad1[MEC_RXD_NRXPAD];
228 1.1 tsutsui uint8_t rxd_buf[MEC_RXD_BUFSIZE];
229 1.1 tsutsui };
230 1.1 tsutsui
231 1.1 tsutsui /*
232 1.1 tsutsui * control structures for DMA ops
233 1.1 tsutsui */
234 1.1 tsutsui struct mec_control_data {
235 1.1 tsutsui /*
236 1.1 tsutsui * TX descriptors and buffers
237 1.1 tsutsui */
238 1.1 tsutsui struct mec_txdesc mcd_txdesc[MEC_NTXDESC];
239 1.1 tsutsui
240 1.1 tsutsui /*
241 1.1 tsutsui * RX descriptors and buffers
242 1.1 tsutsui */
243 1.1 tsutsui struct mec_rxdesc mcd_rxdesc[MEC_NRXDESC];
244 1.1 tsutsui };
245 1.1 tsutsui
246 1.1 tsutsui /*
247 1.1 tsutsui * It _seems_ there are some restrictions on descriptor address:
248 1.1 tsutsui *
249 1.1 tsutsui * - Base address of txdescs should be 8kbyte aligned
250 1.1 tsutsui * - Each txdesc should be 128byte aligned
251 1.1 tsutsui * - Each rxdesc should be 4kbyte aligned
252 1.1 tsutsui *
253 1.3 tsutsui * So we should specify 8k align to allocalte txdescs.
254 1.1 tsutsui * In this case, sizeof(struct mec_txdesc) * MEC_NTXDESC is 8192
255 1.1 tsutsui * so rxdescs are also allocated at 4kbyte aligned.
256 1.1 tsutsui */
257 1.1 tsutsui #define MEC_CONTROL_DATA_ALIGN (8 * 1024)
258 1.1 tsutsui
259 1.1 tsutsui #define MEC_CDOFF(x) offsetof(struct mec_control_data, x)
260 1.1 tsutsui #define MEC_CDTXOFF(x) MEC_CDOFF(mcd_txdesc[(x)])
261 1.1 tsutsui #define MEC_CDRXOFF(x) MEC_CDOFF(mcd_rxdesc[(x)])
262 1.1 tsutsui
263 1.1 tsutsui /*
264 1.1 tsutsui * software state per device
265 1.1 tsutsui */
266 1.1 tsutsui struct mec_softc {
267 1.19 tsutsui device_t sc_dev; /* generic device structures */
268 1.1 tsutsui
269 1.1 tsutsui bus_space_tag_t sc_st; /* bus_space tag */
270 1.1 tsutsui bus_space_handle_t sc_sh; /* bus_space handle */
271 1.1 tsutsui bus_dma_tag_t sc_dmat; /* bus_dma tag */
272 1.14 tsutsui void *sc_sdhook; /* shutdown hook */
273 1.1 tsutsui
274 1.1 tsutsui struct ethercom sc_ethercom; /* Ethernet common part */
275 1.1 tsutsui
276 1.1 tsutsui struct mii_data sc_mii; /* MII/media information */
277 1.1 tsutsui int sc_phyaddr; /* MII address */
278 1.1 tsutsui struct callout sc_tick_ch; /* tick callout */
279 1.1 tsutsui
280 1.1 tsutsui uint8_t sc_enaddr[ETHER_ADDR_LEN]; /* MAC address */
281 1.1 tsutsui
282 1.1 tsutsui bus_dmamap_t sc_cddmamap; /* bus_dma map for control data */
283 1.1 tsutsui #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
284 1.1 tsutsui
285 1.14 tsutsui /* pointer to allocated control data */
286 1.1 tsutsui struct mec_control_data *sc_control_data;
287 1.1 tsutsui #define sc_txdesc sc_control_data->mcd_txdesc
288 1.1 tsutsui #define sc_rxdesc sc_control_data->mcd_rxdesc
289 1.1 tsutsui
290 1.1 tsutsui /* software state for TX descs */
291 1.1 tsutsui struct mec_txsoft sc_txsoft[MEC_NTXDESC];
292 1.1 tsutsui
293 1.1 tsutsui int sc_txpending; /* number of TX requests pending */
294 1.1 tsutsui int sc_txdirty; /* first dirty TX descriptor */
295 1.1 tsutsui int sc_txlast; /* last used TX descriptor */
296 1.1 tsutsui
297 1.1 tsutsui int sc_rxptr; /* next ready RX buffer */
298 1.1 tsutsui
299 1.1 tsutsui #if NRND > 0
300 1.1 tsutsui rndsource_element_t sc_rnd_source; /* random source */
301 1.1 tsutsui #endif
302 1.1 tsutsui };
303 1.1 tsutsui
304 1.1 tsutsui #define MEC_CDTXADDR(sc, x) ((sc)->sc_cddma + MEC_CDTXOFF(x))
305 1.1 tsutsui #define MEC_CDRXADDR(sc, x) ((sc)->sc_cddma + MEC_CDRXOFF(x))
306 1.1 tsutsui
307 1.1 tsutsui #define MEC_TXDESCSYNC(sc, x, ops) \
308 1.1 tsutsui bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
309 1.1 tsutsui MEC_CDTXOFF(x), MEC_TXDESCSIZE, (ops))
310 1.1 tsutsui #define MEC_TXCMDSYNC(sc, x, ops) \
311 1.1 tsutsui bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
312 1.1 tsutsui MEC_CDTXOFF(x), sizeof(uint64_t), (ops))
313 1.1 tsutsui
314 1.1 tsutsui #define MEC_RXSTATSYNC(sc, x, ops) \
315 1.1 tsutsui bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
316 1.1 tsutsui MEC_CDRXOFF(x), sizeof(uint64_t), (ops))
317 1.1 tsutsui #define MEC_RXBUFSYNC(sc, x, len, ops) \
318 1.1 tsutsui bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
319 1.1 tsutsui MEC_CDRXOFF(x) + MEC_RXD_BUFOFFSET, \
320 1.1 tsutsui MEC_ETHER_ALIGN + (len), (ops))
321 1.1 tsutsui
322 1.1 tsutsui /* XXX these values should be moved to <net/if_ether.h> ? */
323 1.1 tsutsui #define ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
324 1.1 tsutsui #define MEC_ETHER_ALIGN 2
325 1.1 tsutsui
326 1.19 tsutsui static int mec_match(device_t, cfdata_t, void *);
327 1.19 tsutsui static void mec_attach(device_t, device_t, void *);
328 1.1 tsutsui
329 1.19 tsutsui static int mec_mii_readreg(device_t, int, int);
330 1.19 tsutsui static void mec_mii_writereg(device_t, int, int, int);
331 1.19 tsutsui static int mec_mii_wait(struct mec_softc *);
332 1.19 tsutsui static void mec_statchg(device_t);
333 1.1 tsutsui
334 1.1 tsutsui static void enaddr_aton(const char *, uint8_t *);
335 1.1 tsutsui
336 1.19 tsutsui static int mec_init(struct ifnet * ifp);
337 1.19 tsutsui static void mec_start(struct ifnet *);
338 1.19 tsutsui static void mec_watchdog(struct ifnet *);
339 1.19 tsutsui static void mec_tick(void *);
340 1.19 tsutsui static int mec_ioctl(struct ifnet *, u_long, void *);
341 1.19 tsutsui static void mec_reset(struct mec_softc *);
342 1.19 tsutsui static void mec_setfilter(struct mec_softc *);
343 1.19 tsutsui static int mec_intr(void *arg);
344 1.19 tsutsui static void mec_stop(struct ifnet *, int);
345 1.19 tsutsui static void mec_rxintr(struct mec_softc *);
346 1.21 tsutsui static void mec_txintr(struct mec_softc *, uint32_t);
347 1.19 tsutsui static void mec_shutdown(void *);
348 1.1 tsutsui
349 1.19 tsutsui CFATTACH_DECL_NEW(mec, sizeof(struct mec_softc),
350 1.1 tsutsui mec_match, mec_attach, NULL, NULL);
351 1.1 tsutsui
352 1.1 tsutsui static int mec_matched = 0;
353 1.1 tsutsui
354 1.19 tsutsui static int
355 1.19 tsutsui mec_match(device_t parent, cfdata_t cf, void *aux)
356 1.1 tsutsui {
357 1.1 tsutsui
358 1.1 tsutsui /* allow only one device */
359 1.1 tsutsui if (mec_matched)
360 1.1 tsutsui return 0;
361 1.1 tsutsui
362 1.1 tsutsui mec_matched = 1;
363 1.1 tsutsui return 1;
364 1.1 tsutsui }
365 1.1 tsutsui
366 1.19 tsutsui static void
367 1.19 tsutsui mec_attach(device_t parent, device_t self, void *aux)
368 1.1 tsutsui {
369 1.19 tsutsui struct mec_softc *sc = device_private(self);
370 1.1 tsutsui struct mace_attach_args *maa = aux;
371 1.1 tsutsui struct ifnet *ifp = &sc->sc_ethercom.ec_if;
372 1.17 tsutsui uint64_t address, command;
373 1.6 martin const char *macaddr;
374 1.1 tsutsui struct mii_softc *child;
375 1.1 tsutsui bus_dma_segment_t seg;
376 1.1 tsutsui int i, err, rseg;
377 1.18 martin bool mac_is_fake;
378 1.1 tsutsui
379 1.19 tsutsui sc->sc_dev = self;
380 1.1 tsutsui sc->sc_st = maa->maa_st;
381 1.1 tsutsui if (bus_space_subregion(sc->sc_st, maa->maa_sh,
382 1.1 tsutsui maa->maa_offset, 0, &sc->sc_sh) != 0) {
383 1.19 tsutsui aprint_error(": can't map i/o space\n");
384 1.1 tsutsui return;
385 1.1 tsutsui }
386 1.1 tsutsui
387 1.1 tsutsui /* set up DMA structures */
388 1.1 tsutsui sc->sc_dmat = maa->maa_dmat;
389 1.1 tsutsui
390 1.1 tsutsui /*
391 1.1 tsutsui * Allocate the control data structures, and create and load the
392 1.1 tsutsui * DMA map for it.
393 1.1 tsutsui */
394 1.1 tsutsui if ((err = bus_dmamem_alloc(sc->sc_dmat,
395 1.1 tsutsui sizeof(struct mec_control_data), MEC_CONTROL_DATA_ALIGN, 0,
396 1.1 tsutsui &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
397 1.19 tsutsui aprint_error(": unable to allocate control data, error = %d\n",
398 1.19 tsutsui err);
399 1.1 tsutsui goto fail_0;
400 1.1 tsutsui }
401 1.1 tsutsui /*
402 1.1 tsutsui * XXX needs re-think...
403 1.1 tsutsui * control data structures contain whole RX data buffer, so
404 1.1 tsutsui * BUS_DMA_COHERENT (which disables cache) may cause some performance
405 1.1 tsutsui * issue on copying data from the RX buffer to mbuf on normal memory,
406 1.1 tsutsui * though we have to make sure all bus_dmamap_sync(9) ops are called
407 1.14 tsutsui * properly in that case.
408 1.1 tsutsui */
409 1.1 tsutsui if ((err = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
410 1.1 tsutsui sizeof(struct mec_control_data),
411 1.10 christos (void **)&sc->sc_control_data, /*BUS_DMA_COHERENT*/ 0)) != 0) {
412 1.19 tsutsui aprint_error(": unable to map control data, error = %d\n", err);
413 1.1 tsutsui goto fail_1;
414 1.1 tsutsui }
415 1.1 tsutsui memset(sc->sc_control_data, 0, sizeof(struct mec_control_data));
416 1.1 tsutsui
417 1.1 tsutsui if ((err = bus_dmamap_create(sc->sc_dmat,
418 1.1 tsutsui sizeof(struct mec_control_data), 1,
419 1.1 tsutsui sizeof(struct mec_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
420 1.19 tsutsui aprint_error(": unable to create control data DMA map,"
421 1.19 tsutsui " error = %d\n", err);
422 1.1 tsutsui goto fail_2;
423 1.1 tsutsui }
424 1.1 tsutsui if ((err = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
425 1.1 tsutsui sc->sc_control_data, sizeof(struct mec_control_data), NULL,
426 1.1 tsutsui BUS_DMA_NOWAIT)) != 0) {
427 1.19 tsutsui aprint_error(": unable to load control data DMA map,"
428 1.19 tsutsui " error = %d\n", err);
429 1.1 tsutsui goto fail_3;
430 1.1 tsutsui }
431 1.1 tsutsui
432 1.1 tsutsui /* create TX buffer DMA maps */
433 1.1 tsutsui for (i = 0; i < MEC_NTXDESC; i++) {
434 1.1 tsutsui if ((err = bus_dmamap_create(sc->sc_dmat,
435 1.22 tsutsui MCLBYTES, 1, MCLBYTES, PAGE_SIZE, 0,
436 1.1 tsutsui &sc->sc_txsoft[i].txs_dmamap)) != 0) {
437 1.19 tsutsui aprint_error(": unable to create tx DMA map %d,"
438 1.19 tsutsui " error = %d\n", i, err);
439 1.1 tsutsui goto fail_4;
440 1.1 tsutsui }
441 1.1 tsutsui }
442 1.1 tsutsui
443 1.11 ad callout_init(&sc->sc_tick_ch, 0);
444 1.1 tsutsui
445 1.14 tsutsui /* get Ethernet address from ARCBIOS */
446 1.1 tsutsui if ((macaddr = ARCBIOS->GetEnvironmentVariable("eaddr")) == NULL) {
447 1.19 tsutsui aprint_error(": unable to get MAC address!\n");
448 1.1 tsutsui goto fail_4;
449 1.1 tsutsui }
450 1.18 martin /*
451 1.18 martin * On some machines the DS2502 chip storing the serial number/
452 1.18 martin * mac address is on the pci riser board - if this board is
453 1.18 martin * missing, ARCBIOS will not know a good ethernet address (but
454 1.18 martin * otherwise the machine will work fine).
455 1.18 martin */
456 1.18 martin mac_is_fake = false;
457 1.18 martin if (strcmp(macaddr, "ff:ff:ff:ff:ff:ff") == 0) {
458 1.18 martin uint32_t ui = 0;
459 1.18 martin const char * netaddr =
460 1.18 martin ARCBIOS->GetEnvironmentVariable("netaddr");
461 1.18 martin
462 1.18 martin /*
463 1.18 martin * Create a MAC address by abusing the "netaddr" env var
464 1.18 martin */
465 1.18 martin sc->sc_enaddr[0] = 0xf2;
466 1.18 martin sc->sc_enaddr[1] = 0x0b;
467 1.18 martin sc->sc_enaddr[2] = 0xa4;
468 1.18 martin if (netaddr) {
469 1.18 martin mac_is_fake = true;
470 1.18 martin while (*netaddr) {
471 1.18 martin int v = 0;
472 1.18 martin while (*netaddr && *netaddr != '.') {
473 1.18 martin if (*netaddr >= '0' && *netaddr <= '9')
474 1.18 martin v = v*10 + (*netaddr - '0');
475 1.18 martin netaddr++;
476 1.18 martin }
477 1.18 martin ui <<= 8;
478 1.18 martin ui |= v;
479 1.18 martin if (*netaddr == '.')
480 1.18 martin netaddr++;
481 1.18 martin }
482 1.18 martin }
483 1.18 martin memcpy(sc->sc_enaddr+3, ((uint8_t *)&ui)+1, 3);
484 1.18 martin }
485 1.18 martin if (!mac_is_fake)
486 1.18 martin enaddr_aton(macaddr, sc->sc_enaddr);
487 1.1 tsutsui
488 1.17 tsutsui /* set the Ethernet address */
489 1.17 tsutsui address = 0;
490 1.17 tsutsui for (i = 0; i < ETHER_ADDR_LEN; i++) {
491 1.17 tsutsui address = address << 8;
492 1.17 tsutsui address |= sc->sc_enaddr[i];
493 1.17 tsutsui }
494 1.17 tsutsui bus_space_write_8(sc->sc_st, sc->sc_sh, MEC_STATION, address);
495 1.17 tsutsui
496 1.1 tsutsui /* reset device */
497 1.1 tsutsui mec_reset(sc);
498 1.1 tsutsui
499 1.1 tsutsui command = bus_space_read_8(sc->sc_st, sc->sc_sh, MEC_MAC_CONTROL);
500 1.1 tsutsui
501 1.19 tsutsui aprint_normal(": MAC-110 Ethernet, rev %u\n",
502 1.17 tsutsui (u_int)((command & MEC_MAC_REVISION) >> MEC_MAC_REVISION_SHIFT));
503 1.1 tsutsui
504 1.18 martin if (mac_is_fake)
505 1.19 tsutsui aprint_normal_dev(self,
506 1.19 tsutsui "could not get ethernet address from firmware"
507 1.18 martin " - generated one from the \"netaddr\" environment"
508 1.19 tsutsui " variable\n");
509 1.19 tsutsui aprint_normal_dev(self, "Ethernet address %s\n",
510 1.1 tsutsui ether_sprintf(sc->sc_enaddr));
511 1.1 tsutsui
512 1.1 tsutsui /* Done, now attach everything */
513 1.1 tsutsui
514 1.1 tsutsui sc->sc_mii.mii_ifp = ifp;
515 1.1 tsutsui sc->sc_mii.mii_readreg = mec_mii_readreg;
516 1.1 tsutsui sc->sc_mii.mii_writereg = mec_mii_writereg;
517 1.1 tsutsui sc->sc_mii.mii_statchg = mec_statchg;
518 1.1 tsutsui
519 1.1 tsutsui /* Set up PHY properties */
520 1.15 dyoung sc->sc_ethercom.ec_mii = &sc->sc_mii;
521 1.15 dyoung ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange,
522 1.15 dyoung ether_mediastatus);
523 1.19 tsutsui mii_attach(self, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
524 1.1 tsutsui MII_OFFSET_ANY, 0);
525 1.1 tsutsui
526 1.1 tsutsui child = LIST_FIRST(&sc->sc_mii.mii_phys);
527 1.1 tsutsui if (child == NULL) {
528 1.1 tsutsui /* No PHY attached */
529 1.1 tsutsui ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL,
530 1.1 tsutsui 0, NULL);
531 1.1 tsutsui ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL);
532 1.1 tsutsui } else {
533 1.1 tsutsui ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
534 1.1 tsutsui sc->sc_phyaddr = child->mii_phy;
535 1.1 tsutsui }
536 1.1 tsutsui
537 1.19 tsutsui strcpy(ifp->if_xname, device_xname(self));
538 1.1 tsutsui ifp->if_softc = sc;
539 1.1 tsutsui ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
540 1.1 tsutsui ifp->if_ioctl = mec_ioctl;
541 1.1 tsutsui ifp->if_start = mec_start;
542 1.1 tsutsui ifp->if_watchdog = mec_watchdog;
543 1.1 tsutsui ifp->if_init = mec_init;
544 1.1 tsutsui ifp->if_stop = mec_stop;
545 1.1 tsutsui ifp->if_mtu = ETHERMTU;
546 1.1 tsutsui IFQ_SET_READY(&ifp->if_snd);
547 1.1 tsutsui
548 1.1 tsutsui if_attach(ifp);
549 1.1 tsutsui ether_ifattach(ifp, sc->sc_enaddr);
550 1.1 tsutsui
551 1.1 tsutsui /* establish interrupt */
552 1.1 tsutsui cpu_intr_establish(maa->maa_intr, maa->maa_intrmask, mec_intr, sc);
553 1.1 tsutsui
554 1.1 tsutsui #if NRND > 0
555 1.19 tsutsui rnd_attach_source(&sc->sc_rnd_source, device_xname(self),
556 1.1 tsutsui RND_TYPE_NET, 0);
557 1.1 tsutsui #endif
558 1.1 tsutsui
559 1.1 tsutsui /* set shutdown hook to reset interface on powerdown */
560 1.1 tsutsui sc->sc_sdhook = shutdownhook_establish(mec_shutdown, sc);
561 1.1 tsutsui
562 1.1 tsutsui return;
563 1.1 tsutsui
564 1.1 tsutsui /*
565 1.1 tsutsui * Free any resources we've allocated during the failed attach
566 1.1 tsutsui * attempt. Do this in reverse order and fall though.
567 1.1 tsutsui */
568 1.1 tsutsui fail_4:
569 1.1 tsutsui for (i = 0; i < MEC_NTXDESC; i++) {
570 1.1 tsutsui if (sc->sc_txsoft[i].txs_dmamap != NULL)
571 1.1 tsutsui bus_dmamap_destroy(sc->sc_dmat,
572 1.1 tsutsui sc->sc_txsoft[i].txs_dmamap);
573 1.1 tsutsui }
574 1.1 tsutsui bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
575 1.1 tsutsui fail_3:
576 1.1 tsutsui bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
577 1.1 tsutsui fail_2:
578 1.10 christos bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
579 1.1 tsutsui sizeof(struct mec_control_data));
580 1.1 tsutsui fail_1:
581 1.1 tsutsui bus_dmamem_free(sc->sc_dmat, &seg, rseg);
582 1.1 tsutsui fail_0:
583 1.1 tsutsui return;
584 1.1 tsutsui }
585 1.1 tsutsui
586 1.19 tsutsui static int
587 1.19 tsutsui mec_mii_readreg(device_t self, int phy, int reg)
588 1.1 tsutsui {
589 1.19 tsutsui struct mec_softc *sc = device_private(self);
590 1.1 tsutsui bus_space_tag_t st = sc->sc_st;
591 1.1 tsutsui bus_space_handle_t sh = sc->sc_sh;
592 1.7 tsutsui uint64_t val;
593 1.1 tsutsui int i;
594 1.1 tsutsui
595 1.1 tsutsui if (mec_mii_wait(sc) != 0)
596 1.1 tsutsui return 0;
597 1.1 tsutsui
598 1.7 tsutsui bus_space_write_8(st, sh, MEC_PHY_ADDRESS,
599 1.1 tsutsui (phy << MEC_PHY_ADDR_DEVSHIFT) | (reg & MEC_PHY_ADDR_REGISTER));
600 1.12 macallan delay(25);
601 1.1 tsutsui bus_space_write_8(st, sh, MEC_PHY_READ_INITIATE, 1);
602 1.1 tsutsui delay(25);
603 1.12 macallan mec_mii_wait(sc);
604 1.1 tsutsui
605 1.1 tsutsui for (i = 0; i < 20; i++) {
606 1.1 tsutsui delay(30);
607 1.1 tsutsui
608 1.7 tsutsui val = bus_space_read_8(st, sh, MEC_PHY_DATA);
609 1.1 tsutsui
610 1.1 tsutsui if ((val & MEC_PHY_DATA_BUSY) == 0)
611 1.1 tsutsui return val & MEC_PHY_DATA_VALUE;
612 1.1 tsutsui }
613 1.1 tsutsui return 0;
614 1.1 tsutsui }
615 1.1 tsutsui
616 1.19 tsutsui static void
617 1.19 tsutsui mec_mii_writereg(device_t self, int phy, int reg, int val)
618 1.1 tsutsui {
619 1.19 tsutsui struct mec_softc *sc = device_private(self);
620 1.1 tsutsui bus_space_tag_t st = sc->sc_st;
621 1.1 tsutsui bus_space_handle_t sh = sc->sc_sh;
622 1.1 tsutsui
623 1.1 tsutsui if (mec_mii_wait(sc) != 0) {
624 1.1 tsutsui printf("timed out writing %x: %x\n", reg, val);
625 1.1 tsutsui return;
626 1.1 tsutsui }
627 1.1 tsutsui
628 1.7 tsutsui bus_space_write_8(st, sh, MEC_PHY_ADDRESS,
629 1.1 tsutsui (phy << MEC_PHY_ADDR_DEVSHIFT) | (reg & MEC_PHY_ADDR_REGISTER));
630 1.1 tsutsui
631 1.1 tsutsui delay(60);
632 1.1 tsutsui
633 1.7 tsutsui bus_space_write_8(st, sh, MEC_PHY_DATA, val & MEC_PHY_DATA_VALUE);
634 1.1 tsutsui
635 1.1 tsutsui delay(60);
636 1.1 tsutsui
637 1.1 tsutsui mec_mii_wait(sc);
638 1.1 tsutsui }
639 1.1 tsutsui
640 1.19 tsutsui static int
641 1.1 tsutsui mec_mii_wait(struct mec_softc *sc)
642 1.1 tsutsui {
643 1.1 tsutsui uint32_t busy;
644 1.1 tsutsui int i, s;
645 1.1 tsutsui
646 1.1 tsutsui for (i = 0; i < 100; i++) {
647 1.1 tsutsui delay(30);
648 1.1 tsutsui
649 1.1 tsutsui s = splhigh();
650 1.7 tsutsui busy = bus_space_read_8(sc->sc_st, sc->sc_sh, MEC_PHY_DATA);
651 1.1 tsutsui splx(s);
652 1.1 tsutsui
653 1.1 tsutsui if ((busy & MEC_PHY_DATA_BUSY) == 0)
654 1.1 tsutsui return 0;
655 1.12 macallan #if 0
656 1.1 tsutsui if (busy == 0xffff) /* XXX ? */
657 1.1 tsutsui return 0;
658 1.12 macallan #endif
659 1.1 tsutsui }
660 1.1 tsutsui
661 1.19 tsutsui printf("%s: MII timed out\n", device_xname(sc->sc_dev));
662 1.1 tsutsui return 1;
663 1.1 tsutsui }
664 1.1 tsutsui
665 1.19 tsutsui static void
666 1.19 tsutsui mec_statchg(device_t self)
667 1.1 tsutsui {
668 1.19 tsutsui struct mec_softc *sc = device_private(self);
669 1.1 tsutsui bus_space_tag_t st = sc->sc_st;
670 1.1 tsutsui bus_space_handle_t sh = sc->sc_sh;
671 1.1 tsutsui uint32_t control;
672 1.1 tsutsui
673 1.1 tsutsui control = bus_space_read_8(st, sh, MEC_MAC_CONTROL);
674 1.1 tsutsui control &= ~(MEC_MAC_IPGT | MEC_MAC_IPGR1 | MEC_MAC_IPGR2 |
675 1.1 tsutsui MEC_MAC_FULL_DUPLEX | MEC_MAC_SPEED_SELECT);
676 1.1 tsutsui
677 1.1 tsutsui /* must also set IPG here for duplex stuff ... */
678 1.1 tsutsui if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0) {
679 1.1 tsutsui control |= MEC_MAC_FULL_DUPLEX;
680 1.1 tsutsui } else {
681 1.1 tsutsui /* set IPG */
682 1.1 tsutsui control |= MEC_MAC_IPG_DEFAULT;
683 1.1 tsutsui }
684 1.1 tsutsui
685 1.1 tsutsui bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
686 1.1 tsutsui }
687 1.1 tsutsui
688 1.1 tsutsui /*
689 1.1 tsutsui * XXX
690 1.1 tsutsui * maybe this function should be moved to common part
691 1.1 tsutsui * (sgimips/machdep.c or elsewhere) for all on-board network devices.
692 1.1 tsutsui */
693 1.1 tsutsui static void
694 1.1 tsutsui enaddr_aton(const char *str, uint8_t *eaddr)
695 1.1 tsutsui {
696 1.1 tsutsui int i;
697 1.1 tsutsui char c;
698 1.1 tsutsui
699 1.1 tsutsui for (i = 0; i < ETHER_ADDR_LEN; i++) {
700 1.1 tsutsui if (*str == ':')
701 1.1 tsutsui str++;
702 1.1 tsutsui
703 1.1 tsutsui c = *str++;
704 1.1 tsutsui if (isdigit(c)) {
705 1.1 tsutsui eaddr[i] = (c - '0');
706 1.1 tsutsui } else if (isxdigit(c)) {
707 1.1 tsutsui eaddr[i] = (toupper(c) + 10 - 'A');
708 1.1 tsutsui }
709 1.1 tsutsui c = *str++;
710 1.1 tsutsui if (isdigit(c)) {
711 1.1 tsutsui eaddr[i] = (eaddr[i] << 4) | (c - '0');
712 1.1 tsutsui } else if (isxdigit(c)) {
713 1.1 tsutsui eaddr[i] = (eaddr[i] << 4) | (toupper(c) + 10 - 'A');
714 1.1 tsutsui }
715 1.1 tsutsui }
716 1.1 tsutsui }
717 1.1 tsutsui
718 1.19 tsutsui static int
719 1.1 tsutsui mec_init(struct ifnet *ifp)
720 1.1 tsutsui {
721 1.1 tsutsui struct mec_softc *sc = ifp->if_softc;
722 1.1 tsutsui bus_space_tag_t st = sc->sc_st;
723 1.1 tsutsui bus_space_handle_t sh = sc->sc_sh;
724 1.1 tsutsui struct mec_rxdesc *rxd;
725 1.15 dyoung int i, rc;
726 1.1 tsutsui
727 1.1 tsutsui /* cancel any pending I/O */
728 1.1 tsutsui mec_stop(ifp, 0);
729 1.1 tsutsui
730 1.1 tsutsui /* reset device */
731 1.1 tsutsui mec_reset(sc);
732 1.1 tsutsui
733 1.1 tsutsui /* setup filter for multicast or promisc mode */
734 1.1 tsutsui mec_setfilter(sc);
735 1.1 tsutsui
736 1.1 tsutsui /* set the TX ring pointer to the base address */
737 1.1 tsutsui bus_space_write_8(st, sh, MEC_TX_RING_BASE, MEC_CDTXADDR(sc, 0));
738 1.1 tsutsui
739 1.1 tsutsui sc->sc_txpending = 0;
740 1.1 tsutsui sc->sc_txdirty = 0;
741 1.1 tsutsui sc->sc_txlast = MEC_NTXDESC - 1;
742 1.1 tsutsui
743 1.1 tsutsui /* put RX buffers into FIFO */
744 1.1 tsutsui for (i = 0; i < MEC_NRXDESC; i++) {
745 1.1 tsutsui rxd = &sc->sc_rxdesc[i];
746 1.1 tsutsui rxd->rxd_stat = 0;
747 1.1 tsutsui MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
748 1.1 tsutsui MEC_RXBUFSYNC(sc, i, ETHER_MAX_LEN, BUS_DMASYNC_PREREAD);
749 1.1 tsutsui bus_space_write_8(st, sh, MEC_MCL_RX_FIFO, MEC_CDRXADDR(sc, i));
750 1.1 tsutsui }
751 1.1 tsutsui sc->sc_rxptr = 0;
752 1.1 tsutsui
753 1.1 tsutsui #if 0 /* XXX no info */
754 1.1 tsutsui bus_space_write_8(st, sh, MEC_TIMER, 0);
755 1.1 tsutsui #endif
756 1.1 tsutsui
757 1.1 tsutsui /*
758 1.1 tsutsui * MEC_DMA_TX_INT_ENABLE will be set later otherwise it causes
759 1.1 tsutsui * spurious interrupts when TX buffers are empty
760 1.1 tsutsui */
761 1.1 tsutsui bus_space_write_8(st, sh, MEC_DMA_CONTROL,
762 1.1 tsutsui (MEC_RXD_DMAOFFSET << MEC_DMA_RX_DMA_OFFSET_SHIFT) |
763 1.1 tsutsui (MEC_NRXDESC << MEC_DMA_RX_INT_THRESH_SHIFT) |
764 1.1 tsutsui MEC_DMA_TX_DMA_ENABLE | /* MEC_DMA_TX_INT_ENABLE | */
765 1.1 tsutsui MEC_DMA_RX_DMA_ENABLE | MEC_DMA_RX_INT_ENABLE);
766 1.1 tsutsui
767 1.1 tsutsui callout_reset(&sc->sc_tick_ch, hz, mec_tick, sc);
768 1.1 tsutsui
769 1.15 dyoung if ((rc = ether_mediachange(ifp)) != 0)
770 1.15 dyoung return rc;
771 1.15 dyoung
772 1.1 tsutsui ifp->if_flags |= IFF_RUNNING;
773 1.1 tsutsui ifp->if_flags &= ~IFF_OACTIVE;
774 1.1 tsutsui mec_start(ifp);
775 1.1 tsutsui
776 1.1 tsutsui return 0;
777 1.1 tsutsui }
778 1.1 tsutsui
779 1.19 tsutsui static void
780 1.1 tsutsui mec_reset(struct mec_softc *sc)
781 1.1 tsutsui {
782 1.1 tsutsui bus_space_tag_t st = sc->sc_st;
783 1.1 tsutsui bus_space_handle_t sh = sc->sc_sh;
784 1.17 tsutsui uint64_t control;
785 1.17 tsutsui
786 1.17 tsutsui /* stop DMA first */
787 1.17 tsutsui bus_space_write_8(st, sh, MEC_DMA_CONTROL, 0);
788 1.1 tsutsui
789 1.1 tsutsui /* reset chip */
790 1.1 tsutsui bus_space_write_8(st, sh, MEC_MAC_CONTROL, MEC_MAC_CORE_RESET);
791 1.8 tsutsui delay(1000);
792 1.1 tsutsui bus_space_write_8(st, sh, MEC_MAC_CONTROL, 0);
793 1.1 tsutsui delay(1000);
794 1.1 tsutsui
795 1.14 tsutsui /* Default to 100/half and let auto-negotiation work its magic */
796 1.1 tsutsui control = MEC_MAC_SPEED_SELECT | MEC_MAC_FILTER_MATCHMULTI |
797 1.1 tsutsui MEC_MAC_IPG_DEFAULT;
798 1.1 tsutsui
799 1.1 tsutsui bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
800 1.17 tsutsui /* stop DMA again for sanity */
801 1.1 tsutsui bus_space_write_8(st, sh, MEC_DMA_CONTROL, 0);
802 1.1 tsutsui
803 1.1 tsutsui DPRINTF(MEC_DEBUG_RESET, ("mec: control now %llx\n",
804 1.1 tsutsui bus_space_read_8(st, sh, MEC_MAC_CONTROL)));
805 1.1 tsutsui }
806 1.1 tsutsui
807 1.19 tsutsui static void
808 1.1 tsutsui mec_start(struct ifnet *ifp)
809 1.1 tsutsui {
810 1.1 tsutsui struct mec_softc *sc = ifp->if_softc;
811 1.1 tsutsui struct mbuf *m0, *m;
812 1.1 tsutsui struct mec_txdesc *txd;
813 1.1 tsutsui struct mec_txsoft *txs;
814 1.1 tsutsui bus_dmamap_t dmamap;
815 1.1 tsutsui bus_space_tag_t st = sc->sc_st;
816 1.1 tsutsui bus_space_handle_t sh = sc->sc_sh;
817 1.1 tsutsui uint64_t txdaddr;
818 1.1 tsutsui int error, firsttx, nexttx, opending;
819 1.1 tsutsui int len, bufoff, buflen, unaligned, txdlen;
820 1.1 tsutsui
821 1.1 tsutsui if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
822 1.1 tsutsui return;
823 1.1 tsutsui
824 1.1 tsutsui /*
825 1.1 tsutsui * Remember the previous txpending and the first transmit descriptor.
826 1.1 tsutsui */
827 1.1 tsutsui opending = sc->sc_txpending;
828 1.1 tsutsui firsttx = MEC_NEXTTX(sc->sc_txlast);
829 1.1 tsutsui
830 1.1 tsutsui DPRINTF(MEC_DEBUG_START,
831 1.1 tsutsui ("mec_start: opending = %d, firsttx = %d\n", opending, firsttx));
832 1.1 tsutsui
833 1.24 tsutsui while (sc->sc_txpending < MEC_NTXDESC - 1) {
834 1.1 tsutsui /* Grab a packet off the queue. */
835 1.1 tsutsui IFQ_POLL(&ifp->if_snd, m0);
836 1.1 tsutsui if (m0 == NULL)
837 1.1 tsutsui break;
838 1.1 tsutsui m = NULL;
839 1.1 tsutsui
840 1.1 tsutsui /*
841 1.1 tsutsui * Get the next available transmit descriptor.
842 1.1 tsutsui */
843 1.1 tsutsui nexttx = MEC_NEXTTX(sc->sc_txlast);
844 1.1 tsutsui txd = &sc->sc_txdesc[nexttx];
845 1.1 tsutsui txs = &sc->sc_txsoft[nexttx];
846 1.1 tsutsui
847 1.1 tsutsui buflen = 0;
848 1.1 tsutsui bufoff = 0;
849 1.1 tsutsui txdaddr = 0; /* XXX gcc */
850 1.1 tsutsui txdlen = 0; /* XXX gcc */
851 1.1 tsutsui
852 1.1 tsutsui len = m0->m_pkthdr.len;
853 1.1 tsutsui
854 1.1 tsutsui DPRINTF(MEC_DEBUG_START,
855 1.1 tsutsui ("mec_start: len = %d, nexttx = %d\n", len, nexttx));
856 1.1 tsutsui
857 1.1 tsutsui if (len < ETHER_PAD_LEN) {
858 1.1 tsutsui /*
859 1.1 tsutsui * I don't know if MEC chip does auto padding,
860 1.1 tsutsui * so if the packet is small enough,
861 1.1 tsutsui * just copy it to the buffer in txdesc.
862 1.1 tsutsui * Maybe this is the simple way.
863 1.1 tsutsui */
864 1.1 tsutsui DPRINTF(MEC_DEBUG_START, ("mec_start: short packet\n"));
865 1.1 tsutsui
866 1.1 tsutsui IFQ_DEQUEUE(&ifp->if_snd, m0);
867 1.1 tsutsui bufoff = MEC_TXD_BUFSTART(ETHER_PAD_LEN);
868 1.1 tsutsui m_copydata(m0, 0, m0->m_pkthdr.len,
869 1.1 tsutsui txd->txd_buf + bufoff);
870 1.1 tsutsui memset(txd->txd_buf + bufoff + len, 0,
871 1.1 tsutsui ETHER_PAD_LEN - len);
872 1.1 tsutsui len = buflen = ETHER_PAD_LEN;
873 1.1 tsutsui
874 1.1 tsutsui txs->txs_flags = MEC_TXS_TXDBUF | buflen;
875 1.1 tsutsui } else {
876 1.1 tsutsui /*
877 1.1 tsutsui * If the packet won't fit the buffer in txdesc,
878 1.14 tsutsui * we have to use concatenate pointer to handle it.
879 1.1 tsutsui * While MEC can handle up to three segments to
880 1.14 tsutsui * concatenate, MEC requires that both the second and
881 1.1 tsutsui * third segments have to be 8 byte aligned.
882 1.1 tsutsui * Since it's unlikely for mbuf clusters, we use
883 1.14 tsutsui * only the first concatenate pointer. If the packet
884 1.1 tsutsui * doesn't fit in one DMA segment, allocate new mbuf
885 1.1 tsutsui * and copy the packet to it.
886 1.1 tsutsui *
887 1.1 tsutsui * Besides, if the start address of the first segments
888 1.1 tsutsui * is not 8 byte aligned, such part have to be copied
889 1.1 tsutsui * to the txdesc buffer. (XXX see below comments)
890 1.1 tsutsui */
891 1.1 tsutsui DPRINTF(MEC_DEBUG_START, ("mec_start: long packet\n"));
892 1.1 tsutsui
893 1.1 tsutsui dmamap = txs->txs_dmamap;
894 1.1 tsutsui if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
895 1.1 tsutsui BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0) {
896 1.1 tsutsui DPRINTF(MEC_DEBUG_START,
897 1.1 tsutsui ("mec_start: re-allocating mbuf\n"));
898 1.1 tsutsui MGETHDR(m, M_DONTWAIT, MT_DATA);
899 1.1 tsutsui if (m == NULL) {
900 1.1 tsutsui printf("%s: unable to allocate "
901 1.19 tsutsui "TX mbuf\n",
902 1.19 tsutsui device_xname(sc->sc_dev));
903 1.1 tsutsui break;
904 1.1 tsutsui }
905 1.1 tsutsui if (len > (MHLEN - MEC_ETHER_ALIGN)) {
906 1.1 tsutsui MCLGET(m, M_DONTWAIT);
907 1.1 tsutsui if ((m->m_flags & M_EXT) == 0) {
908 1.1 tsutsui printf("%s: unable to allocate "
909 1.1 tsutsui "TX cluster\n",
910 1.19 tsutsui device_xname(sc->sc_dev));
911 1.1 tsutsui m_freem(m);
912 1.1 tsutsui break;
913 1.1 tsutsui }
914 1.1 tsutsui }
915 1.1 tsutsui /*
916 1.1 tsutsui * Each packet has the Ethernet header, so
917 1.1 tsutsui * in many case the header isn't 4-byte aligned
918 1.1 tsutsui * and data after the header is 4-byte aligned.
919 1.1 tsutsui * Thus adding 2-byte offset before copying to
920 1.1 tsutsui * new mbuf avoids unaligned copy and this may
921 1.1 tsutsui * improve some performance.
922 1.1 tsutsui * As noted above, unaligned part has to be
923 1.1 tsutsui * copied to txdesc buffer so this may cause
924 1.1 tsutsui * extra copy ops, but for now MEC always
925 1.1 tsutsui * requires some data in txdesc buffer,
926 1.1 tsutsui * so we always have to copy some data anyway.
927 1.1 tsutsui */
928 1.1 tsutsui m->m_data += MEC_ETHER_ALIGN;
929 1.10 christos m_copydata(m0, 0, len, mtod(m, void *));
930 1.1 tsutsui m->m_pkthdr.len = m->m_len = len;
931 1.1 tsutsui error = bus_dmamap_load_mbuf(sc->sc_dmat,
932 1.1 tsutsui dmamap, m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
933 1.1 tsutsui if (error) {
934 1.1 tsutsui printf("%s: unable to load TX buffer, "
935 1.1 tsutsui "error = %d\n",
936 1.19 tsutsui device_xname(sc->sc_dev), error);
937 1.1 tsutsui break;
938 1.1 tsutsui }
939 1.1 tsutsui }
940 1.1 tsutsui IFQ_DEQUEUE(&ifp->if_snd, m0);
941 1.1 tsutsui if (m != NULL) {
942 1.1 tsutsui m_freem(m0);
943 1.1 tsutsui m0 = m;
944 1.1 tsutsui }
945 1.1 tsutsui
946 1.1 tsutsui /* handle unaligned part */
947 1.1 tsutsui txdaddr = MEC_TXD_ROUNDUP(dmamap->dm_segs[0].ds_addr);
948 1.1 tsutsui txs->txs_flags = MEC_TXS_TXDPTR1;
949 1.1 tsutsui unaligned =
950 1.1 tsutsui dmamap->dm_segs[0].ds_addr & (MEC_TXD_ALIGN - 1);
951 1.1 tsutsui DPRINTF(MEC_DEBUG_START,
952 1.1 tsutsui ("mec_start: ds_addr = 0x%08x, unaligned = %d\n",
953 1.1 tsutsui (u_int)dmamap->dm_segs[0].ds_addr, unaligned));
954 1.1 tsutsui if (unaligned != 0) {
955 1.1 tsutsui buflen = MEC_TXD_ALIGN - unaligned;
956 1.1 tsutsui bufoff = MEC_TXD_BUFSTART(buflen);
957 1.1 tsutsui DPRINTF(MEC_DEBUG_START,
958 1.1 tsutsui ("mec_start: unaligned, "
959 1.1 tsutsui "buflen = %d, bufoff = %d\n",
960 1.1 tsutsui buflen, bufoff));
961 1.1 tsutsui memcpy(txd->txd_buf + bufoff,
962 1.10 christos mtod(m0, void *), buflen);
963 1.1 tsutsui txs->txs_flags |= MEC_TXS_TXDBUF | buflen;
964 1.1 tsutsui }
965 1.1 tsutsui #if 1
966 1.1 tsutsui else {
967 1.1 tsutsui /*
968 1.1 tsutsui * XXX needs hardware info XXX
969 1.1 tsutsui * It seems MEC always requires some data
970 1.1 tsutsui * in txd_buf[] even if buffer is
971 1.1 tsutsui * 8-byte aligned otherwise DMA abort error
972 1.1 tsutsui * occurs later...
973 1.1 tsutsui */
974 1.1 tsutsui buflen = MEC_TXD_ALIGN;
975 1.1 tsutsui bufoff = MEC_TXD_BUFSTART(buflen);
976 1.1 tsutsui memcpy(txd->txd_buf + bufoff,
977 1.10 christos mtod(m0, void *), buflen);
978 1.1 tsutsui DPRINTF(MEC_DEBUG_START,
979 1.1 tsutsui ("mec_start: aligned, "
980 1.1 tsutsui "buflen = %d, bufoff = %d\n",
981 1.1 tsutsui buflen, bufoff));
982 1.1 tsutsui txs->txs_flags |= MEC_TXS_TXDBUF | buflen;
983 1.1 tsutsui txdaddr += MEC_TXD_ALIGN;
984 1.1 tsutsui }
985 1.1 tsutsui #endif
986 1.1 tsutsui txdlen = len - buflen;
987 1.1 tsutsui DPRINTF(MEC_DEBUG_START,
988 1.1 tsutsui ("mec_start: txdaddr = 0x%08llx, txdlen = %d\n",
989 1.1 tsutsui txdaddr, txdlen));
990 1.1 tsutsui
991 1.1 tsutsui /*
992 1.1 tsutsui * sync the DMA map for TX mbuf
993 1.1 tsutsui *
994 1.1 tsutsui * XXX unaligned part doesn't have to be sync'ed,
995 1.1 tsutsui * but it's harmless...
996 1.1 tsutsui */
997 1.1 tsutsui bus_dmamap_sync(sc->sc_dmat, dmamap, 0,
998 1.1 tsutsui dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
999 1.1 tsutsui }
1000 1.1 tsutsui
1001 1.1 tsutsui #if NBPFILTER > 0
1002 1.1 tsutsui /*
1003 1.1 tsutsui * Pass packet to bpf if there is a listener.
1004 1.1 tsutsui */
1005 1.1 tsutsui if (ifp->if_bpf)
1006 1.1 tsutsui bpf_mtap(ifp->if_bpf, m0);
1007 1.1 tsutsui #endif
1008 1.1 tsutsui
1009 1.1 tsutsui /*
1010 1.1 tsutsui * setup the transmit descriptor.
1011 1.1 tsutsui */
1012 1.1 tsutsui
1013 1.1 tsutsui /* TXINT bit will be set later on the last packet */
1014 1.1 tsutsui txd->txd_cmd = (len - 1);
1015 1.1 tsutsui /* but also set TXINT bit on a half of TXDESC */
1016 1.1 tsutsui if (sc->sc_txpending == (MEC_NTXDESC / 2))
1017 1.1 tsutsui txd->txd_cmd |= MEC_TXCMD_TXINT;
1018 1.1 tsutsui
1019 1.1 tsutsui if (txs->txs_flags & MEC_TXS_TXDBUF)
1020 1.1 tsutsui txd->txd_cmd |= TXCMD_BUFSTART(MEC_TXDESCSIZE - buflen);
1021 1.1 tsutsui if (txs->txs_flags & MEC_TXS_TXDPTR1) {
1022 1.1 tsutsui txd->txd_cmd |= MEC_TXCMD_PTR1;
1023 1.1 tsutsui txd->txd_ptr[0] = TXPTR_LEN(txdlen - 1) | txdaddr;
1024 1.1 tsutsui /*
1025 1.1 tsutsui * Store a pointer to the packet so we can
1026 1.1 tsutsui * free it later.
1027 1.1 tsutsui */
1028 1.1 tsutsui txs->txs_mbuf = m0;
1029 1.1 tsutsui } else {
1030 1.1 tsutsui txd->txd_ptr[0] = 0;
1031 1.1 tsutsui /*
1032 1.1 tsutsui * In this case all data are copied to buffer in txdesc,
1033 1.1 tsutsui * we can free TX mbuf here.
1034 1.1 tsutsui */
1035 1.1 tsutsui m_freem(m0);
1036 1.1 tsutsui }
1037 1.1 tsutsui
1038 1.1 tsutsui DPRINTF(MEC_DEBUG_START,
1039 1.1 tsutsui ("mec_start: txd_cmd = 0x%016llx, txd_ptr = 0x%016llx\n",
1040 1.1 tsutsui txd->txd_cmd, txd->txd_ptr[0]));
1041 1.1 tsutsui DPRINTF(MEC_DEBUG_START,
1042 1.1 tsutsui ("mec_start: len = %d (0x%04x), buflen = %d (0x%02x)\n",
1043 1.1 tsutsui len, len, buflen, buflen));
1044 1.1 tsutsui
1045 1.1 tsutsui /* sync TX descriptor */
1046 1.1 tsutsui MEC_TXDESCSYNC(sc, nexttx,
1047 1.1 tsutsui BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1048 1.1 tsutsui
1049 1.25 tsutsui /* start TX */
1050 1.25 tsutsui bus_space_write_8(st, sh, MEC_TX_RING_PTR, MEC_NEXTTX(nexttx));
1051 1.25 tsutsui
1052 1.1 tsutsui /* advance the TX pointer. */
1053 1.1 tsutsui sc->sc_txpending++;
1054 1.1 tsutsui sc->sc_txlast = nexttx;
1055 1.1 tsutsui }
1056 1.1 tsutsui
1057 1.23 tsutsui if (sc->sc_txpending == MEC_NTXDESC - 1) {
1058 1.1 tsutsui /* No more slots; notify upper layer. */
1059 1.1 tsutsui ifp->if_flags |= IFF_OACTIVE;
1060 1.1 tsutsui }
1061 1.1 tsutsui
1062 1.1 tsutsui if (sc->sc_txpending != opending) {
1063 1.1 tsutsui /*
1064 1.1 tsutsui * If the transmitter was idle,
1065 1.14 tsutsui * reset the txdirty pointer and re-enable TX interrupt.
1066 1.1 tsutsui */
1067 1.1 tsutsui if (opending == 0) {
1068 1.1 tsutsui sc->sc_txdirty = firsttx;
1069 1.1 tsutsui bus_space_write_8(st, sh, MEC_TX_ALIAS,
1070 1.1 tsutsui MEC_TX_ALIAS_INT_ENABLE);
1071 1.1 tsutsui }
1072 1.1 tsutsui
1073 1.1 tsutsui /* Set a watchdog timer in case the chip flakes out. */
1074 1.1 tsutsui ifp->if_timer = 5;
1075 1.1 tsutsui }
1076 1.1 tsutsui }
1077 1.1 tsutsui
1078 1.19 tsutsui static void
1079 1.1 tsutsui mec_stop(struct ifnet *ifp, int disable)
1080 1.1 tsutsui {
1081 1.1 tsutsui struct mec_softc *sc = ifp->if_softc;
1082 1.1 tsutsui struct mec_txsoft *txs;
1083 1.1 tsutsui int i;
1084 1.1 tsutsui
1085 1.1 tsutsui DPRINTF(MEC_DEBUG_STOP, ("mec_stop\n"));
1086 1.1 tsutsui
1087 1.1 tsutsui ifp->if_timer = 0;
1088 1.1 tsutsui ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1089 1.1 tsutsui
1090 1.1 tsutsui callout_stop(&sc->sc_tick_ch);
1091 1.1 tsutsui mii_down(&sc->sc_mii);
1092 1.1 tsutsui
1093 1.1 tsutsui /* release any TX buffers */
1094 1.1 tsutsui for (i = 0; i < MEC_NTXDESC; i++) {
1095 1.1 tsutsui txs = &sc->sc_txsoft[i];
1096 1.1 tsutsui if ((txs->txs_flags & MEC_TXS_TXDPTR1) != 0) {
1097 1.1 tsutsui bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1098 1.1 tsutsui m_freem(txs->txs_mbuf);
1099 1.1 tsutsui txs->txs_mbuf = NULL;
1100 1.1 tsutsui }
1101 1.1 tsutsui }
1102 1.1 tsutsui }
1103 1.1 tsutsui
1104 1.19 tsutsui static int
1105 1.10 christos mec_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1106 1.1 tsutsui {
1107 1.1 tsutsui int s, error;
1108 1.1 tsutsui
1109 1.1 tsutsui s = splnet();
1110 1.1 tsutsui
1111 1.15 dyoung error = ether_ioctl(ifp, cmd, data);
1112 1.15 dyoung if (error == ENETRESET) {
1113 1.15 dyoung /*
1114 1.15 dyoung * Multicast list has changed; set the hardware filter
1115 1.15 dyoung * accordingly.
1116 1.15 dyoung */
1117 1.15 dyoung if (ifp->if_flags & IFF_RUNNING)
1118 1.15 dyoung error = mec_init(ifp);
1119 1.15 dyoung else
1120 1.15 dyoung error = 0;
1121 1.1 tsutsui }
1122 1.1 tsutsui
1123 1.1 tsutsui /* Try to get more packets going. */
1124 1.1 tsutsui mec_start(ifp);
1125 1.1 tsutsui
1126 1.1 tsutsui splx(s);
1127 1.1 tsutsui return error;
1128 1.1 tsutsui }
1129 1.1 tsutsui
1130 1.19 tsutsui static void
1131 1.1 tsutsui mec_watchdog(struct ifnet *ifp)
1132 1.1 tsutsui {
1133 1.1 tsutsui struct mec_softc *sc = ifp->if_softc;
1134 1.1 tsutsui
1135 1.19 tsutsui printf("%s: device timeout\n", device_xname(sc->sc_dev));
1136 1.1 tsutsui ifp->if_oerrors++;
1137 1.1 tsutsui
1138 1.1 tsutsui mec_init(ifp);
1139 1.1 tsutsui }
1140 1.1 tsutsui
1141 1.19 tsutsui static void
1142 1.1 tsutsui mec_tick(void *arg)
1143 1.1 tsutsui {
1144 1.1 tsutsui struct mec_softc *sc = arg;
1145 1.1 tsutsui int s;
1146 1.1 tsutsui
1147 1.1 tsutsui s = splnet();
1148 1.1 tsutsui mii_tick(&sc->sc_mii);
1149 1.1 tsutsui splx(s);
1150 1.1 tsutsui
1151 1.1 tsutsui callout_reset(&sc->sc_tick_ch, hz, mec_tick, sc);
1152 1.1 tsutsui }
1153 1.1 tsutsui
1154 1.19 tsutsui static void
1155 1.1 tsutsui mec_setfilter(struct mec_softc *sc)
1156 1.1 tsutsui {
1157 1.1 tsutsui struct ethercom *ec = &sc->sc_ethercom;
1158 1.1 tsutsui struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1159 1.1 tsutsui struct ether_multi *enm;
1160 1.1 tsutsui struct ether_multistep step;
1161 1.1 tsutsui bus_space_tag_t st = sc->sc_st;
1162 1.1 tsutsui bus_space_handle_t sh = sc->sc_sh;
1163 1.1 tsutsui uint64_t mchash;
1164 1.1 tsutsui uint32_t control, hash;
1165 1.1 tsutsui int mcnt;
1166 1.1 tsutsui
1167 1.1 tsutsui control = bus_space_read_8(st, sh, MEC_MAC_CONTROL);
1168 1.1 tsutsui control &= ~MEC_MAC_FILTER_MASK;
1169 1.1 tsutsui
1170 1.1 tsutsui if (ifp->if_flags & IFF_PROMISC) {
1171 1.1 tsutsui control |= MEC_MAC_FILTER_PROMISC;
1172 1.1 tsutsui bus_space_write_8(st, sh, MEC_MULTICAST, 0xffffffffffffffffULL);
1173 1.1 tsutsui bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
1174 1.1 tsutsui return;
1175 1.1 tsutsui }
1176 1.1 tsutsui
1177 1.1 tsutsui mcnt = 0;
1178 1.1 tsutsui mchash = 0;
1179 1.1 tsutsui ETHER_FIRST_MULTI(step, ec, enm);
1180 1.1 tsutsui while (enm != NULL) {
1181 1.1 tsutsui if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1182 1.1 tsutsui /* set allmulti for a range of multicast addresses */
1183 1.1 tsutsui control |= MEC_MAC_FILTER_ALLMULTI;
1184 1.1 tsutsui bus_space_write_8(st, sh, MEC_MULTICAST,
1185 1.1 tsutsui 0xffffffffffffffffULL);
1186 1.1 tsutsui bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
1187 1.1 tsutsui return;
1188 1.1 tsutsui }
1189 1.1 tsutsui
1190 1.1 tsutsui #define mec_calchash(addr) (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
1191 1.1 tsutsui
1192 1.1 tsutsui hash = mec_calchash(enm->enm_addrlo);
1193 1.1 tsutsui mchash |= 1 << hash;
1194 1.1 tsutsui mcnt++;
1195 1.1 tsutsui ETHER_NEXT_MULTI(step, enm);
1196 1.1 tsutsui }
1197 1.1 tsutsui
1198 1.1 tsutsui ifp->if_flags &= ~IFF_ALLMULTI;
1199 1.1 tsutsui
1200 1.1 tsutsui if (mcnt > 0)
1201 1.1 tsutsui control |= MEC_MAC_FILTER_MATCHMULTI;
1202 1.1 tsutsui
1203 1.1 tsutsui bus_space_write_8(st, sh, MEC_MULTICAST, mchash);
1204 1.1 tsutsui bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
1205 1.1 tsutsui }
1206 1.1 tsutsui
1207 1.19 tsutsui static int
1208 1.1 tsutsui mec_intr(void *arg)
1209 1.1 tsutsui {
1210 1.1 tsutsui struct mec_softc *sc = arg;
1211 1.1 tsutsui bus_space_tag_t st = sc->sc_st;
1212 1.1 tsutsui bus_space_handle_t sh = sc->sc_sh;
1213 1.1 tsutsui struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1214 1.21 tsutsui uint32_t statreg, statack, txptr;
1215 1.1 tsutsui int handled, sent;
1216 1.1 tsutsui
1217 1.1 tsutsui DPRINTF(MEC_DEBUG_INTR, ("mec_intr: called\n"));
1218 1.1 tsutsui
1219 1.1 tsutsui handled = sent = 0;
1220 1.1 tsutsui
1221 1.1 tsutsui for (;;) {
1222 1.1 tsutsui statreg = bus_space_read_8(st, sh, MEC_INT_STATUS);
1223 1.1 tsutsui
1224 1.1 tsutsui DPRINTF(MEC_DEBUG_INTR,
1225 1.1 tsutsui ("mec_intr: INT_STAT = 0x%08x\n", statreg));
1226 1.1 tsutsui
1227 1.1 tsutsui statack = statreg & MEC_INT_STATUS_MASK;
1228 1.1 tsutsui if (statack == 0)
1229 1.1 tsutsui break;
1230 1.1 tsutsui bus_space_write_8(st, sh, MEC_INT_STATUS, statack);
1231 1.1 tsutsui
1232 1.1 tsutsui handled = 1;
1233 1.1 tsutsui
1234 1.1 tsutsui if (statack &
1235 1.1 tsutsui (MEC_INT_RX_THRESHOLD |
1236 1.1 tsutsui MEC_INT_RX_FIFO_UNDERFLOW)) {
1237 1.1 tsutsui mec_rxintr(sc);
1238 1.1 tsutsui }
1239 1.1 tsutsui
1240 1.1 tsutsui if (statack &
1241 1.1 tsutsui (MEC_INT_TX_EMPTY |
1242 1.1 tsutsui MEC_INT_TX_PACKET_SENT |
1243 1.1 tsutsui MEC_INT_TX_ABORT)) {
1244 1.21 tsutsui txptr = (statreg & MEC_INT_TX_RING_BUFFER_ALIAS)
1245 1.21 tsutsui >> MEC_INT_TX_RING_BUFFER_SHIFT;
1246 1.21 tsutsui mec_txintr(sc, txptr);
1247 1.1 tsutsui sent = 1;
1248 1.21 tsutsui if ((statack & MEC_INT_TX_EMPTY) != 0) {
1249 1.1 tsutsui /*
1250 1.1 tsutsui * disable TX interrupt to stop
1251 1.1 tsutsui * TX empty interrupt
1252 1.1 tsutsui */
1253 1.1 tsutsui bus_space_write_8(st, sh, MEC_TX_ALIAS, 0);
1254 1.1 tsutsui DPRINTF(MEC_DEBUG_INTR,
1255 1.1 tsutsui ("mec_intr: disable TX_INT\n"));
1256 1.1 tsutsui }
1257 1.1 tsutsui }
1258 1.1 tsutsui
1259 1.1 tsutsui if (statack &
1260 1.1 tsutsui (MEC_INT_TX_LINK_FAIL |
1261 1.1 tsutsui MEC_INT_TX_MEM_ERROR |
1262 1.1 tsutsui MEC_INT_TX_ABORT |
1263 1.1 tsutsui MEC_INT_RX_FIFO_UNDERFLOW |
1264 1.1 tsutsui MEC_INT_RX_DMA_UNDERFLOW)) {
1265 1.1 tsutsui printf("%s: mec_intr: interrupt status = 0x%08x\n",
1266 1.19 tsutsui device_xname(sc->sc_dev), statreg);
1267 1.26 tsutsui mec_init(ifp);
1268 1.26 tsutsui break;
1269 1.1 tsutsui }
1270 1.1 tsutsui }
1271 1.1 tsutsui
1272 1.21 tsutsui if (sent && IFQ_IS_EMPTY(&ifp->if_snd)) {
1273 1.1 tsutsui /* try to get more packets going */
1274 1.1 tsutsui mec_start(ifp);
1275 1.1 tsutsui }
1276 1.1 tsutsui
1277 1.1 tsutsui #if NRND > 0
1278 1.1 tsutsui if (handled)
1279 1.1 tsutsui rnd_add_uint32(&sc->sc_rnd_source, statreg);
1280 1.1 tsutsui #endif
1281 1.1 tsutsui
1282 1.1 tsutsui return handled;
1283 1.1 tsutsui }
1284 1.1 tsutsui
1285 1.19 tsutsui static void
1286 1.1 tsutsui mec_rxintr(struct mec_softc *sc)
1287 1.1 tsutsui {
1288 1.1 tsutsui bus_space_tag_t st = sc->sc_st;
1289 1.1 tsutsui bus_space_handle_t sh = sc->sc_sh;
1290 1.1 tsutsui struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1291 1.1 tsutsui struct mbuf *m;
1292 1.1 tsutsui struct mec_rxdesc *rxd;
1293 1.1 tsutsui uint64_t rxstat;
1294 1.1 tsutsui u_int len;
1295 1.1 tsutsui int i;
1296 1.1 tsutsui
1297 1.1 tsutsui DPRINTF(MEC_DEBUG_RXINTR, ("mec_rxintr: called\n"));
1298 1.1 tsutsui
1299 1.1 tsutsui for (i = sc->sc_rxptr;; i = MEC_NEXTRX(i)) {
1300 1.1 tsutsui rxd = &sc->sc_rxdesc[i];
1301 1.1 tsutsui
1302 1.1 tsutsui MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_POSTREAD);
1303 1.1 tsutsui rxstat = rxd->rxd_stat;
1304 1.1 tsutsui
1305 1.1 tsutsui DPRINTF(MEC_DEBUG_RXINTR,
1306 1.1 tsutsui ("mec_rxintr: rxstat = 0x%016llx, rxptr = %d\n",
1307 1.1 tsutsui rxstat, i));
1308 1.1 tsutsui DPRINTF(MEC_DEBUG_RXINTR, ("mec_rxintr: rxfifo = 0x%08x\n",
1309 1.1 tsutsui (u_int)bus_space_read_8(st, sh, MEC_RX_FIFO)));
1310 1.1 tsutsui
1311 1.1 tsutsui if ((rxstat & MEC_RXSTAT_RECEIVED) == 0) {
1312 1.1 tsutsui MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
1313 1.1 tsutsui break;
1314 1.1 tsutsui }
1315 1.1 tsutsui
1316 1.1 tsutsui len = rxstat & MEC_RXSTAT_LEN;
1317 1.1 tsutsui
1318 1.1 tsutsui if (len < ETHER_MIN_LEN ||
1319 1.9 tsutsui len > (MCLBYTES - MEC_ETHER_ALIGN)) {
1320 1.1 tsutsui /* invalid length packet; drop it. */
1321 1.1 tsutsui DPRINTF(MEC_DEBUG_RXINTR,
1322 1.1 tsutsui ("mec_rxintr: wrong packet\n"));
1323 1.1 tsutsui dropit:
1324 1.1 tsutsui ifp->if_ierrors++;
1325 1.1 tsutsui rxd->rxd_stat = 0;
1326 1.1 tsutsui MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
1327 1.1 tsutsui bus_space_write_8(st, sh, MEC_MCL_RX_FIFO,
1328 1.1 tsutsui MEC_CDRXADDR(sc, i));
1329 1.1 tsutsui continue;
1330 1.1 tsutsui }
1331 1.1 tsutsui
1332 1.1 tsutsui if (rxstat &
1333 1.1 tsutsui (MEC_RXSTAT_BADPACKET |
1334 1.1 tsutsui MEC_RXSTAT_LONGEVENT |
1335 1.1 tsutsui MEC_RXSTAT_INVALID |
1336 1.1 tsutsui MEC_RXSTAT_CRCERROR |
1337 1.1 tsutsui MEC_RXSTAT_VIOLATION)) {
1338 1.1 tsutsui printf("%s: mec_rxintr: status = 0x%016llx\n",
1339 1.19 tsutsui device_xname(sc->sc_dev), rxstat);
1340 1.1 tsutsui goto dropit;
1341 1.1 tsutsui }
1342 1.1 tsutsui
1343 1.1 tsutsui /*
1344 1.4 thorpej * The MEC includes the CRC with every packet. Trim
1345 1.4 thorpej * it off here.
1346 1.4 thorpej */
1347 1.4 thorpej len -= ETHER_CRC_LEN;
1348 1.4 thorpej
1349 1.4 thorpej /*
1350 1.1 tsutsui * now allocate an mbuf (and possibly a cluster) to hold
1351 1.1 tsutsui * the received packet.
1352 1.1 tsutsui */
1353 1.1 tsutsui MGETHDR(m, M_DONTWAIT, MT_DATA);
1354 1.1 tsutsui if (m == NULL) {
1355 1.1 tsutsui printf("%s: unable to allocate RX mbuf\n",
1356 1.19 tsutsui device_xname(sc->sc_dev));
1357 1.1 tsutsui goto dropit;
1358 1.1 tsutsui }
1359 1.1 tsutsui if (len > (MHLEN - MEC_ETHER_ALIGN)) {
1360 1.1 tsutsui MCLGET(m, M_DONTWAIT);
1361 1.1 tsutsui if ((m->m_flags & M_EXT) == 0) {
1362 1.1 tsutsui printf("%s: unable to allocate RX cluster\n",
1363 1.19 tsutsui device_xname(sc->sc_dev));
1364 1.1 tsutsui m_freem(m);
1365 1.1 tsutsui m = NULL;
1366 1.1 tsutsui goto dropit;
1367 1.1 tsutsui }
1368 1.1 tsutsui }
1369 1.1 tsutsui
1370 1.1 tsutsui /*
1371 1.1 tsutsui * Note MEC chip seems to insert 2 byte padding at the top of
1372 1.1 tsutsui * RX buffer, but we copy whole buffer to avoid unaligned copy.
1373 1.1 tsutsui */
1374 1.1 tsutsui MEC_RXBUFSYNC(sc, i, len, BUS_DMASYNC_POSTREAD);
1375 1.10 christos memcpy(mtod(m, void *), rxd->rxd_buf, MEC_ETHER_ALIGN + len);
1376 1.1 tsutsui MEC_RXBUFSYNC(sc, i, ETHER_MAX_LEN, BUS_DMASYNC_PREREAD);
1377 1.1 tsutsui m->m_data += MEC_ETHER_ALIGN;
1378 1.1 tsutsui
1379 1.1 tsutsui /* put RX buffer into FIFO again */
1380 1.1 tsutsui rxd->rxd_stat = 0;
1381 1.1 tsutsui MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
1382 1.1 tsutsui bus_space_write_8(st, sh, MEC_MCL_RX_FIFO, MEC_CDRXADDR(sc, i));
1383 1.1 tsutsui
1384 1.1 tsutsui m->m_pkthdr.rcvif = ifp;
1385 1.1 tsutsui m->m_pkthdr.len = m->m_len = len;
1386 1.1 tsutsui
1387 1.1 tsutsui ifp->if_ipackets++;
1388 1.1 tsutsui
1389 1.1 tsutsui #if NBPFILTER > 0
1390 1.1 tsutsui /*
1391 1.1 tsutsui * Pass this up to any BPF listeners, but only
1392 1.14 tsutsui * pass it up the stack if it's for us.
1393 1.1 tsutsui */
1394 1.1 tsutsui if (ifp->if_bpf)
1395 1.1 tsutsui bpf_mtap(ifp->if_bpf, m);
1396 1.1 tsutsui #endif
1397 1.1 tsutsui
1398 1.1 tsutsui /* Pass it on. */
1399 1.1 tsutsui (*ifp->if_input)(ifp, m);
1400 1.1 tsutsui }
1401 1.1 tsutsui
1402 1.1 tsutsui /* update RX pointer */
1403 1.1 tsutsui sc->sc_rxptr = i;
1404 1.1 tsutsui }
1405 1.1 tsutsui
1406 1.19 tsutsui static void
1407 1.21 tsutsui mec_txintr(struct mec_softc *sc, uint32_t txptr)
1408 1.1 tsutsui {
1409 1.1 tsutsui struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1410 1.1 tsutsui struct mec_txdesc *txd;
1411 1.1 tsutsui struct mec_txsoft *txs;
1412 1.1 tsutsui bus_dmamap_t dmamap;
1413 1.1 tsutsui uint64_t txstat;
1414 1.1 tsutsui int i;
1415 1.1 tsutsui u_int col;
1416 1.1 tsutsui
1417 1.1 tsutsui DPRINTF(MEC_DEBUG_TXINTR, ("mec_txintr: called\n"));
1418 1.1 tsutsui
1419 1.21 tsutsui for (i = sc->sc_txdirty; i != txptr && sc->sc_txpending != 0;
1420 1.1 tsutsui i = MEC_NEXTTX(i), sc->sc_txpending--) {
1421 1.1 tsutsui txd = &sc->sc_txdesc[i];
1422 1.1 tsutsui
1423 1.1 tsutsui MEC_TXDESCSYNC(sc, i,
1424 1.1 tsutsui BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1425 1.1 tsutsui
1426 1.1 tsutsui txstat = txd->txd_stat;
1427 1.1 tsutsui DPRINTF(MEC_DEBUG_TXINTR,
1428 1.1 tsutsui ("mec_txintr: dirty = %d, txstat = 0x%016llx\n",
1429 1.1 tsutsui i, txstat));
1430 1.1 tsutsui if ((txstat & MEC_TXSTAT_SENT) == 0) {
1431 1.1 tsutsui MEC_TXCMDSYNC(sc, i, BUS_DMASYNC_PREREAD);
1432 1.1 tsutsui break;
1433 1.1 tsutsui }
1434 1.1 tsutsui
1435 1.1 tsutsui if ((txstat & MEC_TXSTAT_SUCCESS) == 0) {
1436 1.1 tsutsui printf("%s: TX error: txstat = 0x%016llx\n",
1437 1.19 tsutsui device_xname(sc->sc_dev), txstat);
1438 1.1 tsutsui ifp->if_oerrors++;
1439 1.1 tsutsui continue;
1440 1.1 tsutsui }
1441 1.1 tsutsui
1442 1.1 tsutsui txs = &sc->sc_txsoft[i];
1443 1.1 tsutsui if ((txs->txs_flags & MEC_TXS_TXDPTR1) != 0) {
1444 1.1 tsutsui dmamap = txs->txs_dmamap;
1445 1.1 tsutsui bus_dmamap_sync(sc->sc_dmat, dmamap, 0,
1446 1.1 tsutsui dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1447 1.1 tsutsui bus_dmamap_unload(sc->sc_dmat, dmamap);
1448 1.1 tsutsui m_freem(txs->txs_mbuf);
1449 1.1 tsutsui txs->txs_mbuf = NULL;
1450 1.1 tsutsui }
1451 1.1 tsutsui
1452 1.1 tsutsui col = (txstat & MEC_TXSTAT_COLCNT) >> MEC_TXSTAT_COLCNT_SHIFT;
1453 1.1 tsutsui ifp->if_collisions += col;
1454 1.1 tsutsui ifp->if_opackets++;
1455 1.1 tsutsui }
1456 1.1 tsutsui
1457 1.1 tsutsui /* update the dirty TX buffer pointer */
1458 1.1 tsutsui sc->sc_txdirty = i;
1459 1.1 tsutsui DPRINTF(MEC_DEBUG_INTR,
1460 1.1 tsutsui ("mec_txintr: sc_txdirty = %2d, sc_txpending = %2d\n",
1461 1.1 tsutsui sc->sc_txdirty, sc->sc_txpending));
1462 1.1 tsutsui
1463 1.1 tsutsui /* cancel the watchdog timer if there are no pending TX packets */
1464 1.1 tsutsui if (sc->sc_txpending == 0)
1465 1.1 tsutsui ifp->if_timer = 0;
1466 1.21 tsutsui if (sc->sc_txpending < MEC_NTXDESC - MEC_NTXDESC_RSVD)
1467 1.21 tsutsui ifp->if_flags &= ~IFF_OACTIVE;
1468 1.1 tsutsui }
1469 1.1 tsutsui
1470 1.19 tsutsui static void
1471 1.1 tsutsui mec_shutdown(void *arg)
1472 1.1 tsutsui {
1473 1.1 tsutsui struct mec_softc *sc = arg;
1474 1.1 tsutsui
1475 1.1 tsutsui mec_stop(&sc->sc_ethercom.ec_if, 1);
1476 1.17 tsutsui /* make sure to stop DMA etc. */
1477 1.17 tsutsui mec_reset(sc);
1478 1.1 tsutsui }
1479