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