Skip to content

Commit 287d19c

Browse files
Stephen Hemmingerjbarnes993
authored andcommitted
PCI: revise VPD access interface
Change PCI VPD API which was only used by sysfs to something usable in drivers. * move iteration over multiple words to the low level * use conventional types for arguments * add exportable wrapper Signed-off-by: Stephen Hemminger <shemminger@vyatta.com> Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
1 parent 1120f8b commit 287d19c

4 files changed

Lines changed: 114 additions & 90 deletions

File tree

‎drivers/pci/access.c‎

Lines changed: 99 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,39 @@ EXPORT_SYMBOL(pci_bus_write_config_byte);
6666
EXPORT_SYMBOL(pci_bus_write_config_word);
6767
EXPORT_SYMBOL(pci_bus_write_config_dword);
6868

69+
70+
/**
71+
* pci_read_vpd - Read one entry from Vital Product Data
72+
* @dev: pci device struct
73+
* @pos: offset in vpd space
74+
* @count: number of bytes to read
75+
* @buf: pointer to where to store result
76+
*
77+
*/
78+
ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
79+
{
80+
if (!dev->vpd || !dev->vpd->ops)
81+
return -ENODEV;
82+
return dev->vpd->ops->read(dev, pos, count, buf);
83+
}
84+
EXPORT_SYMBOL(pci_read_vpd);
85+
86+
/**
87+
* pci_write_vpd - Write entry to Vital Product Data
88+
* @dev: pci device struct
89+
* @pos: offset in vpd space
90+
* @count: number of bytes to read
91+
* @val: value to write
92+
*
93+
*/
94+
ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
95+
{
96+
if (!dev->vpd || !dev->vpd->ops)
97+
return -ENODEV;
98+
return dev->vpd->ops->write(dev, pos, count, buf);
99+
}
100+
EXPORT_SYMBOL(pci_write_vpd);
101+
69102
/*
70103
* The following routines are to prevent the user from accessing PCI config
71104
* space when it's unsafe to do so. Some devices require this during BIST and
@@ -176,101 +209,110 @@ static int pci_vpd_pci22_wait(struct pci_dev *dev)
176209
}
177210
}
178211

179-
static int pci_vpd_pci22_read(struct pci_dev *dev, int pos, int size,
180-
char *buf)
212+
static ssize_t pci_vpd_pci22_read(struct pci_dev *dev, loff_t pos, size_t count,
213+
void *arg)
181214
{
182215
struct pci_vpd_pci22 *vpd =
183216
container_of(dev->vpd, struct pci_vpd_pci22, base);
184-
u32 val;
185-
int ret = 0;
186-
int begin, end, i;
217+
int ret;
218+
loff_t end = pos + count;
219+
u8 *buf = arg;
187220

188-
if (pos < 0 || pos > vpd->base.len || size > vpd->base.len - pos)
221+
if (pos < 0 || pos > vpd->base.len || end > vpd->base.len)
189222
return -EINVAL;
190-
if (size == 0)
191-
return 0;
192223

193224
if (mutex_lock_killable(&vpd->lock))
194225
return -EINTR;
195226

196227
ret = pci_vpd_pci22_wait(dev);
197228
if (ret < 0)
198229
goto out;
199-
ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
200-
pos & ~3);
201-
if (ret < 0)
202-
goto out;
203230

204-
vpd->busy = true;
205-
vpd->flag = PCI_VPD_ADDR_F;
206-
ret = pci_vpd_pci22_wait(dev);
207-
if (ret < 0)
208-
goto out;
209-
ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA,
210-
&val);
231+
while (pos < end) {
232+
u32 val;
233+
unsigned int i, skip;
234+
235+
ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
236+
pos & ~3);
237+
if (ret < 0)
238+
break;
239+
vpd->busy = true;
240+
vpd->flag = PCI_VPD_ADDR_F;
241+
ret = pci_vpd_pci22_wait(dev);
242+
if (ret < 0)
243+
break;
244+
245+
ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
246+
if (ret < 0)
247+
break;
248+
249+
skip = pos & 3;
250+
for (i = 0; i < sizeof(u32); i++) {
251+
if (i >= skip) {
252+
*buf++ = val;
253+
if (++pos == end)
254+
break;
255+
}
256+
val >>= 8;
257+
}
258+
}
211259
out:
212260
mutex_unlock(&vpd->lock);
213-
if (ret < 0)
214-
return ret;
215-
216-
/* Convert to bytes */
217-
begin = pos & 3;
218-
end = min(4, begin + size);
219-
for (i = 0; i < end; ++i) {
220-
if (i >= begin)
221-
*buf++ = val;
222-
val >>= 8;
223-
}
224-
return end - begin;
261+
return ret ? ret : count;
225262
}
226263

227-
static int pci_vpd_pci22_write(struct pci_dev *dev, int pos, int size,
228-
const char *buf)
264+
static ssize_t pci_vpd_pci22_write(struct pci_dev *dev, loff_t pos, size_t count,
265+
const void *arg)
229266
{
230267
struct pci_vpd_pci22 *vpd =
231268
container_of(dev->vpd, struct pci_vpd_pci22, base);
232-
u32 val;
269+
const u8 *buf = arg;
270+
loff_t end = pos + count;
233271
int ret = 0;
234272

235-
if (pos < 0 || pos > vpd->base.len || pos & 3 ||
236-
size > vpd->base.len - pos || size < 4)
273+
if (pos < 0 || (pos & 3) || (count & 3) || end > vpd->base.len)
237274
return -EINVAL;
238275

239-
val = (u8) *buf++;
240-
val |= ((u8) *buf++) << 8;
241-
val |= ((u8) *buf++) << 16;
242-
val |= ((u32)(u8) *buf++) << 24;
243-
244276
if (mutex_lock_killable(&vpd->lock))
245277
return -EINTR;
278+
246279
ret = pci_vpd_pci22_wait(dev);
247280
if (ret < 0)
248281
goto out;
249-
ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA,
250-
val);
251-
if (ret < 0)
252-
goto out;
253-
ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
254-
pos | PCI_VPD_ADDR_F);
255-
if (ret < 0)
256-
goto out;
257-
vpd->busy = true;
258-
vpd->flag = 0;
259-
ret = pci_vpd_pci22_wait(dev);
282+
283+
while (pos < end) {
284+
u32 val;
285+
286+
val = *buf++;
287+
val |= *buf++ << 8;
288+
val |= *buf++ << 16;
289+
val |= *buf++ << 24;
290+
291+
ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
292+
if (ret < 0)
293+
break;
294+
ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
295+
pos | PCI_VPD_ADDR_F);
296+
if (ret < 0)
297+
break;
298+
299+
vpd->busy = true;
300+
vpd->flag = 0;
301+
ret = pci_vpd_pci22_wait(dev);
302+
303+
pos += sizeof(u32);
304+
}
260305
out:
261306
mutex_unlock(&vpd->lock);
262-
if (ret < 0)
263-
return ret;
264-
265-
return 4;
307+
return ret ? ret : count;
266308
}
267309

268310
static void pci_vpd_pci22_release(struct pci_dev *dev)
269311
{
270312
kfree(container_of(dev->vpd, struct pci_vpd_pci22, base));
271313
}
272314

273-
static struct pci_vpd_ops pci_vpd_pci22_ops = {
315+
static const struct pci_vpd_ops pci_vpd_pci22_ops = {
274316
.read = pci_vpd_pci22_read,
275317
.write = pci_vpd_pci22_write,
276318
.release = pci_vpd_pci22_release,

‎drivers/pci/pci-sysfs.c‎

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -371,55 +371,33 @@ pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
371371
}
372372

373373
static ssize_t
374-
pci_read_vpd(struct kobject *kobj, struct bin_attribute *bin_attr,
375-
char *buf, loff_t off, size_t count)
374+
read_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
375+
char *buf, loff_t off, size_t count)
376376
{
377377
struct pci_dev *dev =
378378
to_pci_dev(container_of(kobj, struct device, kobj));
379-
int end;
380-
int ret;
381379

382380
if (off > bin_attr->size)
383381
count = 0;
384382
else if (count > bin_attr->size - off)
385383
count = bin_attr->size - off;
386-
end = off + count;
387-
388-
while (off < end) {
389-
ret = dev->vpd->ops->read(dev, off, end - off, buf);
390-
if (ret < 0)
391-
return ret;
392-
buf += ret;
393-
off += ret;
394-
}
395384

396-
return count;
385+
return pci_read_vpd(dev, off, count, buf);
397386
}
398387

399388
static ssize_t
400-
pci_write_vpd(struct kobject *kobj, struct bin_attribute *bin_attr,
401-
char *buf, loff_t off, size_t count)
389+
write_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
390+
char *buf, loff_t off, size_t count)
402391
{
403392
struct pci_dev *dev =
404393
to_pci_dev(container_of(kobj, struct device, kobj));
405-
int end;
406-
int ret;
407394

408395
if (off > bin_attr->size)
409396
count = 0;
410397
else if (count > bin_attr->size - off)
411398
count = bin_attr->size - off;
412-
end = off + count;
413-
414-
while (off < end) {
415-
ret = dev->vpd->ops->write(dev, off, end - off, buf);
416-
if (ret < 0)
417-
return ret;
418-
buf += ret;
419-
off += ret;
420-
}
421399

422-
return count;
400+
return pci_write_vpd(dev, off, count, buf);
423401
}
424402

425403
#ifdef HAVE_PCI_LEGACY
@@ -845,8 +823,8 @@ static int pci_create_capabilities_sysfs(struct pci_dev *dev)
845823
attr->size = dev->vpd->len;
846824
attr->attr.name = "vpd";
847825
attr->attr.mode = S_IRUSR | S_IWUSR;
848-
attr->read = pci_read_vpd;
849-
attr->write = pci_write_vpd;
826+
attr->read = read_vpd_attr;
827+
attr->write = write_vpd_attr;
850828
retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
851829
if (retval) {
852830
kfree(dev->vpd->attr);

‎drivers/pci/pci.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ extern int pci_user_write_config_word(struct pci_dev *dev, int where, u16 val);
5656
extern int pci_user_write_config_dword(struct pci_dev *dev, int where, u32 val);
5757

5858
struct pci_vpd_ops {
59-
int (*read)(struct pci_dev *dev, int pos, int size, char *buf);
60-
int (*write)(struct pci_dev *dev, int pos, int size, const char *buf);
59+
ssize_t (*read)(struct pci_dev *dev, loff_t pos, size_t count, void *buf);
60+
ssize_t (*write)(struct pci_dev *dev, loff_t pos, size_t count, const void *buf);
6161
void (*release)(struct pci_dev *dev);
6262
};
6363

6464
struct pci_vpd {
6565
unsigned int len;
66-
struct pci_vpd_ops *ops;
66+
const struct pci_vpd_ops *ops;
6767
struct bin_attribute *attr; /* descriptor for sysfs VPD entry */
6868
};
6969

‎include/linux/pci.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,10 @@ int pci_back_from_sleep(struct pci_dev *dev);
687687
/* Functions for PCI Hotplug drivers to use */
688688
int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap);
689689

690+
/* Vital product data routines */
691+
ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf);
692+
ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf);
693+
690694
/* Helper functions for low-level code (drivers/pci/setup-[bus,res].c) */
691695
void pci_bus_assign_resources(struct pci_bus *bus);
692696
void pci_bus_size_bridges(struct pci_bus *bus);

0 commit comments

Comments
 (0)