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 #define NETIF_VERBOSE_DEBUG
51 #undef NETIF_VERBOSE_DEBUG
54 static int net_probe(struct netif *, void *);
55 static int net_match(struct netif *, void *);
56 static void net_init(struct iodesc *, void *);
57 static int net_get(struct iodesc *, void *, size_t, time_t);
58 static int net_put(struct iodesc *, void *, size_t);
59 static void net_end(struct netif *);
61 struct device_info * ub_dev_get(int i);
64 extern struct netif_stats net_stats[];
66 struct netif_dif net_ifs[] = {
67 /* dif_unit dif_nsel dif_stats dif_private */
68 { 0, 1, &net_stats[0], 0, },
71 struct netif_stats net_stats[NENTS(net_ifs)];
73 struct netif_driver uboot_net = {
74 "uboot_eth", /* netif_bname */
75 net_match, /* netif_match */
76 net_probe, /* netif_probe */
77 net_init, /* netif_init */
78 net_get, /* netif_get */
79 net_put, /* netif_put */
80 net_end, /* netif_end */
81 net_ifs, /* netif_ifs */
82 NENTS(net_ifs) /* netif_nifs */
87 u_int8_t sc_rxbuf[ETHER_MAX_LEN];
88 u_int8_t sc_txbuf[ETHER_MAX_LEN + PKTALIGN];
90 int sc_handle; /* device handle for ub_dev_xxx */
93 static struct uboot_softc uboot_softc;
96 net_match(struct netif *nif, void *machdep_hint)
98 char **a = (char **)machdep_hint;
100 if (memcmp("net", *a, 3) == 0)
103 printf("net_match: could not match network device\n");
108 net_probe(struct netif *nif, void *machdep_hint)
110 struct device_info *di;
113 for (i = 0; i < devs_no; i++)
114 if (di = ub_dev_get(i))
115 if (di->type == DEV_TYP_NET)
118 printf("net_probe: no network devices found, maybe not"
119 " enumerated yet..?\n");
123 #if defined(NETIF_DEBUG)
124 printf("net_probe: network device found: %d\n", i);
126 uboot_softc.sc_handle = i;
132 net_put(struct iodesc *desc, void *pkt, size_t len)
134 struct netif *nif = desc->io_netif;
135 struct uboot_softc *sc = nif->nif_devdata;
137 struct ether_header *eh;
141 #if defined(NETIF_DEBUG)
142 printf("net_put: desc 0x%x, pkt 0x%x, len %d\n", desc, pkt, len);
144 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
145 printf("src: %s ", ether_sprintf(eh->ether_shost));
146 printf("type: 0x%x\n", eh->ether_type & 0xffff);
149 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
150 sendlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
151 bzero(sc->sc_txbufp, sendlen);
155 memcpy(sc->sc_txbufp, pkt, len);
157 rv = ub_dev_send(sc->sc_handle, sc->sc_txbufp, sendlen);
159 #if defined(NETIF_DEBUG)
160 printf("net_put: ub_send returned %d\n", rv);
171 net_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
173 struct netif *nif = desc->io_netif;
174 struct uboot_softc *sc = nif->nif_devdata;
178 #if defined(NETIF_DEBUG)
179 printf("net_get: pkt %x, len %d, timeout %d\n", pkt, len, timeout);
183 length = ub_dev_recv(sc->sc_handle, sc->sc_rxbuf, len);
184 } while ((length == -1 || length == 0) &&
185 (getsecs() - t < timeout));
187 #if defined(NETIF_DEBUG)
188 printf("net_get: received len %d (%x)\n", length, length);
192 memcpy(pkt, sc->sc_rxbuf, MIN(len, length));
194 #if defined(NETIF_DEBUG)
195 printf("net_get: len %x, length %x\n", len, length);
206 net_init(struct iodesc *desc, void *machdep_hint)
208 struct netif *nif = desc->io_netif;
209 struct uboot_softc *sc;
210 struct device_info *di;
213 sc = nif->nif_devdata = &uboot_softc;
215 if (err = ub_dev_open(sc->sc_handle))
216 panic("%s%d: initialisation failed with error %d\n",
217 nif->nif_driver->netif_bname, nif->nif_unit, err);
219 /* Get MAC address */
220 di = ub_dev_get(sc->sc_handle);
221 memcpy(desc->myea, di->di_net.hwaddr, 6);
222 if (memcmp (desc->myea, "\0\0\0\0\0\0", 6) == 0) {
223 panic("%s%d: empty ethernet address!",
224 nif->nif_driver->netif_bname, nif->nif_unit);
227 #if defined(NETIF_DEBUG)
228 printf("network: %s%d attached to %s\n", nif->nif_driver->netif_bname,
229 nif->nif_unit, ether_sprintf(desc->myea));
232 /* Set correct alignment for TX packets */
233 sc->sc_txbufp = sc->sc_txbuf;
234 if ((unsigned long)sc->sc_txbufp % PKTALIGN)
235 sc->sc_txbufp += PKTALIGN -
236 (unsigned long)sc->sc_txbufp % PKTALIGN;
241 net_end(struct netif *nif)
243 struct uboot_softc *sc = nif->nif_devdata;
246 if (err = ub_dev_close(sc->sc_handle))
247 panic("%s%d: net_end failed with error %d\n",
248 nif->nif_driver->netif_bname, nif->nif_unit, err);