RTEMS  5.1
rtemscompat.h
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 device_printf(device,format,args...) printk(format,## args)
259 
260 static inline u_int8_t bus_space_do_read_1(u_long handle, unsigned reg)
261 {
262  return _inp_byte((volatile unsigned char*)((handle)+(reg)));
263 }
264 
265 static inline u_int16_t bus_space_do_read_2(u_long handle, unsigned reg)
266 {
267  return _inp_word((volatile unsigned short*)((handle)+(reg)));
268 }
269 
270 static inline u_int32_t bus_space_do_read_4(u_long handle, unsigned reg)
271 {
272  return _inp_long((volatile unsigned *)((handle)+(reg)));
273 }
274 
275 #define bus_space_read_1(tag,handle,reg) bus_space_do_read_1((handle),(reg))
276 #define bus_space_read_2(tag,handle,reg) bus_space_do_read_2((handle),(reg))
277 #define bus_space_read_4(tag,handle,reg) bus_space_do_read_4((handle),(reg))
278 
279 static inline void bus_space_do_write_multi_1(u_long handle, unsigned reg, unsigned char *addr, int cnt)
280 {
281  int i; for (i=0; i<cnt; i++) _out_byte( (handle) + (reg), (addr)[i]);
282 }
283 
284 static inline void bus_space_do_write_multi_2(u_long handle, unsigned reg, unsigned short *addr, int cnt)
285 {
286  int i; for (i=0; i<cnt; i++) _out_word( (handle) + (reg), (addr)[i]);
287 }
288 
289 static inline void bus_space_do_write_multi_4(u_long handle, unsigned reg, unsigned long *addr, int cnt)
290 {
291  int i; for (i=0; i<cnt; i++) _out_long( (handle) + (reg), (addr)[i]);
292 }
293 
294 
295 #define bus_space_write_multi_1(tag, handle, reg, addr, cnt) \
296  bus_space_do_write_multi_1(handle, reg, addr, cnt)
297 #define bus_space_write_multi_2(tag, handle, reg, addr, cnt) \
298  bus_space_do_write_multi_2(handle, reg, addr, cnt)
299 #define bus_space_write_multi_4(tag, handle, reg, addr, cnt) \
300  bus_space_do_write_multi_4(handle, reg, addr, cnt)
301 
302 static inline void bus_space_do_read_multi_1(u_long handle, unsigned reg, unsigned char *addr, int cnt)
303 {
304  int i; for (i=0; i<cnt; i++)
305  (addr)[i] = _inp_byte((volatile unsigned char*)((handle)+(reg)));
306 }
307 
308 static inline void bus_space_do_read_multi_2(u_long handle, unsigned reg, unsigned short *addr, int cnt)
309 {
310  int i; for (i=0; i<cnt; i++)
311  (addr)[i] = _inp_word((volatile unsigned short*)((handle)+(reg)));
312 }
313 
314 static inline void bus_space_do_read_multi_4(u_long handle, unsigned reg, unsigned long *addr, int cnt)
315 {
316  int i; for (i=0; i<cnt; i++)
317  (addr)[i] = _inp_long((volatile unsigned *)((handle)+(reg)));
318 }
319 
320 #define bus_space_read_multi_1(tag, handle, reg, addr, cnt) \
321  bus_space_do_read_multi_1(handle, reg, addr, cnt)
322 #define bus_space_read_multi_2(tag, handle, reg, addr, cnt) \
323  bus_space_do_read_multi_2(handle, reg, addr, cnt)
324 #define bus_space_read_multi_4(tag, handle, reg, addr, cnt) \
325  bus_space_do_read_multi_4(handle, reg, addr, cnt)
326 
327 
328 
329 #define bus_space_write_1(tag, handle, reg, val) \
330  do { _out_byte( (handle) + (reg), (val)); } while (0)
331 
332 #define bus_space_write_2(tag, handle, reg, val) \
333  do { _out_word( (handle) + (reg), (val)); } while (0)
334 
335 #define bus_space_write_4(tag, handle, reg, val) \
336  do { _out_long( (handle) + (reg), (val)); } while (0)
337 
338 #define BPF_MTAP(a,b) do { } while (0)
339 
340 extern unsigned net_driver_ticks_per_sec;
341 
342 #ifdef __PPC__
343 /* PPC has a timebase - based delay */
344 #define DELAY(n) do { \
345  if ( (n) > 10000 ) \
346  rtems_task_wake_after((((n)*net_driver_ticks_per_sec)/1000000) + 1); \
347  else \
348  rtems_bsp_delay(n); \
349  } while (0)
350 #else
351 #warning "Have no good usec delay implementation"
352 #define DELAY(n) do { \
353  rtems_task_wake_after((((n)*net_driver_ticks_per_sec)/1000000) + 1); \
354  } while (0)
355 #endif
356 
357 
358 #define IRQ_LOCKED(code) \
359  do { unsigned long _xtre_irq_flags; \
360  rtems_interrupt_disable(_xtre_irq_flags); \
361  do { code } while(0); \
362  rtems_interrupt_enable(_xtre_irq_flags); \
363  } while (0)
364 
365 typedef void (driver_intr_t)(void *);
366 
367 #define if_xname if_name
368 
369 /* need to replace those with LOCAL2PCI() and make sure the bus handle is initialized
370  * (on most BSPs we get away with PCI_DRAM_OFFSET [no bus handle needed at all]
371  */
372 #ifndef PCI_DRAM_OFFSET
373 #define PCI_DRAM_OFFSET 0
374 #endif
375 
376 #ifndef PCI_MEM_BASE
377 #define PCI_MEM_BASE 0
378 #endif
379 
380 #define kvtop(a) ((unsigned long)(a) + PCI_DRAM_OFFSET)
381 #define vtophys(a) ((unsigned long)(a) + PCI_DRAM_OFFSET)
382 
383 #define PCI2LOCAL(a,bus) ((unsigned long)(a) + PCI_MEM_BASE)
384 
385 #ifdef PCI0_IO_BASE /* special mvme5500 hack :-( */
386 #define PCI_IO_2LOCAL(a,bus) ((unsigned long)(a) + PCI0_IO_BASE)
387 #elif defined(PCI_IO_BASE)
388 #define PCI_IO_2LOCAL(a,bus) ((unsigned long)(a) + PCI_IO_BASE)
389 #elif defined(_IO_BASE)
390 #define PCI_IO_2LOCAL(a,bus) ((unsigned long)(a) + _IO_BASE)
391 #else
392 #warning "Unable to determine base address of PCI IO space; using ZERO"
393 #define PCI_IO_2LOCAL(a,bus) ((unsigned long)(a))
394 #endif
395 
396 #define if_printf(if,fmt,args...) printf("%s:"fmt,(if)->if_name,args)
397 
398 #ifndef BUS_PROBE_DEFAULT
399 #define BUS_PROBE_DEFAULT 0
400 #endif
401 
402 static inline void *
403 contigmalloc(
404  unsigned long size,
405  int type,
406  int flags,
407  unsigned long lo,
408  unsigned long hi,
409  unsigned long align,
410  unsigned long bound)
411 {
412 void *ptr = rtems_bsdnet_malloc(size + sizeof(ptr) + align-1, type, flags);
413 char *rval = 0;
414  if ( ptr ) {
415  unsigned tmp = (unsigned)ptr + align - 1;
416  tmp -= tmp % align;
417  rval = (char*)tmp;
418  /* save backlink */
419  *(void**)(rval+size) = ptr;
420  }
421  return rval;
422 }
423 
424 static inline void
425 contigfree(void *ptr, size_t size, int type)
426 {
427  rtems_bsdnet_free( *(void**)((unsigned)ptr + size), type);
428 }
429 
430 /* callout stuff */
431 #define callout_init(args...) do {} while (0);
432 #define callout_reset(args...) do {} while (0);
433 #define callout_stop(args...) do {} while (0);
434 
435 #define IFQ_DRV_IS_EMPTY(q) (0 == (q)->ifq_head)
436 #define IFQ_DRV_DEQUEUE(q,m) IF_DEQUEUE((q),(m))
437 #define IFQ_DRV_PREPEND(q,m) IF_PREPEND((q),(m))
438 
439 #define DO_ETHER_INPUT_SKIPPING_ETHER_HEADER(ifp,m) \
440  { struct ether_header *eh; \
441  eh = mtod(m, struct ether_header*); \
442  m->m_data += sizeof(struct ether_header); \
443  m->m_len -= sizeof(struct ether_header); \
444  m->m_pkthdr.len -= sizeof(struct ether_header); \
445  m->m_pkthdr.rcvif = ifp; \
446  ether_input(ifp, eh, m); \
447  } while (0)
448 
449 
450 #ifndef __KERNEL_RCSID
451 #define __KERNEL_RCSID(arg...)
452 #endif
453 
454 #endif
unsigned p
Definition: tte.h:90
Interface to Kernel Print Methods.
unsigned size
Definition: tte.h:74
unsigned v
Definition: tte.h:73
Definition: rtemscompat1.h:32