1 /*- 2 * Copyright (c) 2006 IronPort Systems 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/queue.h> 33 #include <sys/blist.h> 34 #include <sys/conf.h> 35 #include <sys/exec.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> 42 #include <sys/proc.h> 43 #include <sys/resourcevar.h> 44 #include <sys/sbuf.h> 45 #include <sys/smp.h> 46 #include <sys/socket.h> 47 #include <sys/vnode.h> 48 #include <sys/bus.h> 49 #include <sys/pciio.h> 50 51 #include <dev/pci/pcivar.h> 52 #include <dev/pci/pcireg.h> 53 54 #include <net/if.h> 55 56 #include <vm/vm.h> 57 #include <vm/pmap.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> 62 63 #include <machine/bus.h> 64 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> 69 70 struct scsi_host_queue { 71 TAILQ_ENTRY(scsi_host_queue) scsi_host_next; 72 char *path; 73 char *name; 74 }; 75 76 TAILQ_HEAD(,scsi_host_queue) scsi_host_q; 77 78 static int host_number = 0; 79 80 static int 81 atoi(const char *str) 82 { 83 return (int)strtol(str, (char **)NULL, 10); 84 } 85 86 /* 87 * Filler function for proc_name 88 */ 89 static int 90 linsysfs_scsiname(PFS_FILL_ARGS) 91 { 92 struct scsi_host_queue *scsi_host; 93 int index; 94 95 if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) { 96 index = atoi(&pn->pn_parent->pn_name[4]); 97 } else { 98 sbuf_printf(sb, "unknown\n"); 99 return (0); 100 } 101 TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) { 102 if (index-- == 0) { 103 sbuf_printf(sb, "%s\n", scsi_host->name); 104 return (0); 105 } 106 } 107 sbuf_printf(sb, "unknown\n"); 108 return (0); 109 } 110 111 /* 112 * Filler function for device sym-link 113 */ 114 static int 115 linsysfs_link_scsi_host(PFS_FILL_ARGS) 116 { 117 struct scsi_host_queue *scsi_host; 118 int index; 119 120 if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) { 121 index = atoi(&pn->pn_parent->pn_name[4]); 122 } else { 123 sbuf_printf(sb, "unknown\n"); 124 return (0); 125 } 126 TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) { 127 if (index-- == 0) { 128 sbuf_printf(sb, "../../../devices%s", scsi_host->path); 129 return(0); 130 } 131 } 132 sbuf_printf(sb, "unknown\n"); 133 return (0); 134 } 135 136 #define PCI_DEV "pci" 137 static int 138 linsysfs_run_bus(device_t dev, struct pfs_node *dir, struct pfs_node *scsi, char *path, 139 char *prefix) 140 { 141 struct scsi_host_queue *scsi_host; 142 struct pfs_node *sub_dir; 143 int i, nchildren; 144 device_t *children, parent; 145 devclass_t devclass; 146 const char *name = NULL; 147 struct pci_devinfo *dinfo; 148 char *device, *host, *new_path = path; 149 150 parent = device_get_parent(dev); 151 if (parent) { 152 devclass = device_get_devclass(parent); 153 if (devclass != NULL) 154 name = devclass_get_name(devclass); 155 if (name && strcmp(name, PCI_DEV) == 0) { 156 dinfo = device_get_ivars(dev); 157 if (dinfo) { 158 device = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 159 new_path = malloc(MAXPATHLEN, M_TEMP, 160 M_WAITOK); 161 new_path[0] = '\000'; 162 strcpy(new_path, path); 163 host = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 164 device[0] = '\000'; 165 sprintf(device, "%s:%02x:%02x.%x", 166 prefix, 167 dinfo->cfg.bus, 168 dinfo->cfg.slot, 169 dinfo->cfg.func); 170 strcat(new_path, "/"); 171 strcat(new_path, device); 172 dir = pfs_create_dir(dir, device, 173 NULL, NULL, NULL, 0); 174 175 if (dinfo->cfg.baseclass == PCIC_STORAGE) { 176 /* DJA only make this if needed */ 177 sprintf(host, "host%d", host_number++); 178 strcat(new_path, "/"); 179 strcat(new_path, host); 180 pfs_create_dir(dir, host, 181 NULL, NULL, NULL, 0); 182 scsi_host = malloc(sizeof( 183 struct scsi_host_queue), 184 M_DEVBUF, M_NOWAIT); 185 scsi_host->path = malloc( 186 strlen(new_path) + 1, 187 M_DEVBUF, M_NOWAIT); 188 scsi_host->path[0] = '\000'; 189 bcopy(new_path, scsi_host->path, 190 strlen(new_path) + 1); 191 scsi_host->name = "unknown"; 192 193 sub_dir = pfs_create_dir(scsi, host, 194 NULL, NULL, NULL, 0); 195 pfs_create_link(sub_dir, "device", 196 &linsysfs_link_scsi_host, 197 NULL, NULL, NULL, 0); 198 pfs_create_file(sub_dir, "proc_name", 199 &linsysfs_scsiname, 200 NULL, NULL, NULL, PFS_RD); 201 scsi_host->name 202 = linux_driver_get_name_dev(dev); 203 TAILQ_INSERT_TAIL(&scsi_host_q, 204 scsi_host, scsi_host_next); 205 } 206 free(device, M_TEMP); 207 free(host, M_TEMP); 208 } 209 } 210 } 211 212 device_get_children(dev, &children, &nchildren); 213 for (i = 0; i < nchildren; i++) { 214 if (children[i]) 215 linsysfs_run_bus(children[i], dir, scsi, new_path, prefix); 216 } 217 if (new_path != path) 218 free(new_path, M_TEMP); 219 220 return (1); 221 } 222 223 /* 224 * Filler function for sys/devices/system/cpu/online 225 */ 226 static int 227 linsysfs_cpuonline(PFS_FILL_ARGS) 228 { 229 230 sbuf_printf(sb, "%d-%d\n", CPU_FIRST(), mp_maxid); 231 return (0); 232 } 233 234 /* 235 * Filler function for sys/devices/system/cpu/cpuX/online 236 */ 237 static int 238 linsysfs_cpuxonline(PFS_FILL_ARGS) 239 { 240 241 sbuf_printf(sb, "1\n"); 242 return (0); 243 } 244 245 static void 246 linsysfs_listcpus(struct pfs_node *dir) 247 { 248 struct pfs_node *cpu; 249 char *name; 250 int i, count, len; 251 252 len = 1; 253 count = mp_maxcpus; 254 while (count > 10) { 255 count /= 10; 256 len++; 257 } 258 len += sizeof("cpu"); 259 name = malloc(len, M_TEMP, M_WAITOK); 260 261 for (i = 0; i < mp_ncpus; ++i) { 262 /* /sys/devices/system/cpu/cpuX */ 263 sprintf(name, "cpu%d", i); 264 cpu = pfs_create_dir(dir, name, NULL, NULL, NULL, 0); 265 266 pfs_create_file(cpu, "online", &linsysfs_cpuxonline, 267 NULL, NULL, NULL, PFS_RD); 268 } 269 free(name, M_TEMP); 270 } 271 272 /* 273 * Constructor 274 */ 275 static int 276 linsysfs_init(PFS_INIT_ARGS) 277 { 278 struct pfs_node *root; 279 struct pfs_node *dir, *sys, *cpu; 280 struct pfs_node *pci; 281 struct pfs_node *scsi; 282 devclass_t devclass; 283 device_t dev; 284 285 TAILQ_INIT(&scsi_host_q); 286 287 root = pi->pi_root; 288 289 /* /sys/class/... */ 290 scsi = pfs_create_dir(root, "class", NULL, NULL, NULL, 0); 291 scsi = pfs_create_dir(scsi, "scsi_host", NULL, NULL, NULL, 0); 292 293 /* /sys/devices */ 294 dir = pfs_create_dir(root, "devices", NULL, NULL, NULL, 0); 295 296 /* /sys/devices/pci0000:00 */ 297 pci = pfs_create_dir(dir, "pci0000:00", NULL, NULL, NULL, 0); 298 299 devclass = devclass_find("root"); 300 if (devclass == NULL) { 301 return (0); 302 } 303 304 dev = devclass_get_device(devclass, 0); 305 linsysfs_run_bus(dev, pci, scsi, "/pci0000:00", "0000"); 306 307 /* /sys/devices/system */ 308 sys = pfs_create_dir(dir, "system", NULL, NULL, NULL, 0); 309 310 /* /sys/devices/system/cpu */ 311 cpu = pfs_create_dir(sys, "cpu", NULL, NULL, NULL, 0); 312 313 pfs_create_file(cpu, "online", &linsysfs_cpuonline, 314 NULL, NULL, NULL, PFS_RD); 315 316 linsysfs_listcpus(cpu); 317 318 return (0); 319 } 320 321 /* 322 * Destructor 323 */ 324 static int 325 linsysfs_uninit(PFS_INIT_ARGS) 326 { 327 struct scsi_host_queue *scsi_host, *scsi_host_tmp; 328 329 TAILQ_FOREACH_SAFE(scsi_host, &scsi_host_q, scsi_host_next, 330 scsi_host_tmp) { 331 TAILQ_REMOVE(&scsi_host_q, scsi_host, scsi_host_next); 332 free(scsi_host->path, M_TEMP); 333 free(scsi_host, M_TEMP); 334 } 335 336 return (0); 337 } 338 339 PSEUDOFS(linsysfs, 1, PR_ALLOW_MOUNT_LINSYSFS); 340 #if defined(__amd64__) 341 MODULE_DEPEND(linsysfs, linux_common, 1, 1, 1); 342 #else 343 MODULE_DEPEND(linsysfs, linux, 1, 1, 1); 344 #endif 345