Home | History | Annotate | Line # | Download | only in common
netif_sun.c revision 1.10
      1 /*	$NetBSD: netif_sun.c,v 1.10 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/libsa/netif.h>
     51 #include <lib/libkern/libkern.h>
     52 
     53 #include <machine/promlib.h>
     54 #include <sparc/stand/common/promdev.h>
     55 
     56 static struct netif netif_prom;
     57 
     58 #ifdef NETIF_DEBUG
     59 int netif_debug;
     60 #endif
     61 
     62 struct iodesc sockets[SOPEN_MAX];
     63 
     64 struct iodesc *
     65 socktodesc(int sock)
     66 {
     67 
     68 	if (sock != 0) {
     69 		return(NULL);
     70 	}
     71 	return (sockets);
     72 }
     73 
     74 int
     75 netif_open(void *machdep_hint)
     76 {
     77 	struct promdata *pd = machdep_hint;
     78 	struct iodesc *io;
     79 	int node;
     80 
     81 	/* find a free socket */
     82 	io = sockets;
     83 	if (io->io_netif) {
     84 #ifdef	DEBUG
     85 		printf("netif_open: device busy\n");
     86 #endif
     87 		errno = ENFILE;
     88 		return (-1);
     89 	}
     90 	bzero(io, sizeof(*io));
     91 
     92 	netif_prom.nif_devdata = pd;
     93 	io->io_netif = &netif_prom;
     94 
     95 	/* Put our ethernet address in io->myea */
     96 	switch (prom_version()) {
     97 	case PROM_OBP_V2:
     98 	case PROM_OBP_V3:
     99 	case PROM_OPENFIRM:
    100 		node = prom_instance_to_package(pd->fd);
    101 		break;
    102 	default:
    103 		node = 0;
    104 	}
    105 	prom_getether(node, io->myea);
    106 
    107 	return(0);
    108 }
    109 
    110 int
    111 netif_close(int fd)
    112 {
    113 	struct iodesc *io;
    114 	struct netif *ni;
    115 
    116 	if (fd != 0) {
    117 		errno = EBADF;
    118 		return(-1);
    119 	}
    120 
    121 	io = &sockets[fd];
    122 	ni = io->io_netif;
    123 	if (ni != NULL) {
    124 		ni->nif_devdata = NULL;
    125 		io->io_netif = NULL;
    126 	}
    127 	return(0);
    128 }
    129 
    130 /*
    131  * Send a packet.  The ether header is already there.
    132  * Return the length sent (or -1 on error).
    133  */
    134 ssize_t
    135 netif_put(struct iodesc *desc, void *pkt, size_t len)
    136 {
    137 	struct promdata *pd;
    138 	ssize_t rv;
    139 	size_t sendlen;
    140 
    141 	pd = (struct promdata *)((struct netif *)desc->io_netif)->nif_devdata;
    142 
    143 #ifdef NETIF_DEBUG
    144 	if (netif_debug) {
    145 		struct ether_header *eh;
    146 
    147 		printf("netif_put: desc=0x%x pkt=0x%x len=%d\n",
    148 			   desc, pkt, len);
    149 		eh = pkt;
    150 		printf("dst: %s ", ether_sprintf(eh->ether_dhost));
    151 		printf("src: %s ", ether_sprintf(eh->ether_shost));
    152 		printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
    153 	}
    154 #endif
    155 
    156 	sendlen = len;
    157 	if (sendlen < 60) {
    158 		sendlen = 60;
    159 #ifdef NETIF_DEBUG
    160 		printf("netif_put: length padded to %d\n", sendlen);
    161 #endif
    162 	}
    163 
    164 	rv = (*pd->xmit)(pd, pkt, sendlen);
    165 
    166 #ifdef NETIF_DEBUG
    167 	if (netif_debug)
    168 		printf("netif_put: xmit returned %d\n", rv);
    169 #endif
    170 
    171 	return rv;
    172 }
    173 
    174 /*
    175  * Receive a packet, including the ether header.
    176  * Return the total length received (or -1 on error).
    177  */
    178 ssize_t
    179 netif_get(struct iodesc *desc, void *pkt, size_t maxlen, saseconds_t timo)
    180 {
    181 	struct promdata *pd;
    182 	satime_t tick0;
    183 	ssize_t len;
    184 
    185 	pd = (struct promdata *)((struct netif *)desc->io_netif)->nif_devdata;
    186 
    187 #ifdef NETIF_DEBUG
    188 	if (netif_debug)
    189 		printf("netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n",
    190 			   pkt, maxlen, timo);
    191 #endif
    192 
    193 	tick0 = getsecs();
    194 
    195 	do {
    196 		len = (*pd->recv)(pd, pkt, maxlen);
    197 	} while ((len == 0) && ((getsecs() - tick0) < timo));
    198 
    199 #ifdef NETIF_DEBUG
    200 	if (netif_debug)
    201 		printf("netif_get: received len=%d\n", len);
    202 #endif
    203 
    204 	if (len < 12)
    205 		return -1;
    206 
    207 #ifdef NETIF_DEBUG
    208 	if (netif_debug) {
    209 		struct ether_header *eh = pkt;
    210 
    211 		printf("dst: %s ", ether_sprintf(eh->ether_dhost));
    212 		printf("src: %s ", ether_sprintf(eh->ether_shost));
    213 		printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
    214 	}
    215 #endif
    216 
    217 	return len;
    218 }
    219