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