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