blob: 3c044fc52352a0ae341d6c25b577cd3ef2186dc8 [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>
37#include <linux/string.h>
Stefan Richter64ff7122007-03-11 22:50:13 +010038#include <asm/bug.h>
Stefan Richter7fb9add2007-03-11 22:49:05 +010039#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
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)
Stefan Richter982610b2007-03-11 22:49:34 +010049static const u8 csr1212_key_id_type_map[0x30] = {
Andrea Guzzo0749aaa2006-12-08 00:53:24 +010050 __C, /* used by Apple iSight */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 __D | __L, /* Descriptor */
52 __I | __D | __L, /* Bus_Dependent_Info */
53 __I | __D | __L, /* Vendor */
54 __I, /* Hardware_Version */
55 0, 0, /* Reserved */
Andrea Guzzo0749aaa2006-12-08 00:53:24 +010056 __D | __L | __I, /* Module */
57 __I, 0, 0, 0, /* used by Apple iSight, Reserved */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 __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
Stefan Richter982610b2007-03-11 22:49:34 +010085#define quads_to_bytes(_q) ((_q) * sizeof(u32))
86#define bytes_to_quads(_b) (((_b) + sizeof(u32) - 1) / sizeof(u32))
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Stefan Richter6c88e472007-03-11 22:47:34 +010088static void free_keyval(struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
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
Stefan Richter982610b2007-03-11 22:49:34 +010097static u16 csr1212_crc16(const u32 *buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 int shift;
Stefan Richter982610b2007-03-11 22:49:34 +0100100 u32 data;
101 u16 sum, crc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 for (; length; length--) {
Stefan Richter7fb9add2007-03-11 22:49:05 +0100104 data = be32_to_cpu(*buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 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
Stefan Richter7fb9add2007-03-11 22:49:05 +0100113 return cpu_to_be16(crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
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. */
Stefan Richter982610b2007-03-11 22:49:34 +0100120static u16 csr1212_msft_crc16(const u32 *buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 int shift;
Stefan Richter982610b2007-03-11 22:49:34 +0100123 u32 data;
124 u16 sum, crc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 for (; length; length--) {
Stefan Richter7fb9add2007-03-11 22:49:05 +0100127 data = le32_to_cpu(*buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 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
Stefan Richter7fb9add2007-03-11 22:49:05 +0100136 return cpu_to_be16(crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138#endif
139
Stefan Richter6c88e472007-03-11 22:47:34 +0100140static struct csr1212_dentry *
141csr1212_find_keyval(struct csr1212_keyval *dir, struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 struct csr1212_dentry *pos;
144
145 for (pos = dir->value.directory.dentries_head;
Stefan Richterc868ae22007-03-14 00:26:38 +0100146 pos != NULL; pos = pos->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (pos->kv == kv)
148 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return NULL;
150}
151
Stefan Richter6c88e472007-03-11 22:47:34 +0100152static struct csr1212_keyval *
Stefan Richter982610b2007-03-11 22:49:34 +0100153csr1212_find_keyval_offset(struct csr1212_keyval *kv_list, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct csr1212_keyval *kv;
156
Stefan Richterc868ae22007-03-14 00:26:38 +0100157 for (kv = kv_list->next; kv && (kv != kv_list); kv = kv->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (kv->offset == offset)
159 return kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return NULL;
161}
162
163
164/* Creation Routines */
Stefan Richter6c88e472007-03-11 22:47:34 +0100165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166struct 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203void csr1212_init_local_csr(struct csr1212_csr *csr,
Stefan Richter982610b2007-03-11 22:49:34 +0100204 const u32 *bus_info_data, int max_rom)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 static const int mr_map[] = { 4, 64, 1024, 0 };
207
Ben Collins1934b8b2005-07-09 20:01:23 -0400208 BUG_ON(max_rom & ~0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 csr->max_rom = mr_map[max_rom];
210 memcpy(csr->bus_info_data, bus_info_data, csr->bus_info_len);
211}
212
Stefan Richter982610b2007-03-11 22:49:34 +0100213static struct csr1212_keyval *csr1212_new_keyval(u8 type, u8 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
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
Stefan Richter982610b2007-03-11 22:49:34 +0100237struct csr1212_keyval *csr1212_new_immediate(u8 key, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Stefan Richterc868ae22007-03-14 00:26:38 +0100239 struct csr1212_keyval *kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Stefan Richterc868ae22007-03-14 00:26:38 +0100241 kv = csr1212_new_keyval(CSR1212_KV_TYPE_IMMEDIATE, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (!kv)
243 return NULL;
244
245 kv->value.immediate = value;
246 kv->valid = 1;
247 return kv;
248}
249
Stefan Richter6c88e472007-03-11 22:47:34 +0100250static struct csr1212_keyval *
Stefan Richter982610b2007-03-11 22:49:34 +0100251csr1212_new_leaf(u8 key, const void *data, size_t data_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Stefan Richterc868ae22007-03-14 00:26:38 +0100253 struct csr1212_keyval *kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Stefan Richterc868ae22007-03-14 00:26:38 +0100255 kv = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 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
Stefan Richter6c88e472007-03-11 22:47:34 +0100279static struct csr1212_keyval *
Stefan Richter982610b2007-03-11 22:49:34 +0100280csr1212_new_csr_offset(u8 key, u32 csr_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Stefan Richterc868ae22007-03-14 00:26:38 +0100282 struct csr1212_keyval *kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Stefan Richterc868ae22007-03-14 00:26:38 +0100284 kv = csr1212_new_keyval(CSR1212_KV_TYPE_CSR_OFFSET, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 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
Stefan Richter982610b2007-03-11 22:49:34 +0100295struct csr1212_keyval *csr1212_new_directory(u8 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Stefan Richterc868ae22007-03-14 00:26:38 +0100297 struct csr1212_keyval *kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Stefan Richterc868ae22007-03-14 00:26:38 +0100299 kv = csr1212_new_keyval(CSR1212_KV_TYPE_DIRECTORY, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 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
Stefan Richter64ff7122007-03-11 22:50:13 +0100311void csr1212_associate_keyval(struct csr1212_keyval *kv,
312 struct csr1212_keyval *associate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Stefan Richter64ff7122007-03-11 22:50:13 +0100314 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 &&
Stefan Richterc868ae22007-03-14 00:26:38 +0100321 associate->key.id != CSR1212_KV_ID_EXTENDED_KEY) ||
Stefan Richter64ff7122007-03-11 22:50:13 +0100322 (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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (kv->associate)
330 csr1212_release_keyval(kv->associate);
331
332 associate->refcnt++;
333 kv->associate = associate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
337 struct csr1212_keyval *kv)
338{
339 struct csr1212_dentry *dentry;
340
Stefan Richter64ff7122007-03-11 22:50:13 +0100341 BUG_ON(!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 dentry = CSR1212_MALLOC(sizeof(*dentry));
344 if (!dentry)
Stefan Richter7fb9add2007-03-11 22:49:05 +0100345 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
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
Stefan Richter6c88e472007-03-11 22:47:34 +0100364#define CSR1212_DESCRIPTOR_LEAF_DATA(kv) \
365 (&((kv)->value.leaf.data[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Stefan Richter6c88e472007-03-11 22:47:34 +0100367#define CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, type) \
368 ((kv)->value.leaf.data[0] = \
Stefan Richter7fb9add2007-03-11 22:49:05 +0100369 cpu_to_be32(CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) | \
370 ((type) << CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)))
Stefan Richter6c88e472007-03-11 22:47:34 +0100371#define CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, spec_id) \
372 ((kv)->value.leaf.data[0] = \
Stefan Richter7fb9add2007-03-11 22:49:05 +0100373 cpu_to_be32((CSR1212_DESCRIPTOR_LEAF_TYPE(kv) << \
374 CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT) | \
375 ((spec_id) & CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Stefan Richter6c88e472007-03-11 22:47:34 +0100377static struct csr1212_keyval *
Stefan Richter982610b2007-03-11 22:49:34 +0100378csr1212_new_descriptor_leaf(u8 dtype, u32 specifier_id,
Stefan Richter6c88e472007-03-11 22:47:34 +0100379 const void *data, size_t data_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
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
Stefan Richtera1c62502007-03-14 00:27:18 +0100391 if (data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 memcpy(CSR1212_DESCRIPTOR_LEAF_DATA(kv), data, data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 return kv;
395}
396
Stefan Richtera1c62502007-03-14 00:27:18 +0100397/* Check if string conforms to minimal ASCII as per IEEE 1212 clause 7.4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static int csr1212_check_minimal_ascii(const char *s)
399{
400 static const char minimal_ascii_table[] = {
Stefan Richtera1c62502007-03-14 00:27:18 +0100401 /* 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, --, --, --, --, --, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 };
Stefan Richtera1c62502007-03-14 00:27:18 +0100419 int i, j;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 for (; *s; s++) {
Stefan Richtera1c62502007-03-14 00:27:18 +0100422 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
Stefan Richtera1c62502007-03-14 00:27:18 +0100432/* IEEE 1212 clause 7.5.4.1 textual descriptors (English, minimal ASCII) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s)
434{
Stefan Richtera1c62502007-03-14 00:27:18 +0100435 struct csr1212_keyval *kv;
436 u32 *text;
437 size_t str_len, quads;
438
439 if (!s || !*s || csr1212_check_minimal_ascii(s))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return NULL;
441
Stefan Richtera1c62502007-03-14 00:27:18 +0100442 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)
447 return NULL;
448
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458/* Destruction Routines */
459
460void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/* 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. */
Stefan Richterc1a37f22007-03-14 00:20:53 +0100492static void csr1212_destroy_keyval(struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
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) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100517 /* If the current entry is a directory, move all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * the entries to the destruction list. */
519 if (k->value.directory.dentries_head) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100520 tail->next =
521 k->value.directory.dentries_head;
522 k->value.directory.dentries_head->prev =
523 tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 tail = k->value.directory.dentries_tail;
525 }
526 }
527 free_keyval(k);
528 k = a;
529 }
530
531 head = head->next;
532 if (head) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100533 if (head->prev && head->prev != &dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 CSR1212_FREE(head->prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 head->prev = NULL;
Stefan Richterc868ae22007-03-14 00:26:38 +0100536 } else if (tail != &dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 CSR1212_FREE(tail);
Stefan Richterc868ae22007-03-14 00:26:38 +0100538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540}
541
Stefan Richterc1a37f22007-03-14 00:20:53 +0100542void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574/* CSR Image Creation */
575
576static int csr1212_append_new_cache(struct csr1212_csr *csr, size_t romsize)
577{
578 struct csr1212_csr_rom_cache *cache;
Stefan Richter982610b2007-03-11 22:49:34 +0100579 u64 csr_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Stefan Richter64ff7122007-03-11 22:50:13 +0100581 BUG_ON(!csr || !csr->ops || !csr->ops->allocate_addr_range ||
582 !csr->ops->release_addr || csr->max_rom < 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /* ROM size must be a multiple of csr->max_rom */
585 romsize = (romsize + (csr->max_rom - 1)) & ~(csr->max_rom - 1);
586
Stefan Richterc868ae22007-03-14 00:26:38 +0100587 csr_addr = csr->ops->allocate_addr_range(romsize, csr->max_rom,
588 csr->private);
589 if (csr_addr == CSR1212_INVALID_ADDR_SPACE)
Stefan Richter7fb9add2007-03-11 22:49:05 +0100590 return -ENOMEM;
Stefan Richterc868ae22007-03-14 00:26:38 +0100591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (csr_addr < CSR1212_REGISTER_SPACE_BASE) {
593 /* Invalid address returned from allocate_addr_range(). */
594 csr->ops->release_addr(csr_addr, csr->private);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100595 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597
Stefan Richterc868ae22007-03-14 00:26:38 +0100598 cache = csr1212_rom_cache_malloc(csr_addr - CSR1212_REGISTER_SPACE_BASE,
599 romsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (!cache) {
601 csr->ops->release_addr(csr_addr, csr->private);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100602 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
Stefan Richterc868ae22007-03-14 00:26:38 +0100605 cache->ext_rom = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF,
606 CSR1212_KV_ID_EXTENDED_ROM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!cache->ext_rom) {
608 csr->ops->release_addr(csr_addr, csr->private);
609 CSR1212_FREE(cache);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100610 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
Stefan Richterc868ae22007-03-14 00:26:38 +0100613 if (csr1212_attach_keyval_to_directory(csr->root_kv, cache->ext_rom) !=
614 CSR1212_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 csr1212_release_keyval(cache->ext_rom);
616 csr->ops->release_addr(csr_addr, csr->private);
617 CSR1212_FREE(cache);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100618 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
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
Stefan Richter6c88e472007-03-11 22:47:34 +0100631static void csr1212_remove_cache(struct csr1212_csr *csr,
632 struct csr1212_csr_rom_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
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) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100645 csr1212_detach_keyval_from_directory(csr->root_kv,
646 cache->ext_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 csr1212_release_keyval(cache->ext_rom);
648 }
649
650 CSR1212_FREE(cache);
651}
652
653static 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 */
Stefan Richterc868ae22007-03-14 00:26:38 +0100666 if (dkv->key.id ==
667 CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
668 if (last_extkey_spec == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 last_extkey_spec = dkv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100670 else if (dkv->value.immediate !=
671 last_extkey_spec->value.immediate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 last_extkey_spec = dkv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100673 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /* Special Case: Extended Key */
676 } else if (dkv->key.id == CSR1212_KV_ID_EXTENDED_KEY) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100677 if (last_extkey == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 last_extkey = dkv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100679 else if (dkv->value.immediate !=
680 last_extkey->value.immediate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 last_extkey = dkv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100682 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
686 num_entries += 1;
687
Stefan Richterc868ae22007-03-14 00:26:38 +0100688 switch (dkv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 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;
Stefan Richterc868ae22007-03-14 00:26:38 +0100706 /* Don't add Extended ROM leafs in the
707 * layout list, they are handled
708 * differently. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 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
Stefan Richter6c88e472007-03-11 22:47:34 +0100724static size_t csr1212_generate_layout_order(struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct csr1212_keyval *ltail = kv;
727 size_t agg_size = 0;
728
Stefan Richterc868ae22007-03-14 00:26:38 +0100729 while (kv) {
730 switch (kv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 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:
Stefan Richterc868ae22007-03-14 00:26:38 +0100737 kv->value.directory.len =
738 csr1212_generate_layout_subdir(kv, &ltail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* 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
Stefan Richter6c88e472007-03-11 22:47:34 +0100748static struct csr1212_keyval *
749csr1212_generate_positions(struct csr1212_csr_rom_cache *cache,
750 struct csr1212_keyval *start_kv, int start_pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
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
Stefan Richterc868ae22007-03-14 00:26:38 +0100759 while (kv && pos < cache->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* Special case: Extended ROM leafs */
Stefan Richterc868ae22007-03-14 00:26:38 +0100761 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 kv->offset = cache->offset + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Stefan Richterc868ae22007-03-14 00:26:38 +0100764 switch (kv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 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 */
Stefan Richterc94ccf92007-03-14 00:27:46 +0100775 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 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;
Stefan Richterc868ae22007-03-14 00:26:38 +0100789 cache->len = okv->offset - cache->offset + quads_to_bytes(okv_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 return kv;
792}
793
Stefan Richter6c88e472007-03-11 22:47:34 +0100794#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
799static void
Stefan Richter982610b2007-03-11 22:49:34 +0100800csr1212_generate_tree_subdir(struct csr1212_keyval *dir, u32 *data_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
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
Stefan Richterc868ae22007-03-14 00:26:38 +0100807 for (dentry = dir->value.directory.dentries_head;
808 dentry;
809 dentry = dentry->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 struct csr1212_keyval *a;
811
812 for (a = dentry->kv; a; a = a->associate) {
Stefan Richter982610b2007-03-11 22:49:34 +0100813 u32 value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* Special Case: Extended Key Specifier_ID */
Stefan Richterc868ae22007-03-14 00:26:38 +0100816 if (a->key.id ==
817 CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
818 if (last_extkey_spec == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 last_extkey_spec = a;
Stefan Richterc868ae22007-03-14 00:26:38 +0100820 else if (a->value.immediate !=
821 last_extkey_spec->value.immediate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 last_extkey_spec = a;
Stefan Richterc868ae22007-03-14 00:26:38 +0100823 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 continue;
Stefan Richterc868ae22007-03-14 00:26:38 +0100825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* Special Case: Extended Key */
827 } else if (a->key.id == CSR1212_KV_ID_EXTENDED_KEY) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100828 if (last_extkey == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 last_extkey = a;
Stefan Richterc868ae22007-03-14 00:26:38 +0100830 else if (a->value.immediate !=
831 last_extkey->value.immediate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 last_extkey = a;
Stefan Richterc868ae22007-03-14 00:26:38 +0100833 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
Stefan Richterc868ae22007-03-14 00:26:38 +0100837 switch (a->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 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 */
Stefan Richterc94ccf92007-03-14 00:27:46 +0100856 WARN_ON(1);
857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859
Stefan Richterc868ae22007-03-14 00:26:38 +0100860 value |= (a->key.id & CSR1212_KV_KEY_ID_MASK) <<
861 CSR1212_KV_KEY_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 value |= (a->key.type & CSR1212_KV_KEY_TYPE_MASK) <<
Stefan Richterc868ae22007-03-14 00:26:38 +0100863 (CSR1212_KV_KEY_SHIFT +
864 CSR1212_KV_KEY_TYPE_SHIFT);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100865 data_buffer[index] = cpu_to_be32(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 index++;
867 }
868 }
869}
870
Stefan Richter6c88e472007-03-11 22:47:34 +0100871struct csr1212_keyval_img {
Stefan Richter982610b2007-03-11 22:49:34 +0100872 u16 length;
873 u16 crc;
Stefan Richter6c88e472007-03-11 22:47:34 +0100874
875 /* Must be last */
Stefan Richter982610b2007-03-11 22:49:34 +0100876 u32 data[0]; /* older gcc can't handle [] which is standard */
Stefan Richter6c88e472007-03-11 22:47:34 +0100877};
878
879static void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 struct csr1212_keyval *kv, *nkv;
882 struct csr1212_keyval_img *kvi;
883
Stefan Richterc868ae22007-03-14 00:26:38 +0100884 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 default:
891 case CSR1212_KV_TYPE_IMMEDIATE:
892 case CSR1212_KV_TYPE_CSR_OFFSET:
893 /* Should never get here */
Stefan Richterc94ccf92007-03-14 00:27:46 +0100894 WARN_ON(1);
895 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
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
Stefan Richter7fb9add2007-03-11 22:49:05 +0100904 kvi->length = cpu_to_be16(kv->value.leaf.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 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
Stefan Richter7fb9add2007-03-11 22:49:05 +0100911 kvi->length = cpu_to_be16(kv->value.directory.len);
Stefan Richterc868ae22007-03-14 00:26:38 +0100912 kvi->crc = csr1212_crc16(kvi->data,
913 kv->value.directory.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 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
Stefan Richterfd2f3bd2007-03-11 22:51:24 +0100927/* 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))
Stefan Richter6c88e472007-03-11 22:47:34 +0100930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931int 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
Stefan Richter64ff7122007-03-11 22:50:13 +0100940 BUG_ON(!csr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
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
Stefan Richterc868ae22007-03-14 00:26:38 +0100957 for (kv = csr->root_kv, cache = csr->cache_head;
958 kv;
959 cache = cache->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 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 -
Stefan Richter982610b2007-03-11 22:49:34 +0100965 (2 * sizeof(u32))) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 /* Add additional cache regions, extras will be
968 * removed later */
969 for (; est_c; est_c--) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100970 ret = csr1212_append_new_cache(csr,
971 CSR1212_EXTENDED_ROM_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 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;
Stefan Richter982610b2007-03-11 22:49:34 +0100983 init_offset = sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
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. */
Stefan Richterc868ae22007-03-14 00:26:38 +0100996 for (cache = csr->cache_tail; cache; cache = cache->prev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /* 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. */
Stefan Richter64ff7122007-03-11 22:50:13 +01001003 BUG_ON(csr->max_rom < 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 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 */
Stefan Richter982610b2007-03-11 22:49:34 +01001012 leaf_size -= sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
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;
Stefan Richter982610b2007-03-11 22:49:34 +01001030 u16 len = bytes_to_quads(cache->len) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Stefan Richter7fb9add2007-03-11 22:49:05 +01001032 kvi->length = cpu_to_be16(len);
1033 kvi->crc = csr1212_crc16(kvi->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035 }
1036
1037 return CSR1212_SUCCESS;
1038}
1039
Stefan Richter982610b2007-03-11 22:49:34 +01001040int csr1212_read(struct csr1212_csr *csr, u32 offset, void *buffer, u32 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 struct csr1212_csr_rom_cache *cache;
1043
Stefan Richterc868ae22007-03-14 00:26:38 +01001044 for (cache = csr->cache_head; cache; cache = cache->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (offset >= cache->offset &&
1046 (offset + len) <= (cache->offset + cache->size)) {
Stefan Richterc868ae22007-03-14 00:26:38 +01001047 memcpy(buffer, &cache->data[
1048 bytes_to_quads(offset - cache->offset)],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 len);
1050 return CSR1212_SUCCESS;
1051 }
Stefan Richterc868ae22007-03-14 00:26:38 +01001052
Stefan Richter7fb9add2007-03-11 22:49:05 +01001053 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057/* Parse a chunk of data as a Config ROM */
1058
1059static 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. */
Stefan Richter982610b2007-03-11 22:49:34 +01001071 for (i = 0; i < csr->bus_info_len; i += sizeof(u32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
Stefan Richterc868ae22007-03-14 00:26:38 +01001073 sizeof(u32), &csr->cache_head->data[bytes_to_quads(i)],
1074 csr->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (ret != CSR1212_SUCCESS)
1076 return ret;
Stefan Richterb2051f82007-01-03 19:32:13 +01001077
1078 /* check ROM header's info_length */
1079 if (i == 0 &&
Stefan Richter7fb9add2007-03-11 22:49:05 +01001080 be32_to_cpu(csr->cache_head->data[0]) >> 24 !=
Stefan Richterb2051f82007-01-03 19:32:13 +01001081 bytes_to_quads(csr->bus_info_len) - 1)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001082 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
1085 bi = (struct csr1212_bus_info_block_img*)csr->cache_head->data;
1086 csr->crc_len = quads_to_bytes(bi->crc_length);
1087
Stefan Richterc868ae22007-03-14 00:26:38 +01001088 /* 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. */
Stefan Richter982610b2007-03-11 22:49:34 +01001091 for (i = csr->bus_info_len; i <= csr->crc_len; i += sizeof(u32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
Stefan Richterc868ae22007-03-14 00:26:38 +01001093 sizeof(u32), &csr->cache_head->data[bytes_to_quads(i)],
1094 csr->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (ret != CSR1212_SUCCESS)
1096 return ret;
1097 }
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099#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))
Stefan Richter7fb9add2007-03-11 22:49:05 +01001104 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105#endif
1106
Stefan Richter85511582005-11-07 06:31:45 -05001107 cr = CSR1212_MALLOC(sizeof(*cr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (!cr)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001109 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
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
Stefan Richter7fb9add2007-03-11 22:49:05 +01001122#define CSR1212_KV_KEY(q) (be32_to_cpu(q) >> CSR1212_KV_KEY_SHIFT)
Stefan Richter6c88e472007-03-11 22:47:34 +01001123#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
Stefan Richter7fb9add2007-03-11 22:49:05 +01001126#define CSR1212_KV_VAL(q) (be32_to_cpu(q) & CSR1212_KV_VAL_MASK)
Stefan Richter6c88e472007-03-11 22:47:34 +01001127
Stefan Richterc868ae22007-03-14 00:26:38 +01001128static int
1129csr1212_parse_dir_entry(struct csr1212_keyval *dir, u32 ki, u32 kv_pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 int ret = CSR1212_SUCCESS;
1132 struct csr1212_keyval *k = NULL;
Stefan Richter982610b2007-03-11 22:49:34 +01001133 u32 offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Stefan Richterc868ae22007-03-14 00:26:38 +01001135 switch (CSR1212_KV_KEY_TYPE(ki)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 case CSR1212_KV_TYPE_IMMEDIATE:
1137 k = csr1212_new_immediate(CSR1212_KV_KEY_ID(ki),
1138 CSR1212_KV_VAL(ki));
1139 if (!k) {
Stefan Richter7fb9add2007-03-11 22:49:05 +01001140 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 goto fail;
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) {
Stefan Richter7fb9add2007-03-11 22:49:05 +01001151 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 goto fail;
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. */
Stefan Richter7fb9add2007-03-11 22:49:05 +01001164 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 goto fail;
1166 }
1167
1168 k = csr1212_find_keyval_offset(dir, offset);
1169
1170 if (k)
1171 break; /* Found it. */
1172
Stefan Richterc868ae22007-03-14 00:26:38 +01001173 if (CSR1212_KV_KEY_TYPE(ki) == CSR1212_KV_TYPE_DIRECTORY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 k = csr1212_new_directory(CSR1212_KV_KEY_ID(ki));
Stefan Richterc868ae22007-03-14 00:26:38 +01001175 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 k = csr1212_new_leaf(CSR1212_KV_KEY_ID(ki), NULL, 0);
Stefan Richterc868ae22007-03-14 00:26:38 +01001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (!k) {
Stefan Richter7fb9add2007-03-11 22:49:05 +01001179 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto fail;
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);
1192
1193fail:
Stefan Richter6c88e472007-03-11 22:47:34 +01001194 if (ret != CSR1212_SUCCESS && k != NULL)
1195 free_keyval(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return ret;
1197}
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199int csr1212_parse_keyval(struct csr1212_keyval *kv,
1200 struct csr1212_csr_rom_cache *cache)
1201{
1202 struct csr1212_keyval_img *kvi;
1203 int i;
1204 int ret = CSR1212_SUCCESS;
1205 int kvi_len;
1206
Stefan Richterc868ae22007-03-14 00:26:38 +01001207 kvi = (struct csr1212_keyval_img*)
1208 &cache->data[bytes_to_quads(kv->offset - cache->offset)];
Stefan Richter7fb9add2007-03-11 22:49:05 +01001209 kvi_len = be16_to_cpu(kvi->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211#if 0
1212 /* Apparently there are too many differnt wrong implementations of the
1213 * CRC algorithm that verifying them is moot. */
1214 if ((csr1212_crc16(kvi->data, kvi_len) != kvi->crc) &&
1215 (csr1212_msft_crc16(kvi->data, kvi_len) != kvi->crc)) {
Stefan Richter7fb9add2007-03-11 22:49:05 +01001216 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 goto fail;
1218 }
1219#endif
1220
Stefan Richterc868ae22007-03-14 00:26:38 +01001221 switch (kv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 case CSR1212_KV_TYPE_DIRECTORY:
1223 for (i = 0; i < kvi_len; i++) {
Stefan Richter982610b2007-03-11 22:49:34 +01001224 u32 ki = kvi->data[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 /* Some devices put null entries in their unit
1227 * directories. If we come across such an entry,
1228 * then skip it. */
1229 if (ki == 0x0)
1230 continue;
1231 ret = csr1212_parse_dir_entry(kv, ki,
Stefan Richterc868ae22007-03-14 00:26:38 +01001232 kv->offset + quads_to_bytes(i + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234 kv->value.directory.len = kvi_len;
1235 break;
1236
1237 case CSR1212_KV_TYPE_LEAF:
1238 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) {
Stefan Richterc868ae22007-03-14 00:26:38 +01001239 size_t size = quads_to_bytes(kvi_len);
1240
1241 kv->value.leaf.data = CSR1212_MALLOC(size);
Stefan Richter85511582005-11-07 06:31:45 -05001242 if (!kv->value.leaf.data) {
Stefan Richter7fb9add2007-03-11 22:49:05 +01001243 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 goto fail;
1245 }
1246
1247 kv->value.leaf.len = kvi_len;
Stefan Richterc868ae22007-03-14 00:26:38 +01001248 memcpy(kv->value.leaf.data, kvi->data, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250 break;
1251 }
1252
1253 kv->valid = 1;
1254
1255fail:
1256 return ret;
1257}
1258
Stefan Richterc1a37f22007-03-14 00:20:53 +01001259static int
1260csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 struct csr1212_cache_region *cr, *ncr, *newcr = NULL;
1263 struct csr1212_keyval_img *kvi = NULL;
1264 struct csr1212_csr_rom_cache *cache;
1265 int cache_index;
Stefan Richter982610b2007-03-11 22:49:34 +01001266 u64 addr;
1267 u32 *cache_ptr;
1268 u16 kv_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Stefan Richter64ff7122007-03-11 22:50:13 +01001270 BUG_ON(!csr || !kv || csr->max_rom < 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 /* First find which cache the data should be in (or go in if not read
1273 * yet). */
Stefan Richterc868ae22007-03-14 00:26:38 +01001274 for (cache = csr->cache_head; cache; cache = cache->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (kv->offset >= cache->offset &&
1276 kv->offset < (cache->offset + cache->size))
1277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 if (!cache) {
Stefan Richter982610b2007-03-11 22:49:34 +01001280 u32 q, cache_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 /* Only create a new cache for Extended ROM leaves. */
1283 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (csr->ops->bus_read(csr,
1287 CSR1212_REGISTER_SPACE_BASE + kv->offset,
Stefan Richterc868ae22007-03-14 00:26:38 +01001288 sizeof(u32), &q, csr->private))
Stefan Richter7fb9add2007-03-11 22:49:05 +01001289 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Stefan Richter7fb9add2007-03-11 22:49:05 +01001291 kv->value.leaf.len = be32_to_cpu(q) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 cache_size = (quads_to_bytes(kv->value.leaf.len + 1) +
1294 (csr->max_rom - 1)) & ~(csr->max_rom - 1);
1295
1296 cache = csr1212_rom_cache_malloc(kv->offset, cache_size);
1297 if (!cache)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001298 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 kv->value.leaf.data = &cache->data[1];
1301 csr->cache_tail->next = cache;
1302 cache->prev = csr->cache_tail;
1303 cache->next = NULL;
1304 csr->cache_tail = cache;
1305 cache->filled_head =
Stefan Richter85511582005-11-07 06:31:45 -05001306 CSR1212_MALLOC(sizeof(*cache->filled_head));
Stefan Richterc868ae22007-03-14 00:26:38 +01001307 if (!cache->filled_head)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001308 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 cache->filled_head->offset_start = 0;
Stefan Richter982610b2007-03-11 22:49:34 +01001311 cache->filled_head->offset_end = sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 cache->filled_tail = cache->filled_head;
1313 cache->filled_head->next = NULL;
1314 cache->filled_head->prev = NULL;
1315 cache->data[0] = q;
1316
1317 /* Don't read the entire extended ROM now. Pieces of it will
1318 * be read when entries inside it are read. */
1319 return csr1212_parse_keyval(kv, cache);
1320 }
1321
1322 cache_index = kv->offset - cache->offset;
1323
1324 /* Now seach read portions of the cache to see if it is there. */
1325 for (cr = cache->filled_head; cr; cr = cr->next) {
1326 if (cache_index < cr->offset_start) {
Stefan Richter85511582005-11-07 06:31:45 -05001327 newcr = CSR1212_MALLOC(sizeof(*newcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (!newcr)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001329 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 newcr->offset_start = cache_index & ~(csr->max_rom - 1);
1332 newcr->offset_end = newcr->offset_start;
1333 newcr->next = cr;
1334 newcr->prev = cr->prev;
1335 cr->prev = newcr;
1336 cr = newcr;
1337 break;
1338 } else if ((cache_index >= cr->offset_start) &&
1339 (cache_index < cr->offset_end)) {
1340 kvi = (struct csr1212_keyval_img*)
1341 (&cache->data[bytes_to_quads(cache_index)]);
Stefan Richter7fb9add2007-03-11 22:49:05 +01001342 kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 break;
Stefan Richterc868ae22007-03-14 00:26:38 +01001344 } else if (cache_index == cr->offset_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 break;
Stefan Richterc868ae22007-03-14 00:26:38 +01001346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348
1349 if (!cr) {
1350 cr = cache->filled_tail;
Stefan Richter85511582005-11-07 06:31:45 -05001351 newcr = CSR1212_MALLOC(sizeof(*newcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (!newcr)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001353 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 newcr->offset_start = cache_index & ~(csr->max_rom - 1);
1356 newcr->offset_end = newcr->offset_start;
1357 newcr->prev = cr;
1358 newcr->next = cr->next;
1359 cr->next = newcr;
1360 cr = newcr;
1361 cache->filled_tail = newcr;
1362 }
1363
1364 while(!kvi || cr->offset_end < cache_index + kv_len) {
1365 cache_ptr = &cache->data[bytes_to_quads(cr->offset_end &
1366 ~(csr->max_rom - 1))];
1367
1368 addr = (CSR1212_CSR_ARCH_REG_SPACE_BASE + cache->offset +
1369 cr->offset_end) & ~(csr->max_rom - 1);
1370
1371 if (csr->ops->bus_read(csr, addr, csr->max_rom, cache_ptr,
1372 csr->private)) {
1373 if (csr->max_rom == 4)
1374 /* We've got problems! */
Stefan Richter7fb9add2007-03-11 22:49:05 +01001375 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /* Apperently the max_rom value was a lie, set it to
1378 * do quadlet reads and try again. */
1379 csr->max_rom = 4;
1380 continue;
1381 }
1382
1383 cr->offset_end += csr->max_rom - (cr->offset_end &
1384 (csr->max_rom - 1));
1385
1386 if (!kvi && (cr->offset_end > cache_index)) {
1387 kvi = (struct csr1212_keyval_img*)
1388 (&cache->data[bytes_to_quads(cache_index)]);
Stefan Richter7fb9add2007-03-11 22:49:05 +01001389 kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
1392 if ((kv_len + (kv->offset - cache->offset)) > cache->size) {
1393 /* The Leaf or Directory claims its length extends
1394 * beyond the ConfigROM image region and thus beyond the
1395 * end of our cache region. Therefore, we abort now
1396 * rather than seg faulting later. */
Stefan Richter7fb9add2007-03-11 22:49:05 +01001397 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
1400 ncr = cr->next;
1401
1402 if (ncr && (cr->offset_end >= ncr->offset_start)) {
1403 /* consolidate region entries */
1404 ncr->offset_start = cr->offset_start;
1405
1406 if (cr->prev)
1407 cr->prev->next = cr->next;
1408 ncr->prev = cr->prev;
1409 if (cache->filled_head == cr)
1410 cache->filled_head = ncr;
1411 CSR1212_FREE(cr);
1412 cr = ncr;
1413 }
1414 }
1415
1416 return csr1212_parse_keyval(kv, cache);
1417}
1418
Stefan Richterc1a37f22007-03-14 00:20:53 +01001419struct csr1212_keyval *
1420csr1212_get_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
1421{
1422 if (!kv)
1423 return NULL;
1424 if (!kv->valid)
1425 if (csr1212_read_keyval(csr, kv) != CSR1212_SUCCESS)
1426 return NULL;
1427 return kv;
1428}
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430int csr1212_parse_csr(struct csr1212_csr *csr)
1431{
1432 static const int mr_map[] = { 4, 64, 1024, 0 };
1433 struct csr1212_dentry *dentry;
1434 int ret;
1435
Stefan Richter64ff7122007-03-11 22:50:13 +01001436 BUG_ON(!csr || !csr->ops || !csr->ops->bus_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 ret = csr1212_parse_bus_info_block(csr);
1439 if (ret != CSR1212_SUCCESS)
1440 return ret;
1441
Stefan Richterc868ae22007-03-14 00:26:38 +01001442 if (!csr->ops->get_max_rom) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 csr->max_rom = mr_map[0]; /* default value */
Stefan Richterc868ae22007-03-14 00:26:38 +01001444 } else {
Ben Collins1934b8b2005-07-09 20:01:23 -04001445 int i = csr->ops->get_max_rom(csr->bus_info_data,
1446 csr->private);
1447 if (i & ~0x3)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001448 return -EINVAL;
Ben Collins1934b8b2005-07-09 20:01:23 -04001449 csr->max_rom = mr_map[i];
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 csr->cache_head->layout_head = csr->root_kv;
1453 csr->cache_head->layout_tail = csr->root_kv;
1454
1455 csr->root_kv->offset = (CSR1212_CONFIG_ROM_SPACE_BASE & 0xffff) +
1456 csr->bus_info_len;
1457
1458 csr->root_kv->valid = 0;
1459 csr->root_kv->next = csr->root_kv;
1460 csr->root_kv->prev = csr->root_kv;
Stefan Richterc1a37f22007-03-14 00:20:53 +01001461 ret = csr1212_read_keyval(csr, csr->root_kv);
Jody McIntyre5303a982005-11-22 12:17:11 -05001462 if (ret != CSR1212_SUCCESS)
1463 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 /* Scan through the Root directory finding all extended ROM regions
1466 * and make cache regions for them */
1467 for (dentry = csr->root_kv->value.directory.dentries_head;
1468 dentry; dentry = dentry->next) {
Jody McIntyrea96074e2005-11-22 12:17:14 -05001469 if (dentry->kv->key.id == CSR1212_KV_ID_EXTENDED_ROM &&
1470 !dentry->kv->valid) {
Stefan Richterc1a37f22007-03-14 00:20:53 +01001471 ret = csr1212_read_keyval(csr, dentry->kv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (ret != CSR1212_SUCCESS)
1473 return ret;
1474 }
1475 }
1476
1477 return CSR1212_SUCCESS;
1478}