xref: /freebsd/sys/powerpc/ps3/if_glc.c (revision e17f5b1d307b7b8910d67883e57a9604305906d5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2010 Nathan Whitehorn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sockio.h>
33 #include <sys/endian.h>
34 #include <sys/lock.h>
35 #include <sys/mbuf.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/kernel.h>
40 #include <sys/socket.h>
41 
42 #include <vm/vm.h>
43 #include <vm/pmap.h>
44 
45 #include <net/bpf.h>
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/ethernet.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/if_dl.h>
52 
53 #include <machine/pio.h>
54 #include <machine/bus.h>
55 #include <machine/platform.h>
56 #include <machine/resource.h>
57 #include <sys/bus.h>
58 #include <sys/rman.h>
59 
60 #include "ps3bus.h"
61 #include "ps3-hvcall.h"
62 #include "if_glcreg.h"
63 
64 static int	glc_probe(device_t);
65 static int	glc_attach(device_t);
66 static void	glc_init(void *xsc);
67 static void	glc_start(struct ifnet *ifp);
68 static int	glc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
69 static void	glc_set_multicast(struct glc_softc *sc);
70 static int	glc_add_rxbuf(struct glc_softc *sc, int idx);
71 static int	glc_add_rxbuf_dma(struct glc_softc *sc, int idx);
72 static int	glc_encap(struct glc_softc *sc, struct mbuf **m_head,
73 		    bus_addr_t *pktdesc);
74 static int	glc_intr_filter(void *xsc);
75 static void	glc_intr(void *xsc);
76 static void	glc_tick(void *xsc);
77 static void	glc_media_status(struct ifnet *ifp, struct ifmediareq *ifmr);
78 static int	glc_media_change(struct ifnet *ifp);
79 
80 static MALLOC_DEFINE(M_GLC, "gelic", "PS3 GELIC ethernet");
81 
82 static device_method_t glc_methods[] = {
83 	/* Device interface */
84 	DEVMETHOD(device_probe,		glc_probe),
85 	DEVMETHOD(device_attach,	glc_attach),
86 
87 	{ 0, 0 }
88 };
89 
90 static driver_t glc_driver = {
91 	"glc",
92 	glc_methods,
93 	sizeof(struct glc_softc)
94 };
95 
96 static devclass_t glc_devclass;
97 
98 DRIVER_MODULE(glc, ps3bus, glc_driver, glc_devclass, 0, 0);
99 
100 static int
101 glc_probe(device_t dev)
102 {
103 
104 	if (ps3bus_get_bustype(dev) != PS3_BUSTYPE_SYSBUS ||
105 	    ps3bus_get_devtype(dev) != PS3_DEVTYPE_GELIC)
106 		return (ENXIO);
107 
108 	device_set_desc(dev, "Playstation 3 GELIC Network Controller");
109 	return (BUS_PROBE_SPECIFIC);
110 }
111 
112 static void
113 glc_getphys(void *xaddr, bus_dma_segment_t *segs, int nsegs, int error)
114 {
115 	if (error != 0)
116 		return;
117 
118 	*(bus_addr_t *)xaddr = segs[0].ds_addr;
119 }
120 
121 static int
122 glc_attach(device_t dev)
123 {
124 	struct glc_softc *sc;
125 	struct glc_txsoft *txs;
126 	uint64_t mac64, val, junk;
127 	int i, err;
128 
129 	sc = device_get_softc(dev);
130 
131 	sc->sc_bus = ps3bus_get_bus(dev);
132 	sc->sc_dev = ps3bus_get_device(dev);
133 	sc->sc_self = dev;
134 
135 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
136 	    MTX_DEF);
137 	callout_init_mtx(&sc->sc_tick_ch, &sc->sc_mtx, 0);
138 	sc->next_txdma_slot = 0;
139 	sc->bsy_txdma_slots = 0;
140 	sc->sc_next_rxdma_slot = 0;
141 	sc->first_used_txdma_slot = -1;
142 
143 	/*
144 	 * Shut down existing tasks.
145 	 */
146 
147 	lv1_net_stop_tx_dma(sc->sc_bus, sc->sc_dev, 0);
148 	lv1_net_stop_rx_dma(sc->sc_bus, sc->sc_dev, 0);
149 
150 	sc->sc_ifp = if_alloc(IFT_ETHER);
151 	sc->sc_ifp->if_softc = sc;
152 
153 	/*
154 	 * Get MAC address and VLAN id
155 	 */
156 
157 	lv1_net_control(sc->sc_bus, sc->sc_dev, GELIC_GET_MAC_ADDRESS,
158 	    0, 0, 0, &mac64, &junk);
159 	memcpy(sc->sc_enaddr, &((uint8_t *)&mac64)[2], sizeof(sc->sc_enaddr));
160 	sc->sc_tx_vlan = sc->sc_rx_vlan = -1;
161 	err = lv1_net_control(sc->sc_bus, sc->sc_dev, GELIC_GET_VLAN_ID,
162 	    GELIC_VLAN_TX_ETHERNET, 0, 0, &val, &junk);
163 	if (err == 0)
164 		sc->sc_tx_vlan = val;
165 	err = lv1_net_control(sc->sc_bus, sc->sc_dev, GELIC_GET_VLAN_ID,
166 	    GELIC_VLAN_RX_ETHERNET, 0, 0, &val, &junk);
167 	if (err == 0)
168 		sc->sc_rx_vlan = val;
169 
170 	/*
171 	 * Set up interrupt handler
172 	 */
173 	sc->sc_irqid = 0;
174 	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irqid,
175 	    RF_ACTIVE);
176 	if (sc->sc_irq == NULL) {
177 		device_printf(dev, "Could not allocate IRQ!\n");
178 		mtx_destroy(&sc->sc_mtx);
179 		return (ENXIO);
180 	}
181 
182 	bus_setup_intr(dev, sc->sc_irq,
183 	    INTR_TYPE_NET | INTR_MPSAFE | INTR_ENTROPY,
184 	    glc_intr_filter, glc_intr, sc, &sc->sc_irqctx);
185 	sc->sc_hwirq_status = (uint64_t *)contigmalloc(8, M_GLC, M_ZERO, 0,
186 	    BUS_SPACE_MAXADDR_32BIT, 8, PAGE_SIZE);
187 	lv1_net_set_interrupt_status_indicator(sc->sc_bus, sc->sc_dev,
188 	    vtophys(sc->sc_hwirq_status), 0);
189 	lv1_net_set_interrupt_mask(sc->sc_bus, sc->sc_dev,
190 	    GELIC_INT_RXDONE | GELIC_INT_RXFRAME | GELIC_INT_PHY |
191 	    GELIC_INT_TX_CHAIN_END, 0);
192 
193 	/*
194 	 * Set up DMA.
195 	 */
196 
197 	err = bus_dma_tag_create(bus_get_dma_tag(dev), 32, 0,
198 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
199 	    129*sizeof(struct glc_dmadesc), 1, 128*sizeof(struct glc_dmadesc),
200 	    0, NULL,NULL, &sc->sc_dmadesc_tag);
201 
202 	err = bus_dmamem_alloc(sc->sc_dmadesc_tag, (void **)&sc->sc_txdmadesc,
203 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
204 	    &sc->sc_txdmadesc_map);
205 	err = bus_dmamap_load(sc->sc_dmadesc_tag, sc->sc_txdmadesc_map,
206 	    sc->sc_txdmadesc, 128*sizeof(struct glc_dmadesc), glc_getphys,
207 	    &sc->sc_txdmadesc_phys, 0);
208 	err = bus_dmamem_alloc(sc->sc_dmadesc_tag, (void **)&sc->sc_rxdmadesc,
209 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
210 	    &sc->sc_rxdmadesc_map);
211 	err = bus_dmamap_load(sc->sc_dmadesc_tag, sc->sc_rxdmadesc_map,
212 	    sc->sc_rxdmadesc, 128*sizeof(struct glc_dmadesc), glc_getphys,
213 	    &sc->sc_rxdmadesc_phys, 0);
214 
215 	err = bus_dma_tag_create(bus_get_dma_tag(dev), 128, 0,
216 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
217 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, NULL,NULL,
218 	    &sc->sc_rxdma_tag);
219 	err = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
220 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
221 	    BUS_SPACE_MAXSIZE_32BIT, 16, BUS_SPACE_MAXSIZE_32BIT, 0, NULL,NULL,
222 	    &sc->sc_txdma_tag);
223 
224 	/* init transmit descriptors */
225 	STAILQ_INIT(&sc->sc_txfreeq);
226 	STAILQ_INIT(&sc->sc_txdirtyq);
227 
228 	/* create TX DMA maps */
229 	err = ENOMEM;
230 	for (i = 0; i < GLC_MAX_TX_PACKETS; i++) {
231 		txs = &sc->sc_txsoft[i];
232 		txs->txs_mbuf = NULL;
233 		err = bus_dmamap_create(sc->sc_txdma_tag, 0, &txs->txs_dmamap);
234 		if (err) {
235 			device_printf(dev,
236 			    "unable to create TX DMA map %d, error = %d\n",
237 			    i, err);
238 		}
239 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
240 	}
241 
242 	/* Create the receive buffer DMA maps. */
243 	for (i = 0; i < GLC_MAX_RX_PACKETS; i++) {
244 		err = bus_dmamap_create(sc->sc_rxdma_tag, 0,
245 		    &sc->sc_rxsoft[i].rxs_dmamap);
246 		if (err) {
247 			device_printf(dev,
248 			    "unable to create RX DMA map %d, error = %d\n",
249 			    i, err);
250 		}
251 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
252 	}
253 
254 	/*
255 	 * Attach to network stack
256 	 */
257 
258 	if_initname(sc->sc_ifp, device_get_name(dev), device_get_unit(dev));
259 	sc->sc_ifp->if_mtu = ETHERMTU;
260 	sc->sc_ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
261 	sc->sc_ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
262 	sc->sc_ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_RXCSUM;
263 	sc->sc_ifp->if_capenable = IFCAP_HWCSUM | IFCAP_RXCSUM;
264 	sc->sc_ifp->if_start = glc_start;
265 	sc->sc_ifp->if_ioctl = glc_ioctl;
266 	sc->sc_ifp->if_init = glc_init;
267 
268 	ifmedia_init(&sc->sc_media, IFM_IMASK, glc_media_change,
269 	    glc_media_status);
270 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10_T, 0, NULL);
271 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10_T | IFM_FDX, 0, NULL);
272 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_100_TX, 0, NULL);
273 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_100_TX | IFM_FDX, 0, NULL);
274 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
275 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
276 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
277 
278 	IFQ_SET_MAXLEN(&sc->sc_ifp->if_snd, GLC_MAX_TX_PACKETS);
279 	sc->sc_ifp->if_snd.ifq_drv_maxlen = GLC_MAX_TX_PACKETS;
280 	IFQ_SET_READY(&sc->sc_ifp->if_snd);
281 
282 	ether_ifattach(sc->sc_ifp, sc->sc_enaddr);
283 	sc->sc_ifp->if_hwassist = 0;
284 
285 	return (0);
286 
287 	mtx_destroy(&sc->sc_mtx);
288 	if_free(sc->sc_ifp);
289 	return (ENXIO);
290 }
291 
292 static void
293 glc_init_locked(struct glc_softc *sc)
294 {
295 	int i, error;
296 	struct glc_rxsoft *rxs;
297 	struct glc_txsoft *txs;
298 
299 	mtx_assert(&sc->sc_mtx, MA_OWNED);
300 
301 	lv1_net_stop_tx_dma(sc->sc_bus, sc->sc_dev, 0);
302 	lv1_net_stop_rx_dma(sc->sc_bus, sc->sc_dev, 0);
303 
304 	glc_set_multicast(sc);
305 
306 	for (i = 0; i < GLC_MAX_RX_PACKETS; i++) {
307 		rxs = &sc->sc_rxsoft[i];
308 		rxs->rxs_desc_slot = i;
309 
310 		if (rxs->rxs_mbuf == NULL) {
311 			glc_add_rxbuf(sc, i);
312 
313 			if (rxs->rxs_mbuf == NULL) {
314 				rxs->rxs_desc_slot = -1;
315 				break;
316 			}
317 		}
318 
319 		glc_add_rxbuf_dma(sc, i);
320 		bus_dmamap_sync(sc->sc_dmadesc_tag, sc->sc_rxdmadesc_map,
321 		    BUS_DMASYNC_PREREAD);
322 	}
323 
324 	/* Clear TX dirty queue */
325 	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
326 		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
327 		bus_dmamap_unload(sc->sc_txdma_tag, txs->txs_dmamap);
328 
329 		if (txs->txs_mbuf != NULL) {
330 			m_freem(txs->txs_mbuf);
331 			txs->txs_mbuf = NULL;
332 		}
333 
334 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
335 	}
336 	sc->first_used_txdma_slot = -1;
337 	sc->bsy_txdma_slots = 0;
338 
339 	error = lv1_net_start_rx_dma(sc->sc_bus, sc->sc_dev,
340 	    sc->sc_rxsoft[0].rxs_desc, 0);
341 	if (error != 0)
342 		device_printf(sc->sc_self,
343 		    "lv1_net_start_rx_dma error: %d\n", error);
344 
345 	sc->sc_ifp->if_drv_flags |= IFF_DRV_RUNNING;
346 	sc->sc_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
347 	sc->sc_ifpflags = sc->sc_ifp->if_flags;
348 
349 	sc->sc_wdog_timer = 0;
350 	callout_reset(&sc->sc_tick_ch, hz, glc_tick, sc);
351 }
352 
353 static void
354 glc_stop(void *xsc)
355 {
356 	struct glc_softc *sc = xsc;
357 
358 	mtx_assert(&sc->sc_mtx, MA_OWNED);
359 
360 	lv1_net_stop_tx_dma(sc->sc_bus, sc->sc_dev, 0);
361 	lv1_net_stop_rx_dma(sc->sc_bus, sc->sc_dev, 0);
362 }
363 
364 static void
365 glc_init(void *xsc)
366 {
367 	struct glc_softc *sc = xsc;
368 
369 	mtx_lock(&sc->sc_mtx);
370 	glc_init_locked(sc);
371 	mtx_unlock(&sc->sc_mtx);
372 }
373 
374 static void
375 glc_tick(void *xsc)
376 {
377 	struct glc_softc *sc = xsc;
378 
379 	mtx_assert(&sc->sc_mtx, MA_OWNED);
380 
381 	/*
382 	 * XXX: Sometimes the RX queue gets stuck. Poke it periodically until
383 	 * we figure out why. This will fail harmlessly if the RX queue is
384 	 * already running.
385 	 */
386 	lv1_net_start_rx_dma(sc->sc_bus, sc->sc_dev,
387 	    sc->sc_rxsoft[sc->sc_next_rxdma_slot].rxs_desc, 0);
388 
389 	if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0) {
390 		callout_reset(&sc->sc_tick_ch, hz, glc_tick, sc);
391 		return;
392 	}
393 
394 	/* Problems */
395 	device_printf(sc->sc_self, "device timeout\n");
396 
397 	glc_init_locked(sc);
398 }
399 
400 static void
401 glc_start_locked(struct ifnet *ifp)
402 {
403 	struct glc_softc *sc = ifp->if_softc;
404 	bus_addr_t first, pktdesc;
405 	int kickstart = 0;
406 	int error;
407 	struct mbuf *mb_head;
408 
409 	mtx_assert(&sc->sc_mtx, MA_OWNED);
410 	first = 0;
411 
412 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
413 	    IFF_DRV_RUNNING)
414 		return;
415 
416 	if (STAILQ_EMPTY(&sc->sc_txdirtyq))
417 		kickstart = 1;
418 
419 	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
420 		IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head);
421 
422 		if (mb_head == NULL)
423 			break;
424 
425 		/* Check if the ring buffer is full */
426 		if (sc->bsy_txdma_slots > 125) {
427 			/* Put the packet back and stop */
428 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
429 			IFQ_DRV_PREPEND(&ifp->if_snd, mb_head);
430 			break;
431 		}
432 
433 		BPF_MTAP(ifp, mb_head);
434 
435 		if (sc->sc_tx_vlan >= 0)
436 			mb_head = ether_vlanencap(mb_head, sc->sc_tx_vlan);
437 
438 		if (glc_encap(sc, &mb_head, &pktdesc)) {
439 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
440 			break;
441 		}
442 
443 		if (first == 0)
444 			first = pktdesc;
445 	}
446 
447 	if (kickstart && first != 0) {
448 		error = lv1_net_start_tx_dma(sc->sc_bus, sc->sc_dev, first, 0);
449 		if (error != 0)
450 			device_printf(sc->sc_self,
451 			    "lv1_net_start_tx_dma error: %d\n", error);
452 		sc->sc_wdog_timer = 5;
453 	}
454 }
455 
456 static void
457 glc_start(struct ifnet *ifp)
458 {
459 	struct glc_softc *sc = ifp->if_softc;
460 
461 	mtx_lock(&sc->sc_mtx);
462 	glc_start_locked(ifp);
463 	mtx_unlock(&sc->sc_mtx);
464 }
465 
466 static int
467 glc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
468 {
469 	struct glc_softc *sc = ifp->if_softc;
470 	struct ifreq *ifr = (struct ifreq *)data;
471 	int err = 0;
472 
473 	switch (cmd) {
474 	case SIOCSIFFLAGS:
475                 mtx_lock(&sc->sc_mtx);
476 		if ((ifp->if_flags & IFF_UP) != 0) {
477 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
478 			   ((ifp->if_flags ^ sc->sc_ifpflags) &
479 			    (IFF_ALLMULTI | IFF_PROMISC)) != 0)
480 				glc_set_multicast(sc);
481 			else
482 				glc_init_locked(sc);
483 		}
484 		else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
485 			glc_stop(sc);
486 		sc->sc_ifpflags = ifp->if_flags;
487 		mtx_unlock(&sc->sc_mtx);
488 		break;
489 	case SIOCADDMULTI:
490 	case SIOCDELMULTI:
491                 mtx_lock(&sc->sc_mtx);
492 		glc_set_multicast(sc);
493                 mtx_unlock(&sc->sc_mtx);
494 		break;
495 	case SIOCGIFMEDIA:
496 	case SIOCSIFMEDIA:
497 		err = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
498 		break;
499 	default:
500 		err = ether_ioctl(ifp, cmd, data);
501 		break;
502 	}
503 
504 	return (err);
505 }
506 
507 static u_int
508 glc_add_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
509 {
510 	struct glc_softc *sc = arg;
511 	uint64_t addr;
512 
513 	/*
514 	 * Filter can only hold 32 addresses, so fall back to
515 	 * the IFF_ALLMULTI case if we have too many. +1 is for
516 	 * broadcast.
517 	 */
518 	if (cnt + 1 == 32)
519 		return (0);
520 
521 	addr = 0;
522 	memcpy(&((uint8_t *)(&addr))[2], LLADDR(sdl), ETHER_ADDR_LEN);
523 	lv1_net_add_multicast_address(sc->sc_bus, sc->sc_dev, addr, 0);
524 
525 	return (1);
526 }
527 
528 static void
529 glc_set_multicast(struct glc_softc *sc)
530 {
531 	struct ifnet *ifp = sc->sc_ifp;
532 	int naddrs;
533 
534 	/* Clear multicast filter */
535 	lv1_net_remove_multicast_address(sc->sc_bus, sc->sc_dev, 0, 1);
536 
537 	/* Add broadcast */
538 	lv1_net_add_multicast_address(sc->sc_bus, sc->sc_dev,
539 	    0xffffffffffffL, 0);
540 
541 	if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
542 		lv1_net_add_multicast_address(sc->sc_bus, sc->sc_dev, 0, 1);
543 	} else {
544 		naddrs = if_foreach_llmaddr(ifp, glc_add_maddr, sc);
545 		if (naddrs + 1 == 32)
546 			lv1_net_add_multicast_address(sc->sc_bus,
547 			    sc->sc_dev, 0, 1);
548 	}
549 }
550 
551 static int
552 glc_add_rxbuf(struct glc_softc *sc, int idx)
553 {
554 	struct glc_rxsoft *rxs = &sc->sc_rxsoft[idx];
555 	struct mbuf *m;
556 	bus_dma_segment_t segs[1];
557 	int error, nsegs;
558 
559 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
560 	if (m == NULL)
561 		return (ENOBUFS);
562 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
563 
564 	if (rxs->rxs_mbuf != NULL) {
565 		bus_dmamap_sync(sc->sc_rxdma_tag, rxs->rxs_dmamap,
566 		    BUS_DMASYNC_POSTREAD);
567 		bus_dmamap_unload(sc->sc_rxdma_tag, rxs->rxs_dmamap);
568 	}
569 
570 	error = bus_dmamap_load_mbuf_sg(sc->sc_rxdma_tag, rxs->rxs_dmamap, m,
571 	    segs, &nsegs, BUS_DMA_NOWAIT);
572 	if (error != 0) {
573 		device_printf(sc->sc_self,
574 		    "cannot load RS DMA map %d, error = %d\n", idx, error);
575 		m_freem(m);
576 		return (error);
577 	}
578 	/* If nsegs is wrong then the stack is corrupt. */
579 	KASSERT(nsegs == 1,
580 	    ("%s: too many DMA segments (%d)", __func__, nsegs));
581 	rxs->rxs_mbuf = m;
582 	rxs->segment = segs[0];
583 
584 	bus_dmamap_sync(sc->sc_rxdma_tag, rxs->rxs_dmamap, BUS_DMASYNC_PREREAD);
585 
586 	return (0);
587 }
588 
589 static int
590 glc_add_rxbuf_dma(struct glc_softc *sc, int idx)
591 {
592 	struct glc_rxsoft *rxs = &sc->sc_rxsoft[idx];
593 
594 	bzero(&sc->sc_rxdmadesc[idx], sizeof(sc->sc_rxdmadesc[idx]));
595 	sc->sc_rxdmadesc[idx].paddr = rxs->segment.ds_addr;
596 	sc->sc_rxdmadesc[idx].len = rxs->segment.ds_len;
597 	sc->sc_rxdmadesc[idx].next = sc->sc_rxdmadesc_phys +
598 	    ((idx + 1) % GLC_MAX_RX_PACKETS)*sizeof(sc->sc_rxdmadesc[idx]);
599 	sc->sc_rxdmadesc[idx].cmd_stat = GELIC_DESCR_OWNED;
600 
601 	rxs->rxs_desc_slot = idx;
602 	rxs->rxs_desc = sc->sc_rxdmadesc_phys + idx*sizeof(struct glc_dmadesc);
603 
604         return (0);
605 }
606 
607 static int
608 glc_encap(struct glc_softc *sc, struct mbuf **m_head, bus_addr_t *pktdesc)
609 {
610 	bus_dma_segment_t segs[16];
611 	struct glc_txsoft *txs;
612 	struct mbuf *m;
613 	bus_addr_t firstslotphys;
614 	int i, idx, nsegs, nsegs_max;
615 	int err = 0;
616 
617 	/* Max number of segments is the number of free DMA slots */
618 	nsegs_max = 128 - sc->bsy_txdma_slots;
619 
620 	if (nsegs_max > 16 || sc->first_used_txdma_slot < 0)
621 		nsegs_max = 16;
622 
623 	/* Get a work queue entry. */
624 	if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) {
625 		/* Ran out of descriptors. */
626 		return (ENOBUFS);
627 	}
628 
629 	nsegs = 0;
630 	for (m = *m_head; m != NULL; m = m->m_next)
631 		nsegs++;
632 
633 	if (nsegs > nsegs_max) {
634 		m = m_collapse(*m_head, M_NOWAIT, nsegs_max);
635 		if (m == NULL) {
636 			m_freem(*m_head);
637 			*m_head = NULL;
638 			return (ENOBUFS);
639 		}
640 		*m_head = m;
641 	}
642 
643 	err = bus_dmamap_load_mbuf_sg(sc->sc_txdma_tag, txs->txs_dmamap,
644 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
645 	if (err != 0) {
646 		m_freem(*m_head);
647 		*m_head = NULL;
648 		return (err);
649 	}
650 
651 	KASSERT(nsegs <= 128 - sc->bsy_txdma_slots,
652 	    ("GLC: Mapped too many (%d) DMA segments with %d available",
653 	    nsegs, 128 - sc->bsy_txdma_slots));
654 
655 	if (nsegs == 0) {
656 		m_freem(*m_head);
657 		*m_head = NULL;
658 		return (EIO);
659 	}
660 
661 	txs->txs_ndescs = nsegs;
662 	txs->txs_firstdesc = sc->next_txdma_slot;
663 
664 	idx = txs->txs_firstdesc;
665 	firstslotphys = sc->sc_txdmadesc_phys +
666 	    txs->txs_firstdesc*sizeof(struct glc_dmadesc);
667 
668 	for (i = 0; i < nsegs; i++) {
669 		bzero(&sc->sc_txdmadesc[idx], sizeof(sc->sc_txdmadesc[idx]));
670 		sc->sc_txdmadesc[idx].paddr = segs[i].ds_addr;
671 		sc->sc_txdmadesc[idx].len = segs[i].ds_len;
672 		sc->sc_txdmadesc[idx].next = sc->sc_txdmadesc_phys +
673 		    ((idx + 1) % GLC_MAX_TX_PACKETS)*sizeof(struct glc_dmadesc);
674 		sc->sc_txdmadesc[idx].cmd_stat |= GELIC_CMDSTAT_NOIPSEC;
675 
676 		if (i+1 == nsegs) {
677 			txs->txs_lastdesc = idx;
678 			sc->sc_txdmadesc[idx].next = 0;
679 			sc->sc_txdmadesc[idx].cmd_stat |= GELIC_CMDSTAT_LAST;
680 		}
681 
682 		if ((*m_head)->m_pkthdr.csum_flags & CSUM_TCP)
683 			sc->sc_txdmadesc[idx].cmd_stat |= GELIC_CMDSTAT_CSUM_TCP;
684 		if ((*m_head)->m_pkthdr.csum_flags & CSUM_UDP)
685 			sc->sc_txdmadesc[idx].cmd_stat |= GELIC_CMDSTAT_CSUM_UDP;
686 		sc->sc_txdmadesc[idx].cmd_stat |= GELIC_DESCR_OWNED;
687 
688 		idx = (idx + 1) % GLC_MAX_TX_PACKETS;
689 	}
690 	sc->next_txdma_slot = idx;
691 	sc->bsy_txdma_slots += nsegs;
692 	if (txs->txs_firstdesc != 0)
693 		idx = txs->txs_firstdesc - 1;
694 	else
695 		idx = GLC_MAX_TX_PACKETS - 1;
696 
697 	if (sc->first_used_txdma_slot < 0)
698 		sc->first_used_txdma_slot = txs->txs_firstdesc;
699 
700 	bus_dmamap_sync(sc->sc_txdma_tag, txs->txs_dmamap,
701 	    BUS_DMASYNC_PREWRITE);
702 	sc->sc_txdmadesc[idx].next = firstslotphys;
703 
704 	STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
705 	STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
706 	txs->txs_mbuf = *m_head;
707 	*pktdesc = firstslotphys;
708 
709 	return (0);
710 }
711 
712 static void
713 glc_rxintr(struct glc_softc *sc)
714 {
715 	int i, restart_rxdma, error;
716 	struct mbuf *m;
717 	struct ifnet *ifp = sc->sc_ifp;
718 
719 	bus_dmamap_sync(sc->sc_dmadesc_tag, sc->sc_rxdmadesc_map,
720 	    BUS_DMASYNC_POSTREAD);
721 
722 	restart_rxdma = 0;
723 	while ((sc->sc_rxdmadesc[sc->sc_next_rxdma_slot].cmd_stat &
724 	   GELIC_DESCR_OWNED) == 0) {
725 		i = sc->sc_next_rxdma_slot;
726 		sc->sc_next_rxdma_slot++;
727 		if (sc->sc_next_rxdma_slot >= GLC_MAX_RX_PACKETS)
728 			sc->sc_next_rxdma_slot = 0;
729 
730 		if (sc->sc_rxdmadesc[i].cmd_stat & GELIC_CMDSTAT_CHAIN_END)
731 			restart_rxdma = 1;
732 
733 		if (sc->sc_rxdmadesc[i].rxerror & GELIC_RXERRORS) {
734 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
735 			goto requeue;
736 		}
737 
738 		m = sc->sc_rxsoft[i].rxs_mbuf;
739 		if (sc->sc_rxdmadesc[i].data_stat & GELIC_RX_IPCSUM) {
740 			m->m_pkthdr.csum_flags |=
741 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
742 		}
743 		if (sc->sc_rxdmadesc[i].data_stat & GELIC_RX_TCPUDPCSUM) {
744 			m->m_pkthdr.csum_flags |=
745 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
746 			m->m_pkthdr.csum_data = 0xffff;
747 		}
748 
749 		if (glc_add_rxbuf(sc, i)) {
750 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
751 			goto requeue;
752 		}
753 
754 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
755 		m->m_pkthdr.rcvif = ifp;
756 		m->m_len = sc->sc_rxdmadesc[i].valid_size;
757 		m->m_pkthdr.len = m->m_len;
758 
759 		/*
760 		 * Remove VLAN tag. Even on early firmwares that do not allow
761 		 * multiple VLANs, the VLAN tag is still in place here.
762 		 */
763 		m_adj(m, 2);
764 
765 		mtx_unlock(&sc->sc_mtx);
766 		(*ifp->if_input)(ifp, m);
767 		mtx_lock(&sc->sc_mtx);
768 
769 	    requeue:
770 		glc_add_rxbuf_dma(sc, i);
771 	}
772 
773 	bus_dmamap_sync(sc->sc_dmadesc_tag, sc->sc_rxdmadesc_map,
774 	    BUS_DMASYNC_PREWRITE);
775 
776 	if (restart_rxdma) {
777 		error = lv1_net_start_rx_dma(sc->sc_bus, sc->sc_dev,
778 		    sc->sc_rxsoft[sc->sc_next_rxdma_slot].rxs_desc, 0);
779 		if (error != 0)
780 			device_printf(sc->sc_self,
781 			    "lv1_net_start_rx_dma error: %d\n", error);
782 	}
783 }
784 
785 static void
786 glc_txintr(struct glc_softc *sc)
787 {
788 	struct ifnet *ifp = sc->sc_ifp;
789 	struct glc_txsoft *txs;
790 	int progress = 0, kickstart = 0, error;
791 
792 	bus_dmamap_sync(sc->sc_dmadesc_tag, sc->sc_txdmadesc_map,
793 	    BUS_DMASYNC_POSTREAD);
794 
795 	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
796 		if (sc->sc_txdmadesc[txs->txs_lastdesc].cmd_stat
797 		    & GELIC_DESCR_OWNED)
798 			break;
799 
800 		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
801 		bus_dmamap_unload(sc->sc_txdma_tag, txs->txs_dmamap);
802 		sc->bsy_txdma_slots -= txs->txs_ndescs;
803 
804 		if (txs->txs_mbuf != NULL) {
805 			m_freem(txs->txs_mbuf);
806 			txs->txs_mbuf = NULL;
807 		}
808 
809 		if ((sc->sc_txdmadesc[txs->txs_lastdesc].cmd_stat & 0xf0000000)
810 		    != 0) {
811 			lv1_net_stop_tx_dma(sc->sc_bus, sc->sc_dev, 0);
812 			kickstart = 1;
813 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
814 		}
815 
816 		if (sc->sc_txdmadesc[txs->txs_lastdesc].cmd_stat &
817 		    GELIC_CMDSTAT_CHAIN_END)
818 			kickstart = 1;
819 
820 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
821 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
822 		progress = 1;
823 	}
824 
825 	if (txs != NULL)
826 		sc->first_used_txdma_slot = txs->txs_firstdesc;
827 	else
828 		sc->first_used_txdma_slot = -1;
829 
830 	if (kickstart || txs != NULL) {
831 		/* Speculatively (or necessarily) start the TX queue again */
832 		error = lv1_net_start_tx_dma(sc->sc_bus, sc->sc_dev,
833 		    sc->sc_txdmadesc_phys +
834 		    ((txs == NULL) ? 0 : txs->txs_firstdesc)*
835 		     sizeof(struct glc_dmadesc), 0);
836 		if (error != 0)
837 			device_printf(sc->sc_self,
838 			    "lv1_net_start_tx_dma error: %d\n", error);
839 	}
840 
841 	if (progress) {
842 		/*
843 		 * We freed some descriptors, so reset IFF_DRV_OACTIVE
844 		 * and restart.
845 		 */
846 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
847 		sc->sc_wdog_timer = STAILQ_EMPTY(&sc->sc_txdirtyq) ? 0 : 5;
848 
849 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) &&
850 		    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
851 			glc_start_locked(ifp);
852 	}
853 }
854 
855 static int
856 glc_intr_filter(void *xsc)
857 {
858 	struct glc_softc *sc = xsc;
859 
860 	powerpc_sync();
861 	atomic_set_64(&sc->sc_interrupt_status, *sc->sc_hwirq_status);
862 	return (FILTER_SCHEDULE_THREAD);
863 }
864 
865 static void
866 glc_intr(void *xsc)
867 {
868 	struct glc_softc *sc = xsc;
869 	uint64_t status, linkstat, junk;
870 
871 	mtx_lock(&sc->sc_mtx);
872 
873 	status = atomic_readandclear_64(&sc->sc_interrupt_status);
874 
875 	if (status == 0) {
876 		mtx_unlock(&sc->sc_mtx);
877 		return;
878 	}
879 
880 	if (status & (GELIC_INT_RXDONE | GELIC_INT_RXFRAME))
881 		glc_rxintr(sc);
882 
883 	if (status & (GELIC_INT_TXDONE | GELIC_INT_TX_CHAIN_END))
884 		glc_txintr(sc);
885 
886 	if (status & GELIC_INT_PHY) {
887 		lv1_net_control(sc->sc_bus, sc->sc_dev, GELIC_GET_LINK_STATUS,
888 		    GELIC_VLAN_TX_ETHERNET, 0, 0, &linkstat, &junk);
889 
890 		linkstat = (linkstat & GELIC_LINK_UP) ?
891 		    LINK_STATE_UP : LINK_STATE_DOWN;
892 		if (linkstat != sc->sc_ifp->if_link_state)
893 			if_link_state_change(sc->sc_ifp, linkstat);
894 	}
895 
896 	mtx_unlock(&sc->sc_mtx);
897 }
898 
899 static void
900 glc_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
901 {
902 	struct glc_softc *sc = ifp->if_softc;
903 	uint64_t status, junk;
904 
905 	ifmr->ifm_status = IFM_AVALID;
906 	ifmr->ifm_active = IFM_ETHER;
907 
908 	lv1_net_control(sc->sc_bus, sc->sc_dev, GELIC_GET_LINK_STATUS,
909 	    GELIC_VLAN_TX_ETHERNET, 0, 0, &status, &junk);
910 
911 	if (status & GELIC_LINK_UP)
912 		ifmr->ifm_status |= IFM_ACTIVE;
913 
914 	if (status & GELIC_SPEED_10)
915 		ifmr->ifm_active |= IFM_10_T;
916 	else if (status & GELIC_SPEED_100)
917 		ifmr->ifm_active |= IFM_100_TX;
918 	else if (status & GELIC_SPEED_1000)
919 		ifmr->ifm_active |= IFM_1000_T;
920 
921 	if (status & GELIC_FULL_DUPLEX)
922 		ifmr->ifm_active |= IFM_FDX;
923 	else
924 		ifmr->ifm_active |= IFM_HDX;
925 }
926 
927 static int
928 glc_media_change(struct ifnet *ifp)
929 {
930 	struct glc_softc *sc = ifp->if_softc;
931 	uint64_t mode, junk;
932 	int result;
933 
934 	if (IFM_TYPE(sc->sc_media.ifm_media) != IFM_ETHER)
935 		return (EINVAL);
936 
937 	switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
938 	case IFM_AUTO:
939 		mode = GELIC_AUTO_NEG;
940 		break;
941 	case IFM_10_T:
942 		mode = GELIC_SPEED_10;
943 		break;
944 	case IFM_100_TX:
945 		mode = GELIC_SPEED_100;
946 		break;
947 	case IFM_1000_T:
948 		mode = GELIC_SPEED_1000 | GELIC_FULL_DUPLEX;
949 		break;
950 	default:
951 		return (EINVAL);
952 	}
953 
954 	if (IFM_OPTIONS(sc->sc_media.ifm_media) & IFM_FDX)
955 		mode |= GELIC_FULL_DUPLEX;
956 
957 	result = lv1_net_control(sc->sc_bus, sc->sc_dev, GELIC_SET_LINK_MODE,
958 	    GELIC_VLAN_TX_ETHERNET, mode, 0, &junk, &junk);
959 
960 	return (result ? EIO : 0);
961 }
962 
963