blob: d08166bda1c54b2938e720ce19dcfdc5a21fe222 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
Stefan Richter7fb9add2007-03-11 22:49:05 +010036#include <linux/errno.h>
Stefan Richterd2652502007-03-14 00:29:20 +010037#include <linux/kernel.h>
Stefan Richter7fb9add2007-03-11 22:49:05 +010038#include <linux/string.h>
Stefan Richter64ff7122007-03-11 22:50:13 +010039#include <asm/bug.h>
Stefan Richter7fb9add2007-03-11 22:49:05 +010040#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "csr1212.h"
43
44
45/* Permitted key type for each key id */
46#define __I (1 << CSR1212_KV_TYPE_IMMEDIATE)
47#define __C (1 << CSR1212_KV_TYPE_CSR_OFFSET)
48#define __D (1 << CSR1212_KV_TYPE_DIRECTORY)
49#define __L (1 << CSR1212_KV_TYPE_LEAF)
Stefan Richter982610b2007-03-11 22:49:34 +010050static const u8 csr1212_key_id_type_map[0x30] = {
Andrea Guzzo0749aaa2006-12-08 00:53:24 +010051 __C, /* used by Apple iSight */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 __D | __L, /* Descriptor */
53 __I | __D | __L, /* Bus_Dependent_Info */
54 __I | __D | __L, /* Vendor */
55 __I, /* Hardware_Version */
56 0, 0, /* Reserved */
Andrea Guzzo0749aaa2006-12-08 00:53:24 +010057 __D | __L | __I, /* Module */
58 __I, 0, 0, 0, /* used by Apple iSight, Reserved */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 __I, /* Node_Capabilities */
60 __L, /* EUI_64 */
61 0, 0, 0, /* Reserved */
62 __D, /* Unit */
63 __I, /* Specifier_ID */
64 __I, /* Version */
65 __I | __C | __D | __L, /* Dependent_Info */
66 __L, /* Unit_Location */
67 0, /* Reserved */
68 __I, /* Model */
69 __D, /* Instance */
70 __L, /* Keyword */
71 __D, /* Feature */
72 __L, /* Extended_ROM */
73 __I, /* Extended_Key_Specifier_ID */
74 __I, /* Extended_Key */
75 __I | __C | __D | __L, /* Extended_Data */
76 __L, /* Modifiable_Descriptor */
77 __I, /* Directory_ID */
78 __I, /* Revision */
79};
80#undef __I
81#undef __C
82#undef __D
83#undef __L
84
85
Stefan Richter982610b2007-03-11 22:49:34 +010086#define quads_to_bytes(_q) ((_q) * sizeof(u32))
87#define bytes_to_quads(_b) (((_b) + sizeof(u32) - 1) / sizeof(u32))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Stefan Richter6c88e472007-03-11 22:47:34 +010089static void free_keyval(struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 if ((kv->key.type == CSR1212_KV_TYPE_LEAF) &&
92 (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM))
93 CSR1212_FREE(kv->value.leaf.data);
94
95 CSR1212_FREE(kv);
96}
97
Stefan Richter982610b2007-03-11 22:49:34 +010098static u16 csr1212_crc16(const u32 *buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 int shift;
Stefan Richter982610b2007-03-11 22:49:34 +0100101 u32 data;
102 u16 sum, crc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 for (; length; length--) {
Stefan Richter7fb9add2007-03-11 22:49:05 +0100105 data = be32_to_cpu(*buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 buffer++;
107 for (shift = 28; shift >= 0; shift -= 4 ) {
108 sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
109 crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum);
110 }
111 crc &= 0xffff;
112 }
113
Stefan Richter7fb9add2007-03-11 22:49:05 +0100114 return cpu_to_be16(crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Stefan Richterd2652502007-03-14 00:29:20 +0100117/* Microsoft computes the CRC with the bytes in reverse order. */
Stefan Richter982610b2007-03-11 22:49:34 +0100118static u16 csr1212_msft_crc16(const u32 *buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 int shift;
Stefan Richter982610b2007-03-11 22:49:34 +0100121 u32 data;
122 u16 sum, crc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 for (; length; length--) {
Stefan Richter7fb9add2007-03-11 22:49:05 +0100125 data = le32_to_cpu(*buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 buffer++;
127 for (shift = 28; shift >= 0; shift -= 4 ) {
128 sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
129 crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum);
130 }
131 crc &= 0xffff;
132 }
133
Stefan Richter7fb9add2007-03-11 22:49:05 +0100134 return cpu_to_be16(crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Stefan Richter6c88e472007-03-11 22:47:34 +0100137static struct csr1212_dentry *
138csr1212_find_keyval(struct csr1212_keyval *dir, struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 struct csr1212_dentry *pos;
141
142 for (pos = dir->value.directory.dentries_head;
Stefan Richterc868ae22007-03-14 00:26:38 +0100143 pos != NULL; pos = pos->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (pos->kv == kv)
145 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return NULL;
147}
148
Stefan Richter6c88e472007-03-11 22:47:34 +0100149static struct csr1212_keyval *
Stefan Richter982610b2007-03-11 22:49:34 +0100150csr1212_find_keyval_offset(struct csr1212_keyval *kv_list, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct csr1212_keyval *kv;
153
Stefan Richterc868ae22007-03-14 00:26:38 +0100154 for (kv = kv_list->next; kv && (kv != kv_list); kv = kv->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (kv->offset == offset)
156 return kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return NULL;
158}
159
160
161/* Creation Routines */
Stefan Richter6c88e472007-03-11 22:47:34 +0100162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops,
164 size_t bus_info_size, void *private)
165{
166 struct csr1212_csr *csr;
167
168 csr = CSR1212_MALLOC(sizeof(*csr));
169 if (!csr)
170 return NULL;
171
172 csr->cache_head =
173 csr1212_rom_cache_malloc(CSR1212_CONFIG_ROM_SPACE_OFFSET,
174 CSR1212_CONFIG_ROM_SPACE_SIZE);
175 if (!csr->cache_head) {
176 CSR1212_FREE(csr);
177 return NULL;
178 }
179
180 /* The keyval key id is not used for the root node, but a valid key id
181 * that can be used for a directory needs to be passed to
182 * csr1212_new_directory(). */
183 csr->root_kv = csr1212_new_directory(CSR1212_KV_ID_VENDOR);
184 if (!csr->root_kv) {
185 CSR1212_FREE(csr->cache_head);
186 CSR1212_FREE(csr);
187 return NULL;
188 }
189
190 csr->bus_info_data = csr->cache_head->data;
191 csr->bus_info_len = bus_info_size;
192 csr->crc_len = bus_info_size;
193 csr->ops = ops;
194 csr->private = private;
195 csr->cache_tail = csr->cache_head;
196
197 return csr;
198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200void csr1212_init_local_csr(struct csr1212_csr *csr,
Stefan Richter982610b2007-03-11 22:49:34 +0100201 const u32 *bus_info_data, int max_rom)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 static const int mr_map[] = { 4, 64, 1024, 0 };
204
Ben Collins1934b8b2005-07-09 20:01:23 -0400205 BUG_ON(max_rom & ~0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 csr->max_rom = mr_map[max_rom];
207 memcpy(csr->bus_info_data, bus_info_data, csr->bus_info_len);
208}
209
Stefan Richter982610b2007-03-11 22:49:34 +0100210static struct csr1212_keyval *csr1212_new_keyval(u8 type, u8 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct csr1212_keyval *kv;
213
214 if (key < 0x30 && ((csr1212_key_id_type_map[key] & (1 << type)) == 0))
215 return NULL;
216
217 kv = CSR1212_MALLOC(sizeof(*kv));
218 if (!kv)
219 return NULL;
220
221 kv->key.type = type;
222 kv->key.id = key;
223
224 kv->associate = NULL;
225 kv->refcnt = 1;
226
227 kv->next = NULL;
228 kv->prev = NULL;
229 kv->offset = 0;
230 kv->valid = 0;
231 return kv;
232}
233
Stefan Richter982610b2007-03-11 22:49:34 +0100234struct csr1212_keyval *csr1212_new_immediate(u8 key, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Stefan Richterc868ae22007-03-14 00:26:38 +0100236 struct csr1212_keyval *kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Stefan Richterc868ae22007-03-14 00:26:38 +0100238 kv = csr1212_new_keyval(CSR1212_KV_TYPE_IMMEDIATE, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!kv)
240 return NULL;
241
242 kv->value.immediate = value;
243 kv->valid = 1;
244 return kv;
245}
246
Stefan Richter6c88e472007-03-11 22:47:34 +0100247static struct csr1212_keyval *
Stefan Richter982610b2007-03-11 22:49:34 +0100248csr1212_new_leaf(u8 key, const void *data, size_t data_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Stefan Richterc868ae22007-03-14 00:26:38 +0100250 struct csr1212_keyval *kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Stefan Richterc868ae22007-03-14 00:26:38 +0100252 kv = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!kv)
254 return NULL;
255
256 if (data_len > 0) {
257 kv->value.leaf.data = CSR1212_MALLOC(data_len);
258 if (!kv->value.leaf.data) {
259 CSR1212_FREE(kv);
260 return NULL;
261 }
262
263 if (data)
264 memcpy(kv->value.leaf.data, data, data_len);
265 } else {
266 kv->value.leaf.data = NULL;
267 }
268
269 kv->value.leaf.len = bytes_to_quads(data_len);
270 kv->offset = 0;
271 kv->valid = 1;
272
273 return kv;
274}
275
Stefan Richter6c88e472007-03-11 22:47:34 +0100276static struct csr1212_keyval *
Stefan Richter982610b2007-03-11 22:49:34 +0100277csr1212_new_csr_offset(u8 key, u32 csr_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Stefan Richterc868ae22007-03-14 00:26:38 +0100279 struct csr1212_keyval *kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Stefan Richterc868ae22007-03-14 00:26:38 +0100281 kv = csr1212_new_keyval(CSR1212_KV_TYPE_CSR_OFFSET, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!kv)
283 return NULL;
284
285 kv->value.csr_offset = csr_offset;
286
287 kv->offset = 0;
288 kv->valid = 1;
289 return kv;
290}
291
Stefan Richter982610b2007-03-11 22:49:34 +0100292struct csr1212_keyval *csr1212_new_directory(u8 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Stefan Richterc868ae22007-03-14 00:26:38 +0100294 struct csr1212_keyval *kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Stefan Richterc868ae22007-03-14 00:26:38 +0100296 kv = csr1212_new_keyval(CSR1212_KV_TYPE_DIRECTORY, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (!kv)
298 return NULL;
299
300 kv->value.directory.len = 0;
301 kv->offset = 0;
302 kv->value.directory.dentries_head = NULL;
303 kv->value.directory.dentries_tail = NULL;
304 kv->valid = 1;
305 return kv;
306}
307
Stefan Richter64ff7122007-03-11 22:50:13 +0100308void csr1212_associate_keyval(struct csr1212_keyval *kv,
309 struct csr1212_keyval *associate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Stefan Richter64ff7122007-03-11 22:50:13 +0100311 BUG_ON(!kv || !associate || kv->key.id == CSR1212_KV_ID_DESCRIPTOR ||
312 (associate->key.id != CSR1212_KV_ID_DESCRIPTOR &&
313 associate->key.id != CSR1212_KV_ID_DEPENDENT_INFO &&
314 associate->key.id != CSR1212_KV_ID_EXTENDED_KEY &&
315 associate->key.id != CSR1212_KV_ID_EXTENDED_DATA &&
316 associate->key.id < 0x30) ||
317 (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID &&
Stefan Richterc868ae22007-03-14 00:26:38 +0100318 associate->key.id != CSR1212_KV_ID_EXTENDED_KEY) ||
Stefan Richter64ff7122007-03-11 22:50:13 +0100319 (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
320 associate->key.id != CSR1212_KV_ID_EXTENDED_DATA) ||
321 (associate->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
322 kv->key.id != CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) ||
323 (associate->key.id == CSR1212_KV_ID_EXTENDED_DATA &&
324 kv->key.id != CSR1212_KV_ID_EXTENDED_KEY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (kv->associate)
327 csr1212_release_keyval(kv->associate);
328
329 associate->refcnt++;
330 kv->associate = associate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
334 struct csr1212_keyval *kv)
335{
336 struct csr1212_dentry *dentry;
337
Stefan Richter64ff7122007-03-11 22:50:13 +0100338 BUG_ON(!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 dentry = CSR1212_MALLOC(sizeof(*dentry));
341 if (!dentry)
Stefan Richter7fb9add2007-03-11 22:49:05 +0100342 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 dentry->kv = kv;
345
346 kv->refcnt++;
347
348 dentry->next = NULL;
349 dentry->prev = dir->value.directory.dentries_tail;
350
351 if (!dir->value.directory.dentries_head)
352 dir->value.directory.dentries_head = dentry;
353
354 if (dir->value.directory.dentries_tail)
355 dir->value.directory.dentries_tail->next = dentry;
356 dir->value.directory.dentries_tail = dentry;
357
358 return CSR1212_SUCCESS;
359}
360
Stefan Richter6c88e472007-03-11 22:47:34 +0100361#define CSR1212_DESCRIPTOR_LEAF_DATA(kv) \
362 (&((kv)->value.leaf.data[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Stefan Richter6c88e472007-03-11 22:47:34 +0100364#define CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, type) \
365 ((kv)->value.leaf.data[0] = \
Stefan Richter7fb9add2007-03-11 22:49:05 +0100366 cpu_to_be32(CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) | \
367 ((type) << CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)))
Stefan Richter6c88e472007-03-11 22:47:34 +0100368#define CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, spec_id) \
369 ((kv)->value.leaf.data[0] = \
Stefan Richter7fb9add2007-03-11 22:49:05 +0100370 cpu_to_be32((CSR1212_DESCRIPTOR_LEAF_TYPE(kv) << \
371 CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT) | \
372 ((spec_id) & CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Stefan Richter6c88e472007-03-11 22:47:34 +0100374static struct csr1212_keyval *
Stefan Richter982610b2007-03-11 22:49:34 +0100375csr1212_new_descriptor_leaf(u8 dtype, u32 specifier_id,
Stefan Richter6c88e472007-03-11 22:47:34 +0100376 const void *data, size_t data_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct csr1212_keyval *kv;
379
380 kv = csr1212_new_leaf(CSR1212_KV_ID_DESCRIPTOR, NULL,
381 data_len + CSR1212_DESCRIPTOR_LEAF_OVERHEAD);
382 if (!kv)
383 return NULL;
384
385 CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, dtype);
386 CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, specifier_id);
387
Stefan Richtera1c62502007-03-14 00:27:18 +0100388 if (data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 memcpy(CSR1212_DESCRIPTOR_LEAF_DATA(kv), data, data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 return kv;
392}
393
Stefan Richtera1c62502007-03-14 00:27:18 +0100394/* Check if string conforms to minimal ASCII as per IEEE 1212 clause 7.4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static int csr1212_check_minimal_ascii(const char *s)
396{
397 static const char minimal_ascii_table[] = {
Stefan Richtera1c62502007-03-14 00:27:18 +0100398 /* 1 2 4 8 16 32 64 128 */
399 128, /* --, --, --, --, --, --, --, 07, */
400 4 + 16 + 32, /* --, --, 0a, --, 0C, 0D, --, --, */
401 0, /* --, --, --, --, --, --, --, --, */
402 0, /* --, --, --, --, --, --, --, --, */
403 255 - 8 - 16, /* 20, 21, 22, --, --, 25, 26, 27, */
404 255, /* 28, 29, 2a, 2b, 2c, 2d, 2e, 2f, */
405 255, /* 30, 31, 32, 33, 34, 35, 36, 37, */
406 255, /* 38, 39, 3a, 3b, 3c, 3d, 3e, 3f, */
407 255, /* 40, 41, 42, 43, 44, 45, 46, 47, */
408 255, /* 48, 49, 4a, 4b, 4c, 4d, 4e, 4f, */
409 255, /* 50, 51, 52, 53, 54, 55, 56, 57, */
410 1 + 2 + 4 + 128, /* 58, 59, 5a, --, --, --, --, 5f, */
411 255 - 1, /* --, 61, 62, 63, 64, 65, 66, 67, */
412 255, /* 68, 69, 6a, 6b, 6c, 6d, 6e, 6f, */
413 255, /* 70, 71, 72, 73, 74, 75, 76, 77, */
414 1 + 2 + 4, /* 78, 79, 7a, --, --, --, --, --, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 };
Stefan Richtera1c62502007-03-14 00:27:18 +0100416 int i, j;
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 for (; *s; s++) {
Stefan Richtera1c62502007-03-14 00:27:18 +0100419 i = *s >> 3; /* i = *s / 8; */
420 j = 1 << (*s & 3); /* j = 1 << (*s % 8); */
421
422 if (i >= ARRAY_SIZE(minimal_ascii_table) ||
423 !(minimal_ascii_table[i] & j))
424 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
427}
428
Stefan Richtera1c62502007-03-14 00:27:18 +0100429/* IEEE 1212 clause 7.5.4.1 textual descriptors (English, minimal ASCII) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s)
431{
Stefan Richtera1c62502007-03-14 00:27:18 +0100432 struct csr1212_keyval *kv;
433 u32 *text;
434 size_t str_len, quads;
435
436 if (!s || !*s || csr1212_check_minimal_ascii(s))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return NULL;
438
Stefan Richtera1c62502007-03-14 00:27:18 +0100439 str_len = strlen(s);
440 quads = bytes_to_quads(str_len);
441 kv = csr1212_new_descriptor_leaf(0, 0, NULL, quads_to_bytes(quads) +
442 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_OVERHEAD);
443 if (!kv)
444 return NULL;
445
446 kv->value.leaf.data[1] = 0; /* width, character_set, language */
447 text = CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv);
448 text[quads - 1] = 0; /* padding */
449 memcpy(text, s, str_len);
450
451 return kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455/* Destruction Routines */
456
457void csr1212_detach_keyval_from_directory(struct csr1212_keyval *dir,
458 struct csr1212_keyval *kv)
459{
460 struct csr1212_dentry *dentry;
461
462 if (!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY)
463 return;
464
465 dentry = csr1212_find_keyval(dir, kv);
466
467 if (!dentry)
468 return;
469
470 if (dentry->prev)
471 dentry->prev->next = dentry->next;
472 if (dentry->next)
473 dentry->next->prev = dentry->prev;
474 if (dir->value.directory.dentries_head == dentry)
475 dir->value.directory.dentries_head = dentry->next;
476 if (dir->value.directory.dentries_tail == dentry)
477 dir->value.directory.dentries_tail = dentry->prev;
478
479 CSR1212_FREE(dentry);
480
481 csr1212_release_keyval(kv);
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/* This function is used to free the memory taken by a keyval. If the given
485 * keyval is a directory type, then any keyvals contained in that directory
486 * will be destroyed as well if their respective refcnts are 0. By means of
487 * list manipulation, this routine will descend a directory structure in a
488 * non-recursive manner. */
Stefan Richterc1a37f22007-03-14 00:20:53 +0100489static void csr1212_destroy_keyval(struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct csr1212_keyval *k, *a;
492 struct csr1212_dentry dentry;
493 struct csr1212_dentry *head, *tail;
494
495 dentry.kv = kv;
496 dentry.next = NULL;
497 dentry.prev = NULL;
498
499 head = &dentry;
500 tail = head;
501
502 while (head) {
503 k = head->kv;
504
505 while (k) {
506 k->refcnt--;
507
508 if (k->refcnt > 0)
509 break;
510
511 a = k->associate;
512
513 if (k->key.type == CSR1212_KV_TYPE_DIRECTORY) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100514 /* If the current entry is a directory, move all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * the entries to the destruction list. */
516 if (k->value.directory.dentries_head) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100517 tail->next =
518 k->value.directory.dentries_head;
519 k->value.directory.dentries_head->prev =
520 tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 tail = k->value.directory.dentries_tail;
522 }
523 }
524 free_keyval(k);
525 k = a;
526 }
527
528 head = head->next;
529 if (head) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100530 if (head->prev && head->prev != &dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 CSR1212_FREE(head->prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 head->prev = NULL;
Stefan Richterc868ae22007-03-14 00:26:38 +0100533 } else if (tail != &dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 CSR1212_FREE(tail);
Stefan Richterc868ae22007-03-14 00:26:38 +0100535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537}
538
Stefan Richterc1a37f22007-03-14 00:20:53 +0100539void csr1212_release_keyval(struct csr1212_keyval *kv)
540{
541 if (kv->refcnt > 1)
542 kv->refcnt--;
543 else
544 csr1212_destroy_keyval(kv);
545}
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547void csr1212_destroy_csr(struct csr1212_csr *csr)
548{
549 struct csr1212_csr_rom_cache *c, *oc;
550 struct csr1212_cache_region *cr, *ocr;
551
552 csr1212_release_keyval(csr->root_kv);
553
554 c = csr->cache_head;
555 while (c) {
556 oc = c;
557 cr = c->filled_head;
558 while (cr) {
559 ocr = cr;
560 cr = cr->next;
561 CSR1212_FREE(ocr);
562 }
563 c = c->next;
564 CSR1212_FREE(oc);
565 }
566
567 CSR1212_FREE(csr);
568}
569
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/* CSR Image Creation */
572
573static int csr1212_append_new_cache(struct csr1212_csr *csr, size_t romsize)
574{
575 struct csr1212_csr_rom_cache *cache;
Stefan Richter982610b2007-03-11 22:49:34 +0100576 u64 csr_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Stefan Richter64ff7122007-03-11 22:50:13 +0100578 BUG_ON(!csr || !csr->ops || !csr->ops->allocate_addr_range ||
579 !csr->ops->release_addr || csr->max_rom < 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /* ROM size must be a multiple of csr->max_rom */
582 romsize = (romsize + (csr->max_rom - 1)) & ~(csr->max_rom - 1);
583
Stefan Richterc868ae22007-03-14 00:26:38 +0100584 csr_addr = csr->ops->allocate_addr_range(romsize, csr->max_rom,
585 csr->private);
586 if (csr_addr == CSR1212_INVALID_ADDR_SPACE)
Stefan Richter7fb9add2007-03-11 22:49:05 +0100587 return -ENOMEM;
Stefan Richterc868ae22007-03-14 00:26:38 +0100588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (csr_addr < CSR1212_REGISTER_SPACE_BASE) {
590 /* Invalid address returned from allocate_addr_range(). */
591 csr->ops->release_addr(csr_addr, csr->private);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100592 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
Stefan Richterc868ae22007-03-14 00:26:38 +0100595 cache = csr1212_rom_cache_malloc(csr_addr - CSR1212_REGISTER_SPACE_BASE,
596 romsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!cache) {
598 csr->ops->release_addr(csr_addr, csr->private);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100599 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
Stefan Richterc868ae22007-03-14 00:26:38 +0100602 cache->ext_rom = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF,
603 CSR1212_KV_ID_EXTENDED_ROM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!cache->ext_rom) {
605 csr->ops->release_addr(csr_addr, csr->private);
606 CSR1212_FREE(cache);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100607 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
Stefan Richterc868ae22007-03-14 00:26:38 +0100610 if (csr1212_attach_keyval_to_directory(csr->root_kv, cache->ext_rom) !=
611 CSR1212_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 csr1212_release_keyval(cache->ext_rom);
613 csr->ops->release_addr(csr_addr, csr->private);
614 CSR1212_FREE(cache);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100615 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 cache->ext_rom->offset = csr_addr - CSR1212_REGISTER_SPACE_BASE;
618 cache->ext_rom->value.leaf.len = -1;
619 cache->ext_rom->value.leaf.data = cache->data;
620
621 /* Add cache to tail of cache list */
622 cache->prev = csr->cache_tail;
623 csr->cache_tail->next = cache;
624 csr->cache_tail = cache;
625 return CSR1212_SUCCESS;
626}
627
Stefan Richter6c88e472007-03-11 22:47:34 +0100628static void csr1212_remove_cache(struct csr1212_csr *csr,
629 struct csr1212_csr_rom_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 if (csr->cache_head == cache)
632 csr->cache_head = cache->next;
633 if (csr->cache_tail == cache)
634 csr->cache_tail = cache->prev;
635
636 if (cache->prev)
637 cache->prev->next = cache->next;
638 if (cache->next)
639 cache->next->prev = cache->prev;
640
641 if (cache->ext_rom) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100642 csr1212_detach_keyval_from_directory(csr->root_kv,
643 cache->ext_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 csr1212_release_keyval(cache->ext_rom);
645 }
646
647 CSR1212_FREE(cache);
648}
649
650static int csr1212_generate_layout_subdir(struct csr1212_keyval *dir,
651 struct csr1212_keyval **layout_tail)
652{
653 struct csr1212_dentry *dentry;
654 struct csr1212_keyval *dkv;
655 struct csr1212_keyval *last_extkey_spec = NULL;
656 struct csr1212_keyval *last_extkey = NULL;
657 int num_entries = 0;
658
659 for (dentry = dir->value.directory.dentries_head; dentry;
660 dentry = dentry->next) {
661 for (dkv = dentry->kv; dkv; dkv = dkv->associate) {
662 /* Special Case: Extended Key Specifier_ID */
Stefan Richterc868ae22007-03-14 00:26:38 +0100663 if (dkv->key.id ==
664 CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
665 if (last_extkey_spec == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 last_extkey_spec = dkv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100667 else if (dkv->value.immediate !=
668 last_extkey_spec->value.immediate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 last_extkey_spec = dkv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100670 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* Special Case: Extended Key */
673 } else if (dkv->key.id == CSR1212_KV_ID_EXTENDED_KEY) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100674 if (last_extkey == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 last_extkey = dkv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100676 else if (dkv->value.immediate !=
677 last_extkey->value.immediate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 last_extkey = dkv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100679 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
683 num_entries += 1;
684
Stefan Richterc868ae22007-03-14 00:26:38 +0100685 switch (dkv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 default:
687 case CSR1212_KV_TYPE_IMMEDIATE:
688 case CSR1212_KV_TYPE_CSR_OFFSET:
689 break;
690 case CSR1212_KV_TYPE_LEAF:
691 case CSR1212_KV_TYPE_DIRECTORY:
692 /* Remove from list */
693 if (dkv->prev && (dkv->prev->next == dkv))
694 dkv->prev->next = dkv->next;
695 if (dkv->next && (dkv->next->prev == dkv))
696 dkv->next->prev = dkv->prev;
697 //if (dkv == *layout_tail)
698 // *layout_tail = dkv->prev;
699
700 /* Special case: Extended ROM leafs */
701 if (dkv->key.id == CSR1212_KV_ID_EXTENDED_ROM) {
702 dkv->value.leaf.len = -1;
Stefan Richterc868ae22007-03-14 00:26:38 +0100703 /* Don't add Extended ROM leafs in the
704 * layout list, they are handled
705 * differently. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
707 }
708
709 /* Add to tail of list */
710 dkv->next = NULL;
711 dkv->prev = *layout_tail;
712 (*layout_tail)->next = dkv;
713 *layout_tail = dkv;
714 break;
715 }
716 }
717 }
718 return num_entries;
719}
720
Stefan Richter6c88e472007-03-11 22:47:34 +0100721static size_t csr1212_generate_layout_order(struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 struct csr1212_keyval *ltail = kv;
724 size_t agg_size = 0;
725
Stefan Richterc868ae22007-03-14 00:26:38 +0100726 while (kv) {
727 switch (kv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 case CSR1212_KV_TYPE_LEAF:
729 /* Add 1 quadlet for crc/len field */
730 agg_size += kv->value.leaf.len + 1;
731 break;
732
733 case CSR1212_KV_TYPE_DIRECTORY:
Stefan Richterc868ae22007-03-14 00:26:38 +0100734 kv->value.directory.len =
735 csr1212_generate_layout_subdir(kv, &ltail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* Add 1 quadlet for crc/len field */
737 agg_size += kv->value.directory.len + 1;
738 break;
739 }
740 kv = kv->next;
741 }
742 return quads_to_bytes(agg_size);
743}
744
Stefan Richter6c88e472007-03-11 22:47:34 +0100745static struct csr1212_keyval *
746csr1212_generate_positions(struct csr1212_csr_rom_cache *cache,
747 struct csr1212_keyval *start_kv, int start_pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 struct csr1212_keyval *kv = start_kv;
750 struct csr1212_keyval *okv = start_kv;
751 int pos = start_pos;
752 int kv_len = 0, okv_len = 0;
753
754 cache->layout_head = kv;
755
Stefan Richterc868ae22007-03-14 00:26:38 +0100756 while (kv && pos < cache->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* Special case: Extended ROM leafs */
Stefan Richterc868ae22007-03-14 00:26:38 +0100758 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 kv->offset = cache->offset + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Stefan Richterc868ae22007-03-14 00:26:38 +0100761 switch (kv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 case CSR1212_KV_TYPE_LEAF:
763 kv_len = kv->value.leaf.len;
764 break;
765
766 case CSR1212_KV_TYPE_DIRECTORY:
767 kv_len = kv->value.directory.len;
768 break;
769
770 default:
771 /* Should never get here */
Stefan Richterc94ccf92007-03-14 00:27:46 +0100772 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 break;
774 }
775
776 pos += quads_to_bytes(kv_len + 1);
777
778 if (pos <= cache->size) {
779 okv = kv;
780 okv_len = kv_len;
781 kv = kv->next;
782 }
783 }
784
785 cache->layout_tail = okv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100786 cache->len = okv->offset - cache->offset + quads_to_bytes(okv_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 return kv;
789}
790
Stefan Richter6c88e472007-03-11 22:47:34 +0100791#define CSR1212_KV_KEY_SHIFT 24
792#define CSR1212_KV_KEY_TYPE_SHIFT 6
793#define CSR1212_KV_KEY_ID_MASK 0x3f
794#define CSR1212_KV_KEY_TYPE_MASK 0x3 /* after shift */
795
796static void
Stefan Richter982610b2007-03-11 22:49:34 +0100797csr1212_generate_tree_subdir(struct csr1212_keyval *dir, u32 *data_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 struct csr1212_dentry *dentry;
800 struct csr1212_keyval *last_extkey_spec = NULL;
801 struct csr1212_keyval *last_extkey = NULL;
802 int index = 0;
803
Stefan Richterc868ae22007-03-14 00:26:38 +0100804 for (dentry = dir->value.directory.dentries_head;
805 dentry;
806 dentry = dentry->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 struct csr1212_keyval *a;
808
809 for (a = dentry->kv; a; a = a->associate) {
Stefan Richter982610b2007-03-11 22:49:34 +0100810 u32 value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* Special Case: Extended Key Specifier_ID */
Stefan Richterc868ae22007-03-14 00:26:38 +0100813 if (a->key.id ==
814 CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
815 if (last_extkey_spec == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 last_extkey_spec = a;
Stefan Richterc868ae22007-03-14 00:26:38 +0100817 else if (a->value.immediate !=
818 last_extkey_spec->value.immediate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 last_extkey_spec = a;
Stefan Richterc868ae22007-03-14 00:26:38 +0100820 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 continue;
Stefan Richterc868ae22007-03-14 00:26:38 +0100822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* Special Case: Extended Key */
824 } else if (a->key.id == CSR1212_KV_ID_EXTENDED_KEY) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100825 if (last_extkey == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 last_extkey = a;
Stefan Richterc868ae22007-03-14 00:26:38 +0100827 else if (a->value.immediate !=
828 last_extkey->value.immediate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 last_extkey = a;
Stefan Richterc868ae22007-03-14 00:26:38 +0100830 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
Stefan Richterc868ae22007-03-14 00:26:38 +0100834 switch (a->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 case CSR1212_KV_TYPE_IMMEDIATE:
836 value = a->value.immediate;
837 break;
838 case CSR1212_KV_TYPE_CSR_OFFSET:
839 value = a->value.csr_offset;
840 break;
841 case CSR1212_KV_TYPE_LEAF:
842 value = a->offset;
843 value -= dir->offset + quads_to_bytes(1+index);
844 value = bytes_to_quads(value);
845 break;
846 case CSR1212_KV_TYPE_DIRECTORY:
847 value = a->offset;
848 value -= dir->offset + quads_to_bytes(1+index);
849 value = bytes_to_quads(value);
850 break;
851 default:
852 /* Should never get here */
Stefan Richterc94ccf92007-03-14 00:27:46 +0100853 WARN_ON(1);
854 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
Stefan Richterc868ae22007-03-14 00:26:38 +0100857 value |= (a->key.id & CSR1212_KV_KEY_ID_MASK) <<
858 CSR1212_KV_KEY_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 value |= (a->key.type & CSR1212_KV_KEY_TYPE_MASK) <<
Stefan Richterc868ae22007-03-14 00:26:38 +0100860 (CSR1212_KV_KEY_SHIFT +
861 CSR1212_KV_KEY_TYPE_SHIFT);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100862 data_buffer[index] = cpu_to_be32(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 index++;
864 }
865 }
866}
867
Stefan Richter6c88e472007-03-11 22:47:34 +0100868struct csr1212_keyval_img {
Stefan Richter982610b2007-03-11 22:49:34 +0100869 u16 length;
870 u16 crc;
Stefan Richter6c88e472007-03-11 22:47:34 +0100871
872 /* Must be last */
Stefan Richter982610b2007-03-11 22:49:34 +0100873 u32 data[0]; /* older gcc can't handle [] which is standard */
Stefan Richter6c88e472007-03-11 22:47:34 +0100874};
875
876static void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct csr1212_keyval *kv, *nkv;
879 struct csr1212_keyval_img *kvi;
880
Stefan Richterc868ae22007-03-14 00:26:38 +0100881 for (kv = cache->layout_head;
882 kv != cache->layout_tail->next;
883 kv = nkv) {
884 kvi = (struct csr1212_keyval_img *)(cache->data +
885 bytes_to_quads(kv->offset - cache->offset));
886 switch (kv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 default:
888 case CSR1212_KV_TYPE_IMMEDIATE:
889 case CSR1212_KV_TYPE_CSR_OFFSET:
890 /* Should never get here */
Stefan Richterc94ccf92007-03-14 00:27:46 +0100891 WARN_ON(1);
892 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 case CSR1212_KV_TYPE_LEAF:
895 /* Don't copy over Extended ROM areas, they are
896 * already filled out! */
897 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
898 memcpy(kvi->data, kv->value.leaf.data,
899 quads_to_bytes(kv->value.leaf.len));
900
Stefan Richter7fb9add2007-03-11 22:49:05 +0100901 kvi->length = cpu_to_be16(kv->value.leaf.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 kvi->crc = csr1212_crc16(kvi->data, kv->value.leaf.len);
903 break;
904
905 case CSR1212_KV_TYPE_DIRECTORY:
906 csr1212_generate_tree_subdir(kv, kvi->data);
907
Stefan Richter7fb9add2007-03-11 22:49:05 +0100908 kvi->length = cpu_to_be16(kv->value.directory.len);
Stefan Richterc868ae22007-03-14 00:26:38 +0100909 kvi->crc = csr1212_crc16(kvi->data,
910 kv->value.directory.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 break;
912 }
913
914 nkv = kv->next;
915 if (kv->prev)
916 kv->prev->next = NULL;
917 if (kv->next)
918 kv->next->prev = NULL;
919 kv->prev = NULL;
920 kv->next = NULL;
921 }
922}
923
Stefan Richterfd2f3bd2007-03-11 22:51:24 +0100924/* This size is arbitrarily chosen.
925 * The struct overhead is subtracted for more economic allocations. */
926#define CSR1212_EXTENDED_ROM_SIZE (2048 - sizeof(struct csr1212_csr_rom_cache))
Stefan Richter6c88e472007-03-11 22:47:34 +0100927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928int csr1212_generate_csr_image(struct csr1212_csr *csr)
929{
930 struct csr1212_bus_info_block_img *bi;
931 struct csr1212_csr_rom_cache *cache;
932 struct csr1212_keyval *kv;
933 size_t agg_size;
934 int ret;
935 int init_offset;
936
Stefan Richter64ff7122007-03-11 22:50:13 +0100937 BUG_ON(!csr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 cache = csr->cache_head;
940
941 bi = (struct csr1212_bus_info_block_img*)cache->data;
942
943 bi->length = bytes_to_quads(csr->bus_info_len) - 1;
944 bi->crc_length = bi->length;
945 bi->crc = csr1212_crc16(bi->data, bi->crc_length);
946
947 csr->root_kv->next = NULL;
948 csr->root_kv->prev = NULL;
949
950 agg_size = csr1212_generate_layout_order(csr->root_kv);
951
952 init_offset = csr->bus_info_len;
953
Stefan Richterc868ae22007-03-14 00:26:38 +0100954 for (kv = csr->root_kv, cache = csr->cache_head;
955 kv;
956 cache = cache->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (!cache) {
958 /* Estimate approximate number of additional cache
959 * regions needed (it assumes that the cache holding
960 * the first 1K Config ROM space always exists). */
961 int est_c = agg_size / (CSR1212_EXTENDED_ROM_SIZE -
Stefan Richter982610b2007-03-11 22:49:34 +0100962 (2 * sizeof(u32))) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /* Add additional cache regions, extras will be
965 * removed later */
966 for (; est_c; est_c--) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100967 ret = csr1212_append_new_cache(csr,
968 CSR1212_EXTENDED_ROM_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (ret != CSR1212_SUCCESS)
970 return ret;
971 }
972 /* Need to re-layout for additional cache regions */
973 agg_size = csr1212_generate_layout_order(csr->root_kv);
974 kv = csr->root_kv;
975 cache = csr->cache_head;
976 init_offset = csr->bus_info_len;
977 }
978 kv = csr1212_generate_positions(cache, kv, init_offset);
979 agg_size -= cache->len;
Stefan Richter982610b2007-03-11 22:49:34 +0100980 init_offset = sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982
983 /* Remove unused, excess cache regions */
984 while (cache) {
985 struct csr1212_csr_rom_cache *oc = cache;
986
987 cache = cache->next;
988 csr1212_remove_cache(csr, oc);
989 }
990
991 /* Go through the list backward so that when done, the correct CRC
992 * will be calculated for the Extended ROM areas. */
Stefan Richterc868ae22007-03-14 00:26:38 +0100993 for (cache = csr->cache_tail; cache; cache = cache->prev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* Only Extended ROM caches should have this set. */
995 if (cache->ext_rom) {
996 int leaf_size;
997
998 /* Make sure the Extended ROM leaf is a multiple of
999 * max_rom in size. */
Stefan Richter64ff7122007-03-11 22:50:13 +01001000 BUG_ON(csr->max_rom < 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 leaf_size = (cache->len + (csr->max_rom - 1)) &
1002 ~(csr->max_rom - 1);
1003
1004 /* Zero out the unused ROM region */
1005 memset(cache->data + bytes_to_quads(cache->len), 0x00,
1006 leaf_size - cache->len);
1007
1008 /* Subtract leaf header */
Stefan Richter982610b2007-03-11 22:49:34 +01001009 leaf_size -= sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 /* Update the Extended ROM leaf length */
1012 cache->ext_rom->value.leaf.len =
1013 bytes_to_quads(leaf_size);
1014 } else {
1015 /* Zero out the unused ROM region */
1016 memset(cache->data + bytes_to_quads(cache->len), 0x00,
1017 cache->size - cache->len);
1018 }
1019
1020 /* Copy the data into the cache buffer */
1021 csr1212_fill_cache(cache);
1022
1023 if (cache != csr->cache_head) {
1024 /* Set the length and CRC of the extended ROM. */
1025 struct csr1212_keyval_img *kvi =
1026 (struct csr1212_keyval_img*)cache->data;
Stefan Richter982610b2007-03-11 22:49:34 +01001027 u16 len = bytes_to_quads(cache->len) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Stefan Richter7fb9add2007-03-11 22:49:05 +01001029 kvi->length = cpu_to_be16(len);
1030 kvi->crc = csr1212_crc16(kvi->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032 }
1033
1034 return CSR1212_SUCCESS;
1035}
1036
Stefan Richter982610b2007-03-11 22:49:34 +01001037int csr1212_read(struct csr1212_csr *csr, u32 offset, void *buffer, u32 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 struct csr1212_csr_rom_cache *cache;
1040
Stefan Richterc868ae22007-03-14 00:26:38 +01001041 for (cache = csr->cache_head; cache; cache = cache->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (offset >= cache->offset &&
1043 (offset + len) <= (cache->offset + cache->size)) {
Stefan Richterc868ae22007-03-14 00:26:38 +01001044 memcpy(buffer, &cache->data[
1045 bytes_to_quads(offset - cache->offset)],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 len);
1047 return CSR1212_SUCCESS;
1048 }
Stefan Richterc868ae22007-03-14 00:26:38 +01001049
Stefan Richter7fb9add2007-03-11 22:49:05 +01001050 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/* Parse a chunk of data as a Config ROM */
1055
1056static int csr1212_parse_bus_info_block(struct csr1212_csr *csr)
1057{
1058 struct csr1212_bus_info_block_img *bi;
1059 struct csr1212_cache_region *cr;
1060 int i;
1061 int ret;
1062
1063 /* IEEE 1212 says that the entire bus info block should be readable in
1064 * a single transaction regardless of the max_rom value.
1065 * Unfortunately, many IEEE 1394 devices do not abide by that, so the
1066 * bus info block will be read 1 quadlet at a time. The rest of the
1067 * ConfigROM will be read according to the max_rom field. */
Stefan Richter982610b2007-03-11 22:49:34 +01001068 for (i = 0; i < csr->bus_info_len; i += sizeof(u32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
Stefan Richterc868ae22007-03-14 00:26:38 +01001070 sizeof(u32), &csr->cache_head->data[bytes_to_quads(i)],
1071 csr->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (ret != CSR1212_SUCCESS)
1073 return ret;
Stefan Richterb2051f82007-01-03 19:32:13 +01001074
1075 /* check ROM header's info_length */
1076 if (i == 0 &&
Stefan Richter7fb9add2007-03-11 22:49:05 +01001077 be32_to_cpu(csr->cache_head->data[0]) >> 24 !=
Stefan Richterb2051f82007-01-03 19:32:13 +01001078 bytes_to_quads(csr->bus_info_len) - 1)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001079 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
1082 bi = (struct csr1212_bus_info_block_img*)csr->cache_head->data;
1083 csr->crc_len = quads_to_bytes(bi->crc_length);
1084
Stefan Richterc868ae22007-03-14 00:26:38 +01001085 /* IEEE 1212 recommends that crc_len be equal to bus_info_len, but that
1086 * is not always the case, so read the rest of the crc area 1 quadlet at
1087 * a time. */
Stefan Richter982610b2007-03-11 22:49:34 +01001088 for (i = csr->bus_info_len; i <= csr->crc_len; i += sizeof(u32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
Stefan Richterc868ae22007-03-14 00:26:38 +01001090 sizeof(u32), &csr->cache_head->data[bytes_to_quads(i)],
1091 csr->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (ret != CSR1212_SUCCESS)
1093 return ret;
1094 }
1095
Stefan Richterd2652502007-03-14 00:29:20 +01001096 /* Apparently there are many different wrong implementations of the CRC
1097 * algorithm. We don't fail, we just warn. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if ((csr1212_crc16(bi->data, bi->crc_length) != bi->crc) &&
1099 (csr1212_msft_crc16(bi->data, bi->crc_length) != bi->crc))
Stefan Richterd2652502007-03-14 00:29:20 +01001100 printk(KERN_DEBUG "IEEE 1394 device has ROM CRC error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Stefan Richter85511582005-11-07 06:31:45 -05001102 cr = CSR1212_MALLOC(sizeof(*cr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (!cr)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001104 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 cr->next = NULL;
1107 cr->prev = NULL;
1108 cr->offset_start = 0;
1109 cr->offset_end = csr->crc_len + 4;
1110
1111 csr->cache_head->filled_head = cr;
1112 csr->cache_head->filled_tail = cr;
1113
1114 return CSR1212_SUCCESS;
1115}
1116
Stefan Richter7fb9add2007-03-11 22:49:05 +01001117#define CSR1212_KV_KEY(q) (be32_to_cpu(q) >> CSR1212_KV_KEY_SHIFT)
Stefan Richter6c88e472007-03-11 22:47:34 +01001118#define CSR1212_KV_KEY_TYPE(q) (CSR1212_KV_KEY(q) >> CSR1212_KV_KEY_TYPE_SHIFT)
1119#define CSR1212_KV_KEY_ID(q) (CSR1212_KV_KEY(q) & CSR1212_KV_KEY_ID_MASK)
1120#define CSR1212_KV_VAL_MASK 0xffffff
Stefan Richter7fb9add2007-03-11 22:49:05 +01001121#define CSR1212_KV_VAL(q) (be32_to_cpu(q) & CSR1212_KV_VAL_MASK)
Stefan Richter6c88e472007-03-11 22:47:34 +01001122
Stefan Richterc868ae22007-03-14 00:26:38 +01001123static int
1124csr1212_parse_dir_entry(struct csr1212_keyval *dir, u32 ki, u32 kv_pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 int ret = CSR1212_SUCCESS;
1127 struct csr1212_keyval *k = NULL;
Stefan Richter982610b2007-03-11 22:49:34 +01001128 u32 offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Stefan Richterc868ae22007-03-14 00:26:38 +01001130 switch (CSR1212_KV_KEY_TYPE(ki)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 case CSR1212_KV_TYPE_IMMEDIATE:
1132 k = csr1212_new_immediate(CSR1212_KV_KEY_ID(ki),
1133 CSR1212_KV_VAL(ki));
1134 if (!k) {
Stefan Richter7fb9add2007-03-11 22:49:05 +01001135 ret = -ENOMEM;
Stefan Richter511f7b32007-03-14 00:28:36 +01001136 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138
1139 k->refcnt = 0; /* Don't keep local reference when parsing. */
1140 break;
1141
1142 case CSR1212_KV_TYPE_CSR_OFFSET:
1143 k = csr1212_new_csr_offset(CSR1212_KV_KEY_ID(ki),
1144 CSR1212_KV_VAL(ki));
1145 if (!k) {
Stefan Richter7fb9add2007-03-11 22:49:05 +01001146 ret = -ENOMEM;
Stefan Richter511f7b32007-03-14 00:28:36 +01001147 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149 k->refcnt = 0; /* Don't keep local reference when parsing. */
1150 break;
1151
1152 default:
1153 /* Compute the offset from 0xffff f000 0000. */
1154 offset = quads_to_bytes(CSR1212_KV_VAL(ki)) + kv_pos;
1155 if (offset == kv_pos) {
1156 /* Uh-oh. Can't have a relative offset of 0 for Leaves
1157 * or Directories. The Config ROM image is most likely
1158 * messed up, so we'll just abort here. */
Stefan Richter7fb9add2007-03-11 22:49:05 +01001159 ret = -EIO;
Stefan Richter511f7b32007-03-14 00:28:36 +01001160 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
1163 k = csr1212_find_keyval_offset(dir, offset);
1164
1165 if (k)
1166 break; /* Found it. */
1167
Stefan Richterc868ae22007-03-14 00:26:38 +01001168 if (CSR1212_KV_KEY_TYPE(ki) == CSR1212_KV_TYPE_DIRECTORY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 k = csr1212_new_directory(CSR1212_KV_KEY_ID(ki));
Stefan Richterc868ae22007-03-14 00:26:38 +01001170 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 k = csr1212_new_leaf(CSR1212_KV_KEY_ID(ki), NULL, 0);
Stefan Richterc868ae22007-03-14 00:26:38 +01001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (!k) {
Stefan Richter7fb9add2007-03-11 22:49:05 +01001174 ret = -ENOMEM;
Stefan Richter511f7b32007-03-14 00:28:36 +01001175 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 k->refcnt = 0; /* Don't keep local reference when parsing. */
1178 k->valid = 0; /* Contents not read yet so it's not valid. */
1179 k->offset = offset;
1180
1181 k->prev = dir;
1182 k->next = dir->next;
1183 dir->next->prev = k;
1184 dir->next = k;
1185 }
1186 ret = csr1212_attach_keyval_to_directory(dir, k);
Stefan Richter511f7b32007-03-14 00:28:36 +01001187out:
Stefan Richter6c88e472007-03-11 22:47:34 +01001188 if (ret != CSR1212_SUCCESS && k != NULL)
1189 free_keyval(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return ret;
1191}
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193int csr1212_parse_keyval(struct csr1212_keyval *kv,
1194 struct csr1212_csr_rom_cache *cache)
1195{
1196 struct csr1212_keyval_img *kvi;
1197 int i;
1198 int ret = CSR1212_SUCCESS;
1199 int kvi_len;
1200
Stefan Richterc868ae22007-03-14 00:26:38 +01001201 kvi = (struct csr1212_keyval_img*)
1202 &cache->data[bytes_to_quads(kv->offset - cache->offset)];
Stefan Richter7fb9add2007-03-11 22:49:05 +01001203 kvi_len = be16_to_cpu(kvi->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Stefan Richterd2652502007-03-14 00:29:20 +01001205 /* Apparently there are many different wrong implementations of the CRC
1206 * algorithm. We don't fail, we just warn. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if ((csr1212_crc16(kvi->data, kvi_len) != kvi->crc) &&
Stefan Richterd2652502007-03-14 00:29:20 +01001208 (csr1212_msft_crc16(kvi->data, kvi_len) != kvi->crc))
1209 printk(KERN_DEBUG "IEEE 1394 device has ROM CRC error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Stefan Richterc868ae22007-03-14 00:26:38 +01001211 switch (kv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 case CSR1212_KV_TYPE_DIRECTORY:
1213 for (i = 0; i < kvi_len; i++) {
Stefan Richter982610b2007-03-11 22:49:34 +01001214 u32 ki = kvi->data[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /* Some devices put null entries in their unit
1217 * directories. If we come across such an entry,
1218 * then skip it. */
1219 if (ki == 0x0)
1220 continue;
1221 ret = csr1212_parse_dir_entry(kv, ki,
Stefan Richterc868ae22007-03-14 00:26:38 +01001222 kv->offset + quads_to_bytes(i + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 kv->value.directory.len = kvi_len;
1225 break;
1226
1227 case CSR1212_KV_TYPE_LEAF:
1228 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) {
Stefan Richterc868ae22007-03-14 00:26:38 +01001229 size_t size = quads_to_bytes(kvi_len);
1230
1231 kv->value.leaf.data = CSR1212_MALLOC(size);
Stefan Richter85511582005-11-07 06:31:45 -05001232 if (!kv->value.leaf.data) {
Stefan Richter7fb9add2007-03-11 22:49:05 +01001233 ret = -ENOMEM;
Stefan Richter511f7b32007-03-14 00:28:36 +01001234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
1237 kv->value.leaf.len = kvi_len;
Stefan Richterc868ae22007-03-14 00:26:38 +01001238 memcpy(kv->value.leaf.data, kvi->data, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 break;
1241 }
1242
1243 kv->valid = 1;
Stefan Richter511f7b32007-03-14 00:28:36 +01001244out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return ret;
1246}
1247
Stefan Richterc1a37f22007-03-14 00:20:53 +01001248static int
1249csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct csr1212_cache_region *cr, *ncr, *newcr = NULL;
1252 struct csr1212_keyval_img *kvi = NULL;
1253 struct csr1212_csr_rom_cache *cache;
1254 int cache_index;
Stefan Richter982610b2007-03-11 22:49:34 +01001255 u64 addr;
1256 u32 *cache_ptr;
1257 u16 kv_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Stefan Richter64ff7122007-03-11 22:50:13 +01001259 BUG_ON(!csr || !kv || csr->max_rom < 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 /* First find which cache the data should be in (or go in if not read
1262 * yet). */
Stefan Richterc868ae22007-03-14 00:26:38 +01001263 for (cache = csr->cache_head; cache; cache = cache->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (kv->offset >= cache->offset &&
1265 kv->offset < (cache->offset + cache->size))
1266 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 if (!cache) {
Stefan Richter982610b2007-03-11 22:49:34 +01001269 u32 q, cache_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* Only create a new cache for Extended ROM leaves. */
1272 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001273 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 if (csr->ops->bus_read(csr,
1276 CSR1212_REGISTER_SPACE_BASE + kv->offset,
Stefan Richterc868ae22007-03-14 00:26:38 +01001277 sizeof(u32), &q, csr->private))
Stefan Richter7fb9add2007-03-11 22:49:05 +01001278 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Stefan Richter7fb9add2007-03-11 22:49:05 +01001280 kv->value.leaf.len = be32_to_cpu(q) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 cache_size = (quads_to_bytes(kv->value.leaf.len + 1) +
1283 (csr->max_rom - 1)) & ~(csr->max_rom - 1);
1284
1285 cache = csr1212_rom_cache_malloc(kv->offset, cache_size);
1286 if (!cache)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001287 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 kv->value.leaf.data = &cache->data[1];
1290 csr->cache_tail->next = cache;
1291 cache->prev = csr->cache_tail;
1292 cache->next = NULL;
1293 csr->cache_tail = cache;
1294 cache->filled_head =
Stefan Richter85511582005-11-07 06:31:45 -05001295 CSR1212_MALLOC(sizeof(*cache->filled_head));
Stefan Richterc868ae22007-03-14 00:26:38 +01001296 if (!cache->filled_head)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001297 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 cache->filled_head->offset_start = 0;
Stefan Richter982610b2007-03-11 22:49:34 +01001300 cache->filled_head->offset_end = sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 cache->filled_tail = cache->filled_head;
1302 cache->filled_head->next = NULL;
1303 cache->filled_head->prev = NULL;
1304 cache->data[0] = q;
1305
1306 /* Don't read the entire extended ROM now. Pieces of it will
1307 * be read when entries inside it are read. */
1308 return csr1212_parse_keyval(kv, cache);
1309 }
1310
1311 cache_index = kv->offset - cache->offset;
1312
1313 /* Now seach read portions of the cache to see if it is there. */
1314 for (cr = cache->filled_head; cr; cr = cr->next) {
1315 if (cache_index < cr->offset_start) {
Stefan Richter85511582005-11-07 06:31:45 -05001316 newcr = CSR1212_MALLOC(sizeof(*newcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (!newcr)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001318 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 newcr->offset_start = cache_index & ~(csr->max_rom - 1);
1321 newcr->offset_end = newcr->offset_start;
1322 newcr->next = cr;
1323 newcr->prev = cr->prev;
1324 cr->prev = newcr;
1325 cr = newcr;
1326 break;
1327 } else if ((cache_index >= cr->offset_start) &&
1328 (cache_index < cr->offset_end)) {
1329 kvi = (struct csr1212_keyval_img*)
1330 (&cache->data[bytes_to_quads(cache_index)]);
Stefan Richter7fb9add2007-03-11 22:49:05 +01001331 kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 break;
Stefan Richterc868ae22007-03-14 00:26:38 +01001333 } else if (cache_index == cr->offset_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 break;
Stefan Richterc868ae22007-03-14 00:26:38 +01001335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337
1338 if (!cr) {
1339 cr = cache->filled_tail;
Stefan Richter85511582005-11-07 06:31:45 -05001340 newcr = CSR1212_MALLOC(sizeof(*newcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (!newcr)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001342 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 newcr->offset_start = cache_index & ~(csr->max_rom - 1);
1345 newcr->offset_end = newcr->offset_start;
1346 newcr->prev = cr;
1347 newcr->next = cr->next;
1348 cr->next = newcr;
1349 cr = newcr;
1350 cache->filled_tail = newcr;
1351 }
1352
1353 while(!kvi || cr->offset_end < cache_index + kv_len) {
1354 cache_ptr = &cache->data[bytes_to_quads(cr->offset_end &
1355 ~(csr->max_rom - 1))];
1356
1357 addr = (CSR1212_CSR_ARCH_REG_SPACE_BASE + cache->offset +
1358 cr->offset_end) & ~(csr->max_rom - 1);
1359
1360 if (csr->ops->bus_read(csr, addr, csr->max_rom, cache_ptr,
1361 csr->private)) {
1362 if (csr->max_rom == 4)
1363 /* We've got problems! */
Stefan Richter7fb9add2007-03-11 22:49:05 +01001364 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /* Apperently the max_rom value was a lie, set it to
1367 * do quadlet reads and try again. */
1368 csr->max_rom = 4;
1369 continue;
1370 }
1371
1372 cr->offset_end += csr->max_rom - (cr->offset_end &
1373 (csr->max_rom - 1));
1374
1375 if (!kvi && (cr->offset_end > cache_index)) {
1376 kvi = (struct csr1212_keyval_img*)
1377 (&cache->data[bytes_to_quads(cache_index)]);
Stefan Richter7fb9add2007-03-11 22:49:05 +01001378 kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
1381 if ((kv_len + (kv->offset - cache->offset)) > cache->size) {
1382 /* The Leaf or Directory claims its length extends
1383 * beyond the ConfigROM image region and thus beyond the
1384 * end of our cache region. Therefore, we abort now
1385 * rather than seg faulting later. */
Stefan Richter7fb9add2007-03-11 22:49:05 +01001386 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388
1389 ncr = cr->next;
1390
1391 if (ncr && (cr->offset_end >= ncr->offset_start)) {
1392 /* consolidate region entries */
1393 ncr->offset_start = cr->offset_start;
1394
1395 if (cr->prev)
1396 cr->prev->next = cr->next;
1397 ncr->prev = cr->prev;
1398 if (cache->filled_head == cr)
1399 cache->filled_head = ncr;
1400 CSR1212_FREE(cr);
1401 cr = ncr;
1402 }
1403 }
1404
1405 return csr1212_parse_keyval(kv, cache);
1406}
1407
Stefan Richterc1a37f22007-03-14 00:20:53 +01001408struct csr1212_keyval *
1409csr1212_get_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
1410{
1411 if (!kv)
1412 return NULL;
1413 if (!kv->valid)
1414 if (csr1212_read_keyval(csr, kv) != CSR1212_SUCCESS)
1415 return NULL;
1416 return kv;
1417}
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419int csr1212_parse_csr(struct csr1212_csr *csr)
1420{
1421 static const int mr_map[] = { 4, 64, 1024, 0 };
1422 struct csr1212_dentry *dentry;
1423 int ret;
1424
Stefan Richter64ff7122007-03-11 22:50:13 +01001425 BUG_ON(!csr || !csr->ops || !csr->ops->bus_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 ret = csr1212_parse_bus_info_block(csr);
1428 if (ret != CSR1212_SUCCESS)
1429 return ret;
1430
Stefan Richterc868ae22007-03-14 00:26:38 +01001431 if (!csr->ops->get_max_rom) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 csr->max_rom = mr_map[0]; /* default value */
Stefan Richterc868ae22007-03-14 00:26:38 +01001433 } else {
Ben Collins1934b8b2005-07-09 20:01:23 -04001434 int i = csr->ops->get_max_rom(csr->bus_info_data,
1435 csr->private);
1436 if (i & ~0x3)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001437 return -EINVAL;
Ben Collins1934b8b2005-07-09 20:01:23 -04001438 csr->max_rom = mr_map[i];
1439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 csr->cache_head->layout_head = csr->root_kv;
1442 csr->cache_head->layout_tail = csr->root_kv;
1443
1444 csr->root_kv->offset = (CSR1212_CONFIG_ROM_SPACE_BASE & 0xffff) +
1445 csr->bus_info_len;
1446
1447 csr->root_kv->valid = 0;
1448 csr->root_kv->next = csr->root_kv;
1449 csr->root_kv->prev = csr->root_kv;
Stefan Richterc1a37f22007-03-14 00:20:53 +01001450 ret = csr1212_read_keyval(csr, csr->root_kv);
Jody McIntyre5303a982005-11-22 12:17:11 -05001451 if (ret != CSR1212_SUCCESS)
1452 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 /* Scan through the Root directory finding all extended ROM regions
1455 * and make cache regions for them */
1456 for (dentry = csr->root_kv->value.directory.dentries_head;
1457 dentry; dentry = dentry->next) {
Jody McIntyrea96074e2005-11-22 12:17:14 -05001458 if (dentry->kv->key.id == CSR1212_KV_ID_EXTENDED_ROM &&
1459 !dentry->kv->valid) {
Stefan Richterc1a37f22007-03-14 00:20:53 +01001460 ret = csr1212_read_keyval(csr, dentry->kv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (ret != CSR1212_SUCCESS)
1462 return ret;
1463 }
1464 }
1465
1466 return CSR1212_SUCCESS;
1467}