enic.c revision 1.3 1 /* $NetBSD: enic.c,v 1.3 2014/02/24 22:31:56 christos 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 "start.h"
70
71 #include <machine/emipsreg.h>
72
73 #include "start.h"
74 #include "common.h"
75
76 #define the_enic ((struct _Enic *)ETHERNET_DEFAULT_ADDRESS)
77
78 /* forward declarations */
79 static int enicprobe (struct netif *, void *);
80 static int enicmatch (struct netif *, void *);
81 static void enicinit (struct iodesc *, void *);
82 static int enicget (struct iodesc *, void *, size_t, saseconds_t);
83 static int enicput (struct iodesc *, void *, size_t);
84 static void enicend (struct netif *);
85 int enic_getpkt(struct _Enic *regs, void *buf, int bytes, int timeo);
86 int enic_present(int unit);
87
88 #ifdef NET_DEBUG
89 static void dump_packet(void *, int);
90 #endif
91
92 /* BUGBUG do we have this? */
93 #define kvtophys(_v_) ((paddr_t)(_v_) & ~0x80000000)
94
95 #define rpostone(_r_,_p_,_s_) \
96 (_r_)->SizeAndFlags = ES_F_RECV | (_s_); \
97 (_r_)->BufferAddressHi32 = 0; \
98 (_r_)->BufferAddressLo32 = _p_;
99 #define tpostone(_r_,_p_,_s_) \
100 (_r_)->SizeAndFlags = ES_F_XMIT | (_s_); \
101 (_r_)->BufferAddressHi32 = 0; \
102 (_r_)->BufferAddressLo32 = _p_;
103
104
105 /* Send a packet
106 */
107 static int
108 enic_putpkt(struct _Enic *regs, void *buf, int bytes)
109 {
110 paddr_t phys = kvtophys(buf);
111
112 tpostone(regs,phys,bytes);
113
114 /* poll till done? */
115 //printf("\tenic: sent %d at %x\n",bytes,phys);
116 return bytes;
117 }
118
119 /* Get a packet
120 */
121 static int
122 enic_getpkt(struct _Enic *regs, void *buf, int bytes, int timeo)
123 {
124 paddr_t phys;
125 unsigned int isr, saf, hi, lo, fl;
126
127 phys = kvtophys(buf);
128 rpostone(regs,phys,bytes);
129
130 //printf("\tenic: recv %d at %x\n",bytes,phys);
131
132 /* poll till we get some */
133 timeo += getsecs();
134
135 for (;;) {
136
137 /* anything there? */
138 isr = regs->Control;
139
140 if (isr & EC_ERROR) {
141 printf("enic: internal error %x\n", isr);
142 regs->Control = EC_RESET;
143 break;
144 }
145
146 //printf("isr %x ",isr);
147
148 if ((isr & (EC_DONE|EC_OF_EMPTY)) == EC_DONE) {
149
150 /* beware, order matters */
151 saf = regs->SizeAndFlags;
152 hi = regs->BufferAddressHi32; /* BUGBUG 64bit */
153 lo = regs->BufferAddressLo32; /* this pops the fifo */
154 __USE(hi);
155
156 fl = saf & (ES_F_MASK &~ ES_F_DONE);
157
158 if (fl == ES_F_RECV)
159 {
160 /* and we are done? */
161 if (phys == lo)
162 return saf & ES_S_MASK;
163 } else if (fl == ES_F_XMIT)
164 {
165 ;/* nothing */
166 } else if (fl != ES_F_CMD)
167 {
168 printf("enic: invalid saf=x%x (lo=%x, hi=%x)\n", saf, lo, hi);
169 }
170 }
171
172 if (getsecs() >= timeo) {
173 //printf("enic: timeout\n");
174 regs->Control = EC_RESET;
175 break;
176 }
177 }
178
179 return 0;
180 }
181
182 /*
183 */
184 static int enic_getmac(struct _Enic *regs, uint8_t *mac)
185 {
186 uint8_t buffer[8];
187 paddr_t phys = kvtophys(&buffer[0]);
188 int i;
189
190 regs->Control = EC_RESET;
191 Delay(1);
192 regs->Control = regs->Control & (~EC_RXDIS);
193
194 buffer[0] = ENIC_CMD_GET_ADDRESS;
195
196 //printf("%x:%x:%x:%x:%x:%x\n",buffer[0],buffer[1],buffer[2],buffer[3],buffer[4],buffer[5]);
197
198 regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
199 regs->BufferAddressHi32 = 0;
200 regs->BufferAddressLo32 = phys; /* go! */
201
202 for (i = 0; i < 100; i++) {
203 Delay(100);
204 if (0 == (regs->Control & EC_OF_EMPTY))
205 break;
206 }
207 if (i == 100)
208 return 0;
209
210 //printf("%x:%x:%x:%x:%x:%x\n",buffer[0],buffer[1],buffer[2],buffer[3],buffer[4],buffer[5]);
211
212 memcpy(mac,buffer,6);
213 return 1;
214 }
215
216 /* Exported interface
217 */
218 int
219 enic_present(int unit)
220 {
221 if ((unit != 0) || (the_enic->Tag != PMTTAG_ETHERNET))
222 return 0;
223
224 return 1;
225 }
226
227 extern int try_bootp;
228
229 extern struct netif_stats enicstats[];
230 struct netif_dif enicifs[] = {
231 /* dif_unit dif_nsel dif_stats dif_private */
232 { 0, 1, &enicstats[0], the_enic, },
233 };
234 #define NENICIFS (sizeof(enicifs) / sizeof(enicifs[0]))
235 struct netif_stats enicstats[NENICIFS];
236
237 struct netif_driver enic_netif_driver = {
238 "enic", /* netif_bname */
239 enicmatch, /* netif_match */
240 enicprobe, /* netif_probe */
241 enicinit, /* netif_init */
242 enicget, /* netif_get */
243 enicput, /* netif_put */
244 enicend, /* netif_end */
245 enicifs, /* netif_ifs */
246 NENICIFS /* netif_nifs */
247 };
248
249 static int
250 enicmatch(struct netif *nif, void *machdep_hint)
251 {
252 return (1);
253 }
254
255 /* NB: We refuse anything but unit==0
256 */
257 static int
258 enicprobe(struct netif *nif, void *machdep_hint)
259 {
260 int unit = nif->nif_unit;
261 #ifdef NET_DEBUG
262 printf("enic%d: probe\n", unit);
263 #endif
264 return (enic_present(unit) ? 0 : 1);
265 }
266
267 static void
268 enicinit(struct iodesc *desc, void *machdep_hint)
269 {
270 #ifdef NET_DEBUG
271 struct netif *nif = (struct netif *)desc->io_netif;
272 int unit = nif->nif_driver->netif_ifs->dif_unit;
273 printf("enic%d: init %s\n", unit, machdep_hint);
274 #endif
275
276 /*
277 * Yes we wan tDHCP adn this is our MAC
278 */
279 try_bootp = 1;
280 enic_getmac(the_enic,desc->myea);
281 desc->xid = 0xfe63d095;
282 }
283
284
285 static int
286 enicput(struct iodesc *desc, void *pkt, size_t len)
287 {
288 #ifdef NET_DEBUG
289 if (debug)
290 dump_packet(pkt,len);
291 #endif
292
293 return enic_putpkt(the_enic,pkt,len);
294 }
295
296
297 int
298 enicget(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout)
299 {
300 #ifdef NET_DEBUG
301 printf("enicget: %lx %lx\n",len,timeout);
302 #endif
303 return enic_getpkt(the_enic,pkt,len,timeout);
304 }
305
306
307 static void
308 enicend(struct netif *nif)
309 {
310 /* BUGBUG stop it in reset? */
311 }
312
313 #ifdef NET_DEBUG
314 static void dump_packet(void *pkt, int len)
315 {
316 struct ether_header *eh = (struct ether_header *)pkt;
317 struct ip *ih = (struct ip *)(eh + 1);
318
319 printf("ether_dhost = %s\n", ether_sprintf(eh->ether_dhost));
320 printf("ether_shost = %s\n", ether_sprintf(eh->ether_shost));
321 printf("ether_type = 0x%x\n", ntohs(eh->ether_type));
322
323 if (ntohs(eh->ether_type) == 0x0800) {
324 printf("ip packet version %d\n", ih->ip_v);
325 printf("source ip: 0x%x\n", ih->ip_src.s_addr);
326 printf("dest ip: 0x%x\n", ih->ip_dst.s_addr);
327
328 }
329 }
330 #endif
331