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