1 | #ifndef RTEMS_COMPAT_BSD_NET_H |
---|
2 | #define RTEMS_COMPAT_BSD_NET_H |
---|
3 | |
---|
4 | /* BSD -> rtems wrappers; stuff that must be defined |
---|
5 | * prior to including most BSD headers |
---|
6 | */ |
---|
7 | |
---|
8 | /* Copyright: Till Straumann <strauman@slac.stanford.edu>, 2005; |
---|
9 | * License: see LICENSE file. |
---|
10 | */ |
---|
11 | |
---|
12 | #include <rtems.h> |
---|
13 | #include <sys/errno.h> |
---|
14 | #include <sys/types.h> |
---|
15 | |
---|
16 | #include <stdlib.h> |
---|
17 | |
---|
18 | /* Check for RTEMS version; true if >= ma.mi.re */ |
---|
19 | #define ISMINVERSION(ma,mi,re) \ |
---|
20 | ( __RTEMS_MAJOR__ > (ma) \ |
---|
21 | || (__RTEMS_MAJOR__ == (ma) && __RTEMS_MINOR__ > (mi)) \ |
---|
22 | || (__RTEMS_MAJOR__ == (ma) && __RTEMS_MINOR__ == (mi) && __RTEMS_REVISION__ >= (re)) \ |
---|
23 | ) |
---|
24 | |
---|
25 | /* 'align' ARG is evaluated more than once */ |
---|
26 | #define _DO_ALIGN(addr, align) (((uint32_t)(addr) + (align) - 1) & ~((align)-1)) |
---|
27 | |
---|
28 | |
---|
29 | /* malloc/free are redefined :-( */ |
---|
30 | static inline void *the_real_malloc(size_t n) |
---|
31 | { |
---|
32 | return malloc(n); |
---|
33 | } |
---|
34 | |
---|
35 | static inline void the_real_free(void *p) |
---|
36 | { |
---|
37 | return free(p); |
---|
38 | } |
---|
39 | |
---|
40 | #include <machine/rtems-bsd-kernel-space.h> |
---|
41 | #include <rtems/rtems_bsdnet.h> |
---|
42 | #include <rtems/rtems_bsdnet_internal.h> |
---|
43 | #ifdef __i386__ |
---|
44 | #include <libcpu/cpu.h> |
---|
45 | #elif defined(__PPC__) |
---|
46 | #include <libcpu/io.h> |
---|
47 | #else |
---|
48 | #error "dunno what IO ops to use on this architecture" |
---|
49 | #endif |
---|
50 | #include <rtems/bspIo.h> |
---|
51 | |
---|
52 | #define NET_EMB(x,y,z) x ## y ## z |
---|
53 | #define NET_EMBEMB(x,y,z) NET_EMB(x,y,z) |
---|
54 | |
---|
55 | #define NET_STR(arg) #arg |
---|
56 | #define NET_STRSTR(arg) NET_STR(arg) |
---|
57 | |
---|
58 | #define NET_SOFTC NET_EMBEMB(,NETDRIVER_PREFIX,_softc) |
---|
59 | |
---|
60 | #define METHODS NET_EMBEMB(rtems_,NETDRIVER_PREFIX,_methods) |
---|
61 | extern struct _net_drv_tbl METHODS; |
---|
62 | |
---|
63 | #ifdef DEBUG_MODULAR |
---|
64 | #define METHODSPTR NET_EMBEMB(rtems_,NETDRIVER_PREFIX,_methods_p) |
---|
65 | extern struct _net_drv_tbl *volatile METHODSPTR; |
---|
66 | #else |
---|
67 | #define METHODSPTR (&METHODS) |
---|
68 | #endif |
---|
69 | |
---|
70 | #if defined(__LITTLE_ENDIAN__) || (__i386__) |
---|
71 | static inline uint16_t htole16(uint16_t v) { return v; } |
---|
72 | static inline uint32_t htole32(uint32_t v) { return v; } |
---|
73 | static inline uint64_t htole64(uint64_t v) { return v; } |
---|
74 | static inline uint16_t le16toh(uint16_t v) { return v; } |
---|
75 | static inline uint32_t le32toh(uint32_t v) { return v; } |
---|
76 | static inline uint64_t le64toh(uint64_t v) { return v; } |
---|
77 | #elif defined(__BIG_ENDIAN__) |
---|
78 | #ifdef __PPC__ |
---|
79 | #include <libcpu/byteorder.h> |
---|
80 | |
---|
81 | /* Different RTEMS versions use different types |
---|
82 | * for 32-bit (unsigned vs. unsigned long which |
---|
83 | * always cause gcc warnings and possible alias |
---|
84 | * violations, sigh). |
---|
85 | */ |
---|
86 | |
---|
87 | #if ISMINVERSION(4,8,0) |
---|
88 | typedef uint32_t rtemscompat_32_t; |
---|
89 | #else |
---|
90 | typedef unsigned rtemscompat_32_t; |
---|
91 | #endif |
---|
92 | |
---|
93 | static inline uint16_t |
---|
94 | htole16(uint16_t v) |
---|
95 | { |
---|
96 | uint16_t rval; |
---|
97 | st_le16(&rval,v); |
---|
98 | return rval; |
---|
99 | } |
---|
100 | |
---|
101 | static inline uint16_t |
---|
102 | le16toh(uint16_t v) |
---|
103 | { |
---|
104 | return ld_le16((unsigned short*)&v); |
---|
105 | } |
---|
106 | |
---|
107 | static inline uint32_t |
---|
108 | htole32(uint32_t v) |
---|
109 | { |
---|
110 | rtemscompat_32_t rval; |
---|
111 | st_le32(&rval,v); |
---|
112 | return rval; |
---|
113 | } |
---|
114 | |
---|
115 | static inline uint32_t |
---|
116 | le32toh(uint32_t v) |
---|
117 | { |
---|
118 | rtemscompat_32_t vv = v; |
---|
119 | return ld_le32(&vv); |
---|
120 | } |
---|
121 | |
---|
122 | /* Compiler generated floating point instructions for this |
---|
123 | * and rtems_bsdnet_newproc()-generated tasks are non-FP |
---|
124 | * :-( |
---|
125 | */ |
---|
126 | static inline uint64_t |
---|
127 | htole64(uint64_t v) |
---|
128 | { |
---|
129 | union { |
---|
130 | rtemscompat_32_t tmp[2]; |
---|
131 | uint64_t rval; |
---|
132 | } u; |
---|
133 | |
---|
134 | st_le32( &u.tmp[0], (unsigned)(v&0xffffffff) ); |
---|
135 | st_le32( &u.tmp[1], (unsigned)((v>>32)&0xffffffff) ); |
---|
136 | |
---|
137 | return u.rval; |
---|
138 | } |
---|
139 | |
---|
140 | #else |
---|
141 | #error "need htoleXX() implementation for this CPU arch" |
---|
142 | #endif |
---|
143 | |
---|
144 | #else |
---|
145 | #error "Unknown CPU endianness" |
---|
146 | #endif |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | #ifdef __PPC__ |
---|
151 | #define _out_byte(a,v) out_8((volatile uint8_t*)(a),(v)) |
---|
152 | #define _inp_byte(a) in_8((volatile uint8_t*)(a)) |
---|
153 | #ifdef NET_CHIP_LE |
---|
154 | #define _out_word(a,v) out_le16((volatile uint16_t*)(a),(v)) |
---|
155 | #define _out_long(a,v) out_le32((volatile uint32_t *)(a),(v)) |
---|
156 | #define _inp_word(a) in_le16((volatile uint16_t*)(a)) |
---|
157 | #define _inp_long(a) in_le32((volatile uint32_t *)(a)) |
---|
158 | #elif defined(NET_CHIP_BE) |
---|
159 | #define _out_word(a,v) out_be16((volatile uint16_t*)(a),(v)) |
---|
160 | #define _out_long(a,v) out_be32((volatile uint32_t *)(a),(v)) |
---|
161 | #define _inp_word(a) in_be16((volatile uint16_t*)(a)) |
---|
162 | #define _inp_long(a) in_be32((volatile uint32_t *)(a)) |
---|
163 | #else |
---|
164 | #error rtemscompat_defs.h must define either NET_CHIP_LE or NET_CHIP_BE |
---|
165 | #endif |
---|
166 | static inline void wrle32(unsigned *a, unsigned v) |
---|
167 | { |
---|
168 | asm volatile("stwbrx %1,0,%2":"=m"(*a):"r"(v),"r"(a)); |
---|
169 | } |
---|
170 | static inline unsigned rdle32(unsigned *a) |
---|
171 | { |
---|
172 | asm volatile("lwbrx %0,0,%0":"=r"(a):"0"(a),"m"(*a)); |
---|
173 | return (unsigned)a; |
---|
174 | } |
---|
175 | static inline void orle32(unsigned *a,unsigned v) { wrle32(a, rdle32(a) | v); } |
---|
176 | static inline void anle32(unsigned *a,unsigned v) { wrle32(a, rdle32(a) & v); } |
---|
177 | |
---|
178 | static inline void wrle16(unsigned short *a, unsigned short v) |
---|
179 | { |
---|
180 | asm volatile("sthbrx %1,0,%2":"=m"(*a):"r"(v),"r"(a)); |
---|
181 | } |
---|
182 | static inline unsigned short rdle16(unsigned short *a) |
---|
183 | { |
---|
184 | asm volatile("lhbrx %0,0,%0":"=r"(a):"0"(a),"m"(*a)); |
---|
185 | return (unsigned short)(unsigned)a; |
---|
186 | } |
---|
187 | static inline void orle16(unsigned short *a,unsigned short v) { wrle16(a, rdle16(a) | v); } |
---|
188 | static inline void anle16(unsigned short *a,unsigned short v) { wrle16(a, rdle16(a) & v); } |
---|
189 | #endif |
---|
190 | |
---|
191 | #ifdef __i386__ |
---|
192 | #ifdef NET_CHIP_BE |
---|
193 | #error dunno how to output BE data |
---|
194 | #endif |
---|
195 | |
---|
196 | static inline void wrle32(volatile unsigned *p, unsigned v) { *p = v; } |
---|
197 | static inline void orle32(volatile unsigned *p, unsigned v) { *p |= v; } |
---|
198 | static inline void anle32(volatile unsigned *p, unsigned v) { *p &= v; } |
---|
199 | static inline unsigned rdle32(volatile unsigned *p) { return *p; } |
---|
200 | |
---|
201 | static inline void wrle16(volatile unsigned short *p, unsigned short v) { *p = v; } |
---|
202 | static inline void orle16(volatile unsigned short *p, unsigned short v) { *p |= v; } |
---|
203 | static inline void anle16(volatile unsigned short *p, unsigned short v) { *p &= v; } |
---|
204 | static inline unsigned short rdle16(volatile unsigned short *p) { return *p; } |
---|
205 | |
---|
206 | #ifdef NET_CHIP_MEM_IO |
---|
207 | |
---|
208 | #ifdef __i386__ |
---|
209 | static inline void _out_byte(unsigned a, unsigned char v) { *(volatile unsigned char*)a = v; } |
---|
210 | static inline unsigned char _inp_byte(unsigned a) { return *(volatile unsigned char*)a; } |
---|
211 | #ifdef NET_CHIP_LE |
---|
212 | static inline void _out_word(unsigned a, unsigned short v) { *(volatile unsigned short*)a = v; } |
---|
213 | static inline unsigned short _inp_word(unsigned a) { return *(volatile unsigned short*)a; } |
---|
214 | static inline void _out_long(unsigned a, unsigned v) { *(volatile unsigned *)a = v; } |
---|
215 | static inline unsigned _inp_long(unsigned a) { return *(volatile unsigned *)a; } |
---|
216 | #elif defined(NET_CHIP_BE) |
---|
217 | #error "BE memory IO not implemented for i386 yet" |
---|
218 | #else |
---|
219 | #error rtemscompat_defs.h must define either NET_CHIP_LE or NET_CHIP_BE |
---|
220 | #endif |
---|
221 | #else |
---|
222 | |
---|
223 | #error "Memory IO not implemented for this CPU architecture yet" |
---|
224 | |
---|
225 | #endif |
---|
226 | #elif defined(NET_CHIP_PORT_IO) |
---|
227 | #define _out_byte(addr,val) outport_byte((addr),(val)) |
---|
228 | #define _out_word(addr,val) outport_word((addr),(val)) |
---|
229 | #define _out_long(addr,val) outport_long((addr),(val)) |
---|
230 | |
---|
231 | static inline u_int8_t _inp_byte(volatile unsigned char *a) |
---|
232 | { |
---|
233 | register u_int8_t rval; |
---|
234 | inport_byte((unsigned short)(unsigned)a,rval); |
---|
235 | return rval; |
---|
236 | } |
---|
237 | static inline u_int16_t _inp_word(volatile unsigned short *a) |
---|
238 | { |
---|
239 | register u_int16_t rval; |
---|
240 | inport_word((unsigned short)(unsigned)a,rval); |
---|
241 | return rval; |
---|
242 | } |
---|
243 | static inline u_int32_t _inp_long(volatile unsigned *a) |
---|
244 | { |
---|
245 | register u_int32_t rval; |
---|
246 | inport_long((unsigned short)(unsigned)a,rval); |
---|
247 | return rval; |
---|
248 | } |
---|
249 | #else |
---|
250 | #error either NET_CHIP_MEM_IO or NET_CHIP_PORT_IO must be defined |
---|
251 | #endif |
---|
252 | #endif |
---|
253 | |
---|
254 | #ifndef __FBSDID |
---|
255 | #define __FBSDID(arg) |
---|
256 | #endif |
---|
257 | |
---|
258 | #define _KERNEL |
---|
259 | |
---|
260 | #define device_printf(device,format,args...) printk(format,## args) |
---|
261 | |
---|
262 | static inline u_int8_t bus_space_do_read_1(u_long handle, unsigned reg) |
---|
263 | { |
---|
264 | return _inp_byte((volatile unsigned char*)((handle)+(reg))); |
---|
265 | } |
---|
266 | |
---|
267 | static inline u_int16_t bus_space_do_read_2(u_long handle, unsigned reg) |
---|
268 | { |
---|
269 | return _inp_word((volatile unsigned short*)((handle)+(reg))); |
---|
270 | } |
---|
271 | |
---|
272 | static inline u_int32_t bus_space_do_read_4(u_long handle, unsigned reg) |
---|
273 | { |
---|
274 | return _inp_long((volatile unsigned *)((handle)+(reg))); |
---|
275 | } |
---|
276 | |
---|
277 | #define bus_space_read_1(tag,handle,reg) bus_space_do_read_1((handle),(reg)) |
---|
278 | #define bus_space_read_2(tag,handle,reg) bus_space_do_read_2((handle),(reg)) |
---|
279 | #define bus_space_read_4(tag,handle,reg) bus_space_do_read_4((handle),(reg)) |
---|
280 | |
---|
281 | static inline void bus_space_do_write_multi_1(u_long handle, unsigned reg, unsigned char *addr, int cnt) |
---|
282 | { |
---|
283 | int i; for (i=0; i<cnt; i++) _out_byte( (handle) + (reg), (addr)[i]); |
---|
284 | } |
---|
285 | |
---|
286 | static inline void bus_space_do_write_multi_2(u_long handle, unsigned reg, unsigned short *addr, int cnt) |
---|
287 | { |
---|
288 | int i; for (i=0; i<cnt; i++) _out_word( (handle) + (reg), (addr)[i]); |
---|
289 | } |
---|
290 | |
---|
291 | static inline void bus_space_do_write_multi_4(u_long handle, unsigned reg, unsigned long *addr, int cnt) |
---|
292 | { |
---|
293 | int i; for (i=0; i<cnt; i++) _out_long( (handle) + (reg), (addr)[i]); |
---|
294 | } |
---|
295 | |
---|
296 | |
---|
297 | #define bus_space_write_multi_1(tag, handle, reg, addr, cnt) \ |
---|
298 | bus_space_do_write_multi_1(handle, reg, addr, cnt) |
---|
299 | #define bus_space_write_multi_2(tag, handle, reg, addr, cnt) \ |
---|
300 | bus_space_do_write_multi_2(handle, reg, addr, cnt) |
---|
301 | #define bus_space_write_multi_4(tag, handle, reg, addr, cnt) \ |
---|
302 | bus_space_do_write_multi_4(handle, reg, addr, cnt) |
---|
303 | |
---|
304 | static inline void bus_space_do_read_multi_1(u_long handle, unsigned reg, unsigned char *addr, int cnt) |
---|
305 | { |
---|
306 | int i; for (i=0; i<cnt; i++) |
---|
307 | (addr)[i] = _inp_byte((volatile unsigned char*)((handle)+(reg))); |
---|
308 | } |
---|
309 | |
---|
310 | static inline void bus_space_do_read_multi_2(u_long handle, unsigned reg, unsigned short *addr, int cnt) |
---|
311 | { |
---|
312 | int i; for (i=0; i<cnt; i++) |
---|
313 | (addr)[i] = _inp_word((volatile unsigned short*)((handle)+(reg))); |
---|
314 | } |
---|
315 | |
---|
316 | static inline void bus_space_do_read_multi_4(u_long handle, unsigned reg, unsigned long *addr, int cnt) |
---|
317 | { |
---|
318 | int i; for (i=0; i<cnt; i++) |
---|
319 | (addr)[i] = _inp_long((volatile unsigned *)((handle)+(reg))); |
---|
320 | } |
---|
321 | |
---|
322 | #define bus_space_read_multi_1(tag, handle, reg, addr, cnt) \ |
---|
323 | bus_space_do_read_multi_1(handle, reg, addr, cnt) |
---|
324 | #define bus_space_read_multi_2(tag, handle, reg, addr, cnt) \ |
---|
325 | bus_space_do_read_multi_2(handle, reg, addr, cnt) |
---|
326 | #define bus_space_read_multi_4(tag, handle, reg, addr, cnt) \ |
---|
327 | bus_space_do_read_multi_4(handle, reg, addr, cnt) |
---|
328 | |
---|
329 | |
---|
330 | |
---|
331 | #define bus_space_write_1(tag, handle, reg, val) \ |
---|
332 | do { _out_byte( (handle) + (reg), (val)); } while (0) |
---|
333 | |
---|
334 | #define bus_space_write_2(tag, handle, reg, val) \ |
---|
335 | do { _out_word( (handle) + (reg), (val)); } while (0) |
---|
336 | |
---|
337 | #define bus_space_write_4(tag, handle, reg, val) \ |
---|
338 | do { _out_long( (handle) + (reg), (val)); } while (0) |
---|
339 | |
---|
340 | #define BPF_MTAP(a,b) do { } while (0) |
---|
341 | |
---|
342 | extern unsigned net_driver_ticks_per_sec; |
---|
343 | |
---|
344 | #ifdef __PPC__ |
---|
345 | /* PPC has a timebase - based delay */ |
---|
346 | #define DELAY(n) do { \ |
---|
347 | if ( (n) > 10000 ) \ |
---|
348 | rtems_task_wake_after((((n)*net_driver_ticks_per_sec)/1000000) + 1); \ |
---|
349 | else \ |
---|
350 | rtems_bsp_delay(n); \ |
---|
351 | } while (0) |
---|
352 | #else |
---|
353 | #warning "Have no good usec delay implementation" |
---|
354 | #define DELAY(n) do { \ |
---|
355 | rtems_task_wake_after((((n)*net_driver_ticks_per_sec)/1000000) + 1); \ |
---|
356 | } while (0) |
---|
357 | #endif |
---|
358 | |
---|
359 | |
---|
360 | #define IRQ_LOCKED(code) \ |
---|
361 | do { unsigned long _xtre_irq_flags; \ |
---|
362 | rtems_interrupt_disable(_xtre_irq_flags); \ |
---|
363 | do { code } while(0); \ |
---|
364 | rtems_interrupt_enable(_xtre_irq_flags); \ |
---|
365 | } while (0) |
---|
366 | |
---|
367 | typedef void (driver_intr_t)(void *); |
---|
368 | |
---|
369 | #define if_xname if_name |
---|
370 | |
---|
371 | /* need to replace those with LOCAL2PCI() and make sure the bus handle is initialized |
---|
372 | * (on most BSPs we get away with PCI_DRAM_OFFSET [no bus handle needed at all] |
---|
373 | */ |
---|
374 | #ifndef PCI_DRAM_OFFSET |
---|
375 | #define PCI_DRAM_OFFSET 0 |
---|
376 | #endif |
---|
377 | |
---|
378 | #ifndef PCI_MEM_BASE |
---|
379 | #define PCI_MEM_BASE 0 |
---|
380 | #endif |
---|
381 | |
---|
382 | #define kvtop(a) ((unsigned long)(a) + PCI_DRAM_OFFSET) |
---|
383 | #define vtophys(a) ((unsigned long)(a) + PCI_DRAM_OFFSET) |
---|
384 | |
---|
385 | #define PCI2LOCAL(a,bus) ((unsigned long)(a) + PCI_MEM_BASE) |
---|
386 | |
---|
387 | #ifdef PCI0_IO_BASE /* special mvme5500 hack :-( */ |
---|
388 | #define PCI_IO_2LOCAL(a,bus) ((unsigned long)(a) + PCI0_IO_BASE) |
---|
389 | #elif defined(PCI_IO_BASE) |
---|
390 | #define PCI_IO_2LOCAL(a,bus) ((unsigned long)(a) + PCI_IO_BASE) |
---|
391 | #elif defined(_IO_BASE) |
---|
392 | #define PCI_IO_2LOCAL(a,bus) ((unsigned long)(a) + _IO_BASE) |
---|
393 | #else |
---|
394 | #warning "Unable to determine base address of PCI IO space; using ZERO" |
---|
395 | #define PCI_IO_2LOCAL(a,bus) ((unsigned long)(a)) |
---|
396 | #endif |
---|
397 | |
---|
398 | #define if_printf(if,fmt,args...) printf("%s:"fmt,(if)->if_name,args) |
---|
399 | |
---|
400 | #ifndef BUS_PROBE_DEFAULT |
---|
401 | #define BUS_PROBE_DEFAULT 0 |
---|
402 | #endif |
---|
403 | |
---|
404 | static inline void * |
---|
405 | contigmalloc( |
---|
406 | unsigned long size, |
---|
407 | int type, |
---|
408 | int flags, |
---|
409 | unsigned long lo, |
---|
410 | unsigned long hi, |
---|
411 | unsigned long align, |
---|
412 | unsigned long bound) |
---|
413 | { |
---|
414 | void *ptr = rtems_bsdnet_malloc(size + sizeof(ptr) + align-1, type, flags); |
---|
415 | char *rval = 0; |
---|
416 | if ( ptr ) { |
---|
417 | unsigned tmp = (unsigned)ptr + align - 1; |
---|
418 | tmp -= tmp % align; |
---|
419 | rval = (char*)tmp; |
---|
420 | /* save backlink */ |
---|
421 | *(void**)(rval+size) = ptr; |
---|
422 | } |
---|
423 | return rval; |
---|
424 | } |
---|
425 | |
---|
426 | static inline void |
---|
427 | contigfree(void *ptr, size_t size, int type) |
---|
428 | { |
---|
429 | rtems_bsdnet_free( *(void**)((unsigned)ptr + size), type); |
---|
430 | } |
---|
431 | |
---|
432 | /* callout stuff */ |
---|
433 | #define callout_init(args...) do {} while (0); |
---|
434 | #define callout_reset(args...) do {} while (0); |
---|
435 | #define callout_stop(args...) do {} while (0); |
---|
436 | |
---|
437 | #define IFQ_DRV_IS_EMPTY(q) (0 == (q)->ifq_head) |
---|
438 | #define IFQ_DRV_DEQUEUE(q,m) IF_DEQUEUE((q),(m)) |
---|
439 | #define IFQ_DRV_PREPEND(q,m) IF_PREPEND((q),(m)) |
---|
440 | |
---|
441 | #define DO_ETHER_INPUT_SKIPPING_ETHER_HEADER(ifp,m) \ |
---|
442 | { struct ether_header *eh; \ |
---|
443 | eh = mtod(m, struct ether_header*); \ |
---|
444 | m->m_data += sizeof(struct ether_header); \ |
---|
445 | m->m_len -= sizeof(struct ether_header); \ |
---|
446 | m->m_pkthdr.len -= sizeof(struct ether_header); \ |
---|
447 | m->m_pkthdr.rcvif = ifp; \ |
---|
448 | ether_input(ifp, eh, m); \ |
---|
449 | } while (0) |
---|
450 | |
---|
451 | |
---|
452 | #ifndef __KERNEL_RCSID |
---|
453 | #define __KERNEL_RCSID(arg...) |
---|
454 | #endif |
---|
455 | |
---|
456 | #endif |
---|