2 * Copyright (c) 2000-2001 Benno Rice
3 * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/if_ether.h>
39 #include <netinet/ip.h>
45 #include "api_public.h"
49 static int net_probe(struct netif *, void *);
50 static int net_match(struct netif *, void *);
51 static void net_init(struct iodesc *, void *);
52 static int net_get(struct iodesc *, void *, size_t, time_t);
53 static int net_put(struct iodesc *, void *, size_t);
54 static void net_end(struct netif *);
56 extern struct netif_stats net_stats[];
58 struct netif_dif net_ifs[] = {
59 /* dif_unit dif_nsel dif_stats dif_private */
60 { 0, 1, &net_stats[0], 0, },
63 struct netif_stats net_stats[NENTS(net_ifs)];
65 struct netif_driver uboot_net = {
66 "uboot_eth", /* netif_bname */
67 net_match, /* netif_match */
68 net_probe, /* netif_probe */
69 net_init, /* netif_init */
70 net_get, /* netif_get */
71 net_put, /* netif_put */
72 net_end, /* netif_end */
73 net_ifs, /* netif_ifs */
74 NENTS(net_ifs) /* netif_nifs */
79 uint8_t sc_rxbuf[ETHER_MAX_LEN];
80 uint8_t sc_txbuf[ETHER_MAX_LEN + PKTALIGN];
82 int sc_handle; /* device handle for ub_dev_xxx */
85 static struct uboot_softc uboot_softc;
88 net_match(struct netif *nif, void *machdep_hint)
90 char **a = (char **)machdep_hint;
92 if (memcmp("net", *a, 3) == 0)
95 printf("net_match: could not match network device\n");
100 net_probe(struct netif *nif, void *machdep_hint)
102 struct device_info *di;
105 for (i = 0; i < devs_no; i++)
106 if ((di = ub_dev_get(i)) != NULL)
107 if (di->type == DEV_TYP_NET)
111 printf("net_probe: no network devices found, maybe not"
112 " enumerated yet..?\n");
116 #if defined(NETIF_DEBUG)
117 printf("net_probe: network device found: %d\n", i);
119 uboot_softc.sc_handle = i;
125 net_put(struct iodesc *desc, void *pkt, size_t len)
127 struct netif *nif = desc->io_netif;
128 struct uboot_softc *sc = nif->nif_devdata;
132 #if defined(NETIF_DEBUG)
133 struct ether_header *eh;
135 printf("net_put: desc %p, pkt %p, len %d\n", desc, pkt, len);
137 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
138 printf("src: %s ", ether_sprintf(eh->ether_shost));
139 printf("type: 0x%x\n", eh->ether_type & 0xffff);
142 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
143 sendlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
144 bzero(sc->sc_txbufp, sendlen);
148 memcpy(sc->sc_txbufp, pkt, len);
150 rv = ub_dev_send(sc->sc_handle, sc->sc_txbufp, sendlen);
152 #if defined(NETIF_DEBUG)
153 printf("net_put: ub_send returned %d\n", rv);
164 net_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
166 struct netif *nif = desc->io_netif;
167 struct uboot_softc *sc = nif->nif_devdata;
171 #if defined(NETIF_DEBUG)
172 printf("net_get: pkt %p, len %d, timeout %d\n", pkt, len, timeout);
176 err = ub_dev_recv(sc->sc_handle, sc->sc_rxbuf, len, &rlen);
179 printf("net_get: ub_dev_recv() failed, error=%d\n",
184 } while ((rlen == -1 || rlen == 0) && (getsecs() - t < timeout));
186 #if defined(NETIF_DEBUG)
187 printf("net_get: received len %d (%x)\n", rlen, rlen);
191 memcpy(pkt, sc->sc_rxbuf, MIN(len, rlen));
193 #if defined(NETIF_DEBUG)
194 printf("net_get: len %x, rlen %x\n", len, rlen);
204 net_init(struct iodesc *desc, void *machdep_hint)
206 struct netif *nif = desc->io_netif;
207 struct uboot_softc *sc;
208 struct device_info *di;
211 sc = nif->nif_devdata = &uboot_softc;
213 if ((err = ub_dev_open(sc->sc_handle)) != 0)
214 panic("%s%d: initialisation failed with error %d\n",
215 nif->nif_driver->netif_bname, nif->nif_unit, err);
217 /* Get MAC address */
218 di = ub_dev_get(sc->sc_handle);
219 memcpy(desc->myea, di->di_net.hwaddr, 6);
220 if (memcmp (desc->myea, "\0\0\0\0\0\0", 6) == 0) {
221 panic("%s%d: empty ethernet address!",
222 nif->nif_driver->netif_bname, nif->nif_unit);
225 #if defined(NETIF_DEBUG)
226 printf("network: %s%d attached to %s\n", nif->nif_driver->netif_bname,
227 nif->nif_unit, ether_sprintf(desc->myea));
230 /* Set correct alignment for TX packets */
231 sc->sc_txbufp = sc->sc_txbuf;
232 if ((unsigned long)sc->sc_txbufp % PKTALIGN)
233 sc->sc_txbufp += PKTALIGN -
234 (unsigned long)sc->sc_txbufp % PKTALIGN;
238 net_end(struct netif *nif)
240 struct uboot_softc *sc = nif->nif_devdata;
243 if ((err = ub_dev_close(sc->sc_handle)) != 0)
244 panic("%s%d: net_end failed with error %d\n",
245 nif->nif_driver->netif_bname, nif->nif_unit, err);