Merge ../linus
[pandora-kernel.git] / drivers / ieee1394 / nodemgr.c
index f245366..d541b50 100644 (file)
@@ -8,8 +8,8 @@
  * directory of the kernel sources for details.
  */
 
+#include <linux/bitmap.h>
 #include <linux/kernel.h>
-#include <linux/config.h>
 #include <linux/list.h>
 #include <linux/slab.h>
 #include <linux/smp_lock.h>
@@ -38,6 +38,7 @@ struct nodemgr_csr_info {
        struct hpsb_host *host;
        nodeid_t nodeid;
        unsigned int generation;
+       unsigned int speed_unverified:1;
 };
 
 
@@ -57,23 +58,75 @@ static char *nodemgr_find_oui_name(int oui)
        return NULL;
 }
 
+/*
+ * Correct the speed map entry.  This is necessary
+ *  - for nodes with link speed < phy speed,
+ *  - for 1394b nodes with negotiated phy port speed < IEEE1394_SPEED_MAX.
+ * A possible speed is determined by trial and error, using quadlet reads.
+ */
+static int nodemgr_check_speed(struct nodemgr_csr_info *ci, u64 addr,
+                              quadlet_t *buffer)
+{
+       quadlet_t q;
+       u8 i, *speed, old_speed, good_speed;
+       int ret;
+
+       speed = ci->host->speed + NODEID_TO_NODE(ci->nodeid);
+       old_speed = *speed;
+       good_speed = IEEE1394_SPEED_MAX + 1;
+
+       /* Try every speed from S100 to old_speed.
+        * If we did it the other way around, a too low speed could be caught
+        * if the retry succeeded for some other reason, e.g. because the link
+        * just finished its initialization. */
+       for (i = IEEE1394_SPEED_100; i <= old_speed; i++) {
+               *speed = i;
+               ret = hpsb_read(ci->host, ci->nodeid, ci->generation, addr,
+                               &q, sizeof(quadlet_t));
+               if (ret)
+                       break;
+               *buffer = q;
+               good_speed = i;
+       }
+       if (good_speed <= IEEE1394_SPEED_MAX) {
+               HPSB_DEBUG("Speed probe of node " NODE_BUS_FMT " yields %s",
+                          NODE_BUS_ARGS(ci->host, ci->nodeid),
+                          hpsb_speedto_str[good_speed]);
+               *speed = good_speed;
+               ci->speed_unverified = 0;
+               return 0;
+       }
+       *speed = old_speed;
+       return ret;
+}
 
 static int nodemgr_bus_read(struct csr1212_csr *csr, u64 addr, u16 length,
                             void *buffer, void *__ci)
 {
        struct nodemgr_csr_info *ci = (struct nodemgr_csr_info*)__ci;
-       int i, ret = 0;
+       int i, ret;
 
        for (i = 1; ; i++) {
                ret = hpsb_read(ci->host, ci->nodeid, ci->generation, addr,
                                buffer, length);
-               if (!ret || i == 3)
+               if (!ret) {
+                       ci->speed_unverified = 0;
+                       break;
+               }
+               /* Give up after 3rd failure. */
+               if (i == 3)
                        break;
 
+               /* The ieee1394_core guessed the node's speed capability from
+                * the self ID.  Check whether a lower speed works. */
+               if (ci->speed_unverified && length == sizeof(quadlet_t)) {
+                       ret = nodemgr_check_speed(ci, addr, buffer);
+                       if (!ret)
+                               break;
+               }
                if (msleep_interruptible(334))
                        return -EINTR;
        }
-
        return ret;
 }
 
@@ -281,10 +334,12 @@ static ssize_t fw_show_ne_bus_options(struct device *dev, struct device_attribut
 static DEVICE_ATTR(bus_options,S_IRUGO,fw_show_ne_bus_options,NULL);
 
 
+/* tlabels_free, tlabels_allocations, tlabels_mask are read non-atomically
+ * here, therefore displayed values may be occasionally wrong. */
 static ssize_t fw_show_ne_tlabels_free(struct device *dev, struct device_attribute *attr, char *buf)
 {
        struct node_entry *ne = container_of(dev, struct node_entry, device);
-       return sprintf(buf, "%d\n", atomic_read(&ne->tpool->count.count) + 1);
+       return sprintf(buf, "%d\n", 64 - bitmap_weight(ne->tpool->pool, 64));
 }
 static DEVICE_ATTR(tlabels_free,S_IRUGO,fw_show_ne_tlabels_free,NULL);
 
@@ -743,21 +798,20 @@ static struct node_entry *nodemgr_create_node(octlet_t guid, struct csr1212_csr
                                              unsigned int generation)
 {
        struct hpsb_host *host = hi->host;
-        struct node_entry *ne;
-
-       ne = kmalloc(sizeof(struct node_entry), GFP_KERNEL);
-        if (!ne) return NULL;
+       struct node_entry *ne;
 
-       memset(ne, 0, sizeof(struct node_entry));
+       ne = kzalloc(sizeof(*ne), GFP_KERNEL);
+       if (!ne)
+               return NULL;
 
        ne->tpool = &host->tpool[nodeid & NODE_MASK];
 
-        ne->host = host;
-        ne->nodeid = nodeid;
+       ne->host = host;
+       ne->nodeid = nodeid;
        ne->generation = generation;
        ne->needs_probe = 1;
 
-        ne->guid = guid;
+       ne->guid = guid;
        ne->guid_vendor_id = (guid >> 40) & 0xffffff;
        ne->guid_vendor_oui = nodemgr_find_oui_name(ne->guid_vendor_id);
        ne->csr = csr;
@@ -787,7 +841,7 @@ static struct node_entry *nodemgr_create_node(octlet_t guid, struct csr1212_csr
                   (host->node_id == nodeid) ? "Host" : "Node",
                   NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
 
-        return ne;
+       return ne;
 }
 
 
@@ -872,12 +926,10 @@ static struct unit_directory *nodemgr_process_unit_directory
        struct csr1212_keyval *kv;
        u8 last_key_id = 0;
 
-       ud = kmalloc(sizeof(struct unit_directory), GFP_KERNEL);
+       ud = kzalloc(sizeof(*ud), GFP_KERNEL);
        if (!ud)
                goto unit_directory_error;
 
-       memset (ud, 0, sizeof(struct unit_directory));
-
        ud->ne = ne;
        ud->ignore_driver = ignore_drivers;
        ud->address = ud_kv->offset + CSR1212_CONFIG_ROM_SPACE_BASE;
@@ -937,10 +989,10 @@ static struct unit_directory *nodemgr_process_unit_directory
                        /* Logical Unit Number */
                        if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
                                if (ud->flags & UNIT_DIRECTORY_HAS_LUN) {
-                                       ud_child = kmalloc(sizeof(struct unit_directory), GFP_KERNEL);
+                                       ud_child = kmalloc(sizeof(*ud_child), GFP_KERNEL);
                                        if (!ud_child)
                                                goto unit_directory_error;
-                                       memcpy(ud_child, ud, sizeof(struct unit_directory));
+                                       memcpy(ud_child, ud, sizeof(*ud_child));
                                        nodemgr_register_device(ne, ud_child, &ne->device);
                                        ud_child = NULL;
                                        
@@ -1200,13 +1252,15 @@ static void nodemgr_node_scan_one(struct host_info *hi,
        struct csr1212_csr *csr;
        struct nodemgr_csr_info *ci;
 
-       ci = kmalloc(sizeof(struct nodemgr_csr_info), GFP_KERNEL);
+       ci = kmalloc(sizeof(*ci), GFP_KERNEL);
        if (!ci)
                return;
 
        ci->host = host;
        ci->nodeid = nodeid;
        ci->generation = generation;
+       ci->speed_unverified =
+               host->speed[NODEID_TO_NODE(nodeid)] > IEEE1394_SPEED_100;
 
        /* We need to detect when the ConfigROM's generation has changed,
         * so we only update the node's info when it needs to be.  */
@@ -1410,14 +1464,28 @@ static void nodemgr_node_probe(struct host_info *hi, int generation)
        struct hpsb_host *host = hi->host;
        struct class *class = &nodemgr_ne_class;
        struct class_device *cdev;
+       struct node_entry *ne;
 
        /* Do some processing of the nodes we've probed. This pulls them
         * into the sysfs layer if needed, and can result in processing of
         * unit-directories, or just updating the node and it's
-        * unit-directories. */
+        * unit-directories.
+        *
+        * Run updates before probes. Usually, updates are time-critical
+        * while probes are time-consuming. (Well, those probes need some
+        * improvement...) */
+
        down_read(&class->subsys.rwsem);
-       list_for_each_entry(cdev, &class->children, node)
-               nodemgr_probe_ne(hi, container_of(cdev, struct node_entry, class_dev), generation);
+       list_for_each_entry(cdev, &class->children, node) {
+               ne = container_of(cdev, struct node_entry, class_dev);
+               if (!ne->needs_probe)
+                       nodemgr_probe_ne(hi, ne, generation);
+       }
+       list_for_each_entry(cdev, &class->children, node) {
+               ne = container_of(cdev, struct node_entry, class_dev);
+               if (ne->needs_probe)
+                       nodemgr_probe_ne(hi, ne, generation);
+       }
         up_read(&class->subsys.rwsem);
 
 
@@ -1448,7 +1516,8 @@ static int nodemgr_send_resume_packet(struct hpsb_host *host)
        int ret = 1;
 
        packet = hpsb_make_phypacket(host,
-                       0x003c0000 | NODEID_TO_NODE(host->node_id) << 24);
+                       EXTPHYPACKET_TYPE_RESUME |
+                       NODEID_TO_NODE(host->node_id) << PHYPACKET_PORT_SHIFT);
        if (packet) {
                packet->no_waiter = 1;
                packet->generation = get_hpsb_generation(host);