]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/uboot/lib/net.c
This commit was generated by cvs2svn to compensate for changes in r178479,
[FreeBSD/FreeBSD.git] / sys / boot / uboot / lib / net.c
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$");
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 "libuboot.h"
47
48 #define NETIF_DEBUG
49 #define NETIF_VERBOSE_DEBUG
50 #undef NETIF_DEBUG
51 #undef NETIF_VERBOSE_DEBUG
52
53
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 *);
60
61 struct device_info * ub_dev_get(int i);
62
63 extern int                      devs_no;
64 extern struct netif_stats       net_stats[];
65
66 struct netif_dif net_ifs[] = {
67         /*      dif_unit        dif_nsel        dif_stats       dif_private */
68         {       0,              1,              &net_stats[0],  0,      },
69 };
70
71 struct netif_stats net_stats[NENTS(net_ifs)];
72
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 */
83 };
84
85 struct uboot_softc {
86         u_int32_t       sc_pad;
87         u_int8_t        sc_rxbuf[ETHER_MAX_LEN];
88         u_int8_t        sc_txbuf[ETHER_MAX_LEN + PKTALIGN];
89         u_int8_t        *sc_txbufp;
90         int             sc_handle;      /* device handle for ub_dev_xxx */
91 };
92
93 static struct uboot_softc uboot_softc;
94
95 static int
96 net_match(struct netif *nif, void *machdep_hint)
97 {
98         char **a = (char **)machdep_hint;
99
100         if (memcmp("net", *a, 3) == 0)
101                 return (1);
102
103         printf("net_match: could not match network device\n");
104         return (0);
105 }
106
107 static int
108 net_probe(struct netif *nif, void *machdep_hint)
109 {
110         struct device_info      *di;
111         int                     i;
112
113         for (i = 0; i < devs_no; i++)
114                 if (di = ub_dev_get(i))
115                         if (di->type == DEV_TYP_NET)
116                                 break;
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
131 static int
132 net_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
137         struct ether_header     *eh;
138         size_t                  sendlen;
139         ssize_t                 rv;
140
141 #if defined(NETIF_DEBUG)
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
170 static int
171 net_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     length;
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                 length = ub_dev_recv(sc->sc_handle, sc->sc_rxbuf, len);
184         } while ((length == -1 || length == 0) &&
185                 (getsecs() - t < timeout));
186
187 #if defined(NETIF_DEBUG)
188         printf("net_get: received len %d (%x)\n", length, length);
189 #endif
190
191         if (length > 0) {
192                 memcpy(pkt, sc->sc_rxbuf, MIN(len, length));
193                 if (length != len) {
194 #if defined(NETIF_DEBUG)
195                         printf("net_get: len %x, length %x\n", len, length);
196 #endif
197                 }
198                 return (length);
199         }
200
201         return (-1);
202 }
203
204
205 static void
206 net_init(struct iodesc *desc, void *machdep_hint)
207 {
208         struct netif            *nif = desc->io_netif;
209         struct uboot_softc      *sc;
210         struct device_info      *di;
211         int                      err, i;
212
213         sc = nif->nif_devdata = &uboot_softc;
214
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);
218
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);
225         }
226
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));
230 #endif
231
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;
237 }
238
239
240 static void
241 net_end(struct netif *nif)
242 {
243         struct uboot_softc      *sc = nif->nif_devdata;
244         int                      err;
245
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);
249 }