2 * Copyright (c) 2006 IronPort Systems
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/queue.h>
33 #include <sys/blist.h>
36 #include <sys/filedesc.h>
37 #include <sys/kernel.h>
38 #include <sys/linker.h>
39 #include <sys/malloc.h>
40 #include <sys/mount.h>
41 #include <sys/mutex.h>
43 #include <sys/resourcevar.h>
46 #include <sys/socket.h>
47 #include <sys/vnode.h>
49 #include <sys/pciio.h>
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pcireg.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_param.h>
60 #include <vm/vm_object.h>
61 #include <vm/swap_pager.h>
63 #include <machine/bus.h>
65 #include <compat/linux/linux_ioctl.h>
66 #include <compat/linux/linux_mib.h>
67 #include <compat/linux/linux_util.h>
68 #include <fs/pseudofs/pseudofs.h>
70 struct scsi_host_queue {
71 TAILQ_ENTRY(scsi_host_queue) scsi_host_next;
76 TAILQ_HEAD(,scsi_host_queue) scsi_host_q;
78 static int host_number = 0;
83 return (int)strtol(str, (char **)NULL, 10);
87 * Filler function for proc_name
90 linsysfs_scsiname(PFS_FILL_ARGS)
92 struct scsi_host_queue *scsi_host;
95 if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) {
96 index = atoi(&pn->pn_parent->pn_name[4]);
98 sbuf_printf(sb, "unknown\n");
101 TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) {
103 sbuf_printf(sb, "%s\n", scsi_host->name);
107 sbuf_printf(sb, "unknown\n");
112 * Filler function for device sym-link
115 linsysfs_link_scsi_host(PFS_FILL_ARGS)
117 struct scsi_host_queue *scsi_host;
120 if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) {
121 index = atoi(&pn->pn_parent->pn_name[4]);
123 sbuf_printf(sb, "unknown\n");
126 TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) {
128 sbuf_printf(sb, "../../../devices%s", scsi_host->path);
132 sbuf_printf(sb, "unknown\n");
137 linsysfs_fill_data(PFS_FILL_ARGS)
139 sbuf_printf(sb, "%s", (char *)pn->pn_data);
144 linsysfs_fill_vendor(PFS_FILL_ARGS)
146 sbuf_printf(sb, "0x%04x\n", pci_get_vendor((device_t)pn->pn_data));
151 linsysfs_fill_device(PFS_FILL_ARGS)
153 sbuf_printf(sb, "0x%04x\n", pci_get_device((device_t)pn->pn_data));
158 linsysfs_fill_subvendor(PFS_FILL_ARGS)
160 sbuf_printf(sb, "0x%04x\n", pci_get_subvendor((device_t)pn->pn_data));
165 linsysfs_fill_subdevice(PFS_FILL_ARGS)
167 sbuf_printf(sb, "0x%04x\n", pci_get_subdevice((device_t)pn->pn_data));
172 linsysfs_fill_revid(PFS_FILL_ARGS)
174 sbuf_printf(sb, "0x%x\n", pci_get_revid((device_t)pn->pn_data));
179 * Filler function for PCI uevent file
182 linsysfs_fill_uevent_pci(PFS_FILL_ARGS)
186 dev = (device_t)pn->pn_data;
187 sbuf_printf(sb, "DRIVER=%s\nPCI_CLASS=%X\nPCI_ID=%04X:%04X\n"
188 "PCI_SUBSYS_ID=%04X:%04X\nPCI_SLOT_NAME=%04d:%02x:%02x.%x\n",
189 linux_driver_get_name_dev(dev), pci_get_class(dev),
190 pci_get_vendor(dev), pci_get_device(dev), pci_get_subvendor(dev),
191 pci_get_subdevice(dev), pci_get_domain(dev), pci_get_bus(dev),
192 pci_get_slot(dev), pci_get_function(dev));
197 * Filler function for drm uevent file
200 linsysfs_fill_uevent_drm(PFS_FILL_ARGS)
205 dev = (device_t)pn->pn_data;
206 unit = device_get_unit(dev);
208 "MAJOR=226\nMINOR=%d\nDEVNAME=drm/%d\nDEVTYPE=dri_minor\n", unit,
214 get_full_pfs_path(struct pfs_node *cur)
218 temp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
219 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
223 snprintf(temp, MAXPATHLEN, "%s/%s", cur->pn_name, path);
224 strlcpy(path, temp, MAXPATHLEN);
225 cur = cur->pn_parent;
226 } while (cur->pn_parent != NULL);
228 path[strlen(path) - 1] = '\0'; /* remove extra slash */
234 * Filler function for symlink from drm char device to PCI device
237 linsysfs_fill_vgapci(PFS_FILL_ARGS)
241 path = get_full_pfs_path((struct pfs_node*)pn->pn_data);
242 sbuf_printf(sb, "../../../%s", path);
247 #define PCI_DEV "pci"
248 #define DRMN_DEV "drmn"
250 linsysfs_run_bus(device_t dev, struct pfs_node *dir, struct pfs_node *scsi, struct pfs_node *chardev,
251 char *path, char *prefix)
253 struct scsi_host_queue *scsi_host;
254 struct pfs_node *sub_dir, *cur_file, *cur_chardev;
255 int i, nchildren, error;
256 device_t *children, parent;
258 const char *name = NULL;
259 struct pci_devinfo *dinfo;
260 char *device, *host, *new_path, *chardevname;
263 chardevname = malloc(16, M_TEMP, M_WAITOK);
265 parent = device_get_parent(dev);
267 devclass = device_get_devclass(parent);
268 if (devclass != NULL)
269 name = devclass_get_name(devclass);
270 if (name && strcmp(name, PCI_DEV) == 0) {
271 dinfo = device_get_ivars(dev);
273 device = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
274 new_path = malloc(MAXPATHLEN, M_TEMP,
276 new_path[0] = '\000';
277 strcpy(new_path, path);
278 host = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
280 sprintf(device, "%s:%02x:%02x.%x",
285 strcat(new_path, "/");
286 strcat(new_path, device);
287 dir = pfs_create_dir(dir, device,
288 NULL, NULL, NULL, 0);
289 cur_file = pfs_create_file(dir, "vendor",
290 &linsysfs_fill_vendor, NULL, NULL, NULL,
292 cur_file->pn_data = (void*)dev;
293 cur_file = pfs_create_file(dir, "device",
294 &linsysfs_fill_device, NULL, NULL, NULL,
296 cur_file->pn_data = (void*)dev;
297 cur_file = pfs_create_file(dir,
299 &linsysfs_fill_subvendor, NULL, NULL, NULL,
301 cur_file->pn_data = (void*)dev;
302 cur_file = pfs_create_file(dir,
304 &linsysfs_fill_subdevice, NULL, NULL, NULL,
306 cur_file->pn_data = (void*)dev;
307 cur_file = pfs_create_file(dir, "revision",
308 &linsysfs_fill_revid, NULL, NULL, NULL,
310 cur_file->pn_data = (void*)dev;
311 cur_file = pfs_create_file(dir, "uevent",
312 &linsysfs_fill_uevent_pci, NULL, NULL,
314 cur_file->pn_data = (void*)dev;
315 cur_file = pfs_create_link(dir, "subsystem",
316 &linsysfs_fill_data, NULL, NULL, NULL, 0);
317 /* libdrm just checks that the link ends in "/pci" */
318 cur_file->pn_data = "/sys/bus/pci";
320 if (dinfo->cfg.baseclass == PCIC_STORAGE) {
321 /* DJA only make this if needed */
322 sprintf(host, "host%d", host_number++);
323 strcat(new_path, "/");
324 strcat(new_path, host);
325 pfs_create_dir(dir, host,
326 NULL, NULL, NULL, 0);
327 scsi_host = malloc(sizeof(
328 struct scsi_host_queue),
330 scsi_host->path = malloc(
331 strlen(new_path) + 1,
333 scsi_host->path[0] = '\000';
334 bcopy(new_path, scsi_host->path,
335 strlen(new_path) + 1);
336 scsi_host->name = "unknown";
338 sub_dir = pfs_create_dir(scsi, host,
339 NULL, NULL, NULL, 0);
340 pfs_create_link(sub_dir, "device",
341 &linsysfs_link_scsi_host,
342 NULL, NULL, NULL, 0);
343 pfs_create_file(sub_dir, "proc_name",
345 NULL, NULL, NULL, PFS_RD);
347 = linux_driver_get_name_dev(dev);
348 TAILQ_INSERT_TAIL(&scsi_host_q,
349 scsi_host, scsi_host_next);
351 free(device, M_TEMP);
356 devclass = device_get_devclass(dev);
357 if (devclass != NULL)
358 name = devclass_get_name(devclass);
361 if (name != NULL && strcmp(name, DRMN_DEV) == 0 &&
362 device_get_unit(dev) >= 0) {
363 dinfo = device_get_ivars(parent);
364 if (dinfo != NULL && dinfo->cfg.baseclass == PCIC_DISPLAY) {
365 sprintf(chardevname, "226:%d",
366 device_get_unit(dev));
367 cur_chardev = pfs_create_dir(chardev,
368 chardevname, NULL, NULL, NULL, 0);
369 cur_file = pfs_create_link(cur_chardev,
370 "device", &linsysfs_fill_vgapci, NULL,
372 cur_file->pn_data = (void*)dir;
373 cur_file = pfs_create_file(cur_chardev,
374 "uevent", &linsysfs_fill_uevent_drm, NULL,
376 cur_file->pn_data = (void*)dev;
381 error = device_get_children(dev, &children, &nchildren);
383 for (i = 0; i < nchildren; i++)
385 linsysfs_run_bus(children[i], dir, scsi,
386 chardev, new_path, prefix);
387 free(children, M_TEMP);
389 if (new_path != path)
390 free(new_path, M_TEMP);
391 free(chardevname, M_TEMP);
397 * Filler function for sys/devices/system/cpu/online
400 linsysfs_cpuonline(PFS_FILL_ARGS)
403 sbuf_printf(sb, "%d-%d\n", CPU_FIRST(), mp_maxid);
408 * Filler function for sys/devices/system/cpu/cpuX/online
411 linsysfs_cpuxonline(PFS_FILL_ARGS)
414 sbuf_printf(sb, "1\n");
419 linsysfs_listcpus(struct pfs_node *dir)
421 struct pfs_node *cpu;
431 len += sizeof("cpu");
432 name = malloc(len, M_TEMP, M_WAITOK);
434 for (i = 0; i < mp_ncpus; ++i) {
435 /* /sys/devices/system/cpu/cpuX */
436 sprintf(name, "cpu%d", i);
437 cpu = pfs_create_dir(dir, name, NULL, NULL, NULL, 0);
439 pfs_create_file(cpu, "online", &linsysfs_cpuxonline,
440 NULL, NULL, NULL, PFS_RD);
449 linsysfs_init(PFS_INIT_ARGS)
451 struct pfs_node *root;
452 struct pfs_node *dir, *sys, *cpu;
453 struct pfs_node *pci;
454 struct pfs_node *scsi;
455 struct pfs_node *devdir, *chardev;
459 TAILQ_INIT(&scsi_host_q);
464 scsi = pfs_create_dir(root, "class", NULL, NULL, NULL, 0);
465 scsi = pfs_create_dir(scsi, "scsi_host", NULL, NULL, NULL, 0);
468 dir = pfs_create_dir(root, "devices", NULL, NULL, NULL, 0);
470 /* /sys/devices/pci0000:00 */
471 pci = pfs_create_dir(dir, "pci0000:00", NULL, NULL, NULL, 0);
474 devdir = pfs_create_dir(root, "dev", NULL, NULL, NULL, 0);
475 chardev = pfs_create_dir(devdir, "char", NULL, NULL, NULL, 0);
477 devclass = devclass_find("root");
478 if (devclass == NULL) {
482 dev = devclass_get_device(devclass, 0);
483 linsysfs_run_bus(dev, pci, scsi, chardev, "/pci0000:00", "0000");
485 /* /sys/devices/system */
486 sys = pfs_create_dir(dir, "system", NULL, NULL, NULL, 0);
488 /* /sys/devices/system/cpu */
489 cpu = pfs_create_dir(sys, "cpu", NULL, NULL, NULL, 0);
491 pfs_create_file(cpu, "online", &linsysfs_cpuonline,
492 NULL, NULL, NULL, PFS_RD);
494 linsysfs_listcpus(cpu);
503 linsysfs_uninit(PFS_INIT_ARGS)
505 struct scsi_host_queue *scsi_host, *scsi_host_tmp;
507 TAILQ_FOREACH_SAFE(scsi_host, &scsi_host_q, scsi_host_next,
509 TAILQ_REMOVE(&scsi_host_q, scsi_host, scsi_host_next);
510 free(scsi_host->path, M_TEMP);
511 free(scsi_host, M_TEMP);
517 PSEUDOFS(linsysfs, 1, PR_ALLOW_MOUNT_LINSYSFS);
518 #if defined(__amd64__)
519 MODULE_DEPEND(linsysfs, linux_common, 1, 1, 1);
521 MODULE_DEPEND(linsysfs, linux, 1, 1, 1);