1 /* $NetBSD: if_bug.c,v 1.1 2002/02/27 21:02:27 scw Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Steve C. Woodford. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/types.h> 41 #include <sys/socket.h> 42 43 #include <netinet/in.h> 44 #include <netinet/in_systm.h> 45 46 #include <net/if.h> 47 #include <net/if_ether.h> 48 49 #include <lib/libkern/libkern.h> 50 #include <lib/libsa/stand.h> 51 #include <lib/libsa/net.h> 52 #include <lib/libsa/netif.h> 53 54 #include "libsa.h" 55 #include "bugsyscalls.h" 56 57 static int bug_match(struct netif *, void *); 58 static int bug_probe(struct netif *, void *); 59 static void bug_init(struct iodesc *, void *); 60 static int bug_get(struct iodesc *, void *, size_t, time_t); 61 static int bug_put(struct iodesc *, void *, size_t); 62 static void bug_end(struct netif *); 63 64 static struct netif_stats bug_stats; 65 66 static struct netif_dif bug0_dif = { 67 0, 1, &bug_stats, 0, 0 68 }; 69 70 struct netif_driver bug_driver = { 71 "net", /* netif_bname */ 72 bug_match, /* match */ 73 bug_probe, /* probe */ 74 bug_init, /* init */ 75 bug_get, /* get */ 76 bug_put, /* put */ 77 bug_end, /* end */ 78 &bug0_dif, /* netif_ifs */ 79 1, /* netif_nifs */ 80 }; 81 82 struct bug_softc { 83 u_int32_t sc_pad1; 84 u_int8_t sc_rxbuf[ETHER_MAX_LEN]; 85 u_int32_t sc_pad2; 86 u_int8_t sc_txbuf[ETHER_MAX_LEN]; 87 }; 88 89 static struct bug_softc bug_softc; 90 91 int 92 bug_match(nif, machdep_hint) 93 struct netif *nif; 94 void *machdep_hint; 95 { 96 97 if (machdep_hint && 98 memcmp(bug_driver.netif_bname, machdep_hint, 99 strlen(bug_driver.netif_bname)) == 0) 100 return (1); 101 102 return (0); 103 } 104 105 int 106 bug_probe(nif, machdep_hint) 107 struct netif *nif; 108 void *machdep_hint; 109 { 110 111 return (0); 112 } 113 114 void 115 bug_init(desc, machdep_hint) 116 struct iodesc *desc; 117 void *machdep_hint; 118 { 119 struct netif *nif = desc->io_netif; 120 struct bug_netio nio; 121 122 nio.nc_clun = 0; 123 nio.nc_dlun = 0; 124 nio.nc_status = 0; 125 nio.nc_command = BUG_NETIO_CMD_GET_MAC; 126 nio.nc_buffer = desc->myea; 127 nio.nc_length = 6; 128 nio.nc_csr = 0; 129 130 if (bugsys_netio(&nio) != 0 || nio.nc_status != 0) 131 panic("bug_init: Failed to get MAC address! (code 0x%x)", 132 nio.nc_status); 133 134 nio.nc_clun = 0; 135 nio.nc_dlun = 0; 136 nio.nc_status = 0; 137 nio.nc_command = BUG_NETIO_CMD_FLUSH; 138 nio.nc_buffer = NULL; 139 nio.nc_length = 0; 140 nio.nc_csr = 0; 141 142 if (bugsys_netio(&nio) != 0 || nio.nc_status != 0) 143 panic("bug_init: Failed to flush netio device (code 0x%x)", 144 nio.nc_status); 145 146 printf("network: %s%d attached to %s\n", nif->nif_driver->netif_bname, 147 nif->nif_unit, ether_sprintf(desc->myea)); 148 149 nif->nif_devdata = &bug_softc; 150 } 151 152 int 153 bug_get(desc, pkt, len, timeout) 154 struct iodesc *desc; 155 void *pkt; 156 size_t len; 157 time_t timeout; 158 { 159 struct netif *nif = desc->io_netif; 160 struct bug_softc *sc = nif->nif_devdata; 161 struct bug_netio nio; 162 163 nio.nc_clun = 0; 164 nio.nc_dlun = 0; 165 nio.nc_status = 0; 166 nio.nc_command = BUG_NETIO_CMD_RECEIVE; 167 nio.nc_buffer = sc->sc_rxbuf; 168 nio.nc_length = ETHER_MAX_LEN; 169 nio.nc_csr = 0; 170 171 if (bugsys_netio(&nio) != 0 || nio.nc_status != 0) { 172 printf("bug_get: Receive packet failed (code: 0x%x)\n", 173 nio.nc_status); 174 return (0); 175 } 176 177 if (nio.nc_length) { 178 memcpy(pkt, sc->sc_rxbuf, MIN(len, nio.nc_length)); 179 return (MIN(len, nio.nc_length)); 180 } 181 182 return (0); 183 } 184 185 int 186 bug_put(desc, pkt, len) 187 struct iodesc *desc; 188 void *pkt; 189 size_t len; 190 { 191 struct netif *nif = desc->io_netif; 192 struct bug_softc *sc = nif->nif_devdata; 193 struct bug_netio nio; 194 195 memcpy(&sc->sc_txbuf, pkt, len); 196 197 nio.nc_clun = 0; 198 nio.nc_dlun = 0; 199 nio.nc_status = 0; 200 nio.nc_command = BUG_NETIO_CMD_TRANSMIT; 201 nio.nc_buffer = sc->sc_txbuf; 202 nio.nc_length = MAX(len, ETHER_MIN_LEN); 203 nio.nc_csr = 0; 204 205 if (bugsys_netio(&nio) != 0 || nio.nc_status != 0) { 206 printf("bug_put: Send packet failed (code: 0x%x)\n", 207 nio.nc_status); 208 return (0); 209 } 210 211 return (len); 212 } 213 214 void 215 bug_end(nif) 216 struct netif *nif; 217 { 218 struct bug_netio nio; 219 220 nio.nc_clun = 0; 221 nio.nc_dlun = 0; 222 nio.nc_status = 0; 223 nio.nc_command = BUG_NETIO_CMD_FLUSH; 224 nio.nc_buffer = NULL; 225 nio.nc_length = 0; 226 nio.nc_csr = 0; 227 228 if (bugsys_netio(&nio) != 0 || nio.nc_status != 0) 229 printf("bug_end: netio failed (code: 0x%x)\n", nio.nc_status); 230 } 231