Home | History | Annotate | Line # | Download | only in ofwboot
netif_of.c revision 1.1
      1 /*	$NetBSD: netif_of.c,v 1.1 1997/04/16 20:29:19 thorpej 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 #if 0			/* XXX thorpej */
     46 #include <string.h>
     47 #include <time.h>
     48 #endif
     49 
     50 #include <net/if.h>
     51 
     52 #include <netinet/in.h>
     53 #include <lib/libsa/if_ether.h>
     54 #include <netinet/in_systm.h>
     55 
     56 #include <lib/libsa/stand.h>
     57 #include <lib/libsa/net.h>
     58 #include <lib/libsa/netif.h>
     59 
     60 #include <powerpc/stand/ofwboot/ofdev.h>
     61 #include <powerpc/stand/ofwboot/openfirm.h>
     62 
     63 static struct netif netif_of;
     64 
     65 struct iodesc sockets[SOPEN_MAX];
     66 
     67 struct iodesc *
     68 socktodesc(sock)
     69 	int sock;
     70 {
     71 	if (sock != 0)
     72 		return NULL;
     73 	return sockets;
     74 }
     75 
     76 int
     77 netif_open(machdep_hint)
     78 	void *machdep_hint;
     79 {
     80 	struct of_dev *op = machdep_hint;
     81 	struct iodesc *io;
     82 	int fd, error;
     83 	char addr[32];
     84 
     85 #ifdef	NETIF_DEBUG
     86 	printf("netif_open...");
     87 #endif
     88 	/* find a free socket */
     89 	io = sockets;
     90 	if (io->io_netif) {
     91 #ifdef	NETIF_DEBUG
     92 		printf("device busy\n");
     93 #endif
     94 		errno = ENFILE;
     95 		return -1;
     96 	}
     97 	bzero(io, sizeof *io);
     98 
     99 	netif_of.nif_devdata = op;
    100 	io->io_netif = &netif_of;
    101 
    102 	/* Put our ethernet address in io->myea */
    103 	OF_getprop(OF_instance_to_package(op->handle),
    104 		   "mac-address", io->myea, sizeof io->myea);
    105 
    106 #ifdef	NETIF_DEBUG
    107 	printf("OK\n");
    108 #endif
    109 	return 0;
    110 }
    111 
    112 int
    113 netif_close(fd)
    114 	int fd;
    115 {
    116 	struct iodesc *io;
    117 	struct netif *ni;
    118 
    119 #ifdef	NETIF_DEBUG
    120 	printf("netif_close(%x)...", fd);
    121 #endif
    122 	if (fd != 0) {
    123 #ifdef	NETIF_DEBUG
    124 		printf("EBADF\n");
    125 #endif
    126 		errno = EBADF;
    127 		return -1;
    128 	}
    129 
    130 	io = &sockets[fd];
    131 	ni = io->io_netif;
    132 	if (ni != NULL) {
    133 		ni->nif_devdata = NULL;
    134 		io->io_netif = NULL;
    135 	}
    136 #ifdef	NETIF_DEBUG
    137 	printf("OK\n");
    138 #endif
    139 	return 0;
    140 }
    141 
    142 /*
    143  * Send a packet.  The ether header is already there.
    144  * Return the length sent (or -1 on error).
    145  */
    146 ssize_t
    147 netif_put(desc, pkt, len)
    148 	struct iodesc *desc;
    149 	void *pkt;
    150 	size_t len;
    151 {
    152 	struct of_dev *op;
    153 	ssize_t rv;
    154 	size_t sendlen;
    155 
    156 	op = desc->io_netif->nif_devdata;
    157 
    158 #ifdef	NETIF_DEBUG
    159 	{
    160 		struct ether_header *eh;
    161 
    162 		printf("netif_put: desc=0x%x pkt=0x%x len=%d\n",
    163 		       desc, pkt, len);
    164 		eh = pkt;
    165 		printf("dst: %s ", ether_sprintf(eh->ether_dhost));
    166 		printf("src: %s ", ether_sprintf(eh->ether_shost));
    167 		printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
    168 	}
    169 #endif
    170 
    171 	sendlen = len;
    172 	if (sendlen < 60) {
    173 		sendlen = 60;
    174 #ifdef	NETIF_DEBUG
    175 		printf("netif_put: length padded to %d\n", sendlen);
    176 #endif
    177 	}
    178 
    179 	rv = OF_write(op->handle, pkt, sendlen);
    180 
    181 #ifdef	NETIF_DEBUG
    182 	printf("netif_put: xmit returned %d\n", rv);
    183 #endif
    184 
    185 	return rv;
    186 }
    187 
    188 /*
    189  * Receive a packet, including the ether header.
    190  * Return the total length received (or -1 on error).
    191  */
    192 ssize_t
    193 netif_get(desc, pkt, maxlen, timo)
    194 	struct iodesc *desc;
    195 	void *pkt;
    196 	size_t maxlen;
    197 	time_t timo;
    198 {
    199 	struct of_dev *op;
    200 	int tick0, tmo_ms;
    201 	int len;
    202 
    203 	op = desc->io_netif->nif_devdata;
    204 
    205 #ifdef	NETIF_DEBUG
    206 	printf("netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n",
    207 	       pkt, maxlen, timo);
    208 #endif
    209 
    210 	tmo_ms = timo * 1000;
    211 	tick0 = OF_milliseconds();
    212 
    213 	do {
    214 		len = OF_read(op->handle, pkt, maxlen);
    215 	} while ((len == -2) && ((OF_milliseconds() - tick0) < tmo_ms));
    216 
    217 #ifdef	NETIF_DEBUG
    218 	printf("netif_get: received len=%d\n", len);
    219 #endif
    220 
    221 	if (len < 12)
    222 		return -1;
    223 
    224 #ifdef	NETIF_DEBUG
    225 	{
    226 		struct ether_header *eh = pkt;
    227 
    228 		printf("dst: %s ", ether_sprintf(eh->ether_dhost));
    229 		printf("src: %s ", ether_sprintf(eh->ether_shost));
    230 		printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
    231 	}
    232 #endif
    233 
    234 	return len;
    235 }
    236 
    237 /*
    238  * Shouldn't really be here, but is used solely for networking, so...
    239  */
    240 time_t
    241 getsecs()
    242 {
    243 	return OF_milliseconds() / 1000;
    244 }
    245