Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/sparc-2.6
[sfrench/cifs-2.6.git] / drivers / ieee1394 / csr1212.h
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
30 #ifndef __CSR1212_H__
31 #define __CSR1212_H__
32
33
34 /* Compatibility layer */
35 #ifdef __KERNEL__
36
37 #include <linux/types.h>
38 #include <linux/slab.h>
39 #include <linux/interrupt.h>
40 #include <linux/vmalloc.h>
41 #include <asm/pgalloc.h>
42
43 #define CSR1212_MALLOC(size)            vmalloc((size))
44 #define CSR1212_FREE(ptr)               vfree(ptr)
45 #define CSR1212_BE16_TO_CPU(quad)       be16_to_cpu(quad)
46 #define CSR1212_CPU_TO_BE16(quad)       cpu_to_be16(quad)
47 #define CSR1212_BE32_TO_CPU(quad)       be32_to_cpu(quad)
48 #define CSR1212_CPU_TO_BE32(quad)       cpu_to_be32(quad)
49 #define CSR1212_BE64_TO_CPU(quad)       be64_to_cpu(quad)
50 #define CSR1212_CPU_TO_BE64(quad)       cpu_to_be64(quad)
51
52 #define CSR1212_LE16_TO_CPU(quad)       le16_to_cpu(quad)
53 #define CSR1212_CPU_TO_LE16(quad)       cpu_to_le16(quad)
54 #define CSR1212_LE32_TO_CPU(quad)       le32_to_cpu(quad)
55 #define CSR1212_CPU_TO_LE32(quad)       cpu_to_le32(quad)
56 #define CSR1212_LE64_TO_CPU(quad)       le64_to_cpu(quad)
57 #define CSR1212_CPU_TO_LE64(quad)       cpu_to_le64(quad)
58
59 #include <linux/errno.h>
60 #define CSR1212_SUCCESS (0)
61 #define CSR1212_EINVAL  (-EINVAL)
62 #define CSR1212_ENOMEM  (-ENOMEM)
63 #define CSR1212_ENOENT  (-ENOENT)
64 #define CSR1212_EIO     (-EIO)
65 #define CSR1212_EBUSY   (-EBUSY)
66
67 #else   /* Userspace */
68
69 #include <sys/types.h>
70 #include <malloc.h>
71 #define CSR1212_MALLOC(size)            malloc(size)
72 #define CSR1212_FREE(ptr)               free(ptr)
73 #include <endian.h>
74 #if __BYTE_ORDER == __LITTLE_ENDIAN
75 #include <byteswap.h>
76 #define CSR1212_BE16_TO_CPU(quad)       bswap_16(quad)
77 #define CSR1212_CPU_TO_BE16(quad)       bswap_16(quad)
78 #define CSR1212_BE32_TO_CPU(quad)       bswap_32(quad)
79 #define CSR1212_CPU_TO_BE32(quad)       bswap_32(quad)
80 #define CSR1212_BE64_TO_CPU(quad)       bswap_64(quad)
81 #define CSR1212_CPU_TO_BE64(quad)       bswap_64(quad)
82
83 #define CSR1212_LE16_TO_CPU(quad)       (quad)
84 #define CSR1212_CPU_TO_LE16(quad)       (quad)
85 #define CSR1212_LE32_TO_CPU(quad)       (quad)
86 #define CSR1212_CPU_TO_LE32(quad)       (quad)
87 #define CSR1212_LE64_TO_CPU(quad)       (quad)
88 #define CSR1212_CPU_TO_LE64(quad)       (quad)
89 #else
90 #define CSR1212_BE16_TO_CPU(quad)       (quad)
91 #define CSR1212_CPU_TO_BE16(quad)       (quad)
92 #define CSR1212_BE32_TO_CPU(quad)       (quad)
93 #define CSR1212_CPU_TO_BE32(quad)       (quad)
94 #define CSR1212_BE64_TO_CPU(quad)       (quad)
95 #define CSR1212_CPU_TO_BE64(quad)       (quad)
96
97 #define CSR1212_LE16_TO_CPU(quad)       bswap_16(quad)
98 #define CSR1212_CPU_TO_LE16(quad)       bswap_16(quad)
99 #define CSR1212_LE32_TO_CPU(quad)       bswap_32(quad)
100 #define CSR1212_CPU_TO_LE32(quad)       bswap_32(quad)
101 #define CSR1212_LE64_TO_CPU(quad)       bswap_64(quad)
102 #define CSR1212_CPU_TO_LE64(quad)       bswap_64(quad)
103 #endif
104
105 #include <errno.h>
106 #define CSR1212_SUCCESS (0)
107 #define CSR1212_EINVAL  (EINVAL)
108 #define CSR1212_ENOMEM  (ENOMEM)
109 #define CSR1212_ENOENT  (ENOENT)
110 #define CSR1212_EIO     (EIO)
111 #define CSR1212_EBUSY   (EBUSY)
112
113 #endif
114
115
116 #define CSR1212_KV_VAL_MASK                     0xffffff
117 #define CSR1212_KV_KEY_SHIFT                    24
118 #define CSR1212_KV_KEY_TYPE_SHIFT               6
119 #define CSR1212_KV_KEY_ID_MASK                  0x3f
120 #define CSR1212_KV_KEY_TYPE_MASK                0x3             /* After shift */
121
122
123 /* CSR 1212 key types */
124 #define CSR1212_KV_TYPE_IMMEDIATE               0
125 #define CSR1212_KV_TYPE_CSR_OFFSET              1
126 #define CSR1212_KV_TYPE_LEAF                    2
127 #define CSR1212_KV_TYPE_DIRECTORY               3
128
129
130 /* CSR 1212 key ids */
131 #define CSR1212_KV_ID_DESCRIPTOR                0x01
132 #define CSR1212_KV_ID_BUS_DEPENDENT_INFO        0x02
133 #define CSR1212_KV_ID_VENDOR                    0x03
134 #define CSR1212_KV_ID_HARDWARE_VERSION          0x04
135 #define CSR1212_KV_ID_MODULE                    0x07
136 #define CSR1212_KV_ID_NODE_CAPABILITIES         0x0C
137 #define CSR1212_KV_ID_EUI_64                    0x0D
138 #define CSR1212_KV_ID_UNIT                      0x11
139 #define CSR1212_KV_ID_SPECIFIER_ID              0x12
140 #define CSR1212_KV_ID_VERSION                   0x13
141 #define CSR1212_KV_ID_DEPENDENT_INFO            0x14
142 #define CSR1212_KV_ID_UNIT_LOCATION             0x15
143 #define CSR1212_KV_ID_MODEL                     0x17
144 #define CSR1212_KV_ID_INSTANCE                  0x18
145 #define CSR1212_KV_ID_KEYWORD                   0x19
146 #define CSR1212_KV_ID_FEATURE                   0x1A
147 #define CSR1212_KV_ID_EXTENDED_ROM              0x1B
148 #define CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID 0x1C
149 #define CSR1212_KV_ID_EXTENDED_KEY              0x1D
150 #define CSR1212_KV_ID_EXTENDED_DATA             0x1E
151 #define CSR1212_KV_ID_MODIFIABLE_DESCRIPTOR     0x1F
152 #define CSR1212_KV_ID_DIRECTORY_ID              0x20
153 #define CSR1212_KV_ID_REVISION                  0x21
154
155
156 /* IEEE 1212 Address space map */
157 #define CSR1212_ALL_SPACE_BASE                  (0x000000000000ULL)
158 #define CSR1212_ALL_SPACE_SIZE                  (1ULL << 48)
159 #define CSR1212_ALL_SPACE_END                   (CSR1212_ALL_SPACE_BASE + CSR1212_ALL_SPACE_SIZE)
160
161 #define  CSR1212_MEMORY_SPACE_BASE              (0x000000000000ULL)
162 #define  CSR1212_MEMORY_SPACE_SIZE              ((256ULL * (1ULL << 40)) - (512ULL * (1ULL << 20)))
163 #define  CSR1212_MEMORY_SPACE_END               (CSR1212_MEMORY_SPACE_BASE + CSR1212_MEMORY_SPACE_SIZE)
164
165 #define  CSR1212_PRIVATE_SPACE_BASE             (0xffffe0000000ULL)
166 #define  CSR1212_PRIVATE_SPACE_SIZE             (256ULL * (1ULL << 20))
167 #define  CSR1212_PRIVATE_SPACE_END              (CSR1212_PRIVATE_SPACE_BASE + CSR1212_PRIVATE_SPACE_SIZE)
168
169 #define  CSR1212_REGISTER_SPACE_BASE            (0xfffff0000000ULL)
170 #define  CSR1212_REGISTER_SPACE_SIZE            (256ULL * (1ULL << 20))
171 #define  CSR1212_REGISTER_SPACE_END             (CSR1212_REGISTER_SPACE_BASE + CSR1212_REGISTER_SPACE_SIZE)
172
173 #define  CSR1212_CSR_ARCH_REG_SPACE_BASE        (0xfffff0000000ULL)
174 #define  CSR1212_CSR_ARCH_REG_SPACE_SIZE        (512)
175 #define  CSR1212_CSR_ARCH_REG_SPACE_END         (CSR1212_CSR_ARCH_REG_SPACE_BASE + CSR1212_CSR_ARCH_REG_SPACE_SIZE)
176 #define  CSR1212_CSR_ARCH_REG_SPACE_OFFSET      (CSR1212_CSR_ARCH_REG_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
177
178 #define  CSR1212_CSR_BUS_DEP_REG_SPACE_BASE     (0xfffff0000200ULL)
179 #define  CSR1212_CSR_BUS_DEP_REG_SPACE_SIZE     (512)
180 #define  CSR1212_CSR_BUS_DEP_REG_SPACE_END      (CSR1212_CSR_BUS_DEP_REG_SPACE_BASE + CSR1212_CSR_BUS_DEP_REG_SPACE_SIZE)
181 #define  CSR1212_CSR_BUS_DEP_REG_SPACE_OFFSET   (CSR1212_CSR_BUS_DEP_REG_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
182
183 #define  CSR1212_CONFIG_ROM_SPACE_BASE          (0xfffff0000400ULL)
184 #define  CSR1212_CONFIG_ROM_SPACE_SIZE          (1024)
185 #define  CSR1212_CONFIG_ROM_SPACE_END           (CSR1212_CONFIG_ROM_SPACE_BASE + CSR1212_CONFIG_ROM_SPACE_SIZE)
186 #define  CSR1212_CONFIG_ROM_SPACE_OFFSET        (CSR1212_CONFIG_ROM_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
187
188 #define  CSR1212_UNITS_SPACE_BASE               (0xfffff0000800ULL)
189 #define  CSR1212_UNITS_SPACE_SIZE               ((256ULL * (1ULL << 20)) - 2048)
190 #define  CSR1212_UNITS_SPACE_END                (CSR1212_UNITS_SPACE_BASE + CSR1212_UNITS_SPACE_SIZE)
191 #define  CSR1212_UNITS_SPACE_OFFSET             (CSR1212_UNITS_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
192
193 #define  CSR1212_EXTENDED_ROM_SIZE              (0x10000 * sizeof(u_int32_t))
194
195
196 /* Config ROM image structures */
197 struct csr1212_bus_info_block_img {
198         u_int8_t length;
199         u_int8_t crc_length;
200         u_int16_t crc;
201
202         /* Must be last */
203         u_int32_t data[0];      /* older gcc can't handle [] which is standard */
204 };
205
206 #define CSR1212_KV_KEY(quad)            (CSR1212_BE32_TO_CPU(quad) >> CSR1212_KV_KEY_SHIFT)
207 #define CSR1212_KV_KEY_TYPE(quad)       (CSR1212_KV_KEY(quad) >> CSR1212_KV_KEY_TYPE_SHIFT)
208 #define CSR1212_KV_KEY_ID(quad)         (CSR1212_KV_KEY(quad) & CSR1212_KV_KEY_ID_MASK)
209 #define CSR1212_KV_VAL(quad)            (CSR1212_BE32_TO_CPU(quad) & CSR1212_KV_VAL_MASK)
210
211 #define CSR1212_SET_KV_KEY(quad, key)   ((quad) = \
212         CSR1212_CPU_TO_BE32(CSR1212_KV_VAL(quad) | ((key) << CSR1212_KV_KEY_SHIFT)))
213 #define CSR1212_SET_KV_VAL(quad, val)   ((quad) = \
214         CSR1212_CPU_TO_BE32((CSR1212_KV_KEY(quad) << CSR1212_KV_KEY_SHIFT) | (val)))
215 #define CSR1212_SET_KV_TYPEID(quad, type, id)   ((quad) = \
216         CSR1212_CPU_TO_BE32(CSR1212_KV_VAL(quad) | \
217         (((((type) & CSR1212_KV_KEY_TYPE_MASK) << CSR1212_KV_KEY_TYPE_SHIFT) | \
218           ((id) & CSR1212_KV_KEY_ID_MASK)) << CSR1212_KV_KEY_SHIFT)))
219
220 typedef u_int32_t csr1212_quad_t;
221
222
223 struct csr1212_keyval_img {
224         u_int16_t length;
225         u_int16_t crc;
226
227         /* Must be last */
228         csr1212_quad_t data[0]; /* older gcc can't handle [] which is standard */
229 };
230
231 struct csr1212_leaf {
232         int len;
233         u_int32_t *data;
234 };
235
236 struct csr1212_dentry {
237         struct csr1212_dentry *next, *prev;
238         struct csr1212_keyval *kv;
239 };
240
241 struct csr1212_directory {
242         int len;
243         struct csr1212_dentry *dentries_head, *dentries_tail;
244 };
245
246 struct csr1212_keyval {
247         struct {
248                 u_int8_t type;
249                 u_int8_t id;
250         } key;
251         union {
252                 u_int32_t immediate;
253                 u_int32_t csr_offset;
254                 struct csr1212_leaf leaf;
255                 struct csr1212_directory directory;
256         } value;
257         struct csr1212_keyval *associate;
258         int refcnt;
259
260         /* used in generating and/or parsing CSR image */
261         struct csr1212_keyval *next, *prev;     /* flat list of CSR elements */
262         u_int32_t offset;       /* position in CSR from 0xffff f000 0000 */
263         u_int8_t valid;         /* flag indicating keyval has valid data*/
264 };
265
266
267 struct csr1212_cache_region {
268         struct csr1212_cache_region *next, *prev;
269         u_int32_t offset_start;         /* inclusive */
270         u_int32_t offset_end;           /* exclusive */
271 };
272
273 struct csr1212_csr_rom_cache {
274         struct csr1212_csr_rom_cache *next, *prev;
275         struct csr1212_cache_region *filled_head, *filled_tail;
276         struct csr1212_keyval *layout_head, *layout_tail;
277         size_t size;
278         u_int32_t offset;
279         struct csr1212_keyval *ext_rom;
280         size_t len;
281
282         /* Must be last */
283         u_int32_t data[0];      /* older gcc can't handle [] which is standard */
284 };
285
286 struct csr1212_csr {
287         size_t bus_info_len;    /* bus info block length in bytes */
288         size_t crc_len;         /* crc length in bytes */
289         u_int32_t *bus_info_data;       /* bus info data incl bus name and EUI */
290
291         void *private;          /* private, bus specific data */
292         struct csr1212_bus_ops *ops;
293
294         struct csr1212_keyval *root_kv;
295
296         int max_rom;            /* max bytes readable in Config ROM region */
297
298         /* Items below used for image parsing and generation */
299         struct csr1212_csr_rom_cache *cache_head, *cache_tail;
300 };
301
302 struct csr1212_bus_ops {
303         /* This function is used by csr1212 to read additional information
304          * from remote nodes when parsing a Config ROM (i.e., read Config ROM
305          * entries located in the Units Space.  Must return 0 on success
306          * anything else indicates an error. */
307         int (*bus_read) (struct csr1212_csr *csr, u_int64_t addr,
308                          u_int16_t length, void *buffer, void *private);
309
310         /* This function is used by csr1212 to allocate a region in units space
311          * in the event that Config ROM entries don't all fit in the predefined
312          * 1K region.  The void *private parameter is private member of struct
313          * csr1212_csr. */
314         u_int64_t (*allocate_addr_range) (u_int64_t size, u_int32_t alignment,
315                                           void *private);
316
317
318         /* This function is used by csr1212 to release a region in units space
319          * that is no longer needed. */
320         void (*release_addr) (u_int64_t addr, void *private);
321
322         /* This function is used by csr1212 to determine the max read request
323          * supported by a remote node when reading the ConfigROM space.  Must
324          * return 0, 1, or 2 per IEEE 1212.  */
325         int (*get_max_rom) (u_int32_t *bus_info, void *private);
326 };
327
328
329
330
331 /* Descriptor Leaf manipulation macros */
332 #define CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT 24
333 #define CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK 0xffffff
334 #define CSR1212_DESCRIPTOR_LEAF_OVERHEAD (1 * sizeof(u_int32_t))
335
336 #define CSR1212_DESCRIPTOR_LEAF_TYPE(kv) \
337         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[0]) >> CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)
338 #define CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) \
339         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[0]) & \
340          CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)
341 #define CSR1212_DESCRIPTOR_LEAF_DATA(kv) \
342         (&((kv)->value.leaf.data[1]))
343
344 #define CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, type) \
345         ((kv)->value.leaf.data[0] = \
346          CSR1212_CPU_TO_BE32(CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) | \
347                              ((type) << CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)))
348 #define CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, spec_id) \
349         ((kv)->value.leaf.data[0] = \
350          CSR1212_CPU_TO_BE32((CSR1212_DESCRIPTOR_LEAF_TYPE(kv) << \
351                               CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT) | \
352                              ((spec_id) & CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)))
353
354 /* Text Descriptor Leaf manipulation macros */
355 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT 28
356 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_MASK 0xf /* after shift */
357 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT 16
358 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK 0xfff  /* after shift */
359 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK 0xffff
360 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_OVERHEAD (1 * sizeof(u_int32_t))
361
362 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) \
363         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[1]) >> \
364          CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT)
365 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) \
366         ((CSR1212_BE32_TO_CPU((kv)->value.leaf.data[1]) >> \
367                              CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT) & \
368                             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK)
369 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) \
370         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[1]) & \
371          CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK)
372 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv) \
373         (&((kv)->value.leaf.data[2]))
374
375 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_WIDTH(kv, width) \
376         ((kv)->value.leaf.data[1] = \
377          ((kv)->value.leaf.data[1] & \
378           CSR1212_CPU_TO_BE32(~(CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_MASK << \
379                                 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT))) | \
380          CSR1212_CPU_TO_BE32(((width) & \
381                               CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_MASK) << \
382                              CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT))
383 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_CHAR_SET(kv, char_set) \
384         ((kv)->value.leaf.data[1] = \
385          ((kv)->value.leaf.data[1] & \
386           CSR1212_CPU_TO_BE32(~(CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK << \
387                                 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT))) | \
388          CSR1212_CPU_TO_BE32(((char_set) & \
389                               CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK) << \
390                              CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT))
391 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language) \
392         ((kv)->value.leaf.data[1] = \
393          ((kv)->value.leaf.data[1] & \
394           CSR1212_CPU_TO_BE32(~(CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK))) | \
395          CSR1212_CPU_TO_BE32(((language) & \
396                               CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK)))
397
398
399 /* Icon Descriptor Leaf manipulation macros */
400 #define CSR1212_ICON_DESCRIPTOR_LEAF_VERSION_MASK 0xffffff
401 #define CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_SHIFT 30
402 #define CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_MASK 0x3 /* after shift */
403 #define CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_SHIFT 16
404 #define CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_MASK 0xf /* after shift */
405 #define CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE_MASK 0xffff
406 #define CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_SHIFT 16
407 #define CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_MASK 0xffff /* after shift */
408 #define CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN_MASK 0xffff
409 #define CSR1212_ICON_DESCRIPTOR_LEAF_OVERHEAD (3 * sizeof(u_int32_t))
410
411 #define CSR1212_ICON_DESCRIPTOR_LEAF_VERSION(kv) \
412         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[2]) & \
413          CSR1212_ICON_DESCRIPTOR_LEAF_VERSION_MASK)
414
415 #define CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH(kv) \
416         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[3]) >> \
417          CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_SHIFT)
418
419 #define CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE(kv) \
420         ((CSR1212_BE32_TO_CPU((kv)->value.leaf.data[3]) >> \
421           CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_SHIFT) & \
422          CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_MASK)
423
424 #define CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE(kv) \
425         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[3]) & \
426          CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE_MASK)
427
428 #define CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN(kv) \
429         ((CSR1212_BE32_TO_CPU((kv)->value.leaf.data[4]) >> \
430           CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_HSCAN_SHIFT) & \
431          CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_HSCAN_MASK)
432
433 #define CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN(kv) \
434         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[4]) & \
435          CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN_MASK)
436
437 #define CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE(kv) \
438         (&((kv)->value.leaf.data[5]))
439
440 static inline u_int32_t *CSR1212_ICON_DESCRIPTOR_LEAF_PIXELS(struct csr1212_keyval *kv)
441 {
442         static const int pd[4] = { 0, 4, 16, 256 };
443         static const int cs[16] = { 4, 2 };
444         int ps = pd[CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH(kv)];
445
446         return &kv->value.leaf.data[5 +
447                                     (ps * cs[CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE(kv)]) /
448                            sizeof(u_int32_t)];
449 }
450
451 #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_VERSION(kv, version) \
452         ((kv)->value.leaf.data[2] = \
453          ((kv)->value.leaf.data[2] & \
454           CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_VERSION_MASK))) | \
455          CSR1212_CPU_TO_BE32(((version) & \
456                               CSR1212_ICON_DESCRIPTOR_LEAF_VERSION_MASK)))
457
458 #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_PALETTE_DEPTH(kv, palette_depth) \
459         ((kv)->value.leaf.data[3] = \
460          ((kv)->value.leaf.data[3] & \
461           CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_MASK << \
462                                 CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_SHIFT))) | \
463          CSR1212_CPU_TO_BE32(((palette_depth) & \
464                               CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_MASK) << \
465                              CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_SHIFT))
466
467 #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_COLOR_SPACE(kv, color_space) \
468         ((kv)->value.leaf.data[3] = \
469          ((kv)->value.leaf.data[3] & \
470           CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_MASK << \
471                                 CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_SHIFT))) | \
472          CSR1212_CPU_TO_BE32(((color_space) & \
473                               CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_MASK) << \
474                              CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_SHIFT))
475
476 #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language) \
477         ((kv)->value.leaf.data[3] = \
478          ((kv)->value.leaf.data[3] & \
479           CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE_MASK))) | \
480          CSR1212_CPU_TO_BE32(((language) & \
481                               CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE_MASK)))
482
483 #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_HSCAN(kv, hscan) \
484         ((kv)->value.leaf.data[4] = \
485          ((kv)->value.leaf.data[4] & \
486           CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_MASK << \
487                                 CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_SHIFT))) | \
488          CSR1212_CPU_TO_BE32(((hscan) & \
489                               CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_MASK) << \
490                              CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_SHIFT))
491
492 #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_VSCAN(kv, vscan) \
493         ((kv)->value.leaf.data[4] = \
494          (((kv)->value.leaf.data[4] & \
495           CSR1212_CPU_TO_BE32(~CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN_MASK))) | \
496          CSR1212_CPU_TO_BE32(((vscan) & \
497                               CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN_MASK)))
498
499
500 /* Modifiable Descriptor Leaf manipulation macros */
501 #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_SHIFT 16
502 #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_MASK 0xffff
503 #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_HI_SHIFT 32
504 #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_HI_MASK 0xffff
505 #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_LO_MASK 0xffffffffULL
506
507 #define CSR1212_MODIFIABLE_DESCRIPTOR_MAX_SIZE(kv) \
508         CSR1212_BE16_TO_CPU((kv)->value.leaf.data[0] >> CSR1212_MODIFIABLE_DESCRIPTOR_MAX_SIZE_SHIFT)
509
510 #define CSR1212_MODIFIABLE_DESCRIPTOR_ADDRESS(kv) \
511         (CSR1212_BE16_TO_CPU(((u_int64_t)((kv)->value.leaf.data[0])) << \
512                              CSR1212_MODIFIABLE_DESCRIPTOR_ADDR_HI_SHIFT) | \
513          CSR1212_BE32_TO_CPU((kv)->value.leaf.data[1]))
514
515 #define CSR1212_MODIFIABLE_DESCRIPTOR_SET_MAX_SIZE(kv, size) \
516         ((kv)->value.leaf.data[0] = \
517          ((kv)->value.leaf.data[0] & \
518           CSR1212_CPU_TO_BE32(~(CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_MASK << \
519                                 CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_SHIFT))) | \
520          CSR1212_CPU_TO_BE32(((size) & \
521                               CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_MASK) << \
522                              CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_SHIFT))
523
524 #define CSR1212_MODIFIABLE_DESCRIPTOR_SET_ADDRESS_HI(kv, addr) \
525         ((kv)->value.leaf.data[0] = \
526          ((kv)->value.leaf.data[0] & \
527           CSR1212_CPU_TO_BE32(~(CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_HI_MASK))) | \
528           CSR1212_CPU_TO_BE32(((addr) & \
529                                CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_HI_MASK)))
530
531 #define CSR1212_MODIFIABLE_DESCRIPTOR_SET_ADDRESS_LO(kv, addr) \
532         ((kv)->value.leaf.data[1] = \
533          CSR1212_CPU_TO_BE32(addr & CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_LO_MASK))
534
535
536
537 /* The following 2 function are for creating new Configuration ROM trees.  The
538  * first function is used for both creating local trees and parsing remote
539  * trees.  The second function adds pertinent information to local Configuration
540  * ROM trees - namely data for the bus information block. */
541 extern struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops,
542                                               size_t bus_info_size,
543                                               void *private);
544 extern void csr1212_init_local_csr(struct csr1212_csr *csr,
545                                    const u_int32_t *bus_info_data, int max_rom);
546
547
548 /* The following function destroys a Configuration ROM tree and release all
549  * memory taken by the tree. */
550 extern void csr1212_destroy_csr(struct csr1212_csr *csr);
551
552
553 /* The following set of functions are fore creating new keyvals for placement in
554  * a Configuration ROM tree.  Code that creates new keyvals with these functions
555  * must release those keyvals with csr1212_release_keyval() when they are no
556  * longer needed. */
557 extern struct csr1212_keyval *csr1212_new_immediate(u_int8_t key, u_int32_t value);
558 extern struct csr1212_keyval *csr1212_new_leaf(u_int8_t key, const void *data,
559                                                size_t data_len);
560 extern struct csr1212_keyval *csr1212_new_csr_offset(u_int8_t key,
561                                                      u_int32_t csr_offset);
562 extern struct csr1212_keyval *csr1212_new_directory(u_int8_t key);
563 extern struct csr1212_keyval *csr1212_new_extended_immediate(u_int32_t spec,
564                                                              u_int32_t key,
565                                                              u_int32_t value);
566 extern struct csr1212_keyval *csr1212_new_extended_leaf(u_int32_t spec,
567                                                         u_int32_t key,
568                                                         const void *data,
569                                                         size_t data_len);
570 extern struct csr1212_keyval *csr1212_new_descriptor_leaf(u_int8_t dtype,
571                                                           u_int32_t specifier_id,
572                                                           const void *data,
573                                                           size_t data_len);
574 extern struct csr1212_keyval *csr1212_new_textual_descriptor_leaf(u_int8_t cwidth,
575                                                                   u_int16_t cset,
576                                                                   u_int16_t language,
577                                                                   const void *data,
578                                                                   size_t data_len);
579 extern struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s);
580 extern struct csr1212_keyval *csr1212_new_icon_descriptor_leaf(u_int32_t version,
581                                                                u_int8_t palette_depth,
582                                                                u_int8_t color_space,
583                                                                u_int16_t language,
584                                                                u_int16_t hscan,
585                                                                u_int16_t vscan,
586                                                                u_int32_t *palette,
587                                                                u_int32_t *pixels);
588 extern struct csr1212_keyval *csr1212_new_modifiable_descriptor_leaf(u_int16_t max_size,
589                                                                      u_int64_t address);
590 extern struct csr1212_keyval *csr1212_new_keyword_leaf(int strc,
591                                                        const char *strv[]);
592
593
594 /* The following functions manage association between keyvals.  Typically,
595  * Descriptor Leaves and Directories will be associated with another keyval and
596  * it is desirable for the Descriptor keyval to be place immediately after the
597  * keyval that it is associated with.*/
598 extern int csr1212_associate_keyval(struct csr1212_keyval *kv,
599                                     struct csr1212_keyval *associate);
600 extern void csr1212_disassociate_keyval(struct csr1212_keyval *kv);
601
602
603 /* The following functions manage the association of a keyval and directories.
604  * A keyval may be attached to more than one directory. */
605 extern int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
606                                               struct csr1212_keyval *kv);
607 extern void csr1212_detach_keyval_from_directory(struct csr1212_keyval *dir,
608                                                  struct csr1212_keyval *kv);
609
610
611 /* The following functions create a Configuration ROM image from the tree of
612  * keyvals provided.  csr1212_generate_csr_image() creates a complete image in
613  * the list of caches available via csr->cache_head.  The other functions are
614  * provided should there be a need to create a flat image without restrictions
615  * placed by IEEE 1212. */
616 extern struct csr1212_keyval *csr1212_generate_positions(struct csr1212_csr_rom_cache *cache,
617                                                          struct csr1212_keyval *start_kv,
618                                                          int start_pos);
619 extern size_t csr1212_generate_layout_order(struct csr1212_keyval *kv);
620 extern void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache);
621 extern int csr1212_generate_csr_image(struct csr1212_csr *csr);
622
623
624 /* This is a convience function for reading a block of data out of one of the
625  * caches in the csr->cache_head list. */
626 extern int csr1212_read(struct csr1212_csr *csr, u_int32_t offset, void *buffer,
627                         u_int32_t len);
628
629
630 /* The following functions are in place for parsing Configuration ROM images.
631  * csr1212_parse_keyval() is used should there be a need to directly parse a
632  * Configuration ROM directly. */
633 extern int csr1212_parse_keyval(struct csr1212_keyval *kv,
634                                 struct csr1212_csr_rom_cache *cache);
635 extern int csr1212_parse_csr(struct csr1212_csr *csr);
636
637 /* These are internal functions referenced by inline functions below. */
638 extern int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv);
639 extern void _csr1212_destroy_keyval(struct csr1212_keyval *kv);
640
641
642 /* This function allocates a new cache which may be used for either parsing or
643  * generating sub-sets of Configuration ROM images. */
644 static inline struct csr1212_csr_rom_cache *csr1212_rom_cache_malloc(u_int32_t offset,
645                                                                      size_t size)
646 {
647         struct csr1212_csr_rom_cache *cache;
648
649         cache = CSR1212_MALLOC(sizeof(struct csr1212_csr_rom_cache) + size);
650         if (!cache)
651                 return NULL;
652
653         cache->next = NULL;
654         cache->prev = NULL;
655         cache->filled_head = NULL;
656         cache->filled_tail = NULL;
657         cache->layout_head = NULL;
658         cache->layout_tail = NULL;
659         cache->offset = offset;
660         cache->size = size;
661         cache->ext_rom = NULL;
662
663         return cache;
664 }
665
666
667 /* This function ensures that a keyval contains data when referencing a keyval
668  * created by parsing a Configuration ROM. */
669 static inline struct csr1212_keyval *csr1212_get_keyval(struct csr1212_csr *csr,
670                                                         struct csr1212_keyval *kv)
671 {
672         if (!kv)
673                 return NULL;
674         if (!kv->valid)
675                 if (_csr1212_read_keyval(csr, kv) != CSR1212_SUCCESS)
676                         return NULL;
677         return kv;
678 }
679
680
681 /* This function increments the reference count for a keyval should there be a
682  * need for code to retain a keyval that has been parsed. */
683 static inline void csr1212_keep_keyval(struct csr1212_keyval *kv)
684 {
685         kv->refcnt++;
686 }
687
688
689 /* This function decrements a keyval's reference count and will destroy the
690  * keyval when there are no more users of the keyval.  This should be called by
691  * any code that calls csr1212_keep_keyval() or any of the keyval creation
692  * routines csr1212_new_*(). */
693 static inline void csr1212_release_keyval(struct csr1212_keyval *kv)
694 {
695         if (kv->refcnt > 1)
696                 kv->refcnt--;
697         else
698                 _csr1212_destroy_keyval(kv);
699 }
700
701
702 /*
703  * This macro allows for looping over the keyval entries in a directory and it
704  * ensures that keyvals from remote ConfigROMs are parsed properly.
705  *
706  * _csr is a struct csr1212_csr * that points to CSR associated with dir.
707  * _kv is a struct csr1212_keyval * that'll point to the current keyval (loop index).
708  * _dir is a struct csr1212_keyval * that points to the directory to be looped.
709  * _pos is a struct csr1212_dentry * that is used internally for indexing.
710  *
711  * kv will be NULL upon exit of the loop.
712  */
713 #define csr1212_for_each_dir_entry(_csr, _kv, _dir, _pos)                       \
714         for (csr1212_get_keyval((_csr), (_dir)),                                \
715              _pos = (_dir)->value.directory.dentries_head,                      \
716              _kv = (_pos) ? csr1212_get_keyval((_csr), _pos->kv) : NULL;        \
717              (_kv) && (_pos);                                                   \
718              (_kv->associate == NULL) ?                                         \
719                      ((_pos = _pos->next),                                      \
720                       (_kv = (_pos) ? csr1212_get_keyval((_csr), _pos->kv) :    \
721                           NULL)) :                                              \
722                      (_kv = csr1212_get_keyval((_csr), _kv->associate)))
723
724
725
726 #endif /* __CSR1212_H__ */