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 "glue.h"
47#include "libuboot.h"
48
49static int	net_probe(struct netif *, void *);
50static int	net_match(struct netif *, void *);
51static void	net_init(struct iodesc *, void *);
52static int	net_get(struct iodesc *, void *, size_t, time_t);
53static int	net_put(struct iodesc *, void *, size_t);
54static void	net_end(struct netif *);
55
56extern struct netif_stats net_stats[];
57
58struct netif_dif net_ifs[] = {
59	/*	dif_unit	dif_nsel	dif_stats	dif_private */
60	{	0,		1,		&net_stats[0],	0,	},
61};
62
63struct netif_stats net_stats[NENTS(net_ifs)];
64
65struct 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 */
75};
76
77struct uboot_softc {
78	uint32_t	sc_pad;
79	uint8_t		sc_rxbuf[ETHER_MAX_LEN];
80	uint8_t		sc_txbuf[ETHER_MAX_LEN + PKTALIGN];
81	uint8_t		*sc_txbufp;
82	int		sc_handle;	/* device handle for ub_dev_xxx */
83};
84
85static struct uboot_softc uboot_softc;
86
87static int
88net_match(struct netif *nif, void *machdep_hint)
89{
90	char **a = (char **)machdep_hint;
91
92	if (memcmp("net", *a, 3) == 0)
93		return (1);
94
95	printf("net_match: could not match network device\n");
96	return (0);
97}
98
99static int
100net_probe(struct netif *nif, void *machdep_hint)
101{
102	struct device_info *di;
103	int i;
104
105	for (i = 0; i < devs_no; i++)
106		if ((di = ub_dev_get(i)) != NULL)
107			if (di->type == DEV_TYP_NET)
108				break;
109
110	if (i == devs_no) {
111		printf("net_probe: no network devices found, maybe not"
112		    " enumerated yet..?\n");
113		return (-1);
114	}
115
116#if defined(NETIF_DEBUG)
117	printf("net_probe: network device found: %d\n", i);
118#endif
119	uboot_softc.sc_handle = i;
120
121	return (0);
122}
123
124static int
125net_put(struct iodesc *desc, void *pkt, size_t len)
126{
127	struct netif *nif = desc->io_netif;
128	struct uboot_softc *sc = nif->nif_devdata;
129	size_t sendlen;
130	ssize_t rv;
131
132#if defined(NETIF_DEBUG)
133	struct ether_header *eh;
134
135	printf("net_put: desc %p, pkt %p, len %d\n", desc, pkt, len);
136	eh = pkt;
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);
140#endif
141
142	if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
143		sendlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
144		bzero(sc->sc_txbufp, sendlen);
145	} else
146		sendlen = len;
147
148	memcpy(sc->sc_txbufp, pkt, len);
149
150	rv = ub_dev_send(sc->sc_handle, sc->sc_txbufp, sendlen);
151
152#if defined(NETIF_DEBUG)
153	printf("net_put: ub_send returned %d\n", rv);
154#endif
155	if (rv == 0)
156		rv = len;
157	else
158		rv = -1;
159
160	return (rv);
161}
162
163static int
164net_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
165{
166	struct netif *nif = desc->io_netif;
167	struct uboot_softc *sc = nif->nif_devdata;
168	time_t t;
169	int err, rlen;
170
171#if defined(NETIF_DEBUG)
172	printf("net_get: pkt %p, len %d, timeout %d\n", pkt, len, timeout);
173#endif
174	t = getsecs();
175	do {
176		err = ub_dev_recv(sc->sc_handle, sc->sc_rxbuf, len, &rlen);
177
178		if (err != 0) {
179			printf("net_get: ub_dev_recv() failed, error=%d\n",
180			    err);
181			rlen = 0;
182			break;
183		}
184	} while ((rlen == -1 || rlen == 0) && (getsecs() - t < timeout));
185
186#if defined(NETIF_DEBUG)
187	printf("net_get: received len %d (%x)\n", rlen, rlen);
188#endif
189
190	if (rlen > 0) {
191		memcpy(pkt, sc->sc_rxbuf, MIN(len, rlen));
192		if (rlen != len) {
193#if defined(NETIF_DEBUG)
194			printf("net_get: len %x, rlen %x\n", len, rlen);
195#endif
196		}
197		return (rlen);
198	}
199
200	return (-1);
201}
202
203static void
204net_init(struct iodesc *desc, void *machdep_hint)
205{
206	struct netif *nif = desc->io_netif;
207	struct uboot_softc *sc;
208	struct device_info *di;
209	int err;
210
211	sc = nif->nif_devdata = &uboot_softc;
212
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);
216
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);
223	}
224
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));
228#endif
229
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;
235}
236
237static void
238net_end(struct netif *nif)
239{
240	struct uboot_softc *sc = nif->nif_devdata;
241	int err;
242
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);
246}
247