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