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