]> mj.ucw.cz Git - pciutils.git/blob - lib/fbsd-device.c
Simplified pci_fill_info() and friends
[pciutils.git] / lib / fbsd-device.c
1 /*
2  *      The PCI Library -- FreeBSD /dev/pci access
3  *
4  *      Copyright (c) 1999 Jari Kirma <kirma@cs.hut.fi>
5  *      Updated in 2003 by Samy Al Bahra <samy@kerneled.com>
6  *      Updated in 2017 by Imre Vadász <imrevdsz@gmail.com>
7  *
8  *      Can be freely distributed and used under the terms of the GNU GPL.
9  */
10
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <osreldate.h>
18 #include <stdint.h>
19
20 #ifdef __FreeBSD_kernel_version
21 #  ifndef __FreeBSD_version
22 #    define __FreeBSD_version __FreeBSD_kernel_version
23 #  endif
24 #endif
25
26 #if __FreeBSD_version < 430000 && !defined(__DragonFly__)
27 #  include <pci/pcivar.h>
28 #  include <pci/pci_ioctl.h>
29 #else
30 #  include <sys/pciio.h>
31 #endif
32
33 #include "internal.h"
34
35 static void
36 fbsd_config(struct pci_access *a)
37 {
38   pci_define_param(a, "fbsd.path", PCI_PATH_FBSD_DEVICE, "Path to the FreeBSD PCI device");
39 }
40
41 static int
42 fbsd_detect(struct pci_access *a)
43 {
44   char *name = pci_get_param(a, "fbsd.path");
45
46   if (access(name, R_OK))
47     {
48       a->warning("Cannot open %s", name);
49       return 0;
50     }
51   a->debug("...using %s", name);
52   return 1;
53 }
54
55 static void
56 fbsd_init(struct pci_access *a)
57 {
58   char *name = pci_get_param(a, "fbsd.path");
59   int fd;
60
61   a->fd = -1;
62   a->fd_rw = -1;
63   /*
64    * When opening /dev/pci as read-write fails, retry with readonly which
65    * will still allow us to gain some information via the PCIOCGETCONF and
66    * PCIOCGETBAR IOCTLs, even without generic read access to the PCI config
67    * space.
68    */
69   fd = open(name, O_RDWR, 0);
70   if (fd < 0)
71     {
72       fd = open(name, O_RDONLY, 0);
73       if (fd < 0)
74         a->error("fbsd_init: %s open failed", name);
75       else
76         {
77           a->debug("fbsd_init: Fallback to read-only opened %s", name);
78           a->fd = fd;
79         }
80     }
81   else
82     a->fd_rw = fd;
83 }
84
85 static void
86 fbsd_cleanup(struct pci_access *a)
87 {
88   if (a->fd >= 0)
89     {
90       close(a->fd);
91       a->fd = -1;
92     }
93   if (a->fd_rw >= 0)
94     {
95       close(a->fd_rw);
96       a->fd_rw = -1;
97     }
98 }
99
100 static void
101 fbsd_scan(struct pci_access *a)
102 {
103   struct pci_conf_io conf;
104   struct pci_conf *matches;
105   struct pci_dev *t;
106   uint32_t offset = 0;
107   unsigned int i;
108
109   matches = calloc(32, sizeof(struct pci_conf));
110   if (matches == NULL)
111     {
112       a->error("calloc: %s", strerror(errno));
113       return;
114     }
115
116   conf.generation = 0;
117   do
118     {
119       conf.pat_buf_len = 0;
120       conf.num_patterns = 0;
121       conf.patterns = NULL;
122       conf.match_buf_len = 32 * sizeof(struct pci_conf);
123       conf.num_matches = 32;
124       conf.matches = matches;
125       conf.offset = offset;
126       conf.status = 0;
127       if (ioctl(a->fd_rw >= 0 ? a->fd_rw : a->fd, PCIOCGETCONF, &conf) < 0)
128         {
129           if (errno == ENODEV)
130             break;
131           a->error("fbsd_scan: ioctl(PCIOCGETCONF) failed: %s",
132               strerror(errno));
133         }
134       /* PCI_GETCONF_LIST_CHANGED would require us to start over. */
135       if (conf.status == PCI_GETCONF_ERROR ||
136           conf.status == PCI_GETCONF_LIST_CHANGED)
137         {
138           a->error("fbsd_scan: ioctl(PCIOCGETCONF) failed");
139           break;
140         }
141       for (i = 0; i < conf.num_matches; i++)
142         {
143           t = pci_alloc_dev(a);
144           t->bus = matches[i].pc_sel.pc_bus;
145           t->dev = matches[i].pc_sel.pc_dev;
146           t->func = matches[i].pc_sel.pc_func;
147           t->domain = matches[i].pc_sel.pc_domain;
148           t->domain_16 = matches[i].pc_sel.pc_domain;
149           t->vendor_id = matches[i].pc_vendor;
150           t->device_id = matches[i].pc_device;
151           t->known_fields = PCI_FILL_IDENT;
152           t->hdrtype = matches[i].pc_hdr;
153           pci_link_dev(a, t);
154         }
155       offset += conf.num_matches;
156     }
157   while (conf.status == PCI_GETCONF_MORE_DEVS);
158
159   free(matches);
160 }
161
162 static void
163 fbsd_fill_info(struct pci_dev *d, int flags)
164 {
165   struct pci_conf_io conf;
166   struct pci_bar_io bar;
167   struct pci_match_conf pattern;
168   struct pci_conf match;
169   int i;
170
171   if (d->access->fd_rw >= 0)
172     return pci_generic_fill_info(d, flags);
173
174   /*
175    * Can only handle PCI_FILL_IDENT, PCI_FILL_CLASS, PCI_FILL_BASES and
176    * PCI_FILL_SIZES requests with the PCIOCGETCONF and PCIOCGETBAR IOCTLs.
177    */
178
179   conf.pat_buf_len = sizeof(struct pci_match_conf);
180   conf.num_patterns = 1;
181   conf.patterns = &pattern;
182   conf.match_buf_len = sizeof(struct pci_conf);
183   conf.num_matches = 1;
184   conf.matches = &match;
185   conf.offset = 0;
186   conf.generation = 0;
187   conf.status = 0;
188
189   pattern.pc_sel.pc_domain = d->domain;
190   pattern.pc_sel.pc_bus = d->bus;
191   pattern.pc_sel.pc_dev = d->dev;
192   pattern.pc_sel.pc_func = d->func;
193   pattern.flags = PCI_GETCONF_MATCH_DOMAIN | PCI_GETCONF_MATCH_BUS |
194           PCI_GETCONF_MATCH_DEV | PCI_GETCONF_MATCH_FUNC;
195
196   if (ioctl(d->access->fd, PCIOCGETCONF, &conf) < 0)
197     {
198       if (errno == ENODEV)
199         return 0;
200       d->access->error("fbsd_fill_info: ioctl(PCIOCGETCONF) failed: %s", strerror(errno));
201     }
202
203   if (want_fill(d, flags, PCI_FILL_IDENT))
204     {
205       d->vendor_id = match.pc_vendor;
206       d->device_id = match.pc_device;
207     }
208   if (want_fill(d, flags, PCI_FILL_CLASS))
209     d->device_class = (match.pc_class << 8) | match.pc_subclass;
210   if (want_fill(d, flags PCI_FILL_BASES | PCI_FILL_SIZES))
211     {
212       d->rom_base_addr = 0;
213       d->rom_size = 0;
214       for (i = 0; i < 6; i++)
215         {
216           bar.pbi_sel.pc_domain = d->domain;
217           bar.pbi_sel.pc_bus = d->bus;
218           bar.pbi_sel.pc_dev = d->dev;
219           bar.pbi_sel.pc_func = d->func;
220           bar.pbi_reg = 0x10 + 4*i;
221           bar.pbi_enabled = 0;
222           bar.pbi_base = 0;
223           bar.pbi_length = 0;
224           if (ioctl(d->access->fd, PCIOCGETBAR, &bar) < 0)
225             {
226               if (errno == ENODEV)
227                 return 0;
228               if (errno == EINVAL)
229                 {
230                   d->base_addr[i] = 0;
231                   d->size[i] = 0;
232                 }
233               else
234                 d->access->error("fbsd_fill_info: ioctl(PCIOCGETBAR) failed: %s", strerror(errno));
235             }
236           else
237             {
238               d->base_addr[i] = bar.pbi_base;
239               d->size[i] = bar.pbi_length;
240             }
241         }
242     }
243 }
244
245 static int
246 fbsd_read(struct pci_dev *d, int pos, byte *buf, int len)
247 {
248   struct pci_io pi;
249
250   if (d->access->fd_rw < 0)
251     {
252       d->access->warning("fbsd_read: missing permissions");
253       return 0;
254     }
255
256   if (!(len == 1 || len == 2 || len == 4))
257     return pci_generic_block_read(d, pos, buf, len);
258
259   if (pos >= 4096)
260     return 0;
261
262 #if __FreeBSD_version >= 700053 || defined(__DragonFly__)
263   pi.pi_sel.pc_domain = d->domain;
264 #else
265   if (d->domain)
266     return 0;
267 #endif
268   pi.pi_sel.pc_bus = d->bus;
269   pi.pi_sel.pc_dev = d->dev;
270   pi.pi_sel.pc_func = d->func;
271
272   pi.pi_reg = pos;
273   pi.pi_width = len;
274
275   if (ioctl(d->access->fd_rw, PCIOCREAD, &pi) < 0)
276     {
277       if (errno == ENODEV)
278         return 0;
279       d->access->error("fbsd_read: ioctl(PCIOCREAD) failed: %s", strerror(errno));
280     }
281
282   switch (len)
283     {
284     case 1:
285       buf[0] = (u8) pi.pi_data;
286       break;
287     case 2:
288       ((u16 *) buf)[0] = cpu_to_le16((u16) pi.pi_data);
289       break;
290     case 4:
291       ((u32 *) buf)[0] = cpu_to_le32((u32) pi.pi_data);
292       break;
293     }
294   return 1;
295 }
296
297 static int
298 fbsd_write(struct pci_dev *d, int pos, byte *buf, int len)
299 {
300   struct pci_io pi;
301
302   if (d->access->fd_rw < 0)
303     {
304       d->access->warning("fbsd_write: missing permissions");
305       return 0;
306     }
307
308   if (!(len == 1 || len == 2 || len == 4))
309     return pci_generic_block_write(d, pos, buf, len);
310
311   if (pos >= 4096)
312     return 0;
313
314 #if __FreeBSD_version >= 700053 || defined(__DragonFly__)
315   pi.pi_sel.pc_domain = d->domain;
316 #else
317   if (d->domain)
318     return 0;
319 #endif
320   pi.pi_sel.pc_bus = d->bus;
321   pi.pi_sel.pc_dev = d->dev;
322   pi.pi_sel.pc_func = d->func;
323
324   pi.pi_reg = pos;
325   pi.pi_width = len;
326
327   switch (len)
328     {
329     case 1:
330       pi.pi_data = buf[0];
331       break;
332     case 2:
333       pi.pi_data = le16_to_cpu(((u16 *) buf)[0]);
334       break;
335     case 4:
336       pi.pi_data = le32_to_cpu(((u32 *) buf)[0]);
337       break;
338     }
339
340   if (ioctl(d->access->fd_rw, PCIOCWRITE, &pi) < 0)
341     {
342       if (errno == ENODEV)
343         return 0;
344       d->access->error("fbsd_write: ioctl(PCIOCWRITE) failed: %s", strerror(errno));
345     }
346
347   return 1;
348 }
349
350 struct pci_methods pm_fbsd_device = {
351   "fbsd-device",
352   "FreeBSD /dev/pci device",
353   fbsd_config,
354   fbsd_detect,
355   fbsd_init,
356   fbsd_cleanup,
357   fbsd_scan,
358   fbsd_fill_info,
359   fbsd_read,
360   fbsd_write,
361   NULL,                                 /* read_vpd */
362   NULL,                                 /* dev_init */
363   NULL                                  /* dev_cleanup */
364 };