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