csr1212.c 37.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * csr1212.c -- IEEE 1212 Control and Status Register support for Linux
 *
 * Copyright (C) 2003 Francois Retief <fgretief@sun.ac.za>
 *                    Steve Kinneberg <kinnebergsteve@acmsystems.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


/* TODO List:
 * - Verify interface consistency: i.e., public functions that take a size
 *   parameter expect size to be in bytes.
 */

36
#include <linux/errno.h>
37
#include <linux/kernel.h>
38
#include <linux/string.h>
39
#include <asm/bug.h>
40
#include <asm/byteorder.h>
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49

#include "csr1212.h"


/* Permitted key type for each key id */
#define __I (1 << CSR1212_KV_TYPE_IMMEDIATE)
#define __C (1 << CSR1212_KV_TYPE_CSR_OFFSET)
#define __D (1 << CSR1212_KV_TYPE_DIRECTORY)
#define __L (1 << CSR1212_KV_TYPE_LEAF)
50
static const u8 csr1212_key_id_type_map[0x30] = {
51
	__C,			/* used by Apple iSight */
L
Linus Torvalds 已提交
52 53 54 55 56
	__D | __L,		/* Descriptor */
	__I | __D | __L,	/* Bus_Dependent_Info */
	__I | __D | __L,	/* Vendor */
	__I,			/* Hardware_Version */
	0, 0,			/* Reserved */
57 58
	__D | __L | __I,	/* Module */
	__I, 0, 0, 0,		/* used by Apple iSight, Reserved */
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	__I,			/* Node_Capabilities */
	__L,			/* EUI_64 */
	0, 0, 0,		/* Reserved */
	__D,			/* Unit */
	__I,			/* Specifier_ID */
	__I,			/* Version */
	__I | __C | __D | __L,	/* Dependent_Info */
	__L,			/* Unit_Location */
	0,			/* Reserved */
	__I,			/* Model */
	__D,			/* Instance */
	__L,			/* Keyword */
	__D,			/* Feature */
	__L,			/* Extended_ROM */
	__I,			/* Extended_Key_Specifier_ID */
	__I,			/* Extended_Key */
	__I | __C | __D | __L,	/* Extended_Data */
	__L,			/* Modifiable_Descriptor */
	__I,			/* Directory_ID */
	__I,			/* Revision */
};
#undef __I
#undef __C
#undef __D
#undef __L


86
#define quads_to_bytes(_q) ((_q) * sizeof(u32))
J
Julia Lawall 已提交
87
#define bytes_to_quads(_b) DIV_ROUND_UP(_b, sizeof(u32))
L
Linus Torvalds 已提交
88

89
static void free_keyval(struct csr1212_keyval *kv)
L
Linus Torvalds 已提交
90 91 92 93 94 95 96 97
{
	if ((kv->key.type == CSR1212_KV_TYPE_LEAF) &&
	    (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM))
		CSR1212_FREE(kv->value.leaf.data);

	CSR1212_FREE(kv);
}

98
static u16 csr1212_crc16(const u32 *buffer, size_t length)
L
Linus Torvalds 已提交
99 100
{
	int shift;
101 102
	u32 data;
	u16 sum, crc = 0;
L
Linus Torvalds 已提交
103 104

	for (; length; length--) {
105
		data = be32_to_cpu(*buffer);
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113
		buffer++;
		for (shift = 28; shift >= 0; shift -= 4 ) {
			sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
			crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum);
		}
		crc &= 0xffff;
	}

114
	return cpu_to_be16(crc);
L
Linus Torvalds 已提交
115 116
}

117
/* Microsoft computes the CRC with the bytes in reverse order. */
118
static u16 csr1212_msft_crc16(const u32 *buffer, size_t length)
L
Linus Torvalds 已提交
119 120
{
	int shift;
121 122
	u32 data;
	u16 sum, crc = 0;
L
Linus Torvalds 已提交
123 124

	for (; length; length--) {
125
		data = le32_to_cpu(*buffer);
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133
		buffer++;
		for (shift = 28; shift >= 0; shift -= 4 ) {
			sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
			crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum);
		}
		crc &= 0xffff;
	}

134
	return cpu_to_be16(crc);
L
Linus Torvalds 已提交
135 136
}

137 138
static struct csr1212_dentry *
csr1212_find_keyval(struct csr1212_keyval *dir, struct csr1212_keyval *kv)
L
Linus Torvalds 已提交
139 140 141 142
{
	struct csr1212_dentry *pos;

	for (pos = dir->value.directory.dentries_head;
S
Stefan Richter 已提交
143
	     pos != NULL; pos = pos->next)
L
Linus Torvalds 已提交
144 145 146 147 148
		if (pos->kv == kv)
			return pos;
	return NULL;
}

149
static struct csr1212_keyval *
150
csr1212_find_keyval_offset(struct csr1212_keyval *kv_list, u32 offset)
L
Linus Torvalds 已提交
151 152 153
{
	struct csr1212_keyval *kv;

S
Stefan Richter 已提交
154
	for (kv = kv_list->next; kv && (kv != kv_list); kv = kv->next)
L
Linus Torvalds 已提交
155 156 157 158 159 160 161
		if (kv->offset == offset)
			return kv;
	return NULL;
}


/* Creation Routines */
162

L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops,
				       size_t bus_info_size, void *private)
{
	struct csr1212_csr *csr;

	csr = CSR1212_MALLOC(sizeof(*csr));
	if (!csr)
		return NULL;

	csr->cache_head =
		csr1212_rom_cache_malloc(CSR1212_CONFIG_ROM_SPACE_OFFSET,
					 CSR1212_CONFIG_ROM_SPACE_SIZE);
	if (!csr->cache_head) {
		CSR1212_FREE(csr);
		return NULL;
	}

	/* The keyval key id is not used for the root node, but a valid key id
	 * that can be used for a directory needs to be passed to
	 * csr1212_new_directory(). */
	csr->root_kv = csr1212_new_directory(CSR1212_KV_ID_VENDOR);
	if (!csr->root_kv) {
		CSR1212_FREE(csr->cache_head);
		CSR1212_FREE(csr);
		return NULL;
	}

	csr->bus_info_data = csr->cache_head->data;
	csr->bus_info_len = bus_info_size;
	csr->crc_len = bus_info_size;
	csr->ops = ops;
	csr->private = private;
	csr->cache_tail = csr->cache_head;

	return csr;
}

void csr1212_init_local_csr(struct csr1212_csr *csr,
201
			    const u32 *bus_info_data, int max_rom)
L
Linus Torvalds 已提交
202 203 204
{
	static const int mr_map[] = { 4, 64, 1024, 0 };

B
Ben Collins 已提交
205
	BUG_ON(max_rom & ~0x3);
L
Linus Torvalds 已提交
206 207 208 209
	csr->max_rom = mr_map[max_rom];
	memcpy(csr->bus_info_data, bus_info_data, csr->bus_info_len);
}

210
static struct csr1212_keyval *csr1212_new_keyval(u8 type, u8 key)
L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218 219 220
{
	struct csr1212_keyval *kv;

	if (key < 0x30 && ((csr1212_key_id_type_map[key] & (1 << type)) == 0))
		return NULL;

	kv = CSR1212_MALLOC(sizeof(*kv));
	if (!kv)
		return NULL;

221
	atomic_set(&kv->refcnt, 1);
L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229 230 231
	kv->key.type = type;
	kv->key.id = key;
	kv->associate = NULL;
	kv->next = NULL;
	kv->prev = NULL;
	kv->offset = 0;
	kv->valid = 0;
	return kv;
}

232
struct csr1212_keyval *csr1212_new_immediate(u8 key, u32 value)
L
Linus Torvalds 已提交
233
{
S
Stefan Richter 已提交
234
	struct csr1212_keyval *kv;
L
Linus Torvalds 已提交
235

S
Stefan Richter 已提交
236
	kv = csr1212_new_keyval(CSR1212_KV_TYPE_IMMEDIATE, key);
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244
	if (!kv)
		return NULL;

	kv->value.immediate = value;
	kv->valid = 1;
	return kv;
}

245
static struct csr1212_keyval *
246
csr1212_new_leaf(u8 key, const void *data, size_t data_len)
L
Linus Torvalds 已提交
247
{
S
Stefan Richter 已提交
248
	struct csr1212_keyval *kv;
L
Linus Torvalds 已提交
249

S
Stefan Richter 已提交
250
	kv = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, key);
L
Linus Torvalds 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	if (!kv)
		return NULL;

	if (data_len > 0) {
		kv->value.leaf.data = CSR1212_MALLOC(data_len);
		if (!kv->value.leaf.data) {
			CSR1212_FREE(kv);
			return NULL;
		}

		if (data)
			memcpy(kv->value.leaf.data, data, data_len);
	} else {
		kv->value.leaf.data = NULL;
	}

	kv->value.leaf.len = bytes_to_quads(data_len);
	kv->offset = 0;
	kv->valid = 1;

	return kv;
}

274
static struct csr1212_keyval *
275
csr1212_new_csr_offset(u8 key, u32 csr_offset)
L
Linus Torvalds 已提交
276
{
S
Stefan Richter 已提交
277
	struct csr1212_keyval *kv;
L
Linus Torvalds 已提交
278

S
Stefan Richter 已提交
279
	kv = csr1212_new_keyval(CSR1212_KV_TYPE_CSR_OFFSET, key);
L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287 288 289
	if (!kv)
		return NULL;

	kv->value.csr_offset = csr_offset;

	kv->offset = 0;
	kv->valid = 1;
	return kv;
}

290
struct csr1212_keyval *csr1212_new_directory(u8 key)
L
Linus Torvalds 已提交
291
{
S
Stefan Richter 已提交
292
	struct csr1212_keyval *kv;
L
Linus Torvalds 已提交
293

S
Stefan Richter 已提交
294
	kv = csr1212_new_keyval(CSR1212_KV_TYPE_DIRECTORY, key);
L
Linus Torvalds 已提交
295 296 297 298 299 300 301 302 303 304 305
	if (!kv)
		return NULL;

	kv->value.directory.len = 0;
	kv->offset = 0;
	kv->value.directory.dentries_head = NULL;
	kv->value.directory.dentries_tail = NULL;
	kv->valid = 1;
	return kv;
}

306 307
void csr1212_associate_keyval(struct csr1212_keyval *kv,
			      struct csr1212_keyval *associate)
L
Linus Torvalds 已提交
308
{
309 310 311 312 313 314 315
	BUG_ON(!kv || !associate || kv->key.id == CSR1212_KV_ID_DESCRIPTOR ||
	       (associate->key.id != CSR1212_KV_ID_DESCRIPTOR &&
		associate->key.id != CSR1212_KV_ID_DEPENDENT_INFO &&
		associate->key.id != CSR1212_KV_ID_EXTENDED_KEY &&
		associate->key.id != CSR1212_KV_ID_EXTENDED_DATA &&
		associate->key.id < 0x30) ||
	       (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID &&
S
Stefan Richter 已提交
316
		associate->key.id != CSR1212_KV_ID_EXTENDED_KEY) ||
317 318 319 320 321 322
	       (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
		associate->key.id != CSR1212_KV_ID_EXTENDED_DATA) ||
	       (associate->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
		kv->key.id != CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) ||
	       (associate->key.id == CSR1212_KV_ID_EXTENDED_DATA &&
		kv->key.id != CSR1212_KV_ID_EXTENDED_KEY));
L
Linus Torvalds 已提交
323 324 325 326

	if (kv->associate)
		csr1212_release_keyval(kv->associate);

327
	csr1212_keep_keyval(associate);
L
Linus Torvalds 已提交
328 329 330
	kv->associate = associate;
}

331 332 333
static int __csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
						struct csr1212_keyval *kv,
						bool keep_keyval)
L
Linus Torvalds 已提交
334 335 336
{
	struct csr1212_dentry *dentry;

337
	BUG_ON(!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY);
L
Linus Torvalds 已提交
338 339 340

	dentry = CSR1212_MALLOC(sizeof(*dentry));
	if (!dentry)
341
		return -ENOMEM;
L
Linus Torvalds 已提交
342

343 344
	if (keep_keyval)
		csr1212_keep_keyval(kv);
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	dentry->kv = kv;

	dentry->next = NULL;
	dentry->prev = dir->value.directory.dentries_tail;

	if (!dir->value.directory.dentries_head)
		dir->value.directory.dentries_head = dentry;

	if (dir->value.directory.dentries_tail)
		dir->value.directory.dentries_tail->next = dentry;
	dir->value.directory.dentries_tail = dentry;

	return CSR1212_SUCCESS;
}

360 361 362 363 364 365
int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
				       struct csr1212_keyval *kv)
{
	return __csr1212_attach_keyval_to_directory(dir, kv, true);
}

366 367 368 369 370
#define CSR1212_DESCRIPTOR_LEAF_DATA(kv) \
	(&((kv)->value.leaf.data[1]))

#define CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, type) \
	((kv)->value.leaf.data[0] = \
371 372
	 cpu_to_be32(CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) | \
		     ((type) << CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)))
373 374
#define CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, spec_id) \
	((kv)->value.leaf.data[0] = \
375 376 377
	 cpu_to_be32((CSR1212_DESCRIPTOR_LEAF_TYPE(kv) << \
		      CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT) | \
		     ((spec_id) & CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)))
378 379

static struct csr1212_keyval *
380
csr1212_new_descriptor_leaf(u8 dtype, u32 specifier_id,
381
			    const void *data, size_t data_len)
L
Linus Torvalds 已提交
382 383 384 385 386 387 388 389 390 391 392
{
	struct csr1212_keyval *kv;

	kv = csr1212_new_leaf(CSR1212_KV_ID_DESCRIPTOR, NULL,
			      data_len + CSR1212_DESCRIPTOR_LEAF_OVERHEAD);
	if (!kv)
		return NULL;

	CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, dtype);
	CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, specifier_id);

393
	if (data)
L
Linus Torvalds 已提交
394 395 396 397 398
		memcpy(CSR1212_DESCRIPTOR_LEAF_DATA(kv), data, data_len);

	return kv;
}

399
/* Check if string conforms to minimal ASCII as per IEEE 1212 clause 7.4 */
L
Linus Torvalds 已提交
400 401 402
static int csr1212_check_minimal_ascii(const char *s)
{
	static const char minimal_ascii_table[] = {
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
					/*  1   2   4   8  16  32  64  128 */
		128,			/* --, --, --, --, --, --, --, 07, */
		4 + 16 + 32,		/* --, --, 0a, --, 0C, 0D, --, --, */
		0,			/* --, --, --, --, --, --, --, --, */
		0,			/* --, --, --, --, --, --, --, --, */
		255 - 8 - 16,		/* 20, 21, 22, --, --, 25, 26, 27, */
		255,			/* 28, 29, 2a, 2b, 2c, 2d, 2e, 2f, */
		255,			/* 30, 31, 32, 33, 34, 35, 36, 37, */
		255,			/* 38, 39, 3a, 3b, 3c, 3d, 3e, 3f, */
		255,			/* 40, 41, 42, 43, 44, 45, 46, 47, */
		255,			/* 48, 49, 4a, 4b, 4c, 4d, 4e, 4f, */
		255,			/* 50, 51, 52, 53, 54, 55, 56, 57, */
		1 + 2 + 4 + 128,	/* 58, 59, 5a, --, --, --, --, 5f, */
		255 - 1,		/* --, 61, 62, 63, 64, 65, 66, 67, */
		255,			/* 68, 69, 6a, 6b, 6c, 6d, 6e, 6f, */
		255,			/* 70, 71, 72, 73, 74, 75, 76, 77, */
		1 + 2 + 4,		/* 78, 79, 7a, --, --, --, --, --, */
L
Linus Torvalds 已提交
420
	};
421 422
	int i, j;

L
Linus Torvalds 已提交
423
	for (; *s; s++) {
424 425 426 427 428 429
		i = *s >> 3;		/*  i = *s / 8;		*/
		j = 1 << (*s & 3);	/*  j = 1 << (*s % 8);	*/

		if (i >= ARRAY_SIZE(minimal_ascii_table) ||
		    !(minimal_ascii_table[i] & j))
			return -EINVAL;
L
Linus Torvalds 已提交
430 431 432 433
	}
	return 0;
}

434
/* IEEE 1212 clause 7.5.4.1 textual descriptors (English, minimal ASCII) */
L
Linus Torvalds 已提交
435 436
struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s)
{
437 438 439 440 441 442 443 444 445 446 447 448
	struct csr1212_keyval *kv;
	u32 *text;
	size_t str_len, quads;

	if (!s || !*s || csr1212_check_minimal_ascii(s))
		return NULL;

	str_len = strlen(s);
	quads = bytes_to_quads(str_len);
	kv = csr1212_new_descriptor_leaf(0, 0, NULL, quads_to_bytes(quads) +
				      CSR1212_TEXTUAL_DESCRIPTOR_LEAF_OVERHEAD);
	if (!kv)
L
Linus Torvalds 已提交
449 450
		return NULL;

451 452 453 454 455 456
	kv->value.leaf.data[1] = 0;	/* width, character_set, language */
	text = CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv);
	text[quads - 1] = 0;		/* padding */
	memcpy(text, s, str_len);

	return kv;
L
Linus Torvalds 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
}


/* Destruction Routines */

void csr1212_detach_keyval_from_directory(struct csr1212_keyval *dir,
					  struct csr1212_keyval *kv)
{
	struct csr1212_dentry *dentry;

	if (!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY)
		return;

	dentry = csr1212_find_keyval(dir, kv);

	if (!dentry)
		return;

	if (dentry->prev)
		dentry->prev->next = dentry->next;
	if (dentry->next)
		dentry->next->prev = dentry->prev;
	if (dir->value.directory.dentries_head == dentry)
		dir->value.directory.dentries_head = dentry->next;
	if (dir->value.directory.dentries_tail == dentry)
		dir->value.directory.dentries_tail = dentry->prev;

	CSR1212_FREE(dentry);

	csr1212_release_keyval(kv);
}

/* This function is used to free the memory taken by a keyval.  If the given
 * keyval is a directory type, then any keyvals contained in that directory
491
 * will be destroyed as well if noone holds a reference on them.  By means of
L
Linus Torvalds 已提交
492 493
 * list manipulation, this routine will descend a directory structure in a
 * non-recursive manner. */
494
void csr1212_release_keyval(struct csr1212_keyval *kv)
L
Linus Torvalds 已提交
495 496 497 498 499
{
	struct csr1212_keyval *k, *a;
	struct csr1212_dentry dentry;
	struct csr1212_dentry *head, *tail;

500 501 502
	if (!atomic_dec_and_test(&kv->refcnt))
		return;

L
Linus Torvalds 已提交
503 504 505 506 507 508 509 510 511 512 513
	dentry.kv = kv;
	dentry.next = NULL;
	dentry.prev = NULL;

	head = &dentry;
	tail = head;

	while (head) {
		k = head->kv;

		while (k) {
514 515
			/* must not dec_and_test kv->refcnt again */
			if (k != kv && !atomic_dec_and_test(&k->refcnt))
L
Linus Torvalds 已提交
516 517 518 519 520
				break;

			a = k->associate;

			if (k->key.type == CSR1212_KV_TYPE_DIRECTORY) {
S
Stefan Richter 已提交
521
				/* If the current entry is a directory, move all
L
Linus Torvalds 已提交
522 523
				 * the entries to the destruction list. */
				if (k->value.directory.dentries_head) {
S
Stefan Richter 已提交
524 525 526 527
					tail->next =
					    k->value.directory.dentries_head;
					k->value.directory.dentries_head->prev =
					    tail;
L
Linus Torvalds 已提交
528 529 530 531 532 533 534 535 536
					tail = k->value.directory.dentries_tail;
				}
			}
			free_keyval(k);
			k = a;
		}

		head = head->next;
		if (head) {
S
Stefan Richter 已提交
537
			if (head->prev && head->prev != &dentry)
L
Linus Torvalds 已提交
538 539
				CSR1212_FREE(head->prev);
			head->prev = NULL;
S
Stefan Richter 已提交
540
		} else if (tail != &dentry) {
L
Linus Torvalds 已提交
541
			CSR1212_FREE(tail);
S
Stefan Richter 已提交
542
		}
L
Linus Torvalds 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
	}
}

void csr1212_destroy_csr(struct csr1212_csr *csr)
{
	struct csr1212_csr_rom_cache *c, *oc;
	struct csr1212_cache_region *cr, *ocr;

	csr1212_release_keyval(csr->root_kv);

	c = csr->cache_head;
	while (c) {
		oc = c;
		cr = c->filled_head;
		while (cr) {
			ocr = cr;
			cr = cr->next;
			CSR1212_FREE(ocr);
		}
		c = c->next;
		CSR1212_FREE(oc);
	}

	CSR1212_FREE(csr);
}


/* CSR Image Creation */

static int csr1212_append_new_cache(struct csr1212_csr *csr, size_t romsize)
{
	struct csr1212_csr_rom_cache *cache;
575
	u64 csr_addr;
L
Linus Torvalds 已提交
576

577 578
	BUG_ON(!csr || !csr->ops || !csr->ops->allocate_addr_range ||
	       !csr->ops->release_addr || csr->max_rom < 1);
L
Linus Torvalds 已提交
579 580 581 582

	/* ROM size must be a multiple of csr->max_rom */
	romsize = (romsize + (csr->max_rom - 1)) & ~(csr->max_rom - 1);

S
Stefan Richter 已提交
583 584 585
	csr_addr = csr->ops->allocate_addr_range(romsize, csr->max_rom,
						 csr->private);
	if (csr_addr == CSR1212_INVALID_ADDR_SPACE)
586
		return -ENOMEM;
S
Stefan Richter 已提交
587

L
Linus Torvalds 已提交
588 589 590
	if (csr_addr < CSR1212_REGISTER_SPACE_BASE) {
		/* Invalid address returned from allocate_addr_range(). */
		csr->ops->release_addr(csr_addr, csr->private);
591
		return -ENOMEM;
L
Linus Torvalds 已提交
592 593
	}

S
Stefan Richter 已提交
594 595
	cache = csr1212_rom_cache_malloc(csr_addr - CSR1212_REGISTER_SPACE_BASE,
					 romsize);
L
Linus Torvalds 已提交
596 597
	if (!cache) {
		csr->ops->release_addr(csr_addr, csr->private);
598
		return -ENOMEM;
L
Linus Torvalds 已提交
599 600
	}

S
Stefan Richter 已提交
601 602
	cache->ext_rom = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF,
					    CSR1212_KV_ID_EXTENDED_ROM);
L
Linus Torvalds 已提交
603 604 605
	if (!cache->ext_rom) {
		csr->ops->release_addr(csr_addr, csr->private);
		CSR1212_FREE(cache);
606
		return -ENOMEM;
L
Linus Torvalds 已提交
607 608
	}

S
Stefan Richter 已提交
609 610
	if (csr1212_attach_keyval_to_directory(csr->root_kv, cache->ext_rom) !=
	    CSR1212_SUCCESS) {
L
Linus Torvalds 已提交
611 612 613
		csr1212_release_keyval(cache->ext_rom);
		csr->ops->release_addr(csr_addr, csr->private);
		CSR1212_FREE(cache);
614
		return -ENOMEM;
L
Linus Torvalds 已提交
615 616 617 618 619 620 621 622 623 624 625 626
	}
	cache->ext_rom->offset = csr_addr - CSR1212_REGISTER_SPACE_BASE;
	cache->ext_rom->value.leaf.len = -1;
	cache->ext_rom->value.leaf.data = cache->data;

	/* Add cache to tail of cache list */
	cache->prev = csr->cache_tail;
	csr->cache_tail->next = cache;
	csr->cache_tail = cache;
	return CSR1212_SUCCESS;
}

627 628
static void csr1212_remove_cache(struct csr1212_csr *csr,
				 struct csr1212_csr_rom_cache *cache)
L
Linus Torvalds 已提交
629 630 631 632 633 634 635 636 637 638 639 640
{
	if (csr->cache_head == cache)
		csr->cache_head = cache->next;
	if (csr->cache_tail == cache)
		csr->cache_tail = cache->prev;

	if (cache->prev)
		cache->prev->next = cache->next;
	if (cache->next)
		cache->next->prev = cache->prev;

	if (cache->ext_rom) {
S
Stefan Richter 已提交
641 642
		csr1212_detach_keyval_from_directory(csr->root_kv,
						     cache->ext_rom);
L
Linus Torvalds 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
		csr1212_release_keyval(cache->ext_rom);
	}

	CSR1212_FREE(cache);
}

static int csr1212_generate_layout_subdir(struct csr1212_keyval *dir,
					  struct csr1212_keyval **layout_tail)
{
	struct csr1212_dentry *dentry;
	struct csr1212_keyval *dkv;
	struct csr1212_keyval *last_extkey_spec = NULL;
	struct csr1212_keyval *last_extkey = NULL;
	int num_entries = 0;

	for (dentry = dir->value.directory.dentries_head; dentry;
	     dentry = dentry->next) {
		for (dkv = dentry->kv; dkv; dkv = dkv->associate) {
			/* Special Case: Extended Key Specifier_ID */
S
Stefan Richter 已提交
662 663 664
			if (dkv->key.id ==
			    CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
				if (last_extkey_spec == NULL)
L
Linus Torvalds 已提交
665
					last_extkey_spec = dkv;
S
Stefan Richter 已提交
666 667
				else if (dkv->value.immediate !=
					 last_extkey_spec->value.immediate)
L
Linus Torvalds 已提交
668
					last_extkey_spec = dkv;
S
Stefan Richter 已提交
669
				else
L
Linus Torvalds 已提交
670 671 672
					continue;
			/* Special Case: Extended Key */
			} else if (dkv->key.id == CSR1212_KV_ID_EXTENDED_KEY) {
S
Stefan Richter 已提交
673
				if (last_extkey == NULL)
L
Linus Torvalds 已提交
674
					last_extkey = dkv;
S
Stefan Richter 已提交
675 676
				else if (dkv->value.immediate !=
					 last_extkey->value.immediate)
L
Linus Torvalds 已提交
677
					last_extkey = dkv;
S
Stefan Richter 已提交
678
				else
L
Linus Torvalds 已提交
679 680 681 682 683
					continue;
			}

			num_entries += 1;

S
Stefan Richter 已提交
684
			switch (dkv->key.type) {
L
Linus Torvalds 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
			default:
			case CSR1212_KV_TYPE_IMMEDIATE:
			case CSR1212_KV_TYPE_CSR_OFFSET:
				break;
			case CSR1212_KV_TYPE_LEAF:
			case CSR1212_KV_TYPE_DIRECTORY:
				/* Remove from list */
				if (dkv->prev && (dkv->prev->next == dkv))
					dkv->prev->next = dkv->next;
				if (dkv->next && (dkv->next->prev == dkv))
					dkv->next->prev = dkv->prev;
				//if (dkv == *layout_tail)
				//	*layout_tail = dkv->prev;

				/* Special case: Extended ROM leafs */
				if (dkv->key.id == CSR1212_KV_ID_EXTENDED_ROM) {
					dkv->value.leaf.len = -1;
S
Stefan Richter 已提交
702 703 704
					/* Don't add Extended ROM leafs in the
					 * layout list, they are handled
					 * differently. */
L
Linus Torvalds 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
					break;
				}

				/* Add to tail of list */
				dkv->next = NULL;
				dkv->prev = *layout_tail;
				(*layout_tail)->next = dkv;
				*layout_tail = dkv;
				break;
			}
		}
	}
	return num_entries;
}

720
static size_t csr1212_generate_layout_order(struct csr1212_keyval *kv)
L
Linus Torvalds 已提交
721 722 723 724
{
	struct csr1212_keyval *ltail = kv;
	size_t agg_size = 0;

S
Stefan Richter 已提交
725 726
	while (kv) {
		switch (kv->key.type) {
L
Linus Torvalds 已提交
727 728 729 730 731 732
		case CSR1212_KV_TYPE_LEAF:
			/* Add 1 quadlet for crc/len field */
			agg_size += kv->value.leaf.len + 1;
			break;

		case CSR1212_KV_TYPE_DIRECTORY:
S
Stefan Richter 已提交
733 734
			kv->value.directory.len =
				csr1212_generate_layout_subdir(kv, &ltail);
L
Linus Torvalds 已提交
735 736 737 738 739 740 741 742 743
			/* Add 1 quadlet for crc/len field */
			agg_size += kv->value.directory.len + 1;
			break;
		}
		kv = kv->next;
	}
	return quads_to_bytes(agg_size);
}

744 745 746
static struct csr1212_keyval *
csr1212_generate_positions(struct csr1212_csr_rom_cache *cache,
			   struct csr1212_keyval *start_kv, int start_pos)
L
Linus Torvalds 已提交
747 748 749 750 751 752 753 754
{
	struct csr1212_keyval *kv = start_kv;
	struct csr1212_keyval *okv = start_kv;
	int pos = start_pos;
	int kv_len = 0, okv_len = 0;

	cache->layout_head = kv;

S
Stefan Richter 已提交
755
	while (kv && pos < cache->size) {
L
Linus Torvalds 已提交
756
		/* Special case: Extended ROM leafs */
S
Stefan Richter 已提交
757
		if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
L
Linus Torvalds 已提交
758 759
			kv->offset = cache->offset + pos;

S
Stefan Richter 已提交
760
		switch (kv->key.type) {
L
Linus Torvalds 已提交
761 762 763 764 765 766 767 768 769 770
		case CSR1212_KV_TYPE_LEAF:
			kv_len = kv->value.leaf.len;
			break;

		case CSR1212_KV_TYPE_DIRECTORY:
			kv_len = kv->value.directory.len;
			break;

		default:
			/* Should never get here */
771
			WARN_ON(1);
L
Linus Torvalds 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784
			break;
		}

		pos += quads_to_bytes(kv_len + 1);

		if (pos <= cache->size) {
			okv = kv;
			okv_len = kv_len;
			kv = kv->next;
		}
	}

	cache->layout_tail = okv;
S
Stefan Richter 已提交
785
	cache->len = okv->offset - cache->offset + quads_to_bytes(okv_len + 1);
L
Linus Torvalds 已提交
786 787 788 789

	return kv;
}

790 791 792 793 794 795
#define CSR1212_KV_KEY_SHIFT		24
#define CSR1212_KV_KEY_TYPE_SHIFT	6
#define CSR1212_KV_KEY_ID_MASK		0x3f
#define CSR1212_KV_KEY_TYPE_MASK	0x3	/* after shift */

static void
796
csr1212_generate_tree_subdir(struct csr1212_keyval *dir, u32 *data_buffer)
L
Linus Torvalds 已提交
797 798 799 800 801 802
{
	struct csr1212_dentry *dentry;
	struct csr1212_keyval *last_extkey_spec = NULL;
	struct csr1212_keyval *last_extkey = NULL;
	int index = 0;

S
Stefan Richter 已提交
803 804 805
	for (dentry = dir->value.directory.dentries_head;
	     dentry;
	     dentry = dentry->next) {
L
Linus Torvalds 已提交
806 807 808
		struct csr1212_keyval *a;

		for (a = dentry->kv; a; a = a->associate) {
809
			u32 value = 0;
L
Linus Torvalds 已提交
810 811

			/* Special Case: Extended Key Specifier_ID */
S
Stefan Richter 已提交
812 813 814
			if (a->key.id ==
			    CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
				if (last_extkey_spec == NULL)
L
Linus Torvalds 已提交
815
					last_extkey_spec = a;
S
Stefan Richter 已提交
816 817
				else if (a->value.immediate !=
					 last_extkey_spec->value.immediate)
L
Linus Torvalds 已提交
818
					last_extkey_spec = a;
S
Stefan Richter 已提交
819
				else
L
Linus Torvalds 已提交
820
					continue;
S
Stefan Richter 已提交
821

L
Linus Torvalds 已提交
822 823
			/* Special Case: Extended Key */
			} else if (a->key.id == CSR1212_KV_ID_EXTENDED_KEY) {
S
Stefan Richter 已提交
824
				if (last_extkey == NULL)
L
Linus Torvalds 已提交
825
					last_extkey = a;
S
Stefan Richter 已提交
826 827
				else if (a->value.immediate !=
					 last_extkey->value.immediate)
L
Linus Torvalds 已提交
828
					last_extkey = a;
S
Stefan Richter 已提交
829
				else
L
Linus Torvalds 已提交
830 831 832
					continue;
			}

S
Stefan Richter 已提交
833
			switch (a->key.type) {
L
Linus Torvalds 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
			case CSR1212_KV_TYPE_IMMEDIATE:
				value = a->value.immediate;
				break;
			case CSR1212_KV_TYPE_CSR_OFFSET:
				value = a->value.csr_offset;
				break;
			case CSR1212_KV_TYPE_LEAF:
				value = a->offset;
				value -= dir->offset + quads_to_bytes(1+index);
				value = bytes_to_quads(value);
				break;
			case CSR1212_KV_TYPE_DIRECTORY:
				value = a->offset;
				value -= dir->offset + quads_to_bytes(1+index);
				value = bytes_to_quads(value);
				break;
			default:
				/* Should never get here */
852 853
				WARN_ON(1);
				break;
L
Linus Torvalds 已提交
854 855
			}

S
Stefan Richter 已提交
856 857
			value |= (a->key.id & CSR1212_KV_KEY_ID_MASK) <<
				 CSR1212_KV_KEY_SHIFT;
L
Linus Torvalds 已提交
858
			value |= (a->key.type & CSR1212_KV_KEY_TYPE_MASK) <<
S
Stefan Richter 已提交
859 860
				 (CSR1212_KV_KEY_SHIFT +
				  CSR1212_KV_KEY_TYPE_SHIFT);
861
			data_buffer[index] = cpu_to_be32(value);
L
Linus Torvalds 已提交
862 863 864 865 866
			index++;
		}
	}
}

867
struct csr1212_keyval_img {
868 869
	u16 length;
	u16 crc;
870 871

	/* Must be last */
872
	u32 data[0];	/* older gcc can't handle [] which is standard */
873 874 875
};

static void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache)
L
Linus Torvalds 已提交
876 877 878 879
{
	struct csr1212_keyval *kv, *nkv;
	struct csr1212_keyval_img *kvi;

S
Stefan Richter 已提交
880 881 882 883 884 885
	for (kv = cache->layout_head;
	     kv != cache->layout_tail->next;
	     kv = nkv) {
		kvi = (struct csr1212_keyval_img *)(cache->data +
				bytes_to_quads(kv->offset - cache->offset));
		switch (kv->key.type) {
L
Linus Torvalds 已提交
886 887 888 889
		default:
		case CSR1212_KV_TYPE_IMMEDIATE:
		case CSR1212_KV_TYPE_CSR_OFFSET:
			/* Should never get here */
890 891
			WARN_ON(1);
			break;
L
Linus Torvalds 已提交
892 893 894 895 896 897 898 899

		case CSR1212_KV_TYPE_LEAF:
			/* Don't copy over Extended ROM areas, they are
			 * already filled out! */
			if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
				memcpy(kvi->data, kv->value.leaf.data,
				       quads_to_bytes(kv->value.leaf.len));

900
			kvi->length = cpu_to_be16(kv->value.leaf.len);
L
Linus Torvalds 已提交
901 902 903 904 905 906
			kvi->crc = csr1212_crc16(kvi->data, kv->value.leaf.len);
			break;

		case CSR1212_KV_TYPE_DIRECTORY:
			csr1212_generate_tree_subdir(kv, kvi->data);

907
			kvi->length = cpu_to_be16(kv->value.directory.len);
S
Stefan Richter 已提交
908 909
			kvi->crc = csr1212_crc16(kvi->data,
						 kv->value.directory.len);
L
Linus Torvalds 已提交
910 911 912 913 914 915 916 917 918 919 920 921 922
			break;
		}

		nkv = kv->next;
		if (kv->prev)
			kv->prev->next = NULL;
		if (kv->next)
			kv->next->prev = NULL;
		kv->prev = NULL;
		kv->next = NULL;
	}
}

923 924 925
/* This size is arbitrarily chosen.
 * The struct overhead is subtracted for more economic allocations. */
#define CSR1212_EXTENDED_ROM_SIZE (2048 - sizeof(struct csr1212_csr_rom_cache))
926

L
Linus Torvalds 已提交
927 928 929 930 931 932 933 934 935
int csr1212_generate_csr_image(struct csr1212_csr *csr)
{
	struct csr1212_bus_info_block_img *bi;
	struct csr1212_csr_rom_cache *cache;
	struct csr1212_keyval *kv;
	size_t agg_size;
	int ret;
	int init_offset;

936
	BUG_ON(!csr);
L
Linus Torvalds 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952

	cache = csr->cache_head;

	bi = (struct csr1212_bus_info_block_img*)cache->data;

	bi->length = bytes_to_quads(csr->bus_info_len) - 1;
	bi->crc_length = bi->length;
	bi->crc = csr1212_crc16(bi->data, bi->crc_length);

	csr->root_kv->next = NULL;
	csr->root_kv->prev = NULL;

	agg_size = csr1212_generate_layout_order(csr->root_kv);

	init_offset = csr->bus_info_len;

S
Stefan Richter 已提交
953 954 955
	for (kv = csr->root_kv, cache = csr->cache_head;
	     kv;
	     cache = cache->next) {
L
Linus Torvalds 已提交
956 957 958 959 960
		if (!cache) {
			/* Estimate approximate number of additional cache
			 * regions needed (it assumes that the cache holding
			 * the first 1K Config ROM space always exists). */
			int est_c = agg_size / (CSR1212_EXTENDED_ROM_SIZE -
961
						(2 * sizeof(u32))) + 1;
L
Linus Torvalds 已提交
962 963 964 965

			/* Add additional cache regions, extras will be
			 * removed later */
			for (; est_c; est_c--) {
S
Stefan Richter 已提交
966 967
				ret = csr1212_append_new_cache(csr,
						CSR1212_EXTENDED_ROM_SIZE);
L
Linus Torvalds 已提交
968 969 970 971 972 973 974 975 976 977 978
				if (ret != CSR1212_SUCCESS)
					return ret;
			}
			/* Need to re-layout for additional cache regions */
			agg_size = csr1212_generate_layout_order(csr->root_kv);
			kv = csr->root_kv;
			cache = csr->cache_head;
			init_offset = csr->bus_info_len;
		}
		kv = csr1212_generate_positions(cache, kv, init_offset);
		agg_size -= cache->len;
979
		init_offset = sizeof(u32);
L
Linus Torvalds 已提交
980 981 982 983 984 985 986 987 988 989 990 991
	}

	/* Remove unused, excess cache regions */
	while (cache) {
		struct csr1212_csr_rom_cache *oc = cache;

		cache = cache->next;
		csr1212_remove_cache(csr, oc);
	}

	/* Go through the list backward so that when done, the correct CRC
	 * will be calculated for the Extended ROM areas. */
S
Stefan Richter 已提交
992
	for (cache = csr->cache_tail; cache; cache = cache->prev) {
L
Linus Torvalds 已提交
993 994 995 996 997 998
		/* Only Extended ROM caches should have this set. */
		if (cache->ext_rom) {
			int leaf_size;

			/* Make sure the Extended ROM leaf is a multiple of
			 * max_rom in size. */
999
			BUG_ON(csr->max_rom < 1);
L
Linus Torvalds 已提交
1000 1001 1002 1003 1004 1005 1006 1007
			leaf_size = (cache->len + (csr->max_rom - 1)) &
				~(csr->max_rom - 1);

			/* Zero out the unused ROM region */
			memset(cache->data + bytes_to_quads(cache->len), 0x00,
			       leaf_size - cache->len);

			/* Subtract leaf header */
1008
			leaf_size -= sizeof(u32);
L
Linus Torvalds 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

			/* Update the Extended ROM leaf length */
			cache->ext_rom->value.leaf.len =
				bytes_to_quads(leaf_size);
		} else {
			/* Zero out the unused ROM region */
			memset(cache->data + bytes_to_quads(cache->len), 0x00,
			       cache->size - cache->len);
		}

		/* Copy the data into the cache buffer */
		csr1212_fill_cache(cache);

		if (cache != csr->cache_head) {
			/* Set the length and CRC of the extended ROM. */
			struct csr1212_keyval_img *kvi =
				(struct csr1212_keyval_img*)cache->data;
1026
			u16 len = bytes_to_quads(cache->len) - 1;
L
Linus Torvalds 已提交
1027

1028 1029
			kvi->length = cpu_to_be16(len);
			kvi->crc = csr1212_crc16(kvi->data, len);
L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035
		}
	}

	return CSR1212_SUCCESS;
}

1036
int csr1212_read(struct csr1212_csr *csr, u32 offset, void *buffer, u32 len)
L
Linus Torvalds 已提交
1037 1038 1039
{
	struct csr1212_csr_rom_cache *cache;

S
Stefan Richter 已提交
1040
	for (cache = csr->cache_head; cache; cache = cache->next)
L
Linus Torvalds 已提交
1041 1042
		if (offset >= cache->offset &&
		    (offset + len) <= (cache->offset + cache->size)) {
S
Stefan Richter 已提交
1043 1044
			memcpy(buffer, &cache->data[
					bytes_to_quads(offset - cache->offset)],
L
Linus Torvalds 已提交
1045 1046 1047
			       len);
			return CSR1212_SUCCESS;
		}
S
Stefan Richter 已提交
1048

1049
	return -ENOENT;
L
Linus Torvalds 已提交
1050 1051
}

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
/*
 * Apparently there are many different wrong implementations of the CRC
 * algorithm.  We don't fail, we just warn... approximately once per GUID.
 */
static void
csr1212_check_crc(const u32 *buffer, size_t length, u16 crc, __be32 *guid)
{
	static u64 last_bad_eui64;
	u64 eui64 = ((u64)be32_to_cpu(guid[0]) << 32) | be32_to_cpu(guid[1]);

	if (csr1212_crc16(buffer, length) == crc ||
	    csr1212_msft_crc16(buffer, length) == crc ||
	    eui64 == last_bad_eui64)
		return;

	printk(KERN_DEBUG "ieee1394: config ROM CRC error\n");
	last_bad_eui64 = eui64;
}
L
Linus Torvalds 已提交
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079

/* Parse a chunk of data as a Config ROM */

static int csr1212_parse_bus_info_block(struct csr1212_csr *csr)
{
	struct csr1212_bus_info_block_img *bi;
	struct csr1212_cache_region *cr;
	int i;
	int ret;

1080
	for (i = 0; i < csr->bus_info_len; i += sizeof(u32)) {
L
Linus Torvalds 已提交
1081
		ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
1082 1083
				&csr->cache_head->data[bytes_to_quads(i)],
				csr->private);
L
Linus Torvalds 已提交
1084 1085
		if (ret != CSR1212_SUCCESS)
			return ret;
1086 1087 1088

		/* check ROM header's info_length */
		if (i == 0 &&
1089
		    be32_to_cpu(csr->cache_head->data[0]) >> 24 !=
1090
		    bytes_to_quads(csr->bus_info_len) - 1)
1091
			return -EINVAL;
L
Linus Torvalds 已提交
1092 1093 1094 1095 1096
	}

	bi = (struct csr1212_bus_info_block_img*)csr->cache_head->data;
	csr->crc_len = quads_to_bytes(bi->crc_length);

S
Stefan Richter 已提交
1097 1098 1099
	/* IEEE 1212 recommends that crc_len be equal to bus_info_len, but that
	 * is not always the case, so read the rest of the crc area 1 quadlet at
	 * a time. */
1100
	for (i = csr->bus_info_len; i <= csr->crc_len; i += sizeof(u32)) {
L
Linus Torvalds 已提交
1101
		ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
1102 1103
				&csr->cache_head->data[bytes_to_quads(i)],
				csr->private);
L
Linus Torvalds 已提交
1104 1105 1106 1107
		if (ret != CSR1212_SUCCESS)
			return ret;
	}

1108 1109
	csr1212_check_crc(bi->data, bi->crc_length, bi->crc,
			  &csr->bus_info_data[3]);
L
Linus Torvalds 已提交
1110

S
Stefan Richter 已提交
1111
	cr = CSR1212_MALLOC(sizeof(*cr));
L
Linus Torvalds 已提交
1112
	if (!cr)
1113
		return -ENOMEM;
L
Linus Torvalds 已提交
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125

	cr->next = NULL;
	cr->prev = NULL;
	cr->offset_start = 0;
	cr->offset_end = csr->crc_len + 4;

	csr->cache_head->filled_head = cr;
	csr->cache_head->filled_tail = cr;

	return CSR1212_SUCCESS;
}

1126
#define CSR1212_KV_KEY(q)	(be32_to_cpu(q) >> CSR1212_KV_KEY_SHIFT)
1127 1128 1129
#define CSR1212_KV_KEY_TYPE(q)	(CSR1212_KV_KEY(q) >> CSR1212_KV_KEY_TYPE_SHIFT)
#define CSR1212_KV_KEY_ID(q)	(CSR1212_KV_KEY(q) & CSR1212_KV_KEY_ID_MASK)
#define CSR1212_KV_VAL_MASK	0xffffff
1130
#define CSR1212_KV_VAL(q)	(be32_to_cpu(q) & CSR1212_KV_VAL_MASK)
1131

S
Stefan Richter 已提交
1132 1133
static int
csr1212_parse_dir_entry(struct csr1212_keyval *dir, u32 ki, u32 kv_pos)
L
Linus Torvalds 已提交
1134 1135 1136
{
	int ret = CSR1212_SUCCESS;
	struct csr1212_keyval *k = NULL;
1137
	u32 offset;
1138
	bool keep_keyval = true;
L
Linus Torvalds 已提交
1139

S
Stefan Richter 已提交
1140
	switch (CSR1212_KV_KEY_TYPE(ki)) {
L
Linus Torvalds 已提交
1141 1142 1143 1144
	case CSR1212_KV_TYPE_IMMEDIATE:
		k = csr1212_new_immediate(CSR1212_KV_KEY_ID(ki),
					  CSR1212_KV_VAL(ki));
		if (!k) {
1145
			ret = -ENOMEM;
1146
			goto out;
L
Linus Torvalds 已提交
1147
		}
1148 1149
		/* Don't keep local reference when parsing. */
		keep_keyval = false;
L
Linus Torvalds 已提交
1150 1151 1152 1153 1154 1155
		break;

	case CSR1212_KV_TYPE_CSR_OFFSET:
		k = csr1212_new_csr_offset(CSR1212_KV_KEY_ID(ki),
					   CSR1212_KV_VAL(ki));
		if (!k) {
1156
			ret = -ENOMEM;
1157
			goto out;
L
Linus Torvalds 已提交
1158
		}
1159 1160
		/* Don't keep local reference when parsing. */
		keep_keyval = false;
L
Linus Torvalds 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169
		break;

	default:
		/* Compute the offset from 0xffff f000 0000. */
		offset = quads_to_bytes(CSR1212_KV_VAL(ki)) + kv_pos;
		if (offset == kv_pos) {
			/* Uh-oh.  Can't have a relative offset of 0 for Leaves
			 * or Directories.  The Config ROM image is most likely
			 * messed up, so we'll just abort here. */
1170
			ret = -EIO;
1171
			goto out;
L
Linus Torvalds 已提交
1172 1173 1174 1175 1176 1177 1178
		}

		k = csr1212_find_keyval_offset(dir, offset);

		if (k)
			break;		/* Found it. */

S
Stefan Richter 已提交
1179
		if (CSR1212_KV_KEY_TYPE(ki) == CSR1212_KV_TYPE_DIRECTORY)
L
Linus Torvalds 已提交
1180
			k = csr1212_new_directory(CSR1212_KV_KEY_ID(ki));
S
Stefan Richter 已提交
1181
		else
L
Linus Torvalds 已提交
1182
			k = csr1212_new_leaf(CSR1212_KV_KEY_ID(ki), NULL, 0);
S
Stefan Richter 已提交
1183

L
Linus Torvalds 已提交
1184
		if (!k) {
1185
			ret = -ENOMEM;
1186
			goto out;
L
Linus Torvalds 已提交
1187
		}
1188 1189 1190 1191
		/* Don't keep local reference when parsing. */
		keep_keyval = false;
		/* Contents not read yet so it's not valid. */
		k->valid = 0;
L
Linus Torvalds 已提交
1192 1193 1194 1195 1196 1197 1198
		k->offset = offset;

		k->prev = dir;
		k->next = dir->next;
		dir->next->prev = k;
		dir->next = k;
	}
1199
	ret = __csr1212_attach_keyval_to_directory(dir, k, keep_keyval);
1200
out:
1201 1202
	if (ret != CSR1212_SUCCESS && k != NULL)
		free_keyval(k);
L
Linus Torvalds 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
	return ret;
}

int csr1212_parse_keyval(struct csr1212_keyval *kv,
			 struct csr1212_csr_rom_cache *cache)
{
	struct csr1212_keyval_img *kvi;
	int i;
	int ret = CSR1212_SUCCESS;
	int kvi_len;

S
Stefan Richter 已提交
1214 1215
	kvi = (struct csr1212_keyval_img*)
		&cache->data[bytes_to_quads(kv->offset - cache->offset)];
1216
	kvi_len = be16_to_cpu(kvi->length);
L
Linus Torvalds 已提交
1217

1218 1219
	/* GUID is wrong in here in case of extended ROM.  We don't care. */
	csr1212_check_crc(kvi->data, kvi_len, kvi->crc, &cache->data[3]);
L
Linus Torvalds 已提交
1220

S
Stefan Richter 已提交
1221
	switch (kv->key.type) {
L
Linus Torvalds 已提交
1222 1223
	case CSR1212_KV_TYPE_DIRECTORY:
		for (i = 0; i < kvi_len; i++) {
1224
			u32 ki = kvi->data[i];
L
Linus Torvalds 已提交
1225 1226 1227 1228 1229 1230 1231

			/* Some devices put null entries in their unit
			 * directories.  If we come across such an entry,
			 * then skip it. */
			if (ki == 0x0)
				continue;
			ret = csr1212_parse_dir_entry(kv, ki,
S
Stefan Richter 已提交
1232
					kv->offset + quads_to_bytes(i + 1));
L
Linus Torvalds 已提交
1233 1234 1235 1236 1237 1238
		}
		kv->value.directory.len = kvi_len;
		break;

	case CSR1212_KV_TYPE_LEAF:
		if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) {
S
Stefan Richter 已提交
1239 1240 1241
			size_t size = quads_to_bytes(kvi_len);

			kv->value.leaf.data = CSR1212_MALLOC(size);
S
Stefan Richter 已提交
1242
			if (!kv->value.leaf.data) {
1243
				ret = -ENOMEM;
1244
				goto out;
L
Linus Torvalds 已提交
1245 1246 1247
			}

			kv->value.leaf.len = kvi_len;
S
Stefan Richter 已提交
1248
			memcpy(kv->value.leaf.data, kvi->data, size);
L
Linus Torvalds 已提交
1249 1250 1251 1252 1253
		}
		break;
	}

	kv->valid = 1;
1254
out:
L
Linus Torvalds 已提交
1255 1256 1257
	return ret;
}

1258 1259
static int
csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
L
Linus Torvalds 已提交
1260 1261 1262 1263 1264
{
	struct csr1212_cache_region *cr, *ncr, *newcr = NULL;
	struct csr1212_keyval_img *kvi = NULL;
	struct csr1212_csr_rom_cache *cache;
	int cache_index;
1265 1266 1267
	u64 addr;
	u32 *cache_ptr;
	u16 kv_len = 0;
L
Linus Torvalds 已提交
1268

1269
	BUG_ON(!csr || !kv || csr->max_rom < 1);
L
Linus Torvalds 已提交
1270 1271 1272

	/* First find which cache the data should be in (or go in if not read
	 * yet). */
S
Stefan Richter 已提交
1273
	for (cache = csr->cache_head; cache; cache = cache->next)
L
Linus Torvalds 已提交
1274 1275 1276 1277 1278
		if (kv->offset >= cache->offset &&
		    kv->offset < (cache->offset + cache->size))
			break;

	if (!cache) {
1279
		u32 q, cache_size;
L
Linus Torvalds 已提交
1280 1281 1282

		/* Only create a new cache for Extended ROM leaves. */
		if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
1283
			return -EINVAL;
L
Linus Torvalds 已提交
1284 1285 1286

		if (csr->ops->bus_read(csr,
				       CSR1212_REGISTER_SPACE_BASE + kv->offset,
1287
				       &q, csr->private))
1288
			return -EIO;
L
Linus Torvalds 已提交
1289

1290
		kv->value.leaf.len = be32_to_cpu(q) >> 16;
L
Linus Torvalds 已提交
1291 1292 1293 1294 1295 1296

		cache_size = (quads_to_bytes(kv->value.leaf.len + 1) +
			      (csr->max_rom - 1)) & ~(csr->max_rom - 1);

		cache = csr1212_rom_cache_malloc(kv->offset, cache_size);
		if (!cache)
1297
			return -ENOMEM;
L
Linus Torvalds 已提交
1298 1299 1300 1301 1302 1303 1304

		kv->value.leaf.data = &cache->data[1];
		csr->cache_tail->next = cache;
		cache->prev = csr->cache_tail;
		cache->next = NULL;
		csr->cache_tail = cache;
		cache->filled_head =
S
Stefan Richter 已提交
1305
			CSR1212_MALLOC(sizeof(*cache->filled_head));
S
Stefan Richter 已提交
1306
		if (!cache->filled_head)
1307
			return -ENOMEM;
L
Linus Torvalds 已提交
1308 1309

		cache->filled_head->offset_start = 0;
1310
		cache->filled_head->offset_end = sizeof(u32);
L
Linus Torvalds 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
		cache->filled_tail = cache->filled_head;
		cache->filled_head->next = NULL;
		cache->filled_head->prev = NULL;
		cache->data[0] = q;

		/* Don't read the entire extended ROM now.  Pieces of it will
		 * be read when entries inside it are read. */
		return csr1212_parse_keyval(kv, cache);
	}

	cache_index = kv->offset - cache->offset;

	/* Now seach read portions of the cache to see if it is there. */
	for (cr = cache->filled_head; cr; cr = cr->next) {
		if (cache_index < cr->offset_start) {
S
Stefan Richter 已提交
1326
			newcr = CSR1212_MALLOC(sizeof(*newcr));
L
Linus Torvalds 已提交
1327
			if (!newcr)
1328
				return -ENOMEM;
L
Linus Torvalds 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340

			newcr->offset_start = cache_index & ~(csr->max_rom - 1);
			newcr->offset_end = newcr->offset_start;
			newcr->next = cr;
			newcr->prev = cr->prev;
			cr->prev = newcr;
			cr = newcr;
			break;
		} else if ((cache_index >= cr->offset_start) &&
			   (cache_index < cr->offset_end)) {
			kvi = (struct csr1212_keyval_img*)
				(&cache->data[bytes_to_quads(cache_index)]);
1341
			kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1);
L
Linus Torvalds 已提交
1342
			break;
S
Stefan Richter 已提交
1343
		} else if (cache_index == cr->offset_end) {
L
Linus Torvalds 已提交
1344
			break;
S
Stefan Richter 已提交
1345
		}
L
Linus Torvalds 已提交
1346 1347 1348 1349
	}

	if (!cr) {
		cr = cache->filled_tail;
S
Stefan Richter 已提交
1350
		newcr = CSR1212_MALLOC(sizeof(*newcr));
L
Linus Torvalds 已提交
1351
		if (!newcr)
1352
			return -ENOMEM;
L
Linus Torvalds 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369

		newcr->offset_start = cache_index & ~(csr->max_rom - 1);
		newcr->offset_end = newcr->offset_start;
		newcr->prev = cr;
		newcr->next = cr->next;
		cr->next = newcr;
		cr = newcr;
		cache->filled_tail = newcr;
	}

	while(!kvi || cr->offset_end < cache_index + kv_len) {
		cache_ptr = &cache->data[bytes_to_quads(cr->offset_end &
							~(csr->max_rom - 1))];

		addr = (CSR1212_CSR_ARCH_REG_SPACE_BASE + cache->offset +
			cr->offset_end) & ~(csr->max_rom - 1);

1370 1371
		if (csr->ops->bus_read(csr, addr, cache_ptr, csr->private))
			return -EIO;
L
Linus Torvalds 已提交
1372 1373 1374 1375 1376 1377 1378

		cr->offset_end += csr->max_rom - (cr->offset_end &
						  (csr->max_rom - 1));

		if (!kvi && (cr->offset_end > cache_index)) {
			kvi = (struct csr1212_keyval_img*)
				(&cache->data[bytes_to_quads(cache_index)]);
1379
			kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1);
L
Linus Torvalds 已提交
1380 1381 1382 1383 1384 1385 1386
		}

		if ((kv_len + (kv->offset - cache->offset)) > cache->size) {
			/* The Leaf or Directory claims its length extends
			 * beyond the ConfigROM image region and thus beyond the
			 * end of our cache region.  Therefore, we abort now
			 * rather than seg faulting later. */
1387
			return -EIO;
L
Linus Torvalds 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
		}

		ncr = cr->next;

		if (ncr && (cr->offset_end >= ncr->offset_start)) {
			/* consolidate region entries */
			ncr->offset_start = cr->offset_start;

			if (cr->prev)
				cr->prev->next = cr->next;
			ncr->prev = cr->prev;
			if (cache->filled_head == cr)
				cache->filled_head = ncr;
			CSR1212_FREE(cr);
			cr = ncr;
		}
	}

	return csr1212_parse_keyval(kv, cache);
}

1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
struct csr1212_keyval *
csr1212_get_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
{
	if (!kv)
		return NULL;
	if (!kv->valid)
		if (csr1212_read_keyval(csr, kv) != CSR1212_SUCCESS)
			return NULL;
	return kv;
}

L
Linus Torvalds 已提交
1420 1421 1422 1423 1424
int csr1212_parse_csr(struct csr1212_csr *csr)
{
	struct csr1212_dentry *dentry;
	int ret;

1425
	BUG_ON(!csr || !csr->ops || !csr->ops->bus_read);
L
Linus Torvalds 已提交
1426 1427 1428 1429 1430

	ret = csr1212_parse_bus_info_block(csr);
	if (ret != CSR1212_SUCCESS)
		return ret;

1431 1432 1433 1434 1435 1436 1437
	/*
	 * There has been a buggy firmware with bus_info_block.max_rom > 0
	 * spotted which actually only supported quadlet read requests to the
	 * config ROM.  Therefore read everything quadlet by quadlet regardless
	 * of what the bus info block says.
	 */
	csr->max_rom = 4;
L
Linus Torvalds 已提交
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447

	csr->cache_head->layout_head = csr->root_kv;
	csr->cache_head->layout_tail = csr->root_kv;

	csr->root_kv->offset = (CSR1212_CONFIG_ROM_SPACE_BASE & 0xffff) +
		csr->bus_info_len;

	csr->root_kv->valid = 0;
	csr->root_kv->next = csr->root_kv;
	csr->root_kv->prev = csr->root_kv;
1448
	ret = csr1212_read_keyval(csr, csr->root_kv);
1449 1450
	if (ret != CSR1212_SUCCESS)
		return ret;
L
Linus Torvalds 已提交
1451 1452 1453 1454 1455

	/* Scan through the Root directory finding all extended ROM regions
	 * and make cache regions for them */
	for (dentry = csr->root_kv->value.directory.dentries_head;
	     dentry; dentry = dentry->next) {
J
Jody McIntyre 已提交
1456 1457
		if (dentry->kv->key.id == CSR1212_KV_ID_EXTENDED_ROM &&
			!dentry->kv->valid) {
1458
			ret = csr1212_read_keyval(csr, dentry->kv);
L
Linus Torvalds 已提交
1459 1460 1461 1462 1463 1464 1465
			if (ret != CSR1212_SUCCESS)
				return ret;
		}
	}

	return CSR1212_SUCCESS;
}