1 /*- 2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 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 unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_bus.h" /* XXX trim includes */ 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/malloc.h> 35 #include <sys/module.h> 36 #include <sys/linker.h> 37 #include <sys/fcntl.h> 38 #include <sys/conf.h> 39 #include <sys/kernel.h> 40 #include <sys/proc.h> 41 #include <sys/queue.h> 42 #include <sys/types.h> 43 44 #include <vm/vm.h> 45 #include <vm/pmap.h> 46 #include <vm/vm_extern.h> 47 48 #include <sys/bus.h> 49 #include <machine/bus.h> 50 #include <sys/rman.h> 51 #include <machine/resource.h> 52 53 #include <sys/pciio.h> 54 #include <dev/pci/pcireg.h> 55 #include <dev/pci/pcivar.h> 56 57 #include "pcib_if.h" 58 #include "pci_if.h" 59 60 /* 61 * This is the user interface to PCI configuration space. 62 */ 63 64 static d_open_t pci_open; 65 static d_close_t pci_close; 66 static int pci_conf_match(struct pci_match_conf *matches, int num_matches, 67 struct pci_conf *match_buf); 68 static d_ioctl_t pci_ioctl; 69 70 #if __FreeBSD_version < 500000 71 #define PCI_CDEV 78 72 #else 73 #define PCI_CDEV MAJOR_AUTO 74 #endif 75 76 struct cdevsw pcicdev = { 77 .d_open = pci_open, 78 .d_close = pci_close, 79 .d_ioctl = pci_ioctl, 80 .d_name = "pci", 81 .d_maj = PCI_CDEV, 82 }; 83 84 static int 85 pci_open(dev_t dev, int oflags, int devtype, struct thread *td) 86 { 87 int error; 88 89 if (oflags & FWRITE) { 90 error = securelevel_gt(td->td_ucred, 0); 91 if (error) 92 return (error); 93 } 94 95 return (0); 96 } 97 98 static int 99 pci_close(dev_t dev, int flag, int devtype, struct thread *td) 100 { 101 return 0; 102 } 103 104 /* 105 * Match a single pci_conf structure against an array of pci_match_conf 106 * structures. The first argument, 'matches', is an array of num_matches 107 * pci_match_conf structures. match_buf is a pointer to the pci_conf 108 * structure that will be compared to every entry in the matches array. 109 * This function returns 1 on failure, 0 on success. 110 */ 111 static int 112 pci_conf_match(struct pci_match_conf *matches, int num_matches, 113 struct pci_conf *match_buf) 114 { 115 int i; 116 117 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 118 return(1); 119 120 for (i = 0; i < num_matches; i++) { 121 /* 122 * I'm not sure why someone would do this...but... 123 */ 124 if (matches[i].flags == PCI_GETCONF_NO_MATCH) 125 continue; 126 127 /* 128 * Look at each of the match flags. If it's set, do the 129 * comparison. If the comparison fails, we don't have a 130 * match, go on to the next item if there is one. 131 */ 132 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0) 133 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 134 continue; 135 136 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0) 137 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 138 continue; 139 140 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0) 141 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 142 continue; 143 144 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0) 145 && (match_buf->pc_vendor != matches[i].pc_vendor)) 146 continue; 147 148 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0) 149 && (match_buf->pc_device != matches[i].pc_device)) 150 continue; 151 152 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0) 153 && (match_buf->pc_class != matches[i].pc_class)) 154 continue; 155 156 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0) 157 && (match_buf->pd_unit != matches[i].pd_unit)) 158 continue; 159 160 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0) 161 && (strncmp(matches[i].pd_name, match_buf->pd_name, 162 sizeof(match_buf->pd_name)) != 0)) 163 continue; 164 165 return(0); 166 } 167 168 return(1); 169 } 170 171 static int 172 pci_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) 173 { 174 device_t pci, pcib; 175 struct pci_io *io; 176 const char *name; 177 int error; 178 179 if (!(flag & FWRITE) && cmd != PCIOCGETCONF) 180 return EPERM; 181 182 switch(cmd) { 183 case PCIOCGETCONF: 184 { 185 struct pci_devinfo *dinfo; 186 struct pci_conf_io *cio; 187 struct devlist *devlist_head; 188 struct pci_match_conf *pattern_buf; 189 int num_patterns; 190 size_t iolen; 191 int ionum, i; 192 193 cio = (struct pci_conf_io *)data; 194 195 num_patterns = 0; 196 dinfo = NULL; 197 198 /* 199 * If the user specified an offset into the device list, 200 * but the list has changed since they last called this 201 * ioctl, tell them that the list has changed. They will 202 * have to get the list from the beginning. 203 */ 204 if ((cio->offset != 0) 205 && (cio->generation != pci_generation)){ 206 cio->num_matches = 0; 207 cio->status = PCI_GETCONF_LIST_CHANGED; 208 error = 0; 209 break; 210 } 211 212 /* 213 * Check to see whether the user has asked for an offset 214 * past the end of our list. 215 */ 216 if (cio->offset >= pci_numdevs) { 217 cio->num_matches = 0; 218 cio->status = PCI_GETCONF_LAST_DEVICE; 219 error = 0; 220 break; 221 } 222 223 /* get the head of the device queue */ 224 devlist_head = &pci_devq; 225 226 /* 227 * Determine how much room we have for pci_conf structures. 228 * Round the user's buffer size down to the nearest 229 * multiple of sizeof(struct pci_conf) in case the user 230 * didn't specify a multiple of that size. 231 */ 232 iolen = min(cio->match_buf_len - 233 (cio->match_buf_len % sizeof(struct pci_conf)), 234 pci_numdevs * sizeof(struct pci_conf)); 235 236 /* 237 * Since we know that iolen is a multiple of the size of 238 * the pciconf union, it's okay to do this. 239 */ 240 ionum = iolen / sizeof(struct pci_conf); 241 242 /* 243 * If this test is true, the user wants the pci_conf 244 * structures returned to match the supplied entries. 245 */ 246 if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs) 247 && (cio->pat_buf_len > 0)) { 248 /* 249 * pat_buf_len needs to be: 250 * num_patterns * sizeof(struct pci_match_conf) 251 * While it is certainly possible the user just 252 * allocated a large buffer, but set the number of 253 * matches correctly, it is far more likely that 254 * their kernel doesn't match the userland utility 255 * they're using. It's also possible that the user 256 * forgot to initialize some variables. Yes, this 257 * may be overly picky, but I hazard to guess that 258 * it's far more likely to just catch folks that 259 * updated their kernel but not their userland. 260 */ 261 if ((cio->num_patterns * 262 sizeof(struct pci_match_conf)) != cio->pat_buf_len){ 263 /* The user made a mistake, return an error*/ 264 cio->status = PCI_GETCONF_ERROR; 265 cio->num_matches = 0; 266 error = EINVAL; 267 break; 268 } 269 270 /* 271 * Allocate a buffer to hold the patterns. 272 */ 273 pattern_buf = malloc(cio->pat_buf_len, M_TEMP, 274 M_WAITOK); 275 error = copyin(cio->patterns, pattern_buf, 276 cio->pat_buf_len); 277 if (error != 0) { 278 error = EINVAL; 279 goto getconfexit; 280 } 281 num_patterns = cio->num_patterns; 282 283 } else if ((cio->num_patterns > 0) 284 || (cio->pat_buf_len > 0)) { 285 /* 286 * The user made a mistake, spit out an error. 287 */ 288 cio->status = PCI_GETCONF_ERROR; 289 cio->num_matches = 0; 290 error = EINVAL; 291 break; 292 } else 293 pattern_buf = NULL; 294 295 /* 296 * Go through the list of devices and copy out the devices 297 * that match the user's criteria. 298 */ 299 for (cio->num_matches = 0, error = 0, i = 0, 300 dinfo = STAILQ_FIRST(devlist_head); 301 (dinfo != NULL) && (cio->num_matches < ionum) 302 && (error == 0) && (i < pci_numdevs) && (dinfo != NULL); 303 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 304 305 if (i < cio->offset) 306 continue; 307 308 /* Populate pd_name and pd_unit */ 309 name = NULL; 310 if (dinfo->cfg.dev && dinfo->conf.pd_name[0] == '\0') 311 name = device_get_name(dinfo->cfg.dev); 312 if (name) { 313 strncpy(dinfo->conf.pd_name, name, 314 sizeof(dinfo->conf.pd_name)); 315 dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0; 316 dinfo->conf.pd_unit = 317 device_get_unit(dinfo->cfg.dev); 318 } 319 320 if ((pattern_buf == NULL) || 321 (pci_conf_match(pattern_buf, num_patterns, 322 &dinfo->conf) == 0)) { 323 324 /* 325 * If we've filled up the user's buffer, 326 * break out at this point. Since we've 327 * got a match here, we'll pick right back 328 * up at the matching entry. We can also 329 * tell the user that there are more matches 330 * left. 331 */ 332 if (cio->num_matches >= ionum) 333 break; 334 335 /* only if can copy it out do we count it */ 336 if (!(error = copyout(&dinfo->conf, 337 &cio->matches[cio->num_matches], 338 sizeof(struct pci_conf)))) { 339 cio->num_matches++; 340 } 341 } 342 } 343 344 /* 345 * Set the pointer into the list, so if the user is getting 346 * n records at a time, where n < pci_numdevs, 347 */ 348 cio->offset = i; 349 350 /* 351 * Set the generation, the user will need this if they make 352 * another ioctl call with offset != 0. 353 */ 354 cio->generation = pci_generation; 355 356 /* 357 * If this is the last device, inform the user so he won't 358 * bother asking for more devices. If dinfo isn't NULL, we 359 * know that there are more matches in the list because of 360 * the way the traversal is done. 361 */ 362 if (dinfo == NULL) 363 cio->status = PCI_GETCONF_LAST_DEVICE; 364 else 365 cio->status = PCI_GETCONF_MORE_DEVS; 366 367 getconfexit: 368 if (pattern_buf != NULL) 369 free(pattern_buf, M_TEMP); 370 371 break; 372 } 373 374 case PCIOCREAD: 375 case PCIOCWRITE: 376 io = (struct pci_io *)data; 377 switch(io->pi_width) { 378 case 4: 379 case 2: 380 case 1: 381 /* make sure register is in bounds and aligned */ 382 if (cmd == PCIOCREAD || cmd == PCIOCWRITE) 383 if (io->pi_reg < 0 || 384 io->pi_reg + io->pi_width > PCI_REGMAX || 385 io->pi_reg & (io->pi_width - 1)) 386 error = EINVAL; 387 388 /* 389 * Assume that the user-level bus number is 390 * actually the pciN instance number. We map 391 * from that to the real pcib+bus combination. 392 */ 393 pci = devclass_get_device(devclass_find("pci"), 394 io->pi_sel.pc_bus); 395 if (pci) { 396 int b = pcib_get_bus(pci); 397 pcib = device_get_parent(pci); 398 if (cmd == PCIOCWRITE) 399 PCIB_WRITE_CONFIG(pcib, 400 b, 401 io->pi_sel.pc_dev, 402 io->pi_sel.pc_func, 403 io->pi_reg, 404 io->pi_data, 405 io->pi_width); 406 else 407 io->pi_data = 408 PCIB_READ_CONFIG(pcib, 409 b, 410 io->pi_sel.pc_dev, 411 io->pi_sel.pc_func, 412 io->pi_reg, 413 io->pi_width); 414 error = 0; 415 } else { 416 error = ENODEV; 417 } 418 break; 419 default: 420 error = EINVAL; 421 break; 422 } 423 break; 424 425 default: 426 error = ENOTTY; 427 break; 428 } 429 430 return (error); 431 } 432