1/* $NetBSD: if_bug.c,v 1.3 2009/01/12 11:32:44 tsutsui 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/param.h> 33#include <sys/types.h> 34#include <sys/socket.h> 35 36#include <netinet/in.h> 37#include <netinet/in_systm.h> 38 39#include <net/if.h> 40#include <net/if_ether.h> 41 42#include <lib/libkern/libkern.h> 43#include <lib/libsa/stand.h> 44#include <lib/libsa/net.h> 45#include <lib/libsa/netif.h> 46 47#include "libsa.h" 48#include "bugsyscalls.h" 49 50static int bug_match(struct netif *, void *); 51static int bug_probe(struct netif *, void *); 52static void bug_init(struct iodesc *, void *); 53static int bug_get(struct iodesc *, void *, size_t, saseconds_t); 54static int bug_put(struct iodesc *, void *, size_t); 55static void bug_end(struct netif *); 56 57static struct netif_stats bug_stats; 58 59static struct netif_dif bug0_dif = { 60 0, 1, &bug_stats, 0, 0 61}; 62 63struct netif_driver bug_driver = { 64 "net", /* netif_bname */ 65 bug_match, /* match */ 66 bug_probe, /* probe */ 67 bug_init, /* init */ 68 bug_get, /* get */ 69 bug_put, /* put */ 70 bug_end, /* end */ 71 &bug0_dif, /* netif_ifs */ 72 1, /* netif_nifs */ 73}; 74 75struct bug_softc { 76 u_int32_t sc_pad1; 77 u_int8_t sc_rxbuf[ETHER_MAX_LEN]; 78 u_int32_t sc_pad2; 79 u_int8_t sc_txbuf[ETHER_MAX_LEN]; 80}; 81 82static struct bug_softc bug_softc; 83 84int 85bug_match(struct netif *nif, void *machdep_hint) 86{ 87 88 if (machdep_hint && 89 memcmp(bug_driver.netif_bname, machdep_hint, 90 strlen(bug_driver.netif_bname)) == 0) 91 return (1); 92 93 return (0); 94} 95 96int 97bug_probe(struct netif *nif, void *machdep_hint) 98{ 99 100 return (0); 101} 102 103void 104bug_init(struct iodesc *desc, void *machdep_hint) 105{ 106 struct netif *nif = desc->io_netif; 107 struct bug_netio nio; 108 109 nio.nc_clun = 0; 110 nio.nc_dlun = 0; 111 nio.nc_status = 0; 112 nio.nc_command = BUG_NETIO_CMD_GET_MAC; 113 nio.nc_buffer = desc->myea; 114 nio.nc_length = 6; 115 nio.nc_csr = 0; 116 117 if (bugsys_netio(&nio) != 0 || nio.nc_status != 0) 118 panic("bug_init: Failed to get MAC address! (code 0x%x)", 119 nio.nc_status); 120 121 nio.nc_clun = 0; 122 nio.nc_dlun = 0; 123 nio.nc_status = 0; 124 nio.nc_command = BUG_NETIO_CMD_FLUSH; 125 nio.nc_buffer = NULL; 126 nio.nc_length = 0; 127 nio.nc_csr = 0; 128 129 if (bugsys_netio(&nio) != 0 || nio.nc_status != 0) 130 panic("bug_init: Failed to flush netio device (code 0x%x)", 131 nio.nc_status); 132 133 printf("network: %s%d attached to %s\n", nif->nif_driver->netif_bname, 134 nif->nif_unit, ether_sprintf(desc->myea)); 135 136 nif->nif_devdata = &bug_softc; 137} 138 139int 140bug_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout) 141{ 142 struct netif *nif = desc->io_netif; 143 struct bug_softc *sc = nif->nif_devdata; 144 struct bug_netio nio; 145 146 nio.nc_clun = 0; 147 nio.nc_dlun = 0; 148 nio.nc_status = 0; 149 nio.nc_command = BUG_NETIO_CMD_RECEIVE; 150 nio.nc_buffer = sc->sc_rxbuf; 151 nio.nc_length = ETHER_MAX_LEN; 152 nio.nc_csr = 0; 153 154 if (bugsys_netio(&nio) != 0 || nio.nc_status != 0) { 155 printf("bug_get: Receive packet failed (code: 0x%x)\n", 156 nio.nc_status); 157 return (0); 158 } 159 160 if (nio.nc_length) { 161 memcpy(pkt, sc->sc_rxbuf, MIN(len, nio.nc_length)); 162 return (MIN(len, nio.nc_length)); 163 } 164 165 return (0); 166} 167 168int 169bug_put(struct iodesc *desc, void *pkt, size_t len) 170{ 171 struct netif *nif = desc->io_netif; 172 struct bug_softc *sc = nif->nif_devdata; 173 struct bug_netio nio; 174 175 memcpy(&sc->sc_txbuf, pkt, len); 176 177 nio.nc_clun = 0; 178 nio.nc_dlun = 0; 179 nio.nc_status = 0; 180 nio.nc_command = BUG_NETIO_CMD_TRANSMIT; 181 nio.nc_buffer = sc->sc_txbuf; 182 nio.nc_length = MAX(len, ETHER_MIN_LEN); 183 nio.nc_csr = 0; 184 185 if (bugsys_netio(&nio) != 0 || nio.nc_status != 0) { 186 printf("bug_put: Send packet failed (code: 0x%x)\n", 187 nio.nc_status); 188 return (0); 189 } 190 191 return (len); 192} 193 194void 195bug_end(struct netif *nif) 196{ 197 struct bug_netio nio; 198 199 nio.nc_clun = 0; 200 nio.nc_dlun = 0; 201 nio.nc_status = 0; 202 nio.nc_command = BUG_NETIO_CMD_FLUSH; 203 nio.nc_buffer = NULL; 204 nio.nc_length = 0; 205 nio.nc_csr = 0; 206 207 if (bugsys_netio(&nio) != 0 || nio.nc_status != 0) 208 printf("bug_end: netio failed (code: 0x%x)\n", nio.nc_status); 209} 210