These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / bus / pci_settings.c
1 /*
2  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <ipxe/pci.h>
29 #include <ipxe/settings.h>
30 #include <ipxe/init.h>
31
32 /** @file
33  *
34  * PCI device settings
35  *
36  */
37
38 /** PCI device settings scope */
39 static const struct settings_scope pci_settings_scope;
40
41 /**
42  * Check applicability of PCI device setting
43  *
44  * @v settings          Settings block
45  * @v setting           Setting
46  * @ret applies         Setting applies within this settings block
47  */
48 static int pci_settings_applies ( struct settings *settings __unused,
49                                   const struct setting *setting ) {
50
51         return ( setting->scope == &pci_settings_scope );
52 }
53
54 /**
55  * Fetch value of PCI device setting
56  *
57  * @v settings          Settings block
58  * @v setting           Setting to fetch
59  * @v data              Buffer to fill with setting data
60  * @v len               Length of buffer
61  * @ret len             Length of setting data, or negative error
62  */
63 static int pci_settings_fetch ( struct settings *settings __unused,
64                                 struct setting *setting,
65                                 void *data, size_t len ) {
66         struct pci_device pci;
67         unsigned int tag_busdevfn;
68         unsigned int tag_offset;
69         unsigned int tag_len;
70         unsigned int i;
71
72         /* Extract busdevfn, offset, and length from tag */
73         tag_busdevfn = ( ( setting->tag >> 16 ) & 0xffff );
74         tag_offset = ( ( setting->tag >> 8 ) & 0xff );
75         tag_len = ( ( setting->tag >> 0 ) & 0xff );
76
77         /* Locate PCI device */
78         memset ( &pci, 0, sizeof ( pci ) );
79         pci_init ( &pci, tag_busdevfn );
80         DBG ( PCI_FMT " reading %#02x+%#x\n", PCI_ARGS ( &pci ),
81               tag_offset, tag_len );
82
83         /* Read data one byte at a time, in reverse order (since PCI
84          * is little-endian and iPXE settings are essentially
85          * big-endian).
86          */
87         tag_offset += tag_len;
88         for ( i = 0 ; ( ( i < tag_len ) && ( i < len ) ); i++ ) {
89                 pci_read_config_byte ( &pci, --tag_offset, data++ );
90         }
91
92         /* Set type to ":hexraw" if not already specified */
93         if ( ! setting->type )
94                 setting->type = &setting_type_hexraw;
95
96         return tag_len;
97 }
98
99 /** PCI device settings operations */
100 static struct settings_operations pci_settings_operations = {
101         .applies = pci_settings_applies,
102         .fetch = pci_settings_fetch,
103 };
104
105 /** PCI device settings */
106 static struct settings pci_settings = {
107         .refcnt = NULL,
108         .siblings = LIST_HEAD_INIT ( pci_settings.siblings ),
109         .children = LIST_HEAD_INIT ( pci_settings.children ),
110         .op = &pci_settings_operations,
111         .default_scope = &pci_settings_scope,
112 };
113
114 /** Initialise PCI device settings */
115 static void pci_settings_init ( void ) {
116         int rc;
117
118         if ( ( rc = register_settings ( &pci_settings, NULL, "pci" ) ) != 0 ) {
119                 DBG ( "PCI could not register settings: %s\n",
120                       strerror ( rc ) );
121                 return;
122         }
123 }
124
125 /** PCI device settings initialiser */
126 struct init_fn pci_settings_init_fn __init_fn ( INIT_NORMAL ) = {
127         .initialise = pci_settings_init,
128 };