RDMA: iWARP Core Changes.
[safe/jmp/linux-2.6] / drivers / infiniband / core / sysfs.c
index fae1c2d..709323c 100644 (file)
@@ -36,6 +36,9 @@
 
 #include "core_priv.h"
 
+#include <linux/slab.h>
+#include <linux/string.h>
+
 #include <rdma/ib_mad.h>
 
 struct ib_port {
@@ -65,6 +68,11 @@ struct port_table_attribute {
        int                     index;
 };
 
+static inline int ibdev_is_alive(const struct ib_device *dev)
+{
+       return dev->reg_state == IB_DEV_REGISTERED;
+}
+
 static ssize_t port_attr_show(struct kobject *kobj,
                              struct attribute *attr, char *buf)
 {
@@ -74,6 +82,8 @@ static ssize_t port_attr_show(struct kobject *kobj,
 
        if (!port_attr->show)
                return -EIO;
+       if (!ibdev_is_alive(p->ibdev))
+               return -ENODEV;
 
        return port_attr->show(p, port_attr, buf);
 }
@@ -102,7 +112,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");
 }
 
@@ -300,14 +310,13 @@ static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
        if (!p->ibdev->process_mad)
                return sprintf(buf, "N/A (no PMA)\n");
 
-       in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
+       in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
        out_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
        if (!in_mad || !out_mad) {
                ret = -ENOMEM;
                goto out;
        }
 
-       memset(in_mad, 0, sizeof *in_mad);
        in_mad->mad_hdr.base_version  = 1;
        in_mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_PERF_MGMT;
        in_mad->mad_hdr.class_version = 1;
@@ -327,7 +336,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]);
@@ -425,24 +434,18 @@ static void ib_device_release(struct class_device *cdev)
        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 class_device *cdev, char **envp,
+                           int num_envp, char *buf, int size)
 {
        struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
        int i = 0, len = 0;
 
-       if (add_hotplug_env_var(envp, num_envp, &i, buf, size, &len,
-                               "NAME=%s", dev->name))
+       if (add_uevent_var(envp, num_envp, &i, buf, size, &len,
+                          "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;
@@ -463,14 +466,16 @@ alloc_group_attrs(ssize_t (*show)(struct ib_port *,
                return NULL;
 
        for (i = 0; i < len; i++) {
-               element = kcalloc(1, sizeof(struct port_table_attribute),
+               element = kzalloc(sizeof(struct port_table_attribute),
                                  GFP_KERNEL);
                if (!element)
                        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;
@@ -501,10 +506,9 @@ static int add_port(struct ib_device *device, int port_num)
        if (ret)
                return ret;
 
-       p = kmalloc(sizeof *p, GFP_KERNEL);
+       p = kzalloc(sizeof *p, GFP_KERNEL);
        if (!p)
                return -ENOMEM;
-       memset(p, 0, sizeof *p);
 
        p->ibdev      = device;
        p->port_num   = port_num;
@@ -581,11 +585,15 @@ static ssize_t show_node_type(struct class_device *cdev, char *buf)
 {
        struct ib_device *dev = container_of(cdev, struct ib_device, class_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);
        }
 }
 
@@ -595,6 +603,9 @@ static ssize_t show_sys_image_guid(struct class_device *cdev, char *buf)
        struct ib_device_attr attr;
        ssize_t ret;
 
+       if (!ibdev_is_alive(dev))
+               return -ENODEV;
+
        ret = ib_query_device(dev, &attr);
        if (ret)
                return ret;
@@ -609,34 +620,59 @@ static ssize_t show_sys_image_guid(struct class_device *cdev, char *buf)
 static ssize_t show_node_guid(struct class_device *cdev, char *buf)
 {
        struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
-       struct ib_device_attr attr;
-       ssize_t ret;
 
-       ret = ib_query_device(dev, &attr);
+       if (!ibdev_is_alive(dev))
+               return -ENODEV;
+
+       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 class_device *cdev, char *buf)
+{
+       struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
+
+       return sprintf(buf, "%.64s\n", dev->node_desc);
+}
+
+static ssize_t set_node_desc(struct class_device *cdev, const char *buf,
+                             size_t count)
+{
+       struct ib_device *dev = container_of(cdev, struct ib_device, class_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 CLASS_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
+       &class_device_attr_node_guid,
+       &class_device_attr_node_desc
 };
 
 static struct class ib_class = {
        .name    = "infiniband",
        .release = ib_device_release,
-       .hotplug = ib_device_hotplug,
+       .uevent = ib_device_uevent,
 };
 
 int ib_device_register_sysfs(struct ib_device *device)
@@ -673,7 +709,7 @@ int ib_device_register_sysfs(struct ib_device *device)
        if (ret)
                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;