xref: /netbsd/sys/arch/macppc/stand/ofwboot/netif_of.c (revision bf9ec67e)
1 /*	$NetBSD: netif_of.c,v 1.5 2002/03/29 15:15:07 tsutsui 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 "ofdev.h"
56 #include "openfirm.h"
57 
58 static struct netif netif_of;
59 
60 struct iodesc sockets[SOPEN_MAX];
61 
62 struct iodesc *
63 socktodesc(sock)
64 	int sock;
65 {
66 	if (sock != 0)
67 		return NULL;
68 	return sockets;
69 }
70 
71 int
72 netif_open(machdep_hint)
73 	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 	memset(io, 0, 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 	OF_getprop(OF_instance_to_package(op->handle),
99 		   "local-mac-address", io->myea, sizeof io->myea) == -1 &&
100 	OF_getprop(OF_instance_to_package(op->handle),
101 		   "mac-address", io->myea, sizeof io->myea);
102 
103 #ifdef	NETIF_DEBUG
104 	printf("OK\n");
105 #endif
106 	return 0;
107 }
108 
109 int
110 netif_close(fd)
111 	int fd;
112 {
113 	struct iodesc *io;
114 	struct netif *ni;
115 
116 #ifdef	NETIF_DEBUG
117 	printf("netif_close(%x)...", fd);
118 #endif
119 	if (fd != 0) {
120 #ifdef	NETIF_DEBUG
121 		printf("EBADF\n");
122 #endif
123 		errno = EBADF;
124 		return -1;
125 	}
126 
127 	io = &sockets[fd];
128 	ni = io->io_netif;
129 	if (ni != NULL) {
130 		ni->nif_devdata = NULL;
131 		io->io_netif = NULL;
132 	}
133 #ifdef	NETIF_DEBUG
134 	printf("OK\n");
135 #endif
136 	return 0;
137 }
138 
139 /*
140  * Send a packet.  The ether header is already there.
141  * Return the length sent (or -1 on error).
142  */
143 ssize_t
144 netif_put(desc, pkt, len)
145 	struct iodesc *desc;
146 	void *pkt;
147 	size_t len;
148 {
149 	struct of_dev *op;
150 	ssize_t rv;
151 	size_t sendlen;
152 
153 	op = desc->io_netif->nif_devdata;
154 
155 #ifdef	NETIF_DEBUG
156 	{
157 		struct ether_header *eh;
158 
159 		printf("netif_put: desc=0x%x pkt=0x%x len=%d\n",
160 		       desc, pkt, len);
161 		eh = pkt;
162 		printf("dst: %s ", ether_sprintf(eh->ether_dhost));
163 		printf("src: %s ", ether_sprintf(eh->ether_shost));
164 		printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
165 	}
166 #endif
167 
168 	sendlen = len;
169 	if (sendlen < 60) {
170 		sendlen = 60;
171 #ifdef	NETIF_DEBUG
172 		printf("netif_put: length padded to %d\n", sendlen);
173 #endif
174 	}
175 
176 	if (op->dmabuf) {
177 		memcpy(op->dmabuf, pkt, sendlen);
178 		pkt = op->dmabuf;
179 	}
180 	rv = OF_write(op->handle, pkt, sendlen);
181 
182 #ifdef	NETIF_DEBUG
183 	printf("netif_put: xmit returned %d\n", rv);
184 #endif
185 
186 	return rv;
187 }
188 
189 /*
190  * Receive a packet, including the ether header.
191  * Return the total length received (or -1 on error).
192  */
193 ssize_t
194 netif_get(desc, pkt, maxlen, timo)
195 	struct iodesc *desc;
196 	void *pkt;
197 	size_t maxlen;
198 	time_t timo;
199 {
200 	struct of_dev *op;
201 	int tick0, tmo_ms;
202 	int len;
203 
204 	op = desc->io_netif->nif_devdata;
205 
206 #ifdef	NETIF_DEBUG
207 	printf("netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n",
208 	       pkt, maxlen, timo);
209 #endif
210 
211 	tmo_ms = timo * 1000;
212 	tick0 = OF_milliseconds();
213 
214 	do {
215 		len = OF_read(op->handle, pkt, maxlen);
216 	} while ((len == -2 || len == 0) &&
217 		 (OF_milliseconds() - tick0 < tmo_ms));
218 
219 #ifdef	NETIF_DEBUG
220 	printf("netif_get: received len=%d\n", len);
221 #endif
222 
223 	if (len < 12)
224 		return -1;
225 
226 #ifdef	NETIF_DEBUG
227 	{
228 		struct ether_header *eh = pkt;
229 
230 		printf("dst: %s ", ether_sprintf(eh->ether_dhost));
231 		printf("src: %s ", ether_sprintf(eh->ether_shost));
232 		printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
233 	}
234 #endif
235 
236 	return len;
237 }
238 
239 /*
240  * Shouldn't really be here, but is used solely for networking, so...
241  */
242 time_t
243 getsecs()
244 {
245 	return OF_milliseconds() / 1000;
246 }
247