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