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