xref: /netbsd-src/sys/arch/sparc/stand/ofwboot/netif_of.c (revision bb558b20105aaa7fb1b27febcf5f04f8f7dc10d9)
1*bb558b20Sjakllsch /*	$NetBSD: netif_of.c,v 1.9 2011/07/30 04:18:38 jakllsch Exp $	*/
2e144281cSmrg 
3e144281cSmrg /*
4e144281cSmrg  * Copyright (C) 1995 Wolfgang Solfrank.
5e144281cSmrg  * Copyright (C) 1995 TooLs GmbH.
6e144281cSmrg  * All rights reserved.
7e144281cSmrg  *
8e144281cSmrg  * Redistribution and use in source and binary forms, with or without
9e144281cSmrg  * modification, are permitted provided that the following conditions
10e144281cSmrg  * are met:
11e144281cSmrg  * 1. Redistributions of source code must retain the above copyright
12e144281cSmrg  *    notice, this list of conditions and the following disclaimer.
13e144281cSmrg  * 2. Redistributions in binary form must reproduce the above copyright
14e144281cSmrg  *    notice, this list of conditions and the following disclaimer in the
15e144281cSmrg  *    documentation and/or other materials provided with the distribution.
16e144281cSmrg  * 3. All advertising materials mentioning features or use of this software
17e144281cSmrg  *    must display the following acknowledgement:
18e144281cSmrg  *	This product includes software developed by TooLs GmbH.
19e144281cSmrg  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20e144281cSmrg  *    derived from this software without specific prior written permission.
21e144281cSmrg  *
22e144281cSmrg  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23e144281cSmrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24e144281cSmrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25e144281cSmrg  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26e144281cSmrg  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27e144281cSmrg  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28e144281cSmrg  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29e144281cSmrg  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30e144281cSmrg  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31e144281cSmrg  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32e144281cSmrg  */
33e144281cSmrg 
34e144281cSmrg /*
35e144281cSmrg  * Open Firmware does most of the job for interfacing to the hardware,
36e144281cSmrg  * so it is easiest to just replace the netif module with
37e144281cSmrg  * this adaptation to the PROM network interface.
38e144281cSmrg  *
39e144281cSmrg  * Note: this is based in part on sys/arch/sparc/stand/netif_sun.c
40e144281cSmrg  */
41e144281cSmrg 
42e144281cSmrg #include <sys/param.h>
43e144281cSmrg #include <sys/socket.h>
44e144281cSmrg 
45e144281cSmrg #include <net/if.h>
46e144281cSmrg #include <net/if_ether.h>
47e144281cSmrg 
48e144281cSmrg #include <netinet/in.h>
49e144281cSmrg #include <netinet/in_systm.h>
50e144281cSmrg 
51e144281cSmrg #include <lib/libsa/stand.h>
52e144281cSmrg #include <lib/libsa/net.h>
53e144281cSmrg #include <lib/libsa/netif.h>
54e144281cSmrg 
5597aa1417Scdi #include <machine/promlib.h>
5697aa1417Scdi 
57e144281cSmrg #include "ofdev.h"
58e144281cSmrg 
59e144281cSmrg static struct netif netif_of;
60e144281cSmrg 
61e144281cSmrg struct iodesc sockets[SOPEN_MAX];
62e144281cSmrg 
63e144281cSmrg struct iodesc *
socktodesc(int sock)644c3c91e6Suwe socktodesc(int sock)
65e144281cSmrg {
66e144281cSmrg 	if (sock != 0)
67e144281cSmrg 		return NULL;
68e144281cSmrg 	return sockets;
69e144281cSmrg }
70e144281cSmrg 
71e144281cSmrg int
netif_open(void * machdep_hint)724c3c91e6Suwe netif_open(void *machdep_hint)
73e144281cSmrg {
74e144281cSmrg 	struct of_dev *op = machdep_hint;
75e144281cSmrg 	struct iodesc *io;
76e144281cSmrg 
77e144281cSmrg #ifdef	NETIF_DEBUG
78e144281cSmrg 	printf("netif_open...");
79e144281cSmrg #endif
80e144281cSmrg 	/* find a free socket */
81e144281cSmrg 	io = sockets;
82e144281cSmrg 	if (io->io_netif) {
83e144281cSmrg #ifdef	NETIF_DEBUG
84e144281cSmrg 		printf("device busy\n");
85e144281cSmrg #endif
86e144281cSmrg 		errno = ENFILE;
87e144281cSmrg 		return -1;
88e144281cSmrg 	}
89c363a9cbScegger 	memset(io, 0, sizeof *io);
90e144281cSmrg 
91e144281cSmrg 	netif_of.nif_devdata = op;
92e144281cSmrg 	io->io_netif = &netif_of;
93e144281cSmrg 
94e144281cSmrg 	/* Put our ethernet address in io->myea */
9597aa1417Scdi 	_prom_getprop(prom_instance_to_package(op->handle),
96e144281cSmrg 		   "mac-address", io->myea, sizeof io->myea);
97e144281cSmrg 
98e144281cSmrg #ifdef	NETIF_DEBUG
99e144281cSmrg 	printf("OK\n");
100e144281cSmrg #endif
101e144281cSmrg 	return 0;
102e144281cSmrg }
103e144281cSmrg 
104e144281cSmrg int
netif_close(int fd)1054c3c91e6Suwe netif_close(int fd)
106e144281cSmrg {
107e144281cSmrg 	struct iodesc *io;
108e144281cSmrg 	struct netif *ni;
109e144281cSmrg 
110e144281cSmrg #ifdef	NETIF_DEBUG
111e144281cSmrg 	printf("netif_close(%x)...", fd);
112e144281cSmrg #endif
113e144281cSmrg 	if (fd != 0) {
114e144281cSmrg #ifdef	NETIF_DEBUG
115e144281cSmrg 		printf("EBADF\n");
116e144281cSmrg #endif
117e144281cSmrg 		errno = EBADF;
118e144281cSmrg 		return -1;
119e144281cSmrg 	}
120e144281cSmrg 
121e144281cSmrg 	io = &sockets[fd];
122e144281cSmrg 	ni = io->io_netif;
123e144281cSmrg 	if (ni != NULL) {
124e144281cSmrg 		ni->nif_devdata = NULL;
125e144281cSmrg 		io->io_netif = NULL;
126e144281cSmrg 	}
127e144281cSmrg #ifdef	NETIF_DEBUG
128e144281cSmrg 	printf("OK\n");
129e144281cSmrg #endif
130e144281cSmrg 	return 0;
131e144281cSmrg }
132e144281cSmrg 
133e144281cSmrg /*
134e144281cSmrg  * Send a packet.  The ether header is already there.
135e144281cSmrg  * Return the length sent (or -1 on error).
136e144281cSmrg  */
137e144281cSmrg ssize_t
netif_put(struct iodesc * desc,void * pkt,size_t len)1384c3c91e6Suwe netif_put(struct iodesc *desc, void *pkt, size_t len)
139e144281cSmrg {
140e144281cSmrg 	struct of_dev *op;
141e144281cSmrg 	ssize_t rv;
142e144281cSmrg 	size_t sendlen;
143e144281cSmrg 
144f2746edcShannken 	op = ((struct netif *)desc->io_netif)->nif_devdata;
145e144281cSmrg 
146e144281cSmrg #ifdef	NETIF_DEBUG
147e144281cSmrg 	{
148e144281cSmrg 		struct ether_header *eh;
149e144281cSmrg 
150*bb558b20Sjakllsch 		printf("netif_put: desc=%p pkt=%p len=%zu\n",
151e144281cSmrg 		       desc, pkt, len);
152e144281cSmrg 		eh = pkt;
153e144281cSmrg 		printf("dst: %s ", ether_sprintf(eh->ether_dhost));
154e144281cSmrg 		printf("src: %s ", ether_sprintf(eh->ether_shost));
155e144281cSmrg 		printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
156e144281cSmrg 	}
157e144281cSmrg #endif
158e144281cSmrg 
159e144281cSmrg 	sendlen = len;
160e144281cSmrg 	if (sendlen < 60) {
161e144281cSmrg 		sendlen = 60;
162e144281cSmrg #ifdef	NETIF_DEBUG
163*bb558b20Sjakllsch 		printf("netif_put: length padded to %zu\n", sendlen);
164e144281cSmrg #endif
165e144281cSmrg 	}
166e144281cSmrg 
16797aa1417Scdi 	rv = prom_write(op->handle, pkt, sendlen);
168e144281cSmrg 
169e144281cSmrg #ifdef	NETIF_DEBUG
170*bb558b20Sjakllsch 	printf("netif_put: xmit returned %zd\n", rv);
171e144281cSmrg #endif
172e144281cSmrg 
17385de90b8Smlelstv 	if (rv > len)
17485de90b8Smlelstv 		rv = len;
17585de90b8Smlelstv 
176e144281cSmrg 	return rv;
177e144281cSmrg }
178e144281cSmrg 
179e144281cSmrg /*
180e144281cSmrg  * Receive a packet, including the ether header.
181e144281cSmrg  * Return the total length received (or -1 on error).
182e144281cSmrg  */
183e144281cSmrg ssize_t
netif_get(struct iodesc * desc,void * pkt,size_t maxlen,saseconds_t timo)18469cf32a7Stsutsui netif_get(struct iodesc *desc, void *pkt, size_t maxlen, saseconds_t timo)
185e144281cSmrg {
186e144281cSmrg 	struct of_dev *op;
187e144281cSmrg 	int tick0, tmo_ms;
188e144281cSmrg 	int len;
189e144281cSmrg 
190f2746edcShannken 	op = ((struct netif *)desc->io_netif)->nif_devdata;
191e144281cSmrg 
192e144281cSmrg #ifdef	NETIF_DEBUG
193*bb558b20Sjakllsch 	printf("netif_get: pkt=%p, maxlen=%zu, tmo=%d\n",
194e144281cSmrg 	       pkt, maxlen, timo);
195e144281cSmrg #endif
196e144281cSmrg 
197e144281cSmrg 	tmo_ms = timo * 1000;
19897aa1417Scdi 	tick0 = prom_ticks();
199e144281cSmrg 
200e144281cSmrg 	do {
20197aa1417Scdi 		len = prom_read(op->handle, pkt, maxlen);
202e144281cSmrg 	} while ((len == -2 || len == 0) &&
20397aa1417Scdi 		 (prom_ticks() - tick0 < tmo_ms));
204e144281cSmrg 
205e144281cSmrg #ifdef	NETIF_DEBUG
206e144281cSmrg 	printf("netif_get: received len=%d\n", len);
207e144281cSmrg #endif
208e144281cSmrg 
209e144281cSmrg 	if (len < 12)
210e144281cSmrg 		return -1;
211e144281cSmrg 
212e144281cSmrg #ifdef	NETIF_DEBUG
213e144281cSmrg 	{
214e144281cSmrg 		struct ether_header *eh = pkt;
215e144281cSmrg 
216e144281cSmrg 		printf("dst: %s ", ether_sprintf(eh->ether_dhost));
217e144281cSmrg 		printf("src: %s ", ether_sprintf(eh->ether_shost));
218e144281cSmrg 		printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
219e144281cSmrg 	}
220e144281cSmrg #endif
221e144281cSmrg 
222e144281cSmrg 	return len;
223e144281cSmrg }
224e144281cSmrg 
225e144281cSmrg /*
226e144281cSmrg  * Shouldn't really be here, but is used solely for networking, so...
227e144281cSmrg  */
22869cf32a7Stsutsui satime_t
getsecs(void)2294c3c91e6Suwe getsecs(void)
230e144281cSmrg {
2314c3c91e6Suwe 
23297aa1417Scdi 	return prom_ticks() / 1000;
233e144281cSmrg }
234