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