Path: blob/master/drivers/firewire/core-topology.c
15109 views
/*1* Incremental bus scan, based on bus topology2*3* Copyright (C) 2004-2006 Kristian Hoegsberg <[email protected]>4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation; either version 2 of the License, or8* (at your option) any later version.9*10* This program is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13* GNU General Public License for more details.14*15* You should have received a copy of the GNU General Public License16* along with this program; if not, write to the Free Software Foundation,17* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.18*/1920#include <linux/bug.h>21#include <linux/errno.h>22#include <linux/firewire.h>23#include <linux/firewire-constants.h>24#include <linux/jiffies.h>25#include <linux/kernel.h>26#include <linux/list.h>27#include <linux/module.h>28#include <linux/slab.h>29#include <linux/spinlock.h>3031#include <asm/atomic.h>32#include <asm/byteorder.h>33#include <asm/system.h>3435#include "core.h"3637#define SELF_ID_PHY_ID(q) (((q) >> 24) & 0x3f)38#define SELF_ID_EXTENDED(q) (((q) >> 23) & 0x01)39#define SELF_ID_LINK_ON(q) (((q) >> 22) & 0x01)40#define SELF_ID_GAP_COUNT(q) (((q) >> 16) & 0x3f)41#define SELF_ID_PHY_SPEED(q) (((q) >> 14) & 0x03)42#define SELF_ID_CONTENDER(q) (((q) >> 11) & 0x01)43#define SELF_ID_PHY_INITIATOR(q) (((q) >> 1) & 0x01)44#define SELF_ID_MORE_PACKETS(q) (((q) >> 0) & 0x01)4546#define SELF_ID_EXT_SEQUENCE(q) (((q) >> 20) & 0x07)4748#define SELFID_PORT_CHILD 0x349#define SELFID_PORT_PARENT 0x250#define SELFID_PORT_NCONN 0x151#define SELFID_PORT_NONE 0x05253static u32 *count_ports(u32 *sid, int *total_port_count, int *child_port_count)54{55u32 q;56int port_type, shift, seq;5758*total_port_count = 0;59*child_port_count = 0;6061shift = 6;62q = *sid;63seq = 0;6465while (1) {66port_type = (q >> shift) & 0x03;67switch (port_type) {68case SELFID_PORT_CHILD:69(*child_port_count)++;70case SELFID_PORT_PARENT:71case SELFID_PORT_NCONN:72(*total_port_count)++;73case SELFID_PORT_NONE:74break;75}7677shift -= 2;78if (shift == 0) {79if (!SELF_ID_MORE_PACKETS(q))80return sid + 1;8182shift = 16;83sid++;84q = *sid;8586/*87* Check that the extra packets actually are88* extended self ID packets and that the89* sequence numbers in the extended self ID90* packets increase as expected.91*/9293if (!SELF_ID_EXTENDED(q) ||94seq != SELF_ID_EXT_SEQUENCE(q))95return NULL;9697seq++;98}99}100}101102static int get_port_type(u32 *sid, int port_index)103{104int index, shift;105106index = (port_index + 5) / 8;107shift = 16 - ((port_index + 5) & 7) * 2;108return (sid[index] >> shift) & 0x03;109}110111static struct fw_node *fw_node_create(u32 sid, int port_count, int color)112{113struct fw_node *node;114115node = kzalloc(sizeof(*node) + port_count * sizeof(node->ports[0]),116GFP_ATOMIC);117if (node == NULL)118return NULL;119120node->color = color;121node->node_id = LOCAL_BUS | SELF_ID_PHY_ID(sid);122node->link_on = SELF_ID_LINK_ON(sid);123node->phy_speed = SELF_ID_PHY_SPEED(sid);124node->initiated_reset = SELF_ID_PHY_INITIATOR(sid);125node->port_count = port_count;126127atomic_set(&node->ref_count, 1);128INIT_LIST_HEAD(&node->link);129130return node;131}132133/*134* Compute the maximum hop count for this node and it's children. The135* maximum hop count is the maximum number of connections between any136* two nodes in the subtree rooted at this node. We need this for137* setting the gap count. As we build the tree bottom up in138* build_tree() below, this is fairly easy to do: for each node we139* maintain the max hop count and the max depth, ie the number of hops140* to the furthest leaf. Computing the max hop count breaks down into141* two cases: either the path goes through this node, in which case142* the hop count is the sum of the two biggest child depths plus 2.143* Or it could be the case that the max hop path is entirely144* containted in a child tree, in which case the max hop count is just145* the max hop count of this child.146*/147static void update_hop_count(struct fw_node *node)148{149int depths[2] = { -1, -1 };150int max_child_hops = 0;151int i;152153for (i = 0; i < node->port_count; i++) {154if (node->ports[i] == NULL)155continue;156157if (node->ports[i]->max_hops > max_child_hops)158max_child_hops = node->ports[i]->max_hops;159160if (node->ports[i]->max_depth > depths[0]) {161depths[1] = depths[0];162depths[0] = node->ports[i]->max_depth;163} else if (node->ports[i]->max_depth > depths[1])164depths[1] = node->ports[i]->max_depth;165}166167node->max_depth = depths[0] + 1;168node->max_hops = max(max_child_hops, depths[0] + depths[1] + 2);169}170171static inline struct fw_node *fw_node(struct list_head *l)172{173return list_entry(l, struct fw_node, link);174}175176/*177* This function builds the tree representation of the topology given178* by the self IDs from the latest bus reset. During the construction179* of the tree, the function checks that the self IDs are valid and180* internally consistent. On success this function returns the181* fw_node corresponding to the local card otherwise NULL.182*/183static struct fw_node *build_tree(struct fw_card *card,184u32 *sid, int self_id_count)185{186struct fw_node *node, *child, *local_node, *irm_node;187struct list_head stack, *h;188u32 *next_sid, *end, q;189int i, port_count, child_port_count, phy_id, parent_count, stack_depth;190int gap_count;191bool beta_repeaters_present;192193local_node = NULL;194node = NULL;195INIT_LIST_HEAD(&stack);196stack_depth = 0;197end = sid + self_id_count;198phy_id = 0;199irm_node = NULL;200gap_count = SELF_ID_GAP_COUNT(*sid);201beta_repeaters_present = false;202203while (sid < end) {204next_sid = count_ports(sid, &port_count, &child_port_count);205206if (next_sid == NULL) {207fw_error("Inconsistent extended self IDs.\n");208return NULL;209}210211q = *sid;212if (phy_id != SELF_ID_PHY_ID(q)) {213fw_error("PHY ID mismatch in self ID: %d != %d.\n",214phy_id, SELF_ID_PHY_ID(q));215return NULL;216}217218if (child_port_count > stack_depth) {219fw_error("Topology stack underflow\n");220return NULL;221}222223/*224* Seek back from the top of our stack to find the225* start of the child nodes for this node.226*/227for (i = 0, h = &stack; i < child_port_count; i++)228h = h->prev;229/*230* When the stack is empty, this yields an invalid value,231* but that pointer will never be dereferenced.232*/233child = fw_node(h);234235node = fw_node_create(q, port_count, card->color);236if (node == NULL) {237fw_error("Out of memory while building topology.\n");238return NULL;239}240241if (phy_id == (card->node_id & 0x3f))242local_node = node;243244if (SELF_ID_CONTENDER(q))245irm_node = node;246247parent_count = 0;248249for (i = 0; i < port_count; i++) {250switch (get_port_type(sid, i)) {251case SELFID_PORT_PARENT:252/*253* Who's your daddy? We dont know the254* parent node at this time, so we255* temporarily abuse node->color for256* remembering the entry in the257* node->ports array where the parent258* node should be. Later, when we259* handle the parent node, we fix up260* the reference.261*/262parent_count++;263node->color = i;264break;265266case SELFID_PORT_CHILD:267node->ports[i] = child;268/*269* Fix up parent reference for this270* child node.271*/272child->ports[child->color] = node;273child->color = card->color;274child = fw_node(child->link.next);275break;276}277}278279/*280* Check that the node reports exactly one parent281* port, except for the root, which of course should282* have no parents.283*/284if ((next_sid == end && parent_count != 0) ||285(next_sid < end && parent_count != 1)) {286fw_error("Parent port inconsistency for node %d: "287"parent_count=%d\n", phy_id, parent_count);288return NULL;289}290291/* Pop the child nodes off the stack and push the new node. */292__list_del(h->prev, &stack);293list_add_tail(&node->link, &stack);294stack_depth += 1 - child_port_count;295296if (node->phy_speed == SCODE_BETA &&297parent_count + child_port_count > 1)298beta_repeaters_present = true;299300/*301* If PHYs report different gap counts, set an invalid count302* which will force a gap count reconfiguration and a reset.303*/304if (SELF_ID_GAP_COUNT(q) != gap_count)305gap_count = 0;306307update_hop_count(node);308309sid = next_sid;310phy_id++;311}312313card->root_node = node;314card->irm_node = irm_node;315card->gap_count = gap_count;316card->beta_repeaters_present = beta_repeaters_present;317318return local_node;319}320321typedef void (*fw_node_callback_t)(struct fw_card * card,322struct fw_node * node,323struct fw_node * parent);324325static void for_each_fw_node(struct fw_card *card, struct fw_node *root,326fw_node_callback_t callback)327{328struct list_head list;329struct fw_node *node, *next, *child, *parent;330int i;331332INIT_LIST_HEAD(&list);333334fw_node_get(root);335list_add_tail(&root->link, &list);336parent = NULL;337list_for_each_entry(node, &list, link) {338node->color = card->color;339340for (i = 0; i < node->port_count; i++) {341child = node->ports[i];342if (!child)343continue;344if (child->color == card->color)345parent = child;346else {347fw_node_get(child);348list_add_tail(&child->link, &list);349}350}351352callback(card, node, parent);353}354355list_for_each_entry_safe(node, next, &list, link)356fw_node_put(node);357}358359static void report_lost_node(struct fw_card *card,360struct fw_node *node, struct fw_node *parent)361{362fw_node_event(card, node, FW_NODE_DESTROYED);363fw_node_put(node);364365/* Topology has changed - reset bus manager retry counter */366card->bm_retries = 0;367}368369static void report_found_node(struct fw_card *card,370struct fw_node *node, struct fw_node *parent)371{372int b_path = (node->phy_speed == SCODE_BETA);373374if (parent != NULL) {375/* min() macro doesn't work here with gcc 3.4 */376node->max_speed = parent->max_speed < node->phy_speed ?377parent->max_speed : node->phy_speed;378node->b_path = parent->b_path && b_path;379} else {380node->max_speed = node->phy_speed;381node->b_path = b_path;382}383384fw_node_event(card, node, FW_NODE_CREATED);385386/* Topology has changed - reset bus manager retry counter */387card->bm_retries = 0;388}389390void fw_destroy_nodes(struct fw_card *card)391{392unsigned long flags;393394spin_lock_irqsave(&card->lock, flags);395card->color++;396if (card->local_node != NULL)397for_each_fw_node(card, card->local_node, report_lost_node);398card->local_node = NULL;399spin_unlock_irqrestore(&card->lock, flags);400}401402static void move_tree(struct fw_node *node0, struct fw_node *node1, int port)403{404struct fw_node *tree;405int i;406407tree = node1->ports[port];408node0->ports[port] = tree;409for (i = 0; i < tree->port_count; i++) {410if (tree->ports[i] == node1) {411tree->ports[i] = node0;412break;413}414}415}416417/*418* Compare the old topology tree for card with the new one specified by root.419* Queue the nodes and mark them as either found, lost or updated.420* Update the nodes in the card topology tree as we go.421*/422static void update_tree(struct fw_card *card, struct fw_node *root)423{424struct list_head list0, list1;425struct fw_node *node0, *node1, *next1;426int i, event;427428INIT_LIST_HEAD(&list0);429list_add_tail(&card->local_node->link, &list0);430INIT_LIST_HEAD(&list1);431list_add_tail(&root->link, &list1);432433node0 = fw_node(list0.next);434node1 = fw_node(list1.next);435436while (&node0->link != &list0) {437WARN_ON(node0->port_count != node1->port_count);438439if (node0->link_on && !node1->link_on)440event = FW_NODE_LINK_OFF;441else if (!node0->link_on && node1->link_on)442event = FW_NODE_LINK_ON;443else if (node1->initiated_reset && node1->link_on)444event = FW_NODE_INITIATED_RESET;445else446event = FW_NODE_UPDATED;447448node0->node_id = node1->node_id;449node0->color = card->color;450node0->link_on = node1->link_on;451node0->initiated_reset = node1->initiated_reset;452node0->max_hops = node1->max_hops;453node1->color = card->color;454fw_node_event(card, node0, event);455456if (card->root_node == node1)457card->root_node = node0;458if (card->irm_node == node1)459card->irm_node = node0;460461for (i = 0; i < node0->port_count; i++) {462if (node0->ports[i] && node1->ports[i]) {463/*464* This port didn't change, queue the465* connected node for further466* investigation.467*/468if (node0->ports[i]->color == card->color)469continue;470list_add_tail(&node0->ports[i]->link, &list0);471list_add_tail(&node1->ports[i]->link, &list1);472} else if (node0->ports[i]) {473/*474* The nodes connected here were475* unplugged; unref the lost nodes and476* queue FW_NODE_LOST callbacks for477* them.478*/479480for_each_fw_node(card, node0->ports[i],481report_lost_node);482node0->ports[i] = NULL;483} else if (node1->ports[i]) {484/*485* One or more node were connected to486* this port. Move the new nodes into487* the tree and queue FW_NODE_CREATED488* callbacks for them.489*/490move_tree(node0, node1, i);491for_each_fw_node(card, node0->ports[i],492report_found_node);493}494}495496node0 = fw_node(node0->link.next);497next1 = fw_node(node1->link.next);498fw_node_put(node1);499node1 = next1;500}501}502503static void update_topology_map(struct fw_card *card,504u32 *self_ids, int self_id_count)505{506int node_count = (card->root_node->node_id & 0x3f) + 1;507__be32 *map = card->topology_map;508509*map++ = cpu_to_be32((self_id_count + 2) << 16);510*map++ = cpu_to_be32(be32_to_cpu(card->topology_map[1]) + 1);511*map++ = cpu_to_be32((node_count << 16) | self_id_count);512513while (self_id_count--)514*map++ = cpu_to_be32p(self_ids++);515516fw_compute_block_crc(card->topology_map);517}518519void fw_core_handle_bus_reset(struct fw_card *card, int node_id, int generation,520int self_id_count, u32 *self_ids, bool bm_abdicate)521{522struct fw_node *local_node;523unsigned long flags;524525/*526* If the selfID buffer is not the immediate successor of the527* previously processed one, we cannot reliably compare the528* old and new topologies.529*/530if (!is_next_generation(generation, card->generation) &&531card->local_node != NULL) {532fw_notify("skipped bus generations, destroying all nodes\n");533fw_destroy_nodes(card);534card->bm_retries = 0;535}536537spin_lock_irqsave(&card->lock, flags);538539card->broadcast_channel_allocated = card->broadcast_channel_auto_allocated;540card->node_id = node_id;541/*542* Update node_id before generation to prevent anybody from using543* a stale node_id together with a current generation.544*/545smp_wmb();546card->generation = generation;547card->reset_jiffies = get_jiffies_64();548card->bm_node_id = 0xffff;549card->bm_abdicate = bm_abdicate;550fw_schedule_bm_work(card, 0);551552local_node = build_tree(card, self_ids, self_id_count);553554update_topology_map(card, self_ids, self_id_count);555556card->color++;557558if (local_node == NULL) {559fw_error("topology build failed\n");560/* FIXME: We need to issue a bus reset in this case. */561} else if (card->local_node == NULL) {562card->local_node = local_node;563for_each_fw_node(card, local_node, report_found_node);564} else {565update_tree(card, local_node);566}567568spin_unlock_irqrestore(&card->lock, flags);569}570EXPORT_SYMBOL(fw_core_handle_bus_reset);571572573