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