1 /* $NetBSD: enic.c,v 1.1 2011/01/26 01:18:54 pooka Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code was written by Alessandro Forin and Neil Pittman 8 * at Microsoft Research and contributed to The NetBSD Foundation 9 * by Microsoft Corporation. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* -------------------------------------------------------------------------- 34 * 35 * Module: 36 * 37 * enic.c 38 * 39 * Purpose: 40 * 41 * Driver for the Microsoft eNIC (eMIPS system) Ethernet 42 * 43 * Author: 44 * A. Forin (sandrof) 45 * 46 * References: 47 * Internal Microsoft specifications, file eNIC_Design.docx titled 48 * "eNIC: a simple Ethernet" Revision 4/30/99 49 * 50 * Giano simulation module, file Peripherals\enic.cpp 51 * 52 * Various other drivers I wrote for said hardware 53 * -------------------------------------------------------------------------- 54 */ 55 56 #include <sys/param.h> 57 #include <sys/types.h> 58 59 #include <net/if_ether.h> 60 #include <netinet/in.h> 61 #include <netinet/in_systm.h> 62 #include <netinet/ip.h> 63 64 #include <lib/libsa/stand.h> 65 #include <lib/libsa/net.h> 66 #include <lib/libsa/netif.h> 67 #include <lib/libkern/libkern.h> 68 69 #include <machine/emipsreg.h> 70 #define the_enic ((struct _Enic *)ETHERNET_DEFAULT_ADDRESS) 71 72 /* forward declarations */ 73 static int enicprobe (struct netif *, void *); 74 static int enicmatch (struct netif *, void *); 75 static void enicinit (struct iodesc *, void *); 76 static int enicget (struct iodesc *, void *, size_t, saseconds_t); 77 static int enicput (struct iodesc *, void *, size_t); 78 static void enicend (struct netif *); 79 80 #ifdef NET_DEBUG 81 static void dump_packet(void *, int); 82 #endif 83 84 /* BUGBUG do we have this? */ 85 #define kvtophys(_v_) ((paddr_t)(_v_) & ~0x80000000) 86 87 #define rpostone(_r_,_p_,_s_) \ 88 (_r_)->SizeAndFlags = ES_F_RECV | (_s_); \ 89 (_r_)->BufferAddressHi32 = 0; \ 90 (_r_)->BufferAddressLo32 = _p_; 91 #define tpostone(_r_,_p_,_s_) \ 92 (_r_)->SizeAndFlags = ES_F_XMIT | (_s_); \ 93 (_r_)->BufferAddressHi32 = 0; \ 94 (_r_)->BufferAddressLo32 = _p_; 95 96 97 /* Send a packet 98 */ 99 static int enic_putpkt(struct _Enic *regs, void *buf, int bytes) 100 { 101 paddr_t phys = kvtophys(buf); 102 103 tpostone(regs,phys,bytes); 104 105 /* poll till done? */ 106 //printf("\tenic: sent %d at %x\n",bytes,phys); 107 return bytes; 108 } 109 110 /* Get a packet 111 */ 112 int enic_getpkt(struct _Enic *regs, void *buf, int bytes, int timeo) 113 { 114 paddr_t phys; 115 unsigned int isr, saf, hi, lo, fl; 116 117 phys = kvtophys(buf); 118 rpostone(regs,phys,bytes); 119 120 //printf("\tenic: recv %d at %x\n",bytes,phys); 121 122 /* poll till we get some */ 123 timeo += getsecs(); 124 125 for (;;) { 126 127 /* anything there? */ 128 isr = regs->Control; 129 130 if (isr & EC_ERROR) { 131 printf("enic: internal error %x\n", isr); 132 regs->Control = EC_RESET; 133 break; 134 } 135 136 //printf("isr %x ",isr); 137 138 if ((isr & (EC_DONE|EC_OF_EMPTY)) == EC_DONE) { 139 140 /* beware, order matters */ 141 saf = regs->SizeAndFlags; 142 hi = regs->BufferAddressHi32; /* BUGBUG 64bit */ 143 lo = regs->BufferAddressLo32; /* this pops the fifo */ 144 145 fl = saf & (ES_F_MASK &~ ES_F_DONE); 146 147 if (fl == ES_F_RECV) 148 { 149 /* and we are done? */ 150 if (phys == lo) 151 return saf & ES_S_MASK; 152 } else if (fl == ES_F_XMIT) 153 { 154 ;/* nothing */ 155 } else if (fl != ES_F_CMD) 156 { 157 printf("enic: invalid saf=x%x (lo=%x)\n", saf, lo); 158 } 159 } 160 161 if (getsecs() >= timeo) { 162 //printf("enic: timeout\n"); 163 regs->Control = EC_RESET; 164 break; 165 } 166 } 167 168 return 0; 169 } 170 171 /* 172 */ 173 static int enic_getmac(struct _Enic *regs, uint8_t *mac) 174 { 175 uint8_t buffer[8]; 176 paddr_t phys = kvtophys(&buffer[0]); 177 int i; 178 179 regs->Control = EC_RESET; 180 Delay(1); 181 regs->Control = regs->Control & (~EC_RXDIS); 182 183 buffer[0] = ENIC_CMD_GET_ADDRESS; 184 185 //printf("%x:%x:%x:%x:%x:%x\n",buffer[0],buffer[1],buffer[2],buffer[3],buffer[4],buffer[5]); 186 187 regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD; 188 regs->BufferAddressHi32 = 0; 189 regs->BufferAddressLo32 = phys; /* go! */ 190 191 for (i = 0; i < 100; i++) { 192 Delay(100); 193 if (0 == (regs->Control & EC_OF_EMPTY)) 194 break; 195 } 196 if (i == 100) 197 return 0; 198 199 //printf("%x:%x:%x:%x:%x:%x\n",buffer[0],buffer[1],buffer[2],buffer[3],buffer[4],buffer[5]); 200 201 memcpy(mac,buffer,6); 202 return 1; 203 } 204 205 /* Exported interface 206 */ 207 int enic_present(int unit) 208 { 209 if ((unit != 0) || (the_enic->Tag != PMTTAG_ETHERNET)) 210 return 0; 211 212 return 1; 213 } 214 215 extern int try_bootp; 216 217 extern struct netif_stats enicstats[]; 218 struct netif_dif enicifs[] = { 219 /* dif_unit dif_nsel dif_stats dif_private */ 220 { 0, 1, &enicstats[0], the_enic, }, 221 }; 222 #define NENICIFS (sizeof(enicifs) / sizeof(enicifs[0])) 223 struct netif_stats enicstats[NENICIFS]; 224 225 struct netif_driver enic_netif_driver = { 226 "enic", /* netif_bname */ 227 enicmatch, /* netif_match */ 228 enicprobe, /* netif_probe */ 229 enicinit, /* netif_init */ 230 enicget, /* netif_get */ 231 enicput, /* netif_put */ 232 enicend, /* netif_end */ 233 enicifs, /* netif_ifs */ 234 NENICIFS /* netif_nifs */ 235 }; 236 237 static int 238 enicmatch(struct netif *nif, void *machdep_hint) 239 { 240 return (1); 241 } 242 243 /* NB: We refuse anything but unit==0 244 */ 245 static int 246 enicprobe(struct netif *nif, void *machdep_hint) 247 { 248 int unit = nif->nif_unit; 249 #ifdef NET_DEBUG 250 printf("enic%d: probe\n", unit); 251 #endif 252 return (enic_present(unit) ? 0 : 1); 253 } 254 255 static void 256 enicinit(struct iodesc *desc, void *machdep_hint) 257 { 258 #ifdef NET_DEBUG 259 struct netif *nif = (struct netif *)desc->io_netif; 260 int unit = nif->nif_driver->netif_ifs->dif_unit; 261 printf("enic%d: init %s\n", unit, machdep_hint); 262 #endif 263 264 /* 265 * Yes we wan tDHCP adn this is our MAC 266 */ 267 try_bootp = 1; 268 enic_getmac(the_enic,desc->myea); 269 desc->xid = 0xfe63d095; 270 } 271 272 273 static int 274 enicput(struct iodesc *desc, void *pkt, size_t len) 275 { 276 #ifdef NET_DEBUG 277 if (debug) 278 dump_packet(pkt,len); 279 #endif 280 281 return enic_putpkt(the_enic,pkt,len); 282 } 283 284 285 int 286 enicget(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout) 287 { 288 #ifdef NET_DEBUG 289 printf("enicget: %lx %lx\n",len,timeout); 290 #endif 291 return enic_getpkt(the_enic,pkt,len,timeout); 292 } 293 294 295 static void 296 enicend(struct netif *nif) 297 { 298 /* BUGBUG stop it in reset? */ 299 } 300 301 #ifdef NET_DEBUG 302 static void dump_packet(void *pkt, int len) 303 { 304 struct ether_header *eh = (struct ether_header *)pkt; 305 struct ip *ih = (struct ip *)(eh + 1); 306 307 printf("ether_dhost = %s\n", ether_sprintf(eh->ether_dhost)); 308 printf("ether_shost = %s\n", ether_sprintf(eh->ether_shost)); 309 printf("ether_type = 0x%x\n", ntohs(eh->ether_type)); 310 311 if (ntohs(eh->ether_type) == 0x0800) { 312 printf("ip packet version %d\n", ih->ip_v); 313 printf("source ip: 0x%x\n", ih->ip_src.s_addr); 314 printf("dest ip: 0x%x\n", ih->ip_dst.s_addr); 315 316 } 317 } 318 #endif 319