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