if_mvgbe.c revision 1.32 1 1.32 msaitoh /* $NetBSD: if_mvgbe.c,v 1.32 2012/11/09 09:04:19 msaitoh Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2007, 2008 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara */
27 1.1 kiyohara #include <sys/cdefs.h>
28 1.32 msaitoh __KERNEL_RCSID(0, "$NetBSD: if_mvgbe.c,v 1.32 2012/11/09 09:04:19 msaitoh Exp $");
29 1.1 kiyohara
30 1.1 kiyohara #include <sys/param.h>
31 1.1 kiyohara #include <sys/bus.h>
32 1.27 msaitoh #include <sys/callout.h>
33 1.1 kiyohara #include <sys/device.h>
34 1.1 kiyohara #include <sys/endian.h>
35 1.1 kiyohara #include <sys/errno.h>
36 1.32 msaitoh #include <sys/evcnt.h>
37 1.27 msaitoh #include <sys/kernel.h>
38 1.1 kiyohara #include <sys/kmem.h>
39 1.1 kiyohara #include <sys/mutex.h>
40 1.1 kiyohara #include <sys/sockio.h>
41 1.25 msaitoh #include <sys/sysctl.h>
42 1.1 kiyohara
43 1.1 kiyohara #include <dev/marvell/marvellreg.h>
44 1.1 kiyohara #include <dev/marvell/marvellvar.h>
45 1.1 kiyohara #include <dev/marvell/mvgbereg.h>
46 1.1 kiyohara
47 1.1 kiyohara #include <net/if.h>
48 1.1 kiyohara #include <net/if_ether.h>
49 1.1 kiyohara #include <net/if_media.h>
50 1.1 kiyohara
51 1.1 kiyohara #include <netinet/in.h>
52 1.1 kiyohara #include <netinet/in_systm.h>
53 1.1 kiyohara #include <netinet/ip.h>
54 1.1 kiyohara
55 1.1 kiyohara #include <net/bpf.h>
56 1.1 kiyohara #include <sys/rnd.h>
57 1.1 kiyohara
58 1.1 kiyohara #include <dev/mii/mii.h>
59 1.1 kiyohara #include <dev/mii/miivar.h>
60 1.1 kiyohara
61 1.1 kiyohara #include "locators.h"
62 1.1 kiyohara
63 1.1 kiyohara /* #define MVGBE_DEBUG 3 */
64 1.1 kiyohara #ifdef MVGBE_DEBUG
65 1.1 kiyohara #define DPRINTF(x) if (mvgbe_debug) printf x
66 1.1 kiyohara #define DPRINTFN(n,x) if (mvgbe_debug >= (n)) printf x
67 1.1 kiyohara int mvgbe_debug = MVGBE_DEBUG;
68 1.1 kiyohara #else
69 1.1 kiyohara #define DPRINTF(x)
70 1.1 kiyohara #define DPRINTFN(n,x)
71 1.1 kiyohara #endif
72 1.1 kiyohara
73 1.1 kiyohara
74 1.1 kiyohara #define MVGBE_READ(sc, reg) \
75 1.1 kiyohara bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
76 1.1 kiyohara #define MVGBE_WRITE(sc, reg, val) \
77 1.1 kiyohara bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
78 1.5 jakllsch #define MVGBE_READ_FILTER(sc, reg, val, c) \
79 1.5 jakllsch bus_space_read_region_4((sc)->sc_iot, (sc)->sc_dafh, (reg), (val), (c))
80 1.1 kiyohara #define MVGBE_WRITE_FILTER(sc, reg, val, c) \
81 1.5 jakllsch bus_space_write_region_4((sc)->sc_iot, (sc)->sc_dafh, (reg), (val), (c))
82 1.1 kiyohara
83 1.1 kiyohara #define MVGBE_TX_RING_CNT 256
84 1.4 jakllsch #define MVGBE_TX_RING_MSK (MVGBE_TX_RING_CNT - 1)
85 1.4 jakllsch #define MVGBE_TX_RING_NEXT(x) (((x) + 1) & MVGBE_TX_RING_MSK)
86 1.1 kiyohara #define MVGBE_RX_RING_CNT 256
87 1.4 jakllsch #define MVGBE_RX_RING_MSK (MVGBE_RX_RING_CNT - 1)
88 1.4 jakllsch #define MVGBE_RX_RING_NEXT(x) (((x) + 1) & MVGBE_RX_RING_MSK)
89 1.4 jakllsch
90 1.4 jakllsch CTASSERT(MVGBE_TX_RING_CNT > 1 && MVGBE_TX_RING_NEXT(MVGBE_TX_RING_CNT) ==
91 1.4 jakllsch (MVGBE_TX_RING_CNT + 1) % MVGBE_TX_RING_CNT);
92 1.4 jakllsch CTASSERT(MVGBE_RX_RING_CNT > 1 && MVGBE_RX_RING_NEXT(MVGBE_RX_RING_CNT) ==
93 1.4 jakllsch (MVGBE_RX_RING_CNT + 1) % MVGBE_RX_RING_CNT);
94 1.1 kiyohara
95 1.1 kiyohara #define MVGBE_JSLOTS 384 /* XXXX */
96 1.28 msaitoh #define MVGBE_JLEN \
97 1.31 msaitoh ((MVGBE_MRU + MVGBE_HWHEADER_SIZE + MVGBE_RXBUF_ALIGN - 1) & \
98 1.31 msaitoh ~MVGBE_RXBUF_MASK)
99 1.1 kiyohara #define MVGBE_NTXSEG 30
100 1.1 kiyohara #define MVGBE_JPAGESZ PAGE_SIZE
101 1.1 kiyohara #define MVGBE_RESID \
102 1.1 kiyohara (MVGBE_JPAGESZ - (MVGBE_JLEN * MVGBE_JSLOTS) % MVGBE_JPAGESZ)
103 1.1 kiyohara #define MVGBE_JMEM \
104 1.1 kiyohara ((MVGBE_JLEN * MVGBE_JSLOTS) + MVGBE_RESID)
105 1.1 kiyohara
106 1.1 kiyohara #define MVGBE_TX_RING_ADDR(sc, i) \
107 1.1 kiyohara ((sc)->sc_ring_map->dm_segs[0].ds_addr + \
108 1.1 kiyohara offsetof(struct mvgbe_ring_data, mvgbe_tx_ring[(i)]))
109 1.1 kiyohara
110 1.1 kiyohara #define MVGBE_RX_RING_ADDR(sc, i) \
111 1.1 kiyohara ((sc)->sc_ring_map->dm_segs[0].ds_addr + \
112 1.1 kiyohara offsetof(struct mvgbe_ring_data, mvgbe_rx_ring[(i)]))
113 1.1 kiyohara
114 1.1 kiyohara #define MVGBE_CDOFF(x) offsetof(struct mvgbe_ring_data, x)
115 1.1 kiyohara #define MVGBE_CDTXOFF(x) MVGBE_CDOFF(mvgbe_tx_ring[(x)])
116 1.1 kiyohara #define MVGBE_CDRXOFF(x) MVGBE_CDOFF(mvgbe_rx_ring[(x)])
117 1.1 kiyohara
118 1.1 kiyohara #define MVGBE_CDTXSYNC(sc, x, n, ops) \
119 1.1 kiyohara do { \
120 1.1 kiyohara int __x, __n; \
121 1.1 kiyohara const int __descsize = sizeof(struct mvgbe_tx_desc); \
122 1.1 kiyohara \
123 1.1 kiyohara __x = (x); \
124 1.1 kiyohara __n = (n); \
125 1.1 kiyohara \
126 1.1 kiyohara /* If it will wrap around, sync to the end of the ring. */ \
127 1.1 kiyohara if ((__x + __n) > MVGBE_TX_RING_CNT) { \
128 1.1 kiyohara bus_dmamap_sync((sc)->sc_dmat, \
129 1.1 kiyohara (sc)->sc_ring_map, MVGBE_CDTXOFF(__x), \
130 1.1 kiyohara __descsize * (MVGBE_TX_RING_CNT - __x), (ops)); \
131 1.1 kiyohara __n -= (MVGBE_TX_RING_CNT - __x); \
132 1.1 kiyohara __x = 0; \
133 1.1 kiyohara } \
134 1.1 kiyohara \
135 1.1 kiyohara /* Now sync whatever is left. */ \
136 1.1 kiyohara bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_ring_map, \
137 1.1 kiyohara MVGBE_CDTXOFF((__x)), __descsize * __n, (ops)); \
138 1.1 kiyohara } while (0 /*CONSTCOND*/)
139 1.1 kiyohara
140 1.1 kiyohara #define MVGBE_CDRXSYNC(sc, x, ops) \
141 1.1 kiyohara do { \
142 1.1 kiyohara bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_ring_map, \
143 1.1 kiyohara MVGBE_CDRXOFF((x)), sizeof(struct mvgbe_rx_desc), (ops)); \
144 1.1 kiyohara } while (/*CONSTCOND*/0)
145 1.1 kiyohara
146 1.25 msaitoh #define MVGBE_IPGINTTX_DEFAULT 768
147 1.25 msaitoh #define MVGBE_IPGINTRX_DEFAULT 768
148 1.1 kiyohara
149 1.32 msaitoh #ifdef MVGBE_EVENT_COUNTERS
150 1.32 msaitoh #define MVGBE_EVCNT_INCR(ev) (ev)->ev_count++
151 1.32 msaitoh #define MVGBE_EVCNT_ADD(ev, val) (ev)->ev_count += (val)
152 1.32 msaitoh #else
153 1.32 msaitoh #define MVGBE_EVCNT_INCR(ev) /* nothing */
154 1.32 msaitoh #define MVGBE_EVCNT_ADD(ev, val) /* nothing */
155 1.32 msaitoh #endif
156 1.32 msaitoh
157 1.1 kiyohara struct mvgbe_jpool_entry {
158 1.1 kiyohara int slot;
159 1.1 kiyohara LIST_ENTRY(mvgbe_jpool_entry) jpool_entries;
160 1.1 kiyohara };
161 1.1 kiyohara
162 1.1 kiyohara struct mvgbe_chain {
163 1.1 kiyohara void *mvgbe_desc;
164 1.1 kiyohara struct mbuf *mvgbe_mbuf;
165 1.1 kiyohara struct mvgbe_chain *mvgbe_next;
166 1.1 kiyohara };
167 1.1 kiyohara
168 1.1 kiyohara struct mvgbe_txmap_entry {
169 1.1 kiyohara bus_dmamap_t dmamap;
170 1.1 kiyohara SIMPLEQ_ENTRY(mvgbe_txmap_entry) link;
171 1.1 kiyohara };
172 1.1 kiyohara
173 1.1 kiyohara struct mvgbe_chain_data {
174 1.1 kiyohara struct mvgbe_chain mvgbe_tx_chain[MVGBE_TX_RING_CNT];
175 1.1 kiyohara struct mvgbe_txmap_entry *mvgbe_tx_map[MVGBE_TX_RING_CNT];
176 1.1 kiyohara int mvgbe_tx_prod;
177 1.1 kiyohara int mvgbe_tx_cons;
178 1.1 kiyohara int mvgbe_tx_cnt;
179 1.1 kiyohara
180 1.1 kiyohara struct mvgbe_chain mvgbe_rx_chain[MVGBE_RX_RING_CNT];
181 1.1 kiyohara bus_dmamap_t mvgbe_rx_map[MVGBE_RX_RING_CNT];
182 1.1 kiyohara bus_dmamap_t mvgbe_rx_jumbo_map;
183 1.1 kiyohara int mvgbe_rx_prod;
184 1.1 kiyohara int mvgbe_rx_cons;
185 1.1 kiyohara int mvgbe_rx_cnt;
186 1.1 kiyohara
187 1.1 kiyohara /* Stick the jumbo mem management stuff here too. */
188 1.1 kiyohara void *mvgbe_jslots[MVGBE_JSLOTS];
189 1.1 kiyohara void *mvgbe_jumbo_buf;
190 1.1 kiyohara };
191 1.1 kiyohara
192 1.1 kiyohara struct mvgbe_ring_data {
193 1.1 kiyohara struct mvgbe_tx_desc mvgbe_tx_ring[MVGBE_TX_RING_CNT];
194 1.1 kiyohara struct mvgbe_rx_desc mvgbe_rx_ring[MVGBE_RX_RING_CNT];
195 1.1 kiyohara };
196 1.1 kiyohara
197 1.1 kiyohara struct mvgbec_softc {
198 1.1 kiyohara device_t sc_dev;
199 1.1 kiyohara
200 1.1 kiyohara bus_space_tag_t sc_iot;
201 1.1 kiyohara bus_space_handle_t sc_ioh;
202 1.1 kiyohara
203 1.1 kiyohara kmutex_t sc_mtx;
204 1.3 kiyohara
205 1.13 rjs int sc_flags;
206 1.1 kiyohara };
207 1.1 kiyohara
208 1.1 kiyohara struct mvgbe_softc {
209 1.1 kiyohara device_t sc_dev;
210 1.3 kiyohara int sc_port;
211 1.1 kiyohara
212 1.1 kiyohara bus_space_tag_t sc_iot;
213 1.1 kiyohara bus_space_handle_t sc_ioh;
214 1.27 msaitoh bus_space_handle_t sc_dafh; /* dest address filter handle */
215 1.1 kiyohara bus_dma_tag_t sc_dmat;
216 1.1 kiyohara
217 1.1 kiyohara struct ethercom sc_ethercom;
218 1.1 kiyohara struct mii_data sc_mii;
219 1.1 kiyohara u_int8_t sc_enaddr[ETHER_ADDR_LEN]; /* station addr */
220 1.1 kiyohara
221 1.27 msaitoh callout_t sc_tick_ch; /* tick callout */
222 1.27 msaitoh
223 1.1 kiyohara struct mvgbe_chain_data sc_cdata;
224 1.1 kiyohara struct mvgbe_ring_data *sc_rdata;
225 1.1 kiyohara bus_dmamap_t sc_ring_map;
226 1.1 kiyohara int sc_if_flags;
227 1.25 msaitoh unsigned int sc_ipginttx;
228 1.25 msaitoh unsigned int sc_ipgintrx;
229 1.20 msaitoh int sc_wdogsoft;
230 1.1 kiyohara
231 1.1 kiyohara LIST_HEAD(__mvgbe_jfreehead, mvgbe_jpool_entry) sc_jfree_listhead;
232 1.1 kiyohara LIST_HEAD(__mvgbe_jinusehead, mvgbe_jpool_entry) sc_jinuse_listhead;
233 1.1 kiyohara SIMPLEQ_HEAD(__mvgbe_txmaphead, mvgbe_txmap_entry) sc_txmap_head;
234 1.1 kiyohara
235 1.15 tls krndsource_t sc_rnd_source;
236 1.25 msaitoh struct sysctllog *mvgbe_clog;
237 1.32 msaitoh #ifdef MVGBE_EVENT_COUNTERS
238 1.32 msaitoh struct evcnt sc_ev_rxoverrun;
239 1.32 msaitoh struct evcnt sc_ev_wdogsoft;
240 1.32 msaitoh #endif
241 1.1 kiyohara };
242 1.1 kiyohara
243 1.1 kiyohara
244 1.1 kiyohara /* Gigabit Ethernet Unit Global part functions */
245 1.1 kiyohara
246 1.1 kiyohara static int mvgbec_match(device_t, struct cfdata *, void *);
247 1.1 kiyohara static void mvgbec_attach(device_t, device_t, void *);
248 1.1 kiyohara
249 1.1 kiyohara static int mvgbec_print(void *, const char *);
250 1.1 kiyohara static int mvgbec_search(device_t, cfdata_t, const int *, void *);
251 1.1 kiyohara
252 1.1 kiyohara /* MII funcstions */
253 1.1 kiyohara static int mvgbec_miibus_readreg(device_t, int, int);
254 1.1 kiyohara static void mvgbec_miibus_writereg(device_t, int, int, int);
255 1.18 matt static void mvgbec_miibus_statchg(struct ifnet *);
256 1.1 kiyohara
257 1.1 kiyohara static void mvgbec_wininit(struct mvgbec_softc *);
258 1.1 kiyohara
259 1.1 kiyohara /* Gigabit Ethernet Port part functions */
260 1.1 kiyohara
261 1.1 kiyohara static int mvgbe_match(device_t, struct cfdata *, void *);
262 1.1 kiyohara static void mvgbe_attach(device_t, device_t, void *);
263 1.1 kiyohara
264 1.27 msaitoh static void mvgbe_tick(void *);
265 1.1 kiyohara static int mvgbe_intr(void *);
266 1.1 kiyohara
267 1.1 kiyohara static void mvgbe_start(struct ifnet *);
268 1.1 kiyohara static int mvgbe_ioctl(struct ifnet *, u_long, void *);
269 1.1 kiyohara static int mvgbe_init(struct ifnet *);
270 1.1 kiyohara static void mvgbe_stop(struct ifnet *, int);
271 1.1 kiyohara static void mvgbe_watchdog(struct ifnet *);
272 1.1 kiyohara
273 1.5 jakllsch static int mvgbe_ifflags_cb(struct ethercom *);
274 1.5 jakllsch
275 1.5 jakllsch static int mvgbe_mediachange(struct ifnet *);
276 1.5 jakllsch static void mvgbe_mediastatus(struct ifnet *, struct ifmediareq *);
277 1.1 kiyohara
278 1.1 kiyohara static int mvgbe_init_rx_ring(struct mvgbe_softc *);
279 1.1 kiyohara static int mvgbe_init_tx_ring(struct mvgbe_softc *);
280 1.1 kiyohara static int mvgbe_newbuf(struct mvgbe_softc *, int, struct mbuf *, bus_dmamap_t);
281 1.1 kiyohara static int mvgbe_alloc_jumbo_mem(struct mvgbe_softc *);
282 1.1 kiyohara static void *mvgbe_jalloc(struct mvgbe_softc *);
283 1.1 kiyohara static void mvgbe_jfree(struct mbuf *, void *, size_t, void *);
284 1.1 kiyohara static int mvgbe_encap(struct mvgbe_softc *, struct mbuf *, uint32_t *);
285 1.1 kiyohara static void mvgbe_rxeof(struct mvgbe_softc *);
286 1.1 kiyohara static void mvgbe_txeof(struct mvgbe_softc *);
287 1.5 jakllsch static uint8_t mvgbe_crc8(const uint8_t *, size_t);
288 1.5 jakllsch static void mvgbe_filter_setup(struct mvgbe_softc *);
289 1.1 kiyohara #ifdef MVGBE_DEBUG
290 1.1 kiyohara static void mvgbe_dump_txdesc(struct mvgbe_tx_desc *, int);
291 1.1 kiyohara #endif
292 1.28 msaitoh static int mvgbe_ipginttx(struct mvgbec_softc *, struct mvgbe_softc *,
293 1.28 msaitoh unsigned int);
294 1.28 msaitoh static int mvgbe_ipgintrx(struct mvgbec_softc *, struct mvgbe_softc *,
295 1.28 msaitoh unsigned int);
296 1.25 msaitoh static void sysctl_mvgbe_init(struct mvgbe_softc *);
297 1.25 msaitoh static int mvgbe_sysctl_ipginttx(SYSCTLFN_PROTO);
298 1.25 msaitoh static int mvgbe_sysctl_ipgintrx(SYSCTLFN_PROTO);
299 1.1 kiyohara
300 1.1 kiyohara CFATTACH_DECL_NEW(mvgbec_gt, sizeof(struct mvgbec_softc),
301 1.1 kiyohara mvgbec_match, mvgbec_attach, NULL, NULL);
302 1.1 kiyohara CFATTACH_DECL_NEW(mvgbec_mbus, sizeof(struct mvgbec_softc),
303 1.1 kiyohara mvgbec_match, mvgbec_attach, NULL, NULL);
304 1.1 kiyohara
305 1.1 kiyohara CFATTACH_DECL_NEW(mvgbe, sizeof(struct mvgbe_softc),
306 1.1 kiyohara mvgbe_match, mvgbe_attach, NULL, NULL);
307 1.1 kiyohara
308 1.6 christos device_t mvgbec0 = NULL;
309 1.25 msaitoh static int mvgbe_root_num;
310 1.1 kiyohara
311 1.1 kiyohara struct mvgbe_port {
312 1.1 kiyohara int model;
313 1.3 kiyohara int unit;
314 1.1 kiyohara int ports;
315 1.3 kiyohara int irqs[3];
316 1.3 kiyohara int flags;
317 1.3 kiyohara #define FLAGS_FIX_TQTB (1 << 0)
318 1.13 rjs #define FLAGS_FIX_MTU (1 << 1)
319 1.24 msaitoh #define FLAGS_IPG1 (1 << 2)
320 1.24 msaitoh #define FLAGS_IPG2 (1 << 3)
321 1.1 kiyohara } mvgbe_ports[] = {
322 1.3 kiyohara { MARVELL_DISCOVERY_II, 0, 3, { 32, 33, 34 }, 0 },
323 1.3 kiyohara { MARVELL_DISCOVERY_III, 0, 3, { 32, 33, 34 }, 0 },
324 1.1 kiyohara #if 0
325 1.3 kiyohara { MARVELL_DISCOVERY_LT, 0, ?, { }, 0 },
326 1.3 kiyohara { MARVELL_DISCOVERY_V, 0, ?, { }, 0 },
327 1.3 kiyohara { MARVELL_DISCOVERY_VI, 0, ?, { }, 0 },
328 1.1 kiyohara #endif
329 1.13 rjs { MARVELL_ORION_1_88F5082, 0, 1, { 21 }, FLAGS_FIX_MTU },
330 1.13 rjs { MARVELL_ORION_1_88F5180N, 0, 1, { 21 }, FLAGS_FIX_MTU },
331 1.24 msaitoh { MARVELL_ORION_1_88F5181, 0, 1, { 21 }, FLAGS_FIX_MTU | FLAGS_IPG1 },
332 1.24 msaitoh { MARVELL_ORION_1_88F5182, 0, 1, { 21 }, FLAGS_FIX_MTU | FLAGS_IPG1 },
333 1.24 msaitoh { MARVELL_ORION_2_88F5281, 0, 1, { 21 }, FLAGS_FIX_MTU | FLAGS_IPG1 },
334 1.13 rjs { MARVELL_ORION_1_88F6082, 0, 1, { 21 }, FLAGS_FIX_MTU },
335 1.13 rjs { MARVELL_ORION_1_88W8660, 0, 1, { 21 }, FLAGS_FIX_MTU },
336 1.3 kiyohara
337 1.24 msaitoh { MARVELL_KIRKWOOD_88F6180, 0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
338 1.24 msaitoh { MARVELL_KIRKWOOD_88F6192, 0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
339 1.24 msaitoh { MARVELL_KIRKWOOD_88F6192, 1, 1, { 15 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
340 1.24 msaitoh { MARVELL_KIRKWOOD_88F6281, 0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
341 1.24 msaitoh { MARVELL_KIRKWOOD_88F6281, 1, 1, { 15 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
342 1.24 msaitoh { MARVELL_KIRKWOOD_88F6282, 0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
343 1.24 msaitoh { MARVELL_KIRKWOOD_88F6282, 1, 1, { 15 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
344 1.24 msaitoh
345 1.24 msaitoh { MARVELL_MV78XX0_MV78100, 0, 1, { 40 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
346 1.24 msaitoh { MARVELL_MV78XX0_MV78100, 1, 1, { 44 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
347 1.24 msaitoh { MARVELL_MV78XX0_MV78200, 0, 1, { 40 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
348 1.24 msaitoh { MARVELL_MV78XX0_MV78200, 1, 1, { 44 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
349 1.24 msaitoh { MARVELL_MV78XX0_MV78200, 2, 1, { 48 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
350 1.24 msaitoh { MARVELL_MV78XX0_MV78200, 3, 1, { 52 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
351 1.1 kiyohara };
352 1.1 kiyohara
353 1.1 kiyohara
354 1.1 kiyohara /* ARGSUSED */
355 1.1 kiyohara static int
356 1.9 matt mvgbec_match(device_t parent, cfdata_t match, void *aux)
357 1.1 kiyohara {
358 1.1 kiyohara struct marvell_attach_args *mva = aux;
359 1.1 kiyohara int i;
360 1.1 kiyohara
361 1.1 kiyohara if (strcmp(mva->mva_name, match->cf_name) != 0)
362 1.1 kiyohara return 0;
363 1.2 kiyohara if (mva->mva_offset == MVA_OFFSET_DEFAULT)
364 1.1 kiyohara return 0;
365 1.1 kiyohara
366 1.1 kiyohara for (i = 0; i < __arraycount(mvgbe_ports); i++)
367 1.1 kiyohara if (mva->mva_model == mvgbe_ports[i].model) {
368 1.1 kiyohara mva->mva_size = MVGBE_SIZE;
369 1.1 kiyohara return 1;
370 1.1 kiyohara }
371 1.1 kiyohara return 0;
372 1.1 kiyohara }
373 1.1 kiyohara
374 1.1 kiyohara /* ARGSUSED */
375 1.1 kiyohara static void
376 1.1 kiyohara mvgbec_attach(device_t parent, device_t self, void *aux)
377 1.1 kiyohara {
378 1.28 msaitoh struct mvgbec_softc *csc = device_private(self);
379 1.1 kiyohara struct marvell_attach_args *mva = aux, gbea;
380 1.1 kiyohara struct mvgbe_softc *port;
381 1.1 kiyohara struct mii_softc *mii;
382 1.1 kiyohara device_t child;
383 1.1 kiyohara uint32_t phyaddr;
384 1.1 kiyohara int i, j;
385 1.1 kiyohara
386 1.1 kiyohara aprint_naive("\n");
387 1.1 kiyohara aprint_normal(": Marvell Gigabit Ethernet Controller\n");
388 1.1 kiyohara
389 1.28 msaitoh csc->sc_dev = self;
390 1.28 msaitoh csc->sc_iot = mva->mva_iot;
391 1.1 kiyohara if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
392 1.28 msaitoh mva->mva_size, &csc->sc_ioh)) {
393 1.1 kiyohara aprint_error_dev(self, "Cannot map registers\n");
394 1.1 kiyohara return;
395 1.1 kiyohara }
396 1.6 christos
397 1.7 christos if (mvgbec0 == NULL)
398 1.6 christos mvgbec0 = self;
399 1.17 kiyohara
400 1.1 kiyohara phyaddr = 0;
401 1.28 msaitoh MVGBE_WRITE(csc, MVGBE_PHYADDR, phyaddr);
402 1.1 kiyohara
403 1.28 msaitoh mutex_init(&csc->sc_mtx, MUTEX_DEFAULT, IPL_NET);
404 1.1 kiyohara
405 1.1 kiyohara /* Disable and clear Gigabit Ethernet Unit interrupts */
406 1.28 msaitoh MVGBE_WRITE(csc, MVGBE_EUIM, 0);
407 1.28 msaitoh MVGBE_WRITE(csc, MVGBE_EUIC, 0);
408 1.1 kiyohara
409 1.28 msaitoh mvgbec_wininit(csc);
410 1.1 kiyohara
411 1.1 kiyohara memset(&gbea, 0, sizeof(gbea));
412 1.1 kiyohara for (i = 0; i < __arraycount(mvgbe_ports); i++) {
413 1.3 kiyohara if (mvgbe_ports[i].model != mva->mva_model ||
414 1.3 kiyohara mvgbe_ports[i].unit != mva->mva_unit)
415 1.1 kiyohara continue;
416 1.1 kiyohara
417 1.28 msaitoh csc->sc_flags = mvgbe_ports[i].flags;
418 1.3 kiyohara
419 1.1 kiyohara for (j = 0; j < mvgbe_ports[i].ports; j++) {
420 1.1 kiyohara gbea.mva_name = "mvgbe";
421 1.1 kiyohara gbea.mva_model = mva->mva_model;
422 1.28 msaitoh gbea.mva_iot = csc->sc_iot;
423 1.28 msaitoh gbea.mva_ioh = csc->sc_ioh;
424 1.1 kiyohara gbea.mva_unit = j;
425 1.1 kiyohara gbea.mva_dmat = mva->mva_dmat;
426 1.1 kiyohara gbea.mva_irq = mvgbe_ports[i].irqs[j];
427 1.28 msaitoh child = config_found_sm_loc(csc->sc_dev, "mvgbec", NULL,
428 1.1 kiyohara &gbea, mvgbec_print, mvgbec_search);
429 1.1 kiyohara if (child) {
430 1.1 kiyohara port = device_private(child);
431 1.1 kiyohara mii = LIST_FIRST(&port->sc_mii.mii_phys);
432 1.1 kiyohara phyaddr |= MVGBE_PHYADDR_PHYAD(j, mii->mii_phy);
433 1.1 kiyohara }
434 1.1 kiyohara }
435 1.3 kiyohara break;
436 1.1 kiyohara }
437 1.28 msaitoh MVGBE_WRITE(csc, MVGBE_PHYADDR, phyaddr);
438 1.1 kiyohara }
439 1.1 kiyohara
440 1.1 kiyohara static int
441 1.1 kiyohara mvgbec_print(void *aux, const char *pnp)
442 1.1 kiyohara {
443 1.1 kiyohara struct marvell_attach_args *gbea = aux;
444 1.1 kiyohara
445 1.1 kiyohara if (pnp)
446 1.1 kiyohara aprint_normal("%s at %s port %d",
447 1.1 kiyohara gbea->mva_name, pnp, gbea->mva_unit);
448 1.1 kiyohara else {
449 1.1 kiyohara if (gbea->mva_unit != MVGBECCF_PORT_DEFAULT)
450 1.1 kiyohara aprint_normal(" port %d", gbea->mva_unit);
451 1.1 kiyohara if (gbea->mva_irq != MVGBECCF_IRQ_DEFAULT)
452 1.1 kiyohara aprint_normal(" irq %d", gbea->mva_irq);
453 1.1 kiyohara }
454 1.1 kiyohara return UNCONF;
455 1.1 kiyohara }
456 1.1 kiyohara
457 1.1 kiyohara /* ARGSUSED */
458 1.1 kiyohara static int
459 1.1 kiyohara mvgbec_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
460 1.1 kiyohara {
461 1.1 kiyohara struct marvell_attach_args *gbea = aux;
462 1.1 kiyohara
463 1.1 kiyohara if (cf->cf_loc[MVGBECCF_PORT] == gbea->mva_unit &&
464 1.1 kiyohara cf->cf_loc[MVGBECCF_IRQ] != MVGBECCF_IRQ_DEFAULT)
465 1.1 kiyohara gbea->mva_irq = cf->cf_loc[MVGBECCF_IRQ];
466 1.1 kiyohara
467 1.1 kiyohara return config_match(parent, cf, aux);
468 1.1 kiyohara }
469 1.1 kiyohara
470 1.1 kiyohara static int
471 1.1 kiyohara mvgbec_miibus_readreg(device_t dev, int phy, int reg)
472 1.1 kiyohara {
473 1.1 kiyohara struct mvgbe_softc *sc = device_private(dev);
474 1.6 christos struct mvgbec_softc *csc;
475 1.1 kiyohara struct ifnet *ifp = &sc->sc_ethercom.ec_if;
476 1.1 kiyohara uint32_t smi, val;
477 1.1 kiyohara int i;
478 1.1 kiyohara
479 1.6 christos if (mvgbec0 == NULL) {
480 1.6 christos aprint_error_ifnet(ifp, "SMI mvgbec0 not found\n");
481 1.6 christos return -1;
482 1.6 christos }
483 1.6 christos csc = device_private(mvgbec0);
484 1.6 christos
485 1.1 kiyohara mutex_enter(&csc->sc_mtx);
486 1.1 kiyohara
487 1.1 kiyohara for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
488 1.1 kiyohara DELAY(1);
489 1.1 kiyohara if (!(MVGBE_READ(csc, MVGBE_SMI) & MVGBE_SMI_BUSY))
490 1.1 kiyohara break;
491 1.1 kiyohara }
492 1.1 kiyohara if (i == MVGBE_PHY_TIMEOUT) {
493 1.1 kiyohara aprint_error_ifnet(ifp, "SMI busy timeout\n");
494 1.1 kiyohara mutex_exit(&csc->sc_mtx);
495 1.1 kiyohara return -1;
496 1.1 kiyohara }
497 1.1 kiyohara
498 1.1 kiyohara smi =
499 1.1 kiyohara MVGBE_SMI_PHYAD(phy) | MVGBE_SMI_REGAD(reg) | MVGBE_SMI_OPCODE_READ;
500 1.1 kiyohara MVGBE_WRITE(csc, MVGBE_SMI, smi);
501 1.1 kiyohara
502 1.1 kiyohara for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
503 1.1 kiyohara DELAY(1);
504 1.1 kiyohara smi = MVGBE_READ(csc, MVGBE_SMI);
505 1.1 kiyohara if (smi & MVGBE_SMI_READVALID)
506 1.1 kiyohara break;
507 1.1 kiyohara }
508 1.1 kiyohara
509 1.1 kiyohara mutex_exit(&csc->sc_mtx);
510 1.1 kiyohara
511 1.1 kiyohara DPRINTFN(9, ("mvgbec_miibus_readreg: i=%d, timeout=%d\n",
512 1.1 kiyohara i, MVGBE_PHY_TIMEOUT));
513 1.1 kiyohara
514 1.1 kiyohara val = smi & MVGBE_SMI_DATA_MASK;
515 1.1 kiyohara
516 1.1 kiyohara DPRINTFN(9, ("mvgbec_miibus_readreg phy=%d, reg=%#x, val=%#x\n",
517 1.1 kiyohara phy, reg, val));
518 1.1 kiyohara
519 1.1 kiyohara return val;
520 1.1 kiyohara }
521 1.1 kiyohara
522 1.1 kiyohara static void
523 1.1 kiyohara mvgbec_miibus_writereg(device_t dev, int phy, int reg, int val)
524 1.1 kiyohara {
525 1.1 kiyohara struct mvgbe_softc *sc = device_private(dev);
526 1.6 christos struct mvgbec_softc *csc;
527 1.1 kiyohara struct ifnet *ifp = &sc->sc_ethercom.ec_if;
528 1.1 kiyohara uint32_t smi;
529 1.1 kiyohara int i;
530 1.1 kiyohara
531 1.6 christos if (mvgbec0 == NULL) {
532 1.6 christos aprint_error_ifnet(ifp, "SMI mvgbec0 not found\n");
533 1.6 christos return;
534 1.6 christos }
535 1.6 christos csc = device_private(mvgbec0);
536 1.6 christos
537 1.1 kiyohara DPRINTFN(9, ("mvgbec_miibus_writereg phy=%d reg=%#x val=%#x\n",
538 1.1 kiyohara phy, reg, val));
539 1.1 kiyohara
540 1.1 kiyohara mutex_enter(&csc->sc_mtx);
541 1.1 kiyohara
542 1.1 kiyohara for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
543 1.1 kiyohara DELAY(1);
544 1.1 kiyohara if (!(MVGBE_READ(csc, MVGBE_SMI) & MVGBE_SMI_BUSY))
545 1.1 kiyohara break;
546 1.1 kiyohara }
547 1.1 kiyohara if (i == MVGBE_PHY_TIMEOUT) {
548 1.1 kiyohara aprint_error_ifnet(ifp, "SMI busy timeout\n");
549 1.1 kiyohara mutex_exit(&csc->sc_mtx);
550 1.1 kiyohara return;
551 1.1 kiyohara }
552 1.1 kiyohara
553 1.1 kiyohara smi = MVGBE_SMI_PHYAD(phy) | MVGBE_SMI_REGAD(reg) |
554 1.1 kiyohara MVGBE_SMI_OPCODE_WRITE | (val & MVGBE_SMI_DATA_MASK);
555 1.1 kiyohara MVGBE_WRITE(csc, MVGBE_SMI, smi);
556 1.1 kiyohara
557 1.1 kiyohara for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
558 1.1 kiyohara DELAY(1);
559 1.1 kiyohara if (!(MVGBE_READ(csc, MVGBE_SMI) & MVGBE_SMI_BUSY))
560 1.1 kiyohara break;
561 1.1 kiyohara }
562 1.1 kiyohara
563 1.1 kiyohara mutex_exit(&csc->sc_mtx);
564 1.1 kiyohara
565 1.1 kiyohara if (i == MVGBE_PHY_TIMEOUT)
566 1.1 kiyohara aprint_error_ifnet(ifp, "phy write timed out\n");
567 1.1 kiyohara }
568 1.1 kiyohara
569 1.1 kiyohara static void
570 1.18 matt mvgbec_miibus_statchg(struct ifnet *ifp)
571 1.1 kiyohara {
572 1.1 kiyohara
573 1.1 kiyohara /* nothing to do */
574 1.1 kiyohara }
575 1.1 kiyohara
576 1.1 kiyohara
577 1.1 kiyohara static void
578 1.1 kiyohara mvgbec_wininit(struct mvgbec_softc *sc)
579 1.1 kiyohara {
580 1.1 kiyohara device_t pdev = device_parent(sc->sc_dev);
581 1.1 kiyohara uint64_t base;
582 1.1 kiyohara uint32_t en, ac, size;
583 1.1 kiyohara int window, target, attr, rv, i;
584 1.1 kiyohara static int tags[] = {
585 1.1 kiyohara MARVELL_TAG_SDRAM_CS0,
586 1.1 kiyohara MARVELL_TAG_SDRAM_CS1,
587 1.1 kiyohara MARVELL_TAG_SDRAM_CS2,
588 1.1 kiyohara MARVELL_TAG_SDRAM_CS3,
589 1.1 kiyohara
590 1.1 kiyohara MARVELL_TAG_UNDEFINED,
591 1.1 kiyohara };
592 1.1 kiyohara
593 1.1 kiyohara /* First disable all address decode windows */
594 1.1 kiyohara en = MVGBE_BARE_EN_MASK;
595 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_BARE, en);
596 1.1 kiyohara
597 1.1 kiyohara ac = 0;
598 1.1 kiyohara for (window = 0, i = 0;
599 1.1 kiyohara tags[i] != MARVELL_TAG_UNDEFINED && window < MVGBE_NWINDOW; i++) {
600 1.1 kiyohara rv = marvell_winparams_by_tag(pdev, tags[i],
601 1.1 kiyohara &target, &attr, &base, &size);
602 1.1 kiyohara if (rv != 0 || size == 0)
603 1.1 kiyohara continue;
604 1.1 kiyohara
605 1.1 kiyohara if (base > 0xffffffffULL) {
606 1.1 kiyohara if (window >= MVGBE_NREMAP) {
607 1.1 kiyohara aprint_error_dev(sc->sc_dev,
608 1.1 kiyohara "can't remap window %d\n", window);
609 1.1 kiyohara continue;
610 1.1 kiyohara }
611 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_HA(window),
612 1.1 kiyohara (base >> 32) & 0xffffffff);
613 1.1 kiyohara }
614 1.1 kiyohara
615 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_BASEADDR(window),
616 1.1 kiyohara MVGBE_BASEADDR_TARGET(target) |
617 1.1 kiyohara MVGBE_BASEADDR_ATTR(attr) |
618 1.1 kiyohara MVGBE_BASEADDR_BASE(base));
619 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_S(window), MVGBE_S_SIZE(size));
620 1.1 kiyohara
621 1.1 kiyohara en &= ~(1 << window);
622 1.1 kiyohara /* set full access (r/w) */
623 1.1 kiyohara ac |= MVGBE_EPAP_EPAR(window, MVGBE_EPAP_AC_FA);
624 1.1 kiyohara window++;
625 1.1 kiyohara }
626 1.1 kiyohara /* allow to access decode window */
627 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_EPAP, ac);
628 1.1 kiyohara
629 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_BARE, en);
630 1.1 kiyohara }
631 1.1 kiyohara
632 1.1 kiyohara
633 1.1 kiyohara /* ARGSUSED */
634 1.1 kiyohara static int
635 1.9 matt mvgbe_match(device_t parent, cfdata_t match, void *aux)
636 1.1 kiyohara {
637 1.1 kiyohara struct marvell_attach_args *mva = aux;
638 1.1 kiyohara uint32_t pbase, maddrh, maddrl;
639 1.1 kiyohara
640 1.1 kiyohara pbase = MVGBE_PORTR_BASE + mva->mva_unit * MVGBE_PORTR_SIZE;
641 1.1 kiyohara maddrh =
642 1.1 kiyohara bus_space_read_4(mva->mva_iot, mva->mva_ioh, pbase + MVGBE_MACAH);
643 1.1 kiyohara maddrl =
644 1.1 kiyohara bus_space_read_4(mva->mva_iot, mva->mva_ioh, pbase + MVGBE_MACAL);
645 1.1 kiyohara if ((maddrh | maddrl) == 0)
646 1.1 kiyohara return 0;
647 1.1 kiyohara
648 1.1 kiyohara return 1;
649 1.1 kiyohara }
650 1.1 kiyohara
651 1.1 kiyohara /* ARGSUSED */
652 1.1 kiyohara static void
653 1.1 kiyohara mvgbe_attach(device_t parent, device_t self, void *aux)
654 1.1 kiyohara {
655 1.1 kiyohara struct mvgbe_softc *sc = device_private(self);
656 1.1 kiyohara struct marvell_attach_args *mva = aux;
657 1.1 kiyohara struct mvgbe_txmap_entry *entry;
658 1.1 kiyohara struct ifnet *ifp;
659 1.1 kiyohara bus_dma_segment_t seg;
660 1.1 kiyohara bus_dmamap_t dmamap;
661 1.1 kiyohara int rseg, i;
662 1.1 kiyohara uint32_t maddrh, maddrl;
663 1.1 kiyohara void *kva;
664 1.1 kiyohara
665 1.1 kiyohara aprint_naive("\n");
666 1.1 kiyohara aprint_normal("\n");
667 1.1 kiyohara
668 1.1 kiyohara sc->sc_dev = self;
669 1.1 kiyohara sc->sc_port = mva->mva_unit;
670 1.1 kiyohara sc->sc_iot = mva->mva_iot;
671 1.27 msaitoh callout_init(&sc->sc_tick_ch, 0);
672 1.27 msaitoh callout_setfunc(&sc->sc_tick_ch, mvgbe_tick, sc);
673 1.1 kiyohara if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
674 1.1 kiyohara MVGBE_PORTR_BASE + mva->mva_unit * MVGBE_PORTR_SIZE,
675 1.1 kiyohara MVGBE_PORTR_SIZE, &sc->sc_ioh)) {
676 1.1 kiyohara aprint_error_dev(self, "Cannot map registers\n");
677 1.1 kiyohara return;
678 1.1 kiyohara }
679 1.1 kiyohara if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
680 1.1 kiyohara MVGBE_PORTDAFR_BASE + mva->mva_unit * MVGBE_PORTDAFR_SIZE,
681 1.1 kiyohara MVGBE_PORTDAFR_SIZE, &sc->sc_dafh)) {
682 1.1 kiyohara aprint_error_dev(self,
683 1.1 kiyohara "Cannot map destination address filter registers\n");
684 1.1 kiyohara return;
685 1.1 kiyohara }
686 1.1 kiyohara sc->sc_dmat = mva->mva_dmat;
687 1.1 kiyohara
688 1.1 kiyohara maddrh = MVGBE_READ(sc, MVGBE_MACAH);
689 1.1 kiyohara maddrl = MVGBE_READ(sc, MVGBE_MACAL);
690 1.1 kiyohara sc->sc_enaddr[0] = maddrh >> 24;
691 1.1 kiyohara sc->sc_enaddr[1] = maddrh >> 16;
692 1.1 kiyohara sc->sc_enaddr[2] = maddrh >> 8;
693 1.1 kiyohara sc->sc_enaddr[3] = maddrh >> 0;
694 1.1 kiyohara sc->sc_enaddr[4] = maddrl >> 8;
695 1.1 kiyohara sc->sc_enaddr[5] = maddrl >> 0;
696 1.1 kiyohara aprint_normal_dev(self, "Ethernet address %s\n",
697 1.1 kiyohara ether_sprintf(sc->sc_enaddr));
698 1.1 kiyohara
699 1.1 kiyohara /* clear all ethernet port interrupts */
700 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_IC, 0);
701 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_ICE, 0);
702 1.1 kiyohara
703 1.1 kiyohara marvell_intr_establish(mva->mva_irq, IPL_NET, mvgbe_intr, sc);
704 1.1 kiyohara
705 1.1 kiyohara /* Allocate the descriptor queues. */
706 1.1 kiyohara if (bus_dmamem_alloc(sc->sc_dmat, sizeof(struct mvgbe_ring_data),
707 1.1 kiyohara PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
708 1.1 kiyohara aprint_error_dev(self, "can't alloc rx buffers\n");
709 1.1 kiyohara return;
710 1.1 kiyohara }
711 1.1 kiyohara if (bus_dmamem_map(sc->sc_dmat, &seg, rseg,
712 1.1 kiyohara sizeof(struct mvgbe_ring_data), &kva, BUS_DMA_NOWAIT)) {
713 1.1 kiyohara aprint_error_dev(self, "can't map dma buffers (%lu bytes)\n",
714 1.1 kiyohara (u_long)sizeof(struct mvgbe_ring_data));
715 1.1 kiyohara goto fail1;
716 1.1 kiyohara }
717 1.1 kiyohara if (bus_dmamap_create(sc->sc_dmat, sizeof(struct mvgbe_ring_data), 1,
718 1.1 kiyohara sizeof(struct mvgbe_ring_data), 0, BUS_DMA_NOWAIT,
719 1.1 kiyohara &sc->sc_ring_map)) {
720 1.1 kiyohara aprint_error_dev(self, "can't create dma map\n");
721 1.1 kiyohara goto fail2;
722 1.1 kiyohara }
723 1.1 kiyohara if (bus_dmamap_load(sc->sc_dmat, sc->sc_ring_map, kva,
724 1.1 kiyohara sizeof(struct mvgbe_ring_data), NULL, BUS_DMA_NOWAIT)) {
725 1.1 kiyohara aprint_error_dev(self, "can't load dma map\n");
726 1.1 kiyohara goto fail3;
727 1.1 kiyohara }
728 1.1 kiyohara for (i = 0; i < MVGBE_RX_RING_CNT; i++)
729 1.1 kiyohara sc->sc_cdata.mvgbe_rx_chain[i].mvgbe_mbuf = NULL;
730 1.1 kiyohara
731 1.1 kiyohara SIMPLEQ_INIT(&sc->sc_txmap_head);
732 1.1 kiyohara for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
733 1.1 kiyohara sc->sc_cdata.mvgbe_tx_chain[i].mvgbe_mbuf = NULL;
734 1.1 kiyohara
735 1.1 kiyohara if (bus_dmamap_create(sc->sc_dmat,
736 1.1 kiyohara MVGBE_JLEN, MVGBE_NTXSEG, MVGBE_JLEN, 0,
737 1.1 kiyohara BUS_DMA_NOWAIT, &dmamap)) {
738 1.1 kiyohara aprint_error_dev(self, "Can't create TX dmamap\n");
739 1.1 kiyohara goto fail4;
740 1.1 kiyohara }
741 1.1 kiyohara
742 1.1 kiyohara entry = kmem_alloc(sizeof(*entry), KM_SLEEP);
743 1.1 kiyohara if (!entry) {
744 1.1 kiyohara aprint_error_dev(self, "Can't alloc txmap entry\n");
745 1.1 kiyohara bus_dmamap_destroy(sc->sc_dmat, dmamap);
746 1.1 kiyohara goto fail4;
747 1.1 kiyohara }
748 1.1 kiyohara entry->dmamap = dmamap;
749 1.1 kiyohara SIMPLEQ_INSERT_HEAD(&sc->sc_txmap_head, entry, link);
750 1.1 kiyohara }
751 1.1 kiyohara
752 1.1 kiyohara sc->sc_rdata = (struct mvgbe_ring_data *)kva;
753 1.1 kiyohara memset(sc->sc_rdata, 0, sizeof(struct mvgbe_ring_data));
754 1.1 kiyohara
755 1.1 kiyohara /*
756 1.1 kiyohara * We can support 802.1Q VLAN-sized frames and jumbo
757 1.1 kiyohara * Ethernet frames.
758 1.1 kiyohara */
759 1.1 kiyohara sc->sc_ethercom.ec_capabilities |=
760 1.5 jakllsch ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
761 1.1 kiyohara
762 1.1 kiyohara /* Try to allocate memory for jumbo buffers. */
763 1.1 kiyohara if (mvgbe_alloc_jumbo_mem(sc)) {
764 1.1 kiyohara aprint_error_dev(self, "jumbo buffer allocation failed\n");
765 1.1 kiyohara goto fail4;
766 1.1 kiyohara }
767 1.1 kiyohara
768 1.1 kiyohara ifp = &sc->sc_ethercom.ec_if;
769 1.1 kiyohara ifp->if_softc = sc;
770 1.1 kiyohara ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
771 1.1 kiyohara ifp->if_start = mvgbe_start;
772 1.1 kiyohara ifp->if_ioctl = mvgbe_ioctl;
773 1.1 kiyohara ifp->if_init = mvgbe_init;
774 1.1 kiyohara ifp->if_stop = mvgbe_stop;
775 1.1 kiyohara ifp->if_watchdog = mvgbe_watchdog;
776 1.1 kiyohara /*
777 1.1 kiyohara * We can do IPv4/TCPv4/UDPv4 checksums in hardware.
778 1.1 kiyohara */
779 1.1 kiyohara sc->sc_ethercom.ec_if.if_capabilities |=
780 1.1 kiyohara IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
781 1.1 kiyohara IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
782 1.1 kiyohara IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
783 1.5 jakllsch /*
784 1.5 jakllsch * But, IPv6 packets in the stream can cause incorrect TCPv4 Tx sums.
785 1.5 jakllsch */
786 1.5 jakllsch sc->sc_ethercom.ec_if.if_capabilities &= ~IFCAP_CSUM_TCPv4_Tx;
787 1.1 kiyohara IFQ_SET_MAXLEN(&ifp->if_snd, max(MVGBE_TX_RING_CNT - 1, IFQ_MAXLEN));
788 1.1 kiyohara IFQ_SET_READY(&ifp->if_snd);
789 1.1 kiyohara strcpy(ifp->if_xname, device_xname(sc->sc_dev));
790 1.1 kiyohara
791 1.1 kiyohara mvgbe_stop(ifp, 0);
792 1.1 kiyohara
793 1.1 kiyohara /*
794 1.1 kiyohara * Do MII setup.
795 1.1 kiyohara */
796 1.1 kiyohara sc->sc_mii.mii_ifp = ifp;
797 1.1 kiyohara sc->sc_mii.mii_readreg = mvgbec_miibus_readreg;
798 1.1 kiyohara sc->sc_mii.mii_writereg = mvgbec_miibus_writereg;
799 1.1 kiyohara sc->sc_mii.mii_statchg = mvgbec_miibus_statchg;
800 1.1 kiyohara
801 1.1 kiyohara sc->sc_ethercom.ec_mii = &sc->sc_mii;
802 1.1 kiyohara ifmedia_init(&sc->sc_mii.mii_media, 0,
803 1.5 jakllsch mvgbe_mediachange, mvgbe_mediastatus);
804 1.1 kiyohara mii_attach(self, &sc->sc_mii, 0xffffffff,
805 1.8 christos MII_PHY_ANY, parent == mvgbec0 ? 0 : 1, 0);
806 1.1 kiyohara if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
807 1.1 kiyohara aprint_error_dev(self, "no PHY found!\n");
808 1.1 kiyohara ifmedia_add(&sc->sc_mii.mii_media,
809 1.1 kiyohara IFM_ETHER|IFM_MANUAL, 0, NULL);
810 1.1 kiyohara ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
811 1.1 kiyohara } else
812 1.1 kiyohara ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
813 1.1 kiyohara
814 1.1 kiyohara /*
815 1.1 kiyohara * Call MI attach routines.
816 1.1 kiyohara */
817 1.1 kiyohara if_attach(ifp);
818 1.1 kiyohara
819 1.1 kiyohara ether_ifattach(ifp, sc->sc_enaddr);
820 1.5 jakllsch ether_set_ifflags_cb(&sc->sc_ethercom, mvgbe_ifflags_cb);
821 1.1 kiyohara
822 1.25 msaitoh sysctl_mvgbe_init(sc);
823 1.32 msaitoh #ifdef MVGBE_EVENT_COUNTERS
824 1.32 msaitoh /* Attach event counters. */
825 1.32 msaitoh evcnt_attach_dynamic(&sc->sc_ev_rxoverrun, EVCNT_TYPE_MISC,
826 1.32 msaitoh NULL, device_xname(sc->sc_dev), "rxoverrrun");
827 1.32 msaitoh evcnt_attach_dynamic(&sc->sc_ev_wdogsoft, EVCNT_TYPE_MISC,
828 1.32 msaitoh NULL, device_xname(sc->sc_dev), "wdogsoft");
829 1.32 msaitoh #endif
830 1.1 kiyohara rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
831 1.1 kiyohara RND_TYPE_NET, 0);
832 1.1 kiyohara
833 1.1 kiyohara return;
834 1.1 kiyohara
835 1.1 kiyohara fail4:
836 1.1 kiyohara while ((entry = SIMPLEQ_FIRST(&sc->sc_txmap_head)) != NULL) {
837 1.1 kiyohara SIMPLEQ_REMOVE_HEAD(&sc->sc_txmap_head, link);
838 1.1 kiyohara bus_dmamap_destroy(sc->sc_dmat, entry->dmamap);
839 1.1 kiyohara }
840 1.1 kiyohara bus_dmamap_unload(sc->sc_dmat, sc->sc_ring_map);
841 1.1 kiyohara fail3:
842 1.1 kiyohara bus_dmamap_destroy(sc->sc_dmat, sc->sc_ring_map);
843 1.1 kiyohara fail2:
844 1.1 kiyohara bus_dmamem_unmap(sc->sc_dmat, kva, sizeof(struct mvgbe_ring_data));
845 1.1 kiyohara fail1:
846 1.1 kiyohara bus_dmamem_free(sc->sc_dmat, &seg, rseg);
847 1.1 kiyohara return;
848 1.1 kiyohara }
849 1.1 kiyohara
850 1.25 msaitoh static int
851 1.25 msaitoh mvgbe_ipginttx(struct mvgbec_softc *csc, struct mvgbe_softc *sc,
852 1.25 msaitoh unsigned int ipginttx)
853 1.25 msaitoh {
854 1.25 msaitoh uint32_t reg;
855 1.25 msaitoh reg = MVGBE_READ(sc, MVGBE_PTFUT);
856 1.25 msaitoh
857 1.25 msaitoh if (csc->sc_flags & FLAGS_IPG2) {
858 1.25 msaitoh if (ipginttx > MVGBE_PTFUT_IPGINTTX_V2_MAX)
859 1.25 msaitoh return -1;
860 1.25 msaitoh reg &= ~MVGBE_PTFUT_IPGINTTX_V2_MASK;
861 1.25 msaitoh reg |= MVGBE_PTFUT_IPGINTTX_V2(ipginttx);
862 1.25 msaitoh } else if (csc->sc_flags & FLAGS_IPG1) {
863 1.25 msaitoh if (ipginttx > MVGBE_PTFUT_IPGINTTX_V1_MAX)
864 1.25 msaitoh return -1;
865 1.25 msaitoh reg &= ~MVGBE_PTFUT_IPGINTTX_V1_MASK;
866 1.25 msaitoh reg |= MVGBE_PTFUT_IPGINTTX_V1(ipginttx);
867 1.25 msaitoh }
868 1.25 msaitoh MVGBE_WRITE(sc, MVGBE_PTFUT, reg);
869 1.25 msaitoh
870 1.25 msaitoh return 0;
871 1.25 msaitoh }
872 1.25 msaitoh
873 1.25 msaitoh static int
874 1.25 msaitoh mvgbe_ipgintrx(struct mvgbec_softc *csc, struct mvgbe_softc *sc,
875 1.25 msaitoh unsigned int ipgintrx)
876 1.24 msaitoh {
877 1.25 msaitoh uint32_t reg;
878 1.25 msaitoh reg = MVGBE_READ(sc, MVGBE_SDC);
879 1.25 msaitoh
880 1.25 msaitoh if (csc->sc_flags & FLAGS_IPG2) {
881 1.25 msaitoh if (ipgintrx > MVGBE_SDC_IPGINTRX_V2_MAX)
882 1.25 msaitoh return -1;
883 1.25 msaitoh reg &= ~MVGBE_SDC_IPGINTRX_V2_MASK;
884 1.25 msaitoh reg |= MVGBE_SDC_IPGINTRX_V2(ipgintrx);
885 1.25 msaitoh } else if (csc->sc_flags & FLAGS_IPG1) {
886 1.25 msaitoh if (ipgintrx > MVGBE_SDC_IPGINTRX_V1_MAX)
887 1.25 msaitoh return -1;
888 1.25 msaitoh reg &= ~MVGBE_SDC_IPGINTRX_V1_MASK;
889 1.25 msaitoh reg |= MVGBE_SDC_IPGINTRX_V1(ipgintrx);
890 1.25 msaitoh }
891 1.25 msaitoh MVGBE_WRITE(sc, MVGBE_SDC, reg);
892 1.24 msaitoh
893 1.25 msaitoh return 0;
894 1.24 msaitoh }
895 1.1 kiyohara
896 1.27 msaitoh static void
897 1.27 msaitoh mvgbe_tick(void *arg)
898 1.27 msaitoh {
899 1.27 msaitoh struct mvgbe_softc *sc = arg;
900 1.27 msaitoh struct mii_data *mii = &sc->sc_mii;
901 1.27 msaitoh int s;
902 1.27 msaitoh
903 1.27 msaitoh s = splnet();
904 1.27 msaitoh mii_tick(mii);
905 1.27 msaitoh /* Need more work */
906 1.32 msaitoh MVGBE_EVCNT_ADD(&sc->sc_ev_rxoverrun, MVGBE_READ(sc, MVGBE_POFC));
907 1.27 msaitoh splx(s);
908 1.27 msaitoh
909 1.27 msaitoh callout_schedule(&sc->sc_tick_ch, hz);
910 1.27 msaitoh }
911 1.27 msaitoh
912 1.1 kiyohara static int
913 1.1 kiyohara mvgbe_intr(void *arg)
914 1.1 kiyohara {
915 1.1 kiyohara struct mvgbe_softc *sc = arg;
916 1.1 kiyohara struct ifnet *ifp = &sc->sc_ethercom.ec_if;
917 1.1 kiyohara uint32_t ic, ice, datum = 0;
918 1.1 kiyohara int claimed = 0;
919 1.1 kiyohara
920 1.1 kiyohara for (;;) {
921 1.1 kiyohara ice = MVGBE_READ(sc, MVGBE_ICE);
922 1.1 kiyohara ic = MVGBE_READ(sc, MVGBE_IC);
923 1.1 kiyohara
924 1.1 kiyohara DPRINTFN(3, ("mvgbe_intr: ic=%#x, ice=%#x\n", ic, ice));
925 1.1 kiyohara if (ic == 0 && ice == 0)
926 1.1 kiyohara break;
927 1.1 kiyohara
928 1.1 kiyohara datum = datum ^ ic ^ ice;
929 1.1 kiyohara
930 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_IC, ~ic);
931 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_ICE, ~ice);
932 1.1 kiyohara
933 1.1 kiyohara claimed = 1;
934 1.1 kiyohara
935 1.26 msaitoh if (!(ifp->if_flags & IFF_RUNNING))
936 1.26 msaitoh break;
937 1.26 msaitoh
938 1.1 kiyohara if (ice & MVGBE_ICE_LINKCHG) {
939 1.1 kiyohara if (MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP) {
940 1.1 kiyohara /* Enable port RX and TX. */
941 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_ENQ(0));
942 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
943 1.1 kiyohara } else {
944 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_DISQ(0));
945 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_DISQ);
946 1.1 kiyohara }
947 1.30 msaitoh
948 1.30 msaitoh /* Notify link change event to mii layer */
949 1.30 msaitoh mii_pollstat(&sc->sc_mii);
950 1.1 kiyohara }
951 1.1 kiyohara
952 1.1 kiyohara if (ic & (MVGBE_IC_RXBUF | MVGBE_IC_RXERROR))
953 1.1 kiyohara mvgbe_rxeof(sc);
954 1.1 kiyohara
955 1.1 kiyohara if (ice & (MVGBE_ICE_TXBUF | MVGBE_ICE_TXERR))
956 1.1 kiyohara mvgbe_txeof(sc);
957 1.1 kiyohara }
958 1.1 kiyohara
959 1.1 kiyohara if (!IFQ_IS_EMPTY(&ifp->if_snd))
960 1.1 kiyohara mvgbe_start(ifp);
961 1.1 kiyohara
962 1.16 tls rnd_add_uint32(&sc->sc_rnd_source, datum);
963 1.1 kiyohara
964 1.1 kiyohara return claimed;
965 1.1 kiyohara }
966 1.1 kiyohara
967 1.1 kiyohara static void
968 1.1 kiyohara mvgbe_start(struct ifnet *ifp)
969 1.1 kiyohara {
970 1.1 kiyohara struct mvgbe_softc *sc = ifp->if_softc;
971 1.1 kiyohara struct mbuf *m_head = NULL;
972 1.1 kiyohara uint32_t idx = sc->sc_cdata.mvgbe_tx_prod;
973 1.1 kiyohara int pkts = 0;
974 1.1 kiyohara
975 1.1 kiyohara DPRINTFN(3, ("mvgbe_start (idx %d, tx_chain[idx] %p)\n", idx,
976 1.1 kiyohara sc->sc_cdata.mvgbe_tx_chain[idx].mvgbe_mbuf));
977 1.1 kiyohara
978 1.1 kiyohara if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
979 1.1 kiyohara return;
980 1.1 kiyohara /* If Link is DOWN, can't start TX */
981 1.1 kiyohara if (!(MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP))
982 1.1 kiyohara return;
983 1.1 kiyohara
984 1.1 kiyohara while (sc->sc_cdata.mvgbe_tx_chain[idx].mvgbe_mbuf == NULL) {
985 1.1 kiyohara IFQ_POLL(&ifp->if_snd, m_head);
986 1.1 kiyohara if (m_head == NULL)
987 1.1 kiyohara break;
988 1.1 kiyohara
989 1.1 kiyohara /*
990 1.1 kiyohara * Pack the data into the transmit ring. If we
991 1.1 kiyohara * don't have room, set the OACTIVE flag and wait
992 1.1 kiyohara * for the NIC to drain the ring.
993 1.1 kiyohara */
994 1.1 kiyohara if (mvgbe_encap(sc, m_head, &idx)) {
995 1.1 kiyohara ifp->if_flags |= IFF_OACTIVE;
996 1.1 kiyohara break;
997 1.1 kiyohara }
998 1.1 kiyohara
999 1.1 kiyohara /* now we are committed to transmit the packet */
1000 1.1 kiyohara IFQ_DEQUEUE(&ifp->if_snd, m_head);
1001 1.1 kiyohara pkts++;
1002 1.1 kiyohara
1003 1.1 kiyohara /*
1004 1.1 kiyohara * If there's a BPF listener, bounce a copy of this frame
1005 1.1 kiyohara * to him.
1006 1.1 kiyohara */
1007 1.10 rjs bpf_mtap(ifp, m_head);
1008 1.1 kiyohara }
1009 1.1 kiyohara if (pkts == 0)
1010 1.1 kiyohara return;
1011 1.1 kiyohara
1012 1.1 kiyohara /* Transmit at Queue 0 */
1013 1.1 kiyohara if (idx != sc->sc_cdata.mvgbe_tx_prod) {
1014 1.1 kiyohara sc->sc_cdata.mvgbe_tx_prod = idx;
1015 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
1016 1.1 kiyohara
1017 1.1 kiyohara /*
1018 1.1 kiyohara * Set a timeout in case the chip goes out to lunch.
1019 1.1 kiyohara */
1020 1.20 msaitoh ifp->if_timer = 1;
1021 1.20 msaitoh sc->sc_wdogsoft = 1;
1022 1.1 kiyohara }
1023 1.1 kiyohara }
1024 1.1 kiyohara
1025 1.1 kiyohara static int
1026 1.5 jakllsch mvgbe_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1027 1.1 kiyohara {
1028 1.1 kiyohara struct mvgbe_softc *sc = ifp->if_softc;
1029 1.1 kiyohara struct ifreq *ifr = data;
1030 1.1 kiyohara int s, error = 0;
1031 1.1 kiyohara
1032 1.1 kiyohara s = splnet();
1033 1.1 kiyohara
1034 1.5 jakllsch switch (cmd) {
1035 1.1 kiyohara case SIOCGIFMEDIA:
1036 1.1 kiyohara case SIOCSIFMEDIA:
1037 1.1 kiyohara DPRINTFN(2, ("mvgbe_ioctl MEDIA\n"));
1038 1.5 jakllsch error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1039 1.1 kiyohara break;
1040 1.1 kiyohara default:
1041 1.1 kiyohara DPRINTFN(2, ("mvgbe_ioctl ETHER\n"));
1042 1.5 jakllsch error = ether_ioctl(ifp, cmd, data);
1043 1.1 kiyohara if (error == ENETRESET) {
1044 1.1 kiyohara if (ifp->if_flags & IFF_RUNNING) {
1045 1.5 jakllsch mvgbe_filter_setup(sc);
1046 1.1 kiyohara }
1047 1.1 kiyohara error = 0;
1048 1.1 kiyohara }
1049 1.1 kiyohara break;
1050 1.1 kiyohara }
1051 1.1 kiyohara
1052 1.1 kiyohara splx(s);
1053 1.1 kiyohara
1054 1.1 kiyohara return error;
1055 1.1 kiyohara }
1056 1.1 kiyohara
1057 1.1 kiyohara static int
1058 1.1 kiyohara mvgbe_init(struct ifnet *ifp)
1059 1.1 kiyohara {
1060 1.1 kiyohara struct mvgbe_softc *sc = ifp->if_softc;
1061 1.3 kiyohara struct mvgbec_softc *csc = device_private(device_parent(sc->sc_dev));
1062 1.1 kiyohara struct mii_data *mii = &sc->sc_mii;
1063 1.3 kiyohara uint32_t reg;
1064 1.5 jakllsch int i;
1065 1.1 kiyohara
1066 1.1 kiyohara DPRINTFN(2, ("mvgbe_init\n"));
1067 1.1 kiyohara
1068 1.1 kiyohara /* Cancel pending I/O and free all RX/TX buffers. */
1069 1.1 kiyohara mvgbe_stop(ifp, 0);
1070 1.1 kiyohara
1071 1.1 kiyohara /* clear all ethernet port interrupts */
1072 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_IC, 0);
1073 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_ICE, 0);
1074 1.1 kiyohara
1075 1.1 kiyohara /* Init TX/RX descriptors */
1076 1.1 kiyohara if (mvgbe_init_tx_ring(sc) == ENOBUFS) {
1077 1.1 kiyohara aprint_error_ifnet(ifp,
1078 1.1 kiyohara "initialization failed: no memory for tx buffers\n");
1079 1.1 kiyohara return ENOBUFS;
1080 1.1 kiyohara }
1081 1.1 kiyohara if (mvgbe_init_rx_ring(sc) == ENOBUFS) {
1082 1.1 kiyohara aprint_error_ifnet(ifp,
1083 1.1 kiyohara "initialization failed: no memory for rx buffers\n");
1084 1.1 kiyohara return ENOBUFS;
1085 1.1 kiyohara }
1086 1.1 kiyohara
1087 1.25 msaitoh if ((csc->sc_flags & FLAGS_IPG1) || (csc->sc_flags & FLAGS_IPG2)) {
1088 1.25 msaitoh sc->sc_ipginttx = MVGBE_IPGINTTX_DEFAULT;
1089 1.25 msaitoh sc->sc_ipgintrx = MVGBE_IPGINTRX_DEFAULT;
1090 1.25 msaitoh }
1091 1.13 rjs if (csc->sc_flags & FLAGS_FIX_MTU)
1092 1.13 rjs MVGBE_WRITE(sc, MVGBE_MTU, 0); /* hw reset value is wrong */
1093 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_PSC,
1094 1.1 kiyohara MVGBE_PSC_ANFC | /* Enable Auto-Neg Flow Ctrl */
1095 1.1 kiyohara MVGBE_PSC_RESERVED | /* Must be set to 1 */
1096 1.1 kiyohara MVGBE_PSC_FLFAIL | /* Do NOT Force Link Fail */
1097 1.5 jakllsch MVGBE_PSC_MRU(MVGBE_PSC_MRU_9022) | /* we want 9k */
1098 1.1 kiyohara MVGBE_PSC_SETFULLDX); /* Set_FullDx */
1099 1.3 kiyohara /* XXXX: mvgbe(4) always use RGMII. */
1100 1.3 kiyohara MVGBE_WRITE(sc, MVGBE_PSC1,
1101 1.3 kiyohara MVGBE_READ(sc, MVGBE_PSC1) | MVGBE_PSC1_RGMIIEN);
1102 1.3 kiyohara /* XXXX: Also always Weighted Round-Robin Priority Mode */
1103 1.3 kiyohara MVGBE_WRITE(sc, MVGBE_TQFPC, MVGBE_TQFPC_EN(0));
1104 1.1 kiyohara
1105 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_CRDP(0), MVGBE_RX_RING_ADDR(sc, 0));
1106 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_TCQDP, MVGBE_TX_RING_ADDR(sc, 0));
1107 1.1 kiyohara
1108 1.13 rjs if (csc->sc_flags & FLAGS_FIX_TQTB) {
1109 1.1 kiyohara /*
1110 1.3 kiyohara * Queue 0 (offset 0x72700) must be programmed to 0x3fffffff.
1111 1.3 kiyohara * And offset 0x72704 must be programmed to 0x03ffffff.
1112 1.3 kiyohara * Queue 1 through 7 must be programmed to 0x0.
1113 1.1 kiyohara */
1114 1.3 kiyohara MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(0), 0x3fffffff);
1115 1.3 kiyohara MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(0), 0x03ffffff);
1116 1.3 kiyohara for (i = 1; i < 8; i++) {
1117 1.3 kiyohara MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(i), 0x0);
1118 1.3 kiyohara MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(i), 0x0);
1119 1.3 kiyohara }
1120 1.3 kiyohara } else
1121 1.3 kiyohara for (i = 1; i < 8; i++) {
1122 1.3 kiyohara MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(i), 0x3fffffff);
1123 1.3 kiyohara MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(i), 0xffff7fff);
1124 1.3 kiyohara MVGBE_WRITE(sc, MVGBE_TQAC(i), 0xfc0000ff);
1125 1.3 kiyohara }
1126 1.1 kiyohara
1127 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_PXC, MVGBE_PXC_RXCS);
1128 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_PXCX, 0);
1129 1.25 msaitoh
1130 1.25 msaitoh /* Set SDC register except IPGINT bits */
1131 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_SDC,
1132 1.1 kiyohara MVGBE_SDC_RXBSZ_16_64BITWORDS |
1133 1.1 kiyohara #if BYTE_ORDER == LITTLE_ENDIAN
1134 1.4 jakllsch MVGBE_SDC_BLMR | /* Big/Little Endian Receive Mode: No swap */
1135 1.4 jakllsch MVGBE_SDC_BLMT | /* Big/Little Endian Transmit Mode: No swap */
1136 1.1 kiyohara #endif
1137 1.1 kiyohara MVGBE_SDC_TXBSZ_16_64BITWORDS);
1138 1.25 msaitoh /* And then set IPGINT bits */
1139 1.25 msaitoh mvgbe_ipgintrx(csc, sc, sc->sc_ipgintrx);
1140 1.25 msaitoh
1141 1.25 msaitoh /* Tx side */
1142 1.25 msaitoh MVGBE_WRITE(sc, MVGBE_PTFUT, 0);
1143 1.25 msaitoh mvgbe_ipginttx(csc, sc, sc->sc_ipginttx);
1144 1.5 jakllsch
1145 1.5 jakllsch mvgbe_filter_setup(sc);
1146 1.1 kiyohara
1147 1.1 kiyohara mii_mediachg(mii);
1148 1.1 kiyohara
1149 1.1 kiyohara /* Enable port */
1150 1.1 kiyohara reg = MVGBE_READ(sc, MVGBE_PSC);
1151 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_PSC, reg | MVGBE_PSC_PORTEN);
1152 1.1 kiyohara
1153 1.1 kiyohara /* If Link is UP, Start RX and TX traffic */
1154 1.1 kiyohara if (MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP) {
1155 1.1 kiyohara /* Enable port RX/TX. */
1156 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_ENQ(0));
1157 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
1158 1.1 kiyohara }
1159 1.1 kiyohara
1160 1.1 kiyohara /* Enable interrupt masks */
1161 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_PIM,
1162 1.1 kiyohara MVGBE_IC_RXBUF |
1163 1.1 kiyohara MVGBE_IC_EXTEND |
1164 1.1 kiyohara MVGBE_IC_RXBUFQ_MASK |
1165 1.1 kiyohara MVGBE_IC_RXERROR |
1166 1.1 kiyohara MVGBE_IC_RXERRQ_MASK);
1167 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_PEIM,
1168 1.1 kiyohara MVGBE_ICE_TXBUF |
1169 1.1 kiyohara MVGBE_ICE_TXERR |
1170 1.1 kiyohara MVGBE_ICE_LINKCHG);
1171 1.1 kiyohara
1172 1.27 msaitoh callout_schedule(&sc->sc_tick_ch, hz);
1173 1.27 msaitoh
1174 1.1 kiyohara ifp->if_flags |= IFF_RUNNING;
1175 1.1 kiyohara ifp->if_flags &= ~IFF_OACTIVE;
1176 1.1 kiyohara
1177 1.1 kiyohara return 0;
1178 1.1 kiyohara }
1179 1.1 kiyohara
1180 1.1 kiyohara /* ARGSUSED */
1181 1.1 kiyohara static void
1182 1.1 kiyohara mvgbe_stop(struct ifnet *ifp, int disable)
1183 1.1 kiyohara {
1184 1.1 kiyohara struct mvgbe_softc *sc = ifp->if_softc;
1185 1.26 msaitoh struct mvgbec_softc *csc = device_private(device_parent(sc->sc_dev));
1186 1.1 kiyohara struct mvgbe_chain_data *cdata = &sc->sc_cdata;
1187 1.1 kiyohara uint32_t reg;
1188 1.1 kiyohara int i, cnt;
1189 1.1 kiyohara
1190 1.1 kiyohara DPRINTFN(2, ("mvgbe_stop\n"));
1191 1.1 kiyohara
1192 1.27 msaitoh callout_stop(&sc->sc_tick_ch);
1193 1.27 msaitoh
1194 1.1 kiyohara /* Stop Rx port activity. Check port Rx activity. */
1195 1.1 kiyohara reg = MVGBE_READ(sc, MVGBE_RQC);
1196 1.1 kiyohara if (reg & MVGBE_RQC_ENQ_MASK)
1197 1.1 kiyohara /* Issue stop command for active channels only */
1198 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_DISQ_DISABLE(reg));
1199 1.1 kiyohara
1200 1.1 kiyohara /* Stop Tx port activity. Check port Tx activity. */
1201 1.1 kiyohara if (MVGBE_READ(sc, MVGBE_TQC) & MVGBE_TQC_ENQ)
1202 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_DISQ);
1203 1.1 kiyohara
1204 1.1 kiyohara /* Force link down */
1205 1.1 kiyohara reg = MVGBE_READ(sc, MVGBE_PSC);
1206 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_PSC, reg & ~MVGBE_PSC_FLFAIL);
1207 1.1 kiyohara
1208 1.1 kiyohara #define RX_DISABLE_TIMEOUT 0x1000000
1209 1.1 kiyohara #define TX_FIFO_EMPTY_TIMEOUT 0x1000000
1210 1.1 kiyohara /* Wait for all Rx activity to terminate. */
1211 1.1 kiyohara cnt = 0;
1212 1.1 kiyohara do {
1213 1.1 kiyohara if (cnt >= RX_DISABLE_TIMEOUT) {
1214 1.1 kiyohara aprint_error_ifnet(ifp,
1215 1.1 kiyohara "timeout for RX stopped. rqc 0x%x\n", reg);
1216 1.1 kiyohara break;
1217 1.1 kiyohara }
1218 1.1 kiyohara cnt++;
1219 1.1 kiyohara
1220 1.1 kiyohara /*
1221 1.1 kiyohara * Check Receive Queue Command register that all Rx queues
1222 1.1 kiyohara * are stopped
1223 1.1 kiyohara */
1224 1.1 kiyohara reg = MVGBE_READ(sc, MVGBE_RQC);
1225 1.1 kiyohara } while (reg & 0xff);
1226 1.1 kiyohara
1227 1.1 kiyohara /* Double check to verify that TX FIFO is empty */
1228 1.1 kiyohara cnt = 0;
1229 1.1 kiyohara while (1) {
1230 1.1 kiyohara do {
1231 1.1 kiyohara if (cnt >= TX_FIFO_EMPTY_TIMEOUT) {
1232 1.1 kiyohara aprint_error_ifnet(ifp,
1233 1.1 kiyohara "timeout for TX FIFO empty. status 0x%x\n",
1234 1.1 kiyohara reg);
1235 1.1 kiyohara break;
1236 1.1 kiyohara }
1237 1.1 kiyohara cnt++;
1238 1.1 kiyohara
1239 1.1 kiyohara reg = MVGBE_READ(sc, MVGBE_PS);
1240 1.1 kiyohara } while
1241 1.1 kiyohara (!(reg & MVGBE_PS_TXFIFOEMP) || reg & MVGBE_PS_TXINPROG);
1242 1.1 kiyohara
1243 1.1 kiyohara if (cnt >= TX_FIFO_EMPTY_TIMEOUT)
1244 1.1 kiyohara break;
1245 1.1 kiyohara
1246 1.1 kiyohara /* Double check */
1247 1.1 kiyohara reg = MVGBE_READ(sc, MVGBE_PS);
1248 1.1 kiyohara if (reg & MVGBE_PS_TXFIFOEMP && !(reg & MVGBE_PS_TXINPROG))
1249 1.1 kiyohara break;
1250 1.1 kiyohara else
1251 1.1 kiyohara aprint_error_ifnet(ifp,
1252 1.1 kiyohara "TX FIFO empty double check failed."
1253 1.1 kiyohara " %d loops, status 0x%x\n", cnt, reg);
1254 1.1 kiyohara }
1255 1.1 kiyohara
1256 1.1 kiyohara /* Reset the Enable bit in the Port Serial Control Register */
1257 1.1 kiyohara reg = MVGBE_READ(sc, MVGBE_PSC);
1258 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_PSC, reg & ~MVGBE_PSC_PORTEN);
1259 1.1 kiyohara
1260 1.26 msaitoh /*
1261 1.26 msaitoh * Disable and clear interrupts
1262 1.26 msaitoh * 0) controller interrupt
1263 1.26 msaitoh * 1) port interrupt cause
1264 1.26 msaitoh * 2) port interrupt mask
1265 1.26 msaitoh */
1266 1.26 msaitoh MVGBE_WRITE(csc, MVGBE_EUIM, 0);
1267 1.26 msaitoh MVGBE_WRITE(csc, MVGBE_EUIC, 0);
1268 1.26 msaitoh MVGBE_WRITE(sc, MVGBE_IC, 0);
1269 1.26 msaitoh MVGBE_WRITE(sc, MVGBE_ICE, 0);
1270 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_PIM, 0);
1271 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_PEIM, 0);
1272 1.1 kiyohara
1273 1.1 kiyohara /* Free RX and TX mbufs still in the queues. */
1274 1.1 kiyohara for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
1275 1.1 kiyohara if (cdata->mvgbe_rx_chain[i].mvgbe_mbuf != NULL) {
1276 1.1 kiyohara m_freem(cdata->mvgbe_rx_chain[i].mvgbe_mbuf);
1277 1.1 kiyohara cdata->mvgbe_rx_chain[i].mvgbe_mbuf = NULL;
1278 1.1 kiyohara }
1279 1.1 kiyohara }
1280 1.1 kiyohara for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
1281 1.1 kiyohara if (cdata->mvgbe_tx_chain[i].mvgbe_mbuf != NULL) {
1282 1.1 kiyohara m_freem(cdata->mvgbe_tx_chain[i].mvgbe_mbuf);
1283 1.1 kiyohara cdata->mvgbe_tx_chain[i].mvgbe_mbuf = NULL;
1284 1.1 kiyohara }
1285 1.1 kiyohara }
1286 1.1 kiyohara
1287 1.1 kiyohara ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1288 1.1 kiyohara }
1289 1.1 kiyohara
1290 1.1 kiyohara static void
1291 1.1 kiyohara mvgbe_watchdog(struct ifnet *ifp)
1292 1.1 kiyohara {
1293 1.1 kiyohara struct mvgbe_softc *sc = ifp->if_softc;
1294 1.1 kiyohara
1295 1.1 kiyohara /*
1296 1.1 kiyohara * Reclaim first as there is a possibility of losing Tx completion
1297 1.1 kiyohara * interrupts.
1298 1.1 kiyohara */
1299 1.1 kiyohara mvgbe_txeof(sc);
1300 1.1 kiyohara if (sc->sc_cdata.mvgbe_tx_cnt != 0) {
1301 1.20 msaitoh if (sc->sc_wdogsoft) {
1302 1.20 msaitoh /*
1303 1.20 msaitoh * There is race condition between CPU and DMA
1304 1.20 msaitoh * engine. When DMA engine encounters queue end,
1305 1.20 msaitoh * it clears MVGBE_TQC_ENQ bit.
1306 1.20 msaitoh */
1307 1.20 msaitoh MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
1308 1.20 msaitoh ifp->if_timer = 5;
1309 1.20 msaitoh sc->sc_wdogsoft = 0;
1310 1.32 msaitoh MVGBE_EVCNT_INCR(&sc->sc_ev_wdogsoft);
1311 1.20 msaitoh } else {
1312 1.20 msaitoh aprint_error_ifnet(ifp, "watchdog timeout\n");
1313 1.1 kiyohara
1314 1.20 msaitoh ifp->if_oerrors++;
1315 1.1 kiyohara
1316 1.20 msaitoh mvgbe_init(ifp);
1317 1.20 msaitoh }
1318 1.1 kiyohara }
1319 1.1 kiyohara }
1320 1.1 kiyohara
1321 1.5 jakllsch static int
1322 1.5 jakllsch mvgbe_ifflags_cb(struct ethercom *ec)
1323 1.5 jakllsch {
1324 1.5 jakllsch struct ifnet *ifp = &ec->ec_if;
1325 1.5 jakllsch struct mvgbe_softc *sc = ifp->if_softc;
1326 1.5 jakllsch int change = ifp->if_flags ^ sc->sc_if_flags;
1327 1.5 jakllsch
1328 1.5 jakllsch if (change != 0)
1329 1.5 jakllsch sc->sc_if_flags = ifp->if_flags;
1330 1.5 jakllsch
1331 1.5 jakllsch if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0)
1332 1.5 jakllsch return ENETRESET;
1333 1.5 jakllsch
1334 1.5 jakllsch if ((change & IFF_PROMISC) != 0)
1335 1.5 jakllsch mvgbe_filter_setup(sc);
1336 1.5 jakllsch
1337 1.5 jakllsch return 0;
1338 1.5 jakllsch }
1339 1.1 kiyohara
1340 1.1 kiyohara /*
1341 1.1 kiyohara * Set media options.
1342 1.1 kiyohara */
1343 1.1 kiyohara static int
1344 1.5 jakllsch mvgbe_mediachange(struct ifnet *ifp)
1345 1.1 kiyohara {
1346 1.5 jakllsch return ether_mediachange(ifp);
1347 1.1 kiyohara }
1348 1.1 kiyohara
1349 1.1 kiyohara /*
1350 1.1 kiyohara * Report current media status.
1351 1.1 kiyohara */
1352 1.1 kiyohara static void
1353 1.5 jakllsch mvgbe_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1354 1.1 kiyohara {
1355 1.5 jakllsch ether_mediastatus(ifp, ifmr);
1356 1.1 kiyohara }
1357 1.1 kiyohara
1358 1.1 kiyohara
1359 1.1 kiyohara static int
1360 1.1 kiyohara mvgbe_init_rx_ring(struct mvgbe_softc *sc)
1361 1.1 kiyohara {
1362 1.1 kiyohara struct mvgbe_chain_data *cd = &sc->sc_cdata;
1363 1.1 kiyohara struct mvgbe_ring_data *rd = sc->sc_rdata;
1364 1.1 kiyohara int i;
1365 1.1 kiyohara
1366 1.5 jakllsch memset(rd->mvgbe_rx_ring, 0,
1367 1.1 kiyohara sizeof(struct mvgbe_rx_desc) * MVGBE_RX_RING_CNT);
1368 1.1 kiyohara
1369 1.1 kiyohara for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
1370 1.1 kiyohara cd->mvgbe_rx_chain[i].mvgbe_desc =
1371 1.1 kiyohara &rd->mvgbe_rx_ring[i];
1372 1.1 kiyohara if (i == MVGBE_RX_RING_CNT - 1) {
1373 1.1 kiyohara cd->mvgbe_rx_chain[i].mvgbe_next =
1374 1.1 kiyohara &cd->mvgbe_rx_chain[0];
1375 1.1 kiyohara rd->mvgbe_rx_ring[i].nextdescptr =
1376 1.1 kiyohara MVGBE_RX_RING_ADDR(sc, 0);
1377 1.1 kiyohara } else {
1378 1.1 kiyohara cd->mvgbe_rx_chain[i].mvgbe_next =
1379 1.1 kiyohara &cd->mvgbe_rx_chain[i + 1];
1380 1.1 kiyohara rd->mvgbe_rx_ring[i].nextdescptr =
1381 1.1 kiyohara MVGBE_RX_RING_ADDR(sc, i + 1);
1382 1.1 kiyohara }
1383 1.1 kiyohara }
1384 1.1 kiyohara
1385 1.1 kiyohara for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
1386 1.1 kiyohara if (mvgbe_newbuf(sc, i, NULL,
1387 1.1 kiyohara sc->sc_cdata.mvgbe_rx_jumbo_map) == ENOBUFS) {
1388 1.1 kiyohara aprint_error_ifnet(&sc->sc_ethercom.ec_if,
1389 1.1 kiyohara "failed alloc of %dth mbuf\n", i);
1390 1.1 kiyohara return ENOBUFS;
1391 1.1 kiyohara }
1392 1.1 kiyohara }
1393 1.1 kiyohara sc->sc_cdata.mvgbe_rx_prod = 0;
1394 1.1 kiyohara sc->sc_cdata.mvgbe_rx_cons = 0;
1395 1.1 kiyohara
1396 1.1 kiyohara return 0;
1397 1.1 kiyohara }
1398 1.1 kiyohara
1399 1.1 kiyohara static int
1400 1.1 kiyohara mvgbe_init_tx_ring(struct mvgbe_softc *sc)
1401 1.1 kiyohara {
1402 1.1 kiyohara struct mvgbe_chain_data *cd = &sc->sc_cdata;
1403 1.1 kiyohara struct mvgbe_ring_data *rd = sc->sc_rdata;
1404 1.1 kiyohara int i;
1405 1.1 kiyohara
1406 1.5 jakllsch memset(sc->sc_rdata->mvgbe_tx_ring, 0,
1407 1.1 kiyohara sizeof(struct mvgbe_tx_desc) * MVGBE_TX_RING_CNT);
1408 1.1 kiyohara
1409 1.1 kiyohara for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
1410 1.1 kiyohara cd->mvgbe_tx_chain[i].mvgbe_desc =
1411 1.1 kiyohara &rd->mvgbe_tx_ring[i];
1412 1.1 kiyohara if (i == MVGBE_TX_RING_CNT - 1) {
1413 1.1 kiyohara cd->mvgbe_tx_chain[i].mvgbe_next =
1414 1.1 kiyohara &cd->mvgbe_tx_chain[0];
1415 1.1 kiyohara rd->mvgbe_tx_ring[i].nextdescptr =
1416 1.1 kiyohara MVGBE_TX_RING_ADDR(sc, 0);
1417 1.1 kiyohara } else {
1418 1.1 kiyohara cd->mvgbe_tx_chain[i].mvgbe_next =
1419 1.1 kiyohara &cd->mvgbe_tx_chain[i + 1];
1420 1.1 kiyohara rd->mvgbe_tx_ring[i].nextdescptr =
1421 1.1 kiyohara MVGBE_TX_RING_ADDR(sc, i + 1);
1422 1.1 kiyohara }
1423 1.1 kiyohara rd->mvgbe_tx_ring[i].cmdsts = MVGBE_BUFFER_OWNED_BY_HOST;
1424 1.1 kiyohara }
1425 1.1 kiyohara
1426 1.1 kiyohara sc->sc_cdata.mvgbe_tx_prod = 0;
1427 1.1 kiyohara sc->sc_cdata.mvgbe_tx_cons = 0;
1428 1.1 kiyohara sc->sc_cdata.mvgbe_tx_cnt = 0;
1429 1.1 kiyohara
1430 1.1 kiyohara MVGBE_CDTXSYNC(sc, 0, MVGBE_TX_RING_CNT,
1431 1.1 kiyohara BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1432 1.1 kiyohara
1433 1.1 kiyohara return 0;
1434 1.1 kiyohara }
1435 1.1 kiyohara
1436 1.1 kiyohara static int
1437 1.1 kiyohara mvgbe_newbuf(struct mvgbe_softc *sc, int i, struct mbuf *m,
1438 1.1 kiyohara bus_dmamap_t dmamap)
1439 1.1 kiyohara {
1440 1.1 kiyohara struct mbuf *m_new = NULL;
1441 1.1 kiyohara struct mvgbe_chain *c;
1442 1.1 kiyohara struct mvgbe_rx_desc *r;
1443 1.1 kiyohara int align;
1444 1.19 msaitoh vaddr_t offset;
1445 1.1 kiyohara
1446 1.1 kiyohara if (m == NULL) {
1447 1.1 kiyohara void *buf = NULL;
1448 1.1 kiyohara
1449 1.1 kiyohara MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1450 1.1 kiyohara if (m_new == NULL) {
1451 1.1 kiyohara aprint_error_ifnet(&sc->sc_ethercom.ec_if,
1452 1.1 kiyohara "no memory for rx list -- packet dropped!\n");
1453 1.1 kiyohara return ENOBUFS;
1454 1.1 kiyohara }
1455 1.1 kiyohara
1456 1.1 kiyohara /* Allocate the jumbo buffer */
1457 1.1 kiyohara buf = mvgbe_jalloc(sc);
1458 1.1 kiyohara if (buf == NULL) {
1459 1.1 kiyohara m_freem(m_new);
1460 1.1 kiyohara DPRINTFN(1, ("%s jumbo allocation failed -- packet "
1461 1.1 kiyohara "dropped!\n", sc->sc_ethercom.ec_if.if_xname));
1462 1.1 kiyohara return ENOBUFS;
1463 1.1 kiyohara }
1464 1.1 kiyohara
1465 1.1 kiyohara /* Attach the buffer to the mbuf */
1466 1.1 kiyohara m_new->m_len = m_new->m_pkthdr.len = MVGBE_JLEN;
1467 1.1 kiyohara MEXTADD(m_new, buf, MVGBE_JLEN, 0, mvgbe_jfree, sc);
1468 1.1 kiyohara } else {
1469 1.1 kiyohara /*
1470 1.1 kiyohara * We're re-using a previously allocated mbuf;
1471 1.1 kiyohara * be sure to re-init pointers and lengths to
1472 1.1 kiyohara * default values.
1473 1.1 kiyohara */
1474 1.1 kiyohara m_new = m;
1475 1.1 kiyohara m_new->m_len = m_new->m_pkthdr.len = MVGBE_JLEN;
1476 1.1 kiyohara m_new->m_data = m_new->m_ext.ext_buf;
1477 1.1 kiyohara }
1478 1.5 jakllsch align = (u_long)m_new->m_data & MVGBE_RXBUF_MASK;
1479 1.5 jakllsch if (align != 0) {
1480 1.5 jakllsch DPRINTFN(1,("align = %d\n", align));
1481 1.5 jakllsch m_adj(m_new, MVGBE_RXBUF_ALIGN - align);
1482 1.5 jakllsch }
1483 1.1 kiyohara
1484 1.1 kiyohara c = &sc->sc_cdata.mvgbe_rx_chain[i];
1485 1.1 kiyohara r = c->mvgbe_desc;
1486 1.1 kiyohara c->mvgbe_mbuf = m_new;
1487 1.19 msaitoh offset = (vaddr_t)m_new->m_data - (vaddr_t)sc->sc_cdata.mvgbe_jumbo_buf;
1488 1.19 msaitoh r->bufptr = dmamap->dm_segs[0].ds_addr + offset;
1489 1.5 jakllsch r->bufsize = MVGBE_JLEN & ~MVGBE_RXBUF_MASK;
1490 1.1 kiyohara r->cmdsts = MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_ENABLE_INTERRUPT;
1491 1.1 kiyohara
1492 1.19 msaitoh /* Invalidate RX buffer */
1493 1.19 msaitoh bus_dmamap_sync(sc->sc_dmat, dmamap, offset, r->bufsize,
1494 1.19 msaitoh BUS_DMASYNC_PREREAD);
1495 1.19 msaitoh
1496 1.3 kiyohara MVGBE_CDRXSYNC(sc, i, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1497 1.1 kiyohara
1498 1.1 kiyohara return 0;
1499 1.1 kiyohara }
1500 1.1 kiyohara
1501 1.1 kiyohara /*
1502 1.1 kiyohara * Memory management for jumbo frames.
1503 1.1 kiyohara */
1504 1.1 kiyohara
1505 1.1 kiyohara static int
1506 1.1 kiyohara mvgbe_alloc_jumbo_mem(struct mvgbe_softc *sc)
1507 1.1 kiyohara {
1508 1.1 kiyohara char *ptr, *kva;
1509 1.1 kiyohara bus_dma_segment_t seg;
1510 1.1 kiyohara int i, rseg, state, error;
1511 1.1 kiyohara struct mvgbe_jpool_entry *entry;
1512 1.1 kiyohara
1513 1.1 kiyohara state = error = 0;
1514 1.1 kiyohara
1515 1.1 kiyohara /* Grab a big chunk o' storage. */
1516 1.1 kiyohara if (bus_dmamem_alloc(sc->sc_dmat, MVGBE_JMEM, PAGE_SIZE, 0,
1517 1.1 kiyohara &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1518 1.1 kiyohara aprint_error_dev(sc->sc_dev, "can't alloc rx buffers\n");
1519 1.1 kiyohara return ENOBUFS;
1520 1.1 kiyohara }
1521 1.1 kiyohara
1522 1.1 kiyohara state = 1;
1523 1.1 kiyohara if (bus_dmamem_map(sc->sc_dmat, &seg, rseg, MVGBE_JMEM,
1524 1.1 kiyohara (void **)&kva, BUS_DMA_NOWAIT)) {
1525 1.1 kiyohara aprint_error_dev(sc->sc_dev,
1526 1.1 kiyohara "can't map dma buffers (%d bytes)\n", MVGBE_JMEM);
1527 1.1 kiyohara error = ENOBUFS;
1528 1.1 kiyohara goto out;
1529 1.1 kiyohara }
1530 1.1 kiyohara
1531 1.1 kiyohara state = 2;
1532 1.1 kiyohara if (bus_dmamap_create(sc->sc_dmat, MVGBE_JMEM, 1, MVGBE_JMEM, 0,
1533 1.1 kiyohara BUS_DMA_NOWAIT, &sc->sc_cdata.mvgbe_rx_jumbo_map)) {
1534 1.1 kiyohara aprint_error_dev(sc->sc_dev, "can't create dma map\n");
1535 1.1 kiyohara error = ENOBUFS;
1536 1.1 kiyohara goto out;
1537 1.1 kiyohara }
1538 1.1 kiyohara
1539 1.1 kiyohara state = 3;
1540 1.1 kiyohara if (bus_dmamap_load(sc->sc_dmat, sc->sc_cdata.mvgbe_rx_jumbo_map,
1541 1.1 kiyohara kva, MVGBE_JMEM, NULL, BUS_DMA_NOWAIT)) {
1542 1.1 kiyohara aprint_error_dev(sc->sc_dev, "can't load dma map\n");
1543 1.1 kiyohara error = ENOBUFS;
1544 1.1 kiyohara goto out;
1545 1.1 kiyohara }
1546 1.1 kiyohara
1547 1.1 kiyohara state = 4;
1548 1.1 kiyohara sc->sc_cdata.mvgbe_jumbo_buf = (void *)kva;
1549 1.5 jakllsch DPRINTFN(1,("mvgbe_jumbo_buf = %p\n", sc->sc_cdata.mvgbe_jumbo_buf));
1550 1.1 kiyohara
1551 1.1 kiyohara LIST_INIT(&sc->sc_jfree_listhead);
1552 1.1 kiyohara LIST_INIT(&sc->sc_jinuse_listhead);
1553 1.1 kiyohara
1554 1.1 kiyohara /*
1555 1.1 kiyohara * Now divide it up into 9K pieces and save the addresses
1556 1.1 kiyohara * in an array.
1557 1.1 kiyohara */
1558 1.1 kiyohara ptr = sc->sc_cdata.mvgbe_jumbo_buf;
1559 1.1 kiyohara for (i = 0; i < MVGBE_JSLOTS; i++) {
1560 1.1 kiyohara sc->sc_cdata.mvgbe_jslots[i] = ptr;
1561 1.1 kiyohara ptr += MVGBE_JLEN;
1562 1.1 kiyohara entry = kmem_alloc(sizeof(struct mvgbe_jpool_entry), KM_SLEEP);
1563 1.1 kiyohara if (entry == NULL) {
1564 1.1 kiyohara aprint_error_dev(sc->sc_dev,
1565 1.1 kiyohara "no memory for jumbo buffer queue!\n");
1566 1.1 kiyohara error = ENOBUFS;
1567 1.1 kiyohara goto out;
1568 1.1 kiyohara }
1569 1.1 kiyohara entry->slot = i;
1570 1.1 kiyohara if (i)
1571 1.1 kiyohara LIST_INSERT_HEAD(&sc->sc_jfree_listhead, entry,
1572 1.1 kiyohara jpool_entries);
1573 1.1 kiyohara else
1574 1.1 kiyohara LIST_INSERT_HEAD(&sc->sc_jinuse_listhead, entry,
1575 1.1 kiyohara jpool_entries);
1576 1.1 kiyohara }
1577 1.1 kiyohara out:
1578 1.1 kiyohara if (error != 0) {
1579 1.1 kiyohara switch (state) {
1580 1.1 kiyohara case 4:
1581 1.1 kiyohara bus_dmamap_unload(sc->sc_dmat,
1582 1.1 kiyohara sc->sc_cdata.mvgbe_rx_jumbo_map);
1583 1.1 kiyohara case 3:
1584 1.1 kiyohara bus_dmamap_destroy(sc->sc_dmat,
1585 1.1 kiyohara sc->sc_cdata.mvgbe_rx_jumbo_map);
1586 1.1 kiyohara case 2:
1587 1.1 kiyohara bus_dmamem_unmap(sc->sc_dmat, kva, MVGBE_JMEM);
1588 1.1 kiyohara case 1:
1589 1.1 kiyohara bus_dmamem_free(sc->sc_dmat, &seg, rseg);
1590 1.1 kiyohara break;
1591 1.1 kiyohara default:
1592 1.1 kiyohara break;
1593 1.1 kiyohara }
1594 1.1 kiyohara }
1595 1.1 kiyohara
1596 1.1 kiyohara return error;
1597 1.1 kiyohara }
1598 1.1 kiyohara
1599 1.1 kiyohara /*
1600 1.1 kiyohara * Allocate a jumbo buffer.
1601 1.1 kiyohara */
1602 1.1 kiyohara static void *
1603 1.1 kiyohara mvgbe_jalloc(struct mvgbe_softc *sc)
1604 1.1 kiyohara {
1605 1.1 kiyohara struct mvgbe_jpool_entry *entry;
1606 1.1 kiyohara
1607 1.1 kiyohara entry = LIST_FIRST(&sc->sc_jfree_listhead);
1608 1.1 kiyohara
1609 1.1 kiyohara if (entry == NULL)
1610 1.1 kiyohara return NULL;
1611 1.1 kiyohara
1612 1.1 kiyohara LIST_REMOVE(entry, jpool_entries);
1613 1.1 kiyohara LIST_INSERT_HEAD(&sc->sc_jinuse_listhead, entry, jpool_entries);
1614 1.1 kiyohara return sc->sc_cdata.mvgbe_jslots[entry->slot];
1615 1.1 kiyohara }
1616 1.1 kiyohara
1617 1.1 kiyohara /*
1618 1.1 kiyohara * Release a jumbo buffer.
1619 1.1 kiyohara */
1620 1.1 kiyohara static void
1621 1.1 kiyohara mvgbe_jfree(struct mbuf *m, void *buf, size_t size, void *arg)
1622 1.1 kiyohara {
1623 1.1 kiyohara struct mvgbe_jpool_entry *entry;
1624 1.1 kiyohara struct mvgbe_softc *sc;
1625 1.1 kiyohara int i, s;
1626 1.1 kiyohara
1627 1.1 kiyohara /* Extract the softc struct pointer. */
1628 1.1 kiyohara sc = (struct mvgbe_softc *)arg;
1629 1.1 kiyohara
1630 1.1 kiyohara if (sc == NULL)
1631 1.1 kiyohara panic("%s: can't find softc pointer!", __func__);
1632 1.1 kiyohara
1633 1.1 kiyohara /* calculate the slot this buffer belongs to */
1634 1.1 kiyohara
1635 1.1 kiyohara i = ((vaddr_t)buf - (vaddr_t)sc->sc_cdata.mvgbe_jumbo_buf) / MVGBE_JLEN;
1636 1.1 kiyohara
1637 1.1 kiyohara if ((i < 0) || (i >= MVGBE_JSLOTS))
1638 1.1 kiyohara panic("%s: asked to free buffer that we don't manage!",
1639 1.1 kiyohara __func__);
1640 1.1 kiyohara
1641 1.1 kiyohara s = splvm();
1642 1.1 kiyohara entry = LIST_FIRST(&sc->sc_jinuse_listhead);
1643 1.1 kiyohara if (entry == NULL)
1644 1.1 kiyohara panic("%s: buffer not in use!", __func__);
1645 1.1 kiyohara entry->slot = i;
1646 1.1 kiyohara LIST_REMOVE(entry, jpool_entries);
1647 1.1 kiyohara LIST_INSERT_HEAD(&sc->sc_jfree_listhead, entry, jpool_entries);
1648 1.1 kiyohara
1649 1.1 kiyohara if (__predict_true(m != NULL))
1650 1.1 kiyohara pool_cache_put(mb_cache, m);
1651 1.1 kiyohara splx(s);
1652 1.1 kiyohara }
1653 1.1 kiyohara
1654 1.1 kiyohara static int
1655 1.1 kiyohara mvgbe_encap(struct mvgbe_softc *sc, struct mbuf *m_head,
1656 1.1 kiyohara uint32_t *txidx)
1657 1.1 kiyohara {
1658 1.1 kiyohara struct mvgbe_tx_desc *f = NULL;
1659 1.1 kiyohara struct mvgbe_txmap_entry *entry;
1660 1.1 kiyohara bus_dma_segment_t *txseg;
1661 1.1 kiyohara bus_dmamap_t txmap;
1662 1.1 kiyohara uint32_t first, current, last, cmdsts = 0;
1663 1.1 kiyohara int m_csumflags, i;
1664 1.14 jakllsch bool needs_defrag = false;
1665 1.1 kiyohara
1666 1.1 kiyohara DPRINTFN(3, ("mvgbe_encap\n"));
1667 1.1 kiyohara
1668 1.1 kiyohara entry = SIMPLEQ_FIRST(&sc->sc_txmap_head);
1669 1.1 kiyohara if (entry == NULL) {
1670 1.1 kiyohara DPRINTFN(2, ("mvgbe_encap: no txmap available\n"));
1671 1.1 kiyohara return ENOBUFS;
1672 1.1 kiyohara }
1673 1.1 kiyohara txmap = entry->dmamap;
1674 1.1 kiyohara
1675 1.1 kiyohara first = current = last = *txidx;
1676 1.1 kiyohara
1677 1.1 kiyohara /*
1678 1.1 kiyohara * Preserve m_pkthdr.csum_flags here since m_head might be
1679 1.1 kiyohara * updated by m_defrag()
1680 1.1 kiyohara */
1681 1.1 kiyohara m_csumflags = m_head->m_pkthdr.csum_flags;
1682 1.1 kiyohara
1683 1.14 jakllsch do_defrag:
1684 1.14 jakllsch if (__predict_false(needs_defrag == true)) {
1685 1.14 jakllsch /* A small unaligned segment was detected. */
1686 1.14 jakllsch struct mbuf *m_new;
1687 1.14 jakllsch m_new = m_defrag(m_head, M_DONTWAIT);
1688 1.14 jakllsch if (m_new == NULL)
1689 1.14 jakllsch return EFBIG;
1690 1.14 jakllsch m_head = m_new;
1691 1.14 jakllsch }
1692 1.14 jakllsch
1693 1.1 kiyohara /*
1694 1.1 kiyohara * Start packing the mbufs in this chain into
1695 1.1 kiyohara * the fragment pointers. Stop when we run out
1696 1.1 kiyohara * of fragments or hit the end of the mbuf chain.
1697 1.1 kiyohara */
1698 1.1 kiyohara if (bus_dmamap_load_mbuf(sc->sc_dmat, txmap, m_head, BUS_DMA_NOWAIT)) {
1699 1.1 kiyohara DPRINTFN(1, ("mvgbe_encap: dmamap failed\n"));
1700 1.1 kiyohara return ENOBUFS;
1701 1.1 kiyohara }
1702 1.1 kiyohara
1703 1.14 jakllsch txseg = txmap->dm_segs;
1704 1.14 jakllsch
1705 1.14 jakllsch if (__predict_true(needs_defrag == false)) {
1706 1.14 jakllsch /*
1707 1.14 jakllsch * Detect rarely encountered DMA limitation.
1708 1.14 jakllsch */
1709 1.14 jakllsch for (i = 0; i < txmap->dm_nsegs; i++) {
1710 1.14 jakllsch if (((txseg[i].ds_addr & 7) != 0) &&
1711 1.14 jakllsch (txseg[i].ds_len <= 8) &&
1712 1.14 jakllsch (txseg[i].ds_len >= 1)
1713 1.14 jakllsch ) {
1714 1.14 jakllsch txseg = NULL;
1715 1.14 jakllsch bus_dmamap_unload(sc->sc_dmat, txmap);
1716 1.14 jakllsch needs_defrag = true;
1717 1.14 jakllsch goto do_defrag;
1718 1.14 jakllsch }
1719 1.14 jakllsch }
1720 1.14 jakllsch }
1721 1.14 jakllsch
1722 1.1 kiyohara /* Sync the DMA map. */
1723 1.1 kiyohara bus_dmamap_sync(sc->sc_dmat, txmap, 0, txmap->dm_mapsize,
1724 1.1 kiyohara BUS_DMASYNC_PREWRITE);
1725 1.1 kiyohara
1726 1.1 kiyohara if (sc->sc_cdata.mvgbe_tx_cnt + txmap->dm_nsegs >=
1727 1.1 kiyohara MVGBE_TX_RING_CNT) {
1728 1.1 kiyohara DPRINTFN(2, ("mvgbe_encap: too few descriptors free\n"));
1729 1.1 kiyohara bus_dmamap_unload(sc->sc_dmat, txmap);
1730 1.1 kiyohara return ENOBUFS;
1731 1.1 kiyohara }
1732 1.1 kiyohara
1733 1.1 kiyohara
1734 1.1 kiyohara DPRINTFN(2, ("mvgbe_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
1735 1.1 kiyohara
1736 1.1 kiyohara for (i = 0; i < txmap->dm_nsegs; i++) {
1737 1.1 kiyohara f = &sc->sc_rdata->mvgbe_tx_ring[current];
1738 1.1 kiyohara f->bufptr = txseg[i].ds_addr;
1739 1.1 kiyohara f->bytecnt = txseg[i].ds_len;
1740 1.20 msaitoh if (i != 0)
1741 1.20 msaitoh f->cmdsts = MVGBE_BUFFER_OWNED_BY_DMA;
1742 1.1 kiyohara last = current;
1743 1.4 jakllsch current = MVGBE_TX_RING_NEXT(current);
1744 1.1 kiyohara }
1745 1.1 kiyohara
1746 1.1 kiyohara if (m_csumflags & M_CSUM_IPv4)
1747 1.1 kiyohara cmdsts |= MVGBE_TX_GENERATE_IP_CHKSUM;
1748 1.1 kiyohara if (m_csumflags & M_CSUM_TCPv4)
1749 1.1 kiyohara cmdsts |=
1750 1.1 kiyohara MVGBE_TX_GENERATE_L4_CHKSUM | MVGBE_TX_L4_TYPE_TCP;
1751 1.1 kiyohara if (m_csumflags & M_CSUM_UDPv4)
1752 1.1 kiyohara cmdsts |=
1753 1.1 kiyohara MVGBE_TX_GENERATE_L4_CHKSUM | MVGBE_TX_L4_TYPE_UDP;
1754 1.1 kiyohara if (m_csumflags & (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
1755 1.1 kiyohara const int iphdr_unitlen = sizeof(struct ip) / sizeof(uint32_t);
1756 1.1 kiyohara
1757 1.1 kiyohara cmdsts |= MVGBE_TX_IP_NO_FRAG |
1758 1.1 kiyohara MVGBE_TX_IP_HEADER_LEN(iphdr_unitlen); /* unit is 4B */
1759 1.1 kiyohara }
1760 1.1 kiyohara if (txmap->dm_nsegs == 1)
1761 1.1 kiyohara f->cmdsts = cmdsts |
1762 1.1 kiyohara MVGBE_TX_GENERATE_CRC |
1763 1.1 kiyohara MVGBE_TX_ENABLE_INTERRUPT |
1764 1.1 kiyohara MVGBE_TX_ZERO_PADDING |
1765 1.1 kiyohara MVGBE_TX_FIRST_DESC |
1766 1.1 kiyohara MVGBE_TX_LAST_DESC;
1767 1.1 kiyohara else {
1768 1.1 kiyohara f = &sc->sc_rdata->mvgbe_tx_ring[first];
1769 1.1 kiyohara f->cmdsts = cmdsts |
1770 1.1 kiyohara MVGBE_TX_GENERATE_CRC |
1771 1.1 kiyohara MVGBE_TX_FIRST_DESC;
1772 1.1 kiyohara
1773 1.1 kiyohara f = &sc->sc_rdata->mvgbe_tx_ring[last];
1774 1.1 kiyohara f->cmdsts =
1775 1.1 kiyohara MVGBE_BUFFER_OWNED_BY_DMA |
1776 1.1 kiyohara MVGBE_TX_ENABLE_INTERRUPT |
1777 1.1 kiyohara MVGBE_TX_ZERO_PADDING |
1778 1.1 kiyohara MVGBE_TX_LAST_DESC;
1779 1.20 msaitoh
1780 1.20 msaitoh /* Sync descriptors except first */
1781 1.20 msaitoh MVGBE_CDTXSYNC(sc,
1782 1.20 msaitoh (MVGBE_TX_RING_CNT - 1 == *txidx) ? 0 : (*txidx) + 1,
1783 1.20 msaitoh txmap->dm_nsegs - 1,
1784 1.20 msaitoh BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1785 1.1 kiyohara }
1786 1.1 kiyohara
1787 1.1 kiyohara sc->sc_cdata.mvgbe_tx_chain[last].mvgbe_mbuf = m_head;
1788 1.1 kiyohara SIMPLEQ_REMOVE_HEAD(&sc->sc_txmap_head, link);
1789 1.1 kiyohara sc->sc_cdata.mvgbe_tx_map[last] = entry;
1790 1.1 kiyohara
1791 1.20 msaitoh /* Finally, sync first descriptor */
1792 1.20 msaitoh sc->sc_rdata->mvgbe_tx_ring[first].cmdsts |=
1793 1.20 msaitoh MVGBE_BUFFER_OWNED_BY_DMA;
1794 1.20 msaitoh MVGBE_CDTXSYNC(sc, *txidx, 1,
1795 1.3 kiyohara BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1796 1.1 kiyohara
1797 1.1 kiyohara sc->sc_cdata.mvgbe_tx_cnt += i;
1798 1.1 kiyohara *txidx = current;
1799 1.1 kiyohara
1800 1.1 kiyohara DPRINTFN(3, ("mvgbe_encap: completed successfully\n"));
1801 1.1 kiyohara
1802 1.1 kiyohara return 0;
1803 1.1 kiyohara }
1804 1.1 kiyohara
1805 1.1 kiyohara static void
1806 1.1 kiyohara mvgbe_rxeof(struct mvgbe_softc *sc)
1807 1.1 kiyohara {
1808 1.1 kiyohara struct mvgbe_chain_data *cdata = &sc->sc_cdata;
1809 1.1 kiyohara struct mvgbe_rx_desc *cur_rx;
1810 1.1 kiyohara struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1811 1.1 kiyohara struct mbuf *m;
1812 1.1 kiyohara bus_dmamap_t dmamap;
1813 1.1 kiyohara uint32_t rxstat;
1814 1.21 msaitoh uint16_t bufsize;
1815 1.1 kiyohara int idx, cur, total_len;
1816 1.1 kiyohara
1817 1.1 kiyohara idx = sc->sc_cdata.mvgbe_rx_prod;
1818 1.1 kiyohara
1819 1.1 kiyohara DPRINTFN(3, ("mvgbe_rxeof %d\n", idx));
1820 1.1 kiyohara
1821 1.1 kiyohara for (;;) {
1822 1.1 kiyohara cur = idx;
1823 1.1 kiyohara
1824 1.1 kiyohara /* Sync the descriptor */
1825 1.1 kiyohara MVGBE_CDRXSYNC(sc, idx,
1826 1.1 kiyohara BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1827 1.1 kiyohara
1828 1.1 kiyohara cur_rx = &sc->sc_rdata->mvgbe_rx_ring[idx];
1829 1.1 kiyohara
1830 1.1 kiyohara if ((cur_rx->cmdsts & MVGBE_BUFFER_OWNED_MASK) ==
1831 1.1 kiyohara MVGBE_BUFFER_OWNED_BY_DMA) {
1832 1.1 kiyohara /* Invalidate the descriptor -- it's not ready yet */
1833 1.1 kiyohara MVGBE_CDRXSYNC(sc, idx, BUS_DMASYNC_PREREAD);
1834 1.1 kiyohara sc->sc_cdata.mvgbe_rx_prod = idx;
1835 1.1 kiyohara break;
1836 1.1 kiyohara }
1837 1.1 kiyohara #ifdef DIAGNOSTIC
1838 1.1 kiyohara if ((cur_rx->cmdsts &
1839 1.1 kiyohara (MVGBE_RX_LAST_DESC | MVGBE_RX_FIRST_DESC)) !=
1840 1.1 kiyohara (MVGBE_RX_LAST_DESC | MVGBE_RX_FIRST_DESC))
1841 1.1 kiyohara panic(
1842 1.1 kiyohara "mvgbe_rxeof: buffer size is smaller than packet");
1843 1.1 kiyohara #endif
1844 1.1 kiyohara
1845 1.1 kiyohara dmamap = sc->sc_cdata.mvgbe_rx_jumbo_map;
1846 1.1 kiyohara
1847 1.1 kiyohara bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
1848 1.1 kiyohara BUS_DMASYNC_POSTREAD);
1849 1.1 kiyohara
1850 1.1 kiyohara m = cdata->mvgbe_rx_chain[idx].mvgbe_mbuf;
1851 1.1 kiyohara cdata->mvgbe_rx_chain[idx].mvgbe_mbuf = NULL;
1852 1.29 msaitoh total_len = cur_rx->bytecnt - ETHER_CRC_LEN;
1853 1.1 kiyohara rxstat = cur_rx->cmdsts;
1854 1.21 msaitoh bufsize = cur_rx->bufsize;
1855 1.1 kiyohara
1856 1.1 kiyohara cdata->mvgbe_rx_map[idx] = NULL;
1857 1.1 kiyohara
1858 1.4 jakllsch idx = MVGBE_RX_RING_NEXT(idx);
1859 1.1 kiyohara
1860 1.1 kiyohara if (rxstat & MVGBE_ERROR_SUMMARY) {
1861 1.1 kiyohara #if 0
1862 1.1 kiyohara int err = rxstat & MVGBE_RX_ERROR_CODE_MASK;
1863 1.1 kiyohara
1864 1.1 kiyohara if (err == MVGBE_RX_CRC_ERROR)
1865 1.1 kiyohara ifp->if_ierrors++;
1866 1.1 kiyohara if (err == MVGBE_RX_OVERRUN_ERROR)
1867 1.1 kiyohara ifp->if_ierrors++;
1868 1.1 kiyohara if (err == MVGBE_RX_MAX_FRAME_LEN_ERROR)
1869 1.1 kiyohara ifp->if_ierrors++;
1870 1.1 kiyohara if (err == MVGBE_RX_RESOURCE_ERROR)
1871 1.1 kiyohara ifp->if_ierrors++;
1872 1.1 kiyohara #else
1873 1.1 kiyohara ifp->if_ierrors++;
1874 1.1 kiyohara #endif
1875 1.1 kiyohara mvgbe_newbuf(sc, cur, m, dmamap);
1876 1.1 kiyohara continue;
1877 1.1 kiyohara }
1878 1.1 kiyohara
1879 1.5 jakllsch if (rxstat & MVGBE_RX_IP_FRAME_TYPE) {
1880 1.21 msaitoh int flgs = 0;
1881 1.21 msaitoh
1882 1.5 jakllsch /* Check IPv4 header checksum */
1883 1.22 msaitoh flgs |= M_CSUM_IPv4;
1884 1.5 jakllsch if (!(rxstat & MVGBE_RX_IP_HEADER_OK))
1885 1.21 msaitoh flgs |= M_CSUM_IPv4_BAD;
1886 1.23 msaitoh else if ((bufsize & MVGBE_RX_IP_FRAGMENT) == 0) {
1887 1.22 msaitoh /*
1888 1.22 msaitoh * Check TCPv4/UDPv4 checksum for
1889 1.22 msaitoh * non-fragmented packet only.
1890 1.22 msaitoh *
1891 1.22 msaitoh * It seemd that sometimes
1892 1.22 msaitoh * MVGBE_RX_L4_CHECKSUM_OK bit was set to 0
1893 1.22 msaitoh * even if the checksum is correct and the
1894 1.22 msaitoh * packet was not fragmented. So we don't set
1895 1.22 msaitoh * M_CSUM_TCP_UDP_BAD even if csum bit is 0.
1896 1.22 msaitoh */
1897 1.22 msaitoh
1898 1.22 msaitoh if (((rxstat & MVGBE_RX_L4_TYPE_MASK) ==
1899 1.22 msaitoh MVGBE_RX_L4_TYPE_TCP) &&
1900 1.22 msaitoh ((rxstat & MVGBE_RX_L4_CHECKSUM_OK) != 0))
1901 1.21 msaitoh flgs |= M_CSUM_TCPv4;
1902 1.22 msaitoh else if (((rxstat & MVGBE_RX_L4_TYPE_MASK) ==
1903 1.22 msaitoh MVGBE_RX_L4_TYPE_UDP) &&
1904 1.22 msaitoh ((rxstat & MVGBE_RX_L4_CHECKSUM_OK) != 0))
1905 1.21 msaitoh flgs |= M_CSUM_UDPv4;
1906 1.21 msaitoh }
1907 1.21 msaitoh m->m_pkthdr.csum_flags = flgs;
1908 1.1 kiyohara }
1909 1.1 kiyohara
1910 1.1 kiyohara /*
1911 1.1 kiyohara * Try to allocate a new jumbo buffer. If that
1912 1.1 kiyohara * fails, copy the packet to mbufs and put the
1913 1.1 kiyohara * jumbo buffer back in the ring so it can be
1914 1.1 kiyohara * re-used. If allocating mbufs fails, then we
1915 1.1 kiyohara * have to drop the packet.
1916 1.1 kiyohara */
1917 1.1 kiyohara if (mvgbe_newbuf(sc, cur, NULL, dmamap) == ENOBUFS) {
1918 1.1 kiyohara struct mbuf *m0;
1919 1.1 kiyohara
1920 1.1 kiyohara m0 = m_devget(mtod(m, char *), total_len, 0, ifp, NULL);
1921 1.1 kiyohara mvgbe_newbuf(sc, cur, m, dmamap);
1922 1.1 kiyohara if (m0 == NULL) {
1923 1.1 kiyohara aprint_error_ifnet(ifp,
1924 1.1 kiyohara "no receive buffers available --"
1925 1.1 kiyohara " packet dropped!\n");
1926 1.1 kiyohara ifp->if_ierrors++;
1927 1.1 kiyohara continue;
1928 1.1 kiyohara }
1929 1.1 kiyohara m = m0;
1930 1.1 kiyohara } else {
1931 1.1 kiyohara m->m_pkthdr.rcvif = ifp;
1932 1.1 kiyohara m->m_pkthdr.len = m->m_len = total_len;
1933 1.1 kiyohara }
1934 1.1 kiyohara
1935 1.1 kiyohara /* Skip on first 2byte (HW header) */
1936 1.1 kiyohara m_adj(m, MVGBE_HWHEADER_SIZE);
1937 1.1 kiyohara
1938 1.1 kiyohara ifp->if_ipackets++;
1939 1.1 kiyohara
1940 1.10 rjs bpf_mtap(ifp, m);
1941 1.1 kiyohara
1942 1.1 kiyohara /* pass it on. */
1943 1.1 kiyohara (*ifp->if_input)(ifp, m);
1944 1.1 kiyohara }
1945 1.1 kiyohara }
1946 1.1 kiyohara
1947 1.1 kiyohara static void
1948 1.1 kiyohara mvgbe_txeof(struct mvgbe_softc *sc)
1949 1.1 kiyohara {
1950 1.1 kiyohara struct mvgbe_chain_data *cdata = &sc->sc_cdata;
1951 1.1 kiyohara struct mvgbe_tx_desc *cur_tx;
1952 1.1 kiyohara struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1953 1.1 kiyohara struct mvgbe_txmap_entry *entry;
1954 1.1 kiyohara int idx;
1955 1.1 kiyohara
1956 1.1 kiyohara DPRINTFN(3, ("mvgbe_txeof\n"));
1957 1.1 kiyohara
1958 1.1 kiyohara /*
1959 1.1 kiyohara * Go through our tx ring and free mbufs for those
1960 1.1 kiyohara * frames that have been sent.
1961 1.1 kiyohara */
1962 1.1 kiyohara idx = cdata->mvgbe_tx_cons;
1963 1.1 kiyohara while (idx != cdata->mvgbe_tx_prod) {
1964 1.1 kiyohara MVGBE_CDTXSYNC(sc, idx, 1,
1965 1.1 kiyohara BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1966 1.1 kiyohara
1967 1.1 kiyohara cur_tx = &sc->sc_rdata->mvgbe_tx_ring[idx];
1968 1.1 kiyohara #ifdef MVGBE_DEBUG
1969 1.1 kiyohara if (mvgbe_debug >= 3)
1970 1.1 kiyohara mvgbe_dump_txdesc(cur_tx, idx);
1971 1.1 kiyohara #endif
1972 1.1 kiyohara if ((cur_tx->cmdsts & MVGBE_BUFFER_OWNED_MASK) ==
1973 1.1 kiyohara MVGBE_BUFFER_OWNED_BY_DMA) {
1974 1.1 kiyohara MVGBE_CDTXSYNC(sc, idx, 1, BUS_DMASYNC_PREREAD);
1975 1.1 kiyohara break;
1976 1.1 kiyohara }
1977 1.1 kiyohara if (cur_tx->cmdsts & MVGBE_TX_LAST_DESC)
1978 1.1 kiyohara ifp->if_opackets++;
1979 1.1 kiyohara if (cur_tx->cmdsts & MVGBE_ERROR_SUMMARY) {
1980 1.1 kiyohara int err = cur_tx->cmdsts & MVGBE_TX_ERROR_CODE_MASK;
1981 1.1 kiyohara
1982 1.1 kiyohara if (err == MVGBE_TX_LATE_COLLISION_ERROR)
1983 1.1 kiyohara ifp->if_collisions++;
1984 1.1 kiyohara if (err == MVGBE_TX_UNDERRUN_ERROR)
1985 1.1 kiyohara ifp->if_oerrors++;
1986 1.1 kiyohara if (err == MVGBE_TX_EXCESSIVE_COLLISION_ERRO)
1987 1.1 kiyohara ifp->if_collisions++;
1988 1.1 kiyohara }
1989 1.1 kiyohara if (cdata->mvgbe_tx_chain[idx].mvgbe_mbuf != NULL) {
1990 1.1 kiyohara entry = cdata->mvgbe_tx_map[idx];
1991 1.1 kiyohara
1992 1.1 kiyohara m_freem(cdata->mvgbe_tx_chain[idx].mvgbe_mbuf);
1993 1.1 kiyohara cdata->mvgbe_tx_chain[idx].mvgbe_mbuf = NULL;
1994 1.1 kiyohara
1995 1.1 kiyohara bus_dmamap_sync(sc->sc_dmat, entry->dmamap, 0,
1996 1.1 kiyohara entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1997 1.1 kiyohara
1998 1.1 kiyohara bus_dmamap_unload(sc->sc_dmat, entry->dmamap);
1999 1.1 kiyohara SIMPLEQ_INSERT_TAIL(&sc->sc_txmap_head, entry, link);
2000 1.1 kiyohara cdata->mvgbe_tx_map[idx] = NULL;
2001 1.1 kiyohara }
2002 1.1 kiyohara cdata->mvgbe_tx_cnt--;
2003 1.4 jakllsch idx = MVGBE_TX_RING_NEXT(idx);
2004 1.1 kiyohara }
2005 1.1 kiyohara if (cdata->mvgbe_tx_cnt == 0)
2006 1.1 kiyohara ifp->if_timer = 0;
2007 1.1 kiyohara
2008 1.1 kiyohara if (cdata->mvgbe_tx_cnt < MVGBE_TX_RING_CNT - 2)
2009 1.1 kiyohara ifp->if_flags &= ~IFF_OACTIVE;
2010 1.1 kiyohara
2011 1.1 kiyohara cdata->mvgbe_tx_cons = idx;
2012 1.1 kiyohara }
2013 1.1 kiyohara
2014 1.5 jakllsch static uint8_t
2015 1.5 jakllsch mvgbe_crc8(const uint8_t *data, size_t size)
2016 1.5 jakllsch {
2017 1.5 jakllsch int bit;
2018 1.5 jakllsch uint8_t byte;
2019 1.5 jakllsch uint8_t crc = 0;
2020 1.5 jakllsch const uint8_t poly = 0x07;
2021 1.5 jakllsch
2022 1.5 jakllsch while(size--)
2023 1.5 jakllsch for (byte = *data++, bit = NBBY-1; bit >= 0; bit--)
2024 1.5 jakllsch crc = (crc << 1) ^ ((((crc >> 7) ^ (byte >> bit)) & 1) ? poly : 0);
2025 1.5 jakllsch
2026 1.5 jakllsch return crc;
2027 1.5 jakllsch }
2028 1.5 jakllsch
2029 1.5 jakllsch CTASSERT(MVGBE_NDFSMT == MVGBE_NDFOMT);
2030 1.5 jakllsch
2031 1.1 kiyohara static void
2032 1.5 jakllsch mvgbe_filter_setup(struct mvgbe_softc *sc)
2033 1.1 kiyohara {
2034 1.5 jakllsch struct ethercom *ec = &sc->sc_ethercom;
2035 1.1 kiyohara struct ifnet *ifp= &sc->sc_ethercom.ec_if;
2036 1.5 jakllsch struct ether_multi *enm;
2037 1.5 jakllsch struct ether_multistep step;
2038 1.12 jakllsch uint32_t dfut[MVGBE_NDFUT], dfsmt[MVGBE_NDFSMT], dfomt[MVGBE_NDFOMT];
2039 1.5 jakllsch uint32_t pxc;
2040 1.5 jakllsch int i;
2041 1.5 jakllsch const uint8_t special[ETHER_ADDR_LEN] = {0x01,0x00,0x5e,0x00,0x00,0x00};
2042 1.5 jakllsch
2043 1.12 jakllsch memset(dfut, 0, sizeof(dfut));
2044 1.12 jakllsch memset(dfsmt, 0, sizeof(dfsmt));
2045 1.12 jakllsch memset(dfomt, 0, sizeof(dfomt));
2046 1.5 jakllsch
2047 1.5 jakllsch if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC)) {
2048 1.5 jakllsch goto allmulti;
2049 1.5 jakllsch }
2050 1.5 jakllsch
2051 1.5 jakllsch ETHER_FIRST_MULTI(step, ec, enm);
2052 1.5 jakllsch while (enm != NULL) {
2053 1.5 jakllsch if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2054 1.5 jakllsch /* ranges are complex and somewhat rare */
2055 1.5 jakllsch goto allmulti;
2056 1.5 jakllsch }
2057 1.5 jakllsch /* chip handles some IPv4 multicast specially */
2058 1.5 jakllsch if (memcmp(enm->enm_addrlo, special, 5) == 0) {
2059 1.5 jakllsch i = enm->enm_addrlo[5];
2060 1.5 jakllsch dfsmt[i>>2] =
2061 1.5 jakllsch MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
2062 1.5 jakllsch } else {
2063 1.5 jakllsch i = mvgbe_crc8(enm->enm_addrlo, ETHER_ADDR_LEN);
2064 1.5 jakllsch dfomt[i>>2] =
2065 1.5 jakllsch MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
2066 1.5 jakllsch }
2067 1.5 jakllsch
2068 1.5 jakllsch ETHER_NEXT_MULTI(step, enm);
2069 1.5 jakllsch }
2070 1.5 jakllsch goto set;
2071 1.1 kiyohara
2072 1.5 jakllsch allmulti:
2073 1.5 jakllsch if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC)) {
2074 1.5 jakllsch for (i = 0; i < MVGBE_NDFSMT; i++) {
2075 1.5 jakllsch dfsmt[i] = dfomt[i] =
2076 1.5 jakllsch MVGBE_DF(0, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
2077 1.5 jakllsch MVGBE_DF(1, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
2078 1.5 jakllsch MVGBE_DF(2, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
2079 1.5 jakllsch MVGBE_DF(3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
2080 1.5 jakllsch }
2081 1.1 kiyohara }
2082 1.1 kiyohara
2083 1.5 jakllsch set:
2084 1.1 kiyohara pxc = MVGBE_READ(sc, MVGBE_PXC);
2085 1.1 kiyohara pxc &= ~MVGBE_PXC_UPM;
2086 1.5 jakllsch pxc |= MVGBE_PXC_RB | MVGBE_PXC_RBIP | MVGBE_PXC_RBARP;
2087 1.5 jakllsch if (ifp->if_flags & IFF_BROADCAST) {
2088 1.5 jakllsch pxc &= ~(MVGBE_PXC_RB | MVGBE_PXC_RBIP | MVGBE_PXC_RBARP);
2089 1.5 jakllsch }
2090 1.5 jakllsch if (ifp->if_flags & IFF_PROMISC) {
2091 1.5 jakllsch pxc |= MVGBE_PXC_UPM;
2092 1.5 jakllsch }
2093 1.1 kiyohara MVGBE_WRITE(sc, MVGBE_PXC, pxc);
2094 1.1 kiyohara
2095 1.5 jakllsch /* Set Destination Address Filter Unicast Table */
2096 1.5 jakllsch i = sc->sc_enaddr[5] & 0xf; /* last nibble */
2097 1.5 jakllsch dfut[i>>2] = MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
2098 1.5 jakllsch MVGBE_WRITE_FILTER(sc, MVGBE_DFUT, dfut, MVGBE_NDFUT);
2099 1.5 jakllsch
2100 1.1 kiyohara /* Set Destination Address Filter Multicast Tables */
2101 1.5 jakllsch MVGBE_WRITE_FILTER(sc, MVGBE_DFSMT, dfsmt, MVGBE_NDFSMT);
2102 1.5 jakllsch MVGBE_WRITE_FILTER(sc, MVGBE_DFOMT, dfomt, MVGBE_NDFOMT);
2103 1.1 kiyohara }
2104 1.1 kiyohara
2105 1.1 kiyohara #ifdef MVGBE_DEBUG
2106 1.1 kiyohara static void
2107 1.1 kiyohara mvgbe_dump_txdesc(struct mvgbe_tx_desc *desc, int idx)
2108 1.1 kiyohara {
2109 1.1 kiyohara #define DESC_PRINT(X) \
2110 1.1 kiyohara if (X) \
2111 1.1 kiyohara printf("txdesc[%d]." #X "=%#x\n", idx, X);
2112 1.1 kiyohara
2113 1.1 kiyohara #if BYTE_ORDER == BIG_ENDIAN
2114 1.1 kiyohara DESC_PRINT(desc->bytecnt);
2115 1.1 kiyohara DESC_PRINT(desc->l4ichk);
2116 1.1 kiyohara DESC_PRINT(desc->cmdsts);
2117 1.1 kiyohara DESC_PRINT(desc->nextdescptr);
2118 1.1 kiyohara DESC_PRINT(desc->bufptr);
2119 1.1 kiyohara #else /* LITTLE_ENDIAN */
2120 1.1 kiyohara DESC_PRINT(desc->cmdsts);
2121 1.1 kiyohara DESC_PRINT(desc->l4ichk);
2122 1.1 kiyohara DESC_PRINT(desc->bytecnt);
2123 1.1 kiyohara DESC_PRINT(desc->bufptr);
2124 1.1 kiyohara DESC_PRINT(desc->nextdescptr);
2125 1.1 kiyohara #endif
2126 1.1 kiyohara #undef DESC_PRINT
2127 1.1 kiyohara }
2128 1.1 kiyohara #endif
2129 1.25 msaitoh
2130 1.25 msaitoh SYSCTL_SETUP(sysctl_mvgbe, "sysctl mvgbe subtree setup")
2131 1.25 msaitoh {
2132 1.25 msaitoh int rc;
2133 1.25 msaitoh const struct sysctlnode *node;
2134 1.25 msaitoh
2135 1.25 msaitoh if ((rc = sysctl_createv(clog, 0, NULL, NULL,
2136 1.25 msaitoh 0, CTLTYPE_NODE, "hw", NULL,
2137 1.25 msaitoh NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
2138 1.25 msaitoh goto err;
2139 1.25 msaitoh }
2140 1.25 msaitoh
2141 1.25 msaitoh if ((rc = sysctl_createv(clog, 0, NULL, &node,
2142 1.25 msaitoh 0, CTLTYPE_NODE, "mvgbe",
2143 1.25 msaitoh SYSCTL_DESCR("mvgbe interface controls"),
2144 1.25 msaitoh NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
2145 1.25 msaitoh goto err;
2146 1.25 msaitoh }
2147 1.25 msaitoh
2148 1.25 msaitoh mvgbe_root_num = node->sysctl_num;
2149 1.25 msaitoh return;
2150 1.25 msaitoh
2151 1.25 msaitoh err:
2152 1.25 msaitoh aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
2153 1.25 msaitoh }
2154 1.25 msaitoh
2155 1.25 msaitoh static void
2156 1.25 msaitoh sysctl_mvgbe_init(struct mvgbe_softc *sc)
2157 1.25 msaitoh {
2158 1.25 msaitoh const struct sysctlnode *node;
2159 1.25 msaitoh int mvgbe_nodenum;
2160 1.25 msaitoh
2161 1.25 msaitoh if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
2162 1.25 msaitoh 0, CTLTYPE_NODE, device_xname(sc->sc_dev),
2163 1.25 msaitoh SYSCTL_DESCR("mvgbe per-controller controls"),
2164 1.25 msaitoh NULL, 0, NULL, 0, CTL_HW, mvgbe_root_num, CTL_CREATE,
2165 1.25 msaitoh CTL_EOL) != 0) {
2166 1.25 msaitoh aprint_normal_dev(sc->sc_dev, "couldn't create sysctl node\n");
2167 1.25 msaitoh return;
2168 1.25 msaitoh }
2169 1.25 msaitoh mvgbe_nodenum = node->sysctl_num;
2170 1.25 msaitoh
2171 1.25 msaitoh /* interrupt moderation sysctls */
2172 1.25 msaitoh if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
2173 1.25 msaitoh CTLFLAG_READWRITE, CTLTYPE_INT, "ipginttx",
2174 1.25 msaitoh SYSCTL_DESCR("mvgbe TX interrupt moderation timer"),
2175 1.25 msaitoh mvgbe_sysctl_ipginttx, 0, (void *)sc,
2176 1.25 msaitoh 0, CTL_HW, mvgbe_root_num, mvgbe_nodenum, CTL_CREATE,
2177 1.25 msaitoh CTL_EOL) != 0) {
2178 1.25 msaitoh aprint_normal_dev(sc->sc_dev,
2179 1.25 msaitoh "couldn't create ipginttx sysctl node\n");
2180 1.25 msaitoh }
2181 1.25 msaitoh if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
2182 1.25 msaitoh CTLFLAG_READWRITE, CTLTYPE_INT, "ipgintrx",
2183 1.25 msaitoh SYSCTL_DESCR("mvgbe RX interrupt moderation timer"),
2184 1.25 msaitoh mvgbe_sysctl_ipgintrx, 0, (void *)sc,
2185 1.25 msaitoh 0, CTL_HW, mvgbe_root_num, mvgbe_nodenum, CTL_CREATE,
2186 1.25 msaitoh CTL_EOL) != 0) {
2187 1.25 msaitoh aprint_normal_dev(sc->sc_dev,
2188 1.25 msaitoh "couldn't create ipginttx sysctl node\n");
2189 1.25 msaitoh }
2190 1.25 msaitoh }
2191 1.25 msaitoh
2192 1.25 msaitoh static int
2193 1.25 msaitoh mvgbe_sysctl_ipginttx(SYSCTLFN_ARGS)
2194 1.25 msaitoh {
2195 1.25 msaitoh int error;
2196 1.25 msaitoh unsigned int t;
2197 1.25 msaitoh struct sysctlnode node;
2198 1.25 msaitoh struct mvgbec_softc *csc;
2199 1.25 msaitoh struct mvgbe_softc *sc;
2200 1.25 msaitoh
2201 1.25 msaitoh node = *rnode;
2202 1.25 msaitoh sc = node.sysctl_data;
2203 1.25 msaitoh csc = device_private(device_parent(sc->sc_dev));
2204 1.25 msaitoh t = sc->sc_ipginttx;
2205 1.25 msaitoh node.sysctl_data = &t;
2206 1.25 msaitoh error = sysctl_lookup(SYSCTLFN_CALL(&node));
2207 1.25 msaitoh if (error || newp == NULL)
2208 1.25 msaitoh return error;
2209 1.25 msaitoh
2210 1.25 msaitoh if (mvgbe_ipginttx(csc, sc, t) < 0)
2211 1.25 msaitoh return EINVAL;
2212 1.25 msaitoh /*
2213 1.25 msaitoh * update the softc with sysctl-changed value, and mark
2214 1.25 msaitoh * for hardware update
2215 1.25 msaitoh */
2216 1.25 msaitoh sc->sc_ipginttx = t;
2217 1.25 msaitoh
2218 1.25 msaitoh return 0;
2219 1.25 msaitoh }
2220 1.25 msaitoh
2221 1.25 msaitoh static int
2222 1.25 msaitoh mvgbe_sysctl_ipgintrx(SYSCTLFN_ARGS)
2223 1.25 msaitoh {
2224 1.25 msaitoh int error;
2225 1.25 msaitoh unsigned int t;
2226 1.25 msaitoh struct sysctlnode node;
2227 1.25 msaitoh struct mvgbec_softc *csc;
2228 1.25 msaitoh struct mvgbe_softc *sc;
2229 1.25 msaitoh
2230 1.25 msaitoh node = *rnode;
2231 1.25 msaitoh sc = node.sysctl_data;
2232 1.25 msaitoh csc = device_private(device_parent(sc->sc_dev));
2233 1.25 msaitoh t = sc->sc_ipgintrx;
2234 1.25 msaitoh node.sysctl_data = &t;
2235 1.25 msaitoh error = sysctl_lookup(SYSCTLFN_CALL(&node));
2236 1.25 msaitoh if (error || newp == NULL)
2237 1.25 msaitoh return error;
2238 1.25 msaitoh
2239 1.25 msaitoh if (mvgbe_ipgintrx(csc, sc, t) < 0)
2240 1.25 msaitoh return EINVAL;
2241 1.25 msaitoh /*
2242 1.25 msaitoh * update the softc with sysctl-changed value, and mark
2243 1.25 msaitoh * for hardware update
2244 1.25 msaitoh */
2245 1.25 msaitoh sc->sc_ipgintrx = t;
2246 1.25 msaitoh
2247 1.25 msaitoh return 0;
2248 1.25 msaitoh }
2249