netif_sun.c revision 1.1 1 /* $NetBSD: netif_sun.c,v 1.1 1997/06/01 03:39:36 mrg 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
46 #include <netinet/in.h>
47 #include <lib/libsa/if_ether.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 int fd, error;
81
82 /* find a free socket */
83 io = sockets;
84 if (io->io_netif) {
85 #ifdef DEBUG
86 printf("netif_open: device busy\n");
87 #endif
88 errno = ENFILE;
89 return (-1);
90 }
91 bzero(io, sizeof(*io));
92
93 netif_prom.nif_devdata = pd;
94 io->io_netif = &netif_prom;
95
96 /* Put our ethernet address in io->myea */
97 prom_getether(pd->fd, io->myea);
98
99 return(0);
100 }
101
102 int
103 netif_close(fd)
104 int fd;
105 {
106 struct iodesc *io;
107 struct netif *ni;
108
109 if (fd != 0) {
110 errno = EBADF;
111 return(-1);
112 }
113
114 io = &sockets[fd];
115 ni = io->io_netif;
116 if (ni != NULL) {
117 ni->nif_devdata = NULL;
118 io->io_netif = NULL;
119 }
120 return(0);
121 }
122
123 /*
124 * Send a packet. The ether header is already there.
125 * Return the length sent (or -1 on error).
126 */
127 ssize_t
128 netif_put(desc, pkt, len)
129 struct iodesc *desc;
130 void *pkt;
131 size_t len;
132 {
133 struct promdata *pd;
134 ssize_t rv;
135 size_t sendlen;
136
137 pd = (struct promdata *)desc->io_netif->nif_devdata;
138
139 #ifdef NETIF_DEBUG
140 if (netif_debug) {
141 struct ether_header *eh;
142
143 printf("netif_put: desc=0x%x pkt=0x%x len=%d\n",
144 desc, pkt, len);
145 eh = pkt;
146 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
147 printf("src: %s ", ether_sprintf(eh->ether_shost));
148 printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
149 }
150 #endif
151
152 sendlen = len;
153 if (sendlen < 60) {
154 sendlen = 60;
155 #ifdef NETIF_DEBUG
156 printf("netif_put: length padded to %d\n", sendlen);
157 #endif
158 }
159
160 rv = (*pd->xmit)(pd, pkt, sendlen);
161
162 #ifdef NETIF_DEBUG
163 if (netif_debug)
164 printf("netif_put: xmit returned %d\n", rv);
165 #endif
166
167 return rv;
168 }
169
170 /*
171 * Receive a packet, including the ether header.
172 * Return the total length received (or -1 on error).
173 */
174 ssize_t
175 netif_get(desc, pkt, maxlen, timo)
176 struct iodesc *desc;
177 void *pkt;
178 size_t maxlen;
179 time_t timo;
180 {
181 struct promdata *pd;
182 int tick0, tmo_ticks;
183 ssize_t len;
184
185 pd = (struct promdata *)desc->io_netif->nif_devdata;
186
187 #ifdef NETIF_DEBUG
188 if (netif_debug)
189 printf("netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n",
190 pkt, maxlen, timo);
191 #endif
192
193 tmo_ticks = timo * hz;
194 tick0 = getticks();
195
196 do {
197 len = (*pd->recv)(pd, pkt, maxlen);
198 } while ((len == 0) && ((getticks() - tick0) < tmo_ticks));
199
200 #ifdef NETIF_DEBUG
201 if (netif_debug)
202 printf("netif_get: received len=%d\n", len);
203 #endif
204
205 if (len < 12)
206 return -1;
207
208 #ifdef NETIF_DEBUG
209 if (netif_debug) {
210 struct ether_header *eh = pkt;
211
212 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
213 printf("src: %s ", ether_sprintf(eh->ether_shost));
214 printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
215 }
216 #endif
217
218 return len;
219 }
220