]> mj.ucw.cz Git - pciutils.git/blob - lib/fbsd-device.c
fbsd-device should compile again
[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 int
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 (flags & PCI_FILL_IDENT)
204     {
205       d->vendor_id = match.pc_vendor;
206       d->device_id = match.pc_device;
207     }
208   if (flags & PCI_FILL_CLASS)
209     {
210       d->device_class = match.pc_class | (match.pc_subclass << 8);
211     }
212   if (flags & (PCI_FILL_BASES | PCI_FILL_SIZES))
213     {
214       d->rom_base_addr = 0;
215       d->rom_size = 0;
216       for (i = 0; i < 6; i++)
217         {
218           bar.pbi_sel.pc_domain = d->domain;
219           bar.pbi_sel.pc_bus = d->bus;
220           bar.pbi_sel.pc_dev = d->dev;
221           bar.pbi_sel.pc_func = d->func;
222           bar.pbi_reg = 0x10 + 4*i;
223           bar.pbi_enabled = 0;
224           bar.pbi_base = 0;
225           bar.pbi_length = 0;
226           if (ioctl(d->access->fd, PCIOCGETBAR, &bar) < 0)
227             {
228               if (errno == ENODEV)
229                 return 0;
230               if (errno == EINVAL)
231                 {
232                   d->base_addr[i] = 0;
233                   d->size[i] = 0;
234                 }
235               else
236                 d->access->error("fbsd_fill_info: ioctl(PCIOCGETBAR) failed: %s", strerror(errno));
237             }
238           else
239             {
240               d->base_addr[i] = bar.pbi_base;
241               d->size[i] = bar.pbi_length;
242             }
243         }
244     }
245
246   return flags & (PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_BASES |
247                   PCI_FILL_SIZES);
248 }
249
250 static int
251 fbsd_read(struct pci_dev *d, int pos, byte *buf, int len)
252 {
253   struct pci_io pi;
254
255   if (d->access->fd_rw < 0)
256     {
257       d->access->warning("fbsd_read: missing permissions");
258       return 0;
259     }
260
261   if (!(len == 1 || len == 2 || len == 4))
262     return pci_generic_block_read(d, pos, buf, len);
263
264   if (pos >= 4096)
265     return 0;
266
267 #if __FreeBSD_version >= 700053 || defined(__DragonFly__)
268   pi.pi_sel.pc_domain = d->domain;
269 #endif
270   pi.pi_sel.pc_bus = d->bus;
271   pi.pi_sel.pc_dev = d->dev;
272   pi.pi_sel.pc_func = d->func;
273
274   pi.pi_reg = pos;
275   pi.pi_width = len;
276
277   if (ioctl(d->access->fd_rw, PCIOCREAD, &pi) < 0)
278     {
279       if (errno == ENODEV)
280         return 0;
281       d->access->error("fbsd_read: ioctl(PCIOCREAD) failed: %s", strerror(errno));
282     }
283
284   switch (len)
285     {
286     case 1:
287       buf[0] = (u8) pi.pi_data;
288       break;
289     case 2:
290       ((u16 *) buf)[0] = cpu_to_le16((u16) pi.pi_data);
291       break;
292     case 4:
293       ((u32 *) buf)[0] = cpu_to_le32((u32) pi.pi_data);
294       break;
295     }
296   return 1;
297 }
298
299 static int
300 fbsd_write(struct pci_dev *d, int pos, byte *buf, int len)
301 {
302   struct pci_io pi;
303
304   if (d->access->fd_rw < 0)
305     {
306       d->access->warning("fbsd_write: missing permissions");
307       return 0;
308     }
309
310   if (!(len == 1 || len == 2 || len == 4))
311     return pci_generic_block_write(d, pos, buf, len);
312
313   if (pos >= 4096)
314     return 0;
315
316 #if __FreeBSD_version >= 700053 || defined(__DragonFly__)
317   pi.pi_sel.pc_domain = d->domain;
318 #endif
319   pi.pi_sel.pc_bus = d->bus;
320   pi.pi_sel.pc_dev = d->dev;
321   pi.pi_sel.pc_func = d->func;
322
323   pi.pi_reg = pos;
324   pi.pi_width = len;
325
326   switch (len)
327     {
328     case 1:
329       pi.pi_data = buf[0];
330       break;
331     case 2:
332       pi.pi_data = le16_to_cpu(((u16 *) buf)[0]);
333       break;
334     case 4:
335       pi.pi_data = le32_to_cpu(((u32 *) buf)[0]);
336       break;
337     }
338
339   if (ioctl(d->access->fd_rw, PCIOCWRITE, &pi) < 0)
340     {
341       if (errno == ENODEV)
342         return 0;
343       d->access->error("fbsd_write: ioctl(PCIOCWRITE) failed: %s", strerror(errno));
344     }
345
346   return 1;
347 }
348
349 struct pci_methods pm_fbsd_device = {
350   "fbsd-device",
351   "FreeBSD /dev/pci device",
352   fbsd_config,
353   fbsd_detect,
354   fbsd_init,
355   fbsd_cleanup,
356   fbsd_scan,
357   fbsd_fill_info,
358   fbsd_read,
359   fbsd_write,
360   NULL,                                 /* read_vpd */
361   NULL,                                 /* dev_init */
362   NULL                                  /* dev_cleanup */
363 };