1f7d4a6bambrisko/*-
22a03579pfg * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
32a03579pfg *
4f7d4a6bambrisko * Copyright (c) 2006 IronPort Systems
5f7d4a6bambrisko * All rights reserved.
6f7d4a6bambrisko *
7f7d4a6bambrisko * Redistribution and use in source and binary forms, with or without
8f7d4a6bambrisko * modification, are permitted provided that the following conditions
9f7d4a6bambrisko * are met:
10f7d4a6bambrisko * 1. Redistributions of source code must retain the above copyright
11f7d4a6bambrisko *    notice, this list of conditions and the following disclaimer.
12f7d4a6bambrisko * 2. Redistributions in binary form must reproduce the above copyright
13f7d4a6bambrisko *    notice, this list of conditions and the following disclaimer in the
14f7d4a6bambrisko *    documentation and/or other materials provided with the distribution.
15f7d4a6bambrisko *
16f7d4a6bambrisko * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17f7d4a6bambrisko * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f7d4a6bambrisko * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f7d4a6bambrisko * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20f7d4a6bambrisko * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f7d4a6bambrisko * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f7d4a6bambrisko * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f7d4a6bambrisko * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f7d4a6bambrisko * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f7d4a6bambrisko * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f7d4a6bambrisko * SUCH DAMAGE.
27f7d4a6bambrisko */
28f7d4a6bambrisko
29f7d4a6bambrisko#include <sys/cdefs.h>
30f7d4a6bambrisko__FBSDID("$FreeBSD$");
31f7d4a6bambrisko
32f7d4a6bambrisko#include <sys/param.h>
332c1ec83glebius#include <sys/systm.h>
342c9faf1dchagin#include <sys/ctype.h>
35f7d4a6bambrisko#include <sys/kernel.h>
36f7d4a6bambrisko#include <sys/malloc.h>
37f7d4a6bambrisko#include <sys/mount.h>
38f7d4a6bambrisko#include <sys/sbuf.h>
39f7d4a6bambrisko#include <sys/smp.h>
40f7d4a6bambrisko#include <sys/socket.h>
41f7d4a6bambrisko#include <sys/bus.h>
42f7d4a6bambrisko#include <sys/pciio.h>
43f7d4a6bambrisko
44f7d4a6bambrisko#include <dev/pci/pcivar.h>
45f7d4a6bambrisko#include <dev/pci/pcireg.h>
46f7d4a6bambrisko
47f7d4a6bambrisko#include <net/if.h>
482c9faf1dchagin#include <net/if_var.h>
492c9faf1dchagin#include <net/if_dl.h>
50f7d4a6bambrisko
512c9faf1dchagin#include <compat/linux/linux.h>
522c9faf1dchagin#include <compat/linux/linux_common.h>
53f7d4a6bambrisko#include <compat/linux/linux_util.h>
54f7d4a6bambrisko#include <fs/pseudofs/pseudofs.h>
55f7d4a6bambrisko
56f7d4a6bambriskostruct scsi_host_queue {
57f7d4a6bambrisko	TAILQ_ENTRY(scsi_host_queue) scsi_host_next;
58f7d4a6bambrisko	char *path;
59f7d4a6bambrisko	char *name;
60f7d4a6bambrisko};
61f7d4a6bambrisko
62f7d4a6bambriskoTAILQ_HEAD(,scsi_host_queue) scsi_host_q;
63f7d4a6bambrisko
64f7d4a6bambriskostatic int host_number = 0;
65f7d4a6bambrisko
66f7d4a6bambriskostatic int
67f7d4a6bambriskoatoi(const char *str)
68f7d4a6bambrisko{
69f7d4a6bambrisko	return (int)strtol(str, (char **)NULL, 10);
70f7d4a6bambrisko}
71f7d4a6bambrisko
722c9faf1dchaginstatic int
732c9faf1dchaginlinsysfs_ifnet_addr(PFS_FILL_ARGS)
742c9faf1dchagin{
752c9faf1dchagin	struct l_sockaddr lsa;
762c9faf1dchagin	struct ifnet *ifp;
772c9faf1dchagin
782c9faf1dchagin	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
792c9faf1dchagin	if (ifp == NULL)
802c9faf1dchagin		return (ENOENT);
812c9faf1dchagin	if (linux_ifhwaddr(ifp, &lsa) != 0)
822c9faf1dchagin		return (ENOENT);
832c9faf1dchagin	sbuf_printf(sb, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
842c9faf1dchagin	    lsa.sa_data[0], lsa.sa_data[1], lsa.sa_data[2],
852c9faf1dchagin	    lsa.sa_data[3], lsa.sa_data[4], lsa.sa_data[5]);
862c9faf1dchagin	return (0);
872c9faf1dchagin}
882c9faf1dchagin
892c9faf1dchaginstatic int
902c9faf1dchaginlinsysfs_ifnet_addrlen(PFS_FILL_ARGS)
912c9faf1dchagin{
922c9faf1dchagin
932c9faf1dchagin	sbuf_printf(sb, "%d\n", LINUX_IFHWADDRLEN);
942c9faf1dchagin	return (0);
952c9faf1dchagin}
962c9faf1dchagin
972c9faf1dchaginstatic int
982c9faf1dchaginlinsysfs_ifnet_flags(PFS_FILL_ARGS)
992c9faf1dchagin{
1002c9faf1dchagin	struct ifnet *ifp;
1012c9faf1dchagin	unsigned short flags;
1022c9faf1dchagin
1032c9faf1dchagin	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
1042c9faf1dchagin	if (ifp == NULL)
1052c9faf1dchagin		return (ENOENT);
1062c9faf1dchagin	linux_ifflags(ifp, &flags);
1072c9faf1dchagin	sbuf_printf(sb, "0x%x\n", flags);
1082c9faf1dchagin	return (0);
1092c9faf1dchagin}
1102c9faf1dchagin
1112c9faf1dchaginstatic int
1122c9faf1dchaginlinsysfs_ifnet_ifindex(PFS_FILL_ARGS)
1132c9faf1dchagin{
1142c9faf1dchagin	struct ifnet *ifp;
1152c9faf1dchagin
1162c9faf1dchagin	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
1172c9faf1dchagin	if (ifp == NULL)
1182c9faf1dchagin		return (ENOENT);
1192c9faf1dchagin	sbuf_printf(sb, "%u\n", ifp->if_index);
1202c9faf1dchagin	return (0);
1212c9faf1dchagin}
1222c9faf1dchagin
1232c9faf1dchaginstatic int
1242c9faf1dchaginlinsysfs_ifnet_mtu(PFS_FILL_ARGS)
1252c9faf1dchagin{
1262c9faf1dchagin	struct ifnet *ifp;
1272c9faf1dchagin
1282c9faf1dchagin	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
1292c9faf1dchagin	if (ifp == NULL)
1302c9faf1dchagin		return (ENOENT);
1312c9faf1dchagin	sbuf_printf(sb, "%u\n", ifp->if_mtu);
1322c9faf1dchagin	return (0);
1332c9faf1dchagin}
1342c9faf1dchagin
1352c9faf1dchaginstatic int
1362c9faf1dchaginlinsysfs_ifnet_tx_queue_len(PFS_FILL_ARGS)
1372c9faf1dchagin{
1382c9faf1dchagin
1392c9faf1dchagin	/* XXX */
1402c9faf1dchagin	sbuf_printf(sb, "1000\n");
1412c9faf1dchagin	return (0);
1422c9faf1dchagin}
1432c9faf1dchagin
1442c9faf1dchaginstatic int
1452c9faf1dchaginlinsysfs_ifnet_type(PFS_FILL_ARGS)
1462c9faf1dchagin{
1472c9faf1dchagin	struct l_sockaddr lsa;
1482c9faf1dchagin	struct ifnet *ifp;
1492c9faf1dchagin
1502c9faf1dchagin	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
1512c9faf1dchagin	if (ifp == NULL)
1522c9faf1dchagin		return (ENOENT);
1532c9faf1dchagin	if (linux_ifhwaddr(ifp, &lsa) != 0)
1542c9faf1dchagin		return (ENOENT);
1552c9faf1dchagin	sbuf_printf(sb, "%d\n", lsa.sa_family);
1562c9faf1dchagin	return (0);
1572c9faf1dchagin}
1582c9faf1dchagin
1592c9faf1dchaginstatic void
1602c9faf1dchaginlinsysfs_listnics(struct pfs_node *dir)
1612c9faf1dchagin{
1622c9faf1dchagin	struct pfs_node *nic;
1632c9faf1dchagin	struct pfs_node *lo;
1642c9faf1dchagin
1652c9faf1dchagin	nic = pfs_create_dir(dir, "eth0", NULL, NULL, NULL, 0);
1662c9faf1dchagin
1672c9faf1dchagin	pfs_create_file(nic, "address", &linsysfs_ifnet_addr,
1682c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
1692c9faf1dchagin
1702c9faf1dchagin	pfs_create_file(nic, "addr_len", &linsysfs_ifnet_addrlen,
1712c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
1722c9faf1dchagin
1732c9faf1dchagin	pfs_create_file(nic, "flags", &linsysfs_ifnet_flags,
1742c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
1752c9faf1dchagin
1762c9faf1dchagin	pfs_create_file(nic, "ifindex", &linsysfs_ifnet_ifindex,
1772c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
1782c9faf1dchagin
1792c9faf1dchagin	pfs_create_file(nic, "mtu", &linsysfs_ifnet_mtu,
1802c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
1812c9faf1dchagin
1822c9faf1dchagin	pfs_create_file(nic, "tx_queue_len", &linsysfs_ifnet_tx_queue_len,
1832c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
1842c9faf1dchagin
1852c9faf1dchagin	pfs_create_file(nic, "type", &linsysfs_ifnet_type,
1862c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
1872c9faf1dchagin
1882c9faf1dchagin	lo = pfs_create_dir(dir, "lo", NULL, NULL, NULL, 0);
1892c9faf1dchagin
1902c9faf1dchagin	pfs_create_file(lo, "address", &linsysfs_ifnet_addr,
1912c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
1922c9faf1dchagin
1932c9faf1dchagin	pfs_create_file(lo, "addr_len", &linsysfs_ifnet_addrlen,
1942c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
1952c9faf1dchagin
1962c9faf1dchagin	pfs_create_file(lo, "flags", &linsysfs_ifnet_flags,
1972c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
1982c9faf1dchagin
1992c9faf1dchagin	pfs_create_file(lo, "ifindex", &linsysfs_ifnet_ifindex,
2002c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
2012c9faf1dchagin
2022c9faf1dchagin	pfs_create_file(lo, "mtu", &linsysfs_ifnet_mtu,
2032c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
2042c9faf1dchagin
2052c9faf1dchagin	pfs_create_file(lo, "tx_queue_len", &linsysfs_ifnet_tx_queue_len,
2062c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
2072c9faf1dchagin
2082c9faf1dchagin	pfs_create_file(lo, "type", &linsysfs_ifnet_type,
2092c9faf1dchagin	    NULL, NULL, NULL, PFS_RD);
2102c9faf1dchagin}
2112c9faf1dchagin
212f7d4a6bambrisko/*
213f7d4a6bambrisko * Filler function for proc_name
214f7d4a6bambrisko */
215f7d4a6bambriskostatic int
216f7d4a6bambriskolinsysfs_scsiname(PFS_FILL_ARGS)
217f7d4a6bambrisko{
218f7d4a6bambrisko	struct scsi_host_queue *scsi_host;
219f7d4a6bambrisko	int index;
220f7d4a6bambrisko
221f7d4a6bambrisko	if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) {
222f7d4a6bambrisko		index = atoi(&pn->pn_parent->pn_name[4]);
223f7d4a6bambrisko	} else {
224f7d4a6bambrisko		sbuf_printf(sb, "unknown\n");
225f7d4a6bambrisko		return (0);
226f7d4a6bambrisko	}
227f7d4a6bambrisko	TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) {
228f7d4a6bambrisko		if (index-- == 0) {
229f7d4a6bambrisko			sbuf_printf(sb, "%s\n", scsi_host->name);
230f7d4a6bambrisko			return (0);
231f7d4a6bambrisko		}
232f7d4a6bambrisko	}
233f7d4a6bambrisko	sbuf_printf(sb, "unknown\n");
234f7d4a6bambrisko	return (0);
235f7d4a6bambrisko}
236f7d4a6bambrisko
237f7d4a6bambrisko/*
238f7d4a6bambrisko * Filler function for device sym-link
239f7d4a6bambrisko */
240f7d4a6bambriskostatic int
241f7d4a6bambriskolinsysfs_link_scsi_host(PFS_FILL_ARGS)
242f7d4a6bambrisko{
243f7d4a6bambrisko	struct scsi_host_queue *scsi_host;
244f7d4a6bambrisko	int index;
245f7d4a6bambrisko
246f7d4a6bambrisko	if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) {
247f7d4a6bambrisko		index = atoi(&pn->pn_parent->pn_name[4]);
248f7d4a6bambrisko	} else {
249f7d4a6bambrisko		sbuf_printf(sb, "unknown\n");
250f7d4a6bambrisko		return (0);
251f7d4a6bambrisko	}
252f7d4a6bambrisko	TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) {
253f7d4a6bambrisko		if (index-- == 0) {
254f7d4a6bambrisko			sbuf_printf(sb, "../../../devices%s", scsi_host->path);
255f7d4a6bambrisko			return(0);
256f7d4a6bambrisko		}
257f7d4a6bambrisko	}
258f7d4a6bambrisko	sbuf_printf(sb, "unknown\n");
259f7d4a6bambrisko	return (0);
260f7d4a6bambrisko}
261f7d4a6bambrisko
26209ad0b9cemstatic int
26309ad0b9cemlinsysfs_fill_data(PFS_FILL_ARGS)
26409ad0b9cem{
26510c4704rlibby	sbuf_printf(sb, "%s", (char *)pn->pn_data);
26609ad0b9cem	return (0);
26709ad0b9cem}
26809ad0b9cem
26909ad0b9cemstatic int
27009ad0b9cemlinsysfs_fill_vendor(PFS_FILL_ARGS)
27109ad0b9cem{
27209ad0b9cem	sbuf_printf(sb, "0x%04x\n", pci_get_vendor((device_t)pn->pn_data));
27309ad0b9cem	return (0);
27409ad0b9cem}
27509ad0b9cem
27609ad0b9cemstatic int
27709ad0b9cemlinsysfs_fill_device(PFS_FILL_ARGS)
27809ad0b9cem{
27909ad0b9cem	sbuf_printf(sb, "0x%04x\n", pci_get_device((device_t)pn->pn_data));
28009ad0b9cem	return (0);
28109ad0b9cem}
28209ad0b9cem
28309ad0b9cemstatic int
28409ad0b9cemlinsysfs_fill_subvendor(PFS_FILL_ARGS)
28509ad0b9cem{
28609ad0b9cem	sbuf_printf(sb, "0x%04x\n", pci_get_subvendor((device_t)pn->pn_data));
28709ad0b9cem	return (0);
28809ad0b9cem}
28909ad0b9cem
29009ad0b9cemstatic int
29109ad0b9cemlinsysfs_fill_subdevice(PFS_FILL_ARGS)
29209ad0b9cem{
29309ad0b9cem	sbuf_printf(sb, "0x%04x\n", pci_get_subdevice((device_t)pn->pn_data));
29409ad0b9cem	return (0);
29509ad0b9cem}
29609ad0b9cem
29709ad0b9cemstatic int
29809ad0b9cemlinsysfs_fill_revid(PFS_FILL_ARGS)
29909ad0b9cem{
30009ad0b9cem	sbuf_printf(sb, "0x%x\n", pci_get_revid((device_t)pn->pn_data));
30109ad0b9cem	return (0);
30209ad0b9cem}
30309ad0b9cem
304d0053c9tijlstatic int
305d0053c9tijllinsysfs_fill_config(PFS_FILL_ARGS)
306d0053c9tijl{
307d0053c9tijl	uint8_t config[48];
308d0053c9tijl	device_t dev;
309d0053c9tijl	uint32_t reg;
310d0053c9tijl
311d0053c9tijl	dev = (device_t)pn->pn_data;
312d0053c9tijl	bzero(config, sizeof(config));
313d0053c9tijl	reg = pci_get_vendor(dev);
314d0053c9tijl	config[0] = reg;
315d0053c9tijl	config[1] = reg >> 8;
316d0053c9tijl	reg = pci_get_device(dev);
317d0053c9tijl	config[2] = reg;
318d0053c9tijl	config[3] = reg >> 8;
319d0053c9tijl	reg = pci_get_revid(dev);
320d0053c9tijl	config[8] = reg;
321d0053c9tijl	reg = pci_get_subvendor(dev);
322d0053c9tijl	config[44] = reg;
323d0053c9tijl	config[45] = reg >> 8;
324d0053c9tijl	reg = pci_get_subdevice(dev);
325d0053c9tijl	config[46] = reg;
326d0053c9tijl	config[47] = reg >> 8;
327d0053c9tijl	sbuf_bcat(sb, config, sizeof(config));
328d0053c9tijl	return (0);
329d0053c9tijl}
330d0053c9tijl
33109ad0b9cem/*
33209ad0b9cem * Filler function for PCI uevent file
33309ad0b9cem */
33409ad0b9cemstatic int
33509ad0b9cemlinsysfs_fill_uevent_pci(PFS_FILL_ARGS)
33609ad0b9cem{
33709ad0b9cem	device_t dev;
33809ad0b9cem
33909ad0b9cem	dev = (device_t)pn->pn_data;
34009ad0b9cem	sbuf_printf(sb, "DRIVER=%s\nPCI_CLASS=%X\nPCI_ID=%04X:%04X\n"
34109ad0b9cem	    "PCI_SUBSYS_ID=%04X:%04X\nPCI_SLOT_NAME=%04d:%02x:%02x.%x\n",
34209ad0b9cem	    linux_driver_get_name_dev(dev), pci_get_class(dev),
34309ad0b9cem	    pci_get_vendor(dev), pci_get_device(dev), pci_get_subvendor(dev),
34409ad0b9cem	    pci_get_subdevice(dev), pci_get_domain(dev), pci_get_bus(dev),
34509ad0b9cem	    pci_get_slot(dev), pci_get_function(dev));
34609ad0b9cem	return (0);
34709ad0b9cem}
34809ad0b9cem
34909ad0b9cem/*
35009ad0b9cem * Filler function for drm uevent file
35109ad0b9cem */
35209ad0b9cemstatic int
35309ad0b9cemlinsysfs_fill_uevent_drm(PFS_FILL_ARGS)
35409ad0b9cem{
35509ad0b9cem	device_t dev;
35609ad0b9cem	int unit;
35709ad0b9cem
35809ad0b9cem	dev = (device_t)pn->pn_data;
35909ad0b9cem	unit = device_get_unit(dev);
36009ad0b9cem	sbuf_printf(sb,
36150660d4tijl	    "MAJOR=226\nMINOR=%d\nDEVNAME=dri/card%d\nDEVTYPE=dri_minor\n",
36250660d4tijl	    unit, unit);
36309ad0b9cem	return (0);
36409ad0b9cem}
36509ad0b9cem
36609ad0b9cemstatic char *
36709ad0b9cemget_full_pfs_path(struct pfs_node *cur)
36809ad0b9cem{
36909ad0b9cem	char *temp, *path;
37009ad0b9cem
37109ad0b9cem	temp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
37209ad0b9cem	path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
37309ad0b9cem	path[0] = '\0';
37409ad0b9cem
37509ad0b9cem	do {
37609ad0b9cem		snprintf(temp, MAXPATHLEN, "%s/%s", cur->pn_name, path);
37709ad0b9cem		strlcpy(path, temp, MAXPATHLEN);
37809ad0b9cem		cur = cur->pn_parent;
37909ad0b9cem	} while (cur->pn_parent != NULL);
38009ad0b9cem
38109ad0b9cem	path[strlen(path) - 1] = '\0'; /* remove extra slash */
38209ad0b9cem	free(temp, M_TEMP);
38309ad0b9cem	return (path);
38409ad0b9cem}
38509ad0b9cem
38609ad0b9cem/*
38709ad0b9cem * Filler function for symlink from drm char device to PCI device
38809ad0b9cem */
38909ad0b9cemstatic int
39009ad0b9cemlinsysfs_fill_vgapci(PFS_FILL_ARGS)
39109ad0b9cem{
39209ad0b9cem	char *path;
39309ad0b9cem
39409ad0b9cem	path = get_full_pfs_path((struct pfs_node*)pn->pn_data);
39509ad0b9cem	sbuf_printf(sb, "../../../%s", path);
39609ad0b9cem	free(path, M_TEMP);
39709ad0b9cem	return (0);
39809ad0b9cem}
39909ad0b9cem
400ef0811cimp#undef PCI_DEV
401f7d4a6bambrisko#define PCI_DEV "pci"
40209ad0b9cem#define DRMN_DEV "drmn"
403f7d4a6bambriskostatic int
404d0053c9tijllinsysfs_run_bus(device_t dev, struct pfs_node *dir, struct pfs_node *scsi,
405d0053c9tijl    struct pfs_node *chardev, struct pfs_node *drm, char *path, char *prefix)
406f7d4a6bambrisko{
407f7d4a6bambrisko	struct scsi_host_queue *scsi_host;
408d0053c9tijl	struct pfs_node *sub_dir, *cur_file;
40959d8427cem	int i, nchildren, error;
410f7d4a6bambrisko	device_t *children, parent;
411f7d4a6bambrisko	devclass_t devclass;
412f7d4a6bambrisko	const char *name = NULL;
413f7d4a6bambrisko	struct pci_devinfo *dinfo;
414d0053c9tijl	char *device, *host, *new_path, *devname;
41509ad0b9cem
41609ad0b9cem	new_path = path;
417d0053c9tijl	devname = malloc(16, M_TEMP, M_WAITOK);
418f7d4a6bambrisko
419f7d4a6bambrisko	parent = device_get_parent(dev);
420f7d4a6bambrisko	if (parent) {
421f7d4a6bambrisko		devclass = device_get_devclass(parent);
422f7d4a6bambrisko		if (devclass != NULL)
423f7d4a6bambrisko			name = devclass_get_name(devclass);
424f7d4a6bambrisko		if (name && strcmp(name, PCI_DEV) == 0) {
425f7d4a6bambrisko			dinfo = device_get_ivars(dev);
426f7d4a6bambrisko			if (dinfo) {
427f7d4a6bambrisko				device = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
428f7d4a6bambrisko				new_path = malloc(MAXPATHLEN, M_TEMP,
429f7d4a6bambrisko				    M_WAITOK);
430f7d4a6bambrisko				new_path[0] = '\000';
431f7d4a6bambrisko				strcpy(new_path, path);
432f7d4a6bambrisko				host = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
433f7d4a6bambrisko				device[0] = '\000';
434f7d4a6bambrisko				sprintf(device, "%s:%02x:%02x.%x",
435f7d4a6bambrisko				    prefix,
436f7d4a6bambrisko				    dinfo->cfg.bus,
437f7d4a6bambrisko				    dinfo->cfg.slot,
438f7d4a6bambrisko				    dinfo->cfg.func);
439f7d4a6bambrisko				strcat(new_path, "/");
440f7d4a6bambrisko				strcat(new_path, device);
441f7d4a6bambrisko				dir = pfs_create_dir(dir, device,
4423d3e50bdes				    NULL, NULL, NULL, 0);
44309ad0b9cem				cur_file = pfs_create_file(dir, "vendor",
44409ad0b9cem				    &linsysfs_fill_vendor, NULL, NULL, NULL,
44509ad0b9cem				    PFS_RD);
44609ad0b9cem				cur_file->pn_data = (void*)dev;
44709ad0b9cem				cur_file = pfs_create_file(dir, "device",
44809ad0b9cem				    &linsysfs_fill_device, NULL, NULL, NULL,
44909ad0b9cem				    PFS_RD);
45009ad0b9cem				cur_file->pn_data = (void*)dev;
45109ad0b9cem				cur_file = pfs_create_file(dir,
45209ad0b9cem				    "subsystem_vendor",
45309ad0b9cem				    &linsysfs_fill_subvendor, NULL, NULL, NULL,
45409ad0b9cem				    PFS_RD);
45509ad0b9cem				cur_file->pn_data = (void*)dev;
45609ad0b9cem				cur_file = pfs_create_file(dir,
45709ad0b9cem				    "subsystem_device",
45809ad0b9cem				    &linsysfs_fill_subdevice, NULL, NULL, NULL,
45909ad0b9cem				    PFS_RD);
46009ad0b9cem				cur_file->pn_data = (void*)dev;
46109ad0b9cem				cur_file = pfs_create_file(dir, "revision",
46209ad0b9cem				    &linsysfs_fill_revid, NULL, NULL, NULL,
46309ad0b9cem				    PFS_RD);
46409ad0b9cem				cur_file->pn_data = (void*)dev;
465d0053c9tijl				cur_file = pfs_create_file(dir, "config",
466d0053c9tijl				    &linsysfs_fill_config, NULL, NULL, NULL,
467d0053c9tijl				    PFS_RD);
468d0053c9tijl				cur_file->pn_data = (void*)dev;
46909ad0b9cem				cur_file = pfs_create_file(dir, "uevent",
47009ad0b9cem				    &linsysfs_fill_uevent_pci, NULL, NULL,
47109ad0b9cem				    NULL, PFS_RD);
47209ad0b9cem				cur_file->pn_data = (void*)dev;
47309ad0b9cem				cur_file = pfs_create_link(dir, "subsystem",
47409ad0b9cem				    &linsysfs_fill_data, NULL, NULL, NULL, 0);
47509ad0b9cem				/* libdrm just checks that the link ends in "/pci" */
47609ad0b9cem				cur_file->pn_data = "/sys/bus/pci";
477f7d4a6bambrisko
478f7d4a6bambrisko				if (dinfo->cfg.baseclass == PCIC_STORAGE) {
479f7d4a6bambrisko					/* DJA only make this if needed */
480f7d4a6bambrisko					sprintf(host, "host%d", host_number++);
481f7d4a6bambrisko					strcat(new_path, "/");
482f7d4a6bambrisko					strcat(new_path, host);
4835c90ddddes					pfs_create_dir(dir, host,
4845c90ddddes					    NULL, NULL, NULL, 0);
485f7d4a6bambrisko					scsi_host = malloc(sizeof(
486f7d4a6bambrisko					    struct scsi_host_queue),
487f7d4a6bambrisko					    M_DEVBUF, M_NOWAIT);
488f7d4a6bambrisko					scsi_host->path = malloc(
489f7d4a6bambrisko					    strlen(new_path) + 1,
490f7d4a6bambrisko					    M_DEVBUF, M_NOWAIT);
491f7d4a6bambrisko					scsi_host->path[0] = '\000';
492f7d4a6bambrisko					bcopy(new_path, scsi_host->path,
493f7d4a6bambrisko					    strlen(new_path) + 1);
494f7d4a6bambrisko					scsi_host->name = "unknown";
495f7d4a6bambrisko
496f7d4a6bambrisko					sub_dir = pfs_create_dir(scsi, host,
4973d3e50bdes					    NULL, NULL, NULL, 0);
498f7d4a6bambrisko					pfs_create_link(sub_dir, "device",
499f7d4a6bambrisko					    &linsysfs_link_scsi_host,
5003d3e50bdes					    NULL, NULL, NULL, 0);
501f7d4a6bambrisko					pfs_create_file(sub_dir, "proc_name",
502f7d4a6bambrisko					    &linsysfs_scsiname,
5033d3e50bdes					    NULL, NULL, NULL, PFS_RD);
504f7d4a6bambrisko					scsi_host->name
505f7d4a6bambrisko					    = linux_driver_get_name_dev(dev);
506f7d4a6bambrisko					TAILQ_INSERT_TAIL(&scsi_host_q,
507f7d4a6bambrisko					    scsi_host, scsi_host_next);
508f7d4a6bambrisko				}
509f7d4a6bambrisko				free(device, M_TEMP);
510f7d4a6bambrisko				free(host, M_TEMP);
511f7d4a6bambrisko			}
512f7d4a6bambrisko		}
51309ad0b9cem
51459d8427cem		devclass = device_get_devclass(dev);
51559d8427cem		if (devclass != NULL)
51659d8427cem			name = devclass_get_name(devclass);
51759d8427cem		else
51859d8427cem			name = NULL;
51959d8427cem		if (name != NULL && strcmp(name, DRMN_DEV) == 0 &&
52059d8427cem		    device_get_unit(dev) >= 0) {
52159d8427cem			dinfo = device_get_ivars(parent);
52259d8427cem			if (dinfo != NULL && dinfo->cfg.baseclass == PCIC_DISPLAY) {
523bee4305tijl				pfs_create_dir(dir, "drm", NULL, NULL, NULL, 0);
524d0053c9tijl				sprintf(devname, "226:%d",
52509ad0b9cem				    device_get_unit(dev));
526d0053c9tijl				sub_dir = pfs_create_dir(chardev,
527d0053c9tijl				    devname, NULL, NULL, NULL, 0);
528d0053c9tijl				cur_file = pfs_create_link(sub_dir,
52909ad0b9cem				    "device", &linsysfs_fill_vgapci, NULL,
53009ad0b9cem				    NULL, NULL, PFS_RD);
53109ad0b9cem				cur_file->pn_data = (void*)dir;
532d0053c9tijl				cur_file = pfs_create_file(sub_dir,
53309ad0b9cem				    "uevent", &linsysfs_fill_uevent_drm, NULL,
53409ad0b9cem				    NULL, NULL, PFS_RD);
53509ad0b9cem				cur_file->pn_data = (void*)dev;
536d0053c9tijl				sprintf(devname, "card%d",
537d0053c9tijl				    device_get_unit(dev));
538d0053c9tijl				sub_dir = pfs_create_dir(drm,
539d0053c9tijl				    devname, NULL, NULL, NULL, 0);
540d0053c9tijl				cur_file = pfs_create_link(sub_dir,
541d0053c9tijl				    "device", &linsysfs_fill_vgapci, NULL,
542d0053c9tijl				    NULL, NULL, PFS_RD);
543d0053c9tijl				cur_file->pn_data = (void*)dir;
54409ad0b9cem			}
54509ad0b9cem		}
546f7d4a6bambrisko	}
547f7d4a6bambrisko
54859d8427cem	error = device_get_children(dev, &children, &nchildren);
54959d8427cem	if (error == 0) {
55059d8427cem		for (i = 0; i < nchildren; i++)
55159d8427cem			if (children[i])
55259d8427cem				linsysfs_run_bus(children[i], dir, scsi,
553d0053c9tijl				    chardev, drm, new_path, prefix);
55459d8427cem		free(children, M_TEMP);
555f7d4a6bambrisko	}
556f7d4a6bambrisko	if (new_path != path)
557f7d4a6bambrisko		free(new_path, M_TEMP);
558d0053c9tijl	free(devname, M_TEMP);
559f7d4a6bambrisko
560f7d4a6bambrisko	return (1);
561f7d4a6bambrisko}
562f7d4a6bambrisko
563f7d4a6bambrisko/*
564c5cd763trasz * Filler function for sys/devices/system/cpu/{online,possible,present}
5655451804dchagin */
5665451804dchaginstatic int
5675451804dchaginlinsysfs_cpuonline(PFS_FILL_ARGS)
5685451804dchagin{
5695451804dchagin
5705451804dchagin	sbuf_printf(sb, "%d-%d\n", CPU_FIRST(), mp_maxid);
5715451804dchagin	return (0);
5725451804dchagin}
5735451804dchagin
5745451804dchagin/*
5755451804dchagin * Filler function for sys/devices/system/cpu/cpuX/online
5765451804dchagin */
5775451804dchaginstatic int
5785451804dchaginlinsysfs_cpuxonline(PFS_FILL_ARGS)
5795451804dchagin{
5805451804dchagin
5815451804dchagin	sbuf_printf(sb, "1\n");
5825451804dchagin	return (0);
5835451804dchagin}
5845451804dchagin
5855451804dchaginstatic void
5865451804dchaginlinsysfs_listcpus(struct pfs_node *dir)
5875451804dchagin{
5885451804dchagin	struct pfs_node *cpu;
5895451804dchagin	char *name;
5905451804dchagin	int i, count, len;
5915451804dchagin
5925451804dchagin	len = 1;
5935451804dchagin	count = mp_maxcpus;
5945451804dchagin	while (count > 10) {
5955451804dchagin		count /= 10;
5965451804dchagin		len++;
5975451804dchagin	}
5985451804dchagin	len += sizeof("cpu");
5995451804dchagin	name = malloc(len, M_TEMP, M_WAITOK);
6005451804dchagin
6015451804dchagin	for (i = 0; i < mp_ncpus; ++i) {
6025451804dchagin		/* /sys/devices/system/cpu/cpuX */
6035451804dchagin		sprintf(name, "cpu%d", i);
6045451804dchagin		cpu = pfs_create_dir(dir, name, NULL, NULL, NULL, 0);
6055451804dchagin
6065451804dchagin		pfs_create_file(cpu, "online", &linsysfs_cpuxonline,
6075451804dchagin		    NULL, NULL, NULL, PFS_RD);
6085451804dchagin	}
6095451804dchagin	free(name, M_TEMP);
6105451804dchagin}
6115451804dchagin
6125451804dchagin/*
613f7d4a6bambrisko * Constructor
614f7d4a6bambrisko */
615f7d4a6bambriskostatic int
616f7d4a6bambriskolinsysfs_init(PFS_INIT_ARGS)
617f7d4a6bambrisko{
618f7d4a6bambrisko	struct pfs_node *root;
619d0053c9tijl	struct pfs_node *class;
6205451804dchagin	struct pfs_node *dir, *sys, *cpu;
621d0053c9tijl	struct pfs_node *drm;
622f7d4a6bambrisko	struct pfs_node *pci;
623f7d4a6bambrisko	struct pfs_node *scsi;
6242c9faf1dchagin	struct pfs_node *net;
6254e5e350trasz	struct pfs_node *power_supply;
62609ad0b9cem	struct pfs_node *devdir, *chardev;
6274f67284markj	struct pfs_node *kernel;
6284f67284markj	struct pfs_node *debug;
629f7d4a6bambrisko	devclass_t devclass;
630f7d4a6bambrisko	device_t dev;
631f7d4a6bambrisko
632f7d4a6bambrisko	TAILQ_INIT(&scsi_host_q);
633f7d4a6bambrisko
634f7d4a6bambrisko	root = pi->pi_root;
635f7d4a6bambrisko
636f7d4a6bambrisko	/* /sys/class/... */
637d0053c9tijl	class = pfs_create_dir(root, "class", NULL, NULL, NULL, 0);
638d0053c9tijl	scsi = pfs_create_dir(class, "scsi_host", NULL, NULL, NULL, 0);
639d0053c9tijl	drm = pfs_create_dir(class, "drm", NULL, NULL, NULL, 0);
6404e5e350trasz	power_supply = pfs_create_dir(class, "power_supply", NULL, NULL, NULL, 0);
641f7d4a6bambrisko
6422c9faf1dchagin	/* /sys/class/net/.. */
6432c9faf1dchagin	net = pfs_create_dir(class, "net", NULL, NULL, NULL, 0);
6442c9faf1dchagin
645d0053c9tijl	/* /sys/dev/... */
64609ad0b9cem	devdir = pfs_create_dir(root, "dev", NULL, NULL, NULL, 0);
64709ad0b9cem	chardev = pfs_create_dir(devdir, "char", NULL, NULL, NULL, 0);
64809ad0b9cem
649d0053c9tijl	/* /sys/devices/... */
650d0053c9tijl	dir = pfs_create_dir(root, "devices", NULL, NULL, NULL, 0);
651d0053c9tijl	pci = pfs_create_dir(dir, "pci0000:00", NULL, NULL, NULL, 0);
652d0053c9tijl
653f7d4a6bambrisko	devclass = devclass_find("root");
654f7d4a6bambrisko	if (devclass == NULL) {
655f7d4a6bambrisko		return (0);
656f7d4a6bambrisko	}
657f7d4a6bambrisko
658f7d4a6bambrisko	dev = devclass_get_device(devclass, 0);
659d0053c9tijl	linsysfs_run_bus(dev, pci, scsi, chardev, drm, "/pci0000:00", "0000");
6605451804dchagin
6615451804dchagin	/* /sys/devices/system */
6625451804dchagin	sys = pfs_create_dir(dir, "system", NULL, NULL, NULL, 0);
6635451804dchagin
6645451804dchagin	/* /sys/devices/system/cpu */
6655451804dchagin	cpu = pfs_create_dir(sys, "cpu", NULL, NULL, NULL, 0);
6665451804dchagin
6675451804dchagin	pfs_create_file(cpu, "online", &linsysfs_cpuonline,
6685451804dchagin	    NULL, NULL, NULL, PFS_RD);
669c5cd763trasz	pfs_create_file(cpu, "possible", &linsysfs_cpuonline,
670c5cd763trasz	    NULL, NULL, NULL, PFS_RD);
671c5cd763trasz	pfs_create_file(cpu, "present", &linsysfs_cpuonline,
672c5cd763trasz	    NULL, NULL, NULL, PFS_RD);
6735451804dchagin
6745451804dchagin	linsysfs_listcpus(cpu);
6752c9faf1dchagin	linsysfs_listnics(net);
6765451804dchagin
6774f67284markj	/* /sys/kernel */
6784f67284markj	kernel = pfs_create_dir(root, "kernel", NULL, NULL, NULL, 0);
6794f67284markj	/* /sys/kernel/debug, mountpoint for lindebugfs. */
6804f67284markj	debug = pfs_create_dir(kernel, "debug", NULL, NULL, NULL, 0);
6814f67284markj
682f7d4a6bambrisko	return (0);
683f7d4a6bambrisko}
684f7d4a6bambrisko
685f7d4a6bambrisko/*
686f7d4a6bambrisko * Destructor
687f7d4a6bambrisko */
688f7d4a6bambriskostatic int
689f7d4a6bambriskolinsysfs_uninit(PFS_INIT_ARGS)
690f7d4a6bambrisko{
69105483cfnetchild	struct scsi_host_queue *scsi_host, *scsi_host_tmp;
692f7d4a6bambrisko
69305483cfnetchild	TAILQ_FOREACH_SAFE(scsi_host, &scsi_host_q, scsi_host_next,
69405483cfnetchild	    scsi_host_tmp) {
695f7d4a6bambrisko		TAILQ_REMOVE(&scsi_host_q, scsi_host, scsi_host_next);
696f7d4a6bambrisko		free(scsi_host->path, M_TEMP);
697f7d4a6bambrisko		free(scsi_host, M_TEMP);
698f7d4a6bambrisko	}
699f7d4a6bambrisko
700f7d4a6bambrisko	return (0);
701f7d4a6bambrisko}
702f7d4a6bambrisko
7031c11f55jamiePSEUDOFS(linsysfs, 1, VFCF_JAIL);
704d3d3a0demaste#if defined(__aarch64__) || defined(__amd64__)
705b08f3f4dchaginMODULE_DEPEND(linsysfs, linux_common, 1, 1, 1);
706b08f3f4dchagin#else
707f7d4a6bambriskoMODULE_DEPEND(linsysfs, linux, 1, 1, 1);
708b08f3f4dchagin#endif
709