summaryrefslogtreecommitdiff
path: root/com32/hdt/hdt-cli-pci.c
blob: 75fc0011d78ef0b5169527948239eb64646b7ec0 (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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2009 Erwan Velu - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * -----------------------------------------------------------------------
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "hdt-cli.h"
#include "hdt-common.h"

void main_show_pci(int argc __unused, char **argv __unused,
		   struct s_hardware *hardware)
{
    reset_more_printf();
    more_printf("PCI\n");
    more_printf(" NB Devices   : %d\n", hardware->nb_pci_devices);
}

static void show_pci_device(int argc, char **argv, struct s_hardware *hardware)
{
    int i = 0;
    struct pci_device *pci_device = NULL, *temp_pci_device;
    int pcidev = -1;
    bool nopciids = false;
    bool nomodulespcimap = false;
    bool nomodulesalias = false;
    bool nomodulesfiles = false;
    char kernel_modules[LINUX_KERNEL_MODULE_SIZE *
			MAX_KERNEL_MODULES_PER_PCI_DEVICE];
    int bus = 0, slot = 0, func = 0;

    reset_more_printf();
    /* Sanitize arguments */
    if (argc <= 0) {
	more_printf("show device <number>\n");
	return;
    } else
	pcidev = strtol(argv[0], (char **)NULL, 10);

    if (errno == ERANGE) {
	more_printf("This PCI device number is incorrect\n");
	return;
    }
    if ((pcidev > hardware->nb_pci_devices) || (pcidev <= 0)) {
	more_printf("PCI device %d doesn't exist\n", pcidev);
	return;
    }
    if (hardware->pci_ids_return_code == -ENOPCIIDS) {
	nopciids = true;
    }
    if (hardware->modules_pcimap_return_code == -ENOMODULESPCIMAP) {
	nomodulespcimap = true;
    }
    if (hardware->modules_alias_return_code == -ENOMODULESALIAS) {
	nomodulesalias = true;
    }
    nomodulesfiles = nomodulespcimap && nomodulesalias;
    for_each_pci_func(temp_pci_device, hardware->pci_domain) {
	i++;
	if (i == pcidev) {
	    bus = __pci_bus;
	    slot = __pci_slot;
	    func = __pci_func;
	    pci_device = temp_pci_device;
	}
    }

    if (pci_device == NULL) {
	more_printf("We were enabled to find PCI device %d\n", pcidev);
	return;
    }

    memset(kernel_modules, 0, sizeof kernel_modules);
    for (int kmod = 0;
	 kmod < pci_device->dev_info->linux_kernel_module_count; kmod++) {
	if (kmod > 0) {
	    strncat(kernel_modules, " | ", 3);
	}
	strncat(kernel_modules,
		pci_device->dev_info->linux_kernel_module[kmod],
		LINUX_KERNEL_MODULE_SIZE - 1);
    }
    if (pci_device->dev_info->linux_kernel_module_count == 0)
	strlcpy(kernel_modules, "unknown", 7);

    more_printf("PCI Device %d\n", pcidev);

    if (nopciids == false) {
	more_printf("Vendor Name   : %s\n", pci_device->dev_info->vendor_name);
	more_printf("Product Name  : %s\n", pci_device->dev_info->product_name);
	more_printf("Class Name    : %s\n", pci_device->dev_info->class_name);
    }

    if (nomodulesfiles == false) {
	more_printf("Kernel module : %s\n", kernel_modules);
    }

    more_printf("Vendor ID     : %04x\n", pci_device->vendor);
    more_printf("Product ID    : %04x\n", pci_device->product);
    more_printf("SubVendor ID  : %04x\n", pci_device->sub_vendor);
    more_printf("SubProduct ID : %04x\n", pci_device->sub_product);
    more_printf("Class ID      : %02x.%02x.%02x\n", pci_device->class[2],
		pci_device->class[1], pci_device->class[0]);
    more_printf("Revision      : %02x\n", pci_device->revision);
    if ((pci_device->dev_info->irq > 0)
	&& (pci_device->dev_info->irq < 255))
	more_printf("IRQ           : %0d\n", pci_device->dev_info->irq);
    more_printf("Latency       : %0d\n", pci_device->dev_info->latency);
    more_printf("PCI Bus       : %02d\n", bus);
    more_printf("PCI Slot      : %02d\n", slot);
    more_printf("PCI Func      : %02d\n", func);

    if (hardware->is_pxe_valid == true) {
	if ((hardware->pxe.pci_device != NULL)
	    && (hardware->pxe.pci_device == pci_device)) {
	    more_printf("Mac Address   : %s\n", hardware->pxe.mac_addr);
	    more_printf("PXE           : Current boot device\n");
	}
    }
}

static void show_pci_devices(int argc __unused, char **argv __unused,
			     struct s_hardware *hardware)
{
    int i = 1;
    struct pci_device *pci_device;
    char kernel_modules[LINUX_KERNEL_MODULE_SIZE *
			MAX_KERNEL_MODULES_PER_PCI_DEVICE];
    bool nopciids = false;
    bool nomodulespcimap = false;
    bool nomodulesalias = false;
    bool nomodulesfile = false;
    char first_line[81];
    char second_line[81];

    reset_more_printf();
    more_printf("%d PCI devices detected\n", hardware->nb_pci_devices);

    if (hardware->pci_ids_return_code == -ENOPCIIDS) {
	nopciids = true;
    }
    if (hardware->modules_pcimap_return_code == -ENOMODULESPCIMAP) {
	nomodulespcimap = true;
    }
    if (hardware->modules_pcimap_return_code == -ENOMODULESALIAS) {
	nomodulesalias = true;
    }

    nomodulesfile = nomodulespcimap && nomodulesalias;

    /* For every detected pci device, compute its submenu */
    for_each_pci_func(pci_device, hardware->pci_domain) {
	memset(kernel_modules, 0, sizeof kernel_modules);
	for (int kmod = 0;
	     kmod < pci_device->dev_info->linux_kernel_module_count; kmod++) {
	    if (kmod > 0) {
		strncat(kernel_modules, " | ", 3);
	    }
	    strncat(kernel_modules,
		    pci_device->dev_info->linux_kernel_module[kmod],
		    LINUX_KERNEL_MODULE_SIZE - 1);
	}
	if (pci_device->dev_info->linux_kernel_module_count == 0)
	    strlcpy(kernel_modules, "unknown", 7);

	if (nopciids == false) {
	    snprintf(first_line, sizeof(first_line),
		     "%02d: %s %s \n", i,
		     pci_device->dev_info->vendor_name,
		     pci_device->dev_info->product_name);
	    if (nomodulesfile == false)
		snprintf(second_line, sizeof(second_line),
			 "    # %-25s # Kmod: %s\n",
			 pci_device->dev_info->class_name, kernel_modules);
	    else
		snprintf(second_line, sizeof(second_line),
			 "    # %-25s # ID:%04x:%04x[%04x:%04x]\n",
			 pci_device->dev_info->class_name,
			 pci_device->vendor,
			 pci_device->product,
			 pci_device->sub_vendor, pci_device->sub_product);

	    more_printf("%s", first_line);
	    more_printf("%s", second_line);
	    more_printf("\n");
	} else if (nopciids == true) {
	    if (nomodulesfile == true) {
		more_printf("%02d: %04x:%04x [%04x:%04x] \n",
			    i, pci_device->vendor,
			    pci_device->product,
			    pci_device->sub_vendor, pci_device->sub_product);
	    } else {
		more_printf
		    ("%02d: %04x:%04x [%04x:%04x] Kmod:%s\n", i,
		     pci_device->vendor, pci_device->product,
		     pci_device->sub_vendor,
		     pci_device->sub_product, kernel_modules);
	    }
	}
	i++;
    }
}

static void show_pci_irq(int argc __unused, char **argv __unused,
			 struct s_hardware *hardware)
{
    struct pci_device *pci_device;
    bool nopciids = false;

    reset_more_printf();
    more_printf("%d PCI devices detected\n", hardware->nb_pci_devices);
    more_printf("IRQ : product\n");
    more_printf("-------------\n");

    if (hardware->pci_ids_return_code == -ENOPCIIDS) {
	nopciids = true;
    }

    /* For every detected pci device, compute its submenu */
    for_each_pci_func(pci_device, hardware->pci_domain) {
	/* Only display valid IRQs */
	if ((pci_device->dev_info->irq > 0)
	    && (pci_device->dev_info->irq < 255)) {
	    if (nopciids == false) {
		more_printf("%02d  : %s %s \n",
			    pci_device->dev_info->irq,
			    pci_device->dev_info->vendor_name,
			    pci_device->dev_info->product_name);
	    } else {
		more_printf("%02d  : %04x:%04x [%04x:%04x] \n",
			    pci_device->dev_info->irq,
			    pci_device->vendor,
			    pci_device->product,
			    pci_device->sub_vendor, pci_device->sub_product);
	    }
	}
    }
}

struct cli_callback_descr list_pci_show_modules[] = {
    {
     .name = CLI_IRQ,
     .exec = show_pci_irq,
     .nomodule=false,
     },
    {
     .name = CLI_PCI_DEVICE,
     .exec = show_pci_device,
     .nomodule=false,
     },
    {
     .name = NULL,
     .exec = NULL,
     .nomodule=false,
     },
};

struct cli_module_descr pci_show_modules = {
    .modules = list_pci_show_modules,
    .default_callback = show_pci_devices,
};

struct cli_mode_descr pci_mode = {
    .mode = PCI_MODE,
    .name = CLI_PCI,
    .default_modules = NULL,
    .show_modules = &pci_show_modules,
    .set_modules = NULL,
};