net.c revision 183599
1/*- 2 * Copyright (c) 2000-2001 Benno Rice 3 * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/boot/uboot/lib/net.c 183599 2008-10-04 13:19:15Z raj $"); 30 31#include <sys/param.h> 32#include <sys/types.h> 33#include <sys/socket.h> 34 35#include <net/if.h> 36#include <netinet/in.h> 37#include <netinet/in_systm.h> 38#include <netinet/if_ether.h> 39#include <netinet/ip.h> 40 41#include <stand.h> 42#include <net.h> 43#include <netif.h> 44 45#include "api_public.h" 46#include "glue.h" 47#include "libuboot.h" 48 49#define NETIF_DEBUG 50#define NETIF_VERBOSE_DEBUG 51#undef NETIF_DEBUG 52#undef NETIF_VERBOSE_DEBUG 53 54 55static int net_probe(struct netif *, void *); 56static int net_match(struct netif *, void *); 57static void net_init(struct iodesc *, void *); 58static int net_get(struct iodesc *, void *, size_t, time_t); 59static int net_put(struct iodesc *, void *, size_t); 60static void net_end(struct netif *); 61 62extern int devs_no; 63extern struct netif_stats net_stats[]; 64 65struct netif_dif net_ifs[] = { 66 /* dif_unit dif_nsel dif_stats dif_private */ 67 { 0, 1, &net_stats[0], 0, }, 68}; 69 70struct netif_stats net_stats[NENTS(net_ifs)]; 71 72struct netif_driver uboot_net = { 73 "uboot_eth", /* netif_bname */ 74 net_match, /* netif_match */ 75 net_probe, /* netif_probe */ 76 net_init, /* netif_init */ 77 net_get, /* netif_get */ 78 net_put, /* netif_put */ 79 net_end, /* netif_end */ 80 net_ifs, /* netif_ifs */ 81 NENTS(net_ifs) /* netif_nifs */ 82}; 83 84struct uboot_softc { 85 uint32_t sc_pad; 86 uint8_t sc_rxbuf[ETHER_MAX_LEN]; 87 uint8_t sc_txbuf[ETHER_MAX_LEN + PKTALIGN]; 88 uint8_t *sc_txbufp; 89 int sc_handle; /* device handle for ub_dev_xxx */ 90}; 91 92static struct uboot_softc uboot_softc; 93 94static int 95net_match(struct netif *nif, void *machdep_hint) 96{ 97 char **a = (char **)machdep_hint; 98 99 if (memcmp("net", *a, 3) == 0) 100 return (1); 101 102 printf("net_match: could not match network device\n"); 103 return (0); 104} 105 106static int 107net_probe(struct netif *nif, void *machdep_hint) 108{ 109 struct device_info *di; 110 int i; 111 112 for (i = 0; i < devs_no; i++) 113 if ((di = ub_dev_get(i)) != NULL) 114 if (di->type == DEV_TYP_NET) 115 break; 116 117 if (i == devs_no) { 118 printf("net_probe: no network devices found, maybe not" 119 " enumerated yet..?\n"); 120 return (-1); 121 } 122 123#if defined(NETIF_DEBUG) 124 printf("net_probe: network device found: %d\n", i); 125#endif 126 uboot_softc.sc_handle = i; 127 128 return (0); 129} 130 131static int 132net_put(struct iodesc *desc, void *pkt, size_t len) 133{ 134 struct netif *nif = desc->io_netif; 135 struct uboot_softc *sc = nif->nif_devdata; 136 size_t sendlen; 137 ssize_t rv; 138 139#if defined(NETIF_DEBUG) 140 struct ether_header *eh; 141 142 printf("net_put: desc 0x%x, pkt 0x%x, len %d\n", desc, pkt, len); 143 eh = pkt; 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); 147#endif 148 149 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) { 150 sendlen = ETHER_MIN_LEN - ETHER_CRC_LEN; 151 bzero(sc->sc_txbufp, sendlen); 152 } else 153 sendlen = len; 154 155 memcpy(sc->sc_txbufp, pkt, len); 156 157 rv = ub_dev_send(sc->sc_handle, sc->sc_txbufp, sendlen); 158 159#if defined(NETIF_DEBUG) 160 printf("net_put: ub_send returned %d\n", rv); 161#endif 162 if (rv == 0) 163 rv = len; 164 else 165 rv = -1; 166 167 return (rv); 168} 169 170static int 171net_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout) 172{ 173 struct netif *nif = desc->io_netif; 174 struct uboot_softc *sc = nif->nif_devdata; 175 time_t t; 176 int err, rlen; 177 178#if defined(NETIF_DEBUG) 179 printf("net_get: pkt %x, len %d, timeout %d\n", pkt, len, timeout); 180#endif 181 t = getsecs(); 182 do { 183 err = ub_dev_recv(sc->sc_handle, sc->sc_rxbuf, len, &rlen); 184 185 if (err != 0) { 186 printf("net_get: ub_dev_recv() failed, error=%d\n", 187 err); 188 rlen = 0; 189 break; 190 } 191 } while ((rlen == -1 || rlen == 0) && (getsecs() - t < timeout)); 192 193#if defined(NETIF_DEBUG) 194 printf("net_get: received len %d (%x)\n", rlen, rlen); 195#endif 196 197 if (rlen > 0) { 198 memcpy(pkt, sc->sc_rxbuf, MIN(len, rlen)); 199 if (rlen != len) { 200#if defined(NETIF_DEBUG) 201 printf("net_get: len %x, rlen %x\n", len, rlen); 202#endif 203 } 204 return (rlen); 205 } 206 207 return (-1); 208} 209 210static void 211net_init(struct iodesc *desc, void *machdep_hint) 212{ 213 struct netif *nif = desc->io_netif; 214 struct uboot_softc *sc; 215 struct device_info *di; 216 int err; 217 218 sc = nif->nif_devdata = &uboot_softc; 219 220 if ((err = ub_dev_open(sc->sc_handle)) != 0) 221 panic("%s%d: initialisation failed with error %d\n", 222 nif->nif_driver->netif_bname, nif->nif_unit, err); 223 224 /* Get MAC address */ 225 di = ub_dev_get(sc->sc_handle); 226 memcpy(desc->myea, di->di_net.hwaddr, 6); 227 if (memcmp (desc->myea, "\0\0\0\0\0\0", 6) == 0) { 228 panic("%s%d: empty ethernet address!", 229 nif->nif_driver->netif_bname, nif->nif_unit); 230 } 231 232#if defined(NETIF_DEBUG) 233 printf("network: %s%d attached to %s\n", nif->nif_driver->netif_bname, 234 nif->nif_unit, ether_sprintf(desc->myea)); 235#endif 236 237 /* Set correct alignment for TX packets */ 238 sc->sc_txbufp = sc->sc_txbuf; 239 if ((unsigned long)sc->sc_txbufp % PKTALIGN) 240 sc->sc_txbufp += PKTALIGN - 241 (unsigned long)sc->sc_txbufp % PKTALIGN; 242} 243 244static void 245net_end(struct netif *nif) 246{ 247 struct uboot_softc *sc = nif->nif_devdata; 248 int err; 249 250 if ((err = ub_dev_close(sc->sc_handle)) != 0) 251 panic("%s%d: net_end failed with error %d\n", 252 nif->nif_driver->netif_bname, nif->nif_unit, err); 253} 254