blob: a6dfeb0b3372898cb7c4a59dd2604c71981edd92 [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))
Julia Lawall68e2aa72008-08-02 17:13:09 +020087#define bytes_to_quads(_b) DIV_ROUND_UP(_b, 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
Stefan Richter17a19b72007-09-15 14:50:25 +0200221 atomic_set(&kv->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 kv->key.type = type;
223 kv->key.id = key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 kv->associate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 kv->next = NULL;
226 kv->prev = NULL;
227 kv->offset = 0;
228 kv->valid = 0;
229 return kv;
230}
231
Stefan Richter982610b2007-03-11 22:49:34 +0100232struct csr1212_keyval *csr1212_new_immediate(u8 key, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Stefan Richterc868ae22007-03-14 00:26:38 +0100234 struct csr1212_keyval *kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Stefan Richterc868ae22007-03-14 00:26:38 +0100236 kv = csr1212_new_keyval(CSR1212_KV_TYPE_IMMEDIATE, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (!kv)
238 return NULL;
239
240 kv->value.immediate = value;
241 kv->valid = 1;
242 return kv;
243}
244
Stefan Richter6c88e472007-03-11 22:47:34 +0100245static struct csr1212_keyval *
Stefan Richter982610b2007-03-11 22:49:34 +0100246csr1212_new_leaf(u8 key, const void *data, size_t data_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Stefan Richterc868ae22007-03-14 00:26:38 +0100248 struct csr1212_keyval *kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Stefan Richterc868ae22007-03-14 00:26:38 +0100250 kv = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (!kv)
252 return NULL;
253
254 if (data_len > 0) {
255 kv->value.leaf.data = CSR1212_MALLOC(data_len);
256 if (!kv->value.leaf.data) {
257 CSR1212_FREE(kv);
258 return NULL;
259 }
260
261 if (data)
262 memcpy(kv->value.leaf.data, data, data_len);
263 } else {
264 kv->value.leaf.data = NULL;
265 }
266
267 kv->value.leaf.len = bytes_to_quads(data_len);
268 kv->offset = 0;
269 kv->valid = 1;
270
271 return kv;
272}
273
Stefan Richter6c88e472007-03-11 22:47:34 +0100274static struct csr1212_keyval *
Stefan Richter982610b2007-03-11 22:49:34 +0100275csr1212_new_csr_offset(u8 key, u32 csr_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Stefan Richterc868ae22007-03-14 00:26:38 +0100277 struct csr1212_keyval *kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Stefan Richterc868ae22007-03-14 00:26:38 +0100279 kv = csr1212_new_keyval(CSR1212_KV_TYPE_CSR_OFFSET, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!kv)
281 return NULL;
282
283 kv->value.csr_offset = csr_offset;
284
285 kv->offset = 0;
286 kv->valid = 1;
287 return kv;
288}
289
Stefan Richter982610b2007-03-11 22:49:34 +0100290struct csr1212_keyval *csr1212_new_directory(u8 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Stefan Richterc868ae22007-03-14 00:26:38 +0100292 struct csr1212_keyval *kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Stefan Richterc868ae22007-03-14 00:26:38 +0100294 kv = csr1212_new_keyval(CSR1212_KV_TYPE_DIRECTORY, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (!kv)
296 return NULL;
297
298 kv->value.directory.len = 0;
299 kv->offset = 0;
300 kv->value.directory.dentries_head = NULL;
301 kv->value.directory.dentries_tail = NULL;
302 kv->valid = 1;
303 return kv;
304}
305
Stefan Richter64ff7122007-03-11 22:50:13 +0100306void csr1212_associate_keyval(struct csr1212_keyval *kv,
307 struct csr1212_keyval *associate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Stefan Richter64ff7122007-03-11 22:50:13 +0100309 BUG_ON(!kv || !associate || kv->key.id == CSR1212_KV_ID_DESCRIPTOR ||
310 (associate->key.id != CSR1212_KV_ID_DESCRIPTOR &&
311 associate->key.id != CSR1212_KV_ID_DEPENDENT_INFO &&
312 associate->key.id != CSR1212_KV_ID_EXTENDED_KEY &&
313 associate->key.id != CSR1212_KV_ID_EXTENDED_DATA &&
314 associate->key.id < 0x30) ||
315 (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID &&
Stefan Richterc868ae22007-03-14 00:26:38 +0100316 associate->key.id != CSR1212_KV_ID_EXTENDED_KEY) ||
Stefan Richter64ff7122007-03-11 22:50:13 +0100317 (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
318 associate->key.id != CSR1212_KV_ID_EXTENDED_DATA) ||
319 (associate->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
320 kv->key.id != CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) ||
321 (associate->key.id == CSR1212_KV_ID_EXTENDED_DATA &&
322 kv->key.id != CSR1212_KV_ID_EXTENDED_KEY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (kv->associate)
325 csr1212_release_keyval(kv->associate);
326
Stefan Richter17a19b72007-09-15 14:50:25 +0200327 csr1212_keep_keyval(associate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 kv->associate = associate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Stefan Richter17a19b72007-09-15 14:50:25 +0200331static int __csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
332 struct csr1212_keyval *kv,
333 bool keep_keyval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct csr1212_dentry *dentry;
336
Stefan Richter64ff7122007-03-11 22:50:13 +0100337 BUG_ON(!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 dentry = CSR1212_MALLOC(sizeof(*dentry));
340 if (!dentry)
Stefan Richter7fb9add2007-03-11 22:49:05 +0100341 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Stefan Richter17a19b72007-09-15 14:50:25 +0200343 if (keep_keyval)
344 csr1212_keep_keyval(kv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 dentry->kv = kv;
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 dentry->next = NULL;
348 dentry->prev = dir->value.directory.dentries_tail;
349
350 if (!dir->value.directory.dentries_head)
351 dir->value.directory.dentries_head = dentry;
352
353 if (dir->value.directory.dentries_tail)
354 dir->value.directory.dentries_tail->next = dentry;
355 dir->value.directory.dentries_tail = dentry;
356
357 return CSR1212_SUCCESS;
358}
359
Stefan Richter17a19b72007-09-15 14:50:25 +0200360int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
361 struct csr1212_keyval *kv)
362{
363 return __csr1212_attach_keyval_to_directory(dir, kv, true);
364}
365
Stefan Richter6c88e472007-03-11 22:47:34 +0100366#define CSR1212_DESCRIPTOR_LEAF_DATA(kv) \
367 (&((kv)->value.leaf.data[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Stefan Richter6c88e472007-03-11 22:47:34 +0100369#define CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, type) \
370 ((kv)->value.leaf.data[0] = \
Stefan Richter7fb9add2007-03-11 22:49:05 +0100371 cpu_to_be32(CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) | \
372 ((type) << CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)))
Stefan Richter6c88e472007-03-11 22:47:34 +0100373#define CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, spec_id) \
374 ((kv)->value.leaf.data[0] = \
Stefan Richter7fb9add2007-03-11 22:49:05 +0100375 cpu_to_be32((CSR1212_DESCRIPTOR_LEAF_TYPE(kv) << \
376 CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT) | \
377 ((spec_id) & CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Stefan Richter6c88e472007-03-11 22:47:34 +0100379static struct csr1212_keyval *
Stefan Richter982610b2007-03-11 22:49:34 +0100380csr1212_new_descriptor_leaf(u8 dtype, u32 specifier_id,
Stefan Richter6c88e472007-03-11 22:47:34 +0100381 const void *data, size_t data_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct csr1212_keyval *kv;
384
385 kv = csr1212_new_leaf(CSR1212_KV_ID_DESCRIPTOR, NULL,
386 data_len + CSR1212_DESCRIPTOR_LEAF_OVERHEAD);
387 if (!kv)
388 return NULL;
389
390 CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, dtype);
391 CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, specifier_id);
392
Stefan Richtera1c62502007-03-14 00:27:18 +0100393 if (data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 memcpy(CSR1212_DESCRIPTOR_LEAF_DATA(kv), data, data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 return kv;
397}
398
Stefan Richtera1c62502007-03-14 00:27:18 +0100399/* Check if string conforms to minimal ASCII as per IEEE 1212 clause 7.4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400static int csr1212_check_minimal_ascii(const char *s)
401{
402 static const char minimal_ascii_table[] = {
Stefan Richtera1c62502007-03-14 00:27:18 +0100403 /* 1 2 4 8 16 32 64 128 */
404 128, /* --, --, --, --, --, --, --, 07, */
405 4 + 16 + 32, /* --, --, 0a, --, 0C, 0D, --, --, */
406 0, /* --, --, --, --, --, --, --, --, */
407 0, /* --, --, --, --, --, --, --, --, */
408 255 - 8 - 16, /* 20, 21, 22, --, --, 25, 26, 27, */
409 255, /* 28, 29, 2a, 2b, 2c, 2d, 2e, 2f, */
410 255, /* 30, 31, 32, 33, 34, 35, 36, 37, */
411 255, /* 38, 39, 3a, 3b, 3c, 3d, 3e, 3f, */
412 255, /* 40, 41, 42, 43, 44, 45, 46, 47, */
413 255, /* 48, 49, 4a, 4b, 4c, 4d, 4e, 4f, */
414 255, /* 50, 51, 52, 53, 54, 55, 56, 57, */
415 1 + 2 + 4 + 128, /* 58, 59, 5a, --, --, --, --, 5f, */
416 255 - 1, /* --, 61, 62, 63, 64, 65, 66, 67, */
417 255, /* 68, 69, 6a, 6b, 6c, 6d, 6e, 6f, */
418 255, /* 70, 71, 72, 73, 74, 75, 76, 77, */
419 1 + 2 + 4, /* 78, 79, 7a, --, --, --, --, --, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 };
Stefan Richtera1c62502007-03-14 00:27:18 +0100421 int i, j;
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 for (; *s; s++) {
Stefan Richtera1c62502007-03-14 00:27:18 +0100424 i = *s >> 3; /* i = *s / 8; */
425 j = 1 << (*s & 3); /* j = 1 << (*s % 8); */
426
427 if (i >= ARRAY_SIZE(minimal_ascii_table) ||
428 !(minimal_ascii_table[i] & j))
429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432}
433
Stefan Richtera1c62502007-03-14 00:27:18 +0100434/* IEEE 1212 clause 7.5.4.1 textual descriptors (English, minimal ASCII) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s)
436{
Stefan Richtera1c62502007-03-14 00:27:18 +0100437 struct csr1212_keyval *kv;
438 u32 *text;
439 size_t str_len, quads;
440
441 if (!s || !*s || csr1212_check_minimal_ascii(s))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return NULL;
443
Stefan Richtera1c62502007-03-14 00:27:18 +0100444 str_len = strlen(s);
445 quads = bytes_to_quads(str_len);
446 kv = csr1212_new_descriptor_leaf(0, 0, NULL, quads_to_bytes(quads) +
447 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_OVERHEAD);
448 if (!kv)
449 return NULL;
450
451 kv->value.leaf.data[1] = 0; /* width, character_set, language */
452 text = CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv);
453 text[quads - 1] = 0; /* padding */
454 memcpy(text, s, str_len);
455
456 return kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460/* Destruction Routines */
461
462void csr1212_detach_keyval_from_directory(struct csr1212_keyval *dir,
463 struct csr1212_keyval *kv)
464{
465 struct csr1212_dentry *dentry;
466
467 if (!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY)
468 return;
469
470 dentry = csr1212_find_keyval(dir, kv);
471
472 if (!dentry)
473 return;
474
475 if (dentry->prev)
476 dentry->prev->next = dentry->next;
477 if (dentry->next)
478 dentry->next->prev = dentry->prev;
479 if (dir->value.directory.dentries_head == dentry)
480 dir->value.directory.dentries_head = dentry->next;
481 if (dir->value.directory.dentries_tail == dentry)
482 dir->value.directory.dentries_tail = dentry->prev;
483
484 CSR1212_FREE(dentry);
485
486 csr1212_release_keyval(kv);
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/* This function is used to free the memory taken by a keyval. If the given
490 * keyval is a directory type, then any keyvals contained in that directory
Stefan Richter17a19b72007-09-15 14:50:25 +0200491 * will be destroyed as well if noone holds a reference on them. By means of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * list manipulation, this routine will descend a directory structure in a
493 * non-recursive manner. */
Stefan Richter17a19b72007-09-15 14:50:25 +0200494void csr1212_release_keyval(struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct csr1212_keyval *k, *a;
497 struct csr1212_dentry dentry;
498 struct csr1212_dentry *head, *tail;
499
Stefan Richter17a19b72007-09-15 14:50:25 +0200500 if (!atomic_dec_and_test(&kv->refcnt))
501 return;
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 dentry.kv = kv;
504 dentry.next = NULL;
505 dentry.prev = NULL;
506
507 head = &dentry;
508 tail = head;
509
510 while (head) {
511 k = head->kv;
512
513 while (k) {
Stefan Richter17a19b72007-09-15 14:50:25 +0200514 /* must not dec_and_test kv->refcnt again */
515 if (k != kv && !atomic_dec_and_test(&k->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 break;
517
518 a = k->associate;
519
520 if (k->key.type == CSR1212_KV_TYPE_DIRECTORY) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100521 /* If the current entry is a directory, move all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * the entries to the destruction list. */
523 if (k->value.directory.dentries_head) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100524 tail->next =
525 k->value.directory.dentries_head;
526 k->value.directory.dentries_head->prev =
527 tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 tail = k->value.directory.dentries_tail;
529 }
530 }
531 free_keyval(k);
532 k = a;
533 }
534
535 head = head->next;
536 if (head) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100537 if (head->prev && head->prev != &dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 CSR1212_FREE(head->prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 head->prev = NULL;
Stefan Richterc868ae22007-03-14 00:26:38 +0100540 } else if (tail != &dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 CSR1212_FREE(tail);
Stefan Richterc868ae22007-03-14 00:26:38 +0100542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546void csr1212_destroy_csr(struct csr1212_csr *csr)
547{
548 struct csr1212_csr_rom_cache *c, *oc;
549 struct csr1212_cache_region *cr, *ocr;
550
551 csr1212_release_keyval(csr->root_kv);
552
553 c = csr->cache_head;
554 while (c) {
555 oc = c;
556 cr = c->filled_head;
557 while (cr) {
558 ocr = cr;
559 cr = cr->next;
560 CSR1212_FREE(ocr);
561 }
562 c = c->next;
563 CSR1212_FREE(oc);
564 }
565
566 CSR1212_FREE(csr);
567}
568
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/* CSR Image Creation */
571
572static int csr1212_append_new_cache(struct csr1212_csr *csr, size_t romsize)
573{
574 struct csr1212_csr_rom_cache *cache;
Stefan Richter982610b2007-03-11 22:49:34 +0100575 u64 csr_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Stefan Richter64ff7122007-03-11 22:50:13 +0100577 BUG_ON(!csr || !csr->ops || !csr->ops->allocate_addr_range ||
578 !csr->ops->release_addr || csr->max_rom < 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /* ROM size must be a multiple of csr->max_rom */
581 romsize = (romsize + (csr->max_rom - 1)) & ~(csr->max_rom - 1);
582
Stefan Richterc868ae22007-03-14 00:26:38 +0100583 csr_addr = csr->ops->allocate_addr_range(romsize, csr->max_rom,
584 csr->private);
585 if (csr_addr == CSR1212_INVALID_ADDR_SPACE)
Stefan Richter7fb9add2007-03-11 22:49:05 +0100586 return -ENOMEM;
Stefan Richterc868ae22007-03-14 00:26:38 +0100587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (csr_addr < CSR1212_REGISTER_SPACE_BASE) {
589 /* Invalid address returned from allocate_addr_range(). */
590 csr->ops->release_addr(csr_addr, csr->private);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100591 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
Stefan Richterc868ae22007-03-14 00:26:38 +0100594 cache = csr1212_rom_cache_malloc(csr_addr - CSR1212_REGISTER_SPACE_BASE,
595 romsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (!cache) {
597 csr->ops->release_addr(csr_addr, csr->private);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100598 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
Stefan Richterc868ae22007-03-14 00:26:38 +0100601 cache->ext_rom = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF,
602 CSR1212_KV_ID_EXTENDED_ROM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!cache->ext_rom) {
604 csr->ops->release_addr(csr_addr, csr->private);
605 CSR1212_FREE(cache);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100606 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
Stefan Richterc868ae22007-03-14 00:26:38 +0100609 if (csr1212_attach_keyval_to_directory(csr->root_kv, cache->ext_rom) !=
610 CSR1212_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 csr1212_release_keyval(cache->ext_rom);
612 csr->ops->release_addr(csr_addr, csr->private);
613 CSR1212_FREE(cache);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100614 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616 cache->ext_rom->offset = csr_addr - CSR1212_REGISTER_SPACE_BASE;
617 cache->ext_rom->value.leaf.len = -1;
618 cache->ext_rom->value.leaf.data = cache->data;
619
620 /* Add cache to tail of cache list */
621 cache->prev = csr->cache_tail;
622 csr->cache_tail->next = cache;
623 csr->cache_tail = cache;
624 return CSR1212_SUCCESS;
625}
626
Stefan Richter6c88e472007-03-11 22:47:34 +0100627static void csr1212_remove_cache(struct csr1212_csr *csr,
628 struct csr1212_csr_rom_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 if (csr->cache_head == cache)
631 csr->cache_head = cache->next;
632 if (csr->cache_tail == cache)
633 csr->cache_tail = cache->prev;
634
635 if (cache->prev)
636 cache->prev->next = cache->next;
637 if (cache->next)
638 cache->next->prev = cache->prev;
639
640 if (cache->ext_rom) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100641 csr1212_detach_keyval_from_directory(csr->root_kv,
642 cache->ext_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 csr1212_release_keyval(cache->ext_rom);
644 }
645
646 CSR1212_FREE(cache);
647}
648
649static int csr1212_generate_layout_subdir(struct csr1212_keyval *dir,
650 struct csr1212_keyval **layout_tail)
651{
652 struct csr1212_dentry *dentry;
653 struct csr1212_keyval *dkv;
654 struct csr1212_keyval *last_extkey_spec = NULL;
655 struct csr1212_keyval *last_extkey = NULL;
656 int num_entries = 0;
657
658 for (dentry = dir->value.directory.dentries_head; dentry;
659 dentry = dentry->next) {
660 for (dkv = dentry->kv; dkv; dkv = dkv->associate) {
661 /* Special Case: Extended Key Specifier_ID */
Stefan Richterc868ae22007-03-14 00:26:38 +0100662 if (dkv->key.id ==
663 CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
664 if (last_extkey_spec == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 last_extkey_spec = dkv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100666 else if (dkv->value.immediate !=
667 last_extkey_spec->value.immediate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 last_extkey_spec = dkv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100669 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* Special Case: Extended Key */
672 } else if (dkv->key.id == CSR1212_KV_ID_EXTENDED_KEY) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100673 if (last_extkey == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 last_extkey = dkv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100675 else if (dkv->value.immediate !=
676 last_extkey->value.immediate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 last_extkey = dkv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100678 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
682 num_entries += 1;
683
Stefan Richterc868ae22007-03-14 00:26:38 +0100684 switch (dkv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 default:
686 case CSR1212_KV_TYPE_IMMEDIATE:
687 case CSR1212_KV_TYPE_CSR_OFFSET:
688 break;
689 case CSR1212_KV_TYPE_LEAF:
690 case CSR1212_KV_TYPE_DIRECTORY:
691 /* Remove from list */
692 if (dkv->prev && (dkv->prev->next == dkv))
693 dkv->prev->next = dkv->next;
694 if (dkv->next && (dkv->next->prev == dkv))
695 dkv->next->prev = dkv->prev;
696 //if (dkv == *layout_tail)
697 // *layout_tail = dkv->prev;
698
699 /* Special case: Extended ROM leafs */
700 if (dkv->key.id == CSR1212_KV_ID_EXTENDED_ROM) {
701 dkv->value.leaf.len = -1;
Stefan Richterc868ae22007-03-14 00:26:38 +0100702 /* Don't add Extended ROM leafs in the
703 * layout list, they are handled
704 * differently. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 break;
706 }
707
708 /* Add to tail of list */
709 dkv->next = NULL;
710 dkv->prev = *layout_tail;
711 (*layout_tail)->next = dkv;
712 *layout_tail = dkv;
713 break;
714 }
715 }
716 }
717 return num_entries;
718}
719
Stefan Richter6c88e472007-03-11 22:47:34 +0100720static size_t csr1212_generate_layout_order(struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct csr1212_keyval *ltail = kv;
723 size_t agg_size = 0;
724
Stefan Richterc868ae22007-03-14 00:26:38 +0100725 while (kv) {
726 switch (kv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 case CSR1212_KV_TYPE_LEAF:
728 /* Add 1 quadlet for crc/len field */
729 agg_size += kv->value.leaf.len + 1;
730 break;
731
732 case CSR1212_KV_TYPE_DIRECTORY:
Stefan Richterc868ae22007-03-14 00:26:38 +0100733 kv->value.directory.len =
734 csr1212_generate_layout_subdir(kv, &ltail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /* Add 1 quadlet for crc/len field */
736 agg_size += kv->value.directory.len + 1;
737 break;
738 }
739 kv = kv->next;
740 }
741 return quads_to_bytes(agg_size);
742}
743
Stefan Richter6c88e472007-03-11 22:47:34 +0100744static struct csr1212_keyval *
745csr1212_generate_positions(struct csr1212_csr_rom_cache *cache,
746 struct csr1212_keyval *start_kv, int start_pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 struct csr1212_keyval *kv = start_kv;
749 struct csr1212_keyval *okv = start_kv;
750 int pos = start_pos;
751 int kv_len = 0, okv_len = 0;
752
753 cache->layout_head = kv;
754
Stefan Richterc868ae22007-03-14 00:26:38 +0100755 while (kv && pos < cache->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* Special case: Extended ROM leafs */
Stefan Richterc868ae22007-03-14 00:26:38 +0100757 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 kv->offset = cache->offset + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Stefan Richterc868ae22007-03-14 00:26:38 +0100760 switch (kv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 case CSR1212_KV_TYPE_LEAF:
762 kv_len = kv->value.leaf.len;
763 break;
764
765 case CSR1212_KV_TYPE_DIRECTORY:
766 kv_len = kv->value.directory.len;
767 break;
768
769 default:
770 /* Should never get here */
Stefan Richterc94ccf92007-03-14 00:27:46 +0100771 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 break;
773 }
774
775 pos += quads_to_bytes(kv_len + 1);
776
777 if (pos <= cache->size) {
778 okv = kv;
779 okv_len = kv_len;
780 kv = kv->next;
781 }
782 }
783
784 cache->layout_tail = okv;
Stefan Richterc868ae22007-03-14 00:26:38 +0100785 cache->len = okv->offset - cache->offset + quads_to_bytes(okv_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 return kv;
788}
789
Stefan Richter6c88e472007-03-11 22:47:34 +0100790#define CSR1212_KV_KEY_SHIFT 24
791#define CSR1212_KV_KEY_TYPE_SHIFT 6
792#define CSR1212_KV_KEY_ID_MASK 0x3f
793#define CSR1212_KV_KEY_TYPE_MASK 0x3 /* after shift */
794
795static void
Stefan Richter982610b2007-03-11 22:49:34 +0100796csr1212_generate_tree_subdir(struct csr1212_keyval *dir, u32 *data_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 struct csr1212_dentry *dentry;
799 struct csr1212_keyval *last_extkey_spec = NULL;
800 struct csr1212_keyval *last_extkey = NULL;
801 int index = 0;
802
Stefan Richterc868ae22007-03-14 00:26:38 +0100803 for (dentry = dir->value.directory.dentries_head;
804 dentry;
805 dentry = dentry->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 struct csr1212_keyval *a;
807
808 for (a = dentry->kv; a; a = a->associate) {
Stefan Richter982610b2007-03-11 22:49:34 +0100809 u32 value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* Special Case: Extended Key Specifier_ID */
Stefan Richterc868ae22007-03-14 00:26:38 +0100812 if (a->key.id ==
813 CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
814 if (last_extkey_spec == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 last_extkey_spec = a;
Stefan Richterc868ae22007-03-14 00:26:38 +0100816 else if (a->value.immediate !=
817 last_extkey_spec->value.immediate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 last_extkey_spec = a;
Stefan Richterc868ae22007-03-14 00:26:38 +0100819 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 continue;
Stefan Richterc868ae22007-03-14 00:26:38 +0100821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /* Special Case: Extended Key */
823 } else if (a->key.id == CSR1212_KV_ID_EXTENDED_KEY) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100824 if (last_extkey == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 last_extkey = a;
Stefan Richterc868ae22007-03-14 00:26:38 +0100826 else if (a->value.immediate !=
827 last_extkey->value.immediate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 last_extkey = a;
Stefan Richterc868ae22007-03-14 00:26:38 +0100829 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
Stefan Richterc868ae22007-03-14 00:26:38 +0100833 switch (a->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 case CSR1212_KV_TYPE_IMMEDIATE:
835 value = a->value.immediate;
836 break;
837 case CSR1212_KV_TYPE_CSR_OFFSET:
838 value = a->value.csr_offset;
839 break;
840 case CSR1212_KV_TYPE_LEAF:
841 value = a->offset;
842 value -= dir->offset + quads_to_bytes(1+index);
843 value = bytes_to_quads(value);
844 break;
845 case CSR1212_KV_TYPE_DIRECTORY:
846 value = a->offset;
847 value -= dir->offset + quads_to_bytes(1+index);
848 value = bytes_to_quads(value);
849 break;
850 default:
851 /* Should never get here */
Stefan Richterc94ccf92007-03-14 00:27:46 +0100852 WARN_ON(1);
853 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
Stefan Richterc868ae22007-03-14 00:26:38 +0100856 value |= (a->key.id & CSR1212_KV_KEY_ID_MASK) <<
857 CSR1212_KV_KEY_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 value |= (a->key.type & CSR1212_KV_KEY_TYPE_MASK) <<
Stefan Richterc868ae22007-03-14 00:26:38 +0100859 (CSR1212_KV_KEY_SHIFT +
860 CSR1212_KV_KEY_TYPE_SHIFT);
Stefan Richter7fb9add2007-03-11 22:49:05 +0100861 data_buffer[index] = cpu_to_be32(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 index++;
863 }
864 }
865}
866
Stefan Richter6c88e472007-03-11 22:47:34 +0100867struct csr1212_keyval_img {
Stefan Richter982610b2007-03-11 22:49:34 +0100868 u16 length;
869 u16 crc;
Stefan Richter6c88e472007-03-11 22:47:34 +0100870
871 /* Must be last */
Stefan Richter982610b2007-03-11 22:49:34 +0100872 u32 data[0]; /* older gcc can't handle [] which is standard */
Stefan Richter6c88e472007-03-11 22:47:34 +0100873};
874
875static void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 struct csr1212_keyval *kv, *nkv;
878 struct csr1212_keyval_img *kvi;
879
Stefan Richterc868ae22007-03-14 00:26:38 +0100880 for (kv = cache->layout_head;
881 kv != cache->layout_tail->next;
882 kv = nkv) {
883 kvi = (struct csr1212_keyval_img *)(cache->data +
884 bytes_to_quads(kv->offset - cache->offset));
885 switch (kv->key.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 default:
887 case CSR1212_KV_TYPE_IMMEDIATE:
888 case CSR1212_KV_TYPE_CSR_OFFSET:
889 /* Should never get here */
Stefan Richterc94ccf92007-03-14 00:27:46 +0100890 WARN_ON(1);
891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 case CSR1212_KV_TYPE_LEAF:
894 /* Don't copy over Extended ROM areas, they are
895 * already filled out! */
896 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
897 memcpy(kvi->data, kv->value.leaf.data,
898 quads_to_bytes(kv->value.leaf.len));
899
Stefan Richter7fb9add2007-03-11 22:49:05 +0100900 kvi->length = cpu_to_be16(kv->value.leaf.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 kvi->crc = csr1212_crc16(kvi->data, kv->value.leaf.len);
902 break;
903
904 case CSR1212_KV_TYPE_DIRECTORY:
905 csr1212_generate_tree_subdir(kv, kvi->data);
906
Stefan Richter7fb9add2007-03-11 22:49:05 +0100907 kvi->length = cpu_to_be16(kv->value.directory.len);
Stefan Richterc868ae22007-03-14 00:26:38 +0100908 kvi->crc = csr1212_crc16(kvi->data,
909 kv->value.directory.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 break;
911 }
912
913 nkv = kv->next;
914 if (kv->prev)
915 kv->prev->next = NULL;
916 if (kv->next)
917 kv->next->prev = NULL;
918 kv->prev = NULL;
919 kv->next = NULL;
920 }
921}
922
Stefan Richterfd2f3bd2007-03-11 22:51:24 +0100923/* This size is arbitrarily chosen.
924 * The struct overhead is subtracted for more economic allocations. */
925#define CSR1212_EXTENDED_ROM_SIZE (2048 - sizeof(struct csr1212_csr_rom_cache))
Stefan Richter6c88e472007-03-11 22:47:34 +0100926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927int csr1212_generate_csr_image(struct csr1212_csr *csr)
928{
929 struct csr1212_bus_info_block_img *bi;
930 struct csr1212_csr_rom_cache *cache;
931 struct csr1212_keyval *kv;
932 size_t agg_size;
933 int ret;
934 int init_offset;
935
Stefan Richter64ff7122007-03-11 22:50:13 +0100936 BUG_ON(!csr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 cache = csr->cache_head;
939
940 bi = (struct csr1212_bus_info_block_img*)cache->data;
941
942 bi->length = bytes_to_quads(csr->bus_info_len) - 1;
943 bi->crc_length = bi->length;
944 bi->crc = csr1212_crc16(bi->data, bi->crc_length);
945
946 csr->root_kv->next = NULL;
947 csr->root_kv->prev = NULL;
948
949 agg_size = csr1212_generate_layout_order(csr->root_kv);
950
951 init_offset = csr->bus_info_len;
952
Stefan Richterc868ae22007-03-14 00:26:38 +0100953 for (kv = csr->root_kv, cache = csr->cache_head;
954 kv;
955 cache = cache->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (!cache) {
957 /* Estimate approximate number of additional cache
958 * regions needed (it assumes that the cache holding
959 * the first 1K Config ROM space always exists). */
960 int est_c = agg_size / (CSR1212_EXTENDED_ROM_SIZE -
Stefan Richter982610b2007-03-11 22:49:34 +0100961 (2 * sizeof(u32))) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 /* Add additional cache regions, extras will be
964 * removed later */
965 for (; est_c; est_c--) {
Stefan Richterc868ae22007-03-14 00:26:38 +0100966 ret = csr1212_append_new_cache(csr,
967 CSR1212_EXTENDED_ROM_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (ret != CSR1212_SUCCESS)
969 return ret;
970 }
971 /* Need to re-layout for additional cache regions */
972 agg_size = csr1212_generate_layout_order(csr->root_kv);
973 kv = csr->root_kv;
974 cache = csr->cache_head;
975 init_offset = csr->bus_info_len;
976 }
977 kv = csr1212_generate_positions(cache, kv, init_offset);
978 agg_size -= cache->len;
Stefan Richter982610b2007-03-11 22:49:34 +0100979 init_offset = sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981
982 /* Remove unused, excess cache regions */
983 while (cache) {
984 struct csr1212_csr_rom_cache *oc = cache;
985
986 cache = cache->next;
987 csr1212_remove_cache(csr, oc);
988 }
989
990 /* Go through the list backward so that when done, the correct CRC
991 * will be calculated for the Extended ROM areas. */
Stefan Richterc868ae22007-03-14 00:26:38 +0100992 for (cache = csr->cache_tail; cache; cache = cache->prev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /* Only Extended ROM caches should have this set. */
994 if (cache->ext_rom) {
995 int leaf_size;
996
997 /* Make sure the Extended ROM leaf is a multiple of
998 * max_rom in size. */
Stefan Richter64ff7122007-03-11 22:50:13 +0100999 BUG_ON(csr->max_rom < 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 leaf_size = (cache->len + (csr->max_rom - 1)) &
1001 ~(csr->max_rom - 1);
1002
1003 /* Zero out the unused ROM region */
1004 memset(cache->data + bytes_to_quads(cache->len), 0x00,
1005 leaf_size - cache->len);
1006
1007 /* Subtract leaf header */
Stefan Richter982610b2007-03-11 22:49:34 +01001008 leaf_size -= sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 /* Update the Extended ROM leaf length */
1011 cache->ext_rom->value.leaf.len =
1012 bytes_to_quads(leaf_size);
1013 } else {
1014 /* Zero out the unused ROM region */
1015 memset(cache->data + bytes_to_quads(cache->len), 0x00,
1016 cache->size - cache->len);
1017 }
1018
1019 /* Copy the data into the cache buffer */
1020 csr1212_fill_cache(cache);
1021
1022 if (cache != csr->cache_head) {
1023 /* Set the length and CRC of the extended ROM. */
1024 struct csr1212_keyval_img *kvi =
1025 (struct csr1212_keyval_img*)cache->data;
Stefan Richter982610b2007-03-11 22:49:34 +01001026 u16 len = bytes_to_quads(cache->len) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Stefan Richter7fb9add2007-03-11 22:49:05 +01001028 kvi->length = cpu_to_be16(len);
1029 kvi->crc = csr1212_crc16(kvi->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 }
1032
1033 return CSR1212_SUCCESS;
1034}
1035
Stefan Richter982610b2007-03-11 22:49:34 +01001036int csr1212_read(struct csr1212_csr *csr, u32 offset, void *buffer, u32 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 struct csr1212_csr_rom_cache *cache;
1039
Stefan Richterc868ae22007-03-14 00:26:38 +01001040 for (cache = csr->cache_head; cache; cache = cache->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (offset >= cache->offset &&
1042 (offset + len) <= (cache->offset + cache->size)) {
Stefan Richterc868ae22007-03-14 00:26:38 +01001043 memcpy(buffer, &cache->data[
1044 bytes_to_quads(offset - cache->offset)],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 len);
1046 return CSR1212_SUCCESS;
1047 }
Stefan Richterc868ae22007-03-14 00:26:38 +01001048
Stefan Richter7fb9add2007-03-11 22:49:05 +01001049 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Stefan Richterfde675f2008-05-02 20:14:52 +02001052/*
1053 * Apparently there are many different wrong implementations of the CRC
1054 * algorithm. We don't fail, we just warn... approximately once per GUID.
1055 */
1056static void
1057csr1212_check_crc(const u32 *buffer, size_t length, u16 crc, __be32 *guid)
1058{
1059 static u64 last_bad_eui64;
1060 u64 eui64 = ((u64)be32_to_cpu(guid[0]) << 32) | be32_to_cpu(guid[1]);
1061
1062 if (csr1212_crc16(buffer, length) == crc ||
1063 csr1212_msft_crc16(buffer, length) == crc ||
1064 eui64 == last_bad_eui64)
1065 return;
1066
1067 printk(KERN_DEBUG "ieee1394: config ROM CRC error\n");
1068 last_bad_eui64 = eui64;
1069}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/* Parse a chunk of data as a Config ROM */
1072
1073static int csr1212_parse_bus_info_block(struct csr1212_csr *csr)
1074{
1075 struct csr1212_bus_info_block_img *bi;
1076 struct csr1212_cache_region *cr;
1077 int i;
1078 int ret;
1079
Stefan Richter982610b2007-03-11 22:49:34 +01001080 for (i = 0; i < csr->bus_info_len; i += sizeof(u32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
Stefan Richter0bed1812008-12-13 23:12:06 +01001082 &csr->cache_head->data[bytes_to_quads(i)],
1083 csr->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (ret != CSR1212_SUCCESS)
1085 return ret;
Stefan Richterb2051f82007-01-03 19:32:13 +01001086
1087 /* check ROM header's info_length */
1088 if (i == 0 &&
Stefan Richter7fb9add2007-03-11 22:49:05 +01001089 be32_to_cpu(csr->cache_head->data[0]) >> 24 !=
Stefan Richterb2051f82007-01-03 19:32:13 +01001090 bytes_to_quads(csr->bus_info_len) - 1)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001091 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
1094 bi = (struct csr1212_bus_info_block_img*)csr->cache_head->data;
1095 csr->crc_len = quads_to_bytes(bi->crc_length);
1096
Stefan Richterc868ae22007-03-14 00:26:38 +01001097 /* IEEE 1212 recommends that crc_len be equal to bus_info_len, but that
1098 * is not always the case, so read the rest of the crc area 1 quadlet at
1099 * a time. */
Stefan Richter982610b2007-03-11 22:49:34 +01001100 for (i = csr->bus_info_len; i <= csr->crc_len; i += sizeof(u32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
Stefan Richter0bed1812008-12-13 23:12:06 +01001102 &csr->cache_head->data[bytes_to_quads(i)],
1103 csr->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (ret != CSR1212_SUCCESS)
1105 return ret;
1106 }
1107
Stefan Richterfde675f2008-05-02 20:14:52 +02001108 csr1212_check_crc(bi->data, bi->crc_length, bi->crc,
1109 &csr->bus_info_data[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Stefan Richter85511582005-11-07 06:31:45 -05001111 cr = CSR1212_MALLOC(sizeof(*cr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (!cr)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001113 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 cr->next = NULL;
1116 cr->prev = NULL;
1117 cr->offset_start = 0;
1118 cr->offset_end = csr->crc_len + 4;
1119
1120 csr->cache_head->filled_head = cr;
1121 csr->cache_head->filled_tail = cr;
1122
1123 return CSR1212_SUCCESS;
1124}
1125
Stefan Richter7fb9add2007-03-11 22:49:05 +01001126#define CSR1212_KV_KEY(q) (be32_to_cpu(q) >> CSR1212_KV_KEY_SHIFT)
Stefan Richter6c88e472007-03-11 22:47:34 +01001127#define CSR1212_KV_KEY_TYPE(q) (CSR1212_KV_KEY(q) >> CSR1212_KV_KEY_TYPE_SHIFT)
1128#define CSR1212_KV_KEY_ID(q) (CSR1212_KV_KEY(q) & CSR1212_KV_KEY_ID_MASK)
1129#define CSR1212_KV_VAL_MASK 0xffffff
Stefan Richter7fb9add2007-03-11 22:49:05 +01001130#define CSR1212_KV_VAL(q) (be32_to_cpu(q) & CSR1212_KV_VAL_MASK)
Stefan Richter6c88e472007-03-11 22:47:34 +01001131
Stefan Richterc868ae22007-03-14 00:26:38 +01001132static int
1133csr1212_parse_dir_entry(struct csr1212_keyval *dir, u32 ki, u32 kv_pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 int ret = CSR1212_SUCCESS;
1136 struct csr1212_keyval *k = NULL;
Stefan Richter982610b2007-03-11 22:49:34 +01001137 u32 offset;
Stefan Richter17a19b72007-09-15 14:50:25 +02001138 bool keep_keyval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Stefan Richterc868ae22007-03-14 00:26:38 +01001140 switch (CSR1212_KV_KEY_TYPE(ki)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 case CSR1212_KV_TYPE_IMMEDIATE:
1142 k = csr1212_new_immediate(CSR1212_KV_KEY_ID(ki),
1143 CSR1212_KV_VAL(ki));
1144 if (!k) {
Stefan Richter7fb9add2007-03-11 22:49:05 +01001145 ret = -ENOMEM;
Stefan Richter511f7b32007-03-14 00:28:36 +01001146 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
Stefan Richter17a19b72007-09-15 14:50:25 +02001148 /* Don't keep local reference when parsing. */
1149 keep_keyval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 break;
1151
1152 case CSR1212_KV_TYPE_CSR_OFFSET:
1153 k = csr1212_new_csr_offset(CSR1212_KV_KEY_ID(ki),
1154 CSR1212_KV_VAL(ki));
1155 if (!k) {
Stefan Richter7fb9add2007-03-11 22:49:05 +01001156 ret = -ENOMEM;
Stefan Richter511f7b32007-03-14 00:28:36 +01001157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
Stefan Richter17a19b72007-09-15 14:50:25 +02001159 /* Don't keep local reference when parsing. */
1160 keep_keyval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 break;
1162
1163 default:
1164 /* Compute the offset from 0xffff f000 0000. */
1165 offset = quads_to_bytes(CSR1212_KV_VAL(ki)) + kv_pos;
1166 if (offset == kv_pos) {
1167 /* Uh-oh. Can't have a relative offset of 0 for Leaves
1168 * or Directories. The Config ROM image is most likely
1169 * messed up, so we'll just abort here. */
Stefan Richter7fb9add2007-03-11 22:49:05 +01001170 ret = -EIO;
Stefan Richter511f7b32007-03-14 00:28:36 +01001171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173
1174 k = csr1212_find_keyval_offset(dir, offset);
1175
1176 if (k)
1177 break; /* Found it. */
1178
Stefan Richterc868ae22007-03-14 00:26:38 +01001179 if (CSR1212_KV_KEY_TYPE(ki) == CSR1212_KV_TYPE_DIRECTORY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 k = csr1212_new_directory(CSR1212_KV_KEY_ID(ki));
Stefan Richterc868ae22007-03-14 00:26:38 +01001181 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 k = csr1212_new_leaf(CSR1212_KV_KEY_ID(ki), NULL, 0);
Stefan Richterc868ae22007-03-14 00:26:38 +01001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (!k) {
Stefan Richter7fb9add2007-03-11 22:49:05 +01001185 ret = -ENOMEM;
Stefan Richter511f7b32007-03-14 00:28:36 +01001186 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
Stefan Richter17a19b72007-09-15 14:50:25 +02001188 /* Don't keep local reference when parsing. */
1189 keep_keyval = false;
1190 /* Contents not read yet so it's not valid. */
1191 k->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 k->offset = offset;
1193
1194 k->prev = dir;
1195 k->next = dir->next;
1196 dir->next->prev = k;
1197 dir->next = k;
1198 }
Stefan Richter17a19b72007-09-15 14:50:25 +02001199 ret = __csr1212_attach_keyval_to_directory(dir, k, keep_keyval);
Stefan Richter511f7b32007-03-14 00:28:36 +01001200out:
Stefan Richter6c88e472007-03-11 22:47:34 +01001201 if (ret != CSR1212_SUCCESS && k != NULL)
1202 free_keyval(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return ret;
1204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206int csr1212_parse_keyval(struct csr1212_keyval *kv,
1207 struct csr1212_csr_rom_cache *cache)
1208{
1209 struct csr1212_keyval_img *kvi;
1210 int i;
1211 int ret = CSR1212_SUCCESS;
1212 int kvi_len;
1213
Stefan Richterc868ae22007-03-14 00:26:38 +01001214 kvi = (struct csr1212_keyval_img*)
1215 &cache->data[bytes_to_quads(kv->offset - cache->offset)];
Stefan Richter7fb9add2007-03-11 22:49:05 +01001216 kvi_len = be16_to_cpu(kvi->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Stefan Richterfde675f2008-05-02 20:14:52 +02001218 /* GUID is wrong in here in case of extended ROM. We don't care. */
1219 csr1212_check_crc(kvi->data, kvi_len, kvi->crc, &cache->data[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
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;
Stefan Richter511f7b32007-03-14 00:28:36 +01001244 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
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;
Stefan Richter511f7b32007-03-14 00:28:36 +01001254out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return ret;
1256}
1257
Stefan Richterc1a37f22007-03-14 00:20:53 +01001258static int
1259csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 struct csr1212_cache_region *cr, *ncr, *newcr = NULL;
1262 struct csr1212_keyval_img *kvi = NULL;
1263 struct csr1212_csr_rom_cache *cache;
1264 int cache_index;
Stefan Richter982610b2007-03-11 22:49:34 +01001265 u64 addr;
1266 u32 *cache_ptr;
1267 u16 kv_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Stefan Richter64ff7122007-03-11 22:50:13 +01001269 BUG_ON(!csr || !kv || csr->max_rom < 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* First find which cache the data should be in (or go in if not read
1272 * yet). */
Stefan Richterc868ae22007-03-14 00:26:38 +01001273 for (cache = csr->cache_head; cache; cache = cache->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (kv->offset >= cache->offset &&
1275 kv->offset < (cache->offset + cache->size))
1276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 if (!cache) {
Stefan Richter982610b2007-03-11 22:49:34 +01001279 u32 q, cache_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 /* Only create a new cache for Extended ROM leaves. */
1282 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001283 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (csr->ops->bus_read(csr,
1286 CSR1212_REGISTER_SPACE_BASE + kv->offset,
Stefan Richter0bed1812008-12-13 23:12:06 +01001287 &q, csr->private))
Stefan Richter7fb9add2007-03-11 22:49:05 +01001288 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Stefan Richter7fb9add2007-03-11 22:49:05 +01001290 kv->value.leaf.len = be32_to_cpu(q) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 cache_size = (quads_to_bytes(kv->value.leaf.len + 1) +
1293 (csr->max_rom - 1)) & ~(csr->max_rom - 1);
1294
1295 cache = csr1212_rom_cache_malloc(kv->offset, cache_size);
1296 if (!cache)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001297 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 kv->value.leaf.data = &cache->data[1];
1300 csr->cache_tail->next = cache;
1301 cache->prev = csr->cache_tail;
1302 cache->next = NULL;
1303 csr->cache_tail = cache;
1304 cache->filled_head =
Stefan Richter85511582005-11-07 06:31:45 -05001305 CSR1212_MALLOC(sizeof(*cache->filled_head));
Stefan Richterc868ae22007-03-14 00:26:38 +01001306 if (!cache->filled_head)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001307 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 cache->filled_head->offset_start = 0;
Stefan Richter982610b2007-03-11 22:49:34 +01001310 cache->filled_head->offset_end = sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 cache->filled_tail = cache->filled_head;
1312 cache->filled_head->next = NULL;
1313 cache->filled_head->prev = NULL;
1314 cache->data[0] = q;
1315
1316 /* Don't read the entire extended ROM now. Pieces of it will
1317 * be read when entries inside it are read. */
1318 return csr1212_parse_keyval(kv, cache);
1319 }
1320
1321 cache_index = kv->offset - cache->offset;
1322
1323 /* Now seach read portions of the cache to see if it is there. */
1324 for (cr = cache->filled_head; cr; cr = cr->next) {
1325 if (cache_index < cr->offset_start) {
Stefan Richter85511582005-11-07 06:31:45 -05001326 newcr = CSR1212_MALLOC(sizeof(*newcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (!newcr)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001328 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 newcr->offset_start = cache_index & ~(csr->max_rom - 1);
1331 newcr->offset_end = newcr->offset_start;
1332 newcr->next = cr;
1333 newcr->prev = cr->prev;
1334 cr->prev = newcr;
1335 cr = newcr;
1336 break;
1337 } else if ((cache_index >= cr->offset_start) &&
1338 (cache_index < cr->offset_end)) {
1339 kvi = (struct csr1212_keyval_img*)
1340 (&cache->data[bytes_to_quads(cache_index)]);
Stefan Richter7fb9add2007-03-11 22:49:05 +01001341 kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 break;
Stefan Richterc868ae22007-03-14 00:26:38 +01001343 } else if (cache_index == cr->offset_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 break;
Stefan Richterc868ae22007-03-14 00:26:38 +01001345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347
1348 if (!cr) {
1349 cr = cache->filled_tail;
Stefan Richter85511582005-11-07 06:31:45 -05001350 newcr = CSR1212_MALLOC(sizeof(*newcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (!newcr)
Stefan Richter7fb9add2007-03-11 22:49:05 +01001352 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 newcr->offset_start = cache_index & ~(csr->max_rom - 1);
1355 newcr->offset_end = newcr->offset_start;
1356 newcr->prev = cr;
1357 newcr->next = cr->next;
1358 cr->next = newcr;
1359 cr = newcr;
1360 cache->filled_tail = newcr;
1361 }
1362
1363 while(!kvi || cr->offset_end < cache_index + kv_len) {
1364 cache_ptr = &cache->data[bytes_to_quads(cr->offset_end &
1365 ~(csr->max_rom - 1))];
1366
1367 addr = (CSR1212_CSR_ARCH_REG_SPACE_BASE + cache->offset +
1368 cr->offset_end) & ~(csr->max_rom - 1);
1369
Stefan Richter0bed1812008-12-13 23:12:06 +01001370 if (csr->ops->bus_read(csr, addr, cache_ptr, csr->private))
1371 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 cr->offset_end += csr->max_rom - (cr->offset_end &
1374 (csr->max_rom - 1));
1375
1376 if (!kvi && (cr->offset_end > cache_index)) {
1377 kvi = (struct csr1212_keyval_img*)
1378 (&cache->data[bytes_to_quads(cache_index)]);
Stefan Richter7fb9add2007-03-11 22:49:05 +01001379 kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381
1382 if ((kv_len + (kv->offset - cache->offset)) > cache->size) {
1383 /* The Leaf or Directory claims its length extends
1384 * beyond the ConfigROM image region and thus beyond the
1385 * end of our cache region. Therefore, we abort now
1386 * rather than seg faulting later. */
Stefan Richter7fb9add2007-03-11 22:49:05 +01001387 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389
1390 ncr = cr->next;
1391
1392 if (ncr && (cr->offset_end >= ncr->offset_start)) {
1393 /* consolidate region entries */
1394 ncr->offset_start = cr->offset_start;
1395
1396 if (cr->prev)
1397 cr->prev->next = cr->next;
1398 ncr->prev = cr->prev;
1399 if (cache->filled_head == cr)
1400 cache->filled_head = ncr;
1401 CSR1212_FREE(cr);
1402 cr = ncr;
1403 }
1404 }
1405
1406 return csr1212_parse_keyval(kv, cache);
1407}
1408
Stefan Richterc1a37f22007-03-14 00:20:53 +01001409struct csr1212_keyval *
1410csr1212_get_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
1411{
1412 if (!kv)
1413 return NULL;
1414 if (!kv->valid)
1415 if (csr1212_read_keyval(csr, kv) != CSR1212_SUCCESS)
1416 return NULL;
1417 return kv;
1418}
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420int csr1212_parse_csr(struct csr1212_csr *csr)
1421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 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 Richter0bed1812008-12-13 23:12:06 +01001431 /*
1432 * There has been a buggy firmware with bus_info_block.max_rom > 0
1433 * spotted which actually only supported quadlet read requests to the
1434 * config ROM. Therefore read everything quadlet by quadlet regardless
1435 * of what the bus info block says.
1436 */
1437 csr->max_rom = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 csr->cache_head->layout_head = csr->root_kv;
1440 csr->cache_head->layout_tail = csr->root_kv;
1441
1442 csr->root_kv->offset = (CSR1212_CONFIG_ROM_SPACE_BASE & 0xffff) +
1443 csr->bus_info_len;
1444
1445 csr->root_kv->valid = 0;
1446 csr->root_kv->next = csr->root_kv;
1447 csr->root_kv->prev = csr->root_kv;
Stefan Richterc1a37f22007-03-14 00:20:53 +01001448 ret = csr1212_read_keyval(csr, csr->root_kv);
Jody McIntyre5303a982005-11-22 12:17:11 -05001449 if (ret != CSR1212_SUCCESS)
1450 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 /* Scan through the Root directory finding all extended ROM regions
1453 * and make cache regions for them */
1454 for (dentry = csr->root_kv->value.directory.dentries_head;
1455 dentry; dentry = dentry->next) {
Jody McIntyrea96074e2005-11-22 12:17:14 -05001456 if (dentry->kv->key.id == CSR1212_KV_ID_EXTENDED_ROM &&
1457 !dentry->kv->valid) {
Stefan Richterc1a37f22007-03-14 00:20:53 +01001458 ret = csr1212_read_keyval(csr, dentry->kv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (ret != CSR1212_SUCCESS)
1460 return ret;
1461 }
1462 }
1463
1464 return CSR1212_SUCCESS;
1465}