Deleted Added
sdiff udiff text old ( 111695 ) new ( 111815 )
full compact
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 * $FreeBSD: head/sys/dev/pci/pci_user.c 111695 2003-03-01 08:57:16Z se $
27 *
28 */
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 <pci/pcireg.h>
55#include <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
64static d_open_t pci_open;
65static d_close_t pci_close;
66static int pci_conf_match(struct pci_match_conf *matches, int num_matches,
67 struct pci_conf *match_buf);
68static 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
76struct cdevsw pcicdev = {
77 /* open */ pci_open,
78 /* close */ pci_close,
79 /* read */ noread,
80 /* write */ nowrite,
81 /* ioctl */ pci_ioctl,
82 /* poll */ nopoll,
83 /* mmap */ nommap,
84 /* strategy */ nostrategy,
85 /* name */ "pci",
86 /* maj */ PCI_CDEV,
87 /* dump */ nodump,
88 /* psize */ nopsize,
89 /* flags */ 0,
90};
91
92static int
93pci_open(dev_t dev, int oflags, int devtype, struct thread *td)
94{
95 int error;
96
97 if (oflags & FWRITE) {
98 error = securelevel_gt(td->td_ucred, 0);
99 if (error)
100 return (error);
101 }
102
103 return (0);
104}
105
106static int
107pci_close(dev_t dev, int flag, int devtype, struct thread *td)
108{
109 return 0;
110}
111
112/*
113 * Match a single pci_conf structure against an array of pci_match_conf
114 * structures. The first argument, 'matches', is an array of num_matches
115 * pci_match_conf structures. match_buf is a pointer to the pci_conf
116 * structure that will be compared to every entry in the matches array.
117 * This function returns 1 on failure, 0 on success.
118 */
119static int
120pci_conf_match(struct pci_match_conf *matches, int num_matches,
121 struct pci_conf *match_buf)
122{
123 int i;
124
125 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
126 return(1);
127
128 for (i = 0; i < num_matches; i++) {
129 /*
130 * I'm not sure why someone would do this...but...
131 */
132 if (matches[i].flags == PCI_GETCONF_NO_MATCH)
133 continue;
134
135 /*
136 * Look at each of the match flags. If it's set, do the
137 * comparison. If the comparison fails, we don't have a
138 * match, go on to the next item if there is one.
139 */
140 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0)
141 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
142 continue;
143
144 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0)
145 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
146 continue;
147
148 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0)
149 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
150 continue;
151
152 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0)
153 && (match_buf->pc_vendor != matches[i].pc_vendor))
154 continue;
155
156 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0)
157 && (match_buf->pc_device != matches[i].pc_device))
158 continue;
159
160 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0)
161 && (match_buf->pc_class != matches[i].pc_class))
162 continue;
163
164 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0)
165 && (match_buf->pd_unit != matches[i].pd_unit))
166 continue;
167
168 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0)
169 && (strncmp(matches[i].pd_name, match_buf->pd_name,
170 sizeof(match_buf->pd_name)) != 0))
171 continue;
172
173 return(0);
174 }
175
176 return(1);
177}
178
179static int
180pci_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
181{
182 device_t pci, pcib;
183 struct pci_io *io;
184 const char *name;
185 int error;
186
187 if (!(flag & FWRITE))
188 return EPERM;
189
190
191 switch(cmd) {
192 case PCIOCGETCONF:
193 {
194 struct pci_devinfo *dinfo;
195 struct pci_conf_io *cio;
196 struct devlist *devlist_head;
197 struct pci_match_conf *pattern_buf;
198 int num_patterns;
199 size_t iolen;
200 int ionum, i;
201
202 cio = (struct pci_conf_io *)data;
203
204 num_patterns = 0;
205 dinfo = NULL;
206
207 /*
208 * Hopefully the user won't pass in a null pointer, but it
209 * can't hurt to check.
210 */
211 if (cio == NULL) {
212 error = EINVAL;
213 break;
214 }
215
216 /*
217 * If the user specified an offset into the device list,
218 * but the list has changed since they last called this
219 * ioctl, tell them that the list has changed. They will
220 * have to get the list from the beginning.
221 */
222 if ((cio->offset != 0)
223 && (cio->generation != pci_generation)){
224 cio->num_matches = 0;
225 cio->status = PCI_GETCONF_LIST_CHANGED;
226 error = 0;
227 break;
228 }
229
230 /*
231 * Check to see whether the user has asked for an offset
232 * past the end of our list.
233 */
234 if (cio->offset >= pci_numdevs) {
235 cio->num_matches = 0;
236 cio->status = PCI_GETCONF_LAST_DEVICE;
237 error = 0;
238 break;
239 }
240
241 /* get the head of the device queue */
242 devlist_head = &pci_devq;
243
244 /*
245 * Determine how much room we have for pci_conf structures.
246 * Round the user's buffer size down to the nearest
247 * multiple of sizeof(struct pci_conf) in case the user
248 * didn't specify a multiple of that size.
249 */
250 iolen = min(cio->match_buf_len -
251 (cio->match_buf_len % sizeof(struct pci_conf)),
252 pci_numdevs * sizeof(struct pci_conf));
253
254 /*
255 * Since we know that iolen is a multiple of the size of
256 * the pciconf union, it's okay to do this.
257 */
258 ionum = iolen / sizeof(struct pci_conf);
259
260 /*
261 * If this test is true, the user wants the pci_conf
262 * structures returned to match the supplied entries.
263 */
264 if ((cio->num_patterns > 0)
265 && (cio->pat_buf_len > 0)) {
266 /*
267 * pat_buf_len needs to be:
268 * num_patterns * sizeof(struct pci_match_conf)
269 * While it is certainly possible the user just
270 * allocated a large buffer, but set the number of
271 * matches correctly, it is far more likely that
272 * their kernel doesn't match the userland utility
273 * they're using. It's also possible that the user
274 * forgot to initialize some variables. Yes, this
275 * may be overly picky, but I hazard to guess that
276 * it's far more likely to just catch folks that
277 * updated their kernel but not their userland.
278 */
279 if ((cio->num_patterns *
280 sizeof(struct pci_match_conf)) != cio->pat_buf_len){
281 /* The user made a mistake, return an error*/
282 cio->status = PCI_GETCONF_ERROR;
283 printf("pci_ioctl: pat_buf_len %d != "
284 "num_patterns (%d) * sizeof(struct "
285 "pci_match_conf) (%d)\npci_ioctl: "
286 "pat_buf_len should be = %d\n",
287 cio->pat_buf_len, cio->num_patterns,
288 (int)sizeof(struct pci_match_conf),
289 (int)sizeof(struct pci_match_conf) *
290 cio->num_patterns);
291 printf("pci_ioctl: do your headers match your "
292 "kernel?\n");
293 cio->num_matches = 0;
294 error = EINVAL;
295 break;
296 }
297
298 /*
299 * Check the user's buffer to make sure it's readable.
300 */
301 if (!useracc((caddr_t)cio->patterns,
302 cio->pat_buf_len, VM_PROT_READ)) {
303 printf("pci_ioctl: pattern buffer %p, "
304 "length %u isn't user accessible for"
305 " READ\n", cio->patterns,
306 cio->pat_buf_len);
307 error = EACCES;
308 break;
309 }
310 /*
311 * Allocate a buffer to hold the patterns.
312 */
313 pattern_buf = malloc(cio->pat_buf_len, M_TEMP,
314 M_WAITOK);
315 error = copyin(cio->patterns, pattern_buf,
316 cio->pat_buf_len);
317 if (error != 0)
318 break;
319 num_patterns = cio->num_patterns;
320
321 } else if ((cio->num_patterns > 0)
322 || (cio->pat_buf_len > 0)) {
323 /*
324 * The user made a mistake, spit out an error.
325 */
326 cio->status = PCI_GETCONF_ERROR;
327 cio->num_matches = 0;
328 printf("pci_ioctl: invalid GETCONF arguments\n");
329 error = EINVAL;
330 break;
331 } else
332 pattern_buf = NULL;
333
334 /*
335 * Make sure we can write to the match buffer.
336 */
337 if (!useracc((caddr_t)cio->matches,
338 cio->match_buf_len, VM_PROT_WRITE)) {
339 printf("pci_ioctl: match buffer %p, length %u "
340 "isn't user accessible for WRITE\n",
341 cio->matches, cio->match_buf_len);
342 error = EACCES;
343 break;
344 }
345
346 /*
347 * Go through the list of devices and copy out the devices
348 * that match the user's criteria.
349 */
350 for (cio->num_matches = 0, error = 0, i = 0,
351 dinfo = STAILQ_FIRST(devlist_head);
352 (dinfo != NULL) && (cio->num_matches < ionum)
353 && (error == 0) && (i < pci_numdevs);
354 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
355
356 if (i < cio->offset)
357 continue;
358
359 /* Populate pd_name and pd_unit */
360 name = NULL;
361 if (dinfo->cfg.dev && dinfo->conf.pd_name[0] == '\0')
362 name = device_get_name(dinfo->cfg.dev);
363 if (name) {
364 strncpy(dinfo->conf.pd_name, name,
365 sizeof(dinfo->conf.pd_name));
366 dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
367 dinfo->conf.pd_unit =
368 device_get_unit(dinfo->cfg.dev);
369 }
370
371 if ((pattern_buf == NULL) ||
372 (pci_conf_match(pattern_buf, num_patterns,
373 &dinfo->conf) == 0)) {
374
375 /*
376 * If we've filled up the user's buffer,
377 * break out at this point. Since we've
378 * got a match here, we'll pick right back
379 * up at the matching entry. We can also
380 * tell the user that there are more matches
381 * left.
382 */
383 if (cio->num_matches >= ionum)
384 break;
385
386 error = copyout(&dinfo->conf,
387 &cio->matches[cio->num_matches],
388 sizeof(struct pci_conf));
389 cio->num_matches++;
390 }
391 }
392
393 /*
394 * Set the pointer into the list, so if the user is getting
395 * n records at a time, where n < pci_numdevs,
396 */
397 cio->offset = i;
398
399 /*
400 * Set the generation, the user will need this if they make
401 * another ioctl call with offset != 0.
402 */
403 cio->generation = pci_generation;
404
405 /*
406 * If this is the last device, inform the user so he won't
407 * bother asking for more devices. If dinfo isn't NULL, we
408 * know that there are more matches in the list because of
409 * the way the traversal is done.
410 */
411 if (dinfo == NULL)
412 cio->status = PCI_GETCONF_LAST_DEVICE;
413 else
414 cio->status = PCI_GETCONF_MORE_DEVS;
415
416 if (pattern_buf != NULL)
417 free(pattern_buf, M_TEMP);
418
419 break;
420 }
421 case PCIOCREAD:
422 io = (struct pci_io *)data;
423 switch(io->pi_width) {
424 case 4:
425 case 2:
426 case 1:
427 /*
428 * Assume that the user-level bus number is
429 * actually the pciN instance number. We map
430 * from that to the real pcib+bus combination.
431 */
432 pci = devclass_get_device(devclass_find("pci"),
433 io->pi_sel.pc_bus);
434 if (pci) {
435 int b = pcib_get_bus(pci);
436 pcib = device_get_parent(pci);
437 io->pi_data =
438 PCIB_READ_CONFIG(pcib,
439 b,
440 io->pi_sel.pc_dev,
441 io->pi_sel.pc_func,
442 io->pi_reg,
443 io->pi_width);
444 error = 0;
445 } else {
446 error = ENODEV;
447 }
448 break;
449 default:
450 error = ENODEV;
451 break;
452 }
453 break;
454
455 case PCIOCWRITE:
456 io = (struct pci_io *)data;
457 switch(io->pi_width) {
458 case 4:
459 case 2:
460 case 1:
461 /*
462 * Assume that the user-level bus number is
463 * actually the pciN instance number. We map
464 * from that to the real pcib+bus combination.
465 */
466 pci = devclass_get_device(devclass_find("pci"),
467 io->pi_sel.pc_bus);
468 if (pci) {
469 int b = pcib_get_bus(pci);
470 pcib = device_get_parent(pci);
471 PCIB_WRITE_CONFIG(pcib,
472 b,
473 io->pi_sel.pc_dev,
474 io->pi_sel.pc_func,
475 io->pi_reg,
476 io->pi_data,
477 io->pi_width);
478 error = 0;
479 } else {
480 error = ENODEV;
481 }
482 break;
483 default:
484 error = ENODEV;
485 break;
486 }
487 break;
488
489 default:
490 error = ENOTTY;
491 break;
492 }
493
494 return (error);
495}
496