xref: /freebsd/sys/compat/linsysfs/linsysfs.c (revision 61b95bcb42993b24633b280791438266d78f2747)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 IronPort Systems
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/ctype.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/mount.h>
38 #include <sys/sbuf.h>
39 #include <sys/smp.h>
40 #include <sys/bus.h>
41 #include <sys/pciio.h>
42 
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcireg.h>
45 
46 #include <compat/linux/linux_util.h>
47 #include <fs/pseudofs/pseudofs.h>
48 
49 #include <compat/linsysfs/linsysfs.h>
50 
51 MALLOC_DEFINE(M_LINSYSFS, "linsysfs", "Linsysfs structures");
52 
53 struct scsi_host_queue {
54 	TAILQ_ENTRY(scsi_host_queue) scsi_host_next;
55 	char *path;
56 	char *name;
57 };
58 
59 TAILQ_HEAD(,scsi_host_queue) scsi_host_q;
60 
61 static int host_number = 0;
62 
63 static int
64 atoi(const char *str)
65 {
66 	return (int)strtol(str, (char **)NULL, 10);
67 }
68 
69 /*
70  * Filler function for proc_name
71  */
72 static int
73 linsysfs_scsiname(PFS_FILL_ARGS)
74 {
75 	struct scsi_host_queue *scsi_host;
76 	int index;
77 
78 	if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) {
79 		index = atoi(&pn->pn_parent->pn_name[4]);
80 	} else {
81 		sbuf_printf(sb, "unknown\n");
82 		return (0);
83 	}
84 	TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) {
85 		if (index-- == 0) {
86 			sbuf_printf(sb, "%s\n", scsi_host->name);
87 			return (0);
88 		}
89 	}
90 	sbuf_printf(sb, "unknown\n");
91 	return (0);
92 }
93 
94 /*
95  * Filler function for device sym-link
96  */
97 static int
98 linsysfs_link_scsi_host(PFS_FILL_ARGS)
99 {
100 	struct scsi_host_queue *scsi_host;
101 	int index;
102 
103 	if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) {
104 		index = atoi(&pn->pn_parent->pn_name[4]);
105 	} else {
106 		sbuf_printf(sb, "unknown\n");
107 		return (0);
108 	}
109 	TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) {
110 		if (index-- == 0) {
111 			sbuf_printf(sb, "../../../devices%s", scsi_host->path);
112 			return(0);
113 		}
114 	}
115 	sbuf_printf(sb, "unknown\n");
116 	return (0);
117 }
118 
119 static int
120 linsysfs_fill_data(PFS_FILL_ARGS)
121 {
122 	sbuf_printf(sb, "%s", (char *)pn->pn_data);
123 	return (0);
124 }
125 
126 static int
127 linsysfs_fill_vendor(PFS_FILL_ARGS)
128 {
129 	sbuf_printf(sb, "0x%04x\n", pci_get_vendor((device_t)pn->pn_data));
130 	return (0);
131 }
132 
133 static int
134 linsysfs_fill_device(PFS_FILL_ARGS)
135 {
136 	sbuf_printf(sb, "0x%04x\n", pci_get_device((device_t)pn->pn_data));
137 	return (0);
138 }
139 
140 static int
141 linsysfs_fill_subvendor(PFS_FILL_ARGS)
142 {
143 	sbuf_printf(sb, "0x%04x\n", pci_get_subvendor((device_t)pn->pn_data));
144 	return (0);
145 }
146 
147 static int
148 linsysfs_fill_subdevice(PFS_FILL_ARGS)
149 {
150 	sbuf_printf(sb, "0x%04x\n", pci_get_subdevice((device_t)pn->pn_data));
151 	return (0);
152 }
153 
154 static int
155 linsysfs_fill_revid(PFS_FILL_ARGS)
156 {
157 	sbuf_printf(sb, "0x%x\n", pci_get_revid((device_t)pn->pn_data));
158 	return (0);
159 }
160 
161 static int
162 linsysfs_fill_config(PFS_FILL_ARGS)
163 {
164 	uint8_t config[48];
165 	device_t dev;
166 	uint32_t reg;
167 
168 	dev = (device_t)pn->pn_data;
169 	bzero(config, sizeof(config));
170 	reg = pci_get_vendor(dev);
171 	config[0] = reg;
172 	config[1] = reg >> 8;
173 	reg = pci_get_device(dev);
174 	config[2] = reg;
175 	config[3] = reg >> 8;
176 	reg = pci_get_revid(dev);
177 	config[8] = reg;
178 	reg = pci_get_subvendor(dev);
179 	config[44] = reg;
180 	config[45] = reg >> 8;
181 	reg = pci_get_subdevice(dev);
182 	config[46] = reg;
183 	config[47] = reg >> 8;
184 	sbuf_bcat(sb, config, sizeof(config));
185 	return (0);
186 }
187 
188 /*
189  * Filler function for PCI uevent file
190  */
191 static int
192 linsysfs_fill_uevent_pci(PFS_FILL_ARGS)
193 {
194 	device_t dev;
195 
196 	dev = (device_t)pn->pn_data;
197 	sbuf_printf(sb, "DRIVER=%s\nPCI_CLASS=%X\nPCI_ID=%04X:%04X\n"
198 	    "PCI_SUBSYS_ID=%04X:%04X\nPCI_SLOT_NAME=%04d:%02x:%02x.%x\n",
199 	    linux_driver_get_name_dev(dev), pci_get_class(dev),
200 	    pci_get_vendor(dev), pci_get_device(dev), pci_get_subvendor(dev),
201 	    pci_get_subdevice(dev), pci_get_domain(dev), pci_get_bus(dev),
202 	    pci_get_slot(dev), pci_get_function(dev));
203 	return (0);
204 }
205 
206 /*
207  * Filler function for drm uevent file
208  */
209 static int
210 linsysfs_fill_uevent_drm(PFS_FILL_ARGS)
211 {
212 	device_t dev;
213 	int unit;
214 
215 	dev = (device_t)pn->pn_data;
216 	unit = device_get_unit(dev);
217 	sbuf_printf(sb,
218 	    "MAJOR=226\nMINOR=%d\nDEVNAME=dri/card%d\nDEVTYPE=dri_minor\n",
219 	    unit, unit);
220 	return (0);
221 }
222 
223 static char *
224 get_full_pfs_path(struct pfs_node *cur)
225 {
226 	char *temp, *path;
227 
228 	temp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
229 	path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
230 	path[0] = '\0';
231 
232 	do {
233 		snprintf(temp, MAXPATHLEN, "%s/%s", cur->pn_name, path);
234 		strlcpy(path, temp, MAXPATHLEN);
235 		cur = cur->pn_parent;
236 	} while (cur->pn_parent != NULL);
237 
238 	path[strlen(path) - 1] = '\0'; /* remove extra slash */
239 	free(temp, M_TEMP);
240 	return (path);
241 }
242 
243 /*
244  * Filler function for symlink from drm char device to PCI device
245  */
246 static int
247 linsysfs_fill_vgapci(PFS_FILL_ARGS)
248 {
249 	char *path;
250 
251 	path = get_full_pfs_path((struct pfs_node*)pn->pn_data);
252 	sbuf_printf(sb, "../../../%s", path);
253 	free(path, M_TEMP);
254 	return (0);
255 }
256 
257 #undef PCI_DEV
258 #define PCI_DEV "pci"
259 #define DRMN_DEV "drmn"
260 static int
261 linsysfs_run_bus(device_t dev, struct pfs_node *dir, struct pfs_node *scsi,
262     struct pfs_node *chardev, struct pfs_node *drm, char *path, char *prefix)
263 {
264 	struct scsi_host_queue *scsi_host;
265 	struct pfs_node *sub_dir, *cur_file;
266 	int i, nchildren, error;
267 	device_t *children, parent;
268 	devclass_t devclass;
269 	const char *name = NULL;
270 	struct pci_devinfo *dinfo;
271 	char *device, *host, *new_path, *devname;
272 
273 	new_path = path;
274 	devname = malloc(16, M_TEMP, M_WAITOK);
275 
276 	parent = device_get_parent(dev);
277 	if (parent) {
278 		devclass = device_get_devclass(parent);
279 		if (devclass != NULL)
280 			name = devclass_get_name(devclass);
281 		if (name && strcmp(name, PCI_DEV) == 0) {
282 			dinfo = device_get_ivars(dev);
283 			if (dinfo) {
284 				device = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
285 				new_path = malloc(MAXPATHLEN, M_TEMP,
286 				    M_WAITOK);
287 				new_path[0] = '\000';
288 				strcpy(new_path, path);
289 				host = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
290 				device[0] = '\000';
291 				sprintf(device, "%s:%02x:%02x.%x",
292 				    prefix,
293 				    dinfo->cfg.bus,
294 				    dinfo->cfg.slot,
295 				    dinfo->cfg.func);
296 				strcat(new_path, "/");
297 				strcat(new_path, device);
298 				dir = pfs_create_dir(dir, device,
299 				    NULL, NULL, NULL, 0);
300 				cur_file = pfs_create_file(dir, "vendor",
301 				    &linsysfs_fill_vendor, NULL, NULL, NULL,
302 				    PFS_RD);
303 				cur_file->pn_data = (void*)dev;
304 				cur_file = pfs_create_file(dir, "device",
305 				    &linsysfs_fill_device, NULL, NULL, NULL,
306 				    PFS_RD);
307 				cur_file->pn_data = (void*)dev;
308 				cur_file = pfs_create_file(dir,
309 				    "subsystem_vendor",
310 				    &linsysfs_fill_subvendor, NULL, NULL, NULL,
311 				    PFS_RD);
312 				cur_file->pn_data = (void*)dev;
313 				cur_file = pfs_create_file(dir,
314 				    "subsystem_device",
315 				    &linsysfs_fill_subdevice, NULL, NULL, NULL,
316 				    PFS_RD);
317 				cur_file->pn_data = (void*)dev;
318 				cur_file = pfs_create_file(dir, "revision",
319 				    &linsysfs_fill_revid, NULL, NULL, NULL,
320 				    PFS_RD);
321 				cur_file->pn_data = (void*)dev;
322 				cur_file = pfs_create_file(dir, "config",
323 				    &linsysfs_fill_config, NULL, NULL, NULL,
324 				    PFS_RD);
325 				cur_file->pn_data = (void*)dev;
326 				cur_file = pfs_create_file(dir, "uevent",
327 				    &linsysfs_fill_uevent_pci, NULL, NULL,
328 				    NULL, PFS_RD);
329 				cur_file->pn_data = (void*)dev;
330 				cur_file = pfs_create_link(dir, "subsystem",
331 				    &linsysfs_fill_data, NULL, NULL, NULL, 0);
332 				/* libdrm just checks that the link ends in "/pci" */
333 				cur_file->pn_data = "/sys/bus/pci";
334 
335 				if (dinfo->cfg.baseclass == PCIC_STORAGE) {
336 					/* DJA only make this if needed */
337 					sprintf(host, "host%d", host_number++);
338 					strcat(new_path, "/");
339 					strcat(new_path, host);
340 					pfs_create_dir(dir, host,
341 					    NULL, NULL, NULL, 0);
342 					scsi_host = malloc(sizeof(
343 					    struct scsi_host_queue),
344 					    M_DEVBUF, M_NOWAIT);
345 					scsi_host->path = malloc(
346 					    strlen(new_path) + 1,
347 					    M_DEVBUF, M_NOWAIT);
348 					scsi_host->path[0] = '\000';
349 					bcopy(new_path, scsi_host->path,
350 					    strlen(new_path) + 1);
351 					scsi_host->name = "unknown";
352 
353 					sub_dir = pfs_create_dir(scsi, host,
354 					    NULL, NULL, NULL, 0);
355 					pfs_create_link(sub_dir, "device",
356 					    &linsysfs_link_scsi_host,
357 					    NULL, NULL, NULL, 0);
358 					pfs_create_file(sub_dir, "proc_name",
359 					    &linsysfs_scsiname,
360 					    NULL, NULL, NULL, PFS_RD);
361 					scsi_host->name
362 					    = linux_driver_get_name_dev(dev);
363 					TAILQ_INSERT_TAIL(&scsi_host_q,
364 					    scsi_host, scsi_host_next);
365 				}
366 				free(device, M_TEMP);
367 				free(host, M_TEMP);
368 			}
369 		}
370 
371 		devclass = device_get_devclass(dev);
372 		if (devclass != NULL)
373 			name = devclass_get_name(devclass);
374 		else
375 			name = NULL;
376 		if (name != NULL && strcmp(name, DRMN_DEV) == 0 &&
377 		    device_get_unit(dev) >= 0) {
378 			dinfo = device_get_ivars(parent);
379 			if (dinfo != NULL && dinfo->cfg.baseclass == PCIC_DISPLAY) {
380 				pfs_create_dir(dir, "drm", NULL, NULL, NULL, 0);
381 				sprintf(devname, "226:%d",
382 				    device_get_unit(dev));
383 				sub_dir = pfs_create_dir(chardev,
384 				    devname, NULL, NULL, NULL, 0);
385 				cur_file = pfs_create_link(sub_dir,
386 				    "device", &linsysfs_fill_vgapci, NULL,
387 				    NULL, NULL, PFS_RD);
388 				cur_file->pn_data = (void*)dir;
389 				cur_file = pfs_create_file(sub_dir,
390 				    "uevent", &linsysfs_fill_uevent_drm, NULL,
391 				    NULL, NULL, PFS_RD);
392 				cur_file->pn_data = (void*)dev;
393 				sprintf(devname, "card%d",
394 				    device_get_unit(dev));
395 				sub_dir = pfs_create_dir(drm,
396 				    devname, NULL, NULL, NULL, 0);
397 				cur_file = pfs_create_link(sub_dir,
398 				    "device", &linsysfs_fill_vgapci, NULL,
399 				    NULL, NULL, PFS_RD);
400 				cur_file->pn_data = (void*)dir;
401 			}
402 		}
403 	}
404 
405 	error = device_get_children(dev, &children, &nchildren);
406 	if (error == 0) {
407 		for (i = 0; i < nchildren; i++)
408 			if (children[i])
409 				linsysfs_run_bus(children[i], dir, scsi,
410 				    chardev, drm, new_path, prefix);
411 		free(children, M_TEMP);
412 	}
413 	if (new_path != path)
414 		free(new_path, M_TEMP);
415 	free(devname, M_TEMP);
416 
417 	return (1);
418 }
419 
420 /*
421  * Filler function for sys/devices/system/cpu/{online,possible,present}
422  */
423 static int
424 linsysfs_cpuonline(PFS_FILL_ARGS)
425 {
426 
427 	sbuf_printf(sb, "%d-%d\n", CPU_FIRST(), mp_maxid);
428 	return (0);
429 }
430 
431 /*
432  * Filler function for sys/devices/system/cpu/cpuX/online
433  */
434 static int
435 linsysfs_cpuxonline(PFS_FILL_ARGS)
436 {
437 
438 	sbuf_printf(sb, "1\n");
439 	return (0);
440 }
441 
442 static void
443 linsysfs_listcpus(struct pfs_node *dir)
444 {
445 	struct pfs_node *cpu;
446 	char *name;
447 	int i, count, len;
448 
449 	len = 1;
450 	count = mp_maxcpus;
451 	while (count > 10) {
452 		count /= 10;
453 		len++;
454 	}
455 	len += sizeof("cpu");
456 	name = malloc(len, M_TEMP, M_WAITOK);
457 
458 	for (i = 0; i < mp_ncpus; ++i) {
459 		/* /sys/devices/system/cpu/cpuX */
460 		sprintf(name, "cpu%d", i);
461 		cpu = pfs_create_dir(dir, name, NULL, NULL, NULL, 0);
462 
463 		pfs_create_file(cpu, "online", &linsysfs_cpuxonline,
464 		    NULL, NULL, NULL, PFS_RD);
465 	}
466 	free(name, M_TEMP);
467 }
468 
469 /*
470  * Constructor
471  */
472 static int
473 linsysfs_init(PFS_INIT_ARGS)
474 {
475 	struct pfs_node *root;
476 	struct pfs_node *class;
477 	struct pfs_node *dir, *sys, *cpu;
478 	struct pfs_node *drm;
479 	struct pfs_node *pci;
480 	struct pfs_node *scsi;
481 	struct pfs_node *devdir, *chardev;
482 	struct pfs_node *kernel;
483 	devclass_t devclass;
484 	device_t dev;
485 
486 	TAILQ_INIT(&scsi_host_q);
487 
488 	root = pi->pi_root;
489 
490 	/* /sys/bus/... */
491 	dir = pfs_create_dir(root, "bus", NULL, NULL, NULL, 0);
492 
493 	/* /sys/class/... */
494 	class = pfs_create_dir(root, "class", NULL, NULL, NULL, 0);
495 	scsi = pfs_create_dir(class, "scsi_host", NULL, NULL, NULL, 0);
496 	drm = pfs_create_dir(class, "drm", NULL, NULL, NULL, 0);
497 	pfs_create_dir(class, "power_supply", NULL, NULL, NULL, 0);
498 
499 	/* /sys/class/net/.. */
500 	net = pfs_create_dir(class, "net", NULL, NULL, NULL, 0);
501 
502 	/* /sys/dev/... */
503 	devdir = pfs_create_dir(root, "dev", NULL, NULL, NULL, 0);
504 	chardev = pfs_create_dir(devdir, "char", NULL, NULL, NULL, 0);
505 
506 	/* /sys/devices/... */
507 	dir = pfs_create_dir(root, "devices", NULL, NULL, NULL, 0);
508 	pci = pfs_create_dir(dir, "pci0000:00", NULL, NULL, NULL, 0);
509 
510 	devclass = devclass_find("root");
511 	if (devclass == NULL) {
512 		return (0);
513 	}
514 
515 	dev = devclass_get_device(devclass, 0);
516 	linsysfs_run_bus(dev, pci, scsi, chardev, drm, "/pci0000:00", "0000");
517 
518 	/* /sys/devices/system */
519 	sys = pfs_create_dir(dir, "system", NULL, NULL, NULL, 0);
520 
521 	/* /sys/devices/system/cpu */
522 	cpu = pfs_create_dir(sys, "cpu", NULL, NULL, NULL, 0);
523 
524 	pfs_create_file(cpu, "online", &linsysfs_cpuonline,
525 	    NULL, NULL, NULL, PFS_RD);
526 	pfs_create_file(cpu, "possible", &linsysfs_cpuonline,
527 	    NULL, NULL, NULL, PFS_RD);
528 	pfs_create_file(cpu, "present", &linsysfs_cpuonline,
529 	    NULL, NULL, NULL, PFS_RD);
530 
531 	linsysfs_listcpus(cpu);
532 
533 	/* /sys/kernel */
534 	kernel = pfs_create_dir(root, "kernel", NULL, NULL, NULL, 0);
535 	/* /sys/kernel/debug, mountpoint for lindebugfs. */
536 	pfs_create_dir(kernel, "debug", NULL, NULL, NULL, 0);
537 
538 	linsysfs_net_init();
539 
540 	return (0);
541 }
542 
543 /*
544  * Destructor
545  */
546 static int
547 linsysfs_uninit(PFS_INIT_ARGS)
548 {
549 	struct scsi_host_queue *scsi_host, *scsi_host_tmp;
550 
551 	TAILQ_FOREACH_SAFE(scsi_host, &scsi_host_q, scsi_host_next,
552 	    scsi_host_tmp) {
553 		TAILQ_REMOVE(&scsi_host_q, scsi_host, scsi_host_next);
554 		free(scsi_host->path, M_TEMP);
555 		free(scsi_host, M_TEMP);
556 	}
557 
558 	linsysfs_net_uninit();
559 
560 	return (0);
561 }
562 
563 PSEUDOFS(linsysfs, 1, VFCF_JAIL);
564 #if defined(__aarch64__) || defined(__amd64__)
565 MODULE_DEPEND(linsysfs, linux_common, 1, 1, 1);
566 #else
567 MODULE_DEPEND(linsysfs, linux, 1, 1, 1);
568 #endif
569