xref: /aosp_15_r20/external/pciutils/lib/fbsd-device.c (revision c2e0c6b56a71da9abe8df5c8348fb3eb5c2c9251)
1 /*
2  *	The PCI Library -- FreeBSD /dev/pci access
3  *
4  *	Copyright (c) 1999 Jari Kirma <[email protected]>
5  *	Updated in 2003 by Samy Al Bahra <[email protected]>
6  *	Updated in 2017 by Imre Vadász <[email protected]>
7  *
8  *	Can be freely distributed and used under the terms of the GNU GPL v2+.
9  *
10  *	SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <osreldate.h>
20 #include <stdint.h>
21 
22 #ifdef __FreeBSD_kernel_version
23 #  ifndef __FreeBSD_version
24 #    define __FreeBSD_version __FreeBSD_kernel_version
25 #  endif
26 #endif
27 
28 #if __FreeBSD_version < 430000 && !defined(__DragonFly__)
29 #  include <pci/pcivar.h>
30 #  include <pci/pci_ioctl.h>
31 #else
32 #  include <sys/pciio.h>
33 #endif
34 
35 #include "internal.h"
36 
37 static void
fbsd_config(struct pci_access * a)38 fbsd_config(struct pci_access *a)
39 {
40   pci_define_param(a, "fbsd.path", PCI_PATH_FBSD_DEVICE, "Path to the FreeBSD PCI device");
41 }
42 
43 static int
fbsd_detect(struct pci_access * a)44 fbsd_detect(struct pci_access *a)
45 {
46   char *name = pci_get_param(a, "fbsd.path");
47 
48   if (access(name, R_OK))
49     {
50       a->warning("Cannot open %s", name);
51       return 0;
52     }
53   a->debug("...using %s", name);
54   return 1;
55 }
56 
57 static void
fbsd_init(struct pci_access * a)58 fbsd_init(struct pci_access *a)
59 {
60   char *name = pci_get_param(a, "fbsd.path");
61   int fd;
62 
63   a->fd = -1;
64   a->fd_rw = -1;
65   /*
66    * When opening /dev/pci as read-write fails, retry with readonly which
67    * will still allow us to gain some information via the PCIOCGETCONF and
68    * PCIOCGETBAR IOCTLs, even without generic read access to the PCI config
69    * space.
70    */
71   fd = open(name, O_RDWR, 0);
72   if (fd < 0)
73     {
74       fd = open(name, O_RDONLY, 0);
75       if (fd < 0)
76 	a->error("fbsd_init: %s open failed", name);
77       else
78 	{
79 	  a->debug("fbsd_init: Fallback to read-only opened %s", name);
80 	  a->fd = fd;
81 	}
82     }
83   else
84     a->fd_rw = fd;
85 }
86 
87 static void
fbsd_cleanup(struct pci_access * a)88 fbsd_cleanup(struct pci_access *a)
89 {
90   if (a->fd >= 0)
91     {
92       close(a->fd);
93       a->fd = -1;
94     }
95   if (a->fd_rw >= 0)
96     {
97       close(a->fd_rw);
98       a->fd_rw = -1;
99     }
100 }
101 
102 static void
fbsd_scan(struct pci_access * a)103 fbsd_scan(struct pci_access *a)
104 {
105   struct pci_conf_io conf;
106   struct pci_conf *matches;
107   struct pci_dev *t;
108   uint32_t offset = 0;
109   unsigned int i;
110 
111   matches = calloc(32, sizeof(struct pci_conf));
112   if (matches == NULL)
113     {
114       a->error("calloc: %s", strerror(errno));
115       return;
116     }
117 
118   conf.generation = 0;
119   do
120     {
121       conf.pat_buf_len = 0;
122       conf.num_patterns = 0;
123       conf.patterns = NULL;
124       conf.match_buf_len = 32 * sizeof(struct pci_conf);
125       conf.num_matches = 32;
126       conf.matches = matches;
127       conf.offset = offset;
128       conf.status = 0;
129       if (ioctl(a->fd_rw >= 0 ? a->fd_rw : a->fd, PCIOCGETCONF, &conf) < 0)
130 	{
131 	  if (errno == ENODEV)
132 	    break;
133 	  a->error("fbsd_scan: ioctl(PCIOCGETCONF) failed: %s",
134 	      strerror(errno));
135 	}
136       /* PCI_GETCONF_LIST_CHANGED would require us to start over. */
137       if (conf.status == PCI_GETCONF_ERROR ||
138 	  conf.status == PCI_GETCONF_LIST_CHANGED)
139 	{
140 	  a->error("fbsd_scan: ioctl(PCIOCGETCONF) failed");
141 	  break;
142 	}
143       for (i = 0; i < conf.num_matches; i++)
144         {
145 	  t = pci_alloc_dev(a);
146 	  t->bus = matches[i].pc_sel.pc_bus;
147 	  t->dev = matches[i].pc_sel.pc_dev;
148 	  t->func = matches[i].pc_sel.pc_func;
149 	  t->domain = matches[i].pc_sel.pc_domain;
150 	  t->domain_16 = matches[i].pc_sel.pc_domain;
151 	  t->vendor_id = matches[i].pc_vendor;
152 	  t->device_id = matches[i].pc_device;
153 	  t->known_fields = PCI_FILL_IDENT;
154 	  t->hdrtype = matches[i].pc_hdr;
155 	  pci_link_dev(a, t);
156 	}
157       offset += conf.num_matches;
158     }
159   while (conf.status == PCI_GETCONF_MORE_DEVS);
160 
161   free(matches);
162 }
163 
164 static void
fbsd_fill_info(struct pci_dev * d,unsigned int flags)165 fbsd_fill_info(struct pci_dev *d, unsigned int flags)
166 {
167   struct pci_conf_io conf;
168   struct pci_bar_io bar;
169   struct pci_match_conf pattern;
170   struct pci_conf match;
171   int i;
172 
173   if (d->access->fd_rw >= 0)
174     return pci_generic_fill_info(d, flags);
175 
176   /*
177    * Can only handle PCI_FILL_IDENT, PCI_FILL_CLASS, PCI_FILL_BASES and
178    * PCI_FILL_SIZES requests with the PCIOCGETCONF and PCIOCGETBAR IOCTLs.
179    */
180 
181   conf.pat_buf_len = sizeof(struct pci_match_conf);
182   conf.num_patterns = 1;
183   conf.patterns = &pattern;
184   conf.match_buf_len = sizeof(struct pci_conf);
185   conf.num_matches = 1;
186   conf.matches = &match;
187   conf.offset = 0;
188   conf.generation = 0;
189   conf.status = 0;
190 
191   pattern.pc_sel.pc_domain = d->domain;
192   pattern.pc_sel.pc_bus = d->bus;
193   pattern.pc_sel.pc_dev = d->dev;
194   pattern.pc_sel.pc_func = d->func;
195   pattern.flags = PCI_GETCONF_MATCH_DOMAIN | PCI_GETCONF_MATCH_BUS |
196 	  PCI_GETCONF_MATCH_DEV | PCI_GETCONF_MATCH_FUNC;
197 
198   if (ioctl(d->access->fd, PCIOCGETCONF, &conf) < 0)
199     {
200       if (errno != ENODEV)
201 	d->access->error("fbsd_fill_info: ioctl(PCIOCGETCONF) failed: %s", strerror(errno));
202       return;
203     }
204 
205   if (want_fill(d, flags, PCI_FILL_IDENT))
206     {
207       d->vendor_id = match.pc_vendor;
208       d->device_id = match.pc_device;
209     }
210   if (want_fill(d, flags, PCI_FILL_CLASS))
211     d->device_class = (match.pc_class << 8) | match.pc_subclass;
212   if (want_fill(d, 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;
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 
247 static int
fbsd_read(struct pci_dev * d,int pos,byte * buf,int len)248 fbsd_read(struct pci_dev *d, int pos, byte *buf, int len)
249 {
250   struct pci_io pi;
251 
252   if (d->access->fd_rw < 0)
253     {
254       d->access->warning("fbsd_read: missing permissions");
255       return 0;
256     }
257 
258   if (!(len == 1 || len == 2 || len == 4))
259     return pci_generic_block_read(d, pos, buf, len);
260 
261   if (pos >= 4096)
262     return 0;
263 
264 #if __FreeBSD_version >= 700053 || defined(__DragonFly__)
265   pi.pi_sel.pc_domain = d->domain;
266 #else
267   if (d->domain)
268     return 0;
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
fbsd_write(struct pci_dev * d,int pos,byte * buf,int len)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 #else
319   if (d->domain)
320     return 0;
321 #endif
322   pi.pi_sel.pc_bus = d->bus;
323   pi.pi_sel.pc_dev = d->dev;
324   pi.pi_sel.pc_func = d->func;
325 
326   pi.pi_reg = pos;
327   pi.pi_width = len;
328 
329   switch (len)
330     {
331     case 1:
332       pi.pi_data = buf[0];
333       break;
334     case 2:
335       pi.pi_data = le16_to_cpu(((u16 *) buf)[0]);
336       break;
337     case 4:
338       pi.pi_data = le32_to_cpu(((u32 *) buf)[0]);
339       break;
340     }
341 
342   if (ioctl(d->access->fd_rw, PCIOCWRITE, &pi) < 0)
343     {
344       if (errno == ENODEV)
345 	return 0;
346       d->access->error("fbsd_write: ioctl(PCIOCWRITE) failed: %s", strerror(errno));
347     }
348 
349   return 1;
350 }
351 
352 struct pci_methods pm_fbsd_device = {
353   .name = "fbsd-device",
354   .help = "FreeBSD /dev/pci device",
355   .config = fbsd_config,
356   .detect = fbsd_detect,
357   .init = fbsd_init,
358   .cleanup = fbsd_cleanup,
359   .scan = fbsd_scan,
360   .fill_info = fbsd_fill_info,
361   .read = fbsd_read,
362   .write = fbsd_write,
363 };
364