71#include <compat/linux/linux_ioctl.h> 72#include <compat/linux/linux_mib.h> 73#include <compat/linux/linux_util.h> 74#include <fs/pseudofs/pseudofs.h> 75 76struct scsi_host_queue { 77 TAILQ_ENTRY(scsi_host_queue) scsi_host_next; 78 char *path; 79 char *name; 80}; 81 82TAILQ_HEAD(,scsi_host_queue) scsi_host_q; 83 84static int host_number = 0; 85 86static int 87atoi(const char *str) 88{ 89 return (int)strtol(str, (char **)NULL, 10); 90} 91 92/* 93 * Filler function for proc_name 94 */ 95static int 96linsysfs_scsiname(PFS_FILL_ARGS) 97{ 98 struct scsi_host_queue *scsi_host; 99 int index; 100 101 if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) { 102 index = atoi(&pn->pn_parent->pn_name[4]); 103 } else { 104 sbuf_printf(sb, "unknown\n"); 105 return (0); 106 } 107 TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) { 108 if (index-- == 0) { 109 sbuf_printf(sb, "%s\n", scsi_host->name); 110 return (0); 111 } 112 } 113 sbuf_printf(sb, "unknown\n"); 114 return (0); 115} 116 117/* 118 * Filler function for device sym-link 119 */ 120static int 121linsysfs_link_scsi_host(PFS_FILL_ARGS) 122{ 123 struct scsi_host_queue *scsi_host; 124 int index; 125 126 if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) { 127 index = atoi(&pn->pn_parent->pn_name[4]); 128 } else { 129 sbuf_printf(sb, "unknown\n"); 130 return (0); 131 } 132 TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) { 133 if (index-- == 0) { 134 sbuf_printf(sb, "../../../devices%s", scsi_host->path); 135 return(0); 136 } 137 } 138 sbuf_printf(sb, "unknown\n"); 139 return (0); 140} 141 142#define PCI_DEV "pci" 143static int 144linsysfs_run_bus(device_t dev, struct pfs_node *dir, struct pfs_node *scsi, char *path, 145 char *prefix) 146{ 147 struct scsi_host_queue *scsi_host; 148 struct pfs_node *sub_dir; 149 int i, nchildren; 150 device_t *children, parent; 151 devclass_t devclass; 152 const char *name = NULL; 153 struct pci_devinfo *dinfo; 154 char *device, *host, *new_path = path; 155 156 parent = device_get_parent(dev); 157 if (parent) { 158 devclass = device_get_devclass(parent); 159 if (devclass != NULL) 160 name = devclass_get_name(devclass); 161 if (name && strcmp(name, PCI_DEV) == 0) { 162 dinfo = device_get_ivars(dev); 163 if (dinfo) { 164 device = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 165 new_path = malloc(MAXPATHLEN, M_TEMP, 166 M_WAITOK); 167 new_path[0] = '\000'; 168 strcpy(new_path, path); 169 host = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 170 device[0] = '\000'; 171 sprintf(device, "%s:%02x:%02x.%x", 172 prefix, 173 dinfo->cfg.bus, 174 dinfo->cfg.slot, 175 dinfo->cfg.func); 176 strcat(new_path, "/"); 177 strcat(new_path, device); 178 dir = pfs_create_dir(dir, device, 179 NULL, NULL, NULL, 0); 180 181 if (dinfo->cfg.baseclass == PCIC_STORAGE) { 182 /* DJA only make this if needed */ 183 sprintf(host, "host%d", host_number++); 184 strcat(new_path, "/"); 185 strcat(new_path, host); 186 pfs_create_dir(dir, host, 187 NULL, NULL, NULL, 0); 188 scsi_host = malloc(sizeof( 189 struct scsi_host_queue), 190 M_DEVBUF, M_NOWAIT); 191 scsi_host->path = malloc( 192 strlen(new_path) + 1, 193 M_DEVBUF, M_NOWAIT); 194 scsi_host->path[0] = '\000'; 195 bcopy(new_path, scsi_host->path, 196 strlen(new_path) + 1); 197 scsi_host->name = "unknown"; 198 199 sub_dir = pfs_create_dir(scsi, host, 200 NULL, NULL, NULL, 0); 201 pfs_create_link(sub_dir, "device", 202 &linsysfs_link_scsi_host, 203 NULL, NULL, NULL, 0); 204 pfs_create_file(sub_dir, "proc_name", 205 &linsysfs_scsiname, 206 NULL, NULL, NULL, PFS_RD); 207 scsi_host->name 208 = linux_driver_get_name_dev(dev); 209 TAILQ_INSERT_TAIL(&scsi_host_q, 210 scsi_host, scsi_host_next); 211 } 212 free(device, M_TEMP); 213 free(host, M_TEMP); 214 } 215 } 216 } 217 218 device_get_children(dev, &children, &nchildren); 219 for (i = 0; i < nchildren; i++) { 220 if (children[i]) 221 linsysfs_run_bus(children[i], dir, scsi, new_path, prefix); 222 } 223 if (new_path != path) 224 free(new_path, M_TEMP); 225 226 return (1); 227} 228 229/* 230 * Constructor 231 */ 232static int 233linsysfs_init(PFS_INIT_ARGS) 234{ 235 struct pfs_node *root; 236 struct pfs_node *dir; 237 struct pfs_node *pci; 238 struct pfs_node *scsi; 239 devclass_t devclass; 240 device_t dev; 241 242 TAILQ_INIT(&scsi_host_q); 243 244 root = pi->pi_root; 245 246 /* /sys/class/... */ 247 scsi = pfs_create_dir(root, "class", NULL, NULL, NULL, 0); 248 scsi = pfs_create_dir(scsi, "scsi_host", NULL, NULL, NULL, 0); 249 250 /* /sys/device */ 251 dir = pfs_create_dir(root, "devices", NULL, NULL, NULL, 0); 252 253 /* /sys/device/pci0000:00 */ 254 pci = pfs_create_dir(dir, "pci0000:00", NULL, NULL, NULL, 0); 255 256 devclass = devclass_find("root"); 257 if (devclass == NULL) { 258 return (0); 259 } 260 261 dev = devclass_get_device(devclass, 0); 262 linsysfs_run_bus(dev, pci, scsi, "/pci0000:00", "0000"); 263 return (0); 264} 265 266/* 267 * Destructor 268 */ 269static int 270linsysfs_uninit(PFS_INIT_ARGS) 271{ 272 struct scsi_host_queue *scsi_host, *scsi_host_tmp; 273 274 TAILQ_FOREACH_SAFE(scsi_host, &scsi_host_q, scsi_host_next, 275 scsi_host_tmp) { 276 TAILQ_REMOVE(&scsi_host_q, scsi_host, scsi_host_next); 277 free(scsi_host->path, M_TEMP); 278 free(scsi_host, M_TEMP); 279 } 280 281 return (0); 282} 283 284PSEUDOFS(linsysfs, 1, 0);
| 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 70struct scsi_host_queue { 71 TAILQ_ENTRY(scsi_host_queue) scsi_host_next; 72 char *path; 73 char *name; 74}; 75 76TAILQ_HEAD(,scsi_host_queue) scsi_host_q; 77 78static int host_number = 0; 79 80static int 81atoi(const char *str) 82{ 83 return (int)strtol(str, (char **)NULL, 10); 84} 85 86/* 87 * Filler function for proc_name 88 */ 89static int 90linsysfs_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 */ 114static int 115linsysfs_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" 137static int 138linsysfs_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 * Constructor 225 */ 226static int 227linsysfs_init(PFS_INIT_ARGS) 228{ 229 struct pfs_node *root; 230 struct pfs_node *dir; 231 struct pfs_node *pci; 232 struct pfs_node *scsi; 233 devclass_t devclass; 234 device_t dev; 235 236 TAILQ_INIT(&scsi_host_q); 237 238 root = pi->pi_root; 239 240 /* /sys/class/... */ 241 scsi = pfs_create_dir(root, "class", NULL, NULL, NULL, 0); 242 scsi = pfs_create_dir(scsi, "scsi_host", NULL, NULL, NULL, 0); 243 244 /* /sys/device */ 245 dir = pfs_create_dir(root, "devices", NULL, NULL, NULL, 0); 246 247 /* /sys/device/pci0000:00 */ 248 pci = pfs_create_dir(dir, "pci0000:00", NULL, NULL, NULL, 0); 249 250 devclass = devclass_find("root"); 251 if (devclass == NULL) { 252 return (0); 253 } 254 255 dev = devclass_get_device(devclass, 0); 256 linsysfs_run_bus(dev, pci, scsi, "/pci0000:00", "0000"); 257 return (0); 258} 259 260/* 261 * Destructor 262 */ 263static int 264linsysfs_uninit(PFS_INIT_ARGS) 265{ 266 struct scsi_host_queue *scsi_host, *scsi_host_tmp; 267 268 TAILQ_FOREACH_SAFE(scsi_host, &scsi_host_q, scsi_host_next, 269 scsi_host_tmp) { 270 TAILQ_REMOVE(&scsi_host_q, scsi_host, scsi_host_next); 271 free(scsi_host->path, M_TEMP); 272 free(scsi_host, M_TEMP); 273 } 274 275 return (0); 276} 277 278PSEUDOFS(linsysfs, 1, 0);
|