netif_of.c revision 1.3 1 /* $NetBSD: netif_of.c,v 1.3 2006/01/27 18:31:12 cdi 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(sock)
65 int sock;
66 {
67 if (sock != 0)
68 return NULL;
69 return sockets;
70 }
71
72 int
73 netif_open(machdep_hint)
74 void *machdep_hint;
75 {
76 struct of_dev *op = machdep_hint;
77 struct iodesc *io;
78 int fd, error;
79 char addr[32];
80
81 #ifdef NETIF_DEBUG
82 printf("netif_open...");
83 #endif
84 /* find a free socket */
85 io = sockets;
86 if (io->io_netif) {
87 #ifdef NETIF_DEBUG
88 printf("device busy\n");
89 #endif
90 errno = ENFILE;
91 return -1;
92 }
93 bzero(io, sizeof *io);
94
95 netif_of.nif_devdata = op;
96 io->io_netif = &netif_of;
97
98 /* Put our ethernet address in io->myea */
99 _prom_getprop(prom_instance_to_package(op->handle),
100 "mac-address", io->myea, sizeof io->myea);
101
102 #ifdef NETIF_DEBUG
103 printf("OK\n");
104 #endif
105 return 0;
106 }
107
108 int
109 netif_close(fd)
110 int fd;
111 {
112 struct iodesc *io;
113 struct netif *ni;
114
115 #ifdef NETIF_DEBUG
116 printf("netif_close(%x)...", fd);
117 #endif
118 if (fd != 0) {
119 #ifdef NETIF_DEBUG
120 printf("EBADF\n");
121 #endif
122 errno = EBADF;
123 return -1;
124 }
125
126 io = &sockets[fd];
127 ni = io->io_netif;
128 if (ni != NULL) {
129 ni->nif_devdata = NULL;
130 io->io_netif = NULL;
131 }
132 #ifdef NETIF_DEBUG
133 printf("OK\n");
134 #endif
135 return 0;
136 }
137
138 /*
139 * Send a packet. The ether header is already there.
140 * Return the length sent (or -1 on error).
141 */
142 ssize_t
143 netif_put(desc, pkt, len)
144 struct iodesc *desc;
145 void *pkt;
146 size_t len;
147 {
148 struct of_dev *op;
149 ssize_t rv;
150 size_t sendlen;
151
152 op = ((struct netif *)desc->io_netif)->nif_devdata;
153
154 #ifdef NETIF_DEBUG
155 {
156 struct ether_header *eh;
157
158 printf("netif_put: desc=0x%x pkt=0x%x len=%d\n",
159 desc, pkt, len);
160 eh = pkt;
161 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
162 printf("src: %s ", ether_sprintf(eh->ether_shost));
163 printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
164 }
165 #endif
166
167 sendlen = len;
168 if (sendlen < 60) {
169 sendlen = 60;
170 #ifdef NETIF_DEBUG
171 printf("netif_put: length padded to %d\n", sendlen);
172 #endif
173 }
174
175 rv = prom_write(op->handle, pkt, sendlen);
176
177 #ifdef NETIF_DEBUG
178 printf("netif_put: xmit returned %d\n", rv);
179 #endif
180
181 return rv;
182 }
183
184 /*
185 * Receive a packet, including the ether header.
186 * Return the total length received (or -1 on error).
187 */
188 ssize_t
189 netif_get(desc, pkt, maxlen, timo)
190 struct iodesc *desc;
191 void *pkt;
192 size_t maxlen;
193 time_t timo;
194 {
195 struct of_dev *op;
196 int tick0, tmo_ms;
197 int len;
198
199 op = ((struct netif *)desc->io_netif)->nif_devdata;
200
201 #ifdef NETIF_DEBUG
202 printf("netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n",
203 pkt, maxlen, timo);
204 #endif
205
206 tmo_ms = timo * 1000;
207 tick0 = prom_ticks();
208
209 do {
210 len = prom_read(op->handle, pkt, maxlen);
211 } while ((len == -2 || len == 0) &&
212 (prom_ticks() - tick0 < tmo_ms));
213
214 #ifdef NETIF_DEBUG
215 printf("netif_get: received len=%d\n", len);
216 #endif
217
218 if (len < 12)
219 return -1;
220
221 #ifdef NETIF_DEBUG
222 {
223 struct ether_header *eh = pkt;
224
225 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
226 printf("src: %s ", ether_sprintf(eh->ether_shost));
227 printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
228 }
229 #endif
230
231 return len;
232 }
233
234 /*
235 * Shouldn't really be here, but is used solely for networking, so...
236 */
237 time_t
238 getsecs()
239 {
240 return prom_ticks() / 1000;
241 }
242