1 /* $OpenBSD: netif_of.c,v 1.4 2001/08/22 10:44:02 art Exp $ */ 2 /* $NetBSD: netif_of.c,v 1.1 2000/08/20 14:58:39 mrg Exp $ */ 3 4 /* 5 * Copyright (C) 1995 Wolfgang Solfrank. 6 * Copyright (C) 1995 TooLs GmbH. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by TooLs GmbH. 20 * 4. The name of TooLs GmbH may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * Open Firmware does most of the job for interfacing to the hardware, 37 * so it is easiest to just replace the netif module with 38 * this adaptation to the PROM network interface. 39 * 40 * Note: this is based in part on sys/arch/sparc/stand/netif_sun.c 41 */ 42 43 #include <sys/param.h> 44 #include <sys/socket.h> 45 46 #include <net/if.h> 47 48 #include <netinet/in.h> 49 #include <netinet/in_systm.h> 50 #include <netinet/if_ether.h> 51 52 #include <lib/libsa/stand.h> 53 #include <lib/libsa/net.h> 54 #include <lib/libsa/netif.h> 55 56 #include "ofdev.h" 57 #include "openfirm.h" 58 59 static struct netif netif_of; 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 return sockets; 70 } 71 72 int 73 netif_open(machdep_hint) 74 void *machdep_hint; 75 { 76 struct of_dev *op = machdep_hint; 77 struct iodesc *io; 78 int fd, error; 79 char addr[32]; 80 81 #ifdef NETIF_DEBUG 82 printf("netif_open..."); 83 #endif 84 /* find a free socket */ 85 io = sockets; 86 if (io->io_netif) { 87 #ifdef NETIF_DEBUG 88 printf("device busy\n"); 89 #endif 90 errno = ENFILE; 91 return -1; 92 } 93 bzero(io, sizeof *io); 94 95 netif_of.nif_devdata = op; 96 io->io_netif = &netif_of; 97 98 /* Put our ethernet address in io->myea */ 99 OF_getprop(OF_instance_to_package(op->handle), 100 "mac-address", io->myea, sizeof io->myea); 101 102 #ifdef NETIF_DEBUG 103 printf("OK\n"); 104 #endif 105 return 0; 106 } 107 108 int 109 netif_close(fd) 110 int fd; 111 { 112 struct iodesc *io; 113 struct netif *ni; 114 115 #ifdef NETIF_DEBUG 116 printf("netif_close(%x)...", fd); 117 #endif 118 if (fd != 0) { 119 #ifdef NETIF_DEBUG 120 printf("EBADF\n"); 121 #endif 122 errno = EBADF; 123 return -1; 124 } 125 126 io = &sockets[fd]; 127 ni = io->io_netif; 128 if (ni != NULL) { 129 ni->nif_devdata = NULL; 130 io->io_netif = NULL; 131 } 132 #ifdef NETIF_DEBUG 133 printf("OK\n"); 134 #endif 135 return 0; 136 } 137 138 /* 139 * Send a packet. The ether header is already there. 140 * Return the length sent (or -1 on error). 141 */ 142 ssize_t 143 netif_put(desc, pkt, len) 144 struct iodesc *desc; 145 void *pkt; 146 size_t len; 147 { 148 struct of_dev *op; 149 ssize_t rv; 150 size_t sendlen; 151 152 op = desc->io_netif->nif_devdata; 153 154 #ifdef NETIF_DEBUG 155 { 156 struct ether_header *eh; 157 158 printf("netif_put: desc=0x%x pkt=0x%x len=%d\n", 159 desc, pkt, len); 160 eh = pkt; 161 printf("dst: %s ", ether_sprintf(eh->ether_dhost)); 162 printf("src: %s ", ether_sprintf(eh->ether_shost)); 163 printf("type: 0x%x\n", eh->ether_type & 0xFFFF); 164 } 165 #endif 166 167 sendlen = len; 168 if (sendlen < 60) { 169 sendlen = 60; 170 #ifdef NETIF_DEBUG 171 printf("netif_put: length padded to %d\n", sendlen); 172 #endif 173 } 174 175 rv = OF_write(op->handle, pkt, sendlen); 176 177 #ifdef NETIF_DEBUG 178 printf("netif_put: xmit returned %d\n", rv); 179 #endif 180 181 return rv; 182 } 183 184 /* 185 * Receive a packet, including the ether header. 186 * Return the total length received (or -1 on error). 187 */ 188 ssize_t 189 netif_get(desc, pkt, maxlen, timo) 190 struct iodesc *desc; 191 void *pkt; 192 size_t maxlen; 193 time_t timo; 194 { 195 struct of_dev *op; 196 int tick0, tmo_ms; 197 int len; 198 199 op = desc->io_netif->nif_devdata; 200 201 #ifdef NETIF_DEBUG 202 printf("netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n", 203 pkt, maxlen, timo); 204 #endif 205 206 tmo_ms = timo * 1000; 207 tick0 = OF_milliseconds(); 208 209 do { 210 len = OF_read(op->handle, pkt, maxlen); 211 } while ((len == -2 || len == 0) && 212 (OF_milliseconds() - tick0 < tmo_ms)); 213 214 #ifdef NETIF_DEBUG 215 printf("netif_get: received len=%d\n", len); 216 #endif 217 218 if (len < 12) 219 return -1; 220 221 #ifdef NETIF_DEBUG 222 { 223 struct ether_header *eh = pkt; 224 225 printf("dst: %s ", ether_sprintf(eh->ether_dhost)); 226 printf("src: %s ", ether_sprintf(eh->ether_shost)); 227 printf("type: 0x%x\n", eh->ether_type & 0xFFFF); 228 } 229 #endif 230 231 return len; 232 } 233 234 /* 235 * Shouldn't really be here, but is used solely for networking, so... 236 */ 237 time_t 238 getsecs() 239 { 240 return OF_milliseconds() / 1000; 241 } 242