iPXE
pci_settings.c
Go to the documentation of this file.
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 <string.h>
28 #include <errno.h>
29 #include <ipxe/pci.h>
30 #include <ipxe/settings.h>
31 #include <ipxe/init.h>
32 
33 /** @file
34  *
35  * PCI device settings
36  *
37  */
38 
39 /** PCI device settings scope */
40 static const struct settings_scope pci_settings_scope;
41 
42 /**
43  * Check applicability of PCI device setting
44  *
45  * @v settings Settings block
46  * @v setting Setting
47  * @ret applies Setting applies within this settings block
48  */
50  const struct setting *setting ) {
51 
52  return ( setting->scope == &pci_settings_scope );
53 }
54 
55 /**
56  * Fetch value of PCI device setting
57  *
58  * @v settings Settings block
59  * @v setting Setting to fetch
60  * @v data Buffer to fill with setting data
61  * @v len Length of buffer
62  * @ret len Length of setting data, or negative error
63  */
65  struct setting *setting,
66  void *data, size_t len ) {
67  struct pci_device pci;
68  unsigned int tag_busdevfn;
69  unsigned int tag_offset;
70  unsigned int tag_len;
71  unsigned int i;
72 
73  /* Extract busdevfn, offset, and length from tag */
74  tag_busdevfn = ( setting->tag >> 16 );
75  tag_offset = ( ( setting->tag >> 8 ) & 0xff );
76  tag_len = ( ( setting->tag >> 0 ) & 0xff );
77 
78  /* Locate PCI device */
79  memset ( &pci, 0, sizeof ( pci ) );
80  pci_init ( &pci, tag_busdevfn );
81  DBG ( PCI_FMT " reading %#02x+%#x\n", PCI_ARGS ( &pci ),
82  tag_offset, tag_len );
83 
84  /* Read data one byte at a time, in reverse order (since PCI
85  * is little-endian and iPXE settings are essentially
86  * big-endian).
87  */
88  tag_offset += tag_len;
89  for ( i = 0 ; ( ( i < tag_len ) && ( i < len ) ); i++ ) {
90  pci_read_config_byte ( &pci, --tag_offset, data++ );
91  }
92 
93  /* Set type to ":hexraw" if not already specified */
94  if ( ! setting->type )
95  setting->type = &setting_type_hexraw;
96 
97  return tag_len;
98 }
99 
100 /** PCI device settings operations */
103  .fetch = pci_settings_fetch,
104 };
105 
106 /** PCI device settings */
107 static struct settings pci_settings = {
108  .refcnt = NULL,
109  .siblings = LIST_HEAD_INIT ( pci_settings.siblings ),
110  .children = LIST_HEAD_INIT ( pci_settings.children ),
112  .default_scope = &pci_settings_scope,
113 };
114 
115 /** Initialise PCI device settings */
116 static void pci_settings_init ( void ) {
117  int rc;
118 
119  if ( ( rc = register_settings ( &pci_settings, NULL, "pci" ) ) != 0 ) {
120  DBG ( "PCI could not register settings: %s\n",
121  strerror ( rc ) );
122  return;
123  }
124 }
125 
126 /** PCI device settings initialiser */
127 struct init_fn pci_settings_init_fn __init_fn ( INIT_NORMAL ) = {
128  .name = "pci",
129  .initialise = pci_settings_init,
130 };
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
Error codes.
static struct settings_operations pci_settings_operations
PCI device settings operations.
Definition: pci_settings.c:101
static int pci_settings_applies(struct settings *settings __unused, const struct setting *setting)
Check applicability of PCI device setting.
Definition: pci_settings.c:49
uint64_t tag
Setting tag, if applicable.
Definition: settings.h:43
#define INIT_NORMAL
Normal initialisation.
Definition: init.h:31
static const struct settings_scope pci_settings_scope
PCI device settings scope.
Definition: pci_settings.c:40
An initialisation function.
Definition: init.h:14
static int pci_settings_fetch(struct settings *settings __unused, struct setting *setting, void *data, size_t len)
Fetch value of PCI device setting.
Definition: pci_settings.c:64
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
ring len
Length.
Definition: dwmac.h:231
const char * name
Definition: init.h:15
const struct setting_type * type
Setting type.
Definition: settings.h:36
Configuration settings.
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define PCI_FMT
PCI device debug message format.
Definition: pci.h:311
struct init_fn pci_settings_init_fn __init_fn(INIT_NORMAL)
PCI device settings initialiser.
PCI bus.
A PCI device.
Definition: pci.h:210
Settings block operations.
Definition: settings.h:85
A settings block.
Definition: settings.h:132
static struct settings pci_settings
PCI device settings.
Definition: pci_settings.c:107
A setting scope.
Definition: settings.h:176
struct list_head siblings
Sibling settings blocks.
Definition: settings.h:140
A setting.
Definition: settings.h:23
#define PCI_ARGS(pci)
PCI device debug message arguments.
Definition: pci.h:314
uint8_t data[48]
Additional event data.
Definition: ena.h:22
int(* applies)(struct settings *settings, const struct setting *setting)
Check applicability of setting.
Definition: settings.h:98
struct list_head children
Child settings blocks.
Definition: settings.h:142
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition: settings.c:475
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static void pci_init(struct pci_device *pci, unsigned int busdevfn)
Initialise PCI device.
Definition: pci.h:340
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition: list.h:30
static void pci_settings_init(void)
Initialise PCI device settings.
Definition: pci_settings.c:116
const struct settings_scope * scope
Setting scope (or NULL)
Definition: settings.h:49
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
struct refcnt * refcnt
Reference counter.
Definition: settings.h:134
void * memset(void *dest, int character, size_t len) __nonnull
int pci_read_config_byte(struct pci_device *pci, unsigned int where, uint8_t *value)
Read byte from PCI configuration space.