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