1 /*
2 * The PCI Library -- User Access
3 *
4 * Copyright (c) 1997--2022 Martin Mares <[email protected]>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdarg.h>
14 #include <string.h>
15
16 #include "internal.h"
17
18 void
pci_scan_bus(struct pci_access * a)19 pci_scan_bus(struct pci_access *a)
20 {
21 a->methods->scan(a);
22 }
23
24 struct pci_dev *
pci_alloc_dev(struct pci_access * a)25 pci_alloc_dev(struct pci_access *a)
26 {
27 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
28
29 memset(d, 0, sizeof(*d));
30 d->access = a;
31 d->methods = a->methods;
32 d->hdrtype = -1;
33 d->numa_node = -1;
34 if (d->methods->init_dev)
35 d->methods->init_dev(d);
36 return d;
37 }
38
39 int
pci_link_dev(struct pci_access * a,struct pci_dev * d)40 pci_link_dev(struct pci_access *a, struct pci_dev *d)
41 {
42 d->next = a->devices;
43 a->devices = d;
44
45 /*
46 * Applications compiled with older versions of libpci do not expect
47 * 32-bit domain numbers. To keep them working, we keep a 16-bit
48 * version of the domain number at the previous location in struct
49 * pci_dev. This will keep backward compatibility on systems which
50 * don't require large domain numbers.
51 */
52 if (d->domain > 0xffff)
53 d->domain_16 = 0xffff;
54 else
55 d->domain_16 = d->domain;
56
57 return 1;
58 }
59
60 struct pci_dev *
pci_get_dev(struct pci_access * a,int domain,int bus,int dev,int func)61 pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func)
62 {
63 struct pci_dev *d = pci_alloc_dev(a);
64
65 d->domain = domain;
66 d->bus = bus;
67 d->dev = dev;
68 d->func = func;
69 return d;
70 }
71
72 static void
pci_free_properties(struct pci_dev * d)73 pci_free_properties(struct pci_dev *d)
74 {
75 struct pci_property *p;
76
77 while (p = d->properties)
78 {
79 d->properties = p->next;
80 pci_mfree(p);
81 }
82 }
83
pci_free_dev(struct pci_dev * d)84 void pci_free_dev(struct pci_dev *d)
85 {
86 if (d->methods->cleanup_dev)
87 d->methods->cleanup_dev(d);
88
89 pci_free_caps(d);
90 pci_free_properties(d);
91 pci_mfree(d);
92 }
93
94 static inline void
pci_read_data(struct pci_dev * d,void * buf,int pos,int len)95 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
96 {
97 if (pos & (len-1))
98 d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
99 if (pos + len <= d->cache_len)
100 memcpy(buf, d->cache + pos, len);
101 else if (!d->methods->read(d, pos, buf, len))
102 memset(buf, 0xff, len);
103 }
104
105 byte
pci_read_byte(struct pci_dev * d,int pos)106 pci_read_byte(struct pci_dev *d, int pos)
107 {
108 byte buf;
109 pci_read_data(d, &buf, pos, 1);
110 return buf;
111 }
112
113 word
pci_read_word(struct pci_dev * d,int pos)114 pci_read_word(struct pci_dev *d, int pos)
115 {
116 word buf;
117 pci_read_data(d, &buf, pos, 2);
118 return le16_to_cpu(buf);
119 }
120
121 u32
pci_read_long(struct pci_dev * d,int pos)122 pci_read_long(struct pci_dev *d, int pos)
123 {
124 u32 buf;
125 pci_read_data(d, &buf, pos, 4);
126 return le32_to_cpu(buf);
127 }
128
129 int
pci_read_block(struct pci_dev * d,int pos,byte * buf,int len)130 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
131 {
132 return d->methods->read(d, pos, buf, len);
133 }
134
135 int
pci_read_vpd(struct pci_dev * d,int pos,byte * buf,int len)136 pci_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
137 {
138 return d->methods->read_vpd ? d->methods->read_vpd(d, pos, buf, len) : 0;
139 }
140
141 static inline int
pci_write_data(struct pci_dev * d,void * buf,int pos,int len)142 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
143 {
144 if (pos & (len-1))
145 d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
146 if (pos + len <= d->cache_len)
147 memcpy(d->cache + pos, buf, len);
148 return d->methods->write(d, pos, buf, len);
149 }
150
151 int
pci_write_byte(struct pci_dev * d,int pos,byte data)152 pci_write_byte(struct pci_dev *d, int pos, byte data)
153 {
154 return pci_write_data(d, &data, pos, 1);
155 }
156
157 int
pci_write_word(struct pci_dev * d,int pos,word data)158 pci_write_word(struct pci_dev *d, int pos, word data)
159 {
160 word buf = cpu_to_le16(data);
161 return pci_write_data(d, &buf, pos, 2);
162 }
163
164 int
pci_write_long(struct pci_dev * d,int pos,u32 data)165 pci_write_long(struct pci_dev *d, int pos, u32 data)
166 {
167 u32 buf = cpu_to_le32(data);
168 return pci_write_data(d, &buf, pos, 4);
169 }
170
171 int
pci_write_block(struct pci_dev * d,int pos,byte * buf,int len)172 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
173 {
174 if (pos < d->cache_len)
175 {
176 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
177 memcpy(d->cache + pos, buf, l);
178 }
179 return d->methods->write(d, pos, buf, len);
180 }
181
182 static void
pci_reset_properties(struct pci_dev * d)183 pci_reset_properties(struct pci_dev *d)
184 {
185 d->known_fields = 0;
186 d->phy_slot = NULL;
187 d->module_alias = NULL;
188 d->label = NULL;
189 pci_free_caps(d);
190 pci_free_properties(d);
191 }
192
193 int
pci_fill_info_v313(struct pci_dev * d,int flags)194 pci_fill_info_v313(struct pci_dev *d, int flags)
195 {
196 unsigned int uflags = flags;
197 if (uflags & PCI_FILL_RESCAN)
198 {
199 uflags &= ~PCI_FILL_RESCAN;
200 pci_reset_properties(d);
201 }
202 if (uflags & ~d->known_fields)
203 d->methods->fill_info(d, uflags);
204 return d->known_fields;
205 }
206
207 /* In version 3.1, pci_fill_info got new flags => versioned alias */
208 /* In versions 3.2, 3.3, 3.4, 3.5, 3.8 and 3.12, the same has happened */
209 STATIC_ALIAS(int pci_fill_info(struct pci_dev *d, int flags), pci_fill_info_v313(d, flags));
210 DEFINE_ALIAS(int pci_fill_info_v30(struct pci_dev *d, int flags), pci_fill_info_v313);
211 DEFINE_ALIAS(int pci_fill_info_v31(struct pci_dev *d, int flags), pci_fill_info_v313);
212 DEFINE_ALIAS(int pci_fill_info_v32(struct pci_dev *d, int flags), pci_fill_info_v313);
213 DEFINE_ALIAS(int pci_fill_info_v33(struct pci_dev *d, int flags), pci_fill_info_v313);
214 DEFINE_ALIAS(int pci_fill_info_v34(struct pci_dev *d, int flags), pci_fill_info_v313);
215 DEFINE_ALIAS(int pci_fill_info_v35(struct pci_dev *d, int flags), pci_fill_info_v313);
216 DEFINE_ALIAS(int pci_fill_info_v38(struct pci_dev *d, int flags), pci_fill_info_v313);
217 SYMBOL_VERSION(pci_fill_info_v30, pci_fill_info@LIBPCI_3.0);
218 SYMBOL_VERSION(pci_fill_info_v31, pci_fill_info@LIBPCI_3.1);
219 SYMBOL_VERSION(pci_fill_info_v32, pci_fill_info@LIBPCI_3.2);
220 SYMBOL_VERSION(pci_fill_info_v33, pci_fill_info@LIBPCI_3.3);
221 SYMBOL_VERSION(pci_fill_info_v34, pci_fill_info@LIBPCI_3.4);
222 SYMBOL_VERSION(pci_fill_info_v35, pci_fill_info@LIBPCI_3.5);
223 SYMBOL_VERSION(pci_fill_info_v38, pci_fill_info@LIBPCI_3.8);
224 SYMBOL_VERSION(pci_fill_info_v313, pci_fill_info@@LIBPCI_3.13);
225
226 void
pci_setup_cache(struct pci_dev * d,byte * cache,int len)227 pci_setup_cache(struct pci_dev *d, byte *cache, int len)
228 {
229 d->cache = cache;
230 d->cache_len = len;
231 }
232
233 char *
pci_set_property(struct pci_dev * d,u32 key,char * value)234 pci_set_property(struct pci_dev *d, u32 key, char *value)
235 {
236 struct pci_property *p;
237 struct pci_property **pp = &d->properties;
238
239 while (p = *pp)
240 {
241 if (p->key == key)
242 {
243 *pp = p->next;
244 pci_mfree(p);
245 }
246 else
247 pp = &p->next;
248 }
249
250 if (!value)
251 return NULL;
252
253 p = pci_malloc(d->access, sizeof(*p) + strlen(value));
254 *pp = p;
255 p->next = NULL;
256 p->key = key;
257 strcpy(p->value, value);
258
259 return p->value;
260 }
261
262 char *
pci_get_string_property(struct pci_dev * d,u32 prop)263 pci_get_string_property(struct pci_dev *d, u32 prop)
264 {
265 struct pci_property *p;
266
267 for (p = d->properties; p; p = p->next)
268 if (p->key == prop)
269 return p->value;
270
271 return NULL;
272 }
273