summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/drivers/bus/pcivpd.c
blob: 243b1f7796b38e9333927a2a8f6965dd52efd25d (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/pci.h>
#include <ipxe/isapnp.h>
#include <ipxe/pcivpd.h>

/** @file
 *
 * PCI Vital Product Data
 *
 */

/**
 * Initialise PCI Vital Product Data
 *
 * @v vpd		PCI VPD
 * @v pci		PCI device
 * @ret rc		Return status code
 */
int pci_vpd_init ( struct pci_vpd *vpd, struct pci_device *pci ) {

	/* Initialise structure */
	vpd->pci = pci;
	pci_vpd_invalidate_cache ( vpd );

	/* Locate VPD capability */
	vpd->cap = pci_find_capability ( pci, PCI_CAP_ID_VPD );
	if ( ! vpd->cap ) {
		DBGC ( vpd, PCI_FMT " does not support VPD\n",
		       PCI_ARGS ( pci ) );
		return -ENOTTY;
	}

	DBGC ( vpd, PCI_FMT " VPD is at offset %02x\n",
	       PCI_ARGS ( pci ), vpd->cap );
	return 0;
}

/**
 * Read one dword of PCI Vital Product Data
 *
 * @v vpd		PCI VPD
 * @v address		Address to read
 * @ret data		Read data
 * @ret rc		Return status code
 */
static int pci_vpd_read_dword ( struct pci_vpd *vpd, int address,
				uint32_t *data ) {
	struct pci_device *pci = vpd->pci;
	unsigned int cap = vpd->cap;
	unsigned int retries;
	uint16_t flag;

	/* Fail if no VPD present */
	if ( ! cap )
		return -ENOTTY;

	/* Return cached value, if present */
	if ( pci_vpd_cache_is_valid ( vpd ) &&
	     ( vpd->cache.address == address ) ) {
		*data = vpd->cache.data;
		return 0;
	}

	/* Initiate read */
	pci_write_config_word ( pci, ( cap + PCI_VPD_ADDRESS ), address );

	/* Wait for read to complete */
	for ( retries = 0 ; retries < PCI_VPD_MAX_WAIT_MS ; retries++ ) {

		/* Check if data is ready */
		pci_read_config_word ( pci, ( cap + PCI_VPD_ADDRESS ), &flag );
		if ( flag & PCI_VPD_FLAG ) {

			/* Read data */
			pci_read_config_dword ( pci, ( cap + PCI_VPD_DATA ),
						data );
			DBGC2 ( vpd, PCI_FMT " VPD %04x => %08x\n",
				PCI_ARGS ( pci ), address, htonl ( *data ) );

			/* Populate cache */
			vpd->cache.address = address;
			vpd->cache.data = *data;

			return 0;
		}

		/* Wait 1ms before retrying */
		mdelay ( 1 );
	}

	DBGC ( vpd, PCI_FMT " VPD %04x read via %02x timed out\n",
	       PCI_ARGS ( pci ), address, cap );
	return -ETIMEDOUT;
}

/**
 * Write one dword of PCI Vital Product Data
 *
 * @v vpd		PCI VPD
 * @v address		Address to write
 * @v data		Data to write
 * @ret rc		Return status code
 */
static int pci_vpd_write_dword ( struct pci_vpd *vpd, int address,
				 uint32_t data ) {
	struct pci_device *pci = vpd->pci;
	unsigned int cap = vpd->cap;
	unsigned int retries;
	uint16_t flag;

	/* Fail if no VPD present */
	if ( ! cap )
		return -ENOTTY;

	/* Invalidate cache */
	pci_vpd_invalidate_cache ( vpd );

	DBGC2 ( vpd, PCI_FMT " VPD %04x <= %08x\n",
		PCI_ARGS ( pci ), address, htonl ( data ) );

	/* Write data */
	pci_write_config_dword ( pci, ( cap + PCI_VPD_DATA ), data );

	/* Initiate write */
	pci_write_config_word ( pci, ( cap + PCI_VPD_ADDRESS ),
				( address | PCI_VPD_FLAG ) );

	/* Wait for write to complete */
	for ( retries = 0 ; retries < PCI_VPD_MAX_WAIT_MS ; retries++ ) {

		/* Check if write has completed */
		pci_read_config_word ( pci, ( cap + PCI_VPD_ADDRESS ), &flag );
		if ( ! ( flag & PCI_VPD_FLAG ) )
			return 0;

		/* Wait 1ms before retrying */
		mdelay ( 1 );
	}

	DBGC ( vpd, PCI_FMT " VPD %04x write via %02x timed out\n",
	       PCI_ARGS ( pci ), address, cap );
	return -ETIMEDOUT;
}

/**
 * Read PCI VPD
 *
 * @v vpd		PCI VPD
 * @v address		Starting address
 * @v buf		Data buffer
 * @v len		Length of data buffer
 * @ret rc		Return status code
 */
int pci_vpd_read ( struct pci_vpd *vpd, unsigned int address, void *buf,
		   size_t len ) {
	uint8_t *bytes = buf;
	uint32_t data;
	size_t skip_len;
	unsigned int i;
	int rc;

	/* Calculate length to skip at start of data */
	skip_len = ( address & 0x03 );

	/* Read data, a dword at a time */
	for ( address &= ~0x03 ; len ; address += 4 ) {

		/* Read whole dword */
		if ( ( rc = pci_vpd_read_dword ( vpd, address, &data ) ) != 0 )
			return rc;

		/* Copy data to buffer */
		for ( i = 4 ; i ; i-- ) {
			if ( skip_len ) {
				skip_len--;
			} else if ( len ) {
				*(bytes++) = data;
				len--;
			}
			data = ( ( data << 24 ) | ( data >> 8 ) );
		}
	}

	return 0;
}

/**
 * Write PCI VPD
 *
 * @v vpd		PCI VPD
 * @v address		Starting address
 * @v buf		Data buffer
 * @v len		Length of data buffer
 * @ret rc		Return status code
 */
int pci_vpd_write ( struct pci_vpd *vpd, unsigned int address, const void *buf,
		    size_t len ) {
	const uint8_t *bytes = buf;
	uint32_t data;
	size_t skip_len;
	unsigned int i;
	int rc;

	/* Calculate length to skip at start of data */
	skip_len = ( address & 0x03 );

	/* Write data, a dword at a time */
	for ( address &= ~0x03 ; len ; address += 4 ) {

		/* Read existing dword, if necessary */
		if ( skip_len || ( len <= 0x03 ) ) {
			if ( ( rc = pci_vpd_read_dword ( vpd, address,
							 &data ) ) != 0 )
				return rc;
		}

		/* Copy data from buffer */
		for ( i = 4 ; i ; i-- ) {
			if ( skip_len ) {
				skip_len--;
			} else if ( len ) {
				data = ( ( data & ~0xff ) | *(bytes++) );
				len--;
			}
			data = ( ( data << 24 ) | ( data >> 8 ) );
		}

		/* Write whole dword */
		if ( ( rc = pci_vpd_write_dword ( vpd, address, data ) ) != 0 )
			return rc;
	}
	return 0;
}

/**
 * Dump PCI VPD region (for debugging)
 *
 * @v vpd		PCI VPD
 * @v address		Starting address
 * @v len		Length of data
 */
static void pci_vpd_dump ( struct pci_vpd *vpd, unsigned int address,
			   size_t len ) {
	int rc;

	/* Do nothing in non-debug builds */
	if ( ! DBG_LOG )
		return;

	/* Read data */
	{
		char buf[len];
		if ( ( rc = pci_vpd_read ( vpd, address, buf,
					   sizeof ( buf ) ) ) != 0 )
			return;
		DBGC_HDA ( vpd, address, buf, sizeof ( buf ) );
	}
}

/**
 * Locate PCI VPD tag
 *
 * @v vpd		PCI VPD
 * @v tag		ISAPnP tag
 * @ret address		Address of tag body
 * @ret len		Length of tag body
 * @ret rc		Return status code
 */
static int pci_vpd_find_tag ( struct pci_vpd *vpd, unsigned int tag,
			      unsigned int *address, size_t *len ) {
	uint8_t read_tag;
	uint16_t read_len;
	int rc;

	/* Scan through tags looking for a match */
	*address = 0;
	do {
		/* Read tag byte */
		if ( ( rc = pci_vpd_read ( vpd, (*address)++, &read_tag,
					   sizeof ( read_tag ) ) ) != 0 )
			return rc;

		/* Extract tag and length */
		if ( ISAPNP_IS_LARGE_TAG ( read_tag ) ) {
			if ( ( rc = pci_vpd_read ( vpd, *address, &read_len,
						   sizeof ( read_len ) ) ) != 0)
				return rc;
			*address += sizeof ( read_len );
			read_len = le16_to_cpu ( read_len );
			read_tag = ISAPNP_LARGE_TAG_NAME ( read_tag );
		} else {
			read_len = ISAPNP_SMALL_TAG_LEN ( read_tag );
			read_tag = ISAPNP_SMALL_TAG_NAME ( read_tag );
		}

		/* Check for tag match */
		if ( tag == read_tag ) {
			*len = read_len;
			DBGC ( vpd, PCI_FMT " VPD tag %02x is at "
			       "[%04x,%04zx)\n", PCI_ARGS ( vpd->pci ), tag,
			       *address, ( *address + *len ) );
			return 0;
		}

		/* Move to next tag */
		*address += read_len;

	} while ( read_tag != ISAPNP_TAG_END );

	DBGC ( vpd, PCI_FMT " VPD tag %02x not found\n",
	       PCI_ARGS ( vpd->pci ), tag );
	return -ENOENT;
}

/**
 * Locate PCI VPD field
 *
 * @v vpd		PCI VPD
 * @v field		VPD field descriptor
 * @ret address		Address of field body
 * @ret len		Length of field body
 * @ret rc		Return status code
 */
int pci_vpd_find ( struct pci_vpd *vpd, unsigned int field,
		   unsigned int *address, size_t *len ) {
	struct pci_vpd_field read_field;
	int rc;

	/* Locate containing tag */
	if ( ( rc = pci_vpd_find_tag ( vpd, PCI_VPD_TAG ( field ),
				       address, len ) ) != 0 )
		return rc;

	/* Return immediately if we are searching for a whole-tag field */
	if ( ! PCI_VPD_KEYWORD ( field ) ) {
		pci_vpd_dump ( vpd, *address, *len );
		return 0;
	}

	/* Scan through fields looking for a match */
	while ( *len >= sizeof ( read_field ) ) {

		/* Read field header */
		if ( ( rc = pci_vpd_read ( vpd, *address, &read_field,
					   sizeof ( read_field ) ) ) != 0 )
			return rc;
		*address += sizeof ( read_field );
		*len -= sizeof ( read_field );

		/* Check for keyword match */
		if ( read_field.keyword == PCI_VPD_KEYWORD ( field ) ) {
			*len = read_field.len;
			DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT
			       " is at [%04x,%04zx)\n", PCI_ARGS ( vpd->pci ),
			       PCI_VPD_FIELD_ARGS ( field ),
			       *address, ( *address + *len ) );
			pci_vpd_dump ( vpd, *address, *len );
			return 0;
		}

		/* Move to next field */
		if ( read_field.len > *len )
			break;
		*address += read_field.len;
		*len -= read_field.len;
	}

	DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT " not found\n",
	       PCI_ARGS ( vpd->pci ), PCI_VPD_FIELD_ARGS ( field ) );
	return -ENOENT;
}

/**
 * Resize VPD field
 *
 * @v vpd		PCI VPD
 * @v field		VPD field descriptor
 * @v len		New length of field body
 * @ret address		Address of field body
 * @ret rc		Return status code
 */
int pci_vpd_resize ( struct pci_vpd *vpd, unsigned int field, size_t len,
		     unsigned int *address ) {
	struct pci_vpd_field rw_field;
	struct pci_vpd_field old_field;
	struct pci_vpd_field new_field;
	unsigned int rw_address;
	unsigned int old_address;
	unsigned int copy_address;
	unsigned int dst_address;
	unsigned int dump_address;
	size_t rw_len;
	size_t old_len;
	size_t available_len;
	size_t copy_len;
	size_t dump_len;
	void *copy;
	int rc;

	/* Sanity checks */
	assert ( PCI_VPD_TAG ( field ) == PCI_VPD_TAG_RW );
	assert ( PCI_VPD_KEYWORD ( field ) != 0 );
	assert ( field != PCI_VPD_FIELD_RW );

	/* Locate 'RW' field */
	if ( ( rc = pci_vpd_find ( vpd, PCI_VPD_FIELD_RW, &rw_address,
				   &rw_len ) ) != 0 )
		goto err_no_rw;

	/* Locate old field, if any */
	if ( ( rc = pci_vpd_find ( vpd, field, &old_address,
				   &old_len ) ) == 0 ) {

		/* Field already exists */
		if ( old_address > rw_address ) {
			DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT
			       " at [%04x,%04zx) is after field "
			       PCI_VPD_FIELD_FMT " at [%04x,%04zx)\n",
			       PCI_ARGS ( vpd->pci ),
			       PCI_VPD_FIELD_ARGS ( field ),
			       old_address, ( old_address + old_len ),
			       PCI_VPD_FIELD_ARGS ( PCI_VPD_FIELD_RW ),
			       rw_address, ( rw_address + rw_len ) );
			rc = -ENXIO;
			goto err_after_rw;
		}
		dst_address = ( old_address - sizeof ( old_field ) );
		copy_address = ( old_address + old_len );
		copy_len = ( rw_address - sizeof ( rw_field ) - copy_address );

		/* Calculate available length */
		available_len = ( rw_len + old_len );

	} else {

		/* Field does not yet exist */
		dst_address = ( rw_address - sizeof ( rw_field ) );
		copy_address = dst_address;
		copy_len = 0;

		/* Calculate available length */
		available_len = ( ( rw_len > sizeof ( new_field ) ) ?
				  ( rw_len - sizeof ( new_field ) ) : 0 );
	}

	/* Dump region before changes */
	dump_address = dst_address;
	dump_len = ( rw_address + rw_len - dump_address );
	DBGC ( vpd, PCI_FMT " VPD before resizing field " PCI_VPD_FIELD_FMT
	       " to %zd bytes:\n", PCI_ARGS ( vpd->pci ),
	       PCI_VPD_FIELD_ARGS ( field ), len );
	pci_vpd_dump ( vpd, dump_address, dump_len );

	/* Check available length */
	if ( available_len > PCI_VPD_MAX_LEN )
		available_len = PCI_VPD_MAX_LEN;
	if ( len > available_len ) {
		DBGC ( vpd, PCI_FMT " VPD no space for field "
		       PCI_VPD_FIELD_FMT " (need %02zx, have %02zx)\n",
		       PCI_ARGS ( vpd->pci ), PCI_VPD_FIELD_ARGS ( field ),
		       len, available_len );
		rc = -ENOSPC;
		goto err_no_space;
	}

	/* Preserve intermediate fields, if any */
	copy = malloc ( copy_len );
	if ( ! copy ) {
		rc = -ENOMEM;
		goto err_copy_alloc;
	}
	if ( ( rc = pci_vpd_read ( vpd, copy_address, copy, copy_len ) ) != 0 )
		goto err_copy_read;

	/* Create new field, if applicable */
	if ( len ) {
		new_field.keyword = PCI_VPD_KEYWORD ( field );
		new_field.len = len;
		if ( ( rc = pci_vpd_write ( vpd, dst_address, &new_field,
					    sizeof ( new_field ) ) ) != 0 )
			goto err_new_write;
		dst_address += sizeof ( new_field );
		*address = dst_address;
		DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT " is now "
		       "at [%04x,%04x)\n", PCI_ARGS ( vpd->pci ),
		       PCI_VPD_FIELD_ARGS ( field ), dst_address,
		       ( dst_address + new_field.len ) );
		dst_address += len;
	} else {
		DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT
		       " no longer exists\n", PCI_ARGS ( vpd->pci ),
		       PCI_VPD_FIELD_ARGS ( field ) );
	}

	/* Restore intermediate fields, if any */
	if ( ( rc = pci_vpd_write ( vpd, dst_address, copy, copy_len ) ) != 0 )
		goto err_copy_write;
	dst_address += copy_len;

	/* Create 'RW' field */
	rw_field.keyword = PCI_VPD_KEYWORD ( PCI_VPD_FIELD_RW );
	rw_field.len = ( rw_len +
			 ( rw_address - sizeof ( rw_field ) ) - dst_address );
	if ( ( rc = pci_vpd_write ( vpd, dst_address, &rw_field,
				    sizeof ( rw_field ) ) ) != 0 )
		goto err_rw_write;
	dst_address += sizeof ( rw_field );
	DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT " is now "
	       "at [%04x,%04x)\n", PCI_ARGS ( vpd->pci ),
	       PCI_VPD_FIELD_ARGS ( PCI_VPD_FIELD_RW ), dst_address,
	       ( dst_address + rw_field.len ) );

	/* Dump region after changes */
	DBGC ( vpd, PCI_FMT " VPD after resizing field " PCI_VPD_FIELD_FMT
	       " to %zd bytes:\n", PCI_ARGS ( vpd->pci ),
	       PCI_VPD_FIELD_ARGS ( field ), len );
	pci_vpd_dump ( vpd, dump_address, dump_len );

	rc = 0;

 err_rw_write:
 err_new_write:
 err_copy_write:
 err_copy_read:
	free ( copy );
 err_copy_alloc:
 err_no_space:
 err_after_rw:
 err_no_rw:
	return rc;
}