xref: /freebsd-13-stable/sys/compat/linsysfs/linsysfs.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
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/socket.h>
39 #include <sys/bus.h>
40 #include <sys/pciio.h>
41 
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcireg.h>
44 
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_dl.h>
48 
49 #include <compat/linux/linux.h>
50 #include <compat/linux/linux_common.h>
51 #include <compat/linux/linux_util.h>
52 #include <fs/pseudofs/pseudofs.h>
53 
54 struct scsi_host_queue {
55 	TAILQ_ENTRY(scsi_host_queue) scsi_host_next;
56 	char *path;
57 	char *name;
58 };
59 
60 TAILQ_HEAD(,scsi_host_queue) scsi_host_q;
61 
62 static int host_number = 0;
63 
64 static int
atoi(const char * str)65 atoi(const char *str)
66 {
67 	return (int)strtol(str, (char **)NULL, 10);
68 }
69 
70 static int
linsysfs_ifnet_addr(PFS_FILL_ARGS)71 linsysfs_ifnet_addr(PFS_FILL_ARGS)
72 {
73 	struct l_sockaddr lsa;
74 	struct ifnet *ifp;
75 
76 	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
77 	if (ifp == NULL)
78 		return (ENOENT);
79 	if (linux_ifhwaddr(ifp, &lsa) != 0)
80 		return (ENOENT);
81 	sbuf_printf(sb, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
82 	    lsa.sa_data[0], lsa.sa_data[1], lsa.sa_data[2],
83 	    lsa.sa_data[3], lsa.sa_data[4], lsa.sa_data[5]);
84 	return (0);
85 }
86 
87 static int
linsysfs_ifnet_addrlen(PFS_FILL_ARGS)88 linsysfs_ifnet_addrlen(PFS_FILL_ARGS)
89 {
90 
91 	sbuf_printf(sb, "%d\n", LINUX_IFHWADDRLEN);
92 	return (0);
93 }
94 
95 static int
linsysfs_ifnet_flags(PFS_FILL_ARGS)96 linsysfs_ifnet_flags(PFS_FILL_ARGS)
97 {
98 	struct ifnet *ifp;
99 	unsigned short flags;
100 
101 	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
102 	if (ifp == NULL)
103 		return (ENOENT);
104 	linux_ifflags(ifp, &flags);
105 	sbuf_printf(sb, "0x%x\n", flags);
106 	return (0);
107 }
108 
109 static int
linsysfs_ifnet_ifindex(PFS_FILL_ARGS)110 linsysfs_ifnet_ifindex(PFS_FILL_ARGS)
111 {
112 	struct ifnet *ifp;
113 
114 	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
115 	if (ifp == NULL)
116 		return (ENOENT);
117 	sbuf_printf(sb, "%u\n", ifp->if_index);
118 	return (0);
119 }
120 
121 static int
linsysfs_ifnet_mtu(PFS_FILL_ARGS)122 linsysfs_ifnet_mtu(PFS_FILL_ARGS)
123 {
124 	struct ifnet *ifp;
125 
126 	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
127 	if (ifp == NULL)
128 		return (ENOENT);
129 	sbuf_printf(sb, "%u\n", ifp->if_mtu);
130 	return (0);
131 }
132 
133 static int
linsysfs_ifnet_tx_queue_len(PFS_FILL_ARGS)134 linsysfs_ifnet_tx_queue_len(PFS_FILL_ARGS)
135 {
136 
137 	/* XXX */
138 	sbuf_printf(sb, "1000\n");
139 	return (0);
140 }
141 
142 static int
linsysfs_ifnet_type(PFS_FILL_ARGS)143 linsysfs_ifnet_type(PFS_FILL_ARGS)
144 {
145 	struct l_sockaddr lsa;
146 	struct ifnet *ifp;
147 
148 	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
149 	if (ifp == NULL)
150 		return (ENOENT);
151 	if (linux_ifhwaddr(ifp, &lsa) != 0)
152 		return (ENOENT);
153 	sbuf_printf(sb, "%d\n", lsa.sa_family);
154 	return (0);
155 }
156 
157 static void
linsysfs_listnics(struct pfs_node * dir)158 linsysfs_listnics(struct pfs_node *dir)
159 {
160 	struct pfs_node *nic;
161 	struct pfs_node *lo;
162 
163 	nic = pfs_create_dir(dir, "eth0", NULL, NULL, NULL, 0);
164 
165 	pfs_create_file(nic, "address", &linsysfs_ifnet_addr,
166 	    NULL, NULL, NULL, PFS_RD);
167 
168 	pfs_create_file(nic, "addr_len", &linsysfs_ifnet_addrlen,
169 	    NULL, NULL, NULL, PFS_RD);
170 
171 	pfs_create_file(nic, "flags", &linsysfs_ifnet_flags,
172 	    NULL, NULL, NULL, PFS_RD);
173 
174 	pfs_create_file(nic, "ifindex", &linsysfs_ifnet_ifindex,
175 	    NULL, NULL, NULL, PFS_RD);
176 
177 	pfs_create_file(nic, "mtu", &linsysfs_ifnet_mtu,
178 	    NULL, NULL, NULL, PFS_RD);
179 
180 	pfs_create_file(nic, "tx_queue_len", &linsysfs_ifnet_tx_queue_len,
181 	    NULL, NULL, NULL, PFS_RD);
182 
183 	pfs_create_file(nic, "type", &linsysfs_ifnet_type,
184 	    NULL, NULL, NULL, PFS_RD);
185 
186 	lo = pfs_create_dir(dir, "lo", NULL, NULL, NULL, 0);
187 
188 	pfs_create_file(lo, "address", &linsysfs_ifnet_addr,
189 	    NULL, NULL, NULL, PFS_RD);
190 
191 	pfs_create_file(lo, "addr_len", &linsysfs_ifnet_addrlen,
192 	    NULL, NULL, NULL, PFS_RD);
193 
194 	pfs_create_file(lo, "flags", &linsysfs_ifnet_flags,
195 	    NULL, NULL, NULL, PFS_RD);
196 
197 	pfs_create_file(lo, "ifindex", &linsysfs_ifnet_ifindex,
198 	    NULL, NULL, NULL, PFS_RD);
199 
200 	pfs_create_file(lo, "mtu", &linsysfs_ifnet_mtu,
201 	    NULL, NULL, NULL, PFS_RD);
202 
203 	pfs_create_file(lo, "tx_queue_len", &linsysfs_ifnet_tx_queue_len,
204 	    NULL, NULL, NULL, PFS_RD);
205 
206 	pfs_create_file(lo, "type", &linsysfs_ifnet_type,
207 	    NULL, NULL, NULL, PFS_RD);
208 }
209 
210 /*
211  * Filler function for proc_name
212  */
213 static int
linsysfs_scsiname(PFS_FILL_ARGS)214 linsysfs_scsiname(PFS_FILL_ARGS)
215 {
216 	struct scsi_host_queue *scsi_host;
217 	int index;
218 
219 	if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) {
220 		index = atoi(&pn->pn_parent->pn_name[4]);
221 	} else {
222 		sbuf_printf(sb, "unknown\n");
223 		return (0);
224 	}
225 	TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) {
226 		if (index-- == 0) {
227 			sbuf_printf(sb, "%s\n", scsi_host->name);
228 			return (0);
229 		}
230 	}
231 	sbuf_printf(sb, "unknown\n");
232 	return (0);
233 }
234 
235 /*
236  * Filler function for device sym-link
237  */
238 static int
linsysfs_link_scsi_host(PFS_FILL_ARGS)239 linsysfs_link_scsi_host(PFS_FILL_ARGS)
240 {
241 	struct scsi_host_queue *scsi_host;
242 	int index;
243 
244 	if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) {
245 		index = atoi(&pn->pn_parent->pn_name[4]);
246 	} else {
247 		sbuf_printf(sb, "unknown\n");
248 		return (0);
249 	}
250 	TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) {
251 		if (index-- == 0) {
252 			sbuf_printf(sb, "../../../devices%s", scsi_host->path);
253 			return(0);
254 		}
255 	}
256 	sbuf_printf(sb, "unknown\n");
257 	return (0);
258 }
259 
260 static int
linsysfs_fill_data(PFS_FILL_ARGS)261 linsysfs_fill_data(PFS_FILL_ARGS)
262 {
263 	sbuf_printf(sb, "%s", (char *)pn->pn_data);
264 	return (0);
265 }
266 
267 static int
linsysfs_fill_vendor(PFS_FILL_ARGS)268 linsysfs_fill_vendor(PFS_FILL_ARGS)
269 {
270 	sbuf_printf(sb, "0x%04x\n", pci_get_vendor((device_t)pn->pn_data));
271 	return (0);
272 }
273 
274 static int
linsysfs_fill_device(PFS_FILL_ARGS)275 linsysfs_fill_device(PFS_FILL_ARGS)
276 {
277 	sbuf_printf(sb, "0x%04x\n", pci_get_device((device_t)pn->pn_data));
278 	return (0);
279 }
280 
281 static int
linsysfs_fill_subvendor(PFS_FILL_ARGS)282 linsysfs_fill_subvendor(PFS_FILL_ARGS)
283 {
284 	sbuf_printf(sb, "0x%04x\n", pci_get_subvendor((device_t)pn->pn_data));
285 	return (0);
286 }
287 
288 static int
linsysfs_fill_subdevice(PFS_FILL_ARGS)289 linsysfs_fill_subdevice(PFS_FILL_ARGS)
290 {
291 	sbuf_printf(sb, "0x%04x\n", pci_get_subdevice((device_t)pn->pn_data));
292 	return (0);
293 }
294 
295 static int
linsysfs_fill_revid(PFS_FILL_ARGS)296 linsysfs_fill_revid(PFS_FILL_ARGS)
297 {
298 	sbuf_printf(sb, "0x%x\n", pci_get_revid((device_t)pn->pn_data));
299 	return (0);
300 }
301 
302 static int
linsysfs_fill_config(PFS_FILL_ARGS)303 linsysfs_fill_config(PFS_FILL_ARGS)
304 {
305 	uint8_t config[48];
306 	device_t dev;
307 	uint32_t reg;
308 
309 	dev = (device_t)pn->pn_data;
310 	bzero(config, sizeof(config));
311 	reg = pci_get_vendor(dev);
312 	config[0] = reg;
313 	config[1] = reg >> 8;
314 	reg = pci_get_device(dev);
315 	config[2] = reg;
316 	config[3] = reg >> 8;
317 	reg = pci_get_revid(dev);
318 	config[8] = reg;
319 	reg = pci_get_subvendor(dev);
320 	config[44] = reg;
321 	config[45] = reg >> 8;
322 	reg = pci_get_subdevice(dev);
323 	config[46] = reg;
324 	config[47] = reg >> 8;
325 	sbuf_bcat(sb, config, sizeof(config));
326 	return (0);
327 }
328 
329 /*
330  * Filler function for PCI uevent file
331  */
332 static int
linsysfs_fill_uevent_pci(PFS_FILL_ARGS)333 linsysfs_fill_uevent_pci(PFS_FILL_ARGS)
334 {
335 	device_t dev;
336 
337 	dev = (device_t)pn->pn_data;
338 	sbuf_printf(sb, "DRIVER=%s\nPCI_CLASS=%X\nPCI_ID=%04X:%04X\n"
339 	    "PCI_SUBSYS_ID=%04X:%04X\nPCI_SLOT_NAME=%04d:%02x:%02x.%x\n",
340 	    linux_driver_get_name_dev(dev), pci_get_class(dev),
341 	    pci_get_vendor(dev), pci_get_device(dev), pci_get_subvendor(dev),
342 	    pci_get_subdevice(dev), pci_get_domain(dev), pci_get_bus(dev),
343 	    pci_get_slot(dev), pci_get_function(dev));
344 	return (0);
345 }
346 
347 /*
348  * Filler function for drm uevent file
349  */
350 static int
linsysfs_fill_uevent_drm(PFS_FILL_ARGS)351 linsysfs_fill_uevent_drm(PFS_FILL_ARGS)
352 {
353 	device_t dev;
354 	int unit;
355 
356 	dev = (device_t)pn->pn_data;
357 	unit = device_get_unit(dev);
358 	sbuf_printf(sb,
359 	    "MAJOR=226\nMINOR=%d\nDEVNAME=dri/card%d\nDEVTYPE=dri_minor\n",
360 	    unit, unit);
361 	return (0);
362 }
363 
364 static char *
get_full_pfs_path(struct pfs_node * cur)365 get_full_pfs_path(struct pfs_node *cur)
366 {
367 	char *temp, *path;
368 
369 	temp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
370 	path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
371 	path[0] = '\0';
372 
373 	do {
374 		snprintf(temp, MAXPATHLEN, "%s/%s", cur->pn_name, path);
375 		strlcpy(path, temp, MAXPATHLEN);
376 		cur = cur->pn_parent;
377 	} while (cur->pn_parent != NULL);
378 
379 	path[strlen(path) - 1] = '\0'; /* remove extra slash */
380 	free(temp, M_TEMP);
381 	return (path);
382 }
383 
384 /*
385  * Filler function for symlink from drm char device to PCI device
386  */
387 static int
linsysfs_fill_vgapci(PFS_FILL_ARGS)388 linsysfs_fill_vgapci(PFS_FILL_ARGS)
389 {
390 	char *path;
391 
392 	path = get_full_pfs_path((struct pfs_node*)pn->pn_data);
393 	sbuf_printf(sb, "../../../%s", path);
394 	free(path, M_TEMP);
395 	return (0);
396 }
397 
398 #undef PCI_DEV
399 #define PCI_DEV "pci"
400 #define DRMN_DEV "drmn"
401 static int
linsysfs_run_bus(device_t dev,struct pfs_node * dir,struct pfs_node * scsi,struct pfs_node * chardev,struct pfs_node * drm,char * path,char * prefix)402 linsysfs_run_bus(device_t dev, struct pfs_node *dir, struct pfs_node *scsi,
403     struct pfs_node *chardev, struct pfs_node *drm, char *path, char *prefix)
404 {
405 	struct scsi_host_queue *scsi_host;
406 	struct pfs_node *sub_dir, *cur_file;
407 	int i, nchildren, error;
408 	device_t *children, parent;
409 	devclass_t devclass;
410 	const char *name = NULL;
411 	struct pci_devinfo *dinfo;
412 	char *device, *host, *new_path, *devname;
413 
414 	new_path = path;
415 	devname = malloc(16, M_TEMP, M_WAITOK);
416 
417 	parent = device_get_parent(dev);
418 	if (parent) {
419 		devclass = device_get_devclass(parent);
420 		if (devclass != NULL)
421 			name = devclass_get_name(devclass);
422 		if (name && strcmp(name, PCI_DEV) == 0) {
423 			dinfo = device_get_ivars(dev);
424 			if (dinfo) {
425 				device = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
426 				new_path = malloc(MAXPATHLEN, M_TEMP,
427 				    M_WAITOK);
428 				new_path[0] = '\000';
429 				strcpy(new_path, path);
430 				host = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
431 				device[0] = '\000';
432 				sprintf(device, "%s:%02x:%02x.%x",
433 				    prefix,
434 				    dinfo->cfg.bus,
435 				    dinfo->cfg.slot,
436 				    dinfo->cfg.func);
437 				strcat(new_path, "/");
438 				strcat(new_path, device);
439 				dir = pfs_create_dir(dir, device,
440 				    NULL, NULL, NULL, 0);
441 				cur_file = pfs_create_file(dir, "vendor",
442 				    &linsysfs_fill_vendor, NULL, NULL, NULL,
443 				    PFS_RD);
444 				cur_file->pn_data = (void*)dev;
445 				cur_file = pfs_create_file(dir, "device",
446 				    &linsysfs_fill_device, NULL, NULL, NULL,
447 				    PFS_RD);
448 				cur_file->pn_data = (void*)dev;
449 				cur_file = pfs_create_file(dir,
450 				    "subsystem_vendor",
451 				    &linsysfs_fill_subvendor, NULL, NULL, NULL,
452 				    PFS_RD);
453 				cur_file->pn_data = (void*)dev;
454 				cur_file = pfs_create_file(dir,
455 				    "subsystem_device",
456 				    &linsysfs_fill_subdevice, NULL, NULL, NULL,
457 				    PFS_RD);
458 				cur_file->pn_data = (void*)dev;
459 				cur_file = pfs_create_file(dir, "revision",
460 				    &linsysfs_fill_revid, NULL, NULL, NULL,
461 				    PFS_RD);
462 				cur_file->pn_data = (void*)dev;
463 				cur_file = pfs_create_file(dir, "config",
464 				    &linsysfs_fill_config, NULL, NULL, NULL,
465 				    PFS_RD);
466 				cur_file->pn_data = (void*)dev;
467 				cur_file = pfs_create_file(dir, "uevent",
468 				    &linsysfs_fill_uevent_pci, NULL, NULL,
469 				    NULL, PFS_RD);
470 				cur_file->pn_data = (void*)dev;
471 				cur_file = pfs_create_link(dir, "subsystem",
472 				    &linsysfs_fill_data, NULL, NULL, NULL, 0);
473 				/* libdrm just checks that the link ends in "/pci" */
474 				cur_file->pn_data = "/sys/bus/pci";
475 
476 				if (dinfo->cfg.baseclass == PCIC_STORAGE) {
477 					/* DJA only make this if needed */
478 					sprintf(host, "host%d", host_number++);
479 					strcat(new_path, "/");
480 					strcat(new_path, host);
481 					pfs_create_dir(dir, host,
482 					    NULL, NULL, NULL, 0);
483 					scsi_host = malloc(sizeof(
484 					    struct scsi_host_queue),
485 					    M_DEVBUF, M_NOWAIT);
486 					scsi_host->path = malloc(
487 					    strlen(new_path) + 1,
488 					    M_DEVBUF, M_NOWAIT);
489 					scsi_host->path[0] = '\000';
490 					bcopy(new_path, scsi_host->path,
491 					    strlen(new_path) + 1);
492 					scsi_host->name = "unknown";
493 
494 					sub_dir = pfs_create_dir(scsi, host,
495 					    NULL, NULL, NULL, 0);
496 					pfs_create_link(sub_dir, "device",
497 					    &linsysfs_link_scsi_host,
498 					    NULL, NULL, NULL, 0);
499 					pfs_create_file(sub_dir, "proc_name",
500 					    &linsysfs_scsiname,
501 					    NULL, NULL, NULL, PFS_RD);
502 					scsi_host->name
503 					    = linux_driver_get_name_dev(dev);
504 					TAILQ_INSERT_TAIL(&scsi_host_q,
505 					    scsi_host, scsi_host_next);
506 				}
507 				free(device, M_TEMP);
508 				free(host, M_TEMP);
509 			}
510 		}
511 
512 		devclass = device_get_devclass(dev);
513 		if (devclass != NULL)
514 			name = devclass_get_name(devclass);
515 		else
516 			name = NULL;
517 		if (name != NULL && strcmp(name, DRMN_DEV) == 0 &&
518 		    device_get_unit(dev) >= 0) {
519 			dinfo = device_get_ivars(parent);
520 			if (dinfo != NULL && dinfo->cfg.baseclass == PCIC_DISPLAY) {
521 				pfs_create_dir(dir, "drm", NULL, NULL, NULL, 0);
522 				sprintf(devname, "226:%d",
523 				    device_get_unit(dev));
524 				sub_dir = pfs_create_dir(chardev,
525 				    devname, NULL, NULL, NULL, 0);
526 				cur_file = pfs_create_link(sub_dir,
527 				    "device", &linsysfs_fill_vgapci, NULL,
528 				    NULL, NULL, PFS_RD);
529 				cur_file->pn_data = (void*)dir;
530 				cur_file = pfs_create_file(sub_dir,
531 				    "uevent", &linsysfs_fill_uevent_drm, NULL,
532 				    NULL, NULL, PFS_RD);
533 				cur_file->pn_data = (void*)dev;
534 				sprintf(devname, "card%d",
535 				    device_get_unit(dev));
536 				sub_dir = pfs_create_dir(drm,
537 				    devname, NULL, NULL, NULL, 0);
538 				cur_file = pfs_create_link(sub_dir,
539 				    "device", &linsysfs_fill_vgapci, NULL,
540 				    NULL, NULL, PFS_RD);
541 				cur_file->pn_data = (void*)dir;
542 			}
543 		}
544 	}
545 
546 	error = device_get_children(dev, &children, &nchildren);
547 	if (error == 0) {
548 		for (i = 0; i < nchildren; i++)
549 			if (children[i])
550 				linsysfs_run_bus(children[i], dir, scsi,
551 				    chardev, drm, new_path, prefix);
552 		free(children, M_TEMP);
553 	}
554 	if (new_path != path)
555 		free(new_path, M_TEMP);
556 	free(devname, M_TEMP);
557 
558 	return (1);
559 }
560 
561 /*
562  * Filler function for sys/devices/system/cpu/{online,possible,present}
563  */
564 static int
linsysfs_cpuonline(PFS_FILL_ARGS)565 linsysfs_cpuonline(PFS_FILL_ARGS)
566 {
567 
568 	sbuf_printf(sb, "%d-%d\n", CPU_FIRST(), mp_maxid);
569 	return (0);
570 }
571 
572 /*
573  * Filler function for sys/devices/system/cpu/cpuX/online
574  */
575 static int
linsysfs_cpuxonline(PFS_FILL_ARGS)576 linsysfs_cpuxonline(PFS_FILL_ARGS)
577 {
578 
579 	sbuf_printf(sb, "1\n");
580 	return (0);
581 }
582 
583 static void
linsysfs_listcpus(struct pfs_node * dir)584 linsysfs_listcpus(struct pfs_node *dir)
585 {
586 	struct pfs_node *cpu;
587 	char *name;
588 	int i, count, len;
589 
590 	len = 1;
591 	count = mp_maxcpus;
592 	while (count > 10) {
593 		count /= 10;
594 		len++;
595 	}
596 	len += sizeof("cpu");
597 	name = malloc(len, M_TEMP, M_WAITOK);
598 
599 	for (i = 0; i < mp_ncpus; ++i) {
600 		/* /sys/devices/system/cpu/cpuX */
601 		sprintf(name, "cpu%d", i);
602 		cpu = pfs_create_dir(dir, name, NULL, NULL, NULL, 0);
603 
604 		pfs_create_file(cpu, "online", &linsysfs_cpuxonline,
605 		    NULL, NULL, NULL, PFS_RD);
606 	}
607 	free(name, M_TEMP);
608 }
609 
610 /*
611  * Constructor
612  */
613 static int
linsysfs_init(PFS_INIT_ARGS)614 linsysfs_init(PFS_INIT_ARGS)
615 {
616 	struct pfs_node *root;
617 	struct pfs_node *class;
618 	struct pfs_node *dir, *sys, *cpu;
619 	struct pfs_node *drm;
620 	struct pfs_node *pci;
621 	struct pfs_node *scsi;
622 	struct pfs_node *net;
623 	struct pfs_node *devdir, *chardev;
624 	struct pfs_node *kernel;
625 	devclass_t devclass;
626 	device_t dev;
627 
628 	TAILQ_INIT(&scsi_host_q);
629 
630 	root = pi->pi_root;
631 
632 	/* /sys/bus/... */
633 	dir = pfs_create_dir(root, "bus", NULL, NULL, NULL, 0);
634 
635 	/* /sys/class/... */
636 	class = pfs_create_dir(root, "class", NULL, NULL, NULL, 0);
637 	scsi = pfs_create_dir(class, "scsi_host", NULL, NULL, NULL, 0);
638 	drm = pfs_create_dir(class, "drm", NULL, NULL, NULL, 0);
639 	pfs_create_dir(class, "power_supply", NULL, NULL, NULL, 0);
640 
641 	/* /sys/class/net/.. */
642 	net = pfs_create_dir(class, "net", NULL, NULL, NULL, 0);
643 
644 	/* /sys/dev/... */
645 	devdir = pfs_create_dir(root, "dev", NULL, NULL, NULL, 0);
646 	chardev = pfs_create_dir(devdir, "char", NULL, NULL, NULL, 0);
647 
648 	/* /sys/devices/... */
649 	dir = pfs_create_dir(root, "devices", NULL, NULL, NULL, 0);
650 	pci = pfs_create_dir(dir, "pci0000:00", NULL, NULL, NULL, 0);
651 
652 	devclass = devclass_find("root");
653 	if (devclass == NULL) {
654 		return (0);
655 	}
656 
657 	dev = devclass_get_device(devclass, 0);
658 	linsysfs_run_bus(dev, pci, scsi, chardev, drm, "/pci0000:00", "0000");
659 
660 	/* /sys/devices/system */
661 	sys = pfs_create_dir(dir, "system", NULL, NULL, NULL, 0);
662 
663 	/* /sys/devices/system/cpu */
664 	cpu = pfs_create_dir(sys, "cpu", NULL, NULL, NULL, 0);
665 
666 	pfs_create_file(cpu, "online", &linsysfs_cpuonline,
667 	    NULL, NULL, NULL, PFS_RD);
668 	pfs_create_file(cpu, "possible", &linsysfs_cpuonline,
669 	    NULL, NULL, NULL, PFS_RD);
670 	pfs_create_file(cpu, "present", &linsysfs_cpuonline,
671 	    NULL, NULL, NULL, PFS_RD);
672 
673 	linsysfs_listcpus(cpu);
674 	linsysfs_listnics(net);
675 
676 	/* /sys/kernel */
677 	kernel = pfs_create_dir(root, "kernel", NULL, NULL, NULL, 0);
678 	/* /sys/kernel/debug, mountpoint for lindebugfs. */
679 	pfs_create_dir(kernel, "debug", NULL, NULL, NULL, 0);
680 
681 	return (0);
682 }
683 
684 /*
685  * Destructor
686  */
687 static int
linsysfs_uninit(PFS_INIT_ARGS)688 linsysfs_uninit(PFS_INIT_ARGS)
689 {
690 	struct scsi_host_queue *scsi_host, *scsi_host_tmp;
691 
692 	TAILQ_FOREACH_SAFE(scsi_host, &scsi_host_q, scsi_host_next,
693 	    scsi_host_tmp) {
694 		TAILQ_REMOVE(&scsi_host_q, scsi_host, scsi_host_next);
695 		free(scsi_host->path, M_TEMP);
696 		free(scsi_host, M_TEMP);
697 	}
698 
699 	return (0);
700 }
701 
702 PSEUDOFS(linsysfs, 1, VFCF_JAIL);
703 #if defined(__aarch64__) || defined(__amd64__)
704 MODULE_DEPEND(linsysfs, linux_common, 1, 1, 1);
705 #else
706 MODULE_DEPEND(linsysfs, linux, 1, 1, 1);
707 #endif
708