Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / commands / pci_cmd.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 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
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <ipxe/pci.h>
23 #include <ipxe/command.h>
24 #include <ipxe/parseopt.h>
25
26 FILE_LICENCE ( GPL2_OR_LATER );
27
28 /** @file
29  *
30  * PCI commands
31  *
32  */
33
34 /** "pciscan" options */
35 struct pciscan_options {};
36
37 /** "pciscan" option list */
38 static struct option_descriptor pciscan_opts[] = {};
39
40 /** "pciscan" command descriptor */
41 static struct command_descriptor pciscan_cmd =
42         COMMAND_DESC ( struct pciscan_options, pciscan_opts, 1, 1,
43                        "<setting>" );
44
45 /**
46  * "pciscan" command
47  *
48  * @v argc              Argument count
49  * @v argv              Argument list
50  * @ret rc              Return status code
51  */
52 static int pciscan_exec ( int argc, char **argv ) {
53         struct pciscan_options opts;
54         struct named_setting setting;
55         struct pci_device pci;
56         unsigned long prev;
57         int next;
58         int len;
59         int rc;
60
61         /* Parse options */
62         if ( ( rc = parse_options ( argc, argv, &pciscan_cmd, &opts ) ) != 0 )
63                 goto err_parse_options;
64
65         /* Parse setting name */
66         if ( ( rc = parse_autovivified_setting ( argv[optind],
67                                                  &setting ) ) != 0 )
68                 goto err_parse_setting;
69
70         /* Determine starting bus:dev.fn address */
71         if ( ( len = fetchn_setting ( setting.settings, &setting.setting,
72                                       NULL, &setting.setting, &prev ) ) < 0 ) {
73                 /* Setting not yet defined: start searching from 00:00.0 */
74                 prev = 0;
75         } else {
76                 /* Setting is defined: start searching from next location */
77                 prev++;
78         }
79
80         /* Find next existent PCI device */
81         if ( ( next = pci_find_next ( &pci, prev ) ) < 0 ) {
82                 rc = next;
83                 goto err_find_next;
84         }
85
86         /* Apply default type if necessary.  Use ":uint16" rather than
87          * ":busdevfn" to allow for easy inclusion within a
88          * "${pci/${location}.x.y}" constructed setting.
89          */
90         if ( ! setting.setting.type )
91                 setting.setting.type = &setting_type_uint16;
92
93         /* Store setting */
94         if ( ( rc = storen_setting ( setting.settings, &setting.setting,
95                                      next ) ) != 0 ) {
96                 printf ( "Could not store \"%s\": %s\n",
97                          setting.setting.name, strerror ( rc ) );
98                 goto err_store;
99         }
100
101  err_store:
102  err_find_next:
103  err_parse_setting:
104  err_parse_options:
105         return rc;
106 }
107
108 /** PCI commands */
109 struct command pci_commands[] __command = {
110         {
111                 .name = "pciscan",
112                 .exec = pciscan_exec,
113         },
114 };