1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2011 Erwan Velu - All Rights Reserved
4  *
5  *   Permission is hereby granted, free of charge, to any person
6  *   obtaining a copy of this software and associated documentation
7  *   files (the "Software"), to deal in the Software without
8  *   restriction, including without limitation the rights to use,
9  *   copy, modify, merge, publish, distribute, sublicense, and/or
10  *   sell copies of the Software, and to permit persons to whom
11  *   the Software is furnished to do so, subject to the following
12  *   conditions:
13  *
14  *   The above copyright notice and this permission notice shall
15  *   be included in all copies or substantial portions of the Software.
16  *
17  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19  *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21  *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22  *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  *   OTHER DEALINGS IN THE SOFTWARE.
25  *
26  * -----------------------------------------------------------------------
27  */
28 
29 #include "hdt-common.h"
30 #include "hdt-dump.h"
31 
dump_pci(struct s_hardware * hardware,ZZJSON_CONFIG * config,ZZJSON ** item)32 void dump_pci(struct s_hardware *hardware, ZZJSON_CONFIG * config,
33 	      ZZJSON ** item)
34 {
35     int i = 1;
36     struct pci_device *pci_device=NULL;
37     char kernel_modules[LINUX_KERNEL_MODULE_SIZE *
38 			MAX_KERNEL_MODULES_PER_PCI_DEVICE];
39     bool nopciids = false;
40     bool nomodulespcimap = false;
41     bool nomodulesalias = false;
42     bool nomodulesfile = false;
43     int bus = 0, slot = 0, func = 0;
44 
45     if (hardware->pci_ids_return_code == -ENOPCIIDS) {
46 	nopciids = true;
47     }
48     if (hardware->modules_pcimap_return_code == -ENOMODULESPCIMAP) {
49 	nomodulespcimap = true;
50     }
51     if (hardware->modules_pcimap_return_code == -ENOMODULESALIAS) {
52 	nomodulesalias = true;
53     }
54 
55     nomodulesfile = nomodulespcimap && nomodulesalias;
56 
57     CREATE_NEW_OBJECT;
58 
59     add_i("pci_device.count", hardware->nb_pci_devices);
60 
61     FLUSH_OBJECT;
62     /* For every detected pci device, compute its submenu */
63     for_each_pci_func(pci_device, hardware->pci_domain) {
64 	if (pci_device == NULL)
65 	    continue;
66 	char v[10] = { 0 };
67 	char sv[10] = { 0 };
68 	char p[10] = { 0 };
69 	char sp[10] = { 0 };
70 	char c[10] = { 0 };
71 	char r[10] = { 0 };
72 
73 	CREATE_NEW_OBJECT;
74 	bus = __pci_bus;
75 	slot = __pci_slot;
76 	func = __pci_func;
77 
78 	memset(kernel_modules, 0, sizeof kernel_modules);
79 	for (int kmod = 0;
80 	     kmod < pci_device->dev_info->linux_kernel_module_count; kmod++) {
81 	    if (kmod > 0) {
82 		strncat(kernel_modules, " | ", 3);
83 	    }
84 	    strncat(kernel_modules,
85 		    pci_device->dev_info->linux_kernel_module[kmod],
86 		    LINUX_KERNEL_MODULE_SIZE - 1);
87 	}
88 	if (pci_device->dev_info->linux_kernel_module_count == 0)
89 	    strlcpy(kernel_modules, "unknown", 7);
90 
91 	add_i("pci_device.number", i);
92 	if (nopciids == false) {
93 	    add_s("pci_device.vendor_name", pci_device->dev_info->vendor_name);
94 	    add_s("pci_device.product_name",
95 		   pci_device->dev_info->product_name);
96 	}
97 	if (nomodulesfile == false) {
98 	    add_s("pci_device.class_name", pci_device->dev_info->class_name);
99 	    add_s("pci_device.kernel_module", kernel_modules);
100 	}
101 
102 	snprintf(v, sizeof(v), "%04x", pci_device->vendor);
103 	snprintf(p, sizeof(p), "%04x", pci_device->product);
104 	snprintf(sv, sizeof(sv), "%04x", pci_device->sub_vendor);
105 	snprintf(sp, sizeof(sp), "%04x", pci_device->sub_product);
106 	snprintf(c, sizeof(c), "%02x.%02x.%02x",
107 		 pci_device->class[2],
108 		 pci_device->class[1], pci_device->class[0]);
109 	snprintf(r, sizeof(r), "%02x", pci_device->revision);
110 	add_s("pci_device.vendor_id", v);
111 	add_s("pci_device.product_id", p);
112 	add_s("pci_device.sub_vendor_id", sv);
113 	add_s("pci_device.sub_product_id", sp);
114 	add_s("pci_device.class_id", c);
115 	add_s("pci_device.revision", r);
116 	if ((pci_device->dev_info->irq > 0)
117 	    && (pci_device->dev_info->irq < 255))
118 	    add_i("pci_device.irq", pci_device->dev_info->irq);
119 
120 	add_i("pci_device.latency", pci_device->dev_info->latency);
121 	add_i("pci_device.bus", bus);
122 	add_i("pci_device.slot", slot);
123 	add_i("pci_device.func", func);
124 
125 	if (hardware->is_pxe_valid == true) {
126 	    if ((hardware->pxe.pci_device != NULL)
127 		&& (hardware->pxe.pci_device == pci_device)) {
128 		add_hs(pxe.mac_addr);
129 		add_s("pxe", "Current boot device");
130 	    }
131 	}
132 	i++;
133 	FLUSH_OBJECT;
134     }
135     to_cpio("pci");
136 }
137