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