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