Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[linux-2.6.git] / drivers / infiniband / core / sysfs.c
index b812065..4d10421 100644 (file)
  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
- *
- * $Id: sysfs.c 1349 2004-12-16 21:09:43Z roland $
  */
 
 #include "core_priv.h"
 
+#include <linux/slab.h>
+#include <linux/string.h>
+
 #include <rdma/ib_mad.h>
 
 struct ib_port {
@@ -65,7 +66,7 @@ struct port_table_attribute {
        int                     index;
 };
 
-static inline int ibdev_is_alive(const struct ib_device *dev) 
+static inline int ibdev_is_alive(const struct ib_device *dev)
 {
        return dev->reg_state == IB_DEV_REGISTERED;
 }
@@ -109,7 +110,7 @@ static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
                return ret;
 
        return sprintf(buf, "%d: %s\n", attr.state,
-                      attr.state >= 0 && attr.state <= ARRAY_SIZE(state_name) ?
+                      attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
                       state_name[attr.state] : "UNKNOWN");
 }
 
@@ -308,7 +309,7 @@ static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
                return sprintf(buf, "N/A (no PMA)\n");
 
        in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
-       out_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
+       out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
        if (!in_mad || !out_mad) {
                ret = -ENOMEM;
                goto out;
@@ -333,7 +334,7 @@ static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
        switch (width) {
        case 4:
                ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >>
-                                           (offset % 4)) & 0xf);
+                                           (4 - (offset % 8))) & 0xf);
                break;
        case 8:
                ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]);
@@ -424,34 +425,25 @@ static struct kobj_type port_type = {
        .default_attrs = port_default_attrs
 };
 
-static void ib_device_release(struct class_device *cdev)
+static void ib_device_release(struct device *device)
 {
-       struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
+       struct ib_device *dev = container_of(device, struct ib_device, dev);
 
        kfree(dev);
 }
 
-static int ib_device_hotplug(struct class_device *cdev, char **envp,
-                            int num_envp, char *buf, int size)
+static int ib_device_uevent(struct device *device,
+                           struct kobj_uevent_env *env)
 {
-       struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
-       int i = 0, len = 0;
+       struct ib_device *dev = container_of(device, struct ib_device, dev);
 
-       if (add_hotplug_env_var(envp, num_envp, &i, buf, size, &len,
-                               "NAME=%s", dev->name))
+       if (add_uevent_var(env, "NAME=%s", dev->name))
                return -ENOMEM;
 
        /*
-        * It might be nice to pass the node GUID to hotplug, but
-        * right now the only way to get it is to query the device
-        * provider, and this can crash during device removal because
-        * we are will be running after driver removal has started.
-        * We could add a node_guid field to struct ib_device, or we
-        * could just let the hotplug script read the node GUID from
-        * sysfs when devices are added.
+        * It would be nice to pass the node GUID with the event...
         */
 
-       envp[i] = NULL;
        return 0;
 }
 
@@ -475,12 +467,13 @@ alloc_group_attrs(ssize_t (*show)(struct ib_port *,
                        goto err;
 
                if (snprintf(element->name, sizeof(element->name),
-                            "%d", i) >= sizeof(element->name))
+                            "%d", i) >= sizeof(element->name)) {
+                       kfree(element);
                        goto err;
+               }
 
                element->attr.attr.name  = element->name;
                element->attr.attr.mode  = S_IRUGO;
-               element->attr.attr.owner = THIS_MODULE;
                element->attr.show       = show;
                element->index           = i;
 
@@ -513,19 +506,10 @@ static int add_port(struct ib_device *device, int port_num)
 
        p->ibdev      = device;
        p->port_num   = port_num;
-       p->kobj.ktype = &port_type;
 
-       p->kobj.parent = kobject_get(&device->ports_parent);
-       if (!p->kobj.parent) {
-               ret = -EBUSY;
-               goto err;
-       }
-
-       ret = kobject_set_name(&p->kobj, "%d", port_num);
-       if (ret)
-               goto err_put;
-
-       ret = kobject_register(&p->kobj);
+       ret = kobject_init_and_add(&p->kobj, &port_type,
+                                  kobject_get(device->ports_parent),
+                                  "%d", port_num);
        if (ret)
                goto err_put;
 
@@ -554,6 +538,7 @@ static int add_port(struct ib_device *device, int port_num)
 
        list_add_tail(&p->kobj.entry, &device->port_list);
 
+       kobject_uevent(&p->kobj, KOBJ_ADD);
        return 0;
 
 err_free_pkey:
@@ -575,31 +560,32 @@ err_remove_pma:
        sysfs_remove_group(&p->kobj, &pma_group);
 
 err_put:
-       kobject_put(&device->ports_parent);
-
-err:
+       kobject_put(device->ports_parent);
        kfree(p);
        return ret;
 }
 
-static ssize_t show_node_type(struct class_device *cdev, char *buf)
+static ssize_t show_node_type(struct device *device,
+                             struct device_attribute *attr, char *buf)
 {
-       struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
+       struct ib_device *dev = container_of(device, struct ib_device, dev);
 
        if (!ibdev_is_alive(dev))
                return -ENODEV;
 
        switch (dev->node_type) {
-       case IB_NODE_CA:     return sprintf(buf, "%d: CA\n", dev->node_type);
-       case IB_NODE_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
-       case IB_NODE_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
-       default:             return sprintf(buf, "%d: <unknown>\n", dev->node_type);
+       case RDMA_NODE_IB_CA:     return sprintf(buf, "%d: CA\n", dev->node_type);
+       case RDMA_NODE_RNIC:      return sprintf(buf, "%d: RNIC\n", dev->node_type);
+       case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
+       case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
+       default:                  return sprintf(buf, "%d: <unknown>\n", dev->node_type);
        }
 }
 
-static ssize_t show_sys_image_guid(struct class_device *cdev, char *buf)
+static ssize_t show_sys_image_guid(struct device *device,
+                                  struct device_attribute *dev_attr, char *buf)
 {
-       struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
+       struct ib_device *dev = container_of(device, struct ib_device, dev);
        struct ib_device_attr attr;
        ssize_t ret;
 
@@ -617,83 +603,215 @@ static ssize_t show_sys_image_guid(struct class_device *cdev, char *buf)
                       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[3]));
 }
 
-static ssize_t show_node_guid(struct class_device *cdev, char *buf)
+static ssize_t show_node_guid(struct device *device,
+                             struct device_attribute *attr, char *buf)
 {
-       struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
-       struct ib_device_attr attr;
-       ssize_t ret;
+       struct ib_device *dev = container_of(device, struct ib_device, dev);
 
        if (!ibdev_is_alive(dev))
                return -ENODEV;
 
-       ret = ib_query_device(dev, &attr);
+       return sprintf(buf, "%04x:%04x:%04x:%04x\n",
+                      be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
+                      be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
+                      be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
+                      be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
+}
+
+static ssize_t show_node_desc(struct device *device,
+                             struct device_attribute *attr, char *buf)
+{
+       struct ib_device *dev = container_of(device, struct ib_device, dev);
+
+       return sprintf(buf, "%.64s\n", dev->node_desc);
+}
+
+static ssize_t set_node_desc(struct device *device,
+                            struct device_attribute *attr,
+                            const char *buf, size_t count)
+{
+       struct ib_device *dev = container_of(device, struct ib_device, dev);
+       struct ib_device_modify desc = {};
+       int ret;
+
+       if (!dev->modify_device)
+               return -EIO;
+
+       memcpy(desc.node_desc, buf, min_t(int, count, 64));
+       ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
        if (ret)
                return ret;
 
-       return sprintf(buf, "%04x:%04x:%04x:%04x\n",
-                      be16_to_cpu(((__be16 *) &attr.node_guid)[0]),
-                      be16_to_cpu(((__be16 *) &attr.node_guid)[1]),
-                      be16_to_cpu(((__be16 *) &attr.node_guid)[2]),
-                      be16_to_cpu(((__be16 *) &attr.node_guid)[3]));
+       return count;
 }
 
-static CLASS_DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
-static CLASS_DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
-static CLASS_DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
+static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
+static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
+static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
+static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
 
-static struct class_device_attribute *ib_class_attributes[] = {
-       &class_device_attr_node_type,
-       &class_device_attr_sys_image_guid,
-       &class_device_attr_node_guid
+static struct device_attribute *ib_class_attributes[] = {
+       &dev_attr_node_type,
+       &dev_attr_sys_image_guid,
+       &dev_attr_node_guid,
+       &dev_attr_node_desc
 };
 
 static struct class ib_class = {
        .name    = "infiniband",
-       .release = ib_device_release,
-       .hotplug = ib_device_hotplug,
+       .dev_release = ib_device_release,
+       .dev_uevent = ib_device_uevent,
+};
+
+/* Show a given an attribute in the statistics group */
+static ssize_t show_protocol_stat(const struct device *device,
+                           struct device_attribute *attr, char *buf,
+                           unsigned offset)
+{
+       struct ib_device *dev = container_of(device, struct ib_device, dev);
+       union rdma_protocol_stats stats;
+       ssize_t ret;
+
+       ret = dev->get_protocol_stats(dev, &stats);
+       if (ret)
+               return ret;
+
+       return sprintf(buf, "%llu\n",
+                      (unsigned long long) ((u64 *) &stats)[offset]);
+}
+
+/* generate a read-only iwarp statistics attribute */
+#define IW_STATS_ENTRY(name)                                           \
+static ssize_t show_##name(struct device *device,                      \
+                          struct device_attribute *attr, char *buf)    \
+{                                                                      \
+       return show_protocol_stat(device, attr, buf,                    \
+                                 offsetof(struct iw_protocol_stats, name) / \
+                                 sizeof (u64));                        \
+}                                                                      \
+static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
+
+IW_STATS_ENTRY(ipInReceives);
+IW_STATS_ENTRY(ipInHdrErrors);
+IW_STATS_ENTRY(ipInTooBigErrors);
+IW_STATS_ENTRY(ipInNoRoutes);
+IW_STATS_ENTRY(ipInAddrErrors);
+IW_STATS_ENTRY(ipInUnknownProtos);
+IW_STATS_ENTRY(ipInTruncatedPkts);
+IW_STATS_ENTRY(ipInDiscards);
+IW_STATS_ENTRY(ipInDelivers);
+IW_STATS_ENTRY(ipOutForwDatagrams);
+IW_STATS_ENTRY(ipOutRequests);
+IW_STATS_ENTRY(ipOutDiscards);
+IW_STATS_ENTRY(ipOutNoRoutes);
+IW_STATS_ENTRY(ipReasmTimeout);
+IW_STATS_ENTRY(ipReasmReqds);
+IW_STATS_ENTRY(ipReasmOKs);
+IW_STATS_ENTRY(ipReasmFails);
+IW_STATS_ENTRY(ipFragOKs);
+IW_STATS_ENTRY(ipFragFails);
+IW_STATS_ENTRY(ipFragCreates);
+IW_STATS_ENTRY(ipInMcastPkts);
+IW_STATS_ENTRY(ipOutMcastPkts);
+IW_STATS_ENTRY(ipInBcastPkts);
+IW_STATS_ENTRY(ipOutBcastPkts);
+IW_STATS_ENTRY(tcpRtoAlgorithm);
+IW_STATS_ENTRY(tcpRtoMin);
+IW_STATS_ENTRY(tcpRtoMax);
+IW_STATS_ENTRY(tcpMaxConn);
+IW_STATS_ENTRY(tcpActiveOpens);
+IW_STATS_ENTRY(tcpPassiveOpens);
+IW_STATS_ENTRY(tcpAttemptFails);
+IW_STATS_ENTRY(tcpEstabResets);
+IW_STATS_ENTRY(tcpCurrEstab);
+IW_STATS_ENTRY(tcpInSegs);
+IW_STATS_ENTRY(tcpOutSegs);
+IW_STATS_ENTRY(tcpRetransSegs);
+IW_STATS_ENTRY(tcpInErrs);
+IW_STATS_ENTRY(tcpOutRsts);
+
+static struct attribute *iw_proto_stats_attrs[] = {
+       &dev_attr_ipInReceives.attr,
+       &dev_attr_ipInHdrErrors.attr,
+       &dev_attr_ipInTooBigErrors.attr,
+       &dev_attr_ipInNoRoutes.attr,
+       &dev_attr_ipInAddrErrors.attr,
+       &dev_attr_ipInUnknownProtos.attr,
+       &dev_attr_ipInTruncatedPkts.attr,
+       &dev_attr_ipInDiscards.attr,
+       &dev_attr_ipInDelivers.attr,
+       &dev_attr_ipOutForwDatagrams.attr,
+       &dev_attr_ipOutRequests.attr,
+       &dev_attr_ipOutDiscards.attr,
+       &dev_attr_ipOutNoRoutes.attr,
+       &dev_attr_ipReasmTimeout.attr,
+       &dev_attr_ipReasmReqds.attr,
+       &dev_attr_ipReasmOKs.attr,
+       &dev_attr_ipReasmFails.attr,
+       &dev_attr_ipFragOKs.attr,
+       &dev_attr_ipFragFails.attr,
+       &dev_attr_ipFragCreates.attr,
+       &dev_attr_ipInMcastPkts.attr,
+       &dev_attr_ipOutMcastPkts.attr,
+       &dev_attr_ipInBcastPkts.attr,
+       &dev_attr_ipOutBcastPkts.attr,
+       &dev_attr_tcpRtoAlgorithm.attr,
+       &dev_attr_tcpRtoMin.attr,
+       &dev_attr_tcpRtoMax.attr,
+       &dev_attr_tcpMaxConn.attr,
+       &dev_attr_tcpActiveOpens.attr,
+       &dev_attr_tcpPassiveOpens.attr,
+       &dev_attr_tcpAttemptFails.attr,
+       &dev_attr_tcpEstabResets.attr,
+       &dev_attr_tcpCurrEstab.attr,
+       &dev_attr_tcpInSegs.attr,
+       &dev_attr_tcpOutSegs.attr,
+       &dev_attr_tcpRetransSegs.attr,
+       &dev_attr_tcpInErrs.attr,
+       &dev_attr_tcpOutRsts.attr,
+       NULL
+};
+
+static struct attribute_group iw_stats_group = {
+       .name   = "proto_stats",
+       .attrs  = iw_proto_stats_attrs,
 };
 
 int ib_device_register_sysfs(struct ib_device *device)
 {
-       struct class_device *class_dev = &device->class_dev;
+       struct device *class_dev = &device->dev;
        int ret;
        int i;
 
        class_dev->class      = &ib_class;
-       class_dev->class_data = device;
-       strlcpy(class_dev->class_id, device->name, BUS_ID_SIZE);
+       class_dev->driver_data = device;
+       class_dev->parent     = device->dma_device;
+       strlcpy(class_dev->bus_id, device->name, BUS_ID_SIZE);
 
        INIT_LIST_HEAD(&device->port_list);
 
-       ret = class_device_register(class_dev);
+       ret = device_register(class_dev);
        if (ret)
                goto err;
 
        for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
-               ret = class_device_create_file(class_dev, ib_class_attributes[i]);
+               ret = device_create_file(class_dev, ib_class_attributes[i]);
                if (ret)
                        goto err_unregister;
        }
 
-       device->ports_parent.parent = kobject_get(&class_dev->kobj);
-       if (!device->ports_parent.parent) {
-               ret = -EBUSY;
-               goto err_unregister;
-       }
-       ret = kobject_set_name(&device->ports_parent, "ports");
-       if (ret)
-               goto err_put;
-       ret = kobject_register(&device->ports_parent);
-       if (ret)
+       device->ports_parent = kobject_create_and_add("ports",
+                                       kobject_get(&class_dev->kobj));
+       if (!device->ports_parent) {
+               ret = -ENOMEM;
                goto err_put;
+       }
 
-       if (device->node_type == IB_NODE_SWITCH) {
+       if (device->node_type == RDMA_NODE_IB_SWITCH) {
                ret = add_port(device, 0);
                if (ret)
                        goto err_put;
        } else {
-               int i;
-
                for (i = 1; i <= device->phys_port_cnt; ++i) {
                        ret = add_port(device, i);
                        if (ret)
@@ -701,6 +819,12 @@ int ib_device_register_sysfs(struct ib_device *device)
                }
        }
 
+       if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
+               ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
+               if (ret)
+                       goto err_put;
+       }
+
        return 0;
 
 err_put:
@@ -714,14 +838,14 @@ err_put:
                        sysfs_remove_group(p, &pma_group);
                        sysfs_remove_group(p, &port->pkey_group);
                        sysfs_remove_group(p, &port->gid_group);
-                       kobject_unregister(p);
+                       kobject_put(p);
                }
        }
 
        kobject_put(&class_dev->kobj);
 
 err_unregister:
-       class_device_unregister(class_dev);
+       device_unregister(class_dev);
 
 err:
        return ret;
@@ -738,11 +862,11 @@ void ib_device_unregister_sysfs(struct ib_device *device)
                sysfs_remove_group(p, &pma_group);
                sysfs_remove_group(p, &port->pkey_group);
                sysfs_remove_group(p, &port->gid_group);
-               kobject_unregister(p);
+               kobject_put(p);
        }
 
-       kobject_unregister(&device->ports_parent);
-       class_device_unregister(&device->class_dev);
+       kobject_put(device->ports_parent);
+       device_unregister(&device->dev);
 }
 
 int ib_sysfs_setup(void)