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