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