1 | /* |
---|
2 | * RTEMS driver for Opencores Ethernet Controller |
---|
3 | * |
---|
4 | * Weakly based on dec21140 rtems driver and open_eth linux driver |
---|
5 | * Written by Jiri Gaisler, Gaisler Research |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | */ |
---|
12 | |
---|
13 | /* |
---|
14 | * This driver current only supports architectures with the old style |
---|
15 | * exception processing. The following checks try to keep this |
---|
16 | * from being compiled on systems which can't support this driver. |
---|
17 | * |
---|
18 | * NOTE: The i386, ARM, and PowerPC use a different interrupt API than |
---|
19 | * that used by this driver. |
---|
20 | */ |
---|
21 | |
---|
22 | #if defined(__i386__) || defined(__arm__) || defined(__PPC__) |
---|
23 | #define OPENETH_NOT_SUPPORTED |
---|
24 | #endif |
---|
25 | |
---|
26 | #if !defined(OPENETH_NOT_SUPPORTED) |
---|
27 | #include <bsp.h> |
---|
28 | #include <rtems.h> |
---|
29 | |
---|
30 | #include <bsp.h> |
---|
31 | |
---|
32 | #include <inttypes.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #include <stdio.h> |
---|
35 | #include <stdarg.h> |
---|
36 | #include <errno.h> |
---|
37 | |
---|
38 | #include <rtems/error.h> |
---|
39 | #include <rtems/rtems_bsdnet.h> |
---|
40 | #include <libchip/open_eth.h> |
---|
41 | |
---|
42 | #include <sys/param.h> |
---|
43 | #include <sys/mbuf.h> |
---|
44 | |
---|
45 | #include <sys/socket.h> |
---|
46 | #include <sys/sockio.h> |
---|
47 | #include <net/if.h> |
---|
48 | #include <netinet/in.h> |
---|
49 | #include <netinet/if_ether.h> |
---|
50 | |
---|
51 | #ifdef malloc |
---|
52 | #undef malloc |
---|
53 | #endif |
---|
54 | #ifdef free |
---|
55 | #undef free |
---|
56 | #endif |
---|
57 | |
---|
58 | #if defined(__m68k__) |
---|
59 | extern m68k_isr_entry set_vector( rtems_isr_entry, rtems_vector_number, int ); |
---|
60 | #else |
---|
61 | extern rtems_isr_entry set_vector( rtems_isr_entry, rtems_vector_number, int ); |
---|
62 | #endif |
---|
63 | |
---|
64 | /* |
---|
65 | #define OPEN_ETH_DEBUG |
---|
66 | */ |
---|
67 | |
---|
68 | #ifdef CPU_U32_FIX |
---|
69 | extern void ipalign(struct mbuf *m); |
---|
70 | #endif |
---|
71 | |
---|
72 | /* message descriptor entry */ |
---|
73 | struct MDTX |
---|
74 | { |
---|
75 | char *buf; |
---|
76 | }; |
---|
77 | |
---|
78 | struct MDRX |
---|
79 | { |
---|
80 | struct mbuf *m; |
---|
81 | }; |
---|
82 | |
---|
83 | /* |
---|
84 | * Number of OCs supported by this driver |
---|
85 | */ |
---|
86 | #define NOCDRIVER 1 |
---|
87 | |
---|
88 | /* |
---|
89 | * Receive buffer size -- Allow for a full ethernet packet including CRC |
---|
90 | */ |
---|
91 | #define RBUF_SIZE 1536 |
---|
92 | |
---|
93 | #define ET_MINLEN 64 /* minimum message length */ |
---|
94 | |
---|
95 | /* |
---|
96 | * RTEMS event used by interrupt handler to signal driver tasks. |
---|
97 | * This must not be any of the events used by the network task synchronization. |
---|
98 | */ |
---|
99 | #define INTERRUPT_EVENT RTEMS_EVENT_1 |
---|
100 | |
---|
101 | /* |
---|
102 | * RTEMS event used to start transmit daemon. |
---|
103 | * This must not be the same as INTERRUPT_EVENT. |
---|
104 | */ |
---|
105 | #define START_TRANSMIT_EVENT RTEMS_EVENT_2 |
---|
106 | |
---|
107 | /* event to send when tx buffers become available */ |
---|
108 | #define OPEN_ETH_TX_WAIT_EVENT RTEMS_EVENT_3 |
---|
109 | |
---|
110 | /* suspend when all TX descriptors exhausted */ |
---|
111 | /* |
---|
112 | #define OETH_SUSPEND_NOTXBUF |
---|
113 | */ |
---|
114 | |
---|
115 | #if (MCLBYTES < RBUF_SIZE) |
---|
116 | # error "Driver must have MCLBYTES > RBUF_SIZE" |
---|
117 | #endif |
---|
118 | |
---|
119 | /* |
---|
120 | * Per-device data |
---|
121 | */ |
---|
122 | struct open_eth_softc |
---|
123 | { |
---|
124 | |
---|
125 | struct arpcom arpcom; |
---|
126 | |
---|
127 | oeth_regs *regs; |
---|
128 | |
---|
129 | int acceptBroadcast; |
---|
130 | rtems_id rxDaemonTid; |
---|
131 | rtems_id txDaemonTid; |
---|
132 | |
---|
133 | unsigned int tx_ptr; |
---|
134 | unsigned int rx_ptr; |
---|
135 | unsigned int txbufs; |
---|
136 | unsigned int rxbufs; |
---|
137 | struct MDTX *txdesc; |
---|
138 | struct MDRX *rxdesc; |
---|
139 | rtems_vector_number vector; |
---|
140 | unsigned int en100MHz; |
---|
141 | |
---|
142 | /* |
---|
143 | * Statistics |
---|
144 | */ |
---|
145 | unsigned long rxInterrupts; |
---|
146 | unsigned long rxPackets; |
---|
147 | unsigned long rxLengthError; |
---|
148 | unsigned long rxNonOctet; |
---|
149 | unsigned long rxBadCRC; |
---|
150 | unsigned long rxOverrun; |
---|
151 | unsigned long rxMiss; |
---|
152 | unsigned long rxCollision; |
---|
153 | |
---|
154 | unsigned long txInterrupts; |
---|
155 | unsigned long txDeferred; |
---|
156 | unsigned long txHeartbeat; |
---|
157 | unsigned long txLateCollision; |
---|
158 | unsigned long txRetryLimit; |
---|
159 | unsigned long txUnderrun; |
---|
160 | unsigned long txLostCarrier; |
---|
161 | unsigned long txRawWait; |
---|
162 | }; |
---|
163 | |
---|
164 | static struct open_eth_softc oc; |
---|
165 | |
---|
166 | /* OPEN_ETH interrupt handler */ |
---|
167 | |
---|
168 | static rtems_isr |
---|
169 | open_eth_interrupt_handler (rtems_vector_number v) |
---|
170 | { |
---|
171 | uint32_t status; |
---|
172 | |
---|
173 | /* read and clear interrupt cause */ |
---|
174 | |
---|
175 | status = oc.regs->int_src; |
---|
176 | oc.regs->int_src = status; |
---|
177 | |
---|
178 | /* Frame received? */ |
---|
179 | |
---|
180 | if (status & (OETH_INT_RXF | OETH_INT_RXE)) |
---|
181 | { |
---|
182 | oc.rxInterrupts++; |
---|
183 | rtems_event_send (oc.rxDaemonTid, INTERRUPT_EVENT); |
---|
184 | } |
---|
185 | #ifdef OETH_SUSPEND_NOTXBUF |
---|
186 | if (status & (OETH_INT_MASK_TXB | OETH_INT_MASK_TXC | OETH_INT_MASK_TXE)) |
---|
187 | { |
---|
188 | oc.txInterrupts++; |
---|
189 | rtems_event_send (oc.txDaemonTid, OPEN_ETH_TX_WAIT_EVENT); |
---|
190 | } |
---|
191 | #endif |
---|
192 | /* |
---|
193 | #ifdef __leon__ |
---|
194 | LEON_Clear_interrupt(v-0x10); |
---|
195 | #endif |
---|
196 | */ |
---|
197 | } |
---|
198 | |
---|
199 | static uint32_t read_mii(uint32_t addr) |
---|
200 | { |
---|
201 | while (oc.regs->miistatus & OETH_MIISTATUS_BUSY) {} |
---|
202 | oc.regs->miiaddress = addr << 8; |
---|
203 | oc.regs->miicommand = OETH_MIICOMMAND_RSTAT; |
---|
204 | while (oc.regs->miistatus & OETH_MIISTATUS_BUSY) {} |
---|
205 | if (!(oc.regs->miistatus & OETH_MIISTATUS_NVALID)) |
---|
206 | return(oc.regs->miirx_data); |
---|
207 | else { |
---|
208 | printf("open_eth: failed to read mii\n"); |
---|
209 | return (0); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | static void write_mii(uint32_t addr, uint32_t data) |
---|
214 | { |
---|
215 | while (oc.regs->miistatus & OETH_MIISTATUS_BUSY) {} |
---|
216 | oc.regs->miiaddress = addr << 8; |
---|
217 | oc.regs->miitx_data = data; |
---|
218 | oc.regs->miicommand = OETH_MIICOMMAND_WCTRLDATA; |
---|
219 | while (oc.regs->miistatus & OETH_MIISTATUS_BUSY) {} |
---|
220 | } |
---|
221 | /* |
---|
222 | * Initialize the ethernet hardware |
---|
223 | */ |
---|
224 | static void |
---|
225 | open_eth_initialize_hardware (struct open_eth_softc *sc) |
---|
226 | { |
---|
227 | struct mbuf *m; |
---|
228 | int i; |
---|
229 | int mii_cr = 0; |
---|
230 | |
---|
231 | oeth_regs *regs; |
---|
232 | |
---|
233 | regs = sc->regs; |
---|
234 | |
---|
235 | /* Reset the controller. */ |
---|
236 | |
---|
237 | regs->ctrlmoder = 0; |
---|
238 | regs->moder = OETH_MODER_RST; /* Reset ON */ |
---|
239 | regs->moder = 0; /* Reset OFF */ |
---|
240 | |
---|
241 | /* reset PHY and wait for complettion */ |
---|
242 | mii_cr = 0x3300; |
---|
243 | if (!sc->en100MHz) mii_cr = 0; |
---|
244 | write_mii(0, mii_cr | 0x8000); |
---|
245 | while (read_mii(0) & 0x8000) {} |
---|
246 | if (!sc->en100MHz) write_mii(0, 0); |
---|
247 | mii_cr = read_mii(0); |
---|
248 | printf("open_eth: driver attached, PHY config : 0x%04" PRIx32 "\n", read_mii(0)); |
---|
249 | |
---|
250 | #ifdef OPEN_ETH_DEBUG |
---|
251 | printf("mii_cr: %04x\n", mii_cr); |
---|
252 | for (i=0;i<21;i++) |
---|
253 | printf("mii_reg %2d : 0x%04x\n", i, read_mii(i)); |
---|
254 | #endif |
---|
255 | |
---|
256 | /* Setting TXBD base to sc->txbufs */ |
---|
257 | |
---|
258 | regs->tx_bd_num = sc->txbufs; |
---|
259 | |
---|
260 | /* Initialize rx/tx pointers. */ |
---|
261 | |
---|
262 | sc->rx_ptr = 0; |
---|
263 | sc->tx_ptr = 0; |
---|
264 | |
---|
265 | /* Set min/max packet length */ |
---|
266 | regs->packet_len = 0x00400600; |
---|
267 | |
---|
268 | /* Set IPGT register to recomended value */ |
---|
269 | regs->ipgt = 0x00000015; |
---|
270 | |
---|
271 | /* Set IPGR1 register to recomended value */ |
---|
272 | regs->ipgr1 = 0x0000000c; |
---|
273 | |
---|
274 | /* Set IPGR2 register to recomended value */ |
---|
275 | regs->ipgr2 = 0x00000012; |
---|
276 | |
---|
277 | /* Set COLLCONF register to recomended value */ |
---|
278 | regs->collconf = 0x000f003f; |
---|
279 | |
---|
280 | /* initialize TX descriptors */ |
---|
281 | |
---|
282 | sc->txdesc = calloc(sc->txbufs, sizeof(*sc->txdesc)); |
---|
283 | for (i = 0; i < sc->txbufs; i++) |
---|
284 | { |
---|
285 | sc->regs->xd[i].len_status = OETH_TX_BD_PAD | OETH_TX_BD_CRC; |
---|
286 | sc->txdesc[i].buf = calloc(1, OETH_MAXBUF_LEN); |
---|
287 | #ifdef OPEN_ETH_DEBUG |
---|
288 | printf("TXBUF: %08x\n", (int) sc->txdesc[i].buf); |
---|
289 | #endif |
---|
290 | } |
---|
291 | sc->regs->xd[sc->txbufs - 1].len_status |= OETH_TX_BD_WRAP; |
---|
292 | |
---|
293 | /* allocate RX buffers */ |
---|
294 | |
---|
295 | sc->rxdesc = calloc(sc->rxbufs, sizeof(*sc->rxdesc)); |
---|
296 | for (i = 0; i < sc->rxbufs; i++) |
---|
297 | { |
---|
298 | |
---|
299 | MGETHDR (m, M_WAIT, MT_DATA); |
---|
300 | MCLGET (m, M_WAIT); |
---|
301 | m->m_pkthdr.rcvif = &sc->arpcom.ac_if; |
---|
302 | sc->rxdesc[i].m = m; |
---|
303 | sc->regs->xd[i + sc->txbufs].addr = mtod (m, uint32_t*); |
---|
304 | sc->regs->xd[i + sc->txbufs].len_status = |
---|
305 | OETH_RX_BD_EMPTY | OETH_RX_BD_IRQ; |
---|
306 | #ifdef OPEN_ETH_DEBUG |
---|
307 | printf("RXBUF: %08x\n", (int) sc->rxdesc[i].m); |
---|
308 | #endif |
---|
309 | } |
---|
310 | sc->regs->xd[sc->rxbufs + sc->txbufs - 1].len_status |= OETH_RX_BD_WRAP; |
---|
311 | |
---|
312 | |
---|
313 | /* set ethernet address. */ |
---|
314 | |
---|
315 | regs->mac_addr1 = sc->arpcom.ac_enaddr[0] << 8 | sc->arpcom.ac_enaddr[1]; |
---|
316 | regs->mac_addr0 = sc->arpcom.ac_enaddr[2] << 24 | sc->arpcom.ac_enaddr[3] << 16 | |
---|
317 | sc->arpcom.ac_enaddr[4] << 8 | sc->arpcom.ac_enaddr[5]; |
---|
318 | |
---|
319 | /* install interrupt vector */ |
---|
320 | set_vector (open_eth_interrupt_handler, sc->vector, 1); |
---|
321 | |
---|
322 | /* clear all pending interrupts */ |
---|
323 | |
---|
324 | regs->int_src = 0xffffffff; |
---|
325 | |
---|
326 | /* MAC mode register: PAD, IFG, CRCEN */ |
---|
327 | |
---|
328 | regs->moder = OETH_MODER_PAD | OETH_MODER_CRCEN | ((mii_cr & 0x100) << 2); |
---|
329 | |
---|
330 | /* enable interrupts */ |
---|
331 | |
---|
332 | regs->int_mask = OETH_INT_MASK_RXF | OETH_INT_MASK_RXE | OETH_INT_MASK_RXC; |
---|
333 | |
---|
334 | #ifdef OETH_SUSPEND_NOTXBUF |
---|
335 | regs->int_mask |= OETH_INT_MASK_TXB | OETH_INT_MASK_TXC | OETH_INT_MASK_TXE | OETH_INT_BUSY;*/ |
---|
336 | sc->regs->xd[(sc->txbufs - 1)/2].len_status |= OETH_TX_BD_IRQ; |
---|
337 | sc->regs->xd[sc->txbufs - 1].len_status |= OETH_TX_BD_IRQ; |
---|
338 | #endif |
---|
339 | |
---|
340 | regs->moder |= OETH_MODER_RXEN | OETH_MODER_TXEN; |
---|
341 | } |
---|
342 | |
---|
343 | static void |
---|
344 | open_eth_rxDaemon (void *arg) |
---|
345 | { |
---|
346 | struct ether_header *eh; |
---|
347 | struct open_eth_softc *dp = (struct open_eth_softc *) &oc; |
---|
348 | struct ifnet *ifp = &dp->arpcom.ac_if; |
---|
349 | struct mbuf *m; |
---|
350 | unsigned int len, len_status, bad; |
---|
351 | rtems_event_set events; |
---|
352 | |
---|
353 | |
---|
354 | for (;;) |
---|
355 | { |
---|
356 | |
---|
357 | rtems_bsdnet_event_receive (INTERRUPT_EVENT, |
---|
358 | RTEMS_WAIT | RTEMS_EVENT_ANY, |
---|
359 | RTEMS_NO_TIMEOUT, &events); |
---|
360 | #ifdef OPEN_ETH_DEBUG |
---|
361 | printf ("r\n"); |
---|
362 | #endif |
---|
363 | |
---|
364 | while (! |
---|
365 | ((len_status = |
---|
366 | dp->regs->xd[dp->rx_ptr+dp->txbufs].len_status) & OETH_RX_BD_EMPTY)) |
---|
367 | { |
---|
368 | bad = 0; |
---|
369 | if (len_status & (OETH_RX_BD_TOOLONG | OETH_RX_BD_SHORT)) |
---|
370 | { |
---|
371 | dp->rxLengthError++; |
---|
372 | bad = 1; |
---|
373 | } |
---|
374 | if (len_status & OETH_RX_BD_DRIBBLE) |
---|
375 | { |
---|
376 | dp->rxNonOctet++; |
---|
377 | bad = 1; |
---|
378 | } |
---|
379 | if (len_status & OETH_RX_BD_CRCERR) |
---|
380 | { |
---|
381 | dp->rxBadCRC++; |
---|
382 | bad = 1; |
---|
383 | } |
---|
384 | if (len_status & OETH_RX_BD_OVERRUN) |
---|
385 | { |
---|
386 | dp->rxOverrun++; |
---|
387 | bad = 1; |
---|
388 | } |
---|
389 | if (len_status & OETH_RX_BD_MISS) |
---|
390 | { |
---|
391 | dp->rxMiss++; |
---|
392 | bad = 1; |
---|
393 | } |
---|
394 | if (len_status & OETH_RX_BD_LATECOL) |
---|
395 | { |
---|
396 | dp->rxCollision++; |
---|
397 | bad = 1; |
---|
398 | } |
---|
399 | |
---|
400 | if (!bad) |
---|
401 | { |
---|
402 | /* pass on the packet in the receive buffer */ |
---|
403 | len = len_status >> 16; |
---|
404 | m = (struct mbuf *) (dp->rxdesc[dp->rx_ptr].m); |
---|
405 | m->m_len = m->m_pkthdr.len = |
---|
406 | len - sizeof (struct ether_header); |
---|
407 | eh = mtod (m, struct ether_header *); |
---|
408 | m->m_data += sizeof (struct ether_header); |
---|
409 | #ifdef CPU_U32_FIX |
---|
410 | ipalign(m); /* Align packet on 32-bit boundary */ |
---|
411 | #endif |
---|
412 | |
---|
413 | ether_input (ifp, eh, m); |
---|
414 | |
---|
415 | /* get a new mbuf */ |
---|
416 | MGETHDR (m, M_WAIT, MT_DATA); |
---|
417 | MCLGET (m, M_WAIT); |
---|
418 | m->m_pkthdr.rcvif = ifp; |
---|
419 | dp->rxdesc[dp->rx_ptr].m = m; |
---|
420 | dp->regs->xd[dp->rx_ptr + dp->txbufs].addr = |
---|
421 | (uint32_t*) mtod (m, void *); |
---|
422 | dp->rxPackets++; |
---|
423 | } |
---|
424 | |
---|
425 | dp->regs->xd[dp->rx_ptr+dp->txbufs].len_status = |
---|
426 | (dp->regs->xd[dp->rx_ptr+dp->txbufs].len_status & |
---|
427 | ~OETH_TX_BD_STATS) | OETH_TX_BD_READY; |
---|
428 | dp->rx_ptr = (dp->rx_ptr + 1) % dp->rxbufs; |
---|
429 | } |
---|
430 | } |
---|
431 | } |
---|
432 | |
---|
433 | static int inside = 0; |
---|
434 | static void |
---|
435 | sendpacket (struct ifnet *ifp, struct mbuf *m) |
---|
436 | { |
---|
437 | struct open_eth_softc *dp = ifp->if_softc; |
---|
438 | unsigned char *temp; |
---|
439 | struct mbuf *n; |
---|
440 | unsigned int len, len_status; |
---|
441 | |
---|
442 | if (inside) printf ("error: sendpacket re-entered!!\n"); |
---|
443 | inside = 1; |
---|
444 | /* |
---|
445 | * Waiting for Transmitter ready |
---|
446 | */ |
---|
447 | n = m; |
---|
448 | |
---|
449 | while (dp->regs->xd[dp->tx_ptr].len_status & OETH_TX_BD_READY) |
---|
450 | { |
---|
451 | #ifdef OETH_SUSPEND_NOTXBUF |
---|
452 | rtems_event_set events; |
---|
453 | rtems_bsdnet_event_receive (OPEN_ETH_TX_WAIT_EVENT, |
---|
454 | RTEMS_WAIT | RTEMS_EVENT_ANY, |
---|
455 | TOD_MILLISECONDS_TO_TICKS(500), &events); |
---|
456 | #endif |
---|
457 | } |
---|
458 | |
---|
459 | len = 0; |
---|
460 | temp = (unsigned char *) dp->txdesc[dp->tx_ptr].buf; |
---|
461 | dp->regs->xd[dp->tx_ptr].addr = (uint32_t*) temp; |
---|
462 | |
---|
463 | #ifdef OPEN_ETH_DEBUG |
---|
464 | printf("TXD: 0x%08x\n", (int) m->m_data); |
---|
465 | #endif |
---|
466 | for (;;) |
---|
467 | { |
---|
468 | #ifdef OPEN_ETH_DEBUG |
---|
469 | int i; |
---|
470 | printf("MBUF: 0x%08x : ", (int) m->m_data); |
---|
471 | for (i=0;i<m->m_len;i++) |
---|
472 | printf("%x%x", (m->m_data[i] >> 4) & 0x0ff, m->m_data[i] & 0x0ff); |
---|
473 | printf("\n"); |
---|
474 | #endif |
---|
475 | len += m->m_len; |
---|
476 | if (len <= RBUF_SIZE) |
---|
477 | memcpy ((void *) temp, (char *) m->m_data, m->m_len); |
---|
478 | temp += m->m_len; |
---|
479 | if ((m = m->m_next) == NULL) |
---|
480 | break; |
---|
481 | } |
---|
482 | |
---|
483 | m_freem (n); |
---|
484 | |
---|
485 | /* don't send long packets */ |
---|
486 | |
---|
487 | if (len <= RBUF_SIZE) { |
---|
488 | |
---|
489 | /* Clear all of the status flags. */ |
---|
490 | len_status = dp->regs->xd[dp->tx_ptr].len_status & ~OETH_TX_BD_STATS; |
---|
491 | |
---|
492 | /* If the frame is short, tell CPM to pad it. */ |
---|
493 | if (len < ET_MINLEN) { |
---|
494 | len_status |= OETH_TX_BD_PAD; |
---|
495 | len = ET_MINLEN; |
---|
496 | } |
---|
497 | else |
---|
498 | len_status &= ~OETH_TX_BD_PAD; |
---|
499 | |
---|
500 | /* write buffer descriptor length and status */ |
---|
501 | len_status &= 0x0000ffff; |
---|
502 | len_status |= (len << 16) | (OETH_TX_BD_READY | OETH_TX_BD_CRC); |
---|
503 | dp->regs->xd[dp->tx_ptr].len_status = len_status; |
---|
504 | dp->tx_ptr = (dp->tx_ptr + 1) % dp->txbufs; |
---|
505 | |
---|
506 | } |
---|
507 | inside = 0; |
---|
508 | } |
---|
509 | |
---|
510 | /* |
---|
511 | * Driver transmit daemon |
---|
512 | */ |
---|
513 | void |
---|
514 | open_eth_txDaemon (void *arg) |
---|
515 | { |
---|
516 | struct open_eth_softc *sc = (struct open_eth_softc *) arg; |
---|
517 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
518 | struct mbuf *m; |
---|
519 | rtems_event_set events; |
---|
520 | |
---|
521 | for (;;) |
---|
522 | { |
---|
523 | /* |
---|
524 | * Wait for packet |
---|
525 | */ |
---|
526 | |
---|
527 | rtems_bsdnet_event_receive (START_TRANSMIT_EVENT, |
---|
528 | RTEMS_EVENT_ANY | RTEMS_WAIT, |
---|
529 | RTEMS_NO_TIMEOUT, &events); |
---|
530 | #ifdef OPEN_ETH_DEBUG |
---|
531 | printf ("t\n"); |
---|
532 | #endif |
---|
533 | |
---|
534 | /* |
---|
535 | * Send packets till queue is empty |
---|
536 | */ |
---|
537 | for (;;) |
---|
538 | { |
---|
539 | /* |
---|
540 | * Get the next mbuf chain to transmit. |
---|
541 | */ |
---|
542 | IF_DEQUEUE (&ifp->if_snd, m); |
---|
543 | if (!m) |
---|
544 | break; |
---|
545 | sendpacket (ifp, m); |
---|
546 | } |
---|
547 | ifp->if_flags &= ~IFF_OACTIVE; |
---|
548 | } |
---|
549 | } |
---|
550 | |
---|
551 | |
---|
552 | static void |
---|
553 | open_eth_start (struct ifnet *ifp) |
---|
554 | { |
---|
555 | struct open_eth_softc *sc = ifp->if_softc; |
---|
556 | |
---|
557 | rtems_event_send (sc->txDaemonTid, START_TRANSMIT_EVENT); |
---|
558 | ifp->if_flags |= IFF_OACTIVE; |
---|
559 | } |
---|
560 | |
---|
561 | /* |
---|
562 | * Initialize and start the device |
---|
563 | */ |
---|
564 | static void |
---|
565 | open_eth_init (void *arg) |
---|
566 | { |
---|
567 | struct open_eth_softc *sc = arg; |
---|
568 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
569 | |
---|
570 | if (sc->txDaemonTid == 0) |
---|
571 | { |
---|
572 | |
---|
573 | /* |
---|
574 | * Set up OPEN_ETH hardware |
---|
575 | */ |
---|
576 | open_eth_initialize_hardware (sc); |
---|
577 | |
---|
578 | /* |
---|
579 | * Start driver tasks |
---|
580 | */ |
---|
581 | sc->rxDaemonTid = rtems_bsdnet_newproc ("DCrx", 4096, |
---|
582 | open_eth_rxDaemon, sc); |
---|
583 | sc->txDaemonTid = rtems_bsdnet_newproc ("DCtx", 4096, |
---|
584 | open_eth_txDaemon, sc); |
---|
585 | } |
---|
586 | |
---|
587 | /* |
---|
588 | * Tell the world that we're running. |
---|
589 | */ |
---|
590 | ifp->if_flags |= IFF_RUNNING; |
---|
591 | |
---|
592 | } |
---|
593 | |
---|
594 | /* |
---|
595 | * Stop the device |
---|
596 | */ |
---|
597 | static void |
---|
598 | open_eth_stop (struct open_eth_softc *sc) |
---|
599 | { |
---|
600 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
601 | |
---|
602 | ifp->if_flags &= ~IFF_RUNNING; |
---|
603 | |
---|
604 | sc->regs->moder = 0; /* RX/TX OFF */ |
---|
605 | sc->regs->moder = OETH_MODER_RST; /* Reset ON */ |
---|
606 | sc->regs->moder = 0; /* Reset OFF */ |
---|
607 | } |
---|
608 | |
---|
609 | |
---|
610 | /* |
---|
611 | * Show interface statistics |
---|
612 | */ |
---|
613 | static void |
---|
614 | open_eth_stats (struct open_eth_softc *sc) |
---|
615 | { |
---|
616 | printf (" Rx Packets:%-8lu", sc->rxPackets); |
---|
617 | printf (" Rx Interrupts:%-8lu", sc->rxInterrupts); |
---|
618 | printf (" Length:%-8lu", sc->rxLengthError); |
---|
619 | printf (" Non-octet:%-8lu\n", sc->rxNonOctet); |
---|
620 | printf (" Bad CRC:%-8lu", sc->rxBadCRC); |
---|
621 | printf (" Overrun:%-8lu", sc->rxOverrun); |
---|
622 | printf (" Miss:%-8lu", sc->rxMiss); |
---|
623 | printf (" Collision:%-8lu\n", sc->rxCollision); |
---|
624 | |
---|
625 | printf (" Tx Interrupts:%-8lu", sc->txInterrupts); |
---|
626 | printf (" Deferred:%-8lu", sc->txDeferred); |
---|
627 | printf (" Missed Hearbeat:%-8lu\n", sc->txHeartbeat); |
---|
628 | printf (" No Carrier:%-8lu", sc->txLostCarrier); |
---|
629 | printf ("Retransmit Limit:%-8lu", sc->txRetryLimit); |
---|
630 | printf (" Late Collision:%-8lu\n", sc->txLateCollision); |
---|
631 | printf (" Underrun:%-8lu", sc->txUnderrun); |
---|
632 | printf (" Raw output wait:%-8lu\n", sc->txRawWait); |
---|
633 | } |
---|
634 | |
---|
635 | /* |
---|
636 | * Driver ioctl handler |
---|
637 | */ |
---|
638 | static int |
---|
639 | open_eth_ioctl (struct ifnet *ifp, ioctl_command_t command, caddr_t data) |
---|
640 | { |
---|
641 | struct open_eth_softc *sc = ifp->if_softc; |
---|
642 | int error = 0; |
---|
643 | |
---|
644 | switch (command) |
---|
645 | { |
---|
646 | case SIOCGIFADDR: |
---|
647 | case SIOCSIFADDR: |
---|
648 | ether_ioctl (ifp, command, data); |
---|
649 | break; |
---|
650 | |
---|
651 | case SIOCSIFFLAGS: |
---|
652 | switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) |
---|
653 | { |
---|
654 | case IFF_RUNNING: |
---|
655 | open_eth_stop (sc); |
---|
656 | break; |
---|
657 | |
---|
658 | case IFF_UP: |
---|
659 | open_eth_init (sc); |
---|
660 | break; |
---|
661 | |
---|
662 | case IFF_UP | IFF_RUNNING: |
---|
663 | open_eth_stop (sc); |
---|
664 | open_eth_init (sc); |
---|
665 | break; |
---|
666 | |
---|
667 | default: |
---|
668 | break; |
---|
669 | } |
---|
670 | break; |
---|
671 | |
---|
672 | case SIO_RTEMS_SHOW_STATS: |
---|
673 | open_eth_stats (sc); |
---|
674 | break; |
---|
675 | |
---|
676 | /* |
---|
677 | * FIXME: All sorts of multicast commands need to be added here! |
---|
678 | */ |
---|
679 | default: |
---|
680 | error = EINVAL; |
---|
681 | break; |
---|
682 | } |
---|
683 | |
---|
684 | return error; |
---|
685 | } |
---|
686 | |
---|
687 | /* |
---|
688 | * Attach an OPEN_ETH driver to the system |
---|
689 | */ |
---|
690 | int |
---|
691 | rtems_open_eth_driver_attach (struct rtems_bsdnet_ifconfig *config, |
---|
692 | open_eth_configuration_t * chip) |
---|
693 | { |
---|
694 | struct open_eth_softc *sc; |
---|
695 | struct ifnet *ifp; |
---|
696 | int mtu; |
---|
697 | int unitNumber; |
---|
698 | char *unitName; |
---|
699 | |
---|
700 | /* parse driver name */ |
---|
701 | if ((unitNumber = rtems_bsdnet_parse_driver_name (config, &unitName)) < 0) |
---|
702 | return 0; |
---|
703 | |
---|
704 | sc = &oc; |
---|
705 | ifp = &sc->arpcom.ac_if; |
---|
706 | memset (sc, 0, sizeof (*sc)); |
---|
707 | |
---|
708 | if (config->hardware_address) |
---|
709 | { |
---|
710 | memcpy (sc->arpcom.ac_enaddr, config->hardware_address, |
---|
711 | ETHER_ADDR_LEN); |
---|
712 | } |
---|
713 | else |
---|
714 | { |
---|
715 | memset (sc->arpcom.ac_enaddr, 0x08, ETHER_ADDR_LEN); |
---|
716 | } |
---|
717 | |
---|
718 | if (config->mtu) |
---|
719 | mtu = config->mtu; |
---|
720 | else |
---|
721 | mtu = ETHERMTU; |
---|
722 | |
---|
723 | sc->acceptBroadcast = !config->ignore_broadcast; |
---|
724 | sc->regs = (void *) chip->base_address; |
---|
725 | sc->vector = chip->vector; |
---|
726 | sc->txbufs = chip->txd_count; |
---|
727 | sc->rxbufs = chip->rxd_count; |
---|
728 | sc->en100MHz = chip->en100MHz; |
---|
729 | |
---|
730 | |
---|
731 | /* |
---|
732 | * Set up network interface values |
---|
733 | */ |
---|
734 | ifp->if_softc = sc; |
---|
735 | ifp->if_unit = unitNumber; |
---|
736 | ifp->if_name = unitName; |
---|
737 | ifp->if_mtu = mtu; |
---|
738 | ifp->if_init = open_eth_init; |
---|
739 | ifp->if_ioctl = open_eth_ioctl; |
---|
740 | ifp->if_start = open_eth_start; |
---|
741 | ifp->if_output = ether_output; |
---|
742 | ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX; |
---|
743 | if (ifp->if_snd.ifq_maxlen == 0) |
---|
744 | ifp->if_snd.ifq_maxlen = ifqmaxlen; |
---|
745 | |
---|
746 | /* |
---|
747 | * Attach the interface |
---|
748 | */ |
---|
749 | if_attach (ifp); |
---|
750 | ether_ifattach (ifp); |
---|
751 | |
---|
752 | #ifdef OPEN_ETH_DEBUG |
---|
753 | printf ("OPEN_ETH : driver has been attached\n"); |
---|
754 | #endif |
---|
755 | return 1; |
---|
756 | }; |
---|
757 | |
---|
758 | #endif /* OPENETH_NOT_SUPPORTED */ |
---|