csr1212.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * csr1212.h -- IEEE 1212 Control and Status Register support for Linux
  3. *
  4. * Copyright (C) 2003 Francois Retief <fgretief@sun.ac.za>
  5. * Steve Kinneberg <kinnebergsteve@acmsystems.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  21. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  24. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  26. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  27. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef __CSR1212_H__
  30. #define __CSR1212_H__
  31. #include <linux/types.h>
  32. #include <linux/slab.h>
  33. #include <asm/atomic.h>
  34. #define CSR1212_MALLOC(size) kmalloc((size), GFP_KERNEL)
  35. #define CSR1212_FREE(ptr) kfree(ptr)
  36. #define CSR1212_SUCCESS (0)
  37. /* CSR 1212 key types */
  38. #define CSR1212_KV_TYPE_IMMEDIATE 0
  39. #define CSR1212_KV_TYPE_CSR_OFFSET 1
  40. #define CSR1212_KV_TYPE_LEAF 2
  41. #define CSR1212_KV_TYPE_DIRECTORY 3
  42. /* CSR 1212 key ids */
  43. #define CSR1212_KV_ID_DESCRIPTOR 0x01
  44. #define CSR1212_KV_ID_BUS_DEPENDENT_INFO 0x02
  45. #define CSR1212_KV_ID_VENDOR 0x03
  46. #define CSR1212_KV_ID_HARDWARE_VERSION 0x04
  47. #define CSR1212_KV_ID_MODULE 0x07
  48. #define CSR1212_KV_ID_NODE_CAPABILITIES 0x0C
  49. #define CSR1212_KV_ID_EUI_64 0x0D
  50. #define CSR1212_KV_ID_UNIT 0x11
  51. #define CSR1212_KV_ID_SPECIFIER_ID 0x12
  52. #define CSR1212_KV_ID_VERSION 0x13
  53. #define CSR1212_KV_ID_DEPENDENT_INFO 0x14
  54. #define CSR1212_KV_ID_UNIT_LOCATION 0x15
  55. #define CSR1212_KV_ID_MODEL 0x17
  56. #define CSR1212_KV_ID_INSTANCE 0x18
  57. #define CSR1212_KV_ID_KEYWORD 0x19
  58. #define CSR1212_KV_ID_FEATURE 0x1A
  59. #define CSR1212_KV_ID_EXTENDED_ROM 0x1B
  60. #define CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID 0x1C
  61. #define CSR1212_KV_ID_EXTENDED_KEY 0x1D
  62. #define CSR1212_KV_ID_EXTENDED_DATA 0x1E
  63. #define CSR1212_KV_ID_MODIFIABLE_DESCRIPTOR 0x1F
  64. #define CSR1212_KV_ID_DIRECTORY_ID 0x20
  65. #define CSR1212_KV_ID_REVISION 0x21
  66. /* IEEE 1212 Address space map */
  67. #define CSR1212_ALL_SPACE_BASE (0x000000000000ULL)
  68. #define CSR1212_ALL_SPACE_SIZE (1ULL << 48)
  69. #define CSR1212_ALL_SPACE_END (CSR1212_ALL_SPACE_BASE + CSR1212_ALL_SPACE_SIZE)
  70. #define CSR1212_MEMORY_SPACE_BASE (0x000000000000ULL)
  71. #define CSR1212_MEMORY_SPACE_SIZE ((256ULL * (1ULL << 40)) - (512ULL * (1ULL << 20)))
  72. #define CSR1212_MEMORY_SPACE_END (CSR1212_MEMORY_SPACE_BASE + CSR1212_MEMORY_SPACE_SIZE)
  73. #define CSR1212_PRIVATE_SPACE_BASE (0xffffe0000000ULL)
  74. #define CSR1212_PRIVATE_SPACE_SIZE (256ULL * (1ULL << 20))
  75. #define CSR1212_PRIVATE_SPACE_END (CSR1212_PRIVATE_SPACE_BASE + CSR1212_PRIVATE_SPACE_SIZE)
  76. #define CSR1212_REGISTER_SPACE_BASE (0xfffff0000000ULL)
  77. #define CSR1212_REGISTER_SPACE_SIZE (256ULL * (1ULL << 20))
  78. #define CSR1212_REGISTER_SPACE_END (CSR1212_REGISTER_SPACE_BASE + CSR1212_REGISTER_SPACE_SIZE)
  79. #define CSR1212_CSR_ARCH_REG_SPACE_BASE (0xfffff0000000ULL)
  80. #define CSR1212_CSR_ARCH_REG_SPACE_SIZE (512)
  81. #define CSR1212_CSR_ARCH_REG_SPACE_END (CSR1212_CSR_ARCH_REG_SPACE_BASE + CSR1212_CSR_ARCH_REG_SPACE_SIZE)
  82. #define CSR1212_CSR_ARCH_REG_SPACE_OFFSET (CSR1212_CSR_ARCH_REG_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
  83. #define CSR1212_CSR_BUS_DEP_REG_SPACE_BASE (0xfffff0000200ULL)
  84. #define CSR1212_CSR_BUS_DEP_REG_SPACE_SIZE (512)
  85. #define CSR1212_CSR_BUS_DEP_REG_SPACE_END (CSR1212_CSR_BUS_DEP_REG_SPACE_BASE + CSR1212_CSR_BUS_DEP_REG_SPACE_SIZE)
  86. #define CSR1212_CSR_BUS_DEP_REG_SPACE_OFFSET (CSR1212_CSR_BUS_DEP_REG_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
  87. #define CSR1212_CONFIG_ROM_SPACE_BASE (0xfffff0000400ULL)
  88. #define CSR1212_CONFIG_ROM_SPACE_SIZE (1024)
  89. #define CSR1212_CONFIG_ROM_SPACE_END (CSR1212_CONFIG_ROM_SPACE_BASE + CSR1212_CONFIG_ROM_SPACE_SIZE)
  90. #define CSR1212_CONFIG_ROM_SPACE_OFFSET (CSR1212_CONFIG_ROM_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
  91. #define CSR1212_UNITS_SPACE_BASE (0xfffff0000800ULL)
  92. #define CSR1212_UNITS_SPACE_SIZE ((256ULL * (1ULL << 20)) - 2048)
  93. #define CSR1212_UNITS_SPACE_END (CSR1212_UNITS_SPACE_BASE + CSR1212_UNITS_SPACE_SIZE)
  94. #define CSR1212_UNITS_SPACE_OFFSET (CSR1212_UNITS_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
  95. #define CSR1212_INVALID_ADDR_SPACE -1
  96. /* Config ROM image structures */
  97. struct csr1212_bus_info_block_img {
  98. u8 length;
  99. u8 crc_length;
  100. u16 crc;
  101. /* Must be last */
  102. u32 data[0]; /* older gcc can't handle [] which is standard */
  103. };
  104. struct csr1212_leaf {
  105. int len;
  106. u32 *data;
  107. };
  108. struct csr1212_dentry {
  109. struct csr1212_dentry *next, *prev;
  110. struct csr1212_keyval *kv;
  111. };
  112. struct csr1212_directory {
  113. int len;
  114. struct csr1212_dentry *dentries_head, *dentries_tail;
  115. };
  116. struct csr1212_keyval {
  117. struct {
  118. u8 type;
  119. u8 id;
  120. } key;
  121. union {
  122. u32 immediate;
  123. u32 csr_offset;
  124. struct csr1212_leaf leaf;
  125. struct csr1212_directory directory;
  126. } value;
  127. struct csr1212_keyval *associate;
  128. atomic_t refcnt;
  129. /* used in generating and/or parsing CSR image */
  130. struct csr1212_keyval *next, *prev; /* flat list of CSR elements */
  131. u32 offset; /* position in CSR from 0xffff f000 0000 */
  132. u8 valid; /* flag indicating keyval has valid data*/
  133. };
  134. struct csr1212_cache_region {
  135. struct csr1212_cache_region *next, *prev;
  136. u32 offset_start; /* inclusive */
  137. u32 offset_end; /* exclusive */
  138. };
  139. struct csr1212_csr_rom_cache {
  140. struct csr1212_csr_rom_cache *next, *prev;
  141. struct csr1212_cache_region *filled_head, *filled_tail;
  142. struct csr1212_keyval *layout_head, *layout_tail;
  143. size_t size;
  144. u32 offset;
  145. struct csr1212_keyval *ext_rom;
  146. size_t len;
  147. /* Must be last */
  148. u32 data[0]; /* older gcc can't handle [] which is standard */
  149. };
  150. struct csr1212_csr {
  151. size_t bus_info_len; /* bus info block length in bytes */
  152. size_t crc_len; /* crc length in bytes */
  153. __be32 *bus_info_data; /* bus info data incl bus name and EUI */
  154. void *private; /* private, bus specific data */
  155. struct csr1212_bus_ops *ops;
  156. struct csr1212_keyval *root_kv;
  157. int max_rom; /* max bytes readable in Config ROM region */
  158. /* Items below used for image parsing and generation */
  159. struct csr1212_csr_rom_cache *cache_head, *cache_tail;
  160. };
  161. struct csr1212_bus_ops {
  162. /* This function is used by csr1212 to read additional information
  163. * from remote nodes when parsing a Config ROM (i.e., read Config ROM
  164. * entries located in the Units Space. Must return 0 on success
  165. * anything else indicates an error. */
  166. int (*bus_read) (struct csr1212_csr *csr, u64 addr,
  167. void *buffer, void *private);
  168. /* This function is used by csr1212 to allocate a region in units space
  169. * in the event that Config ROM entries don't all fit in the predefined
  170. * 1K region. The void *private parameter is private member of struct
  171. * csr1212_csr. */
  172. u64 (*allocate_addr_range) (u64 size, u32 alignment, void *private);
  173. /* This function is used by csr1212 to release a region in units space
  174. * that is no longer needed. */
  175. void (*release_addr) (u64 addr, void *private);
  176. };
  177. /* Descriptor Leaf manipulation macros */
  178. #define CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT 24
  179. #define CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK 0xffffff
  180. #define CSR1212_DESCRIPTOR_LEAF_OVERHEAD (1 * sizeof(u32))
  181. #define CSR1212_DESCRIPTOR_LEAF_TYPE(kv) \
  182. (be32_to_cpu((kv)->value.leaf.data[0]) >> \
  183. CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)
  184. #define CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) \
  185. (be32_to_cpu((kv)->value.leaf.data[0]) & \
  186. CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)
  187. /* Text Descriptor Leaf manipulation macros */
  188. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT 28
  189. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_MASK 0xf /* after shift */
  190. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT 16
  191. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK 0xfff /* after shift */
  192. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK 0xffff
  193. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_OVERHEAD (1 * sizeof(u32))
  194. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) \
  195. (be32_to_cpu((kv)->value.leaf.data[1]) >> \
  196. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT)
  197. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) \
  198. ((be32_to_cpu((kv)->value.leaf.data[1]) >> \
  199. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT) & \
  200. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK)
  201. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) \
  202. (be32_to_cpu((kv)->value.leaf.data[1]) & \
  203. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK)
  204. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv) \
  205. (&((kv)->value.leaf.data[2]))
  206. /* The following 2 function are for creating new Configuration ROM trees. The
  207. * first function is used for both creating local trees and parsing remote
  208. * trees. The second function adds pertinent information to local Configuration
  209. * ROM trees - namely data for the bus information block. */
  210. extern struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops,
  211. size_t bus_info_size,
  212. void *private);
  213. extern void csr1212_init_local_csr(struct csr1212_csr *csr,
  214. const u32 *bus_info_data, int max_rom);
  215. /* Destroy a Configuration ROM tree and release all memory taken by the tree. */
  216. extern void csr1212_destroy_csr(struct csr1212_csr *csr);
  217. /* The following set of functions are fore creating new keyvals for placement in
  218. * a Configuration ROM tree. Code that creates new keyvals with these functions
  219. * must release those keyvals with csr1212_release_keyval() when they are no
  220. * longer needed. */
  221. extern struct csr1212_keyval *csr1212_new_immediate(u8 key, u32 value);
  222. extern struct csr1212_keyval *csr1212_new_directory(u8 key);
  223. extern struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s);
  224. /* The following function manages association between keyvals. Typically,
  225. * Descriptor Leaves and Directories will be associated with another keyval and
  226. * it is desirable for the Descriptor keyval to be place immediately after the
  227. * keyval that it is associated with.
  228. * Take care with subsequent ROM modifications: There is no function to remove
  229. * previously specified associations.
  230. */
  231. extern void csr1212_associate_keyval(struct csr1212_keyval *kv,
  232. struct csr1212_keyval *associate);
  233. /* The following functions manage the association of a keyval and directories.
  234. * A keyval may be attached to more than one directory. */
  235. extern int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
  236. struct csr1212_keyval *kv);
  237. extern void csr1212_detach_keyval_from_directory(struct csr1212_keyval *dir,
  238. struct csr1212_keyval *kv);
  239. /* Creates a complete Configuration ROM image in the list of caches available
  240. * via csr->cache_head. */
  241. extern int csr1212_generate_csr_image(struct csr1212_csr *csr);
  242. /* This is a convience function for reading a block of data out of one of the
  243. * caches in the csr->cache_head list. */
  244. extern int csr1212_read(struct csr1212_csr *csr, u32 offset, void *buffer,
  245. u32 len);
  246. /* The following functions are in place for parsing Configuration ROM images.
  247. * csr1212_parse_keyval() is used should there be a need to directly parse a
  248. * Configuration ROM directly. */
  249. extern int csr1212_parse_keyval(struct csr1212_keyval *kv,
  250. struct csr1212_csr_rom_cache *cache);
  251. extern int csr1212_parse_csr(struct csr1212_csr *csr);
  252. /* This function allocates a new cache which may be used for either parsing or
  253. * generating sub-sets of Configuration ROM images. */
  254. static inline struct csr1212_csr_rom_cache *
  255. csr1212_rom_cache_malloc(u32 offset, size_t size)
  256. {
  257. struct csr1212_csr_rom_cache *cache;
  258. cache = CSR1212_MALLOC(sizeof(*cache) + size);
  259. if (!cache)
  260. return NULL;
  261. cache->next = NULL;
  262. cache->prev = NULL;
  263. cache->filled_head = NULL;
  264. cache->filled_tail = NULL;
  265. cache->layout_head = NULL;
  266. cache->layout_tail = NULL;
  267. cache->offset = offset;
  268. cache->size = size;
  269. cache->ext_rom = NULL;
  270. return cache;
  271. }
  272. /* This function ensures that a keyval contains data when referencing a keyval
  273. * created by parsing a Configuration ROM. */
  274. extern struct csr1212_keyval *
  275. csr1212_get_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv);
  276. /* This function increments the reference count for a keyval should there be a
  277. * need for code to retain a keyval that has been parsed. */
  278. static inline void csr1212_keep_keyval(struct csr1212_keyval *kv)
  279. {
  280. atomic_inc(&kv->refcnt);
  281. smp_mb__after_atomic_inc();
  282. }
  283. /* This function decrements a keyval's reference count and will destroy the
  284. * keyval when there are no more users of the keyval. This should be called by
  285. * any code that calls csr1212_keep_keyval() or any of the keyval creation
  286. * routines csr1212_new_*(). */
  287. extern void csr1212_release_keyval(struct csr1212_keyval *kv);
  288. /*
  289. * This macro allows for looping over the keyval entries in a directory and it
  290. * ensures that keyvals from remote ConfigROMs are parsed properly.
  291. *
  292. * struct csr1212_csr *_csr points to the CSR associated with dir.
  293. * struct csr1212_keyval *_kv points to the current keyval (loop index).
  294. * struct csr1212_keyval *_dir points to the directory to be looped.
  295. * struct csr1212_dentry *_pos is used internally for indexing.
  296. *
  297. * kv will be NULL upon exit of the loop.
  298. */
  299. #define csr1212_for_each_dir_entry(_csr, _kv, _dir, _pos) \
  300. for (csr1212_get_keyval((_csr), (_dir)), \
  301. _pos = (_dir)->value.directory.dentries_head, \
  302. _kv = (_pos) ? csr1212_get_keyval((_csr), _pos->kv) : NULL;\
  303. (_kv) && (_pos); \
  304. (_kv->associate == NULL) ? \
  305. ((_pos = _pos->next), (_kv = (_pos) ? \
  306. csr1212_get_keyval((_csr), _pos->kv) : \
  307. NULL)) : \
  308. (_kv = csr1212_get_keyval((_csr), _kv->associate)))
  309. #endif /* __CSR1212_H__ */