1 /* $OpenBSD: netif_of.c,v 1.7 2014/08/21 14:24:08 mpi 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/if_ether.h> 50 51 #include <lib/libsa/stand.h> 52 #include <lib/libsa/net.h> 53 #include <lib/libsa/netif.h> 54 55 #include "ofdev.h" 56 #include "openfirm.h" 57 58 static struct netif netif_of; 59 60 struct iodesc sockets[SOPEN_MAX]; 61 62 struct iodesc * 63 socktodesc(int sock) 64 { 65 if (sock != 0) 66 return NULL; 67 return sockets; 68 } 69 70 int 71 netif_open(void *machdep_hint) 72 { 73 struct of_dev *op = machdep_hint; 74 struct iodesc *io; 75 int fd, error; 76 char addr[32]; 77 78 DNPRINTF(BOOT_D_OFNET, "netif_open..."); 79 80 /* find a free socket */ 81 io = sockets; 82 if (io->io_netif) { 83 DNPRINTF(BOOT_D_OFNET, "device busy\n"); 84 errno = ENFILE; 85 return -1; 86 } 87 bzero(io, sizeof *io); 88 89 netif_of.nif_devdata = op; 90 io->io_netif = &netif_of; 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 DNPRINTF(BOOT_D_OFNET, "OK\n"); 97 return 0; 98 } 99 100 int 101 netif_close(int fd) 102 { 103 struct iodesc *io; 104 struct netif *ni; 105 106 DNPRINTF(BOOT_D_OFNET, "netif_close(%x)...", fd); 107 108 if (fd != 0) { 109 DNPRINTF(BOOT_D_OFNET, "EBADF\n"); 110 errno = EBADF; 111 return -1; 112 } 113 114 io = &sockets[fd]; 115 ni = io->io_netif; 116 if (ni != NULL) { 117 ni->nif_devdata = NULL; 118 io->io_netif = NULL; 119 } 120 121 DNPRINTF(BOOT_D_OFNET, "OK\n"); 122 return 0; 123 } 124 125 /* 126 * Send a packet. The ether header is already there. 127 * Return the length sent (or -1 on error). 128 */ 129 ssize_t 130 netif_put(struct iodesc *desc, void *pkt, size_t len) 131 { 132 struct of_dev *op; 133 ssize_t rv; 134 size_t sendlen; 135 136 op = desc->io_netif->nif_devdata; 137 138 DNPRINTF(BOOT_D_OFNET, "netif_put: desc=0x%x pkt=0x%x len=%d\n", 139 desc, pkt, len); 140 DNPRINTF(BOOT_D_OFNET, "dst: %s, src: %s, type: 0x%x\n", 141 ether_sprintf(((struct ether_header *)pkt)->ether_dhost), 142 ether_sprintf(((struct ether_header *)pkt)->ether_shost), 143 ((struct ether_header *)pkt)->ether_type & 0xFFFF); 144 145 sendlen = len; 146 if (sendlen < 60) { 147 sendlen = 60; 148 DNPRINTF(BOOT_D_OFNET, "netif_put: length padded to %d\n", 149 sendlen); 150 } 151 152 rv = OF_write(op->handle, pkt, sendlen); 153 154 DNPRINTF(BOOT_D_OFNET, "netif_put: xmit returned %d\n", rv); 155 156 return rv; 157 } 158 159 /* 160 * Receive a packet, including the ether header. 161 * Return the total length received (or -1 on error). 162 */ 163 ssize_t 164 netif_get(struct iodesc *desc, void *pkt, size_t maxlen, time_t timo) 165 { 166 struct of_dev *op; 167 int tick0, tmo_ms; 168 int len; 169 170 op = desc->io_netif->nif_devdata; 171 172 DNPRINTF(BOOT_D_OFNET, "netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n", 173 pkt, maxlen, timo); 174 175 tmo_ms = timo * 1000; 176 tick0 = OF_milliseconds(); 177 178 do { 179 len = OF_read(op->handle, pkt, maxlen); 180 } while ((len == -2 || len == 0) && 181 (OF_milliseconds() - tick0 < tmo_ms)); 182 183 DNPRINTF(BOOT_D_OFNET, "netif_get: received len=%d\n", len); 184 185 if (len < 12) 186 return -1; 187 188 DNPRINTF(BOOT_D_OFNET, "dst: %s, src: %s, type: 0x%x\n", 189 ether_sprintf(((struct ether_header *)pkt)->ether_dhost), 190 ether_sprintf(((struct ether_header *)pkt)->ether_shost), 191 ((struct ether_header *)pkt)->ether_type & 0xFFFF); 192 193 return len; 194 } 195 196 /* 197 * Shouldn't really be here, but is used solely for networking, so... 198 */ 199 time_t 200 getsecs(void) 201 { 202 return OF_milliseconds() / 1000; 203 } 204