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