Commit 287d19ce authored by Stephen Hemminger's avatar Stephen Hemminger Committed by Jesse Barnes

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: default avatarStephen Hemminger <shemminger@vyatta.com>
Signed-off-by: default avatarJesse Barnes <jbarnes@virtuousgeek.org>
parent 1120f8b8
...@@ -66,6 +66,39 @@ EXPORT_SYMBOL(pci_bus_write_config_byte); ...@@ -66,6 +66,39 @@ EXPORT_SYMBOL(pci_bus_write_config_byte);
EXPORT_SYMBOL(pci_bus_write_config_word); EXPORT_SYMBOL(pci_bus_write_config_word);
EXPORT_SYMBOL(pci_bus_write_config_dword); EXPORT_SYMBOL(pci_bus_write_config_dword);
/**
* pci_read_vpd - Read one entry from Vital Product Data
* @dev: pci device struct
* @pos: offset in vpd space
* @count: number of bytes to read
* @buf: pointer to where to store result
*
*/
ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
{
if (!dev->vpd || !dev->vpd->ops)
return -ENODEV;
return dev->vpd->ops->read(dev, pos, count, buf);
}
EXPORT_SYMBOL(pci_read_vpd);
/**
* pci_write_vpd - Write entry to Vital Product Data
* @dev: pci device struct
* @pos: offset in vpd space
* @count: number of bytes to read
* @val: value to write
*
*/
ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
{
if (!dev->vpd || !dev->vpd->ops)
return -ENODEV;
return dev->vpd->ops->write(dev, pos, count, buf);
}
EXPORT_SYMBOL(pci_write_vpd);
/* /*
* The following routines are to prevent the user from accessing PCI config * The following routines are to prevent the user from accessing PCI config
* space when it's unsafe to do so. Some devices require this during BIST and * space when it's unsafe to do so. Some devices require this during BIST and
...@@ -176,19 +209,17 @@ static int pci_vpd_pci22_wait(struct pci_dev *dev) ...@@ -176,19 +209,17 @@ static int pci_vpd_pci22_wait(struct pci_dev *dev)
} }
} }
static int pci_vpd_pci22_read(struct pci_dev *dev, int pos, int size, static ssize_t pci_vpd_pci22_read(struct pci_dev *dev, loff_t pos, size_t count,
char *buf) void *arg)
{ {
struct pci_vpd_pci22 *vpd = struct pci_vpd_pci22 *vpd =
container_of(dev->vpd, struct pci_vpd_pci22, base); container_of(dev->vpd, struct pci_vpd_pci22, base);
u32 val; int ret;
int ret = 0; loff_t end = pos + count;
int begin, end, i; u8 *buf = arg;
if (pos < 0 || pos > vpd->base.len || size > vpd->base.len - pos) if (pos < 0 || pos > vpd->base.len || end > vpd->base.len)
return -EINVAL; return -EINVAL;
if (size == 0)
return 0;
if (mutex_lock_killable(&vpd->lock)) if (mutex_lock_killable(&vpd->lock))
return -EINTR; return -EINTR;
...@@ -196,73 +227,84 @@ static int pci_vpd_pci22_read(struct pci_dev *dev, int pos, int size, ...@@ -196,73 +227,84 @@ static int pci_vpd_pci22_read(struct pci_dev *dev, int pos, int size,
ret = pci_vpd_pci22_wait(dev); ret = pci_vpd_pci22_wait(dev);
if (ret < 0) if (ret < 0)
goto out; goto out;
ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
pos & ~3);
if (ret < 0)
goto out;
vpd->busy = true; while (pos < end) {
vpd->flag = PCI_VPD_ADDR_F; u32 val;
ret = pci_vpd_pci22_wait(dev); unsigned int i, skip;
if (ret < 0)
goto out; ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, pos & ~3);
&val); if (ret < 0)
break;
vpd->busy = true;
vpd->flag = PCI_VPD_ADDR_F;
ret = pci_vpd_pci22_wait(dev);
if (ret < 0)
break;
ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
if (ret < 0)
break;
skip = pos & 3;
for (i = 0; i < sizeof(u32); i++) {
if (i >= skip) {
*buf++ = val;
if (++pos == end)
break;
}
val >>= 8;
}
}
out: out:
mutex_unlock(&vpd->lock); mutex_unlock(&vpd->lock);
if (ret < 0) return ret ? ret : count;
return ret;
/* Convert to bytes */
begin = pos & 3;
end = min(4, begin + size);
for (i = 0; i < end; ++i) {
if (i >= begin)
*buf++ = val;
val >>= 8;
}
return end - begin;
} }
static int pci_vpd_pci22_write(struct pci_dev *dev, int pos, int size, static ssize_t pci_vpd_pci22_write(struct pci_dev *dev, loff_t pos, size_t count,
const char *buf) const void *arg)
{ {
struct pci_vpd_pci22 *vpd = struct pci_vpd_pci22 *vpd =
container_of(dev->vpd, struct pci_vpd_pci22, base); container_of(dev->vpd, struct pci_vpd_pci22, base);
u32 val; const u8 *buf = arg;
loff_t end = pos + count;
int ret = 0; int ret = 0;
if (pos < 0 || pos > vpd->base.len || pos & 3 || if (pos < 0 || (pos & 3) || (count & 3) || end > vpd->base.len)
size > vpd->base.len - pos || size < 4)
return -EINVAL; return -EINVAL;
val = (u8) *buf++;
val |= ((u8) *buf++) << 8;
val |= ((u8) *buf++) << 16;
val |= ((u32)(u8) *buf++) << 24;
if (mutex_lock_killable(&vpd->lock)) if (mutex_lock_killable(&vpd->lock))
return -EINTR; return -EINTR;
ret = pci_vpd_pci22_wait(dev); ret = pci_vpd_pci22_wait(dev);
if (ret < 0) if (ret < 0)
goto out; goto out;
ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA,
val); while (pos < end) {
if (ret < 0) u32 val;
goto out;
ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR, val = *buf++;
pos | PCI_VPD_ADDR_F); val |= *buf++ << 8;
if (ret < 0) val |= *buf++ << 16;
goto out; val |= *buf++ << 24;
vpd->busy = true;
vpd->flag = 0; ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
ret = pci_vpd_pci22_wait(dev); if (ret < 0)
break;
ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
pos | PCI_VPD_ADDR_F);
if (ret < 0)
break;
vpd->busy = true;
vpd->flag = 0;
ret = pci_vpd_pci22_wait(dev);
pos += sizeof(u32);
}
out: out:
mutex_unlock(&vpd->lock); mutex_unlock(&vpd->lock);
if (ret < 0) return ret ? ret : count;
return ret;
return 4;
} }
static void pci_vpd_pci22_release(struct pci_dev *dev) static void pci_vpd_pci22_release(struct pci_dev *dev)
...@@ -270,7 +312,7 @@ static void pci_vpd_pci22_release(struct pci_dev *dev) ...@@ -270,7 +312,7 @@ static void pci_vpd_pci22_release(struct pci_dev *dev)
kfree(container_of(dev->vpd, struct pci_vpd_pci22, base)); kfree(container_of(dev->vpd, struct pci_vpd_pci22, base));
} }
static struct pci_vpd_ops pci_vpd_pci22_ops = { static const struct pci_vpd_ops pci_vpd_pci22_ops = {
.read = pci_vpd_pci22_read, .read = pci_vpd_pci22_read,
.write = pci_vpd_pci22_write, .write = pci_vpd_pci22_write,
.release = pci_vpd_pci22_release, .release = pci_vpd_pci22_release,
......
...@@ -371,55 +371,33 @@ pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr, ...@@ -371,55 +371,33 @@ pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
} }
static ssize_t static ssize_t
pci_read_vpd(struct kobject *kobj, struct bin_attribute *bin_attr, read_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
char *buf, loff_t off, size_t count) char *buf, loff_t off, size_t count)
{ {
struct pci_dev *dev = struct pci_dev *dev =
to_pci_dev(container_of(kobj, struct device, kobj)); to_pci_dev(container_of(kobj, struct device, kobj));
int end;
int ret;
if (off > bin_attr->size) if (off > bin_attr->size)
count = 0; count = 0;
else if (count > bin_attr->size - off) else if (count > bin_attr->size - off)
count = bin_attr->size - off; count = bin_attr->size - off;
end = off + count;
while (off < end) {
ret = dev->vpd->ops->read(dev, off, end - off, buf);
if (ret < 0)
return ret;
buf += ret;
off += ret;
}
return count; return pci_read_vpd(dev, off, count, buf);
} }
static ssize_t static ssize_t
pci_write_vpd(struct kobject *kobj, struct bin_attribute *bin_attr, write_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
char *buf, loff_t off, size_t count) char *buf, loff_t off, size_t count)
{ {
struct pci_dev *dev = struct pci_dev *dev =
to_pci_dev(container_of(kobj, struct device, kobj)); to_pci_dev(container_of(kobj, struct device, kobj));
int end;
int ret;
if (off > bin_attr->size) if (off > bin_attr->size)
count = 0; count = 0;
else if (count > bin_attr->size - off) else if (count > bin_attr->size - off)
count = bin_attr->size - off; count = bin_attr->size - off;
end = off + count;
while (off < end) {
ret = dev->vpd->ops->write(dev, off, end - off, buf);
if (ret < 0)
return ret;
buf += ret;
off += ret;
}
return count; return pci_write_vpd(dev, off, count, buf);
} }
#ifdef HAVE_PCI_LEGACY #ifdef HAVE_PCI_LEGACY
...@@ -845,8 +823,8 @@ static int pci_create_capabilities_sysfs(struct pci_dev *dev) ...@@ -845,8 +823,8 @@ static int pci_create_capabilities_sysfs(struct pci_dev *dev)
attr->size = dev->vpd->len; attr->size = dev->vpd->len;
attr->attr.name = "vpd"; attr->attr.name = "vpd";
attr->attr.mode = S_IRUSR | S_IWUSR; attr->attr.mode = S_IRUSR | S_IWUSR;
attr->read = pci_read_vpd; attr->read = read_vpd_attr;
attr->write = pci_write_vpd; attr->write = write_vpd_attr;
retval = sysfs_create_bin_file(&dev->dev.kobj, attr); retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
if (retval) { if (retval) {
kfree(dev->vpd->attr); kfree(dev->vpd->attr);
......
...@@ -56,14 +56,14 @@ extern int pci_user_write_config_word(struct pci_dev *dev, int where, u16 val); ...@@ -56,14 +56,14 @@ extern int pci_user_write_config_word(struct pci_dev *dev, int where, u16 val);
extern int pci_user_write_config_dword(struct pci_dev *dev, int where, u32 val); extern int pci_user_write_config_dword(struct pci_dev *dev, int where, u32 val);
struct pci_vpd_ops { struct pci_vpd_ops {
int (*read)(struct pci_dev *dev, int pos, int size, char *buf); ssize_t (*read)(struct pci_dev *dev, loff_t pos, size_t count, void *buf);
int (*write)(struct pci_dev *dev, int pos, int size, const char *buf); ssize_t (*write)(struct pci_dev *dev, loff_t pos, size_t count, const void *buf);
void (*release)(struct pci_dev *dev); void (*release)(struct pci_dev *dev);
}; };
struct pci_vpd { struct pci_vpd {
unsigned int len; unsigned int len;
struct pci_vpd_ops *ops; const struct pci_vpd_ops *ops;
struct bin_attribute *attr; /* descriptor for sysfs VPD entry */ struct bin_attribute *attr; /* descriptor for sysfs VPD entry */
}; };
......
...@@ -687,6 +687,10 @@ int pci_back_from_sleep(struct pci_dev *dev); ...@@ -687,6 +687,10 @@ int pci_back_from_sleep(struct pci_dev *dev);
/* Functions for PCI Hotplug drivers to use */ /* Functions for PCI Hotplug drivers to use */
int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap); int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap);
/* Vital product data routines */
ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf);
ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf);
/* Helper functions for low-level code (drivers/pci/setup-[bus,res].c) */ /* Helper functions for low-level code (drivers/pci/setup-[bus,res].c) */
void pci_bus_assign_resources(struct pci_bus *bus); void pci_bus_assign_resources(struct pci_bus *bus);
void pci_bus_size_bridges(struct pci_bus *bus); void pci_bus_size_bridges(struct pci_bus *bus);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment