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