summaryrefslogtreecommitdiff
path: root/devel/libdevq/files/patch-src_device.c
blob: d7609c1eefc0d90e95e154b1d53aca159c297ebc (plain) (blame)
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
From 7b48b8726b85eac66c1b164fab87d154be5aa068 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-S=C3=A9bastien=20P=C3=A9dron?= <dumbbell@FreeBSD.org>
Date: Fri, 13 Feb 2015 19:46:37 +0100
Subject: [PATCH] Support dev.$driver.$n.%location from FreeBSD 11.x

The format changed from:
    slot=1 function=0
to:
    pci0:2:0:0 handle=\_SB_.PCI0.PEG3.MXM3

Now devq_device_get_pciid_from_fd() supports both formats.
---
 src/device.c | 89 ++++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 29 deletions(-)

diff --git a/src/device.c b/src/device.c
index 9aadc2b..77df580 100644
--- src/device.c
+++ src/device.c
@@ -188,6 +188,58 @@ devq_device_get_devpath_from_fd(int fd,
 #endif /* defined(HAVE_LIBPROCSTAT_H) */
 }
 
+static int
+devq_compare_vgapci_busaddr(int i, int *domain, int *bus, int *slot,
+    int *function)
+{
+	int ret;
+	char sysctl_name[32], sysctl_value[128];
+	size_t sysctl_value_len;
+
+	sprintf(sysctl_name, "dev.vgapci.%d.%%location", i);
+
+	sysctl_value_len = sizeof(sysctl_value);
+	memset(sysctl_value, 0, sysctl_value_len);
+	ret = sysctlbyname(sysctl_name, sysctl_value,
+	    &sysctl_value_len, NULL, 0);
+	if (ret != 0)
+		return (-1);
+
+	/*
+	 * dev.vgapci.$m.%location can have two formats:
+	 *     o  "pci0:2:0:0 handle=\_SB_.PCI0.PEG3.MXM3" (FreeBSD 11+)
+	 *     o  "slot=1 function=0" (up-to FreeBSD 10)
+	 */
+
+	ret = sscanf(sysctl_value, "pci%d:%d:%d:%d %*s",
+	    domain, bus, slot, function);
+	if (ret == 4)
+		return (0);
+
+	ret = sscanf(sysctl_value, "slot=%d function=%d %*s",
+	    slot, function);
+	if (ret != 2)
+		return (-1);
+
+	sprintf(sysctl_name, "dev.vgapci.%d.%%parent", i);
+
+	sysctl_value_len = sizeof(sysctl_value);
+	memset(sysctl_value, 0, sysctl_value_len);
+	ret = sysctlbyname(sysctl_name, sysctl_value,
+	    &sysctl_value_len, NULL, 0);
+	if (ret != 0)
+		return (-1);
+
+	ret = sscanf(sysctl_value, "pci%d", bus);
+	if (ret != 1)
+		return (-1);
+
+	/* FIXME: What domain to assume? */
+	*domain = 0;
+
+	return (0);
+}
+
 int
 devq_device_get_pciid_from_fd(int fd,
     int *vendor_id, int *device_id)
@@ -253,38 +305,17 @@ devq_device_get_pciid_from_fd(int fd,
 	 *     o  dev.vgapci.$m.%parent
 	 */
 	for (i = 0; i < DEVQ_MAX_DEVS; ++i) {
-		sprintf(sysctl_name, "dev.vgapci.%d.%%location", i);
-
-		sysctl_value_len = sizeof(sysctl_value);
-		memset(sysctl_value, 0, sysctl_value_len);
-		ret = sysctlbyname(sysctl_name, sysctl_value,
-		    &sysctl_value_len, NULL, 0);
-		if (ret != 0)
-			continue;
+		int tmp_domain, tmp_bus, tmp_slot, tmp_function;
 
-		int tmp_slot, tmp_function;
-		ret = sscanf(sysctl_value, "slot=%d function=%d %*s",
+		ret = devq_compare_vgapci_busaddr(i, &tmp_domain, &tmp_bus,
 		    &tmp_slot, &tmp_function);
-		if (ret != 2 ||
-		    tmp_slot != slot || tmp_function != function)
-			continue;
-
-		sprintf(sysctl_name, "dev.vgapci.%d.%%parent", i);
-
-		sysctl_value_len = sizeof(sysctl_value);
-		memset(sysctl_value, 0, sysctl_value_len);
-		ret = sysctlbyname(sysctl_name, sysctl_value,
-		    &sysctl_value_len, NULL, 0);
-		if (ret != 0)
-			continue;
-
-		int tmp_bus;
-		ret = sscanf(sysctl_value, "pci%d",
-		    &tmp_bus);
-		if (ret != 1 || tmp_bus != bus)
-			continue;
 
-		break;
+		if (ret == 0 &&
+		    tmp_domain == domain &&
+		    tmp_bus == bus &&
+		    tmp_slot == slot &&
+		    tmp_function == function)
+			break;
 	}
 
 	if (i == DEVQ_MAX_DEVS) {