firewire: Implement gap count optimization.

Signed-off-by: Kristian Høgsberg <krh@redhat.com>
Signed-off-by: Stefan Richter <stefanr@s5r6.in-berlin.de>
diff --git a/drivers/firewire/fw-card.c b/drivers/firewire/fw-card.c
index c8b7d69..307c8b8 100644
--- a/drivers/firewire/fw-card.c
+++ b/drivers/firewire/fw-card.c
@@ -186,14 +186,17 @@
 }
 EXPORT_SYMBOL(fw_core_remove_descriptor);
 
+static const char gap_count_table[] = {
+	63, 5, 7, 8, 10, 13, 16, 18, 21, 24, 26, 29, 32, 35, 37, 40
+};
+
 static void
 fw_card_irm_work(struct work_struct *work)
 {
-	struct fw_card *card =
-		container_of(work, struct fw_card, work.work);
+	struct fw_card *card = container_of(work, struct fw_card, work.work);
 	struct fw_device *root;
 	unsigned long flags;
-	int new_irm_id, generation;
+	int root_id, new_irm_id, gap_count, generation, do_reset = 0;
 
 	/* FIXME: This simple bus management unconditionally picks a
 	 * cycle master if the current root can't do it.  We need to
@@ -206,35 +209,50 @@
 
 	generation = card->generation;
 	root = card->root_node->data;
+	root_id = card->root_node->node_id;
 
-	if (root == NULL)
+	if (root == NULL) {
 		/* Either link_on is false, or we failed to read the
 		 * config rom.  In either case, pick another root. */
 		new_irm_id = card->local_node->node_id;
-	else if (root->state != FW_DEVICE_RUNNING)
+	} else if (root->state != FW_DEVICE_RUNNING) {
 		/* If we haven't probed this device yet, bail out now
 		 * and let's try again once that's done. */
-		new_irm_id = -1;
-	else if (root->config_rom[2] & bib_cmc)
+		new_irm_id = root_id;
+	} else if (root->config_rom[2] & bib_cmc) {
 		/* FIXME: I suppose we should set the cmstr bit in the
 		 * STATE_CLEAR register of this node, as described in
 		 * 1394-1995, 8.4.2.6.  Also, send out a force root
 		 * packet for this node. */
-		new_irm_id = -1;
-	else
+		new_irm_id = root_id;
+	} else {
 		/* Current root has an active link layer and we
 		 * successfully read the config rom, but it's not
 		 * cycle master capable. */
 		new_irm_id = card->local_node->node_id;
+	}
 
-	if (card->irm_retries++ > 5)
-		new_irm_id = -1;
+	/* Now figure out what gap count to set. */
+	if (card->topology_type == FW_TOPOLOGY_A &&
+	    card->root_node->max_hops < ARRAY_SIZE(gap_count_table))
+		gap_count = gap_count_table[card->root_node->max_hops];
+	else
+		gap_count = 63;
+
+	/* Finally, figure out if we should do a reset or not.  If we've
+	 * done less that 5 resets with the same physical topology and we
+	 * have either a new root or a new gap count setting, let's do it. */
+
+	if (card->irm_retries++ < 5 &&
+	    (card->gap_count != gap_count || new_irm_id != root_id))
+		do_reset = 1;
 
 	spin_unlock_irqrestore(&card->lock, flags);
 
-	if (new_irm_id > 0) {
-		fw_notify("Trying to become root (card %d)\n", card->index);
-		fw_send_force_root(card, new_irm_id, generation);
+	if (do_reset) {
+		fw_notify("phy config: card %d, new root=%x, gap_count=%d\n",
+			  card->index, new_irm_id, gap_count);
+		fw_send_phy_config(card, new_irm_id, generation, gap_count);
 		fw_core_initiate_bus_reset(card, 1);
 	}
 }
diff --git a/drivers/firewire/fw-topology.c b/drivers/firewire/fw-topology.c
index 684d87d..d3131e7 100644
--- a/drivers/firewire/fw-topology.c
+++ b/drivers/firewire/fw-topology.c
@@ -113,6 +113,44 @@
 	return node;
 }
 
+/* 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
+ * build_tree() below, this is fairly easy to do: for each node we
+ * maintain the max hop count and the max depth, ie the number of hops
+ * to the furthest leaf.  Computing the max hop count breaks down into
+ * two cases: either the path goes through this node, in which case
+ * the hop count is the sum of the two biggest child depths plus 2.
+ * Or it could be the case that the max hop path is entirely
+ * containted in a child tree, in which case the max hop count is just
+ * the max hop count of this child.
+ */
+static void update_hop_count(struct fw_node *node)
+{
+	int depths[2] = { -1, -1 };
+	int max_child_hops = 0;
+	int i;
+
+	for (i = 0; i < node->port_count; i++) {
+		if (node->ports[i].node == 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].node->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;
+	}
+
+	node->max_depth = depths[0] + 1;
+	node->max_hops = max(max_child_hops, depths[0] + depths[1] + 2);
+}
+
+
 /**
  * build_tree - Build the tree representation of the topology
  * @self_ids: array of self IDs to create the tree from
@@ -131,6 +169,7 @@
 	struct list_head stack, *h;
 	u32 *sid, *next_sid, *end, q;
 	int i, port_count, child_port_count, phy_id, parent_count, stack_depth;
+	int gap_count, topology_type;
 
 	local_node = NULL;
 	node = NULL;
@@ -140,6 +179,8 @@
 	end = sid + card->self_id_count;
 	phy_id = 0;
 	card->irm_node = NULL;
+	gap_count = self_id_gap_count(*sid);
+	topology_type = 0;
 
 	while (sid < end) {
 		next_sid = count_ports(sid, &port_count, &child_port_count);
@@ -179,6 +220,11 @@
 		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;
+
 		parent_count = 0;
 
 		for (i = 0; i < port_count; i++) {
@@ -223,11 +269,21 @@
 		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;
+
+		update_hop_count(node);
+
 		sid = next_sid;
 		phy_id++;
 	}
 
 	card->root_node = node;
+	card->gap_count = gap_count;
+	card->topology_type = topology_type;
 
 	return local_node;
 }
@@ -286,7 +342,8 @@
 	int b_path = (node->phy_speed == SCODE_BETA);
 
 	if (parent != NULL) {
-		node->max_speed = min(parent->max_speed, node->phy_speed);
+		node->max_speed = min((u8)parent->max_speed,
+				      (u8)node->phy_speed);
 		node->b_path = parent->b_path && b_path;
 	} else {
 		node->max_speed = node->phy_speed;
@@ -329,7 +386,7 @@
  * as we go.
  */
 static void
-update_tree(struct fw_card *card, struct fw_node *root, int *changed)
+update_tree(struct fw_card *card, struct fw_node *root)
 {
 	struct list_head list0, list1;
 	struct fw_node *node0, *node1;
@@ -342,7 +399,6 @@
 
 	node0 = fw_node(list0.next);
 	node1 = fw_node(list1.next);
-	*changed = 0;
 
 	while (&node0->link != &list0) {
 
@@ -358,6 +414,7 @@
 		node0->color = card->color;
 		node0->link_on = node1->link_on;
 		node0->initiated_reset = node1->initiated_reset;
+		node0->max_hops = node1->max_hops;
 		node1->color = card->color;
 		fw_node_event(card, node0, event);
 
@@ -386,7 +443,6 @@
 				for_each_fw_node(card, node0->ports[i].node,
 						 report_lost_node);
 				node0->ports[i].node = NULL;
-				*changed = 1;
 			} else if (node1->ports[i].node) {
 				/* One or more node were connected to
 				 * this port. Move the new nodes into
@@ -395,7 +451,6 @@
 				move_tree(node0, node1, i);
 				for_each_fw_node(card, node0->ports[i].node,
 						 report_found_node);
-				*changed = 1;
 			}
 		}
 
@@ -411,12 +466,17 @@
 {
 	struct fw_node *local_node;
 	unsigned long flags;
-	int changed;
 
 	fw_flush_transactions(card);
 
 	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->node_id = node_id;
 	card->self_id_count = self_id_count;
 	card->generation = generation;
@@ -433,9 +493,7 @@
 		card->local_node = local_node;
 		for_each_fw_node(card, local_node, report_found_node);
 	} else {
-		update_tree(card, local_node, &changed);
-		if (changed)
-			card->irm_retries = 0;
+		update_tree(card, local_node);
 	}
 
 	/* If we're not the root node, we may have to do some IRM work. */
diff --git a/drivers/firewire/fw-topology.h b/drivers/firewire/fw-topology.h
index ab03059..f2a575e 100644
--- a/drivers/firewire/fw-topology.h
+++ b/drivers/firewire/fw-topology.h
@@ -23,6 +23,12 @@
 #define __fw_topology_h
 
 enum {
+	FW_TOPOLOGY_A =		0x01,
+	FW_TOPOLOGY_B =		0x02,
+	FW_TOPOLOGY_MIXED =	0x03,
+};
+
+enum {
 	FW_NODE_CREATED =   0x00,
 	FW_NODE_UPDATED =   0x01,
 	FW_NODE_DESTROYED = 0x02,
@@ -42,10 +48,11 @@
 	unsigned link_on : 1;
 	unsigned initiated_reset : 1;
 	unsigned b_path : 1;
-	u8 phy_speed; /* As in the self ID packet. */
-	u8 max_speed; /* Minimum of all phy-speeds and port speeds on
-		       * the path from the local node to this node. */
-
+	u8 phy_speed : 3; /* As in the self ID packet. */
+	u8 max_speed : 5; /* Minimum of all phy-speeds and port speeds on
+			   * the path from the local node to this node. */
+	u8 max_depth : 4; /* Maximum depth to any leaf node */
+	u8 max_hops : 4;  /* Max hops in this sub tree */
 	atomic_t ref_count;
 
 	/* For serializing node topology into a list. */
diff --git a/drivers/firewire/fw-transaction.c b/drivers/firewire/fw-transaction.c
index 439a3e3..57ecf95 100644
--- a/drivers/firewire/fw-transaction.c
+++ b/drivers/firewire/fw-transaction.c
@@ -274,11 +274,15 @@
 	card->driver->send_request(card, packet);
 }
 
-void fw_send_force_root(struct fw_card *card, int node_id, int generation)
+void fw_send_phy_config(struct fw_card *card,
+			int node_id, int generation, int gap_count)
 {
 	u32 q;
 
-	q = phy_identifier(PHY_PACKET_CONFIG) | phy_config_root_id(node_id);
+	q = phy_identifier(PHY_PACKET_CONFIG) |
+		phy_config_root_id(node_id) |
+		phy_config_gap_count(gap_count);
+
 	send_phy_packet(card, q, generation);
 }
 
diff --git a/drivers/firewire/fw-transaction.h b/drivers/firewire/fw-transaction.h
index 22d2871..df65245 100644
--- a/drivers/firewire/fw-transaction.h
+++ b/drivers/firewire/fw-transaction.h
@@ -259,6 +259,8 @@
 	struct fw_node *root_node;
 	struct fw_node *irm_node;
 	int color;
+	int gap_count;
+	int topology_type;
 
 	int index;
 
@@ -386,8 +388,8 @@
 
 void fw_flush_transactions(struct fw_card *card);
 
-void
-fw_send_force_root(struct fw_card *card, int node_id, int generation);
+void fw_send_phy_config(struct fw_card *card,
+			int node_id, int generation, int gap_count);
 
 /* Called by the topology code to inform the device code of node
  * activity; found, lost, or updated nodes */