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