1/*	$NetBSD: netif_sun.c,v 1.11 2009/03/18 16:00:15 cegger 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 * The Sun PROM has a fairly general set of network drivers,
30 * so it is easiest to just replace the netif module with
31 * this adaptation to the PROM network interface.
32 */
33
34#include <sys/param.h>
35#include <sys/socket.h>
36
37#include <net/if.h>
38#include <net/if_ether.h>
39
40#include <netinet/in.h>
41#include <netinet/in_systm.h>
42
43#include <lib/libsa/stand.h>
44#include <lib/libsa/net.h>
45#include <lib/libsa/netif.h>
46#include <lib/libkern/libkern.h>
47
48#include <machine/promlib.h>
49#include <sparc/stand/common/promdev.h>
50
51static struct netif netif_prom;
52
53#ifdef NETIF_DEBUG
54int netif_debug;
55#endif
56
57struct iodesc sockets[SOPEN_MAX];
58
59struct iodesc *
60socktodesc(int sock)
61{
62
63	if (sock != 0) {
64		return(NULL);
65	}
66	return (sockets);
67}
68
69int
70netif_open(void *machdep_hint)
71{
72	struct promdata *pd = machdep_hint;
73	struct iodesc *io;
74	int node;
75
76	/* find a free socket */
77	io = sockets;
78	if (io->io_netif) {
79#ifdef	DEBUG
80		printf("netif_open: device busy\n");
81#endif
82		errno = ENFILE;
83		return (-1);
84	}
85	memset(io, 0, sizeof(*io));
86
87	netif_prom.nif_devdata = pd;
88	io->io_netif = &netif_prom;
89
90	/* Put our ethernet address in io->myea */
91	switch (prom_version()) {
92	case PROM_OBP_V2:
93	case PROM_OBP_V3:
94	case PROM_OPENFIRM:
95		node = prom_instance_to_package(pd->fd);
96		break;
97	default:
98		node = 0;
99	}
100	prom_getether(node, io->myea);
101
102	return(0);
103}
104
105int
106netif_close(int fd)
107{
108	struct iodesc *io;
109	struct netif *ni;
110
111	if (fd != 0) {
112		errno = EBADF;
113		return(-1);
114	}
115
116	io = &sockets[fd];
117	ni = io->io_netif;
118	if (ni != NULL) {
119		ni->nif_devdata = NULL;
120		io->io_netif = NULL;
121	}
122	return(0);
123}
124
125/*
126 * Send a packet.  The ether header is already there.
127 * Return the length sent (or -1 on error).
128 */
129ssize_t
130netif_put(struct iodesc *desc, void *pkt, size_t len)
131{
132	struct promdata *pd;
133	ssize_t rv;
134	size_t sendlen;
135
136	pd = (struct promdata *)((struct netif *)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 */
173ssize_t
174netif_get(struct iodesc *desc, void *pkt, size_t maxlen, saseconds_t timo)
175{
176	struct promdata *pd;
177	satime_t tick0;
178	ssize_t len;
179
180	pd = (struct promdata *)((struct netif *)desc->io_netif)->nif_devdata;
181
182#ifdef NETIF_DEBUG
183	if (netif_debug)
184		printf("netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n",
185			   pkt, maxlen, timo);
186#endif
187
188	tick0 = getsecs();
189
190	do {
191		len = (*pd->recv)(pd, pkt, maxlen);
192	} while ((len == 0) && ((getsecs() - tick0) < timo));
193
194#ifdef NETIF_DEBUG
195	if (netif_debug)
196		printf("netif_get: received len=%d\n", len);
197#endif
198
199	if (len < 12)
200		return -1;
201
202#ifdef NETIF_DEBUG
203	if (netif_debug) {
204		struct ether_header *eh = pkt;
205
206		printf("dst: %s ", ether_sprintf(eh->ether_dhost));
207		printf("src: %s ", ether_sprintf(eh->ether_shost));
208		printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
209	}
210#endif
211
212	return len;
213}
214