Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * csr1212.c -- 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 | ||
31 | /* TODO List: | |
32 | * - Verify interface consistency: i.e., public functions that take a size | |
33 | * parameter expect size to be in bytes. | |
1da177e4 LT |
34 | */ |
35 | ||
7fb9addb SR |
36 | #include <linux/errno.h> |
37 | #include <linux/string.h> | |
64ff7123 | 38 | #include <asm/bug.h> |
7fb9addb | 39 | #include <asm/byteorder.h> |
1da177e4 LT |
40 | |
41 | #include "csr1212.h" | |
42 | ||
43 | ||
44 | /* Permitted key type for each key id */ | |
45 | #define __I (1 << CSR1212_KV_TYPE_IMMEDIATE) | |
46 | #define __C (1 << CSR1212_KV_TYPE_CSR_OFFSET) | |
47 | #define __D (1 << CSR1212_KV_TYPE_DIRECTORY) | |
48 | #define __L (1 << CSR1212_KV_TYPE_LEAF) | |
982610bd | 49 | static const u8 csr1212_key_id_type_map[0x30] = { |
0749aaab | 50 | __C, /* used by Apple iSight */ |
1da177e4 LT |
51 | __D | __L, /* Descriptor */ |
52 | __I | __D | __L, /* Bus_Dependent_Info */ | |
53 | __I | __D | __L, /* Vendor */ | |
54 | __I, /* Hardware_Version */ | |
55 | 0, 0, /* Reserved */ | |
0749aaab AG |
56 | __D | __L | __I, /* Module */ |
57 | __I, 0, 0, 0, /* used by Apple iSight, Reserved */ | |
1da177e4 LT |
58 | __I, /* Node_Capabilities */ |
59 | __L, /* EUI_64 */ | |
60 | 0, 0, 0, /* Reserved */ | |
61 | __D, /* Unit */ | |
62 | __I, /* Specifier_ID */ | |
63 | __I, /* Version */ | |
64 | __I | __C | __D | __L, /* Dependent_Info */ | |
65 | __L, /* Unit_Location */ | |
66 | 0, /* Reserved */ | |
67 | __I, /* Model */ | |
68 | __D, /* Instance */ | |
69 | __L, /* Keyword */ | |
70 | __D, /* Feature */ | |
71 | __L, /* Extended_ROM */ | |
72 | __I, /* Extended_Key_Specifier_ID */ | |
73 | __I, /* Extended_Key */ | |
74 | __I | __C | __D | __L, /* Extended_Data */ | |
75 | __L, /* Modifiable_Descriptor */ | |
76 | __I, /* Directory_ID */ | |
77 | __I, /* Revision */ | |
78 | }; | |
79 | #undef __I | |
80 | #undef __C | |
81 | #undef __D | |
82 | #undef __L | |
83 | ||
84 | ||
982610bd SR |
85 | #define quads_to_bytes(_q) ((_q) * sizeof(u32)) |
86 | #define bytes_to_quads(_b) (((_b) + sizeof(u32) - 1) / sizeof(u32)) | |
1da177e4 | 87 | |
6c88e475 | 88 | static void free_keyval(struct csr1212_keyval *kv) |
1da177e4 LT |
89 | { |
90 | if ((kv->key.type == CSR1212_KV_TYPE_LEAF) && | |
91 | (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)) | |
92 | CSR1212_FREE(kv->value.leaf.data); | |
93 | ||
94 | CSR1212_FREE(kv); | |
95 | } | |
96 | ||
982610bd | 97 | static u16 csr1212_crc16(const u32 *buffer, size_t length) |
1da177e4 LT |
98 | { |
99 | int shift; | |
982610bd SR |
100 | u32 data; |
101 | u16 sum, crc = 0; | |
1da177e4 LT |
102 | |
103 | for (; length; length--) { | |
7fb9addb | 104 | data = be32_to_cpu(*buffer); |
1da177e4 LT |
105 | buffer++; |
106 | for (shift = 28; shift >= 0; shift -= 4 ) { | |
107 | sum = ((crc >> 12) ^ (data >> shift)) & 0xf; | |
108 | crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum); | |
109 | } | |
110 | crc &= 0xffff; | |
111 | } | |
112 | ||
7fb9addb | 113 | return cpu_to_be16(crc); |
1da177e4 LT |
114 | } |
115 | ||
116 | #if 0 | |
117 | /* Microsoft computes the CRC with the bytes in reverse order. Therefore we | |
118 | * have a special version of the CRC algorithm to account for their buggy | |
119 | * software. */ | |
982610bd | 120 | static u16 csr1212_msft_crc16(const u32 *buffer, size_t length) |
1da177e4 LT |
121 | { |
122 | int shift; | |
982610bd SR |
123 | u32 data; |
124 | u16 sum, crc = 0; | |
1da177e4 LT |
125 | |
126 | for (; length; length--) { | |
7fb9addb | 127 | data = le32_to_cpu(*buffer); |
1da177e4 LT |
128 | buffer++; |
129 | for (shift = 28; shift >= 0; shift -= 4 ) { | |
130 | sum = ((crc >> 12) ^ (data >> shift)) & 0xf; | |
131 | crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum); | |
132 | } | |
133 | crc &= 0xffff; | |
134 | } | |
135 | ||
7fb9addb | 136 | return cpu_to_be16(crc); |
1da177e4 LT |
137 | } |
138 | #endif | |
139 | ||
6c88e475 SR |
140 | static struct csr1212_dentry * |
141 | csr1212_find_keyval(struct csr1212_keyval *dir, struct csr1212_keyval *kv) | |
1da177e4 LT |
142 | { |
143 | struct csr1212_dentry *pos; | |
144 | ||
145 | for (pos = dir->value.directory.dentries_head; | |
c868ae2a | 146 | pos != NULL; pos = pos->next) |
1da177e4 LT |
147 | if (pos->kv == kv) |
148 | return pos; | |
1da177e4 LT |
149 | return NULL; |
150 | } | |
151 | ||
6c88e475 | 152 | static struct csr1212_keyval * |
982610bd | 153 | csr1212_find_keyval_offset(struct csr1212_keyval *kv_list, u32 offset) |
1da177e4 LT |
154 | { |
155 | struct csr1212_keyval *kv; | |
156 | ||
c868ae2a | 157 | for (kv = kv_list->next; kv && (kv != kv_list); kv = kv->next) |
1da177e4 LT |
158 | if (kv->offset == offset) |
159 | return kv; | |
1da177e4 LT |
160 | return NULL; |
161 | } | |
162 | ||
163 | ||
164 | /* Creation Routines */ | |
6c88e475 | 165 | |
1da177e4 LT |
166 | struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops, |
167 | size_t bus_info_size, void *private) | |
168 | { | |
169 | struct csr1212_csr *csr; | |
170 | ||
171 | csr = CSR1212_MALLOC(sizeof(*csr)); | |
172 | if (!csr) | |
173 | return NULL; | |
174 | ||
175 | csr->cache_head = | |
176 | csr1212_rom_cache_malloc(CSR1212_CONFIG_ROM_SPACE_OFFSET, | |
177 | CSR1212_CONFIG_ROM_SPACE_SIZE); | |
178 | if (!csr->cache_head) { | |
179 | CSR1212_FREE(csr); | |
180 | return NULL; | |
181 | } | |
182 | ||
183 | /* The keyval key id is not used for the root node, but a valid key id | |
184 | * that can be used for a directory needs to be passed to | |
185 | * csr1212_new_directory(). */ | |
186 | csr->root_kv = csr1212_new_directory(CSR1212_KV_ID_VENDOR); | |
187 | if (!csr->root_kv) { | |
188 | CSR1212_FREE(csr->cache_head); | |
189 | CSR1212_FREE(csr); | |
190 | return NULL; | |
191 | } | |
192 | ||
193 | csr->bus_info_data = csr->cache_head->data; | |
194 | csr->bus_info_len = bus_info_size; | |
195 | csr->crc_len = bus_info_size; | |
196 | csr->ops = ops; | |
197 | csr->private = private; | |
198 | csr->cache_tail = csr->cache_head; | |
199 | ||
200 | return csr; | |
201 | } | |
202 | ||
1da177e4 | 203 | void csr1212_init_local_csr(struct csr1212_csr *csr, |
982610bd | 204 | const u32 *bus_info_data, int max_rom) |
1da177e4 LT |
205 | { |
206 | static const int mr_map[] = { 4, 64, 1024, 0 }; | |
207 | ||
1934b8b6 | 208 | BUG_ON(max_rom & ~0x3); |
1da177e4 LT |
209 | csr->max_rom = mr_map[max_rom]; |
210 | memcpy(csr->bus_info_data, bus_info_data, csr->bus_info_len); | |
211 | } | |
212 | ||
982610bd | 213 | static struct csr1212_keyval *csr1212_new_keyval(u8 type, u8 key) |
1da177e4 LT |
214 | { |
215 | struct csr1212_keyval *kv; | |
216 | ||
217 | if (key < 0x30 && ((csr1212_key_id_type_map[key] & (1 << type)) == 0)) | |
218 | return NULL; | |
219 | ||
220 | kv = CSR1212_MALLOC(sizeof(*kv)); | |
221 | if (!kv) | |
222 | return NULL; | |
223 | ||
224 | kv->key.type = type; | |
225 | kv->key.id = key; | |
226 | ||
227 | kv->associate = NULL; | |
228 | kv->refcnt = 1; | |
229 | ||
230 | kv->next = NULL; | |
231 | kv->prev = NULL; | |
232 | kv->offset = 0; | |
233 | kv->valid = 0; | |
234 | return kv; | |
235 | } | |
236 | ||
982610bd | 237 | struct csr1212_keyval *csr1212_new_immediate(u8 key, u32 value) |
1da177e4 | 238 | { |
c868ae2a | 239 | struct csr1212_keyval *kv; |
1da177e4 | 240 | |
c868ae2a | 241 | kv = csr1212_new_keyval(CSR1212_KV_TYPE_IMMEDIATE, key); |
1da177e4 LT |
242 | if (!kv) |
243 | return NULL; | |
244 | ||
245 | kv->value.immediate = value; | |
246 | kv->valid = 1; | |
247 | return kv; | |
248 | } | |
249 | ||
6c88e475 | 250 | static struct csr1212_keyval * |
982610bd | 251 | csr1212_new_leaf(u8 key, const void *data, size_t data_len) |
1da177e4 | 252 | { |
c868ae2a | 253 | struct csr1212_keyval *kv; |
1da177e4 | 254 | |
c868ae2a | 255 | kv = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, key); |
1da177e4 LT |
256 | if (!kv) |
257 | return NULL; | |
258 | ||
259 | if (data_len > 0) { | |
260 | kv->value.leaf.data = CSR1212_MALLOC(data_len); | |
261 | if (!kv->value.leaf.data) { | |
262 | CSR1212_FREE(kv); | |
263 | return NULL; | |
264 | } | |
265 | ||
266 | if (data) | |
267 | memcpy(kv->value.leaf.data, data, data_len); | |
268 | } else { | |
269 | kv->value.leaf.data = NULL; | |
270 | } | |
271 | ||
272 | kv->value.leaf.len = bytes_to_quads(data_len); | |
273 | kv->offset = 0; | |
274 | kv->valid = 1; | |
275 | ||
276 | return kv; | |
277 | } | |
278 | ||
6c88e475 | 279 | static struct csr1212_keyval * |
982610bd | 280 | csr1212_new_csr_offset(u8 key, u32 csr_offset) |
1da177e4 | 281 | { |
c868ae2a | 282 | struct csr1212_keyval *kv; |
1da177e4 | 283 | |
c868ae2a | 284 | kv = csr1212_new_keyval(CSR1212_KV_TYPE_CSR_OFFSET, key); |
1da177e4 LT |
285 | if (!kv) |
286 | return NULL; | |
287 | ||
288 | kv->value.csr_offset = csr_offset; | |
289 | ||
290 | kv->offset = 0; | |
291 | kv->valid = 1; | |
292 | return kv; | |
293 | } | |
294 | ||
982610bd | 295 | struct csr1212_keyval *csr1212_new_directory(u8 key) |
1da177e4 | 296 | { |
c868ae2a | 297 | struct csr1212_keyval *kv; |
1da177e4 | 298 | |
c868ae2a | 299 | kv = csr1212_new_keyval(CSR1212_KV_TYPE_DIRECTORY, key); |
1da177e4 LT |
300 | if (!kv) |
301 | return NULL; | |
302 | ||
303 | kv->value.directory.len = 0; | |
304 | kv->offset = 0; | |
305 | kv->value.directory.dentries_head = NULL; | |
306 | kv->value.directory.dentries_tail = NULL; | |
307 | kv->valid = 1; | |
308 | return kv; | |
309 | } | |
310 | ||
64ff7123 SR |
311 | void csr1212_associate_keyval(struct csr1212_keyval *kv, |
312 | struct csr1212_keyval *associate) | |
1da177e4 | 313 | { |
64ff7123 SR |
314 | BUG_ON(!kv || !associate || kv->key.id == CSR1212_KV_ID_DESCRIPTOR || |
315 | (associate->key.id != CSR1212_KV_ID_DESCRIPTOR && | |
316 | associate->key.id != CSR1212_KV_ID_DEPENDENT_INFO && | |
317 | associate->key.id != CSR1212_KV_ID_EXTENDED_KEY && | |
318 | associate->key.id != CSR1212_KV_ID_EXTENDED_DATA && | |
319 | associate->key.id < 0x30) || | |
320 | (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID && | |
c868ae2a | 321 | associate->key.id != CSR1212_KV_ID_EXTENDED_KEY) || |
64ff7123 SR |
322 | (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY && |
323 | associate->key.id != CSR1212_KV_ID_EXTENDED_DATA) || | |
324 | (associate->key.id == CSR1212_KV_ID_EXTENDED_KEY && | |
325 | kv->key.id != CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) || | |
326 | (associate->key.id == CSR1212_KV_ID_EXTENDED_DATA && | |
327 | kv->key.id != CSR1212_KV_ID_EXTENDED_KEY)); | |
1da177e4 LT |
328 | |
329 | if (kv->associate) | |
330 | csr1212_release_keyval(kv->associate); | |
331 | ||
332 | associate->refcnt++; | |
333 | kv->associate = associate; | |
1da177e4 LT |
334 | } |
335 | ||
336 | int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir, | |
337 | struct csr1212_keyval *kv) | |
338 | { | |
339 | struct csr1212_dentry *dentry; | |
340 | ||
64ff7123 | 341 | BUG_ON(!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY); |
1da177e4 LT |
342 | |
343 | dentry = CSR1212_MALLOC(sizeof(*dentry)); | |
344 | if (!dentry) | |
7fb9addb | 345 | return -ENOMEM; |
1da177e4 LT |
346 | |
347 | dentry->kv = kv; | |
348 | ||
349 | kv->refcnt++; | |
350 | ||
351 | dentry->next = NULL; | |
352 | dentry->prev = dir->value.directory.dentries_tail; | |
353 | ||
354 | if (!dir->value.directory.dentries_head) | |
355 | dir->value.directory.dentries_head = dentry; | |
356 | ||
357 | if (dir->value.directory.dentries_tail) | |
358 | dir->value.directory.dentries_tail->next = dentry; | |
359 | dir->value.directory.dentries_tail = dentry; | |
360 | ||
361 | return CSR1212_SUCCESS; | |
362 | } | |
363 | ||
6c88e475 SR |
364 | #define CSR1212_DESCRIPTOR_LEAF_DATA(kv) \ |
365 | (&((kv)->value.leaf.data[1])) | |
366 | ||
367 | #define CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, type) \ | |
368 | ((kv)->value.leaf.data[0] = \ | |
7fb9addb SR |
369 | cpu_to_be32(CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) | \ |
370 | ((type) << CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT))) | |
6c88e475 SR |
371 | #define CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, spec_id) \ |
372 | ((kv)->value.leaf.data[0] = \ | |
7fb9addb SR |
373 | cpu_to_be32((CSR1212_DESCRIPTOR_LEAF_TYPE(kv) << \ |
374 | CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT) | \ | |
375 | ((spec_id) & CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK))) | |
6c88e475 SR |
376 | |
377 | static struct csr1212_keyval * | |
982610bd | 378 | csr1212_new_descriptor_leaf(u8 dtype, u32 specifier_id, |
6c88e475 | 379 | const void *data, size_t data_len) |
1da177e4 LT |
380 | { |
381 | struct csr1212_keyval *kv; | |
382 | ||
383 | kv = csr1212_new_leaf(CSR1212_KV_ID_DESCRIPTOR, NULL, | |
384 | data_len + CSR1212_DESCRIPTOR_LEAF_OVERHEAD); | |
385 | if (!kv) | |
386 | return NULL; | |
387 | ||
388 | CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, dtype); | |
389 | CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, specifier_id); | |
390 | ||
a1c6250c | 391 | if (data) |
1da177e4 | 392 | memcpy(CSR1212_DESCRIPTOR_LEAF_DATA(kv), data, data_len); |
1da177e4 LT |
393 | |
394 | return kv; | |
395 | } | |
396 | ||
a1c6250c | 397 | /* Check if string conforms to minimal ASCII as per IEEE 1212 clause 7.4 */ |
1da177e4 LT |
398 | static int csr1212_check_minimal_ascii(const char *s) |
399 | { | |
400 | static const char minimal_ascii_table[] = { | |
a1c6250c SR |
401 | /* 1 2 4 8 16 32 64 128 */ |
402 | 128, /* --, --, --, --, --, --, --, 07, */ | |
403 | 4 + 16 + 32, /* --, --, 0a, --, 0C, 0D, --, --, */ | |
404 | 0, /* --, --, --, --, --, --, --, --, */ | |
405 | 0, /* --, --, --, --, --, --, --, --, */ | |
406 | 255 - 8 - 16, /* 20, 21, 22, --, --, 25, 26, 27, */ | |
407 | 255, /* 28, 29, 2a, 2b, 2c, 2d, 2e, 2f, */ | |
408 | 255, /* 30, 31, 32, 33, 34, 35, 36, 37, */ | |
409 | 255, /* 38, 39, 3a, 3b, 3c, 3d, 3e, 3f, */ | |
410 | 255, /* 40, 41, 42, 43, 44, 45, 46, 47, */ | |
411 | 255, /* 48, 49, 4a, 4b, 4c, 4d, 4e, 4f, */ | |
412 | 255, /* 50, 51, 52, 53, 54, 55, 56, 57, */ | |
413 | 1 + 2 + 4 + 128, /* 58, 59, 5a, --, --, --, --, 5f, */ | |
414 | 255 - 1, /* --, 61, 62, 63, 64, 65, 66, 67, */ | |
415 | 255, /* 68, 69, 6a, 6b, 6c, 6d, 6e, 6f, */ | |
416 | 255, /* 70, 71, 72, 73, 74, 75, 76, 77, */ | |
417 | 1 + 2 + 4, /* 78, 79, 7a, --, --, --, --, --, */ | |
1da177e4 | 418 | }; |
a1c6250c SR |
419 | int i, j; |
420 | ||
1da177e4 | 421 | for (; *s; s++) { |
a1c6250c SR |
422 | i = *s >> 3; /* i = *s / 8; */ |
423 | j = 1 << (*s & 3); /* j = 1 << (*s % 8); */ | |
424 | ||
425 | if (i >= ARRAY_SIZE(minimal_ascii_table) || | |
426 | !(minimal_ascii_table[i] & j)) | |
427 | return -EINVAL; | |
1da177e4 | 428 | } |
1da177e4 LT |
429 | return 0; |
430 | } | |
431 | ||
a1c6250c | 432 | /* IEEE 1212 clause 7.5.4.1 textual descriptors (English, minimal ASCII) */ |
1da177e4 LT |
433 | struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s) |
434 | { | |
a1c6250c SR |
435 | struct csr1212_keyval *kv; |
436 | u32 *text; | |
437 | size_t str_len, quads; | |
438 | ||
439 | if (!s || !*s || csr1212_check_minimal_ascii(s)) | |
440 | return NULL; | |
441 | ||
442 | str_len = strlen(s); | |
443 | quads = bytes_to_quads(str_len); | |
444 | kv = csr1212_new_descriptor_leaf(0, 0, NULL, quads_to_bytes(quads) + | |
445 | CSR1212_TEXTUAL_DESCRIPTOR_LEAF_OVERHEAD); | |
446 | if (!kv) | |
1da177e4 LT |
447 | return NULL; |
448 | ||
a1c6250c SR |
449 | kv->value.leaf.data[1] = 0; /* width, character_set, language */ |
450 | text = CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv); | |
451 | text[quads - 1] = 0; /* padding */ | |
452 | memcpy(text, s, str_len); | |
453 | ||
454 | return kv; | |
1da177e4 LT |
455 | } |
456 | ||
1da177e4 LT |
457 | |
458 | /* Destruction Routines */ | |
459 | ||
460 | void csr1212_detach_keyval_from_directory(struct csr1212_keyval *dir, | |
461 | struct csr1212_keyval *kv) | |
462 | { | |
463 | struct csr1212_dentry *dentry; | |
464 | ||
465 | if (!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY) | |
466 | return; | |
467 | ||
468 | dentry = csr1212_find_keyval(dir, kv); | |
469 | ||
470 | if (!dentry) | |
471 | return; | |
472 | ||
473 | if (dentry->prev) | |
474 | dentry->prev->next = dentry->next; | |
475 | if (dentry->next) | |
476 | dentry->next->prev = dentry->prev; | |
477 | if (dir->value.directory.dentries_head == dentry) | |
478 | dir->value.directory.dentries_head = dentry->next; | |
479 | if (dir->value.directory.dentries_tail == dentry) | |
480 | dir->value.directory.dentries_tail = dentry->prev; | |
481 | ||
482 | CSR1212_FREE(dentry); | |
483 | ||
484 | csr1212_release_keyval(kv); | |
485 | } | |
486 | ||
1da177e4 LT |
487 | /* This function is used to free the memory taken by a keyval. If the given |
488 | * keyval is a directory type, then any keyvals contained in that directory | |
489 | * will be destroyed as well if their respective refcnts are 0. By means of | |
490 | * list manipulation, this routine will descend a directory structure in a | |
491 | * non-recursive manner. */ | |
c1a37f2c | 492 | static void csr1212_destroy_keyval(struct csr1212_keyval *kv) |
1da177e4 LT |
493 | { |
494 | struct csr1212_keyval *k, *a; | |
495 | struct csr1212_dentry dentry; | |
496 | struct csr1212_dentry *head, *tail; | |
497 | ||
498 | dentry.kv = kv; | |
499 | dentry.next = NULL; | |
500 | dentry.prev = NULL; | |
501 | ||
502 | head = &dentry; | |
503 | tail = head; | |
504 | ||
505 | while (head) { | |
506 | k = head->kv; | |
507 | ||
508 | while (k) { | |
509 | k->refcnt--; | |
510 | ||
511 | if (k->refcnt > 0) | |
512 | break; | |
513 | ||
514 | a = k->associate; | |
515 | ||
516 | if (k->key.type == CSR1212_KV_TYPE_DIRECTORY) { | |
c868ae2a | 517 | /* If the current entry is a directory, move all |
1da177e4 LT |
518 | * the entries to the destruction list. */ |
519 | if (k->value.directory.dentries_head) { | |
c868ae2a SR |
520 | tail->next = |
521 | k->value.directory.dentries_head; | |
522 | k->value.directory.dentries_head->prev = | |
523 | tail; | |
1da177e4 LT |
524 | tail = k->value.directory.dentries_tail; |
525 | } | |
526 | } | |
527 | free_keyval(k); | |
528 | k = a; | |
529 | } | |
530 | ||
531 | head = head->next; | |
532 | if (head) { | |
c868ae2a | 533 | if (head->prev && head->prev != &dentry) |
1da177e4 | 534 | CSR1212_FREE(head->prev); |
1da177e4 | 535 | head->prev = NULL; |
c868ae2a | 536 | } else if (tail != &dentry) { |
1da177e4 | 537 | CSR1212_FREE(tail); |
c868ae2a | 538 | } |
1da177e4 LT |
539 | } |
540 | } | |
541 | ||
c1a37f2c SR |
542 | void csr1212_release_keyval(struct csr1212_keyval *kv) |
543 | { | |
544 | if (kv->refcnt > 1) | |
545 | kv->refcnt--; | |
546 | else | |
547 | csr1212_destroy_keyval(kv); | |
548 | } | |
549 | ||
1da177e4 LT |
550 | void csr1212_destroy_csr(struct csr1212_csr *csr) |
551 | { | |
552 | struct csr1212_csr_rom_cache *c, *oc; | |
553 | struct csr1212_cache_region *cr, *ocr; | |
554 | ||
555 | csr1212_release_keyval(csr->root_kv); | |
556 | ||
557 | c = csr->cache_head; | |
558 | while (c) { | |
559 | oc = c; | |
560 | cr = c->filled_head; | |
561 | while (cr) { | |
562 | ocr = cr; | |
563 | cr = cr->next; | |
564 | CSR1212_FREE(ocr); | |
565 | } | |
566 | c = c->next; | |
567 | CSR1212_FREE(oc); | |
568 | } | |
569 | ||
570 | CSR1212_FREE(csr); | |
571 | } | |
572 | ||
573 | ||
1da177e4 LT |
574 | /* CSR Image Creation */ |
575 | ||
576 | static int csr1212_append_new_cache(struct csr1212_csr *csr, size_t romsize) | |
577 | { | |
578 | struct csr1212_csr_rom_cache *cache; | |
982610bd | 579 | u64 csr_addr; |
1da177e4 | 580 | |
64ff7123 SR |
581 | BUG_ON(!csr || !csr->ops || !csr->ops->allocate_addr_range || |
582 | !csr->ops->release_addr || csr->max_rom < 1); | |
1da177e4 LT |
583 | |
584 | /* ROM size must be a multiple of csr->max_rom */ | |
585 | romsize = (romsize + (csr->max_rom - 1)) & ~(csr->max_rom - 1); | |
586 | ||
c868ae2a SR |
587 | csr_addr = csr->ops->allocate_addr_range(romsize, csr->max_rom, |
588 | csr->private); | |
589 | if (csr_addr == CSR1212_INVALID_ADDR_SPACE) | |
7fb9addb | 590 | return -ENOMEM; |
c868ae2a | 591 | |
1da177e4 LT |
592 | if (csr_addr < CSR1212_REGISTER_SPACE_BASE) { |
593 | /* Invalid address returned from allocate_addr_range(). */ | |
594 | csr->ops->release_addr(csr_addr, csr->private); | |
7fb9addb | 595 | return -ENOMEM; |
1da177e4 LT |
596 | } |
597 | ||
c868ae2a SR |
598 | cache = csr1212_rom_cache_malloc(csr_addr - CSR1212_REGISTER_SPACE_BASE, |
599 | romsize); | |
1da177e4 LT |
600 | if (!cache) { |
601 | csr->ops->release_addr(csr_addr, csr->private); | |
7fb9addb | 602 | return -ENOMEM; |
1da177e4 LT |
603 | } |
604 | ||
c868ae2a SR |
605 | cache->ext_rom = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, |
606 | CSR1212_KV_ID_EXTENDED_ROM); | |
1da177e4 LT |
607 | if (!cache->ext_rom) { |
608 | csr->ops->release_addr(csr_addr, csr->private); | |
609 | CSR1212_FREE(cache); | |
7fb9addb | 610 | return -ENOMEM; |
1da177e4 LT |
611 | } |
612 | ||
c868ae2a SR |
613 | if (csr1212_attach_keyval_to_directory(csr->root_kv, cache->ext_rom) != |
614 | CSR1212_SUCCESS) { | |
1da177e4 LT |
615 | csr1212_release_keyval(cache->ext_rom); |
616 | csr->ops->release_addr(csr_addr, csr->private); | |
617 | CSR1212_FREE(cache); | |
7fb9addb | 618 | return -ENOMEM; |
1da177e4 LT |
619 | } |
620 | cache->ext_rom->offset = csr_addr - CSR1212_REGISTER_SPACE_BASE; | |
621 | cache->ext_rom->value.leaf.len = -1; | |
622 | cache->ext_rom->value.leaf.data = cache->data; | |
623 | ||
624 | /* Add cache to tail of cache list */ | |
625 | cache->prev = csr->cache_tail; | |
626 | csr->cache_tail->next = cache; | |
627 | csr->cache_tail = cache; | |
628 | return CSR1212_SUCCESS; | |
629 | } | |
630 | ||
6c88e475 SR |
631 | static void csr1212_remove_cache(struct csr1212_csr *csr, |
632 | struct csr1212_csr_rom_cache *cache) | |
1da177e4 LT |
633 | { |
634 | if (csr->cache_head == cache) | |
635 | csr->cache_head = cache->next; | |
636 | if (csr->cache_tail == cache) | |
637 | csr->cache_tail = cache->prev; | |
638 | ||
639 | if (cache->prev) | |
640 | cache->prev->next = cache->next; | |
641 | if (cache->next) | |
642 | cache->next->prev = cache->prev; | |
643 | ||
644 | if (cache->ext_rom) { | |
c868ae2a SR |
645 | csr1212_detach_keyval_from_directory(csr->root_kv, |
646 | cache->ext_rom); | |
1da177e4 LT |
647 | csr1212_release_keyval(cache->ext_rom); |
648 | } | |
649 | ||
650 | CSR1212_FREE(cache); | |
651 | } | |
652 | ||
653 | static int csr1212_generate_layout_subdir(struct csr1212_keyval *dir, | |
654 | struct csr1212_keyval **layout_tail) | |
655 | { | |
656 | struct csr1212_dentry *dentry; | |
657 | struct csr1212_keyval *dkv; | |
658 | struct csr1212_keyval *last_extkey_spec = NULL; | |
659 | struct csr1212_keyval *last_extkey = NULL; | |
660 | int num_entries = 0; | |
661 | ||
662 | for (dentry = dir->value.directory.dentries_head; dentry; | |
663 | dentry = dentry->next) { | |
664 | for (dkv = dentry->kv; dkv; dkv = dkv->associate) { | |
665 | /* Special Case: Extended Key Specifier_ID */ | |
c868ae2a SR |
666 | if (dkv->key.id == |
667 | CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) { | |
668 | if (last_extkey_spec == NULL) | |
1da177e4 | 669 | last_extkey_spec = dkv; |
c868ae2a SR |
670 | else if (dkv->value.immediate != |
671 | last_extkey_spec->value.immediate) | |
1da177e4 | 672 | last_extkey_spec = dkv; |
c868ae2a | 673 | else |
1da177e4 | 674 | continue; |
1da177e4 LT |
675 | /* Special Case: Extended Key */ |
676 | } else if (dkv->key.id == CSR1212_KV_ID_EXTENDED_KEY) { | |
c868ae2a | 677 | if (last_extkey == NULL) |
1da177e4 | 678 | last_extkey = dkv; |
c868ae2a SR |
679 | else if (dkv->value.immediate != |
680 | last_extkey->value.immediate) | |
1da177e4 | 681 | last_extkey = dkv; |
c868ae2a | 682 | else |
1da177e4 | 683 | continue; |
1da177e4 LT |
684 | } |
685 | ||
686 | num_entries += 1; | |
687 | ||
c868ae2a | 688 | switch (dkv->key.type) { |
1da177e4 LT |
689 | default: |
690 | case CSR1212_KV_TYPE_IMMEDIATE: | |
691 | case CSR1212_KV_TYPE_CSR_OFFSET: | |
692 | break; | |
693 | case CSR1212_KV_TYPE_LEAF: | |
694 | case CSR1212_KV_TYPE_DIRECTORY: | |
695 | /* Remove from list */ | |
696 | if (dkv->prev && (dkv->prev->next == dkv)) | |
697 | dkv->prev->next = dkv->next; | |
698 | if (dkv->next && (dkv->next->prev == dkv)) | |
699 | dkv->next->prev = dkv->prev; | |
700 | //if (dkv == *layout_tail) | |
701 | // *layout_tail = dkv->prev; | |
702 | ||
703 | /* Special case: Extended ROM leafs */ | |
704 | if (dkv->key.id == CSR1212_KV_ID_EXTENDED_ROM) { | |
705 | dkv->value.leaf.len = -1; | |
c868ae2a SR |
706 | /* Don't add Extended ROM leafs in the |
707 | * layout list, they are handled | |
708 | * differently. */ | |
1da177e4 LT |
709 | break; |
710 | } | |
711 | ||
712 | /* Add to tail of list */ | |
713 | dkv->next = NULL; | |
714 | dkv->prev = *layout_tail; | |
715 | (*layout_tail)->next = dkv; | |
716 | *layout_tail = dkv; | |
717 | break; | |
718 | } | |
719 | } | |
720 | } | |
721 | return num_entries; | |
722 | } | |
723 | ||
6c88e475 | 724 | static size_t csr1212_generate_layout_order(struct csr1212_keyval *kv) |
1da177e4 LT |
725 | { |
726 | struct csr1212_keyval *ltail = kv; | |
727 | size_t agg_size = 0; | |
728 | ||
c868ae2a SR |
729 | while (kv) { |
730 | switch (kv->key.type) { | |
1da177e4 LT |
731 | case CSR1212_KV_TYPE_LEAF: |
732 | /* Add 1 quadlet for crc/len field */ | |
733 | agg_size += kv->value.leaf.len + 1; | |
734 | break; | |
735 | ||
736 | case CSR1212_KV_TYPE_DIRECTORY: | |
c868ae2a SR |
737 | kv->value.directory.len = |
738 | csr1212_generate_layout_subdir(kv, <ail); | |
1da177e4 LT |
739 | /* Add 1 quadlet for crc/len field */ |
740 | agg_size += kv->value.directory.len + 1; | |
741 | break; | |
742 | } | |
743 | kv = kv->next; | |
744 | } | |
745 | return quads_to_bytes(agg_size); | |
746 | } | |
747 | ||
6c88e475 SR |
748 | static struct csr1212_keyval * |
749 | csr1212_generate_positions(struct csr1212_csr_rom_cache *cache, | |
750 | struct csr1212_keyval *start_kv, int start_pos) | |
1da177e4 LT |
751 | { |
752 | struct csr1212_keyval *kv = start_kv; | |
753 | struct csr1212_keyval *okv = start_kv; | |
754 | int pos = start_pos; | |
755 | int kv_len = 0, okv_len = 0; | |
756 | ||
757 | cache->layout_head = kv; | |
758 | ||
c868ae2a | 759 | while (kv && pos < cache->size) { |
1da177e4 | 760 | /* Special case: Extended ROM leafs */ |
c868ae2a | 761 | if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) |
1da177e4 | 762 | kv->offset = cache->offset + pos; |
1da177e4 | 763 | |
c868ae2a | 764 | switch (kv->key.type) { |
1da177e4 LT |
765 | case CSR1212_KV_TYPE_LEAF: |
766 | kv_len = kv->value.leaf.len; | |
767 | break; | |
768 | ||
769 | case CSR1212_KV_TYPE_DIRECTORY: | |
770 | kv_len = kv->value.directory.len; | |
771 | break; | |
772 | ||
773 | default: | |
774 | /* Should never get here */ | |
c94ccf9e | 775 | WARN_ON(1); |
1da177e4 LT |
776 | break; |
777 | } | |
778 | ||
779 | pos += quads_to_bytes(kv_len + 1); | |
780 | ||
781 | if (pos <= cache->size) { | |
782 | okv = kv; | |
783 | okv_len = kv_len; | |
784 | kv = kv->next; | |
785 | } | |
786 | } | |
787 | ||
788 | cache->layout_tail = okv; | |
c868ae2a | 789 | cache->len = okv->offset - cache->offset + quads_to_bytes(okv_len + 1); |
1da177e4 LT |
790 | |
791 | return kv; | |
792 | } | |
793 | ||
6c88e475 SR |
794 | #define CSR1212_KV_KEY_SHIFT 24 |
795 | #define CSR1212_KV_KEY_TYPE_SHIFT 6 | |
796 | #define CSR1212_KV_KEY_ID_MASK 0x3f | |
797 | #define CSR1212_KV_KEY_TYPE_MASK 0x3 /* after shift */ | |
798 | ||
799 | static void | |
982610bd | 800 | csr1212_generate_tree_subdir(struct csr1212_keyval *dir, u32 *data_buffer) |
1da177e4 LT |
801 | { |
802 | struct csr1212_dentry *dentry; | |
803 | struct csr1212_keyval *last_extkey_spec = NULL; | |
804 | struct csr1212_keyval *last_extkey = NULL; | |
805 | int index = 0; | |
806 | ||
c868ae2a SR |
807 | for (dentry = dir->value.directory.dentries_head; |
808 | dentry; | |
809 | dentry = dentry->next) { | |
1da177e4 LT |
810 | struct csr1212_keyval *a; |
811 | ||
812 | for (a = dentry->kv; a; a = a->associate) { | |
982610bd | 813 | u32 value = 0; |
1da177e4 LT |
814 | |
815 | /* Special Case: Extended Key Specifier_ID */ | |
c868ae2a SR |
816 | if (a->key.id == |
817 | CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) { | |
818 | if (last_extkey_spec == NULL) | |
1da177e4 | 819 | last_extkey_spec = a; |
c868ae2a SR |
820 | else if (a->value.immediate != |
821 | last_extkey_spec->value.immediate) | |
1da177e4 | 822 | last_extkey_spec = a; |
c868ae2a | 823 | else |
1da177e4 | 824 | continue; |
c868ae2a | 825 | |
1da177e4 LT |
826 | /* Special Case: Extended Key */ |
827 | } else if (a->key.id == CSR1212_KV_ID_EXTENDED_KEY) { | |
c868ae2a | 828 | if (last_extkey == NULL) |
1da177e4 | 829 | last_extkey = a; |
c868ae2a SR |
830 | else if (a->value.immediate != |
831 | last_extkey->value.immediate) | |
1da177e4 | 832 | last_extkey = a; |
c868ae2a | 833 | else |
1da177e4 | 834 | continue; |
1da177e4 LT |
835 | } |
836 | ||
c868ae2a | 837 | switch (a->key.type) { |
1da177e4 LT |
838 | case CSR1212_KV_TYPE_IMMEDIATE: |
839 | value = a->value.immediate; | |
840 | break; | |
841 | case CSR1212_KV_TYPE_CSR_OFFSET: | |
842 | value = a->value.csr_offset; | |
843 | break; | |
844 | case CSR1212_KV_TYPE_LEAF: | |
845 | value = a->offset; | |
846 | value -= dir->offset + quads_to_bytes(1+index); | |
847 | value = bytes_to_quads(value); | |
848 | break; | |
849 | case CSR1212_KV_TYPE_DIRECTORY: | |
850 | value = a->offset; | |
851 | value -= dir->offset + quads_to_bytes(1+index); | |
852 | value = bytes_to_quads(value); | |
853 | break; | |
854 | default: | |
855 | /* Should never get here */ | |
c94ccf9e SR |
856 | WARN_ON(1); |
857 | break; | |
1da177e4 LT |
858 | } |
859 | ||
c868ae2a SR |
860 | value |= (a->key.id & CSR1212_KV_KEY_ID_MASK) << |
861 | CSR1212_KV_KEY_SHIFT; | |
1da177e4 | 862 | value |= (a->key.type & CSR1212_KV_KEY_TYPE_MASK) << |
c868ae2a SR |
863 | (CSR1212_KV_KEY_SHIFT + |
864 | CSR1212_KV_KEY_TYPE_SHIFT); | |
7fb9addb | 865 | data_buffer[index] = cpu_to_be32(value); |
1da177e4 LT |
866 | index++; |
867 | } | |
868 | } | |
869 | } | |
870 | ||
6c88e475 | 871 | struct csr1212_keyval_img { |
982610bd SR |
872 | u16 length; |
873 | u16 crc; | |
6c88e475 SR |
874 | |
875 | /* Must be last */ | |
982610bd | 876 | u32 data[0]; /* older gcc can't handle [] which is standard */ |
6c88e475 SR |
877 | }; |
878 | ||
879 | static void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache) | |
1da177e4 LT |
880 | { |
881 | struct csr1212_keyval *kv, *nkv; | |
882 | struct csr1212_keyval_img *kvi; | |
883 | ||
c868ae2a SR |
884 | for (kv = cache->layout_head; |
885 | kv != cache->layout_tail->next; | |
886 | kv = nkv) { | |
887 | kvi = (struct csr1212_keyval_img *)(cache->data + | |
888 | bytes_to_quads(kv->offset - cache->offset)); | |
889 | switch (kv->key.type) { | |
1da177e4 LT |
890 | default: |
891 | case CSR1212_KV_TYPE_IMMEDIATE: | |
892 | case CSR1212_KV_TYPE_CSR_OFFSET: | |
893 | /* Should never get here */ | |
c94ccf9e SR |
894 | WARN_ON(1); |
895 | break; | |
1da177e4 LT |
896 | |
897 | case CSR1212_KV_TYPE_LEAF: | |
898 | /* Don't copy over Extended ROM areas, they are | |
899 | * already filled out! */ | |
900 | if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) | |
901 | memcpy(kvi->data, kv->value.leaf.data, | |
902 | quads_to_bytes(kv->value.leaf.len)); | |
903 | ||
7fb9addb | 904 | kvi->length = cpu_to_be16(kv->value.leaf.len); |
1da177e4 LT |
905 | kvi->crc = csr1212_crc16(kvi->data, kv->value.leaf.len); |
906 | break; | |
907 | ||
908 | case CSR1212_KV_TYPE_DIRECTORY: | |
909 | csr1212_generate_tree_subdir(kv, kvi->data); | |
910 | ||
7fb9addb | 911 | kvi->length = cpu_to_be16(kv->value.directory.len); |
c868ae2a SR |
912 | kvi->crc = csr1212_crc16(kvi->data, |
913 | kv->value.directory.len); | |
1da177e4 LT |
914 | break; |
915 | } | |
916 | ||
917 | nkv = kv->next; | |
918 | if (kv->prev) | |
919 | kv->prev->next = NULL; | |
920 | if (kv->next) | |
921 | kv->next->prev = NULL; | |
922 | kv->prev = NULL; | |
923 | kv->next = NULL; | |
924 | } | |
925 | } | |
926 | ||
fd2f3bdd SR |
927 | /* This size is arbitrarily chosen. |
928 | * The struct overhead is subtracted for more economic allocations. */ | |
929 | #define CSR1212_EXTENDED_ROM_SIZE (2048 - sizeof(struct csr1212_csr_rom_cache)) | |
6c88e475 | 930 | |
1da177e4 LT |
931 | int csr1212_generate_csr_image(struct csr1212_csr *csr) |
932 | { | |
933 | struct csr1212_bus_info_block_img *bi; | |
934 | struct csr1212_csr_rom_cache *cache; | |
935 | struct csr1212_keyval *kv; | |
936 | size_t agg_size; | |
937 | int ret; | |
938 | int init_offset; | |
939 | ||
64ff7123 | 940 | BUG_ON(!csr); |
1da177e4 LT |
941 | |
942 | cache = csr->cache_head; | |
943 | ||
944 | bi = (struct csr1212_bus_info_block_img*)cache->data; | |
945 | ||
946 | bi->length = bytes_to_quads(csr->bus_info_len) - 1; | |
947 | bi->crc_length = bi->length; | |
948 | bi->crc = csr1212_crc16(bi->data, bi->crc_length); | |
949 | ||
950 | csr->root_kv->next = NULL; | |
951 | csr->root_kv->prev = NULL; | |
952 | ||
953 | agg_size = csr1212_generate_layout_order(csr->root_kv); | |
954 | ||
955 | init_offset = csr->bus_info_len; | |
956 | ||
c868ae2a SR |
957 | for (kv = csr->root_kv, cache = csr->cache_head; |
958 | kv; | |
959 | cache = cache->next) { | |
1da177e4 LT |
960 | if (!cache) { |
961 | /* Estimate approximate number of additional cache | |
962 | * regions needed (it assumes that the cache holding | |
963 | * the first 1K Config ROM space always exists). */ | |
964 | int est_c = agg_size / (CSR1212_EXTENDED_ROM_SIZE - | |
982610bd | 965 | (2 * sizeof(u32))) + 1; |
1da177e4 LT |
966 | |
967 | /* Add additional cache regions, extras will be | |
968 | * removed later */ | |
969 | for (; est_c; est_c--) { | |
c868ae2a SR |
970 | ret = csr1212_append_new_cache(csr, |
971 | CSR1212_EXTENDED_ROM_SIZE); | |
1da177e4 LT |
972 | if (ret != CSR1212_SUCCESS) |
973 | return ret; | |
974 | } | |
975 | /* Need to re-layout for additional cache regions */ | |
976 | agg_size = csr1212_generate_layout_order(csr->root_kv); | |
977 | kv = csr->root_kv; | |
978 | cache = csr->cache_head; | |
979 | init_offset = csr->bus_info_len; | |
980 | } | |
981 | kv = csr1212_generate_positions(cache, kv, init_offset); | |
982 | agg_size -= cache->len; | |
982610bd | 983 | init_offset = sizeof(u32); |
1da177e4 LT |
984 | } |
985 | ||
986 | /* Remove unused, excess cache regions */ | |
987 | while (cache) { | |
988 | struct csr1212_csr_rom_cache *oc = cache; | |
989 | ||
990 | cache = cache->next; | |
991 | csr1212_remove_cache(csr, oc); | |
992 | } | |
993 | ||
994 | /* Go through the list backward so that when done, the correct CRC | |
995 | * will be calculated for the Extended ROM areas. */ | |
c868ae2a | 996 | for (cache = csr->cache_tail; cache; cache = cache->prev) { |
1da177e4 LT |
997 | /* Only Extended ROM caches should have this set. */ |
998 | if (cache->ext_rom) { | |
999 | int leaf_size; | |
1000 | ||
1001 | /* Make sure the Extended ROM leaf is a multiple of | |
1002 | * max_rom in size. */ | |
64ff7123 | 1003 | BUG_ON(csr->max_rom < 1); |
1da177e4 LT |
1004 | leaf_size = (cache->len + (csr->max_rom - 1)) & |
1005 | ~(csr->max_rom - 1); | |
1006 | ||
1007 | /* Zero out the unused ROM region */ | |
1008 | memset(cache->data + bytes_to_quads(cache->len), 0x00, | |
1009 | leaf_size - cache->len); | |
1010 | ||
1011 | /* Subtract leaf header */ | |
982610bd | 1012 | leaf_size -= sizeof(u32); |
1da177e4 LT |
1013 | |
1014 | /* Update the Extended ROM leaf length */ | |
1015 | cache->ext_rom->value.leaf.len = | |
1016 | bytes_to_quads(leaf_size); | |
1017 | } else { | |
1018 | /* Zero out the unused ROM region */ | |
1019 | memset(cache->data + bytes_to_quads(cache->len), 0x00, | |
1020 | cache->size - cache->len); | |
1021 | } | |
1022 | ||
1023 | /* Copy the data into the cache buffer */ | |
1024 | csr1212_fill_cache(cache); | |
1025 | ||
1026 | if (cache != csr->cache_head) { | |
1027 | /* Set the length and CRC of the extended ROM. */ | |
1028 | struct csr1212_keyval_img *kvi = | |
1029 | (struct csr1212_keyval_img*)cache->data; | |
982610bd | 1030 | u16 len = bytes_to_quads(cache->len) - 1; |
1da177e4 | 1031 | |
7fb9addb SR |
1032 | kvi->length = cpu_to_be16(len); |
1033 | kvi->crc = csr1212_crc16(kvi->data, len); | |
1da177e4 LT |
1034 | } |
1035 | } | |
1036 | ||
1037 | return CSR1212_SUCCESS; | |
1038 | } | |
1039 | ||
982610bd | 1040 | int csr1212_read(struct csr1212_csr *csr, u32 offset, void *buffer, u32 len) |
1da177e4 LT |
1041 | { |
1042 | struct csr1212_csr_rom_cache *cache; | |
1043 | ||
c868ae2a | 1044 | for (cache = csr->cache_head; cache; cache = cache->next) |
1da177e4 LT |
1045 | if (offset >= cache->offset && |
1046 | (offset + len) <= (cache->offset + cache->size)) { | |
c868ae2a SR |
1047 | memcpy(buffer, &cache->data[ |
1048 | bytes_to_quads(offset - cache->offset)], | |
1da177e4 LT |
1049 | len); |
1050 | return CSR1212_SUCCESS; | |
1051 | } | |
c868ae2a | 1052 | |
7fb9addb | 1053 | return -ENOENT; |
1da177e4 LT |
1054 | } |
1055 | ||
1056 | ||
1da177e4 LT |
1057 | /* Parse a chunk of data as a Config ROM */ |
1058 | ||
1059 | static int csr1212_parse_bus_info_block(struct csr1212_csr *csr) | |
1060 | { | |
1061 | struct csr1212_bus_info_block_img *bi; | |
1062 | struct csr1212_cache_region *cr; | |
1063 | int i; | |
1064 | int ret; | |
1065 | ||
1066 | /* IEEE 1212 says that the entire bus info block should be readable in | |
1067 | * a single transaction regardless of the max_rom value. | |
1068 | * Unfortunately, many IEEE 1394 devices do not abide by that, so the | |
1069 | * bus info block will be read 1 quadlet at a time. The rest of the | |
1070 | * ConfigROM will be read according to the max_rom field. */ | |
982610bd | 1071 | for (i = 0; i < csr->bus_info_len; i += sizeof(u32)) { |
1da177e4 | 1072 | ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i, |
c868ae2a SR |
1073 | sizeof(u32), &csr->cache_head->data[bytes_to_quads(i)], |
1074 | csr->private); | |
1da177e4 LT |
1075 | if (ret != CSR1212_SUCCESS) |
1076 | return ret; | |
b2051f88 SR |
1077 | |
1078 | /* check ROM header's info_length */ | |
1079 | if (i == 0 && | |
7fb9addb | 1080 | be32_to_cpu(csr->cache_head->data[0]) >> 24 != |
b2051f88 | 1081 | bytes_to_quads(csr->bus_info_len) - 1) |
7fb9addb | 1082 | return -EINVAL; |
1da177e4 LT |
1083 | } |
1084 | ||
1085 | bi = (struct csr1212_bus_info_block_img*)csr->cache_head->data; | |
1086 | csr->crc_len = quads_to_bytes(bi->crc_length); | |
1087 | ||
c868ae2a SR |
1088 | /* IEEE 1212 recommends that crc_len be equal to bus_info_len, but that |
1089 | * is not always the case, so read the rest of the crc area 1 quadlet at | |
1090 | * a time. */ | |
982610bd | 1091 | for (i = csr->bus_info_len; i <= csr->crc_len; i += sizeof(u32)) { |
1da177e4 | 1092 | ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i, |
c868ae2a SR |
1093 | sizeof(u32), &csr->cache_head->data[bytes_to_quads(i)], |
1094 | csr->private); | |
1da177e4 LT |
1095 | if (ret != CSR1212_SUCCESS) |
1096 | return ret; | |
1097 | } | |
1098 | ||
1da177e4 LT |
1099 | #if 0 |
1100 | /* Apparently there are too many differnt wrong implementations of the | |
1101 | * CRC algorithm that verifying them is moot. */ | |
1102 | if ((csr1212_crc16(bi->data, bi->crc_length) != bi->crc) && | |
1103 | (csr1212_msft_crc16(bi->data, bi->crc_length) != bi->crc)) | |
7fb9addb | 1104 | return -EINVAL; |
1da177e4 LT |
1105 | #endif |
1106 | ||
8551158a | 1107 | cr = CSR1212_MALLOC(sizeof(*cr)); |
1da177e4 | 1108 | if (!cr) |
7fb9addb | 1109 | return -ENOMEM; |
1da177e4 LT |
1110 | |
1111 | cr->next = NULL; | |
1112 | cr->prev = NULL; | |
1113 | cr->offset_start = 0; | |
1114 | cr->offset_end = csr->crc_len + 4; | |
1115 | ||
1116 | csr->cache_head->filled_head = cr; | |
1117 | csr->cache_head->filled_tail = cr; | |
1118 | ||
1119 | return CSR1212_SUCCESS; | |
1120 | } | |
1121 | ||
7fb9addb | 1122 | #define CSR1212_KV_KEY(q) (be32_to_cpu(q) >> CSR1212_KV_KEY_SHIFT) |
6c88e475 SR |
1123 | #define CSR1212_KV_KEY_TYPE(q) (CSR1212_KV_KEY(q) >> CSR1212_KV_KEY_TYPE_SHIFT) |
1124 | #define CSR1212_KV_KEY_ID(q) (CSR1212_KV_KEY(q) & CSR1212_KV_KEY_ID_MASK) | |
1125 | #define CSR1212_KV_VAL_MASK 0xffffff | |
7fb9addb | 1126 | #define CSR1212_KV_VAL(q) (be32_to_cpu(q) & CSR1212_KV_VAL_MASK) |
6c88e475 | 1127 | |
c868ae2a SR |
1128 | static int |
1129 | csr1212_parse_dir_entry(struct csr1212_keyval *dir, u32 ki, u32 kv_pos) | |
1da177e4 LT |
1130 | { |
1131 | int ret = CSR1212_SUCCESS; | |
1132 | struct csr1212_keyval *k = NULL; | |
982610bd | 1133 | u32 offset; |
1da177e4 | 1134 | |
c868ae2a | 1135 | switch (CSR1212_KV_KEY_TYPE(ki)) { |
1da177e4 LT |
1136 | case CSR1212_KV_TYPE_IMMEDIATE: |
1137 | k = csr1212_new_immediate(CSR1212_KV_KEY_ID(ki), | |
1138 | CSR1212_KV_VAL(ki)); | |
1139 | if (!k) { | |
7fb9addb | 1140 | ret = -ENOMEM; |
511f7b32 | 1141 | goto out; |
1da177e4 LT |
1142 | } |
1143 | ||
1144 | k->refcnt = 0; /* Don't keep local reference when parsing. */ | |
1145 | break; | |
1146 | ||
1147 | case CSR1212_KV_TYPE_CSR_OFFSET: | |
1148 | k = csr1212_new_csr_offset(CSR1212_KV_KEY_ID(ki), | |
1149 | CSR1212_KV_VAL(ki)); | |
1150 | if (!k) { | |
7fb9addb | 1151 | ret = -ENOMEM; |
511f7b32 | 1152 | goto out; |
1da177e4 LT |
1153 | } |
1154 | k->refcnt = 0; /* Don't keep local reference when parsing. */ | |
1155 | break; | |
1156 | ||
1157 | default: | |
1158 | /* Compute the offset from 0xffff f000 0000. */ | |
1159 | offset = quads_to_bytes(CSR1212_KV_VAL(ki)) + kv_pos; | |
1160 | if (offset == kv_pos) { | |
1161 | /* Uh-oh. Can't have a relative offset of 0 for Leaves | |
1162 | * or Directories. The Config ROM image is most likely | |
1163 | * messed up, so we'll just abort here. */ | |
7fb9addb | 1164 | ret = -EIO; |
511f7b32 | 1165 | goto out; |
1da177e4 LT |
1166 | } |
1167 | ||
1168 | k = csr1212_find_keyval_offset(dir, offset); | |
1169 | ||
1170 | if (k) | |
1171 | break; /* Found it. */ | |
1172 | ||
c868ae2a | 1173 | if (CSR1212_KV_KEY_TYPE(ki) == CSR1212_KV_TYPE_DIRECTORY) |
1da177e4 | 1174 | k = csr1212_new_directory(CSR1212_KV_KEY_ID(ki)); |
c868ae2a | 1175 | else |
1da177e4 | 1176 | k = csr1212_new_leaf(CSR1212_KV_KEY_ID(ki), NULL, 0); |
c868ae2a | 1177 | |
1da177e4 | 1178 | if (!k) { |
7fb9addb | 1179 | ret = -ENOMEM; |
511f7b32 | 1180 | goto out; |
1da177e4 LT |
1181 | } |
1182 | k->refcnt = 0; /* Don't keep local reference when parsing. */ | |
1183 | k->valid = 0; /* Contents not read yet so it's not valid. */ | |
1184 | k->offset = offset; | |
1185 | ||
1186 | k->prev = dir; | |
1187 | k->next = dir->next; | |
1188 | dir->next->prev = k; | |
1189 | dir->next = k; | |
1190 | } | |
1191 | ret = csr1212_attach_keyval_to_directory(dir, k); | |
511f7b32 | 1192 | out: |
6c88e475 SR |
1193 | if (ret != CSR1212_SUCCESS && k != NULL) |
1194 | free_keyval(k); | |
1da177e4 LT |
1195 | return ret; |
1196 | } | |
1197 | ||
1da177e4 LT |
1198 | int csr1212_parse_keyval(struct csr1212_keyval *kv, |
1199 | struct csr1212_csr_rom_cache *cache) | |
1200 | { | |
1201 | struct csr1212_keyval_img *kvi; | |
1202 | int i; | |
1203 | int ret = CSR1212_SUCCESS; | |
1204 | int kvi_len; | |
1205 | ||
c868ae2a SR |
1206 | kvi = (struct csr1212_keyval_img*) |
1207 | &cache->data[bytes_to_quads(kv->offset - cache->offset)]; | |
7fb9addb | 1208 | kvi_len = be16_to_cpu(kvi->length); |
1da177e4 LT |
1209 | |
1210 | #if 0 | |
1211 | /* Apparently there are too many differnt wrong implementations of the | |
1212 | * CRC algorithm that verifying them is moot. */ | |
1213 | if ((csr1212_crc16(kvi->data, kvi_len) != kvi->crc) && | |
1214 | (csr1212_msft_crc16(kvi->data, kvi_len) != kvi->crc)) { | |
7fb9addb | 1215 | ret = -EINVAL; |
511f7b32 | 1216 | goto out; |
1da177e4 LT |
1217 | } |
1218 | #endif | |
1219 | ||
c868ae2a | 1220 | switch (kv->key.type) { |
1da177e4 LT |
1221 | case CSR1212_KV_TYPE_DIRECTORY: |
1222 | for (i = 0; i < kvi_len; i++) { | |
982610bd | 1223 | u32 ki = kvi->data[i]; |
1da177e4 LT |
1224 | |
1225 | /* Some devices put null entries in their unit | |
1226 | * directories. If we come across such an entry, | |
1227 | * then skip it. */ | |
1228 | if (ki == 0x0) | |
1229 | continue; | |
1230 | ret = csr1212_parse_dir_entry(kv, ki, | |
c868ae2a | 1231 | kv->offset + quads_to_bytes(i + 1)); |
1da177e4 LT |
1232 | } |
1233 | kv->value.directory.len = kvi_len; | |
1234 | break; | |
1235 | ||
1236 | case CSR1212_KV_TYPE_LEAF: | |
1237 | if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) { | |
c868ae2a SR |
1238 | size_t size = quads_to_bytes(kvi_len); |
1239 | ||
1240 | kv->value.leaf.data = CSR1212_MALLOC(size); | |
8551158a | 1241 | if (!kv->value.leaf.data) { |
7fb9addb | 1242 | ret = -ENOMEM; |
511f7b32 | 1243 | goto out; |
1da177e4 LT |
1244 | } |
1245 | ||
1246 | kv->value.leaf.len = kvi_len; | |
c868ae2a | 1247 | memcpy(kv->value.leaf.data, kvi->data, size); |
1da177e4 LT |
1248 | } |
1249 | break; | |
1250 | } | |
1251 | ||
1252 | kv->valid = 1; | |
511f7b32 | 1253 | out: |
1da177e4 LT |
1254 | return ret; |
1255 | } | |
1256 | ||
c1a37f2c SR |
1257 | static int |
1258 | csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv) | |
1da177e4 LT |
1259 | { |
1260 | struct csr1212_cache_region *cr, *ncr, *newcr = NULL; | |
1261 | struct csr1212_keyval_img *kvi = NULL; | |
1262 | struct csr1212_csr_rom_cache *cache; | |
1263 | int cache_index; | |
982610bd SR |
1264 | u64 addr; |
1265 | u32 *cache_ptr; | |
1266 | u16 kv_len = 0; | |
1da177e4 | 1267 | |
64ff7123 | 1268 | BUG_ON(!csr || !kv || csr->max_rom < 1); |
1da177e4 LT |
1269 | |
1270 | /* First find which cache the data should be in (or go in if not read | |
1271 | * yet). */ | |
c868ae2a | 1272 | for (cache = csr->cache_head; cache; cache = cache->next) |
1da177e4 LT |
1273 | if (kv->offset >= cache->offset && |
1274 | kv->offset < (cache->offset + cache->size)) | |
1275 | break; | |
1da177e4 LT |
1276 | |
1277 | if (!cache) { | |
982610bd | 1278 | u32 q, cache_size; |
1da177e4 LT |
1279 | |
1280 | /* Only create a new cache for Extended ROM leaves. */ | |
1281 | if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) | |
7fb9addb | 1282 | return -EINVAL; |
1da177e4 LT |
1283 | |
1284 | if (csr->ops->bus_read(csr, | |
1285 | CSR1212_REGISTER_SPACE_BASE + kv->offset, | |
c868ae2a | 1286 | sizeof(u32), &q, csr->private)) |
7fb9addb | 1287 | return -EIO; |
1da177e4 | 1288 | |
7fb9addb | 1289 | kv->value.leaf.len = be32_to_cpu(q) >> 16; |
1da177e4 LT |
1290 | |
1291 | cache_size = (quads_to_bytes(kv->value.leaf.len + 1) + | |
1292 | (csr->max_rom - 1)) & ~(csr->max_rom - 1); | |
1293 | ||
1294 | cache = csr1212_rom_cache_malloc(kv->offset, cache_size); | |
1295 | if (!cache) | |
7fb9addb | 1296 | return -ENOMEM; |
1da177e4 LT |
1297 | |
1298 | kv->value.leaf.data = &cache->data[1]; | |
1299 | csr->cache_tail->next = cache; | |
1300 | cache->prev = csr->cache_tail; | |
1301 | cache->next = NULL; | |
1302 | csr->cache_tail = cache; | |
1303 | cache->filled_head = | |
8551158a | 1304 | CSR1212_MALLOC(sizeof(*cache->filled_head)); |
c868ae2a | 1305 | if (!cache->filled_head) |
7fb9addb | 1306 | return -ENOMEM; |
1da177e4 LT |
1307 | |
1308 | cache->filled_head->offset_start = 0; | |
982610bd | 1309 | cache->filled_head->offset_end = sizeof(u32); |
1da177e4 LT |
1310 | cache->filled_tail = cache->filled_head; |
1311 | cache->filled_head->next = NULL; | |
1312 | cache->filled_head->prev = NULL; | |
1313 | cache->data[0] = q; | |
1314 | ||
1315 | /* Don't read the entire extended ROM now. Pieces of it will | |
1316 | * be read when entries inside it are read. */ | |
1317 | return csr1212_parse_keyval(kv, cache); | |
1318 | } | |
1319 | ||
1320 | cache_index = kv->offset - cache->offset; | |
1321 | ||
1322 | /* Now seach read portions of the cache to see if it is there. */ | |
1323 | for (cr = cache->filled_head; cr; cr = cr->next) { | |
1324 | if (cache_index < cr->offset_start) { | |
8551158a | 1325 | newcr = CSR1212_MALLOC(sizeof(*newcr)); |
1da177e4 | 1326 | if (!newcr) |
7fb9addb | 1327 | return -ENOMEM; |
1da177e4 LT |
1328 | |
1329 | newcr->offset_start = cache_index & ~(csr->max_rom - 1); | |
1330 | newcr->offset_end = newcr->offset_start; | |
1331 | newcr->next = cr; | |
1332 | newcr->prev = cr->prev; | |
1333 | cr->prev = newcr; | |
1334 | cr = newcr; | |
1335 | break; | |
1336 | } else if ((cache_index >= cr->offset_start) && | |
1337 | (cache_index < cr->offset_end)) { | |
1338 | kvi = (struct csr1212_keyval_img*) | |
1339 | (&cache->data[bytes_to_quads(cache_index)]); | |
7fb9addb | 1340 | kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1); |
1da177e4 | 1341 | break; |
c868ae2a | 1342 | } else if (cache_index == cr->offset_end) { |
1da177e4 | 1343 | break; |
c868ae2a | 1344 | } |
1da177e4 LT |
1345 | } |
1346 | ||
1347 | if (!cr) { | |
1348 | cr = cache->filled_tail; | |
8551158a | 1349 | newcr = CSR1212_MALLOC(sizeof(*newcr)); |
1da177e4 | 1350 | if (!newcr) |
7fb9addb | 1351 | return -ENOMEM; |
1da177e4 LT |
1352 | |
1353 | newcr->offset_start = cache_index & ~(csr->max_rom - 1); | |
1354 | newcr->offset_end = newcr->offset_start; | |
1355 | newcr->prev = cr; | |
1356 | newcr->next = cr->next; | |
1357 | cr->next = newcr; | |
1358 | cr = newcr; | |
1359 | cache->filled_tail = newcr; | |
1360 | } | |
1361 | ||
1362 | while(!kvi || cr->offset_end < cache_index + kv_len) { | |
1363 | cache_ptr = &cache->data[bytes_to_quads(cr->offset_end & | |
1364 | ~(csr->max_rom - 1))]; | |
1365 | ||
1366 | addr = (CSR1212_CSR_ARCH_REG_SPACE_BASE + cache->offset + | |
1367 | cr->offset_end) & ~(csr->max_rom - 1); | |
1368 | ||
1369 | if (csr->ops->bus_read(csr, addr, csr->max_rom, cache_ptr, | |
1370 | csr->private)) { | |
1371 | if (csr->max_rom == 4) | |
1372 | /* We've got problems! */ | |
7fb9addb | 1373 | return -EIO; |
1da177e4 LT |
1374 | |
1375 | /* Apperently the max_rom value was a lie, set it to | |
1376 | * do quadlet reads and try again. */ | |
1377 | csr->max_rom = 4; | |
1378 | continue; | |
1379 | } | |
1380 | ||
1381 | cr->offset_end += csr->max_rom - (cr->offset_end & | |
1382 | (csr->max_rom - 1)); | |
1383 | ||
1384 | if (!kvi && (cr->offset_end > cache_index)) { | |
1385 | kvi = (struct csr1212_keyval_img*) | |
1386 | (&cache->data[bytes_to_quads(cache_index)]); | |
7fb9addb | 1387 | kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1); |
1da177e4 LT |
1388 | } |
1389 | ||
1390 | if ((kv_len + (kv->offset - cache->offset)) > cache->size) { | |
1391 | /* The Leaf or Directory claims its length extends | |
1392 | * beyond the ConfigROM image region and thus beyond the | |
1393 | * end of our cache region. Therefore, we abort now | |
1394 | * rather than seg faulting later. */ | |
7fb9addb | 1395 | return -EIO; |
1da177e4 LT |
1396 | } |
1397 | ||
1398 | ncr = cr->next; | |
1399 | ||
1400 | if (ncr && (cr->offset_end >= ncr->offset_start)) { | |
1401 | /* consolidate region entries */ | |
1402 | ncr->offset_start = cr->offset_start; | |
1403 | ||
1404 | if (cr->prev) | |
1405 | cr->prev->next = cr->next; | |
1406 | ncr->prev = cr->prev; | |
1407 | if (cache->filled_head == cr) | |
1408 | cache->filled_head = ncr; | |
1409 | CSR1212_FREE(cr); | |
1410 | cr = ncr; | |
1411 | } | |
1412 | } | |
1413 | ||
1414 | return csr1212_parse_keyval(kv, cache); | |
1415 | } | |
1416 | ||
c1a37f2c SR |
1417 | struct csr1212_keyval * |
1418 | csr1212_get_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv) | |
1419 | { | |
1420 | if (!kv) | |
1421 | return NULL; | |
1422 | if (!kv->valid) | |
1423 | if (csr1212_read_keyval(csr, kv) != CSR1212_SUCCESS) | |
1424 | return NULL; | |
1425 | return kv; | |
1426 | } | |
1427 | ||
1da177e4 LT |
1428 | int csr1212_parse_csr(struct csr1212_csr *csr) |
1429 | { | |
1430 | static const int mr_map[] = { 4, 64, 1024, 0 }; | |
1431 | struct csr1212_dentry *dentry; | |
1432 | int ret; | |
1433 | ||
64ff7123 | 1434 | BUG_ON(!csr || !csr->ops || !csr->ops->bus_read); |
1da177e4 LT |
1435 | |
1436 | ret = csr1212_parse_bus_info_block(csr); | |
1437 | if (ret != CSR1212_SUCCESS) | |
1438 | return ret; | |
1439 | ||
c868ae2a | 1440 | if (!csr->ops->get_max_rom) { |
1da177e4 | 1441 | csr->max_rom = mr_map[0]; /* default value */ |
c868ae2a | 1442 | } else { |
1934b8b6 BC |
1443 | int i = csr->ops->get_max_rom(csr->bus_info_data, |
1444 | csr->private); | |
1445 | if (i & ~0x3) | |
7fb9addb | 1446 | return -EINVAL; |
1934b8b6 BC |
1447 | csr->max_rom = mr_map[i]; |
1448 | } | |
1da177e4 LT |
1449 | |
1450 | csr->cache_head->layout_head = csr->root_kv; | |
1451 | csr->cache_head->layout_tail = csr->root_kv; | |
1452 | ||
1453 | csr->root_kv->offset = (CSR1212_CONFIG_ROM_SPACE_BASE & 0xffff) + | |
1454 | csr->bus_info_len; | |
1455 | ||
1456 | csr->root_kv->valid = 0; | |
1457 | csr->root_kv->next = csr->root_kv; | |
1458 | csr->root_kv->prev = csr->root_kv; | |
c1a37f2c | 1459 | ret = csr1212_read_keyval(csr, csr->root_kv); |
5303a986 JM |
1460 | if (ret != CSR1212_SUCCESS) |
1461 | return ret; | |
1da177e4 LT |
1462 | |
1463 | /* Scan through the Root directory finding all extended ROM regions | |
1464 | * and make cache regions for them */ | |
1465 | for (dentry = csr->root_kv->value.directory.dentries_head; | |
1466 | dentry; dentry = dentry->next) { | |
a96074e7 JM |
1467 | if (dentry->kv->key.id == CSR1212_KV_ID_EXTENDED_ROM && |
1468 | !dentry->kv->valid) { | |
c1a37f2c | 1469 | ret = csr1212_read_keyval(csr, dentry->kv); |
1da177e4 LT |
1470 | if (ret != CSR1212_SUCCESS) |
1471 | return ret; | |
1472 | } | |
1473 | } | |
1474 | ||
1475 | return CSR1212_SUCCESS; | |
1476 | } |