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