1/*	$OpenBSD: netif_of.c,v 1.9 2023/06/03 21:37:53 krw Exp $	*/
2/*	$NetBSD: netif_of.c,v 1.1 2000/08/20 14:58:39 mrg Exp $	*/
3
4/*
5 * Copyright (C) 1995 Wolfgang Solfrank.
6 * Copyright (C) 1995 TooLs GmbH.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by TooLs GmbH.
20 * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 *    derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/*
36 * Open Firmware does most of the job for interfacing to the hardware,
37 * so it is easiest to just replace the netif module with
38 * this adaptation to the PROM network interface.
39 *
40 * Note: this is based in part on sys/arch/sparc/stand/netif_sun.c
41 */
42
43#include <sys/param.h>
44#include <sys/socket.h>
45
46#include <net/if.h>
47
48#include <netinet/in.h>
49#include <netinet/if_ether.h>
50
51#include <lib/libsa/stand.h>
52#include <lib/libsa/net.h>
53#include <lib/libsa/netif.h>
54
55#include "ofdev.h"
56#include "openfirm.h"
57
58static struct netif netif_of;
59
60struct iodesc sockets[SOPEN_MAX];
61
62struct iodesc *
63socktodesc(int sock)
64{
65	if (sock != 0)
66		return NULL;
67	return sockets;
68}
69
70int
71netif_open(void *machdep_hint)
72{
73	struct of_dev *op = machdep_hint;
74	struct iodesc *io;
75
76	DNPRINTF(BOOT_D_OFNET, "netif_open...");
77
78	/* find a free socket */
79	io = sockets;
80	if (io->io_netif) {
81		DNPRINTF(BOOT_D_OFNET, "device busy\n");
82		errno = ENFILE;
83		return -1;
84	}
85	bzero(io, sizeof *io);
86
87	netif_of.nif_devdata = op;
88	io->io_netif = &netif_of;
89
90	/* Put our ethernet address in io->myea */
91	OF_getprop(OF_instance_to_package(op->handle),
92		   "mac-address", io->myea, sizeof io->myea);
93
94	DNPRINTF(BOOT_D_OFNET, "OK\n");
95	return 0;
96}
97
98int
99netif_close(int fd)
100{
101	struct iodesc *io;
102	struct netif *ni;
103
104	DNPRINTF(BOOT_D_OFNET, "netif_close(%x)...", fd);
105
106	if (fd != 0) {
107		DNPRINTF(BOOT_D_OFNET, "EBADF\n");
108		errno = EBADF;
109		return -1;
110	}
111
112	io = &sockets[fd];
113	ni = io->io_netif;
114	if (ni != NULL) {
115		ni->nif_devdata = NULL;
116		io->io_netif = NULL;
117	}
118
119	DNPRINTF(BOOT_D_OFNET, "OK\n");
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 */
127ssize_t
128netif_put(struct iodesc *desc, void *pkt, size_t len)
129{
130	struct of_dev *op;
131	ssize_t rv;
132	size_t sendlen;
133
134	op = desc->io_netif->nif_devdata;
135
136	DNPRINTF(BOOT_D_OFNET, "netif_put: desc=0x%x pkt=0x%x len=%d\n",
137	    desc, pkt, len);
138	DNPRINTF(BOOT_D_OFNET, "dst: %s, src: %s, type: 0x%x\n",
139	    ether_sprintf(((struct ether_header *)pkt)->ether_dhost),
140	    ether_sprintf(((struct ether_header *)pkt)->ether_shost),
141	    ((struct ether_header *)pkt)->ether_type & 0xFFFF);
142
143	sendlen = len;
144	if (sendlen < 60) {
145		sendlen = 60;
146		DNPRINTF(BOOT_D_OFNET, "netif_put: length padded to %d\n",
147		    sendlen);
148	}
149
150	rv = OF_write(op->handle, pkt, sendlen);
151
152	DNPRINTF(BOOT_D_OFNET, "netif_put: xmit returned %d\n", rv);
153
154	return rv;
155}
156
157/*
158 * Receive a packet, including the ether header.
159 * Return the total length received (or -1 on error).
160 */
161ssize_t
162netif_get(struct iodesc *desc, void *pkt, size_t maxlen, time_t timo)
163{
164	struct of_dev *op;
165	int tick0, tmo_ms;
166	int len;
167
168	op = desc->io_netif->nif_devdata;
169
170	DNPRINTF(BOOT_D_OFNET, "netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n",
171	    pkt, maxlen, timo);
172
173	tmo_ms = timo * 1000;
174	tick0 = OF_milliseconds();
175
176	do {
177		len = OF_read(op->handle, pkt, maxlen);
178	} while ((len == -2 || len == 0) &&
179		 (OF_milliseconds() - tick0 < tmo_ms));
180
181	DNPRINTF(BOOT_D_OFNET, "netif_get: received len=%d\n", len);
182
183	if (len < 12)
184		return -1;
185
186	DNPRINTF(BOOT_D_OFNET, "dst: %s, src: %s, type: 0x%x\n",
187	    ether_sprintf(((struct ether_header *)pkt)->ether_dhost),
188	    ether_sprintf(((struct ether_header *)pkt)->ether_shost),
189	    ((struct ether_header *)pkt)->ether_type & 0xFFFF);
190
191	return len;
192}
193
194/*
195 * Shouldn't really be here, but is used solely for networking, so...
196 */
197time_t
198getsecs(void)
199{
200	return OF_milliseconds() / 1000;
201}
202