Revert "drm: don't associate _DRM_DRIVER maps with a master"
[safe/jmp/linux-2.6] / drivers / firewire / fw-topology.c
index d3131e7..d0deecc 100644 (file)
@@ -1,6 +1,5 @@
-/*                                             -*- c-basic-offset: 8 -*-
- *
- * fw-topology.c - Incremental bus scan, based on bus topology
+/*
+ * Incremental bus scan, based on bus topology
  *
  * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net>
  *
 #include <linux/module.h>
 #include <linux/wait.h>
 #include <linux/errno.h>
+#include <asm/bug.h>
+#include <asm/system.h>
 #include "fw-transaction.h"
 #include "fw-topology.h"
 
-#define self_id_phy_id(q)              (((q) >> 24) & 0x3f)
-#define self_id_extended(q)            (((q) >> 23) & 0x01)
-#define self_id_link_on(q)             (((q) >> 22) & 0x01)
-#define self_id_gap_count(q)           (((q) >> 16) & 0x3f)
-#define self_id_phy_speed(q)           (((q) >> 14) & 0x03)
-#define self_id_contender(q)           (((q) >> 11) & 0x01)
-#define self_id_phy_initiator(q)       (((q) >>  1) & 0x01)
-#define self_id_more_packets(q)                (((q) >>  0) & 0x01)
+#define SELF_ID_PHY_ID(q)              (((q) >> 24) & 0x3f)
+#define SELF_ID_EXTENDED(q)            (((q) >> 23) & 0x01)
+#define SELF_ID_LINK_ON(q)             (((q) >> 22) & 0x01)
+#define SELF_ID_GAP_COUNT(q)           (((q) >> 16) & 0x3f)
+#define SELF_ID_PHY_SPEED(q)           (((q) >> 14) & 0x03)
+#define SELF_ID_CONTENDER(q)           (((q) >> 11) & 0x01)
+#define SELF_ID_PHY_INITIATOR(q)       (((q) >>  1) & 0x01)
+#define SELF_ID_MORE_PACKETS(q)                (((q) >>  0) & 0x01)
 
-#define self_id_ext_sequence(q)                (((q) >> 20) & 0x07)
+#define SELF_ID_EXT_SEQUENCE(q)                (((q) >> 20) & 0x07)
 
 static u32 *count_ports(u32 *sid, int *total_port_count, int *child_port_count)
 {
@@ -62,20 +63,22 @@ static u32 *count_ports(u32 *sid, int *total_port_count, int *child_port_count)
 
                shift -= 2;
                if (shift == 0) {
-                       if (!self_id_more_packets(q))
+                       if (!SELF_ID_MORE_PACKETS(q))
                                return sid + 1;
 
                        shift = 16;
                        sid++;
                        q = *sid;
 
-                       /* Check that the extra packets actually are
+                       /*
+                        * Check that the extra packets actually are
                         * extended self ID packets and that the
                         * sequence numbers in the extended self ID
-                        * packets increase as expected. */
+                        * packets increase as expected.
+                        */
 
-                       if (!self_id_extended(q) ||
-                           seq != self_id_ext_sequence(q))
+                       if (!SELF_ID_EXTENDED(q) ||
+                           seq != SELF_ID_EXT_SEQUENCE(q))
                                return NULL;
 
                        seq++;
@@ -96,15 +99,16 @@ static struct fw_node *fw_node_create(u32 sid, int port_count, int color)
 {
        struct fw_node *node;
 
-       node = kzalloc(sizeof *node + port_count * sizeof node->ports[0],
+       node = kzalloc(sizeof(*node) + port_count * sizeof(node->ports[0]),
                       GFP_ATOMIC);
        if (node == NULL)
                return NULL;
 
        node->color = color;
-       node->node_id = LOCAL_BUS | self_id_phy_id(sid);
-       node->link_on = self_id_link_on(sid);
-       node->phy_speed = self_id_phy_speed(sid);
+       node->node_id = LOCAL_BUS | SELF_ID_PHY_ID(sid);
+       node->link_on = SELF_ID_LINK_ON(sid);
+       node->phy_speed = SELF_ID_PHY_SPEED(sid);
+       node->initiated_reset = SELF_ID_PHY_INITIATOR(sid);
        node->port_count = port_count;
 
        atomic_set(&node->ref_count, 1);
@@ -113,7 +117,8 @@ static struct fw_node *fw_node_create(u32 sid, int port_count, int color)
        return node;
 }
 
-/* Compute the maximum hop count for this node and it's children.  The
+/*
+ * Compute the maximum hop count for this node and it's children.  The
  * maximum hop count is the maximum number of connections between any
  * two nodes in the subtree rooted at this node.  We need this for
  * setting the gap count.  As we build the tree bottom up in
@@ -133,23 +138,27 @@ static void update_hop_count(struct fw_node *node)
        int i;
 
        for (i = 0; i < node->port_count; i++) {
-               if (node->ports[i].node == NULL)
+               if (node->ports[i] == NULL)
                        continue;
 
-               if (node->ports[i].node->max_hops > max_child_hops)
-                       max_child_hops = node->ports[i].node->max_hops;
+               if (node->ports[i]->max_hops > max_child_hops)
+                       max_child_hops = node->ports[i]->max_hops;
 
-               if (node->ports[i].node->max_depth > depths[0]) {
+               if (node->ports[i]->max_depth > depths[0]) {
                        depths[1] = depths[0];
-                       depths[0] = node->ports[i].node->max_depth;
-               } else if (node->ports[i].node->max_depth > depths[1])
-                       depths[1] = node->ports[i].node->max_depth;
+                       depths[0] = node->ports[i]->max_depth;
+               } else if (node->ports[i]->max_depth > depths[1])
+                       depths[1] = node->ports[i]->max_depth;
        }
 
        node->max_depth = depths[0] + 1;
        node->max_hops = max(max_child_hops, depths[0] + depths[1] + 2);
 }
 
+static inline struct fw_node *fw_node(struct list_head *l)
+{
+       return list_entry(l, struct fw_node, link);
+}
 
 /**
  * build_tree - Build the tree representation of the topology
@@ -160,27 +169,28 @@ static void update_hop_count(struct fw_node *node)
  * This function builds the tree representation of the topology given
  * by the self IDs from the latest bus reset.  During the construction
  * of the tree, the function checks that the self IDs are valid and
- * internally consistent.  On succcess this funtions returns the
+ * internally consistent.  On succcess this function returns the
  * fw_node corresponding to the local card otherwise NULL.
  */
-static struct fw_node *build_tree(struct fw_card *card)
+static struct fw_node *build_tree(struct fw_card *card,
+                                 u32 *sid, int self_id_count)
 {
-       struct fw_node *node, *child, *local_node;
+       struct fw_node *node, *child, *local_node, *irm_node;
        struct list_head stack, *h;
-       u32 *sid, *next_sid, *end, q;
+       u32 *next_sid, *end, q;
        int i, port_count, child_port_count, phy_id, parent_count, stack_depth;
-       int gap_count, topology_type;
+       int gap_count;
+       bool beta_repeaters_present;
 
        local_node = NULL;
        node = NULL;
        INIT_LIST_HEAD(&stack);
        stack_depth = 0;
-       sid = card->self_ids;
-       end = sid + card->self_id_count;
+       end = sid + self_id_count;
        phy_id = 0;
-       card->irm_node = NULL;
-       gap_count = self_id_gap_count(*sid);
-       topology_type = 0;
+       irm_node = NULL;
+       gap_count = SELF_ID_GAP_COUNT(*sid);
+       beta_repeaters_present = false;
 
        while (sid < end) {
                next_sid = count_ports(sid, &port_count, &child_port_count);
@@ -191,9 +201,9 @@ static struct fw_node *build_tree(struct fw_card *card)
                }
 
                q = *sid;
-               if (phy_id != self_id_phy_id(q)) {
+               if (phy_id != SELF_ID_PHY_ID(q)) {
                        fw_error("PHY ID mismatch in self ID: %d != %d.\n",
-                                phy_id, self_id_phy_id(q));
+                                phy_id, SELF_ID_PHY_ID(q));
                        return NULL;
                }
 
@@ -202,35 +212,37 @@ static struct fw_node *build_tree(struct fw_card *card)
                        return NULL;
                }
 
-               /* Seek back from the top of our stack to find the
-                * start of the child nodes for this node. */
+               /*
+                * Seek back from the top of our stack to find the
+                * start of the child nodes for this node.
+                */
                for (i = 0, h = &stack; i < child_port_count; i++)
                        h = h->prev;
+               /*
+                * When the stack is empty, this yields an invalid value,
+                * but that pointer will never be dereferenced.
+                */
                child = fw_node(h);
 
                node = fw_node_create(q, port_count, card->color);
                if (node == NULL) {
-                       fw_error("Out of memory while building topology.");
+                       fw_error("Out of memory while building topology.\n");
                        return NULL;
                }
 
                if (phy_id == (card->node_id & 0x3f))
                        local_node = node;
 
-               if (self_id_contender(q))
-                       card->irm_node = node;
-
-               if (node->phy_speed == SCODE_BETA)
-                       topology_type |= FW_TOPOLOGY_B;
-               else
-                       topology_type |= FW_TOPOLOGY_A;
+               if (SELF_ID_CONTENDER(q))
+                       irm_node = node;
 
                parent_count = 0;
 
                for (i = 0; i < port_count; i++) {
                        switch (get_port_type(sid, i)) {
                        case SELFID_PORT_PARENT:
-                               /* Who's your daddy?  We dont know the
+                               /*
+                                * Who's your daddy?  We dont know the
                                 * parent node at this time, so we
                                 * temporarily abuse node->color for
                                 * remembering the entry in the
@@ -244,19 +256,23 @@ static struct fw_node *build_tree(struct fw_card *card)
                                break;
 
                        case SELFID_PORT_CHILD:
-                               node->ports[i].node = child;
-                               /* Fix up parent reference for this
-                                * child node. */
-                               child->ports[child->color].node = node;
+                               node->ports[i] = child;
+                               /*
+                                * Fix up parent reference for this
+                                * child node.
+                                */
+                               child->ports[child->color] = node;
                                child->color = card->color;
                                child = fw_node(child->link.next);
                                break;
                        }
                }
 
-               /* Check that the node reports exactly one parent
+               /*
+                * Check that the node reports exactly one parent
                 * port, except for the root, which of course should
-                * have no parents. */
+                * have no parents.
+                */
                if ((next_sid == end && parent_count != 0) ||
                    (next_sid < end && parent_count != 1)) {
                        fw_error("Parent port inconsistency for node %d: "
@@ -269,11 +285,16 @@ static struct fw_node *build_tree(struct fw_card *card)
                list_add_tail(&node->link, &stack);
                stack_depth += 1 - child_port_count;
 
-               /* If all PHYs does not report the same gap count
-                * setting, we fall back to 63 which will force a gap
-                * count reconfiguration and a reset. */
-               if (self_id_gap_count(q) != gap_count)
-                       gap_count = 63;
+               if (node->phy_speed == SCODE_BETA &&
+                   parent_count + child_port_count > 1)
+                       beta_repeaters_present = true;
+
+               /*
+                * If PHYs report different gap counts, set an invalid count
+                * which will force a gap count reconfiguration and a reset.
+                */
+               if (SELF_ID_GAP_COUNT(q) != gap_count)
+                       gap_count = 0;
 
                update_hop_count(node);
 
@@ -282,19 +303,19 @@ static struct fw_node *build_tree(struct fw_card *card)
        }
 
        card->root_node = node;
+       card->irm_node = irm_node;
        card->gap_count = gap_count;
-       card->topology_type = topology_type;
+       card->beta_repeaters_present = beta_repeaters_present;
 
        return local_node;
 }
 
-typedef void (*fw_node_callback_t) (struct fw_card * card,
-                                   struct fw_node * node,
-                                   struct fw_node * parent);
+typedef void (*fw_node_callback_t)(struct fw_card * card,
+                                  struct fw_node * node,
+                                  struct fw_node * parent);
 
-static void
-for_each_fw_node(struct fw_card *card, struct fw_node *root,
-                fw_node_callback_t callback)
+static void for_each_fw_node(struct fw_card *card, struct fw_node *root,
+                            fw_node_callback_t callback)
 {
        struct list_head list;
        struct fw_node *node, *next, *child, *parent;
@@ -309,7 +330,7 @@ for_each_fw_node(struct fw_card *card, struct fw_node *root,
                node->color = card->color;
 
                for (i = 0; i < node->port_count; i++) {
-                       child = node->ports[i].node;
+                       child = node->ports[i];
                        if (!child)
                                continue;
                        if (child->color == card->color)
@@ -327,23 +348,25 @@ for_each_fw_node(struct fw_card *card, struct fw_node *root,
                fw_node_put(node);
 }
 
-static void
-report_lost_node(struct fw_card *card,
-                struct fw_node *node, struct fw_node *parent)
+static void report_lost_node(struct fw_card *card,
+                            struct fw_node *node, struct fw_node *parent)
 {
        fw_node_event(card, node, FW_NODE_DESTROYED);
        fw_node_put(node);
+
+       /* Topology has changed - reset bus manager retry counter */
+       card->bm_retries = 0;
 }
 
-static void
-report_found_node(struct fw_card *card,
-                 struct fw_node *node, struct fw_node *parent)
+static void report_found_node(struct fw_card *card,
+                             struct fw_node *node, struct fw_node *parent)
 {
        int b_path = (node->phy_speed == SCODE_BETA);
 
        if (parent != NULL) {
-               node->max_speed = min((u8)parent->max_speed,
-                                     (u8)node->phy_speed);
+               /* min() macro doesn't work here with gcc 3.4 */
+               node->max_speed = parent->max_speed < node->phy_speed ?
+                                       parent->max_speed : node->phy_speed;
                node->b_path = parent->b_path && b_path;
        } else {
                node->max_speed = node->phy_speed;
@@ -351,6 +374,9 @@ report_found_node(struct fw_card *card,
        }
 
        fw_node_event(card, node, FW_NODE_CREATED);
+
+       /* Topology has changed - reset bus manager retry counter */
+       card->bm_retries = 0;
 }
 
 void fw_destroy_nodes(struct fw_card *card)
@@ -361,6 +387,7 @@ void fw_destroy_nodes(struct fw_card *card)
        card->color++;
        if (card->local_node != NULL)
                for_each_fw_node(card, card->local_node, report_lost_node);
+       card->local_node = NULL;
        spin_unlock_irqrestore(&card->lock, flags);
 }
 
@@ -369,11 +396,11 @@ static void move_tree(struct fw_node *node0, struct fw_node *node1, int port)
        struct fw_node *tree;
        int i;
 
-       tree = node1->ports[port].node;
-       node0->ports[port].node = tree;
+       tree = node1->ports[port];
+       node0->ports[port] = tree;
        for (i = 0; i < tree->port_count; i++) {
-               if (tree->ports[i].node == node1) {
-                       tree->ports[i].node = node0;
+               if (tree->ports[i] == node1) {
+                       tree->ports[i] = node0;
                        break;
                }
        }
@@ -385,11 +412,10 @@ static void move_tree(struct fw_node *node0, struct fw_node *node1, int port)
  * found, lost or updated.  Update the nodes in the card topology tree
  * as we go.
  */
-static void
-update_tree(struct fw_card *card, struct fw_node *root)
+static void update_tree(struct fw_card *card, struct fw_node *root)
 {
        struct list_head list0, list1;
-       struct fw_node *node0, *node1;
+       struct fw_node *node0, *node1, *next1;
        int i, event;
 
        INIT_LIST_HEAD(&list0);
@@ -401,12 +427,14 @@ update_tree(struct fw_card *card, struct fw_node *root)
        node1 = fw_node(list1.next);
 
        while (&node0->link != &list0) {
+               WARN_ON(node0->port_count != node1->port_count);
 
-               /* assert(node0->port_count == node1->port_count); */
                if (node0->link_on && !node1->link_on)
                        event = FW_NODE_LINK_OFF;
                else if (!node0->link_on && node1->link_on)
                        event = FW_NODE_LINK_ON;
+               else if (node1->initiated_reset && node1->link_on)
+                       event = FW_NODE_INITIATED_RESET;
                else
                        event = FW_NODE_UPDATED;
 
@@ -424,65 +452,94 @@ update_tree(struct fw_card *card, struct fw_node *root)
                        card->irm_node = node0;
 
                for (i = 0; i < node0->port_count; i++) {
-                       if (node0->ports[i].node && node1->ports[i].node) {
-                               /* This port didn't change, queue the
+                       if (node0->ports[i] && node1->ports[i]) {
+                               /*
+                                * This port didn't change, queue the
                                 * connected node for further
-                                * investigation. */
-                               if (node0->ports[i].node->color == card->color)
+                                * investigation.
+                                */
+                               if (node0->ports[i]->color == card->color)
                                        continue;
-                               list_add_tail(&node0->ports[i].node->link,
-                                             &list0);
-                               list_add_tail(&node1->ports[i].node->link,
-                                             &list1);
-                       } else if (node0->ports[i].node) {
-                               /* The nodes connected here were
+                               list_add_tail(&node0->ports[i]->link, &list0);
+                               list_add_tail(&node1->ports[i]->link, &list1);
+                       } else if (node0->ports[i]) {
+                               /*
+                                * The nodes connected here were
                                 * unplugged; unref the lost nodes and
                                 * queue FW_NODE_LOST callbacks for
-                                * them. */
+                                * them.
+                                */
 
-                               for_each_fw_node(card, node0->ports[i].node,
+                               for_each_fw_node(card, node0->ports[i],
                                                 report_lost_node);
-                               node0->ports[i].node = NULL;
-                       } else if (node1->ports[i].node) {
-                               /* One or more node were connected to
+                               node0->ports[i] = NULL;
+                       } else if (node1->ports[i]) {
+                               /*
+                                * One or more node were connected to
                                 * this port. Move the new nodes into
                                 * the tree and queue FW_NODE_CREATED
-                                * callbacks for them. */
+                                * callbacks for them.
+                                */
                                move_tree(node0, node1, i);
-                               for_each_fw_node(card, node0->ports[i].node,
+                               for_each_fw_node(card, node0->ports[i],
                                                 report_found_node);
                        }
                }
 
                node0 = fw_node(node0->link.next);
-               node1 = fw_node(node1->link.next);
+               next1 = fw_node(node1->link.next);
+               fw_node_put(node1);
+               node1 = next1;
        }
 }
 
-void
-fw_core_handle_bus_reset(struct fw_card *card,
-                        int node_id, int generation,
-                        int self_id_count, u32 * self_ids)
+static void update_topology_map(struct fw_card *card,
+                               u32 *self_ids, int self_id_count)
+{
+       int node_count;
+
+       card->topology_map[1]++;
+       node_count = (card->root_node->node_id & 0x3f) + 1;
+       card->topology_map[2] = (node_count << 16) | self_id_count;
+       card->topology_map[0] = (self_id_count + 2) << 16;
+       memcpy(&card->topology_map[3], self_ids, self_id_count * 4);
+       fw_compute_block_crc(card->topology_map);
+}
+
+void fw_core_handle_bus_reset(struct fw_card *card, int node_id, int generation,
+                             int self_id_count, u32 *self_ids)
 {
        struct fw_node *local_node;
        unsigned long flags;
 
-       fw_flush_transactions(card);
+       /*
+        * If the selfID buffer is not the immediate successor of the
+        * previously processed one, we cannot reliably compare the
+        * old and new topologies.
+        */
+       if (!is_next_generation(generation, card->generation) &&
+           card->local_node != NULL) {
+               fw_notify("skipped bus generations, destroying all nodes\n");
+               fw_destroy_nodes(card);
+               card->bm_retries = 0;
+       }
 
        spin_lock_irqsave(&card->lock, flags);
 
-       /* If the new topology has a different self_id_count the topology
-        * changed, either nodes were added or removed. In that case we
-        * reset the IRM reset counter. */
-       if (card->self_id_count != self_id_count)
-               card->irm_retries = 0;
-
+       card->broadcast_channel_allocated = false;
        card->node_id = node_id;
-       card->self_id_count = self_id_count;
+       /*
+        * Update node_id before generation to prevent anybody from using
+        * a stale node_id together with a current generation.
+        */
+       smp_wmb();
        card->generation = generation;
-       memcpy(card->self_ids, self_ids, self_id_count * 4);
+       card->reset_jiffies = jiffies;
+       fw_schedule_bm_work(card, 0);
 
-       local_node = build_tree(card);
+       local_node = build_tree(card, self_ids, self_id_count);
+
+       update_topology_map(card, self_ids, self_id_count);
 
        card->color++;
 
@@ -496,10 +553,6 @@ fw_core_handle_bus_reset(struct fw_card *card,
                update_tree(card, local_node);
        }
 
-       /* If we're not the root node, we may have to do some IRM work. */
-       if (card->local_node != card->root_node)
-               schedule_delayed_work(&card->work, 0);
-
        spin_unlock_irqrestore(&card->lock, flags);
 }
 EXPORT_SYMBOL(fw_core_handle_bus_reset);