1 | |
---|
2 | /* |
---|
3 | * RTEMS/TCPIP driver for MCF5329 Fast Ethernet Controller |
---|
4 | * |
---|
5 | * TO DO: Check network stack code -- force longword alignment of all tx mbufs? |
---|
6 | */ |
---|
7 | |
---|
8 | #include <bsp.h> |
---|
9 | #include <stdio.h> |
---|
10 | #include <errno.h> |
---|
11 | #include <stdarg.h> |
---|
12 | #include <string.h> |
---|
13 | #include <rtems.h> |
---|
14 | #include <rtems/error.h> |
---|
15 | #include <rtems/rtems_bsdnet.h> |
---|
16 | |
---|
17 | #include <sys/param.h> |
---|
18 | #include <sys/mbuf.h> |
---|
19 | #include <sys/socket.h> |
---|
20 | #include <sys/sockio.h> |
---|
21 | |
---|
22 | #include <net/ethernet.h> |
---|
23 | #include <net/if.h> |
---|
24 | |
---|
25 | #include <netinet/in.h> |
---|
26 | #include <netinet/if_ether.h> |
---|
27 | |
---|
28 | /* |
---|
29 | * Number of interfaces supported by this driver |
---|
30 | */ |
---|
31 | #define NIFACES 1 |
---|
32 | |
---|
33 | #define FEC_INTC0_TX_VECTOR (64+36) |
---|
34 | #define FEC_INTC0_RX_VECTOR (64+40) |
---|
35 | |
---|
36 | /* |
---|
37 | * Default number of buffer descriptors set aside for this driver. |
---|
38 | * The number of transmit buffer descriptors has to be quite large |
---|
39 | * since a single frame often uses three or more buffer descriptors. |
---|
40 | */ |
---|
41 | #define RX_BUF_COUNT 32 |
---|
42 | #define TX_BUF_COUNT 20 |
---|
43 | #define TX_BD_PER_BUF 3 |
---|
44 | |
---|
45 | #define INET_ADDR_MAX_BUF_SIZE (sizeof "255.255.255.255") |
---|
46 | |
---|
47 | /* |
---|
48 | * RTEMS event used by interrupt handler to signal daemons. |
---|
49 | * This must *not* be the same event used by the TCP/IP task synchronization. |
---|
50 | */ |
---|
51 | #define TX_INTERRUPT_EVENT RTEMS_EVENT_1 |
---|
52 | #define RX_INTERRUPT_EVENT RTEMS_EVENT_1 |
---|
53 | |
---|
54 | /* |
---|
55 | * RTEMS event used to start transmit daemon. |
---|
56 | * This must not be the same as INTERRUPT_EVENT. |
---|
57 | */ |
---|
58 | #define START_TRANSMIT_EVENT RTEMS_EVENT_2 |
---|
59 | |
---|
60 | /* |
---|
61 | * Receive buffer size -- Allow for a full ethernet packet plus CRC (1518). |
---|
62 | * Round off to nearest multiple of RBUF_ALIGN. |
---|
63 | */ |
---|
64 | #define MAX_MTU_SIZE 1518 |
---|
65 | #define RBUF_ALIGN 4 |
---|
66 | #define RBUF_SIZE ((MAX_MTU_SIZE + RBUF_ALIGN) & ~RBUF_ALIGN) |
---|
67 | |
---|
68 | #if (MCLBYTES < RBUF_SIZE) |
---|
69 | # error "Driver must have MCLBYTES > RBUF_SIZE" |
---|
70 | #endif |
---|
71 | |
---|
72 | typedef struct mcf5329BufferDescriptor_ |
---|
73 | { |
---|
74 | volatile uint16_t status; |
---|
75 | uint16_t length; |
---|
76 | volatile void *buffer; |
---|
77 | } mcf5329BufferDescriptor_t; |
---|
78 | |
---|
79 | /* |
---|
80 | * Per-device data |
---|
81 | */ |
---|
82 | struct mcf5329_enet_struct |
---|
83 | { |
---|
84 | struct arpcom arpcom; |
---|
85 | struct mbuf **rxMbuf; |
---|
86 | struct mbuf **txMbuf; |
---|
87 | int acceptBroadcast; |
---|
88 | int rxBdCount; |
---|
89 | int txBdCount; |
---|
90 | int txBdHead; |
---|
91 | int txBdTail; |
---|
92 | int txBdActiveCount; |
---|
93 | mcf5329BufferDescriptor_t *rxBdBase; |
---|
94 | mcf5329BufferDescriptor_t *txBdBase; |
---|
95 | rtems_id rxDaemonTid; |
---|
96 | rtems_id txDaemonTid; |
---|
97 | |
---|
98 | /* |
---|
99 | * Statistics |
---|
100 | */ |
---|
101 | unsigned long rxInterrupts; |
---|
102 | unsigned long txInterrupts; |
---|
103 | unsigned long txRawWait; |
---|
104 | unsigned long txRealign; |
---|
105 | }; |
---|
106 | static struct mcf5329_enet_struct enet_driver[NIFACES]; |
---|
107 | |
---|
108 | static rtems_isr mcf5329_fec_rx_interrupt_handler(rtems_vector_number v) |
---|
109 | { |
---|
110 | MCF_FEC_EIR = MCF_FEC_EIR_RXF; |
---|
111 | MCF_FEC_EIMR &= ~MCF_FEC_EIMR_RXF; |
---|
112 | enet_driver[0].rxInterrupts++; |
---|
113 | rtems_event_send(enet_driver[0].rxDaemonTid, RX_INTERRUPT_EVENT); |
---|
114 | } |
---|
115 | |
---|
116 | static rtems_isr mcf5329_fec_tx_interrupt_handler(rtems_vector_number v) |
---|
117 | { |
---|
118 | MCF_FEC_EIR = MCF_FEC_EIR_TXF; |
---|
119 | MCF_FEC_EIMR &= ~MCF_FEC_EIMR_TXF; |
---|
120 | enet_driver[0].txInterrupts++; |
---|
121 | rtems_event_send(enet_driver[0].txDaemonTid, TX_INTERRUPT_EVENT); |
---|
122 | } |
---|
123 | |
---|
124 | /* |
---|
125 | * Allocate buffer descriptors from (non-cached) on-chip static RAM |
---|
126 | * Ensure 128-bit (16-byte) alignment |
---|
127 | */ |
---|
128 | static mcf5329BufferDescriptor_t *mcf5329_bd_allocate(unsigned int count) |
---|
129 | { |
---|
130 | extern char _CoreSRamBase[]; |
---|
131 | static mcf5329BufferDescriptor_t *bdp = |
---|
132 | (mcf5329BufferDescriptor_t *) _CoreSRamBase; |
---|
133 | mcf5329BufferDescriptor_t *p = bdp; |
---|
134 | |
---|
135 | bdp += count; |
---|
136 | if ((int) bdp & 0xF) |
---|
137 | bdp = |
---|
138 | (mcf5329BufferDescriptor_t *) ((char *) bdp + (16 - ((int) bdp & 0xF))); |
---|
139 | return p; |
---|
140 | } |
---|
141 | |
---|
142 | #if UNUSED |
---|
143 | |
---|
144 | /* |
---|
145 | * Read MII register |
---|
146 | * Busy-waits, but transfer time should be short! |
---|
147 | */ |
---|
148 | static int getMII(int phyNumber, int regNumber) |
---|
149 | { |
---|
150 | MCF_FEC_MMFR = (0x1 << 30) | |
---|
151 | (0x2 << 28) | (phyNumber << 23) | (regNumber << 18) | (0x2 << 16); |
---|
152 | while ((MCF_FEC_EIR & MCF_FEC_EIR_MII) == 0) ; |
---|
153 | MCF_FEC_EIR = MCF_FEC_EIR_MII; |
---|
154 | return MCF_FEC_MMFR & 0xFFFF; |
---|
155 | } |
---|
156 | #endif |
---|
157 | |
---|
158 | /* |
---|
159 | * Write MII register |
---|
160 | * Busy-waits, but transfer time should be short! |
---|
161 | */ |
---|
162 | static void setMII(int phyNumber, int regNumber, int value) |
---|
163 | { |
---|
164 | MCF_FEC_MMFR = (0x1 << 30) | |
---|
165 | (0x1 << 28) | |
---|
166 | (phyNumber << 23) | (regNumber << 18) | (0x2 << 16) | (value & 0xFFFF); |
---|
167 | while ((MCF_FEC_EIR & MCF_FEC_EIR_MII) == 0) ; |
---|
168 | MCF_FEC_EIR = MCF_FEC_EIR_MII; |
---|
169 | } |
---|
170 | |
---|
171 | static void mcf5329_fec_initialize_hardware(struct mcf5329_enet_struct *sc) |
---|
172 | { |
---|
173 | int i; |
---|
174 | const unsigned char *hwaddr = 0; |
---|
175 | rtems_status_code status; |
---|
176 | rtems_isr_entry old_handler; |
---|
177 | uint32_t clock_speed = bsp_get_BUS_clock_speed(); |
---|
178 | |
---|
179 | /* |
---|
180 | * Issue reset to FEC |
---|
181 | */ |
---|
182 | MCF_FEC_ECR = MCF_FEC_ECR_RESET; |
---|
183 | rtems_task_wake_after(1); |
---|
184 | MCF_FEC_ECR = 0; |
---|
185 | |
---|
186 | /* |
---|
187 | * Configuration of I/O ports is done outside of this function |
---|
188 | */ |
---|
189 | #if 0 |
---|
190 | imm->gpio.pbcnt |= MCF_GPIO_PBCNT_SET_FEC; /* Set up port b FEC pins */ |
---|
191 | #endif |
---|
192 | |
---|
193 | /* |
---|
194 | * Set our physical address |
---|
195 | */ |
---|
196 | hwaddr = sc->arpcom.ac_enaddr; |
---|
197 | MCF_FEC_PALR = (hwaddr[0] << 24) | (hwaddr[1] << 16) | |
---|
198 | (hwaddr[2] << 8) | (hwaddr[3] << 0); |
---|
199 | MCF_FEC_PAUR = (hwaddr[4] << 24) | (hwaddr[5] << 16); |
---|
200 | |
---|
201 | /* |
---|
202 | * Clear the hash table |
---|
203 | */ |
---|
204 | MCF_FEC_GAUR = 0; |
---|
205 | MCF_FEC_GALR = 0; |
---|
206 | |
---|
207 | /* |
---|
208 | * Set up receive buffer size |
---|
209 | */ |
---|
210 | MCF_FEC_EMRBR = 1520; /* Standard Ethernet */ |
---|
211 | |
---|
212 | /* |
---|
213 | * Allocate mbuf pointers |
---|
214 | */ |
---|
215 | sc->rxMbuf = malloc(sc->rxBdCount * sizeof *sc->rxMbuf, M_MBUF, M_NOWAIT); |
---|
216 | sc->txMbuf = malloc(sc->txBdCount * sizeof *sc->txMbuf, M_MBUF, M_NOWAIT); |
---|
217 | if (!sc->rxMbuf || !sc->txMbuf) |
---|
218 | rtems_panic("No memory for mbuf pointers"); |
---|
219 | |
---|
220 | /* |
---|
221 | * Set receiver and transmitter buffer descriptor bases |
---|
222 | */ |
---|
223 | sc->rxBdBase = mcf5329_bd_allocate(sc->rxBdCount); |
---|
224 | sc->txBdBase = mcf5329_bd_allocate(sc->txBdCount); |
---|
225 | MCF_FEC_ERDSR = (int) sc->rxBdBase; |
---|
226 | MCF_FEC_ETDSR = (int) sc->txBdBase; |
---|
227 | |
---|
228 | /* |
---|
229 | * Set up Receive Control Register: |
---|
230 | * Not promiscuous |
---|
231 | * MII mode |
---|
232 | * Full duplex |
---|
233 | * No loopback |
---|
234 | */ |
---|
235 | MCF_FEC_RCR = MCF_FEC_RCR_MAX_FL(MAX_MTU_SIZE) | MCF_FEC_RCR_MII_MODE; |
---|
236 | |
---|
237 | /* |
---|
238 | * Set up Transmit Control Register: |
---|
239 | * Full duplex |
---|
240 | * No heartbeat |
---|
241 | */ |
---|
242 | MCF_FEC_TCR = MCF_FEC_TCR_FDEN; |
---|
243 | |
---|
244 | /* |
---|
245 | * Initialize statistic counters |
---|
246 | */ |
---|
247 | MCF_FEC_MIBC = MCF_FEC_MIBC_MIB_DISABLE; |
---|
248 | { |
---|
249 | vuint32 *vuip = &MCF_FEC_RMON_T_DROP; |
---|
250 | |
---|
251 | while (vuip <= &MCF_FEC_IEEE_R_OCTETS_OK) |
---|
252 | *vuip++ = 0; |
---|
253 | } |
---|
254 | MCF_FEC_MIBC = 0; |
---|
255 | |
---|
256 | /* |
---|
257 | * Set MII speed to <= 2.5 MHz |
---|
258 | */ |
---|
259 | i = (clock_speed + 5000000 - 1) / 5000000; |
---|
260 | MCF_FEC_MSCR = MCF_FEC_MSCR_MII_SPEED(i); |
---|
261 | |
---|
262 | /* |
---|
263 | * Set PHYS to 100 Mb/s, full duplex |
---|
264 | */ |
---|
265 | setMII(1, 0, 0x2100); |
---|
266 | |
---|
267 | /* |
---|
268 | * Set up receive buffer descriptors |
---|
269 | */ |
---|
270 | for (i = 0; i < sc->rxBdCount; i++) |
---|
271 | (sc->rxBdBase + i)->status = 0; |
---|
272 | |
---|
273 | /* |
---|
274 | * Set up transmit buffer descriptors |
---|
275 | */ |
---|
276 | for (i = 0; i < sc->txBdCount; i++) { |
---|
277 | sc->txBdBase[i].status = 0; |
---|
278 | sc->txMbuf[i] = NULL; |
---|
279 | } |
---|
280 | sc->txBdHead = sc->txBdTail = 0; |
---|
281 | sc->txBdActiveCount = 0; |
---|
282 | |
---|
283 | /* |
---|
284 | * Set up interrupts |
---|
285 | */ |
---|
286 | status = |
---|
287 | rtems_interrupt_catch(mcf5329_fec_tx_interrupt_handler, |
---|
288 | FEC_INTC0_TX_VECTOR, &old_handler); |
---|
289 | if (status != RTEMS_SUCCESSFUL) |
---|
290 | rtems_panic("Can't attach MCF FEC TX interrupt handler: %s\n", |
---|
291 | rtems_status_text(status)); |
---|
292 | status = |
---|
293 | rtems_interrupt_catch(mcf5329_fec_rx_interrupt_handler, |
---|
294 | FEC_INTC0_RX_VECTOR, &old_handler); |
---|
295 | if (status != RTEMS_SUCCESSFUL) |
---|
296 | rtems_panic("Can't attach MCF FEC RX interrupt handler: %s\n", |
---|
297 | rtems_status_text(status)); |
---|
298 | MCF_INTC0_ICR36 = MCF_INTC_ICR_IL(FEC_IRQ_LEVEL); |
---|
299 | MCF_INTC0_IMRH &= ~(MCF_INTC_IMRH_INT_MASK36); |
---|
300 | MCF_INTC0_ICR40 = MCF_INTC_ICR_IL(FEC_IRQ_LEVEL); |
---|
301 | MCF_INTC0_IMRH &= ~(MCF_INTC_IMRH_INT_MASK40); |
---|
302 | } |
---|
303 | |
---|
304 | /* |
---|
305 | * Get the MAC address from the hardware. |
---|
306 | */ |
---|
307 | static void |
---|
308 | fec_get_mac_address(volatile struct mcf5329_enet_struct *sc, |
---|
309 | unsigned char *hwaddr) |
---|
310 | { |
---|
311 | unsigned long addr; |
---|
312 | |
---|
313 | addr = MCF_FEC_PALR; |
---|
314 | |
---|
315 | hwaddr[0] = (addr >> 24) & 0xff; |
---|
316 | hwaddr[1] = (addr >> 16) & 0xff; |
---|
317 | hwaddr[2] = (addr >> 8) & 0xff; |
---|
318 | hwaddr[3] = (addr >> 0) & 0xff; |
---|
319 | |
---|
320 | addr = MCF_FEC_PAUR; |
---|
321 | |
---|
322 | hwaddr[4] = (addr >> 24) & 0xff; |
---|
323 | hwaddr[5] = (addr >> 16) & 0xff; |
---|
324 | } |
---|
325 | |
---|
326 | /* |
---|
327 | * Soak up buffer descriptors that have been sent. |
---|
328 | */ |
---|
329 | static void fec_retire_tx_bd(volatile struct mcf5329_enet_struct *sc) |
---|
330 | { |
---|
331 | struct mbuf *m, *n; |
---|
332 | |
---|
333 | while ((sc->txBdActiveCount != 0) |
---|
334 | && ((sc->txBdBase[sc->txBdTail].status & MCF_FEC_TxBD_R) == 0)) { |
---|
335 | m = sc->txMbuf[sc->txBdTail]; |
---|
336 | MFREE(m, n); |
---|
337 | if (++sc->txBdTail == sc->txBdCount) |
---|
338 | sc->txBdTail = 0; |
---|
339 | sc->txBdActiveCount--; |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|
343 | static void fec_rxDaemon(void *arg) |
---|
344 | { |
---|
345 | volatile struct mcf5329_enet_struct *sc = |
---|
346 | (volatile struct mcf5329_enet_struct *) arg; |
---|
347 | struct ifnet *ifp = (struct ifnet *) &sc->arpcom.ac_if; |
---|
348 | struct mbuf *m; |
---|
349 | volatile uint16_t status; |
---|
350 | volatile mcf5329BufferDescriptor_t *rxBd; |
---|
351 | int rxBdIndex; |
---|
352 | |
---|
353 | /* |
---|
354 | * Allocate space for incoming packets and start reception |
---|
355 | */ |
---|
356 | for (rxBdIndex = 0;;) { |
---|
357 | rxBd = sc->rxBdBase + rxBdIndex; |
---|
358 | MGETHDR(m, M_WAIT, MT_DATA); |
---|
359 | MCLGET(m, M_WAIT); |
---|
360 | m->m_pkthdr.rcvif = ifp; |
---|
361 | sc->rxMbuf[rxBdIndex] = m; |
---|
362 | rxBd->buffer = mtod(m, void *); |
---|
363 | |
---|
364 | rxBd->status = MCF_FEC_RxBD_E; |
---|
365 | if (++rxBdIndex == sc->rxBdCount) { |
---|
366 | rxBd->status |= MCF_FEC_RxBD_W; |
---|
367 | break; |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | /* |
---|
372 | * Input packet handling loop |
---|
373 | */ |
---|
374 | /* Indicate we have some ready buffers available */ |
---|
375 | MCF_FEC_RDAR = MCF_FEC_RDAR_R_DES_ACTIVE; |
---|
376 | |
---|
377 | rxBdIndex = 0; |
---|
378 | for (;;) { |
---|
379 | rxBd = sc->rxBdBase + rxBdIndex; |
---|
380 | |
---|
381 | /* |
---|
382 | * Wait for packet if there's not one ready |
---|
383 | */ |
---|
384 | if ((status = rxBd->status) & MCF_FEC_RxBD_E) { |
---|
385 | /* |
---|
386 | * Clear old events. |
---|
387 | */ |
---|
388 | MCF_FEC_EIR = MCF_FEC_EIR_RXF; |
---|
389 | |
---|
390 | /* |
---|
391 | * Wait for packet to arrive. |
---|
392 | * Check the buffer descriptor before waiting for the event. |
---|
393 | * This catches the case when a packet arrives between the |
---|
394 | * `if' above, and the clearing of the RXF bit in the EIR. |
---|
395 | */ |
---|
396 | while ((status = rxBd->status) & MCF_FEC_RxBD_E) { |
---|
397 | rtems_event_set events; |
---|
398 | int level; |
---|
399 | |
---|
400 | rtems_interrupt_disable(level); |
---|
401 | MCF_FEC_EIMR |= MCF_FEC_EIMR_RXF; |
---|
402 | rtems_interrupt_enable(level); |
---|
403 | rtems_bsdnet_event_receive(RX_INTERRUPT_EVENT, |
---|
404 | RTEMS_WAIT | RTEMS_EVENT_ANY, |
---|
405 | RTEMS_NO_TIMEOUT, &events); |
---|
406 | } |
---|
407 | } |
---|
408 | |
---|
409 | /* |
---|
410 | * Check that packet is valid |
---|
411 | */ |
---|
412 | if (status & MCF_FEC_RxBD_L) { |
---|
413 | /* |
---|
414 | * Pass the packet up the chain. |
---|
415 | * FIXME: Packet filtering hook could be done here. |
---|
416 | */ |
---|
417 | struct ether_header *eh; |
---|
418 | int len = rxBd->length - sizeof(uint32_t);; |
---|
419 | |
---|
420 | m = sc->rxMbuf[rxBdIndex]; |
---|
421 | |
---|
422 | rtems_cache_invalidate_multiple_data_lines(m->m_data, len); |
---|
423 | |
---|
424 | m->m_len = m->m_pkthdr.len = len - sizeof(struct ether_header); |
---|
425 | eh = mtod(m, struct ether_header *); |
---|
426 | m->m_data += sizeof(struct ether_header); |
---|
427 | ether_input(ifp, eh, m); |
---|
428 | |
---|
429 | /* |
---|
430 | * Allocate a new mbuf |
---|
431 | */ |
---|
432 | MGETHDR(m, M_WAIT, MT_DATA); |
---|
433 | MCLGET(m, M_WAIT); |
---|
434 | m->m_pkthdr.rcvif = ifp; |
---|
435 | sc->rxMbuf[rxBdIndex] = m; |
---|
436 | rxBd->buffer = mtod(m, void *); |
---|
437 | } |
---|
438 | |
---|
439 | /* |
---|
440 | * Reenable the buffer descriptor |
---|
441 | */ |
---|
442 | rxBd->status = (status & MCF_FEC_RxBD_W) | MCF_FEC_RxBD_E; |
---|
443 | MCF_FEC_RDAR = MCF_FEC_RDAR_R_DES_ACTIVE; |
---|
444 | |
---|
445 | /* |
---|
446 | * Move to next buffer descriptor |
---|
447 | */ |
---|
448 | if (++rxBdIndex == sc->rxBdCount) |
---|
449 | rxBdIndex = 0; |
---|
450 | } |
---|
451 | } |
---|
452 | |
---|
453 | static void fec_sendpacket(struct ifnet *ifp, struct mbuf *m) |
---|
454 | { |
---|
455 | struct mcf5329_enet_struct *sc = ifp->if_softc; |
---|
456 | volatile mcf5329BufferDescriptor_t *firstTxBd, *txBd; |
---|
457 | uint16_t status; |
---|
458 | int nAdded; |
---|
459 | |
---|
460 | /* |
---|
461 | * Free up buffer descriptors |
---|
462 | */ |
---|
463 | fec_retire_tx_bd(sc); |
---|
464 | |
---|
465 | /* |
---|
466 | * Set up the transmit buffer descriptors. |
---|
467 | * No need to pad out short packets since the |
---|
468 | * hardware takes care of that automatically. |
---|
469 | * No need to copy the packet to a contiguous buffer |
---|
470 | * since the hardware is capable of scatter/gather DMA. |
---|
471 | */ |
---|
472 | nAdded = 0; |
---|
473 | firstTxBd = sc->txBdBase + sc->txBdHead; |
---|
474 | |
---|
475 | for (;;) { |
---|
476 | /* |
---|
477 | * Wait for buffer descriptor to become available |
---|
478 | */ |
---|
479 | if ((sc->txBdActiveCount + nAdded) == sc->txBdCount) { |
---|
480 | /* |
---|
481 | * Clear old events. |
---|
482 | */ |
---|
483 | MCF_FEC_EIR = MCF_FEC_EIR_TXF; |
---|
484 | |
---|
485 | /* |
---|
486 | * Wait for buffer descriptor to become available. |
---|
487 | * Check for buffer descriptors before waiting for the event. |
---|
488 | * This catches the case when a buffer became available between |
---|
489 | * the `if' above, and the clearing of the TXF bit in the EIR. |
---|
490 | */ |
---|
491 | fec_retire_tx_bd(sc); |
---|
492 | while ((sc->txBdActiveCount + nAdded) == sc->txBdCount) { |
---|
493 | rtems_event_set events; |
---|
494 | int level; |
---|
495 | |
---|
496 | rtems_interrupt_disable(level); |
---|
497 | MCF_FEC_EIMR |= MCF_FEC_EIMR_TXF; |
---|
498 | rtems_interrupt_enable(level); |
---|
499 | sc->txRawWait++; |
---|
500 | rtems_bsdnet_event_receive(TX_INTERRUPT_EVENT, |
---|
501 | RTEMS_WAIT | RTEMS_EVENT_ANY, |
---|
502 | RTEMS_NO_TIMEOUT, &events); |
---|
503 | fec_retire_tx_bd(sc); |
---|
504 | } |
---|
505 | } |
---|
506 | |
---|
507 | /* |
---|
508 | * Don't set the READY flag on the first fragment |
---|
509 | * until the whole packet has been readied. |
---|
510 | */ |
---|
511 | status = nAdded ? MCF_FEC_TxBD_R : 0; |
---|
512 | |
---|
513 | /* |
---|
514 | * The IP fragmentation routine in ip_output |
---|
515 | * can produce fragments with zero length. |
---|
516 | */ |
---|
517 | txBd = sc->txBdBase + sc->txBdHead; |
---|
518 | if (m->m_len) { |
---|
519 | char *p = mtod(m, char *); |
---|
520 | |
---|
521 | /* |
---|
522 | * Stupid FEC can't handle misaligned data! |
---|
523 | * Given the way that mbuf's are layed out it should be |
---|
524 | * safe to shuffle the data down like this..... |
---|
525 | * Perhaps this code could be improved with a "Duff's Device". |
---|
526 | */ |
---|
527 | if ((int) p & 0x3) { |
---|
528 | int l = m->m_len; |
---|
529 | char *dest = p - ((int) p & 0x3); |
---|
530 | uint16_t *o = (uint16_t *) dest, *i = (uint16_t *) p; |
---|
531 | |
---|
532 | while (l > 0) { |
---|
533 | *o++ = *i++; |
---|
534 | l -= sizeof(uint16_t); |
---|
535 | } |
---|
536 | p = dest; |
---|
537 | sc->txRealign++; |
---|
538 | } |
---|
539 | |
---|
540 | txBd->buffer = p; |
---|
541 | txBd->length = m->m_len; |
---|
542 | |
---|
543 | rtems_cache_flush_multiple_data_lines(txBd->buffer, txBd->length); |
---|
544 | |
---|
545 | sc->txMbuf[sc->txBdHead] = m; |
---|
546 | nAdded++; |
---|
547 | if (++sc->txBdHead == sc->txBdCount) { |
---|
548 | status |= MCF_FEC_TxBD_W; |
---|
549 | sc->txBdHead = 0; |
---|
550 | } |
---|
551 | m = m->m_next; |
---|
552 | } else { |
---|
553 | /* |
---|
554 | * Just toss empty mbufs |
---|
555 | */ |
---|
556 | struct mbuf *n; |
---|
557 | |
---|
558 | MFREE(m, n); |
---|
559 | m = n; |
---|
560 | } |
---|
561 | if (m == NULL) { |
---|
562 | if (nAdded) { |
---|
563 | txBd->status = status | MCF_FEC_TxBD_R |
---|
564 | | MCF_FEC_TxBD_L | MCF_FEC_TxBD_TC; |
---|
565 | if (nAdded > 1) |
---|
566 | firstTxBd->status |= MCF_FEC_TxBD_R; |
---|
567 | MCF_FEC_TDAR = MCF_FEC_TDAR_X_DES_ACTIVE; |
---|
568 | sc->txBdActiveCount += nAdded; |
---|
569 | } |
---|
570 | break; |
---|
571 | } |
---|
572 | txBd->status = status; |
---|
573 | } |
---|
574 | } |
---|
575 | |
---|
576 | void fec_txDaemon(void *arg) |
---|
577 | { |
---|
578 | struct mcf5329_enet_struct *sc = (struct mcf5329_enet_struct *) arg; |
---|
579 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
580 | struct mbuf *m; |
---|
581 | rtems_event_set events; |
---|
582 | |
---|
583 | for (;;) { |
---|
584 | /* |
---|
585 | * Wait for packet |
---|
586 | */ |
---|
587 | rtems_bsdnet_event_receive(START_TRANSMIT_EVENT, |
---|
588 | RTEMS_EVENT_ANY | RTEMS_WAIT, |
---|
589 | RTEMS_NO_TIMEOUT, &events); |
---|
590 | |
---|
591 | /* |
---|
592 | * Send packets till queue is empty |
---|
593 | */ |
---|
594 | for (;;) { |
---|
595 | /* |
---|
596 | * Get the next mbuf chain to transmit. |
---|
597 | */ |
---|
598 | IF_DEQUEUE(&ifp->if_snd, m); |
---|
599 | if (!m) |
---|
600 | break; |
---|
601 | fec_sendpacket(ifp, m); |
---|
602 | } |
---|
603 | ifp->if_flags &= ~IFF_OACTIVE; |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | /* |
---|
608 | * Send packet (caller provides header). |
---|
609 | */ |
---|
610 | static void mcf5329_enet_start(struct ifnet *ifp) |
---|
611 | { |
---|
612 | struct mcf5329_enet_struct *sc = ifp->if_softc; |
---|
613 | |
---|
614 | rtems_event_send(sc->txDaemonTid, START_TRANSMIT_EVENT); |
---|
615 | ifp->if_flags |= IFF_OACTIVE; |
---|
616 | } |
---|
617 | |
---|
618 | static void fec_init(void *arg) |
---|
619 | { |
---|
620 | struct mcf5329_enet_struct *sc = arg; |
---|
621 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
622 | |
---|
623 | if (sc->txDaemonTid == 0) { |
---|
624 | /* |
---|
625 | * Set up hardware |
---|
626 | */ |
---|
627 | mcf5329_fec_initialize_hardware(sc); |
---|
628 | |
---|
629 | /* |
---|
630 | * Start driver tasks |
---|
631 | */ |
---|
632 | sc->txDaemonTid = rtems_bsdnet_newproc("FECtx", 4096, fec_txDaemon, sc); |
---|
633 | sc->rxDaemonTid = rtems_bsdnet_newproc("FECrx", 4096, fec_rxDaemon, sc); |
---|
634 | } |
---|
635 | |
---|
636 | /* |
---|
637 | * Set flags appropriately |
---|
638 | */ |
---|
639 | if (ifp->if_flags & IFF_PROMISC) |
---|
640 | MCF_FEC_RCR |= MCF_FEC_RCR_PROM; |
---|
641 | else |
---|
642 | MCF_FEC_RCR &= ~MCF_FEC_RCR_PROM; |
---|
643 | |
---|
644 | /* |
---|
645 | * Tell the world that we're running. |
---|
646 | */ |
---|
647 | ifp->if_flags |= IFF_RUNNING; |
---|
648 | |
---|
649 | /* |
---|
650 | * Enable receiver and transmitter |
---|
651 | */ |
---|
652 | MCF_FEC_ECR = MCF_FEC_ECR_ETHER_EN; |
---|
653 | } |
---|
654 | |
---|
655 | static void fec_stop(struct mcf5329_enet_struct *sc) |
---|
656 | { |
---|
657 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
658 | |
---|
659 | ifp->if_flags &= ~IFF_RUNNING; |
---|
660 | |
---|
661 | /* |
---|
662 | * Shut down receiver and transmitter |
---|
663 | */ |
---|
664 | MCF_FEC_ECR = 0x0; |
---|
665 | } |
---|
666 | |
---|
667 | /* |
---|
668 | * Show interface statistics |
---|
669 | */ |
---|
670 | static void enet_stats(struct mcf5329_enet_struct *sc) |
---|
671 | { |
---|
672 | printf(" Rx Interrupts:%-10lu", sc->rxInterrupts); |
---|
673 | printf("Rx Packet Count:%-10lu", MCF_FEC_RMON_R_PACKETS); |
---|
674 | printf(" Rx Broadcast:%-10lu\n", MCF_FEC_RMON_R_BC_PKT); |
---|
675 | printf(" Rx Multicast:%-10lu", MCF_FEC_RMON_R_MC_PKT); |
---|
676 | printf("CRC/Align error:%-10lu", MCF_FEC_RMON_R_CRC_ALIGN); |
---|
677 | printf(" Rx Undersize:%-10lu\n", MCF_FEC_RMON_R_UNDERSIZE); |
---|
678 | printf(" Rx Oversize:%-10lu", MCF_FEC_RMON_R_OVERSIZE); |
---|
679 | printf(" Rx Fragment:%-10lu", MCF_FEC_RMON_R_FRAG); |
---|
680 | printf(" Rx Jabber:%-10lu\n", MCF_FEC_RMON_R_JAB); |
---|
681 | printf(" Rx 64:%-10lu", MCF_FEC_RMON_R_P64); |
---|
682 | printf(" Rx 65-127:%-10lu", MCF_FEC_RMON_R_P65TO127); |
---|
683 | printf(" Rx 128-255:%-10lu\n", MCF_FEC_RMON_R_P128TO255); |
---|
684 | printf(" Rx 256-511:%-10lu", MCF_FEC_RMON_R_P256TO511); |
---|
685 | printf(" Rx 511-1023:%-10lu", MCF_FEC_RMON_R_512TO1023); |
---|
686 | printf(" Rx 1024-2047:%-10lu\n", MCF_FEC_RMON_R_1024TO2047); |
---|
687 | printf(" Rx >=2048:%-10lu", MCF_FEC_RMON_R_P_GTE2048); |
---|
688 | printf(" Rx Octets:%-10lu", MCF_FEC_RMON_R_OCTETS); |
---|
689 | printf(" Rx Dropped:%-10lu\n", MCF_FEC_IEEE_R_DROP); |
---|
690 | printf(" Rx frame OK:%-10lu", MCF_FEC_IEEE_R_FRAME_OK); |
---|
691 | printf(" Rx CRC error:%-10lu", MCF_FEC_IEEE_R_CRC); |
---|
692 | printf(" Rx Align error:%-10lu\n", MCF_FEC_IEEE_R_ALIGN); |
---|
693 | printf(" FIFO Overflow:%-10lu", MCF_FEC_IEEE_R_MACERR); |
---|
694 | printf("Rx Pause Frames:%-10lu", MCF_FEC_IEEE_R_FDXFC); |
---|
695 | printf(" Rx Octets OK:%-10lu\n", MCF_FEC_IEEE_R_OCTETS_OK); |
---|
696 | printf(" Tx Interrupts:%-10lu", sc->txInterrupts); |
---|
697 | printf("Tx Output Waits:%-10lu", sc->txRawWait); |
---|
698 | printf("Tx Realignments:%-10lu\n", sc->txRealign); |
---|
699 | printf(" Tx Unaccounted:%-10lu", MCF_FEC_RMON_T_DROP); |
---|
700 | printf("Tx Packet Count:%-10lu", MCF_FEC_RMON_T_PACKETS); |
---|
701 | printf(" Tx Broadcast:%-10lu\n", MCF_FEC_RMON_T_BC_PKT); |
---|
702 | printf(" Tx Multicast:%-10lu", MCF_FEC_RMON_T_MC_PKT); |
---|
703 | printf("CRC/Align error:%-10lu", MCF_FEC_RMON_T_CRC_ALIGN); |
---|
704 | printf(" Tx Undersize:%-10lu\n", MCF_FEC_RMON_T_UNDERSIZE); |
---|
705 | printf(" Tx Oversize:%-10lu", MCF_FEC_RMON_T_OVERSIZE); |
---|
706 | printf(" Tx Fragment:%-10lu", MCF_FEC_RMON_T_FRAG); |
---|
707 | printf(" Tx Jabber:%-10lu\n", MCF_FEC_RMON_T_JAB); |
---|
708 | printf(" Tx Collisions:%-10lu", MCF_FEC_RMON_T_COL); |
---|
709 | printf(" Tx 64:%-10lu", MCF_FEC_RMON_T_P64); |
---|
710 | printf(" Tx 65-127:%-10lu\n", MCF_FEC_RMON_T_P65TO127); |
---|
711 | printf(" Tx 128-255:%-10lu", MCF_FEC_RMON_T_P128TO255); |
---|
712 | printf(" Tx 256-511:%-10lu", MCF_FEC_RMON_T_P256TO511); |
---|
713 | printf(" Tx 511-1023:%-10lu\n", MCF_FEC_RMON_T_P512TO1023); |
---|
714 | printf(" Tx 1024-2047:%-10lu", MCF_FEC_RMON_T_P1024TO2047); |
---|
715 | printf(" Tx >=2048:%-10lu", MCF_FEC_RMON_T_P_GTE2048); |
---|
716 | printf(" Tx Octets:%-10lu\n", MCF_FEC_RMON_T_OCTETS); |
---|
717 | printf(" Tx Dropped:%-10lu", MCF_FEC_IEEE_T_DROP); |
---|
718 | printf(" Tx Frame OK:%-10lu", MCF_FEC_IEEE_T_FRAME_OK); |
---|
719 | printf(" Tx 1 Collision:%-10lu\n", MCF_FEC_IEEE_T_1COL); |
---|
720 | printf("Tx >1 Collision:%-10lu", MCF_FEC_IEEE_T_MCOL); |
---|
721 | printf(" Tx Deferred:%-10lu", MCF_FEC_IEEE_T_DEF); |
---|
722 | printf(" Late Collision:%-10lu\n", MCF_FEC_IEEE_T_LCOL); |
---|
723 | printf(" Excessive Coll:%-10lu", MCF_FEC_IEEE_T_EXCOL); |
---|
724 | printf(" FIFO Underrun:%-10lu", MCF_FEC_IEEE_T_MACERR); |
---|
725 | printf(" Carrier Error:%-10lu\n", MCF_FEC_IEEE_T_CSERR); |
---|
726 | printf(" Tx SQE Error:%-10lu", MCF_FEC_IEEE_T_SQE); |
---|
727 | printf("Tx Pause Frames:%-10lu", MCF_FEC_IEEE_T_FDXFC); |
---|
728 | printf(" Tx Octets OK:%-10lu\n", MCF_FEC_IEEE_T_OCTETS_OK); |
---|
729 | } |
---|
730 | |
---|
731 | static int fec_ioctl(struct ifnet *ifp, ioctl_command_t command, caddr_t data) |
---|
732 | { |
---|
733 | struct mcf5329_enet_struct *sc = ifp->if_softc; |
---|
734 | int error = 0; |
---|
735 | |
---|
736 | switch (command) { |
---|
737 | case SIOCGIFADDR: |
---|
738 | case SIOCSIFADDR: |
---|
739 | ether_ioctl(ifp, command, data); |
---|
740 | break; |
---|
741 | |
---|
742 | case SIOCSIFFLAGS: |
---|
743 | switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) { |
---|
744 | case IFF_RUNNING: |
---|
745 | fec_stop(sc); |
---|
746 | break; |
---|
747 | |
---|
748 | case IFF_UP: |
---|
749 | fec_init(sc); |
---|
750 | break; |
---|
751 | |
---|
752 | case IFF_UP | IFF_RUNNING: |
---|
753 | fec_stop(sc); |
---|
754 | fec_init(sc); |
---|
755 | break; |
---|
756 | |
---|
757 | default: |
---|
758 | break; |
---|
759 | } |
---|
760 | break; |
---|
761 | |
---|
762 | case SIO_RTEMS_SHOW_STATS: |
---|
763 | enet_stats(sc); |
---|
764 | break; |
---|
765 | |
---|
766 | /* |
---|
767 | * FIXME: All sorts of multicast commands need to be added here! |
---|
768 | */ |
---|
769 | default: |
---|
770 | error = EINVAL; |
---|
771 | break; |
---|
772 | } |
---|
773 | return error; |
---|
774 | } |
---|
775 | |
---|
776 | int |
---|
777 | rtems_fec_driver_attach(struct rtems_bsdnet_ifconfig *config, int attaching) |
---|
778 | { |
---|
779 | struct mcf5329_enet_struct *sc; |
---|
780 | struct ifnet *ifp; |
---|
781 | int mtu; |
---|
782 | int unitNumber; |
---|
783 | char *unitName; |
---|
784 | unsigned char *hwaddr; |
---|
785 | |
---|
786 | /* |
---|
787 | * Parse driver name |
---|
788 | */ |
---|
789 | if ((unitNumber = rtems_bsdnet_parse_driver_name(config, &unitName)) < 0) |
---|
790 | return 0; |
---|
791 | |
---|
792 | /* |
---|
793 | * Is driver free? |
---|
794 | */ |
---|
795 | if ((unitNumber < 0) || (unitNumber >= NIFACES)) { |
---|
796 | printf("mcf5329: bad FEC unit number.\n"); |
---|
797 | return 0; |
---|
798 | } |
---|
799 | sc = &enet_driver[unitNumber]; |
---|
800 | ifp = &sc->arpcom.ac_if; |
---|
801 | if (ifp->if_softc != NULL) { |
---|
802 | printf("mcf5329: driver already in use.\n"); |
---|
803 | return 0; |
---|
804 | } |
---|
805 | |
---|
806 | /* |
---|
807 | * Process options |
---|
808 | */ |
---|
809 | if (config->hardware_address) |
---|
810 | memcpy(sc->arpcom.ac_enaddr, config->hardware_address, ETHER_ADDR_LEN); |
---|
811 | else |
---|
812 | fec_get_mac_address(sc, sc->arpcom.ac_enaddr); |
---|
813 | |
---|
814 | hwaddr = config->hardware_address; |
---|
815 | printf("%s%d: mac: %02x:%02x:%02x:%02x:%02x:%02x\n", |
---|
816 | unitName, unitNumber, |
---|
817 | hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]); |
---|
818 | |
---|
819 | if (config->mtu) |
---|
820 | mtu = config->mtu; |
---|
821 | else |
---|
822 | mtu = ETHERMTU; |
---|
823 | if (config->rbuf_count) |
---|
824 | sc->rxBdCount = config->rbuf_count; |
---|
825 | else |
---|
826 | sc->rxBdCount = RX_BUF_COUNT; |
---|
827 | if (config->xbuf_count) |
---|
828 | sc->txBdCount = config->xbuf_count; |
---|
829 | else |
---|
830 | sc->txBdCount = TX_BUF_COUNT * TX_BD_PER_BUF; |
---|
831 | |
---|
832 | sc->acceptBroadcast = !config->ignore_broadcast; |
---|
833 | |
---|
834 | /* |
---|
835 | * Set up network interface values |
---|
836 | */ |
---|
837 | ifp->if_softc = sc; |
---|
838 | ifp->if_unit = unitNumber; |
---|
839 | ifp->if_name = unitName; |
---|
840 | ifp->if_mtu = mtu; |
---|
841 | ifp->if_init = fec_init; |
---|
842 | ifp->if_ioctl = fec_ioctl; |
---|
843 | ifp->if_start = mcf5329_enet_start; |
---|
844 | ifp->if_output = ether_output; |
---|
845 | ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX; |
---|
846 | if (ifp->if_snd.ifq_maxlen == 0) |
---|
847 | ifp->if_snd.ifq_maxlen = ifqmaxlen; |
---|
848 | |
---|
849 | /* |
---|
850 | * Attach the interface |
---|
851 | */ |
---|
852 | if_attach(ifp); |
---|
853 | ether_ifattach(ifp); |
---|
854 | return 1; |
---|
855 | }; |
---|