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