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